jedec.c 7.83 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 77 78

	usleep(200);
}

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 86 87

	usleep(200);
}

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

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

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

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

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

132
	printf_debug("%s: id1 0x%02x, id2 0x%02x", __FUNCTION__, largeid1, largeid2);
133 134
	if (!oddparity(id1))
		printf_debug(", id1 parity violation");
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

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

155
	printf_debug("\n");
156
	if (largeid1 == flash->manufacture_id && largeid2 == flash->model_id)
157
		return 1;
158

159
	return 0;
Ollie Lho's avatar
Ollie Lho committed
160
}
161

162
int erase_sector_jedec(chipaddr bios, unsigned int page)
Ollie Lho's avatar
Ollie Lho committed
163
{
164
	/*  Issue the Sector Erase command   */
165
	chip_writeb(0xAA, bios + 0x5555);
166
	myusec_delay(10);
167
	chip_writeb(0x55, bios + 0x2AAA);
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
168
	myusec_delay(10);
169
	chip_writeb(0x80, bios + 0x5555);
Ollie Lho's avatar
Ollie Lho committed
170
	myusec_delay(10);
171

172
	chip_writeb(0xAA, bios + 0x5555);
Ollie Lho's avatar
Ollie Lho committed
173
	myusec_delay(10);
174
	chip_writeb(0x55, bios + 0x2AAA);
Ollie Lho's avatar
Ollie Lho committed
175
	myusec_delay(10);
176
	chip_writeb(0x30, bios + page);
Ollie Lho's avatar
Ollie Lho committed
177
	myusec_delay(10);
178 179 180 181

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

Uwe Hermann's avatar
Uwe Hermann committed
182
	return 0;
183
}
184

185
int erase_block_jedec(chipaddr bios, unsigned int block)
186 187
{
	/*  Issue the Sector Erase command   */
188
	chip_writeb(0xAA, bios + 0x5555);
189
	myusec_delay(10);
190
	chip_writeb(0x55, bios + 0x2AAA);
191
	myusec_delay(10);
192
	chip_writeb(0x80, bios + 0x5555);
193
	myusec_delay(10);
194

195
	chip_writeb(0xAA, bios + 0x5555);
196
	myusec_delay(10);
197
	chip_writeb(0x55, bios + 0x2AAA);
198
	myusec_delay(10);
199
	chip_writeb(0x50, bios + block);
200
	myusec_delay(10);
201

Ollie Lho's avatar
Ollie Lho committed
202 203
	/* wait for Toggle bit ready         */
	toggle_ready_jedec(bios);
204

Uwe Hermann's avatar
Uwe Hermann committed
205
	return 0;
206 207
}

208
int erase_chip_jedec(struct flashchip *flash)
209
{
210
	chipaddr bios = flash->virtual_memory;
211

212
	/*  Issue the JEDEC Chip Erase command   */
213
	chip_writeb(0xAA, bios + 0x5555);
Ollie Lho's avatar
Ollie Lho committed
214
	myusec_delay(10);
215
	chip_writeb(0x55, bios + 0x2AAA);
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
216
	myusec_delay(10);
217
	chip_writeb(0x80, bios + 0x5555);
Ollie Lho's avatar
Ollie Lho committed
218
	myusec_delay(10);
219

220
	chip_writeb(0xAA, bios + 0x5555);
Ollie Lho's avatar
Ollie Lho committed
221
	myusec_delay(10);
222
	chip_writeb(0x55, bios + 0x2AAA);
Ollie Lho's avatar
Ollie Lho committed
223
	myusec_delay(10);
224
	chip_writeb(0x10, bios + 0x5555);
Ollie Lho's avatar
Ollie Lho committed
225 226
	myusec_delay(10);

227
	toggle_ready_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
228

Uwe Hermann's avatar
Uwe Hermann committed
229
	return 0;
230 231
}

232 233
int write_page_write_jedec(chipaddr bios, uint8_t *src,
			   chipaddr dst, int page_size)
