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 void toggle_ready_jedec(volatile uint8_t *dst)
37 while (i++ < 0xFFFFFFF) {
46 void data_polling_jedec(volatile uint8_t *dst, uint8_t data)
53 while (i++ < 0xFFFFFFF) {
61 void unprotect_jedec(volatile uint8_t *bios)
63 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
64 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
65 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
66 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
67 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
68 *(volatile uint8_t *)(bios + 0x5555) = 0x20;
73 void protect_jedec(volatile uint8_t *bios)
75 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
76 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
77 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
82 int probe_jedec(struct flashchip *flash)
84 volatile uint8_t *bios = flash->virtual_memory;
86 uint32_t largeid1, largeid2;
88 /* Issue JEDEC Product ID Entry command */
89 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
91 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
93 *(volatile uint8_t *)(bios + 0x5555) = 0x90;
94 /* Older chips may need up to 100 us to respond. The ATMEL 29C020
95 * needs 10 ms according to the data sheet, but it has been tested
96 * to work reliably with 20 us. Allow a factor of 2 safety margin.
100 /* Read product ID */
101 id1 = *(volatile uint8_t *)bios;
102 id2 = *(volatile uint8_t *)(bios + 0x01);
106 /* Check if it is a continuation ID, this should be a while loop. */
109 id1 = *(volatile uint8_t *)(bios + 0x100);
114 id2 = *(volatile uint8_t *)(bios + 0x101);
118 /* Issue JEDEC Product ID Exit command */
119 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
121 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
123 *(volatile uint8_t *)(bios + 0x5555) = 0xF0;
126 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, largeid1, largeid2);
127 if (largeid1 == flash->manufacture_id && largeid2 == flash->model_id)
133 int erase_sector_jedec(volatile uint8_t *bios, unsigned int page)
135 /* Issue the Sector Erase command */
136 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
138 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
140 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
143 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
145 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
147 *(volatile uint8_t *)(bios + page) = 0x30;
150 /* wait for Toggle bit ready */
151 toggle_ready_jedec(bios);
156 int erase_block_jedec(volatile uint8_t *bios, unsigned int block)
158 /* Issue the Sector Erase command */
159 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
161 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
163 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
166 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
168 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
170 *(volatile uint8_t *)(bios + block) = 0x50;
173 /* wait for Toggle bit ready */
174 toggle_ready_jedec(bios);
179 int erase_chip_jedec(struct flashchip *flash)
181 volatile uint8_t *bios = flash->virtual_memory;
183 /* Issue the JEDEC Chip Erase command */
184 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
186 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
188 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
191 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
193 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
195 *(volatile uint8_t *)(bios + 0x5555) = 0x10;
198 toggle_ready_jedec(bios);
203 int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src,
204 volatile uint8_t *dst, int page_size)
206 int i, tried = 0, start_index = 0, ok;
207 volatile uint8_t *d = dst;
211 /* Issue JEDEC Data Unprotect comand */
212 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
213 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
214 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
216 /* transfer data from source to destination */
217 for (i = start_index; i < page_size; i++) {
218 /* If the data is 0xFF, don't program it */
225 toggle_ready_jedec(dst - 1);
230 for (i = 0; i < page_size; i++) {
239 if (!ok && tried++ < MAX_REFLASH_TRIES) {
244 fprintf(stderr, " page %d failed!\n",
245 (unsigned int)(d - bios) / page_size);
250 int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src,
251 volatile uint8_t *dst)
253 int tried = 0, ok = 1;
255 /* If the data is 0xFF, don't program it */
261 /* Issue JEDEC Byte Program command */
262 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
263 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
264 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
266 /* transfer data from source to destination */
268 toggle_ready_jedec(bios);
270 if (*dst != *src && tried++ < MAX_REFLASH_TRIES) {
274 if (tried >= MAX_REFLASH_TRIES)
280 int write_sector_jedec(volatile uint8_t *bios, uint8_t *src,
281 volatile uint8_t *dst, unsigned int page_size)
285 for (i = 0; i < page_size; i++) {
286 write_byte_program_jedec(bios, src, dst);
293 int write_jedec(struct flashchip *flash, uint8_t *buf)
296 int total_size = flash->total_size * 1024;
297 int page_size = flash->page_size;
298 volatile uint8_t *bios = flash->virtual_memory;
300 erase_chip_jedec(flash);
301 // dumb check if erase was successful.
302 for (i = 0; i < total_size; i++) {
303 if (bios[i] != (uint8_t) 0xff) {
304 printf("ERASE FAILED @%d, val %02x!\n", i, bios[i]);
309 printf("Programming page: ");
310 for (i = 0; i < total_size / page_size; i++) {
311 printf("%04d at address: 0x%08x", i, i * page_size);
312 write_page_write_jedec(bios, buf + i * page_size,
313 bios + i * page_size, page_size);
314 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");