1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2011 Renesas Solutions Corp.
17 #include <dm/device_compat.h>
18 #include <linux/bitops.h>
19 #include <linux/delay.h>
20 #include <linux/errno.h>
21 #include <linux/compat.h>
23 #include <linux/sizes.h>
25 #include <asm/global_data.h>
27 #define DRIVER_NAME "sh_mmcif"
29 static int sh_mmcif_intr(void *dev_id)
31 struct sh_mmcif_host *host = dev_id;
34 state = sh_mmcif_read(&host->regs->ce_int);
35 state &= sh_mmcif_read(&host->regs->ce_int_mask);
37 if (state & INT_RBSYE) {
38 sh_mmcif_write(~(INT_RBSYE | INT_CRSPE), &host->regs->ce_int);
39 sh_mmcif_bitclr(MASK_MRBSYE, &host->regs->ce_int_mask);
41 } else if (state & INT_CRSPE) {
42 sh_mmcif_write(~INT_CRSPE, &host->regs->ce_int);
43 sh_mmcif_bitclr(MASK_MCRSPE, &host->regs->ce_int_mask);
44 /* one more interrupt (INT_RBSYE) */
45 if (sh_mmcif_read(&host->regs->ce_cmd_set) & CMD_SET_RBSY)
48 } else if (state & INT_BUFREN) {
49 sh_mmcif_write(~INT_BUFREN, &host->regs->ce_int);
50 sh_mmcif_bitclr(MASK_MBUFREN, &host->regs->ce_int_mask);
52 } else if (state & INT_BUFWEN) {
53 sh_mmcif_write(~INT_BUFWEN, &host->regs->ce_int);
54 sh_mmcif_bitclr(MASK_MBUFWEN, &host->regs->ce_int_mask);
56 } else if (state & INT_CMD12DRE) {
57 sh_mmcif_write(~(INT_CMD12DRE | INT_CMD12RBE | INT_CMD12CRE |
58 INT_BUFRE), &host->regs->ce_int);
59 sh_mmcif_bitclr(MASK_MCMD12DRE, &host->regs->ce_int_mask);
61 } else if (state & INT_BUFRE) {
62 sh_mmcif_write(~INT_BUFRE, &host->regs->ce_int);
63 sh_mmcif_bitclr(MASK_MBUFRE, &host->regs->ce_int_mask);
65 } else if (state & INT_DTRANE) {
66 sh_mmcif_write(~INT_DTRANE, &host->regs->ce_int);
67 sh_mmcif_bitclr(MASK_MDTRANE, &host->regs->ce_int_mask);
69 } else if (state & INT_CMD12RBE) {
70 sh_mmcif_write(~(INT_CMD12RBE | INT_CMD12CRE),
72 sh_mmcif_bitclr(MASK_MCMD12RBE, &host->regs->ce_int_mask);
74 } else if (state & INT_ERR_STS) {
76 sh_mmcif_write(~state, &host->regs->ce_int);
77 sh_mmcif_bitclr(state, &host->regs->ce_int_mask);
84 debug("%s: int err state = %08x\n", DRIVER_NAME, state);
90 static int mmcif_wait_interrupt_flag(struct sh_mmcif_host *host)
92 int timeout = 10000000;
101 if (!sh_mmcif_intr(host))
104 udelay(1); /* 1 usec */
107 return 1; /* Return value: NOT 0 = complete waiting */
110 static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
112 sh_mmcif_bitclr(CLK_ENABLE, &host->regs->ce_clk_ctrl);
113 sh_mmcif_bitclr(CLK_CLEAR, &host->regs->ce_clk_ctrl);
118 if (clk == CLKDEV_EMMC_DATA)
119 sh_mmcif_bitset(CLK_PCLK, &host->regs->ce_clk_ctrl);
121 sh_mmcif_bitset((fls(DIV_ROUND_UP(host->clk,
122 clk) - 1) - 1) << 16,
123 &host->regs->ce_clk_ctrl);
124 sh_mmcif_bitset(CLK_ENABLE, &host->regs->ce_clk_ctrl);
127 static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
131 tmp = sh_mmcif_read(&host->regs->ce_clk_ctrl) & (CLK_ENABLE |
134 sh_mmcif_write(SOFT_RST_ON, &host->regs->ce_version);
135 sh_mmcif_write(SOFT_RST_OFF, &host->regs->ce_version);
136 sh_mmcif_bitset(tmp | SRSPTO_256 | SRBSYTO_29 | SRWDTO_29 | SCCSTO_29,
137 &host->regs->ce_clk_ctrl);
139 sh_mmcif_bitset(BUF_ACC_ATYP, &host->regs->ce_buf_acc);
142 static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
145 int ret, timeout = 10000000;
150 state1 = sh_mmcif_read(&host->regs->ce_host_sts1);
151 state2 = sh_mmcif_read(&host->regs->ce_host_sts2);
152 debug("%s: ERR HOST_STS1 = %08x\n", \
153 DRIVER_NAME, sh_mmcif_read(&host->regs->ce_host_sts1));
154 debug("%s: ERR HOST_STS2 = %08x\n", \
155 DRIVER_NAME, sh_mmcif_read(&host->regs->ce_host_sts2));
157 if (state1 & STS1_CMDSEQ) {
158 debug("%s: Forced end of command sequence\n", DRIVER_NAME);
159 sh_mmcif_bitset(CMD_CTRL_BREAK, &host->regs->ce_cmd_ctrl);
160 sh_mmcif_bitset(~CMD_CTRL_BREAK, &host->regs->ce_cmd_ctrl);
164 printf(DRIVER_NAME": Forceed end of " \
165 "command sequence timeout err\n");
168 if (!(sh_mmcif_read(&host->regs->ce_host_sts1)
172 sh_mmcif_sync_reset(host);
176 if (state2 & STS2_CRC_ERR)
178 else if (state2 & STS2_TIMEOUT_ERR)
185 static int sh_mmcif_single_read(struct sh_mmcif_host *host,
186 struct mmc_data *data)
190 unsigned long *p = (unsigned long *)data->dest;
192 if ((unsigned long)p & 0x00000001) {
193 printf("%s: The data pointer is unaligned.", __func__);
199 /* buf read enable */
200 sh_mmcif_bitset(MASK_MBUFREN, &host->regs->ce_int_mask);
201 time = mmcif_wait_interrupt_flag(host);
202 if (time == 0 || host->sd_error != 0)
203 return sh_mmcif_error_manage(host);
206 blocksize = (BLOCK_SIZE_MASK &
207 sh_mmcif_read(&host->regs->ce_block_set)) + 3;
208 for (i = 0; i < blocksize / 4; i++)
209 *p++ = sh_mmcif_read(&host->regs->ce_data);
211 /* buffer read end */
212 sh_mmcif_bitset(MASK_MBUFRE, &host->regs->ce_int_mask);
213 time = mmcif_wait_interrupt_flag(host);
214 if (time == 0 || host->sd_error != 0)
215 return sh_mmcif_error_manage(host);
221 static int sh_mmcif_multi_read(struct sh_mmcif_host *host,
222 struct mmc_data *data)
226 unsigned long *p = (unsigned long *)data->dest;
228 if ((unsigned long)p & 0x00000001) {
229 printf("%s: The data pointer is unaligned.", __func__);
234 blocksize = BLOCK_SIZE_MASK & sh_mmcif_read(&host->regs->ce_block_set);
235 for (j = 0; j < data->blocks; j++) {
236 sh_mmcif_bitset(MASK_MBUFREN, &host->regs->ce_int_mask);
237 time = mmcif_wait_interrupt_flag(host);
238 if (time == 0 || host->sd_error != 0)
239 return sh_mmcif_error_manage(host);
242 for (i = 0; i < blocksize / 4; i++)
243 *p++ = sh_mmcif_read(&host->regs->ce_data);
250 static int sh_mmcif_single_write(struct sh_mmcif_host *host,
251 struct mmc_data *data)
255 const unsigned long *p = (unsigned long *)data->dest;
257 if ((unsigned long)p & 0x00000001) {
258 printf("%s: The data pointer is unaligned.", __func__);
263 sh_mmcif_bitset(MASK_MBUFWEN, &host->regs->ce_int_mask);
265 time = mmcif_wait_interrupt_flag(host);
266 if (time == 0 || host->sd_error != 0)
267 return sh_mmcif_error_manage(host);
270 blocksize = (BLOCK_SIZE_MASK &
271 sh_mmcif_read(&host->regs->ce_block_set)) + 3;
272 for (i = 0; i < blocksize / 4; i++)
273 sh_mmcif_write(*p++, &host->regs->ce_data);
275 /* buffer write end */
276 sh_mmcif_bitset(MASK_MDTRANE, &host->regs->ce_int_mask);
278 time = mmcif_wait_interrupt_flag(host);
279 if (time == 0 || host->sd_error != 0)
280 return sh_mmcif_error_manage(host);
286 static int sh_mmcif_multi_write(struct sh_mmcif_host *host,
287 struct mmc_data *data)
291 const unsigned long *p = (unsigned long *)data->dest;
293 if ((unsigned long)p & 0x00000001) {
294 printf("%s: The data pointer is unaligned.", __func__);
299 blocksize = BLOCK_SIZE_MASK & sh_mmcif_read(&host->regs->ce_block_set);
300 for (j = 0; j < data->blocks; j++) {
301 sh_mmcif_bitset(MASK_MBUFWEN, &host->regs->ce_int_mask);
303 time = mmcif_wait_interrupt_flag(host);
305 if (time == 0 || host->sd_error != 0)
306 return sh_mmcif_error_manage(host);
309 for (i = 0; i < blocksize / 4; i++)
310 sh_mmcif_write(*p++, &host->regs->ce_data);
317 static void sh_mmcif_get_response(struct sh_mmcif_host *host,
320 if (cmd->resp_type & MMC_RSP_136) {
321 cmd->response[0] = sh_mmcif_read(&host->regs->ce_resp3);
322 cmd->response[1] = sh_mmcif_read(&host->regs->ce_resp2);
323 cmd->response[2] = sh_mmcif_read(&host->regs->ce_resp1);
324 cmd->response[3] = sh_mmcif_read(&host->regs->ce_resp0);
325 debug(" RESP %08x, %08x, %08x, %08x\n", cmd->response[0],
326 cmd->response[1], cmd->response[2], cmd->response[3]);
328 cmd->response[0] = sh_mmcif_read(&host->regs->ce_resp0);
332 static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
335 cmd->response[0] = sh_mmcif_read(&host->regs->ce_resp_cmd12);
338 static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
339 struct mmc_data *data, struct mmc_cmd *cmd)
342 u32 opc = cmd->cmdidx;
344 /* Response Type check */
345 switch (cmd->resp_type) {
347 tmp |= CMD_SET_RTYP_NO;
352 tmp |= CMD_SET_RTYP_6B;
355 tmp |= CMD_SET_RTYP_17B;
358 printf(DRIVER_NAME": Not support type response.\n");
363 if (opc == MMC_CMD_SWITCH)
369 switch (host->bus_width) {
370 case MMC_BUS_WIDTH_1:
371 tmp |= CMD_SET_DATW_1;
373 case MMC_BUS_WIDTH_4:
374 tmp |= CMD_SET_DATW_4;
376 case MMC_BUS_WIDTH_8:
377 tmp |= CMD_SET_DATW_8;
380 printf(DRIVER_NAME": Not support bus width.\n");
385 if (opc == MMC_CMD_WRITE_SINGLE_BLOCK ||
386 opc == MMC_CMD_WRITE_MULTIPLE_BLOCK)
389 if (opc == MMC_CMD_READ_MULTIPLE_BLOCK ||
390 opc == MMC_CMD_WRITE_MULTIPLE_BLOCK) {
391 tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN;
392 sh_mmcif_bitset(data->blocks << 16, &host->regs->ce_block_set);
394 /* RIDXC[1:0] check bits */
395 if (opc == MMC_CMD_SEND_OP_COND || opc == MMC_CMD_ALL_SEND_CID ||
396 opc == MMC_CMD_SEND_CSD || opc == MMC_CMD_SEND_CID)
397 tmp |= CMD_SET_RIDXC_BITS;
398 /* RCRC7C[1:0] check bits */
399 if (opc == MMC_CMD_SEND_OP_COND)
400 tmp |= CMD_SET_CRC7C_BITS;
401 /* RCRC7C[1:0] internal CRC7 */
402 if (opc == MMC_CMD_ALL_SEND_CID ||
403 opc == MMC_CMD_SEND_CSD || opc == MMC_CMD_SEND_CID)
404 tmp |= CMD_SET_CRC7C_INTERNAL;
406 return opc = ((opc << 24) | tmp);
409 static u32 sh_mmcif_data_trans(struct sh_mmcif_host *host,
410 struct mmc_data *data, u16 opc)
415 case MMC_CMD_READ_MULTIPLE_BLOCK:
416 ret = sh_mmcif_multi_read(host, data);
418 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
419 ret = sh_mmcif_multi_write(host, data);
421 case MMC_CMD_WRITE_SINGLE_BLOCK:
422 ret = sh_mmcif_single_write(host, data);
424 case MMC_CMD_READ_SINGLE_BLOCK:
425 case MMC_CMD_SEND_EXT_CSD:
426 ret = sh_mmcif_single_read(host, data);
429 printf(DRIVER_NAME": NOT SUPPORT CMD = d'%08d\n", opc);
436 static int sh_mmcif_start_cmd(struct sh_mmcif_host *host,
437 struct mmc_data *data, struct mmc_cmd *cmd)
440 int ret = 0, mask = 0;
441 u32 opc = cmd->cmdidx;
443 if (opc == MMC_CMD_STOP_TRANSMISSION) {
444 /* MMCIF sends the STOP command automatically */
445 if (host->last_cmd == MMC_CMD_READ_MULTIPLE_BLOCK)
446 sh_mmcif_bitset(MASK_MCMD12DRE,
447 &host->regs->ce_int_mask);
449 sh_mmcif_bitset(MASK_MCMD12RBE,
450 &host->regs->ce_int_mask);
452 time = mmcif_wait_interrupt_flag(host);
453 if (time == 0 || host->sd_error != 0)
454 return sh_mmcif_error_manage(host);
456 sh_mmcif_get_cmd12response(host, cmd);
459 if (opc == MMC_CMD_SWITCH)
464 mask |= MASK_MCMDVIO | MASK_MBUFVIO | MASK_MWDATERR |
465 MASK_MRDATERR | MASK_MRIDXERR | MASK_MRSPERR |
466 MASK_MCCSTO | MASK_MCRCSTO | MASK_MWDATTO |
467 MASK_MRDATTO | MASK_MRBSYTO | MASK_MRSPTO;
470 sh_mmcif_write(0, &host->regs->ce_block_set);
471 sh_mmcif_write(data->blocksize, &host->regs->ce_block_set);
473 opc = sh_mmcif_set_cmd(host, data, cmd);
475 sh_mmcif_write(INT_START_MAGIC, &host->regs->ce_int);
476 sh_mmcif_write(mask, &host->regs->ce_int_mask);
478 debug("CMD%d ARG:%08x\n", cmd->cmdidx, cmd->cmdarg);
480 sh_mmcif_write(cmd->cmdarg, &host->regs->ce_arg);
483 sh_mmcif_write(opc, &host->regs->ce_cmd_set);
485 time = mmcif_wait_interrupt_flag(host);
487 return sh_mmcif_error_manage(host);
489 if (host->sd_error) {
490 switch (cmd->cmdidx) {
491 case MMC_CMD_ALL_SEND_CID:
492 case MMC_CMD_SELECT_CARD:
493 case MMC_CMD_APP_CMD:
497 printf(DRIVER_NAME": Cmd(d'%d) err\n", cmd->cmdidx);
498 ret = sh_mmcif_error_manage(host);
507 if (!(opc & 0x00C00000))
510 if (host->wait_int == 1) {
511 sh_mmcif_get_response(host, cmd);
515 ret = sh_mmcif_data_trans(host, data, cmd->cmdidx);
516 host->last_cmd = cmd->cmdidx;
521 static int sh_mmcif_send_cmd_common(struct sh_mmcif_host *host,
522 struct mmc_cmd *cmd, struct mmc_data *data)
528 switch (cmd->cmdidx) {
529 case MMC_CMD_APP_CMD:
531 case MMC_CMD_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */
536 /* send_if_cond cmd (not support) */
543 ret = sh_mmcif_start_cmd(host, data, cmd);
549 static int sh_mmcif_set_ios_common(struct sh_mmcif_host *host, struct mmc *mmc)
552 sh_mmcif_clock_control(host, mmc->clock);
554 if (mmc->bus_width == 8)
555 host->bus_width = MMC_BUS_WIDTH_8;
556 else if (mmc->bus_width == 4)
557 host->bus_width = MMC_BUS_WIDTH_4;
559 host->bus_width = MMC_BUS_WIDTH_1;
561 debug("clock = %d, buswidth = %d\n", mmc->clock, mmc->bus_width);
566 static int sh_mmcif_initialize_common(struct sh_mmcif_host *host)
568 sh_mmcif_sync_reset(host);
569 sh_mmcif_write(MASK_ALL, &host->regs->ce_int_mask);
573 #ifndef CONFIG_DM_MMC
574 static void *mmc_priv(struct mmc *mmc)
576 return (void *)mmc->priv;
579 static int sh_mmcif_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
580 struct mmc_data *data)
582 struct sh_mmcif_host *host = mmc_priv(mmc);
584 return sh_mmcif_send_cmd_common(host, cmd, data);
587 static int sh_mmcif_set_ios(struct mmc *mmc)
589 struct sh_mmcif_host *host = mmc_priv(mmc);
591 return sh_mmcif_set_ios_common(host, mmc);
594 static int sh_mmcif_initialize(struct mmc *mmc)
596 struct sh_mmcif_host *host = mmc_priv(mmc);
598 return sh_mmcif_initialize_common(host);
601 static const struct mmc_ops sh_mmcif_ops = {
602 .send_cmd = sh_mmcif_send_cmd,
603 .set_ios = sh_mmcif_set_ios,
604 .init = sh_mmcif_initialize,
607 static struct mmc_config sh_mmcif_cfg = {
609 .ops = &sh_mmcif_ops,
610 .host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT |
612 .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
613 .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT,
616 int mmcif_mmc_init(void)
619 struct sh_mmcif_host *host = NULL;
621 host = malloc(sizeof(struct sh_mmcif_host));
624 memset(host, 0, sizeof(*host));
626 host->regs = (struct sh_mmcif_regs *)CONFIG_SH_MMCIF_ADDR;
627 host->clk = CONFIG_SH_MMCIF_CLK;
629 sh_mmcif_cfg.f_min = MMC_CLK_DIV_MIN(host->clk);
630 sh_mmcif_cfg.f_max = MMC_CLK_DIV_MAX(host->clk);
632 mmc = mmc_create(&sh_mmcif_cfg, host);
642 struct sh_mmcif_plat {
643 struct mmc_config cfg;
647 int sh_mmcif_dm_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
648 struct mmc_data *data)
650 struct sh_mmcif_host *host = dev_get_priv(dev);
652 return sh_mmcif_send_cmd_common(host, cmd, data);
655 int sh_mmcif_dm_set_ios(struct udevice *dev)
657 struct sh_mmcif_host *host = dev_get_priv(dev);
658 struct mmc *mmc = mmc_get_mmc_dev(dev);
660 return sh_mmcif_set_ios_common(host, mmc);
663 static const struct dm_mmc_ops sh_mmcif_dm_ops = {
664 .send_cmd = sh_mmcif_dm_send_cmd,
665 .set_ios = sh_mmcif_dm_set_ios,
668 static int sh_mmcif_dm_bind(struct udevice *dev)
670 struct sh_mmcif_plat *plat = dev_get_plat(dev);
672 return mmc_bind(dev, &plat->mmc, &plat->cfg);
675 static int sh_mmcif_dm_probe(struct udevice *dev)
677 struct sh_mmcif_plat *plat = dev_get_plat(dev);
678 struct sh_mmcif_host *host = dev_get_priv(dev);
679 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
680 struct clk sh_mmcif_clk;
684 base = dev_read_addr(dev);
685 if (base == FDT_ADDR_T_NONE)
688 host->regs = (struct sh_mmcif_regs *)devm_ioremap(dev, base, SZ_2K);
692 ret = clk_get_by_index(dev, 0, &sh_mmcif_clk);
694 debug("failed to get clock, ret=%d\n", ret);
698 ret = clk_enable(&sh_mmcif_clk);
700 debug("failed to enable clock, ret=%d\n", ret);
704 host->clk = clk_set_rate(&sh_mmcif_clk, 97500000);
706 plat->cfg.name = dev->name;
707 plat->cfg.host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
709 switch (fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "bus-width",
712 plat->cfg.host_caps |= MMC_MODE_8BIT;
715 plat->cfg.host_caps |= MMC_MODE_4BIT;
720 dev_err(dev, "Invalid \"bus-width\" value\n");
724 sh_mmcif_initialize_common(host);
726 plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
727 plat->cfg.f_min = MMC_CLK_DIV_MIN(host->clk);
728 plat->cfg.f_max = MMC_CLK_DIV_MAX(host->clk);
729 plat->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
731 upriv->mmc = &plat->mmc;
736 static const struct udevice_id sh_mmcif_sd_match[] = {
737 { .compatible = "renesas,sh-mmcif" },
741 U_BOOT_DRIVER(sh_mmcif_mmc) = {
744 .of_match = sh_mmcif_sd_match,
745 .bind = sh_mmcif_dm_bind,
746 .probe = sh_mmcif_dm_probe,
747 .priv_auto = sizeof(struct sh_mmcif_host),
748 .plat_auto = sizeof(struct sh_mmcif_plat),
749 .ops = &sh_mmcif_dm_ops,