blob: 996b9a91d6f49bd42b0644a6f633dacf81ffaab8 [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>
26#include <linux/platform_device.h>
27#include <linux/delay.h>
28#include <linux/interrupt.h>
29#include <linux/dma-mapping.h>
30#include <linux/dmaengine.h>
31#include <linux/highmem.h>
32#include <linux/clk.h>
33#include <linux/err.h>
34#include <linux/completion.h>
35#include <linux/mmc/host.h>
36#include <linux/mmc/mmc.h>
37#include <linux/mmc/sdio.h>
38#include <linux/gpio.h>
39#include <linux/regulator/consumer.h>
40
41#include <mach/mxs.h>
42#include <mach/common.h>
43#include <mach/dma.h>
44#include <mach/mmc.h>
45
46#define DRIVER_NAME "mxs-mmc"
47
48/* card detect polling timeout */
49#define MXS_MMC_DETECT_TIMEOUT (HZ/2)
50
51#define SSP_VERSION_LATEST 4
52#define ssp_is_old() (host->version < SSP_VERSION_LATEST)
53
54/* SSP registers */
55#define HW_SSP_CTRL0 0x000
56#define BM_SSP_CTRL0_RUN (1 << 29)
57#define BM_SSP_CTRL0_SDIO_IRQ_CHECK (1 << 28)
58#define BM_SSP_CTRL0_IGNORE_CRC (1 << 26)
59#define BM_SSP_CTRL0_READ (1 << 25)
60#define BM_SSP_CTRL0_DATA_XFER (1 << 24)
61#define BP_SSP_CTRL0_BUS_WIDTH (22)
62#define BM_SSP_CTRL0_BUS_WIDTH (0x3 << 22)
63#define BM_SSP_CTRL0_WAIT_FOR_IRQ (1 << 21)
64#define BM_SSP_CTRL0_LONG_RESP (1 << 19)
65#define BM_SSP_CTRL0_GET_RESP (1 << 17)
66#define BM_SSP_CTRL0_ENABLE (1 << 16)
67#define BP_SSP_CTRL0_XFER_COUNT (0)
68#define BM_SSP_CTRL0_XFER_COUNT (0xffff)
69#define HW_SSP_CMD0 0x010
70#define BM_SSP_CMD0_DBL_DATA_RATE_EN (1 << 25)
71#define BM_SSP_CMD0_SLOW_CLKING_EN (1 << 22)
72#define BM_SSP_CMD0_CONT_CLKING_EN (1 << 21)
73#define BM_SSP_CMD0_APPEND_8CYC (1 << 20)
74#define BP_SSP_CMD0_BLOCK_SIZE (16)
75#define BM_SSP_CMD0_BLOCK_SIZE (0xf << 16)
76#define BP_SSP_CMD0_BLOCK_COUNT (8)
77#define BM_SSP_CMD0_BLOCK_COUNT (0xff << 8)
78#define BP_SSP_CMD0_CMD (0)
79#define BM_SSP_CMD0_CMD (0xff)
80#define HW_SSP_CMD1 0x020
81#define HW_SSP_XFER_SIZE 0x030
82#define HW_SSP_BLOCK_SIZE 0x040
83#define BP_SSP_BLOCK_SIZE_BLOCK_COUNT (4)
84#define BM_SSP_BLOCK_SIZE_BLOCK_COUNT (0xffffff << 4)
85#define BP_SSP_BLOCK_SIZE_BLOCK_SIZE (0)
86#define BM_SSP_BLOCK_SIZE_BLOCK_SIZE (0xf)
87#define HW_SSP_TIMING (ssp_is_old() ? 0x050 : 0x070)
88#define BP_SSP_TIMING_TIMEOUT (16)
89#define BM_SSP_TIMING_TIMEOUT (0xffff << 16)
90#define BP_SSP_TIMING_CLOCK_DIVIDE (8)
91#define BM_SSP_TIMING_CLOCK_DIVIDE (0xff << 8)
92#define BP_SSP_TIMING_CLOCK_RATE (0)
93#define BM_SSP_TIMING_CLOCK_RATE (0xff)
94#define HW_SSP_CTRL1 (ssp_is_old() ? 0x060 : 0x080)
95#define BM_SSP_CTRL1_SDIO_IRQ (1 << 31)
96#define BM_SSP_CTRL1_SDIO_IRQ_EN (1 << 30)
97#define BM_SSP_CTRL1_RESP_ERR_IRQ (1 << 29)
98#define BM_SSP_CTRL1_RESP_ERR_IRQ_EN (1 << 28)
99#define BM_SSP_CTRL1_RESP_TIMEOUT_IRQ (1 << 27)
100#define BM_SSP_CTRL1_RESP_TIMEOUT_IRQ_EN (1 << 26)
101#define BM_SSP_CTRL1_DATA_TIMEOUT_IRQ (1 << 25)
102#define BM_SSP_CTRL1_DATA_TIMEOUT_IRQ_EN (1 << 24)
103#define BM_SSP_CTRL1_DATA_CRC_IRQ (1 << 23)
104#define BM_SSP_CTRL1_DATA_CRC_IRQ_EN (1 << 22)
105#define BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ (1 << 21)
106#define BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ_EN (1 << 20)
107#define BM_SSP_CTRL1_RECV_TIMEOUT_IRQ (1 << 17)
108#define BM_SSP_CTRL1_RECV_TIMEOUT_IRQ_EN (1 << 16)
109#define BM_SSP_CTRL1_FIFO_OVERRUN_IRQ (1 << 15)
110#define BM_SSP_CTRL1_FIFO_OVERRUN_IRQ_EN (1 << 14)
111#define BM_SSP_CTRL1_DMA_ENABLE (1 << 13)
112#define BM_SSP_CTRL1_POLARITY (1 << 9)
113#define BP_SSP_CTRL1_WORD_LENGTH (4)
114#define BM_SSP_CTRL1_WORD_LENGTH (0xf << 4)
115#define BP_SSP_CTRL1_SSP_MODE (0)
116#define BM_SSP_CTRL1_SSP_MODE (0xf)
117#define HW_SSP_SDRESP0 (ssp_is_old() ? 0x080 : 0x0a0)
118#define HW_SSP_SDRESP1 (ssp_is_old() ? 0x090 : 0x0b0)
119#define HW_SSP_SDRESP2 (ssp_is_old() ? 0x0a0 : 0x0c0)
120#define HW_SSP_SDRESP3 (ssp_is_old() ? 0x0b0 : 0x0d0)
121#define HW_SSP_STATUS (ssp_is_old() ? 0x0c0 : 0x100)
122#define BM_SSP_STATUS_CARD_DETECT (1 << 28)
123#define BM_SSP_STATUS_SDIO_IRQ (1 << 17)
124#define HW_SSP_VERSION (cpu_is_mx23() ? 0x110 : 0x130)
125#define BP_SSP_VERSION_MAJOR (24)
126
127#define BF_SSP(value, field) (((value) << BP_SSP_##field) & BM_SSP_##field)
128
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)
137
138#define SSP_PIO_NUM 3
139
140struct mxs_mmc_host {
141 struct mmc_host *mmc;
142 struct mmc_request *mrq;
143 struct mmc_command *cmd;
144 struct mmc_data *data;
145
146 void __iomem *base;
147 int irq;
148 struct resource *res;
149 struct resource *dma_res;
150 struct clk *clk;
151 unsigned int clk_rate;
152
153 struct dma_chan *dmach;
154 struct mxs_dma_data dma_data;
155 unsigned int dma_dir;
Vinod Koul05f57992011-10-14 10:45:11 +0530156 enum dma_transfer_direction slave_dirn;
Shawn Guoe4243f12011-02-21 18:35:28 +0800157 u32 ssp_pio_words[SSP_PIO_NUM];
158
159 unsigned int version;
160 unsigned char bus_width;
161 spinlock_t lock;
162 int sdio_irq_en;
163};
164
165static int mxs_mmc_get_ro(struct mmc_host *mmc)
166{
167 struct mxs_mmc_host *host = mmc_priv(mmc);
168 struct mxs_mmc_platform_data *pdata =
169 mmc_dev(host->mmc)->platform_data;
170
171 if (!pdata)
172 return -EFAULT;
173
174 if (!gpio_is_valid(pdata->wp_gpio))
175 return -EINVAL;
176
177 return gpio_get_value(pdata->wp_gpio);
178}
179
180static int mxs_mmc_get_cd(struct mmc_host *mmc)
181{
182 struct mxs_mmc_host *host = mmc_priv(mmc);
183
184 return !(readl(host->base + HW_SSP_STATUS) &
185 BM_SSP_STATUS_CARD_DETECT);
186}
187
188static void mxs_mmc_reset(struct mxs_mmc_host *host)
189{
190 u32 ctrl0, ctrl1;
191
192 mxs_reset_block(host->base);
193
194 ctrl0 = BM_SSP_CTRL0_IGNORE_CRC;
195 ctrl1 = BF_SSP(0x3, CTRL1_SSP_MODE) |
196 BF_SSP(0x7, CTRL1_WORD_LENGTH) |
197 BM_SSP_CTRL1_DMA_ENABLE |
198 BM_SSP_CTRL1_POLARITY |
199 BM_SSP_CTRL1_RECV_TIMEOUT_IRQ_EN |
200 BM_SSP_CTRL1_DATA_CRC_IRQ_EN |
201 BM_SSP_CTRL1_DATA_TIMEOUT_IRQ_EN |
202 BM_SSP_CTRL1_RESP_TIMEOUT_IRQ_EN |
203 BM_SSP_CTRL1_RESP_ERR_IRQ_EN;
204
205 writel(BF_SSP(0xffff, TIMING_TIMEOUT) |
206 BF_SSP(2, TIMING_CLOCK_DIVIDE) |
207 BF_SSP(0, TIMING_CLOCK_RATE),
208 host->base + HW_SSP_TIMING);
209
210 if (host->sdio_irq_en) {
211 ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
212 ctrl1 |= BM_SSP_CTRL1_SDIO_IRQ_EN;
213 }
214
215 writel(ctrl0, host->base + HW_SSP_CTRL0);
216 writel(ctrl1, host->base + HW_SSP_CTRL1);
217}
218
219static void mxs_mmc_start_cmd(struct mxs_mmc_host *host,
220 struct mmc_command *cmd);
221
222static void mxs_mmc_request_done(struct mxs_mmc_host *host)
223{
224 struct mmc_command *cmd = host->cmd;
225 struct mmc_data *data = host->data;
226 struct mmc_request *mrq = host->mrq;
227
228 if (mmc_resp_type(cmd) & MMC_RSP_PRESENT) {
229 if (mmc_resp_type(cmd) & MMC_RSP_136) {
230 cmd->resp[3] = readl(host->base + HW_SSP_SDRESP0);
231 cmd->resp[2] = readl(host->base + HW_SSP_SDRESP1);
232 cmd->resp[1] = readl(host->base + HW_SSP_SDRESP2);
233 cmd->resp[0] = readl(host->base + HW_SSP_SDRESP3);
234 } else {
235 cmd->resp[0] = readl(host->base + HW_SSP_SDRESP0);
236 }
237 }
238
239 if (data) {
240 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
241 data->sg_len, host->dma_dir);
242 /*
243 * If there was an error on any block, we mark all
244 * data blocks as being in error.
245 */
246 if (!data->error)
247 data->bytes_xfered = data->blocks * data->blksz;
248 else
249 data->bytes_xfered = 0;
250
251 host->data = NULL;
252 if (mrq->stop) {
253 mxs_mmc_start_cmd(host, mrq->stop);
254 return;
255 }
256 }
257
258 host->mrq = NULL;
259 mmc_request_done(host->mmc, mrq);
260}
261
262static void mxs_mmc_dma_irq_callback(void *param)
263{
264 struct mxs_mmc_host *host = param;
265
266 mxs_mmc_request_done(host);
267}
268
269static irqreturn_t mxs_mmc_irq_handler(int irq, void *dev_id)
270{
271 struct mxs_mmc_host *host = dev_id;
272 struct mmc_command *cmd = host->cmd;
273 struct mmc_data *data = host->data;
274 u32 stat;
275
276 spin_lock(&host->lock);
277
278 stat = readl(host->base + HW_SSP_CTRL1);
279 writel(stat & MXS_MMC_IRQ_BITS,
280 host->base + HW_SSP_CTRL1 + MXS_CLR_ADDR);
281
282 if ((stat & BM_SSP_CTRL1_SDIO_IRQ) && (stat & BM_SSP_CTRL1_SDIO_IRQ_EN))
283 mmc_signal_sdio_irq(host->mmc);
284
285 spin_unlock(&host->lock);
286
287 if (stat & BM_SSP_CTRL1_RESP_TIMEOUT_IRQ)
288 cmd->error = -ETIMEDOUT;
289 else if (stat & BM_SSP_CTRL1_RESP_ERR_IRQ)
290 cmd->error = -EIO;
291
292 if (data) {
293 if (stat & (BM_SSP_CTRL1_DATA_TIMEOUT_IRQ |
294 BM_SSP_CTRL1_RECV_TIMEOUT_IRQ))
295 data->error = -ETIMEDOUT;
296 else if (stat & BM_SSP_CTRL1_DATA_CRC_IRQ)
297 data->error = -EILSEQ;
298 else if (stat & (BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ |
299 BM_SSP_CTRL1_FIFO_OVERRUN_IRQ))
300 data->error = -EIO;
301 }
302
303 return IRQ_HANDLED;
304}
305
306static struct dma_async_tx_descriptor *mxs_mmc_prep_dma(
307 struct mxs_mmc_host *host, unsigned int append)
308{
309 struct dma_async_tx_descriptor *desc;
310 struct mmc_data *data = host->data;
311 struct scatterlist * sgl;
312 unsigned int sg_len;
313
314 if (data) {
315 /* data */
316 dma_map_sg(mmc_dev(host->mmc), data->sg,
317 data->sg_len, host->dma_dir);
318 sgl = data->sg;
319 sg_len = data->sg_len;
320 } else {
321 /* pio */
322 sgl = (struct scatterlist *) host->ssp_pio_words;
323 sg_len = SSP_PIO_NUM;
324 }
325
326 desc = host->dmach->device->device_prep_slave_sg(host->dmach,
Vinod Koul05f57992011-10-14 10:45:11 +0530327 sgl, sg_len, host->slave_dirn, append);
Shawn Guoe4243f12011-02-21 18:35:28 +0800328 if (desc) {
329 desc->callback = mxs_mmc_dma_irq_callback;
330 desc->callback_param = host;
331 } else {
332 if (data)
333 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
334 data->sg_len, host->dma_dir);
335 }
336
337 return desc;
338}
339
340static void mxs_mmc_bc(struct mxs_mmc_host *host)
341{
342 struct mmc_command *cmd = host->cmd;
343 struct dma_async_tx_descriptor *desc;
344 u32 ctrl0, cmd0, cmd1;
345
346 ctrl0 = BM_SSP_CTRL0_ENABLE | BM_SSP_CTRL0_IGNORE_CRC;
347 cmd0 = BF_SSP(cmd->opcode, CMD0_CMD) | BM_SSP_CMD0_APPEND_8CYC;
348 cmd1 = cmd->arg;
349
350 if (host->sdio_irq_en) {
351 ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
352 cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN;
353 }
354
355 host->ssp_pio_words[0] = ctrl0;
356 host->ssp_pio_words[1] = cmd0;
357 host->ssp_pio_words[2] = cmd1;
358 host->dma_dir = DMA_NONE;
359 desc = mxs_mmc_prep_dma(host, 0);
360 if (!desc)
361 goto out;
362
363 dmaengine_submit(desc);
364 return;
365
366out:
367 dev_warn(mmc_dev(host->mmc),
368 "%s: failed to prep dma\n", __func__);
369}
370
371static void mxs_mmc_ac(struct mxs_mmc_host *host)
372{
373 struct mmc_command *cmd = host->cmd;
374 struct dma_async_tx_descriptor *desc;
375 u32 ignore_crc, get_resp, long_resp;
376 u32 ctrl0, cmd0, cmd1;
377
378 ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ?
379 0 : BM_SSP_CTRL0_IGNORE_CRC;
380 get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ?
381 BM_SSP_CTRL0_GET_RESP : 0;
382 long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ?
383 BM_SSP_CTRL0_LONG_RESP : 0;
384
385 ctrl0 = BM_SSP_CTRL0_ENABLE | ignore_crc | get_resp | long_resp;
386 cmd0 = BF_SSP(cmd->opcode, CMD0_CMD);
387 cmd1 = cmd->arg;
388
389 if (host->sdio_irq_en) {
390 ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
391 cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN;
392 }
393
394 host->ssp_pio_words[0] = ctrl0;
395 host->ssp_pio_words[1] = cmd0;
396 host->ssp_pio_words[2] = cmd1;
397 host->dma_dir = DMA_NONE;
398 desc = mxs_mmc_prep_dma(host, 0);
399 if (!desc)
400 goto out;
401
402 dmaengine_submit(desc);
403 return;
404
405out:
406 dev_warn(mmc_dev(host->mmc),
407 "%s: failed to prep dma\n", __func__);
408}
409
410static unsigned short mxs_ns_to_ssp_ticks(unsigned clock_rate, unsigned ns)
411{
412 const unsigned int ssp_timeout_mul = 4096;
413 /*
414 * Calculate ticks in ms since ns are large numbers
415 * and might overflow
416 */
417 const unsigned int clock_per_ms = clock_rate / 1000;
418 const unsigned int ms = ns / 1000;
419 const unsigned int ticks = ms * clock_per_ms;
420 const unsigned int ssp_ticks = ticks / ssp_timeout_mul;
421
422 WARN_ON(ssp_ticks == 0);
423 return ssp_ticks;
424}
425
426static void mxs_mmc_adtc(struct mxs_mmc_host *host)
427{
428 struct mmc_command *cmd = host->cmd;
429 struct mmc_data *data = cmd->data;
430 struct dma_async_tx_descriptor *desc;
431 struct scatterlist *sgl = data->sg, *sg;
432 unsigned int sg_len = data->sg_len;
433 int i;
434
435 unsigned short dma_data_dir, timeout;
Vinod Koul05f57992011-10-14 10:45:11 +0530436 enum dma_transfer_direction slave_dirn;
Shawn Guoe4243f12011-02-21 18:35:28 +0800437 unsigned int data_size = 0, log2_blksz;
438 unsigned int blocks = data->blocks;
439
440 u32 ignore_crc, get_resp, long_resp, read;
441 u32 ctrl0, cmd0, cmd1, val;
442
443 ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ?
444 0 : BM_SSP_CTRL0_IGNORE_CRC;
445 get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ?
446 BM_SSP_CTRL0_GET_RESP : 0;
447 long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ?
448 BM_SSP_CTRL0_LONG_RESP : 0;
449
450 if (data->flags & MMC_DATA_WRITE) {
451 dma_data_dir = DMA_TO_DEVICE;
Vinod Koul05f57992011-10-14 10:45:11 +0530452 slave_dirn = DMA_MEM_TO_DEV;
Shawn Guoe4243f12011-02-21 18:35:28 +0800453 read = 0;
454 } else {
455 dma_data_dir = DMA_FROM_DEVICE;
Vinod Koul05f57992011-10-14 10:45:11 +0530456 slave_dirn = DMA_DEV_TO_MEM;
Shawn Guoe4243f12011-02-21 18:35:28 +0800457 read = BM_SSP_CTRL0_READ;
458 }
459
460 ctrl0 = BF_SSP(host->bus_width, CTRL0_BUS_WIDTH) |
461 ignore_crc | get_resp | long_resp |
462 BM_SSP_CTRL0_DATA_XFER | read |
463 BM_SSP_CTRL0_WAIT_FOR_IRQ |
464 BM_SSP_CTRL0_ENABLE;
465
466 cmd0 = BF_SSP(cmd->opcode, CMD0_CMD);
467
468 /* get logarithm to base 2 of block size for setting register */
469 log2_blksz = ilog2(data->blksz);
470
471 /*
472 * take special care of the case that data size from data->sg
473 * is not equal to blocks x blksz
474 */
475 for_each_sg(sgl, sg, sg_len, i)
476 data_size += sg->length;
477
478 if (data_size != data->blocks * data->blksz)
479 blocks = 1;
480
481 /* xfer count, block size and count need to be set differently */
482 if (ssp_is_old()) {
483 ctrl0 |= BF_SSP(data_size, CTRL0_XFER_COUNT);
484 cmd0 |= BF_SSP(log2_blksz, CMD0_BLOCK_SIZE) |
485 BF_SSP(blocks - 1, CMD0_BLOCK_COUNT);
486 } else {
487 writel(data_size, host->base + HW_SSP_XFER_SIZE);
488 writel(BF_SSP(log2_blksz, BLOCK_SIZE_BLOCK_SIZE) |
489 BF_SSP(blocks - 1, BLOCK_SIZE_BLOCK_COUNT),
490 host->base + HW_SSP_BLOCK_SIZE);
491 }
492
493 if ((cmd->opcode == MMC_STOP_TRANSMISSION) ||
494 (cmd->opcode == SD_IO_RW_EXTENDED))
495 cmd0 |= BM_SSP_CMD0_APPEND_8CYC;
496
497 cmd1 = cmd->arg;
498
499 if (host->sdio_irq_en) {
500 ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
501 cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN;
502 }
503
504 /* set the timeout count */
505 timeout = mxs_ns_to_ssp_ticks(host->clk_rate, data->timeout_ns);
506 val = readl(host->base + HW_SSP_TIMING);
507 val &= ~(BM_SSP_TIMING_TIMEOUT);
508 val |= BF_SSP(timeout, TIMING_TIMEOUT);
509 writel(val, host->base + HW_SSP_TIMING);
510
511 /* pio */
512 host->ssp_pio_words[0] = ctrl0;
513 host->ssp_pio_words[1] = cmd0;
514 host->ssp_pio_words[2] = cmd1;
515 host->dma_dir = DMA_NONE;
516 desc = mxs_mmc_prep_dma(host, 0);
517 if (!desc)
518 goto out;
519
520 /* append data sg */
521 WARN_ON(host->data != NULL);
522 host->data = data;
523 host->dma_dir = dma_data_dir;
Vinod Koul05f57992011-10-14 10:45:11 +0530524 host->slave_dirn = slave_dirn;
Shawn Guoe4243f12011-02-21 18:35:28 +0800525 desc = mxs_mmc_prep_dma(host, 1);
526 if (!desc)
527 goto out;
528
529 dmaengine_submit(desc);
530 return;
531out:
532 dev_warn(mmc_dev(host->mmc),
533 "%s: failed to prep dma\n", __func__);
534}
535
536static void mxs_mmc_start_cmd(struct mxs_mmc_host *host,
537 struct mmc_command *cmd)
538{
539 host->cmd = cmd;
540
541 switch (mmc_cmd_type(cmd)) {
542 case MMC_CMD_BC:
543 mxs_mmc_bc(host);
544 break;
545 case MMC_CMD_BCR:
546 mxs_mmc_ac(host);
547 break;
548 case MMC_CMD_AC:
549 mxs_mmc_ac(host);
550 break;
551 case MMC_CMD_ADTC:
552 mxs_mmc_adtc(host);
553 break;
554 default:
555 dev_warn(mmc_dev(host->mmc),
556 "%s: unknown MMC command\n", __func__);
557 break;
558 }
559}
560
561static void mxs_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
562{
563 struct mxs_mmc_host *host = mmc_priv(mmc);
564
565 WARN_ON(host->mrq != NULL);
566 host->mrq = mrq;
567 mxs_mmc_start_cmd(host, mrq->cmd);
568}
569
570static void mxs_mmc_set_clk_rate(struct mxs_mmc_host *host, unsigned int rate)
571{
Koen Beeld982dcd2011-07-15 17:39:00 -0400572 unsigned int ssp_clk, ssp_sck;
573 u32 clock_divide, clock_rate;
Shawn Guoe4243f12011-02-21 18:35:28 +0800574 u32 val;
575
Koen Beeld982dcd2011-07-15 17:39:00 -0400576 ssp_clk = clk_get_rate(host->clk);
Shawn Guoe4243f12011-02-21 18:35:28 +0800577
Koen Beeld982dcd2011-07-15 17:39:00 -0400578 for (clock_divide = 2; clock_divide <= 254; clock_divide += 2) {
579 clock_rate = DIV_ROUND_UP(ssp_clk, rate * clock_divide);
580 clock_rate = (clock_rate > 0) ? clock_rate - 1 : 0;
581 if (clock_rate <= 255)
Shawn Guoe4243f12011-02-21 18:35:28 +0800582 break;
583 }
584
Koen Beeld982dcd2011-07-15 17:39:00 -0400585 if (clock_divide > 254) {
Shawn Guoe4243f12011-02-21 18:35:28 +0800586 dev_err(mmc_dev(host->mmc),
587 "%s: cannot set clock to %d\n", __func__, rate);
588 return;
589 }
590
Koen Beeld982dcd2011-07-15 17:39:00 -0400591 ssp_sck = ssp_clk / clock_divide / (1 + clock_rate);
Shawn Guoe4243f12011-02-21 18:35:28 +0800592
593 val = readl(host->base + HW_SSP_TIMING);
594 val &= ~(BM_SSP_TIMING_CLOCK_DIVIDE | BM_SSP_TIMING_CLOCK_RATE);
Koen Beeld982dcd2011-07-15 17:39:00 -0400595 val |= BF_SSP(clock_divide, TIMING_CLOCK_DIVIDE);
596 val |= BF_SSP(clock_rate, TIMING_CLOCK_RATE);
Shawn Guoe4243f12011-02-21 18:35:28 +0800597 writel(val, host->base + HW_SSP_TIMING);
598
Koen Beeld982dcd2011-07-15 17:39:00 -0400599 host->clk_rate = ssp_sck;
Shawn Guoe4243f12011-02-21 18:35:28 +0800600
601 dev_dbg(mmc_dev(host->mmc),
Koen Beeld982dcd2011-07-15 17:39:00 -0400602 "%s: clock_divide %d, clock_rate %d, ssp_clk %d, rate_actual %d, rate_requested %d\n",
603 __func__, clock_divide, clock_rate, ssp_clk, ssp_sck, rate);
Shawn Guoe4243f12011-02-21 18:35:28 +0800604}
605
606static void mxs_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
607{
608 struct mxs_mmc_host *host = mmc_priv(mmc);
609
610 if (ios->bus_width == MMC_BUS_WIDTH_8)
611 host->bus_width = 2;
612 else if (ios->bus_width == MMC_BUS_WIDTH_4)
613 host->bus_width = 1;
614 else
615 host->bus_width = 0;
616
617 if (ios->clock)
618 mxs_mmc_set_clk_rate(host, ios->clock);
619}
620
621static void mxs_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
622{
623 struct mxs_mmc_host *host = mmc_priv(mmc);
624 unsigned long flags;
625
626 spin_lock_irqsave(&host->lock, flags);
627
628 host->sdio_irq_en = enable;
629
630 if (enable) {
631 writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK,
632 host->base + HW_SSP_CTRL0 + MXS_SET_ADDR);
633 writel(BM_SSP_CTRL1_SDIO_IRQ_EN,
634 host->base + HW_SSP_CTRL1 + MXS_SET_ADDR);
635
636 if (readl(host->base + HW_SSP_STATUS) & BM_SSP_STATUS_SDIO_IRQ)
637 mmc_signal_sdio_irq(host->mmc);
638
639 } else {
640 writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK,
641 host->base + HW_SSP_CTRL0 + MXS_CLR_ADDR);
642 writel(BM_SSP_CTRL1_SDIO_IRQ_EN,
643 host->base + HW_SSP_CTRL1 + MXS_CLR_ADDR);
644 }
645
646 spin_unlock_irqrestore(&host->lock, flags);
647}
648
649static const struct mmc_host_ops mxs_mmc_ops = {
650 .request = mxs_mmc_request,
651 .get_ro = mxs_mmc_get_ro,
652 .get_cd = mxs_mmc_get_cd,
653 .set_ios = mxs_mmc_set_ios,
654 .enable_sdio_irq = mxs_mmc_enable_sdio_irq,
655};
656
657static bool mxs_mmc_dma_filter(struct dma_chan *chan, void *param)
658{
659 struct mxs_mmc_host *host = param;
660
661 if (!mxs_dma_is_apbh(chan))
662 return false;
663
664 if (chan->chan_id != host->dma_res->start)
665 return false;
666
667 chan->private = &host->dma_data;
668
669 return true;
670}
671
672static int mxs_mmc_probe(struct platform_device *pdev)
673{
674 struct mxs_mmc_host *host;
675 struct mmc_host *mmc;
676 struct resource *iores, *dmares, *r;
677 struct mxs_mmc_platform_data *pdata;
678 int ret = 0, irq_err, irq_dma;
679 dma_cap_mask_t mask;
680
681 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
682 dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
683 irq_err = platform_get_irq(pdev, 0);
684 irq_dma = platform_get_irq(pdev, 1);
685 if (!iores || !dmares || irq_err < 0 || irq_dma < 0)
686 return -EINVAL;
687
688 r = request_mem_region(iores->start, resource_size(iores), pdev->name);
689 if (!r)
690 return -EBUSY;
691
692 mmc = mmc_alloc_host(sizeof(struct mxs_mmc_host), &pdev->dev);
693 if (!mmc) {
694 ret = -ENOMEM;
695 goto out_release_mem;
696 }
697
698 host = mmc_priv(mmc);
699 host->base = ioremap(r->start, resource_size(r));
700 if (!host->base) {
701 ret = -ENOMEM;
702 goto out_mmc_free;
703 }
704
705 /* only major verion does matter */
706 host->version = readl(host->base + HW_SSP_VERSION) >>
707 BP_SSP_VERSION_MAJOR;
708
709 host->mmc = mmc;
710 host->res = r;
711 host->dma_res = dmares;
712 host->irq = irq_err;
713 host->sdio_irq_en = 0;
714
715 host->clk = clk_get(&pdev->dev, NULL);
716 if (IS_ERR(host->clk)) {
717 ret = PTR_ERR(host->clk);
718 goto out_iounmap;
719 }
720 clk_enable(host->clk);
721
722 mxs_mmc_reset(host);
723
724 dma_cap_zero(mask);
725 dma_cap_set(DMA_SLAVE, mask);
726 host->dma_data.chan_irq = irq_dma;
727 host->dmach = dma_request_channel(mask, mxs_mmc_dma_filter, host);
728 if (!host->dmach) {
729 dev_err(mmc_dev(host->mmc),
730 "%s: failed to request dma\n", __func__);
731 goto out_clk_put;
732 }
733
734 /* set mmc core parameters */
735 mmc->ops = &mxs_mmc_ops;
736 mmc->caps = MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
737 MMC_CAP_SDIO_IRQ | MMC_CAP_NEEDS_POLL;
738
739 pdata = mmc_dev(host->mmc)->platform_data;
740 if (pdata) {
741 if (pdata->flags & SLOTF_8_BIT_CAPABLE)
742 mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA;
743 if (pdata->flags & SLOTF_4_BIT_CAPABLE)
744 mmc->caps |= MMC_CAP_4_BIT_DATA;
745 }
746
747 mmc->f_min = 400000;
748 mmc->f_max = 288000000;
749 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
750
751 mmc->max_segs = 52;
752 mmc->max_blk_size = 1 << 0xf;
753 mmc->max_blk_count = (ssp_is_old()) ? 0xff : 0xffffff;
754 mmc->max_req_size = (ssp_is_old()) ? 0xffff : 0xffffffff;
755 mmc->max_seg_size = dma_get_max_seg_size(host->dmach->device->dev);
756
757 platform_set_drvdata(pdev, mmc);
758
759 ret = request_irq(host->irq, mxs_mmc_irq_handler, 0, DRIVER_NAME, host);
760 if (ret)
761 goto out_free_dma;
762
763 spin_lock_init(&host->lock);
764
765 ret = mmc_add_host(mmc);
766 if (ret)
767 goto out_free_irq;
768
769 dev_info(mmc_dev(host->mmc), "initialized\n");
770
771 return 0;
772
773out_free_irq:
774 free_irq(host->irq, host);
775out_free_dma:
776 if (host->dmach)
777 dma_release_channel(host->dmach);
778out_clk_put:
779 clk_disable(host->clk);
780 clk_put(host->clk);
781out_iounmap:
782 iounmap(host->base);
783out_mmc_free:
784 mmc_free_host(mmc);
785out_release_mem:
786 release_mem_region(iores->start, resource_size(iores));
787 return ret;
788}
789
790static int mxs_mmc_remove(struct platform_device *pdev)
791{
792 struct mmc_host *mmc = platform_get_drvdata(pdev);
793 struct mxs_mmc_host *host = mmc_priv(mmc);
794 struct resource *res = host->res;
795
796 mmc_remove_host(mmc);
797
798 free_irq(host->irq, host);
799
800 platform_set_drvdata(pdev, NULL);
801
802 if (host->dmach)
803 dma_release_channel(host->dmach);
804
805 clk_disable(host->clk);
806 clk_put(host->clk);
807
808 iounmap(host->base);
809
810 mmc_free_host(mmc);
811
812 release_mem_region(res->start, resource_size(res));
813
814 return 0;
815}
816
817#ifdef CONFIG_PM
818static int mxs_mmc_suspend(struct device *dev)
819{
820 struct mmc_host *mmc = dev_get_drvdata(dev);
821 struct mxs_mmc_host *host = mmc_priv(mmc);
822 int ret = 0;
823
824 ret = mmc_suspend_host(mmc);
825
826 clk_disable(host->clk);
827
828 return ret;
829}
830
831static int mxs_mmc_resume(struct device *dev)
832{
833 struct mmc_host *mmc = dev_get_drvdata(dev);
834 struct mxs_mmc_host *host = mmc_priv(mmc);
835 int ret = 0;
836
837 clk_enable(host->clk);
838
839 ret = mmc_resume_host(mmc);
840
841 return ret;
842}
843
844static const struct dev_pm_ops mxs_mmc_pm_ops = {
845 .suspend = mxs_mmc_suspend,
846 .resume = mxs_mmc_resume,
847};
848#endif
849
850static struct platform_driver mxs_mmc_driver = {
851 .probe = mxs_mmc_probe,
852 .remove = mxs_mmc_remove,
853 .driver = {
854 .name = DRIVER_NAME,
855 .owner = THIS_MODULE,
856#ifdef CONFIG_PM
857 .pm = &mxs_mmc_pm_ops,
858#endif
859 },
860};
861
862static int __init mxs_mmc_init(void)
863{
864 return platform_driver_register(&mxs_mmc_driver);
865}
866
867static void __exit mxs_mmc_exit(void)
868{
869 platform_driver_unregister(&mxs_mmc_driver);
870}
871
872module_init(mxs_mmc_init);
873module_exit(mxs_mmc_exit);
874
875MODULE_DESCRIPTION("FREESCALE MXS MMC peripheral");
876MODULE_AUTHOR("Freescale Semiconductor");
877MODULE_LICENSE("GPL");