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