sdmmc_host.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdbool.h>
  7. #include <stddef.h>
  8. #include <sys/param.h>
  9. #include "esp_log.h"
  10. #include "esp_intr_alloc.h"
  11. #include "soc/soc_caps.h"
  12. #include "soc/soc_pins.h"
  13. #include "soc/gpio_periph.h"
  14. #include "esp_rom_gpio.h"
  15. #include "esp_rom_sys.h"
  16. #include "driver/gpio.h"
  17. #include "driver/sdmmc_host.h"
  18. #include "esp_private/periph_ctrl.h"
  19. #include "sdmmc_private.h"
  20. #include "freertos/FreeRTOS.h"
  21. #include "freertos/semphr.h"
  22. #include "soc/sdmmc_periph.h"
  23. #include "hal/gpio_hal.h"
  24. #define SDMMC_EVENT_QUEUE_LENGTH 32
  25. static void sdmmc_isr(void* arg);
  26. static void sdmmc_host_dma_init(void);
  27. static const char* TAG = "sdmmc_periph";
  28. static intr_handle_t s_intr_handle;
  29. static QueueHandle_t s_event_queue;
  30. static SemaphoreHandle_t s_io_intr_event;
  31. static size_t s_slot_width[2] = {1, 1};
  32. /* The following definitions are used to simplify GPIO configuration in the driver,
  33. * whether IOMUX or GPIO Matrix is used by the chip.
  34. * Two simple "APIs" are provided to the driver code:
  35. * - configure_pin(name, slot, mode): Configures signal "name" for the given slot and mode.
  36. * - GPIO_NUM(slot, name): Returns the GPIO number of signal "name" for the given slot.
  37. *
  38. * To make this work, configure_pin is defined as a macro that picks the parameters required
  39. * for configuring GPIO matrix or IOMUX from relevant arrays, and passes them to either of
  40. * configure_pin_gpio_matrix, configure_pin_iomux functions.
  41. * Likewise, GPIO_NUM is a macro that picks the pin number from one of the two structures.
  42. *
  43. * Macros are used rather than inline functions to look up members of different structures
  44. * with same names. E.g. the number of pin d3 is obtained either from .d3 member of
  45. * sdmmc_slot_gpio_num array (for IOMUX) or from .d3 member of s_sdmmc_slot_gpio_num array
  46. * (for GPIO matrix).
  47. */
  48. #ifdef SOC_SDMMC_USE_GPIO_MATRIX
  49. static void configure_pin_gpio_matrix(uint8_t gpio_num, uint8_t gpio_matrix_sig, gpio_mode_t mode, const char* name);
  50. #define configure_pin(name, slot, mode) \
  51. configure_pin_gpio_matrix(s_sdmmc_slot_gpio_num[slot].name, sdmmc_slot_gpio_sig[slot].name, mode, #name)
  52. static sdmmc_slot_io_info_t s_sdmmc_slot_gpio_num[SOC_SDMMC_NUM_SLOTS];
  53. #define GPIO_NUM(slot, name) s_sdmmc_slot_gpio_num[slot].name
  54. #elif SOC_SDMMC_USE_IOMUX
  55. static void configure_pin_iomux(uint8_t gpio_num);
  56. #define configure_pin(name, slot, mode) configure_pin_iomux(sdmmc_slot_gpio_num[slot].name)
  57. #define GPIO_NUM(slot, name) sdmmc_slot_gpio_num[slot].name
  58. #endif // SOC_SDMMC_USE_GPIO_MATRIX
  59. static esp_err_t sdmmc_host_pullup_en_internal(int slot, int width);
  60. void sdmmc_host_reset(void)
  61. {
  62. // Set reset bits
  63. SDMMC.ctrl.controller_reset = 1;
  64. SDMMC.ctrl.dma_reset = 1;
  65. SDMMC.ctrl.fifo_reset = 1;
  66. // Wait for the reset bits to be cleared by hardware
  67. while (SDMMC.ctrl.controller_reset || SDMMC.ctrl.fifo_reset || SDMMC.ctrl.dma_reset) {
  68. ;
  69. }
  70. }
  71. /* We have two clock divider stages:
  72. * - one is the clock generator which drives SDMMC peripheral,
  73. * it can be configured using SDMMC.clock register. It can generate
  74. * frequencies 160MHz/(N + 1), where 0 < N < 16, I.e. from 10 to 80 MHz.
  75. * - 4 clock dividers inside SDMMC peripheral, which can divide clock
  76. * from the first stage by 2 * M, where 0 < M < 255
  77. * (they can also be bypassed).
  78. *
  79. * For cards which aren't UHS-1 or UHS-2 cards, which we don't support,
  80. * maximum bus frequency in high speed (HS) mode is 50 MHz.
  81. * Note: for non-UHS-1 cards, HS mode is optional.
  82. * Default speed (DS) mode is mandatory, it works up to 25 MHz.
  83. * Whether the card supports HS or not can be determined using TRAN_SPEED
  84. * field of card's CSD register.
  85. *
  86. * 50 MHz can not be obtained exactly, closest we can get is 53 MHz.
  87. *
  88. * The first stage divider is set to the highest possible value for the given
  89. * frequency, and the the second stage dividers are used if division factor
  90. * is >16.
  91. *
  92. * Of the second stage dividers, div0 is used for card 0, and div1 is used
  93. * for card 1.
  94. */
  95. static void sdmmc_host_set_clk_div(int div)
  96. {
  97. // Set frequency to 160MHz / div
  98. // div = p + 1
  99. // duty cycle = (h + 1)/(p + 1) (should be = 1/2)
  100. assert (div > 1 && div <= 16);
  101. int p = div - 1;
  102. int h = div / 2 - 1;
  103. SDMMC.clock.div_factor_p = p;
  104. SDMMC.clock.div_factor_h = h;
  105. SDMMC.clock.div_factor_m = p;
  106. // Make sure 160 MHz source clock is used
  107. #if SOC_SDMMC_SUPPORT_XTAL_CLOCK
  108. SDMMC.clock.clk_sel = 1;
  109. #endif
  110. #if SOC_SDMMC_USE_GPIO_MATRIX
  111. // 90 degree phase on input and output clocks
  112. const int inout_clock_phase = 1;
  113. #else
  114. // 180 degree phase on input and output clocks
  115. const int inout_clock_phase = 4;
  116. #endif
  117. // Set phases for in/out clocks
  118. SDMMC.clock.phase_dout = inout_clock_phase;
  119. SDMMC.clock.phase_din = inout_clock_phase;
  120. SDMMC.clock.phase_core = 0;
  121. // Wait for the clock to propagate
  122. esp_rom_delay_us(10);
  123. }
  124. static void sdmmc_host_input_clk_disable(void)
  125. {
  126. SDMMC.clock.val = 0;
  127. }
  128. static void sdmmc_host_clock_update_command(int slot)
  129. {
  130. // Clock update command (not a real command; just updates CIU registers)
  131. sdmmc_hw_cmd_t cmd_val = {
  132. .card_num = slot,
  133. .update_clk_reg = 1,
  134. .wait_complete = 1
  135. };
  136. bool repeat = true;
  137. while(repeat) {
  138. sdmmc_host_start_command(slot, cmd_val, 0);
  139. while (true) {
  140. // Sending clock update command to the CIU can generate HLE error.
  141. // According to the manual, this is okay and we must retry the command.
  142. if (SDMMC.rintsts.hle) {
  143. SDMMC.rintsts.hle = 1;
  144. repeat = true;
  145. break;
  146. }
  147. // When the command is accepted by CIU, start_command bit will be
  148. // cleared in SDMMC.cmd register.
  149. if (SDMMC.cmd.start_command == 0) {
  150. repeat = false;
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. void sdmmc_host_get_clk_dividers(const uint32_t freq_khz, int *host_div, int *card_div)
  157. {
  158. // Calculate new dividers
  159. if (freq_khz >= SDMMC_FREQ_HIGHSPEED) {
  160. *host_div = 4; // 160 MHz / 4 = 40 MHz
  161. *card_div = 0;
  162. } else if (freq_khz == SDMMC_FREQ_DEFAULT) {
  163. *host_div = 8; // 160 MHz / 8 = 20 MHz
  164. *card_div = 0;
  165. } else if (freq_khz == SDMMC_FREQ_PROBING) {
  166. *host_div = 10; // 160 MHz / 10 / (20 * 2) = 400 kHz
  167. *card_div = 20;
  168. } else {
  169. /*
  170. * for custom frequencies use maximum range of host divider (1-16), find the closest <= div. combination
  171. * if exceeded, combine with the card divider to keep reasonable precision (applies mainly to low frequencies)
  172. * effective frequency range: 400 kHz - 32 MHz (32.1 - 39.9 MHz cannot be covered with given divider scheme)
  173. */
  174. *host_div = (2 * APB_CLK_FREQ) / (freq_khz * 1000);
  175. if (*host_div > 15 ) {
  176. *host_div = 2;
  177. *card_div = APB_CLK_FREQ / (2 * freq_khz * 1000);
  178. if ( (APB_CLK_FREQ % (2 * freq_khz * 1000)) > 0 ) {
  179. (*card_div)++;
  180. }
  181. } else if ( ((2 * APB_CLK_FREQ) % (freq_khz * 1000)) > 0 ) {
  182. (*host_div)++;
  183. }
  184. }
  185. }
  186. static int sdmmc_host_calc_freq(const int host_div, const int card_div)
  187. {
  188. return 2 * APB_CLK_FREQ / host_div / ((card_div == 0) ? 1 : card_div * 2) / 1000;
  189. }
  190. esp_err_t sdmmc_host_set_card_clk(int slot, uint32_t freq_khz)
  191. {
  192. if (!(slot == 0 || slot == 1)) {
  193. return ESP_ERR_INVALID_ARG;
  194. }
  195. // Disable clock first
  196. SDMMC.clkena.cclk_enable &= ~BIT(slot);
  197. sdmmc_host_clock_update_command(slot);
  198. int host_div = 0; /* clock divider of the host (SDMMC.clock) */
  199. int card_div = 0; /* 1/2 of card clock divider (SDMMC.clkdiv) */
  200. sdmmc_host_get_clk_dividers(freq_khz, &host_div, &card_div);
  201. int real_freq = sdmmc_host_calc_freq(host_div, card_div);
  202. ESP_LOGD(TAG, "slot=%d host_div=%d card_div=%d freq=%dkHz (max %" PRIu32 "kHz)", slot, host_div, card_div, real_freq, freq_khz);
  203. // Program CLKDIV and CLKSRC, send them to the CIU
  204. switch(slot) {
  205. case 0:
  206. SDMMC.clksrc.card0 = 0;
  207. SDMMC.clkdiv.div0 = card_div;
  208. break;
  209. case 1:
  210. SDMMC.clksrc.card1 = 1;
  211. SDMMC.clkdiv.div1 = card_div;
  212. break;
  213. }
  214. sdmmc_host_set_clk_div(host_div);
  215. sdmmc_host_clock_update_command(slot);
  216. // Re-enable clocks
  217. SDMMC.clkena.cclk_enable |= BIT(slot);
  218. SDMMC.clkena.cclk_low_power |= BIT(slot);
  219. sdmmc_host_clock_update_command(slot);
  220. // set data timeout
  221. const uint32_t data_timeout_ms = 100;
  222. uint32_t data_timeout_cycles = data_timeout_ms * freq_khz;
  223. const uint32_t data_timeout_cycles_max = 0xffffff;
  224. if (data_timeout_cycles > data_timeout_cycles_max) {
  225. data_timeout_cycles = data_timeout_cycles_max;
  226. }
  227. SDMMC.tmout.data = data_timeout_cycles;
  228. // always set response timeout to highest value, it's small enough anyway
  229. SDMMC.tmout.response = 255;
  230. return ESP_OK;
  231. }
  232. esp_err_t sdmmc_host_get_real_freq(int slot, int* real_freq_khz)
  233. {
  234. if (real_freq_khz == NULL) {
  235. return ESP_ERR_INVALID_ARG;
  236. }
  237. if (!(slot == 0 || slot == 1)) {
  238. return ESP_ERR_INVALID_ARG;
  239. }
  240. int host_div = SDMMC.clock.div_factor_p + 1;
  241. int card_div = slot == 0 ? SDMMC.clkdiv.div0 : SDMMC.clkdiv.div1;
  242. *real_freq_khz = sdmmc_host_calc_freq(host_div, card_div);
  243. return ESP_OK;
  244. }
  245. esp_err_t sdmmc_host_start_command(int slot, sdmmc_hw_cmd_t cmd, uint32_t arg) {
  246. if (!(slot == 0 || slot == 1)) {
  247. return ESP_ERR_INVALID_ARG;
  248. }
  249. if ((SDMMC.cdetect.cards & BIT(slot)) != 0) {
  250. return ESP_ERR_NOT_FOUND;
  251. }
  252. if (cmd.data_expected && cmd.rw && (SDMMC.wrtprt.cards & BIT(slot)) != 0) {
  253. return ESP_ERR_INVALID_STATE;
  254. }
  255. while (SDMMC.cmd.start_command == 1) {
  256. ;
  257. }
  258. SDMMC.cmdarg = arg;
  259. cmd.card_num = slot;
  260. cmd.start_command = 1;
  261. SDMMC.cmd = cmd;
  262. return ESP_OK;
  263. }
  264. esp_err_t sdmmc_host_init(void)
  265. {
  266. if (s_intr_handle) {
  267. return ESP_ERR_INVALID_STATE;
  268. }
  269. periph_module_reset(PERIPH_SDMMC_MODULE);
  270. periph_module_enable(PERIPH_SDMMC_MODULE);
  271. // Enable clock to peripheral. Use smallest divider first.
  272. sdmmc_host_set_clk_div(2);
  273. // Reset
  274. sdmmc_host_reset();
  275. ESP_LOGD(TAG, "peripheral version %"PRIx32", hardware config %08"PRIx32, SDMMC.verid, SDMMC.hcon);
  276. // Clear interrupt status and set interrupt mask to known state
  277. SDMMC.rintsts.val = 0xffffffff;
  278. SDMMC.intmask.val = 0;
  279. SDMMC.ctrl.int_enable = 0;
  280. // Allocate event queue
  281. s_event_queue = xQueueCreate(SDMMC_EVENT_QUEUE_LENGTH, sizeof(sdmmc_event_t));
  282. if (!s_event_queue) {
  283. return ESP_ERR_NO_MEM;
  284. }
  285. s_io_intr_event = xSemaphoreCreateBinary();
  286. if (!s_io_intr_event) {
  287. vQueueDelete(s_event_queue);
  288. s_event_queue = NULL;
  289. return ESP_ERR_NO_MEM;
  290. }
  291. // Attach interrupt handler
  292. esp_err_t ret = esp_intr_alloc(ETS_SDIO_HOST_INTR_SOURCE, 0, &sdmmc_isr, s_event_queue, &s_intr_handle);
  293. if (ret != ESP_OK) {
  294. vQueueDelete(s_event_queue);
  295. s_event_queue = NULL;
  296. vSemaphoreDelete(s_io_intr_event);
  297. s_io_intr_event = NULL;
  298. return ret;
  299. }
  300. // Enable interrupts
  301. SDMMC.intmask.val =
  302. SDMMC_INTMASK_CD |
  303. SDMMC_INTMASK_CMD_DONE |
  304. SDMMC_INTMASK_DATA_OVER |
  305. SDMMC_INTMASK_RCRC | SDMMC_INTMASK_DCRC |
  306. SDMMC_INTMASK_RTO | SDMMC_INTMASK_DTO | SDMMC_INTMASK_HTO |
  307. SDMMC_INTMASK_SBE | SDMMC_INTMASK_EBE |
  308. SDMMC_INTMASK_RESP_ERR | SDMMC_INTMASK_HLE; //sdio is enabled only when use.
  309. SDMMC.ctrl.int_enable = 1;
  310. // Disable generation of Busy Clear Interrupt
  311. SDMMC.cardthrctl.busy_clr_int_en = 0;
  312. // Enable DMA
  313. sdmmc_host_dma_init();
  314. // Initialize transaction handler
  315. ret = sdmmc_host_transaction_handler_init();
  316. if (ret != ESP_OK) {
  317. vQueueDelete(s_event_queue);
  318. s_event_queue = NULL;
  319. vSemaphoreDelete(s_io_intr_event);
  320. s_io_intr_event = NULL;
  321. esp_intr_free(s_intr_handle);
  322. s_intr_handle = NULL;
  323. return ret;
  324. }
  325. return ESP_OK;
  326. }
  327. #ifdef SOC_SDMMC_USE_IOMUX
  328. static void configure_pin_iomux(uint8_t gpio_num)
  329. {
  330. const int sdmmc_func = 3;
  331. const int drive_strength = 3;
  332. assert(gpio_num != (uint8_t) GPIO_NUM_NC);
  333. gpio_pulldown_dis(gpio_num);
  334. uint32_t reg = GPIO_PIN_MUX_REG[gpio_num];
  335. assert(reg != UINT32_MAX);
  336. PIN_INPUT_ENABLE(reg);
  337. gpio_hal_iomux_func_sel(reg, sdmmc_func);
  338. PIN_SET_DRV(reg, drive_strength);
  339. }
  340. #elif SOC_SDMMC_USE_GPIO_MATRIX
  341. static void configure_pin_gpio_matrix(uint8_t gpio_num, uint8_t gpio_matrix_sig, gpio_mode_t mode, const char* name)
  342. {
  343. assert (gpio_num != (uint8_t) GPIO_NUM_NC);
  344. ESP_LOGD(TAG, "using GPIO%d as %s pin", gpio_num, name);
  345. gpio_reset_pin(gpio_num);
  346. gpio_set_direction(gpio_num, mode);
  347. gpio_pulldown_dis(gpio_num);
  348. if (mode == GPIO_MODE_INPUT || mode == GPIO_MODE_INPUT_OUTPUT) {
  349. esp_rom_gpio_connect_in_signal(gpio_num, gpio_matrix_sig, false);
  350. }
  351. if (mode == GPIO_MODE_OUTPUT || mode == GPIO_MODE_INPUT_OUTPUT) {
  352. esp_rom_gpio_connect_out_signal(gpio_num, gpio_matrix_sig, false, false);
  353. }
  354. }
  355. #endif // SOC_SDMMC_USE_{IOMUX,GPIO_MATRIX}
  356. esp_err_t sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t* slot_config)
  357. {
  358. if (!s_intr_handle) {
  359. return ESP_ERR_INVALID_STATE;
  360. }
  361. if (!(slot == 0 || slot == 1)) {
  362. return ESP_ERR_INVALID_ARG;
  363. }
  364. if (slot_config == NULL) {
  365. return ESP_ERR_INVALID_ARG;
  366. }
  367. int gpio_cd = slot_config->cd;
  368. int gpio_wp = slot_config->wp;
  369. uint8_t slot_width = slot_config->width;
  370. // Configure pins
  371. const sdmmc_slot_info_t* slot_info = &sdmmc_slot_info[slot];
  372. if (slot_width == SDMMC_SLOT_WIDTH_DEFAULT) {
  373. slot_width = slot_info->width;
  374. }
  375. else if (slot_width > slot_info->width) {
  376. return ESP_ERR_INVALID_ARG;
  377. }
  378. s_slot_width[slot] = slot_width;
  379. #if SOC_SDMMC_USE_GPIO_MATRIX
  380. /* Save pin configuration for this slot */
  381. s_sdmmc_slot_gpio_num[slot].clk = slot_config->clk;
  382. s_sdmmc_slot_gpio_num[slot].cmd = slot_config->cmd;
  383. s_sdmmc_slot_gpio_num[slot].d0 = slot_config->d0;
  384. /* Save d1 even in 1-line mode, it might be needed for SDIO INT line */
  385. s_sdmmc_slot_gpio_num[slot].d1 = slot_config->d1;
  386. if (slot_width >= 4) {
  387. s_sdmmc_slot_gpio_num[slot].d2 = slot_config->d2;
  388. }
  389. /* Save d3 even for 1-line mode, as it needs to be set high */
  390. s_sdmmc_slot_gpio_num[slot].d3 = slot_config->d3;
  391. if (slot_width >= 8) {
  392. s_sdmmc_slot_gpio_num[slot].d4 = slot_config->d4;
  393. s_sdmmc_slot_gpio_num[slot].d5 = slot_config->d5;
  394. s_sdmmc_slot_gpio_num[slot].d6 = slot_config->d6;
  395. s_sdmmc_slot_gpio_num[slot].d7 = slot_config->d7;
  396. }
  397. #endif
  398. bool pullup = slot_config->flags & SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
  399. if (pullup) {
  400. sdmmc_host_pullup_en_internal(slot, slot_config->width);
  401. }
  402. configure_pin(clk, slot, GPIO_MODE_OUTPUT);
  403. configure_pin(cmd, slot, GPIO_MODE_INPUT_OUTPUT);
  404. configure_pin(d0, slot, GPIO_MODE_INPUT_OUTPUT);
  405. if (slot_width >= 4) {
  406. configure_pin(d1, slot, GPIO_MODE_INPUT_OUTPUT);
  407. configure_pin(d2, slot, GPIO_MODE_INPUT_OUTPUT);
  408. // Force D3 high to make slave enter SD mode.
  409. // Connect to peripheral after width configuration.
  410. gpio_config_t gpio_conf = {
  411. .pin_bit_mask = BIT64(GPIO_NUM(slot, d3)),
  412. .mode = GPIO_MODE_OUTPUT,
  413. .pull_up_en = 0,
  414. .pull_down_en = 0,
  415. .intr_type = GPIO_INTR_DISABLE,
  416. };
  417. gpio_config(&gpio_conf);
  418. gpio_set_level(GPIO_NUM(slot, d3), 1);
  419. }
  420. if (slot_width == 8) {
  421. configure_pin(d4, slot, GPIO_MODE_INPUT_OUTPUT);
  422. configure_pin(d5, slot, GPIO_MODE_INPUT_OUTPUT);
  423. configure_pin(d6, slot, GPIO_MODE_INPUT_OUTPUT);
  424. configure_pin(d7, slot, GPIO_MODE_INPUT_OUTPUT);
  425. }
  426. // SDIO slave interrupt is edge sensitive to ~(int_n | card_int | card_detect)
  427. // set this and card_detect to high to enable sdio interrupt
  428. esp_rom_gpio_connect_in_signal(GPIO_MATRIX_CONST_ONE_INPUT, slot_info->card_int, false);
  429. // Set up Card Detect input
  430. int matrix_in_cd;
  431. if (gpio_cd != SDMMC_SLOT_NO_CD) {
  432. ESP_LOGD(TAG, "using GPIO%d as CD pin", gpio_cd);
  433. esp_rom_gpio_pad_select_gpio(gpio_cd);
  434. gpio_set_direction(gpio_cd, GPIO_MODE_INPUT);
  435. matrix_in_cd = gpio_cd;
  436. } else {
  437. // if not set, default to CD low (card present)
  438. matrix_in_cd = GPIO_MATRIX_CONST_ZERO_INPUT;
  439. }
  440. esp_rom_gpio_connect_in_signal(matrix_in_cd, slot_info->card_detect, false);
  441. // Set up Write Protect input
  442. int matrix_in_wp;
  443. if (gpio_wp != SDMMC_SLOT_NO_WP) {
  444. ESP_LOGD(TAG, "using GPIO%d as WP pin", gpio_wp);
  445. esp_rom_gpio_pad_select_gpio(gpio_wp);
  446. gpio_set_direction(gpio_wp, GPIO_MODE_INPUT);
  447. matrix_in_wp = gpio_wp;
  448. } else {
  449. // if not set, default to WP high (not write protected)
  450. matrix_in_wp = GPIO_MATRIX_CONST_ONE_INPUT;
  451. }
  452. // WP signal is normally active low, but hardware expects
  453. // an active-high signal, so invert it in GPIO matrix
  454. esp_rom_gpio_connect_in_signal(matrix_in_wp, slot_info->write_protect, true);
  455. // By default, set probing frequency (400kHz) and 1-bit bus
  456. esp_err_t ret = sdmmc_host_set_card_clk(slot, 400);
  457. if (ret != ESP_OK) {
  458. return ret;
  459. }
  460. ret = sdmmc_host_set_bus_width(slot, 1);
  461. if (ret != ESP_OK) {
  462. return ret;
  463. }
  464. return ESP_OK;
  465. }
  466. esp_err_t sdmmc_host_deinit(void)
  467. {
  468. if (!s_intr_handle) {
  469. return ESP_ERR_INVALID_STATE;
  470. }
  471. esp_intr_free(s_intr_handle);
  472. s_intr_handle = NULL;
  473. vQueueDelete(s_event_queue);
  474. s_event_queue = NULL;
  475. vQueueDelete(s_io_intr_event);
  476. s_io_intr_event = NULL;
  477. sdmmc_host_input_clk_disable();
  478. sdmmc_host_transaction_handler_deinit();
  479. periph_module_disable(PERIPH_SDMMC_MODULE);
  480. return ESP_OK;
  481. }
  482. esp_err_t sdmmc_host_wait_for_event(int tick_count, sdmmc_event_t* out_event)
  483. {
  484. if (!out_event) {
  485. return ESP_ERR_INVALID_ARG;
  486. }
  487. if (!s_event_queue) {
  488. return ESP_ERR_INVALID_STATE;
  489. }
  490. int ret = xQueueReceive(s_event_queue, out_event, tick_count);
  491. if (ret == pdFALSE) {
  492. return ESP_ERR_TIMEOUT;
  493. }
  494. return ESP_OK;
  495. }
  496. esp_err_t sdmmc_host_set_bus_width(int slot, size_t width)
  497. {
  498. if (!(slot == 0 || slot == 1)) {
  499. return ESP_ERR_INVALID_ARG;
  500. }
  501. if (sdmmc_slot_info[slot].width < width) {
  502. return ESP_ERR_INVALID_ARG;
  503. }
  504. const uint16_t mask = BIT(slot);
  505. if (width == 1) {
  506. SDMMC.ctype.card_width_8 &= ~mask;
  507. SDMMC.ctype.card_width &= ~mask;
  508. } else if (width == 4) {
  509. SDMMC.ctype.card_width_8 &= ~mask;
  510. SDMMC.ctype.card_width |= mask;
  511. // D3 was set to GPIO high to force slave into SD mode, until 4-bit mode is set
  512. configure_pin(d3, slot, GPIO_MODE_INPUT_OUTPUT);
  513. } else if (width == 8) {
  514. SDMMC.ctype.card_width_8 |= mask;
  515. // D3 was set to GPIO high to force slave into SD mode, until 4-bit mode is set
  516. configure_pin(d3, slot, GPIO_MODE_INPUT_OUTPUT);
  517. } else {
  518. return ESP_ERR_INVALID_ARG;
  519. }
  520. ESP_LOGD(TAG, "slot=%d width=%d", slot, width);
  521. return ESP_OK;
  522. }
  523. size_t sdmmc_host_get_slot_width(int slot)
  524. {
  525. assert( slot == 0 || slot == 1 );
  526. return s_slot_width[slot];
  527. }
  528. esp_err_t sdmmc_host_set_bus_ddr_mode(int slot, bool ddr_enabled)
  529. {
  530. if (!(slot == 0 || slot == 1)) {
  531. return ESP_ERR_INVALID_ARG;
  532. }
  533. if (s_slot_width[slot] == 8 && ddr_enabled) {
  534. ESP_LOGW(TAG, "DDR mode with 8-bit bus width is not supported yet");
  535. // requires reconfiguring controller clock for 2x card frequency
  536. return ESP_ERR_NOT_SUPPORTED;
  537. }
  538. uint32_t mask = BIT(slot);
  539. if (ddr_enabled) {
  540. SDMMC.uhs.ddr |= mask;
  541. SDMMC.emmc_ddr_reg |= mask;
  542. } else {
  543. SDMMC.uhs.ddr &= ~mask;
  544. SDMMC.emmc_ddr_reg &= ~mask;
  545. }
  546. ESP_LOGD(TAG, "slot=%d ddr=%d", slot, ddr_enabled ? 1 : 0);
  547. return ESP_OK;
  548. }
  549. static void sdmmc_host_dma_init(void)
  550. {
  551. SDMMC.ctrl.dma_enable = 1;
  552. SDMMC.bmod.val = 0;
  553. SDMMC.bmod.sw_reset = 1;
  554. SDMMC.idinten.ni = 1;
  555. SDMMC.idinten.ri = 1;
  556. SDMMC.idinten.ti = 1;
  557. }
  558. void sdmmc_host_dma_stop(void)
  559. {
  560. SDMMC.ctrl.use_internal_dma = 0;
  561. SDMMC.ctrl.dma_reset = 1;
  562. SDMMC.bmod.fb = 0;
  563. SDMMC.bmod.enable = 0;
  564. }
  565. void sdmmc_host_dma_prepare(sdmmc_desc_t* desc, size_t block_size, size_t data_size)
  566. {
  567. // Set size of data and DMA descriptor pointer
  568. SDMMC.bytcnt = data_size;
  569. SDMMC.blksiz = block_size;
  570. SDMMC.dbaddr = desc;
  571. // Enable everything needed to use DMA
  572. SDMMC.ctrl.dma_enable = 1;
  573. SDMMC.ctrl.use_internal_dma = 1;
  574. SDMMC.bmod.enable = 1;
  575. SDMMC.bmod.fb = 1;
  576. sdmmc_host_dma_resume();
  577. }
  578. void sdmmc_host_dma_resume(void)
  579. {
  580. SDMMC.pldmnd = 1;
  581. }
  582. bool sdmmc_host_card_busy(void)
  583. {
  584. return SDMMC.status.data_busy == 1;
  585. }
  586. esp_err_t sdmmc_host_io_int_enable(int slot)
  587. {
  588. configure_pin(d1, slot, GPIO_MODE_INPUT_OUTPUT);
  589. return ESP_OK;
  590. }
  591. esp_err_t sdmmc_host_io_int_wait(int slot, TickType_t timeout_ticks)
  592. {
  593. /* SDIO interrupts are negedge sensitive ones: the status bit is only set
  594. * when first interrupt triggered.
  595. *
  596. * If D1 GPIO is low when entering this function, we know that interrupt
  597. * (in SDIO sense) has occurred and we don't need to use SDMMC peripheral
  598. * interrupt.
  599. */
  600. SDMMC.intmask.sdio &= ~BIT(slot); /* Disable SDIO interrupt */
  601. SDMMC.rintsts.sdio = BIT(slot);
  602. if (gpio_get_level(GPIO_NUM(slot, d1)) == 0) {
  603. return ESP_OK;
  604. }
  605. /* Otherwise, need to wait for an interrupt. Since D1 was high,
  606. * SDMMC peripheral interrupt is guaranteed to trigger on negedge.
  607. */
  608. xSemaphoreTake(s_io_intr_event, 0);
  609. SDMMC.intmask.sdio |= BIT(slot); /* Re-enable SDIO interrupt */
  610. if (xSemaphoreTake(s_io_intr_event, timeout_ticks) == pdTRUE) {
  611. return ESP_OK;
  612. } else {
  613. return ESP_ERR_TIMEOUT;
  614. }
  615. }
  616. /**
  617. * @brief SDMMC interrupt handler
  618. *
  619. * All communication in SD protocol is driven by the master, and the hardware
  620. * handles things like stop commands automatically.
  621. * So the interrupt handler doesn't need to do much, we just push interrupt
  622. * status into a queue, clear interrupt flags, and let the task currently
  623. * doing communication figure out what to do next.
  624. * This also applies to SDIO interrupts which are generated by the slave.
  625. *
  626. * Card detect interrupts pose a small issue though, because if a card is
  627. * plugged in and out a few times, while there is no task to process
  628. * the events, event queue can become full and some card detect events
  629. * may be dropped. We ignore this problem for now, since the there are no other
  630. * interesting events which can get lost due to this.
  631. */
  632. static void sdmmc_isr(void* arg) {
  633. QueueHandle_t queue = (QueueHandle_t) arg;
  634. sdmmc_event_t event;
  635. int higher_priority_task_awoken = pdFALSE;
  636. uint32_t pending = SDMMC.mintsts.val & 0xFFFF;
  637. SDMMC.rintsts.val = pending;
  638. event.sdmmc_status = pending;
  639. uint32_t dma_pending = SDMMC.idsts.val;
  640. SDMMC.idsts.val = dma_pending;
  641. event.dma_status = dma_pending & 0x1f;
  642. if (pending != 0 || dma_pending != 0) {
  643. xQueueSendFromISR(queue, &event, &higher_priority_task_awoken);
  644. }
  645. uint32_t sdio_pending = SDMMC.mintsts.sdio;
  646. if (sdio_pending) {
  647. // disable the interrupt (no need to clear here, this is done in sdmmc_host_io_wait_int)
  648. SDMMC.intmask.sdio &= ~sdio_pending;
  649. xSemaphoreGiveFromISR(s_io_intr_event, &higher_priority_task_awoken);
  650. }
  651. if (higher_priority_task_awoken == pdTRUE) {
  652. portYIELD_FROM_ISR();
  653. }
  654. }
  655. static esp_err_t sdmmc_host_pullup_en_internal(int slot, int width)
  656. {
  657. if (width > sdmmc_slot_info[slot].width) {
  658. //in esp32 we only support 8 bit in slot 0, note this is occupied by the flash by default
  659. return ESP_ERR_INVALID_ARG;
  660. }
  661. // according to the spec, the host controls the clk, we don't to pull it up here
  662. gpio_pullup_en(GPIO_NUM(slot, cmd));
  663. gpio_pullup_en(GPIO_NUM(slot, d0));
  664. if (width >= 4) {
  665. gpio_pullup_en(GPIO_NUM(slot, d1));
  666. gpio_pullup_en(GPIO_NUM(slot, d2));
  667. gpio_pullup_en(GPIO_NUM(slot, d3));
  668. }
  669. if (width == 8) {
  670. gpio_pullup_en(GPIO_NUM(slot, d4));
  671. gpio_pullup_en(GPIO_NUM(slot, d5));
  672. gpio_pullup_en(GPIO_NUM(slot, d6));
  673. gpio_pullup_en(GPIO_NUM(slot, d7));
  674. }
  675. return ESP_OK;
  676. }