234
{
235
	int i, tried = 0, start_index = 0, ok;
236
	chipaddr d = dst;
237
	uint8_t *s = src;
238

239
retry:
240
	/* Issue JEDEC Data Unprotect comand */
241 242 243
	chip_writeb(0xAA, bios + 0x5555);
	chip_writeb(0x55, bios + 0x2AAA);
	chip_writeb(0xA0, bios + 0x5555);
244

245
	/* transfer data from source to destination */
246
	for (i = start_index; i < page_size; i++) {
247
		/* If the data is 0xFF, don't program it */
248
		if (*src != 0xFF)
249
			chip_writeb(*src, dst);
250 251
		dst++;
		src++;
252 253 254
	}

	toggle_ready_jedec(dst - 1);
255

256 257 258 259
	dst = d;
	src = s;
	ok = 1;
	for (i = 0; i < page_size; i++) {
260
		if (chip_readb(dst) != *src) {
261 262 263 264 265 266
			ok = 0;
			break;
		}
		dst++;
		src++;
	}
267

268 269
	if (!ok && tried++ < MAX_REFLASH_TRIES) {
		start_index = i;
270 271
		goto retry;
	}
272
	if (!ok) {
273 274
		fprintf(stderr, " page 0x%lx failed!\n",
			(d - bios) / page_size);
275
	}
Uwe Hermann's avatar
Uwe Hermann committed
276
	return !ok;
277 278
}

279 280
int write_byte_program_jedec(chipaddr bios, uint8_t *src,
			     chipaddr dst)
Ollie Lho's avatar
Ollie Lho committed
281
{
282
	int tried = 0, ok = 1;
283

284
	/* If the data is 0xFF, don't program it */
Ollie Lho's avatar
Ollie Lho committed
285
	if (*src == 0xFF) {
286
		return -1;
Ollie Lho's avatar
Ollie Lho committed
287
	}
288

289
retry:
Ollie Lho's avatar
Ollie Lho committed
290
	/* Issue JEDEC Byte Program command */
291 292 293
	chip_writeb(0xAA, bios + 0x5555);
	chip_writeb(0x55, bios + 0x2AAA);
	chip_writeb(0xA0, bios + 0x5555);
294 295

	/* transfer data from source to destination */
296
	chip_writeb(*src, dst);
Ollie Lho's avatar
Ollie Lho committed
297
	toggle_ready_jedec(bios);
298

299
	if (chip_readb(dst) != *src && tried++ < MAX_REFLASH_TRIES) {
300 301
		goto retry;
	}
302

303
	if (tried >= MAX_REFLASH_TRIES)
304
		ok = 0;
305

Uwe Hermann's avatar
Uwe Hermann committed
306
	return !ok;
Ollie Lho's avatar
Ollie Lho committed
307 308
}

309 310
int write_sector_jedec(chipaddr bios, uint8_t *src,
		       chipaddr dst, unsigned int page_size)
311 312 313 314
{
	int i;

	for (i = 0; i < page_size; i++) {
315 316
		write_byte_program_jedec(bios, src, dst);
		dst++, src++;
317 318
	}

Uwe Hermann's avatar
Uwe Hermann committed
319
	return 0;
320 321
}

322
int write_jedec(struct flashchip *flash, uint8_t *buf)
323 324
{
	int i;
Ollie Lho's avatar
Ollie Lho committed
325 326
	int total_size = flash->total_size * 1024;
	int page_size = flash->page_size;
327
	chipaddr bios = flash->virtual_memory;
328

329
	erase_chip_jedec(flash);
330 331
	// dumb check if erase was successful.
	for (i = 0; i < total_size; i++) {
332
		if (chip_readb(bios + i) != 0xff) {
333
			printf("ERASE FAILED @%d, val %02x!\n", i, chip_readb(bios + i));
334 335 336
			return -1;
		}
	}
337

338
	printf("Programming page: ");
339 340
	for (i = 0; i < total_size / page_size; i++) {
		printf("%04d at address: 0x%08x", i, i * page_size);
341 342
		write_page_write_jedec(bios, buf + i * page_size,
				       bios + i * page_size, page_size);
Ollie Lho's avatar
Ollie Lho committed
343
		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");
344 345
	}
	printf("\n");
346
	protect_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
347

Uwe Hermann's avatar
Uwe Hermann committed
348
	return 0;
349
}