spiram_psram.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. Driver bits for PSRAM chips (at the moment only the ESP-PSRAM32 chip).
  3. */
  4. // Copyright 2013-2017 Espressif Systems (Shanghai) PTE LTD
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. #include "sdkconfig.h"
  18. #include "string.h"
  19. #include "esp_attr.h"
  20. #include "esp_err.h"
  21. #include "esp_types.h"
  22. #include "esp_log.h"
  23. #include "spiram_psram.h"
  24. #include "esp32s3/rom/spi_flash.h"
  25. #include "esp32s3/rom/opi_flash.h"
  26. #include "esp32s3/rom/cache.h"
  27. #include "esp32s3/rom/efuse.h"
  28. #include "esp_rom_gpio.h"
  29. #include "esp_rom_efuse.h"
  30. #include "soc/dport_reg.h"
  31. #include "soc/efuse_periph.h"
  32. #include "soc/soc_caps.h"
  33. #include "soc/io_mux_reg.h"
  34. #include "soc/apb_ctrl_reg.h"
  35. #include "soc/efuse_reg.h"
  36. #include "soc/soc.h"
  37. #include "soc/io_mux_reg.h"
  38. #include "driver/gpio.h"
  39. #include "hal/gpio_hal.h"
  40. #include "driver/spi_common_internal.h"
  41. #include "driver/spi_common.h"
  42. #include "driver/periph_ctrl.h"
  43. #include "bootloader_common.h"
  44. #if CONFIG_SPIRAM
  45. #include "soc/rtc.h"
  46. static const char* TAG = "psram";
  47. //Commands for PSRAM chip
  48. #define PSRAM_READ 0x03
  49. #define PSRAM_FAST_READ 0x0B
  50. #define PSRAM_FAST_READ_DUMMY 0x3
  51. #define PSRAM_FAST_READ_QUAD 0xEB
  52. #define PSRAM_FAST_READ_QUAD_DUMMY 0x5
  53. #define PSRAM_WRITE 0x02
  54. #define PSRAM_QUAD_WRITE 0x38
  55. #define PSRAM_ENTER_QMODE 0x35
  56. #define PSRAM_EXIT_QMODE 0xF5
  57. #define PSRAM_RESET_EN 0x66
  58. #define PSRAM_RESET 0x99
  59. #define PSRAM_SET_BURST_LEN 0xC0
  60. #define PSRAM_DEVICE_ID 0x9F
  61. // ID
  62. #define PSRAM_ID_KGD_M 0xff
  63. #define PSRAM_ID_KGD_S 8
  64. #define PSRAM_ID_KGD 0x5d
  65. #define PSRAM_ID_EID_M 0xff
  66. #define PSRAM_ID_EID_S 16
  67. // Use the [7:5](bit7~bit5) of EID to distinguish the psram size:
  68. //
  69. // BIT7 | BIT6 | BIT5 | SIZE(MBIT)
  70. // -------------------------------------
  71. // 0 | 0 | 0 | 16
  72. // 0 | 0 | 1 | 32
  73. // 0 | 1 | 0 | 64
  74. #define PSRAM_EID_SIZE_M 0x07
  75. #define PSRAM_EID_SIZE_S 5
  76. #define PSRAM_KGD(id) (((id) >> PSRAM_ID_KGD_S) & PSRAM_ID_KGD_M)
  77. #define PSRAM_EID(id) (((id) >> PSRAM_ID_EID_S) & PSRAM_ID_EID_M)
  78. #define PSRAM_SIZE_ID(id) ((PSRAM_EID(id) >> PSRAM_EID_SIZE_S) & PSRAM_EID_SIZE_M)
  79. #define PSRAM_IS_VALID(id) (PSRAM_KGD(id) == PSRAM_ID_KGD)
  80. // For the old version 32Mbit psram, using the spicial driver */
  81. #define PSRAM_IS_32MBIT_VER0(id) (PSRAM_EID(id) == 0x20)
  82. #define PSRAM_IS_64MBIT_TRIAL(id) (PSRAM_EID(id) == 0x26)
  83. // IO-pins for PSRAM.
  84. // WARNING: PSRAM shares all but the CS and CLK pins with the flash, so these defines
  85. // hardcode the flash pins as well, making this code incompatible with either a setup
  86. // that has the flash on non-standard pins or ESP32s with built-in flash.
  87. #define FLASH_CLK_IO SPI_CLK_GPIO_NUM
  88. #define FLASH_CS_IO SPI_CS0_GPIO_NUM
  89. // PSRAM clock and cs IO should be configured based on hardware design.
  90. #define PSRAM_CLK_IO CONFIG_DEFAULT_PSRAM_CLK_IO // Default value is 30
  91. #define PSRAM_CS_IO CONFIG_DEFAULT_PSRAM_CS_IO // Default value is 26
  92. #define PSRAM_SPIQ_SD0_IO SPI_Q_GPIO_NUM
  93. #define PSRAM_SPID_SD1_IO SPI_D_GPIO_NUM
  94. #define PSRAM_SPIWP_SD3_IO SPI_WP_GPIO_NUM
  95. #define PSRAM_SPIHD_SD2_IO SPI_HD_GPIO_NUM
  96. #define CS_PSRAM_SEL SPI_MEM_CS1_DIS_M
  97. #define CS_FLASH_SEL SPI_MEM_CS0_DIS_M
  98. #define PSRAM_IO_MATRIX_DUMMY_20M 0
  99. #define PSRAM_IO_MATRIX_DUMMY_40M 0
  100. #define PSRAM_IO_MATRIX_DUMMY_80M 0
  101. #define _SPI_CACHE_PORT 0
  102. #define _SPI_FLASH_PORT 1
  103. #define _SPI_80M_CLK_DIV 1
  104. #define _SPI_40M_CLK_DIV 2
  105. #define _SPI_20M_CLK_DIV 4
  106. typedef enum {
  107. PSRAM_CLK_MODE_NORM = 0, /*!< Normal SPI mode */
  108. PSRAM_CLK_MODE_A1C, /*!< ONE extra clock cycles after CS is set high level */
  109. PSRAM_CLK_MODE_A2C, /*!< Two extra clock cycles after CS is set high level */
  110. PSRAM_CLK_MODE_ALON, /*!< clock always on */
  111. PSRAM_CLK_MODE_MAX,
  112. } psram_clk_mode_t;
  113. typedef enum {
  114. PSRAM_EID_SIZE_16MBITS = 0,
  115. PSRAM_EID_SIZE_32MBITS = 1,
  116. PSRAM_EID_SIZE_64MBITS = 2,
  117. } psram_eid_size_t;
  118. typedef struct {
  119. uint8_t flash_clk_io;
  120. uint8_t flash_cs_io;
  121. uint8_t psram_clk_io;
  122. uint8_t psram_cs_io;
  123. uint8_t psram_spiq_sd0_io;
  124. uint8_t psram_spid_sd1_io;
  125. uint8_t psram_spiwp_sd3_io;
  126. uint8_t psram_spihd_sd2_io;
  127. } psram_io_t;
  128. #define PSRAM_IO_CONF_DEFAULT() { \
  129. .flash_clk_io = FLASH_CLK_IO, \
  130. .flash_cs_io = FLASH_CS_IO, \
  131. .psram_clk_io = PSRAM_CLK_IO, \
  132. .psram_cs_io = PSRAM_CS_IO, \
  133. .psram_spiq_sd0_io = PSRAM_SPIQ_SD0_IO, \
  134. .psram_spid_sd1_io = PSRAM_SPID_SD1_IO, \
  135. .psram_spiwp_sd3_io = PSRAM_SPIWP_SD3_IO, \
  136. .psram_spihd_sd2_io = PSRAM_SPIHD_SD2_IO, \
  137. }
  138. typedef enum {
  139. PSRAM_SPI_1 = 0x1,
  140. /* PSRAM_SPI_2, */
  141. /* PSRAM_SPI_3, */
  142. PSRAM_SPI_MAX ,
  143. } psram_spi_num_t;
  144. typedef enum {
  145. PSRAM_CMD_QPI,
  146. PSRAM_CMD_SPI,
  147. } psram_cmd_mode_t;
  148. typedef esp_rom_spi_cmd_t psram_cmd_t;
  149. static uint32_t s_psram_id = 0;
  150. static void IRAM_ATTR psram_cache_init(psram_cache_mode_t psram_cache_mode, psram_vaddr_mode_t vaddrmode);
  151. extern void esp_rom_spi_set_op_mode(int spi_num, esp_rom_spiflash_read_mode_t mode);
  152. static void psram_set_op_mode(int spi_num, psram_cmd_mode_t mode)
  153. {
  154. if (mode == PSRAM_CMD_QPI) {
  155. esp_rom_spi_set_op_mode(spi_num, ESP_ROM_SPIFLASH_QIO_MODE);
  156. SET_PERI_REG_MASK(SPI_MEM_CTRL_REG(spi_num), SPI_MEM_FCMD_QUAD_M);
  157. } else if (mode == PSRAM_CMD_SPI) {
  158. esp_rom_spi_set_op_mode(spi_num, ESP_ROM_SPIFLASH_SLOWRD_MODE);
  159. }
  160. }
  161. static void _psram_exec_cmd(int spi_num,
  162. uint32_t cmd, int cmd_bit_len,
  163. uint32_t addr, int addr_bit_len,
  164. int dummy_bits,
  165. uint8_t* mosi_data, int mosi_bit_len,
  166. uint8_t* miso_data, int miso_bit_len)
  167. {
  168. esp_rom_spi_cmd_t conf;
  169. uint32_t _addr = addr;
  170. conf.addr = &_addr;
  171. conf.addrBitLen = addr_bit_len;
  172. conf.cmd = cmd;
  173. conf.cmdBitLen = cmd_bit_len;
  174. conf.dummyBitLen = dummy_bits; // There is a hardware approach on chip723
  175. conf.txData = (uint32_t*) mosi_data;
  176. conf.txDataBitLen = mosi_bit_len;
  177. conf.rxData = (uint32_t*) miso_data;
  178. conf.rxDataBitLen = miso_bit_len;
  179. esp_rom_spi_cmd_config(spi_num, &conf);
  180. }
  181. void psram_exec_cmd(int spi_num, psram_cmd_mode_t mode,
  182. uint32_t cmd, int cmd_bit_len,
  183. uint32_t addr, int addr_bit_len,
  184. int dummy_bits,
  185. uint8_t* mosi_data, int mosi_bit_len,
  186. uint8_t* miso_data, int miso_bit_len,
  187. uint32_t cs_mask,
  188. bool is_write_erase_operation)
  189. {
  190. uint32_t backup_usr = READ_PERI_REG(SPI_MEM_USER_REG(spi_num));
  191. uint32_t backup_usr1 = READ_PERI_REG(SPI_MEM_USER1_REG(spi_num));
  192. uint32_t backup_usr2 = READ_PERI_REG(SPI_MEM_USER2_REG(spi_num));
  193. uint32_t backup_ctrl = READ_PERI_REG(SPI_MEM_CTRL_REG(spi_num));
  194. psram_set_op_mode(spi_num, mode);
  195. _psram_exec_cmd(spi_num, cmd, cmd_bit_len, addr, addr_bit_len,
  196. dummy_bits, mosi_data, mosi_bit_len, miso_data, miso_bit_len);
  197. esp_rom_spi_cmd_start(spi_num, miso_data, miso_bit_len / 8, cs_mask, is_write_erase_operation);
  198. WRITE_PERI_REG(SPI_MEM_USER_REG(spi_num), backup_usr);
  199. WRITE_PERI_REG(SPI_MEM_USER1_REG(spi_num), backup_usr1);
  200. WRITE_PERI_REG(SPI_MEM_USER2_REG(spi_num), backup_usr2);
  201. WRITE_PERI_REG(SPI_MEM_CTRL_REG(spi_num), backup_ctrl);
  202. }
  203. //exit QPI mode(set back to SPI mode)
  204. static void psram_disable_qio_mode(int spi_num)
  205. {
  206. psram_exec_cmd(spi_num, PSRAM_CMD_QPI,
  207. PSRAM_EXIT_QMODE, 8, /* command and command bit len*/
  208. 0, 0, /* address and address bit len*/
  209. 0, /* dummy bit len */
  210. NULL, 0, /* tx data and tx bit len*/
  211. NULL, 0, /* rx data and rx bit len*/
  212. CS_PSRAM_SEL, /* cs bit mask*/
  213. false); /* whether is program/erase operation */
  214. }
  215. //switch psram burst length(32 bytes or 1024 bytes)
  216. //datasheet says it should be 1024 bytes by default
  217. static void psram_set_wrap_burst_length(int spi_num, psram_cmd_mode_t mode)
  218. {
  219. psram_exec_cmd(spi_num, mode,
  220. PSRAM_SET_BURST_LEN, 8, /* command and command bit len*/
  221. 0, 0, /* address and address bit len*/
  222. 0, /* dummy bit len */
  223. NULL, 0, /* tx data and tx bit len*/
  224. NULL, 0, /* rx data and rx bit len*/
  225. CS_PSRAM_SEL, /* cs bit mask*/
  226. false); /* whether is program/erase operation */
  227. }
  228. //send reset command to psram, in spi mode
  229. static void psram_reset_mode(int spi_num)
  230. {
  231. psram_exec_cmd(spi_num, PSRAM_CMD_SPI,
  232. PSRAM_RESET_EN, 8, /* command and command bit len*/
  233. 0, 0, /* address and address bit len*/
  234. 0, /* dummy bit len */
  235. NULL, 0, /* tx data and tx bit len*/
  236. NULL, 0, /* rx data and rx bit len*/
  237. CS_PSRAM_SEL, /* cs bit mask*/
  238. false); /* whether is program/erase operation */
  239. psram_exec_cmd(spi_num, PSRAM_CMD_SPI,
  240. PSRAM_RESET, 8, /* command and command bit len*/
  241. 0, 0, /* address and address bit len*/
  242. 0, /* dummy bit len */
  243. NULL, 0, /* tx data and tx bit len*/
  244. NULL, 0, /* rx data and rx bit len*/
  245. CS_PSRAM_SEL, /* cs bit mask*/
  246. false); /* whether is program/erase operation */
  247. }
  248. esp_err_t psram_enable_wrap(uint32_t wrap_size)
  249. {
  250. static uint32_t current_wrap_size = 0;
  251. if (current_wrap_size == wrap_size) {
  252. return ESP_OK;
  253. }
  254. switch (wrap_size) {
  255. case 32:
  256. case 0:
  257. psram_set_wrap_burst_length(PSRAM_SPI_1, PSRAM_CMD_QPI);
  258. current_wrap_size = wrap_size;
  259. return ESP_OK;
  260. case 16:
  261. case 64:
  262. default:
  263. return ESP_FAIL;
  264. }
  265. }
  266. bool psram_support_wrap_size(uint32_t wrap_size)
  267. {
  268. switch (wrap_size) {
  269. case 0:
  270. case 32:
  271. return true;
  272. case 16:
  273. case 64:
  274. default:
  275. return false;
  276. }
  277. }
  278. //read psram id, should issue `psram_disable_qio_mode` before calling this
  279. static void psram_read_id(int spi_num, uint32_t* dev_id)
  280. {
  281. psram_exec_cmd(spi_num, PSRAM_CMD_SPI,
  282. PSRAM_DEVICE_ID, 8, /* command and command bit len*/
  283. 0, 24, /* address and address bit len*/
  284. 0, /* dummy bit len */
  285. NULL, 0, /* tx data and tx bit len*/
  286. (uint8_t*) dev_id, 24, /* rx data and rx bit len*/
  287. CS_PSRAM_SEL, /* cs bit mask*/
  288. false); /* whether is program/erase operation */
  289. }
  290. //enter QPI mode
  291. static void IRAM_ATTR psram_enable_qio_mode(int spi_num)
  292. {
  293. psram_exec_cmd(spi_num, PSRAM_CMD_SPI,
  294. PSRAM_ENTER_QMODE, 8, /* command and command bit len*/
  295. 0, 0, /* address and address bit len*/
  296. 0, /* dummy bit len */
  297. NULL, 0, /* tx data and tx bit len*/
  298. NULL, 0, /* rx data and rx bit len*/
  299. CS_PSRAM_SEL, /* cs bit mask*/
  300. false); /* whether is program/erase operation */
  301. }
  302. static void psram_set_spi1_cmd_cs_timing(psram_clk_mode_t clk_mode)
  303. {
  304. if (clk_mode == PSRAM_CLK_MODE_NORM) {
  305. // SPI1 Flash Operation port
  306. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(_SPI_FLASH_PORT), SPI_MEM_CS_HOLD_TIME_V, 1, SPI_MEM_CS_HOLD_TIME_S);
  307. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(_SPI_FLASH_PORT), SPI_MEM_CS_SETUP_TIME_V, 0, SPI_MEM_CS_SETUP_TIME_S);
  308. SET_PERI_REG_MASK(SPI_MEM_USER_REG(_SPI_FLASH_PORT), SPI_MEM_CS_HOLD_M | SPI_MEM_CS_SETUP_M);
  309. } else {
  310. SET_PERI_REG_MASK(SPI_MEM_USER_REG(_SPI_FLASH_PORT), SPI_MEM_CS_HOLD_M | SPI_MEM_CS_SETUP_M);
  311. }
  312. }
  313. static void psram_set_spi0_cache_cs_timing(psram_clk_mode_t clk_mode)
  314. {
  315. if (clk_mode == PSRAM_CLK_MODE_NORM) {
  316. // SPI0 SRAM Cache port
  317. SET_PERI_REG_BITS(SPI_MEM_SPI_SMEM_AC_REG(_SPI_CACHE_PORT), SPI_MEM_SPI_SMEM_CS_HOLD_TIME_V, 1, SPI_MEM_SPI_SMEM_CS_HOLD_TIME_S);
  318. SET_PERI_REG_BITS(SPI_MEM_SPI_SMEM_AC_REG(_SPI_CACHE_PORT), SPI_MEM_SPI_SMEM_CS_SETUP_TIME_V, 0, SPI_MEM_SPI_SMEM_CS_SETUP_TIME_S);
  319. SET_PERI_REG_MASK(SPI_MEM_SPI_SMEM_AC_REG(_SPI_CACHE_PORT), SPI_MEM_SPI_SMEM_CS_HOLD_M | SPI_MEM_SPI_SMEM_CS_SETUP_M);
  320. // SPI0 Flash Cache port
  321. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(_SPI_CACHE_PORT), SPI_MEM_CS_HOLD_TIME_V, 0, SPI_MEM_CS_HOLD_TIME_S);
  322. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(_SPI_CACHE_PORT), SPI_MEM_CS_SETUP_TIME_V, 0, SPI_MEM_CS_SETUP_TIME_S);
  323. SET_PERI_REG_MASK(SPI_MEM_USER_REG(_SPI_CACHE_PORT), SPI_MEM_CS_HOLD_M | SPI_MEM_CS_SETUP_M);
  324. } else {
  325. CLEAR_PERI_REG_MASK(SPI_MEM_USER_REG(_SPI_CACHE_PORT), SPI_CS_HOLD_M | SPI_CS_SETUP_M);
  326. }
  327. }
  328. //psram gpio init , different working frequency we have different solutions
  329. static void IRAM_ATTR psram_gpio_config(psram_cache_mode_t mode)
  330. {
  331. psram_io_t psram_io = PSRAM_IO_CONF_DEFAULT();
  332. const uint32_t spiconfig = esp_rom_efuse_get_flash_gpio_info();
  333. if (spiconfig == ESP_ROM_EFUSE_FLASH_DEFAULT_SPI) {
  334. /* FLASH pins(except wp / hd) are all configured via IO_MUX in rom. */
  335. } else {
  336. // FLASH pins are all configured via GPIO matrix in ROM.
  337. psram_io.flash_clk_io = EFUSE_SPICONFIG_RET_SPICLK(spiconfig);
  338. psram_io.flash_cs_io = EFUSE_SPICONFIG_RET_SPICS0(spiconfig);
  339. psram_io.psram_spiq_sd0_io = EFUSE_SPICONFIG_RET_SPIQ(spiconfig);
  340. psram_io.psram_spid_sd1_io = EFUSE_SPICONFIG_RET_SPID(spiconfig);
  341. psram_io.psram_spihd_sd2_io = EFUSE_SPICONFIG_RET_SPIHD(spiconfig);
  342. psram_io.psram_spiwp_sd3_io = esp_rom_efuse_get_flash_wp_gpio();
  343. }
  344. esp_rom_spiflash_select_qio_pins(psram_io.psram_spiwp_sd3_io, spiconfig);
  345. if (psram_io.psram_cs_io == SPI_CS1_GPIO_NUM) {
  346. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[psram_io.psram_cs_io], FUNC_SPICS1_SPICS1);
  347. } else {
  348. esp_rom_gpio_connect_out_signal(psram_io.psram_cs_io, SPICS1_OUT_IDX, 0, 0);
  349. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[psram_io.psram_cs_io], PIN_FUNC_GPIO);
  350. }
  351. }
  352. psram_size_t psram_get_size(void)
  353. {
  354. if ((PSRAM_SIZE_ID(s_psram_id) == PSRAM_EID_SIZE_64MBITS) || PSRAM_IS_64MBIT_TRIAL(s_psram_id)) {
  355. return PSRAM_SIZE_64MBITS;
  356. } else if (PSRAM_SIZE_ID(s_psram_id) == PSRAM_EID_SIZE_32MBITS) {
  357. return PSRAM_SIZE_32MBITS;
  358. } else if (PSRAM_SIZE_ID(s_psram_id) == PSRAM_EID_SIZE_16MBITS) {
  359. return PSRAM_SIZE_16MBITS;
  360. } else {
  361. return PSRAM_SIZE_MAX;
  362. }
  363. return PSRAM_SIZE_MAX;
  364. }
  365. //used in UT only
  366. bool psram_is_32mbit_ver0(void)
  367. {
  368. return PSRAM_IS_32MBIT_VER0(s_psram_id);
  369. }
  370. static void psram_set_clk_mode(int spi_num, psram_clk_mode_t clk_mode)
  371. {
  372. if (spi_num == _SPI_CACHE_PORT) {
  373. REG_SET_FIELD(SPI_MEM_SRAM_CMD_REG(0), SPI_MEM_SCLK_MODE, clk_mode);
  374. } else if (spi_num == _SPI_FLASH_PORT) {
  375. REG_SET_FIELD(SPI_MEM_CTRL1_REG(1), SPI_MEM_CLK_MODE, clk_mode);
  376. }
  377. }
  378. /*
  379. * Psram mode init will overwrite original flash speed mode, so that it is possible to change psram and flash speed after OTA.
  380. * Flash read mode(QIO/QOUT/DIO/DOUT) will not be changed in app bin. It is decided by bootloader, OTA can not change this mode.
  381. */
  382. esp_err_t IRAM_ATTR psram_enable(psram_cache_mode_t mode, psram_vaddr_mode_t vaddrmode) //psram init
  383. {
  384. assert(mode < PSRAM_CACHE_MAX && "we don't support any other mode for now.");
  385. // GPIO related settings
  386. psram_gpio_config(mode);
  387. /* SPI1: set spi1 clk mode, in order to send commands on SPI1 */
  388. /* SPI1: set cs timing(hold time) in order to send commands on SPI1 */
  389. psram_set_clk_mode(_SPI_FLASH_PORT, PSRAM_CLK_MODE_A1C);
  390. psram_set_spi1_cmd_cs_timing(PSRAM_CLK_MODE_A1C);
  391. int spi_num = PSRAM_SPI_1;
  392. psram_disable_qio_mode(spi_num);
  393. psram_read_id(spi_num, &s_psram_id);
  394. if (!PSRAM_IS_VALID(s_psram_id)) {
  395. /* 16Mbit psram ID read error workaround:
  396. * treat the first read id as a dummy one as the pre-condition,
  397. * Send Read ID command again
  398. */
  399. psram_read_id(spi_num, &s_psram_id);
  400. if (!PSRAM_IS_VALID(s_psram_id)) {
  401. ESP_EARLY_LOGE(TAG, "PSRAM ID read error: 0x%08x", s_psram_id);
  402. return ESP_FAIL;
  403. }
  404. }
  405. psram_clk_mode_t clk_mode = PSRAM_CLK_MODE_MAX;
  406. if (psram_is_32mbit_ver0()) {
  407. clk_mode = PSRAM_CLK_MODE_A1C;
  408. // SPI1: keep clock mode and cs timing for spi1
  409. } else {
  410. // For other psram, we don't need any extra clock cycles after cs get back to high level
  411. clk_mode = PSRAM_CLK_MODE_NORM;
  412. // SPI1: set clock mode and cs timing to normal mode
  413. psram_set_clk_mode(_SPI_FLASH_PORT, PSRAM_CLK_MODE_NORM);
  414. psram_set_spi1_cmd_cs_timing(PSRAM_CLK_MODE_NORM);
  415. }
  416. /* SPI1: send psram reset command */
  417. /* SPI1: send QPI enable command */
  418. psram_reset_mode(PSRAM_SPI_1);
  419. psram_enable_qio_mode(PSRAM_SPI_1);
  420. // after sending commands, set spi1 clock mode and cs timing to normal mode.
  421. // since all the operations are sent via SPI0 Cache
  422. /* SPI1: set clock mode to normal mode. */
  423. /* SPI1: set cs timing to normal */
  424. psram_set_clk_mode(_SPI_FLASH_PORT, PSRAM_CLK_MODE_NORM);
  425. psram_set_spi1_cmd_cs_timing(PSRAM_CLK_MODE_NORM);
  426. /* SPI0: set spi0 clock mode */
  427. /* SPI0: set spi0 flash/cache cs timing */
  428. psram_set_clk_mode(_SPI_CACHE_PORT, clk_mode);
  429. psram_set_spi0_cache_cs_timing(clk_mode);
  430. // SPI0: init SPI commands for Cache
  431. psram_cache_init(mode, vaddrmode);
  432. return ESP_OK;
  433. }
  434. static void IRAM_ATTR psram_clock_set(int spi_num, int8_t freqdiv)
  435. {
  436. uint32_t freqbits;
  437. if (1 >= freqdiv) {
  438. WRITE_PERI_REG(SPI_MEM_SRAM_CLK_REG(spi_num), SPI_MEM_SCLK_EQU_SYSCLK);
  439. } else {
  440. freqbits = (((freqdiv-1)<<SPI_MEM_SCLKCNT_N_S)) | (((freqdiv/2-1)<<SPI_MEM_SCLKCNT_H_S)) | ((freqdiv-1)<<SPI_MEM_SCLKCNT_L_S);
  441. WRITE_PERI_REG(SPI_MEM_SRAM_CLK_REG(spi_num), freqbits);
  442. }
  443. }
  444. //register initialization for sram cache params and r/w commands
  445. static void IRAM_ATTR psram_cache_init(psram_cache_mode_t psram_cache_mode, psram_vaddr_mode_t vaddrmode)
  446. {
  447. int extra_dummy = 0;
  448. switch (psram_cache_mode) {
  449. case PSRAM_CACHE_S80M:
  450. psram_clock_set(0, 1);
  451. extra_dummy = PSRAM_IO_MATRIX_DUMMY_80M;
  452. break;
  453. case PSRAM_CACHE_S40M:
  454. psram_clock_set(0, 2);
  455. extra_dummy = PSRAM_IO_MATRIX_DUMMY_40M;
  456. break;
  457. case PSRAM_CACHE_S26M:
  458. psram_clock_set(0, 3);
  459. extra_dummy = PSRAM_IO_MATRIX_DUMMY_20M;
  460. break;
  461. case PSRAM_CACHE_S20M:
  462. psram_clock_set(0, 4);
  463. extra_dummy = PSRAM_IO_MATRIX_DUMMY_20M;
  464. break;
  465. default:
  466. psram_clock_set(0, 2);
  467. break;
  468. }
  469. CLEAR_PERI_REG_MASK(SPI_MEM_CACHE_SCTRL_REG(0), SPI_MEM_USR_SRAM_DIO_M); //disable dio mode for cache command
  470. SET_PERI_REG_MASK(SPI_MEM_CACHE_SCTRL_REG(0), SPI_MEM_USR_SRAM_QIO_M); //enable qio mode for cache command
  471. SET_PERI_REG_MASK(SPI_MEM_CACHE_SCTRL_REG(0), SPI_MEM_CACHE_SRAM_USR_RCMD_M); //enable cache read command
  472. SET_PERI_REG_MASK(SPI_MEM_CACHE_SCTRL_REG(0), SPI_MEM_CACHE_SRAM_USR_WCMD_M); //enable cache write command
  473. SET_PERI_REG_BITS(SPI_MEM_CACHE_SCTRL_REG(0), SPI_MEM_SRAM_ADDR_BITLEN_V, 23, SPI_MEM_SRAM_ADDR_BITLEN_S); //write address for cache command.
  474. SET_PERI_REG_MASK(SPI_MEM_CACHE_SCTRL_REG(0), SPI_MEM_USR_RD_SRAM_DUMMY_M); //enable cache read dummy
  475. //config sram cache r/w command
  476. SET_PERI_REG_BITS(SPI_MEM_SRAM_DWR_CMD_REG(0), SPI_MEM_CACHE_SRAM_USR_WR_CMD_BITLEN, 7,
  477. SPI_MEM_CACHE_SRAM_USR_WR_CMD_BITLEN_S);
  478. SET_PERI_REG_BITS(SPI_MEM_SRAM_DWR_CMD_REG(0), SPI_MEM_CACHE_SRAM_USR_WR_CMD_VALUE, PSRAM_QUAD_WRITE,
  479. SPI_MEM_CACHE_SRAM_USR_WR_CMD_VALUE_S); //0x38
  480. SET_PERI_REG_BITS(SPI_MEM_SRAM_DRD_CMD_REG(0), SPI_MEM_CACHE_SRAM_USR_RD_CMD_BITLEN_V, 7,
  481. SPI_MEM_CACHE_SRAM_USR_RD_CMD_BITLEN_S);
  482. SET_PERI_REG_BITS(SPI_MEM_SRAM_DRD_CMD_REG(0), SPI_MEM_CACHE_SRAM_USR_RD_CMD_VALUE_V, PSRAM_FAST_READ_QUAD,
  483. SPI_MEM_CACHE_SRAM_USR_RD_CMD_VALUE_S); //0x0b
  484. SET_PERI_REG_BITS(SPI_MEM_CACHE_SCTRL_REG(0), SPI_MEM_SRAM_RDUMMY_CYCLELEN_V, PSRAM_FAST_READ_QUAD_DUMMY + extra_dummy,
  485. SPI_MEM_SRAM_RDUMMY_CYCLELEN_S); //dummy, psram cache : 40m--+1dummy,80m--+2dummy
  486. CLEAR_PERI_REG_MASK(SPI_MEM_MISC_REG(0), SPI_MEM_CS1_DIS_M); //ENABLE SPI0 CS1 TO PSRAM(CS0--FLASH; CS1--SRAM)
  487. }
  488. #endif // CONFIG_SPIRAM