blob: 8ba7b06a94d574efa45f29edd4e4a5e82d32015b [file] [log] [blame]
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * 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>
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +00007 * Copyright (C) 2007 Carl-Daniel Hailfinger
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00008 *
Uwe Hermannd1107642007-08-29 17:52:32 +00009 * 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.
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000013 *
Uwe Hermannd1107642007-08-29 17:52:32 +000014 * 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.
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000018 *
Uwe Hermannd1107642007-08-29 17:52:32 +000019 * 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
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000022 */
23
24#include "flash.h"
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000025
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +000026#define MAX_REFLASH_TRIES 0x10
27
Carl-Daniel Hailfingera758f512008-05-14 12:03:06 +000028/* Check one byte for odd parity */
29uint8_t oddparity(uint8_t val)
30{
31 val = (val ^ (val >> 4)) & 0xf;
32 val = (val ^ (val >> 2)) & 0x3;
33 return (val ^ (val >> 1)) & 0x1;
34}
35
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000036void toggle_ready_jedec(chipaddr dst)
Uwe Hermann51582f22007-08-23 10:20:40 +000037{
38 unsigned int i = 0;
39 uint8_t tmp1, tmp2;
40
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000041 tmp1 = chip_readb(dst) & 0x40;
Uwe Hermann51582f22007-08-23 10:20:40 +000042
43 while (i++ < 0xFFFFFFF) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000044 tmp2 = chip_readb(dst) & 0x40;
Uwe Hermann51582f22007-08-23 10:20:40 +000045 if (tmp1 == tmp2) {
46 break;
47 }
48 tmp1 = tmp2;
49 }
50}
51
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000052void data_polling_jedec(chipaddr dst, uint8_t data)
Uwe Hermann51582f22007-08-23 10:20:40 +000053{
54 unsigned int i = 0;
55 uint8_t tmp;
56
57 data &= 0x80;
58
59 while (i++ < 0xFFFFFFF) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000060 tmp = chip_readb(dst) & 0x80;
Uwe Hermann51582f22007-08-23 10:20:40 +000061 if (tmp == data) {
62 break;
63 }
64 }
65}
66
Michael Karcher972cec22009-11-26 14:50:52 +000067void start_program_jedec(chipaddr bios)
Uwe Hermann51582f22007-08-23 10:20:40 +000068{
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000069 chip_writeb(0xAA, bios + 0x5555);
70 chip_writeb(0x55, bios + 0x2AAA);
71 chip_writeb(0xA0, bios + 0x5555);
Uwe Hermann51582f22007-08-23 10:20:40 +000072}
73
Ollie Lho761bf1b2004-03-20 16:46:10 +000074int probe_jedec(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000075{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000076 chipaddr bios = flash->virtual_memory;
Ollie Lho184a4042005-11-26 21:55:36 +000077 uint8_t id1, id2;
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +000078 uint32_t largeid1, largeid2;
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +000079 uint32_t flashcontent1, flashcontent2;
Maciej Pijankac6e11112009-06-03 14:46:22 +000080 int probe_timing_enter, probe_timing_exit;
81
82 if (flash->probe_timing > 0)
83 probe_timing_enter = probe_timing_exit = flash->probe_timing;
84 else if (flash->probe_timing == TIMING_ZERO) { /* No delay. */
85 probe_timing_enter = probe_timing_exit = 0;
86 } else if (flash->probe_timing == TIMING_FIXME) { /* == _IGNORED */
87 printf_debug("Chip lacks correct probe timing information, "
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +000088 "using default 10mS/40uS. ");
Maciej Pijankac6e11112009-06-03 14:46:22 +000089 probe_timing_enter = 10000;
90 probe_timing_exit = 40;
91 } else {
92 printf("Chip has negative value in probe_timing, failing "
93 "without chip access\n");
94 return 0;
95 }
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000096
Ollie Lho761bf1b2004-03-20 16:46:10 +000097 /* Issue JEDEC Product ID Entry command */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000098 chip_writeb(0xAA, bios + 0x5555);
Sean Nelsonc12fc712009-12-17 04:22:40 +000099 if (probe_timing_enter)
100 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000101 chip_writeb(0x55, bios + 0x2AAA);
Sean Nelsonc12fc712009-12-17 04:22:40 +0000102 if (probe_timing_enter)
103 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000104 chip_writeb(0x90, bios + 0x5555);
Sean Nelsonc12fc712009-12-17 04:22:40 +0000105 if (probe_timing_enter)
106 programmer_delay(probe_timing_enter);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000107
Ollie Lho761bf1b2004-03-20 16:46:10 +0000108 /* Read product ID */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000109 id1 = chip_readb(bios);
110 id2 = chip_readb(bios + 0x01);
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000111 largeid1 = id1;
112 largeid2 = id2;
113
114 /* Check if it is a continuation ID, this should be a while loop. */
115 if (id1 == 0x7F) {
116 largeid1 <<= 8;
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000117 id1 = chip_readb(bios + 0x100);
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000118 largeid1 |= id1;
119 }
120 if (id2 == 0x7F) {
121 largeid2 <<= 8;
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000122 id2 = chip_readb(bios + 0x101);
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000123 largeid2 |= id2;
124 }
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000125
Ollie Lho761bf1b2004-03-20 16:46:10 +0000126 /* Issue JEDEC Product ID Exit command */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000127 chip_writeb(0xAA, bios + 0x5555);
Sean Nelsonc12fc712009-12-17 04:22:40 +0000128 if (probe_timing_exit)
129 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000130 chip_writeb(0x55, bios + 0x2AAA);
Sean Nelsonc12fc712009-12-17 04:22:40 +0000131 if (probe_timing_exit)
132 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000133 chip_writeb(0xF0, bios + 0x5555);
Sean Nelsonc12fc712009-12-17 04:22:40 +0000134 if (probe_timing_exit)
135 programmer_delay(probe_timing_exit);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000136
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000137 printf_debug("%s: id1 0x%02x, id2 0x%02x", __func__, largeid1, largeid2);
Carl-Daniel Hailfingera758f512008-05-14 12:03:06 +0000138 if (!oddparity(id1))
139 printf_debug(", id1 parity violation");
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000140
141 /* Read the product ID location again. We should now see normal flash contents. */
142 flashcontent1 = chip_readb(bios);
143 flashcontent2 = chip_readb(bios + 0x01);
144
145 /* Check if it is a continuation ID, this should be a while loop. */
146 if (flashcontent1 == 0x7F) {
147 flashcontent1 <<= 8;
148 flashcontent1 |= chip_readb(bios + 0x100);
149 }
150 if (flashcontent2 == 0x7F) {
151 flashcontent2 <<= 8;
152 flashcontent2 |= chip_readb(bios + 0x101);
153 }
154
155 if (largeid1 == flashcontent1)
156 printf_debug(", id1 is normal flash content");
157 if (largeid2 == flashcontent2)
158 printf_debug(", id2 is normal flash content");
159
Carl-Daniel Hailfingera758f512008-05-14 12:03:06 +0000160 printf_debug("\n");
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000161 if (largeid1 == flash->manufacture_id && largeid2 == flash->model_id)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000162 return 1;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000163
Ollie Lho761bf1b2004-03-20 16:46:10 +0000164 return 0;
Ollie Lho73eca802004-03-19 22:10:07 +0000165}
166
Carl-Daniel Hailfingera06287c2009-09-23 22:01:33 +0000167int erase_sector_jedec(struct flashchip *flash, unsigned int page, unsigned int pagesize)
Ollie Lho73eca802004-03-19 22:10:07 +0000168{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000169 chipaddr bios = flash->virtual_memory;
170
Ollie Lho761bf1b2004-03-20 16:46:10 +0000171 /* Issue the Sector Erase command */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000172 chip_writeb(0xAA, bios + 0x5555);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000173 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000174 chip_writeb(0x55, bios + 0x2AAA);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000175 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000176 chip_writeb(0x80, bios + 0x5555);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000177 programmer_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +0000178
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000179 chip_writeb(0xAA, bios + 0x5555);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000180 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000181 chip_writeb(0x55, bios + 0x2AAA);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000182 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000183 chip_writeb(0x30, bios + page);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000184 programmer_delay(10);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000185
Ollie Lho73eca802004-03-19 22:10:07 +0000186 /* wait for Toggle bit ready */
187 toggle_ready_jedec(bios);
188
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000189 if (check_erased_range(flash, page, pagesize)) {
190 fprintf(stderr,"ERASE FAILED!\n");
191 return -1;
192 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000193 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000194}
Ollie Lho98bea8a2004-12-07 03:15:51 +0000195
Carl-Daniel Hailfingera06287c2009-09-23 22:01:33 +0000196int erase_block_jedec(struct flashchip *flash, unsigned int block, unsigned int blocksize)
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000197{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000198 chipaddr bios = flash->virtual_memory;
199
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000200 /* Issue the Sector Erase command */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000201 chip_writeb(0xAA, bios + 0x5555);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000202 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000203 chip_writeb(0x55, bios + 0x2AAA);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000204 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000205 chip_writeb(0x80, bios + 0x5555);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000206 programmer_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +0000207
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000208 chip_writeb(0xAA, bios + 0x5555);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000209 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000210 chip_writeb(0x55, bios + 0x2AAA);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000211 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000212 chip_writeb(0x50, bios + block);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000213 programmer_delay(10);
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000214
215 /* wait for Toggle bit ready */
216 toggle_ready_jedec(bios);
217
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000218 if (check_erased_range(flash, block, blocksize)) {
219 fprintf(stderr,"ERASE FAILED!\n");
220 return -1;
221 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000222 return 0;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000223}
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000224
Ollie Lho761bf1b2004-03-20 16:46:10 +0000225int erase_chip_jedec(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000226{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000227 int total_size = flash->total_size * 1024;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000228 chipaddr bios = flash->virtual_memory;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000229
Ollie Lho761bf1b2004-03-20 16:46:10 +0000230 /* Issue the JEDEC Chip Erase command */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000231 chip_writeb(0xAA, bios + 0x5555);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000232 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000233 chip_writeb(0x55, bios + 0x2AAA);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000234 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000235 chip_writeb(0x80, bios + 0x5555);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000236 programmer_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +0000237
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000238 chip_writeb(0xAA, bios + 0x5555);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000239 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000240 chip_writeb(0x55, bios + 0x2AAA);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000241 programmer_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000242 chip_writeb(0x10, bios + 0x5555);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000243 programmer_delay(10);
Ollie Lho73eca802004-03-19 22:10:07 +0000244
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000245 toggle_ready_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000246
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000247 if (check_erased_range(flash, 0, total_size)) {
248 fprintf(stderr,"ERASE FAILED!\n");
249 return -1;
250 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000251 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000252}
253
Urja Rannikko0c854c02009-06-25 13:57:31 +0000254int write_page_write_jedec(struct flashchip *flash, uint8_t *src,
255 int start, int page_size)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000256{
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000257 int i, tried = 0, failed;
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000258 uint8_t *s = src;
Urja Rannikko0c854c02009-06-25 13:57:31 +0000259 chipaddr bios = flash->virtual_memory;
260 chipaddr dst = bios + start;
261 chipaddr d = dst;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000262
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000263retry:
Ollie Lho761bf1b2004-03-20 16:46:10 +0000264 /* Issue JEDEC Data Unprotect comand */
Michael Karcher972cec22009-11-26 14:50:52 +0000265 start_program_jedec(bios);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000266
Ollie Lho98bea8a2004-12-07 03:15:51 +0000267 /* transfer data from source to destination */
Carl-Daniel Hailfinger8a8a2262009-11-14 03:48:33 +0000268 for (i = 0; i < page_size; i++) {
Ollie Lho98bea8a2004-12-07 03:15:51 +0000269 /* If the data is 0xFF, don't program it */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000270 if (*src != 0xFF)
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000271 chip_writeb(*src, dst);
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000272 dst++;
273 src++;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000274 }
275
Ollie Lho761bf1b2004-03-20 16:46:10 +0000276 toggle_ready_jedec(dst - 1);
Ollie Lho98bea8a2004-12-07 03:15:51 +0000277
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000278 dst = d;
279 src = s;
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000280 failed = verify_range(flash, src, start, page_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000281
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000282 if (failed && tried++ < MAX_REFLASH_TRIES) {
Carl-Daniel Hailfinger8a8a2262009-11-14 03:48:33 +0000283 fprintf(stderr, "retrying.\n");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000284 goto retry;
285 }
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000286 if (failed) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000287 fprintf(stderr, " page 0x%lx failed!\n",
288 (d - bios) / page_size);
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000289 }
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000290 return failed;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000291}
292
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000293int write_byte_program_jedec(chipaddr bios, uint8_t *src,
294 chipaddr dst)
Ollie Lho070647d2004-03-22 22:19:17 +0000295{
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000296 int tried = 0, failed = 0;
Ollie Lho1b8b6602004-12-08 02:10:33 +0000297
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000298 /* If the data is 0xFF, don't program it and don't complain. */
Ollie Lho070647d2004-03-22 22:19:17 +0000299 if (*src == 0xFF) {
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000300 return 0;
Ollie Lho070647d2004-03-22 22:19:17 +0000301 }
Ollie Lho98bea8a2004-12-07 03:15:51 +0000302
Ollie Lho1b8b6602004-12-08 02:10:33 +0000303retry:
Ollie Lho070647d2004-03-22 22:19:17 +0000304 /* Issue JEDEC Byte Program command */
Michael Karcher972cec22009-11-26 14:50:52 +0000305 start_program_jedec(bios);
Ollie Lho98bea8a2004-12-07 03:15:51 +0000306
307 /* transfer data from source to destination */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000308 chip_writeb(*src, dst);
Ollie Lho070647d2004-03-22 22:19:17 +0000309 toggle_ready_jedec(bios);
Ollie Lho8b8897a2004-03-27 00:18:15 +0000310
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000311 if (chip_readb(dst) != *src && tried++ < MAX_REFLASH_TRIES) {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000312 goto retry;
313 }
Ollie Lho1b8b6602004-12-08 02:10:33 +0000314
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000315 if (tried >= MAX_REFLASH_TRIES)
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000316 failed = 1;
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000317
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000318 return failed;
Ollie Lho070647d2004-03-22 22:19:17 +0000319}
320
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000321int write_sector_jedec(chipaddr bios, uint8_t *src,
322 chipaddr dst, unsigned int page_size)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000323{
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000324 int i, failed = 0;
325 chipaddr olddst;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000326
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000327 olddst = dst;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000328 for (i = 0; i < page_size; i++) {
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000329 if (write_byte_program_jedec(bios, src, dst))
330 failed = 1;
Ollie Lho8b8897a2004-03-27 00:18:15 +0000331 dst++, src++;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000332 }
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000333 if (failed)
334 fprintf(stderr, " writing sector at 0x%lx failed!\n", olddst);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000335
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000336 return failed;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000337}
338
Ollie Lho184a4042005-11-26 21:55:36 +0000339int write_jedec(struct flashchip *flash, uint8_t *buf)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000340{
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000341 int i, failed = 0;
Ollie Lho070647d2004-03-22 22:19:17 +0000342 int total_size = flash->total_size * 1024;
343 int page_size = flash->page_size;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000344
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000345 if (erase_chip_jedec(flash)) {
346 fprintf(stderr,"ERASE FAILED!\n");
347 return -1;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000348 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000349
Uwe Hermanna502dce2007-10-17 23:55:15 +0000350 printf("Programming page: ");
Ollie Lho761bf1b2004-03-20 16:46:10 +0000351 for (i = 0; i < total_size / page_size; i++) {
352 printf("%04d at address: 0x%08x", i, i * page_size);
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000353 if (write_page_write_jedec(flash, buf + i * page_size,
354 i * page_size, page_size))
355 failed = 1;
Ollie Lho070647d2004-03-22 22:19:17 +0000356 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");
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000357 }
358 printf("\n");
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000359
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000360 return failed;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000361}
Michael Karcher1c296ca2009-11-27 17:49:42 +0000362
363int write_jedec_1(struct flashchip *flash, uint8_t * buf)
364{
365 int i;
366 chipaddr bios = flash->virtual_memory;
367 chipaddr dst = bios;
368
369 programmer_delay(10);
370 if (erase_flash(flash)) {
371 fprintf(stderr, "ERASE FAILED!\n");
372 return -1;
373 }
374
375 printf("Programming page: ");
376 for (i = 0; i < flash->total_size; i++) {
377 if ((i & 0x3) == 0)
378 printf("address: 0x%08lx", (unsigned long)i * 1024);
379
380 write_sector_jedec(bios, buf + i * 1024, dst + i * 1024, 1024);
381
382 if ((i & 0x3) == 0)
383 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
384 }
385
386 printf("\n");
387 return 0;
388}