2 * This file is part of the flashrom project.
4 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2006 Giampiero Giancipoli <gianci@email.it>
6 * Copyright (C) 2006 coresystems GmbH <info@coresystems.de>
7 * Copyright (C) 2007 Carl-Daniel Hailfinger
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #define MAX_REFLASH_TRIES 0x10
30 /* Check one byte for odd parity */
31 uint8_t oddparity(uint8_t val)
33 val = (val ^ (val >> 4)) & 0xf;
34 val = (val ^ (val >> 2)) & 0x3;
35 return (val ^ (val >> 1)) & 0x1;
38 void toggle_ready_jedec(volatile uint8_t *dst)
43 tmp1 = chip_readb(dst) & 0x40;
45 while (i++ < 0xFFFFFFF) {
46 tmp2 = chip_readb(dst) & 0x40;
54 void data_polling_jedec(volatile uint8_t *dst, uint8_t data)
61 while (i++ < 0xFFFFFFF) {
62 tmp = chip_readb(dst) & 0x80;
69 void unprotect_jedec(volatile uint8_t *bios)
71 chip_writeb(0xAA, bios + 0x5555);
72 chip_writeb(0x55, bios + 0x2AAA);
73 chip_writeb(0x80, bios + 0x5555);
74 chip_writeb(0xAA, bios + 0x5555);
75 chip_writeb(0x55, bios + 0x2AAA);
76 chip_writeb(0x20, bios + 0x5555);
81 void protect_jedec(volatile uint8_t *bios)
83 chip_writeb(0xAA, bios + 0x5555);
84 chip_writeb(0x55, bios + 0x2AAA);
85 chip_writeb(0xA0, bios + 0x5555);
90 int probe_jedec(struct flashchip *flash)
92 volatile uint8_t *bios = flash->virtual_memory;
94 uint32_t largeid1, largeid2;
96 /* Issue JEDEC Product ID Entry command */
97 chip_writeb(0xAA, bios + 0x5555);
99 chip_writeb(0x55, bios + 0x2AAA);
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.
107 /* Read product ID */
108 id1 = chip_readb(bios);
109 id2 = chip_readb(bios + 0x01);
113 /* Check if it is a continuation ID, this should be a while loop. */
116 id1 = chip_readb(bios + 0x100);
121 id2 = chip_readb(bios + 0x101);
125 /* Issue JEDEC Product ID Exit command */
126 chip_writeb(0xAA, bios + 0x5555);
128 chip_writeb(0x55, bios + 0x2AAA);
130 chip_writeb(0xF0, bios + 0x5555);
133 printf_debug("%s: id1 0x%02x, id2 0x%02x", __FUNCTION__, largeid1, largeid2);
135 printf_debug(", id1 parity violation");
137 if (largeid1 == flash->manufacture_id && largeid2 == flash->model_id)
143 int erase_sector_jedec(volatile uint8_t *bios, unsigned int page)
145 /* Issue the Sector Erase command */
146 chip_writeb(0xAA, bios + 0x5555);
148 chip_writeb(0x55, bios + 0x2AAA);
150 chip_writeb(0x80, bios + 0x5555);
153 chip_writeb(0xAA, bios + 0x5555);
155 chip_writeb(0x55, bios + 0x2AAA);
157 chip_writeb(0x30, bios + page);
160 /* wait for Toggle bit ready */
161 toggle_ready_jedec(bios);
166 int erase_block_jedec(volatile uint8_t *bios, unsigned int block)
168 /* Issue the Sector Erase command */
169 chip_writeb(0xAA, bios + 0x5555);
171 chip_writeb(0x55, bios + 0x2AAA);
173 chip_writeb(0x80, bios + 0x5555);
176 chip_writeb(0xAA, bios + 0x5555);
178 chip_writeb(0x55, bios + 0x2AAA);
180 chip_writeb(0x50, bios + block);
183 /* wait for Toggle bit ready */
184 toggle_ready_jedec(bios);
189 int erase_chip_jedec(struct flashchip *flash)
191 volatile uint8_t *bios = flash->virtual_memory;
193 /* Issue the JEDEC Chip Erase command */
194 chip_writeb(0xAA, bios + 0x5555);
196 chip_writeb(0x55, bios + 0x2AAA);
198 chip_writeb(0x80, bios + 0x5555);
201 chip_writeb(0xAA, bios + 0x5555);
203 chip_writeb(0x55, bios + 0x2AAA);
205 chip_writeb(0x10, bios + 0x5555);
208 toggle_ready_jedec(bios);
213 int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src,
214 volatile uint8_t *dst, int page_size)
216 int i, tried = 0, start_index = 0, ok;
217 volatile uint8_t *d = dst;
221 /* Issue JEDEC Data Unprotect comand */
222 chip_writeb(0xAA, bios + 0x5555);
223 chip_writeb(0x55, bios + 0x2AAA);
224 chip_writeb(0xA0, bios + 0x5555);
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 */
230 chip_writeb(*src, dst);
235 toggle_ready_jedec(dst - 1);
240 for (i = 0; i < page_size; i++) {
241 if (chip_readb(dst) != *src) {
249 if (!ok && tried++ < MAX_REFLASH_TRIES) {
254 fprintf(stderr, " page %d failed!\n",
255 (unsigned int)(d - bios) / page_size);
260 int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src,
261 volatile uint8_t *dst)
263 int tried = 0, ok = 1;
265 /* If the data is 0xFF, don't program it */
271 /* Issue JEDEC Byte Program command */
272 chip_writeb(0xAA, bios + 0x5555);
273 chip_writeb(0x55, bios + 0x2AAA);
274 chip_writeb(0xA0, bios + 0x5555);
276 /* transfer data from source to destination */
277 chip_writeb(*src, dst);
278 toggle_ready_jedec(bios);
280 if (chip_readb(dst) != *src && tried++ < MAX_REFLASH_TRIES) {
284 if (tried >= MAX_REFLASH_TRIES)
290 int write_sector_jedec(volatile uint8_t *bios, uint8_t *src,
291 volatile uint8_t *dst, unsigned int page_size)
295 for (i = 0; i < page_size; i++) {
296 write_byte_program_jedec(bios, src, dst);
303 int write_jedec(struct flashchip *flash, uint8_t *buf)
306 int total_size = flash->total_size * 1024;
307 int page_size = flash->page_size;
308 volatile uint8_t *bios = flash->virtual_memory;
310 erase_chip_jedec(flash);
311 // dumb check if erase was successful.
312 for (i = 0; i < total_size; i++) {
313 if (bios[i] != (uint8_t) 0xff) {
314 printf("ERASE FAILED @%d, val %02x!\n", i, bios[i]);
319 printf("Programming page: ");
320 for (i = 0; i < total_size / page_size; i++) {
321 printf("%04d at address: 0x%08x", i, i * page_size);
322 write_page_write_jedec(bios, buf + i * page_size,
323 bios + i * page_size, page_size);
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");