spi_flash_timing_tuning.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "sdkconfig.h"
  9. #include "esp_attr.h"
  10. #include "esp_err.h"
  11. #include "esp_types.h"
  12. #include "esp_log.h"
  13. #include "soc/spi_mem_reg.h"
  14. #include "soc/io_mux_reg.h"
  15. #include "esp_private/spi_flash_os.h"
  16. #include "soc/soc.h"
  17. #if CONFIG_IDF_TARGET_ESP32S3
  18. #include "esp32s3/spi_timing_config.h"
  19. #include "esp32s3/rom/cache.h"
  20. #endif
  21. #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(*(arr)))
  22. #if SPI_TIMING_FLASH_NEEDS_TUNING || SPI_TIMING_PSRAM_NEEDS_TUNING
  23. const static char *TAG = "MSPI Timing";
  24. static spi_timing_tuning_param_t s_flash_best_timing_tuning_config;
  25. static spi_timing_tuning_param_t s_psram_best_timing_tuning_config;
  26. #endif
  27. /*------------------------------------------------------------------------------
  28. * Common settings
  29. *----------------------------------------------------------------------------*/
  30. void spi_timing_set_pin_drive_strength(void)
  31. {
  32. //For now, set them all to 3. Need to check after QVL test results are out. TODO: IDF-3663
  33. //Set default clk
  34. SET_PERI_REG_MASK(SPI_MEM_DATE_REG(0), SPI_MEM_SPICLK_PAD_DRV_CTL_EN);
  35. REG_SET_FIELD(SPI_MEM_DATE_REG(0), SPI_MEM_SPI_SMEM_SPICLK_FUN_DRV, 3);
  36. REG_SET_FIELD(SPI_MEM_DATE_REG(0), SPI_MEM_SPI_FMEM_SPICLK_FUN_DRV, 3);
  37. //Set default mspi d0 ~ d7, dqs pin drive strength
  38. uint32_t regs[] = {IO_MUX_GPIO27_REG, IO_MUX_GPIO28_REG,
  39. IO_MUX_GPIO31_REG, IO_MUX_GPIO32_REG,
  40. IO_MUX_GPIO33_REG, IO_MUX_GPIO34_REG,
  41. IO_MUX_GPIO35_REG, IO_MUX_GPIO36_REG,
  42. IO_MUX_GPIO37_REG};
  43. for (int i = 0; i < ARRAY_SIZE(regs); i++) {
  44. PIN_SET_DRV(regs[i], 3);
  45. }
  46. }
  47. /*------------------------------------------------------------------------------
  48. * Static functions to get clock configs
  49. *----------------------------------------------------------------------------*/
  50. static spi_timing_config_core_clock_t get_mspi_core_clock(void)
  51. {
  52. return spi_timing_config_get_core_clock();
  53. }
  54. static uint32_t get_flash_clock_divider(void)
  55. {
  56. #if CONFIG_ESPTOOLPY_FLASHFREQ_20M
  57. return SPI_TIMING_CORE_CLOCK_MHZ / 20;
  58. #elif CONFIG_ESPTOOLPY_FLASHFREQ_40M
  59. return SPI_TIMING_CORE_CLOCK_MHZ / 40;
  60. #elif CONFIG_ESPTOOLPY_FLASHFREQ_80M
  61. return SPI_TIMING_CORE_CLOCK_MHZ / 80;
  62. #elif CONFIG_ESPTOOLPY_FLASHFREQ_120M
  63. return SPI_TIMING_CORE_CLOCK_MHZ / 120;
  64. #else
  65. abort();
  66. #endif
  67. }
  68. static uint32_t get_psram_clock_divider(void)
  69. {
  70. #if CONFIG_SPIRAM_SPEED_40M
  71. return SPI_TIMING_CORE_CLOCK_MHZ / 40;
  72. #elif CONFIG_SPIRAM_SPEED_80M
  73. return SPI_TIMING_CORE_CLOCK_MHZ / 80;
  74. #elif CONFIG_SPIRAM_SPEED_120M
  75. return SPI_TIMING_CORE_CLOCK_MHZ / 120;
  76. #else
  77. //Will enter this branch only if PSRAM is not enable
  78. return 0;
  79. #endif
  80. }
  81. #if SPI_TIMING_FLASH_NEEDS_TUNING || SPI_TIMING_PSRAM_NEEDS_TUNING
  82. /*------------------------------------------------------------------------------
  83. * Static functions to do timing tuning
  84. *----------------------------------------------------------------------------*/
  85. /**
  86. * Set timing tuning regs, in order to get successful sample points
  87. */
  88. static void init_spi1_for_tuning(bool is_flash)
  89. {
  90. //Get required core clock and module clock settings
  91. spi_timing_config_core_clock_t core_clock = get_mspi_core_clock();
  92. //Set SPI1 core clock. SPI0 and SPI1 share the register for core clock. So we only set SPI0 here.
  93. spi_timing_config_set_core_clock(0, core_clock);
  94. //Set SPI1 module clock as required
  95. if (is_flash) {
  96. uint32_t flash_div = get_flash_clock_divider();
  97. spi_timing_config_set_flash_clock(1, flash_div);
  98. //Power on HCLK
  99. REG_SET_BIT(SPI_MEM_TIMING_CALI_REG(0), SPI_MEM_TIMING_CLK_ENA);
  100. } else {
  101. //We use SPI1 Flash to tune PSRAM, PSRAM timing related regs do nothing on SPI1
  102. uint32_t psram_div = get_psram_clock_divider();
  103. spi_timing_config_set_flash_clock(1, psram_div);
  104. //Power on HCLK
  105. REG_SET_BIT(SPI_MEM_SPI_SMEM_TIMING_CALI_REG(0), SPI_MEM_SPI_SMEM_TIMING_CLK_ENA);
  106. }
  107. }
  108. /**
  109. * We use different SPI1 timing tuning config to read data to see if current MSPI sampling is successful.
  110. * The sampling result will be stored in an array. In this array, successful item will be 1, failed item will be 0.
  111. */
  112. static void sweep_for_success_sample_points(uint8_t *reference_data, const spi_timing_config_t *config, bool is_flash, uint8_t *out_array)
  113. {
  114. uint32_t config_idx = 0;
  115. uint8_t read_data[SPI_TIMING_TEST_DATA_LEN] = {0};
  116. for (config_idx = 0; config_idx < config->available_config_num; config_idx++) {
  117. memset(read_data, 0, SPI_TIMING_TEST_DATA_LEN);
  118. #if SPI_TIMING_FLASH_NEEDS_TUNING
  119. if (is_flash) {
  120. spi_timing_config_flash_tune_din_num_mode(config->tuning_config_table[config_idx].spi_din_mode, config->tuning_config_table[config_idx].spi_din_num);
  121. spi_timing_config_flash_tune_dummy(config->tuning_config_table[config_idx].extra_dummy_len);
  122. spi_timing_config_flash_read_data(1, read_data, SPI_TIMING_FLASH_TEST_DATA_ADDR, sizeof(read_data));
  123. }
  124. #endif
  125. #if SPI_TIMING_PSRAM_NEEDS_TUNING
  126. if (!is_flash) {
  127. spi_timing_config_psram_tune_din_num_mode(config->tuning_config_table[config_idx].spi_din_mode, config->tuning_config_table[config_idx].spi_din_num);
  128. spi_timing_config_psram_tune_dummy(config->tuning_config_table[config_idx].extra_dummy_len);
  129. spi_timing_config_psram_read_data(1, read_data, SPI_TIMING_PSRAM_TEST_DATA_ADDR, SPI_TIMING_TEST_DATA_LEN);
  130. }
  131. #endif
  132. if (memcmp(reference_data, read_data, sizeof(read_data)) == 0) {
  133. out_array[config_idx] = 1;
  134. }
  135. }
  136. }
  137. /**
  138. * Find consecutive successful sampling points.
  139. * e.g. array: {1, 1, 0, 0, 1, 1, 1, 0}
  140. * out_length: 3
  141. * outout_end_index: 6
  142. */
  143. static void find_max_consecutive_success_points(uint8_t *array, uint32_t size, uint32_t *out_length, uint32_t *out_end_index)
  144. {
  145. uint32_t max = 0;
  146. uint32_t match_num = 0;
  147. uint32_t i = 0;
  148. uint32_t end = 0;
  149. while (i < size) {
  150. if (array[i]) {
  151. match_num++;
  152. } else {
  153. if (match_num > max) {
  154. max = match_num;
  155. end = i - 1;
  156. }
  157. match_num = 0;
  158. }
  159. i++;
  160. }
  161. *out_length = match_num > max ? match_num : max;
  162. *out_end_index = match_num == size ? size : end;
  163. }
  164. #if SPI_TIMING_FLASH_DTR_MODE || SPI_TIMING_PSRAM_DTR_MODE
  165. static uint32_t select_best_tuning_config_dtr(spi_timing_config_t *config, uint32_t consecutive_length, uint32_t end)
  166. {
  167. #if (SPI_TIMING_CORE_CLOCK_MHZ == 160)
  168. //Core clock 160M DTR best point scheme
  169. uint32_t best_point;
  170. //Define these magic number in macros in `spi_timing_config.h`. TODO: IDF-3663
  171. if (consecutive_length <= 2 || consecutive_length >= 6) {
  172. //tuning is FAIL, select default point, and generate a warning
  173. best_point = config->default_config_id;
  174. ESP_EARLY_LOGW(TAG, "tuning fail, best point is fallen back to index %d", best_point);
  175. } else if (consecutive_length <= 4) {
  176. //consecutive length : 3 or 4
  177. best_point = end - 1;
  178. ESP_EARLY_LOGD(TAG,"tuning success, best point is index %d", best_point);
  179. } else {
  180. //consecutive point list length equals 5
  181. best_point = end - 2;
  182. ESP_EARLY_LOGD(TAG,"tuning success, best point is index %d", best_point);
  183. }
  184. return best_point;
  185. #else
  186. //won't reach here
  187. abort();
  188. #endif
  189. }
  190. #endif
  191. #if SPI_TIMING_FLASH_STR_MODE || SPI_TIMING_PSRAM_STR_MODE
  192. static uint32_t select_best_tuning_config_str(spi_timing_config_t *config, uint32_t consecutive_length, uint32_t end)
  193. {
  194. #if (SPI_TIMING_CORE_CLOCK_MHZ == 120 || SPI_TIMING_CORE_CLOCK_MHZ == 240)
  195. //STR best point scheme
  196. uint32_t best_point;
  197. if (consecutive_length <= 2|| consecutive_length >= 5) {
  198. //tuning is FAIL, select default point, and generate a warning
  199. best_point = config->default_config_id;
  200. ESP_EARLY_LOGW(TAG, "tuning fail, best point is fallen back to index %d", best_point);
  201. } else {
  202. //consecutive length : 3 or 4
  203. best_point = end - consecutive_length / 2;
  204. ESP_EARLY_LOGD(TAG,"tuning success, best point is index %d", best_point);
  205. }
  206. return best_point;
  207. #else
  208. //won't reach here
  209. abort();
  210. #endif
  211. }
  212. #endif
  213. static void select_best_tuning_config(spi_timing_config_t *config, uint32_t consecutive_length, uint32_t end, bool is_flash)
  214. {
  215. uint32_t best_point = 0;
  216. if (is_flash) {
  217. #if SPI_TIMING_FLASH_DTR_MODE
  218. best_point = select_best_tuning_config_dtr(config, consecutive_length, end);
  219. #elif SPI_TIMING_FLASH_STR_MODE
  220. best_point = select_best_tuning_config_str(config, consecutive_length, end);
  221. #endif
  222. s_flash_best_timing_tuning_config = config->tuning_config_table[best_point];
  223. } else {
  224. #if SPI_TIMING_PSRAM_DTR_MODE
  225. best_point = select_best_tuning_config_dtr(config, consecutive_length, end);
  226. #elif SPI_TIMING_PSRAM_STR_MODE
  227. best_point = select_best_tuning_config_str(config, consecutive_length, end);
  228. #endif
  229. s_psram_best_timing_tuning_config = config->tuning_config_table[best_point];
  230. }
  231. }
  232. static void do_tuning(uint8_t *reference_data, spi_timing_config_t *timing_config, bool is_flash)
  233. {
  234. /**
  235. * We use SPI1 to tune the timing:
  236. * 1. Get all SPI1 sampling results.
  237. * 2. Find the longest consecutive successful sampling points from the result above.
  238. * 3. The middle one will be the best sampling point.
  239. */
  240. uint32_t consecutive_length = 0;
  241. uint32_t last_success_point = 0;
  242. uint8_t sample_result[SPI_TIMING_CONFIG_NUM_DEFAULT] = {0};
  243. init_spi1_for_tuning(is_flash);
  244. sweep_for_success_sample_points(reference_data, timing_config, is_flash, sample_result);
  245. find_max_consecutive_success_points(sample_result, SPI_TIMING_CONFIG_NUM_DEFAULT, &consecutive_length, &last_success_point);
  246. select_best_tuning_config(timing_config, consecutive_length, last_success_point, is_flash);
  247. }
  248. #endif //#if SPI_TIMING_FLASH_NEEDS_TUNING || SPI_TIMING_PSRAM_NEEDS_TUNING
  249. /*------------------------------------------------------------------------------
  250. * FLASH Timing Tuning
  251. *----------------------------------------------------------------------------*/
  252. #if SPI_TIMING_FLASH_NEEDS_TUNING
  253. static void get_flash_tuning_configs(spi_timing_config_t *config)
  254. {
  255. #if SPI_TIMING_FLASH_DTR_MODE
  256. #define FLASH_MODE DTR_MODE
  257. #else //SPI_TIMING_FLASH_STR_MODE
  258. #define FLASH_MODE STR_MODE
  259. #endif
  260. #if CONFIG_ESPTOOLPY_FLASHFREQ_20M
  261. *config = SPI_TIMING_FLASH_GET_TUNING_CONFIG(SPI_TIMING_CORE_CLOCK_MHZ, 20, FLASH_MODE);
  262. #elif CONFIG_ESPTOOLPY_FLASHFREQ_40M
  263. *config = SPI_TIMING_FLASH_GET_TUNING_CONFIG(SPI_TIMING_CORE_CLOCK_MHZ, 40, FLASH_MODE);
  264. #elif CONFIG_ESPTOOLPY_FLASHFREQ_80M
  265. *config = SPI_TIMING_FLASH_GET_TUNING_CONFIG(SPI_TIMING_CORE_CLOCK_MHZ, 80, FLASH_MODE);
  266. #elif CONFIG_ESPTOOLPY_FLASHFREQ_120M
  267. *config = SPI_TIMING_FLASH_GET_TUNING_CONFIG(SPI_TIMING_CORE_CLOCK_MHZ, 120, FLASH_MODE);
  268. #endif
  269. #undef FLASH_MODE
  270. }
  271. void spi_timing_flash_tuning(void)
  272. {
  273. ESP_EARLY_LOGW("FLASH", "DO NOT USE FOR MASS PRODUCTION! Timing parameters will be updated in future IDF version.");
  274. /**
  275. * set SPI01 related regs to 20mhz configuration, to get reference data from FLASH
  276. * see detailed comments in this function (`spi_timing_enter_mspi_low_speed_mode`)
  277. */
  278. spi_timing_enter_mspi_low_speed_mode(true);
  279. //Disable the variable dummy mode when doing timing tuning
  280. CLEAR_PERI_REG_MASK(SPI_MEM_DDR_REG(1), SPI_MEM_SPI_FMEM_VAR_DUMMY); //GD flash will read error in variable mode with 20MHz
  281. uint8_t reference_data[SPI_TIMING_TEST_DATA_LEN] = {0};
  282. spi_timing_config_flash_read_data(1, reference_data, SPI_TIMING_FLASH_TEST_DATA_ADDR, sizeof(reference_data));
  283. spi_timing_config_t timing_configs = {0};
  284. get_flash_tuning_configs(&timing_configs);
  285. do_tuning(reference_data, &timing_configs, true);
  286. spi_timing_enter_mspi_high_speed_mode(true);
  287. }
  288. #else
  289. void spi_timing_flash_tuning(void)
  290. {
  291. //Empty function for compatibility, therefore upper layer won't need to know that FLASH in which operation mode and frequency config needs to be tuned
  292. }
  293. #endif //SPI_TIMING_FLASH_NEEDS_TUNING
  294. /*------------------------------------------------------------------------------
  295. * PSRAM Timing Tuning
  296. *----------------------------------------------------------------------------*/
  297. #if SPI_TIMING_PSRAM_NEEDS_TUNING
  298. static void get_psram_tuning_configs(spi_timing_config_t *config)
  299. {
  300. #if SPI_TIMING_PSRAM_DTR_MODE
  301. #define PSRAM_MODE DTR_MODE
  302. #else //SPI_TIMING_PSRAM_STR_MODE
  303. #define PSRAM_MODE STR_MODE
  304. #endif
  305. #if CONFIG_SPIRAM_SPEED_40M
  306. *config = SPI_TIMING_PSRAM_GET_TUNING_CONFIG(SPI_TIMING_CORE_CLOCK_MHZ, 40, PSRAM_MODE);
  307. #elif CONFIG_SPIRAM_SPEED_80M
  308. *config = SPI_TIMING_PSRAM_GET_TUNING_CONFIG(SPI_TIMING_CORE_CLOCK_MHZ, 80, PSRAM_MODE);
  309. #elif CONFIG_SPIRAM_SPEED_120M
  310. *config = SPI_TIMING_PSRAM_GET_TUNING_CONFIG(SPI_TIMING_CORE_CLOCK_MHZ, 120, PSRAM_MODE);
  311. #endif
  312. #undef PSRAM_MODE
  313. }
  314. void spi_timing_psram_tuning(void)
  315. {
  316. ESP_EARLY_LOGW("PSRAM", "DO NOT USE FOR MASS PRODUCTION! Timing parameters will be updated in future IDF version.");
  317. /**
  318. * set SPI01 related regs to 20mhz configuration, to write reference data to PSRAM
  319. * see detailed comments in this function (`spi_timing_enter_mspi_low_speed_mode`)
  320. */
  321. spi_timing_enter_mspi_low_speed_mode(true);
  322. // write data into psram, used to do timing tuning test.
  323. uint8_t reference_data[SPI_TIMING_TEST_DATA_LEN];
  324. for (int i=0; i < SPI_TIMING_TEST_DATA_LEN/4; i++) {
  325. ((uint32_t *)reference_data)[i] = 0xa5ff005a;
  326. }
  327. spi_timing_config_psram_write_data(1, reference_data, SPI_TIMING_PSRAM_TEST_DATA_ADDR, SPI_TIMING_TEST_DATA_LEN);
  328. spi_timing_config_t timing_configs = {0};
  329. get_psram_tuning_configs(&timing_configs);
  330. //Disable the variable dummy mode when doing timing tuning
  331. CLEAR_PERI_REG_MASK(SPI_MEM_DDR_REG(1), SPI_MEM_SPI_FMEM_VAR_DUMMY);
  332. //Get required config, and set them to PSRAM related registers
  333. do_tuning(reference_data, &timing_configs, false);
  334. spi_timing_enter_mspi_high_speed_mode(true);
  335. }
  336. #else
  337. void spi_timing_psram_tuning(void)
  338. {
  339. //Empty function for compatibility, therefore upper layer won't need to know that FLASH in which operation mode and frequency config needs to be tuned
  340. }
  341. #endif //SPI_TIMING_PSRAM_NEEDS_TUNING
  342. /*------------------------------------------------------------------------------
  343. * APIs to make SPI0 (and SPI1) FLASH work for high/low freq
  344. *----------------------------------------------------------------------------*/
  345. #if SPI_TIMING_FLASH_NEEDS_TUNING || SPI_TIMING_PSRAM_NEEDS_TUNING
  346. static void clear_timing_tuning_regs(bool control_spi1)
  347. {
  348. spi_timing_config_flash_set_din_mode_num(0, 0, 0); //SPI0 and SPI1 share the registers for flash din mode and num setting, so we only set SPI0's reg
  349. spi_timing_config_flash_set_extra_dummy(0, 0);
  350. if (control_spi1) {
  351. spi_timing_config_flash_set_extra_dummy(1, 0);
  352. } else {
  353. //Won't touch SPI1 registers
  354. }
  355. }
  356. #endif //#if SPI_TIMING_FLASH_NEEDS_TUNING || SPI_TIMING_PSRAM_NEEDS_TUNING
  357. void spi_timing_enter_mspi_low_speed_mode(bool control_spi1)
  358. {
  359. /**
  360. * Here we are going to slow the SPI1 frequency to 20Mhz, so we need to set SPI1 din_num and din_mode regs.
  361. *
  362. * Because SPI0 and SPI1 share the din_num and din_mode regs, so if we clear SPI1 din_num and din_mode to
  363. * 0, if the SPI0 flash module clock is still in high freq, it may not work correctly.
  364. *
  365. * Therefore, here we need to slow both the SPI0 and SPI1 and related timing tuning regs to 20Mhz configuration.
  366. */
  367. //Switch SPI1 and SPI0 clock as 20MHz, set its SPIMEM core clock as 80M and set clock division as 4
  368. spi_timing_config_set_core_clock(0, SPI_TIMING_CONFIG_CORE_CLOCK_80M); //SPI0 and SPI1 share the register for core clock. So we only set SPI0 here.
  369. spi_timing_config_set_flash_clock(0, 4);
  370. if (control_spi1) {
  371. //After tuning, won't touch SPI1 again
  372. spi_timing_config_set_flash_clock(1, 4);
  373. }
  374. #if SPI_TIMING_FLASH_NEEDS_TUNING || SPI_TIMING_PSRAM_NEEDS_TUNING
  375. clear_timing_tuning_regs(control_spi1);
  376. #endif
  377. }
  378. #if SPI_TIMING_FLASH_NEEDS_TUNING || SPI_TIMING_PSRAM_NEEDS_TUNING
  379. static void set_timing_tuning_regs_as_required(bool control_spi1)
  380. {
  381. //SPI0 and SPI1 share the registers for flash din mode and num setting, so we only set SPI0's reg
  382. spi_timing_config_flash_set_din_mode_num(0, s_flash_best_timing_tuning_config.spi_din_mode, s_flash_best_timing_tuning_config.spi_din_num);
  383. spi_timing_config_flash_set_extra_dummy(0, s_flash_best_timing_tuning_config.extra_dummy_len);
  384. if (control_spi1) {
  385. spi_timing_config_flash_set_extra_dummy(1, s_flash_best_timing_tuning_config.extra_dummy_len);
  386. }
  387. spi_timing_config_psram_set_din_mode_num(0, s_psram_best_timing_tuning_config.spi_din_mode, s_psram_best_timing_tuning_config.spi_din_num);
  388. spi_timing_config_psram_set_extra_dummy(0, s_psram_best_timing_tuning_config.extra_dummy_len);
  389. }
  390. #endif //#if SPI_TIMING_FLASH_NEEDS_TUNING || SPI_TIMING_PSRAM_NEEDS_TUNING
  391. /**
  392. * Set SPI0 FLASH and PSRAM module clock, din_num, din_mode and extra dummy,
  393. * according to the configuration got from timing tuning function (`calculate_best_flash_tuning_config`).
  394. * iF control_spi1 == 1, will also update SPI1 timing registers. Should only be set to 1 when do tuning.
  395. *
  396. * This function should always be called after `spi_timing_flash_tuning` or `calculate_best_flash_tuning_config`
  397. */
  398. void spi_timing_enter_mspi_high_speed_mode(bool control_spi1)
  399. {
  400. spi_timing_config_core_clock_t core_clock = get_mspi_core_clock();
  401. uint32_t flash_div = get_flash_clock_divider();
  402. uint32_t psram_div = get_psram_clock_divider();
  403. //Set SPI01 core clock
  404. spi_timing_config_set_core_clock(0, core_clock); //SPI0 and SPI1 share the register for core clock. So we only set SPI0 here.
  405. //Set FLASH module clock
  406. spi_timing_config_set_flash_clock(0, flash_div);
  407. if (control_spi1) {
  408. spi_timing_config_set_flash_clock(1, flash_div);
  409. }
  410. //Set PSRAM module clock
  411. spi_timing_config_set_psram_clock(0, psram_div);
  412. #if SPI_TIMING_FLASH_NEEDS_TUNING || SPI_TIMING_PSRAM_NEEDS_TUNING
  413. set_timing_tuning_regs_as_required(true);
  414. #endif
  415. }
  416. void spi_timing_change_speed_mode_cache_safe(bool switch_down)
  417. {
  418. Cache_Freeze_ICache_Enable(1);
  419. Cache_Freeze_DCache_Enable(1);
  420. if (switch_down) {
  421. //enter MSPI low speed mode, extra delays should be removed
  422. spi_timing_enter_mspi_low_speed_mode(false);
  423. } else {
  424. //enter MSPI high speed mode, extra delays should be considered
  425. spi_timing_enter_mspi_high_speed_mode(false);
  426. }
  427. Cache_Freeze_DCache_Disable();
  428. Cache_Freeze_ICache_Disable();
  429. }
  430. /*------------------------------------------------------------------------------
  431. * APIs to inform SPI1 Flash driver of necessary timing configurations
  432. *----------------------------------------------------------------------------*/
  433. bool spi_timing_is_tuned(void)
  434. {
  435. #if SPI_TIMING_FLASH_NEEDS_TUNING || SPI_TIMING_PSRAM_NEEDS_TUNING
  436. return true;
  437. #else
  438. return false;
  439. #endif
  440. }
  441. #if SPI_TIMING_FLASH_NEEDS_TUNING || SPI_TIMING_PSRAM_NEEDS_TUNING
  442. void spi_timing_get_flash_timing_param(spi_flash_hal_timing_config_t *out_timing_config)
  443. {
  444. // Get clock configuration directly from system.
  445. out_timing_config->clock_config.spimem.val = spi_timing_config_get_flash_clock_reg();
  446. // Get extra dummy length here. Therefore, no matter what freq, or mode.
  447. // If it needs tuning, it will return correct extra dummy len. If no tuning, it will return 0.
  448. out_timing_config->extra_dummy = s_flash_best_timing_tuning_config.extra_dummy_len;
  449. // Get CS setup/hold value here.
  450. spi_timing_config_get_cs_timing(&out_timing_config->cs_setup, &out_timing_config->cs_hold);
  451. }
  452. #else
  453. void spi_timing_get_flash_timing_param(spi_flash_hal_timing_config_t *out_timing_config)
  454. {
  455. // This function shouldn't be called if timing tuning is not used.
  456. abort();
  457. }
  458. #endif // SPI_TIMING_FLASH_NEEDS_TUNING || SPI_TIMING_PSRAM_NEEDS_TUNING