blob: 0709efa5efeae560886c594433e7096198666c8c [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"
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +000023#include "chipdrivers.h"
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000024
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +000025#define MAX_REFLASH_TRIES 0x10
Sean Nelsonc57a9202010-01-04 17:15:23 +000026#define MASK_FULL 0xffff
27#define MASK_2AA 0x7ff
Sean Nelson35727f72010-01-28 23:55:12 +000028#define MASK_AAA 0xfff
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +000029
Carl-Daniel Hailfingera758f512008-05-14 12:03:06 +000030/* Check one byte for odd parity */
31uint8_t oddparity(uint8_t val)
32{
33 val = (val ^ (val >> 4)) & 0xf;
34 val = (val ^ (val >> 2)) & 0x3;
35 return (val ^ (val >> 1)) & 0x1;
36}
37
Stefan Taunerf80419c2014-05-02 15:41:42 +000038static void toggle_ready_jedec_common(const struct flashctx *flash, chipaddr dst, unsigned int delay)
Uwe Hermann51582f22007-08-23 10:20:40 +000039{
40 unsigned int i = 0;
41 uint8_t tmp1, tmp2;
42
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000043 tmp1 = chip_readb(flash, dst) & 0x40;
Uwe Hermann51582f22007-08-23 10:20:40 +000044
45 while (i++ < 0xFFFFFFF) {
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000046 if (delay)
47 programmer_delay(delay);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000048 tmp2 = chip_readb(flash, dst) & 0x40;
Uwe Hermann51582f22007-08-23 10:20:40 +000049 if (tmp1 == tmp2) {
50 break;
51 }
52 tmp1 = tmp2;
53 }
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000054 if (i > 0x100000)
Sean Nelsoned479d22010-03-24 23:14:32 +000055 msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i);
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000056}
57
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000058void toggle_ready_jedec(const struct flashctx *flash, chipaddr dst)
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000059{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000060 toggle_ready_jedec_common(flash, dst, 0);
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000061}
62
63/* Some chips require a minimum delay between toggle bit reads.
64 * The Winbond W39V040C wants 50 ms between reads on sector erase toggle,
65 * but experiments show that 2 ms are already enough. Pick a safety factor
66 * of 4 and use an 8 ms delay.
Elyes HAOUAS124ef382018-03-27 12:15:09 +020067 * Given that erase is slow on all chips, it is recommended to use
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000068 * toggle_ready_jedec_slow in erase functions.
69 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000070static void toggle_ready_jedec_slow(const struct flashctx *flash, chipaddr dst)
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000071{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000072 toggle_ready_jedec_common(flash, dst, 8 * 1000);
Uwe Hermann51582f22007-08-23 10:20:40 +000073}
74
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000075void data_polling_jedec(const struct flashctx *flash, chipaddr dst,
76 uint8_t data)
Uwe Hermann51582f22007-08-23 10:20:40 +000077{
78 unsigned int i = 0;
79 uint8_t tmp;
80
81 data &= 0x80;
82
83 while (i++ < 0xFFFFFFF) {
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000084 tmp = chip_readb(flash, dst) & 0x80;
Uwe Hermann51582f22007-08-23 10:20:40 +000085 if (tmp == data) {
86 break;
87 }
88 }
Carl-Daniel Hailfingeraa000982009-12-17 16:20:26 +000089 if (i > 0x100000)
Sean Nelsoned479d22010-03-24 23:14:32 +000090 msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i);
Uwe Hermann51582f22007-08-23 10:20:40 +000091}
92
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +000093static unsigned int getaddrmask(const struct flashchip *chip)
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +000094{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +000095 switch (chip->feature_bits & FEATURE_ADDR_MASK) {
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +000096 case FEATURE_ADDR_FULL:
97 return MASK_FULL;
98 break;
99 case FEATURE_ADDR_2AA:
100 return MASK_2AA;
101 break;
102 case FEATURE_ADDR_AAA:
103 return MASK_AAA;
104 break;
105 default:
106 msg_cerr("%s called with unknown mask\n", __func__);
107 return 0;
108 break;
109 }
110}
111
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000112static void start_program_jedec_common(const struct flashctx *flash, unsigned int mask)
Uwe Hermann51582f22007-08-23 10:20:40 +0000113{
Sean Nelsonc57a9202010-01-04 17:15:23 +0000114 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000115 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
116
117 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
118 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
119 chip_writeb(flash, 0xA0, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Uwe Hermann51582f22007-08-23 10:20:40 +0000120}
121
Stefan Tauner03a9c3c2014-08-03 14:15:14 +0000122int probe_jedec_29gl(struct flashctx *flash)
123{
124 unsigned int mask = getaddrmask(flash->chip);
125 chipaddr bios = flash->virtual_memory;
126 const struct flashchip *chip = flash->chip;
127
128 /* Reset chip to a clean slate */
129 chip_writeb(flash, 0xF0, bios + (0x5555 & mask));
130
131 /* Issue JEDEC Product ID Entry command */
132 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
133 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
134 chip_writeb(flash, 0x90, bios + (0x5555 & mask));
135
136 /* Read product ID */
137 // FIXME: Continuation loop, second byte is at word 0x100/byte 0x200
138 uint32_t man_id = chip_readb(flash, bios + 0x00);
139 uint32_t dev_id = (chip_readb(flash, bios + 0x01) << 16) |
140 (chip_readb(flash, bios + 0x0E) << 8) |
141 (chip_readb(flash, bios + 0x0F) << 0);
142
143 /* Issue JEDEC Product ID Exit command */
144 chip_writeb(flash, 0xF0, bios + (0x5555 & mask));
145
146 msg_cdbg("%s: man_id 0x%02x, dev_id 0x%06x", __func__, man_id, dev_id);
147 if (!oddparity(man_id))
148 msg_cdbg(", man_id parity violation");
149
150 /* Read the product ID location again. We should now see normal flash contents. */
151 uint32_t flashcontent1 = chip_readb(flash, bios + 0x00); // FIXME: Continuation loop
152 uint32_t flashcontent2 = (chip_readb(flash, bios + 0x01) << 16) |
153 (chip_readb(flash, bios + 0x0E) << 8) |
154 (chip_readb(flash, bios + 0x0F) << 0);
155
156 if (man_id == flashcontent1)
157 msg_cdbg(", man_id seems to be normal flash content");
158 if (dev_id == flashcontent2)
159 msg_cdbg(", dev_id seems to be normal flash content");
160
161 msg_cdbg("\n");
162 if (man_id != chip->manufacture_id || dev_id != chip->model_id)
163 return 0;
164
Stefan Tauner03a9c3c2014-08-03 14:15:14 +0000165 return 1;
166}
167
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000168static int probe_jedec_common(struct flashctx *flash, unsigned int mask)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000169{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000170 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000171 const struct flashchip *chip = flash->chip;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000172 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
Ollie Lho184a4042005-11-26 21:55:36 +0000173 uint8_t id1, id2;
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000174 uint32_t largeid1, largeid2;
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000175 uint32_t flashcontent1, flashcontent2;
Stefan Taunerf80419c2014-05-02 15:41:42 +0000176 unsigned int probe_timing_enter, probe_timing_exit;
Maciej Pijankac6e11112009-06-03 14:46:22 +0000177
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000178 if (chip->probe_timing > 0)
179 probe_timing_enter = probe_timing_exit = chip->probe_timing;
180 else if (chip->probe_timing == TIMING_ZERO) { /* No delay. */
Maciej Pijankac6e11112009-06-03 14:46:22 +0000181 probe_timing_enter = probe_timing_exit = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000182 } else if (chip->probe_timing == TIMING_FIXME) { /* == _IGNORED */
Stefan Tauner23e10b82016-01-23 16:16:49 +0000183 msg_cdbg("Chip lacks correct probe timing information, using default 10ms/40us. ");
Maciej Pijankac6e11112009-06-03 14:46:22 +0000184 probe_timing_enter = 10000;
185 probe_timing_exit = 40;
186 } else {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000187 msg_cerr("Chip has negative value in probe_timing, failing without chip access\n");
Maciej Pijankac6e11112009-06-03 14:46:22 +0000188 return 0;
189 }
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000190
Sean Nelsonf59e2632010-10-20 21:13:19 +0000191 /* Earlier probes might have been too fast for the chip to enter ID
192 * mode completely. Allow the chip to finish this before seeing a
193 * reset command.
194 */
195 if (probe_timing_enter)
196 programmer_delay(probe_timing_enter);
197 /* Reset chip to a clean slate */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000198 if ((chip->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET)
Sean Nelsonf59e2632010-10-20 21:13:19 +0000199 {
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000200 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonf59e2632010-10-20 21:13:19 +0000201 if (probe_timing_exit)
202 programmer_delay(10);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000203 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Sean Nelsonf59e2632010-10-20 21:13:19 +0000204 if (probe_timing_exit)
205 programmer_delay(10);
206 }
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000207 chip_writeb(flash, 0xF0, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonf59e2632010-10-20 21:13:19 +0000208 if (probe_timing_exit)
209 programmer_delay(probe_timing_exit);
210
Ollie Lho761bf1b2004-03-20 16:46:10 +0000211 /* Issue JEDEC Product ID Entry command */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000212 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonc12fc712009-12-17 04:22:40 +0000213 if (probe_timing_enter)
214 programmer_delay(10);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000215 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Sean Nelsonc12fc712009-12-17 04:22:40 +0000216 if (probe_timing_enter)
217 programmer_delay(10);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000218 chip_writeb(flash, 0x90, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonc12fc712009-12-17 04:22:40 +0000219 if (probe_timing_enter)
220 programmer_delay(probe_timing_enter);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000221
Ollie Lho761bf1b2004-03-20 16:46:10 +0000222 /* Read product ID */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000223 id1 = chip_readb(flash, bios + (0x00 << shifted));
224 id2 = chip_readb(flash, bios + (0x01 << shifted));
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000225 largeid1 = id1;
226 largeid2 = id2;
227
228 /* Check if it is a continuation ID, this should be a while loop. */
229 if (id1 == 0x7F) {
230 largeid1 <<= 8;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000231 id1 = chip_readb(flash, bios + 0x100);
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000232 largeid1 |= id1;
233 }
234 if (id2 == 0x7F) {
235 largeid2 <<= 8;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000236 id2 = chip_readb(flash, bios + 0x101);
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +0000237 largeid2 |= id2;
238 }
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000239
Ollie Lho761bf1b2004-03-20 16:46:10 +0000240 /* Issue JEDEC Product ID Exit command */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000241 if ((chip->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000242 {
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000243 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonc57a9202010-01-04 17:15:23 +0000244 if (probe_timing_exit)
245 programmer_delay(10);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000246 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Sean Nelsonc57a9202010-01-04 17:15:23 +0000247 if (probe_timing_exit)
248 programmer_delay(10);
249 }
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000250 chip_writeb(flash, 0xF0, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Sean Nelsonc12fc712009-12-17 04:22:40 +0000251 if (probe_timing_exit)
252 programmer_delay(probe_timing_exit);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000253
Sean Nelsoned479d22010-03-24 23:14:32 +0000254 msg_cdbg("%s: id1 0x%02x, id2 0x%02x", __func__, largeid1, largeid2);
Carl-Daniel Hailfingera758f512008-05-14 12:03:06 +0000255 if (!oddparity(id1))
Sean Nelsoned479d22010-03-24 23:14:32 +0000256 msg_cdbg(", id1 parity violation");
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000257
258 /* Read the product ID location again. We should now see normal flash contents. */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000259 flashcontent1 = chip_readb(flash, bios + (0x00 << shifted));
260 flashcontent2 = chip_readb(flash, bios + (0x01 << shifted));
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000261
262 /* Check if it is a continuation ID, this should be a while loop. */
263 if (flashcontent1 == 0x7F) {
264 flashcontent1 <<= 8;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000265 flashcontent1 |= chip_readb(flash, bios + 0x100);
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000266 }
267 if (flashcontent2 == 0x7F) {
268 flashcontent2 <<= 8;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000269 flashcontent2 |= chip_readb(flash, bios + 0x101);
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000270 }
271
272 if (largeid1 == flashcontent1)
Sean Nelsoned479d22010-03-24 23:14:32 +0000273 msg_cdbg(", id1 is normal flash content");
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000274 if (largeid2 == flashcontent2)
Sean Nelsoned479d22010-03-24 23:14:32 +0000275 msg_cdbg(", id2 is normal flash content");
Carl-Daniel Hailfinger8130f2d2009-05-11 14:40:31 +0000276
Sean Nelsoned479d22010-03-24 23:14:32 +0000277 msg_cdbg("\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000278 if (largeid1 != chip->manufacture_id || largeid2 != chip->model_id)
Carl-Daniel Hailfingere9404662010-01-09 02:24:17 +0000279 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000280
Carl-Daniel Hailfingere9404662010-01-09 02:24:17 +0000281 return 1;
Ollie Lho73eca802004-03-19 22:10:07 +0000282}
283
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000284static int erase_sector_jedec_common(struct flashctx *flash, unsigned int page,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000285 unsigned int pagesize, unsigned int mask)
Ollie Lho73eca802004-03-19 22:10:07 +0000286{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000287 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000288 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
Stefan Taunerf80419c2014-05-02 15:41:42 +0000289 unsigned int delay_us = 0;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000290
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000291 if(flash->chip->probe_timing != TIMING_ZERO)
Stefan Taunerf80419c2014-05-02 15:41:42 +0000292 delay_us = 10;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000293
Ollie Lho761bf1b2004-03-20 16:46:10 +0000294 /* Issue the Sector Erase command */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000295 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000296 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000297 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000298 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000299 chip_writeb(flash, 0x80, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000300 programmer_delay(delay_us);
Ollie Lhoefa28582004-12-08 20:10:01 +0000301
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000302 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000303 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000304 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000305 programmer_delay(delay_us);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000306 chip_writeb(flash, 0x30, bios + page);
Michael Karcher880e8672011-04-15 00:03:37 +0000307 programmer_delay(delay_us);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000308
Ollie Lho73eca802004-03-19 22:10:07 +0000309 /* wait for Toggle bit ready */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000310 toggle_ready_jedec_slow(flash, bios);
Ollie Lho73eca802004-03-19 22:10:07 +0000311
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000312 /* FIXME: Check the status register for errors. */
Uwe Hermannffec5f32007-08-23 16:08:21 +0000313 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000314}
Ollie Lho98bea8a2004-12-07 03:15:51 +0000315
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000316static int erase_block_jedec_common(struct flashctx *flash, unsigned int block,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000317 unsigned int blocksize, unsigned int mask)
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000318{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000319 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000320 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
Stefan Taunerf80419c2014-05-02 15:41:42 +0000321 unsigned int delay_us = 0;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000322
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000323 if(flash->chip->probe_timing != TIMING_ZERO)
Stefan Taunerf80419c2014-05-02 15:41:42 +0000324 delay_us = 10;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000325
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000326 /* Issue the Sector Erase command */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000327 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000328 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000329 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000330 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000331 chip_writeb(flash, 0x80, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000332 programmer_delay(delay_us);
Ollie Lhoefa28582004-12-08 20:10:01 +0000333
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000334 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000335 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000336 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000337 programmer_delay(delay_us);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000338 chip_writeb(flash, 0x50, bios + block);
Michael Karcher880e8672011-04-15 00:03:37 +0000339 programmer_delay(delay_us);
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000340
341 /* wait for Toggle bit ready */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000342 toggle_ready_jedec_slow(flash, bios);
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000343
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000344 /* FIXME: Check the status register for errors. */
Uwe Hermannffec5f32007-08-23 16:08:21 +0000345 return 0;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000346}
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000347
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000348static int erase_chip_jedec_common(struct flashctx *flash, unsigned int mask)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000349{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000350 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000351 bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED);
Stefan Taunerf80419c2014-05-02 15:41:42 +0000352 unsigned int delay_us = 0;
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000353
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000354 if(flash->chip->probe_timing != TIMING_ZERO)
Stefan Taunerf80419c2014-05-02 15:41:42 +0000355 delay_us = 10;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000356
Ollie Lho761bf1b2004-03-20 16:46:10 +0000357 /* Issue the JEDEC Chip Erase command */
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000358 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000359 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000360 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000361 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000362 chip_writeb(flash, 0x80, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000363 programmer_delay(delay_us);
Ollie Lhoefa28582004-12-08 20:10:01 +0000364
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000365 chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000366 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000367 chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000368 programmer_delay(delay_us);
Carl-Daniel Hailfingera8cf3622014-08-08 08:33:01 +0000369 chip_writeb(flash, 0x10, bios + ((shifted ? 0x2AAA : 0x5555) & mask));
Michael Karcher880e8672011-04-15 00:03:37 +0000370 programmer_delay(delay_us);
Ollie Lho73eca802004-03-19 22:10:07 +0000371
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000372 toggle_ready_jedec_slow(flash, bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000373
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000374 /* FIXME: Check the status register for errors. */
Uwe Hermannffec5f32007-08-23 16:08:21 +0000375 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000376}
377
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000378static int write_byte_program_jedec_common(const struct flashctx *flash, const uint8_t *src,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000379 chipaddr dst, unsigned int mask)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000380{
381 int tried = 0, failed = 0;
382 chipaddr bios = flash->virtual_memory;
383
384 /* If the data is 0xFF, don't program it and don't complain. */
385 if (*src == 0xFF) {
386 return 0;
387 }
388
389retry:
390 /* Issue JEDEC Byte Program command */
391 start_program_jedec_common(flash, mask);
392
393 /* transfer data from source to destination */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000394 chip_writeb(flash, *src, dst);
395 toggle_ready_jedec(flash, bios);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000396
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000397 if (chip_readb(flash, dst) != *src && tried++ < MAX_REFLASH_TRIES) {
Sean Nelsonc57a9202010-01-04 17:15:23 +0000398 goto retry;
399 }
400
401 if (tried >= MAX_REFLASH_TRIES)
402 failed = 1;
403
404 return failed;
405}
406
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000407/* chunksize is 1 */
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000408int write_jedec_1(struct flashctx *flash, const uint8_t *src, unsigned int start,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000409 unsigned int len)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000410{
Nico Huber519be662018-12-23 20:03:35 +0100411 unsigned int i;
412 int failed = 0;
Carl-Daniel Hailfingerb30a5ed2010-10-10 14:02:27 +0000413 chipaddr dst = flash->virtual_memory + start;
Sean Nelsonc57a9202010-01-04 17:15:23 +0000414 chipaddr olddst;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000415 unsigned int mask;
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000416
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000417 mask = getaddrmask(flash->chip);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000418
419 olddst = dst;
Carl-Daniel Hailfingerb30a5ed2010-10-10 14:02:27 +0000420 for (i = 0; i < len; i++) {
Sean Nelsonc57a9202010-01-04 17:15:23 +0000421 if (write_byte_program_jedec_common(flash, src, dst, mask))
422 failed = 1;
423 dst++, src++;
424 }
425 if (failed)
Stefan Taunerc2333752013-07-13 23:31:37 +0000426 msg_cerr(" writing sector at 0x%" PRIxPTR " failed!\n", olddst);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000427
428 return failed;
429}
430
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000431static int write_page_write_jedec_common(struct flashctx *flash, const uint8_t *src,
Stefan Tauner0554ca52013-07-25 22:54:25 +0000432 unsigned int start, unsigned int page_size)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000433{
Nico Huber519be662018-12-23 20:03:35 +0100434 unsigned int i;
435 int tried = 0, failed;
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000436 const uint8_t *s = src;
Urja Rannikko0c854c02009-06-25 13:57:31 +0000437 chipaddr bios = flash->virtual_memory;
438 chipaddr dst = bios + start;
439 chipaddr d = dst;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000440 unsigned int mask;
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000441
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000442 mask = getaddrmask(flash->chip);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000443
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000444retry:
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000445 /* Issue JEDEC Start Program command */
Sean Nelsonc57a9202010-01-04 17:15:23 +0000446 start_program_jedec_common(flash, mask);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000447
Ollie Lho98bea8a2004-12-07 03:15:51 +0000448 /* transfer data from source to destination */
Carl-Daniel Hailfinger8a8a2262009-11-14 03:48:33 +0000449 for (i = 0; i < page_size; i++) {
Ollie Lho98bea8a2004-12-07 03:15:51 +0000450 /* If the data is 0xFF, don't program it */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000451 if (*src != 0xFF)
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000452 chip_writeb(flash, *src, dst);
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000453 dst++;
454 src++;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000455 }
456
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000457 toggle_ready_jedec(flash, dst - 1);
Ollie Lho98bea8a2004-12-07 03:15:51 +0000458
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000459 dst = d;
460 src = s;
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000461 failed = verify_range(flash, src, start, page_size);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000462
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000463 if (failed && tried++ < MAX_REFLASH_TRIES) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000464 msg_cerr("retrying.\n");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000465 goto retry;
466 }
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000467 if (failed) {
Stefan Taunerc2333752013-07-13 23:31:37 +0000468 msg_cerr(" page 0x%" PRIxPTR " failed!\n", (d - bios) / page_size);
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000469 }
Carl-Daniel Hailfinger2925d6f2009-11-25 16:41:50 +0000470 return failed;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000471}
472
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000473/* chunksize is page_size */
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000474/*
475 * Write a part of the flash chip.
476 * FIXME: Use the chunk code from Michael Karcher instead.
477 * This function is a slightly modified copy of spi_write_chunked.
478 * Each page is written separately in chunks with a maximum size of chunksize.
479 */
Stefan Tauner0ab1e5d2014-05-29 11:51:24 +0000480int write_jedec(struct flashctx *flash, const uint8_t *buf, unsigned int start,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000481 int unsigned len)
Carl-Daniel Hailfinger4bf4e792010-01-09 03:15:50 +0000482{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000483 unsigned int i, starthere, lenhere;
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000484 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000485 * in struct flashctx to do this properly. All chips using
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000486 * write_jedec have page_size set to max_writechunk_size, so
487 * we're OK for now.
488 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000489 unsigned int page_size = flash->chip->page_size;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000490
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000491 /* Warning: This loop has a very unusual condition and body.
492 * The loop needs to go through each page with at least one affected
493 * byte. The lowest page number is (start / page_size) since that
494 * division rounds down. The highest page number we want is the page
495 * where the last byte of the range lives. That last byte has the
496 * address (start + len - 1), thus the highest page number is
497 * (start + len - 1) / page_size. Since we want to include that last
498 * page as well, the loop condition uses <=.
499 */
500 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
501 /* Byte position of the first byte in the range in this page. */
502 /* starthere is an offset to the base address of the chip. */
503 starthere = max(start, i * page_size);
504 /* Length of bytes in the range in this page. */
505 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Sean Nelson35727f72010-01-28 23:55:12 +0000506
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000507 if (write_page_write_jedec_common(flash, buf + starthere - start, starthere, lenhere))
508 return 1;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000509 }
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000510
Carl-Daniel Hailfinger79e67572010-10-13 21:49:30 +0000511 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000512}
Michael Karcher1c296ca2009-11-27 17:49:42 +0000513
Sean Nelsonc57a9202010-01-04 17:15:23 +0000514/* erase chip with block_erase() prototype */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000515int erase_chip_block_jedec(struct flashctx *flash, unsigned int addr,
Sean Nelsonc57a9202010-01-04 17:15:23 +0000516 unsigned int blocksize)
517{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000518 unsigned int mask;
Sean Nelson35727f72010-01-28 23:55:12 +0000519
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000520 mask = getaddrmask(flash->chip);
521 if ((addr != 0) || (blocksize != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000522 msg_cerr("%s called with incorrect arguments\n",
Sean Nelsonc57a9202010-01-04 17:15:23 +0000523 __func__);
524 return -1;
525 }
Sean Nelson35727f72010-01-28 23:55:12 +0000526 return erase_chip_jedec_common(flash, mask);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000527}
528
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000529int probe_jedec(struct flashctx *flash)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000530{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000531 unsigned int mask;
Carl-Daniel Hailfinger4bf4e792010-01-09 03:15:50 +0000532
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000533 mask = getaddrmask(flash->chip);
Sean Nelson35727f72010-01-28 23:55:12 +0000534 return probe_jedec_common(flash, mask);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000535}
536
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000537int erase_sector_jedec(struct flashctx *flash, unsigned int page,
538 unsigned int size)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000539{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000540 unsigned int mask;
Sean Nelson35727f72010-01-28 23:55:12 +0000541
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000542 mask = getaddrmask(flash->chip);
Sean Nelson35727f72010-01-28 23:55:12 +0000543 return erase_sector_jedec_common(flash, page, size, mask);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000544}
545
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000546int erase_block_jedec(struct flashctx *flash, unsigned int page,
547 unsigned int size)
Sean Nelsonc57a9202010-01-04 17:15:23 +0000548{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000549 unsigned int mask;
Sean Nelson35727f72010-01-28 23:55:12 +0000550
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000551 mask = getaddrmask(flash->chip);
Sean Nelson35727f72010-01-28 23:55:12 +0000552 return erase_block_jedec_common(flash, page, size, mask);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000553}
554
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000555struct unlockblock {
556 unsigned int size;
557 unsigned int count;
558};
559
560typedef int (*unlockblock_func)(const struct flashctx *flash, chipaddr offset);
561static int regspace2_walk_unlockblocks(const struct flashctx *flash, const struct unlockblock *block, unlockblock_func func)
562{
563 chipaddr off = flash->virtual_registers + 2;
564 while (block->count != 0) {
565 unsigned int j;
566 for (j = 0; j < block->count; j++) {
567 if (func(flash, off))
568 return -1;
569 off += block->size;
570 }
571 block++;
572 }
573 return 0;
574}
575
576#define REG2_RWLOCK ((1 << 2) | (1 << 0))
577#define REG2_LOCKDOWN (1 << 1)
578#define REG2_MASK (REG2_RWLOCK | REG2_LOCKDOWN)
579
Stefan Tauner5859ced2014-12-20 16:45:31 +0000580static int printlock_regspace2_block(const struct flashctx *flash, chipaddr lockreg)
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000581{
Stefan Tauner5859ced2014-12-20 16:45:31 +0000582 uint8_t state = chip_readb(flash, lockreg);
583 msg_cdbg("Lock status of block at 0x%0*" PRIxPTR " is ", PRIxPTR_WIDTH, lockreg);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000584 switch (state & REG2_MASK) {
585 case 0:
586 msg_cdbg("Full Access.\n");
587 break;
588 case 1:
589 msg_cdbg("Write Lock (Default State).\n");
590 break;
591 case 2:
592 msg_cdbg("Locked Open (Full Access, Locked Down).\n");
593 break;
594 case 3:
595 msg_cdbg("Write Lock, Locked Down.\n");
596 break;
597 case 4:
598 msg_cdbg("Read Lock.\n");
599 break;
600 case 5:
601 msg_cdbg("Read/Write Lock.\n");
602 break;
603 case 6:
604 msg_cdbg("Read Lock, Locked Down.\n");
605 break;
606 case 7:
607 msg_cdbg("Read/Write Lock, Locked Down.\n");
608 break;
609 }
610 return 0;
611}
612
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000613static int printlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
614{
615 const unsigned int elems = flash->chip->total_size * 1024 / block_size;
616 struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
617 return regspace2_walk_unlockblocks(flash, blocks, &printlock_regspace2_block);
618}
619
620int printlock_regspace2_uniform_64k(struct flashctx *flash)
621{
622 return printlock_regspace2_uniform(flash, 64 * 1024);
623}
624
625int printlock_regspace2_block_eraser_0(struct flashctx *flash)
626{
627 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
628 const struct unlockblock *unlockblocks =
629 (const struct unlockblock *)flash->chip->block_erasers[0].eraseblocks;
630 return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block);
631}
632
633int printlock_regspace2_block_eraser_1(struct flashctx *flash)
634{
635 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
636 const struct unlockblock *unlockblocks =
637 (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks;
638 return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block);
639}
640
Stefan Tauner5859ced2014-12-20 16:45:31 +0000641/* Try to change the lock register at address lockreg from cur to new.
642 *
643 * - Try to unlock the lock bit if requested and it is currently set (although this is probably futile).
644 * - Try to change the read/write bits if requested.
645 * - Try to set the lockdown bit if requested.
646 * Return an error immediately if any of this fails. */
647static int changelock_regspace2_block(const struct flashctx *flash, chipaddr lockreg, uint8_t cur, uint8_t new)
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000648{
Stefan Tauner5859ced2014-12-20 16:45:31 +0000649 /* Only allow changes to known read/write/lockdown bits */
650 if (((cur ^ new) & ~REG2_MASK) != 0) {
651 msg_cerr("Invalid lock change from 0x%02x to 0x%02x requested at 0x%0*" PRIxPTR "!\n"
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000652 "Please report a bug at flashrom@flashrom.org\n",
Stefan Tauner5859ced2014-12-20 16:45:31 +0000653 cur, new, PRIxPTR_WIDTH, lockreg);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000654 return -1;
655 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000656
657 /* Exit early if no change (of read/write/lockdown bits) was requested. */
658 if (((cur ^ new) & REG2_MASK) == 0) {
659 msg_cdbg2("Lock bits at 0x%0*" PRIxPTR " not changed.\n", PRIxPTR_WIDTH, lockreg);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000660 return 0;
661 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000662
663 /* Normally the lockdown bit can not be cleared. Try nevertheless if requested. */
664 if ((cur & REG2_LOCKDOWN) && !(new & REG2_LOCKDOWN)) {
665 chip_writeb(flash, cur & ~REG2_LOCKDOWN, lockreg);
666 cur = chip_readb(flash, lockreg);
667 if ((cur & REG2_LOCKDOWN) == REG2_LOCKDOWN) {
668 msg_cwarn("Lockdown can't be removed at 0x%0*" PRIxPTR "! New value: 0x%02x.\n",
669 PRIxPTR_WIDTH, lockreg, cur);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000670 return -1;
671 }
672 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000673
674 /* Change read and/or write bit */
675 if ((cur ^ new) & REG2_RWLOCK) {
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000676 /* Do not lockdown yet. */
Stefan Tauner5859ced2014-12-20 16:45:31 +0000677 uint8_t wanted = (cur & ~REG2_RWLOCK) | (new & REG2_RWLOCK);
678 chip_writeb(flash, wanted, lockreg);
679 cur = chip_readb(flash, lockreg);
680 if (cur != wanted) {
681 msg_cerr("Changing lock bits failed at 0x%0*" PRIxPTR "! New value: 0x%02x.\n",
682 PRIxPTR_WIDTH, lockreg, cur);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000683 return -1;
684 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000685 msg_cdbg("Changed lock bits at 0x%0*" PRIxPTR " to 0x%02x.\n",
686 PRIxPTR_WIDTH, lockreg, cur);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000687 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000688
689 /* Eventually, enable lockdown if requested. */
690 if (!(cur & REG2_LOCKDOWN) && (new & REG2_LOCKDOWN)) {
691 chip_writeb(flash, new, lockreg);
692 cur = chip_readb(flash, lockreg);
693 if (cur != new) {
694 msg_cerr("Enabling lockdown FAILED at 0x%0*" PRIxPTR "! New value: 0x%02x.\n",
695 PRIxPTR_WIDTH, lockreg, cur);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000696 return -1;
697 }
Stefan Tauner5859ced2014-12-20 16:45:31 +0000698 msg_cdbg("Enabled lockdown at 0x%0*" PRIxPTR ".\n", PRIxPTR_WIDTH, lockreg);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000699 }
700
701 return 0;
702}
703
Stefan Tauner5859ced2014-12-20 16:45:31 +0000704static int unlock_regspace2_block_generic(const struct flashctx *flash, chipaddr lockreg)
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000705{
Stefan Tauner5859ced2014-12-20 16:45:31 +0000706 uint8_t old = chip_readb(flash, lockreg);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000707 /* 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 +0000708 return changelock_regspace2_block(flash, lockreg, old, old & ~REG2_RWLOCK);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000709}
710
711static int unlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
712{
713 const unsigned int elems = flash->chip->total_size * 1024 / block_size;
714 struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
Stefan Tauner5859ced2014-12-20 16:45:31 +0000715 return regspace2_walk_unlockblocks(flash, blocks, &unlock_regspace2_block_generic);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000716}
717
718int unlock_regspace2_uniform_64k(struct flashctx *flash)
719{
720 return unlock_regspace2_uniform(flash, 64 * 1024);
721}
722
723int unlock_regspace2_uniform_32k(struct flashctx *flash)
724{
725 return unlock_regspace2_uniform(flash, 32 * 1024);
726}
727
728int unlock_regspace2_block_eraser_0(struct flashctx *flash)
729{
730 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
731 const struct unlockblock *unlockblocks =
732 (const struct unlockblock *)flash->chip->block_erasers[0].eraseblocks;
Stefan Tauner5859ced2014-12-20 16:45:31 +0000733 return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block_generic);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000734}
735
736int unlock_regspace2_block_eraser_1(struct flashctx *flash)
737{
738 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
739 const struct unlockblock *unlockblocks =
740 (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks;
Stefan Tauner5859ced2014-12-20 16:45:31 +0000741 return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block_generic);
Carl-Daniel Hailfingeref3ac8a2014-08-03 13:05:34 +0000742}