1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
/*
* File : drv_pmu.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2008 - 2012, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2016-03-29 Urey the first version
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <stdint.h>
#include "board.h"
#include "drv_clock.h"
#include "drv_gpio.h"
#include "drv_ost.h"
#include "drv_rtc.h"
struct sleep_save_register
{
unsigned int lcr;
unsigned int opcr;
unsigned int sleep_voice_enable;
unsigned int ddr_training_space[20];
};
static struct sleep_save_register s_reg;
static void write_aic_register(unsigned int addr,unsigned char val)
{
while(REG_ICDC_RGADW & (1 << 16));
REG_ICDC_RGADW = (1 << 16) | (addr << 8) | val;
}
static void _delay_us(rt_uint32_t ns)
{
volatile rt_uint16_t delay;
while(ns--)
{
delay = 200;
while(delay--);
}
}
static void _delay_ms(rt_uint32_t ms)
{
volatile rt_uint16_t delay;
while(ms--)
{
_delay_us(1000);
}
}
#if 0
static int jz_pm_do_sleep(void)
{
unsigned int div;
unsigned long opcr = REG_CPM_OPCR;
unsigned long icmr0 = REG_INTC_IMCR(0);
unsigned long icmr1 = REG_INTC_IMCR(1);
unsigned long sleep_gpio_save[5*(GPIO_NR_PORTS)];
unsigned long cpuflags;
unsigned long msc0cdr = REG_CPM_MSC0CDR;
void (*resume_addr)(void);
unsigned int val;
unsigned int gint_mask = REG_GINT_MASK;
unsigned int level = rt_hw_interrupt_disable();
unsigned int clkgr0 = REG_CPM_CLKGR0;
/* set SLEEP mode */
//CMSREG32(CPM_LCR, LCR_LPM_SLEEP, LCR_LPM_MASK);
REG_CPM_CLKGR0 &= ~(1 << 11);
write_aic_register(0x13, 0x10);
write_aic_register(0xd, 0xb1);
write_aic_register(0xe, 0xb5);
*(volatile unsigned int *)0xb000003c |= (1 << 25) | (1 << 21) | (1 << 20);
REG_CPM_CLKGR0 = 0x0fdefff7;
/* Mask all interrupts */
REG_INTC_IMCR(0) = 0xffffffff;
REG_INTC_IMCR(1) = 0xffffffff;
REG_GINT_MASK = 0;
REG_GINT_MASK = 1 << 31;
*((volatile unsigned int *)(0xb2000038)) = 1;
/* OUTREG32(INTC_ICMCR(0), 0xffffffff); */
/* /\* unmask rtc interrupts *\/ */
/* OUTREG32(INTC_ICMCR(1), 0x1); */
/* Sleep on-board modules */
jzsoc_do_sleep(sleep_gpio_save);
/* config_irq_wakeup(); */
load_pm_firmware_new(SLEEP_TCSM_SPACE);
sleep_param = (struct sleep_param *)SLEEP_TCSM_SPACE;
sleep_param->post_resume_pc = (unsigned int)restore_goto;
sleep_param->uart_id = 2;
memcpy(&s_reg.ddr_training_space,(void*)0x80000000,sizeof(s_reg.ddr_training_space));
s_reg.opcr = INREG32(CPM_OPCR);
s_reg.lcr = INREG32(CPM_LCR);
/*
* set OPCR.
*/
val = s_reg.opcr;
#if 0
val &= ~((1 << 25) | (1 << 22) | (0xfff << 8) | (1 << 7) | (1 << 6) | (1 << 4) | (1 << 3) | (1 << 2));
val |= (1 << 31) | (1 << 30) | (1 << 25) | (1 << 23) | (0xfff << 8) | (1 << 4) | (1 << 3) | (1 << 2);
#else
val &= ~((1 << 31)|(1 << 30)| (1 << 25) | (1 << 22) | (0xfff << 8) | (1 << 7) | (1 << 6) | (1 << 4) | (1 << 3) | (1 << 2));
val |= (1 << 25) | (1 << 23) | (0xfff << 8) | (1 << 3) | (1 << 2) | (1 << 4);
#endif
REG_CPM_OPCR = val;
*(volatile unsigned int *)0xb000003c &= ~(1 << 31);
*(volatile unsigned int *)0xb000003c |= (1 << 20);
val = s_reg.lcr;
val &= ~3;
//val |= 1;
val |= 0xff << 8; /* power stable time */
REG_CPM_LCR = val;
// *(volatile unsigned int *)0xb0000010 &= ~(1 << 8);
// *(volatile unsigned int *)0xb0000014 &= ~(1 << 7);
printf("clkgr = 0x%08x\n", *(volatile unsigned int *)0xb0000020);
printf("opcr = 0x%08x\n", *(volatile unsigned int *)0xb0000024);
printf("otg phy = 0x%08x\n", *(volatile unsigned int *)0xb0000048);
printf("otg = 0x%08x\n", *(volatile unsigned int *)0xb0000050);
mb();
save_goto((unsigned int)sleep_param->pm_core_enter);
mb();
memcpy((void*)0x80000000,&s_reg.ddr_training_space,sizeof(s_reg.ddr_training_space));
dma_cache_wback_inv(0x80000000,sizeof(s_reg.ddr_training_space));
OUTREG32(CPM_LCR, s_reg.lcr);
OUTREG32(CPM_OPCR, s_reg.opcr);
spin_unlock_irqrestore(sr);
REG_GINT_MASK = gint_mask;
/* Restore interrupts */
//*((volatile unsigned int *)(0xb2000008)) = 1;
REG_CPM_CLKGR0 = clkgr0;
*((volatile unsigned int *)(0xb2000034)) = 1;
OUTREG32(INTC_ICMR(0), icmr0);
OUTREG32(INTC_ICMR(1), icmr1);
/* Resume on-board modules */
jzsoc_do_resume(sleep_gpio_save);
/* Restore Oscillator and Power Control Register */
/* OUTREG32(CPM_OPCR, opcr); */
return 0;
}
#endif
/*
* Function: Keep power for CPU core when reset.
* So that EPC, tcsm and so on can maintain it's status after reset-key pressed.
*/
int reset_keep_power(void)
{
rtc_write_reg(RTC_BASE + RTC_PWRONCR, rtc_read_reg(RTC_BASE + RTC_PWRONCR) & ~(1 << 0));
return 0;
}
int drv_pmu_get_keypwr(void)
{
int level = 0;
#ifdef BOARD_CANNA_OVC
level = gpio_get_value(DET_PWR_PORT,DET_PWR_PIN);
do
{
_delay_ms(5); //ȥ��
}while(level != gpio_get_value(DET_PWR_PORT,DET_PWR_PIN));
#endif
return level;
}
void drv_pmu_power_down(void)
{
#ifdef BOARD_CANNA_OVC
rt_kprintf("SET STB down...\n");
gpio_set_value(EXT_DEV_RST_PORT, EXT_DEV_RST_PIN, 0);
gpio_direction_output(CTR_STB_PORT, CTR_STB_PIN, 0);
_delay_ms(100);
gpio_direction_output(CTR_LOCK_PORT, CTR_LOCK_PIN, 1);
_delay_ms(100);
rt_hw_led_off(LED_GREEN);
rt_hw_led_off(LED_RED);
rt_hw_led_off(LED_BLUE);
#endif
}
void drv_pmu_power_up(void)
{
}
int drv_pmu_init(void)
{
volatile unsigned int lcr, opcr;
/* init opcr and lcr for idle */
lcr = cpm_inl(CPM_LCR);
lcr &= ~(0x3); /* LCR.SLEEP.DS=0'b0,LCR.LPM=1'b00*/
lcr |= 0xff << 8; /* power stable time */
cpm_outl(lcr, CPM_LCR);
opcr = cpm_inl(CPM_OPCR);
opcr |= 0xff << 8; /* EXCLK stable time */
opcr &= ~(1 << 4); /* EXCLK stable time */
cpm_outl(opcr, CPM_OPCR);
return 0;
}
|