blob: 4ff1040e77f6b64ac9863080b5af9668782a502b [file] [log] [blame]
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2006 Giampiero Giancipoli <gianci@email.it>
6 * Copyright (C) 2006 coresystems GmbH <info@coresystems.de>
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +00007 * Copyright (C) 2007-2012 Carl-Daniel Hailfinger
Sean Nelsonc57a9202010-01-04 17:15:23 +00008 * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com>
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +00009 * Copyright (C) 2014 Stefan Tauner
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000010 *
Uwe Hermannd1107642007-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.
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000015 *
Uwe Hermannd1107642007-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.
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000020 */
21
22#include "flash.h"
Nico Huber2e947462026-03-07 17:04:59 +010023#include "programmer.h"
Nico Huber10337f72026-03-04 19:57:27 +010024#include "chipdrivers/memory_bus.h"
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000025
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +000026#define MAX_REFLASH_TRIES 0x10
Sean Nelsonc57a9202010-01-04 17:15:23 +000027#define MASK_FULL 0xffff
28#define MASK_2AA 0x7ff
Sean Nelson35727f72010-01-28 23:55:12 +000029#define MASK_AAA 0xfff
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +000030
Carl-Daniel Hailfingera758f512008-05-14 12:03:06 +000031/* Check one byte for odd parity */
32uint8_t oddparity(uint8_t val)
33{
34 val = (val ^ (val >> 4)) & 0xf;
35 val = (val ^ (val >> 2)) & 0x3;
36 return (val ^ (val >> 1)) & 0x1;
37}
38
Stefan Taunerf80419c2014-05-02 15:41:42 +000039static void toggle_ready_jedec_common(const struct flashctx *flash, chipaddr dst, unsigned int delay)
Uwe Hermann51582f22007-08-23 10:20:40 +000040{
41 unsigned int i = 0;
42 uint8_t tmp1, tmp2;
43
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000044 tmp1 = chip_readb(flash, dst) & 0x40;
Uwe Hermann51582f22007-08-23 10:20:40 +000045
46 while (i++ < 0xFFFFFFF) {
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000047 if (delay)
48 programmer_delay(delay);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000049 tmp2 = chip_readb(flash, dst) & 0x40;
Uwe Hermann51582f22007-08-23 10:20:40 +000050 if (tmp1 == tmp2) {
51 break;
52 }
53 tmp1 = tmp2;
54 }
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000055 if (i > 0x100000)
Sean Nelsoned479d22010-03-24 23:14:32 +000056 msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i);
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000057}
58
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000059void toggle_ready_jedec(const struct flashctx *flash, chipaddr dst)
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000060{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000061 toggle_ready_jedec_common(flash, dst, 0);
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000062}
63
64/* Some chips require a minimum delay between toggle bit reads.
65 * The Winbond W39V040C wants 50 ms between reads on sector erase toggle,
66 * but experiments show that 2 ms are already enough. Pick a safety factor
67 * of 4 and use an 8 ms delay.
Elyes HAOUAS124ef382018-03-27 12:15:09 +020068 * Given that erase is slow on all chips, it is recommended to use
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000069 * toggle_ready_jedec_slow in erase functions.
70 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000071static void toggle_ready_jedec_slow(const struct flashctx *flash, chipaddr dst)
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000072{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000073 toggle_ready_jedec_common(flash, dst, 8 * 1000);
Uwe Hermann51582f22007-08-23 10:20:40 +000074}
75
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000076void data_polling_jedec(const struct flashctx *flash, chipaddr dst,
77 uint8_t data)
Uwe Hermann51582f22007-08-23 10:20:40 +000078{
79 unsigned int i = 0;
80 uint8_t tmp;
81
82 data &= 0x80;
83
84 while (i++ < 0xFFFFFFF) {
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000085 tmp = chip_readb(flash, dst) & 0x80;
Uwe Hermann51582f22007-08-23 10:20:40 +000086 if (tmp == data) {
87 break;
88 }
89 }
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000090 if (i > 0x100000)
Sean Nelsoned479d22010-03-24 23:14:32 +000091 msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i);
Uwe Hermann51582f22007-08-23 10:20:40 +000092}
93
Nico Huber2e947462026-03-07 17:04:59 +010094static unsigned int getaddrmask_from_features(feature_bits_t chip_features)
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +000095{
Nico Huber2e947462026-03-07 17:04:59 +010096 switch (chip_features & FEATURE_ADDR_MASK) {
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +000097 case FEATURE_ADDR_FULL:
98 return MASK_FULL;
99 break;
100 case FEATURE_ADDR_2AA:
101 return MASK_2AA;
102 break;
103 case FEATURE_ADDR_AAA:
104 return MASK_AAA;
105 break;
106 default:
107 msg_cerr("%s called with unknown mask\n", __func__);
108 return 0;
109 break;
110 }
111}
112
Nico Huber2e947462026-03-07 17:04:59 +0100113static unsigned int getaddrmask(const struct flashprog_flashctx *flash)
114{
115 return getaddrmask_from_features(flash->chip->feature_bits);
116}
117
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000118static void start_program_jedec_common(const struct flashctx *flash, unsigned int mask)
Uwe Hermann51582f22007-08-23 10:20:40 +0000119{
Sean Nelsonc57a9202010-01-04 17:15:23 +0000120 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000121 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
122
123 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
124 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
125 chip_writeb(flash, 0xA0, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Uwe Hermann51582f22007-08-23 10:20:40 +0000126}
127
Nico Huber2e947462026-03-07 17:04:59 +0100128static struct found_id *probe_jedec_29gl_generic(
129 const struct par_master *const par,
130 const chipsize_t chip_size, const feature_bits_t chip_features)
Stefan Tauner03a9c3c2014-08-03 14:15:14 +0000131{
Nico Huber2e947462026-03-07 17:04:59 +0100132 const unsigned int mask = getaddrmask_from_features(chip_features);
133 uint8_t raw[4];
134
135 const chipaddr bios = (chipaddr)programmer_map_flash_data(par, chip_size, "");
136 if (bios == (chipaddr)ERROR_PTR)
137 return NULL;
Stefan Tauner03a9c3c2014-08-03 14:15:14 +0000138
139 /* Reset chip to a clean slate */
Nico Huber2e947462026-03-07 17:04:59 +0100140 par->chip_writeb(par, 0xF0, bios + (0x5555 & mask));
Stefan Tauner03a9c3c2014-08-03 14:15:14 +0000141
142 /* Issue JEDEC Product ID Entry command */
Nico Huber2e947462026-03-07 17:04:59 +0100143 par->chip_writeb(par, 0xAA, bios + (0x5555 & mask));
144 par->chip_writeb(par, 0x55, bios + (0x2AAA & mask));
145 par->chip_writeb(par, 0x90, bios + (0x5555 & mask));
Stefan Tauner03a9c3c2014-08-03 14:15:14 +0000146
147 /* Read product ID */
148 // FIXME: Continuation loop, second byte is at word 0x100/byte 0x200
Nico Huber2e947462026-03-07 17:04:59 +0100149 raw[0] = par->chip_readb(par, bios + 0x00);
150 raw[1] = par->chip_readb(par, bios + 0x01);
151 raw[2] = par->chip_readb(par, bios + 0x0E);
152 raw[3] = par->chip_readb(par, bios + 0x0F);
Stefan Tauner03a9c3c2014-08-03 14:15:14 +0000153
154 /* Issue JEDEC Product ID Exit command */
Nico Huber2e947462026-03-07 17:04:59 +0100155 par->chip_writeb(par, 0xF0, bios + (0x5555 & mask));
Stefan Tauner03a9c3c2014-08-03 14:15:14 +0000156
Nico Huber2e947462026-03-07 17:04:59 +0100157 const uint32_t man_id = raw[0];
158 const uint32_t dev_id = raw[1] << 16 | raw[2] << 8 | raw[3];
Stefan Tauner03a9c3c2014-08-03 14:15:14 +0000159
160 /* Read the product ID location again. We should now see normal flash contents. */
Nico Huber2e947462026-03-07 17:04:59 +0100161 uint32_t flashcontent1 = par->chip_readb(par, bios + 0x00); // FIXME: Continuation loop
162 uint32_t flashcontent2 = (par->chip_readb(par, bios + 0x01) << 16) |
163 (par->chip_readb(par, bios + 0x0E) << 8) |
164 (par->chip_readb(par, bios + 0x0F) << 0);
165
166 programmer_unmap_flash_region(par, (void *)bios, chip_size);
167
168 if (flashprog_no_data(raw, sizeof(raw)))
169 return NULL;
170
171 msg_cdbg("%s (%uKiB, features: 0x%02x): man_id 0x%02x, dev_id 0x%06x",
172 __func__, chip_size / KiB, chip_features, man_id, dev_id);
173
174 if (!oddparity(man_id))
175 msg_cdbg(", man_id parity violation");
Stefan Tauner03a9c3c2014-08-03 14:15:14 +0000176
177 if (man_id == flashcontent1)
178 msg_cdbg(", man_id seems to be normal flash content");
179 if (dev_id == flashcontent2)
180 msg_cdbg(", dev_id seems to be normal flash content");
181
182 msg_cdbg("\n");
Stefan Tauner03a9c3c2014-08-03 14:15:14 +0000183
Nico Huber2e947462026-03-07 17:04:59 +0100184 struct memory_found_id *const found = alloc_memory_found_id();
185 if (!found) {
186 msg_cerr("Out of memory!\n");
187 return NULL;
188 }
189
190 found->generic.info.id.manufacture = man_id;
191 found->generic.info.id.model = dev_id;
192 found->generic.info.id.type = ID_JEDEC_29GL;
193 found->memory_info.chip_size = chip_size;
194 found->memory_info.chip_features = chip_features;
195
196 return &found->generic;
Stefan Tauner03a9c3c2014-08-03 14:15:14 +0000197}
198
Nico Huber2e947462026-03-07 17:04:59 +0100199struct found_id *probe_jedec_29gl(const struct bus_probe *probe,
200 const struct master_common *mst,
201 const struct flashchip *chip)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000202{
Nico Huber2e947462026-03-07 17:04:59 +0100203 const struct par_master *const par = (const struct par_master *)mst;
204 struct found_id *ids = NULL, **next_ptr = &ids;
205 chipsize_t chip_size;
206
207 if (chip)
208 return probe_jedec_29gl_generic(par, chip->total_size * KiB, chip->feature_bits);
209
210 /* XXX: Only limited sizes and single mask supported at this time: */
211 for (chip_size = 16*MiB; chip_size >= 4*MiB; chip_size /= 2) {
212 *next_ptr = probe_jedec_29gl_generic(par, chip_size, FEATURE_ADDR_2AA);
213 if (*next_ptr)
214 next_ptr = &(*next_ptr)->next;
215 }
216
217 return ids;
218}
219
220static struct found_id *probe_jedec_generic(
221 const struct par_master *const par, const chipsize_t chip_size,
222 const feature_bits_t chip_features, const signed int probe_timing)
223{
224 const unsigned int mask = getaddrmask_from_features(chip_features);
225 const bool shifted = (chip_features & FEATURE_ADDR_SHIFTED);
226
227 uint8_t raw[4];
228 unsigned int idx = 0;
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000229 uint32_t largeid1, largeid2;
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000230 uint32_t flashcontent1, flashcontent2;
Stefan Taunerf80419c2014-05-02 15:41:42 +0000231 unsigned int probe_timing_enter, probe_timing_exit;
Maciej Pijankac6e11112009-06-03 14:46:22 +0000232
Nico Huber2e947462026-03-07 17:04:59 +0100233 const chipaddr bios = (chipaddr)programmer_map_flash_data(par, chip_size, "");
234 if (bios == (chipaddr)ERROR_PTR)
235 return NULL;
236
237 if (probe_timing == TIMING_FIXME) {
Maciej Pijankac6e11112009-06-03 14:46:22 +0000238 probe_timing_enter = 10000;
239 probe_timing_exit = 40;
Nico Huber2e947462026-03-07 17:04:59 +0100240 } else if (probe_timing > 0) {
241 probe_timing_enter = probe_timing_exit = probe_timing;
Maciej Pijankac6e11112009-06-03 14:46:22 +0000242 } else {
Nico Huber2e947462026-03-07 17:04:59 +0100243 probe_timing_enter = probe_timing_exit = 0;
Maciej Pijankac6e11112009-06-03 14:46:22 +0000244 }
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000245
Sean Nelsonf59e2632010-10-20 21:13:19 +0000246 /* Earlier probes might have been too fast for the chip to enter ID
247 * mode completely. Allow the chip to finish this before seeing a
248 * reset command.
249 */
250 if (probe_timing_enter)
251 programmer_delay(probe_timing_enter);
252 /* Reset chip to a clean slate */
Nico Huber2e947462026-03-07 17:04:59 +0100253 if (chip_features & FEATURE_LONG_RESET)
Sean Nelsonf59e2632010-10-20 21:13:19 +0000254 {
Nico Huber2e947462026-03-07 17:04:59 +0100255 par->chip_writeb(par, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonf59e2632010-10-20 21:13:19 +0000256 if (probe_timing_exit)
257 programmer_delay(10);
Nico Huber2e947462026-03-07 17:04:59 +0100258 par->chip_writeb(par, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Sean Nelsonf59e2632010-10-20 21:13:19 +0000259 if (probe_timing_exit)
260 programmer_delay(10);
261 }
Nico Huber2e947462026-03-07 17:04:59 +0100262 par->chip_writeb(par, 0xF0, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonf59e2632010-10-20 21:13:19 +0000263 if (probe_timing_exit)
264 programmer_delay(probe_timing_exit);
265
Ollie Lho761bf1b2004-03-20 16:46:10 +0000266 /* Issue JEDEC Product ID Entry command */
Nico Huber2e947462026-03-07 17:04:59 +0100267 par->chip_writeb(par, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonc12fc712009-12-17 04:22:40 +0000268 if (probe_timing_enter)
269 programmer_delay(10);
Nico Huber2e947462026-03-07 17:04:59 +0100270 par->chip_writeb(par, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Sean Nelsonc12fc712009-12-17 04:22:40 +0000271 if (probe_timing_enter)
272 programmer_delay(10);
Nico Huber2e947462026-03-07 17:04:59 +0100273 par->chip_writeb(par, 0x90, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonc12fc712009-12-17 04:22:40 +0000274 if (probe_timing_enter)
275 programmer_delay(probe_timing_enter);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000276
Ollie Lho761bf1b2004-03-20 16:46:10 +0000277 /* Read product ID */
Nico Huber2e947462026-03-07 17:04:59 +0100278 largeid1 = raw[idx++] = par->chip_readb(par, bios + (0x00 << shifted));
279 largeid2 = raw[idx++] = par->chip_readb(par, bios + (0x01 << shifted));
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000280
281 /* Check if it is a continuation ID, this should be a while loop. */
Nico Huber2e947462026-03-07 17:04:59 +0100282 if (largeid1 == 0x7F) {
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000283 largeid1 <<= 8;
Nico Huberb72fa1a2026-03-08 15:01:57 +0100284 largeid1 |= raw[idx++] = par->chip_readb(par, bios + (0x100 << shifted));
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000285 }
Nico Huber2e947462026-03-07 17:04:59 +0100286 if (largeid2 == 0x7F) {
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000287 largeid2 <<= 8;
Nico Huberb72fa1a2026-03-08 15:01:57 +0100288 largeid2 |= raw[idx++] = par->chip_readb(par, bios + (0x101 << shifted));
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000289 }
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000290
Ollie Lho761bf1b2004-03-20 16:46:10 +0000291 /* Issue JEDEC Product ID Exit command */
Nico Huber2e947462026-03-07 17:04:59 +0100292 if (chip_features & FEATURE_LONG_RESET)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000293 {
Nico Huber2e947462026-03-07 17:04:59 +0100294 par->chip_writeb(par, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonc57a9202010-01-04 17:15:23 +0000295 if (probe_timing_exit)
296 programmer_delay(10);
Nico Huber2e947462026-03-07 17:04:59 +0100297 par->chip_writeb(par, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Sean Nelsonc57a9202010-01-04 17:15:23 +0000298 if (probe_timing_exit)
299 programmer_delay(10);
300 }
Nico Huber2e947462026-03-07 17:04:59 +0100301 par->chip_writeb(par, 0xF0, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonc12fc712009-12-17 04:22:40 +0000302 if (probe_timing_exit)
303 programmer_delay(probe_timing_exit);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000304
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000305 /* Read the product ID location again. We should now see normal flash contents. */
Nico Huber2e947462026-03-07 17:04:59 +0100306 flashcontent1 = par->chip_readb(par, bios + (0x00 << shifted));
307 flashcontent2 = par->chip_readb(par, bios + (0x01 << shifted));
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000308
309 /* Check if it is a continuation ID, this should be a while loop. */
310 if (flashcontent1 == 0x7F) {
311 flashcontent1 <<= 8;
Nico Huberb72fa1a2026-03-08 15:01:57 +0100312 flashcontent1 |= par->chip_readb(par, bios + (0x100 << shifted));
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000313 }
314 if (flashcontent2 == 0x7F) {
315 flashcontent2 <<= 8;
Nico Huberb72fa1a2026-03-08 15:01:57 +0100316 flashcontent2 |= par->chip_readb(par, bios + (0x101 << shifted));
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000317 }
318
Nico Huber2e947462026-03-07 17:04:59 +0100319 programmer_unmap_flash_region(par, (void *)bios, chip_size);
320
321 if (flashprog_no_data(raw, idx))
322 return NULL;
323
324 msg_cdbg("%s (%uKiB, features: 0x%02x): id1 0x%02x, id2 0x%02x",
325 __func__, chip_size / KiB, chip_features, largeid1, largeid2);
326
327 if (!oddparity(raw[0]))
328 msg_cdbg(", id1 parity violation");
329
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000330 if (largeid1 == flashcontent1)
Sean Nelsoned479d22010-03-24 23:14:32 +0000331 msg_cdbg(", id1 is normal flash content");
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000332 if (largeid2 == flashcontent2)
Sean Nelsoned479d22010-03-24 23:14:32 +0000333 msg_cdbg(", id2 is normal flash content");
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000334
Sean Nelsoned479d22010-03-24 23:14:32 +0000335 msg_cdbg("\n");
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000336
Nico Huber2e947462026-03-07 17:04:59 +0100337 struct memory_found_id *const found = alloc_memory_found_id();
338 if (!found) {
339 msg_cerr("Out of memory!\n");
340 return NULL;
341 }
342
343 found->generic.info.id.manufacture = largeid1;
344 found->generic.info.id.model = largeid2;
345 found->generic.info.id.type = ID_JEDEC;
346 found->memory_info.chip_size = chip_size;
347 found->memory_info.chip_features = chip_features;
348 found->memory_info.probe_timing = probe_timing;
349
350 return &found->generic;
351}
352
353struct found_id *probe_jedec(const struct bus_probe *probe,
354 const struct master_common *mst,
355 const struct flashchip *chip)
356{
357 const struct par_master *const par = (const struct par_master *)mst;
358 struct found_id *ids = NULL, **next_ptr = &ids;
359 chipsize_t chip_size;
360 unsigned int set;
361
362 if (chip) {
363 return probe_jedec_generic(par,
364 chip->total_size * KiB, chip->feature_bits, chip->probe_timing);
365 }
366
367 static const struct {
368 feature_bits_t features;
369 signed int probe_timing;
370 } common_sets[] = {
371 { FEATURE_SHORT_RESET | FEATURE_ADDR_2AA, TIMING_ZERO },
372 { FEATURE_LONG_RESET, 10000 },
373 { FEATURE_LONG_RESET, 10 },
374 { FEATURE_LONG_RESET, 1 },
375 { FEATURE_LONG_RESET, TIMING_ZERO },
376 };
377 for (set = 0; set < ARRAY_SIZE(common_sets); ++set) {
378 for (chip_size = 1*MiB; chip_size >= 64*KiB; chip_size /= 2) {
379 *next_ptr = probe_jedec_generic(par, chip_size,
380 common_sets[set].features, common_sets[set].probe_timing);
381 if (*next_ptr)
382 next_ptr = &(*next_ptr)->next;
383 }
384 }
385
386 static const struct {
387 chipsize_t chip_size;
388 feature_bits_t features;
389 signed int probe_timing;
390 } additional_sets[] = {
Nico Huber3a353372026-03-08 15:06:51 +0100391 { 8*MiB, FEATURE_SHORT_RESET | FEATURE_ADDR_SHIFTED | FEATURE_ADDR_AAA, 10 },
392 { 2*MiB, FEATURE_SHORT_RESET, TIMING_ZERO },
393 { 2*MiB, FEATURE_LONG_RESET | FEATURE_ADDR_SHIFTED, 10 },
394 { 512*KiB, FEATURE_LONG_RESET | FEATURE_ADDR_SHIFTED, 10 },
395 { 384*KiB, FEATURE_LONG_RESET, 1 },
396 { 256*KiB, FEATURE_LONG_RESET | FEATURE_ADDR_2AA, TIMING_FIXME },
397 { 256*KiB, FEATURE_LONG_RESET | FEATURE_ADDR_AAA, TIMING_ZERO },
398 { 128*KiB, FEATURE_SHORT_RESET, TIMING_ZERO },
Nico Huber2e947462026-03-07 17:04:59 +0100399 };
400 for (set = 0; set < ARRAY_SIZE(additional_sets); ++set) {
401 *next_ptr = probe_jedec_generic(par, additional_sets[set].chip_size,
402 additional_sets[set].features, additional_sets[set].probe_timing);
403 if (*next_ptr)
404 next_ptr = &(*next_ptr)->next;
405 }
406
407 return ids;
Ollie Lho73eca802004-03-19 22:10:07 +0000408}
409
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000410static int erase_sector_jedec_common(struct flashctx *flash, unsigned int page,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000411 unsigned int pagesize, unsigned int mask)
Ollie Lho73eca802004-03-19 22:10:07 +0000412{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000413 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000414 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
Stefan Taunerf80419c2014-05-02 15:41:42 +0000415 unsigned int delay_us = 0;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000416
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000417 if(flash->chip->probe_timing != TIMING_ZERO)
Stefan Taunerf80419c2014-05-02 15:41:42 +0000418 delay_us = 10;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000419
Ollie Lho761bf1b2004-03-20 16:46:10 +0000420 /* Issue the Sector Erase command */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000421 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000422 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000423 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000424 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000425 chip_writeb(flash, 0x80, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000426 programmer_delay(delay_us);
Ollie Lhoefa28582004-12-08 20:10:01 +0000427
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000428 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000429 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000430 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000431 programmer_delay(delay_us);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000432 chip_writeb(flash, 0x30, bios + page);
Michael Karcher880e8672011-04-15 00:03:37 +0000433 programmer_delay(delay_us);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000434
Ollie Lho73eca802004-03-19 22:10:07 +0000435 /* wait for Toggle bit ready */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000436 toggle_ready_jedec_slow(flash, bios);
Ollie Lho73eca802004-03-19 22:10:07 +0000437
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000438 /* FIXME: Check the status register for errors. */
Uwe Hermannffec5f32007-08-23 16:08:21 +0000439 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000440}
Ollie Lho98bea8a2004-12-07 03:15:51 +0000441
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000442static int erase_block_jedec_common(struct flashctx *flash, unsigned int block,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000443 unsigned int blocksize, unsigned int mask)
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000444{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000445 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000446 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
Stefan Taunerf80419c2014-05-02 15:41:42 +0000447 unsigned int delay_us = 0;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000448
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000449 if(flash->chip->probe_timing != TIMING_ZERO)
Stefan Taunerf80419c2014-05-02 15:41:42 +0000450 delay_us = 10;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000451
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000452 /* Issue the Sector Erase command */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000453 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000454 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000455 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000456 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000457 chip_writeb(flash, 0x80, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000458 programmer_delay(delay_us);
Ollie Lhoefa28582004-12-08 20:10:01 +0000459
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000460 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000461 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000462 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000463 programmer_delay(delay_us);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000464 chip_writeb(flash, 0x50, bios + block);
Michael Karcher880e8672011-04-15 00:03:37 +0000465 programmer_delay(delay_us);
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000466
467 /* wait for Toggle bit ready */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000468 toggle_ready_jedec_slow(flash, bios);
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000469
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000470 /* FIXME: Check the status register for errors. */
Uwe Hermannffec5f32007-08-23 16:08:21 +0000471 return 0;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000472}
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000473
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000474static int erase_chip_jedec_common(struct flashctx *flash, unsigned int mask)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000475{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000476 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000477 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
Stefan Taunerf80419c2014-05-02 15:41:42 +0000478 unsigned int delay_us = 0;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000479
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000480 if(flash->chip->probe_timing != TIMING_ZERO)
Stefan Taunerf80419c2014-05-02 15:41:42 +0000481 delay_us = 10;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000482
Ollie Lho761bf1b2004-03-20 16:46:10 +0000483 /* Issue the JEDEC Chip Erase command */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000484 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000485 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000486 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000487 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000488 chip_writeb(flash, 0x80, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000489 programmer_delay(delay_us);
Ollie Lhoefa28582004-12-08 20:10:01 +0000490
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000491 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000492 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000493 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000494 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000495 chip_writeb(flash, 0x10, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000496 programmer_delay(delay_us);
Ollie Lho73eca802004-03-19 22:10:07 +0000497
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000498 toggle_ready_jedec_slow(flash, bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000499
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000500 /* FIXME: Check the status register for errors. */
Uwe Hermannffec5f32007-08-23 16:08:21 +0000501 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000502}
503
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000504static int write_byte_program_jedec_common(const struct flashctx *flash, const uint8_t *src,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000505 chipaddr dst, unsigned int mask)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000506{
507 int tried = 0, failed = 0;
508 chipaddr bios = flash->virtual_memory;
509
510 /* If the data is 0xFF, don't program it and don't complain. */
511 if (*src == 0xFF) {
512 return 0;
513 }
514
515retry:
516 /* Issue JEDEC Byte Program command */
517 start_program_jedec_common(flash, mask);
518
519 /* transfer data from source to destination */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000520 chip_writeb(flash, *src, dst);
521 toggle_ready_jedec(flash, bios);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000522
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000523 if (chip_readb(flash, dst) != *src && tried++ < MAX_REFLASH_TRIES) {
Sean Nelsonc57a9202010-01-04 17:15:23 +0000524 goto retry;
525 }
526
527 if (tried >= MAX_REFLASH_TRIES)
528 failed = 1;
529
530 return failed;
531}
532
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000533/* chunksize is 1 */
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000534int write_jedec_1(struct flashctx *flash, const uint8_t *src, unsigned int start,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000535 unsigned int len)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000536{
Nico Huber519be662018-12-23 20:03:35 +0100537 unsigned int i;
538 int failed = 0;
Carl-Daniel Hailfingerb30a5ed2010-10-10 14:02:27 +0000539 chipaddr dst = flash->virtual_memory + start;
Sean Nelsonc57a9202010-01-04 17:15:23 +0000540 chipaddr olddst;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000541 unsigned int mask;
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000542
Nico Huber2e947462026-03-07 17:04:59 +0100543 mask = getaddrmask(flash);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000544
545 olddst = dst;
Carl-Daniel Hailfingerb30a5ed2010-10-10 14:02:27 +0000546 for (i = 0; i < len; i++) {
Sean Nelsonc57a9202010-01-04 17:15:23 +0000547 if (write_byte_program_jedec_common(flash, src, dst, mask))
548 failed = 1;
549 dst++, src++;
Richard Hughes842d6782021-01-15 09:48:12 +0000550 flashprog_progress_add(flash, 1);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000551 }
552 if (failed)
Stefan Taunerc2333752013-07-13 23:31:37 +0000553 msg_cerr(" writing sector at 0x%" PRIxPTR " failed!\n", olddst);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000554
555 return failed;
556}
557
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000558static int write_page_write_jedec_common(struct flashctx *flash, const uint8_t *src,
Stefan Tauner0554ca52013-07-25 22:54:25 +0000559 unsigned int start, unsigned int page_size)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000560{
Nico Huber519be662018-12-23 20:03:35 +0100561 unsigned int i;
562 int tried = 0, failed;
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000563 const uint8_t *s = src;
Urja Rannikko0c854c02009-06-25 13:57:31 +0000564 chipaddr bios = flash->virtual_memory;
565 chipaddr dst = bios + start;
566 chipaddr d = dst;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000567 unsigned int mask;
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000568
Nico Huber2e947462026-03-07 17:04:59 +0100569 mask = getaddrmask(flash);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000570
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000571retry:
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000572 /* Issue JEDEC Start Program command */
Sean Nelsonc57a9202010-01-04 17:15:23 +0000573 start_program_jedec_common(flash, mask);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000574
Ollie Lho98bea8a2004-12-07 03:15:51 +0000575 /* transfer data from source to destination */
Carl-Daniel Hailfinger8a8a2262009-11-14 03:48:33 +0000576 for (i = 0; i < page_size; i++) {
Ollie Lho98bea8a2004-12-07 03:15:51 +0000577 /* If the data is 0xFF, don't program it */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000578 if (*src != 0xFF)
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000579 chip_writeb(flash, *src, dst);
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000580 dst++;
581 src++;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000582 }
583
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000584 toggle_ready_jedec(flash, dst - 1);
Ollie Lho98bea8a2004-12-07 03:15:51 +0000585
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000586 dst = d;
587 src = s;
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000588 failed = verify_range(flash, src, start, page_size);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000589
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000590 if (failed && tried++ < MAX_REFLASH_TRIES) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000591 msg_cerr("retrying.\n");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000592 goto retry;
593 }
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000594 if (failed) {
Stefan Taunerc2333752013-07-13 23:31:37 +0000595 msg_cerr(" page 0x%" PRIxPTR " failed!\n", (d - bios) / page_size);
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000596 }
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000597 return failed;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000598}
599
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000600/* chunksize is page_size */
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000601/*
602 * Write a part of the flash chip.
603 * FIXME: Use the chunk code from Michael Karcher instead.
604 * This function is a slightly modified copy of spi_write_chunked.
605 * Each page is written separately in chunks with a maximum size of chunksize.
606 */
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000607int write_jedec(struct flashctx *flash, const uint8_t *buf, unsigned int start,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000608 int unsigned len)
Carl-Daniel Hailfinger4bf4e792010-01-09 03:15:50 +0000609{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000610 unsigned int i, starthere, lenhere;
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000611 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000612 * in struct flashctx to do this properly. All chips using
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000613 * write_jedec have page_size set to max_writechunk_size, so
614 * we're OK for now.
615 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000616 unsigned int page_size = flash->chip->page_size;
Richard Hughes842d6782021-01-15 09:48:12 +0000617 unsigned int nwrites = (start + len - 1) / page_size;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000618
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000619 /* Warning: This loop has a very unusual condition and body.
620 * The loop needs to go through each page with at least one affected
621 * byte. The lowest page number is (start / page_size) since that
622 * division rounds down. The highest page number we want is the page
623 * where the last byte of the range lives. That last byte has the
624 * address (start + len - 1), thus the highest page number is
625 * (start + len - 1) / page_size. Since we want to include that last
626 * page as well, the loop condition uses <=.
627 */
Richard Hughes842d6782021-01-15 09:48:12 +0000628 for (i = start / page_size; i <= nwrites; i++) {
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000629 /* Byte position of the first byte in the range in this page. */
630 /* starthere is an offset to the base address of the chip. */
631 starthere = max(start, i * page_size);
632 /* Length of bytes in the range in this page. */
633 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Sean Nelson35727f72010-01-28 23:55:12 +0000634
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000635 if (write_page_write_jedec_common(flash, buf + starthere - start, starthere, lenhere))
636 return 1;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000637 }
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000638
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000639 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000640}
Michael Karcher1c296ca2009-11-27 17:49:42 +0000641
Sean Nelsonc57a9202010-01-04 17:15:23 +0000642/* erase chip with block_erase() prototype */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000643int erase_chip_block_jedec(struct flashctx *flash, unsigned int addr,
Sean Nelsonc57a9202010-01-04 17:15:23 +0000644 unsigned int blocksize)
645{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000646 unsigned int mask;
Sean Nelson35727f72010-01-28 23:55:12 +0000647
Nico Huber2e947462026-03-07 17:04:59 +0100648 mask = getaddrmask(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000649 if ((addr != 0) || (blocksize != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000650 msg_cerr("%s called with incorrect arguments\n",
Sean Nelsonc57a9202010-01-04 17:15:23 +0000651 __func__);
652 return -1;
653 }
Sean Nelson35727f72010-01-28 23:55:12 +0000654 return erase_chip_jedec_common(flash, mask);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000655}
656
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000657int erase_sector_jedec(struct flashctx *flash, unsigned int page,
658 unsigned int size)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000659{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000660 unsigned int mask;
Sean Nelson35727f72010-01-28 23:55:12 +0000661
Nico Huber2e947462026-03-07 17:04:59 +0100662 mask = getaddrmask(flash);
Sean Nelson35727f72010-01-28 23:55:12 +0000663 return erase_sector_jedec_common(flash, page, size, mask);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000664}
665
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000666int erase_block_jedec(struct flashctx *flash, unsigned int page,
667 unsigned int size)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000668{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000669 unsigned int mask;
Sean Nelson35727f72010-01-28 23:55:12 +0000670
Nico Huber2e947462026-03-07 17:04:59 +0100671 mask = getaddrmask(flash);
Sean Nelson35727f72010-01-28 23:55:12 +0000672 return erase_block_jedec_common(flash, page, size, mask);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000673}
674
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000675struct unlockblock {
676 unsigned int size;
677 unsigned int count;
678};
679
680typedef int (*unlockblock_func)(const struct flashctx *flash, chipaddr offset);
681static int regspace2_walk_unlockblocks(const struct flashctx *flash, const struct unlockblock *block, unlockblock_func func)
682{
683 chipaddr off = flash->virtual_registers + 2;
684 while (block->count != 0) {
685 unsigned int j;
686 for (j = 0; j < block->count; j++) {
687 if (func(flash, off))
688 return -1;
689 off += block->size;
690 }
691 block++;
692 }
693 return 0;
694}
695
696#define REG2_RWLOCK ((1 << 2) | (1 << 0))
697#define REG2_LOCKDOWN (1 << 1)
698#define REG2_MASK (REG2_RWLOCK | REG2_LOCKDOWN)
699
Stefan Tauner5859ced2014-12-20 16:45:31 +0000700static int printlock_regspace2_block(const struct flashctx *flash, chipaddr lockreg)
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000701{
Stefan Tauner5859ced2014-12-20 16:45:31 +0000702 uint8_t state = chip_readb(flash, lockreg);
703 msg_cdbg("Lock status of block at 0x%0*" PRIxPTR " is ", PRIxPTR_WIDTH, lockreg);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000704 switch (state & REG2_MASK) {
705 case 0:
706 msg_cdbg("Full Access.\n");
707 break;
708 case 1:
709 msg_cdbg("Write Lock (Default State).\n");
710 break;
711 case 2:
712 msg_cdbg("Locked Open (Full Access, Locked Down).\n");
713 break;
714 case 3:
715 msg_cdbg("Write Lock, Locked Down.\n");
716 break;
717 case 4:
718 msg_cdbg("Read Lock.\n");
719 break;
720 case 5:
721 msg_cdbg("Read/Write Lock.\n");
722 break;
723 case 6:
724 msg_cdbg("Read Lock, Locked Down.\n");
725 break;
726 case 7:
727 msg_cdbg("Read/Write Lock, Locked Down.\n");
728 break;
729 }
730 return 0;
731}
732
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000733static int printlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
734{
735 const unsigned int elems = flash->chip->total_size * 1024 / block_size;
736 struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
737 return regspace2_walk_unlockblocks(flash, blocks, &printlock_regspace2_block);
738}
739
740int printlock_regspace2_uniform_64k(struct flashctx *flash)
741{
742 return printlock_regspace2_uniform(flash, 64 * 1024);
743}
744
745int printlock_regspace2_block_eraser_0(struct flashctx *flash)
746{
747 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
748 const struct unlockblock *unlockblocks =
749 (const struct unlockblock *)flash->chip->block_erasers[0].eraseblocks;
750 return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block);
751}
752
753int printlock_regspace2_block_eraser_1(struct flashctx *flash)
754{
755 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
756 const struct unlockblock *unlockblocks =
757 (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks;
758 return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block);
759}
760
Stefan Tauner5859ced2014-12-20 16:45:31 +0000761/* Try to change the lock register at address lockreg from cur to new.
762 *
763 * - Try to unlock the lock bit if requested and it is currently set (although this is probably futile).
764 * - Try to change the read/write bits if requested.
765 * - Try to set the lockdown bit if requested.
766 * Return an error immediately if any of this fails. */
767static int changelock_regspace2_block(const struct flashctx *flash, chipaddr lockreg, uint8_t cur, uint8_t new)
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000768{
Stefan Tauner5859ced2014-12-20 16:45:31 +0000769 /* Only allow changes to known read/write/lockdown bits */
770 if (((cur ^ new) & ~REG2_MASK) != 0) {
771 msg_cerr("Invalid lock change from 0x%02x to 0x%02x requested at 0x%0*" PRIxPTR "!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200772 "Please report a bug at flashprog@flashprog.org\n",
Stefan Tauner5859ced2014-12-20 16:45:31 +0000773 cur, new, PRIxPTR_WIDTH, lockreg);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000774 return -1;
775 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000776
777 /* Exit early if no change (of read/write/lockdown bits) was requested. */
778 if (((cur ^ new) & REG2_MASK) == 0) {
779 msg_cdbg2("Lock bits at 0x%0*" PRIxPTR " not changed.\n", PRIxPTR_WIDTH, lockreg);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000780 return 0;
781 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000782
783 /* Normally the lockdown bit can not be cleared. Try nevertheless if requested. */
784 if ((cur & REG2_LOCKDOWN) && !(new & REG2_LOCKDOWN)) {
785 chip_writeb(flash, cur & ~REG2_LOCKDOWN, lockreg);
786 cur = chip_readb(flash, lockreg);
787 if ((cur & REG2_LOCKDOWN) == REG2_LOCKDOWN) {
788 msg_cwarn("Lockdown can't be removed at 0x%0*" PRIxPTR "! New value: 0x%02x.\n",
789 PRIxPTR_WIDTH, lockreg, cur);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000790 return -1;
791 }
792 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000793
794 /* Change read and/or write bit */
795 if ((cur ^ new) & REG2_RWLOCK) {
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000796 /* Do not lockdown yet. */
Stefan Tauner5859ced2014-12-20 16:45:31 +0000797 uint8_t wanted = (cur & ~REG2_RWLOCK) | (new & REG2_RWLOCK);
798 chip_writeb(flash, wanted, lockreg);
799 cur = chip_readb(flash, lockreg);
800 if (cur != wanted) {
801 msg_cerr("Changing lock bits failed at 0x%0*" PRIxPTR "! New value: 0x%02x.\n",
802 PRIxPTR_WIDTH, lockreg, cur);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000803 return -1;
804 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000805 msg_cdbg("Changed lock bits at 0x%0*" PRIxPTR " to 0x%02x.\n",
806 PRIxPTR_WIDTH, lockreg, cur);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000807 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000808
809 /* Eventually, enable lockdown if requested. */
810 if (!(cur & REG2_LOCKDOWN) && (new & REG2_LOCKDOWN)) {
811 chip_writeb(flash, new, lockreg);
812 cur = chip_readb(flash, lockreg);
813 if (cur != new) {
814 msg_cerr("Enabling lockdown FAILED at 0x%0*" PRIxPTR "! New value: 0x%02x.\n",
815 PRIxPTR_WIDTH, lockreg, cur);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000816 return -1;
817 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000818 msg_cdbg("Enabled lockdown at 0x%0*" PRIxPTR ".\n", PRIxPTR_WIDTH, lockreg);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000819 }
820
821 return 0;
822}
823
Stefan Tauner5859ced2014-12-20 16:45:31 +0000824static int unlock_regspace2_block_generic(const struct flashctx *flash, chipaddr lockreg)
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000825{
Stefan Tauner5859ced2014-12-20 16:45:31 +0000826 uint8_t old = chip_readb(flash, lockreg);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000827 /* We don't care for the lockdown bit as long as the RW locks are 0 after we're done */
Stefan Tauner5859ced2014-12-20 16:45:31 +0000828 return changelock_regspace2_block(flash, lockreg, old, old & ~REG2_RWLOCK);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000829}
830
831static int unlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
832{
833 const unsigned int elems = flash->chip->total_size * 1024 / block_size;
834 struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
Stefan Tauner5859ced2014-12-20 16:45:31 +0000835 return regspace2_walk_unlockblocks(flash, blocks, &unlock_regspace2_block_generic);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000836}
837
838int unlock_regspace2_uniform_64k(struct flashctx *flash)
839{
840 return unlock_regspace2_uniform(flash, 64 * 1024);
841}
842
843int unlock_regspace2_uniform_32k(struct flashctx *flash)
844{
845 return unlock_regspace2_uniform(flash, 32 * 1024);
846}
847
848int unlock_regspace2_block_eraser_0(struct flashctx *flash)
849{
850 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
851 const struct unlockblock *unlockblocks =
852 (const struct unlockblock *)flash->chip->block_erasers[0].eraseblocks;
Stefan Tauner5859ced2014-12-20 16:45:31 +0000853 return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block_generic);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000854}
855
856int unlock_regspace2_block_eraser_1(struct flashctx *flash)
857{
858 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
859 const struct unlockblock *unlockblocks =
860 (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks;
Stefan Tauner5859ced2014-12-20 16:45:31 +0000861 return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block_generic);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000862}