23 #include <linux/module.h>
43 #define DAVINCI_MMCCTL 0x00
44 #define DAVINCI_MMCCLK 0x04
45 #define DAVINCI_MMCST0 0x08
46 #define DAVINCI_MMCST1 0x0C
47 #define DAVINCI_MMCIM 0x10
48 #define DAVINCI_MMCTOR 0x14
49 #define DAVINCI_MMCTOD 0x18
50 #define DAVINCI_MMCBLEN 0x1C
51 #define DAVINCI_MMCNBLK 0x20
52 #define DAVINCI_MMCNBLC 0x24
53 #define DAVINCI_MMCDRR 0x28
54 #define DAVINCI_MMCDXR 0x2C
55 #define DAVINCI_MMCCMD 0x30
56 #define DAVINCI_MMCARGHL 0x34
57 #define DAVINCI_MMCRSP01 0x38
58 #define DAVINCI_MMCRSP23 0x3C
59 #define DAVINCI_MMCRSP45 0x40
60 #define DAVINCI_MMCRSP67 0x44
61 #define DAVINCI_MMCDRSP 0x48
62 #define DAVINCI_MMCETOK 0x4C
63 #define DAVINCI_MMCCIDX 0x50
64 #define DAVINCI_MMCCKC 0x54
65 #define DAVINCI_MMCTORC 0x58
66 #define DAVINCI_MMCTODC 0x5C
67 #define DAVINCI_MMCBLNC 0x60
68 #define DAVINCI_SDIOCTL 0x64
69 #define DAVINCI_SDIOST0 0x68
70 #define DAVINCI_SDIOIEN 0x6C
71 #define DAVINCI_SDIOIST 0x70
72 #define DAVINCI_MMCFIFOCTL 0x74
75 #define MMCCTL_DATRST (1 << 0)
76 #define MMCCTL_CMDRST (1 << 1)
77 #define MMCCTL_WIDTH_8_BIT (1 << 8)
78 #define MMCCTL_WIDTH_4_BIT (1 << 2)
79 #define MMCCTL_DATEG_DISABLED (0 << 6)
80 #define MMCCTL_DATEG_RISING (1 << 6)
81 #define MMCCTL_DATEG_FALLING (2 << 6)
82 #define MMCCTL_DATEG_BOTH (3 << 6)
83 #define MMCCTL_PERMDR_LE (0 << 9)
84 #define MMCCTL_PERMDR_BE (1 << 9)
85 #define MMCCTL_PERMDX_LE (0 << 10)
86 #define MMCCTL_PERMDX_BE (1 << 10)
89 #define MMCCLK_CLKEN (1 << 8)
90 #define MMCCLK_CLKRT_MASK (0xFF << 0)
93 #define MMCST0_DATDNE BIT(0)
94 #define MMCST0_BSYDNE BIT(1)
95 #define MMCST0_RSPDNE BIT(2)
96 #define MMCST0_TOUTRD BIT(3)
97 #define MMCST0_TOUTRS BIT(4)
98 #define MMCST0_CRCWR BIT(5)
99 #define MMCST0_CRCRD BIT(6)
100 #define MMCST0_CRCRS BIT(7)
101 #define MMCST0_DXRDY BIT(9)
102 #define MMCST0_DRRDY BIT(10)
103 #define MMCST0_DATED BIT(11)
104 #define MMCST0_TRNDNE BIT(12)
107 #define MMCST1_BUSY (1 << 0)
110 #define MMCCMD_CMD_MASK (0x3F << 0)
111 #define MMCCMD_PPLEN (1 << 7)
112 #define MMCCMD_BSYEXP (1 << 8)
113 #define MMCCMD_RSPFMT_MASK (3 << 9)
114 #define MMCCMD_RSPFMT_NONE (0 << 9)
115 #define MMCCMD_RSPFMT_R1456 (1 << 9)
116 #define MMCCMD_RSPFMT_R2 (2 << 9)
117 #define MMCCMD_RSPFMT_R3 (3 << 9)
118 #define MMCCMD_DTRW (1 << 11)
119 #define MMCCMD_STRMTP (1 << 12)
120 #define MMCCMD_WDATX (1 << 13)
121 #define MMCCMD_INITCK (1 << 14)
122 #define MMCCMD_DCLR (1 << 15)
123 #define MMCCMD_DMATRIG (1 << 16)
126 #define MMCFIFOCTL_FIFORST (1 << 0)
127 #define MMCFIFOCTL_FIFODIR_WR (1 << 1)
128 #define MMCFIFOCTL_FIFODIR_RD (0 << 1)
129 #define MMCFIFOCTL_FIFOLEV (1 << 2)
130 #define MMCFIFOCTL_ACCWD_4 (0 << 3)
131 #define MMCFIFOCTL_ACCWD_3 (1 << 3)
132 #define MMCFIFOCTL_ACCWD_2 (2 << 3)
133 #define MMCFIFOCTL_ACCWD_1 (3 << 3)
136 #define SDIOST0_DAT1_HI BIT(0)
139 #define SDIOIEN_IOINTEN BIT(0)
142 #define SDIOIST_IOINT BIT(0)
145 #define MMCSD_INIT_CLOCK 200000
155 #define MAX_CCNT ((1 << 16) - 1)
159 static unsigned rw_threshold = 32;
162 "Read/Write threshold. Default = 32");
164 static unsigned poll_threshold = 128;
167 "Polling transaction size threshold. Default = 128");
169 static unsigned poll_loopcount = 32;
172 "Maximum polling loop count. Default = 32");
189 #define DAVINCI_MMC_DATADIR_NONE 0
190 #define DAVINCI_MMC_DATADIR_READ 1
191 #define DAVINCI_MMC_DATADIR_WRITE 2
221 #ifdef CONFIG_CPU_FREQ
245 mmc_davinci_sg_to_buf(host);
259 for (i = 0; i < (n >> 2); i++) {
268 for (i = 0; i < (n >> 2); i++) {
291 s =
", R1/R5/R6/R7 response";
294 s =
", R1b response";
300 s =
", R3/R4 response";
303 s =
", (R? response)";
380 davinci_fifo_data_trans(host, rw_threshold);
391 mmc_davinci_irq(0, host);
413 dmaengine_terminate_all(sync_dev);
432 dmaengine_slave_config(host->
dma_tx, &dma_tx_conf);
434 desc = dmaengine_prep_slave_sg(host->
dma_tx,
441 "failed to allocate DMA TX descriptor");
454 dmaengine_slave_config(host->
dma_rx, &dma_rx_conf);
456 desc = dmaengine_prep_slave_sg(host->
dma_rx,
463 "failed to allocate DMA RX descriptor");
469 dmaengine_submit(desc);
470 dma_async_issue_pending(chan);
480 int mask = rw_threshold - 1;
489 for (i = 0; i < host->
sg_len; i++) {
501 ret = mmc_davinci_send_dma_request(host, data);
536 goto free_master_write;
575 if (timeout > 0xffff)
613 && mmc_davinci_start_dma_transfer(host, data) == 0) {
619 host->
sg = host->
data->sg;
620 mmc_davinci_sg_to_buf(host);
647 mmc_davinci_prepare_data(host, req);
648 mmc_davinci_start_command(host, req->
cmd);
652 unsigned int mmc_req_freq)
654 unsigned int mmc_freq = 0, mmc_pclk = 0, mmc_push_pull_divisor = 0;
657 if (mmc_req_freq && mmc_pclk > (2 * mmc_req_freq))
658 mmc_push_pull_divisor = ((
unsigned int)mmc_pclk
659 / (2 * mmc_req_freq)) - 1;
661 mmc_push_pull_divisor = 0;
663 mmc_freq = (
unsigned int)mmc_pclk
664 / (2 * (mmc_push_pull_divisor + 1));
666 if (mmc_freq > mmc_req_freq)
667 mmc_push_pull_divisor = mmc_push_pull_divisor + 1;
669 if (mmc_req_freq <= 400000)
671 / (2 * (mmc_push_pull_divisor + 1)))/1000));
674 / (2 * (mmc_push_pull_divisor + 1)))/1000000));
676 return mmc_push_pull_divisor;
679 static void calculate_clk_divider(
struct mmc_host *mmc,
struct mmc_ios *ios)
681 unsigned int open_drain_freq = 0, mmc_pclk = 0;
682 unsigned int mmc_push_pull_freq = 0;
691 open_drain_freq = ((
unsigned int)mmc_pclk
694 if (open_drain_freq > 0xFF)
695 open_drain_freq = 0xFF;
698 temp |= open_drain_freq;
705 mmc_push_pull_freq = calculate_freq_for_card(host, ios->
clock);
707 if (mmc_push_pull_freq > 0xFF)
708 mmc_push_pull_freq = 0xFF;
716 temp |= mmc_push_pull_freq;
725 static void mmc_davinci_set_ios(
struct mmc_host *mmc,
struct mmc_ios *ios)
732 "clock %dHz busmode %d powermode %d Vdd %04x\n",
778 calculate_clk_divider(mmc, ios);
818 mmc_signal_sdio_irq(host->
mmc);
823 davinci_abort_dma(host);
833 if (!data->
stop || (host->
cmd && host->
cmd->error)) {
838 mmc_davinci_start_command(host, data->
stop);
861 cmd->
mrq->cmd->retries = 0;
886 mmc_davinci_reset_ctrl(host, 1);
887 mmc_davinci_reset_ctrl(host, 0);
898 "SDIO interrupt status %x\n", status);
900 mmc_signal_sdio_irq(host->
mmc);
905 static irqreturn_t mmc_davinci_irq(
int irq,
void *dev_id)
908 unsigned int status, qstatus;
910 int end_transfer = 0;
916 "Spurious interrupt 0x%04x\n", status);
933 unsigned long im_val;
946 davinci_fifo_data_trans(host, rw_threshold);
968 davinci_fifo_data_trans(host, host->
bytes_left);
974 "DATDNE with no host->data\n");
984 "read data timeout, status %x\n",
987 davinci_abort_data(host, data);
1008 (qstatus & MMCST0_CRCWR) ?
"write" :
"read",
1011 davinci_abort_data(host, data);
1018 "CMD%d timeout, status %x\n",
1019 host->
cmd->opcode, qstatus);
1023 davinci_abort_data(host, data);
1040 end_command = (
int) host->
cmd;
1044 mmc_davinci_cmd_done(host, host->
cmd);
1046 mmc_davinci_xfer_done(host, data);
1050 static int mmc_davinci_get_cd(
struct mmc_host *mmc)
1055 if (!config || !config->
get_cd)
1060 static int mmc_davinci_get_ro(
struct mmc_host *mmc)
1065 if (!config || !config->
get_ro)
1070 static void mmc_davinci_enable_sdio_irq(
struct mmc_host *mmc,
int enable)
1077 mmc_signal_sdio_irq(host->
mmc);
1091 .request = mmc_davinci_request,
1092 .set_ios = mmc_davinci_set_ios,
1093 .get_cd = mmc_davinci_get_cd,
1094 .get_ro = mmc_davinci_get_ro,
1095 .enable_sdio_irq = mmc_davinci_enable_sdio_irq,
1100 #ifdef CONFIG_CPU_FREQ
1101 static int mmc_davinci_cpufreq_transition(
struct notifier_block *nb,
1102 unsigned long val,
void *data)
1105 unsigned int mmc_pclk;
1107 unsigned long flags;
1116 calculate_clk_divider(mmc, &mmc->
ios);
1117 spin_unlock_irqrestore(&mmc->
lock, flags);
1123 static inline int mmc_davinci_cpufreq_register(
struct mmc_davinci_host *host)
1125 host->freq_transition.notifier_call = mmc_davinci_cpufreq_transition;
1131 static inline void mmc_davinci_cpufreq_deregister(
struct mmc_davinci_host *host)
1137 static inline int mmc_davinci_cpufreq_register(
struct mmc_davinci_host *host)
1142 static inline void mmc_davinci_cpufreq_deregister(
struct mmc_davinci_host *host)
1149 mmc_davinci_reset_ctrl(host, 1);
1157 mmc_davinci_reset_ctrl(host, 0);
1166 int ret = 0, irq = 0;
1178 mem_size = resource_size(r);
1188 host = mmc_priv(mmc);
1208 if (IS_ERR(host->
clk)) {
1209 ret = PTR_ERR(host->
clk);
1215 init_mmcsd_host(host);
1227 if (host->
use_dma && davinci_acquire_dma_channels(host) != 0)
1234 if (pdata && (pdata->
wires == 4 || pdata->
wires == 0))
1237 if (pdata && (pdata->
wires == 8))
1242 mmc->
ops = &mmc_davinci_ops;
1243 mmc->
f_min = 312500;
1244 mmc->
f_max = 25000000;
1247 if (pdata && pdata->
caps)
1270 platform_set_drvdata(pdev, host);
1272 ret = mmc_davinci_cpufreq_register(host);
1274 dev_err(&pdev->
dev,
"failed to register cpufreq\n");
1296 host->
use_dma ?
"DMA" :
"PIO",
1302 mmc_davinci_cpufreq_deregister(host);
1305 davinci_release_dma_channels(host);
1331 platform_set_drvdata(pdev,
NULL);
1333 mmc_davinci_cpufreq_deregister(host);
1340 davinci_release_dma_channels(host);
1356 static int davinci_mmcsd_suspend(
struct device *
dev)
1365 mmc_davinci_reset_ctrl(host, 1);
1375 static int davinci_mmcsd_resume(
struct device *dev)
1386 mmc_davinci_reset_ctrl(host, 0);
1394 static const struct dev_pm_ops davinci_mmcsd_pm = {
1395 .
suspend = davinci_mmcsd_suspend,
1396 .resume = davinci_mmcsd_resume,
1399 #define davinci_mmcsd_pm_ops (&davinci_mmcsd_pm)
1401 #define davinci_mmcsd_pm_ops NULL
1406 .name =
"davinci_mmc",
1410 .remove =
__exit_p(davinci_mmcsd_remove),
1413 static int __init davinci_mmcsd_init(
void)
1416 davinci_mmcsd_probe);
1420 static void __exit davinci_mmcsd_exit(
void)