-
Notifications
You must be signed in to change notification settings - Fork 0
/
power.c
119 lines (110 loc) · 3.51 KB
/
power.c
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
/*
* Copyright (C) 2018 bzt (bztsrc@github)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#include "gpio.h"
#include "mbox.h"
#include "delays.h"
#define PM_RSTC ((volatile unsigned int *)(MMIO_BASE + 0x0010001c))
#define PM_RSTS ((volatile unsigned int *)(MMIO_BASE + 0x00100020))
#define PM_WDOG ((volatile unsigned int *)(MMIO_BASE + 0x00100024))
#define PM_WDOG_MAGIC 0x5a000000
#define PM_RSTC_FULLRST 0x00000020
/**
* Shutdown the board
*/
void power_off()
{
unsigned long r;
// power off devices one by one
for (r = 0; r < 16; r++)
{
mbox[0] = 8 * 4;
mbox[1] = MBOX_REQUEST;
mbox[2] = MBOX_TAG_SETPOWER; // set power state
mbox[3] = 8;
mbox[4] = 8;
mbox[5] = (unsigned int)r; // device id
mbox[6] = 0; // bit 0: off, bit 1: no wait
mbox[7] = MBOX_TAG_LAST;
mbox_call(MBOX_CH_PROP);
}
// power off gpio pins (but not VCC pins)
*GPFSEL0 = 0;
*GPFSEL1 = 0;
*GPFSEL2 = 0;
*GPFSEL3 = 0;
*GPFSEL4 = 0;
*GPFSEL5 = 0;
*GPPUD = 0;
wait_cycles(150);
*GPPUDCLK0 = 0xffffffff;
*GPPUDCLK1 = 0xffffffff;
wait_cycles(150);
*GPPUDCLK0 = 0;
*GPPUDCLK1 = 0; // flush GPIO setup
// power off the SoC (GPU + CPU)
r = *PM_RSTS;
r &= ~0xfffffaaa;
r |= 0x555; // partition 63 used to indicate halt
*PM_RSTS = PM_WDOG_MAGIC | r;
*PM_WDOG = PM_WDOG_MAGIC | 10;
*PM_RSTC = PM_WDOG_MAGIC | PM_RSTC_FULLRST;
}
/**
* Reboot
*/
void reset(int partition)
{
// the register values are weird, the partition number must be converted
// to hex, then padded with 0's every other bit, so for example, partition
// 63 (which actually means halt), is turned into "111111", which is then
// turned into "0b0101010101" and those bits are set in PM_RSTS
unsigned int partitionBitField = 0;
switch (partition)
{
case 1:
// first partition is "0"
partitionBitField = 0b0;
break;
case 2:
partitionBitField = 0b100;
break;
case 3:
partitionBitField = 0b100;
break;
case 4:
partitionBitField = 0b101;
break;
// etc. etc.
}
unsigned int r;
// trigger a restart by instructing the GPU to boot from partition 0
r = *PM_RSTS;
r &= ~0xfffffaaa;
// set the bit field for which partition to boot from
r |= partitionBitField;
*PM_RSTS = PM_WDOG_MAGIC | r;
*PM_WDOG = PM_WDOG_MAGIC | 10;
*PM_RSTC = PM_WDOG_MAGIC | PM_RSTC_FULLRST;
}