sdmmc_host.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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 "driver/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. esp_err_t sdmmc_host_set_card_clk(int slot, uint32_t freq_khz)
  157. {
  158. if (!(slot == 0 || slot == 1)) {
  159. return ESP_ERR_INVALID_ARG;
  160. }
  161. const int clk40m = 40000;
  162. // Disable clock first
  163. SDMMC.clkena.cclk_enable &= ~BIT(slot);
  164. sdmmc_host_clock_update_command(slot);
  165. int host_div = 0; /* clock divider of the host (SDMMC.clock) */
  166. int card_div = 0; /* 1/2 of card clock divider (SDMMC.clkdiv) */
  167. // Calculate new dividers
  168. if (freq_khz >= SDMMC_FREQ_HIGHSPEED) {
  169. host_div = 4; // 160 MHz / 4 = 40 MHz
  170. card_div = 0;
  171. } else if (freq_khz == SDMMC_FREQ_DEFAULT) {
  172. host_div = 8; // 160 MHz / 8 = 20 MHz
  173. card_div = 0;
  174. } else if (freq_khz == SDMMC_FREQ_PROBING) {
  175. host_div = 10; // 160 MHz / 10 / (20 * 2) = 400 kHz
  176. card_div = 20;
  177. } else {
  178. host_div = 2;
  179. card_div = (clk40m + freq_khz * 2 - 1) / (freq_khz * 2); // round up
  180. }
  181. ESP_LOGD(TAG, "slot=%d host_div=%d card_div=%d freq=%dkHz",
  182. slot, host_div, card_div,
  183. 2 * APB_CLK_FREQ / host_div / ((card_div == 0) ? 1 : card_div * 2) / 1000);
  184. // Program CLKDIV and CLKSRC, send them to the CIU
  185. switch(slot) {
  186. case 0:
  187. SDMMC.clksrc.card0 = 0;
  188. SDMMC.clkdiv.div0 = card_div;
  189. break;
  190. case 1:
  191. SDMMC.clksrc.card1 = 1;
  192. SDMMC.clkdiv.div1 = card_div;
  193. break;
  194. }
  195. sdmmc_host_set_clk_div(host_div);
  196. sdmmc_host_clock_update_command(slot);
  197. // Re-enable clocks
  198. SDMMC.clkena.cclk_enable |= BIT(slot);
  199. SDMMC.clkena.cclk_low_power |= BIT(slot);
  200. sdmmc_host_clock_update_command(slot);
  201. // set data timeout
  202. const uint32_t data_timeout_ms = 100;
  203. uint32_t data_timeout_cycles = data_timeout_ms * freq_khz;
  204. const uint32_t data_timeout_cycles_max = 0xffffff;
  205. if (data_timeout_cycles > data_timeout_cycles_max) {
  206. data_timeout_cycles = data_timeout_cycles_max;
  207. }
  208. SDMMC.tmout.data = data_timeout_cycles;
  209. // always set response timeout to highest value, it's small enough anyway
  210. SDMMC.tmout.response = 255;
  211. return ESP_OK;
  212. }
  213. esp_err_t sdmmc_host_start_command(int slot, sdmmc_hw_cmd_t cmd, uint32_t arg) {
  214. if (!(slot == 0 || slot == 1)) {
  215. return ESP_ERR_INVALID_ARG;
  216. }
  217. if ((SDMMC.cdetect.cards & BIT(slot)) != 0) {
  218. return ESP_ERR_NOT_FOUND;
  219. }
  220. if (cmd.data_expected && cmd.rw && (SDMMC.wrtprt.cards & BIT(slot)) != 0) {
  221. return ESP_ERR_INVALID_STATE;
  222. }
  223. while (SDMMC.cmd.start_command == 1) {
  224. ;
  225. }
  226. SDMMC.cmdarg = arg;
  227. cmd.card_num = slot;
  228. cmd.start_command = 1;
  229. SDMMC.cmd = cmd;
  230. return ESP_OK;
  231. }
  232. esp_err_t sdmmc_host_init(void)
  233. {
  234. if (s_intr_handle) {
  235. return ESP_ERR_INVALID_STATE;
  236. }
  237. periph_module_reset(PERIPH_SDMMC_MODULE);
  238. periph_module_enable(PERIPH_SDMMC_MODULE);
  239. // Enable clock to peripheral. Use smallest divider first.
  240. sdmmc_host_set_clk_div(2);
  241. // Reset
  242. sdmmc_host_reset();
  243. ESP_LOGD(TAG, "peripheral version %x, hardware config %08x", SDMMC.verid, SDMMC.hcon);
  244. // Clear interrupt status and set interrupt mask to known state
  245. SDMMC.rintsts.val = 0xffffffff;
  246. SDMMC.intmask.val = 0;
  247. SDMMC.ctrl.int_enable = 0;
  248. // Allocate event queue
  249. s_event_queue = xQueueCreate(SDMMC_EVENT_QUEUE_LENGTH, sizeof(sdmmc_event_t));
  250. if (!s_event_queue) {
  251. return ESP_ERR_NO_MEM;
  252. }
  253. s_io_intr_event = xSemaphoreCreateBinary();
  254. if (!s_io_intr_event) {
  255. vQueueDelete(s_event_queue);
  256. s_event_queue = NULL;
  257. return ESP_ERR_NO_MEM;
  258. }
  259. // Attach interrupt handler
  260. esp_err_t ret = esp_intr_alloc(ETS_SDIO_HOST_INTR_SOURCE, 0, &sdmmc_isr, s_event_queue, &s_intr_handle);
  261. if (ret != ESP_OK) {
  262. vQueueDelete(s_event_queue);
  263. s_event_queue = NULL;
  264. vSemaphoreDelete(s_io_intr_event);
  265. s_io_intr_event = NULL;
  266. return ret;
  267. }
  268. // Enable interrupts
  269. SDMMC.intmask.val =
  270. SDMMC_INTMASK_CD |
  271. SDMMC_INTMASK_CMD_DONE |
  272. SDMMC_INTMASK_DATA_OVER |
  273. SDMMC_INTMASK_RCRC | SDMMC_INTMASK_DCRC |
  274. SDMMC_INTMASK_RTO | SDMMC_INTMASK_DTO | SDMMC_INTMASK_HTO |
  275. SDMMC_INTMASK_SBE | SDMMC_INTMASK_EBE |
  276. SDMMC_INTMASK_RESP_ERR | SDMMC_INTMASK_HLE; //sdio is enabled only when use.
  277. SDMMC.ctrl.int_enable = 1;
  278. // Disable generation of Busy Clear Interrupt
  279. SDMMC.cardthrctl.busy_clr_int_en = 0;
  280. // Enable DMA
  281. sdmmc_host_dma_init();
  282. // Initialize transaction handler
  283. ret = sdmmc_host_transaction_handler_init();
  284. if (ret != ESP_OK) {
  285. vQueueDelete(s_event_queue);
  286. s_event_queue = NULL;
  287. vSemaphoreDelete(s_io_intr_event);
  288. s_io_intr_event = NULL;
  289. esp_intr_free(s_intr_handle);
  290. s_intr_handle = NULL;
  291. return ret;
  292. }
  293. return ESP_OK;
  294. }
  295. #ifdef SOC_SDMMC_USE_IOMUX
  296. static void configure_pin_iomux(uint8_t gpio_num)
  297. {
  298. const int sdmmc_func = 3;
  299. const int drive_strength = 3;
  300. assert(gpio_num != (uint8_t) GPIO_NUM_NC);
  301. gpio_pulldown_dis(gpio_num);
  302. uint32_t reg = GPIO_PIN_MUX_REG[gpio_num];
  303. assert(reg != UINT32_MAX);
  304. PIN_INPUT_ENABLE(reg);
  305. gpio_hal_iomux_func_sel(reg, sdmmc_func);
  306. PIN_SET_DRV(reg, drive_strength);
  307. }
  308. #elif SOC_SDMMC_USE_GPIO_MATRIX
  309. static void configure_pin_gpio_matrix(uint8_t gpio_num, uint8_t gpio_matrix_sig, gpio_mode_t mode, const char* name)
  310. {
  311. assert (gpio_num != (uint8_t) GPIO_NUM_NC);
  312. ESP_LOGD(TAG, "using GPIO%d as %s pin", gpio_num, name);
  313. gpio_reset_pin(gpio_num);
  314. gpio_set_direction(gpio_num, mode);
  315. gpio_pulldown_dis(gpio_num);
  316. if (mode == GPIO_MODE_INPUT || mode == GPIO_MODE_INPUT_OUTPUT) {
  317. esp_rom_gpio_connect_in_signal(gpio_num, gpio_matrix_sig, false);
  318. }
  319. if (mode == GPIO_MODE_OUTPUT || mode == GPIO_MODE_INPUT_OUTPUT) {
  320. esp_rom_gpio_connect_out_signal(gpio_num, gpio_matrix_sig, false, false);
  321. }
  322. }
  323. #endif // SOC_SDMMC_USE_{IOMUX,GPIO_MATRIX}
  324. esp_err_t sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t* slot_config)
  325. {
  326. if (!s_intr_handle) {
  327. return ESP_ERR_INVALID_STATE;
  328. }
  329. if (!(slot == 0 || slot == 1)) {
  330. return ESP_ERR_INVALID_ARG;
  331. }
  332. if (slot_config == NULL) {
  333. return ESP_ERR_INVALID_ARG;
  334. }
  335. int gpio_cd = slot_config->cd;
  336. int gpio_wp = slot_config->wp;
  337. uint8_t slot_width = slot_config->width;
  338. // Configure pins
  339. const sdmmc_slot_info_t* slot_info = &sdmmc_slot_info[slot];
  340. if (slot_width == SDMMC_SLOT_WIDTH_DEFAULT) {
  341. slot_width = slot_info->width;
  342. }
  343. else if (slot_width > slot_info->width) {
  344. return ESP_ERR_INVALID_ARG;
  345. }
  346. s_slot_width[slot] = slot_width;
  347. #if SOC_SDMMC_USE_GPIO_MATRIX
  348. /* Save pin configuration for this slot */
  349. s_sdmmc_slot_gpio_num[slot].clk = slot_config->clk;
  350. s_sdmmc_slot_gpio_num[slot].cmd = slot_config->cmd;
  351. s_sdmmc_slot_gpio_num[slot].d0 = slot_config->d0;
  352. /* Save d1 even in 1-line mode, it might be needed for SDIO INT line */
  353. s_sdmmc_slot_gpio_num[slot].d1 = slot_config->d1;
  354. if (slot_width >= 4) {
  355. s_sdmmc_slot_gpio_num[slot].d2 = slot_config->d2;
  356. }
  357. /* Save d3 even for 1-line mode, as it needs to be set high */
  358. s_sdmmc_slot_gpio_num[slot].d3 = slot_config->d3;
  359. if (slot_width >= 8) {
  360. s_sdmmc_slot_gpio_num[slot].d4 = slot_config->d4;
  361. s_sdmmc_slot_gpio_num[slot].d5 = slot_config->d5;
  362. s_sdmmc_slot_gpio_num[slot].d6 = slot_config->d6;
  363. s_sdmmc_slot_gpio_num[slot].d7 = slot_config->d7;
  364. }
  365. #endif
  366. bool pullup = slot_config->flags & SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
  367. if (pullup) {
  368. sdmmc_host_pullup_en_internal(slot, slot_config->width);
  369. }
  370. configure_pin(clk, slot, GPIO_MODE_OUTPUT);
  371. configure_pin(cmd, slot, GPIO_MODE_INPUT_OUTPUT);
  372. configure_pin(d0, slot, GPIO_MODE_INPUT_OUTPUT);
  373. if (slot_width >= 4) {
  374. configure_pin(d1, slot, GPIO_MODE_INPUT_OUTPUT);
  375. configure_pin(d2, slot, GPIO_MODE_INPUT_OUTPUT);
  376. // Force D3 high to make slave enter SD mode.
  377. // Connect to peripheral after width configuration.
  378. gpio_config_t gpio_conf = {
  379. .pin_bit_mask = BIT64(GPIO_NUM(slot, d3)),
  380. .mode = GPIO_MODE_OUTPUT,
  381. .pull_up_en = 0,
  382. .pull_down_en = 0,
  383. .intr_type = GPIO_INTR_DISABLE,
  384. };
  385. gpio_config(&gpio_conf);
  386. gpio_set_level(GPIO_NUM(slot, d3), 1);
  387. }
  388. if (slot_width == 8) {
  389. configure_pin(d4, slot, GPIO_MODE_INPUT_OUTPUT);
  390. configure_pin(d5, slot, GPIO_MODE_INPUT_OUTPUT);
  391. configure_pin(d6, slot, GPIO_MODE_INPUT_OUTPUT);
  392. configure_pin(d7, slot, GPIO_MODE_INPUT_OUTPUT);
  393. }
  394. // SDIO slave interrupt is edge sensitive to ~(int_n | card_int | card_detect)
  395. // set this and card_detect to high to enable sdio interrupt
  396. esp_rom_gpio_connect_in_signal(GPIO_MATRIX_CONST_ONE_INPUT, slot_info->card_int, false);
  397. // Set up Card Detect input
  398. int matrix_in_cd;
  399. if (gpio_cd != SDMMC_SLOT_NO_CD) {
  400. ESP_LOGD(TAG, "using GPIO%d as CD pin", gpio_cd);
  401. esp_rom_gpio_pad_select_gpio(gpio_cd);
  402. gpio_set_direction(gpio_cd, GPIO_MODE_INPUT);
  403. matrix_in_cd = gpio_cd;
  404. } else {
  405. // if not set, default to CD low (card present)
  406. matrix_in_cd = GPIO_MATRIX_CONST_ZERO_INPUT;
  407. }
  408. esp_rom_gpio_connect_in_signal(matrix_in_cd, slot_info->card_detect, false);
  409. // Set up Write Protect input
  410. int matrix_in_wp;
  411. if (gpio_wp != SDMMC_SLOT_NO_WP) {
  412. ESP_LOGD(TAG, "using GPIO%d as WP pin", gpio_wp);
  413. esp_rom_gpio_pad_select_gpio(gpio_wp);
  414. gpio_set_direction(gpio_wp, GPIO_MODE_INPUT);
  415. matrix_in_wp = gpio_wp;
  416. } else {
  417. // if not set, default to WP high (not write protected)
  418. matrix_in_wp = GPIO_MATRIX_CONST_ONE_INPUT;
  419. }
  420. // WP signal is normally active low, but hardware expects
  421. // an active-high signal, so invert it in GPIO matrix
  422. esp_rom_gpio_connect_in_signal(matrix_in_wp, slot_info->write_protect, true);
  423. // By default, set probing frequency (400kHz) and 1-bit bus
  424. esp_err_t ret = sdmmc_host_set_card_clk(slot, 400);
  425. if (ret != ESP_OK) {
  426. return ret;
  427. }
  428. ret = sdmmc_host_set_bus_width(slot, 1);
  429. if (ret != ESP_OK) {
  430. return ret;
  431. }
  432. return ESP_OK;
  433. }
  434. esp_err_t sdmmc_host_deinit(void)
  435. {
  436. if (!s_intr_handle) {
  437. return ESP_ERR_INVALID_STATE;
  438. }
  439. esp_intr_free(s_intr_handle);
  440. s_intr_handle = NULL;
  441. vQueueDelete(s_event_queue);
  442. s_event_queue = NULL;
  443. vQueueDelete(s_io_intr_event);
  444. s_io_intr_event = NULL;
  445. sdmmc_host_input_clk_disable();
  446. sdmmc_host_transaction_handler_deinit();
  447. periph_module_disable(PERIPH_SDMMC_MODULE);
  448. return ESP_OK;
  449. }
  450. esp_err_t sdmmc_host_wait_for_event(int tick_count, sdmmc_event_t* out_event)
  451. {
  452. if (!out_event) {
  453. return ESP_ERR_INVALID_ARG;
  454. }
  455. if (!s_event_queue) {
  456. return ESP_ERR_INVALID_STATE;
  457. }
  458. int ret = xQueueReceive(s_event_queue, out_event, tick_count);
  459. if (ret == pdFALSE) {
  460. return ESP_ERR_TIMEOUT;
  461. }
  462. return ESP_OK;
  463. }
  464. esp_err_t sdmmc_host_set_bus_width(int slot, size_t width)
  465. {
  466. if (!(slot == 0 || slot == 1)) {
  467. return ESP_ERR_INVALID_ARG;
  468. }
  469. if (sdmmc_slot_info[slot].width < width) {
  470. return ESP_ERR_INVALID_ARG;
  471. }
  472. const uint16_t mask = BIT(slot);
  473. if (width == 1) {
  474. SDMMC.ctype.card_width_8 &= ~mask;
  475. SDMMC.ctype.card_width &= ~mask;
  476. } else if (width == 4) {
  477. SDMMC.ctype.card_width_8 &= ~mask;
  478. SDMMC.ctype.card_width |= mask;
  479. // D3 was set to GPIO high to force slave into SD mode, until 4-bit mode is set
  480. configure_pin(d3, slot, GPIO_MODE_INPUT_OUTPUT);
  481. } else if (width == 8) {
  482. SDMMC.ctype.card_width_8 |= mask;
  483. // D3 was set to GPIO high to force slave into SD mode, until 4-bit mode is set
  484. configure_pin(d3, slot, GPIO_MODE_INPUT_OUTPUT);
  485. } else {
  486. return ESP_ERR_INVALID_ARG;
  487. }
  488. ESP_LOGD(TAG, "slot=%d width=%d", slot, width);
  489. return ESP_OK;
  490. }
  491. size_t sdmmc_host_get_slot_width(int slot)
  492. {
  493. assert( slot == 0 || slot == 1 );
  494. return s_slot_width[slot];
  495. }
  496. esp_err_t sdmmc_host_set_bus_ddr_mode(int slot, bool ddr_enabled)
  497. {
  498. if (!(slot == 0 || slot == 1)) {
  499. return ESP_ERR_INVALID_ARG;
  500. }
  501. if (s_slot_width[slot] == 8 && ddr_enabled) {
  502. ESP_LOGW(TAG, "DDR mode with 8-bit bus width is not supported yet");
  503. // requires reconfiguring controller clock for 2x card frequency
  504. return ESP_ERR_NOT_SUPPORTED;
  505. }
  506. uint32_t mask = BIT(slot);
  507. if (ddr_enabled) {
  508. SDMMC.uhs.ddr |= mask;
  509. SDMMC.emmc_ddr_reg |= mask;
  510. } else {
  511. SDMMC.uhs.ddr &= ~mask;
  512. SDMMC.emmc_ddr_reg &= ~mask;
  513. }
  514. ESP_LOGD(TAG, "slot=%d ddr=%d", slot, ddr_enabled ? 1 : 0);
  515. return ESP_OK;
  516. }
  517. static void sdmmc_host_dma_init(void)
  518. {
  519. SDMMC.ctrl.dma_enable = 1;
  520. SDMMC.bmod.val = 0;
  521. SDMMC.bmod.sw_reset = 1;
  522. SDMMC.idinten.ni = 1;
  523. SDMMC.idinten.ri = 1;
  524. SDMMC.idinten.ti = 1;
  525. }
  526. void sdmmc_host_dma_stop(void)
  527. {
  528. SDMMC.ctrl.use_internal_dma = 0;
  529. SDMMC.ctrl.dma_reset = 1;
  530. SDMMC.bmod.fb = 0;
  531. SDMMC.bmod.enable = 0;
  532. }
  533. void sdmmc_host_dma_prepare(sdmmc_desc_t* desc, size_t block_size, size_t data_size)
  534. {
  535. // Set size of data and DMA descriptor pointer
  536. SDMMC.bytcnt = data_size;
  537. SDMMC.blksiz = block_size;
  538. SDMMC.dbaddr = desc;
  539. // Enable everything needed to use DMA
  540. SDMMC.ctrl.dma_enable = 1;
  541. SDMMC.ctrl.use_internal_dma = 1;
  542. SDMMC.bmod.enable = 1;
  543. SDMMC.bmod.fb = 1;
  544. sdmmc_host_dma_resume();
  545. }
  546. void sdmmc_host_dma_resume(void)
  547. {
  548. SDMMC.pldmnd = 1;
  549. }
  550. bool sdmmc_host_card_busy(void)
  551. {
  552. return SDMMC.status.data_busy == 1;
  553. }
  554. esp_err_t sdmmc_host_io_int_enable(int slot)
  555. {
  556. configure_pin(d1, slot, GPIO_MODE_INPUT_OUTPUT);
  557. return ESP_OK;
  558. }
  559. esp_err_t sdmmc_host_io_int_wait(int slot, TickType_t timeout_ticks)
  560. {
  561. /* SDIO interrupts are negedge sensitive ones: the status bit is only set
  562. * when first interrupt triggered.
  563. *
  564. * If D1 GPIO is low when entering this function, we know that interrupt
  565. * (in SDIO sense) has occurred and we don't need to use SDMMC peripheral
  566. * interrupt.
  567. */
  568. SDMMC.intmask.sdio &= ~BIT(slot); /* Disable SDIO interrupt */
  569. SDMMC.rintsts.sdio = BIT(slot);
  570. if (gpio_get_level(GPIO_NUM(slot, d1)) == 0) {
  571. return ESP_OK;
  572. }
  573. /* Otherwise, need to wait for an interrupt. Since D1 was high,
  574. * SDMMC peripheral interrupt is guaranteed to trigger on negedge.
  575. */
  576. xSemaphoreTake(s_io_intr_event, 0);
  577. SDMMC.intmask.sdio |= BIT(slot); /* Re-enable SDIO interrupt */
  578. if (xSemaphoreTake(s_io_intr_event, timeout_ticks) == pdTRUE) {
  579. return ESP_OK;
  580. } else {
  581. return ESP_ERR_TIMEOUT;
  582. }
  583. }
  584. /**
  585. * @brief SDMMC interrupt handler
  586. *
  587. * All communication in SD protocol is driven by the master, and the hardware
  588. * handles things like stop commands automatically.
  589. * So the interrupt handler doesn't need to do much, we just push interrupt
  590. * status into a queue, clear interrupt flags, and let the task currently
  591. * doing communication figure out what to do next.
  592. * This also applies to SDIO interrupts which are generated by the slave.
  593. *
  594. * Card detect interrupts pose a small issue though, because if a card is
  595. * plugged in and out a few times, while there is no task to process
  596. * the events, event queue can become full and some card detect events
  597. * may be dropped. We ignore this problem for now, since the there are no other
  598. * interesting events which can get lost due to this.
  599. */
  600. static void sdmmc_isr(void* arg) {
  601. QueueHandle_t queue = (QueueHandle_t) arg;
  602. sdmmc_event_t event;
  603. int higher_priority_task_awoken = pdFALSE;
  604. uint32_t pending = SDMMC.mintsts.val & 0xFFFF;
  605. SDMMC.rintsts.val = pending;
  606. event.sdmmc_status = pending;
  607. uint32_t dma_pending = SDMMC.idsts.val;
  608. SDMMC.idsts.val = dma_pending;
  609. event.dma_status = dma_pending & 0x1f;
  610. if (pending != 0 || dma_pending != 0) {
  611. xQueueSendFromISR(queue, &event, &higher_priority_task_awoken);
  612. }
  613. uint32_t sdio_pending = SDMMC.mintsts.sdio;
  614. if (sdio_pending) {
  615. // disable the interrupt (no need to clear here, this is done in sdmmc_host_io_wait_int)
  616. SDMMC.intmask.sdio &= ~sdio_pending;
  617. xSemaphoreGiveFromISR(s_io_intr_event, &higher_priority_task_awoken);
  618. }
  619. if (higher_priority_task_awoken == pdTRUE) {
  620. portYIELD_FROM_ISR();
  621. }
  622. }
  623. static esp_err_t sdmmc_host_pullup_en_internal(int slot, int width)
  624. {
  625. if (width > sdmmc_slot_info[slot].width) {
  626. //in esp32 we only support 8 bit in slot 0, note this is occupied by the flash by default
  627. return ESP_ERR_INVALID_ARG;
  628. }
  629. // according to the spec, the host controls the clk, we don't to pull it up here
  630. gpio_pullup_en(GPIO_NUM(slot, cmd));
  631. gpio_pullup_en(GPIO_NUM(slot, d0));
  632. if (width >= 4) {
  633. gpio_pullup_en(GPIO_NUM(slot, d1));
  634. gpio_pullup_en(GPIO_NUM(slot, d2));
  635. gpio_pullup_en(GPIO_NUM(slot, d3));
  636. }
  637. if (width == 8) {
  638. gpio_pullup_en(GPIO_NUM(slot, d4));
  639. gpio_pullup_en(GPIO_NUM(slot, d5));
  640. gpio_pullup_en(GPIO_NUM(slot, d6));
  641. gpio_pullup_en(GPIO_NUM(slot, d7));
  642. }
  643. return ESP_OK;
  644. }
  645. /* Deprecared public function */
  646. esp_err_t sdmmc_host_pullup_en(int slot, int width) __attribute__((alias("sdmmc_host_pullup_en_internal")));