blob: 073e871a0fc8fe8ef00e6aca2da8a46b172488e6 [file] [log] [blame]
Shawn Guoe4243f12011-02-21 18:35:28 +08001/*
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 Guo6de4d812012-05-06 13:30:44 +080026#include <linux/of.h>
27#include <linux/of_device.h>
28#include <linux/of_gpio.h>
Shawn Guoe4243f12011-02-21 18:35:28 +080029#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>
Sascha Hauerabd37cc2013-12-05 14:34:50 +010041#include <linux/mmc/slot-gpio.h>
Shawn Guoe4243f12011-02-21 18:35:28 +080042#include <linux/gpio.h>
43#include <linux/regulator/consumer.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040044#include <linux/module.h>
Shawn Guo70e60202012-05-05 19:40:09 +080045#include <linux/stmp_device.h>
Marek Vasut8be3d3b2012-08-03 17:26:06 +020046#include <linux/spi/mxs-spi.h>
Shawn Guoe4243f12011-02-21 18:35:28 +080047
48#define DRIVER_NAME "mxs-mmc"
49
Shawn Guoe4243f12011-02-21 18:35:28 +080050#define MXS_MMC_IRQ_BITS (BM_SSP_CTRL1_SDIO_IRQ | \
51 BM_SSP_CTRL1_RESP_ERR_IRQ | \
52 BM_SSP_CTRL1_RESP_TIMEOUT_IRQ | \
53 BM_SSP_CTRL1_DATA_TIMEOUT_IRQ | \
54 BM_SSP_CTRL1_DATA_CRC_IRQ | \
55 BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ | \
56 BM_SSP_CTRL1_RECV_TIMEOUT_IRQ | \
57 BM_SSP_CTRL1_FIFO_OVERRUN_IRQ)
58
Marek Vasut8be3d3b2012-08-03 17:26:06 +020059/* card detect polling timeout */
60#define MXS_MMC_DETECT_TIMEOUT (HZ/2)
Shawn Guoef9b4d32012-05-05 20:24:01 +080061
Shawn Guoe4243f12011-02-21 18:35:28 +080062struct mxs_mmc_host {
Marek Vasut829c1bf2012-08-03 17:26:09 +020063 struct mxs_ssp ssp;
64
Shawn Guoe4243f12011-02-21 18:35:28 +080065 struct mmc_host *mmc;
66 struct mmc_request *mrq;
67 struct mmc_command *cmd;
68 struct mmc_data *data;
69
Shawn Guoe4243f12011-02-21 18:35:28 +080070 unsigned char bus_width;
71 spinlock_t lock;
72 int sdio_irq_en;
73};
74
Shawn Guoe4243f12011-02-21 18:35:28 +080075static int mxs_mmc_get_cd(struct mmc_host *mmc)
76{
77 struct mxs_mmc_host *host = mmc_priv(mmc);
Marek Vasut829c1bf2012-08-03 17:26:09 +020078 struct mxs_ssp *ssp = &host->ssp;
Sascha Hauerd1a1dfb2013-12-05 14:34:51 +010079 int present, ret;
80
81 ret = mmc_gpio_get_cd(mmc);
82 if (ret >= 0)
83 return ret;
Shawn Guoe4243f12011-02-21 18:35:28 +080084
Sascha Hauer6c3331d2013-12-05 14:34:49 +010085 present = !(readl(ssp->base + HW_SSP_STATUS(ssp)) &
86 BM_SSP_STATUS_CARD_DETECT);
87
88 if (mmc->caps2 & MMC_CAP2_CD_ACTIVE_HIGH)
89 present = !present;
90
91 return present;
Shawn Guoe4243f12011-02-21 18:35:28 +080092}
93
Fabio Estevam02c18c42013-07-03 17:02:38 -030094static int mxs_mmc_reset(struct mxs_mmc_host *host)
Shawn Guoe4243f12011-02-21 18:35:28 +080095{
Marek Vasut829c1bf2012-08-03 17:26:09 +020096 struct mxs_ssp *ssp = &host->ssp;
Shawn Guoe4243f12011-02-21 18:35:28 +080097 u32 ctrl0, ctrl1;
Fabio Estevam02c18c42013-07-03 17:02:38 -030098 int ret;
Shawn Guoe4243f12011-02-21 18:35:28 +080099
Fabio Estevam02c18c42013-07-03 17:02:38 -0300100 ret = stmp_reset_block(ssp->base);
101 if (ret)
102 return ret;
Shawn Guoe4243f12011-02-21 18:35:28 +0800103
104 ctrl0 = BM_SSP_CTRL0_IGNORE_CRC;
105 ctrl1 = BF_SSP(0x3, CTRL1_SSP_MODE) |
106 BF_SSP(0x7, CTRL1_WORD_LENGTH) |
107 BM_SSP_CTRL1_DMA_ENABLE |
108 BM_SSP_CTRL1_POLARITY |
109 BM_SSP_CTRL1_RECV_TIMEOUT_IRQ_EN |
110 BM_SSP_CTRL1_DATA_CRC_IRQ_EN |
111 BM_SSP_CTRL1_DATA_TIMEOUT_IRQ_EN |
112 BM_SSP_CTRL1_RESP_TIMEOUT_IRQ_EN |
113 BM_SSP_CTRL1_RESP_ERR_IRQ_EN;
114
115 writel(BF_SSP(0xffff, TIMING_TIMEOUT) |
116 BF_SSP(2, TIMING_CLOCK_DIVIDE) |
117 BF_SSP(0, TIMING_CLOCK_RATE),
Marek Vasut829c1bf2012-08-03 17:26:09 +0200118 ssp->base + HW_SSP_TIMING(ssp));
Shawn Guoe4243f12011-02-21 18:35:28 +0800119
120 if (host->sdio_irq_en) {
121 ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
122 ctrl1 |= BM_SSP_CTRL1_SDIO_IRQ_EN;
123 }
124
Marek Vasut829c1bf2012-08-03 17:26:09 +0200125 writel(ctrl0, ssp->base + HW_SSP_CTRL0);
126 writel(ctrl1, ssp->base + HW_SSP_CTRL1(ssp));
Fabio Estevam02c18c42013-07-03 17:02:38 -0300127 return 0;
Shawn Guoe4243f12011-02-21 18:35:28 +0800128}
129
130static void mxs_mmc_start_cmd(struct mxs_mmc_host *host,
131 struct mmc_command *cmd);
132
133static void mxs_mmc_request_done(struct mxs_mmc_host *host)
134{
135 struct mmc_command *cmd = host->cmd;
136 struct mmc_data *data = host->data;
137 struct mmc_request *mrq = host->mrq;
Marek Vasut829c1bf2012-08-03 17:26:09 +0200138 struct mxs_ssp *ssp = &host->ssp;
Shawn Guoe4243f12011-02-21 18:35:28 +0800139
140 if (mmc_resp_type(cmd) & MMC_RSP_PRESENT) {
141 if (mmc_resp_type(cmd) & MMC_RSP_136) {
Marek Vasut829c1bf2012-08-03 17:26:09 +0200142 cmd->resp[3] = readl(ssp->base + HW_SSP_SDRESP0(ssp));
143 cmd->resp[2] = readl(ssp->base + HW_SSP_SDRESP1(ssp));
144 cmd->resp[1] = readl(ssp->base + HW_SSP_SDRESP2(ssp));
145 cmd->resp[0] = readl(ssp->base + HW_SSP_SDRESP3(ssp));
Shawn Guoe4243f12011-02-21 18:35:28 +0800146 } else {
Marek Vasut829c1bf2012-08-03 17:26:09 +0200147 cmd->resp[0] = readl(ssp->base + HW_SSP_SDRESP0(ssp));
Shawn Guoe4243f12011-02-21 18:35:28 +0800148 }
149 }
150
151 if (data) {
152 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
Marek Vasut65defb92012-08-03 17:26:12 +0200153 data->sg_len, ssp->dma_dir);
Shawn Guoe4243f12011-02-21 18:35:28 +0800154 /*
155 * If there was an error on any block, we mark all
156 * data blocks as being in error.
157 */
158 if (!data->error)
159 data->bytes_xfered = data->blocks * data->blksz;
160 else
161 data->bytes_xfered = 0;
162
163 host->data = NULL;
164 if (mrq->stop) {
165 mxs_mmc_start_cmd(host, mrq->stop);
166 return;
167 }
168 }
169
170 host->mrq = NULL;
171 mmc_request_done(host->mmc, mrq);
172}
173
174static void mxs_mmc_dma_irq_callback(void *param)
175{
176 struct mxs_mmc_host *host = param;
177
178 mxs_mmc_request_done(host);
179}
180
181static irqreturn_t mxs_mmc_irq_handler(int irq, void *dev_id)
182{
183 struct mxs_mmc_host *host = dev_id;
184 struct mmc_command *cmd = host->cmd;
185 struct mmc_data *data = host->data;
Marek Vasut829c1bf2012-08-03 17:26:09 +0200186 struct mxs_ssp *ssp = &host->ssp;
Shawn Guoe4243f12011-02-21 18:35:28 +0800187 u32 stat;
188
189 spin_lock(&host->lock);
190
Marek Vasut829c1bf2012-08-03 17:26:09 +0200191 stat = readl(ssp->base + HW_SSP_CTRL1(ssp));
Shawn Guoe4243f12011-02-21 18:35:28 +0800192 writel(stat & MXS_MMC_IRQ_BITS,
Marek Vasut829c1bf2012-08-03 17:26:09 +0200193 ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_CLR);
Shawn Guoe4243f12011-02-21 18:35:28 +0800194
Lauri Hintsala1af36b22012-07-17 17:16:09 +0300195 spin_unlock(&host->lock);
196
Shawn Guoe4243f12011-02-21 18:35:28 +0800197 if ((stat & BM_SSP_CTRL1_SDIO_IRQ) && (stat & BM_SSP_CTRL1_SDIO_IRQ_EN))
198 mmc_signal_sdio_irq(host->mmc);
199
Shawn Guoe4243f12011-02-21 18:35:28 +0800200 if (stat & BM_SSP_CTRL1_RESP_TIMEOUT_IRQ)
201 cmd->error = -ETIMEDOUT;
202 else if (stat & BM_SSP_CTRL1_RESP_ERR_IRQ)
203 cmd->error = -EIO;
204
205 if (data) {
206 if (stat & (BM_SSP_CTRL1_DATA_TIMEOUT_IRQ |
207 BM_SSP_CTRL1_RECV_TIMEOUT_IRQ))
208 data->error = -ETIMEDOUT;
209 else if (stat & BM_SSP_CTRL1_DATA_CRC_IRQ)
210 data->error = -EILSEQ;
211 else if (stat & (BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ |
212 BM_SSP_CTRL1_FIFO_OVERRUN_IRQ))
213 data->error = -EIO;
214 }
215
216 return IRQ_HANDLED;
217}
218
219static struct dma_async_tx_descriptor *mxs_mmc_prep_dma(
Huang Shijie921de862012-02-16 14:17:33 +0800220 struct mxs_mmc_host *host, unsigned long flags)
Shawn Guoe4243f12011-02-21 18:35:28 +0800221{
Marek Vasut65defb92012-08-03 17:26:12 +0200222 struct mxs_ssp *ssp = &host->ssp;
Shawn Guoe4243f12011-02-21 18:35:28 +0800223 struct dma_async_tx_descriptor *desc;
224 struct mmc_data *data = host->data;
225 struct scatterlist * sgl;
226 unsigned int sg_len;
227
228 if (data) {
229 /* data */
230 dma_map_sg(mmc_dev(host->mmc), data->sg,
Marek Vasut65defb92012-08-03 17:26:12 +0200231 data->sg_len, ssp->dma_dir);
Shawn Guoe4243f12011-02-21 18:35:28 +0800232 sgl = data->sg;
233 sg_len = data->sg_len;
234 } else {
235 /* pio */
Marek Vasut65defb92012-08-03 17:26:12 +0200236 sgl = (struct scatterlist *) ssp->ssp_pio_words;
Shawn Guoe4243f12011-02-21 18:35:28 +0800237 sg_len = SSP_PIO_NUM;
238 }
239
Marek Vasut65defb92012-08-03 17:26:12 +0200240 desc = dmaengine_prep_slave_sg(ssp->dmach,
241 sgl, sg_len, ssp->slave_dirn, flags);
Shawn Guoe4243f12011-02-21 18:35:28 +0800242 if (desc) {
243 desc->callback = mxs_mmc_dma_irq_callback;
244 desc->callback_param = host;
245 } else {
246 if (data)
247 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
Marek Vasut65defb92012-08-03 17:26:12 +0200248 data->sg_len, ssp->dma_dir);
Shawn Guoe4243f12011-02-21 18:35:28 +0800249 }
250
251 return desc;
252}
253
254static void mxs_mmc_bc(struct mxs_mmc_host *host)
255{
Marek Vasut65defb92012-08-03 17:26:12 +0200256 struct mxs_ssp *ssp = &host->ssp;
Shawn Guoe4243f12011-02-21 18:35:28 +0800257 struct mmc_command *cmd = host->cmd;
258 struct dma_async_tx_descriptor *desc;
259 u32 ctrl0, cmd0, cmd1;
260
261 ctrl0 = BM_SSP_CTRL0_ENABLE | BM_SSP_CTRL0_IGNORE_CRC;
262 cmd0 = BF_SSP(cmd->opcode, CMD0_CMD) | BM_SSP_CMD0_APPEND_8CYC;
263 cmd1 = cmd->arg;
264
265 if (host->sdio_irq_en) {
266 ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
267 cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN;
268 }
269
Marek Vasut65defb92012-08-03 17:26:12 +0200270 ssp->ssp_pio_words[0] = ctrl0;
271 ssp->ssp_pio_words[1] = cmd0;
272 ssp->ssp_pio_words[2] = cmd1;
273 ssp->dma_dir = DMA_NONE;
274 ssp->slave_dirn = DMA_TRANS_NONE;
Huang Shijie921de862012-02-16 14:17:33 +0800275 desc = mxs_mmc_prep_dma(host, DMA_CTRL_ACK);
Shawn Guoe4243f12011-02-21 18:35:28 +0800276 if (!desc)
277 goto out;
278
279 dmaengine_submit(desc);
Marek Vasut65defb92012-08-03 17:26:12 +0200280 dma_async_issue_pending(ssp->dmach);
Shawn Guoe4243f12011-02-21 18:35:28 +0800281 return;
282
283out:
284 dev_warn(mmc_dev(host->mmc),
285 "%s: failed to prep dma\n", __func__);
286}
287
288static void mxs_mmc_ac(struct mxs_mmc_host *host)
289{
Marek Vasut65defb92012-08-03 17:26:12 +0200290 struct mxs_ssp *ssp = &host->ssp;
Shawn Guoe4243f12011-02-21 18:35:28 +0800291 struct mmc_command *cmd = host->cmd;
292 struct dma_async_tx_descriptor *desc;
293 u32 ignore_crc, get_resp, long_resp;
294 u32 ctrl0, cmd0, cmd1;
295
296 ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ?
297 0 : BM_SSP_CTRL0_IGNORE_CRC;
298 get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ?
299 BM_SSP_CTRL0_GET_RESP : 0;
300 long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ?
301 BM_SSP_CTRL0_LONG_RESP : 0;
302
303 ctrl0 = BM_SSP_CTRL0_ENABLE | ignore_crc | get_resp | long_resp;
304 cmd0 = BF_SSP(cmd->opcode, CMD0_CMD);
305 cmd1 = cmd->arg;
306
307 if (host->sdio_irq_en) {
308 ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
309 cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN;
310 }
311
Marek Vasut65defb92012-08-03 17:26:12 +0200312 ssp->ssp_pio_words[0] = ctrl0;
313 ssp->ssp_pio_words[1] = cmd0;
314 ssp->ssp_pio_words[2] = cmd1;
315 ssp->dma_dir = DMA_NONE;
316 ssp->slave_dirn = DMA_TRANS_NONE;
Huang Shijie921de862012-02-16 14:17:33 +0800317 desc = mxs_mmc_prep_dma(host, DMA_CTRL_ACK);
Shawn Guoe4243f12011-02-21 18:35:28 +0800318 if (!desc)
319 goto out;
320
321 dmaengine_submit(desc);
Marek Vasut65defb92012-08-03 17:26:12 +0200322 dma_async_issue_pending(ssp->dmach);
Shawn Guoe4243f12011-02-21 18:35:28 +0800323 return;
324
325out:
326 dev_warn(mmc_dev(host->mmc),
327 "%s: failed to prep dma\n", __func__);
328}
329
330static unsigned short mxs_ns_to_ssp_ticks(unsigned clock_rate, unsigned ns)
331{
332 const unsigned int ssp_timeout_mul = 4096;
333 /*
334 * Calculate ticks in ms since ns are large numbers
335 * and might overflow
336 */
337 const unsigned int clock_per_ms = clock_rate / 1000;
338 const unsigned int ms = ns / 1000;
339 const unsigned int ticks = ms * clock_per_ms;
340 const unsigned int ssp_ticks = ticks / ssp_timeout_mul;
341
342 WARN_ON(ssp_ticks == 0);
343 return ssp_ticks;
344}
345
346static void mxs_mmc_adtc(struct mxs_mmc_host *host)
347{
348 struct mmc_command *cmd = host->cmd;
349 struct mmc_data *data = cmd->data;
350 struct dma_async_tx_descriptor *desc;
351 struct scatterlist *sgl = data->sg, *sg;
352 unsigned int sg_len = data->sg_len;
Fabio Estevamfd63ac72013-01-07 22:42:35 -0200353 unsigned int i;
Shawn Guoe4243f12011-02-21 18:35:28 +0800354
355 unsigned short dma_data_dir, timeout;
Vinod Koul05f57992011-10-14 10:45:11 +0530356 enum dma_transfer_direction slave_dirn;
Shawn Guoe4243f12011-02-21 18:35:28 +0800357 unsigned int data_size = 0, log2_blksz;
358 unsigned int blocks = data->blocks;
359
Marek Vasut829c1bf2012-08-03 17:26:09 +0200360 struct mxs_ssp *ssp = &host->ssp;
361
Shawn Guoe4243f12011-02-21 18:35:28 +0800362 u32 ignore_crc, get_resp, long_resp, read;
363 u32 ctrl0, cmd0, cmd1, val;
364
365 ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ?
366 0 : BM_SSP_CTRL0_IGNORE_CRC;
367 get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ?
368 BM_SSP_CTRL0_GET_RESP : 0;
369 long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ?
370 BM_SSP_CTRL0_LONG_RESP : 0;
371
372 if (data->flags & MMC_DATA_WRITE) {
373 dma_data_dir = DMA_TO_DEVICE;
Vinod Koul05f57992011-10-14 10:45:11 +0530374 slave_dirn = DMA_MEM_TO_DEV;
Shawn Guoe4243f12011-02-21 18:35:28 +0800375 read = 0;
376 } else {
377 dma_data_dir = DMA_FROM_DEVICE;
Vinod Koul05f57992011-10-14 10:45:11 +0530378 slave_dirn = DMA_DEV_TO_MEM;
Shawn Guoe4243f12011-02-21 18:35:28 +0800379 read = BM_SSP_CTRL0_READ;
380 }
381
382 ctrl0 = BF_SSP(host->bus_width, CTRL0_BUS_WIDTH) |
383 ignore_crc | get_resp | long_resp |
384 BM_SSP_CTRL0_DATA_XFER | read |
385 BM_SSP_CTRL0_WAIT_FOR_IRQ |
386 BM_SSP_CTRL0_ENABLE;
387
388 cmd0 = BF_SSP(cmd->opcode, CMD0_CMD);
389
390 /* get logarithm to base 2 of block size for setting register */
391 log2_blksz = ilog2(data->blksz);
392
393 /*
394 * take special care of the case that data size from data->sg
395 * is not equal to blocks x blksz
396 */
397 for_each_sg(sgl, sg, sg_len, i)
398 data_size += sg->length;
399
400 if (data_size != data->blocks * data->blksz)
401 blocks = 1;
402
403 /* xfer count, block size and count need to be set differently */
Marek Vasut829c1bf2012-08-03 17:26:09 +0200404 if (ssp_is_old(ssp)) {
Shawn Guoe4243f12011-02-21 18:35:28 +0800405 ctrl0 |= BF_SSP(data_size, CTRL0_XFER_COUNT);
406 cmd0 |= BF_SSP(log2_blksz, CMD0_BLOCK_SIZE) |
407 BF_SSP(blocks - 1, CMD0_BLOCK_COUNT);
408 } else {
Marek Vasut829c1bf2012-08-03 17:26:09 +0200409 writel(data_size, ssp->base + HW_SSP_XFER_SIZE);
Shawn Guoe4243f12011-02-21 18:35:28 +0800410 writel(BF_SSP(log2_blksz, BLOCK_SIZE_BLOCK_SIZE) |
411 BF_SSP(blocks - 1, BLOCK_SIZE_BLOCK_COUNT),
Marek Vasut829c1bf2012-08-03 17:26:09 +0200412 ssp->base + HW_SSP_BLOCK_SIZE);
Shawn Guoe4243f12011-02-21 18:35:28 +0800413 }
414
415 if ((cmd->opcode == MMC_STOP_TRANSMISSION) ||
416 (cmd->opcode == SD_IO_RW_EXTENDED))
417 cmd0 |= BM_SSP_CMD0_APPEND_8CYC;
418
419 cmd1 = cmd->arg;
420
421 if (host->sdio_irq_en) {
422 ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
423 cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN;
424 }
425
426 /* set the timeout count */
Marek Vasut829c1bf2012-08-03 17:26:09 +0200427 timeout = mxs_ns_to_ssp_ticks(ssp->clk_rate, data->timeout_ns);
428 val = readl(ssp->base + HW_SSP_TIMING(ssp));
Shawn Guoe4243f12011-02-21 18:35:28 +0800429 val &= ~(BM_SSP_TIMING_TIMEOUT);
430 val |= BF_SSP(timeout, TIMING_TIMEOUT);
Marek Vasut829c1bf2012-08-03 17:26:09 +0200431 writel(val, ssp->base + HW_SSP_TIMING(ssp));
Shawn Guoe4243f12011-02-21 18:35:28 +0800432
433 /* pio */
Marek Vasut65defb92012-08-03 17:26:12 +0200434 ssp->ssp_pio_words[0] = ctrl0;
435 ssp->ssp_pio_words[1] = cmd0;
436 ssp->ssp_pio_words[2] = cmd1;
437 ssp->dma_dir = DMA_NONE;
438 ssp->slave_dirn = DMA_TRANS_NONE;
Shawn Guoe4243f12011-02-21 18:35:28 +0800439 desc = mxs_mmc_prep_dma(host, 0);
440 if (!desc)
441 goto out;
442
443 /* append data sg */
444 WARN_ON(host->data != NULL);
445 host->data = data;
Marek Vasut65defb92012-08-03 17:26:12 +0200446 ssp->dma_dir = dma_data_dir;
447 ssp->slave_dirn = slave_dirn;
Huang Shijie921de862012-02-16 14:17:33 +0800448 desc = mxs_mmc_prep_dma(host, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Shawn Guoe4243f12011-02-21 18:35:28 +0800449 if (!desc)
450 goto out;
451
452 dmaengine_submit(desc);
Marek Vasut65defb92012-08-03 17:26:12 +0200453 dma_async_issue_pending(ssp->dmach);
Shawn Guoe4243f12011-02-21 18:35:28 +0800454 return;
455out:
456 dev_warn(mmc_dev(host->mmc),
457 "%s: failed to prep dma\n", __func__);
458}
459
460static void mxs_mmc_start_cmd(struct mxs_mmc_host *host,
461 struct mmc_command *cmd)
462{
463 host->cmd = cmd;
464
465 switch (mmc_cmd_type(cmd)) {
466 case MMC_CMD_BC:
467 mxs_mmc_bc(host);
468 break;
469 case MMC_CMD_BCR:
470 mxs_mmc_ac(host);
471 break;
472 case MMC_CMD_AC:
473 mxs_mmc_ac(host);
474 break;
475 case MMC_CMD_ADTC:
476 mxs_mmc_adtc(host);
477 break;
478 default:
479 dev_warn(mmc_dev(host->mmc),
480 "%s: unknown MMC command\n", __func__);
481 break;
482 }
483}
484
485static void mxs_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
486{
487 struct mxs_mmc_host *host = mmc_priv(mmc);
488
489 WARN_ON(host->mrq != NULL);
490 host->mrq = mrq;
491 mxs_mmc_start_cmd(host, mrq->cmd);
492}
493
Shawn Guoe4243f12011-02-21 18:35:28 +0800494static void mxs_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
495{
496 struct mxs_mmc_host *host = mmc_priv(mmc);
497
498 if (ios->bus_width == MMC_BUS_WIDTH_8)
499 host->bus_width = 2;
500 else if (ios->bus_width == MMC_BUS_WIDTH_4)
501 host->bus_width = 1;
502 else
503 host->bus_width = 0;
504
505 if (ios->clock)
Marek Vasut13082392012-08-03 17:26:10 +0200506 mxs_ssp_set_clk_rate(&host->ssp, ios->clock);
Shawn Guoe4243f12011-02-21 18:35:28 +0800507}
508
509static void mxs_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
510{
511 struct mxs_mmc_host *host = mmc_priv(mmc);
Marek Vasut829c1bf2012-08-03 17:26:09 +0200512 struct mxs_ssp *ssp = &host->ssp;
Shawn Guoe4243f12011-02-21 18:35:28 +0800513 unsigned long flags;
514
515 spin_lock_irqsave(&host->lock, flags);
516
517 host->sdio_irq_en = enable;
518
519 if (enable) {
520 writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK,
Marek Vasut829c1bf2012-08-03 17:26:09 +0200521 ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoe4243f12011-02-21 18:35:28 +0800522 writel(BM_SSP_CTRL1_SDIO_IRQ_EN,
Marek Vasut4c5bb2e2012-10-01 20:52:43 +0000523 ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_SET);
Shawn Guoe4243f12011-02-21 18:35:28 +0800524 } else {
525 writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK,
Marek Vasut829c1bf2012-08-03 17:26:09 +0200526 ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
Shawn Guoe4243f12011-02-21 18:35:28 +0800527 writel(BM_SSP_CTRL1_SDIO_IRQ_EN,
Marek Vasut829c1bf2012-08-03 17:26:09 +0200528 ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_CLR);
Shawn Guoe4243f12011-02-21 18:35:28 +0800529 }
530
531 spin_unlock_irqrestore(&host->lock, flags);
Lauri Hintsalafc108d22012-07-17 17:16:10 +0300532
Mark Brownac48f6c2012-09-25 13:41:02 +0100533 if (enable && readl(ssp->base + HW_SSP_STATUS(ssp)) &
Lauri Hintsalafc108d22012-07-17 17:16:10 +0300534 BM_SSP_STATUS_SDIO_IRQ)
535 mmc_signal_sdio_irq(host->mmc);
536
Shawn Guoe4243f12011-02-21 18:35:28 +0800537}
538
539static const struct mmc_host_ops mxs_mmc_ops = {
540 .request = mxs_mmc_request,
Sascha Hauerabd37cc2013-12-05 14:34:50 +0100541 .get_ro = mmc_gpio_get_ro,
Shawn Guoe4243f12011-02-21 18:35:28 +0800542 .get_cd = mxs_mmc_get_cd,
543 .set_ios = mxs_mmc_set_ios,
544 .enable_sdio_irq = mxs_mmc_enable_sdio_irq,
545};
546
Marek Vasut600a9912012-08-03 17:26:07 +0200547static struct platform_device_id mxs_ssp_ids[] = {
Shawn Guoef9b4d32012-05-05 20:24:01 +0800548 {
549 .name = "imx23-mmc",
Marek Vasut600a9912012-08-03 17:26:07 +0200550 .driver_data = IMX23_SSP,
Shawn Guoef9b4d32012-05-05 20:24:01 +0800551 }, {
552 .name = "imx28-mmc",
Marek Vasut600a9912012-08-03 17:26:07 +0200553 .driver_data = IMX28_SSP,
Shawn Guoef9b4d32012-05-05 20:24:01 +0800554 }, {
555 /* sentinel */
556 }
557};
Marek Vasut600a9912012-08-03 17:26:07 +0200558MODULE_DEVICE_TABLE(platform, mxs_ssp_ids);
Shawn Guoef9b4d32012-05-05 20:24:01 +0800559
Shawn Guo6de4d812012-05-06 13:30:44 +0800560static const struct of_device_id mxs_mmc_dt_ids[] = {
Marek Vasut600a9912012-08-03 17:26:07 +0200561 { .compatible = "fsl,imx23-mmc", .data = (void *) IMX23_SSP, },
562 { .compatible = "fsl,imx28-mmc", .data = (void *) IMX28_SSP, },
Shawn Guo6de4d812012-05-06 13:30:44 +0800563 { /* sentinel */ }
564};
565MODULE_DEVICE_TABLE(of, mxs_mmc_dt_ids);
566
Shawn Guoe4243f12011-02-21 18:35:28 +0800567static int mxs_mmc_probe(struct platform_device *pdev)
568{
Shawn Guo6de4d812012-05-06 13:30:44 +0800569 const struct of_device_id *of_id =
570 of_match_device(mxs_mmc_dt_ids, &pdev->dev);
Shawn Guoe4243f12011-02-21 18:35:28 +0800571 struct mxs_mmc_host *host;
572 struct mmc_host *mmc;
Shawn Guo0e91e432013-02-26 10:10:38 +0800573 struct resource *iores;
Sascha Hauerd1a1dfb2013-12-05 14:34:51 +0100574 int ret = 0, irq_err;
Shawn Guo4dc5a792012-06-26 16:38:57 +0800575 struct regulator *reg_vmmc;
Marek Vasut829c1bf2012-08-03 17:26:09 +0200576 struct mxs_ssp *ssp;
Shawn Guoe4243f12011-02-21 18:35:28 +0800577
578 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Shawn Guoe4243f12011-02-21 18:35:28 +0800579 irq_err = platform_get_irq(pdev, 0);
Shawn Guo0e91e432013-02-26 10:10:38 +0800580 if (!iores || irq_err < 0)
Shawn Guoe4243f12011-02-21 18:35:28 +0800581 return -EINVAL;
582
Shawn Guoe4243f12011-02-21 18:35:28 +0800583 mmc = mmc_alloc_host(sizeof(struct mxs_mmc_host), &pdev->dev);
Shawn Guodf06bfc2012-05-06 11:20:40 +0800584 if (!mmc)
585 return -ENOMEM;
Shawn Guoe4243f12011-02-21 18:35:28 +0800586
587 host = mmc_priv(mmc);
Marek Vasut829c1bf2012-08-03 17:26:09 +0200588 ssp = &host->ssp;
589 ssp->dev = &pdev->dev;
Thierry Redinga3e2cd72013-01-21 11:09:11 +0100590 ssp->base = devm_ioremap_resource(&pdev->dev, iores);
591 if (IS_ERR(ssp->base)) {
592 ret = PTR_ERR(ssp->base);
Shawn Guoe4243f12011-02-21 18:35:28 +0800593 goto out_mmc_free;
594 }
595
Shawn Guo0e91e432013-02-26 10:10:38 +0800596 ssp->devid = (enum mxs_ssp_id) of_id->data;
Shawn Guo6de4d812012-05-06 13:30:44 +0800597
Shawn Guoe4243f12011-02-21 18:35:28 +0800598 host->mmc = mmc;
Shawn Guoe4243f12011-02-21 18:35:28 +0800599 host->sdio_irq_en = 0;
600
Shawn Guo4dc5a792012-06-26 16:38:57 +0800601 reg_vmmc = devm_regulator_get(&pdev->dev, "vmmc");
602 if (!IS_ERR(reg_vmmc)) {
603 ret = regulator_enable(reg_vmmc);
604 if (ret) {
605 dev_err(&pdev->dev,
606 "Failed to enable vmmc regulator: %d\n", ret);
607 goto out_mmc_free;
608 }
609 }
610
Fabio Estevama5adbff2013-07-03 17:02:37 -0300611 ssp->clk = devm_clk_get(&pdev->dev, NULL);
Marek Vasut829c1bf2012-08-03 17:26:09 +0200612 if (IS_ERR(ssp->clk)) {
613 ret = PTR_ERR(ssp->clk);
Shawn Guodf06bfc2012-05-06 11:20:40 +0800614 goto out_mmc_free;
Shawn Guoe4243f12011-02-21 18:35:28 +0800615 }
Marek Vasut829c1bf2012-08-03 17:26:09 +0200616 clk_prepare_enable(ssp->clk);
Shawn Guoe4243f12011-02-21 18:35:28 +0800617
Fabio Estevam02c18c42013-07-03 17:02:38 -0300618 ret = mxs_mmc_reset(host);
619 if (ret) {
620 dev_err(&pdev->dev, "Failed to reset mmc: %d\n", ret);
621 goto out_clk_disable;
622 }
Shawn Guoe4243f12011-02-21 18:35:28 +0800623
Shawn Guo0e91e432013-02-26 10:10:38 +0800624 ssp->dmach = dma_request_slave_channel(&pdev->dev, "rx-tx");
Marek Vasut65defb92012-08-03 17:26:12 +0200625 if (!ssp->dmach) {
Shawn Guoe4243f12011-02-21 18:35:28 +0800626 dev_err(mmc_dev(host->mmc),
627 "%s: failed to request dma\n", __func__);
Wei Yongjun0c5ce162013-04-19 10:11:11 +0800628 ret = -ENODEV;
Fabio Estevama5adbff2013-07-03 17:02:37 -0300629 goto out_clk_disable;
Shawn Guoe4243f12011-02-21 18:35:28 +0800630 }
631
632 /* set mmc core parameters */
633 mmc->ops = &mxs_mmc_ops;
634 mmc->caps = MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
635 MMC_CAP_SDIO_IRQ | MMC_CAP_NEEDS_POLL;
636
Shawn Guoe4243f12011-02-21 18:35:28 +0800637 mmc->f_min = 400000;
638 mmc->f_max = 288000000;
Sascha Hauerd1a1dfb2013-12-05 14:34:51 +0100639
640 ret = mmc_of_parse(mmc);
641 if (ret)
642 goto out_clk_disable;
643
Shawn Guoe4243f12011-02-21 18:35:28 +0800644 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
645
646 mmc->max_segs = 52;
647 mmc->max_blk_size = 1 << 0xf;
Marek Vasut829c1bf2012-08-03 17:26:09 +0200648 mmc->max_blk_count = (ssp_is_old(ssp)) ? 0xff : 0xffffff;
649 mmc->max_req_size = (ssp_is_old(ssp)) ? 0xffff : 0xffffffff;
Marek Vasut65defb92012-08-03 17:26:12 +0200650 mmc->max_seg_size = dma_get_max_seg_size(ssp->dmach->device->dev);
Shawn Guoe4243f12011-02-21 18:35:28 +0800651
652 platform_set_drvdata(pdev, mmc);
653
Shawn Guodf06bfc2012-05-06 11:20:40 +0800654 ret = devm_request_irq(&pdev->dev, irq_err, mxs_mmc_irq_handler, 0,
655 DRIVER_NAME, host);
Shawn Guoe4243f12011-02-21 18:35:28 +0800656 if (ret)
657 goto out_free_dma;
658
659 spin_lock_init(&host->lock);
660
661 ret = mmc_add_host(mmc);
662 if (ret)
Shawn Guodf06bfc2012-05-06 11:20:40 +0800663 goto out_free_dma;
Shawn Guoe4243f12011-02-21 18:35:28 +0800664
665 dev_info(mmc_dev(host->mmc), "initialized\n");
666
667 return 0;
668
Shawn Guoe4243f12011-02-21 18:35:28 +0800669out_free_dma:
Marek Vasut65defb92012-08-03 17:26:12 +0200670 if (ssp->dmach)
671 dma_release_channel(ssp->dmach);
Fabio Estevama5adbff2013-07-03 17:02:37 -0300672out_clk_disable:
Marek Vasut829c1bf2012-08-03 17:26:09 +0200673 clk_disable_unprepare(ssp->clk);
Shawn Guoe4243f12011-02-21 18:35:28 +0800674out_mmc_free:
675 mmc_free_host(mmc);
Shawn Guoe4243f12011-02-21 18:35:28 +0800676 return ret;
677}
678
679static int mxs_mmc_remove(struct platform_device *pdev)
680{
681 struct mmc_host *mmc = platform_get_drvdata(pdev);
682 struct mxs_mmc_host *host = mmc_priv(mmc);
Marek Vasut829c1bf2012-08-03 17:26:09 +0200683 struct mxs_ssp *ssp = &host->ssp;
Shawn Guoe4243f12011-02-21 18:35:28 +0800684
685 mmc_remove_host(mmc);
686
Marek Vasut65defb92012-08-03 17:26:12 +0200687 if (ssp->dmach)
688 dma_release_channel(ssp->dmach);
Shawn Guoe4243f12011-02-21 18:35:28 +0800689
Marek Vasut829c1bf2012-08-03 17:26:09 +0200690 clk_disable_unprepare(ssp->clk);
Shawn Guoe4243f12011-02-21 18:35:28 +0800691
Shawn Guoe4243f12011-02-21 18:35:28 +0800692 mmc_free_host(mmc);
693
Shawn Guoe4243f12011-02-21 18:35:28 +0800694 return 0;
695}
696
697#ifdef CONFIG_PM
698static int mxs_mmc_suspend(struct device *dev)
699{
700 struct mmc_host *mmc = dev_get_drvdata(dev);
701 struct mxs_mmc_host *host = mmc_priv(mmc);
Marek Vasut829c1bf2012-08-03 17:26:09 +0200702 struct mxs_ssp *ssp = &host->ssp;
Shawn Guoe4243f12011-02-21 18:35:28 +0800703
Marek Vasut829c1bf2012-08-03 17:26:09 +0200704 clk_disable_unprepare(ssp->clk);
Ulf Hansson68359072013-09-25 15:21:53 +0200705 return 0;
Shawn Guoe4243f12011-02-21 18:35:28 +0800706}
707
708static int mxs_mmc_resume(struct device *dev)
709{
710 struct mmc_host *mmc = dev_get_drvdata(dev);
711 struct mxs_mmc_host *host = mmc_priv(mmc);
Marek Vasut829c1bf2012-08-03 17:26:09 +0200712 struct mxs_ssp *ssp = &host->ssp;
Shawn Guoe4243f12011-02-21 18:35:28 +0800713
Marek Vasut829c1bf2012-08-03 17:26:09 +0200714 clk_prepare_enable(ssp->clk);
Ulf Hansson68359072013-09-25 15:21:53 +0200715 return 0;
Shawn Guoe4243f12011-02-21 18:35:28 +0800716}
717
718static const struct dev_pm_ops mxs_mmc_pm_ops = {
719 .suspend = mxs_mmc_suspend,
720 .resume = mxs_mmc_resume,
721};
722#endif
723
724static struct platform_driver mxs_mmc_driver = {
725 .probe = mxs_mmc_probe,
726 .remove = mxs_mmc_remove,
Marek Vasut600a9912012-08-03 17:26:07 +0200727 .id_table = mxs_ssp_ids,
Shawn Guoe4243f12011-02-21 18:35:28 +0800728 .driver = {
729 .name = DRIVER_NAME,
730 .owner = THIS_MODULE,
731#ifdef CONFIG_PM
732 .pm = &mxs_mmc_pm_ops,
733#endif
Marek Vasuta3e545e2012-05-21 06:33:27 +0200734 .of_match_table = mxs_mmc_dt_ids,
Shawn Guoe4243f12011-02-21 18:35:28 +0800735 },
736};
737
Axel Lind1f81a62011-11-26 12:55:43 +0800738module_platform_driver(mxs_mmc_driver);
Shawn Guoe4243f12011-02-21 18:35:28 +0800739
740MODULE_DESCRIPTION("FREESCALE MXS MMC peripheral");
741MODULE_AUTHOR("Freescale Semiconductor");
742MODULE_LICENSE("GPL");
Fabio Estevame7be4342013-01-07 20:41:52 -0200743MODULE_ALIAS("platform:" DRIVER_NAME);