jedec.c 8.77 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 unprotect_jedec(chipaddr bios)
68
{
69 70 71 72 73 74
	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);
75

76
	programmer_delay(200);
77 78
}

79
void protect_jedec(chipaddr bios)
80
{
81 82 83
	chip_writeb(0xAA, bios + 0x5555);
	chip_writeb(0x55, bios + 0x2AAA);
	chip_writeb(0xA0, bios + 0x5555);
84

85
	programmer_delay(200);
86 87
}

88
int probe_jedec(struct flashchip *flash)
89
{
90
	chipaddr bios = flash->virtual_memory;
91
	uint8_t id1, id2;
92
	uint32_t largeid1, largeid2;
93
	uint32_t flashcontent1, flashcontent2;
94 95 96 97 98 99 100 101
	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, "
102
			     "using default 10mS/40uS. ");
103 104 105 106 107 108 109
		probe_timing_enter = 10000;
		probe_timing_exit = 40;
	} else {
		printf("Chip has negative value in probe_timing, failing "
		       "without chip access\n");
		return 0;
	}
110 111

	/* Issue JEDEC Product ID Entry command */
112
	chip_writeb(0xAA, bios + 0x5555);
113
	programmer_delay(10);
114
	chip_writeb(0x55, bios + 0x2AAA);
115
	programmer_delay(10);
116
	chip_writeb(0x90, bios + 0x5555);
117
	programmer_delay(probe_timing_enter);
118 119

	/* Read product ID */
120 121
	id1 = chip_readb(bios);
	id2 = chip_readb(bios + 0x01);
122 123 124 125 126 127
	largeid1 = id1;
	largeid2 = id2;

	/* Check if it is a continuation ID, this should be a while loop. */
	if (id1 == 0x7F) {
		largeid1 <<= 8;
128
		id1 = chip_readb(bios + 0x100);
129 130 131 132
		largeid1 |= id1;
	}
	if (id2 == 0x7F) {
		largeid2 <<= 8;
133
		id2 = chip_readb(bios + 0x101);
134 135
		largeid2 |= id2;
	}
136 137

	/* Issue JEDEC Product ID Exit command */
138
	chip_writeb(0xAA, bios + 0x5555);
139
	programmer_delay(10);
140
	chip_writeb(0x55, bios + 0x2AAA);
141
	programmer_delay(10);
142
	chip_writeb(0xF0, bios + 0x5555);
143
	programmer_delay(probe_timing_exit);
144

145
	printf_debug("%s: id1 0x%02x, id2 0x%02x", __func__, largeid1, largeid2);
146 147
	if (!oddparity(id1))
		printf_debug(", id1 parity violation");
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

	/* 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");

168
	printf_debug("\n");
169
	if (largeid1 == flash->manufacture_id && largeid2 == flash->model_id)
170
		return 1;
171

172
	return 0;
Ollie Lho's avatar
Ollie Lho committed
173
}
174

175
int erase_sector_jedec(struct flashchip *flash, unsigned int page, unsigned int pagesize)
Ollie Lho's avatar
Ollie Lho committed
176
{
177 178
	chipaddr bios = flash->virtual_memory;

179
	/*  Issue the Sector Erase command   */
180
	chip_writeb(0xAA, bios + 0x5555);
181
	programmer_delay(10);
182
	chip_writeb(0x55, bios + 0x2AAA);
183
	programmer_delay(10);
184
	chip_writeb(0x80, bios + 0x5555);
185
	programmer_delay(10);
186

187
	chip_writeb(0xAA, bios + 0x5555);
188
	programmer_delay(10);
189
	chip_writeb(0x55, bios + 0x2AAA);
190
	programmer_delay(10);
191
	chip_writeb(0x30, bios + page);
192
	programmer_delay(10);
193 194 195 196

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

197 198 199 200
	if (check_erased_range(flash, page, pagesize)) {
		fprintf(stderr,"ERASE FAILED!\n");
		return -1;
	}
Uwe Hermann's avatar
Uwe Hermann committed
201
	return 0;
202
}
203

204
int erase_block_jedec(struct flashchip *flash, unsigned int block, unsigned int blocksize)
205
{
206 207
	chipaddr bios = flash->virtual_memory;

208
	/*  Issue the Sector Erase command   */
209
	chip_writeb(0xAA, bios + 0x5555);
210
	programmer_delay(10);
211
	chip_writeb(0x55, bios + 0x2AAA);
212
	programmer_delay(10);
213
	chip_writeb(0x80, bios + 0x5555);
214
	programmer_delay(10);
215

216
	chip_writeb(0xAA, bios + 0x5555);
217
	programmer_delay(10);
218
	chip_writeb(0x55, bios + 0x2AAA);
219
	programmer_delay(10);
220
	chip_writeb(0x50, bios + block);
221
	programmer_delay(10);
222

Ollie Lho's avatar
Ollie Lho committed
223 224
	/* wait for Toggle bit ready         */
	toggle_ready_jedec(bios);
225

226 227 228 229
	if (check_erased_range(flash, block, blocksize)) {
		fprintf(stderr,"ERASE FAILED!\n");
		return -1;
	}
Uwe Hermann's avatar
Uwe Hermann committed
230
	return 0;
231 232
}

