esp_psram_impl_quad.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * SPDX-FileCopyrightText: 2013-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #include "string.h"
  8. #include "esp_attr.h"
  9. #include "esp_err.h"
  10. #include "esp_types.h"
  11. #include "esp_log.h"
  12. #include "../esp_psram_impl.h"
  13. #include "esp32s3/rom/spi_flash.h"
  14. #include "esp32s3/rom/opi_flash.h"
  15. #include "esp_rom_gpio.h"
  16. #include "esp_rom_efuse.h"
  17. #include "hal/gpio_hal.h"
  18. #include "esp_private/spi_flash_os.h"
  19. static const char* TAG = "quad_psram";
  20. //Commands for PSRAM chip
  21. #define PSRAM_READ 0x03
  22. #define PSRAM_FAST_READ 0x0B
  23. #define PSRAM_FAST_READ_QUAD 0xEB
  24. #define PSRAM_WRITE 0x02
  25. #define PSRAM_QUAD_WRITE 0x38
  26. #define PSRAM_ENTER_QMODE 0x35
  27. #define PSRAM_EXIT_QMODE 0xF5
  28. #define PSRAM_RESET_EN 0x66
  29. #define PSRAM_RESET 0x99
  30. #define PSRAM_SET_BURST_LEN 0xC0
  31. #define PSRAM_DEVICE_ID 0x9F
  32. #define PSRAM_FAST_READ_DUMMY 4
  33. #define PSRAM_FAST_READ_QUAD_DUMMY 6
  34. // ID
  35. #define PSRAM_ID_KGD_M 0xff
  36. #define PSRAM_ID_KGD_S 8
  37. #define PSRAM_ID_KGD 0x5d
  38. #define PSRAM_ID_EID_M 0xff
  39. #define PSRAM_ID_EID_S 16
  40. // Use the [7:5](bit7~bit5) of EID to distinguish the psram size:
  41. //
  42. // BIT7 | BIT6 | BIT5 | SIZE(MBIT)
  43. // -------------------------------------
  44. // 0 | 0 | 0 | 16
  45. // 0 | 0 | 1 | 32
  46. // 0 | 1 | 0 | 64
  47. #define PSRAM_EID_SIZE_M 0x07
  48. #define PSRAM_EID_SIZE_S 5
  49. #define PSRAM_KGD(id) (((id) >> PSRAM_ID_KGD_S) & PSRAM_ID_KGD_M)
  50. #define PSRAM_EID(id) (((id) >> PSRAM_ID_EID_S) & PSRAM_ID_EID_M)
  51. #define PSRAM_SIZE_ID(id) ((PSRAM_EID(id) >> PSRAM_EID_SIZE_S) & PSRAM_EID_SIZE_M)
  52. #define PSRAM_IS_VALID(id) (PSRAM_KGD(id) == PSRAM_ID_KGD)
  53. #define PSRAM_IS_64MBIT_TRIAL(id) (PSRAM_EID(id) == 0x26)
  54. // IO-pins for PSRAM.
  55. // WARNING: PSRAM shares all but the CS and CLK pins with the flash, so these defines
  56. // hardcode the flash pins as well, making this code incompatible with either a setup
  57. // that has the flash on non-standard pins or ESP32s with built-in flash.
  58. #define FLASH_CLK_IO SPI_CLK_GPIO_NUM
  59. #define FLASH_CS_IO SPI_CS0_GPIO_NUM
  60. // PSRAM clock and cs IO should be configured based on hardware design.
  61. #define PSRAM_CLK_IO CONFIG_DEFAULT_PSRAM_CLK_IO // Default value is 30
  62. #define PSRAM_CS_IO CONFIG_DEFAULT_PSRAM_CS_IO // Default value is 26
  63. #define PSRAM_SPIQ_SD0_IO SPI_Q_GPIO_NUM
  64. #define PSRAM_SPID_SD1_IO SPI_D_GPIO_NUM
  65. #define PSRAM_SPIWP_SD3_IO SPI_WP_GPIO_NUM
  66. #define PSRAM_SPIHD_SD2_IO SPI_HD_GPIO_NUM
  67. #define CS_PSRAM_SEL SPI_MEM_CS1_DIS_M
  68. #define CS_FLASH_SEL SPI_MEM_CS0_DIS_M
  69. #define SPI1_NUM 1
  70. #define SPI0_NUM 0
  71. typedef enum {
  72. PSRAM_CMD_QPI,
  73. PSRAM_CMD_SPI,
  74. } psram_cmd_mode_t;
  75. typedef esp_rom_spi_cmd_t psram_cmd_t;
  76. static uint32_t s_psram_id = 0;
  77. static uint32_t s_psram_size = 0; //this stands for physical psram size in bytes
  78. static void config_psram_spi_phases(void);
  79. extern void esp_rom_spi_set_op_mode(int spi_num, esp_rom_spiflash_read_mode_t mode);
  80. static uint8_t s_psram_cs_io = (uint8_t)-1;
  81. uint8_t esp_psram_impl_get_cs_io(void)
  82. {
  83. return s_psram_cs_io;
  84. }
  85. static void psram_set_op_mode(int spi_num, psram_cmd_mode_t mode)
  86. {
  87. if (mode == PSRAM_CMD_QPI) {
  88. esp_rom_spi_set_op_mode(spi_num, ESP_ROM_SPIFLASH_QIO_MODE);
  89. SET_PERI_REG_MASK(SPI_MEM_CTRL_REG(spi_num), SPI_MEM_FCMD_QUAD_M);
  90. } else if (mode == PSRAM_CMD_SPI) {
  91. esp_rom_spi_set_op_mode(spi_num, ESP_ROM_SPIFLASH_SLOWRD_MODE);
  92. }
  93. }
  94. static void _psram_exec_cmd(int spi_num,
  95. uint32_t cmd, int cmd_bit_len,
  96. uint32_t addr, int addr_bit_len,
  97. int dummy_bits,
  98. uint8_t* mosi_data, int mosi_bit_len,
  99. uint8_t* miso_data, int miso_bit_len)
  100. {
  101. esp_rom_spi_cmd_t conf;
  102. uint32_t _addr = addr;
  103. conf.addr = &_addr;
  104. conf.addrBitLen = addr_bit_len;
  105. conf.cmd = cmd;
  106. conf.cmdBitLen = cmd_bit_len;
  107. conf.dummyBitLen = dummy_bits; // There is a hardware approach on chip723
  108. conf.txData = (uint32_t*) mosi_data;
  109. conf.txDataBitLen = mosi_bit_len;
  110. conf.rxData = (uint32_t*) miso_data;
  111. conf.rxDataBitLen = miso_bit_len;
  112. esp_rom_spi_cmd_config(spi_num, &conf);
  113. }
  114. void psram_exec_cmd(int spi_num, psram_cmd_mode_t mode,
  115. uint32_t cmd, int cmd_bit_len,
  116. uint32_t addr, int addr_bit_len,
  117. int dummy_bits,
  118. uint8_t* mosi_data, int mosi_bit_len,
  119. uint8_t* miso_data, int miso_bit_len,
  120. uint32_t cs_mask,
  121. bool is_write_erase_operation)
  122. {
  123. uint32_t backup_usr = READ_PERI_REG(SPI_MEM_USER_REG(spi_num));
  124. uint32_t backup_usr1 = READ_PERI_REG(SPI_MEM_USER1_REG(spi_num));
  125. uint32_t backup_usr2 = READ_PERI_REG(SPI_MEM_USER2_REG(spi_num));
  126. uint32_t backup_ctrl = READ_PERI_REG(SPI_MEM_CTRL_REG(spi_num));
  127. psram_set_op_mode(spi_num, mode);
  128. _psram_exec_cmd(spi_num, cmd, cmd_bit_len, addr, addr_bit_len,
  129. dummy_bits, mosi_data, mosi_bit_len, miso_data, miso_bit_len);
  130. esp_rom_spi_cmd_start(spi_num, miso_data, miso_bit_len / 8, cs_mask, is_write_erase_operation);
  131. WRITE_PERI_REG(SPI_MEM_USER_REG(spi_num), backup_usr);
  132. WRITE_PERI_REG(SPI_MEM_USER1_REG(spi_num), backup_usr1);
  133. WRITE_PERI_REG(SPI_MEM_USER2_REG(spi_num), backup_usr2);
  134. WRITE_PERI_REG(SPI_MEM_CTRL_REG(spi_num), backup_ctrl);
  135. }
  136. //exit QPI mode(set back to SPI mode)
  137. static void psram_disable_qio_mode(int spi_num)
  138. {
  139. psram_exec_cmd(spi_num, PSRAM_CMD_QPI,
  140. PSRAM_EXIT_QMODE, 8, /* command and command bit len*/
  141. 0, 0, /* address and address bit len*/
  142. 0, /* dummy bit len */
  143. NULL, 0, /* tx data and tx bit len*/
  144. NULL, 0, /* rx data and rx bit len*/
  145. CS_PSRAM_SEL, /* cs bit mask*/
  146. false); /* whether is program/erase operation */
  147. }
  148. //TODO IDF-4307
  149. //switch psram burst length(32 bytes or 1024 bytes)
  150. //datasheet says it should be 1024 bytes by default
  151. static void psram_set_wrap_burst_length(int spi_num, psram_cmd_mode_t mode)
  152. {
  153. psram_exec_cmd(spi_num, mode,
  154. PSRAM_SET_BURST_LEN, 8, /* command and command bit len*/
  155. 0, 0, /* address and address bit len*/
  156. 0, /* dummy bit len */
  157. NULL, 0, /* tx data and tx bit len*/
  158. NULL, 0, /* rx data and rx bit len*/
  159. CS_PSRAM_SEL, /* cs bit mask*/
  160. false); /* whether is program/erase operation */
  161. }
  162. //send reset command to psram, in spi mode
  163. static void psram_reset_mode(int spi_num)
  164. {
  165. psram_exec_cmd(spi_num, PSRAM_CMD_SPI,
  166. PSRAM_RESET_EN, 8, /* command and command bit len*/
  167. 0, 0, /* address and address bit len*/
  168. 0, /* dummy bit len */
  169. NULL, 0, /* tx data and tx bit len*/
  170. NULL, 0, /* rx data and rx bit len*/
  171. CS_PSRAM_SEL, /* cs bit mask*/
  172. false); /* whether is program/erase operation */
  173. psram_exec_cmd(spi_num, PSRAM_CMD_SPI,
  174. PSRAM_RESET, 8, /* command and command bit len*/
  175. 0, 0, /* address and address bit len*/
  176. 0, /* dummy bit len */
  177. NULL, 0, /* tx data and tx bit len*/
  178. NULL, 0, /* rx data and rx bit len*/
  179. CS_PSRAM_SEL, /* cs bit mask*/
  180. false); /* whether is program/erase operation */
  181. }
  182. esp_err_t psram_enable_wrap(uint32_t wrap_size)
  183. {
  184. //TODO: IDF-4307
  185. static uint32_t current_wrap_size = 0;
  186. if (current_wrap_size == wrap_size) {
  187. return ESP_OK;
  188. }
  189. switch (wrap_size) {
  190. case 32:
  191. case 0:
  192. psram_set_wrap_burst_length(1, PSRAM_CMD_QPI);
  193. current_wrap_size = wrap_size;
  194. return ESP_OK;
  195. case 16:
  196. case 64:
  197. default:
  198. return ESP_FAIL;
  199. }
  200. }
  201. bool psram_support_wrap_size(uint32_t wrap_size)
  202. {
  203. switch (wrap_size) {
  204. case 0:
  205. case 32:
  206. return true;
  207. case 16:
  208. case 64:
  209. default:
  210. return false;
  211. }
  212. }
  213. //Read ID operation only supports SPI CMD and mode, should issue `psram_disable_qio_mode` before calling this
  214. static void psram_read_id(int spi_num, uint32_t* dev_id)
  215. {
  216. psram_exec_cmd(spi_num, PSRAM_CMD_SPI,
  217. PSRAM_DEVICE_ID, 8, /* command and command bit len*/
  218. 0, 24, /* address and address bit len*/
  219. 0, /* dummy bit len */
  220. NULL, 0, /* tx data and tx bit len*/
  221. (uint8_t*) dev_id, 24, /* rx data and rx bit len*/
  222. CS_PSRAM_SEL, /* cs bit mask*/
  223. false); /* whether is program/erase operation */
  224. }
  225. //enter QPI mode
  226. static void psram_enable_qio_mode(int spi_num)
  227. {
  228. psram_exec_cmd(spi_num, PSRAM_CMD_SPI,
  229. PSRAM_ENTER_QMODE, 8, /* command and command bit len*/
  230. 0, 0, /* address and address bit len*/
  231. 0, /* dummy bit len */
  232. NULL, 0, /* tx data and tx bit len*/
  233. NULL, 0, /* rx data and rx bit len*/
  234. CS_PSRAM_SEL, /* cs bit mask*/
  235. false); /* whether is program/erase operation */
  236. }
  237. static void psram_set_cs_timing(void)
  238. {
  239. //SPI0/1 share the cs_hold / cs_setup, cd_hold_time / cd_setup_time registers for PSRAM, so we only need to set SPI0 related registers here
  240. SET_PERI_REG_BITS(SPI_MEM_SPI_SMEM_AC_REG(0), SPI_MEM_SPI_SMEM_CS_HOLD_TIME_V, 0, SPI_MEM_SPI_SMEM_CS_HOLD_TIME_S);
  241. SET_PERI_REG_BITS(SPI_MEM_SPI_SMEM_AC_REG(0), SPI_MEM_SPI_SMEM_CS_SETUP_TIME_V, 0, SPI_MEM_SPI_SMEM_CS_SETUP_TIME_S);
  242. SET_PERI_REG_MASK(SPI_MEM_SPI_SMEM_AC_REG(0), SPI_MEM_SPI_SMEM_CS_HOLD_M | SPI_MEM_SPI_SMEM_CS_SETUP_M);
  243. }
  244. static void psram_gpio_config(void)
  245. {
  246. //CS1
  247. uint8_t cs1_io = PSRAM_CS_IO;
  248. if (cs1_io == SPI_CS1_GPIO_NUM) {
  249. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[cs1_io], FUNC_SPICS1_SPICS1);
  250. } else {
  251. esp_rom_gpio_connect_out_signal(cs1_io, SPICS1_OUT_IDX, 0, 0);
  252. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[cs1_io], PIN_FUNC_GPIO);
  253. }
  254. s_psram_cs_io = cs1_io;
  255. //WP HD
  256. uint8_t wp_io = PSRAM_SPIWP_SD3_IO;
  257. const uint32_t spiconfig = esp_rom_efuse_get_flash_gpio_info();
  258. if (spiconfig == ESP_ROM_EFUSE_FLASH_DEFAULT_SPI) {
  259. // MSPI pins (except wp / hd) are all configured via IO_MUX in 1st bootloader.
  260. } else {
  261. // MSPI pins (except wp / hd) are all configured via GPIO matrix in 1st bootloader.
  262. wp_io = esp_rom_efuse_get_flash_wp_gpio();
  263. }
  264. //This ROM function will init both WP and HD pins.
  265. esp_rom_spiflash_select_qio_pins(wp_io, spiconfig);
  266. }
  267. esp_err_t esp_psram_impl_enable(psram_vaddr_mode_t vaddrmode) //psram init
  268. {
  269. psram_gpio_config();
  270. psram_set_cs_timing();
  271. //enter MSPI slow mode to init PSRAM device registers
  272. spi_timing_enter_mspi_low_speed_mode(true);
  273. //We use SPI1 to init PSRAM
  274. psram_disable_qio_mode(SPI1_NUM);
  275. psram_read_id(SPI1_NUM, &s_psram_id);
  276. if (!PSRAM_IS_VALID(s_psram_id)) {
  277. /* 16Mbit psram ID read error workaround:
  278. * treat the first read id as a dummy one as the pre-condition,
  279. * Send Read ID command again
  280. */
  281. psram_read_id(SPI1_NUM, &s_psram_id);
  282. if (!PSRAM_IS_VALID(s_psram_id)) {
  283. ESP_EARLY_LOGE(TAG, "PSRAM ID read error: 0x%08x", s_psram_id);
  284. return ESP_FAIL;
  285. }
  286. }
  287. if (PSRAM_IS_64MBIT_TRIAL(s_psram_id)) {
  288. s_psram_size = PSRAM_SIZE_8MB;
  289. } else {
  290. uint8_t density = PSRAM_SIZE_ID(s_psram_id);
  291. s_psram_size = density == 0x0 ? PSRAM_SIZE_2MB :
  292. density == 0x1 ? PSRAM_SIZE_4MB :
  293. density == 0x2 ? PSRAM_SIZE_8MB : 0;
  294. }
  295. //SPI1: send psram reset command
  296. psram_reset_mode(SPI1_NUM);
  297. //SPI1: send QPI enable command
  298. psram_enable_qio_mode(SPI1_NUM);
  299. //Do PSRAM timing tuning, we use SPI1 to do the tuning, and set the SPI0 PSRAM timing related registers accordingly
  300. spi_timing_psram_tuning();
  301. //Configure SPI0 PSRAM related SPI Phases
  302. config_psram_spi_phases();
  303. //Back to the high speed mode. Flash/PSRAM clocks are set to the clock that user selected. SPI0/1 registers are all set correctly
  304. spi_timing_enter_mspi_high_speed_mode(true);
  305. return ESP_OK;
  306. }
  307. //Configure PSRAM SPI0 phase related registers here according to the PSRAM chip requirement
  308. static void config_psram_spi_phases(void)
  309. {
  310. //Config CMD phase
  311. CLEAR_PERI_REG_MASK(SPI_MEM_CACHE_SCTRL_REG(0), SPI_MEM_USR_SRAM_DIO_M); //disable dio mode for cache command
  312. SET_PERI_REG_MASK(SPI_MEM_CACHE_SCTRL_REG(0), SPI_MEM_USR_SRAM_QIO_M); //enable qio mode for cache command
  313. SET_PERI_REG_MASK(SPI_MEM_CACHE_SCTRL_REG(0), SPI_MEM_CACHE_SRAM_USR_RCMD_M); //enable cache read command
  314. SET_PERI_REG_MASK(SPI_MEM_CACHE_SCTRL_REG(0), SPI_MEM_CACHE_SRAM_USR_WCMD_M); //enable cache write command
  315. SET_PERI_REG_BITS(SPI_MEM_SRAM_DWR_CMD_REG(0), SPI_MEM_CACHE_SRAM_USR_WR_CMD_BITLEN, 7, SPI_MEM_CACHE_SRAM_USR_WR_CMD_BITLEN_S);
  316. SET_PERI_REG_BITS(SPI_MEM_SRAM_DWR_CMD_REG(0), SPI_MEM_CACHE_SRAM_USR_WR_CMD_VALUE, PSRAM_QUAD_WRITE, SPI_MEM_CACHE_SRAM_USR_WR_CMD_VALUE_S); //0x38
  317. SET_PERI_REG_BITS(SPI_MEM_SRAM_DRD_CMD_REG(0), SPI_MEM_CACHE_SRAM_USR_RD_CMD_BITLEN_V, 7, SPI_MEM_CACHE_SRAM_USR_RD_CMD_BITLEN_S);
  318. SET_PERI_REG_BITS(SPI_MEM_SRAM_DRD_CMD_REG(0), SPI_MEM_CACHE_SRAM_USR_RD_CMD_VALUE_V, PSRAM_FAST_READ_QUAD, SPI_MEM_CACHE_SRAM_USR_RD_CMD_VALUE_S); //0xEB
  319. //Config ADDR phase
  320. SET_PERI_REG_BITS(SPI_MEM_CACHE_SCTRL_REG(0), SPI_MEM_SRAM_ADDR_BITLEN_V, 23, SPI_MEM_SRAM_ADDR_BITLEN_S);
  321. //Dummy
  322. /**
  323. * We set the PSRAM chip required dummy here. If timing tuning is needed,
  324. * the dummy length will be updated in `spi_timing_enter_mspi_high_speed_mode()`
  325. */
  326. SET_PERI_REG_MASK(SPI_MEM_CACHE_SCTRL_REG(0), SPI_MEM_USR_RD_SRAM_DUMMY_M); //enable cache read dummy
  327. SET_PERI_REG_BITS(SPI_MEM_CACHE_SCTRL_REG(0), SPI_MEM_SRAM_RDUMMY_CYCLELEN_V, (PSRAM_FAST_READ_QUAD_DUMMY - 1), SPI_MEM_SRAM_RDUMMY_CYCLELEN_S); //dummy
  328. CLEAR_PERI_REG_MASK(SPI_MEM_MISC_REG(0), SPI_MEM_CS1_DIS_M); //ENABLE SPI0 CS1 TO PSRAM(CS0--FLASH; CS1--SRAM)
  329. }
  330. /*---------------------------------------------------------------------------------
  331. * Following APIs are not required to be IRAM-Safe
  332. *
  333. * Consider moving these to another file if this kind of APIs grows dramatically
  334. *-------------------------------------------------------------------------------*/
  335. esp_err_t esp_psram_impl_get_physical_size(uint32_t *out_size_bytes)
  336. {
  337. if (!out_size_bytes) {
  338. return ESP_ERR_INVALID_ARG;
  339. }
  340. *out_size_bytes = s_psram_size;
  341. return (s_psram_size ? ESP_OK : ESP_ERR_INVALID_STATE);
  342. }
  343. /**
  344. * This function is to get the available physical psram size in bytes.
  345. *
  346. * When ECC is enabled, the available size will be reduced.
  347. * On S3 Quad PSRAM, ECC is not enabled for now.
  348. */
  349. esp_err_t esp_psram_impl_get_available_size(uint32_t *out_size_bytes)
  350. {
  351. if (!out_size_bytes) {
  352. return ESP_ERR_INVALID_ARG;
  353. }
  354. *out_size_bytes = s_psram_size;
  355. return (s_psram_size ? ESP_OK : ESP_ERR_INVALID_STATE);
  356. }