jedec.c 9.21 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 24 25
 */

#include "flash.h"

26 27
#define MAX_REFLASH_TRIES 0x10

28 29 30 31 32 33 34 35
/* 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;
}

36
void toggle_ready_jedec(chipaddr dst)
37 38 39 40
{
	unsigned int i = 0;
	uint8_t tmp1, tmp2;

41
	tmp1 = chip_readb(dst) & 0x40;
42 43

	while (i++ < 0xFFFFFFF) {
44
		tmp2 = chip_readb(dst) & 0x40;
45 46 47 48 49 50 51
		if (tmp1 == tmp2) {
			break;
		}
		tmp1 = tmp2;
	}
}

52
void data_polling_jedec(chipaddr dst, uint8_t data)
53 54 55 56 57 58 59
{
	unsigned int i = 0;
	uint8_t tmp;

	data &= 0x80;

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

67
void start_program_jedec(chipaddr bios)
68
{
69 70 71
	chip_writeb(0xAA, bios + 0x5555);
	chip_writeb(0x55, bios + 0x2AAA);
	chip_writeb(0xA0, bios + 0x5555);
72 73
}

74
int probe_jedec(struct flashchip *flash)
75
{
76
	chipaddr bios = flash->virtual_memory;
77
	uint8_t id1, id2;
78
	uint32_t largeid1, largeid2;
79
	uint32_t flashcontent1, flashcontent2;
80 81 82 83 84 85 86 87
	int probe_timing_enter, probe_timing_exit;

	if (flash->probe_timing > 0) 
		probe_timing_enter = probe_timing_exit = flash->probe_timing;
	else if (flash->probe_timing == TIMING_ZERO) { /* No delay. */
		probe_timing_enter = probe_timing_exit = 0;
	} else if (flash->probe_timing == TIMING_FIXME) { /* == _IGNORED */
		printf_debug("Chip lacks correct probe timing information, "
88
			     "using default 10mS/40uS. ");
89 90 91 92 93 94 95
		probe_timing_enter = 10000;
		probe_timing_exit = 40;
	} else {
		printf("Chip has negative value in probe_timing, failing "
		       "without chip access\n");
		return 0;
	}
96 97

	/* Issue JEDEC Product ID Entry command */
98
	chip_writeb(0xAA, bios + 0x5555);
99 100
	if (probe_timing_enter)
		programmer_delay(10);
101
	chip_writeb(0x55, bios + 0x2AAA);
102 103
	if (probe_timing_enter)
		programmer_delay(10);
104
	chip_writeb(0x90, bios + 0x5555);
105 106
	if (probe_timing_enter)
		programmer_delay(probe_timing_enter);
107 108

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

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

	/* Issue JEDEC Product ID Exit command */
127
	chip_writeb(0xAA, bios + 0x5555);
128 129
	if (probe_timing_exit)
		programmer_delay(10);
130
	chip_writeb(0x55, bios + 0x2AAA);
131 132
	if (probe_timing_exit)
		programmer_delay(10);
133
	chip_writeb(0xF0, bios + 0x5555);
134 135
	if (probe_timing_exit)
		programmer_delay(probe_timing_exit);
136

137
	printf_debug("%s: id1 0x%02x, id2 0x%02x", __func__, largeid1, largeid2);
138 139
	if (!oddparity(id1))
		printf_debug(", id1 parity violation");
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

	/* Read the product ID location again. We should now see normal flash contents. */
	flashcontent1 = chip_readb(bios);
	flashcontent2 = chip_readb(bios + 0x01);

	/* Check if it is a continuation ID, this should be a while loop. */
	if (flashcontent1 == 0x7F) {
		flashcontent1 <<= 8;
		flashcontent1 |= chip_readb(bios + 0x100);
	}
	if (flashcontent2 == 0x7F) {
		flashcontent2 <<= 8;
		flashcontent2 |= chip_readb(bios + 0x101);
	}

	if (largeid1 == flashcontent1)
		printf_debug(", id1 is normal flash content");
	if (largeid2 == flashcontent2)
		printf_debug(", id2 is normal flash content");

160
	printf_debug("\n");
