summaryrefslogtreecommitdiffstats
path: root/op-flasher/pflash/powerpc_io.c
blob: 1aa224beab0276dd1c33f1edb8570e5f4946582d (plain)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#define _GNU_SOURCE /* for strcasestr */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <byteswap.h>
#include <stdint.h>
#include <stdbool.h>
#include <getopt.h>
#include <limits.h>
#include <arpa/inet.h>
#include <assert.h>

#include "io.h"

/* Big endian warning/note:
 *
 * The register accessors return byteswapped data for registers
 */
uint32_t (*ast_ahb_readl)(uint32_t offset);
void (*ast_ahb_writel)(uint32_t val, uint32_t offset);
int (*ast_copy_to_ahb)(uint32_t reg, const void *src, uint32_t len);
int (*ast_copy_from_ahb)(void *dst, uint32_t reg, uint32_t len);

static enum ppc_platform {
	plat_unknown,
	plat_rhesus,
	plat_ast_bmc,
} ppc_platform;

static int lpc_io_fd = -1, lpc_fw_fd = -1;
static uint32_t lpc_old_flash_reg;
static uint32_t ahb_flash_base, ahb_flash_size, lpc_flash_offset;

static void lpc_outb(uint8_t val, uint16_t port)
{
	int rc;

	lseek(lpc_io_fd, port, SEEK_SET);
	rc = write(lpc_io_fd, &val, 1);
	if (rc != 1) {
		perror("Can't write to LPC IO");
		exit(1);
	}
}

static uint8_t lpc_inb(uint16_t port)
{
	uint8_t val;
	int rc;

	lseek(lpc_io_fd, port, SEEK_SET);
	rc = read(lpc_io_fd, &val, 1);
	if (rc != 1) {
		perror("Can't read from LPC IO");
		exit(1);
	}
	return val;
}

int lpc_fw_write32(uint32_t val, uint32_t addr)
{
	int rc;

	/* The value passed in is in big endian always */
	lseek(lpc_fw_fd, addr, SEEK_SET);
	rc = write(lpc_fw_fd, &val, 4);
	if (rc != 4) {
		perror("Can't write to LPC FW");
		exit(1);
	}
	return 0;
}

int lpc_fw_read32(uint32_t *val, uint32_t addr)
{
	int rc;

	lseek(lpc_fw_fd, addr, SEEK_SET);
	rc = read(lpc_fw_fd, val, 4);
	if (rc != 4) {
		perror("Can't read from LPC FW");
		exit(1);
	}
	return 0;
}

static void lpc_sio_outb(uint8_t val, uint8_t reg)
{
	lpc_outb(reg, 0x2e);
	lpc_outb(val, 0x2f);
}

static uint8_t lpc_sio_inb(uint8_t reg)
{
	lpc_outb(reg, 0x2e);
	return lpc_inb(0x2f);
}

static void lpc_ahb_prep(uint32_t reg, uint8_t type)
{
	/* Address */
	lpc_sio_outb((reg >> 24) & 0xff, 0xf0);
	lpc_sio_outb((reg >> 16) & 0xff, 0xf1);
	lpc_sio_outb((reg >>  8) & 0xff, 0xf2);
	lpc_sio_outb((reg      ) & 0xff, 0xf3);

	/* 4 bytes cycle */
	lpc_sio_outb(type, 0xf8);
}

static void lpc_ahb_writel(uint32_t val, uint32_t reg)
{
	lpc_ahb_prep(reg, 2);

	/* Write data */
	lpc_sio_outb(val >> 24, 0xf4);
	lpc_sio_outb(val >> 16, 0xf5);
	lpc_sio_outb(val >>  8, 0xf6);
	lpc_sio_outb(val      , 0xf7);

	/* Trigger */
	lpc_sio_outb(0xcf, 0xfe);
}

static uint32_t lpc_ahb_readl(uint32_t reg)
{
	uint32_t val = 0;

	lpc_ahb_prep(reg, 2);

	/* Trigger */	
	lpc_sio_inb(0xfe);

	/* Read results */
	val = (val << 8) | lpc_sio_inb(0xf4);
	val = (val << 8) | lpc_sio_inb(0xf5);
	val = (val << 8) | lpc_sio_inb(0xf6);
	val = (val << 8) | lpc_sio_inb(0xf7);

	return val;
}

static void lpc_ahb_init(bool bmc_flash)
{
	uint32_t b;

	/* Send SuperIO password */
	lpc_outb(0xa5, 0x2e);
	lpc_outb(0xa5, 0x2e);

	/* Select logical dev d */
	lpc_sio_outb(0x0d, 0x07);

	/* Enable iLPC->AHB */
	lpc_sio_outb(0x01, 0x30);

	/* Save flash base */
	lpc_old_flash_reg = b = lpc_ahb_readl(LPC_CTRL_BASE + 0x88);
	/* Upate flash base */
	if (bmc_flash) {
		ahb_flash_base = BMC_FLASH_BASE;
		ahb_flash_size = BMC_FLASH_SIZE;
	} else {
		ahb_flash_base = PNOR_FLASH_BASE;
		ahb_flash_size = PNOR_FLASH_SIZE;
	}
	lpc_flash_offset = 0x0e000000;
	b = (b & 0x0000ffff) | ahb_flash_base;
	lpc_ahb_writel(b, LPC_CTRL_BASE + 0x88);
	b = lpc_ahb_readl(LPC_CTRL_BASE + 0x88);
}

