2 * linux/drivers/mmc/s3cmci.h - Samsung S3C MCI driver
4 * Copyright (C) 2004-2006 maintech GmbH, Thomas Kleffel <tk@maintech.de>
6 * Current driver maintained by Ben Dooks and Simtec Electronics
7 * Copyright (C) 2008 Simtec Electronics <ben-linux@fluff.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/clk.h>
17 #include <linux/mmc/host.h>
18 #include <linux/platform_device.h>
19 #include <linux/cpufreq.h>
20 #include <linux/debugfs.h>
21 #include <linux/seq_file.h>
22 #include <linux/gpio.h>
23 #include <linux/irq.h>
27 #include <mach/gpio-samsung.h>
29 #include <linux/platform_data/mmc-s3cmci.h>
33 #define DRIVER_NAME "s3c-mci"
35 #define S3C2410_SDICON (0x00)
36 #define S3C2410_SDIPRE (0x04)
37 #define S3C2410_SDICMDARG (0x08)
38 #define S3C2410_SDICMDCON (0x0C)
39 #define S3C2410_SDICMDSTAT (0x10)
40 #define S3C2410_SDIRSP0 (0x14)
41 #define S3C2410_SDIRSP1 (0x18)
42 #define S3C2410_SDIRSP2 (0x1C)
43 #define S3C2410_SDIRSP3 (0x20)
44 #define S3C2410_SDITIMER (0x24)
45 #define S3C2410_SDIBSIZE (0x28)
46 #define S3C2410_SDIDCON (0x2C)
47 #define S3C2410_SDIDCNT (0x30)
48 #define S3C2410_SDIDSTA (0x34)
49 #define S3C2410_SDIFSTA (0x38)
51 #define S3C2410_SDIDATA (0x3C)
52 #define S3C2410_SDIIMSK (0x40)
54 #define S3C2440_SDIDATA (0x40)
55 #define S3C2440_SDIIMSK (0x3C)
57 #define S3C2440_SDICON_SDRESET (1 << 8)
58 #define S3C2410_SDICON_SDIOIRQ (1 << 3)
59 #define S3C2410_SDICON_FIFORESET (1 << 1)
60 #define S3C2410_SDICON_CLOCKTYPE (1 << 0)
62 #define S3C2410_SDICMDCON_LONGRSP (1 << 10)
63 #define S3C2410_SDICMDCON_WAITRSP (1 << 9)
64 #define S3C2410_SDICMDCON_CMDSTART (1 << 8)
65 #define S3C2410_SDICMDCON_SENDERHOST (1 << 6)
66 #define S3C2410_SDICMDCON_INDEX (0x3f)
68 #define S3C2410_SDICMDSTAT_CRCFAIL (1 << 12)
69 #define S3C2410_SDICMDSTAT_CMDSENT (1 << 11)
70 #define S3C2410_SDICMDSTAT_CMDTIMEOUT (1 << 10)
71 #define S3C2410_SDICMDSTAT_RSPFIN (1 << 9)
73 #define S3C2440_SDIDCON_DS_WORD (2 << 22)
74 #define S3C2410_SDIDCON_TXAFTERRESP (1 << 20)
75 #define S3C2410_SDIDCON_RXAFTERCMD (1 << 19)
76 #define S3C2410_SDIDCON_BLOCKMODE (1 << 17)
77 #define S3C2410_SDIDCON_WIDEBUS (1 << 16)
78 #define S3C2410_SDIDCON_DMAEN (1 << 15)
79 #define S3C2410_SDIDCON_STOP (1 << 14)
80 #define S3C2440_SDIDCON_DATSTART (1 << 14)
82 #define S3C2410_SDIDCON_XFER_RXSTART (2 << 12)
83 #define S3C2410_SDIDCON_XFER_TXSTART (3 << 12)
85 #define S3C2410_SDIDCON_BLKNUM_MASK (0xFFF)
87 #define S3C2410_SDIDSTA_SDIOIRQDETECT (1 << 9)
88 #define S3C2410_SDIDSTA_FIFOFAIL (1 << 8)
89 #define S3C2410_SDIDSTA_CRCFAIL (1 << 7)
90 #define S3C2410_SDIDSTA_RXCRCFAIL (1 << 6)
91 #define S3C2410_SDIDSTA_DATATIMEOUT (1 << 5)
92 #define S3C2410_SDIDSTA_XFERFINISH (1 << 4)
93 #define S3C2410_SDIDSTA_TXDATAON (1 << 1)
94 #define S3C2410_SDIDSTA_RXDATAON (1 << 0)
96 #define S3C2440_SDIFSTA_FIFORESET (1 << 16)
97 #define S3C2440_SDIFSTA_FIFOFAIL (3 << 14)
98 #define S3C2410_SDIFSTA_TFDET (1 << 13)
99 #define S3C2410_SDIFSTA_RFDET (1 << 12)
100 #define S3C2410_SDIFSTA_COUNTMASK (0x7f)
102 #define S3C2410_SDIIMSK_RESPONSECRC (1 << 17)
103 #define S3C2410_SDIIMSK_CMDSENT (1 << 16)
104 #define S3C2410_SDIIMSK_CMDTIMEOUT (1 << 15)
105 #define S3C2410_SDIIMSK_RESPONSEND (1 << 14)
106 #define S3C2410_SDIIMSK_SDIOIRQ (1 << 12)
107 #define S3C2410_SDIIMSK_FIFOFAIL (1 << 11)
108 #define S3C2410_SDIIMSK_CRCSTATUS (1 << 10)
109 #define S3C2410_SDIIMSK_DATACRC (1 << 9)
110 #define S3C2410_SDIIMSK_DATATIMEOUT (1 << 8)
111 #define S3C2410_SDIIMSK_DATAFINISH (1 << 7)
112 #define S3C2410_SDIIMSK_TXFIFOHALF (1 << 4)
113 #define S3C2410_SDIIMSK_RXFIFOLAST (1 << 2)
114 #define S3C2410_SDIIMSK_RXFIFOHALF (1 << 0)
118 dbg_debug = (1 << 1),
128 static const int dbgmap_err = dbg_fail;
129 static const int dbgmap_info = dbg_info | dbg_conf;
130 static const int dbgmap_debug = dbg_err | dbg_debug;
132 #define dbg(host, channels, args...) \
134 if (dbgmap_err & channels) \
135 dev_err(&host->pdev->dev, args); \
136 else if (dbgmap_info & channels) \
137 dev_info(&host->pdev->dev, args); \
138 else if (dbgmap_debug & channels) \
139 dev_dbg(&host->pdev->dev, args); \
142 static struct s3c2410_dma_client s3cmci_dma_client = {
146 static void finalize_request(struct s3cmci_host *host);
147 static void s3cmci_send_request(struct mmc_host *mmc);
148 static void s3cmci_reset(struct s3cmci_host *host);
150 #ifdef CONFIG_MMC_DEBUG
152 static void dbg_dumpregs(struct s3cmci_host *host, char *prefix)
154 u32 con, pre, cmdarg, cmdcon, cmdsta, r0, r1, r2, r3, timer, bsize;
155 u32 datcon, datcnt, datsta, fsta, imask;
157 con = readl(host->base + S3C2410_SDICON);
158 pre = readl(host->base + S3C2410_SDIPRE);
159 cmdarg = readl(host->base + S3C2410_SDICMDARG);
160 cmdcon = readl(host->base + S3C2410_SDICMDCON);
161 cmdsta = readl(host->base + S3C2410_SDICMDSTAT);
162 r0 = readl(host->base + S3C2410_SDIRSP0);
163 r1 = readl(host->base + S3C2410_SDIRSP1);
164 r2 = readl(host->base + S3C2410_SDIRSP2);
165 r3 = readl(host->base + S3C2410_SDIRSP3);
166 timer = readl(host->base + S3C2410_SDITIMER);
167 bsize = readl(host->base + S3C2410_SDIBSIZE);
168 datcon = readl(host->base + S3C2410_SDIDCON);
169 datcnt = readl(host->base + S3C2410_SDIDCNT);
170 datsta = readl(host->base + S3C2410_SDIDSTA);
171 fsta = readl(host->base + S3C2410_SDIFSTA);
172 imask = readl(host->base + host->sdiimsk);
174 dbg(host, dbg_debug, "%s CON:[%08x] PRE:[%08x] TMR:[%08x]\n",
175 prefix, con, pre, timer);
177 dbg(host, dbg_debug, "%s CCON:[%08x] CARG:[%08x] CSTA:[%08x]\n",
178 prefix, cmdcon, cmdarg, cmdsta);
180 dbg(host, dbg_debug, "%s DCON:[%08x] FSTA:[%08x]"
181 " DSTA:[%08x] DCNT:[%08x]\n",
182 prefix, datcon, fsta, datsta, datcnt);
184 dbg(host, dbg_debug, "%s R0:[%08x] R1:[%08x]"
185 " R2:[%08x] R3:[%08x]\n",
186 prefix, r0, r1, r2, r3);
189 static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
192 snprintf(host->dbgmsg_cmd, 300,
193 "#%u%s op:%i arg:0x%08x flags:0x08%x retries:%u",
194 host->ccnt, (stop ? " (STOP)" : ""),
195 cmd->opcode, cmd->arg, cmd->flags, cmd->retries);
198 snprintf(host->dbgmsg_dat, 300,
199 "#%u bsize:%u blocks:%u bytes:%u",
200 host->dcnt, cmd->data->blksz,
202 cmd->data->blocks * cmd->data->blksz);
204 host->dbgmsg_dat[0] = '\0';
208 static void dbg_dumpcmd(struct s3cmci_host *host, struct mmc_command *cmd,
211 unsigned int dbglvl = fail ? dbg_fail : dbg_debug;
216 if (cmd->error == 0) {
217 dbg(host, dbglvl, "CMD[OK] %s R0:0x%08x\n",
218 host->dbgmsg_cmd, cmd->resp[0]);
220 dbg(host, dbglvl, "CMD[ERR %i] %s Status:%s\n",
221 cmd->error, host->dbgmsg_cmd, host->status);
227 if (cmd->data->error == 0) {
228 dbg(host, dbglvl, "DAT[OK] %s\n", host->dbgmsg_dat);
230 dbg(host, dbglvl, "DAT[ERR %i] %s DCNT:0x%08x\n",
231 cmd->data->error, host->dbgmsg_dat,
232 readl(host->base + S3C2410_SDIDCNT));
236 static void dbg_dumpcmd(struct s3cmci_host *host,
237 struct mmc_command *cmd, int fail) { }
239 static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
242 static void dbg_dumpregs(struct s3cmci_host *host, char *prefix) { }
244 #endif /* CONFIG_MMC_DEBUG */
247 * s3cmci_host_usedma - return whether the host is using dma or pio
248 * @host: The host state
250 * Return true if the host is using DMA to transfer data, else false
251 * to use PIO mode. Will return static data depending on the driver
254 static inline bool s3cmci_host_usedma(struct s3cmci_host *host)
256 #ifdef CONFIG_MMC_S3C_PIO
258 #elif defined(CONFIG_MMC_S3C_DMA)
266 * s3cmci_host_canpio - return true if host has pio code available
268 * Return true if the driver has been compiled with the PIO support code
271 static inline bool s3cmci_host_canpio(void)
273 #ifdef CONFIG_MMC_S3C_PIO
280 static inline u32 enable_imask(struct s3cmci_host *host, u32 imask)
284 newmask = readl(host->base + host->sdiimsk);
287 writel(newmask, host->base + host->sdiimsk);
292 static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
296 newmask = readl(host->base + host->sdiimsk);
299 writel(newmask, host->base + host->sdiimsk);
304 static inline void clear_imask(struct s3cmci_host *host)
306 u32 mask = readl(host->base + host->sdiimsk);
308 /* preserve the SDIO IRQ mask state */
309 mask &= S3C2410_SDIIMSK_SDIOIRQ;
310 writel(mask, host->base + host->sdiimsk);
314 * s3cmci_check_sdio_irq - test whether the SDIO IRQ is being signalled
315 * @host: The host to check.
317 * Test to see if the SDIO interrupt is being signalled in case the
318 * controller has failed to re-detect a card interrupt. Read GPE8 and
319 * see if it is low and if so, signal a SDIO interrupt.
321 * This is currently called if a request is finished (we assume that the
322 * bus is now idle) and when the SDIO IRQ is enabled in case the IRQ is
323 * already being indicated.
325 static void s3cmci_check_sdio_irq(struct s3cmci_host *host)
327 if (host->sdio_irqen) {
328 if (gpio_get_value(S3C2410_GPE(8)) == 0) {
329 pr_debug("%s: signalling irq\n", __func__);
330 mmc_signal_sdio_irq(host->mmc);
335 static inline int get_data_buffer(struct s3cmci_host *host,
336 u32 *bytes, u32 **pointer)
338 struct scatterlist *sg;
340 if (host->pio_active == XFER_NONE)
343 if ((!host->mrq) || (!host->mrq->data))
346 if (host->pio_sgptr >= host->mrq->data->sg_len) {
347 dbg(host, dbg_debug, "no more buffers (%i/%i)\n",
348 host->pio_sgptr, host->mrq->data->sg_len);
351 sg = &host->mrq->data->sg[host->pio_sgptr];
354 *pointer = sg_virt(sg);
358 dbg(host, dbg_sg, "new buffer (%i/%i)\n",
359 host->pio_sgptr, host->mrq->data->sg_len);
364 static inline u32 fifo_count(struct s3cmci_host *host)
366 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
368 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
372 static inline u32 fifo_free(struct s3cmci_host *host)
374 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
376 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
377 return 63 - fifostat;
381 * s3cmci_enable_irq - enable IRQ, after having disabled it.
382 * @host: The device state.
383 * @more: True if more IRQs are expected from transfer.
385 * Enable the main IRQ if needed after it has been disabled.
387 * The IRQ can be one of the following states:
388 * - disabled during IDLE
389 * - disabled whilst processing data
390 * - enabled during transfer
391 * - enabled whilst awaiting SDIO interrupt detection
393 static void s3cmci_enable_irq(struct s3cmci_host *host, bool more)
398 local_irq_save(flags);
400 host->irq_enabled = more;
401 host->irq_disabled = false;
403 enable = more | host->sdio_irqen;
405 if (host->irq_state != enable) {
406 host->irq_state = enable;
409 enable_irq(host->irq);
411 disable_irq(host->irq);
414 local_irq_restore(flags);
420 static void s3cmci_disable_irq(struct s3cmci_host *host, bool transfer)
424 local_irq_save(flags);
426 /* pr_debug("%s: transfer %d\n", __func__, transfer); */
428 host->irq_disabled = transfer;
430 if (transfer && host->irq_state) {
431 host->irq_state = false;
432 disable_irq(host->irq);
435 local_irq_restore(flags);
438 static void do_pio_read(struct s3cmci_host *host)
444 void __iomem *from_ptr;
446 /* write real prescaler to host, it might be set slow to fix */
447 writel(host->prescaler, host->base + S3C2410_SDIPRE);
449 from_ptr = host->base + host->sdidata;
451 while ((fifo = fifo_count(host))) {
452 if (!host->pio_bytes) {
453 res = get_data_buffer(host, &host->pio_bytes,
456 host->pio_active = XFER_NONE;
457 host->complete_what = COMPLETION_FINALIZE;
459 dbg(host, dbg_pio, "pio_read(): "
460 "complete (no more data).\n");
465 "pio_read(): new target: [%i]@[%p]\n",
466 host->pio_bytes, host->pio_ptr);
470 "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
471 fifo, host->pio_bytes,
472 readl(host->base + S3C2410_SDIDCNT));
474 /* If we have reached the end of the block, we can
475 * read a word and get 1 to 3 bytes. If we in the
476 * middle of the block, we have to read full words,
477 * otherwise we will write garbage, so round down to
478 * an even multiple of 4. */
479 if (fifo >= host->pio_bytes)
480 fifo = host->pio_bytes;
484 host->pio_bytes -= fifo;
485 host->pio_count += fifo;
487 fifo_words = fifo >> 2;
490 *ptr++ = readl(from_ptr);
495 u32 data = readl(from_ptr);
496 u8 *p = (u8 *)host->pio_ptr;
505 if (!host->pio_bytes) {
506 res = get_data_buffer(host, &host->pio_bytes, &host->pio_ptr);
509 "pio_read(): complete (no more buffers).\n");
510 host->pio_active = XFER_NONE;
511 host->complete_what = COMPLETION_FINALIZE;
518 S3C2410_SDIIMSK_RXFIFOHALF | S3C2410_SDIIMSK_RXFIFOLAST);
521 static void do_pio_write(struct s3cmci_host *host)
523 void __iomem *to_ptr;
528 to_ptr = host->base + host->sdidata;
530 while ((fifo = fifo_free(host)) > 3) {
531 if (!host->pio_bytes) {
532 res = get_data_buffer(host, &host->pio_bytes,
536 "pio_write(): complete (no more data).\n");
537 host->pio_active = XFER_NONE;
543 "pio_write(): new source: [%i]@[%p]\n",
544 host->pio_bytes, host->pio_ptr);
548 /* If we have reached the end of the block, we have to
549 * write exactly the remaining number of bytes. If we
550 * in the middle of the block, we have to write full
551 * words, so round down to an even multiple of 4. */
552 if (fifo >= host->pio_bytes)
553 fifo = host->pio_bytes;
557 host->pio_bytes -= fifo;
558 host->pio_count += fifo;
560 fifo = (fifo + 3) >> 2;
563 writel(*ptr++, to_ptr);
567 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
570 static void pio_tasklet(unsigned long data)
572 struct s3cmci_host *host = (struct s3cmci_host *) data;
574 s3cmci_disable_irq(host, true);
576 if (host->pio_active == XFER_WRITE)
579 if (host->pio_active == XFER_READ)
582 if (host->complete_what == COMPLETION_FINALIZE) {
584 if (host->pio_active != XFER_NONE) {
585 dbg(host, dbg_err, "unfinished %s "
586 "- pio_count:[%u] pio_bytes:[%u]\n",
587 (host->pio_active == XFER_READ) ? "read" : "write",
588 host->pio_count, host->pio_bytes);
591 host->mrq->data->error = -EINVAL;
594 s3cmci_enable_irq(host, false);
595 finalize_request(host);
597 s3cmci_enable_irq(host, true);
601 * ISR for SDI Interface IRQ
602 * Communication between driver and ISR works as follows:
603 * host->mrq points to current request
604 * host->complete_what Indicates when the request is considered done
605 * COMPLETION_CMDSENT when the command was sent
606 * COMPLETION_RSPFIN when a response was received
607 * COMPLETION_XFERFINISH when the data transfer is finished
608 * COMPLETION_XFERFINISH_RSPFIN both of the above.
609 * host->complete_request is the completion-object the driver waits for
611 * 1) Driver sets up host->mrq and host->complete_what
612 * 2) Driver prepares the transfer
613 * 3) Driver enables interrupts
614 * 4) Driver starts transfer
615 * 5) Driver waits for host->complete_rquest
616 * 6) ISR checks for request status (errors and success)
617 * 6) ISR sets host->mrq->cmd->error and host->mrq->data->error
618 * 7) ISR completes host->complete_request
619 * 8) ISR disables interrupts
620 * 9) Driver wakes up and takes care of the request
622 * Note: "->error"-fields are expected to be set to 0 before the request
623 * was issued by mmc.c - therefore they are only set, when an error
627 static irqreturn_t s3cmci_irq(int irq, void *dev_id)
629 struct s3cmci_host *host = dev_id;
630 struct mmc_command *cmd;
631 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
632 u32 mci_cclear = 0, mci_dclear;
633 unsigned long iflags;
635 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
636 mci_imsk = readl(host->base + host->sdiimsk);
638 if (mci_dsta & S3C2410_SDIDSTA_SDIOIRQDETECT) {
639 if (mci_imsk & S3C2410_SDIIMSK_SDIOIRQ) {
640 mci_dclear = S3C2410_SDIDSTA_SDIOIRQDETECT;
641 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
643 mmc_signal_sdio_irq(host->mmc);
648 spin_lock_irqsave(&host->complete_lock, iflags);
650 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
651 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
652 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
655 if ((host->complete_what == COMPLETION_NONE) ||
656 (host->complete_what == COMPLETION_FINALIZE)) {
657 host->status = "nothing to complete";
663 host->status = "no active mrq";
668 cmd = host->cmd_is_stop ? host->mrq->stop : host->mrq->cmd;
671 host->status = "no active cmd";
676 if (!s3cmci_host_usedma(host)) {
677 if ((host->pio_active == XFER_WRITE) &&
678 (mci_fsta & S3C2410_SDIFSTA_TFDET)) {
680 disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
681 tasklet_schedule(&host->pio_tasklet);
682 host->status = "pio tx";
685 if ((host->pio_active == XFER_READ) &&
686 (mci_fsta & S3C2410_SDIFSTA_RFDET)) {
689 S3C2410_SDIIMSK_RXFIFOHALF |
690 S3C2410_SDIIMSK_RXFIFOLAST);
692 tasklet_schedule(&host->pio_tasklet);
693 host->status = "pio rx";
697 if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {
698 dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");
699 cmd->error = -ETIMEDOUT;
700 host->status = "error: command timeout";
704 if (mci_csta & S3C2410_SDICMDSTAT_CMDSENT) {
705 if (host->complete_what == COMPLETION_CMDSENT) {
706 host->status = "ok: command sent";
710 mci_cclear |= S3C2410_SDICMDSTAT_CMDSENT;
713 if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {
714 if (cmd->flags & MMC_RSP_CRC) {
715 if (host->mrq->cmd->flags & MMC_RSP_136) {
717 "fixup: ignore CRC fail with long rsp\n");
719 /* note, we used to fail the transfer
720 * here, but it seems that this is just
721 * the hardware getting it wrong.
723 * cmd->error = -EILSEQ;
724 * host->status = "error: bad command crc";
725 * goto fail_transfer;
730 mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;
733 if (mci_csta & S3C2410_SDICMDSTAT_RSPFIN) {
734 if (host->complete_what == COMPLETION_RSPFIN) {
735 host->status = "ok: command response received";
739 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
740 host->complete_what = COMPLETION_XFERFINISH;
742 mci_cclear |= S3C2410_SDICMDSTAT_RSPFIN;
745 /* errors handled after this point are only relevant
746 when a data transfer is in progress */
749 goto clear_status_bits;
751 /* Check for FIFO failure */
753 if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {
754 dbg(host, dbg_err, "FIFO failure\n");
755 host->mrq->data->error = -EILSEQ;
756 host->status = "error: 2440 fifo failure";
760 if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {
761 dbg(host, dbg_err, "FIFO failure\n");
762 cmd->data->error = -EILSEQ;
763 host->status = "error: fifo failure";
768 if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {
769 dbg(host, dbg_err, "bad data crc (outgoing)\n");
770 cmd->data->error = -EILSEQ;
771 host->status = "error: bad data crc (outgoing)";
775 if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {
776 dbg(host, dbg_err, "bad data crc (incoming)\n");
777 cmd->data->error = -EILSEQ;
778 host->status = "error: bad data crc (incoming)";
782 if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {
783 dbg(host, dbg_err, "data timeout\n");
784 cmd->data->error = -ETIMEDOUT;
785 host->status = "error: data timeout";
789 if (mci_dsta & S3C2410_SDIDSTA_XFERFINISH) {
790 if (host->complete_what == COMPLETION_XFERFINISH) {
791 host->status = "ok: data transfer completed";
795 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
796 host->complete_what = COMPLETION_RSPFIN;
798 mci_dclear |= S3C2410_SDIDSTA_XFERFINISH;
802 writel(mci_cclear, host->base + S3C2410_SDICMDSTAT);
803 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
808 host->pio_active = XFER_NONE;
811 host->complete_what = COMPLETION_FINALIZE;
814 tasklet_schedule(&host->pio_tasklet);
820 "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",
821 mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->status);
823 spin_unlock_irqrestore(&host->complete_lock, iflags);
829 * ISR for the CardDetect Pin
832 static irqreturn_t s3cmci_irq_cd(int irq, void *dev_id)
834 struct s3cmci_host *host = (struct s3cmci_host *)dev_id;
836 dbg(host, dbg_irq, "card detect\n");
838 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
843 static void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch,
844 void *buf_id, int size,
845 enum s3c2410_dma_buffresult result)
847 struct s3cmci_host *host = buf_id;
848 unsigned long iflags;
849 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt;
851 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
852 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
853 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
854 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
857 BUG_ON(!host->mrq->data);
858 BUG_ON(!host->dmatogo);
860 spin_lock_irqsave(&host->complete_lock, iflags);
862 if (result != S3C2410_RES_OK) {
863 dbg(host, dbg_fail, "DMA FAILED: csta=0x%08x dsta=0x%08x "
864 "fsta=0x%08x dcnt:0x%08x result:0x%08x toGo:%u\n",
865 mci_csta, mci_dsta, mci_fsta,
866 mci_dcnt, result, host->dmatogo);
873 dbg(host, dbg_dma, "DMA DONE Size:%i DSTA:[%08x] "
874 "DCNT:[%08x] toGo:%u\n",
875 size, mci_dsta, mci_dcnt, host->dmatogo);
880 dbg(host, dbg_dma, "DMA FINISHED Size:%i DSTA:%08x DCNT:%08x\n",
881 size, mci_dsta, mci_dcnt);
883 host->dma_complete = 1;
884 host->complete_what = COMPLETION_FINALIZE;
887 tasklet_schedule(&host->pio_tasklet);
888 spin_unlock_irqrestore(&host->complete_lock, iflags);
892 host->mrq->data->error = -EINVAL;
893 host->complete_what = COMPLETION_FINALIZE;
899 static void finalize_request(struct s3cmci_host *host)
901 struct mmc_request *mrq = host->mrq;
902 struct mmc_command *cmd;
903 int debug_as_failure = 0;
905 if (host->complete_what != COMPLETION_FINALIZE)
910 cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
912 if (cmd->data && (cmd->error == 0) &&
913 (cmd->data->error == 0)) {
914 if (s3cmci_host_usedma(host) && (!host->dma_complete)) {
915 dbg(host, dbg_dma, "DMA Missing (%d)!\n",
921 /* Read response from controller. */
922 cmd->resp[0] = readl(host->base + S3C2410_SDIRSP0);
923 cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1);
924 cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2);
925 cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3);
927 writel(host->prescaler, host->base + S3C2410_SDIPRE);
930 debug_as_failure = 1;
932 if (cmd->data && cmd->data->error)
933 debug_as_failure = 1;
935 dbg_dumpcmd(host, cmd, debug_as_failure);
937 /* Cleanup controller */
938 writel(0, host->base + S3C2410_SDICMDARG);
939 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
940 writel(0, host->base + S3C2410_SDICMDCON);
943 if (cmd->data && cmd->error)
944 cmd->data->error = cmd->error;
946 if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
947 host->cmd_is_stop = 1;
948 s3cmci_send_request(host->mmc);
952 /* If we have no data transfer we are finished here */
956 /* Calculate the amout of bytes transfer if there was no error */
957 if (mrq->data->error == 0) {
958 mrq->data->bytes_xfered =
959 (mrq->data->blocks * mrq->data->blksz);
961 mrq->data->bytes_xfered = 0;
964 /* If we had an error while transferring data we flush the
965 * DMA channel and the fifo to clear out any garbage. */
966 if (mrq->data->error != 0) {
967 if (s3cmci_host_usedma(host))
968 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
971 /* Clear failure register and reset fifo. */
972 writel(S3C2440_SDIFSTA_FIFORESET |
973 S3C2440_SDIFSTA_FIFOFAIL,
974 host->base + S3C2410_SDIFSTA);
979 mci_con = readl(host->base + S3C2410_SDICON);
980 mci_con |= S3C2410_SDICON_FIFORESET;
982 writel(mci_con, host->base + S3C2410_SDICON);
987 host->complete_what = COMPLETION_NONE;
990 s3cmci_check_sdio_irq(host);
991 mmc_request_done(host->mmc, mrq);
994 static void s3cmci_dma_setup(struct s3cmci_host *host,
995 enum dma_data_direction source)
997 static enum dma_data_direction last_source = -1;
1000 if (last_source == source)
1003 last_source = source;
1005 s3c2410_dma_devconfig(host->dma, source,
1006 host->mem->start + host->sdidata);
1009 s3c2410_dma_config(host->dma, 4);
1010 s3c2410_dma_set_buffdone_fn(host->dma,
1011 s3cmci_dma_done_callback);
1012 s3c2410_dma_setflags(host->dma, S3C2410_DMAF_AUTOSTART);
1017 static void s3cmci_send_command(struct s3cmci_host *host,
1018 struct mmc_command *cmd)
1022 imsk = S3C2410_SDIIMSK_CRCSTATUS | S3C2410_SDIIMSK_CMDTIMEOUT |
1023 S3C2410_SDIIMSK_RESPONSEND | S3C2410_SDIIMSK_CMDSENT |
1024 S3C2410_SDIIMSK_RESPONSECRC;
1026 enable_imask(host, imsk);
1029 host->complete_what = COMPLETION_XFERFINISH_RSPFIN;
1030 else if (cmd->flags & MMC_RSP_PRESENT)
1031 host->complete_what = COMPLETION_RSPFIN;
1033 host->complete_what = COMPLETION_CMDSENT;
1035 writel(cmd->arg, host->base + S3C2410_SDICMDARG);
1037 ccon = cmd->opcode & S3C2410_SDICMDCON_INDEX;
1038 ccon |= S3C2410_SDICMDCON_SENDERHOST | S3C2410_SDICMDCON_CMDSTART;
1040 if (cmd->flags & MMC_RSP_PRESENT)
1041 ccon |= S3C2410_SDICMDCON_WAITRSP;
1043 if (cmd->flags & MMC_RSP_136)
1044 ccon |= S3C2410_SDICMDCON_LONGRSP;
1046 writel(ccon, host->base + S3C2410_SDICMDCON);
1049 static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
1051 u32 dcon, imsk, stoptries = 3;
1053 /* write DCON register */
1056 writel(0, host->base + S3C2410_SDIDCON);
1060 if ((data->blksz & 3) != 0) {
1061 /* We cannot deal with unaligned blocks with more than
1062 * one block being transferred. */
1064 if (data->blocks > 1) {
1065 pr_warning("%s: can't do non-word sized block transfers (blksz %d)\n", __func__, data->blksz);
1070 while (readl(host->base + S3C2410_SDIDSTA) &
1071 (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {
1074 "mci_setup_data() transfer stillin progress.\n");
1076 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
1079 if ((stoptries--) == 0) {
1080 dbg_dumpregs(host, "DRF");
1085 dcon = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;
1087 if (s3cmci_host_usedma(host))
1088 dcon |= S3C2410_SDIDCON_DMAEN;
1090 if (host->bus_width == MMC_BUS_WIDTH_4)
1091 dcon |= S3C2410_SDIDCON_WIDEBUS;
1093 if (!(data->flags & MMC_DATA_STREAM))
1094 dcon |= S3C2410_SDIDCON_BLOCKMODE;
1096 if (data->flags & MMC_DATA_WRITE) {
1097 dcon |= S3C2410_SDIDCON_TXAFTERRESP;
1098 dcon |= S3C2410_SDIDCON_XFER_TXSTART;
1101 if (data->flags & MMC_DATA_READ) {
1102 dcon |= S3C2410_SDIDCON_RXAFTERCMD;
1103 dcon |= S3C2410_SDIDCON_XFER_RXSTART;
1107 dcon |= S3C2440_SDIDCON_DS_WORD;
1108 dcon |= S3C2440_SDIDCON_DATSTART;
1111 writel(dcon, host->base + S3C2410_SDIDCON);
1113 /* write BSIZE register */
1115 writel(data->blksz, host->base + S3C2410_SDIBSIZE);
1117 /* add to IMASK register */
1118 imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |
1119 S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;
1121 enable_imask(host, imsk);
1123 /* write TIMER register */
1126 writel(0x007FFFFF, host->base + S3C2410_SDITIMER);
1128 writel(0x0000FFFF, host->base + S3C2410_SDITIMER);
1130 /* FIX: set slow clock to prevent timeouts on read */
1131 if (data->flags & MMC_DATA_READ)
1132 writel(0xFF, host->base + S3C2410_SDIPRE);
1138 #define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)
1140 static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
1142 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
1144 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
1146 host->pio_sgptr = 0;
1147 host->pio_bytes = 0;
1148 host->pio_count = 0;
1149 host->pio_active = rw ? XFER_WRITE : XFER_READ;
1153 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
1155 enable_imask(host, S3C2410_SDIIMSK_RXFIFOHALF
1156 | S3C2410_SDIIMSK_RXFIFOLAST);
1162 static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
1165 int rw = data->flags & MMC_DATA_WRITE;
1167 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
1169 s3cmci_dma_setup(host, rw ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1170 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
1172 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1173 rw ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1178 host->dma_complete = 0;
1179 host->dmatogo = dma_len;
1181 for (i = 0; i < dma_len; i++) {
1184 dbg(host, dbg_dma, "enqueue %i: %08x@%u\n", i,
1185 sg_dma_address(&data->sg[i]),
1186 sg_dma_len(&data->sg[i]));
1188 res = s3c2410_dma_enqueue(host->dma, host,
1189 sg_dma_address(&data->sg[i]),
1190 sg_dma_len(&data->sg[i]));
1193 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
1198 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_START);
1203 static void s3cmci_send_request(struct mmc_host *mmc)
1205 struct s3cmci_host *host = mmc_priv(mmc);
1206 struct mmc_request *mrq = host->mrq;
1207 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
1210 prepare_dbgmsg(host, cmd, host->cmd_is_stop);
1212 /* Clear command, data and fifo status registers
1213 Fifo clear only necessary on 2440, but doesn't hurt on 2410
1215 writel(0xFFFFFFFF, host->base + S3C2410_SDICMDSTAT);
1216 writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA);
1217 writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA);
1220 int res = s3cmci_setup_data(host, cmd->data);
1225 dbg(host, dbg_err, "setup data error %d\n", res);
1227 cmd->data->error = res;
1229 mmc_request_done(mmc, mrq);
1233 if (s3cmci_host_usedma(host))
1234 res = s3cmci_prepare_dma(host, cmd->data);
1236 res = s3cmci_prepare_pio(host, cmd->data);
1239 dbg(host, dbg_err, "data prepare error %d\n", res);
1241 cmd->data->error = res;
1243 mmc_request_done(mmc, mrq);
1249 s3cmci_send_command(host, cmd);
1251 /* Enable Interrupt */
1252 s3cmci_enable_irq(host, true);
1255 static int s3cmci_card_present(struct mmc_host *mmc)
1257 struct s3cmci_host *host = mmc_priv(mmc);
1258 struct s3c24xx_mci_pdata *pdata = host->pdata;
1261 if (pdata->no_detect)
1264 ret = gpio_get_value(pdata->gpio_detect) ? 0 : 1;
1265 return ret ^ pdata->detect_invert;
1268 static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1270 struct s3cmci_host *host = mmc_priv(mmc);
1272 host->status = "mmc request";
1273 host->cmd_is_stop = 0;
1276 if (s3cmci_card_present(mmc) == 0) {
1277 dbg(host, dbg_err, "%s: no medium present\n", __func__);
1278 host->mrq->cmd->error = -ENOMEDIUM;
1279 mmc_request_done(mmc, mrq);
1281 s3cmci_send_request(mmc);
1284 static void s3cmci_set_clk(struct s3cmci_host *host, struct mmc_ios *ios)
1289 for (mci_psc = 0; mci_psc < 255; mci_psc++) {
1290 host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));
1292 if (host->real_rate <= ios->clock)
1299 host->prescaler = mci_psc;
1300 writel(host->prescaler, host->base + S3C2410_SDIPRE);
1302 /* If requested clock is 0, real_rate will be 0, too */
1303 if (ios->clock == 0)
1304 host->real_rate = 0;
1307 static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1309 struct s3cmci_host *host = mmc_priv(mmc);
1312 /* Set the power state */
1314 mci_con = readl(host->base + S3C2410_SDICON);
1316 switch (ios->power_mode) {
1319 /* Configure GPE5...GPE10 pins in SD mode */
1320 s3c_gpio_cfgall_range(S3C2410_GPE(5), 6, S3C_GPIO_SFN(2),
1321 S3C_GPIO_PULL_NONE);
1323 if (host->pdata->set_power)
1324 host->pdata->set_power(ios->power_mode, ios->vdd);
1327 mci_con |= S3C2410_SDICON_FIFORESET;
1333 gpio_direction_output(S3C2410_GPE(5), 0);
1336 mci_con |= S3C2440_SDICON_SDRESET;
1338 if (host->pdata->set_power)
1339 host->pdata->set_power(ios->power_mode, ios->vdd);
1344 s3cmci_set_clk(host, ios);
1346 /* Set CLOCK_ENABLE */
1348 mci_con |= S3C2410_SDICON_CLOCKTYPE;
1350 mci_con &= ~S3C2410_SDICON_CLOCKTYPE;
1352 writel(mci_con, host->base + S3C2410_SDICON);
1354 if ((ios->power_mode == MMC_POWER_ON) ||
1355 (ios->power_mode == MMC_POWER_UP)) {
1356 dbg(host, dbg_conf, "running at %lukHz (requested: %ukHz).\n",
1357 host->real_rate/1000, ios->clock/1000);
1359 dbg(host, dbg_conf, "powered down.\n");
1362 host->bus_width = ios->bus_width;
1365 static void s3cmci_reset(struct s3cmci_host *host)
1367 u32 con = readl(host->base + S3C2410_SDICON);
1369 con |= S3C2440_SDICON_SDRESET;
1370 writel(con, host->base + S3C2410_SDICON);
1373 static int s3cmci_get_ro(struct mmc_host *mmc)
1375 struct s3cmci_host *host = mmc_priv(mmc);
1376 struct s3c24xx_mci_pdata *pdata = host->pdata;
1379 if (pdata->no_wprotect)
1382 ret = gpio_get_value(pdata->gpio_wprotect) ? 1 : 0;
1383 ret ^= pdata->wprotect_invert;
1388 static void s3cmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1390 struct s3cmci_host *host = mmc_priv(mmc);
1391 unsigned long flags;
1394 local_irq_save(flags);
1396 con = readl(host->base + S3C2410_SDICON);
1397 host->sdio_irqen = enable;
1399 if (enable == host->sdio_irqen)
1403 con |= S3C2410_SDICON_SDIOIRQ;
1404 enable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
1406 if (!host->irq_state && !host->irq_disabled) {
1407 host->irq_state = true;
1408 enable_irq(host->irq);
1411 disable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
1412 con &= ~S3C2410_SDICON_SDIOIRQ;
1414 if (!host->irq_enabled && host->irq_state) {
1415 disable_irq_nosync(host->irq);
1416 host->irq_state = false;
1420 writel(con, host->base + S3C2410_SDICON);
1423 local_irq_restore(flags);
1425 s3cmci_check_sdio_irq(host);
1428 static struct mmc_host_ops s3cmci_ops = {
1429 .request = s3cmci_request,
1430 .set_ios = s3cmci_set_ios,
1431 .get_ro = s3cmci_get_ro,
1432 .get_cd = s3cmci_card_present,
1433 .enable_sdio_irq = s3cmci_enable_sdio_irq,
1436 static struct s3c24xx_mci_pdata s3cmci_def_pdata = {
1437 /* This is currently here to avoid a number of if (host->pdata)
1438 * checks. Any zero fields to ensure reasonable defaults are picked. */
1443 #ifdef CONFIG_CPU_FREQ
1445 static int s3cmci_cpufreq_transition(struct notifier_block *nb,
1446 unsigned long val, void *data)
1448 struct s3cmci_host *host;
1449 struct mmc_host *mmc;
1450 unsigned long newclk;
1451 unsigned long flags;
1453 host = container_of(nb, struct s3cmci_host, freq_transition);
1454 newclk = clk_get_rate(host->clk);
1457 if ((val == CPUFREQ_PRECHANGE && newclk > host->clk_rate) ||
1458 (val == CPUFREQ_POSTCHANGE && newclk < host->clk_rate)) {
1459 spin_lock_irqsave(&mmc->lock, flags);
1461 host->clk_rate = newclk;
1463 if (mmc->ios.power_mode != MMC_POWER_OFF &&
1464 mmc->ios.clock != 0)
1465 s3cmci_set_clk(host, &mmc->ios);
1467 spin_unlock_irqrestore(&mmc->lock, flags);
1473 static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1475 host->freq_transition.notifier_call = s3cmci_cpufreq_transition;
1477 return cpufreq_register_notifier(&host->freq_transition,
1478 CPUFREQ_TRANSITION_NOTIFIER);
1481 static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1483 cpufreq_unregister_notifier(&host->freq_transition,
1484 CPUFREQ_TRANSITION_NOTIFIER);
1488 static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1493 static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1499 #ifdef CONFIG_DEBUG_FS
1501 static int s3cmci_state_show(struct seq_file *seq, void *v)
1503 struct s3cmci_host *host = seq->private;
1505 seq_printf(seq, "Register base = 0x%08x\n", (u32)host->base);
1506 seq_printf(seq, "Clock rate = %ld\n", host->clk_rate);
1507 seq_printf(seq, "Prescale = %d\n", host->prescaler);
1508 seq_printf(seq, "is2440 = %d\n", host->is2440);
1509 seq_printf(seq, "IRQ = %d\n", host->irq);
1510 seq_printf(seq, "IRQ enabled = %d\n", host->irq_enabled);
1511 seq_printf(seq, "IRQ disabled = %d\n", host->irq_disabled);
1512 seq_printf(seq, "IRQ state = %d\n", host->irq_state);
1513 seq_printf(seq, "CD IRQ = %d\n", host->irq_cd);
1514 seq_printf(seq, "Do DMA = %d\n", s3cmci_host_usedma(host));
1515 seq_printf(seq, "SDIIMSK at %d\n", host->sdiimsk);
1516 seq_printf(seq, "SDIDATA at %d\n", host->sdidata);
1521 static int s3cmci_state_open(struct inode *inode, struct file *file)
1523 return single_open(file, s3cmci_state_show, inode->i_private);
1526 static const struct file_operations s3cmci_fops_state = {
1527 .owner = THIS_MODULE,
1528 .open = s3cmci_state_open,
1530 .llseek = seq_lseek,
1531 .release = single_release,
1534 #define DBG_REG(_r) { .addr = S3C2410_SDI##_r, .name = #_r }
1537 unsigned short addr;
1538 unsigned char *name;
1558 static int s3cmci_regs_show(struct seq_file *seq, void *v)
1560 struct s3cmci_host *host = seq->private;
1561 struct s3cmci_reg *rptr = debug_regs;
1563 for (; rptr->name; rptr++)
1564 seq_printf(seq, "SDI%s\t=0x%08x\n", rptr->name,
1565 readl(host->base + rptr->addr));
1567 seq_printf(seq, "SDIIMSK\t=0x%08x\n", readl(host->base + host->sdiimsk));
1572 static int s3cmci_regs_open(struct inode *inode, struct file *file)
1574 return single_open(file, s3cmci_regs_show, inode->i_private);
1577 static const struct file_operations s3cmci_fops_regs = {
1578 .owner = THIS_MODULE,
1579 .open = s3cmci_regs_open,
1581 .llseek = seq_lseek,
1582 .release = single_release,
1585 static void s3cmci_debugfs_attach(struct s3cmci_host *host)
1587 struct device *dev = &host->pdev->dev;
1589 host->debug_root = debugfs_create_dir(dev_name(dev), NULL);
1590 if (IS_ERR(host->debug_root)) {
1591 dev_err(dev, "failed to create debugfs root\n");
1595 host->debug_state = debugfs_create_file("state", 0444,
1596 host->debug_root, host,
1597 &s3cmci_fops_state);
1599 if (IS_ERR(host->debug_state))
1600 dev_err(dev, "failed to create debug state file\n");
1602 host->debug_regs = debugfs_create_file("regs", 0444,
1603 host->debug_root, host,
1606 if (IS_ERR(host->debug_regs))
1607 dev_err(dev, "failed to create debug regs file\n");
1610 static void s3cmci_debugfs_remove(struct s3cmci_host *host)
1612 debugfs_remove(host->debug_regs);
1613 debugfs_remove(host->debug_state);
1614 debugfs_remove(host->debug_root);
1618 static inline void s3cmci_debugfs_attach(struct s3cmci_host *host) { }
1619 static inline void s3cmci_debugfs_remove(struct s3cmci_host *host) { }
1621 #endif /* CONFIG_DEBUG_FS */
1623 static int s3cmci_probe(struct platform_device *pdev)
1625 struct s3cmci_host *host;
1626 struct mmc_host *mmc;
1631 is2440 = platform_get_device_id(pdev)->driver_data;
1633 mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
1639 for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++) {
1640 ret = gpio_request(i, dev_name(&pdev->dev));
1642 dev_err(&pdev->dev, "failed to get gpio %d\n", i);
1644 for (i--; i >= S3C2410_GPE(5); i--)
1647 goto probe_free_host;
1651 host = mmc_priv(mmc);
1654 host->is2440 = is2440;
1656 host->pdata = pdev->dev.platform_data;
1658 pdev->dev.platform_data = &s3cmci_def_pdata;
1659 host->pdata = &s3cmci_def_pdata;
1662 spin_lock_init(&host->complete_lock);
1663 tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);
1666 host->sdiimsk = S3C2440_SDIIMSK;
1667 host->sdidata = S3C2440_SDIDATA;
1670 host->sdiimsk = S3C2410_SDIIMSK;
1671 host->sdidata = S3C2410_SDIDATA;
1675 host->complete_what = COMPLETION_NONE;
1676 host->pio_active = XFER_NONE;
1678 #ifdef CONFIG_MMC_S3C_PIODMA
1679 host->dodma = host->pdata->use_dma;
1682 host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1685 "failed to get io memory region resource.\n");
1688 goto probe_free_gpio;
1691 host->mem = request_mem_region(host->mem->start,
1692 resource_size(host->mem), pdev->name);
1695 dev_err(&pdev->dev, "failed to request io memory region.\n");
1697 goto probe_free_gpio;
1700 host->base = ioremap(host->mem->start, resource_size(host->mem));
1702 dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
1704 goto probe_free_mem_region;
1707 host->irq = platform_get_irq(pdev, 0);
1708 if (host->irq == 0) {
1709 dev_err(&pdev->dev, "failed to get interrupt resource.\n");
1714 if (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)) {
1715 dev_err(&pdev->dev, "failed to request mci interrupt.\n");
1720 /* We get spurious interrupts even when we have set the IMSK
1721 * register to ignore everything, so use disable_irq() to make
1722 * ensure we don't lock the system with un-serviceable requests. */
1724 disable_irq(host->irq);
1725 host->irq_state = false;
1727 if (!host->pdata->no_detect) {
1728 ret = gpio_request(host->pdata->gpio_detect, "s3cmci detect");
1730 dev_err(&pdev->dev, "failed to get detect gpio\n");
1731 goto probe_free_irq;
1734 host->irq_cd = gpio_to_irq(host->pdata->gpio_detect);
1736 if (host->irq_cd >= 0) {
1737 if (request_irq(host->irq_cd, s3cmci_irq_cd,
1738 IRQF_TRIGGER_RISING |
1739 IRQF_TRIGGER_FALLING,
1740 DRIVER_NAME, host)) {
1742 "can't get card detect irq.\n");
1744 goto probe_free_gpio_cd;
1747 dev_warn(&pdev->dev,
1748 "host detect has no irq available\n");
1749 gpio_direction_input(host->pdata->gpio_detect);
1754 if (!host->pdata->no_wprotect) {
1755 ret = gpio_request(host->pdata->gpio_wprotect, "s3cmci wp");
1757 dev_err(&pdev->dev, "failed to get writeprotect\n");
1758 goto probe_free_irq_cd;
1761 gpio_direction_input(host->pdata->gpio_wprotect);
1764 /* depending on the dma state, get a dma channel to use. */
1766 if (s3cmci_host_usedma(host)) {
1767 host->dma = s3c2410_dma_request(DMACH_SDI, &s3cmci_dma_client,
1769 if (host->dma < 0) {
1770 dev_err(&pdev->dev, "cannot get DMA channel.\n");
1771 if (!s3cmci_host_canpio()) {
1773 goto probe_free_gpio_wp;
1775 dev_warn(&pdev->dev, "falling back to PIO.\n");
1781 host->clk = clk_get(&pdev->dev, "sdi");
1782 if (IS_ERR(host->clk)) {
1783 dev_err(&pdev->dev, "failed to find clock source.\n");
1784 ret = PTR_ERR(host->clk);
1786 goto probe_free_dma;
1789 ret = clk_enable(host->clk);
1791 dev_err(&pdev->dev, "failed to enable clock source.\n");
1795 host->clk_rate = clk_get_rate(host->clk);
1797 mmc->ops = &s3cmci_ops;
1798 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1799 #ifdef CONFIG_MMC_S3C_HW_SDIO_IRQ
1800 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
1802 mmc->caps = MMC_CAP_4_BIT_DATA;
1804 mmc->f_min = host->clk_rate / (host->clk_div * 256);
1805 mmc->f_max = host->clk_rate / host->clk_div;
1807 if (host->pdata->ocr_avail)
1808 mmc->ocr_avail = host->pdata->ocr_avail;
1810 mmc->max_blk_count = 4095;
1811 mmc->max_blk_size = 4095;
1812 mmc->max_req_size = 4095 * 512;
1813 mmc->max_seg_size = mmc->max_req_size;
1815 mmc->max_segs = 128;
1817 dbg(host, dbg_debug,
1818 "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%u.\n",
1819 (host->is2440?"2440":""),
1820 host->base, host->irq, host->irq_cd, host->dma);
1822 ret = s3cmci_cpufreq_register(host);
1824 dev_err(&pdev->dev, "failed to register cpufreq\n");
1828 ret = mmc_add_host(mmc);
1830 dev_err(&pdev->dev, "failed to add mmc host.\n");
1834 s3cmci_debugfs_attach(host);
1836 platform_set_drvdata(pdev, mmc);
1837 dev_info(&pdev->dev, "%s - using %s, %s SDIO IRQ\n", mmc_hostname(mmc),
1838 s3cmci_host_usedma(host) ? "dma" : "pio",
1839 mmc->caps & MMC_CAP_SDIO_IRQ ? "hw" : "sw");
1844 s3cmci_cpufreq_deregister(host);
1847 clk_disable(host->clk);
1853 if (s3cmci_host_usedma(host))
1854 s3c2410_dma_free(host->dma, &s3cmci_dma_client);
1857 if (!host->pdata->no_wprotect)
1858 gpio_free(host->pdata->gpio_wprotect);
1861 if (!host->pdata->no_detect)
1862 gpio_free(host->pdata->gpio_detect);
1865 if (host->irq_cd >= 0)
1866 free_irq(host->irq_cd, host);
1869 free_irq(host->irq, host);
1872 iounmap(host->base);
1874 probe_free_mem_region:
1875 release_mem_region(host->mem->start, resource_size(host->mem));
1878 for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++)
1888 static void s3cmci_shutdown(struct platform_device *pdev)
1890 struct mmc_host *mmc = platform_get_drvdata(pdev);
1891 struct s3cmci_host *host = mmc_priv(mmc);
1893 if (host->irq_cd >= 0)
1894 free_irq(host->irq_cd, host);
1896 s3cmci_debugfs_remove(host);
1897 s3cmci_cpufreq_deregister(host);
1898 mmc_remove_host(mmc);
1899 clk_disable(host->clk);
1902 static int s3cmci_remove(struct platform_device *pdev)
1904 struct mmc_host *mmc = platform_get_drvdata(pdev);
1905 struct s3cmci_host *host = mmc_priv(mmc);
1906 struct s3c24xx_mci_pdata *pd = host->pdata;
1909 s3cmci_shutdown(pdev);
1913 tasklet_disable(&host->pio_tasklet);
1915 if (s3cmci_host_usedma(host))
1916 s3c2410_dma_free(host->dma, &s3cmci_dma_client);
1918 free_irq(host->irq, host);
1920 if (!pd->no_wprotect)
1921 gpio_free(pd->gpio_wprotect);
1924 gpio_free(pd->gpio_detect);
1926 for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++)
1930 iounmap(host->base);
1931 release_mem_region(host->mem->start, resource_size(host->mem));
1937 static struct platform_device_id s3cmci_driver_ids[] = {
1939 .name = "s3c2410-sdi",
1942 .name = "s3c2412-sdi",
1945 .name = "s3c2440-sdi",
1951 MODULE_DEVICE_TABLE(platform, s3cmci_driver_ids);
1953 static struct platform_driver s3cmci_driver = {
1956 .owner = THIS_MODULE,
1958 .id_table = s3cmci_driver_ids,
1959 .probe = s3cmci_probe,
1960 .remove = s3cmci_remove,
1961 .shutdown = s3cmci_shutdown,
1964 module_platform_driver(s3cmci_driver);
1966 MODULE_DESCRIPTION("Samsung S3C MMC/SD Card Interface driver");
1967 MODULE_LICENSE("GPL v2");
1968 MODULE_AUTHOR("Thomas Kleffel <tk@maintech.de>, Ben Dooks <ben-linux@fluff.org>");