jedec.c 7.38 KB
Newer Older
1
/*
2
 * This file is part of the flashrom project.
3
 *
4 5 6
 * Copyright (C) 2000 Silicon Integrated System Corporation
 * Copyright (C) 2006 Giampiero Giancipoli <gianci@email.it>
 * Copyright (C) 2006 coresystems GmbH <info@coresystems.de>
7
 * Copyright (C) 2007 Carl-Daniel Hailfinger
8
 *
9 10 11 12
 * 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.
13
 *
14 15 16 17
 * 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.
18
 *
19 20 21
 * 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
22 23
 */

Ronald G. Minnich's avatar
Ronald G. Minnich committed
24
#include <stdio.h>
25
#include <stdint.h>
26 27
#include "flash.h"

28 29
#define MAX_REFLASH_TRIES 0x10

30 31 32 33 34 35 36 37
/* Check one byte for odd parity */
uint8_t oddparity(uint8_t val)
{
	val = (val ^ (val >> 4)) & 0xf;
	val = (val ^ (val >> 2)) & 0x3;
	return (val ^ (val >> 1)) & 0x1;
}

38 39 40 41 42
void toggle_ready_jedec(volatile uint8_t *dst)
{
	unsigned int i = 0;
	uint8_t tmp1, tmp2;

43
	tmp1 = chip_readb(dst) & 0x40;
44 45

	while (i++ < 0xFFFFFFF) {
46
		tmp2 = chip_readb(dst) & 0x40;
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
		if (tmp1 == tmp2) {
			break;
		}
		tmp1 = tmp2;
	}
}

void data_polling_jedec(volatile uint8_t *dst, uint8_t data)
{
	unsigned int i = 0;
	uint8_t tmp;

	data &= 0x80;

	while (i++ < 0xFFFFFFF) {
62
		tmp = chip_readb(dst) & 0x80;
63 64 65 66 67 68 69 70
		if (tmp == data) {
			break;
		}
	}
}

void unprotect_jedec(volatile uint8_t *bios)
{
71 72 73 74 75 76
	chip_writeb(0xAA, bios + 0x5555);
	chip_writeb(0x55, bios + 0x2AAA);
	chip_writeb(0x80, bios + 0x5555);
	chip_writeb(0xAA, bios + 0x5555);
	chip_writeb(0x55, bios + 0x2AAA);
	chip_writeb(0x20, bios + 0x5555);
77 78 79 80 81 82

	usleep(200);
}

void protect_jedec(volatile uint8_t *bios)
{
83 84 85
	chip_writeb(0xAA, bios + 0x5555);
	chip_writeb(0x55, bios + 0x2AAA);
	chip_writeb(0xA0, bios + 0x5555);
86 87 88 89

	usleep(200);
}

90
int probe_jedec(struct flashchip *flash)
91
{
92
	volatile uint8_t *bios = flash->virtual_memory;
93
	uint8_t id1, id2;
94
	uint32_t largeid1, largeid2;
95 96

	/* Issue JEDEC Product ID Entry command */
97
	chip_writeb(0xAA, bios + 0x5555);
98
	myusec_delay(10);
99
	chip_writeb(0x55, bios + 0x2AAA);
100
	myusec_delay(10);
101
	chip_writeb(0x90, bios + 0x5555);
102
	/* Older chips may need up to 100 us to respond. The ATMEL 29C020
103
	 * needs 10 ms according to the data sheet.
104
	 */
105
	myusec_delay(10000);
106 107

	/* Read product ID */
108 109
	id1 = chip_readb(bios);
	id2 = chip_readb(bios + 0x01);
110 111 112 113 114 115
	largeid1 = id1;
	largeid2 = id2;

	/* Check if it is a continuation ID, this should be a while loop. */
	if (id1 == 0x7F) {
		largeid1 <<= 8;
116
		id1 = chip_readb(bios + 0x100);
117 118 119 120
		largeid1 |= id1;
	}
	if (id2 == 0x7F) {
		largeid2 <<= 8;
121
		id2 = chip_readb(bios + 0x101);
122 123
		largeid2 |= id2;
	}
124 125

	/* Issue JEDEC Product ID Exit command */
126
	chip_writeb(0xAA, bios + 0x5555);
127
	myusec_delay(10);
128
	chip_writeb(0x55, bios + 0x2AAA);
129
	myusec_delay(10);
130
	chip_writeb(0xF0, bios + 0x5555);
131
	myusec_delay(40);
132

133
	printf_debug("%s: id1 0x%02x, id2 0x%02x", __FUNCTION__, largeid1, largeid2);
134 135 136
	if (!oddparity(id1))
		printf_debug(", id1 parity violation");
	printf_debug("\n");
137
	if (largeid1 == flash->manufacture_id && largeid2 == flash->model_id)
138
		return 1;
139

140
	return 0;
Ollie Lho's avatar
Ollie Lho committed
141
}
142

