1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/mmc/s3cmci.h - Samsung S3C MCI driver
5 * Copyright (C) 2004-2006 maintech GmbH, Thomas Kleffel <tk@maintech.de>
7 * Current driver maintained by Ben Dooks and Simtec Electronics
8 * Copyright (C) 2008 Simtec Electronics <ben-linux@fluff.org>
11 #include <linux/module.h>
12 #include <linux/dmaengine.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/clk.h>
15 #include <linux/mmc/host.h>
16 #include <linux/platform_device.h>
17 #include <linux/cpufreq.h>
18 #include <linux/debugfs.h>
19 #include <linux/seq_file.h>
20 #include <linux/gpio.h>
21 #include <linux/interrupt.h>
22 #include <linux/irq.h>
25 #include <linux/of_device.h>
26 #include <linux/mmc/slot-gpio.h>
28 #include <plat/gpio-cfg.h>
30 #include <mach/gpio-samsung.h>
32 #include <linux/platform_data/mmc-s3cmci.h>
36 #define DRIVER_NAME "s3c-mci"
38 #define S3C2410_SDICON (0x00)
39 #define S3C2410_SDIPRE (0x04)
40 #define S3C2410_SDICMDARG (0x08)
41 #define S3C2410_SDICMDCON (0x0C)
42 #define S3C2410_SDICMDSTAT (0x10)
43 #define S3C2410_SDIRSP0 (0x14)
44 #define S3C2410_SDIRSP1 (0x18)
45 #define S3C2410_SDIRSP2 (0x1C)
46 #define S3C2410_SDIRSP3 (0x20)
47 #define S3C2410_SDITIMER (0x24)
48 #define S3C2410_SDIBSIZE (0x28)
49 #define S3C2410_SDIDCON (0x2C)
50 #define S3C2410_SDIDCNT (0x30)
51 #define S3C2410_SDIDSTA (0x34)
52 #define S3C2410_SDIFSTA (0x38)
54 #define S3C2410_SDIDATA (0x3C)
55 #define S3C2410_SDIIMSK (0x40)
57 #define S3C2440_SDIDATA (0x40)
58 #define S3C2440_SDIIMSK (0x3C)
60 #define S3C2440_SDICON_SDRESET (1 << 8)
61 #define S3C2410_SDICON_SDIOIRQ (1 << 3)
62 #define S3C2410_SDICON_FIFORESET (1 << 1)
63 #define S3C2410_SDICON_CLOCKTYPE (1 << 0)
65 #define S3C2410_SDICMDCON_LONGRSP (1 << 10)
66 #define S3C2410_SDICMDCON_WAITRSP (1 << 9)
67 #define S3C2410_SDICMDCON_CMDSTART (1 << 8)
68 #define S3C2410_SDICMDCON_SENDERHOST (1 << 6)
69 #define S3C2410_SDICMDCON_INDEX (0x3f)
71 #define S3C2410_SDICMDSTAT_CRCFAIL (1 << 12)
72 #define S3C2410_SDICMDSTAT_CMDSENT (1 << 11)
73 #define S3C2410_SDICMDSTAT_CMDTIMEOUT (1 << 10)
74 #define S3C2410_SDICMDSTAT_RSPFIN (1 << 9)
76 #define S3C2440_SDIDCON_DS_WORD (2 << 22)
77 #define S3C2410_SDIDCON_TXAFTERRESP (1 << 20)
78 #define S3C2410_SDIDCON_RXAFTERCMD (1 << 19)
79 #define S3C2410_SDIDCON_BLOCKMODE (1 << 17)
80 #define S3C2410_SDIDCON_WIDEBUS (1 << 16)
81 #define S3C2410_SDIDCON_DMAEN (1 << 15)
82 #define S3C2410_SDIDCON_STOP (1 << 14)
83 #define S3C2440_SDIDCON_DATSTART (1 << 14)
85 #define S3C2410_SDIDCON_XFER_RXSTART (2 << 12)
86 #define S3C2410_SDIDCON_XFER_TXSTART (3 << 12)
88 #define S3C2410_SDIDCON_BLKNUM_MASK (0xFFF)
90 #define S3C2410_SDIDSTA_SDIOIRQDETECT (1 << 9)
91 #define S3C2410_SDIDSTA_FIFOFAIL (1 << 8)
92 #define S3C2410_SDIDSTA_CRCFAIL (1 << 7)
93 #define S3C2410_SDIDSTA_RXCRCFAIL (1 << 6)
94 #define S3C2410_SDIDSTA_DATATIMEOUT (1 << 5)
95 #define S3C2410_SDIDSTA_XFERFINISH (1 << 4)
96 #define S3C2410_SDIDSTA_TXDATAON (1 << 1)
97 #define S3C2410_SDIDSTA_RXDATAON (1 << 0)
99 #define S3C2440_SDIFSTA_FIFORESET (1 << 16)
100 #define S3C2440_SDIFSTA_FIFOFAIL (3 << 14)
101 #define S3C2410_SDIFSTA_TFDET (1 << 13)
102 #define S3C2410_SDIFSTA_RFDET (1 << 12)
103 #define S3C2410_SDIFSTA_COUNTMASK (0x7f)
105 #define S3C2410_SDIIMSK_RESPONSECRC (1 << 17)
106 #define S3C2410_SDIIMSK_CMDSENT (1 << 16)
107 #define S3C2410_SDIIMSK_CMDTIMEOUT (1 << 15)
108 #define S3C2410_SDIIMSK_RESPONSEND (1 << 14)
109 #define S3C2410_SDIIMSK_SDIOIRQ (1 << 12)
110 #define S3C2410_SDIIMSK_FIFOFAIL (1 << 11)
111 #define S3C2410_SDIIMSK_CRCSTATUS (1 << 10)
112 #define S3C2410_SDIIMSK_DATACRC (1 << 9)
113 #define S3C2410_SDIIMSK_DATATIMEOUT (1 << 8)
114 #define S3C2410_SDIIMSK_DATAFINISH (1 << 7)
115 #define S3C2410_SDIIMSK_TXFIFOHALF (1 << 4)
116 #define S3C2410_SDIIMSK_RXFIFOLAST (1 << 2)
117 #define S3C2410_SDIIMSK_RXFIFOHALF (1 << 0)
121 dbg_debug = (1 << 1),
131 static const int dbgmap_err = dbg_fail;
132 static const int dbgmap_info = dbg_info | dbg_conf;
133 static const int dbgmap_debug = dbg_err | dbg_debug;
135 #define dbg(host, channels, args...) \
137 if (dbgmap_err & channels) \
138 dev_err(&host->pdev->dev, args); \
139 else if (dbgmap_info & channels) \
140 dev_info(&host->pdev->dev, args); \
141 else if (dbgmap_debug & channels) \
142 dev_dbg(&host->pdev->dev, args); \
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;
154 u32 datcon, datcnt, datsta, fsta;
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 datcon = readl(host->base + S3C2410_SDIDCON);
167 datcnt = readl(host->base + S3C2410_SDIDCNT);
168 datsta = readl(host->base + S3C2410_SDIDSTA);
169 fsta = readl(host->base + S3C2410_SDIFSTA);
171 dbg(host, dbg_debug, "%s CON:[%08x] PRE:[%08x] TMR:[%08x]\n",
172 prefix, con, pre, timer);
174 dbg(host, dbg_debug, "%s CCON:[%08x] CARG:[%08x] CSTA:[%08x]\n",
175 prefix, cmdcon, cmdarg, cmdsta);
177 dbg(host, dbg_debug, "%s DCON:[%08x] FSTA:[%08x]"
178 " DSTA:[%08x] DCNT:[%08x]\n",
179 prefix, datcon, fsta, datsta, datcnt);
181 dbg(host, dbg_debug, "%s R0:[%08x] R1:[%08x]"
182 " R2:[%08x] R3:[%08x]\n",
183 prefix, r0, r1, r2, r3);
186 static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
189 snprintf(host->dbgmsg_cmd, 300,
190 "#%u%s op:%i arg:0x%08x flags:0x08%x retries:%u",
191 host->ccnt, (stop ? " (STOP)" : ""),
192 cmd->opcode, cmd->arg, cmd->flags, cmd->retries);
195 snprintf(host->dbgmsg_dat, 300,
196 "#%u bsize:%u blocks:%u bytes:%u",
197 host->dcnt, cmd->data->blksz,
199 cmd->data->blocks * cmd->data->blksz);
201 host->dbgmsg_dat[0] = '\0';
205 static void dbg_dumpcmd(struct s3cmci_host *host, struct mmc_command *cmd,
208 unsigned int dbglvl = fail ? dbg_fail : dbg_debug;
213 if (cmd->error == 0) {
214 dbg(host, dbglvl, "CMD[OK] %s R0:0x%08x\n",
215 host->dbgmsg_cmd, cmd->resp[0]);
217 dbg(host, dbglvl, "CMD[ERR %i] %s Status:%s\n",
218 cmd->error, host->dbgmsg_cmd, host->status);
224 if (cmd->data->error == 0) {
225 dbg(host, dbglvl, "DAT[OK] %s\n", host->dbgmsg_dat);
227 dbg(host, dbglvl, "DAT[ERR %i] %s DCNT:0x%08x\n",
228 cmd->data->error, host->dbgmsg_dat,
229 readl(host->base + S3C2410_SDIDCNT));
233 static void dbg_dumpcmd(struct s3cmci_host *host,
234 struct mmc_command *cmd, int fail) { }
236 static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
239 static void dbg_dumpregs(struct s3cmci_host *host, char *prefix) { }
241 #endif /* CONFIG_MMC_DEBUG */
244 * s3cmci_host_usedma - return whether the host is using dma or pio
245 * @host: The host state
247 * Return true if the host is using DMA to transfer data, else false
248 * to use PIO mode. Will return static data depending on the driver
251 static inline bool s3cmci_host_usedma(struct s3cmci_host *host)
253 #ifdef CONFIG_MMC_S3C_PIO
255 #else /* CONFIG_MMC_S3C_DMA */
260 static inline u32 enable_imask(struct s3cmci_host *host, u32 imask)
264 newmask = readl(host->base + host->sdiimsk);
267 writel(newmask, host->base + host->sdiimsk);
272 static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
276 newmask = readl(host->base + host->sdiimsk);
279 writel(newmask, host->base + host->sdiimsk);
284 static inline void clear_imask(struct s3cmci_host *host)
286 u32 mask = readl(host->base + host->sdiimsk);
288 /* preserve the SDIO IRQ mask state */
289 mask &= S3C2410_SDIIMSK_SDIOIRQ;
290 writel(mask, host->base + host->sdiimsk);
294 * s3cmci_check_sdio_irq - test whether the SDIO IRQ is being signalled
295 * @host: The host to check.
297 * Test to see if the SDIO interrupt is being signalled in case the
298 * controller has failed to re-detect a card interrupt. Read GPE8 and
299 * see if it is low and if so, signal a SDIO interrupt.
301 * This is currently called if a request is finished (we assume that the
302 * bus is now idle) and when the SDIO IRQ is enabled in case the IRQ is
303 * already being indicated.
305 static void s3cmci_check_sdio_irq(struct s3cmci_host *host)
307 if (host->sdio_irqen) {
308 if (gpio_get_value(S3C2410_GPE(8)) == 0) {
309 pr_debug("%s: signalling irq\n", __func__);
310 mmc_signal_sdio_irq(host->mmc);
315 static inline int get_data_buffer(struct s3cmci_host *host,
316 u32 *bytes, u32 **pointer)
318 struct scatterlist *sg;
320 if (host->pio_active == XFER_NONE)
323 if ((!host->mrq) || (!host->mrq->data))
326 if (host->pio_sgptr >= host->mrq->data->sg_len) {
327 dbg(host, dbg_debug, "no more buffers (%i/%i)\n",
328 host->pio_sgptr, host->mrq->data->sg_len);
331 sg = &host->mrq->data->sg[host->pio_sgptr];
334 *pointer = sg_virt(sg);
338 dbg(host, dbg_sg, "new buffer (%i/%i)\n",
339 host->pio_sgptr, host->mrq->data->sg_len);
344 static inline u32 fifo_count(struct s3cmci_host *host)
346 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
348 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
352 static inline u32 fifo_free(struct s3cmci_host *host)
354 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
356 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
357 return 63 - fifostat;
361 * s3cmci_enable_irq - enable IRQ, after having disabled it.
362 * @host: The device state.
363 * @more: True if more IRQs are expected from transfer.
365 * Enable the main IRQ if needed after it has been disabled.
367 * The IRQ can be one of the following states:
368 * - disabled during IDLE
369 * - disabled whilst processing data
370 * - enabled during transfer
371 * - enabled whilst awaiting SDIO interrupt detection
373 static void s3cmci_enable_irq(struct s3cmci_host *host, bool more)
378 local_irq_save(flags);
380 host->irq_enabled = more;
381 host->irq_disabled = false;
383 enable = more | host->sdio_irqen;
385 if (host->irq_state != enable) {
386 host->irq_state = enable;
389 enable_irq(host->irq);
391 disable_irq(host->irq);
394 local_irq_restore(flags);
397 static void s3cmci_disable_irq(struct s3cmci_host *host, bool transfer)
401 local_irq_save(flags);
403 /* pr_debug("%s: transfer %d\n", __func__, transfer); */
405 host->irq_disabled = transfer;
407 if (transfer && host->irq_state) {
408 host->irq_state = false;
409 disable_irq(host->irq);
412 local_irq_restore(flags);
415 static void do_pio_read(struct s3cmci_host *host)
421 void __iomem *from_ptr;
423 /* write real prescaler to host, it might be set slow to fix */
424 writel(host->prescaler, host->base + S3C2410_SDIPRE);
426 from_ptr = host->base + host->sdidata;
428 while ((fifo = fifo_count(host))) {
429 if (!host->pio_bytes) {
430 res = get_data_buffer(host, &host->pio_bytes,
433 host->pio_active = XFER_NONE;
434 host->complete_what = COMPLETION_FINALIZE;
436 dbg(host, dbg_pio, "pio_read(): "
437 "complete (no more data).\n");
442 "pio_read(): new target: [%i]@[%p]\n",
443 host->pio_bytes, host->pio_ptr);
447 "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
448 fifo, host->pio_bytes,
449 readl(host->base + S3C2410_SDIDCNT));
451 /* If we have reached the end of the block, we can
452 * read a word and get 1 to 3 bytes. If we in the
453 * middle of the block, we have to read full words,
454 * otherwise we will write garbage, so round down to
455 * an even multiple of 4. */
456 if (fifo >= host->pio_bytes)
457 fifo = host->pio_bytes;
461 host->pio_bytes -= fifo;
462 host->pio_count += fifo;
464 fifo_words = fifo >> 2;
467 *ptr++ = readl(from_ptr);
472 u32 data = readl(from_ptr);
473 u8 *p = (u8 *)host->pio_ptr;
482 if (!host->pio_bytes) {
483 res = get_data_buffer(host, &host->pio_bytes, &host->pio_ptr);
486 "pio_read(): complete (no more buffers).\n");
487 host->pio_active = XFER_NONE;
488 host->complete_what = COMPLETION_FINALIZE;
495 S3C2410_SDIIMSK_RXFIFOHALF | S3C2410_SDIIMSK_RXFIFOLAST);
498 static void do_pio_write(struct s3cmci_host *host)
500 void __iomem *to_ptr;
505 to_ptr = host->base + host->sdidata;
507 while ((fifo = fifo_free(host)) > 3) {
508 if (!host->pio_bytes) {
509 res = get_data_buffer(host, &host->pio_bytes,
513 "pio_write(): complete (no more data).\n");
514 host->pio_active = XFER_NONE;
520 "pio_write(): new source: [%i]@[%p]\n",
521 host->pio_bytes, host->pio_ptr);
525 /* If we have reached the end of the block, we have to
526 * write exactly the remaining number of bytes. If we
527 * in the middle of the block, we have to write full
528 * words, so round down to an even multiple of 4. */
529 if (fifo >= host->pio_bytes)
530 fifo = host->pio_bytes;
534 host->pio_bytes -= fifo;
535 host->pio_count += fifo;
537 fifo = (fifo + 3) >> 2;
540 writel(*ptr++, to_ptr);
544 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
547 static void pio_tasklet(unsigned long data)
549 struct s3cmci_host *host = (struct s3cmci_host *) data;
551 s3cmci_disable_irq(host, true);
553 if (host->pio_active == XFER_WRITE)
556 if (host->pio_active == XFER_READ)
559 if (host->complete_what == COMPLETION_FINALIZE) {
561 if (host->pio_active != XFER_NONE) {
562 dbg(host, dbg_err, "unfinished %s "
563 "- pio_count:[%u] pio_bytes:[%u]\n",
564 (host->pio_active == XFER_READ) ? "read" : "write",
565 host->pio_count, host->pio_bytes);
568 host->mrq->data->error = -EINVAL;
571 s3cmci_enable_irq(host, false);
572 finalize_request(host);
574 s3cmci_enable_irq(host, true);
578 * ISR for SDI Interface IRQ
579 * Communication between driver and ISR works as follows:
580 * host->mrq points to current request
581 * host->complete_what Indicates when the request is considered done
582 * COMPLETION_CMDSENT when the command was sent
583 * COMPLETION_RSPFIN when a response was received
584 * COMPLETION_XFERFINISH when the data transfer is finished
585 * COMPLETION_XFERFINISH_RSPFIN both of the above.
586 * host->complete_request is the completion-object the driver waits for
588 * 1) Driver sets up host->mrq and host->complete_what
589 * 2) Driver prepares the transfer
590 * 3) Driver enables interrupts
591 * 4) Driver starts transfer
592 * 5) Driver waits for host->complete_rquest
593 * 6) ISR checks for request status (errors and success)
594 * 6) ISR sets host->mrq->cmd->error and host->mrq->data->error
595 * 7) ISR completes host->complete_request
596 * 8) ISR disables interrupts
597 * 9) Driver wakes up and takes care of the request
599 * Note: "->error"-fields are expected to be set to 0 before the request
600 * was issued by mmc.c - therefore they are only set, when an error
604 static irqreturn_t s3cmci_irq(int irq, void *dev_id)
606 struct s3cmci_host *host = dev_id;
607 struct mmc_command *cmd;
608 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
609 u32 mci_cclear = 0, mci_dclear;
610 unsigned long iflags;
612 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
613 mci_imsk = readl(host->base + host->sdiimsk);
615 if (mci_dsta & S3C2410_SDIDSTA_SDIOIRQDETECT) {
616 if (mci_imsk & S3C2410_SDIIMSK_SDIOIRQ) {
617 mci_dclear = S3C2410_SDIDSTA_SDIOIRQDETECT;
618 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
620 mmc_signal_sdio_irq(host->mmc);
625 spin_lock_irqsave(&host->complete_lock, iflags);
627 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
628 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
629 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
632 if ((host->complete_what == COMPLETION_NONE) ||
633 (host->complete_what == COMPLETION_FINALIZE)) {
634 host->status = "nothing to complete";
640 host->status = "no active mrq";
645 cmd = host->cmd_is_stop ? host->mrq->stop : host->mrq->cmd;
648 host->status = "no active cmd";
653 if (!s3cmci_host_usedma(host)) {
654 if ((host->pio_active == XFER_WRITE) &&
655 (mci_fsta & S3C2410_SDIFSTA_TFDET)) {
657 disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
658 tasklet_schedule(&host->pio_tasklet);
659 host->status = "pio tx";
662 if ((host->pio_active == XFER_READ) &&
663 (mci_fsta & S3C2410_SDIFSTA_RFDET)) {
666 S3C2410_SDIIMSK_RXFIFOHALF |
667 S3C2410_SDIIMSK_RXFIFOLAST);
669 tasklet_schedule(&host->pio_tasklet);
670 host->status = "pio rx";
674 if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {
675 dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");
676 cmd->error = -ETIMEDOUT;
677 host->status = "error: command timeout";
681 if (mci_csta & S3C2410_SDICMDSTAT_CMDSENT) {
682 if (host->complete_what == COMPLETION_CMDSENT) {
683 host->status = "ok: command sent";
687 mci_cclear |= S3C2410_SDICMDSTAT_CMDSENT;
690 if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {
691 if (cmd->flags & MMC_RSP_CRC) {
692 if (host->mrq->cmd->flags & MMC_RSP_136) {
694 "fixup: ignore CRC fail with long rsp\n");
696 /* note, we used to fail the transfer
697 * here, but it seems that this is just
698 * the hardware getting it wrong.
700 * cmd->error = -EILSEQ;
701 * host->status = "error: bad command crc";
702 * goto fail_transfer;
707 mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;
710 if (mci_csta & S3C2410_SDICMDSTAT_RSPFIN) {
711 if (host->complete_what == COMPLETION_RSPFIN) {
712 host->status = "ok: command response received";
716 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
717 host->complete_what = COMPLETION_XFERFINISH;
719 mci_cclear |= S3C2410_SDICMDSTAT_RSPFIN;
722 /* errors handled after this point are only relevant
723 when a data transfer is in progress */
726 goto clear_status_bits;
728 /* Check for FIFO failure */
730 if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {
731 dbg(host, dbg_err, "FIFO failure\n");
732 host->mrq->data->error = -EILSEQ;
733 host->status = "error: 2440 fifo failure";
737 if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {
738 dbg(host, dbg_err, "FIFO failure\n");
739 cmd->data->error = -EILSEQ;
740 host->status = "error: fifo failure";
745 if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {
746 dbg(host, dbg_err, "bad data crc (outgoing)\n");
747 cmd->data->error = -EILSEQ;
748 host->status = "error: bad data crc (outgoing)";
752 if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {
753 dbg(host, dbg_err, "bad data crc (incoming)\n");
754 cmd->data->error = -EILSEQ;
755 host->status = "error: bad data crc (incoming)";
759 if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {
760 dbg(host, dbg_err, "data timeout\n");
761 cmd->data->error = -ETIMEDOUT;
762 host->status = "error: data timeout";
766 if (mci_dsta & S3C2410_SDIDSTA_XFERFINISH) {
767 if (host->complete_what == COMPLETION_XFERFINISH) {
768 host->status = "ok: data transfer completed";
772 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
773 host->complete_what = COMPLETION_RSPFIN;
775 mci_dclear |= S3C2410_SDIDSTA_XFERFINISH;
779 writel(mci_cclear, host->base + S3C2410_SDICMDSTAT);
780 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
785 host->pio_active = XFER_NONE;
788 host->complete_what = COMPLETION_FINALIZE;
791 tasklet_schedule(&host->pio_tasklet);
797 "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",
798 mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->status);
800 spin_unlock_irqrestore(&host->complete_lock, iflags);
805 static void s3cmci_dma_done_callback(void *arg)
807 struct s3cmci_host *host = arg;
808 unsigned long iflags;
811 BUG_ON(!host->mrq->data);
813 spin_lock_irqsave(&host->complete_lock, iflags);
815 dbg(host, dbg_dma, "DMA FINISHED\n");
817 host->dma_complete = 1;
818 host->complete_what = COMPLETION_FINALIZE;
820 tasklet_schedule(&host->pio_tasklet);
821 spin_unlock_irqrestore(&host->complete_lock, iflags);
825 static void finalize_request(struct s3cmci_host *host)
827 struct mmc_request *mrq = host->mrq;
828 struct mmc_command *cmd;
829 int debug_as_failure = 0;
831 if (host->complete_what != COMPLETION_FINALIZE)
836 cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
838 if (cmd->data && (cmd->error == 0) &&
839 (cmd->data->error == 0)) {
840 if (s3cmci_host_usedma(host) && (!host->dma_complete)) {
841 dbg(host, dbg_dma, "DMA Missing (%d)!\n",
847 /* Read response from controller. */
848 cmd->resp[0] = readl(host->base + S3C2410_SDIRSP0);
849 cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1);
850 cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2);
851 cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3);
853 writel(host->prescaler, host->base + S3C2410_SDIPRE);
856 debug_as_failure = 1;
858 if (cmd->data && cmd->data->error)
859 debug_as_failure = 1;
861 dbg_dumpcmd(host, cmd, debug_as_failure);
863 /* Cleanup controller */
864 writel(0, host->base + S3C2410_SDICMDARG);
865 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
866 writel(0, host->base + S3C2410_SDICMDCON);
869 if (cmd->data && cmd->error)
870 cmd->data->error = cmd->error;
872 if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
873 host->cmd_is_stop = 1;
874 s3cmci_send_request(host->mmc);
878 /* If we have no data transfer we are finished here */
882 /* Calculate the amout of bytes transfer if there was no error */
883 if (mrq->data->error == 0) {
884 mrq->data->bytes_xfered =
885 (mrq->data->blocks * mrq->data->blksz);
887 mrq->data->bytes_xfered = 0;
890 /* If we had an error while transferring data we flush the
891 * DMA channel and the fifo to clear out any garbage. */
892 if (mrq->data->error != 0) {
893 if (s3cmci_host_usedma(host))
894 dmaengine_terminate_all(host->dma);
897 /* Clear failure register and reset fifo. */
898 writel(S3C2440_SDIFSTA_FIFORESET |
899 S3C2440_SDIFSTA_FIFOFAIL,
900 host->base + S3C2410_SDIFSTA);
905 mci_con = readl(host->base + S3C2410_SDICON);
906 mci_con |= S3C2410_SDICON_FIFORESET;
908 writel(mci_con, host->base + S3C2410_SDICON);
913 host->complete_what = COMPLETION_NONE;
916 s3cmci_check_sdio_irq(host);
917 mmc_request_done(host->mmc, mrq);
920 static void s3cmci_send_command(struct s3cmci_host *host,
921 struct mmc_command *cmd)
925 imsk = S3C2410_SDIIMSK_CRCSTATUS | S3C2410_SDIIMSK_CMDTIMEOUT |
926 S3C2410_SDIIMSK_RESPONSEND | S3C2410_SDIIMSK_CMDSENT |
927 S3C2410_SDIIMSK_RESPONSECRC;
929 enable_imask(host, imsk);
932 host->complete_what = COMPLETION_XFERFINISH_RSPFIN;
933 else if (cmd->flags & MMC_RSP_PRESENT)
934 host->complete_what = COMPLETION_RSPFIN;
936 host->complete_what = COMPLETION_CMDSENT;
938 writel(cmd->arg, host->base + S3C2410_SDICMDARG);
940 ccon = cmd->opcode & S3C2410_SDICMDCON_INDEX;
941 ccon |= S3C2410_SDICMDCON_SENDERHOST | S3C2410_SDICMDCON_CMDSTART;
943 if (cmd->flags & MMC_RSP_PRESENT)
944 ccon |= S3C2410_SDICMDCON_WAITRSP;
946 if (cmd->flags & MMC_RSP_136)
947 ccon |= S3C2410_SDICMDCON_LONGRSP;
949 writel(ccon, host->base + S3C2410_SDICMDCON);
952 static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
954 u32 dcon, imsk, stoptries = 3;
956 if ((data->blksz & 3) != 0) {
957 /* We cannot deal with unaligned blocks with more than
958 * one block being transferred. */
960 if (data->blocks > 1) {
961 pr_warn("%s: can't do non-word sized block transfers (blksz %d)\n",
962 __func__, data->blksz);
967 while (readl(host->base + S3C2410_SDIDSTA) &
968 (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {
971 "mci_setup_data() transfer stillin progress.\n");
973 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
976 if ((stoptries--) == 0) {
977 dbg_dumpregs(host, "DRF");
982 dcon = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;
984 if (s3cmci_host_usedma(host))
985 dcon |= S3C2410_SDIDCON_DMAEN;
987 if (host->bus_width == MMC_BUS_WIDTH_4)
988 dcon |= S3C2410_SDIDCON_WIDEBUS;
990 dcon |= S3C2410_SDIDCON_BLOCKMODE;
992 if (data->flags & MMC_DATA_WRITE) {
993 dcon |= S3C2410_SDIDCON_TXAFTERRESP;
994 dcon |= S3C2410_SDIDCON_XFER_TXSTART;
997 if (data->flags & MMC_DATA_READ) {
998 dcon |= S3C2410_SDIDCON_RXAFTERCMD;
999 dcon |= S3C2410_SDIDCON_XFER_RXSTART;
1003 dcon |= S3C2440_SDIDCON_DS_WORD;
1004 dcon |= S3C2440_SDIDCON_DATSTART;
1007 writel(dcon, host->base + S3C2410_SDIDCON);
1009 /* write BSIZE register */
1011 writel(data->blksz, host->base + S3C2410_SDIBSIZE);
1013 /* add to IMASK register */
1014 imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |
1015 S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;
1017 enable_imask(host, imsk);
1019 /* write TIMER register */
1022 writel(0x007FFFFF, host->base + S3C2410_SDITIMER);
1024 writel(0x0000FFFF, host->base + S3C2410_SDITIMER);
1026 /* FIX: set slow clock to prevent timeouts on read */
1027 if (data->flags & MMC_DATA_READ)
1028 writel(0xFF, host->base + S3C2410_SDIPRE);
1034 #define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)
1036 static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
1038 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
1040 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
1042 host->pio_sgptr = 0;
1043 host->pio_bytes = 0;
1044 host->pio_count = 0;
1045 host->pio_active = rw ? XFER_WRITE : XFER_READ;
1049 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
1051 enable_imask(host, S3C2410_SDIIMSK_RXFIFOHALF
1052 | S3C2410_SDIIMSK_RXFIFOLAST);
1058 static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
1060 int rw = data->flags & MMC_DATA_WRITE;
1061 struct dma_async_tx_descriptor *desc;
1062 struct dma_slave_config conf = {
1063 .src_addr = host->mem->start + host->sdidata,
1064 .dst_addr = host->mem->start + host->sdidata,
1065 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
1066 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
1069 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
1071 /* Restore prescaler value */
1072 writel(host->prescaler, host->base + S3C2410_SDIPRE);
1075 conf.direction = DMA_DEV_TO_MEM;
1077 conf.direction = DMA_MEM_TO_DEV;
1079 dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1080 mmc_get_dma_dir(data));
1082 dmaengine_slave_config(host->dma, &conf);
1083 desc = dmaengine_prep_slave_sg(host->dma, data->sg, data->sg_len,
1085 DMA_CTRL_ACK | DMA_PREP_INTERRUPT);
1088 desc->callback = s3cmci_dma_done_callback;
1089 desc->callback_param = host;
1090 dmaengine_submit(desc);
1091 dma_async_issue_pending(host->dma);
1096 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1097 mmc_get_dma_dir(data));
1101 static void s3cmci_send_request(struct mmc_host *mmc)
1103 struct s3cmci_host *host = mmc_priv(mmc);
1104 struct mmc_request *mrq = host->mrq;
1105 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
1108 prepare_dbgmsg(host, cmd, host->cmd_is_stop);
1110 /* Clear command, data and fifo status registers
1111 Fifo clear only necessary on 2440, but doesn't hurt on 2410
1113 writel(0xFFFFFFFF, host->base + S3C2410_SDICMDSTAT);
1114 writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA);
1115 writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA);
1118 int res = s3cmci_setup_data(host, cmd->data);
1123 dbg(host, dbg_err, "setup data error %d\n", res);
1125 cmd->data->error = res;
1127 mmc_request_done(mmc, mrq);
1131 if (s3cmci_host_usedma(host))
1132 res = s3cmci_prepare_dma(host, cmd->data);
1134 res = s3cmci_prepare_pio(host, cmd->data);
1137 dbg(host, dbg_err, "data prepare error %d\n", res);
1139 cmd->data->error = res;
1141 mmc_request_done(mmc, mrq);
1147 s3cmci_send_command(host, cmd);
1149 /* Enable Interrupt */
1150 s3cmci_enable_irq(host, true);
1153 static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1155 struct s3cmci_host *host = mmc_priv(mmc);
1157 host->status = "mmc request";
1158 host->cmd_is_stop = 0;
1161 if (mmc_gpio_get_cd(mmc) == 0) {
1162 dbg(host, dbg_err, "%s: no medium present\n", __func__);
1163 host->mrq->cmd->error = -ENOMEDIUM;
1164 mmc_request_done(mmc, mrq);
1166 s3cmci_send_request(mmc);
1169 static void s3cmci_set_clk(struct s3cmci_host *host, struct mmc_ios *ios)
1174 for (mci_psc = 0; mci_psc < 255; mci_psc++) {
1175 host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));
1177 if (host->real_rate <= ios->clock)
1184 host->prescaler = mci_psc;
1185 writel(host->prescaler, host->base + S3C2410_SDIPRE);
1187 /* If requested clock is 0, real_rate will be 0, too */
1188 if (ios->clock == 0)
1189 host->real_rate = 0;
1192 static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1194 struct s3cmci_host *host = mmc_priv(mmc);
1197 /* Set the power state */
1199 mci_con = readl(host->base + S3C2410_SDICON);
1201 switch (ios->power_mode) {
1204 /* Configure GPE5...GPE10 pins in SD mode */
1205 if (!host->pdev->dev.of_node)
1206 s3c_gpio_cfgall_range(S3C2410_GPE(5), 6, S3C_GPIO_SFN(2),
1207 S3C_GPIO_PULL_NONE);
1209 if (host->pdata->set_power)
1210 host->pdata->set_power(ios->power_mode, ios->vdd);
1213 mci_con |= S3C2410_SDICON_FIFORESET;
1219 if (!host->pdev->dev.of_node)
1220 gpio_direction_output(S3C2410_GPE(5), 0);
1223 mci_con |= S3C2440_SDICON_SDRESET;
1225 if (host->pdata->set_power)
1226 host->pdata->set_power(ios->power_mode, ios->vdd);
1231 s3cmci_set_clk(host, ios);
1233 /* Set CLOCK_ENABLE */
1235 mci_con |= S3C2410_SDICON_CLOCKTYPE;
1237 mci_con &= ~S3C2410_SDICON_CLOCKTYPE;
1239 writel(mci_con, host->base + S3C2410_SDICON);
1241 if ((ios->power_mode == MMC_POWER_ON) ||
1242 (ios->power_mode == MMC_POWER_UP)) {
1243 dbg(host, dbg_conf, "running at %lukHz (requested: %ukHz).\n",
1244 host->real_rate/1000, ios->clock/1000);
1246 dbg(host, dbg_conf, "powered down.\n");
1249 host->bus_width = ios->bus_width;
1252 static void s3cmci_reset(struct s3cmci_host *host)
1254 u32 con = readl(host->base + S3C2410_SDICON);
1256 con |= S3C2440_SDICON_SDRESET;
1257 writel(con, host->base + S3C2410_SDICON);
1260 static void s3cmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1262 struct s3cmci_host *host = mmc_priv(mmc);
1263 unsigned long flags;
1266 local_irq_save(flags);
1268 con = readl(host->base + S3C2410_SDICON);
1269 host->sdio_irqen = enable;
1271 if (enable == host->sdio_irqen)
1275 con |= S3C2410_SDICON_SDIOIRQ;
1276 enable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
1278 if (!host->irq_state && !host->irq_disabled) {
1279 host->irq_state = true;
1280 enable_irq(host->irq);
1283 disable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
1284 con &= ~S3C2410_SDICON_SDIOIRQ;
1286 if (!host->irq_enabled && host->irq_state) {
1287 disable_irq_nosync(host->irq);
1288 host->irq_state = false;
1292 writel(con, host->base + S3C2410_SDICON);
1295 local_irq_restore(flags);
1297 s3cmci_check_sdio_irq(host);
1300 static const struct mmc_host_ops s3cmci_ops = {
1301 .request = s3cmci_request,
1302 .set_ios = s3cmci_set_ios,
1303 .get_ro = mmc_gpio_get_ro,
1304 .get_cd = mmc_gpio_get_cd,
1305 .enable_sdio_irq = s3cmci_enable_sdio_irq,
1308 static struct s3c24xx_mci_pdata s3cmci_def_pdata = {
1309 /* This is currently here to avoid a number of if (host->pdata)
1310 * checks. Any zero fields to ensure reasonable defaults are picked. */
1315 #ifdef CONFIG_ARM_S3C24XX_CPUFREQ
1317 static int s3cmci_cpufreq_transition(struct notifier_block *nb,
1318 unsigned long val, void *data)
1320 struct s3cmci_host *host;
1321 struct mmc_host *mmc;
1322 unsigned long newclk;
1323 unsigned long flags;
1325 host = container_of(nb, struct s3cmci_host, freq_transition);
1326 newclk = clk_get_rate(host->clk);
1329 if ((val == CPUFREQ_PRECHANGE && newclk > host->clk_rate) ||
1330 (val == CPUFREQ_POSTCHANGE && newclk < host->clk_rate)) {
1331 spin_lock_irqsave(&mmc->lock, flags);
1333 host->clk_rate = newclk;
1335 if (mmc->ios.power_mode != MMC_POWER_OFF &&
1336 mmc->ios.clock != 0)
1337 s3cmci_set_clk(host, &mmc->ios);
1339 spin_unlock_irqrestore(&mmc->lock, flags);
1345 static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1347 host->freq_transition.notifier_call = s3cmci_cpufreq_transition;
1349 return cpufreq_register_notifier(&host->freq_transition,
1350 CPUFREQ_TRANSITION_NOTIFIER);
1353 static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1355 cpufreq_unregister_notifier(&host->freq_transition,
1356 CPUFREQ_TRANSITION_NOTIFIER);
1360 static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1365 static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1371 #ifdef CONFIG_DEBUG_FS
1373 static int s3cmci_state_show(struct seq_file *seq, void *v)
1375 struct s3cmci_host *host = seq->private;
1377 seq_printf(seq, "Register base = 0x%p\n", host->base);
1378 seq_printf(seq, "Clock rate = %ld\n", host->clk_rate);
1379 seq_printf(seq, "Prescale = %d\n", host->prescaler);
1380 seq_printf(seq, "is2440 = %d\n", host->is2440);
1381 seq_printf(seq, "IRQ = %d\n", host->irq);
1382 seq_printf(seq, "IRQ enabled = %d\n", host->irq_enabled);
1383 seq_printf(seq, "IRQ disabled = %d\n", host->irq_disabled);
1384 seq_printf(seq, "IRQ state = %d\n", host->irq_state);
1385 seq_printf(seq, "CD IRQ = %d\n", host->irq_cd);
1386 seq_printf(seq, "Do DMA = %d\n", s3cmci_host_usedma(host));
1387 seq_printf(seq, "SDIIMSK at %d\n", host->sdiimsk);
1388 seq_printf(seq, "SDIDATA at %d\n", host->sdidata);
1393 DEFINE_SHOW_ATTRIBUTE(s3cmci_state);
1395 #define DBG_REG(_r) { .addr = S3C2410_SDI##_r, .name = #_r }
1398 unsigned short addr;
1399 unsigned char *name;
1402 static const struct s3cmci_reg debug_regs[] = {
1421 static int s3cmci_regs_show(struct seq_file *seq, void *v)
1423 struct s3cmci_host *host = seq->private;
1424 const struct s3cmci_reg *rptr = debug_regs;
1426 for (; rptr->name; rptr++)
1427 seq_printf(seq, "SDI%s\t=0x%08x\n", rptr->name,
1428 readl(host->base + rptr->addr));
1430 seq_printf(seq, "SDIIMSK\t=0x%08x\n", readl(host->base + host->sdiimsk));
1435 DEFINE_SHOW_ATTRIBUTE(s3cmci_regs);
1437 static void s3cmci_debugfs_attach(struct s3cmci_host *host)
1439 struct device *dev = &host->pdev->dev;
1440 struct dentry *root;
1442 root = debugfs_create_dir(dev_name(dev), NULL);
1443 host->debug_root = root;
1445 debugfs_create_file("state", 0444, root, host, &s3cmci_state_fops);
1446 debugfs_create_file("regs", 0444, root, host, &s3cmci_regs_fops);
1449 static void s3cmci_debugfs_remove(struct s3cmci_host *host)
1451 debugfs_remove_recursive(host->debug_root);
1455 static inline void s3cmci_debugfs_attach(struct s3cmci_host *host) { }
1456 static inline void s3cmci_debugfs_remove(struct s3cmci_host *host) { }
1458 #endif /* CONFIG_DEBUG_FS */
1460 static int s3cmci_probe_pdata(struct s3cmci_host *host)
1462 struct platform_device *pdev = host->pdev;
1463 struct mmc_host *mmc = host->mmc;
1464 struct s3c24xx_mci_pdata *pdata;
1467 host->is2440 = platform_get_device_id(pdev)->driver_data;
1469 for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++) {
1470 ret = gpio_request(i, dev_name(&pdev->dev));
1472 dev_err(&pdev->dev, "failed to get gpio %d\n", i);
1474 for (i--; i >= S3C2410_GPE(5); i--)
1481 if (!pdev->dev.platform_data)
1482 pdev->dev.platform_data = &s3cmci_def_pdata;
1484 pdata = pdev->dev.platform_data;
1486 if (pdata->no_wprotect)
1487 mmc->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
1489 if (pdata->no_detect)
1490 mmc->caps |= MMC_CAP_NEEDS_POLL;
1492 if (pdata->wprotect_invert)
1493 mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
1495 /* If we get -ENOENT we have no card detect GPIO line */
1496 ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0);
1497 if (ret != -ENOENT) {
1498 dev_err(&pdev->dev, "error requesting GPIO for CD %d\n",
1503 ret = mmc_gpiod_request_ro(host->mmc, "wp", 0, 0);
1504 if (ret != -ENOENT) {
1505 dev_err(&pdev->dev, "error requesting GPIO for WP %d\n",
1513 static int s3cmci_probe_dt(struct s3cmci_host *host)
1515 struct platform_device *pdev = host->pdev;
1516 struct s3c24xx_mci_pdata *pdata;
1517 struct mmc_host *mmc = host->mmc;
1520 host->is2440 = (long) of_device_get_match_data(&pdev->dev);
1522 ret = mmc_of_parse(mmc);
1526 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1530 pdev->dev.platform_data = pdata;
1535 static int s3cmci_probe(struct platform_device *pdev)
1537 struct s3cmci_host *host;
1538 struct mmc_host *mmc;
1542 mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
1548 host = mmc_priv(mmc);
1552 if (pdev->dev.of_node)
1553 ret = s3cmci_probe_dt(host);
1555 ret = s3cmci_probe_pdata(host);
1558 goto probe_free_host;
1560 host->pdata = pdev->dev.platform_data;
1562 spin_lock_init(&host->complete_lock);
1563 tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);
1566 host->sdiimsk = S3C2440_SDIIMSK;
1567 host->sdidata = S3C2440_SDIDATA;
1570 host->sdiimsk = S3C2410_SDIIMSK;
1571 host->sdidata = S3C2410_SDIDATA;
1575 host->complete_what = COMPLETION_NONE;
1576 host->pio_active = XFER_NONE;
1578 host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1581 "failed to get io memory region resource.\n");
1584 goto probe_free_gpio;
1587 host->mem = request_mem_region(host->mem->start,
1588 resource_size(host->mem), pdev->name);
1591 dev_err(&pdev->dev, "failed to request io memory region.\n");
1593 goto probe_free_gpio;
1596 host->base = ioremap(host->mem->start, resource_size(host->mem));
1598 dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
1600 goto probe_free_mem_region;
1603 host->irq = platform_get_irq(pdev, 0);
1604 if (host->irq <= 0) {
1609 if (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)) {
1610 dev_err(&pdev->dev, "failed to request mci interrupt.\n");
1615 /* We get spurious interrupts even when we have set the IMSK
1616 * register to ignore everything, so use disable_irq() to make
1617 * ensure we don't lock the system with un-serviceable requests. */
1619 disable_irq(host->irq);
1620 host->irq_state = false;
1622 /* Depending on the dma state, get a DMA channel to use. */
1624 if (s3cmci_host_usedma(host)) {
1625 host->dma = dma_request_chan(&pdev->dev, "rx-tx");
1626 ret = PTR_ERR_OR_ZERO(host->dma);
1628 dev_err(&pdev->dev, "cannot get DMA channel.\n");
1629 goto probe_free_irq;
1633 host->clk = clk_get(&pdev->dev, "sdi");
1634 if (IS_ERR(host->clk)) {
1635 dev_err(&pdev->dev, "failed to find clock source.\n");
1636 ret = PTR_ERR(host->clk);
1638 goto probe_free_dma;
1641 ret = clk_prepare_enable(host->clk);
1643 dev_err(&pdev->dev, "failed to enable clock source.\n");
1647 host->clk_rate = clk_get_rate(host->clk);
1649 mmc->ops = &s3cmci_ops;
1650 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1651 #ifdef CONFIG_MMC_S3C_HW_SDIO_IRQ
1652 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
1654 mmc->caps = MMC_CAP_4_BIT_DATA;
1656 mmc->f_min = host->clk_rate / (host->clk_div * 256);
1657 mmc->f_max = host->clk_rate / host->clk_div;
1659 if (host->pdata->ocr_avail)
1660 mmc->ocr_avail = host->pdata->ocr_avail;
1662 mmc->max_blk_count = 4095;
1663 mmc->max_blk_size = 4095;
1664 mmc->max_req_size = 4095 * 512;
1665 mmc->max_seg_size = mmc->max_req_size;
1667 mmc->max_segs = 128;
1669 dbg(host, dbg_debug,
1670 "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%p.\n",
1671 (host->is2440?"2440":""),
1672 host->base, host->irq, host->irq_cd, host->dma);
1674 ret = s3cmci_cpufreq_register(host);
1676 dev_err(&pdev->dev, "failed to register cpufreq\n");
1680 ret = mmc_add_host(mmc);
1682 dev_err(&pdev->dev, "failed to add mmc host.\n");
1686 s3cmci_debugfs_attach(host);
1688 platform_set_drvdata(pdev, mmc);
1689 dev_info(&pdev->dev, "%s - using %s, %s SDIO IRQ\n", mmc_hostname(mmc),
1690 s3cmci_host_usedma(host) ? "dma" : "pio",
1691 mmc->caps & MMC_CAP_SDIO_IRQ ? "hw" : "sw");
1696 s3cmci_cpufreq_deregister(host);
1699 clk_disable_unprepare(host->clk);
1705 if (s3cmci_host_usedma(host))
1706 dma_release_channel(host->dma);
1709 free_irq(host->irq, host);
1712 iounmap(host->base);
1714 probe_free_mem_region:
1715 release_mem_region(host->mem->start, resource_size(host->mem));
1718 if (!pdev->dev.of_node)
1719 for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++)
1729 static void s3cmci_shutdown(struct platform_device *pdev)
1731 struct mmc_host *mmc = platform_get_drvdata(pdev);
1732 struct s3cmci_host *host = mmc_priv(mmc);
1734 if (host->irq_cd >= 0)
1735 free_irq(host->irq_cd, host);
1737 s3cmci_debugfs_remove(host);
1738 s3cmci_cpufreq_deregister(host);
1739 mmc_remove_host(mmc);
1740 clk_disable_unprepare(host->clk);
1743 static int s3cmci_remove(struct platform_device *pdev)
1745 struct mmc_host *mmc = platform_get_drvdata(pdev);
1746 struct s3cmci_host *host = mmc_priv(mmc);
1749 s3cmci_shutdown(pdev);
1753 tasklet_disable(&host->pio_tasklet);
1755 if (s3cmci_host_usedma(host))
1756 dma_release_channel(host->dma);
1758 free_irq(host->irq, host);
1760 if (!pdev->dev.of_node)
1761 for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++)
1764 iounmap(host->base);
1765 release_mem_region(host->mem->start, resource_size(host->mem));
1771 static const struct of_device_id s3cmci_dt_match[] = {
1773 .compatible = "samsung,s3c2410-sdi",
1777 .compatible = "samsung,s3c2412-sdi",
1781 .compatible = "samsung,s3c2440-sdi",
1786 MODULE_DEVICE_TABLE(of, s3cmci_dt_match);
1788 static const struct platform_device_id s3cmci_driver_ids[] = {
1790 .name = "s3c2410-sdi",
1793 .name = "s3c2412-sdi",
1796 .name = "s3c2440-sdi",
1802 MODULE_DEVICE_TABLE(platform, s3cmci_driver_ids);
1804 static struct platform_driver s3cmci_driver = {
1807 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1808 .of_match_table = s3cmci_dt_match,
1810 .id_table = s3cmci_driver_ids,
1811 .probe = s3cmci_probe,
1812 .remove = s3cmci_remove,
1813 .shutdown = s3cmci_shutdown,
1816 module_platform_driver(s3cmci_driver);
1818 MODULE_DESCRIPTION("Samsung S3C MMC/SD Card Interface driver");
1819 MODULE_LICENSE("GPL v2");
1820 MODULE_AUTHOR("Thomas Kleffel <tk@maintech.de>, Ben Dooks <ben-linux@fluff.org>");