spi_flash_hal.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. // HAL for SPI Flash (non-IRAM part)
  7. // The IRAM part is in spi_flash_hal_iram.c, spi_flash_hal_gpspi.c, spi_flash_hal_common.inc.
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <math.h>
  11. #include "soc/soc_caps.h"
  12. #include "hal/spi_flash_hal.h"
  13. #include "hal/assert.h"
  14. #include "hal/log.h"
  15. #include "hal/spi_flash_types.h"
  16. #define APB_CYCLE_NS (1000*1000*1000LL/APB_CLK_FREQ)
  17. static const char *TAG = "flash_hal";
  18. static uint32_t get_flash_clock_divider(const spi_flash_hal_config_t *cfg)
  19. {
  20. const int clk_source = cfg->clock_src_freq;
  21. const int clk_freq_mhz = cfg->freq_mhz;
  22. // On ESP32, ESP32-S2, ESP32-C3, we allow specific frequency 26.666MHz
  23. // If user passes freq_mhz like 26 or 27, it's allowed to use integer divider 3.
  24. // However on other chips or on other frequency, we only allow user pass frequency which
  25. // can be integer divided. If no, the following strategy is round up the division and
  26. // round down flash frequency to keep it safe.
  27. int best_div = 0;
  28. if (clk_source < clk_freq_mhz) {
  29. HAL_LOGE(TAG, "Target frequency %dMHz higher than supported.", clk_freq_mhz);
  30. abort();
  31. }
  32. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3
  33. if (clk_freq_mhz == 26 || clk_freq_mhz == 27) {
  34. best_div = 3;
  35. } else
  36. #endif
  37. {
  38. /* Do not use float/double as the FPU may not have been initialized yet on startup.
  39. * The values are in MHz, so for sure we won't have an overflow by adding them. */
  40. best_div = (clk_source + clk_freq_mhz - 1) / clk_freq_mhz;
  41. /* Perform a division that returns both quotient and remainder */
  42. const div_t res = div(clk_source, clk_freq_mhz);
  43. if (res.rem != 0) {
  44. HAL_LOGW(TAG, "Flash clock frequency round down to %d", res.quot);
  45. }
  46. }
  47. return best_div;
  48. }
  49. static uint32_t spi_flash_cal_clock(const spi_flash_hal_config_t *cfg)
  50. {
  51. uint32_t div_parameter = spi_flash_ll_calculate_clock_reg(cfg->host_id, get_flash_clock_divider(cfg));
  52. return div_parameter;
  53. }
  54. static inline int get_dummy_n(bool gpio_is_used, int input_delay_ns, int eff_clk)
  55. {
  56. const int apbclk_kHz = APB_CLK_FREQ / 1000;
  57. //calculate how many apb clocks a period has
  58. const int apbclk_n = APB_CLK_FREQ / eff_clk;
  59. const int gpio_delay_ns = gpio_is_used ? GPIO_MATRIX_DELAY_NS : 0;
  60. //calculate how many apb clocks the delay is, the 1 is to compensate in case ``input_delay_ns`` is rounded off.
  61. int apb_period_n = (1 + input_delay_ns + gpio_delay_ns) * apbclk_kHz / 1000 / 1000;
  62. if (apb_period_n < 0) {
  63. apb_period_n = 0;
  64. }
  65. return apb_period_n / apbclk_n;
  66. }
  67. #if SOC_SPI_MEM_SUPPORT_TIMING_TUNING
  68. static inline int extra_dummy_under_timing_tuning(const spi_flash_hal_config_t *cfg)
  69. {
  70. bool main_flash = (cfg->host_id == SPI1_HOST && cfg->cs_num == 0);
  71. int extra_dummy = 0;
  72. if (main_flash) {
  73. /**
  74. * 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.
  75. * Instead, for both Quad and Octal Flash, we use `usr_dummy` and set the whole dummy length (usr_dummy + extra_dummy) to this register.
  76. */
  77. extra_dummy = cfg->extra_dummy;
  78. } else {
  79. // TODO: for other flash chips, dummy get logic implement here. Currently, still calculate extra dummy by itself.
  80. abort();
  81. }
  82. return extra_dummy;
  83. }
  84. #endif //SOC_SPI_MEM_SUPPORT_TIMING_TUNING
  85. esp_err_t spi_flash_hal_init(spi_flash_hal_context_t *data_out, const spi_flash_hal_config_t *cfg)
  86. {
  87. if (cfg->cs_num >= SOC_SPI_PERIPH_CS_NUM(cfg->host_id)) {
  88. return ESP_ERR_INVALID_ARG;
  89. }
  90. *data_out = (spi_flash_hal_context_t) {
  91. .inst = data_out->inst, // Keeps the function pointer table
  92. .spi = spi_flash_ll_get_hw(cfg->host_id),
  93. .cs_num = cfg->cs_num,
  94. .cs_hold = cfg->cs_hold,
  95. .cs_setup = cfg->cs_setup,
  96. .base_io_mode = cfg->default_io_mode,
  97. .freq_mhz = cfg->freq_mhz,
  98. };
  99. #if SOC_SPI_MEM_SUPPORT_TIMING_TUNING
  100. if (cfg->using_timing_tuning) {
  101. data_out->extra_dummy = extra_dummy_under_timing_tuning(cfg);
  102. data_out->clock_conf = cfg->clock_config;
  103. } else
  104. #endif // SOC_SPI_MEM_SUPPORT_TIMING_TUNING
  105. {
  106. data_out->extra_dummy = get_dummy_n(!cfg->iomux, cfg->input_delay_ns, APB_CLK_FREQ/get_flash_clock_divider(cfg));
  107. data_out->clock_conf = (spi_flash_ll_clock_reg_t)spi_flash_cal_clock(cfg);
  108. }
  109. if (cfg->auto_sus_en) {
  110. data_out->flags |= SPI_FLASH_HOST_CONTEXT_FLAG_AUTO_SUSPEND;
  111. data_out->flags |= SPI_FLASH_HOST_CONTEXT_FLAG_AUTO_RESUME;
  112. data_out->tsus_val = cfg->tsus_val;
  113. }
  114. #if SOC_SPI_MEM_SUPPORT_OPI_MODE
  115. if (cfg->octal_mode_en) {
  116. data_out->flags |= SPI_FLASH_HOST_CONTEXT_FLAG_OCTAL_MODE;
  117. }
  118. if (cfg->default_io_mode == SPI_FLASH_OPI_DTR) {
  119. data_out->slicer_flags |= SPI_FLASH_HOST_CONTEXT_SLICER_FLAG_DTR;
  120. }
  121. #endif
  122. return ESP_OK;
  123. }
  124. bool spi_flash_hal_supports_direct_write(spi_flash_host_inst_t *host, const void *p)
  125. {
  126. (void)p;
  127. bool direct_write = (((spi_flash_hal_context_t *)host)->spi != spi_flash_ll_get_hw(SPI1_HOST));
  128. return direct_write;
  129. }
  130. bool spi_flash_hal_supports_direct_read(spi_flash_host_inst_t *host, const void *p)
  131. {
  132. (void)p;
  133. //currently the host doesn't support to read through dma, no word-aligned requirements
  134. bool direct_read = ( ((spi_flash_hal_context_t *)host)->spi != spi_flash_ll_get_hw(SPI1_HOST));
  135. return direct_read;
  136. }