blob: fcff3c042f69466ea4889912db176dd88c7c1f21 [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>
25#include <linux/scatterlist.h>
26#include <linux/seq_file.h>
27#include <linux/slab.h>
28#include <linux/stat.h>
29#include <linux/delay.h>
30#include <linux/irq.h>
31#include <linux/mmc/host.h>
32#include <linux/mmc/mmc.h>
33#include <linux/mmc/dw_mmc.h>
34#include <linux/bitops.h>
Jaehoon Chungc07946a2011-02-25 11:08:14 +090035#include <linux/regulator/consumer.h>
James Hogan1791b13e2011-06-24 13:55:55 +010036#include <linux/workqueue.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) \
65 ((d)->des1 = ((d)->des1 & 0x03ffc000) | ((s) & 0x3fff))
66
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
James Hogan1791b13e2011-06-24 13:55:55 +0100104static struct workqueue_struct *dw_mci_card_workqueue;
105
Will Newtonf95f3852011-01-02 01:11:59 -0500106#if defined(CONFIG_DEBUG_FS)
107static int dw_mci_req_show(struct seq_file *s, void *v)
108{
109 struct dw_mci_slot *slot = s->private;
110 struct mmc_request *mrq;
111 struct mmc_command *cmd;
112 struct mmc_command *stop;
113 struct mmc_data *data;
114
115 /* Make sure we get a consistent snapshot */
116 spin_lock_bh(&slot->host->lock);
117 mrq = slot->mrq;
118
119 if (mrq) {
120 cmd = mrq->cmd;
121 data = mrq->data;
122 stop = mrq->stop;
123
124 if (cmd)
125 seq_printf(s,
126 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
127 cmd->opcode, cmd->arg, cmd->flags,
128 cmd->resp[0], cmd->resp[1], cmd->resp[2],
129 cmd->resp[2], cmd->error);
130 if (data)
131 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
132 data->bytes_xfered, data->blocks,
133 data->blksz, data->flags, data->error);
134 if (stop)
135 seq_printf(s,
136 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
137 stop->opcode, stop->arg, stop->flags,
138 stop->resp[0], stop->resp[1], stop->resp[2],
139 stop->resp[2], stop->error);
140 }
141
142 spin_unlock_bh(&slot->host->lock);
143
144 return 0;
145}
146
147static int dw_mci_req_open(struct inode *inode, struct file *file)
148{
149 return single_open(file, dw_mci_req_show, inode->i_private);
150}
151
152static const struct file_operations dw_mci_req_fops = {
153 .owner = THIS_MODULE,
154 .open = dw_mci_req_open,
155 .read = seq_read,
156 .llseek = seq_lseek,
157 .release = single_release,
158};
159
160static int dw_mci_regs_show(struct seq_file *s, void *v)
161{
162 seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
163 seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
164 seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
165 seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
166 seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
167 seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
168
169 return 0;
170}
171
172static int dw_mci_regs_open(struct inode *inode, struct file *file)
173{
174 return single_open(file, dw_mci_regs_show, inode->i_private);
175}
176
177static const struct file_operations dw_mci_regs_fops = {
178 .owner = THIS_MODULE,
179 .open = dw_mci_regs_open,
180 .read = seq_read,
181 .llseek = seq_lseek,
182 .release = single_release,
183};
184
185static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
186{
187 struct mmc_host *mmc = slot->mmc;
188 struct dw_mci *host = slot->host;
189 struct dentry *root;
190 struct dentry *node;
191
192 root = mmc->debugfs_root;
193 if (!root)
194 return;
195
196 node = debugfs_create_file("regs", S_IRUSR, root, host,
197 &dw_mci_regs_fops);
198 if (!node)
199 goto err;
200
201 node = debugfs_create_file("req", S_IRUSR, root, slot,
202 &dw_mci_req_fops);
203 if (!node)
204 goto err;
205
206 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
207 if (!node)
208 goto err;
209
210 node = debugfs_create_x32("pending_events", S_IRUSR, root,
211 (u32 *)&host->pending_events);
212 if (!node)
213 goto err;
214
215 node = debugfs_create_x32("completed_events", S_IRUSR, root,
216 (u32 *)&host->completed_events);
217 if (!node)
218 goto err;
219
220 return;
221
222err:
223 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
224}
225#endif /* defined(CONFIG_DEBUG_FS) */
226
227static void dw_mci_set_timeout(struct dw_mci *host)
228{
229 /* timeout (maximum) */
230 mci_writel(host, TMOUT, 0xffffffff);
231}
232
233static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
234{
235 struct mmc_data *data;
236 u32 cmdr;
237 cmd->error = -EINPROGRESS;
238
239 cmdr = cmd->opcode;
240
241 if (cmdr == MMC_STOP_TRANSMISSION)
242 cmdr |= SDMMC_CMD_STOP;
243 else
244 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
245
246 if (cmd->flags & MMC_RSP_PRESENT) {
247 /* We expect a response, so set this bit */
248 cmdr |= SDMMC_CMD_RESP_EXP;
249 if (cmd->flags & MMC_RSP_136)
250 cmdr |= SDMMC_CMD_RESP_LONG;
251 }
252
253 if (cmd->flags & MMC_RSP_CRC)
254 cmdr |= SDMMC_CMD_RESP_CRC;
255
256 data = cmd->data;
257 if (data) {
258 cmdr |= SDMMC_CMD_DAT_EXP;
259 if (data->flags & MMC_DATA_STREAM)
260 cmdr |= SDMMC_CMD_STRM_MODE;
261 if (data->flags & MMC_DATA_WRITE)
262 cmdr |= SDMMC_CMD_DAT_WR;
263 }
264
265 return cmdr;
266}
267
268static void dw_mci_start_command(struct dw_mci *host,
269 struct mmc_command *cmd, u32 cmd_flags)
270{
271 host->cmd = cmd;
272 dev_vdbg(&host->pdev->dev,
273 "start command: ARGR=0x%08x CMDR=0x%08x\n",
274 cmd->arg, cmd_flags);
275
276 mci_writel(host, CMDARG, cmd->arg);
277 wmb();
278
279 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
280}
281
282static void send_stop_cmd(struct dw_mci *host, struct mmc_data *data)
283{
284 dw_mci_start_command(host, data->stop, host->stop_cmdr);
285}
286
287/* DMA interface functions */
288static void dw_mci_stop_dma(struct dw_mci *host)
289{
James Hogan03e8cb52011-06-29 09:28:43 +0100290 if (host->using_dma) {
Will Newtonf95f3852011-01-02 01:11:59 -0500291 host->dma_ops->stop(host);
292 host->dma_ops->cleanup(host);
293 } else {
294 /* Data transfer was stopped by the interrupt handler */
295 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
296 }
297}
298
299#ifdef CONFIG_MMC_DW_IDMAC
300static void dw_mci_dma_cleanup(struct dw_mci *host)
301{
302 struct mmc_data *data = host->data;
303
304 if (data)
305 dma_unmap_sg(&host->pdev->dev, data->sg, data->sg_len,
306 ((data->flags & MMC_DATA_WRITE)
307 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
308}
309
310static void dw_mci_idmac_stop_dma(struct dw_mci *host)
311{
312 u32 temp;
313
314 /* Disable and reset the IDMAC interface */
315 temp = mci_readl(host, CTRL);
316 temp &= ~SDMMC_CTRL_USE_IDMAC;
317 temp |= SDMMC_CTRL_DMA_RESET;
318 mci_writel(host, CTRL, temp);
319
320 /* Stop the IDMAC running */
321 temp = mci_readl(host, BMOD);
Jaehoon Chunga5289a42011-02-25 11:08:13 +0900322 temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
Will Newtonf95f3852011-01-02 01:11:59 -0500323 mci_writel(host, BMOD, temp);
324}
325
326static void dw_mci_idmac_complete_dma(struct dw_mci *host)
327{
328 struct mmc_data *data = host->data;
329
330 dev_vdbg(&host->pdev->dev, "DMA complete\n");
331
332 host->dma_ops->cleanup(host);
333
334 /*
335 * If the card was removed, data will be NULL. No point in trying to
336 * send the stop command or waiting for NBUSY in this case.
337 */
338 if (data) {
339 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
340 tasklet_schedule(&host->tasklet);
341 }
342}
343
344static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
345 unsigned int sg_len)
346{
347 int i;
348 struct idmac_desc *desc = host->sg_cpu;
349
350 for (i = 0; i < sg_len; i++, desc++) {
351 unsigned int length = sg_dma_len(&data->sg[i]);
352 u32 mem_addr = sg_dma_address(&data->sg[i]);
353
354 /* Set the OWN bit and disable interrupts for this descriptor */
355 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | IDMAC_DES0_CH;
356
357 /* Buffer length */
358 IDMAC_SET_BUFFER1_SIZE(desc, length);
359
360 /* Physical address to DMA to/from */
361 desc->des2 = mem_addr;
362 }
363
364 /* Set first descriptor */
365 desc = host->sg_cpu;
366 desc->des0 |= IDMAC_DES0_FD;
367
368 /* Set last descriptor */
369 desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc);
370 desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
371 desc->des0 |= IDMAC_DES0_LD;
372
373 wmb();
374}
375
376static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
377{
378 u32 temp;
379
380 dw_mci_translate_sglist(host, host->data, sg_len);
381
382 /* Select IDMAC interface */
383 temp = mci_readl(host, CTRL);
384 temp |= SDMMC_CTRL_USE_IDMAC;
385 mci_writel(host, CTRL, temp);
386
387 wmb();
388
389 /* Enable the IDMAC */
390 temp = mci_readl(host, BMOD);
Jaehoon Chunga5289a42011-02-25 11:08:13 +0900391 temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
Will Newtonf95f3852011-01-02 01:11:59 -0500392 mci_writel(host, BMOD, temp);
393
394 /* Start it running */
395 mci_writel(host, PLDMND, 1);
396}
397
398static int dw_mci_idmac_init(struct dw_mci *host)
399{
400 struct idmac_desc *p;
401 int i;
402
403 /* Number of descriptors in the ring buffer */
404 host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
405
406 /* Forward link the descriptor list */
407 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++)
408 p->des3 = host->sg_dma + (sizeof(struct idmac_desc) * (i + 1));
409
410 /* Set the last descriptor as the end-of-ring descriptor */
411 p->des3 = host->sg_dma;
412 p->des0 = IDMAC_DES0_ER;
413
414 /* Mask out interrupts - get Tx & Rx complete only */
415 mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI | SDMMC_IDMAC_INT_RI |
416 SDMMC_IDMAC_INT_TI);
417
418 /* Set the descriptor base address */
419 mci_writel(host, DBADDR, host->sg_dma);
420 return 0;
421}
422
423static struct dw_mci_dma_ops dw_mci_idmac_ops = {
424 .init = dw_mci_idmac_init,
425 .start = dw_mci_idmac_start_dma,
426 .stop = dw_mci_idmac_stop_dma,
427 .complete = dw_mci_idmac_complete_dma,
428 .cleanup = dw_mci_dma_cleanup,
429};
430#endif /* CONFIG_MMC_DW_IDMAC */
431
432static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
433{
434 struct scatterlist *sg;
435 unsigned int i, direction, sg_len;
436 u32 temp;
437
James Hogan03e8cb52011-06-29 09:28:43 +0100438 host->using_dma = 0;
439
Will Newtonf95f3852011-01-02 01:11:59 -0500440 /* If we don't have a channel, we can't do DMA */
441 if (!host->use_dma)
442 return -ENODEV;
443
444 /*
445 * We don't do DMA on "complex" transfers, i.e. with
446 * non-word-aligned buffers or lengths. Also, we don't bother
447 * with all the DMA setup overhead for short transfers.
448 */
449 if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
450 return -EINVAL;
451 if (data->blksz & 3)
452 return -EINVAL;
453
454 for_each_sg(data->sg, sg, data->sg_len, i) {
455 if (sg->offset & 3 || sg->length & 3)
456 return -EINVAL;
457 }
458
James Hogan03e8cb52011-06-29 09:28:43 +0100459 host->using_dma = 1;
460
Will Newtonf95f3852011-01-02 01:11:59 -0500461 if (data->flags & MMC_DATA_READ)
462 direction = DMA_FROM_DEVICE;
463 else
464 direction = DMA_TO_DEVICE;
465
466 sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len,
467 direction);
468
469 dev_vdbg(&host->pdev->dev,
470 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
471 (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma,
472 sg_len);
473
474 /* Enable the DMA interface */
475 temp = mci_readl(host, CTRL);
476 temp |= SDMMC_CTRL_DMA_ENABLE;
477 mci_writel(host, CTRL, temp);
478
479 /* Disable RX/TX IRQs, let DMA handle it */
480 temp = mci_readl(host, INTMASK);
481 temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
482 mci_writel(host, INTMASK, temp);
483
484 host->dma_ops->start(host, sg_len);
485
486 return 0;
487}
488
489static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
490{
491 u32 temp;
492
493 data->error = -EINPROGRESS;
494
495 WARN_ON(host->data);
496 host->sg = NULL;
497 host->data = data;
498
499 if (dw_mci_submit_data_dma(host, data)) {
500 host->sg = data->sg;
501 host->pio_offset = 0;
James Hogan34b664a2011-06-24 13:57:56 +0100502 host->part_buf_start = 0;
503 host->part_buf_count = 0;
Will Newtonf95f3852011-01-02 01:11:59 -0500504 if (data->flags & MMC_DATA_READ)
505 host->dir_status = DW_MCI_RECV_STATUS;
506 else
507 host->dir_status = DW_MCI_SEND_STATUS;
508
James Hoganb40af3a2011-06-24 13:54:06 +0100509 mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
Will Newtonf95f3852011-01-02 01:11:59 -0500510 temp = mci_readl(host, INTMASK);
511 temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
512 mci_writel(host, INTMASK, temp);
513
514 temp = mci_readl(host, CTRL);
515 temp &= ~SDMMC_CTRL_DMA_ENABLE;
516 mci_writel(host, CTRL, temp);
517 }
518}
519
520static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
521{
522 struct dw_mci *host = slot->host;
523 unsigned long timeout = jiffies + msecs_to_jiffies(500);
524 unsigned int cmd_status = 0;
525
526 mci_writel(host, CMDARG, arg);
527 wmb();
528 mci_writel(host, CMD, SDMMC_CMD_START | cmd);
529
530 while (time_before(jiffies, timeout)) {
531 cmd_status = mci_readl(host, CMD);
532 if (!(cmd_status & SDMMC_CMD_START))
533 return;
534 }
535 dev_err(&slot->mmc->class_dev,
536 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
537 cmd, arg, cmd_status);
538}
539
540static void dw_mci_setup_bus(struct dw_mci_slot *slot)
541{
542 struct dw_mci *host = slot->host;
543 u32 div;
544
545 if (slot->clock != host->current_speed) {
546 if (host->bus_hz % slot->clock)
547 /*
548 * move the + 1 after the divide to prevent
549 * over-clocking the card.
550 */
551 div = ((host->bus_hz / slot->clock) >> 1) + 1;
552 else
553 div = (host->bus_hz / slot->clock) >> 1;
554
555 dev_info(&slot->mmc->class_dev,
556 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ"
557 " div = %d)\n", slot->id, host->bus_hz, slot->clock,
558 div ? ((host->bus_hz / div) >> 1) : host->bus_hz, div);
559
560 /* disable clock */
561 mci_writel(host, CLKENA, 0);
562 mci_writel(host, CLKSRC, 0);
563
564 /* inform CIU */
565 mci_send_cmd(slot,
566 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
567
568 /* set clock to desired speed */
569 mci_writel(host, CLKDIV, div);
570
571 /* inform CIU */
572 mci_send_cmd(slot,
573 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
574
575 /* enable clock */
Will Newtonaadb9f42011-02-10 10:40:57 +0000576 mci_writel(host, CLKENA, SDMMC_CLKEN_ENABLE |
577 SDMMC_CLKEN_LOW_PWR);
Will Newtonf95f3852011-01-02 01:11:59 -0500578
579 /* inform CIU */
580 mci_send_cmd(slot,
581 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
582
583 host->current_speed = slot->clock;
584 }
585
586 /* Set the current slot bus width */
Seungwon Jeon1d56c452011-06-20 17:23:53 +0900587 mci_writel(host, CTYPE, (slot->ctype << slot->id));
Will Newtonf95f3852011-01-02 01:11:59 -0500588}
589
590static void dw_mci_start_request(struct dw_mci *host,
591 struct dw_mci_slot *slot)
592{
593 struct mmc_request *mrq;
594 struct mmc_command *cmd;
595 struct mmc_data *data;
596 u32 cmdflags;
597
598 mrq = slot->mrq;
599 if (host->pdata->select_slot)
600 host->pdata->select_slot(slot->id);
601
602 /* Slot specific timing and width adjustment */
603 dw_mci_setup_bus(slot);
604
605 host->cur_slot = slot;
606 host->mrq = mrq;
607
608 host->pending_events = 0;
609 host->completed_events = 0;
610 host->data_status = 0;
611
612 data = mrq->data;
613 if (data) {
614 dw_mci_set_timeout(host);
615 mci_writel(host, BYTCNT, data->blksz*data->blocks);
616 mci_writel(host, BLKSIZ, data->blksz);
617 }
618
619 cmd = mrq->cmd;
620 cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
621
622 /* this is the first command, send the initialization clock */
623 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
624 cmdflags |= SDMMC_CMD_INIT;
625
626 if (data) {
627 dw_mci_submit_data(host, data);
628 wmb();
629 }
630
631 dw_mci_start_command(host, cmd, cmdflags);
632
633 if (mrq->stop)
634 host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
635}
636
James Hogan7456caa2011-06-24 13:55:10 +0100637/* must be called with host->lock held */
Will Newtonf95f3852011-01-02 01:11:59 -0500638static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
639 struct mmc_request *mrq)
640{
641 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
642 host->state);
643
Will Newtonf95f3852011-01-02 01:11:59 -0500644 slot->mrq = mrq;
645
646 if (host->state == STATE_IDLE) {
647 host->state = STATE_SENDING_CMD;
648 dw_mci_start_request(host, slot);
649 } else {
650 list_add_tail(&slot->queue_node, &host->queue);
651 }
Will Newtonf95f3852011-01-02 01:11:59 -0500652}
653
654static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
655{
656 struct dw_mci_slot *slot = mmc_priv(mmc);
657 struct dw_mci *host = slot->host;
658
659 WARN_ON(slot->mrq);
660
James Hogan7456caa2011-06-24 13:55:10 +0100661 /*
662 * The check for card presence and queueing of the request must be
663 * atomic, otherwise the card could be removed in between and the
664 * request wouldn't fail until another card was inserted.
665 */
666 spin_lock_bh(&host->lock);
667
Will Newtonf95f3852011-01-02 01:11:59 -0500668 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
James Hogan7456caa2011-06-24 13:55:10 +0100669 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -0500670 mrq->cmd->error = -ENOMEDIUM;
671 mmc_request_done(mmc, mrq);
672 return;
673 }
674
Will Newtonf95f3852011-01-02 01:11:59 -0500675 dw_mci_queue_request(host, slot, mrq);
James Hogan7456caa2011-06-24 13:55:10 +0100676
677 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -0500678}
679
680static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
681{
682 struct dw_mci_slot *slot = mmc_priv(mmc);
Jaehoon Chung41babf72011-02-24 13:46:11 +0900683 u32 regs;
Will Newtonf95f3852011-01-02 01:11:59 -0500684
685 /* set default 1 bit mode */
686 slot->ctype = SDMMC_CTYPE_1BIT;
687
688 switch (ios->bus_width) {
689 case MMC_BUS_WIDTH_1:
690 slot->ctype = SDMMC_CTYPE_1BIT;
691 break;
692 case MMC_BUS_WIDTH_4:
693 slot->ctype = SDMMC_CTYPE_4BIT;
694 break;
Jaehoon Chungc9b2a062011-02-17 16:12:38 +0900695 case MMC_BUS_WIDTH_8:
696 slot->ctype = SDMMC_CTYPE_8BIT;
697 break;
Will Newtonf95f3852011-01-02 01:11:59 -0500698 }
699
Jaehoon Chung41babf72011-02-24 13:46:11 +0900700 /* DDR mode set */
701 if (ios->ddr) {
702 regs = mci_readl(slot->host, UHS_REG);
703 regs |= (0x1 << slot->id) << 16;
704 mci_writel(slot->host, UHS_REG, regs);
705 }
706
Will Newtonf95f3852011-01-02 01:11:59 -0500707 if (ios->clock) {
708 /*
709 * Use mirror of ios->clock to prevent race with mmc
710 * core ios update when finding the minimum.
711 */
712 slot->clock = ios->clock;
713 }
714
715 switch (ios->power_mode) {
716 case MMC_POWER_UP:
717 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
718 break;
719 default:
720 break;
721 }
722}
723
724static int dw_mci_get_ro(struct mmc_host *mmc)
725{
726 int read_only;
727 struct dw_mci_slot *slot = mmc_priv(mmc);
728 struct dw_mci_board *brd = slot->host->pdata;
729
730 /* Use platform get_ro function, else try on board write protect */
731 if (brd->get_ro)
732 read_only = brd->get_ro(slot->id);
733 else
734 read_only =
735 mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
736
737 dev_dbg(&mmc->class_dev, "card is %s\n",
738 read_only ? "read-only" : "read-write");
739
740 return read_only;
741}
742
743static int dw_mci_get_cd(struct mmc_host *mmc)
744{
745 int present;
746 struct dw_mci_slot *slot = mmc_priv(mmc);
747 struct dw_mci_board *brd = slot->host->pdata;
748
749 /* Use platform get_cd function, else try onboard card detect */
Jaehoon Chungfc3d7722011-02-25 11:08:15 +0900750 if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
751 present = 1;
752 else if (brd->get_cd)
Will Newtonf95f3852011-01-02 01:11:59 -0500753 present = !brd->get_cd(slot->id);
754 else
755 present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
756 == 0 ? 1 : 0;
757
758 if (present)
759 dev_dbg(&mmc->class_dev, "card is present\n");
760 else
761 dev_dbg(&mmc->class_dev, "card is not present\n");
762
763 return present;
764}
765
766static const struct mmc_host_ops dw_mci_ops = {
767 .request = dw_mci_request,
768 .set_ios = dw_mci_set_ios,
769 .get_ro = dw_mci_get_ro,
770 .get_cd = dw_mci_get_cd,
771};
772
773static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
774 __releases(&host->lock)
775 __acquires(&host->lock)
776{
777 struct dw_mci_slot *slot;
778 struct mmc_host *prev_mmc = host->cur_slot->mmc;
779
780 WARN_ON(host->cmd || host->data);
781
782 host->cur_slot->mrq = NULL;
783 host->mrq = NULL;
784 if (!list_empty(&host->queue)) {
785 slot = list_entry(host->queue.next,
786 struct dw_mci_slot, queue_node);
787 list_del(&slot->queue_node);
788 dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
789 mmc_hostname(slot->mmc));
790 host->state = STATE_SENDING_CMD;
791 dw_mci_start_request(host, slot);
792 } else {
793 dev_vdbg(&host->pdev->dev, "list empty\n");
794 host->state = STATE_IDLE;
795 }
796
797 spin_unlock(&host->lock);
798 mmc_request_done(prev_mmc, mrq);
799 spin_lock(&host->lock);
800}
801
802static void dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
803{
804 u32 status = host->cmd_status;
805
806 host->cmd_status = 0;
807
808 /* Read the response from the card (up to 16 bytes) */
809 if (cmd->flags & MMC_RSP_PRESENT) {
810 if (cmd->flags & MMC_RSP_136) {
811 cmd->resp[3] = mci_readl(host, RESP0);
812 cmd->resp[2] = mci_readl(host, RESP1);
813 cmd->resp[1] = mci_readl(host, RESP2);
814 cmd->resp[0] = mci_readl(host, RESP3);
815 } else {
816 cmd->resp[0] = mci_readl(host, RESP0);
817 cmd->resp[1] = 0;
818 cmd->resp[2] = 0;
819 cmd->resp[3] = 0;
820 }
821 }
822
823 if (status & SDMMC_INT_RTO)
824 cmd->error = -ETIMEDOUT;
825 else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
826 cmd->error = -EILSEQ;
827 else if (status & SDMMC_INT_RESP_ERR)
828 cmd->error = -EIO;
829 else
830 cmd->error = 0;
831
832 if (cmd->error) {
833 /* newer ip versions need a delay between retries */
834 if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
835 mdelay(20);
836
837 if (cmd->data) {
838 host->data = NULL;
839 dw_mci_stop_dma(host);
840 }
841 }
842}
843
844static void dw_mci_tasklet_func(unsigned long priv)
845{
846 struct dw_mci *host = (struct dw_mci *)priv;
847 struct mmc_data *data;
848 struct mmc_command *cmd;
849 enum dw_mci_state state;
850 enum dw_mci_state prev_state;
851 u32 status;
852
853 spin_lock(&host->lock);
854
855 state = host->state;
856 data = host->data;
857
858 do {
859 prev_state = state;
860
861 switch (state) {
862 case STATE_IDLE:
863 break;
864
865 case STATE_SENDING_CMD:
866 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
867 &host->pending_events))
868 break;
869
870 cmd = host->cmd;
871 host->cmd = NULL;
872 set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
873 dw_mci_command_complete(host, host->mrq->cmd);
874 if (!host->mrq->data || cmd->error) {
875 dw_mci_request_end(host, host->mrq);
876 goto unlock;
877 }
878
879 prev_state = state = STATE_SENDING_DATA;
880 /* fall through */
881
882 case STATE_SENDING_DATA:
883 if (test_and_clear_bit(EVENT_DATA_ERROR,
884 &host->pending_events)) {
885 dw_mci_stop_dma(host);
886 if (data->stop)
887 send_stop_cmd(host, data);
888 state = STATE_DATA_ERROR;
889 break;
890 }
891
892 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
893 &host->pending_events))
894 break;
895
896 set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
897 prev_state = state = STATE_DATA_BUSY;
898 /* fall through */
899
900 case STATE_DATA_BUSY:
901 if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
902 &host->pending_events))
903 break;
904
905 host->data = NULL;
906 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
907 status = host->data_status;
908
909 if (status & DW_MCI_DATA_ERROR_FLAGS) {
910 if (status & SDMMC_INT_DTO) {
911 dev_err(&host->pdev->dev,
912 "data timeout error\n");
913 data->error = -ETIMEDOUT;
914 } else if (status & SDMMC_INT_DCRC) {
915 dev_err(&host->pdev->dev,
916 "data CRC error\n");
917 data->error = -EILSEQ;
918 } else {
919 dev_err(&host->pdev->dev,
920 "data FIFO error "
921 "(status=%08x)\n",
922 status);
923 data->error = -EIO;
924 }
925 } else {
926 data->bytes_xfered = data->blocks * data->blksz;
927 data->error = 0;
928 }
929
930 if (!data->stop) {
931 dw_mci_request_end(host, host->mrq);
932 goto unlock;
933 }
934
935 prev_state = state = STATE_SENDING_STOP;
936 if (!data->error)
937 send_stop_cmd(host, data);
938 /* fall through */
939
940 case STATE_SENDING_STOP:
941 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
942 &host->pending_events))
943 break;
944
945 host->cmd = NULL;
946 dw_mci_command_complete(host, host->mrq->stop);
947 dw_mci_request_end(host, host->mrq);
948 goto unlock;
949
950 case STATE_DATA_ERROR:
951 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
952 &host->pending_events))
953 break;
954
955 state = STATE_DATA_BUSY;
956 break;
957 }
958 } while (state != prev_state);
959
960 host->state = state;
961unlock:
962 spin_unlock(&host->lock);
963
964}
965
James Hogan34b664a2011-06-24 13:57:56 +0100966/* push final bytes to part_buf, only use during push */
967static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
968{
969 memcpy((void *)&host->part_buf, buf, cnt);
970 host->part_buf_count = cnt;
971}
972
973/* append bytes to part_buf, only use during push */
974static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
975{
976 cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
977 memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
978 host->part_buf_count += cnt;
979 return cnt;
980}
981
982/* pull first bytes from part_buf, only use during pull */
983static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
984{
985 cnt = min(cnt, (int)host->part_buf_count);
986 if (cnt) {
987 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
988 cnt);
989 host->part_buf_count -= cnt;
990 host->part_buf_start += cnt;
991 }
992 return cnt;
993}
994
995/* pull final bytes from the part_buf, assuming it's just been filled */
996static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
997{
998 memcpy(buf, &host->part_buf, cnt);
999 host->part_buf_start = cnt;
1000 host->part_buf_count = (1 << host->data_shift) - cnt;
1001}
1002
Will Newtonf95f3852011-01-02 01:11:59 -05001003static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
1004{
James Hogan34b664a2011-06-24 13:57:56 +01001005 /* try and push anything in the part_buf */
1006 if (unlikely(host->part_buf_count)) {
1007 int len = dw_mci_push_part_bytes(host, buf, cnt);
1008 buf += len;
1009 cnt -= len;
1010 if (!sg_next(host->sg) || host->part_buf_count == 2) {
1011 mci_writew(host, DATA, host->part_buf16);
1012 host->part_buf_count = 0;
1013 }
1014 }
1015#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1016 if (unlikely((unsigned long)buf & 0x1)) {
1017 while (cnt >= 2) {
1018 u16 aligned_buf[64];
1019 int len = min(cnt & -2, (int)sizeof(aligned_buf));
1020 int items = len >> 1;
1021 int i;
1022 /* memcpy from input buffer into aligned buffer */
1023 memcpy(aligned_buf, buf, len);
1024 buf += len;
1025 cnt -= len;
1026 /* push data from aligned buffer into fifo */
1027 for (i = 0; i < items; ++i)
1028 mci_writew(host, DATA, aligned_buf[i]);
1029 }
1030 } else
1031#endif
1032 {
1033 u16 *pdata = buf;
1034 for (; cnt >= 2; cnt -= 2)
1035 mci_writew(host, DATA, *pdata++);
1036 buf = pdata;
1037 }
1038 /* put anything remaining in the part_buf */
1039 if (cnt) {
1040 dw_mci_set_part_bytes(host, buf, cnt);
1041 if (!sg_next(host->sg))
1042 mci_writew(host, DATA, host->part_buf16);
Will Newtonf95f3852011-01-02 01:11:59 -05001043 }
1044}
1045
1046static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
1047{
James Hogan34b664a2011-06-24 13:57:56 +01001048#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1049 if (unlikely((unsigned long)buf & 0x1)) {
1050 while (cnt >= 2) {
1051 /* pull data from fifo into aligned buffer */
1052 u16 aligned_buf[64];
1053 int len = min(cnt & -2, (int)sizeof(aligned_buf));
1054 int items = len >> 1;
1055 int i;
1056 for (i = 0; i < items; ++i)
1057 aligned_buf[i] = mci_readw(host, DATA);
1058 /* memcpy from aligned buffer into output buffer */
1059 memcpy(buf, aligned_buf, len);
1060 buf += len;
1061 cnt -= len;
1062 }
1063 } else
1064#endif
1065 {
1066 u16 *pdata = buf;
1067 for (; cnt >= 2; cnt -= 2)
1068 *pdata++ = mci_readw(host, DATA);
1069 buf = pdata;
1070 }
1071 if (cnt) {
1072 host->part_buf16 = mci_readw(host, DATA);
1073 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05001074 }
1075}
1076
1077static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
1078{
James Hogan34b664a2011-06-24 13:57:56 +01001079 /* try and push anything in the part_buf */
1080 if (unlikely(host->part_buf_count)) {
1081 int len = dw_mci_push_part_bytes(host, buf, cnt);
1082 buf += len;
1083 cnt -= len;
1084 if (!sg_next(host->sg) || host->part_buf_count == 4) {
1085 mci_writel(host, DATA, host->part_buf32);
1086 host->part_buf_count = 0;
1087 }
1088 }
1089#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1090 if (unlikely((unsigned long)buf & 0x3)) {
1091 while (cnt >= 4) {
1092 u32 aligned_buf[32];
1093 int len = min(cnt & -4, (int)sizeof(aligned_buf));
1094 int items = len >> 2;
1095 int i;
1096 /* memcpy from input buffer into aligned buffer */
1097 memcpy(aligned_buf, buf, len);
1098 buf += len;
1099 cnt -= len;
1100 /* push data from aligned buffer into fifo */
1101 for (i = 0; i < items; ++i)
1102 mci_writel(host, DATA, aligned_buf[i]);
1103 }
1104 } else
1105#endif
1106 {
1107 u32 *pdata = buf;
1108 for (; cnt >= 4; cnt -= 4)
1109 mci_writel(host, DATA, *pdata++);
1110 buf = pdata;
1111 }
1112 /* put anything remaining in the part_buf */
1113 if (cnt) {
1114 dw_mci_set_part_bytes(host, buf, cnt);
1115 if (!sg_next(host->sg))
1116 mci_writel(host, DATA, host->part_buf32);
Will Newtonf95f3852011-01-02 01:11:59 -05001117 }
1118}
1119
1120static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
1121{
James Hogan34b664a2011-06-24 13:57:56 +01001122#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1123 if (unlikely((unsigned long)buf & 0x3)) {
1124 while (cnt >= 4) {
1125 /* pull data from fifo into aligned buffer */
1126 u32 aligned_buf[32];
1127 int len = min(cnt & -4, (int)sizeof(aligned_buf));
1128 int items = len >> 2;
1129 int i;
1130 for (i = 0; i < items; ++i)
1131 aligned_buf[i] = mci_readl(host, DATA);
1132 /* memcpy from aligned buffer into output buffer */
1133 memcpy(buf, aligned_buf, len);
1134 buf += len;
1135 cnt -= len;
1136 }
1137 } else
1138#endif
1139 {
1140 u32 *pdata = buf;
1141 for (; cnt >= 4; cnt -= 4)
1142 *pdata++ = mci_readl(host, DATA);
1143 buf = pdata;
1144 }
1145 if (cnt) {
1146 host->part_buf32 = mci_readl(host, DATA);
1147 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05001148 }
1149}
1150
1151static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
1152{
James Hogan34b664a2011-06-24 13:57:56 +01001153 /* try and push anything in the part_buf */
1154 if (unlikely(host->part_buf_count)) {
1155 int len = dw_mci_push_part_bytes(host, buf, cnt);
1156 buf += len;
1157 cnt -= len;
1158 if (!sg_next(host->sg) || host->part_buf_count == 8) {
1159 mci_writew(host, DATA, host->part_buf);
1160 host->part_buf_count = 0;
1161 }
1162 }
1163#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1164 if (unlikely((unsigned long)buf & 0x7)) {
1165 while (cnt >= 8) {
1166 u64 aligned_buf[16];
1167 int len = min(cnt & -8, (int)sizeof(aligned_buf));
1168 int items = len >> 3;
1169 int i;
1170 /* memcpy from input buffer into aligned buffer */
1171 memcpy(aligned_buf, buf, len);
1172 buf += len;
1173 cnt -= len;
1174 /* push data from aligned buffer into fifo */
1175 for (i = 0; i < items; ++i)
1176 mci_writeq(host, DATA, aligned_buf[i]);
1177 }
1178 } else
1179#endif
1180 {
1181 u64 *pdata = buf;
1182 for (; cnt >= 8; cnt -= 8)
1183 mci_writeq(host, DATA, *pdata++);
1184 buf = pdata;
1185 }
1186 /* put anything remaining in the part_buf */
1187 if (cnt) {
1188 dw_mci_set_part_bytes(host, buf, cnt);
1189 if (!sg_next(host->sg))
1190 mci_writeq(host, DATA, host->part_buf);
Will Newtonf95f3852011-01-02 01:11:59 -05001191 }
1192}
1193
1194static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
1195{
James Hogan34b664a2011-06-24 13:57:56 +01001196#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1197 if (unlikely((unsigned long)buf & 0x7)) {
1198 while (cnt >= 8) {
1199 /* pull data from fifo into aligned buffer */
1200 u64 aligned_buf[16];
1201 int len = min(cnt & -8, (int)sizeof(aligned_buf));
1202 int items = len >> 3;
1203 int i;
1204 for (i = 0; i < items; ++i)
1205 aligned_buf[i] = mci_readq(host, DATA);
1206 /* memcpy from aligned buffer into output buffer */
1207 memcpy(buf, aligned_buf, len);
1208 buf += len;
1209 cnt -= len;
1210 }
1211 } else
1212#endif
1213 {
1214 u64 *pdata = buf;
1215 for (; cnt >= 8; cnt -= 8)
1216 *pdata++ = mci_readq(host, DATA);
1217 buf = pdata;
Will Newtonf95f3852011-01-02 01:11:59 -05001218 }
James Hogan34b664a2011-06-24 13:57:56 +01001219 if (cnt) {
1220 host->part_buf = mci_readq(host, DATA);
1221 dw_mci_pull_final_bytes(host, buf, cnt);
1222 }
1223}
1224
1225static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
1226{
1227 int len;
1228
1229 /* get remaining partial bytes */
1230 len = dw_mci_pull_part_bytes(host, buf, cnt);
1231 if (unlikely(len == cnt))
1232 return;
1233 buf += len;
1234 cnt -= len;
1235
1236 /* get the rest of the data */
1237 host->pull_data(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05001238}
1239
1240static void dw_mci_read_data_pio(struct dw_mci *host)
1241{
1242 struct scatterlist *sg = host->sg;
1243 void *buf = sg_virt(sg);
1244 unsigned int offset = host->pio_offset;
1245 struct mmc_data *data = host->data;
1246 int shift = host->data_shift;
1247 u32 status;
Chris Ballba6a9022011-02-28 16:45:10 -05001248 unsigned int nbytes = 0, len;
Will Newtonf95f3852011-01-02 01:11:59 -05001249
1250 do {
James Hogan34b664a2011-06-24 13:57:56 +01001251 len = host->part_buf_count +
1252 (SDMMC_GET_FCNT(mci_readl(host, STATUS)) << shift);
Will Newtonf95f3852011-01-02 01:11:59 -05001253 if (offset + len <= sg->length) {
James Hogan34b664a2011-06-24 13:57:56 +01001254 dw_mci_pull_data(host, (void *)(buf + offset), len);
Will Newtonf95f3852011-01-02 01:11:59 -05001255
1256 offset += len;
1257 nbytes += len;
1258
1259 if (offset == sg->length) {
1260 flush_dcache_page(sg_page(sg));
1261 host->sg = sg = sg_next(sg);
1262 if (!sg)
1263 goto done;
1264
1265 offset = 0;
1266 buf = sg_virt(sg);
1267 }
1268 } else {
1269 unsigned int remaining = sg->length - offset;
James Hogan34b664a2011-06-24 13:57:56 +01001270 dw_mci_pull_data(host, (void *)(buf + offset),
1271 remaining);
Will Newtonf95f3852011-01-02 01:11:59 -05001272 nbytes += remaining;
1273
1274 flush_dcache_page(sg_page(sg));
1275 host->sg = sg = sg_next(sg);
1276 if (!sg)
1277 goto done;
1278
1279 offset = len - remaining;
1280 buf = sg_virt(sg);
James Hogan34b664a2011-06-24 13:57:56 +01001281 dw_mci_pull_data(host, buf, offset);
Will Newtonf95f3852011-01-02 01:11:59 -05001282 nbytes += offset;
1283 }
1284
1285 status = mci_readl(host, MINTSTS);
1286 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
1287 if (status & DW_MCI_DATA_ERROR_FLAGS) {
1288 host->data_status = status;
1289 data->bytes_xfered += nbytes;
1290 smp_wmb();
1291
1292 set_bit(EVENT_DATA_ERROR, &host->pending_events);
1293
1294 tasklet_schedule(&host->tasklet);
1295 return;
1296 }
Will Newtonf95f3852011-01-02 01:11:59 -05001297 } while (status & SDMMC_INT_RXDR); /*if the RXDR is ready read again*/
Will Newtonf95f3852011-01-02 01:11:59 -05001298 host->pio_offset = offset;
1299 data->bytes_xfered += nbytes;
1300 return;
1301
1302done:
1303 data->bytes_xfered += nbytes;
1304 smp_wmb();
1305 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
1306}
1307
1308static void dw_mci_write_data_pio(struct dw_mci *host)
1309{
1310 struct scatterlist *sg = host->sg;
1311 void *buf = sg_virt(sg);
1312 unsigned int offset = host->pio_offset;
1313 struct mmc_data *data = host->data;
1314 int shift = host->data_shift;
1315 u32 status;
1316 unsigned int nbytes = 0, len;
1317
1318 do {
James Hogan34b664a2011-06-24 13:57:56 +01001319 len = ((host->fifo_depth -
1320 SDMMC_GET_FCNT(mci_readl(host, STATUS))) << shift)
1321 - host->part_buf_count;
Will Newtonf95f3852011-01-02 01:11:59 -05001322 if (offset + len <= sg->length) {
1323 host->push_data(host, (void *)(buf + offset), len);
1324
1325 offset += len;
1326 nbytes += len;
1327 if (offset == sg->length) {
1328 host->sg = sg = sg_next(sg);
1329 if (!sg)
1330 goto done;
1331
1332 offset = 0;
1333 buf = sg_virt(sg);
1334 }
1335 } else {
1336 unsigned int remaining = sg->length - offset;
1337
1338 host->push_data(host, (void *)(buf + offset),
1339 remaining);
1340 nbytes += remaining;
1341
1342 host->sg = sg = sg_next(sg);
1343 if (!sg)
1344 goto done;
1345
1346 offset = len - remaining;
1347 buf = sg_virt(sg);
1348 host->push_data(host, (void *)buf, offset);
1349 nbytes += offset;
1350 }
1351
1352 status = mci_readl(host, MINTSTS);
1353 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
1354 if (status & DW_MCI_DATA_ERROR_FLAGS) {
1355 host->data_status = status;
1356 data->bytes_xfered += nbytes;
1357
1358 smp_wmb();
1359
1360 set_bit(EVENT_DATA_ERROR, &host->pending_events);
1361
1362 tasklet_schedule(&host->tasklet);
1363 return;
1364 }
1365 } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
Will Newtonf95f3852011-01-02 01:11:59 -05001366 host->pio_offset = offset;
1367 data->bytes_xfered += nbytes;
Will Newtonf95f3852011-01-02 01:11:59 -05001368 return;
1369
1370done:
1371 data->bytes_xfered += nbytes;
1372 smp_wmb();
1373 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
1374}
1375
1376static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
1377{
1378 if (!host->cmd_status)
1379 host->cmd_status = status;
1380
1381 smp_wmb();
1382
1383 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
1384 tasklet_schedule(&host->tasklet);
1385}
1386
1387static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
1388{
1389 struct dw_mci *host = dev_id;
1390 u32 status, pending;
1391 unsigned int pass_count = 0;
1392
1393 do {
1394 status = mci_readl(host, RINTSTS);
1395 pending = mci_readl(host, MINTSTS); /* read-only mask reg */
1396
1397 /*
1398 * DTO fix - version 2.10a and below, and only if internal DMA
1399 * is configured.
1400 */
1401 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
1402 if (!pending &&
1403 ((mci_readl(host, STATUS) >> 17) & 0x1fff))
1404 pending |= SDMMC_INT_DATA_OVER;
1405 }
1406
1407 if (!pending)
1408 break;
1409
1410 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
1411 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
1412 host->cmd_status = status;
1413 smp_wmb();
1414 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -05001415 }
1416
1417 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
1418 /* if there is an error report DATA_ERROR */
1419 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
1420 host->data_status = status;
1421 smp_wmb();
1422 set_bit(EVENT_DATA_ERROR, &host->pending_events);
Seungwon Jeon6e83e102011-06-20 17:24:16 +09001423 if (!(pending & (SDMMC_INT_DTO | SDMMC_INT_DCRC |
1424 SDMMC_INT_SBE | SDMMC_INT_EBE)))
1425 tasklet_schedule(&host->tasklet);
Will Newtonf95f3852011-01-02 01:11:59 -05001426 }
1427
1428 if (pending & SDMMC_INT_DATA_OVER) {
1429 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
1430 if (!host->data_status)
1431 host->data_status = status;
1432 smp_wmb();
1433 if (host->dir_status == DW_MCI_RECV_STATUS) {
1434 if (host->sg != NULL)
1435 dw_mci_read_data_pio(host);
1436 }
1437 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
1438 tasklet_schedule(&host->tasklet);
1439 }
1440
1441 if (pending & SDMMC_INT_RXDR) {
1442 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01001443 if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
Will Newtonf95f3852011-01-02 01:11:59 -05001444 dw_mci_read_data_pio(host);
1445 }
1446
1447 if (pending & SDMMC_INT_TXDR) {
1448 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01001449 if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
Will Newtonf95f3852011-01-02 01:11:59 -05001450 dw_mci_write_data_pio(host);
1451 }
1452
1453 if (pending & SDMMC_INT_CMD_DONE) {
1454 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
1455 dw_mci_cmd_interrupt(host, status);
1456 }
1457
1458 if (pending & SDMMC_INT_CD) {
1459 mci_writel(host, RINTSTS, SDMMC_INT_CD);
James Hogan1791b13e2011-06-24 13:55:55 +01001460 queue_work(dw_mci_card_workqueue, &host->card_work);
Will Newtonf95f3852011-01-02 01:11:59 -05001461 }
1462
1463 } while (pass_count++ < 5);
1464
1465#ifdef CONFIG_MMC_DW_IDMAC
1466 /* Handle DMA interrupts */
1467 pending = mci_readl(host, IDSTS);
1468 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
1469 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI);
1470 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
1471 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
1472 host->dma_ops->complete(host);
1473 }
1474#endif
1475
1476 return IRQ_HANDLED;
1477}
1478
James Hogan1791b13e2011-06-24 13:55:55 +01001479static void dw_mci_work_routine_card(struct work_struct *work)
Will Newtonf95f3852011-01-02 01:11:59 -05001480{
James Hogan1791b13e2011-06-24 13:55:55 +01001481 struct dw_mci *host = container_of(work, struct dw_mci, card_work);
Will Newtonf95f3852011-01-02 01:11:59 -05001482 int i;
1483
1484 for (i = 0; i < host->num_slots; i++) {
1485 struct dw_mci_slot *slot = host->slot[i];
1486 struct mmc_host *mmc = slot->mmc;
1487 struct mmc_request *mrq;
1488 int present;
1489 u32 ctrl;
1490
1491 present = dw_mci_get_cd(mmc);
1492 while (present != slot->last_detect_state) {
Will Newtonf95f3852011-01-02 01:11:59 -05001493 dev_dbg(&slot->mmc->class_dev, "card %s\n",
1494 present ? "inserted" : "removed");
1495
James Hogan1791b13e2011-06-24 13:55:55 +01001496 /* Power up slot (before spin_lock, may sleep) */
1497 if (present != 0 && host->pdata->setpower)
1498 host->pdata->setpower(slot->id, mmc->ocr_avail);
1499
1500 spin_lock_bh(&host->lock);
1501
Will Newtonf95f3852011-01-02 01:11:59 -05001502 /* Card change detected */
1503 slot->last_detect_state = present;
1504
James Hogan1791b13e2011-06-24 13:55:55 +01001505 /* Mark card as present if applicable */
1506 if (present != 0)
Will Newtonf95f3852011-01-02 01:11:59 -05001507 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
Will Newtonf95f3852011-01-02 01:11:59 -05001508
1509 /* Clean up queue if present */
1510 mrq = slot->mrq;
1511 if (mrq) {
1512 if (mrq == host->mrq) {
1513 host->data = NULL;
1514 host->cmd = NULL;
1515
1516 switch (host->state) {
1517 case STATE_IDLE:
1518 break;
1519 case STATE_SENDING_CMD:
1520 mrq->cmd->error = -ENOMEDIUM;
1521 if (!mrq->data)
1522 break;
1523 /* fall through */
1524 case STATE_SENDING_DATA:
1525 mrq->data->error = -ENOMEDIUM;
1526 dw_mci_stop_dma(host);
1527 break;
1528 case STATE_DATA_BUSY:
1529 case STATE_DATA_ERROR:
1530 if (mrq->data->error == -EINPROGRESS)
1531 mrq->data->error = -ENOMEDIUM;
1532 if (!mrq->stop)
1533 break;
1534 /* fall through */
1535 case STATE_SENDING_STOP:
1536 mrq->stop->error = -ENOMEDIUM;
1537 break;
1538 }
1539
1540 dw_mci_request_end(host, mrq);
1541 } else {
1542 list_del(&slot->queue_node);
1543 mrq->cmd->error = -ENOMEDIUM;
1544 if (mrq->data)
1545 mrq->data->error = -ENOMEDIUM;
1546 if (mrq->stop)
1547 mrq->stop->error = -ENOMEDIUM;
1548
1549 spin_unlock(&host->lock);
1550 mmc_request_done(slot->mmc, mrq);
1551 spin_lock(&host->lock);
1552 }
1553 }
1554
1555 /* Power down slot */
1556 if (present == 0) {
Will Newtonf95f3852011-01-02 01:11:59 -05001557 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1558
1559 /*
1560 * Clear down the FIFO - doing so generates a
1561 * block interrupt, hence setting the
1562 * scatter-gather pointer to NULL.
1563 */
1564 host->sg = NULL;
1565
1566 ctrl = mci_readl(host, CTRL);
1567 ctrl |= SDMMC_CTRL_FIFO_RESET;
1568 mci_writel(host, CTRL, ctrl);
1569
1570#ifdef CONFIG_MMC_DW_IDMAC
1571 ctrl = mci_readl(host, BMOD);
1572 ctrl |= 0x01; /* Software reset of DMA */
1573 mci_writel(host, BMOD, ctrl);
1574#endif
1575
1576 }
1577
James Hogan1791b13e2011-06-24 13:55:55 +01001578 spin_unlock_bh(&host->lock);
1579
1580 /* Power down slot (after spin_unlock, may sleep) */
1581 if (present == 0 && host->pdata->setpower)
1582 host->pdata->setpower(slot->id, 0);
1583
Will Newtonf95f3852011-01-02 01:11:59 -05001584 present = dw_mci_get_cd(mmc);
1585 }
1586
1587 mmc_detect_change(slot->mmc,
1588 msecs_to_jiffies(host->pdata->detect_delay_ms));
1589 }
1590}
1591
1592static int __init dw_mci_init_slot(struct dw_mci *host, unsigned int id)
1593{
1594 struct mmc_host *mmc;
1595 struct dw_mci_slot *slot;
1596
1597 mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), &host->pdev->dev);
1598 if (!mmc)
1599 return -ENOMEM;
1600
1601 slot = mmc_priv(mmc);
1602 slot->id = id;
1603 slot->mmc = mmc;
1604 slot->host = host;
1605
1606 mmc->ops = &dw_mci_ops;
1607 mmc->f_min = DIV_ROUND_UP(host->bus_hz, 510);
1608 mmc->f_max = host->bus_hz;
1609
1610 if (host->pdata->get_ocr)
1611 mmc->ocr_avail = host->pdata->get_ocr(id);
1612 else
1613 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1614
1615 /*
1616 * Start with slot power disabled, it will be enabled when a card
1617 * is detected.
1618 */
1619 if (host->pdata->setpower)
1620 host->pdata->setpower(id, 0);
1621
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09001622 if (host->pdata->caps)
1623 mmc->caps = host->pdata->caps;
1624 else
1625 mmc->caps = 0;
1626
Will Newtonf95f3852011-01-02 01:11:59 -05001627 if (host->pdata->get_bus_wd)
1628 if (host->pdata->get_bus_wd(slot->id) >= 4)
1629 mmc->caps |= MMC_CAP_4_BIT_DATA;
1630
1631 if (host->pdata->quirks & DW_MCI_QUIRK_HIGHSPEED)
1632 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1633
1634#ifdef CONFIG_MMC_DW_IDMAC
1635 mmc->max_segs = host->ring_size;
1636 mmc->max_blk_size = 65536;
1637 mmc->max_blk_count = host->ring_size;
1638 mmc->max_seg_size = 0x1000;
1639 mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count;
1640#else
1641 if (host->pdata->blk_settings) {
1642 mmc->max_segs = host->pdata->blk_settings->max_segs;
1643 mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
1644 mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
1645 mmc->max_req_size = host->pdata->blk_settings->max_req_size;
1646 mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
1647 } else {
1648 /* Useful defaults if platform data is unset. */
1649 mmc->max_segs = 64;
1650 mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
1651 mmc->max_blk_count = 512;
1652 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1653 mmc->max_seg_size = mmc->max_req_size;
1654 }
1655#endif /* CONFIG_MMC_DW_IDMAC */
1656
Jaehoon Chungc07946a2011-02-25 11:08:14 +09001657 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
1658 if (IS_ERR(host->vmmc)) {
1659 printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
1660 host->vmmc = NULL;
1661 } else
1662 regulator_enable(host->vmmc);
1663
Will Newtonf95f3852011-01-02 01:11:59 -05001664 if (dw_mci_get_cd(mmc))
1665 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1666 else
1667 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1668
1669 host->slot[id] = slot;
1670 mmc_add_host(mmc);
1671
1672#if defined(CONFIG_DEBUG_FS)
1673 dw_mci_init_debugfs(slot);
1674#endif
1675
1676 /* Card initially undetected */
1677 slot->last_detect_state = 0;
1678
Will Newtondd6c4b92011-02-10 14:37:03 -05001679 /*
1680 * Card may have been plugged in prior to boot so we
1681 * need to run the detect tasklet
1682 */
James Hogan1791b13e2011-06-24 13:55:55 +01001683 queue_work(dw_mci_card_workqueue, &host->card_work);
Will Newtondd6c4b92011-02-10 14:37:03 -05001684
Will Newtonf95f3852011-01-02 01:11:59 -05001685 return 0;
1686}
1687
1688static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
1689{
1690 /* Shutdown detect IRQ */
1691 if (slot->host->pdata->exit)
1692 slot->host->pdata->exit(id);
1693
1694 /* Debugfs stuff is cleaned up by mmc core */
1695 mmc_remove_host(slot->mmc);
1696 slot->host->slot[id] = NULL;
1697 mmc_free_host(slot->mmc);
1698}
1699
1700static void dw_mci_init_dma(struct dw_mci *host)
1701{
1702 /* Alloc memory for sg translation */
1703 host->sg_cpu = dma_alloc_coherent(&host->pdev->dev, PAGE_SIZE,
1704 &host->sg_dma, GFP_KERNEL);
1705 if (!host->sg_cpu) {
1706 dev_err(&host->pdev->dev, "%s: could not alloc DMA memory\n",
1707 __func__);
1708 goto no_dma;
1709 }
1710
1711 /* Determine which DMA interface to use */
1712#ifdef CONFIG_MMC_DW_IDMAC
1713 host->dma_ops = &dw_mci_idmac_ops;
1714 dev_info(&host->pdev->dev, "Using internal DMA controller.\n");
1715#endif
1716
1717 if (!host->dma_ops)
1718 goto no_dma;
1719
1720 if (host->dma_ops->init) {
1721 if (host->dma_ops->init(host)) {
1722 dev_err(&host->pdev->dev, "%s: Unable to initialize "
1723 "DMA Controller.\n", __func__);
1724 goto no_dma;
1725 }
1726 } else {
1727 dev_err(&host->pdev->dev, "DMA initialization not found.\n");
1728 goto no_dma;
1729 }
1730
1731 host->use_dma = 1;
1732 return;
1733
1734no_dma:
1735 dev_info(&host->pdev->dev, "Using PIO mode.\n");
1736 host->use_dma = 0;
1737 return;
1738}
1739
1740static bool mci_wait_reset(struct device *dev, struct dw_mci *host)
1741{
1742 unsigned long timeout = jiffies + msecs_to_jiffies(500);
1743 unsigned int ctrl;
1744
1745 mci_writel(host, CTRL, (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
1746 SDMMC_CTRL_DMA_RESET));
1747
1748 /* wait till resets clear */
1749 do {
1750 ctrl = mci_readl(host, CTRL);
1751 if (!(ctrl & (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
1752 SDMMC_CTRL_DMA_RESET)))
1753 return true;
1754 } while (time_before(jiffies, timeout));
1755
1756 dev_err(dev, "Timeout resetting block (ctrl %#x)\n", ctrl);
1757
1758 return false;
1759}
1760
1761static int dw_mci_probe(struct platform_device *pdev)
1762{
1763 struct dw_mci *host;
1764 struct resource *regs;
1765 struct dw_mci_board *pdata;
1766 int irq, ret, i, width;
1767 u32 fifo_size;
1768
1769 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1770 if (!regs)
1771 return -ENXIO;
1772
1773 irq = platform_get_irq(pdev, 0);
1774 if (irq < 0)
1775 return irq;
1776
1777 host = kzalloc(sizeof(struct dw_mci), GFP_KERNEL);
1778 if (!host)
1779 return -ENOMEM;
1780
1781 host->pdev = pdev;
1782 host->pdata = pdata = pdev->dev.platform_data;
1783 if (!pdata || !pdata->init) {
1784 dev_err(&pdev->dev,
1785 "Platform data must supply init function\n");
1786 ret = -ENODEV;
1787 goto err_freehost;
1788 }
1789
1790 if (!pdata->select_slot && pdata->num_slots > 1) {
1791 dev_err(&pdev->dev,
1792 "Platform data must supply select_slot function\n");
1793 ret = -ENODEV;
1794 goto err_freehost;
1795 }
1796
1797 if (!pdata->bus_hz) {
1798 dev_err(&pdev->dev,
1799 "Platform data must supply bus speed\n");
1800 ret = -ENODEV;
1801 goto err_freehost;
1802 }
1803
1804 host->bus_hz = pdata->bus_hz;
1805 host->quirks = pdata->quirks;
1806
1807 spin_lock_init(&host->lock);
1808 INIT_LIST_HEAD(&host->queue);
1809
1810 ret = -ENOMEM;
1811 host->regs = ioremap(regs->start, regs->end - regs->start + 1);
1812 if (!host->regs)
1813 goto err_freehost;
1814
1815 host->dma_ops = pdata->dma_ops;
1816 dw_mci_init_dma(host);
1817
1818 /*
1819 * Get the host data width - this assumes that HCON has been set with
1820 * the correct values.
1821 */
1822 i = (mci_readl(host, HCON) >> 7) & 0x7;
1823 if (!i) {
1824 host->push_data = dw_mci_push_data16;
1825 host->pull_data = dw_mci_pull_data16;
1826 width = 16;
1827 host->data_shift = 1;
1828 } else if (i == 2) {
1829 host->push_data = dw_mci_push_data64;
1830 host->pull_data = dw_mci_pull_data64;
1831 width = 64;
1832 host->data_shift = 3;
1833 } else {
1834 /* Check for a reserved value, and warn if it is */
1835 WARN((i != 1),
1836 "HCON reports a reserved host data width!\n"
1837 "Defaulting to 32-bit access.\n");
1838 host->push_data = dw_mci_push_data32;
1839 host->pull_data = dw_mci_pull_data32;
1840 width = 32;
1841 host->data_shift = 2;
1842 }
1843
1844 /* Reset all blocks */
1845 if (!mci_wait_reset(&pdev->dev, host)) {
1846 ret = -ENODEV;
1847 goto err_dmaunmap;
1848 }
1849
1850 /* Clear the interrupts for the host controller */
1851 mci_writel(host, RINTSTS, 0xFFFFFFFF);
1852 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
1853
1854 /* Put in max timeout */
1855 mci_writel(host, TMOUT, 0xFFFFFFFF);
1856
1857 /*
1858 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
1859 * Tx Mark = fifo_size / 2 DMA Size = 8
1860 */
James Hoganb86d8252011-06-24 13:57:18 +01001861 if (!host->pdata->fifo_depth) {
1862 /*
1863 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
1864 * have been overwritten by the bootloader, just like we're
1865 * about to do, so if you know the value for your hardware, you
1866 * should put it in the platform data.
1867 */
1868 fifo_size = mci_readl(host, FIFOTH);
1869 fifo_size = 1 + ((fifo_size >> 16) & 0x7ff);
1870 } else {
1871 fifo_size = host->pdata->fifo_depth;
1872 }
1873 host->fifo_depth = fifo_size;
Jaehoon Chunge61cf112011-03-17 20:32:33 +09001874 host->fifoth_val = ((0x2 << 28) | ((fifo_size/2 - 1) << 16) |
1875 ((fifo_size/2) << 0));
1876 mci_writel(host, FIFOTH, host->fifoth_val);
Will Newtonf95f3852011-01-02 01:11:59 -05001877
1878 /* disable clock to CIU */
1879 mci_writel(host, CLKENA, 0);
1880 mci_writel(host, CLKSRC, 0);
1881
1882 tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
James Hogan1791b13e2011-06-24 13:55:55 +01001883 dw_mci_card_workqueue = alloc_workqueue("dw-mci-card",
1884 WQ_MEM_RECLAIM | WQ_NON_REENTRANT, 1);
1885 if (!dw_mci_card_workqueue)
1886 goto err_dmaunmap;
1887 INIT_WORK(&host->card_work, dw_mci_work_routine_card);
Will Newtonf95f3852011-01-02 01:11:59 -05001888
1889 ret = request_irq(irq, dw_mci_interrupt, 0, "dw-mci", host);
1890 if (ret)
James Hogan1791b13e2011-06-24 13:55:55 +01001891 goto err_workqueue;
Will Newtonf95f3852011-01-02 01:11:59 -05001892
1893 platform_set_drvdata(pdev, host);
1894
1895 if (host->pdata->num_slots)
1896 host->num_slots = host->pdata->num_slots;
1897 else
1898 host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
1899
1900 /* We need at least one slot to succeed */
1901 for (i = 0; i < host->num_slots; i++) {
1902 ret = dw_mci_init_slot(host, i);
1903 if (ret) {
1904 ret = -ENODEV;
1905 goto err_init_slot;
1906 }
1907 }
1908
1909 /*
1910 * Enable interrupts for command done, data over, data empty, card det,
1911 * receive ready and error such as transmit, receive timeout, crc error
1912 */
1913 mci_writel(host, RINTSTS, 0xFFFFFFFF);
1914 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
1915 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
1916 DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
1917 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
1918
1919 dev_info(&pdev->dev, "DW MMC controller at irq %d, "
James Hoganb86d8252011-06-24 13:57:18 +01001920 "%d bit host data width, "
1921 "%u deep fifo\n",
1922 irq, width, fifo_size);
Will Newtonf95f3852011-01-02 01:11:59 -05001923 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
1924 dev_info(&pdev->dev, "Internal DMAC interrupt fix enabled.\n");
1925
1926 return 0;
1927
1928err_init_slot:
1929 /* De-init any initialized slots */
1930 while (i > 0) {
1931 if (host->slot[i])
1932 dw_mci_cleanup_slot(host->slot[i], i);
1933 i--;
1934 }
1935 free_irq(irq, host);
1936
James Hogan1791b13e2011-06-24 13:55:55 +01001937err_workqueue:
1938 destroy_workqueue(dw_mci_card_workqueue);
1939
Will Newtonf95f3852011-01-02 01:11:59 -05001940err_dmaunmap:
1941 if (host->use_dma && host->dma_ops->exit)
1942 host->dma_ops->exit(host);
1943 dma_free_coherent(&host->pdev->dev, PAGE_SIZE,
1944 host->sg_cpu, host->sg_dma);
1945 iounmap(host->regs);
1946
Jaehoon Chungc07946a2011-02-25 11:08:14 +09001947 if (host->vmmc) {
1948 regulator_disable(host->vmmc);
1949 regulator_put(host->vmmc);
1950 }
1951
1952
Will Newtonf95f3852011-01-02 01:11:59 -05001953err_freehost:
1954 kfree(host);
1955 return ret;
1956}
1957
1958static int __exit dw_mci_remove(struct platform_device *pdev)
1959{
1960 struct dw_mci *host = platform_get_drvdata(pdev);
1961 int i;
1962
1963 mci_writel(host, RINTSTS, 0xFFFFFFFF);
1964 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
1965
1966 platform_set_drvdata(pdev, NULL);
1967
1968 for (i = 0; i < host->num_slots; i++) {
1969 dev_dbg(&pdev->dev, "remove slot %d\n", i);
1970 if (host->slot[i])
1971 dw_mci_cleanup_slot(host->slot[i], i);
1972 }
1973
1974 /* disable clock to CIU */
1975 mci_writel(host, CLKENA, 0);
1976 mci_writel(host, CLKSRC, 0);
1977
1978 free_irq(platform_get_irq(pdev, 0), host);
James Hogan1791b13e2011-06-24 13:55:55 +01001979 destroy_workqueue(dw_mci_card_workqueue);
Will Newtonf95f3852011-01-02 01:11:59 -05001980 dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
1981
1982 if (host->use_dma && host->dma_ops->exit)
1983 host->dma_ops->exit(host);
1984
Jaehoon Chungc07946a2011-02-25 11:08:14 +09001985 if (host->vmmc) {
1986 regulator_disable(host->vmmc);
1987 regulator_put(host->vmmc);
1988 }
1989
Will Newtonf95f3852011-01-02 01:11:59 -05001990 iounmap(host->regs);
1991
1992 kfree(host);
1993 return 0;
1994}
1995
1996#ifdef CONFIG_PM
1997/*
1998 * TODO: we should probably disable the clock to the card in the suspend path.
1999 */
2000static int dw_mci_suspend(struct platform_device *pdev, pm_message_t mesg)
2001{
2002 int i, ret;
2003 struct dw_mci *host = platform_get_drvdata(pdev);
2004
2005 for (i = 0; i < host->num_slots; i++) {
2006 struct dw_mci_slot *slot = host->slot[i];
2007 if (!slot)
2008 continue;
2009 ret = mmc_suspend_host(slot->mmc);
2010 if (ret < 0) {
2011 while (--i >= 0) {
2012 slot = host->slot[i];
2013 if (slot)
2014 mmc_resume_host(host->slot[i]->mmc);
2015 }
2016 return ret;
2017 }
2018 }
2019
Jaehoon Chungc07946a2011-02-25 11:08:14 +09002020 if (host->vmmc)
2021 regulator_disable(host->vmmc);
2022
Will Newtonf95f3852011-01-02 01:11:59 -05002023 return 0;
2024}
2025
2026static int dw_mci_resume(struct platform_device *pdev)
2027{
2028 int i, ret;
2029 struct dw_mci *host = platform_get_drvdata(pdev);
2030
Jaehoon Chung1d6c4e02011-05-11 15:52:39 +09002031 if (host->vmmc)
2032 regulator_enable(host->vmmc);
2033
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002034 if (host->dma_ops->init)
2035 host->dma_ops->init(host);
2036
2037 if (!mci_wait_reset(&pdev->dev, host)) {
2038 ret = -ENODEV;
2039 return ret;
2040 }
2041
2042 /* Restore the old value at FIFOTH register */
2043 mci_writel(host, FIFOTH, host->fifoth_val);
2044
2045 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2046 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
2047 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
2048 DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
2049 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
2050
Will Newtonf95f3852011-01-02 01:11:59 -05002051 for (i = 0; i < host->num_slots; i++) {
2052 struct dw_mci_slot *slot = host->slot[i];
2053 if (!slot)
2054 continue;
2055 ret = mmc_resume_host(host->slot[i]->mmc);
2056 if (ret < 0)
2057 return ret;
2058 }
2059
2060 return 0;
2061}
2062#else
2063#define dw_mci_suspend NULL
2064#define dw_mci_resume NULL
2065#endif /* CONFIG_PM */
2066
2067static struct platform_driver dw_mci_driver = {
2068 .remove = __exit_p(dw_mci_remove),
2069 .suspend = dw_mci_suspend,
2070 .resume = dw_mci_resume,
2071 .driver = {
2072 .name = "dw_mmc",
2073 },
2074};
2075
2076static int __init dw_mci_init(void)
2077{
2078 return platform_driver_probe(&dw_mci_driver, dw_mci_probe);
2079}
2080
2081static void __exit dw_mci_exit(void)
2082{
2083 platform_driver_unregister(&dw_mci_driver);
2084}
2085
2086module_init(dw_mci_init);
2087module_exit(dw_mci_exit);
2088
2089MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
2090MODULE_AUTHOR("NXP Semiconductor VietNam");
2091MODULE_AUTHOR("Imagination Technologies Ltd");
2092MODULE_LICENSE("GPL v2");