blob: 7ca3260ed3cd3ccd2555d7395d961323225fbcbd [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"
Kangheui Won4974cc12019-10-18 12:59:01 +110023#include <inttypes.h>
rminnich8d3ff912003-10-25 17:01:29 +000024
stepan7abc6322006-11-22 00:29:51 +000025#define MAX_REFLASH_TRIES 0x10
snelson63133f92010-01-04 17:15:23 +000026#define MASK_FULL 0xffff
27#define MASK_2AA 0x7ff
snelsonc6855342010-01-28 23:55:12 +000028#define MASK_AAA 0xfff
stepan7abc6322006-11-22 00:29:51 +000029
hailfinger79cf3672008-05-14 12:03:06 +000030/* Check one byte for odd parity */
31uint8_t oddparity(uint8_t val)
32{
33 val = (val ^ (val >> 4)) & 0xf;
34 val = (val ^ (val >> 2)) & 0x3;
35 return (val ^ (val >> 1)) & 0x1;
36}
37
Souvik Ghoshd75cd672016-06-17 14:21:39 -070038static void toggle_ready_jedec_common(const struct flashctx *flash, chipaddr dst, int delay)
uwedf467892007-08-23 10:20:40 +000039{
40 unsigned int i = 0;
41 uint8_t tmp1, tmp2;
42
Souvik Ghoshd75cd672016-06-17 14:21:39 -070043 tmp1 = chip_readb(flash, dst) & 0x40;
uwedf467892007-08-23 10:20:40 +000044
45 while (i++ < 0xFFFFFFF) {
hailfinger10023012009-12-17 16:20:26 +000046 if (delay)
47 programmer_delay(delay);
Souvik Ghoshd75cd672016-06-17 14:21:39 -070048 tmp2 = chip_readb(flash, dst) & 0x40;
uwedf467892007-08-23 10:20:40 +000049 if (tmp1 == tmp2) {
50 break;
51 }
52 tmp1 = tmp2;
53 }
hailfinger10023012009-12-17 16:20:26 +000054 if (i > 0x100000)
snelsonfc007bb2010-03-24 23:14:32 +000055 msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i);
hailfinger10023012009-12-17 16:20:26 +000056}
57
Souvik Ghoshd75cd672016-06-17 14:21:39 -070058void toggle_ready_jedec(const struct flashctx *flash, chipaddr dst)
hailfinger10023012009-12-17 16:20:26 +000059{
Souvik Ghoshd75cd672016-06-17 14:21:39 -070060 toggle_ready_jedec_common(flash, dst, 0);
hailfinger10023012009-12-17 16:20:26 +000061}
62
63/* Some chips require a minimum delay between toggle bit reads.
64 * The Winbond W39V040C wants 50 ms between reads on sector erase toggle,
65 * but experiments show that 2 ms are already enough. Pick a safety factor
66 * of 4 and use an 8 ms delay.
67 * Given that erase is slow on all chips, it is recommended to use
68 * toggle_ready_jedec_slow in erase functions.
69 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070070static void toggle_ready_jedec_slow(const struct flashctx *flash, chipaddr dst)
hailfinger10023012009-12-17 16:20:26 +000071{
Souvik Ghoshd75cd672016-06-17 14:21:39 -070072 toggle_ready_jedec_common(flash, dst, 8 * 1000);
uwedf467892007-08-23 10:20:40 +000073}
74
Souvik Ghoshd75cd672016-06-17 14:21:39 -070075void data_polling_jedec(const struct flashctx *flash, chipaddr dst, uint8_t data)
uwedf467892007-08-23 10:20:40 +000076{
77 unsigned int i = 0;
78 uint8_t tmp;
79
80 data &= 0x80;
81
82 while (i++ < 0xFFFFFFF) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -070083 tmp = chip_readb(flash, dst) & 0x80;
uwedf467892007-08-23 10:20:40 +000084 if (tmp == data) {
85 break;
86 }
87 }
hailfinger10023012009-12-17 16:20:26 +000088 if (i > 0x100000)
snelsonfc007bb2010-03-24 23:14:32 +000089 msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i);
uwedf467892007-08-23 10:20:40 +000090}
91
Souvik Ghoshd75cd672016-06-17 14:21:39 -070092static unsigned int getaddrmask(struct flashctx *flash)
hailfinger86bf3b52010-10-13 21:49:30 +000093{
Patrick Georgif3fa2992017-02-02 16:24:44 +010094 switch (flash->chip->feature_bits & FEATURE_ADDR_MASK) {
hailfinger86bf3b52010-10-13 21:49:30 +000095 case FEATURE_ADDR_FULL:
96 return MASK_FULL;
97 break;
98 case FEATURE_ADDR_2AA:
99 return MASK_2AA;
100 break;
101 case FEATURE_ADDR_AAA:
102 return MASK_AAA;
103 break;
104 default:
105 msg_cerr("%s called with unknown mask\n", __func__);
106 return 0;
107 break;
108 }
109}
110
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700111static void start_program_jedec_common(struct flashctx *flash, unsigned int mask)
uwedf467892007-08-23 10:20:40 +0000112{
snelson63133f92010-01-04 17:15:23 +0000113 chipaddr bios = flash->virtual_memory;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700114 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
115 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
116 chip_writeb(flash, 0xA0, bios + (0x5555 & mask));
uwedf467892007-08-23 10:20:40 +0000117}
118
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700119static int probe_jedec_common(struct flashctx *flash, unsigned int mask)
rminnich8d3ff912003-10-25 17:01:29 +0000120{
hailfinger82719632009-05-16 21:22:56 +0000121 chipaddr bios = flash->virtual_memory;
ollie6a600992005-11-26 21:55:36 +0000122 uint8_t id1, id2;
hailfinger428f2012007-12-31 01:49:00 +0000123 uint32_t largeid1, largeid2;
hailfinger027d7d92009-05-11 14:40:31 +0000124 uint32_t flashcontent1, flashcontent2;
hailfingerd5b35922009-06-03 14:46:22 +0000125 int probe_timing_enter, probe_timing_exit;
126
Patrick Georgif3fa2992017-02-02 16:24:44 +0100127 if (flash->chip->probe_timing > 0)
128 probe_timing_enter = probe_timing_exit = flash->chip->probe_timing;
129 else if (flash->chip->probe_timing == TIMING_ZERO) { /* No delay. */
hailfingerd5b35922009-06-03 14:46:22 +0000130 probe_timing_enter = probe_timing_exit = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100131 } else if (flash->chip->probe_timing == TIMING_FIXME) { /* == _IGNORED */
snelsonfc007bb2010-03-24 23:14:32 +0000132 msg_cdbg("Chip lacks correct probe timing information, "
hailfinger0cb68252009-07-23 01:33:43 +0000133 "using default 10mS/40uS. ");
hailfingerd5b35922009-06-03 14:46:22 +0000134 probe_timing_enter = 10000;
135 probe_timing_exit = 40;
136 } else {
snelsonfc007bb2010-03-24 23:14:32 +0000137 msg_cerr("Chip has negative value in probe_timing, failing "
hailfingerd5b35922009-06-03 14:46:22 +0000138 "without chip access\n");
139 return 0;
140 }
rminnich8d3ff912003-10-25 17:01:29 +0000141
hailfingerb07dc972010-10-20 21:13:19 +0000142 /* Earlier probes might have been too fast for the chip to enter ID
143 * mode completely. Allow the chip to finish this before seeing a
144 * reset command.
145 */
146 if (probe_timing_enter)
147 programmer_delay(probe_timing_enter);
148 /* Reset chip to a clean slate */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100149 if ((flash->chip->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET)
hailfingerb07dc972010-10-20 21:13:19 +0000150 {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700151 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
hailfingerb07dc972010-10-20 21:13:19 +0000152 if (probe_timing_exit)
153 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700154 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
hailfingerb07dc972010-10-20 21:13:19 +0000155 if (probe_timing_exit)
156 programmer_delay(10);
157 }
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700158 chip_writeb(flash, 0xF0, bios + (0x5555 & mask));
hailfingerb07dc972010-10-20 21:13:19 +0000159 if (probe_timing_exit)
160 programmer_delay(probe_timing_exit);
161
ollie5b621572004-03-20 16:46:10 +0000162 /* Issue JEDEC Product ID Entry command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700163 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000164 if (probe_timing_enter)
165 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700166 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000167 if (probe_timing_enter)
168 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700169 chip_writeb(flash, 0x90, bios + (0x5555 & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000170 if (probe_timing_enter)
171 programmer_delay(probe_timing_enter);
rminnich8d3ff912003-10-25 17:01:29 +0000172
ollie5b621572004-03-20 16:46:10 +0000173 /* Read product ID */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700174 id1 = chip_readb(flash, bios);
175 id2 = chip_readb(flash, bios + 0x01);
hailfinger428f2012007-12-31 01:49:00 +0000176 largeid1 = id1;
177 largeid2 = id2;
178
179 /* Check if it is a continuation ID, this should be a while loop. */
180 if (id1 == 0x7F) {
181 largeid1 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700182 id1 = chip_readb(flash, bios + 0x100);
hailfinger428f2012007-12-31 01:49:00 +0000183 largeid1 |= id1;
184 }
185 if (id2 == 0x7F) {
186 largeid2 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700187 id2 = chip_readb(flash, bios + 0x101);
hailfinger428f2012007-12-31 01:49:00 +0000188 largeid2 |= id2;
189 }
rminnich8d3ff912003-10-25 17:01:29 +0000190
ollie5b621572004-03-20 16:46:10 +0000191 /* Issue JEDEC Product ID Exit command */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100192 if ((flash->chip->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET)
snelson63133f92010-01-04 17:15:23 +0000193 {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700194 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
snelson63133f92010-01-04 17:15:23 +0000195 if (probe_timing_exit)
196 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700197 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
snelson63133f92010-01-04 17:15:23 +0000198 if (probe_timing_exit)
199 programmer_delay(10);
200 }
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700201 chip_writeb(flash, 0xF0, bios + (0x5555 & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000202 if (probe_timing_exit)
203 programmer_delay(probe_timing_exit);
rminnich8d3ff912003-10-25 17:01:29 +0000204
snelsonfc007bb2010-03-24 23:14:32 +0000205 msg_cdbg("%s: id1 0x%02x, id2 0x%02x", __func__, largeid1, largeid2);
hailfinger79cf3672008-05-14 12:03:06 +0000206 if (!oddparity(id1))
snelsonfc007bb2010-03-24 23:14:32 +0000207 msg_cdbg(", id1 parity violation");
hailfinger027d7d92009-05-11 14:40:31 +0000208
209 /* Read the product ID location again. We should now see normal flash contents. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700210 flashcontent1 = chip_readb(flash, bios);
211 flashcontent2 = chip_readb(flash, bios + 0x01);
hailfinger027d7d92009-05-11 14:40:31 +0000212
213 /* Check if it is a continuation ID, this should be a while loop. */
214 if (flashcontent1 == 0x7F) {
215 flashcontent1 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700216 flashcontent1 |= chip_readb(flash, bios + 0x100);
hailfinger027d7d92009-05-11 14:40:31 +0000217 }
218 if (flashcontent2 == 0x7F) {
219 flashcontent2 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700220 flashcontent2 |= chip_readb(flash, bios + 0x101);
hailfinger027d7d92009-05-11 14:40:31 +0000221 }
222
223 if (largeid1 == flashcontent1)
snelsonfc007bb2010-03-24 23:14:32 +0000224 msg_cdbg(", id1 is normal flash content");
hailfinger027d7d92009-05-11 14:40:31 +0000225 if (largeid2 == flashcontent2)
snelsonfc007bb2010-03-24 23:14:32 +0000226 msg_cdbg(", id2 is normal flash content");
hailfinger027d7d92009-05-11 14:40:31 +0000227
snelsonfc007bb2010-03-24 23:14:32 +0000228 msg_cdbg("\n");
Patrick Georgif3fa2992017-02-02 16:24:44 +0100229 if (largeid1 != flash->chip->manufacture_id || largeid2 != flash->chip->model_id)
hailfingerafac00e2010-01-09 02:24:17 +0000230 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000231
Patrick Georgif3fa2992017-02-02 16:24:44 +0100232 if (flash->chip->feature_bits & FEATURE_REGISTERMAP)
snelson63133f92010-01-04 17:15:23 +0000233 map_flash_registers(flash);
234
hailfingerafac00e2010-01-09 02:24:17 +0000235 return 1;
olliea3def632004-03-19 22:10:07 +0000236}
237
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700238static int erase_sector_jedec_common(struct flashctx *flash, unsigned int page,
snelson63133f92010-01-04 17:15:23 +0000239 unsigned int pagesize, unsigned int mask)
olliea3def632004-03-19 22:10:07 +0000240{
hailfinger7af83692009-06-15 17:23:36 +0000241 chipaddr bios = flash->virtual_memory;
mkarcherf7af1b42011-04-15 00:03:37 +0000242 int delay_us = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100243 if(flash->chip->probe_timing != TIMING_ZERO)
mkarcherf7af1b42011-04-15 00:03:37 +0000244 delay_us = 10;
hailfinger7af83692009-06-15 17:23:36 +0000245
ollie5b621572004-03-20 16:46:10 +0000246 /* Issue the Sector Erase command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700247 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000248 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700249 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000250 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700251 chip_writeb(flash, 0x80, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000252 programmer_delay(delay_us);
ollie0eb62d62004-12-08 20:10:01 +0000253
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700254 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000255 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700256 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000257 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700258 chip_writeb(flash, 0x30, bios + page);
mkarcherf7af1b42011-04-15 00:03:37 +0000259 programmer_delay(delay_us);
ollie5b621572004-03-20 16:46:10 +0000260
olliea3def632004-03-19 22:10:07 +0000261 /* wait for Toggle bit ready */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700262 toggle_ready_jedec_slow(flash, bios);
olliea3def632004-03-19 22:10:07 +0000263
hailfingerac8e3182011-06-26 17:04:16 +0000264 /* FIXME: Check the status register for errors. */
uwebe4477b2007-08-23 16:08:21 +0000265 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000266}
olliea4302802004-12-07 03:15:51 +0000267
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700268static int erase_block_jedec_common(struct flashctx *flash, unsigned int block,
snelson63133f92010-01-04 17:15:23 +0000269 unsigned int blocksize, unsigned int mask)
rminnichdfcbaa72004-09-30 16:37:01 +0000270{
hailfinger7af83692009-06-15 17:23:36 +0000271 chipaddr bios = flash->virtual_memory;
mkarcherf7af1b42011-04-15 00:03:37 +0000272 int delay_us = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100273 if(flash->chip->probe_timing != TIMING_ZERO)
mkarcherf7af1b42011-04-15 00:03:37 +0000274 delay_us = 10;
hailfinger7af83692009-06-15 17:23:36 +0000275
rminnichdfcbaa72004-09-30 16:37:01 +0000276 /* Issue the Sector Erase command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700277 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000278 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700279 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000280 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700281 chip_writeb(flash, 0x80, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000282 programmer_delay(delay_us);
ollie0eb62d62004-12-08 20:10:01 +0000283
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700284 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000285 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700286 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000287 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700288 chip_writeb(flash, 0x50, bios + block);
mkarcherf7af1b42011-04-15 00:03:37 +0000289 programmer_delay(delay_us);
rminnichdfcbaa72004-09-30 16:37:01 +0000290
291 /* wait for Toggle bit ready */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700292 toggle_ready_jedec_slow(flash, bios);
rminnichdfcbaa72004-09-30 16:37:01 +0000293
hailfingerac8e3182011-06-26 17:04:16 +0000294 /* FIXME: Check the status register for errors. */
uwebe4477b2007-08-23 16:08:21 +0000295 return 0;
rminnichdfcbaa72004-09-30 16:37:01 +0000296}
rminnich8d3ff912003-10-25 17:01:29 +0000297
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700298static int erase_chip_jedec_common(struct flashctx *flash, unsigned int mask)
rminnich8d3ff912003-10-25 17:01:29 +0000299{
hailfinger82719632009-05-16 21:22:56 +0000300 chipaddr bios = flash->virtual_memory;
mkarcherf7af1b42011-04-15 00:03:37 +0000301 int delay_us = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100302 if(flash->chip->probe_timing != TIMING_ZERO)
mkarcherf7af1b42011-04-15 00:03:37 +0000303 delay_us = 10;
rminnich8d3ff912003-10-25 17:01:29 +0000304
ollie5b621572004-03-20 16:46:10 +0000305 /* Issue the JEDEC Chip Erase command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700306 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000307 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700308 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000309 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700310 chip_writeb(flash, 0x80, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000311 programmer_delay(delay_us);
ollie0eb62d62004-12-08 20:10:01 +0000312
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700313 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000314 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700315 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000316 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700317 chip_writeb(flash, 0x10, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000318 programmer_delay(delay_us);
olliea3def632004-03-19 22:10:07 +0000319
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700320 toggle_ready_jedec_slow(flash, bios);
rminnich8d3ff912003-10-25 17:01:29 +0000321
hailfingerac8e3182011-06-26 17:04:16 +0000322 /* FIXME: Check the status register for errors. */
uwebe4477b2007-08-23 16:08:21 +0000323 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000324}
325
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700326static int write_byte_program_jedec_common(struct flashctx *flash, uint8_t *src,
snelson63133f92010-01-04 17:15:23 +0000327 chipaddr dst, unsigned int mask)
328{
329 int tried = 0, failed = 0;
330 chipaddr bios = flash->virtual_memory;
331
332 /* If the data is 0xFF, don't program it and don't complain. */
333 if (*src == 0xFF) {
334 return 0;
335 }
336
337retry:
338 /* Issue JEDEC Byte Program command */
339 start_program_jedec_common(flash, mask);
340
341 /* transfer data from source to destination */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700342 chip_writeb(flash, *src, dst);
343 toggle_ready_jedec(flash, bios);
snelson63133f92010-01-04 17:15:23 +0000344
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700345 if (chip_readb(flash, dst) != *src && tried++ < MAX_REFLASH_TRIES) {
snelson63133f92010-01-04 17:15:23 +0000346 goto retry;
347 }
348
349 if (tried >= MAX_REFLASH_TRIES)
350 failed = 1;
351
352 return failed;
353}
354
hailfinger71e1bd42010-10-13 22:26:56 +0000355/* chunksize is 1 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700356int write_jedec_1(struct flashctx *flash, uint8_t *src, unsigned int start, unsigned int len)
snelson63133f92010-01-04 17:15:23 +0000357{
358 int i, failed = 0;
hailfingera10a6072010-10-10 14:02:27 +0000359 chipaddr dst = flash->virtual_memory + start;
snelson63133f92010-01-04 17:15:23 +0000360 chipaddr olddst;
stefanctc5eb8a92011-11-23 09:13:48 +0000361 unsigned int mask;
hailfinger86bf3b52010-10-13 21:49:30 +0000362
363 mask = getaddrmask(flash);
snelson63133f92010-01-04 17:15:23 +0000364
365 olddst = dst;
hailfingera10a6072010-10-10 14:02:27 +0000366 for (i = 0; i < len; i++) {
snelson63133f92010-01-04 17:15:23 +0000367 if (write_byte_program_jedec_common(flash, src, dst, mask))
368 failed = 1;
369 dst++, src++;
370 }
371 if (failed)
Kangheui Won4974cc12019-10-18 12:59:01 +1100372 msg_cerr(" writing sector at 0x%" PRIxPTR " failed!\n", olddst);
snelson63133f92010-01-04 17:15:23 +0000373
374 return failed;
375}
376
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700377int write_page_write_jedec_common(struct flashctx *flash, uint8_t *src, unsigned int start, unsigned int page_size)
rminnich8d3ff912003-10-25 17:01:29 +0000378{
hailfingerf2ac27e2009-11-25 16:41:50 +0000379 int i, tried = 0, failed;
stepan7abc6322006-11-22 00:29:51 +0000380 uint8_t *s = src;
hailfingerc2cfc592009-06-25 13:57:31 +0000381 chipaddr bios = flash->virtual_memory;
382 chipaddr dst = bios + start;
383 chipaddr d = dst;
stefanctc5eb8a92011-11-23 09:13:48 +0000384 unsigned int mask;
hailfinger86bf3b52010-10-13 21:49:30 +0000385
386 mask = getaddrmask(flash);
rminnich8d3ff912003-10-25 17:01:29 +0000387
stepan7abc6322006-11-22 00:29:51 +0000388retry:
uwe3a3ab2f2010-03-25 23:18:41 +0000389 /* Issue JEDEC Start Program command */
snelson63133f92010-01-04 17:15:23 +0000390 start_program_jedec_common(flash, mask);
ollie5b621572004-03-20 16:46:10 +0000391
olliea4302802004-12-07 03:15:51 +0000392 /* transfer data from source to destination */
hailfingerfe072472009-11-14 03:48:33 +0000393 for (i = 0; i < page_size; i++) {
olliea4302802004-12-07 03:15:51 +0000394 /* If the data is 0xFF, don't program it */
uwef6641642007-05-09 10:17:44 +0000395 if (*src != 0xFF)
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700396 chip_writeb(flash, *src, dst);
stepan7abc6322006-11-22 00:29:51 +0000397 dst++;
398 src++;
ollie5b621572004-03-20 16:46:10 +0000399 }
400
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700401 toggle_ready_jedec(flash, dst - 1);
olliea4302802004-12-07 03:15:51 +0000402
stepan7abc6322006-11-22 00:29:51 +0000403 dst = d;
404 src = s;
hailfingerf2ac27e2009-11-25 16:41:50 +0000405 failed = verify_range(flash, src, start, page_size, NULL);
uwef6641642007-05-09 10:17:44 +0000406
hailfingerf2ac27e2009-11-25 16:41:50 +0000407 if (failed && tried++ < MAX_REFLASH_TRIES) {
snelsonfc007bb2010-03-24 23:14:32 +0000408 msg_cerr("retrying.\n");
uwef6641642007-05-09 10:17:44 +0000409 goto retry;
410 }
hailfingerf2ac27e2009-11-25 16:41:50 +0000411 if (failed) {
Kangheui Won4974cc12019-10-18 12:59:01 +1100412 msg_cerr(" page 0x%" PRIxPTR " failed!\n",
hailfinger82719632009-05-16 21:22:56 +0000413 (d - bios) / page_size);
stepan7abc6322006-11-22 00:29:51 +0000414 }
hailfingerf2ac27e2009-11-25 16:41:50 +0000415 return failed;
ollie5b621572004-03-20 16:46:10 +0000416}
417
hailfinger71e1bd42010-10-13 22:26:56 +0000418/* chunksize is page_size */
hailfinger86bf3b52010-10-13 21:49:30 +0000419/*
420 * Write a part of the flash chip.
421 * FIXME: Use the chunk code from Michael Karcher instead.
422 * This function is a slightly modified copy of spi_write_chunked.
423 * Each page is written separately in chunks with a maximum size of chunksize.
424 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700425int write_jedec(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger80dea312010-01-09 03:15:50 +0000426{
stefanctc5eb8a92011-11-23 09:13:48 +0000427 unsigned int i, starthere, lenhere;
hailfinger86bf3b52010-10-13 21:49:30 +0000428 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700429 * in struct flashctx to do this properly. All chips using
hailfinger86bf3b52010-10-13 21:49:30 +0000430 * write_jedec have page_size set to max_writechunk_size, so
431 * we're OK for now.
432 */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100433 unsigned int page_size = flash->chip->page_size;
ollie5b621572004-03-20 16:46:10 +0000434
hailfinger86bf3b52010-10-13 21:49:30 +0000435 /* Warning: This loop has a very unusual condition and body.
436 * The loop needs to go through each page with at least one affected
437 * byte. The lowest page number is (start / page_size) since that
438 * division rounds down. The highest page number we want is the page
439 * where the last byte of the range lives. That last byte has the
440 * address (start + len - 1), thus the highest page number is
441 * (start + len - 1) / page_size. Since we want to include that last
442 * page as well, the loop condition uses <=.
443 */
444 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
445 /* Byte position of the first byte in the range in this page. */
446 /* starthere is an offset to the base address of the chip. */
447 starthere = max(start, i * page_size);
448 /* Length of bytes in the range in this page. */
449 lenhere = min(start + len, (i + 1) * page_size) - starthere;
snelsonc6855342010-01-28 23:55:12 +0000450
hailfinger86bf3b52010-10-13 21:49:30 +0000451 if (write_page_write_jedec_common(flash, buf + starthere - start, starthere, lenhere))
452 return 1;
rminnich8d3ff912003-10-25 17:01:29 +0000453 }
rminnich8d3ff912003-10-25 17:01:29 +0000454
hailfinger86bf3b52010-10-13 21:49:30 +0000455 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000456}
hailfingerfff99532009-11-27 17:49:42 +0000457
snelson63133f92010-01-04 17:15:23 +0000458/* erase chip with block_erase() prototype */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700459int erase_chip_block_jedec(struct flashctx *flash, unsigned int addr,
snelson63133f92010-01-04 17:15:23 +0000460 unsigned int blocksize)
461{
stefanctc5eb8a92011-11-23 09:13:48 +0000462 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000463
464 mask = getaddrmask(flash);
Patrick Georgif3fa2992017-02-02 16:24:44 +0100465 if ((addr != 0) || (blocksize != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000466 msg_cerr("%s called with incorrect arguments\n",
snelson63133f92010-01-04 17:15:23 +0000467 __func__);
468 return -1;
469 }
snelsonc6855342010-01-28 23:55:12 +0000470 return erase_chip_jedec_common(flash, mask);
snelson63133f92010-01-04 17:15:23 +0000471}
472
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700473int probe_jedec(struct flashctx *flash)
snelson63133f92010-01-04 17:15:23 +0000474{
stefanctc5eb8a92011-11-23 09:13:48 +0000475 unsigned int mask;
hailfinger80dea312010-01-09 03:15:50 +0000476
477 mask = getaddrmask(flash);
snelsonc6855342010-01-28 23:55:12 +0000478 return probe_jedec_common(flash, mask);
snelson63133f92010-01-04 17:15:23 +0000479}
480
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700481int erase_sector_jedec(struct flashctx *flash, unsigned int page, unsigned int size)
snelson63133f92010-01-04 17:15:23 +0000482{
stefanctc5eb8a92011-11-23 09:13:48 +0000483 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000484
485 mask = getaddrmask(flash);
486 return erase_sector_jedec_common(flash, page, size, mask);
snelson63133f92010-01-04 17:15:23 +0000487}
488
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700489int erase_block_jedec(struct flashctx *flash, unsigned int page, unsigned int size)
snelson63133f92010-01-04 17:15:23 +0000490{
stefanctc5eb8a92011-11-23 09:13:48 +0000491 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000492
493 mask = getaddrmask(flash);
494 return erase_block_jedec_common(flash, page, size, mask);
snelson63133f92010-01-04 17:15:23 +0000495}
496
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700497int erase_chip_jedec(struct flashctx *flash)
snelson63133f92010-01-04 17:15:23 +0000498{
stefanctc5eb8a92011-11-23 09:13:48 +0000499 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000500
501 mask = getaddrmask(flash);
502 return erase_chip_jedec_common(flash, mask);
snelson63133f92010-01-04 17:15:23 +0000503}
Alan Greend793f5b2019-09-02 17:03:51 +1000504
505struct unlockblock {
506 unsigned int size;
507 unsigned int count;
508};
509
510typedef int (*unlockblock_func)(const struct flashctx *flash, chipaddr offset);
511static int regspace2_walk_unlockblocks(const struct flashctx *flash, const struct unlockblock *block, unlockblock_func func)
512{
513 chipaddr off = flash->virtual_registers + 2;
514 while (block->count != 0) {
515 unsigned int j;
516 for (j = 0; j < block->count; j++) {
517 if (func(flash, off))
518 return -1;
519 off += block->size;
520 }
521 block++;
522 }
523 return 0;
524}
525
526#define REG2_RWLOCK ((1 << 2) | (1 << 0))
527#define REG2_LOCKDOWN (1 << 1)
528#define REG2_MASK (REG2_RWLOCK | REG2_LOCKDOWN)
529
530static int printlock_regspace2_block(const struct flashctx *flash, chipaddr lockreg)
531{
532 uint8_t state = chip_readb(flash, lockreg);
533 msg_cdbg("Lock status of block at %p is ", (void *)lockreg);
534 switch (state & REG2_MASK) {
535 case 0:
536 msg_cdbg("Full Access.\n");
537 break;
538 case 1:
539 msg_cdbg("Write Lock (Default State).\n");
540 break;
541 case 2:
542 msg_cdbg("Locked Open (Full Access, Locked Down).\n");
543 break;
544 case 3:
545 msg_cdbg("Write Lock, Locked Down.\n");
546 break;
547 case 4:
548 msg_cdbg("Read Lock.\n");
549 break;
550 case 5:
551 msg_cdbg("Read/Write Lock.\n");
552 break;
553 case 6:
554 msg_cdbg("Read Lock, Locked Down.\n");
555 break;
556 case 7:
557 msg_cdbg("Read/Write Lock, Locked Down.\n");
558 break;
559 }
560 return 0;
561}
562
563static int printlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
564{
565 const unsigned int elems = flash->chip->total_size * 1024 / block_size;
566 struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
567 return regspace2_walk_unlockblocks(flash, blocks, &printlock_regspace2_block);
568}
569
570int printlock_regspace2_uniform_64k(struct flashctx *flash)
571{
572 return printlock_regspace2_uniform(flash, 64 * 1024);
573}
574
575int printlock_regspace2_block_eraser_0(struct flashctx *flash)
576{
577 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
578 const struct unlockblock *unlockblocks =
579 (const struct unlockblock *)flash->chip->block_erasers[0].eraseblocks;
580 return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block);
581}
582
583int printlock_regspace2_block_eraser_1(struct flashctx *flash)
584{
585 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
586 const struct unlockblock *unlockblocks =
587 (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks;
588 return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block);
589}
590
591/* Try to change the lock register at address lockreg from cur to new.
592 *
593 * - Try to unlock the lock bit if requested and it is currently set (although this is probably futile).
594 * - Try to change the read/write bits if requested.
595 * - Try to set the lockdown bit if requested.
596 * Return an error immediately if any of this fails. */
597static int changelock_regspace2_block(const struct flashctx *flash, chipaddr lockreg, uint8_t cur, uint8_t new)
598{
599 /* Only allow changes to known read/write/lockdown bits */
600 if (((cur ^ new) & ~REG2_MASK) != 0) {
601 msg_cerr("Invalid lock change from 0x%02x to 0x%02x requested at %p!\n"
602 "Please report a bug at flashrom@flashrom.org\n",
603 cur, new, (void *)lockreg);
604 return -1;
605 }
606
607 /* Exit early if no change (of read/write/lockdown bits) was requested. */
608 if (((cur ^ new) & REG2_MASK) == 0) {
609 msg_cdbg2("Lock bits at %p not changed.\n", (void *) lockreg);
610 return 0;
611 }
612
613 /* Normally the lockdown bit can not be cleared. Try nevertheless if requested. */
614 if ((cur & REG2_LOCKDOWN) && !(new & REG2_LOCKDOWN)) {
615 chip_writeb(flash, cur & ~REG2_LOCKDOWN, lockreg);
616 cur = chip_readb(flash, lockreg);
617 if ((cur & REG2_LOCKDOWN) == REG2_LOCKDOWN) {
618 msg_cwarn("Lockdown can't be removed at %p! New value: 0x%02x.\n",
619 (void *) lockreg, cur);
620 return -1;
621 }
622 }
623
624 /* Change read and/or write bit */
625 if ((cur ^ new) & REG2_RWLOCK) {
626 /* Do not lockdown yet. */
627 uint8_t wanted = (cur & ~REG2_RWLOCK) | (new & REG2_RWLOCK);
628 chip_writeb(flash, wanted, lockreg);
629 cur = chip_readb(flash, lockreg);
630 if (cur != wanted) {
631 msg_cerr("Changing lock bits failed at %p! New value: 0x%02x.\n",
632 (void *) lockreg, cur);
633 return -1;
634 }
635 msg_cdbg("Changed lock bits at %p to 0x%02x.\n", (void *) lockreg, cur);
636 }
637
638 /* Eventually, enable lockdown if requested. */
639 if (!(cur & REG2_LOCKDOWN) && (new & REG2_LOCKDOWN)) {
640 chip_writeb(flash, new, lockreg);
641 cur = chip_readb(flash, lockreg);
642 if (cur != new) {
643 msg_cerr("Enabling lockdown FAILED at %p! New value: 0x%02x.\n",
644 (void *) lockreg, cur);
645 return -1;
646 }
647 msg_cdbg("Enabled lockdown at %p\n", (void *) lockreg);
648 }
649
650 return 0;
651}
652
653static int unlock_regspace2_block_generic(const struct flashctx *flash, chipaddr lockreg)
654{
655 uint8_t old = chip_readb(flash, lockreg);
656 /* We don't care for the lockdown bit as long as the RW locks are 0 after we're done */
657 return changelock_regspace2_block(flash, lockreg, old, old & ~REG2_RWLOCK);
658}
659
660static int unlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
661{
662 const unsigned int elems = flash->chip->total_size * 1024 / block_size;
663 struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
664 return regspace2_walk_unlockblocks(flash, blocks, &unlock_regspace2_block_generic);
665}
666
667int unlock_regspace2_uniform_64k(struct flashctx *flash)
668{
669 return unlock_regspace2_uniform(flash, 64 * 1024);
670}
671
672int unlock_regspace2_uniform_32k(struct flashctx *flash)
673{
674 return unlock_regspace2_uniform(flash, 32 * 1024);
675}
676
677int unlock_regspace2_block_eraser_0(struct flashctx *flash)
678{
679 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
680 const struct unlockblock *unlockblocks =
681 (const struct unlockblock *)flash->chip->block_erasers[0].eraseblocks;
682 return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block_generic);
683}
684
685int unlock_regspace2_block_eraser_1(struct flashctx *flash)
686{
687 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
688 const struct unlockblock *unlockblocks =
689 (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks;
690 return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block_generic);
691}