233
int erase_chip_jedec(struct flashchip *flash)
234
{
235
	int total_size = flash->total_size * 1024;
236
	chipaddr bios = flash->virtual_memory;
237

238
	/*  Issue the JEDEC Chip Erase command   */
239
	chip_writeb(0xAA, bios + 0x5555);
240
	programmer_delay(10);
241
	chip_writeb(0x55, bios + 0x2AAA);
242
	programmer_delay(10);
243
	chip_writeb(0x80, bios + 0x5555);
244
	programmer_delay(10);
245

246
	chip_writeb(0xAA, bios + 0x5555);
247
	programmer_delay(10);
248
	chip_writeb(0x55, bios + 0x2AAA);
249
	programmer_delay(10);
250
	chip_writeb(0x10, bios + 0x5555);
251
	programmer_delay(10);
Ollie Lho's avatar
Ollie Lho committed
252

253
	toggle_ready_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
254

255 256 257 258
	if (check_erased_range(flash, 0, total_size)) {
		fprintf(stderr,"ERASE FAILED!\n");
		return -1;
	}
Uwe Hermann's avatar
Uwe Hermann committed
259
	return 0;
260 261
}

262 263
int write_page_write_jedec(struct flashchip *flash, uint8_t *src,
			   int start, int page_size)
264
{
265
	int i, tried = 0, ok;
266
	uint8_t *s = src;
267 268 269
	chipaddr bios = flash->virtual_memory;
	chipaddr dst = bios + start;
	chipaddr d = dst;
270

271
retry:
272
	/* Issue JEDEC Data Unprotect comand */
273 274 275
	chip_writeb(0xAA, bios + 0x5555);
	chip_writeb(0x55, bios + 0x2AAA);
	chip_writeb(0xA0, bios + 0x5555);
276

277
	/* transfer data from source to destination */
278
	for (i = 0; i < page_size; i++) {
279
		/* If the data is 0xFF, don't program it */
280
		if (*src != 0xFF)
281
			chip_writeb(*src, dst);
282 283
		dst++;
		src++;
284 285 286
	}

	toggle_ready_jedec(dst - 1);
287

288 289
	dst = d;
	src = s;
290
	ok = !verify_range(flash, src, start, page_size, NULL);
291

292
	if (!ok && tried++ < MAX_REFLASH_TRIES) {
293
		fprintf(stderr, "retrying.\n");
294 295
		goto retry;
	}
296
	if (!ok) {
297 298
		fprintf(stderr, " page 0x%lx failed!\n",
			(d - bios) / page_size);
299
	}
Uwe Hermann's avatar
Uwe Hermann committed
300
	return !ok;
301 302
}

303 304
int write_byte_program_jedec(chipaddr bios, uint8_t *src,
			     chipaddr dst)
Ollie Lho's avatar
Ollie Lho committed
305
{
306
	int tried = 0, ok = 1;
307

308
	/* If the data is 0xFF, don't program it */
Ollie Lho's avatar
Ollie Lho committed
309
	if (*src == 0xFF) {
310
		return -1;
Ollie Lho's avatar
Ollie Lho committed
311
	}
312

313
retry:
Ollie Lho's avatar
Ollie Lho committed
314
	/* Issue JEDEC Byte Program command */
315 316 317
	chip_writeb(0xAA, bios + 0x5555);
	chip_writeb(0x55, bios + 0x2AAA);
	chip_writeb(0xA0, bios + 0x5555);
318 319

	/* transfer data from source to destination */
320
	chip_writeb(*src, dst);
Ollie Lho's avatar
Ollie Lho committed
321
	toggle_ready_jedec(bios);
322

323
	if (chip_readb(dst) != *src && tried++ < MAX_REFLASH_TRIES) {
324 325
		goto retry;
	}
326

327
	if (tried >= MAX_REFLASH_TRIES)
328
		ok = 0;
329

Uwe Hermann's avatar
Uwe Hermann committed
330
	return !ok;
Ollie Lho's avatar
Ollie Lho committed
331 332
}

333 334
int write_sector_jedec(chipaddr bios, uint8_t *src,
		       chipaddr dst, unsigned int page_size)
335 336 337 338
{
	int i;

	for (i = 0; i < page_size; i++) {
339 340
		write_byte_program_jedec(bios, src, dst);
		dst++, src++;
341 342
	}

Uwe Hermann's avatar
Uwe Hermann committed
343
	return 0;
344 345
}

346
int write_jedec(struct flashchip *flash, uint8_t *buf)
347 348
{
	int i;
Ollie Lho's avatar
Ollie Lho committed
349 350
	int total_size = flash->total_size * 1024;
	int page_size = flash->page_size;
351
	chipaddr bios = flash->virtual_memory;
352

353 354 355
	if (erase_chip_jedec(flash)) {
		fprintf(stderr,"ERASE FAILED!\n");
		return -1;
356
	}
357
	
358
	printf("Programming page: ");
359 360
	for (i = 0; i < total_size / page_size; i++) {
		printf("%04d at address: 0x%08x", i, i * page_size);
361 362
		write_page_write_jedec(flash, buf + i * page_size,
				       i * page_size, page_size);
Ollie Lho's avatar
Ollie Lho committed
363
		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");
364 365
	}
	printf("\n");
366
	protect_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
367

Uwe Hermann's avatar
Uwe Hermann committed
368
	return 0;
369
}