soc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2016 Open-RnD Sp. z o.o.
  3. * Copyright (c) 2016 Linaro Limited.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. /**
  8. * @file
  9. * @brief System/hardware module for ATJ215X processor
  10. */
  11. #include <device.h>
  12. #include <init.h>
  13. #include <arch/cpu.h>
  14. #include "soc.h"
  15. #include <linker/linker-defs.h>
  16. //#include <arch/arm/aarch32/cortex_m/cmsis.h>
  17. static void jtag_config(unsigned int group_id)
  18. {
  19. printk("jtag switch to group=%d\n", group_id);
  20. if (group_id < 3)
  21. sys_write32((sys_read32(JTAG_CTL) & ~(3 << 0)) | (group_id << 0) | (1 << 4), JTAG_CTL);
  22. }
  23. void jtag_set(void)
  24. {
  25. jtag_config(0);
  26. }
  27. /**
  28. * \brief clear watchdog
  29. */
  30. void soc_watchdog_clear(void)
  31. {
  32. sys_set_bit(WD_CTL, 0);
  33. }
  34. static void wd_check_wdreset_cnt(void)
  35. {
  36. uint32_t reset_cnt;
  37. soc_watchdog_clear();
  38. soc_pstore_get(SOC_PSTORE_TAG_WD_RESET_CNT, &reset_cnt);
  39. printk("wd cnt=%d, WD_CTL=0x%x\n", reset_cnt, sys_read32(WD_CTL));
  40. if(reset_cnt > 10){
  41. printk("reboot ota\n");
  42. soc_pstore_set(SOC_PSTORE_TAG_WD_RESET_CNT, 0);
  43. sys_pm_reboot(REBOOT_TYPE_GOTO_OTA);
  44. }else{
  45. reset_cnt++;
  46. soc_pstore_set(SOC_PSTORE_TAG_WD_RESET_CNT, reset_cnt);
  47. }
  48. }
  49. /**
  50. * \brief if boot to main clear wd reset cnt
  51. */
  52. void wd_clear_wdreset_cnt(void)
  53. {
  54. soc_pstore_set(SOC_PSTORE_TAG_WD_RESET_CNT, 0);
  55. }
  56. __sleepfunc int soc_dvfs_opt(void)
  57. {
  58. return (sys_read32(UID1) >> 17) & 0x7;
  59. }
  60. uint8_t ipmsg_btc_get_ic_pkt(void)
  61. {
  62. uint32_t val;
  63. val = sys_read32(UID1);
  64. return (uint8_t)((val >> 24) & 0xF); /* bit24~27: IC Package */
  65. }
  66. #if 0
  67. static void cpu_init(void)
  68. {
  69. int i;
  70. unsigned int val;
  71. printk("rc192 cal\n");
  72. sys_write32(0x80011481, RC192M_CTL);
  73. soc_udelay(20);
  74. sys_write32(0x8001148d, RC192M_CTL);
  75. soc_udelay(200);
  76. while(1){
  77. if(sys_read32(RC192M_CTL)& (1<<24)){
  78. for(i = 0; i < 3; i++){ // 3 cal done is ok
  79. soc_udelay(20);
  80. if(!(sys_read32(RC192M_CTL)& (1<<24)))
  81. break;
  82. }
  83. if(i == 3)
  84. break;
  85. }
  86. }
  87. val = (sys_read32(RC192M_CTL) & (0x7f<<25)) >> 25;
  88. if(val)
  89. val -= 1;
  90. sys_write32(0x80011001 | (val<<4) , RC192M_CTL);
  91. soc_udelay(200);
  92. sys_write32(0x215, CMU_SYSCLK);// CPU rcM256/2 =128, ahb div 1
  93. }
  94. #endif
  95. __sleepfunc static void leopard_set_hosc_ctrl()
  96. {
  97. /*
  98. IO_WRITE(HOSC_CTL, (0x601f1b6 | (CAP<<17)));
  99. 如果是首次配置(HOSC_CTL[25]: 0->1)需要将CPUCLK切换到RC4M,等待HOSC_CTL[28]为1后再将CPUCLK切回HOSC/COREPLL
  100. */
  101. uint32_t val;
  102. /* switch cpuclk src to 4MRC */
  103. val = sys_read32(CMU_SYSCLK);
  104. sys_write32(val & ~0x7, CMU_SYSCLK);
  105. /* update HOSC_CTL & wait HOSC_READY */
  106. sys_write32(0x600f7f6 | (sys_read32(HOSC_CTL) & HOSC_CTL_HOSC_CAP_MASK), HOSC_CTL);
  107. while(!(sys_read32(HOSC_CTL) & (1<< HOSC_CTL_HOSC_READY)));
  108. /* backup cpuclk src to COREPLL */
  109. sys_write32(val, CMU_SYSCLK);
  110. }
  111. /**
  112. * @brief Perform basic hardware initialization at boot.
  113. *
  114. * This needs to be run from the very beginning.
  115. * So the init priority has to be 0 (zero).
  116. *
  117. * @return 0
  118. */
  119. static int leopard_init(const struct device *arg)
  120. {
  121. uint32_t key;
  122. uint32_t val;
  123. ARG_UNUSED(arg);
  124. soc_udelay(50);
  125. leopard_set_hosc_ctrl();
  126. soc_udelay(10);
  127. key = irq_lock();
  128. /* Install default handler that simply resets the CPU
  129. * if configured in the kernel, NOP otherwise
  130. */
  131. NMI_INIT();
  132. irq_unlock(key);
  133. soc_powergate_init();
  134. sys_write32(0x10e, CMU_GPUCLK);//COREPLL/1.5
  135. acts_clock_peripheral_enable(CLOCK_ID_GPU); //for the gpu reset, because the gpu is in an unstable state
  136. soc_udelay(1);
  137. acts_reset_peripheral_deassert(RESET_ID_GPU);
  138. soc_udelay(1);
  139. acts_reset_peripheral_assert(RESET_ID_GPU);//
  140. soc_udelay(2);
  141. acts_clock_peripheral_disable(CLOCK_ID_GPU);//
  142. /* disable gpu power gating */
  143. //sys_write32(sys_read32(PWRGATE_DIG) & ~(0x1 << 25), PWRGATE_DIG);
  144. if(soc_powergate_is_poweron(POWERGATE_GPU_PG_DEV))
  145. soc_powergate_set(POWERGATE_GPU_PG_DEV, false);
  146. //cpu_init();
  147. wd_check_wdreset_cnt();
  148. /* Update CMSIS SystemCoreClock variable (HCLK) */
  149. /* At reset, system core clock is set to 16 MHz from HSI */
  150. //SystemCoreClock = 16000000;
  151. //while(!arg);
  152. //sys_write32(0x0, WIO0_CTL); //default set wio0 to gpio func
  153. /*for lowpower*/
  154. //sys_write32(0x30F, SPI1_CLKGATING);
  155. /* init ppi */
  156. ppi_init();
  157. /* Initialize SDMA */
  158. acts_reset_peripheral_assert(RESET_ID_SDMA);
  159. acts_clock_peripheral_enable(CLOCK_ID_SDMA);
  160. acts_reset_peripheral_deassert(RESET_ID_SDMA);
  161. /* Enable SDMA, DE access SPI0 NOR and SPI1 PSRAM, and GPU accessing SPI1 PSRAM */
  162. //sys_write32(sys_read32(SPICACHE_CTL) | BIT(10) | BIT(11), SPICACHE_CTL);
  163. sys_write32((sys_read32(SPICACHE_CTL) & ~(0x3 << 5)) | (0x1 << 5) | BIT(10) | BIT(11), SPICACHE_CTL);
  164. if (soc_dvfs_opt()) {
  165. sys_write32((sys_read32(SPI1_DELAYCHAIN) & ~(0x3f << 8)) \
  166. | (SPI1_DELAYCHAIN_CLKOUT << 8), SPI1_DELAYCHAIN);
  167. sys_write32(sys_read32(SPI1_CACHE_CTL) | BIT(8) | BIT(10) | BIT(11) | BIT(14) | BIT(15), SPI1_CACHE_CTL);
  168. } else {
  169. sys_write32(sys_read32(SPI1_CACHE_CTL) | BIT(8) | BIT(10) | BIT(11) | BIT(14) /*| BIT(15)*/, SPI1_CACHE_CTL);
  170. }
  171. sys_write32(BIT(24) | (0<<4) | (0xa<<8) | (0x1<<0), SPI1_GPU_CTL);
  172. /* Share RAM clock, select HOSC(32MHZ)*/
  173. val = sys_read32(CMU_MEMCLKSRC0);
  174. val = (val & ~(7 << 25)) | (1 << 25);
  175. sys_write32(val, CMU_MEMCLKSRC0);
  176. val = sys_read32(CMU_MEMCLKEN0);
  177. val |= (1 << 25);
  178. sys_write32(val, CMU_MEMCLKEN0);
  179. //val = sys_read32(VOUT_CTL1_S1M) ;
  180. //val = (val & ~(0xFff)) | 0xedd; // vdd set 1.2
  181. //sys_write32(val, VOUT_CTL1_S1M);
  182. //sys_write32(0x12, CMU_SYSCLK);// cpu 200
  183. return 0;
  184. }
  185. SYS_INIT(leopard_init, PRE_KERNEL_1, 0);
  186. /*if CONFIG_WDOG_ACTS enable , wd driver call wd_clear_wdreset_cnt */
  187. #ifndef CONFIG_WDOG_ACTS
  188. /**
  189. * @brief Perform basic hardware initialization at boot.
  190. *
  191. * before boot to main clear wd reset cnt
  192. * @return 0
  193. */
  194. static int wd_reset_cnt_init(const struct device *arg)
  195. {
  196. wd_clear_wdreset_cnt();
  197. sys_write32(0, WD_CTL); /*disable watch dog*/
  198. return 0;
  199. }
  200. SYS_INIT(wd_reset_cnt_init, APPLICATION, 91);
  201. #endif