143
int erase_sector_jedec(volatile uint8_t *bios, unsigned int page)
Ollie Lho's avatar
Ollie Lho committed
144
{
145
	/*  Issue the Sector Erase command   */
146
	chip_writeb(0xAA, bios + 0x5555);
147
	myusec_delay(10);
148
	chip_writeb(0x55, bios + 0x2AAA);
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
149
	myusec_delay(10);
150
	chip_writeb(0x80, bios + 0x5555);
Ollie Lho's avatar
Ollie Lho committed
151
	myusec_delay(10);
152

153
	chip_writeb(0xAA, bios + 0x5555);
Ollie Lho's avatar
Ollie Lho committed
154
	myusec_delay(10);
155
	chip_writeb(0x55, bios + 0x2AAA);
Ollie Lho's avatar
Ollie Lho committed
156
	myusec_delay(10);
157
	chip_writeb(0x30, bios + page);
Ollie Lho's avatar
Ollie Lho committed
158
	myusec_delay(10);
159 160 161 162

	/* wait for Toggle bit ready         */
	toggle_ready_jedec(bios);

Uwe Hermann's avatar
Uwe Hermann committed
163
	return 0;
164
}
165

166
int erase_block_jedec(volatile uint8_t *bios, unsigned int block)
167 168
{
	/*  Issue the Sector Erase command   */
169
	chip_writeb(0xAA, bios + 0x5555);
170
	myusec_delay(10);
171
	chip_writeb(0x55, bios + 0x2AAA);
172
	myusec_delay(10);
173
	chip_writeb(0x80, bios + 0x5555);
174
	myusec_delay(10);
175

176
	chip_writeb(0xAA, bios + 0x5555);
177
	myusec_delay(10);
178
	chip_writeb(0x55, bios + 0x2AAA);
179
	myusec_delay(10);
180
	chip_writeb(0x50, bios + block);
181
	myusec_delay(10);
182

Ollie Lho's avatar
Ollie Lho committed
183 184
	/* wait for Toggle bit ready         */
	toggle_ready_jedec(bios);
185

Uwe Hermann's avatar
Uwe Hermann committed
186
	return 0;
187 188
}

189
int erase_chip_jedec(struct flashchip *flash)
190
{
191
	volatile uint8_t *bios = flash->virtual_memory;
192

193
	/*  Issue the JEDEC Chip Erase command   */
194
	chip_writeb(0xAA, bios + 0x5555);
Ollie Lho's avatar
Ollie Lho committed
195
	myusec_delay(10);
196
	chip_writeb(0x55, bios + 0x2AAA);
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
197
	myusec_delay(10);
198
	chip_writeb(0x80, bios + 0x5555);
Ollie Lho's avatar
Ollie Lho committed
199
	myusec_delay(10);
200

201
	chip_writeb(0xAA, bios + 0x5555);
Ollie Lho's avatar
Ollie Lho committed
202
	myusec_delay(10);
203
	chip_writeb(0x55, bios + 0x2AAA);
Ollie Lho's avatar
Ollie Lho committed
204
	myusec_delay(10);
205
	chip_writeb(0x10, bios + 0x5555);
Ollie Lho's avatar
Ollie Lho committed
206 207
	myusec_delay(10);

208
	toggle_ready_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
209

Uwe Hermann's avatar
Uwe Hermann committed
210
	return 0;
211 212
}

213 214
int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src,
			   volatile uint8_t *dst, int page_size)
