blob: f23cf5352c5fe7605a5a75e6fb91981df28f5b42 [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 *
uweb25f1ea2007-08-29 17:52:32 +000020 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
rminnich8d3ff912003-10-25 17:01:29 +000023 */
24
rminnich8d3ff912003-10-25 17:01:29 +000025#include "flash.h"
snelson8913d082010-02-26 05:48:29 +000026#include "chipdrivers.h"
rminnich8d3ff912003-10-25 17:01:29 +000027
stepan7abc6322006-11-22 00:29:51 +000028#define MAX_REFLASH_TRIES 0x10
snelson63133f92010-01-04 17:15:23 +000029#define MASK_FULL 0xffff
30#define MASK_2AA 0x7ff
snelsonc6855342010-01-28 23:55:12 +000031#define MASK_AAA 0xfff
stepan7abc6322006-11-22 00:29:51 +000032
hailfinger79cf3672008-05-14 12:03:06 +000033/* Check one byte for odd parity */
34uint8_t oddparity(uint8_t val)
35{
36 val = (val ^ (val >> 4)) & 0xf;
37 val = (val ^ (val >> 2)) & 0x3;
38 return (val ^ (val >> 1)) & 0x1;
39}
40
hailfinger1ff33dc2010-07-03 11:02:10 +000041static void toggle_ready_jedec_common(chipaddr dst, int delay)
uwedf467892007-08-23 10:20:40 +000042{
43 unsigned int i = 0;
44 uint8_t tmp1, tmp2;
45
hailfinger1ff6e362009-03-06 22:26:00 +000046 tmp1 = chip_readb(dst) & 0x40;
uwedf467892007-08-23 10:20:40 +000047
48 while (i++ < 0xFFFFFFF) {
hailfinger10023012009-12-17 16:20:26 +000049 if (delay)
50 programmer_delay(delay);
hailfinger1ff6e362009-03-06 22:26:00 +000051 tmp2 = chip_readb(dst) & 0x40;
uwedf467892007-08-23 10:20:40 +000052 if (tmp1 == tmp2) {
53 break;
54 }
55 tmp1 = tmp2;
56 }
hailfinger10023012009-12-17 16:20:26 +000057 if (i > 0x100000)
snelsonfc007bb2010-03-24 23:14:32 +000058 msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i);
hailfinger10023012009-12-17 16:20:26 +000059}
60
61void toggle_ready_jedec(chipaddr dst)
62{
63 toggle_ready_jedec_common(dst, 0);
64}
65
66/* Some chips require a minimum delay between toggle bit reads.
67 * The Winbond W39V040C wants 50 ms between reads on sector erase toggle,
68 * but experiments show that 2 ms are already enough. Pick a safety factor
69 * of 4 and use an 8 ms delay.
70 * Given that erase is slow on all chips, it is recommended to use
71 * toggle_ready_jedec_slow in erase functions.
72 */
hailfinger1ff33dc2010-07-03 11:02:10 +000073static void toggle_ready_jedec_slow(chipaddr dst)
hailfinger10023012009-12-17 16:20:26 +000074{
75 toggle_ready_jedec_common(dst, 8 * 1000);
uwedf467892007-08-23 10:20:40 +000076}
77
hailfinger82719632009-05-16 21:22:56 +000078void data_polling_jedec(chipaddr dst, uint8_t data)
uwedf467892007-08-23 10:20:40 +000079{
80 unsigned int i = 0;
81 uint8_t tmp;
82
83 data &= 0x80;
84
85 while (i++ < 0xFFFFFFF) {
hailfinger1ff6e362009-03-06 22:26:00 +000086 tmp = chip_readb(dst) & 0x80;
uwedf467892007-08-23 10:20:40 +000087 if (tmp == data) {
88 break;
89 }
90 }
hailfinger10023012009-12-17 16:20:26 +000091 if (i > 0x100000)
snelsonfc007bb2010-03-24 23:14:32 +000092 msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i);
uwedf467892007-08-23 10:20:40 +000093}
94
hailfinger86bf3b52010-10-13 21:49:30 +000095static int getaddrmask(struct flashchip *flash)
96{
97 switch (flash->feature_bits & FEATURE_ADDR_MASK) {
98 case FEATURE_ADDR_FULL:
99 return MASK_FULL;
100 break;
101 case FEATURE_ADDR_2AA:
102 return MASK_2AA;
103 break;
104 case FEATURE_ADDR_AAA:
105 return MASK_AAA;
106 break;
107 default:
108 msg_cerr("%s called with unknown mask\n", __func__);
109 return 0;
110 break;
111 }
112}
113
hailfinger1ff33dc2010-07-03 11:02:10 +0000114static void start_program_jedec_common(struct flashchip *flash, unsigned int mask)
uwedf467892007-08-23 10:20:40 +0000115{
snelson63133f92010-01-04 17:15:23 +0000116 chipaddr bios = flash->virtual_memory;
117 chip_writeb(0xAA, bios + (0x5555 & mask));
118 chip_writeb(0x55, bios + (0x2AAA & mask));
119 chip_writeb(0xA0, bios + (0x5555 & mask));
uwedf467892007-08-23 10:20:40 +0000120}
121
hailfinger1ff33dc2010-07-03 11:02:10 +0000122static int probe_jedec_common(struct flashchip *flash, unsigned int mask)
rminnich8d3ff912003-10-25 17:01:29 +0000123{
hailfinger82719632009-05-16 21:22:56 +0000124 chipaddr bios = flash->virtual_memory;
ollie6a600992005-11-26 21:55:36 +0000125 uint8_t id1, id2;
hailfinger428f2012007-12-31 01:49:00 +0000126 uint32_t largeid1, largeid2;
hailfinger027d7d92009-05-11 14:40:31 +0000127 uint32_t flashcontent1, flashcontent2;
hailfingerd5b35922009-06-03 14:46:22 +0000128 int probe_timing_enter, probe_timing_exit;
129
130 if (flash->probe_timing > 0)
131 probe_timing_enter = probe_timing_exit = flash->probe_timing;
132 else if (flash->probe_timing == TIMING_ZERO) { /* No delay. */
133 probe_timing_enter = probe_timing_exit = 0;
134 } else if (flash->probe_timing == TIMING_FIXME) { /* == _IGNORED */
snelsonfc007bb2010-03-24 23:14:32 +0000135 msg_cdbg("Chip lacks correct probe timing information, "
hailfinger0cb68252009-07-23 01:33:43 +0000136 "using default 10mS/40uS. ");
hailfingerd5b35922009-06-03 14:46:22 +0000137 probe_timing_enter = 10000;
138 probe_timing_exit = 40;
139 } else {
snelsonfc007bb2010-03-24 23:14:32 +0000140 msg_cerr("Chip has negative value in probe_timing, failing "
hailfingerd5b35922009-06-03 14:46:22 +0000141 "without chip access\n");
142 return 0;
143 }
rminnich8d3ff912003-10-25 17:01:29 +0000144
hailfingerb07dc972010-10-20 21:13:19 +0000145 /* Earlier probes might have been too fast for the chip to enter ID
146 * mode completely. Allow the chip to finish this before seeing a
147 * reset command.
148 */
149 if (probe_timing_enter)
150 programmer_delay(probe_timing_enter);
151 /* Reset chip to a clean slate */
152 if ((flash->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET)
153 {
154 chip_writeb(0xAA, bios + (0x5555 & mask));
155 if (probe_timing_exit)
156 programmer_delay(10);
157 chip_writeb(0x55, bios + (0x2AAA & mask));
158 if (probe_timing_exit)
159 programmer_delay(10);
160 }
161 chip_writeb(0xF0, bios + (0x5555 & mask));
162 if (probe_timing_exit)
163 programmer_delay(probe_timing_exit);
164
ollie5b621572004-03-20 16:46:10 +0000165 /* Issue JEDEC Product ID Entry command */
snelson63133f92010-01-04 17:15:23 +0000166 chip_writeb(0xAA, bios + (0x5555 & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000167 if (probe_timing_enter)
168 programmer_delay(10);
snelson63133f92010-01-04 17:15:23 +0000169 chip_writeb(0x55, bios + (0x2AAA & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000170 if (probe_timing_enter)
171 programmer_delay(10);
snelson63133f92010-01-04 17:15:23 +0000172 chip_writeb(0x90, bios + (0x5555 & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000173 if (probe_timing_enter)
174 programmer_delay(probe_timing_enter);
rminnich8d3ff912003-10-25 17:01:29 +0000175
ollie5b621572004-03-20 16:46:10 +0000176 /* Read product ID */
hailfinger1ff6e362009-03-06 22:26:00 +0000177 id1 = chip_readb(bios);
178 id2 = chip_readb(bios + 0x01);
hailfinger428f2012007-12-31 01:49:00 +0000179 largeid1 = id1;
180 largeid2 = id2;
181
182 /* Check if it is a continuation ID, this should be a while loop. */
183 if (id1 == 0x7F) {
184 largeid1 <<= 8;
hailfinger1ff6e362009-03-06 22:26:00 +0000185 id1 = chip_readb(bios + 0x100);
hailfinger428f2012007-12-31 01:49:00 +0000186 largeid1 |= id1;
187 }
188 if (id2 == 0x7F) {
189 largeid2 <<= 8;
hailfinger1ff6e362009-03-06 22:26:00 +0000190 id2 = chip_readb(bios + 0x101);
hailfinger428f2012007-12-31 01:49:00 +0000191 largeid2 |= id2;
192 }
rminnich8d3ff912003-10-25 17:01:29 +0000193
ollie5b621572004-03-20 16:46:10 +0000194 /* Issue JEDEC Product ID Exit command */
hailfingerb07dc972010-10-20 21:13:19 +0000195 if ((flash->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET)
snelson63133f92010-01-04 17:15:23 +0000196 {
197 chip_writeb(0xAA, bios + (0x5555 & mask));
198 if (probe_timing_exit)
199 programmer_delay(10);
200 chip_writeb(0x55, bios + (0x2AAA & mask));
201 if (probe_timing_exit)
202 programmer_delay(10);
203 }
204 chip_writeb(0xF0, bios + (0x5555 & mask));
hailfingerc7568d52009-12-17 04:22:40 +0000205 if (probe_timing_exit)
206 programmer_delay(probe_timing_exit);
rminnich8d3ff912003-10-25 17:01:29 +0000207
snelsonfc007bb2010-03-24 23:14:32 +0000208 msg_cdbg("%s: id1 0x%02x, id2 0x%02x", __func__, largeid1, largeid2);
hailfinger79cf3672008-05-14 12:03:06 +0000209 if (!oddparity(id1))
snelsonfc007bb2010-03-24 23:14:32 +0000210 msg_cdbg(", id1 parity violation");
hailfinger027d7d92009-05-11 14:40:31 +0000211
212 /* Read the product ID location again. We should now see normal flash contents. */
213 flashcontent1 = chip_readb(bios);
214 flashcontent2 = chip_readb(bios + 0x01);
215
216 /* Check if it is a continuation ID, this should be a while loop. */
217 if (flashcontent1 == 0x7F) {
218 flashcontent1 <<= 8;
219 flashcontent1 |= chip_readb(bios + 0x100);
220 }
221 if (flashcontent2 == 0x7F) {
222 flashcontent2 <<= 8;
223 flashcontent2 |= chip_readb(bios + 0x101);
224 }
225
226 if (largeid1 == flashcontent1)
snelsonfc007bb2010-03-24 23:14:32 +0000227 msg_cdbg(", id1 is normal flash content");
hailfinger027d7d92009-05-11 14:40:31 +0000228 if (largeid2 == flashcontent2)
snelsonfc007bb2010-03-24 23:14:32 +0000229 msg_cdbg(", id2 is normal flash content");
hailfinger027d7d92009-05-11 14:40:31 +0000230
snelsonfc007bb2010-03-24 23:14:32 +0000231 msg_cdbg("\n");
hailfingerafac00e2010-01-09 02:24:17 +0000232 if (largeid1 != flash->manufacture_id || largeid2 != flash->model_id)
233 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000234
snelson63133f92010-01-04 17:15:23 +0000235 if (flash->feature_bits & FEATURE_REGISTERMAP)
236 map_flash_registers(flash);
237
hailfingerafac00e2010-01-09 02:24:17 +0000238 return 1;
olliea3def632004-03-19 22:10:07 +0000239}
240
hailfinger1ff33dc2010-07-03 11:02:10 +0000241static int erase_sector_jedec_common(struct flashchip *flash, unsigned int page,
snelson63133f92010-01-04 17:15:23 +0000242 unsigned int pagesize, unsigned int mask)
olliea3def632004-03-19 22:10:07 +0000243{
hailfinger7af83692009-06-15 17:23:36 +0000244 chipaddr bios = flash->virtual_memory;
mkarcherf7af1b42011-04-15 00:03:37 +0000245 int delay_us = 0;
246 if(flash->probe_timing != TIMING_ZERO)
247 delay_us = 10;
hailfinger7af83692009-06-15 17:23:36 +0000248
ollie5b621572004-03-20 16:46:10 +0000249 /* Issue the Sector Erase command */
snelson63133f92010-01-04 17:15:23 +0000250 chip_writeb(0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000251 programmer_delay(delay_us);
snelson63133f92010-01-04 17:15:23 +0000252 chip_writeb(0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000253 programmer_delay(delay_us);
snelson63133f92010-01-04 17:15:23 +0000254 chip_writeb(0x80, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000255 programmer_delay(delay_us);
ollie0eb62d62004-12-08 20:10:01 +0000256
snelson63133f92010-01-04 17:15:23 +0000257 chip_writeb(0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000258 programmer_delay(delay_us);
snelson63133f92010-01-04 17:15:23 +0000259 chip_writeb(0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000260 programmer_delay(delay_us);
hailfinger1ff6e362009-03-06 22:26:00 +0000261 chip_writeb(0x30, bios + page);
mkarcherf7af1b42011-04-15 00:03:37 +0000262 programmer_delay(delay_us);
ollie5b621572004-03-20 16:46:10 +0000263
olliea3def632004-03-19 22:10:07 +0000264 /* wait for Toggle bit ready */
hailfinger10023012009-12-17 16:20:26 +0000265 toggle_ready_jedec_slow(bios);
olliea3def632004-03-19 22:10:07 +0000266
hailfinger7af83692009-06-15 17:23:36 +0000267 if (check_erased_range(flash, page, pagesize)) {
snelsonfc007bb2010-03-24 23:14:32 +0000268 msg_cerr("ERASE FAILED!\n");
hailfinger7af83692009-06-15 17:23:36 +0000269 return -1;
270 }
uwebe4477b2007-08-23 16:08:21 +0000271 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000272}
olliea4302802004-12-07 03:15:51 +0000273
hailfinger1ff33dc2010-07-03 11:02:10 +0000274static int erase_block_jedec_common(struct flashchip *flash, unsigned int block,
snelson63133f92010-01-04 17:15:23 +0000275 unsigned int blocksize, unsigned int mask)
rminnichdfcbaa72004-09-30 16:37:01 +0000276{
hailfinger7af83692009-06-15 17:23:36 +0000277 chipaddr bios = flash->virtual_memory;
mkarcherf7af1b42011-04-15 00:03:37 +0000278 int delay_us = 0;
279 if(flash->probe_timing != TIMING_ZERO)
280 delay_us = 10;
hailfinger7af83692009-06-15 17:23:36 +0000281
rminnichdfcbaa72004-09-30 16:37:01 +0000282 /* Issue the Sector Erase command */
snelson63133f92010-01-04 17:15:23 +0000283 chip_writeb(0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000284 programmer_delay(delay_us);
snelson63133f92010-01-04 17:15:23 +0000285 chip_writeb(0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000286 programmer_delay(delay_us);
snelson63133f92010-01-04 17:15:23 +0000287 chip_writeb(0x80, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000288 programmer_delay(delay_us);
ollie0eb62d62004-12-08 20:10:01 +0000289
snelson63133f92010-01-04 17:15:23 +0000290 chip_writeb(0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000291 programmer_delay(delay_us);
snelson63133f92010-01-04 17:15:23 +0000292 chip_writeb(0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000293 programmer_delay(delay_us);
hailfinger1ff6e362009-03-06 22:26:00 +0000294 chip_writeb(0x50, bios + block);
mkarcherf7af1b42011-04-15 00:03:37 +0000295 programmer_delay(delay_us);
rminnichdfcbaa72004-09-30 16:37:01 +0000296
297 /* wait for Toggle bit ready */
hailfinger10023012009-12-17 16:20:26 +0000298 toggle_ready_jedec_slow(bios);
rminnichdfcbaa72004-09-30 16:37:01 +0000299
hailfinger7af83692009-06-15 17:23:36 +0000300 if (check_erased_range(flash, block, blocksize)) {
snelsonfc007bb2010-03-24 23:14:32 +0000301 msg_cerr("ERASE FAILED!\n");
hailfinger7af83692009-06-15 17:23:36 +0000302 return -1;
303 }
uwebe4477b2007-08-23 16:08:21 +0000304 return 0;
rminnichdfcbaa72004-09-30 16:37:01 +0000305}
rminnich8d3ff912003-10-25 17:01:29 +0000306
hailfinger1ff33dc2010-07-03 11:02:10 +0000307static int erase_chip_jedec_common(struct flashchip *flash, unsigned int mask)
rminnich8d3ff912003-10-25 17:01:29 +0000308{
hailfinger7af83692009-06-15 17:23:36 +0000309 int total_size = flash->total_size * 1024;
hailfinger82719632009-05-16 21:22:56 +0000310 chipaddr bios = flash->virtual_memory;
mkarcherf7af1b42011-04-15 00:03:37 +0000311 int delay_us = 0;
312 if(flash->probe_timing != TIMING_ZERO)
313 delay_us = 10;
rminnich8d3ff912003-10-25 17:01:29 +0000314
ollie5b621572004-03-20 16:46:10 +0000315 /* Issue the JEDEC Chip Erase command */
snelson63133f92010-01-04 17:15:23 +0000316 chip_writeb(0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000317 programmer_delay(delay_us);
snelson63133f92010-01-04 17:15:23 +0000318 chip_writeb(0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000319 programmer_delay(delay_us);
snelson63133f92010-01-04 17:15:23 +0000320 chip_writeb(0x80, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000321 programmer_delay(delay_us);
ollie0eb62d62004-12-08 20:10:01 +0000322
snelson63133f92010-01-04 17:15:23 +0000323 chip_writeb(0xAA, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000324 programmer_delay(delay_us);
snelson63133f92010-01-04 17:15:23 +0000325 chip_writeb(0x55, bios + (0x2AAA & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000326 programmer_delay(delay_us);
snelson63133f92010-01-04 17:15:23 +0000327 chip_writeb(0x10, bios + (0x5555 & mask));
mkarcherf7af1b42011-04-15 00:03:37 +0000328 programmer_delay(delay_us);
olliea3def632004-03-19 22:10:07 +0000329
hailfinger10023012009-12-17 16:20:26 +0000330 toggle_ready_jedec_slow(bios);
rminnich8d3ff912003-10-25 17:01:29 +0000331
hailfinger7af83692009-06-15 17:23:36 +0000332 if (check_erased_range(flash, 0, total_size)) {
snelsonfc007bb2010-03-24 23:14:32 +0000333 msg_cerr("ERASE FAILED!\n");
hailfinger7af83692009-06-15 17:23:36 +0000334 return -1;
335 }
uwebe4477b2007-08-23 16:08:21 +0000336 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000337}
338
hailfinger1ff33dc2010-07-03 11:02:10 +0000339static int write_byte_program_jedec_common(struct flashchip *flash, uint8_t *src,
snelson63133f92010-01-04 17:15:23 +0000340 chipaddr dst, unsigned int mask)
341{
342 int tried = 0, failed = 0;
343 chipaddr bios = flash->virtual_memory;
344
345 /* If the data is 0xFF, don't program it and don't complain. */
346 if (*src == 0xFF) {
347 return 0;
348 }
349
350retry:
351 /* Issue JEDEC Byte Program command */
352 start_program_jedec_common(flash, mask);
353
354 /* transfer data from source to destination */
355 chip_writeb(*src, dst);
356 toggle_ready_jedec(bios);
357
358 if (chip_readb(dst) != *src && tried++ < MAX_REFLASH_TRIES) {
359 goto retry;
360 }
361
362 if (tried >= MAX_REFLASH_TRIES)
363 failed = 1;
364
365 return failed;
366}
367
hailfinger71e1bd42010-10-13 22:26:56 +0000368/* chunksize is 1 */
369int write_jedec_1(struct flashchip *flash, uint8_t *src, int start, int len)
snelson63133f92010-01-04 17:15:23 +0000370{
371 int i, failed = 0;
hailfingera10a6072010-10-10 14:02:27 +0000372 chipaddr dst = flash->virtual_memory + start;
snelson63133f92010-01-04 17:15:23 +0000373 chipaddr olddst;
hailfinger86bf3b52010-10-13 21:49:30 +0000374 int mask;
375
376 mask = getaddrmask(flash);
snelson63133f92010-01-04 17:15:23 +0000377
378 olddst = dst;
hailfingera10a6072010-10-10 14:02:27 +0000379 for (i = 0; i < len; i++) {
snelson63133f92010-01-04 17:15:23 +0000380 if (write_byte_program_jedec_common(flash, src, dst, mask))
381 failed = 1;
382 dst++, src++;
383 }
384 if (failed)
snelsonfc007bb2010-03-24 23:14:32 +0000385 msg_cerr(" writing sector at 0x%lx failed!\n", olddst);
snelson63133f92010-01-04 17:15:23 +0000386
387 return failed;
388}
389
hailfinger86bf3b52010-10-13 21:49:30 +0000390int write_page_write_jedec_common(struct flashchip *flash, uint8_t *src, int start, int page_size)
rminnich8d3ff912003-10-25 17:01:29 +0000391{
hailfingerf2ac27e2009-11-25 16:41:50 +0000392 int i, tried = 0, failed;
stepan7abc6322006-11-22 00:29:51 +0000393 uint8_t *s = src;
hailfingerc2cfc592009-06-25 13:57:31 +0000394 chipaddr bios = flash->virtual_memory;
395 chipaddr dst = bios + start;
396 chipaddr d = dst;
hailfinger86bf3b52010-10-13 21:49:30 +0000397 int mask;
398
399 mask = getaddrmask(flash);
rminnich8d3ff912003-10-25 17:01:29 +0000400
stepan7abc6322006-11-22 00:29:51 +0000401retry:
uwe3a3ab2f2010-03-25 23:18:41 +0000402 /* Issue JEDEC Start Program command */
snelson63133f92010-01-04 17:15:23 +0000403 start_program_jedec_common(flash, mask);
ollie5b621572004-03-20 16:46:10 +0000404
olliea4302802004-12-07 03:15:51 +0000405 /* transfer data from source to destination */
hailfingerfe072472009-11-14 03:48:33 +0000406 for (i = 0; i < page_size; i++) {
olliea4302802004-12-07 03:15:51 +0000407 /* If the data is 0xFF, don't program it */
uwef6641642007-05-09 10:17:44 +0000408 if (*src != 0xFF)
hailfinger1ff6e362009-03-06 22:26:00 +0000409 chip_writeb(*src, dst);
stepan7abc6322006-11-22 00:29:51 +0000410 dst++;
411 src++;
ollie5b621572004-03-20 16:46:10 +0000412 }
413
ollie5b621572004-03-20 16:46:10 +0000414 toggle_ready_jedec(dst - 1);
olliea4302802004-12-07 03:15:51 +0000415
stepan7abc6322006-11-22 00:29:51 +0000416 dst = d;
417 src = s;
hailfingerf2ac27e2009-11-25 16:41:50 +0000418 failed = verify_range(flash, src, start, page_size, NULL);
uwef6641642007-05-09 10:17:44 +0000419
hailfingerf2ac27e2009-11-25 16:41:50 +0000420 if (failed && tried++ < MAX_REFLASH_TRIES) {
snelsonfc007bb2010-03-24 23:14:32 +0000421 msg_cerr("retrying.\n");
uwef6641642007-05-09 10:17:44 +0000422 goto retry;
423 }
hailfingerf2ac27e2009-11-25 16:41:50 +0000424 if (failed) {
snelsonfc007bb2010-03-24 23:14:32 +0000425 msg_cerr(" page 0x%lx failed!\n",
hailfinger82719632009-05-16 21:22:56 +0000426 (d - bios) / page_size);
stepan7abc6322006-11-22 00:29:51 +0000427 }
hailfingerf2ac27e2009-11-25 16:41:50 +0000428 return failed;
ollie5b621572004-03-20 16:46:10 +0000429}
430
hailfinger71e1bd42010-10-13 22:26:56 +0000431/* chunksize is page_size */
hailfinger86bf3b52010-10-13 21:49:30 +0000432/*
433 * Write a part of the flash chip.
434 * FIXME: Use the chunk code from Michael Karcher instead.
435 * This function is a slightly modified copy of spi_write_chunked.
436 * Each page is written separately in chunks with a maximum size of chunksize.
437 */
hailfinger71e1bd42010-10-13 22:26:56 +0000438int write_jedec(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfinger80dea312010-01-09 03:15:50 +0000439{
hailfinger86bf3b52010-10-13 21:49:30 +0000440 int i, starthere, lenhere;
441 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
442 * in struct flashchip to do this properly. All chips using
443 * write_jedec have page_size set to max_writechunk_size, so
444 * we're OK for now.
445 */
olliebb5917a2004-03-22 22:19:17 +0000446 int page_size = flash->page_size;
ollie5b621572004-03-20 16:46:10 +0000447
hailfinger86bf3b52010-10-13 21:49:30 +0000448 /* Warning: This loop has a very unusual condition and body.
449 * The loop needs to go through each page with at least one affected
450 * byte. The lowest page number is (start / page_size) since that
451 * division rounds down. The highest page number we want is the page
452 * where the last byte of the range lives. That last byte has the
453 * address (start + len - 1), thus the highest page number is
454 * (start + len - 1) / page_size. Since we want to include that last
455 * page as well, the loop condition uses <=.
456 */
457 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
458 /* Byte position of the first byte in the range in this page. */
459 /* starthere is an offset to the base address of the chip. */
460 starthere = max(start, i * page_size);
461 /* Length of bytes in the range in this page. */
462 lenhere = min(start + len, (i + 1) * page_size) - starthere;
snelsonc6855342010-01-28 23:55:12 +0000463
hailfinger86bf3b52010-10-13 21:49:30 +0000464 if (write_page_write_jedec_common(flash, buf + starthere - start, starthere, lenhere))
465 return 1;
rminnich8d3ff912003-10-25 17:01:29 +0000466 }
rminnich8d3ff912003-10-25 17:01:29 +0000467
hailfinger86bf3b52010-10-13 21:49:30 +0000468 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000469}
hailfingerfff99532009-11-27 17:49:42 +0000470
snelson63133f92010-01-04 17:15:23 +0000471/* erase chip with block_erase() prototype */
472int erase_chip_block_jedec(struct flashchip *flash, unsigned int addr,
473 unsigned int blocksize)
474{
snelsonc6855342010-01-28 23:55:12 +0000475 int mask;
476
477 mask = getaddrmask(flash);
snelson63133f92010-01-04 17:15:23 +0000478 if ((addr != 0) || (blocksize != flash->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000479 msg_cerr("%s called with incorrect arguments\n",
snelson63133f92010-01-04 17:15:23 +0000480 __func__);
481 return -1;
482 }
snelsonc6855342010-01-28 23:55:12 +0000483 return erase_chip_jedec_common(flash, mask);
snelson63133f92010-01-04 17:15:23 +0000484}
485
486int probe_jedec(struct flashchip *flash)
487{
hailfinger80dea312010-01-09 03:15:50 +0000488 int mask;
489
490 mask = getaddrmask(flash);
snelsonc6855342010-01-28 23:55:12 +0000491 return probe_jedec_common(flash, mask);
snelson63133f92010-01-04 17:15:23 +0000492}
493
494int erase_sector_jedec(struct flashchip *flash, unsigned int page, unsigned int size)
495{
snelsonc6855342010-01-28 23:55:12 +0000496 int mask;
497
498 mask = getaddrmask(flash);
499 return erase_sector_jedec_common(flash, page, size, mask);
snelson63133f92010-01-04 17:15:23 +0000500}
501
502int erase_block_jedec(struct flashchip *flash, unsigned int page, unsigned int size)
503{
snelsonc6855342010-01-28 23:55:12 +0000504 int mask;
505
506 mask = getaddrmask(flash);
507 return erase_block_jedec_common(flash, page, size, mask);
snelson63133f92010-01-04 17:15:23 +0000508}
509
510int erase_chip_jedec(struct flashchip *flash)
511{
snelsonc6855342010-01-28 23:55:12 +0000512 int mask;
513
514 mask = getaddrmask(flash);
515 return erase_chip_jedec_common(flash, mask);
snelson63133f92010-01-04 17:15:23 +0000516}