coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
sdhci.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Secure Digital (SD) Host Controller interface specific code
4  */
5 
6 #include "bouncebuf.h"
8 #include <commonlib/sdhci.h>
9 #include <commonlib/stdlib.h>
10 #include <commonlib/storage.h>
11 #include <delay.h>
12 #include <endian.h>
13 #include <lib.h>
14 #include "sdhci.h"
15 #include "sd_mmc.h"
16 #include "storage.h"
17 #include <timer.h>
18 
19 #define DMA_AVAILABLE ((CONFIG(SDHCI_ADMA_IN_BOOTBLOCK) && ENV_BOOTBLOCK) \
20  || (CONFIG(SDHCI_ADMA_IN_VERSTAGE) && ENV_SEPARATE_VERSTAGE) \
21  || (CONFIG(SDHCI_ADMA_IN_ROMSTAGE) && ENV_ROMSTAGE) \
22  || ENV_POSTCAR || ENV_RAMSTAGE)
23 
24 __weak void *dma_malloc(size_t length_in_bytes)
25 {
26  return malloc(length_in_bytes);
27 }
28 
30 {
31  struct stopwatch sw;
32 
33  /* Wait max 100 ms */
35 
38  if (stopwatch_expired(&sw)) {
39  sdhc_error("Reset 0x%x never completed.\n", (int)mask);
40  return;
41  }
42  udelay(1000);
43  }
44 }
45 
47 {
48  int i;
49  if (cmd->resp_type & CARD_RSP_136) {
50  /* CRC is stripped so we need to do some shifting. */
51  for (i = 0; i < 4; i++) {
53  SDHCI_RESPONSE + (3-i)*4) << 8;
54  if (i != 3)
55  cmd->response[i] |= sdhci_readb(sdhci_ctrlr,
56  SDHCI_RESPONSE + (3-i)*4-1);
57  }
58  sdhc_log_response(4, &cmd->response[0]);
59  sdhc_trace("Response: 0x%08x.%08x.%08x.%08x\n",
60  cmd->response[3], cmd->response[2], cmd->response[1],
61  cmd->response[0]);
62  } else {
64  sdhc_log_response(1, &cmd->response[0]);
65  sdhc_trace("Response: 0x%08x\n", cmd->response[0]);
66  }
67 }
68 
70  struct mmc_data *data, unsigned int start_addr)
71 {
72  uint32_t block_count;
74  uint32_t *buffer_end;
75  uint32_t ps;
76  uint32_t ps_mask;
77  uint32_t stat;
78  struct stopwatch sw;
79 
80  block_count = 0;
81  buffer = (uint32_t *)data->dest;
82  ps_mask = (data->flags & DATA_FLAG_READ)
85  do {
86  /* Stop transfers if there is an error */
89  if (stat & SDHCI_INT_ERROR) {
90  sdhc_error("Error detected in status(0x%X)!\n", stat);
91  return -1;
92  }
93 
94  /* Determine if the buffer is ready to move data */
96  if (!(ps & ps_mask)) {
97  if (stopwatch_expired(&sw)) {
98  sdhc_error("Transfer data timeout\n");
99  return -1;
100  }
101  udelay(1);
102  continue;
103  }
104 
105  /* Transfer a block of data */
106  buffer_end = &buffer[data->blocksize >> 2];
107  if (data->flags == DATA_FLAG_READ)
108  while (buffer_end > buffer)
110  SDHCI_BUFFER);
111  else
112  while (buffer_end > buffer)
114  SDHCI_BUFFER);
115  if (++block_count >= data->blocks)
116  break;
117  } while (!(stat & SDHCI_INT_DATA_END));
118  return 0;
119 }
120 
121 static int sdhci_send_command_bounced(struct sd_mmc_ctrlr *ctrlr,
122  struct mmc_command *cmd, struct mmc_data *data,
123  struct bounce_buffer *bbstate)
124 {
125  struct sdhci_ctrlr *sdhci_ctrlr = (struct sdhci_ctrlr *)ctrlr;
126  u16 mode = 0;
127  unsigned int stat = 0;
128  int ret = 0;
129  u32 mask, flags;
130  unsigned int timeout, start_addr = 0;
131  struct stopwatch sw;
132 
133  /* Wait max 1 s */
134  timeout = 1000;
135 
138 
139  /* We shouldn't wait for data inhibit for stop commands, even
140  though they might use busy signaling */
141  if (cmd->flags & CMD_FLAG_IGNORE_INHIBIT)
143 
145  if (timeout == 0) {
146  sdhc_trace("Cmd: %2d, Arg: 0x%08x, not sent\n",
147  cmd->cmdidx, cmd->cmdarg);
148  sdhc_error("Controller never released inhibit bit(s), "
149  "present state %#8.8x.\n",
151  return CARD_COMM_ERR;
152  }
153  timeout--;
154  udelay(1000);
155  }
156 
158  if (!(cmd->resp_type & CARD_RSP_PRESENT))
159  flags = SDHCI_CMD_RESP_NONE;
160  else if (cmd->resp_type & CARD_RSP_136)
161  flags = SDHCI_CMD_RESP_LONG;
162  else if (cmd->resp_type & CARD_RSP_BUSY) {
165  } else
166  flags = SDHCI_CMD_RESP_SHORT;
167 
168  if (cmd->resp_type & CARD_RSP_CRC)
169  flags |= SDHCI_CMD_CRC;
170  if (cmd->resp_type & CARD_RSP_OPCODE)
171  flags |= SDHCI_CMD_INDEX;
172  if (data)
173  flags |= SDHCI_CMD_DATA;
174 
175  /* Set Transfer mode regarding to data flag */
176  if (data) {
179  data->blocksize), SDHCI_BLOCK_SIZE);
180 
181  if (data->flags == DATA_FLAG_READ)
182  mode |= SDHCI_TRNS_READ;
183 
184  if (data->blocks > 1)
185  mode |= SDHCI_TRNS_BLK_CNT_EN |
187 
189 
190  if (DMA_AVAILABLE && (ctrlr->caps & DRVR_CAP_AUTO_CMD12)
191  && (cmd->cmdidx != MMC_CMD_AUTO_TUNING_SEQUENCE)) {
192  if (sdhci_setup_adma(sdhci_ctrlr, data))
193  return -1;
194  mode |= SDHCI_TRNS_DMA;
195  }
197  }
198 
199  sdhc_trace("Cmd: %2d, Arg: 0x%08x\n", cmd->cmdidx, cmd->cmdarg);
202  SDHCI_COMMAND);
204 
205  if (DMA_AVAILABLE && (mode & SDHCI_TRNS_DMA))
206  return sdhci_complete_adma(sdhci_ctrlr, cmd);
207 
208  stopwatch_init_msecs_expire(&sw, 2550);
209  do {
211  if (stat & SDHCI_INT_ERROR) {
212  sdhc_trace("Error - IntStatus: 0x%08x\n", stat);
213  break;
214  }
215 
216  if (stat & SDHCI_INT_DATA_AVAIL) {
218  return 0;
219  }
220 
221  /* Apply max timeout for R1b-type CMD defined in eMMC ext_csd
222  except for erase ones */
223  if (stopwatch_expired(&sw)) {
224  if (ctrlr->caps & DRVR_CAP_BROKEN_R1B)
225  return 0;
226  sdhc_error(
227  "Timeout for status update! IntStatus: 0x%08x\n",
228  stat);
229  return CARD_TIMEOUT;
230  }
231  } while ((stat & mask) != mask);
232 
233  if ((stat & (SDHCI_INT_ERROR | mask)) == mask) {
234  if (cmd->cmdidx)
237  } else
238  ret = -1;
239 
240  if (!ret && data)
241  ret = sdhci_transfer_data(sdhci_ctrlr, data, start_addr);
242 
243  if (ctrlr->udelay_wait_after_cmd)
245 
248 
249  if (!ret)
250  return 0;
251 
254  if (stat & SDHCI_INT_TIMEOUT) {
255  sdhc_error("CMD%d timeout, IntStatus: 0x%08x\n", cmd->cmdidx,
256  stat);
257  return CARD_TIMEOUT;
258  }
259 
260  sdhc_error("CMD%d failed, IntStatus: 0x%08x\n", cmd->cmdidx, stat);
261  return CARD_COMM_ERR;
262 }
263 
265 {
266 }
267 
269 {
270 }
271 
273  uint32_t *response)
274 {
275 }
276 
277 __weak void sdhc_log_ret(int ret)
278 {
279 }
280 
281 static void sdhci_led_control(struct sd_mmc_ctrlr *ctrlr, int on)
282 {
283  uint8_t host_ctrl;
284  struct sdhci_ctrlr *sdhci_ctrlr = (struct sdhci_ctrlr *)ctrlr;
285 
287  host_ctrl &= ~SDHCI_CTRL_LED;
288  if (on)
289  host_ctrl |= SDHCI_CTRL_LED;
291 }
292 
293 static int sdhci_send_command(struct sd_mmc_ctrlr *ctrlr,
294  struct mmc_command *cmd, struct mmc_data *data)
295 {
296  void *buf;
297  unsigned int bbflags;
298  size_t len;
299  struct bounce_buffer *bbstate = NULL;
300  struct bounce_buffer bbstate_val;
301  int ret;
302 
303  sdhc_log_command(cmd);
304 
305  if (CONFIG(SDHCI_BOUNCE_BUFFER) && data) {
306  if (data->flags & DATA_FLAG_READ) {
307  buf = data->dest;
308  bbflags = GEN_BB_WRITE;
309  } else {
310  buf = (void *)data->src;
311  bbflags = GEN_BB_READ;
312  }
313  len = data->blocks * data->blocksize;
314 
315  /*
316  * on some platform(like rk3399 etc) need to worry about
317  * cache coherency, so check the buffer, if not dma
318  * coherent, use bounce_buffer to do DMA management.
319  */
320  if (!dma_coherent(buf)) {
321  bbstate = &bbstate_val;
322  if (bounce_buffer_start(bbstate, buf, len, bbflags)) {
323  sdhc_error(
324  "ERROR: Failed to get bounce buffer.\n");
325  return -1;
326  }
327  }
328  }
329 
330  sdhci_led_control(ctrlr, 1);
331  ret = sdhci_send_command_bounced(ctrlr, cmd, data, bbstate);
332  sdhci_led_control(ctrlr, 0);
333  sdhc_log_ret(ret);
334 
335  if (CONFIG(SDHCI_BOUNCE_BUFFER) && bbstate)
336  bounce_buffer_stop(bbstate);
337 
338  return ret;
339 }
340 
341 static int sdhci_set_clock(struct sdhci_ctrlr *sdhci_ctrlr, unsigned int clock)
342 {
343  struct sd_mmc_ctrlr *ctrlr = &sdhci_ctrlr->sd_mmc_ctrlr;
344  unsigned int actual, div, clk, timeout;
345 
346  /* Turn off the clock if requested */
347  actual = clock;
348  if (actual == 0) {
350  sdhc_debug("SDHCI bus clock: Off\n");
351  return 0;
352  }
353 
354  /* Compute the divisor for the new clock frequency */
355  actual = MIN(actual, ctrlr->f_max);
356  actual = MAX(actual, ctrlr->f_min);
357  if (ctrlr->clock_base <= actual)
358  div = 0;
359  else {
360  /* Version 3.00 divisors must be a multiple of 2. */
361  if ((ctrlr->version & SDHCI_SPEC_VER_MASK)
362  >= SDHCI_SPEC_300) {
363  div = MIN(((ctrlr->clock_base + actual - 1)
364  / actual), SDHCI_MAX_DIV_SPEC_300);
365  actual = ctrlr->clock_base / div;
366  div += 1;
367  } else {
368  /* Version 2.00 divisors must be a power of 2. */
369  for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
370  if ((ctrlr->clock_base / div) <= actual)
371  break;
372  }
373  actual = ctrlr->clock_base / div;
374  }
375  div >>= 1;
376  }
377 
378  /* Set the new clock frequency */
379  if (actual != ctrlr->bus_hz) {
380  /* Turn off the clock */
382 
383  /* Set the new clock frequency */
384  clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
385  clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
387  clk |= SDHCI_CLOCK_INT_EN;
389 
390  /* Display the requested clock frequency */
391  sdhc_debug("SDHCI bus clock: %d.%03d MHz\n",
392  actual / 1000000,
393  (actual / 1000) % 1000);
394 
395  /* Wait max 20 ms */
396  timeout = 20;
397  while (!((clk = sdhci_readw(sdhci_ctrlr, SDHCI_CLOCK_CONTROL))
399  if (timeout == 0) {
400  sdhc_error(
401  "Internal clock never stabilised.\n");
402  return -1;
403  }
404  timeout--;
405  udelay(1000);
406  }
407 
408  clk |= SDHCI_CLOCK_CARD_EN;
410  ctrlr->bus_hz = actual;
411  }
412  return 0;
413 }
414 
416  unsigned short power)
417 {
418  struct sd_mmc_ctrlr *ctrlr = &sdhci_ctrlr->sd_mmc_ctrlr;
419  u8 pwr = 0;
420  u8 pwr_ctrl;
421  const char *voltage;
422 
423  if (power != (unsigned short)-1) {
424  switch (1 << power) {
425  case MMC_VDD_165_195:
426  voltage = "1.8";
427  pwr = SDHCI_POWER_180;
428  break;
429  case MMC_VDD_29_30:
430  case MMC_VDD_30_31:
431  voltage = "3.0";
432  pwr = SDHCI_POWER_300;
433  break;
434  case MMC_VDD_32_33:
435  case MMC_VDD_33_34:
436  voltage = "3.3";
437  pwr = SDHCI_POWER_330;
438  break;
439  }
440  }
441 
442  /* Determine the power state */
444  if (pwr == 0) {
445  if (pwr_ctrl & SDHCI_POWER_ON)
446  sdhc_debug("SDHCI voltage: Off\n");
448  return;
449  }
450 
451  /* Determine if the power has changed */
452  if (pwr_ctrl != (pwr | SDHCI_POWER_ON)) {
453  sdhc_debug("SDHCI voltage: %s Volts\n", voltage);
454 
455  /* Select the voltage */
458 
459  /* Apply power to the SD/MMC device */
460  pwr |= SDHCI_POWER_ON;
462  }
463 }
464 
466  0, /* 0: BUS_TIMING_LEGACY */
467  0, /* 1: BUS_TIMING_MMC_HS */
468  0, /* 2: BUS_TIMING_SD_HS */
469  SDHCI_CTRL_UHS_SDR12 | SDHCI_CTRL_VDD_180, /* 3: BUS_TIMING_UHS_SDR12 */
470  SDHCI_CTRL_UHS_SDR25 | SDHCI_CTRL_VDD_180, /* 4: BUS_TIMING_UHS_SDR25 */
471  SDHCI_CTRL_UHS_SDR50 | SDHCI_CTRL_VDD_180, /* 5: BUS_TIMING_UHS_SDR50 */
472  /* 6: BUS_TIMING_UHS_SDR104 */
474  SDHCI_CTRL_UHS_DDR50 | SDHCI_CTRL_VDD_180, /* 7: BUS_TIMING_UHS_DDR50 */
475  SDHCI_CTRL_UHS_DDR50 | SDHCI_CTRL_VDD_180, /* 8: BUS_TIMING_MMC_DDR52 */
476  /* 9: BUS_TIMING_MMC_HS200 */
478  /* 10: BUS_TIMING_MMC_HS400 */
480  /* 11: BUS_TIMING_MMC_HS400ES */
482 };
483 
486 {
487  u16 ctrl_2;
488 
489  /* Select bus speed mode, driver and VDD 1.8 volt support */
494  ctrl_2 |= speed_driver_voltage[timing];
496 }
497 
498 static void sdhci_set_ios(struct sd_mmc_ctrlr *ctrlr)
499 {
500  struct sdhci_ctrlr *sdhci_ctrlr = (struct sdhci_ctrlr *)ctrlr;
501  u32 ctrl;
502  u32 previous_ctrl;
503  u32 bus_width;
504  int version;
505 
506  /* Set the clock frequency */
507  if (ctrlr->bus_hz != ctrlr->request_hz)
509 
510  /* Switch to 1.8 volt for HS200 */
511  if (ctrlr->caps & DRVR_CAP_1V8_VDD)
512  if (ctrlr->bus_hz == CLOCK_200MHZ)
514 
515  /* Determine the new bus width */
516  bus_width = 1;
518  previous_ctrl = ctrl;
519  ctrl &= ~SDHCI_CTRL_4BITBUS;
521  if (version >= SDHCI_SPEC_300)
522  ctrl &= ~SDHCI_CTRL_8BITBUS;
523 
524  if ((ctrlr->bus_width == 8) && (version >= SDHCI_SPEC_300)) {
525  ctrl |= SDHCI_CTRL_8BITBUS;
526  bus_width = 8;
527  } else if (ctrlr->bus_width == 4) {
528  ctrl |= SDHCI_CTRL_4BITBUS;
529  bus_width = 4;
530  }
531 
532  if (!(ctrlr->timing == BUS_TIMING_LEGACY) &&
533  !(ctrlr->caps & DRVR_CAP_NO_HISPD_BIT))
534  ctrl |= SDHCI_CTRL_HISPD;
535  else
536  ctrl &= ~SDHCI_CTRL_HISPD;
537 
539 
540  if (DMA_AVAILABLE) {
541  if (ctrlr->caps & DRVR_CAP_AUTO_CMD12) {
542  ctrl &= ~SDHCI_CTRL_DMA_MASK;
543  if (ctrlr->caps & DRVR_CAP_DMA_64BIT)
544  ctrl |= SDHCI_CTRL_ADMA64;
545  else
546  ctrl |= SDHCI_CTRL_ADMA32;
547  }
548  }
549 
550  /* Set the new bus width */
551  if (CONFIG(SDHC_DEBUG)
552  && ((ctrl ^ previous_ctrl) & (SDHCI_CTRL_4BITBUS
553  | ((version >= SDHCI_SPEC_300) ? SDHCI_CTRL_8BITBUS : 0))))
554  sdhc_debug("SDHCI bus width: %d bit%s\n", bus_width,
555  (bus_width != 1) ? "s" : "");
557 }
558 
559 static void sdhci_tuning_start(struct sd_mmc_ctrlr *ctrlr, int retune)
560 {
561  uint16_t host_ctrl2;
562  struct sdhci_ctrlr *sdhci_ctrlr = (struct sdhci_ctrlr *)ctrlr;
563 
564  /* Start the bus tuning */
566  host_ctrl2 &= ~SDHCI_CTRL_TUNED_CLK;
567  host_ctrl2 |= (retune ? SDHCI_CTRL_TUNED_CLK : 0)
570 }
571 
572 static int sdhci_is_tuning_complete(struct sd_mmc_ctrlr *ctrlr, int *successful)
573 {
574  uint16_t host_ctrl2;
575  struct sdhci_ctrlr *sdhci_ctrlr = (struct sdhci_ctrlr *)ctrlr;
576 
577  /* Determine if the bus tuning has completed */
579  *successful = ((host_ctrl2 & SDHCI_CTRL_TUNED_CLK) != 0);
580  return ((host_ctrl2 & SDHCI_CTRL_EXEC_TUNING) == 0);
581 }
582 
583 /* Prepare SDHCI controller to be initialized */
585 {
586  struct sd_mmc_ctrlr *ctrlr = &sdhci_ctrlr->sd_mmc_ctrlr;
587  unsigned int caps, caps_1;
588 
589  /* Get controller version and capabilities */
593 
594  /* Determine the supported voltages */
595  if (caps & SDHCI_CAN_VDD_330)
597  if (caps & SDHCI_CAN_VDD_300)
599  if (caps & SDHCI_CAN_VDD_180)
600  ctrlr->voltages |= MMC_VDD_165_195;
601 
602  /* Get the controller's base clock frequency */
603  if ((ctrlr->version & SDHCI_SPEC_VER_MASK) >= SDHCI_SPEC_300)
606  else
609  ctrlr->clock_base *= 1000000;
610  ctrlr->f_max = ctrlr->clock_base;
611 
612  /* Determine the controller's clock frequency range */
613  ctrlr->f_min = 0;
614  if ((ctrlr->version & SDHCI_SPEC_VER_MASK) >= SDHCI_SPEC_300)
615  ctrlr->f_min =
617  else
618  ctrlr->f_min =
620 
621  /* Determine the controller's modes of operation */
622  ctrlr->caps |= DRVR_CAP_HS52 | DRVR_CAP_HS;
623  if (ctrlr->clock_base >= CLOCK_200MHZ) {
625  if (caps_1 & SDHCI_SUPPORT_HS400)
626  ctrlr->caps |= DRVR_CAP_HS400
628  }
629 
630  /* Determine the bus widths the controller supports */
631  ctrlr->caps |= DRVR_CAP_4BIT;
632  if (caps & SDHCI_CAN_DO_8BIT)
633  ctrlr->caps |= DRVR_CAP_8BIT;
634 
635  /* Determine the controller's DMA support */
636  if (caps & SDHCI_CAN_DO_ADMA2)
637  ctrlr->caps |= DRVR_CAP_AUTO_CMD12;
639  ctrlr->caps |= DRVR_CAP_DMA_64BIT;
640 
641  /* Specify the modes that the driver stack supports */
642  ctrlr->caps |= DRVR_CAP_HC;
643 
644  /* Let the SOC adjust the configuration to handle controller quirks */
646  if (ctrlr->clock_base == 0) {
647  sdhc_error("Hardware doesn't specify base clock frequency\n");
648  return -1;
649  }
650  if (!ctrlr->f_max)
651  ctrlr->f_max = ctrlr->clock_base;
652 
653  /* Display the results */
654  sdhc_trace("0x%08x: ctrlr->caps\n", ctrlr->caps);
655  sdhc_trace("%d.%03d MHz: ctrlr->clock_base\n",
656  ctrlr->clock_base / 1000000,
657  (ctrlr->clock_base / 1000) % 1000);
658  sdhc_trace("%d.%03d MHz: ctrlr->f_max\n",
659  ctrlr->f_max / 1000000,
660  (ctrlr->f_max / 1000) % 1000);
661  sdhc_trace("%d.%03d MHz: ctrlr->f_min\n",
662  ctrlr->f_min / 1000000,
663  (ctrlr->f_min / 1000) % 1000);
664  sdhc_trace("0x%08x: ctrlr->voltages\n", ctrlr->voltages);
665 
667 
668  return 0;
669 }
670 
672  *ctrlr)
673 {
674 }
675 
676 static int sdhci_init(struct sdhci_ctrlr *sdhci_ctrlr)
677 {
678  struct sd_mmc_ctrlr *ctrlr = &sdhci_ctrlr->sd_mmc_ctrlr;
679  int rv;
680 
681  /* Only initialize the controller upon reset or card insertion */
682  if (ctrlr->initialized)
683  return 0;
684 
685  sdhc_debug("SDHCI Controller Base Address: %p\n",
687 
689  if (rv)
690  return rv; /* The error has been already reported */
691 
693 
694  if (ctrlr->caps & DRVR_CAP_NO_CD) {
695  unsigned int status;
696 
699 
701  while ((!(status & SDHCI_CARD_PRESENT)) ||
702  (!(status & SDHCI_CARD_STATE_STABLE)) ||
703  (!(status & SDHCI_CARD_DETECT_PIN_LEVEL)))
705  }
706 
707  /* Enable only interrupts served by the SD controller */
710  /* Mask all sdhci interrupt sources */
712 
713  /* Set timeout to maximum, shouldn't happen if everything's right. */
715 
716  mdelay(10);
717  ctrlr->initialized = 1;
718  return 0;
719 }
720 
722 {
723  struct sd_mmc_ctrlr *ctrlr = &sdhci_ctrlr->sd_mmc_ctrlr;
724 
725  if (ctrlr->caps & DRVR_CAP_REMOVABLE) {
726  int present = (sdhci_readl(sdhci_ctrlr, SDHCI_PRESENT_STATE) &
727  SDHCI_CARD_PRESENT) != 0;
728 
729  if (!present) {
730  /* A card was present indicate the controller needs
731  * initialization on the next call.
732  */
733  ctrlr->initialized = 0;
734  return 0;
735  }
736  }
737 
738  /* A card is present, get it ready. */
739  if (sdhci_init(sdhci_ctrlr))
740  return -1;
741  return 0;
742 }
743 
745 {
746  struct sd_mmc_ctrlr *ctrlr = &sdhci_ctrlr->sd_mmc_ctrlr;
747 
748  /* Update the routine pointers */
749  ctrlr->send_cmd = &sdhci_send_command;
750  ctrlr->set_ios = &sdhci_set_ios;
753 }
754 
756 {
757  struct sd_mmc_ctrlr *ctrlr = &sdhci_ctrlr->sd_mmc_ctrlr;
758 
760 
761  /* TODO(vbendeb): check if SDHCI spec allows to retrieve this value. */
762  ctrlr->b_max = 65535;
763 
764  /* Initialize the SDHC controller */
765  return sdhci_update(sdhci_ctrlr);
766 }
int bounce_buffer_start(struct bounce_buffer *state, void *data, size_t len, unsigned int flags)
bounce_buffer_start() – Start the bounce buffer session state: stores state passed between bounce_buf...
Definition: bouncebuf.c:32
int bounce_buffer_stop(struct bounce_buffer *state)
bounce_buffer_stop() – Finish the bounce buffer session state: stores state passed between bounce_buf...
Definition: bouncebuf.c:61
#define GEN_BB_READ
Definition: bouncebuf.h:20
#define GEN_BB_WRITE
Definition: bouncebuf.h:28
#define ARRAY_SIZE(a)
Definition: helpers.h:12
#define MIN(a, b)
Definition: helpers.h:37
#define MAX(a, b)
Definition: helpers.h:40
int dma_coherent(void *ptr)
void mdelay(unsigned int msecs)
Definition: delay.c:2
@ CONFIG
Definition: dsi_common.h:201
#define DRVR_CAP_BROKEN_R1B
Definition: sdhci.h:13
#define DRVR_CAP_1V8_VDD
Definition: sdhci.h:11
#define DRVR_CAP_NO_SIMULT_VDD_AND_POWER
Definition: sdhci.h:16
#define DRVR_CAP_NO_HISPD_BIT
Definition: sdhci.h:15
#define DRVR_CAP_NO_CD
Definition: sdhci.h:14
void * malloc(size_t size)
Definition: malloc.c:53
static int stopwatch_expired(struct stopwatch *sw)
Definition: timer.h:152
static void stopwatch_init_msecs_expire(struct stopwatch *sw, long ms)
Definition: timer.h:133
unsigned int version[2]
Definition: edid.c:55
unsigned int voltage
Definition: edid.c:62
static int __fls(u32 x)
Definition: lib.h:57
static uint8_t * buf
Definition: uart.c:7
static const struct pnpconfig power[]
Definition: pnpconfig.c:14
u8 buffer[C2P_BUFFER_MAXSIZE]
Definition: psp_smm.c:18
const struct smm_save_state_ops *legacy_ops __weak
Definition: save_state.c:8
#define sdhc_trace(format...)
Definition: sd_mmc.h:69
#define sdhc_debug(format...)
Definition: sd_mmc.h:64
#define sdhc_error(format...)
Definition: sd_mmc.h:74
#define DRVR_CAP_HS200_TUNING
Definition: sd_mmc_ctrlr.h:183
#define CLOCK_200MHZ
Definition: sd_mmc_ctrlr.h:164
#define CARD_RSP_BUSY
Definition: sd_mmc_ctrlr.h:72
#define DRVR_CAP_HS200
Definition: sd_mmc_ctrlr.h:178
#define CARD_RSP_136
Definition: sd_mmc_ctrlr.h:70
#define CMD_FLAG_IGNORE_INHIBIT
Definition: sd_mmc_ctrlr.h:94
#define DRVR_CAP_4BIT
Definition: sd_mmc_ctrlr.h:172
#define DATA_FLAG_READ
Definition: sd_mmc_ctrlr.h:113
#define DRVR_CAP_HS52
Definition: sd_mmc_ctrlr.h:177
#define MMC_VDD_33_34
Definition: sd_mmc_ctrlr.h:145
#define DRVR_CAP_8BIT
Definition: sd_mmc_ctrlr.h:173
#define DRVR_CAP_REMOVABLE
Definition: sd_mmc_ctrlr.h:181
#define MMC_VDD_30_31
Definition: sd_mmc_ctrlr.h:142
#define DRVR_CAP_ENHANCED_STROBE
Definition: sd_mmc_ctrlr.h:180
#define CARD_TIMEOUT
Definition: sd_mmc_ctrlr.h:13
#define CARD_COMM_ERR
Definition: sd_mmc_ctrlr.h:12
#define CARD_RSP_PRESENT
Definition: sd_mmc_ctrlr.h:69
#define DRVR_CAP_DMA_64BIT
Definition: sd_mmc_ctrlr.h:182
#define BUS_TIMING_LEGACY
Definition: sd_mmc_ctrlr.h:188
#define MMC_CMD_AUTO_TUNING_SEQUENCE
Definition: sd_mmc_ctrlr.h:48
#define DRVR_CAP_AUTO_CMD12
Definition: sd_mmc_ctrlr.h:174
#define MMC_VDD_32_33
Definition: sd_mmc_ctrlr.h:144
#define DRVR_CAP_HS
Definition: sd_mmc_ctrlr.h:176
#define CARD_RSP_CRC
Definition: sd_mmc_ctrlr.h:71
#define MMC_VDD_29_30
Definition: sd_mmc_ctrlr.h:141
#define DRVR_CAP_HC
Definition: sd_mmc_ctrlr.h:175
#define MMC_VDD_165_195_SHIFT
Definition: sd_mmc_ctrlr.h:149
#define DRVR_CAP_HS400
Definition: sd_mmc_ctrlr.h:179
#define MMC_VDD_165_195
Definition: sd_mmc_ctrlr.h:131
#define CARD_RSP_OPCODE
Definition: sd_mmc_ctrlr.h:73
void sdhci_reset(struct sdhci_ctrlr *sdhci_ctrlr, u8 mask)
Definition: sdhci.c:29
const u16 speed_driver_voltage[]
Definition: sdhci.c:465
static int sdhci_update(struct sdhci_ctrlr *sdhci_ctrlr)
Definition: sdhci.c:721
void sdhci_cmd_done(struct sdhci_ctrlr *sdhci_ctrlr, struct mmc_command *cmd)
Definition: sdhci.c:46
void sdhci_update_pointers(struct sdhci_ctrlr *sdhci_ctrlr)
Definition: sdhci.c:744
static void sdhci_set_uhs_signaling(struct sdhci_ctrlr *sdhci_ctrlr, uint32_t timing)
Definition: sdhci.c:484
static int sdhci_pre_init(struct sdhci_ctrlr *sdhci_ctrlr)
Definition: sdhci.c:584
__weak void sdhc_log_command(struct mmc_command *cmd)
Definition: sdhci.c:264
static void sdhci_set_power(struct sdhci_ctrlr *sdhci_ctrlr, unsigned short power)
Definition: sdhci.c:415
__weak void soc_sd_mmc_controller_quirks(struct sd_mmc_ctrlr *ctrlr)
Definition: sdhci.c:671
static void sdhci_tuning_start(struct sd_mmc_ctrlr *ctrlr, int retune)
Definition: sdhci.c:559
static int sdhci_init(struct sdhci_ctrlr *sdhci_ctrlr)
Definition: sdhci.c:676
static int sdhci_send_command(struct sd_mmc_ctrlr *ctrlr, struct mmc_command *cmd, struct mmc_data *data)
Definition: sdhci.c:293
static void sdhci_led_control(struct sd_mmc_ctrlr *ctrlr, int on)
Definition: sdhci.c:281
int add_sdhci(struct sdhci_ctrlr *sdhci_ctrlr)
Definition: sdhci.c:755
#define DMA_AVAILABLE
Definition: sdhci.c:19
__weak void * dma_malloc(size_t length_in_bytes)
Definition: sdhci.c:24
static void sdhci_set_ios(struct sd_mmc_ctrlr *ctrlr)
Definition: sdhci.c:498
__weak void sdhc_log_response(uint32_t entries, uint32_t *response)
Definition: sdhci.c:272
__weak void sdhc_log_ret(int ret)
Definition: sdhci.c:277
__weak void sdhc_log_command_issued(void)
Definition: sdhci.c:268
static int sdhci_set_clock(struct sdhci_ctrlr *sdhci_ctrlr, unsigned int clock)
Definition: sdhci.c:341
static int sdhci_send_command_bounced(struct sd_mmc_ctrlr *ctrlr, struct mmc_command *cmd, struct mmc_data *data, struct bounce_buffer *bbstate)
Definition: sdhci.c:121
static int sdhci_is_tuning_complete(struct sd_mmc_ctrlr *ctrlr, int *successful)
Definition: sdhci.c:572
static int sdhci_transfer_data(struct sdhci_ctrlr *sdhci_ctrlr, struct mmc_data *data, unsigned int start_addr)
Definition: sdhci.c:69
static const int mask[4]
Definition: gpio.c:308
#define NULL
Definition: stddef.h:19
unsigned short uint16_t
Definition: stdint.h:11
unsigned int uint32_t
Definition: stdint.h:14
uint32_t u32
Definition: stdint.h:51
uint16_t u16
Definition: stdint.h:48
uint8_t u8
Definition: stdint.h:45
unsigned char uint8_t
Definition: stdint.h:8
#define SDHCI_TRNS_ACMD12
Definition: sdhci.h:24
#define SDHCI_CLOCK_INT_STABLE
Definition: sdhci.h:92
#define SDHCI_CMD_RESP_NONE
Definition: sdhci.h:35
#define SDHCI_CAN_DO_ADMA2
Definition: sdhci.h:167
#define SDHCI_CARD_STATE_STABLE
Definition: sdhci.h:55
#define SDHCI_CTRL_UHS_DDR50
Definition: sdhci.h:145
#define SDHCI_HOST_VERSION
Definition: sdhci.h:196
#define SDHCI_CTRL_UHS_SDR25
Definition: sdhci.h:142
#define SDHCI_BUFFER
Definition: sdhci.h:45
#define SDHCI_CAN_DO_8BIT
Definition: sdhci.h:166
#define SDHCI_RESET_CMD
Definition: sdhci.h:99
#define SDHCI_CTRL_EXEC_TUNING
Definition: sdhci.h:153
#define SDHCI_CTRL_UHS_SDR12
Definition: sdhci.h:141
#define SDHCI_MAX_DIV_SPEC_300
Definition: sdhci.h:210
#define SDHCI_ARGUMENT
Definition: sdhci.h:19
#define SDHCI_TIMEOUT_CONTROL
Definition: sdhci.h:95
#define SDHCI_INT_DATA_MASK
Definition: sdhci.h:131
#define SDHCI_POWER_ON
Definition: sdhci.h:73
#define SDHCI_CAN_VDD_330
Definition: sdhci.h:171
static void sdhci_writew(struct sdhci_ctrlr *sdhci_ctrlr, u16 val, int reg)
Definition: sdhci.h:234
#define SDHCI_SOFTWARE_RESET
Definition: sdhci.h:97
#define SDHCI_CMD_RESP_LONG
Definition: sdhci.h:36
#define SDHCI_CTRL_CD_TEST_INS
Definition: sdhci.h:69
#define SDHCI_CLOCK_INT_EN
Definition: sdhci.h:93
#define SDHCI_CTRL_4BITBUS
Definition: sdhci.h:61
#define SDHCI_CTRL_UHS_SDR50
Definition: sdhci.h:143
#define SDHCI_CTRL_TUNED_CLK
Definition: sdhci.h:154
#define SDHCI_CTRL_UHS_MASK
Definition: sdhci.h:140
#define SDHCI_SPEC_300
Definition: sdhci.h:203
#define SDHCI_CLOCK_BASE_SHIFT
Definition: sdhci.h:163
int sdhci_setup_adma(struct sdhci_ctrlr *sdhci_ctrlr, struct mmc_data *data)
Definition: sdhci_adma.c:69
#define SDHCI_TRANSFER_MODE
Definition: sdhci.h:21
#define SDHCI_CTRL_CD_TEST
Definition: sdhci.h:70
#define SDHCI_HOST_CONTROL
Definition: sdhci.h:59
#define SDHCI_CTRL_VDD_180
Definition: sdhci.h:147
#define SDHCI_CMD_INDEX
Definition: sdhci.h:31
#define SDHCI_SPEC_VER_MASK
Definition: sdhci.h:199
#define SDHCI_SPACE_AVAILABLE
Definition: sdhci.h:52
int sdhci_complete_adma(struct sdhci_ctrlr *sdhci_ctrlr, struct mmc_command *cmd)
Definition: sdhci_adma.c:132
#define SDHCI_CTRL_DMA_MASK
Definition: sdhci.h:63
#define SDHCI_POWER_330
Definition: sdhci.h:76
#define SDHCI_DIV_MASK
Definition: sdhci.h:88
#define SDHCI_POWER_300
Definition: sdhci.h:75
#define SDHCI_TRNS_READ
Definition: sdhci.h:25
#define SDHCI_CLOCK_CONTROL
Definition: sdhci.h:85
#define SDHCI_MAX_DIV_SPEC_200
Definition: sdhci.h:209
#define SDHCI_CLOCK_V3_BASE_MASK
Definition: sdhci.h:162
#define SDHCI_CMD_RESP_SHORT
Definition: sdhci.h:37
#define SDHCI_CAN_64BIT
Definition: sdhci.h:174
#define SDHCI_HOST_CONTROL2
Definition: sdhci.h:139
static u8 sdhci_readb(struct sdhci_ctrlr *sdhci_ctrlr, int reg)
Definition: sdhci.h:255
#define SDHCI_CTRL_ADMA64
Definition: sdhci.h:67
static u32 sdhci_readl(struct sdhci_ctrlr *sdhci_ctrlr, int reg)
Definition: sdhci.h:245
#define SDHCI_SUPPORT_HS400
Definition: sdhci.h:177
#define SDHCI_INT_RESPONSE
Definition: sdhci.h:105
#define SDHCI_CTRL_LED
Definition: sdhci.h:60
#define SDHCI_DIV_MASK_LEN
Definition: sdhci.h:89
#define SDHCI_INT_DATA_AVAIL
Definition: sdhci.h:109
#define SDHCI_RESET_ALL
Definition: sdhci.h:98
#define SDHCI_INT_ERROR
Definition: sdhci.h:113
#define SDHCI_RESPONSE
Definition: sdhci.h:43
#define SDHCI_CTRL_HISPD
Definition: sdhci.h:62
#define SDHCI_MAKE_CMD(c, f)
Definition: sdhci.h:40
#define SDHCI_INT_ENABLE
Definition: sdhci.h:103
#define SDHCI_CAN_VDD_180
Definition: sdhci.h:173
#define SDHCI_CMD_CRC
Definition: sdhci.h:30
#define SDHCI_DATA_AVAILABLE
Definition: sdhci.h:53
#define SDHCI_DATA_INHIBIT
Definition: sdhci.h:49
#define SDHCI_TRNS_BLK_CNT_EN
Definition: sdhci.h:23
static u16 sdhci_readw(struct sdhci_ctrlr *sdhci_ctrlr, int reg)
Definition: sdhci.h:250
#define SDHCI_CMD_RESP_SHORT_BUSY
Definition: sdhci.h:38
#define SDHCI_BLOCK_COUNT
Definition: sdhci.h:17
#define SDHCI_DEFAULT_BOUNDARY_ARG
Definition: sdhci.h:216
#define SDHCI_CTRL_8BITBUS
Definition: sdhci.h:68
#define SDHCI_INT_STATUS
Definition: sdhci.h:102
#define SDHCI_CTRL_HS400
Definition: sdhci.h:146
#define SDHCI_CTRL_UHS_SDR104
Definition: sdhci.h:144
#define SDHCI_CAPABILITIES_1
Definition: sdhci.h:176
#define SDHCI_CMD_INHIBIT
Definition: sdhci.h:48
#define SDHCI_CLOCK_CARD_EN
Definition: sdhci.h:91
#define SDHCI_CTRL_DRV_TYPE_A
Definition: sdhci.h:150
#define SDHCI_CARD_PRESENT
Definition: sdhci.h:54
#define SDHCI_COMMAND
Definition: sdhci.h:28
#define SDHCI_DIVIDER_HI_SHIFT
Definition: sdhci.h:87
#define SDHCI_PRESENT_STATE
Definition: sdhci.h:47
#define SDHCI_RESET_DATA
Definition: sdhci.h:100
#define SDHCI_CAN_VDD_300
Definition: sdhci.h:172
#define SDHCI_INT_TIMEOUT
Definition: sdhci.h:114
#define SDHCI_CARD_DETECT_PIN_LEVEL
Definition: sdhci.h:56
static void sdhci_writel(struct sdhci_ctrlr *sdhci_ctrlr, u32 val, int reg)
Definition: sdhci.h:228
#define SDHCI_INT_CMD_MASK
Definition: sdhci.h:128
#define SDHCI_DIV_HI_MASK
Definition: sdhci.h:90
#define SDHCI_SIGNAL_ENABLE
Definition: sdhci.h:104
#define SDHCI_INT_ALL_MASK
Definition: sdhci.h:135
#define SDHCI_CMD_DATA
Definition: sdhci.h:32
#define SDHCI_CAPABILITIES
Definition: sdhci.h:157
#define SDHCI_MAKE_BLKSZ(dma, blksz)
Definition: sdhci.h:15
static void sdhci_writeb(struct sdhci_ctrlr *sdhci_ctrlr, u8 val, int reg)
Definition: sdhci.h:240
#define SDHCI_CTRL_ADMA32
Definition: sdhci.h:66
#define SDHCI_BLOCK_SIZE
Definition: sdhci.h:14
#define SDHCI_TRNS_DMA
Definition: sdhci.h:22
#define SDHCI_POWER_CONTROL
Definition: sdhci.h:72
#define SDHCI_DIVIDER_SHIFT
Definition: sdhci.h:86
#define SDHCI_TRNS_MULTI
Definition: sdhci.h:26
#define SDHCI_CTRL_DRV_TYPE_MASK
Definition: sdhci.h:148
#define SDHCI_INT_DATA_END
Definition: sdhci.h:106
#define SDHCI_POWER_180
Definition: sdhci.h:74
#define SDHCI_CLOCK_BASE_MASK
Definition: sdhci.h:161
size_t len
Definition: bouncebuf.h:49
uint32_t response[4]
Definition: sd_mmc_ctrlr.h:91
uint32_t cmdarg
Definition: sd_mmc_ctrlr.h:86
uint16_t cmdidx
Definition: sd_mmc_ctrlr.h:25
uint32_t flags
Definition: sd_mmc_ctrlr.h:92
uint32_t resp_type
Definition: sd_mmc_ctrlr.h:67
char * dest
Definition: sd_mmc_ctrlr.h:108
uint32_t blocksize
Definition: sd_mmc_ctrlr.h:117
uint32_t flags
Definition: sd_mmc_ctrlr.h:111
const char * src
Definition: sd_mmc_ctrlr.h:109
uint32_t blocks
Definition: sd_mmc_ctrlr.h:116
Definition: spm.h:654
void(* tuning_start)(struct sd_mmc_ctrlr *ctrlr, int retune)
Definition: sd_mmc_ctrlr.h:124
uint32_t f_min
Definition: sd_mmc_ctrlr.h:152
uint32_t f_max
Definition: sd_mmc_ctrlr.h:153
uint32_t voltages
Definition: sd_mmc_ctrlr.h:129
uint32_t udelay_wait_after_cmd
Definition: sd_mmc_ctrlr.h:203
unsigned int version
Definition: sd_mmc_ctrlr.h:128
uint32_t b_max
Definition: sd_mmc_ctrlr.h:185
uint32_t bus_width
Definition: sd_mmc_ctrlr.h:166
uint32_t bus_hz
Definition: sd_mmc_ctrlr.h:155
uint32_t caps
Definition: sd_mmc_ctrlr.h:167
int(* send_cmd)(struct sd_mmc_ctrlr *ctrlr, struct mmc_command *cmd, struct mmc_data *data)
Definition: sd_mmc_ctrlr.h:121
uint32_t timing
Definition: sd_mmc_ctrlr.h:186
int(* is_tuning_complete)(struct sd_mmc_ctrlr *ctrlr, int *successful)
Definition: sd_mmc_ctrlr.h:125
uint32_t clock_base
Definition: sd_mmc_ctrlr.h:151
uint32_t request_hz
Definition: sd_mmc_ctrlr.h:154
void(* set_ios)(struct sd_mmc_ctrlr *ctrlr)
Definition: sd_mmc_ctrlr.h:123
void * ioaddr
Definition: sdhci.h:37
struct sd_mmc_ctrlr sd_mmc_ctrlr
Definition: sdhci.h:36
void udelay(uint32_t us)
Definition: udelay.c:15