sdmmc_host.c 14 KB

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