161
	if (largeid1 == flash->manufacture_id && largeid2 == flash->model_id)
162
		return 1;
163

164
	return 0;
Ollie Lho's avatar
Ollie Lho committed
165
}
166

167
int erase_sector_jedec(struct flashchip *flash, unsigned int page, unsigned int pagesize)
Ollie Lho's avatar
Ollie Lho committed
168
{
169 170
	chipaddr bios = flash->virtual_memory;

171
	/*  Issue the Sector Erase command   */
172
	chip_writeb(0xAA, bios + 0x5555);
173
	programmer_delay(10);
174
	chip_writeb(0x55, bios + 0x2AAA);
175
	programmer_delay(10);
176
	chip_writeb(0x80, bios + 0x5555);
177
	programmer_delay(10);
178

179
	chip_writeb(0xAA, bios + 0x5555);
180
	programmer_delay(10);
181
	chip_writeb(0x55, bios + 0x2AAA);
182
	programmer_delay(10);
183
	chip_writeb(0x30, bios + page);
184
	programmer_delay(10);
185 186 187 188

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

189 190 191 192
	if (check_erased_range(flash, page, pagesize)) {
		fprintf(stderr,"ERASE FAILED!\n");
		return -1;
	}
Uwe Hermann's avatar
Uwe Hermann committed
193
	return 0;
194
}
195

196
int erase_block_jedec(struct flashchip *flash, unsigned int block, unsigned int blocksize)
197
{
198 199
	chipaddr bios = flash->virtual_memory;

200
	/*  Issue the Sector Erase command   */
201
	chip_writeb(0xAA, bios + 0x5555);
202
	programmer_delay(10);
203
	chip_writeb(0x55, bios + 0x2AAA);
204
	programmer_delay(10);
205
	chip_writeb(0x80, bios + 0x5555);
206
	programmer_delay(10);
207

208
	chip_writeb(0xAA, bios + 0x5555);
209
	programmer_delay(10);
210
	chip_writeb(0x55, bios + 0x2AAA);
211
	programmer_delay(10);
212
	chip_writeb(0x50, bios + block);
213
	programmer_delay(10);
214

Ollie Lho's avatar
Ollie Lho committed
215 216
	/* wait for Toggle bit ready         */
	toggle_ready_jedec(bios);
217

218 219 220 221
	if (check_erased_range(flash, block, blocksize)) {
		fprintf(stderr,"ERASE FAILED!\n");
		return -1;
	}
Uwe Hermann's avatar
Uwe Hermann committed
222
	return 0;
223 224
}

225
int erase_chip_jedec(struct flashchip *flash)
226
{
227
	int total_size = flash->total_size * 1024;
228
	chipaddr bios = flash->virtual_memory;
229

230
	/*  Issue the JEDEC Chip Erase command   */
231
	chip_writeb(0xAA, bios + 0x5555);
232
	programmer_delay(10);
233
	chip_writeb(0x55, bios + 0x2AAA);
234
	programmer_delay(10);
235
	chip_writeb(0x80, bios + 0x5555);
236
	programmer_delay(10);
237

238
	chip_writeb(0xAA, bios + 0x5555);
239
	programmer_delay(10);
240
	chip_writeb(0x55, bios + 0x2AAA);
241
	programmer_delay(10);
242
	chip_writeb(0x10, bios + 0x5555);
243
	programmer_delay(10);
Ollie Lho's avatar
Ollie Lho committed
244

245
	toggle_ready_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
246

247 248 249 250
	if (check_erased_range(flash, 0, total_size)) {
		fprintf(stderr,"ERASE FAILED!\n");
		return -1;
	}
Uwe Hermann's avatar
Uwe Hermann committed
251
	return 0;
252 253
}

254 255
int write_page_write_jedec(struct flashchip *flash, uint8_t *src,
			   int start, int page_size)
