spi_flash_hal.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2015-2018 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. // HAL for SPI Flash (non-IRAM part)
  15. // The IRAM part is in spi_flash_hal_iram.c, spi_flash_hal_gpspi.c, spi_flash_hal_common.inc.
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "soc/soc_caps.h"
  19. #include "hal/spi_flash_hal.h"
  20. #include "hal/log.h"
  21. #define APB_CYCLE_NS (1000*1000*1000LL/APB_CLK_FREQ)
  22. static const char TAG[] = "FLASH_HAL";
  23. typedef struct {
  24. int div;
  25. spi_flash_ll_clock_reg_t clock_reg_val;
  26. } spi_flash_hal_clock_config_t;
  27. static const spi_flash_hal_clock_config_t spi_flash_clk_cfg_reg[ESP_FLASH_SPEED_MAX] = {
  28. {16, SPI_FLASH_LL_CLKREG_VAL_5MHZ},
  29. {8, SPI_FLASH_LL_CLKREG_VAL_10MHZ},
  30. {4, SPI_FLASH_LL_CLKREG_VAL_20MHZ},
  31. {3, SPI_FLASH_LL_CLKREG_VAL_26MHZ},
  32. {2, SPI_FLASH_LL_CLKREG_VAL_40MHZ},
  33. {1, SPI_FLASH_LL_CLKREG_VAL_80MHZ},
  34. };
  35. #if !CONFIG_IDF_TARGET_ESP32
  36. static const spi_flash_hal_clock_config_t spi_flash_gpspi_clk_cfg_reg[ESP_FLASH_SPEED_MAX] = {
  37. {16, {.gpspi=GPSPI_FLASH_LL_CLKREG_VAL_5MHZ}},
  38. {8, {.gpspi=GPSPI_FLASH_LL_CLKREG_VAL_10MHZ}},
  39. {4, {.gpspi=GPSPI_FLASH_LL_CLKREG_VAL_20MHZ}},
  40. {3, {.gpspi=GPSPI_FLASH_LL_CLKREG_VAL_26MHZ}},
  41. {2, {.gpspi=GPSPI_FLASH_LL_CLKREG_VAL_40MHZ}},
  42. {1, {.gpspi=GPSPI_FLASH_LL_CLKREG_VAL_80MHZ}},
  43. };
  44. #else
  45. #define spi_flash_gpspi_clk_cfg_reg spi_flash_clk_cfg_reg
  46. #endif
  47. static inline int get_dummy_n(bool gpio_is_used, int input_delay_ns, int eff_clk)
  48. {
  49. const int apbclk_kHz = APB_CLK_FREQ / 1000;
  50. //calculate how many apb clocks a period has
  51. const int apbclk_n = APB_CLK_FREQ / eff_clk;
  52. const int gpio_delay_ns = gpio_is_used ? GPIO_MATRIX_DELAY_NS : 0;
  53. //calculate how many apb clocks the delay is, the 1 is to compensate in case ``input_delay_ns`` is rounded off.
  54. int apb_period_n = (1 + input_delay_ns + gpio_delay_ns) * apbclk_kHz / 1000 / 1000;
  55. if (apb_period_n < 0) {
  56. apb_period_n = 0;
  57. }
  58. return apb_period_n / apbclk_n;
  59. }
  60. #if SOC_SPI_MEM_SUPPORT_TIME_TUNING
  61. static inline int extra_dummy_under_timing_tuning(const spi_flash_hal_config_t *cfg)
  62. {
  63. bool main_flash = (cfg->host_id == SPI1_HOST && cfg->cs_num == 0);
  64. int extra_dummy = 0;
  65. if (main_flash) {
  66. /**
  67. * For Octal Flash, the dummy is `usr_dummy` + `extra_dummy`, they are in two different regs, we don't touch `extra_dummy` here, so set extra_dummy 0.
  68. * Instead, for both Quad and Octal Flash, we use `usr_dummy` and set the whole dummy length (usr_dummy + extra_dummy) to this register.
  69. */
  70. extra_dummy = cfg->extra_dummy;
  71. } else {
  72. // TODO: for other flash chips, dummy get logic implement here. Currently, still calculate extra dummy by itself.
  73. abort();
  74. }
  75. return extra_dummy;
  76. }
  77. #endif //SOC_SPI_MEM_SUPPORT_TIME_TUNING
  78. esp_err_t spi_flash_hal_init(spi_flash_hal_context_t *data_out, const spi_flash_hal_config_t *cfg)
  79. {
  80. if (!esp_ptr_internal(data_out) && cfg->host_id == SPI1_HOST) {
  81. return ESP_ERR_INVALID_ARG;
  82. }
  83. if (cfg->cs_num >= SOC_SPI_PERIPH_CS_NUM(cfg->host_id)) {
  84. return ESP_ERR_INVALID_ARG;
  85. }
  86. bool gpspi = (cfg->host_id > SPI1_HOST);
  87. const spi_flash_hal_clock_config_t *clock_cfg = gpspi? &spi_flash_gpspi_clk_cfg_reg[cfg->speed]: &spi_flash_clk_cfg_reg[cfg->speed];
  88. *data_out = (spi_flash_hal_context_t) {
  89. .inst = data_out->inst, // Keeps the function pointer table
  90. .spi = spi_flash_ll_get_hw(cfg->host_id),
  91. .cs_num = cfg->cs_num,
  92. .cs_hold = cfg->cs_hold,
  93. .cs_setup = cfg->cs_setup,
  94. .base_io_mode = cfg->default_io_mode,
  95. };
  96. #if SOC_SPI_MEM_SUPPORT_TIME_TUNING
  97. if (cfg->using_timing_tuning) {
  98. data_out->extra_dummy = extra_dummy_under_timing_tuning(cfg);
  99. data_out->clock_conf = cfg->clock_config;
  100. } else
  101. #endif // SOC_SPI_MEM_SUPPORT_TIME_TUNING
  102. {
  103. data_out->extra_dummy = get_dummy_n(!cfg->iomux, cfg->input_delay_ns, APB_CLK_FREQ/clock_cfg->div);
  104. data_out->clock_conf = clock_cfg->clock_reg_val;
  105. }
  106. if (cfg->auto_sus_en) {
  107. data_out->flags |= SPI_FLASH_HOST_CONTEXT_FLAG_AUTO_SUSPEND;
  108. data_out->flags |= SPI_FLASH_HOST_CONTEXT_FLAG_AUTO_RESUME;
  109. }
  110. #if SOC_SPI_MEM_SUPPORT_OPI_MODE
  111. if (cfg->octal_mode_en) {
  112. data_out->flags |= SPI_FLASH_HOST_CONTEXT_FLAG_OCTAL_MODE;
  113. }
  114. if (cfg->default_io_mode == SPI_FLASH_OPI_DTR) {
  115. data_out->slicer_flags |= SPI_FLASH_HOST_CONTEXT_SLICER_FLAG_DTR;
  116. }
  117. #endif
  118. HAL_LOGD(TAG, "extra_dummy: %d", data_out->extra_dummy);
  119. return ESP_OK;
  120. }
  121. bool spi_flash_hal_supports_direct_write(spi_flash_host_inst_t *host, const void *p)
  122. {
  123. bool direct_write = ( ((spi_flash_hal_context_t *)host)->spi != spi_flash_ll_get_hw(SPI1_HOST)
  124. || esp_ptr_in_dram(p) );
  125. return direct_write;
  126. }
  127. bool spi_flash_hal_supports_direct_read(spi_flash_host_inst_t *host, const void *p)
  128. {
  129. //currently the host doesn't support to read through dma, no word-aligned requirements
  130. bool direct_read = ( ((spi_flash_hal_context_t *)host)->spi != spi_flash_ll_get_hw(SPI1_HOST)
  131. || esp_ptr_in_dram(p) );
  132. return direct_read;
  133. }