esp_targets.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* Copyright 2020 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. */
  15. #include "esp_targets.h"
  16. #include <stddef.h>
  17. #define MAX_MAGIC_VALUES 2
  18. typedef esp_loader_error_t (*read_spi_config_t)(uint32_t efuse_base, uint32_t *spi_config);
  19. typedef struct {
  20. target_registers_t regs;
  21. uint32_t efuse_base;
  22. uint32_t chip_magic_value[MAX_MAGIC_VALUES];
  23. read_spi_config_t read_spi_config;
  24. bool encryption_in_begin_flash_cmd;
  25. } esp_target_t;
  26. // This ROM address has a different value on each chip model
  27. #define CHIP_DETECT_MAGIC_REG_ADDR 0x40001000
  28. #define ESP8266_SPI_REG_BASE 0x60000200
  29. #define ESP32S2_SPI_REG_BASE 0x3f402000
  30. #define ESP32xx_SPI_REG_BASE 0x60002000
  31. #define ESP32_SPI_REG_BASE 0x3ff42000
  32. static esp_loader_error_t spi_config_esp32(uint32_t efuse_base, uint32_t *spi_config);
  33. static esp_loader_error_t spi_config_esp32xx(uint32_t efuse_base, uint32_t *spi_config);
  34. static const esp_target_t esp_target[ESP_MAX_CHIP] = {
  35. // ESP8266
  36. {
  37. .regs = {
  38. .cmd = ESP8266_SPI_REG_BASE + 0x00,
  39. .usr = ESP8266_SPI_REG_BASE + 0x1c,
  40. .usr1 = ESP8266_SPI_REG_BASE + 0x20,
  41. .usr2 = ESP8266_SPI_REG_BASE + 0x24,
  42. .w0 = ESP8266_SPI_REG_BASE + 0x40,
  43. .mosi_dlen = 0,
  44. .miso_dlen = 0,
  45. },
  46. .efuse_base = 0, // Not used
  47. .chip_magic_value = { 0xfff0c101, 0 },
  48. .read_spi_config = NULL, // Not used
  49. },
  50. // ESP32
  51. {
  52. .regs = {
  53. .cmd = ESP32_SPI_REG_BASE + 0x00,
  54. .usr = ESP32_SPI_REG_BASE + 0x1c,
  55. .usr1 = ESP32_SPI_REG_BASE + 0x20,
  56. .usr2 = ESP32_SPI_REG_BASE + 0x24,
  57. .w0 = ESP32_SPI_REG_BASE + 0x80,
  58. .mosi_dlen = ESP32_SPI_REG_BASE + 0x28,
  59. .miso_dlen = ESP32_SPI_REG_BASE + 0x2c,
  60. },
  61. .efuse_base = 0x3ff5A000,
  62. .chip_magic_value = { 0x00f01d83, 0 },
  63. .read_spi_config = spi_config_esp32,
  64. },
  65. // ESP32S2
  66. {
  67. .regs = {
  68. .cmd = ESP32S2_SPI_REG_BASE + 0x00,
  69. .usr = ESP32S2_SPI_REG_BASE + 0x18,
  70. .usr1 = ESP32S2_SPI_REG_BASE + 0x1c,
  71. .usr2 = ESP32S2_SPI_REG_BASE + 0x20,
  72. .w0 = ESP32S2_SPI_REG_BASE + 0x58,
  73. .mosi_dlen = ESP32S2_SPI_REG_BASE + 0x24,
  74. .miso_dlen = ESP32S2_SPI_REG_BASE + 0x28,
  75. },
  76. .efuse_base = 0x3f41A000,
  77. .chip_magic_value = { 0x000007c6, 0 },
  78. .read_spi_config = spi_config_esp32xx,
  79. },
  80. // ESP32C3
  81. {
  82. .regs = {
  83. .cmd = ESP32xx_SPI_REG_BASE + 0x00,
  84. .usr = ESP32xx_SPI_REG_BASE + 0x18,
  85. .usr1 = ESP32xx_SPI_REG_BASE + 0x1c,
  86. .usr2 = ESP32xx_SPI_REG_BASE + 0x20,
  87. .w0 = ESP32xx_SPI_REG_BASE + 0x58,
  88. .mosi_dlen = ESP32xx_SPI_REG_BASE + 0x24,
  89. .miso_dlen = ESP32xx_SPI_REG_BASE + 0x28,
  90. },
  91. .efuse_base = 0x60008800,
  92. .chip_magic_value = { 0x6921506f, 0x1b31506f },
  93. .read_spi_config = spi_config_esp32xx,
  94. },
  95. // ESP32S3
  96. {
  97. .regs = {
  98. .cmd = ESP32xx_SPI_REG_BASE + 0x00,
  99. .usr = ESP32xx_SPI_REG_BASE + 0x18,
  100. .usr1 = ESP32xx_SPI_REG_BASE + 0x1c,
  101. .usr2 = ESP32xx_SPI_REG_BASE + 0x20,
  102. .w0 = ESP32xx_SPI_REG_BASE + 0x58,
  103. .mosi_dlen = ESP32xx_SPI_REG_BASE + 0x24,
  104. .miso_dlen = ESP32xx_SPI_REG_BASE + 0x28,
  105. },
  106. .efuse_base = 0x60007000,
  107. .chip_magic_value = { 0x00000009, 0 },
  108. .read_spi_config = spi_config_esp32xx,
  109. },
  110. // ESP32C2
  111. {
  112. .regs = {
  113. .cmd = ESP32xx_SPI_REG_BASE + 0x00,
  114. .usr = ESP32xx_SPI_REG_BASE + 0x18,
  115. .usr1 = ESP32xx_SPI_REG_BASE + 0x1c,
  116. .usr2 = ESP32xx_SPI_REG_BASE + 0x20,
  117. .w0 = ESP32xx_SPI_REG_BASE + 0x58,
  118. .mosi_dlen = ESP32xx_SPI_REG_BASE + 0x24,
  119. .miso_dlen = ESP32xx_SPI_REG_BASE + 0x28,
  120. },
  121. .efuse_base = 0x60008800,
  122. .chip_magic_value = { 0x6f51306f, 0 },
  123. .read_spi_config = spi_config_esp32xx,
  124. },
  125. // ESP32H4
  126. {
  127. .regs = {
  128. .cmd = ESP32xx_SPI_REG_BASE + 0x00,
  129. .usr = ESP32xx_SPI_REG_BASE + 0x18,
  130. .usr1 = ESP32xx_SPI_REG_BASE + 0x1c,
  131. .usr2 = ESP32xx_SPI_REG_BASE + 0x20,
  132. .w0 = ESP32xx_SPI_REG_BASE + 0x58,
  133. .mosi_dlen = ESP32xx_SPI_REG_BASE + 0x24,
  134. .miso_dlen = ESP32xx_SPI_REG_BASE + 0x28,
  135. },
  136. .efuse_base = 0x6001A000,
  137. .chip_magic_value = {0xca26cc22, 0x6881b06f}, // ESP32H4-BETA1, ESP32H4-BETA2
  138. .read_spi_config = spi_config_esp32xx,
  139. },
  140. // ESP32H2
  141. {
  142. .regs = {
  143. .cmd = ESP32xx_SPI_REG_BASE + 0x00,
  144. .usr = ESP32xx_SPI_REG_BASE + 0x18,
  145. .usr1 = ESP32xx_SPI_REG_BASE + 0x1c,
  146. .usr2 = ESP32xx_SPI_REG_BASE + 0x20,
  147. .w0 = ESP32xx_SPI_REG_BASE + 0x58,
  148. .mosi_dlen = ESP32xx_SPI_REG_BASE + 0x24,
  149. .miso_dlen = ESP32xx_SPI_REG_BASE + 0x28,
  150. },
  151. .efuse_base = 0x6001A000,
  152. .chip_magic_value = {0xd7b73e80, 0},
  153. .read_spi_config = spi_config_esp32xx,
  154. },
  155. };
  156. const target_registers_t *get_esp_target_data(target_chip_t chip)
  157. {
  158. return (const target_registers_t *)&esp_target[chip];
  159. }
  160. esp_loader_error_t loader_detect_chip(target_chip_t *target_chip, const target_registers_t **target_data)
  161. {
  162. uint32_t magic_value;
  163. RETURN_ON_ERROR( esp_loader_read_register(CHIP_DETECT_MAGIC_REG_ADDR, &magic_value) );
  164. for (int chip = 0; chip < ESP_MAX_CHIP; chip++) {
  165. for(int index = 0; index < MAX_MAGIC_VALUES; index++) {
  166. if (magic_value == esp_target[chip].chip_magic_value[index]) {
  167. *target_chip = (target_chip_t)chip;
  168. *target_data = (target_registers_t *)&esp_target[chip];
  169. return ESP_LOADER_SUCCESS;
  170. }
  171. }
  172. }
  173. return ESP_LOADER_ERROR_INVALID_TARGET;
  174. }
  175. esp_loader_error_t loader_read_spi_config(target_chip_t target_chip, uint32_t *spi_config)
  176. {
  177. const esp_target_t *target = &esp_target[target_chip];
  178. return target->read_spi_config(target->efuse_base, spi_config);
  179. }
  180. static inline uint32_t efuse_word_addr(uint32_t efuse_base, uint32_t n)
  181. {
  182. return efuse_base + (n * 4);
  183. }
  184. // 30->GPIO32 | 31->GPIO33
  185. static inline uint8_t adjust_pin_number(uint8_t num)
  186. {
  187. return (num >= 30) ? num + 2 : num;
  188. }
  189. static esp_loader_error_t spi_config_esp32(uint32_t efuse_base, uint32_t *spi_config)
  190. {
  191. *spi_config = 0;
  192. uint32_t reg5, reg3;
  193. RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 5), &reg5) );
  194. RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 3), &reg3) );
  195. uint32_t pins = reg5 & 0xfffff;
  196. if (pins == 0 || pins == 0xfffff) {
  197. return ESP_LOADER_SUCCESS;
  198. }
  199. uint8_t clk = adjust_pin_number( (pins >> 0) & 0x1f );
  200. uint8_t q = adjust_pin_number( (pins >> 5) & 0x1f );
  201. uint8_t d = adjust_pin_number( (pins >> 10) & 0x1f );
  202. uint8_t cs = adjust_pin_number( (pins >> 15) & 0x1f );
  203. uint8_t hd = adjust_pin_number( (reg3 >> 4) & 0x1f );
  204. if (clk == cs || clk == d || clk == q || q == cs || q == d || q == d) {
  205. return ESP_LOADER_SUCCESS;
  206. }
  207. *spi_config = (hd << 24) | (cs << 18) | (d << 12) | (q << 6) | clk;
  208. return ESP_LOADER_SUCCESS;
  209. }
  210. // Applies for esp32s2, esp32c3 and esp32c3
  211. static esp_loader_error_t spi_config_esp32xx(uint32_t efuse_base, uint32_t *spi_config)
  212. {
  213. *spi_config = 0;
  214. uint32_t reg1, reg2;
  215. RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 18), &reg1) );
  216. RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 19), &reg2) );
  217. uint32_t pins = ((reg1 >> 16) | ((reg2 & 0xfffff) << 16)) & 0x3fffffff;
  218. if (pins == 0 || pins == 0xffffffff) {
  219. return ESP_LOADER_SUCCESS;
  220. }
  221. *spi_config = pins;
  222. return ESP_LOADER_SUCCESS;
  223. }
  224. bool encryption_in_begin_flash_cmd(target_chip_t target)
  225. {
  226. return target == ESP32_CHIP || target == ESP8266_CHIP;
  227. }