256
{
257
	int i, tried = 0, failed;
258
	uint8_t *s = src;
259 260 261
	chipaddr bios = flash->virtual_memory;
	chipaddr dst = bios + start;
	chipaddr d = dst;
262

263
retry:
264
	/* Issue JEDEC Data Unprotect comand */
265
	start_program_jedec(bios);
266

267
	/* transfer data from source to destination */
268
	for (i = 0; i < page_size; i++) {
269
		/* If the data is 0xFF, don't program it */
270
		if (*src != 0xFF)
271
			chip_writeb(*src, dst);
272 273
		dst++;
		src++;
274 275 276
	}

	toggle_ready_jedec(dst - 1);
277

278 279
	dst = d;
	src = s;
280
	failed = verify_range(flash, src, start, page_size, NULL);
281

282
	if (failed && tried++ < MAX_REFLASH_TRIES) {
283
		fprintf(stderr, "retrying.\n");
284 285
		goto retry;
	}
286
	if (failed) {
287 288
		fprintf(stderr, " page 0x%lx failed!\n",
			(d - bios) / page_size);
289
	}
290
	return failed;
291 292
}

293 294
int write_byte_program_jedec(chipaddr bios, uint8_t *src,
			     chipaddr dst)
Ollie Lho's avatar
Ollie Lho committed
295
{
296
	int tried = 0, failed = 0;
297

298
	/* If the data is 0xFF, don't program it and don't complain. */
Ollie Lho's avatar
Ollie Lho committed
299
	if (*src == 0xFF) {
300
		return 0;
Ollie Lho's avatar
Ollie Lho committed
301
	}
302

303
retry:
Ollie Lho's avatar
Ollie Lho committed
304
	/* Issue JEDEC Byte Program command */
305
	start_program_jedec(bios);
306 307

	/* transfer data from source to destination */
308
	chip_writeb(*src, dst);
Ollie Lho's avatar
Ollie Lho committed
309
	toggle_ready_jedec(bios);
310

311
	if (chip_readb(dst) != *src && tried++ < MAX_REFLASH_TRIES) {
312 313
		goto retry;
	}
314

315
	if (tried >= MAX_REFLASH_TRIES)
316
		failed = 1;
317

318
	return failed;
Ollie Lho's avatar
Ollie Lho committed
319 320
}

321 322
int write_sector_jedec(chipaddr bios, uint8_t *src,
		       chipaddr dst, unsigned int page_size)
323
{
324 325
	int i, failed = 0;
	chipaddr olddst;
326

327
	olddst = dst;
328
	for (i = 0; i < page_size; i++) {
329 330
		if (write_byte_program_jedec(bios, src, dst))
			failed = 1;
331
		dst++, src++;
332
	}
333 334
	if (failed)
		fprintf(stderr, " writing sector at 0x%lx failed!\n", olddst);
335

336
	return failed;
337 338
}

339
int write_jedec(struct flashchip *flash, uint8_t *buf)
340
{
341
	int i, failed = 0;
Ollie Lho's avatar
Ollie Lho committed
342 343
	int total_size = flash->total_size * 1024;
	int page_size = flash->page_size;
344

345 346 347
	if (erase_chip_jedec(flash)) {
		fprintf(stderr,"ERASE FAILED!\n");
		return -1;
348
	}
349
	
350
	printf("Programming page: ");
351 352
	for (i = 0; i < total_size / page_size; i++) {
		printf("%04d at address: 0x%08x", i, i * page_size);
353 354 355
		if (write_page_write_jedec(flash, buf + i * page_size,
					   i * page_size, page_size))
			failed = 1;
Ollie Lho's avatar
Ollie Lho committed
356
		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");
357 358
	}
	printf("\n");
Ronald G. Minnich's avatar
Ronald G. Minnich committed
359

360
	return failed;
361
}
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

int write_jedec_1(struct flashchip *flash, uint8_t * buf)
{
	int i;
	chipaddr bios = flash->virtual_memory;
	chipaddr dst = bios;

	programmer_delay(10);
	if (erase_flash(flash)) {
		fprintf(stderr, "ERASE FAILED!\n");
		return -1;
	}

	printf("Programming page: ");
	for (i = 0; i < flash->total_size; i++) {
		if ((i & 0x3) == 0)
			printf("address: 0x%08lx", (unsigned long)i * 1024);

                write_sector_jedec(bios, buf + i * 1024, dst + i * 1024, 1024);

		if ((i & 0x3) == 0)
			printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
	}

	printf("\n");
	return 0;
}