forked from flashrom/flashrom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcengines.c
304 lines (252 loc) · 6.35 KB
/
pcengines.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* This file is part of the flashrom project.
*
* Copyright (C) 2016 PC Engines GmbH
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
#include <time.h>
#include "programmer.h"
#include "hwaccess.h"
#include "flash.h"
static void *gpiobase;
static enum part partno;
static int apu1_detect(enum chipbustype dongle);
static int apu2_detect(enum chipbustype dongle);
static void override_flash_bus(enum chipbustype bus);
int apu_open(enum part pn)
{
enum chipbustype dongle = BUS_NONE;
char *param = extract_programmer_param("recovery_dongle");
if (param) {
if (!strcmp(param, "SPI"))
dongle = BUS_SPI;
else if (!strcmp(param, "LPC"))
dongle = BUS_LPC;
else if (!strcmp(param, "FWH"))
dongle = BUS_FWH;
else
return -1;
}
uintptr_t acpibase = 0xfed80000;
partno = pn;
if (partno == pn_apu1)
gpiobase = physmap(NULL, acpibase, 0x1000);
else if (partno == pn_apu2)
gpiobase = physmap(NULL, acpibase, 0x2000);
if (gpiobase == NULL)
return -1;
if (partno == pn_apu1)
return apu1_detect(dongle);
else if (partno == pn_apu2)
return apu2_detect(dongle);
return 0;
}
void apu_close(void)
{
if (partno == pn_apu1)
physunmap(gpiobase, 0x1000);
else if (partno == pn_apu2)
physunmap(gpiobase, 0x2000);
override_flash_bus(BUS_NONE);
}
static void override_flash_bus(enum chipbustype bus)
{
uint8_t reg;
OUTB(0xca, 0xcd6);
reg = INB(0xcd7);
reg &= ~0x07;
switch (bus) {
case BUS_LPC:
reg |= 0x04;
disable_masters(BUS_SPI);
disable_masters(BUS_FWH);
break;
case BUS_FWH:
reg |= 0x06;
disable_masters(BUS_LPC);
disable_masters(BUS_SPI);
break;
case BUS_SPI:
reg |= 0x07;
disable_masters(BUS_LPC);
disable_masters(BUS_FWH);
break;
case BUS_NONE:
default:
break;
}
OUTB(reg, 0xcd7);
}
/* PC Engines APU1 product */
static int apu1_detect(enum chipbustype dongle)
{
int lpc_recovery;
uint32_t *straps;
uint32_t val;
bool EC_PWM2, EC_PWM3;
straps = gpiobase + 0xE00 + 0x80;
/* Unset override to read straps correctly. */
override_flash_bus(BUS_NONE);
val = mmio_readl(straps);
EC_PWM3 = (val & 1);
EC_PWM2 = !(val & 2);
lpc_recovery = (EC_PWM2 == 1) && (EC_PWM3 == 0);
if (lpc_recovery)
msg_cinfo(" (LPC recovery) ");
if (dongle == BUS_SPI)
return -1;
else if (dongle == BUS_LPC)
override_flash_bus(BUS_LPC);
else if (dongle == BUS_FWH)
override_flash_bus(BUS_FWH);
else
override_flash_bus(BUS_SPI);
return 0;
}
static void apu1_set_leds(char d5, char d6, char d7)
{
if (d5) {
mmio_writeb(0x8, gpiobase + 0x1bf);
} else {
mmio_writeb(0xc8, gpiobase + 0x1bf);
}
if (d6) {
mmio_writeb(0x8, gpiobase + 0x1be);
} else {
mmio_writeb(0xc8, gpiobase + 0x1be);
}
if (d7) {
mmio_writeb(0x8, gpiobase + 0x1bd);
} else {
mmio_writeb(0xc8, gpiobase + 0x1bd);
}
}
/* PC Engines APU2 product */
static int apu2_detect(enum chipbustype dongle)
{
int spi_recovery;
uint32_t *gpio;
uint32_t val;
gpio = gpiobase + 0x1500 + 76 * sizeof(u32);
/* Set input with pull-down enable. */
val = mmio_readl(gpio);
mmio_writel((val & ~(0xf << 20)) | (0x2<<20), gpio);
val = mmio_readl(gpio);
spi_recovery = !!(val & (1<<16));
if (spi_recovery)
msg_cinfo(" (SPI recovery) ");
if (!spi_recovery && (dongle == BUS_SPI))
return -1;
if (dongle == BUS_NONE) {
/* Drive SPICTL output low. */
mmio_writel((val & ~(0xf << 20)) | (0x8<<20), gpio);
override_flash_bus(BUS_SPI);
} else if (dongle == BUS_SPI) {
/* Drive SPICTL output high. */
mmio_writel((val & ~(0xf << 20)) | (0xc<<20), gpio);
override_flash_bus(BUS_SPI);
} else if (dongle == BUS_LPC) {
override_flash_bus(BUS_LPC);
} else {
return -1;
}
return 0;
}
static void apu2_set_leds(char d4, char d5, char d6)
{
uint32_t *gpio;
gpio = gpiobase + 0x1500 + 70 * sizeof(u32);
if (d4) {
mmio_writel(mmio_readl(gpio) & ~(1<<22), gpio);
} else {
mmio_writel(mmio_readl(gpio) | (1<<22), gpio);
}
gpio = gpiobase + 0x1500 + 69 * sizeof(u32);
if (d5) {
mmio_writel(mmio_readl(gpio) & ~(1<<22), gpio);
} else {
mmio_writel(mmio_readl(gpio) | (1<<22), gpio);
}
gpio = gpiobase + 0x1500 + 68 * sizeof(u32);
if (d6) {
mmio_writel(mmio_readl(gpio) & ~(1<<22), gpio);
} else {
mmio_writel(mmio_readl(gpio) | (1<<22), gpio);
}
}
static char s_wr, s_rd, s_act;
static void led_set(char act, char rd, char wr)
{
s_act = act;
s_rd = rd;
s_wr = wr;
if (partno == pn_apu1)
apu1_set_leds(s_wr, s_rd, s_act);
else if (partno == pn_apu2)
apu2_set_leds(s_wr, s_rd, s_act);
}
static void led_toggle(char act, char rd, char wr)
{
if (act)
s_act = !s_act;
if (rd)
s_rd = !s_rd;
if (wr)
s_wr = !s_wr;
if (partno == pn_apu1)
apu1_set_leds(s_wr, s_rd, s_act);
else if (partno == pn_apu2)
apu2_set_leds(s_wr, s_rd, s_act);
}
static void led_update(enum phase cmd)
{
static enum phase last_cmd;
if (cmd == ph_success)
led_set(0, 0, 1);
else if (cmd == ph_error)
led_set(1, 1, 1);
else {
if (cmd != last_cmd) {
last_cmd = cmd;
led_set(0, 0, 0);
}
led_toggle(1, cmd == ph_read, (cmd == ph_erase) || (cmd == ph_write));
}
}
static struct timespec next_led_toggle;
void progress_bar_init(void)
{
clock_gettime(CLOCK_MONOTONIC, &next_led_toggle);
}
void progress_bar(enum phase cmd, const uintptr_t pos, const uintptr_t max)
{
struct timespec current;
clock_gettime(CLOCK_MONOTONIC, ¤t);
if ((current.tv_sec < next_led_toggle.tv_sec) ||
((current.tv_sec == next_led_toggle.tv_sec) &&
(current.tv_nsec < next_led_toggle.tv_nsec)))
return;
next_led_toggle = current;
next_led_toggle.tv_nsec += 250*1000*1000;
next_led_toggle.tv_sec += (next_led_toggle.tv_nsec / (1000*1000*1000));
next_led_toggle.tv_nsec %= 1000*1000*1000;
led_update(cmd);
}
void progress_bar_exit(int err)
{
led_update((err < 0) ? ph_error : ph_success);
}