esp_targets.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. typedef esp_loader_error_t (*read_spi_config_t)(uint32_t efuse_base, uint32_t *spi_config);
  18. typedef struct {
  19. target_registers_t regs;
  20. uint32_t efuse_base;
  21. uint32_t chip_magic_value;
  22. read_spi_config_t read_spi_config;
  23. } esp_target_t;
  24. // This ROM address has a different value on each chip model
  25. #define CHIP_DETECT_MAGIC_REG_ADDR 0x40001000
  26. #define ESP8266_SPI_REG_BASE 0x60000200
  27. #define ESP32S2_SPI_REG_BASE 0x3f402000
  28. #define ESP32_SPI_REG_BASE 0x3ff42000
  29. static esp_loader_error_t spi_config_esp32(uint32_t efuse_base, uint32_t *spi_config);
  30. static esp_loader_error_t spi_config_esp32s2(uint32_t efuse_base, uint32_t *spi_config);
  31. static const esp_target_t esp_target[ESP_MAX_CHIP] = {
  32. // ESP8266
  33. {
  34. .regs = {
  35. .cmd = ESP8266_SPI_REG_BASE + 0x00,
  36. .usr = ESP8266_SPI_REG_BASE + 0x1c,
  37. .usr1 = ESP8266_SPI_REG_BASE + 0x20,
  38. .usr2 = ESP8266_SPI_REG_BASE + 0x24,
  39. .w0 = ESP8266_SPI_REG_BASE + 0x40,
  40. .mosi_dlen = 0,
  41. .miso_dlen = 0,
  42. },
  43. .efuse_base = 0, // Not used
  44. .chip_magic_value = 0xfff0c101,
  45. .read_spi_config = NULL, // Not used
  46. },
  47. // ESP32
  48. {
  49. .regs = {
  50. .cmd = ESP32_SPI_REG_BASE + 0x00,
  51. .usr = ESP32_SPI_REG_BASE + 0x1c,
  52. .usr1 = ESP32_SPI_REG_BASE + 0x20,
  53. .usr2 = ESP32_SPI_REG_BASE + 0x24,
  54. .w0 = ESP32_SPI_REG_BASE + 0x80,
  55. .mosi_dlen = ESP32_SPI_REG_BASE + 0x28,
  56. .miso_dlen = ESP32_SPI_REG_BASE + 0x2c,
  57. },
  58. .efuse_base = 0x3ff5A000,
  59. .chip_magic_value = 0x00f01d83,
  60. .read_spi_config = spi_config_esp32,
  61. },
  62. // ESP32S2
  63. {
  64. .regs = {
  65. .cmd = ESP32S2_SPI_REG_BASE + 0x00,
  66. .usr = ESP32S2_SPI_REG_BASE + 0x18,
  67. .usr1 = ESP32S2_SPI_REG_BASE + 0x1c,
  68. .usr2 = ESP32S2_SPI_REG_BASE + 0x20,
  69. .w0 = ESP32S2_SPI_REG_BASE + 0x58,
  70. .mosi_dlen = ESP32S2_SPI_REG_BASE + 0x24,
  71. .miso_dlen = ESP32S2_SPI_REG_BASE + 0x28,
  72. },
  73. .efuse_base = 0x3f41A000,
  74. .chip_magic_value = 0x000007c6,
  75. .read_spi_config = spi_config_esp32s2,
  76. }
  77. };
  78. const target_registers_t *get_esp_target_data(target_chip_t chip)
  79. {
  80. return (target_registers_t *)&esp_target[chip];
  81. }
  82. esp_loader_error_t loader_detect_chip(target_chip_t *target_chip, const target_registers_t **target_data)
  83. {
  84. uint32_t magic_value;
  85. RETURN_ON_ERROR( esp_loader_read_register(CHIP_DETECT_MAGIC_REG_ADDR, &magic_value) );
  86. for (int chip = 0; chip < ESP_MAX_CHIP; chip++) {
  87. if (magic_value == esp_target[chip].chip_magic_value) {
  88. *target_chip = (target_chip_t)chip;
  89. *target_data = (target_registers_t*)&esp_target[chip];
  90. return ESP_LOADER_SUCCESS;
  91. }
  92. }
  93. return ESP_LOADER_ERROR_INVALID_TARGET;
  94. }
  95. esp_loader_error_t loader_read_spi_config(target_chip_t target_chip, uint32_t *spi_config)
  96. {
  97. const esp_target_t *target = &esp_target[target_chip];
  98. return target->read_spi_config(target->efuse_base, spi_config);
  99. }
  100. static inline uint32_t efuse_word_addr(uint32_t efuse_base, uint32_t n)
  101. {
  102. return efuse_base + (n * 4);
  103. }
  104. // 30->GPIO32 | 31->GPIO33
  105. static inline uint8_t adjust_pin_number(uint8_t num)
  106. {
  107. return (num >= 30) ? num + 2 : num;
  108. }
  109. static esp_loader_error_t spi_config_esp32(uint32_t efuse_base, uint32_t *spi_config)
  110. {
  111. *spi_config = 0;
  112. uint32_t reg5, reg3;
  113. RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 5), &reg5) );
  114. RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 3), &reg3) );
  115. uint32_t pins = reg5 & 0xfffff;
  116. if (pins == 0 || pins == 0xfffff) {
  117. return ESP_LOADER_SUCCESS;
  118. }
  119. uint8_t clk = adjust_pin_number( (pins >> 0) & 0x1f );
  120. uint8_t q = adjust_pin_number( (pins >> 5) & 0x1f );
  121. uint8_t d = adjust_pin_number( (pins >> 10) & 0x1f );
  122. uint8_t cs = adjust_pin_number( (pins >> 15) & 0x1f );
  123. uint8_t hd = adjust_pin_number( (reg3 >> 4) & 0x1f );
  124. if (clk == cs || clk == d || clk == q || q == cs || q == d || q == d) {
  125. return ESP_LOADER_SUCCESS;
  126. }
  127. *spi_config = (hd << 24) | (cs << 18) | (d << 12) | (q << 6) | clk;
  128. return ESP_LOADER_SUCCESS;
  129. }
  130. static esp_loader_error_t spi_config_esp32s2(uint32_t efuse_base, uint32_t *spi_config)
  131. {
  132. *spi_config = 0;
  133. uint32_t reg1, reg2;
  134. RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 18), &reg1) );
  135. RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 19), &reg2) );
  136. uint32_t pins = ((reg1 >> 16) | ((reg2 & 0xfffff) << 16)) & 0x3fffffff;
  137. if (pins == 0 || pins == 0xffffffff) {
  138. return ESP_LOADER_SUCCESS;
  139. }
  140. *spi_config = pins;
  141. return ESP_LOADER_SUCCESS;
  142. }