2 * linux/drivers/mmc/s3cmci.h - Samsung S3C MCI driver
4 * Copyright (C) 2004-2006 maintech GmbH, Thomas Kleffel <tk@maintech.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/clk.h>
14 #include <linux/mmc/host.h>
15 #include <linux/platform_device.h>
16 #include <linux/irq.h>
21 #include <mach/regs-sdi.h>
22 #include <mach/regs-gpio.h>
24 #include <asm/plat-s3c24xx/mci.h>
28 #define DRIVER_NAME "s3c-mci"
42 static const int dbgmap_err = dbg_err | dbg_fail;
43 static const int dbgmap_info = dbg_info | dbg_conf;
44 static const int dbgmap_debug = dbg_debug;
46 #define dbg(host, channels, args...) \
48 if (dbgmap_err & channels) \
49 dev_err(&host->pdev->dev, args); \
50 else if (dbgmap_info & channels) \
51 dev_info(&host->pdev->dev, args); \
52 else if (dbgmap_debug & channels) \
53 dev_dbg(&host->pdev->dev, args); \
56 #define RESSIZE(ressource) (((ressource)->end - (ressource)->start)+1)
58 static struct s3c2410_dma_client s3cmci_dma_client = {
62 static void finalize_request(struct s3cmci_host *host);
63 static void s3cmci_send_request(struct mmc_host *mmc);
64 static void s3cmci_reset(struct s3cmci_host *host);
66 #ifdef CONFIG_MMC_DEBUG
68 static void dbg_dumpregs(struct s3cmci_host *host, char *prefix)
70 u32 con, pre, cmdarg, cmdcon, cmdsta, r0, r1, r2, r3, timer, bsize;
71 u32 datcon, datcnt, datsta, fsta, imask;
73 con = readl(host->base + S3C2410_SDICON);
74 pre = readl(host->base + S3C2410_SDIPRE);
75 cmdarg = readl(host->base + S3C2410_SDICMDARG);
76 cmdcon = readl(host->base + S3C2410_SDICMDCON);
77 cmdsta = readl(host->base + S3C2410_SDICMDSTAT);
78 r0 = readl(host->base + S3C2410_SDIRSP0);
79 r1 = readl(host->base + S3C2410_SDIRSP1);
80 r2 = readl(host->base + S3C2410_SDIRSP2);
81 r3 = readl(host->base + S3C2410_SDIRSP3);
82 timer = readl(host->base + S3C2410_SDITIMER);
83 bsize = readl(host->base + S3C2410_SDIBSIZE);
84 datcon = readl(host->base + S3C2410_SDIDCON);
85 datcnt = readl(host->base + S3C2410_SDIDCNT);
86 datsta = readl(host->base + S3C2410_SDIDSTA);
87 fsta = readl(host->base + S3C2410_SDIFSTA);
88 imask = readl(host->base + host->sdiimsk);
90 dbg(host, dbg_debug, "%s CON:[%08x] PRE:[%08x] TMR:[%08x]\n",
91 prefix, con, pre, timer);
93 dbg(host, dbg_debug, "%s CCON:[%08x] CARG:[%08x] CSTA:[%08x]\n",
94 prefix, cmdcon, cmdarg, cmdsta);
96 dbg(host, dbg_debug, "%s DCON:[%08x] FSTA:[%08x]"
97 " DSTA:[%08x] DCNT:[%08x]\n",
98 prefix, datcon, fsta, datsta, datcnt);
100 dbg(host, dbg_debug, "%s R0:[%08x] R1:[%08x]"
101 " R2:[%08x] R3:[%08x]\n",
102 prefix, r0, r1, r2, r3);
105 static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
108 snprintf(host->dbgmsg_cmd, 300,
109 "#%u%s op:%i arg:0x%08x flags:0x08%x retries:%u",
110 host->ccnt, (stop ? " (STOP)" : ""),
111 cmd->opcode, cmd->arg, cmd->flags, cmd->retries);
114 snprintf(host->dbgmsg_dat, 300,
115 "#%u bsize:%u blocks:%u bytes:%u",
116 host->dcnt, cmd->data->blksz,
118 cmd->data->blocks * cmd->data->blksz);
120 host->dbgmsg_dat[0] = '\0';
124 static void dbg_dumpcmd(struct s3cmci_host *host, struct mmc_command *cmd,
127 unsigned int dbglvl = fail ? dbg_fail : dbg_debug;
132 if (cmd->error == 0) {
133 dbg(host, dbglvl, "CMD[OK] %s R0:0x%08x\n",
134 host->dbgmsg_cmd, cmd->resp[0]);
136 dbg(host, dbglvl, "CMD[ERR %i] %s Status:%s\n",
137 cmd->error, host->dbgmsg_cmd, host->status);
143 if (cmd->data->error == 0) {
144 dbg(host, dbglvl, "DAT[OK] %s\n", host->dbgmsg_dat);
146 dbg(host, dbglvl, "DAT[ERR %i] %s DCNT:0x%08x\n",
147 cmd->data->error, host->dbgmsg_dat,
148 readl(host->base + S3C2410_SDIDCNT));
152 static void dbg_dumpcmd(struct s3cmci_host *host,
153 struct mmc_command *cmd, int fail) { }
155 static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
158 static void dbg_dumpregs(struct s3cmci_host *host, char *prefix) { }
160 #endif /* CONFIG_MMC_DEBUG */
162 static inline u32 enable_imask(struct s3cmci_host *host, u32 imask)
166 newmask = readl(host->base + host->sdiimsk);
169 writel(newmask, host->base + host->sdiimsk);
174 static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
178 newmask = readl(host->base + host->sdiimsk);
181 writel(newmask, host->base + host->sdiimsk);
186 static inline void clear_imask(struct s3cmci_host *host)
188 writel(0, host->base + host->sdiimsk);
191 static inline int get_data_buffer(struct s3cmci_host *host,
192 u32 *words, u32 **pointer)
194 struct scatterlist *sg;
196 if (host->pio_active == XFER_NONE)
199 if ((!host->mrq) || (!host->mrq->data))
202 if (host->pio_sgptr >= host->mrq->data->sg_len) {
203 dbg(host, dbg_debug, "no more buffers (%i/%i)\n",
204 host->pio_sgptr, host->mrq->data->sg_len);
207 sg = &host->mrq->data->sg[host->pio_sgptr];
209 *words = sg->length >> 2;
210 *pointer = sg_virt(sg);
214 dbg(host, dbg_sg, "new buffer (%i/%i)\n",
215 host->pio_sgptr, host->mrq->data->sg_len);
220 static inline u32 fifo_count(struct s3cmci_host *host)
222 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
224 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
225 return fifostat >> 2;
228 static inline u32 fifo_free(struct s3cmci_host *host)
230 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
232 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
233 return (63 - fifostat) >> 2;
236 static void do_pio_read(struct s3cmci_host *host)
240 void __iomem *from_ptr;
242 /* write real prescaler to host, it might be set slow to fix */
243 writel(host->prescaler, host->base + S3C2410_SDIPRE);
245 from_ptr = host->base + host->sdidata;
247 while ((fifo = fifo_count(host))) {
248 if (!host->pio_words) {
249 res = get_data_buffer(host, &host->pio_words,
252 host->pio_active = XFER_NONE;
253 host->complete_what = COMPLETION_FINALIZE;
255 dbg(host, dbg_pio, "pio_read(): "
256 "complete (no more data).\n");
261 "pio_read(): new target: [%i]@[%p]\n",
262 host->pio_words, host->pio_ptr);
266 "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
267 fifo, host->pio_words,
268 readl(host->base + S3C2410_SDIDCNT));
270 if (fifo > host->pio_words)
271 fifo = host->pio_words;
273 host->pio_words -= fifo;
274 host->pio_count += fifo;
277 *(host->pio_ptr++) = readl(from_ptr);
280 if (!host->pio_words) {
281 res = get_data_buffer(host, &host->pio_words, &host->pio_ptr);
284 "pio_read(): complete (no more buffers).\n");
285 host->pio_active = XFER_NONE;
286 host->complete_what = COMPLETION_FINALIZE;
293 S3C2410_SDIIMSK_RXFIFOHALF | S3C2410_SDIIMSK_RXFIFOLAST);
296 static void do_pio_write(struct s3cmci_host *host)
298 void __iomem *to_ptr;
302 to_ptr = host->base + host->sdidata;
304 while ((fifo = fifo_free(host))) {
305 if (!host->pio_words) {
306 res = get_data_buffer(host, &host->pio_words,
310 "pio_write(): complete (no more data).\n");
311 host->pio_active = XFER_NONE;
317 "pio_write(): new source: [%i]@[%p]\n",
318 host->pio_words, host->pio_ptr);
322 if (fifo > host->pio_words)
323 fifo = host->pio_words;
325 host->pio_words -= fifo;
326 host->pio_count += fifo;
329 writel(*(host->pio_ptr++), to_ptr);
332 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
335 static void pio_tasklet(unsigned long data)
337 struct s3cmci_host *host = (struct s3cmci_host *) data;
340 disable_irq(host->irq);
342 if (host->pio_active == XFER_WRITE)
345 if (host->pio_active == XFER_READ)
348 if (host->complete_what == COMPLETION_FINALIZE) {
350 if (host->pio_active != XFER_NONE) {
351 dbg(host, dbg_err, "unfinished %s "
352 "- pio_count:[%u] pio_words:[%u]\n",
353 (host->pio_active == XFER_READ) ? "read" : "write",
354 host->pio_count, host->pio_words);
357 host->mrq->data->error = -EINVAL;
360 finalize_request(host);
362 enable_irq(host->irq);
366 * ISR for SDI Interface IRQ
367 * Communication between driver and ISR works as follows:
368 * host->mrq points to current request
369 * host->complete_what Indicates when the request is considered done
370 * COMPLETION_CMDSENT when the command was sent
371 * COMPLETION_RSPFIN when a response was received
372 * COMPLETION_XFERFINISH when the data transfer is finished
373 * COMPLETION_XFERFINISH_RSPFIN both of the above.
374 * host->complete_request is the completion-object the driver waits for
376 * 1) Driver sets up host->mrq and host->complete_what
377 * 2) Driver prepares the transfer
378 * 3) Driver enables interrupts
379 * 4) Driver starts transfer
380 * 5) Driver waits for host->complete_rquest
381 * 6) ISR checks for request status (errors and success)
382 * 6) ISR sets host->mrq->cmd->error and host->mrq->data->error
383 * 7) ISR completes host->complete_request
384 * 8) ISR disables interrupts
385 * 9) Driver wakes up and takes care of the request
387 * Note: "->error"-fields are expected to be set to 0 before the request
388 * was issued by mmc.c - therefore they are only set, when an error
392 static irqreturn_t s3cmci_irq(int irq, void *dev_id)
394 struct s3cmci_host *host = dev_id;
395 struct mmc_command *cmd;
396 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
397 u32 mci_cclear, mci_dclear;
398 unsigned long iflags;
400 spin_lock_irqsave(&host->complete_lock, iflags);
402 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
403 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
404 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
405 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
406 mci_imsk = readl(host->base + host->sdiimsk);
410 if ((host->complete_what == COMPLETION_NONE) ||
411 (host->complete_what == COMPLETION_FINALIZE)) {
412 host->status = "nothing to complete";
418 host->status = "no active mrq";
423 cmd = host->cmd_is_stop ? host->mrq->stop : host->mrq->cmd;
426 host->status = "no active cmd";
432 if ((host->pio_active == XFER_WRITE) &&
433 (mci_fsta & S3C2410_SDIFSTA_TFDET)) {
435 disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
436 tasklet_schedule(&host->pio_tasklet);
437 host->status = "pio tx";
440 if ((host->pio_active == XFER_READ) &&
441 (mci_fsta & S3C2410_SDIFSTA_RFDET)) {
444 S3C2410_SDIIMSK_RXFIFOHALF |
445 S3C2410_SDIIMSK_RXFIFOLAST);
447 tasklet_schedule(&host->pio_tasklet);
448 host->status = "pio rx";
452 if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {
453 dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");
454 cmd->error = -ETIMEDOUT;
455 host->status = "error: command timeout";
459 if (mci_csta & S3C2410_SDICMDSTAT_CMDSENT) {
460 if (host->complete_what == COMPLETION_CMDSENT) {
461 host->status = "ok: command sent";
465 mci_cclear |= S3C2410_SDICMDSTAT_CMDSENT;
468 if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {
469 if (cmd->flags & MMC_RSP_CRC) {
470 if (host->mrq->cmd->flags & MMC_RSP_136) {
472 "fixup: ignore CRC fail with long rsp\n");
474 /* note, we used to fail the transfer
475 * here, but it seems that this is just
476 * the hardware getting it wrong.
478 * cmd->error = -EILSEQ;
479 * host->status = "error: bad command crc";
480 * goto fail_transfer;
485 mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;
488 if (mci_csta & S3C2410_SDICMDSTAT_RSPFIN) {
489 if (host->complete_what == COMPLETION_RSPFIN) {
490 host->status = "ok: command response received";
494 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
495 host->complete_what = COMPLETION_XFERFINISH;
497 mci_cclear |= S3C2410_SDICMDSTAT_RSPFIN;
500 /* errors handled after this point are only relevant
501 when a data transfer is in progress */
504 goto clear_status_bits;
506 /* Check for FIFO failure */
508 if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {
509 dbg(host, dbg_err, "FIFO failure\n");
510 host->mrq->data->error = -EILSEQ;
511 host->status = "error: 2440 fifo failure";
515 if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {
516 dbg(host, dbg_err, "FIFO failure\n");
517 cmd->data->error = -EILSEQ;
518 host->status = "error: fifo failure";
523 if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {
524 dbg(host, dbg_err, "bad data crc (outgoing)\n");
525 cmd->data->error = -EILSEQ;
526 host->status = "error: bad data crc (outgoing)";
530 if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {
531 dbg(host, dbg_err, "bad data crc (incoming)\n");
532 cmd->data->error = -EILSEQ;
533 host->status = "error: bad data crc (incoming)";
537 if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {
538 dbg(host, dbg_err, "data timeout\n");
539 cmd->data->error = -ETIMEDOUT;
540 host->status = "error: data timeout";
544 if (mci_dsta & S3C2410_SDIDSTA_XFERFINISH) {
545 if (host->complete_what == COMPLETION_XFERFINISH) {
546 host->status = "ok: data transfer completed";
550 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
551 host->complete_what = COMPLETION_RSPFIN;
553 mci_dclear |= S3C2410_SDIDSTA_XFERFINISH;
557 writel(mci_cclear, host->base + S3C2410_SDICMDSTAT);
558 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
563 host->pio_active = XFER_NONE;
566 host->complete_what = COMPLETION_FINALIZE;
569 tasklet_schedule(&host->pio_tasklet);
575 "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",
576 mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->status);
578 spin_unlock_irqrestore(&host->complete_lock, iflags);
584 * ISR for the CardDetect Pin
587 static irqreturn_t s3cmci_irq_cd(int irq, void *dev_id)
589 struct s3cmci_host *host = (struct s3cmci_host *)dev_id;
591 dbg(host, dbg_irq, "card detect\n");
593 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
598 static void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch,
599 void *buf_id, int size,
600 enum s3c2410_dma_buffresult result)
602 struct s3cmci_host *host = buf_id;
603 unsigned long iflags;
604 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt;
606 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
607 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
608 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
609 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
612 BUG_ON(!host->mrq->data);
613 BUG_ON(!host->dmatogo);
615 spin_lock_irqsave(&host->complete_lock, iflags);
617 if (result != S3C2410_RES_OK) {
618 dbg(host, dbg_fail, "DMA FAILED: csta=0x%08x dsta=0x%08x "
619 "fsta=0x%08x dcnt:0x%08x result:0x%08x toGo:%u\n",
620 mci_csta, mci_dsta, mci_fsta,
621 mci_dcnt, result, host->dmatogo);
628 dbg(host, dbg_dma, "DMA DONE Size:%i DSTA:[%08x] "
629 "DCNT:[%08x] toGo:%u\n",
630 size, mci_dsta, mci_dcnt, host->dmatogo);
635 dbg(host, dbg_dma, "DMA FINISHED Size:%i DSTA:%08x DCNT:%08x\n",
636 size, mci_dsta, mci_dcnt);
638 host->complete_what = COMPLETION_FINALIZE;
641 tasklet_schedule(&host->pio_tasklet);
642 spin_unlock_irqrestore(&host->complete_lock, iflags);
646 host->mrq->data->error = -EINVAL;
647 host->complete_what = COMPLETION_FINALIZE;
648 writel(0, host->base + host->sdiimsk);
653 static void finalize_request(struct s3cmci_host *host)
655 struct mmc_request *mrq = host->mrq;
656 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
657 int debug_as_failure = 0;
659 if (host->complete_what != COMPLETION_FINALIZE)
665 if (cmd->data && (cmd->error == 0) &&
666 (cmd->data->error == 0)) {
667 if (host->dodma && (!host->dma_complete)) {
668 dbg(host, dbg_dma, "DMA Missing!\n");
673 /* Read response from controller. */
674 cmd->resp[0] = readl(host->base + S3C2410_SDIRSP0);
675 cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1);
676 cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2);
677 cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3);
679 writel(host->prescaler, host->base + S3C2410_SDIPRE);
682 debug_as_failure = 1;
684 if (cmd->data && cmd->data->error)
685 debug_as_failure = 1;
687 dbg_dumpcmd(host, cmd, debug_as_failure);
689 /* Cleanup controller */
690 writel(0, host->base + S3C2410_SDICMDARG);
691 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
692 writel(0, host->base + S3C2410_SDICMDCON);
693 writel(0, host->base + host->sdiimsk);
695 if (cmd->data && cmd->error)
696 cmd->data->error = cmd->error;
698 if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
699 host->cmd_is_stop = 1;
700 s3cmci_send_request(host->mmc);
704 /* If we have no data transfer we are finished here */
708 /* Calulate the amout of bytes transfer if there was no error */
709 if (mrq->data->error == 0) {
710 mrq->data->bytes_xfered =
711 (mrq->data->blocks * mrq->data->blksz);
713 mrq->data->bytes_xfered = 0;
716 /* If we had an error while transfering data we flush the
717 * DMA channel and the fifo to clear out any garbage. */
718 if (mrq->data->error != 0) {
720 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
723 /* Clear failure register and reset fifo. */
724 writel(S3C2440_SDIFSTA_FIFORESET |
725 S3C2440_SDIFSTA_FIFOFAIL,
726 host->base + S3C2410_SDIFSTA);
731 mci_con = readl(host->base + S3C2410_SDICON);
732 mci_con |= S3C2410_SDICON_FIFORESET;
734 writel(mci_con, host->base + S3C2410_SDICON);
739 host->complete_what = COMPLETION_NONE;
741 mmc_request_done(host->mmc, mrq);
744 static void s3cmci_dma_setup(struct s3cmci_host *host,
745 enum s3c2410_dmasrc source)
747 static enum s3c2410_dmasrc last_source = -1;
750 if (last_source == source)
753 last_source = source;
755 s3c2410_dma_devconfig(host->dma, source, 3,
756 host->mem->start + host->sdidata);
759 s3c2410_dma_config(host->dma, 4,
760 (S3C2410_DCON_HWTRIG | S3C2410_DCON_CH0_SDI));
761 s3c2410_dma_set_buffdone_fn(host->dma,
762 s3cmci_dma_done_callback);
763 s3c2410_dma_setflags(host->dma, S3C2410_DMAF_AUTOSTART);
768 static void s3cmci_send_command(struct s3cmci_host *host,
769 struct mmc_command *cmd)
773 imsk = S3C2410_SDIIMSK_CRCSTATUS | S3C2410_SDIIMSK_CMDTIMEOUT |
774 S3C2410_SDIIMSK_RESPONSEND | S3C2410_SDIIMSK_CMDSENT |
775 S3C2410_SDIIMSK_RESPONSECRC;
777 enable_imask(host, imsk);
780 host->complete_what = COMPLETION_XFERFINISH_RSPFIN;
781 else if (cmd->flags & MMC_RSP_PRESENT)
782 host->complete_what = COMPLETION_RSPFIN;
784 host->complete_what = COMPLETION_CMDSENT;
786 writel(cmd->arg, host->base + S3C2410_SDICMDARG);
788 ccon = cmd->opcode & S3C2410_SDICMDCON_INDEX;
789 ccon |= S3C2410_SDICMDCON_SENDERHOST | S3C2410_SDICMDCON_CMDSTART;
791 if (cmd->flags & MMC_RSP_PRESENT)
792 ccon |= S3C2410_SDICMDCON_WAITRSP;
794 if (cmd->flags & MMC_RSP_136)
795 ccon |= S3C2410_SDICMDCON_LONGRSP;
797 writel(ccon, host->base + S3C2410_SDICMDCON);
800 static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
802 u32 dcon, imsk, stoptries = 3;
804 /* write DCON register */
807 writel(0, host->base + S3C2410_SDIDCON);
811 if ((data->blksz & 3) != 0) {
812 /* We cannot deal with unaligned blocks with more than
813 * one block being transfered. */
815 if (data->blocks > 1)
818 /* No support yet for non-word block transfers. */
822 while (readl(host->base + S3C2410_SDIDSTA) &
823 (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {
826 "mci_setup_data() transfer stillin progress.\n");
828 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
831 if ((stoptries--) == 0) {
832 dbg_dumpregs(host, "DRF");
837 dcon = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;
840 dcon |= S3C2410_SDIDCON_DMAEN;
842 if (host->bus_width == MMC_BUS_WIDTH_4)
843 dcon |= S3C2410_SDIDCON_WIDEBUS;
845 if (!(data->flags & MMC_DATA_STREAM))
846 dcon |= S3C2410_SDIDCON_BLOCKMODE;
848 if (data->flags & MMC_DATA_WRITE) {
849 dcon |= S3C2410_SDIDCON_TXAFTERRESP;
850 dcon |= S3C2410_SDIDCON_XFER_TXSTART;
853 if (data->flags & MMC_DATA_READ) {
854 dcon |= S3C2410_SDIDCON_RXAFTERCMD;
855 dcon |= S3C2410_SDIDCON_XFER_RXSTART;
859 dcon |= S3C2440_SDIDCON_DS_WORD;
860 dcon |= S3C2440_SDIDCON_DATSTART;
863 writel(dcon, host->base + S3C2410_SDIDCON);
865 /* write BSIZE register */
867 writel(data->blksz, host->base + S3C2410_SDIBSIZE);
869 /* add to IMASK register */
870 imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |
871 S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;
873 enable_imask(host, imsk);
875 /* write TIMER register */
878 writel(0x007FFFFF, host->base + S3C2410_SDITIMER);
880 writel(0x0000FFFF, host->base + S3C2410_SDITIMER);
882 /* FIX: set slow clock to prevent timeouts on read */
883 if (data->flags & MMC_DATA_READ)
884 writel(0xFF, host->base + S3C2410_SDIPRE);
890 #define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)
892 static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
894 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
896 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
901 host->pio_active = rw ? XFER_WRITE : XFER_READ;
905 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
907 enable_imask(host, S3C2410_SDIIMSK_RXFIFOHALF
908 | S3C2410_SDIIMSK_RXFIFOLAST);
914 static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
917 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
919 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
921 s3cmci_dma_setup(host, rw ? S3C2410_DMASRC_MEM : S3C2410_DMASRC_HW);
922 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
924 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
925 (rw) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
930 host->dma_complete = 0;
931 host->dmatogo = dma_len;
933 for (i = 0; i < dma_len; i++) {
936 dbg(host, dbg_dma, "enqueue %i:%u@%u\n", i,
937 sg_dma_address(&data->sg[i]),
938 sg_dma_len(&data->sg[i]));
940 res = s3c2410_dma_enqueue(host->dma, (void *) host,
941 sg_dma_address(&data->sg[i]),
942 sg_dma_len(&data->sg[i]));
945 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
950 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_START);
955 static void s3cmci_send_request(struct mmc_host *mmc)
957 struct s3cmci_host *host = mmc_priv(mmc);
958 struct mmc_request *mrq = host->mrq;
959 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
962 prepare_dbgmsg(host, cmd, host->cmd_is_stop);
964 /* Clear command, data and fifo status registers
965 Fifo clear only necessary on 2440, but doesn't hurt on 2410
967 writel(0xFFFFFFFF, host->base + S3C2410_SDICMDSTAT);
968 writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA);
969 writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA);
972 int res = s3cmci_setup_data(host, cmd->data);
977 dbg(host, dbg_err, "setup data error %d\n", res);
979 cmd->data->error = res;
981 mmc_request_done(mmc, mrq);
986 res = s3cmci_prepare_dma(host, cmd->data);
988 res = s3cmci_prepare_pio(host, cmd->data);
991 dbg(host, dbg_err, "data prepare error %d\n", res);
993 cmd->data->error = res;
995 mmc_request_done(mmc, mrq);
1001 s3cmci_send_command(host, cmd);
1003 /* Enable Interrupt */
1004 enable_irq(host->irq);
1007 static int s3cmci_card_present(struct mmc_host *mmc)
1009 struct s3cmci_host *host = mmc_priv(mmc);
1010 struct s3c24xx_mci_pdata *pdata = host->pdata;
1013 if (pdata->gpio_detect == 0)
1016 ret = s3c2410_gpio_getpin(pdata->gpio_detect) ? 0 : 1;
1017 return ret ^ pdata->detect_invert;
1020 static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1022 struct s3cmci_host *host = mmc_priv(mmc);
1024 host->status = "mmc request";
1025 host->cmd_is_stop = 0;
1028 if (s3cmci_card_present(mmc) == 0) {
1029 dbg(host, dbg_err, "%s: no medium present\n", __func__);
1030 host->mrq->cmd->error = -ENOMEDIUM;
1031 mmc_request_done(mmc, mrq);
1033 s3cmci_send_request(mmc);
1036 static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1038 struct s3cmci_host *host = mmc_priv(mmc);
1039 u32 mci_psc, mci_con;
1041 /* Set the power state */
1043 mci_con = readl(host->base + S3C2410_SDICON);
1045 switch (ios->power_mode) {
1048 s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPE5_SDCLK);
1049 s3c2410_gpio_cfgpin(S3C2410_GPE6, S3C2410_GPE6_SDCMD);
1050 s3c2410_gpio_cfgpin(S3C2410_GPE7, S3C2410_GPE7_SDDAT0);
1051 s3c2410_gpio_cfgpin(S3C2410_GPE8, S3C2410_GPE8_SDDAT1);
1052 s3c2410_gpio_cfgpin(S3C2410_GPE9, S3C2410_GPE9_SDDAT2);
1053 s3c2410_gpio_cfgpin(S3C2410_GPE10, S3C2410_GPE10_SDDAT3);
1055 if (host->pdata->set_power)
1056 host->pdata->set_power(ios->power_mode, ios->vdd);
1059 mci_con |= S3C2410_SDICON_FIFORESET;
1065 s3c2410_gpio_setpin(S3C2410_GPE5, 0);
1066 s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPE5_OUTP);
1069 mci_con |= S3C2440_SDICON_SDRESET;
1071 if (host->pdata->set_power)
1072 host->pdata->set_power(ios->power_mode, ios->vdd);
1078 for (mci_psc = 0; mci_psc < 255; mci_psc++) {
1079 host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));
1081 if (host->real_rate <= ios->clock)
1088 host->prescaler = mci_psc;
1089 writel(host->prescaler, host->base + S3C2410_SDIPRE);
1091 /* If requested clock is 0, real_rate will be 0, too */
1092 if (ios->clock == 0)
1093 host->real_rate = 0;
1095 /* Set CLOCK_ENABLE */
1097 mci_con |= S3C2410_SDICON_CLOCKTYPE;
1099 mci_con &= ~S3C2410_SDICON_CLOCKTYPE;
1101 writel(mci_con, host->base + S3C2410_SDICON);
1103 if ((ios->power_mode == MMC_POWER_ON) ||
1104 (ios->power_mode == MMC_POWER_UP)) {
1105 dbg(host, dbg_conf, "running at %lukHz (requested: %ukHz).\n",
1106 host->real_rate/1000, ios->clock/1000);
1108 dbg(host, dbg_conf, "powered down.\n");
1111 host->bus_width = ios->bus_width;
1114 static void s3cmci_reset(struct s3cmci_host *host)
1116 u32 con = readl(host->base + S3C2410_SDICON);
1118 con |= S3C2440_SDICON_SDRESET;
1119 writel(con, host->base + S3C2410_SDICON);
1122 static int s3cmci_get_ro(struct mmc_host *mmc)
1124 struct s3cmci_host *host = mmc_priv(mmc);
1125 struct s3c24xx_mci_pdata *pdata = host->pdata;
1128 if (pdata->gpio_wprotect == 0)
1131 ret = s3c2410_gpio_getpin(pdata->gpio_wprotect);
1133 if (pdata->wprotect_invert)
1139 static struct mmc_host_ops s3cmci_ops = {
1140 .request = s3cmci_request,
1141 .set_ios = s3cmci_set_ios,
1142 .get_ro = s3cmci_get_ro,
1143 .get_cd = s3cmci_card_present,
1146 static struct s3c24xx_mci_pdata s3cmci_def_pdata = {
1147 /* This is currently here to avoid a number of if (host->pdata)
1148 * checks. Any zero fields to ensure reaonable defaults are picked. */
1151 static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
1153 struct s3cmci_host *host;
1154 struct mmc_host *mmc;
1157 mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
1163 host = mmc_priv(mmc);
1166 host->is2440 = is2440;
1168 host->pdata = pdev->dev.platform_data;
1170 pdev->dev.platform_data = &s3cmci_def_pdata;
1171 host->pdata = &s3cmci_def_pdata;
1174 spin_lock_init(&host->complete_lock);
1175 tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);
1178 host->sdiimsk = S3C2440_SDIIMSK;
1179 host->sdidata = S3C2440_SDIDATA;
1182 host->sdiimsk = S3C2410_SDIIMSK;
1183 host->sdidata = S3C2410_SDIDATA;
1188 host->complete_what = COMPLETION_NONE;
1189 host->pio_active = XFER_NONE;
1191 host->dma = S3CMCI_DMA;
1193 host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1196 "failed to get io memory region resouce.\n");
1199 goto probe_free_host;
1202 host->mem = request_mem_region(host->mem->start,
1203 RESSIZE(host->mem), pdev->name);
1206 dev_err(&pdev->dev, "failed to request io memory region.\n");
1208 goto probe_free_host;
1211 host->base = ioremap(host->mem->start, RESSIZE(host->mem));
1213 dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
1215 goto probe_free_mem_region;
1218 host->irq = platform_get_irq(pdev, 0);
1219 if (host->irq == 0) {
1220 dev_err(&pdev->dev, "failed to get interrupt resouce.\n");
1225 if (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)) {
1226 dev_err(&pdev->dev, "failed to request mci interrupt.\n");
1231 /* We get spurious interrupts even when we have set the IMSK
1232 * register to ignore everything, so use disable_irq() to make
1233 * ensure we don't lock the system with un-serviceable requests. */
1235 disable_irq(host->irq);
1237 host->irq_cd = s3c2410_gpio_getirq(host->pdata->gpio_detect);
1239 if (host->irq_cd >= 0) {
1240 if (request_irq(host->irq_cd, s3cmci_irq_cd,
1241 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1242 DRIVER_NAME, host)) {
1243 dev_err(&pdev->dev, "can't get card detect irq.\n");
1245 goto probe_free_irq;
1248 dev_warn(&pdev->dev, "host detect has no irq available\n");
1249 s3c2410_gpio_cfgpin(host->pdata->gpio_detect,
1250 S3C2410_GPIO_INPUT);
1253 if (host->pdata->gpio_wprotect)
1254 s3c2410_gpio_cfgpin(host->pdata->gpio_wprotect,
1255 S3C2410_GPIO_INPUT);
1257 if (s3c2410_dma_request(S3CMCI_DMA, &s3cmci_dma_client, NULL) < 0) {
1258 dev_err(&pdev->dev, "unable to get DMA channel.\n");
1260 goto probe_free_irq_cd;
1263 host->clk = clk_get(&pdev->dev, "sdi");
1264 if (IS_ERR(host->clk)) {
1265 dev_err(&pdev->dev, "failed to find clock source.\n");
1266 ret = PTR_ERR(host->clk);
1268 goto probe_free_host;
1271 ret = clk_enable(host->clk);
1273 dev_err(&pdev->dev, "failed to enable clock source.\n");
1277 host->clk_rate = clk_get_rate(host->clk);
1279 mmc->ops = &s3cmci_ops;
1280 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1281 mmc->caps = MMC_CAP_4_BIT_DATA;
1282 mmc->f_min = host->clk_rate / (host->clk_div * 256);
1283 mmc->f_max = host->clk_rate / host->clk_div;
1285 if (host->pdata->ocr_avail)
1286 mmc->ocr_avail = host->pdata->ocr_avail;
1288 mmc->max_blk_count = 4095;
1289 mmc->max_blk_size = 4095;
1290 mmc->max_req_size = 4095 * 512;
1291 mmc->max_seg_size = mmc->max_req_size;
1293 mmc->max_phys_segs = 128;
1294 mmc->max_hw_segs = 128;
1296 dbg(host, dbg_debug,
1297 "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%u.\n",
1298 (host->is2440?"2440":""),
1299 host->base, host->irq, host->irq_cd, host->dma);
1301 ret = mmc_add_host(mmc);
1303 dev_err(&pdev->dev, "failed to add mmc host.\n");
1307 platform_set_drvdata(pdev, mmc);
1308 dev_info(&pdev->dev, "initialisation done.\n");
1313 clk_disable(host->clk);
1319 if (host->irq_cd >= 0)
1320 free_irq(host->irq_cd, host);
1323 free_irq(host->irq, host);
1326 iounmap(host->base);
1328 probe_free_mem_region:
1329 release_mem_region(host->mem->start, RESSIZE(host->mem));
1337 static void s3cmci_shutdown(struct platform_device *pdev)
1339 struct mmc_host *mmc = platform_get_drvdata(pdev);
1340 struct s3cmci_host *host = mmc_priv(mmc);
1342 if (host->irq_cd >= 0)
1343 free_irq(host->irq_cd, host);
1345 mmc_remove_host(mmc);
1346 clk_disable(host->clk);
1349 static int __devexit s3cmci_remove(struct platform_device *pdev)
1351 struct mmc_host *mmc = platform_get_drvdata(pdev);
1352 struct s3cmci_host *host = mmc_priv(mmc);
1354 s3cmci_shutdown(pdev);
1358 tasklet_disable(&host->pio_tasklet);
1359 s3c2410_dma_free(S3CMCI_DMA, &s3cmci_dma_client);
1361 free_irq(host->irq, host);
1363 iounmap(host->base);
1364 release_mem_region(host->mem->start, RESSIZE(host->mem));
1370 static int __devinit s3cmci_2410_probe(struct platform_device *dev)
1372 return s3cmci_probe(dev, 0);
1375 static int __devinit s3cmci_2412_probe(struct platform_device *dev)
1377 return s3cmci_probe(dev, 1);
1380 static int __devinit s3cmci_2440_probe(struct platform_device *dev)
1382 return s3cmci_probe(dev, 1);
1387 static int s3cmci_suspend(struct platform_device *dev, pm_message_t state)
1389 struct mmc_host *mmc = platform_get_drvdata(dev);
1391 return mmc_suspend_host(mmc, state);
1394 static int s3cmci_resume(struct platform_device *dev)
1396 struct mmc_host *mmc = platform_get_drvdata(dev);
1398 return mmc_resume_host(mmc);
1401 #else /* CONFIG_PM */
1402 #define s3cmci_suspend NULL
1403 #define s3cmci_resume NULL
1404 #endif /* CONFIG_PM */
1407 static struct platform_driver s3cmci_2410_driver = {
1408 .driver.name = "s3c2410-sdi",
1409 .driver.owner = THIS_MODULE,
1410 .probe = s3cmci_2410_probe,
1411 .remove = __devexit_p(s3cmci_remove),
1412 .shutdown = s3cmci_shutdown,
1413 .suspend = s3cmci_suspend,
1414 .resume = s3cmci_resume,
1417 static struct platform_driver s3cmci_2412_driver = {
1418 .driver.name = "s3c2412-sdi",
1419 .driver.owner = THIS_MODULE,
1420 .probe = s3cmci_2412_probe,
1421 .remove = __devexit_p(s3cmci_remove),
1422 .shutdown = s3cmci_shutdown,
1423 .suspend = s3cmci_suspend,
1424 .resume = s3cmci_resume,
1427 static struct platform_driver s3cmci_2440_driver = {
1428 .driver.name = "s3c2440-sdi",
1429 .driver.owner = THIS_MODULE,
1430 .probe = s3cmci_2440_probe,
1431 .remove = __devexit_p(s3cmci_remove),
1432 .shutdown = s3cmci_shutdown,
1433 .suspend = s3cmci_suspend,
1434 .resume = s3cmci_resume,
1438 static int __init s3cmci_init(void)
1440 platform_driver_register(&s3cmci_2410_driver);
1441 platform_driver_register(&s3cmci_2412_driver);
1442 platform_driver_register(&s3cmci_2440_driver);
1446 static void __exit s3cmci_exit(void)
1448 platform_driver_unregister(&s3cmci_2410_driver);
1449 platform_driver_unregister(&s3cmci_2412_driver);
1450 platform_driver_unregister(&s3cmci_2440_driver);
1453 module_init(s3cmci_init);
1454 module_exit(s3cmci_exit);
1456 MODULE_DESCRIPTION("Samsung S3C MMC/SD Card Interface driver");
1457 MODULE_LICENSE("GPL v2");
1458 MODULE_AUTHOR("Thomas Kleffel <tk@maintech.de>");
1459 MODULE_ALIAS("platform:s3c2410-sdi");
1460 MODULE_ALIAS("platform:s3c2412-sdi");
1461 MODULE_ALIAS("platform:s3c2440-sdi");