1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2011 Renesas Solutions Corp.
17 #include <dm/device_compat.h>
18 #include <linux/delay.h>
19 #include <linux/errno.h>
20 #include <linux/compat.h>
22 #include <linux/sizes.h>
25 #define DRIVER_NAME "sh_mmcif"
27 static int sh_mmcif_intr(void *dev_id)
29 struct sh_mmcif_host *host = dev_id;
32 state = sh_mmcif_read(&host->regs->ce_int);
33 state &= sh_mmcif_read(&host->regs->ce_int_mask);
35 if (state & INT_RBSYE) {
36 sh_mmcif_write(~(INT_RBSYE | INT_CRSPE), &host->regs->ce_int);
37 sh_mmcif_bitclr(MASK_MRBSYE, &host->regs->ce_int_mask);
39 } else if (state & INT_CRSPE) {
40 sh_mmcif_write(~INT_CRSPE, &host->regs->ce_int);
41 sh_mmcif_bitclr(MASK_MCRSPE, &host->regs->ce_int_mask);
42 /* one more interrupt (INT_RBSYE) */
43 if (sh_mmcif_read(&host->regs->ce_cmd_set) & CMD_SET_RBSY)
46 } else if (state & INT_BUFREN) {
47 sh_mmcif_write(~INT_BUFREN, &host->regs->ce_int);
48 sh_mmcif_bitclr(MASK_MBUFREN, &host->regs->ce_int_mask);
50 } else if (state & INT_BUFWEN) {
51 sh_mmcif_write(~INT_BUFWEN, &host->regs->ce_int);
52 sh_mmcif_bitclr(MASK_MBUFWEN, &host->regs->ce_int_mask);
54 } else if (state & INT_CMD12DRE) {
55 sh_mmcif_write(~(INT_CMD12DRE | INT_CMD12RBE | INT_CMD12CRE |
56 INT_BUFRE), &host->regs->ce_int);
57 sh_mmcif_bitclr(MASK_MCMD12DRE, &host->regs->ce_int_mask);
59 } else if (state & INT_BUFRE) {
60 sh_mmcif_write(~INT_BUFRE, &host->regs->ce_int);
61 sh_mmcif_bitclr(MASK_MBUFRE, &host->regs->ce_int_mask);
63 } else if (state & INT_DTRANE) {
64 sh_mmcif_write(~INT_DTRANE, &host->regs->ce_int);
65 sh_mmcif_bitclr(MASK_MDTRANE, &host->regs->ce_int_mask);
67 } else if (state & INT_CMD12RBE) {
68 sh_mmcif_write(~(INT_CMD12RBE | INT_CMD12CRE),
70 sh_mmcif_bitclr(MASK_MCMD12RBE, &host->regs->ce_int_mask);
72 } else if (state & INT_ERR_STS) {
74 sh_mmcif_write(~state, &host->regs->ce_int);
75 sh_mmcif_bitclr(state, &host->regs->ce_int_mask);
82 debug("%s: int err state = %08x\n", DRIVER_NAME, state);
88 static int mmcif_wait_interrupt_flag(struct sh_mmcif_host *host)
90 int timeout = 10000000;
99 if (!sh_mmcif_intr(host))
102 udelay(1); /* 1 usec */
105 return 1; /* Return value: NOT 0 = complete waiting */
108 static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
110 sh_mmcif_bitclr(CLK_ENABLE, &host->regs->ce_clk_ctrl);
111 sh_mmcif_bitclr(CLK_CLEAR, &host->regs->ce_clk_ctrl);
116 if (clk == CLKDEV_EMMC_DATA)
117 sh_mmcif_bitset(CLK_PCLK, &host->regs->ce_clk_ctrl);
119 sh_mmcif_bitset((fls(DIV_ROUND_UP(host->clk,
120 clk) - 1) - 1) << 16,
121 &host->regs->ce_clk_ctrl);
122 sh_mmcif_bitset(CLK_ENABLE, &host->regs->ce_clk_ctrl);
125 static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
129 tmp = sh_mmcif_read(&host->regs->ce_clk_ctrl) & (CLK_ENABLE |
132 sh_mmcif_write(SOFT_RST_ON, &host->regs->ce_version);
133 sh_mmcif_write(SOFT_RST_OFF, &host->regs->ce_version);
134 sh_mmcif_bitset(tmp | SRSPTO_256 | SRBSYTO_29 | SRWDTO_29 | SCCSTO_29,
135 &host->regs->ce_clk_ctrl);
137 sh_mmcif_bitset(BUF_ACC_ATYP, &host->regs->ce_buf_acc);
140 static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
143 int ret, timeout = 10000000;
148 state1 = sh_mmcif_read(&host->regs->ce_host_sts1);
149 state2 = sh_mmcif_read(&host->regs->ce_host_sts2);
150 debug("%s: ERR HOST_STS1 = %08x\n", \
151 DRIVER_NAME, sh_mmcif_read(&host->regs->ce_host_sts1));
152 debug("%s: ERR HOST_STS2 = %08x\n", \
153 DRIVER_NAME, sh_mmcif_read(&host->regs->ce_host_sts2));
155 if (state1 & STS1_CMDSEQ) {
156 debug("%s: Forced end of command sequence\n", DRIVER_NAME);
157 sh_mmcif_bitset(CMD_CTRL_BREAK, &host->regs->ce_cmd_ctrl);
158 sh_mmcif_bitset(~CMD_CTRL_BREAK, &host->regs->ce_cmd_ctrl);
162 printf(DRIVER_NAME": Forceed end of " \
163 "command sequence timeout err\n");
166 if (!(sh_mmcif_read(&host->regs->ce_host_sts1)
170 sh_mmcif_sync_reset(host);
174 if (state2 & STS2_CRC_ERR)
176 else if (state2 & STS2_TIMEOUT_ERR)
183 static int sh_mmcif_single_read(struct sh_mmcif_host *host,
184 struct mmc_data *data)
188 unsigned long *p = (unsigned long *)data->dest;
190 if ((unsigned long)p & 0x00000001) {
191 printf("%s: The data pointer is unaligned.", __func__);
197 /* buf read enable */
198 sh_mmcif_bitset(MASK_MBUFREN, &host->regs->ce_int_mask);
199 time = mmcif_wait_interrupt_flag(host);
200 if (time == 0 || host->sd_error != 0)
201 return sh_mmcif_error_manage(host);
204 blocksize = (BLOCK_SIZE_MASK &
205 sh_mmcif_read(&host->regs->ce_block_set)) + 3;
206 for (i = 0; i < blocksize / 4; i++)
207 *p++ = sh_mmcif_read(&host->regs->ce_data);
209 /* buffer read end */
210 sh_mmcif_bitset(MASK_MBUFRE, &host->regs->ce_int_mask);
211 time = mmcif_wait_interrupt_flag(host);
212 if (time == 0 || host->sd_error != 0)
213 return sh_mmcif_error_manage(host);
219 static int sh_mmcif_multi_read(struct sh_mmcif_host *host,
220 struct mmc_data *data)
224 unsigned long *p = (unsigned long *)data->dest;
226 if ((unsigned long)p & 0x00000001) {
227 printf("%s: The data pointer is unaligned.", __func__);
232 blocksize = BLOCK_SIZE_MASK & sh_mmcif_read(&host->regs->ce_block_set);
233 for (j = 0; j < data->blocks; j++) {
234 sh_mmcif_bitset(MASK_MBUFREN, &host->regs->ce_int_mask);
235 time = mmcif_wait_interrupt_flag(host);
236 if (time == 0 || host->sd_error != 0)
237 return sh_mmcif_error_manage(host);
240 for (i = 0; i < blocksize / 4; i++)
241 *p++ = sh_mmcif_read(&host->regs->ce_data);
248 static int sh_mmcif_single_write(struct sh_mmcif_host *host,
249 struct mmc_data *data)
253 const unsigned long *p = (unsigned long *)data->dest;
255 if ((unsigned long)p & 0x00000001) {
256 printf("%s: The data pointer is unaligned.", __func__);
261 sh_mmcif_bitset(MASK_MBUFWEN, &host->regs->ce_int_mask);
263 time = mmcif_wait_interrupt_flag(host);
264 if (time == 0 || host->sd_error != 0)
265 return sh_mmcif_error_manage(host);
268 blocksize = (BLOCK_SIZE_MASK &
269 sh_mmcif_read(&host->regs->ce_block_set)) + 3;
270 for (i = 0; i < blocksize / 4; i++)
271 sh_mmcif_write(*p++, &host->regs->ce_data);
273 /* buffer write end */
274 sh_mmcif_bitset(MASK_MDTRANE, &host->regs->ce_int_mask);
276 time = mmcif_wait_interrupt_flag(host);
277 if (time == 0 || host->sd_error != 0)
278 return sh_mmcif_error_manage(host);
284 static int sh_mmcif_multi_write(struct sh_mmcif_host *host,
285 struct mmc_data *data)
289 const unsigned long *p = (unsigned long *)data->dest;
291 if ((unsigned long)p & 0x00000001) {
292 printf("%s: The data pointer is unaligned.", __func__);
297 blocksize = BLOCK_SIZE_MASK & sh_mmcif_read(&host->regs->ce_block_set);
298 for (j = 0; j < data->blocks; j++) {
299 sh_mmcif_bitset(MASK_MBUFWEN, &host->regs->ce_int_mask);
301 time = mmcif_wait_interrupt_flag(host);
303 if (time == 0 || host->sd_error != 0)
304 return sh_mmcif_error_manage(host);
307 for (i = 0; i < blocksize / 4; i++)
308 sh_mmcif_write(*p++, &host->regs->ce_data);
315 static void sh_mmcif_get_response(struct sh_mmcif_host *host,
318 if (cmd->resp_type & MMC_RSP_136) {
319 cmd->response[0] = sh_mmcif_read(&host->regs->ce_resp3);
320 cmd->response[1] = sh_mmcif_read(&host->regs->ce_resp2);
321 cmd->response[2] = sh_mmcif_read(&host->regs->ce_resp1);
322 cmd->response[3] = sh_mmcif_read(&host->regs->ce_resp0);
323 debug(" RESP %08x, %08x, %08x, %08x\n", cmd->response[0],
324 cmd->response[1], cmd->response[2], cmd->response[3]);
326 cmd->response[0] = sh_mmcif_read(&host->regs->ce_resp0);
330 static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
333 cmd->response[0] = sh_mmcif_read(&host->regs->ce_resp_cmd12);
336 static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
337 struct mmc_data *data, struct mmc_cmd *cmd)
340 u32 opc = cmd->cmdidx;
342 /* Response Type check */
343 switch (cmd->resp_type) {
345 tmp |= CMD_SET_RTYP_NO;
350 tmp |= CMD_SET_RTYP_6B;
353 tmp |= CMD_SET_RTYP_17B;
356 printf(DRIVER_NAME": Not support type response.\n");
361 if (opc == MMC_CMD_SWITCH)
367 switch (host->bus_width) {
368 case MMC_BUS_WIDTH_1:
369 tmp |= CMD_SET_DATW_1;
371 case MMC_BUS_WIDTH_4:
372 tmp |= CMD_SET_DATW_4;
374 case MMC_BUS_WIDTH_8:
375 tmp |= CMD_SET_DATW_8;
378 printf(DRIVER_NAME": Not support bus width.\n");
383 if (opc == MMC_CMD_WRITE_SINGLE_BLOCK ||
384 opc == MMC_CMD_WRITE_MULTIPLE_BLOCK)
387 if (opc == MMC_CMD_READ_MULTIPLE_BLOCK ||
388 opc == MMC_CMD_WRITE_MULTIPLE_BLOCK) {
389 tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN;
390 sh_mmcif_bitset(data->blocks << 16, &host->regs->ce_block_set);
392 /* RIDXC[1:0] check bits */
393 if (opc == MMC_CMD_SEND_OP_COND || opc == MMC_CMD_ALL_SEND_CID ||
394 opc == MMC_CMD_SEND_CSD || opc == MMC_CMD_SEND_CID)
395 tmp |= CMD_SET_RIDXC_BITS;
396 /* RCRC7C[1:0] check bits */
397 if (opc == MMC_CMD_SEND_OP_COND)
398 tmp |= CMD_SET_CRC7C_BITS;
399 /* RCRC7C[1:0] internal CRC7 */
400 if (opc == MMC_CMD_ALL_SEND_CID ||
401 opc == MMC_CMD_SEND_CSD || opc == MMC_CMD_SEND_CID)
402 tmp |= CMD_SET_CRC7C_INTERNAL;
404 return opc = ((opc << 24) | tmp);
407 static u32 sh_mmcif_data_trans(struct sh_mmcif_host *host,
408 struct mmc_data *data, u16 opc)
413 case MMC_CMD_READ_MULTIPLE_BLOCK:
414 ret = sh_mmcif_multi_read(host, data);
416 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
417 ret = sh_mmcif_multi_write(host, data);
419 case MMC_CMD_WRITE_SINGLE_BLOCK:
420 ret = sh_mmcif_single_write(host, data);
422 case MMC_CMD_READ_SINGLE_BLOCK:
423 case MMC_CMD_SEND_EXT_CSD:
424 ret = sh_mmcif_single_read(host, data);
427 printf(DRIVER_NAME": NOT SUPPORT CMD = d'%08d\n", opc);
434 static int sh_mmcif_start_cmd(struct sh_mmcif_host *host,
435 struct mmc_data *data, struct mmc_cmd *cmd)
438 int ret = 0, mask = 0;
439 u32 opc = cmd->cmdidx;
441 if (opc == MMC_CMD_STOP_TRANSMISSION) {
442 /* MMCIF sends the STOP command automatically */
443 if (host->last_cmd == MMC_CMD_READ_MULTIPLE_BLOCK)
444 sh_mmcif_bitset(MASK_MCMD12DRE,
445 &host->regs->ce_int_mask);
447 sh_mmcif_bitset(MASK_MCMD12RBE,
448 &host->regs->ce_int_mask);
450 time = mmcif_wait_interrupt_flag(host);
451 if (time == 0 || host->sd_error != 0)
452 return sh_mmcif_error_manage(host);
454 sh_mmcif_get_cmd12response(host, cmd);
457 if (opc == MMC_CMD_SWITCH)
462 mask |= MASK_MCMDVIO | MASK_MBUFVIO | MASK_MWDATERR |
463 MASK_MRDATERR | MASK_MRIDXERR | MASK_MRSPERR |
464 MASK_MCCSTO | MASK_MCRCSTO | MASK_MWDATTO |
465 MASK_MRDATTO | MASK_MRBSYTO | MASK_MRSPTO;
468 sh_mmcif_write(0, &host->regs->ce_block_set);
469 sh_mmcif_write(data->blocksize, &host->regs->ce_block_set);
471 opc = sh_mmcif_set_cmd(host, data, cmd);
473 sh_mmcif_write(INT_START_MAGIC, &host->regs->ce_int);
474 sh_mmcif_write(mask, &host->regs->ce_int_mask);
476 debug("CMD%d ARG:%08x\n", cmd->cmdidx, cmd->cmdarg);
478 sh_mmcif_write(cmd->cmdarg, &host->regs->ce_arg);
481 sh_mmcif_write(opc, &host->regs->ce_cmd_set);
483 time = mmcif_wait_interrupt_flag(host);
485 return sh_mmcif_error_manage(host);
487 if (host->sd_error) {
488 switch (cmd->cmdidx) {
489 case MMC_CMD_ALL_SEND_CID:
490 case MMC_CMD_SELECT_CARD:
491 case MMC_CMD_APP_CMD:
495 printf(DRIVER_NAME": Cmd(d'%d) err\n", cmd->cmdidx);
496 ret = sh_mmcif_error_manage(host);
505 if (!(opc & 0x00C00000))
508 if (host->wait_int == 1) {
509 sh_mmcif_get_response(host, cmd);
513 ret = sh_mmcif_data_trans(host, data, cmd->cmdidx);
514 host->last_cmd = cmd->cmdidx;
519 static int sh_mmcif_send_cmd_common(struct sh_mmcif_host *host,
520 struct mmc_cmd *cmd, struct mmc_data *data)
526 switch (cmd->cmdidx) {
527 case MMC_CMD_APP_CMD:
529 case MMC_CMD_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */
534 /* send_if_cond cmd (not support) */
541 ret = sh_mmcif_start_cmd(host, data, cmd);
547 static int sh_mmcif_set_ios_common(struct sh_mmcif_host *host, struct mmc *mmc)
550 sh_mmcif_clock_control(host, mmc->clock);
552 if (mmc->bus_width == 8)
553 host->bus_width = MMC_BUS_WIDTH_8;
554 else if (mmc->bus_width == 4)
555 host->bus_width = MMC_BUS_WIDTH_4;
557 host->bus_width = MMC_BUS_WIDTH_1;
559 debug("clock = %d, buswidth = %d\n", mmc->clock, mmc->bus_width);
564 static int sh_mmcif_initialize_common(struct sh_mmcif_host *host)
566 sh_mmcif_sync_reset(host);
567 sh_mmcif_write(MASK_ALL, &host->regs->ce_int_mask);
571 #ifndef CONFIG_DM_MMC
572 static void *mmc_priv(struct mmc *mmc)
574 return (void *)mmc->priv;
577 static int sh_mmcif_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
578 struct mmc_data *data)
580 struct sh_mmcif_host *host = mmc_priv(mmc);
582 return sh_mmcif_send_cmd_common(host, cmd, data);
585 static int sh_mmcif_set_ios(struct mmc *mmc)
587 struct sh_mmcif_host *host = mmc_priv(mmc);
589 return sh_mmcif_set_ios_common(host, mmc);
592 static int sh_mmcif_initialize(struct mmc *mmc)
594 struct sh_mmcif_host *host = mmc_priv(mmc);
596 return sh_mmcif_initialize_common(host);
599 static const struct mmc_ops sh_mmcif_ops = {
600 .send_cmd = sh_mmcif_send_cmd,
601 .set_ios = sh_mmcif_set_ios,
602 .init = sh_mmcif_initialize,
605 static struct mmc_config sh_mmcif_cfg = {
607 .ops = &sh_mmcif_ops,
608 .host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT |
610 .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
611 .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT,
614 int mmcif_mmc_init(void)
617 struct sh_mmcif_host *host = NULL;
619 host = malloc(sizeof(struct sh_mmcif_host));
622 memset(host, 0, sizeof(*host));
624 host->regs = (struct sh_mmcif_regs *)CONFIG_SH_MMCIF_ADDR;
625 host->clk = CONFIG_SH_MMCIF_CLK;
627 sh_mmcif_cfg.f_min = MMC_CLK_DIV_MIN(host->clk);
628 sh_mmcif_cfg.f_max = MMC_CLK_DIV_MAX(host->clk);
630 mmc = mmc_create(&sh_mmcif_cfg, host);
640 struct sh_mmcif_plat {
641 struct mmc_config cfg;
645 int sh_mmcif_dm_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
646 struct mmc_data *data)
648 struct sh_mmcif_host *host = dev_get_priv(dev);
650 return sh_mmcif_send_cmd_common(host, cmd, data);
653 int sh_mmcif_dm_set_ios(struct udevice *dev)
655 struct sh_mmcif_host *host = dev_get_priv(dev);
656 struct mmc *mmc = mmc_get_mmc_dev(dev);
658 return sh_mmcif_set_ios_common(host, mmc);
661 static const struct dm_mmc_ops sh_mmcif_dm_ops = {
662 .send_cmd = sh_mmcif_dm_send_cmd,
663 .set_ios = sh_mmcif_dm_set_ios,
666 static int sh_mmcif_dm_bind(struct udevice *dev)
668 struct sh_mmcif_plat *plat = dev_get_platdata(dev);
670 return mmc_bind(dev, &plat->mmc, &plat->cfg);
673 static int sh_mmcif_dm_probe(struct udevice *dev)
675 struct sh_mmcif_plat *plat = dev_get_platdata(dev);
676 struct sh_mmcif_host *host = dev_get_priv(dev);
677 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
678 struct clk sh_mmcif_clk;
682 base = devfdt_get_addr(dev);
683 if (base == FDT_ADDR_T_NONE)
686 host->regs = (struct sh_mmcif_regs *)devm_ioremap(dev, base, SZ_2K);
690 ret = clk_get_by_index(dev, 0, &sh_mmcif_clk);
692 debug("failed to get clock, ret=%d\n", ret);
696 ret = clk_enable(&sh_mmcif_clk);
698 debug("failed to enable clock, ret=%d\n", ret);
702 host->clk = clk_set_rate(&sh_mmcif_clk, 97500000);
704 plat->cfg.name = dev->name;
705 plat->cfg.host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
707 switch (fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "bus-width",
710 plat->cfg.host_caps |= MMC_MODE_8BIT;
713 plat->cfg.host_caps |= MMC_MODE_4BIT;
718 dev_err(dev, "Invalid \"bus-width\" value\n");
722 sh_mmcif_initialize_common(host);
724 plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
725 plat->cfg.f_min = MMC_CLK_DIV_MIN(host->clk);
726 plat->cfg.f_max = MMC_CLK_DIV_MAX(host->clk);
727 plat->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
729 upriv->mmc = &plat->mmc;
734 static const struct udevice_id sh_mmcif_sd_match[] = {
735 { .compatible = "renesas,sh-mmcif" },
739 U_BOOT_DRIVER(sh_mmcif_mmc) = {
742 .of_match = sh_mmcif_sd_match,
743 .bind = sh_mmcif_dm_bind,
744 .probe = sh_mmcif_dm_probe,
745 .priv_auto_alloc_size = sizeof(struct sh_mmcif_host),
746 .platdata_auto_alloc_size = sizeof(struct sh_mmcif_plat),
747 .ops = &sh_mmcif_dm_ops,