drv_clock.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 BE_GATING_DRAM (0x1<<26)
  36. #define FE_GATING_DRAM (0x1<<24)
  37. #define TVD_GATING_DRAM (0x1<<3)
  38. #define DEINTERLACE_GATING_DRAM (0x1<<2)
  39. #define CSI_GATING_DRAM (0x1<<1)
  40. #define VE_GATING_DRAM (0x1<<0)
  41. /* */
  42. #define TCON_PLL_VIDEO_X1 (0x000)
  43. #define TCON_PLL_VIDEO_X2 (0x002)
  44. #define PLL_CPU_ENABLE_STATE (0x1<<31)
  45. #define PLL_CPU_HAS_BEEN_STABLE (0x1<<28)
  46. #define PLL_CPU_DIV_P(reg) ((reg>>16)&0x3)
  47. #define PLL_CPU_FACTOR_N(reg) ((reg>>8)&0x1f)
  48. #define PLL_CPU_FACTOR_K(reg) ((reg>>4)&0x3)
  49. #define PLL_CPU_FACTOR_M(reg) ((reg)&0x3)
  50. #define PLL_AUDIO_ENABLE_STATE (0x1<<31)
  51. #define PLL_AUDIO_HAS_BEEN_STABLE (0x1<<28)
  52. #define PLL_AUDIO_FACTOR_N(reg) ((reg>>8)&0x7f)
  53. #define PLL_AUDIO_PREDIV_M(reg) ((reg)&0x1f)
  54. #define PLL_VIDEO_ENABLE_STATE (0x1<<31)
  55. #define PLL_VIDEO_MODE (0x1<<30)
  56. #define PLL_VIDEO_HAS_BEEN_STABLE (0x1<<28)
  57. #define PLL_VIDEO_FRAC_CLK_OUT (0x1<<25)
  58. #define PLL_VIDEO_MODE_SEL (0x1<<24)
  59. #define PLL_VIDEO_SDM_EN (0x1<<20)
  60. #define PLL_VIDEO_FACTOR_N(reg) ((reg>>8)&0x7f)
  61. #define PLL_VIDEO_PREDIV_M(reg) (reg&0xf)
  62. #define PLL_VE_ENABLE_STATE (0x1<<31)
  63. #define PLL_VE_HAS_BEEN_STABLE (0x1<<28)
  64. #define PLL_VE_FRAC_CLK_OUT (0x1<<25)
  65. #define PLL_VE_MODE_SEL (0x1<<24)
  66. #define PLL_VE_FACTOR_N(reg) ((reg>>8)&0x7f)
  67. #define PLL_VE_PREDIV_M(reg) (reg&0xf)
  68. #define PLL_DDR_ENABLE_STATE (0x1<<31)
  69. #define PLL_DDR_HAS_BEEN_STABLE (0x1<<28)
  70. #define SDRAM_SIGMA_DELTA_EN (0x1<<24)
  71. #define PLL_DDR_CFG_UPDATE (0x1<<20)
  72. #define PLL_DDR_FACTOR_N(reg) ((reg>>8)&0x1f)
  73. #define PLL_DDR_FACTOR_K(reg) ((reg>>4)&0x3)
  74. #define PLL_DDR_FACTOR_M(reg) ((reg)&0x3)
  75. #define PLL_PERIPH_ENABLE_STATE (0x1<<31)
  76. #define PLL_PERIPH_HAS_BEEN_STABLE (0x1<<28)
  77. #define PLL_PERIPH_24M_OUT_EN (0x1<<18)
  78. #define PLL_PERIPH_24M_POST_DIV(reg) ((reg>>16)&0x3)
  79. #define PLL_PERIPH_FACTOR_N(reg) ((reg>>8)&0x1f)
  80. #define PLL_PERIPH_FACTOR_K(reg) ((reg>>4)&0x3)
  81. #define PLL_PERIPH_FACTOR_M(reg) (reg&0x3)
  82. #define HCLKC_DIV(reg) ((reg>>16)&0x3)
  83. #define AHB_SRC_SEL(reg) ((reg>>12)&0x3)
  84. #define AHB_CLK_DIV(reg) ((reg>>4)&0x3)
  85. #define AHB_PRE_DIV(reg) ((reg>>6)&0x3)
  86. #define APH_CLK_PATIO(reg) ((reg>>8)&0x3)
  87. #define CCM_MMC_CTRL_OSCM24 (0x00)
  88. #define CCM_MMC_CTRL_PLL_PERIPH (0x01)
  89. #define CCU_BASE_ADDR (0x01C20000)
  90. #define _24MHZ_ (24000000U)
  91. #define _32KHZ_ (32000U)
  92. /* GATE */
  93. #define BUS_GATE_OFFSET_BIT (12)
  94. enum bus_gate
  95. {
  96. USB_OTG_GATING = (0x18 | (0x0 << BUS_GATE_OFFSET_BIT)),
  97. SPI1_GATING = (0x15 | (0x0 << BUS_GATE_OFFSET_BIT)),
  98. SPI0_GATING = (0x14 | (0x0 << BUS_GATE_OFFSET_BIT)),
  99. SDRAM_GATING = (0x0E | (0x0 << BUS_GATE_OFFSET_BIT)),
  100. SD1_GATING = (0x09 | (0x0 << BUS_GATE_OFFSET_BIT)),
  101. SD0_GATING = (0x08 | (0x0 << BUS_GATE_OFFSET_BIT)),
  102. DMA_GATING = (0x06 | (0x0 << BUS_GATE_OFFSET_BIT)),
  103. DEFE_GATING = (0x0E | (0x1 << BUS_GATE_OFFSET_BIT)),
  104. DEBE_GATING = (0x0C | (0x1 << BUS_GATE_OFFSET_BIT)),
  105. TVE_GATING = (0x0A | (0x1 << BUS_GATE_OFFSET_BIT)),
  106. TVD_GATING = (0x09 | (0x1 << BUS_GATE_OFFSET_BIT)),
  107. CSI_GATING = (0x08 | (0x1 << BUS_GATE_OFFSET_BIT)),
  108. DEINTERLACE_GATING = (0x05 | (0x1 << BUS_GATE_OFFSET_BIT)),
  109. LCD_GATING = (0x04 | (0x1 << BUS_GATE_OFFSET_BIT)),
  110. VE_GATING = (0x00 | (0x1 << BUS_GATE_OFFSET_BIT)),
  111. UART2_GATING = (0x16 | (0x2 << BUS_GATE_OFFSET_BIT)),
  112. UART1_GATING = (0x15 | (0x2 << BUS_GATE_OFFSET_BIT)),
  113. UART0_GATING = (0x14 | (0x2 << BUS_GATE_OFFSET_BIT)),
  114. TWI2_GATING = (0x12 | (0x2 << BUS_GATE_OFFSET_BIT)),
  115. TWI1_GATING = (0x11 | (0x2 << BUS_GATE_OFFSET_BIT)),
  116. TWI0_GATING = (0x10 | (0x2 << BUS_GATE_OFFSET_BIT)),
  117. DAUDIO_GATING = (0x0C | (0x2 << BUS_GATE_OFFSET_BIT)),
  118. RSB_GATING = (0x03 | (0x2 << BUS_GATE_OFFSET_BIT)),
  119. CIR_GATING = (0x02 | (0x2 << BUS_GATE_OFFSET_BIT)),
  120. OWA_GATING = (0x01 | (0x2 << BUS_GATE_OFFSET_BIT)),
  121. AUDIO_CODEC_GATING = (0x00 | (0x2 << BUS_GATE_OFFSET_BIT)),
  122. };
  123. struct tina_ccu
  124. {
  125. volatile rt_uint32_t pll_cpu_ctrl; /* 0x000 */
  126. volatile rt_uint32_t reserved0;
  127. volatile rt_uint32_t pll_audio_ctrl; /* 0x008 */
  128. volatile rt_uint32_t reserved1;
  129. volatile rt_uint32_t pll_video_ctrl; /* 0x010 */
  130. volatile rt_uint32_t reserved2;
  131. volatile rt_uint32_t pll_ve_ctrl; /* 0x018 */
  132. volatile rt_uint32_t reserved3;
  133. volatile rt_uint32_t pll_ddr_ctrl; /* 0x020 */
  134. volatile rt_uint32_t reserved4;
  135. volatile rt_uint32_t pll_periph_ctrl; /* 0x028 */
  136. volatile rt_uint32_t reserved5[9];
  137. volatile rt_uint32_t cpu_clk_src; /* 0x050 */
  138. volatile rt_uint32_t ahb_apb_hclkc_cfg; /* 0x054 */
  139. volatile rt_uint32_t reserved6[2];
  140. volatile rt_uint32_t bus_clk_gating0; /* 0x060 */
  141. volatile rt_uint32_t bus_clk_gating1; /* 0x064 */
  142. volatile rt_uint32_t bus_clk_gating2; /* 0x068 */
  143. volatile rt_uint32_t reserved7[7];
  144. volatile rt_uint32_t sdmmc0_clk; /* 0x088 */
  145. volatile rt_uint32_t sdmmc1_clk; /* 0x08C */
  146. volatile rt_uint32_t reserved8[8];
  147. volatile rt_uint32_t daudio_clk; /* 0x0B0 */
  148. volatile rt_uint32_t owa_clk; /* 0x0B4 */
  149. volatile rt_uint32_t cir_clk; /* 0x0B8 */
  150. volatile rt_uint32_t reserved9[4];
  151. volatile rt_uint32_t usbphy_clk; /* 0x0CC */
  152. volatile rt_uint32_t reserved10[12];
  153. volatile rt_uint32_t dram_gating; /* 0x100 */
  154. volatile rt_uint32_t be_clk; /* 0x104 */
  155. volatile rt_uint32_t reserved11;
  156. volatile rt_uint32_t fe_clk; /* 0x10C */
  157. volatile rt_uint32_t reserved12[2];
  158. volatile rt_uint32_t tcon_clk; /* 0x118*/
  159. volatile rt_uint32_t di_clk; /* 0x11C */
  160. volatile rt_uint32_t tve_clk; /* 0x120 */
  161. volatile rt_uint32_t tvd_clk; /* 0x124 */
  162. volatile rt_uint32_t reserved13[3];
  163. volatile rt_uint32_t csi_clk; /* 0x134 */
  164. volatile rt_uint32_t reserved14;
  165. volatile rt_uint32_t ve_clk; /* 0x13C */
  166. volatile rt_uint32_t audio_codec_clk; /* 0x140 */
  167. volatile rt_uint32_t avs_clk; /* 0x144 */
  168. volatile rt_uint32_t reserved15[46];
  169. volatile rt_uint32_t pll_stable_time0; /* 0x200 */
  170. volatile rt_uint32_t pll_stable_time1; /* 0x204 */
  171. volatile rt_uint32_t reserved16[6];
  172. volatile rt_uint32_t pll_cpu_bias; /* 0x220 */
  173. volatile rt_uint32_t pll_audio_bias; /* 0x224 */
  174. volatile rt_uint32_t pll_video_bias; /* 0x228 */
  175. volatile rt_uint32_t pll_ve_bias; /* 0x22C */
  176. volatile rt_uint32_t pll_ddr_bias; /* 0x230 */
  177. volatile rt_uint32_t pll_periph_bias; /* 0x234 */
  178. volatile rt_uint32_t reserved17[6];
  179. volatile rt_uint32_t pll_cpu_tun; /* 0x250 */
  180. volatile rt_uint32_t reserved18[3];
  181. volatile rt_uint32_t pll_ddr_tun; /* 0x260 */
  182. volatile rt_uint32_t reserved19[8];
  183. volatile rt_uint32_t pll_audio_pat_ctrl; /* 0x284 */
  184. volatile rt_uint32_t pll_video_pat_ctrl; /* 0x288 */
  185. volatile rt_uint32_t reserved20;
  186. volatile rt_uint32_t pll_ddr_pat_ctrl; /* 0x290 */
  187. volatile rt_uint32_t reserved21[11];
  188. volatile rt_uint32_t bus_soft_rst0; /* 0x2C0 */
  189. volatile rt_uint32_t bus_soft_rst1; /* 0x2C4 */
  190. volatile rt_uint32_t reserved22[2];
  191. volatile rt_uint32_t bus_soft_rst2; /* 0x2D0 */
  192. };
  193. typedef struct tina_ccu *tina_ccu_t;
  194. #define CCU ((tina_ccu_t) CCU_BASE_ADDR)
  195. int cpu_get_pll_clk(void);
  196. int audio_get_pll_clk(void);
  197. int video_get_pll_clk(void);
  198. int ve_get_pll_clk(void);
  199. int ddr_get_pll_clk(void);
  200. int periph_get_pll_clk(void);
  201. int cpu_get_clk(void);
  202. int ahb_get_clk(void);
  203. int apb_get_clk(void);
  204. rt_err_t cpu_set_pll_clk(int clk);
  205. rt_err_t audio_set_pll_clk(int clk);
  206. rt_err_t video_set_pll_clk(int clk);
  207. rt_err_t ve_set_pll_clk(int clk);
  208. rt_err_t periph_set_pll_clk(int clk);
  209. rt_err_t cpu_set_clk(int clk);
  210. rt_err_t bus_gate_clk_enalbe(enum bus_gate bus);
  211. rt_err_t bus_gate_clk_disalbe(enum bus_gate bus);
  212. rt_err_t bus_software_reset_enalbe(enum bus_gate bus);
  213. rt_err_t bus_software_reset_disalbe(enum bus_gate bus);
  214. #endif