drv_clock.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * File : drv_clock.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-02-08 RT-Thread the first version
  23. */
  24. #ifndef __DRV_CLOCK_H__
  25. #define __DRV_CLOCK_H__
  26. /* PLL state */
  27. #define PLL_ENBALE (0x1)
  28. #define PLL_STABLE (0x2)
  29. /* Clock source selection */
  30. #define CLK_LOSC_SRC (0x00)
  31. #define CLK_OSC24M_SRC (0x01)
  32. #define CLK_PLL_SRC (0x02)
  33. #define PRE_DIV_SRC (0x03)
  34. /* */
  35. #define TCON_PLL_VIDEO_X1 (0x000)
  36. #define TCON_PLL_VIDEO_X2 (0x002)
  37. #define PLL_CPU_ENABLE_STATE (0x1<<31)
  38. #define PLL_CPU_HAS_BEEN_STABLE (0x1<<28)
  39. #define PLL_CPU_DIV_P(reg) ((reg>>16)&0x3)
  40. #define PLL_CPU_FACTOR_N(reg) ((reg>>8)&0x1f)
  41. #define PLL_CPU_FACTOR_K(reg) ((reg>>4)&0x3)
  42. #define PLL_CPU_FACTOR_M(reg) ((reg)&0x3)
  43. #define PLL_AUDIO_ENABLE_STATE (0x1<<31)
  44. #define PLL_AUDIO_HAS_BEEN_STABLE (0x1<<28)
  45. #define PLL_AUDIO_FACTOR_N(reg) ((reg>>8)&0x7f)
  46. #define PLL_AUDIO_PREDIV_M(reg) ((reg)&0x1f)
  47. #define PLL_VIDEO_ENABLE_STATE (0x1<<31)
  48. #define PLL_VIDEO_MODE (0x1<<30)
  49. #define PLL_VIDEO_HAS_BEEN_STABLE (0x1<<28)
  50. #define PLL_VIDEO_FRAC_CLK_OUT (0x1<<25)
  51. #define PLL_VIDEO_MODE_SEL (0x1<<24)
  52. #define PLL_VIDEO_SDM_EN (0x1<<20)
  53. #define PLL_VIDEO_FACTOR_N(reg) ((reg>>8)&0x7f)
  54. #define PLL_VIDEO_PREDIV_M(reg) (reg&0xf)
  55. #define PLL_VE_ENABLE_STATE (0x1<<31)
  56. #define PLL_VE_HAS_BEEN_STABLE (0x1<<28)
  57. #define PLL_VE_FRAC_CLK_OUT (0x1<<25)
  58. #define PLL_VE_MODE_SEL (0x1<<24)
  59. #define PLL_VE_FACTOR_N(reg) ((reg>>8)&0x7f)
  60. #define PLL_VE_PREDIV_M(reg) (reg&0xf)
  61. #define PLL_DDR_ENABLE_STATE (0x1<<31)
  62. #define PLL_DDR_HAS_BEEN_STABLE (0x1<<28)
  63. #define SDRAM_SIGMA_DELTA_EN (0x1<<24)
  64. #define PLL_DDR_CFG_UPDATE (0x1<<20)
  65. #define PLL_DDR_FACTOR_N(reg) ((reg>>8)&0x1f)
  66. #define PLL_DDR_FACTOR_K(reg) ((reg>>4)&0x3)
  67. #define PLL_DDR_FACTOR_M(reg) ((reg)&0x3)
  68. #define PLL_PERIPH_ENABLE_STATE (0x1<<31)
  69. #define PLL_PERIPH_HAS_BEEN_STABLE (0x1<<28)
  70. #define PLL_PERIPH_24M_OUT_EN (0x1<<18)
  71. #define PLL_PERIPH_24M_POST_DIV(reg) ((reg>>16)&0x3)
  72. #define PLL_PERIPH_FACTOR_N(reg) ((reg>>8)&0x1f)
  73. #define PLL_PERIPH_FACTOR_K(reg) ((reg>>4)&0x3)
  74. #define PLL_PERIPH_FACTOR_M(reg) (reg&0x3)
  75. #define HCLKC_DIV(reg) ((reg>>16)&0x3)
  76. #define AHB_SRC_SEL(reg) ((reg>>12)&0x3)
  77. #define AHB_CLK_DIV(reg) ((reg>>4)&0x3)
  78. #define AHB_PRE_DIV(reg) ((reg>>6)&0x3)
  79. #define APH_CLK_PATIO(reg) ((reg>>8)&0x3)
  80. #define CCM_MMC_CTRL_OSCM24 (0x00)
  81. #define CCM_MMC_CTRL_PLL_PERIPH (0x01)
  82. #define CCU_BASE_ADDR (0x01C20000)
  83. #define _24MHZ_ (24000000U)
  84. #define _32KHZ_ (32000U)
  85. /* GATE */
  86. #define BUS_GATE_OFFSET_BIT (12)
  87. enum bus_gate
  88. {
  89. USB_OTG_GATING = (0x18 | (0x0 << BUS_GATE_OFFSET_BIT)),
  90. SPI1_GATING = (0x15 | (0x0 << BUS_GATE_OFFSET_BIT)),
  91. SPI0_GATING = (0x14 | (0x0 << BUS_GATE_OFFSET_BIT)),
  92. SDRAM_GATING = (0x0E | (0x0 << BUS_GATE_OFFSET_BIT)),
  93. SD1_GATING = (0x09 | (0x0 << BUS_GATE_OFFSET_BIT)),
  94. SD0_GATING = (0x08 | (0x0 << BUS_GATE_OFFSET_BIT)),
  95. DMA_GATING = (0x06 | (0x0 << BUS_GATE_OFFSET_BIT)),
  96. DEFE_GATING = (0x0E | (0x1 << BUS_GATE_OFFSET_BIT)),
  97. DEBE_GATING = (0x0C | (0x1 << BUS_GATE_OFFSET_BIT)),
  98. TVE_GATING = (0x0A | (0x1 << BUS_GATE_OFFSET_BIT)),
  99. TVD_GATING = (0x09 | (0x1 << BUS_GATE_OFFSET_BIT)),
  100. CSI_GATING = (0x08 | (0x1 << BUS_GATE_OFFSET_BIT)),
  101. DEINTERLACE_GATING = (0x05 | (0x1 << BUS_GATE_OFFSET_BIT)),
  102. LCD_GATING = (0x04 | (0x1 << BUS_GATE_OFFSET_BIT)),
  103. VE_GATING = (0x00 | (0x1 << BUS_GATE_OFFSET_BIT)),
  104. UART2_GATING = (0x16 | (0x2 << BUS_GATE_OFFSET_BIT)),
  105. UART1_GATING = (0x15 | (0x2 << BUS_GATE_OFFSET_BIT)),
  106. UART0_GATING = (0x14 | (0x2 << BUS_GATE_OFFSET_BIT)),
  107. TWI2_GATING = (0x12 | (0x2 << BUS_GATE_OFFSET_BIT)),
  108. TWI1_GATING = (0x11 | (0x2 << BUS_GATE_OFFSET_BIT)),
  109. TWI0_GATING = (0x10 | (0x2 << BUS_GATE_OFFSET_BIT)),
  110. DAUDIO_GATING = (0x0C | (0x2 << BUS_GATE_OFFSET_BIT)),
  111. RSB_GATING = (0x03 | (0x2 << BUS_GATE_OFFSET_BIT)),
  112. CIR_GATING = (0x02 | (0x2 << BUS_GATE_OFFSET_BIT)),
  113. OWA_GATING = (0x01 | (0x2 << BUS_GATE_OFFSET_BIT)),
  114. AUDIO_CODEC_GATING = (0x00 | (0x2 << BUS_GATE_OFFSET_BIT)),
  115. };
  116. enum dram_gate
  117. {
  118. BE_GATING_DRAM = 26,
  119. FE_GATING_DRAM = 24,
  120. TVD_GATING_DRAM = 3,
  121. DEINTERLACE_GATING_DRAM = 2,
  122. CSI_GATING_DRAM = 1,
  123. VE_GATING_DRAM = 0
  124. };
  125. enum mmc_clk_id
  126. {
  127. SDMMC0,
  128. SDMMC1,
  129. };
  130. struct tina_ccu
  131. {
  132. volatile rt_uint32_t pll_cpu_ctrl; /* 0x000 */
  133. volatile rt_uint32_t reserved0;
  134. volatile rt_uint32_t pll_audio_ctrl; /* 0x008 */
  135. volatile rt_uint32_t reserved1;
  136. volatile rt_uint32_t pll_video_ctrl; /* 0x010 */
  137. volatile rt_uint32_t reserved2;
  138. volatile rt_uint32_t pll_ve_ctrl; /* 0x018 */
  139. volatile rt_uint32_t reserved3;
  140. volatile rt_uint32_t pll_ddr_ctrl; /* 0x020 */
  141. volatile rt_uint32_t reserved4;
  142. volatile rt_uint32_t pll_periph_ctrl; /* 0x028 */
  143. volatile rt_uint32_t reserved5[9];
  144. volatile rt_uint32_t cpu_clk_src; /* 0x050 */
  145. volatile rt_uint32_t ahb_apb_hclkc_cfg; /* 0x054 */
  146. volatile rt_uint32_t reserved6[2];
  147. volatile rt_uint32_t bus_clk_gating0; /* 0x060 */
  148. volatile rt_uint32_t bus_clk_gating1; /* 0x064 */
  149. volatile rt_uint32_t bus_clk_gating2; /* 0x068 */
  150. volatile rt_uint32_t reserved7[7];
  151. volatile rt_uint32_t sdmmc0_clk; /* 0x088 */
  152. volatile rt_uint32_t sdmmc1_clk; /* 0x08C */
  153. volatile rt_uint32_t reserved8[8];
  154. volatile rt_uint32_t daudio_clk; /* 0x0B0 */
  155. volatile rt_uint32_t owa_clk; /* 0x0B4 */
  156. volatile rt_uint32_t cir_clk; /* 0x0B8 */
  157. volatile rt_uint32_t reserved9[4];
  158. volatile rt_uint32_t usbphy_clk; /* 0x0CC */
  159. volatile rt_uint32_t reserved10[12];
  160. volatile rt_uint32_t dram_gating; /* 0x100 */
  161. volatile rt_uint32_t be_clk; /* 0x104 */
  162. volatile rt_uint32_t reserved11;
  163. volatile rt_uint32_t fe_clk; /* 0x10C */
  164. volatile rt_uint32_t reserved12[2];
  165. volatile rt_uint32_t tcon_clk; /* 0x118*/
  166. volatile rt_uint32_t di_clk; /* 0x11C */
  167. volatile rt_uint32_t tve_clk; /* 0x120 */
  168. volatile rt_uint32_t tvd_clk; /* 0x124 */
  169. volatile rt_uint32_t reserved13[3];
  170. volatile rt_uint32_t csi_clk; /* 0x134 */
  171. volatile rt_uint32_t reserved14;
  172. volatile rt_uint32_t ve_clk; /* 0x13C */
  173. volatile rt_uint32_t audio_codec_clk; /* 0x140 */
  174. volatile rt_uint32_t avs_clk; /* 0x144 */
  175. volatile rt_uint32_t reserved15[46];
  176. volatile rt_uint32_t pll_stable_time0; /* 0x200 */
  177. volatile rt_uint32_t pll_stable_time1; /* 0x204 */
  178. volatile rt_uint32_t reserved16[6];
  179. volatile rt_uint32_t pll_cpu_bias; /* 0x220 */
  180. volatile rt_uint32_t pll_audio_bias; /* 0x224 */
  181. volatile rt_uint32_t pll_video_bias; /* 0x228 */
  182. volatile rt_uint32_t pll_ve_bias; /* 0x22C */
  183. volatile rt_uint32_t pll_ddr_bias; /* 0x230 */
  184. volatile rt_uint32_t pll_periph_bias; /* 0x234 */
  185. volatile rt_uint32_t reserved17[6];
  186. volatile rt_uint32_t pll_cpu_tun; /* 0x250 */
  187. volatile rt_uint32_t reserved18[3];
  188. volatile rt_uint32_t pll_ddr_tun; /* 0x260 */
  189. volatile rt_uint32_t reserved19[8];
  190. volatile rt_uint32_t pll_audio_pat_ctrl; /* 0x284 */
  191. volatile rt_uint32_t pll_video_pat_ctrl; /* 0x288 */
  192. volatile rt_uint32_t reserved20;
  193. volatile rt_uint32_t pll_ddr_pat_ctrl; /* 0x290 */
  194. volatile rt_uint32_t reserved21[11];
  195. volatile rt_uint32_t bus_soft_rst0; /* 0x2C0 */
  196. volatile rt_uint32_t bus_soft_rst1; /* 0x2C4 */
  197. volatile rt_uint32_t reserved22[2];
  198. volatile rt_uint32_t bus_soft_rst2; /* 0x2D0 */
  199. };
  200. typedef struct tina_ccu *tina_ccu_t;
  201. #define CCU ((tina_ccu_t) CCU_BASE_ADDR)
  202. int cpu_get_pll_clk(void);
  203. int audio_get_pll_clk(void);
  204. int video_get_pll_clk(void);
  205. int ve_get_pll_clk(void);
  206. int ddr_get_pll_clk(void);
  207. int periph_get_pll_clk(void);
  208. int cpu_get_clk(void);
  209. int ahb_get_clk(void);
  210. int apb_get_clk(void);
  211. rt_err_t cpu_set_pll_clk(int clk);
  212. rt_err_t audio_set_pll_clk(int clk);
  213. rt_err_t video_set_pll_clk(int clk);
  214. rt_err_t ve_set_pll_clk(int clk);
  215. rt_err_t periph_set_pll_clk(int clk);
  216. rt_err_t cpu_set_clk(int clk);
  217. rt_err_t bus_gate_clk_enalbe(enum bus_gate bus);
  218. rt_err_t bus_gate_clk_disalbe(enum bus_gate bus);
  219. rt_err_t bus_software_reset_enalbe(enum bus_gate bus);
  220. rt_err_t bus_software_reset_disalbe(enum bus_gate bus);
  221. rt_err_t dram_gate_clk_enable(enum dram_gate dram_gate);
  222. rt_err_t dram_gate_clk_disable(enum dram_gate dram_gate);
  223. rt_err_t mmc_set_clk(enum mmc_clk_id clk_id, int hz);
  224. #endif