-
Notifications
You must be signed in to change notification settings - Fork 257
/
Copy pathp9arch.cpp
231 lines (192 loc) · 4.35 KB
/
p9arch.cpp
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
#include "p9arch.h"
#include <circle/timer.h>
#include <circle/synchronize.h>
#include <circle/machineinfo.h>
#include <circle/interrupt.h>
#include <circle/dmachannel.h>
#include <circle/bcm2712.h>
#include <circle/memio.h>
#include <assert.h>
static struct machine_t machine;
struct machine_t *m = &machine;
#if RASPPI <= 4
static CDMAChannel *s_pDMA;
#endif
void gpiosel (unsigned pin, gpio_mode_t mode)
{
#if RASPPI <= 4
uintptr nSelReg = ARM_GPIO_GPFSEL0 + (pin / 10) * 4;
unsigned nShift = (pin % 10) * 3;
PeripheralEntry ();
u32 nValue = read32 (nSelReg);
nValue &= ~(7 << nShift);
nValue |= mode << nShift;
write32 (nSelReg, nValue);
PeripheralExit ();
#else
uintptr nSelReg = ARM_PINCTRL1_BASE;
unsigned nShift;
if (get_soc_stepping () < SOC_STEPPING_D0)
{
nSelReg += (pin / 8) * 4;
nShift = (pin % 8) * 4;
}
else
{
assert (28 <= pin && pin <= 35);
nSelReg += (pin < 32 ? 2 : 3) * 4;
nShift = ((pin - 24) % 8) * 4;
}
u32 nValue = read32 (nSelReg);
nValue &= ~(0xF << nShift);
nValue |= mode << nShift;
write32 (nSelReg, nValue);
if (mode == Output)
{
assert (pin < 32); // only bank 0 implemented
write32 (ARM_GPIO1_IODIR0, read32 (ARM_GPIO1_IODIR0) & ~BIT (pin));
}
#endif
}
static void gpiopull (unsigned pin, unsigned mode)
{
PeripheralEntry ();
#if RASPPI <= 3
u32 nRegOffset = (pin / 32) * 4;
u32 nRegMask = 1 << (pin % 32);
// See: https://forums.raspberrypi.com/viewtopic.php?t=163352#p1059178
uintptr nClkReg = ARM_GPIO_GPPUDCLK0 + nRegOffset;
write32 (ARM_GPIO_GPPUD, mode);
CTimer::SimpleusDelay (5); // 1us should be enough, but to be sure
write32 (nClkReg, nRegMask);
CTimer::SimpleusDelay (5); // 1us should be enough, but to be sure
write32 (ARM_GPIO_GPPUD, 0);
write32 (nClkReg, 0);
#elif RASPPI == 4
uintptr nModeReg = ARM_GPIO_GPPUPPDN0 + (pin / 16) * 4;
unsigned nShift = (pin % 16) * 2;
u32 nValue = read32 (nModeReg);
nValue &= ~(3 << nShift);
nValue |= mode << nShift;
write32 (nModeReg, nValue);
#else
u32 pad_bit;
uintptr pad_base = ARM_PINCTRL1_BASE;
if (get_soc_stepping () < SOC_STEPPING_D0)
{
// See: https://forums.raspberrypi.com/viewtopic.php?t=362326#p2174597
u32 offset = pin + 112;
pad_bit = (offset % 15) * 2;
pad_base += (uintptr) ((offset / 15) * 4);
}
else
{
assert (28 <= pin && pin <= 35);
pad_bit = ((pin - 18) % 15) * 2;
pad_base += (uintptr) ((pin < 33 ? 5 : 6) * 4);
}
u32 val = read32 (pad_base);
val &= ~(3 << pad_bit);
val |= (mode << pad_bit);
write32 (pad_base, val);
#endif
PeripheralExit ();
}
void gpiopulloff (unsigned pin)
{
gpiopull (pin, 0);
}
void gpiopullup (unsigned pin)
{
#if RASPPI != 4
gpiopull (pin, 2);
#else
gpiopull (pin, 1);
#endif
}
#if RASPPI >= 5
void gpioset (unsigned pin, unsigned val)
{
assert (pin < 32); // only bank 0 implemented
if (val)
{
write32 (ARM_GPIO1_DATA0, read32 (ARM_GPIO1_DATA0) | BIT (pin));
}
else
{
write32 (ARM_GPIO1_DATA0, read32 (ARM_GPIO1_DATA0) & ~BIT (pin));
}
}
unsigned get_soc_stepping (void)
{
return CMachineInfo::Get ()->GetSoCStepping ();
}
#endif
unsigned getclkrate (unsigned clk)
{
#if RASPPI <= 4
return CMachineInfo::Get ()->GetClockRate (clk);
#else
return 54000000;
#endif
}
void delay (unsigned msecs)
{
CTimer::Get ()->MsDelay (msecs);
}
void microdelay (unsigned usecs)
{
CTimer::Get ()->usDelay (usecs);
}
void coherence (void)
{
DataSyncBarrier ();
}
static irqhandler_t *s_pIRQHandler;
static void IRQStub (void *context)
{
(*s_pIRQHandler) (0, context);
}
void intrenable (unsigned irq, irqhandler_t *handler, void *context, unsigned, const char *name)
{
s_pIRQHandler = handler;
CInterruptSystem::Get ()->ConnectIRQ (irq, IRQStub, context);
}
#if RASPPI <= 4
void dmastart (unsigned chan, unsigned dev, unsigned dir, void *from, void *to, size_t len)
{
if (dir == DmaD2M)
{
s_pDMA->SetupIORead (to, (u32) (uintptr) from, len, (TDREQ) dev);
}
else
{
s_pDMA->SetupIOWrite ((u32) (uintptr) to, from, len, (TDREQ) dev);
}
s_pDMA->Start ();
}
int dmawait (unsigned chan)
{
if (!s_pDMA->Wait ())
{
return -1;
}
return 0;
}
#endif
void cachedinvse (void *buf, size_t len)
{
CleanAndInvalidateDataCacheRange ((uintptr) buf, len);
}
static void PeriodicHandler (void)
{
m->ticks++;
}
void p9arch_init(void)
{
#if RASPPI <= 4
s_pDMA = new CDMAChannel (DMA_CHANNEL_NORMAL);
assert (s_pDMA != 0);
#endif
CTimer::Get ()->RegisterPeriodicHandler (PeriodicHandler);
}