blob: ddc36694e1edb299261e5bc8288299e6b379d7e4 [file] [log] [blame]
Suneel Garapati70859892019-10-19 18:03:01 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2019 Marvell International Ltd.
4 *
5 * https://spdx.org/licenses
6 */
7
8//#define DEBUG
9#include <cpu_func.h>
10#include <dm.h>
11#include <dm/lists.h>
12#include <env.h>
13#include <errno.h>
14#include <fdtdec.h>
15#include <log.h>
16#include <malloc.h>
17#include <memalign.h>
18#include <mmc.h>
19#include <part.h>
20#include <pci.h>
21#include <pci_ids.h>
22#include <time.h>
23#include <watchdog.h>
24
25#include <linux/delay.h>
26#include <linux/kernel.h>
27#include <linux/libfdt.h>
28
29#include <asm/arch/board.h>
30#include <asm/arch/clock.h>
31#include <asm/arch/csrs/csrs-mio_emm.h>
32#include <asm/io.h>
33
34#include <power/regulator.h>
35
36#include "octeontx_hsmmc.h"
37
38#define MMC_TIMEOUT_SHORT 20 /* in ms */
39#define MMC_TIMEOUT_LONG 1000
40#define MMC_TIMEOUT_ERASE 10000
41
42#define MMC_DEFAULT_DATA_IN_TAP 10
43#define MMC_DEFAULT_CMD_IN_TAP 10
44#define MMC_DEFAULT_CMD_OUT_TAP 39
45#define MMC_DEFAULT_DATA_OUT_TAP 39
46#define MMC_DEFAULT_HS200_CMD_IN_TAP 24
47#define MMC_DEFAULT_HS200_DATA_IN_TAP 24
48#define MMC_DEFAULT_HS200_CMD_OUT_TAP (otx_is_soc(CN95XX) ? 10 : 5)
49#define MMC_DEFAULT_HS200_DATA_OUT_TAP (otx_is_soc(CN95XX) ? 10 : 5)
50#define MMC_DEFAULT_HS400_CMD_OUT_TAP (otx_is_soc(CN95XX) ? 10 : 5)
51#define MMC_DEFAULT_HS400_DATA_OUT_TAP (otx_is_soc(CN95XX) ? 5 : 3)
52#define MMC_DEFAULT_HS200_CMD_OUT_DLY 800 /* Delay in ps */
53#define MMC_DEFAULT_HS200_DATA_OUT_DLY 800 /* Delay in ps */
54#define MMC_DEFAULT_HS400_CMD_OUT_DLY 800 /* Delay in ps */
55#define MMC_DEFAULT_HS400_DATA_OUT_DLY 400 /* Delay in ps */
56#define MMC_DEFAULT_SD_UHS_SDR104_CMD_OUT_TAP MMC_DEFAULT_HS200_CMD_OUT_TAP
57#define MMC_DEFAULT_SD_UHS_SDR104_DATA_OUT_TAP MMC_DEFAULT_HS200_DATA_OUT_TAP
58#define MMC_LEGACY_DEFAULT_CMD_OUT_TAP 39
59#define MMC_LEGACY_DEFAULT_DATA_OUT_TAP 39
60#define MMC_SD_LEGACY_DEFAULT_CMD_OUT_TAP 63
61#define MMC_SD_LEGACY_DEFAULT_DATA_OUT_TAP 63
62#define MMC_HS_CMD_OUT_TAP 32
63#define MMC_HS_DATA_OUT_TAP 32
64#define MMC_SD_HS_CMD_OUT_TAP 26
65#define MMC_SD_HS_DATA_OUT_TAP 26
66#define MMC_SD_UHS_SDR25_CMD_OUT_TAP 26
67#define MMC_SD_UHS_SDR25_DATA_OUT_TAP 26
68#define MMC_SD_UHS_SDR50_CMD_OUT_TAP 26
69#define MMC_SD_UHS_SDR50_DATA_OUT_TAP 26
70#define MMC_DEFAULT_TAP_DELAY 4
71#define TOTAL_NO_OF_TAPS 512
72static void octeontx_mmc_switch_to(struct mmc *mmc);
73static int octeontx_mmc_configure_delay(struct mmc *mmc);
74static void octeontx_mmc_set_timing(struct mmc *mmc);
75static void set_wdog(struct mmc *mmc, u64 us);
76static void do_switch(struct mmc *mmc, union mio_emm_switch emm_switch);
77static int octeontx_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
78 struct mmc_data *data);
79static int octeontx2_mmc_calc_delay(struct mmc *mmc, int delay);
80static int octeontx_mmc_calibrate_delay(struct mmc *mmc);
81static int octeontx_mmc_set_input_bus_timing(struct mmc *mmc);
82static int octeontx_mmc_set_output_bus_timing(struct mmc *mmc);
83
84static bool host_probed;
85
86/**
87 * Get the slot data structure from a MMC data structure
88 */
89static inline struct octeontx_mmc_slot *mmc_to_slot(struct mmc *mmc)
90{
91 return container_of(mmc, struct octeontx_mmc_slot, mmc);
92}
93
94static inline struct octeontx_mmc_host *mmc_to_host(struct mmc *mmc)
95{
96 return mmc_to_slot(mmc)->host;
97}
98
99static inline struct octeontx_mmc_slot *dev_to_mmc_slot(struct udevice *dev)
100{
101 return dev_get_priv(dev);
102}
103
104static inline struct mmc *dev_to_mmc(struct udevice *dev)
105{
106 return &((struct octeontx_mmc_slot *)dev_get_priv(dev))->mmc;
107}
108
109#ifdef DEBUG
110const char *mmc_reg_str(u64 reg)
111{
112 if (reg == MIO_EMM_DMA_CFG())
113 return "MIO_EMM_DMA_CFG";
114 if (reg == MIO_EMM_DMA_ADR())
115 return "MIO_EMM_DMA_ADR";
116 if (reg == MIO_EMM_DMA_INT())
117 return "MIO_EMM_DMA_INT";
118 if (reg == MIO_EMM_CFG())
119 return "MIO_EMM_CFG";
120 if (reg == MIO_EMM_MODEX(0))
121 return "MIO_EMM_MODE0";
122 if (reg == MIO_EMM_MODEX(1))
123 return "MIO_EMM_MODE1";
124 if (reg == MIO_EMM_MODEX(2))
125 return "MIO_EMM_MODE2";
126 if (reg == MIO_EMM_MODEX(3))
127 return "MIO_EMM_MODE3";
128 if (reg == MIO_EMM_IO_CTL())
129 return "MIO_EMM_IO_CTL";
130 if (reg == MIO_EMM_SWITCH())
131 return "MIO_EMM_SWITCH";
132 if (reg == MIO_EMM_DMA())
133 return "MIO_EMM_DMA";
134 if (reg == MIO_EMM_CMD())
135 return "MIO_EMM_CMD";
136 if (reg == MIO_EMM_RSP_STS())
137 return "MIO_EMM_RSP_STS";
138 if (reg == MIO_EMM_RSP_LO())
139 return "MIO_EMM_RSP_LO";
140 if (reg == MIO_EMM_RSP_HI())
141 return "MIO_EMM_RSP_HI";
142 if (reg == MIO_EMM_INT())
143 return "MIO_EMM_INT";
144 if (reg == MIO_EMM_WDOG())
145 return "MIO_EMM_WDOG";
146 if (reg == MIO_EMM_DMA_ARG())
147 return "MIO_EMM_DMA_ARG";
148 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
149 if (reg == MIO_EMM_SAMPLE())
150 return "MIO_EMM_SAMPLE";
151 }
152 if (reg == MIO_EMM_STS_MASK())
153 return "MIO_EMM_STS_MASK";
154 if (reg == MIO_EMM_RCA())
155 return "MIO_EMM_RCA";
156 if (reg == MIO_EMM_BUF_IDX())
157 return "MIO_EMM_BUF_IDX";
158 if (reg == MIO_EMM_BUF_DAT())
159 return "MIO_EMM_BUF_DAT";
160 if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
161 if (reg == MIO_EMM_CALB())
162 return "MIO_EMM_CALB";
163 if (reg == MIO_EMM_TAP())
164 return "MIO_EMM_TAP";
165 if (reg == MIO_EMM_TIMING())
166 return "MIO_EMM_TIMING";
167 if (reg == MIO_EMM_DEBUG())
168 return "MIO_EMM_DEBUG";
169 }
170
171 return "UNKNOWN";
172}
173#endif
174
175static void octeontx_print_rsp_sts(struct mmc *mmc)
176{
177#ifdef DEBUG
178 union mio_emm_rsp_sts emm_rsp_sts;
179 const struct octeontx_mmc_host *host = mmc_to_host(mmc);
180 static const char * const ctype_xor_str[] = {
181 "No data",
182 "Read data into Dbuf",
183 "Write data from Dbuf",
184 "Reserved"
185 };
186
187 static const char * const rtype_xor_str[] = {
188 "No response",
189 "R1, 48 bits",
190 "R2, 136 bits",
191 "R3, 48 bits",
192 "R4, 48 bits",
193 "R5, 48 bits",
194 "Reserved 6",
195 "Reserved 7"
196 };
197
198 emm_rsp_sts.u = readq(host->base_addr + MIO_EMM_RSP_STS());
199 printf("\nMIO_EMM_RSP_STS: 0x%016llx\n", emm_rsp_sts.u);
200 printf(" 60-61: bus_id: %u\n", emm_rsp_sts.s.bus_id);
201 printf(" 59: cmd_val: %s\n",
202 emm_rsp_sts.s.cmd_val ? "yes" : "no");
203 printf(" 58: switch_val: %s\n",
204 emm_rsp_sts.s.switch_val ? "yes" : "no");
205 printf(" 57: dma_val: %s\n",
206 emm_rsp_sts.s.dma_val ? "yes" : "no");
207 printf(" 56: dma_pend: %s\n",
208 emm_rsp_sts.s.dma_pend ? "yes" : "no");
209 printf(" 28: dbuf_err: %s\n",
210 emm_rsp_sts.s.dbuf_err ? "yes" : "no");
211 printf(" 23: dbuf: %u\n", emm_rsp_sts.s.dbuf);
212 printf(" 22: blk_timeout: %s\n",
213 emm_rsp_sts.s.blk_timeout ? "yes" : "no");
214 printf(" 21: blk_crc_err: %s\n",
215 emm_rsp_sts.s.blk_crc_err ? "yes" : "no");
216 printf(" 20: rsp_busybit: %s\n",
217 emm_rsp_sts.s.rsp_busybit ? "yes" : "no");
218 printf(" 19: stp_timeout: %s\n",
219 emm_rsp_sts.s.stp_timeout ? "yes" : "no");
220 printf(" 18: stp_crc_err: %s\n",
221 emm_rsp_sts.s.stp_crc_err ? "yes" : "no");
222 printf(" 17: stp_bad_sts: %s\n",
223 emm_rsp_sts.s.stp_bad_sts ? "yes" : "no");
224 printf(" 16: stp_val: %s\n",
225 emm_rsp_sts.s.stp_val ? "yes" : "no");
226 printf(" 15: rsp_timeout: %s\n",
227 emm_rsp_sts.s.rsp_timeout ? "yes" : "no");
228 printf(" 14: rsp_crc_err: %s\n",
229 emm_rsp_sts.s.rsp_crc_err ? "yes" : "no");
230 printf(" 13: rsp_bad_sts: %s\n",
231 emm_rsp_sts.s.rsp_bad_sts ? "yes" : "no");
232 printf(" 12: rsp_val: %s\n",
233 emm_rsp_sts.s.rsp_val ? "yes" : "no");
234 printf(" 9-11: rsp_type: %s\n",
235 rtype_xor_str[emm_rsp_sts.s.rsp_type]);
236 printf(" 7-8: cmd_type: %s\n",
237 ctype_xor_str[emm_rsp_sts.s.cmd_type]);
238 printf(" 1-6: cmd_idx: %u\n",
239 emm_rsp_sts.s.cmd_idx);
240 printf(" 0: cmd_done: %s\n",
241 emm_rsp_sts.s.cmd_done ? "yes" : "no");
242#endif
243}
244
245static inline u64 read_csr(struct mmc *mmc, u64 reg)
246{
247 const struct octeontx_mmc_host *host = mmc_to_host(mmc);
248 u64 value = readq(host->base_addr + reg);
249#ifdef DEBUG_CSR
250 printf(" %s: %s(0x%p) => 0x%llx\n", __func__,
251 mmc_reg_str(reg), host->base_addr + reg,
252 value);
253#endif
254 return value;
255}
256
257/**
258 * Writes to a CSR register
259 *
260 * @param[in] mmc pointer to mmc data structure
261 * @param reg register offset
262 * @param value value to write to register
263 */
264static inline void write_csr(struct mmc *mmc, u64 reg, u64 value)
265{
266 const struct octeontx_mmc_host *host = mmc_to_host(mmc);
267 void *addr = host->base_addr + reg;
268
269#ifdef DEBUG_CSR
270 printf(" %s: %s(0x%p) <= 0x%llx\n", __func__, mmc_reg_str(reg),
271 addr, value);
272#endif
273 writeq(value, addr);
274}
275
276#ifdef DEBUG
277static void mmc_print_status(u32 status)
278{
279#ifdef DEBUG_STATUS
280 static const char * const state[] = {
281 "Idle", /* 0 */
282 "Ready", /* 1 */
283 "Ident", /* 2 */
284 "Standby", /* 3 */
285 "Tran", /* 4 */
286 "Data", /* 5 */
287 "Receive", /* 6 */
288 "Program", /* 7 */
289 "Dis", /* 8 */
290 "Btst", /* 9 */
291 "Sleep", /* 10 */
292 "reserved", /* 11 */
293 "reserved", /* 12 */
294 "reserved", /* 13 */
295 "reserved", /* 14 */
296 "reserved" /* 15 */ };
297 if (status & R1_APP_CMD)
298 puts("MMC ACMD\n");
299 if (status & R1_SWITCH_ERROR)
300 puts("MMC switch error\n");
301 if (status & R1_READY_FOR_DATA)
302 puts("MMC ready for data\n");
303 printf("MMC %s state\n", state[R1_CURRENT_STATE(status)]);
304 if (status & R1_ERASE_RESET)
305 puts("MMC erase reset\n");
306 if (status & R1_WP_ERASE_SKIP)
307 puts("MMC partial erase due to write protected blocks\n");
308 if (status & R1_CID_CSD_OVERWRITE)
309 puts("MMC CID/CSD overwrite error\n");
310 if (status & R1_ERROR)
311 puts("MMC undefined device error\n");
312 if (status & R1_CC_ERROR)
313 puts("MMC device error\n");
314 if (status & R1_CARD_ECC_FAILED)
315 puts("MMC internal ECC failed to correct data\n");
316 if (status & R1_ILLEGAL_COMMAND)
317 puts("MMC illegal command\n");
318 if (status & R1_COM_CRC_ERROR)
319 puts("MMC CRC of previous command failed\n");
320 if (status & R1_LOCK_UNLOCK_FAILED)
321 puts("MMC sequence or password error in lock/unlock device command\n");
322 if (status & R1_CARD_IS_LOCKED)
323 puts("MMC device locked by host\n");
324 if (status & R1_WP_VIOLATION)
325 puts("MMC attempt to program write protected block\n");
326 if (status & R1_ERASE_PARAM)
327 puts("MMC invalid selection of erase groups for erase\n");
328 if (status & R1_ERASE_SEQ_ERROR)
329 puts("MMC error in sequence of erase commands\n");
330 if (status & R1_BLOCK_LEN_ERROR)
331 puts("MMC block length error\n");
332 if (status & R1_ADDRESS_ERROR)
333 puts("MMC address misalign error\n");
334 if (status & R1_OUT_OF_RANGE)
335 puts("MMC address out of range\n");
336#endif
337}
338#endif
339
340/**
341 * Print out all of the register values where mmc is optional
342 *
343 * @param mmc MMC device (can be NULL)
344 * @param host Pointer to host data structure (can be NULL if mmc is !NULL)
345 */
346static void octeontx_mmc_print_registers2(struct mmc *mmc,
347 struct octeontx_mmc_host *host)
348{
349 struct octeontx_mmc_slot *slot = mmc ? mmc->priv : NULL;
350 union mio_emm_dma_cfg emm_dma_cfg;
351 union mio_emm_dma_adr emm_dma_adr;
352 union mio_emm_dma_int emm_dma_int;
353 union mio_emm_cfg emm_cfg;
354 union mio_emm_modex emm_mode;
355 union mio_emm_switch emm_switch;
356 union mio_emm_dma emm_dma;
357 union mio_emm_cmd emm_cmd;
358 union mio_emm_rsp_sts emm_rsp_sts;
359 union mio_emm_rsp_lo emm_rsp_lo;
360 union mio_emm_rsp_hi emm_rsp_hi;
361 union mio_emm_int emm_int;
362 union mio_emm_wdog emm_wdog;
363 union mio_emm_sample emm_sample;
364 union mio_emm_calb emm_calb;
365 union mio_emm_tap emm_tap;
366 union mio_emm_timing emm_timing;
367 union mio_emm_io_ctl io_ctl;
368 union mio_emm_debug emm_debug;
369 union mio_emm_sts_mask emm_sts_mask;
370 union mio_emm_rca emm_rca;
371 int bus;
372
373 static const char * const bus_width_str[] = {
374 "1-bit data bus (power on)",
375 "4-bit data bus",
376 "8-bit data bus",
377 "reserved (3)",
378 "reserved (4)",
379 "4-bit data bus (dual data rate)",
380 "8-bit data bus (dual data rate)",
381 "reserved (7)",
382 "reserved (8)",
383 "invalid (9)",
384 "invalid (10)",
385 "invalid (11)",
386 "invalid (12)",
387 "invalid (13)",
388 "invalid (14)",
389 "invalid (15)",
390 };
391 static const char * const ctype_xor_str[] = {
392 "No data",
393 "Read data into Dbuf",
394 "Write data from Dbuf",
395 "Reserved"
396 };
397
398 static const char * const rtype_xor_str[] = {
399 "No response",
400 "R1, 48 bits",
401 "R2, 136 bits",
402 "R3, 48 bits",
403 "R4, 48 bits",
404 "R5, 48 bits",
405 "Reserved 6",
406 "Reserved 7"
407 };
408
409 if (!host && mmc)
410 host = mmc_to_host(mmc);
411
412 if (mmc)
413 printf("%s: bus id: %u\n", __func__, slot->bus_id);
414 emm_dma_cfg.u = readq(host->base_addr + MIO_EMM_DMA_CFG());
415 printf("MIO_EMM_DMA_CFG: 0x%016llx\n",
416 emm_dma_cfg.u);
417 printf(" 63: en: %s\n",
418 emm_dma_cfg.s.en ? "enabled" : "disabled");
419 printf(" 62: rw: %s\n",
420 emm_dma_cfg.s.rw ? "write" : "read");
421 printf(" 61: clr: %s\n",
422 emm_dma_cfg.s.clr ? "clear" : "not clear");
423 printf(" 59: swap32: %s\n",
424 emm_dma_cfg.s.swap32 ? "yes" : "no");
425 printf(" 58: swap16: %s\n",
426 emm_dma_cfg.s.swap16 ? "yes" : "no");
427 printf(" 57: swap8: %s\n",
428 emm_dma_cfg.s.swap8 ? "yes" : "no");
429 printf(" 56: endian: %s\n",
430 emm_dma_cfg.s.endian ? "little" : "big");
431 printf(" 36-55: size: %u\n",
432 emm_dma_cfg.s.size);
433
434 emm_dma_adr.u = readq(host->base_addr + MIO_EMM_DMA_ADR());
435 printf("MIO_EMM_DMA_ADR: 0x%016llx\n", emm_dma_adr.u);
436 printf(" 0-49: adr: 0x%llx\n",
437 (u64)emm_dma_adr.s.adr);
438
439 emm_dma_int.u = readq(host->base_addr + MIO_EMM_DMA_INT());
440 printf("\nMIO_EMM_DMA_INT: 0x%016llx\n",
441 emm_dma_int.u);
442 printf(" 1: FIFO: %s\n",
443 emm_dma_int.s.fifo ? "yes" : "no");
444 printf(" 0: Done: %s\n",
445 emm_dma_int.s.done ? "yes" : "no");
446 emm_cfg.u = readq(host->base_addr + MIO_EMM_CFG());
447
448 printf("\nMIO_EMM_CFG: 0x%016llx\n",
449 emm_cfg.u);
450 printf(" 3: bus_ena3: %s\n",
451 emm_cfg.s.bus_ena & 0x08 ? "yes" : "no");
452 printf(" 2: bus_ena2: %s\n",
453 emm_cfg.s.bus_ena & 0x04 ? "yes" : "no");
454 printf(" 1: bus_ena1: %s\n",
455 emm_cfg.s.bus_ena & 0x02 ? "yes" : "no");
456 printf(" 0: bus_ena0: %s\n",
457 emm_cfg.s.bus_ena & 0x01 ? "yes" : "no");
458 for (bus = 0; bus < 4; bus++) {
459 emm_mode.u = readq(host->base_addr + MIO_EMM_MODEX(bus));
460 printf("\nMIO_EMM_MODE%u: 0x%016llx\n",
461 bus, emm_mode.u);
462 if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
463 printf(" 50: hs400_timing: %s\n",
464 emm_mode.s.hs400_timing ? "yes" : "no");
465 printf(" 49: hs200_timing: %s\n",
466 emm_mode.s.hs200_timing ? "yes" : "no");
467 }
468 printf(" 48: hs_timing: %s\n",
469 emm_mode.s.hs_timing ? "yes" : "no");
470 printf(" 40-42: bus_width: %s\n",
471 bus_width_str[emm_mode.s.bus_width]);
472 printf(" 32-35: power_class %u\n",
473 emm_mode.s.power_class);
474 printf(" 16-31: clk_hi: %u\n",
475 emm_mode.s.clk_hi);
476 printf(" 0-15: clk_lo: %u\n",
477 emm_mode.s.clk_lo);
478 }
479
480 emm_switch.u = readq(host->base_addr + MIO_EMM_SWITCH());
481 printf("\nMIO_EMM_SWITCH: 0x%016llx\n", emm_switch.u);
482 printf(" 60-61: bus_id: %u\n", emm_switch.s.bus_id);
483 printf(" 59: switch_exe: %s\n",
484 emm_switch.s.switch_exe ? "yes" : "no");
485 printf(" 58: switch_err0: %s\n",
486 emm_switch.s.switch_err0 ? "yes" : "no");
487 printf(" 57: switch_err1: %s\n",
488 emm_switch.s.switch_err1 ? "yes" : "no");
489 printf(" 56: switch_err2: %s\n",
490 emm_switch.s.switch_err2 ? "yes" : "no");
491 printf(" 48: hs_timing: %s\n",
492 emm_switch.s.hs_timing ? "yes" : "no");
493 printf(" 42-40: bus_width: %s\n",
494 bus_width_str[emm_switch.s.bus_width]);
495 printf(" 32-35: power_class: %u\n",
496 emm_switch.s.power_class);
497 printf(" 16-31: clk_hi: %u\n",
498 emm_switch.s.clk_hi);
499 printf(" 0-15: clk_lo: %u\n", emm_switch.s.clk_lo);
500
501 emm_dma.u = readq(host->base_addr + MIO_EMM_DMA());
502 printf("\nMIO_EMM_DMA: 0x%016llx\n", emm_dma.u);
503 printf(" 60-61: bus_id: %u\n", emm_dma.s.bus_id);
504 printf(" 59: dma_val: %s\n",
505 emm_dma.s.dma_val ? "yes" : "no");
506 printf(" 58: sector: %s mode\n",
507 emm_dma.s.sector ? "sector" : "byte");
508 printf(" 57: dat_null: %s\n",
509 emm_dma.s.dat_null ? "yes" : "no");
510 printf(" 51-56: thres: %u\n", emm_dma.s.thres);
511 printf(" 50: rel_wr: %s\n",
512 emm_dma.s.rel_wr ? "yes" : "no");
513 printf(" 49: rw: %s\n",
514 emm_dma.s.rw ? "write" : "read");
515 printf(" 48: multi: %s\n",
516 emm_dma.s.multi ? "yes" : "no");
517 printf(" 32-47: block_cnt: %u\n",
518 emm_dma.s.block_cnt);
519 printf(" 0-31: card_addr: 0x%x\n",
520 emm_dma.s.card_addr);
521
522 emm_cmd.u = readq(host->base_addr + MIO_EMM_CMD());
523 printf("\nMIO_EMM_CMD: 0x%016llx\n", emm_cmd.u);
524 printf("\n 62: skip_busy: %s\n",
525 emm_cmd.s.skip_busy ? "yes" : "no");
526 printf(" 60-61: bus_id: %u\n", emm_cmd.s.bus_id);
527 printf(" 59: cmd_val: %s\n",
528 emm_cmd.s.cmd_val ? "yes" : "no");
529 printf(" 55: dbuf: %u\n", emm_cmd.s.dbuf);
530 printf(" 49-54: offset: %u\n", emm_cmd.s.offset);
531 printf(" 41-42: ctype_xor: %s\n",
532 ctype_xor_str[emm_cmd.s.ctype_xor]);
533 printf(" 38-40: rtype_xor: %s\n",
534 rtype_xor_str[emm_cmd.s.rtype_xor]);
535 printf(" 32-37: cmd_idx: %u\n", emm_cmd.s.cmd_idx);
536 printf(" 0-31: arg: 0x%x\n", emm_cmd.s.arg);
537
538 emm_rsp_sts.u = readq(host->base_addr + MIO_EMM_RSP_STS());
539 printf("\nMIO_EMM_RSP_STS: 0x%016llx\n", emm_rsp_sts.u);
540 printf(" 60-61: bus_id: %u\n", emm_rsp_sts.s.bus_id);
541 printf(" 59: cmd_val: %s\n",
542 emm_rsp_sts.s.cmd_val ? "yes" : "no");
543 printf(" 58: switch_val: %s\n",
544 emm_rsp_sts.s.switch_val ? "yes" : "no");
545 printf(" 57: dma_val: %s\n",
546 emm_rsp_sts.s.dma_val ? "yes" : "no");
547 printf(" 56: dma_pend: %s\n",
548 emm_rsp_sts.s.dma_pend ? "yes" : "no");
549 printf(" 28: dbuf_err: %s\n",
550 emm_rsp_sts.s.dbuf_err ? "yes" : "no");
551 printf(" 23: dbuf: %u\n", emm_rsp_sts.s.dbuf);
552 printf(" 22: blk_timeout: %s\n",
553 emm_rsp_sts.s.blk_timeout ? "yes" : "no");
554 printf(" 21: blk_crc_err: %s\n",
555 emm_rsp_sts.s.blk_crc_err ? "yes" : "no");
556 printf(" 20: rsp_busybit: %s\n",
557 emm_rsp_sts.s.rsp_busybit ? "yes" : "no");
558 printf(" 19: stp_timeout: %s\n",
559 emm_rsp_sts.s.stp_timeout ? "yes" : "no");
560 printf(" 18: stp_crc_err: %s\n",
561 emm_rsp_sts.s.stp_crc_err ? "yes" : "no");
562 printf(" 17: stp_bad_sts: %s\n",
563 emm_rsp_sts.s.stp_bad_sts ? "yes" : "no");
564 printf(" 16: stp_val: %s\n",
565 emm_rsp_sts.s.stp_val ? "yes" : "no");
566 printf(" 15: rsp_timeout: %s\n",
567 emm_rsp_sts.s.rsp_timeout ? "yes" : "no");
568 printf(" 14: rsp_crc_err: %s\n",
569 emm_rsp_sts.s.rsp_crc_err ? "yes" : "no");
570 printf(" 13: rsp_bad_sts: %s\n",
571 emm_rsp_sts.s.rsp_bad_sts ? "yes" : "no");
572 printf(" 12: rsp_val: %s\n",
573 emm_rsp_sts.s.rsp_val ? "yes" : "no");
574 printf(" 9-11: rsp_type: %s\n",
575 rtype_xor_str[emm_rsp_sts.s.rsp_type]);
576 printf(" 7-8: cmd_type: %s\n",
577 ctype_xor_str[emm_rsp_sts.s.cmd_type]);
578 printf(" 1-6: cmd_idx: %u\n",
579 emm_rsp_sts.s.cmd_idx);
580 printf(" 0: cmd_done: %s\n",
581 emm_rsp_sts.s.cmd_done ? "yes" : "no");
582
583 emm_rsp_lo.u = readq(host->base_addr + MIO_EMM_RSP_LO());
584 printf("\nMIO_EMM_RSP_STS_LO: 0x%016llx\n", emm_rsp_lo.u);
585
586 emm_rsp_hi.u = readq(host->base_addr + MIO_EMM_RSP_HI());
587 printf("\nMIO_EMM_RSP_STS_HI: 0x%016llx\n", emm_rsp_hi.u);
588
589 emm_int.u = readq(host->base_addr + MIO_EMM_INT());
590 printf("\nMIO_EMM_INT: 0x%016llx\n", emm_int.u);
591 printf(" 6: switch_err: %s\n",
592 emm_int.s.switch_err ? "yes" : "no");
593 printf(" 5: switch_done: %s\n",
594 emm_int.s.switch_done ? "yes" : "no");
595 printf(" 4: dma_err: %s\n",
596 emm_int.s.dma_err ? "yes" : "no");
597 printf(" 3: cmd_err: %s\n",
598 emm_int.s.cmd_err ? "yes" : "no");
599 printf(" 2: dma_done: %s\n",
600 emm_int.s.dma_done ? "yes" : "no");
601 printf(" 1: cmd_done: %s\n",
602 emm_int.s.cmd_done ? "yes" : "no");
603 printf(" 0: buf_done: %s\n",
604 emm_int.s.buf_done ? "yes" : "no");
605
606 emm_wdog.u = readq(host->base_addr + MIO_EMM_WDOG());
607 printf("\nMIO_EMM_WDOG: 0x%016llx (%u)\n",
608 emm_wdog.u, emm_wdog.s.clk_cnt);
609
610 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
611 emm_sample.u = readq(host->base_addr + MIO_EMM_SAMPLE());
612 printf("\nMIO_EMM_SAMPLE: 0x%016llx\n",
613 emm_sample.u);
614 printf(" 16-25: cmd_cnt: %u\n",
615 emm_sample.s.cmd_cnt);
616 printf(" 0-9: dat_cnt: %u\n",
617 emm_sample.s.dat_cnt);
618 }
619
620 emm_sts_mask.u = readq(host->base_addr + MIO_EMM_STS_MASK());
621 printf("\nMIO_EMM_STS_MASK: 0x%016llx\n", emm_sts_mask.u);
622
623 emm_rca.u = readq(host->base_addr + MIO_EMM_RCA());
624 printf("\nMIO_EMM_RCA: 0x%016llx\n", emm_rca.u);
625 printf(" 0-15: card_rca: 0x%04x\n",
626 emm_rca.s.card_rca);
627 if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
628 emm_calb.u = readq(host->base_addr + MIO_EMM_CALB());
629 printf("\nMIO_EMM_CALB: 0x%016llx\n",
630 emm_calb.u);
631 printf(" 0: start: %u\n",
632 emm_calb.s.start);
633 emm_tap.u = readq(host->base_addr + MIO_EMM_TAP());
634 printf("\nMIO_EMM_TAP: 0x%016llx\n",
635 emm_tap.u);
636 printf(" 7-0: delay: %u\n", emm_tap.s.delay);
637 emm_timing.u = readq(host->base_addr + MIO_EMM_TIMING());
638 printf("\nMIO_EMM_TIMING: 0x%016llx\n",
639 emm_timing.u);
640 printf(" 53-48: cmd_in_tap: %u\n",
641 emm_timing.s.cmd_in_tap);
642 printf(" 37-32: cmd_out_tap: %u\n",
643 emm_timing.s.cmd_out_tap);
644 printf(" 21-16: data_in_tap: %u\n",
645 emm_timing.s.data_in_tap);
646 printf(" 5-0: data_out_tap: %u\n",
647 emm_timing.s.data_out_tap);
648 io_ctl.u = readq(host->base_addr + MIO_EMM_IO_CTL());
649 printf("\nMIO_IO_CTL: 0x%016llx\n", io_ctl.u);
650 printf(" 3-2: drive: %u (%u mA)\n",
651 io_ctl.s.drive, 2 << io_ctl.s.drive);
652 printf(" 0: slew: %u %s\n", io_ctl.s.slew,
653 io_ctl.s.slew ? "high" : "low");
654 emm_debug.u = readq(host->base_addr + MIO_EMM_DEBUG());
655 printf("\nMIO_EMM_DEBUG: 0x%016llx\n",
656 emm_debug.u);
657 printf(" 21: rdsync_rst 0x%x\n",
658 emm_debug.s.rdsync_rst);
659 printf(" 20: emmc_clk_disable 0x%x\n",
660 emm_debug.s.emmc_clk_disable);
661 printf(" 19-16: dma_sm: 0x%x\n",
662 emm_debug.s.dma_sm);
663 printf(" 15-12: data_sm: 0x%x\n",
664 emm_debug.s.data_sm);
665 printf(" 11-8: cmd_sm: 0x%x\n",
666 emm_debug.s.cmd_sm);
667 printf(" 0: clk_on: 0x%x\n",
668 emm_debug.s.clk_on);
669 }
670
671 puts("\n");
672}
673
674/**
675 * Print out all of the register values
676 *
677 * @param mmc MMC device
678 */
679static void octeontx_mmc_print_registers(struct mmc *mmc)
680{
681#ifdef DEBUG_REGISTERS
682 const int print = 1;
683#else
684 const int print = 0;
685#endif
686 if (print)
687 octeontx_mmc_print_registers2(mmc, mmc_to_host(mmc));
688}
689
690static const struct octeontx_sd_mods octeontx_cr_types[] = {
691{ {0, 0}, {0, 0}, {0, 0} }, /* CMD0 */
692{ {0, 3}, {0, 3}, {0, 0} }, /* CMD1 */
693{ {0, 2}, {0, 2}, {0, 0} }, /* CMD2 */
694{ {0, 1}, {0, 3}, {0, 0} }, /* CMD3 SD_CMD_SEND_RELATIVE_ADDR 0, 2 */
695{ {0, 0}, {0, 0}, {0, 0} }, /* CMD4 */
696{ {0, 1}, {0, 1}, {0, 0} }, /* CMD5 */
697{ {0, 1}, {1, 1}, {0, 1} }, /*
698 * CMD6 SD_CMD_SWITCH_FUNC 1,0
699 * (ACMD) SD_APP_SET_BUS_WIDTH
700 */
701{ {0, 1}, {0, 1}, {0, 0} }, /* CMD7 */
702{ {1, 1}, {0, 3}, {0, 0} }, /* CMD8 SD_CMD_SEND_IF_COND 1,2 */
703{ {0, 2}, {0, 2}, {0, 0} }, /* CMD9 */
704{ {0, 2}, {0, 2}, {0, 0} }, /* CMD10 */
705{ {1, 1}, {0, 1}, {1, 1} }, /* CMD11 SD_CMD_SWITCH_UHS18V 1,0 */
706{ {0, 1}, {0, 1}, {0, 0} }, /* CMD12 */
707{ {0, 1}, {0, 1}, {1, 3} }, /* CMD13 (ACMD)) SD_CMD_APP_SD_STATUS 1,2 */
708{ {1, 1}, {1, 1}, {0, 0} }, /* CMD14 */
709{ {0, 0}, {0, 0}, {0, 0} }, /* CMD15 */
710{ {0, 1}, {0, 1}, {0, 0} }, /* CMD16 */
711{ {1, 1}, {1, 1}, {0, 0} }, /* CMD17 */
712{ {1, 1}, {1, 1}, {0, 0} }, /* CMD18 */
713{ {3, 1}, {3, 1}, {0, 0} }, /* CMD19 */
714{ {2, 1}, {0, 0}, {0, 0} }, /* CMD20 */ /* SD 2,0 */
715{ {0, 0}, {0, 0}, {0, 0} }, /* CMD21 */
716{ {0, 0}, {0, 0}, {1, 1} }, /* CMD22 (ACMD) SD_APP_SEND_NUM_WR_BLKS 1,0 */
717{ {0, 1}, {0, 1}, {0, 1} }, /* CMD23 */ /* SD ACMD 1,0 */
718{ {2, 1}, {2, 1}, {2, 1} }, /* CMD24 */
719{ {2, 1}, {2, 1}, {2, 1} }, /* CMD25 */
720{ {2, 1}, {2, 1}, {2, 1} }, /* CMD26 */
721{ {2, 1}, {2, 1}, {2, 1} }, /* CMD27 */
722{ {0, 1}, {0, 1}, {0, 1} }, /* CMD28 */
723{ {0, 1}, {0, 1}, {0, 1} }, /* CMD29 */
724{ {1, 1}, {1, 1}, {1, 1} }, /* CMD30 */
725{ {1, 1}, {1, 1}, {1, 1} }, /* CMD31 */
726{ {0, 0}, {0, 1}, {0, 0} }, /* CMD32 SD_CMD_ERASE_WR_BLK_START 0,1 */
727{ {0, 0}, {0, 1}, {0, 0} }, /* CMD33 SD_CMD_ERASE_WR_BLK_END 0,1 */
728{ {0, 0}, {0, 0}, {0, 0} }, /* CMD34 */
729{ {0, 1}, {0, 1}, {0, 1} }, /* CMD35 */
730{ {0, 1}, {0, 1}, {0, 1} }, /* CMD36 */
731{ {0, 0}, {0, 0}, {0, 0} }, /* CMD37 */
732{ {0, 1}, {0, 1}, {0, 1} }, /* CMD38 */
733{ {0, 4}, {0, 4}, {0, 4} }, /* CMD39 */
734{ {0, 5}, {0, 5}, {0, 5} }, /* CMD40 */
735{ {0, 0}, {0, 0}, {0, 3} }, /* CMD41 (ACMD) SD_CMD_APP_SEND_OP_COND 0,3 */
736{ {2, 1}, {2, 1}, {2, 1} }, /* CMD42 */
737{ {0, 0}, {0, 0}, {0, 0} }, /* CMD43 */
738{ {0, 0}, {0, 0}, {0, 0} }, /* CMD44 */
739{ {0, 0}, {0, 0}, {0, 0} }, /* CMD45 */
740{ {0, 0}, {0, 0}, {0, 0} }, /* CMD46 */
741{ {0, 0}, {0, 0}, {0, 0} }, /* CMD47 */
742{ {0, 0}, {1, 0}, {0, 0} }, /* CMD48 SD_CMD_READ_EXTR_SINGLE */
743{ {0, 0}, {2, 0}, {0, 0} }, /* CMD49 SD_CMD_WRITE_EXTR_SINGLE */
744{ {0, 0}, {0, 0}, {0, 0} }, /* CMD50 */
745{ {0, 0}, {0, 0}, {1, 1} }, /* CMD51 (ACMD) SD_CMD_APP_SEND_SCR 1,1 */
746{ {0, 0}, {0, 0}, {0, 0} }, /* CMD52 */
747{ {0, 0}, {0, 0}, {0, 0} }, /* CMD53 */
748{ {0, 0}, {0, 0}, {0, 0} }, /* CMD54 */
749{ {0, 1}, {0, 1}, {0, 1} }, /* CMD55 */
750{ {0xff, 0xff}, {0xff, 0xff}, {0xff, 0xff} }, /* CMD56 */
751{ {0, 0}, {0, 0}, {0, 0} }, /* CMD57 */
752{ {0, 0}, {0, 3}, {0, 3} }, /* CMD58 SD_CMD_SPI_READ_OCR 0,3 */
753{ {0, 0}, {0, 1}, {0, 0} }, /* CMD59 SD_CMD_SPI_CRC_ON_OFF 0,1 */
754{ {0, 0}, {0, 0}, {0, 0} }, /* CMD60 */
755{ {0, 0}, {0, 0}, {0, 0} }, /* CMD61 */
756{ {0, 0}, {0, 0}, {0, 0} }, /* CMD62 */
757{ {0, 0}, {0, 0}, {0, 0} } /* CMD63 */
758};
759
760/**
761 * Returns XOR values needed for SD commands and other quirks
762 *
763 * @param mmc mmc device
764 * @param cmd command information
765 *
766 * @return octeontx_mmc_cr_mods data structure with various quirks and flags
767 */
768static struct octeontx_mmc_cr_mods
769octeontx_mmc_get_cr_mods(struct mmc *mmc, const struct mmc_cmd *cmd,
770 const struct mmc_data *data)
771{
772 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
773 struct octeontx_mmc_cr_mods cr = {0, 0};
774 const struct octeontx_sd_mods *sdm =
775 &octeontx_cr_types[cmd->cmdidx & 0x3f];
776 u8 c = sdm->mmc.c, r = sdm->mmc.r;
777 u8 desired_ctype = 0;
778
779 if (IS_MMC(mmc)) {
780#ifdef MMC_SUPPORTS_TUNING
781 if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
782 if (cmd->resp_type == MMC_RSP_R1)
783 cr.rtype_xor = 1;
784 if (data && data->flags & MMC_DATA_READ)
785 cr.ctype_xor = 1;
786 }
787#endif
788 return cr;
789 }
790
791 if (cmd->cmdidx == 56)
792 c = (cmd->cmdarg & 1) ? 1 : 2;
793
794 if (data) {
795 if (data->flags & MMC_DATA_READ)
796 desired_ctype = 1;
797 else if (data->flags & MMC_DATA_WRITE)
798 desired_ctype = 2;
799 }
800
801 cr.ctype_xor = c ^ desired_ctype;
802 if (slot->is_acmd)
803 cr.rtype_xor = r ^ sdm->sdacmd.r;
804 else
805 cr.rtype_xor = r ^ sdm->sd.r;
806
807 debug("%s(%s): mmc c: %d, mmc r: %d, desired c: %d, xor c: %d, xor r: %d\n",
808 __func__, mmc->dev->name, c, r, desired_ctype,
809 cr.ctype_xor, cr.rtype_xor);
810 return cr;
811}
812
813/**
814 * Keep track of switch commands internally
815 */
816static void octeontx_mmc_track_switch(struct mmc *mmc, u32 cmd_arg)
817{
818 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
819 u8 how = (cmd_arg >> 24) & 3;
820 u8 where = (u8)(cmd_arg >> 16);
821 u8 val = (u8)(cmd_arg >> 8);
822
823 slot->want_switch = slot->cached_switch;
824
825 if (slot->is_acmd)
826 return;
827
828 if (how != 3)
829 return;
830
831 switch (where) {
832 case EXT_CSD_BUS_WIDTH:
833 slot->want_switch.s.bus_width = val;
834 break;
835 case EXT_CSD_POWER_CLASS:
836 slot->want_switch.s.power_class = val;
837 break;
838 case EXT_CSD_HS_TIMING:
839 slot->want_switch.s.hs_timing = 0;
840 slot->want_switch.s.hs200_timing = 0;
841 slot->want_switch.s.hs400_timing = 0;
842 switch (val & 0xf) {
843 case 0:
844 break;
845 case 1:
846 slot->want_switch.s.hs_timing = 1;
847 break;
848 case 2:
849 if (!slot->is_asim && !slot->is_emul)
850 slot->want_switch.s.hs200_timing = 1;
851 break;
852 case 3:
853 if (!slot->is_asim && !slot->is_emul)
854 slot->want_switch.s.hs400_timing = 1;
855 break;
856 default:
857 pr_err("%s(%s): Unsupported timing mode 0x%x\n",
858 __func__, mmc->dev->name, val & 0xf);
859 break;
860 }
861 break;
862 default:
863 break;
864 }
865}
866
867static int octeontx_mmc_print_rsp_errors(struct mmc *mmc,
868 union mio_emm_rsp_sts rsp_sts)
869{
870 bool err = false;
871 const char *name = mmc->dev->name;
872
873 if (rsp_sts.s.acc_timeout) {
874 pr_warn("%s(%s): acc_timeout\n", __func__, name);
875 err = true;
876 }
877 if (rsp_sts.s.dbuf_err) {
878 pr_warn("%s(%s): dbuf_err\n", __func__, name);
879 err = true;
880 }
881 if (rsp_sts.s.blk_timeout) {
882 pr_warn("%s(%s): blk_timeout\n", __func__, name);
883 err = true;
884 }
885 if (rsp_sts.s.blk_crc_err) {
886 pr_warn("%s(%s): blk_crc_err\n", __func__, name);
887 err = true;
888 }
889 if (rsp_sts.s.stp_timeout) {
890 pr_warn("%s(%s): stp_timeout\n", __func__, name);
891 err = true;
892 }
893 if (rsp_sts.s.stp_crc_err) {
894 pr_warn("%s(%s): stp_crc_err\n", __func__, name);
895 err = true;
896 }
897 if (rsp_sts.s.stp_bad_sts) {
898 pr_warn("%s(%s): stp_bad_sts\n", __func__, name);
899 err = true;
900 }
901 if (err)
902 pr_warn(" rsp_sts: 0x%llx\n", rsp_sts.u);
903
904 return err ? -1 : 0;
905}
906
907/**
908 * Starts a DMA operation for block read/write
909 *
910 * @param mmc mmc device
911 * @param write true if write operation
912 * @param clear true to clear DMA operation
913 * @param adr source or destination DMA address
914 * @param size size in blocks
915 * @param timeout timeout in ms
916 */
917static void octeontx_mmc_start_dma(struct mmc *mmc, bool write,
918 bool clear, u32 block, dma_addr_t adr,
919 u32 size, int timeout)
920{
921 const struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
922 union mio_emm_dma_cfg emm_dma_cfg;
923 union mio_emm_dma_adr emm_dma_adr;
924 union mio_emm_dma emm_dma;
925
926 /* Clear any interrupts */
927 write_csr(mmc, MIO_EMM_DMA_INT(),
928 read_csr(mmc, MIO_EMM_DMA_INT()));
929
930 emm_dma_cfg.u = 0;
931 emm_dma_cfg.s.en = 1;
932 emm_dma_cfg.s.rw = !!write;
933 emm_dma_cfg.s.clr = !!clear;
934 emm_dma_cfg.s.size = ((u64)(size * mmc->read_bl_len) / 8) - 1;
935#if __BYTE_ORDER != __BIG_ENDIAN
936 emm_dma_cfg.s.endian = 1;
937#endif
938 emm_dma_adr.u = 0;
939 emm_dma_adr.s.adr = adr;
940 write_csr(mmc, MIO_EMM_DMA_ADR(), emm_dma_adr.u);
941 write_csr(mmc, MIO_EMM_DMA_CFG(), emm_dma_cfg.u);
942
943 emm_dma.u = 0;
944 emm_dma.s.bus_id = slot->bus_id;
945 emm_dma.s.dma_val = 1;
946 emm_dma.s.rw = !!write;
947 emm_dma.s.sector = mmc->high_capacity ? 1 : 0;
948
949 if (size > 1 && ((IS_SD(mmc) && (mmc->scr[0] & 2)) || !IS_SD(mmc)))
950 emm_dma.s.multi = 1;
951 else
952 emm_dma.s.multi = 0;
953
954 emm_dma.s.block_cnt = size;
955 if (!mmc->high_capacity)
956 block *= mmc->read_bl_len;
957 emm_dma.s.card_addr = block;
958 debug("%s(%s): card address: 0x%x, size: %d, multi: %d\n",
959 __func__, mmc->dev->name, block, size, emm_dma.s.multi);
960
961 if (timeout > 0)
962 timeout = (timeout * 1000) - 1000;
963 set_wdog(mmc, timeout);
964
965 debug(" Writing 0x%llx to mio_emm_dma\n", emm_dma.u);
966 write_csr(mmc, MIO_EMM_DMA(), emm_dma.u);
967}
968
969/**
970 * Waits for a DMA operation to complete
971 *
972 * @param mmc mmc device
973 * @param timeout timeout in ms
974 *
975 * @return 0 for success (could be DMA errors), -ETIMEDOUT on timeout
976 */
977
978/**
979 * Cleanup DMA engine after a failure
980 *
981 * @param mmc mmc device
982 * @param rsp_sts rsp status
983 */
984static void octeontx_mmc_cleanup_dma(struct mmc *mmc,
985 union mio_emm_rsp_sts rsp_sts)
986{
987 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
988 union mio_emm_dma emm_dma;
989 ulong start;
990 int retries = 3;
991
992 do {
993 debug("%s(%s): rsp_sts: 0x%llx, rsp_lo: 0x%llx, dma_int: 0x%llx\n",
994 __func__, mmc->dev->name, rsp_sts.u,
995 read_csr(mmc, MIO_EMM_RSP_LO()),
996 read_csr(mmc, MIO_EMM_DMA_INT()));
997 emm_dma.u = read_csr(mmc, MIO_EMM_DMA());
998 emm_dma.s.dma_val = 1;
999 emm_dma.s.dat_null = 1;
1000 emm_dma.s.bus_id = slot->bus_id;
1001 write_csr(mmc, MIO_EMM_DMA(), emm_dma.u);
1002 start = get_timer(0);
1003 do {
1004 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1005 WATCHDOG_RESET();
1006 } while (get_timer(start) < 100 &&
1007 (rsp_sts.s.dma_val || rsp_sts.s.dma_pend));
1008 } while (retries-- >= 0 && rsp_sts.s.dma_pend);
1009 if (rsp_sts.s.dma_val)
1010 pr_err("%s(%s): Error: could not clean up DMA. RSP_STS: 0x%llx, RSP_LO: 0x%llx\n",
1011 __func__, mmc->dev->name, rsp_sts.u,
1012 read_csr(mmc, MIO_EMM_RSP_LO()));
1013 debug(" rsp_sts after clearing up DMA: 0x%llx\n",
1014 read_csr(mmc, MIO_EMM_RSP_STS()));
1015}
1016
1017/**
1018 * Waits for a DMA operation to complete
1019 *
1020 * @param mmc mmc device
1021 * @param timeout timeout in ms
1022 * @param verbose true to print out error information
1023 *
1024 * @return 0 for success (could be DMA errors), -ETIMEDOUT on timeout
1025 * or -EIO if IO error.
1026 */
1027static int octeontx_mmc_wait_dma(struct mmc *mmc, bool write, ulong timeout,
1028 bool verbose)
1029{
1030 struct octeontx_mmc_host *host = mmc_to_host(mmc);
1031 ulong start_time = get_timer(0);
1032 union mio_emm_dma_int emm_dma_int;
1033 union mio_emm_rsp_sts rsp_sts;
1034 union mio_emm_dma emm_dma;
1035 bool timed_out = false;
1036 bool err = false;
1037
1038 debug("%s(%s, %lu, %d), delay: %uus\n", __func__, mmc->dev->name,
1039 timeout, verbose, host->dma_wait_delay);
1040
1041 udelay(host->dma_wait_delay);
1042 do {
1043 emm_dma_int.u = read_csr(mmc, MIO_EMM_DMA_INT());
1044 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1045 if (write) {
1046 if ((rsp_sts.s.dma_pend && !rsp_sts.s.dma_val) ||
1047 rsp_sts.s.blk_timeout ||
1048 rsp_sts.s.stp_timeout ||
1049 rsp_sts.s.rsp_timeout) {
1050 err = true;
1051#ifdef DEBUG
1052 debug("%s: f1\n", __func__);
1053 octeontx_mmc_print_rsp_errors(mmc, rsp_sts);
1054#endif
1055 break;
1056 }
1057 } else {
1058 if (rsp_sts.s.blk_crc_err ||
1059 (rsp_sts.s.dma_pend && !rsp_sts.s.dma_val)) {
1060 err = true;
1061#if defined(DEBUG)
1062 octeontx_mmc_print_rsp_errors(mmc, rsp_sts);
1063#endif
1064 break;
1065 }
1066 }
1067 if (rsp_sts.s.dma_pend) {
1068 /*
1069 * If this is set then an error has occurred.
1070 * Try and restart the DMA operation.
1071 */
1072 emm_dma.u = read_csr(mmc, MIO_EMM_DMA());
1073 if (verbose) {
1074 pr_err("%s(%s): DMA pending error: rsp_sts: 0x%llx, dma_int: 0x%llx, emm_dma: 0x%llx\n",
1075 __func__, mmc->dev->name, rsp_sts.u,
1076 emm_dma_int.u, emm_dma.u);
1077 octeontx_print_rsp_sts(mmc);
1078 debug(" MIO_EMM_DEBUG: 0x%llx\n",
1079 read_csr(mmc, MIO_EMM_DEBUG()));
1080 pr_err("%s: Trying DMA resume...\n", __func__);
1081 }
1082 emm_dma.s.dma_val = 1;
1083 emm_dma.s.dat_null = 1;
1084 write_csr(mmc, MIO_EMM_DMA(), emm_dma.u);
1085 udelay(10);
1086 } else if (!rsp_sts.s.dma_val && emm_dma_int.s.done) {
1087 break;
1088 }
1089 WATCHDOG_RESET();
1090 timed_out = (get_timer(start_time) > timeout);
1091 } while (!timed_out);
1092
1093 if (timed_out || err) {
1094 if (verbose) {
1095 pr_err("%s(%s): MMC DMA %s after %lu ms, rsp_sts: 0x%llx, dma_int: 0x%llx, rsp_sts_lo: 0x%llx, emm_dma: 0x%llx\n",
1096 __func__, mmc->dev->name,
1097 timed_out ? "timed out" : "error",
1098 get_timer(start_time), rsp_sts.u,
1099 emm_dma_int.u,
1100 read_csr(mmc, MIO_EMM_RSP_LO()),
1101 read_csr(mmc, MIO_EMM_DMA()));
1102 octeontx_print_rsp_sts(mmc);
1103 }
1104 if (rsp_sts.s.dma_pend)
1105 octeontx_mmc_cleanup_dma(mmc, rsp_sts);
1106 } else {
1107 write_csr(mmc, MIO_EMM_DMA_INT(),
1108 read_csr(mmc, MIO_EMM_DMA_INT()));
1109 }
1110
1111 return timed_out ? -ETIMEDOUT : (err ? -EIO : 0);
1112}
1113
1114/**
1115 * Read blocks from the MMC/SD device
1116 *
1117 * @param mmc mmc device
1118 * @param cmd command
1119 * @param data data for read
1120 * @param verbose true to print out error information
1121 *
1122 * @return number of blocks read or 0 if error
1123 */
1124static int octeontx_mmc_read_blocks(struct mmc *mmc, struct mmc_cmd *cmd,
1125 struct mmc_data *data, bool verbose)
1126{
1127 struct octeontx_mmc_host *host = mmc_to_host(mmc);
1128 union mio_emm_rsp_sts rsp_sts;
1129 dma_addr_t dma_addr = (dma_addr_t)dm_pci_virt_to_mem(host->dev,
1130 data->dest);
1131 ulong count;
1132 ulong blkcnt = data->blocks;
1133 ulong start = cmd->cmdarg;
1134 int timeout = 1000 + blkcnt * 20;
1135 bool timed_out = false;
1136 bool multi_xfer = cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK;
1137
1138 debug("%s(%s): dest: %p, dma address: 0x%llx, blkcnt: %lu, start: %lu\n",
1139 __func__, mmc->dev->name, data->dest, dma_addr, blkcnt, start);
1140 debug("%s: rsp_sts: 0x%llx\n", __func__,
1141 read_csr(mmc, MIO_EMM_RSP_STS()));
1142 /* use max timeout for multi-block transfers */
1143 /* timeout = 0; */
1144
1145 /*
1146 * If we have a valid SD card in the slot, we set the response bit
1147 * mask to check for CRC errors and timeouts only.
1148 * Otherwise, use the default power on reset value.
1149 */
1150 write_csr(mmc, MIO_EMM_STS_MASK(),
1151 IS_SD(mmc) ? 0x00b00000ull : 0xe4390080ull);
1152 invalidate_dcache_range((u64)data->dest,
1153 (u64)data->dest + blkcnt * data->blocksize);
1154
1155 if (multi_xfer) {
1156 octeontx_mmc_start_dma(mmc, false, false, start, dma_addr,
1157 blkcnt, timeout);
1158 timed_out = !!octeontx_mmc_wait_dma(mmc, false, timeout,
1159 verbose);
1160 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1161 if (timed_out || rsp_sts.s.dma_val || rsp_sts.s.dma_pend) {
1162 if (!verbose)
1163 return 0;
1164
1165 pr_err("%s(%s): Error: DMA timed out. rsp_sts: 0x%llx, emm_int: 0x%llx, dma_int: 0x%llx, rsp_lo: 0x%llx\n",
1166 __func__, mmc->dev->name, rsp_sts.u,
1167 read_csr(mmc, MIO_EMM_INT()),
1168 read_csr(mmc, MIO_EMM_DMA_INT()),
1169 read_csr(mmc, MIO_EMM_RSP_LO()));
1170 pr_err("%s: block count: %lu, start: 0x%lx\n",
1171 __func__, blkcnt, start);
1172 octeontx_mmc_print_registers(mmc);
1173 return 0;
1174 }
1175 } else {
1176 count = blkcnt;
1177 timeout = 1000;
1178 do {
1179 octeontx_mmc_start_dma(mmc, false, false, start,
1180 dma_addr, 1, timeout);
1181 dma_addr += mmc->read_bl_len;
1182 start++;
1183
1184 timed_out = !!octeontx_mmc_wait_dma(mmc, false,
1185 timeout, verbose);
1186 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1187 if (timed_out || rsp_sts.s.dma_val ||
1188 rsp_sts.s.dma_pend) {
1189 if (verbose) {
1190 pr_err("%s: Error: DMA timed out. rsp_sts: 0x%llx, emm_int: 0x%llx, dma_int: 0x%llx, rsp_lo: 0x%llx\n",
1191 __func__, rsp_sts.u,
1192 read_csr(mmc, MIO_EMM_INT()),
1193 read_csr(mmc, MIO_EMM_DMA_INT()),
1194 read_csr(mmc, MIO_EMM_RSP_LO()));
1195 pr_err("%s: block count: 1, start: 0x%lx\n",
1196 __func__, start);
1197 octeontx_mmc_print_registers(mmc);
1198 }
1199 return blkcnt - count;
1200 }
1201 WATCHDOG_RESET();
1202 } while (--count);
1203 }
1204#ifdef DEBUG
1205 debug("%s(%s): Read %lu (0x%lx) blocks starting at block %u (0x%x) to address %p (dma address 0x%llx)\n",
1206 __func__, mmc->dev->name, blkcnt, blkcnt,
1207 cmd->cmdarg, cmd->cmdarg, data->dest,
1208 dm_pci_virt_to_mem(host->dev, data->dest));
1209 print_buffer(0, data->dest, 1, 0x200, 0);
1210#endif
1211 return blkcnt;
1212}
1213
1214static int octeontx_mmc_poll_ready(struct mmc *mmc, ulong timeout)
1215{
1216 ulong start;
1217 struct mmc_cmd cmd;
1218 int err;
1219 bool not_ready = false;
1220
1221 memset(&cmd, 0, sizeof(cmd));
1222 cmd.cmdidx = MMC_CMD_SEND_STATUS;
1223 cmd.cmdarg = mmc->rca << 16;
1224 cmd.resp_type = MMC_RSP_R1;
1225 start = get_timer(0);
1226 do {
1227 err = octeontx_mmc_send_cmd(mmc, &cmd, NULL);
1228 if (err) {
1229 pr_err("%s(%s): MMC command error: %d; Retry...\n",
1230 __func__, mmc->dev->name, err);
1231 not_ready = true;
1232 } else if (cmd.response[0] & R1_READY_FOR_DATA) {
1233 return 0;
1234 }
1235 WATCHDOG_RESET();
1236 } while (get_timer(start) < timeout);
1237
1238 if (not_ready)
1239 pr_err("%s(%s): MMC command error; Retry timeout\n",
1240 __func__, mmc->dev->name);
1241 return -ETIMEDOUT;
1242}
1243
1244static ulong octeontx_mmc_write_blocks(struct mmc *mmc, struct mmc_cmd *cmd,
1245 struct mmc_data *data)
1246{
1247 struct octeontx_mmc_host *host = mmc_to_host(mmc);
1248 ulong start = cmd->cmdarg;
1249 ulong blkcnt = data->blocks;
1250 dma_addr_t dma_addr;
1251 union mio_emm_rsp_sts rsp_sts;
1252 union mio_emm_sts_mask emm_sts_mask;
1253 ulong timeout;
1254 int count;
1255 bool timed_out = false;
1256 bool multi_xfer = (blkcnt > 1) &&
1257 ((IS_SD(mmc) && mmc->scr[0] & 2) || !IS_SD(mmc));
1258
1259 octeontx_mmc_switch_to(mmc);
1260 emm_sts_mask.u = 0;
1261 emm_sts_mask.s.sts_msk = R1_BLOCK_WRITE_MASK;
1262 write_csr(mmc, MIO_EMM_STS_MASK(), emm_sts_mask.u);
1263
1264 if (octeontx_mmc_poll_ready(mmc, 10000)) {
1265 pr_err("%s(%s): Ready timed out\n", __func__, mmc->dev->name);
1266 return 0;
1267 }
1268 flush_dcache_range((u64)data->src,
1269 (u64)data->src + blkcnt * mmc->write_bl_len);
1270 dma_addr = (u64)dm_pci_virt_to_mem(host->dev, (void *)data->src);
1271 if (multi_xfer) {
1272 timeout = 5000 + 100 * blkcnt;
1273 octeontx_mmc_start_dma(mmc, true, false, start, dma_addr,
1274 blkcnt, timeout);
1275 timed_out = !!octeontx_mmc_wait_dma(mmc, true, timeout, true);
1276 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1277 if (timed_out || rsp_sts.s.dma_val || rsp_sts.s.dma_pend) {
1278 pr_err("%s(%s): Error: multi-DMA timed out after %lums. rsp_sts: 0x%llx, emm_int: 0x%llx, emm_dma_int: 0x%llx, rsp_sts_lo: 0x%llx, emm_dma: 0x%llx\n",
1279 __func__, mmc->dev->name, timeout,
1280 rsp_sts.u,
1281 read_csr(mmc, MIO_EMM_INT()),
1282 read_csr(mmc, MIO_EMM_DMA_INT()),
1283 read_csr(mmc, MIO_EMM_RSP_LO()),
1284 read_csr(mmc, MIO_EMM_DMA()));
1285 return 0;
1286 }
1287 } else {
1288 timeout = 5000;
1289 count = blkcnt;
1290 do {
1291 octeontx_mmc_start_dma(mmc, true, false, start,
1292 dma_addr, 1, timeout);
1293 dma_addr += mmc->read_bl_len;
1294 start++;
1295
1296 timed_out = !!octeontx_mmc_wait_dma(mmc, true, timeout,
1297 true);
1298 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1299 if (timed_out || rsp_sts.s.dma_val ||
1300 rsp_sts.s.dma_pend) {
1301 pr_err("%s(%s): Error: single-DMA timed out after %lums. rsp_sts: 0x%llx, emm_int: 0x%llx, emm_dma_int: 0x%llx, rsp_sts_lo: 0x%llx, emm_dma: 0x%llx\n",
1302 __func__, mmc->dev->name, timeout,
1303 rsp_sts.u,
1304 read_csr(mmc, MIO_EMM_RSP_STS()),
1305 read_csr(mmc, MIO_EMM_DMA_INT()),
1306 read_csr(mmc, MIO_EMM_RSP_LO()),
1307 read_csr(mmc, MIO_EMM_DMA()));
1308 return blkcnt - count;
1309 }
1310 WATCHDOG_RESET();
1311 } while (--count);
1312 }
1313
1314 return blkcnt;
1315}
1316
1317/**
1318 * Send a command to the eMMC/SD device
1319 *
1320 * @param mmc mmc device
1321 * @param cmd cmd to send and response
1322 * @param data additional data
1323 * @param flags
1324 * @return 0 for success, otherwise error
1325 */
1326static int octeontx_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1327 struct mmc_data *data)
1328{
1329 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
1330 const char *name = slot->dev->name;
1331 struct octeontx_mmc_cr_mods mods = {0, 0};
1332 union mio_emm_rsp_sts rsp_sts;
1333 union mio_emm_cmd emm_cmd;
1334 union mio_emm_rsp_lo rsp_lo;
1335 union mio_emm_buf_idx emm_buf_idx;
1336 union mio_emm_buf_dat emm_buf_dat;
1337 ulong start;
1338 int i;
1339 ulong blkcnt;
1340
1341 /**
1342 * This constant has a 1 bit for each command which should have a short
1343 * timeout and a 0 for each bit with a long timeout. Currently the
1344 * following commands have a long timeout:
1345 * CMD6, CMD17, CMD18, CMD24, CMD25, CMD32, CMD33, CMD35, CMD36 and
1346 * CMD38.
1347 */
1348 static const u64 timeout_short = 0xFFFFFFA4FCF9FFDFull;
1349 uint timeout;
1350
1351 if (cmd->cmdidx == MMC_CMD_SEND_EXT_CSD) {
1352 union mio_emm_rca emm_rca;
1353
1354 emm_rca.u = 0;
1355 emm_rca.s.card_rca = mmc->rca;
1356 write_csr(mmc, MIO_EMM_RCA(), emm_rca.u);
1357 }
1358
1359 if (timeout_short & (1ull << cmd->cmdidx))
1360 timeout = MMC_TIMEOUT_SHORT;
1361 else if (cmd->cmdidx == MMC_CMD_SWITCH && IS_SD(mmc))
1362 timeout = 2560;
1363 else if (cmd->cmdidx == MMC_CMD_ERASE)
1364 timeout = MMC_TIMEOUT_ERASE;
1365 else
1366 timeout = MMC_TIMEOUT_LONG;
1367
1368 debug("%s(%s): cmd idx: %u, arg: 0x%x, resp type: 0x%x, timeout: %u\n",
1369 __func__, name, cmd->cmdidx, cmd->cmdarg, cmd->resp_type,
1370 timeout);
1371 if (data)
1372 debug(" data: addr: %p, flags: 0x%x, blocks: %u, blocksize: %u\n",
1373 data->dest, data->flags, data->blocks, data->blocksize);
1374
1375 octeontx_mmc_switch_to(mmc);
1376
1377 /* Clear any interrupts */
1378 write_csr(mmc, MIO_EMM_INT(), read_csr(mmc, MIO_EMM_INT()));
1379
1380 /*
1381 * We need to override the default command types and response types
1382 * when dealing with SD cards.
1383 */
1384 mods = octeontx_mmc_get_cr_mods(mmc, cmd, data);
1385
1386 /* Handle block read/write/stop operations */
1387 switch (cmd->cmdidx) {
1388 case MMC_CMD_GO_IDLE_STATE:
1389 slot->tuned = false;
1390 slot->hs200_tuned = false;
1391 slot->hs400_tuned = false;
1392 break;
1393 case MMC_CMD_STOP_TRANSMISSION:
1394 return 0;
1395 case MMC_CMD_READ_MULTIPLE_BLOCK:
1396 case MMC_CMD_READ_SINGLE_BLOCK:
1397 pr_debug("%s(%s): Reading blocks\n", __func__, name);
1398 blkcnt = octeontx_mmc_read_blocks(mmc, cmd, data, true);
1399 return (blkcnt > 0) ? 0 : -1;
1400 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
1401 case MMC_CMD_WRITE_SINGLE_BLOCK:
1402 blkcnt = octeontx_mmc_write_blocks(mmc, cmd, data);
1403 return (blkcnt > 0) ? 0 : -1;
1404 case MMC_CMD_SELECT_CARD:
1405 /* Set the RCA register (is it set automatically?) */
1406 if (IS_SD(mmc)) {
1407 union mio_emm_rca emm_rca;
1408
1409 emm_rca.u = 0;
1410 emm_rca.s.card_rca = (cmd->cmdarg >> 16);
1411 write_csr(mmc, MIO_EMM_RCA(), emm_rca.u);
1412 debug("%s: Set SD relative address (RCA) to 0x%x\n",
1413 __func__, emm_rca.s.card_rca);
1414 }
1415 break;
1416
1417 case MMC_CMD_SWITCH:
1418 if (!data && !slot->is_acmd)
1419 octeontx_mmc_track_switch(mmc, cmd->cmdarg);
1420 break;
1421 }
1422
1423 emm_cmd.u = 0;
1424 emm_cmd.s.cmd_val = 1;
1425 emm_cmd.s.bus_id = slot->bus_id;
1426 emm_cmd.s.cmd_idx = cmd->cmdidx;
1427 emm_cmd.s.arg = cmd->cmdarg;
1428 emm_cmd.s.ctype_xor = mods.ctype_xor;
1429 emm_cmd.s.rtype_xor = mods.rtype_xor;
1430 if (data && data->blocks == 1 && data->blocksize != 512) {
1431 emm_cmd.s.offset =
1432 64 - ((data->blocks * data->blocksize) / 8);
1433 debug("%s: offset set to %u\n", __func__, emm_cmd.s.offset);
1434 }
1435
1436 if (data && data->flags & MMC_DATA_WRITE) {
1437 u8 *src = (u8 *)data->src;
1438
1439 if (!src) {
1440 pr_err("%s(%s): Error: data source for cmd 0x%x is NULL!\n",
1441 __func__, name, cmd->cmdidx);
1442 return -1;
1443 }
1444 if (data->blocksize > 512) {
1445 pr_err("%s(%s): Error: data for cmd 0x%x exceeds 512 bytes\n",
1446 __func__, name, cmd->cmdidx);
1447 return -1;
1448 }
1449#ifdef DEBUG
1450 debug("%s: Sending %d bytes data\n", __func__, data->blocksize);
1451 print_buffer(0, src, 1, data->blocksize, 0);
1452#endif
1453 emm_buf_idx.u = 0;
1454 emm_buf_idx.s.inc = 1;
1455 write_csr(mmc, MIO_EMM_BUF_IDX(), emm_buf_idx.u);
1456 for (i = 0; i < (data->blocksize + 7) / 8; i++) {
1457 memcpy(&emm_buf_dat.u, src, sizeof(emm_buf_dat.u));
1458 write_csr(mmc, MIO_EMM_BUF_DAT(),
1459 cpu_to_be64(emm_buf_dat.u));
1460 src += sizeof(emm_buf_dat.u);
1461 }
1462 write_csr(mmc, MIO_EMM_BUF_IDX(), 0);
1463 }
1464 debug("%s(%s): Sending command %u (emm_cmd: 0x%llx)\n", __func__,
1465 name, cmd->cmdidx, emm_cmd.u);
1466 set_wdog(mmc, timeout * 1000);
1467 write_csr(mmc, MIO_EMM_CMD(), emm_cmd.u);
1468
1469 /* Wait for command to finish or time out */
1470 start = get_timer(0);
1471 do {
1472 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
1473 WATCHDOG_RESET();
1474 } while (!rsp_sts.s.cmd_done && !rsp_sts.s.rsp_timeout &&
1475 (get_timer(start) < timeout + 10));
1476 octeontx_mmc_print_rsp_errors(mmc, rsp_sts);
1477 if (rsp_sts.s.rsp_timeout || !rsp_sts.s.cmd_done) {
1478 debug("%s(%s): Error: command %u(0x%x) timed out. rsp_sts: 0x%llx\n",
1479 __func__, name, cmd->cmdidx, cmd->cmdarg, rsp_sts.u);
1480 octeontx_mmc_print_registers(mmc);
1481 return -ETIMEDOUT;
1482 }
1483 if (rsp_sts.s.rsp_crc_err) {
1484 debug("%s(%s): RSP CRC error, rsp_sts: 0x%llx, cmdidx: %u, arg: 0x%08x\n",
1485 __func__, name, rsp_sts.u, cmd->cmdidx, cmd->cmdarg);
1486 octeontx_mmc_print_registers(mmc);
1487 return -1;
1488 }
1489 if (slot->bus_id != rsp_sts.s.bus_id) {
1490 pr_warn("%s(%s): bus id mismatch, got %d, expected %d for command 0x%x(0x%x)\n",
1491 __func__, name,
1492 rsp_sts.s.bus_id, slot->bus_id,
1493 cmd->cmdidx, cmd->cmdarg);
1494 goto error;
1495 }
1496 if (rsp_sts.s.rsp_bad_sts) {
1497 rsp_lo.u = read_csr(mmc, MIO_EMM_RSP_LO());
1498 debug("%s: Bad response for bus id %d, cmd id %d:\n"
1499 " rsp_timeout: %d\n"
1500 " rsp_bad_sts: %d\n"
1501 " rsp_crc_err: %d\n",
1502 __func__, slot->bus_id, cmd->cmdidx,
1503 rsp_sts.s.rsp_timeout,
1504 rsp_sts.s.rsp_bad_sts,
1505 rsp_sts.s.rsp_crc_err);
1506 if (rsp_sts.s.rsp_type == 1 && rsp_sts.s.rsp_bad_sts) {
1507 debug(" Response status: 0x%llx\n",
1508 (rsp_lo.u >> 8) & 0xffffffff);
1509#ifdef DEBUG
1510 mmc_print_status((rsp_lo.u >> 8) & 0xffffffff);
1511#endif
1512 }
1513 goto error;
1514 }
1515 if (rsp_sts.s.cmd_idx != cmd->cmdidx) {
1516 debug("%s(%s): Command response index %d does not match command index %d\n",
1517 __func__, name, rsp_sts.s.cmd_idx, cmd->cmdidx);
1518 octeontx_print_rsp_sts(mmc);
1519 debug("%s: rsp_lo: 0x%llx\n", __func__,
1520 read_csr(mmc, MIO_EMM_RSP_LO()));
1521
1522 goto error;
1523 }
1524
1525 slot->is_acmd = (cmd->cmdidx == MMC_CMD_APP_CMD);
1526
1527 if (!cmd->resp_type & MMC_RSP_PRESENT)
1528 debug(" Response type: 0x%x, no response expected\n",
1529 cmd->resp_type);
1530 /* Get the response if present */
1531 if (rsp_sts.s.rsp_val && (cmd->resp_type & MMC_RSP_PRESENT)) {
1532 union mio_emm_rsp_hi rsp_hi;
1533
1534 rsp_lo.u = read_csr(mmc, MIO_EMM_RSP_LO());
1535
1536 switch (rsp_sts.s.rsp_type) {
1537 case 1:
1538 case 3:
1539 case 4:
1540 case 5:
1541 cmd->response[0] = (rsp_lo.u >> 8) & 0xffffffffull;
1542 debug(" response: 0x%08x\n",
1543 cmd->response[0]);
1544 cmd->response[1] = 0;
1545 cmd->response[2] = 0;
1546 cmd->response[3] = 0;
1547 break;
1548 case 2:
1549 cmd->response[3] = rsp_lo.u & 0xffffffff;
1550 cmd->response[2] = (rsp_lo.u >> 32) & 0xffffffff;
1551 rsp_hi.u = read_csr(mmc, MIO_EMM_RSP_HI());
1552 cmd->response[1] = rsp_hi.u & 0xffffffff;
1553 cmd->response[0] = (rsp_hi.u >> 32) & 0xffffffff;
1554 debug(" response: 0x%08x 0x%08x 0x%08x 0x%08x\n",
1555 cmd->response[0], cmd->response[1],
1556 cmd->response[2], cmd->response[3]);
1557 break;
1558 default:
1559 pr_err("%s(%s): Unknown response type 0x%x for command %d, arg: 0x%x, rsp_sts: 0x%llx\n",
1560 __func__, name, rsp_sts.s.rsp_type, cmd->cmdidx,
1561 cmd->cmdarg, rsp_sts.u);
1562 return -1;
1563 }
1564 } else {
1565 debug(" Response not expected\n");
1566 }
1567
1568 if (data && data->flags & MMC_DATA_READ) {
1569 u8 *dest = (u8 *)data->dest;
1570
1571 if (!dest) {
1572 pr_err("%s(%s): Error, destination buffer NULL!\n",
1573 __func__, mmc->dev->name);
1574 goto error;
1575 }
1576 if (data->blocksize > 512) {
1577 printf("%s(%s): Error: data size %u exceeds 512\n",
1578 __func__, mmc->dev->name,
1579 data->blocksize);
1580 goto error;
1581 }
1582 emm_buf_idx.u = 0;
1583 emm_buf_idx.s.inc = 1;
1584 write_csr(mmc, MIO_EMM_BUF_IDX(), emm_buf_idx.u);
1585 for (i = 0; i < (data->blocksize + 7) / 8; i++) {
1586 emm_buf_dat.u = read_csr(mmc, MIO_EMM_BUF_DAT());
1587 emm_buf_dat.u = be64_to_cpu(emm_buf_dat.u);
1588 memcpy(dest, &emm_buf_dat.u, sizeof(emm_buf_dat.u));
1589 dest += sizeof(emm_buf_dat.u);
1590 }
1591 write_csr(mmc, MIO_EMM_BUF_IDX(), 0);
1592#ifdef DEBUG
1593 debug("%s: Received %d bytes data\n", __func__,
1594 data->blocksize);
1595 print_buffer(0, data->dest, 1, data->blocksize, 0);
1596#endif
1597 }
1598
1599 return 0;
1600error:
1601#ifdef DEBUG
1602 octeontx_mmc_print_registers(mmc);
1603#endif
1604 return -1;
1605}
1606
1607static int octeontx_mmc_dev_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1608 struct mmc_data *data)
1609{
1610 return octeontx_mmc_send_cmd(dev_to_mmc(dev), cmd, data);
1611}
1612
1613#ifdef MMC_SUPPORTS_TUNING
1614static int octeontx_mmc_test_cmd(struct mmc *mmc, u32 opcode, int *statp)
1615{
1616 struct mmc_cmd cmd;
1617 int err;
1618
1619 memset(&cmd, 0, sizeof(cmd));
1620
1621 debug("%s(%s, %u, %p)\n", __func__, mmc->dev->name, opcode, statp);
1622 cmd.cmdidx = opcode;
1623 cmd.resp_type = MMC_RSP_R1;
1624 cmd.cmdarg = mmc->rca << 16;
1625
1626 err = octeontx_mmc_send_cmd(mmc, &cmd, NULL);
1627 if (err)
1628 debug("%s(%s, %u) returned %d\n", __func__,
1629 mmc->dev->name, opcode, err);
1630 if (statp)
1631 *statp = cmd.response[0];
1632 return err;
1633}
1634
1635static int octeontx_mmc_test_get_ext_csd(struct mmc *mmc, u32 opcode,
1636 int *statp)
1637{
1638 struct mmc_cmd cmd;
1639 struct mmc_data data;
1640 int err;
1641 u8 ext_csd[MMC_MAX_BLOCK_LEN];
1642
1643 debug("%s(%s, %u, %p)\n", __func__, mmc->dev->name, opcode, statp);
1644 memset(&cmd, 0, sizeof(cmd));
1645
1646 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
1647 cmd.resp_type = MMC_RSP_R1;
1648 cmd.cmdarg = 0;
1649
1650 data.dest = (char *)ext_csd;
1651 data.blocks = 1;
1652 data.blocksize = MMC_MAX_BLOCK_LEN;
1653 data.flags = MMC_DATA_READ;
1654
1655 err = octeontx_mmc_send_cmd(mmc, &cmd, &data);
1656 if (statp)
1657 *statp = cmd.response[0];
1658
1659 return err;
1660}
1661
1662/**
1663 * Wrapper to set the MIO_EMM_TIMING register
1664 *
1665 * @param mmc pointer to mmc data structure
1666 * @param emm_timing New emm_timing register value
1667 *
1668 * On some devices it is possible that changing the data out value can
1669 * cause a glitch on an internal fifo. This works around this problem
1670 * by performing a soft-reset immediately before setting the timing register.
1671 *
1672 * Note: this function should not be called from any function that
1673 * performs DMA or block operations since not all registers are
1674 * preserved.
1675 */
1676static void octeontx_mmc_set_emm_timing(struct mmc *mmc,
1677 union mio_emm_timing emm_timing)
1678{
1679 union mio_emm_cfg emm_cfg;
1680 struct octeontx_mmc_slot *slot = mmc->priv;
1681 union mio_emm_debug emm_debug;
1682
1683 debug("%s(%s, 0x%llx) din: %u\n", __func__, mmc->dev->name,
1684 emm_timing.u, emm_timing.s.data_in_tap);
1685
1686 udelay(1);
1687 if (slot->host->tap_requires_noclk) {
1688 /* Turn off the clock */
1689 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
1690 emm_debug.s.emmc_clk_disable = 1;
1691 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
1692 udelay(1);
1693 emm_debug.s.rdsync_rst = 1;
1694 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
1695 }
1696 emm_cfg.u = read_csr(mmc, MIO_EMM_CFG());
1697 emm_cfg.s.bus_ena = 1 << 3;
1698 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
1699
1700 udelay(1);
1701 write_csr(mmc, MIO_EMM_TIMING(), emm_timing.u);
1702 udelay(1);
1703
1704 if (slot->host->tap_requires_noclk) {
1705 /* Turn on the clock */
1706 emm_debug.s.rdsync_rst = 0;
1707 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
1708 udelay(1);
1709 emm_debug.s.emmc_clk_disable = 0;
1710 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
1711 udelay(1);
1712 }
1713 emm_cfg.s.bus_ena = 1 << mmc_to_slot(mmc)->bus_id;
1714 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
1715}
1716
1717static const u8 octeontx_hs400_tuning_block[512] = {
1718 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
1719 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
1720 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
1721 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
1722 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
1723 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
1724 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
1725 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
1726 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
1727 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
1728 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
1729 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
1730 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
1731 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
1732 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
1733 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
1734 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
1735 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
1736 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
1737 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
1738 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
1739 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
1740 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
1741 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
1742 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
1743 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
1744 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
1745 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
1746 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
1747 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
1748 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
1749 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
1750 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
1751 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
1752 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
1753 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
1754 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
1755 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
1756 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
1757 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
1758 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
1759 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
1760 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
1761 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
1762 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
1763 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
1764 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
1765 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
1766 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00,
1767 0x00, 0xff, 0x00, 0xff, 0x55, 0xaa, 0x55, 0xaa,
1768 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
1769 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
1770 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
1771 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
1772 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
1773 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
1774 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00,
1775 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
1776 0x01, 0xfe, 0x01, 0xfe, 0xcc, 0xcc, 0xcc, 0xff,
1777 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
1778 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
1779 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
1780 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
1781 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
1782
1783};
1784
1785/**
1786 * Perform tuning in HS400 mode
1787 *
1788 * @param[in] mmc mmc data structure
1789 *
1790 * @ret 0 for success, otherwise error
1791 */
1792static int octeontx_tune_hs400(struct mmc *mmc)
1793{
1794 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
1795 struct mmc_cmd cmd;
1796 struct mmc_data data;
1797 union mio_emm_timing emm_timing;
1798 u8 buffer[mmc->read_bl_len];
1799 int tap_adj;
1800 int err = -1;
1801 int tap;
1802 int run = 0;
1803 int start_run = -1;
1804 int best_run = 0;
1805 int best_start = -1;
1806 bool prev_ok = false;
1807 char env_name[64];
1808 char how[MAX_NO_OF_TAPS + 1] = "";
1809
1810 if (slot->hs400_tuning_block == -1)
1811 return 0;
1812
1813 /* The eMMC standard disables all tuning support when operating in
1814 * DDR modes like HS400. The problem with this is that there are
1815 * many cases where the HS200 tuning does not work for HS400 mode.
1816 * In order to perform this tuning, while in HS200 a block is written
1817 * to a block specified in the device tree (marvell,hs400-tuning-block)
1818 * which is used for tuning in this function by repeatedly reading
1819 * this block and comparing the data and return code. This function
1820 * chooses the data input tap in the middle of the longest run of
1821 * successful read operations.
1822 */
1823
1824 emm_timing = slot->hs200_taps;
1825 debug("%s(%s): Start ci: %d, co: %d, di: %d, do: %d\n",
1826 __func__, mmc->dev->name, emm_timing.s.cmd_in_tap,
1827 emm_timing.s.cmd_out_tap, emm_timing.s.data_in_tap,
1828 emm_timing.s.data_out_tap);
1829 memset(buffer, 0xdb, sizeof(buffer));
1830
1831 snprintf(env_name, sizeof(env_name), "emmc%d_data_in_tap_hs400",
1832 slot->bus_id);
1833 tap = env_get_ulong(env_name, 10, -1L);
1834 if (tap >= 0 && tap < MAX_NO_OF_TAPS) {
1835 printf("Overriding data input tap for HS400 mode to %d\n", tap);
1836 emm_timing.s.data_in_tap = tap;
1837 octeontx_mmc_set_emm_timing(mmc, emm_timing);
1838 return 0;
1839 }
1840
1841 for (tap = 0; tap <= MAX_NO_OF_TAPS; tap++, prev_ok = !err) {
1842 if (tap < MAX_NO_OF_TAPS) {
1843 debug("%s: Testing data in tap %d\n", __func__, tap);
1844 emm_timing.s.data_in_tap = tap;
1845 octeontx_mmc_set_emm_timing(mmc, emm_timing);
1846
1847 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
1848 cmd.cmdarg = slot->hs400_tuning_block;
1849 cmd.resp_type = MMC_RSP_R1;
1850 data.dest = (void *)buffer;
1851 data.blocks = 1;
1852 data.blocksize = mmc->read_bl_len;
1853 data.flags = MMC_DATA_READ;
1854 err = !octeontx_mmc_read_blocks(mmc, &cmd, &data,
1855 false);
1856 if (err || memcmp(buffer, octeontx_hs400_tuning_block,
1857 sizeof(buffer))) {
1858#ifdef DEBUG
1859 if (!err) {
1860 debug("%s: data mismatch. Read:\n",
1861 __func__);
1862 print_buffer(0, buffer, 1,
1863 sizeof(buffer), 0);
1864 debug("\nExpected:\n");
1865 print_buffer(0,
1866 octeontx_hs400_tuning_block, 1,
1867 sizeof(octeontx_hs400_tuning_block),
1868 0);
1869 } else {
1870 debug("%s: Error %d reading block\n",
1871 __func__, err);
1872 }
1873#endif
1874 err = -EINVAL;
1875 } else {
1876 debug("%s: tap %d good\n", __func__, tap);
1877 }
1878 how[tap] = "-+"[!err];
1879 } else {
1880 err = -EINVAL;
1881 }
1882
1883 if (!err) {
1884 if (!prev_ok)
1885 start_run = tap;
1886 } else if (prev_ok) {
1887 run = tap - 1 - start_run;
1888 if (start_run >= 0 && run > best_run) {
1889 best_start = start_run;
1890 best_run = run;
1891 }
1892 }
1893 }
1894
1895 how[tap - 1] = '\0';
1896 if (best_start < 0) {
1897 printf("%s(%s): %lldMHz tuning failed for HS400\n",
1898 __func__, mmc->dev->name, slot->clock / 1000000);
1899 return -EINVAL;
1900 }
1901 tap = best_start + best_run / 2;
1902
1903 snprintf(env_name, sizeof(env_name), "emmc%d_data_in_tap_adj_hs400",
1904 slot->bus_id);
1905 tap_adj = env_get_ulong(env_name, 10, slot->hs400_tap_adj);
1906 /*
1907 * Keep it in range and if out of range force it back in with a small
1908 * buffer.
1909 */
1910 if (best_run > 3) {
1911 tap = tap + tap_adj;
1912 if (tap >= best_start + best_run)
1913 tap = best_start + best_run - 2;
1914 if (tap <= best_start)
1915 tap = best_start + 2;
1916 }
1917 how[tap] = '@';
1918 debug("Tuning: %s\n", how);
1919 debug("%s(%s): HS400 tap: best run start: %d, length: %d, tap: %d\n",
1920 __func__, mmc->dev->name, best_start, best_run, tap);
1921 slot->hs400_taps = slot->hs200_taps;
1922 slot->hs400_taps.s.data_in_tap = tap;
1923 slot->hs400_tuned = true;
1924 if (env_get_yesno("emmc_export_hs400_taps") > 0) {
1925 debug("%s(%s): Exporting HS400 taps\n",
1926 __func__, mmc->dev->name);
1927 env_set_ulong("emmc_timing_tap", slot->host->timing_taps);
1928 snprintf(env_name, sizeof(env_name),
1929 "emmc%d_hs400_data_in_tap_debug",
1930 slot->bus_id);
1931 env_set(env_name, how);
1932 snprintf(env_name, sizeof(env_name),
1933 "emmc%d_hs400_data_in_tap_val",
1934 slot->bus_id);
1935 env_set_ulong(env_name, tap);
1936 snprintf(env_name, sizeof(env_name),
1937 "emmc%d_hs400_data_in_tap_start",
1938 slot->bus_id);
1939 env_set_ulong(env_name, best_start);
1940 snprintf(env_name, sizeof(env_name),
1941 "emmc%d_hs400_data_in_tap_end",
1942 slot->bus_id);
1943 env_set_ulong(env_name, best_start + best_run);
1944 snprintf(env_name, sizeof(env_name),
1945 "emmc%d_hs400_cmd_in_tap",
1946 slot->bus_id);
1947 env_set_ulong(env_name, slot->hs400_taps.s.cmd_in_tap);
1948 snprintf(env_name, sizeof(env_name),
1949 "emmc%d_hs400_cmd_out_tap",
1950 slot->bus_id);
1951 env_set_ulong(env_name, slot->hs400_taps.s.cmd_out_tap);
1952 snprintf(env_name, sizeof(env_name),
1953 "emmc%d_hs400_cmd_out_delay",
1954 slot->bus_id);
1955 env_set_ulong(env_name, slot->cmd_out_hs400_delay);
1956 snprintf(env_name, sizeof(env_name),
1957 "emmc%d_hs400_data_out_tap",
1958 slot->bus_id);
1959 env_set_ulong(env_name, slot->hs400_taps.s.data_out_tap);
1960 snprintf(env_name, sizeof(env_name),
1961 "emmc%d_hs400_data_out_delay",
1962 slot->bus_id);
1963 env_set_ulong(env_name, slot->data_out_hs400_delay);
1964 } else {
1965 debug("%s(%s): HS400 environment export disabled\n",
1966 __func__, mmc->dev->name);
1967 }
1968 octeontx_mmc_set_timing(mmc);
1969
1970 return 0;
1971}
1972
1973struct adj {
1974 const char *name;
1975 u8 mask_shift;
1976 int (*test)(struct mmc *mmc, u32 opcode, int *error);
1977 u32 opcode;
1978 bool ddr_only;
1979 bool hs200_only;
1980 bool not_hs200_only;
1981 u8 num_runs;
1982};
1983
1984struct adj adj[] = {
1985 { "CMD_IN", 48, octeontx_mmc_test_cmd, MMC_CMD_SEND_STATUS,
1986 false, false, false, 2, },
1987/* { "CMD_OUT", 32, octeontx_mmc_test_cmd, MMC_CMD_SEND_STATUS, },*/
1988 { "DATA_IN(HS200)", 16, mmc_send_tuning,
1989 MMC_CMD_SEND_TUNING_BLOCK_HS200, false, true, false, 2, },
1990 { "DATA_IN", 16, octeontx_mmc_test_get_ext_csd, 0, false, false,
1991 true, 2, },
1992/* { "DATA_OUT", 0, octeontx_mmc_test_cmd, 0, true, false},*/
1993 { NULL, },
1994};
1995
1996/**
1997 * Perform tuning tests to find optimal timing
1998 *
1999 * @param mmc mmc device
2000 * @param adj parameter to tune
2001 * @param opcode command opcode to use
2002 *
2003 * @return 0 for success, -1 if tuning failed
2004 */
2005static int octeontx_mmc_adjust_tuning(struct mmc *mmc, struct adj *adj,
2006 u32 opcode)
2007{
2008 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2009 union mio_emm_timing timing;
2010 union mio_emm_debug emm_debug;
2011 int tap;
2012 int err = -1;
2013 int run = 0;
2014 int count;
2015 int start_run = -1;
2016 int best_run = 0;
2017 int best_start = -1;
2018 bool prev_ok = false;
2019 u64 tap_status = 0;
2020 const int tap_adj = slot->hs200_tap_adj;
2021 char how[MAX_NO_OF_TAPS + 1] = "";
2022 bool is_hs200 = mmc->selected_mode == MMC_HS_200;
2023
2024 debug("%s(%s, %s, %d), hs200: %d\n", __func__, mmc->dev->name,
2025 adj->name, opcode, is_hs200);
2026 octeontx_mmc_set_emm_timing(mmc,
2027 is_hs200 ? slot->hs200_taps : slot->taps);
2028
2029#ifdef DEBUG
2030 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
2031 printf("%s(%s): Before tuning %s, opcode: %d\n",
2032 __func__, mmc->dev->name, adj->name, opcode);
2033 octeontx_mmc_print_registers2(mmc, NULL);
2034 }
2035#endif
2036
2037 /*
2038 * The algorithm to find the optimal timing is to start
2039 * at the end and work backwards and select the second
2040 * value that passes. Each test is repeated twice.
2041 */
2042 for (tap = 0; tap <= MAX_NO_OF_TAPS; tap++, prev_ok = !err) {
2043 if (tap < MAX_NO_OF_TAPS) {
2044 if (slot->host->tap_requires_noclk) {
2045 /* Turn off the clock */
2046 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
2047 emm_debug.s.emmc_clk_disable = 1;
2048 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2049 udelay(1);
2050 emm_debug.s.rdsync_rst = 1;
2051 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2052 udelay(1);
2053 }
2054
2055 timing.u = read_csr(mmc, MIO_EMM_TIMING());
2056 timing.u &= ~(0x3full << adj->mask_shift);
2057 timing.u |= (u64)tap << adj->mask_shift;
2058 write_csr(mmc, MIO_EMM_TIMING(), timing.u);
2059 debug("%s(%s): Testing ci: %d, co: %d, di: %d, do: %d\n",
2060 __func__, mmc->dev->name, timing.s.cmd_in_tap,
2061 timing.s.cmd_out_tap, timing.s.data_in_tap,
2062 timing.s.data_out_tap);
2063
2064 if (slot->host->tap_requires_noclk) {
2065 /* Turn off the clock */
2066 emm_debug.s.rdsync_rst = 0;
2067 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2068 udelay(1);
2069 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
2070 emm_debug.s.emmc_clk_disable = 0;
2071 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2072 udelay(1);
2073 }
2074 for (count = 0; count < 2; count++) {
2075 err = adj->test(mmc, opcode, NULL);
2076 if (err) {
2077 debug("%s(%s, %s): tap %d failed, count: %d, rsp_sts: 0x%llx, rsp_lo: 0x%llx\n",
2078 __func__, mmc->dev->name,
2079 adj->name, tap, count,
2080 read_csr(mmc,
2081 MIO_EMM_RSP_STS()),
2082 read_csr(mmc,
2083 MIO_EMM_RSP_LO()));
2084 debug("%s(%s, %s): tap: %d, do: %d, di: %d, co: %d, ci: %d\n",
2085 __func__, mmc->dev->name,
2086 adj->name, tap,
2087 timing.s.data_out_tap,
2088 timing.s.data_in_tap,
2089 timing.s.cmd_out_tap,
2090 timing.s.cmd_in_tap);
2091 break;
2092 }
2093 debug("%s(%s, %s): tap %d passed, count: %d, rsp_sts: 0x%llx, rsp_lo: 0x%llx\n",
2094 __func__, mmc->dev->name, adj->name, tap,
2095 count,
2096 read_csr(mmc, MIO_EMM_RSP_STS()),
2097 read_csr(mmc, MIO_EMM_RSP_LO()));
2098 }
2099 tap_status |= (u64)(!err) << tap;
2100 how[tap] = "-+"[!err];
2101 } else {
2102 /*
2103 * Putting the end+1 case in the loop simplifies
2104 * logic, allowing 'prev_ok' to process a sweet
2105 * spot in tuning which extends to the wall.
2106 */
2107 err = -EINVAL;
2108 }
2109 if (!err) {
2110 /*
2111 * If no CRC/etc errors in the response, but previous
2112 * failed, note the start of a new run.
2113 */
2114 debug(" prev_ok: %d\n", prev_ok);
2115 if (!prev_ok)
2116 start_run = tap;
2117 } else if (prev_ok) {
2118 run = tap - 1 - start_run;
2119 /* did we just exit a wider sweet spot? */
2120 if (start_run >= 0 && run > best_run) {
2121 best_start = start_run;
2122 best_run = run;
2123 }
2124 }
2125 }
2126 how[tap - 1] = '\0';
2127 if (best_start < 0) {
2128 printf("%s(%s, %s): %lldMHz tuning %s failed\n", __func__,
2129 mmc->dev->name, adj->name, slot->clock / 1000000,
2130 adj->name);
2131 return -EINVAL;
2132 }
2133
2134 tap = best_start + best_run / 2;
2135 debug(" tap %d is center, start: %d, run: %d\n", tap,
2136 best_start, best_run);
2137 if (is_hs200) {
2138 slot->hs200_taps.u &= ~(0x3full << adj->mask_shift);
2139 slot->hs200_taps.u |= (u64)tap << adj->mask_shift;
2140 } else {
2141 slot->taps.u &= ~(0x3full << adj->mask_shift);
2142 slot->taps.u |= (u64)tap << adj->mask_shift;
2143 }
2144 if (best_start < 0) {
2145 printf("%s(%s, %s): %lldMHz tuning %s failed\n", __func__,
2146 mmc->dev->name, adj->name, slot->clock / 1000000,
2147 adj->name);
2148 return -EINVAL;
2149 }
2150
2151 tap = best_start + best_run / 2;
2152 if (is_hs200 && (tap + tap_adj >= 0) && (tap + tap_adj < 64) &&
2153 tap_status & (1ULL << (tap + tap_adj))) {
2154 debug("Adjusting tap from %d by %d to %d\n",
2155 tap, tap_adj, tap + tap_adj);
2156 tap += tap_adj;
2157 }
2158 how[tap] = '@';
2159 debug("%s/%s %d/%d/%d %s\n", mmc->dev->name,
2160 adj->name, best_start, tap, best_start + best_run, how);
2161
2162 if (is_hs200) {
2163 slot->hs200_taps.u &= ~(0x3full << adj->mask_shift);
2164 slot->hs200_taps.u |= (u64)tap << adj->mask_shift;
2165 } else {
2166 slot->taps.u &= ~(0x3full << adj->mask_shift);
2167 slot->taps.u |= (u64)tap << adj->mask_shift;
2168 }
2169
2170#ifdef DEBUG
2171 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
2172 debug("%s(%s, %s): After successful tuning\n",
2173 __func__, mmc->dev->name, adj->name);
2174 debug("%s(%s, %s): tap: %d, new do: %d, di: %d, co: %d, ci: %d\n",
2175 __func__, mmc->dev->name, adj->name, tap,
2176 slot->taps.s.data_out_tap,
2177 slot->taps.s.data_in_tap,
2178 slot->taps.s.cmd_out_tap,
2179 slot->taps.s.cmd_in_tap);
2180 debug("%s(%s, %s): tap: %d, new do HS200: %d, di: %d, co: %d, ci: %d\n",
2181 __func__, mmc->dev->name, adj->name, tap,
2182 slot->hs200_taps.s.data_out_tap,
2183 slot->hs200_taps.s.data_in_tap,
2184 slot->hs200_taps.s.cmd_out_tap,
2185 slot->hs200_taps.s.cmd_in_tap);
2186 }
2187#endif
2188 octeontx_mmc_set_timing(mmc);
2189
2190 if (is_hs200 && env_get_yesno("emmc_export_hs200_taps")) {
2191 char env_name[64];
2192
2193 env_set_ulong("emmc_timing_tap", slot->host->timing_taps);
2194 switch (opcode) {
2195 case MMC_CMD_SEND_TUNING_BLOCK:
2196 snprintf(env_name, sizeof(env_name),
2197 "emmc%d_hs200_data_in_tap_debug",
2198 slot->bus_id);
2199 env_set(env_name, how);
2200 snprintf(env_name, sizeof(env_name),
2201 "emmc%d_hs200_data_in_tap_val", slot->bus_id);
2202 env_set_ulong(env_name, tap);
2203 snprintf(env_name, sizeof(env_name),
2204 "emmc%d_hs200_data_in_tap_start",
2205 slot->bus_id);
2206 env_set_ulong(env_name, best_start);
2207 snprintf(env_name, sizeof(env_name),
2208 "emmc%d_hs200_data_in_tap_end",
2209 slot->bus_id);
2210 env_set_ulong(env_name, best_start + best_run);
2211 break;
2212 case MMC_CMD_SEND_STATUS:
2213 snprintf(env_name, sizeof(env_name),
2214 "emmc%d_hs200_cmd_in_tap_debug",
2215 slot->bus_id);
2216 env_set(env_name, how);
2217 snprintf(env_name, sizeof(env_name),
2218 "emmc%d_hs200_cmd_in_tap_val", slot->bus_id);
2219 env_set_ulong(env_name, tap);
2220 snprintf(env_name, sizeof(env_name),
2221 "emmc%d_hs200_cmd_in_tap_start",
2222 slot->bus_id);
2223 env_set_ulong(env_name, best_start);
2224 snprintf(env_name, sizeof(env_name),
2225 "emmc%d_hs200_cmd_in_tap_end",
2226 slot->bus_id);
2227 env_set_ulong(env_name, best_start + best_run);
2228 break;
2229 default:
2230 snprintf(env_name, sizeof(env_name),
2231 "emmc%d_hs200_data_out_tap", slot->bus_id);
2232 env_set_ulong(env_name, slot->data_out_hs200_delay);
2233 snprintf(env_name, sizeof(env_name),
2234 "emmc%d_hs200_cmd_out_tap", slot->bus_id);
2235 env_set_ulong(env_name, slot->cmd_out_hs200_delay);
2236 break;
2237 }
2238 }
2239
2240 return 0;
2241}
2242
2243static int octeontx_mmc_execute_tuning(struct udevice *dev, u32 opcode)
2244{
2245 struct mmc *mmc = dev_to_mmc(dev);
2246 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2247 union mio_emm_timing emm_timing;
2248 int err;
2249 struct adj *a;
2250 bool is_hs200;
2251 char env_name[64];
2252
2253 pr_info("%s re-tuning, opcode 0x%x\n", dev->name, opcode);
2254
2255 if (slot->is_asim || slot->is_emul)
2256 return 0;
2257
2258 is_hs200 = (mmc->selected_mode == MMC_HS_200);
2259 if (is_hs200) {
2260 slot->hs200_tuned = false;
2261 slot->hs400_tuned = false;
2262 } else {
2263 slot->tuned = false;
2264 }
2265 octeontx_mmc_set_output_bus_timing(mmc);
2266 octeontx_mmc_set_input_bus_timing(mmc);
2267 emm_timing.u = read_csr(mmc, MIO_EMM_TIMING());
2268 if (mmc->selected_mode == MMC_HS_200) {
2269 slot->hs200_taps.s.cmd_out_tap = emm_timing.s.cmd_out_tap;
2270 slot->hs200_taps.s.data_out_tap = emm_timing.s.data_out_tap;
2271 } else {
2272 slot->taps.s.cmd_out_tap = emm_timing.s.cmd_out_tap;
2273 slot->taps.s.data_out_tap = emm_timing.s.data_out_tap;
2274 }
2275 octeontx_mmc_set_input_bus_timing(mmc);
2276 octeontx_mmc_set_output_bus_timing(mmc);
2277
2278 for (a = adj; a->name; a++) {
2279 ulong in_tap;
2280
2281 if (!strcmp(a->name, "CMD_IN")) {
2282 snprintf(env_name, sizeof(env_name),
2283 "emmc%d_cmd_in_tap", slot->bus_id);
2284 in_tap = env_get_ulong(env_name, 10, (ulong)-1);
2285 if (in_tap != (ulong)-1) {
2286 if (mmc->selected_mode == MMC_HS_200 ||
2287 a->hs200_only) {
2288 slot->hs200_taps.s.cmd_in_tap = in_tap;
2289 slot->hs400_taps.s.cmd_in_tap = in_tap;
2290 } else {
2291 slot->taps.s.cmd_in_tap = in_tap;
2292 }
2293 continue;
2294 }
2295 } else if (a->hs200_only &&
2296 !strcmp(a->name, "DATA_IN(HS200)")) {
2297 snprintf(env_name, sizeof(env_name),
2298 "emmc%d_data_in_tap_hs200", slot->bus_id);
2299 in_tap = env_get_ulong(env_name, 10, (ulong)-1);
2300 if (in_tap != (ulong)-1) {
2301 debug("%s(%s): Overriding HS200 data in tap to %d\n",
2302 __func__, dev->name, (int)in_tap);
2303 slot->hs200_taps.s.data_in_tap = in_tap;
2304 continue;
2305 }
2306 } else if (!a->hs200_only && !strcmp(a->name, "DATA_IN")) {
2307 snprintf(env_name, sizeof(env_name),
2308 "emmc%d_data_in_tap", slot->bus_id);
2309 in_tap = env_get_ulong(env_name, 10, (ulong)-1);
2310 if (in_tap != (ulong)-1) {
2311 debug("%s(%s): Overriding non-HS200 data in tap to %d\n",
2312 __func__, dev->name, (int)in_tap);
2313 slot->taps.s.data_in_tap = in_tap;
2314 continue;
2315 }
2316 }
2317
2318 debug("%s(%s): Testing: %s, mode: %s, opcode: %u\n", __func__,
2319 dev->name, a->name, mmc_mode_name(mmc->selected_mode),
2320 opcode);
2321
2322 /* Skip DDR only test when not in DDR mode */
2323 if (a->ddr_only && !mmc->ddr_mode) {
2324 debug("%s(%s): Skipping %s due to non-DDR mode\n",
2325 __func__, dev->name, a->name);
2326 continue;
2327 }
2328 /* Skip hs200 tests in non-hs200 mode and
2329 * non-hs200 tests in hs200 mode
2330 */
2331 if (is_hs200) {
2332 if (a->not_hs200_only) {
2333 debug("%s(%s): Skipping %s\n", __func__,
2334 dev->name, a->name);
2335 continue;
2336 }
2337 } else {
2338 if (a->hs200_only) {
2339 debug("%s(%s): Skipping %s\n", __func__,
2340 dev->name, a->name);
2341 continue;
2342 }
2343 }
2344
2345 err = octeontx_mmc_adjust_tuning(mmc, a, a->opcode ?
2346 a->opcode : opcode);
2347 if (err) {
2348 pr_err("%s(%s, %u): tuning %s failed\n", __func__,
2349 dev->name, opcode, a->name);
2350 return err;
2351 }
2352 }
2353
2354 octeontx_mmc_set_timing(mmc);
2355 if (is_hs200)
2356 slot->hs200_tuned = true;
2357 else
2358 slot->tuned = true;
2359
2360 if (slot->hs400_tuning_block != -1) {
2361 struct mmc_cmd cmd;
2362 struct mmc_data data;
2363 u8 buffer[mmc->read_bl_len];
2364
2365 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
2366 cmd.cmdarg = slot->hs400_tuning_block;
2367 cmd.resp_type = MMC_RSP_R1;
2368 data.dest = (void *)buffer;
2369 data.blocks = 1;
2370 data.blocksize = mmc->read_bl_len;
2371 data.flags = MMC_DATA_READ;
2372 err = octeontx_mmc_read_blocks(mmc, &cmd, &data, true) != 1;
2373
2374 if (err) {
2375 printf("%s: Cannot read HS400 tuning block %u\n",
2376 dev->name, slot->hs400_tuning_block);
2377 return err;
2378 }
2379 if (memcmp(buffer, octeontx_hs400_tuning_block,
2380 sizeof(buffer))) {
2381 debug("%s(%s): Writing new HS400 tuning block to block %d\n",
2382 __func__, dev->name, slot->hs400_tuning_block);
2383 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
2384 data.src = (void *)octeontx_hs400_tuning_block;
2385 data.flags = MMC_DATA_WRITE;
2386 err = !octeontx_mmc_write_blocks(mmc, &cmd, &data);
2387 if (err) {
2388 printf("%s: Cannot write HS400 tuning block %u\n",
2389 dev->name, slot->hs400_tuning_block);
2390 return -EINVAL;
2391 }
2392 }
2393 }
2394
2395 return 0;
2396}
2397#else /* MMC_SUPPORTS_TUNING */
2398static void octeontx_mmc_set_emm_timing(struct mmc *mmc,
2399 union mio_emm_timing emm_timing)
2400{
2401}
2402#endif /* MMC_SUPPORTS_TUNING */
2403
2404/**
2405 * Calculate the clock period with rounding up
2406 *
2407 * @param mmc mmc device
2408 * @return clock period in system clocks for clk_lo + clk_hi
2409 */
2410static u32 octeontx_mmc_calc_clk_period(struct mmc *mmc)
2411{
2412 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2413 struct octeontx_mmc_host *host = slot->host;
2414
2415 return DIV_ROUND_UP(host->sys_freq, mmc->clock);
2416}
2417
2418static int octeontx_mmc_set_ios(struct udevice *dev)
2419{
2420 struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
2421 struct mmc *mmc = &slot->mmc;
2422 struct octeontx_mmc_host *host = slot->host;
2423 union mio_emm_switch emm_switch;
2424 union mio_emm_modex mode;
2425 uint clock;
2426 int bus_width = 0;
2427 int clk_period = 0;
2428 int power_class = 10;
2429 int err = 0;
2430 bool is_hs200 = false;
2431 bool is_hs400 = false;
2432
2433 debug("%s(%s): Entry\n", __func__, dev->name);
2434 debug(" clock: %u, bus width: %u, mode: %u\n", mmc->clock,
2435 mmc->bus_width, mmc->selected_mode);
2436 debug(" host caps: 0x%x, card caps: 0x%x\n", mmc->host_caps,
2437 mmc->card_caps);
2438 octeontx_mmc_switch_to(mmc);
2439
2440 clock = mmc->clock;
2441 if (!clock)
2442 clock = mmc->cfg->f_min;
2443
2444 switch (mmc->bus_width) {
2445 case 8:
2446 bus_width = 2;
2447 break;
2448 case 4:
2449 bus_width = 1;
2450 break;
2451 case 1:
2452 bus_width = 0;
2453 break;
2454 default:
2455 pr_warn("%s(%s): Invalid bus width %d, defaulting to 1\n",
2456 __func__, dev->name, mmc->bus_width);
2457 bus_width = 0;
2458 }
2459
2460 /* DDR is available for 4/8 bit bus width */
2461 if (mmc->ddr_mode && bus_width)
2462 bus_width |= 4;
2463
2464 debug("%s: sys_freq: %llu\n", __func__, host->sys_freq);
2465 clk_period = octeontx_mmc_calc_clk_period(mmc);
2466
2467 emm_switch.u = 0;
2468 emm_switch.s.bus_width = bus_width;
2469 emm_switch.s.power_class = power_class;
2470 emm_switch.s.clk_hi = clk_period / 2;
2471 emm_switch.s.clk_lo = clk_period / 2;
2472
2473 debug("%s: last mode: %d, mode: %d, last clock: %u, clock: %u, ddr: %d\n",
2474 __func__, slot->last_mode, mmc->selected_mode,
2475 slot->last_clock, mmc->clock, mmc->ddr_mode);
2476 switch (mmc->selected_mode) {
2477 case MMC_LEGACY:
2478 break;
2479 case MMC_HS:
2480 case SD_HS:
2481 case MMC_HS_52:
2482 emm_switch.s.hs_timing = 1;
2483 break;
2484 case MMC_HS_200:
2485 is_hs200 = true;
2486 fallthrough;
2487 case UHS_SDR12:
2488 case UHS_SDR25:
2489 case UHS_SDR50:
2490 case UHS_SDR104:
2491 emm_switch.s.hs200_timing = 1;
2492 break;
2493 case MMC_HS_400:
2494 is_hs400 = true;
2495 fallthrough;
2496 case UHS_DDR50:
2497 case MMC_DDR_52:
2498 emm_switch.s.hs400_timing = 1;
2499 break;
2500 default:
2501 pr_err("%s(%s): Unsupported mode 0x%x\n", __func__, dev->name,
2502 mmc->selected_mode);
2503 return -1;
2504 }
2505 emm_switch.s.bus_id = slot->bus_id;
2506
2507 if (!is_hs200 && !is_hs400 &&
2508 (mmc->selected_mode != slot->last_mode ||
2509 mmc->clock != slot->last_clock) &&
2510 !mmc->ddr_mode) {
2511 slot->tuned = false;
2512 slot->last_mode = mmc->selected_mode;
2513 slot->last_clock = mmc->clock;
2514 }
2515
2516 if (CONFIG_IS_ENABLED(MMC_VERBOSE)) {
2517 debug("%s(%s): Setting bus mode to %s\n", __func__, dev->name,
2518 mmc_mode_name(mmc->selected_mode));
2519 } else {
2520 debug("%s(%s): Setting bus mode to 0x%x\n", __func__, dev->name,
2521 mmc->selected_mode);
2522 }
2523
2524 debug(" Trying switch 0x%llx w%d hs:%d hs200:%d hs400:%d\n",
2525 emm_switch.u, emm_switch.s.bus_width, emm_switch.s.hs_timing,
2526 emm_switch.s.hs200_timing, emm_switch.s.hs400_timing);
2527
2528 set_wdog(mmc, 1000);
2529 do_switch(mmc, emm_switch);
2530 mdelay(100);
2531 mode.u = read_csr(mmc, MIO_EMM_MODEX(slot->bus_id));
2532 debug("%s(%s): mode: 0x%llx w:%d, hs:%d, hs200:%d, hs400:%d\n",
2533 __func__, dev->name, mode.u, mode.s.bus_width,
2534 mode.s.hs_timing, mode.s.hs200_timing, mode.s.hs400_timing);
2535
2536 err = octeontx_mmc_configure_delay(mmc);
2537
2538#ifdef MMC_SUPPORTS_TUNING
2539 if (!err && mmc->selected_mode == MMC_HS_400 && !slot->hs400_tuned) {
2540 debug("%s: Tuning HS400 mode\n", __func__);
2541 err = octeontx_tune_hs400(mmc);
2542 }
2543#endif
2544
2545 return err;
2546}
2547
2548/**
2549 * Gets the status of the card detect pin
2550 */
2551static int octeontx_mmc_get_cd(struct udevice *dev)
2552{
2553 struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
2554 int val = 1;
2555
2556 if (dm_gpio_is_valid(&slot->cd_gpio)) {
2557 val = dm_gpio_get_value(&slot->cd_gpio);
2558 val ^= slot->cd_inverted;
2559 }
2560 debug("%s(%s): cd: %d\n", __func__, dev->name, val);
2561 return val;
2562}
2563
2564/**
2565 * Gets the status of the write protect pin
2566 */
2567static int octeontx_mmc_get_wp(struct udevice *dev)
2568{
2569 struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
2570 int val = 0;
2571
2572 if (dm_gpio_is_valid(&slot->wp_gpio)) {
2573 val = dm_gpio_get_value(&slot->wp_gpio);
2574 val ^= slot->wp_inverted;
2575 }
2576 debug("%s(%s): wp: %d\n", __func__, dev->name, val);
2577 return val;
2578}
2579
2580static void octeontx_mmc_set_timing(struct mmc *mmc)
2581{
2582 union mio_emm_timing timing;
2583 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2584
2585 switch (mmc->selected_mode) {
2586 case MMC_HS_200:
2587 timing = slot->hs200_taps;
2588 break;
2589 case MMC_HS_400:
2590 timing = slot->hs400_tuned ?
2591 slot->hs400_taps : slot->hs200_taps;
2592 break;
2593 default:
2594 timing = slot->taps;
2595 break;
2596 }
2597
2598 debug("%s(%s):\n cmd_in_tap: %u\n cmd_out_tap: %u\n data_in_tap: %u\n data_out_tap: %u\n",
2599 __func__, mmc->dev->name, timing.s.cmd_in_tap,
2600 timing.s.cmd_out_tap, timing.s.data_in_tap,
2601 timing.s.data_out_tap);
2602
2603 octeontx_mmc_set_emm_timing(mmc, timing);
2604}
2605
2606static int octeontx_mmc_configure_delay(struct mmc *mmc)
2607{
2608 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2609 struct octeontx_mmc_host *host __maybe_unused = slot->host;
2610 bool __maybe_unused is_hs200;
2611 bool __maybe_unused is_hs400;
2612
2613 debug("%s(%s)\n", __func__, mmc->dev->name);
2614
2615 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
2616 union mio_emm_sample emm_sample;
2617
2618 emm_sample.u = 0;
2619 emm_sample.s.cmd_cnt = slot->cmd_cnt;
2620 emm_sample.s.dat_cnt = slot->dat_cnt;
2621 write_csr(mmc, MIO_EMM_SAMPLE(), emm_sample.u);
2622 } else {
2623 is_hs200 = (mmc->selected_mode == MMC_HS_200);
2624 is_hs400 = (mmc->selected_mode == MMC_HS_400);
2625
2626 if ((is_hs200 && slot->hs200_tuned) ||
2627 (is_hs400 && slot->hs400_tuned) ||
2628 (!is_hs200 && !is_hs400 && slot->tuned)) {
2629 octeontx_mmc_set_output_bus_timing(mmc);
2630 } else {
2631 int half = MAX_NO_OF_TAPS / 2;
2632 int dout, cout;
2633
2634 switch (mmc->selected_mode) {
2635 case MMC_LEGACY:
2636 if (IS_SD(mmc)) {
2637 cout = MMC_SD_LEGACY_DEFAULT_CMD_OUT_TAP;
2638 dout = MMC_SD_LEGACY_DEFAULT_DATA_OUT_TAP;
2639 } else {
2640 cout = MMC_LEGACY_DEFAULT_CMD_OUT_TAP;
2641 dout = MMC_LEGACY_DEFAULT_DATA_OUT_TAP;
2642 }
2643 break;
2644 case MMC_HS:
2645 cout = MMC_HS_CMD_OUT_TAP;
2646 dout = MMC_HS_DATA_OUT_TAP;
2647 break;
2648 case SD_HS:
2649 case UHS_SDR12:
2650 case UHS_SDR25:
2651 case UHS_SDR50:
2652 cout = MMC_SD_HS_CMD_OUT_TAP;
2653 dout = MMC_SD_HS_DATA_OUT_TAP;
2654 break;
2655 case UHS_SDR104:
2656 case UHS_DDR50:
2657 case MMC_HS_52:
2658 case MMC_DDR_52:
2659 cout = MMC_DEFAULT_CMD_OUT_TAP;
2660 dout = MMC_DEFAULT_DATA_OUT_TAP;
2661 break;
2662 case MMC_HS_200:
2663 cout = -1;
2664 dout = -1;
2665 if (host->timing_calibrated) {
2666 cout = octeontx2_mmc_calc_delay(
2667 mmc, slot->cmd_out_hs200_delay);
2668 dout = octeontx2_mmc_calc_delay(
2669 mmc,
2670 slot->data_out_hs200_delay);
2671 debug("%s(%s): Calibrated HS200/HS400 cmd out delay: %dps tap: %d, data out delay: %d, tap: %d\n",
2672 __func__, mmc->dev->name,
2673 slot->cmd_out_hs200_delay, cout,
2674 slot->data_out_hs200_delay, dout);
2675 } else {
2676 cout = MMC_DEFAULT_HS200_CMD_OUT_TAP;
2677 dout = MMC_DEFAULT_HS200_DATA_OUT_TAP;
2678 }
2679 is_hs200 = true;
2680 break;
2681 case MMC_HS_400:
2682 cout = -1;
2683 dout = -1;
2684 if (host->timing_calibrated) {
2685 if (slot->cmd_out_hs400_delay)
2686 cout = octeontx2_mmc_calc_delay(
2687 mmc,
2688 slot->cmd_out_hs400_delay);
2689 if (slot->data_out_hs400_delay)
2690 dout = octeontx2_mmc_calc_delay(
2691 mmc,
2692 slot->data_out_hs400_delay);
2693 debug("%s(%s): Calibrated HS200/HS400 cmd out delay: %dps tap: %d, data out delay: %d, tap: %d\n",
2694 __func__, mmc->dev->name,
2695 slot->cmd_out_hs400_delay, cout,
2696 slot->data_out_hs400_delay, dout);
2697 } else {
2698 cout = MMC_DEFAULT_HS400_CMD_OUT_TAP;
2699 dout = MMC_DEFAULT_HS400_DATA_OUT_TAP;
2700 }
2701 is_hs400 = true;
2702 break;
2703 default:
2704 pr_err("%s(%s): Invalid mode %d\n", __func__,
2705 mmc->dev->name, mmc->selected_mode);
2706 return -1;
2707 }
2708 debug("%s(%s): Not tuned, hs200: %d, hs200 tuned: %d, hs400: %d, hs400 tuned: %d, tuned: %d\n",
2709 __func__, mmc->dev->name, is_hs200,
2710 slot->hs200_tuned,
2711 is_hs400, slot->hs400_tuned, slot->tuned);
2712 /* Set some defaults */
2713 if (is_hs200) {
2714 slot->hs200_taps.u = 0;
2715 slot->hs200_taps.s.cmd_out_tap = cout;
2716 slot->hs200_taps.s.data_out_tap = dout;
2717 slot->hs200_taps.s.cmd_in_tap = half;
2718 slot->hs200_taps.s.data_in_tap = half;
2719 } else if (is_hs400) {
2720 slot->hs400_taps.u = 0;
2721 slot->hs400_taps.s.cmd_out_tap = cout;
2722 slot->hs400_taps.s.data_out_tap = dout;
2723 slot->hs400_taps.s.cmd_in_tap = half;
2724 slot->hs400_taps.s.data_in_tap = half;
2725 } else {
2726 slot->taps.u = 0;
2727 slot->taps.s.cmd_out_tap = cout;
2728 slot->taps.s.data_out_tap = dout;
2729 slot->taps.s.cmd_in_tap = half;
2730 slot->taps.s.data_in_tap = half;
2731 }
2732 }
2733
2734 if (is_hs200)
2735 debug("%s(%s): hs200 taps: ci: %u, co: %u, di: %u, do: %u\n",
2736 __func__, mmc->dev->name,
2737 slot->hs200_taps.s.cmd_in_tap,
2738 slot->hs200_taps.s.cmd_out_tap,
2739 slot->hs200_taps.s.data_in_tap,
2740 slot->hs200_taps.s.data_out_tap);
2741 else if (is_hs400)
2742 debug("%s(%s): hs400 taps: ci: %u, co: %u, di: %u, do: %u\n",
2743 __func__, mmc->dev->name,
2744 slot->hs400_taps.s.cmd_in_tap,
2745 slot->hs400_taps.s.cmd_out_tap,
2746 slot->hs400_taps.s.data_in_tap,
2747 slot->hs400_taps.s.data_out_tap);
2748 else
2749 debug("%s(%s): taps: ci: %u, co: %u, di: %u, do: %u\n",
2750 __func__, mmc->dev->name, slot->taps.s.cmd_in_tap,
2751 slot->taps.s.cmd_out_tap,
2752 slot->taps.s.data_in_tap,
2753 slot->taps.s.data_out_tap);
2754 octeontx_mmc_set_timing(mmc);
2755 debug("%s: Done\n", __func__);
2756 }
2757
2758 return 0;
2759}
2760
2761/**
2762 * Sets the MMC watchdog timer in microseconds
2763 *
2764 * @param mmc mmc device
2765 * @param us timeout in microseconds, 0 for maximum timeout
2766 */
2767static void set_wdog(struct mmc *mmc, u64 us)
2768{
2769 union mio_emm_wdog wdog;
2770 u64 val;
2771
2772 val = (us * mmc->clock) / 1000000;
2773 if (val >= (1 << 26) || !us) {
2774 if (us)
2775 pr_debug("%s: warning: timeout %llu exceeds max value %llu, truncating\n",
2776 __func__, us,
2777 (u64)(((1ULL << 26) - 1) * 1000000ULL) /
2778 mmc->clock);
2779 val = (1 << 26) - 1;
2780 }
2781 wdog.u = 0;
2782 wdog.s.clk_cnt = val;
2783 write_csr(mmc, MIO_EMM_WDOG(), wdog.u);
2784}
2785
2786/**
2787 * Set the IO drive strength and slew
2788 *
2789 * @param mmc mmc device
2790 */
2791static void octeontx_mmc_io_drive_setup(struct mmc *mmc)
2792{
2793 if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
2794 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2795 union mio_emm_io_ctl io_ctl;
2796
2797 if (slot->drive < 0 || slot->slew < 0)
2798 return;
2799
2800 io_ctl.u = 0;
2801 io_ctl.s.drive = slot->drive;
2802 io_ctl.s.slew = slot->slew;
2803 write_csr(mmc, MIO_EMM_IO_CTL(), io_ctl.u);
2804 }
2805}
2806
2807/**
2808 * Print switch errors
2809 *
2810 * @param mmc mmc device
2811 */
2812static void check_switch_errors(struct mmc *mmc)
2813{
2814 union mio_emm_switch emm_switch;
2815
2816 emm_switch.u = read_csr(mmc, MIO_EMM_SWITCH());
2817 if (emm_switch.s.switch_err0)
2818 pr_err("%s: Switch power class error\n", mmc->cfg->name);
2819 if (emm_switch.s.switch_err1)
2820 pr_err("%s: Switch HS timing error\n", mmc->cfg->name);
2821 if (emm_switch.s.switch_err2)
2822 pr_err("%s: Switch bus width error\n", mmc->cfg->name);
2823}
2824
2825static void do_switch(struct mmc *mmc, union mio_emm_switch emm_switch)
2826{
2827 union mio_emm_rsp_sts rsp_sts;
2828 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
2829 int bus_id = emm_switch.s.bus_id;
2830 ulong start;
2831
2832 if (emm_switch.s.bus_id != 0) {
2833 emm_switch.s.bus_id = 0;
2834 write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
2835 udelay(100);
2836 emm_switch.s.bus_id = bus_id;
2837 }
2838 debug("%s(%s, 0x%llx)\n", __func__, mmc->dev->name, emm_switch.u);
2839 write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
2840
2841 start = get_timer(0);
2842 do {
2843 rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
2844 if (!rsp_sts.s.switch_val)
2845 break;
2846 udelay(100);
2847 } while (get_timer(start) < 10);
2848 if (rsp_sts.s.switch_val) {
2849 pr_warn("%s(%s): Warning: writing 0x%llx to emm_switch timed out, status: 0x%llx\n",
2850 __func__, mmc->dev->name, emm_switch.u, rsp_sts.u);
2851 }
2852 slot->cached_switch = emm_switch;
2853 check_switch_errors(mmc);
2854 slot->cached_switch.u = emm_switch.u;
2855 debug("%s: emm_switch: 0x%llx, rsp_lo: 0x%llx\n",
2856 __func__, read_csr(mmc, MIO_EMM_SWITCH()),
2857 read_csr(mmc, MIO_EMM_RSP_LO()));
2858}
2859
2860/**
2861 * Given a delay in ps, return the tap delay count
2862 *
2863 * @param mmc mmc data structure
2864 * @param delay delay in picoseconds
2865 *
2866 * @return Number of tap cycles or error if -1
2867 */
2868static int octeontx2_mmc_calc_delay(struct mmc *mmc, int delay)
2869{
2870 struct octeontx_mmc_host *host = mmc_to_host(mmc);
2871
2872 if (host->is_asim || host->is_emul)
2873 return 63;
2874
2875 if (!host->timing_taps) {
2876 pr_err("%s(%s): Error: host timing not calibrated\n",
2877 __func__, mmc->dev->name);
2878 return -1;
2879 }
2880 debug("%s(%s, %d) timing taps: %llu\n", __func__, mmc->dev->name,
2881 delay, host->timing_taps);
2882 return min_t(int, DIV_ROUND_UP(delay, host->timing_taps), 63);
2883}
2884
2885/**
2886 * Calibrates the delay based on the internal clock
2887 *
2888 * @param mmc Pointer to mmc data structure
2889 *
2890 * @return 0 for success or -ETIMEDOUT on error
2891 *
2892 * NOTE: On error a default value will be calculated.
2893 */
2894static int octeontx_mmc_calibrate_delay(struct mmc *mmc)
2895{
2896 union mio_emm_calb emm_calb;
2897 union mio_emm_tap emm_tap;
2898 union mio_emm_cfg emm_cfg;
2899 union mio_emm_io_ctl emm_io_ctl;
2900 union mio_emm_switch emm_switch;
2901 union mio_emm_wdog emm_wdog;
2902 union mio_emm_sts_mask emm_sts_mask;
2903 union mio_emm_debug emm_debug;
2904 union mio_emm_timing emm_timing;
2905 struct octeontx_mmc_host *host = mmc_to_host(mmc);
2906 ulong start;
2907 u8 bus_id, bus_ena;
2908
2909 debug("%s: Calibrating delay\n", __func__);
2910 if (host->is_asim || host->is_emul) {
2911 debug(" No calibration for ASIM\n");
2912 return 0;
2913 }
2914 emm_tap.u = 0;
2915 if (host->calibrate_glitch) {
2916 emm_tap.s.delay = MMC_DEFAULT_TAP_DELAY;
2917 } else {
2918 /* Save registers */
2919 emm_cfg.u = read_csr(mmc, MIO_EMM_CFG());
2920 emm_io_ctl.u = read_csr(mmc, MIO_EMM_IO_CTL());
2921 emm_switch.u = read_csr(mmc, MIO_EMM_SWITCH());
2922 emm_wdog.u = read_csr(mmc, MIO_EMM_WDOG());
2923 emm_sts_mask.u = read_csr(mmc, MIO_EMM_STS_MASK());
2924 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
2925 emm_timing.u = read_csr(mmc, MIO_EMM_TIMING());
2926 bus_ena = emm_cfg.s.bus_ena;
2927 bus_id = emm_switch.s.bus_id;
2928 emm_cfg.s.bus_ena = 0;
2929 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
2930 udelay(1);
2931 emm_cfg.s.bus_ena = 1ULL << 3;
2932 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
2933 mdelay(1);
2934 emm_calb.u = 0;
2935 write_csr(mmc, MIO_EMM_CALB(), emm_calb.u);
2936 emm_calb.s.start = 1;
2937 write_csr(mmc, MIO_EMM_CALB(), emm_calb.u);
2938 start = get_timer(0);
2939 /* This should only take 3 microseconds */
2940 do {
2941 udelay(5);
2942 emm_tap.u = read_csr(mmc, MIO_EMM_TAP());
2943 } while (!emm_tap.s.delay && get_timer(start) < 10);
2944
2945 emm_calb.s.start = 0;
2946 write_csr(mmc, MIO_EMM_CALB(), emm_calb.u);
2947
2948 emm_cfg.s.bus_ena = 0;
2949 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
2950 udelay(1);
2951 /* Restore registers */
2952 emm_cfg.s.bus_ena = bus_ena;
2953 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
2954 if (host->tap_requires_noclk) {
2955 /* Turn off the clock */
2956 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
2957 emm_debug.s.emmc_clk_disable = 1;
2958 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2959 udelay(1);
2960 emm_debug.s.rdsync_rst = 1;
2961 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2962 udelay(1);
2963 }
2964
2965 write_csr(mmc, MIO_EMM_TIMING(), emm_timing.u);
2966 if (host->tap_requires_noclk) {
2967 /* Turn the clock back on */
2968 udelay(1);
2969 emm_debug.s.rdsync_rst = 0;
2970 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2971 udelay(1);
2972 emm_debug.s.emmc_clk_disable = 0;
2973 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2974 }
2975 udelay(1);
2976 write_csr(mmc, MIO_EMM_IO_CTL(), emm_io_ctl.u);
2977 bus_id = emm_switch.s.bus_id;
2978 emm_switch.s.bus_id = 0;
2979 write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
2980 emm_switch.s.bus_id = bus_id;
2981 write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
2982 write_csr(mmc, MIO_EMM_WDOG(), emm_wdog.u);
2983 write_csr(mmc, MIO_EMM_STS_MASK(), emm_sts_mask.u);
2984 write_csr(mmc, MIO_EMM_RCA(), mmc->rca);
2985 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
2986
2987 if (!emm_tap.s.delay) {
2988 pr_err("%s: Error: delay calibration failed, timed out.\n",
2989 __func__);
2990 /* Set to default value if timed out */
2991 emm_tap.s.delay = MMC_DEFAULT_TAP_DELAY;
2992 return -ETIMEDOUT;
2993 }
2994 }
2995 /* Round up */
2996 host->timing_taps = (10 * 1000 * emm_tap.s.delay) / TOTAL_NO_OF_TAPS;
2997 debug("%s(%s): timing taps: %llu, delay: %u\n",
2998 __func__, mmc->dev->name, host->timing_taps, emm_tap.s.delay);
2999 host->timing_calibrated = true;
3000 return 0;
3001}
3002
3003static int octeontx_mmc_set_input_bus_timing(struct mmc *mmc)
3004{
3005 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3006
3007 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
3008 union mio_emm_sample sample;
3009
3010 sample.u = 0;
3011 sample.s.cmd_cnt = slot->cmd_clk_skew;
3012 sample.s.dat_cnt = slot->dat_clk_skew;
3013 write_csr(mmc, MIO_EMM_SAMPLE(), sample.u);
3014 } else {
3015 union mio_emm_timing timing;
3016
3017 timing.u = read_csr(mmc, MIO_EMM_TIMING());
3018 if (mmc->selected_mode == MMC_HS_200) {
3019 if (slot->hs200_tuned) {
3020 timing.s.cmd_in_tap =
3021 slot->hs200_taps.s.cmd_in_tap;
3022 timing.s.data_in_tap =
3023 slot->hs200_taps.s.data_in_tap;
3024 } else {
3025 pr_warn("%s(%s): Warning: hs200 timing not tuned\n",
3026 __func__, mmc->dev->name);
3027 timing.s.cmd_in_tap =
3028 MMC_DEFAULT_HS200_CMD_IN_TAP;
3029 timing.s.data_in_tap =
3030 MMC_DEFAULT_HS200_DATA_IN_TAP;
3031 }
3032 } else if (mmc->selected_mode == MMC_HS_400) {
3033 if (slot->hs400_tuned) {
3034 timing.s.cmd_in_tap =
3035 slot->hs400_taps.s.cmd_in_tap;
3036 timing.s.data_in_tap =
3037 slot->hs400_taps.s.data_in_tap;
3038 } else if (slot->hs200_tuned) {
3039 timing.s.cmd_in_tap =
3040 slot->hs200_taps.s.cmd_in_tap;
3041 timing.s.data_in_tap =
3042 slot->hs200_taps.s.data_in_tap;
3043 } else {
3044 pr_warn("%s(%s): Warning: hs400 timing not tuned\n",
3045 __func__, mmc->dev->name);
3046 timing.s.cmd_in_tap =
3047 MMC_DEFAULT_HS200_CMD_IN_TAP;
3048 timing.s.data_in_tap =
3049 MMC_DEFAULT_HS200_DATA_IN_TAP;
3050 }
3051 } else if (slot->tuned) {
3052 timing.s.cmd_in_tap = slot->taps.s.cmd_in_tap;
3053 timing.s.data_in_tap = slot->taps.s.data_in_tap;
3054 } else {
3055 timing.s.cmd_in_tap = MMC_DEFAULT_CMD_IN_TAP;
3056 timing.s.data_in_tap = MMC_DEFAULT_DATA_IN_TAP;
3057 }
3058 octeontx_mmc_set_emm_timing(mmc, timing);
3059 }
3060
3061 return 0;
3062}
3063
3064/**
3065 * Sets the default bus timing for the current mode.
3066 *
3067 * @param mmc mmc data structure
3068 *
3069 * @return 0 for success, error otherwise
3070 */
3071static int octeontx_mmc_set_output_bus_timing(struct mmc *mmc)
3072{
3073 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3074 union mio_emm_timing timing;
3075 int cout_bdelay, dout_bdelay;
3076 unsigned int cout_delay, dout_delay;
3077 char env_name[32];
3078
3079 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX))
3080 return 0;
3081
3082 debug("%s(%s)\n", __func__, mmc->dev->name);
3083 if (slot->is_asim || slot->is_emul)
3084 return 0;
3085
3086 octeontx_mmc_calibrate_delay(mmc);
3087
3088 if (mmc->clock < 26000000) {
3089 cout_delay = 5000;
3090 dout_delay = 5000;
3091 } else if (mmc->clock <= 52000000) {
3092 cout_delay = 2500;
3093 dout_delay = 2500;
3094 } else if (!mmc_is_mode_ddr(mmc->selected_mode)) {
3095 cout_delay = slot->cmd_out_hs200_delay;
3096 dout_delay = slot->data_out_hs200_delay;
3097 } else {
3098 cout_delay = slot->cmd_out_hs400_delay;
3099 dout_delay = slot->data_out_hs400_delay;
3100 }
3101
3102 snprintf(env_name, sizeof(env_name), "mmc%d_hs200_dout_delay_ps",
3103 slot->bus_id);
3104 dout_delay = env_get_ulong(env_name, 10, dout_delay);
3105 debug("%s: dout_delay: %u\n", __func__, dout_delay);
3106
3107 cout_bdelay = octeontx2_mmc_calc_delay(mmc, cout_delay);
3108 dout_bdelay = octeontx2_mmc_calc_delay(mmc, dout_delay);
3109
3110 debug("%s: cmd output delay: %u, data output delay: %u, cmd bdelay: %d, data bdelay: %d, clock: %d\n",
3111 __func__, cout_delay, dout_delay, cout_bdelay, dout_bdelay,
3112 mmc->clock);
3113 if (cout_bdelay < 0 || dout_bdelay < 0) {
3114 pr_err("%s: Error: could not calculate command and/or data clock skew\n",
3115 __func__);
3116 return -1;
3117 }
3118 timing.u = read_csr(mmc, MIO_EMM_TIMING());
3119 timing.s.cmd_out_tap = cout_bdelay;
3120 timing.s.data_out_tap = dout_bdelay;
3121 if (mmc->selected_mode == MMC_HS_200) {
3122 slot->hs200_taps.s.cmd_out_tap = cout_bdelay;
3123 slot->hs200_taps.s.data_out_tap = dout_bdelay;
3124 } else if (mmc->selected_mode == MMC_HS_400) {
3125 slot->hs400_taps.s.cmd_out_tap = cout_bdelay;
3126 slot->hs400_taps.s.data_out_tap = dout_bdelay;
3127 } else {
3128 slot->taps.s.cmd_out_tap = cout_bdelay;
3129 slot->taps.s.data_out_tap = dout_bdelay;
3130 }
3131 octeontx_mmc_set_emm_timing(mmc, timing);
3132 debug("%s(%s): bdelay: %d/%d, clock: %d, ddr: %s, timing taps: %llu, do: %d, di: %d, co: %d, ci: %d\n",
3133 __func__, mmc->dev->name, cout_bdelay, dout_bdelay, mmc->clock,
3134 mmc->ddr_mode ? "yes" : "no",
3135 mmc_to_host(mmc)->timing_taps,
3136 timing.s.data_out_tap,
3137 timing.s.data_in_tap,
3138 timing.s.cmd_out_tap,
3139 timing.s.cmd_in_tap);
3140
3141 return 0;
3142}
3143
3144static void octeontx_mmc_set_clock(struct mmc *mmc)
3145{
3146 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3147 uint clock;
3148
3149 clock = min(mmc->cfg->f_max, (uint)slot->clock);
3150 clock = max(mmc->cfg->f_min, clock);
3151 debug("%s(%s): f_min: %u, f_max: %u, clock: %u\n", __func__,
3152 mmc->dev->name, mmc->cfg->f_min, mmc->cfg->f_max, clock);
3153 slot->clock = clock;
3154 mmc->clock = clock;
3155}
3156
3157/**
3158 * This switches I/O power as needed when switching between slots.
3159 *
3160 * @param mmc mmc data structure
3161 */
3162static void octeontx_mmc_switch_io(struct mmc *mmc)
3163{
3164 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3165 struct octeontx_mmc_host *host = slot->host;
3166 struct mmc *last_mmc = host->last_mmc;
3167 static struct udevice *last_reg;
3168 union mio_emm_cfg emm_cfg;
3169 int bus;
3170 static bool initialized;
3171
3172 /* First time? */
3173 if (!initialized || mmc != host->last_mmc) {
3174 struct mmc *ommc;
3175
3176 /* Switch to bus 3 which is unused */
3177 emm_cfg.u = read_csr(mmc, MIO_EMM_CFG());
3178 emm_cfg.s.bus_ena = 1 << 3;
3179 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
3180
3181 /* Turn off all other I/O interfaces with first initialization
3182 * if at least one supply was found.
3183 */
3184 for (bus = 0; bus <= OCTEONTX_MAX_MMC_SLOT; bus++) {
3185 ommc = &host->slots[bus].mmc;
3186
3187 /* Handle self case later */
3188 if (ommc == mmc || !ommc->vqmmc_supply)
3189 continue;
3190
3191 /* Skip if we're not switching regulators */
3192 if (last_reg == mmc->vqmmc_supply)
3193 continue;
3194
3195 /* Turn off other regulators */
3196 if (ommc->vqmmc_supply != mmc->vqmmc_supply)
3197 regulator_set_enable(ommc->vqmmc_supply, false);
3198 }
3199 /* Turn ourself on */
3200 if (mmc->vqmmc_supply && last_reg != mmc->vqmmc_supply)
3201 regulator_set_enable(mmc->vqmmc_supply, true);
3202 mdelay(1); /* Settle time */
3203 /* Switch to new bus */
3204 emm_cfg.s.bus_ena = 1 << slot->bus_id;
3205 write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
3206 last_reg = mmc->vqmmc_supply;
3207 initialized = true;
3208 return;
3209 }
3210
3211 /* No change in device */
3212 if (last_mmc == mmc)
3213 return;
3214
3215 if (!last_mmc) {
3216 pr_warn("%s(%s): No previous slot detected in IO slot switch!\n",
3217 __func__, mmc->dev->name);
3218 return;
3219 }
3220
3221 debug("%s(%s): last: %s, supply: %p\n", __func__, mmc->dev->name,
3222 last_mmc->dev->name, mmc->vqmmc_supply);
3223
3224 /* The supply is the same so we do nothing */
3225 if (last_mmc->vqmmc_supply == mmc->vqmmc_supply)
3226 return;
3227
3228 /* Turn off the old slot I/O supply */
3229 if (last_mmc->vqmmc_supply) {
3230 debug("%s(%s): Turning off IO to %s, supply: %s\n",
3231 __func__, mmc->dev->name, last_mmc->dev->name,
3232 last_mmc->vqmmc_supply->name);
3233 regulator_set_enable(last_mmc->vqmmc_supply, false);
3234 }
3235 /* Turn on the new slot I/O supply */
3236 if (mmc->vqmmc_supply) {
3237 debug("%s(%s): Turning on IO to slot %d, supply: %s\n",
3238 __func__, mmc->dev->name, slot->bus_id,
3239 mmc->vqmmc_supply->name);
3240 regulator_set_enable(mmc->vqmmc_supply, true);
3241 }
3242 /* Allow power to settle */
3243 mdelay(1);
3244}
3245
3246/**
3247 * Called to switch between mmc devices
3248 *
3249 * @param mmc new mmc device
3250 */
3251static void octeontx_mmc_switch_to(struct mmc *mmc)
3252{
3253 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3254 struct octeontx_mmc_slot *old_slot;
3255 struct octeontx_mmc_host *host = slot->host;
3256 union mio_emm_switch emm_switch;
3257 union mio_emm_sts_mask emm_sts_mask;
3258 union mio_emm_rca emm_rca;
3259
3260 if (slot->bus_id == host->last_slotid)
3261 return;
3262
3263 debug("%s(%s) switching from slot %d to slot %d\n", __func__,
3264 mmc->dev->name, host->last_slotid, slot->bus_id);
3265 octeontx_mmc_switch_io(mmc);
3266
3267 if (host->last_slotid >= 0 && slot->valid) {
3268 old_slot = &host->slots[host->last_slotid];
3269 old_slot->cached_switch.u = read_csr(mmc, MIO_EMM_SWITCH());
3270 old_slot->cached_rca.u = read_csr(mmc, MIO_EMM_RCA());
3271 }
3272 if (mmc->rca)
3273 write_csr(mmc, MIO_EMM_RCA(), mmc->rca);
3274 emm_switch = slot->cached_switch;
3275 do_switch(mmc, emm_switch);
3276 emm_rca.u = 0;
3277 emm_rca.s.card_rca = mmc->rca;
3278 write_csr(mmc, MIO_EMM_RCA(), emm_rca.u);
3279 mdelay(100);
3280
3281 set_wdog(mmc, 100000);
3282 if (octeontx_mmc_set_output_bus_timing(mmc) ||
3283 octeontx_mmc_set_input_bus_timing(mmc))
3284 pr_err("%s(%s): Error setting bus timing\n", __func__,
3285 mmc->dev->name);
3286 octeontx_mmc_io_drive_setup(mmc);
3287
3288 emm_sts_mask.u = 0;
3289 emm_sts_mask.s.sts_msk = 1 << 7 | 1 << 22 | 1 << 23 | 1 << 19;
3290 write_csr(mmc, MIO_EMM_STS_MASK(), emm_sts_mask.u);
3291 host->last_slotid = slot->bus_id;
3292 host->last_mmc = mmc;
3293 mdelay(10);
3294}
3295
3296/**
3297 * Perform initial timing configuration
3298 *
3299 * @param mmc mmc device
3300 *
3301 * @return 0 for success
3302 *
3303 * NOTE: This will need to be updated when new silicon comes out
3304 */
3305static int octeontx_mmc_init_timing(struct mmc *mmc)
3306{
3307 union mio_emm_timing timing;
3308
3309 if (mmc_to_slot(mmc)->is_asim || mmc_to_slot(mmc)->is_emul)
3310 return 0;
3311
3312 debug("%s(%s)\n", __func__, mmc->dev->name);
3313 timing.u = 0;
3314 timing.s.cmd_out_tap = MMC_DEFAULT_CMD_OUT_TAP;
3315 timing.s.data_out_tap = MMC_DEFAULT_DATA_OUT_TAP;
3316 timing.s.cmd_in_tap = MMC_DEFAULT_CMD_IN_TAP;
3317 timing.s.data_in_tap = MMC_DEFAULT_DATA_IN_TAP;
3318 octeontx_mmc_set_emm_timing(mmc, timing);
3319 return 0;
3320}
3321
3322/**
3323 * Perform low-level initialization
3324 *
3325 * @param mmc mmc device
3326 *
3327 * @return 0 for success, error otherwise
3328 */
3329static int octeontx_mmc_init_lowlevel(struct mmc *mmc)
3330{
3331 struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
3332 struct octeontx_mmc_host *host = slot->host;
3333 union mio_emm_switch emm_switch;
3334 u32 clk_period;
3335
3336 debug("%s(%s): lowlevel init for slot %d\n", __func__,
3337 mmc->dev->name, slot->bus_id);
3338 host->emm_cfg.s.bus_ena &= ~(1 << slot->bus_id);
3339 write_csr(mmc, MIO_EMM_CFG(), host->emm_cfg.u);
3340 udelay(100);
3341 host->emm_cfg.s.bus_ena |= 1 << slot->bus_id;
3342 write_csr(mmc, MIO_EMM_CFG(), host->emm_cfg.u);
3343 udelay(10);
3344 slot->clock = mmc->cfg->f_min;
3345 octeontx_mmc_set_clock(&slot->mmc);
3346
3347 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
3348 if (host->cond_clock_glitch) {
3349 union mio_emm_debug emm_debug;
3350
3351 emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
3352 emm_debug.s.clk_on = 1;
3353 write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
3354 }
3355 octeontx_mmc_calibrate_delay(&slot->mmc);
3356 }
3357
3358 clk_period = octeontx_mmc_calc_clk_period(mmc);
3359 emm_switch.u = 0;
3360 emm_switch.s.power_class = 10;
3361 emm_switch.s.clk_lo = clk_period / 2;
3362 emm_switch.s.clk_hi = clk_period / 2;
3363
3364 emm_switch.s.bus_id = slot->bus_id;
3365 debug("%s: Performing switch\n", __func__);
3366 do_switch(mmc, emm_switch);
3367 slot->cached_switch.u = emm_switch.u;
3368
3369 if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX))
3370 octeontx_mmc_init_timing(mmc);
3371
3372 set_wdog(mmc, 1000000); /* Set to 1 second */
3373 write_csr(mmc, MIO_EMM_STS_MASK(), 0xe4390080ull);
3374 write_csr(mmc, MIO_EMM_RCA(), 1);
3375 mdelay(10);
3376 debug("%s: done\n", __func__);
3377 return 0;
3378}
3379
3380/**
3381 * Translates a voltage number to bits in MMC register
3382 *
3383 * @param voltage voltage in microvolts
3384 *
3385 * @return MMC register value for voltage
3386 */
3387static u32 xlate_voltage(u32 voltage)
3388{
3389 u32 volt = 0;
3390
3391 /* Convert to millivolts */
3392 voltage /= 1000;
3393 if (voltage >= 1650 && voltage <= 1950)
3394 volt |= MMC_VDD_165_195;
3395 if (voltage >= 2000 && voltage <= 2100)
3396 volt |= MMC_VDD_20_21;
3397 if (voltage >= 2100 && voltage <= 2200)
3398 volt |= MMC_VDD_21_22;
3399 if (voltage >= 2200 && voltage <= 2300)
3400 volt |= MMC_VDD_22_23;
3401 if (voltage >= 2300 && voltage <= 2400)
3402 volt |= MMC_VDD_23_24;
3403 if (voltage >= 2400 && voltage <= 2500)
3404 volt |= MMC_VDD_24_25;
3405 if (voltage >= 2500 && voltage <= 2600)
3406 volt |= MMC_VDD_25_26;
3407 if (voltage >= 2600 && voltage <= 2700)
3408 volt |= MMC_VDD_26_27;
3409 if (voltage >= 2700 && voltage <= 2800)
3410 volt |= MMC_VDD_27_28;
3411 if (voltage >= 2800 && voltage <= 2900)
3412 volt |= MMC_VDD_28_29;
3413 if (voltage >= 2900 && voltage <= 3000)
3414 volt |= MMC_VDD_29_30;
3415 if (voltage >= 3000 && voltage <= 3100)
3416 volt |= MMC_VDD_30_31;
3417 if (voltage >= 3100 && voltage <= 3200)
3418 volt |= MMC_VDD_31_32;
3419 if (voltage >= 3200 && voltage <= 3300)
3420 volt |= MMC_VDD_32_33;
3421 if (voltage >= 3300 && voltage <= 3400)
3422 volt |= MMC_VDD_33_34;
3423 if (voltage >= 3400 && voltage <= 3500)
3424 volt |= MMC_VDD_34_35;
3425 if (voltage >= 3500 && voltage <= 3600)
3426 volt |= MMC_VDD_35_36;
3427
3428 return volt;
3429}
3430
3431/**
3432 * Check if a slot is valid in the device tree
3433 *
3434 * @param dev slot device to check
3435 *
3436 * @return true if status reports "ok" or "okay" or if no status,
3437 * false otherwise.
3438 */
3439static bool octeontx_mmc_get_valid(struct udevice *dev)
3440{
3441 const char *stat = ofnode_read_string(dev->node, "status");
3442
3443 if (!stat || !strncmp(stat, "ok", 2))
3444 return true;
3445 else
3446 return false;
3447}
3448
3449/**
3450 * Reads slot configuration from the device tree
3451 *
3452 * @param dev slot device
3453 *
3454 * @return 0 on success, otherwise error
3455 */
3456static int octeontx_mmc_get_config(struct udevice *dev)
3457{
3458 struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
3459 uint voltages[2];
3460 uint low, high;
3461 char env_name[32];
3462 int err;
3463 ofnode node = dev->node;
3464 int bus_width = 1;
3465 ulong new_max_freq;
3466
3467 debug("%s(%s)", __func__, dev->name);
3468 slot->cfg.name = dev->name;
3469
3470 slot->cfg.f_max = ofnode_read_s32_default(dev->node, "max-frequency",
3471 26000000);
3472 snprintf(env_name, sizeof(env_name), "mmc_max_frequency%d",
3473 slot->bus_id);
3474
3475 new_max_freq = env_get_ulong(env_name, 10, slot->cfg.f_max);
3476 debug("Reading %s, got %lu\n", env_name, new_max_freq);
3477
3478 if (new_max_freq != slot->cfg.f_max) {
3479 printf("Overriding device tree MMC maximum frequency %u to %lu\n",
3480 slot->cfg.f_max, new_max_freq);
3481 slot->cfg.f_max = new_max_freq;
3482 }
3483 slot->cfg.f_min = 400000;
3484 slot->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
3485
3486 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
3487 slot->hs400_tuning_block =
3488 ofnode_read_s32_default(dev->node,
3489 "marvell,hs400-tuning-block",
3490 -1);
3491 debug("%s(%s): mmc HS400 tuning block: %d\n", __func__,
3492 dev->name, slot->hs400_tuning_block);
3493
3494 slot->hs200_tap_adj =
3495 ofnode_read_s32_default(dev->node,
3496 "marvell,hs200-tap-adjust", 0);
3497 debug("%s(%s): hs200-tap-adjust: %d\n", __func__, dev->name,
3498 slot->hs200_tap_adj);
3499 slot->hs400_tap_adj =
3500 ofnode_read_s32_default(dev->node,
3501 "marvell,hs400-tap-adjust", 0);
3502 debug("%s(%s): hs400-tap-adjust: %d\n", __func__, dev->name,
3503 slot->hs400_tap_adj);
3504 }
3505
3506 err = ofnode_read_u32_array(dev->node, "voltage-ranges", voltages, 2);
3507 if (err) {
3508 slot->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
3509 } else {
3510 low = xlate_voltage(voltages[0]);
3511 high = xlate_voltage(voltages[1]);
3512 debug(" low voltage: 0x%x (%u), high: 0x%x (%u)\n",
3513 low, voltages[0], high, voltages[1]);
3514 if (low > high || !low || !high) {
3515 pr_err("Invalid MMC voltage range [%u-%u] specified for %s\n",
3516 low, high, dev->name);
3517 return -1;
3518 }
3519 slot->cfg.voltages = 0;
3520 do {
3521 slot->cfg.voltages |= low;
3522 low <<= 1;
3523 } while (low <= high);
3524 }
3525 debug("%s: config voltages: 0x%x\n", __func__, slot->cfg.voltages);
3526 slot->slew = ofnode_read_s32_default(node, "cavium,clk-slew", -1);
3527 slot->drive = ofnode_read_s32_default(node, "cavium,drv-strength", -1);
3528 gpio_request_by_name(dev, "cd-gpios", 0, &slot->cd_gpio, GPIOD_IS_IN);
3529 slot->cd_inverted = ofnode_read_bool(node, "cd-inverted");
3530 gpio_request_by_name(dev, "wp-gpios", 0, &slot->wp_gpio, GPIOD_IS_IN);
3531 slot->wp_inverted = ofnode_read_bool(node, "wp-inverted");
3532 if (slot->cfg.voltages & MMC_VDD_165_195) {
3533 slot->is_1_8v = true;
3534 slot->is_3_3v = false;
3535 } else if (slot->cfg.voltages & (MMC_VDD_30_31 | MMC_VDD_31_32 |
3536 MMC_VDD_33_34 | MMC_VDD_34_35 |
3537 MMC_VDD_35_36)) {
3538 slot->is_1_8v = false;
3539 slot->is_3_3v = true;
3540 }
3541
3542 bus_width = ofnode_read_u32_default(node, "bus-width", 1);
3543 /* Note fall-through */
3544 switch (bus_width) {
3545 case 8:
3546 slot->cfg.host_caps |= MMC_MODE_8BIT;
3547 case 4:
3548 slot->cfg.host_caps |= MMC_MODE_4BIT;
3549 case 1:
3550 slot->cfg.host_caps |= MMC_MODE_1BIT;
3551 break;
3552 }
3553 if (ofnode_read_bool(node, "no-1-8-v")) {
3554 slot->is_3_3v = true;
3555 slot->is_1_8v = false;
3556 if (!(slot->cfg.voltages & (MMC_VDD_32_33 | MMC_VDD_33_34)))
3557 pr_warn("%s(%s): voltages indicate 3.3v but 3.3v not supported\n",
3558 __func__, dev->name);
3559 }
3560 if (ofnode_read_bool(node, "mmc-ddr-3-3v")) {
3561 slot->is_3_3v = true;
3562 slot->is_1_8v = false;
3563 if (!(slot->cfg.voltages & (MMC_VDD_32_33 | MMC_VDD_33_34)))
3564 pr_warn("%s(%s): voltages indicate 3.3v but 3.3v not supported\n",
3565 __func__, dev->name);
3566 }
3567 if (ofnode_read_bool(node, "cap-sd-highspeed") ||
3568 ofnode_read_bool(node, "cap-mmc-highspeed") ||
3569 ofnode_read_bool(node, "sd-uhs-sdr25"))
3570 slot->cfg.host_caps |= MMC_MODE_HS;
3571 if (slot->cfg.f_max >= 50000000 &&
3572 slot->cfg.host_caps & MMC_MODE_HS)
3573 slot->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
3574 if (ofnode_read_bool(node, "sd-uhs-sdr50"))
3575 slot->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
3576 if (ofnode_read_bool(node, "sd-uhs-ddr50"))
3577 slot->cfg.host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz |
3578 MMC_MODE_DDR_52MHz;
3579
3580 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
3581 if (!slot->is_asim && !slot->is_emul) {
3582 if (ofnode_read_bool(node, "mmc-hs200-1_8v"))
3583 slot->cfg.host_caps |= MMC_MODE_HS200 |
3584 MMC_MODE_HS_52MHz;
3585 if (ofnode_read_bool(node, "mmc-hs400-1_8v"))
3586 slot->cfg.host_caps |= MMC_MODE_HS400 |
3587 MMC_MODE_HS_52MHz |
3588 MMC_MODE_HS200 |
3589 MMC_MODE_DDR_52MHz;
3590 slot->cmd_out_hs200_delay =
3591 ofnode_read_u32_default(node,
3592 "marvell,cmd-out-hs200-dly",
3593 MMC_DEFAULT_HS200_CMD_OUT_DLY);
3594 debug("%s(%s): HS200 cmd out delay: %d\n",
3595 __func__, dev->name, slot->cmd_out_hs200_delay);
3596 slot->data_out_hs200_delay =
3597 ofnode_read_u32_default(node,
3598 "marvell,data-out-hs200-dly",
3599 MMC_DEFAULT_HS200_DATA_OUT_DLY);
3600 debug("%s(%s): HS200 data out delay: %d\n",
3601 __func__, dev->name, slot->data_out_hs200_delay);
3602 slot->cmd_out_hs400_delay =
3603 ofnode_read_u32_default(node,
3604 "marvell,cmd-out-hs400-dly",
3605 MMC_DEFAULT_HS400_CMD_OUT_DLY);
3606 debug("%s(%s): HS400 cmd out delay: %d\n",
3607 __func__, dev->name, slot->cmd_out_hs400_delay);
3608 slot->data_out_hs400_delay =
3609 ofnode_read_u32_default(node,
3610 "marvell,data-out-hs400-dly",
3611 MMC_DEFAULT_HS400_DATA_OUT_DLY);
3612 debug("%s(%s): HS400 data out delay: %d\n",
3613 __func__, dev->name, slot->data_out_hs400_delay);
3614 }
3615 }
3616
3617 slot->disable_ddr = ofnode_read_bool(node, "marvell,disable-ddr");
3618 slot->non_removable = ofnode_read_bool(node, "non-removable");
3619 slot->cmd_clk_skew = ofnode_read_u32_default(node,
3620 "cavium,cmd-clk-skew", 0);
3621 slot->dat_clk_skew = ofnode_read_u32_default(node,
3622 "cavium,dat-clk-skew", 0);
3623 debug("%s(%s): host caps: 0x%x\n", __func__,
3624 dev->name, slot->cfg.host_caps);
3625 return 0;
3626}
3627
3628/**
3629 * Probes a MMC slot
3630 *
3631 * @param dev mmc device
3632 *
3633 * @return 0 for success, error otherwise
3634 */
3635static int octeontx_mmc_slot_probe(struct udevice *dev)
3636{
3637 struct octeontx_mmc_slot *slot;
3638 struct mmc *mmc;
3639 int err;
3640
3641 printk("%s (%d)\n", __func__, __LINE__); // test-only
3642 debug("%s(%s)\n", __func__, dev->name);
3643 if (!host_probed) {
3644 pr_err("%s(%s): Error: host not probed yet\n",
3645 __func__, dev->name);
3646 }
3647 slot = dev_to_mmc_slot(dev);
3648 mmc = &slot->mmc;
3649 mmc->dev = dev;
3650
3651 slot->valid = false;
3652 if (!octeontx_mmc_get_valid(dev)) {
3653 debug("%s(%s): slot is invalid\n", __func__, dev->name);
3654 return -ENODEV;
3655 }
3656
3657 debug("%s(%s): Getting config\n", __func__, dev->name);
3658 err = octeontx_mmc_get_config(dev);
3659 if (err) {
3660 pr_err("probe(%s): Error getting config\n", dev->name);
3661 return err;
3662 }
3663
3664 debug("%s(%s): mmc bind, mmc: %p\n", __func__, dev->name, &slot->mmc);
3665 err = mmc_bind(dev, &slot->mmc, &slot->cfg);
3666 if (err) {
3667 pr_err("%s(%s): Error binding mmc\n", __func__, dev->name);
3668 return -1;
3669 }
3670
3671 /* For some reason, mmc_bind always assigns priv to the device */
3672 slot->mmc.priv = slot;
3673
3674 debug("%s(%s): lowlevel init\n", __func__, dev->name);
3675 err = octeontx_mmc_init_lowlevel(mmc);
3676 if (err) {
3677 pr_err("probe(%s): Low-level init failed\n", dev->name);
3678 return err;
3679 }
3680
3681 slot->valid = true;
3682
3683 debug("%s(%s):\n"
3684 " base address : %p\n"
3685 " bus id : %d\n", __func__, dev->name,
3686 slot->base_addr, slot->bus_id);
3687
3688 return err;
3689}
3690
3691/**
3692 * MMC slot driver operations
3693 */
3694static const struct dm_mmc_ops octeontx_hsmmc_ops = {
3695 .send_cmd = octeontx_mmc_dev_send_cmd,
3696 .set_ios = octeontx_mmc_set_ios,
3697 .get_cd = octeontx_mmc_get_cd,
3698 .get_wp = octeontx_mmc_get_wp,
3699#ifdef MMC_SUPPORTS_TUNING
3700 .execute_tuning = octeontx_mmc_execute_tuning,
3701#endif
3702};
3703
3704static const struct udevice_id octeontx_hsmmc_ids[] = {
3705 { .compatible = "mmc-slot" },
3706 { }
3707};
3708
3709U_BOOT_DRIVER(octeontx_hsmmc_slot) = {
3710 .name = "octeontx_hsmmc_slot",
3711 .id = UCLASS_MMC,
3712 .of_match = of_match_ptr(octeontx_hsmmc_ids),
3713 .probe = octeontx_mmc_slot_probe,
3714 .ops = &octeontx_hsmmc_ops,
3715};
3716
3717/*****************************************************************
3718 * PCI host driver
3719 *
3720 * The PCI host driver contains the resources used by all of the
3721 * slot drivers.
3722 *
3723 * The slot drivers are pseudo drivers.
3724 */
3725
3726/**
3727 * Probe the MMC host controller
3728 *
3729 * @param dev mmc host controller device
3730 *
3731 * @return 0 for success, -1 on error
3732 */
3733static int octeontx_mmc_host_probe(struct udevice *dev)
3734{
3735 pci_dev_t bdf = dm_pci_get_bdf(dev);
3736 struct octeontx_mmc_host *host = dev_get_priv(dev);
3737 union mio_emm_int emm_int;
3738 u8 rev;
3739
3740 debug("%s(%s): Entry host: %p\n", __func__, dev->name, host);
3741
3742 if (!octeontx_mmc_get_valid(dev)) {
3743 debug("%s(%s): mmc host not valid\n", __func__, dev->name);
3744 return -ENODEV;
3745 }
3746 memset(host, 0, sizeof(*host));
3747 host->base_addr = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0,
3748 PCI_REGION_MEM);
3749 if (!host->base_addr) {
3750 pr_err("%s: Error: MMC base address not found\n", __func__);
3751 return -1;
3752 }
3753 host->dev = dev;
3754 debug("%s(%s): Base address: %p\n", __func__, dev->name,
3755 host->base_addr);
3756 if (!dev_has_of_node(dev)) {
3757 pr_err("%s: No device tree information found\n", __func__);
3758 return -1;
3759 }
3760 host->node = dev->node;
3761 dev->req_seq = PCI_FUNC(bdf);
3762 host->last_slotid = -1;
3763 if (otx_is_platform(PLATFORM_ASIM))
3764 host->is_asim = true;
3765 if (otx_is_platform(PLATFORM_EMULATOR))
3766 host->is_emul = true;
3767 host->dma_wait_delay =
3768 ofnode_read_u32_default(dev->node, "marvell,dma-wait-delay", 1);
3769 /* Force reset of eMMC */
3770 writeq(0, host->base_addr + MIO_EMM_CFG());
3771 debug("%s: Clearing MIO_EMM_CFG\n", __func__);
3772 udelay(100);
3773 emm_int.u = readq(host->base_addr + MIO_EMM_INT());
3774 debug("%s: Writing 0x%llx to MIO_EMM_INT\n", __func__, emm_int.u);
3775 writeq(emm_int.u, host->base_addr + MIO_EMM_INT());
3776
3777 debug("%s(%s): Getting I/O clock\n", __func__, dev->name);
3778 host->sys_freq = octeontx_get_io_clock();
3779 debug("%s(%s): I/O clock %llu\n", __func__, dev->name, host->sys_freq);
3780
3781 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
3782 /* Flags for issues to work around */
3783 dm_pci_read_config8(dev, PCI_REVISION_ID, &rev);
3784 if (otx_is_soc(CN96XX)) {
3785 debug("%s: CN96XX revision %d\n", __func__, rev);
3786 switch (rev) {
3787 case 0:
3788 host->calibrate_glitch = true;
3789 host->cond_clock_glitch = true;
3790 break;
3791 case 1:
3792 break;
3793 case 2:
3794 break;
3795 case 0x10: /* C0 */
3796 host->hs400_skew_needed = true;
3797 debug("HS400 skew support enabled\n");
3798 fallthrough;
3799 default:
3800 debug("CN96XX rev C0+ detected\n");
3801 host->tap_requires_noclk = true;
3802 break;
3803 }
3804 } else if (otx_is_soc(CN95XX)) {
3805 if (!rev)
3806 host->cond_clock_glitch = true;
3807 }
3808 }
3809
3810 host_probed = true;
3811
3812 return 0;
3813}
3814
3815/**
3816 * This performs some initial setup before a probe occurs.
3817 *
3818 * @param dev: MMC slot device
3819 *
3820 * @return 0 for success, -1 on failure
3821 *
3822 * Do some pre-initialization before probing a slot.
3823 */
3824static int octeontx_mmc_host_child_pre_probe(struct udevice *dev)
3825{
3826 struct octeontx_mmc_host *host = dev_get_priv(dev_get_parent(dev));
3827 struct octeontx_mmc_slot *slot;
3828 struct mmc_uclass_priv *upriv;
3829 ofnode node = dev->node;
3830 u32 bus_id;
3831 char name[16];
3832 int err;
3833
3834 debug("%s(%s) Pre-Probe\n", __func__, dev->name);
3835 if (ofnode_read_u32(node, "reg", &bus_id)) {
3836 pr_err("%s(%s): Error: \"reg\" not found in device tree\n",
3837 __func__, dev->name);
3838 return -1;
3839 }
3840 if (bus_id > OCTEONTX_MAX_MMC_SLOT) {
3841 pr_err("%s(%s): Error: \"reg\" out of range of 0..%d\n",
3842 __func__, dev->name, OCTEONTX_MAX_MMC_SLOT);
3843 return -1;
3844 }
3845
3846 slot = &host->slots[bus_id];
3847 dev->priv = slot;
3848 slot->host = host;
3849 slot->bus_id = bus_id;
3850 slot->dev = dev;
3851 slot->base_addr = host->base_addr;
3852 slot->is_asim = host->is_asim;
3853 slot->is_emul = host->is_emul;
3854
3855 snprintf(name, sizeof(name), "octeontx-mmc%d", bus_id);
3856 err = device_set_name(dev, name);
3857
3858 if (!dev->uclass_priv) {
3859 debug("%s(%s): Allocating uclass priv\n", __func__,
3860 dev->name);
3861 upriv = calloc(1, sizeof(struct mmc_uclass_priv));
3862 if (!upriv)
3863 return -ENOMEM;
3864 dev->uclass_priv = upriv;
3865 dev->uclass->priv = upriv;
3866 } else {
3867 upriv = dev->uclass_priv;
3868 }
3869
3870 upriv->mmc = &slot->mmc;
3871 debug("%s: uclass priv: %p, mmc: %p\n", dev->name, upriv, upriv->mmc);
3872
3873 debug("%s: ret: %d\n", __func__, err);
3874 return err;
3875}
3876
3877static const struct udevice_id octeontx_hsmmc_host_ids[] = {
3878 { .compatible = "cavium,thunder-8890-mmc" },
3879 { }
3880};
3881
3882U_BOOT_DRIVER(octeontx_hsmmc_host) = {
3883 .name = "octeontx_hsmmc_host",
3884 .id = UCLASS_MISC,
3885 .of_match = of_match_ptr(octeontx_hsmmc_host_ids),
3886 .probe = octeontx_mmc_host_probe,
3887 .priv_auto_alloc_size = sizeof(struct octeontx_mmc_host),
3888 .child_pre_probe = octeontx_mmc_host_child_pre_probe,
3889 .flags = DM_FLAG_PRE_RELOC,
3890};
3891
3892static struct pci_device_id octeontx_mmc_supported[] = {
3893 { PCI_VDEVICE(CAVIUM, PCI_DEVICE_ID_CAVIUM_EMMC) },
3894 { },
3895};
3896
3897U_BOOT_PCI_DEVICE(octeontx_hsmmc_host, octeontx_mmc_supported);