spiram_psram.c 22 KB

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