blob: 358b8507dea47a6a18321f960889db82ef9e828a [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 *
Uwe Hermannd1107642007-08-29 17:52:32 +000021 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000024 */
25
26#include "flash.h"
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +000027#include "chipdrivers.h"
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000028
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +000029#define MAX_REFLASH_TRIES 0x10
Sean Nelsonc57a9202010-01-04 17:15:23 +000030#define MASK_FULL 0xffff
31#define MASK_2AA 0x7ff
Sean Nelson35727f72010-01-28 23:55:12 +000032#define MASK_AAA 0xfff
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +000033
Carl-Daniel Hailfingera758f512008-05-14 12:03:06 +000034/* Check one byte for odd parity */
35uint8_t oddparity(uint8_t val)
36{
37 val = (val ^ (val >> 4)) & 0xf;
38 val = (val ^ (val >> 2)) & 0x3;
39 return (val ^ (val >> 1)) & 0x1;
40}
41
Stefan Taunerf80419c2014-05-02 15:41:42 +000042static void toggle_ready_jedec_common(const struct flashctx *flash, chipaddr dst, unsigned int delay)
Uwe Hermann51582f22007-08-23 10:20:40 +000043{
44 unsigned int i = 0;
45 uint8_t tmp1, tmp2;
46
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000047 tmp1 = chip_readb(flash, dst) & 0x40;
Uwe Hermann51582f22007-08-23 10:20:40 +000048
49 while (i++ < 0xFFFFFFF) {
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000050 if (delay)
51 programmer_delay(delay);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000052 tmp2 = chip_readb(flash, dst) & 0x40;
Uwe Hermann51582f22007-08-23 10:20:40 +000053 if (tmp1 == tmp2) {
54 break;
55 }
56 tmp1 = tmp2;
57 }
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000058 if (i > 0x100000)
Sean Nelsoned479d22010-03-24 23:14:32 +000059 msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i);
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000060}
61
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000062void toggle_ready_jedec(const struct flashctx *flash, chipaddr dst)
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000063{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000064 toggle_ready_jedec_common(flash, dst, 0);
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000065}
66
67/* Some chips require a minimum delay between toggle bit reads.
68 * The Winbond W39V040C wants 50 ms between reads on sector erase toggle,
69 * but experiments show that 2 ms are already enough. Pick a safety factor
70 * of 4 and use an 8 ms delay.
71 * Given that erase is slow on all chips, it is recommended to use
72 * toggle_ready_jedec_slow in erase functions.
73 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000074static void toggle_ready_jedec_slow(const struct flashctx *flash, chipaddr dst)
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000075{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000076 toggle_ready_jedec_common(flash, dst, 8 * 1000);
Uwe Hermann51582f22007-08-23 10:20:40 +000077}
78
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000079void data_polling_jedec(const struct flashctx *flash, chipaddr dst,
80 uint8_t data)
Uwe Hermann51582f22007-08-23 10:20:40 +000081{
82 unsigned int i = 0;
83 uint8_t tmp;
84
85 data &= 0x80;
86
87 while (i++ < 0xFFFFFFF) {
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000088 tmp = chip_readb(flash, dst) & 0x80;
Uwe Hermann51582f22007-08-23 10:20:40 +000089 if (tmp == data) {
90 break;
91 }
92 }
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000093 if (i > 0x100000)
Sean Nelsoned479d22010-03-24 23:14:32 +000094 msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i);
Uwe Hermann51582f22007-08-23 10:20:40 +000095}
96
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +000097static unsigned int getaddrmask(const struct flashchip *chip)
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +000098{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +000099 switch (chip->feature_bits & FEATURE_ADDR_MASK) {
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000100 case FEATURE_ADDR_FULL:
101 return MASK_FULL;
102 break;
103 case FEATURE_ADDR_2AA:
104 return MASK_2AA;
105 break;
106 case FEATURE_ADDR_AAA:
107 return MASK_AAA;
108 break;
109 default:
110 msg_cerr("%s called with unknown mask\n", __func__);
111 return 0;
112 break;
113 }
114}
115
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000116static void start_program_jedec_common(const struct flashctx *flash, unsigned int mask)
Uwe Hermann51582f22007-08-23 10:20:40 +0000117{
Sean Nelsonc57a9202010-01-04 17:15:23 +0000118 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000119 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
120
121 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
122 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
123 chip_writeb(flash, 0xA0, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Uwe Hermann51582f22007-08-23 10:20:40 +0000124}
125
Stefan Tauner03a9c3c2014-08-03 14:15:14 +0000126int probe_jedec_29gl(struct flashctx *flash)
127{
128 unsigned int mask = getaddrmask(flash->chip);
129 chipaddr bios = flash->virtual_memory;
130 const struct flashchip *chip = flash->chip;
131
132 /* Reset chip to a clean slate */
133 chip_writeb(flash, 0xF0, bios + (0x5555 & mask));
134
135 /* Issue JEDEC Product ID Entry command */
136 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
137 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
138 chip_writeb(flash, 0x90, bios + (0x5555 & mask));
139
140 /* Read product ID */
141 // FIXME: Continuation loop, second byte is at word 0x100/byte 0x200
142 uint32_t man_id = chip_readb(flash, bios + 0x00);
143 uint32_t dev_id = (chip_readb(flash, bios + 0x01) << 16) |
144 (chip_readb(flash, bios + 0x0E) << 8) |
145 (chip_readb(flash, bios + 0x0F) << 0);
146
147 /* Issue JEDEC Product ID Exit command */
148 chip_writeb(flash, 0xF0, bios + (0x5555 & mask));
149
150 msg_cdbg("%s: man_id 0x%02x, dev_id 0x%06x", __func__, man_id, dev_id);
151 if (!oddparity(man_id))
152 msg_cdbg(", man_id parity violation");
153
154 /* Read the product ID location again. We should now see normal flash contents. */
155 uint32_t flashcontent1 = chip_readb(flash, bios + 0x00); // FIXME: Continuation loop
156 uint32_t flashcontent2 = (chip_readb(flash, bios + 0x01) << 16) |
157 (chip_readb(flash, bios + 0x0E) << 8) |
158 (chip_readb(flash, bios + 0x0F) << 0);
159
160 if (man_id == flashcontent1)
161 msg_cdbg(", man_id seems to be normal flash content");
162 if (dev_id == flashcontent2)
163 msg_cdbg(", dev_id seems to be normal flash content");
164
165 msg_cdbg("\n");
166 if (man_id != chip->manufacture_id || dev_id != chip->model_id)
167 return 0;
168
169 if (chip->feature_bits & FEATURE_REGISTERMAP)
170 map_flash_registers(flash);
171
172 return 1;
173}
174
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000175static int probe_jedec_common(struct flashctx *flash, unsigned int mask)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000176{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000177 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000178 const struct flashchip *chip = flash->chip;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000179 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
Ollie Lho184a4042005-11-26 21:55:36 +0000180 uint8_t id1, id2;
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000181 uint32_t largeid1, largeid2;
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000182 uint32_t flashcontent1, flashcontent2;
Stefan Taunerf80419c2014-05-02 15:41:42 +0000183 unsigned int probe_timing_enter, probe_timing_exit;
Maciej Pijankac6e11112009-06-03 14:46:22 +0000184
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000185 if (chip->probe_timing > 0)
186 probe_timing_enter = probe_timing_exit = chip->probe_timing;
187 else if (chip->probe_timing == TIMING_ZERO) { /* No delay. */
Maciej Pijankac6e11112009-06-03 14:46:22 +0000188 probe_timing_enter = probe_timing_exit = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000189 } else if (chip->probe_timing == TIMING_FIXME) { /* == _IGNORED */
Sean Nelsoned479d22010-03-24 23:14:32 +0000190 msg_cdbg("Chip lacks correct probe timing information, "
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000191 "using default 10mS/40uS. ");
Maciej Pijankac6e11112009-06-03 14:46:22 +0000192 probe_timing_enter = 10000;
193 probe_timing_exit = 40;
194 } else {
Sean Nelsoned479d22010-03-24 23:14:32 +0000195 msg_cerr("Chip has negative value in probe_timing, failing "
Maciej Pijankac6e11112009-06-03 14:46:22 +0000196 "without chip access\n");
197 return 0;
198 }
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000199
Sean Nelsonf59e2632010-10-20 21:13:19 +0000200 /* Earlier probes might have been too fast for the chip to enter ID
201 * mode completely. Allow the chip to finish this before seeing a
202 * reset command.
203 */
204 if (probe_timing_enter)
205 programmer_delay(probe_timing_enter);
206 /* Reset chip to a clean slate */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000207 if ((chip->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET)
Sean Nelsonf59e2632010-10-20 21:13:19 +0000208 {
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000209 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonf59e2632010-10-20 21:13:19 +0000210 if (probe_timing_exit)
211 programmer_delay(10);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000212 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Sean Nelsonf59e2632010-10-20 21:13:19 +0000213 if (probe_timing_exit)
214 programmer_delay(10);
215 }
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000216 chip_writeb(flash, 0xF0, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonf59e2632010-10-20 21:13:19 +0000217 if (probe_timing_exit)
218 programmer_delay(probe_timing_exit);
219
Ollie Lho761bf1b2004-03-20 16:46:10 +0000220 /* Issue JEDEC Product ID Entry command */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000221 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonc12fc712009-12-17 04:22:40 +0000222 if (probe_timing_enter)
223 programmer_delay(10);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000224 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Sean Nelsonc12fc712009-12-17 04:22:40 +0000225 if (probe_timing_enter)
226 programmer_delay(10);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000227 chip_writeb(flash, 0x90, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonc12fc712009-12-17 04:22:40 +0000228 if (probe_timing_enter)
229 programmer_delay(probe_timing_enter);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000230
Ollie Lho761bf1b2004-03-20 16:46:10 +0000231 /* Read product ID */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000232 id1 = chip_readb(flash, bios + (0x00 << shifted));
233 id2 = chip_readb(flash, bios + (0x01 << shifted));
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000234 largeid1 = id1;
235 largeid2 = id2;
236
237 /* Check if it is a continuation ID, this should be a while loop. */
238 if (id1 == 0x7F) {
239 largeid1 <<= 8;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000240 id1 = chip_readb(flash, bios + 0x100);
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000241 largeid1 |= id1;
242 }
243 if (id2 == 0x7F) {
244 largeid2 <<= 8;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000245 id2 = chip_readb(flash, bios + 0x101);
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000246 largeid2 |= id2;
247 }
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000248
Ollie Lho761bf1b2004-03-20 16:46:10 +0000249 /* Issue JEDEC Product ID Exit command */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000250 if ((chip->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000251 {
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000252 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonc57a9202010-01-04 17:15:23 +0000253 if (probe_timing_exit)
254 programmer_delay(10);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000255 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Sean Nelsonc57a9202010-01-04 17:15:23 +0000256 if (probe_timing_exit)
257 programmer_delay(10);
258 }
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000259 chip_writeb(flash, 0xF0, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonc12fc712009-12-17 04:22:40 +0000260 if (probe_timing_exit)
261 programmer_delay(probe_timing_exit);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000262
Sean Nelsoned479d22010-03-24 23:14:32 +0000263 msg_cdbg("%s: id1 0x%02x, id2 0x%02x", __func__, largeid1, largeid2);
Carl-Daniel Hailfingera758f512008-05-14 12:03:06 +0000264 if (!oddparity(id1))
Sean Nelsoned479d22010-03-24 23:14:32 +0000265 msg_cdbg(", id1 parity violation");
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000266
267 /* Read the product ID location again. We should now see normal flash contents. */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000268 flashcontent1 = chip_readb(flash, bios + (0x00 << shifted));
269 flashcontent2 = chip_readb(flash, bios + (0x01 << shifted));
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000270
271 /* Check if it is a continuation ID, this should be a while loop. */
272 if (flashcontent1 == 0x7F) {
273 flashcontent1 <<= 8;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000274 flashcontent1 |= chip_readb(flash, bios + 0x100);
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000275 }
276 if (flashcontent2 == 0x7F) {
277 flashcontent2 <<= 8;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000278 flashcontent2 |= chip_readb(flash, bios + 0x101);
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000279 }
280
281 if (largeid1 == flashcontent1)
Sean Nelsoned479d22010-03-24 23:14:32 +0000282 msg_cdbg(", id1 is normal flash content");
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000283 if (largeid2 == flashcontent2)
Sean Nelsoned479d22010-03-24 23:14:32 +0000284 msg_cdbg(", id2 is normal flash content");
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000285
Sean Nelsoned479d22010-03-24 23:14:32 +0000286 msg_cdbg("\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000287 if (largeid1 != chip->manufacture_id || largeid2 != chip->model_id)
Carl-Daniel Hailfingere9404662010-01-09 02:24:17 +0000288 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000289
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000290 if (chip->feature_bits & FEATURE_REGISTERMAP)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000291 map_flash_registers(flash);
292
Carl-Daniel Hailfingere9404662010-01-09 02:24:17 +0000293 return 1;
Ollie Lho73eca802004-03-19 22:10:07 +0000294}
295
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000296static int erase_sector_jedec_common(struct flashctx *flash, unsigned int page,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000297 unsigned int pagesize, unsigned int mask)
Ollie Lho73eca802004-03-19 22:10:07 +0000298{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000299 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000300 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
Stefan Taunerf80419c2014-05-02 15:41:42 +0000301 unsigned int delay_us = 0;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000302
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000303 if(flash->chip->probe_timing != TIMING_ZERO)
Stefan Taunerf80419c2014-05-02 15:41:42 +0000304 delay_us = 10;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000305
Ollie Lho761bf1b2004-03-20 16:46:10 +0000306 /* Issue the Sector Erase command */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000307 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000308 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000309 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000310 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000311 chip_writeb(flash, 0x80, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000312 programmer_delay(delay_us);
Ollie Lhoefa28582004-12-08 20:10:01 +0000313
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000314 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000315 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000316 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000317 programmer_delay(delay_us);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000318 chip_writeb(flash, 0x30, bios + page);
Michael Karcher880e8672011-04-15 00:03:37 +0000319 programmer_delay(delay_us);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000320
Ollie Lho73eca802004-03-19 22:10:07 +0000321 /* wait for Toggle bit ready */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000322 toggle_ready_jedec_slow(flash, bios);
Ollie Lho73eca802004-03-19 22:10:07 +0000323
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000324 /* FIXME: Check the status register for errors. */
Uwe Hermannffec5f32007-08-23 16:08:21 +0000325 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000326}
Ollie Lho98bea8a2004-12-07 03:15:51 +0000327
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000328static int erase_block_jedec_common(struct flashctx *flash, unsigned int block,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000329 unsigned int blocksize, unsigned int mask)
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000330{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000331 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000332 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
Stefan Taunerf80419c2014-05-02 15:41:42 +0000333 unsigned int delay_us = 0;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000334
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000335 if(flash->chip->probe_timing != TIMING_ZERO)
Stefan Taunerf80419c2014-05-02 15:41:42 +0000336 delay_us = 10;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000337
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000338 /* Issue the Sector Erase command */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000339 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000340 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000341 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000342 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000343 chip_writeb(flash, 0x80, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000344 programmer_delay(delay_us);
Ollie Lhoefa28582004-12-08 20:10:01 +0000345
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000346 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000347 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000348 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000349 programmer_delay(delay_us);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000350 chip_writeb(flash, 0x50, bios + block);
Michael Karcher880e8672011-04-15 00:03:37 +0000351 programmer_delay(delay_us);
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000352
353 /* wait for Toggle bit ready */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000354 toggle_ready_jedec_slow(flash, bios);
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000355
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000356 /* FIXME: Check the status register for errors. */
Uwe Hermannffec5f32007-08-23 16:08:21 +0000357 return 0;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000358}
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000359
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000360static int erase_chip_jedec_common(struct flashctx *flash, unsigned int mask)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000361{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000362 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000363 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
Stefan Taunerf80419c2014-05-02 15:41:42 +0000364 unsigned int delay_us = 0;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000365
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000366 if(flash->chip->probe_timing != TIMING_ZERO)
Stefan Taunerf80419c2014-05-02 15:41:42 +0000367 delay_us = 10;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000368
Ollie Lho761bf1b2004-03-20 16:46:10 +0000369 /* Issue the JEDEC Chip Erase command */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000370 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000371 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000372 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000373 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000374 chip_writeb(flash, 0x80, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000375 programmer_delay(delay_us);
Ollie Lhoefa28582004-12-08 20:10:01 +0000376
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000377 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000378 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000379 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000380 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000381 chip_writeb(flash, 0x10, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000382 programmer_delay(delay_us);
Ollie Lho73eca802004-03-19 22:10:07 +0000383
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000384 toggle_ready_jedec_slow(flash, bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000385
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000386 /* FIXME: Check the status register for errors. */
Uwe Hermannffec5f32007-08-23 16:08:21 +0000387 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000388}
389
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000390static int write_byte_program_jedec_common(const struct flashctx *flash, const uint8_t *src,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000391 chipaddr dst, unsigned int mask)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000392{
393 int tried = 0, failed = 0;
394 chipaddr bios = flash->virtual_memory;
395
396 /* If the data is 0xFF, don't program it and don't complain. */
397 if (*src == 0xFF) {
398 return 0;
399 }
400
401retry:
402 /* Issue JEDEC Byte Program command */
403 start_program_jedec_common(flash, mask);
404
405 /* transfer data from source to destination */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000406 chip_writeb(flash, *src, dst);
407 toggle_ready_jedec(flash, bios);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000408
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000409 if (chip_readb(flash, dst) != *src && tried++ < MAX_REFLASH_TRIES) {
Sean Nelsonc57a9202010-01-04 17:15:23 +0000410 goto retry;
411 }
412
413 if (tried >= MAX_REFLASH_TRIES)
414 failed = 1;
415
416 return failed;
417}
418
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000419/* chunksize is 1 */
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000420int write_jedec_1(struct flashctx *flash, const uint8_t *src, unsigned int start,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000421 unsigned int len)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000422{
423 int i, failed = 0;
Carl-Daniel Hailfingerb30a5ed2010-10-10 14:02:27 +0000424 chipaddr dst = flash->virtual_memory + start;
Sean Nelsonc57a9202010-01-04 17:15:23 +0000425 chipaddr olddst;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000426 unsigned int mask;
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000427
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000428 mask = getaddrmask(flash->chip);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000429
430 olddst = dst;
Carl-Daniel Hailfingerb30a5ed2010-10-10 14:02:27 +0000431 for (i = 0; i < len; i++) {
Sean Nelsonc57a9202010-01-04 17:15:23 +0000432 if (write_byte_program_jedec_common(flash, src, dst, mask))
433 failed = 1;
434 dst++, src++;
435 }
436 if (failed)
Stefan Taunerc2333752013-07-13 23:31:37 +0000437 msg_cerr(" writing sector at 0x%" PRIxPTR " failed!\n", olddst);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000438
439 return failed;
440}
441
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000442static int write_page_write_jedec_common(struct flashctx *flash, const uint8_t *src,
Stefan Tauner0554ca52013-07-25 22:54:25 +0000443 unsigned int start, unsigned int page_size)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000444{
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000445 int i, tried = 0, failed;
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000446 const uint8_t *s = src;
Urja Rannikko0c854c02009-06-25 13:57:31 +0000447 chipaddr bios = flash->virtual_memory;
448 chipaddr dst = bios + start;
449 chipaddr d = dst;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000450 unsigned int mask;
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000451
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000452 mask = getaddrmask(flash->chip);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000453
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000454retry:
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000455 /* Issue JEDEC Start Program command */
Sean Nelsonc57a9202010-01-04 17:15:23 +0000456 start_program_jedec_common(flash, mask);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000457
Ollie Lho98bea8a2004-12-07 03:15:51 +0000458 /* transfer data from source to destination */
Carl-Daniel Hailfinger8a8a2262009-11-14 03:48:33 +0000459 for (i = 0; i < page_size; i++) {
Ollie Lho98bea8a2004-12-07 03:15:51 +0000460 /* If the data is 0xFF, don't program it */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000461 if (*src != 0xFF)
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000462 chip_writeb(flash, *src, dst);
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000463 dst++;
464 src++;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000465 }
466
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000467 toggle_ready_jedec(flash, dst - 1);
Ollie Lho98bea8a2004-12-07 03:15:51 +0000468
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000469 dst = d;
470 src = s;
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000471 failed = verify_range(flash, src, start, page_size);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000472
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000473 if (failed && tried++ < MAX_REFLASH_TRIES) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000474 msg_cerr("retrying.\n");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000475 goto retry;
476 }
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000477 if (failed) {
Stefan Taunerc2333752013-07-13 23:31:37 +0000478 msg_cerr(" page 0x%" PRIxPTR " failed!\n", (d - bios) / page_size);
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000479 }
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000480 return failed;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000481}
482
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000483/* chunksize is page_size */
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000484/*
485 * Write a part of the flash chip.
486 * FIXME: Use the chunk code from Michael Karcher instead.
487 * This function is a slightly modified copy of spi_write_chunked.
488 * Each page is written separately in chunks with a maximum size of chunksize.
489 */
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000490int write_jedec(struct flashctx *flash, const uint8_t *buf, unsigned int start,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000491 int unsigned len)
Carl-Daniel Hailfinger4bf4e792010-01-09 03:15:50 +0000492{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000493 unsigned int i, starthere, lenhere;
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000494 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000495 * in struct flashctx to do this properly. All chips using
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000496 * write_jedec have page_size set to max_writechunk_size, so
497 * we're OK for now.
498 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000499 unsigned int page_size = flash->chip->page_size;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000500
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000501 /* Warning: This loop has a very unusual condition and body.
502 * The loop needs to go through each page with at least one affected
503 * byte. The lowest page number is (start / page_size) since that
504 * division rounds down. The highest page number we want is the page
505 * where the last byte of the range lives. That last byte has the
506 * address (start + len - 1), thus the highest page number is
507 * (start + len - 1) / page_size. Since we want to include that last
508 * page as well, the loop condition uses <=.
509 */
510 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
511 /* Byte position of the first byte in the range in this page. */
512 /* starthere is an offset to the base address of the chip. */
513 starthere = max(start, i * page_size);
514 /* Length of bytes in the range in this page. */
515 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Sean Nelson35727f72010-01-28 23:55:12 +0000516
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000517 if (write_page_write_jedec_common(flash, buf + starthere - start, starthere, lenhere))
518 return 1;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000519 }
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000520
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000521 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000522}
Michael Karcher1c296ca2009-11-27 17:49:42 +0000523
Sean Nelsonc57a9202010-01-04 17:15:23 +0000524/* erase chip with block_erase() prototype */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000525int erase_chip_block_jedec(struct flashctx *flash, unsigned int addr,
Sean Nelsonc57a9202010-01-04 17:15:23 +0000526 unsigned int blocksize)
527{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000528 unsigned int mask;
Sean Nelson35727f72010-01-28 23:55:12 +0000529
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000530 mask = getaddrmask(flash->chip);
531 if ((addr != 0) || (blocksize != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000532 msg_cerr("%s called with incorrect arguments\n",
Sean Nelsonc57a9202010-01-04 17:15:23 +0000533 __func__);
534 return -1;
535 }
Sean Nelson35727f72010-01-28 23:55:12 +0000536 return erase_chip_jedec_common(flash, mask);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000537}
538
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000539int probe_jedec(struct flashctx *flash)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000540{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000541 unsigned int mask;
Carl-Daniel Hailfinger4bf4e792010-01-09 03:15:50 +0000542
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000543 mask = getaddrmask(flash->chip);
Sean Nelson35727f72010-01-28 23:55:12 +0000544 return probe_jedec_common(flash, mask);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000545}
546
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000547int erase_sector_jedec(struct flashctx *flash, unsigned int page,
548 unsigned int size)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000549{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000550 unsigned int mask;
Sean Nelson35727f72010-01-28 23:55:12 +0000551
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000552 mask = getaddrmask(flash->chip);
Sean Nelson35727f72010-01-28 23:55:12 +0000553 return erase_sector_jedec_common(flash, page, size, mask);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000554}
555
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000556int erase_block_jedec(struct flashctx *flash, unsigned int page,
557 unsigned int size)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000558{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000559 unsigned int mask;
Sean Nelson35727f72010-01-28 23:55:12 +0000560
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000561 mask = getaddrmask(flash->chip);
Sean Nelson35727f72010-01-28 23:55:12 +0000562 return erase_block_jedec_common(flash, page, size, mask);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000563}
564
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000565int erase_chip_jedec(struct flashctx *flash)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000566{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000567 unsigned int mask;
Sean Nelson35727f72010-01-28 23:55:12 +0000568
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000569 mask = getaddrmask(flash->chip);
Sean Nelson35727f72010-01-28 23:55:12 +0000570 return erase_chip_jedec_common(flash, mask);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000571}
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000572
573struct unlockblock {
574 unsigned int size;
575 unsigned int count;
576};
577
578typedef int (*unlockblock_func)(const struct flashctx *flash, chipaddr offset);
579static int regspace2_walk_unlockblocks(const struct flashctx *flash, const struct unlockblock *block, unlockblock_func func)
580{
581 chipaddr off = flash->virtual_registers + 2;
582 while (block->count != 0) {
583 unsigned int j;
584 for (j = 0; j < block->count; j++) {
585 if (func(flash, off))
586 return -1;
587 off += block->size;
588 }
589 block++;
590 }
591 return 0;
592}
593
594#define REG2_RWLOCK ((1 << 2) | (1 << 0))
595#define REG2_LOCKDOWN (1 << 1)
596#define REG2_MASK (REG2_RWLOCK | REG2_LOCKDOWN)
597
598static int printlock_regspace2_block(const struct flashctx *flash, chipaddr offset)
599{
600 chipaddr wrprotect = flash->virtual_registers + offset + 2;
601 uint8_t state = chip_readb(flash, wrprotect);
602 msg_cdbg("Lock status of block at 0x%0*" PRIxPTR " is ", PRIxPTR_WIDTH, offset);
603 switch (state & REG2_MASK) {
604 case 0:
605 msg_cdbg("Full Access.\n");
606 break;
607 case 1:
608 msg_cdbg("Write Lock (Default State).\n");
609 break;
610 case 2:
611 msg_cdbg("Locked Open (Full Access, Locked Down).\n");
612 break;
613 case 3:
614 msg_cdbg("Write Lock, Locked Down.\n");
615 break;
616 case 4:
617 msg_cdbg("Read Lock.\n");
618 break;
619 case 5:
620 msg_cdbg("Read/Write Lock.\n");
621 break;
622 case 6:
623 msg_cdbg("Read Lock, Locked Down.\n");
624 break;
625 case 7:
626 msg_cdbg("Read/Write Lock, Locked Down.\n");
627 break;
628 }
629 return 0;
630}
631
632int printlock_regspace2_blocks(const struct flashctx *flash, const struct unlockblock *blocks)
633{
634 return regspace2_walk_unlockblocks(flash, blocks, &printlock_regspace2_block);
635}
636
637static int printlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
638{
639 const unsigned int elems = flash->chip->total_size * 1024 / block_size;
640 struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
641 return regspace2_walk_unlockblocks(flash, blocks, &printlock_regspace2_block);
642}
643
644int printlock_regspace2_uniform_64k(struct flashctx *flash)
645{
646 return printlock_regspace2_uniform(flash, 64 * 1024);
647}
648
649int printlock_regspace2_block_eraser_0(struct flashctx *flash)
650{
651 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
652 const struct unlockblock *unlockblocks =
653 (const struct unlockblock *)flash->chip->block_erasers[0].eraseblocks;
654 return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block);
655}
656
657int printlock_regspace2_block_eraser_1(struct flashctx *flash)
658{
659 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
660 const struct unlockblock *unlockblocks =
661 (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks;
662 return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block);
663}
664
665static int changelock_regspace2_block(const struct flashctx *flash, chipaddr offset, uint8_t new_bits)
666{
667 chipaddr wrprotect = flash->virtual_registers + offset + 2;
668 uint8_t old;
669
670 if (new_bits & ~REG2_MASK) {
671 msg_cerr("Invalid locking change 0x%02x requested at 0x%0*" PRIxPTR "! "
672 "Please report a bug at flashrom@flashrom.org\n",
673 new_bits, PRIxPTR_WIDTH, offset);
674 return -1;
675 }
676 old = chip_readb(flash, wrprotect);
677 /* Early exist if no change (of read/write/lockdown) was requested. */
678 if (((old ^ new_bits) & REG2_MASK) == 0) {
679 msg_cdbg2("Locking status at 0x%0*" PRIxPTR " not changed\n", PRIxPTR_WIDTH, offset);
680 return 0;
681 }
682 /* Normally lockdowns can not be cleared. Try nevertheless if requested. */
683 if ((old & REG2_LOCKDOWN) && !(new_bits & REG2_LOCKDOWN)) {
684 chip_writeb(flash, old & ~REG2_LOCKDOWN, wrprotect);
685 if (chip_readb(flash, wrprotect) != (old & ~REG2_LOCKDOWN)) {
686 msg_cerr("Lockdown can't be removed at 0x%0*" PRIxPTR "!\n", PRIxPTR_WIDTH, offset);
687 return -1;
688 }
689 }
690 /* Change read or write lock? */
691 if ((old ^ new_bits) & REG2_RWLOCK) {
692 /* Do not lockdown yet. */
693 msg_cdbg("Changing locking status at 0x%0*" PRIxPTR " to 0x%02x\n", PRIxPTR_WIDTH, offset, new_bits & REG2_RWLOCK);
694 chip_writeb(flash, new_bits & REG2_RWLOCK, wrprotect);
695 if (chip_readb(flash, wrprotect) != (new_bits & REG2_RWLOCK)) {
696 msg_cerr("Locking status change FAILED at 0x%0*" PRIxPTR "!\n", PRIxPTR_WIDTH, offset);
697 return -1;
698 }
699 }
700 /* Enable lockdown if requested. */
701 if (!(old & REG2_LOCKDOWN) && (new_bits & REG2_LOCKDOWN)) {
702 msg_cdbg("Enabling lockdown at 0x%0*" PRIxPTR "\n", PRIxPTR_WIDTH, offset);
703 chip_writeb(flash, new_bits, wrprotect);
704 if (chip_readb(flash, wrprotect) != new_bits) {
705 msg_cerr("Enabling lockdown FAILED at 0x%0*" PRIxPTR "!\n", PRIxPTR_WIDTH, offset);
706 return -1;
707 }
708 }
709
710 return 0;
711}
712
713int unlock_regspace2_block(const struct flashctx *flash, chipaddr off)
714{
715 chipaddr wrprotect = flash->virtual_registers + off + 2;
716 uint8_t old = chip_readb(flash, wrprotect);
717 /* We don't care for the lockdown bit as long as the RW locks are 0 after we're done */
718 return changelock_regspace2_block(flash, off, old & ~REG2_RWLOCK);
719}
720
721static int unlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
722{
723 const unsigned int elems = flash->chip->total_size * 1024 / block_size;
724 struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
725 return regspace2_walk_unlockblocks(flash, blocks, &unlock_regspace2_block);
726}
727
728int unlock_regspace2_uniform_64k(struct flashctx *flash)
729{
730 return unlock_regspace2_uniform(flash, 64 * 1024);
731}
732
733int unlock_regspace2_uniform_32k(struct flashctx *flash)
734{
735 return unlock_regspace2_uniform(flash, 32 * 1024);
736}
737
738int unlock_regspace2_block_eraser_0(struct flashctx *flash)
739{
740 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
741 const struct unlockblock *unlockblocks =
742 (const struct unlockblock *)flash->chip->block_erasers[0].eraseblocks;
743 return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block);
744}
745
746int unlock_regspace2_block_eraser_1(struct flashctx *flash)
747{
748 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
749 const struct unlockblock *unlockblocks =
750 (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks;
751 return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block);
752}
753