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>
27 #include <linux/platform_data/mmc-s3cmci.h>
31 #define DRIVER_NAME "s3c-mci"
33 #define S3C2410_SDICON (0x00)
34 #define S3C2410_SDIPRE (0x04)
35 #define S3C2410_SDICMDARG (0x08)
36 #define S3C2410_SDICMDCON (0x0C)
37 #define S3C2410_SDICMDSTAT (0x10)
38 #define S3C2410_SDIRSP0 (0x14)
39 #define S3C2410_SDIRSP1 (0x18)
40 #define S3C2410_SDIRSP2 (0x1C)
41 #define S3C2410_SDIRSP3 (0x20)
42 #define S3C2410_SDITIMER (0x24)
43 #define S3C2410_SDIBSIZE (0x28)
44 #define S3C2410_SDIDCON (0x2C)
45 #define S3C2410_SDIDCNT (0x30)
46 #define S3C2410_SDIDSTA (0x34)
47 #define S3C2410_SDIFSTA (0x38)
49 #define S3C2410_SDIDATA (0x3C)
50 #define S3C2410_SDIIMSK (0x40)
52 #define S3C2440_SDIDATA (0x40)
53 #define S3C2440_SDIIMSK (0x3C)
55 #define S3C2440_SDICON_SDRESET (1 << 8)
56 #define S3C2410_SDICON_SDIOIRQ (1 << 3)
57 #define S3C2410_SDICON_FIFORESET (1 << 1)
58 #define S3C2410_SDICON_CLOCKTYPE (1 << 0)
60 #define S3C2410_SDICMDCON_LONGRSP (1 << 10)
61 #define S3C2410_SDICMDCON_WAITRSP (1 << 9)
62 #define S3C2410_SDICMDCON_CMDSTART (1 << 8)
63 #define S3C2410_SDICMDCON_SENDERHOST (1 << 6)
64 #define S3C2410_SDICMDCON_INDEX (0x3f)
66 #define S3C2410_SDICMDSTAT_CRCFAIL (1 << 12)
67 #define S3C2410_SDICMDSTAT_CMDSENT (1 << 11)
68 #define S3C2410_SDICMDSTAT_CMDTIMEOUT (1 << 10)
69 #define S3C2410_SDICMDSTAT_RSPFIN (1 << 9)
71 #define S3C2440_SDIDCON_DS_WORD (2 << 22)
72 #define S3C2410_SDIDCON_TXAFTERRESP (1 << 20)
73 #define S3C2410_SDIDCON_RXAFTERCMD (1 << 19)
74 #define S3C2410_SDIDCON_BLOCKMODE (1 << 17)
75 #define S3C2410_SDIDCON_WIDEBUS (1 << 16)
76 #define S3C2410_SDIDCON_DMAEN (1 << 15)
77 #define S3C2410_SDIDCON_STOP (1 << 14)
78 #define S3C2440_SDIDCON_DATSTART (1 << 14)
80 #define S3C2410_SDIDCON_XFER_RXSTART (2 << 12)
81 #define S3C2410_SDIDCON_XFER_TXSTART (3 << 12)
83 #define S3C2410_SDIDCON_BLKNUM_MASK (0xFFF)
85 #define S3C2410_SDIDSTA_SDIOIRQDETECT (1 << 9)
86 #define S3C2410_SDIDSTA_FIFOFAIL (1 << 8)
87 #define S3C2410_SDIDSTA_CRCFAIL (1 << 7)
88 #define S3C2410_SDIDSTA_RXCRCFAIL (1 << 6)
89 #define S3C2410_SDIDSTA_DATATIMEOUT (1 << 5)
90 #define S3C2410_SDIDSTA_XFERFINISH (1 << 4)
91 #define S3C2410_SDIDSTA_TXDATAON (1 << 1)
92 #define S3C2410_SDIDSTA_RXDATAON (1 << 0)
94 #define S3C2440_SDIFSTA_FIFORESET (1 << 16)
95 #define S3C2440_SDIFSTA_FIFOFAIL (3 << 14)
96 #define S3C2410_SDIFSTA_TFDET (1 << 13)
97 #define S3C2410_SDIFSTA_RFDET (1 << 12)
98 #define S3C2410_SDIFSTA_COUNTMASK (0x7f)
100 #define S3C2410_SDIIMSK_RESPONSECRC (1 << 17)
101 #define S3C2410_SDIIMSK_CMDSENT (1 << 16)
102 #define S3C2410_SDIIMSK_CMDTIMEOUT (1 << 15)
103 #define S3C2410_SDIIMSK_RESPONSEND (1 << 14)
104 #define S3C2410_SDIIMSK_SDIOIRQ (1 << 12)
105 #define S3C2410_SDIIMSK_FIFOFAIL (1 << 11)
106 #define S3C2410_SDIIMSK_CRCSTATUS (1 << 10)
107 #define S3C2410_SDIIMSK_DATACRC (1 << 9)
108 #define S3C2410_SDIIMSK_DATATIMEOUT (1 << 8)
109 #define S3C2410_SDIIMSK_DATAFINISH (1 << 7)
110 #define S3C2410_SDIIMSK_TXFIFOHALF (1 << 4)
111 #define S3C2410_SDIIMSK_RXFIFOLAST (1 << 2)
112 #define S3C2410_SDIIMSK_RXFIFOHALF (1 << 0)
116 dbg_debug = (1 << 1),
126 static const int dbgmap_err = dbg_fail;
127 static const int dbgmap_info = dbg_info | dbg_conf;
128 static const int dbgmap_debug = dbg_err | dbg_debug;
130 #define dbg(host, channels, args...) \
132 if (dbgmap_err & channels) \
133 dev_err(&host->pdev->dev, args); \
134 else if (dbgmap_info & channels) \
135 dev_info(&host->pdev->dev, args); \
136 else if (dbgmap_debug & channels) \
137 dev_dbg(&host->pdev->dev, args); \
140 static void finalize_request(struct s3cmci_host *host);
141 static void s3cmci_send_request(struct mmc_host *mmc);
142 static void s3cmci_reset(struct s3cmci_host *host);
144 #ifdef CONFIG_MMC_DEBUG
146 static void dbg_dumpregs(struct s3cmci_host *host, char *prefix)
148 u32 con, pre, cmdarg, cmdcon, cmdsta, r0, r1, r2, r3, timer, bsize;
149 u32 datcon, datcnt, datsta, fsta, imask;
151 con = readl(host->base + S3C2410_SDICON);
152 pre = readl(host->base + S3C2410_SDIPRE);
153 cmdarg = readl(host->base + S3C2410_SDICMDARG);
154 cmdcon = readl(host->base + S3C2410_SDICMDCON);
155 cmdsta = readl(host->base + S3C2410_SDICMDSTAT);
156 r0 = readl(host->base + S3C2410_SDIRSP0);
157 r1 = readl(host->base + S3C2410_SDIRSP1);
158 r2 = readl(host->base + S3C2410_SDIRSP2);
159 r3 = readl(host->base + S3C2410_SDIRSP3);
160 timer = readl(host->base + S3C2410_SDITIMER);
161 bsize = readl(host->base + S3C2410_SDIBSIZE);
162 datcon = readl(host->base + S3C2410_SDIDCON);
163 datcnt = readl(host->base + S3C2410_SDIDCNT);
164 datsta = readl(host->base + S3C2410_SDIDSTA);
165 fsta = readl(host->base + S3C2410_SDIFSTA);
166 imask = readl(host->base + host->sdiimsk);
168 dbg(host, dbg_debug, "%s CON:[%08x] PRE:[%08x] TMR:[%08x]\n",
169 prefix, con, pre, timer);
171 dbg(host, dbg_debug, "%s CCON:[%08x] CARG:[%08x] CSTA:[%08x]\n",
172 prefix, cmdcon, cmdarg, cmdsta);
174 dbg(host, dbg_debug, "%s DCON:[%08x] FSTA:[%08x]"
175 " DSTA:[%08x] DCNT:[%08x]\n",
176 prefix, datcon, fsta, datsta, datcnt);
178 dbg(host, dbg_debug, "%s R0:[%08x] R1:[%08x]"
179 " R2:[%08x] R3:[%08x]\n",
180 prefix, r0, r1, r2, r3);
183 static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
186 snprintf(host->dbgmsg_cmd, 300,
187 "#%u%s op:%i arg:0x%08x flags:0x08%x retries:%u",
188 host->ccnt, (stop ? " (STOP)" : ""),
189 cmd->opcode, cmd->arg, cmd->flags, cmd->retries);
192 snprintf(host->dbgmsg_dat, 300,
193 "#%u bsize:%u blocks:%u bytes:%u",
194 host->dcnt, cmd->data->blksz,
196 cmd->data->blocks * cmd->data->blksz);
198 host->dbgmsg_dat[0] = '\0';
202 static void dbg_dumpcmd(struct s3cmci_host *host, struct mmc_command *cmd,
205 unsigned int dbglvl = fail ? dbg_fail : dbg_debug;
210 if (cmd->error == 0) {
211 dbg(host, dbglvl, "CMD[OK] %s R0:0x%08x\n",
212 host->dbgmsg_cmd, cmd->resp[0]);
214 dbg(host, dbglvl, "CMD[ERR %i] %s Status:%s\n",
215 cmd->error, host->dbgmsg_cmd, host->status);
221 if (cmd->data->error == 0) {
222 dbg(host, dbglvl, "DAT[OK] %s\n", host->dbgmsg_dat);
224 dbg(host, dbglvl, "DAT[ERR %i] %s DCNT:0x%08x\n",
225 cmd->data->error, host->dbgmsg_dat,
226 readl(host->base + S3C2410_SDIDCNT));
230 static void dbg_dumpcmd(struct s3cmci_host *host,
231 struct mmc_command *cmd, int fail) { }
233 static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
236 static void dbg_dumpregs(struct s3cmci_host *host, char *prefix) { }
238 #endif /* CONFIG_MMC_DEBUG */
241 * s3cmci_host_usedma - return whether the host is using dma or pio
242 * @host: The host state
244 * Return true if the host is using DMA to transfer data, else false
245 * to use PIO mode. Will return static data depending on the driver
248 static inline bool s3cmci_host_usedma(struct s3cmci_host *host)
250 #ifdef CONFIG_MMC_S3C_PIO
252 #else /* CONFIG_MMC_S3C_DMA */
257 static inline u32 enable_imask(struct s3cmci_host *host, u32 imask)
261 newmask = readl(host->base + host->sdiimsk);
264 writel(newmask, host->base + host->sdiimsk);
269 static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
273 newmask = readl(host->base + host->sdiimsk);
276 writel(newmask, host->base + host->sdiimsk);
281 static inline void clear_imask(struct s3cmci_host *host)
283 u32 mask = readl(host->base + host->sdiimsk);
285 /* preserve the SDIO IRQ mask state */
286 mask &= S3C2410_SDIIMSK_SDIOIRQ;
287 writel(mask, host->base + host->sdiimsk);
291 * s3cmci_check_sdio_irq - test whether the SDIO IRQ is being signalled
292 * @host: The host to check.
294 * Test to see if the SDIO interrupt is being signalled in case the
295 * controller has failed to re-detect a card interrupt. Read GPE8 and
296 * see if it is low and if so, signal a SDIO interrupt.
298 * This is currently called if a request is finished (we assume that the
299 * bus is now idle) and when the SDIO IRQ is enabled in case the IRQ is
300 * already being indicated.
302 static void s3cmci_check_sdio_irq(struct s3cmci_host *host)
304 if (host->sdio_irqen) {
305 if (host->pdata->bus[3] &&
306 gpiod_get_value(host->pdata->bus[3]) == 0) {
307 pr_debug("%s: signalling irq\n", __func__);
308 mmc_signal_sdio_irq(host->mmc);
313 static inline int get_data_buffer(struct s3cmci_host *host,
314 u32 *bytes, u32 **pointer)
316 struct scatterlist *sg;
318 if (host->pio_active == XFER_NONE)
321 if ((!host->mrq) || (!host->mrq->data))
324 if (host->pio_sgptr >= host->mrq->data->sg_len) {
325 dbg(host, dbg_debug, "no more buffers (%i/%i)\n",
326 host->pio_sgptr, host->mrq->data->sg_len);
329 sg = &host->mrq->data->sg[host->pio_sgptr];
332 *pointer = sg_virt(sg);
336 dbg(host, dbg_sg, "new buffer (%i/%i)\n",
337 host->pio_sgptr, host->mrq->data->sg_len);
342 static inline u32 fifo_count(struct s3cmci_host *host)
344 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
346 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
350 static inline u32 fifo_free(struct s3cmci_host *host)
352 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
354 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
355 return 63 - fifostat;
359 * s3cmci_enable_irq - enable IRQ, after having disabled it.
360 * @host: The device state.
361 * @more: True if more IRQs are expected from transfer.
363 * Enable the main IRQ if needed after it has been disabled.
365 * The IRQ can be one of the following states:
366 * - disabled during IDLE
367 * - disabled whilst processing data
368 * - enabled during transfer
369 * - enabled whilst awaiting SDIO interrupt detection
371 static void s3cmci_enable_irq(struct s3cmci_host *host, bool more)
376 local_irq_save(flags);
378 host->irq_enabled = more;
379 host->irq_disabled = false;
381 enable = more | host->sdio_irqen;
383 if (host->irq_state != enable) {
384 host->irq_state = enable;
387 enable_irq(host->irq);
389 disable_irq(host->irq);
392 local_irq_restore(flags);
398 static void s3cmci_disable_irq(struct s3cmci_host *host, bool transfer)
402 local_irq_save(flags);
404 /* pr_debug("%s: transfer %d\n", __func__, transfer); */
406 host->irq_disabled = transfer;
408 if (transfer && host->irq_state) {
409 host->irq_state = false;
410 disable_irq(host->irq);
413 local_irq_restore(flags);
416 static void do_pio_read(struct s3cmci_host *host)
422 void __iomem *from_ptr;
424 /* write real prescaler to host, it might be set slow to fix */
425 writel(host->prescaler, host->base + S3C2410_SDIPRE);
427 from_ptr = host->base + host->sdidata;
429 while ((fifo = fifo_count(host))) {
430 if (!host->pio_bytes) {
431 res = get_data_buffer(host, &host->pio_bytes,
434 host->pio_active = XFER_NONE;
435 host->complete_what = COMPLETION_FINALIZE;
437 dbg(host, dbg_pio, "pio_read(): "
438 "complete (no more data).\n");
443 "pio_read(): new target: [%i]@[%p]\n",
444 host->pio_bytes, host->pio_ptr);
448 "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
449 fifo, host->pio_bytes,
450 readl(host->base + S3C2410_SDIDCNT));
452 /* If we have reached the end of the block, we can
453 * read a word and get 1 to 3 bytes. If we in the
454 * middle of the block, we have to read full words,
455 * otherwise we will write garbage, so round down to
456 * an even multiple of 4. */
457 if (fifo >= host->pio_bytes)
458 fifo = host->pio_bytes;
462 host->pio_bytes -= fifo;
463 host->pio_count += fifo;
465 fifo_words = fifo >> 2;
468 *ptr++ = readl(from_ptr);
473 u32 data = readl(from_ptr);
474 u8 *p = (u8 *)host->pio_ptr;
483 if (!host->pio_bytes) {
484 res = get_data_buffer(host, &host->pio_bytes, &host->pio_ptr);
487 "pio_read(): complete (no more buffers).\n");
488 host->pio_active = XFER_NONE;
489 host->complete_what = COMPLETION_FINALIZE;
496 S3C2410_SDIIMSK_RXFIFOHALF | S3C2410_SDIIMSK_RXFIFOLAST);
499 static void do_pio_write(struct s3cmci_host *host)
501 void __iomem *to_ptr;
506 to_ptr = host->base + host->sdidata;
508 while ((fifo = fifo_free(host)) > 3) {
509 if (!host->pio_bytes) {
510 res = get_data_buffer(host, &host->pio_bytes,
514 "pio_write(): complete (no more data).\n");
515 host->pio_active = XFER_NONE;
521 "pio_write(): new source: [%i]@[%p]\n",
522 host->pio_bytes, host->pio_ptr);
526 /* If we have reached the end of the block, we have to
527 * write exactly the remaining number of bytes. If we
528 * in the middle of the block, we have to write full
529 * words, so round down to an even multiple of 4. */
530 if (fifo >= host->pio_bytes)
531 fifo = host->pio_bytes;
535 host->pio_bytes -= fifo;
536 host->pio_count += fifo;
538 fifo = (fifo + 3) >> 2;
541 writel(*ptr++, to_ptr);
545 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
548 static void pio_tasklet(unsigned long data)
550 struct s3cmci_host *host = (struct s3cmci_host *) data;
552 s3cmci_disable_irq(host, true);
554 if (host->pio_active == XFER_WRITE)
557 if (host->pio_active == XFER_READ)
560 if (host->complete_what == COMPLETION_FINALIZE) {
562 if (host->pio_active != XFER_NONE) {
563 dbg(host, dbg_err, "unfinished %s "
564 "- pio_count:[%u] pio_bytes:[%u]\n",
565 (host->pio_active == XFER_READ) ? "read" : "write",
566 host->pio_count, host->pio_bytes);
569 host->mrq->data->error = -EINVAL;
572 s3cmci_enable_irq(host, false);
573 finalize_request(host);
575 s3cmci_enable_irq(host, true);
579 * ISR for SDI Interface IRQ
580 * Communication between driver and ISR works as follows:
581 * host->mrq points to current request
582 * host->complete_what Indicates when the request is considered done
583 * COMPLETION_CMDSENT when the command was sent
584 * COMPLETION_RSPFIN when a response was received
585 * COMPLETION_XFERFINISH when the data transfer is finished
586 * COMPLETION_XFERFINISH_RSPFIN both of the above.
587 * host->complete_request is the completion-object the driver waits for
589 * 1) Driver sets up host->mrq and host->complete_what
590 * 2) Driver prepares the transfer
591 * 3) Driver enables interrupts
592 * 4) Driver starts transfer
593 * 5) Driver waits for host->complete_rquest
594 * 6) ISR checks for request status (errors and success)
595 * 6) ISR sets host->mrq->cmd->error and host->mrq->data->error
596 * 7) ISR completes host->complete_request
597 * 8) ISR disables interrupts
598 * 9) Driver wakes up and takes care of the request
600 * Note: "->error"-fields are expected to be set to 0 before the request
601 * was issued by mmc.c - therefore they are only set, when an error
605 static irqreturn_t s3cmci_irq(int irq, void *dev_id)
607 struct s3cmci_host *host = dev_id;
608 struct mmc_command *cmd;
609 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
610 u32 mci_cclear = 0, mci_dclear;
611 unsigned long iflags;
613 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
614 mci_imsk = readl(host->base + host->sdiimsk);
616 if (mci_dsta & S3C2410_SDIDSTA_SDIOIRQDETECT) {
617 if (mci_imsk & S3C2410_SDIIMSK_SDIOIRQ) {
618 mci_dclear = S3C2410_SDIDSTA_SDIOIRQDETECT;
619 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
621 mmc_signal_sdio_irq(host->mmc);
626 spin_lock_irqsave(&host->complete_lock, iflags);
628 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
629 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
630 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
633 if ((host->complete_what == COMPLETION_NONE) ||
634 (host->complete_what == COMPLETION_FINALIZE)) {
635 host->status = "nothing to complete";
641 host->status = "no active mrq";
646 cmd = host->cmd_is_stop ? host->mrq->stop : host->mrq->cmd;
649 host->status = "no active cmd";
654 if (!s3cmci_host_usedma(host)) {
655 if ((host->pio_active == XFER_WRITE) &&
656 (mci_fsta & S3C2410_SDIFSTA_TFDET)) {
658 disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
659 tasklet_schedule(&host->pio_tasklet);
660 host->status = "pio tx";
663 if ((host->pio_active == XFER_READ) &&
664 (mci_fsta & S3C2410_SDIFSTA_RFDET)) {
667 S3C2410_SDIIMSK_RXFIFOHALF |
668 S3C2410_SDIIMSK_RXFIFOLAST);
670 tasklet_schedule(&host->pio_tasklet);
671 host->status = "pio rx";
675 if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {
676 dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");
677 cmd->error = -ETIMEDOUT;
678 host->status = "error: command timeout";
682 if (mci_csta & S3C2410_SDICMDSTAT_CMDSENT) {
683 if (host->complete_what == COMPLETION_CMDSENT) {
684 host->status = "ok: command sent";
688 mci_cclear |= S3C2410_SDICMDSTAT_CMDSENT;
691 if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {
692 if (cmd->flags & MMC_RSP_CRC) {
693 if (host->mrq->cmd->flags & MMC_RSP_136) {
695 "fixup: ignore CRC fail with long rsp\n");
697 /* note, we used to fail the transfer
698 * here, but it seems that this is just
699 * the hardware getting it wrong.
701 * cmd->error = -EILSEQ;
702 * host->status = "error: bad command crc";
703 * goto fail_transfer;
708 mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;
711 if (mci_csta & S3C2410_SDICMDSTAT_RSPFIN) {
712 if (host->complete_what == COMPLETION_RSPFIN) {
713 host->status = "ok: command response received";
717 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
718 host->complete_what = COMPLETION_XFERFINISH;
720 mci_cclear |= S3C2410_SDICMDSTAT_RSPFIN;
723 /* errors handled after this point are only relevant
724 when a data transfer is in progress */
727 goto clear_status_bits;
729 /* Check for FIFO failure */
731 if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {
732 dbg(host, dbg_err, "FIFO failure\n");
733 host->mrq->data->error = -EILSEQ;
734 host->status = "error: 2440 fifo failure";
738 if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {
739 dbg(host, dbg_err, "FIFO failure\n");
740 cmd->data->error = -EILSEQ;
741 host->status = "error: fifo failure";
746 if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {
747 dbg(host, dbg_err, "bad data crc (outgoing)\n");
748 cmd->data->error = -EILSEQ;
749 host->status = "error: bad data crc (outgoing)";
753 if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {
754 dbg(host, dbg_err, "bad data crc (incoming)\n");
755 cmd->data->error = -EILSEQ;
756 host->status = "error: bad data crc (incoming)";
760 if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {
761 dbg(host, dbg_err, "data timeout\n");
762 cmd->data->error = -ETIMEDOUT;
763 host->status = "error: data timeout";
767 if (mci_dsta & S3C2410_SDIDSTA_XFERFINISH) {
768 if (host->complete_what == COMPLETION_XFERFINISH) {
769 host->status = "ok: data transfer completed";
773 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
774 host->complete_what = COMPLETION_RSPFIN;
776 mci_dclear |= S3C2410_SDIDSTA_XFERFINISH;
780 writel(mci_cclear, host->base + S3C2410_SDICMDSTAT);
781 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
786 host->pio_active = XFER_NONE;
789 host->complete_what = COMPLETION_FINALIZE;
792 tasklet_schedule(&host->pio_tasklet);
798 "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",
799 mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->status);
801 spin_unlock_irqrestore(&host->complete_lock, iflags);
806 static void s3cmci_dma_done_callback(void *arg)
808 struct s3cmci_host *host = arg;
809 unsigned long iflags;
812 BUG_ON(!host->mrq->data);
814 spin_lock_irqsave(&host->complete_lock, iflags);
816 dbg(host, dbg_dma, "DMA FINISHED\n");
818 host->dma_complete = 1;
819 host->complete_what = COMPLETION_FINALIZE;
821 tasklet_schedule(&host->pio_tasklet);
822 spin_unlock_irqrestore(&host->complete_lock, iflags);
826 static void finalize_request(struct s3cmci_host *host)
828 struct mmc_request *mrq = host->mrq;
829 struct mmc_command *cmd;
830 int debug_as_failure = 0;
832 if (host->complete_what != COMPLETION_FINALIZE)
837 cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
839 if (cmd->data && (cmd->error == 0) &&
840 (cmd->data->error == 0)) {
841 if (s3cmci_host_usedma(host) && (!host->dma_complete)) {
842 dbg(host, dbg_dma, "DMA Missing (%d)!\n",
848 /* Read response from controller. */
849 cmd->resp[0] = readl(host->base + S3C2410_SDIRSP0);
850 cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1);
851 cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2);
852 cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3);
854 writel(host->prescaler, host->base + S3C2410_SDIPRE);
857 debug_as_failure = 1;
859 if (cmd->data && cmd->data->error)
860 debug_as_failure = 1;
862 dbg_dumpcmd(host, cmd, debug_as_failure);
864 /* Cleanup controller */
865 writel(0, host->base + S3C2410_SDICMDARG);
866 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
867 writel(0, host->base + S3C2410_SDICMDCON);
870 if (cmd->data && cmd->error)
871 cmd->data->error = cmd->error;
873 if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
874 host->cmd_is_stop = 1;
875 s3cmci_send_request(host->mmc);
879 /* If we have no data transfer we are finished here */
883 /* Calculate the amout of bytes transfer if there was no error */
884 if (mrq->data->error == 0) {
885 mrq->data->bytes_xfered =
886 (mrq->data->blocks * mrq->data->blksz);
888 mrq->data->bytes_xfered = 0;
891 /* If we had an error while transferring data we flush the
892 * DMA channel and the fifo to clear out any garbage. */
893 if (mrq->data->error != 0) {
894 if (s3cmci_host_usedma(host))
895 dmaengine_terminate_all(host->dma);
898 /* Clear failure register and reset fifo. */
899 writel(S3C2440_SDIFSTA_FIFORESET |
900 S3C2440_SDIFSTA_FIFOFAIL,
901 host->base + S3C2410_SDIFSTA);
906 mci_con = readl(host->base + S3C2410_SDICON);
907 mci_con |= S3C2410_SDICON_FIFORESET;
909 writel(mci_con, host->base + S3C2410_SDICON);
914 host->complete_what = COMPLETION_NONE;
917 s3cmci_check_sdio_irq(host);
918 mmc_request_done(host->mmc, mrq);
921 static void s3cmci_send_command(struct s3cmci_host *host,
922 struct mmc_command *cmd)
926 imsk = S3C2410_SDIIMSK_CRCSTATUS | S3C2410_SDIIMSK_CMDTIMEOUT |
927 S3C2410_SDIIMSK_RESPONSEND | S3C2410_SDIIMSK_CMDSENT |
928 S3C2410_SDIIMSK_RESPONSECRC;
930 enable_imask(host, imsk);
933 host->complete_what = COMPLETION_XFERFINISH_RSPFIN;
934 else if (cmd->flags & MMC_RSP_PRESENT)
935 host->complete_what = COMPLETION_RSPFIN;
937 host->complete_what = COMPLETION_CMDSENT;
939 writel(cmd->arg, host->base + S3C2410_SDICMDARG);
941 ccon = cmd->opcode & S3C2410_SDICMDCON_INDEX;
942 ccon |= S3C2410_SDICMDCON_SENDERHOST | S3C2410_SDICMDCON_CMDSTART;
944 if (cmd->flags & MMC_RSP_PRESENT)
945 ccon |= S3C2410_SDICMDCON_WAITRSP;
947 if (cmd->flags & MMC_RSP_136)
948 ccon |= S3C2410_SDICMDCON_LONGRSP;
950 writel(ccon, host->base + S3C2410_SDICMDCON);
953 static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
955 u32 dcon, imsk, stoptries = 3;
957 if ((data->blksz & 3) != 0) {
958 /* We cannot deal with unaligned blocks with more than
959 * one block being transferred. */
961 if (data->blocks > 1) {
962 pr_warn("%s: can't do non-word sized block transfers (blksz %d)\n",
963 __func__, data->blksz);
968 while (readl(host->base + S3C2410_SDIDSTA) &
969 (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {
972 "mci_setup_data() transfer stillin progress.\n");
974 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
977 if ((stoptries--) == 0) {
978 dbg_dumpregs(host, "DRF");
983 dcon = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;
985 if (s3cmci_host_usedma(host))
986 dcon |= S3C2410_SDIDCON_DMAEN;
988 if (host->bus_width == MMC_BUS_WIDTH_4)
989 dcon |= S3C2410_SDIDCON_WIDEBUS;
991 dcon |= S3C2410_SDIDCON_BLOCKMODE;
993 if (data->flags & MMC_DATA_WRITE) {
994 dcon |= S3C2410_SDIDCON_TXAFTERRESP;
995 dcon |= S3C2410_SDIDCON_XFER_TXSTART;
998 if (data->flags & MMC_DATA_READ) {
999 dcon |= S3C2410_SDIDCON_RXAFTERCMD;
1000 dcon |= S3C2410_SDIDCON_XFER_RXSTART;
1004 dcon |= S3C2440_SDIDCON_DS_WORD;
1005 dcon |= S3C2440_SDIDCON_DATSTART;
1008 writel(dcon, host->base + S3C2410_SDIDCON);
1010 /* write BSIZE register */
1012 writel(data->blksz, host->base + S3C2410_SDIBSIZE);
1014 /* add to IMASK register */
1015 imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |
1016 S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;
1018 enable_imask(host, imsk);
1020 /* write TIMER register */
1023 writel(0x007FFFFF, host->base + S3C2410_SDITIMER);
1025 writel(0x0000FFFF, host->base + S3C2410_SDITIMER);
1027 /* FIX: set slow clock to prevent timeouts on read */
1028 if (data->flags & MMC_DATA_READ)
1029 writel(0xFF, host->base + S3C2410_SDIPRE);
1035 #define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)
1037 static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
1039 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
1041 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
1043 host->pio_sgptr = 0;
1044 host->pio_bytes = 0;
1045 host->pio_count = 0;
1046 host->pio_active = rw ? XFER_WRITE : XFER_READ;
1050 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
1052 enable_imask(host, S3C2410_SDIIMSK_RXFIFOHALF
1053 | S3C2410_SDIIMSK_RXFIFOLAST);
1059 static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
1061 int rw = data->flags & MMC_DATA_WRITE;
1062 struct dma_async_tx_descriptor *desc;
1063 struct dma_slave_config conf = {
1064 .src_addr = host->mem->start + host->sdidata,
1065 .dst_addr = host->mem->start + host->sdidata,
1066 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
1067 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
1070 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
1072 /* Restore prescaler value */
1073 writel(host->prescaler, host->base + S3C2410_SDIPRE);
1076 conf.direction = DMA_DEV_TO_MEM;
1078 conf.direction = DMA_MEM_TO_DEV;
1080 dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1081 mmc_get_dma_dir(data));
1083 dmaengine_slave_config(host->dma, &conf);
1084 desc = dmaengine_prep_slave_sg(host->dma, data->sg, data->sg_len,
1086 DMA_CTRL_ACK | DMA_PREP_INTERRUPT);
1089 desc->callback = s3cmci_dma_done_callback;
1090 desc->callback_param = host;
1091 dmaengine_submit(desc);
1092 dma_async_issue_pending(host->dma);
1097 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1098 mmc_get_dma_dir(data));
1102 static void s3cmci_send_request(struct mmc_host *mmc)
1104 struct s3cmci_host *host = mmc_priv(mmc);
1105 struct mmc_request *mrq = host->mrq;
1106 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
1109 prepare_dbgmsg(host, cmd, host->cmd_is_stop);
1111 /* Clear command, data and fifo status registers
1112 Fifo clear only necessary on 2440, but doesn't hurt on 2410
1114 writel(0xFFFFFFFF, host->base + S3C2410_SDICMDSTAT);
1115 writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA);
1116 writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA);
1119 int res = s3cmci_setup_data(host, cmd->data);
1124 dbg(host, dbg_err, "setup data error %d\n", res);
1126 cmd->data->error = res;
1128 mmc_request_done(mmc, mrq);
1132 if (s3cmci_host_usedma(host))
1133 res = s3cmci_prepare_dma(host, cmd->data);
1135 res = s3cmci_prepare_pio(host, cmd->data);
1138 dbg(host, dbg_err, "data prepare error %d\n", res);
1140 cmd->data->error = res;
1142 mmc_request_done(mmc, mrq);
1148 s3cmci_send_command(host, cmd);
1150 /* Enable Interrupt */
1151 s3cmci_enable_irq(host, true);
1154 static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1156 struct s3cmci_host *host = mmc_priv(mmc);
1158 host->status = "mmc request";
1159 host->cmd_is_stop = 0;
1162 if (mmc_gpio_get_cd(mmc) == 0) {
1163 dbg(host, dbg_err, "%s: no medium present\n", __func__);
1164 host->mrq->cmd->error = -ENOMEDIUM;
1165 mmc_request_done(mmc, mrq);
1167 s3cmci_send_request(mmc);
1170 static void s3cmci_set_clk(struct s3cmci_host *host, struct mmc_ios *ios)
1175 for (mci_psc = 0; mci_psc < 255; mci_psc++) {
1176 host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));
1178 if (host->real_rate <= ios->clock)
1185 host->prescaler = mci_psc;
1186 writel(host->prescaler, host->base + S3C2410_SDIPRE);
1188 /* If requested clock is 0, real_rate will be 0, too */
1189 if (ios->clock == 0)
1190 host->real_rate = 0;
1193 static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1195 struct s3cmci_host *host = mmc_priv(mmc);
1198 /* Set the power state */
1200 mci_con = readl(host->base + S3C2410_SDICON);
1202 switch (ios->power_mode) {
1206 mci_con |= S3C2410_SDICON_FIFORESET;
1212 mci_con |= S3C2440_SDICON_SDRESET;
1216 if (host->pdata->set_power)
1217 host->pdata->set_power(ios->power_mode, ios->vdd);
1219 s3cmci_set_clk(host, ios);
1221 /* Set CLOCK_ENABLE */
1223 mci_con |= S3C2410_SDICON_CLOCKTYPE;
1225 mci_con &= ~S3C2410_SDICON_CLOCKTYPE;
1227 writel(mci_con, host->base + S3C2410_SDICON);
1229 if ((ios->power_mode == MMC_POWER_ON) ||
1230 (ios->power_mode == MMC_POWER_UP)) {
1231 dbg(host, dbg_conf, "running at %lukHz (requested: %ukHz).\n",
1232 host->real_rate/1000, ios->clock/1000);
1234 dbg(host, dbg_conf, "powered down.\n");
1237 host->bus_width = ios->bus_width;
1240 static void s3cmci_reset(struct s3cmci_host *host)
1242 u32 con = readl(host->base + S3C2410_SDICON);
1244 con |= S3C2440_SDICON_SDRESET;
1245 writel(con, host->base + S3C2410_SDICON);
1248 static void s3cmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1250 struct s3cmci_host *host = mmc_priv(mmc);
1251 unsigned long flags;
1254 local_irq_save(flags);
1256 con = readl(host->base + S3C2410_SDICON);
1257 host->sdio_irqen = enable;
1259 if (enable == host->sdio_irqen)
1263 con |= S3C2410_SDICON_SDIOIRQ;
1264 enable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
1266 if (!host->irq_state && !host->irq_disabled) {
1267 host->irq_state = true;
1268 enable_irq(host->irq);
1271 disable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
1272 con &= ~S3C2410_SDICON_SDIOIRQ;
1274 if (!host->irq_enabled && host->irq_state) {
1275 disable_irq_nosync(host->irq);
1276 host->irq_state = false;
1280 writel(con, host->base + S3C2410_SDICON);
1283 local_irq_restore(flags);
1285 s3cmci_check_sdio_irq(host);
1288 static const struct mmc_host_ops s3cmci_ops = {
1289 .request = s3cmci_request,
1290 .set_ios = s3cmci_set_ios,
1291 .get_ro = mmc_gpio_get_ro,
1292 .get_cd = mmc_gpio_get_cd,
1293 .enable_sdio_irq = s3cmci_enable_sdio_irq,
1296 #ifdef CONFIG_ARM_S3C24XX_CPUFREQ
1298 static int s3cmci_cpufreq_transition(struct notifier_block *nb,
1299 unsigned long val, void *data)
1301 struct s3cmci_host *host;
1302 struct mmc_host *mmc;
1303 unsigned long newclk;
1304 unsigned long flags;
1306 host = container_of(nb, struct s3cmci_host, freq_transition);
1307 newclk = clk_get_rate(host->clk);
1310 if ((val == CPUFREQ_PRECHANGE && newclk > host->clk_rate) ||
1311 (val == CPUFREQ_POSTCHANGE && newclk < host->clk_rate)) {
1312 spin_lock_irqsave(&mmc->lock, flags);
1314 host->clk_rate = newclk;
1316 if (mmc->ios.power_mode != MMC_POWER_OFF &&
1317 mmc->ios.clock != 0)
1318 s3cmci_set_clk(host, &mmc->ios);
1320 spin_unlock_irqrestore(&mmc->lock, flags);
1326 static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1328 host->freq_transition.notifier_call = s3cmci_cpufreq_transition;
1330 return cpufreq_register_notifier(&host->freq_transition,
1331 CPUFREQ_TRANSITION_NOTIFIER);
1334 static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1336 cpufreq_unregister_notifier(&host->freq_transition,
1337 CPUFREQ_TRANSITION_NOTIFIER);
1341 static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1346 static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1352 #ifdef CONFIG_DEBUG_FS
1354 static int s3cmci_state_show(struct seq_file *seq, void *v)
1356 struct s3cmci_host *host = seq->private;
1358 seq_printf(seq, "Register base = 0x%08x\n", (u32)host->base);
1359 seq_printf(seq, "Clock rate = %ld\n", host->clk_rate);
1360 seq_printf(seq, "Prescale = %d\n", host->prescaler);
1361 seq_printf(seq, "is2440 = %d\n", host->is2440);
1362 seq_printf(seq, "IRQ = %d\n", host->irq);
1363 seq_printf(seq, "IRQ enabled = %d\n", host->irq_enabled);
1364 seq_printf(seq, "IRQ disabled = %d\n", host->irq_disabled);
1365 seq_printf(seq, "IRQ state = %d\n", host->irq_state);
1366 seq_printf(seq, "CD IRQ = %d\n", host->irq_cd);
1367 seq_printf(seq, "Do DMA = %d\n", s3cmci_host_usedma(host));
1368 seq_printf(seq, "SDIIMSK at %d\n", host->sdiimsk);
1369 seq_printf(seq, "SDIDATA at %d\n", host->sdidata);
1374 DEFINE_SHOW_ATTRIBUTE(s3cmci_state);
1376 #define DBG_REG(_r) { .addr = S3C2410_SDI##_r, .name = #_r }
1379 unsigned short addr;
1380 unsigned char *name;
1383 static const struct s3cmci_reg debug_regs[] = {
1402 static int s3cmci_regs_show(struct seq_file *seq, void *v)
1404 struct s3cmci_host *host = seq->private;
1405 const struct s3cmci_reg *rptr = debug_regs;
1407 for (; rptr->name; rptr++)
1408 seq_printf(seq, "SDI%s\t=0x%08x\n", rptr->name,
1409 readl(host->base + rptr->addr));
1411 seq_printf(seq, "SDIIMSK\t=0x%08x\n", readl(host->base + host->sdiimsk));
1416 DEFINE_SHOW_ATTRIBUTE(s3cmci_regs);
1418 static void s3cmci_debugfs_attach(struct s3cmci_host *host)
1420 struct device *dev = &host->pdev->dev;
1421 struct dentry *root;
1423 root = debugfs_create_dir(dev_name(dev), NULL);
1424 host->debug_root = root;
1426 debugfs_create_file("state", 0444, root, host, &s3cmci_state_fops);
1427 debugfs_create_file("regs", 0444, root, host, &s3cmci_regs_fops);
1430 static void s3cmci_debugfs_remove(struct s3cmci_host *host)
1432 debugfs_remove_recursive(host->debug_root);
1436 static inline void s3cmci_debugfs_attach(struct s3cmci_host *host) { }
1437 static inline void s3cmci_debugfs_remove(struct s3cmci_host *host) { }
1439 #endif /* CONFIG_DEBUG_FS */
1441 static int s3cmci_probe_pdata(struct s3cmci_host *host)
1443 struct platform_device *pdev = host->pdev;
1444 struct mmc_host *mmc = host->mmc;
1445 struct s3c24xx_mci_pdata *pdata;
1448 host->is2440 = platform_get_device_id(pdev)->driver_data;
1449 pdata = pdev->dev.platform_data;
1451 dev_err(&pdev->dev, "need platform data");
1455 for (i = 0; i < 6; i++) {
1456 pdata->bus[i] = devm_gpiod_get_index(&pdev->dev, "bus", i,
1458 if (IS_ERR(pdata->bus[i])) {
1459 dev_err(&pdev->dev, "failed to get gpio %d\n", i);
1460 return PTR_ERR(pdata->bus[i]);
1464 if (pdata->no_wprotect)
1465 mmc->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
1467 if (pdata->no_detect)
1468 mmc->caps |= MMC_CAP_NEEDS_POLL;
1470 if (pdata->wprotect_invert)
1471 mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
1473 /* If we get -ENOENT we have no card detect GPIO line */
1474 ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0);
1475 if (ret != -ENOENT) {
1476 dev_err(&pdev->dev, "error requesting GPIO for CD %d\n",
1481 ret = mmc_gpiod_request_ro(host->mmc, "wp", 0, 0);
1482 if (ret != -ENOENT) {
1483 dev_err(&pdev->dev, "error requesting GPIO for WP %d\n",
1491 static int s3cmci_probe_dt(struct s3cmci_host *host)
1493 struct platform_device *pdev = host->pdev;
1494 struct s3c24xx_mci_pdata *pdata;
1495 struct mmc_host *mmc = host->mmc;
1498 host->is2440 = (int) of_device_get_match_data(&pdev->dev);
1500 ret = mmc_of_parse(mmc);
1504 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1508 pdev->dev.platform_data = pdata;
1513 static int s3cmci_probe(struct platform_device *pdev)
1515 struct s3cmci_host *host;
1516 struct mmc_host *mmc;
1519 mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
1525 host = mmc_priv(mmc);
1529 if (pdev->dev.of_node)
1530 ret = s3cmci_probe_dt(host);
1532 ret = s3cmci_probe_pdata(host);
1535 goto probe_free_host;
1537 host->pdata = pdev->dev.platform_data;
1539 spin_lock_init(&host->complete_lock);
1540 tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);
1543 host->sdiimsk = S3C2440_SDIIMSK;
1544 host->sdidata = S3C2440_SDIDATA;
1547 host->sdiimsk = S3C2410_SDIIMSK;
1548 host->sdidata = S3C2410_SDIDATA;
1552 host->complete_what = COMPLETION_NONE;
1553 host->pio_active = XFER_NONE;
1555 host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1558 "failed to get io memory region resource.\n");
1561 goto probe_free_host;
1564 host->mem = request_mem_region(host->mem->start,
1565 resource_size(host->mem), pdev->name);
1568 dev_err(&pdev->dev, "failed to request io memory region.\n");
1570 goto probe_free_host;
1573 host->base = ioremap(host->mem->start, resource_size(host->mem));
1575 dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
1577 goto probe_free_mem_region;
1580 host->irq = platform_get_irq(pdev, 0);
1581 if (host->irq <= 0) {
1586 if (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)) {
1587 dev_err(&pdev->dev, "failed to request mci interrupt.\n");
1592 /* We get spurious interrupts even when we have set the IMSK
1593 * register to ignore everything, so use disable_irq() to make
1594 * ensure we don't lock the system with un-serviceable requests. */
1596 disable_irq(host->irq);
1597 host->irq_state = false;
1599 /* Depending on the dma state, get a DMA channel to use. */
1601 if (s3cmci_host_usedma(host)) {
1602 host->dma = dma_request_chan(&pdev->dev, "rx-tx");
1603 ret = PTR_ERR_OR_ZERO(host->dma);
1605 dev_err(&pdev->dev, "cannot get DMA channel.\n");
1606 goto probe_free_irq;
1610 host->clk = clk_get(&pdev->dev, "sdi");
1611 if (IS_ERR(host->clk)) {
1612 dev_err(&pdev->dev, "failed to find clock source.\n");
1613 ret = PTR_ERR(host->clk);
1615 goto probe_free_dma;
1618 ret = clk_prepare_enable(host->clk);
1620 dev_err(&pdev->dev, "failed to enable clock source.\n");
1624 host->clk_rate = clk_get_rate(host->clk);
1626 mmc->ops = &s3cmci_ops;
1627 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1628 #ifdef CONFIG_MMC_S3C_HW_SDIO_IRQ
1629 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
1631 mmc->caps = MMC_CAP_4_BIT_DATA;
1633 mmc->f_min = host->clk_rate / (host->clk_div * 256);
1634 mmc->f_max = host->clk_rate / host->clk_div;
1636 if (host->pdata->ocr_avail)
1637 mmc->ocr_avail = host->pdata->ocr_avail;
1639 mmc->max_blk_count = 4095;
1640 mmc->max_blk_size = 4095;
1641 mmc->max_req_size = 4095 * 512;
1642 mmc->max_seg_size = mmc->max_req_size;
1644 mmc->max_segs = 128;
1646 dbg(host, dbg_debug,
1647 "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%p.\n",
1648 (host->is2440?"2440":""),
1649 host->base, host->irq, host->irq_cd, host->dma);
1651 ret = s3cmci_cpufreq_register(host);
1653 dev_err(&pdev->dev, "failed to register cpufreq\n");
1657 ret = mmc_add_host(mmc);
1659 dev_err(&pdev->dev, "failed to add mmc host.\n");
1663 s3cmci_debugfs_attach(host);
1665 platform_set_drvdata(pdev, mmc);
1666 dev_info(&pdev->dev, "%s - using %s, %s SDIO IRQ\n", mmc_hostname(mmc),
1667 s3cmci_host_usedma(host) ? "dma" : "pio",
1668 mmc->caps & MMC_CAP_SDIO_IRQ ? "hw" : "sw");
1673 s3cmci_cpufreq_deregister(host);
1676 clk_disable_unprepare(host->clk);
1682 if (s3cmci_host_usedma(host))
1683 dma_release_channel(host->dma);
1686 free_irq(host->irq, host);
1689 iounmap(host->base);
1691 probe_free_mem_region:
1692 release_mem_region(host->mem->start, resource_size(host->mem));
1701 static void s3cmci_shutdown(struct platform_device *pdev)
1703 struct mmc_host *mmc = platform_get_drvdata(pdev);
1704 struct s3cmci_host *host = mmc_priv(mmc);
1706 if (host->irq_cd >= 0)
1707 free_irq(host->irq_cd, host);
1709 s3cmci_debugfs_remove(host);
1710 s3cmci_cpufreq_deregister(host);
1711 mmc_remove_host(mmc);
1712 clk_disable_unprepare(host->clk);
1715 static int s3cmci_remove(struct platform_device *pdev)
1717 struct mmc_host *mmc = platform_get_drvdata(pdev);
1718 struct s3cmci_host *host = mmc_priv(mmc);
1720 s3cmci_shutdown(pdev);
1724 tasklet_disable(&host->pio_tasklet);
1726 if (s3cmci_host_usedma(host))
1727 dma_release_channel(host->dma);
1729 free_irq(host->irq, host);
1731 iounmap(host->base);
1732 release_mem_region(host->mem->start, resource_size(host->mem));
1738 static const struct of_device_id s3cmci_dt_match[] = {
1740 .compatible = "samsung,s3c2410-sdi",
1744 .compatible = "samsung,s3c2412-sdi",
1748 .compatible = "samsung,s3c2440-sdi",
1753 MODULE_DEVICE_TABLE(of, s3cmci_dt_match);
1755 static const struct platform_device_id s3cmci_driver_ids[] = {
1757 .name = "s3c2410-sdi",
1760 .name = "s3c2412-sdi",
1763 .name = "s3c2440-sdi",
1769 MODULE_DEVICE_TABLE(platform, s3cmci_driver_ids);
1771 static struct platform_driver s3cmci_driver = {
1774 .of_match_table = s3cmci_dt_match,
1776 .id_table = s3cmci_driver_ids,
1777 .probe = s3cmci_probe,
1778 .remove = s3cmci_remove,
1779 .shutdown = s3cmci_shutdown,
1782 module_platform_driver(s3cmci_driver);
1784 MODULE_DESCRIPTION("Samsung S3C MMC/SD Card Interface driver");
1785 MODULE_LICENSE("GPL v2");
1786 MODULE_AUTHOR("Thomas Kleffel <tk@maintech.de>, Ben Dooks <ben-linux@fluff.org>");