1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 Marvell International Ltd.
5 * https://spdx.org/licenses
25 #include <linux/delay.h>
26 #include <linux/kernel.h>
27 #include <linux/libfdt.h>
29 #include <asm/arch/board.h>
30 #include <asm/arch/clock.h>
31 #include <asm/arch/csrs/csrs-mio_emm.h>
34 #include <power/regulator.h>
36 #include "octeontx_hsmmc.h"
38 #define MMC_TIMEOUT_SHORT 20 /* in ms */
39 #define MMC_TIMEOUT_LONG 1000
40 #define MMC_TIMEOUT_ERASE 10000
42 #define MMC_DEFAULT_DATA_IN_TAP 10
43 #define MMC_DEFAULT_CMD_IN_TAP 10
44 #define MMC_DEFAULT_CMD_OUT_TAP 39
45 #define MMC_DEFAULT_DATA_OUT_TAP 39
46 #define MMC_DEFAULT_HS200_CMD_IN_TAP 24
47 #define MMC_DEFAULT_HS200_DATA_IN_TAP 24
48 #define MMC_DEFAULT_HS200_CMD_OUT_TAP (otx_is_soc(CN95XX) ? 10 : 5)
49 #define MMC_DEFAULT_HS200_DATA_OUT_TAP (otx_is_soc(CN95XX) ? 10 : 5)
50 #define MMC_DEFAULT_HS400_CMD_OUT_TAP (otx_is_soc(CN95XX) ? 10 : 5)
51 #define MMC_DEFAULT_HS400_DATA_OUT_TAP (otx_is_soc(CN95XX) ? 5 : 3)
52 #define MMC_DEFAULT_HS200_CMD_OUT_DLY 800 /* Delay in ps */
53 #define MMC_DEFAULT_HS200_DATA_OUT_DLY 800 /* Delay in ps */
54 #define MMC_DEFAULT_HS400_CMD_OUT_DLY 800 /* Delay in ps */
55 #define MMC_DEFAULT_HS400_DATA_OUT_DLY 400 /* Delay in ps */
56 #define MMC_DEFAULT_SD_UHS_SDR104_CMD_OUT_TAP MMC_DEFAULT_HS200_CMD_OUT_TAP
57 #define MMC_DEFAULT_SD_UHS_SDR104_DATA_OUT_TAP MMC_DEFAULT_HS200_DATA_OUT_TAP
58 #define MMC_LEGACY_DEFAULT_CMD_OUT_TAP 39
59 #define MMC_LEGACY_DEFAULT_DATA_OUT_TAP 39
60 #define MMC_SD_LEGACY_DEFAULT_CMD_OUT_TAP 63
61 #define MMC_SD_LEGACY_DEFAULT_DATA_OUT_TAP 63
62 #define MMC_HS_CMD_OUT_TAP 32
63 #define MMC_HS_DATA_OUT_TAP 32
64 #define MMC_SD_HS_CMD_OUT_TAP 26
65 #define MMC_SD_HS_DATA_OUT_TAP 26
66 #define MMC_SD_UHS_SDR25_CMD_OUT_TAP 26
67 #define MMC_SD_UHS_SDR25_DATA_OUT_TAP 26
68 #define MMC_SD_UHS_SDR50_CMD_OUT_TAP 26
69 #define MMC_SD_UHS_SDR50_DATA_OUT_TAP 26
70 #define MMC_DEFAULT_TAP_DELAY 4
71 #define TOTAL_NO_OF_TAPS 512
72 static void octeontx_mmc_switch_to(struct mmc *mmc);
73 static int octeontx_mmc_configure_delay(struct mmc *mmc);
74 static void octeontx_mmc_set_timing(struct mmc *mmc);
75 static void set_wdog(struct mmc *mmc, u64 us);
76 static void do_switch(struct mmc *mmc, union mio_emm_switch emm_switch);
77 static int octeontx_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
78 struct mmc_data *data);
79 static int octeontx2_mmc_calc_delay(struct mmc *mmc, int delay);
80 static int octeontx_mmc_calibrate_delay(struct mmc *mmc);
81 static int octeontx_mmc_set_input_bus_timing(struct mmc *mmc);
82 static int octeontx_mmc_set_output_bus_timing(struct mmc *mmc);
84 static bool host_probed;
87 * Get the slot data structure from a MMC data structure
89 static inline struct octeontx_mmc_slot *mmc_to_slot(struct mmc *mmc)
91 return container_of(mmc, struct octeontx_mmc_slot, mmc);
94 static inline struct octeontx_mmc_host *mmc_to_host(struct mmc *mmc)
96 return mmc_to_slot(mmc)->host;
99 static inline struct octeontx_mmc_slot *dev_to_mmc_slot(struct udevice *dev)
101 return dev_get_priv(dev);
104 static inline struct mmc *dev_to_mmc(struct udevice *dev)
106 return &((struct octeontx_mmc_slot *)dev_get_priv(dev))->mmc;
110 const char *mmc_reg_str(u64 reg)
112 if (reg == MIO_EMM_DMA_CFG())
113 return "MIO_EMM_DMA_CFG";
114 if (reg == MIO_EMM_DMA_ADR())
115 return "MIO_EMM_DMA_ADR";
116 if (reg == MIO_EMM_DMA_INT())
117 return "MIO_EMM_DMA_INT";
118 if (reg == MIO_EMM_CFG())
119 return "MIO_EMM_CFG";
120 if (reg == MIO_EMM_MODEX(0))
121 return "MIO_EMM_MODE0";
122 if (reg == MIO_EMM_MODEX(1))
123 return "MIO_EMM_MODE1";
124 if (reg == MIO_EMM_MODEX(2))
125 return "MIO_EMM_MODE2";
126 if (reg == MIO_EMM_MODEX(3))
127 return "MIO_EMM_MODE3";
128 if (reg == MIO_EMM_IO_CTL())
129 return "MIO_EMM_IO_CTL";
130 if (reg == MIO_EMM_SWITCH())
131 return "MIO_EMM_SWITCH";
132 if (reg == MIO_EMM_DMA())
133 return "MIO_EMM_DMA";
134 if (reg == MIO_EMM_CMD())
135 return "MIO_EMM_CMD";
136 if (reg == MIO_EMM_RSP_STS())
137 return "MIO_EMM_RSP_STS";
138 if (reg == MIO_EMM_RSP_LO())
139 return "MIO_EMM_RSP_LO";
140 if (reg == MIO_EMM_RSP_HI())
141 return "MIO_EMM_RSP_HI";
142 if (reg == MIO_EMM_INT())
143 return "MIO_EMM_INT";
144 if (reg == MIO_EMM_WDOG())
145 return "MIO_EMM_WDOG";
146 if (reg == MIO_EMM_DMA_ARG())
147 return "MIO_EMM_DMA_ARG";
148 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
149 if (reg == MIO_EMM_SAMPLE())
150 return "MIO_EMM_SAMPLE";
152 if (reg == MIO_EMM_STS_MASK())
153 return "MIO_EMM_STS_MASK";
154 if (reg == MIO_EMM_RCA())
155 return "MIO_EMM_RCA";
156 if (reg == MIO_EMM_BUF_IDX())
157 return "MIO_EMM_BUF_IDX";
158 if (reg == MIO_EMM_BUF_DAT())
159 return "MIO_EMM_BUF_DAT";
160 if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
161 if (reg == MIO_EMM_CALB())
162 return "MIO_EMM_CALB";
163 if (reg == MIO_EMM_TAP())
164 return "MIO_EMM_TAP";
165 if (reg == MIO_EMM_TIMING())
166 return "MIO_EMM_TIMING";
167 if (reg == MIO_EMM_DEBUG())
168 return "MIO_EMM_DEBUG";
175 static void octeontx_print_rsp_sts(struct mmc *mmc)
178 union mio_emm_rsp_sts emm_rsp_sts;
179 const struct octeontx_mmc_host *host = mmc_to_host(mmc);
180 static const char * const ctype_xor_str[] = {
182 "Read data into Dbuf",
183 "Write data from Dbuf",
187 static const char * const rtype_xor_str[] = {
198 emm_rsp_sts.u = readq(host->base_addr + MIO_EMM_RSP_STS());
199 printf("\nMIO_EMM_RSP_STS: 0x%016llx\n", emm_rsp_sts.u);
200 printf(" 60-61: bus_id: %u\n", emm_rsp_sts.s.bus_id);
201 printf(" 59: cmd_val: %s\n",
202 emm_rsp_sts.s.cmd_val ? "yes" : "no");
203 printf(" 58: switch_val: %s\n",
204 emm_rsp_sts.s.switch_val ? "yes" : "no");
205 printf(" 57: dma_val: %s\n",
206 emm_rsp_sts.s.dma_val ? "yes" : "no");
207 printf(" 56: dma_pend: %s\n",
208 emm_rsp_sts.s.dma_pend ? "yes" : "no");
209 printf(" 28: dbuf_err: %s\n",
210 emm_rsp_sts.s.dbuf_err ? "yes" : "no");
211 printf(" 23: dbuf: %u\n", emm_rsp_sts.s.dbuf);
212 printf(" 22: blk_timeout: %s\n",
213 emm_rsp_sts.s.blk_timeout ? "yes" : "no");
214 printf(" 21: blk_crc_err: %s\n",
215 emm_rsp_sts.s.blk_crc_err ? "yes" : "no");
216 printf(" 20: rsp_busybit: %s\n",
217 emm_rsp_sts.s.rsp_busybit ? "yes" : "no");
218 printf(" 19: stp_timeout: %s\n",
219 emm_rsp_sts.s.stp_timeout ? "yes" : "no");
220 printf(" 18: stp_crc_err: %s\n",
221 emm_rsp_sts.s.stp_crc_err ? "yes" : "no");
222 printf(" 17: stp_bad_sts: %s\n",
223 emm_rsp_sts.s.stp_bad_sts ? "yes" : "no");
224 printf(" 16: stp_val: %s\n",
225 emm_rsp_sts.s.stp_val ? "yes" : "no");
226 printf(" 15: rsp_timeout: %s\n",
227 emm_rsp_sts.s.rsp_timeout ? "yes" : "no");
228 printf(" 14: rsp_crc_err: %s\n",
229 emm_rsp_sts.s.rsp_crc_err ? "yes" : "no");
230 printf(" 13: rsp_bad_sts: %s\n",
231 emm_rsp_sts.s.rsp_bad_sts ? "yes" : "no");
232 printf(" 12: rsp_val: %s\n",
233 emm_rsp_sts.s.rsp_val ? "yes" : "no");
234 printf(" 9-11: rsp_type: %s\n",
235 rtype_xor_str[emm_rsp_sts.s.rsp_type]);
236 printf(" 7-8: cmd_type: %s\n",
237 ctype_xor_str[emm_rsp_sts.s.cmd_type]);
238 printf(" 1-6: cmd_idx: %u\n",
239 emm_rsp_sts.s.cmd_idx);
240 printf(" 0: cmd_done: %s\n",
241 emm_rsp_sts.s.cmd_done ? "yes" : "no");
245 static inline u64 read_csr(struct mmc *mmc, u64 reg)
247 const struct octeontx_mmc_host *host = mmc_to_host(mmc);
248 u64 value = readq(host->base_addr + reg);
250 printf(" %s: %s(0x%p) => 0x%llx\n", __func__,
251 mmc_reg_str(reg), host->base_addr + reg,
258 * Writes to a CSR register
260 * @param[in] mmc pointer to mmc data structure
261 * @param reg register offset
262 * @param value value to write to register
264 static inline void write_csr(struct mmc *mmc, u64 reg, u64 value)
266 const struct octeontx_mmc_host *host = mmc_to_host(mmc);
267 void *addr = host->base_addr + reg;
270 printf(" %s: %s(0x%p) <= 0x%llx\n", __func__, mmc_reg_str(reg),
277 static void mmc_print_status(u32 status)
280 static const char * const state[] = {
296 "reserved" /* 15 */ };
297 if (status & R1_APP_CMD)
299 if (status & R1_SWITCH_ERROR)
300 puts("MMC switch error\n");
301 if (status & R1_READY_FOR_DATA)
302 puts("MMC ready for data\n");
303 printf("MMC %s state\n", state[R1_CURRENT_STATE(status)]);
304 if (status & R1_ERASE_RESET)
305 puts("MMC erase reset\n");
306 if (status & R1_WP_ERASE_SKIP)
307 puts("MMC partial erase due to write protected blocks\n");
308 if (status & R1_CID_CSD_OVERWRITE)
309 puts("MMC CID/CSD overwrite error\n");
310 if (status & R1_ERROR)
311 puts("MMC undefined device error\n");
312 if (status & R1_CC_ERROR)
313 puts("MMC device error\n");
314 if (status & R1_CARD_ECC_FAILED)
315 puts("MMC internal ECC failed to correct data\n");
316 if (status & R1_ILLEGAL_COMMAND)
317 puts("MMC illegal command\n");
318 if (status & R1_COM_CRC_ERROR)
319 puts("MMC CRC of previous command failed\n");
320 if (status & R1_LOCK_UNLOCK_FAILED)
321 puts("MMC sequence or password error in lock/unlock device command\n");
322 if (status & R1_CARD_IS_LOCKED)
323 puts("MMC device locked by host\n");
324 if (status & R1_WP_VIOLATION)
325 puts("MMC attempt to program write protected block\n");
326 if (status & R1_ERASE_PARAM)
327 puts("MMC invalid selection of erase groups for erase\n");
328 if (status & R1_ERASE_SEQ_ERROR)
329 puts("MMC error in sequence of erase commands\n");
330 if (status & R1_BLOCK_LEN_ERROR)
331 puts("MMC block length error\n");
332 if (status & R1_ADDRESS_ERROR)
333 puts("MMC address misalign error\n");
334 if (status & R1_OUT_OF_RANGE)
335 puts("MMC address out of range\n");
341 * Print out all of the register values where mmc is optional
343 * @param mmc MMC device (can be NULL)
344 * @param host Pointer to host data structure (can be NULL if mmc is !NULL)
346 static void octeontx_mmc_print_registers2(struct mmc *mmc,
347 struct octeontx_mmc_host *host)
349 struct octeontx_mmc_slot *slot = mmc ? mmc->priv : NULL;
350 union mio_emm_dma_cfg emm_dma_cfg;
351 union mio_emm_dma_adr emm_dma_adr;
352 union mio_emm_dma_int emm_dma_int;
353 union mio_emm_cfg emm_cfg;
354 union mio_emm_modex emm_mode;
355 union mio_emm_switch emm_switch;
356 union mio_emm_dma emm_dma;
357 union mio_emm_cmd emm_cmd;
358 union mio_emm_rsp_sts emm_rsp_sts;
359 union mio_emm_rsp_lo emm_rsp_lo;
360 union mio_emm_rsp_hi emm_rsp_hi;
361 union mio_emm_int emm_int;
362 union mio_emm_wdog emm_wdog;
363 union mio_emm_sample emm_sample;
364 union mio_emm_calb emm_calb;
365 union mio_emm_tap emm_tap;
366 union mio_emm_timing emm_timing;
367 union mio_emm_io_ctl io_ctl;
368 union mio_emm_debug emm_debug;
369 union mio_emm_sts_mask emm_sts_mask;
370 union mio_emm_rca emm_rca;
373 static const char * const bus_width_str[] = {
374 "1-bit data bus (power on)",
379 "4-bit data bus (dual data rate)",
380 "8-bit data bus (dual data rate)",
391 static const char * const ctype_xor_str[] = {
393 "Read data into Dbuf",
394 "Write data from Dbuf",
398 static const char * const rtype_xor_str[] = {
410 host = mmc_to_host(mmc);
413 printf("%s: bus id: %u\n", __func__, slot->bus_id);
414 emm_dma_cfg.u = readq(host->base_addr + MIO_EMM_DMA_CFG());
415 printf("MIO_EMM_DMA_CFG: 0x%016llx\n",
417 printf(" 63: en: %s\n",
418 emm_dma_cfg.s.en ? "enabled" : "disabled");
419 printf(" 62: rw: %s\n",
420 emm_dma_cfg.s.rw ? "write" : "read");
421 printf(" 61: clr: %s\n",
422 emm_dma_cfg.s.clr ? "clear" : "not clear");
423 printf(" 59: swap32: %s\n",
424 emm_dma_cfg.s.swap32 ? "yes" : "no");
425 printf(" 58: swap16: %s\n",
426 emm_dma_cfg.s.swap16 ? "yes" : "no");
427 printf(" 57: swap8: %s\n",
428 emm_dma_cfg.s.swap8 ? "yes" : "no");
429 printf(" 56: endian: %s\n",
430 emm_dma_cfg.s.endian ? "little" : "big");
431 printf(" 36-55: size: %u\n",
434 emm_dma_adr.u = readq(host->base_addr + MIO_EMM_DMA_ADR());
435 printf("MIO_EMM_DMA_ADR: 0x%016llx\n", emm_dma_adr.u);
436 printf(" 0-49: adr: 0x%llx\n",
437 (u64)emm_dma_adr.s.adr);
439 emm_dma_int.u = readq(host->base_addr + MIO_EMM_DMA_INT());
440 printf("\nMIO_EMM_DMA_INT: 0x%016llx\n",
442 printf(" 1: FIFO: %s\n",
443 emm_dma_int.s.fifo ? "yes" : "no");
444 printf(" 0: Done: %s\n",
445 emm_dma_int.s.done ? "yes" : "no");
446 emm_cfg.u = readq(host->base_addr + MIO_EMM_CFG());
448 printf("\nMIO_EMM_CFG: 0x%016llx\n",
450 printf(" 3: bus_ena3: %s\n",
451 emm_cfg.s.bus_ena & 0x08 ? "yes" : "no");
452 printf(" 2: bus_ena2: %s\n",
453 emm_cfg.s.bus_ena & 0x04 ? "yes" : "no");
454 printf(" 1: bus_ena1: %s\n",
455 emm_cfg.s.bus_ena & 0x02 ? "yes" : "no");
456 printf(" 0: bus_ena0: %s\n",
457 emm_cfg.s.bus_ena & 0x01 ? "yes" : "no");
458 for (bus = 0; bus < 4; bus++) {
459 emm_mode.u = readq(host->base_addr + MIO_EMM_MODEX(bus));
460 printf("\nMIO_EMM_MODE%u: 0x%016llx\n",
462 if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
463 printf(" 50: hs400_timing: %s\n",
464 emm_mode.s.hs400_timing ? "yes" : "no");
465 printf(" 49: hs200_timing: %s\n",
466 emm_mode.s.hs200_timing ? "yes" : "no");
468 printf(" 48: hs_timing: %s\n",
469 emm_mode.s.hs_timing ? "yes" : "no");
470 printf(" 40-42: bus_width: %s\n",
471 bus_width_str[emm_mode.s.bus_width]);
472 printf(" 32-35: power_class %u\n",
473 emm_mode.s.power_class);
474 printf(" 16-31: clk_hi: %u\n",
476 printf(" 0-15: clk_lo: %u\n",
480 emm_switch.u = readq(host->base_addr + MIO_EMM_SWITCH());
481 printf("\nMIO_EMM_SWITCH: 0x%016llx\n", emm_switch.u);
482 printf(" 60-61: bus_id: %u\n", emm_switch.s.bus_id);
483 printf(" 59: switch_exe: %s\n",
484 emm_switch.s.switch_exe ? "yes" : "no");
485 printf(" 58: switch_err0: %s\n",
486 emm_switch.s.switch_err0 ? "yes" : "no");
487 printf(" 57: switch_err1: %s\n",
488 emm_switch.s.switch_err1 ? "yes" : "no");
489 printf(" 56: switch_err2: %s\n",
490 emm_switch.s.switch_err2 ? "yes" : "no");
491 printf(" 48: hs_timing: %s\n",
492 emm_switch.s.hs_timing ? "yes" : "no");
493 printf(" 42-40: bus_width: %s\n",
494 bus_width_str[emm_switch.s.bus_width]);
495 printf(" 32-35: power_class: %u\n",
496 emm_switch.s.power_class);
497 printf(" 16-31: clk_hi: %u\n",
498 emm_switch.s.clk_hi);
499 printf(" 0-15: clk_lo: %u\n", emm_switch.s.clk_lo);
501 emm_dma.u = readq(host->base_addr + MIO_EMM_DMA());
502 printf("\nMIO_EMM_DMA: 0x%016llx\n", emm_dma.u);
503 printf(" 60-61: bus_id: %u\n", emm_dma.s.bus_id);
504 printf(" 59: dma_val: %s\n",
505 emm_dma.s.dma_val ? "yes" : "no");
506 printf(" 58: sector: %s mode\n",
507 emm_dma.s.sector ? "sector" : "byte");
508 printf(" 57: dat_null: %s\n",
509 emm_dma.s.dat_null ? "yes" : "no");
510 printf(" 51-56: thres: %u\n", emm_dma.s.thres);
511 printf(" 50: rel_wr: %s\n",
512 emm_dma.s.rel_wr ? "yes" : "no");
513 printf(" 49: rw: %s\n",
514 emm_dma.s.rw ? "write" : "read");
515 printf(" 48: multi: %s\n",
516 emm_dma.s.multi ? "yes" : "no");
517 printf(" 32-47: block_cnt: %u\n",
518 emm_dma.s.block_cnt);
519 printf(" 0-31: card_addr: 0x%x\n",
520 emm_dma.s.card_addr);
522 emm_cmd.u = readq(host->base_addr + MIO_EMM_CMD());
523 printf("\nMIO_EMM_CMD: 0x%016llx\n", emm_cmd.u);
524 printf("\n 62: skip_busy: %s\n",
525 emm_cmd.s.skip_busy ? "yes" : "no");
526 printf(" 60-61: bus_id: %u\n", emm_cmd.s.bus_id);
527 printf(" 59: cmd_val: %s\n",
528 emm_cmd.s.cmd_val ? "yes" : "no");
529 printf(" 55: dbuf: %u\n", emm_cmd.s.dbuf);
530 printf(" 49-54: offset: %u\n", emm_cmd.s.offset);
531 printf(" 41-42: ctype_xor: %s\n",
532 ctype_xor_str[emm_cmd.s.ctype_xor]);
533 printf(" 38-40: rtype_xor: %s\n",
534 rtype_xor_str[emm_cmd.s.rtype_xor]);
535 printf(" 32-37: cmd_idx: %u\n", emm_cmd.s.cmd_idx);
536 printf(" 0-31: arg: 0x%x\n", emm_cmd.s.arg);
538 emm_rsp_sts.u = readq(host->base_addr + MIO_EMM_RSP_STS());
539 printf("\nMIO_EMM_RSP_STS: 0x%016llx\n", emm_rsp_sts.u);
540 printf(" 60-61: bus_id: %u\n", emm_rsp_sts.s.bus_id);
541 printf(" 59: cmd_val: %s\n",
542 emm_rsp_sts.s.cmd_val ? "yes" : "no");
543 printf(" 58: switch_val: %s\n",
544 emm_rsp_sts.s.switch_val ? "yes" : "no");
545 printf(" 57: dma_val: %s\n",
546 emm_rsp_sts.s.dma_val ? "yes" : "no");
547 printf(" 56: dma_pend: %s\n",
548 emm_rsp_sts.s.dma_pend ? "yes" : "no");
549 printf(" 28: dbuf_err: %s\n",
550 emm_rsp_sts.s.dbuf_err ? "yes" : "no");
551 printf(" 23: dbuf: %u\n", emm_rsp_sts.s.dbuf);
552 printf(" 22: blk_timeout: %s\n",
553 emm_rsp_sts.s.blk_timeout ? "yes" : "no");
554 printf(" 21: blk_crc_err: %s\n",
555 emm_rsp_sts.s.blk_crc_err ? "yes" : "no");
556 printf(" 20: rsp_busybit: %s\n",
557 emm_rsp_sts.s.rsp_busybit ? "yes" : "no");
558 printf(" 19: stp_timeout: %s\n",
559 emm_rsp_sts.s.stp_timeout ? "yes" : "no");
560 printf(" 18: stp_crc_err: %s\n",
561 emm_rsp_sts.s.stp_crc_err ? "yes" : "no");
562 printf(" 17: stp_bad_sts: %s\n",
563 emm_rsp_sts.s.stp_bad_sts ? "yes" : "no");
564 printf(" 16: stp_val: %s\n",
565 emm_rsp_sts.s.stp_val ? "yes" : "no");
566 printf(" 15: rsp_timeout: %s\n",
567 emm_rsp_sts.s.rsp_timeout ? "yes" : "no");
568 printf(" 14: rsp_crc_err: %s\n",
569 emm_rsp_sts.s.rsp_crc_err ? "yes" : "no");
570 printf(" 13: rsp_bad_sts: %s\n",
571 emm_rsp_sts.s.rsp_bad_sts ? "yes" : "no");
572 printf(" 12: rsp_val: %s\n",
573 emm_rsp_sts.s.rsp_val ? "yes" : "no");
574 printf(" 9-11: rsp_type: %s\n",
575 rtype_xor_str[emm_rsp_sts.s.rsp_type]);
576 printf(" 7-8: cmd_type: %s\n",
577 ctype_xor_str[emm_rsp_sts.s.cmd_type]);
578 printf(" 1-6: cmd_idx: %u\n",
579 emm_rsp_sts.s.cmd_idx);
580 printf(" 0: cmd_done: %s\n",
581 emm_rsp_sts.s.cmd_done ? "yes" : "no");
583 emm_rsp_lo.u = readq(host->base_addr + MIO_EMM_RSP_LO());
584 printf("\nMIO_EMM_RSP_STS_LO: 0x%016llx\n", emm_rsp_lo.u);
586 emm_rsp_hi.u = readq(host->base_addr + MIO_EMM_RSP_HI());
587 printf("\nMIO_EMM_RSP_STS_HI: 0x%016llx\n", emm_rsp_hi.u);
589 emm_int.u = readq(host->base_addr + MIO_EMM_INT());
590 printf("\nMIO_EMM_INT: 0x%016llx\n", emm_int.u);
591 printf(" 6: switch_err: %s\n",
592 emm_int.s.switch_err ? "yes" : "no");
593 printf(" 5: switch_done: %s\n",
594 emm_int.s.switch_done ? "yes" : "no");
595 printf(" 4: dma_err: %s\n",
596 emm_int.s.dma_err ? "yes" : "no");
597 printf(" 3: cmd_err: %s\n",
598 emm_int.s.cmd_err ? "yes" : "no");
599 printf(" 2: dma_done: %s\n",
600 emm_int.s.dma_done ? "yes" : "no");
601 printf(" 1: cmd_done: %s\n",
602 emm_int.s.cmd_done ? "yes" : "no");
603 printf(" 0: buf_done: %s\n",
604 emm_int.s.buf_done ? "yes" : "no");
606 emm_wdog.u = readq(host->base_addr + MIO_EMM_WDOG());
607 printf("\nMIO_EMM_WDOG: 0x%016llx (%u)\n",
608 emm_wdog.u, emm_wdog.s.clk_cnt);
610 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
611 emm_sample.u = readq(host->base_addr + MIO_EMM_SAMPLE());
612 printf("\nMIO_EMM_SAMPLE: 0x%016llx\n",
614 printf(" 16-25: cmd_cnt: %u\n",
615 emm_sample.s.cmd_cnt);
616 printf(" 0-9: dat_cnt: %u\n",
617 emm_sample.s.dat_cnt);
620 emm_sts_mask.u = readq(host->base_addr + MIO_EMM_STS_MASK());
621 printf("\nMIO_EMM_STS_MASK: 0x%016llx\n", emm_sts_mask.u);
623 emm_rca.u = readq(host->base_addr + MIO_EMM_RCA());
624 printf("\nMIO_EMM_RCA: 0x%016llx\n", emm_rca.u);
625 printf(" 0-15: card_rca: 0x%04x\n",
627 if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
628 emm_calb.u = readq(host->base_addr + MIO_EMM_CALB());
629 printf("\nMIO_EMM_CALB: 0x%016llx\n",
631 printf(" 0: start: %u\n",
633 emm_tap.u = readq(host->base_addr + MIO_EMM_TAP());
634 printf("\nMIO_EMM_TAP: 0x%016llx\n",
636 printf(" 7-0: delay: %u\n", emm_tap.s.delay);
637 emm_timing.u = readq(host->base_addr + MIO_EMM_TIMING());
638 printf("\nMIO_EMM_TIMING: 0x%016llx\n",
640 printf(" 53-48: cmd_in_tap: %u\n",
641 emm_timing.s.cmd_in_tap);
642 printf(" 37-32: cmd_out_tap: %u\n",
643 emm_timing.s.cmd_out_tap);
644 printf(" 21-16: data_in_tap: %u\n",
645 emm_timing.s.data_in_tap);
646 printf(" 5-0: data_out_tap: %u\n",
647 emm_timing.s.data_out_tap);
648 io_ctl.u = readq(host->base_addr + MIO_EMM_IO_CTL());
649 printf("\nMIO_IO_CTL: 0x%016llx\n", io_ctl.u);
650 printf(" 3-2: drive: %u (%u mA)\n",
651 io_ctl.s.drive, 2 << io_ctl.s.drive);
652 printf(" 0: slew: %u %s\n", io_ctl.s.slew,
653 io_ctl.s.slew ? "high" : "low");
654 emm_debug.u = readq(host->base_addr + MIO_EMM_DEBUG());
655 printf("\nMIO_EMM_DEBUG: 0x%016llx\n",
657 printf(" 21: rdsync_rst 0x%x\n",
658 emm_debug.s.rdsync_rst);
659 printf(" 20: emmc_clk_disable 0x%x\n",
660 emm_debug.s.emmc_clk_disable);
661 printf(" 19-16: dma_sm: 0x%x\n",
663 printf(" 15-12: data_sm: 0x%x\n",
664 emm_debug.s.data_sm);
665 printf(" 11-8: cmd_sm: 0x%x\n",
667 printf(" 0: clk_on: 0x%x\n",
675 * Print out all of the register values
677 * @param mmc MMC device
679 static void octeontx_mmc_print_registers(struct mmc *mmc)
681 #ifdef DEBUG_REGISTERS
687 octeontx_mmc_print_registers2(mmc, mmc_to_host(mmc));
690 static const struct octeontx_sd_mods octeontx_cr_types[] = {
691 { {0, 0}, {0, 0}, {0, 0} }, /* CMD0 */
692 { {0, 3}, {0, 3}, {0, 0} }, /* CMD1 */
693 { {0, 2}, {0, 2}, {0, 0} }, /* CMD2 */
694 { {0, 1}, {0, 3}, {0, 0} }, /* CMD3 SD_CMD_SEND_RELATIVE_ADDR 0, 2 */
695 { {0, 0}, {0, 0}, {0, 0} }, /* CMD4 */
696 { {0, 1}, {0, 1}, {0, 0} }, /* CMD5 */
697 { {0, 1}, {1, 1}, {0, 1} }, /*
698 * CMD6 SD_CMD_SWITCH_FUNC 1,0
699 * (ACMD) SD_APP_SET_BUS_WIDTH
701 { {0, 1}, {0, 1}, {0, 0} }, /* CMD7 */
702 { {1, 1}, {0, 3}, {0, 0} }, /* CMD8 SD_CMD_SEND_IF_COND 1,2 */
703 { {0, 2}, {0, 2}, {0, 0} }, /* CMD9 */
704 { {0, 2}, {0, 2}, {0, 0} }, /* CMD10 */
705 { {1, 1}, {0, 1}, {1, 1} }, /* CMD11 SD_CMD_SWITCH_UHS18V 1,0 */
706 { {0, 1}, {0, 1}, {0, 0} }, /* CMD12 */
707 { {0, 1}, {0, 1}, {1, 3} }, /* CMD13 (ACMD)) SD_CMD_APP_SD_STATUS 1,2 */
708 { {1, 1}, {1, 1}, {0, 0} }, /* CMD14 */
709 { {0, 0}, {0, 0}, {0, 0} }, /* CMD15 */
710 { {0, 1}, {0, 1}, {0, 0} }, /* CMD16 */
711 { {1, 1}, {1, 1}, {0, 0} }, /* CMD17 */
712 { {1, 1}, {1, 1}, {0, 0} }, /* CMD18 */
713 { {3, 1}, {3, 1}, {0, 0} }, /* CMD19 */
714 { {2, 1}, {0, 0}, {0, 0} }, /* CMD20 */ /* SD 2,0 */
715 { {0, 0}, {0, 0}, {0, 0} }, /* CMD21 */
716 { {0, 0}, {0, 0}, {1, 1} }, /* CMD22 (ACMD) SD_APP_SEND_NUM_WR_BLKS 1,0 */
717 { {0, 1}, {0, 1}, {0, 1} }, /* CMD23 */ /* SD ACMD 1,0 */
718 { {2, 1}, {2, 1}, {2, 1} }, /* CMD24 */
719 { {2, 1}, {2, 1}, {2, 1} }, /* CMD25 */
720 { {2, 1}, {2, 1}, {2, 1} }, /* CMD26 */
721 { {2, 1}, {2, 1}, {2, 1} }, /* CMD27 */
722 { {0, 1}, {0, 1}, {0, 1} }, /* CMD28 */
723 { {0, 1}, {0, 1}, {0, 1} }, /* CMD29 */
724 { {1, 1}, {1, 1}, {1, 1} }, /* CMD30 */
725 { {1, 1}, {1, 1}, {1, 1} }, /* CMD31 */
726 { {0, 0}, {0, 1}, {0, 0} }, /* CMD32 SD_CMD_ERASE_WR_BLK_START 0,1 */
727 { {0, 0}, {0, 1}, {0, 0} }, /* CMD33 SD_CMD_ERASE_WR_BLK_END 0,1 */
728 { {0, 0}, {0, 0}, {0, 0} }, /* CMD34 */
729 { {0, 1}, {0, 1}, {0, 1} }, /* CMD35 */
730 { {0, 1}, {0, 1}, {0, 1} }, /* CMD36 */
731 { {0, 0}, {0, 0}, {0, 0} }, /* CMD37 */
732 { {0, 1}, {0, 1}, {0, 1} }, /* CMD38 */
733 { {0, 4}, {0, 4}, {0, 4} }, /* CMD39 */
734 { {0, 5}, {0, 5}, {0, 5} }, /* CMD40 */
735 { {0, 0}, {0, 0}, {0, 3} }, /* CMD41 (ACMD) SD_CMD_APP_SEND_OP_COND 0,3 */
736 { {2, 1}, {2, 1}, {2, 1} }, /* CMD42 */
737 { {0, 0}, {0, 0}, {0, 0} }, /* CMD43 */
738 { {0, 0}, {0, 0}, {0, 0} }, /* CMD44 */
739 { {0, 0}, {0, 0}, {0, 0} }, /* CMD45 */
740 { {0, 0}, {0, 0}, {0, 0} }, /* CMD46 */
741 { {0, 0}, {0, 0}, {0, 0} }, /* CMD47 */
742 { {0, 0}, {1, 0}, {0, 0} }, /* CMD48 SD_CMD_READ_EXTR_SINGLE */
743 { {0, 0}, {2, 0}, {0, 0} }, /* CMD49 SD_CMD_WRITE_EXTR_SINGLE */
744 { {0, 0}, {0, 0}, {0, 0} }, /* CMD50 */
745 { {0, 0}, {0, 0}, {1, 1} }, /* CMD51 (ACMD) SD_CMD_APP_SEND_SCR 1,1 */
746 { {0, 0}, {0, 0}, {0, 0} }, /* CMD52 */
747 { {0, 0}, {0, 0}, {0, 0} }, /* CMD53 */
748 { {0, 0}, {0, 0}, {0, 0} }, /* CMD54 */
749 { {0, 1}, {0, 1}, {0, 1} }, /* CMD55 */
750 { {0xff, 0xff}, {0xff, 0xff}, {0xff, 0xff} }, /* CMD56 */
751 { {0, 0}, {0, 0}, {0, 0} }, /* CMD57 */
752 { {0, 0}, {0, 3}, {0, 3} }, /* CMD58 SD_CMD_SPI_READ_OCR 0,3 */
753 { {0, 0}, {0, 1}, {0, 0} }, /* CMD59 SD_CMD_SPI_CRC_ON_OFF 0,1 */
754 { {0, 0}, {0, 0}, {0, 0} }, /* CMD60 */
755 { {0, 0}, {0, 0}, {0, 0} }, /* CMD61 */
756 { {0, 0}, {0, 0}, {0, 0} }, /* CMD62 */
757 { {0, 0}, {0, 0}, {0, 0} } /* CMD63 */
761 * Returns XOR values needed for SD commands and other quirks
763 * @param mmc mmc device
764 * @param cmd command information
766 * @return octeontx_mmc_cr_mods data structure with various quirks and flags
768 static struct octeontx_mmc_cr_mods
769 octeontx_mmc_get_cr_mods(struct mmc *mmc, const struct mmc_cmd *cmd,
770 const struct mmc_data *data)
772 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
773 struct octeontx_mmc_cr_mods cr = {0, 0};
774 const struct octeontx_sd_mods *sdm =
775 &octeontx_cr_types[cmd->cmdidx & 0x3f];
776 u8 c = sdm->mmc.c, r = sdm->mmc.r;
777 u8 desired_ctype = 0;
780 #ifdef MMC_SUPPORTS_TUNING
781 if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
782 if (cmd->resp_type == MMC_RSP_R1)
784 if (data && data->flags & MMC_DATA_READ)
791 if (cmd->cmdidx == 56)
792 c = (cmd->cmdarg & 1) ? 1 : 2;
795 if (data->flags & MMC_DATA_READ)
797 else if (data->flags & MMC_DATA_WRITE)
801 cr.ctype_xor = c ^ desired_ctype;
803 cr.rtype_xor = r ^ sdm->sdacmd.r;
805 cr.rtype_xor = r ^ sdm->sd.r;
807 debug("%s(%s): mmc c: %d, mmc r: %d, desired c: %d, xor c: %d, xor r: %d\n",
808 __func__, mmc->dev->name, c, r, desired_ctype,
809 cr.ctype_xor, cr.rtype_xor);
814 * Keep track of switch commands internally
816 static void octeontx_mmc_track_switch(struct mmc *mmc, u32 cmd_arg)
818 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
819 u8 how = (cmd_arg >> 24) & 3;
820 u8 where = (u8)(cmd_arg >> 16);
821 u8 val = (u8)(cmd_arg >> 8);
823 slot->want_switch = slot->cached_switch;
832 case EXT_CSD_BUS_WIDTH:
833 slot->want_switch.s.bus_width = val;
835 case EXT_CSD_POWER_CLASS:
836 slot->want_switch.s.power_class = val;
838 case EXT_CSD_HS_TIMING:
839 slot->want_switch.s.hs_timing = 0;
840 slot->want_switch.s.hs200_timing = 0;
841 slot->want_switch.s.hs400_timing = 0;
846 slot->want_switch.s.hs_timing = 1;
849 if (!slot->is_asim && !slot->is_emul)
850 slot->want_switch.s.hs200_timing = 1;
853 if (!slot->is_asim && !slot->is_emul)
854 slot->want_switch.s.hs400_timing = 1;
857 pr_err("%s(%s): Unsupported timing mode 0x%x\n",
858 __func__, mmc->dev->name, val & 0xf);
867 static int octeontx_mmc_print_rsp_errors(struct mmc *mmc,
868 union mio_emm_rsp_sts rsp_sts)
871 const char *name = mmc->dev->name;
873 if (rsp_sts.s.acc_timeout) {
874 pr_warn("%s(%s): acc_timeout\n", __func__, name);
877 if (rsp_sts.s.dbuf_err) {
878 pr_warn("%s(%s): dbuf_err\n", __func__, name);
881 if (rsp_sts.s.blk_timeout) {
882 pr_warn("%s(%s): blk_timeout\n", __func__, name);
885 if (rsp_sts.s.blk_crc_err) {
886 pr_warn("%s(%s): blk_crc_err\n", __func__, name);
889 if (rsp_sts.s.stp_timeout) {
890 pr_warn("%s(%s): stp_timeout\n", __func__, name);
893 if (rsp_sts.s.stp_crc_err) {
894 pr_warn("%s(%s): stp_crc_err\n", __func__, name);
897 if (rsp_sts.s.stp_bad_sts) {
898 pr_warn("%s(%s): stp_bad_sts\n", __func__, name);
902 pr_warn(" rsp_sts: 0x%llx\n", rsp_sts.u);
908 * Starts a DMA operation for block read/write
910 * @param mmc mmc device
911 * @param write true if write operation
912 * @param clear true to clear DMA operation
913 * @param adr source or destination DMA address
914 * @param size size in blocks
915 * @param timeout timeout in ms
917 static void octeontx_mmc_start_dma(struct mmc *mmc, bool write,
918 bool clear, u32 block, dma_addr_t adr,
919 u32 size, int timeout)
921 const struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
922 union mio_emm_dma_cfg emm_dma_cfg;
923 union mio_emm_dma_adr emm_dma_adr;
924 union mio_emm_dma emm_dma;
926 /* Clear any interrupts */
927 write_csr(mmc, MIO_EMM_DMA_INT(),
928 read_csr(mmc, MIO_EMM_DMA_INT()));
931 emm_dma_cfg.s.en = 1;
932 emm_dma_cfg.s.rw = !!write;
933 emm_dma_cfg.s.clr = !!clear;
934 emm_dma_cfg.s.size = ((u64)(size * mmc->read_bl_len) / 8) - 1;
935 #if __BYTE_ORDER != __BIG_ENDIAN
936 emm_dma_cfg.s.endian = 1;
939 emm_dma_adr.s.adr = adr;
940 write_csr(mmc, MIO_EMM_DMA_ADR(), emm_dma_adr.u);
941 write_csr(mmc, MIO_EMM_DMA_CFG(), emm_dma_cfg.u);
944 emm_dma.s.bus_id = slot->bus_id;
945 emm_dma.s.dma_val = 1;
946 emm_dma.s.rw = !!write;
947 emm_dma.s.sector = mmc->high_capacity ? 1 : 0;
949 if (size > 1 && ((IS_SD(mmc) && (mmc->scr[0] & 2)) || !IS_SD(mmc)))
954 emm_dma.s.block_cnt = size;
955 if (!mmc->high_capacity)
956 block *= mmc->read_bl_len;
957 emm_dma.s.card_addr = block;
958 debug("%s(%s): card address: 0x%x, size: %d, multi: %d\n",
959 __func__, mmc->dev->name, block, size, emm_dma.s.multi);
962 timeout = (timeout * 1000) - 1000;
963 set_wdog(mmc, timeout);
965 debug(" Writing 0x%llx to mio_emm_dma\n", emm_dma.u);
966 write_csr(mmc, MIO_EMM_DMA(), emm_dma.u);
970 * Waits for a DMA operation to complete
972 * @param mmc mmc device
973 * @param timeout timeout in ms
975 * @return 0 for success (could be DMA errors), -ETIMEDOUT on timeout
979 * Cleanup DMA engine after a failure
981 * @param mmc mmc device
982 * @param rsp_sts rsp status
984 static void octeontx_mmc_cleanup_dma(struct mmc *mmc,
985 union mio_emm_rsp_sts rsp_sts)
987 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
988 union mio_emm_dma emm_dma;
993 debug("%s(%s): rsp_sts: 0x%llx, rsp_lo: 0x%llx, dma_int: 0x%llx\n",
994 __func__, mmc->dev->name, rsp_sts.u,
995 read_csr(mmc, MIO_EMM_RSP_LO()),
996 read_csr(mmc, MIO_EMM_DMA_INT()));
997 emm_dma.u = read_csr(mmc, MIO_EMM_DMA());
998 emm_dma.s.dma_val = 1;
999 emm_dma.s.dat_null = 1;
1000 emm_dma.s.bus_id = slot->bus_id;
1001 write_csr(mmc, MIO_EMM_DMA(), emm_dma.u);
1002 start = get_timer(0);
1004 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1006 } while (get_timer(start) < 100 &&
1007 (rsp_sts.s.dma_val || rsp_sts.s.dma_pend));
1008 } while (retries-- >= 0 && rsp_sts.s.dma_pend);
1009 if (rsp_sts.s.dma_val)
1010 pr_err("%s(%s): Error: could not clean up DMA. RSP_STS: 0x%llx, RSP_LO: 0x%llx\n",
1011 __func__, mmc->dev->name, rsp_sts.u,
1012 read_csr(mmc, MIO_EMM_RSP_LO()));
1013 debug(" rsp_sts after clearing up DMA: 0x%llx\n",
1014 read_csr(mmc, MIO_EMM_RSP_STS()));
1018 * Waits for a DMA operation to complete
1020 * @param mmc mmc device
1021 * @param timeout timeout in ms
1022 * @param verbose true to print out error information
1024 * @return 0 for success (could be DMA errors), -ETIMEDOUT on timeout
1025 * or -EIO if IO error.
1027 static int octeontx_mmc_wait_dma(struct mmc *mmc, bool write, ulong timeout,
1030 struct octeontx_mmc_host *host = mmc_to_host(mmc);
1031 ulong start_time = get_timer(0);
1032 union mio_emm_dma_int emm_dma_int;
1033 union mio_emm_rsp_sts rsp_sts;
1034 union mio_emm_dma emm_dma;
1035 bool timed_out = false;
1038 debug("%s(%s, %lu, %d), delay: %uus\n", __func__, mmc->dev->name,
1039 timeout, verbose, host->dma_wait_delay);
1041 udelay(host->dma_wait_delay);
1043 emm_dma_int.u = read_csr(mmc, MIO_EMM_DMA_INT());
1044 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1046 if ((rsp_sts.s.dma_pend && !rsp_sts.s.dma_val) ||
1047 rsp_sts.s.blk_timeout ||
1048 rsp_sts.s.stp_timeout ||
1049 rsp_sts.s.rsp_timeout) {
1052 debug("%s: f1\n", __func__);
1053 octeontx_mmc_print_rsp_errors(mmc, rsp_sts);
1058 if (rsp_sts.s.blk_crc_err ||
1059 (rsp_sts.s.dma_pend && !rsp_sts.s.dma_val)) {
1062 octeontx_mmc_print_rsp_errors(mmc, rsp_sts);
1067 if (rsp_sts.s.dma_pend) {
1069 * If this is set then an error has occurred.
1070 * Try and restart the DMA operation.
1072 emm_dma.u = read_csr(mmc, MIO_EMM_DMA());
1074 pr_err("%s(%s): DMA pending error: rsp_sts: 0x%llx, dma_int: 0x%llx, emm_dma: 0x%llx\n",
1075 __func__, mmc->dev->name, rsp_sts.u,
1076 emm_dma_int.u, emm_dma.u);
1077 octeontx_print_rsp_sts(mmc);
1078 debug(" MIO_EMM_DEBUG: 0x%llx\n",
1079 read_csr(mmc, MIO_EMM_DEBUG()));
1080 pr_err("%s: Trying DMA resume...\n", __func__);
1082 emm_dma.s.dma_val = 1;
1083 emm_dma.s.dat_null = 1;
1084 write_csr(mmc, MIO_EMM_DMA(), emm_dma.u);
1086 } else if (!rsp_sts.s.dma_val && emm_dma_int.s.done) {
1090 timed_out = (get_timer(start_time) > timeout);
1091 } while (!timed_out);
1093 if (timed_out || err) {
1095 pr_err("%s(%s): MMC DMA %s after %lu ms, rsp_sts: 0x%llx, dma_int: 0x%llx, rsp_sts_lo: 0x%llx, emm_dma: 0x%llx\n",
1096 __func__, mmc->dev->name,
1097 timed_out ? "timed out" : "error",
1098 get_timer(start_time), rsp_sts.u,
1100 read_csr(mmc, MIO_EMM_RSP_LO()),
1101 read_csr(mmc, MIO_EMM_DMA()));
1102 octeontx_print_rsp_sts(mmc);
1104 if (rsp_sts.s.dma_pend)
1105 octeontx_mmc_cleanup_dma(mmc, rsp_sts);
1107 write_csr(mmc, MIO_EMM_DMA_INT(),
1108 read_csr(mmc, MIO_EMM_DMA_INT()));
1111 return timed_out ? -ETIMEDOUT : (err ? -EIO : 0);
1115 * Read blocks from the MMC/SD device
1117 * @param mmc mmc device
1118 * @param cmd command
1119 * @param data data for read
1120 * @param verbose true to print out error information
1122 * @return number of blocks read or 0 if error
1124 static int octeontx_mmc_read_blocks(struct mmc *mmc, struct mmc_cmd *cmd,
1125 struct mmc_data *data, bool verbose)
1127 struct octeontx_mmc_host *host = mmc_to_host(mmc);
1128 union mio_emm_rsp_sts rsp_sts;
1129 dma_addr_t dma_addr = (dma_addr_t)dm_pci_virt_to_mem(host->dev,
1132 ulong blkcnt = data->blocks;
1133 ulong start = cmd->cmdarg;
1134 int timeout = 1000 + blkcnt * 20;
1135 bool timed_out = false;
1136 bool multi_xfer = cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK;
1138 debug("%s(%s): dest: %p, dma address: 0x%llx, blkcnt: %lu, start: %lu\n",
1139 __func__, mmc->dev->name, data->dest, dma_addr, blkcnt, start);
1140 debug("%s: rsp_sts: 0x%llx\n", __func__,
1141 read_csr(mmc, MIO_EMM_RSP_STS()));
1142 /* use max timeout for multi-block transfers */
1146 * If we have a valid SD card in the slot, we set the response bit
1147 * mask to check for CRC errors and timeouts only.
1148 * Otherwise, use the default power on reset value.
1150 write_csr(mmc, MIO_EMM_STS_MASK(),
1151 IS_SD(mmc) ? 0x00b00000ull : 0xe4390080ull);
1152 invalidate_dcache_range((u64)data->dest,
1153 (u64)data->dest + blkcnt * data->blocksize);
1156 octeontx_mmc_start_dma(mmc, false, false, start, dma_addr,
1158 timed_out = !!octeontx_mmc_wait_dma(mmc, false, timeout,
1160 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1161 if (timed_out || rsp_sts.s.dma_val || rsp_sts.s.dma_pend) {
1165 pr_err("%s(%s): Error: DMA timed out. rsp_sts: 0x%llx, emm_int: 0x%llx, dma_int: 0x%llx, rsp_lo: 0x%llx\n",
1166 __func__, mmc->dev->name, rsp_sts.u,
1167 read_csr(mmc, MIO_EMM_INT()),
1168 read_csr(mmc, MIO_EMM_DMA_INT()),
1169 read_csr(mmc, MIO_EMM_RSP_LO()));
1170 pr_err("%s: block count: %lu, start: 0x%lx\n",
1171 __func__, blkcnt, start);
1172 octeontx_mmc_print_registers(mmc);
1179 octeontx_mmc_start_dma(mmc, false, false, start,
1180 dma_addr, 1, timeout);
1181 dma_addr += mmc->read_bl_len;
1184 timed_out = !!octeontx_mmc_wait_dma(mmc, false,
1186 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1187 if (timed_out || rsp_sts.s.dma_val ||
1188 rsp_sts.s.dma_pend) {
1190 pr_err("%s: Error: DMA timed out. rsp_sts: 0x%llx, emm_int: 0x%llx, dma_int: 0x%llx, rsp_lo: 0x%llx\n",
1191 __func__, rsp_sts.u,
1192 read_csr(mmc, MIO_EMM_INT()),
1193 read_csr(mmc, MIO_EMM_DMA_INT()),
1194 read_csr(mmc, MIO_EMM_RSP_LO()));
1195 pr_err("%s: block count: 1, start: 0x%lx\n",
1197 octeontx_mmc_print_registers(mmc);
1199 return blkcnt - count;
1205 debug("%s(%s): Read %lu (0x%lx) blocks starting at block %u (0x%x) to address %p (dma address 0x%llx)\n",
1206 __func__, mmc->dev->name, blkcnt, blkcnt,
1207 cmd->cmdarg, cmd->cmdarg, data->dest,
1208 dm_pci_virt_to_mem(host->dev, data->dest));
1209 print_buffer(0, data->dest, 1, 0x200, 0);
1214 static int octeontx_mmc_poll_ready(struct mmc *mmc, ulong timeout)
1219 bool not_ready = false;
1221 memset(&cmd, 0, sizeof(cmd));
1222 cmd.cmdidx = MMC_CMD_SEND_STATUS;
1223 cmd.cmdarg = mmc->rca << 16;
1224 cmd.resp_type = MMC_RSP_R1;
1225 start = get_timer(0);
1227 err = octeontx_mmc_send_cmd(mmc, &cmd, NULL);
1229 pr_err("%s(%s): MMC command error: %d; Retry...\n",
1230 __func__, mmc->dev->name, err);
1232 } else if (cmd.response[0] & R1_READY_FOR_DATA) {
1236 } while (get_timer(start) < timeout);
1239 pr_err("%s(%s): MMC command error; Retry timeout\n",
1240 __func__, mmc->dev->name);
1244 static ulong octeontx_mmc_write_blocks(struct mmc *mmc, struct mmc_cmd *cmd,
1245 struct mmc_data *data)
1247 struct octeontx_mmc_host *host = mmc_to_host(mmc);
1248 ulong start = cmd->cmdarg;
1249 ulong blkcnt = data->blocks;
1250 dma_addr_t dma_addr;
1251 union mio_emm_rsp_sts rsp_sts;
1252 union mio_emm_sts_mask emm_sts_mask;
1255 bool timed_out = false;
1256 bool multi_xfer = (blkcnt > 1) &&
1257 ((IS_SD(mmc) && mmc->scr[0] & 2) || !IS_SD(mmc));
1259 octeontx_mmc_switch_to(mmc);
1261 emm_sts_mask.s.sts_msk = R1_BLOCK_WRITE_MASK;
1262 write_csr(mmc, MIO_EMM_STS_MASK(), emm_sts_mask.u);
1264 if (octeontx_mmc_poll_ready(mmc, 10000)) {
1265 pr_err("%s(%s): Ready timed out\n", __func__, mmc->dev->name);
1268 flush_dcache_range((u64)data->src,
1269 (u64)data->src + blkcnt * mmc->write_bl_len);
1270 dma_addr = (u64)dm_pci_virt_to_mem(host->dev, (void *)data->src);
1272 timeout = 5000 + 100 * blkcnt;
1273 octeontx_mmc_start_dma(mmc, true, false, start, dma_addr,
1275 timed_out = !!octeontx_mmc_wait_dma(mmc, true, timeout, true);
1276 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1277 if (timed_out || rsp_sts.s.dma_val || rsp_sts.s.dma_pend) {
1278 pr_err("%s(%s): Error: multi-DMA timed out after %lums. rsp_sts: 0x%llx, emm_int: 0x%llx, emm_dma_int: 0x%llx, rsp_sts_lo: 0x%llx, emm_dma: 0x%llx\n",
1279 __func__, mmc->dev->name, timeout,
1281 read_csr(mmc, MIO_EMM_INT()),
1282 read_csr(mmc, MIO_EMM_DMA_INT()),
1283 read_csr(mmc, MIO_EMM_RSP_LO()),
1284 read_csr(mmc, MIO_EMM_DMA()));
1291 octeontx_mmc_start_dma(mmc, true, false, start,
1292 dma_addr, 1, timeout);
1293 dma_addr += mmc->read_bl_len;
1296 timed_out = !!octeontx_mmc_wait_dma(mmc, true, timeout,
1298 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1299 if (timed_out || rsp_sts.s.dma_val ||
1300 rsp_sts.s.dma_pend) {
1301 pr_err("%s(%s): Error: single-DMA timed out after %lums. rsp_sts: 0x%llx, emm_int: 0x%llx, emm_dma_int: 0x%llx, rsp_sts_lo: 0x%llx, emm_dma: 0x%llx\n",
1302 __func__, mmc->dev->name, timeout,
1304 read_csr(mmc, MIO_EMM_RSP_STS()),
1305 read_csr(mmc, MIO_EMM_DMA_INT()),
1306 read_csr(mmc, MIO_EMM_RSP_LO()),
1307 read_csr(mmc, MIO_EMM_DMA()));
1308 return blkcnt - count;
1318 * Send a command to the eMMC/SD device
1320 * @param mmc mmc device
1321 * @param cmd cmd to send and response
1322 * @param data additional data
1324 * @return 0 for success, otherwise error
1326 static int octeontx_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1327 struct mmc_data *data)
1329 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
1330 const char *name = slot->dev->name;
1331 struct octeontx_mmc_cr_mods mods = {0, 0};
1332 union mio_emm_rsp_sts rsp_sts;
1333 union mio_emm_cmd emm_cmd;
1334 union mio_emm_rsp_lo rsp_lo;
1335 union mio_emm_buf_idx emm_buf_idx;
1336 union mio_emm_buf_dat emm_buf_dat;
1342 * This constant has a 1 bit for each command which should have a short
1343 * timeout and a 0 for each bit with a long timeout. Currently the
1344 * following commands have a long timeout:
1345 * CMD6, CMD17, CMD18, CMD24, CMD25, CMD32, CMD33, CMD35, CMD36 and
1348 static const u64 timeout_short = 0xFFFFFFA4FCF9FFDFull;
1351 if (cmd->cmdidx == MMC_CMD_SEND_EXT_CSD) {
1352 union mio_emm_rca emm_rca;
1355 emm_rca.s.card_rca = mmc->rca;
1356 write_csr(mmc, MIO_EMM_RCA(), emm_rca.u);
1359 if (timeout_short & (1ull << cmd->cmdidx))
1360 timeout = MMC_TIMEOUT_SHORT;
1361 else if (cmd->cmdidx == MMC_CMD_SWITCH && IS_SD(mmc))
1363 else if (cmd->cmdidx == MMC_CMD_ERASE)
1364 timeout = MMC_TIMEOUT_ERASE;
1366 timeout = MMC_TIMEOUT_LONG;
1368 debug("%s(%s): cmd idx: %u, arg: 0x%x, resp type: 0x%x, timeout: %u\n",
1369 __func__, name, cmd->cmdidx, cmd->cmdarg, cmd->resp_type,
1372 debug(" data: addr: %p, flags: 0x%x, blocks: %u, blocksize: %u\n",
1373 data->dest, data->flags, data->blocks, data->blocksize);
1375 octeontx_mmc_switch_to(mmc);
1377 /* Clear any interrupts */
1378 write_csr(mmc, MIO_EMM_INT(), read_csr(mmc, MIO_EMM_INT()));
1381 * We need to override the default command types and response types
1382 * when dealing with SD cards.
1384 mods = octeontx_mmc_get_cr_mods(mmc, cmd, data);
1386 /* Handle block read/write/stop operations */
1387 switch (cmd->cmdidx) {
1388 case MMC_CMD_GO_IDLE_STATE:
1389 slot->tuned = false;
1390 slot->hs200_tuned = false;
1391 slot->hs400_tuned = false;
1393 case MMC_CMD_STOP_TRANSMISSION:
1395 case MMC_CMD_READ_MULTIPLE_BLOCK:
1396 case MMC_CMD_READ_SINGLE_BLOCK:
1397 pr_debug("%s(%s): Reading blocks\n", __func__, name);
1398 blkcnt = octeontx_mmc_read_blocks(mmc, cmd, data, true);
1399 return (blkcnt > 0) ? 0 : -1;
1400 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
1401 case MMC_CMD_WRITE_SINGLE_BLOCK:
1402 blkcnt = octeontx_mmc_write_blocks(mmc, cmd, data);
1403 return (blkcnt > 0) ? 0 : -1;
1404 case MMC_CMD_SELECT_CARD:
1405 /* Set the RCA register (is it set automatically?) */
1407 union mio_emm_rca emm_rca;
1410 emm_rca.s.card_rca = (cmd->cmdarg >> 16);
1411 write_csr(mmc, MIO_EMM_RCA(), emm_rca.u);
1412 debug("%s: Set SD relative address (RCA) to 0x%x\n",
1413 __func__, emm_rca.s.card_rca);
1417 case MMC_CMD_SWITCH:
1418 if (!data && !slot->is_acmd)
1419 octeontx_mmc_track_switch(mmc, cmd->cmdarg);
1424 emm_cmd.s.cmd_val = 1;
1425 emm_cmd.s.bus_id = slot->bus_id;
1426 emm_cmd.s.cmd_idx = cmd->cmdidx;
1427 emm_cmd.s.arg = cmd->cmdarg;
1428 emm_cmd.s.ctype_xor = mods.ctype_xor;
1429 emm_cmd.s.rtype_xor = mods.rtype_xor;
1430 if (data && data->blocks == 1 && data->blocksize != 512) {
1432 64 - ((data->blocks * data->blocksize) / 8);
1433 debug("%s: offset set to %u\n", __func__, emm_cmd.s.offset);
1436 if (data && data->flags & MMC_DATA_WRITE) {
1437 u8 *src = (u8 *)data->src;
1440 pr_err("%s(%s): Error: data source for cmd 0x%x is NULL!\n",
1441 __func__, name, cmd->cmdidx);
1444 if (data->blocksize > 512) {
1445 pr_err("%s(%s): Error: data for cmd 0x%x exceeds 512 bytes\n",
1446 __func__, name, cmd->cmdidx);
1450 debug("%s: Sending %d bytes data\n", __func__, data->blocksize);
1451 print_buffer(0, src, 1, data->blocksize, 0);
1454 emm_buf_idx.s.inc = 1;
1455 write_csr(mmc, MIO_EMM_BUF_IDX(), emm_buf_idx.u);
1456 for (i = 0; i < (data->blocksize + 7) / 8; i++) {
1457 memcpy(&emm_buf_dat.u, src, sizeof(emm_buf_dat.u));
1458 write_csr(mmc, MIO_EMM_BUF_DAT(),
1459 cpu_to_be64(emm_buf_dat.u));
1460 src += sizeof(emm_buf_dat.u);
1462 write_csr(mmc, MIO_EMM_BUF_IDX(), 0);
1464 debug("%s(%s): Sending command %u (emm_cmd: 0x%llx)\n", __func__,
1465 name, cmd->cmdidx, emm_cmd.u);
1466 set_wdog(mmc, timeout * 1000);
1467 write_csr(mmc, MIO_EMM_CMD(), emm_cmd.u);
1469 /* Wait for command to finish or time out */
1470 start = get_timer(0);
1472 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1474 } while (!rsp_sts.s.cmd_done && !rsp_sts.s.rsp_timeout &&
1475 (get_timer(start) < timeout + 10));
1476 octeontx_mmc_print_rsp_errors(mmc, rsp_sts);
1477 if (rsp_sts.s.rsp_timeout || !rsp_sts.s.cmd_done) {
1478 debug("%s(%s): Error: command %u(0x%x) timed out. rsp_sts: 0x%llx\n",
1479 __func__, name, cmd->cmdidx, cmd->cmdarg, rsp_sts.u);
1480 octeontx_mmc_print_registers(mmc);
1483 if (rsp_sts.s.rsp_crc_err) {
1484 debug("%s(%s): RSP CRC error, rsp_sts: 0x%llx, cmdidx: %u, arg: 0x%08x\n",
1485 __func__, name, rsp_sts.u, cmd->cmdidx, cmd->cmdarg);
1486 octeontx_mmc_print_registers(mmc);
1489 if (slot->bus_id != rsp_sts.s.bus_id) {
1490 pr_warn("%s(%s): bus id mismatch, got %d, expected %d for command 0x%x(0x%x)\n",
1492 rsp_sts.s.bus_id, slot->bus_id,
1493 cmd->cmdidx, cmd->cmdarg);
1496 if (rsp_sts.s.rsp_bad_sts) {
1497 rsp_lo.u = read_csr(mmc, MIO_EMM_RSP_LO());
1498 debug("%s: Bad response for bus id %d, cmd id %d:\n"
1499 " rsp_timeout: %d\n"
1500 " rsp_bad_sts: %d\n"
1501 " rsp_crc_err: %d\n",
1502 __func__, slot->bus_id, cmd->cmdidx,
1503 rsp_sts.s.rsp_timeout,
1504 rsp_sts.s.rsp_bad_sts,
1505 rsp_sts.s.rsp_crc_err);
1506 if (rsp_sts.s.rsp_type == 1 && rsp_sts.s.rsp_bad_sts) {
1507 debug(" Response status: 0x%llx\n",
1508 (rsp_lo.u >> 8) & 0xffffffff);
1510 mmc_print_status((rsp_lo.u >> 8) & 0xffffffff);
1515 if (rsp_sts.s.cmd_idx != cmd->cmdidx) {
1516 debug("%s(%s): Command response index %d does not match command index %d\n",
1517 __func__, name, rsp_sts.s.cmd_idx, cmd->cmdidx);
1518 octeontx_print_rsp_sts(mmc);
1519 debug("%s: rsp_lo: 0x%llx\n", __func__,
1520 read_csr(mmc, MIO_EMM_RSP_LO()));
1525 slot->is_acmd = (cmd->cmdidx == MMC_CMD_APP_CMD);
1527 if (!cmd->resp_type & MMC_RSP_PRESENT)
1528 debug(" Response type: 0x%x, no response expected\n",
1530 /* Get the response if present */
1531 if (rsp_sts.s.rsp_val && (cmd->resp_type & MMC_RSP_PRESENT)) {
1532 union mio_emm_rsp_hi rsp_hi;
1534 rsp_lo.u = read_csr(mmc, MIO_EMM_RSP_LO());
1536 switch (rsp_sts.s.rsp_type) {
1541 cmd->response[0] = (rsp_lo.u >> 8) & 0xffffffffull;
1542 debug(" response: 0x%08x\n",
1544 cmd->response[1] = 0;
1545 cmd->response[2] = 0;
1546 cmd->response[3] = 0;
1549 cmd->response[3] = rsp_lo.u & 0xffffffff;
1550 cmd->response[2] = (rsp_lo.u >> 32) & 0xffffffff;
1551 rsp_hi.u = read_csr(mmc, MIO_EMM_RSP_HI());
1552 cmd->response[1] = rsp_hi.u & 0xffffffff;
1553 cmd->response[0] = (rsp_hi.u >> 32) & 0xffffffff;
1554 debug(" response: 0x%08x 0x%08x 0x%08x 0x%08x\n",
1555 cmd->response[0], cmd->response[1],
1556 cmd->response[2], cmd->response[3]);
1559 pr_err("%s(%s): Unknown response type 0x%x for command %d, arg: 0x%x, rsp_sts: 0x%llx\n",
1560 __func__, name, rsp_sts.s.rsp_type, cmd->cmdidx,
1561 cmd->cmdarg, rsp_sts.u);
1565 debug(" Response not expected\n");
1568 if (data && data->flags & MMC_DATA_READ) {
1569 u8 *dest = (u8 *)data->dest;
1572 pr_err("%s(%s): Error, destination buffer NULL!\n",
1573 __func__, mmc->dev->name);
1576 if (data->blocksize > 512) {
1577 printf("%s(%s): Error: data size %u exceeds 512\n",
1578 __func__, mmc->dev->name,
1583 emm_buf_idx.s.inc = 1;
1584 write_csr(mmc, MIO_EMM_BUF_IDX(), emm_buf_idx.u);
1585 for (i = 0; i < (data->blocksize + 7) / 8; i++) {
1586 emm_buf_dat.u = read_csr(mmc, MIO_EMM_BUF_DAT());
1587 emm_buf_dat.u = be64_to_cpu(emm_buf_dat.u);
1588 memcpy(dest, &emm_buf_dat.u, sizeof(emm_buf_dat.u));
1589 dest += sizeof(emm_buf_dat.u);
1591 write_csr(mmc, MIO_EMM_BUF_IDX(), 0);
1593 debug("%s: Received %d bytes data\n", __func__,
1595 print_buffer(0, data->dest, 1, data->blocksize, 0);
1602 octeontx_mmc_print_registers(mmc);
1607 static int octeontx_mmc_dev_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1608 struct mmc_data *data)
1610 return octeontx_mmc_send_cmd(dev_to_mmc(dev), cmd, data);
1613 #ifdef MMC_SUPPORTS_TUNING
1614 static int octeontx_mmc_test_cmd(struct mmc *mmc, u32 opcode, int *statp)
1619 memset(&cmd, 0, sizeof(cmd));
1621 debug("%s(%s, %u, %p)\n", __func__, mmc->dev->name, opcode, statp);
1622 cmd.cmdidx = opcode;
1623 cmd.resp_type = MMC_RSP_R1;
1624 cmd.cmdarg = mmc->rca << 16;
1626 err = octeontx_mmc_send_cmd(mmc, &cmd, NULL);
1628 debug("%s(%s, %u) returned %d\n", __func__,
1629 mmc->dev->name, opcode, err);
1631 *statp = cmd.response[0];
1635 static int octeontx_mmc_test_get_ext_csd(struct mmc *mmc, u32 opcode,
1639 struct mmc_data data;
1641 u8 ext_csd[MMC_MAX_BLOCK_LEN];
1643 debug("%s(%s, %u, %p)\n", __func__, mmc->dev->name, opcode, statp);
1644 memset(&cmd, 0, sizeof(cmd));
1646 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
1647 cmd.resp_type = MMC_RSP_R1;
1650 data.dest = (char *)ext_csd;
1652 data.blocksize = MMC_MAX_BLOCK_LEN;
1653 data.flags = MMC_DATA_READ;
1655 err = octeontx_mmc_send_cmd(mmc, &cmd, &data);
1657 *statp = cmd.response[0];
1663 * Wrapper to set the MIO_EMM_TIMING register
1665 * @param mmc pointer to mmc data structure
1666 * @param emm_timing New emm_timing register value
1668 * On some devices it is possible that changing the data out value can
1669 * cause a glitch on an internal fifo. This works around this problem
1670 * by performing a soft-reset immediately before setting the timing register.
1672 * Note: this function should not be called from any function that
1673 * performs DMA or block operations since not all registers are
1676 static void octeontx_mmc_set_emm_timing(struct mmc *mmc,
1677 union mio_emm_timing emm_timing)
1679 union mio_emm_cfg emm_cfg;
1680 struct octeontx_mmc_slot *slot = mmc->priv;
1681 union mio_emm_debug emm_debug;
1683 debug("%s(%s, 0x%llx) din: %u\n", __func__, mmc->dev->name,
1684 emm_timing.u, emm_timing.s.data_in_tap);
1687 if (slot->host->tap_requires_noclk) {
1688 /* Turn off the clock */
1689 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
1690 emm_debug.s.emmc_clk_disable = 1;
1691 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
1693 emm_debug.s.rdsync_rst = 1;
1694 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
1696 emm_cfg.u = read_csr(mmc, MIO_EMM_CFG());
1697 emm_cfg.s.bus_ena = 1 << 3;
1698 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
1701 write_csr(mmc, MIO_EMM_TIMING(), emm_timing.u);
1704 if (slot->host->tap_requires_noclk) {
1705 /* Turn on the clock */
1706 emm_debug.s.rdsync_rst = 0;
1707 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
1709 emm_debug.s.emmc_clk_disable = 0;
1710 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
1713 emm_cfg.s.bus_ena = 1 << mmc_to_slot(mmc)->bus_id;
1714 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
1717 static const u8 octeontx_hs400_tuning_block[512] = {
1718 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
1719 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
1720 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
1721 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
1722 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
1723 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
1724 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
1725 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
1726 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
1727 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
1728 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
1729 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
1730 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
1731 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
1732 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
1733 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
1734 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
1735 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
1736 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
1737 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
1738 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
1739 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
1740 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
1741 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
1742 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
1743 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
1744 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
1745 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
1746 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
1747 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
1748 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
1749 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
1750 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
1751 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
1752 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
1753 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
1754 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
1755 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
1756 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
1757 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
1758 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
1759 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
1760 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
1761 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
1762 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
1763 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
1764 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
1765 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
1766 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00,
1767 0x00, 0xff, 0x00, 0xff, 0x55, 0xaa, 0x55, 0xaa,
1768 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
1769 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
1770 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
1771 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
1772 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
1773 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
1774 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00,
1775 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
1776 0x01, 0xfe, 0x01, 0xfe, 0xcc, 0xcc, 0xcc, 0xff,
1777 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
1778 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
1779 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
1780 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
1781 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
1786 * Perform tuning in HS400 mode
1788 * @param[in] mmc mmc data structure
1790 * @ret 0 for success, otherwise error
1792 static int octeontx_tune_hs400(struct mmc *mmc)
1794 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
1796 struct mmc_data data;
1797 union mio_emm_timing emm_timing;
1798 u8 buffer[mmc->read_bl_len];
1805 int best_start = -1;
1806 bool prev_ok = false;
1808 char how[MAX_NO_OF_TAPS + 1] = "";
1810 if (slot->hs400_tuning_block == -1)
1813 /* The eMMC standard disables all tuning support when operating in
1814 * DDR modes like HS400. The problem with this is that there are
1815 * many cases where the HS200 tuning does not work for HS400 mode.
1816 * In order to perform this tuning, while in HS200 a block is written
1817 * to a block specified in the device tree (marvell,hs400-tuning-block)
1818 * which is used for tuning in this function by repeatedly reading
1819 * this block and comparing the data and return code. This function
1820 * chooses the data input tap in the middle of the longest run of
1821 * successful read operations.
1824 emm_timing = slot->hs200_taps;
1825 debug("%s(%s): Start ci: %d, co: %d, di: %d, do: %d\n",
1826 __func__, mmc->dev->name, emm_timing.s.cmd_in_tap,
1827 emm_timing.s.cmd_out_tap, emm_timing.s.data_in_tap,
1828 emm_timing.s.data_out_tap);
1829 memset(buffer, 0xdb, sizeof(buffer));
1831 snprintf(env_name, sizeof(env_name), "emmc%d_data_in_tap_hs400",
1833 tap = env_get_ulong(env_name, 10, -1L);
1834 if (tap >= 0 && tap < MAX_NO_OF_TAPS) {
1835 printf("Overriding data input tap for HS400 mode to %d\n", tap);
1836 emm_timing.s.data_in_tap = tap;
1837 octeontx_mmc_set_emm_timing(mmc, emm_timing);
1841 for (tap = 0; tap <= MAX_NO_OF_TAPS; tap++, prev_ok = !err) {
1842 if (tap < MAX_NO_OF_TAPS) {
1843 debug("%s: Testing data in tap %d\n", __func__, tap);
1844 emm_timing.s.data_in_tap = tap;
1845 octeontx_mmc_set_emm_timing(mmc, emm_timing);
1847 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
1848 cmd.cmdarg = slot->hs400_tuning_block;
1849 cmd.resp_type = MMC_RSP_R1;
1850 data.dest = (void *)buffer;
1852 data.blocksize = mmc->read_bl_len;
1853 data.flags = MMC_DATA_READ;
1854 err = !octeontx_mmc_read_blocks(mmc, &cmd, &data,
1856 if (err || memcmp(buffer, octeontx_hs400_tuning_block,
1860 debug("%s: data mismatch. Read:\n",
1862 print_buffer(0, buffer, 1,
1864 debug("\nExpected:\n");
1866 octeontx_hs400_tuning_block, 1,
1867 sizeof(octeontx_hs400_tuning_block),
1870 debug("%s: Error %d reading block\n",
1876 debug("%s: tap %d good\n", __func__, tap);
1878 how[tap] = "-+"[!err];
1886 } else if (prev_ok) {
1887 run = tap - 1 - start_run;
1888 if (start_run >= 0 && run > best_run) {
1889 best_start = start_run;
1895 how[tap - 1] = '\0';
1896 if (best_start < 0) {
1897 printf("%s(%s): %lldMHz tuning failed for HS400\n",
1898 __func__, mmc->dev->name, slot->clock / 1000000);
1901 tap = best_start + best_run / 2;
1903 snprintf(env_name, sizeof(env_name), "emmc%d_data_in_tap_adj_hs400",
1905 tap_adj = env_get_ulong(env_name, 10, slot->hs400_tap_adj);
1907 * Keep it in range and if out of range force it back in with a small
1911 tap = tap + tap_adj;
1912 if (tap >= best_start + best_run)
1913 tap = best_start + best_run - 2;
1914 if (tap <= best_start)
1915 tap = best_start + 2;
1918 debug("Tuning: %s\n", how);
1919 debug("%s(%s): HS400 tap: best run start: %d, length: %d, tap: %d\n",
1920 __func__, mmc->dev->name, best_start, best_run, tap);
1921 slot->hs400_taps = slot->hs200_taps;
1922 slot->hs400_taps.s.data_in_tap = tap;
1923 slot->hs400_tuned = true;
1924 if (env_get_yesno("emmc_export_hs400_taps") > 0) {
1925 debug("%s(%s): Exporting HS400 taps\n",
1926 __func__, mmc->dev->name);
1927 env_set_ulong("emmc_timing_tap", slot->host->timing_taps);
1928 snprintf(env_name, sizeof(env_name),
1929 "emmc%d_hs400_data_in_tap_debug",
1931 env_set(env_name, how);
1932 snprintf(env_name, sizeof(env_name),
1933 "emmc%d_hs400_data_in_tap_val",
1935 env_set_ulong(env_name, tap);
1936 snprintf(env_name, sizeof(env_name),
1937 "emmc%d_hs400_data_in_tap_start",
1939 env_set_ulong(env_name, best_start);
1940 snprintf(env_name, sizeof(env_name),
1941 "emmc%d_hs400_data_in_tap_end",
1943 env_set_ulong(env_name, best_start + best_run);
1944 snprintf(env_name, sizeof(env_name),
1945 "emmc%d_hs400_cmd_in_tap",
1947 env_set_ulong(env_name, slot->hs400_taps.s.cmd_in_tap);
1948 snprintf(env_name, sizeof(env_name),
1949 "emmc%d_hs400_cmd_out_tap",
1951 env_set_ulong(env_name, slot->hs400_taps.s.cmd_out_tap);
1952 snprintf(env_name, sizeof(env_name),
1953 "emmc%d_hs400_cmd_out_delay",
1955 env_set_ulong(env_name, slot->cmd_out_hs400_delay);
1956 snprintf(env_name, sizeof(env_name),
1957 "emmc%d_hs400_data_out_tap",
1959 env_set_ulong(env_name, slot->hs400_taps.s.data_out_tap);
1960 snprintf(env_name, sizeof(env_name),
1961 "emmc%d_hs400_data_out_delay",
1963 env_set_ulong(env_name, slot->data_out_hs400_delay);
1965 debug("%s(%s): HS400 environment export disabled\n",
1966 __func__, mmc->dev->name);
1968 octeontx_mmc_set_timing(mmc);
1976 int (*test)(struct mmc *mmc, u32 opcode, int *error);
1980 bool not_hs200_only;
1984 struct adj adj[] = {
1985 { "CMD_IN", 48, octeontx_mmc_test_cmd, MMC_CMD_SEND_STATUS,
1986 false, false, false, 2, },
1987 /* { "CMD_OUT", 32, octeontx_mmc_test_cmd, MMC_CMD_SEND_STATUS, },*/
1988 { "DATA_IN(HS200)", 16, mmc_send_tuning,
1989 MMC_CMD_SEND_TUNING_BLOCK_HS200, false, true, false, 2, },
1990 { "DATA_IN", 16, octeontx_mmc_test_get_ext_csd, 0, false, false,
1992 /* { "DATA_OUT", 0, octeontx_mmc_test_cmd, 0, true, false},*/
1997 * Perform tuning tests to find optimal timing
1999 * @param mmc mmc device
2000 * @param adj parameter to tune
2001 * @param opcode command opcode to use
2003 * @return 0 for success, -1 if tuning failed
2005 static int octeontx_mmc_adjust_tuning(struct mmc *mmc, struct adj *adj,
2008 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2009 union mio_emm_timing timing;
2010 union mio_emm_debug emm_debug;
2017 int best_start = -1;
2018 bool prev_ok = false;
2020 const int tap_adj = slot->hs200_tap_adj;
2021 char how[MAX_NO_OF_TAPS + 1] = "";
2022 bool is_hs200 = mmc->selected_mode == MMC_HS_200;
2024 debug("%s(%s, %s, %d), hs200: %d\n", __func__, mmc->dev->name,
2025 adj->name, opcode, is_hs200);
2026 octeontx_mmc_set_emm_timing(mmc,
2027 is_hs200 ? slot->hs200_taps : slot->taps);
2030 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
2031 printf("%s(%s): Before tuning %s, opcode: %d\n",
2032 __func__, mmc->dev->name, adj->name, opcode);
2033 octeontx_mmc_print_registers2(mmc, NULL);
2038 * The algorithm to find the optimal timing is to start
2039 * at the end and work backwards and select the second
2040 * value that passes. Each test is repeated twice.
2042 for (tap = 0; tap <= MAX_NO_OF_TAPS; tap++, prev_ok = !err) {
2043 if (tap < MAX_NO_OF_TAPS) {
2044 if (slot->host->tap_requires_noclk) {
2045 /* Turn off the clock */
2046 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
2047 emm_debug.s.emmc_clk_disable = 1;
2048 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2050 emm_debug.s.rdsync_rst = 1;
2051 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2055 timing.u = read_csr(mmc, MIO_EMM_TIMING());
2056 timing.u &= ~(0x3full << adj->mask_shift);
2057 timing.u |= (u64)tap << adj->mask_shift;
2058 write_csr(mmc, MIO_EMM_TIMING(), timing.u);
2059 debug("%s(%s): Testing ci: %d, co: %d, di: %d, do: %d\n",
2060 __func__, mmc->dev->name, timing.s.cmd_in_tap,
2061 timing.s.cmd_out_tap, timing.s.data_in_tap,
2062 timing.s.data_out_tap);
2064 if (slot->host->tap_requires_noclk) {
2065 /* Turn off the clock */
2066 emm_debug.s.rdsync_rst = 0;
2067 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2069 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
2070 emm_debug.s.emmc_clk_disable = 0;
2071 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2074 for (count = 0; count < 2; count++) {
2075 err = adj->test(mmc, opcode, NULL);
2077 debug("%s(%s, %s): tap %d failed, count: %d, rsp_sts: 0x%llx, rsp_lo: 0x%llx\n",
2078 __func__, mmc->dev->name,
2079 adj->name, tap, count,
2084 debug("%s(%s, %s): tap: %d, do: %d, di: %d, co: %d, ci: %d\n",
2085 __func__, mmc->dev->name,
2087 timing.s.data_out_tap,
2088 timing.s.data_in_tap,
2089 timing.s.cmd_out_tap,
2090 timing.s.cmd_in_tap);
2093 debug("%s(%s, %s): tap %d passed, count: %d, rsp_sts: 0x%llx, rsp_lo: 0x%llx\n",
2094 __func__, mmc->dev->name, adj->name, tap,
2096 read_csr(mmc, MIO_EMM_RSP_STS()),
2097 read_csr(mmc, MIO_EMM_RSP_LO()));
2099 tap_status |= (u64)(!err) << tap;
2100 how[tap] = "-+"[!err];
2103 * Putting the end+1 case in the loop simplifies
2104 * logic, allowing 'prev_ok' to process a sweet
2105 * spot in tuning which extends to the wall.
2111 * If no CRC/etc errors in the response, but previous
2112 * failed, note the start of a new run.
2114 debug(" prev_ok: %d\n", prev_ok);
2117 } else if (prev_ok) {
2118 run = tap - 1 - start_run;
2119 /* did we just exit a wider sweet spot? */
2120 if (start_run >= 0 && run > best_run) {
2121 best_start = start_run;
2126 how[tap - 1] = '\0';
2127 if (best_start < 0) {
2128 printf("%s(%s, %s): %lldMHz tuning %s failed\n", __func__,
2129 mmc->dev->name, adj->name, slot->clock / 1000000,
2134 tap = best_start + best_run / 2;
2135 debug(" tap %d is center, start: %d, run: %d\n", tap,
2136 best_start, best_run);
2138 slot->hs200_taps.u &= ~(0x3full << adj->mask_shift);
2139 slot->hs200_taps.u |= (u64)tap << adj->mask_shift;
2141 slot->taps.u &= ~(0x3full << adj->mask_shift);
2142 slot->taps.u |= (u64)tap << adj->mask_shift;
2144 if (best_start < 0) {
2145 printf("%s(%s, %s): %lldMHz tuning %s failed\n", __func__,
2146 mmc->dev->name, adj->name, slot->clock / 1000000,
2151 tap = best_start + best_run / 2;
2152 if (is_hs200 && (tap + tap_adj >= 0) && (tap + tap_adj < 64) &&
2153 tap_status & (1ULL << (tap + tap_adj))) {
2154 debug("Adjusting tap from %d by %d to %d\n",
2155 tap, tap_adj, tap + tap_adj);
2159 debug("%s/%s %d/%d/%d %s\n", mmc->dev->name,
2160 adj->name, best_start, tap, best_start + best_run, how);
2163 slot->hs200_taps.u &= ~(0x3full << adj->mask_shift);
2164 slot->hs200_taps.u |= (u64)tap << adj->mask_shift;
2166 slot->taps.u &= ~(0x3full << adj->mask_shift);
2167 slot->taps.u |= (u64)tap << adj->mask_shift;
2171 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
2172 debug("%s(%s, %s): After successful tuning\n",
2173 __func__, mmc->dev->name, adj->name);
2174 debug("%s(%s, %s): tap: %d, new do: %d, di: %d, co: %d, ci: %d\n",
2175 __func__, mmc->dev->name, adj->name, tap,
2176 slot->taps.s.data_out_tap,
2177 slot->taps.s.data_in_tap,
2178 slot->taps.s.cmd_out_tap,
2179 slot->taps.s.cmd_in_tap);
2180 debug("%s(%s, %s): tap: %d, new do HS200: %d, di: %d, co: %d, ci: %d\n",
2181 __func__, mmc->dev->name, adj->name, tap,
2182 slot->hs200_taps.s.data_out_tap,
2183 slot->hs200_taps.s.data_in_tap,
2184 slot->hs200_taps.s.cmd_out_tap,
2185 slot->hs200_taps.s.cmd_in_tap);
2188 octeontx_mmc_set_timing(mmc);
2190 if (is_hs200 && env_get_yesno("emmc_export_hs200_taps")) {
2193 env_set_ulong("emmc_timing_tap", slot->host->timing_taps);
2195 case MMC_CMD_SEND_TUNING_BLOCK:
2196 snprintf(env_name, sizeof(env_name),
2197 "emmc%d_hs200_data_in_tap_debug",
2199 env_set(env_name, how);
2200 snprintf(env_name, sizeof(env_name),
2201 "emmc%d_hs200_data_in_tap_val", slot->bus_id);
2202 env_set_ulong(env_name, tap);
2203 snprintf(env_name, sizeof(env_name),
2204 "emmc%d_hs200_data_in_tap_start",
2206 env_set_ulong(env_name, best_start);
2207 snprintf(env_name, sizeof(env_name),
2208 "emmc%d_hs200_data_in_tap_end",
2210 env_set_ulong(env_name, best_start + best_run);
2212 case MMC_CMD_SEND_STATUS:
2213 snprintf(env_name, sizeof(env_name),
2214 "emmc%d_hs200_cmd_in_tap_debug",
2216 env_set(env_name, how);
2217 snprintf(env_name, sizeof(env_name),
2218 "emmc%d_hs200_cmd_in_tap_val", slot->bus_id);
2219 env_set_ulong(env_name, tap);
2220 snprintf(env_name, sizeof(env_name),
2221 "emmc%d_hs200_cmd_in_tap_start",
2223 env_set_ulong(env_name, best_start);
2224 snprintf(env_name, sizeof(env_name),
2225 "emmc%d_hs200_cmd_in_tap_end",
2227 env_set_ulong(env_name, best_start + best_run);
2230 snprintf(env_name, sizeof(env_name),
2231 "emmc%d_hs200_data_out_tap", slot->bus_id);
2232 env_set_ulong(env_name, slot->data_out_hs200_delay);
2233 snprintf(env_name, sizeof(env_name),
2234 "emmc%d_hs200_cmd_out_tap", slot->bus_id);
2235 env_set_ulong(env_name, slot->cmd_out_hs200_delay);
2243 static int octeontx_mmc_execute_tuning(struct udevice *dev, u32 opcode)
2245 struct mmc *mmc = dev_to_mmc(dev);
2246 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2247 union mio_emm_timing emm_timing;
2253 pr_info("%s re-tuning, opcode 0x%x\n", dev->name, opcode);
2255 if (slot->is_asim || slot->is_emul)
2258 is_hs200 = (mmc->selected_mode == MMC_HS_200);
2260 slot->hs200_tuned = false;
2261 slot->hs400_tuned = false;
2263 slot->tuned = false;
2265 octeontx_mmc_set_output_bus_timing(mmc);
2266 octeontx_mmc_set_input_bus_timing(mmc);
2267 emm_timing.u = read_csr(mmc, MIO_EMM_TIMING());
2268 if (mmc->selected_mode == MMC_HS_200) {
2269 slot->hs200_taps.s.cmd_out_tap = emm_timing.s.cmd_out_tap;
2270 slot->hs200_taps.s.data_out_tap = emm_timing.s.data_out_tap;
2272 slot->taps.s.cmd_out_tap = emm_timing.s.cmd_out_tap;
2273 slot->taps.s.data_out_tap = emm_timing.s.data_out_tap;
2275 octeontx_mmc_set_input_bus_timing(mmc);
2276 octeontx_mmc_set_output_bus_timing(mmc);
2278 for (a = adj; a->name; a++) {
2281 if (!strcmp(a->name, "CMD_IN")) {
2282 snprintf(env_name, sizeof(env_name),
2283 "emmc%d_cmd_in_tap", slot->bus_id);
2284 in_tap = env_get_ulong(env_name, 10, (ulong)-1);
2285 if (in_tap != (ulong)-1) {
2286 if (mmc->selected_mode == MMC_HS_200 ||
2288 slot->hs200_taps.s.cmd_in_tap = in_tap;
2289 slot->hs400_taps.s.cmd_in_tap = in_tap;
2291 slot->taps.s.cmd_in_tap = in_tap;
2295 } else if (a->hs200_only &&
2296 !strcmp(a->name, "DATA_IN(HS200)")) {
2297 snprintf(env_name, sizeof(env_name),
2298 "emmc%d_data_in_tap_hs200", slot->bus_id);
2299 in_tap = env_get_ulong(env_name, 10, (ulong)-1);
2300 if (in_tap != (ulong)-1) {
2301 debug("%s(%s): Overriding HS200 data in tap to %d\n",
2302 __func__, dev->name, (int)in_tap);
2303 slot->hs200_taps.s.data_in_tap = in_tap;
2306 } else if (!a->hs200_only && !strcmp(a->name, "DATA_IN")) {
2307 snprintf(env_name, sizeof(env_name),
2308 "emmc%d_data_in_tap", slot->bus_id);
2309 in_tap = env_get_ulong(env_name, 10, (ulong)-1);
2310 if (in_tap != (ulong)-1) {
2311 debug("%s(%s): Overriding non-HS200 data in tap to %d\n",
2312 __func__, dev->name, (int)in_tap);
2313 slot->taps.s.data_in_tap = in_tap;
2318 debug("%s(%s): Testing: %s, mode: %s, opcode: %u\n", __func__,
2319 dev->name, a->name, mmc_mode_name(mmc->selected_mode),
2322 /* Skip DDR only test when not in DDR mode */
2323 if (a->ddr_only && !mmc->ddr_mode) {
2324 debug("%s(%s): Skipping %s due to non-DDR mode\n",
2325 __func__, dev->name, a->name);
2328 /* Skip hs200 tests in non-hs200 mode and
2329 * non-hs200 tests in hs200 mode
2332 if (a->not_hs200_only) {
2333 debug("%s(%s): Skipping %s\n", __func__,
2334 dev->name, a->name);
2338 if (a->hs200_only) {
2339 debug("%s(%s): Skipping %s\n", __func__,
2340 dev->name, a->name);
2345 err = octeontx_mmc_adjust_tuning(mmc, a, a->opcode ?
2346 a->opcode : opcode);
2348 pr_err("%s(%s, %u): tuning %s failed\n", __func__,
2349 dev->name, opcode, a->name);
2354 octeontx_mmc_set_timing(mmc);
2356 slot->hs200_tuned = true;
2360 if (slot->hs400_tuning_block != -1) {
2362 struct mmc_data data;
2363 u8 buffer[mmc->read_bl_len];
2365 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
2366 cmd.cmdarg = slot->hs400_tuning_block;
2367 cmd.resp_type = MMC_RSP_R1;
2368 data.dest = (void *)buffer;
2370 data.blocksize = mmc->read_bl_len;
2371 data.flags = MMC_DATA_READ;
2372 err = octeontx_mmc_read_blocks(mmc, &cmd, &data, true) != 1;
2375 printf("%s: Cannot read HS400 tuning block %u\n",
2376 dev->name, slot->hs400_tuning_block);
2379 if (memcmp(buffer, octeontx_hs400_tuning_block,
2381 debug("%s(%s): Writing new HS400 tuning block to block %d\n",
2382 __func__, dev->name, slot->hs400_tuning_block);
2383 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
2384 data.src = (void *)octeontx_hs400_tuning_block;
2385 data.flags = MMC_DATA_WRITE;
2386 err = !octeontx_mmc_write_blocks(mmc, &cmd, &data);
2388 printf("%s: Cannot write HS400 tuning block %u\n",
2389 dev->name, slot->hs400_tuning_block);
2397 #else /* MMC_SUPPORTS_TUNING */
2398 static void octeontx_mmc_set_emm_timing(struct mmc *mmc,
2399 union mio_emm_timing emm_timing)
2402 #endif /* MMC_SUPPORTS_TUNING */
2405 * Calculate the clock period with rounding up
2407 * @param mmc mmc device
2408 * @return clock period in system clocks for clk_lo + clk_hi
2410 static u32 octeontx_mmc_calc_clk_period(struct mmc *mmc)
2412 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2413 struct octeontx_mmc_host *host = slot->host;
2415 return DIV_ROUND_UP(host->sys_freq, mmc->clock);
2418 static int octeontx_mmc_set_ios(struct udevice *dev)
2420 struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
2421 struct mmc *mmc = &slot->mmc;
2422 struct octeontx_mmc_host *host = slot->host;
2423 union mio_emm_switch emm_switch;
2424 union mio_emm_modex mode;
2428 int power_class = 10;
2430 bool is_hs200 = false;
2431 bool is_hs400 = false;
2433 debug("%s(%s): Entry\n", __func__, dev->name);
2434 debug(" clock: %u, bus width: %u, mode: %u\n", mmc->clock,
2435 mmc->bus_width, mmc->selected_mode);
2436 debug(" host caps: 0x%x, card caps: 0x%x\n", mmc->host_caps,
2438 octeontx_mmc_switch_to(mmc);
2442 clock = mmc->cfg->f_min;
2444 switch (mmc->bus_width) {
2455 pr_warn("%s(%s): Invalid bus width %d, defaulting to 1\n",
2456 __func__, dev->name, mmc->bus_width);
2460 /* DDR is available for 4/8 bit bus width */
2461 if (mmc->ddr_mode && bus_width)
2464 debug("%s: sys_freq: %llu\n", __func__, host->sys_freq);
2465 clk_period = octeontx_mmc_calc_clk_period(mmc);
2468 emm_switch.s.bus_width = bus_width;
2469 emm_switch.s.power_class = power_class;
2470 emm_switch.s.clk_hi = clk_period / 2;
2471 emm_switch.s.clk_lo = clk_period / 2;
2473 debug("%s: last mode: %d, mode: %d, last clock: %u, clock: %u, ddr: %d\n",
2474 __func__, slot->last_mode, mmc->selected_mode,
2475 slot->last_clock, mmc->clock, mmc->ddr_mode);
2476 switch (mmc->selected_mode) {
2482 emm_switch.s.hs_timing = 1;
2491 emm_switch.s.hs200_timing = 1;
2498 emm_switch.s.hs400_timing = 1;
2501 pr_err("%s(%s): Unsupported mode 0x%x\n", __func__, dev->name,
2502 mmc->selected_mode);
2505 emm_switch.s.bus_id = slot->bus_id;
2507 if (!is_hs200 && !is_hs400 &&
2508 (mmc->selected_mode != slot->last_mode ||
2509 mmc->clock != slot->last_clock) &&
2511 slot->tuned = false;
2512 slot->last_mode = mmc->selected_mode;
2513 slot->last_clock = mmc->clock;
2516 if (CONFIG_IS_ENABLED(MMC_VERBOSE)) {
2517 debug("%s(%s): Setting bus mode to %s\n", __func__, dev->name,
2518 mmc_mode_name(mmc->selected_mode));
2520 debug("%s(%s): Setting bus mode to 0x%x\n", __func__, dev->name,
2521 mmc->selected_mode);
2524 debug(" Trying switch 0x%llx w%d hs:%d hs200:%d hs400:%d\n",
2525 emm_switch.u, emm_switch.s.bus_width, emm_switch.s.hs_timing,
2526 emm_switch.s.hs200_timing, emm_switch.s.hs400_timing);
2528 set_wdog(mmc, 1000);
2529 do_switch(mmc, emm_switch);
2531 mode.u = read_csr(mmc, MIO_EMM_MODEX(slot->bus_id));
2532 debug("%s(%s): mode: 0x%llx w:%d, hs:%d, hs200:%d, hs400:%d\n",
2533 __func__, dev->name, mode.u, mode.s.bus_width,
2534 mode.s.hs_timing, mode.s.hs200_timing, mode.s.hs400_timing);
2536 err = octeontx_mmc_configure_delay(mmc);
2538 #ifdef MMC_SUPPORTS_TUNING
2539 if (!err && mmc->selected_mode == MMC_HS_400 && !slot->hs400_tuned) {
2540 debug("%s: Tuning HS400 mode\n", __func__);
2541 err = octeontx_tune_hs400(mmc);
2549 * Gets the status of the card detect pin
2551 static int octeontx_mmc_get_cd(struct udevice *dev)
2553 struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
2556 if (dm_gpio_is_valid(&slot->cd_gpio)) {
2557 val = dm_gpio_get_value(&slot->cd_gpio);
2558 val ^= slot->cd_inverted;
2560 debug("%s(%s): cd: %d\n", __func__, dev->name, val);
2565 * Gets the status of the write protect pin
2567 static int octeontx_mmc_get_wp(struct udevice *dev)
2569 struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
2572 if (dm_gpio_is_valid(&slot->wp_gpio)) {
2573 val = dm_gpio_get_value(&slot->wp_gpio);
2574 val ^= slot->wp_inverted;
2576 debug("%s(%s): wp: %d\n", __func__, dev->name, val);
2580 static void octeontx_mmc_set_timing(struct mmc *mmc)
2582 union mio_emm_timing timing;
2583 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2585 switch (mmc->selected_mode) {
2587 timing = slot->hs200_taps;
2590 timing = slot->hs400_tuned ?
2591 slot->hs400_taps : slot->hs200_taps;
2594 timing = slot->taps;
2598 debug("%s(%s):\n cmd_in_tap: %u\n cmd_out_tap: %u\n data_in_tap: %u\n data_out_tap: %u\n",
2599 __func__, mmc->dev->name, timing.s.cmd_in_tap,
2600 timing.s.cmd_out_tap, timing.s.data_in_tap,
2601 timing.s.data_out_tap);
2603 octeontx_mmc_set_emm_timing(mmc, timing);
2606 static int octeontx_mmc_configure_delay(struct mmc *mmc)
2608 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2609 struct octeontx_mmc_host *host __maybe_unused = slot->host;
2610 bool __maybe_unused is_hs200;
2611 bool __maybe_unused is_hs400;
2613 debug("%s(%s)\n", __func__, mmc->dev->name);
2615 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
2616 union mio_emm_sample emm_sample;
2619 emm_sample.s.cmd_cnt = slot->cmd_cnt;
2620 emm_sample.s.dat_cnt = slot->dat_cnt;
2621 write_csr(mmc, MIO_EMM_SAMPLE(), emm_sample.u);
2623 is_hs200 = (mmc->selected_mode == MMC_HS_200);
2624 is_hs400 = (mmc->selected_mode == MMC_HS_400);
2626 if ((is_hs200 && slot->hs200_tuned) ||
2627 (is_hs400 && slot->hs400_tuned) ||
2628 (!is_hs200 && !is_hs400 && slot->tuned)) {
2629 octeontx_mmc_set_output_bus_timing(mmc);
2631 int half = MAX_NO_OF_TAPS / 2;
2634 switch (mmc->selected_mode) {
2637 cout = MMC_SD_LEGACY_DEFAULT_CMD_OUT_TAP;
2638 dout = MMC_SD_LEGACY_DEFAULT_DATA_OUT_TAP;
2640 cout = MMC_LEGACY_DEFAULT_CMD_OUT_TAP;
2641 dout = MMC_LEGACY_DEFAULT_DATA_OUT_TAP;
2645 cout = MMC_HS_CMD_OUT_TAP;
2646 dout = MMC_HS_DATA_OUT_TAP;
2652 cout = MMC_SD_HS_CMD_OUT_TAP;
2653 dout = MMC_SD_HS_DATA_OUT_TAP;
2659 cout = MMC_DEFAULT_CMD_OUT_TAP;
2660 dout = MMC_DEFAULT_DATA_OUT_TAP;
2665 if (host->timing_calibrated) {
2666 cout = octeontx2_mmc_calc_delay(
2667 mmc, slot->cmd_out_hs200_delay);
2668 dout = octeontx2_mmc_calc_delay(
2670 slot->data_out_hs200_delay);
2671 debug("%s(%s): Calibrated HS200/HS400 cmd out delay: %dps tap: %d, data out delay: %d, tap: %d\n",
2672 __func__, mmc->dev->name,
2673 slot->cmd_out_hs200_delay, cout,
2674 slot->data_out_hs200_delay, dout);
2676 cout = MMC_DEFAULT_HS200_CMD_OUT_TAP;
2677 dout = MMC_DEFAULT_HS200_DATA_OUT_TAP;
2684 if (host->timing_calibrated) {
2685 if (slot->cmd_out_hs400_delay)
2686 cout = octeontx2_mmc_calc_delay(
2688 slot->cmd_out_hs400_delay);
2689 if (slot->data_out_hs400_delay)
2690 dout = octeontx2_mmc_calc_delay(
2692 slot->data_out_hs400_delay);
2693 debug("%s(%s): Calibrated HS200/HS400 cmd out delay: %dps tap: %d, data out delay: %d, tap: %d\n",
2694 __func__, mmc->dev->name,
2695 slot->cmd_out_hs400_delay, cout,
2696 slot->data_out_hs400_delay, dout);
2698 cout = MMC_DEFAULT_HS400_CMD_OUT_TAP;
2699 dout = MMC_DEFAULT_HS400_DATA_OUT_TAP;
2704 pr_err("%s(%s): Invalid mode %d\n", __func__,
2705 mmc->dev->name, mmc->selected_mode);
2708 debug("%s(%s): Not tuned, hs200: %d, hs200 tuned: %d, hs400: %d, hs400 tuned: %d, tuned: %d\n",
2709 __func__, mmc->dev->name, is_hs200,
2711 is_hs400, slot->hs400_tuned, slot->tuned);
2712 /* Set some defaults */
2714 slot->hs200_taps.u = 0;
2715 slot->hs200_taps.s.cmd_out_tap = cout;
2716 slot->hs200_taps.s.data_out_tap = dout;
2717 slot->hs200_taps.s.cmd_in_tap = half;
2718 slot->hs200_taps.s.data_in_tap = half;
2719 } else if (is_hs400) {
2720 slot->hs400_taps.u = 0;
2721 slot->hs400_taps.s.cmd_out_tap = cout;
2722 slot->hs400_taps.s.data_out_tap = dout;
2723 slot->hs400_taps.s.cmd_in_tap = half;
2724 slot->hs400_taps.s.data_in_tap = half;
2727 slot->taps.s.cmd_out_tap = cout;
2728 slot->taps.s.data_out_tap = dout;
2729 slot->taps.s.cmd_in_tap = half;
2730 slot->taps.s.data_in_tap = half;
2735 debug("%s(%s): hs200 taps: ci: %u, co: %u, di: %u, do: %u\n",
2736 __func__, mmc->dev->name,
2737 slot->hs200_taps.s.cmd_in_tap,
2738 slot->hs200_taps.s.cmd_out_tap,
2739 slot->hs200_taps.s.data_in_tap,
2740 slot->hs200_taps.s.data_out_tap);
2742 debug("%s(%s): hs400 taps: ci: %u, co: %u, di: %u, do: %u\n",
2743 __func__, mmc->dev->name,
2744 slot->hs400_taps.s.cmd_in_tap,
2745 slot->hs400_taps.s.cmd_out_tap,
2746 slot->hs400_taps.s.data_in_tap,
2747 slot->hs400_taps.s.data_out_tap);
2749 debug("%s(%s): taps: ci: %u, co: %u, di: %u, do: %u\n",
2750 __func__, mmc->dev->name, slot->taps.s.cmd_in_tap,
2751 slot->taps.s.cmd_out_tap,
2752 slot->taps.s.data_in_tap,
2753 slot->taps.s.data_out_tap);
2754 octeontx_mmc_set_timing(mmc);
2755 debug("%s: Done\n", __func__);
2762 * Sets the MMC watchdog timer in microseconds
2764 * @param mmc mmc device
2765 * @param us timeout in microseconds, 0 for maximum timeout
2767 static void set_wdog(struct mmc *mmc, u64 us)
2769 union mio_emm_wdog wdog;
2772 val = (us * mmc->clock) / 1000000;
2773 if (val >= (1 << 26) || !us) {
2775 pr_debug("%s: warning: timeout %llu exceeds max value %llu, truncating\n",
2777 (u64)(((1ULL << 26) - 1) * 1000000ULL) /
2779 val = (1 << 26) - 1;
2782 wdog.s.clk_cnt = val;
2783 write_csr(mmc, MIO_EMM_WDOG(), wdog.u);
2787 * Set the IO drive strength and slew
2789 * @param mmc mmc device
2791 static void octeontx_mmc_io_drive_setup(struct mmc *mmc)
2793 if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
2794 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2795 union mio_emm_io_ctl io_ctl;
2797 if (slot->drive < 0 || slot->slew < 0)
2801 io_ctl.s.drive = slot->drive;
2802 io_ctl.s.slew = slot->slew;
2803 write_csr(mmc, MIO_EMM_IO_CTL(), io_ctl.u);
2808 * Print switch errors
2810 * @param mmc mmc device
2812 static void check_switch_errors(struct mmc *mmc)
2814 union mio_emm_switch emm_switch;
2816 emm_switch.u = read_csr(mmc, MIO_EMM_SWITCH());
2817 if (emm_switch.s.switch_err0)
2818 pr_err("%s: Switch power class error\n", mmc->cfg->name);
2819 if (emm_switch.s.switch_err1)
2820 pr_err("%s: Switch HS timing error\n", mmc->cfg->name);
2821 if (emm_switch.s.switch_err2)
2822 pr_err("%s: Switch bus width error\n", mmc->cfg->name);
2825 static void do_switch(struct mmc *mmc, union mio_emm_switch emm_switch)
2827 union mio_emm_rsp_sts rsp_sts;
2828 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2829 int bus_id = emm_switch.s.bus_id;
2832 if (emm_switch.s.bus_id != 0) {
2833 emm_switch.s.bus_id = 0;
2834 write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
2836 emm_switch.s.bus_id = bus_id;
2838 debug("%s(%s, 0x%llx)\n", __func__, mmc->dev->name, emm_switch.u);
2839 write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
2841 start = get_timer(0);
2843 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
2844 if (!rsp_sts.s.switch_val)
2847 } while (get_timer(start) < 10);
2848 if (rsp_sts.s.switch_val) {
2849 pr_warn("%s(%s): Warning: writing 0x%llx to emm_switch timed out, status: 0x%llx\n",
2850 __func__, mmc->dev->name, emm_switch.u, rsp_sts.u);
2852 slot->cached_switch = emm_switch;
2853 check_switch_errors(mmc);
2854 slot->cached_switch.u = emm_switch.u;
2855 debug("%s: emm_switch: 0x%llx, rsp_lo: 0x%llx\n",
2856 __func__, read_csr(mmc, MIO_EMM_SWITCH()),
2857 read_csr(mmc, MIO_EMM_RSP_LO()));
2861 * Given a delay in ps, return the tap delay count
2863 * @param mmc mmc data structure
2864 * @param delay delay in picoseconds
2866 * @return Number of tap cycles or error if -1
2868 static int octeontx2_mmc_calc_delay(struct mmc *mmc, int delay)
2870 struct octeontx_mmc_host *host = mmc_to_host(mmc);
2872 if (host->is_asim || host->is_emul)
2875 if (!host->timing_taps) {
2876 pr_err("%s(%s): Error: host timing not calibrated\n",
2877 __func__, mmc->dev->name);
2880 debug("%s(%s, %d) timing taps: %llu\n", __func__, mmc->dev->name,
2881 delay, host->timing_taps);
2882 return min_t(int, DIV_ROUND_UP(delay, host->timing_taps), 63);
2886 * Calibrates the delay based on the internal clock
2888 * @param mmc Pointer to mmc data structure
2890 * @return 0 for success or -ETIMEDOUT on error
2892 * NOTE: On error a default value will be calculated.
2894 static int octeontx_mmc_calibrate_delay(struct mmc *mmc)
2896 union mio_emm_calb emm_calb;
2897 union mio_emm_tap emm_tap;
2898 union mio_emm_cfg emm_cfg;
2899 union mio_emm_io_ctl emm_io_ctl;
2900 union mio_emm_switch emm_switch;
2901 union mio_emm_wdog emm_wdog;
2902 union mio_emm_sts_mask emm_sts_mask;
2903 union mio_emm_debug emm_debug;
2904 union mio_emm_timing emm_timing;
2905 struct octeontx_mmc_host *host = mmc_to_host(mmc);
2909 debug("%s: Calibrating delay\n", __func__);
2910 if (host->is_asim || host->is_emul) {
2911 debug(" No calibration for ASIM\n");
2915 if (host->calibrate_glitch) {
2916 emm_tap.s.delay = MMC_DEFAULT_TAP_DELAY;
2918 /* Save registers */
2919 emm_cfg.u = read_csr(mmc, MIO_EMM_CFG());
2920 emm_io_ctl.u = read_csr(mmc, MIO_EMM_IO_CTL());
2921 emm_switch.u = read_csr(mmc, MIO_EMM_SWITCH());
2922 emm_wdog.u = read_csr(mmc, MIO_EMM_WDOG());
2923 emm_sts_mask.u = read_csr(mmc, MIO_EMM_STS_MASK());
2924 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
2925 emm_timing.u = read_csr(mmc, MIO_EMM_TIMING());
2926 bus_ena = emm_cfg.s.bus_ena;
2927 bus_id = emm_switch.s.bus_id;
2928 emm_cfg.s.bus_ena = 0;
2929 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
2931 emm_cfg.s.bus_ena = 1ULL << 3;
2932 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
2935 write_csr(mmc, MIO_EMM_CALB(), emm_calb.u);
2936 emm_calb.s.start = 1;
2937 write_csr(mmc, MIO_EMM_CALB(), emm_calb.u);
2938 start = get_timer(0);
2939 /* This should only take 3 microseconds */
2942 emm_tap.u = read_csr(mmc, MIO_EMM_TAP());
2943 } while (!emm_tap.s.delay && get_timer(start) < 10);
2945 emm_calb.s.start = 0;
2946 write_csr(mmc, MIO_EMM_CALB(), emm_calb.u);
2948 emm_cfg.s.bus_ena = 0;
2949 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
2951 /* Restore registers */
2952 emm_cfg.s.bus_ena = bus_ena;
2953 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
2954 if (host->tap_requires_noclk) {
2955 /* Turn off the clock */
2956 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
2957 emm_debug.s.emmc_clk_disable = 1;
2958 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2960 emm_debug.s.rdsync_rst = 1;
2961 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2965 write_csr(mmc, MIO_EMM_TIMING(), emm_timing.u);
2966 if (host->tap_requires_noclk) {
2967 /* Turn the clock back on */
2969 emm_debug.s.rdsync_rst = 0;
2970 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2972 emm_debug.s.emmc_clk_disable = 0;
2973 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2976 write_csr(mmc, MIO_EMM_IO_CTL(), emm_io_ctl.u);
2977 bus_id = emm_switch.s.bus_id;
2978 emm_switch.s.bus_id = 0;
2979 write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
2980 emm_switch.s.bus_id = bus_id;
2981 write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
2982 write_csr(mmc, MIO_EMM_WDOG(), emm_wdog.u);
2983 write_csr(mmc, MIO_EMM_STS_MASK(), emm_sts_mask.u);
2984 write_csr(mmc, MIO_EMM_RCA(), mmc->rca);
2985 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2987 if (!emm_tap.s.delay) {
2988 pr_err("%s: Error: delay calibration failed, timed out.\n",
2990 /* Set to default value if timed out */
2991 emm_tap.s.delay = MMC_DEFAULT_TAP_DELAY;
2996 host->timing_taps = (10 * 1000 * emm_tap.s.delay) / TOTAL_NO_OF_TAPS;
2997 debug("%s(%s): timing taps: %llu, delay: %u\n",
2998 __func__, mmc->dev->name, host->timing_taps, emm_tap.s.delay);
2999 host->timing_calibrated = true;
3003 static int octeontx_mmc_set_input_bus_timing(struct mmc *mmc)
3005 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3007 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
3008 union mio_emm_sample sample;
3011 sample.s.cmd_cnt = slot->cmd_clk_skew;
3012 sample.s.dat_cnt = slot->dat_clk_skew;
3013 write_csr(mmc, MIO_EMM_SAMPLE(), sample.u);
3015 union mio_emm_timing timing;
3017 timing.u = read_csr(mmc, MIO_EMM_TIMING());
3018 if (mmc->selected_mode == MMC_HS_200) {
3019 if (slot->hs200_tuned) {
3020 timing.s.cmd_in_tap =
3021 slot->hs200_taps.s.cmd_in_tap;
3022 timing.s.data_in_tap =
3023 slot->hs200_taps.s.data_in_tap;
3025 pr_warn("%s(%s): Warning: hs200 timing not tuned\n",
3026 __func__, mmc->dev->name);
3027 timing.s.cmd_in_tap =
3028 MMC_DEFAULT_HS200_CMD_IN_TAP;
3029 timing.s.data_in_tap =
3030 MMC_DEFAULT_HS200_DATA_IN_TAP;
3032 } else if (mmc->selected_mode == MMC_HS_400) {
3033 if (slot->hs400_tuned) {
3034 timing.s.cmd_in_tap =
3035 slot->hs400_taps.s.cmd_in_tap;
3036 timing.s.data_in_tap =
3037 slot->hs400_taps.s.data_in_tap;
3038 } else if (slot->hs200_tuned) {
3039 timing.s.cmd_in_tap =
3040 slot->hs200_taps.s.cmd_in_tap;
3041 timing.s.data_in_tap =
3042 slot->hs200_taps.s.data_in_tap;
3044 pr_warn("%s(%s): Warning: hs400 timing not tuned\n",
3045 __func__, mmc->dev->name);
3046 timing.s.cmd_in_tap =
3047 MMC_DEFAULT_HS200_CMD_IN_TAP;
3048 timing.s.data_in_tap =
3049 MMC_DEFAULT_HS200_DATA_IN_TAP;
3051 } else if (slot->tuned) {
3052 timing.s.cmd_in_tap = slot->taps.s.cmd_in_tap;
3053 timing.s.data_in_tap = slot->taps.s.data_in_tap;
3055 timing.s.cmd_in_tap = MMC_DEFAULT_CMD_IN_TAP;
3056 timing.s.data_in_tap = MMC_DEFAULT_DATA_IN_TAP;
3058 octeontx_mmc_set_emm_timing(mmc, timing);
3065 * Sets the default bus timing for the current mode.
3067 * @param mmc mmc data structure
3069 * @return 0 for success, error otherwise
3071 static int octeontx_mmc_set_output_bus_timing(struct mmc *mmc)
3073 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3074 union mio_emm_timing timing;
3075 int cout_bdelay, dout_bdelay;
3076 unsigned int cout_delay, dout_delay;
3079 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX))
3082 debug("%s(%s)\n", __func__, mmc->dev->name);
3083 if (slot->is_asim || slot->is_emul)
3086 octeontx_mmc_calibrate_delay(mmc);
3088 if (mmc->clock < 26000000) {
3091 } else if (mmc->clock <= 52000000) {
3094 } else if (!mmc_is_mode_ddr(mmc->selected_mode)) {
3095 cout_delay = slot->cmd_out_hs200_delay;
3096 dout_delay = slot->data_out_hs200_delay;
3098 cout_delay = slot->cmd_out_hs400_delay;
3099 dout_delay = slot->data_out_hs400_delay;
3102 snprintf(env_name, sizeof(env_name), "mmc%d_hs200_dout_delay_ps",
3104 dout_delay = env_get_ulong(env_name, 10, dout_delay);
3105 debug("%s: dout_delay: %u\n", __func__, dout_delay);
3107 cout_bdelay = octeontx2_mmc_calc_delay(mmc, cout_delay);
3108 dout_bdelay = octeontx2_mmc_calc_delay(mmc, dout_delay);
3110 debug("%s: cmd output delay: %u, data output delay: %u, cmd bdelay: %d, data bdelay: %d, clock: %d\n",
3111 __func__, cout_delay, dout_delay, cout_bdelay, dout_bdelay,
3113 if (cout_bdelay < 0 || dout_bdelay < 0) {
3114 pr_err("%s: Error: could not calculate command and/or data clock skew\n",
3118 timing.u = read_csr(mmc, MIO_EMM_TIMING());
3119 timing.s.cmd_out_tap = cout_bdelay;
3120 timing.s.data_out_tap = dout_bdelay;
3121 if (mmc->selected_mode == MMC_HS_200) {
3122 slot->hs200_taps.s.cmd_out_tap = cout_bdelay;
3123 slot->hs200_taps.s.data_out_tap = dout_bdelay;
3124 } else if (mmc->selected_mode == MMC_HS_400) {
3125 slot->hs400_taps.s.cmd_out_tap = cout_bdelay;
3126 slot->hs400_taps.s.data_out_tap = dout_bdelay;
3128 slot->taps.s.cmd_out_tap = cout_bdelay;
3129 slot->taps.s.data_out_tap = dout_bdelay;
3131 octeontx_mmc_set_emm_timing(mmc, timing);
3132 debug("%s(%s): bdelay: %d/%d, clock: %d, ddr: %s, timing taps: %llu, do: %d, di: %d, co: %d, ci: %d\n",
3133 __func__, mmc->dev->name, cout_bdelay, dout_bdelay, mmc->clock,
3134 mmc->ddr_mode ? "yes" : "no",
3135 mmc_to_host(mmc)->timing_taps,
3136 timing.s.data_out_tap,
3137 timing.s.data_in_tap,
3138 timing.s.cmd_out_tap,
3139 timing.s.cmd_in_tap);
3144 static void octeontx_mmc_set_clock(struct mmc *mmc)
3146 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3149 clock = min(mmc->cfg->f_max, (uint)slot->clock);
3150 clock = max(mmc->cfg->f_min, clock);
3151 debug("%s(%s): f_min: %u, f_max: %u, clock: %u\n", __func__,
3152 mmc->dev->name, mmc->cfg->f_min, mmc->cfg->f_max, clock);
3153 slot->clock = clock;
3158 * This switches I/O power as needed when switching between slots.
3160 * @param mmc mmc data structure
3162 static void octeontx_mmc_switch_io(struct mmc *mmc)
3164 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3165 struct octeontx_mmc_host *host = slot->host;
3166 struct mmc *last_mmc = host->last_mmc;
3167 static struct udevice *last_reg;
3168 union mio_emm_cfg emm_cfg;
3170 static bool initialized;
3173 if (!initialized || mmc != host->last_mmc) {
3176 /* Switch to bus 3 which is unused */
3177 emm_cfg.u = read_csr(mmc, MIO_EMM_CFG());
3178 emm_cfg.s.bus_ena = 1 << 3;
3179 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
3181 /* Turn off all other I/O interfaces with first initialization
3182 * if at least one supply was found.
3184 for (bus = 0; bus <= OCTEONTX_MAX_MMC_SLOT; bus++) {
3185 ommc = &host->slots[bus].mmc;
3187 /* Handle self case later */
3188 if (ommc == mmc || !ommc->vqmmc_supply)
3191 /* Skip if we're not switching regulators */
3192 if (last_reg == mmc->vqmmc_supply)
3195 /* Turn off other regulators */
3196 if (ommc->vqmmc_supply != mmc->vqmmc_supply)
3197 regulator_set_enable(ommc->vqmmc_supply, false);
3199 /* Turn ourself on */
3200 if (mmc->vqmmc_supply && last_reg != mmc->vqmmc_supply)
3201 regulator_set_enable(mmc->vqmmc_supply, true);
3202 mdelay(1); /* Settle time */
3203 /* Switch to new bus */
3204 emm_cfg.s.bus_ena = 1 << slot->bus_id;
3205 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
3206 last_reg = mmc->vqmmc_supply;
3211 /* No change in device */
3212 if (last_mmc == mmc)
3216 pr_warn("%s(%s): No previous slot detected in IO slot switch!\n",
3217 __func__, mmc->dev->name);
3221 debug("%s(%s): last: %s, supply: %p\n", __func__, mmc->dev->name,
3222 last_mmc->dev->name, mmc->vqmmc_supply);
3224 /* The supply is the same so we do nothing */
3225 if (last_mmc->vqmmc_supply == mmc->vqmmc_supply)
3228 /* Turn off the old slot I/O supply */
3229 if (last_mmc->vqmmc_supply) {
3230 debug("%s(%s): Turning off IO to %s, supply: %s\n",
3231 __func__, mmc->dev->name, last_mmc->dev->name,
3232 last_mmc->vqmmc_supply->name);
3233 regulator_set_enable(last_mmc->vqmmc_supply, false);
3235 /* Turn on the new slot I/O supply */
3236 if (mmc->vqmmc_supply) {
3237 debug("%s(%s): Turning on IO to slot %d, supply: %s\n",
3238 __func__, mmc->dev->name, slot->bus_id,
3239 mmc->vqmmc_supply->name);
3240 regulator_set_enable(mmc->vqmmc_supply, true);
3242 /* Allow power to settle */
3247 * Called to switch between mmc devices
3249 * @param mmc new mmc device
3251 static void octeontx_mmc_switch_to(struct mmc *mmc)
3253 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3254 struct octeontx_mmc_slot *old_slot;
3255 struct octeontx_mmc_host *host = slot->host;
3256 union mio_emm_switch emm_switch;
3257 union mio_emm_sts_mask emm_sts_mask;
3258 union mio_emm_rca emm_rca;
3260 if (slot->bus_id == host->last_slotid)
3263 debug("%s(%s) switching from slot %d to slot %d\n", __func__,
3264 mmc->dev->name, host->last_slotid, slot->bus_id);
3265 octeontx_mmc_switch_io(mmc);
3267 if (host->last_slotid >= 0 && slot->valid) {
3268 old_slot = &host->slots[host->last_slotid];
3269 old_slot->cached_switch.u = read_csr(mmc, MIO_EMM_SWITCH());
3270 old_slot->cached_rca.u = read_csr(mmc, MIO_EMM_RCA());
3273 write_csr(mmc, MIO_EMM_RCA(), mmc->rca);
3274 emm_switch = slot->cached_switch;
3275 do_switch(mmc, emm_switch);
3277 emm_rca.s.card_rca = mmc->rca;
3278 write_csr(mmc, MIO_EMM_RCA(), emm_rca.u);
3281 set_wdog(mmc, 100000);
3282 if (octeontx_mmc_set_output_bus_timing(mmc) ||
3283 octeontx_mmc_set_input_bus_timing(mmc))
3284 pr_err("%s(%s): Error setting bus timing\n", __func__,
3286 octeontx_mmc_io_drive_setup(mmc);
3289 emm_sts_mask.s.sts_msk = 1 << 7 | 1 << 22 | 1 << 23 | 1 << 19;
3290 write_csr(mmc, MIO_EMM_STS_MASK(), emm_sts_mask.u);
3291 host->last_slotid = slot->bus_id;
3292 host->last_mmc = mmc;
3297 * Perform initial timing configuration
3299 * @param mmc mmc device
3301 * @return 0 for success
3303 * NOTE: This will need to be updated when new silicon comes out
3305 static int octeontx_mmc_init_timing(struct mmc *mmc)
3307 union mio_emm_timing timing;
3309 if (mmc_to_slot(mmc)->is_asim || mmc_to_slot(mmc)->is_emul)
3312 debug("%s(%s)\n", __func__, mmc->dev->name);
3314 timing.s.cmd_out_tap = MMC_DEFAULT_CMD_OUT_TAP;
3315 timing.s.data_out_tap = MMC_DEFAULT_DATA_OUT_TAP;
3316 timing.s.cmd_in_tap = MMC_DEFAULT_CMD_IN_TAP;
3317 timing.s.data_in_tap = MMC_DEFAULT_DATA_IN_TAP;
3318 octeontx_mmc_set_emm_timing(mmc, timing);
3323 * Perform low-level initialization
3325 * @param mmc mmc device
3327 * @return 0 for success, error otherwise
3329 static int octeontx_mmc_init_lowlevel(struct mmc *mmc)
3331 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3332 struct octeontx_mmc_host *host = slot->host;
3333 union mio_emm_switch emm_switch;
3336 debug("%s(%s): lowlevel init for slot %d\n", __func__,
3337 mmc->dev->name, slot->bus_id);
3338 host->emm_cfg.s.bus_ena &= ~(1 << slot->bus_id);
3339 write_csr(mmc, MIO_EMM_CFG(), host->emm_cfg.u);
3341 host->emm_cfg.s.bus_ena |= 1 << slot->bus_id;
3342 write_csr(mmc, MIO_EMM_CFG(), host->emm_cfg.u);
3344 slot->clock = mmc->cfg->f_min;
3345 octeontx_mmc_set_clock(&slot->mmc);
3347 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
3348 if (host->cond_clock_glitch) {
3349 union mio_emm_debug emm_debug;
3351 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
3352 emm_debug.s.clk_on = 1;
3353 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
3355 octeontx_mmc_calibrate_delay(&slot->mmc);
3358 clk_period = octeontx_mmc_calc_clk_period(mmc);
3360 emm_switch.s.power_class = 10;
3361 emm_switch.s.clk_lo = clk_period / 2;
3362 emm_switch.s.clk_hi = clk_period / 2;
3364 emm_switch.s.bus_id = slot->bus_id;
3365 debug("%s: Performing switch\n", __func__);
3366 do_switch(mmc, emm_switch);
3367 slot->cached_switch.u = emm_switch.u;
3369 if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX))
3370 octeontx_mmc_init_timing(mmc);
3372 set_wdog(mmc, 1000000); /* Set to 1 second */
3373 write_csr(mmc, MIO_EMM_STS_MASK(), 0xe4390080ull);
3374 write_csr(mmc, MIO_EMM_RCA(), 1);
3376 debug("%s: done\n", __func__);
3381 * Translates a voltage number to bits in MMC register
3383 * @param voltage voltage in microvolts
3385 * @return MMC register value for voltage
3387 static u32 xlate_voltage(u32 voltage)
3391 /* Convert to millivolts */
3393 if (voltage >= 1650 && voltage <= 1950)
3394 volt |= MMC_VDD_165_195;
3395 if (voltage >= 2000 && voltage <= 2100)
3396 volt |= MMC_VDD_20_21;
3397 if (voltage >= 2100 && voltage <= 2200)
3398 volt |= MMC_VDD_21_22;
3399 if (voltage >= 2200 && voltage <= 2300)
3400 volt |= MMC_VDD_22_23;
3401 if (voltage >= 2300 && voltage <= 2400)
3402 volt |= MMC_VDD_23_24;
3403 if (voltage >= 2400 && voltage <= 2500)
3404 volt |= MMC_VDD_24_25;
3405 if (voltage >= 2500 && voltage <= 2600)
3406 volt |= MMC_VDD_25_26;
3407 if (voltage >= 2600 && voltage <= 2700)
3408 volt |= MMC_VDD_26_27;
3409 if (voltage >= 2700 && voltage <= 2800)
3410 volt |= MMC_VDD_27_28;
3411 if (voltage >= 2800 && voltage <= 2900)
3412 volt |= MMC_VDD_28_29;
3413 if (voltage >= 2900 && voltage <= 3000)
3414 volt |= MMC_VDD_29_30;
3415 if (voltage >= 3000 && voltage <= 3100)
3416 volt |= MMC_VDD_30_31;
3417 if (voltage >= 3100 && voltage <= 3200)
3418 volt |= MMC_VDD_31_32;
3419 if (voltage >= 3200 && voltage <= 3300)
3420 volt |= MMC_VDD_32_33;
3421 if (voltage >= 3300 && voltage <= 3400)
3422 volt |= MMC_VDD_33_34;
3423 if (voltage >= 3400 && voltage <= 3500)
3424 volt |= MMC_VDD_34_35;
3425 if (voltage >= 3500 && voltage <= 3600)
3426 volt |= MMC_VDD_35_36;
3432 * Check if a slot is valid in the device tree
3434 * @param dev slot device to check
3436 * @return true if status reports "ok" or "okay" or if no status,
3439 static bool octeontx_mmc_get_valid(struct udevice *dev)
3441 const char *stat = ofnode_read_string(dev->node, "status");
3443 if (!stat || !strncmp(stat, "ok", 2))
3450 * Reads slot configuration from the device tree
3452 * @param dev slot device
3454 * @return 0 on success, otherwise error
3456 static int octeontx_mmc_get_config(struct udevice *dev)
3458 struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
3463 ofnode node = dev->node;
3467 debug("%s(%s)", __func__, dev->name);
3468 slot->cfg.name = dev->name;
3470 slot->cfg.f_max = ofnode_read_s32_default(dev->node, "max-frequency",
3472 snprintf(env_name, sizeof(env_name), "mmc_max_frequency%d",
3475 new_max_freq = env_get_ulong(env_name, 10, slot->cfg.f_max);
3476 debug("Reading %s, got %lu\n", env_name, new_max_freq);
3478 if (new_max_freq != slot->cfg.f_max) {
3479 printf("Overriding device tree MMC maximum frequency %u to %lu\n",
3480 slot->cfg.f_max, new_max_freq);
3481 slot->cfg.f_max = new_max_freq;
3483 slot->cfg.f_min = 400000;
3484 slot->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
3486 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
3487 slot->hs400_tuning_block =
3488 ofnode_read_s32_default(dev->node,
3489 "marvell,hs400-tuning-block",
3491 debug("%s(%s): mmc HS400 tuning block: %d\n", __func__,
3492 dev->name, slot->hs400_tuning_block);
3494 slot->hs200_tap_adj =
3495 ofnode_read_s32_default(dev->node,
3496 "marvell,hs200-tap-adjust", 0);
3497 debug("%s(%s): hs200-tap-adjust: %d\n", __func__, dev->name,
3498 slot->hs200_tap_adj);
3499 slot->hs400_tap_adj =
3500 ofnode_read_s32_default(dev->node,
3501 "marvell,hs400-tap-adjust", 0);
3502 debug("%s(%s): hs400-tap-adjust: %d\n", __func__, dev->name,
3503 slot->hs400_tap_adj);
3506 err = ofnode_read_u32_array(dev->node, "voltage-ranges", voltages, 2);
3508 slot->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
3510 low = xlate_voltage(voltages[0]);
3511 high = xlate_voltage(voltages[1]);
3512 debug(" low voltage: 0x%x (%u), high: 0x%x (%u)\n",
3513 low, voltages[0], high, voltages[1]);
3514 if (low > high || !low || !high) {
3515 pr_err("Invalid MMC voltage range [%u-%u] specified for %s\n",
3516 low, high, dev->name);
3519 slot->cfg.voltages = 0;
3521 slot->cfg.voltages |= low;
3523 } while (low <= high);
3525 debug("%s: config voltages: 0x%x\n", __func__, slot->cfg.voltages);
3526 slot->slew = ofnode_read_s32_default(node, "cavium,clk-slew", -1);
3527 slot->drive = ofnode_read_s32_default(node, "cavium,drv-strength", -1);
3528 gpio_request_by_name(dev, "cd-gpios", 0, &slot->cd_gpio, GPIOD_IS_IN);
3529 slot->cd_inverted = ofnode_read_bool(node, "cd-inverted");
3530 gpio_request_by_name(dev, "wp-gpios", 0, &slot->wp_gpio, GPIOD_IS_IN);
3531 slot->wp_inverted = ofnode_read_bool(node, "wp-inverted");
3532 if (slot->cfg.voltages & MMC_VDD_165_195) {
3533 slot->is_1_8v = true;
3534 slot->is_3_3v = false;
3535 } else if (slot->cfg.voltages & (MMC_VDD_30_31 | MMC_VDD_31_32 |
3536 MMC_VDD_33_34 | MMC_VDD_34_35 |
3538 slot->is_1_8v = false;
3539 slot->is_3_3v = true;
3542 bus_width = ofnode_read_u32_default(node, "bus-width", 1);
3543 /* Note fall-through */
3544 switch (bus_width) {
3546 slot->cfg.host_caps |= MMC_MODE_8BIT;
3548 slot->cfg.host_caps |= MMC_MODE_4BIT;
3550 slot->cfg.host_caps |= MMC_MODE_1BIT;
3553 if (ofnode_read_bool(node, "no-1-8-v")) {
3554 slot->is_3_3v = true;
3555 slot->is_1_8v = false;
3556 if (!(slot->cfg.voltages & (MMC_VDD_32_33 | MMC_VDD_33_34)))
3557 pr_warn("%s(%s): voltages indicate 3.3v but 3.3v not supported\n",
3558 __func__, dev->name);
3560 if (ofnode_read_bool(node, "mmc-ddr-3-3v")) {
3561 slot->is_3_3v = true;
3562 slot->is_1_8v = false;
3563 if (!(slot->cfg.voltages & (MMC_VDD_32_33 | MMC_VDD_33_34)))
3564 pr_warn("%s(%s): voltages indicate 3.3v but 3.3v not supported\n",
3565 __func__, dev->name);
3567 if (ofnode_read_bool(node, "cap-sd-highspeed") ||
3568 ofnode_read_bool(node, "cap-mmc-highspeed") ||
3569 ofnode_read_bool(node, "sd-uhs-sdr25"))
3570 slot->cfg.host_caps |= MMC_MODE_HS;
3571 if (slot->cfg.f_max >= 50000000 &&
3572 slot->cfg.host_caps & MMC_MODE_HS)
3573 slot->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
3574 if (ofnode_read_bool(node, "sd-uhs-sdr50"))
3575 slot->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
3576 if (ofnode_read_bool(node, "sd-uhs-ddr50"))
3577 slot->cfg.host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz |
3580 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
3581 if (!slot->is_asim && !slot->is_emul) {
3582 if (ofnode_read_bool(node, "mmc-hs200-1_8v"))
3583 slot->cfg.host_caps |= MMC_MODE_HS200 |
3585 if (ofnode_read_bool(node, "mmc-hs400-1_8v"))
3586 slot->cfg.host_caps |= MMC_MODE_HS400 |
3590 slot->cmd_out_hs200_delay =
3591 ofnode_read_u32_default(node,
3592 "marvell,cmd-out-hs200-dly",
3593 MMC_DEFAULT_HS200_CMD_OUT_DLY);
3594 debug("%s(%s): HS200 cmd out delay: %d\n",
3595 __func__, dev->name, slot->cmd_out_hs200_delay);
3596 slot->data_out_hs200_delay =
3597 ofnode_read_u32_default(node,
3598 "marvell,data-out-hs200-dly",
3599 MMC_DEFAULT_HS200_DATA_OUT_DLY);
3600 debug("%s(%s): HS200 data out delay: %d\n",
3601 __func__, dev->name, slot->data_out_hs200_delay);
3602 slot->cmd_out_hs400_delay =
3603 ofnode_read_u32_default(node,
3604 "marvell,cmd-out-hs400-dly",
3605 MMC_DEFAULT_HS400_CMD_OUT_DLY);
3606 debug("%s(%s): HS400 cmd out delay: %d\n",
3607 __func__, dev->name, slot->cmd_out_hs400_delay);
3608 slot->data_out_hs400_delay =
3609 ofnode_read_u32_default(node,
3610 "marvell,data-out-hs400-dly",
3611 MMC_DEFAULT_HS400_DATA_OUT_DLY);
3612 debug("%s(%s): HS400 data out delay: %d\n",
3613 __func__, dev->name, slot->data_out_hs400_delay);
3617 slot->disable_ddr = ofnode_read_bool(node, "marvell,disable-ddr");
3618 slot->non_removable = ofnode_read_bool(node, "non-removable");
3619 slot->cmd_clk_skew = ofnode_read_u32_default(node,
3620 "cavium,cmd-clk-skew", 0);
3621 slot->dat_clk_skew = ofnode_read_u32_default(node,
3622 "cavium,dat-clk-skew", 0);
3623 debug("%s(%s): host caps: 0x%x\n", __func__,
3624 dev->name, slot->cfg.host_caps);
3631 * @param dev mmc device
3633 * @return 0 for success, error otherwise
3635 static int octeontx_mmc_slot_probe(struct udevice *dev)
3637 struct octeontx_mmc_slot *slot;
3641 debug("%s(%s)\n", __func__, dev->name);
3643 pr_err("%s(%s): Error: host not probed yet\n",
3644 __func__, dev->name);
3646 slot = dev_to_mmc_slot(dev);
3650 slot->valid = false;
3651 if (!octeontx_mmc_get_valid(dev)) {
3652 debug("%s(%s): slot is invalid\n", __func__, dev->name);
3656 debug("%s(%s): Getting config\n", __func__, dev->name);
3657 err = octeontx_mmc_get_config(dev);
3659 pr_err("probe(%s): Error getting config\n", dev->name);
3663 debug("%s(%s): mmc bind, mmc: %p\n", __func__, dev->name, &slot->mmc);
3664 err = mmc_bind(dev, &slot->mmc, &slot->cfg);
3666 pr_err("%s(%s): Error binding mmc\n", __func__, dev->name);
3670 /* For some reason, mmc_bind always assigns priv to the device */
3671 slot->mmc.priv = slot;
3673 debug("%s(%s): lowlevel init\n", __func__, dev->name);
3674 err = octeontx_mmc_init_lowlevel(mmc);
3676 pr_err("probe(%s): Low-level init failed\n", dev->name);
3683 " base address : %p\n"
3684 " bus id : %d\n", __func__, dev->name,
3685 slot->base_addr, slot->bus_id);
3691 * MMC slot driver operations
3693 static const struct dm_mmc_ops octeontx_hsmmc_ops = {
3694 .send_cmd = octeontx_mmc_dev_send_cmd,
3695 .set_ios = octeontx_mmc_set_ios,
3696 .get_cd = octeontx_mmc_get_cd,
3697 .get_wp = octeontx_mmc_get_wp,
3698 #ifdef MMC_SUPPORTS_TUNING
3699 .execute_tuning = octeontx_mmc_execute_tuning,
3703 static const struct udevice_id octeontx_hsmmc_ids[] = {
3704 { .compatible = "mmc-slot" },
3708 U_BOOT_DRIVER(octeontx_hsmmc_slot) = {
3709 .name = "octeontx_hsmmc_slot",
3711 .of_match = of_match_ptr(octeontx_hsmmc_ids),
3712 .probe = octeontx_mmc_slot_probe,
3713 .ops = &octeontx_hsmmc_ops,
3716 /*****************************************************************
3719 * The PCI host driver contains the resources used by all of the
3722 * The slot drivers are pseudo drivers.
3726 * Probe the MMC host controller
3728 * @param dev mmc host controller device
3730 * @return 0 for success, -1 on error
3732 static int octeontx_mmc_host_probe(struct udevice *dev)
3734 pci_dev_t bdf = dm_pci_get_bdf(dev);
3735 struct octeontx_mmc_host *host = dev_get_priv(dev);
3736 union mio_emm_int emm_int;
3739 debug("%s(%s): Entry host: %p\n", __func__, dev->name, host);
3741 if (!octeontx_mmc_get_valid(dev)) {
3742 debug("%s(%s): mmc host not valid\n", __func__, dev->name);
3745 memset(host, 0, sizeof(*host));
3746 host->base_addr = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0,
3748 if (!host->base_addr) {
3749 pr_err("%s: Error: MMC base address not found\n", __func__);
3753 debug("%s(%s): Base address: %p\n", __func__, dev->name,
3755 if (!dev_has_of_node(dev)) {
3756 pr_err("%s: No device tree information found\n", __func__);
3759 host->node = dev->node;
3760 dev->req_seq = PCI_FUNC(bdf);
3761 host->last_slotid = -1;
3762 if (otx_is_platform(PLATFORM_ASIM))
3763 host->is_asim = true;
3764 if (otx_is_platform(PLATFORM_EMULATOR))
3765 host->is_emul = true;
3766 host->dma_wait_delay =
3767 ofnode_read_u32_default(dev->node, "marvell,dma-wait-delay", 1);
3768 /* Force reset of eMMC */
3769 writeq(0, host->base_addr + MIO_EMM_CFG());
3770 debug("%s: Clearing MIO_EMM_CFG\n", __func__);
3772 emm_int.u = readq(host->base_addr + MIO_EMM_INT());
3773 debug("%s: Writing 0x%llx to MIO_EMM_INT\n", __func__, emm_int.u);
3774 writeq(emm_int.u, host->base_addr + MIO_EMM_INT());
3776 debug("%s(%s): Getting I/O clock\n", __func__, dev->name);
3777 host->sys_freq = octeontx_get_io_clock();
3778 debug("%s(%s): I/O clock %llu\n", __func__, dev->name, host->sys_freq);
3780 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
3781 /* Flags for issues to work around */
3782 dm_pci_read_config8(dev, PCI_REVISION_ID, &rev);
3783 if (otx_is_soc(CN96XX)) {
3784 debug("%s: CN96XX revision %d\n", __func__, rev);
3787 host->calibrate_glitch = true;
3788 host->cond_clock_glitch = true;
3795 host->hs400_skew_needed = true;
3796 debug("HS400 skew support enabled\n");
3799 debug("CN96XX rev C0+ detected\n");
3800 host->tap_requires_noclk = true;
3803 } else if (otx_is_soc(CN95XX)) {
3805 host->cond_clock_glitch = true;
3815 * This performs some initial setup before a probe occurs.
3817 * @param dev: MMC slot device
3819 * @return 0 for success, -1 on failure
3821 * Do some pre-initialization before probing a slot.
3823 static int octeontx_mmc_host_child_pre_probe(struct udevice *dev)
3825 struct octeontx_mmc_host *host = dev_get_priv(dev_get_parent(dev));
3826 struct octeontx_mmc_slot *slot;
3827 struct mmc_uclass_priv *upriv;
3828 ofnode node = dev->node;
3833 debug("%s(%s) Pre-Probe\n", __func__, dev->name);
3834 if (ofnode_read_u32(node, "reg", &bus_id)) {
3835 pr_err("%s(%s): Error: \"reg\" not found in device tree\n",
3836 __func__, dev->name);
3839 if (bus_id > OCTEONTX_MAX_MMC_SLOT) {
3840 pr_err("%s(%s): Error: \"reg\" out of range of 0..%d\n",
3841 __func__, dev->name, OCTEONTX_MAX_MMC_SLOT);
3845 slot = &host->slots[bus_id];
3848 slot->bus_id = bus_id;
3850 slot->base_addr = host->base_addr;
3851 slot->is_asim = host->is_asim;
3852 slot->is_emul = host->is_emul;
3854 snprintf(name, sizeof(name), "octeontx-mmc%d", bus_id);
3855 err = device_set_name(dev, name);
3857 if (!dev->uclass_priv) {
3858 debug("%s(%s): Allocating uclass priv\n", __func__,
3860 upriv = calloc(1, sizeof(struct mmc_uclass_priv));
3863 dev->uclass_priv = upriv;
3864 dev->uclass->priv = upriv;
3866 upriv = dev->uclass_priv;
3869 upriv->mmc = &slot->mmc;
3870 debug("%s: uclass priv: %p, mmc: %p\n", dev->name, upriv, upriv->mmc);
3872 debug("%s: ret: %d\n", __func__, err);
3876 static const struct udevice_id octeontx_hsmmc_host_ids[] = {
3877 { .compatible = "cavium,thunder-8890-mmc" },
3881 U_BOOT_DRIVER(octeontx_hsmmc_host) = {
3882 .name = "octeontx_hsmmc_host",
3884 .of_match = of_match_ptr(octeontx_hsmmc_host_ids),
3885 .probe = octeontx_mmc_host_probe,
3886 .priv_auto = sizeof(struct octeontx_mmc_host),
3887 .child_pre_probe = octeontx_mmc_host_child_pre_probe,
3888 .flags = DM_FLAG_PRE_RELOC,
3891 static struct pci_device_id octeontx_mmc_supported[] = {
3892 { PCI_VDEVICE(CAVIUM, PCI_DEVICE_ID_CAVIUM_EMMC) },
3896 U_BOOT_PCI_DEVICE(octeontx_hsmmc_host, octeontx_mmc_supported);