Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Portions copyright (C) 2003 Russell King, PXA MMCI Driver |
| 3 | * Portions copyright (C) 2004-2005 Pierre Ossman, W83L51xD SD/MMC driver |
| 4 | * |
| 5 | * Copyright 2008 Embedded Alley Solutions, Inc. |
| 6 | * Copyright 2009-2011 Freescale Semiconductor, Inc. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/ioport.h> |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 26 | #include <linux/of.h> |
| 27 | #include <linux/of_device.h> |
| 28 | #include <linux/of_gpio.h> |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 29 | #include <linux/platform_device.h> |
| 30 | #include <linux/delay.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | #include <linux/dma-mapping.h> |
| 33 | #include <linux/dmaengine.h> |
| 34 | #include <linux/highmem.h> |
| 35 | #include <linux/clk.h> |
| 36 | #include <linux/err.h> |
| 37 | #include <linux/completion.h> |
| 38 | #include <linux/mmc/host.h> |
| 39 | #include <linux/mmc/mmc.h> |
| 40 | #include <linux/mmc/sdio.h> |
| 41 | #include <linux/gpio.h> |
| 42 | #include <linux/regulator/consumer.h> |
Paul Gortmaker | 88b4767 | 2011-07-03 15:15:51 -0400 | [diff] [blame] | 43 | #include <linux/module.h> |
Huang Shijie | 3946860 | 2012-02-16 14:17:32 +0800 | [diff] [blame] | 44 | #include <linux/fsl/mxs-dma.h> |
Shawn Guo | 9c92cf2 | 2012-05-06 22:56:16 +0800 | [diff] [blame] | 45 | #include <linux/pinctrl/consumer.h> |
Shawn Guo | 70e6020 | 2012-05-05 19:40:09 +0800 | [diff] [blame] | 46 | #include <linux/stmp_device.h> |
Shawn Guo | 81f38ee | 2012-05-06 10:04:23 +0800 | [diff] [blame] | 47 | #include <linux/mmc/mxs-mmc.h> |
Marek Vasut | 8be3d3b | 2012-08-03 17:26:06 +0200 | [diff] [blame] | 48 | #include <linux/spi/mxs-spi.h> |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 49 | |
| 50 | #define DRIVER_NAME "mxs-mmc" |
| 51 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 52 | #define MXS_MMC_IRQ_BITS (BM_SSP_CTRL1_SDIO_IRQ | \ |
| 53 | BM_SSP_CTRL1_RESP_ERR_IRQ | \ |
| 54 | BM_SSP_CTRL1_RESP_TIMEOUT_IRQ | \ |
| 55 | BM_SSP_CTRL1_DATA_TIMEOUT_IRQ | \ |
| 56 | BM_SSP_CTRL1_DATA_CRC_IRQ | \ |
| 57 | BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ | \ |
| 58 | BM_SSP_CTRL1_RECV_TIMEOUT_IRQ | \ |
| 59 | BM_SSP_CTRL1_FIFO_OVERRUN_IRQ) |
| 60 | |
Marek Vasut | 8be3d3b | 2012-08-03 17:26:06 +0200 | [diff] [blame] | 61 | /* card detect polling timeout */ |
| 62 | #define MXS_MMC_DETECT_TIMEOUT (HZ/2) |
Shawn Guo | ef9b4d3 | 2012-05-05 20:24:01 +0800 | [diff] [blame] | 63 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 64 | struct mxs_mmc_host { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 65 | struct mxs_ssp ssp; |
| 66 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 67 | struct mmc_host *mmc; |
| 68 | struct mmc_request *mrq; |
| 69 | struct mmc_command *cmd; |
| 70 | struct mmc_data *data; |
| 71 | |
Shawn Guo | b60188c | 2012-05-06 11:25:35 +0800 | [diff] [blame] | 72 | int dma_channel; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 73 | struct dma_chan *dmach; |
| 74 | struct mxs_dma_data dma_data; |
| 75 | unsigned int dma_dir; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 76 | enum dma_transfer_direction slave_dirn; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 77 | u32 ssp_pio_words[SSP_PIO_NUM]; |
| 78 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 79 | unsigned char bus_width; |
| 80 | spinlock_t lock; |
| 81 | int sdio_irq_en; |
Shawn Guo | 31b0ff5 | 2012-05-06 13:33:40 +0800 | [diff] [blame] | 82 | int wp_gpio; |
Marek Vasut | b6e76f1 | 2012-07-19 11:11:39 -0400 | [diff] [blame] | 83 | bool wp_inverted; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static int mxs_mmc_get_ro(struct mmc_host *mmc) |
| 87 | { |
| 88 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | b6e76f1 | 2012-07-19 11:11:39 -0400 | [diff] [blame] | 89 | int ret; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 90 | |
Shawn Guo | 31b0ff5 | 2012-05-06 13:33:40 +0800 | [diff] [blame] | 91 | if (!gpio_is_valid(host->wp_gpio)) |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 92 | return -EINVAL; |
| 93 | |
Marek Vasut | b6e76f1 | 2012-07-19 11:11:39 -0400 | [diff] [blame] | 94 | ret = gpio_get_value(host->wp_gpio); |
| 95 | |
| 96 | if (host->wp_inverted) |
| 97 | ret = !ret; |
| 98 | |
| 99 | return ret; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static int mxs_mmc_get_cd(struct mmc_host *mmc) |
| 103 | { |
| 104 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 105 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 106 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 107 | return !(readl(ssp->base + HW_SSP_STATUS(ssp)) & |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 108 | BM_SSP_STATUS_CARD_DETECT); |
| 109 | } |
| 110 | |
| 111 | static void mxs_mmc_reset(struct mxs_mmc_host *host) |
| 112 | { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 113 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 114 | u32 ctrl0, ctrl1; |
| 115 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 116 | stmp_reset_block(ssp->base); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 117 | |
| 118 | ctrl0 = BM_SSP_CTRL0_IGNORE_CRC; |
| 119 | ctrl1 = BF_SSP(0x3, CTRL1_SSP_MODE) | |
| 120 | BF_SSP(0x7, CTRL1_WORD_LENGTH) | |
| 121 | BM_SSP_CTRL1_DMA_ENABLE | |
| 122 | BM_SSP_CTRL1_POLARITY | |
| 123 | BM_SSP_CTRL1_RECV_TIMEOUT_IRQ_EN | |
| 124 | BM_SSP_CTRL1_DATA_CRC_IRQ_EN | |
| 125 | BM_SSP_CTRL1_DATA_TIMEOUT_IRQ_EN | |
| 126 | BM_SSP_CTRL1_RESP_TIMEOUT_IRQ_EN | |
| 127 | BM_SSP_CTRL1_RESP_ERR_IRQ_EN; |
| 128 | |
| 129 | writel(BF_SSP(0xffff, TIMING_TIMEOUT) | |
| 130 | BF_SSP(2, TIMING_CLOCK_DIVIDE) | |
| 131 | BF_SSP(0, TIMING_CLOCK_RATE), |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 132 | ssp->base + HW_SSP_TIMING(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 133 | |
| 134 | if (host->sdio_irq_en) { |
| 135 | ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK; |
| 136 | ctrl1 |= BM_SSP_CTRL1_SDIO_IRQ_EN; |
| 137 | } |
| 138 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 139 | writel(ctrl0, ssp->base + HW_SSP_CTRL0); |
| 140 | writel(ctrl1, ssp->base + HW_SSP_CTRL1(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void mxs_mmc_start_cmd(struct mxs_mmc_host *host, |
| 144 | struct mmc_command *cmd); |
| 145 | |
| 146 | static void mxs_mmc_request_done(struct mxs_mmc_host *host) |
| 147 | { |
| 148 | struct mmc_command *cmd = host->cmd; |
| 149 | struct mmc_data *data = host->data; |
| 150 | struct mmc_request *mrq = host->mrq; |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 151 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 152 | |
| 153 | if (mmc_resp_type(cmd) & MMC_RSP_PRESENT) { |
| 154 | if (mmc_resp_type(cmd) & MMC_RSP_136) { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 155 | cmd->resp[3] = readl(ssp->base + HW_SSP_SDRESP0(ssp)); |
| 156 | cmd->resp[2] = readl(ssp->base + HW_SSP_SDRESP1(ssp)); |
| 157 | cmd->resp[1] = readl(ssp->base + HW_SSP_SDRESP2(ssp)); |
| 158 | cmd->resp[0] = readl(ssp->base + HW_SSP_SDRESP3(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 159 | } else { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 160 | cmd->resp[0] = readl(ssp->base + HW_SSP_SDRESP0(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | if (data) { |
| 165 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, |
| 166 | data->sg_len, host->dma_dir); |
| 167 | /* |
| 168 | * If there was an error on any block, we mark all |
| 169 | * data blocks as being in error. |
| 170 | */ |
| 171 | if (!data->error) |
| 172 | data->bytes_xfered = data->blocks * data->blksz; |
| 173 | else |
| 174 | data->bytes_xfered = 0; |
| 175 | |
| 176 | host->data = NULL; |
| 177 | if (mrq->stop) { |
| 178 | mxs_mmc_start_cmd(host, mrq->stop); |
| 179 | return; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | host->mrq = NULL; |
| 184 | mmc_request_done(host->mmc, mrq); |
| 185 | } |
| 186 | |
| 187 | static void mxs_mmc_dma_irq_callback(void *param) |
| 188 | { |
| 189 | struct mxs_mmc_host *host = param; |
| 190 | |
| 191 | mxs_mmc_request_done(host); |
| 192 | } |
| 193 | |
| 194 | static irqreturn_t mxs_mmc_irq_handler(int irq, void *dev_id) |
| 195 | { |
| 196 | struct mxs_mmc_host *host = dev_id; |
| 197 | struct mmc_command *cmd = host->cmd; |
| 198 | struct mmc_data *data = host->data; |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 199 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 200 | u32 stat; |
| 201 | |
| 202 | spin_lock(&host->lock); |
| 203 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 204 | stat = readl(ssp->base + HW_SSP_CTRL1(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 205 | writel(stat & MXS_MMC_IRQ_BITS, |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 206 | ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_CLR); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 207 | |
| 208 | if ((stat & BM_SSP_CTRL1_SDIO_IRQ) && (stat & BM_SSP_CTRL1_SDIO_IRQ_EN)) |
| 209 | mmc_signal_sdio_irq(host->mmc); |
| 210 | |
| 211 | spin_unlock(&host->lock); |
| 212 | |
| 213 | if (stat & BM_SSP_CTRL1_RESP_TIMEOUT_IRQ) |
| 214 | cmd->error = -ETIMEDOUT; |
| 215 | else if (stat & BM_SSP_CTRL1_RESP_ERR_IRQ) |
| 216 | cmd->error = -EIO; |
| 217 | |
| 218 | if (data) { |
| 219 | if (stat & (BM_SSP_CTRL1_DATA_TIMEOUT_IRQ | |
| 220 | BM_SSP_CTRL1_RECV_TIMEOUT_IRQ)) |
| 221 | data->error = -ETIMEDOUT; |
| 222 | else if (stat & BM_SSP_CTRL1_DATA_CRC_IRQ) |
| 223 | data->error = -EILSEQ; |
| 224 | else if (stat & (BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ | |
| 225 | BM_SSP_CTRL1_FIFO_OVERRUN_IRQ)) |
| 226 | data->error = -EIO; |
| 227 | } |
| 228 | |
| 229 | return IRQ_HANDLED; |
| 230 | } |
| 231 | |
| 232 | static struct dma_async_tx_descriptor *mxs_mmc_prep_dma( |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 233 | struct mxs_mmc_host *host, unsigned long flags) |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 234 | { |
| 235 | struct dma_async_tx_descriptor *desc; |
| 236 | struct mmc_data *data = host->data; |
| 237 | struct scatterlist * sgl; |
| 238 | unsigned int sg_len; |
| 239 | |
| 240 | if (data) { |
| 241 | /* data */ |
| 242 | dma_map_sg(mmc_dev(host->mmc), data->sg, |
| 243 | data->sg_len, host->dma_dir); |
| 244 | sgl = data->sg; |
| 245 | sg_len = data->sg_len; |
| 246 | } else { |
| 247 | /* pio */ |
| 248 | sgl = (struct scatterlist *) host->ssp_pio_words; |
| 249 | sg_len = SSP_PIO_NUM; |
| 250 | } |
| 251 | |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 252 | desc = dmaengine_prep_slave_sg(host->dmach, |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 253 | sgl, sg_len, host->slave_dirn, flags); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 254 | if (desc) { |
| 255 | desc->callback = mxs_mmc_dma_irq_callback; |
| 256 | desc->callback_param = host; |
| 257 | } else { |
| 258 | if (data) |
| 259 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, |
| 260 | data->sg_len, host->dma_dir); |
| 261 | } |
| 262 | |
| 263 | return desc; |
| 264 | } |
| 265 | |
| 266 | static void mxs_mmc_bc(struct mxs_mmc_host *host) |
| 267 | { |
| 268 | struct mmc_command *cmd = host->cmd; |
| 269 | struct dma_async_tx_descriptor *desc; |
| 270 | u32 ctrl0, cmd0, cmd1; |
| 271 | |
| 272 | ctrl0 = BM_SSP_CTRL0_ENABLE | BM_SSP_CTRL0_IGNORE_CRC; |
| 273 | cmd0 = BF_SSP(cmd->opcode, CMD0_CMD) | BM_SSP_CMD0_APPEND_8CYC; |
| 274 | cmd1 = cmd->arg; |
| 275 | |
| 276 | if (host->sdio_irq_en) { |
| 277 | ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK; |
| 278 | cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN; |
| 279 | } |
| 280 | |
| 281 | host->ssp_pio_words[0] = ctrl0; |
| 282 | host->ssp_pio_words[1] = cmd0; |
| 283 | host->ssp_pio_words[2] = cmd1; |
| 284 | host->dma_dir = DMA_NONE; |
Shawn Guo | a4e3e86 | 2011-12-13 23:48:04 +0800 | [diff] [blame] | 285 | host->slave_dirn = DMA_TRANS_NONE; |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 286 | desc = mxs_mmc_prep_dma(host, DMA_CTRL_ACK); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 287 | if (!desc) |
| 288 | goto out; |
| 289 | |
| 290 | dmaengine_submit(desc); |
Shawn Guo | d04525e | 2012-04-11 13:29:31 +0800 | [diff] [blame] | 291 | dma_async_issue_pending(host->dmach); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 292 | return; |
| 293 | |
| 294 | out: |
| 295 | dev_warn(mmc_dev(host->mmc), |
| 296 | "%s: failed to prep dma\n", __func__); |
| 297 | } |
| 298 | |
| 299 | static void mxs_mmc_ac(struct mxs_mmc_host *host) |
| 300 | { |
| 301 | struct mmc_command *cmd = host->cmd; |
| 302 | struct dma_async_tx_descriptor *desc; |
| 303 | u32 ignore_crc, get_resp, long_resp; |
| 304 | u32 ctrl0, cmd0, cmd1; |
| 305 | |
| 306 | ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ? |
| 307 | 0 : BM_SSP_CTRL0_IGNORE_CRC; |
| 308 | get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ? |
| 309 | BM_SSP_CTRL0_GET_RESP : 0; |
| 310 | long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ? |
| 311 | BM_SSP_CTRL0_LONG_RESP : 0; |
| 312 | |
| 313 | ctrl0 = BM_SSP_CTRL0_ENABLE | ignore_crc | get_resp | long_resp; |
| 314 | cmd0 = BF_SSP(cmd->opcode, CMD0_CMD); |
| 315 | cmd1 = cmd->arg; |
| 316 | |
| 317 | if (host->sdio_irq_en) { |
| 318 | ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK; |
| 319 | cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN; |
| 320 | } |
| 321 | |
| 322 | host->ssp_pio_words[0] = ctrl0; |
| 323 | host->ssp_pio_words[1] = cmd0; |
| 324 | host->ssp_pio_words[2] = cmd1; |
| 325 | host->dma_dir = DMA_NONE; |
Shawn Guo | a4e3e86 | 2011-12-13 23:48:04 +0800 | [diff] [blame] | 326 | host->slave_dirn = DMA_TRANS_NONE; |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 327 | desc = mxs_mmc_prep_dma(host, DMA_CTRL_ACK); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 328 | if (!desc) |
| 329 | goto out; |
| 330 | |
| 331 | dmaengine_submit(desc); |
Shawn Guo | d04525e | 2012-04-11 13:29:31 +0800 | [diff] [blame] | 332 | dma_async_issue_pending(host->dmach); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 333 | return; |
| 334 | |
| 335 | out: |
| 336 | dev_warn(mmc_dev(host->mmc), |
| 337 | "%s: failed to prep dma\n", __func__); |
| 338 | } |
| 339 | |
| 340 | static unsigned short mxs_ns_to_ssp_ticks(unsigned clock_rate, unsigned ns) |
| 341 | { |
| 342 | const unsigned int ssp_timeout_mul = 4096; |
| 343 | /* |
| 344 | * Calculate ticks in ms since ns are large numbers |
| 345 | * and might overflow |
| 346 | */ |
| 347 | const unsigned int clock_per_ms = clock_rate / 1000; |
| 348 | const unsigned int ms = ns / 1000; |
| 349 | const unsigned int ticks = ms * clock_per_ms; |
| 350 | const unsigned int ssp_ticks = ticks / ssp_timeout_mul; |
| 351 | |
| 352 | WARN_ON(ssp_ticks == 0); |
| 353 | return ssp_ticks; |
| 354 | } |
| 355 | |
| 356 | static void mxs_mmc_adtc(struct mxs_mmc_host *host) |
| 357 | { |
| 358 | struct mmc_command *cmd = host->cmd; |
| 359 | struct mmc_data *data = cmd->data; |
| 360 | struct dma_async_tx_descriptor *desc; |
| 361 | struct scatterlist *sgl = data->sg, *sg; |
| 362 | unsigned int sg_len = data->sg_len; |
| 363 | int i; |
| 364 | |
| 365 | unsigned short dma_data_dir, timeout; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 366 | enum dma_transfer_direction slave_dirn; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 367 | unsigned int data_size = 0, log2_blksz; |
| 368 | unsigned int blocks = data->blocks; |
| 369 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 370 | struct mxs_ssp *ssp = &host->ssp; |
| 371 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 372 | u32 ignore_crc, get_resp, long_resp, read; |
| 373 | u32 ctrl0, cmd0, cmd1, val; |
| 374 | |
| 375 | ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ? |
| 376 | 0 : BM_SSP_CTRL0_IGNORE_CRC; |
| 377 | get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ? |
| 378 | BM_SSP_CTRL0_GET_RESP : 0; |
| 379 | long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ? |
| 380 | BM_SSP_CTRL0_LONG_RESP : 0; |
| 381 | |
| 382 | if (data->flags & MMC_DATA_WRITE) { |
| 383 | dma_data_dir = DMA_TO_DEVICE; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 384 | slave_dirn = DMA_MEM_TO_DEV; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 385 | read = 0; |
| 386 | } else { |
| 387 | dma_data_dir = DMA_FROM_DEVICE; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 388 | slave_dirn = DMA_DEV_TO_MEM; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 389 | read = BM_SSP_CTRL0_READ; |
| 390 | } |
| 391 | |
| 392 | ctrl0 = BF_SSP(host->bus_width, CTRL0_BUS_WIDTH) | |
| 393 | ignore_crc | get_resp | long_resp | |
| 394 | BM_SSP_CTRL0_DATA_XFER | read | |
| 395 | BM_SSP_CTRL0_WAIT_FOR_IRQ | |
| 396 | BM_SSP_CTRL0_ENABLE; |
| 397 | |
| 398 | cmd0 = BF_SSP(cmd->opcode, CMD0_CMD); |
| 399 | |
| 400 | /* get logarithm to base 2 of block size for setting register */ |
| 401 | log2_blksz = ilog2(data->blksz); |
| 402 | |
| 403 | /* |
| 404 | * take special care of the case that data size from data->sg |
| 405 | * is not equal to blocks x blksz |
| 406 | */ |
| 407 | for_each_sg(sgl, sg, sg_len, i) |
| 408 | data_size += sg->length; |
| 409 | |
| 410 | if (data_size != data->blocks * data->blksz) |
| 411 | blocks = 1; |
| 412 | |
| 413 | /* xfer count, block size and count need to be set differently */ |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 414 | if (ssp_is_old(ssp)) { |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 415 | ctrl0 |= BF_SSP(data_size, CTRL0_XFER_COUNT); |
| 416 | cmd0 |= BF_SSP(log2_blksz, CMD0_BLOCK_SIZE) | |
| 417 | BF_SSP(blocks - 1, CMD0_BLOCK_COUNT); |
| 418 | } else { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 419 | writel(data_size, ssp->base + HW_SSP_XFER_SIZE); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 420 | writel(BF_SSP(log2_blksz, BLOCK_SIZE_BLOCK_SIZE) | |
| 421 | BF_SSP(blocks - 1, BLOCK_SIZE_BLOCK_COUNT), |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 422 | ssp->base + HW_SSP_BLOCK_SIZE); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | if ((cmd->opcode == MMC_STOP_TRANSMISSION) || |
| 426 | (cmd->opcode == SD_IO_RW_EXTENDED)) |
| 427 | cmd0 |= BM_SSP_CMD0_APPEND_8CYC; |
| 428 | |
| 429 | cmd1 = cmd->arg; |
| 430 | |
| 431 | if (host->sdio_irq_en) { |
| 432 | ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK; |
| 433 | cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN; |
| 434 | } |
| 435 | |
| 436 | /* set the timeout count */ |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 437 | timeout = mxs_ns_to_ssp_ticks(ssp->clk_rate, data->timeout_ns); |
| 438 | val = readl(ssp->base + HW_SSP_TIMING(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 439 | val &= ~(BM_SSP_TIMING_TIMEOUT); |
| 440 | val |= BF_SSP(timeout, TIMING_TIMEOUT); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 441 | writel(val, ssp->base + HW_SSP_TIMING(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 442 | |
| 443 | /* pio */ |
| 444 | host->ssp_pio_words[0] = ctrl0; |
| 445 | host->ssp_pio_words[1] = cmd0; |
| 446 | host->ssp_pio_words[2] = cmd1; |
| 447 | host->dma_dir = DMA_NONE; |
Shawn Guo | a4e3e86 | 2011-12-13 23:48:04 +0800 | [diff] [blame] | 448 | host->slave_dirn = DMA_TRANS_NONE; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 449 | desc = mxs_mmc_prep_dma(host, 0); |
| 450 | if (!desc) |
| 451 | goto out; |
| 452 | |
| 453 | /* append data sg */ |
| 454 | WARN_ON(host->data != NULL); |
| 455 | host->data = data; |
| 456 | host->dma_dir = dma_data_dir; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 457 | host->slave_dirn = slave_dirn; |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 458 | desc = mxs_mmc_prep_dma(host, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 459 | if (!desc) |
| 460 | goto out; |
| 461 | |
| 462 | dmaengine_submit(desc); |
Shawn Guo | d04525e | 2012-04-11 13:29:31 +0800 | [diff] [blame] | 463 | dma_async_issue_pending(host->dmach); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 464 | return; |
| 465 | out: |
| 466 | dev_warn(mmc_dev(host->mmc), |
| 467 | "%s: failed to prep dma\n", __func__); |
| 468 | } |
| 469 | |
| 470 | static void mxs_mmc_start_cmd(struct mxs_mmc_host *host, |
| 471 | struct mmc_command *cmd) |
| 472 | { |
| 473 | host->cmd = cmd; |
| 474 | |
| 475 | switch (mmc_cmd_type(cmd)) { |
| 476 | case MMC_CMD_BC: |
| 477 | mxs_mmc_bc(host); |
| 478 | break; |
| 479 | case MMC_CMD_BCR: |
| 480 | mxs_mmc_ac(host); |
| 481 | break; |
| 482 | case MMC_CMD_AC: |
| 483 | mxs_mmc_ac(host); |
| 484 | break; |
| 485 | case MMC_CMD_ADTC: |
| 486 | mxs_mmc_adtc(host); |
| 487 | break; |
| 488 | default: |
| 489 | dev_warn(mmc_dev(host->mmc), |
| 490 | "%s: unknown MMC command\n", __func__); |
| 491 | break; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | static void mxs_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 496 | { |
| 497 | struct mxs_mmc_host *host = mmc_priv(mmc); |
| 498 | |
| 499 | WARN_ON(host->mrq != NULL); |
| 500 | host->mrq = mrq; |
| 501 | mxs_mmc_start_cmd(host, mrq->cmd); |
| 502 | } |
| 503 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 504 | static void mxs_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 505 | { |
| 506 | struct mxs_mmc_host *host = mmc_priv(mmc); |
| 507 | |
| 508 | if (ios->bus_width == MMC_BUS_WIDTH_8) |
| 509 | host->bus_width = 2; |
| 510 | else if (ios->bus_width == MMC_BUS_WIDTH_4) |
| 511 | host->bus_width = 1; |
| 512 | else |
| 513 | host->bus_width = 0; |
| 514 | |
| 515 | if (ios->clock) |
Marek Vasut | 1308239 | 2012-08-03 17:26:10 +0200 | [diff] [blame^] | 516 | mxs_ssp_set_clk_rate(&host->ssp, ios->clock); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | static void mxs_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable) |
| 520 | { |
| 521 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 522 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 523 | unsigned long flags; |
| 524 | |
| 525 | spin_lock_irqsave(&host->lock, flags); |
| 526 | |
| 527 | host->sdio_irq_en = enable; |
| 528 | |
| 529 | if (enable) { |
| 530 | writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK, |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 531 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 532 | writel(BM_SSP_CTRL1_SDIO_IRQ_EN, |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 533 | ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_SET); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 534 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 535 | if (readl(ssp->base + HW_SSP_STATUS(ssp)) & |
Shawn Guo | e0bf141 | 2012-05-06 09:36:39 +0800 | [diff] [blame] | 536 | BM_SSP_STATUS_SDIO_IRQ) |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 537 | mmc_signal_sdio_irq(host->mmc); |
| 538 | |
| 539 | } else { |
| 540 | writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK, |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 541 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 542 | writel(BM_SSP_CTRL1_SDIO_IRQ_EN, |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 543 | ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_CLR); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | spin_unlock_irqrestore(&host->lock, flags); |
| 547 | } |
| 548 | |
| 549 | static const struct mmc_host_ops mxs_mmc_ops = { |
| 550 | .request = mxs_mmc_request, |
| 551 | .get_ro = mxs_mmc_get_ro, |
| 552 | .get_cd = mxs_mmc_get_cd, |
| 553 | .set_ios = mxs_mmc_set_ios, |
| 554 | .enable_sdio_irq = mxs_mmc_enable_sdio_irq, |
| 555 | }; |
| 556 | |
| 557 | static bool mxs_mmc_dma_filter(struct dma_chan *chan, void *param) |
| 558 | { |
| 559 | struct mxs_mmc_host *host = param; |
| 560 | |
| 561 | if (!mxs_dma_is_apbh(chan)) |
| 562 | return false; |
| 563 | |
Shawn Guo | b60188c | 2012-05-06 11:25:35 +0800 | [diff] [blame] | 564 | if (chan->chan_id != host->dma_channel) |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 565 | return false; |
| 566 | |
| 567 | chan->private = &host->dma_data; |
| 568 | |
| 569 | return true; |
| 570 | } |
| 571 | |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 572 | static struct platform_device_id mxs_ssp_ids[] = { |
Shawn Guo | ef9b4d3 | 2012-05-05 20:24:01 +0800 | [diff] [blame] | 573 | { |
| 574 | .name = "imx23-mmc", |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 575 | .driver_data = IMX23_SSP, |
Shawn Guo | ef9b4d3 | 2012-05-05 20:24:01 +0800 | [diff] [blame] | 576 | }, { |
| 577 | .name = "imx28-mmc", |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 578 | .driver_data = IMX28_SSP, |
Shawn Guo | ef9b4d3 | 2012-05-05 20:24:01 +0800 | [diff] [blame] | 579 | }, { |
| 580 | /* sentinel */ |
| 581 | } |
| 582 | }; |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 583 | MODULE_DEVICE_TABLE(platform, mxs_ssp_ids); |
Shawn Guo | ef9b4d3 | 2012-05-05 20:24:01 +0800 | [diff] [blame] | 584 | |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 585 | static const struct of_device_id mxs_mmc_dt_ids[] = { |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 586 | { .compatible = "fsl,imx23-mmc", .data = (void *) IMX23_SSP, }, |
| 587 | { .compatible = "fsl,imx28-mmc", .data = (void *) IMX28_SSP, }, |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 588 | { /* sentinel */ } |
| 589 | }; |
| 590 | MODULE_DEVICE_TABLE(of, mxs_mmc_dt_ids); |
| 591 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 592 | static int mxs_mmc_probe(struct platform_device *pdev) |
| 593 | { |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 594 | const struct of_device_id *of_id = |
| 595 | of_match_device(mxs_mmc_dt_ids, &pdev->dev); |
| 596 | struct device_node *np = pdev->dev.of_node; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 597 | struct mxs_mmc_host *host; |
| 598 | struct mmc_host *mmc; |
Shawn Guo | df06bfc | 2012-05-06 11:20:40 +0800 | [diff] [blame] | 599 | struct resource *iores, *dmares; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 600 | struct mxs_mmc_platform_data *pdata; |
Shawn Guo | 9c92cf2 | 2012-05-06 22:56:16 +0800 | [diff] [blame] | 601 | struct pinctrl *pinctrl; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 602 | int ret = 0, irq_err, irq_dma; |
| 603 | dma_cap_mask_t mask; |
Shawn Guo | 4dc5a79 | 2012-06-26 16:38:57 +0800 | [diff] [blame] | 604 | struct regulator *reg_vmmc; |
Marek Vasut | b6e76f1 | 2012-07-19 11:11:39 -0400 | [diff] [blame] | 605 | enum of_gpio_flags flags; |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 606 | struct mxs_ssp *ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 607 | |
| 608 | iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 609 | dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 610 | irq_err = platform_get_irq(pdev, 0); |
| 611 | irq_dma = platform_get_irq(pdev, 1); |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 612 | if (!iores || irq_err < 0 || irq_dma < 0) |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 613 | return -EINVAL; |
| 614 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 615 | mmc = mmc_alloc_host(sizeof(struct mxs_mmc_host), &pdev->dev); |
Shawn Guo | df06bfc | 2012-05-06 11:20:40 +0800 | [diff] [blame] | 616 | if (!mmc) |
| 617 | return -ENOMEM; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 618 | |
| 619 | host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 620 | ssp = &host->ssp; |
| 621 | ssp->dev = &pdev->dev; |
| 622 | ssp->base = devm_request_and_ioremap(&pdev->dev, iores); |
| 623 | if (!ssp->base) { |
Shawn Guo | df06bfc | 2012-05-06 11:20:40 +0800 | [diff] [blame] | 624 | ret = -EADDRNOTAVAIL; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 625 | goto out_mmc_free; |
| 626 | } |
| 627 | |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 628 | if (np) { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 629 | ssp->devid = (enum mxs_ssp_id) of_id->data; |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 630 | /* |
| 631 | * TODO: This is a temporary solution and should be changed |
| 632 | * to use generic DMA binding later when the helpers get in. |
| 633 | */ |
| 634 | ret = of_property_read_u32(np, "fsl,ssp-dma-channel", |
| 635 | &host->dma_channel); |
| 636 | if (ret) { |
| 637 | dev_err(mmc_dev(host->mmc), |
| 638 | "failed to get dma channel\n"); |
| 639 | goto out_mmc_free; |
| 640 | } |
| 641 | } else { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 642 | ssp->devid = pdev->id_entry->driver_data; |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 643 | host->dma_channel = dmares->start; |
| 644 | } |
| 645 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 646 | host->mmc = mmc; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 647 | host->sdio_irq_en = 0; |
| 648 | |
Shawn Guo | 4dc5a79 | 2012-06-26 16:38:57 +0800 | [diff] [blame] | 649 | reg_vmmc = devm_regulator_get(&pdev->dev, "vmmc"); |
| 650 | if (!IS_ERR(reg_vmmc)) { |
| 651 | ret = regulator_enable(reg_vmmc); |
| 652 | if (ret) { |
| 653 | dev_err(&pdev->dev, |
| 654 | "Failed to enable vmmc regulator: %d\n", ret); |
| 655 | goto out_mmc_free; |
| 656 | } |
| 657 | } |
| 658 | |
Shawn Guo | 9c92cf2 | 2012-05-06 22:56:16 +0800 | [diff] [blame] | 659 | pinctrl = devm_pinctrl_get_select_default(&pdev->dev); |
| 660 | if (IS_ERR(pinctrl)) { |
| 661 | ret = PTR_ERR(pinctrl); |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 662 | goto out_mmc_free; |
Shawn Guo | 9c92cf2 | 2012-05-06 22:56:16 +0800 | [diff] [blame] | 663 | } |
| 664 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 665 | ssp->clk = clk_get(&pdev->dev, NULL); |
| 666 | if (IS_ERR(ssp->clk)) { |
| 667 | ret = PTR_ERR(ssp->clk); |
Shawn Guo | df06bfc | 2012-05-06 11:20:40 +0800 | [diff] [blame] | 668 | goto out_mmc_free; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 669 | } |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 670 | clk_prepare_enable(ssp->clk); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 671 | |
| 672 | mxs_mmc_reset(host); |
| 673 | |
| 674 | dma_cap_zero(mask); |
| 675 | dma_cap_set(DMA_SLAVE, mask); |
| 676 | host->dma_data.chan_irq = irq_dma; |
| 677 | host->dmach = dma_request_channel(mask, mxs_mmc_dma_filter, host); |
| 678 | if (!host->dmach) { |
| 679 | dev_err(mmc_dev(host->mmc), |
| 680 | "%s: failed to request dma\n", __func__); |
| 681 | goto out_clk_put; |
| 682 | } |
| 683 | |
| 684 | /* set mmc core parameters */ |
| 685 | mmc->ops = &mxs_mmc_ops; |
| 686 | mmc->caps = MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED | |
| 687 | MMC_CAP_SDIO_IRQ | MMC_CAP_NEEDS_POLL; |
| 688 | |
| 689 | pdata = mmc_dev(host->mmc)->platform_data; |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 690 | if (!pdata) { |
| 691 | u32 bus_width = 0; |
| 692 | of_property_read_u32(np, "bus-width", &bus_width); |
| 693 | if (bus_width == 4) |
| 694 | mmc->caps |= MMC_CAP_4_BIT_DATA; |
| 695 | else if (bus_width == 8) |
| 696 | mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA; |
Marek Vasut | b6e76f1 | 2012-07-19 11:11:39 -0400 | [diff] [blame] | 697 | host->wp_gpio = of_get_named_gpio_flags(np, "wp-gpios", 0, |
| 698 | &flags); |
| 699 | if (flags & OF_GPIO_ACTIVE_LOW) |
| 700 | host->wp_inverted = 1; |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 701 | } else { |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 702 | if (pdata->flags & SLOTF_8_BIT_CAPABLE) |
| 703 | mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA; |
| 704 | if (pdata->flags & SLOTF_4_BIT_CAPABLE) |
| 705 | mmc->caps |= MMC_CAP_4_BIT_DATA; |
Shawn Guo | 31b0ff5 | 2012-05-06 13:33:40 +0800 | [diff] [blame] | 706 | host->wp_gpio = pdata->wp_gpio; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | mmc->f_min = 400000; |
| 710 | mmc->f_max = 288000000; |
| 711 | mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 712 | |
| 713 | mmc->max_segs = 52; |
| 714 | mmc->max_blk_size = 1 << 0xf; |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 715 | mmc->max_blk_count = (ssp_is_old(ssp)) ? 0xff : 0xffffff; |
| 716 | mmc->max_req_size = (ssp_is_old(ssp)) ? 0xffff : 0xffffffff; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 717 | mmc->max_seg_size = dma_get_max_seg_size(host->dmach->device->dev); |
| 718 | |
| 719 | platform_set_drvdata(pdev, mmc); |
| 720 | |
Shawn Guo | df06bfc | 2012-05-06 11:20:40 +0800 | [diff] [blame] | 721 | ret = devm_request_irq(&pdev->dev, irq_err, mxs_mmc_irq_handler, 0, |
| 722 | DRIVER_NAME, host); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 723 | if (ret) |
| 724 | goto out_free_dma; |
| 725 | |
| 726 | spin_lock_init(&host->lock); |
| 727 | |
| 728 | ret = mmc_add_host(mmc); |
| 729 | if (ret) |
Shawn Guo | df06bfc | 2012-05-06 11:20:40 +0800 | [diff] [blame] | 730 | goto out_free_dma; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 731 | |
| 732 | dev_info(mmc_dev(host->mmc), "initialized\n"); |
| 733 | |
| 734 | return 0; |
| 735 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 736 | out_free_dma: |
| 737 | if (host->dmach) |
| 738 | dma_release_channel(host->dmach); |
| 739 | out_clk_put: |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 740 | clk_disable_unprepare(ssp->clk); |
| 741 | clk_put(ssp->clk); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 742 | out_mmc_free: |
| 743 | mmc_free_host(mmc); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 744 | return ret; |
| 745 | } |
| 746 | |
| 747 | static int mxs_mmc_remove(struct platform_device *pdev) |
| 748 | { |
| 749 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
| 750 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 751 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 752 | |
| 753 | mmc_remove_host(mmc); |
| 754 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 755 | platform_set_drvdata(pdev, NULL); |
| 756 | |
| 757 | if (host->dmach) |
| 758 | dma_release_channel(host->dmach); |
| 759 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 760 | clk_disable_unprepare(ssp->clk); |
| 761 | clk_put(ssp->clk); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 762 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 763 | mmc_free_host(mmc); |
| 764 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | #ifdef CONFIG_PM |
| 769 | static int mxs_mmc_suspend(struct device *dev) |
| 770 | { |
| 771 | struct mmc_host *mmc = dev_get_drvdata(dev); |
| 772 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 773 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 774 | int ret = 0; |
| 775 | |
| 776 | ret = mmc_suspend_host(mmc); |
| 777 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 778 | clk_disable_unprepare(ssp->clk); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 779 | |
| 780 | return ret; |
| 781 | } |
| 782 | |
| 783 | static int mxs_mmc_resume(struct device *dev) |
| 784 | { |
| 785 | struct mmc_host *mmc = dev_get_drvdata(dev); |
| 786 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 787 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 788 | int ret = 0; |
| 789 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 790 | clk_prepare_enable(ssp->clk); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 791 | |
| 792 | ret = mmc_resume_host(mmc); |
| 793 | |
| 794 | return ret; |
| 795 | } |
| 796 | |
| 797 | static const struct dev_pm_ops mxs_mmc_pm_ops = { |
| 798 | .suspend = mxs_mmc_suspend, |
| 799 | .resume = mxs_mmc_resume, |
| 800 | }; |
| 801 | #endif |
| 802 | |
| 803 | static struct platform_driver mxs_mmc_driver = { |
| 804 | .probe = mxs_mmc_probe, |
| 805 | .remove = mxs_mmc_remove, |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 806 | .id_table = mxs_ssp_ids, |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 807 | .driver = { |
| 808 | .name = DRIVER_NAME, |
| 809 | .owner = THIS_MODULE, |
| 810 | #ifdef CONFIG_PM |
| 811 | .pm = &mxs_mmc_pm_ops, |
| 812 | #endif |
Marek Vasut | a3e545e | 2012-05-21 06:33:27 +0200 | [diff] [blame] | 813 | .of_match_table = mxs_mmc_dt_ids, |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 814 | }, |
| 815 | }; |
| 816 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 817 | module_platform_driver(mxs_mmc_driver); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 818 | |
| 819 | MODULE_DESCRIPTION("FREESCALE MXS MMC peripheral"); |
| 820 | MODULE_AUTHOR("Freescale Semiconductor"); |
| 821 | MODULE_LICENSE("GPL"); |