2 * drivers/mmc/sh_sdhi.c
4 * SD/MMC driver for Renesas rmobile ARM SoCs.
6 * Copyright (C) 2011,2013-2017 Renesas Electronics Corporation
7 * Copyright (C) 2014 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
8 * Copyright (C) 2008-2009 Renesas Solutions Corp.
10 * SPDX-License-Identifier: GPL-2.0
17 #include <linux/errno.h>
18 #include <linux/compat.h>
20 #include <linux/sizes.h>
21 #include <asm/arch/rmobile.h>
22 #include <asm/arch/sh_sdhi.h>
24 #define DRIVER_NAME "sh-sdhi"
31 unsigned char wait_int;
32 unsigned char sd_error;
33 unsigned char detect_waiting;
34 unsigned char app_cmd;
37 static inline void sh_sdhi_writeq(struct sh_sdhi_host *host, int reg, u64 val)
39 writeq(val, host->addr + (reg << host->bus_shift));
42 static inline u64 sh_sdhi_readq(struct sh_sdhi_host *host, int reg)
44 return readq(host->addr + (reg << host->bus_shift));
47 static inline void sh_sdhi_writew(struct sh_sdhi_host *host, int reg, u16 val)
49 writew(val, host->addr + (reg << host->bus_shift));
52 static inline u16 sh_sdhi_readw(struct sh_sdhi_host *host, int reg)
54 return readw(host->addr + (reg << host->bus_shift));
57 static void sh_sdhi_detect(struct sh_sdhi_host *host)
59 sh_sdhi_writew(host, SDHI_OPTION,
60 OPT_BUS_WIDTH_1 | sh_sdhi_readw(host, SDHI_OPTION));
62 host->detect_waiting = 0;
65 static int sh_sdhi_intr(void *dev_id)
67 struct sh_sdhi_host *host = dev_id;
68 int state1 = 0, state2 = 0;
70 state1 = sh_sdhi_readw(host, SDHI_INFO1);
71 state2 = sh_sdhi_readw(host, SDHI_INFO2);
73 debug("%s: state1 = %x, state2 = %x\n", __func__, state1, state2);
76 if (state1 & INFO1_CARD_IN) {
77 sh_sdhi_writew(host, SDHI_INFO1, ~INFO1_CARD_IN);
78 if (!host->detect_waiting) {
79 host->detect_waiting = 1;
82 sh_sdhi_writew(host, SDHI_INFO1_MASK, INFO1M_RESP_END |
83 INFO1M_ACCESS_END | INFO1M_CARD_IN |
84 INFO1M_DATA3_CARD_RE | INFO1M_DATA3_CARD_IN);
88 if (state1 & INFO1_CARD_RE) {
89 sh_sdhi_writew(host, SDHI_INFO1, ~INFO1_CARD_RE);
90 if (!host->detect_waiting) {
91 host->detect_waiting = 1;
94 sh_sdhi_writew(host, SDHI_INFO1_MASK, INFO1M_RESP_END |
95 INFO1M_ACCESS_END | INFO1M_CARD_RE |
96 INFO1M_DATA3_CARD_RE | INFO1M_DATA3_CARD_IN);
97 sh_sdhi_writew(host, SDHI_SDIO_INFO1_MASK, SDIO_INFO1M_ON);
98 sh_sdhi_writew(host, SDHI_SDIO_MODE, SDIO_MODE_OFF);
102 if (state2 & INFO2_ALL_ERR) {
103 sh_sdhi_writew(host, SDHI_INFO2,
104 (unsigned short)~(INFO2_ALL_ERR));
105 sh_sdhi_writew(host, SDHI_INFO2_MASK,
107 sh_sdhi_readw(host, SDHI_INFO2_MASK));
113 if (state1 & INFO1_RESP_END) {
114 sh_sdhi_writew(host, SDHI_INFO1, ~INFO1_RESP_END);
115 sh_sdhi_writew(host, SDHI_INFO1_MASK,
117 sh_sdhi_readw(host, SDHI_INFO1_MASK));
121 /* SD_BUF Read Enable */
122 if (state2 & INFO2_BRE_ENABLE) {
123 sh_sdhi_writew(host, SDHI_INFO2, ~INFO2_BRE_ENABLE);
124 sh_sdhi_writew(host, SDHI_INFO2_MASK,
125 INFO2M_BRE_ENABLE | INFO2M_BUF_ILL_READ |
126 sh_sdhi_readw(host, SDHI_INFO2_MASK));
130 /* SD_BUF Write Enable */
131 if (state2 & INFO2_BWE_ENABLE) {
132 sh_sdhi_writew(host, SDHI_INFO2, ~INFO2_BWE_ENABLE);
133 sh_sdhi_writew(host, SDHI_INFO2_MASK,
134 INFO2_BWE_ENABLE | INFO2M_BUF_ILL_WRITE |
135 sh_sdhi_readw(host, SDHI_INFO2_MASK));
140 if (state1 & INFO1_ACCESS_END) {
141 sh_sdhi_writew(host, SDHI_INFO1, ~INFO1_ACCESS_END);
142 sh_sdhi_writew(host, SDHI_INFO1_MASK,
144 sh_sdhi_readw(host, SDHI_INFO1_MASK));
151 static int sh_sdhi_wait_interrupt_flag(struct sh_sdhi_host *host)
153 int timeout = 10000000;
158 debug(DRIVER_NAME": %s timeout\n", __func__);
162 if (!sh_sdhi_intr(host))
165 udelay(1); /* 1 usec */
168 return 1; /* Return value: NOT 0 = complete waiting */
171 static int sh_sdhi_clock_control(struct sh_sdhi_host *host, unsigned long clk)
173 u32 clkdiv, i, timeout;
175 if (sh_sdhi_readw(host, SDHI_INFO2) & (1 << 14)) {
176 printf(DRIVER_NAME": Busy state ! Cannot change the clock\n");
180 sh_sdhi_writew(host, SDHI_CLK_CTRL,
181 ~CLK_ENABLE & sh_sdhi_readw(host, SDHI_CLK_CTRL));
187 i = CONFIG_SH_SDHI_FREQ >> (0x8 + 1);
188 for (; clkdiv && clk >= (i << 1); (clkdiv >>= 1))
191 sh_sdhi_writew(host, SDHI_CLK_CTRL, clkdiv);
194 /* Waiting for SD Bus busy to be cleared */
196 if ((sh_sdhi_readw(host, SDHI_INFO2) & 0x2000))
201 sh_sdhi_writew(host, SDHI_CLK_CTRL,
202 CLK_ENABLE | sh_sdhi_readw(host, SDHI_CLK_CTRL));
209 static int sh_sdhi_sync_reset(struct sh_sdhi_host *host)
212 sh_sdhi_writew(host, SDHI_SOFT_RST, SOFT_RST_ON);
213 sh_sdhi_writew(host, SDHI_SOFT_RST, SOFT_RST_OFF);
214 sh_sdhi_writew(host, SDHI_CLK_CTRL,
215 CLK_ENABLE | sh_sdhi_readw(host, SDHI_CLK_CTRL));
219 if (!(sh_sdhi_readw(host, SDHI_INFO2) & INFO2_CBUSY))
227 if (host->quirks & SH_SDHI_QUIRK_16BIT_BUF)
228 sh_sdhi_writew(host, SDHI_HOST_MODE, 1);
233 static int sh_sdhi_error_manage(struct sh_sdhi_host *host)
235 unsigned short e_state1, e_state2;
241 e_state1 = sh_sdhi_readw(host, SDHI_ERR_STS1);
242 e_state2 = sh_sdhi_readw(host, SDHI_ERR_STS2);
243 if (e_state2 & ERR_STS2_SYS_ERROR) {
244 if (e_state2 & ERR_STS2_RES_STOP_TIMEOUT)
248 debug("%s: ERR_STS2 = %04x\n",
249 DRIVER_NAME, sh_sdhi_readw(host, SDHI_ERR_STS2));
250 sh_sdhi_sync_reset(host);
252 sh_sdhi_writew(host, SDHI_INFO1_MASK,
253 INFO1M_DATA3_CARD_RE | INFO1M_DATA3_CARD_IN);
256 if (e_state1 & ERR_STS1_CRC_ERROR || e_state1 & ERR_STS1_CMD_ERROR)
261 debug("%s: ERR_STS1 = %04x\n",
262 DRIVER_NAME, sh_sdhi_readw(host, SDHI_ERR_STS1));
263 sh_sdhi_sync_reset(host);
264 sh_sdhi_writew(host, SDHI_INFO1_MASK,
265 INFO1M_DATA3_CARD_RE | INFO1M_DATA3_CARD_IN);
269 static int sh_sdhi_single_read(struct sh_sdhi_host *host, struct mmc_data *data)
272 unsigned short blocksize, i;
273 unsigned short *p = (unsigned short *)data->dest;
274 u64 *q = (u64 *)data->dest;
276 if ((unsigned long)p & 0x00000001) {
277 debug(DRIVER_NAME": %s: The data pointer is unaligned.",
283 sh_sdhi_writew(host, SDHI_INFO2_MASK,
284 ~(INFO2M_BRE_ENABLE | INFO2M_BUF_ILL_READ) &
285 sh_sdhi_readw(host, SDHI_INFO2_MASK));
286 sh_sdhi_writew(host, SDHI_INFO1_MASK,
288 sh_sdhi_readw(host, SDHI_INFO1_MASK));
289 time = sh_sdhi_wait_interrupt_flag(host);
290 if (time == 0 || host->sd_error != 0)
291 return sh_sdhi_error_manage(host);
294 blocksize = sh_sdhi_readw(host, SDHI_SIZE);
295 if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF)
296 for (i = 0; i < blocksize / 8; i++)
297 *q++ = sh_sdhi_readq(host, SDHI_BUF0);
299 for (i = 0; i < blocksize / 2; i++)
300 *p++ = sh_sdhi_readw(host, SDHI_BUF0);
302 time = sh_sdhi_wait_interrupt_flag(host);
303 if (time == 0 || host->sd_error != 0)
304 return sh_sdhi_error_manage(host);
310 static int sh_sdhi_multi_read(struct sh_sdhi_host *host, struct mmc_data *data)
313 unsigned short blocksize, i, sec;
314 unsigned short *p = (unsigned short *)data->dest;
315 u64 *q = (u64 *)data->dest;
317 if ((unsigned long)p & 0x00000001) {
318 debug(DRIVER_NAME": %s: The data pointer is unaligned.",
323 debug("%s: blocks = %d, blocksize = %d\n",
324 __func__, data->blocks, data->blocksize);
327 for (sec = 0; sec < data->blocks; sec++) {
328 sh_sdhi_writew(host, SDHI_INFO2_MASK,
329 ~(INFO2M_BRE_ENABLE | INFO2M_BUF_ILL_READ) &
330 sh_sdhi_readw(host, SDHI_INFO2_MASK));
332 time = sh_sdhi_wait_interrupt_flag(host);
333 if (time == 0 || host->sd_error != 0)
334 return sh_sdhi_error_manage(host);
337 blocksize = sh_sdhi_readw(host, SDHI_SIZE);
338 if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF)
339 for (i = 0; i < blocksize / 8; i++)
340 *q++ = sh_sdhi_readq(host, SDHI_BUF0);
342 for (i = 0; i < blocksize / 2; i++)
343 *p++ = sh_sdhi_readw(host, SDHI_BUF0);
349 static int sh_sdhi_single_write(struct sh_sdhi_host *host,
350 struct mmc_data *data)
353 unsigned short blocksize, i;
354 const unsigned short *p = (const unsigned short *)data->src;
355 const u64 *q = (const u64 *)data->src;
357 if ((unsigned long)p & 0x00000001) {
358 debug(DRIVER_NAME": %s: The data pointer is unaligned.",
363 debug("%s: blocks = %d, blocksize = %d\n",
364 __func__, data->blocks, data->blocksize);
367 sh_sdhi_writew(host, SDHI_INFO2_MASK,
368 ~(INFO2M_BWE_ENABLE | INFO2M_BUF_ILL_WRITE) &
369 sh_sdhi_readw(host, SDHI_INFO2_MASK));
370 sh_sdhi_writew(host, SDHI_INFO1_MASK,
372 sh_sdhi_readw(host, SDHI_INFO1_MASK));
374 time = sh_sdhi_wait_interrupt_flag(host);
375 if (time == 0 || host->sd_error != 0)
376 return sh_sdhi_error_manage(host);
379 blocksize = sh_sdhi_readw(host, SDHI_SIZE);
380 if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF)
381 for (i = 0; i < blocksize / 8; i++)
382 sh_sdhi_writeq(host, SDHI_BUF0, *q++);
384 for (i = 0; i < blocksize / 2; i++)
385 sh_sdhi_writew(host, SDHI_BUF0, *p++);
387 time = sh_sdhi_wait_interrupt_flag(host);
388 if (time == 0 || host->sd_error != 0)
389 return sh_sdhi_error_manage(host);
395 static int sh_sdhi_multi_write(struct sh_sdhi_host *host, struct mmc_data *data)
398 unsigned short i, sec, blocksize;
399 const unsigned short *p = (const unsigned short *)data->src;
400 const u64 *q = (const u64 *)data->src;
402 debug("%s: blocks = %d, blocksize = %d\n",
403 __func__, data->blocks, data->blocksize);
406 for (sec = 0; sec < data->blocks; sec++) {
407 sh_sdhi_writew(host, SDHI_INFO2_MASK,
408 ~(INFO2M_BWE_ENABLE | INFO2M_BUF_ILL_WRITE) &
409 sh_sdhi_readw(host, SDHI_INFO2_MASK));
411 time = sh_sdhi_wait_interrupt_flag(host);
412 if (time == 0 || host->sd_error != 0)
413 return sh_sdhi_error_manage(host);
416 blocksize = sh_sdhi_readw(host, SDHI_SIZE);
417 if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF)
418 for (i = 0; i < blocksize / 8; i++)
419 sh_sdhi_writeq(host, SDHI_BUF0, *q++);
421 for (i = 0; i < blocksize / 2; i++)
422 sh_sdhi_writew(host, SDHI_BUF0, *p++);
428 static void sh_sdhi_get_response(struct sh_sdhi_host *host, struct mmc_cmd *cmd)
430 unsigned short i, j, cnt = 1;
431 unsigned short resp[8];
433 if (cmd->resp_type & MMC_RSP_136) {
435 resp[0] = sh_sdhi_readw(host, SDHI_RSP00);
436 resp[1] = sh_sdhi_readw(host, SDHI_RSP01);
437 resp[2] = sh_sdhi_readw(host, SDHI_RSP02);
438 resp[3] = sh_sdhi_readw(host, SDHI_RSP03);
439 resp[4] = sh_sdhi_readw(host, SDHI_RSP04);
440 resp[5] = sh_sdhi_readw(host, SDHI_RSP05);
441 resp[6] = sh_sdhi_readw(host, SDHI_RSP06);
442 resp[7] = sh_sdhi_readw(host, SDHI_RSP07);
444 /* SDHI REGISTER SPECIFICATION */
445 for (i = 7, j = 6; i > 0; i--) {
446 resp[i] = (resp[i] << 8) & 0xff00;
447 resp[i] |= (resp[j--] >> 8) & 0x00ff;
449 resp[0] = (resp[0] << 8) & 0xff00;
451 resp[0] = sh_sdhi_readw(host, SDHI_RSP00);
452 resp[1] = sh_sdhi_readw(host, SDHI_RSP01);
455 #if defined(__BIG_ENDIAN_BITFIELD)
457 cmd->response[0] = (resp[6] << 16) | resp[7];
458 cmd->response[1] = (resp[4] << 16) | resp[5];
459 cmd->response[2] = (resp[2] << 16) | resp[3];
460 cmd->response[3] = (resp[0] << 16) | resp[1];
462 cmd->response[0] = (resp[0] << 16) | resp[1];
466 cmd->response[0] = (resp[7] << 16) | resp[6];
467 cmd->response[1] = (resp[5] << 16) | resp[4];
468 cmd->response[2] = (resp[3] << 16) | resp[2];
469 cmd->response[3] = (resp[1] << 16) | resp[0];
471 cmd->response[0] = (resp[1] << 16) | resp[0];
473 #endif /* __BIG_ENDIAN_BITFIELD */
476 static unsigned short sh_sdhi_set_cmd(struct sh_sdhi_host *host,
477 struct mmc_data *data, unsigned short opc)
487 return opc | (data ? 0x1c00 : 0x40);
488 case MMC_CMD_SEND_EXT_CSD:
489 return opc | (data ? 0x1c00 : 0);
490 case MMC_CMD_SEND_OP_COND:
492 case MMC_CMD_APP_CMD:
499 static unsigned short sh_sdhi_data_trans(struct sh_sdhi_host *host,
500 struct mmc_data *data, unsigned short opc)
505 case SD_CMD_APP_SEND_SCR:
506 case SD_CMD_APP_SD_STATUS:
507 return sh_sdhi_single_read(host, data);
509 printf(DRIVER_NAME": SD: NOT SUPPORT APP CMD = d'%04d\n",
515 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
516 return sh_sdhi_multi_write(host, data);
517 case MMC_CMD_READ_MULTIPLE_BLOCK:
518 return sh_sdhi_multi_read(host, data);
519 case MMC_CMD_WRITE_SINGLE_BLOCK:
520 return sh_sdhi_single_write(host, data);
521 case MMC_CMD_READ_SINGLE_BLOCK:
523 case MMC_CMD_SEND_EXT_CSD:;
524 return sh_sdhi_single_read(host, data);
526 printf(DRIVER_NAME": SD: NOT SUPPORT CMD = d'%04d\n", opc);
532 static int sh_sdhi_start_cmd(struct sh_sdhi_host *host,
533 struct mmc_data *data, struct mmc_cmd *cmd)
536 unsigned short shcmd, opc = cmd->cmdidx;
538 unsigned long timeout;
540 debug("opc = %d, arg = %x, resp_type = %x\n",
541 opc, cmd->cmdarg, cmd->resp_type);
543 if (opc == MMC_CMD_STOP_TRANSMISSION) {
544 /* SDHI sends the STOP command automatically by STOP reg */
545 sh_sdhi_writew(host, SDHI_INFO1_MASK, ~INFO1M_ACCESS_END &
546 sh_sdhi_readw(host, SDHI_INFO1_MASK));
548 time = sh_sdhi_wait_interrupt_flag(host);
549 if (time == 0 || host->sd_error != 0)
550 return sh_sdhi_error_manage(host);
552 sh_sdhi_get_response(host, cmd);
557 if ((opc == MMC_CMD_READ_MULTIPLE_BLOCK) ||
558 opc == MMC_CMD_WRITE_MULTIPLE_BLOCK) {
559 sh_sdhi_writew(host, SDHI_STOP, STOP_SEC_ENABLE);
560 sh_sdhi_writew(host, SDHI_SECCNT, data->blocks);
562 sh_sdhi_writew(host, SDHI_SIZE, data->blocksize);
565 shcmd = sh_sdhi_set_cmd(host, data, opc);
568 * U-Boot cannot use interrupt.
569 * So this flag may not be clear by timing
571 sh_sdhi_writew(host, SDHI_INFO1, ~INFO1_RESP_END);
573 sh_sdhi_writew(host, SDHI_INFO1_MASK,
574 INFO1M_RESP_END | sh_sdhi_readw(host, SDHI_INFO1_MASK));
575 sh_sdhi_writew(host, SDHI_ARG0,
576 (unsigned short)(cmd->cmdarg & ARG0_MASK));
577 sh_sdhi_writew(host, SDHI_ARG1,
578 (unsigned short)((cmd->cmdarg >> 16) & ARG1_MASK));
581 /* Waiting for SD Bus busy to be cleared */
583 if ((sh_sdhi_readw(host, SDHI_INFO2) & 0x2000))
588 sh_sdhi_writew(host, SDHI_INFO1_MASK,
589 ~INFO1M_RESP_END & sh_sdhi_readw(host, SDHI_INFO1_MASK));
590 sh_sdhi_writew(host, SDHI_INFO2_MASK,
591 ~(INFO2M_CMD_ERROR | INFO2M_CRC_ERROR |
592 INFO2M_END_ERROR | INFO2M_TIMEOUT |
593 INFO2M_RESP_TIMEOUT | INFO2M_ILA) &
594 sh_sdhi_readw(host, SDHI_INFO2_MASK));
596 sh_sdhi_writew(host, SDHI_CMD, (unsigned short)(shcmd & CMD_MASK));
597 time = sh_sdhi_wait_interrupt_flag(host);
600 return sh_sdhi_error_manage(host);
603 if (host->sd_error) {
604 switch (cmd->cmdidx) {
605 case MMC_CMD_ALL_SEND_CID:
606 case MMC_CMD_SELECT_CARD:
607 case SD_CMD_SEND_IF_COND:
608 case MMC_CMD_APP_CMD:
612 debug(DRIVER_NAME": Cmd(d'%d) err\n", opc);
613 debug(DRIVER_NAME": cmdidx = %d\n", cmd->cmdidx);
614 ret = sh_sdhi_error_manage(host);
623 if (sh_sdhi_readw(host, SDHI_INFO1) & INFO1_RESP_END) {
628 if (host->wait_int) {
629 sh_sdhi_get_response(host, cmd);
634 ret = sh_sdhi_data_trans(host, data, opc);
636 debug("ret = %d, resp = %08x, %08x, %08x, %08x\n",
637 ret, cmd->response[0], cmd->response[1],
638 cmd->response[2], cmd->response[3]);
642 static int sh_sdhi_send_cmd_common(struct sh_sdhi_host *host,
643 struct mmc_cmd *cmd, struct mmc_data *data)
647 return sh_sdhi_start_cmd(host, data, cmd);
650 static int sh_sdhi_set_ios_common(struct sh_sdhi_host *host, struct mmc *mmc)
654 ret = sh_sdhi_clock_control(host, mmc->clock);
658 if (mmc->bus_width == 8)
659 sh_sdhi_writew(host, SDHI_OPTION,
660 OPT_BUS_WIDTH_8 | (~OPT_BUS_WIDTH_M &
661 sh_sdhi_readw(host, SDHI_OPTION)));
662 else if (mmc->bus_width == 4)
663 sh_sdhi_writew(host, SDHI_OPTION,
664 OPT_BUS_WIDTH_4 | (~OPT_BUS_WIDTH_M &
665 sh_sdhi_readw(host, SDHI_OPTION)));
667 sh_sdhi_writew(host, SDHI_OPTION,
668 OPT_BUS_WIDTH_1 | (~OPT_BUS_WIDTH_M &
669 sh_sdhi_readw(host, SDHI_OPTION)));
671 debug("clock = %d, buswidth = %d\n", mmc->clock, mmc->bus_width);
676 static int sh_sdhi_initialize_common(struct sh_sdhi_host *host)
678 int ret = sh_sdhi_sync_reset(host);
680 sh_sdhi_writew(host, SDHI_PORTSEL, USE_1PORT);
682 #if defined(__BIG_ENDIAN_BITFIELD)
683 sh_sdhi_writew(host, SDHI_EXT_SWAP, SET_SWAP);
686 sh_sdhi_writew(host, SDHI_INFO1_MASK, INFO1M_RESP_END |
687 INFO1M_ACCESS_END | INFO1M_CARD_RE |
688 INFO1M_DATA3_CARD_RE | INFO1M_DATA3_CARD_IN);
693 #ifndef CONFIG_DM_MMC
694 static void *mmc_priv(struct mmc *mmc)
696 return (void *)mmc->priv;
699 static int sh_sdhi_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
700 struct mmc_data *data)
702 struct sh_sdhi_host *host = mmc_priv(mmc);
704 return sh_sdhi_send_cmd_common(host, cmd, data);
707 static int sh_sdhi_set_ios(struct mmc *mmc)
709 struct sh_sdhi_host *host = mmc_priv(mmc);
711 return sh_sdhi_set_ios_common(host, mmc);
714 static int sh_sdhi_initialize(struct mmc *mmc)
716 struct sh_sdhi_host *host = mmc_priv(mmc);
718 return sh_sdhi_initialize_common(host);
721 static const struct mmc_ops sh_sdhi_ops = {
722 .send_cmd = sh_sdhi_send_cmd,
723 .set_ios = sh_sdhi_set_ios,
724 .init = sh_sdhi_initialize,
727 #ifdef CONFIG_RCAR_GEN3
728 static struct mmc_config sh_sdhi_cfg = {
731 .f_min = CLKDEV_INIT,
732 .f_max = CLKDEV_HS_DATA,
733 .voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34,
734 .host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HS |
736 .part_type = PART_TYPE_DOS,
737 .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT,
740 static struct mmc_config sh_sdhi_cfg = {
743 .f_min = CLKDEV_INIT,
744 .f_max = CLKDEV_HS_DATA,
745 .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
746 .host_caps = MMC_MODE_4BIT | MMC_MODE_HS,
747 .part_type = PART_TYPE_DOS,
748 .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT,
752 int sh_sdhi_init(unsigned long addr, int ch, unsigned long quirks)
756 struct sh_sdhi_host *host = NULL;
758 if (ch >= CONFIG_SYS_SH_SDHI_NR_CHANNEL)
761 host = malloc(sizeof(struct sh_sdhi_host));
765 mmc = mmc_create(&sh_sdhi_cfg, host);
772 host->addr = (void __iomem *)addr;
773 host->quirks = quirks;
775 if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF)
777 else if (host->quirks & SH_SDHI_QUIRK_16BIT_BUF)
789 struct sh_sdhi_plat {
790 struct mmc_config cfg;
794 int sh_sdhi_dm_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
795 struct mmc_data *data)
797 struct sh_sdhi_host *host = dev_get_priv(dev);
799 return sh_sdhi_send_cmd_common(host, cmd, data);
802 int sh_sdhi_dm_set_ios(struct udevice *dev)
804 struct sh_sdhi_host *host = dev_get_priv(dev);
805 struct mmc *mmc = mmc_get_mmc_dev(dev);
807 return sh_sdhi_set_ios_common(host, mmc);
810 static const struct dm_mmc_ops sh_sdhi_dm_ops = {
811 .send_cmd = sh_sdhi_dm_send_cmd,
812 .set_ios = sh_sdhi_dm_set_ios,
815 static int sh_sdhi_dm_bind(struct udevice *dev)
817 struct sh_sdhi_plat *plat = dev_get_platdata(dev);
819 return mmc_bind(dev, &plat->mmc, &plat->cfg);
822 static int sh_sdhi_dm_probe(struct udevice *dev)
824 struct sh_sdhi_plat *plat = dev_get_platdata(dev);
825 struct sh_sdhi_host *host = dev_get_priv(dev);
826 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
827 const u32 quirks = dev_get_driver_data(dev);
830 base = devfdt_get_addr(dev);
831 if (base == FDT_ADDR_T_NONE)
834 host->addr = devm_ioremap(dev, base, SZ_2K);
838 host->quirks = quirks;
840 if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF)
842 else if (host->quirks & SH_SDHI_QUIRK_16BIT_BUF)
845 plat->cfg.name = dev->name;
846 plat->cfg.host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
848 switch (fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "bus-width",
851 plat->cfg.host_caps |= MMC_MODE_8BIT;
854 plat->cfg.host_caps |= MMC_MODE_4BIT;
859 dev_err(dev, "Invalid \"bus-width\" value\n");
863 sh_sdhi_initialize_common(host);
865 plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
866 plat->cfg.f_min = CLKDEV_INIT;
867 plat->cfg.f_max = CLKDEV_HS_DATA;
868 plat->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
870 upriv->mmc = &plat->mmc;
875 static const struct udevice_id sh_sdhi_sd_match[] = {
876 { .compatible = "renesas,sdhi-r8a7795", .data = SH_SDHI_QUIRK_64BIT_BUF },
877 { .compatible = "renesas,sdhi-r8a7796", .data = SH_SDHI_QUIRK_64BIT_BUF },
881 U_BOOT_DRIVER(sh_sdhi_mmc) = {
882 .name = "sh-sdhi-mmc",
884 .of_match = sh_sdhi_sd_match,
885 .bind = sh_sdhi_dm_bind,
886 .probe = sh_sdhi_dm_probe,
887 .priv_auto_alloc_size = sizeof(struct sh_sdhi_host),
888 .platdata_auto_alloc_size = sizeof(struct sh_sdhi_plat),
889 .ops = &sh_sdhi_dm_ops,