blob: 19b289d5dba85832780f68e3f042bee966dcceee [file] [log] [blame]
Will Newtonf95f3852011-01-02 01:11:59 -05001/*
2 * Synopsys DesignWare Multimedia Card Interface driver
3 * (Based on NXP driver for lpc 31xx)
4 *
5 * Copyright (C) 2009 NXP Semiconductors
6 * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
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
14#include <linux/blkdev.h>
15#include <linux/clk.h>
16#include <linux/debugfs.h>
17#include <linux/device.h>
18#include <linux/dma-mapping.h>
19#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/interrupt.h>
22#include <linux/ioport.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
Will Newtonf95f3852011-01-02 01:11:59 -050025#include <linux/seq_file.h>
26#include <linux/slab.h>
27#include <linux/stat.h>
28#include <linux/delay.h>
29#include <linux/irq.h>
30#include <linux/mmc/host.h>
31#include <linux/mmc/mmc.h>
32#include <linux/mmc/dw_mmc.h>
33#include <linux/bitops.h>
Jaehoon Chungc07946a2011-02-25 11:08:14 +090034#include <linux/regulator/consumer.h>
James Hogan1791b13e2011-06-24 13:55:55 +010035#include <linux/workqueue.h>
Thomas Abrahamc91eab42012-09-17 18:16:40 +000036#include <linux/of.h>
Doug Anderson55a6ceb2013-01-11 17:03:53 +000037#include <linux/of_gpio.h>
Will Newtonf95f3852011-01-02 01:11:59 -050038
39#include "dw_mmc.h"
40
41/* Common flag combinations */
Jaehoon Chung3f7eec62013-05-27 13:47:57 +090042#define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
Will Newtonf95f3852011-01-02 01:11:59 -050043 SDMMC_INT_HTO | SDMMC_INT_SBE | \
44 SDMMC_INT_EBE)
45#define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
46 SDMMC_INT_RESP_ERR)
47#define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
48 DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
49#define DW_MCI_SEND_STATUS 1
50#define DW_MCI_RECV_STATUS 2
51#define DW_MCI_DMA_THRESHOLD 16
52
53#ifdef CONFIG_MMC_DW_IDMAC
Joonyoung Shimfc79a4d2013-04-26 15:35:22 +090054#define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
55 SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
56 SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
57 SDMMC_IDMAC_INT_TI)
58
Will Newtonf95f3852011-01-02 01:11:59 -050059struct idmac_desc {
60 u32 des0; /* Control Descriptor */
61#define IDMAC_DES0_DIC BIT(1)
62#define IDMAC_DES0_LD BIT(2)
63#define IDMAC_DES0_FD BIT(3)
64#define IDMAC_DES0_CH BIT(4)
65#define IDMAC_DES0_ER BIT(5)
66#define IDMAC_DES0_CES BIT(30)
67#define IDMAC_DES0_OWN BIT(31)
68
69 u32 des1; /* Buffer sizes */
70#define IDMAC_SET_BUFFER1_SIZE(d, s) \
Shashidhar Hiremath9b7bbe12011-07-29 08:49:50 -040071 ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
Will Newtonf95f3852011-01-02 01:11:59 -050072
73 u32 des2; /* buffer 1 physical address */
74
75 u32 des3; /* buffer 2 physical address */
76};
77#endif /* CONFIG_MMC_DW_IDMAC */
78
79/**
80 * struct dw_mci_slot - MMC slot state
81 * @mmc: The mmc_host representing this slot.
82 * @host: The MMC controller this slot is using.
Doug Andersona70aaa62013-01-11 17:03:50 +000083 * @quirks: Slot-level quirks (DW_MCI_SLOT_QUIRK_XXX)
Doug Anderson55a6ceb2013-01-11 17:03:53 +000084 * @wp_gpio: If gpio_is_valid() we'll use this to read write protect.
Will Newtonf95f3852011-01-02 01:11:59 -050085 * @ctype: Card type for this slot.
86 * @mrq: mmc_request currently being processed or waiting to be
87 * processed, or NULL when the slot is idle.
88 * @queue_node: List node for placing this node in the @queue list of
89 * &struct dw_mci.
90 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
Doug Andersonfdf492a2013-08-31 00:11:43 +090091 * @__clk_old: The last updated clock with reflecting clock divider.
92 * Keeping track of this helps us to avoid spamming the console
93 * with CONFIG_MMC_CLKGATE.
Will Newtonf95f3852011-01-02 01:11:59 -050094 * @flags: Random state bits associated with the slot.
95 * @id: Number of this slot.
96 * @last_detect_state: Most recently observed card detect state.
97 */
98struct dw_mci_slot {
99 struct mmc_host *mmc;
100 struct dw_mci *host;
101
Doug Andersona70aaa62013-01-11 17:03:50 +0000102 int quirks;
Doug Anderson55a6ceb2013-01-11 17:03:53 +0000103 int wp_gpio;
Doug Andersona70aaa62013-01-11 17:03:50 +0000104
Will Newtonf95f3852011-01-02 01:11:59 -0500105 u32 ctype;
106
107 struct mmc_request *mrq;
108 struct list_head queue_node;
109
110 unsigned int clock;
Doug Andersonfdf492a2013-08-31 00:11:43 +0900111 unsigned int __clk_old;
Will Newtonf95f3852011-01-02 01:11:59 -0500112 unsigned long flags;
113#define DW_MMC_CARD_PRESENT 0
114#define DW_MMC_CARD_NEED_INIT 1
115 int id;
116 int last_detect_state;
117};
118
119#if defined(CONFIG_DEBUG_FS)
120static int dw_mci_req_show(struct seq_file *s, void *v)
121{
122 struct dw_mci_slot *slot = s->private;
123 struct mmc_request *mrq;
124 struct mmc_command *cmd;
125 struct mmc_command *stop;
126 struct mmc_data *data;
127
128 /* Make sure we get a consistent snapshot */
129 spin_lock_bh(&slot->host->lock);
130 mrq = slot->mrq;
131
132 if (mrq) {
133 cmd = mrq->cmd;
134 data = mrq->data;
135 stop = mrq->stop;
136
137 if (cmd)
138 seq_printf(s,
139 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
140 cmd->opcode, cmd->arg, cmd->flags,
141 cmd->resp[0], cmd->resp[1], cmd->resp[2],
142 cmd->resp[2], cmd->error);
143 if (data)
144 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
145 data->bytes_xfered, data->blocks,
146 data->blksz, data->flags, data->error);
147 if (stop)
148 seq_printf(s,
149 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
150 stop->opcode, stop->arg, stop->flags,
151 stop->resp[0], stop->resp[1], stop->resp[2],
152 stop->resp[2], stop->error);
153 }
154
155 spin_unlock_bh(&slot->host->lock);
156
157 return 0;
158}
159
160static int dw_mci_req_open(struct inode *inode, struct file *file)
161{
162 return single_open(file, dw_mci_req_show, inode->i_private);
163}
164
165static const struct file_operations dw_mci_req_fops = {
166 .owner = THIS_MODULE,
167 .open = dw_mci_req_open,
168 .read = seq_read,
169 .llseek = seq_lseek,
170 .release = single_release,
171};
172
173static int dw_mci_regs_show(struct seq_file *s, void *v)
174{
175 seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
176 seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
177 seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
178 seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
179 seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
180 seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
181
182 return 0;
183}
184
185static int dw_mci_regs_open(struct inode *inode, struct file *file)
186{
187 return single_open(file, dw_mci_regs_show, inode->i_private);
188}
189
190static const struct file_operations dw_mci_regs_fops = {
191 .owner = THIS_MODULE,
192 .open = dw_mci_regs_open,
193 .read = seq_read,
194 .llseek = seq_lseek,
195 .release = single_release,
196};
197
198static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
199{
200 struct mmc_host *mmc = slot->mmc;
201 struct dw_mci *host = slot->host;
202 struct dentry *root;
203 struct dentry *node;
204
205 root = mmc->debugfs_root;
206 if (!root)
207 return;
208
209 node = debugfs_create_file("regs", S_IRUSR, root, host,
210 &dw_mci_regs_fops);
211 if (!node)
212 goto err;
213
214 node = debugfs_create_file("req", S_IRUSR, root, slot,
215 &dw_mci_req_fops);
216 if (!node)
217 goto err;
218
219 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
220 if (!node)
221 goto err;
222
223 node = debugfs_create_x32("pending_events", S_IRUSR, root,
224 (u32 *)&host->pending_events);
225 if (!node)
226 goto err;
227
228 node = debugfs_create_x32("completed_events", S_IRUSR, root,
229 (u32 *)&host->completed_events);
230 if (!node)
231 goto err;
232
233 return;
234
235err:
236 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
237}
238#endif /* defined(CONFIG_DEBUG_FS) */
239
240static void dw_mci_set_timeout(struct dw_mci *host)
241{
242 /* timeout (maximum) */
243 mci_writel(host, TMOUT, 0xffffffff);
244}
245
246static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
247{
248 struct mmc_data *data;
Thomas Abraham800d78b2012-09-17 18:16:42 +0000249 struct dw_mci_slot *slot = mmc_priv(mmc);
Arnd Bergmanne95baf12012-11-08 14:26:11 +0000250 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
Will Newtonf95f3852011-01-02 01:11:59 -0500251 u32 cmdr;
252 cmd->error = -EINPROGRESS;
253
254 cmdr = cmd->opcode;
255
256 if (cmdr == MMC_STOP_TRANSMISSION)
257 cmdr |= SDMMC_CMD_STOP;
258 else
259 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
260
261 if (cmd->flags & MMC_RSP_PRESENT) {
262 /* We expect a response, so set this bit */
263 cmdr |= SDMMC_CMD_RESP_EXP;
264 if (cmd->flags & MMC_RSP_136)
265 cmdr |= SDMMC_CMD_RESP_LONG;
266 }
267
268 if (cmd->flags & MMC_RSP_CRC)
269 cmdr |= SDMMC_CMD_RESP_CRC;
270
271 data = cmd->data;
272 if (data) {
273 cmdr |= SDMMC_CMD_DAT_EXP;
274 if (data->flags & MMC_DATA_STREAM)
275 cmdr |= SDMMC_CMD_STRM_MODE;
276 if (data->flags & MMC_DATA_WRITE)
277 cmdr |= SDMMC_CMD_DAT_WR;
278 }
279
James Hogancb27a842012-10-16 09:43:08 +0100280 if (drv_data && drv_data->prepare_command)
281 drv_data->prepare_command(slot->host, &cmdr);
Thomas Abraham800d78b2012-09-17 18:16:42 +0000282
Will Newtonf95f3852011-01-02 01:11:59 -0500283 return cmdr;
284}
285
286static void dw_mci_start_command(struct dw_mci *host,
287 struct mmc_command *cmd, u32 cmd_flags)
288{
289 host->cmd = cmd;
Thomas Abraham4a909202012-09-17 18:16:35 +0000290 dev_vdbg(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -0500291 "start command: ARGR=0x%08x CMDR=0x%08x\n",
292 cmd->arg, cmd_flags);
293
294 mci_writel(host, CMDARG, cmd->arg);
295 wmb();
296
297 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
298}
299
300static void send_stop_cmd(struct dw_mci *host, struct mmc_data *data)
301{
302 dw_mci_start_command(host, data->stop, host->stop_cmdr);
303}
304
305/* DMA interface functions */
306static void dw_mci_stop_dma(struct dw_mci *host)
307{
James Hogan03e8cb52011-06-29 09:28:43 +0100308 if (host->using_dma) {
Will Newtonf95f3852011-01-02 01:11:59 -0500309 host->dma_ops->stop(host);
310 host->dma_ops->cleanup(host);
311 } else {
312 /* Data transfer was stopped by the interrupt handler */
313 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
314 }
315}
316
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900317static int dw_mci_get_dma_dir(struct mmc_data *data)
318{
319 if (data->flags & MMC_DATA_WRITE)
320 return DMA_TO_DEVICE;
321 else
322 return DMA_FROM_DEVICE;
323}
324
Jaehoon Chung9beee912012-02-16 11:19:38 +0900325#ifdef CONFIG_MMC_DW_IDMAC
Will Newtonf95f3852011-01-02 01:11:59 -0500326static void dw_mci_dma_cleanup(struct dw_mci *host)
327{
328 struct mmc_data *data = host->data;
329
330 if (data)
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900331 if (!data->host_cookie)
Thomas Abraham4a909202012-09-17 18:16:35 +0000332 dma_unmap_sg(host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900333 data->sg,
334 data->sg_len,
335 dw_mci_get_dma_dir(data));
Will Newtonf95f3852011-01-02 01:11:59 -0500336}
337
338static void dw_mci_idmac_stop_dma(struct dw_mci *host)
339{
340 u32 temp;
341
342 /* Disable and reset the IDMAC interface */
343 temp = mci_readl(host, CTRL);
344 temp &= ~SDMMC_CTRL_USE_IDMAC;
345 temp |= SDMMC_CTRL_DMA_RESET;
346 mci_writel(host, CTRL, temp);
347
348 /* Stop the IDMAC running */
349 temp = mci_readl(host, BMOD);
Jaehoon Chunga5289a42011-02-25 11:08:13 +0900350 temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
Will Newtonf95f3852011-01-02 01:11:59 -0500351 mci_writel(host, BMOD, temp);
352}
353
354static void dw_mci_idmac_complete_dma(struct dw_mci *host)
355{
356 struct mmc_data *data = host->data;
357
Thomas Abraham4a909202012-09-17 18:16:35 +0000358 dev_vdbg(host->dev, "DMA complete\n");
Will Newtonf95f3852011-01-02 01:11:59 -0500359
360 host->dma_ops->cleanup(host);
361
362 /*
363 * If the card was removed, data will be NULL. No point in trying to
364 * send the stop command or waiting for NBUSY in this case.
365 */
366 if (data) {
367 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
368 tasklet_schedule(&host->tasklet);
369 }
370}
371
372static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
373 unsigned int sg_len)
374{
375 int i;
376 struct idmac_desc *desc = host->sg_cpu;
377
378 for (i = 0; i < sg_len; i++, desc++) {
379 unsigned int length = sg_dma_len(&data->sg[i]);
380 u32 mem_addr = sg_dma_address(&data->sg[i]);
381
382 /* Set the OWN bit and disable interrupts for this descriptor */
383 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | IDMAC_DES0_CH;
384
385 /* Buffer length */
386 IDMAC_SET_BUFFER1_SIZE(desc, length);
387
388 /* Physical address to DMA to/from */
389 desc->des2 = mem_addr;
390 }
391
392 /* Set first descriptor */
393 desc = host->sg_cpu;
394 desc->des0 |= IDMAC_DES0_FD;
395
396 /* Set last descriptor */
397 desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc);
398 desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
399 desc->des0 |= IDMAC_DES0_LD;
400
401 wmb();
402}
403
404static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
405{
406 u32 temp;
407
408 dw_mci_translate_sglist(host, host->data, sg_len);
409
410 /* Select IDMAC interface */
411 temp = mci_readl(host, CTRL);
412 temp |= SDMMC_CTRL_USE_IDMAC;
413 mci_writel(host, CTRL, temp);
414
415 wmb();
416
417 /* Enable the IDMAC */
418 temp = mci_readl(host, BMOD);
Jaehoon Chunga5289a42011-02-25 11:08:13 +0900419 temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
Will Newtonf95f3852011-01-02 01:11:59 -0500420 mci_writel(host, BMOD, temp);
421
422 /* Start it running */
423 mci_writel(host, PLDMND, 1);
424}
425
426static int dw_mci_idmac_init(struct dw_mci *host)
427{
428 struct idmac_desc *p;
Seungwon Jeon897b69e2012-09-19 13:58:31 +0800429 int i;
Will Newtonf95f3852011-01-02 01:11:59 -0500430
431 /* Number of descriptors in the ring buffer */
432 host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
433
434 /* Forward link the descriptor list */
435 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++)
436 p->des3 = host->sg_dma + (sizeof(struct idmac_desc) * (i + 1));
437
438 /* Set the last descriptor as the end-of-ring descriptor */
439 p->des3 = host->sg_dma;
440 p->des0 = IDMAC_DES0_ER;
441
Seungwon Jeon141a7122012-05-22 13:01:03 +0900442 mci_writel(host, BMOD, SDMMC_IDMAC_SWRESET);
443
Will Newtonf95f3852011-01-02 01:11:59 -0500444 /* Mask out interrupts - get Tx & Rx complete only */
Joonyoung Shimfc79a4d2013-04-26 15:35:22 +0900445 mci_writel(host, IDSTS, IDMAC_INT_CLR);
Will Newtonf95f3852011-01-02 01:11:59 -0500446 mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI | SDMMC_IDMAC_INT_RI |
447 SDMMC_IDMAC_INT_TI);
448
449 /* Set the descriptor base address */
450 mci_writel(host, DBADDR, host->sg_dma);
451 return 0;
452}
453
Arnd Bergmann8e2b36e2012-11-06 22:55:31 +0100454static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900455 .init = dw_mci_idmac_init,
456 .start = dw_mci_idmac_start_dma,
457 .stop = dw_mci_idmac_stop_dma,
458 .complete = dw_mci_idmac_complete_dma,
459 .cleanup = dw_mci_dma_cleanup,
460};
461#endif /* CONFIG_MMC_DW_IDMAC */
462
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900463static int dw_mci_pre_dma_transfer(struct dw_mci *host,
464 struct mmc_data *data,
465 bool next)
Will Newtonf95f3852011-01-02 01:11:59 -0500466{
467 struct scatterlist *sg;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900468 unsigned int i, sg_len;
Will Newtonf95f3852011-01-02 01:11:59 -0500469
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900470 if (!next && data->host_cookie)
471 return data->host_cookie;
Will Newtonf95f3852011-01-02 01:11:59 -0500472
473 /*
474 * We don't do DMA on "complex" transfers, i.e. with
475 * non-word-aligned buffers or lengths. Also, we don't bother
476 * with all the DMA setup overhead for short transfers.
477 */
478 if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
479 return -EINVAL;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900480
Will Newtonf95f3852011-01-02 01:11:59 -0500481 if (data->blksz & 3)
482 return -EINVAL;
483
484 for_each_sg(data->sg, sg, data->sg_len, i) {
485 if (sg->offset & 3 || sg->length & 3)
486 return -EINVAL;
487 }
488
Thomas Abraham4a909202012-09-17 18:16:35 +0000489 sg_len = dma_map_sg(host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900490 data->sg,
491 data->sg_len,
492 dw_mci_get_dma_dir(data));
493 if (sg_len == 0)
494 return -EINVAL;
495
496 if (next)
497 data->host_cookie = sg_len;
498
499 return sg_len;
500}
501
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900502static void dw_mci_pre_req(struct mmc_host *mmc,
503 struct mmc_request *mrq,
504 bool is_first_req)
505{
506 struct dw_mci_slot *slot = mmc_priv(mmc);
507 struct mmc_data *data = mrq->data;
508
509 if (!slot->host->use_dma || !data)
510 return;
511
512 if (data->host_cookie) {
513 data->host_cookie = 0;
514 return;
515 }
516
517 if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0)
518 data->host_cookie = 0;
519}
520
521static void dw_mci_post_req(struct mmc_host *mmc,
522 struct mmc_request *mrq,
523 int err)
524{
525 struct dw_mci_slot *slot = mmc_priv(mmc);
526 struct mmc_data *data = mrq->data;
527
528 if (!slot->host->use_dma || !data)
529 return;
530
531 if (data->host_cookie)
Thomas Abraham4a909202012-09-17 18:16:35 +0000532 dma_unmap_sg(slot->host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900533 data->sg,
534 data->sg_len,
535 dw_mci_get_dma_dir(data));
536 data->host_cookie = 0;
537}
538
539static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
540{
541 int sg_len;
542 u32 temp;
543
544 host->using_dma = 0;
545
546 /* If we don't have a channel, we can't do DMA */
547 if (!host->use_dma)
548 return -ENODEV;
549
550 sg_len = dw_mci_pre_dma_transfer(host, data, 0);
Seungwon Jeona99aa9b2012-04-10 09:53:32 +0900551 if (sg_len < 0) {
552 host->dma_ops->stop(host);
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900553 return sg_len;
Seungwon Jeona99aa9b2012-04-10 09:53:32 +0900554 }
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900555
James Hogan03e8cb52011-06-29 09:28:43 +0100556 host->using_dma = 1;
557
Thomas Abraham4a909202012-09-17 18:16:35 +0000558 dev_vdbg(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -0500559 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
560 (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma,
561 sg_len);
562
563 /* Enable the DMA interface */
564 temp = mci_readl(host, CTRL);
565 temp |= SDMMC_CTRL_DMA_ENABLE;
566 mci_writel(host, CTRL, temp);
567
568 /* Disable RX/TX IRQs, let DMA handle it */
569 temp = mci_readl(host, INTMASK);
570 temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
571 mci_writel(host, INTMASK, temp);
572
573 host->dma_ops->start(host, sg_len);
574
575 return 0;
576}
577
578static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
579{
580 u32 temp;
581
582 data->error = -EINPROGRESS;
583
584 WARN_ON(host->data);
585 host->sg = NULL;
586 host->data = data;
587
James Hogan55c5efbc2011-06-29 09:29:58 +0100588 if (data->flags & MMC_DATA_READ)
589 host->dir_status = DW_MCI_RECV_STATUS;
590 else
591 host->dir_status = DW_MCI_SEND_STATUS;
592
Will Newtonf95f3852011-01-02 01:11:59 -0500593 if (dw_mci_submit_data_dma(host, data)) {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +0900594 int flags = SG_MITER_ATOMIC;
595 if (host->data->flags & MMC_DATA_READ)
596 flags |= SG_MITER_TO_SG;
597 else
598 flags |= SG_MITER_FROM_SG;
599
600 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Will Newtonf95f3852011-01-02 01:11:59 -0500601 host->sg = data->sg;
James Hogan34b664a2011-06-24 13:57:56 +0100602 host->part_buf_start = 0;
603 host->part_buf_count = 0;
Will Newtonf95f3852011-01-02 01:11:59 -0500604
James Hoganb40af3a2011-06-24 13:54:06 +0100605 mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
Will Newtonf95f3852011-01-02 01:11:59 -0500606 temp = mci_readl(host, INTMASK);
607 temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
608 mci_writel(host, INTMASK, temp);
609
610 temp = mci_readl(host, CTRL);
611 temp &= ~SDMMC_CTRL_DMA_ENABLE;
612 mci_writel(host, CTRL, temp);
613 }
614}
615
616static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
617{
618 struct dw_mci *host = slot->host;
619 unsigned long timeout = jiffies + msecs_to_jiffies(500);
620 unsigned int cmd_status = 0;
621
622 mci_writel(host, CMDARG, arg);
623 wmb();
624 mci_writel(host, CMD, SDMMC_CMD_START | cmd);
625
626 while (time_before(jiffies, timeout)) {
627 cmd_status = mci_readl(host, CMD);
628 if (!(cmd_status & SDMMC_CMD_START))
629 return;
630 }
631 dev_err(&slot->mmc->class_dev,
632 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
633 cmd, arg, cmd_status);
634}
635
Abhilash Kesavanab269122012-11-19 10:26:21 +0530636static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
Will Newtonf95f3852011-01-02 01:11:59 -0500637{
638 struct dw_mci *host = slot->host;
Doug Andersonfdf492a2013-08-31 00:11:43 +0900639 unsigned int clock = slot->clock;
Will Newtonf95f3852011-01-02 01:11:59 -0500640 u32 div;
Doug Anderson9623b5b2012-07-25 08:33:17 -0700641 u32 clk_en_a;
Will Newtonf95f3852011-01-02 01:11:59 -0500642
Doug Andersonfdf492a2013-08-31 00:11:43 +0900643 if (!clock) {
644 mci_writel(host, CLKENA, 0);
645 mci_send_cmd(slot,
646 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
647 } else if (clock != host->current_speed || force_clkinit) {
648 div = host->bus_hz / clock;
649 if (host->bus_hz % clock && host->bus_hz > clock)
Will Newtonf95f3852011-01-02 01:11:59 -0500650 /*
651 * move the + 1 after the divide to prevent
652 * over-clocking the card.
653 */
Seungwon Jeone4199902012-05-22 13:01:21 +0900654 div += 1;
655
Doug Andersonfdf492a2013-08-31 00:11:43 +0900656 div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
Will Newtonf95f3852011-01-02 01:11:59 -0500657
Doug Andersonfdf492a2013-08-31 00:11:43 +0900658 if ((clock << div) != slot->__clk_old || force_clkinit)
659 dev_info(&slot->mmc->class_dev,
660 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
661 slot->id, host->bus_hz, clock,
662 div ? ((host->bus_hz / div) >> 1) :
663 host->bus_hz, div);
Will Newtonf95f3852011-01-02 01:11:59 -0500664
665 /* disable clock */
666 mci_writel(host, CLKENA, 0);
667 mci_writel(host, CLKSRC, 0);
668
669 /* inform CIU */
670 mci_send_cmd(slot,
671 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
672
673 /* set clock to desired speed */
674 mci_writel(host, CLKDIV, div);
675
676 /* inform CIU */
677 mci_send_cmd(slot,
678 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
679
Doug Anderson9623b5b2012-07-25 08:33:17 -0700680 /* enable clock; only low power if no SDIO */
681 clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
682 if (!(mci_readl(host, INTMASK) & SDMMC_INT_SDIO(slot->id)))
683 clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
684 mci_writel(host, CLKENA, clk_en_a);
Will Newtonf95f3852011-01-02 01:11:59 -0500685
686 /* inform CIU */
687 mci_send_cmd(slot,
688 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
689
Doug Andersonfdf492a2013-08-31 00:11:43 +0900690 /* keep the clock with reflecting clock dividor */
691 slot->__clk_old = clock << div;
Will Newtonf95f3852011-01-02 01:11:59 -0500692 }
693
Doug Andersonfdf492a2013-08-31 00:11:43 +0900694 host->current_speed = clock;
695
Will Newtonf95f3852011-01-02 01:11:59 -0500696 /* Set the current slot bus width */
Seungwon Jeon1d56c452011-06-20 17:23:53 +0900697 mci_writel(host, CTYPE, (slot->ctype << slot->id));
Will Newtonf95f3852011-01-02 01:11:59 -0500698}
699
Seungwon Jeon053b3ce2011-12-22 18:01:29 +0900700static void __dw_mci_start_request(struct dw_mci *host,
701 struct dw_mci_slot *slot,
702 struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -0500703{
704 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -0500705 struct mmc_data *data;
706 u32 cmdflags;
707
708 mrq = slot->mrq;
709 if (host->pdata->select_slot)
710 host->pdata->select_slot(slot->id);
711
Will Newtonf95f3852011-01-02 01:11:59 -0500712 host->cur_slot = slot;
713 host->mrq = mrq;
714
715 host->pending_events = 0;
716 host->completed_events = 0;
717 host->data_status = 0;
718
Seungwon Jeon053b3ce2011-12-22 18:01:29 +0900719 data = cmd->data;
Will Newtonf95f3852011-01-02 01:11:59 -0500720 if (data) {
721 dw_mci_set_timeout(host);
722 mci_writel(host, BYTCNT, data->blksz*data->blocks);
723 mci_writel(host, BLKSIZ, data->blksz);
724 }
725
Will Newtonf95f3852011-01-02 01:11:59 -0500726 cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
727
728 /* this is the first command, send the initialization clock */
729 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
730 cmdflags |= SDMMC_CMD_INIT;
731
732 if (data) {
733 dw_mci_submit_data(host, data);
734 wmb();
735 }
736
737 dw_mci_start_command(host, cmd, cmdflags);
738
739 if (mrq->stop)
740 host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
741}
742
Seungwon Jeon053b3ce2011-12-22 18:01:29 +0900743static void dw_mci_start_request(struct dw_mci *host,
744 struct dw_mci_slot *slot)
745{
746 struct mmc_request *mrq = slot->mrq;
747 struct mmc_command *cmd;
748
749 cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
750 __dw_mci_start_request(host, slot, cmd);
751}
752
James Hogan7456caa2011-06-24 13:55:10 +0100753/* must be called with host->lock held */
Will Newtonf95f3852011-01-02 01:11:59 -0500754static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
755 struct mmc_request *mrq)
756{
757 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
758 host->state);
759
Will Newtonf95f3852011-01-02 01:11:59 -0500760 slot->mrq = mrq;
761
762 if (host->state == STATE_IDLE) {
763 host->state = STATE_SENDING_CMD;
764 dw_mci_start_request(host, slot);
765 } else {
766 list_add_tail(&slot->queue_node, &host->queue);
767 }
Will Newtonf95f3852011-01-02 01:11:59 -0500768}
769
770static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
771{
772 struct dw_mci_slot *slot = mmc_priv(mmc);
773 struct dw_mci *host = slot->host;
774
775 WARN_ON(slot->mrq);
776
James Hogan7456caa2011-06-24 13:55:10 +0100777 /*
778 * The check for card presence and queueing of the request must be
779 * atomic, otherwise the card could be removed in between and the
780 * request wouldn't fail until another card was inserted.
781 */
782 spin_lock_bh(&host->lock);
783
Will Newtonf95f3852011-01-02 01:11:59 -0500784 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
James Hogan7456caa2011-06-24 13:55:10 +0100785 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -0500786 mrq->cmd->error = -ENOMEDIUM;
787 mmc_request_done(mmc, mrq);
788 return;
789 }
790
Will Newtonf95f3852011-01-02 01:11:59 -0500791 dw_mci_queue_request(host, slot, mrq);
James Hogan7456caa2011-06-24 13:55:10 +0100792
793 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -0500794}
795
796static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
797{
798 struct dw_mci_slot *slot = mmc_priv(mmc);
Arnd Bergmanne95baf12012-11-08 14:26:11 +0000799 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
Jaehoon Chung41babf72011-02-24 13:46:11 +0900800 u32 regs;
Will Newtonf95f3852011-01-02 01:11:59 -0500801
Will Newtonf95f3852011-01-02 01:11:59 -0500802 switch (ios->bus_width) {
Will Newtonf95f3852011-01-02 01:11:59 -0500803 case MMC_BUS_WIDTH_4:
804 slot->ctype = SDMMC_CTYPE_4BIT;
805 break;
Jaehoon Chungc9b2a062011-02-17 16:12:38 +0900806 case MMC_BUS_WIDTH_8:
807 slot->ctype = SDMMC_CTYPE_8BIT;
808 break;
Jaehoon Chungb2f7cb42012-11-08 17:35:31 +0900809 default:
810 /* set default 1 bit mode */
811 slot->ctype = SDMMC_CTYPE_1BIT;
Will Newtonf95f3852011-01-02 01:11:59 -0500812 }
813
Seungwon Jeon3f514292012-01-02 16:00:02 +0900814 regs = mci_readl(slot->host, UHS_REG);
815
Jaehoon Chung41babf72011-02-24 13:46:11 +0900816 /* DDR mode set */
Seungwon Jeon3f514292012-01-02 16:00:02 +0900817 if (ios->timing == MMC_TIMING_UHS_DDR50)
Hyeonsu Kimc69042a2013-02-22 09:32:46 +0900818 regs |= ((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +0900819 else
Hyeonsu Kimc69042a2013-02-22 09:32:46 +0900820 regs &= ~((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +0900821
822 mci_writel(slot->host, UHS_REG, regs);
Jaehoon Chung41babf72011-02-24 13:46:11 +0900823
Doug Andersonfdf492a2013-08-31 00:11:43 +0900824 /*
825 * Use mirror of ios->clock to prevent race with mmc
826 * core ios update when finding the minimum.
827 */
828 slot->clock = ios->clock;
Will Newtonf95f3852011-01-02 01:11:59 -0500829
James Hogancb27a842012-10-16 09:43:08 +0100830 if (drv_data && drv_data->set_ios)
831 drv_data->set_ios(slot->host, ios);
Thomas Abraham800d78b2012-09-17 18:16:42 +0000832
Jaehoon Chungbf7cb222012-11-08 17:35:29 +0900833 /* Slot specific timing and width adjustment */
834 dw_mci_setup_bus(slot, false);
835
Will Newtonf95f3852011-01-02 01:11:59 -0500836 switch (ios->power_mode) {
837 case MMC_POWER_UP:
838 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
James Hogane6f34e22013-03-12 10:43:32 +0000839 /* Power up slot */
840 if (slot->host->pdata->setpower)
841 slot->host->pdata->setpower(slot->id, mmc->ocr_avail);
Jaehoon Chung4366dcc2013-03-26 21:36:14 +0900842 regs = mci_readl(slot->host, PWREN);
843 regs |= (1 << slot->id);
844 mci_writel(slot->host, PWREN, regs);
James Hogane6f34e22013-03-12 10:43:32 +0000845 break;
846 case MMC_POWER_OFF:
847 /* Power down slot */
848 if (slot->host->pdata->setpower)
849 slot->host->pdata->setpower(slot->id, 0);
Jaehoon Chung4366dcc2013-03-26 21:36:14 +0900850 regs = mci_readl(slot->host, PWREN);
851 regs &= ~(1 << slot->id);
852 mci_writel(slot->host, PWREN, regs);
Will Newtonf95f3852011-01-02 01:11:59 -0500853 break;
854 default:
855 break;
856 }
857}
858
859static int dw_mci_get_ro(struct mmc_host *mmc)
860{
861 int read_only;
862 struct dw_mci_slot *slot = mmc_priv(mmc);
863 struct dw_mci_board *brd = slot->host->pdata;
864
865 /* Use platform get_ro function, else try on board write protect */
Doug Anderson96406392013-01-11 17:03:54 +0000866 if (slot->quirks & DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT)
Thomas Abrahamb4967aa2012-09-17 18:16:39 +0000867 read_only = 0;
868 else if (brd->get_ro)
Will Newtonf95f3852011-01-02 01:11:59 -0500869 read_only = brd->get_ro(slot->id);
Doug Anderson55a6ceb2013-01-11 17:03:53 +0000870 else if (gpio_is_valid(slot->wp_gpio))
871 read_only = gpio_get_value(slot->wp_gpio);
Will Newtonf95f3852011-01-02 01:11:59 -0500872 else
873 read_only =
874 mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
875
876 dev_dbg(&mmc->class_dev, "card is %s\n",
877 read_only ? "read-only" : "read-write");
878
879 return read_only;
880}
881
882static int dw_mci_get_cd(struct mmc_host *mmc)
883{
884 int present;
885 struct dw_mci_slot *slot = mmc_priv(mmc);
886 struct dw_mci_board *brd = slot->host->pdata;
887
888 /* Use platform get_cd function, else try onboard card detect */
Jaehoon Chungfc3d7722011-02-25 11:08:15 +0900889 if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
890 present = 1;
891 else if (brd->get_cd)
Will Newtonf95f3852011-01-02 01:11:59 -0500892 present = !brd->get_cd(slot->id);
893 else
894 present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
895 == 0 ? 1 : 0;
896
897 if (present)
898 dev_dbg(&mmc->class_dev, "card is present\n");
899 else
900 dev_dbg(&mmc->class_dev, "card is not present\n");
901
902 return present;
903}
904
Doug Anderson9623b5b2012-07-25 08:33:17 -0700905/*
906 * Disable lower power mode.
907 *
908 * Low power mode will stop the card clock when idle. According to the
909 * description of the CLKENA register we should disable low power mode
910 * for SDIO cards if we need SDIO interrupts to work.
911 *
912 * This function is fast if low power mode is already disabled.
913 */
914static void dw_mci_disable_low_power(struct dw_mci_slot *slot)
915{
916 struct dw_mci *host = slot->host;
917 u32 clk_en_a;
918 const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
919
920 clk_en_a = mci_readl(host, CLKENA);
921
922 if (clk_en_a & clken_low_pwr) {
923 mci_writel(host, CLKENA, clk_en_a & ~clken_low_pwr);
924 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
925 SDMMC_CMD_PRV_DAT_WAIT, 0);
926 }
927}
928
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +0530929static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
930{
931 struct dw_mci_slot *slot = mmc_priv(mmc);
932 struct dw_mci *host = slot->host;
933 u32 int_mask;
934
935 /* Enable/disable Slot Specific SDIO interrupt */
936 int_mask = mci_readl(host, INTMASK);
937 if (enb) {
Doug Anderson9623b5b2012-07-25 08:33:17 -0700938 /*
939 * Turn off low power mode if it was enabled. This is a bit of
940 * a heavy operation and we disable / enable IRQs a lot, so
941 * we'll leave low power mode disabled and it will get
942 * re-enabled again in dw_mci_setup_bus().
943 */
944 dw_mci_disable_low_power(slot);
945
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +0530946 mci_writel(host, INTMASK,
Kyoungil Kim705ad042012-05-14 17:38:48 +0900947 (int_mask | SDMMC_INT_SDIO(slot->id)));
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +0530948 } else {
949 mci_writel(host, INTMASK,
Kyoungil Kim705ad042012-05-14 17:38:48 +0900950 (int_mask & ~SDMMC_INT_SDIO(slot->id)));
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +0530951 }
952}
953
Will Newtonf95f3852011-01-02 01:11:59 -0500954static const struct mmc_host_ops dw_mci_ops = {
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +0530955 .request = dw_mci_request,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900956 .pre_req = dw_mci_pre_req,
957 .post_req = dw_mci_post_req,
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +0530958 .set_ios = dw_mci_set_ios,
959 .get_ro = dw_mci_get_ro,
960 .get_cd = dw_mci_get_cd,
961 .enable_sdio_irq = dw_mci_enable_sdio_irq,
Will Newtonf95f3852011-01-02 01:11:59 -0500962};
963
964static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
965 __releases(&host->lock)
966 __acquires(&host->lock)
967{
968 struct dw_mci_slot *slot;
969 struct mmc_host *prev_mmc = host->cur_slot->mmc;
970
971 WARN_ON(host->cmd || host->data);
972
973 host->cur_slot->mrq = NULL;
974 host->mrq = NULL;
975 if (!list_empty(&host->queue)) {
976 slot = list_entry(host->queue.next,
977 struct dw_mci_slot, queue_node);
978 list_del(&slot->queue_node);
Thomas Abraham4a909202012-09-17 18:16:35 +0000979 dev_vdbg(host->dev, "list not empty: %s is next\n",
Will Newtonf95f3852011-01-02 01:11:59 -0500980 mmc_hostname(slot->mmc));
981 host->state = STATE_SENDING_CMD;
982 dw_mci_start_request(host, slot);
983 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +0000984 dev_vdbg(host->dev, "list empty\n");
Will Newtonf95f3852011-01-02 01:11:59 -0500985 host->state = STATE_IDLE;
986 }
987
988 spin_unlock(&host->lock);
989 mmc_request_done(prev_mmc, mrq);
990 spin_lock(&host->lock);
991}
992
993static void dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
994{
995 u32 status = host->cmd_status;
996
997 host->cmd_status = 0;
998
999 /* Read the response from the card (up to 16 bytes) */
1000 if (cmd->flags & MMC_RSP_PRESENT) {
1001 if (cmd->flags & MMC_RSP_136) {
1002 cmd->resp[3] = mci_readl(host, RESP0);
1003 cmd->resp[2] = mci_readl(host, RESP1);
1004 cmd->resp[1] = mci_readl(host, RESP2);
1005 cmd->resp[0] = mci_readl(host, RESP3);
1006 } else {
1007 cmd->resp[0] = mci_readl(host, RESP0);
1008 cmd->resp[1] = 0;
1009 cmd->resp[2] = 0;
1010 cmd->resp[3] = 0;
1011 }
1012 }
1013
1014 if (status & SDMMC_INT_RTO)
1015 cmd->error = -ETIMEDOUT;
1016 else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
1017 cmd->error = -EILSEQ;
1018 else if (status & SDMMC_INT_RESP_ERR)
1019 cmd->error = -EIO;
1020 else
1021 cmd->error = 0;
1022
1023 if (cmd->error) {
1024 /* newer ip versions need a delay between retries */
1025 if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
1026 mdelay(20);
1027
1028 if (cmd->data) {
Will Newtonf95f3852011-01-02 01:11:59 -05001029 dw_mci_stop_dma(host);
Seungwon Jeonfda5f732012-05-22 13:01:13 +09001030 host->data = NULL;
Will Newtonf95f3852011-01-02 01:11:59 -05001031 }
1032 }
1033}
1034
1035static void dw_mci_tasklet_func(unsigned long priv)
1036{
1037 struct dw_mci *host = (struct dw_mci *)priv;
1038 struct mmc_data *data;
1039 struct mmc_command *cmd;
1040 enum dw_mci_state state;
1041 enum dw_mci_state prev_state;
James Hogan94dd5b32011-06-29 09:30:47 +01001042 u32 status, ctrl;
Will Newtonf95f3852011-01-02 01:11:59 -05001043
1044 spin_lock(&host->lock);
1045
1046 state = host->state;
1047 data = host->data;
1048
1049 do {
1050 prev_state = state;
1051
1052 switch (state) {
1053 case STATE_IDLE:
1054 break;
1055
1056 case STATE_SENDING_CMD:
1057 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1058 &host->pending_events))
1059 break;
1060
1061 cmd = host->cmd;
1062 host->cmd = NULL;
1063 set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001064 dw_mci_command_complete(host, cmd);
1065 if (cmd == host->mrq->sbc && !cmd->error) {
1066 prev_state = state = STATE_SENDING_CMD;
1067 __dw_mci_start_request(host, host->cur_slot,
1068 host->mrq->cmd);
1069 goto unlock;
1070 }
1071
Will Newtonf95f3852011-01-02 01:11:59 -05001072 if (!host->mrq->data || cmd->error) {
1073 dw_mci_request_end(host, host->mrq);
1074 goto unlock;
1075 }
1076
1077 prev_state = state = STATE_SENDING_DATA;
1078 /* fall through */
1079
1080 case STATE_SENDING_DATA:
1081 if (test_and_clear_bit(EVENT_DATA_ERROR,
1082 &host->pending_events)) {
1083 dw_mci_stop_dma(host);
1084 if (data->stop)
1085 send_stop_cmd(host, data);
1086 state = STATE_DATA_ERROR;
1087 break;
1088 }
1089
1090 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
1091 &host->pending_events))
1092 break;
1093
1094 set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
1095 prev_state = state = STATE_DATA_BUSY;
1096 /* fall through */
1097
1098 case STATE_DATA_BUSY:
1099 if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
1100 &host->pending_events))
1101 break;
1102
1103 host->data = NULL;
1104 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
1105 status = host->data_status;
1106
1107 if (status & DW_MCI_DATA_ERROR_FLAGS) {
Jaehoon Chung3f7eec62013-05-27 13:47:57 +09001108 if (status & SDMMC_INT_DRTO) {
Will Newtonf95f3852011-01-02 01:11:59 -05001109 data->error = -ETIMEDOUT;
1110 } else if (status & SDMMC_INT_DCRC) {
Will Newtonf95f3852011-01-02 01:11:59 -05001111 data->error = -EILSEQ;
James Hogan55c5efbc2011-06-29 09:29:58 +01001112 } else if (status & SDMMC_INT_EBE &&
1113 host->dir_status ==
1114 DW_MCI_SEND_STATUS) {
1115 /*
1116 * No data CRC status was returned.
1117 * The number of bytes transferred will
1118 * be exaggerated in PIO mode.
1119 */
1120 data->bytes_xfered = 0;
1121 data->error = -ETIMEDOUT;
Will Newtonf95f3852011-01-02 01:11:59 -05001122 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00001123 dev_err(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -05001124 "data FIFO error "
1125 "(status=%08x)\n",
1126 status);
1127 data->error = -EIO;
1128 }
James Hogan94dd5b32011-06-29 09:30:47 +01001129 /*
1130 * After an error, there may be data lingering
1131 * in the FIFO, so reset it - doing so
1132 * generates a block interrupt, hence setting
1133 * the scatter-gather pointer to NULL.
1134 */
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001135 sg_miter_stop(&host->sg_miter);
James Hogan94dd5b32011-06-29 09:30:47 +01001136 host->sg = NULL;
1137 ctrl = mci_readl(host, CTRL);
1138 ctrl |= SDMMC_CTRL_FIFO_RESET;
1139 mci_writel(host, CTRL, ctrl);
Will Newtonf95f3852011-01-02 01:11:59 -05001140 } else {
1141 data->bytes_xfered = data->blocks * data->blksz;
1142 data->error = 0;
1143 }
1144
1145 if (!data->stop) {
1146 dw_mci_request_end(host, host->mrq);
1147 goto unlock;
1148 }
1149
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001150 if (host->mrq->sbc && !data->error) {
1151 data->stop->error = 0;
1152 dw_mci_request_end(host, host->mrq);
1153 goto unlock;
1154 }
1155
Will Newtonf95f3852011-01-02 01:11:59 -05001156 prev_state = state = STATE_SENDING_STOP;
1157 if (!data->error)
1158 send_stop_cmd(host, data);
1159 /* fall through */
1160
1161 case STATE_SENDING_STOP:
1162 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1163 &host->pending_events))
1164 break;
1165
1166 host->cmd = NULL;
1167 dw_mci_command_complete(host, host->mrq->stop);
1168 dw_mci_request_end(host, host->mrq);
1169 goto unlock;
1170
1171 case STATE_DATA_ERROR:
1172 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
1173 &host->pending_events))
1174 break;
1175
1176 state = STATE_DATA_BUSY;
1177 break;
1178 }
1179 } while (state != prev_state);
1180
1181 host->state = state;
1182unlock:
1183 spin_unlock(&host->lock);
1184
1185}
1186
James Hogan34b664a2011-06-24 13:57:56 +01001187/* push final bytes to part_buf, only use during push */
1188static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
1189{
1190 memcpy((void *)&host->part_buf, buf, cnt);
1191 host->part_buf_count = cnt;
1192}
1193
1194/* append bytes to part_buf, only use during push */
1195static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
1196{
1197 cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
1198 memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
1199 host->part_buf_count += cnt;
1200 return cnt;
1201}
1202
1203/* pull first bytes from part_buf, only use during pull */
1204static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
1205{
1206 cnt = min(cnt, (int)host->part_buf_count);
1207 if (cnt) {
1208 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
1209 cnt);
1210 host->part_buf_count -= cnt;
1211 host->part_buf_start += cnt;
1212 }
1213 return cnt;
1214}
1215
1216/* pull final bytes from the part_buf, assuming it's just been filled */
1217static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
1218{
1219 memcpy(buf, &host->part_buf, cnt);
1220 host->part_buf_start = cnt;
1221 host->part_buf_count = (1 << host->data_shift) - cnt;
1222}
1223
Will Newtonf95f3852011-01-02 01:11:59 -05001224static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
1225{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001226 struct mmc_data *data = host->data;
1227 int init_cnt = cnt;
1228
James Hogan34b664a2011-06-24 13:57:56 +01001229 /* try and push anything in the part_buf */
1230 if (unlikely(host->part_buf_count)) {
1231 int len = dw_mci_push_part_bytes(host, buf, cnt);
1232 buf += len;
1233 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001234 if (host->part_buf_count == 2) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001235 mci_writew(host, DATA(host->data_offset),
1236 host->part_buf16);
James Hogan34b664a2011-06-24 13:57:56 +01001237 host->part_buf_count = 0;
1238 }
1239 }
1240#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1241 if (unlikely((unsigned long)buf & 0x1)) {
1242 while (cnt >= 2) {
1243 u16 aligned_buf[64];
1244 int len = min(cnt & -2, (int)sizeof(aligned_buf));
1245 int items = len >> 1;
1246 int i;
1247 /* memcpy from input buffer into aligned buffer */
1248 memcpy(aligned_buf, buf, len);
1249 buf += len;
1250 cnt -= len;
1251 /* push data from aligned buffer into fifo */
1252 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001253 mci_writew(host, DATA(host->data_offset),
1254 aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01001255 }
1256 } else
1257#endif
1258 {
1259 u16 *pdata = buf;
1260 for (; cnt >= 2; cnt -= 2)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001261 mci_writew(host, DATA(host->data_offset), *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01001262 buf = pdata;
1263 }
1264 /* put anything remaining in the part_buf */
1265 if (cnt) {
1266 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001267 /* Push data if we have reached the expected data length */
1268 if ((data->bytes_xfered + init_cnt) ==
1269 (data->blksz * data->blocks))
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001270 mci_writew(host, DATA(host->data_offset),
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001271 host->part_buf16);
Will Newtonf95f3852011-01-02 01:11:59 -05001272 }
1273}
1274
1275static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
1276{
James Hogan34b664a2011-06-24 13:57:56 +01001277#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1278 if (unlikely((unsigned long)buf & 0x1)) {
1279 while (cnt >= 2) {
1280 /* pull data from fifo into aligned buffer */
1281 u16 aligned_buf[64];
1282 int len = min(cnt & -2, (int)sizeof(aligned_buf));
1283 int items = len >> 1;
1284 int i;
1285 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001286 aligned_buf[i] = mci_readw(host,
1287 DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001288 /* memcpy from aligned buffer into output buffer */
1289 memcpy(buf, aligned_buf, len);
1290 buf += len;
1291 cnt -= len;
1292 }
1293 } else
1294#endif
1295 {
1296 u16 *pdata = buf;
1297 for (; cnt >= 2; cnt -= 2)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001298 *pdata++ = mci_readw(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001299 buf = pdata;
1300 }
1301 if (cnt) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001302 host->part_buf16 = mci_readw(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001303 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05001304 }
1305}
1306
1307static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
1308{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001309 struct mmc_data *data = host->data;
1310 int init_cnt = cnt;
1311
James Hogan34b664a2011-06-24 13:57:56 +01001312 /* try and push anything in the part_buf */
1313 if (unlikely(host->part_buf_count)) {
1314 int len = dw_mci_push_part_bytes(host, buf, cnt);
1315 buf += len;
1316 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001317 if (host->part_buf_count == 4) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001318 mci_writel(host, DATA(host->data_offset),
1319 host->part_buf32);
James Hogan34b664a2011-06-24 13:57:56 +01001320 host->part_buf_count = 0;
1321 }
1322 }
1323#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1324 if (unlikely((unsigned long)buf & 0x3)) {
1325 while (cnt >= 4) {
1326 u32 aligned_buf[32];
1327 int len = min(cnt & -4, (int)sizeof(aligned_buf));
1328 int items = len >> 2;
1329 int i;
1330 /* memcpy from input buffer into aligned buffer */
1331 memcpy(aligned_buf, buf, len);
1332 buf += len;
1333 cnt -= len;
1334 /* push data from aligned buffer into fifo */
1335 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001336 mci_writel(host, DATA(host->data_offset),
1337 aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01001338 }
1339 } else
1340#endif
1341 {
1342 u32 *pdata = buf;
1343 for (; cnt >= 4; cnt -= 4)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001344 mci_writel(host, DATA(host->data_offset), *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01001345 buf = pdata;
1346 }
1347 /* put anything remaining in the part_buf */
1348 if (cnt) {
1349 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001350 /* Push data if we have reached the expected data length */
1351 if ((data->bytes_xfered + init_cnt) ==
1352 (data->blksz * data->blocks))
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001353 mci_writel(host, DATA(host->data_offset),
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001354 host->part_buf32);
Will Newtonf95f3852011-01-02 01:11:59 -05001355 }
1356}
1357
1358static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
1359{
James Hogan34b664a2011-06-24 13:57:56 +01001360#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1361 if (unlikely((unsigned long)buf & 0x3)) {
1362 while (cnt >= 4) {
1363 /* pull data from fifo into aligned buffer */
1364 u32 aligned_buf[32];
1365 int len = min(cnt & -4, (int)sizeof(aligned_buf));
1366 int items = len >> 2;
1367 int i;
1368 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001369 aligned_buf[i] = mci_readl(host,
1370 DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001371 /* memcpy from aligned buffer into output buffer */
1372 memcpy(buf, aligned_buf, len);
1373 buf += len;
1374 cnt -= len;
1375 }
1376 } else
1377#endif
1378 {
1379 u32 *pdata = buf;
1380 for (; cnt >= 4; cnt -= 4)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001381 *pdata++ = mci_readl(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001382 buf = pdata;
1383 }
1384 if (cnt) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001385 host->part_buf32 = mci_readl(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001386 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05001387 }
1388}
1389
1390static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
1391{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001392 struct mmc_data *data = host->data;
1393 int init_cnt = cnt;
1394
James Hogan34b664a2011-06-24 13:57:56 +01001395 /* try and push anything in the part_buf */
1396 if (unlikely(host->part_buf_count)) {
1397 int len = dw_mci_push_part_bytes(host, buf, cnt);
1398 buf += len;
1399 cnt -= len;
Seungwon Jeonc09fbd72013-03-25 16:28:22 +09001400
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001401 if (host->part_buf_count == 8) {
Seungwon Jeonc09fbd72013-03-25 16:28:22 +09001402 mci_writeq(host, DATA(host->data_offset),
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001403 host->part_buf);
James Hogan34b664a2011-06-24 13:57:56 +01001404 host->part_buf_count = 0;
1405 }
1406 }
1407#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1408 if (unlikely((unsigned long)buf & 0x7)) {
1409 while (cnt >= 8) {
1410 u64 aligned_buf[16];
1411 int len = min(cnt & -8, (int)sizeof(aligned_buf));
1412 int items = len >> 3;
1413 int i;
1414 /* memcpy from input buffer into aligned buffer */
1415 memcpy(aligned_buf, buf, len);
1416 buf += len;
1417 cnt -= len;
1418 /* push data from aligned buffer into fifo */
1419 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001420 mci_writeq(host, DATA(host->data_offset),
1421 aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01001422 }
1423 } else
1424#endif
1425 {
1426 u64 *pdata = buf;
1427 for (; cnt >= 8; cnt -= 8)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001428 mci_writeq(host, DATA(host->data_offset), *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01001429 buf = pdata;
1430 }
1431 /* put anything remaining in the part_buf */
1432 if (cnt) {
1433 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001434 /* Push data if we have reached the expected data length */
1435 if ((data->bytes_xfered + init_cnt) ==
1436 (data->blksz * data->blocks))
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001437 mci_writeq(host, DATA(host->data_offset),
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001438 host->part_buf);
Will Newtonf95f3852011-01-02 01:11:59 -05001439 }
1440}
1441
1442static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
1443{
James Hogan34b664a2011-06-24 13:57:56 +01001444#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1445 if (unlikely((unsigned long)buf & 0x7)) {
1446 while (cnt >= 8) {
1447 /* pull data from fifo into aligned buffer */
1448 u64 aligned_buf[16];
1449 int len = min(cnt & -8, (int)sizeof(aligned_buf));
1450 int items = len >> 3;
1451 int i;
1452 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001453 aligned_buf[i] = mci_readq(host,
1454 DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001455 /* memcpy from aligned buffer into output buffer */
1456 memcpy(buf, aligned_buf, len);
1457 buf += len;
1458 cnt -= len;
1459 }
1460 } else
1461#endif
1462 {
1463 u64 *pdata = buf;
1464 for (; cnt >= 8; cnt -= 8)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001465 *pdata++ = mci_readq(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001466 buf = pdata;
Will Newtonf95f3852011-01-02 01:11:59 -05001467 }
James Hogan34b664a2011-06-24 13:57:56 +01001468 if (cnt) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001469 host->part_buf = mci_readq(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001470 dw_mci_pull_final_bytes(host, buf, cnt);
1471 }
1472}
1473
1474static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
1475{
1476 int len;
1477
1478 /* get remaining partial bytes */
1479 len = dw_mci_pull_part_bytes(host, buf, cnt);
1480 if (unlikely(len == cnt))
1481 return;
1482 buf += len;
1483 cnt -= len;
1484
1485 /* get the rest of the data */
1486 host->pull_data(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05001487}
1488
Kyoungil Kim87a74d32013-01-22 16:46:30 +09001489static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
Will Newtonf95f3852011-01-02 01:11:59 -05001490{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001491 struct sg_mapping_iter *sg_miter = &host->sg_miter;
1492 void *buf;
1493 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05001494 struct mmc_data *data = host->data;
1495 int shift = host->data_shift;
1496 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04001497 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001498 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05001499
1500 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001501 if (!sg_miter_next(sg_miter))
1502 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05001503
Imre Deak4225fc82013-02-27 17:02:57 -08001504 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001505 buf = sg_miter->addr;
1506 remain = sg_miter->length;
1507 offset = 0;
1508
1509 do {
1510 fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
1511 << shift) + host->part_buf_count;
1512 len = min(remain, fcnt);
1513 if (!len)
1514 break;
1515 dw_mci_pull_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04001516 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05001517 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001518 remain -= len;
1519 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05001520
Seungwon Jeone74f3a92012-08-01 09:30:46 +09001521 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05001522 status = mci_readl(host, MINTSTS);
1523 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
Kyoungil Kim87a74d32013-01-22 16:46:30 +09001524 /* if the RXDR is ready read again */
1525 } while ((status & SDMMC_INT_RXDR) ||
1526 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001527
1528 if (!remain) {
1529 if (!sg_miter_next(sg_miter))
1530 goto done;
1531 sg_miter->consumed = 0;
1532 }
1533 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05001534 return;
1535
1536done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001537 sg_miter_stop(sg_miter);
1538 host->sg = NULL;
Will Newtonf95f3852011-01-02 01:11:59 -05001539 smp_wmb();
1540 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
1541}
1542
1543static void dw_mci_write_data_pio(struct dw_mci *host)
1544{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001545 struct sg_mapping_iter *sg_miter = &host->sg_miter;
1546 void *buf;
1547 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05001548 struct mmc_data *data = host->data;
1549 int shift = host->data_shift;
1550 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04001551 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001552 unsigned int fifo_depth = host->fifo_depth;
1553 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05001554
1555 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001556 if (!sg_miter_next(sg_miter))
1557 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05001558
Imre Deak4225fc82013-02-27 17:02:57 -08001559 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001560 buf = sg_miter->addr;
1561 remain = sg_miter->length;
1562 offset = 0;
1563
1564 do {
1565 fcnt = ((fifo_depth -
1566 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
1567 << shift) - host->part_buf_count;
1568 len = min(remain, fcnt);
1569 if (!len)
1570 break;
1571 host->push_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04001572 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05001573 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001574 remain -= len;
1575 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05001576
Seungwon Jeone74f3a92012-08-01 09:30:46 +09001577 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05001578 status = mci_readl(host, MINTSTS);
1579 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
Will Newtonf95f3852011-01-02 01:11:59 -05001580 } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001581
1582 if (!remain) {
1583 if (!sg_miter_next(sg_miter))
1584 goto done;
1585 sg_miter->consumed = 0;
1586 }
1587 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05001588 return;
1589
1590done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001591 sg_miter_stop(sg_miter);
1592 host->sg = NULL;
Will Newtonf95f3852011-01-02 01:11:59 -05001593 smp_wmb();
1594 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
1595}
1596
1597static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
1598{
1599 if (!host->cmd_status)
1600 host->cmd_status = status;
1601
1602 smp_wmb();
1603
1604 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
1605 tasklet_schedule(&host->tasklet);
1606}
1607
1608static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
1609{
1610 struct dw_mci *host = dev_id;
Seungwon Jeon182c9082012-08-01 09:30:30 +09001611 u32 pending;
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301612 int i;
Will Newtonf95f3852011-01-02 01:11:59 -05001613
Markos Chandras1fb5f682013-03-12 10:53:11 +00001614 pending = mci_readl(host, MINTSTS); /* read-only mask reg */
1615
Doug Anderson476d79f2013-07-09 13:04:40 -07001616 /*
1617 * DTO fix - version 2.10a and below, and only if internal DMA
1618 * is configured.
1619 */
1620 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
1621 if (!pending &&
1622 ((mci_readl(host, STATUS) >> 17) & 0x1fff))
1623 pending |= SDMMC_INT_DATA_OVER;
1624 }
1625
Markos Chandras1fb5f682013-03-12 10:53:11 +00001626 if (pending) {
Will Newtonf95f3852011-01-02 01:11:59 -05001627 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
1628 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09001629 host->cmd_status = pending;
Will Newtonf95f3852011-01-02 01:11:59 -05001630 smp_wmb();
1631 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -05001632 }
1633
1634 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
1635 /* if there is an error report DATA_ERROR */
1636 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09001637 host->data_status = pending;
Will Newtonf95f3852011-01-02 01:11:59 -05001638 smp_wmb();
1639 set_bit(EVENT_DATA_ERROR, &host->pending_events);
Seungwon Jeon9b2026a2012-08-01 09:30:40 +09001640 tasklet_schedule(&host->tasklet);
Will Newtonf95f3852011-01-02 01:11:59 -05001641 }
1642
1643 if (pending & SDMMC_INT_DATA_OVER) {
1644 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
1645 if (!host->data_status)
Seungwon Jeon182c9082012-08-01 09:30:30 +09001646 host->data_status = pending;
Will Newtonf95f3852011-01-02 01:11:59 -05001647 smp_wmb();
1648 if (host->dir_status == DW_MCI_RECV_STATUS) {
1649 if (host->sg != NULL)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09001650 dw_mci_read_data_pio(host, true);
Will Newtonf95f3852011-01-02 01:11:59 -05001651 }
1652 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
1653 tasklet_schedule(&host->tasklet);
1654 }
1655
1656 if (pending & SDMMC_INT_RXDR) {
1657 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01001658 if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09001659 dw_mci_read_data_pio(host, false);
Will Newtonf95f3852011-01-02 01:11:59 -05001660 }
1661
1662 if (pending & SDMMC_INT_TXDR) {
1663 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01001664 if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
Will Newtonf95f3852011-01-02 01:11:59 -05001665 dw_mci_write_data_pio(host);
1666 }
1667
1668 if (pending & SDMMC_INT_CMD_DONE) {
1669 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
Seungwon Jeon182c9082012-08-01 09:30:30 +09001670 dw_mci_cmd_interrupt(host, pending);
Will Newtonf95f3852011-01-02 01:11:59 -05001671 }
1672
1673 if (pending & SDMMC_INT_CD) {
1674 mci_writel(host, RINTSTS, SDMMC_INT_CD);
Thomas Abraham95dcc2c2012-05-01 14:57:36 -07001675 queue_work(host->card_workqueue, &host->card_work);
Will Newtonf95f3852011-01-02 01:11:59 -05001676 }
1677
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301678 /* Handle SDIO Interrupts */
1679 for (i = 0; i < host->num_slots; i++) {
1680 struct dw_mci_slot *slot = host->slot[i];
1681 if (pending & SDMMC_INT_SDIO(i)) {
1682 mci_writel(host, RINTSTS, SDMMC_INT_SDIO(i));
1683 mmc_signal_sdio_irq(slot->mmc);
1684 }
1685 }
1686
Markos Chandras1fb5f682013-03-12 10:53:11 +00001687 }
Will Newtonf95f3852011-01-02 01:11:59 -05001688
1689#ifdef CONFIG_MMC_DW_IDMAC
1690 /* Handle DMA interrupts */
1691 pending = mci_readl(host, IDSTS);
1692 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
1693 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI);
1694 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
Will Newtonf95f3852011-01-02 01:11:59 -05001695 host->dma_ops->complete(host);
1696 }
1697#endif
1698
1699 return IRQ_HANDLED;
1700}
1701
James Hogan1791b13e2011-06-24 13:55:55 +01001702static void dw_mci_work_routine_card(struct work_struct *work)
Will Newtonf95f3852011-01-02 01:11:59 -05001703{
James Hogan1791b13e2011-06-24 13:55:55 +01001704 struct dw_mci *host = container_of(work, struct dw_mci, card_work);
Will Newtonf95f3852011-01-02 01:11:59 -05001705 int i;
1706
1707 for (i = 0; i < host->num_slots; i++) {
1708 struct dw_mci_slot *slot = host->slot[i];
1709 struct mmc_host *mmc = slot->mmc;
1710 struct mmc_request *mrq;
1711 int present;
1712 u32 ctrl;
1713
1714 present = dw_mci_get_cd(mmc);
1715 while (present != slot->last_detect_state) {
Will Newtonf95f3852011-01-02 01:11:59 -05001716 dev_dbg(&slot->mmc->class_dev, "card %s\n",
1717 present ? "inserted" : "removed");
1718
James Hogan1791b13e2011-06-24 13:55:55 +01001719 spin_lock_bh(&host->lock);
1720
Will Newtonf95f3852011-01-02 01:11:59 -05001721 /* Card change detected */
1722 slot->last_detect_state = present;
1723
James Hogan1791b13e2011-06-24 13:55:55 +01001724 /* Mark card as present if applicable */
1725 if (present != 0)
Will Newtonf95f3852011-01-02 01:11:59 -05001726 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
Will Newtonf95f3852011-01-02 01:11:59 -05001727
1728 /* Clean up queue if present */
1729 mrq = slot->mrq;
1730 if (mrq) {
1731 if (mrq == host->mrq) {
1732 host->data = NULL;
1733 host->cmd = NULL;
1734
1735 switch (host->state) {
1736 case STATE_IDLE:
1737 break;
1738 case STATE_SENDING_CMD:
1739 mrq->cmd->error = -ENOMEDIUM;
1740 if (!mrq->data)
1741 break;
1742 /* fall through */
1743 case STATE_SENDING_DATA:
1744 mrq->data->error = -ENOMEDIUM;
1745 dw_mci_stop_dma(host);
1746 break;
1747 case STATE_DATA_BUSY:
1748 case STATE_DATA_ERROR:
1749 if (mrq->data->error == -EINPROGRESS)
1750 mrq->data->error = -ENOMEDIUM;
1751 if (!mrq->stop)
1752 break;
1753 /* fall through */
1754 case STATE_SENDING_STOP:
1755 mrq->stop->error = -ENOMEDIUM;
1756 break;
1757 }
1758
1759 dw_mci_request_end(host, mrq);
1760 } else {
1761 list_del(&slot->queue_node);
1762 mrq->cmd->error = -ENOMEDIUM;
1763 if (mrq->data)
1764 mrq->data->error = -ENOMEDIUM;
1765 if (mrq->stop)
1766 mrq->stop->error = -ENOMEDIUM;
1767
1768 spin_unlock(&host->lock);
1769 mmc_request_done(slot->mmc, mrq);
1770 spin_lock(&host->lock);
1771 }
1772 }
1773
1774 /* Power down slot */
1775 if (present == 0) {
Will Newtonf95f3852011-01-02 01:11:59 -05001776 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1777
1778 /*
1779 * Clear down the FIFO - doing so generates a
1780 * block interrupt, hence setting the
1781 * scatter-gather pointer to NULL.
1782 */
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001783 sg_miter_stop(&host->sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05001784 host->sg = NULL;
1785
1786 ctrl = mci_readl(host, CTRL);
1787 ctrl |= SDMMC_CTRL_FIFO_RESET;
1788 mci_writel(host, CTRL, ctrl);
1789
1790#ifdef CONFIG_MMC_DW_IDMAC
1791 ctrl = mci_readl(host, BMOD);
Seungwon Jeon141a7122012-05-22 13:01:03 +09001792 /* Software reset of DMA */
1793 ctrl |= SDMMC_IDMAC_SWRESET;
Will Newtonf95f3852011-01-02 01:11:59 -05001794 mci_writel(host, BMOD, ctrl);
1795#endif
1796
1797 }
1798
James Hogan1791b13e2011-06-24 13:55:55 +01001799 spin_unlock_bh(&host->lock);
1800
Will Newtonf95f3852011-01-02 01:11:59 -05001801 present = dw_mci_get_cd(mmc);
1802 }
1803
1804 mmc_detect_change(slot->mmc,
1805 msecs_to_jiffies(host->pdata->detect_delay_ms));
1806 }
1807}
1808
Thomas Abrahamc91eab42012-09-17 18:16:40 +00001809#ifdef CONFIG_OF
1810/* given a slot id, find out the device node representing that slot */
1811static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
1812{
1813 struct device_node *np;
1814 const __be32 *addr;
1815 int len;
1816
1817 if (!dev || !dev->of_node)
1818 return NULL;
1819
1820 for_each_child_of_node(dev->of_node, np) {
1821 addr = of_get_property(np, "reg", &len);
1822 if (!addr || (len < sizeof(int)))
1823 continue;
1824 if (be32_to_cpup(addr) == slot)
1825 return np;
1826 }
1827 return NULL;
1828}
1829
Doug Andersona70aaa62013-01-11 17:03:50 +00001830static struct dw_mci_of_slot_quirks {
1831 char *quirk;
1832 int id;
1833} of_slot_quirks[] = {
1834 {
1835 .quirk = "disable-wp",
1836 .id = DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT,
1837 },
1838};
1839
1840static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
1841{
1842 struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
1843 int quirks = 0;
1844 int idx;
1845
1846 /* get quirks */
1847 for (idx = 0; idx < ARRAY_SIZE(of_slot_quirks); idx++)
1848 if (of_get_property(np, of_slot_quirks[idx].quirk, NULL))
1849 quirks |= of_slot_quirks[idx].id;
1850
1851 return quirks;
1852}
1853
Thomas Abrahamc91eab42012-09-17 18:16:40 +00001854/* find out bus-width for a given slot */
1855static u32 dw_mci_of_get_bus_wd(struct device *dev, u8 slot)
1856{
1857 struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
1858 u32 bus_wd = 1;
1859
1860 if (!np)
1861 return 1;
1862
1863 if (of_property_read_u32(np, "bus-width", &bus_wd))
1864 dev_err(dev, "bus-width property not found, assuming width"
1865 " as 1\n");
1866 return bus_wd;
1867}
Doug Anderson55a6ceb2013-01-11 17:03:53 +00001868
1869/* find the write protect gpio for a given slot; or -1 if none specified */
1870static int dw_mci_of_get_wp_gpio(struct device *dev, u8 slot)
1871{
1872 struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
1873 int gpio;
1874
1875 if (!np)
1876 return -EINVAL;
1877
1878 gpio = of_get_named_gpio(np, "wp-gpios", 0);
1879
1880 /* Having a missing entry is valid; return silently */
1881 if (!gpio_is_valid(gpio))
1882 return -EINVAL;
1883
1884 if (devm_gpio_request(dev, gpio, "dw-mci-wp")) {
1885 dev_warn(dev, "gpio [%d] request failed\n", gpio);
1886 return -EINVAL;
1887 }
1888
1889 return gpio;
1890}
Thomas Abrahamc91eab42012-09-17 18:16:40 +00001891#else /* CONFIG_OF */
Doug Andersona70aaa62013-01-11 17:03:50 +00001892static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
1893{
1894 return 0;
1895}
Thomas Abrahamc91eab42012-09-17 18:16:40 +00001896static u32 dw_mci_of_get_bus_wd(struct device *dev, u8 slot)
1897{
1898 return 1;
1899}
1900static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
1901{
1902 return NULL;
1903}
Doug Anderson55a6ceb2013-01-11 17:03:53 +00001904static int dw_mci_of_get_wp_gpio(struct device *dev, u8 slot)
1905{
1906 return -EINVAL;
1907}
Thomas Abrahamc91eab42012-09-17 18:16:40 +00001908#endif /* CONFIG_OF */
1909
Jaehoon Chung36c179a2012-08-23 20:31:48 +09001910static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
Will Newtonf95f3852011-01-02 01:11:59 -05001911{
1912 struct mmc_host *mmc;
1913 struct dw_mci_slot *slot;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00001914 const struct dw_mci_drv_data *drv_data = host->drv_data;
Thomas Abraham800d78b2012-09-17 18:16:42 +00001915 int ctrl_id, ret;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00001916 u8 bus_width;
Will Newtonf95f3852011-01-02 01:11:59 -05001917
Thomas Abraham4a909202012-09-17 18:16:35 +00001918 mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
Will Newtonf95f3852011-01-02 01:11:59 -05001919 if (!mmc)
1920 return -ENOMEM;
1921
1922 slot = mmc_priv(mmc);
1923 slot->id = id;
1924 slot->mmc = mmc;
1925 slot->host = host;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00001926 host->slot[id] = slot;
Will Newtonf95f3852011-01-02 01:11:59 -05001927
Doug Andersona70aaa62013-01-11 17:03:50 +00001928 slot->quirks = dw_mci_of_get_slot_quirks(host->dev, slot->id);
1929
Will Newtonf95f3852011-01-02 01:11:59 -05001930 mmc->ops = &dw_mci_ops;
1931 mmc->f_min = DIV_ROUND_UP(host->bus_hz, 510);
1932 mmc->f_max = host->bus_hz;
1933
1934 if (host->pdata->get_ocr)
1935 mmc->ocr_avail = host->pdata->get_ocr(id);
1936 else
1937 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1938
1939 /*
1940 * Start with slot power disabled, it will be enabled when a card
1941 * is detected.
1942 */
1943 if (host->pdata->setpower)
1944 host->pdata->setpower(id, 0);
1945
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09001946 if (host->pdata->caps)
1947 mmc->caps = host->pdata->caps;
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09001948
Abhilash Kesavanab269122012-11-19 10:26:21 +05301949 if (host->pdata->pm_caps)
1950 mmc->pm_caps = host->pdata->pm_caps;
1951
Thomas Abraham800d78b2012-09-17 18:16:42 +00001952 if (host->dev->of_node) {
1953 ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
1954 if (ctrl_id < 0)
1955 ctrl_id = 0;
1956 } else {
1957 ctrl_id = to_platform_device(host->dev)->id;
1958 }
James Hogancb27a842012-10-16 09:43:08 +01001959 if (drv_data && drv_data->caps)
1960 mmc->caps |= drv_data->caps[ctrl_id];
Thomas Abraham800d78b2012-09-17 18:16:42 +00001961
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09001962 if (host->pdata->caps2)
1963 mmc->caps2 = host->pdata->caps2;
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09001964
Will Newtonf95f3852011-01-02 01:11:59 -05001965 if (host->pdata->get_bus_wd)
Thomas Abrahamc91eab42012-09-17 18:16:40 +00001966 bus_width = host->pdata->get_bus_wd(slot->id);
1967 else if (host->dev->of_node)
1968 bus_width = dw_mci_of_get_bus_wd(host->dev, slot->id);
1969 else
1970 bus_width = 1;
1971
1972 switch (bus_width) {
1973 case 8:
1974 mmc->caps |= MMC_CAP_8_BIT_DATA;
1975 case 4:
1976 mmc->caps |= MMC_CAP_4_BIT_DATA;
1977 }
Will Newtonf95f3852011-01-02 01:11:59 -05001978
1979 if (host->pdata->quirks & DW_MCI_QUIRK_HIGHSPEED)
Seungwon Jeon6daa7772011-08-05 12:35:03 +09001980 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Will Newtonf95f3852011-01-02 01:11:59 -05001981
Will Newtonf95f3852011-01-02 01:11:59 -05001982 if (host->pdata->blk_settings) {
1983 mmc->max_segs = host->pdata->blk_settings->max_segs;
1984 mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
1985 mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
1986 mmc->max_req_size = host->pdata->blk_settings->max_req_size;
1987 mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
1988 } else {
1989 /* Useful defaults if platform data is unset. */
Jaehoon Chunga39e5742012-02-04 17:00:27 -05001990#ifdef CONFIG_MMC_DW_IDMAC
1991 mmc->max_segs = host->ring_size;
1992 mmc->max_blk_size = 65536;
1993 mmc->max_blk_count = host->ring_size;
1994 mmc->max_seg_size = 0x1000;
1995 mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count;
1996#else
Will Newtonf95f3852011-01-02 01:11:59 -05001997 mmc->max_segs = 64;
1998 mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
1999 mmc->max_blk_count = 512;
2000 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2001 mmc->max_seg_size = mmc->max_req_size;
Will Newtonf95f3852011-01-02 01:11:59 -05002002#endif /* CONFIG_MMC_DW_IDMAC */
Jaehoon Chunga39e5742012-02-04 17:00:27 -05002003 }
Will Newtonf95f3852011-01-02 01:11:59 -05002004
2005 if (dw_mci_get_cd(mmc))
2006 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
2007 else
2008 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
2009
Doug Anderson55a6ceb2013-01-11 17:03:53 +00002010 slot->wp_gpio = dw_mci_of_get_wp_gpio(host->dev, slot->id);
2011
Jaehoon Chung0cea5292013-02-15 23:45:45 +09002012 ret = mmc_add_host(mmc);
2013 if (ret)
2014 goto err_setup_bus;
Will Newtonf95f3852011-01-02 01:11:59 -05002015
2016#if defined(CONFIG_DEBUG_FS)
2017 dw_mci_init_debugfs(slot);
2018#endif
2019
2020 /* Card initially undetected */
2021 slot->last_detect_state = 0;
2022
Will Newtonf95f3852011-01-02 01:11:59 -05002023 return 0;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002024
2025err_setup_bus:
2026 mmc_free_host(mmc);
2027 return -EINVAL;
Will Newtonf95f3852011-01-02 01:11:59 -05002028}
2029
2030static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
2031{
2032 /* Shutdown detect IRQ */
2033 if (slot->host->pdata->exit)
2034 slot->host->pdata->exit(id);
2035
2036 /* Debugfs stuff is cleaned up by mmc core */
2037 mmc_remove_host(slot->mmc);
2038 slot->host->slot[id] = NULL;
2039 mmc_free_host(slot->mmc);
2040}
2041
2042static void dw_mci_init_dma(struct dw_mci *host)
2043{
2044 /* Alloc memory for sg translation */
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002045 host->sg_cpu = dmam_alloc_coherent(host->dev, PAGE_SIZE,
Will Newtonf95f3852011-01-02 01:11:59 -05002046 &host->sg_dma, GFP_KERNEL);
2047 if (!host->sg_cpu) {
Thomas Abraham4a909202012-09-17 18:16:35 +00002048 dev_err(host->dev, "%s: could not alloc DMA memory\n",
Will Newtonf95f3852011-01-02 01:11:59 -05002049 __func__);
2050 goto no_dma;
2051 }
2052
2053 /* Determine which DMA interface to use */
2054#ifdef CONFIG_MMC_DW_IDMAC
2055 host->dma_ops = &dw_mci_idmac_ops;
Seungwon Jeon00956ea2012-09-28 19:13:11 +09002056 dev_info(host->dev, "Using internal DMA controller.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002057#endif
2058
2059 if (!host->dma_ops)
2060 goto no_dma;
2061
Jaehoon Chunge1631f92012-04-18 15:42:31 +09002062 if (host->dma_ops->init && host->dma_ops->start &&
2063 host->dma_ops->stop && host->dma_ops->cleanup) {
Will Newtonf95f3852011-01-02 01:11:59 -05002064 if (host->dma_ops->init(host)) {
Thomas Abraham4a909202012-09-17 18:16:35 +00002065 dev_err(host->dev, "%s: Unable to initialize "
Will Newtonf95f3852011-01-02 01:11:59 -05002066 "DMA Controller.\n", __func__);
2067 goto no_dma;
2068 }
2069 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00002070 dev_err(host->dev, "DMA initialization not found.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002071 goto no_dma;
2072 }
2073
2074 host->use_dma = 1;
2075 return;
2076
2077no_dma:
Thomas Abraham4a909202012-09-17 18:16:35 +00002078 dev_info(host->dev, "Using PIO mode.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002079 host->use_dma = 0;
2080 return;
2081}
2082
2083static bool mci_wait_reset(struct device *dev, struct dw_mci *host)
2084{
2085 unsigned long timeout = jiffies + msecs_to_jiffies(500);
2086 unsigned int ctrl;
2087
2088 mci_writel(host, CTRL, (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
2089 SDMMC_CTRL_DMA_RESET));
2090
2091 /* wait till resets clear */
2092 do {
2093 ctrl = mci_readl(host, CTRL);
2094 if (!(ctrl & (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
2095 SDMMC_CTRL_DMA_RESET)))
2096 return true;
2097 } while (time_before(jiffies, timeout));
2098
2099 dev_err(dev, "Timeout resetting block (ctrl %#x)\n", ctrl);
2100
2101 return false;
2102}
2103
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002104#ifdef CONFIG_OF
2105static struct dw_mci_of_quirks {
2106 char *quirk;
2107 int id;
2108} of_quirks[] = {
2109 {
2110 .quirk = "supports-highspeed",
2111 .id = DW_MCI_QUIRK_HIGHSPEED,
2112 }, {
2113 .quirk = "broken-cd",
2114 .id = DW_MCI_QUIRK_BROKEN_CARD_DETECTION,
2115 },
2116};
2117
2118static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2119{
2120 struct dw_mci_board *pdata;
2121 struct device *dev = host->dev;
2122 struct device_node *np = dev->of_node;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002123 const struct dw_mci_drv_data *drv_data = host->drv_data;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002124 int idx, ret;
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002125 u32 clock_frequency;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002126
2127 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2128 if (!pdata) {
2129 dev_err(dev, "could not allocate memory for pdata\n");
2130 return ERR_PTR(-ENOMEM);
2131 }
2132
2133 /* find out number of slots supported */
2134 if (of_property_read_u32(dev->of_node, "num-slots",
2135 &pdata->num_slots)) {
2136 dev_info(dev, "num-slots property not found, "
2137 "assuming 1 slot is available\n");
2138 pdata->num_slots = 1;
2139 }
2140
2141 /* get quirks */
2142 for (idx = 0; idx < ARRAY_SIZE(of_quirks); idx++)
2143 if (of_get_property(np, of_quirks[idx].quirk, NULL))
2144 pdata->quirks |= of_quirks[idx].id;
2145
2146 if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
2147 dev_info(dev, "fifo-depth property not found, using "
2148 "value of FIFOTH register as default\n");
2149
2150 of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
2151
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002152 if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
2153 pdata->bus_hz = clock_frequency;
2154
James Hogancb27a842012-10-16 09:43:08 +01002155 if (drv_data && drv_data->parse_dt) {
2156 ret = drv_data->parse_dt(host);
Thomas Abraham800d78b2012-09-17 18:16:42 +00002157 if (ret)
2158 return ERR_PTR(ret);
2159 }
2160
Abhilash Kesavanab269122012-11-19 10:26:21 +05302161 if (of_find_property(np, "keep-power-in-suspend", NULL))
2162 pdata->pm_caps |= MMC_PM_KEEP_POWER;
2163
2164 if (of_find_property(np, "enable-sdio-wakeup", NULL))
2165 pdata->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
2166
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002167 return pdata;
2168}
2169
2170#else /* CONFIG_OF */
2171static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2172{
2173 return ERR_PTR(-EINVAL);
2174}
2175#endif /* CONFIG_OF */
2176
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302177int dw_mci_probe(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002178{
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002179 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302180 int width, i, ret = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002181 u32 fifo_size;
Thomas Abraham1c2215b2012-09-17 18:16:37 +00002182 int init_slots = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002183
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002184 if (!host->pdata) {
2185 host->pdata = dw_mci_parse_dt(host);
2186 if (IS_ERR(host->pdata)) {
2187 dev_err(host->dev, "platform data not available\n");
2188 return -EINVAL;
2189 }
Will Newtonf95f3852011-01-02 01:11:59 -05002190 }
2191
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302192 if (!host->pdata->select_slot && host->pdata->num_slots > 1) {
Thomas Abraham4a909202012-09-17 18:16:35 +00002193 dev_err(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -05002194 "Platform data must supply select_slot function\n");
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302195 return -ENODEV;
Will Newtonf95f3852011-01-02 01:11:59 -05002196 }
2197
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002198 host->biu_clk = devm_clk_get(host->dev, "biu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002199 if (IS_ERR(host->biu_clk)) {
2200 dev_dbg(host->dev, "biu clock not available\n");
2201 } else {
2202 ret = clk_prepare_enable(host->biu_clk);
2203 if (ret) {
2204 dev_err(host->dev, "failed to enable biu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002205 return ret;
2206 }
Will Newtonf95f3852011-01-02 01:11:59 -05002207 }
2208
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002209 host->ciu_clk = devm_clk_get(host->dev, "ciu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002210 if (IS_ERR(host->ciu_clk)) {
2211 dev_dbg(host->dev, "ciu clock not available\n");
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002212 host->bus_hz = host->pdata->bus_hz;
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002213 } else {
2214 ret = clk_prepare_enable(host->ciu_clk);
2215 if (ret) {
2216 dev_err(host->dev, "failed to enable ciu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002217 goto err_clk_biu;
2218 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002219
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002220 if (host->pdata->bus_hz) {
2221 ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
2222 if (ret)
2223 dev_warn(host->dev,
2224 "Unable to set bus rate to %ul\n",
2225 host->pdata->bus_hz);
2226 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002227 host->bus_hz = clk_get_rate(host->ciu_clk);
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002228 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002229
James Hogancb27a842012-10-16 09:43:08 +01002230 if (drv_data && drv_data->setup_clock) {
2231 ret = drv_data->setup_clock(host);
Thomas Abraham800d78b2012-09-17 18:16:42 +00002232 if (ret) {
2233 dev_err(host->dev,
2234 "implementation specific clock setup failed\n");
2235 goto err_clk_ciu;
2236 }
2237 }
2238
Mark Browna55d6ff2013-07-29 21:55:27 +01002239 host->vmmc = devm_regulator_get_optional(host->dev, "vmmc");
Doug Anderson870556a2013-06-07 10:28:29 -07002240 if (IS_ERR(host->vmmc)) {
2241 ret = PTR_ERR(host->vmmc);
2242 if (ret == -EPROBE_DEFER)
2243 goto err_clk_ciu;
2244
2245 dev_info(host->dev, "no vmmc regulator found: %d\n", ret);
2246 host->vmmc = NULL;
2247 } else {
2248 ret = regulator_enable(host->vmmc);
2249 if (ret) {
2250 if (ret != -EPROBE_DEFER)
2251 dev_err(host->dev,
2252 "regulator_enable fail: %d\n", ret);
2253 goto err_clk_ciu;
2254 }
2255 }
2256
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002257 if (!host->bus_hz) {
2258 dev_err(host->dev,
2259 "Platform data must supply bus speed\n");
2260 ret = -ENODEV;
Doug Anderson870556a2013-06-07 10:28:29 -07002261 goto err_regulator;
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002262 }
2263
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302264 host->quirks = host->pdata->quirks;
Will Newtonf95f3852011-01-02 01:11:59 -05002265
2266 spin_lock_init(&host->lock);
2267 INIT_LIST_HEAD(&host->queue);
2268
Will Newtonf95f3852011-01-02 01:11:59 -05002269 /*
2270 * Get the host data width - this assumes that HCON has been set with
2271 * the correct values.
2272 */
2273 i = (mci_readl(host, HCON) >> 7) & 0x7;
2274 if (!i) {
2275 host->push_data = dw_mci_push_data16;
2276 host->pull_data = dw_mci_pull_data16;
2277 width = 16;
2278 host->data_shift = 1;
2279 } else if (i == 2) {
2280 host->push_data = dw_mci_push_data64;
2281 host->pull_data = dw_mci_pull_data64;
2282 width = 64;
2283 host->data_shift = 3;
2284 } else {
2285 /* Check for a reserved value, and warn if it is */
2286 WARN((i != 1),
2287 "HCON reports a reserved host data width!\n"
2288 "Defaulting to 32-bit access.\n");
2289 host->push_data = dw_mci_push_data32;
2290 host->pull_data = dw_mci_pull_data32;
2291 width = 32;
2292 host->data_shift = 2;
2293 }
2294
2295 /* Reset all blocks */
Thomas Abraham4a909202012-09-17 18:16:35 +00002296 if (!mci_wait_reset(host->dev, host))
Seungwon Jeon141a7122012-05-22 13:01:03 +09002297 return -ENODEV;
2298
2299 host->dma_ops = host->pdata->dma_ops;
2300 dw_mci_init_dma(host);
Will Newtonf95f3852011-01-02 01:11:59 -05002301
2302 /* Clear the interrupts for the host controller */
2303 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2304 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
2305
2306 /* Put in max timeout */
2307 mci_writel(host, TMOUT, 0xFFFFFFFF);
2308
2309 /*
2310 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
2311 * Tx Mark = fifo_size / 2 DMA Size = 8
2312 */
James Hoganb86d8252011-06-24 13:57:18 +01002313 if (!host->pdata->fifo_depth) {
2314 /*
2315 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
2316 * have been overwritten by the bootloader, just like we're
2317 * about to do, so if you know the value for your hardware, you
2318 * should put it in the platform data.
2319 */
2320 fifo_size = mci_readl(host, FIFOTH);
Jaehoon Chung8234e862012-01-11 09:28:21 +00002321 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
James Hoganb86d8252011-06-24 13:57:18 +01002322 } else {
2323 fifo_size = host->pdata->fifo_depth;
2324 }
2325 host->fifo_depth = fifo_size;
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002326 host->fifoth_val = ((0x2 << 28) | ((fifo_size/2 - 1) << 16) |
2327 ((fifo_size/2) << 0));
2328 mci_writel(host, FIFOTH, host->fifoth_val);
Will Newtonf95f3852011-01-02 01:11:59 -05002329
2330 /* disable clock to CIU */
2331 mci_writel(host, CLKENA, 0);
2332 mci_writel(host, CLKSRC, 0);
2333
James Hogan63008762013-03-12 10:43:54 +00002334 /*
2335 * In 2.40a spec, Data offset is changed.
2336 * Need to check the version-id and set data-offset for DATA register.
2337 */
2338 host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
2339 dev_info(host->dev, "Version ID is %04x\n", host->verid);
2340
2341 if (host->verid < DW_MMC_240A)
2342 host->data_offset = DATA_OFFSET;
2343 else
2344 host->data_offset = DATA_240A_OFFSET;
2345
Will Newtonf95f3852011-01-02 01:11:59 -05002346 tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
Thomas Abraham95dcc2c2012-05-01 14:57:36 -07002347 host->card_workqueue = alloc_workqueue("dw-mci-card",
James Hogan1791b13e2011-06-24 13:55:55 +01002348 WQ_MEM_RECLAIM | WQ_NON_REENTRANT, 1);
Wei Yongjunef7aef92013-04-19 09:25:45 +08002349 if (!host->card_workqueue) {
2350 ret = -ENOMEM;
James Hogan1791b13e2011-06-24 13:55:55 +01002351 goto err_dmaunmap;
Wei Yongjunef7aef92013-04-19 09:25:45 +08002352 }
James Hogan1791b13e2011-06-24 13:55:55 +01002353 INIT_WORK(&host->card_work, dw_mci_work_routine_card);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002354 ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
2355 host->irq_flags, "dw-mci", host);
Will Newtonf95f3852011-01-02 01:11:59 -05002356 if (ret)
James Hogan1791b13e2011-06-24 13:55:55 +01002357 goto err_workqueue;
Will Newtonf95f3852011-01-02 01:11:59 -05002358
Will Newtonf95f3852011-01-02 01:11:59 -05002359 if (host->pdata->num_slots)
2360 host->num_slots = host->pdata->num_slots;
2361 else
2362 host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
2363
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05302364 /*
2365 * Enable interrupts for command done, data over, data empty, card det,
2366 * receive ready and error such as transmit, receive timeout, crc error
2367 */
2368 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2369 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
2370 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
2371 DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
2372 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
2373
2374 dev_info(host->dev, "DW MMC controller at irq %d, "
2375 "%d bit host data width, "
2376 "%u deep fifo\n",
2377 host->irq, width, fifo_size);
2378
Will Newtonf95f3852011-01-02 01:11:59 -05002379 /* We need at least one slot to succeed */
2380 for (i = 0; i < host->num_slots; i++) {
2381 ret = dw_mci_init_slot(host, i);
Thomas Abraham1c2215b2012-09-17 18:16:37 +00002382 if (ret)
2383 dev_dbg(host->dev, "slot %d init failed\n", i);
2384 else
2385 init_slots++;
2386 }
2387
2388 if (init_slots) {
2389 dev_info(host->dev, "%d slots initialized\n", init_slots);
2390 } else {
2391 dev_dbg(host->dev, "attempted to initialize %d slots, "
2392 "but failed on all\n", host->num_slots);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002393 goto err_workqueue;
Will Newtonf95f3852011-01-02 01:11:59 -05002394 }
2395
Will Newtonf95f3852011-01-02 01:11:59 -05002396 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
Thomas Abraham4a909202012-09-17 18:16:35 +00002397 dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002398
2399 return 0;
2400
James Hogan1791b13e2011-06-24 13:55:55 +01002401err_workqueue:
Thomas Abraham95dcc2c2012-05-01 14:57:36 -07002402 destroy_workqueue(host->card_workqueue);
James Hogan1791b13e2011-06-24 13:55:55 +01002403
Will Newtonf95f3852011-01-02 01:11:59 -05002404err_dmaunmap:
2405 if (host->use_dma && host->dma_ops->exit)
2406 host->dma_ops->exit(host);
Will Newtonf95f3852011-01-02 01:11:59 -05002407
Doug Anderson870556a2013-06-07 10:28:29 -07002408err_regulator:
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002409 if (host->vmmc)
Jaehoon Chungc07946a2011-02-25 11:08:14 +09002410 regulator_disable(host->vmmc);
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002411
2412err_clk_ciu:
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002413 if (!IS_ERR(host->ciu_clk))
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002414 clk_disable_unprepare(host->ciu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002415
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002416err_clk_biu:
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002417 if (!IS_ERR(host->biu_clk))
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002418 clk_disable_unprepare(host->biu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002419
Will Newtonf95f3852011-01-02 01:11:59 -05002420 return ret;
2421}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302422EXPORT_SYMBOL(dw_mci_probe);
Will Newtonf95f3852011-01-02 01:11:59 -05002423
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302424void dw_mci_remove(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002425{
Will Newtonf95f3852011-01-02 01:11:59 -05002426 int i;
2427
2428 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2429 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
2430
Will Newtonf95f3852011-01-02 01:11:59 -05002431 for (i = 0; i < host->num_slots; i++) {
Thomas Abraham4a909202012-09-17 18:16:35 +00002432 dev_dbg(host->dev, "remove slot %d\n", i);
Will Newtonf95f3852011-01-02 01:11:59 -05002433 if (host->slot[i])
2434 dw_mci_cleanup_slot(host->slot[i], i);
2435 }
2436
2437 /* disable clock to CIU */
2438 mci_writel(host, CLKENA, 0);
2439 mci_writel(host, CLKSRC, 0);
2440
Thomas Abraham95dcc2c2012-05-01 14:57:36 -07002441 destroy_workqueue(host->card_workqueue);
Will Newtonf95f3852011-01-02 01:11:59 -05002442
2443 if (host->use_dma && host->dma_ops->exit)
2444 host->dma_ops->exit(host);
2445
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002446 if (host->vmmc)
Jaehoon Chungc07946a2011-02-25 11:08:14 +09002447 regulator_disable(host->vmmc);
Jaehoon Chungc07946a2011-02-25 11:08:14 +09002448
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002449 if (!IS_ERR(host->ciu_clk))
2450 clk_disable_unprepare(host->ciu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002451
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002452 if (!IS_ERR(host->biu_clk))
2453 clk_disable_unprepare(host->biu_clk);
Will Newtonf95f3852011-01-02 01:11:59 -05002454}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302455EXPORT_SYMBOL(dw_mci_remove);
2456
2457
Will Newtonf95f3852011-01-02 01:11:59 -05002458
Jaehoon Chung6fe88902011-12-08 19:23:03 +09002459#ifdef CONFIG_PM_SLEEP
Will Newtonf95f3852011-01-02 01:11:59 -05002460/*
2461 * TODO: we should probably disable the clock to the card in the suspend path.
2462 */
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302463int dw_mci_suspend(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002464{
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302465 int i, ret = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002466
2467 for (i = 0; i < host->num_slots; i++) {
2468 struct dw_mci_slot *slot = host->slot[i];
2469 if (!slot)
2470 continue;
2471 ret = mmc_suspend_host(slot->mmc);
2472 if (ret < 0) {
2473 while (--i >= 0) {
2474 slot = host->slot[i];
2475 if (slot)
2476 mmc_resume_host(host->slot[i]->mmc);
2477 }
2478 return ret;
2479 }
2480 }
2481
Jaehoon Chungc07946a2011-02-25 11:08:14 +09002482 if (host->vmmc)
2483 regulator_disable(host->vmmc);
2484
Will Newtonf95f3852011-01-02 01:11:59 -05002485 return 0;
2486}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302487EXPORT_SYMBOL(dw_mci_suspend);
Will Newtonf95f3852011-01-02 01:11:59 -05002488
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302489int dw_mci_resume(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002490{
2491 int i, ret;
Will Newtonf95f3852011-01-02 01:11:59 -05002492
Sachin Kamatf2f942c2013-04-04 11:25:10 +05302493 if (host->vmmc) {
2494 ret = regulator_enable(host->vmmc);
2495 if (ret) {
2496 dev_err(host->dev,
2497 "failed to enable regulator: %d\n", ret);
2498 return ret;
2499 }
2500 }
Jaehoon Chung1d6c4e02011-05-11 15:52:39 +09002501
Thomas Abraham4a909202012-09-17 18:16:35 +00002502 if (!mci_wait_reset(host->dev, host)) {
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002503 ret = -ENODEV;
2504 return ret;
2505 }
2506
Jonathan Kliegman3bfe6192012-06-14 13:31:55 -04002507 if (host->use_dma && host->dma_ops->init)
Seungwon Jeon141a7122012-05-22 13:01:03 +09002508 host->dma_ops->init(host);
2509
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002510 /* Restore the old value at FIFOTH register */
2511 mci_writel(host, FIFOTH, host->fifoth_val);
2512
Doug Anderson2eb29442013-08-31 00:11:49 +09002513 /* Put in max timeout */
2514 mci_writel(host, TMOUT, 0xFFFFFFFF);
2515
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002516 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2517 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
2518 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
2519 DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
2520 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
2521
Will Newtonf95f3852011-01-02 01:11:59 -05002522 for (i = 0; i < host->num_slots; i++) {
2523 struct dw_mci_slot *slot = host->slot[i];
2524 if (!slot)
2525 continue;
Abhilash Kesavanab269122012-11-19 10:26:21 +05302526 if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER) {
2527 dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
2528 dw_mci_setup_bus(slot, true);
2529 }
2530
Will Newtonf95f3852011-01-02 01:11:59 -05002531 ret = mmc_resume_host(host->slot[i]->mmc);
2532 if (ret < 0)
2533 return ret;
2534 }
Will Newtonf95f3852011-01-02 01:11:59 -05002535 return 0;
2536}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302537EXPORT_SYMBOL(dw_mci_resume);
Jaehoon Chung6fe88902011-12-08 19:23:03 +09002538#endif /* CONFIG_PM_SLEEP */
2539
Will Newtonf95f3852011-01-02 01:11:59 -05002540static int __init dw_mci_init(void)
2541{
Sachin Kamat8e1c4e42013-04-04 11:25:11 +05302542 pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302543 return 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002544}
2545
2546static void __exit dw_mci_exit(void)
2547{
Will Newtonf95f3852011-01-02 01:11:59 -05002548}
2549
2550module_init(dw_mci_init);
2551module_exit(dw_mci_exit);
2552
2553MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
2554MODULE_AUTHOR("NXP Semiconductor VietNam");
2555MODULE_AUTHOR("Imagination Technologies Ltd");
2556MODULE_LICENSE("GPL v2");