1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 Marvell International Ltd.
9 #include <dm/device-internal.h>
21 #include <power/regulator.h>
25 #include <linux/delay.h>
26 #include <linux/kernel.h>
27 #include <linux/libfdt.h>
29 #if defined(CONFIG_ARCH_OCTEON)
30 #include <mach/octeon-model.h>
31 #include <mach/cvmx-regs.h>
32 #include <mach/cvmx-mio-emm-defs.h>
34 #include <asm/arch/board.h>
35 #include <asm/arch/clock.h>
36 #include <asm/arch/csrs/csrs-mio_emm.h>
39 #include "octeontx_hsmmc.h"
41 /* Use dummy implementation for MIPS Octeon to always return false */
42 #if defined(CONFIG_ARCH_OCTEON)
43 #define otx_is_soc(ver) 0
46 #define MMC_TIMEOUT_SHORT 20 /* in ms */
47 #define MMC_TIMEOUT_LONG 1000
48 #define MMC_TIMEOUT_ERASE 10000
50 #define MMC_DEFAULT_DATA_IN_TAP 10
51 #define MMC_DEFAULT_CMD_IN_TAP 10
52 #define MMC_DEFAULT_CMD_OUT_TAP 39
53 #define MMC_DEFAULT_DATA_OUT_TAP 39
54 #define MMC_DEFAULT_HS200_CMD_IN_TAP 24
55 #define MMC_DEFAULT_HS200_DATA_IN_TAP 24
56 #define MMC_DEFAULT_HS200_CMD_OUT_TAP (otx_is_soc(CN95XX) ? 10 : 5)
57 #define MMC_DEFAULT_HS200_DATA_OUT_TAP (otx_is_soc(CN95XX) ? 10 : 5)
58 #define MMC_DEFAULT_HS400_CMD_OUT_TAP (otx_is_soc(CN95XX) ? 10 : 5)
59 #define MMC_DEFAULT_HS400_DATA_OUT_TAP (otx_is_soc(CN95XX) ? 5 : 3)
60 #define MMC_DEFAULT_HS200_CMD_OUT_DLY 800 /* Delay in ps */
61 #define MMC_DEFAULT_HS200_DATA_OUT_DLY 800 /* Delay in ps */
62 #define MMC_DEFAULT_HS400_CMD_OUT_DLY 800 /* Delay in ps */
63 #define MMC_DEFAULT_HS400_DATA_OUT_DLY 400 /* Delay in ps */
64 #define MMC_DEFAULT_SD_UHS_SDR104_CMD_OUT_TAP MMC_DEFAULT_HS200_CMD_OUT_TAP
65 #define MMC_DEFAULT_SD_UHS_SDR104_DATA_OUT_TAP MMC_DEFAULT_HS200_DATA_OUT_TAP
66 #define MMC_LEGACY_DEFAULT_CMD_OUT_TAP 39
67 #define MMC_LEGACY_DEFAULT_DATA_OUT_TAP 39
68 #define MMC_SD_LEGACY_DEFAULT_CMD_OUT_TAP 63
69 #define MMC_SD_LEGACY_DEFAULT_DATA_OUT_TAP 63
70 #define MMC_HS_CMD_OUT_TAP 32
71 #define MMC_HS_DATA_OUT_TAP 32
72 #define MMC_SD_HS_CMD_OUT_TAP 26
73 #define MMC_SD_HS_DATA_OUT_TAP 26
74 #define MMC_SD_UHS_SDR25_CMD_OUT_TAP 26
75 #define MMC_SD_UHS_SDR25_DATA_OUT_TAP 26
76 #define MMC_SD_UHS_SDR50_CMD_OUT_TAP 26
77 #define MMC_SD_UHS_SDR50_DATA_OUT_TAP 26
78 #define MMC_DEFAULT_TAP_DELAY 4
79 #define TOTAL_NO_OF_TAPS 512
80 static void octeontx_mmc_switch_to(struct mmc *mmc);
81 static void set_wdog(struct mmc *mmc, u64 us);
82 static void do_switch(struct mmc *mmc, union mio_emm_switch emm_switch);
83 static int octeontx_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
84 struct mmc_data *data);
85 static int octeontx_mmc_configure_delay(struct mmc *mmc);
86 static int octeontx_mmc_calibrate_delay(struct mmc *mmc);
87 #if !defined(CONFIG_ARCH_OCTEON)
88 static int octeontx2_mmc_calc_delay(struct mmc *mmc, int delay);
89 static void octeontx_mmc_set_timing(struct mmc *mmc);
90 static int octeontx_mmc_set_input_bus_timing(struct mmc *mmc);
91 static int octeontx_mmc_set_output_bus_timing(struct mmc *mmc);
94 static bool host_probed;
97 * Get the slot data structure from a MMC data structure
99 static inline struct octeontx_mmc_slot *mmc_to_slot(struct mmc *mmc)
101 return container_of(mmc, struct octeontx_mmc_slot, mmc);
104 static inline struct octeontx_mmc_host *mmc_to_host(struct mmc *mmc)
106 return mmc_to_slot(mmc)->host;
109 static inline struct octeontx_mmc_slot *dev_to_mmc_slot(struct udevice *dev)
111 return dev_get_priv(dev);
114 static inline struct mmc *dev_to_mmc(struct udevice *dev)
116 return &((struct octeontx_mmc_slot *)dev_get_priv(dev))->mmc;
120 const char *mmc_reg_str(u64 reg)
122 if (reg == MIO_EMM_DMA_CFG())
123 return "MIO_EMM_DMA_CFG";
124 if (reg == MIO_EMM_DMA_ADR())
125 return "MIO_EMM_DMA_ADR";
126 if (reg == MIO_EMM_DMA_INT())
127 return "MIO_EMM_DMA_INT";
128 if (reg == MIO_EMM_CFG())
129 return "MIO_EMM_CFG";
130 if (reg == MIO_EMM_MODEX(0))
131 return "MIO_EMM_MODE0";
132 if (reg == MIO_EMM_MODEX(1))
133 return "MIO_EMM_MODE1";
134 if (reg == MIO_EMM_MODEX(2))
135 return "MIO_EMM_MODE2";
136 if (reg == MIO_EMM_MODEX(3))
137 return "MIO_EMM_MODE3";
138 if (reg == MIO_EMM_IO_CTL())
139 return "MIO_EMM_IO_CTL";
140 if (reg == MIO_EMM_SWITCH())
141 return "MIO_EMM_SWITCH";
142 if (reg == MIO_EMM_DMA())
143 return "MIO_EMM_DMA";
144 if (reg == MIO_EMM_CMD())
145 return "MIO_EMM_CMD";
146 if (reg == MIO_EMM_RSP_STS())
147 return "MIO_EMM_RSP_STS";
148 if (reg == MIO_EMM_RSP_LO())
149 return "MIO_EMM_RSP_LO";
150 if (reg == MIO_EMM_RSP_HI())
151 return "MIO_EMM_RSP_HI";
152 if (reg == MIO_EMM_INT())
153 return "MIO_EMM_INT";
154 if (reg == MIO_EMM_WDOG())
155 return "MIO_EMM_WDOG";
156 if (reg == MIO_EMM_DMA_ARG())
157 return "MIO_EMM_DMA_ARG";
158 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
159 if (reg == MIO_EMM_SAMPLE())
160 return "MIO_EMM_SAMPLE";
162 if (reg == MIO_EMM_STS_MASK())
163 return "MIO_EMM_STS_MASK";
164 if (reg == MIO_EMM_RCA())
165 return "MIO_EMM_RCA";
166 if (reg == MIO_EMM_BUF_IDX())
167 return "MIO_EMM_BUF_IDX";
168 if (reg == MIO_EMM_BUF_DAT())
169 return "MIO_EMM_BUF_DAT";
170 if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
171 if (reg == MIO_EMM_CALB())
172 return "MIO_EMM_CALB";
173 if (reg == MIO_EMM_TAP())
174 return "MIO_EMM_TAP";
175 if (reg == MIO_EMM_TIMING())
176 return "MIO_EMM_TIMING";
177 if (reg == MIO_EMM_DEBUG())
178 return "MIO_EMM_DEBUG";
185 static void octeontx_print_rsp_sts(struct mmc *mmc)
188 union mio_emm_rsp_sts emm_rsp_sts;
189 const struct octeontx_mmc_host *host = mmc_to_host(mmc);
190 static const char * const ctype_xor_str[] = {
192 "Read data into Dbuf",
193 "Write data from Dbuf",
197 static const char * const rtype_xor_str[] = {
208 emm_rsp_sts.u = readq(host->base_addr + MIO_EMM_RSP_STS());
209 printf("\nMIO_EMM_RSP_STS: 0x%016llx\n", emm_rsp_sts.u);
210 printf(" 60-61: bus_id: %u\n", emm_rsp_sts.s.bus_id);
211 printf(" 59: cmd_val: %s\n",
212 emm_rsp_sts.s.cmd_val ? "yes" : "no");
213 printf(" 58: switch_val: %s\n",
214 emm_rsp_sts.s.switch_val ? "yes" : "no");
215 printf(" 57: dma_val: %s\n",
216 emm_rsp_sts.s.dma_val ? "yes" : "no");
217 printf(" 56: dma_pend: %s\n",
218 emm_rsp_sts.s.dma_pend ? "yes" : "no");
219 printf(" 28: dbuf_err: %s\n",
220 emm_rsp_sts.s.dbuf_err ? "yes" : "no");
221 printf(" 23: dbuf: %u\n", emm_rsp_sts.s.dbuf);
222 printf(" 22: blk_timeout: %s\n",
223 emm_rsp_sts.s.blk_timeout ? "yes" : "no");
224 printf(" 21: blk_crc_err: %s\n",
225 emm_rsp_sts.s.blk_crc_err ? "yes" : "no");
226 printf(" 20: rsp_busybit: %s\n",
227 emm_rsp_sts.s.rsp_busybit ? "yes" : "no");
228 printf(" 19: stp_timeout: %s\n",
229 emm_rsp_sts.s.stp_timeout ? "yes" : "no");
230 printf(" 18: stp_crc_err: %s\n",
231 emm_rsp_sts.s.stp_crc_err ? "yes" : "no");
232 printf(" 17: stp_bad_sts: %s\n",
233 emm_rsp_sts.s.stp_bad_sts ? "yes" : "no");
234 printf(" 16: stp_val: %s\n",
235 emm_rsp_sts.s.stp_val ? "yes" : "no");
236 printf(" 15: rsp_timeout: %s\n",
237 emm_rsp_sts.s.rsp_timeout ? "yes" : "no");
238 printf(" 14: rsp_crc_err: %s\n",
239 emm_rsp_sts.s.rsp_crc_err ? "yes" : "no");
240 printf(" 13: rsp_bad_sts: %s\n",
241 emm_rsp_sts.s.rsp_bad_sts ? "yes" : "no");
242 printf(" 12: rsp_val: %s\n",
243 emm_rsp_sts.s.rsp_val ? "yes" : "no");
244 printf(" 9-11: rsp_type: %s\n",
245 rtype_xor_str[emm_rsp_sts.s.rsp_type]);
246 printf(" 7-8: cmd_type: %s\n",
247 ctype_xor_str[emm_rsp_sts.s.cmd_type]);
248 printf(" 1-6: cmd_idx: %u\n",
249 emm_rsp_sts.s.cmd_idx);
250 printf(" 0: cmd_done: %s\n",
251 emm_rsp_sts.s.cmd_done ? "yes" : "no");
255 static inline u64 read_csr(struct mmc *mmc, u64 reg)
257 const struct octeontx_mmc_host *host = mmc_to_host(mmc);
258 u64 value = readq(host->base_addr + reg);
260 printf(" %s: %s(0x%p) => 0x%llx\n", __func__,
261 mmc_reg_str(reg), host->base_addr + reg,
268 * Writes to a CSR register
270 * @param[in] mmc pointer to mmc data structure
271 * @param reg register offset
272 * @param value value to write to register
274 static inline void write_csr(struct mmc *mmc, u64 reg, u64 value)
276 const struct octeontx_mmc_host *host = mmc_to_host(mmc);
277 void *addr = host->base_addr + reg;
280 printf(" %s: %s(0x%p) <= 0x%llx\n", __func__, mmc_reg_str(reg),
287 static void mmc_print_status(u32 status)
290 static const char * const state[] = {
306 "reserved" /* 15 */ };
307 if (status & R1_APP_CMD)
309 if (status & R1_SWITCH_ERROR)
310 puts("MMC switch error\n");
311 if (status & R1_READY_FOR_DATA)
312 puts("MMC ready for data\n");
313 printf("MMC %s state\n", state[R1_CURRENT_STATE(status)]);
314 if (status & R1_ERASE_RESET)
315 puts("MMC erase reset\n");
316 if (status & R1_WP_ERASE_SKIP)
317 puts("MMC partial erase due to write protected blocks\n");
318 if (status & R1_CID_CSD_OVERWRITE)
319 puts("MMC CID/CSD overwrite error\n");
320 if (status & R1_ERROR)
321 puts("MMC undefined device error\n");
322 if (status & R1_CC_ERROR)
323 puts("MMC device error\n");
324 if (status & R1_CARD_ECC_FAILED)
325 puts("MMC internal ECC failed to correct data\n");
326 if (status & R1_ILLEGAL_COMMAND)
327 puts("MMC illegal command\n");
328 if (status & R1_COM_CRC_ERROR)
329 puts("MMC CRC of previous command failed\n");
330 if (status & R1_LOCK_UNLOCK_FAILED)
331 puts("MMC sequence or password error in lock/unlock device command\n");
332 if (status & R1_CARD_IS_LOCKED)
333 puts("MMC device locked by host\n");
334 if (status & R1_WP_VIOLATION)
335 puts("MMC attempt to program write protected block\n");
336 if (status & R1_ERASE_PARAM)
337 puts("MMC invalid selection of erase groups for erase\n");
338 if (status & R1_ERASE_SEQ_ERROR)
339 puts("MMC error in sequence of erase commands\n");
340 if (status & R1_BLOCK_LEN_ERROR)
341 puts("MMC block length error\n");
342 if (status & R1_ADDRESS_ERROR)
343 puts("MMC address misalign error\n");
344 if (status & R1_OUT_OF_RANGE)
345 puts("MMC address out of range\n");
350 #if !defined(CONFIG_ARCH_OCTEON)
352 * Print out all of the register values where mmc is optional
354 * @param mmc MMC device (can be NULL)
355 * @param host Pointer to host data structure (can be NULL if mmc is !NULL)
357 static void octeontx_mmc_print_registers2(struct mmc *mmc,
358 struct octeontx_mmc_host *host)
360 struct octeontx_mmc_slot *slot = mmc ? mmc->priv : NULL;
361 union mio_emm_dma_cfg emm_dma_cfg;
362 union mio_emm_dma_adr emm_dma_adr;
363 union mio_emm_dma_int emm_dma_int;
364 union mio_emm_cfg emm_cfg;
365 union mio_emm_modex emm_mode;
366 union mio_emm_switch emm_switch;
367 union mio_emm_dma emm_dma;
368 union mio_emm_cmd emm_cmd;
369 union mio_emm_rsp_sts emm_rsp_sts;
370 union mio_emm_rsp_lo emm_rsp_lo;
371 union mio_emm_rsp_hi emm_rsp_hi;
372 union mio_emm_int emm_int;
373 union mio_emm_wdog emm_wdog;
374 union mio_emm_sample emm_sample;
375 union mio_emm_calb emm_calb;
376 union mio_emm_tap emm_tap;
377 union mio_emm_timing emm_timing;
378 union mio_emm_io_ctl io_ctl;
379 union mio_emm_debug emm_debug;
380 union mio_emm_sts_mask emm_sts_mask;
381 union mio_emm_rca emm_rca;
384 static const char * const bus_width_str[] = {
385 "1-bit data bus (power on)",
390 "4-bit data bus (dual data rate)",
391 "8-bit data bus (dual data rate)",
402 static const char * const ctype_xor_str[] = {
404 "Read data into Dbuf",
405 "Write data from Dbuf",
409 static const char * const rtype_xor_str[] = {
421 host = mmc_to_host(mmc);
424 printf("%s: bus id: %u\n", __func__, slot->bus_id);
425 emm_dma_cfg.u = readq(host->base_addr + MIO_EMM_DMA_CFG());
426 printf("MIO_EMM_DMA_CFG: 0x%016llx\n",
428 printf(" 63: en: %s\n",
429 emm_dma_cfg.s.en ? "enabled" : "disabled");
430 printf(" 62: rw: %s\n",
431 emm_dma_cfg.s.rw ? "write" : "read");
432 printf(" 61: clr: %s\n",
433 emm_dma_cfg.s.clr ? "clear" : "not clear");
434 printf(" 59: swap32: %s\n",
435 emm_dma_cfg.s.swap32 ? "yes" : "no");
436 printf(" 58: swap16: %s\n",
437 emm_dma_cfg.s.swap16 ? "yes" : "no");
438 printf(" 57: swap8: %s\n",
439 emm_dma_cfg.s.swap8 ? "yes" : "no");
440 printf(" 56: endian: %s\n",
441 emm_dma_cfg.s.endian ? "little" : "big");
442 printf(" 36-55: size: %u\n",
445 emm_dma_adr.u = readq(host->base_addr + MIO_EMM_DMA_ADR());
446 printf("MIO_EMM_DMA_ADR: 0x%016llx\n", emm_dma_adr.u);
447 printf(" 0-49: adr: 0x%llx\n",
448 (u64)emm_dma_adr.s.adr);
450 emm_dma_int.u = readq(host->base_addr + MIO_EMM_DMA_INT());
451 printf("\nMIO_EMM_DMA_INT: 0x%016llx\n",
453 printf(" 1: FIFO: %s\n",
454 emm_dma_int.s.fifo ? "yes" : "no");
455 printf(" 0: Done: %s\n",
456 emm_dma_int.s.done ? "yes" : "no");
457 emm_cfg.u = readq(host->base_addr + MIO_EMM_CFG());
459 printf("\nMIO_EMM_CFG: 0x%016llx\n",
461 printf(" 3: bus_ena3: %s\n",
462 emm_cfg.s.bus_ena & 0x08 ? "yes" : "no");
463 printf(" 2: bus_ena2: %s\n",
464 emm_cfg.s.bus_ena & 0x04 ? "yes" : "no");
465 printf(" 1: bus_ena1: %s\n",
466 emm_cfg.s.bus_ena & 0x02 ? "yes" : "no");
467 printf(" 0: bus_ena0: %s\n",
468 emm_cfg.s.bus_ena & 0x01 ? "yes" : "no");
469 for (bus = 0; bus < 4; bus++) {
470 emm_mode.u = readq(host->base_addr + MIO_EMM_MODEX(bus));
471 printf("\nMIO_EMM_MODE%u: 0x%016llx\n",
473 if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
474 printf(" 50: hs400_timing: %s\n",
475 emm_mode.s.hs400_timing ? "yes" : "no");
476 printf(" 49: hs200_timing: %s\n",
477 emm_mode.s.hs200_timing ? "yes" : "no");
479 printf(" 48: hs_timing: %s\n",
480 emm_mode.s.hs_timing ? "yes" : "no");
481 printf(" 40-42: bus_width: %s\n",
482 bus_width_str[emm_mode.s.bus_width]);
483 printf(" 32-35: power_class %u\n",
484 emm_mode.s.power_class);
485 printf(" 16-31: clk_hi: %u\n",
487 printf(" 0-15: clk_lo: %u\n",
491 emm_switch.u = readq(host->base_addr + MIO_EMM_SWITCH());
492 printf("\nMIO_EMM_SWITCH: 0x%016llx\n", emm_switch.u);
493 printf(" 60-61: bus_id: %u\n", emm_switch.s.bus_id);
494 printf(" 59: switch_exe: %s\n",
495 emm_switch.s.switch_exe ? "yes" : "no");
496 printf(" 58: switch_err0: %s\n",
497 emm_switch.s.switch_err0 ? "yes" : "no");
498 printf(" 57: switch_err1: %s\n",
499 emm_switch.s.switch_err1 ? "yes" : "no");
500 printf(" 56: switch_err2: %s\n",
501 emm_switch.s.switch_err2 ? "yes" : "no");
502 printf(" 48: hs_timing: %s\n",
503 emm_switch.s.hs_timing ? "yes" : "no");
504 printf(" 42-40: bus_width: %s\n",
505 bus_width_str[emm_switch.s.bus_width]);
506 printf(" 32-35: power_class: %u\n",
507 emm_switch.s.power_class);
508 printf(" 16-31: clk_hi: %u\n",
509 emm_switch.s.clk_hi);
510 printf(" 0-15: clk_lo: %u\n", emm_switch.s.clk_lo);
512 emm_dma.u = readq(host->base_addr + MIO_EMM_DMA());
513 printf("\nMIO_EMM_DMA: 0x%016llx\n", emm_dma.u);
514 printf(" 60-61: bus_id: %u\n", emm_dma.s.bus_id);
515 printf(" 59: dma_val: %s\n",
516 emm_dma.s.dma_val ? "yes" : "no");
517 printf(" 58: sector: %s mode\n",
518 emm_dma.s.sector ? "sector" : "byte");
519 printf(" 57: dat_null: %s\n",
520 emm_dma.s.dat_null ? "yes" : "no");
521 printf(" 51-56: thres: %u\n", emm_dma.s.thres);
522 printf(" 50: rel_wr: %s\n",
523 emm_dma.s.rel_wr ? "yes" : "no");
524 printf(" 49: rw: %s\n",
525 emm_dma.s.rw ? "write" : "read");
526 printf(" 48: multi: %s\n",
527 emm_dma.s.multi ? "yes" : "no");
528 printf(" 32-47: block_cnt: %u\n",
529 emm_dma.s.block_cnt);
530 printf(" 0-31: card_addr: 0x%x\n",
531 emm_dma.s.card_addr);
533 emm_cmd.u = readq(host->base_addr + MIO_EMM_CMD());
534 printf("\nMIO_EMM_CMD: 0x%016llx\n", emm_cmd.u);
535 printf("\n 62: skip_busy: %s\n",
536 emm_cmd.s.skip_busy ? "yes" : "no");
537 printf(" 60-61: bus_id: %u\n", emm_cmd.s.bus_id);
538 printf(" 59: cmd_val: %s\n",
539 emm_cmd.s.cmd_val ? "yes" : "no");
540 printf(" 55: dbuf: %u\n", emm_cmd.s.dbuf);
541 printf(" 49-54: offset: %u\n", emm_cmd.s.offset);
542 printf(" 41-42: ctype_xor: %s\n",
543 ctype_xor_str[emm_cmd.s.ctype_xor]);
544 printf(" 38-40: rtype_xor: %s\n",
545 rtype_xor_str[emm_cmd.s.rtype_xor]);
546 printf(" 32-37: cmd_idx: %u\n", emm_cmd.s.cmd_idx);
547 printf(" 0-31: arg: 0x%x\n", emm_cmd.s.arg);
549 emm_rsp_sts.u = readq(host->base_addr + MIO_EMM_RSP_STS());
550 printf("\nMIO_EMM_RSP_STS: 0x%016llx\n", emm_rsp_sts.u);
551 printf(" 60-61: bus_id: %u\n", emm_rsp_sts.s.bus_id);
552 printf(" 59: cmd_val: %s\n",
553 emm_rsp_sts.s.cmd_val ? "yes" : "no");
554 printf(" 58: switch_val: %s\n",
555 emm_rsp_sts.s.switch_val ? "yes" : "no");
556 printf(" 57: dma_val: %s\n",
557 emm_rsp_sts.s.dma_val ? "yes" : "no");
558 printf(" 56: dma_pend: %s\n",
559 emm_rsp_sts.s.dma_pend ? "yes" : "no");
560 printf(" 28: dbuf_err: %s\n",
561 emm_rsp_sts.s.dbuf_err ? "yes" : "no");
562 printf(" 23: dbuf: %u\n", emm_rsp_sts.s.dbuf);
563 printf(" 22: blk_timeout: %s\n",
564 emm_rsp_sts.s.blk_timeout ? "yes" : "no");
565 printf(" 21: blk_crc_err: %s\n",
566 emm_rsp_sts.s.blk_crc_err ? "yes" : "no");
567 printf(" 20: rsp_busybit: %s\n",
568 emm_rsp_sts.s.rsp_busybit ? "yes" : "no");
569 printf(" 19: stp_timeout: %s\n",
570 emm_rsp_sts.s.stp_timeout ? "yes" : "no");
571 printf(" 18: stp_crc_err: %s\n",
572 emm_rsp_sts.s.stp_crc_err ? "yes" : "no");
573 printf(" 17: stp_bad_sts: %s\n",
574 emm_rsp_sts.s.stp_bad_sts ? "yes" : "no");
575 printf(" 16: stp_val: %s\n",
576 emm_rsp_sts.s.stp_val ? "yes" : "no");
577 printf(" 15: rsp_timeout: %s\n",
578 emm_rsp_sts.s.rsp_timeout ? "yes" : "no");
579 printf(" 14: rsp_crc_err: %s\n",
580 emm_rsp_sts.s.rsp_crc_err ? "yes" : "no");
581 printf(" 13: rsp_bad_sts: %s\n",
582 emm_rsp_sts.s.rsp_bad_sts ? "yes" : "no");
583 printf(" 12: rsp_val: %s\n",
584 emm_rsp_sts.s.rsp_val ? "yes" : "no");
585 printf(" 9-11: rsp_type: %s\n",
586 rtype_xor_str[emm_rsp_sts.s.rsp_type]);
587 printf(" 7-8: cmd_type: %s\n",
588 ctype_xor_str[emm_rsp_sts.s.cmd_type]);
589 printf(" 1-6: cmd_idx: %u\n",
590 emm_rsp_sts.s.cmd_idx);
591 printf(" 0: cmd_done: %s\n",
592 emm_rsp_sts.s.cmd_done ? "yes" : "no");
594 emm_rsp_lo.u = readq(host->base_addr + MIO_EMM_RSP_LO());
595 printf("\nMIO_EMM_RSP_STS_LO: 0x%016llx\n", emm_rsp_lo.u);
597 emm_rsp_hi.u = readq(host->base_addr + MIO_EMM_RSP_HI());
598 printf("\nMIO_EMM_RSP_STS_HI: 0x%016llx\n", emm_rsp_hi.u);
600 emm_int.u = readq(host->base_addr + MIO_EMM_INT());
601 printf("\nMIO_EMM_INT: 0x%016llx\n", emm_int.u);
602 printf(" 6: switch_err: %s\n",
603 emm_int.s.switch_err ? "yes" : "no");
604 printf(" 5: switch_done: %s\n",
605 emm_int.s.switch_done ? "yes" : "no");
606 printf(" 4: dma_err: %s\n",
607 emm_int.s.dma_err ? "yes" : "no");
608 printf(" 3: cmd_err: %s\n",
609 emm_int.s.cmd_err ? "yes" : "no");
610 printf(" 2: dma_done: %s\n",
611 emm_int.s.dma_done ? "yes" : "no");
612 printf(" 1: cmd_done: %s\n",
613 emm_int.s.cmd_done ? "yes" : "no");
614 printf(" 0: buf_done: %s\n",
615 emm_int.s.buf_done ? "yes" : "no");
617 emm_wdog.u = readq(host->base_addr + MIO_EMM_WDOG());
618 printf("\nMIO_EMM_WDOG: 0x%016llx (%u)\n",
619 emm_wdog.u, emm_wdog.s.clk_cnt);
621 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
622 emm_sample.u = readq(host->base_addr + MIO_EMM_SAMPLE());
623 printf("\nMIO_EMM_SAMPLE: 0x%016llx\n",
625 printf(" 16-25: cmd_cnt: %u\n",
626 emm_sample.s.cmd_cnt);
627 printf(" 0-9: dat_cnt: %u\n",
628 emm_sample.s.dat_cnt);
631 emm_sts_mask.u = readq(host->base_addr + MIO_EMM_STS_MASK());
632 printf("\nMIO_EMM_STS_MASK: 0x%016llx\n", emm_sts_mask.u);
634 emm_rca.u = readq(host->base_addr + MIO_EMM_RCA());
635 printf("\nMIO_EMM_RCA: 0x%016llx\n", emm_rca.u);
636 printf(" 0-15: card_rca: 0x%04x\n",
638 if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
639 emm_calb.u = readq(host->base_addr + MIO_EMM_CALB());
640 printf("\nMIO_EMM_CALB: 0x%016llx\n",
642 printf(" 0: start: %u\n",
644 emm_tap.u = readq(host->base_addr + MIO_EMM_TAP());
645 printf("\nMIO_EMM_TAP: 0x%016llx\n",
647 printf(" 7-0: delay: %u\n", emm_tap.s.delay);
648 emm_timing.u = readq(host->base_addr + MIO_EMM_TIMING());
649 printf("\nMIO_EMM_TIMING: 0x%016llx\n",
651 printf(" 53-48: cmd_in_tap: %u\n",
652 emm_timing.s.cmd_in_tap);
653 printf(" 37-32: cmd_out_tap: %u\n",
654 emm_timing.s.cmd_out_tap);
655 printf(" 21-16: data_in_tap: %u\n",
656 emm_timing.s.data_in_tap);
657 printf(" 5-0: data_out_tap: %u\n",
658 emm_timing.s.data_out_tap);
659 io_ctl.u = readq(host->base_addr + MIO_EMM_IO_CTL());
660 printf("\nMIO_IO_CTL: 0x%016llx\n", io_ctl.u);
661 printf(" 3-2: drive: %u (%u mA)\n",
662 io_ctl.s.drive, 2 << io_ctl.s.drive);
663 printf(" 0: slew: %u %s\n", io_ctl.s.slew,
664 io_ctl.s.slew ? "high" : "low");
665 emm_debug.u = readq(host->base_addr + MIO_EMM_DEBUG());
666 printf("\nMIO_EMM_DEBUG: 0x%016llx\n",
668 printf(" 21: rdsync_rst 0x%x\n",
669 emm_debug.s.rdsync_rst);
670 printf(" 20: emmc_clk_disable 0x%x\n",
671 emm_debug.s.emmc_clk_disable);
672 printf(" 19-16: dma_sm: 0x%x\n",
674 printf(" 15-12: data_sm: 0x%x\n",
675 emm_debug.s.data_sm);
676 printf(" 11-8: cmd_sm: 0x%x\n",
678 printf(" 0: clk_on: 0x%x\n",
686 * Print out all of the register values
688 * @param mmc MMC device
690 static void octeontx_mmc_print_registers(struct mmc *mmc)
692 #ifdef DEBUG_REGISTERS
698 octeontx_mmc_print_registers2(mmc, mmc_to_host(mmc));
701 static void octeontx_mmc_print_registers(struct mmc *mmc)
707 static const struct octeontx_sd_mods octeontx_cr_types[] = {
708 { {0, 0}, {0, 0}, {0, 0} }, /* CMD0 */
709 { {0, 3}, {0, 3}, {0, 0} }, /* CMD1 */
710 { {0, 2}, {0, 2}, {0, 0} }, /* CMD2 */
711 { {0, 1}, {0, 3}, {0, 0} }, /* CMD3 SD_CMD_SEND_RELATIVE_ADDR 0, 2 */
712 { {0, 0}, {0, 0}, {0, 0} }, /* CMD4 */
713 { {0, 1}, {0, 1}, {0, 0} }, /* CMD5 */
714 { {0, 1}, {1, 1}, {0, 1} }, /*
715 * CMD6 SD_CMD_SWITCH_FUNC 1,0
716 * (ACMD) SD_APP_SET_BUS_WIDTH
718 { {0, 1}, {0, 1}, {0, 0} }, /* CMD7 */
719 { {1, 1}, {0, 3}, {0, 0} }, /* CMD8 SD_CMD_SEND_IF_COND 1,2 */
720 { {0, 2}, {0, 2}, {0, 0} }, /* CMD9 */
721 { {0, 2}, {0, 2}, {0, 0} }, /* CMD10 */
722 { {1, 1}, {0, 1}, {1, 1} }, /* CMD11 SD_CMD_SWITCH_UHS18V 1,0 */
723 { {0, 1}, {0, 1}, {0, 0} }, /* CMD12 */
724 { {0, 1}, {0, 1}, {1, 3} }, /* CMD13 (ACMD)) SD_CMD_APP_SD_STATUS 1,2 */
725 { {1, 1}, {1, 1}, {0, 0} }, /* CMD14 */
726 { {0, 0}, {0, 0}, {0, 0} }, /* CMD15 */
727 { {0, 1}, {0, 1}, {0, 0} }, /* CMD16 */
728 { {1, 1}, {1, 1}, {0, 0} }, /* CMD17 */
729 { {1, 1}, {1, 1}, {0, 0} }, /* CMD18 */
730 { {3, 1}, {3, 1}, {0, 0} }, /* CMD19 */
731 { {2, 1}, {0, 0}, {0, 0} }, /* CMD20 */ /* SD 2,0 */
732 { {0, 0}, {0, 0}, {0, 0} }, /* CMD21 */
733 { {0, 0}, {0, 0}, {1, 1} }, /* CMD22 (ACMD) SD_APP_SEND_NUM_WR_BLKS 1,0 */
734 { {0, 1}, {0, 1}, {0, 1} }, /* CMD23 */ /* SD ACMD 1,0 */
735 { {2, 1}, {2, 1}, {2, 1} }, /* CMD24 */
736 { {2, 1}, {2, 1}, {2, 1} }, /* CMD25 */
737 { {2, 1}, {2, 1}, {2, 1} }, /* CMD26 */
738 { {2, 1}, {2, 1}, {2, 1} }, /* CMD27 */
739 { {0, 1}, {0, 1}, {0, 1} }, /* CMD28 */
740 { {0, 1}, {0, 1}, {0, 1} }, /* CMD29 */
741 { {1, 1}, {1, 1}, {1, 1} }, /* CMD30 */
742 { {1, 1}, {1, 1}, {1, 1} }, /* CMD31 */
743 { {0, 0}, {0, 1}, {0, 0} }, /* CMD32 SD_CMD_ERASE_WR_BLK_START 0,1 */
744 { {0, 0}, {0, 1}, {0, 0} }, /* CMD33 SD_CMD_ERASE_WR_BLK_END 0,1 */
745 { {0, 0}, {0, 0}, {0, 0} }, /* CMD34 */
746 { {0, 1}, {0, 1}, {0, 1} }, /* CMD35 */
747 { {0, 1}, {0, 1}, {0, 1} }, /* CMD36 */
748 { {0, 0}, {0, 0}, {0, 0} }, /* CMD37 */
749 { {0, 1}, {0, 1}, {0, 1} }, /* CMD38 */
750 { {0, 4}, {0, 4}, {0, 4} }, /* CMD39 */
751 { {0, 5}, {0, 5}, {0, 5} }, /* CMD40 */
752 { {0, 0}, {0, 0}, {0, 3} }, /* CMD41 (ACMD) SD_CMD_APP_SEND_OP_COND 0,3 */
753 { {2, 1}, {2, 1}, {2, 1} }, /* CMD42 */
754 { {0, 0}, {0, 0}, {0, 0} }, /* CMD43 */
755 { {0, 0}, {0, 0}, {0, 0} }, /* CMD44 */
756 { {0, 0}, {0, 0}, {0, 0} }, /* CMD45 */
757 { {0, 0}, {0, 0}, {0, 0} }, /* CMD46 */
758 { {0, 0}, {0, 0}, {0, 0} }, /* CMD47 */
759 { {0, 0}, {1, 0}, {0, 0} }, /* CMD48 SD_CMD_READ_EXTR_SINGLE */
760 { {0, 0}, {2, 0}, {0, 0} }, /* CMD49 SD_CMD_WRITE_EXTR_SINGLE */
761 { {0, 0}, {0, 0}, {0, 0} }, /* CMD50 */
762 { {0, 0}, {0, 0}, {1, 1} }, /* CMD51 (ACMD) SD_CMD_APP_SEND_SCR 1,1 */
763 { {0, 0}, {0, 0}, {0, 0} }, /* CMD52 */
764 { {0, 0}, {0, 0}, {0, 0} }, /* CMD53 */
765 { {0, 0}, {0, 0}, {0, 0} }, /* CMD54 */
766 { {0, 1}, {0, 1}, {0, 1} }, /* CMD55 */
767 { {0xff, 0xff}, {0xff, 0xff}, {0xff, 0xff} }, /* CMD56 */
768 { {0, 0}, {0, 0}, {0, 0} }, /* CMD57 */
769 { {0, 0}, {0, 3}, {0, 3} }, /* CMD58 SD_CMD_SPI_READ_OCR 0,3 */
770 { {0, 0}, {0, 1}, {0, 0} }, /* CMD59 SD_CMD_SPI_CRC_ON_OFF 0,1 */
771 { {0, 0}, {0, 0}, {0, 0} }, /* CMD60 */
772 { {0, 0}, {0, 0}, {0, 0} }, /* CMD61 */
773 { {0, 0}, {0, 0}, {0, 0} }, /* CMD62 */
774 { {0, 0}, {0, 0}, {0, 0} } /* CMD63 */
778 * Returns XOR values needed for SD commands and other quirks
780 * @param mmc mmc device
781 * @param cmd command information
783 * Return: octeontx_mmc_cr_mods data structure with various quirks and flags
785 static struct octeontx_mmc_cr_mods
786 octeontx_mmc_get_cr_mods(struct mmc *mmc, const struct mmc_cmd *cmd,
787 const struct mmc_data *data)
789 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
790 struct octeontx_mmc_cr_mods cr = {0, 0};
791 const struct octeontx_sd_mods *sdm =
792 &octeontx_cr_types[cmd->cmdidx & 0x3f];
793 u8 c = sdm->mmc.c, r = sdm->mmc.r;
794 u8 desired_ctype = 0;
797 #ifdef MMC_SUPPORTS_TUNING
798 if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
799 if (cmd->resp_type == MMC_RSP_R1)
801 if (data && data->flags & MMC_DATA_READ)
808 if (cmd->cmdidx == 56)
809 c = (cmd->cmdarg & 1) ? 1 : 2;
812 if (data->flags & MMC_DATA_READ)
814 else if (data->flags & MMC_DATA_WRITE)
818 cr.ctype_xor = c ^ desired_ctype;
820 cr.rtype_xor = r ^ sdm->sdacmd.r;
822 cr.rtype_xor = r ^ sdm->sd.r;
824 debug("%s(%s): mmc c: %d, mmc r: %d, desired c: %d, xor c: %d, xor r: %d\n",
825 __func__, mmc->dev->name, c, r, desired_ctype,
826 cr.ctype_xor, cr.rtype_xor);
831 * Keep track of switch commands internally
833 static void octeontx_mmc_track_switch(struct mmc *mmc, u32 cmd_arg)
835 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
836 u8 how = (cmd_arg >> 24) & 3;
837 u8 where = (u8)(cmd_arg >> 16);
838 u8 val = (u8)(cmd_arg >> 8);
840 slot->want_switch = slot->cached_switch;
849 case EXT_CSD_BUS_WIDTH:
850 slot->want_switch.s.bus_width = val;
852 case EXT_CSD_POWER_CLASS:
853 slot->want_switch.s.power_class = val;
855 case EXT_CSD_HS_TIMING:
856 slot->want_switch.s.hs_timing = 0;
857 #if !defined(CONFIG_ARCH_OCTEON)
858 slot->want_switch.s.hs200_timing = 0;
859 slot->want_switch.s.hs400_timing = 0;
865 slot->want_switch.s.hs_timing = 1;
867 #if !defined(CONFIG_ARCH_OCTEON)
869 if (!slot->is_asim && !slot->is_emul)
870 slot->want_switch.s.hs200_timing = 1;
873 if (!slot->is_asim && !slot->is_emul)
874 slot->want_switch.s.hs400_timing = 1;
878 pr_err("%s(%s): Unsupported timing mode 0x%x\n",
879 __func__, mmc->dev->name, val & 0xf);
888 static int octeontx_mmc_print_rsp_errors(struct mmc *mmc,
889 union mio_emm_rsp_sts rsp_sts)
892 const char *name = mmc->dev->name;
894 if (rsp_sts.s.acc_timeout) {
895 pr_warn("%s(%s): acc_timeout\n", __func__, name);
898 if (rsp_sts.s.dbuf_err) {
899 pr_warn("%s(%s): dbuf_err\n", __func__, name);
902 if (rsp_sts.s.blk_timeout) {
903 pr_warn("%s(%s): blk_timeout\n", __func__, name);
906 if (rsp_sts.s.blk_crc_err) {
907 pr_warn("%s(%s): blk_crc_err\n", __func__, name);
910 if (rsp_sts.s.stp_timeout) {
911 pr_warn("%s(%s): stp_timeout\n", __func__, name);
914 if (rsp_sts.s.stp_crc_err) {
915 pr_warn("%s(%s): stp_crc_err\n", __func__, name);
918 if (rsp_sts.s.stp_bad_sts) {
919 pr_warn("%s(%s): stp_bad_sts\n", __func__, name);
923 pr_warn(" rsp_sts: 0x%llx\n", rsp_sts.u);
929 * Starts a DMA operation for block read/write
931 * @param mmc mmc device
932 * @param write true if write operation
933 * @param clear true to clear DMA operation
934 * @param adr source or destination DMA address
935 * @param size size in blocks
936 * @param timeout timeout in ms
938 static void octeontx_mmc_start_dma(struct mmc *mmc, bool write,
939 bool clear, u32 block, dma_addr_t adr,
940 u32 size, int timeout)
942 const struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
943 union mio_emm_dma_cfg emm_dma_cfg;
944 union mio_emm_dma_adr emm_dma_adr;
945 union mio_emm_dma emm_dma;
947 /* Clear any interrupts */
948 write_csr(mmc, MIO_EMM_DMA_INT(),
949 read_csr(mmc, MIO_EMM_DMA_INT()));
952 emm_dma_cfg.s.en = 1;
953 emm_dma_cfg.s.rw = !!write;
954 emm_dma_cfg.s.clr = !!clear;
955 emm_dma_cfg.s.size = ((u64)(size * mmc->read_bl_len) / 8) - 1;
956 #if __BYTE_ORDER != __BIG_ENDIAN
957 emm_dma_cfg.s.endian = 1;
960 emm_dma_adr.s.adr = adr;
961 write_csr(mmc, MIO_EMM_DMA_ADR(), emm_dma_adr.u);
962 write_csr(mmc, MIO_EMM_DMA_CFG(), emm_dma_cfg.u);
965 emm_dma.s.bus_id = slot->bus_id;
966 emm_dma.s.dma_val = 1;
967 emm_dma.s.rw = !!write;
968 emm_dma.s.sector = mmc->high_capacity ? 1 : 0;
970 if (size > 1 && ((IS_SD(mmc) && (mmc->scr[0] & 2)) || !IS_SD(mmc)))
975 emm_dma.s.block_cnt = size;
976 if (!mmc->high_capacity)
977 block *= mmc->read_bl_len;
978 emm_dma.s.card_addr = block;
979 debug("%s(%s): card address: 0x%x, size: %d, multi: %d\n",
980 __func__, mmc->dev->name, block, size, emm_dma.s.multi);
983 timeout = (timeout * 1000) - 1000;
984 set_wdog(mmc, timeout);
986 debug(" Writing 0x%llx to mio_emm_dma\n", emm_dma.u);
987 write_csr(mmc, MIO_EMM_DMA(), emm_dma.u);
991 * Waits for a DMA operation to complete
993 * @param mmc mmc device
994 * @param timeout timeout in ms
996 * Return: 0 for success (could be DMA errors), -ETIMEDOUT on timeout
1000 * Cleanup DMA engine after a failure
1002 * @param mmc mmc device
1003 * @param rsp_sts rsp status
1005 static void octeontx_mmc_cleanup_dma(struct mmc *mmc,
1006 union mio_emm_rsp_sts rsp_sts)
1008 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
1009 union mio_emm_dma emm_dma;
1014 debug("%s(%s): rsp_sts: 0x%llx, rsp_lo: 0x%llx, dma_int: 0x%llx\n",
1015 __func__, mmc->dev->name, rsp_sts.u,
1016 read_csr(mmc, MIO_EMM_RSP_LO()),
1017 read_csr(mmc, MIO_EMM_DMA_INT()));
1018 emm_dma.u = read_csr(mmc, MIO_EMM_DMA());
1019 emm_dma.s.dma_val = 1;
1020 emm_dma.s.dat_null = 1;
1021 emm_dma.s.bus_id = slot->bus_id;
1022 write_csr(mmc, MIO_EMM_DMA(), emm_dma.u);
1023 start = get_timer(0);
1025 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1027 } while (get_timer(start) < 100 &&
1028 (rsp_sts.s.dma_val || rsp_sts.s.dma_pend));
1029 } while (retries-- >= 0 && rsp_sts.s.dma_pend);
1030 if (rsp_sts.s.dma_val)
1031 pr_err("%s(%s): Error: could not clean up DMA. RSP_STS: 0x%llx, RSP_LO: 0x%llx\n",
1032 __func__, mmc->dev->name, rsp_sts.u,
1033 read_csr(mmc, MIO_EMM_RSP_LO()));
1034 debug(" rsp_sts after clearing up DMA: 0x%llx\n",
1035 read_csr(mmc, MIO_EMM_RSP_STS()));
1039 * Waits for a DMA operation to complete
1041 * @param mmc mmc device
1042 * @param timeout timeout in ms
1043 * @param verbose true to print out error information
1045 * Return: 0 for success (could be DMA errors), -ETIMEDOUT on timeout
1046 * or -EIO if IO error.
1048 static int octeontx_mmc_wait_dma(struct mmc *mmc, bool write, ulong timeout,
1051 struct octeontx_mmc_host *host = mmc_to_host(mmc);
1052 ulong start_time = get_timer(0);
1053 union mio_emm_dma_int emm_dma_int;
1054 union mio_emm_rsp_sts rsp_sts;
1055 union mio_emm_dma emm_dma;
1056 bool timed_out = false;
1059 debug("%s(%s, %lu, %d), delay: %uus\n", __func__, mmc->dev->name,
1060 timeout, verbose, host->dma_wait_delay);
1062 udelay(host->dma_wait_delay);
1064 emm_dma_int.u = read_csr(mmc, MIO_EMM_DMA_INT());
1065 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1067 if ((rsp_sts.s.dma_pend && !rsp_sts.s.dma_val) ||
1068 rsp_sts.s.blk_timeout ||
1069 rsp_sts.s.stp_timeout ||
1070 rsp_sts.s.rsp_timeout) {
1073 debug("%s: f1\n", __func__);
1074 octeontx_mmc_print_rsp_errors(mmc, rsp_sts);
1079 if (rsp_sts.s.blk_crc_err ||
1080 (rsp_sts.s.dma_pend && !rsp_sts.s.dma_val)) {
1083 octeontx_mmc_print_rsp_errors(mmc, rsp_sts);
1088 if (rsp_sts.s.dma_pend) {
1090 * If this is set then an error has occurred.
1091 * Try and restart the DMA operation.
1093 emm_dma.u = read_csr(mmc, MIO_EMM_DMA());
1095 pr_err("%s(%s): DMA pending error: rsp_sts: 0x%llx, dma_int: 0x%llx, emm_dma: 0x%llx\n",
1096 __func__, mmc->dev->name, rsp_sts.u,
1097 emm_dma_int.u, emm_dma.u);
1098 octeontx_print_rsp_sts(mmc);
1099 debug(" MIO_EMM_DEBUG: 0x%llx\n",
1100 read_csr(mmc, MIO_EMM_DEBUG()));
1101 pr_err("%s: Trying DMA resume...\n", __func__);
1103 emm_dma.s.dma_val = 1;
1104 emm_dma.s.dat_null = 1;
1105 write_csr(mmc, MIO_EMM_DMA(), emm_dma.u);
1107 } else if (!rsp_sts.s.dma_val && emm_dma_int.s.done) {
1111 timed_out = (get_timer(start_time) > timeout);
1112 } while (!timed_out);
1114 if (timed_out || err) {
1116 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",
1117 __func__, mmc->dev->name,
1118 timed_out ? "timed out" : "error",
1119 get_timer(start_time), rsp_sts.u,
1121 read_csr(mmc, MIO_EMM_RSP_LO()),
1122 read_csr(mmc, MIO_EMM_DMA()));
1123 octeontx_print_rsp_sts(mmc);
1125 if (rsp_sts.s.dma_pend)
1126 octeontx_mmc_cleanup_dma(mmc, rsp_sts);
1128 write_csr(mmc, MIO_EMM_DMA_INT(),
1129 read_csr(mmc, MIO_EMM_DMA_INT()));
1132 return timed_out ? -ETIMEDOUT : (err ? -EIO : 0);
1136 * Read blocks from the MMC/SD device
1138 * @param mmc mmc device
1139 * @param cmd command
1140 * @param data data for read
1141 * @param verbose true to print out error information
1143 * Return: number of blocks read or 0 if error
1145 static int octeontx_mmc_read_blocks(struct mmc *mmc, struct mmc_cmd *cmd,
1146 struct mmc_data *data, bool verbose)
1148 struct octeontx_mmc_host *host = mmc_to_host(mmc);
1149 union mio_emm_rsp_sts rsp_sts;
1150 dma_addr_t dma_addr = (dma_addr_t)dm_pci_virt_to_mem(host->dev,
1153 ulong blkcnt = data->blocks;
1154 ulong start = cmd->cmdarg;
1155 int timeout = 1000 + blkcnt * 20;
1156 bool timed_out = false;
1157 bool multi_xfer = cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK;
1159 debug("%s(%s): dest: %p, dma address: 0x%llx, blkcnt: %lu, start: %lu\n",
1160 __func__, mmc->dev->name, data->dest, dma_addr, blkcnt, start);
1161 debug("%s: rsp_sts: 0x%llx\n", __func__,
1162 read_csr(mmc, MIO_EMM_RSP_STS()));
1163 /* use max timeout for multi-block transfers */
1167 * If we have a valid SD card in the slot, we set the response bit
1168 * mask to check for CRC errors and timeouts only.
1169 * Otherwise, use the default power on reset value.
1171 write_csr(mmc, MIO_EMM_STS_MASK(),
1172 IS_SD(mmc) ? 0x00b00000ull : 0xe4390080ull);
1173 invalidate_dcache_range((u64)data->dest,
1174 (u64)data->dest + blkcnt * data->blocksize);
1177 octeontx_mmc_start_dma(mmc, false, false, start, dma_addr,
1179 timed_out = !!octeontx_mmc_wait_dma(mmc, false, timeout,
1181 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1182 if (timed_out || rsp_sts.s.dma_val || rsp_sts.s.dma_pend) {
1186 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",
1187 __func__, mmc->dev->name, rsp_sts.u,
1188 read_csr(mmc, MIO_EMM_INT()),
1189 read_csr(mmc, MIO_EMM_DMA_INT()),
1190 read_csr(mmc, MIO_EMM_RSP_LO()));
1191 pr_err("%s: block count: %lu, start: 0x%lx\n",
1192 __func__, blkcnt, start);
1193 octeontx_mmc_print_registers(mmc);
1200 octeontx_mmc_start_dma(mmc, false, false, start,
1201 dma_addr, 1, timeout);
1202 dma_addr += mmc->read_bl_len;
1205 timed_out = !!octeontx_mmc_wait_dma(mmc, false,
1207 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1208 if (timed_out || rsp_sts.s.dma_val ||
1209 rsp_sts.s.dma_pend) {
1211 pr_err("%s: Error: DMA timed out. rsp_sts: 0x%llx, emm_int: 0x%llx, dma_int: 0x%llx, rsp_lo: 0x%llx\n",
1212 __func__, rsp_sts.u,
1213 read_csr(mmc, MIO_EMM_INT()),
1214 read_csr(mmc, MIO_EMM_DMA_INT()),
1215 read_csr(mmc, MIO_EMM_RSP_LO()));
1216 pr_err("%s: block count: 1, start: 0x%lx\n",
1218 octeontx_mmc_print_registers(mmc);
1220 return blkcnt - count;
1226 debug("%s(%s): Read %lu (0x%lx) blocks starting at block %u (0x%x) to address %p (dma address 0x%llx)\n",
1227 __func__, mmc->dev->name, blkcnt, blkcnt,
1228 cmd->cmdarg, cmd->cmdarg, data->dest,
1229 dm_pci_virt_to_mem(host->dev, data->dest));
1230 print_buffer(0, data->dest, 1, 0x200, 0);
1235 static int octeontx_mmc_poll_ready(struct mmc *mmc, ulong timeout)
1240 bool not_ready = false;
1242 memset(&cmd, 0, sizeof(cmd));
1243 cmd.cmdidx = MMC_CMD_SEND_STATUS;
1244 cmd.cmdarg = mmc->rca << 16;
1245 cmd.resp_type = MMC_RSP_R1;
1246 start = get_timer(0);
1248 err = octeontx_mmc_send_cmd(mmc, &cmd, NULL);
1250 pr_err("%s(%s): MMC command error: %d; Retry...\n",
1251 __func__, mmc->dev->name, err);
1253 } else if (cmd.response[0] & R1_READY_FOR_DATA) {
1257 } while (get_timer(start) < timeout);
1260 pr_err("%s(%s): MMC command error; Retry timeout\n",
1261 __func__, mmc->dev->name);
1265 static ulong octeontx_mmc_write_blocks(struct mmc *mmc, struct mmc_cmd *cmd,
1266 struct mmc_data *data)
1268 struct octeontx_mmc_host *host = mmc_to_host(mmc);
1269 ulong start = cmd->cmdarg;
1270 ulong blkcnt = data->blocks;
1271 dma_addr_t dma_addr;
1272 union mio_emm_rsp_sts rsp_sts;
1273 union mio_emm_sts_mask emm_sts_mask;
1276 bool timed_out = false;
1277 bool multi_xfer = (blkcnt > 1) &&
1278 ((IS_SD(mmc) && mmc->scr[0] & 2) || !IS_SD(mmc));
1280 octeontx_mmc_switch_to(mmc);
1282 emm_sts_mask.s.sts_msk = R1_BLOCK_WRITE_MASK;
1283 write_csr(mmc, MIO_EMM_STS_MASK(), emm_sts_mask.u);
1285 if (octeontx_mmc_poll_ready(mmc, 10000)) {
1286 pr_err("%s(%s): Ready timed out\n", __func__, mmc->dev->name);
1289 flush_dcache_range((u64)data->src,
1290 (u64)data->src + blkcnt * mmc->write_bl_len);
1291 dma_addr = (u64)dm_pci_virt_to_mem(host->dev, (void *)data->src);
1293 timeout = 5000 + 100 * blkcnt;
1294 octeontx_mmc_start_dma(mmc, true, false, start, dma_addr,
1296 timed_out = !!octeontx_mmc_wait_dma(mmc, true, timeout, true);
1297 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1298 if (timed_out || rsp_sts.s.dma_val || rsp_sts.s.dma_pend) {
1299 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",
1300 __func__, mmc->dev->name, timeout,
1302 read_csr(mmc, MIO_EMM_INT()),
1303 read_csr(mmc, MIO_EMM_DMA_INT()),
1304 read_csr(mmc, MIO_EMM_RSP_LO()),
1305 read_csr(mmc, MIO_EMM_DMA()));
1312 octeontx_mmc_start_dma(mmc, true, false, start,
1313 dma_addr, 1, timeout);
1314 dma_addr += mmc->read_bl_len;
1317 timed_out = !!octeontx_mmc_wait_dma(mmc, true, timeout,
1319 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1320 if (timed_out || rsp_sts.s.dma_val ||
1321 rsp_sts.s.dma_pend) {
1322 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",
1323 __func__, mmc->dev->name, timeout,
1325 read_csr(mmc, MIO_EMM_RSP_STS()),
1326 read_csr(mmc, MIO_EMM_DMA_INT()),
1327 read_csr(mmc, MIO_EMM_RSP_LO()),
1328 read_csr(mmc, MIO_EMM_DMA()));
1329 return blkcnt - count;
1339 * Send a command to the eMMC/SD device
1341 * @param mmc mmc device
1342 * @param cmd cmd to send and response
1343 * @param data additional data
1345 * Return: 0 for success, otherwise error
1347 static int octeontx_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1348 struct mmc_data *data)
1350 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
1351 const char *name = slot->dev->name;
1352 struct octeontx_mmc_cr_mods mods = {0, 0};
1353 union mio_emm_rsp_sts rsp_sts;
1354 union mio_emm_cmd emm_cmd;
1355 union mio_emm_rsp_lo rsp_lo;
1356 union mio_emm_buf_idx emm_buf_idx;
1357 union mio_emm_buf_dat emm_buf_dat;
1363 * This constant has a 1 bit for each command which should have a short
1364 * timeout and a 0 for each bit with a long timeout. Currently the
1365 * following commands have a long timeout:
1366 * CMD6, CMD17, CMD18, CMD24, CMD25, CMD32, CMD33, CMD35, CMD36 and
1369 static const u64 timeout_short = 0xFFFFFFA4FCF9FFDFull;
1372 if (cmd->cmdidx == MMC_CMD_SEND_EXT_CSD) {
1373 union mio_emm_rca emm_rca;
1376 emm_rca.s.card_rca = mmc->rca;
1377 write_csr(mmc, MIO_EMM_RCA(), emm_rca.u);
1380 if (timeout_short & (1ull << cmd->cmdidx))
1381 timeout = MMC_TIMEOUT_SHORT;
1382 else if (cmd->cmdidx == MMC_CMD_SWITCH && IS_SD(mmc))
1384 else if (cmd->cmdidx == MMC_CMD_ERASE)
1385 timeout = MMC_TIMEOUT_ERASE;
1387 timeout = MMC_TIMEOUT_LONG;
1389 debug("%s(%s): cmd idx: %u, arg: 0x%x, resp type: 0x%x, timeout: %u\n",
1390 __func__, name, cmd->cmdidx, cmd->cmdarg, cmd->resp_type,
1393 debug(" data: addr: %p, flags: 0x%x, blocks: %u, blocksize: %u\n",
1394 data->dest, data->flags, data->blocks, data->blocksize);
1396 octeontx_mmc_switch_to(mmc);
1398 /* Clear any interrupts */
1399 write_csr(mmc, MIO_EMM_INT(), read_csr(mmc, MIO_EMM_INT()));
1402 * We need to override the default command types and response types
1403 * when dealing with SD cards.
1405 mods = octeontx_mmc_get_cr_mods(mmc, cmd, data);
1407 /* Handle block read/write/stop operations */
1408 switch (cmd->cmdidx) {
1409 case MMC_CMD_GO_IDLE_STATE:
1410 slot->tuned = false;
1411 slot->hs200_tuned = false;
1412 slot->hs400_tuned = false;
1414 case MMC_CMD_STOP_TRANSMISSION:
1416 case MMC_CMD_READ_MULTIPLE_BLOCK:
1417 case MMC_CMD_READ_SINGLE_BLOCK:
1418 pr_debug("%s(%s): Reading blocks\n", __func__, name);
1419 blkcnt = octeontx_mmc_read_blocks(mmc, cmd, data, true);
1420 return (blkcnt > 0) ? 0 : -1;
1421 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
1422 case MMC_CMD_WRITE_SINGLE_BLOCK:
1423 blkcnt = octeontx_mmc_write_blocks(mmc, cmd, data);
1424 return (blkcnt > 0) ? 0 : -1;
1425 case MMC_CMD_SELECT_CARD:
1426 /* Set the RCA register (is it set automatically?) */
1428 union mio_emm_rca emm_rca;
1431 emm_rca.s.card_rca = (cmd->cmdarg >> 16);
1432 write_csr(mmc, MIO_EMM_RCA(), emm_rca.u);
1433 debug("%s: Set SD relative address (RCA) to 0x%x\n",
1434 __func__, emm_rca.s.card_rca);
1438 case MMC_CMD_SWITCH:
1439 if (!data && !slot->is_acmd)
1440 octeontx_mmc_track_switch(mmc, cmd->cmdarg);
1445 emm_cmd.s.cmd_val = 1;
1446 emm_cmd.s.bus_id = slot->bus_id;
1447 emm_cmd.s.cmd_idx = cmd->cmdidx;
1448 emm_cmd.s.arg = cmd->cmdarg;
1449 emm_cmd.s.ctype_xor = mods.ctype_xor;
1450 emm_cmd.s.rtype_xor = mods.rtype_xor;
1451 if (data && data->blocks == 1 && data->blocksize != 512) {
1453 64 - ((data->blocks * data->blocksize) / 8);
1454 debug("%s: offset set to %u\n", __func__, emm_cmd.s.offset);
1457 if (data && data->flags & MMC_DATA_WRITE) {
1458 u8 *src = (u8 *)data->src;
1461 pr_err("%s(%s): Error: data source for cmd 0x%x is NULL!\n",
1462 __func__, name, cmd->cmdidx);
1465 if (data->blocksize > 512) {
1466 pr_err("%s(%s): Error: data for cmd 0x%x exceeds 512 bytes\n",
1467 __func__, name, cmd->cmdidx);
1471 debug("%s: Sending %d bytes data\n", __func__, data->blocksize);
1472 print_buffer(0, src, 1, data->blocksize, 0);
1475 emm_buf_idx.s.inc = 1;
1476 write_csr(mmc, MIO_EMM_BUF_IDX(), emm_buf_idx.u);
1477 for (i = 0; i < (data->blocksize + 7) / 8; i++) {
1478 memcpy(&emm_buf_dat.u, src, sizeof(emm_buf_dat.u));
1479 write_csr(mmc, MIO_EMM_BUF_DAT(),
1480 cpu_to_be64(emm_buf_dat.u));
1481 src += sizeof(emm_buf_dat.u);
1483 write_csr(mmc, MIO_EMM_BUF_IDX(), 0);
1485 debug("%s(%s): Sending command %u (emm_cmd: 0x%llx)\n", __func__,
1486 name, cmd->cmdidx, emm_cmd.u);
1487 set_wdog(mmc, timeout * 1000);
1488 write_csr(mmc, MIO_EMM_CMD(), emm_cmd.u);
1490 /* Wait for command to finish or time out */
1491 start = get_timer(0);
1493 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1495 } while (!rsp_sts.s.cmd_done && !rsp_sts.s.rsp_timeout &&
1496 (get_timer(start) < timeout + 10));
1497 octeontx_mmc_print_rsp_errors(mmc, rsp_sts);
1498 if (rsp_sts.s.rsp_timeout || !rsp_sts.s.cmd_done) {
1499 debug("%s(%s): Error: command %u(0x%x) timed out. rsp_sts: 0x%llx\n",
1500 __func__, name, cmd->cmdidx, cmd->cmdarg, rsp_sts.u);
1501 octeontx_mmc_print_registers(mmc);
1504 if (rsp_sts.s.rsp_crc_err) {
1505 debug("%s(%s): RSP CRC error, rsp_sts: 0x%llx, cmdidx: %u, arg: 0x%08x\n",
1506 __func__, name, rsp_sts.u, cmd->cmdidx, cmd->cmdarg);
1507 octeontx_mmc_print_registers(mmc);
1510 if (slot->bus_id != rsp_sts.s.bus_id) {
1511 pr_warn("%s(%s): bus id mismatch, got %d, expected %d for command 0x%x(0x%x)\n",
1513 rsp_sts.s.bus_id, slot->bus_id,
1514 cmd->cmdidx, cmd->cmdarg);
1517 if (rsp_sts.s.rsp_bad_sts) {
1518 rsp_lo.u = read_csr(mmc, MIO_EMM_RSP_LO());
1519 debug("%s: Bad response for bus id %d, cmd id %d:\n"
1520 " rsp_timeout: %d\n"
1521 " rsp_bad_sts: %d\n"
1522 " rsp_crc_err: %d\n",
1523 __func__, slot->bus_id, cmd->cmdidx,
1524 rsp_sts.s.rsp_timeout,
1525 rsp_sts.s.rsp_bad_sts,
1526 rsp_sts.s.rsp_crc_err);
1527 if (rsp_sts.s.rsp_type == 1 && rsp_sts.s.rsp_bad_sts) {
1528 debug(" Response status: 0x%llx\n",
1529 (rsp_lo.u >> 8) & 0xffffffff);
1531 mmc_print_status((rsp_lo.u >> 8) & 0xffffffff);
1536 if (rsp_sts.s.cmd_idx != cmd->cmdidx) {
1537 debug("%s(%s): Command response index %d does not match command index %d\n",
1538 __func__, name, rsp_sts.s.cmd_idx, cmd->cmdidx);
1539 octeontx_print_rsp_sts(mmc);
1540 debug("%s: rsp_lo: 0x%llx\n", __func__,
1541 read_csr(mmc, MIO_EMM_RSP_LO()));
1546 slot->is_acmd = (cmd->cmdidx == MMC_CMD_APP_CMD);
1548 if (!cmd->resp_type & MMC_RSP_PRESENT)
1549 debug(" Response type: 0x%x, no response expected\n",
1551 /* Get the response if present */
1552 if (rsp_sts.s.rsp_val && (cmd->resp_type & MMC_RSP_PRESENT)) {
1553 union mio_emm_rsp_hi rsp_hi;
1555 rsp_lo.u = read_csr(mmc, MIO_EMM_RSP_LO());
1557 switch (rsp_sts.s.rsp_type) {
1562 cmd->response[0] = (rsp_lo.u >> 8) & 0xffffffffull;
1563 debug(" response: 0x%08x\n",
1565 cmd->response[1] = 0;
1566 cmd->response[2] = 0;
1567 cmd->response[3] = 0;
1570 cmd->response[3] = rsp_lo.u & 0xffffffff;
1571 cmd->response[2] = (rsp_lo.u >> 32) & 0xffffffff;
1572 rsp_hi.u = read_csr(mmc, MIO_EMM_RSP_HI());
1573 cmd->response[1] = rsp_hi.u & 0xffffffff;
1574 cmd->response[0] = (rsp_hi.u >> 32) & 0xffffffff;
1575 debug(" response: 0x%08x 0x%08x 0x%08x 0x%08x\n",
1576 cmd->response[0], cmd->response[1],
1577 cmd->response[2], cmd->response[3]);
1580 pr_err("%s(%s): Unknown response type 0x%x for command %d, arg: 0x%x, rsp_sts: 0x%llx\n",
1581 __func__, name, rsp_sts.s.rsp_type, cmd->cmdidx,
1582 cmd->cmdarg, rsp_sts.u);
1586 debug(" Response not expected\n");
1589 if (data && data->flags & MMC_DATA_READ) {
1590 u8 *dest = (u8 *)data->dest;
1593 pr_err("%s(%s): Error, destination buffer NULL!\n",
1594 __func__, mmc->dev->name);
1597 if (data->blocksize > 512) {
1598 printf("%s(%s): Error: data size %u exceeds 512\n",
1599 __func__, mmc->dev->name,
1604 emm_buf_idx.s.inc = 1;
1605 write_csr(mmc, MIO_EMM_BUF_IDX(), emm_buf_idx.u);
1606 for (i = 0; i < (data->blocksize + 7) / 8; i++) {
1607 emm_buf_dat.u = read_csr(mmc, MIO_EMM_BUF_DAT());
1608 emm_buf_dat.u = be64_to_cpu(emm_buf_dat.u);
1609 memcpy(dest, &emm_buf_dat.u, sizeof(emm_buf_dat.u));
1610 dest += sizeof(emm_buf_dat.u);
1612 write_csr(mmc, MIO_EMM_BUF_IDX(), 0);
1614 debug("%s: Received %d bytes data\n", __func__,
1616 print_buffer(0, data->dest, 1, data->blocksize, 0);
1623 octeontx_mmc_print_registers(mmc);
1628 static int octeontx_mmc_dev_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1629 struct mmc_data *data)
1631 return octeontx_mmc_send_cmd(dev_to_mmc(dev), cmd, data);
1634 #ifdef MMC_SUPPORTS_TUNING
1635 static int octeontx_mmc_test_cmd(struct mmc *mmc, u32 opcode, int *statp)
1640 memset(&cmd, 0, sizeof(cmd));
1642 debug("%s(%s, %u, %p)\n", __func__, mmc->dev->name, opcode, statp);
1643 cmd.cmdidx = opcode;
1644 cmd.resp_type = MMC_RSP_R1;
1645 cmd.cmdarg = mmc->rca << 16;
1647 err = octeontx_mmc_send_cmd(mmc, &cmd, NULL);
1649 debug("%s(%s, %u) returned %d\n", __func__,
1650 mmc->dev->name, opcode, err);
1652 *statp = cmd.response[0];
1656 static int octeontx_mmc_test_get_ext_csd(struct mmc *mmc, u32 opcode,
1660 struct mmc_data data;
1662 u8 ext_csd[MMC_MAX_BLOCK_LEN];
1664 debug("%s(%s, %u, %p)\n", __func__, mmc->dev->name, opcode, statp);
1665 memset(&cmd, 0, sizeof(cmd));
1667 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
1668 cmd.resp_type = MMC_RSP_R1;
1671 data.dest = (char *)ext_csd;
1673 data.blocksize = MMC_MAX_BLOCK_LEN;
1674 data.flags = MMC_DATA_READ;
1676 err = octeontx_mmc_send_cmd(mmc, &cmd, &data);
1678 *statp = cmd.response[0];
1684 * Wrapper to set the MIO_EMM_TIMING register
1686 * @param mmc pointer to mmc data structure
1687 * @param emm_timing New emm_timing register value
1689 * On some devices it is possible that changing the data out value can
1690 * cause a glitch on an internal fifo. This works around this problem
1691 * by performing a soft-reset immediately before setting the timing register.
1693 * Note: this function should not be called from any function that
1694 * performs DMA or block operations since not all registers are
1697 static void octeontx_mmc_set_emm_timing(struct mmc *mmc,
1698 union mio_emm_timing emm_timing)
1700 union mio_emm_cfg emm_cfg;
1701 struct octeontx_mmc_slot *slot = mmc->priv;
1702 union mio_emm_debug emm_debug;
1704 debug("%s(%s, 0x%llx) din: %u\n", __func__, mmc->dev->name,
1705 emm_timing.u, emm_timing.s.data_in_tap);
1708 if (slot->host->tap_requires_noclk) {
1709 /* Turn off the clock */
1710 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
1711 emm_debug.s.emmc_clk_disable = 1;
1712 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
1714 emm_debug.s.rdsync_rst = 1;
1715 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
1717 emm_cfg.u = read_csr(mmc, MIO_EMM_CFG());
1718 emm_cfg.s.bus_ena = 1 << 3;
1719 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
1722 write_csr(mmc, MIO_EMM_TIMING(), emm_timing.u);
1725 if (slot->host->tap_requires_noclk) {
1726 /* Turn on the clock */
1727 emm_debug.s.rdsync_rst = 0;
1728 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
1730 emm_debug.s.emmc_clk_disable = 0;
1731 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
1734 emm_cfg.s.bus_ena = 1 << mmc_to_slot(mmc)->bus_id;
1735 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
1738 static const u8 octeontx_hs400_tuning_block[512] = {
1739 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
1740 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
1741 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
1742 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
1743 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
1744 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
1745 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
1746 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
1747 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
1748 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
1749 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
1750 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
1751 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
1752 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
1753 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
1754 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
1755 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
1756 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
1757 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
1758 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
1759 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
1760 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
1761 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
1762 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
1763 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
1764 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
1765 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
1766 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
1767 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
1768 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
1769 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
1770 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
1771 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
1772 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
1773 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
1774 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
1775 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
1776 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
1777 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
1778 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
1779 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
1780 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
1781 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
1782 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
1783 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
1784 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
1785 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
1786 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
1787 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00,
1788 0x00, 0xff, 0x00, 0xff, 0x55, 0xaa, 0x55, 0xaa,
1789 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
1790 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
1791 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
1792 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
1793 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
1794 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
1795 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00,
1796 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
1797 0x01, 0xfe, 0x01, 0xfe, 0xcc, 0xcc, 0xcc, 0xff,
1798 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
1799 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
1800 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
1801 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
1802 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
1807 * Perform tuning in HS400 mode
1809 * @param[in] mmc mmc data structure
1811 * @ret 0 for success, otherwise error
1813 static int octeontx_tune_hs400(struct mmc *mmc)
1815 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
1817 struct mmc_data data;
1818 union mio_emm_timing emm_timing;
1819 u8 buffer[mmc->read_bl_len];
1826 int best_start = -1;
1827 bool prev_ok = false;
1829 char how[MAX_NO_OF_TAPS + 1] = "";
1831 if (slot->hs400_tuning_block == -1)
1834 /* The eMMC standard disables all tuning support when operating in
1835 * DDR modes like HS400. The problem with this is that there are
1836 * many cases where the HS200 tuning does not work for HS400 mode.
1837 * In order to perform this tuning, while in HS200 a block is written
1838 * to a block specified in the device tree (marvell,hs400-tuning-block)
1839 * which is used for tuning in this function by repeatedly reading
1840 * this block and comparing the data and return code. This function
1841 * chooses the data input tap in the middle of the longest run of
1842 * successful read operations.
1845 emm_timing = slot->hs200_taps;
1846 debug("%s(%s): Start ci: %d, co: %d, di: %d, do: %d\n",
1847 __func__, mmc->dev->name, emm_timing.s.cmd_in_tap,
1848 emm_timing.s.cmd_out_tap, emm_timing.s.data_in_tap,
1849 emm_timing.s.data_out_tap);
1850 memset(buffer, 0xdb, sizeof(buffer));
1852 snprintf(env_name, sizeof(env_name), "emmc%d_data_in_tap_hs400",
1854 tap = env_get_ulong(env_name, 10, -1L);
1855 if (tap >= 0 && tap < MAX_NO_OF_TAPS) {
1856 printf("Overriding data input tap for HS400 mode to %d\n", tap);
1857 emm_timing.s.data_in_tap = tap;
1858 octeontx_mmc_set_emm_timing(mmc, emm_timing);
1862 for (tap = 0; tap <= MAX_NO_OF_TAPS; tap++, prev_ok = !err) {
1863 if (tap < MAX_NO_OF_TAPS) {
1864 debug("%s: Testing data in tap %d\n", __func__, tap);
1865 emm_timing.s.data_in_tap = tap;
1866 octeontx_mmc_set_emm_timing(mmc, emm_timing);
1868 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
1869 cmd.cmdarg = slot->hs400_tuning_block;
1870 cmd.resp_type = MMC_RSP_R1;
1871 data.dest = (void *)buffer;
1873 data.blocksize = mmc->read_bl_len;
1874 data.flags = MMC_DATA_READ;
1875 err = !octeontx_mmc_read_blocks(mmc, &cmd, &data,
1877 if (err || memcmp(buffer, octeontx_hs400_tuning_block,
1881 debug("%s: data mismatch. Read:\n",
1883 print_buffer(0, buffer, 1,
1885 debug("\nExpected:\n");
1887 octeontx_hs400_tuning_block, 1,
1888 sizeof(octeontx_hs400_tuning_block),
1891 debug("%s: Error %d reading block\n",
1897 debug("%s: tap %d good\n", __func__, tap);
1899 how[tap] = "-+"[!err];
1907 } else if (prev_ok) {
1908 run = tap - 1 - start_run;
1909 if (start_run >= 0 && run > best_run) {
1910 best_start = start_run;
1916 how[tap - 1] = '\0';
1917 if (best_start < 0) {
1918 printf("%s(%s): %lldMHz tuning failed for HS400\n",
1919 __func__, mmc->dev->name, slot->clock / 1000000);
1922 tap = best_start + best_run / 2;
1924 snprintf(env_name, sizeof(env_name), "emmc%d_data_in_tap_adj_hs400",
1926 tap_adj = env_get_ulong(env_name, 10, slot->hs400_tap_adj);
1928 * Keep it in range and if out of range force it back in with a small
1932 tap = tap + tap_adj;
1933 if (tap >= best_start + best_run)
1934 tap = best_start + best_run - 2;
1935 if (tap <= best_start)
1936 tap = best_start + 2;
1939 debug("Tuning: %s\n", how);
1940 debug("%s(%s): HS400 tap: best run start: %d, length: %d, tap: %d\n",
1941 __func__, mmc->dev->name, best_start, best_run, tap);
1942 slot->hs400_taps = slot->hs200_taps;
1943 slot->hs400_taps.s.data_in_tap = tap;
1944 slot->hs400_tuned = true;
1945 if (env_get_yesno("emmc_export_hs400_taps") > 0) {
1946 debug("%s(%s): Exporting HS400 taps\n",
1947 __func__, mmc->dev->name);
1948 env_set_ulong("emmc_timing_tap", slot->host->timing_taps);
1949 snprintf(env_name, sizeof(env_name),
1950 "emmc%d_hs400_data_in_tap_debug",
1952 env_set(env_name, how);
1953 snprintf(env_name, sizeof(env_name),
1954 "emmc%d_hs400_data_in_tap_val",
1956 env_set_ulong(env_name, tap);
1957 snprintf(env_name, sizeof(env_name),
1958 "emmc%d_hs400_data_in_tap_start",
1960 env_set_ulong(env_name, best_start);
1961 snprintf(env_name, sizeof(env_name),
1962 "emmc%d_hs400_data_in_tap_end",
1964 env_set_ulong(env_name, best_start + best_run);
1965 snprintf(env_name, sizeof(env_name),
1966 "emmc%d_hs400_cmd_in_tap",
1968 env_set_ulong(env_name, slot->hs400_taps.s.cmd_in_tap);
1969 snprintf(env_name, sizeof(env_name),
1970 "emmc%d_hs400_cmd_out_tap",
1972 env_set_ulong(env_name, slot->hs400_taps.s.cmd_out_tap);
1973 snprintf(env_name, sizeof(env_name),
1974 "emmc%d_hs400_cmd_out_delay",
1976 env_set_ulong(env_name, slot->cmd_out_hs400_delay);
1977 snprintf(env_name, sizeof(env_name),
1978 "emmc%d_hs400_data_out_tap",
1980 env_set_ulong(env_name, slot->hs400_taps.s.data_out_tap);
1981 snprintf(env_name, sizeof(env_name),
1982 "emmc%d_hs400_data_out_delay",
1984 env_set_ulong(env_name, slot->data_out_hs400_delay);
1986 debug("%s(%s): HS400 environment export disabled\n",
1987 __func__, mmc->dev->name);
1989 octeontx_mmc_set_timing(mmc);
1997 int (*test)(struct mmc *mmc, u32 opcode, int *error);
2001 bool not_hs200_only;
2005 struct adj adj[] = {
2006 { "CMD_IN", 48, octeontx_mmc_test_cmd, MMC_CMD_SEND_STATUS,
2007 false, false, false, 2, },
2008 /* { "CMD_OUT", 32, octeontx_mmc_test_cmd, MMC_CMD_SEND_STATUS, },*/
2009 { "DATA_IN(HS200)", 16, mmc_send_tuning,
2010 MMC_CMD_SEND_TUNING_BLOCK_HS200, false, true, false, 2, },
2011 { "DATA_IN", 16, octeontx_mmc_test_get_ext_csd, 0, false, false,
2013 /* { "DATA_OUT", 0, octeontx_mmc_test_cmd, 0, true, false},*/
2018 * Perform tuning tests to find optimal timing
2020 * @param mmc mmc device
2021 * @param adj parameter to tune
2022 * @param opcode command opcode to use
2024 * Return: 0 for success, -1 if tuning failed
2026 static int octeontx_mmc_adjust_tuning(struct mmc *mmc, struct adj *adj,
2029 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2030 union mio_emm_timing timing;
2031 union mio_emm_debug emm_debug;
2038 int best_start = -1;
2039 bool prev_ok = false;
2041 const int tap_adj = slot->hs200_tap_adj;
2042 char how[MAX_NO_OF_TAPS + 1] = "";
2043 bool is_hs200 = mmc->selected_mode == MMC_HS_200;
2045 debug("%s(%s, %s, %d), hs200: %d\n", __func__, mmc->dev->name,
2046 adj->name, opcode, is_hs200);
2047 octeontx_mmc_set_emm_timing(mmc,
2048 is_hs200 ? slot->hs200_taps : slot->taps);
2051 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
2052 printf("%s(%s): Before tuning %s, opcode: %d\n",
2053 __func__, mmc->dev->name, adj->name, opcode);
2054 octeontx_mmc_print_registers2(mmc, NULL);
2059 * The algorithm to find the optimal timing is to start
2060 * at the end and work backwards and select the second
2061 * value that passes. Each test is repeated twice.
2063 for (tap = 0; tap <= MAX_NO_OF_TAPS; tap++, prev_ok = !err) {
2064 if (tap < MAX_NO_OF_TAPS) {
2065 if (slot->host->tap_requires_noclk) {
2066 /* Turn off the clock */
2067 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
2068 emm_debug.s.emmc_clk_disable = 1;
2069 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2071 emm_debug.s.rdsync_rst = 1;
2072 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2076 timing.u = read_csr(mmc, MIO_EMM_TIMING());
2077 timing.u &= ~(0x3full << adj->mask_shift);
2078 timing.u |= (u64)tap << adj->mask_shift;
2079 write_csr(mmc, MIO_EMM_TIMING(), timing.u);
2080 debug("%s(%s): Testing ci: %d, co: %d, di: %d, do: %d\n",
2081 __func__, mmc->dev->name, timing.s.cmd_in_tap,
2082 timing.s.cmd_out_tap, timing.s.data_in_tap,
2083 timing.s.data_out_tap);
2085 if (slot->host->tap_requires_noclk) {
2086 /* Turn off the clock */
2087 emm_debug.s.rdsync_rst = 0;
2088 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2090 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
2091 emm_debug.s.emmc_clk_disable = 0;
2092 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2095 for (count = 0; count < 2; count++) {
2096 err = adj->test(mmc, opcode, NULL);
2098 debug("%s(%s, %s): tap %d failed, count: %d, rsp_sts: 0x%llx, rsp_lo: 0x%llx\n",
2099 __func__, mmc->dev->name,
2100 adj->name, tap, count,
2105 debug("%s(%s, %s): tap: %d, do: %d, di: %d, co: %d, ci: %d\n",
2106 __func__, mmc->dev->name,
2108 timing.s.data_out_tap,
2109 timing.s.data_in_tap,
2110 timing.s.cmd_out_tap,
2111 timing.s.cmd_in_tap);
2114 debug("%s(%s, %s): tap %d passed, count: %d, rsp_sts: 0x%llx, rsp_lo: 0x%llx\n",
2115 __func__, mmc->dev->name, adj->name, tap,
2117 read_csr(mmc, MIO_EMM_RSP_STS()),
2118 read_csr(mmc, MIO_EMM_RSP_LO()));
2120 tap_status |= (u64)(!err) << tap;
2121 how[tap] = "-+"[!err];
2124 * Putting the end+1 case in the loop simplifies
2125 * logic, allowing 'prev_ok' to process a sweet
2126 * spot in tuning which extends to the wall.
2132 * If no CRC/etc errors in the response, but previous
2133 * failed, note the start of a new run.
2135 debug(" prev_ok: %d\n", prev_ok);
2138 } else if (prev_ok) {
2139 run = tap - 1 - start_run;
2140 /* did we just exit a wider sweet spot? */
2141 if (start_run >= 0 && run > best_run) {
2142 best_start = start_run;
2147 how[tap - 1] = '\0';
2148 if (best_start < 0) {
2149 printf("%s(%s, %s): %lldMHz tuning %s failed\n", __func__,
2150 mmc->dev->name, adj->name, slot->clock / 1000000,
2155 tap = best_start + best_run / 2;
2156 debug(" tap %d is center, start: %d, run: %d\n", tap,
2157 best_start, best_run);
2159 slot->hs200_taps.u &= ~(0x3full << adj->mask_shift);
2160 slot->hs200_taps.u |= (u64)tap << adj->mask_shift;
2162 slot->taps.u &= ~(0x3full << adj->mask_shift);
2163 slot->taps.u |= (u64)tap << adj->mask_shift;
2165 if (best_start < 0) {
2166 printf("%s(%s, %s): %lldMHz tuning %s failed\n", __func__,
2167 mmc->dev->name, adj->name, slot->clock / 1000000,
2172 tap = best_start + best_run / 2;
2173 if (is_hs200 && (tap + tap_adj >= 0) && (tap + tap_adj < 64) &&
2174 tap_status & (1ULL << (tap + tap_adj))) {
2175 debug("Adjusting tap from %d by %d to %d\n",
2176 tap, tap_adj, tap + tap_adj);
2180 debug("%s/%s %d/%d/%d %s\n", mmc->dev->name,
2181 adj->name, best_start, tap, best_start + best_run, how);
2184 slot->hs200_taps.u &= ~(0x3full << adj->mask_shift);
2185 slot->hs200_taps.u |= (u64)tap << adj->mask_shift;
2187 slot->taps.u &= ~(0x3full << adj->mask_shift);
2188 slot->taps.u |= (u64)tap << adj->mask_shift;
2192 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
2193 debug("%s(%s, %s): After successful tuning\n",
2194 __func__, mmc->dev->name, adj->name);
2195 debug("%s(%s, %s): tap: %d, new do: %d, di: %d, co: %d, ci: %d\n",
2196 __func__, mmc->dev->name, adj->name, tap,
2197 slot->taps.s.data_out_tap,
2198 slot->taps.s.data_in_tap,
2199 slot->taps.s.cmd_out_tap,
2200 slot->taps.s.cmd_in_tap);
2201 debug("%s(%s, %s): tap: %d, new do HS200: %d, di: %d, co: %d, ci: %d\n",
2202 __func__, mmc->dev->name, adj->name, tap,
2203 slot->hs200_taps.s.data_out_tap,
2204 slot->hs200_taps.s.data_in_tap,
2205 slot->hs200_taps.s.cmd_out_tap,
2206 slot->hs200_taps.s.cmd_in_tap);
2209 octeontx_mmc_set_timing(mmc);
2211 if (is_hs200 && env_get_yesno("emmc_export_hs200_taps")) {
2214 env_set_ulong("emmc_timing_tap", slot->host->timing_taps);
2216 case MMC_CMD_SEND_TUNING_BLOCK:
2217 snprintf(env_name, sizeof(env_name),
2218 "emmc%d_hs200_data_in_tap_debug",
2220 env_set(env_name, how);
2221 snprintf(env_name, sizeof(env_name),
2222 "emmc%d_hs200_data_in_tap_val", slot->bus_id);
2223 env_set_ulong(env_name, tap);
2224 snprintf(env_name, sizeof(env_name),
2225 "emmc%d_hs200_data_in_tap_start",
2227 env_set_ulong(env_name, best_start);
2228 snprintf(env_name, sizeof(env_name),
2229 "emmc%d_hs200_data_in_tap_end",
2231 env_set_ulong(env_name, best_start + best_run);
2233 case MMC_CMD_SEND_STATUS:
2234 snprintf(env_name, sizeof(env_name),
2235 "emmc%d_hs200_cmd_in_tap_debug",
2237 env_set(env_name, how);
2238 snprintf(env_name, sizeof(env_name),
2239 "emmc%d_hs200_cmd_in_tap_val", slot->bus_id);
2240 env_set_ulong(env_name, tap);
2241 snprintf(env_name, sizeof(env_name),
2242 "emmc%d_hs200_cmd_in_tap_start",
2244 env_set_ulong(env_name, best_start);
2245 snprintf(env_name, sizeof(env_name),
2246 "emmc%d_hs200_cmd_in_tap_end",
2248 env_set_ulong(env_name, best_start + best_run);
2251 snprintf(env_name, sizeof(env_name),
2252 "emmc%d_hs200_data_out_tap", slot->bus_id);
2253 env_set_ulong(env_name, slot->data_out_hs200_delay);
2254 snprintf(env_name, sizeof(env_name),
2255 "emmc%d_hs200_cmd_out_tap", slot->bus_id);
2256 env_set_ulong(env_name, slot->cmd_out_hs200_delay);
2264 static int octeontx_mmc_execute_tuning(struct udevice *dev, u32 opcode)
2266 struct mmc *mmc = dev_to_mmc(dev);
2267 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2268 union mio_emm_timing emm_timing;
2274 pr_info("%s re-tuning, opcode 0x%x\n", dev->name, opcode);
2276 if (slot->is_asim || slot->is_emul)
2279 is_hs200 = (mmc->selected_mode == MMC_HS_200);
2281 slot->hs200_tuned = false;
2282 slot->hs400_tuned = false;
2284 slot->tuned = false;
2286 octeontx_mmc_set_output_bus_timing(mmc);
2287 octeontx_mmc_set_input_bus_timing(mmc);
2288 emm_timing.u = read_csr(mmc, MIO_EMM_TIMING());
2289 if (mmc->selected_mode == MMC_HS_200) {
2290 slot->hs200_taps.s.cmd_out_tap = emm_timing.s.cmd_out_tap;
2291 slot->hs200_taps.s.data_out_tap = emm_timing.s.data_out_tap;
2293 slot->taps.s.cmd_out_tap = emm_timing.s.cmd_out_tap;
2294 slot->taps.s.data_out_tap = emm_timing.s.data_out_tap;
2296 octeontx_mmc_set_input_bus_timing(mmc);
2297 octeontx_mmc_set_output_bus_timing(mmc);
2299 for (a = adj; a->name; a++) {
2302 if (!strcmp(a->name, "CMD_IN")) {
2303 snprintf(env_name, sizeof(env_name),
2304 "emmc%d_cmd_in_tap", slot->bus_id);
2305 in_tap = env_get_ulong(env_name, 10, (ulong)-1);
2306 if (in_tap != (ulong)-1) {
2307 if (mmc->selected_mode == MMC_HS_200 ||
2309 slot->hs200_taps.s.cmd_in_tap = in_tap;
2310 slot->hs400_taps.s.cmd_in_tap = in_tap;
2312 slot->taps.s.cmd_in_tap = in_tap;
2316 } else if (a->hs200_only &&
2317 !strcmp(a->name, "DATA_IN(HS200)")) {
2318 snprintf(env_name, sizeof(env_name),
2319 "emmc%d_data_in_tap_hs200", slot->bus_id);
2320 in_tap = env_get_ulong(env_name, 10, (ulong)-1);
2321 if (in_tap != (ulong)-1) {
2322 debug("%s(%s): Overriding HS200 data in tap to %d\n",
2323 __func__, dev->name, (int)in_tap);
2324 slot->hs200_taps.s.data_in_tap = in_tap;
2327 } else if (!a->hs200_only && !strcmp(a->name, "DATA_IN")) {
2328 snprintf(env_name, sizeof(env_name),
2329 "emmc%d_data_in_tap", slot->bus_id);
2330 in_tap = env_get_ulong(env_name, 10, (ulong)-1);
2331 if (in_tap != (ulong)-1) {
2332 debug("%s(%s): Overriding non-HS200 data in tap to %d\n",
2333 __func__, dev->name, (int)in_tap);
2334 slot->taps.s.data_in_tap = in_tap;
2339 debug("%s(%s): Testing: %s, mode: %s, opcode: %u\n", __func__,
2340 dev->name, a->name, mmc_mode_name(mmc->selected_mode),
2343 /* Skip DDR only test when not in DDR mode */
2344 if (a->ddr_only && !mmc->ddr_mode) {
2345 debug("%s(%s): Skipping %s due to non-DDR mode\n",
2346 __func__, dev->name, a->name);
2349 /* Skip hs200 tests in non-hs200 mode and
2350 * non-hs200 tests in hs200 mode
2353 if (a->not_hs200_only) {
2354 debug("%s(%s): Skipping %s\n", __func__,
2355 dev->name, a->name);
2359 if (a->hs200_only) {
2360 debug("%s(%s): Skipping %s\n", __func__,
2361 dev->name, a->name);
2366 err = octeontx_mmc_adjust_tuning(mmc, a, a->opcode ?
2367 a->opcode : opcode);
2369 pr_err("%s(%s, %u): tuning %s failed\n", __func__,
2370 dev->name, opcode, a->name);
2375 octeontx_mmc_set_timing(mmc);
2377 slot->hs200_tuned = true;
2381 if (slot->hs400_tuning_block != -1) {
2383 struct mmc_data data;
2384 u8 buffer[mmc->read_bl_len];
2386 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
2387 cmd.cmdarg = slot->hs400_tuning_block;
2388 cmd.resp_type = MMC_RSP_R1;
2389 data.dest = (void *)buffer;
2391 data.blocksize = mmc->read_bl_len;
2392 data.flags = MMC_DATA_READ;
2393 err = octeontx_mmc_read_blocks(mmc, &cmd, &data, true) != 1;
2396 printf("%s: Cannot read HS400 tuning block %u\n",
2397 dev->name, slot->hs400_tuning_block);
2400 if (memcmp(buffer, octeontx_hs400_tuning_block,
2402 debug("%s(%s): Writing new HS400 tuning block to block %d\n",
2403 __func__, dev->name, slot->hs400_tuning_block);
2404 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
2405 data.src = (void *)octeontx_hs400_tuning_block;
2406 data.flags = MMC_DATA_WRITE;
2407 err = !octeontx_mmc_write_blocks(mmc, &cmd, &data);
2409 printf("%s: Cannot write HS400 tuning block %u\n",
2410 dev->name, slot->hs400_tuning_block);
2418 #else /* MMC_SUPPORTS_TUNING */
2419 static void octeontx_mmc_set_emm_timing(struct mmc *mmc,
2420 union mio_emm_timing emm_timing)
2423 #endif /* MMC_SUPPORTS_TUNING */
2426 * Calculate the clock period with rounding up
2428 * @param mmc mmc device
2429 * Return: clock period in system clocks for clk_lo + clk_hi
2431 static u32 octeontx_mmc_calc_clk_period(struct mmc *mmc)
2433 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2434 struct octeontx_mmc_host *host = slot->host;
2437 return DIV_ROUND_UP(host->sys_freq, mmc->clock);
2442 static int octeontx_mmc_set_ios(struct udevice *dev)
2444 struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
2445 struct mmc *mmc = &slot->mmc;
2446 struct octeontx_mmc_host *host = slot->host;
2447 union mio_emm_switch emm_switch;
2448 union mio_emm_modex mode;
2452 int power_class = 10;
2454 bool is_hs200 = false;
2455 bool is_hs400 = false;
2457 debug("%s(%s): Entry\n", __func__, dev->name);
2458 debug(" clock: %u, bus width: %u, mode: %u\n", mmc->clock,
2459 mmc->bus_width, mmc->selected_mode);
2460 debug(" host caps: 0x%x, card caps: 0x%x\n", mmc->host_caps,
2462 octeontx_mmc_switch_to(mmc);
2466 clock = mmc->cfg->f_min;
2468 switch (mmc->bus_width) {
2479 pr_warn("%s(%s): Invalid bus width %d, defaulting to 1\n",
2480 __func__, dev->name, mmc->bus_width);
2484 /* DDR is available for 4/8 bit bus width */
2485 if (mmc->ddr_mode && bus_width)
2488 debug("%s: sys_freq: %llu\n", __func__, host->sys_freq);
2489 clk_period = octeontx_mmc_calc_clk_period(mmc);
2492 emm_switch.s.bus_width = bus_width;
2493 emm_switch.s.power_class = power_class;
2494 emm_switch.s.clk_hi = clk_period / 2;
2495 emm_switch.s.clk_lo = clk_period / 2;
2497 debug("%s: last mode: %d, mode: %d, last clock: %u, clock: %u, ddr: %d\n",
2498 __func__, slot->last_mode, mmc->selected_mode,
2499 slot->last_clock, mmc->clock, mmc->ddr_mode);
2500 switch (mmc->selected_mode) {
2506 emm_switch.s.hs_timing = 1;
2515 #if !defined(CONFIG_ARCH_OCTEON)
2516 emm_switch.s.hs200_timing = 1;
2524 #if !defined(CONFIG_ARCH_OCTEON)
2525 emm_switch.s.hs400_timing = 1;
2529 pr_err("%s(%s): Unsupported mode 0x%x\n", __func__, dev->name,
2530 mmc->selected_mode);
2533 emm_switch.s.bus_id = slot->bus_id;
2535 if (!is_hs200 && !is_hs400 &&
2536 (mmc->selected_mode != slot->last_mode ||
2537 mmc->clock != slot->last_clock) &&
2539 slot->tuned = false;
2540 slot->last_mode = mmc->selected_mode;
2541 slot->last_clock = mmc->clock;
2544 if (CONFIG_IS_ENABLED(MMC_VERBOSE)) {
2545 debug("%s(%s): Setting bus mode to %s\n", __func__, dev->name,
2546 mmc_mode_name(mmc->selected_mode));
2548 debug("%s(%s): Setting bus mode to 0x%x\n", __func__, dev->name,
2549 mmc->selected_mode);
2552 #if !defined(CONFIG_ARCH_OCTEON)
2553 debug(" Trying switch 0x%llx w%d hs:%d hs200:%d hs400:%d\n",
2554 emm_switch.u, emm_switch.s.bus_width, emm_switch.s.hs_timing,
2555 emm_switch.s.hs200_timing, emm_switch.s.hs400_timing);
2558 set_wdog(mmc, 1000);
2559 do_switch(mmc, emm_switch);
2561 mode.u = read_csr(mmc, MIO_EMM_MODEX(slot->bus_id));
2562 #if !defined(CONFIG_ARCH_OCTEON)
2563 debug("%s(%s): mode: 0x%llx w:%d, hs:%d, hs200:%d, hs400:%d\n",
2564 __func__, dev->name, mode.u, mode.s.bus_width,
2565 mode.s.hs_timing, mode.s.hs200_timing, mode.s.hs400_timing);
2568 err = octeontx_mmc_configure_delay(mmc);
2570 #ifdef MMC_SUPPORTS_TUNING
2571 if (!err && mmc->selected_mode == MMC_HS_400 && !slot->hs400_tuned) {
2572 debug("%s: Tuning HS400 mode\n", __func__);
2573 err = octeontx_tune_hs400(mmc);
2581 * Gets the status of the card detect pin
2583 static int octeontx_mmc_get_cd(struct udevice *dev)
2585 struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
2588 if (dm_gpio_is_valid(&slot->cd_gpio)) {
2589 val = dm_gpio_get_value(&slot->cd_gpio);
2590 val ^= slot->cd_inverted;
2592 debug("%s(%s): cd: %d\n", __func__, dev->name, val);
2597 * Gets the status of the write protect pin
2599 static int octeontx_mmc_get_wp(struct udevice *dev)
2601 struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
2604 if (dm_gpio_is_valid(&slot->wp_gpio)) {
2605 val = dm_gpio_get_value(&slot->wp_gpio);
2606 val ^= slot->wp_inverted;
2608 debug("%s(%s): wp: %d\n", __func__, dev->name, val);
2612 #if defined(CONFIG_ARCH_OCTEON)
2613 static int octeontx_mmc_configure_delay(struct mmc *mmc)
2615 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2616 union mio_emm_sample emm_sample;
2618 debug("%s(%s)\n", __func__, mmc->dev->name);
2621 emm_sample.s.cmd_cnt = slot->cmd_cnt;
2622 emm_sample.s.dat_cnt = slot->dat_cnt;
2623 write_csr(mmc, MIO_EMM_SAMPLE(), emm_sample.u);
2628 static void octeontx_mmc_io_drive_setup(struct mmc *mmc)
2632 static void octeontx_mmc_set_timing(struct mmc *mmc)
2634 union mio_emm_timing timing;
2635 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2637 switch (mmc->selected_mode) {
2639 timing = slot->hs200_taps;
2642 timing = slot->hs400_tuned ?
2643 slot->hs400_taps : slot->hs200_taps;
2646 timing = slot->taps;
2650 debug("%s(%s):\n cmd_in_tap: %u\n cmd_out_tap: %u\n data_in_tap: %u\n data_out_tap: %u\n",
2651 __func__, mmc->dev->name, timing.s.cmd_in_tap,
2652 timing.s.cmd_out_tap, timing.s.data_in_tap,
2653 timing.s.data_out_tap);
2655 octeontx_mmc_set_emm_timing(mmc, timing);
2658 static int octeontx_mmc_configure_delay(struct mmc *mmc)
2660 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2661 struct octeontx_mmc_host *host __maybe_unused = slot->host;
2662 bool __maybe_unused is_hs200;
2663 bool __maybe_unused is_hs400;
2665 debug("%s(%s)\n", __func__, mmc->dev->name);
2667 if (IS_ENABLED(CONFIG_ARCH_OCTEON) ||
2668 IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
2669 union mio_emm_sample emm_sample;
2672 emm_sample.s.cmd_cnt = slot->cmd_cnt;
2673 emm_sample.s.dat_cnt = slot->dat_cnt;
2674 write_csr(mmc, MIO_EMM_SAMPLE(), emm_sample.u);
2676 is_hs200 = (mmc->selected_mode == MMC_HS_200);
2677 is_hs400 = (mmc->selected_mode == MMC_HS_400);
2679 if ((is_hs200 && slot->hs200_tuned) ||
2680 (is_hs400 && slot->hs400_tuned) ||
2681 (!is_hs200 && !is_hs400 && slot->tuned)) {
2682 octeontx_mmc_set_output_bus_timing(mmc);
2684 int half = MAX_NO_OF_TAPS / 2;
2687 switch (mmc->selected_mode) {
2690 cout = MMC_SD_LEGACY_DEFAULT_CMD_OUT_TAP;
2691 dout = MMC_SD_LEGACY_DEFAULT_DATA_OUT_TAP;
2693 cout = MMC_LEGACY_DEFAULT_CMD_OUT_TAP;
2694 dout = MMC_LEGACY_DEFAULT_DATA_OUT_TAP;
2698 cout = MMC_HS_CMD_OUT_TAP;
2699 dout = MMC_HS_DATA_OUT_TAP;
2705 cout = MMC_SD_HS_CMD_OUT_TAP;
2706 dout = MMC_SD_HS_DATA_OUT_TAP;
2712 cout = MMC_DEFAULT_CMD_OUT_TAP;
2713 dout = MMC_DEFAULT_DATA_OUT_TAP;
2718 if (host->timing_calibrated) {
2719 cout = octeontx2_mmc_calc_delay(
2720 mmc, slot->cmd_out_hs200_delay);
2721 dout = octeontx2_mmc_calc_delay(
2723 slot->data_out_hs200_delay);
2724 debug("%s(%s): Calibrated HS200/HS400 cmd out delay: %dps tap: %d, data out delay: %d, tap: %d\n",
2725 __func__, mmc->dev->name,
2726 slot->cmd_out_hs200_delay, cout,
2727 slot->data_out_hs200_delay, dout);
2729 cout = MMC_DEFAULT_HS200_CMD_OUT_TAP;
2730 dout = MMC_DEFAULT_HS200_DATA_OUT_TAP;
2737 if (host->timing_calibrated) {
2738 if (slot->cmd_out_hs400_delay)
2739 cout = octeontx2_mmc_calc_delay(
2741 slot->cmd_out_hs400_delay);
2742 if (slot->data_out_hs400_delay)
2743 dout = octeontx2_mmc_calc_delay(
2745 slot->data_out_hs400_delay);
2746 debug("%s(%s): Calibrated HS200/HS400 cmd out delay: %dps tap: %d, data out delay: %d, tap: %d\n",
2747 __func__, mmc->dev->name,
2748 slot->cmd_out_hs400_delay, cout,
2749 slot->data_out_hs400_delay, dout);
2751 cout = MMC_DEFAULT_HS400_CMD_OUT_TAP;
2752 dout = MMC_DEFAULT_HS400_DATA_OUT_TAP;
2757 pr_err("%s(%s): Invalid mode %d\n", __func__,
2758 mmc->dev->name, mmc->selected_mode);
2761 debug("%s(%s): Not tuned, hs200: %d, hs200 tuned: %d, hs400: %d, hs400 tuned: %d, tuned: %d\n",
2762 __func__, mmc->dev->name, is_hs200,
2764 is_hs400, slot->hs400_tuned, slot->tuned);
2765 /* Set some defaults */
2767 slot->hs200_taps.u = 0;
2768 slot->hs200_taps.s.cmd_out_tap = cout;
2769 slot->hs200_taps.s.data_out_tap = dout;
2770 slot->hs200_taps.s.cmd_in_tap = half;
2771 slot->hs200_taps.s.data_in_tap = half;
2772 } else if (is_hs400) {
2773 slot->hs400_taps.u = 0;
2774 slot->hs400_taps.s.cmd_out_tap = cout;
2775 slot->hs400_taps.s.data_out_tap = dout;
2776 slot->hs400_taps.s.cmd_in_tap = half;
2777 slot->hs400_taps.s.data_in_tap = half;
2780 slot->taps.s.cmd_out_tap = cout;
2781 slot->taps.s.data_out_tap = dout;
2782 slot->taps.s.cmd_in_tap = half;
2783 slot->taps.s.data_in_tap = half;
2788 debug("%s(%s): hs200 taps: ci: %u, co: %u, di: %u, do: %u\n",
2789 __func__, mmc->dev->name,
2790 slot->hs200_taps.s.cmd_in_tap,
2791 slot->hs200_taps.s.cmd_out_tap,
2792 slot->hs200_taps.s.data_in_tap,
2793 slot->hs200_taps.s.data_out_tap);
2795 debug("%s(%s): hs400 taps: ci: %u, co: %u, di: %u, do: %u\n",
2796 __func__, mmc->dev->name,
2797 slot->hs400_taps.s.cmd_in_tap,
2798 slot->hs400_taps.s.cmd_out_tap,
2799 slot->hs400_taps.s.data_in_tap,
2800 slot->hs400_taps.s.data_out_tap);
2802 debug("%s(%s): taps: ci: %u, co: %u, di: %u, do: %u\n",
2803 __func__, mmc->dev->name, slot->taps.s.cmd_in_tap,
2804 slot->taps.s.cmd_out_tap,
2805 slot->taps.s.data_in_tap,
2806 slot->taps.s.data_out_tap);
2807 octeontx_mmc_set_timing(mmc);
2808 debug("%s: Done\n", __func__);
2815 * Set the IO drive strength and slew
2817 * @param mmc mmc device
2819 static void octeontx_mmc_io_drive_setup(struct mmc *mmc)
2821 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
2822 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2823 union mio_emm_io_ctl io_ctl;
2825 if (slot->drive < 0 || slot->slew < 0)
2829 io_ctl.s.drive = slot->drive;
2830 io_ctl.s.slew = slot->slew;
2831 write_csr(mmc, MIO_EMM_IO_CTL(), io_ctl.u);
2837 * Sets the MMC watchdog timer in microseconds
2839 * @param mmc mmc device
2840 * @param us timeout in microseconds, 0 for maximum timeout
2842 static void set_wdog(struct mmc *mmc, u64 us)
2844 union mio_emm_wdog wdog;
2847 val = (us * mmc->clock) / 1000000;
2848 if (val >= (1 << 26) || !us) {
2850 pr_debug("%s: warning: timeout %llu exceeds max value %llu, truncating\n",
2852 (u64)(((1ULL << 26) - 1) * 1000000ULL) /
2854 val = (1 << 26) - 1;
2857 wdog.s.clk_cnt = val;
2858 write_csr(mmc, MIO_EMM_WDOG(), wdog.u);
2862 * Print switch errors
2864 * @param mmc mmc device
2866 static void check_switch_errors(struct mmc *mmc)
2868 union mio_emm_switch emm_switch;
2870 emm_switch.u = read_csr(mmc, MIO_EMM_SWITCH());
2871 if (emm_switch.s.switch_err0)
2872 pr_err("%s: Switch power class error\n", mmc->cfg->name);
2873 if (emm_switch.s.switch_err1)
2874 pr_err("%s: Switch HS timing error\n", mmc->cfg->name);
2875 if (emm_switch.s.switch_err2)
2876 pr_err("%s: Switch bus width error\n", mmc->cfg->name);
2879 static void do_switch(struct mmc *mmc, union mio_emm_switch emm_switch)
2881 union mio_emm_rsp_sts rsp_sts;
2882 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2883 int bus_id = emm_switch.s.bus_id;
2886 if (emm_switch.s.bus_id != 0) {
2887 emm_switch.s.bus_id = 0;
2888 write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
2890 emm_switch.s.bus_id = bus_id;
2892 debug("%s(%s, 0x%llx)\n", __func__, mmc->dev->name, emm_switch.u);
2893 write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
2895 start = get_timer(0);
2897 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
2898 if (!rsp_sts.s.switch_val)
2901 } while (get_timer(start) < 10);
2902 if (rsp_sts.s.switch_val) {
2903 pr_warn("%s(%s): Warning: writing 0x%llx to emm_switch timed out, status: 0x%llx\n",
2904 __func__, mmc->dev->name, emm_switch.u, rsp_sts.u);
2906 slot->cached_switch = emm_switch;
2907 check_switch_errors(mmc);
2908 slot->cached_switch.u = emm_switch.u;
2909 debug("%s: emm_switch: 0x%llx, rsp_lo: 0x%llx\n",
2910 __func__, read_csr(mmc, MIO_EMM_SWITCH()),
2911 read_csr(mmc, MIO_EMM_RSP_LO()));
2915 * Calibrates the delay based on the internal clock
2917 * @param mmc Pointer to mmc data structure
2919 * Return: 0 for success or -ETIMEDOUT on error
2921 * NOTE: On error a default value will be calculated.
2923 #if defined(CONFIG_ARCH_OCTEON)
2924 static int octeontx_mmc_set_input_bus_timing(struct mmc *mmc)
2929 static int octeontx_mmc_set_output_bus_timing(struct mmc *mmc)
2934 static int octeontx_mmc_calibrate_delay(struct mmc *mmc)
2940 * Given a delay in ps, return the tap delay count
2942 * @param mmc mmc data structure
2943 * @param delay delay in picoseconds
2945 * Return: Number of tap cycles or error if -1
2947 static int octeontx2_mmc_calc_delay(struct mmc *mmc, int delay)
2949 struct octeontx_mmc_host *host = mmc_to_host(mmc);
2951 if (host->is_asim || host->is_emul)
2954 if (!host->timing_taps) {
2955 pr_err("%s(%s): Error: host timing not calibrated\n",
2956 __func__, mmc->dev->name);
2959 debug("%s(%s, %d) timing taps: %llu\n", __func__, mmc->dev->name,
2960 delay, host->timing_taps);
2961 return min_t(int, DIV_ROUND_UP(delay, host->timing_taps), 63);
2964 static int octeontx_mmc_calibrate_delay(struct mmc *mmc)
2966 union mio_emm_calb emm_calb;
2967 union mio_emm_tap emm_tap;
2968 union mio_emm_cfg emm_cfg;
2969 union mio_emm_io_ctl emm_io_ctl;
2970 union mio_emm_switch emm_switch;
2971 union mio_emm_wdog emm_wdog;
2972 union mio_emm_sts_mask emm_sts_mask;
2973 union mio_emm_debug emm_debug;
2974 union mio_emm_timing emm_timing;
2975 struct octeontx_mmc_host *host = mmc_to_host(mmc);
2979 debug("%s: Calibrating delay\n", __func__);
2980 if (host->is_asim || host->is_emul) {
2981 debug(" No calibration for ASIM\n");
2985 if (host->calibrate_glitch) {
2986 emm_tap.s.delay = MMC_DEFAULT_TAP_DELAY;
2988 /* Save registers */
2989 emm_cfg.u = read_csr(mmc, MIO_EMM_CFG());
2990 emm_io_ctl.u = read_csr(mmc, MIO_EMM_IO_CTL());
2991 emm_switch.u = read_csr(mmc, MIO_EMM_SWITCH());
2992 emm_wdog.u = read_csr(mmc, MIO_EMM_WDOG());
2993 emm_sts_mask.u = read_csr(mmc, MIO_EMM_STS_MASK());
2994 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
2995 emm_timing.u = read_csr(mmc, MIO_EMM_TIMING());
2996 bus_ena = emm_cfg.s.bus_ena;
2997 bus_id = emm_switch.s.bus_id;
2998 emm_cfg.s.bus_ena = 0;
2999 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
3001 emm_cfg.s.bus_ena = 1ULL << 3;
3002 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
3005 write_csr(mmc, MIO_EMM_CALB(), emm_calb.u);
3006 emm_calb.s.start = 1;
3007 write_csr(mmc, MIO_EMM_CALB(), emm_calb.u);
3008 start = get_timer(0);
3009 /* This should only take 3 microseconds */
3012 emm_tap.u = read_csr(mmc, MIO_EMM_TAP());
3013 } while (!emm_tap.s.delay && get_timer(start) < 10);
3015 emm_calb.s.start = 0;
3016 write_csr(mmc, MIO_EMM_CALB(), emm_calb.u);
3018 emm_cfg.s.bus_ena = 0;
3019 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
3021 /* Restore registers */
3022 emm_cfg.s.bus_ena = bus_ena;
3023 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
3024 if (host->tap_requires_noclk) {
3025 /* Turn off the clock */
3026 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
3027 emm_debug.s.emmc_clk_disable = 1;
3028 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
3030 emm_debug.s.rdsync_rst = 1;
3031 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
3035 write_csr(mmc, MIO_EMM_TIMING(), emm_timing.u);
3036 if (host->tap_requires_noclk) {
3037 /* Turn the clock back on */
3039 emm_debug.s.rdsync_rst = 0;
3040 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
3042 emm_debug.s.emmc_clk_disable = 0;
3043 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
3046 write_csr(mmc, MIO_EMM_IO_CTL(), emm_io_ctl.u);
3047 bus_id = emm_switch.s.bus_id;
3048 emm_switch.s.bus_id = 0;
3049 write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
3050 emm_switch.s.bus_id = bus_id;
3051 write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
3052 write_csr(mmc, MIO_EMM_WDOG(), emm_wdog.u);
3053 write_csr(mmc, MIO_EMM_STS_MASK(), emm_sts_mask.u);
3054 write_csr(mmc, MIO_EMM_RCA(), mmc->rca);
3055 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
3057 if (!emm_tap.s.delay) {
3058 pr_err("%s: Error: delay calibration failed, timed out.\n",
3060 /* Set to default value if timed out */
3061 emm_tap.s.delay = MMC_DEFAULT_TAP_DELAY;
3066 host->timing_taps = (10 * 1000 * emm_tap.s.delay) / TOTAL_NO_OF_TAPS;
3067 debug("%s(%s): timing taps: %llu, delay: %u\n",
3068 __func__, mmc->dev->name, host->timing_taps, emm_tap.s.delay);
3069 host->timing_calibrated = true;
3073 static int octeontx_mmc_set_input_bus_timing(struct mmc *mmc)
3075 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3077 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
3078 union mio_emm_sample sample;
3081 sample.s.cmd_cnt = slot->cmd_clk_skew;
3082 sample.s.dat_cnt = slot->dat_clk_skew;
3083 write_csr(mmc, MIO_EMM_SAMPLE(), sample.u);
3085 union mio_emm_timing timing;
3087 timing.u = read_csr(mmc, MIO_EMM_TIMING());
3088 if (mmc->selected_mode == MMC_HS_200) {
3089 if (slot->hs200_tuned) {
3090 timing.s.cmd_in_tap =
3091 slot->hs200_taps.s.cmd_in_tap;
3092 timing.s.data_in_tap =
3093 slot->hs200_taps.s.data_in_tap;
3095 pr_warn("%s(%s): Warning: hs200 timing not tuned\n",
3096 __func__, mmc->dev->name);
3097 timing.s.cmd_in_tap =
3098 MMC_DEFAULT_HS200_CMD_IN_TAP;
3099 timing.s.data_in_tap =
3100 MMC_DEFAULT_HS200_DATA_IN_TAP;
3102 } else if (mmc->selected_mode == MMC_HS_400) {
3103 if (slot->hs400_tuned) {
3104 timing.s.cmd_in_tap =
3105 slot->hs400_taps.s.cmd_in_tap;
3106 timing.s.data_in_tap =
3107 slot->hs400_taps.s.data_in_tap;
3108 } else if (slot->hs200_tuned) {
3109 timing.s.cmd_in_tap =
3110 slot->hs200_taps.s.cmd_in_tap;
3111 timing.s.data_in_tap =
3112 slot->hs200_taps.s.data_in_tap;
3114 pr_warn("%s(%s): Warning: hs400 timing not tuned\n",
3115 __func__, mmc->dev->name);
3116 timing.s.cmd_in_tap =
3117 MMC_DEFAULT_HS200_CMD_IN_TAP;
3118 timing.s.data_in_tap =
3119 MMC_DEFAULT_HS200_DATA_IN_TAP;
3121 } else if (slot->tuned) {
3122 timing.s.cmd_in_tap = slot->taps.s.cmd_in_tap;
3123 timing.s.data_in_tap = slot->taps.s.data_in_tap;
3125 timing.s.cmd_in_tap = MMC_DEFAULT_CMD_IN_TAP;
3126 timing.s.data_in_tap = MMC_DEFAULT_DATA_IN_TAP;
3128 octeontx_mmc_set_emm_timing(mmc, timing);
3135 * Sets the default bus timing for the current mode.
3137 * @param mmc mmc data structure
3139 * Return: 0 for success, error otherwise
3141 static int octeontx_mmc_set_output_bus_timing(struct mmc *mmc)
3143 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3144 union mio_emm_timing timing;
3145 int cout_bdelay, dout_bdelay;
3146 unsigned int cout_delay, dout_delay;
3149 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX))
3152 debug("%s(%s)\n", __func__, mmc->dev->name);
3153 if (slot->is_asim || slot->is_emul)
3156 octeontx_mmc_calibrate_delay(mmc);
3158 if (mmc->clock < 26000000) {
3161 } else if (mmc->clock <= 52000000) {
3164 } else if (!mmc_is_mode_ddr(mmc->selected_mode)) {
3165 cout_delay = slot->cmd_out_hs200_delay;
3166 dout_delay = slot->data_out_hs200_delay;
3168 cout_delay = slot->cmd_out_hs400_delay;
3169 dout_delay = slot->data_out_hs400_delay;
3172 snprintf(env_name, sizeof(env_name), "mmc%d_hs200_dout_delay_ps",
3174 dout_delay = env_get_ulong(env_name, 10, dout_delay);
3175 debug("%s: dout_delay: %u\n", __func__, dout_delay);
3177 cout_bdelay = octeontx2_mmc_calc_delay(mmc, cout_delay);
3178 dout_bdelay = octeontx2_mmc_calc_delay(mmc, dout_delay);
3180 debug("%s: cmd output delay: %u, data output delay: %u, cmd bdelay: %d, data bdelay: %d, clock: %d\n",
3181 __func__, cout_delay, dout_delay, cout_bdelay, dout_bdelay,
3183 if (cout_bdelay < 0 || dout_bdelay < 0) {
3184 pr_err("%s: Error: could not calculate command and/or data clock skew\n",
3188 timing.u = read_csr(mmc, MIO_EMM_TIMING());
3189 timing.s.cmd_out_tap = cout_bdelay;
3190 timing.s.data_out_tap = dout_bdelay;
3191 if (mmc->selected_mode == MMC_HS_200) {
3192 slot->hs200_taps.s.cmd_out_tap = cout_bdelay;
3193 slot->hs200_taps.s.data_out_tap = dout_bdelay;
3194 } else if (mmc->selected_mode == MMC_HS_400) {
3195 slot->hs400_taps.s.cmd_out_tap = cout_bdelay;
3196 slot->hs400_taps.s.data_out_tap = dout_bdelay;
3198 slot->taps.s.cmd_out_tap = cout_bdelay;
3199 slot->taps.s.data_out_tap = dout_bdelay;
3201 octeontx_mmc_set_emm_timing(mmc, timing);
3202 debug("%s(%s): bdelay: %d/%d, clock: %d, ddr: %s, timing taps: %llu, do: %d, di: %d, co: %d, ci: %d\n",
3203 __func__, mmc->dev->name, cout_bdelay, dout_bdelay, mmc->clock,
3204 mmc->ddr_mode ? "yes" : "no",
3205 mmc_to_host(mmc)->timing_taps,
3206 timing.s.data_out_tap,
3207 timing.s.data_in_tap,
3208 timing.s.cmd_out_tap,
3209 timing.s.cmd_in_tap);
3215 static void octeontx_mmc_set_clock(struct mmc *mmc)
3217 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3220 clock = min(mmc->cfg->f_max, (uint)slot->clock);
3221 clock = max(mmc->cfg->f_min, clock);
3222 debug("%s(%s): f_min: %u, f_max: %u, clock: %u\n", __func__,
3223 mmc->dev->name, mmc->cfg->f_min, mmc->cfg->f_max, clock);
3224 slot->clock = clock;
3229 * This switches I/O power as needed when switching between slots.
3231 * @param mmc mmc data structure
3233 static void octeontx_mmc_switch_io(struct mmc *mmc)
3235 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3236 struct octeontx_mmc_host *host = slot->host;
3237 struct mmc *last_mmc = host->last_mmc;
3238 static struct udevice *last_reg;
3239 union mio_emm_cfg emm_cfg;
3241 static bool initialized;
3244 if (!initialized || mmc != host->last_mmc) {
3247 /* Switch to bus 3 which is unused */
3248 emm_cfg.u = read_csr(mmc, MIO_EMM_CFG());
3249 emm_cfg.s.bus_ena = 1 << 3;
3250 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
3252 /* Turn off all other I/O interfaces with first initialization
3253 * if at least one supply was found.
3255 for (bus = 0; bus <= OCTEONTX_MAX_MMC_SLOT; bus++) {
3256 ommc = &host->slots[bus].mmc;
3258 /* Handle self case later */
3259 if (ommc == mmc || !ommc->vqmmc_supply)
3262 /* Skip if we're not switching regulators */
3263 if (last_reg == mmc->vqmmc_supply)
3266 /* Turn off other regulators */
3267 if (ommc->vqmmc_supply != mmc->vqmmc_supply)
3268 regulator_set_enable(ommc->vqmmc_supply, false);
3270 /* Turn ourself on */
3271 if (mmc->vqmmc_supply && last_reg != mmc->vqmmc_supply)
3272 regulator_set_enable(mmc->vqmmc_supply, true);
3273 mdelay(1); /* Settle time */
3274 /* Switch to new bus */
3275 emm_cfg.s.bus_ena = 1 << slot->bus_id;
3276 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
3277 last_reg = mmc->vqmmc_supply;
3282 /* No change in device */
3283 if (last_mmc == mmc)
3287 pr_warn("%s(%s): No previous slot detected in IO slot switch!\n",
3288 __func__, mmc->dev->name);
3292 debug("%s(%s): last: %s, supply: %p\n", __func__, mmc->dev->name,
3293 last_mmc->dev->name, mmc->vqmmc_supply);
3295 /* The supply is the same so we do nothing */
3296 if (last_mmc->vqmmc_supply == mmc->vqmmc_supply)
3299 /* Turn off the old slot I/O supply */
3300 if (last_mmc->vqmmc_supply) {
3301 debug("%s(%s): Turning off IO to %s, supply: %s\n",
3302 __func__, mmc->dev->name, last_mmc->dev->name,
3303 last_mmc->vqmmc_supply->name);
3304 regulator_set_enable(last_mmc->vqmmc_supply, false);
3306 /* Turn on the new slot I/O supply */
3307 if (mmc->vqmmc_supply) {
3308 debug("%s(%s): Turning on IO to slot %d, supply: %s\n",
3309 __func__, mmc->dev->name, slot->bus_id,
3310 mmc->vqmmc_supply->name);
3311 regulator_set_enable(mmc->vqmmc_supply, true);
3313 /* Allow power to settle */
3318 * Called to switch between mmc devices
3320 * @param mmc new mmc device
3322 static void octeontx_mmc_switch_to(struct mmc *mmc)
3324 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3325 struct octeontx_mmc_slot *old_slot;
3326 struct octeontx_mmc_host *host = slot->host;
3327 union mio_emm_switch emm_switch;
3328 union mio_emm_sts_mask emm_sts_mask;
3329 union mio_emm_rca emm_rca;
3331 if (slot->bus_id == host->last_slotid)
3334 debug("%s(%s) switching from slot %d to slot %d\n", __func__,
3335 mmc->dev->name, host->last_slotid, slot->bus_id);
3336 octeontx_mmc_switch_io(mmc);
3338 if (host->last_slotid >= 0 && slot->valid) {
3339 old_slot = &host->slots[host->last_slotid];
3340 old_slot->cached_switch.u = read_csr(mmc, MIO_EMM_SWITCH());
3341 old_slot->cached_rca.u = read_csr(mmc, MIO_EMM_RCA());
3344 write_csr(mmc, MIO_EMM_RCA(), mmc->rca);
3345 emm_switch = slot->cached_switch;
3346 do_switch(mmc, emm_switch);
3348 emm_rca.s.card_rca = mmc->rca;
3349 write_csr(mmc, MIO_EMM_RCA(), emm_rca.u);
3352 set_wdog(mmc, 100000);
3353 if (octeontx_mmc_set_output_bus_timing(mmc) ||
3354 octeontx_mmc_set_input_bus_timing(mmc))
3355 pr_err("%s(%s): Error setting bus timing\n", __func__,
3357 octeontx_mmc_io_drive_setup(mmc);
3360 emm_sts_mask.s.sts_msk = 1 << 7 | 1 << 22 | 1 << 23 | 1 << 19;
3361 write_csr(mmc, MIO_EMM_STS_MASK(), emm_sts_mask.u);
3362 host->last_slotid = slot->bus_id;
3363 host->last_mmc = mmc;
3368 * Perform initial timing configuration
3370 * @param mmc mmc device
3372 * Return: 0 for success
3374 * NOTE: This will need to be updated when new silicon comes out
3376 static int octeontx_mmc_init_timing(struct mmc *mmc)
3378 union mio_emm_timing timing;
3380 if (mmc_to_slot(mmc)->is_asim || mmc_to_slot(mmc)->is_emul)
3383 debug("%s(%s)\n", __func__, mmc->dev->name);
3385 timing.s.cmd_out_tap = MMC_DEFAULT_CMD_OUT_TAP;
3386 timing.s.data_out_tap = MMC_DEFAULT_DATA_OUT_TAP;
3387 timing.s.cmd_in_tap = MMC_DEFAULT_CMD_IN_TAP;
3388 timing.s.data_in_tap = MMC_DEFAULT_DATA_IN_TAP;
3389 octeontx_mmc_set_emm_timing(mmc, timing);
3394 * Perform low-level initialization
3396 * @param mmc mmc device
3398 * Return: 0 for success, error otherwise
3400 static int octeontx_mmc_init_lowlevel(struct mmc *mmc)
3402 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3403 struct octeontx_mmc_host *host = slot->host;
3404 union mio_emm_switch emm_switch;
3407 debug("%s(%s): lowlevel init for slot %d\n", __func__,
3408 mmc->dev->name, slot->bus_id);
3409 host->emm_cfg.s.bus_ena &= ~(1 << slot->bus_id);
3410 write_csr(mmc, MIO_EMM_CFG(), host->emm_cfg.u);
3412 host->emm_cfg.s.bus_ena |= 1 << slot->bus_id;
3413 write_csr(mmc, MIO_EMM_CFG(), host->emm_cfg.u);
3415 slot->clock = mmc->cfg->f_min;
3416 octeontx_mmc_set_clock(&slot->mmc);
3418 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
3419 if (host->cond_clock_glitch) {
3420 union mio_emm_debug emm_debug;
3422 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
3423 emm_debug.s.clk_on = 1;
3424 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
3426 octeontx_mmc_calibrate_delay(&slot->mmc);
3429 clk_period = octeontx_mmc_calc_clk_period(mmc);
3431 emm_switch.s.power_class = 10;
3432 emm_switch.s.clk_lo = clk_period / 2;
3433 emm_switch.s.clk_hi = clk_period / 2;
3435 emm_switch.s.bus_id = slot->bus_id;
3436 debug("%s: Performing switch\n", __func__);
3437 do_switch(mmc, emm_switch);
3438 slot->cached_switch.u = emm_switch.u;
3440 if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX))
3441 octeontx_mmc_init_timing(mmc);
3443 set_wdog(mmc, 1000000); /* Set to 1 second */
3444 write_csr(mmc, MIO_EMM_STS_MASK(), 0xe4390080ull);
3445 write_csr(mmc, MIO_EMM_RCA(), 1);
3447 debug("%s: done\n", __func__);
3452 * Translates a voltage number to bits in MMC register
3454 * @param voltage voltage in microvolts
3456 * Return: MMC register value for voltage
3458 static u32 xlate_voltage(u32 voltage)
3462 /* Convert to millivolts. Only necessary on ARM Octeon TX/TX2 */
3463 if (!IS_ENABLED(CONFIG_ARCH_OCTEON))
3466 if (voltage >= 1650 && voltage <= 1950)
3467 volt |= MMC_VDD_165_195;
3468 if (voltage >= 2000 && voltage <= 2100)
3469 volt |= MMC_VDD_20_21;
3470 if (voltage >= 2100 && voltage <= 2200)
3471 volt |= MMC_VDD_21_22;
3472 if (voltage >= 2200 && voltage <= 2300)
3473 volt |= MMC_VDD_22_23;
3474 if (voltage >= 2300 && voltage <= 2400)
3475 volt |= MMC_VDD_23_24;
3476 if (voltage >= 2400 && voltage <= 2500)
3477 volt |= MMC_VDD_24_25;
3478 if (voltage >= 2500 && voltage <= 2600)
3479 volt |= MMC_VDD_25_26;
3480 if (voltage >= 2600 && voltage <= 2700)
3481 volt |= MMC_VDD_26_27;
3482 if (voltage >= 2700 && voltage <= 2800)
3483 volt |= MMC_VDD_27_28;
3484 if (voltage >= 2800 && voltage <= 2900)
3485 volt |= MMC_VDD_28_29;
3486 if (voltage >= 2900 && voltage <= 3000)
3487 volt |= MMC_VDD_29_30;
3488 if (voltage >= 3000 && voltage <= 3100)
3489 volt |= MMC_VDD_30_31;
3490 if (voltage >= 3100 && voltage <= 3200)
3491 volt |= MMC_VDD_31_32;
3492 if (voltage >= 3200 && voltage <= 3300)
3493 volt |= MMC_VDD_32_33;
3494 if (voltage >= 3300 && voltage <= 3400)
3495 volt |= MMC_VDD_33_34;
3496 if (voltage >= 3400 && voltage <= 3500)
3497 volt |= MMC_VDD_34_35;
3498 if (voltage >= 3500 && voltage <= 3600)
3499 volt |= MMC_VDD_35_36;
3505 * Check if a slot is valid in the device tree
3507 * @param dev slot device to check
3509 * Return: true if status reports "ok" or "okay" or if no status,
3512 static bool octeontx_mmc_get_valid(struct udevice *dev)
3514 const char *stat = ofnode_read_string(dev_ofnode(dev), "status");
3516 if (!stat || !strncmp(stat, "ok", 2))
3523 * Reads slot configuration from the device tree
3525 * @param dev slot device
3527 * Return: 0 on success, otherwise error
3529 static int octeontx_mmc_get_config(struct udevice *dev)
3531 struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
3536 ofnode node = dev_ofnode(dev);
3540 debug("%s(%s)", __func__, dev->name);
3541 slot->cfg.name = dev->name;
3543 slot->cfg.f_max = ofnode_read_s32_default(dev_ofnode(dev),
3546 snprintf(env_name, sizeof(env_name), "mmc_max_frequency%d",
3549 new_max_freq = env_get_ulong(env_name, 10, slot->cfg.f_max);
3550 debug("Reading %s, got %lu\n", env_name, new_max_freq);
3552 if (new_max_freq != slot->cfg.f_max) {
3553 printf("Overriding device tree MMC maximum frequency %u to %lu\n",
3554 slot->cfg.f_max, new_max_freq);
3555 slot->cfg.f_max = new_max_freq;
3557 slot->cfg.f_min = 400000;
3558 slot->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
3560 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
3561 slot->hs400_tuning_block =
3562 ofnode_read_s32_default(dev_ofnode(dev),
3563 "marvell,hs400-tuning-block",
3565 debug("%s(%s): mmc HS400 tuning block: %d\n", __func__,
3566 dev->name, slot->hs400_tuning_block);
3568 slot->hs200_tap_adj =
3569 ofnode_read_s32_default(dev_ofnode(dev),
3570 "marvell,hs200-tap-adjust", 0);
3571 debug("%s(%s): hs200-tap-adjust: %d\n", __func__, dev->name,
3572 slot->hs200_tap_adj);
3573 slot->hs400_tap_adj =
3574 ofnode_read_s32_default(dev_ofnode(dev),
3575 "marvell,hs400-tap-adjust", 0);
3576 debug("%s(%s): hs400-tap-adjust: %d\n", __func__, dev->name,
3577 slot->hs400_tap_adj);
3580 err = ofnode_read_u32_array(dev_ofnode(dev), "voltage-ranges",
3583 slot->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
3585 low = xlate_voltage(voltages[0]);
3586 high = xlate_voltage(voltages[1]);
3587 debug(" low voltage: 0x%x (%u), high: 0x%x (%u)\n",
3588 low, voltages[0], high, voltages[1]);
3589 if (low > high || !low || !high) {
3590 pr_err("Invalid MMC voltage range [%u-%u] specified for %s\n",
3591 low, high, dev->name);
3594 slot->cfg.voltages = 0;
3596 slot->cfg.voltages |= low;
3598 } while (low <= high);
3600 debug("%s: config voltages: 0x%x\n", __func__, slot->cfg.voltages);
3601 slot->slew = ofnode_read_s32_default(node, "cavium,clk-slew", -1);
3602 slot->drive = ofnode_read_s32_default(node, "cavium,drv-strength", -1);
3603 gpio_request_by_name(dev, "cd-gpios", 0, &slot->cd_gpio, GPIOD_IS_IN);
3604 slot->cd_inverted = ofnode_read_bool(node, "cd-inverted");
3605 gpio_request_by_name(dev, "wp-gpios", 0, &slot->wp_gpio, GPIOD_IS_IN);
3606 slot->wp_inverted = ofnode_read_bool(node, "wp-inverted");
3607 if (slot->cfg.voltages & MMC_VDD_165_195) {
3608 slot->is_1_8v = true;
3609 slot->is_3_3v = false;
3610 } else if (slot->cfg.voltages & (MMC_VDD_30_31 | MMC_VDD_31_32 |
3611 MMC_VDD_33_34 | MMC_VDD_34_35 |
3613 slot->is_1_8v = false;
3614 slot->is_3_3v = true;
3617 bus_width = ofnode_read_u32_default(node, "bus-width", 1);
3618 /* Note fall-through */
3619 switch (bus_width) {
3621 slot->cfg.host_caps |= MMC_MODE_8BIT;
3623 slot->cfg.host_caps |= MMC_MODE_4BIT;
3625 slot->cfg.host_caps |= MMC_MODE_1BIT;
3628 if (ofnode_read_bool(node, "no-1-8-v")) {
3629 slot->is_3_3v = true;
3630 slot->is_1_8v = false;
3631 if (!(slot->cfg.voltages & (MMC_VDD_32_33 | MMC_VDD_33_34)))
3632 pr_warn("%s(%s): voltages indicate 3.3v but 3.3v not supported\n",
3633 __func__, dev->name);
3635 if (ofnode_read_bool(node, "mmc-ddr-3-3v")) {
3636 slot->is_3_3v = true;
3637 slot->is_1_8v = false;
3638 if (!(slot->cfg.voltages & (MMC_VDD_32_33 | MMC_VDD_33_34)))
3639 pr_warn("%s(%s): voltages indicate 3.3v but 3.3v not supported\n",
3640 __func__, dev->name);
3642 if (ofnode_read_bool(node, "cap-sd-highspeed") ||
3643 ofnode_read_bool(node, "cap-mmc-highspeed") ||
3644 ofnode_read_bool(node, "sd-uhs-sdr25"))
3645 slot->cfg.host_caps |= MMC_MODE_HS;
3646 if (slot->cfg.f_max >= 50000000 &&
3647 slot->cfg.host_caps & MMC_MODE_HS)
3648 slot->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
3649 if (ofnode_read_bool(node, "sd-uhs-sdr50"))
3650 slot->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
3651 if (ofnode_read_bool(node, "sd-uhs-ddr50"))
3652 slot->cfg.host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz |
3655 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
3656 if (!slot->is_asim && !slot->is_emul) {
3657 if (ofnode_read_bool(node, "mmc-hs200-1_8v"))
3658 slot->cfg.host_caps |= MMC_MODE_HS200 |
3660 if (ofnode_read_bool(node, "mmc-hs400-1_8v"))
3661 slot->cfg.host_caps |= MMC_MODE_HS400 |
3665 slot->cmd_out_hs200_delay =
3666 ofnode_read_u32_default(node,
3667 "marvell,cmd-out-hs200-dly",
3668 MMC_DEFAULT_HS200_CMD_OUT_DLY);
3669 debug("%s(%s): HS200 cmd out delay: %d\n",
3670 __func__, dev->name, slot->cmd_out_hs200_delay);
3671 slot->data_out_hs200_delay =
3672 ofnode_read_u32_default(node,
3673 "marvell,data-out-hs200-dly",
3674 MMC_DEFAULT_HS200_DATA_OUT_DLY);
3675 debug("%s(%s): HS200 data out delay: %d\n",
3676 __func__, dev->name, slot->data_out_hs200_delay);
3677 slot->cmd_out_hs400_delay =
3678 ofnode_read_u32_default(node,
3679 "marvell,cmd-out-hs400-dly",
3680 MMC_DEFAULT_HS400_CMD_OUT_DLY);
3681 debug("%s(%s): HS400 cmd out delay: %d\n",
3682 __func__, dev->name, slot->cmd_out_hs400_delay);
3683 slot->data_out_hs400_delay =
3684 ofnode_read_u32_default(node,
3685 "marvell,data-out-hs400-dly",
3686 MMC_DEFAULT_HS400_DATA_OUT_DLY);
3687 debug("%s(%s): HS400 data out delay: %d\n",
3688 __func__, dev->name, slot->data_out_hs400_delay);
3692 slot->disable_ddr = ofnode_read_bool(node, "marvell,disable-ddr");
3693 slot->non_removable = ofnode_read_bool(node, "non-removable");
3694 slot->cmd_clk_skew = ofnode_read_u32_default(node,
3695 "cavium,cmd-clk-skew", 0);
3696 slot->dat_clk_skew = ofnode_read_u32_default(node,
3697 "cavium,dat-clk-skew", 0);
3698 debug("%s(%s): host caps: 0x%x\n", __func__,
3699 dev->name, slot->cfg.host_caps);
3706 * @param dev mmc device
3708 * Return: 0 for success, error otherwise
3710 static int octeontx_mmc_slot_probe(struct udevice *dev)
3712 struct octeontx_mmc_slot *slot;
3716 debug("%s(%s)\n", __func__, dev->name);
3718 pr_err("%s(%s): Error: host not probed yet\n",
3719 __func__, dev->name);
3721 slot = dev_to_mmc_slot(dev);
3725 slot->valid = false;
3726 if (!octeontx_mmc_get_valid(dev)) {
3727 debug("%s(%s): slot is invalid\n", __func__, dev->name);
3731 debug("%s(%s): Getting config\n", __func__, dev->name);
3732 err = octeontx_mmc_get_config(dev);
3734 pr_err("probe(%s): Error getting config\n", dev->name);
3738 debug("%s(%s): mmc bind, mmc: %p\n", __func__, dev->name, &slot->mmc);
3739 err = mmc_bind(dev, &slot->mmc, &slot->cfg);
3741 pr_err("%s(%s): Error binding mmc\n", __func__, dev->name);
3745 /* For some reason, mmc_bind always assigns priv to the device */
3746 slot->mmc.priv = slot;
3748 debug("%s(%s): lowlevel init\n", __func__, dev->name);
3749 err = octeontx_mmc_init_lowlevel(mmc);
3751 pr_err("probe(%s): Low-level init failed\n", dev->name);
3758 " base address : %p\n"
3759 " bus id : %d\n", __func__, dev->name,
3760 slot->base_addr, slot->bus_id);
3766 * MMC slot driver operations
3768 static const struct dm_mmc_ops octeontx_hsmmc_ops = {
3769 .send_cmd = octeontx_mmc_dev_send_cmd,
3770 .set_ios = octeontx_mmc_set_ios,
3771 .get_cd = octeontx_mmc_get_cd,
3772 .get_wp = octeontx_mmc_get_wp,
3773 #ifdef MMC_SUPPORTS_TUNING
3774 .execute_tuning = octeontx_mmc_execute_tuning,
3778 static const struct udevice_id octeontx_hsmmc_ids[] = {
3779 { .compatible = "mmc-slot" },
3783 U_BOOT_DRIVER(octeontx_hsmmc_slot) = {
3784 .name = "octeontx_hsmmc_slot",
3786 .of_match = of_match_ptr(octeontx_hsmmc_ids),
3787 .probe = octeontx_mmc_slot_probe,
3788 .ops = &octeontx_hsmmc_ops,
3791 /*****************************************************************
3794 * The PCI host driver contains the resources used by all of the
3797 * The slot drivers are pseudo drivers.
3801 * Probe the MMC host controller
3803 * @param dev mmc host controller device
3805 * Return: 0 for success, -1 on error
3807 static int octeontx_mmc_host_probe(struct udevice *dev)
3809 struct octeontx_mmc_host *host = dev_get_priv(dev);
3810 union mio_emm_int emm_int;
3815 debug("%s(%s): Entry host: %p\n", __func__, dev->name, host);
3817 if (!octeontx_mmc_get_valid(dev)) {
3818 debug("%s(%s): mmc host not valid\n", __func__, dev->name);
3821 memset(host, 0, sizeof(*host));
3823 /* Octeon TX & TX2 use PCI based probing */
3824 if (device_is_compatible(dev, "cavium,thunder-8890-mmc")) {
3825 host->base_addr = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0, 0, PCI_REGION_TYPE,
3827 if (!host->base_addr) {
3828 pr_err("%s: Error: MMC base address not found\n",
3833 host->base_addr = dev_remap_addr(dev);
3837 debug("%s(%s): Base address: %p\n", __func__, dev->name,
3839 if (!dev_has_ofnode(dev)) {
3840 pr_err("%s: No device tree information found\n", __func__);
3843 host->node = dev_ofnode(dev);
3844 host->last_slotid = -1;
3845 #if !defined(CONFIG_ARCH_OCTEON)
3846 if (otx_is_platform(PLATFORM_ASIM))
3847 host->is_asim = true;
3848 if (otx_is_platform(PLATFORM_EMULATOR))
3849 host->is_emul = true;
3851 host->dma_wait_delay =
3852 ofnode_read_u32_default(dev_ofnode(dev),
3853 "marvell,dma-wait-delay", 1);
3854 /* Force reset of eMMC */
3855 writeq(0, host->base_addr + MIO_EMM_CFG());
3856 debug("%s: Clearing MIO_EMM_CFG\n", __func__);
3858 emm_int.u = readq(host->base_addr + MIO_EMM_INT());
3859 debug("%s: Writing 0x%llx to MIO_EMM_INT\n", __func__, emm_int.u);
3860 writeq(emm_int.u, host->base_addr + MIO_EMM_INT());
3862 debug("%s(%s): Getting I/O clock\n", __func__, dev->name);
3863 ret = clk_get_by_index(dev, 0, &clk);
3867 ret = clk_enable(&clk);
3871 host->sys_freq = clk_get_rate(&clk);
3872 debug("%s(%s): I/O clock %llu\n", __func__, dev->name, host->sys_freq);
3874 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
3875 /* Flags for issues to work around */
3876 dm_pci_read_config8(dev, PCI_REVISION_ID, &rev);
3877 if (otx_is_soc(CN96XX)) {
3878 debug("%s: CN96XX revision %d\n", __func__, rev);
3881 host->calibrate_glitch = true;
3882 host->cond_clock_glitch = true;
3889 host->hs400_skew_needed = true;
3890 debug("HS400 skew support enabled\n");
3893 debug("CN96XX rev C0+ detected\n");
3894 host->tap_requires_noclk = true;
3897 } else if (otx_is_soc(CN95XX)) {
3899 host->cond_clock_glitch = true;
3909 * This performs some initial setup before a probe occurs.
3911 * @param dev: MMC slot device
3913 * Return: 0 for success, -1 on failure
3915 * Do some pre-initialization before probing a slot.
3917 static int octeontx_mmc_host_child_pre_probe(struct udevice *dev)
3919 struct octeontx_mmc_host *host = dev_get_priv(dev_get_parent(dev));
3920 struct octeontx_mmc_slot *slot;
3921 struct mmc_uclass_priv *upriv;
3922 ofnode node = dev_ofnode(dev);
3927 debug("%s(%s) Pre-Probe\n", __func__, dev->name);
3928 if (ofnode_read_u32(node, "reg", &bus_id)) {
3929 pr_err("%s(%s): Error: \"reg\" not found in device tree\n",
3930 __func__, dev->name);
3933 if (bus_id > OCTEONTX_MAX_MMC_SLOT) {
3934 pr_err("%s(%s): Error: \"reg\" out of range of 0..%d\n",
3935 __func__, dev->name, OCTEONTX_MAX_MMC_SLOT);
3939 slot = &host->slots[bus_id];
3940 dev_set_priv(dev, slot);
3942 slot->bus_id = bus_id;
3944 slot->base_addr = host->base_addr;
3945 slot->is_asim = host->is_asim;
3946 slot->is_emul = host->is_emul;
3948 snprintf(name, sizeof(name), "octeontx-mmc%d", bus_id);
3949 err = device_set_name(dev, name);
3951 /* FIXME: This code should not be needed */
3952 if (!dev_get_uclass_priv(dev)) {
3953 debug("%s(%s): Allocating uclass priv\n", __func__,
3955 upriv = calloc(1, sizeof(struct mmc_uclass_priv));
3960 * FIXME: This is not allowed
3961 * dev_set_uclass_priv(dev, upriv);
3962 * uclass_set_priv(dev->uclass, upriv);
3965 upriv = dev_get_uclass_priv(dev);
3968 upriv->mmc = &slot->mmc;
3969 debug("%s: uclass priv: %p, mmc: %p\n", dev->name, upriv, upriv->mmc);
3971 debug("%s: ret: %d\n", __func__, err);
3975 static const struct udevice_id octeontx_hsmmc_host_ids[] = {
3976 { .compatible = "cavium,thunder-8890-mmc" },
3977 { .compatible = "cavium,octeon-7360-mmc" },
3981 U_BOOT_DRIVER(octeontx_hsmmc_host) = {
3982 .name = "octeontx_hsmmc_host",
3983 /* FIXME: Why is this not UCLASS_MMC? */
3985 .of_match = of_match_ptr(octeontx_hsmmc_host_ids),
3986 .probe = octeontx_mmc_host_probe,
3987 .priv_auto = sizeof(struct octeontx_mmc_host),
3988 .child_pre_probe = octeontx_mmc_host_child_pre_probe,
3989 .flags = DM_FLAG_PRE_RELOC,
3992 static struct pci_device_id octeontx_mmc_supported[] = {
3993 { PCI_VDEVICE(CAVIUM, PCI_DEVICE_ID_CAVIUM_EMMC) },
3997 U_BOOT_PCI_DEVICE(octeontx_hsmmc_host, octeontx_mmc_supported);