1 /*
2  * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
3  * Adaptations to ESP-IDF Copyright (c) 2016-2018 Espressif Systems (Shanghai) PTE LTD
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #pragma once
18 
19 #include <string.h>
20 #include "esp_log.h"
21 #include "esp_heap_caps.h"
22 #include "freertos/FreeRTOS.h"
23 #include "freertos/task.h"
24 #include "driver/sdmmc_defs.h"
25 #include "driver/sdmmc_types.h"
26 #include "sdmmc_cmd.h"
27 #include "sys/param.h"
28 #include "soc/soc_memory_layout.h"
29 
30 #define SDMMC_GO_IDLE_DELAY_MS              20
31 #define SDMMC_IO_SEND_OP_COND_DELAY_MS      10
32 
33 /* These delay values are mostly useful for cases when CD pin is not used, and
34  * the card is removed. In this case, SDMMC peripheral may not always return
35  * CMD_DONE / DATA_DONE interrupts after signaling the error. These timeouts work
36  * as a safety net in such cases.
37  */
38 #define SDMMC_DEFAULT_CMD_TIMEOUT_MS  1000   // Max timeout of ordinary commands
39 #define SDMMC_WRITE_CMD_TIMEOUT_MS    5000   // Max timeout of write commands
40 
41 /* Maximum retry/error count for SEND_OP_COND (CMD1).
42  * These are somewhat arbitrary, values originate from OpenBSD driver.
43  */
44 #define SDMMC_SEND_OP_COND_MAX_RETRIES  100
45 #define SDMMC_SEND_OP_COND_MAX_ERRORS   3
46 
47 /* Functions to send individual commands */
48 esp_err_t sdmmc_send_cmd(sdmmc_card_t* card, sdmmc_command_t* cmd);
49 esp_err_t sdmmc_send_app_cmd(sdmmc_card_t* card, sdmmc_command_t* cmd);
50 esp_err_t sdmmc_send_cmd_go_idle_state(sdmmc_card_t* card);
51 esp_err_t sdmmc_send_cmd_send_if_cond(sdmmc_card_t* card, uint32_t ocr);
52 esp_err_t sdmmc_send_cmd_send_op_cond(sdmmc_card_t* card, uint32_t ocr, uint32_t *ocrp);
53 esp_err_t sdmmc_send_cmd_read_ocr(sdmmc_card_t *card, uint32_t *ocrp);
54 esp_err_t sdmmc_send_cmd_send_cid(sdmmc_card_t *card, sdmmc_cid_t *out_cid);
55 esp_err_t sdmmc_send_cmd_all_send_cid(sdmmc_card_t* card, sdmmc_response_t* out_raw_cid);
56 esp_err_t sdmmc_send_cmd_set_relative_addr(sdmmc_card_t* card, uint16_t* out_rca);
57 esp_err_t sdmmc_send_cmd_set_blocklen(sdmmc_card_t* card, sdmmc_csd_t* csd);
58 esp_err_t sdmmc_send_cmd_switch_func(sdmmc_card_t* card,
59         uint32_t mode, uint32_t group, uint32_t function,
60         sdmmc_switch_func_rsp_t* resp);
61 esp_err_t sdmmc_send_cmd_send_csd(sdmmc_card_t* card, sdmmc_csd_t* out_csd);
62 esp_err_t sdmmc_send_cmd_select_card(sdmmc_card_t* card, uint32_t rca);
63 esp_err_t sdmmc_send_cmd_send_scr(sdmmc_card_t* card, sdmmc_scr_t *out_scr);
64 esp_err_t sdmmc_send_cmd_set_bus_width(sdmmc_card_t* card, int width);
65 esp_err_t sdmmc_send_cmd_send_status(sdmmc_card_t* card, uint32_t* out_status);
66 esp_err_t sdmmc_send_cmd_crc_on_off(sdmmc_card_t* card, bool crc_enable);
67 
68 /* Higher level functions */
69 esp_err_t sdmmc_enable_hs_mode(sdmmc_card_t* card);
70 esp_err_t sdmmc_enable_hs_mode_and_check(sdmmc_card_t* card);
71 esp_err_t sdmmc_write_sectors_dma(sdmmc_card_t* card, const void* src,
72         size_t start_block, size_t block_count);
73 esp_err_t sdmmc_read_sectors_dma(sdmmc_card_t* card, void* dst,
74         size_t start_block, size_t block_count);
75 
76 /* SD specific */
77 esp_err_t sdmmc_check_scr(sdmmc_card_t* card);
78 esp_err_t sdmmc_decode_cid(sdmmc_response_t resp, sdmmc_cid_t* out_cid);
79 esp_err_t sdmmc_decode_csd(sdmmc_response_t response, sdmmc_csd_t* out_csd);
80 esp_err_t sdmmc_decode_scr(uint32_t *raw_scr, sdmmc_scr_t* out_scr);
81 
82 /* SDIO specific */
83 esp_err_t sdmmc_io_reset(sdmmc_card_t* card);
84 esp_err_t sdmmc_io_enable_hs_mode(sdmmc_card_t* card);
85 esp_err_t sdmmc_io_send_op_cond(sdmmc_card_t* card, uint32_t ocr, uint32_t *ocrp);
86 esp_err_t sdmmc_io_rw_direct(sdmmc_card_t* card, int function,
87         uint32_t reg, uint32_t arg, uint8_t *byte);
88 esp_err_t sdmmc_io_rw_extended(sdmmc_card_t* card, int function,
89         uint32_t reg, int arg, void *data, size_t size);
90 
91 
92 /* MMC specific */
93 esp_err_t sdmmc_mmc_send_ext_csd_data(sdmmc_card_t* card, void *out_data, size_t datalen);
94 esp_err_t sdmmc_mmc_switch(sdmmc_card_t* card, uint8_t set, uint8_t index, uint8_t value);
95 esp_err_t sdmmc_mmc_decode_cid(int mmc_ver, sdmmc_response_t resp, sdmmc_cid_t* out_cid);
96 esp_err_t sdmmc_mmc_decode_csd(sdmmc_response_t response, sdmmc_csd_t* out_csd);
97 esp_err_t sdmmc_mmc_enable_hs_mode(sdmmc_card_t* card);
98 
99 /* Parts of card initialization flow */
100 esp_err_t sdmmc_init_sd_if_cond(sdmmc_card_t* card);
101 esp_err_t sdmmc_init_select_card(sdmmc_card_t* card);
102 esp_err_t sdmmc_init_csd(sdmmc_card_t* card);
103 esp_err_t sdmmc_init_cid(sdmmc_card_t* card);
104 esp_err_t sdmmc_init_rca(sdmmc_card_t* card);
105 esp_err_t sdmmc_init_mmc_decode_cid(sdmmc_card_t* card);
106 esp_err_t sdmmc_init_ocr(sdmmc_card_t* card);
107 esp_err_t sdmmc_init_spi_crc(sdmmc_card_t* card);
108 esp_err_t sdmmc_init_io(sdmmc_card_t* card);
109 esp_err_t sdmmc_init_sd_blocklen(sdmmc_card_t* card);
110 esp_err_t sdmmc_init_sd_scr(sdmmc_card_t* card);
111 esp_err_t sdmmc_init_sd_wait_data_ready(sdmmc_card_t* card);
112 esp_err_t sdmmc_init_mmc_read_ext_csd(sdmmc_card_t* card);
113 esp_err_t sdmmc_init_mmc_read_cid(sdmmc_card_t* card);
114 esp_err_t sdmmc_init_host_bus_width(sdmmc_card_t* card);
115 esp_err_t sdmmc_init_sd_bus_width(sdmmc_card_t* card);
116 esp_err_t sdmmc_init_io_bus_width(sdmmc_card_t* card);
117 esp_err_t sdmmc_init_mmc_bus_width(sdmmc_card_t* card);
118 esp_err_t sdmmc_init_card_hs_mode(sdmmc_card_t* card);
119 esp_err_t sdmmc_init_host_frequency(sdmmc_card_t* card);
120 esp_err_t sdmmc_init_mmc_check_csd(sdmmc_card_t* card);
121 
122 /* Various helper functions */
host_is_spi(const sdmmc_card_t * card)123 static inline bool host_is_spi(const sdmmc_card_t* card)
124 {
125     return (card->host.flags & SDMMC_HOST_FLAG_SPI) != 0;
126 }
127 
get_host_ocr(float voltage)128 static inline uint32_t get_host_ocr(float voltage)
129 {
130     // TODO: report exact voltage to the card
131     // For now tell that the host has 2.8-3.6V voltage range
132     (void) voltage;
133     return SD_OCR_VOL_MASK;
134 }
135 
136 void sdmmc_flip_byte_order(uint32_t* response, size_t size);
137 
138 esp_err_t sdmmc_fix_host_flags(sdmmc_card_t* card);
139