esp_psram_impl_quad.c 16 KB

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