blob: e771e646bc1ba7aac96b6b169db8ffc5322fe509 [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>
Stuart Langley60395ef2020-03-25 20:32:45 +11007 * Copyright (C) 2007-2012 Carl-Daniel Hailfinger
snelson63133f92010-01-04 17:15:23 +00008 * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com>
Stuart Langley60395ef2020-03-25 20:32:45 +11009 * Copyright (C) 2014 Stefan Tauner
rminnich8d3ff912003-10-25 17:01:29 +000010 *
uweb25f1ea2007-08-29 17:52:32 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
rminnich8d3ff912003-10-25 17:01:29 +000015 *
uweb25f1ea2007-08-29 17:52:32 +000016 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
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
Stuart Langley60395ef2020-03-25 20:32:45 +110038static void toggle_ready_jedec_common(const struct flashctx *flash, chipaddr dst, unsigned 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.
Stuart Langley60395ef2020-03-25 20:32:45 +110067 * Given that erase is slow on all chips, it is recommended to use
hailfinger10023012009-12-17 16:20:26 +000068 * 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
Stuart Langley60395ef2020-03-25 20:32:45 +110075void data_polling_jedec(const struct flashctx *flash, chipaddr dst,
76 uint8_t data)
uwedf467892007-08-23 10:20:40 +000077{
78 unsigned int i = 0;
79 uint8_t tmp;
80
81 data &= 0x80;
82
83 while (i++ < 0xFFFFFFF) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -070084 tmp = chip_readb(flash, dst) & 0x80;
uwedf467892007-08-23 10:20:40 +000085 if (tmp == data) {
86 break;
87 }
88 }
hailfinger10023012009-12-17 16:20:26 +000089 if (i > 0x100000)
snelsonfc007bb2010-03-24 23:14:32 +000090 msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i);
uwedf467892007-08-23 10:20:40 +000091}
92
Stuart Langley60395ef2020-03-25 20:32:45 +110093static unsigned int getaddrmask(const struct flashchip *chip)
hailfinger86bf3b52010-10-13 21:49:30 +000094{
Stuart Langley60395ef2020-03-25 20:32:45 +110095 switch (chip->feature_bits & FEATURE_ADDR_MASK) {
hailfinger86bf3b52010-10-13 21:49:30 +000096 case FEATURE_ADDR_FULL:
97 return MASK_FULL;
98 break;
99 case FEATURE_ADDR_2AA:
100 return MASK_2AA;
101 break;
102 case FEATURE_ADDR_AAA:
103 return MASK_AAA;
104 break;
105 default:
106 msg_cerr("%s called with unknown mask\n", __func__);
107 return 0;
108 break;
109 }
110}
111
Stuart Langley60395ef2020-03-25 20:32:45 +1100112static void start_program_jedec_common(const struct flashctx *flash, unsigned int mask)
uwedf467892007-08-23 10:20:40 +0000113{
snelson63133f92010-01-04 17:15:23 +0000114 chipaddr bios = flash->virtual_memory;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700115 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
116 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
117 chip_writeb(flash, 0xA0, bios + (0x5555 & mask));
uwedf467892007-08-23 10:20:40 +0000118}
119
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700120static int probe_jedec_common(struct flashctx *flash, unsigned int mask)
rminnich8d3ff912003-10-25 17:01:29 +0000121{
hailfinger82719632009-05-16 21:22:56 +0000122 chipaddr bios = flash->virtual_memory;
ollie6a600992005-11-26 21:55:36 +0000123 uint8_t id1, id2;
hailfinger428f2012007-12-31 01:49:00 +0000124 uint32_t largeid1, largeid2;
hailfinger027d7d92009-05-11 14:40:31 +0000125 uint32_t flashcontent1, flashcontent2;
hailfingerd5b35922009-06-03 14:46:22 +0000126 int probe_timing_enter, probe_timing_exit;
127
Patrick Georgif3fa2992017-02-02 16:24:44 +0100128 if (flash->chip->probe_timing > 0)
129 probe_timing_enter = probe_timing_exit = flash->chip->probe_timing;
130 else if (flash->chip->probe_timing == TIMING_ZERO) { /* No delay. */
hailfingerd5b35922009-06-03 14:46:22 +0000131 probe_timing_enter = probe_timing_exit = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100132 } else if (flash->chip->probe_timing == TIMING_FIXME) { /* == _IGNORED */
snelsonfc007bb2010-03-24 23:14:32 +0000133 msg_cdbg("Chip lacks correct probe timing information, "
hailfinger0cb68252009-07-23 01:33:43 +0000134 "using default 10mS/40uS. ");
hailfingerd5b35922009-06-03 14:46:22 +0000135 probe_timing_enter = 10000;
136 probe_timing_exit = 40;
137 } else {
snelsonfc007bb2010-03-24 23:14:32 +0000138 msg_cerr("Chip has negative value in probe_timing, failing "
hailfingerd5b35922009-06-03 14:46:22 +0000139 "without chip access\n");
140 return 0;
141 }
rminnich8d3ff912003-10-25 17:01:29 +0000142
hailfingerb07dc972010-10-20 21:13:19 +0000143 /* Earlier probes might have been too fast for the chip to enter ID
144 * mode completely. Allow the chip to finish this before seeing a
145 * reset command.
146 */
147 if (probe_timing_enter)
148 programmer_delay(probe_timing_enter);
149 /* Reset chip to a clean slate */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100150 if ((flash->chip->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET)
hailfingerb07dc972010-10-20 21:13:19 +0000151 {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700152 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
hailfingerb07dc972010-10-20 21:13:19 +0000153 if (probe_timing_exit)
154 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700155 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
hailfingerb07dc972010-10-20 21:13:19 +0000156 if (probe_timing_exit)
157 programmer_delay(10);
158 }
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700159 chip_writeb(flash, 0xF0, bios + (0x5555 & mask));
hailfingerb07dc972010-10-20 21:13:19 +0000160 if (probe_timing_exit)
161 programmer_delay(probe_timing_exit);
162
ollie5b621572004-03-20 16:46:10 +0000163 /* Issue JEDEC Product ID Entry command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700164 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000165 if (probe_timing_enter)
166 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700167 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000168 if (probe_timing_enter)
169 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700170 chip_writeb(flash, 0x90, bios + (0x5555 & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000171 if (probe_timing_enter)
172 programmer_delay(probe_timing_enter);
rminnich8d3ff912003-10-25 17:01:29 +0000173
ollie5b621572004-03-20 16:46:10 +0000174 /* Read product ID */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700175 id1 = chip_readb(flash, bios);
176 id2 = chip_readb(flash, bios + 0x01);
hailfinger428f2012007-12-31 01:49:00 +0000177 largeid1 = id1;
178 largeid2 = id2;
179
180 /* Check if it is a continuation ID, this should be a while loop. */
181 if (id1 == 0x7F) {
182 largeid1 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700183 id1 = chip_readb(flash, bios + 0x100);
hailfinger428f2012007-12-31 01:49:00 +0000184 largeid1 |= id1;
185 }
186 if (id2 == 0x7F) {
187 largeid2 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700188 id2 = chip_readb(flash, bios + 0x101);
hailfinger428f2012007-12-31 01:49:00 +0000189 largeid2 |= id2;
190 }
rminnich8d3ff912003-10-25 17:01:29 +0000191
ollie5b621572004-03-20 16:46:10 +0000192 /* Issue JEDEC Product ID Exit command */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100193 if ((flash->chip->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET)
snelson63133f92010-01-04 17:15:23 +0000194 {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700195 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
snelson63133f92010-01-04 17:15:23 +0000196 if (probe_timing_exit)
197 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700198 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
snelson63133f92010-01-04 17:15:23 +0000199 if (probe_timing_exit)
200 programmer_delay(10);
201 }
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700202 chip_writeb(flash, 0xF0, bios + (0x5555 & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000203 if (probe_timing_exit)
204 programmer_delay(probe_timing_exit);
rminnich8d3ff912003-10-25 17:01:29 +0000205
snelsonfc007bb2010-03-24 23:14:32 +0000206 msg_cdbg("%s: id1 0x%02x, id2 0x%02x", __func__, largeid1, largeid2);
hailfinger79cf3672008-05-14 12:03:06 +0000207 if (!oddparity(id1))
snelsonfc007bb2010-03-24 23:14:32 +0000208 msg_cdbg(", id1 parity violation");
hailfinger027d7d92009-05-11 14:40:31 +0000209
210 /* Read the product ID location again. We should now see normal flash contents. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700211 flashcontent1 = chip_readb(flash, bios);
212 flashcontent2 = chip_readb(flash, bios + 0x01);
hailfinger027d7d92009-05-11 14:40:31 +0000213
214 /* Check if it is a continuation ID, this should be a while loop. */
215 if (flashcontent1 == 0x7F) {
216 flashcontent1 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700217 flashcontent1 |= chip_readb(flash, bios + 0x100);
hailfinger027d7d92009-05-11 14:40:31 +0000218 }
219 if (flashcontent2 == 0x7F) {
220 flashcontent2 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700221 flashcontent2 |= chip_readb(flash, bios + 0x101);
hailfinger027d7d92009-05-11 14:40:31 +0000222 }
223
224 if (largeid1 == flashcontent1)
snelsonfc007bb2010-03-24 23:14:32 +0000225 msg_cdbg(", id1 is normal flash content");
hailfinger027d7d92009-05-11 14:40:31 +0000226 if (largeid2 == flashcontent2)
snelsonfc007bb2010-03-24 23:14:32 +0000227 msg_cdbg(", id2 is normal flash content");
hailfinger027d7d92009-05-11 14:40:31 +0000228
snelsonfc007bb2010-03-24 23:14:32 +0000229 msg_cdbg("\n");
Patrick Georgif3fa2992017-02-02 16:24:44 +0100230 if (largeid1 != flash->chip->manufacture_id || largeid2 != flash->chip->model_id)
hailfingerafac00e2010-01-09 02:24:17 +0000231 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000232
Patrick Georgif3fa2992017-02-02 16:24:44 +0100233 if (flash->chip->feature_bits & FEATURE_REGISTERMAP)
snelson63133f92010-01-04 17:15:23 +0000234 map_flash_registers(flash);
235
hailfingerafac00e2010-01-09 02:24:17 +0000236 return 1;
olliea3def632004-03-19 22:10:07 +0000237}
238
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700239static int erase_sector_jedec_common(struct flashctx *flash, unsigned int page,
Stuart Langley60395ef2020-03-25 20:32:45 +1100240 unsigned int pagesize, unsigned int mask)
olliea3def632004-03-19 22:10:07 +0000241{
hailfinger7af83692009-06-15 17:23:36 +0000242 chipaddr bios = flash->virtual_memory;
mkarcherf7af1b42011-04-15 00:03:37 +0000243 int delay_us = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100244 if(flash->chip->probe_timing != TIMING_ZERO)
mkarcherf7af1b42011-04-15 00:03:37 +0000245 delay_us = 10;
hailfinger7af83692009-06-15 17:23:36 +0000246
ollie5b621572004-03-20 16:46:10 +0000247 /* Issue the Sector Erase command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700248 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000249 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700250 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000251 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700252 chip_writeb(flash, 0x80, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000253 programmer_delay(delay_us);
ollie0eb62d62004-12-08 20:10:01 +0000254
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700255 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000256 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700257 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000258 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700259 chip_writeb(flash, 0x30, bios + page);
mkarcherf7af1b42011-04-15 00:03:37 +0000260 programmer_delay(delay_us);
ollie5b621572004-03-20 16:46:10 +0000261
olliea3def632004-03-19 22:10:07 +0000262 /* wait for Toggle bit ready */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700263 toggle_ready_jedec_slow(flash, bios);
olliea3def632004-03-19 22:10:07 +0000264
hailfingerac8e3182011-06-26 17:04:16 +0000265 /* FIXME: Check the status register for errors. */
uwebe4477b2007-08-23 16:08:21 +0000266 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000267}
olliea4302802004-12-07 03:15:51 +0000268
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700269static int erase_block_jedec_common(struct flashctx *flash, unsigned int block,
Stuart Langley60395ef2020-03-25 20:32:45 +1100270 unsigned int blocksize, unsigned int mask)
rminnichdfcbaa72004-09-30 16:37:01 +0000271{
hailfinger7af83692009-06-15 17:23:36 +0000272 chipaddr bios = flash->virtual_memory;
mkarcherf7af1b42011-04-15 00:03:37 +0000273 int delay_us = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100274 if(flash->chip->probe_timing != TIMING_ZERO)
mkarcherf7af1b42011-04-15 00:03:37 +0000275 delay_us = 10;
hailfinger7af83692009-06-15 17:23:36 +0000276
rminnichdfcbaa72004-09-30 16:37:01 +0000277 /* Issue the Sector Erase command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700278 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000279 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700280 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000281 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700282 chip_writeb(flash, 0x80, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000283 programmer_delay(delay_us);
ollie0eb62d62004-12-08 20:10:01 +0000284
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700285 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000286 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700287 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000288 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700289 chip_writeb(flash, 0x50, bios + block);
mkarcherf7af1b42011-04-15 00:03:37 +0000290 programmer_delay(delay_us);
rminnichdfcbaa72004-09-30 16:37:01 +0000291
292 /* wait for Toggle bit ready */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700293 toggle_ready_jedec_slow(flash, bios);
rminnichdfcbaa72004-09-30 16:37:01 +0000294
hailfingerac8e3182011-06-26 17:04:16 +0000295 /* FIXME: Check the status register for errors. */
uwebe4477b2007-08-23 16:08:21 +0000296 return 0;
rminnichdfcbaa72004-09-30 16:37:01 +0000297}
rminnich8d3ff912003-10-25 17:01:29 +0000298
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700299static int erase_chip_jedec_common(struct flashctx *flash, unsigned int mask)
rminnich8d3ff912003-10-25 17:01:29 +0000300{
hailfinger82719632009-05-16 21:22:56 +0000301 chipaddr bios = flash->virtual_memory;
mkarcherf7af1b42011-04-15 00:03:37 +0000302 int delay_us = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100303 if(flash->chip->probe_timing != TIMING_ZERO)
mkarcherf7af1b42011-04-15 00:03:37 +0000304 delay_us = 10;
rminnich8d3ff912003-10-25 17:01:29 +0000305
ollie5b621572004-03-20 16:46:10 +0000306 /* Issue the JEDEC Chip Erase command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700307 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000308 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700309 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000310 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700311 chip_writeb(flash, 0x80, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000312 programmer_delay(delay_us);
ollie0eb62d62004-12-08 20:10:01 +0000313
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700314 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000315 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700316 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000317 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700318 chip_writeb(flash, 0x10, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000319 programmer_delay(delay_us);
olliea3def632004-03-19 22:10:07 +0000320
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700321 toggle_ready_jedec_slow(flash, bios);
rminnich8d3ff912003-10-25 17:01:29 +0000322
hailfingerac8e3182011-06-26 17:04:16 +0000323 /* FIXME: Check the status register for errors. */
uwebe4477b2007-08-23 16:08:21 +0000324 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000325}
326
Stuart Langley60395ef2020-03-25 20:32:45 +1100327static int write_byte_program_jedec_common(const struct flashctx *flash, const uint8_t *src,
328 chipaddr dst, unsigned int mask)
snelson63133f92010-01-04 17:15:23 +0000329{
330 int tried = 0, failed = 0;
331 chipaddr bios = flash->virtual_memory;
332
333 /* If the data is 0xFF, don't program it and don't complain. */
334 if (*src == 0xFF) {
335 return 0;
336 }
337
338retry:
339 /* Issue JEDEC Byte Program command */
340 start_program_jedec_common(flash, mask);
341
342 /* transfer data from source to destination */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700343 chip_writeb(flash, *src, dst);
344 toggle_ready_jedec(flash, bios);
snelson63133f92010-01-04 17:15:23 +0000345
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700346 if (chip_readb(flash, dst) != *src && tried++ < MAX_REFLASH_TRIES) {
snelson63133f92010-01-04 17:15:23 +0000347 goto retry;
348 }
349
350 if (tried >= MAX_REFLASH_TRIES)
351 failed = 1;
352
353 return failed;
354}
355
hailfinger71e1bd42010-10-13 22:26:56 +0000356/* chunksize is 1 */
Stuart Langley60395ef2020-03-25 20:32:45 +1100357int write_jedec_1(struct flashctx *flash, const uint8_t *src, unsigned int start,
358 unsigned int len)
snelson63133f92010-01-04 17:15:23 +0000359{
Stuart Langley60395ef2020-03-25 20:32:45 +1100360 unsigned int i;
361 int failed = 0;
hailfingera10a6072010-10-10 14:02:27 +0000362 chipaddr dst = flash->virtual_memory + start;
snelson63133f92010-01-04 17:15:23 +0000363 chipaddr olddst;
stefanctc5eb8a92011-11-23 09:13:48 +0000364 unsigned int mask;
hailfinger86bf3b52010-10-13 21:49:30 +0000365
Stuart Langley60395ef2020-03-25 20:32:45 +1100366 mask = getaddrmask(flash->chip);
snelson63133f92010-01-04 17:15:23 +0000367
368 olddst = dst;
hailfingera10a6072010-10-10 14:02:27 +0000369 for (i = 0; i < len; i++) {
snelson63133f92010-01-04 17:15:23 +0000370 if (write_byte_program_jedec_common(flash, src, dst, mask))
371 failed = 1;
372 dst++, src++;
373 }
374 if (failed)
Kangheui Won4974cc12019-10-18 12:59:01 +1100375 msg_cerr(" writing sector at 0x%" PRIxPTR " failed!\n", olddst);
snelson63133f92010-01-04 17:15:23 +0000376
377 return failed;
378}
379
Stuart Langley60395ef2020-03-25 20:32:45 +1100380static int write_page_write_jedec_common(struct flashctx *flash, const uint8_t *src,
381 unsigned int start, unsigned int page_size)
rminnich8d3ff912003-10-25 17:01:29 +0000382{
Stuart Langley60395ef2020-03-25 20:32:45 +1100383 unsigned int i;
384 int tried = 0, failed;
385 const uint8_t *s = src;
hailfingerc2cfc592009-06-25 13:57:31 +0000386 chipaddr bios = flash->virtual_memory;
387 chipaddr dst = bios + start;
388 chipaddr d = dst;
stefanctc5eb8a92011-11-23 09:13:48 +0000389 unsigned int mask;
hailfinger86bf3b52010-10-13 21:49:30 +0000390
Stuart Langley60395ef2020-03-25 20:32:45 +1100391 mask = getaddrmask(flash->chip);
rminnich8d3ff912003-10-25 17:01:29 +0000392
stepan7abc6322006-11-22 00:29:51 +0000393retry:
uwe3a3ab2f2010-03-25 23:18:41 +0000394 /* Issue JEDEC Start Program command */
snelson63133f92010-01-04 17:15:23 +0000395 start_program_jedec_common(flash, mask);
ollie5b621572004-03-20 16:46:10 +0000396
olliea4302802004-12-07 03:15:51 +0000397 /* transfer data from source to destination */
hailfingerfe072472009-11-14 03:48:33 +0000398 for (i = 0; i < page_size; i++) {
olliea4302802004-12-07 03:15:51 +0000399 /* If the data is 0xFF, don't program it */
uwef6641642007-05-09 10:17:44 +0000400 if (*src != 0xFF)
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700401 chip_writeb(flash, *src, dst);
stepan7abc6322006-11-22 00:29:51 +0000402 dst++;
403 src++;
ollie5b621572004-03-20 16:46:10 +0000404 }
405
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700406 toggle_ready_jedec(flash, dst - 1);
olliea4302802004-12-07 03:15:51 +0000407
stepan7abc6322006-11-22 00:29:51 +0000408 dst = d;
409 src = s;
hailfingerf2ac27e2009-11-25 16:41:50 +0000410 failed = verify_range(flash, src, start, page_size, NULL);
uwef6641642007-05-09 10:17:44 +0000411
hailfingerf2ac27e2009-11-25 16:41:50 +0000412 if (failed && tried++ < MAX_REFLASH_TRIES) {
snelsonfc007bb2010-03-24 23:14:32 +0000413 msg_cerr("retrying.\n");
uwef6641642007-05-09 10:17:44 +0000414 goto retry;
415 }
hailfingerf2ac27e2009-11-25 16:41:50 +0000416 if (failed) {
Stuart Langley60395ef2020-03-25 20:32:45 +1100417 msg_cerr(" page 0x%" PRIxPTR " failed!\n", (d - bios) / page_size);
stepan7abc6322006-11-22 00:29:51 +0000418 }
hailfingerf2ac27e2009-11-25 16:41:50 +0000419 return failed;
ollie5b621572004-03-20 16:46:10 +0000420}
421
hailfinger71e1bd42010-10-13 22:26:56 +0000422/* chunksize is page_size */
hailfinger86bf3b52010-10-13 21:49:30 +0000423/*
424 * Write a part of the flash chip.
425 * FIXME: Use the chunk code from Michael Karcher instead.
426 * This function is a slightly modified copy of spi_write_chunked.
427 * Each page is written separately in chunks with a maximum size of chunksize.
428 */
Stuart Langley60395ef2020-03-25 20:32:45 +1100429int write_jedec(struct flashctx *flash, const uint8_t *buf, unsigned int start,
430 int unsigned len)
hailfinger80dea312010-01-09 03:15:50 +0000431{
stefanctc5eb8a92011-11-23 09:13:48 +0000432 unsigned int i, starthere, lenhere;
hailfinger86bf3b52010-10-13 21:49:30 +0000433 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700434 * in struct flashctx to do this properly. All chips using
hailfinger86bf3b52010-10-13 21:49:30 +0000435 * write_jedec have page_size set to max_writechunk_size, so
436 * we're OK for now.
437 */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100438 unsigned int page_size = flash->chip->page_size;
ollie5b621572004-03-20 16:46:10 +0000439
hailfinger86bf3b52010-10-13 21:49:30 +0000440 /* Warning: This loop has a very unusual condition and body.
441 * The loop needs to go through each page with at least one affected
442 * byte. The lowest page number is (start / page_size) since that
443 * division rounds down. The highest page number we want is the page
444 * where the last byte of the range lives. That last byte has the
445 * address (start + len - 1), thus the highest page number is
446 * (start + len - 1) / page_size. Since we want to include that last
447 * page as well, the loop condition uses <=.
448 */
449 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
450 /* Byte position of the first byte in the range in this page. */
451 /* starthere is an offset to the base address of the chip. */
452 starthere = max(start, i * page_size);
453 /* Length of bytes in the range in this page. */
454 lenhere = min(start + len, (i + 1) * page_size) - starthere;
snelsonc6855342010-01-28 23:55:12 +0000455
hailfinger86bf3b52010-10-13 21:49:30 +0000456 if (write_page_write_jedec_common(flash, buf + starthere - start, starthere, lenhere))
457 return 1;
rminnich8d3ff912003-10-25 17:01:29 +0000458 }
rminnich8d3ff912003-10-25 17:01:29 +0000459
hailfinger86bf3b52010-10-13 21:49:30 +0000460 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000461}
hailfingerfff99532009-11-27 17:49:42 +0000462
snelson63133f92010-01-04 17:15:23 +0000463/* erase chip with block_erase() prototype */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700464int erase_chip_block_jedec(struct flashctx *flash, unsigned int addr,
snelson63133f92010-01-04 17:15:23 +0000465 unsigned int blocksize)
466{
stefanctc5eb8a92011-11-23 09:13:48 +0000467 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000468
Stuart Langley60395ef2020-03-25 20:32:45 +1100469 mask = getaddrmask(flash->chip);
Patrick Georgif3fa2992017-02-02 16:24:44 +0100470 if ((addr != 0) || (blocksize != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000471 msg_cerr("%s called with incorrect arguments\n",
snelson63133f92010-01-04 17:15:23 +0000472 __func__);
473 return -1;
474 }
snelsonc6855342010-01-28 23:55:12 +0000475 return erase_chip_jedec_common(flash, mask);
snelson63133f92010-01-04 17:15:23 +0000476}
477
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700478int probe_jedec(struct flashctx *flash)
snelson63133f92010-01-04 17:15:23 +0000479{
stefanctc5eb8a92011-11-23 09:13:48 +0000480 unsigned int mask;
hailfinger80dea312010-01-09 03:15:50 +0000481
Stuart Langley60395ef2020-03-25 20:32:45 +1100482 mask = getaddrmask(flash->chip);
snelsonc6855342010-01-28 23:55:12 +0000483 return probe_jedec_common(flash, mask);
snelson63133f92010-01-04 17:15:23 +0000484}
485
Stuart Langley60395ef2020-03-25 20:32:45 +1100486int erase_sector_jedec(struct flashctx *flash, unsigned int page,
487 unsigned int size)
snelson63133f92010-01-04 17:15:23 +0000488{
stefanctc5eb8a92011-11-23 09:13:48 +0000489 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000490
Stuart Langley60395ef2020-03-25 20:32:45 +1100491 mask = getaddrmask(flash->chip);
snelsonc6855342010-01-28 23:55:12 +0000492 return erase_sector_jedec_common(flash, page, size, mask);
snelson63133f92010-01-04 17:15:23 +0000493}
494
Stuart Langley60395ef2020-03-25 20:32:45 +1100495int erase_block_jedec(struct flashctx *flash, unsigned int page,
496 unsigned int size)
snelson63133f92010-01-04 17:15:23 +0000497{
stefanctc5eb8a92011-11-23 09:13:48 +0000498 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000499
Stuart Langley60395ef2020-03-25 20:32:45 +1100500 mask = getaddrmask(flash->chip);
snelsonc6855342010-01-28 23:55:12 +0000501 return erase_block_jedec_common(flash, page, size, mask);
snelson63133f92010-01-04 17:15:23 +0000502}
503
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700504int erase_chip_jedec(struct flashctx *flash)
snelson63133f92010-01-04 17:15:23 +0000505{
stefanctc5eb8a92011-11-23 09:13:48 +0000506 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000507
Stuart Langley60395ef2020-03-25 20:32:45 +1100508 mask = getaddrmask(flash->chip);
snelsonc6855342010-01-28 23:55:12 +0000509 return erase_chip_jedec_common(flash, mask);
snelson63133f92010-01-04 17:15:23 +0000510}
Alan Greend793f5b2019-09-02 17:03:51 +1000511
512struct unlockblock {
513 unsigned int size;
514 unsigned int count;
515};
516
517typedef int (*unlockblock_func)(const struct flashctx *flash, chipaddr offset);
518static int regspace2_walk_unlockblocks(const struct flashctx *flash, const struct unlockblock *block, unlockblock_func func)
519{
520 chipaddr off = flash->virtual_registers + 2;
521 while (block->count != 0) {
522 unsigned int j;
523 for (j = 0; j < block->count; j++) {
524 if (func(flash, off))
525 return -1;
526 off += block->size;
527 }
528 block++;
529 }
530 return 0;
531}
532
533#define REG2_RWLOCK ((1 << 2) | (1 << 0))
534#define REG2_LOCKDOWN (1 << 1)
535#define REG2_MASK (REG2_RWLOCK | REG2_LOCKDOWN)
536
537static int printlock_regspace2_block(const struct flashctx *flash, chipaddr lockreg)
538{
539 uint8_t state = chip_readb(flash, lockreg);
Stuart Langley60395ef2020-03-25 20:32:45 +1100540 msg_cdbg("Lock status of block at 0x%0*" PRIxPTR " is ", PRIxPTR_WIDTH, lockreg);
Alan Greend793f5b2019-09-02 17:03:51 +1000541 switch (state & REG2_MASK) {
542 case 0:
543 msg_cdbg("Full Access.\n");
544 break;
545 case 1:
546 msg_cdbg("Write Lock (Default State).\n");
547 break;
548 case 2:
549 msg_cdbg("Locked Open (Full Access, Locked Down).\n");
550 break;
551 case 3:
552 msg_cdbg("Write Lock, Locked Down.\n");
553 break;
554 case 4:
555 msg_cdbg("Read Lock.\n");
556 break;
557 case 5:
558 msg_cdbg("Read/Write Lock.\n");
559 break;
560 case 6:
561 msg_cdbg("Read Lock, Locked Down.\n");
562 break;
563 case 7:
564 msg_cdbg("Read/Write Lock, Locked Down.\n");
565 break;
566 }
567 return 0;
568}
569
570static int printlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
571{
572 const unsigned int elems = flash->chip->total_size * 1024 / block_size;
573 struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
574 return regspace2_walk_unlockblocks(flash, blocks, &printlock_regspace2_block);
575}
576
577int printlock_regspace2_uniform_64k(struct flashctx *flash)
578{
579 return printlock_regspace2_uniform(flash, 64 * 1024);
580}
581
582int printlock_regspace2_block_eraser_0(struct flashctx *flash)
583{
584 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
585 const struct unlockblock *unlockblocks =
586 (const struct unlockblock *)flash->chip->block_erasers[0].eraseblocks;
587 return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block);
588}
589
590int printlock_regspace2_block_eraser_1(struct flashctx *flash)
591{
592 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
593 const struct unlockblock *unlockblocks =
594 (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks;
595 return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block);
596}
597
598/* Try to change the lock register at address lockreg from cur to new.
599 *
600 * - Try to unlock the lock bit if requested and it is currently set (although this is probably futile).
601 * - Try to change the read/write bits if requested.
602 * - Try to set the lockdown bit if requested.
603 * Return an error immediately if any of this fails. */
604static int changelock_regspace2_block(const struct flashctx *flash, chipaddr lockreg, uint8_t cur, uint8_t new)
605{
606 /* Only allow changes to known read/write/lockdown bits */
607 if (((cur ^ new) & ~REG2_MASK) != 0) {
Stuart Langley60395ef2020-03-25 20:32:45 +1100608 msg_cerr("Invalid lock change from 0x%02x to 0x%02x requested at 0x%0*" PRIxPTR "!\n"
Alan Greend793f5b2019-09-02 17:03:51 +1000609 "Please report a bug at flashrom@flashrom.org\n",
Stuart Langley60395ef2020-03-25 20:32:45 +1100610 cur, new, PRIxPTR_WIDTH, lockreg);
Alan Greend793f5b2019-09-02 17:03:51 +1000611 return -1;
612 }
613
614 /* Exit early if no change (of read/write/lockdown bits) was requested. */
615 if (((cur ^ new) & REG2_MASK) == 0) {
Stuart Langley60395ef2020-03-25 20:32:45 +1100616 msg_cdbg2("Lock bits at 0x%0*" PRIxPTR " not changed.\n", PRIxPTR_WIDTH, lockreg);
Alan Greend793f5b2019-09-02 17:03:51 +1000617 return 0;
618 }
619
620 /* Normally the lockdown bit can not be cleared. Try nevertheless if requested. */
621 if ((cur & REG2_LOCKDOWN) && !(new & REG2_LOCKDOWN)) {
622 chip_writeb(flash, cur & ~REG2_LOCKDOWN, lockreg);
623 cur = chip_readb(flash, lockreg);
624 if ((cur & REG2_LOCKDOWN) == REG2_LOCKDOWN) {
Stuart Langley60395ef2020-03-25 20:32:45 +1100625 msg_cwarn("Lockdown can't be removed at 0x%0*" PRIxPTR "! New value: 0x%02x.\n",
626 PRIxPTR_WIDTH, lockreg, cur);
Alan Greend793f5b2019-09-02 17:03:51 +1000627 return -1;
628 }
629 }
630
631 /* Change read and/or write bit */
632 if ((cur ^ new) & REG2_RWLOCK) {
633 /* Do not lockdown yet. */
634 uint8_t wanted = (cur & ~REG2_RWLOCK) | (new & REG2_RWLOCK);
635 chip_writeb(flash, wanted, lockreg);
636 cur = chip_readb(flash, lockreg);
637 if (cur != wanted) {
Stuart Langley60395ef2020-03-25 20:32:45 +1100638 msg_cerr("Changing lock bits failed at 0x%0*" PRIxPTR "! New value: 0x%02x.\n",
639 PRIxPTR_WIDTH, lockreg, cur);
Alan Greend793f5b2019-09-02 17:03:51 +1000640 return -1;
641 }
Stuart Langley60395ef2020-03-25 20:32:45 +1100642 msg_cdbg("Changed lock bits at 0x%0*" PRIxPTR " to 0x%02x.\n",
643 PRIxPTR_WIDTH, lockreg, cur);
Alan Greend793f5b2019-09-02 17:03:51 +1000644 }
645
646 /* Eventually, enable lockdown if requested. */
647 if (!(cur & REG2_LOCKDOWN) && (new & REG2_LOCKDOWN)) {
648 chip_writeb(flash, new, lockreg);
649 cur = chip_readb(flash, lockreg);
650 if (cur != new) {
Stuart Langley60395ef2020-03-25 20:32:45 +1100651 msg_cerr("Enabling lockdown FAILED at 0x%0*" PRIxPTR "! New value: 0x%02x.\n",
652 PRIxPTR_WIDTH, lockreg, cur);
Alan Greend793f5b2019-09-02 17:03:51 +1000653 return -1;
654 }
Stuart Langley60395ef2020-03-25 20:32:45 +1100655 msg_cdbg("Enabled lockdown at 0x%0*" PRIxPTR ".\n", PRIxPTR_WIDTH, lockreg);
Alan Greend793f5b2019-09-02 17:03:51 +1000656 }
657
658 return 0;
659}
660
661static int unlock_regspace2_block_generic(const struct flashctx *flash, chipaddr lockreg)
662{
663 uint8_t old = chip_readb(flash, lockreg);
664 /* We don't care for the lockdown bit as long as the RW locks are 0 after we're done */
665 return changelock_regspace2_block(flash, lockreg, old, old & ~REG2_RWLOCK);
666}
667
668static int unlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
669{
670 const unsigned int elems = flash->chip->total_size * 1024 / block_size;
671 struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
672 return regspace2_walk_unlockblocks(flash, blocks, &unlock_regspace2_block_generic);
673}
674
675int unlock_regspace2_uniform_64k(struct flashctx *flash)
676{
677 return unlock_regspace2_uniform(flash, 64 * 1024);
678}
679
680int unlock_regspace2_uniform_32k(struct flashctx *flash)
681{
682 return unlock_regspace2_uniform(flash, 32 * 1024);
683}
684
685int unlock_regspace2_block_eraser_0(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[0].eraseblocks;
690 return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block_generic);
691}
692
693int unlock_regspace2_block_eraser_1(struct flashctx *flash)
694{
695 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
696 const struct unlockblock *unlockblocks =
697 (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks;
698 return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block_generic);
699}