esp_targets.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. } esp_target_t;
  25. // This ROM address has a different value on each chip model
  26. #define CHIP_DETECT_MAGIC_REG_ADDR 0x40001000
  27. #define ESP8266_SPI_REG_BASE 0x60000200
  28. #define ESP32S2_SPI_REG_BASE 0x3f402000
  29. #define ESP32C3_SPI_REG_BASE 0x60002000
  30. #define ESP32S3_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 = ESP32C3_SPI_REG_BASE + 0x00,
  84. .usr = ESP32C3_SPI_REG_BASE + 0x18,
  85. .usr1 = ESP32C3_SPI_REG_BASE + 0x1c,
  86. .usr2 = ESP32C3_SPI_REG_BASE + 0x20,
  87. .w0 = ESP32C3_SPI_REG_BASE + 0x58,
  88. .mosi_dlen = ESP32C3_SPI_REG_BASE + 0x24,
  89. .miso_dlen = ESP32C3_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 = ESP32C3_SPI_REG_BASE + 0x00,
  99. .usr = ESP32C3_SPI_REG_BASE + 0x18,
  100. .usr1 = ESP32C3_SPI_REG_BASE + 0x1c,
  101. .usr2 = ESP32C3_SPI_REG_BASE + 0x20,
  102. .w0 = ESP32C3_SPI_REG_BASE + 0x58,
  103. .mosi_dlen = ESP32C3_SPI_REG_BASE + 0x24,
  104. .miso_dlen = ESP32C3_SPI_REG_BASE + 0x28,
  105. },
  106. .efuse_base = 0x60007000,
  107. .chip_magic_value = { 0x00000009, 0 },
  108. .read_spi_config = spi_config_esp32xx, // !
  109. },
  110. };
  111. const target_registers_t *get_esp_target_data(target_chip_t chip)
  112. {
  113. return (target_registers_t *)&esp_target[chip];
  114. }
  115. esp_loader_error_t loader_detect_chip(target_chip_t *target_chip, const target_registers_t **target_data)
  116. {
  117. uint32_t magic_value;
  118. RETURN_ON_ERROR( esp_loader_read_register(CHIP_DETECT_MAGIC_REG_ADDR, &magic_value) );
  119. for (int chip = 0; chip < ESP_MAX_CHIP; chip++) {
  120. for(int index = 0; index < MAX_MAGIC_VALUES; index++) {
  121. if (magic_value == esp_target[chip].chip_magic_value[index]) {
  122. *target_chip = (target_chip_t)chip;
  123. *target_data = (target_registers_t *)&esp_target[chip];
  124. return ESP_LOADER_SUCCESS;
  125. }
  126. }
  127. }
  128. return ESP_LOADER_ERROR_INVALID_TARGET;
  129. }
  130. esp_loader_error_t loader_read_spi_config(target_chip_t target_chip, uint32_t *spi_config)
  131. {
  132. const esp_target_t *target = &esp_target[target_chip];
  133. return target->read_spi_config(target->efuse_base, spi_config);
  134. }
  135. static inline uint32_t efuse_word_addr(uint32_t efuse_base, uint32_t n)
  136. {
  137. return efuse_base + (n * 4);
  138. }
  139. // 30->GPIO32 | 31->GPIO33
  140. static inline uint8_t adjust_pin_number(uint8_t num)
  141. {
  142. return (num >= 30) ? num + 2 : num;
  143. }
  144. static esp_loader_error_t spi_config_esp32(uint32_t efuse_base, uint32_t *spi_config)
  145. {
  146. *spi_config = 0;
  147. uint32_t reg5, reg3;
  148. RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 5), &reg5) );
  149. RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 3), &reg3) );
  150. uint32_t pins = reg5 & 0xfffff;
  151. if (pins == 0 || pins == 0xfffff) {
  152. return ESP_LOADER_SUCCESS;
  153. }
  154. uint8_t clk = adjust_pin_number( (pins >> 0) & 0x1f );
  155. uint8_t q = adjust_pin_number( (pins >> 5) & 0x1f );
  156. uint8_t d = adjust_pin_number( (pins >> 10) & 0x1f );
  157. uint8_t cs = adjust_pin_number( (pins >> 15) & 0x1f );
  158. uint8_t hd = adjust_pin_number( (reg3 >> 4) & 0x1f );
  159. if (clk == cs || clk == d || clk == q || q == cs || q == d || q == d) {
  160. return ESP_LOADER_SUCCESS;
  161. }
  162. *spi_config = (hd << 24) | (cs << 18) | (d << 12) | (q << 6) | clk;
  163. return ESP_LOADER_SUCCESS;
  164. }
  165. // Applies for esp32s2, esp32c3 and esp32c3
  166. static esp_loader_error_t spi_config_esp32xx(uint32_t efuse_base, uint32_t *spi_config)
  167. {
  168. *spi_config = 0;
  169. uint32_t reg1, reg2;
  170. RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 18), &reg1) );
  171. RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 19), &reg2) );
  172. uint32_t pins = ((reg1 >> 16) | ((reg2 & 0xfffff) << 16)) & 0x3fffffff;
  173. if (pins == 0 || pins == 0xffffffff) {
  174. return ESP_LOADER_SUCCESS;
  175. }
  176. *spi_config = pins;
  177. return ESP_LOADER_SUCCESS;
  178. }