blob: df40ec3fe396b067a46e1a24fa73e3b1b82976e0 [file] [log] [blame]
rminnich8d3ff912003-10-25 17:01:29 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
rminnich8d3ff912003-10-25 17:01:29 +00003 *
uwe555dd972007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2006 Giampiero Giancipoli <gianci@email.it>
6 * Copyright (C) 2006 coresystems GmbH <info@coresystems.de>
hailfinger428f2012007-12-31 01:49:00 +00007 * Copyright (C) 2007 Carl-Daniel Hailfinger
snelson63133f92010-01-04 17:15:23 +00008 * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com>
rminnich8d3ff912003-10-25 17:01:29 +00009 *
uweb25f1ea2007-08-29 17:52:32 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
rminnich8d3ff912003-10-25 17:01:29 +000014 *
uweb25f1ea2007-08-29 17:52:32 +000015 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
rminnich8d3ff912003-10-25 17:01:29 +000019 *
rminnich8d3ff912003-10-25 17:01:29 +000020 */
21
rminnich8d3ff912003-10-25 17:01:29 +000022#include "flash.h"
rminnich8d3ff912003-10-25 17:01:29 +000023
stepan7abc6322006-11-22 00:29:51 +000024#define MAX_REFLASH_TRIES 0x10
snelson63133f92010-01-04 17:15:23 +000025#define MASK_FULL 0xffff
26#define MASK_2AA 0x7ff
snelsonc6855342010-01-28 23:55:12 +000027#define MASK_AAA 0xfff
stepan7abc6322006-11-22 00:29:51 +000028
hailfinger79cf3672008-05-14 12:03:06 +000029/* Check one byte for odd parity */
30uint8_t oddparity(uint8_t val)
31{
32 val = (val ^ (val >> 4)) & 0xf;
33 val = (val ^ (val >> 2)) & 0x3;
34 return (val ^ (val >> 1)) & 0x1;
35}
36
Souvik Ghoshd75cd672016-06-17 14:21:39 -070037static void toggle_ready_jedec_common(const struct flashctx *flash, chipaddr dst, int delay)
uwedf467892007-08-23 10:20:40 +000038{
39 unsigned int i = 0;
40 uint8_t tmp1, tmp2;
41
Souvik Ghoshd75cd672016-06-17 14:21:39 -070042 tmp1 = chip_readb(flash, dst) & 0x40;
uwedf467892007-08-23 10:20:40 +000043
44 while (i++ < 0xFFFFFFF) {
hailfinger10023012009-12-17 16:20:26 +000045 if (delay)
46 programmer_delay(delay);
Souvik Ghoshd75cd672016-06-17 14:21:39 -070047 tmp2 = chip_readb(flash, dst) & 0x40;
uwedf467892007-08-23 10:20:40 +000048 if (tmp1 == tmp2) {
49 break;
50 }
51 tmp1 = tmp2;
52 }
hailfinger10023012009-12-17 16:20:26 +000053 if (i > 0x100000)
snelsonfc007bb2010-03-24 23:14:32 +000054 msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i);
hailfinger10023012009-12-17 16:20:26 +000055}
56
Souvik Ghoshd75cd672016-06-17 14:21:39 -070057void toggle_ready_jedec(const struct flashctx *flash, chipaddr dst)
hailfinger10023012009-12-17 16:20:26 +000058{
Souvik Ghoshd75cd672016-06-17 14:21:39 -070059 toggle_ready_jedec_common(flash, dst, 0);
hailfinger10023012009-12-17 16:20:26 +000060}
61
62/* Some chips require a minimum delay between toggle bit reads.
63 * The Winbond W39V040C wants 50 ms between reads on sector erase toggle,
64 * but experiments show that 2 ms are already enough. Pick a safety factor
65 * of 4 and use an 8 ms delay.
66 * Given that erase is slow on all chips, it is recommended to use
67 * toggle_ready_jedec_slow in erase functions.
68 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070069static void toggle_ready_jedec_slow(const struct flashctx *flash, chipaddr dst)
hailfinger10023012009-12-17 16:20:26 +000070{
Souvik Ghoshd75cd672016-06-17 14:21:39 -070071 toggle_ready_jedec_common(flash, dst, 8 * 1000);
uwedf467892007-08-23 10:20:40 +000072}
73
Souvik Ghoshd75cd672016-06-17 14:21:39 -070074void data_polling_jedec(const struct flashctx *flash, chipaddr dst, uint8_t data)
uwedf467892007-08-23 10:20:40 +000075{
76 unsigned int i = 0;
77 uint8_t tmp;
78
79 data &= 0x80;
80
81 while (i++ < 0xFFFFFFF) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -070082 tmp = chip_readb(flash, dst) & 0x80;
uwedf467892007-08-23 10:20:40 +000083 if (tmp == data) {
84 break;
85 }
86 }
hailfinger10023012009-12-17 16:20:26 +000087 if (i > 0x100000)
snelsonfc007bb2010-03-24 23:14:32 +000088 msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i);
uwedf467892007-08-23 10:20:40 +000089}
90
Souvik Ghoshd75cd672016-06-17 14:21:39 -070091static unsigned int getaddrmask(struct flashctx *flash)
hailfinger86bf3b52010-10-13 21:49:30 +000092{
Patrick Georgif3fa2992017-02-02 16:24:44 +010093 switch (flash->chip->feature_bits & FEATURE_ADDR_MASK) {
hailfinger86bf3b52010-10-13 21:49:30 +000094 case FEATURE_ADDR_FULL:
95 return MASK_FULL;
96 break;
97 case FEATURE_ADDR_2AA:
98 return MASK_2AA;
99 break;
100 case FEATURE_ADDR_AAA:
101 return MASK_AAA;
102 break;
103 default:
104 msg_cerr("%s called with unknown mask\n", __func__);
105 return 0;
106 break;
107 }
108}
109
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700110static void start_program_jedec_common(struct flashctx *flash, unsigned int mask)
uwedf467892007-08-23 10:20:40 +0000111{
snelson63133f92010-01-04 17:15:23 +0000112 chipaddr bios = flash->virtual_memory;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700113 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
114 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
115 chip_writeb(flash, 0xA0, bios + (0x5555 & mask));
uwedf467892007-08-23 10:20:40 +0000116}
117
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700118static int probe_jedec_common(struct flashctx *flash, unsigned int mask)
rminnich8d3ff912003-10-25 17:01:29 +0000119{
hailfinger82719632009-05-16 21:22:56 +0000120 chipaddr bios = flash->virtual_memory;
ollie6a600992005-11-26 21:55:36 +0000121 uint8_t id1, id2;
hailfinger428f2012007-12-31 01:49:00 +0000122 uint32_t largeid1, largeid2;
hailfinger027d7d92009-05-11 14:40:31 +0000123 uint32_t flashcontent1, flashcontent2;
hailfingerd5b35922009-06-03 14:46:22 +0000124 int probe_timing_enter, probe_timing_exit;
125
Patrick Georgif3fa2992017-02-02 16:24:44 +0100126 if (flash->chip->probe_timing > 0)
127 probe_timing_enter = probe_timing_exit = flash->chip->probe_timing;
128 else if (flash->chip->probe_timing == TIMING_ZERO) { /* No delay. */
hailfingerd5b35922009-06-03 14:46:22 +0000129 probe_timing_enter = probe_timing_exit = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100130 } else if (flash->chip->probe_timing == TIMING_FIXME) { /* == _IGNORED */
snelsonfc007bb2010-03-24 23:14:32 +0000131 msg_cdbg("Chip lacks correct probe timing information, "
hailfinger0cb68252009-07-23 01:33:43 +0000132 "using default 10mS/40uS. ");
hailfingerd5b35922009-06-03 14:46:22 +0000133 probe_timing_enter = 10000;
134 probe_timing_exit = 40;
135 } else {
snelsonfc007bb2010-03-24 23:14:32 +0000136 msg_cerr("Chip has negative value in probe_timing, failing "
hailfingerd5b35922009-06-03 14:46:22 +0000137 "without chip access\n");
138 return 0;
139 }
rminnich8d3ff912003-10-25 17:01:29 +0000140
hailfingerb07dc972010-10-20 21:13:19 +0000141 /* Earlier probes might have been too fast for the chip to enter ID
142 * mode completely. Allow the chip to finish this before seeing a
143 * reset command.
144 */
145 if (probe_timing_enter)
146 programmer_delay(probe_timing_enter);
147 /* Reset chip to a clean slate */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100148 if ((flash->chip->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET)
hailfingerb07dc972010-10-20 21:13:19 +0000149 {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700150 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
hailfingerb07dc972010-10-20 21:13:19 +0000151 if (probe_timing_exit)
152 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700153 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
hailfingerb07dc972010-10-20 21:13:19 +0000154 if (probe_timing_exit)
155 programmer_delay(10);
156 }
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700157 chip_writeb(flash, 0xF0, bios + (0x5555 & mask));
hailfingerb07dc972010-10-20 21:13:19 +0000158 if (probe_timing_exit)
159 programmer_delay(probe_timing_exit);
160
ollie5b621572004-03-20 16:46:10 +0000161 /* Issue JEDEC Product ID Entry command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700162 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000163 if (probe_timing_enter)
164 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700165 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000166 if (probe_timing_enter)
167 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700168 chip_writeb(flash, 0x90, bios + (0x5555 & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000169 if (probe_timing_enter)
170 programmer_delay(probe_timing_enter);
rminnich8d3ff912003-10-25 17:01:29 +0000171
ollie5b621572004-03-20 16:46:10 +0000172 /* Read product ID */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700173 id1 = chip_readb(flash, bios);
174 id2 = chip_readb(flash, bios + 0x01);
hailfinger428f2012007-12-31 01:49:00 +0000175 largeid1 = id1;
176 largeid2 = id2;
177
178 /* Check if it is a continuation ID, this should be a while loop. */
179 if (id1 == 0x7F) {
180 largeid1 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700181 id1 = chip_readb(flash, bios + 0x100);
hailfinger428f2012007-12-31 01:49:00 +0000182 largeid1 |= id1;
183 }
184 if (id2 == 0x7F) {
185 largeid2 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700186 id2 = chip_readb(flash, bios + 0x101);
hailfinger428f2012007-12-31 01:49:00 +0000187 largeid2 |= id2;
188 }
rminnich8d3ff912003-10-25 17:01:29 +0000189
ollie5b621572004-03-20 16:46:10 +0000190 /* Issue JEDEC Product ID Exit command */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100191 if ((flash->chip->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET)
snelson63133f92010-01-04 17:15:23 +0000192 {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700193 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
snelson63133f92010-01-04 17:15:23 +0000194 if (probe_timing_exit)
195 programmer_delay(10);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700196 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
snelson63133f92010-01-04 17:15:23 +0000197 if (probe_timing_exit)
198 programmer_delay(10);
199 }
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700200 chip_writeb(flash, 0xF0, bios + (0x5555 & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000201 if (probe_timing_exit)
202 programmer_delay(probe_timing_exit);
rminnich8d3ff912003-10-25 17:01:29 +0000203
snelsonfc007bb2010-03-24 23:14:32 +0000204 msg_cdbg("%s: id1 0x%02x, id2 0x%02x", __func__, largeid1, largeid2);
hailfinger79cf3672008-05-14 12:03:06 +0000205 if (!oddparity(id1))
snelsonfc007bb2010-03-24 23:14:32 +0000206 msg_cdbg(", id1 parity violation");
hailfinger027d7d92009-05-11 14:40:31 +0000207
208 /* Read the product ID location again. We should now see normal flash contents. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700209 flashcontent1 = chip_readb(flash, bios);
210 flashcontent2 = chip_readb(flash, bios + 0x01);
hailfinger027d7d92009-05-11 14:40:31 +0000211
212 /* Check if it is a continuation ID, this should be a while loop. */
213 if (flashcontent1 == 0x7F) {
214 flashcontent1 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700215 flashcontent1 |= chip_readb(flash, bios + 0x100);
hailfinger027d7d92009-05-11 14:40:31 +0000216 }
217 if (flashcontent2 == 0x7F) {
218 flashcontent2 <<= 8;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700219 flashcontent2 |= chip_readb(flash, bios + 0x101);
hailfinger027d7d92009-05-11 14:40:31 +0000220 }
221
222 if (largeid1 == flashcontent1)
snelsonfc007bb2010-03-24 23:14:32 +0000223 msg_cdbg(", id1 is normal flash content");
hailfinger027d7d92009-05-11 14:40:31 +0000224 if (largeid2 == flashcontent2)
snelsonfc007bb2010-03-24 23:14:32 +0000225 msg_cdbg(", id2 is normal flash content");
hailfinger027d7d92009-05-11 14:40:31 +0000226
snelsonfc007bb2010-03-24 23:14:32 +0000227 msg_cdbg("\n");
Patrick Georgif3fa2992017-02-02 16:24:44 +0100228 if (largeid1 != flash->chip->manufacture_id || largeid2 != flash->chip->model_id)
hailfingerafac00e2010-01-09 02:24:17 +0000229 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000230
Patrick Georgif3fa2992017-02-02 16:24:44 +0100231 if (flash->chip->feature_bits & FEATURE_REGISTERMAP)
snelson63133f92010-01-04 17:15:23 +0000232 map_flash_registers(flash);
233
hailfingerafac00e2010-01-09 02:24:17 +0000234 return 1;
olliea3def632004-03-19 22:10:07 +0000235}
236
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700237static int erase_sector_jedec_common(struct flashctx *flash, unsigned int page,
snelson63133f92010-01-04 17:15:23 +0000238 unsigned int pagesize, unsigned int mask)
olliea3def632004-03-19 22:10:07 +0000239{
hailfinger7af83692009-06-15 17:23:36 +0000240 chipaddr bios = flash->virtual_memory;
mkarcherf7af1b42011-04-15 00:03:37 +0000241 int delay_us = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100242 if(flash->chip->probe_timing != TIMING_ZERO)
mkarcherf7af1b42011-04-15 00:03:37 +0000243 delay_us = 10;
hailfinger7af83692009-06-15 17:23:36 +0000244
ollie5b621572004-03-20 16:46:10 +0000245 /* Issue the Sector Erase command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700246 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000247 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700248 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000249 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700250 chip_writeb(flash, 0x80, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000251 programmer_delay(delay_us);
ollie0eb62d62004-12-08 20:10:01 +0000252
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700253 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000254 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700255 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000256 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700257 chip_writeb(flash, 0x30, bios + page);
mkarcherf7af1b42011-04-15 00:03:37 +0000258 programmer_delay(delay_us);
ollie5b621572004-03-20 16:46:10 +0000259
olliea3def632004-03-19 22:10:07 +0000260 /* wait for Toggle bit ready */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700261 toggle_ready_jedec_slow(flash, bios);
olliea3def632004-03-19 22:10:07 +0000262
hailfingerac8e3182011-06-26 17:04:16 +0000263 /* FIXME: Check the status register for errors. */
uwebe4477b2007-08-23 16:08:21 +0000264 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000265}
olliea4302802004-12-07 03:15:51 +0000266
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700267static int erase_block_jedec_common(struct flashctx *flash, unsigned int block,
snelson63133f92010-01-04 17:15:23 +0000268 unsigned int blocksize, unsigned int mask)
rminnichdfcbaa72004-09-30 16:37:01 +0000269{
hailfinger7af83692009-06-15 17:23:36 +0000270 chipaddr bios = flash->virtual_memory;
mkarcherf7af1b42011-04-15 00:03:37 +0000271 int delay_us = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100272 if(flash->chip->probe_timing != TIMING_ZERO)
mkarcherf7af1b42011-04-15 00:03:37 +0000273 delay_us = 10;
hailfinger7af83692009-06-15 17:23:36 +0000274
rminnichdfcbaa72004-09-30 16:37:01 +0000275 /* Issue the Sector Erase command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700276 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000277 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700278 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000279 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700280 chip_writeb(flash, 0x80, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000281 programmer_delay(delay_us);
ollie0eb62d62004-12-08 20:10:01 +0000282
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700283 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000284 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700285 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000286 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700287 chip_writeb(flash, 0x50, bios + block);
mkarcherf7af1b42011-04-15 00:03:37 +0000288 programmer_delay(delay_us);
rminnichdfcbaa72004-09-30 16:37:01 +0000289
290 /* wait for Toggle bit ready */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700291 toggle_ready_jedec_slow(flash, bios);
rminnichdfcbaa72004-09-30 16:37:01 +0000292
hailfingerac8e3182011-06-26 17:04:16 +0000293 /* FIXME: Check the status register for errors. */
uwebe4477b2007-08-23 16:08:21 +0000294 return 0;
rminnichdfcbaa72004-09-30 16:37:01 +0000295}
rminnich8d3ff912003-10-25 17:01:29 +0000296
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700297static int erase_chip_jedec_common(struct flashctx *flash, unsigned int mask)
rminnich8d3ff912003-10-25 17:01:29 +0000298{
hailfinger82719632009-05-16 21:22:56 +0000299 chipaddr bios = flash->virtual_memory;
mkarcherf7af1b42011-04-15 00:03:37 +0000300 int delay_us = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100301 if(flash->chip->probe_timing != TIMING_ZERO)
mkarcherf7af1b42011-04-15 00:03:37 +0000302 delay_us = 10;
rminnich8d3ff912003-10-25 17:01:29 +0000303
ollie5b621572004-03-20 16:46:10 +0000304 /* Issue the JEDEC Chip Erase command */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700305 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000306 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700307 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000308 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700309 chip_writeb(flash, 0x80, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000310 programmer_delay(delay_us);
ollie0eb62d62004-12-08 20:10:01 +0000311
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700312 chip_writeb(flash, 0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000313 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700314 chip_writeb(flash, 0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000315 programmer_delay(delay_us);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700316 chip_writeb(flash, 0x10, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000317 programmer_delay(delay_us);
olliea3def632004-03-19 22:10:07 +0000318
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700319 toggle_ready_jedec_slow(flash, bios);
rminnich8d3ff912003-10-25 17:01:29 +0000320
hailfingerac8e3182011-06-26 17:04:16 +0000321 /* FIXME: Check the status register for errors. */
uwebe4477b2007-08-23 16:08:21 +0000322 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000323}
324
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700325static int write_byte_program_jedec_common(struct flashctx *flash, uint8_t *src,
snelson63133f92010-01-04 17:15:23 +0000326 chipaddr dst, unsigned int mask)
327{
328 int tried = 0, failed = 0;
329 chipaddr bios = flash->virtual_memory;
330
331 /* If the data is 0xFF, don't program it and don't complain. */
332 if (*src == 0xFF) {
333 return 0;
334 }
335
336retry:
337 /* Issue JEDEC Byte Program command */
338 start_program_jedec_common(flash, mask);
339
340 /* transfer data from source to destination */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700341 chip_writeb(flash, *src, dst);
342 toggle_ready_jedec(flash, bios);
snelson63133f92010-01-04 17:15:23 +0000343
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700344 if (chip_readb(flash, dst) != *src && tried++ < MAX_REFLASH_TRIES) {
snelson63133f92010-01-04 17:15:23 +0000345 goto retry;
346 }
347
348 if (tried >= MAX_REFLASH_TRIES)
349 failed = 1;
350
351 return failed;
352}
353
hailfinger71e1bd42010-10-13 22:26:56 +0000354/* chunksize is 1 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700355int write_jedec_1(struct flashctx *flash, uint8_t *src, unsigned int start, unsigned int len)
snelson63133f92010-01-04 17:15:23 +0000356{
357 int i, failed = 0;
hailfingera10a6072010-10-10 14:02:27 +0000358 chipaddr dst = flash->virtual_memory + start;
snelson63133f92010-01-04 17:15:23 +0000359 chipaddr olddst;
stefanctc5eb8a92011-11-23 09:13:48 +0000360 unsigned int mask;
hailfinger86bf3b52010-10-13 21:49:30 +0000361
362 mask = getaddrmask(flash);
snelson63133f92010-01-04 17:15:23 +0000363
364 olddst = dst;
hailfingera10a6072010-10-10 14:02:27 +0000365 for (i = 0; i < len; i++) {
snelson63133f92010-01-04 17:15:23 +0000366 if (write_byte_program_jedec_common(flash, src, dst, mask))
367 failed = 1;
368 dst++, src++;
369 }
370 if (failed)
snelsonfc007bb2010-03-24 23:14:32 +0000371 msg_cerr(" writing sector at 0x%lx failed!\n", olddst);
snelson63133f92010-01-04 17:15:23 +0000372
373 return failed;
374}
375
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700376int write_page_write_jedec_common(struct flashctx *flash, uint8_t *src, unsigned int start, unsigned int page_size)
rminnich8d3ff912003-10-25 17:01:29 +0000377{
hailfingerf2ac27e2009-11-25 16:41:50 +0000378 int i, tried = 0, failed;
stepan7abc6322006-11-22 00:29:51 +0000379 uint8_t *s = src;
hailfingerc2cfc592009-06-25 13:57:31 +0000380 chipaddr bios = flash->virtual_memory;
381 chipaddr dst = bios + start;
382 chipaddr d = dst;
stefanctc5eb8a92011-11-23 09:13:48 +0000383 unsigned int mask;
hailfinger86bf3b52010-10-13 21:49:30 +0000384
385 mask = getaddrmask(flash);
rminnich8d3ff912003-10-25 17:01:29 +0000386
stepan7abc6322006-11-22 00:29:51 +0000387retry:
uwe3a3ab2f2010-03-25 23:18:41 +0000388 /* Issue JEDEC Start Program command */
snelson63133f92010-01-04 17:15:23 +0000389 start_program_jedec_common(flash, mask);
ollie5b621572004-03-20 16:46:10 +0000390
olliea4302802004-12-07 03:15:51 +0000391 /* transfer data from source to destination */
hailfingerfe072472009-11-14 03:48:33 +0000392 for (i = 0; i < page_size; i++) {
olliea4302802004-12-07 03:15:51 +0000393 /* If the data is 0xFF, don't program it */
uwef6641642007-05-09 10:17:44 +0000394 if (*src != 0xFF)
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700395 chip_writeb(flash, *src, dst);
stepan7abc6322006-11-22 00:29:51 +0000396 dst++;
397 src++;
ollie5b621572004-03-20 16:46:10 +0000398 }
399
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700400 toggle_ready_jedec(flash, dst - 1);
olliea4302802004-12-07 03:15:51 +0000401
stepan7abc6322006-11-22 00:29:51 +0000402 dst = d;
403 src = s;
hailfingerf2ac27e2009-11-25 16:41:50 +0000404 failed = verify_range(flash, src, start, page_size, NULL);
uwef6641642007-05-09 10:17:44 +0000405
hailfingerf2ac27e2009-11-25 16:41:50 +0000406 if (failed && tried++ < MAX_REFLASH_TRIES) {
snelsonfc007bb2010-03-24 23:14:32 +0000407 msg_cerr("retrying.\n");
uwef6641642007-05-09 10:17:44 +0000408 goto retry;
409 }
hailfingerf2ac27e2009-11-25 16:41:50 +0000410 if (failed) {
snelsonfc007bb2010-03-24 23:14:32 +0000411 msg_cerr(" page 0x%lx failed!\n",
hailfinger82719632009-05-16 21:22:56 +0000412 (d - bios) / page_size);
stepan7abc6322006-11-22 00:29:51 +0000413 }
hailfingerf2ac27e2009-11-25 16:41:50 +0000414 return failed;
ollie5b621572004-03-20 16:46:10 +0000415}
416
hailfinger71e1bd42010-10-13 22:26:56 +0000417/* chunksize is page_size */
hailfinger86bf3b52010-10-13 21:49:30 +0000418/*
419 * Write a part of the flash chip.
420 * FIXME: Use the chunk code from Michael Karcher instead.
421 * This function is a slightly modified copy of spi_write_chunked.
422 * Each page is written separately in chunks with a maximum size of chunksize.
423 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700424int write_jedec(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger80dea312010-01-09 03:15:50 +0000425{
stefanctc5eb8a92011-11-23 09:13:48 +0000426 unsigned int i, starthere, lenhere;
hailfinger86bf3b52010-10-13 21:49:30 +0000427 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700428 * in struct flashctx to do this properly. All chips using
hailfinger86bf3b52010-10-13 21:49:30 +0000429 * write_jedec have page_size set to max_writechunk_size, so
430 * we're OK for now.
431 */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100432 unsigned int page_size = flash->chip->page_size;
ollie5b621572004-03-20 16:46:10 +0000433
hailfinger86bf3b52010-10-13 21:49:30 +0000434 /* Warning: This loop has a very unusual condition and body.
435 * The loop needs to go through each page with at least one affected
436 * byte. The lowest page number is (start / page_size) since that
437 * division rounds down. The highest page number we want is the page
438 * where the last byte of the range lives. That last byte has the
439 * address (start + len - 1), thus the highest page number is
440 * (start + len - 1) / page_size. Since we want to include that last
441 * page as well, the loop condition uses <=.
442 */
443 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
444 /* Byte position of the first byte in the range in this page. */
445 /* starthere is an offset to the base address of the chip. */
446 starthere = max(start, i * page_size);
447 /* Length of bytes in the range in this page. */
448 lenhere = min(start + len, (i + 1) * page_size) - starthere;
snelsonc6855342010-01-28 23:55:12 +0000449
hailfinger86bf3b52010-10-13 21:49:30 +0000450 if (write_page_write_jedec_common(flash, buf + starthere - start, starthere, lenhere))
451 return 1;
rminnich8d3ff912003-10-25 17:01:29 +0000452 }
rminnich8d3ff912003-10-25 17:01:29 +0000453
hailfinger86bf3b52010-10-13 21:49:30 +0000454 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000455}
hailfingerfff99532009-11-27 17:49:42 +0000456
snelson63133f92010-01-04 17:15:23 +0000457/* erase chip with block_erase() prototype */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700458int erase_chip_block_jedec(struct flashctx *flash, unsigned int addr,
snelson63133f92010-01-04 17:15:23 +0000459 unsigned int blocksize)
460{
stefanctc5eb8a92011-11-23 09:13:48 +0000461 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000462
463 mask = getaddrmask(flash);
Patrick Georgif3fa2992017-02-02 16:24:44 +0100464 if ((addr != 0) || (blocksize != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000465 msg_cerr("%s called with incorrect arguments\n",
snelson63133f92010-01-04 17:15:23 +0000466 __func__);
467 return -1;
468 }
snelsonc6855342010-01-28 23:55:12 +0000469 return erase_chip_jedec_common(flash, mask);
snelson63133f92010-01-04 17:15:23 +0000470}
471
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700472int probe_jedec(struct flashctx *flash)
snelson63133f92010-01-04 17:15:23 +0000473{
stefanctc5eb8a92011-11-23 09:13:48 +0000474 unsigned int mask;
hailfinger80dea312010-01-09 03:15:50 +0000475
476 mask = getaddrmask(flash);
snelsonc6855342010-01-28 23:55:12 +0000477 return probe_jedec_common(flash, mask);
snelson63133f92010-01-04 17:15:23 +0000478}
479
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700480int erase_sector_jedec(struct flashctx *flash, unsigned int page, unsigned int size)
snelson63133f92010-01-04 17:15:23 +0000481{
stefanctc5eb8a92011-11-23 09:13:48 +0000482 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000483
484 mask = getaddrmask(flash);
485 return erase_sector_jedec_common(flash, page, size, mask);
snelson63133f92010-01-04 17:15:23 +0000486}
487
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700488int erase_block_jedec(struct flashctx *flash, unsigned int page, unsigned int size)
snelson63133f92010-01-04 17:15:23 +0000489{
stefanctc5eb8a92011-11-23 09:13:48 +0000490 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000491
492 mask = getaddrmask(flash);
493 return erase_block_jedec_common(flash, page, size, mask);
snelson63133f92010-01-04 17:15:23 +0000494}
495
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700496int erase_chip_jedec(struct flashctx *flash)
snelson63133f92010-01-04 17:15:23 +0000497{
stefanctc5eb8a92011-11-23 09:13:48 +0000498 unsigned int mask;
snelsonc6855342010-01-28 23:55:12 +0000499
500 mask = getaddrmask(flash);
501 return erase_chip_jedec_common(flash, mask);
snelson63133f92010-01-04 17:15:23 +0000502}
Alan Greend793f5b2019-09-02 17:03:51 +1000503
504struct unlockblock {
505 unsigned int size;
506 unsigned int count;
507};
508
509typedef int (*unlockblock_func)(const struct flashctx *flash, chipaddr offset);
510static int regspace2_walk_unlockblocks(const struct flashctx *flash, const struct unlockblock *block, unlockblock_func func)
511{
512 chipaddr off = flash->virtual_registers + 2;
513 while (block->count != 0) {
514 unsigned int j;
515 for (j = 0; j < block->count; j++) {
516 if (func(flash, off))
517 return -1;
518 off += block->size;
519 }
520 block++;
521 }
522 return 0;
523}
524
525#define REG2_RWLOCK ((1 << 2) | (1 << 0))
526#define REG2_LOCKDOWN (1 << 1)
527#define REG2_MASK (REG2_RWLOCK | REG2_LOCKDOWN)
528
529static int printlock_regspace2_block(const struct flashctx *flash, chipaddr lockreg)
530{
531 uint8_t state = chip_readb(flash, lockreg);
532 msg_cdbg("Lock status of block at %p is ", (void *)lockreg);
533 switch (state & REG2_MASK) {
534 case 0:
535 msg_cdbg("Full Access.\n");
536 break;
537 case 1:
538 msg_cdbg("Write Lock (Default State).\n");
539 break;
540 case 2:
541 msg_cdbg("Locked Open (Full Access, Locked Down).\n");
542 break;
543 case 3:
544 msg_cdbg("Write Lock, Locked Down.\n");
545 break;
546 case 4:
547 msg_cdbg("Read Lock.\n");
548 break;
549 case 5:
550 msg_cdbg("Read/Write Lock.\n");
551 break;
552 case 6:
553 msg_cdbg("Read Lock, Locked Down.\n");
554 break;
555 case 7:
556 msg_cdbg("Read/Write Lock, Locked Down.\n");
557 break;
558 }
559 return 0;
560}
561
562static int printlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
563{
564 const unsigned int elems = flash->chip->total_size * 1024 / block_size;
565 struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
566 return regspace2_walk_unlockblocks(flash, blocks, &printlock_regspace2_block);
567}
568
569int printlock_regspace2_uniform_64k(struct flashctx *flash)
570{
571 return printlock_regspace2_uniform(flash, 64 * 1024);
572}
573
574int printlock_regspace2_block_eraser_0(struct flashctx *flash)
575{
576 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
577 const struct unlockblock *unlockblocks =
578 (const struct unlockblock *)flash->chip->block_erasers[0].eraseblocks;
579 return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block);
580}
581
582int printlock_regspace2_block_eraser_1(struct flashctx *flash)
583{
584 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
585 const struct unlockblock *unlockblocks =
586 (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks;
587 return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block);
588}
589
590/* Try to change the lock register at address lockreg from cur to new.
591 *
592 * - Try to unlock the lock bit if requested and it is currently set (although this is probably futile).
593 * - Try to change the read/write bits if requested.
594 * - Try to set the lockdown bit if requested.
595 * Return an error immediately if any of this fails. */
596static int changelock_regspace2_block(const struct flashctx *flash, chipaddr lockreg, uint8_t cur, uint8_t new)
597{
598 /* Only allow changes to known read/write/lockdown bits */
599 if (((cur ^ new) & ~REG2_MASK) != 0) {
600 msg_cerr("Invalid lock change from 0x%02x to 0x%02x requested at %p!\n"
601 "Please report a bug at flashrom@flashrom.org\n",
602 cur, new, (void *)lockreg);
603 return -1;
604 }
605
606 /* Exit early if no change (of read/write/lockdown bits) was requested. */
607 if (((cur ^ new) & REG2_MASK) == 0) {
608 msg_cdbg2("Lock bits at %p not changed.\n", (void *) lockreg);
609 return 0;
610 }
611
612 /* Normally the lockdown bit can not be cleared. Try nevertheless if requested. */
613 if ((cur & REG2_LOCKDOWN) && !(new & REG2_LOCKDOWN)) {
614 chip_writeb(flash, cur & ~REG2_LOCKDOWN, lockreg);
615 cur = chip_readb(flash, lockreg);
616 if ((cur & REG2_LOCKDOWN) == REG2_LOCKDOWN) {
617 msg_cwarn("Lockdown can't be removed at %p! New value: 0x%02x.\n",
618 (void *) lockreg, cur);
619 return -1;
620 }
621 }
622
623 /* Change read and/or write bit */
624 if ((cur ^ new) & REG2_RWLOCK) {
625 /* Do not lockdown yet. */
626 uint8_t wanted = (cur & ~REG2_RWLOCK) | (new & REG2_RWLOCK);
627 chip_writeb(flash, wanted, lockreg);
628 cur = chip_readb(flash, lockreg);
629 if (cur != wanted) {
630 msg_cerr("Changing lock bits failed at %p! New value: 0x%02x.\n",
631 (void *) lockreg, cur);
632 return -1;
633 }
634 msg_cdbg("Changed lock bits at %p to 0x%02x.\n", (void *) lockreg, cur);
635 }
636
637 /* Eventually, enable lockdown if requested. */
638 if (!(cur & REG2_LOCKDOWN) && (new & REG2_LOCKDOWN)) {
639 chip_writeb(flash, new, lockreg);
640 cur = chip_readb(flash, lockreg);
641 if (cur != new) {
642 msg_cerr("Enabling lockdown FAILED at %p! New value: 0x%02x.\n",
643 (void *) lockreg, cur);
644 return -1;
645 }
646 msg_cdbg("Enabled lockdown at %p\n", (void *) lockreg);
647 }
648
649 return 0;
650}
651
652static int unlock_regspace2_block_generic(const struct flashctx *flash, chipaddr lockreg)
653{
654 uint8_t old = chip_readb(flash, lockreg);
655 /* We don't care for the lockdown bit as long as the RW locks are 0 after we're done */
656 return changelock_regspace2_block(flash, lockreg, old, old & ~REG2_RWLOCK);
657}
658
659static int unlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
660{
661 const unsigned int elems = flash->chip->total_size * 1024 / block_size;
662 struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
663 return regspace2_walk_unlockblocks(flash, blocks, &unlock_regspace2_block_generic);
664}
665
666int unlock_regspace2_uniform_64k(struct flashctx *flash)
667{
668 return unlock_regspace2_uniform(flash, 64 * 1024);
669}
670
671int unlock_regspace2_uniform_32k(struct flashctx *flash)
672{
673 return unlock_regspace2_uniform(flash, 32 * 1024);
674}
675
676int unlock_regspace2_block_eraser_0(struct flashctx *flash)
677{
678 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
679 const struct unlockblock *unlockblocks =
680 (const struct unlockblock *)flash->chip->block_erasers[0].eraseblocks;
681 return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block_generic);
682}
683
684int unlock_regspace2_block_eraser_1(struct flashctx *flash)
685{
686 // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated).
687 const struct unlockblock *unlockblocks =
688 (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks;
689 return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block_generic);
690}