hpm_femc_drv.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2021-2022 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef _HPM_FEMC_DRV_H
  8. #define _HPM_FEMC_DRV_H
  9. #include "hpm_femc_regs.h"
  10. /**
  11. * @brief femc driver APIs
  12. * @defgroup dram_interface femc driver APIs
  13. * @ingroup io_interfaces
  14. * @{
  15. *
  16. */
  17. #define FEMC_SDRAM_MAX_BURST_LENGTH_IN_BYTE (8UL)
  18. /* @brief femc sdram column address bit width */
  19. #define FEMC_SDRAM_COLUMN_ADDR_12_BITS (0U)
  20. #define FEMC_SDRAM_COLUMN_ADDR_11_BITS (1U)
  21. #define FEMC_SDRAM_COLUMN_ADDR_10_BITS (2U)
  22. #define FEMC_SDRAM_COLUMN_ADDR_9_BITS (3U)
  23. #define FEMC_SDRAM_COLUMN_ADDR_8_BITS (4U)
  24. /* @brief cas latency */
  25. #define FEMC_SDRAM_CAS_LATENCY_1 (1U)
  26. #define FEMC_SDRAM_CAS_LATENCY_2 (2U)
  27. #define FEMC_SDRAM_CAS_LATENCY_3 (3U)
  28. /* @brief iomux options */
  29. #define FEMC_IO_MUX_NOT_USED (0U)
  30. #define FEMC_IO_MUX_CSX0 (1U)
  31. #define FEMC_IO_MUX_CSX1 (2U)
  32. #define FEMC_IO_MUX_CSX2 (3U)
  33. #define FEMC_IO_MUX_CSX3 (4U)
  34. #define FEMC_IO_MUX_RDY (5U)
  35. /* @brief sdram bank number */
  36. #define FEMC_SDRAM_BANK_NUM_4 (0U)
  37. #define FEMC_SDRAM_BANK_NUM_2 (1U)
  38. /* @brief chip select */
  39. #define FEMC_SDRAM_CS0 (0U)
  40. #define FEMC_SDRAM_CS1 (1U)
  41. /* @brief sdram port size */
  42. #define FEMC_SDRAM_PORT_SIZE_8_BITS (0U)
  43. #define FEMC_SDRAM_PORT_SIZE_16_BITS (1U)
  44. #define FEMC_SDRAM_PORT_SIZE_32_BITS (2U)
  45. #define FEMC_AXI_Q_COUNT (2U)
  46. #define FEMC_AXI_Q_A (0U)
  47. #define FEMC_AXI_Q_B (1U)
  48. /* @brief DQS option */
  49. #define FEMC_DQS_INTERNAL (0U)
  50. #define FEMC_DQS_FROM_PAD (1U)
  51. #define FEMC_BR_COUNT (2U)
  52. #define FEMC_CMD_KEY FEMC_IPCMD_KEY_SET(0xA55A)
  53. #define FEMC_CMD_WRITE_FLAG (1UL << 31)
  54. #define FEMC_CMD_SDRAM_READ (0x8U)
  55. #define FEMC_CMD_SDRAM_WRITE (FEMC_CMD_WRITE_FLAG | 0x9U)
  56. #define FEMC_CMD_SDRAM_MODE_SET (FEMC_CMD_WRITE_FLAG | 0xAU)
  57. #define FEMC_CMD_SDRAM_ACTIVE (0xBU)
  58. #define FEMC_CMD_SDRAM_AUTO_REFRESH (0xCU)
  59. #define FEMC_CMD_SDRAM_SELF_REFRESH (0xDU)
  60. #define FEMC_CMD_SDRAM_PRECHARGE (0xEU)
  61. #define FEMC_CMD_SDRAM_PRECHARGE_ALL (0xFU)
  62. /* @brief sram address mode */
  63. #define FEMC_SRAM_AD_MUX_MODE (0U)
  64. #define FEMC_SRAM_AD_NONMUX_MODE (3U)
  65. /* @brief sram adv hold state */
  66. #define FEMC_SRAM_ADV_HOLD_HIGH (0U)
  67. #define FEMC_SRAM_ADV_HOLD_LOW (1U)
  68. /* @brief sram adv polarity */
  69. #define FEMC_SRAM_ADV_ACTIVE_LOW (0U)
  70. #define FEMC_SRAM_ADV_ACTIVE_HIGH (1U)
  71. /* @brief sram port size */
  72. #define FEMC_SRAM_PORT_SIZE_8_BITS (0U)
  73. #define FEMC_SRAM_PORT_SIZE_16_BITS (1U)
  74. /* @brief IO_CSX selection */
  75. #define FEMC_IO_CSX_SDRAM_CS1 (1U)
  76. #define FEMC_IO_CSX_SRAM_CE (6U)
  77. /**
  78. * @brief Structure for specifying the configuration of AXI queue weight
  79. */
  80. typedef struct {
  81. bool enable; /**< Enable AXI weight setting flag */
  82. uint8_t qos;
  83. uint8_t age;
  84. uint8_t slave_hit_wo_rw;
  85. uint8_t slave_hit; /**< only available for queue A */
  86. uint8_t page_hit; /**< only available for queue B */
  87. uint8_t bank_rotation; /**< only available for queue B */
  88. } femc_axi_q_weight_t;
  89. /**
  90. * @brief Structure for specifying the configuration of SDRAM
  91. */
  92. typedef struct {
  93. uint32_t base_address; /**< external SDRAM base address */
  94. uint32_t size_in_byte; /**< external SDRAM size in byte */
  95. uint32_t refresh_count; /**< referesh count */
  96. uint8_t col_addr_bits; /**< column address bit count */
  97. uint8_t cas_latency; /**< CAS latency */
  98. uint8_t cs; /**< chip select */
  99. uint8_t cs_mux_pin; /**< chip select mux */
  100. uint8_t bank_num; /**< bank number */
  101. uint8_t prescaler; /**< presecaler */
  102. uint8_t port_size; /**< SDRAM port size */
  103. uint8_t burst_len_in_byte; /**< 1/2/4/8 bytes */
  104. uint8_t cke_off_in_ns; /**< Tcks */
  105. uint8_t act_to_precharge_in_ns; /**< Tras */
  106. uint8_t precharge_to_act_in_ns; /**< Trp */
  107. uint8_t act_to_rw_in_ns; /**< Trcd */
  108. uint8_t act_to_act_in_ns; /**< Trrd */
  109. uint8_t refresh_to_refresh_in_ns; /**< Trc */
  110. uint8_t write_recover_in_ns; /**< Tdpl */
  111. uint8_t self_refresh_recover_in_ns; /**< Txsr */
  112. uint8_t refresh_recover_in_ns; /**< Txsr */
  113. uint8_t refresh_in_ms; /**< Tref */
  114. uint8_t idle_timeout_in_ns;
  115. uint8_t data_width_in_byte;
  116. uint8_t auto_refresh_count_in_one_burst;
  117. uint8_t delay_cell_value; /**< Delay cell value */
  118. } femc_sdram_config_t;
  119. /**
  120. * @brief Structure for specifying the configuration of SRAM
  121. */
  122. typedef struct {
  123. uint32_t base_address; /**< external SRAM base address */
  124. uint32_t size_in_byte; /**< external SRAM size in byte */
  125. uint8_t address_mode; /**< address mode */
  126. uint8_t port_size; /**< port size */
  127. uint8_t adv_hold_state; /**< adv hold level */
  128. uint8_t adv_polarity; /**< adv polarity */
  129. uint8_t oeh_in_ns; /**< OE high time */
  130. uint8_t oel_in_ns; /**< OE low time */
  131. uint8_t weh_in_ns; /**< WE high time */
  132. uint8_t wel_in_ns; /**< WE low time */
  133. uint8_t ah_in_ns; /**< address hold time */
  134. uint8_t as_in_ns; /**< address setup time */
  135. uint8_t ceh_in_ns; /**< chip enable hold time */
  136. uint8_t ces_in_ns; /**< chip enable setup time */
  137. } femc_sram_config_t;
  138. /**
  139. * @brief Structure for specifying the configuration of FEMC
  140. */
  141. typedef struct {
  142. uint8_t dqs; /**< DQS setting */
  143. uint8_t cmd_timeout; /**< command timeout */
  144. uint8_t bus_timeout; /**< bus timeout */
  145. femc_axi_q_weight_t axi_q_weight[FEMC_AXI_Q_COUNT];
  146. } femc_config_t;
  147. /**
  148. * @brief Structure for FEMC command
  149. */
  150. typedef struct {
  151. uint32_t opcode;
  152. uint32_t data;
  153. } femc_cmd_t;
  154. /*
  155. * @brief FEMC specific status
  156. */
  157. enum {
  158. status_femc_cmd_err = MAKE_STATUS(status_group_femc, 1),
  159. };
  160. #ifdef __cplusplus
  161. extern "C" {
  162. #endif
  163. /**
  164. * @brief femc enable
  165. *
  166. * Enable FEMC
  167. *
  168. * @param[in] ptr FEMC base address
  169. */
  170. static inline void femc_enable(FEMC_Type *ptr)
  171. {
  172. ptr->CTRL &= ~FEMC_CTRL_DIS_MASK;
  173. }
  174. /**
  175. * @brief femc disable
  176. *
  177. * Disable FEMC
  178. *
  179. * @param[in] ptr FEMC base address
  180. */
  181. static inline void femc_disable(FEMC_Type *ptr)
  182. {
  183. while ((ptr->STAT0 & (uint32_t) FEMC_STAT0_IDLE_MASK) == 0) {
  184. }
  185. ptr->CTRL |= FEMC_CTRL_DIS_MASK;
  186. }
  187. /**
  188. * @brief femc software reset
  189. *
  190. * Perform software reset
  191. *
  192. * @param[in] ptr FEMC base address
  193. */
  194. static inline void femc_sw_reset(FEMC_Type *ptr)
  195. {
  196. ptr->CTRL = FEMC_CTRL_RST_MASK;
  197. while ((ptr->CTRL & (uint32_t) FEMC_CTRL_RST_MASK) != 0) {
  198. }
  199. }
  200. /**
  201. * @brief femc get default config
  202. *
  203. * Get FEMC default parameters
  204. *
  205. * @param[in] ptr FEMC base address
  206. * @param[out] config femc_config_t address
  207. */
  208. void femc_default_config(FEMC_Type *ptr, femc_config_t *config);
  209. /**
  210. * @brief femc init controller
  211. *
  212. * Initialize FEMC with give femc_config_t
  213. *
  214. * @param[in] ptr FEMC base address
  215. * @param[in] config femc_config_t to initialize femc
  216. */
  217. void femc_init(FEMC_Type *ptr, femc_config_t *config);
  218. /**
  219. * @brief femc get typical sdram config
  220. *
  221. * Fill out the structure of femc_sdram_config_t with typical SDRAM parameters which should work
  222. * with most SDRAMs.
  223. *
  224. * @param[in] ptr FEMC base address
  225. * @param[out] config femc_sdram_config_t sdram configuration struction to config femc
  226. */
  227. void femc_get_typical_sdram_config(FEMC_Type *ptr, femc_sdram_config_t *config);
  228. /**
  229. * @brief femc config sdram
  230. *
  231. * Configure FEMC controlling external SDRAM using parameters specified in femc_sdram_config_t
  232. *
  233. * @param[in] ptr FEMC base address
  234. * @param[in] clk_in_hz femc source clock frequency in Hz
  235. * @param[in] config femc_sdram_config_t sdram configuration struction to config femc
  236. */
  237. hpm_stat_t femc_config_sdram(FEMC_Type *ptr, uint32_t clk_in_hz, femc_sdram_config_t *config);
  238. /**
  239. * @brief femc get typical sram config
  240. *
  241. * Fill out the structure of femc_sram_config_t with typical SRAM parameters which should work
  242. * with most SRAMs.
  243. *
  244. * @param[in] ptr FEMC base address
  245. * @param[out] config femc_sdram_config_t sdram configuration struction to config femc
  246. */
  247. void femc_get_typical_sram_config(FEMC_Type *ptr, femc_sram_config_t *config);
  248. /**
  249. * @brief femc config sram
  250. *
  251. * Configure FEMC controlling external SRAM using parameters specified in femc_sram_config_t
  252. *
  253. * @param[in] ptr FEMC base address
  254. * @param[in] clk_in_hz femc source clock frequency in Hz
  255. * @param[in] config femc_sram_config_t sram configuration struction to config femc
  256. */
  257. hpm_stat_t femc_config_sram(FEMC_Type *ptr, uint32_t clk_in_hz, femc_sram_config_t *config);
  258. #ifdef __cplusplus
  259. }
  260. #endif
  261. /**
  262. * @}
  263. */
  264. #endif /* _HPM_FEMC_DRV_H */