blob: c792466fb8ac7f51300c35a7b2ce8e78bc05a7cb [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>
Will Newtonf95f3852011-01-02 01:11:59 -050037
38#include "dw_mmc.h"
39
40/* Common flag combinations */
41#define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DTO | SDMMC_INT_DCRC | \
42 SDMMC_INT_HTO | SDMMC_INT_SBE | \
43 SDMMC_INT_EBE)
44#define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
45 SDMMC_INT_RESP_ERR)
46#define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
47 DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
48#define DW_MCI_SEND_STATUS 1
49#define DW_MCI_RECV_STATUS 2
50#define DW_MCI_DMA_THRESHOLD 16
51
52#ifdef CONFIG_MMC_DW_IDMAC
53struct idmac_desc {
54 u32 des0; /* Control Descriptor */
55#define IDMAC_DES0_DIC BIT(1)
56#define IDMAC_DES0_LD BIT(2)
57#define IDMAC_DES0_FD BIT(3)
58#define IDMAC_DES0_CH BIT(4)
59#define IDMAC_DES0_ER BIT(5)
60#define IDMAC_DES0_CES BIT(30)
61#define IDMAC_DES0_OWN BIT(31)
62
63 u32 des1; /* Buffer sizes */
64#define IDMAC_SET_BUFFER1_SIZE(d, s) \
Shashidhar Hiremath9b7bbe12011-07-29 08:49:50 -040065 ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
Will Newtonf95f3852011-01-02 01:11:59 -050066
67 u32 des2; /* buffer 1 physical address */
68
69 u32 des3; /* buffer 2 physical address */
70};
71#endif /* CONFIG_MMC_DW_IDMAC */
72
73/**
74 * struct dw_mci_slot - MMC slot state
75 * @mmc: The mmc_host representing this slot.
76 * @host: The MMC controller this slot is using.
77 * @ctype: Card type for this slot.
78 * @mrq: mmc_request currently being processed or waiting to be
79 * processed, or NULL when the slot is idle.
80 * @queue_node: List node for placing this node in the @queue list of
81 * &struct dw_mci.
82 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
83 * @flags: Random state bits associated with the slot.
84 * @id: Number of this slot.
85 * @last_detect_state: Most recently observed card detect state.
86 */
87struct dw_mci_slot {
88 struct mmc_host *mmc;
89 struct dw_mci *host;
90
91 u32 ctype;
92
93 struct mmc_request *mrq;
94 struct list_head queue_node;
95
96 unsigned int clock;
97 unsigned long flags;
98#define DW_MMC_CARD_PRESENT 0
99#define DW_MMC_CARD_NEED_INIT 1
100 int id;
101 int last_detect_state;
102};
103
104#if defined(CONFIG_DEBUG_FS)
105static int dw_mci_req_show(struct seq_file *s, void *v)
106{
107 struct dw_mci_slot *slot = s->private;
108 struct mmc_request *mrq;
109 struct mmc_command *cmd;
110 struct mmc_command *stop;
111 struct mmc_data *data;
112
113 /* Make sure we get a consistent snapshot */
114 spin_lock_bh(&slot->host->lock);
115 mrq = slot->mrq;
116
117 if (mrq) {
118 cmd = mrq->cmd;
119 data = mrq->data;
120 stop = mrq->stop;
121
122 if (cmd)
123 seq_printf(s,
124 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
125 cmd->opcode, cmd->arg, cmd->flags,
126 cmd->resp[0], cmd->resp[1], cmd->resp[2],
127 cmd->resp[2], cmd->error);
128 if (data)
129 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
130 data->bytes_xfered, data->blocks,
131 data->blksz, data->flags, data->error);
132 if (stop)
133 seq_printf(s,
134 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
135 stop->opcode, stop->arg, stop->flags,
136 stop->resp[0], stop->resp[1], stop->resp[2],
137 stop->resp[2], stop->error);
138 }
139
140 spin_unlock_bh(&slot->host->lock);
141
142 return 0;
143}
144
145static int dw_mci_req_open(struct inode *inode, struct file *file)
146{
147 return single_open(file, dw_mci_req_show, inode->i_private);
148}
149
150static const struct file_operations dw_mci_req_fops = {
151 .owner = THIS_MODULE,
152 .open = dw_mci_req_open,
153 .read = seq_read,
154 .llseek = seq_lseek,
155 .release = single_release,
156};
157
158static int dw_mci_regs_show(struct seq_file *s, void *v)
159{
160 seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
161 seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
162 seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
163 seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
164 seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
165 seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
166
167 return 0;
168}
169
170static int dw_mci_regs_open(struct inode *inode, struct file *file)
171{
172 return single_open(file, dw_mci_regs_show, inode->i_private);
173}
174
175static const struct file_operations dw_mci_regs_fops = {
176 .owner = THIS_MODULE,
177 .open = dw_mci_regs_open,
178 .read = seq_read,
179 .llseek = seq_lseek,
180 .release = single_release,
181};
182
183static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
184{
185 struct mmc_host *mmc = slot->mmc;
186 struct dw_mci *host = slot->host;
187 struct dentry *root;
188 struct dentry *node;
189
190 root = mmc->debugfs_root;
191 if (!root)
192 return;
193
194 node = debugfs_create_file("regs", S_IRUSR, root, host,
195 &dw_mci_regs_fops);
196 if (!node)
197 goto err;
198
199 node = debugfs_create_file("req", S_IRUSR, root, slot,
200 &dw_mci_req_fops);
201 if (!node)
202 goto err;
203
204 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
205 if (!node)
206 goto err;
207
208 node = debugfs_create_x32("pending_events", S_IRUSR, root,
209 (u32 *)&host->pending_events);
210 if (!node)
211 goto err;
212
213 node = debugfs_create_x32("completed_events", S_IRUSR, root,
214 (u32 *)&host->completed_events);
215 if (!node)
216 goto err;
217
218 return;
219
220err:
221 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
222}
223#endif /* defined(CONFIG_DEBUG_FS) */
224
225static void dw_mci_set_timeout(struct dw_mci *host)
226{
227 /* timeout (maximum) */
228 mci_writel(host, TMOUT, 0xffffffff);
229}
230
231static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
232{
233 struct mmc_data *data;
234 u32 cmdr;
235 cmd->error = -EINPROGRESS;
236
237 cmdr = cmd->opcode;
238
239 if (cmdr == MMC_STOP_TRANSMISSION)
240 cmdr |= SDMMC_CMD_STOP;
241 else
242 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
243
244 if (cmd->flags & MMC_RSP_PRESENT) {
245 /* We expect a response, so set this bit */
246 cmdr |= SDMMC_CMD_RESP_EXP;
247 if (cmd->flags & MMC_RSP_136)
248 cmdr |= SDMMC_CMD_RESP_LONG;
249 }
250
251 if (cmd->flags & MMC_RSP_CRC)
252 cmdr |= SDMMC_CMD_RESP_CRC;
253
254 data = cmd->data;
255 if (data) {
256 cmdr |= SDMMC_CMD_DAT_EXP;
257 if (data->flags & MMC_DATA_STREAM)
258 cmdr |= SDMMC_CMD_STRM_MODE;
259 if (data->flags & MMC_DATA_WRITE)
260 cmdr |= SDMMC_CMD_DAT_WR;
261 }
262
263 return cmdr;
264}
265
266static void dw_mci_start_command(struct dw_mci *host,
267 struct mmc_command *cmd, u32 cmd_flags)
268{
269 host->cmd = cmd;
Thomas Abraham4a909202012-09-17 18:16:35 +0000270 dev_vdbg(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -0500271 "start command: ARGR=0x%08x CMDR=0x%08x\n",
272 cmd->arg, cmd_flags);
273
274 mci_writel(host, CMDARG, cmd->arg);
275 wmb();
276
277 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
278}
279
280static void send_stop_cmd(struct dw_mci *host, struct mmc_data *data)
281{
282 dw_mci_start_command(host, data->stop, host->stop_cmdr);
283}
284
285/* DMA interface functions */
286static void dw_mci_stop_dma(struct dw_mci *host)
287{
James Hogan03e8cb52011-06-29 09:28:43 +0100288 if (host->using_dma) {
Will Newtonf95f3852011-01-02 01:11:59 -0500289 host->dma_ops->stop(host);
290 host->dma_ops->cleanup(host);
291 } else {
292 /* Data transfer was stopped by the interrupt handler */
293 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
294 }
295}
296
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900297static int dw_mci_get_dma_dir(struct mmc_data *data)
298{
299 if (data->flags & MMC_DATA_WRITE)
300 return DMA_TO_DEVICE;
301 else
302 return DMA_FROM_DEVICE;
303}
304
Jaehoon Chung9beee912012-02-16 11:19:38 +0900305#ifdef CONFIG_MMC_DW_IDMAC
Will Newtonf95f3852011-01-02 01:11:59 -0500306static void dw_mci_dma_cleanup(struct dw_mci *host)
307{
308 struct mmc_data *data = host->data;
309
310 if (data)
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900311 if (!data->host_cookie)
Thomas Abraham4a909202012-09-17 18:16:35 +0000312 dma_unmap_sg(host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900313 data->sg,
314 data->sg_len,
315 dw_mci_get_dma_dir(data));
Will Newtonf95f3852011-01-02 01:11:59 -0500316}
317
318static void dw_mci_idmac_stop_dma(struct dw_mci *host)
319{
320 u32 temp;
321
322 /* Disable and reset the IDMAC interface */
323 temp = mci_readl(host, CTRL);
324 temp &= ~SDMMC_CTRL_USE_IDMAC;
325 temp |= SDMMC_CTRL_DMA_RESET;
326 mci_writel(host, CTRL, temp);
327
328 /* Stop the IDMAC running */
329 temp = mci_readl(host, BMOD);
Jaehoon Chunga5289a42011-02-25 11:08:13 +0900330 temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
Will Newtonf95f3852011-01-02 01:11:59 -0500331 mci_writel(host, BMOD, temp);
332}
333
334static void dw_mci_idmac_complete_dma(struct dw_mci *host)
335{
336 struct mmc_data *data = host->data;
337
Thomas Abraham4a909202012-09-17 18:16:35 +0000338 dev_vdbg(host->dev, "DMA complete\n");
Will Newtonf95f3852011-01-02 01:11:59 -0500339
340 host->dma_ops->cleanup(host);
341
342 /*
343 * If the card was removed, data will be NULL. No point in trying to
344 * send the stop command or waiting for NBUSY in this case.
345 */
346 if (data) {
347 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
348 tasklet_schedule(&host->tasklet);
349 }
350}
351
352static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
353 unsigned int sg_len)
354{
355 int i;
356 struct idmac_desc *desc = host->sg_cpu;
357
358 for (i = 0; i < sg_len; i++, desc++) {
359 unsigned int length = sg_dma_len(&data->sg[i]);
360 u32 mem_addr = sg_dma_address(&data->sg[i]);
361
362 /* Set the OWN bit and disable interrupts for this descriptor */
363 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | IDMAC_DES0_CH;
364
365 /* Buffer length */
366 IDMAC_SET_BUFFER1_SIZE(desc, length);
367
368 /* Physical address to DMA to/from */
369 desc->des2 = mem_addr;
370 }
371
372 /* Set first descriptor */
373 desc = host->sg_cpu;
374 desc->des0 |= IDMAC_DES0_FD;
375
376 /* Set last descriptor */
377 desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc);
378 desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
379 desc->des0 |= IDMAC_DES0_LD;
380
381 wmb();
382}
383
384static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
385{
386 u32 temp;
387
388 dw_mci_translate_sglist(host, host->data, sg_len);
389
390 /* Select IDMAC interface */
391 temp = mci_readl(host, CTRL);
392 temp |= SDMMC_CTRL_USE_IDMAC;
393 mci_writel(host, CTRL, temp);
394
395 wmb();
396
397 /* Enable the IDMAC */
398 temp = mci_readl(host, BMOD);
Jaehoon Chunga5289a42011-02-25 11:08:13 +0900399 temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
Will Newtonf95f3852011-01-02 01:11:59 -0500400 mci_writel(host, BMOD, temp);
401
402 /* Start it running */
403 mci_writel(host, PLDMND, 1);
404}
405
406static int dw_mci_idmac_init(struct dw_mci *host)
407{
408 struct idmac_desc *p;
Girish K S94c6cee2012-06-12 15:28:22 +0530409 int i, dma_support;
Will Newtonf95f3852011-01-02 01:11:59 -0500410
411 /* Number of descriptors in the ring buffer */
412 host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
413
Girish K S94c6cee2012-06-12 15:28:22 +0530414 /* Check if Hardware Configuration Register has support for DMA */
415 dma_support = (mci_readl(host, HCON) >> 16) & 0x3;
416
417 if (!dma_support || dma_support > 2) {
Thomas Abraham4a909202012-09-17 18:16:35 +0000418 dev_err(host->dev,
Girish K S94c6cee2012-06-12 15:28:22 +0530419 "Host Controller does not support IDMA Tx.\n");
420 host->dma_ops = NULL;
421 return -ENODEV;
422 }
423
Thomas Abraham4a909202012-09-17 18:16:35 +0000424 dev_info(host->dev, "Using internal DMA controller.\n");
Girish K S94c6cee2012-06-12 15:28:22 +0530425
Will Newtonf95f3852011-01-02 01:11:59 -0500426 /* Forward link the descriptor list */
427 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++)
428 p->des3 = host->sg_dma + (sizeof(struct idmac_desc) * (i + 1));
429
430 /* Set the last descriptor as the end-of-ring descriptor */
431 p->des3 = host->sg_dma;
432 p->des0 = IDMAC_DES0_ER;
433
Seungwon Jeon141a7122012-05-22 13:01:03 +0900434 mci_writel(host, BMOD, SDMMC_IDMAC_SWRESET);
435
Will Newtonf95f3852011-01-02 01:11:59 -0500436 /* Mask out interrupts - get Tx & Rx complete only */
437 mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI | SDMMC_IDMAC_INT_RI |
438 SDMMC_IDMAC_INT_TI);
439
440 /* Set the descriptor base address */
441 mci_writel(host, DBADDR, host->sg_dma);
442 return 0;
443}
444
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900445static struct dw_mci_dma_ops dw_mci_idmac_ops = {
446 .init = dw_mci_idmac_init,
447 .start = dw_mci_idmac_start_dma,
448 .stop = dw_mci_idmac_stop_dma,
449 .complete = dw_mci_idmac_complete_dma,
450 .cleanup = dw_mci_dma_cleanup,
451};
452#endif /* CONFIG_MMC_DW_IDMAC */
453
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900454static int dw_mci_pre_dma_transfer(struct dw_mci *host,
455 struct mmc_data *data,
456 bool next)
Will Newtonf95f3852011-01-02 01:11:59 -0500457{
458 struct scatterlist *sg;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900459 unsigned int i, sg_len;
Will Newtonf95f3852011-01-02 01:11:59 -0500460
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900461 if (!next && data->host_cookie)
462 return data->host_cookie;
Will Newtonf95f3852011-01-02 01:11:59 -0500463
464 /*
465 * We don't do DMA on "complex" transfers, i.e. with
466 * non-word-aligned buffers or lengths. Also, we don't bother
467 * with all the DMA setup overhead for short transfers.
468 */
469 if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
470 return -EINVAL;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900471
Will Newtonf95f3852011-01-02 01:11:59 -0500472 if (data->blksz & 3)
473 return -EINVAL;
474
475 for_each_sg(data->sg, sg, data->sg_len, i) {
476 if (sg->offset & 3 || sg->length & 3)
477 return -EINVAL;
478 }
479
Thomas Abraham4a909202012-09-17 18:16:35 +0000480 sg_len = dma_map_sg(host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900481 data->sg,
482 data->sg_len,
483 dw_mci_get_dma_dir(data));
484 if (sg_len == 0)
485 return -EINVAL;
486
487 if (next)
488 data->host_cookie = sg_len;
489
490 return sg_len;
491}
492
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900493static void dw_mci_pre_req(struct mmc_host *mmc,
494 struct mmc_request *mrq,
495 bool is_first_req)
496{
497 struct dw_mci_slot *slot = mmc_priv(mmc);
498 struct mmc_data *data = mrq->data;
499
500 if (!slot->host->use_dma || !data)
501 return;
502
503 if (data->host_cookie) {
504 data->host_cookie = 0;
505 return;
506 }
507
508 if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0)
509 data->host_cookie = 0;
510}
511
512static void dw_mci_post_req(struct mmc_host *mmc,
513 struct mmc_request *mrq,
514 int err)
515{
516 struct dw_mci_slot *slot = mmc_priv(mmc);
517 struct mmc_data *data = mrq->data;
518
519 if (!slot->host->use_dma || !data)
520 return;
521
522 if (data->host_cookie)
Thomas Abraham4a909202012-09-17 18:16:35 +0000523 dma_unmap_sg(slot->host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900524 data->sg,
525 data->sg_len,
526 dw_mci_get_dma_dir(data));
527 data->host_cookie = 0;
528}
529
530static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
531{
532 int sg_len;
533 u32 temp;
534
535 host->using_dma = 0;
536
537 /* If we don't have a channel, we can't do DMA */
538 if (!host->use_dma)
539 return -ENODEV;
540
541 sg_len = dw_mci_pre_dma_transfer(host, data, 0);
Seungwon Jeona99aa9b2012-04-10 09:53:32 +0900542 if (sg_len < 0) {
543 host->dma_ops->stop(host);
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900544 return sg_len;
Seungwon Jeona99aa9b2012-04-10 09:53:32 +0900545 }
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900546
James Hogan03e8cb52011-06-29 09:28:43 +0100547 host->using_dma = 1;
548
Thomas Abraham4a909202012-09-17 18:16:35 +0000549 dev_vdbg(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -0500550 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
551 (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma,
552 sg_len);
553
554 /* Enable the DMA interface */
555 temp = mci_readl(host, CTRL);
556 temp |= SDMMC_CTRL_DMA_ENABLE;
557 mci_writel(host, CTRL, temp);
558
559 /* Disable RX/TX IRQs, let DMA handle it */
560 temp = mci_readl(host, INTMASK);
561 temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
562 mci_writel(host, INTMASK, temp);
563
564 host->dma_ops->start(host, sg_len);
565
566 return 0;
567}
568
569static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
570{
571 u32 temp;
572
573 data->error = -EINPROGRESS;
574
575 WARN_ON(host->data);
576 host->sg = NULL;
577 host->data = data;
578
James Hogan55c5efbc2011-06-29 09:29:58 +0100579 if (data->flags & MMC_DATA_READ)
580 host->dir_status = DW_MCI_RECV_STATUS;
581 else
582 host->dir_status = DW_MCI_SEND_STATUS;
583
Will Newtonf95f3852011-01-02 01:11:59 -0500584 if (dw_mci_submit_data_dma(host, data)) {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +0900585 int flags = SG_MITER_ATOMIC;
586 if (host->data->flags & MMC_DATA_READ)
587 flags |= SG_MITER_TO_SG;
588 else
589 flags |= SG_MITER_FROM_SG;
590
591 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Will Newtonf95f3852011-01-02 01:11:59 -0500592 host->sg = data->sg;
James Hogan34b664a2011-06-24 13:57:56 +0100593 host->part_buf_start = 0;
594 host->part_buf_count = 0;
Will Newtonf95f3852011-01-02 01:11:59 -0500595
James Hoganb40af3a2011-06-24 13:54:06 +0100596 mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
Will Newtonf95f3852011-01-02 01:11:59 -0500597 temp = mci_readl(host, INTMASK);
598 temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
599 mci_writel(host, INTMASK, temp);
600
601 temp = mci_readl(host, CTRL);
602 temp &= ~SDMMC_CTRL_DMA_ENABLE;
603 mci_writel(host, CTRL, temp);
604 }
605}
606
607static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
608{
609 struct dw_mci *host = slot->host;
610 unsigned long timeout = jiffies + msecs_to_jiffies(500);
611 unsigned int cmd_status = 0;
612
613 mci_writel(host, CMDARG, arg);
614 wmb();
615 mci_writel(host, CMD, SDMMC_CMD_START | cmd);
616
617 while (time_before(jiffies, timeout)) {
618 cmd_status = mci_readl(host, CMD);
619 if (!(cmd_status & SDMMC_CMD_START))
620 return;
621 }
622 dev_err(&slot->mmc->class_dev,
623 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
624 cmd, arg, cmd_status);
625}
626
627static void dw_mci_setup_bus(struct dw_mci_slot *slot)
628{
629 struct dw_mci *host = slot->host;
630 u32 div;
Doug Anderson9623b5b2012-07-25 08:33:17 -0700631 u32 clk_en_a;
Will Newtonf95f3852011-01-02 01:11:59 -0500632
633 if (slot->clock != host->current_speed) {
Seungwon Jeone4199902012-05-22 13:01:21 +0900634 div = host->bus_hz / slot->clock;
635 if (host->bus_hz % slot->clock && host->bus_hz > slot->clock)
Will Newtonf95f3852011-01-02 01:11:59 -0500636 /*
637 * move the + 1 after the divide to prevent
638 * over-clocking the card.
639 */
Seungwon Jeone4199902012-05-22 13:01:21 +0900640 div += 1;
641
642 div = (host->bus_hz != slot->clock) ? DIV_ROUND_UP(div, 2) : 0;
Will Newtonf95f3852011-01-02 01:11:59 -0500643
644 dev_info(&slot->mmc->class_dev,
645 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ"
646 " div = %d)\n", slot->id, host->bus_hz, slot->clock,
647 div ? ((host->bus_hz / div) >> 1) : host->bus_hz, div);
648
649 /* disable clock */
650 mci_writel(host, CLKENA, 0);
651 mci_writel(host, CLKSRC, 0);
652
653 /* inform CIU */
654 mci_send_cmd(slot,
655 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
656
657 /* set clock to desired speed */
658 mci_writel(host, CLKDIV, div);
659
660 /* inform CIU */
661 mci_send_cmd(slot,
662 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
663
Doug Anderson9623b5b2012-07-25 08:33:17 -0700664 /* enable clock; only low power if no SDIO */
665 clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
666 if (!(mci_readl(host, INTMASK) & SDMMC_INT_SDIO(slot->id)))
667 clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
668 mci_writel(host, CLKENA, clk_en_a);
Will Newtonf95f3852011-01-02 01:11:59 -0500669
670 /* inform CIU */
671 mci_send_cmd(slot,
672 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
673
674 host->current_speed = slot->clock;
675 }
676
677 /* Set the current slot bus width */
Seungwon Jeon1d56c452011-06-20 17:23:53 +0900678 mci_writel(host, CTYPE, (slot->ctype << slot->id));
Will Newtonf95f3852011-01-02 01:11:59 -0500679}
680
Seungwon Jeon053b3ce2011-12-22 18:01:29 +0900681static void __dw_mci_start_request(struct dw_mci *host,
682 struct dw_mci_slot *slot,
683 struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -0500684{
685 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -0500686 struct mmc_data *data;
687 u32 cmdflags;
688
689 mrq = slot->mrq;
690 if (host->pdata->select_slot)
691 host->pdata->select_slot(slot->id);
692
693 /* Slot specific timing and width adjustment */
694 dw_mci_setup_bus(slot);
695
696 host->cur_slot = slot;
697 host->mrq = mrq;
698
699 host->pending_events = 0;
700 host->completed_events = 0;
701 host->data_status = 0;
702
Seungwon Jeon053b3ce2011-12-22 18:01:29 +0900703 data = cmd->data;
Will Newtonf95f3852011-01-02 01:11:59 -0500704 if (data) {
705 dw_mci_set_timeout(host);
706 mci_writel(host, BYTCNT, data->blksz*data->blocks);
707 mci_writel(host, BLKSIZ, data->blksz);
708 }
709
Will Newtonf95f3852011-01-02 01:11:59 -0500710 cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
711
712 /* this is the first command, send the initialization clock */
713 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
714 cmdflags |= SDMMC_CMD_INIT;
715
716 if (data) {
717 dw_mci_submit_data(host, data);
718 wmb();
719 }
720
721 dw_mci_start_command(host, cmd, cmdflags);
722
723 if (mrq->stop)
724 host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
725}
726
Seungwon Jeon053b3ce2011-12-22 18:01:29 +0900727static void dw_mci_start_request(struct dw_mci *host,
728 struct dw_mci_slot *slot)
729{
730 struct mmc_request *mrq = slot->mrq;
731 struct mmc_command *cmd;
732
733 cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
734 __dw_mci_start_request(host, slot, cmd);
735}
736
James Hogan7456caa2011-06-24 13:55:10 +0100737/* must be called with host->lock held */
Will Newtonf95f3852011-01-02 01:11:59 -0500738static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
739 struct mmc_request *mrq)
740{
741 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
742 host->state);
743
Will Newtonf95f3852011-01-02 01:11:59 -0500744 slot->mrq = mrq;
745
746 if (host->state == STATE_IDLE) {
747 host->state = STATE_SENDING_CMD;
748 dw_mci_start_request(host, slot);
749 } else {
750 list_add_tail(&slot->queue_node, &host->queue);
751 }
Will Newtonf95f3852011-01-02 01:11:59 -0500752}
753
754static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
755{
756 struct dw_mci_slot *slot = mmc_priv(mmc);
757 struct dw_mci *host = slot->host;
758
759 WARN_ON(slot->mrq);
760
James Hogan7456caa2011-06-24 13:55:10 +0100761 /*
762 * The check for card presence and queueing of the request must be
763 * atomic, otherwise the card could be removed in between and the
764 * request wouldn't fail until another card was inserted.
765 */
766 spin_lock_bh(&host->lock);
767
Will Newtonf95f3852011-01-02 01:11:59 -0500768 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
James Hogan7456caa2011-06-24 13:55:10 +0100769 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -0500770 mrq->cmd->error = -ENOMEDIUM;
771 mmc_request_done(mmc, mrq);
772 return;
773 }
774
Will Newtonf95f3852011-01-02 01:11:59 -0500775 dw_mci_queue_request(host, slot, mrq);
James Hogan7456caa2011-06-24 13:55:10 +0100776
777 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -0500778}
779
780static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
781{
782 struct dw_mci_slot *slot = mmc_priv(mmc);
Jaehoon Chung41babf72011-02-24 13:46:11 +0900783 u32 regs;
Will Newtonf95f3852011-01-02 01:11:59 -0500784
785 /* set default 1 bit mode */
786 slot->ctype = SDMMC_CTYPE_1BIT;
787
788 switch (ios->bus_width) {
789 case MMC_BUS_WIDTH_1:
790 slot->ctype = SDMMC_CTYPE_1BIT;
791 break;
792 case MMC_BUS_WIDTH_4:
793 slot->ctype = SDMMC_CTYPE_4BIT;
794 break;
Jaehoon Chungc9b2a062011-02-17 16:12:38 +0900795 case MMC_BUS_WIDTH_8:
796 slot->ctype = SDMMC_CTYPE_8BIT;
797 break;
Will Newtonf95f3852011-01-02 01:11:59 -0500798 }
799
Seungwon Jeon3f514292012-01-02 16:00:02 +0900800 regs = mci_readl(slot->host, UHS_REG);
801
Jaehoon Chung41babf72011-02-24 13:46:11 +0900802 /* DDR mode set */
Seungwon Jeon3f514292012-01-02 16:00:02 +0900803 if (ios->timing == MMC_TIMING_UHS_DDR50)
Jaehoon Chung41babf72011-02-24 13:46:11 +0900804 regs |= (0x1 << slot->id) << 16;
Seungwon Jeon3f514292012-01-02 16:00:02 +0900805 else
806 regs &= ~(0x1 << slot->id) << 16;
807
808 mci_writel(slot->host, UHS_REG, regs);
Jaehoon Chung41babf72011-02-24 13:46:11 +0900809
Will Newtonf95f3852011-01-02 01:11:59 -0500810 if (ios->clock) {
811 /*
812 * Use mirror of ios->clock to prevent race with mmc
813 * core ios update when finding the minimum.
814 */
815 slot->clock = ios->clock;
816 }
817
818 switch (ios->power_mode) {
819 case MMC_POWER_UP:
820 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
821 break;
822 default:
823 break;
824 }
825}
826
827static int dw_mci_get_ro(struct mmc_host *mmc)
828{
829 int read_only;
830 struct dw_mci_slot *slot = mmc_priv(mmc);
831 struct dw_mci_board *brd = slot->host->pdata;
832
833 /* Use platform get_ro function, else try on board write protect */
Thomas Abrahamb4967aa2012-09-17 18:16:39 +0000834 if (brd->quirks & DW_MCI_QUIRK_NO_WRITE_PROTECT)
835 read_only = 0;
836 else if (brd->get_ro)
Will Newtonf95f3852011-01-02 01:11:59 -0500837 read_only = brd->get_ro(slot->id);
838 else
839 read_only =
840 mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
841
842 dev_dbg(&mmc->class_dev, "card is %s\n",
843 read_only ? "read-only" : "read-write");
844
845 return read_only;
846}
847
848static int dw_mci_get_cd(struct mmc_host *mmc)
849{
850 int present;
851 struct dw_mci_slot *slot = mmc_priv(mmc);
852 struct dw_mci_board *brd = slot->host->pdata;
853
854 /* Use platform get_cd function, else try onboard card detect */
Jaehoon Chungfc3d7722011-02-25 11:08:15 +0900855 if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
856 present = 1;
857 else if (brd->get_cd)
Will Newtonf95f3852011-01-02 01:11:59 -0500858 present = !brd->get_cd(slot->id);
859 else
860 present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
861 == 0 ? 1 : 0;
862
863 if (present)
864 dev_dbg(&mmc->class_dev, "card is present\n");
865 else
866 dev_dbg(&mmc->class_dev, "card is not present\n");
867
868 return present;
869}
870
Doug Anderson9623b5b2012-07-25 08:33:17 -0700871/*
872 * Disable lower power mode.
873 *
874 * Low power mode will stop the card clock when idle. According to the
875 * description of the CLKENA register we should disable low power mode
876 * for SDIO cards if we need SDIO interrupts to work.
877 *
878 * This function is fast if low power mode is already disabled.
879 */
880static void dw_mci_disable_low_power(struct dw_mci_slot *slot)
881{
882 struct dw_mci *host = slot->host;
883 u32 clk_en_a;
884 const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
885
886 clk_en_a = mci_readl(host, CLKENA);
887
888 if (clk_en_a & clken_low_pwr) {
889 mci_writel(host, CLKENA, clk_en_a & ~clken_low_pwr);
890 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
891 SDMMC_CMD_PRV_DAT_WAIT, 0);
892 }
893}
894
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +0530895static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
896{
897 struct dw_mci_slot *slot = mmc_priv(mmc);
898 struct dw_mci *host = slot->host;
899 u32 int_mask;
900
901 /* Enable/disable Slot Specific SDIO interrupt */
902 int_mask = mci_readl(host, INTMASK);
903 if (enb) {
Doug Anderson9623b5b2012-07-25 08:33:17 -0700904 /*
905 * Turn off low power mode if it was enabled. This is a bit of
906 * a heavy operation and we disable / enable IRQs a lot, so
907 * we'll leave low power mode disabled and it will get
908 * re-enabled again in dw_mci_setup_bus().
909 */
910 dw_mci_disable_low_power(slot);
911
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +0530912 mci_writel(host, INTMASK,
Kyoungil Kim705ad042012-05-14 17:38:48 +0900913 (int_mask | SDMMC_INT_SDIO(slot->id)));
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +0530914 } else {
915 mci_writel(host, INTMASK,
Kyoungil Kim705ad042012-05-14 17:38:48 +0900916 (int_mask & ~SDMMC_INT_SDIO(slot->id)));
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +0530917 }
918}
919
Will Newtonf95f3852011-01-02 01:11:59 -0500920static const struct mmc_host_ops dw_mci_ops = {
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +0530921 .request = dw_mci_request,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900922 .pre_req = dw_mci_pre_req,
923 .post_req = dw_mci_post_req,
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +0530924 .set_ios = dw_mci_set_ios,
925 .get_ro = dw_mci_get_ro,
926 .get_cd = dw_mci_get_cd,
927 .enable_sdio_irq = dw_mci_enable_sdio_irq,
Will Newtonf95f3852011-01-02 01:11:59 -0500928};
929
930static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
931 __releases(&host->lock)
932 __acquires(&host->lock)
933{
934 struct dw_mci_slot *slot;
935 struct mmc_host *prev_mmc = host->cur_slot->mmc;
936
937 WARN_ON(host->cmd || host->data);
938
939 host->cur_slot->mrq = NULL;
940 host->mrq = NULL;
941 if (!list_empty(&host->queue)) {
942 slot = list_entry(host->queue.next,
943 struct dw_mci_slot, queue_node);
944 list_del(&slot->queue_node);
Thomas Abraham4a909202012-09-17 18:16:35 +0000945 dev_vdbg(host->dev, "list not empty: %s is next\n",
Will Newtonf95f3852011-01-02 01:11:59 -0500946 mmc_hostname(slot->mmc));
947 host->state = STATE_SENDING_CMD;
948 dw_mci_start_request(host, slot);
949 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +0000950 dev_vdbg(host->dev, "list empty\n");
Will Newtonf95f3852011-01-02 01:11:59 -0500951 host->state = STATE_IDLE;
952 }
953
954 spin_unlock(&host->lock);
955 mmc_request_done(prev_mmc, mrq);
956 spin_lock(&host->lock);
957}
958
959static void dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
960{
961 u32 status = host->cmd_status;
962
963 host->cmd_status = 0;
964
965 /* Read the response from the card (up to 16 bytes) */
966 if (cmd->flags & MMC_RSP_PRESENT) {
967 if (cmd->flags & MMC_RSP_136) {
968 cmd->resp[3] = mci_readl(host, RESP0);
969 cmd->resp[2] = mci_readl(host, RESP1);
970 cmd->resp[1] = mci_readl(host, RESP2);
971 cmd->resp[0] = mci_readl(host, RESP3);
972 } else {
973 cmd->resp[0] = mci_readl(host, RESP0);
974 cmd->resp[1] = 0;
975 cmd->resp[2] = 0;
976 cmd->resp[3] = 0;
977 }
978 }
979
980 if (status & SDMMC_INT_RTO)
981 cmd->error = -ETIMEDOUT;
982 else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
983 cmd->error = -EILSEQ;
984 else if (status & SDMMC_INT_RESP_ERR)
985 cmd->error = -EIO;
986 else
987 cmd->error = 0;
988
989 if (cmd->error) {
990 /* newer ip versions need a delay between retries */
991 if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
992 mdelay(20);
993
994 if (cmd->data) {
Will Newtonf95f3852011-01-02 01:11:59 -0500995 dw_mci_stop_dma(host);
Seungwon Jeonfda5f732012-05-22 13:01:13 +0900996 host->data = NULL;
Will Newtonf95f3852011-01-02 01:11:59 -0500997 }
998 }
999}
1000
1001static void dw_mci_tasklet_func(unsigned long priv)
1002{
1003 struct dw_mci *host = (struct dw_mci *)priv;
1004 struct mmc_data *data;
1005 struct mmc_command *cmd;
1006 enum dw_mci_state state;
1007 enum dw_mci_state prev_state;
James Hogan94dd5b32011-06-29 09:30:47 +01001008 u32 status, ctrl;
Will Newtonf95f3852011-01-02 01:11:59 -05001009
1010 spin_lock(&host->lock);
1011
1012 state = host->state;
1013 data = host->data;
1014
1015 do {
1016 prev_state = state;
1017
1018 switch (state) {
1019 case STATE_IDLE:
1020 break;
1021
1022 case STATE_SENDING_CMD:
1023 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1024 &host->pending_events))
1025 break;
1026
1027 cmd = host->cmd;
1028 host->cmd = NULL;
1029 set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001030 dw_mci_command_complete(host, cmd);
1031 if (cmd == host->mrq->sbc && !cmd->error) {
1032 prev_state = state = STATE_SENDING_CMD;
1033 __dw_mci_start_request(host, host->cur_slot,
1034 host->mrq->cmd);
1035 goto unlock;
1036 }
1037
Will Newtonf95f3852011-01-02 01:11:59 -05001038 if (!host->mrq->data || cmd->error) {
1039 dw_mci_request_end(host, host->mrq);
1040 goto unlock;
1041 }
1042
1043 prev_state = state = STATE_SENDING_DATA;
1044 /* fall through */
1045
1046 case STATE_SENDING_DATA:
1047 if (test_and_clear_bit(EVENT_DATA_ERROR,
1048 &host->pending_events)) {
1049 dw_mci_stop_dma(host);
1050 if (data->stop)
1051 send_stop_cmd(host, data);
1052 state = STATE_DATA_ERROR;
1053 break;
1054 }
1055
1056 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
1057 &host->pending_events))
1058 break;
1059
1060 set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
1061 prev_state = state = STATE_DATA_BUSY;
1062 /* fall through */
1063
1064 case STATE_DATA_BUSY:
1065 if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
1066 &host->pending_events))
1067 break;
1068
1069 host->data = NULL;
1070 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
1071 status = host->data_status;
1072
1073 if (status & DW_MCI_DATA_ERROR_FLAGS) {
1074 if (status & SDMMC_INT_DTO) {
Will Newtonf95f3852011-01-02 01:11:59 -05001075 data->error = -ETIMEDOUT;
1076 } else if (status & SDMMC_INT_DCRC) {
Will Newtonf95f3852011-01-02 01:11:59 -05001077 data->error = -EILSEQ;
James Hogan55c5efbc2011-06-29 09:29:58 +01001078 } else if (status & SDMMC_INT_EBE &&
1079 host->dir_status ==
1080 DW_MCI_SEND_STATUS) {
1081 /*
1082 * No data CRC status was returned.
1083 * The number of bytes transferred will
1084 * be exaggerated in PIO mode.
1085 */
1086 data->bytes_xfered = 0;
1087 data->error = -ETIMEDOUT;
Will Newtonf95f3852011-01-02 01:11:59 -05001088 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00001089 dev_err(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -05001090 "data FIFO error "
1091 "(status=%08x)\n",
1092 status);
1093 data->error = -EIO;
1094 }
James Hogan94dd5b32011-06-29 09:30:47 +01001095 /*
1096 * After an error, there may be data lingering
1097 * in the FIFO, so reset it - doing so
1098 * generates a block interrupt, hence setting
1099 * the scatter-gather pointer to NULL.
1100 */
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001101 sg_miter_stop(&host->sg_miter);
James Hogan94dd5b32011-06-29 09:30:47 +01001102 host->sg = NULL;
1103 ctrl = mci_readl(host, CTRL);
1104 ctrl |= SDMMC_CTRL_FIFO_RESET;
1105 mci_writel(host, CTRL, ctrl);
Will Newtonf95f3852011-01-02 01:11:59 -05001106 } else {
1107 data->bytes_xfered = data->blocks * data->blksz;
1108 data->error = 0;
1109 }
1110
1111 if (!data->stop) {
1112 dw_mci_request_end(host, host->mrq);
1113 goto unlock;
1114 }
1115
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001116 if (host->mrq->sbc && !data->error) {
1117 data->stop->error = 0;
1118 dw_mci_request_end(host, host->mrq);
1119 goto unlock;
1120 }
1121
Will Newtonf95f3852011-01-02 01:11:59 -05001122 prev_state = state = STATE_SENDING_STOP;
1123 if (!data->error)
1124 send_stop_cmd(host, data);
1125 /* fall through */
1126
1127 case STATE_SENDING_STOP:
1128 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1129 &host->pending_events))
1130 break;
1131
1132 host->cmd = NULL;
1133 dw_mci_command_complete(host, host->mrq->stop);
1134 dw_mci_request_end(host, host->mrq);
1135 goto unlock;
1136
1137 case STATE_DATA_ERROR:
1138 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
1139 &host->pending_events))
1140 break;
1141
1142 state = STATE_DATA_BUSY;
1143 break;
1144 }
1145 } while (state != prev_state);
1146
1147 host->state = state;
1148unlock:
1149 spin_unlock(&host->lock);
1150
1151}
1152
James Hogan34b664a2011-06-24 13:57:56 +01001153/* push final bytes to part_buf, only use during push */
1154static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
1155{
1156 memcpy((void *)&host->part_buf, buf, cnt);
1157 host->part_buf_count = cnt;
1158}
1159
1160/* append bytes to part_buf, only use during push */
1161static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
1162{
1163 cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
1164 memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
1165 host->part_buf_count += cnt;
1166 return cnt;
1167}
1168
1169/* pull first bytes from part_buf, only use during pull */
1170static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
1171{
1172 cnt = min(cnt, (int)host->part_buf_count);
1173 if (cnt) {
1174 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
1175 cnt);
1176 host->part_buf_count -= cnt;
1177 host->part_buf_start += cnt;
1178 }
1179 return cnt;
1180}
1181
1182/* pull final bytes from the part_buf, assuming it's just been filled */
1183static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
1184{
1185 memcpy(buf, &host->part_buf, cnt);
1186 host->part_buf_start = cnt;
1187 host->part_buf_count = (1 << host->data_shift) - cnt;
1188}
1189
Will Newtonf95f3852011-01-02 01:11:59 -05001190static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
1191{
James Hogan34b664a2011-06-24 13:57:56 +01001192 /* try and push anything in the part_buf */
1193 if (unlikely(host->part_buf_count)) {
1194 int len = dw_mci_push_part_bytes(host, buf, cnt);
1195 buf += len;
1196 cnt -= len;
1197 if (!sg_next(host->sg) || host->part_buf_count == 2) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001198 mci_writew(host, DATA(host->data_offset),
1199 host->part_buf16);
James Hogan34b664a2011-06-24 13:57:56 +01001200 host->part_buf_count = 0;
1201 }
1202 }
1203#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1204 if (unlikely((unsigned long)buf & 0x1)) {
1205 while (cnt >= 2) {
1206 u16 aligned_buf[64];
1207 int len = min(cnt & -2, (int)sizeof(aligned_buf));
1208 int items = len >> 1;
1209 int i;
1210 /* memcpy from input buffer into aligned buffer */
1211 memcpy(aligned_buf, buf, len);
1212 buf += len;
1213 cnt -= len;
1214 /* push data from aligned buffer into fifo */
1215 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001216 mci_writew(host, DATA(host->data_offset),
1217 aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01001218 }
1219 } else
1220#endif
1221 {
1222 u16 *pdata = buf;
1223 for (; cnt >= 2; cnt -= 2)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001224 mci_writew(host, DATA(host->data_offset), *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01001225 buf = pdata;
1226 }
1227 /* put anything remaining in the part_buf */
1228 if (cnt) {
1229 dw_mci_set_part_bytes(host, buf, cnt);
1230 if (!sg_next(host->sg))
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001231 mci_writew(host, DATA(host->data_offset),
1232 host->part_buf16);
Will Newtonf95f3852011-01-02 01:11:59 -05001233 }
1234}
1235
1236static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
1237{
James Hogan34b664a2011-06-24 13:57:56 +01001238#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1239 if (unlikely((unsigned long)buf & 0x1)) {
1240 while (cnt >= 2) {
1241 /* pull data from fifo into aligned buffer */
1242 u16 aligned_buf[64];
1243 int len = min(cnt & -2, (int)sizeof(aligned_buf));
1244 int items = len >> 1;
1245 int i;
1246 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001247 aligned_buf[i] = mci_readw(host,
1248 DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001249 /* memcpy from aligned buffer into output buffer */
1250 memcpy(buf, aligned_buf, len);
1251 buf += len;
1252 cnt -= len;
1253 }
1254 } else
1255#endif
1256 {
1257 u16 *pdata = buf;
1258 for (; cnt >= 2; cnt -= 2)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001259 *pdata++ = mci_readw(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001260 buf = pdata;
1261 }
1262 if (cnt) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001263 host->part_buf16 = mci_readw(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001264 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05001265 }
1266}
1267
1268static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
1269{
James Hogan34b664a2011-06-24 13:57:56 +01001270 /* try and push anything in the part_buf */
1271 if (unlikely(host->part_buf_count)) {
1272 int len = dw_mci_push_part_bytes(host, buf, cnt);
1273 buf += len;
1274 cnt -= len;
1275 if (!sg_next(host->sg) || host->part_buf_count == 4) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001276 mci_writel(host, DATA(host->data_offset),
1277 host->part_buf32);
James Hogan34b664a2011-06-24 13:57:56 +01001278 host->part_buf_count = 0;
1279 }
1280 }
1281#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1282 if (unlikely((unsigned long)buf & 0x3)) {
1283 while (cnt >= 4) {
1284 u32 aligned_buf[32];
1285 int len = min(cnt & -4, (int)sizeof(aligned_buf));
1286 int items = len >> 2;
1287 int i;
1288 /* memcpy from input buffer into aligned buffer */
1289 memcpy(aligned_buf, buf, len);
1290 buf += len;
1291 cnt -= len;
1292 /* push data from aligned buffer into fifo */
1293 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001294 mci_writel(host, DATA(host->data_offset),
1295 aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01001296 }
1297 } else
1298#endif
1299 {
1300 u32 *pdata = buf;
1301 for (; cnt >= 4; cnt -= 4)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001302 mci_writel(host, DATA(host->data_offset), *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01001303 buf = pdata;
1304 }
1305 /* put anything remaining in the part_buf */
1306 if (cnt) {
1307 dw_mci_set_part_bytes(host, buf, cnt);
1308 if (!sg_next(host->sg))
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001309 mci_writel(host, DATA(host->data_offset),
1310 host->part_buf32);
Will Newtonf95f3852011-01-02 01:11:59 -05001311 }
1312}
1313
1314static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
1315{
James Hogan34b664a2011-06-24 13:57:56 +01001316#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1317 if (unlikely((unsigned long)buf & 0x3)) {
1318 while (cnt >= 4) {
1319 /* pull data from fifo into aligned buffer */
1320 u32 aligned_buf[32];
1321 int len = min(cnt & -4, (int)sizeof(aligned_buf));
1322 int items = len >> 2;
1323 int i;
1324 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001325 aligned_buf[i] = mci_readl(host,
1326 DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001327 /* memcpy from aligned buffer into output buffer */
1328 memcpy(buf, aligned_buf, len);
1329 buf += len;
1330 cnt -= len;
1331 }
1332 } else
1333#endif
1334 {
1335 u32 *pdata = buf;
1336 for (; cnt >= 4; cnt -= 4)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001337 *pdata++ = mci_readl(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001338 buf = pdata;
1339 }
1340 if (cnt) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001341 host->part_buf32 = mci_readl(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001342 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05001343 }
1344}
1345
1346static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
1347{
James Hogan34b664a2011-06-24 13:57:56 +01001348 /* try and push anything in the part_buf */
1349 if (unlikely(host->part_buf_count)) {
1350 int len = dw_mci_push_part_bytes(host, buf, cnt);
1351 buf += len;
1352 cnt -= len;
1353 if (!sg_next(host->sg) || host->part_buf_count == 8) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001354 mci_writew(host, DATA(host->data_offset),
1355 host->part_buf);
James Hogan34b664a2011-06-24 13:57:56 +01001356 host->part_buf_count = 0;
1357 }
1358 }
1359#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1360 if (unlikely((unsigned long)buf & 0x7)) {
1361 while (cnt >= 8) {
1362 u64 aligned_buf[16];
1363 int len = min(cnt & -8, (int)sizeof(aligned_buf));
1364 int items = len >> 3;
1365 int i;
1366 /* memcpy from input buffer into aligned buffer */
1367 memcpy(aligned_buf, buf, len);
1368 buf += len;
1369 cnt -= len;
1370 /* push data from aligned buffer into fifo */
1371 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001372 mci_writeq(host, DATA(host->data_offset),
1373 aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01001374 }
1375 } else
1376#endif
1377 {
1378 u64 *pdata = buf;
1379 for (; cnt >= 8; cnt -= 8)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001380 mci_writeq(host, DATA(host->data_offset), *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01001381 buf = pdata;
1382 }
1383 /* put anything remaining in the part_buf */
1384 if (cnt) {
1385 dw_mci_set_part_bytes(host, buf, cnt);
1386 if (!sg_next(host->sg))
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001387 mci_writeq(host, DATA(host->data_offset),
1388 host->part_buf);
Will Newtonf95f3852011-01-02 01:11:59 -05001389 }
1390}
1391
1392static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
1393{
James Hogan34b664a2011-06-24 13:57:56 +01001394#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1395 if (unlikely((unsigned long)buf & 0x7)) {
1396 while (cnt >= 8) {
1397 /* pull data from fifo into aligned buffer */
1398 u64 aligned_buf[16];
1399 int len = min(cnt & -8, (int)sizeof(aligned_buf));
1400 int items = len >> 3;
1401 int i;
1402 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001403 aligned_buf[i] = mci_readq(host,
1404 DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001405 /* memcpy from aligned buffer into output buffer */
1406 memcpy(buf, aligned_buf, len);
1407 buf += len;
1408 cnt -= len;
1409 }
1410 } else
1411#endif
1412 {
1413 u64 *pdata = buf;
1414 for (; cnt >= 8; cnt -= 8)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001415 *pdata++ = mci_readq(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001416 buf = pdata;
Will Newtonf95f3852011-01-02 01:11:59 -05001417 }
James Hogan34b664a2011-06-24 13:57:56 +01001418 if (cnt) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001419 host->part_buf = mci_readq(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001420 dw_mci_pull_final_bytes(host, buf, cnt);
1421 }
1422}
1423
1424static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
1425{
1426 int len;
1427
1428 /* get remaining partial bytes */
1429 len = dw_mci_pull_part_bytes(host, buf, cnt);
1430 if (unlikely(len == cnt))
1431 return;
1432 buf += len;
1433 cnt -= len;
1434
1435 /* get the rest of the data */
1436 host->pull_data(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05001437}
1438
1439static void dw_mci_read_data_pio(struct dw_mci *host)
1440{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001441 struct sg_mapping_iter *sg_miter = &host->sg_miter;
1442 void *buf;
1443 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05001444 struct mmc_data *data = host->data;
1445 int shift = host->data_shift;
1446 u32 status;
Chris Ballba6a9022011-02-28 16:45:10 -05001447 unsigned int nbytes = 0, len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001448 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05001449
1450 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001451 if (!sg_miter_next(sg_miter))
1452 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05001453
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001454 host->sg = sg_miter->__sg;
1455 buf = sg_miter->addr;
1456 remain = sg_miter->length;
1457 offset = 0;
1458
1459 do {
1460 fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
1461 << shift) + host->part_buf_count;
1462 len = min(remain, fcnt);
1463 if (!len)
1464 break;
1465 dw_mci_pull_data(host, (void *)(buf + offset), len);
Will Newtonf95f3852011-01-02 01:11:59 -05001466 offset += len;
1467 nbytes += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001468 remain -= len;
1469 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05001470
Seungwon Jeone74f3a92012-08-01 09:30:46 +09001471 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05001472 status = mci_readl(host, MINTSTS);
1473 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
Will Newtonf95f3852011-01-02 01:11:59 -05001474 } while (status & SDMMC_INT_RXDR); /*if the RXDR is ready read again*/
Will Newtonf95f3852011-01-02 01:11:59 -05001475 data->bytes_xfered += nbytes;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001476
1477 if (!remain) {
1478 if (!sg_miter_next(sg_miter))
1479 goto done;
1480 sg_miter->consumed = 0;
1481 }
1482 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05001483 return;
1484
1485done:
1486 data->bytes_xfered += nbytes;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001487 sg_miter_stop(sg_miter);
1488 host->sg = NULL;
Will Newtonf95f3852011-01-02 01:11:59 -05001489 smp_wmb();
1490 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
1491}
1492
1493static void dw_mci_write_data_pio(struct dw_mci *host)
1494{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001495 struct sg_mapping_iter *sg_miter = &host->sg_miter;
1496 void *buf;
1497 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05001498 struct mmc_data *data = host->data;
1499 int shift = host->data_shift;
1500 u32 status;
1501 unsigned int nbytes = 0, len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001502 unsigned int fifo_depth = host->fifo_depth;
1503 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05001504
1505 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001506 if (!sg_miter_next(sg_miter))
1507 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05001508
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001509 host->sg = sg_miter->__sg;
1510 buf = sg_miter->addr;
1511 remain = sg_miter->length;
1512 offset = 0;
1513
1514 do {
1515 fcnt = ((fifo_depth -
1516 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
1517 << shift) - host->part_buf_count;
1518 len = min(remain, fcnt);
1519 if (!len)
1520 break;
1521 host->push_data(host, (void *)(buf + offset), len);
Will Newtonf95f3852011-01-02 01:11:59 -05001522 offset += len;
1523 nbytes += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001524 remain -= len;
1525 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05001526
Seungwon Jeone74f3a92012-08-01 09:30:46 +09001527 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05001528 status = mci_readl(host, MINTSTS);
1529 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
Will Newtonf95f3852011-01-02 01:11:59 -05001530 } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
Will Newtonf95f3852011-01-02 01:11:59 -05001531 data->bytes_xfered += nbytes;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001532
1533 if (!remain) {
1534 if (!sg_miter_next(sg_miter))
1535 goto done;
1536 sg_miter->consumed = 0;
1537 }
1538 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05001539 return;
1540
1541done:
1542 data->bytes_xfered += nbytes;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001543 sg_miter_stop(sg_miter);
1544 host->sg = NULL;
Will Newtonf95f3852011-01-02 01:11:59 -05001545 smp_wmb();
1546 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
1547}
1548
1549static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
1550{
1551 if (!host->cmd_status)
1552 host->cmd_status = status;
1553
1554 smp_wmb();
1555
1556 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
1557 tasklet_schedule(&host->tasklet);
1558}
1559
1560static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
1561{
1562 struct dw_mci *host = dev_id;
Seungwon Jeon182c9082012-08-01 09:30:30 +09001563 u32 pending;
Will Newtonf95f3852011-01-02 01:11:59 -05001564 unsigned int pass_count = 0;
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301565 int i;
Will Newtonf95f3852011-01-02 01:11:59 -05001566
1567 do {
Will Newtonf95f3852011-01-02 01:11:59 -05001568 pending = mci_readl(host, MINTSTS); /* read-only mask reg */
1569
1570 /*
1571 * DTO fix - version 2.10a and below, and only if internal DMA
1572 * is configured.
1573 */
1574 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
1575 if (!pending &&
1576 ((mci_readl(host, STATUS) >> 17) & 0x1fff))
1577 pending |= SDMMC_INT_DATA_OVER;
1578 }
1579
1580 if (!pending)
1581 break;
1582
1583 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
1584 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09001585 host->cmd_status = pending;
Will Newtonf95f3852011-01-02 01:11:59 -05001586 smp_wmb();
1587 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -05001588 }
1589
1590 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
1591 /* if there is an error report DATA_ERROR */
1592 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09001593 host->data_status = pending;
Will Newtonf95f3852011-01-02 01:11:59 -05001594 smp_wmb();
1595 set_bit(EVENT_DATA_ERROR, &host->pending_events);
Seungwon Jeon9b2026a2012-08-01 09:30:40 +09001596 tasklet_schedule(&host->tasklet);
Will Newtonf95f3852011-01-02 01:11:59 -05001597 }
1598
1599 if (pending & SDMMC_INT_DATA_OVER) {
1600 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
1601 if (!host->data_status)
Seungwon Jeon182c9082012-08-01 09:30:30 +09001602 host->data_status = pending;
Will Newtonf95f3852011-01-02 01:11:59 -05001603 smp_wmb();
1604 if (host->dir_status == DW_MCI_RECV_STATUS) {
1605 if (host->sg != NULL)
1606 dw_mci_read_data_pio(host);
1607 }
1608 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
1609 tasklet_schedule(&host->tasklet);
1610 }
1611
1612 if (pending & SDMMC_INT_RXDR) {
1613 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01001614 if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
Will Newtonf95f3852011-01-02 01:11:59 -05001615 dw_mci_read_data_pio(host);
1616 }
1617
1618 if (pending & SDMMC_INT_TXDR) {
1619 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01001620 if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
Will Newtonf95f3852011-01-02 01:11:59 -05001621 dw_mci_write_data_pio(host);
1622 }
1623
1624 if (pending & SDMMC_INT_CMD_DONE) {
1625 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
Seungwon Jeon182c9082012-08-01 09:30:30 +09001626 dw_mci_cmd_interrupt(host, pending);
Will Newtonf95f3852011-01-02 01:11:59 -05001627 }
1628
1629 if (pending & SDMMC_INT_CD) {
1630 mci_writel(host, RINTSTS, SDMMC_INT_CD);
Thomas Abraham95dcc2c2012-05-01 14:57:36 -07001631 queue_work(host->card_workqueue, &host->card_work);
Will Newtonf95f3852011-01-02 01:11:59 -05001632 }
1633
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301634 /* Handle SDIO Interrupts */
1635 for (i = 0; i < host->num_slots; i++) {
1636 struct dw_mci_slot *slot = host->slot[i];
1637 if (pending & SDMMC_INT_SDIO(i)) {
1638 mci_writel(host, RINTSTS, SDMMC_INT_SDIO(i));
1639 mmc_signal_sdio_irq(slot->mmc);
1640 }
1641 }
1642
Will Newtonf95f3852011-01-02 01:11:59 -05001643 } while (pass_count++ < 5);
1644
1645#ifdef CONFIG_MMC_DW_IDMAC
1646 /* Handle DMA interrupts */
1647 pending = mci_readl(host, IDSTS);
1648 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
1649 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI);
1650 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
Will Newtonf95f3852011-01-02 01:11:59 -05001651 host->dma_ops->complete(host);
1652 }
1653#endif
1654
1655 return IRQ_HANDLED;
1656}
1657
James Hogan1791b13e2011-06-24 13:55:55 +01001658static void dw_mci_work_routine_card(struct work_struct *work)
Will Newtonf95f3852011-01-02 01:11:59 -05001659{
James Hogan1791b13e2011-06-24 13:55:55 +01001660 struct dw_mci *host = container_of(work, struct dw_mci, card_work);
Will Newtonf95f3852011-01-02 01:11:59 -05001661 int i;
1662
1663 for (i = 0; i < host->num_slots; i++) {
1664 struct dw_mci_slot *slot = host->slot[i];
1665 struct mmc_host *mmc = slot->mmc;
1666 struct mmc_request *mrq;
1667 int present;
1668 u32 ctrl;
1669
1670 present = dw_mci_get_cd(mmc);
1671 while (present != slot->last_detect_state) {
Will Newtonf95f3852011-01-02 01:11:59 -05001672 dev_dbg(&slot->mmc->class_dev, "card %s\n",
1673 present ? "inserted" : "removed");
1674
James Hogan1791b13e2011-06-24 13:55:55 +01001675 /* Power up slot (before spin_lock, may sleep) */
1676 if (present != 0 && host->pdata->setpower)
1677 host->pdata->setpower(slot->id, mmc->ocr_avail);
1678
1679 spin_lock_bh(&host->lock);
1680
Will Newtonf95f3852011-01-02 01:11:59 -05001681 /* Card change detected */
1682 slot->last_detect_state = present;
1683
James Hogan1791b13e2011-06-24 13:55:55 +01001684 /* Mark card as present if applicable */
1685 if (present != 0)
Will Newtonf95f3852011-01-02 01:11:59 -05001686 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
Will Newtonf95f3852011-01-02 01:11:59 -05001687
1688 /* Clean up queue if present */
1689 mrq = slot->mrq;
1690 if (mrq) {
1691 if (mrq == host->mrq) {
1692 host->data = NULL;
1693 host->cmd = NULL;
1694
1695 switch (host->state) {
1696 case STATE_IDLE:
1697 break;
1698 case STATE_SENDING_CMD:
1699 mrq->cmd->error = -ENOMEDIUM;
1700 if (!mrq->data)
1701 break;
1702 /* fall through */
1703 case STATE_SENDING_DATA:
1704 mrq->data->error = -ENOMEDIUM;
1705 dw_mci_stop_dma(host);
1706 break;
1707 case STATE_DATA_BUSY:
1708 case STATE_DATA_ERROR:
1709 if (mrq->data->error == -EINPROGRESS)
1710 mrq->data->error = -ENOMEDIUM;
1711 if (!mrq->stop)
1712 break;
1713 /* fall through */
1714 case STATE_SENDING_STOP:
1715 mrq->stop->error = -ENOMEDIUM;
1716 break;
1717 }
1718
1719 dw_mci_request_end(host, mrq);
1720 } else {
1721 list_del(&slot->queue_node);
1722 mrq->cmd->error = -ENOMEDIUM;
1723 if (mrq->data)
1724 mrq->data->error = -ENOMEDIUM;
1725 if (mrq->stop)
1726 mrq->stop->error = -ENOMEDIUM;
1727
1728 spin_unlock(&host->lock);
1729 mmc_request_done(slot->mmc, mrq);
1730 spin_lock(&host->lock);
1731 }
1732 }
1733
1734 /* Power down slot */
1735 if (present == 0) {
Will Newtonf95f3852011-01-02 01:11:59 -05001736 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1737
1738 /*
1739 * Clear down the FIFO - doing so generates a
1740 * block interrupt, hence setting the
1741 * scatter-gather pointer to NULL.
1742 */
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001743 sg_miter_stop(&host->sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05001744 host->sg = NULL;
1745
1746 ctrl = mci_readl(host, CTRL);
1747 ctrl |= SDMMC_CTRL_FIFO_RESET;
1748 mci_writel(host, CTRL, ctrl);
1749
1750#ifdef CONFIG_MMC_DW_IDMAC
1751 ctrl = mci_readl(host, BMOD);
Seungwon Jeon141a7122012-05-22 13:01:03 +09001752 /* Software reset of DMA */
1753 ctrl |= SDMMC_IDMAC_SWRESET;
Will Newtonf95f3852011-01-02 01:11:59 -05001754 mci_writel(host, BMOD, ctrl);
1755#endif
1756
1757 }
1758
James Hogan1791b13e2011-06-24 13:55:55 +01001759 spin_unlock_bh(&host->lock);
1760
1761 /* Power down slot (after spin_unlock, may sleep) */
1762 if (present == 0 && host->pdata->setpower)
1763 host->pdata->setpower(slot->id, 0);
1764
Will Newtonf95f3852011-01-02 01:11:59 -05001765 present = dw_mci_get_cd(mmc);
1766 }
1767
1768 mmc_detect_change(slot->mmc,
1769 msecs_to_jiffies(host->pdata->detect_delay_ms));
1770 }
1771}
1772
Thomas Abrahamc91eab42012-09-17 18:16:40 +00001773#ifdef CONFIG_OF
1774/* given a slot id, find out the device node representing that slot */
1775static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
1776{
1777 struct device_node *np;
1778 const __be32 *addr;
1779 int len;
1780
1781 if (!dev || !dev->of_node)
1782 return NULL;
1783
1784 for_each_child_of_node(dev->of_node, np) {
1785 addr = of_get_property(np, "reg", &len);
1786 if (!addr || (len < sizeof(int)))
1787 continue;
1788 if (be32_to_cpup(addr) == slot)
1789 return np;
1790 }
1791 return NULL;
1792}
1793
1794/* find out bus-width for a given slot */
1795static u32 dw_mci_of_get_bus_wd(struct device *dev, u8 slot)
1796{
1797 struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
1798 u32 bus_wd = 1;
1799
1800 if (!np)
1801 return 1;
1802
1803 if (of_property_read_u32(np, "bus-width", &bus_wd))
1804 dev_err(dev, "bus-width property not found, assuming width"
1805 " as 1\n");
1806 return bus_wd;
1807}
1808#else /* CONFIG_OF */
1809static u32 dw_mci_of_get_bus_wd(struct device *dev, u8 slot)
1810{
1811 return 1;
1812}
1813static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
1814{
1815 return NULL;
1816}
1817#endif /* CONFIG_OF */
1818
Jaehoon Chung36c179a2012-08-23 20:31:48 +09001819static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
Will Newtonf95f3852011-01-02 01:11:59 -05001820{
1821 struct mmc_host *mmc;
1822 struct dw_mci_slot *slot;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00001823 u8 bus_width;
Will Newtonf95f3852011-01-02 01:11:59 -05001824
Thomas Abraham4a909202012-09-17 18:16:35 +00001825 mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
Will Newtonf95f3852011-01-02 01:11:59 -05001826 if (!mmc)
1827 return -ENOMEM;
1828
1829 slot = mmc_priv(mmc);
1830 slot->id = id;
1831 slot->mmc = mmc;
1832 slot->host = host;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00001833 host->slot[id] = slot;
Will Newtonf95f3852011-01-02 01:11:59 -05001834
1835 mmc->ops = &dw_mci_ops;
1836 mmc->f_min = DIV_ROUND_UP(host->bus_hz, 510);
1837 mmc->f_max = host->bus_hz;
1838
1839 if (host->pdata->get_ocr)
1840 mmc->ocr_avail = host->pdata->get_ocr(id);
1841 else
1842 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1843
1844 /*
1845 * Start with slot power disabled, it will be enabled when a card
1846 * is detected.
1847 */
1848 if (host->pdata->setpower)
1849 host->pdata->setpower(id, 0);
1850
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09001851 if (host->pdata->caps)
1852 mmc->caps = host->pdata->caps;
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09001853
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09001854 if (host->pdata->caps2)
1855 mmc->caps2 = host->pdata->caps2;
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09001856
Will Newtonf95f3852011-01-02 01:11:59 -05001857 if (host->pdata->get_bus_wd)
Thomas Abrahamc91eab42012-09-17 18:16:40 +00001858 bus_width = host->pdata->get_bus_wd(slot->id);
1859 else if (host->dev->of_node)
1860 bus_width = dw_mci_of_get_bus_wd(host->dev, slot->id);
1861 else
1862 bus_width = 1;
1863
1864 switch (bus_width) {
1865 case 8:
1866 mmc->caps |= MMC_CAP_8_BIT_DATA;
1867 case 4:
1868 mmc->caps |= MMC_CAP_4_BIT_DATA;
1869 }
Will Newtonf95f3852011-01-02 01:11:59 -05001870
1871 if (host->pdata->quirks & DW_MCI_QUIRK_HIGHSPEED)
Seungwon Jeon6daa7772011-08-05 12:35:03 +09001872 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Will Newtonf95f3852011-01-02 01:11:59 -05001873
Jaehoon Chung356ac2c2012-01-13 17:31:32 +09001874 if (mmc->caps2 & MMC_CAP2_POWEROFF_NOTIFY)
1875 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
1876 else
1877 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_NONE;
1878
Will Newtonf95f3852011-01-02 01:11:59 -05001879 if (host->pdata->blk_settings) {
1880 mmc->max_segs = host->pdata->blk_settings->max_segs;
1881 mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
1882 mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
1883 mmc->max_req_size = host->pdata->blk_settings->max_req_size;
1884 mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
1885 } else {
1886 /* Useful defaults if platform data is unset. */
Jaehoon Chunga39e5742012-02-04 17:00:27 -05001887#ifdef CONFIG_MMC_DW_IDMAC
1888 mmc->max_segs = host->ring_size;
1889 mmc->max_blk_size = 65536;
1890 mmc->max_blk_count = host->ring_size;
1891 mmc->max_seg_size = 0x1000;
1892 mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count;
1893#else
Will Newtonf95f3852011-01-02 01:11:59 -05001894 mmc->max_segs = 64;
1895 mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
1896 mmc->max_blk_count = 512;
1897 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1898 mmc->max_seg_size = mmc->max_req_size;
Will Newtonf95f3852011-01-02 01:11:59 -05001899#endif /* CONFIG_MMC_DW_IDMAC */
Jaehoon Chunga39e5742012-02-04 17:00:27 -05001900 }
Will Newtonf95f3852011-01-02 01:11:59 -05001901
Jaehoon Chungc07946a2011-02-25 11:08:14 +09001902 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
1903 if (IS_ERR(host->vmmc)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301904 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Jaehoon Chungc07946a2011-02-25 11:08:14 +09001905 host->vmmc = NULL;
1906 } else
1907 regulator_enable(host->vmmc);
1908
Will Newtonf95f3852011-01-02 01:11:59 -05001909 if (dw_mci_get_cd(mmc))
1910 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1911 else
1912 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1913
Will Newtonf95f3852011-01-02 01:11:59 -05001914 mmc_add_host(mmc);
1915
1916#if defined(CONFIG_DEBUG_FS)
1917 dw_mci_init_debugfs(slot);
1918#endif
1919
1920 /* Card initially undetected */
1921 slot->last_detect_state = 0;
1922
Will Newtondd6c4b92011-02-10 14:37:03 -05001923 /*
1924 * Card may have been plugged in prior to boot so we
1925 * need to run the detect tasklet
1926 */
Thomas Abraham95dcc2c2012-05-01 14:57:36 -07001927 queue_work(host->card_workqueue, &host->card_work);
Will Newtondd6c4b92011-02-10 14:37:03 -05001928
Will Newtonf95f3852011-01-02 01:11:59 -05001929 return 0;
1930}
1931
1932static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
1933{
1934 /* Shutdown detect IRQ */
1935 if (slot->host->pdata->exit)
1936 slot->host->pdata->exit(id);
1937
1938 /* Debugfs stuff is cleaned up by mmc core */
1939 mmc_remove_host(slot->mmc);
1940 slot->host->slot[id] = NULL;
1941 mmc_free_host(slot->mmc);
1942}
1943
1944static void dw_mci_init_dma(struct dw_mci *host)
1945{
1946 /* Alloc memory for sg translation */
Thomas Abraham4a909202012-09-17 18:16:35 +00001947 host->sg_cpu = dma_alloc_coherent(host->dev, PAGE_SIZE,
Will Newtonf95f3852011-01-02 01:11:59 -05001948 &host->sg_dma, GFP_KERNEL);
1949 if (!host->sg_cpu) {
Thomas Abraham4a909202012-09-17 18:16:35 +00001950 dev_err(host->dev, "%s: could not alloc DMA memory\n",
Will Newtonf95f3852011-01-02 01:11:59 -05001951 __func__);
1952 goto no_dma;
1953 }
1954
1955 /* Determine which DMA interface to use */
1956#ifdef CONFIG_MMC_DW_IDMAC
1957 host->dma_ops = &dw_mci_idmac_ops;
Will Newtonf95f3852011-01-02 01:11:59 -05001958#endif
1959
1960 if (!host->dma_ops)
1961 goto no_dma;
1962
Jaehoon Chunge1631f92012-04-18 15:42:31 +09001963 if (host->dma_ops->init && host->dma_ops->start &&
1964 host->dma_ops->stop && host->dma_ops->cleanup) {
Will Newtonf95f3852011-01-02 01:11:59 -05001965 if (host->dma_ops->init(host)) {
Thomas Abraham4a909202012-09-17 18:16:35 +00001966 dev_err(host->dev, "%s: Unable to initialize "
Will Newtonf95f3852011-01-02 01:11:59 -05001967 "DMA Controller.\n", __func__);
1968 goto no_dma;
1969 }
1970 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00001971 dev_err(host->dev, "DMA initialization not found.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05001972 goto no_dma;
1973 }
1974
1975 host->use_dma = 1;
1976 return;
1977
1978no_dma:
Thomas Abraham4a909202012-09-17 18:16:35 +00001979 dev_info(host->dev, "Using PIO mode.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05001980 host->use_dma = 0;
1981 return;
1982}
1983
1984static bool mci_wait_reset(struct device *dev, struct dw_mci *host)
1985{
1986 unsigned long timeout = jiffies + msecs_to_jiffies(500);
1987 unsigned int ctrl;
1988
1989 mci_writel(host, CTRL, (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
1990 SDMMC_CTRL_DMA_RESET));
1991
1992 /* wait till resets clear */
1993 do {
1994 ctrl = mci_readl(host, CTRL);
1995 if (!(ctrl & (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
1996 SDMMC_CTRL_DMA_RESET)))
1997 return true;
1998 } while (time_before(jiffies, timeout));
1999
2000 dev_err(dev, "Timeout resetting block (ctrl %#x)\n", ctrl);
2001
2002 return false;
2003}
2004
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002005#ifdef CONFIG_OF
2006static struct dw_mci_of_quirks {
2007 char *quirk;
2008 int id;
2009} of_quirks[] = {
2010 {
2011 .quirk = "supports-highspeed",
2012 .id = DW_MCI_QUIRK_HIGHSPEED,
2013 }, {
2014 .quirk = "broken-cd",
2015 .id = DW_MCI_QUIRK_BROKEN_CARD_DETECTION,
2016 },
2017};
2018
2019static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2020{
2021 struct dw_mci_board *pdata;
2022 struct device *dev = host->dev;
2023 struct device_node *np = dev->of_node;
2024 int idx;
2025
2026 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2027 if (!pdata) {
2028 dev_err(dev, "could not allocate memory for pdata\n");
2029 return ERR_PTR(-ENOMEM);
2030 }
2031
2032 /* find out number of slots supported */
2033 if (of_property_read_u32(dev->of_node, "num-slots",
2034 &pdata->num_slots)) {
2035 dev_info(dev, "num-slots property not found, "
2036 "assuming 1 slot is available\n");
2037 pdata->num_slots = 1;
2038 }
2039
2040 /* get quirks */
2041 for (idx = 0; idx < ARRAY_SIZE(of_quirks); idx++)
2042 if (of_get_property(np, of_quirks[idx].quirk, NULL))
2043 pdata->quirks |= of_quirks[idx].id;
2044
2045 if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
2046 dev_info(dev, "fifo-depth property not found, using "
2047 "value of FIFOTH register as default\n");
2048
2049 of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
2050
2051 return pdata;
2052}
2053
2054#else /* CONFIG_OF */
2055static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2056{
2057 return ERR_PTR(-EINVAL);
2058}
2059#endif /* CONFIG_OF */
2060
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302061int dw_mci_probe(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002062{
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302063 int width, i, ret = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002064 u32 fifo_size;
Thomas Abraham1c2215b2012-09-17 18:16:37 +00002065 int init_slots = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002066
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002067 if (!host->pdata) {
2068 host->pdata = dw_mci_parse_dt(host);
2069 if (IS_ERR(host->pdata)) {
2070 dev_err(host->dev, "platform data not available\n");
2071 return -EINVAL;
2072 }
Will Newtonf95f3852011-01-02 01:11:59 -05002073 }
2074
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302075 if (!host->pdata->select_slot && host->pdata->num_slots > 1) {
Thomas Abraham4a909202012-09-17 18:16:35 +00002076 dev_err(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -05002077 "Platform data must supply select_slot function\n");
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302078 return -ENODEV;
Will Newtonf95f3852011-01-02 01:11:59 -05002079 }
2080
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002081 host->biu_clk = clk_get(host->dev, "biu");
2082 if (IS_ERR(host->biu_clk)) {
2083 dev_dbg(host->dev, "biu clock not available\n");
2084 } else {
2085 ret = clk_prepare_enable(host->biu_clk);
2086 if (ret) {
2087 dev_err(host->dev, "failed to enable biu clock\n");
2088 clk_put(host->biu_clk);
2089 return ret;
2090 }
Will Newtonf95f3852011-01-02 01:11:59 -05002091 }
2092
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002093 host->ciu_clk = clk_get(host->dev, "ciu");
2094 if (IS_ERR(host->ciu_clk)) {
2095 dev_dbg(host->dev, "ciu clock not available\n");
2096 } else {
2097 ret = clk_prepare_enable(host->ciu_clk);
2098 if (ret) {
2099 dev_err(host->dev, "failed to enable ciu clock\n");
2100 clk_put(host->ciu_clk);
2101 goto err_clk_biu;
2102 }
2103 }
2104
2105 if (IS_ERR(host->ciu_clk))
2106 host->bus_hz = host->pdata->bus_hz;
2107 else
2108 host->bus_hz = clk_get_rate(host->ciu_clk);
2109
2110 if (!host->bus_hz) {
2111 dev_err(host->dev,
2112 "Platform data must supply bus speed\n");
2113 ret = -ENODEV;
2114 goto err_clk_ciu;
2115 }
2116
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302117 host->quirks = host->pdata->quirks;
Will Newtonf95f3852011-01-02 01:11:59 -05002118
2119 spin_lock_init(&host->lock);
2120 INIT_LIST_HEAD(&host->queue);
2121
Will Newtonf95f3852011-01-02 01:11:59 -05002122 /*
2123 * Get the host data width - this assumes that HCON has been set with
2124 * the correct values.
2125 */
2126 i = (mci_readl(host, HCON) >> 7) & 0x7;
2127 if (!i) {
2128 host->push_data = dw_mci_push_data16;
2129 host->pull_data = dw_mci_pull_data16;
2130 width = 16;
2131 host->data_shift = 1;
2132 } else if (i == 2) {
2133 host->push_data = dw_mci_push_data64;
2134 host->pull_data = dw_mci_pull_data64;
2135 width = 64;
2136 host->data_shift = 3;
2137 } else {
2138 /* Check for a reserved value, and warn if it is */
2139 WARN((i != 1),
2140 "HCON reports a reserved host data width!\n"
2141 "Defaulting to 32-bit access.\n");
2142 host->push_data = dw_mci_push_data32;
2143 host->pull_data = dw_mci_pull_data32;
2144 width = 32;
2145 host->data_shift = 2;
2146 }
2147
2148 /* Reset all blocks */
Thomas Abraham4a909202012-09-17 18:16:35 +00002149 if (!mci_wait_reset(host->dev, host))
Seungwon Jeon141a7122012-05-22 13:01:03 +09002150 return -ENODEV;
2151
2152 host->dma_ops = host->pdata->dma_ops;
2153 dw_mci_init_dma(host);
Will Newtonf95f3852011-01-02 01:11:59 -05002154
2155 /* Clear the interrupts for the host controller */
2156 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2157 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
2158
2159 /* Put in max timeout */
2160 mci_writel(host, TMOUT, 0xFFFFFFFF);
2161
2162 /*
2163 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
2164 * Tx Mark = fifo_size / 2 DMA Size = 8
2165 */
James Hoganb86d8252011-06-24 13:57:18 +01002166 if (!host->pdata->fifo_depth) {
2167 /*
2168 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
2169 * have been overwritten by the bootloader, just like we're
2170 * about to do, so if you know the value for your hardware, you
2171 * should put it in the platform data.
2172 */
2173 fifo_size = mci_readl(host, FIFOTH);
Jaehoon Chung8234e862012-01-11 09:28:21 +00002174 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
James Hoganb86d8252011-06-24 13:57:18 +01002175 } else {
2176 fifo_size = host->pdata->fifo_depth;
2177 }
2178 host->fifo_depth = fifo_size;
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002179 host->fifoth_val = ((0x2 << 28) | ((fifo_size/2 - 1) << 16) |
2180 ((fifo_size/2) << 0));
2181 mci_writel(host, FIFOTH, host->fifoth_val);
Will Newtonf95f3852011-01-02 01:11:59 -05002182
2183 /* disable clock to CIU */
2184 mci_writel(host, CLKENA, 0);
2185 mci_writel(host, CLKSRC, 0);
2186
2187 tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
Thomas Abraham95dcc2c2012-05-01 14:57:36 -07002188 host->card_workqueue = alloc_workqueue("dw-mci-card",
James Hogan1791b13e2011-06-24 13:55:55 +01002189 WQ_MEM_RECLAIM | WQ_NON_REENTRANT, 1);
Thomas Abraham95dcc2c2012-05-01 14:57:36 -07002190 if (!host->card_workqueue)
James Hogan1791b13e2011-06-24 13:55:55 +01002191 goto err_dmaunmap;
2192 INIT_WORK(&host->card_work, dw_mci_work_routine_card);
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302193 ret = request_irq(host->irq, dw_mci_interrupt, host->irq_flags, "dw-mci", host);
Will Newtonf95f3852011-01-02 01:11:59 -05002194 if (ret)
James Hogan1791b13e2011-06-24 13:55:55 +01002195 goto err_workqueue;
Will Newtonf95f3852011-01-02 01:11:59 -05002196
Will Newtonf95f3852011-01-02 01:11:59 -05002197 if (host->pdata->num_slots)
2198 host->num_slots = host->pdata->num_slots;
2199 else
2200 host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
2201
2202 /* We need at least one slot to succeed */
2203 for (i = 0; i < host->num_slots; i++) {
2204 ret = dw_mci_init_slot(host, i);
Thomas Abraham1c2215b2012-09-17 18:16:37 +00002205 if (ret)
2206 dev_dbg(host->dev, "slot %d init failed\n", i);
2207 else
2208 init_slots++;
2209 }
2210
2211 if (init_slots) {
2212 dev_info(host->dev, "%d slots initialized\n", init_slots);
2213 } else {
2214 dev_dbg(host->dev, "attempted to initialize %d slots, "
2215 "but failed on all\n", host->num_slots);
2216 goto err_init_slot;
Will Newtonf95f3852011-01-02 01:11:59 -05002217 }
2218
2219 /*
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09002220 * In 2.40a spec, Data offset is changed.
2221 * Need to check the version-id and set data-offset for DATA register.
2222 */
2223 host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
Thomas Abraham4a909202012-09-17 18:16:35 +00002224 dev_info(host->dev, "Version ID is %04x\n", host->verid);
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09002225
2226 if (host->verid < DW_MMC_240A)
2227 host->data_offset = DATA_OFFSET;
2228 else
2229 host->data_offset = DATA_240A_OFFSET;
2230
2231 /*
Will Newtonf95f3852011-01-02 01:11:59 -05002232 * Enable interrupts for command done, data over, data empty, card det,
2233 * receive ready and error such as transmit, receive timeout, crc error
2234 */
2235 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2236 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
2237 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
2238 DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
2239 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
2240
Thomas Abraham4a909202012-09-17 18:16:35 +00002241 dev_info(host->dev, "DW MMC controller at irq %d, "
James Hoganb86d8252011-06-24 13:57:18 +01002242 "%d bit host data width, "
2243 "%u deep fifo\n",
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302244 host->irq, width, fifo_size);
Will Newtonf95f3852011-01-02 01:11:59 -05002245 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
Thomas Abraham4a909202012-09-17 18:16:35 +00002246 dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002247
2248 return 0;
2249
2250err_init_slot:
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302251 free_irq(host->irq, host);
Will Newtonf95f3852011-01-02 01:11:59 -05002252
James Hogan1791b13e2011-06-24 13:55:55 +01002253err_workqueue:
Thomas Abraham95dcc2c2012-05-01 14:57:36 -07002254 destroy_workqueue(host->card_workqueue);
James Hogan1791b13e2011-06-24 13:55:55 +01002255
Will Newtonf95f3852011-01-02 01:11:59 -05002256err_dmaunmap:
2257 if (host->use_dma && host->dma_ops->exit)
2258 host->dma_ops->exit(host);
Thomas Abraham4a909202012-09-17 18:16:35 +00002259 dma_free_coherent(host->dev, PAGE_SIZE,
Will Newtonf95f3852011-01-02 01:11:59 -05002260 host->sg_cpu, host->sg_dma);
Will Newtonf95f3852011-01-02 01:11:59 -05002261
Jaehoon Chungc07946a2011-02-25 11:08:14 +09002262 if (host->vmmc) {
2263 regulator_disable(host->vmmc);
2264 regulator_put(host->vmmc);
2265 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002266
2267err_clk_ciu:
2268 if (!IS_ERR(host->ciu_clk)) {
2269 clk_disable_unprepare(host->ciu_clk);
2270 clk_put(host->ciu_clk);
2271 }
2272err_clk_biu:
2273 if (!IS_ERR(host->biu_clk)) {
2274 clk_disable_unprepare(host->biu_clk);
2275 clk_put(host->biu_clk);
2276 }
Will Newtonf95f3852011-01-02 01:11:59 -05002277 return ret;
2278}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302279EXPORT_SYMBOL(dw_mci_probe);
Will Newtonf95f3852011-01-02 01:11:59 -05002280
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302281void dw_mci_remove(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002282{
Will Newtonf95f3852011-01-02 01:11:59 -05002283 int i;
2284
2285 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2286 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
2287
Will Newtonf95f3852011-01-02 01:11:59 -05002288 for (i = 0; i < host->num_slots; i++) {
Thomas Abraham4a909202012-09-17 18:16:35 +00002289 dev_dbg(host->dev, "remove slot %d\n", i);
Will Newtonf95f3852011-01-02 01:11:59 -05002290 if (host->slot[i])
2291 dw_mci_cleanup_slot(host->slot[i], i);
2292 }
2293
2294 /* disable clock to CIU */
2295 mci_writel(host, CLKENA, 0);
2296 mci_writel(host, CLKSRC, 0);
2297
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302298 free_irq(host->irq, host);
Thomas Abraham95dcc2c2012-05-01 14:57:36 -07002299 destroy_workqueue(host->card_workqueue);
Thomas Abraham4a909202012-09-17 18:16:35 +00002300 dma_free_coherent(host->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
Will Newtonf95f3852011-01-02 01:11:59 -05002301
2302 if (host->use_dma && host->dma_ops->exit)
2303 host->dma_ops->exit(host);
2304
Jaehoon Chungc07946a2011-02-25 11:08:14 +09002305 if (host->vmmc) {
2306 regulator_disable(host->vmmc);
2307 regulator_put(host->vmmc);
2308 }
2309
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002310 if (!IS_ERR(host->ciu_clk))
2311 clk_disable_unprepare(host->ciu_clk);
2312 if (!IS_ERR(host->biu_clk))
2313 clk_disable_unprepare(host->biu_clk);
2314 clk_put(host->ciu_clk);
2315 clk_put(host->biu_clk);
Will Newtonf95f3852011-01-02 01:11:59 -05002316}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302317EXPORT_SYMBOL(dw_mci_remove);
2318
2319
Will Newtonf95f3852011-01-02 01:11:59 -05002320
Jaehoon Chung6fe88902011-12-08 19:23:03 +09002321#ifdef CONFIG_PM_SLEEP
Will Newtonf95f3852011-01-02 01:11:59 -05002322/*
2323 * TODO: we should probably disable the clock to the card in the suspend path.
2324 */
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302325int dw_mci_suspend(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002326{
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302327 int i, ret = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002328
2329 for (i = 0; i < host->num_slots; i++) {
2330 struct dw_mci_slot *slot = host->slot[i];
2331 if (!slot)
2332 continue;
2333 ret = mmc_suspend_host(slot->mmc);
2334 if (ret < 0) {
2335 while (--i >= 0) {
2336 slot = host->slot[i];
2337 if (slot)
2338 mmc_resume_host(host->slot[i]->mmc);
2339 }
2340 return ret;
2341 }
2342 }
2343
Jaehoon Chungc07946a2011-02-25 11:08:14 +09002344 if (host->vmmc)
2345 regulator_disable(host->vmmc);
2346
Will Newtonf95f3852011-01-02 01:11:59 -05002347 return 0;
2348}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302349EXPORT_SYMBOL(dw_mci_suspend);
Will Newtonf95f3852011-01-02 01:11:59 -05002350
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302351int dw_mci_resume(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002352{
2353 int i, ret;
Will Newtonf95f3852011-01-02 01:11:59 -05002354
Jaehoon Chung1d6c4e02011-05-11 15:52:39 +09002355 if (host->vmmc)
2356 regulator_enable(host->vmmc);
2357
Thomas Abraham4a909202012-09-17 18:16:35 +00002358 if (!mci_wait_reset(host->dev, host)) {
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002359 ret = -ENODEV;
2360 return ret;
2361 }
2362
Jonathan Kliegman3bfe6192012-06-14 13:31:55 -04002363 if (host->use_dma && host->dma_ops->init)
Seungwon Jeon141a7122012-05-22 13:01:03 +09002364 host->dma_ops->init(host);
2365
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002366 /* Restore the old value at FIFOTH register */
2367 mci_writel(host, FIFOTH, host->fifoth_val);
2368
2369 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2370 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
2371 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
2372 DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
2373 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
2374
Will Newtonf95f3852011-01-02 01:11:59 -05002375 for (i = 0; i < host->num_slots; i++) {
2376 struct dw_mci_slot *slot = host->slot[i];
2377 if (!slot)
2378 continue;
2379 ret = mmc_resume_host(host->slot[i]->mmc);
2380 if (ret < 0)
2381 return ret;
2382 }
Will Newtonf95f3852011-01-02 01:11:59 -05002383 return 0;
2384}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302385EXPORT_SYMBOL(dw_mci_resume);
Jaehoon Chung6fe88902011-12-08 19:23:03 +09002386#endif /* CONFIG_PM_SLEEP */
2387
Will Newtonf95f3852011-01-02 01:11:59 -05002388static int __init dw_mci_init(void)
2389{
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302390 printk(KERN_INFO "Synopsys Designware Multimedia Card Interface Driver");
2391 return 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002392}
2393
2394static void __exit dw_mci_exit(void)
2395{
Will Newtonf95f3852011-01-02 01:11:59 -05002396}
2397
2398module_init(dw_mci_init);
2399module_exit(dw_mci_exit);
2400
2401MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
2402MODULE_AUTHOR("NXP Semiconductor VietNam");
2403MODULE_AUTHOR("Imagination Technologies Ltd");
2404MODULE_LICENSE("GPL v2");