215
{
216 217 218
	int i, tried = 0, start_index = 0, ok;
	volatile uint8_t *d = dst;
	uint8_t *s = src;
219

220
retry:
221
	/* Issue JEDEC Data Unprotect comand */
222 223 224
	chip_writeb(0xAA, bios + 0x5555);
	chip_writeb(0x55, bios + 0x2AAA);
	chip_writeb(0xA0, bios + 0x5555);
225

226
	/* transfer data from source to destination */
227
	for (i = start_index; i < page_size; i++) {
228
		/* If the data is 0xFF, don't program it */
229
		if (*src != 0xFF)
230
			chip_writeb(*src, dst);
231 232
		dst++;
		src++;
233 234 235
	}

	toggle_ready_jedec(dst - 1);
236

237 238 239 240
	dst = d;
	src = s;
	ok = 1;
	for (i = 0; i < page_size; i++) {
241
		if (chip_readb(dst) != *src) {
242 243 244 245 246 247
			ok = 0;
			break;
		}
		dst++;
		src++;
	}
248

249 250
	if (!ok && tried++ < MAX_REFLASH_TRIES) {
		start_index = i;
251 252
		goto retry;
	}
253
	if (!ok) {
254 255
		fprintf(stderr, " page %d failed!\n",
			(unsigned int)(d - bios) / page_size);
256
	}
Uwe Hermann's avatar
Uwe Hermann committed
257
	return !ok;
258 259
}

260 261
int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src,
			     volatile uint8_t *dst)
Ollie Lho's avatar
Ollie Lho committed
262
{
263
	int tried = 0, ok = 1;
264

265
	/* If the data is 0xFF, don't program it */
Ollie Lho's avatar
Ollie Lho committed
266
	if (*src == 0xFF) {
267
		return -1;
Ollie Lho's avatar
Ollie Lho committed
268
	}
269

270
retry:
Ollie Lho's avatar
Ollie Lho committed
271
	/* Issue JEDEC Byte Program command */
272 273 274
	chip_writeb(0xAA, bios + 0x5555);
	chip_writeb(0x55, bios + 0x2AAA);
	chip_writeb(0xA0, bios + 0x5555);
275 276

	/* transfer data from source to destination */
277
	chip_writeb(*src, dst);
Ollie Lho's avatar
Ollie Lho committed
278
	toggle_ready_jedec(bios);
279

280
	if (chip_readb(dst) != *src && tried++ < MAX_REFLASH_TRIES) {
281 282
		goto retry;
	}
283

284
	if (tried >= MAX_REFLASH_TRIES)
285
		ok = 0;
286

Uwe Hermann's avatar
Uwe Hermann committed
287
	return !ok;
Ollie Lho's avatar
Ollie Lho committed
288 289
}

290 291
int write_sector_jedec(volatile uint8_t *bios, uint8_t *src,
		       volatile uint8_t *dst, unsigned int page_size)
292 293 294 295
{
	int i;

	for (i = 0; i < page_size; i++) {
296 297
		write_byte_program_jedec(bios, src, dst);
		dst++, src++;
298 299
	}

Uwe Hermann's avatar
Uwe Hermann committed
300
	return 0;
301 302
}

303
int write_jedec(struct flashchip *flash, uint8_t *buf)
304 305
{
	int i;
Ollie Lho's avatar
Ollie Lho committed
306 307
	int total_size = flash->total_size * 1024;
	int page_size = flash->page_size;
308
	volatile uint8_t *bios = flash->virtual_memory;
309

310
	erase_chip_jedec(flash);
311 312 313
	// dumb check if erase was successful.
	for (i = 0; i < total_size; i++) {
		if (bios[i] != (uint8_t) 0xff) {
314
			printf("ERASE FAILED @%d, val %02x!\n", i, bios[i]);
315 316 317
			return -1;
		}
	}
318

319
	printf("Programming page: ");
320 321
	for (i = 0; i < total_size / page_size; i++) {
		printf("%04d at address: 0x%08x", i, i * page_size);
322 323
		write_page_write_jedec(bios, buf + i * page_size,
				       bios + i * page_size, page_size);
Ollie Lho's avatar
Ollie Lho committed
324
		printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
325 326
	}
	printf("\n");
327
	protect_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
328

Uwe Hermann's avatar
Uwe Hermann committed
329
	return 0;
330
}