hpm_dram_drv.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2021 hpmicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef _HPM_DRAM_DRV_H
  8. #define _HPM_DRAM_DRV_H
  9. #include "hpm_dram_regs.h"
  10. /**
  11. * @brief DRAM driver APIs
  12. * @defgroup dram_interface DRAM driver APIs
  13. * @ingroup io_interfaces
  14. * @{
  15. *
  16. */
  17. #define DRAM_SDRAM_MAX_BURST_LENGTH_IN_BYTE (8UL)
  18. /* @brief dram sdram column address bit width */
  19. #define DRAM_SDRAM_COLUMN_ADDR_12_BITS (0U)
  20. #define DRAM_SDRAM_COLUMN_ADDR_11_BITS (1U)
  21. #define DRAM_SDRAM_COLUMN_ADDR_10_BITS (2U)
  22. #define DRAM_SDRAM_COLUMN_ADDR_9_BITS (3U)
  23. #define DRAM_SDRAM_COLUMN_ADDR_8_BITS (4U)
  24. /* @brief cas latency */
  25. #define DRAM_SDRAM_CAS_LATENCY_1 (1U)
  26. #define DRAM_SDRAM_CAS_LATENCY_2 (2U)
  27. #define DRAM_SDRAM_CAS_LATENCY_3 (3U)
  28. /* @brief iomux options */
  29. #define DRAM_IO_MUX_NOT_USED (0U)
  30. #define DRAM_IO_MUX_CSX0 (1U)
  31. #define DRAM_IO_MUX_CSX1 (2U)
  32. #define DRAM_IO_MUX_CSX2 (3U)
  33. #define DRAM_IO_MUX_CSX3 (4U)
  34. #define DRAM_IO_MUX_RDY (5U)
  35. /* @brief sdram bank number */
  36. #define DRAM_SDRAM_BANK_NUM_4 (0U)
  37. #define DRAM_SDRAM_BANK_NUM_2 (1U)
  38. /* @brief chip select */
  39. #define DRAM_SDRAM_CS0 (0U)
  40. #define DRAM_SDRAM_CS1 (1U)
  41. /* @brief sdram port size */
  42. #define DRAM_SDRAM_PORT_SIZE_8_BITS (0U)
  43. #define DRAM_SDRAM_PORT_SIZE_16_BITS (1U)
  44. #define DRAM_SDRAM_PORT_SIZE_32_BITS (2U)
  45. #define DRAM_AXI_Q_COUNT (2U)
  46. #define DRAM_AXI_Q_A (0U)
  47. #define DRAM_AXI_Q_B (1U)
  48. /* @brief DQS option */
  49. #define DRAM_DQS_INTERNAL (0U)
  50. #define DRAM_DQS_FROM_PAD (1U)
  51. #define DRAM_BR_COUNT (2U)
  52. #define DRAM_CMD_KEY DRAM_IPCMD_KEY_SET(0xA55A)
  53. #define DRAM_CMD_WRITE_FLAG (1UL << 31)
  54. #define DRAM_CMD_SDRAM_READ (0x8U)
  55. #define DRAM_CMD_SDRAM_WRITE (DRAM_CMD_WRITE_FLAG | 0x9U)
  56. #define DRAM_CMD_SDRAM_MODE_SET (DRAM_CMD_WRITE_FLAG | 0xAU)
  57. #define DRAM_CMD_SDRAM_ACTIVE (0xBU)
  58. #define DRAM_CMD_SDRAM_AUTO_REFRESH (0xCU)
  59. #define DRAM_CMD_SDRAM_SELF_REFRESH (0xDU)
  60. #define DRAM_CMD_SDRAM_PRECHARGE (0xEU)
  61. #define DRAM_CMD_SDRAM_PRECHARGE_ALL (0xFU)
  62. /**
  63. * @brief Structure for specifying the configuration of AXI queue weight
  64. */
  65. typedef struct {
  66. bool enable; /**< Enable AXI weight setting flag */
  67. uint8_t qos;
  68. uint8_t age;
  69. uint8_t slave_hit_wo_rw;
  70. uint8_t slave_hit; /**< only available for queue A */
  71. uint8_t page_hit; /**< only available for queue B */
  72. uint8_t bank_rotation; /**< only available for queue B */
  73. } dram_axi_q_weight_t;
  74. /**
  75. * @brief Structure for specifying the configuration of SDRAM
  76. */
  77. typedef struct {
  78. uint32_t base_address; /**< external SDRAM base address */
  79. uint32_t size_in_byte; /**< external SDRAM size in byte */
  80. uint32_t refresh_count; /**< referesh count */
  81. uint8_t col_addr_bits; /**< column address bit count */
  82. uint8_t cas_latency; /**< CAS latency */
  83. uint8_t cs; /**< chip select */
  84. uint8_t cs_mux_pin; /**< chip select mux */
  85. uint8_t bank_num; /**< bank number */
  86. uint8_t prescaler; /**< presecaler */
  87. uint8_t port_size; /**< SDRAM port size */
  88. uint8_t burst_len_in_byte; /**< 1/2/4/8 bytes */
  89. uint8_t cke_off_in_ns; /**< Tcks */
  90. uint8_t act_to_precharge_in_ns; /**< Tras */
  91. uint8_t precharge_to_act_in_ns; /**< Trp */
  92. uint8_t act_to_rw_in_ns; /**< Trcd */
  93. uint8_t act_to_act_in_ns; /**< Trrd */
  94. uint8_t refresh_to_refresh_in_ns; /**< Trc */
  95. uint8_t write_recover_in_ns; /**< Tdpl */
  96. uint8_t self_refresh_recover_in_ns; /**< Txsr */
  97. uint8_t refresh_recover_in_ns; /**< Txsr */
  98. uint8_t refresh_in_ms; /**< Tref */
  99. uint8_t idle_timeout_in_ns;
  100. uint8_t data_width_in_byte;
  101. uint8_t auto_refresh_count_in_one_burst;
  102. uint8_t delay_cell_value; /**< Delay cell value */
  103. } dram_sdram_config_t;
  104. /**
  105. * @brief Structure for specifying the configuration of DRAM
  106. */
  107. typedef struct {
  108. uint8_t dqs; /**< DQS setting */
  109. uint8_t cmd_timeout; /**< command timeout */
  110. uint8_t bus_timeout; /**< bus timeout */
  111. dram_axi_q_weight_t axi_q_weight[DRAM_AXI_Q_COUNT];
  112. } dram_config_t;
  113. /**
  114. * @brief Structure for DRAM command
  115. */
  116. typedef struct {
  117. uint32_t opcode;
  118. uint32_t data;
  119. } dram_cmd_t;
  120. /*
  121. * @brief DRAM specific status
  122. */
  123. enum {
  124. status_dram_cmd_err = MAKE_STATUS(status_group_dram, 1),
  125. };
  126. #ifdef __cplusplus
  127. extern "C" {
  128. #endif
  129. /**
  130. * @brief dram enable
  131. *
  132. * Enable DRAM
  133. *
  134. * @param[in] ptr DRAM base address
  135. */
  136. static inline void dram_enable(DRAM_Type *ptr)
  137. {
  138. ptr->CTRL &= ~DRAM_CTRL_DIS_MASK;
  139. }
  140. /**
  141. * @brief dram disable
  142. *
  143. * Disable DRAM
  144. *
  145. * @param[in] ptr DRAM base address
  146. */
  147. static inline void dram_disable(DRAM_Type *ptr)
  148. {
  149. while((ptr->STAT0 & (uint32_t) DRAM_STAT0_IDLE_MASK) == 0) {}
  150. ptr->CTRL |= DRAM_CTRL_DIS_MASK;
  151. }
  152. /**
  153. * @brief dram software reset
  154. *
  155. * Perform software reset
  156. *
  157. * @param[in] ptr DRAM base address
  158. */
  159. static inline void dram_sw_reset(DRAM_Type *ptr)
  160. {
  161. ptr->CTRL = DRAM_CTRL_RST_MASK;
  162. while((ptr->CTRL & (uint32_t) DRAM_CTRL_RST_MASK) != 0) {}
  163. }
  164. /**
  165. * @brief dram get default config
  166. *
  167. * Get DRAM default parameters
  168. *
  169. * @param[in] ptr DRAM base address
  170. * @param[out] config dram_config_t address
  171. */
  172. void dram_default_config(DRAM_Type *ptr, dram_config_t *config);
  173. /**
  174. * @brief dram init controller
  175. *
  176. * Initialize DRAM with give dram_config_t
  177. *
  178. * @param[in] ptr DRAM base address
  179. * @param[in] config dram_config_t to initialize dram
  180. */
  181. void dram_init(DRAM_Type *ptr, dram_config_t *config);
  182. /**
  183. * @brief dram get typical sdram config
  184. *
  185. * Fill out the structure of dram_sdram_config_t with typical SDRAM parameters which should work
  186. * with most SDRAMs.
  187. *
  188. * @param[in] ptr DRAM base address
  189. * @param[out] config dram_sdram_config_t sdram configuration struction to config dram
  190. */
  191. void dram_get_typical_sdram_config(DRAM_Type *ptr, dram_sdram_config_t *config);
  192. /**
  193. * @brief dram config sdram
  194. *
  195. * Configure DRAM controlling external SDRAM using parameters specified in dram_sdram_config
  196. *
  197. * @param[in] ptr DRAM base address
  198. * @param[in] clk_in_hz dram source clock frequency in Hz
  199. * @param[in] config dram_sdram_config_t sdram configuration struction to config dram
  200. */
  201. hpm_stat_t dram_config_sdram(DRAM_Type *ptr, uint32_t clk_in_hz, dram_sdram_config_t *config);
  202. #ifdef __cplusplus
  203. }
  204. #endif
  205. /**
  206. * @}
  207. */
  208. #endif /* _HPM_DRAM_DRV_H */