blob: 3fbfd2b53465b525875f42b3fce03dc1df278bdc [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 Huber2e947462026-03-07 17:04:59 +0100284 largeid1 |= raw[idx++] = par->chip_readb(par, bios + 0x100);
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 Huber2e947462026-03-07 17:04:59 +0100288 largeid2 |= raw[idx++] = par->chip_readb(par, bios + 0x101);
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 Huber2e947462026-03-07 17:04:59 +0100312 flashcontent1 |= par->chip_readb(par, bios + 0x100);
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000313 }
314 if (flashcontent2 == 0x7F) {
315 flashcontent2 <<= 8;
Nico Huber2e947462026-03-07 17:04:59 +0100316 flashcontent2 |= par->chip_readb(par, bios + 0x101);
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[] = {
391 { 2*MiB, FEATURE_SHORT_RESET, TIMING_ZERO },
392 { 2*MiB, FEATURE_LONG_RESET | FEATURE_ADDR_SHIFTED, 10 },
393 { 512*KiB, FEATURE_LONG_RESET | FEATURE_ADDR_SHIFTED, 10 },
394 { 384*KiB, FEATURE_LONG_RESET, 1 },
395 { 256*KiB, FEATURE_LONG_RESET | FEATURE_ADDR_2AA, TIMING_FIXME },
396 { 256*KiB, FEATURE_LONG_RESET | FEATURE_ADDR_AAA, TIMING_ZERO },
397 { 128*KiB, FEATURE_SHORT_RESET, TIMING_ZERO },
398 };
399 for (set = 0; set < ARRAY_SIZE(additional_sets); ++set) {
400 *next_ptr = probe_jedec_generic(par, additional_sets[set].chip_size,
401 additional_sets[set].features, additional_sets[set].probe_timing);
402 if (*next_ptr)
403 next_ptr = &(*next_ptr)->next;
404 }
405
406 return ids;
Ollie Lho73eca802004-03-19 22:10:07 +0000407}
408
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000409static int erase_sector_jedec_common(struct flashctx *flash, unsigned int page,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000410 unsigned int pagesize, unsigned int mask)
Ollie Lho73eca802004-03-19 22:10:07 +0000411{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000412 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000413 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
Stefan Taunerf80419c2014-05-02 15:41:42 +0000414 unsigned int delay_us = 0;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000415
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000416 if(flash->chip->probe_timing != TIMING_ZERO)
Stefan Taunerf80419c2014-05-02 15:41:42 +0000417 delay_us = 10;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000418
Ollie Lho761bf1b2004-03-20 16:46:10 +0000419 /* Issue the Sector Erase command */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000420 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000421 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000422 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000423 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000424 chip_writeb(flash, 0x80, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000425 programmer_delay(delay_us);
Ollie Lhoefa28582004-12-08 20:10:01 +0000426
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000427 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000428 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000429 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000430 programmer_delay(delay_us);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000431 chip_writeb(flash, 0x30, bios + page);
Michael Karcher880e8672011-04-15 00:03:37 +0000432 programmer_delay(delay_us);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000433
Ollie Lho73eca802004-03-19 22:10:07 +0000434 /* wait for Toggle bit ready */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000435 toggle_ready_jedec_slow(flash, bios);
Ollie Lho73eca802004-03-19 22:10:07 +0000436
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000437 /* FIXME: Check the status register for errors. */
Uwe Hermannffec5f32007-08-23 16:08:21 +0000438 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000439}
Ollie Lho98bea8a2004-12-07 03:15:51 +0000440
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000441static int erase_block_jedec_common(struct flashctx *flash, unsigned int block,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000442 unsigned int blocksize, unsigned int mask)
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000443{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000444 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000445 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
Stefan Taunerf80419c2014-05-02 15:41:42 +0000446 unsigned int delay_us = 0;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000447
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000448 if(flash->chip->probe_timing != TIMING_ZERO)
Stefan Taunerf80419c2014-05-02 15:41:42 +0000449 delay_us = 10;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000450
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000451 /* Issue the Sector Erase command */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000452 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000453 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000454 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000455 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000456 chip_writeb(flash, 0x80, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000457 programmer_delay(delay_us);
Ollie Lhoefa28582004-12-08 20:10:01 +0000458
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000459 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000460 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000461 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000462 programmer_delay(delay_us);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000463 chip_writeb(flash, 0x50, bios + block);
Michael Karcher880e8672011-04-15 00:03:37 +0000464 programmer_delay(delay_us);
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000465
466 /* wait for Toggle bit ready */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000467 toggle_ready_jedec_slow(flash, bios);
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000468
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000469 /* FIXME: Check the status register for errors. */
Uwe Hermannffec5f32007-08-23 16:08:21 +0000470 return 0;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000471}
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000472
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000473static int erase_chip_jedec_common(struct flashctx *flash, unsigned int mask)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000474{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000475 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000476 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
Stefan Taunerf80419c2014-05-02 15:41:42 +0000477 unsigned int delay_us = 0;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000478
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000479 if(flash->chip->probe_timing != TIMING_ZERO)
Stefan Taunerf80419c2014-05-02 15:41:42 +0000480 delay_us = 10;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000481
Ollie Lho761bf1b2004-03-20 16:46:10 +0000482 /* Issue the JEDEC Chip Erase command */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000483 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000484 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000485 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000486 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000487 chip_writeb(flash, 0x80, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000488 programmer_delay(delay_us);
Ollie Lhoefa28582004-12-08 20:10:01 +0000489
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000490 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000491 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000492 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000493 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000494 chip_writeb(flash, 0x10, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000495 programmer_delay(delay_us);
Ollie Lho73eca802004-03-19 22:10:07 +0000496
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000497 toggle_ready_jedec_slow(flash, bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000498
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000499 /* FIXME: Check the status register for errors. */
Uwe Hermannffec5f32007-08-23 16:08:21 +0000500 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000501}
502
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000503static int write_byte_program_jedec_common(const struct flashctx *flash, const uint8_t *src,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000504 chipaddr dst, unsigned int mask)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000505{
506 int tried = 0, failed = 0;
507 chipaddr bios = flash->virtual_memory;
508
509 /* If the data is 0xFF, don't program it and don't complain. */
510 if (*src == 0xFF) {
511 return 0;
512 }
513
514retry:
515 /* Issue JEDEC Byte Program command */
516 start_program_jedec_common(flash, mask);
517
518 /* transfer data from source to destination */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000519 chip_writeb(flash, *src, dst);
520 toggle_ready_jedec(flash, bios);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000521
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000522 if (chip_readb(flash, dst) != *src && tried++ < MAX_REFLASH_TRIES) {
Sean Nelsonc57a9202010-01-04 17:15:23 +0000523 goto retry;
524 }
525
526 if (tried >= MAX_REFLASH_TRIES)
527 failed = 1;
528
529 return failed;
530}
531
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000532/* chunksize is 1 */
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000533int write_jedec_1(struct flashctx *flash, const uint8_t *src, unsigned int start,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000534 unsigned int len)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000535{
Nico Huber519be662018-12-23 20:03:35 +0100536 unsigned int i;
537 int failed = 0;
Carl-Daniel Hailfingerb30a5ed2010-10-10 14:02:27 +0000538 chipaddr dst = flash->virtual_memory + start;
Sean Nelsonc57a9202010-01-04 17:15:23 +0000539 chipaddr olddst;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000540 unsigned int mask;
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000541
Nico Huber2e947462026-03-07 17:04:59 +0100542 mask = getaddrmask(flash);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000543
544 olddst = dst;
Carl-Daniel Hailfingerb30a5ed2010-10-10 14:02:27 +0000545 for (i = 0; i < len; i++) {
Sean Nelsonc57a9202010-01-04 17:15:23 +0000546 if (write_byte_program_jedec_common(flash, src, dst, mask))
547 failed = 1;
548 dst++, src++;
Richard Hughes842d6782021-01-15 09:48:12 +0000549 flashprog_progress_add(flash, 1);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000550 }
551 if (failed)
Stefan Taunerc2333752013-07-13 23:31:37 +0000552 msg_cerr(" writing sector at 0x%" PRIxPTR " failed!\n", olddst);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000553
554 return failed;
555}
556
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000557static int write_page_write_jedec_common(struct flashctx *flash, const uint8_t *src,
Stefan Tauner0554ca52013-07-25 22:54:25 +0000558 unsigned int start, unsigned int page_size)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000559{
Nico Huber519be662018-12-23 20:03:35 +0100560 unsigned int i;
561 int tried = 0, failed;
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000562 const uint8_t *s = src;
Urja Rannikko0c854c02009-06-25 13:57:31 +0000563 chipaddr bios = flash->virtual_memory;
564 chipaddr dst = bios + start;
565 chipaddr d = dst;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000566 unsigned int mask;
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000567
Nico Huber2e947462026-03-07 17:04:59 +0100568 mask = getaddrmask(flash);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000569
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000570retry:
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000571 /* Issue JEDEC Start Program command */
Sean Nelsonc57a9202010-01-04 17:15:23 +0000572 start_program_jedec_common(flash, mask);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000573
Ollie Lho98bea8a2004-12-07 03:15:51 +0000574 /* transfer data from source to destination */
Carl-Daniel Hailfinger8a8a2262009-11-14 03:48:33 +0000575 for (i = 0; i < page_size; i++) {
Ollie Lho98bea8a2004-12-07 03:15:51 +0000576 /* If the data is 0xFF, don't program it */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000577 if (*src != 0xFF)
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000578 chip_writeb(flash, *src, dst);
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000579 dst++;
580 src++;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000581 }
582
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000583 toggle_ready_jedec(flash, dst - 1);
Ollie Lho98bea8a2004-12-07 03:15:51 +0000584
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000585 dst = d;
586 src = s;
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000587 failed = verify_range(flash, src, start, page_size);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000588
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000589 if (failed && tried++ < MAX_REFLASH_TRIES) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000590 msg_cerr("retrying.\n");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000591 goto retry;
592 }
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000593 if (failed) {
Stefan Taunerc2333752013-07-13 23:31:37 +0000594 msg_cerr(" page 0x%" PRIxPTR " failed!\n", (d - bios) / page_size);
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000595 }
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000596 return failed;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000597}
598
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000599/* chunksize is page_size */
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000600/*
601 * Write a part of the flash chip.
602 * FIXME: Use the chunk code from Michael Karcher instead.
603 * This function is a slightly modified copy of spi_write_chunked.
604 * Each page is written separately in chunks with a maximum size of chunksize.
605 */
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000606int write_jedec(struct flashctx *flash, const uint8_t *buf, unsigned int start,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000607 int unsigned len)
Carl-Daniel Hailfinger4bf4e792010-01-09 03:15:50 +0000608{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000609 unsigned int i, starthere, lenhere;
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000610 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000611 * in struct flashctx to do this properly. All chips using
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000612 * write_jedec have page_size set to max_writechunk_size, so
613 * we're OK for now.
614 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000615 unsigned int page_size = flash->chip->page_size;
Richard Hughes842d6782021-01-15 09:48:12 +0000616 unsigned int nwrites = (start + len - 1) / page_size;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000617
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000618 /* Warning: This loop has a very unusual condition and body.
619 * The loop needs to go through each page with at least one affected
620 * byte. The lowest page number is (start / page_size) since that
621 * division rounds down. The highest page number we want is the page
622 * where the last byte of the range lives. That last byte has the
623 * address (start + len - 1), thus the highest page number is
624 * (start + len - 1) / page_size. Since we want to include that last
625 * page as well, the loop condition uses <=.
626 */
Richard Hughes842d6782021-01-15 09:48:12 +0000627 for (i = start / page_size; i <= nwrites; i++) {
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000628 /* Byte position of the first byte in the range in this page. */
629 /* starthere is an offset to the base address of the chip. */
630 starthere = max(start, i * page_size);
631 /* Length of bytes in the range in this page. */
632 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Sean Nelson35727f72010-01-28 23:55:12 +0000633
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000634 if (write_page_write_jedec_common(flash, buf + starthere - start, starthere, lenhere))
635 return 1;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000636 }
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000637
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000638 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000639}
Michael Karcher1c296ca2009-11-27 17:49:42 +0000640
Sean Nelsonc57a9202010-01-04 17:15:23 +0000641/* erase chip with block_erase() prototype */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000642int erase_chip_block_jedec(struct flashctx *flash, unsigned int addr,
Sean Nelsonc57a9202010-01-04 17:15:23 +0000643 unsigned int blocksize)
644{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000645 unsigned int mask;
Sean Nelson35727f72010-01-28 23:55:12 +0000646
Nico Huber2e947462026-03-07 17:04:59 +0100647 mask = getaddrmask(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000648 if ((addr != 0) || (blocksize != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000649 msg_cerr("%s called with incorrect arguments\n",
Sean Nelsonc57a9202010-01-04 17:15:23 +0000650 __func__);
651 return -1;
652 }
Sean Nelson35727f72010-01-28 23:55:12 +0000653 return erase_chip_jedec_common(flash, mask);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000654}
655
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000656int erase_sector_jedec(struct flashctx *flash, unsigned int page,
657 unsigned int size)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000658{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000659 unsigned int mask;
Sean Nelson35727f72010-01-28 23:55:12 +0000660
Nico Huber2e947462026-03-07 17:04:59 +0100661 mask = getaddrmask(flash);
Sean Nelson35727f72010-01-28 23:55:12 +0000662 return erase_sector_jedec_common(flash, page, size, mask);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000663}
664
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000665int erase_block_jedec(struct flashctx *flash, unsigned int page,
666 unsigned int size)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000667{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000668 unsigned int mask;
Sean Nelson35727f72010-01-28 23:55:12 +0000669
Nico Huber2e947462026-03-07 17:04:59 +0100670 mask = getaddrmask(flash);
Sean Nelson35727f72010-01-28 23:55:12 +0000671 return erase_block_jedec_common(flash, page, size, mask);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000672}
673
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000674struct unlockblock {
675 unsigned int size;
676 unsigned int count;
677};
678
679typedef int (*unlockblock_func)(const struct flashctx *flash, chipaddr offset);
680static int regspace2_walk_unlockblocks(const struct flashctx *flash, const struct unlockblock *block, unlockblock_func func)
681{
682 chipaddr off = flash->virtual_registers + 2;
683 while (block->count != 0) {
684 unsigned int j;
685 for (j = 0; j < block->count; j++) {
686 if (func(flash, off))
687 return -1;
688 off += block->size;
689 }
690 block++;
691 }
692 return 0;
693}
694
695#define REG2_RWLOCK ((1 << 2) | (1 << 0))
696#define REG2_LOCKDOWN (1 << 1)
697#define REG2_MASK (REG2_RWLOCK | REG2_LOCKDOWN)
698
Stefan Tauner5859ced2014-12-20 16:45:31 +0000699static int printlock_regspace2_block(const struct flashctx *flash, chipaddr lockreg)
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000700{
Stefan Tauner5859ced2014-12-20 16:45:31 +0000701 uint8_t state = chip_readb(flash, lockreg);
702 msg_cdbg("Lock status of block at 0x%0*" PRIxPTR " is ", PRIxPTR_WIDTH, lockreg);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000703 switch (state & REG2_MASK) {
704 case 0:
705 msg_cdbg("Full Access.\n");
706 break;
707 case 1:
708 msg_cdbg("Write Lock (Default State).\n");
709 break;
710 case 2:
711 msg_cdbg("Locked Open (Full Access, Locked Down).\n");
712 break;
713 case 3:
714 msg_cdbg("Write Lock, Locked Down.\n");
715 break;
716 case 4:
717 msg_cdbg("Read Lock.\n");
718 break;
719 case 5:
720 msg_cdbg("Read/Write Lock.\n");
721 break;
722 case 6:
723 msg_cdbg("Read Lock, Locked Down.\n");
724 break;
725 case 7:
726 msg_cdbg("Read/Write Lock, Locked Down.\n");
727 break;
728 }
729 return 0;
730}
731
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000732static int printlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
733{
734 const unsigned int elems = flash->chip->total_size * 1024 / block_size;
735 struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
736 return regspace2_walk_unlockblocks(flash, blocks, &printlock_regspace2_block);
737}
738
739int printlock_regspace2_uniform_64k(struct flashctx *flash)
740{
741 return printlock_regspace2_uniform(flash, 64 * 1024);
742}
743
744int printlock_regspace2_block_eraser_0(struct flashctx *flash)
745{
746 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
747 const struct unlockblock *unlockblocks =
748 (const struct unlockblock *)flash->chip->block_erasers[0].eraseblocks;
749 return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block);
750}
751
752int printlock_regspace2_block_eraser_1(struct flashctx *flash)
753{
754 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
755 const struct unlockblock *unlockblocks =
756 (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks;
757 return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block);
758}
759
Stefan Tauner5859ced2014-12-20 16:45:31 +0000760/* Try to change the lock register at address lockreg from cur to new.
761 *
762 * - Try to unlock the lock bit if requested and it is currently set (although this is probably futile).
763 * - Try to change the read/write bits if requested.
764 * - Try to set the lockdown bit if requested.
765 * Return an error immediately if any of this fails. */
766static int changelock_regspace2_block(const struct flashctx *flash, chipaddr lockreg, uint8_t cur, uint8_t new)
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000767{
Stefan Tauner5859ced2014-12-20 16:45:31 +0000768 /* Only allow changes to known read/write/lockdown bits */
769 if (((cur ^ new) & ~REG2_MASK) != 0) {
770 msg_cerr("Invalid lock change from 0x%02x to 0x%02x requested at 0x%0*" PRIxPTR "!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200771 "Please report a bug at flashprog@flashprog.org\n",
Stefan Tauner5859ced2014-12-20 16:45:31 +0000772 cur, new, PRIxPTR_WIDTH, lockreg);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000773 return -1;
774 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000775
776 /* Exit early if no change (of read/write/lockdown bits) was requested. */
777 if (((cur ^ new) & REG2_MASK) == 0) {
778 msg_cdbg2("Lock bits at 0x%0*" PRIxPTR " not changed.\n", PRIxPTR_WIDTH, lockreg);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000779 return 0;
780 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000781
782 /* Normally the lockdown bit can not be cleared. Try nevertheless if requested. */
783 if ((cur & REG2_LOCKDOWN) && !(new & REG2_LOCKDOWN)) {
784 chip_writeb(flash, cur & ~REG2_LOCKDOWN, lockreg);
785 cur = chip_readb(flash, lockreg);
786 if ((cur & REG2_LOCKDOWN) == REG2_LOCKDOWN) {
787 msg_cwarn("Lockdown can't be removed at 0x%0*" PRIxPTR "! New value: 0x%02x.\n",
788 PRIxPTR_WIDTH, lockreg, cur);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000789 return -1;
790 }
791 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000792
793 /* Change read and/or write bit */
794 if ((cur ^ new) & REG2_RWLOCK) {
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000795 /* Do not lockdown yet. */
Stefan Tauner5859ced2014-12-20 16:45:31 +0000796 uint8_t wanted = (cur & ~REG2_RWLOCK) | (new & REG2_RWLOCK);
797 chip_writeb(flash, wanted, lockreg);
798 cur = chip_readb(flash, lockreg);
799 if (cur != wanted) {
800 msg_cerr("Changing lock bits failed at 0x%0*" PRIxPTR "! New value: 0x%02x.\n",
801 PRIxPTR_WIDTH, lockreg, cur);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000802 return -1;
803 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000804 msg_cdbg("Changed lock bits at 0x%0*" PRIxPTR " to 0x%02x.\n",
805 PRIxPTR_WIDTH, lockreg, cur);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000806 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000807
808 /* Eventually, enable lockdown if requested. */
809 if (!(cur & REG2_LOCKDOWN) && (new & REG2_LOCKDOWN)) {
810 chip_writeb(flash, new, lockreg);
811 cur = chip_readb(flash, lockreg);
812 if (cur != new) {
813 msg_cerr("Enabling lockdown FAILED at 0x%0*" PRIxPTR "! New value: 0x%02x.\n",
814 PRIxPTR_WIDTH, lockreg, cur);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000815 return -1;
816 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000817 msg_cdbg("Enabled lockdown at 0x%0*" PRIxPTR ".\n", PRIxPTR_WIDTH, lockreg);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000818 }
819
820 return 0;
821}
822
Stefan Tauner5859ced2014-12-20 16:45:31 +0000823static int unlock_regspace2_block_generic(const struct flashctx *flash, chipaddr lockreg)
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000824{
Stefan Tauner5859ced2014-12-20 16:45:31 +0000825 uint8_t old = chip_readb(flash, lockreg);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000826 /* 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 +0000827 return changelock_regspace2_block(flash, lockreg, old, old & ~REG2_RWLOCK);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000828}
829
830static int unlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
831{
832 const unsigned int elems = flash->chip->total_size * 1024 / block_size;
833 struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
Stefan Tauner5859ced2014-12-20 16:45:31 +0000834 return regspace2_walk_unlockblocks(flash, blocks, &unlock_regspace2_block_generic);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000835}
836
837int unlock_regspace2_uniform_64k(struct flashctx *flash)
838{
839 return unlock_regspace2_uniform(flash, 64 * 1024);
840}
841
842int unlock_regspace2_uniform_32k(struct flashctx *flash)
843{
844 return unlock_regspace2_uniform(flash, 32 * 1024);
845}
846
847int unlock_regspace2_block_eraser_0(struct flashctx *flash)
848{
849 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
850 const struct unlockblock *unlockblocks =
851 (const struct unlockblock *)flash->chip->block_erasers[0].eraseblocks;
Stefan Tauner5859ced2014-12-20 16:45:31 +0000852 return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block_generic);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000853}
854
855int unlock_regspace2_block_eraser_1(struct flashctx *flash)
856{
857 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
858 const struct unlockblock *unlockblocks =
859 (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks;
Stefan Tauner5859ced2014-12-20 16:45:31 +0000860 return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block_generic);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000861}