45 #include <linux/bitops.h>
64 #include <linux/module.h>
66 #define DRIVER_NAME "sh_mmcif"
67 #define DRIVER_VERSION "2010-04-28"
70 #define CMD_MASK 0x3f000000
71 #define CMD_SET_RTYP_NO ((0 << 23) | (0 << 22))
72 #define CMD_SET_RTYP_6B ((0 << 23) | (1 << 22))
73 #define CMD_SET_RTYP_17B ((1 << 23) | (0 << 22))
74 #define CMD_SET_RBSY (1 << 21)
75 #define CMD_SET_CCSEN (1 << 20)
76 #define CMD_SET_WDAT (1 << 19)
77 #define CMD_SET_DWEN (1 << 18)
78 #define CMD_SET_CMLTE (1 << 17)
79 #define CMD_SET_CMD12EN (1 << 16)
80 #define CMD_SET_RIDXC_INDEX ((0 << 15) | (0 << 14))
81 #define CMD_SET_RIDXC_BITS ((0 << 15) | (1 << 14))
82 #define CMD_SET_RIDXC_NO ((1 << 15) | (0 << 14))
83 #define CMD_SET_CRC7C ((0 << 13) | (0 << 12))
84 #define CMD_SET_CRC7C_BITS ((0 << 13) | (1 << 12))
85 #define CMD_SET_CRC7C_INTERNAL ((1 << 13) | (0 << 12))
86 #define CMD_SET_CRC16C (1 << 10)
87 #define CMD_SET_CRCSTE (1 << 8)
88 #define CMD_SET_TBIT (1 << 7)
89 #define CMD_SET_OPDM (1 << 6)
90 #define CMD_SET_CCSH (1 << 5)
91 #define CMD_SET_DATW_1 ((0 << 1) | (0 << 0))
92 #define CMD_SET_DATW_4 ((0 << 1) | (1 << 0))
93 #define CMD_SET_DATW_8 ((1 << 1) | (0 << 0))
96 #define CMD_CTRL_BREAK (1 << 0)
99 #define BLOCK_SIZE_MASK 0x0000ffff
102 #define INT_CCSDE (1 << 29)
103 #define INT_CMD12DRE (1 << 26)
104 #define INT_CMD12RBE (1 << 25)
105 #define INT_CMD12CRE (1 << 24)
106 #define INT_DTRANE (1 << 23)
107 #define INT_BUFRE (1 << 22)
108 #define INT_BUFWEN (1 << 21)
109 #define INT_BUFREN (1 << 20)
110 #define INT_CCSRCV (1 << 19)
111 #define INT_RBSYE (1 << 17)
112 #define INT_CRSPE (1 << 16)
113 #define INT_CMDVIO (1 << 15)
114 #define INT_BUFVIO (1 << 14)
115 #define INT_WDATERR (1 << 11)
116 #define INT_RDATERR (1 << 10)
117 #define INT_RIDXERR (1 << 9)
118 #define INT_RSPERR (1 << 8)
119 #define INT_CCSTO (1 << 5)
120 #define INT_CRCSTO (1 << 4)
121 #define INT_WDATTO (1 << 3)
122 #define INT_RDATTO (1 << 2)
123 #define INT_RBSYTO (1 << 1)
124 #define INT_RSPTO (1 << 0)
125 #define INT_ERR_STS (INT_CMDVIO | INT_BUFVIO | INT_WDATERR | \
126 INT_RDATERR | INT_RIDXERR | INT_RSPERR | \
127 INT_CCSTO | INT_CRCSTO | INT_WDATTO | \
128 INT_RDATTO | INT_RBSYTO | INT_RSPTO)
131 #define MASK_ALL 0x00000000
132 #define MASK_MCCSDE (1 << 29)
133 #define MASK_MCMD12DRE (1 << 26)
134 #define MASK_MCMD12RBE (1 << 25)
135 #define MASK_MCMD12CRE (1 << 24)
136 #define MASK_MDTRANE (1 << 23)
137 #define MASK_MBUFRE (1 << 22)
138 #define MASK_MBUFWEN (1 << 21)
139 #define MASK_MBUFREN (1 << 20)
140 #define MASK_MCCSRCV (1 << 19)
141 #define MASK_MRBSYE (1 << 17)
142 #define MASK_MCRSPE (1 << 16)
143 #define MASK_MCMDVIO (1 << 15)
144 #define MASK_MBUFVIO (1 << 14)
145 #define MASK_MWDATERR (1 << 11)
146 #define MASK_MRDATERR (1 << 10)
147 #define MASK_MRIDXERR (1 << 9)
148 #define MASK_MRSPERR (1 << 8)
149 #define MASK_MCCSTO (1 << 5)
150 #define MASK_MCRCSTO (1 << 4)
151 #define MASK_MWDATTO (1 << 3)
152 #define MASK_MRDATTO (1 << 2)
153 #define MASK_MRBSYTO (1 << 1)
154 #define MASK_MRSPTO (1 << 0)
156 #define MASK_START_CMD (MASK_MCMDVIO | MASK_MBUFVIO | MASK_MWDATERR | \
157 MASK_MRDATERR | MASK_MRIDXERR | MASK_MRSPERR | \
158 MASK_MCCSTO | MASK_MCRCSTO | MASK_MWDATTO | \
159 MASK_MRDATTO | MASK_MRBSYTO | MASK_MRSPTO)
162 #define STS1_CMDSEQ (1 << 31)
165 #define STS2_CRCSTE (1 << 31)
166 #define STS2_CRC16E (1 << 30)
167 #define STS2_AC12CRCE (1 << 29)
168 #define STS2_RSPCRC7E (1 << 28)
169 #define STS2_CRCSTEBE (1 << 27)
170 #define STS2_RDATEBE (1 << 26)
171 #define STS2_AC12REBE (1 << 25)
172 #define STS2_RSPEBE (1 << 24)
173 #define STS2_AC12IDXE (1 << 23)
174 #define STS2_RSPIDXE (1 << 22)
175 #define STS2_CCSTO (1 << 15)
176 #define STS2_RDATTO (1 << 14)
177 #define STS2_DATBSYTO (1 << 13)
178 #define STS2_CRCSTTO (1 << 12)
179 #define STS2_AC12BSYTO (1 << 11)
180 #define STS2_RSPBSYTO (1 << 10)
181 #define STS2_AC12RSPTO (1 << 9)
182 #define STS2_RSPTO (1 << 8)
183 #define STS2_CRC_ERR (STS2_CRCSTE | STS2_CRC16E | \
184 STS2_AC12CRCE | STS2_RSPCRC7E | STS2_CRCSTEBE)
185 #define STS2_TIMEOUT_ERR (STS2_CCSTO | STS2_RDATTO | \
186 STS2_DATBSYTO | STS2_CRCSTTO | \
187 STS2_AC12BSYTO | STS2_RSPBSYTO | \
188 STS2_AC12RSPTO | STS2_RSPTO)
190 #define CLKDEV_EMMC_DATA 52000000
191 #define CLKDEV_MMC_DATA 20000000
192 #define CLKDEV_INIT 400000
253 static void mmcif_dma_complete(
void *
arg)
258 dev_dbg(&host->
pd->dev,
"Command completed\n");
260 if (
WARN(!data,
"%s: NULL data in DMA completion!\n",
261 dev_name(&host->
pd->dev)))
276 static void sh_mmcif_start_dma_rx(
struct sh_mmcif_host *host)
289 desc = dmaengine_prep_slave_sg(chan, sg, ret,
294 desc->
callback = mmcif_dma_complete;
296 cookie = dmaengine_submit(desc);
298 dma_async_issue_pending(chan);
300 dev_dbg(&host->
pd->dev,
"%s(): mapped %d -> %d, cookie %d\n",
301 __func__, data->
sg_len, ret, cookie);
317 "DMA failed: %d, falling back to PIO\n", ret);
321 dev_dbg(&host->
pd->dev,
"%s(): desc %p, cookie %d, sg[%d]\n", __func__,
322 desc, cookie, data->
sg_len);
325 static void sh_mmcif_start_dma_tx(
struct sh_mmcif_host *host)
338 desc = dmaengine_prep_slave_sg(chan, sg, ret,
343 desc->
callback = mmcif_dma_complete;
345 cookie = dmaengine_submit(desc);
347 dma_async_issue_pending(chan);
349 dev_dbg(&host->
pd->dev,
"%s(): mapped %d -> %d, cookie %d\n",
350 __func__, data->
sg_len, ret, cookie);
366 "DMA failed: %d, falling back to PIO\n", ret);
370 dev_dbg(&host->
pd->dev,
"%s(): desc %p, cookie %d\n", __func__,
396 dev_dbg(&host->
pd->dev,
"%s: TX: got channel %p\n", __func__,
406 ret = dmaengine_slave_config(host->
chan_tx, &
cfg);
412 dev_dbg(&host->
pd->dev,
"%s: RX: got channel %p\n", __func__,
422 ret = dmaengine_slave_config(host->
chan_rx, &
cfg);
457 static void sh_mmcif_clock_control(
struct sh_mmcif_host *host,
unsigned int clk)
467 if (sup_pclk && clk == host->
clk)
472 clk) - 1) - 1) << 16));
500 dev_dbg(&host->
pd->dev,
"ERR HOST_STS1 = %08x\n", state1);
501 dev_dbg(&host->
pd->dev,
"ERR HOST_STS2 = %08x\n", state2);
506 for (timeout = 10000000; timeout; timeout--) {
514 "Forced end of command sequence timeout err\n");
517 sh_mmcif_sync_reset(host);
518 dev_dbg(&host->
pd->dev,
"Forced end of command sequence\n");
523 dev_dbg(&host->
pd->dev,
": CRC error\n");
529 dev_dbg(&host->
pd->dev,
": End/Index error\n");
574 u32 *p = sg_virt(data->
sg);
578 data->
error = sh_mmcif_error_manage(host);
582 for (i = 0; i < host->
blocksize / 4; i++)
597 if (!data->
sg_len || !data->
sg->length)
618 data->
error = sh_mmcif_error_manage(host);
624 for (i = 0; i < host->
blocksize / 4; i++)
627 if (!sh_mmcif_next_block(host, p))
636 static void sh_mmcif_single_write(
struct sh_mmcif_host *host,
652 u32 *p = sg_virt(data->
sg);
656 data->
error = sh_mmcif_error_manage(host);
660 for (i = 0; i < host->
blocksize / 4; i++)
675 if (!data->
sg_len || !data->
sg->length)
689 static bool sh_mmcif_mwrite_block(
struct sh_mmcif_host *host)
696 data->
error = sh_mmcif_error_manage(host);
702 for (i = 0; i < host->
blocksize / 4; i++)
705 if (!sh_mmcif_next_block(host, p))
714 static void sh_mmcif_get_response(
struct sh_mmcif_host *host,
726 static void sh_mmcif_get_cmd12response(
struct sh_mmcif_host *host,
754 dev_err(&host->
pd->dev,
"Unsupported response type.\n");
781 dev_err(&host->
pd->dev,
"Unsupported bus width.\n");
806 return (opc << 24) |
tmp;
814 sh_mmcif_multi_read(host, mrq);
817 sh_mmcif_multi_write(host, mrq);
820 sh_mmcif_single_write(host, mrq);
824 sh_mmcif_single_read(host, mrq);
827 dev_err(&host->
pd->dev,
"UNSUPPORTED CMD = d'%08d\n", opc);
858 opc = sh_mmcif_set_cmd(host, mrq);
874 switch (mrq->
cmd->opcode) {
882 dev_err(&host->
pd->dev,
"unsupported stop cmd\n");
883 mrq->
stop->error = sh_mmcif_error_manage(host);
898 spin_unlock_irqrestore(&host->
lock, flags);
905 spin_unlock_irqrestore(&host->
lock, flags);
907 switch (mrq->
cmd->opcode) {
924 sh_mmcif_start_cmd(host, mrq);
933 host->
mmc->f_max = host->
clk / 2;
934 host->
mmc->f_min = host->
clk / 512;
947 if (!IS_ERR(mmc->
supply.vmmc))
949 mmc_regulator_set_ocr(mmc, mmc->
supply.vmmc,
953 static void sh_mmcif_set_ios(
struct mmc_host *mmc,
struct mmc_ios *ios)
960 spin_unlock_irqrestore(&host->
lock, flags);
965 spin_unlock_irqrestore(&host->
lock, flags);
970 sh_mmcif_request_dma(host, host->
pd->dev.platform_data);
973 sh_mmcif_set_power(host, ios);
976 sh_mmcif_clock_control(host, 0);
979 sh_mmcif_release_dma(host);
984 pm_runtime_put(&host->
pd->dev);
988 sh_mmcif_set_power(host, ios);
996 sh_mmcif_clk_update(host);
997 pm_runtime_get_sync(&host->
pd->dev);
999 sh_mmcif_sync_reset(host);
1001 sh_mmcif_clock_control(host, ios->
clock);
1008 static int sh_mmcif_get_cd(
struct mmc_host *mmc)
1024 .request = sh_mmcif_request,
1025 .set_ios = sh_mmcif_set_ios,
1026 .get_cd = sh_mmcif_get_cd,
1044 cmd->
error = sh_mmcif_error_manage(host);
1045 dev_dbg(&host->
pd->dev,
"Cmd(d'%d) error %d\n",
1056 sh_mmcif_get_response(host, cmd);
1063 sh_mmcif_start_dma_rx(host);
1066 sh_mmcif_start_dma_tx(host);
1081 "Error IRQ while waiting for DMA completion!\n");
1084 dmaengine_terminate_all(host->
chan_rx);
1086 dmaengine_terminate_all(host->
chan_tx);
1087 data->
error = sh_mmcif_error_manage(host);
1090 }
else if (time < 0) {
1119 if (sh_mmcif_end_cmd(host))
1124 if (sh_mmcif_mread_block(host))
1129 if (sh_mmcif_read_block(host))
1134 if (sh_mmcif_mwrite_block(host))
1139 if (sh_mmcif_write_block(host))
1145 mrq->
stop->error = sh_mmcif_error_manage(host);
1148 sh_mmcif_get_cmd12response(host, mrq->
stop);
1149 mrq->
stop->error = 0;
1154 mrq->
data->error = sh_mmcif_error_manage(host);
1162 if (!mrq->
cmd->error && data && !data->
error)
1166 if (mrq->
stop && !mrq->
cmd->error && (!data || !data->
error)) {
1167 sh_mmcif_stop_cmd(host, mrq);
1168 if (!mrq->
stop->error)
1181 static irqreturn_t sh_mmcif_intr(
int irq,
void *dev_id)
1225 dev_dbg(&host->
pd->dev,
"Unsupported interrupt: 0x%x\n", state);
1232 dev_dbg(&host->
pd->dev,
"int err state = %08x\n", state);
1238 mmcif_dma_complete(host);
1240 dev_dbg(&host->
pd->dev,
"Unexpected IRQ 0x%x\n", state);
1262 mrq->
cmd->error = sh_mmcif_error_manage(host);
1265 mrq->
stop->error = sh_mmcif_error_manage(host);
1273 mrq->
data->error = sh_mmcif_error_manage(host);
1290 mmc_regulator_get_supply(mmc);
1303 int ret = 0, irq[2];
1313 if (irq[0] < 0 || irq[1] < 0) {
1319 dev_err(&pdev->
dev,
"platform_get_resource error.\n");
1333 host = mmc_priv(mmc);
1342 mmc->
ops = &sh_mmcif_ops;
1343 sh_mmcif_init_ocr(host);
1354 platform_set_drvdata(pdev, host);
1357 host->
power =
false;
1359 snprintf(clk_name,
sizeof(clk_name),
"mmc%d", pdev->
id);
1361 if (IS_ERR(host->
hclk)) {
1362 ret = PTR_ERR(host->
hclk);
1363 dev_err(&pdev->
dev,
"cannot get clock \"%s\": %d\n", clk_name, ret);
1366 ret = sh_mmcif_clk_update(host);
1370 ret = pm_runtime_resume(&pdev->
dev);
1376 sh_mmcif_sync_reset(host);
1381 dev_err(&pdev->
dev,
"request_irq error (sh_mmc:error)\n");
1386 dev_err(&pdev->
dev,
"request_irq error (sh_mmc:int)\n");
1401 dev_pm_qos_expose_latency_limit(&pdev->
dev, 100);
1416 pm_runtime_suspend(&pdev->
dev);
1422 pm_runtime_disable(&pdev->
dev);
1437 pm_runtime_get_sync(&pdev->
dev);
1439 dev_pm_qos_hide_latency_limit(&pdev->
dev);
1463 platform_set_drvdata(pdev,
NULL);
1467 pm_runtime_put_sync(&pdev->
dev);
1468 pm_runtime_disable(&pdev->
dev);
1492 #define sh_mmcif_suspend NULL
1493 #define sh_mmcif_resume NULL
1497 { .compatible =
"renesas,sh-mmcif" },
1502 static const struct dev_pm_ops sh_mmcif_dev_pm_ops = {
1508 .probe = sh_mmcif_probe,
1509 .remove = sh_mmcif_remove,
1512 .pm = &sh_mmcif_dev_pm_ops,
1514 .of_match_table = mmcif_of_match,