spiram_psram.c 21 KB

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