2 * Portions copyright (C) 2003 Russell King, PXA MMCI Driver
3 * Portions copyright (C) 2004-2005 Pierre Ossman, W83L51xD SD/MMC driver
5 * Copyright 2008 Embedded Alley Solutions, Inc.
6 * Copyright 2009-2011 Freescale Semiconductor, Inc.
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.
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.
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.
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/ioport.h>
27 #include <linux/of_device.h>
28 #include <linux/of_gpio.h>
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>
43 #include <linux/module.h>
44 #include <linux/fsl/mxs-dma.h>
45 #include <linux/pinctrl/consumer.h>
46 #include <linux/stmp_device.h>
47 #include <linux/mmc/mxs-mmc.h>
49 #define DRIVER_NAME "mxs-mmc"
51 /* card detect polling timeout */
52 #define MXS_MMC_DETECT_TIMEOUT (HZ/2)
54 #define ssp_is_old(host) ((host)->devid == IMX23_MMC)
57 #define HW_SSP_CTRL0 0x000
58 #define BM_SSP_CTRL0_RUN (1 << 29)
59 #define BM_SSP_CTRL0_SDIO_IRQ_CHECK (1 << 28)
60 #define BM_SSP_CTRL0_IGNORE_CRC (1 << 26)
61 #define BM_SSP_CTRL0_READ (1 << 25)
62 #define BM_SSP_CTRL0_DATA_XFER (1 << 24)
63 #define BP_SSP_CTRL0_BUS_WIDTH (22)
64 #define BM_SSP_CTRL0_BUS_WIDTH (0x3 << 22)
65 #define BM_SSP_CTRL0_WAIT_FOR_IRQ (1 << 21)
66 #define BM_SSP_CTRL0_LONG_RESP (1 << 19)
67 #define BM_SSP_CTRL0_GET_RESP (1 << 17)
68 #define BM_SSP_CTRL0_ENABLE (1 << 16)
69 #define BP_SSP_CTRL0_XFER_COUNT (0)
70 #define BM_SSP_CTRL0_XFER_COUNT (0xffff)
71 #define HW_SSP_CMD0 0x010
72 #define BM_SSP_CMD0_DBL_DATA_RATE_EN (1 << 25)
73 #define BM_SSP_CMD0_SLOW_CLKING_EN (1 << 22)
74 #define BM_SSP_CMD0_CONT_CLKING_EN (1 << 21)
75 #define BM_SSP_CMD0_APPEND_8CYC (1 << 20)
76 #define BP_SSP_CMD0_BLOCK_SIZE (16)
77 #define BM_SSP_CMD0_BLOCK_SIZE (0xf << 16)
78 #define BP_SSP_CMD0_BLOCK_COUNT (8)
79 #define BM_SSP_CMD0_BLOCK_COUNT (0xff << 8)
80 #define BP_SSP_CMD0_CMD (0)
81 #define BM_SSP_CMD0_CMD (0xff)
82 #define HW_SSP_CMD1 0x020
83 #define HW_SSP_XFER_SIZE 0x030
84 #define HW_SSP_BLOCK_SIZE 0x040
85 #define BP_SSP_BLOCK_SIZE_BLOCK_COUNT (4)
86 #define BM_SSP_BLOCK_SIZE_BLOCK_COUNT (0xffffff << 4)
87 #define BP_SSP_BLOCK_SIZE_BLOCK_SIZE (0)
88 #define BM_SSP_BLOCK_SIZE_BLOCK_SIZE (0xf)
89 #define HW_SSP_TIMING(h) (ssp_is_old(h) ? 0x050 : 0x070)
90 #define BP_SSP_TIMING_TIMEOUT (16)
91 #define BM_SSP_TIMING_TIMEOUT (0xffff << 16)
92 #define BP_SSP_TIMING_CLOCK_DIVIDE (8)
93 #define BM_SSP_TIMING_CLOCK_DIVIDE (0xff << 8)
94 #define BP_SSP_TIMING_CLOCK_RATE (0)
95 #define BM_SSP_TIMING_CLOCK_RATE (0xff)
96 #define HW_SSP_CTRL1(h) (ssp_is_old(h) ? 0x060 : 0x080)
97 #define BM_SSP_CTRL1_SDIO_IRQ (1 << 31)
98 #define BM_SSP_CTRL1_SDIO_IRQ_EN (1 << 30)
99 #define BM_SSP_CTRL1_RESP_ERR_IRQ (1 << 29)
100 #define BM_SSP_CTRL1_RESP_ERR_IRQ_EN (1 << 28)
101 #define BM_SSP_CTRL1_RESP_TIMEOUT_IRQ (1 << 27)
102 #define BM_SSP_CTRL1_RESP_TIMEOUT_IRQ_EN (1 << 26)
103 #define BM_SSP_CTRL1_DATA_TIMEOUT_IRQ (1 << 25)
104 #define BM_SSP_CTRL1_DATA_TIMEOUT_IRQ_EN (1 << 24)
105 #define BM_SSP_CTRL1_DATA_CRC_IRQ (1 << 23)
106 #define BM_SSP_CTRL1_DATA_CRC_IRQ_EN (1 << 22)
107 #define BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ (1 << 21)
108 #define BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ_EN (1 << 20)
109 #define BM_SSP_CTRL1_RECV_TIMEOUT_IRQ (1 << 17)
110 #define BM_SSP_CTRL1_RECV_TIMEOUT_IRQ_EN (1 << 16)
111 #define BM_SSP_CTRL1_FIFO_OVERRUN_IRQ (1 << 15)
112 #define BM_SSP_CTRL1_FIFO_OVERRUN_IRQ_EN (1 << 14)
113 #define BM_SSP_CTRL1_DMA_ENABLE (1 << 13)
114 #define BM_SSP_CTRL1_POLARITY (1 << 9)
115 #define BP_SSP_CTRL1_WORD_LENGTH (4)
116 #define BM_SSP_CTRL1_WORD_LENGTH (0xf << 4)
117 #define BP_SSP_CTRL1_SSP_MODE (0)
118 #define BM_SSP_CTRL1_SSP_MODE (0xf)
119 #define HW_SSP_SDRESP0(h) (ssp_is_old(h) ? 0x080 : 0x0a0)
120 #define HW_SSP_SDRESP1(h) (ssp_is_old(h) ? 0x090 : 0x0b0)
121 #define HW_SSP_SDRESP2(h) (ssp_is_old(h) ? 0x0a0 : 0x0c0)
122 #define HW_SSP_SDRESP3(h) (ssp_is_old(h) ? 0x0b0 : 0x0d0)
123 #define HW_SSP_STATUS(h) (ssp_is_old(h) ? 0x0c0 : 0x100)
124 #define BM_SSP_STATUS_CARD_DETECT (1 << 28)
125 #define BM_SSP_STATUS_SDIO_IRQ (1 << 17)
127 #define BF_SSP(value, field) (((value) << BP_SSP_##field) & BM_SSP_##field)
129 #define MXS_MMC_IRQ_BITS (BM_SSP_CTRL1_SDIO_IRQ | \
130 BM_SSP_CTRL1_RESP_ERR_IRQ | \
131 BM_SSP_CTRL1_RESP_TIMEOUT_IRQ | \
132 BM_SSP_CTRL1_DATA_TIMEOUT_IRQ | \
133 BM_SSP_CTRL1_DATA_CRC_IRQ | \
134 BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ | \
135 BM_SSP_CTRL1_RECV_TIMEOUT_IRQ | \
136 BM_SSP_CTRL1_FIFO_OVERRUN_IRQ)
138 #define SSP_PIO_NUM 3
145 struct mxs_mmc_host {
146 struct mmc_host *mmc;
147 struct mmc_request *mrq;
148 struct mmc_command *cmd;
149 struct mmc_data *data;
154 unsigned int clk_rate;
156 struct dma_chan *dmach;
157 struct mxs_dma_data dma_data;
158 unsigned int dma_dir;
159 enum dma_transfer_direction slave_dirn;
160 u32 ssp_pio_words[SSP_PIO_NUM];
162 enum mxs_mmc_id devid;
163 unsigned char bus_width;
170 static int mxs_mmc_get_ro(struct mmc_host *mmc)
172 struct mxs_mmc_host *host = mmc_priv(mmc);
175 if (!gpio_is_valid(host->wp_gpio))
178 ret = gpio_get_value(host->wp_gpio);
180 if (host->wp_inverted)
186 static int mxs_mmc_get_cd(struct mmc_host *mmc)
188 struct mxs_mmc_host *host = mmc_priv(mmc);
190 return !(readl(host->base + HW_SSP_STATUS(host)) &
191 BM_SSP_STATUS_CARD_DETECT);
194 static void mxs_mmc_reset(struct mxs_mmc_host *host)
198 stmp_reset_block(host->base);
200 ctrl0 = BM_SSP_CTRL0_IGNORE_CRC;
201 ctrl1 = BF_SSP(0x3, CTRL1_SSP_MODE) |
202 BF_SSP(0x7, CTRL1_WORD_LENGTH) |
203 BM_SSP_CTRL1_DMA_ENABLE |
204 BM_SSP_CTRL1_POLARITY |
205 BM_SSP_CTRL1_RECV_TIMEOUT_IRQ_EN |
206 BM_SSP_CTRL1_DATA_CRC_IRQ_EN |
207 BM_SSP_CTRL1_DATA_TIMEOUT_IRQ_EN |
208 BM_SSP_CTRL1_RESP_TIMEOUT_IRQ_EN |
209 BM_SSP_CTRL1_RESP_ERR_IRQ_EN;
211 writel(BF_SSP(0xffff, TIMING_TIMEOUT) |
212 BF_SSP(2, TIMING_CLOCK_DIVIDE) |
213 BF_SSP(0, TIMING_CLOCK_RATE),
214 host->base + HW_SSP_TIMING(host));
216 if (host->sdio_irq_en) {
217 ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
218 ctrl1 |= BM_SSP_CTRL1_SDIO_IRQ_EN;
221 writel(ctrl0, host->base + HW_SSP_CTRL0);
222 writel(ctrl1, host->base + HW_SSP_CTRL1(host));
225 static void mxs_mmc_start_cmd(struct mxs_mmc_host *host,
226 struct mmc_command *cmd);
228 static void mxs_mmc_request_done(struct mxs_mmc_host *host)
230 struct mmc_command *cmd = host->cmd;
231 struct mmc_data *data = host->data;
232 struct mmc_request *mrq = host->mrq;
234 if (mmc_resp_type(cmd) & MMC_RSP_PRESENT) {
235 if (mmc_resp_type(cmd) & MMC_RSP_136) {
236 cmd->resp[3] = readl(host->base + HW_SSP_SDRESP0(host));
237 cmd->resp[2] = readl(host->base + HW_SSP_SDRESP1(host));
238 cmd->resp[1] = readl(host->base + HW_SSP_SDRESP2(host));
239 cmd->resp[0] = readl(host->base + HW_SSP_SDRESP3(host));
241 cmd->resp[0] = readl(host->base + HW_SSP_SDRESP0(host));
246 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
247 data->sg_len, host->dma_dir);
249 * If there was an error on any block, we mark all
250 * data blocks as being in error.
253 data->bytes_xfered = data->blocks * data->blksz;
255 data->bytes_xfered = 0;
259 mxs_mmc_start_cmd(host, mrq->stop);
265 mmc_request_done(host->mmc, mrq);
268 static void mxs_mmc_dma_irq_callback(void *param)
270 struct mxs_mmc_host *host = param;
272 mxs_mmc_request_done(host);
275 static irqreturn_t mxs_mmc_irq_handler(int irq, void *dev_id)
277 struct mxs_mmc_host *host = dev_id;
278 struct mmc_command *cmd = host->cmd;
279 struct mmc_data *data = host->data;
282 spin_lock(&host->lock);
284 stat = readl(host->base + HW_SSP_CTRL1(host));
285 writel(stat & MXS_MMC_IRQ_BITS,
286 host->base + HW_SSP_CTRL1(host) + STMP_OFFSET_REG_CLR);
288 if ((stat & BM_SSP_CTRL1_SDIO_IRQ) && (stat & BM_SSP_CTRL1_SDIO_IRQ_EN))
289 mmc_signal_sdio_irq(host->mmc);
291 spin_unlock(&host->lock);
293 if (stat & BM_SSP_CTRL1_RESP_TIMEOUT_IRQ)
294 cmd->error = -ETIMEDOUT;
295 else if (stat & BM_SSP_CTRL1_RESP_ERR_IRQ)
299 if (stat & (BM_SSP_CTRL1_DATA_TIMEOUT_IRQ |
300 BM_SSP_CTRL1_RECV_TIMEOUT_IRQ))
301 data->error = -ETIMEDOUT;
302 else if (stat & BM_SSP_CTRL1_DATA_CRC_IRQ)
303 data->error = -EILSEQ;
304 else if (stat & (BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ |
305 BM_SSP_CTRL1_FIFO_OVERRUN_IRQ))
312 static struct dma_async_tx_descriptor *mxs_mmc_prep_dma(
313 struct mxs_mmc_host *host, unsigned long flags)
315 struct dma_async_tx_descriptor *desc;
316 struct mmc_data *data = host->data;
317 struct scatterlist * sgl;
322 dma_map_sg(mmc_dev(host->mmc), data->sg,
323 data->sg_len, host->dma_dir);
325 sg_len = data->sg_len;
328 sgl = (struct scatterlist *) host->ssp_pio_words;
329 sg_len = SSP_PIO_NUM;
332 desc = dmaengine_prep_slave_sg(host->dmach,
333 sgl, sg_len, host->slave_dirn, flags);
335 desc->callback = mxs_mmc_dma_irq_callback;
336 desc->callback_param = host;
339 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
340 data->sg_len, host->dma_dir);
346 static void mxs_mmc_bc(struct mxs_mmc_host *host)
348 struct mmc_command *cmd = host->cmd;
349 struct dma_async_tx_descriptor *desc;
350 u32 ctrl0, cmd0, cmd1;
352 ctrl0 = BM_SSP_CTRL0_ENABLE | BM_SSP_CTRL0_IGNORE_CRC;
353 cmd0 = BF_SSP(cmd->opcode, CMD0_CMD) | BM_SSP_CMD0_APPEND_8CYC;
356 if (host->sdio_irq_en) {
357 ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
358 cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN;
361 host->ssp_pio_words[0] = ctrl0;
362 host->ssp_pio_words[1] = cmd0;
363 host->ssp_pio_words[2] = cmd1;
364 host->dma_dir = DMA_NONE;
365 host->slave_dirn = DMA_TRANS_NONE;
366 desc = mxs_mmc_prep_dma(host, DMA_CTRL_ACK);
370 dmaengine_submit(desc);
371 dma_async_issue_pending(host->dmach);
375 dev_warn(mmc_dev(host->mmc),
376 "%s: failed to prep dma\n", __func__);
379 static void mxs_mmc_ac(struct mxs_mmc_host *host)
381 struct mmc_command *cmd = host->cmd;
382 struct dma_async_tx_descriptor *desc;
383 u32 ignore_crc, get_resp, long_resp;
384 u32 ctrl0, cmd0, cmd1;
386 ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ?
387 0 : BM_SSP_CTRL0_IGNORE_CRC;
388 get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ?
389 BM_SSP_CTRL0_GET_RESP : 0;
390 long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ?
391 BM_SSP_CTRL0_LONG_RESP : 0;
393 ctrl0 = BM_SSP_CTRL0_ENABLE | ignore_crc | get_resp | long_resp;
394 cmd0 = BF_SSP(cmd->opcode, CMD0_CMD);
397 if (host->sdio_irq_en) {
398 ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
399 cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN;
402 host->ssp_pio_words[0] = ctrl0;
403 host->ssp_pio_words[1] = cmd0;
404 host->ssp_pio_words[2] = cmd1;
405 host->dma_dir = DMA_NONE;
406 host->slave_dirn = DMA_TRANS_NONE;
407 desc = mxs_mmc_prep_dma(host, DMA_CTRL_ACK);
411 dmaengine_submit(desc);
412 dma_async_issue_pending(host->dmach);
416 dev_warn(mmc_dev(host->mmc),
417 "%s: failed to prep dma\n", __func__);
420 static unsigned short mxs_ns_to_ssp_ticks(unsigned clock_rate, unsigned ns)
422 const unsigned int ssp_timeout_mul = 4096;
424 * Calculate ticks in ms since ns are large numbers
427 const unsigned int clock_per_ms = clock_rate / 1000;
428 const unsigned int ms = ns / 1000;
429 const unsigned int ticks = ms * clock_per_ms;
430 const unsigned int ssp_ticks = ticks / ssp_timeout_mul;
432 WARN_ON(ssp_ticks == 0);
436 static void mxs_mmc_adtc(struct mxs_mmc_host *host)
438 struct mmc_command *cmd = host->cmd;
439 struct mmc_data *data = cmd->data;
440 struct dma_async_tx_descriptor *desc;
441 struct scatterlist *sgl = data->sg, *sg;
442 unsigned int sg_len = data->sg_len;
445 unsigned short dma_data_dir, timeout;
446 enum dma_transfer_direction slave_dirn;
447 unsigned int data_size = 0, log2_blksz;
448 unsigned int blocks = data->blocks;
450 u32 ignore_crc, get_resp, long_resp, read;
451 u32 ctrl0, cmd0, cmd1, val;
453 ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ?
454 0 : BM_SSP_CTRL0_IGNORE_CRC;
455 get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ?
456 BM_SSP_CTRL0_GET_RESP : 0;
457 long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ?
458 BM_SSP_CTRL0_LONG_RESP : 0;
460 if (data->flags & MMC_DATA_WRITE) {
461 dma_data_dir = DMA_TO_DEVICE;
462 slave_dirn = DMA_MEM_TO_DEV;
465 dma_data_dir = DMA_FROM_DEVICE;
466 slave_dirn = DMA_DEV_TO_MEM;
467 read = BM_SSP_CTRL0_READ;
470 ctrl0 = BF_SSP(host->bus_width, CTRL0_BUS_WIDTH) |
471 ignore_crc | get_resp | long_resp |
472 BM_SSP_CTRL0_DATA_XFER | read |
473 BM_SSP_CTRL0_WAIT_FOR_IRQ |
476 cmd0 = BF_SSP(cmd->opcode, CMD0_CMD);
478 /* get logarithm to base 2 of block size for setting register */
479 log2_blksz = ilog2(data->blksz);
482 * take special care of the case that data size from data->sg
483 * is not equal to blocks x blksz
485 for_each_sg(sgl, sg, sg_len, i)
486 data_size += sg->length;
488 if (data_size != data->blocks * data->blksz)
491 /* xfer count, block size and count need to be set differently */
492 if (ssp_is_old(host)) {
493 ctrl0 |= BF_SSP(data_size, CTRL0_XFER_COUNT);
494 cmd0 |= BF_SSP(log2_blksz, CMD0_BLOCK_SIZE) |
495 BF_SSP(blocks - 1, CMD0_BLOCK_COUNT);
497 writel(data_size, host->base + HW_SSP_XFER_SIZE);
498 writel(BF_SSP(log2_blksz, BLOCK_SIZE_BLOCK_SIZE) |
499 BF_SSP(blocks - 1, BLOCK_SIZE_BLOCK_COUNT),
500 host->base + HW_SSP_BLOCK_SIZE);
503 if ((cmd->opcode == MMC_STOP_TRANSMISSION) ||
504 (cmd->opcode == SD_IO_RW_EXTENDED))
505 cmd0 |= BM_SSP_CMD0_APPEND_8CYC;
509 if (host->sdio_irq_en) {
510 ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
511 cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN;
514 /* set the timeout count */
515 timeout = mxs_ns_to_ssp_ticks(host->clk_rate, data->timeout_ns);
516 val = readl(host->base + HW_SSP_TIMING(host));
517 val &= ~(BM_SSP_TIMING_TIMEOUT);
518 val |= BF_SSP(timeout, TIMING_TIMEOUT);
519 writel(val, host->base + HW_SSP_TIMING(host));
522 host->ssp_pio_words[0] = ctrl0;
523 host->ssp_pio_words[1] = cmd0;
524 host->ssp_pio_words[2] = cmd1;
525 host->dma_dir = DMA_NONE;
526 host->slave_dirn = DMA_TRANS_NONE;
527 desc = mxs_mmc_prep_dma(host, 0);
532 WARN_ON(host->data != NULL);
534 host->dma_dir = dma_data_dir;
535 host->slave_dirn = slave_dirn;
536 desc = mxs_mmc_prep_dma(host, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
540 dmaengine_submit(desc);
541 dma_async_issue_pending(host->dmach);
544 dev_warn(mmc_dev(host->mmc),
545 "%s: failed to prep dma\n", __func__);
548 static void mxs_mmc_start_cmd(struct mxs_mmc_host *host,
549 struct mmc_command *cmd)
553 switch (mmc_cmd_type(cmd)) {
567 dev_warn(mmc_dev(host->mmc),
568 "%s: unknown MMC command\n", __func__);
573 static void mxs_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
575 struct mxs_mmc_host *host = mmc_priv(mmc);
577 WARN_ON(host->mrq != NULL);
579 mxs_mmc_start_cmd(host, mrq->cmd);
582 static void mxs_mmc_set_clk_rate(struct mxs_mmc_host *host, unsigned int rate)
584 unsigned int ssp_clk, ssp_sck;
585 u32 clock_divide, clock_rate;
588 ssp_clk = clk_get_rate(host->clk);
590 for (clock_divide = 2; clock_divide <= 254; clock_divide += 2) {
591 clock_rate = DIV_ROUND_UP(ssp_clk, rate * clock_divide);
592 clock_rate = (clock_rate > 0) ? clock_rate - 1 : 0;
593 if (clock_rate <= 255)
597 if (clock_divide > 254) {
598 dev_err(mmc_dev(host->mmc),
599 "%s: cannot set clock to %d\n", __func__, rate);
603 ssp_sck = ssp_clk / clock_divide / (1 + clock_rate);
605 val = readl(host->base + HW_SSP_TIMING(host));
606 val &= ~(BM_SSP_TIMING_CLOCK_DIVIDE | BM_SSP_TIMING_CLOCK_RATE);
607 val |= BF_SSP(clock_divide, TIMING_CLOCK_DIVIDE);
608 val |= BF_SSP(clock_rate, TIMING_CLOCK_RATE);
609 writel(val, host->base + HW_SSP_TIMING(host));
611 host->clk_rate = ssp_sck;
613 dev_dbg(mmc_dev(host->mmc),
614 "%s: clock_divide %d, clock_rate %d, ssp_clk %d, rate_actual %d, rate_requested %d\n",
615 __func__, clock_divide, clock_rate, ssp_clk, ssp_sck, rate);
618 static void mxs_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
620 struct mxs_mmc_host *host = mmc_priv(mmc);
622 if (ios->bus_width == MMC_BUS_WIDTH_8)
624 else if (ios->bus_width == MMC_BUS_WIDTH_4)
630 mxs_mmc_set_clk_rate(host, ios->clock);
633 static void mxs_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
635 struct mxs_mmc_host *host = mmc_priv(mmc);
638 spin_lock_irqsave(&host->lock, flags);
640 host->sdio_irq_en = enable;
643 writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK,
644 host->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
645 writel(BM_SSP_CTRL1_SDIO_IRQ_EN,
646 host->base + HW_SSP_CTRL1(host) + STMP_OFFSET_REG_SET);
648 if (readl(host->base + HW_SSP_STATUS(host)) &
649 BM_SSP_STATUS_SDIO_IRQ)
650 mmc_signal_sdio_irq(host->mmc);
653 writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK,
654 host->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
655 writel(BM_SSP_CTRL1_SDIO_IRQ_EN,
656 host->base + HW_SSP_CTRL1(host) + STMP_OFFSET_REG_CLR);
659 spin_unlock_irqrestore(&host->lock, flags);
662 static const struct mmc_host_ops mxs_mmc_ops = {
663 .request = mxs_mmc_request,
664 .get_ro = mxs_mmc_get_ro,
665 .get_cd = mxs_mmc_get_cd,
666 .set_ios = mxs_mmc_set_ios,
667 .enable_sdio_irq = mxs_mmc_enable_sdio_irq,
670 static bool mxs_mmc_dma_filter(struct dma_chan *chan, void *param)
672 struct mxs_mmc_host *host = param;
674 if (!mxs_dma_is_apbh(chan))
677 if (chan->chan_id != host->dma_channel)
680 chan->private = &host->dma_data;
685 static struct platform_device_id mxs_mmc_ids[] = {
688 .driver_data = IMX23_MMC,
691 .driver_data = IMX28_MMC,
696 MODULE_DEVICE_TABLE(platform, mxs_mmc_ids);
698 static const struct of_device_id mxs_mmc_dt_ids[] = {
699 { .compatible = "fsl,imx23-mmc", .data = (void *) IMX23_MMC, },
700 { .compatible = "fsl,imx28-mmc", .data = (void *) IMX28_MMC, },
703 MODULE_DEVICE_TABLE(of, mxs_mmc_dt_ids);
705 static int mxs_mmc_probe(struct platform_device *pdev)
707 const struct of_device_id *of_id =
708 of_match_device(mxs_mmc_dt_ids, &pdev->dev);
709 struct device_node *np = pdev->dev.of_node;
710 struct mxs_mmc_host *host;
711 struct mmc_host *mmc;
712 struct resource *iores, *dmares;
713 struct mxs_mmc_platform_data *pdata;
714 struct pinctrl *pinctrl;
715 int ret = 0, irq_err, irq_dma;
717 struct regulator *reg_vmmc;
718 enum of_gpio_flags flags;
720 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
721 dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
722 irq_err = platform_get_irq(pdev, 0);
723 irq_dma = platform_get_irq(pdev, 1);
724 if (!iores || irq_err < 0 || irq_dma < 0)
727 mmc = mmc_alloc_host(sizeof(struct mxs_mmc_host), &pdev->dev);
731 host = mmc_priv(mmc);
732 host->base = devm_request_and_ioremap(&pdev->dev, iores);
734 ret = -EADDRNOTAVAIL;
739 host->devid = (enum mxs_mmc_id) of_id->data;
741 * TODO: This is a temporary solution and should be changed
742 * to use generic DMA binding later when the helpers get in.
744 ret = of_property_read_u32(np, "fsl,ssp-dma-channel",
747 dev_err(mmc_dev(host->mmc),
748 "failed to get dma channel\n");
752 host->devid = pdev->id_entry->driver_data;
753 host->dma_channel = dmares->start;
757 host->sdio_irq_en = 0;
759 reg_vmmc = devm_regulator_get(&pdev->dev, "vmmc");
760 if (!IS_ERR(reg_vmmc)) {
761 ret = regulator_enable(reg_vmmc);
764 "Failed to enable vmmc regulator: %d\n", ret);
769 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
770 if (IS_ERR(pinctrl)) {
771 ret = PTR_ERR(pinctrl);
775 host->clk = clk_get(&pdev->dev, NULL);
776 if (IS_ERR(host->clk)) {
777 ret = PTR_ERR(host->clk);
780 clk_prepare_enable(host->clk);
785 dma_cap_set(DMA_SLAVE, mask);
786 host->dma_data.chan_irq = irq_dma;
787 host->dmach = dma_request_channel(mask, mxs_mmc_dma_filter, host);
789 dev_err(mmc_dev(host->mmc),
790 "%s: failed to request dma\n", __func__);
794 /* set mmc core parameters */
795 mmc->ops = &mxs_mmc_ops;
796 mmc->caps = MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
797 MMC_CAP_SDIO_IRQ | MMC_CAP_NEEDS_POLL;
799 pdata = mmc_dev(host->mmc)->platform_data;
802 of_property_read_u32(np, "bus-width", &bus_width);
804 mmc->caps |= MMC_CAP_4_BIT_DATA;
805 else if (bus_width == 8)
806 mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA;
807 host->wp_gpio = of_get_named_gpio_flags(np, "wp-gpios", 0,
809 if (flags & OF_GPIO_ACTIVE_LOW)
810 host->wp_inverted = 1;
812 if (pdata->flags & SLOTF_8_BIT_CAPABLE)
813 mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA;
814 if (pdata->flags & SLOTF_4_BIT_CAPABLE)
815 mmc->caps |= MMC_CAP_4_BIT_DATA;
816 host->wp_gpio = pdata->wp_gpio;
820 mmc->f_max = 288000000;
821 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
824 mmc->max_blk_size = 1 << 0xf;
825 mmc->max_blk_count = (ssp_is_old(host)) ? 0xff : 0xffffff;
826 mmc->max_req_size = (ssp_is_old(host)) ? 0xffff : 0xffffffff;
827 mmc->max_seg_size = dma_get_max_seg_size(host->dmach->device->dev);
829 platform_set_drvdata(pdev, mmc);
831 ret = devm_request_irq(&pdev->dev, irq_err, mxs_mmc_irq_handler, 0,
836 spin_lock_init(&host->lock);
838 ret = mmc_add_host(mmc);
842 dev_info(mmc_dev(host->mmc), "initialized\n");
848 dma_release_channel(host->dmach);
850 clk_disable_unprepare(host->clk);
857 static int mxs_mmc_remove(struct platform_device *pdev)
859 struct mmc_host *mmc = platform_get_drvdata(pdev);
860 struct mxs_mmc_host *host = mmc_priv(mmc);
862 mmc_remove_host(mmc);
864 platform_set_drvdata(pdev, NULL);
867 dma_release_channel(host->dmach);
869 clk_disable_unprepare(host->clk);
878 static int mxs_mmc_suspend(struct device *dev)
880 struct mmc_host *mmc = dev_get_drvdata(dev);
881 struct mxs_mmc_host *host = mmc_priv(mmc);
884 ret = mmc_suspend_host(mmc);
886 clk_disable_unprepare(host->clk);
891 static int mxs_mmc_resume(struct device *dev)
893 struct mmc_host *mmc = dev_get_drvdata(dev);
894 struct mxs_mmc_host *host = mmc_priv(mmc);
897 clk_prepare_enable(host->clk);
899 ret = mmc_resume_host(mmc);
904 static const struct dev_pm_ops mxs_mmc_pm_ops = {
905 .suspend = mxs_mmc_suspend,
906 .resume = mxs_mmc_resume,
910 static struct platform_driver mxs_mmc_driver = {
911 .probe = mxs_mmc_probe,
912 .remove = mxs_mmc_remove,
913 .id_table = mxs_mmc_ids,
916 .owner = THIS_MODULE,
918 .pm = &mxs_mmc_pm_ops,
920 .of_match_table = mxs_mmc_dt_ids,
924 module_platform_driver(mxs_mmc_driver);
926 MODULE_DESCRIPTION("FREESCALE MXS MMC peripheral");
927 MODULE_AUTHOR("Freescale Semiconductor");
928 MODULE_LICENSE("GPL");