spi_flash_hal.c 4.9 KB

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