Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 1 | /* |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 2 | * This file is part of the flashrom project. |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 3 | * |
Uwe Hermann | d22a1d4 | 2007-09-09 20:21:05 +0000 | [diff] [blame] | 4 | * 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 Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 7 | * Copyright (C) 2007-2012 Carl-Daniel Hailfinger |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 8 | * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com> |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 9 | * Copyright (C) 2014 Stefan Tauner |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 10 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 11 | * 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. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 15 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 16 | * 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. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 20 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include "flash.h" |
Stefan Tauner | 0ab1e5d | 2014-05-29 11:51:24 +0000 | [diff] [blame] | 27 | #include "chipdrivers.h" |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 28 | |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 29 | #define MAX_REFLASH_TRIES 0x10 |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 30 | #define MASK_FULL 0xffff |
| 31 | #define MASK_2AA 0x7ff |
Sean Nelson | 35727f7 | 2010-01-28 23:55:12 +0000 | [diff] [blame] | 32 | #define MASK_AAA 0xfff |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 33 | |
Carl-Daniel Hailfinger | a758f51 | 2008-05-14 12:03:06 +0000 | [diff] [blame] | 34 | /* Check one byte for odd parity */ |
| 35 | uint8_t oddparity(uint8_t val) |
| 36 | { |
| 37 | val = (val ^ (val >> 4)) & 0xf; |
| 38 | val = (val ^ (val >> 2)) & 0x3; |
| 39 | return (val ^ (val >> 1)) & 0x1; |
| 40 | } |
| 41 | |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 42 | static void toggle_ready_jedec_common(const struct flashctx *flash, chipaddr dst, unsigned int delay) |
Uwe Hermann | 51582f2 | 2007-08-23 10:20:40 +0000 | [diff] [blame] | 43 | { |
| 44 | unsigned int i = 0; |
| 45 | uint8_t tmp1, tmp2; |
| 46 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 47 | tmp1 = chip_readb(flash, dst) & 0x40; |
Uwe Hermann | 51582f2 | 2007-08-23 10:20:40 +0000 | [diff] [blame] | 48 | |
| 49 | while (i++ < 0xFFFFFFF) { |
Carl-Daniel Hailfinger | aa00098 | 2009-12-17 16:20:26 +0000 | [diff] [blame] | 50 | if (delay) |
| 51 | programmer_delay(delay); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 52 | tmp2 = chip_readb(flash, dst) & 0x40; |
Uwe Hermann | 51582f2 | 2007-08-23 10:20:40 +0000 | [diff] [blame] | 53 | if (tmp1 == tmp2) { |
| 54 | break; |
| 55 | } |
| 56 | tmp1 = tmp2; |
| 57 | } |
Carl-Daniel Hailfinger | aa00098 | 2009-12-17 16:20:26 +0000 | [diff] [blame] | 58 | if (i > 0x100000) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 59 | msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i); |
Carl-Daniel Hailfinger | aa00098 | 2009-12-17 16:20:26 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 62 | void toggle_ready_jedec(const struct flashctx *flash, chipaddr dst) |
Carl-Daniel Hailfinger | aa00098 | 2009-12-17 16:20:26 +0000 | [diff] [blame] | 63 | { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 64 | toggle_ready_jedec_common(flash, dst, 0); |
Carl-Daniel Hailfinger | aa00098 | 2009-12-17 16:20:26 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* Some chips require a minimum delay between toggle bit reads. |
| 68 | * The Winbond W39V040C wants 50 ms between reads on sector erase toggle, |
| 69 | * but experiments show that 2 ms are already enough. Pick a safety factor |
| 70 | * of 4 and use an 8 ms delay. |
| 71 | * Given that erase is slow on all chips, it is recommended to use |
| 72 | * toggle_ready_jedec_slow in erase functions. |
| 73 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 74 | static void toggle_ready_jedec_slow(const struct flashctx *flash, chipaddr dst) |
Carl-Daniel Hailfinger | aa00098 | 2009-12-17 16:20:26 +0000 | [diff] [blame] | 75 | { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 76 | toggle_ready_jedec_common(flash, dst, 8 * 1000); |
Uwe Hermann | 51582f2 | 2007-08-23 10:20:40 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 79 | void data_polling_jedec(const struct flashctx *flash, chipaddr dst, |
| 80 | uint8_t data) |
Uwe Hermann | 51582f2 | 2007-08-23 10:20:40 +0000 | [diff] [blame] | 81 | { |
| 82 | unsigned int i = 0; |
| 83 | uint8_t tmp; |
| 84 | |
| 85 | data &= 0x80; |
| 86 | |
| 87 | while (i++ < 0xFFFFFFF) { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 88 | tmp = chip_readb(flash, dst) & 0x80; |
Uwe Hermann | 51582f2 | 2007-08-23 10:20:40 +0000 | [diff] [blame] | 89 | if (tmp == data) { |
| 90 | break; |
| 91 | } |
| 92 | } |
Carl-Daniel Hailfinger | aa00098 | 2009-12-17 16:20:26 +0000 | [diff] [blame] | 93 | if (i > 0x100000) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 94 | msg_cdbg("%s: excessive loops, i=0x%x\n", __func__, i); |
Uwe Hermann | 51582f2 | 2007-08-23 10:20:40 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 97 | static unsigned int getaddrmask(const struct flashchip *chip) |
Carl-Daniel Hailfinger | 79e6757 | 2010-10-13 21:49:30 +0000 | [diff] [blame] | 98 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 99 | switch (chip->feature_bits & FEATURE_ADDR_MASK) { |
Carl-Daniel Hailfinger | 79e6757 | 2010-10-13 21:49:30 +0000 | [diff] [blame] | 100 | case FEATURE_ADDR_FULL: |
| 101 | return MASK_FULL; |
| 102 | break; |
| 103 | case FEATURE_ADDR_2AA: |
| 104 | return MASK_2AA; |
| 105 | break; |
| 106 | case FEATURE_ADDR_AAA: |
| 107 | return MASK_AAA; |
| 108 | break; |
| 109 | default: |
| 110 | msg_cerr("%s called with unknown mask\n", __func__); |
| 111 | return 0; |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
Stefan Tauner | 0ab1e5d | 2014-05-29 11:51:24 +0000 | [diff] [blame] | 116 | static void start_program_jedec_common(const struct flashctx *flash, unsigned int mask) |
Uwe Hermann | 51582f2 | 2007-08-23 10:20:40 +0000 | [diff] [blame] | 117 | { |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 118 | chipaddr bios = flash->virtual_memory; |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 119 | bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED); |
| 120 | |
| 121 | chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
| 122 | chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask)); |
| 123 | chip_writeb(flash, 0xA0, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Uwe Hermann | 51582f2 | 2007-08-23 10:20:40 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Stefan Tauner | 03a9c3c | 2014-08-03 14:15:14 +0000 | [diff] [blame] | 126 | int probe_jedec_29gl(struct flashctx *flash) |
| 127 | { |
| 128 | unsigned int mask = getaddrmask(flash->chip); |
| 129 | chipaddr bios = flash->virtual_memory; |
| 130 | const struct flashchip *chip = flash->chip; |
| 131 | |
| 132 | /* Reset chip to a clean slate */ |
| 133 | chip_writeb(flash, 0xF0, bios + (0x5555 & mask)); |
| 134 | |
| 135 | /* Issue JEDEC Product ID Entry command */ |
| 136 | chip_writeb(flash, 0xAA, bios + (0x5555 & mask)); |
| 137 | chip_writeb(flash, 0x55, bios + (0x2AAA & mask)); |
| 138 | chip_writeb(flash, 0x90, bios + (0x5555 & mask)); |
| 139 | |
| 140 | /* Read product ID */ |
| 141 | // FIXME: Continuation loop, second byte is at word 0x100/byte 0x200 |
| 142 | uint32_t man_id = chip_readb(flash, bios + 0x00); |
| 143 | uint32_t dev_id = (chip_readb(flash, bios + 0x01) << 16) | |
| 144 | (chip_readb(flash, bios + 0x0E) << 8) | |
| 145 | (chip_readb(flash, bios + 0x0F) << 0); |
| 146 | |
| 147 | /* Issue JEDEC Product ID Exit command */ |
| 148 | chip_writeb(flash, 0xF0, bios + (0x5555 & mask)); |
| 149 | |
| 150 | msg_cdbg("%s: man_id 0x%02x, dev_id 0x%06x", __func__, man_id, dev_id); |
| 151 | if (!oddparity(man_id)) |
| 152 | msg_cdbg(", man_id parity violation"); |
| 153 | |
| 154 | /* Read the product ID location again. We should now see normal flash contents. */ |
| 155 | uint32_t flashcontent1 = chip_readb(flash, bios + 0x00); // FIXME: Continuation loop |
| 156 | uint32_t flashcontent2 = (chip_readb(flash, bios + 0x01) << 16) | |
| 157 | (chip_readb(flash, bios + 0x0E) << 8) | |
| 158 | (chip_readb(flash, bios + 0x0F) << 0); |
| 159 | |
| 160 | if (man_id == flashcontent1) |
| 161 | msg_cdbg(", man_id seems to be normal flash content"); |
| 162 | if (dev_id == flashcontent2) |
| 163 | msg_cdbg(", dev_id seems to be normal flash content"); |
| 164 | |
| 165 | msg_cdbg("\n"); |
| 166 | if (man_id != chip->manufacture_id || dev_id != chip->model_id) |
| 167 | return 0; |
| 168 | |
Stefan Tauner | 03a9c3c | 2014-08-03 14:15:14 +0000 | [diff] [blame] | 169 | return 1; |
| 170 | } |
| 171 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 172 | static int probe_jedec_common(struct flashctx *flash, unsigned int mask) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 173 | { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 174 | chipaddr bios = flash->virtual_memory; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 175 | const struct flashchip *chip = flash->chip; |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 176 | bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 177 | uint8_t id1, id2; |
Carl-Daniel Hailfinger | ae8afa9 | 2007-12-31 01:49:00 +0000 | [diff] [blame] | 178 | uint32_t largeid1, largeid2; |
Carl-Daniel Hailfinger | 8130f2d | 2009-05-11 14:40:31 +0000 | [diff] [blame] | 179 | uint32_t flashcontent1, flashcontent2; |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 180 | unsigned int probe_timing_enter, probe_timing_exit; |
Maciej Pijanka | c6e1111 | 2009-06-03 14:46:22 +0000 | [diff] [blame] | 181 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 182 | if (chip->probe_timing > 0) |
| 183 | probe_timing_enter = probe_timing_exit = chip->probe_timing; |
| 184 | else if (chip->probe_timing == TIMING_ZERO) { /* No delay. */ |
Maciej Pijanka | c6e1111 | 2009-06-03 14:46:22 +0000 | [diff] [blame] | 185 | probe_timing_enter = probe_timing_exit = 0; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 186 | } else if (chip->probe_timing == TIMING_FIXME) { /* == _IGNORED */ |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 187 | msg_cdbg("Chip lacks correct probe timing information, " |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 188 | "using default 10mS/40uS. "); |
Maciej Pijanka | c6e1111 | 2009-06-03 14:46:22 +0000 | [diff] [blame] | 189 | probe_timing_enter = 10000; |
| 190 | probe_timing_exit = 40; |
| 191 | } else { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 192 | msg_cerr("Chip has negative value in probe_timing, failing " |
Maciej Pijanka | c6e1111 | 2009-06-03 14:46:22 +0000 | [diff] [blame] | 193 | "without chip access\n"); |
| 194 | return 0; |
| 195 | } |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 196 | |
Sean Nelson | f59e263 | 2010-10-20 21:13:19 +0000 | [diff] [blame] | 197 | /* Earlier probes might have been too fast for the chip to enter ID |
| 198 | * mode completely. Allow the chip to finish this before seeing a |
| 199 | * reset command. |
| 200 | */ |
| 201 | if (probe_timing_enter) |
| 202 | programmer_delay(probe_timing_enter); |
| 203 | /* Reset chip to a clean slate */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 204 | if ((chip->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET) |
Sean Nelson | f59e263 | 2010-10-20 21:13:19 +0000 | [diff] [blame] | 205 | { |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 206 | chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Sean Nelson | f59e263 | 2010-10-20 21:13:19 +0000 | [diff] [blame] | 207 | if (probe_timing_exit) |
| 208 | programmer_delay(10); |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 209 | chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask)); |
Sean Nelson | f59e263 | 2010-10-20 21:13:19 +0000 | [diff] [blame] | 210 | if (probe_timing_exit) |
| 211 | programmer_delay(10); |
| 212 | } |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 213 | chip_writeb(flash, 0xF0, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Sean Nelson | f59e263 | 2010-10-20 21:13:19 +0000 | [diff] [blame] | 214 | if (probe_timing_exit) |
| 215 | programmer_delay(probe_timing_exit); |
| 216 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 217 | /* Issue JEDEC Product ID Entry command */ |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 218 | chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Sean Nelson | c12fc71 | 2009-12-17 04:22:40 +0000 | [diff] [blame] | 219 | if (probe_timing_enter) |
| 220 | programmer_delay(10); |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 221 | chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask)); |
Sean Nelson | c12fc71 | 2009-12-17 04:22:40 +0000 | [diff] [blame] | 222 | if (probe_timing_enter) |
| 223 | programmer_delay(10); |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 224 | chip_writeb(flash, 0x90, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Sean Nelson | c12fc71 | 2009-12-17 04:22:40 +0000 | [diff] [blame] | 225 | if (probe_timing_enter) |
| 226 | programmer_delay(probe_timing_enter); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 227 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 228 | /* Read product ID */ |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 229 | id1 = chip_readb(flash, bios + (0x00 << shifted)); |
| 230 | id2 = chip_readb(flash, bios + (0x01 << shifted)); |
Carl-Daniel Hailfinger | ae8afa9 | 2007-12-31 01:49:00 +0000 | [diff] [blame] | 231 | largeid1 = id1; |
| 232 | largeid2 = id2; |
| 233 | |
| 234 | /* Check if it is a continuation ID, this should be a while loop. */ |
| 235 | if (id1 == 0x7F) { |
| 236 | largeid1 <<= 8; |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 237 | id1 = chip_readb(flash, bios + 0x100); |
Carl-Daniel Hailfinger | ae8afa9 | 2007-12-31 01:49:00 +0000 | [diff] [blame] | 238 | largeid1 |= id1; |
| 239 | } |
| 240 | if (id2 == 0x7F) { |
| 241 | largeid2 <<= 8; |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 242 | id2 = chip_readb(flash, bios + 0x101); |
Carl-Daniel Hailfinger | ae8afa9 | 2007-12-31 01:49:00 +0000 | [diff] [blame] | 243 | largeid2 |= id2; |
| 244 | } |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 245 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 246 | /* Issue JEDEC Product ID Exit command */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 247 | if ((chip->feature_bits & FEATURE_RESET_MASK) == FEATURE_LONG_RESET) |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 248 | { |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 249 | chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 250 | if (probe_timing_exit) |
| 251 | programmer_delay(10); |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 252 | chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask)); |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 253 | if (probe_timing_exit) |
| 254 | programmer_delay(10); |
| 255 | } |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 256 | chip_writeb(flash, 0xF0, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Sean Nelson | c12fc71 | 2009-12-17 04:22:40 +0000 | [diff] [blame] | 257 | if (probe_timing_exit) |
| 258 | programmer_delay(probe_timing_exit); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 259 | |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 260 | msg_cdbg("%s: id1 0x%02x, id2 0x%02x", __func__, largeid1, largeid2); |
Carl-Daniel Hailfinger | a758f51 | 2008-05-14 12:03:06 +0000 | [diff] [blame] | 261 | if (!oddparity(id1)) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 262 | msg_cdbg(", id1 parity violation"); |
Carl-Daniel Hailfinger | 8130f2d | 2009-05-11 14:40:31 +0000 | [diff] [blame] | 263 | |
| 264 | /* Read the product ID location again. We should now see normal flash contents. */ |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 265 | flashcontent1 = chip_readb(flash, bios + (0x00 << shifted)); |
| 266 | flashcontent2 = chip_readb(flash, bios + (0x01 << shifted)); |
Carl-Daniel Hailfinger | 8130f2d | 2009-05-11 14:40:31 +0000 | [diff] [blame] | 267 | |
| 268 | /* Check if it is a continuation ID, this should be a while loop. */ |
| 269 | if (flashcontent1 == 0x7F) { |
| 270 | flashcontent1 <<= 8; |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 271 | flashcontent1 |= chip_readb(flash, bios + 0x100); |
Carl-Daniel Hailfinger | 8130f2d | 2009-05-11 14:40:31 +0000 | [diff] [blame] | 272 | } |
| 273 | if (flashcontent2 == 0x7F) { |
| 274 | flashcontent2 <<= 8; |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 275 | flashcontent2 |= chip_readb(flash, bios + 0x101); |
Carl-Daniel Hailfinger | 8130f2d | 2009-05-11 14:40:31 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | if (largeid1 == flashcontent1) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 279 | msg_cdbg(", id1 is normal flash content"); |
Carl-Daniel Hailfinger | 8130f2d | 2009-05-11 14:40:31 +0000 | [diff] [blame] | 280 | if (largeid2 == flashcontent2) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 281 | msg_cdbg(", id2 is normal flash content"); |
Carl-Daniel Hailfinger | 8130f2d | 2009-05-11 14:40:31 +0000 | [diff] [blame] | 282 | |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 283 | msg_cdbg("\n"); |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 284 | if (largeid1 != chip->manufacture_id || largeid2 != chip->model_id) |
Carl-Daniel Hailfinger | e940466 | 2010-01-09 02:24:17 +0000 | [diff] [blame] | 285 | return 0; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 286 | |
Carl-Daniel Hailfinger | e940466 | 2010-01-09 02:24:17 +0000 | [diff] [blame] | 287 | return 1; |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 290 | static int erase_sector_jedec_common(struct flashctx *flash, unsigned int page, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 291 | unsigned int pagesize, unsigned int mask) |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 292 | { |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 293 | chipaddr bios = flash->virtual_memory; |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 294 | bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED); |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 295 | unsigned int delay_us = 0; |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 296 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 297 | if(flash->chip->probe_timing != TIMING_ZERO) |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 298 | delay_us = 10; |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 299 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 300 | /* Issue the Sector Erase command */ |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 301 | chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 302 | programmer_delay(delay_us); |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 303 | chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 304 | programmer_delay(delay_us); |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 305 | chip_writeb(flash, 0x80, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 306 | programmer_delay(delay_us); |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 307 | |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 308 | chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 309 | programmer_delay(delay_us); |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 310 | chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 311 | programmer_delay(delay_us); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 312 | chip_writeb(flash, 0x30, bios + page); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 313 | programmer_delay(delay_us); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 314 | |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 315 | /* wait for Toggle bit ready */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 316 | toggle_ready_jedec_slow(flash, bios); |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 317 | |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 318 | /* FIXME: Check the status register for errors. */ |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 319 | return 0; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 320 | } |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 321 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 322 | static int erase_block_jedec_common(struct flashctx *flash, unsigned int block, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 323 | unsigned int blocksize, unsigned int mask) |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 324 | { |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 325 | chipaddr bios = flash->virtual_memory; |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 326 | bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED); |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 327 | unsigned int delay_us = 0; |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 328 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 329 | if(flash->chip->probe_timing != TIMING_ZERO) |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 330 | delay_us = 10; |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 331 | |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 332 | /* Issue the Sector Erase command */ |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 333 | chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 334 | programmer_delay(delay_us); |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 335 | chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 336 | programmer_delay(delay_us); |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 337 | chip_writeb(flash, 0x80, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 338 | programmer_delay(delay_us); |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 339 | |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 340 | chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 341 | programmer_delay(delay_us); |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 342 | chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 343 | programmer_delay(delay_us); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 344 | chip_writeb(flash, 0x50, bios + block); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 345 | programmer_delay(delay_us); |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 346 | |
| 347 | /* wait for Toggle bit ready */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 348 | toggle_ready_jedec_slow(flash, bios); |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 349 | |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 350 | /* FIXME: Check the status register for errors. */ |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 351 | return 0; |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 352 | } |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 353 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 354 | static int erase_chip_jedec_common(struct flashctx *flash, unsigned int mask) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 355 | { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 356 | chipaddr bios = flash->virtual_memory; |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 357 | bool shifted = (flash->chip->feature_bits & FEATURE_ADDR_SHIFTED); |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 358 | unsigned int delay_us = 0; |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 359 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 360 | if(flash->chip->probe_timing != TIMING_ZERO) |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 361 | delay_us = 10; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 362 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 363 | /* Issue the JEDEC Chip Erase command */ |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 364 | chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 365 | programmer_delay(delay_us); |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 366 | chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 367 | programmer_delay(delay_us); |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 368 | chip_writeb(flash, 0x80, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 369 | programmer_delay(delay_us); |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 370 | |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 371 | chip_writeb(flash, 0xAA, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 372 | programmer_delay(delay_us); |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 373 | chip_writeb(flash, 0x55, bios + ((shifted ? 0x5555 : 0x2AAA) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 374 | programmer_delay(delay_us); |
Carl-Daniel Hailfinger | a8cf362 | 2014-08-08 08:33:01 +0000 | [diff] [blame] | 375 | chip_writeb(flash, 0x10, bios + ((shifted ? 0x2AAA : 0x5555) & mask)); |
Michael Karcher | 880e867 | 2011-04-15 00:03:37 +0000 | [diff] [blame] | 376 | programmer_delay(delay_us); |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 377 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 378 | toggle_ready_jedec_slow(flash, bios); |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 379 | |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 380 | /* FIXME: Check the status register for errors. */ |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 381 | return 0; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Stefan Tauner | 0ab1e5d | 2014-05-29 11:51:24 +0000 | [diff] [blame] | 384 | static int write_byte_program_jedec_common(const struct flashctx *flash, const uint8_t *src, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 385 | chipaddr dst, unsigned int mask) |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 386 | { |
| 387 | int tried = 0, failed = 0; |
| 388 | chipaddr bios = flash->virtual_memory; |
| 389 | |
| 390 | /* If the data is 0xFF, don't program it and don't complain. */ |
| 391 | if (*src == 0xFF) { |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | retry: |
| 396 | /* Issue JEDEC Byte Program command */ |
| 397 | start_program_jedec_common(flash, mask); |
| 398 | |
| 399 | /* transfer data from source to destination */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 400 | chip_writeb(flash, *src, dst); |
| 401 | toggle_ready_jedec(flash, bios); |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 402 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 403 | if (chip_readb(flash, dst) != *src && tried++ < MAX_REFLASH_TRIES) { |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 404 | goto retry; |
| 405 | } |
| 406 | |
| 407 | if (tried >= MAX_REFLASH_TRIES) |
| 408 | failed = 1; |
| 409 | |
| 410 | return failed; |
| 411 | } |
| 412 | |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 413 | /* chunksize is 1 */ |
Stefan Tauner | 0ab1e5d | 2014-05-29 11:51:24 +0000 | [diff] [blame] | 414 | int write_jedec_1(struct flashctx *flash, const uint8_t *src, unsigned int start, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 415 | unsigned int len) |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 416 | { |
| 417 | int i, failed = 0; |
Carl-Daniel Hailfinger | b30a5ed | 2010-10-10 14:02:27 +0000 | [diff] [blame] | 418 | chipaddr dst = flash->virtual_memory + start; |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 419 | chipaddr olddst; |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 420 | unsigned int mask; |
Carl-Daniel Hailfinger | 79e6757 | 2010-10-13 21:49:30 +0000 | [diff] [blame] | 421 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 422 | mask = getaddrmask(flash->chip); |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 423 | |
| 424 | olddst = dst; |
Carl-Daniel Hailfinger | b30a5ed | 2010-10-10 14:02:27 +0000 | [diff] [blame] | 425 | for (i = 0; i < len; i++) { |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 426 | if (write_byte_program_jedec_common(flash, src, dst, mask)) |
| 427 | failed = 1; |
| 428 | dst++, src++; |
| 429 | } |
| 430 | if (failed) |
Stefan Tauner | c233375 | 2013-07-13 23:31:37 +0000 | [diff] [blame] | 431 | msg_cerr(" writing sector at 0x%" PRIxPTR " failed!\n", olddst); |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 432 | |
| 433 | return failed; |
| 434 | } |
| 435 | |
Stefan Tauner | 0ab1e5d | 2014-05-29 11:51:24 +0000 | [diff] [blame] | 436 | static int write_page_write_jedec_common(struct flashctx *flash, const uint8_t *src, |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 437 | unsigned int start, unsigned int page_size) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 438 | { |
Carl-Daniel Hailfinger | 2925d6f | 2009-11-25 16:41:50 +0000 | [diff] [blame] | 439 | int i, tried = 0, failed; |
Stefan Tauner | 0ab1e5d | 2014-05-29 11:51:24 +0000 | [diff] [blame] | 440 | const uint8_t *s = src; |
Urja Rannikko | 0c854c0 | 2009-06-25 13:57:31 +0000 | [diff] [blame] | 441 | chipaddr bios = flash->virtual_memory; |
| 442 | chipaddr dst = bios + start; |
| 443 | chipaddr d = dst; |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 444 | unsigned int mask; |
Carl-Daniel Hailfinger | 79e6757 | 2010-10-13 21:49:30 +0000 | [diff] [blame] | 445 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 446 | mask = getaddrmask(flash->chip); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 447 | |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 448 | retry: |
Uwe Hermann | 4e3d0b3 | 2010-03-25 23:18:41 +0000 | [diff] [blame] | 449 | /* Issue JEDEC Start Program command */ |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 450 | start_program_jedec_common(flash, mask); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 451 | |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 452 | /* transfer data from source to destination */ |
Carl-Daniel Hailfinger | 8a8a226 | 2009-11-14 03:48:33 +0000 | [diff] [blame] | 453 | for (i = 0; i < page_size; i++) { |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 454 | /* If the data is 0xFF, don't program it */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 455 | if (*src != 0xFF) |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 456 | chip_writeb(flash, *src, dst); |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 457 | dst++; |
| 458 | src++; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 461 | toggle_ready_jedec(flash, dst - 1); |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 462 | |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 463 | dst = d; |
| 464 | src = s; |
Stefan Tauner | 78ffbea | 2012-10-27 15:36:56 +0000 | [diff] [blame] | 465 | failed = verify_range(flash, src, start, page_size); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 466 | |
Carl-Daniel Hailfinger | 2925d6f | 2009-11-25 16:41:50 +0000 | [diff] [blame] | 467 | if (failed && tried++ < MAX_REFLASH_TRIES) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 468 | msg_cerr("retrying.\n"); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 469 | goto retry; |
| 470 | } |
Carl-Daniel Hailfinger | 2925d6f | 2009-11-25 16:41:50 +0000 | [diff] [blame] | 471 | if (failed) { |
Stefan Tauner | c233375 | 2013-07-13 23:31:37 +0000 | [diff] [blame] | 472 | msg_cerr(" page 0x%" PRIxPTR " failed!\n", (d - bios) / page_size); |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 473 | } |
Carl-Daniel Hailfinger | 2925d6f | 2009-11-25 16:41:50 +0000 | [diff] [blame] | 474 | return failed; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 477 | /* chunksize is page_size */ |
Carl-Daniel Hailfinger | 79e6757 | 2010-10-13 21:49:30 +0000 | [diff] [blame] | 478 | /* |
| 479 | * Write a part of the flash chip. |
| 480 | * FIXME: Use the chunk code from Michael Karcher instead. |
| 481 | * This function is a slightly modified copy of spi_write_chunked. |
| 482 | * Each page is written separately in chunks with a maximum size of chunksize. |
| 483 | */ |
Stefan Tauner | 0ab1e5d | 2014-05-29 11:51:24 +0000 | [diff] [blame] | 484 | int write_jedec(struct flashctx *flash, const uint8_t *buf, unsigned int start, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 485 | int unsigned len) |
Carl-Daniel Hailfinger | 4bf4e79 | 2010-01-09 03:15:50 +0000 | [diff] [blame] | 486 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 487 | unsigned int i, starthere, lenhere; |
Carl-Daniel Hailfinger | 79e6757 | 2010-10-13 21:49:30 +0000 | [diff] [blame] | 488 | /* FIXME: page_size is the wrong variable. We need max_writechunk_size |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 489 | * in struct flashctx to do this properly. All chips using |
Carl-Daniel Hailfinger | 79e6757 | 2010-10-13 21:49:30 +0000 | [diff] [blame] | 490 | * write_jedec have page_size set to max_writechunk_size, so |
| 491 | * we're OK for now. |
| 492 | */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 493 | unsigned int page_size = flash->chip->page_size; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 494 | |
Carl-Daniel Hailfinger | 79e6757 | 2010-10-13 21:49:30 +0000 | [diff] [blame] | 495 | /* Warning: This loop has a very unusual condition and body. |
| 496 | * The loop needs to go through each page with at least one affected |
| 497 | * byte. The lowest page number is (start / page_size) since that |
| 498 | * division rounds down. The highest page number we want is the page |
| 499 | * where the last byte of the range lives. That last byte has the |
| 500 | * address (start + len - 1), thus the highest page number is |
| 501 | * (start + len - 1) / page_size. Since we want to include that last |
| 502 | * page as well, the loop condition uses <=. |
| 503 | */ |
| 504 | for (i = start / page_size; i <= (start + len - 1) / page_size; i++) { |
| 505 | /* Byte position of the first byte in the range in this page. */ |
| 506 | /* starthere is an offset to the base address of the chip. */ |
| 507 | starthere = max(start, i * page_size); |
| 508 | /* Length of bytes in the range in this page. */ |
| 509 | lenhere = min(start + len, (i + 1) * page_size) - starthere; |
Sean Nelson | 35727f7 | 2010-01-28 23:55:12 +0000 | [diff] [blame] | 510 | |
Carl-Daniel Hailfinger | 79e6757 | 2010-10-13 21:49:30 +0000 | [diff] [blame] | 511 | if (write_page_write_jedec_common(flash, buf + starthere - start, starthere, lenhere)) |
| 512 | return 1; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 513 | } |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 514 | |
Carl-Daniel Hailfinger | 79e6757 | 2010-10-13 21:49:30 +0000 | [diff] [blame] | 515 | return 0; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 516 | } |
Michael Karcher | 1c296ca | 2009-11-27 17:49:42 +0000 | [diff] [blame] | 517 | |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 518 | /* erase chip with block_erase() prototype */ |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 519 | int erase_chip_block_jedec(struct flashctx *flash, unsigned int addr, |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 520 | unsigned int blocksize) |
| 521 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 522 | unsigned int mask; |
Sean Nelson | 35727f7 | 2010-01-28 23:55:12 +0000 | [diff] [blame] | 523 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 524 | mask = getaddrmask(flash->chip); |
| 525 | if ((addr != 0) || (blocksize != flash->chip->total_size * 1024)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 526 | msg_cerr("%s called with incorrect arguments\n", |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 527 | __func__); |
| 528 | return -1; |
| 529 | } |
Sean Nelson | 35727f7 | 2010-01-28 23:55:12 +0000 | [diff] [blame] | 530 | return erase_chip_jedec_common(flash, mask); |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 533 | int probe_jedec(struct flashctx *flash) |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 534 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 535 | unsigned int mask; |
Carl-Daniel Hailfinger | 4bf4e79 | 2010-01-09 03:15:50 +0000 | [diff] [blame] | 536 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 537 | mask = getaddrmask(flash->chip); |
Sean Nelson | 35727f7 | 2010-01-28 23:55:12 +0000 | [diff] [blame] | 538 | return probe_jedec_common(flash, mask); |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 541 | int erase_sector_jedec(struct flashctx *flash, unsigned int page, |
| 542 | unsigned int size) |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 543 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 544 | unsigned int mask; |
Sean Nelson | 35727f7 | 2010-01-28 23:55:12 +0000 | [diff] [blame] | 545 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 546 | mask = getaddrmask(flash->chip); |
Sean Nelson | 35727f7 | 2010-01-28 23:55:12 +0000 | [diff] [blame] | 547 | return erase_sector_jedec_common(flash, page, size, mask); |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 550 | int erase_block_jedec(struct flashctx *flash, unsigned int page, |
| 551 | unsigned int size) |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 552 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 553 | unsigned int mask; |
Sean Nelson | 35727f7 | 2010-01-28 23:55:12 +0000 | [diff] [blame] | 554 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 555 | mask = getaddrmask(flash->chip); |
Sean Nelson | 35727f7 | 2010-01-28 23:55:12 +0000 | [diff] [blame] | 556 | return erase_block_jedec_common(flash, page, size, mask); |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 559 | int erase_chip_jedec(struct flashctx *flash) |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 560 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 561 | unsigned int mask; |
Sean Nelson | 35727f7 | 2010-01-28 23:55:12 +0000 | [diff] [blame] | 562 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 563 | mask = getaddrmask(flash->chip); |
Sean Nelson | 35727f7 | 2010-01-28 23:55:12 +0000 | [diff] [blame] | 564 | return erase_chip_jedec_common(flash, mask); |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 565 | } |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 566 | |
| 567 | struct unlockblock { |
| 568 | unsigned int size; |
| 569 | unsigned int count; |
| 570 | }; |
| 571 | |
| 572 | typedef int (*unlockblock_func)(const struct flashctx *flash, chipaddr offset); |
| 573 | static int regspace2_walk_unlockblocks(const struct flashctx *flash, const struct unlockblock *block, unlockblock_func func) |
| 574 | { |
| 575 | chipaddr off = flash->virtual_registers + 2; |
| 576 | while (block->count != 0) { |
| 577 | unsigned int j; |
| 578 | for (j = 0; j < block->count; j++) { |
| 579 | if (func(flash, off)) |
| 580 | return -1; |
| 581 | off += block->size; |
| 582 | } |
| 583 | block++; |
| 584 | } |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | #define REG2_RWLOCK ((1 << 2) | (1 << 0)) |
| 589 | #define REG2_LOCKDOWN (1 << 1) |
| 590 | #define REG2_MASK (REG2_RWLOCK | REG2_LOCKDOWN) |
| 591 | |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 592 | static int printlock_regspace2_block(const struct flashctx *flash, chipaddr lockreg) |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 593 | { |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 594 | uint8_t state = chip_readb(flash, lockreg); |
| 595 | msg_cdbg("Lock status of block at 0x%0*" PRIxPTR " is ", PRIxPTR_WIDTH, lockreg); |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 596 | switch (state & REG2_MASK) { |
| 597 | case 0: |
| 598 | msg_cdbg("Full Access.\n"); |
| 599 | break; |
| 600 | case 1: |
| 601 | msg_cdbg("Write Lock (Default State).\n"); |
| 602 | break; |
| 603 | case 2: |
| 604 | msg_cdbg("Locked Open (Full Access, Locked Down).\n"); |
| 605 | break; |
| 606 | case 3: |
| 607 | msg_cdbg("Write Lock, Locked Down.\n"); |
| 608 | break; |
| 609 | case 4: |
| 610 | msg_cdbg("Read Lock.\n"); |
| 611 | break; |
| 612 | case 5: |
| 613 | msg_cdbg("Read/Write Lock.\n"); |
| 614 | break; |
| 615 | case 6: |
| 616 | msg_cdbg("Read Lock, Locked Down.\n"); |
| 617 | break; |
| 618 | case 7: |
| 619 | msg_cdbg("Read/Write Lock, Locked Down.\n"); |
| 620 | break; |
| 621 | } |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | int printlock_regspace2_blocks(const struct flashctx *flash, const struct unlockblock *blocks) |
| 626 | { |
| 627 | return regspace2_walk_unlockblocks(flash, blocks, &printlock_regspace2_block); |
| 628 | } |
| 629 | |
| 630 | static int printlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size) |
| 631 | { |
| 632 | const unsigned int elems = flash->chip->total_size * 1024 / block_size; |
| 633 | struct unlockblock blocks[2] = {{.size = block_size, .count = elems}}; |
| 634 | return regspace2_walk_unlockblocks(flash, blocks, &printlock_regspace2_block); |
| 635 | } |
| 636 | |
| 637 | int printlock_regspace2_uniform_64k(struct flashctx *flash) |
| 638 | { |
| 639 | return printlock_regspace2_uniform(flash, 64 * 1024); |
| 640 | } |
| 641 | |
| 642 | int printlock_regspace2_block_eraser_0(struct flashctx *flash) |
| 643 | { |
| 644 | // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated). |
| 645 | const struct unlockblock *unlockblocks = |
| 646 | (const struct unlockblock *)flash->chip->block_erasers[0].eraseblocks; |
| 647 | return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block); |
| 648 | } |
| 649 | |
| 650 | int printlock_regspace2_block_eraser_1(struct flashctx *flash) |
| 651 | { |
| 652 | // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated). |
| 653 | const struct unlockblock *unlockblocks = |
| 654 | (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks; |
| 655 | return regspace2_walk_unlockblocks(flash, unlockblocks, &printlock_regspace2_block); |
| 656 | } |
| 657 | |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 658 | /* Try to change the lock register at address lockreg from cur to new. |
| 659 | * |
| 660 | * - Try to unlock the lock bit if requested and it is currently set (although this is probably futile). |
| 661 | * - Try to change the read/write bits if requested. |
| 662 | * - Try to set the lockdown bit if requested. |
| 663 | * Return an error immediately if any of this fails. */ |
| 664 | static int changelock_regspace2_block(const struct flashctx *flash, chipaddr lockreg, uint8_t cur, uint8_t new) |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 665 | { |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 666 | /* Only allow changes to known read/write/lockdown bits */ |
| 667 | if (((cur ^ new) & ~REG2_MASK) != 0) { |
| 668 | msg_cerr("Invalid lock change from 0x%02x to 0x%02x requested at 0x%0*" PRIxPTR "!\n" |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 669 | "Please report a bug at flashrom@flashrom.org\n", |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 670 | cur, new, PRIxPTR_WIDTH, lockreg); |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 671 | return -1; |
| 672 | } |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 673 | |
| 674 | /* Exit early if no change (of read/write/lockdown bits) was requested. */ |
| 675 | if (((cur ^ new) & REG2_MASK) == 0) { |
| 676 | msg_cdbg2("Lock bits at 0x%0*" PRIxPTR " not changed.\n", PRIxPTR_WIDTH, lockreg); |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 677 | return 0; |
| 678 | } |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 679 | |
| 680 | /* Normally the lockdown bit can not be cleared. Try nevertheless if requested. */ |
| 681 | if ((cur & REG2_LOCKDOWN) && !(new & REG2_LOCKDOWN)) { |
| 682 | chip_writeb(flash, cur & ~REG2_LOCKDOWN, lockreg); |
| 683 | cur = chip_readb(flash, lockreg); |
| 684 | if ((cur & REG2_LOCKDOWN) == REG2_LOCKDOWN) { |
| 685 | msg_cwarn("Lockdown can't be removed at 0x%0*" PRIxPTR "! New value: 0x%02x.\n", |
| 686 | PRIxPTR_WIDTH, lockreg, cur); |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 687 | return -1; |
| 688 | } |
| 689 | } |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 690 | |
| 691 | /* Change read and/or write bit */ |
| 692 | if ((cur ^ new) & REG2_RWLOCK) { |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 693 | /* Do not lockdown yet. */ |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 694 | uint8_t wanted = (cur & ~REG2_RWLOCK) | (new & REG2_RWLOCK); |
| 695 | chip_writeb(flash, wanted, lockreg); |
| 696 | cur = chip_readb(flash, lockreg); |
| 697 | if (cur != wanted) { |
| 698 | msg_cerr("Changing lock bits failed at 0x%0*" PRIxPTR "! New value: 0x%02x.\n", |
| 699 | PRIxPTR_WIDTH, lockreg, cur); |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 700 | return -1; |
| 701 | } |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 702 | msg_cdbg("Changed lock bits at 0x%0*" PRIxPTR " to 0x%02x.\n", |
| 703 | PRIxPTR_WIDTH, lockreg, cur); |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 704 | } |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 705 | |
| 706 | /* Eventually, enable lockdown if requested. */ |
| 707 | if (!(cur & REG2_LOCKDOWN) && (new & REG2_LOCKDOWN)) { |
| 708 | chip_writeb(flash, new, lockreg); |
| 709 | cur = chip_readb(flash, lockreg); |
| 710 | if (cur != new) { |
| 711 | msg_cerr("Enabling lockdown FAILED at 0x%0*" PRIxPTR "! New value: 0x%02x.\n", |
| 712 | PRIxPTR_WIDTH, lockreg, cur); |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 713 | return -1; |
| 714 | } |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 715 | msg_cdbg("Enabled lockdown at 0x%0*" PRIxPTR ".\n", PRIxPTR_WIDTH, lockreg); |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 721 | static int unlock_regspace2_block_generic(const struct flashctx *flash, chipaddr lockreg) |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 722 | { |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 723 | uint8_t old = chip_readb(flash, lockreg); |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 724 | /* We don't care for the lockdown bit as long as the RW locks are 0 after we're done */ |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 725 | return changelock_regspace2_block(flash, lockreg, old, old & ~REG2_RWLOCK); |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | static int unlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size) |
| 729 | { |
| 730 | const unsigned int elems = flash->chip->total_size * 1024 / block_size; |
| 731 | struct unlockblock blocks[2] = {{.size = block_size, .count = elems}}; |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 732 | return regspace2_walk_unlockblocks(flash, blocks, &unlock_regspace2_block_generic); |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | int unlock_regspace2_uniform_64k(struct flashctx *flash) |
| 736 | { |
| 737 | return unlock_regspace2_uniform(flash, 64 * 1024); |
| 738 | } |
| 739 | |
| 740 | int unlock_regspace2_uniform_32k(struct flashctx *flash) |
| 741 | { |
| 742 | return unlock_regspace2_uniform(flash, 32 * 1024); |
| 743 | } |
| 744 | |
| 745 | int unlock_regspace2_block_eraser_0(struct flashctx *flash) |
| 746 | { |
| 747 | // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated). |
| 748 | const struct unlockblock *unlockblocks = |
| 749 | (const struct unlockblock *)flash->chip->block_erasers[0].eraseblocks; |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 750 | return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block_generic); |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | int unlock_regspace2_block_eraser_1(struct flashctx *flash) |
| 754 | { |
| 755 | // FIXME: this depends on the eraseblocks not to be filled up completely (i.e. to be null-terminated). |
| 756 | const struct unlockblock *unlockblocks = |
| 757 | (const struct unlockblock *)flash->chip->block_erasers[1].eraseblocks; |
Stefan Tauner | 5859ced | 2014-12-20 16:45:31 +0000 | [diff] [blame] | 758 | return regspace2_walk_unlockblocks(flash, unlockblocks, &unlock_regspace2_block_generic); |
Carl-Daniel Hailfinger | ef3ac8a | 2014-08-03 13:05:34 +0000 | [diff] [blame] | 759 | } |
| 760 | |