summaryrefslogtreecommitdiffstats
path: root/external/common/arch_flash_arm.c
blob: f65bddc2fca451db07a9837deb7f80285f3d0ad2 (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
/* Copyright 2015 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <assert.h>

#include <ccan/container_of/container_of.h>

#include <libflash/libflash.h>
#include <libflash/file.h>
#include "ast.h"
#include "arch_flash.h"
#include "arch_flash_arm_io.h"

struct flash_chip;

static struct arch_arm_data {
	int fd;
	void *ahb_reg_map;
	void *gpio_ctrl;
	size_t ahb_flash_base;
	size_t ahb_flash_size;
	void *ahb_flash_map;
	int bmc;
	struct flash_chip *flash_chip;
	struct blocklevel_device *init_bl;
} arch_data;

uint32_t ast_ahb_readl(uint32_t offset)
{
	assert(((offset ^ AHB_REGS_BASE) & ~(AHB_REGS_SIZE - 1)) == 0);

	return readl(arch_data.ahb_reg_map + (offset - AHB_REGS_BASE));
}

void ast_ahb_writel(uint32_t val, uint32_t offset)
{
	assert(((offset ^ AHB_REGS_BASE) & ~(AHB_REGS_SIZE - 1)) == 0);

	writel(val, arch_data.ahb_reg_map + (offset - AHB_REGS_BASE));
}

int ast_copy_to_ahb(uint32_t reg, const void *src, uint32_t len)
{
	if (reg < arch_data.ahb_flash_base ||
	    (reg + len) > (arch_data.ahb_flash_base + arch_data.ahb_flash_size))
		return -1;
	reg -= arch_data.ahb_flash_base;

	if (((reg | (unsigned long)src | len) & 3) == 0) {
		while(len > 3) {
			uint32_t val = *(uint32_t *)src;
			writel(val, arch_data.ahb_flash_map + reg);
			src += 4;
			reg += 4;
			len -= 4;
		}
	}

	while(len--) {
		uint8_t val = *(uint8_t *)src;
		writeb(val, arch_data.ahb_flash_map + reg++);
		src += 1;
	}
	return 0;
}

/*
 * GPIO stuff to be replaced by higher level accessors for
 * controlling the flash write lock via sysfs
 */

static inline uint32_t gpio_ctl_readl(uint32_t offset)
{
	return readl(arch_data.gpio_ctrl + offset);
}

static inline void gpio_ctl_writel(uint32_t val, uint32_t offset)
{
	writel(val, arch_data.gpio_ctrl + offset);
}

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

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

	return was_protected;
}

int ast_copy_from_ahb(void *dst, uint32_t reg, uint32_t len)
{
	if (reg < arch_data.ahb_flash_base ||
	    (reg + len) > (arch_data.ahb_flash_base + arch_data.ahb_flash_size))
		return -1;
	reg -= arch_data.ahb_flash_base;

	if (((reg | (unsigned long)dst | len) & 3) == 0) {
		while(len > 3) {
			*(uint32_t *)dst = readl(arch_data.ahb_flash_map + reg);
			dst += 4;
			reg += 4;
			len -= 4;
		}
	}

	while(len--) {
		*(uint8_t *)dst = readb(arch_data.ahb_flash_map + reg++);
		dst += 1;
	}
	return 0;
}

static void close_devs(void)
{
	/*
	 * Old code doesn't do this, not sure why not
	 *
	 * munmap(arch_data.ahb_flash_map, arch_data.ahb_flash_size);
	 * munmap(arch_data.gpio_ctrl, GPIO_CTRL_SIZE);
	 * munmap(arch_data.ahb_reg_map, AHB_REGS_SIZE);
	 * close(arch_data.fd);
	 */
}

