2 * linux/drivers/mmc/s3cmci.h - Samsung S3C MCI driver
6 * Current driver maintained by Ben Dooks and Simtec Electronics
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>
28 #include <linux/platform_data/mmc-s3cmci.h>
32 #define DRIVER_NAME "s3c-mci"
34 #define S3C2410_SDICON (0x00)
35 #define S3C2410_SDIPRE (0x04)
36 #define S3C2410_SDICMDARG (0x08)
37 #define S3C2410_SDICMDCON (0x0C)
38 #define S3C2410_SDICMDSTAT (0x10)
39 #define S3C2410_SDIRSP0 (0x14)
40 #define S3C2410_SDIRSP1 (0x18)
41 #define S3C2410_SDIRSP2 (0x1C)
42 #define S3C2410_SDIRSP3 (0x20)
43 #define S3C2410_SDITIMER (0x24)
44 #define S3C2410_SDIBSIZE (0x28)
45 #define S3C2410_SDIDCON (0x2C)
46 #define S3C2410_SDIDCNT (0x30)
47 #define S3C2410_SDIDSTA (0x34)
48 #define S3C2410_SDIFSTA (0x38)
50 #define S3C2410_SDIDATA (0x3C)
51 #define S3C2410_SDIIMSK (0x40)
53 #define S3C2440_SDIDATA (0x40)
54 #define S3C2440_SDIIMSK (0x3C)
56 #define S3C2440_SDICON_SDRESET (1 << 8)
57 #define S3C2410_SDICON_SDIOIRQ (1 << 3)
58 #define S3C2410_SDICON_FIFORESET (1 << 1)
59 #define S3C2410_SDICON_CLOCKTYPE (1 << 0)
61 #define S3C2410_SDICMDCON_LONGRSP (1 << 10)
62 #define S3C2410_SDICMDCON_WAITRSP (1 << 9)
63 #define S3C2410_SDICMDCON_CMDSTART (1 << 8)
64 #define S3C2410_SDICMDCON_SENDERHOST (1 << 6)
65 #define S3C2410_SDICMDCON_INDEX (0x3f)
67 #define S3C2410_SDICMDSTAT_CRCFAIL (1 << 12)
68 #define S3C2410_SDICMDSTAT_CMDSENT (1 << 11)
69 #define S3C2410_SDICMDSTAT_CMDTIMEOUT (1 << 10)
70 #define S3C2410_SDICMDSTAT_RSPFIN (1 << 9)
72 #define S3C2440_SDIDCON_DS_WORD (2 << 22)
73 #define S3C2410_SDIDCON_TXAFTERRESP (1 << 20)
74 #define S3C2410_SDIDCON_RXAFTERCMD (1 << 19)
75 #define S3C2410_SDIDCON_BLOCKMODE (1 << 17)
76 #define S3C2410_SDIDCON_WIDEBUS (1 << 16)
77 #define S3C2410_SDIDCON_DMAEN (1 << 15)
78 #define S3C2410_SDIDCON_STOP (1 << 14)
79 #define S3C2440_SDIDCON_DATSTART (1 << 14)
81 #define S3C2410_SDIDCON_XFER_RXSTART (2 << 12)
82 #define S3C2410_SDIDCON_XFER_TXSTART (3 << 12)
84 #define S3C2410_SDIDCON_BLKNUM_MASK (0xFFF)
86 #define S3C2410_SDIDSTA_SDIOIRQDETECT (1 << 9)
87 #define S3C2410_SDIDSTA_FIFOFAIL (1 << 8)
88 #define S3C2410_SDIDSTA_CRCFAIL (1 << 7)
89 #define S3C2410_SDIDSTA_RXCRCFAIL (1 << 6)
90 #define S3C2410_SDIDSTA_DATATIMEOUT (1 << 5)
91 #define S3C2410_SDIDSTA_XFERFINISH (1 << 4)
92 #define S3C2410_SDIDSTA_TXDATAON (1 << 1)
93 #define S3C2410_SDIDSTA_RXDATAON (1 << 0)
95 #define S3C2440_SDIFSTA_FIFORESET (1 << 16)
96 #define S3C2440_SDIFSTA_FIFOFAIL (3 << 14)
97 #define S3C2410_SDIFSTA_TFDET (1 << 13)
98 #define S3C2410_SDIFSTA_RFDET (1 << 12)
99 #define S3C2410_SDIFSTA_COUNTMASK (0x7f)
101 #define S3C2410_SDIIMSK_RESPONSECRC (1 << 17)
102 #define S3C2410_SDIIMSK_CMDSENT (1 << 16)
103 #define S3C2410_SDIIMSK_CMDTIMEOUT (1 << 15)
104 #define S3C2410_SDIIMSK_RESPONSEND (1 << 14)
105 #define S3C2410_SDIIMSK_SDIOIRQ (1 << 12)
106 #define S3C2410_SDIIMSK_FIFOFAIL (1 << 11)
107 #define S3C2410_SDIIMSK_CRCSTATUS (1 << 10)
108 #define S3C2410_SDIIMSK_DATACRC (1 << 9)
109 #define S3C2410_SDIIMSK_DATATIMEOUT (1 << 8)
110 #define S3C2410_SDIIMSK_DATAFINISH (1 << 7)
111 #define S3C2410_SDIIMSK_TXFIFOHALF (1 << 4)
112 #define S3C2410_SDIIMSK_RXFIFOLAST (1 << 2)
113 #define S3C2410_SDIIMSK_RXFIFOHALF (1 << 0)
117 dbg_debug = (1 << 1),
127 static const int dbgmap_err = dbg_fail;
128 static const int dbgmap_info = dbg_info | dbg_conf;
129 static const int dbgmap_debug = dbg_err | dbg_debug;
131 #define dbg(host, channels, args...) \
133 if (dbgmap_err & channels) \
134 dev_err(&host->pdev->dev, args); \
135 else if (dbgmap_info & channels) \
136 dev_info(&host->pdev->dev, args); \
137 else if (dbgmap_debug & channels) \
138 dev_dbg(&host->pdev->dev, args); \
141 static struct s3c2410_dma_client s3cmci_dma_client = {
145 static void finalize_request(struct s3cmci_host *host);
146 static void s3cmci_send_request(struct mmc_host *mmc);
147 static void s3cmci_reset(struct s3cmci_host *host);
149 #ifdef CONFIG_MMC_DEBUG
151 static void dbg_dumpregs(struct s3cmci_host *host, char *prefix)
153 u32 con, pre, cmdarg, cmdcon, cmdsta, r0, r1, r2, r3, timer, bsize;
154 u32 datcon, datcnt, datsta, fsta, imask;
156 con = readl(host->base + S3C2410_SDICON);
157 pre = readl(host->base + S3C2410_SDIPRE);
158 cmdarg = readl(host->base + S3C2410_SDICMDARG);
159 cmdcon = readl(host->base + S3C2410_SDICMDCON);
160 cmdsta = readl(host->base + S3C2410_SDICMDSTAT);
161 r0 = readl(host->base + S3C2410_SDIRSP0);
162 r1 = readl(host->base + S3C2410_SDIRSP1);
163 r2 = readl(host->base + S3C2410_SDIRSP2);
164 r3 = readl(host->base + S3C2410_SDIRSP3);
165 timer = readl(host->base + S3C2410_SDITIMER);
166 bsize = readl(host->base + S3C2410_SDIBSIZE);
167 datcon = readl(host->base + S3C2410_SDIDCON);
168 datcnt = readl(host->base + S3C2410_SDIDCNT);
169 datsta = readl(host->base + S3C2410_SDIDSTA);
170 fsta = readl(host->base + S3C2410_SDIFSTA);
171 imask = readl(host->base + host->sdiimsk);
173 dbg(host, dbg_debug, "%s CON:[%08x] PRE:[%08x] TMR:[%08x]\n",
174 prefix, con, pre, timer);
176 dbg(host, dbg_debug, "%s CCON:[%08x] CARG:[%08x] CSTA:[%08x]\n",
177 prefix, cmdcon, cmdarg, cmdsta);
179 dbg(host, dbg_debug, "%s DCON:[%08x] FSTA:[%08x]"
180 " DSTA:[%08x] DCNT:[%08x]\n",
181 prefix, datcon, fsta, datsta, datcnt);
183 dbg(host, dbg_debug, "%s R0:[%08x] R1:[%08x]"
184 " R2:[%08x] R3:[%08x]\n",
185 prefix, r0, r1, r2, r3);
188 static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
191 snprintf(host->dbgmsg_cmd, 300,
192 "#%u%s op:%i arg:0x%08x flags:0x08%x retries:%u",
193 host->ccnt, (stop ? " (STOP)" : ""),
194 cmd->opcode, cmd->arg, cmd->flags, cmd->retries);
197 snprintf(host->dbgmsg_dat, 300,
198 "#%u bsize:%u blocks:%u bytes:%u",
199 host->dcnt, cmd->data->blksz,
201 cmd->data->blocks * cmd->data->blksz);
203 host->dbgmsg_dat[0] = '\0';
207 static void dbg_dumpcmd(struct s3cmci_host *host, struct mmc_command *cmd,
210 unsigned int dbglvl = fail ? dbg_fail : dbg_debug;
215 if (cmd->error == 0) {
216 dbg(host, dbglvl, "CMD[OK] %s R0:0x%08x\n",
217 host->dbgmsg_cmd, cmd->resp[0]);
219 dbg(host, dbglvl, "CMD[ERR %i] %s Status:%s\n",
220 cmd->error, host->dbgmsg_cmd, host->status);
226 if (cmd->data->error == 0) {
227 dbg(host, dbglvl, "DAT[OK] %s\n", host->dbgmsg_dat);
229 dbg(host, dbglvl, "DAT[ERR %i] %s DCNT:0x%08x\n",
230 cmd->data->error, host->dbgmsg_dat,
231 readl(host->base + S3C2410_SDIDCNT));
235 static void dbg_dumpcmd(struct s3cmci_host *host,
236 struct mmc_command *cmd, int fail) { }
238 static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
241 static void dbg_dumpregs(struct s3cmci_host *host, char *prefix) { }
243 #endif /* CONFIG_MMC_DEBUG */
246 * s3cmci_host_usedma - return whether the host is using dma or pio
247 * @host: The host state
249 * Return true if the host is using DMA to transfer data, else false
250 * to use PIO mode. Will return static data depending on the driver
253 static inline bool s3cmci_host_usedma(struct s3cmci_host *host)
255 #ifdef CONFIG_MMC_S3C_PIO
257 #elif defined(CONFIG_MMC_S3C_DMA)
265 * s3cmci_host_canpio - return true if host has pio code available
267 * Return true if the driver has been compiled with the PIO support code
270 static inline bool s3cmci_host_canpio(void)
272 #ifdef CONFIG_MMC_S3C_PIO
279 static inline u32 enable_imask(struct s3cmci_host *host, u32 imask)
283 newmask = readl(host->base + host->sdiimsk);
286 writel(newmask, host->base + host->sdiimsk);
291 static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
295 newmask = readl(host->base + host->sdiimsk);
298 writel(newmask, host->base + host->sdiimsk);
303 static inline void clear_imask(struct s3cmci_host *host)
305 u32 mask = readl(host->base + host->sdiimsk);
307 /* preserve the SDIO IRQ mask state */
308 mask &= S3C2410_SDIIMSK_SDIOIRQ;
309 writel(mask, host->base + host->sdiimsk);
313 * s3cmci_check_sdio_irq - test whether the SDIO IRQ is being signalled
314 * @host: The host to check.
316 * Test to see if the SDIO interrupt is being signalled in case the
317 * controller has failed to re-detect a card interrupt. Read GPE8 and
318 * see if it is low and if so, signal a SDIO interrupt.
320 * This is currently called if a request is finished (we assume that the
321 * bus is now idle) and when the SDIO IRQ is enabled in case the IRQ is
322 * already being indicated.
324 static void s3cmci_check_sdio_irq(struct s3cmci_host *host)
326 if (host->sdio_irqen) {
327 if (gpio_get_value(S3C2410_GPE(8)) == 0) {
328 pr_debug("%s: signalling irq\n", __func__);
329 mmc_signal_sdio_irq(host->mmc);
334 static inline int get_data_buffer(struct s3cmci_host *host,
335 u32 *bytes, u32 **pointer)
337 struct scatterlist *sg;
339 if (host->pio_active == XFER_NONE)
342 if ((!host->mrq) || (!host->mrq->data))
345 if (host->pio_sgptr >= host->mrq->data->sg_len) {
346 dbg(host, dbg_debug, "no more buffers (%i/%i)\n",
347 host->pio_sgptr, host->mrq->data->sg_len);
350 sg = &host->mrq->data->sg[host->pio_sgptr];
353 *pointer = sg_virt(sg);
357 dbg(host, dbg_sg, "new buffer (%i/%i)\n",
358 host->pio_sgptr, host->mrq->data->sg_len);
363 static inline u32 fifo_count(struct s3cmci_host *host)
365 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
367 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
371 static inline u32 fifo_free(struct s3cmci_host *host)
373 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
375 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
376 return 63 - fifostat;
380 * s3cmci_enable_irq - enable IRQ, after having disabled it.
381 * @host: The device state.
382 * @more: True if more IRQs are expected from transfer.
384 * Enable the main IRQ if needed after it has been disabled.
386 * The IRQ can be one of the following states:
387 * - disabled during IDLE
388 * - disabled whilst processing data
389 * - enabled during transfer
390 * - enabled whilst awaiting SDIO interrupt detection
392 static void s3cmci_enable_irq(struct s3cmci_host *host, bool more)
397 local_irq_save(flags);
399 host->irq_enabled = more;
400 host->irq_disabled = false;
402 enable = more | host->sdio_irqen;
404 if (host->irq_state != enable) {
405 host->irq_state = enable;
408 enable_irq(host->irq);
410 disable_irq(host->irq);
413 local_irq_restore(flags);
419 static void s3cmci_disable_irq(struct s3cmci_host *host, bool transfer)
423 local_irq_save(flags);
425 /* pr_debug("%s: transfer %d\n", __func__, transfer); */
427 host->irq_disabled = transfer;
429 if (transfer && host->irq_state) {
430 host->irq_state = false;
431 disable_irq(host->irq);
434 local_irq_restore(flags);
437 static void do_pio_read(struct s3cmci_host *host)
443 void __iomem *from_ptr;
445 /* write real prescaler to host, it might be set slow to fix */
446 writel(host->prescaler, host->base + S3C2410_SDIPRE);
448 from_ptr = host->base + host->sdidata;
450 while ((fifo = fifo_count(host))) {
451 if (!host->pio_bytes) {
452 res = get_data_buffer(host, &host->pio_bytes,
455 host->pio_active = XFER_NONE;
456 host->complete_what = COMPLETION_FINALIZE;
458 dbg(host, dbg_pio, "pio_read(): "
459 "complete (no more data).\n");
464 "pio_read(): new target: [%i]@[%p]\n",
465 host->pio_bytes, host->pio_ptr);
469 "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
470 fifo, host->pio_bytes,
471 readl(host->base + S3C2410_SDIDCNT));
473 /* If we have reached the end of the block, we can
474 * read a word and get 1 to 3 bytes. If we in the
475 * middle of the block, we have to read full words,
476 * otherwise we will write garbage, so round down to
477 * an even multiple of 4. */
478 if (fifo >= host->pio_bytes)
479 fifo = host->pio_bytes;
483 host->pio_bytes -= fifo;
484 host->pio_count += fifo;
486 fifo_words = fifo >> 2;
489 *ptr++ = readl(from_ptr);
494 u32 data = readl(from_ptr);
495 u8 *p = (u8 *)host->pio_ptr;
504 if (!host->pio_bytes) {
505 res = get_data_buffer(host, &host->pio_bytes, &host->pio_ptr);
508 "pio_read(): complete (no more buffers).\n");
509 host->pio_active = XFER_NONE;
510 host->complete_what = COMPLETION_FINALIZE;
517 S3C2410_SDIIMSK_RXFIFOHALF | S3C2410_SDIIMSK_RXFIFOLAST);
520 static void do_pio_write(struct s3cmci_host *host)
522 void __iomem *to_ptr;
527 to_ptr = host->base + host->sdidata;
529 while ((fifo = fifo_free(host)) > 3) {
530 if (!host->pio_bytes) {
531 res = get_data_buffer(host, &host->pio_bytes,
535 "pio_write(): complete (no more data).\n");
536 host->pio_active = XFER_NONE;
542 "pio_write(): new source: [%i]@[%p]\n",
543 host->pio_bytes, host->pio_ptr);
547 /* If we have reached the end of the block, we have to
548 * write exactly the remaining number of bytes. If we
549 * in the middle of the block, we have to write full
550 * words, so round down to an even multiple of 4. */
551 if (fifo >= host->pio_bytes)
552 fifo = host->pio_bytes;
556 host->pio_bytes -= fifo;
557 host->pio_count += fifo;
559 fifo = (fifo + 3) >> 2;
562 writel(*ptr++, to_ptr);
566 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
569 static void pio_tasklet(unsigned long data)
571 struct s3cmci_host *host = (struct s3cmci_host *) data;
573 s3cmci_disable_irq(host, true);
575 if (host->pio_active == XFER_WRITE)
578 if (host->pio_active == XFER_READ)
581 if (host->complete_what == COMPLETION_FINALIZE) {
583 if (host->pio_active != XFER_NONE) {
584 dbg(host, dbg_err, "unfinished %s "
585 "- pio_count:[%u] pio_bytes:[%u]\n",
586 (host->pio_active == XFER_READ) ? "read" : "write",
587 host->pio_count, host->pio_bytes);
590 host->mrq->data->error = -EINVAL;
593 s3cmci_enable_irq(host, false);
594 finalize_request(host);
596 s3cmci_enable_irq(host, true);
600 * ISR for SDI Interface IRQ
601 * Communication between driver and ISR works as follows:
602 * host->mrq points to current request
603 * host->complete_what Indicates when the request is considered done
604 * COMPLETION_CMDSENT when the command was sent
605 * COMPLETION_RSPFIN when a response was received
606 * COMPLETION_XFERFINISH when the data transfer is finished
607 * COMPLETION_XFERFINISH_RSPFIN both of the above.
608 * host->complete_request is the completion-object the driver waits for
610 * 1) Driver sets up host->mrq and host->complete_what
611 * 2) Driver prepares the transfer
612 * 3) Driver enables interrupts
613 * 4) Driver starts transfer
614 * 5) Driver waits for host->complete_rquest
615 * 6) ISR checks for request status (errors and success)
616 * 6) ISR sets host->mrq->cmd->error and host->mrq->data->error
617 * 7) ISR completes host->complete_request
618 * 8) ISR disables interrupts
619 * 9) Driver wakes up and takes care of the request
621 * Note: "->error"-fields are expected to be set to 0 before the request
622 * was issued by mmc.c - therefore they are only set, when an error
626 static irqreturn_t s3cmci_irq(int irq, void *dev_id)
628 struct s3cmci_host *host = dev_id;
629 struct mmc_command *cmd;
630 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
631 u32 mci_cclear = 0, mci_dclear;
632 unsigned long iflags;
634 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
635 mci_imsk = readl(host->base + host->sdiimsk);
637 if (mci_dsta & S3C2410_SDIDSTA_SDIOIRQDETECT) {
638 if (mci_imsk & S3C2410_SDIIMSK_SDIOIRQ) {
639 mci_dclear = S3C2410_SDIDSTA_SDIOIRQDETECT;
640 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
642 mmc_signal_sdio_irq(host->mmc);
647 spin_lock_irqsave(&host->complete_lock, iflags);
649 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
650 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
651 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
654 if ((host->complete_what == COMPLETION_NONE) ||
655 (host->complete_what == COMPLETION_FINALIZE)) {
656 host->status = "nothing to complete";
662 host->status = "no active mrq";
667 cmd = host->cmd_is_stop ? host->mrq->stop : host->mrq->cmd;
670 host->status = "no active cmd";
675 if (!s3cmci_host_usedma(host)) {
676 if ((host->pio_active == XFER_WRITE) &&
677 (mci_fsta & S3C2410_SDIFSTA_TFDET)) {
679 disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
680 tasklet_schedule(&host->pio_tasklet);
681 host->status = "pio tx";
684 if ((host->pio_active == XFER_READ) &&
685 (mci_fsta & S3C2410_SDIFSTA_RFDET)) {
688 S3C2410_SDIIMSK_RXFIFOHALF |
689 S3C2410_SDIIMSK_RXFIFOLAST);
691 tasklet_schedule(&host->pio_tasklet);
692 host->status = "pio rx";
696 if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {
697 dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");
698 cmd->error = -ETIMEDOUT;
699 host->status = "error: command timeout";
703 if (mci_csta & S3C2410_SDICMDSTAT_CMDSENT) {
704 if (host->complete_what == COMPLETION_CMDSENT) {
705 host->status = "ok: command sent";
709 mci_cclear |= S3C2410_SDICMDSTAT_CMDSENT;
712 if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {
713 if (cmd->flags & MMC_RSP_CRC) {
714 if (host->mrq->cmd->flags & MMC_RSP_136) {
716 "fixup: ignore CRC fail with long rsp\n");
718 /* note, we used to fail the transfer
719 * here, but it seems that this is just
720 * the hardware getting it wrong.
722 * cmd->error = -EILSEQ;
723 * host->status = "error: bad command crc";
724 * goto fail_transfer;
729 mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;
732 if (mci_csta & S3C2410_SDICMDSTAT_RSPFIN) {
733 if (host->complete_what == COMPLETION_RSPFIN) {
734 host->status = "ok: command response received";
738 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
739 host->complete_what = COMPLETION_XFERFINISH;
741 mci_cclear |= S3C2410_SDICMDSTAT_RSPFIN;
744 /* errors handled after this point are only relevant
745 when a data transfer is in progress */
748 goto clear_status_bits;
750 /* Check for FIFO failure */
752 if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {
753 dbg(host, dbg_err, "FIFO failure\n");
754 host->mrq->data->error = -EILSEQ;
755 host->status = "error: 2440 fifo failure";
759 if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {
760 dbg(host, dbg_err, "FIFO failure\n");
761 cmd->data->error = -EILSEQ;
762 host->status = "error: fifo failure";
767 if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {
768 dbg(host, dbg_err, "bad data crc (outgoing)\n");
769 cmd->data->error = -EILSEQ;
770 host->status = "error: bad data crc (outgoing)";
774 if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {
775 dbg(host, dbg_err, "bad data crc (incoming)\n");
776 cmd->data->error = -EILSEQ;
777 host->status = "error: bad data crc (incoming)";
781 if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {
782 dbg(host, dbg_err, "data timeout\n");
783 cmd->data->error = -ETIMEDOUT;
784 host->status = "error: data timeout";
788 if (mci_dsta & S3C2410_SDIDSTA_XFERFINISH) {
789 if (host->complete_what == COMPLETION_XFERFINISH) {
790 host->status = "ok: data transfer completed";
794 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
795 host->complete_what = COMPLETION_RSPFIN;
797 mci_dclear |= S3C2410_SDIDSTA_XFERFINISH;
801 writel(mci_cclear, host->base + S3C2410_SDICMDSTAT);
802 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
807 host->pio_active = XFER_NONE;
810 host->complete_what = COMPLETION_FINALIZE;
813 tasklet_schedule(&host->pio_tasklet);
819 "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",
820 mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->status);
822 spin_unlock_irqrestore(&host->complete_lock, iflags);
828 * ISR for the CardDetect Pin
831 static irqreturn_t s3cmci_irq_cd(int irq, void *dev_id)
833 struct s3cmci_host *host = (struct s3cmci_host *)dev_id;
835 dbg(host, dbg_irq, "card detect\n");
837 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
842 static void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch,
843 void *buf_id, int size,
844 enum s3c2410_dma_buffresult result)
846 struct s3cmci_host *host = buf_id;
847 unsigned long iflags;
848 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt;
850 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
851 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
852 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
853 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
856 BUG_ON(!host->mrq->data);
857 BUG_ON(!host->dmatogo);
859 spin_lock_irqsave(&host->complete_lock, iflags);
861 if (result != S3C2410_RES_OK) {
862 dbg(host, dbg_fail, "DMA FAILED: csta=0x%08x dsta=0x%08x "
863 "fsta=0x%08x dcnt:0x%08x result:0x%08x toGo:%u\n",
864 mci_csta, mci_dsta, mci_fsta,
865 mci_dcnt, result, host->dmatogo);
872 dbg(host, dbg_dma, "DMA DONE Size:%i DSTA:[%08x] "
873 "DCNT:[%08x] toGo:%u\n",
874 size, mci_dsta, mci_dcnt, host->dmatogo);
879 dbg(host, dbg_dma, "DMA FINISHED Size:%i DSTA:%08x DCNT:%08x\n",
880 size, mci_dsta, mci_dcnt);
882 host->dma_complete = 1;
883 host->complete_what = COMPLETION_FINALIZE;
886 tasklet_schedule(&host->pio_tasklet);
887 spin_unlock_irqrestore(&host->complete_lock, iflags);
891 host->mrq->data->error = -EINVAL;
892 host->complete_what = COMPLETION_FINALIZE;
898 static void finalize_request(struct s3cmci_host *host)
900 struct mmc_request *mrq = host->mrq;
901 struct mmc_command *cmd;
902 int debug_as_failure = 0;
904 if (host->complete_what != COMPLETION_FINALIZE)
909 cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
911 if (cmd->data && (cmd->error == 0) &&
912 (cmd->data->error == 0)) {
913 if (s3cmci_host_usedma(host) && (!host->dma_complete)) {
914 dbg(host, dbg_dma, "DMA Missing (%d)!\n",
920 /* Read response from controller. */
921 cmd->resp[0] = readl(host->base + S3C2410_SDIRSP0);
922 cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1);
923 cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2);
924 cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3);
926 writel(host->prescaler, host->base + S3C2410_SDIPRE);
929 debug_as_failure = 1;
931 if (cmd->data && cmd->data->error)
932 debug_as_failure = 1;
934 dbg_dumpcmd(host, cmd, debug_as_failure);
936 /* Cleanup controller */
937 writel(0, host->base + S3C2410_SDICMDARG);
938 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
939 writel(0, host->base + S3C2410_SDICMDCON);
942 if (cmd->data && cmd->error)
943 cmd->data->error = cmd->error;
945 if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
946 host->cmd_is_stop = 1;
947 s3cmci_send_request(host->mmc);
951 /* If we have no data transfer we are finished here */
955 /* Calculate the amout of bytes transfer if there was no error */
956 if (mrq->data->error == 0) {
957 mrq->data->bytes_xfered =
958 (mrq->data->blocks * mrq->data->blksz);
960 mrq->data->bytes_xfered = 0;
963 /* If we had an error while transferring data we flush the
964 * DMA channel and the fifo to clear out any garbage. */
965 if (mrq->data->error != 0) {
966 if (s3cmci_host_usedma(host))
967 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
970 /* Clear failure register and reset fifo. */
971 writel(S3C2440_SDIFSTA_FIFORESET |
972 S3C2440_SDIFSTA_FIFOFAIL,
973 host->base + S3C2410_SDIFSTA);
978 mci_con = readl(host->base + S3C2410_SDICON);
979 mci_con |= S3C2410_SDICON_FIFORESET;
981 writel(mci_con, host->base + S3C2410_SDICON);
986 host->complete_what = COMPLETION_NONE;
989 s3cmci_check_sdio_irq(host);
990 mmc_request_done(host->mmc, mrq);
993 static void s3cmci_dma_setup(struct s3cmci_host *host,
994 enum dma_data_direction source)
996 static enum dma_data_direction last_source = -1;
999 if (last_source == source)
1002 last_source = source;
1004 s3c2410_dma_devconfig(host->dma, source,
1005 host->mem->start + host->sdidata);
1008 s3c2410_dma_config(host->dma, 4);
1009 s3c2410_dma_set_buffdone_fn(host->dma,
1010 s3cmci_dma_done_callback);
1011 s3c2410_dma_setflags(host->dma, S3C2410_DMAF_AUTOSTART);
1016 static void s3cmci_send_command(struct s3cmci_host *host,
1017 struct mmc_command *cmd)
1021 imsk = S3C2410_SDIIMSK_CRCSTATUS | S3C2410_SDIIMSK_CMDTIMEOUT |
1022 S3C2410_SDIIMSK_RESPONSEND | S3C2410_SDIIMSK_CMDSENT |
1023 S3C2410_SDIIMSK_RESPONSECRC;
1025 enable_imask(host, imsk);
1028 host->complete_what = COMPLETION_XFERFINISH_RSPFIN;
1029 else if (cmd->flags & MMC_RSP_PRESENT)
1030 host->complete_what = COMPLETION_RSPFIN;
1032 host->complete_what = COMPLETION_CMDSENT;
1034 writel(cmd->arg, host->base + S3C2410_SDICMDARG);
1036 ccon = cmd->opcode & S3C2410_SDICMDCON_INDEX;
1037 ccon |= S3C2410_SDICMDCON_SENDERHOST | S3C2410_SDICMDCON_CMDSTART;
1039 if (cmd->flags & MMC_RSP_PRESENT)
1040 ccon |= S3C2410_SDICMDCON_WAITRSP;
1042 if (cmd->flags & MMC_RSP_136)
1043 ccon |= S3C2410_SDICMDCON_LONGRSP;
1045 writel(ccon, host->base + S3C2410_SDICMDCON);
1048 static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
1050 u32 dcon, imsk, stoptries = 3;
1052 /* write DCON register */
1055 writel(0, host->base + S3C2410_SDIDCON);
1059 if ((data->blksz & 3) != 0) {
1060 /* We cannot deal with unaligned blocks with more than
1061 * one block being transferred. */
1063 if (data->blocks > 1) {
1064 pr_warning("%s: can't do non-word sized block transfers (blksz %d)\n", __func__, data->blksz);
1069 while (readl(host->base + S3C2410_SDIDSTA) &
1070 (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {
1073 "mci_setup_data() transfer stillin progress.\n");
1075 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
1078 if ((stoptries--) == 0) {
1079 dbg_dumpregs(host, "DRF");
1084 dcon = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;
1086 if (s3cmci_host_usedma(host))
1087 dcon |= S3C2410_SDIDCON_DMAEN;
1089 if (host->bus_width == MMC_BUS_WIDTH_4)
1090 dcon |= S3C2410_SDIDCON_WIDEBUS;
1092 if (!(data->flags & MMC_DATA_STREAM))
1093 dcon |= S3C2410_SDIDCON_BLOCKMODE;
1095 if (data->flags & MMC_DATA_WRITE) {
1096 dcon |= S3C2410_SDIDCON_TXAFTERRESP;
1097 dcon |= S3C2410_SDIDCON_XFER_TXSTART;
1100 if (data->flags & MMC_DATA_READ) {
1101 dcon |= S3C2410_SDIDCON_RXAFTERCMD;
1102 dcon |= S3C2410_SDIDCON_XFER_RXSTART;
1106 dcon |= S3C2440_SDIDCON_DS_WORD;
1107 dcon |= S3C2440_SDIDCON_DATSTART;
1110 writel(dcon, host->base + S3C2410_SDIDCON);
1112 /* write BSIZE register */
1114 writel(data->blksz, host->base + S3C2410_SDIBSIZE);
1116 /* add to IMASK register */
1117 imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |
1118 S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;
1120 enable_imask(host, imsk);
1122 /* write TIMER register */
1125 writel(0x007FFFFF, host->base + S3C2410_SDITIMER);
1127 writel(0x0000FFFF, host->base + S3C2410_SDITIMER);
1129 /* FIX: set slow clock to prevent timeouts on read */
1130 if (data->flags & MMC_DATA_READ)
1131 writel(0xFF, host->base + S3C2410_SDIPRE);
1137 #define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)
1139 static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
1141 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
1143 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
1145 host->pio_sgptr = 0;
1146 host->pio_bytes = 0;
1147 host->pio_count = 0;
1148 host->pio_active = rw ? XFER_WRITE : XFER_READ;
1152 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
1154 enable_imask(host, S3C2410_SDIIMSK_RXFIFOHALF
1155 | S3C2410_SDIIMSK_RXFIFOLAST);
1161 static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
1164 int rw = data->flags & MMC_DATA_WRITE;
1166 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
1168 s3cmci_dma_setup(host, rw ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1169 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
1171 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1172 rw ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1177 host->dma_complete = 0;
1178 host->dmatogo = dma_len;
1180 for (i = 0; i < dma_len; i++) {
1183 dbg(host, dbg_dma, "enqueue %i: %08x@%u\n", i,
1184 sg_dma_address(&data->sg[i]),
1185 sg_dma_len(&data->sg[i]));
1187 res = s3c2410_dma_enqueue(host->dma, host,
1188 sg_dma_address(&data->sg[i]),
1189 sg_dma_len(&data->sg[i]));
1192 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
1197 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_START);
1202 static void s3cmci_send_request(struct mmc_host *mmc)
1204 struct s3cmci_host *host = mmc_priv(mmc);
1205 struct mmc_request *mrq = host->mrq;
1206 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
1209 prepare_dbgmsg(host, cmd, host->cmd_is_stop);
1211 /* Clear command, data and fifo status registers
1212 Fifo clear only necessary on 2440, but doesn't hurt on 2410
1214 writel(0xFFFFFFFF, host->base + S3C2410_SDICMDSTAT);
1215 writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA);
1216 writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA);
1219 int res = s3cmci_setup_data(host, cmd->data);
1224 dbg(host, dbg_err, "setup data error %d\n", res);
1226 cmd->data->error = res;
1228 mmc_request_done(mmc, mrq);
1232 if (s3cmci_host_usedma(host))
1233 res = s3cmci_prepare_dma(host, cmd->data);
1235 res = s3cmci_prepare_pio(host, cmd->data);
1238 dbg(host, dbg_err, "data prepare error %d\n", res);
1240 cmd->data->error = res;
1242 mmc_request_done(mmc, mrq);
1248 s3cmci_send_command(host, cmd);
1250 /* Enable Interrupt */
1251 s3cmci_enable_irq(host, true);
1254 static int s3cmci_card_present(struct mmc_host *mmc)
1256 struct s3cmci_host *host = mmc_priv(mmc);
1257 struct s3c24xx_mci_pdata *pdata = host->pdata;
1260 if (pdata->no_detect)
1263 ret = gpio_get_value(pdata->gpio_detect) ? 0 : 1;
1264 return ret ^ pdata->detect_invert;
1267 static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1269 struct s3cmci_host *host = mmc_priv(mmc);
1271 host->status = "mmc request";
1272 host->cmd_is_stop = 0;
1275 if (s3cmci_card_present(mmc) == 0) {
1276 dbg(host, dbg_err, "%s: no medium present\n", __func__);
1277 host->mrq->cmd->error = -ENOMEDIUM;
1278 mmc_request_done(mmc, mrq);
1280 s3cmci_send_request(mmc);
1283 static void s3cmci_set_clk(struct s3cmci_host *host, struct mmc_ios *ios)
1288 for (mci_psc = 0; mci_psc < 255; mci_psc++) {
1289 host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));
1291 if (host->real_rate <= ios->clock)
1298 host->prescaler = mci_psc;
1299 writel(host->prescaler, host->base + S3C2410_SDIPRE);
1301 /* If requested clock is 0, real_rate will be 0, too */
1302 if (ios->clock == 0)
1303 host->real_rate = 0;
1306 static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1308 struct s3cmci_host *host = mmc_priv(mmc);
1311 /* Set the power state */
1313 mci_con = readl(host->base + S3C2410_SDICON);
1315 switch (ios->power_mode) {
1318 /* Configure GPE5...GPE10 pins in SD mode */
1319 s3c_gpio_cfgall_range(S3C2410_GPE(5), 6, S3C_GPIO_SFN(2),
1320 S3C_GPIO_PULL_NONE);
1322 if (host->pdata->set_power)
1323 host->pdata->set_power(ios->power_mode, ios->vdd);
1326 mci_con |= S3C2410_SDICON_FIFORESET;
1332 gpio_direction_output(S3C2410_GPE(5), 0);
1335 mci_con |= S3C2440_SDICON_SDRESET;
1337 if (host->pdata->set_power)
1338 host->pdata->set_power(ios->power_mode, ios->vdd);
1343 s3cmci_set_clk(host, ios);
1345 /* Set CLOCK_ENABLE */
1347 mci_con |= S3C2410_SDICON_CLOCKTYPE;
1349 mci_con &= ~S3C2410_SDICON_CLOCKTYPE;
1351 writel(mci_con, host->base + S3C2410_SDICON);
1353 if ((ios->power_mode == MMC_POWER_ON) ||
1354 (ios->power_mode == MMC_POWER_UP)) {
1355 dbg(host, dbg_conf, "running at %lukHz (requested: %ukHz).\n",
1356 host->real_rate/1000, ios->clock/1000);
1358 dbg(host, dbg_conf, "powered down.\n");
1361 host->bus_width = ios->bus_width;
1364 static void s3cmci_reset(struct s3cmci_host *host)
1366 u32 con = readl(host->base + S3C2410_SDICON);
1368 con |= S3C2440_SDICON_SDRESET;
1369 writel(con, host->base + S3C2410_SDICON);
1372 static int s3cmci_get_ro(struct mmc_host *mmc)
1374 struct s3cmci_host *host = mmc_priv(mmc);
1375 struct s3c24xx_mci_pdata *pdata = host->pdata;
1378 if (pdata->no_wprotect)
1381 ret = gpio_get_value(pdata->gpio_wprotect) ? 1 : 0;
1382 ret ^= pdata->wprotect_invert;
1387 static void s3cmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1389 struct s3cmci_host *host = mmc_priv(mmc);
1390 unsigned long flags;
1393 local_irq_save(flags);
1395 con = readl(host->base + S3C2410_SDICON);
1396 host->sdio_irqen = enable;
1398 if (enable == host->sdio_irqen)
1402 con |= S3C2410_SDICON_SDIOIRQ;
1403 enable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
1405 if (!host->irq_state && !host->irq_disabled) {
1406 host->irq_state = true;
1407 enable_irq(host->irq);
1410 disable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
1411 con &= ~S3C2410_SDICON_SDIOIRQ;
1413 if (!host->irq_enabled && host->irq_state) {
1414 disable_irq_nosync(host->irq);
1415 host->irq_state = false;
1419 writel(con, host->base + S3C2410_SDICON);
1422 local_irq_restore(flags);
1424 s3cmci_check_sdio_irq(host);
1427 static struct mmc_host_ops s3cmci_ops = {
1428 .request = s3cmci_request,
1429 .set_ios = s3cmci_set_ios,
1430 .get_ro = s3cmci_get_ro,
1431 .get_cd = s3cmci_card_present,
1432 .enable_sdio_irq = s3cmci_enable_sdio_irq,
1435 static struct s3c24xx_mci_pdata s3cmci_def_pdata = {
1436 /* This is currently here to avoid a number of if (host->pdata)
1437 * checks. Any zero fields to ensure reasonable defaults are picked. */
1442 #ifdef CONFIG_CPU_FREQ
1444 static int s3cmci_cpufreq_transition(struct notifier_block *nb,
1445 unsigned long val, void *data)
1447 struct s3cmci_host *host;
1448 struct mmc_host *mmc;
1449 unsigned long newclk;
1450 unsigned long flags;
1452 host = container_of(nb, struct s3cmci_host, freq_transition);
1453 newclk = clk_get_rate(host->clk);
1456 if ((val == CPUFREQ_PRECHANGE && newclk > host->clk_rate) ||
1457 (val == CPUFREQ_POSTCHANGE && newclk < host->clk_rate)) {
1458 spin_lock_irqsave(&mmc->lock, flags);
1460 host->clk_rate = newclk;
1462 if (mmc->ios.power_mode != MMC_POWER_OFF &&
1463 mmc->ios.clock != 0)
1464 s3cmci_set_clk(host, &mmc->ios);
1466 spin_unlock_irqrestore(&mmc->lock, flags);
1472 static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1474 host->freq_transition.notifier_call = s3cmci_cpufreq_transition;
1476 return cpufreq_register_notifier(&host->freq_transition,
1477 CPUFREQ_TRANSITION_NOTIFIER);
1480 static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1482 cpufreq_unregister_notifier(&host->freq_transition,
1483 CPUFREQ_TRANSITION_NOTIFIER);
1487 static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1492 static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1498 #ifdef CONFIG_DEBUG_FS
1500 static int s3cmci_state_show(struct seq_file *seq, void *v)
1502 struct s3cmci_host *host = seq->private;
1504 seq_printf(seq, "Register base = 0x%08x\n", (u32)host->base);
1505 seq_printf(seq, "Clock rate = %ld\n", host->clk_rate);
1506 seq_printf(seq, "Prescale = %d\n", host->prescaler);
1507 seq_printf(seq, "is2440 = %d\n", host->is2440);
1508 seq_printf(seq, "IRQ = %d\n", host->irq);
1509 seq_printf(seq, "IRQ enabled = %d\n", host->irq_enabled);
1510 seq_printf(seq, "IRQ disabled = %d\n", host->irq_disabled);
1511 seq_printf(seq, "IRQ state = %d\n", host->irq_state);
1512 seq_printf(seq, "CD IRQ = %d\n", host->irq_cd);
1513 seq_printf(seq, "Do DMA = %d\n", s3cmci_host_usedma(host));
1514 seq_printf(seq, "SDIIMSK at %d\n", host->sdiimsk);
1515 seq_printf(seq, "SDIDATA at %d\n", host->sdidata);
1520 static int s3cmci_state_open(struct inode *inode, struct file *file)
1522 return single_open(file, s3cmci_state_show, inode->i_private);
1525 static const struct file_operations s3cmci_fops_state = {
1526 .owner = THIS_MODULE,
1527 .open = s3cmci_state_open,
1529 .llseek = seq_lseek,
1530 .release = single_release,
1533 #define DBG_REG(_r) { .addr = S3C2410_SDI##_r, .name = #_r }
1536 unsigned short addr;
1537 unsigned char *name;
1557 static int s3cmci_regs_show(struct seq_file *seq, void *v)
1559 struct s3cmci_host *host = seq->private;
1560 struct s3cmci_reg *rptr = debug_regs;
1562 for (; rptr->name; rptr++)
1563 seq_printf(seq, "SDI%s\t=0x%08x\n", rptr->name,
1564 readl(host->base + rptr->addr));
1566 seq_printf(seq, "SDIIMSK\t=0x%08x\n", readl(host->base + host->sdiimsk));
1571 static int s3cmci_regs_open(struct inode *inode, struct file *file)
1573 return single_open(file, s3cmci_regs_show, inode->i_private);
1576 static const struct file_operations s3cmci_fops_regs = {
1577 .owner = THIS_MODULE,
1578 .open = s3cmci_regs_open,
1580 .llseek = seq_lseek,
1581 .release = single_release,
1584 static void s3cmci_debugfs_attach(struct s3cmci_host *host)
1586 struct device *dev = &host->pdev->dev;
1588 host->debug_root = debugfs_create_dir(dev_name(dev), NULL);
1589 if (IS_ERR(host->debug_root)) {
1590 dev_err(dev, "failed to create debugfs root\n");
1594 host->debug_state = debugfs_create_file("state", 0444,
1595 host->debug_root, host,
1596 &s3cmci_fops_state);
1598 if (IS_ERR(host->debug_state))
1599 dev_err(dev, "failed to create debug state file\n");
1601 host->debug_regs = debugfs_create_file("regs", 0444,
1602 host->debug_root, host,
1605 if (IS_ERR(host->debug_regs))
1606 dev_err(dev, "failed to create debug regs file\n");
1609 static void s3cmci_debugfs_remove(struct s3cmci_host *host)
1611 debugfs_remove(host->debug_regs);
1612 debugfs_remove(host->debug_state);
1613 debugfs_remove(host->debug_root);
1617 static inline void s3cmci_debugfs_attach(struct s3cmci_host *host) { }
1618 static inline void s3cmci_debugfs_remove(struct s3cmci_host *host) { }
1620 #endif /* CONFIG_DEBUG_FS */
1622 static int s3cmci_probe(struct platform_device *pdev)
1624 struct s3cmci_host *host;
1625 struct mmc_host *mmc;
1630 is2440 = platform_get_device_id(pdev)->driver_data;
1632 mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
1638 for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++) {
1639 ret = gpio_request(i, dev_name(&pdev->dev));
1641 dev_err(&pdev->dev, "failed to get gpio %d\n", i);
1643 for (i--; i >= S3C2410_GPE(5); i--)
1646 goto probe_free_host;
1650 host = mmc_priv(mmc);
1653 host->is2440 = is2440;
1655 host->pdata = pdev->dev.platform_data;
1657 pdev->dev.platform_data = &s3cmci_def_pdata;
1658 host->pdata = &s3cmci_def_pdata;
1661 spin_lock_init(&host->complete_lock);
1662 tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);
1665 host->sdiimsk = S3C2440_SDIIMSK;
1666 host->sdidata = S3C2440_SDIDATA;
1669 host->sdiimsk = S3C2410_SDIIMSK;
1670 host->sdidata = S3C2410_SDIDATA;
1674 host->complete_what = COMPLETION_NONE;
1675 host->pio_active = XFER_NONE;
1677 #ifdef CONFIG_MMC_S3C_PIODMA
1678 host->dodma = host->pdata->use_dma;
1681 host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1684 "failed to get io memory region resource.\n");
1687 goto probe_free_gpio;
1690 host->mem = request_mem_region(host->mem->start,
1691 resource_size(host->mem), pdev->name);
1694 dev_err(&pdev->dev, "failed to request io memory region.\n");
1696 goto probe_free_gpio;
1699 host->base = ioremap(host->mem->start, resource_size(host->mem));
1701 dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
1703 goto probe_free_mem_region;
1706 host->irq = platform_get_irq(pdev, 0);
1707 if (host->irq == 0) {
1708 dev_err(&pdev->dev, "failed to get interrupt resource.\n");
1713 if (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)) {
1714 dev_err(&pdev->dev, "failed to request mci interrupt.\n");
1719 /* We get spurious interrupts even when we have set the IMSK
1720 * register to ignore everything, so use disable_irq() to make
1721 * ensure we don't lock the system with un-serviceable requests. */
1723 disable_irq(host->irq);
1724 host->irq_state = false;
1726 if (!host->pdata->no_detect) {
1727 ret = gpio_request(host->pdata->gpio_detect, "s3cmci detect");
1729 dev_err(&pdev->dev, "failed to get detect gpio\n");
1730 goto probe_free_irq;
1733 host->irq_cd = gpio_to_irq(host->pdata->gpio_detect);
1735 if (host->irq_cd >= 0) {
1736 if (request_irq(host->irq_cd, s3cmci_irq_cd,
1737 IRQF_TRIGGER_RISING |
1738 IRQF_TRIGGER_FALLING,
1739 DRIVER_NAME, host)) {
1741 "can't get card detect irq.\n");
1743 goto probe_free_gpio_cd;
1746 dev_warn(&pdev->dev,
1747 "host detect has no irq available\n");
1748 gpio_direction_input(host->pdata->gpio_detect);
1753 if (!host->pdata->no_wprotect) {
1754 ret = gpio_request(host->pdata->gpio_wprotect, "s3cmci wp");
1756 dev_err(&pdev->dev, "failed to get writeprotect\n");
1757 goto probe_free_irq_cd;
1760 gpio_direction_input(host->pdata->gpio_wprotect);
1763 /* depending on the dma state, get a dma channel to use. */
1765 if (s3cmci_host_usedma(host)) {
1766 host->dma = s3c2410_dma_request(DMACH_SDI, &s3cmci_dma_client,
1768 if (host->dma < 0) {
1769 dev_err(&pdev->dev, "cannot get DMA channel.\n");
1770 if (!s3cmci_host_canpio()) {
1772 goto probe_free_gpio_wp;
1774 dev_warn(&pdev->dev, "falling back to PIO.\n");
1780 host->clk = clk_get(&pdev->dev, "sdi");
1781 if (IS_ERR(host->clk)) {
1782 dev_err(&pdev->dev, "failed to find clock source.\n");
1783 ret = PTR_ERR(host->clk);
1785 goto probe_free_dma;
1788 ret = clk_enable(host->clk);
1790 dev_err(&pdev->dev, "failed to enable clock source.\n");
1794 host->clk_rate = clk_get_rate(host->clk);
1796 mmc->ops = &s3cmci_ops;
1797 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1798 #ifdef CONFIG_MMC_S3C_HW_SDIO_IRQ
1799 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
1801 mmc->caps = MMC_CAP_4_BIT_DATA;
1803 mmc->f_min = host->clk_rate / (host->clk_div * 256);
1804 mmc->f_max = host->clk_rate / host->clk_div;
1806 if (host->pdata->ocr_avail)
1807 mmc->ocr_avail = host->pdata->ocr_avail;
1809 mmc->max_blk_count = 4095;
1810 mmc->max_blk_size = 4095;
1811 mmc->max_req_size = 4095 * 512;
1812 mmc->max_seg_size = mmc->max_req_size;
1814 mmc->max_segs = 128;
1816 dbg(host, dbg_debug,
1817 "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%u.\n",
1818 (host->is2440?"2440":""),
1819 host->base, host->irq, host->irq_cd, host->dma);
1821 ret = s3cmci_cpufreq_register(host);
1823 dev_err(&pdev->dev, "failed to register cpufreq\n");
1827 ret = mmc_add_host(mmc);
1829 dev_err(&pdev->dev, "failed to add mmc host.\n");
1833 s3cmci_debugfs_attach(host);
1835 platform_set_drvdata(pdev, mmc);
1836 dev_info(&pdev->dev, "%s - using %s, %s SDIO IRQ\n", mmc_hostname(mmc),
1837 s3cmci_host_usedma(host) ? "dma" : "pio",
1838 mmc->caps & MMC_CAP_SDIO_IRQ ? "hw" : "sw");
1843 s3cmci_cpufreq_deregister(host);
1846 clk_disable(host->clk);
1852 if (s3cmci_host_usedma(host))
1853 s3c2410_dma_free(host->dma, &s3cmci_dma_client);
1856 if (!host->pdata->no_wprotect)
1857 gpio_free(host->pdata->gpio_wprotect);
1860 if (!host->pdata->no_detect)
1861 gpio_free(host->pdata->gpio_detect);
1864 if (host->irq_cd >= 0)
1865 free_irq(host->irq_cd, host);
1868 free_irq(host->irq, host);
1871 iounmap(host->base);
1873 probe_free_mem_region:
1874 release_mem_region(host->mem->start, resource_size(host->mem));
1877 for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++)
1887 static void s3cmci_shutdown(struct platform_device *pdev)
1889 struct mmc_host *mmc = platform_get_drvdata(pdev);
1890 struct s3cmci_host *host = mmc_priv(mmc);
1892 if (host->irq_cd >= 0)
1893 free_irq(host->irq_cd, host);
1895 s3cmci_debugfs_remove(host);
1896 s3cmci_cpufreq_deregister(host);
1897 mmc_remove_host(mmc);
1898 clk_disable(host->clk);
1901 static int s3cmci_remove(struct platform_device *pdev)
1903 struct mmc_host *mmc = platform_get_drvdata(pdev);
1904 struct s3cmci_host *host = mmc_priv(mmc);
1905 struct s3c24xx_mci_pdata *pd = host->pdata;
1908 s3cmci_shutdown(pdev);
1912 tasklet_disable(&host->pio_tasklet);
1914 if (s3cmci_host_usedma(host))
1915 s3c2410_dma_free(host->dma, &s3cmci_dma_client);
1917 free_irq(host->irq, host);
1919 if (!pd->no_wprotect)
1920 gpio_free(pd->gpio_wprotect);
1923 gpio_free(pd->gpio_detect);
1925 for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++)
1929 iounmap(host->base);
1930 release_mem_region(host->mem->start, resource_size(host->mem));
1936 static struct platform_device_id s3cmci_driver_ids[] = {
1938 .name = "s3c2410-sdi",
1941 .name = "s3c2412-sdi",
1944 .name = "s3c2440-sdi",
1950 MODULE_DEVICE_TABLE(platform, s3cmci_driver_ids);
1955 static int s3cmci_suspend(struct device *dev)
1957 struct mmc_host *mmc = platform_get_drvdata(to_platform_device(dev));
1959 return mmc_suspend_host(mmc);
1962 static int s3cmci_resume(struct device *dev)
1964 struct mmc_host *mmc = platform_get_drvdata(to_platform_device(dev));
1966 return mmc_resume_host(mmc);
1969 static const struct dev_pm_ops s3cmci_pm = {
1970 .suspend = s3cmci_suspend,
1971 .resume = s3cmci_resume,
1974 #define s3cmci_pm_ops &s3cmci_pm
1975 #else /* CONFIG_PM */
1976 #define s3cmci_pm_ops NULL
1977 #endif /* CONFIG_PM */
1980 static struct platform_driver s3cmci_driver = {
1983 .owner = THIS_MODULE,
1984 .pm = s3cmci_pm_ops,
1986 .id_table = s3cmci_driver_ids,
1987 .probe = s3cmci_probe,
1988 .remove = s3cmci_remove,
1989 .shutdown = s3cmci_shutdown,
1992 module_platform_driver(s3cmci_driver);
1994 MODULE_DESCRIPTION("Samsung S3C MMC/SD Card Interface driver");
1995 MODULE_LICENSE("GPL v2");