sdmmc_host.c 15 KB

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