static int open_devs(int bmc)
{
	arch_data.fd = open("/dev/mem", O_RDWR | O_SYNC);
	if (arch_data.fd < 0) {
		perror("can't open /dev/mem");
		return -1;
	}

	arch_data.ahb_reg_map = mmap(0, AHB_REGS_SIZE, PROT_READ | PROT_WRITE,
			MAP_SHARED, arch_data.fd, AHB_REGS_BASE);
	if (arch_data.ahb_reg_map == MAP_FAILED) {
		perror("can't map AHB registers /dev/mem");
		return -1;
	}
	arch_data.gpio_ctrl = mmap(0, GPIO_CTRL_SIZE, PROT_READ | PROT_WRITE,
			 MAP_SHARED, arch_data.fd, GPIO_CTRL_BASE);
	if (arch_data.gpio_ctrl == MAP_FAILED) {
		perror("can't map GPIO control via /dev/mem");
		return -1;
	}
	arch_data.ahb_flash_base = bmc ? BMC_FLASH_BASE : PNOR_FLASH_BASE;
	arch_data.ahb_flash_size = bmc ? BMC_FLASH_SIZE : PNOR_FLASH_SIZE;
	arch_data.ahb_flash_map = mmap(0, arch_data.ahb_flash_size, PROT_READ |
			PROT_WRITE, MAP_SHARED, arch_data.fd, arch_data.ahb_flash_base);
	if (arch_data.ahb_flash_map == MAP_FAILED) {
		perror("can't map flash via /dev/mem");
		return -1;
	}
	return 0;
}

static struct blocklevel_device *flash_setup(int bmc)
{
	int rc;
	struct blocklevel_device *bl;
	struct spi_flash_ctrl *fl;

	/* Open and map devices */
	open_devs(bmc);

	/* Create the AST flash controller */
	rc = ast_sf_open(bmc ? AST_SF_TYPE_BMC : AST_SF_TYPE_PNOR, &fl);
	if (rc) {
		fprintf(stderr, "Failed to open controller\n");
		return NULL;
	}

	/* Open flash chip */
	rc = flash_init(fl, &bl, &arch_data.flash_chip);
	if (rc) {
		fprintf(stderr, "Failed to open flash chip\n");
		return NULL;
	}

	return bl;
}

int arch_flash_bmc(struct blocklevel_device *bl, int bmc)
{
	if (!arch_data.init_bl) {
		arch_data.bmc = bmc;
		return 0;
	}

	/* Called with a BL not inited here, bail */
	if (arch_data.init_bl != bl)
		return -1;

	return arch_data.flash_chip ? arch_data.bmc : -1;
}

int arch_flash_erase_chip(struct blocklevel_device *bl)
{
	/* Called with a BL not inited here, bail */
	if (!arch_data.init_bl || arch_data.init_bl != bl)
		return -1;

	if (!arch_data.flash_chip)
		return -1;

	return flash_erase_chip(arch_data.flash_chip);
}

int arch_flash_4b_mode(struct blocklevel_device *bl, int set_4b)
{
	/* Called with a BL not inited here, bail */
	if (!arch_data.init_bl || arch_data.init_bl != bl)
		return -1;

	if (!arch_data.flash_chip)
		return -1;

	return flash_force_4b_mode(arch_data.flash_chip, set_4b);
}

int arch_flash_set_wrprotect(struct blocklevel_device *bl, int set)
{
	/* Called with a BL not inited here, bail */
	if (!arch_data.init_bl || arch_data.init_bl != bl)
		return -1;

	if (!arch_data.flash_chip)
		return -1;

	return set_wrprotect(set);
}

int arch_flash_init(struct blocklevel_device **r_bl, const char *file, bool keep_alive)
{
	struct blocklevel_device *new_bl;

	/* Check we haven't already inited */
	if (arch_data.init_bl)
		return -1;

	if (file) {
		file_init_path(file, NULL, keep_alive, &new_bl);
	} else {
		new_bl = flash_setup(arch_data.bmc);
	}
	if (!new_bl)
		return -1;

	arch_data.init_bl = new_bl;
	*r_bl = new_bl;
	return 0;
}

void arch_flash_close(struct blocklevel_device *bl, const char *file)
{
	if (file) {
		file_exit_close(bl);
	} else {
		flash_exit_close(bl, &ast_sf_close);
		close_devs();
	}
}
OpenPOWER on IntegriCloud