sdmmc_host.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdbool.h>
  15. #include <stddef.h>
  16. #include <sys/param.h>
  17. #include "esp_log.h"
  18. #include "esp_intr_alloc.h"
  19. #include "soc/sdmmc_struct.h"
  20. #include "soc/sdmmc_reg.h"
  21. #include "soc/io_mux_reg.h"
  22. #include "soc/gpio_sig_map.h"
  23. #include "rom/gpio.h"
  24. #include "driver/gpio.h"
  25. #include "driver/sdmmc_host.h"
  26. #include "driver/periph_ctrl.h"
  27. #include "sdmmc_private.h"
  28. #define SDMMC_EVENT_QUEUE_LENGTH 32
  29. typedef struct {
  30. uint32_t clk;
  31. uint32_t cmd;
  32. uint32_t d0;
  33. uint32_t d1;
  34. uint32_t d2;
  35. uint32_t d3;
  36. uint32_t d4;
  37. uint32_t d5;
  38. uint32_t d6;
  39. uint32_t d7;
  40. uint8_t card_detect;
  41. uint8_t write_protect;
  42. uint8_t width;
  43. } sdmmc_slot_info_t;
  44. static void sdmmc_isr(void* arg);
  45. static void sdmmc_host_dma_init();
  46. static const sdmmc_slot_info_t s_slot_info[2] = {
  47. {
  48. .clk = PERIPHS_IO_MUX_SD_CLK_U,
  49. .cmd = PERIPHS_IO_MUX_SD_CMD_U,
  50. .d0 = PERIPHS_IO_MUX_SD_DATA0_U,
  51. .d1 = PERIPHS_IO_MUX_SD_DATA1_U,
  52. .d2 = PERIPHS_IO_MUX_SD_DATA2_U,
  53. .d3 = PERIPHS_IO_MUX_SD_DATA3_U,
  54. .d4 = PERIPHS_IO_MUX_GPIO16_U,
  55. .d5 = PERIPHS_IO_MUX_GPIO17_U,
  56. .d6 = PERIPHS_IO_MUX_GPIO5_U,
  57. .d7 = PERIPHS_IO_MUX_GPIO18_U,
  58. .card_detect = HOST_CARD_DETECT_N_1_IDX,
  59. .write_protect = HOST_CARD_WRITE_PRT_1_IDX,
  60. .width = 8
  61. },
  62. {
  63. .clk = PERIPHS_IO_MUX_MTMS_U,
  64. .cmd = PERIPHS_IO_MUX_MTDO_U,
  65. .d0 = PERIPHS_IO_MUX_GPIO2_U,
  66. .d1 = PERIPHS_IO_MUX_GPIO4_U,
  67. .d2 = PERIPHS_IO_MUX_MTDI_U,
  68. .d3 = PERIPHS_IO_MUX_MTCK_U,
  69. .card_detect = HOST_CARD_DETECT_N_2_IDX,
  70. .write_protect = HOST_CARD_WRITE_PRT_2_IDX,
  71. .width = 4
  72. }
  73. };
  74. static const char* TAG = "sdmmc_periph";
  75. static intr_handle_t s_intr_handle;
  76. static QueueHandle_t s_event_queue;
  77. void sdmmc_host_reset()
  78. {
  79. // Set reset bits
  80. SDMMC.ctrl.controller_reset = 1;
  81. SDMMC.ctrl.dma_reset = 1;
  82. SDMMC.ctrl.fifo_reset = 1;
  83. // Wait for the reset bits to be cleared by hardware
  84. while (SDMMC.ctrl.controller_reset || SDMMC.ctrl.fifo_reset || SDMMC.ctrl.dma_reset) {
  85. ;
  86. }
  87. }
  88. /* We have two clock divider stages:
  89. * - one is the clock generator which drives SDMMC peripheral,
  90. * it can be configured using SDMMC.clock register. It can generate
  91. * frequencies 160MHz/(N + 1), where 0 < N < 16, I.e. from 10 to 80 MHz.
  92. * - 4 clock dividers inside SDMMC peripheral, which can divide clock
  93. * from the first stage by 2 * M, where 0 < M < 255
  94. * (they can also be bypassed).
  95. *
  96. * For cards which aren't UHS-1 or UHS-2 cards, which we don't support,
  97. * maximum bus frequency in high speed (HS) mode is 50 MHz.
  98. * Note: for non-UHS-1 cards, HS mode is optional.
  99. * Default speed (DS) mode is mandatory, it works up to 25 MHz.
  100. * Whether the card supports HS or not can be determined using TRAN_SPEED
  101. * field of card's CSD register.
  102. *
  103. * 50 MHz can not be obtained exactly, closest we can get is 53 MHz.
  104. * For now set the first stage divider to generate 40MHz, and then configure
  105. * the second stage dividers to generate the frequency requested.
  106. *
  107. * Of the second stage dividers, div0 is used for card 0, and div1 is used
  108. * for card 1.
  109. */
  110. static void sdmmc_host_input_clk_enable()
  111. {
  112. // Set frequency to 160MHz / (p + 1) = 40MHz, duty cycle (h + 1)/(p + 1) = 1/2
  113. SDMMC.clock.div_factor_p = 3;
  114. SDMMC.clock.div_factor_h = 1;
  115. SDMMC.clock.div_factor_m = 3;
  116. // Set phases for in/out clocks
  117. SDMMC.clock.phase_dout = 4;
  118. SDMMC.clock.phase_din = 4;
  119. SDMMC.clock.phase_core = 0;
  120. // Wait for the clock to propagate
  121. ets_delay_us(10);
  122. }
  123. static void sdmmc_host_input_clk_disable()
  124. {
  125. SDMMC.clock.val = 0;
  126. }
  127. static void sdmmc_host_clock_update_command(int slot)
  128. {
  129. // Clock update command (not a real command; just updates CIU registers)
  130. sdmmc_hw_cmd_t cmd_val = {
  131. .card_num = slot,
  132. .update_clk_reg = 1,
  133. .wait_complete = 1
  134. };
  135. bool repeat = true;
  136. while(repeat) {
  137. sdmmc_host_start_command(slot, cmd_val, 0);
  138. while (true) {
  139. // Sending clock update command to the CIU can generate HLE error.
  140. // According to the manual, this is okay and we must retry the command.
  141. if (SDMMC.rintsts.hle) {
  142. SDMMC.rintsts.hle = 1;
  143. repeat = true;
  144. break;
  145. }
  146. // When the command is accepted by CIU, start_command bit will be
  147. // cleared in SDMMC.cmd register.
  148. if (SDMMC.cmd.start_command == 0) {
  149. repeat = false;
  150. break;
  151. }
  152. }
  153. }
  154. }
  155. esp_err_t sdmmc_host_set_card_clk(int slot, uint32_t freq_khz)
  156. {
  157. if (!(slot == 0 || slot == 1)) {
  158. return ESP_ERR_INVALID_ARG;
  159. }
  160. const int clk40m = 40000;
  161. // Disable clock first
  162. SDMMC.clkena.cclk_enable &= ~BIT(slot);
  163. sdmmc_host_clock_update_command(slot);
  164. // Calculate new dividers
  165. int div = 0;
  166. if (freq_khz < clk40m) {
  167. // round up; extra *2 is because clock divider divides by 2*n
  168. div = (clk40m + freq_khz * 2 - 1) / (freq_khz * 2);
  169. }
  170. ESP_LOGD(TAG, "slot=%d div=%d freq=%dkHz", slot, div,
  171. (div == 0) ? clk40m : clk40m / (2 * div));
  172. // Program CLKDIV and CLKSRC, send them to the CIU
  173. switch(slot) {
  174. case 0:
  175. SDMMC.clksrc.card0 = 0;
  176. SDMMC.clkdiv.div0 = div;
  177. break;
  178. case 1:
  179. SDMMC.clksrc.card1 = 1;
  180. SDMMC.clkdiv.div1 = div;
  181. break;
  182. }
  183. sdmmc_host_clock_update_command(slot);
  184. // Re-enable clocks
  185. SDMMC.clkena.cclk_enable |= BIT(slot);
  186. SDMMC.clkena.cclk_low_power |= BIT(slot);
  187. sdmmc_host_clock_update_command(slot);
  188. // set data timeout
  189. const uint32_t data_timeout_ms = 100;
  190. uint32_t data_timeout_cycles = data_timeout_ms * freq_khz;
  191. const uint32_t data_timeout_cycles_max = 0xffffff;
  192. if (data_timeout_cycles > data_timeout_cycles_max) {
  193. data_timeout_cycles = data_timeout_cycles_max;
  194. }
  195. SDMMC.tmout.data = data_timeout_cycles;
  196. // always set response timeout to highest value, it's small enough anyway
  197. SDMMC.tmout.response = 255;
  198. return ESP_OK;
  199. }
  200. esp_err_t sdmmc_host_start_command(int slot, sdmmc_hw_cmd_t cmd, uint32_t arg) {
  201. if (!(slot == 0 || slot == 1)) {
  202. return ESP_ERR_INVALID_ARG;
  203. }
  204. while (SDMMC.cmd.start_command == 1) {
  205. ;
  206. }
  207. SDMMC.cmdarg = arg;
  208. cmd.card_num = slot;
  209. cmd.start_command = 1;
  210. SDMMC.cmd = cmd;
  211. return ESP_OK;
  212. }
  213. esp_err_t sdmmc_host_init()
  214. {
  215. if (s_intr_handle) {
  216. return ESP_ERR_INVALID_STATE;
  217. }
  218. periph_module_enable(PERIPH_SDMMC_MODULE);
  219. // Enable clock to peripheral
  220. sdmmc_host_input_clk_enable();
  221. // Reset
  222. sdmmc_host_reset();
  223. ESP_LOGD(TAG, "peripheral version %x, hardware config %08x", SDMMC.verid, SDMMC.hcon);
  224. // Clear interrupt status and set interrupt mask to known state
  225. SDMMC.rintsts.val = 0xffffffff;
  226. SDMMC.intmask.val = 0;
  227. SDMMC.ctrl.int_enable = 0;
  228. // Allocate event queue
  229. s_event_queue = xQueueCreate(SDMMC_EVENT_QUEUE_LENGTH, sizeof(sdmmc_event_t));
  230. if (!s_event_queue) {
  231. return ESP_ERR_NO_MEM;
  232. }
  233. // Attach interrupt handler
  234. esp_err_t ret = esp_intr_alloc(ETS_SDIO_HOST_INTR_SOURCE, 0, &sdmmc_isr, s_event_queue, &s_intr_handle);
  235. if (ret != ESP_OK) {
  236. vQueueDelete(s_event_queue);
  237. s_event_queue = NULL;
  238. return ret;
  239. }
  240. // Enable interrupts
  241. SDMMC.intmask.val =
  242. SDMMC_INTMASK_CD |
  243. SDMMC_INTMASK_CMD_DONE |
  244. SDMMC_INTMASK_DATA_OVER |
  245. SDMMC_INTMASK_RCRC | SDMMC_INTMASK_DCRC |
  246. SDMMC_INTMASK_RTO | SDMMC_INTMASK_DTO | SDMMC_INTMASK_HTO |
  247. SDMMC_INTMASK_SBE | SDMMC_INTMASK_EBE |
  248. SDMMC_INTMASK_RESP_ERR | SDMMC_INTMASK_HLE;
  249. SDMMC.ctrl.int_enable = 1;
  250. // Enable DMA
  251. sdmmc_host_dma_init();
  252. // Initialize transaction handler
  253. ret = sdmmc_host_transaction_handler_init();
  254. if (ret != ESP_OK) {
  255. vQueueDelete(s_event_queue);
  256. s_event_queue = NULL;
  257. esp_intr_free(s_intr_handle);
  258. s_intr_handle = NULL;
  259. return ret;
  260. }
  261. return ESP_OK;
  262. }
  263. static inline void configure_pin(uint32_t io_mux_reg)
  264. {
  265. const int sdmmc_func = 3;
  266. const int drive_strength = 3;
  267. PIN_INPUT_ENABLE(io_mux_reg);
  268. PIN_FUNC_SELECT(io_mux_reg, sdmmc_func);
  269. PIN_SET_DRV(io_mux_reg, drive_strength);
  270. }
  271. esp_err_t sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t* slot_config)
  272. {
  273. if (!s_intr_handle) {
  274. return ESP_ERR_INVALID_STATE;
  275. }
  276. if (!(slot == 0 || slot == 1)) {
  277. return ESP_ERR_INVALID_ARG;
  278. }
  279. if (slot_config == NULL) {
  280. return ESP_ERR_INVALID_ARG;
  281. }
  282. int gpio_cd = slot_config->gpio_cd;
  283. int gpio_wp = slot_config->gpio_wp;
  284. uint8_t slot_width = slot_config->width;
  285. // Configure pins
  286. const sdmmc_slot_info_t* pslot = &s_slot_info[slot];
  287. if (slot_width == SDMMC_SLOT_WIDTH_DEFAULT) {
  288. slot_width = pslot->width;
  289. }
  290. else if (slot_width > pslot->width) {
  291. return ESP_ERR_INVALID_ARG;
  292. }
  293. configure_pin(pslot->clk);
  294. configure_pin(pslot->cmd);
  295. configure_pin(pslot->d0);
  296. if (slot_width >= 4) {
  297. configure_pin(pslot->d1);
  298. configure_pin(pslot->d2);
  299. configure_pin(pslot->d3);
  300. if (slot_width == 8) {
  301. configure_pin(pslot->d4);
  302. configure_pin(pslot->d5);
  303. configure_pin(pslot->d6);
  304. configure_pin(pslot->d7);
  305. }
  306. }
  307. if (gpio_cd != -1) {
  308. gpio_set_direction(gpio_cd, GPIO_MODE_INPUT);
  309. gpio_matrix_in(gpio_cd, pslot->card_detect, 0);
  310. }
  311. if (gpio_wp != -1) {
  312. gpio_set_direction(gpio_wp, GPIO_MODE_INPUT);
  313. gpio_matrix_in(gpio_wp, pslot->write_protect, 0);
  314. }
  315. // By default, set probing frequency (400kHz) and 1-bit bus
  316. esp_err_t ret = sdmmc_host_set_card_clk(slot, 400);
  317. if (ret != ESP_OK) {
  318. return ret;
  319. }
  320. ret = sdmmc_host_set_bus_width(slot, 1);
  321. if (ret != ESP_OK) {
  322. return ret;
  323. }
  324. return ESP_OK;
  325. }
  326. esp_err_t sdmmc_host_deinit()
  327. {
  328. if (!s_intr_handle) {
  329. return ESP_ERR_INVALID_STATE;
  330. }
  331. esp_intr_free(s_intr_handle);
  332. s_intr_handle = NULL;
  333. vQueueDelete(s_event_queue);
  334. s_event_queue = NULL;
  335. sdmmc_host_input_clk_disable();
  336. sdmmc_host_transaction_handler_deinit();
  337. periph_module_disable(PERIPH_SDMMC_MODULE);
  338. return ESP_OK;
  339. }
  340. esp_err_t sdmmc_host_wait_for_event(int tick_count, sdmmc_event_t* out_event)
  341. {
  342. if (!out_event) {
  343. return ESP_ERR_INVALID_ARG;
  344. }
  345. if (!s_event_queue) {
  346. return ESP_ERR_INVALID_STATE;
  347. }
  348. int ret = xQueueReceive(s_event_queue, out_event, tick_count);
  349. if (ret == pdFALSE) {
  350. return ESP_ERR_TIMEOUT;
  351. }
  352. return ESP_OK;
  353. }
  354. esp_err_t sdmmc_host_set_bus_width(int slot, size_t width)
  355. {
  356. if (!(slot == 0 || slot == 1)) {
  357. return ESP_ERR_INVALID_ARG;
  358. }
  359. if (s_slot_info[slot].width < width) {
  360. return ESP_ERR_INVALID_ARG;
  361. }
  362. const uint16_t mask = BIT(slot);
  363. if (width == 1) {
  364. SDMMC.ctype.card_width_8 &= ~mask;
  365. SDMMC.ctype.card_width &= ~mask;
  366. } else if (width == 4) {
  367. SDMMC.ctype.card_width_8 &= ~mask;
  368. SDMMC.ctype.card_width |= mask;
  369. } else if (width == 8){
  370. SDMMC.ctype.card_width_8 |= mask;
  371. } else {
  372. return ESP_ERR_INVALID_ARG;
  373. }
  374. ESP_LOGD(TAG, "slot=%d width=%d", slot, width);
  375. return ESP_OK;
  376. }
  377. static void sdmmc_host_dma_init()
  378. {
  379. SDMMC.ctrl.dma_enable = 1;
  380. SDMMC.bmod.val = 0;
  381. SDMMC.bmod.sw_reset = 1;
  382. SDMMC.idinten.ni = 1;
  383. SDMMC.idinten.ri = 1;
  384. SDMMC.idinten.ti = 1;
  385. }
  386. void sdmmc_host_dma_stop()
  387. {
  388. SDMMC.ctrl.use_internal_dma = 0;
  389. SDMMC.ctrl.dma_reset = 1;
  390. SDMMC.bmod.fb = 0;
  391. SDMMC.bmod.enable = 0;
  392. }
  393. void sdmmc_host_dma_prepare(sdmmc_desc_t* desc, size_t block_size, size_t data_size)
  394. {
  395. // Set size of data and DMA descriptor pointer
  396. SDMMC.bytcnt = data_size;
  397. SDMMC.blksiz = block_size;
  398. SDMMC.dbaddr = desc;
  399. // Enable everything needed to use DMA
  400. SDMMC.ctrl.dma_enable = 1;
  401. SDMMC.ctrl.use_internal_dma = 1;
  402. SDMMC.bmod.enable = 1;
  403. SDMMC.bmod.fb = 1;
  404. sdmmc_host_dma_resume();
  405. }
  406. void sdmmc_host_dma_resume()
  407. {
  408. SDMMC.pldmnd = 1;
  409. }
  410. /**
  411. * @brief SDMMC interrupt handler
  412. *
  413. * Ignoring SDIO and streaming read/writes for now (and considering just SD memory cards),
  414. * all communication is driven by the master, and the hardware handles things like stop
  415. * commands automatically. So the interrupt handler doesn't need to do much, we just push
  416. * interrupt status into a queue, clear interrupt flags, and let the task currently doing
  417. * communication figure out what to do next.
  418. *
  419. * Card detect interrupts pose a small issue though, because if a card is plugged in and
  420. * out a few times, while there is no task to process the events, event queue can become
  421. * full and some card detect events may be dropped. We ignore this problem for now, since
  422. * the there are no other interesting events which can get lost due to this.
  423. */
  424. static void sdmmc_isr(void* arg) {
  425. QueueHandle_t queue = (QueueHandle_t) arg;
  426. sdmmc_event_t event;
  427. uint32_t pending = SDMMC.mintsts.val;
  428. SDMMC.rintsts.val = pending;
  429. event.sdmmc_status = pending;
  430. uint32_t dma_pending = SDMMC.idsts.val;
  431. SDMMC.idsts.val = dma_pending;
  432. event.dma_status = dma_pending & 0x1f;
  433. int higher_priority_task_awoken = pdFALSE;
  434. xQueueSendFromISR(queue, &event, &higher_priority_task_awoken);
  435. if (higher_priority_task_awoken == pdTRUE) {
  436. portYIELD_FROM_ISR();
  437. }
  438. }