static int lpc_ast_copy_from_ahb(void *dst, uint32_t reg, uint32_t len)
{
	int rc;

	if (reg < ahb_flash_base ||
	    (reg + len) > (ahb_flash_base + ahb_flash_size))
		return -1;
	reg = (reg - ahb_flash_base) + lpc_flash_offset;

	lseek(lpc_fw_fd, reg, SEEK_SET);
	rc = read(lpc_fw_fd, dst, len);
	if (rc != len) {
		perror("Can't read bulk from LPC FW");
		exit(1);
	}
	return 0;
}

static int lpc_ast_copy_to_ahb(uint32_t reg, const void *src, uint32_t len)
{
	int rc;

	if (reg < ahb_flash_base ||
	    (reg + len) > (ahb_flash_base + ahb_flash_size))
		return -1;
	reg = (reg - ahb_flash_base) + lpc_flash_offset;

	lseek(lpc_fw_fd, reg, SEEK_SET);
	rc = write(lpc_fw_fd, src, len);
	if (rc != len) {
		perror("Can't write bulk from LPC FW");
		exit(1);
	}
	return 0;
}

/*
 * Write protect: TODO use custom IPMI to control lock from BMC
 */
static uint32_t lpc_gpio_ctl_readl(uint32_t offset)
{
	return lpc_ahb_readl(GPIO_CTRL_BASE + offset);
}

static void lpc_gpio_ctl_writel(uint32_t val, uint32_t offset)
{
	lpc_ahb_writel(val, GPIO_CTRL_BASE + offset);
}

bool set_wrprotect(bool protect)
{
	uint32_t reg;
	bool was_protected;

	if (ppc_platform != plat_ast_bmc)
		return false;

	reg = lpc_gpio_ctl_readl(0x20);
	was_protected = !!(reg & 0x00004000);
	if (protect)
		reg |= 0x00004000; /* GPIOF[6] value */
	else
		reg &= ~0x00004000; /* GPIOF[6] value */
	lpc_gpio_ctl_writel(reg, 0x20);
	reg = lpc_gpio_ctl_readl(0x24);
	reg |= 0x00004000; /* GPIOF[6] direction */
	lpc_gpio_ctl_writel(reg, 0x24);

	return was_protected;
}

static void open_lpc(bool bmc_flash)
{      
	lpc_fw_fd = open("/sys/kernel/debug/powerpc/lpc/fw", O_RDWR);
	if (lpc_fw_fd < 0) {
		perror("can't open LPC MEM");
		exit(1);
	}

	if (ppc_platform != plat_ast_bmc)
		return;

	lpc_io_fd = open("/sys/kernel/debug/powerpc/lpc/io", O_RDWR);
	if (lpc_io_fd < 0) {
		perror("can't open LPC IO");
		exit(1);
	}

	ast_ahb_readl = lpc_ahb_readl;
	ast_ahb_writel = lpc_ahb_writel;
	ast_copy_to_ahb = lpc_ast_copy_to_ahb;
	ast_copy_from_ahb = lpc_ast_copy_from_ahb;

	lpc_ahb_init(bmc_flash);
}

void close_devs(void)
{
	if (lpc_io_fd < 0 ||  lpc_fw_fd < 0)
		return;

	if (ppc_platform != plat_ast_bmc)
		return;

	/* Restore flash base */
	lpc_ahb_writel(lpc_old_flash_reg, LPC_CTRL_BASE + 0x88);
}

static void open_pci(bool bmc_flash)
{
	/* XXX */
	fprintf(stderr, "WARNING: PCI access method not implemented !\n");
	fprintf(stderr, "         Use -l or --lpc\n");
	exit(1);
}

static void identify_platform(void)
{
	FILE *cpuinfo;
	char *lptr = NULL;
	size_t lsize = 0;
	bool found = false;

	ppc_platform = plat_unknown;

	cpuinfo = fopen("/proc/cpuinfo", "r");
	if (!cpuinfo) {
		perror("Can't open /proc/cpuinfo");
		exit(1);
	}
	while(!found && getline(&lptr, &lsize, cpuinfo) >= 0) {
		if (!strncmp(lptr, "model", 5)) {
			if (strcasestr(lptr, "rhesus"))
				ppc_platform = plat_rhesus;
			else if (strcasestr(lptr, "palmetto"))
				ppc_platform = plat_ast_bmc;
			found = true;
		}
		free(lptr);
		lptr = NULL;
		lsize = 0;
	}
}

void open_devs(bool use_lpc, bool bmc_flash)
{
	if (ppc_platform == plat_unknown) {
		fprintf(stderr, "Unsupported platform !\n");
		exit(1);
	}

	if (use_lpc)
		open_lpc(bmc_flash);
	else
		open_pci(bmc_flash);
}

void check_platform(bool *has_sfc, bool *has_ast)
{
	identify_platform();

	*has_sfc = ppc_platform == plat_rhesus;
	*has_ast = ppc_platform == plat_ast_bmc;
}
OpenPOWER on IntegriCloud