14 #include <linux/module.h>
34 #define DRIVER_NAME "s3c-mci"
48 static const int dbgmap_err =
dbg_fail;
52 #define dbg(host, channels, args...) \
54 if (dbgmap_err & channels) \
55 dev_err(&host->pdev->dev, args); \
56 else if (dbgmap_info & channels) \
57 dev_info(&host->pdev->dev, args); \
58 else if (dbgmap_debug & channels) \
59 dev_dbg(&host->pdev->dev, args); \
67 static void s3cmci_send_request(
struct mmc_host *mmc);
70 #ifdef CONFIG_MMC_DEBUG
74 u32 con, pre, cmdarg, cmdcon, cmdsta,
r0,
r1,
r2,
r3,
timer, bsize;
75 u32 datcon, datcnt, datsta,
fsta, imask;
94 dbg(host,
dbg_debug,
"%s CON:[%08x] PRE:[%08x] TMR:[%08x]\n",
95 prefix, con, pre, timer);
97 dbg(host,
dbg_debug,
"%s CCON:[%08x] CARG:[%08x] CSTA:[%08x]\n",
98 prefix, cmdcon, cmdarg, cmdsta);
101 " DSTA:[%08x] DCNT:[%08x]\n",
102 prefix, datcon, fsta, datsta, datcnt);
105 " R2:[%08x] R3:[%08x]\n",
106 prefix, r0, r1, r2, r3);
113 "#%u%s op:%i arg:0x%08x flags:0x08%x retries:%u",
114 host->
ccnt, (stop ?
" (STOP)" :
""),
119 "#%u bsize:%u blocks:%u bytes:%u",
122 cmd->
data->blocks * cmd->
data->blksz);
136 if (cmd->
error == 0) {
137 dbg(host, dbglvl,
"CMD[OK] %s R0:0x%08x\n",
140 dbg(host, dbglvl,
"CMD[ERR %i] %s Status:%s\n",
147 if (cmd->
data->error == 0) {
150 dbg(host, dbglvl,
"DAT[ERR %i] %s DCNT:0x%08x\n",
174 static inline bool s3cmci_host_usedma(
struct s3cmci_host *host)
176 #ifdef CONFIG_MMC_S3C_PIO
178 #elif defined(CONFIG_MMC_S3C_DMA)
191 static inline bool s3cmci_host_canpio(
void)
193 #ifdef CONFIG_MMC_S3C_PIO
224 static inline void clear_imask(
struct s3cmci_host *host)
245 static void s3cmci_check_sdio_irq(
struct s3cmci_host *host)
249 pr_debug(
"%s: signalling irq\n", __func__);
250 mmc_signal_sdio_irq(host->
mmc);
255 static inline int get_data_buffer(
struct s3cmci_host *host,
263 if ((!host->
mrq) || (!host->
mrq->data))
274 *pointer = sg_virt(sg);
278 dbg(host,
dbg_sg,
"new buffer (%i/%i)\n",
297 return 63 - fifostat;
313 static void s3cmci_enable_irq(
struct s3cmci_host *host,
bool more)
340 static void s3cmci_disable_irq(
struct s3cmci_host *host,
bool transfer)
371 while ((fifo = fifo_count(host))) {
373 res = get_data_buffer(host, &host->
pio_bytes,
380 "complete (no more data).\n");
385 "pio_read(): new target: [%i]@[%p]\n",
390 "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
407 fifo_words = fifo >> 2;
410 *ptr++ =
readl(from_ptr);
429 "pio_read(): complete (no more buffers).\n");
450 while ((fifo = fifo_free(host)) > 3) {
452 res = get_data_buffer(host, &host->
pio_bytes,
456 "pio_write(): complete (no more data).\n");
463 "pio_write(): new source: [%i]@[%p]\n",
480 fifo = (fifo + 3) >> 2;
490 static void pio_tasklet(
unsigned long data)
494 s3cmci_disable_irq(host,
true);
506 "- pio_count:[%u] pio_bytes:[%u]\n",
514 s3cmci_enable_irq(host,
false);
515 finalize_request(host);
517 s3cmci_enable_irq(host,
true);
551 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
552 u32 mci_cclear = 0, mci_dclear;
553 unsigned long iflags;
563 mmc_signal_sdio_irq(host->
mmc);
577 host->
status =
"nothing to complete";
583 host->
status =
"no active mrq";
591 host->
status =
"no active cmd";
596 if (!s3cmci_host_usedma(host)) {
618 dbg(host,
dbg_err,
"CMDSTAT: error CMDTIMEOUT\n");
620 host->
status =
"error: command timeout";
626 host->
status =
"ok: command sent";
637 "fixup: ignore CRC fail with long rsp\n");
655 host->
status =
"ok: command response received";
669 goto clear_status_bits;
676 host->
status =
"error: 2440 fifo failure";
683 host->
status =
"error: fifo failure";
689 dbg(host,
dbg_err,
"bad data crc (outgoing)\n");
691 host->
status =
"error: bad data crc (outgoing)";
696 dbg(host,
dbg_err,
"bad data crc (incoming)\n");
698 host->
status =
"error: bad data crc (incoming)";
705 host->
status =
"error: data timeout";
711 host->
status =
"ok: data transfer completed";
740 "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",
741 mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->
status);
752 static irqreturn_t s3cmci_irq_cd(
int irq,
void *dev_id)
764 void *buf_id,
int size,
768 unsigned long iflags;
769 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt;
783 dbg(host,
dbg_fail,
"DMA FAILED: csta=0x%08x dsta=0x%08x "
784 "fsta=0x%08x dcnt:0x%08x result:0x%08x toGo:%u\n",
785 mci_csta, mci_dsta, mci_fsta,
786 mci_dcnt, result, host->
dmatogo);
793 dbg(host,
dbg_dma,
"DMA DONE Size:%i DSTA:[%08x] "
794 "DCNT:[%08x] toGo:%u\n",
795 size, mci_dsta, mci_dcnt, host->
dmatogo);
800 dbg(host,
dbg_dma,
"DMA FINISHED Size:%i DSTA:%08x DCNT:%08x\n",
801 size, mci_dsta, mci_dcnt);
819 static void finalize_request(
struct s3cmci_host *host)
823 int debug_as_failure = 0;
833 (cmd->
data->error == 0)) {
834 if (s3cmci_host_usedma(host) && (!host->
dma_complete)) {
850 debug_as_failure = 1;
853 debug_as_failure = 1;
855 dbg_dumpcmd(host, cmd, debug_as_failure);
868 s3cmci_send_request(host->
mmc);
877 if (mrq->
data->error == 0) {
878 mrq->
data->bytes_xfered =
879 (mrq->
data->blocks * mrq->
data->blksz);
881 mrq->
data->bytes_xfered = 0;
886 if (mrq->
data->error != 0) {
887 if (s3cmci_host_usedma(host))
910 s3cmci_check_sdio_irq(host);
914 static void s3cmci_dma_setup(
struct s3cmci_host *host,
920 if (last_source == source)
931 s3cmci_dma_done_callback);
937 static void s3cmci_send_command(
struct s3cmci_host *host,
946 enable_imask(host, imsk);
971 u32 dcon, imsk, stoptries = 3;
980 if ((data->
blksz & 3) != 0) {
985 pr_warning(
"%s: can't do non-word sized block transfers (blksz %d)\n", __func__, data->
blksz);
994 "mci_setup_data() transfer stillin progress.\n");
999 if ((stoptries--) == 0) {
1000 dbg_dumpregs(host,
"DRF");
1007 if (s3cmci_host_usedma(host))
1041 enable_imask(host, imsk);
1058 #define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)
1101 for (i = 0; i < dma_len; i++) {
1104 dbg(host,
dbg_dma,
"enqueue %i: %08x@%u\n", i,
1123 static void s3cmci_send_request(
struct mmc_host *mmc)
1140 int res = s3cmci_setup_data(host, cmd->
data);
1145 dbg(host,
dbg_err,
"setup data error %d\n", res);
1153 if (s3cmci_host_usedma(host))
1154 res = s3cmci_prepare_dma(host, cmd->
data);
1156 res = s3cmci_prepare_pio(host, cmd->
data);
1159 dbg(host,
dbg_err,
"data prepare error %d\n", res);
1169 s3cmci_send_command(host, cmd);
1172 s3cmci_enable_irq(host,
true);
1175 static int s3cmci_card_present(
struct mmc_host *mmc)
1192 host->
status =
"mmc request";
1196 if (s3cmci_card_present(mmc) == 0) {
1197 dbg(host,
dbg_err,
"%s: no medium present\n", __func__);
1201 s3cmci_send_request(mmc);
1209 for (mci_psc = 0; mci_psc < 255; mci_psc++) {
1223 if (ios->
clock == 0)
1243 if (host->
pdata->set_power)
1258 if (host->
pdata->set_power)
1264 s3cmci_set_clk(host, ios);
1276 dbg(host,
dbg_conf,
"running at %lukHz (requested: %ukHz).\n",
1285 static void s3cmci_reset(
struct s3cmci_host *host)
1293 static int s3cmci_get_ro(
struct mmc_host *mmc)
1308 static void s3cmci_enable_sdio_irq(
struct mmc_host *mmc,
int enable)
1311 unsigned long flags;
1345 s3cmci_check_sdio_irq(host);
1349 .request = s3cmci_request,
1350 .set_ios = s3cmci_set_ios,
1351 .get_ro = s3cmci_get_ro,
1352 .get_cd = s3cmci_card_present,
1353 .enable_sdio_irq = s3cmci_enable_sdio_irq,
1363 #ifdef CONFIG_CPU_FREQ
1366 unsigned long val,
void *data)
1370 unsigned long newclk;
1371 unsigned long flags;
1384 mmc->
ios.clock != 0)
1385 s3cmci_set_clk(host, &mmc->
ios);
1387 spin_unlock_irqrestore(&mmc->
lock, flags);
1393 static inline int s3cmci_cpufreq_register(
struct s3cmci_host *host)
1395 host->freq_transition.notifier_call = s3cmci_cpufreq_transition;
1401 static inline void s3cmci_cpufreq_deregister(
struct s3cmci_host *host)
1408 static inline int s3cmci_cpufreq_register(
struct s3cmci_host *host)
1413 static inline void s3cmci_cpufreq_deregister(
struct s3cmci_host *host)
1419 #ifdef CONFIG_DEBUG_FS
1421 static int s3cmci_state_show(
struct seq_file *seq,
void *
v)
1434 seq_printf(seq,
"Do DMA = %d\n", s3cmci_host_usedma(host));
1448 .open = s3cmci_state_open,
1454 #define DBG_REG(_r) { .addr = S3C2410_SDI##_r, .name = #_r }
1457 unsigned short addr;
1458 unsigned char *
name;
1478 static int s3cmci_regs_show(
struct seq_file *seq,
void *v)
1481 struct s3cmci_reg *
rptr = debug_regs;
1483 for (; rptr->name; rptr++)
1484 seq_printf(seq,
"SDI%s\t=0x%08x\n", rptr->name,
1499 .open = s3cmci_regs_open,
1505 static void s3cmci_debugfs_attach(
struct s3cmci_host *host)
1510 if (IS_ERR(host->debug_root)) {
1511 dev_err(dev,
"failed to create debugfs root\n");
1516 host->debug_root, host,
1517 &s3cmci_fops_state);
1519 if (IS_ERR(host->debug_state))
1520 dev_err(dev,
"failed to create debug state file\n");
1523 host->debug_root, host,
1526 if (IS_ERR(host->debug_regs))
1527 dev_err(dev,
"failed to create debug regs file\n");
1530 static void s3cmci_debugfs_remove(
struct s3cmci_host *host)
1538 static inline void s3cmci_debugfs_attach(
struct s3cmci_host *host) { }
1539 static inline void s3cmci_debugfs_remove(
struct s3cmci_host *host) { }
1562 dev_err(&pdev->
dev,
"failed to get gpio %d\n", i);
1567 goto probe_free_host;
1571 host = mmc_priv(mmc);
1576 host->
pdata = pdev->
dev.platform_data;
1578 pdev->
dev.platform_data = &s3cmci_def_pdata;
1579 host->
pdata = &s3cmci_def_pdata;
1598 #ifdef CONFIG_MMC_S3C_PIODMA
1605 "failed to get io memory region resource.\n");
1608 goto probe_free_gpio;
1612 resource_size(host->
mem), pdev->
name);
1615 dev_err(&pdev->
dev,
"failed to request io memory region.\n");
1617 goto probe_free_gpio;
1622 dev_err(&pdev->
dev,
"failed to ioremap() io memory region.\n");
1624 goto probe_free_mem_region;
1628 if (host->
irq == 0) {
1629 dev_err(&pdev->
dev,
"failed to get interrupt resource.\n");
1635 dev_err(&pdev->
dev,
"failed to request mci interrupt.\n");
1647 if (!host->
pdata->no_detect) {
1650 dev_err(&pdev->
dev,
"failed to get detect gpio\n");
1651 goto probe_free_irq;
1662 "can't get card detect irq.\n");
1664 goto probe_free_gpio_cd;
1668 "host detect has no irq available\n");
1674 if (!host->
pdata->no_wprotect) {
1677 dev_err(&pdev->
dev,
"failed to get writeprotect\n");
1678 goto probe_free_irq_cd;
1686 if (s3cmci_host_usedma(host)) {
1689 if (host->
dma < 0) {
1690 dev_err(&pdev->
dev,
"cannot get DMA channel.\n");
1691 if (!s3cmci_host_canpio()) {
1693 goto probe_free_gpio_wp;
1702 if (IS_ERR(host->
clk)) {
1703 dev_err(&pdev->
dev,
"failed to find clock source.\n");
1704 ret = PTR_ERR(host->
clk);
1706 goto probe_free_dma;
1711 dev_err(&pdev->
dev,
"failed to enable clock source.\n");
1717 mmc->
ops = &s3cmci_ops;
1719 #ifdef CONFIG_MMC_S3C_HW_SDIO_IRQ
1727 if (host->
pdata->ocr_avail)
1738 "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%u.\n",
1739 (host->
is2440?
"2440":
""),
1742 ret = s3cmci_cpufreq_register(host);
1744 dev_err(&pdev->
dev,
"failed to register cpufreq\n");
1750 dev_err(&pdev->
dev,
"failed to add mmc host.\n");
1754 s3cmci_debugfs_attach(host);
1756 platform_set_drvdata(pdev, mmc);
1758 s3cmci_host_usedma(host) ?
"dma" :
"pio",
1764 s3cmci_cpufreq_deregister(host);
1773 if (s3cmci_host_usedma(host))
1777 if (!host->
pdata->no_wprotect)
1781 if (!host->
pdata->no_detect)
1794 probe_free_mem_region:
1810 struct mmc_host *mmc = platform_get_drvdata(pdev);
1816 s3cmci_debugfs_remove(host);
1817 s3cmci_cpufreq_deregister(host);
1824 struct mmc_host *mmc = platform_get_drvdata(pdev);
1829 s3cmci_shutdown(pdev);
1835 if (s3cmci_host_usedma(host))
1859 .name =
"s3c2410-sdi",
1862 .name =
"s3c2412-sdi",
1865 .name =
"s3c2440-sdi",
1876 static int s3cmci_suspend(
struct device *dev)
1883 static int s3cmci_resume(
struct device *dev)
1892 .resume = s3cmci_resume,
1895 #define s3cmci_pm_ops &s3cmci_pm
1897 #define s3cmci_pm_ops NULL
1907 .id_table = s3cmci_driver_ids,
1908 .probe = s3cmci_probe,
1910 .shutdown = s3cmci_shutdown,