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