drv_pmu.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * File : drv_pmu.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2008 - 2012, 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. * 2016-03-29 Urey the first version
  23. */
  24. #include <rthw.h>
  25. #include <rtthread.h>
  26. #include <rtdevice.h>
  27. #include <stdint.h>
  28. #include "board.h"
  29. #include "drv_clock.h"
  30. #include "drv_gpio.h"
  31. #include "drv_ost.h"
  32. #include "drv_rtc.h"
  33. struct sleep_save_register
  34. {
  35. unsigned int lcr;
  36. unsigned int opcr;
  37. unsigned int sleep_voice_enable;
  38. unsigned int ddr_training_space[20];
  39. };
  40. static struct sleep_save_register s_reg;
  41. static void write_aic_register(unsigned int addr,unsigned char val)
  42. {
  43. while(REG_ICDC_RGADW & (1 << 16));
  44. REG_ICDC_RGADW = (1 << 16) | (addr << 8) | val;
  45. }
  46. static void _delay_us(rt_uint32_t ns)
  47. {
  48. volatile rt_uint16_t delay;
  49. while(ns--)
  50. {
  51. delay = 200;
  52. while(delay--);
  53. }
  54. }
  55. static void _delay_ms(rt_uint32_t ms)
  56. {
  57. volatile rt_uint16_t delay;
  58. while(ms--)
  59. {
  60. _delay_us(1000);
  61. }
  62. }
  63. #if 0
  64. static int jz_pm_do_sleep(void)
  65. {
  66. unsigned int div;
  67. unsigned long opcr = REG_CPM_OPCR;
  68. unsigned long icmr0 = REG_INTC_IMCR(0);
  69. unsigned long icmr1 = REG_INTC_IMCR(1);
  70. unsigned long sleep_gpio_save[5*(GPIO_NR_PORTS)];
  71. unsigned long cpuflags;
  72. unsigned long msc0cdr = REG_CPM_MSC0CDR;
  73. void (*resume_addr)(void);
  74. unsigned int val;
  75. unsigned int gint_mask = REG_GINT_MASK;
  76. unsigned int level = rt_hw_interrupt_disable();
  77. unsigned int clkgr0 = REG_CPM_CLKGR0;
  78. /* set SLEEP mode */
  79. //CMSREG32(CPM_LCR, LCR_LPM_SLEEP, LCR_LPM_MASK);
  80. REG_CPM_CLKGR0 &= ~(1 << 11);
  81. write_aic_register(0x13, 0x10);
  82. write_aic_register(0xd, 0xb1);
  83. write_aic_register(0xe, 0xb5);
  84. *(volatile unsigned int *)0xb000003c |= (1 << 25) | (1 << 21) | (1 << 20);
  85. REG_CPM_CLKGR0 = 0x0fdefff7;
  86. /* Mask all interrupts */
  87. REG_INTC_IMCR(0) = 0xffffffff;
  88. REG_INTC_IMCR(1) = 0xffffffff;
  89. REG_GINT_MASK = 0;
  90. REG_GINT_MASK = 1 << 31;
  91. *((volatile unsigned int *)(0xb2000038)) = 1;
  92. /* OUTREG32(INTC_ICMCR(0), 0xffffffff); */
  93. /* /\* unmask rtc interrupts *\/ */
  94. /* OUTREG32(INTC_ICMCR(1), 0x1); */
  95. /* Sleep on-board modules */
  96. jzsoc_do_sleep(sleep_gpio_save);
  97. /* config_irq_wakeup(); */
  98. load_pm_firmware_new(SLEEP_TCSM_SPACE);
  99. sleep_param = (struct sleep_param *)SLEEP_TCSM_SPACE;
  100. sleep_param->post_resume_pc = (unsigned int)restore_goto;
  101. sleep_param->uart_id = 2;
  102. memcpy(&s_reg.ddr_training_space,(void*)0x80000000,sizeof(s_reg.ddr_training_space));
  103. s_reg.opcr = INREG32(CPM_OPCR);
  104. s_reg.lcr = INREG32(CPM_LCR);
  105. /*
  106. * set OPCR.
  107. */
  108. val = s_reg.opcr;
  109. #if 0
  110. val &= ~((1 << 25) | (1 << 22) | (0xfff << 8) | (1 << 7) | (1 << 6) | (1 << 4) | (1 << 3) | (1 << 2));
  111. val |= (1 << 31) | (1 << 30) | (1 << 25) | (1 << 23) | (0xfff << 8) | (1 << 4) | (1 << 3) | (1 << 2);
  112. #else
  113. val &= ~((1 << 31)|(1 << 30)| (1 << 25) | (1 << 22) | (0xfff << 8) | (1 << 7) | (1 << 6) | (1 << 4) | (1 << 3) | (1 << 2));
  114. val |= (1 << 25) | (1 << 23) | (0xfff << 8) | (1 << 3) | (1 << 2) | (1 << 4);
  115. #endif
  116. REG_CPM_OPCR = val;
  117. *(volatile unsigned int *)0xb000003c &= ~(1 << 31);
  118. *(volatile unsigned int *)0xb000003c |= (1 << 20);
  119. val = s_reg.lcr;
  120. val &= ~3;
  121. //val |= 1;
  122. val |= 0xff << 8; /* power stable time */
  123. REG_CPM_LCR = val;
  124. // *(volatile unsigned int *)0xb0000010 &= ~(1 << 8);
  125. // *(volatile unsigned int *)0xb0000014 &= ~(1 << 7);
  126. printf("clkgr = 0x%08x\n", *(volatile unsigned int *)0xb0000020);
  127. printf("opcr = 0x%08x\n", *(volatile unsigned int *)0xb0000024);
  128. printf("otg phy = 0x%08x\n", *(volatile unsigned int *)0xb0000048);
  129. printf("otg = 0x%08x\n", *(volatile unsigned int *)0xb0000050);
  130. mb();
  131. save_goto((unsigned int)sleep_param->pm_core_enter);
  132. mb();
  133. memcpy((void*)0x80000000,&s_reg.ddr_training_space,sizeof(s_reg.ddr_training_space));
  134. dma_cache_wback_inv(0x80000000,sizeof(s_reg.ddr_training_space));
  135. OUTREG32(CPM_LCR, s_reg.lcr);
  136. OUTREG32(CPM_OPCR, s_reg.opcr);
  137. spin_unlock_irqrestore(sr);
  138. REG_GINT_MASK = gint_mask;
  139. /* Restore interrupts */
  140. //*((volatile unsigned int *)(0xb2000008)) = 1;
  141. REG_CPM_CLKGR0 = clkgr0;
  142. *((volatile unsigned int *)(0xb2000034)) = 1;
  143. OUTREG32(INTC_ICMR(0), icmr0);
  144. OUTREG32(INTC_ICMR(1), icmr1);
  145. /* Resume on-board modules */
  146. jzsoc_do_resume(sleep_gpio_save);
  147. /* Restore Oscillator and Power Control Register */
  148. /* OUTREG32(CPM_OPCR, opcr); */
  149. return 0;
  150. }
  151. #endif
  152. /*
  153. * Function: Keep power for CPU core when reset.
  154. * So that EPC, tcsm and so on can maintain it's status after reset-key pressed.
  155. */
  156. int reset_keep_power(void)
  157. {
  158. rtc_write_reg(RTC_BASE + RTC_PWRONCR, rtc_read_reg(RTC_BASE + RTC_PWRONCR) & ~(1 << 0));
  159. return 0;
  160. }
  161. int drv_pmu_get_keypwr(void)
  162. {
  163. int level = 0;
  164. #ifdef BOARD_CANNA_OVC
  165. level = gpio_get_value(DET_PWR_PORT,DET_PWR_PIN);
  166. do
  167. {
  168. _delay_ms(5); //È¥¶¶
  169. }while(level != gpio_get_value(DET_PWR_PORT,DET_PWR_PIN));
  170. #endif
  171. return level;
  172. }
  173. void drv_pmu_power_down(void)
  174. {
  175. #ifdef BOARD_CANNA_OVC
  176. rt_kprintf("SET STB down...\n");
  177. gpio_set_value(EXT_DEV_RST_PORT, EXT_DEV_RST_PIN, 0);
  178. gpio_direction_output(CTR_STB_PORT, CTR_STB_PIN, 0);
  179. _delay_ms(100);
  180. gpio_direction_output(CTR_LOCK_PORT, CTR_LOCK_PIN, 1);
  181. _delay_ms(100);
  182. rt_hw_led_off(LED_GREEN);
  183. rt_hw_led_off(LED_RED);
  184. rt_hw_led_off(LED_BLUE);
  185. #endif
  186. }
  187. void drv_pmu_power_up(void)
  188. {
  189. }
  190. int drv_pmu_init(void)
  191. {
  192. volatile unsigned int lcr, opcr;
  193. /* init opcr and lcr for idle */
  194. lcr = cpm_inl(CPM_LCR);
  195. lcr &= ~(0x3); /* LCR.SLEEP.DS=0'b0,LCR.LPM=1'b00*/
  196. lcr |= 0xff << 8; /* power stable time */
  197. cpm_outl(lcr, CPM_LCR);
  198. opcr = cpm_inl(CPM_OPCR);
  199. opcr |= 0xff << 8; /* EXCLK stable time */
  200. opcr &= ~(1 << 4); /* EXCLK stable time */
  201. cpm_outl(opcr, CPM_OPCR);
  202. return 0;
  203. }