blob: 2594cf67ee13dd3319615e7b2322866f04dc334e [file] [log] [blame]
rminnich8d3ff912003-10-25 17:01:29 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
rminnich8d3ff912003-10-25 17:01:29 +00003 *
uwe555dd972007-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>
hailfinger428f2012007-12-31 01:49:00 +00007 * Copyright (C) 2007 Carl-Daniel Hailfinger
snelson63133f92010-01-04 17:15:23 +00008 * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com>
rminnich8d3ff912003-10-25 17:01:29 +00009 *
uweb25f1ea2007-08-29 17:52:32 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
rminnich8d3ff912003-10-25 17:01:29 +000014 *
uweb25f1ea2007-08-29 17:52:32 +000015 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
rminnich8d3ff912003-10-25 17:01:29 +000019 *
rminnich8d3ff912003-10-25 17:01:29 +000020 */
21
rminnich8d3ff912003-10-25 17:01:29 +000022#include "flash.h"
rminnich8d3ff912003-10-25 17:01:29 +000023
stepan7abc6322006-11-22 00:29:51 +000024#define MAX_REFLASH_TRIES 0x10
snelson63133f92010-01-04 17:15:23 +000025#define MASK_FULL 0xffff
26#define MASK_2AA 0x7ff
snelsonc6855342010-01-28 23:55:12 +000027#define MASK_AAA 0xfff
stepan7abc6322006-11-22 00:29:51 +000028
hailfinger79cf3672008-05-14 12:03:06 +000029/* Check one byte for odd parity */
30uint8_t oddparity(uint8_t val)
31{
32 val = (val ^ (val >> 4)) & 0xf;
33 val = (val ^ (val >> 2)) & 0x3;
34 return (val ^ (val >> 1)) & 0x1;
35}
36
Souvik Ghoshd75cd672016-06-17 14:21:39 -070037static void toggle_ready_jedec_common(const struct flashctx *flash, chipaddr dst, int delay)
uwedf467892007-08-23 10:20:40 +000038{
39 unsigned int i = 0;
40 uint8_t tmp1, tmp2;
41
Souvik Ghoshd75cd672016-06-17 14:21:39 -070042 tmp1 = chip_readb(flash, dst) & 0x40;
uwedf467892007-08-23 10:20:40 +000043
44 while (i++ < 0xFFFFFFF) {
hailfinger10023012009-12-17 16:20:26 +000045 if (delay)
46 programmer_delay(delay);
Souvik Ghoshd75cd672016-06-17 14:21:39 -070047 tmp2 = chip_readb(flash, dst) & 0x40;
uwedf467892007-08-23 10:20:40 +000048 if (tmp1 == tmp2) {
49 break;
50 }
51 tmp1 = tmp2;
52 }
hailfinger10023012009-12-17 16:20:26 +000053 if (i > 0x100000)
snelsonfc007bb2010-03-24 23:14:32 +000054 msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i);
hailfinger10023012009-12-17 16:20:26 +000055}
56
Souvik Ghoshd75cd672016-06-17 14:21:39 -070057void toggle_ready_jedec(const struct flashctx *flash, chipaddr dst)
hailfinger10023012009-12-17 16:20:26 +000058{
Souvik Ghoshd75cd672016-06-17 14:21:39 -070059 toggle_ready_jedec_common(flash, dst, 0);
hailfinger10023012009-12-17 16:20:26 +000060}
61
62/* Some chips require a minimum delay between toggle bit reads.
63 * The Winbond W39V040C wants 50 ms between reads on sector erase toggle,
64 * but experiments show that 2 ms are already enough. Pick a safety factor
65 * of 4 and use an 8 ms delay.
66 * Given that erase is slow on all chips, it is recommended to use
67 * toggle_ready_jedec_slow in erase functions.
68 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070069static void toggle_ready_jedec_slow(const struct flashctx *flash, chipaddr dst)
hailfinger10023012009-12-17 16:20:26 +000070{
Souvik Ghoshd75cd672016-06-17 14:21:39 -070071 toggle_ready_jedec_common(flash, dst, 8 * 1000);
uwedf467892007-08-23 10:20:40 +000072}
73
Souvik Ghoshd75cd672016-06-17 14:21:39 -070074void data_polling_jedec(const struct flashctx *flash, chipaddr dst, uint8_t data)
uwedf467892007-08-23 10:20:40 +000075{
76 unsigned int i = 0;
77 uint8_t tmp;
78
79 data &= 0x80;
80
81 while (i++ < 0xFFFFFFF) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -070082 tmp = chip_readb(flash, dst) & 0x80;
uwedf467892007-08-23 10:20:40 +000083 if (tmp == data) {
84 break;
85 }
86 }
hailfinger10023012009-12-17 16:20:26 +000087 if (i > 0x100000)
snelsonfc007bb2010-03-24 23:14:32 +000088 msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i);
uwedf467892007-08-23 10:20:40 +000089}
90
Souvik Ghoshd75cd672016-06-17 14:21:39 -070091static unsigned int getaddrmask(struct flashctx *flash)
hailfinger86bf3b52010-10-13 21:49:30 +000092{
Patrick Georgif3fa2992017-02-02 16:24:44 +010093 switch (flash->chip->feature_bits & FEATURE_ADDR_MASK) {
hailfinger86bf3b52010-10-13 21:49:30 +000094 case FEATURE_ADDR_FULL:
95 return MASK_FULL;
96 break;
97 case FEATURE_ADDR_2AA:
98 return MASK_2AA;
99 break;
100 case FEATURE_ADDR_AAA:
101 return MASK_AAA;
102 break;
103 default:
104 msg_cerr("%s called with unknown mask\n", __func__);
105 return 0;
106 break;
107 }
108}
109
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700110static void start_program_jedec_common(struct flashctx *flash, unsigned int mask)
uwedf467892007-08-23 10:20:40 +0000111{
snelson63133f92010-01-04 17:15:23 +0000112 chipaddr bios = flash->virtual_memory;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700113 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
114 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
115 chip_writeb(flash, 0xA0, bios + (0x5555 & mask));
uwedf467892007-08-23 10:20:40 +0000116}
117
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700118static int probe_jedec_common(struct flashctx *flash, unsigned int mask)
rminnich8d3ff912003-10-25 17:01:29 +0000119{
hailfinger82719632009-05-16 21:22:56 +0000120 chipaddr bios = flash->virtual_memory;
ollie6a600992005-11-26 21:55:36 +0000121 uint8_t id1, id2;
hailfinger428f2012007-12-31 01:49:00 +0000122 uint32_t largeid1, largeid2;
hailfinger027d7d92009-05-11 14:40:31 +0000123 uint32_t flashcontent1, flashcontent2;
hailfingerd5b35922009-06-03 14:46:22 +0000124 int probe_timing_enter, probe_timing_exit;
125
Patrick Georgif3fa2992017-02-02 16:24:44 +0100126 if (flash->chip->probe_timing > 0)
127 probe_timing_enter = probe_timing_exit = flash->chip->probe_timing;
128 else if (flash->chip->probe_timing == TIMING_ZERO) { /* No delay. */
hailfingerd5b35922009-06-03 14:46:22 +0000129 probe_timing_enter = probe_timing_exit = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100130 } else if (flash->chip->probe_timing == TIMING_FIXME) { /* == _IGNORED */
snelsonfc007bb2010-03-24 23:14:32 +0000131 msg_cdbg("Chip lacks correct probe timing information, "
hailfinger0cb68252009-07-23 01:33:43 +0000132 "using default 10mS/40uS. ");
hailfingerd5b35922009-06-03 14:46:22 +0000133 probe_timing_enter = 10000;
134 probe_timing_exit = 40;
135 } else {
snelsonfc007bb2010-03-24 23:14:32 +0000136 msg_cerr("Chip has negative value in probe_timing, failing "
hailfingerd5b35922009-06-03 14:46:22 +0000137 "without chip access\n");
138 return 0;
139 }
rminnich8d3ff912003-10-25 17:01:29 +0000140
hailfingerb07dc972010-10-20 21:13:19 +0000141 /* Earlier probes might have been too fast for the chip to enter ID
142 * mode completely. Allow the chip to finish this before seeing a
143 * reset command.
144 */
145 if (probe_timing_enter)
146 programmer_delay(probe_timing_enter);
147 /* Reset chip to a clean slate */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100148 if ((flash->chip->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET)
hailfingerb07dc972010-10-20 21:13:19 +0000149 {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700150 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
hailfingerb07dc972010-10-20 21:13:19 +0000151 if (probe_timing_exit)
152 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700153 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
hailfingerb07dc972010-10-20 21:13:19 +0000154 if (probe_timing_exit)
155 programmer_delay(10);
156 }
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700157 chip_writeb(flash, 0xF0, bios + (0x5555 & mask));
hailfingerb07dc972010-10-20 21:13:19 +0000158 if (probe_timing_exit)
159 programmer_delay(probe_timing_exit);
160
ollie5b621572004-03-20 16:46:10 +0000161 /* Issue JEDEC Product ID Entry command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700162 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000163 if (probe_timing_enter)
164 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700165 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000166 if (probe_timing_enter)
167 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700168 chip_writeb(flash, 0x90, bios + (0x5555 & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000169 if (probe_timing_enter)
170 programmer_delay(probe_timing_enter);
rminnich8d3ff912003-10-25 17:01:29 +0000171
ollie5b621572004-03-20 16:46:10 +0000172 /* Read product ID */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700173 id1 = chip_readb(flash, bios);
174 id2 = chip_readb(flash, bios + 0x01);
hailfinger428f2012007-12-31 01:49:00 +0000175 largeid1 = id1;
176 largeid2 = id2;
177
178 /* Check if it is a continuation ID, this should be a while loop. */
179 if (id1 == 0x7F) {
180 largeid1 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700181 id1 = chip_readb(flash, bios + 0x100);
hailfinger428f2012007-12-31 01:49:00 +0000182 largeid1 |= id1;
183 }
184 if (id2 == 0x7F) {
185 largeid2 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700186 id2 = chip_readb(flash, bios + 0x101);
hailfinger428f2012007-12-31 01:49:00 +0000187 largeid2 |= id2;
188 }
rminnich8d3ff912003-10-25 17:01:29 +0000189
ollie5b621572004-03-20 16:46:10 +0000190 /* Issue JEDEC Product ID Exit command */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100191 if ((flash->chip->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET)
snelson63133f92010-01-04 17:15:23 +0000192 {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700193 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
snelson63133f92010-01-04 17:15:23 +0000194 if (probe_timing_exit)
195 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700196 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
snelson63133f92010-01-04 17:15:23 +0000197 if (probe_timing_exit)
198 programmer_delay(10);
199 }
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700200 chip_writeb(flash, 0xF0, bios + (0x5555 & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000201 if (probe_timing_exit)
202 programmer_delay(probe_timing_exit);
rminnich8d3ff912003-10-25 17:01:29 +0000203
snelsonfc007bb2010-03-24 23:14:32 +0000204 msg_cdbg("%s: id1 0x%02x, id2 0x%02x", __func__, largeid1, largeid2);
hailfinger79cf3672008-05-14 12:03:06 +0000205 if (!oddparity(id1))
snelsonfc007bb2010-03-24 23:14:32 +0000206 msg_cdbg(", id1 parity violation");
hailfinger027d7d92009-05-11 14:40:31 +0000207
208 /* Read the product ID location again. We should now see normal flash contents. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700209 flashcontent1 = chip_readb(flash, bios);
210 flashcontent2 = chip_readb(flash, bios + 0x01);
hailfinger027d7d92009-05-11 14:40:31 +0000211
212 /* Check if it is a continuation ID, this should be a while loop. */
213 if (flashcontent1 == 0x7F) {
214 flashcontent1 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700215 flashcontent1 |= chip_readb(flash, bios + 0x100);
hailfinger027d7d92009-05-11 14:40:31 +0000216 }
217 if (flashcontent2 == 0x7F) {
218 flashcontent2 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700219 flashcontent2 |= chip_readb(flash, bios + 0x101);
hailfinger027d7d92009-05-11 14:40:31 +0000220 }
221
222 if (largeid1 == flashcontent1)
snelsonfc007bb2010-03-24 23:14:32 +0000223 msg_cdbg(", id1 is normal flash content");
hailfinger027d7d92009-05-11 14:40:31 +0000224 if (largeid2 == flashcontent2)
snelsonfc007bb2010-03-24 23:14:32 +0000225 msg_cdbg(", id2 is normal flash content");
hailfinger027d7d92009-05-11 14:40:31 +0000226
snelsonfc007bb2010-03-24 23:14:32 +0000227 msg_cdbg("\n");
Patrick Georgif3fa2992017-02-02 16:24:44 +0100228 if (largeid1 != flash->chip->manufacture_id || largeid2 != flash->chip->model_id)
hailfingerafac00e2010-01-09 02:24:17 +0000229 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000230
Patrick Georgif3fa2992017-02-02 16:24:44 +0100231 if (flash->chip->feature_bits & FEATURE_REGISTERMAP)
snelson63133f92010-01-04 17:15:23 +0000232 map_flash_registers(flash);
233
hailfingerafac00e2010-01-09 02:24:17 +0000234 return 1;
olliea3def632004-03-19 22:10:07 +0000235}
236
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700237static int erase_sector_jedec_common(struct flashctx *flash, unsigned int page,
snelson63133f92010-01-04 17:15:23 +0000238 unsigned int pagesize, unsigned int mask)
olliea3def632004-03-19 22:10:07 +0000239{
hailfinger7af83692009-06-15 17:23:36 +0000240 chipaddr bios = flash->virtual_memory;
mkarcherf7af1b42011-04-15 00:03:37 +0000241 int delay_us = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100242 if(flash->chip->probe_timing != TIMING_ZERO)
mkarcherf7af1b42011-04-15 00:03:37 +0000243 delay_us = 10;
hailfinger7af83692009-06-15 17:23:36 +0000244
ollie5b621572004-03-20 16:46:10 +0000245 /* Issue the Sector Erase command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700246 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000247 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700248 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000249 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700250 chip_writeb(flash, 0x80, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000251 programmer_delay(delay_us);
ollie0eb62d62004-12-08 20:10:01 +0000252
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700253 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000254 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700255 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000256 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700257 chip_writeb(flash, 0x30, bios + page);
mkarcherf7af1b42011-04-15 00:03:37 +0000258 programmer_delay(delay_us);
ollie5b621572004-03-20 16:46:10 +0000259
olliea3def632004-03-19 22:10:07 +0000260 /* wait for Toggle bit ready */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700261 toggle_ready_jedec_slow(flash, bios);
olliea3def632004-03-19 22:10:07 +0000262
hailfingerac8e3182011-06-26 17:04:16 +0000263 /* FIXME: Check the status register for errors. */
uwebe4477b2007-08-23 16:08:21 +0000264 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000265}
olliea4302802004-12-07 03:15:51 +0000266
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700267static int erase_block_jedec_common(struct flashctx *flash, unsigned int block,
snelson63133f92010-01-04 17:15:23 +0000268 unsigned int blocksize, unsigned int mask)
rminnichdfcbaa72004-09-30 16:37:01 +0000269{
hailfinger7af83692009-06-15 17:23:36 +0000270 chipaddr bios = flash->virtual_memory;
mkarcherf7af1b42011-04-15 00:03:37 +0000271 int delay_us = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100272 if(flash->chip->probe_timing != TIMING_ZERO)
mkarcherf7af1b42011-04-15 00:03:37 +0000273 delay_us = 10;
hailfinger7af83692009-06-15 17:23:36 +0000274
rminnichdfcbaa72004-09-30 16:37:01 +0000275 /* Issue the Sector Erase command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700276 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000277 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700278 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000279 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700280 chip_writeb(flash, 0x80, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000281 programmer_delay(delay_us);
ollie0eb62d62004-12-08 20:10:01 +0000282
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700283 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000284 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700285 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000286 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700287 chip_writeb(flash, 0x50, bios + block);
mkarcherf7af1b42011-04-15 00:03:37 +0000288 programmer_delay(delay_us);
rminnichdfcbaa72004-09-30 16:37:01 +0000289
290 /* wait for Toggle bit ready */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700291 toggle_ready_jedec_slow(flash, bios);
rminnichdfcbaa72004-09-30 16:37:01 +0000292
hailfingerac8e3182011-06-26 17:04:16 +0000293 /* FIXME: Check the status register for errors. */
uwebe4477b2007-08-23 16:08:21 +0000294 return 0;
rminnichdfcbaa72004-09-30 16:37:01 +0000295}
rminnich8d3ff912003-10-25 17:01:29 +0000296
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700297static int erase_chip_jedec_common(struct flashctx *flash, unsigned int mask)
rminnich8d3ff912003-10-25 17:01:29 +0000298{
hailfinger82719632009-05-16 21:22:56 +0000299 chipaddr bios = flash->virtual_memory;
mkarcherf7af1b42011-04-15 00:03:37 +0000300 int delay_us = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100301 if(flash->chip->probe_timing != TIMING_ZERO)
mkarcherf7af1b42011-04-15 00:03:37 +0000302 delay_us = 10;
rminnich8d3ff912003-10-25 17:01:29 +0000303
ollie5b621572004-03-20 16:46:10 +0000304 /* Issue the JEDEC Chip Erase command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700305 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000306 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700307 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000308 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700309 chip_writeb(flash, 0x80, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000310 programmer_delay(delay_us);
ollie0eb62d62004-12-08 20:10:01 +0000311
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700312 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000313 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700314 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000315 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700316 chip_writeb(flash, 0x10, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000317 programmer_delay(delay_us);
olliea3def632004-03-19 22:10:07 +0000318
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700319 toggle_ready_jedec_slow(flash, bios);
rminnich8d3ff912003-10-25 17:01:29 +0000320
hailfingerac8e3182011-06-26 17:04:16 +0000321 /* FIXME: Check the status register for errors. */
uwebe4477b2007-08-23 16:08:21 +0000322 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000323}
324
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700325static int write_byte_program_jedec_common(struct flashctx *flash, uint8_t *src,
snelson63133f92010-01-04 17:15:23 +0000326 chipaddr dst, unsigned int mask)
327{
328 int tried = 0, failed = 0;
329 chipaddr bios = flash->virtual_memory;
330
331 /* If the data is 0xFF, don't program it and don't complain. */
332 if (*src == 0xFF) {
333 return 0;
334 }
335
336retry:
337 /* Issue JEDEC Byte Program command */
338 start_program_jedec_common(flash, mask);
339
340 /* transfer data from source to destination */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700341 chip_writeb(flash, *src, dst);
342 toggle_ready_jedec(flash, bios);
snelson63133f92010-01-04 17:15:23 +0000343
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700344 if (chip_readb(flash, dst) != *src && tried++ < MAX_REFLASH_TRIES) {
snelson63133f92010-01-04 17:15:23 +0000345 goto retry;
346 }
347
348 if (tried >= MAX_REFLASH_TRIES)
349 failed = 1;
350
351 return failed;
352}
353
hailfinger71e1bd42010-10-13 22:26:56 +0000354/* chunksize is 1 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700355int write_jedec_1(struct flashctx *flash, uint8_t *src, unsigned int start, unsigned int len)
snelson63133f92010-01-04 17:15:23 +0000356{
357 int i, failed = 0;
hailfingera10a6072010-10-10 14:02:27 +0000358 chipaddr dst = flash->virtual_memory + start;
snelson63133f92010-01-04 17:15:23 +0000359 chipaddr olddst;
stefanctc5eb8a92011-11-23 09:13:48 +0000360 unsigned int mask;
hailfinger86bf3b52010-10-13 21:49:30 +0000361
362 mask = getaddrmask(flash);
snelson63133f92010-01-04 17:15:23 +0000363
364 olddst = dst;
hailfingera10a6072010-10-10 14:02:27 +0000365 for (i = 0; i < len; i++) {
snelson63133f92010-01-04 17:15:23 +0000366 if (write_byte_program_jedec_common(flash, src, dst, mask))
367 failed = 1;
368 dst++, src++;
369 }
370 if (failed)
snelsonfc007bb2010-03-24 23:14:32 +0000371 msg_cerr(" writing sector at 0x%lx failed!\n", olddst);
snelson63133f92010-01-04 17:15:23 +0000372
373 return failed;
374}
375
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700376int write_page_write_jedec_common(struct flashctx *flash, uint8_t *src, unsigned int start, unsigned int page_size)
rminnich8d3ff912003-10-25 17:01:29 +0000377{
hailfingerf2ac27e2009-11-25 16:41:50 +0000378 int i, tried = 0, failed;
stepan7abc6322006-11-22 00:29:51 +0000379 uint8_t *s = src;
hailfingerc2cfc592009-06-25 13:57:31 +0000380 chipaddr bios = flash->virtual_memory;
381 chipaddr dst = bios + start;
382 chipaddr d = dst;
stefanctc5eb8a92011-11-23 09:13:48 +0000383 unsigned int mask;
hailfinger86bf3b52010-10-13 21:49:30 +0000384
385 mask = getaddrmask(flash);
rminnich8d3ff912003-10-25 17:01:29 +0000386
stepan7abc6322006-11-22 00:29:51 +0000387retry:
uwe3a3ab2f2010-03-25 23:18:41 +0000388 /* Issue JEDEC Start Program command */
snelson63133f92010-01-04 17:15:23 +0000389 start_program_jedec_common(flash, mask);
ollie5b621572004-03-20 16:46:10 +0000390
olliea4302802004-12-07 03:15:51 +0000391 /* transfer data from source to destination */
hailfingerfe072472009-11-14 03:48:33 +0000392 for (i = 0; i < page_size; i++) {
olliea4302802004-12-07 03:15:51 +0000393 /* If the data is 0xFF, don't program it */
uwef6641642007-05-09 10:17:44 +0000394 if (*src != 0xFF)
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700395 chip_writeb(flash, *src, dst);
stepan7abc6322006-11-22 00:29:51 +0000396 dst++;
397 src++;
ollie5b621572004-03-20 16:46:10 +0000398 }
399
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700400 toggle_ready_jedec(flash, dst - 1);
olliea4302802004-12-07 03:15:51 +0000401
stepan7abc6322006-11-22 00:29:51 +0000402 dst = d;
403 src = s;
hailfingerf2ac27e2009-11-25 16:41:50 +0000404 failed = verify_range(flash, src, start, page_size, NULL);
uwef6641642007-05-09 10:17:44 +0000405
hailfingerf2ac27e2009-11-25 16:41:50 +0000406 if (failed && tried++ < MAX_REFLASH_TRIES) {
snelsonfc007bb2010-03-24 23:14:32 +0000407 msg_cerr("retrying.\n");
uwef6641642007-05-09 10:17:44 +0000408 goto retry;
409 }
hailfingerf2ac27e2009-11-25 16:41:50 +0000410 if (failed) {
snelsonfc007bb2010-03-24 23:14:32 +0000411 msg_cerr(" page 0x%lx failed!\n",
hailfinger82719632009-05-16 21:22:56 +0000412 (d - bios) / page_size);
stepan7abc6322006-11-22 00:29:51 +0000413 }
hailfingerf2ac27e2009-11-25 16:41:50 +0000414 return failed;
ollie5b621572004-03-20 16:46:10 +0000415}
416
hailfinger71e1bd42010-10-13 22:26:56 +0000417/* chunksize is page_size */
hailfinger86bf3b52010-10-13 21:49:30 +0000418/*
419 * Write a part of the flash chip.
420 * FIXME: Use the chunk code from Michael Karcher instead.
421 * This function is a slightly modified copy of spi_write_chunked.
422 * Each page is written separately in chunks with a maximum size of chunksize.
423 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700424int write_jedec(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger80dea312010-01-09 03:15:50 +0000425{
stefanctc5eb8a92011-11-23 09:13:48 +0000426 unsigned int i, starthere, lenhere;
hailfinger86bf3b52010-10-13 21:49:30 +0000427 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700428 * in struct flashctx to do this properly. All chips using
hailfinger86bf3b52010-10-13 21:49:30 +0000429 * write_jedec have page_size set to max_writechunk_size, so
430 * we're OK for now.
431 */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100432 unsigned int page_size = flash->chip->page_size;
ollie5b621572004-03-20 16:46:10 +0000433
hailfinger86bf3b52010-10-13 21:49:30 +0000434 /* Warning: This loop has a very unusual condition and body.
435 * The loop needs to go through each page with at least one affected
436 * byte. The lowest page number is (start / page_size) since that
437 * division rounds down. The highest page number we want is the page
438 * where the last byte of the range lives. That last byte has the
439 * address (start + len - 1), thus the highest page number is
440 * (start + len - 1) / page_size. Since we want to include that last
441 * page as well, the loop condition uses <=.
442 */
443 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
444 /* Byte position of the first byte in the range in this page. */
445 /* starthere is an offset to the base address of the chip. */
446 starthere = max(start, i * page_size);
447 /* Length of bytes in the range in this page. */
448 lenhere = min(start + len, (i + 1) * page_size) - starthere;
snelsonc6855342010-01-28 23:55:12 +0000449
hailfinger86bf3b52010-10-13 21:49:30 +0000450 if (write_page_write_jedec_common(flash, buf + starthere - start, starthere, lenhere))
451 return 1;
rminnich8d3ff912003-10-25 17:01:29 +0000452 }
rminnich8d3ff912003-10-25 17:01:29 +0000453
hailfinger86bf3b52010-10-13 21:49:30 +0000454 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000455}
hailfingerfff99532009-11-27 17:49:42 +0000456
snelson63133f92010-01-04 17:15:23 +0000457/* erase chip with block_erase() prototype */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700458int erase_chip_block_jedec(struct flashctx *flash, unsigned int addr,
snelson63133f92010-01-04 17:15:23 +0000459 unsigned int blocksize)
460{
stefanctc5eb8a92011-11-23 09:13:48 +0000461 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000462
463 mask = getaddrmask(flash);
Patrick Georgif3fa2992017-02-02 16:24:44 +0100464 if ((addr != 0) || (blocksize != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000465 msg_cerr("%s called with incorrect arguments\n",
snelson63133f92010-01-04 17:15:23 +0000466 __func__);
467 return -1;
468 }
snelsonc6855342010-01-28 23:55:12 +0000469 return erase_chip_jedec_common(flash, mask);
snelson63133f92010-01-04 17:15:23 +0000470}
471
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700472int probe_jedec(struct flashctx *flash)
snelson63133f92010-01-04 17:15:23 +0000473{
stefanctc5eb8a92011-11-23 09:13:48 +0000474 unsigned int mask;
hailfinger80dea312010-01-09 03:15:50 +0000475
476 mask = getaddrmask(flash);
snelsonc6855342010-01-28 23:55:12 +0000477 return probe_jedec_common(flash, mask);
snelson63133f92010-01-04 17:15:23 +0000478}
479
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700480int erase_sector_jedec(struct flashctx *flash, unsigned int page, unsigned int size)
snelson63133f92010-01-04 17:15:23 +0000481{
stefanctc5eb8a92011-11-23 09:13:48 +0000482 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000483
484 mask = getaddrmask(flash);
485 return erase_sector_jedec_common(flash, page, size, mask);
snelson63133f92010-01-04 17:15:23 +0000486}
487
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700488int erase_block_jedec(struct flashctx *flash, unsigned int page, unsigned int size)
snelson63133f92010-01-04 17:15:23 +0000489{
stefanctc5eb8a92011-11-23 09:13:48 +0000490 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000491
492 mask = getaddrmask(flash);
493 return erase_block_jedec_common(flash, page, size, mask);
snelson63133f92010-01-04 17:15:23 +0000494}
495
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700496int erase_chip_jedec(struct flashctx *flash)
snelson63133f92010-01-04 17:15:23 +0000497{
stefanctc5eb8a92011-11-23 09:13:48 +0000498 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000499
500 mask = getaddrmask(flash);
501 return erase_chip_jedec_common(flash, mask);
snelson63133f92010-01-04 17:15:23 +0000502}