snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007, 2008, 2009, 2010 Carl-Daniel Hailfinger |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008 coresystems GmbH |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Contains the common SPI chip driver functions |
| 23 | */ |
| 24 | |
| 25 | #include <string.h> |
| 26 | #include "flash.h" |
| 27 | #include "flashchips.h" |
| 28 | #include "chipdrivers.h" |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 29 | #include "programmer.h" |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 30 | #include "spi.h" |
| 31 | |
| 32 | void spi_prettyprint_status_register(struct flashchip *flash); |
| 33 | |
| 34 | static int spi_rdid(unsigned char *readarr, int bytes) |
| 35 | { |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 36 | static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID }; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 37 | int ret; |
| 38 | int i; |
| 39 | |
| 40 | ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr); |
| 41 | if (ret) |
| 42 | return ret; |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 43 | msg_cspew("RDID returned"); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 44 | for (i = 0; i < bytes; i++) |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 45 | msg_cspew(" 0x%02x", readarr[i]); |
| 46 | msg_cspew(". "); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static int spi_rems(unsigned char *readarr) |
| 51 | { |
| 52 | unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 }; |
| 53 | uint32_t readaddr; |
| 54 | int ret; |
| 55 | |
| 56 | ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr); |
| 57 | if (ret == SPI_INVALID_ADDRESS) { |
| 58 | /* Find the lowest even address allowed for reads. */ |
| 59 | readaddr = (spi_get_valid_read_addr() + 1) & ~1; |
| 60 | cmd[1] = (readaddr >> 16) & 0xff, |
| 61 | cmd[2] = (readaddr >> 8) & 0xff, |
| 62 | cmd[3] = (readaddr >> 0) & 0xff, |
| 63 | ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr); |
| 64 | } |
| 65 | if (ret) |
| 66 | return ret; |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 67 | msg_cspew("REMS returned %02x %02x. ", readarr[0], readarr[1]); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 71 | static int spi_res(unsigned char *readarr, int bytes) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 72 | { |
| 73 | unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 }; |
| 74 | uint32_t readaddr; |
| 75 | int ret; |
hailfinger | cb0564e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 76 | int i; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 77 | |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 78 | ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 79 | if (ret == SPI_INVALID_ADDRESS) { |
| 80 | /* Find the lowest even address allowed for reads. */ |
| 81 | readaddr = (spi_get_valid_read_addr() + 1) & ~1; |
| 82 | cmd[1] = (readaddr >> 16) & 0xff, |
| 83 | cmd[2] = (readaddr >> 8) & 0xff, |
| 84 | cmd[3] = (readaddr >> 0) & 0xff, |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 85 | ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 86 | } |
| 87 | if (ret) |
| 88 | return ret; |
hailfinger | cb0564e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 89 | msg_cspew("RES returned"); |
| 90 | for (i = 0; i < bytes; i++) |
| 91 | msg_cspew(" 0x%02x", readarr[i]); |
| 92 | msg_cspew(". "); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | int spi_write_enable(void) |
| 97 | { |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 98 | static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN }; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 99 | int result; |
| 100 | |
| 101 | /* Send WREN (Write Enable) */ |
| 102 | result = spi_send_command(sizeof(cmd), 0, cmd, NULL); |
| 103 | |
| 104 | if (result) |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 105 | msg_cerr("%s failed\n", __func__); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 106 | |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | int spi_write_disable(void) |
| 111 | { |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 112 | static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI }; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 113 | |
| 114 | /* Send WRDI (Write Disable) */ |
| 115 | return spi_send_command(sizeof(cmd), 0, cmd, NULL); |
| 116 | } |
| 117 | |
| 118 | static int probe_spi_rdid_generic(struct flashchip *flash, int bytes) |
| 119 | { |
| 120 | unsigned char readarr[4]; |
| 121 | uint32_t id1; |
| 122 | uint32_t id2; |
| 123 | |
| 124 | if (spi_rdid(readarr, bytes)) |
| 125 | return 0; |
| 126 | |
| 127 | if (!oddparity(readarr[0])) |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 128 | msg_cdbg("RDID byte 0 parity violation. "); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 129 | |
hailfinger | cb0564e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 130 | /* Check if this is a continuation vendor ID. |
| 131 | * FIXME: Handle continuation device IDs. |
| 132 | */ |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 133 | if (readarr[0] == 0x7f) { |
| 134 | if (!oddparity(readarr[1])) |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 135 | msg_cdbg("RDID byte 1 parity violation. "); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 136 | id1 = (readarr[0] << 8) | readarr[1]; |
| 137 | id2 = readarr[2]; |
| 138 | if (bytes > 3) { |
| 139 | id2 <<= 8; |
| 140 | id2 |= readarr[3]; |
| 141 | } |
| 142 | } else { |
| 143 | id1 = readarr[0]; |
| 144 | id2 = (readarr[1] << 8) | readarr[2]; |
| 145 | } |
| 146 | |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 147 | msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 148 | |
| 149 | if (id1 == flash->manufacture_id && id2 == flash->model_id) { |
| 150 | /* Print the status register to tell the |
| 151 | * user about possible write protection. |
| 152 | */ |
| 153 | spi_prettyprint_status_register(flash); |
| 154 | |
| 155 | return 1; |
| 156 | } |
| 157 | |
| 158 | /* Test if this is a pure vendor match. */ |
| 159 | if (id1 == flash->manufacture_id && |
| 160 | GENERIC_DEVICE_ID == flash->model_id) |
| 161 | return 1; |
| 162 | |
| 163 | /* Test if there is any vendor ID. */ |
| 164 | if (GENERIC_MANUF_ID == flash->manufacture_id && |
| 165 | id1 != 0xff) |
| 166 | return 1; |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | int probe_spi_rdid(struct flashchip *flash) |
| 172 | { |
| 173 | return probe_spi_rdid_generic(flash, 3); |
| 174 | } |
| 175 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 176 | int probe_spi_rdid4(struct flashchip *flash) |
| 177 | { |
hailfinger | cb0564e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 178 | /* Some SPI controllers do not support commands with writecnt=1 and |
| 179 | * readcnt=4. |
| 180 | */ |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 181 | switch (spi_controller) { |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 182 | #if CONFIG_INTERNAL == 1 |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 183 | #if defined(__i386__) || defined(__x86_64__) |
hailfinger | cb0564e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 184 | case SPI_CONTROLLER_IT87XX: |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 185 | case SPI_CONTROLLER_WBSIO: |
hailfinger | cb0564e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 186 | msg_cinfo("4 byte RDID not supported on this SPI controller\n"); |
| 187 | return 0; |
| 188 | break; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 189 | #endif |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 190 | #endif |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 191 | default: |
hailfinger | cb0564e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 192 | return probe_spi_rdid_generic(flash, 4); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | int probe_spi_rems(struct flashchip *flash) |
| 199 | { |
| 200 | unsigned char readarr[JEDEC_REMS_INSIZE]; |
| 201 | uint32_t id1, id2; |
| 202 | |
| 203 | if (spi_rems(readarr)) |
| 204 | return 0; |
| 205 | |
| 206 | id1 = readarr[0]; |
| 207 | id2 = readarr[1]; |
| 208 | |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 209 | msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 210 | |
| 211 | if (id1 == flash->manufacture_id && id2 == flash->model_id) { |
| 212 | /* Print the status register to tell the |
| 213 | * user about possible write protection. |
| 214 | */ |
| 215 | spi_prettyprint_status_register(flash); |
| 216 | |
| 217 | return 1; |
| 218 | } |
| 219 | |
| 220 | /* Test if this is a pure vendor match. */ |
| 221 | if (id1 == flash->manufacture_id && |
| 222 | GENERIC_DEVICE_ID == flash->model_id) |
| 223 | return 1; |
| 224 | |
| 225 | /* Test if there is any vendor ID. */ |
| 226 | if (GENERIC_MANUF_ID == flash->manufacture_id && |
| 227 | id1 != 0xff) |
| 228 | return 1; |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 233 | int probe_spi_res1(struct flashchip *flash) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 234 | { |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 235 | static const unsigned char allff[] = {0xff, 0xff, 0xff}; |
| 236 | static const unsigned char all00[] = {0x00, 0x00, 0x00}; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 237 | unsigned char readarr[3]; |
| 238 | uint32_t id2; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 239 | |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 240 | /* We only want one-byte RES if RDID and REMS are unusable. */ |
| 241 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 242 | /* Check if RDID is usable and does not return 0xff 0xff 0xff or |
| 243 | * 0x00 0x00 0x00. In that case, RES is pointless. |
| 244 | */ |
| 245 | if (!spi_rdid(readarr, 3) && memcmp(readarr, allff, 3) && |
| 246 | memcmp(readarr, all00, 3)) { |
| 247 | msg_cdbg("Ignoring RES in favour of RDID.\n"); |
| 248 | return 0; |
| 249 | } |
| 250 | /* Check if REMS is usable and does not return 0xff 0xff or |
| 251 | * 0x00 0x00. In that case, RES is pointless. |
| 252 | */ |
| 253 | if (!spi_rems(readarr) && memcmp(readarr, allff, JEDEC_REMS_INSIZE) && |
| 254 | memcmp(readarr, all00, JEDEC_REMS_INSIZE)) { |
| 255 | msg_cdbg("Ignoring RES in favour of REMS.\n"); |
| 256 | return 0; |
| 257 | } |
| 258 | |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 259 | if (spi_res(readarr, 1)) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 260 | return 0; |
| 261 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 262 | id2 = readarr[0]; |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 263 | |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 264 | msg_cdbg("%s: id 0x%x\n", __func__, id2); |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 265 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 266 | if (id2 != flash->model_id) |
| 267 | return 0; |
| 268 | |
| 269 | /* Print the status register to tell the |
| 270 | * user about possible write protection. |
| 271 | */ |
| 272 | spi_prettyprint_status_register(flash); |
| 273 | return 1; |
| 274 | } |
| 275 | |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 276 | int probe_spi_res2(struct flashchip *flash) |
| 277 | { |
| 278 | unsigned char readarr[2]; |
| 279 | uint32_t id1, id2; |
| 280 | |
| 281 | if (spi_res(readarr, 2)) |
| 282 | return 0; |
| 283 | |
| 284 | id1 = readarr[0]; |
| 285 | id2 = readarr[1]; |
| 286 | |
| 287 | msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2); |
| 288 | |
| 289 | if (id1 != flash->manufacture_id || id2 != flash->model_id) |
| 290 | return 0; |
| 291 | |
| 292 | /* Print the status register to tell the |
| 293 | * user about possible write protection. |
| 294 | */ |
| 295 | spi_prettyprint_status_register(flash); |
| 296 | return 1; |
| 297 | } |
| 298 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 299 | uint8_t spi_read_status_register(void) |
| 300 | { |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 301 | static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR }; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 302 | /* FIXME: No workarounds for driver/hardware bugs in generic code. */ |
| 303 | unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */ |
| 304 | int ret; |
| 305 | |
| 306 | /* Read Status Register */ |
| 307 | ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr); |
| 308 | if (ret) |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 309 | msg_cerr("RDSR failed!\n"); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 310 | |
| 311 | return readarr[0]; |
| 312 | } |
| 313 | |
| 314 | /* Prettyprint the status register. Common definitions. */ |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 315 | static void spi_prettyprint_status_register_welwip(uint8_t status) |
| 316 | { |
| 317 | msg_cdbg("Chip status register: Write Enable Latch (WEL) is " |
| 318 | "%sset\n", (status & (1 << 1)) ? "" : "not "); |
| 319 | msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is " |
| 320 | "%sset\n", (status & (1 << 0)) ? "" : "not "); |
| 321 | } |
| 322 | |
| 323 | /* Prettyprint the status register. Common definitions. */ |
| 324 | static void spi_prettyprint_status_register_common(uint8_t status) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 325 | { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 326 | msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is " |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 327 | "%sset\n", (status & (1 << 5)) ? "" : "not "); |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 328 | msg_cdbg("Chip status register: Bit 4 / Block Protect 2 (BP2) is " |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 329 | "%sset\n", (status & (1 << 4)) ? "" : "not "); |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 330 | msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is " |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 331 | "%sset\n", (status & (1 << 3)) ? "" : "not "); |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 332 | msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is " |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 333 | "%sset\n", (status & (1 << 2)) ? "" : "not "); |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 334 | spi_prettyprint_status_register_welwip(status); |
| 335 | } |
| 336 | |
| 337 | /* Prettyprint the status register. Works for |
| 338 | * AMIC A25L series |
| 339 | */ |
| 340 | void spi_prettyprint_status_register_amic_a25l(uint8_t status) |
| 341 | { |
| 342 | msg_cdbg("Chip status register: Status Register Write Disable " |
| 343 | "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not "); |
| 344 | spi_prettyprint_status_register_common(status); |
| 345 | } |
| 346 | |
| 347 | /* Prettyprint the status register. Common definitions. */ |
| 348 | static void spi_prettyprint_status_register_at25_srplepewpp(uint8_t status) |
| 349 | { |
| 350 | msg_cdbg("Chip status register: Sector Protection Register Lock (SRPL) " |
| 351 | "is %sset\n", (status & (1 << 7)) ? "" : "not "); |
| 352 | msg_cdbg("Chip status register: Bit 6 " |
| 353 | "is %sset\n", (status & (1 << 6)) ? "" : "not "); |
| 354 | msg_cdbg("Chip status register: Erase/Program Error (EPE) " |
| 355 | "is %sset\n", (status & (1 << 5)) ? "" : "not "); |
| 356 | msg_cdbg("Chip status register: WP# pin (WPP) " |
| 357 | "is %sactive\n", (status & (1 << 4)) ? "not " : ""); |
| 358 | } |
| 359 | |
| 360 | int spi_prettyprint_status_register_at25df(struct flashchip *flash) |
| 361 | { |
| 362 | uint8_t status; |
| 363 | |
| 364 | status = spi_read_status_register(); |
| 365 | msg_cdbg("Chip status register is %02x\n", status); |
| 366 | |
| 367 | spi_prettyprint_status_register_at25_srplepewpp(status); |
| 368 | msg_cdbg("Chip status register: Software Protection Status (SWP): "); |
| 369 | switch (status & (3 << 2)) { |
| 370 | case 0x0 << 2: |
| 371 | msg_cdbg("no sectors are protected\n"); |
| 372 | break; |
| 373 | case 0x1 << 2: |
| 374 | msg_cdbg("some sectors are protected\n"); |
| 375 | /* FIXME: Read individual Sector Protection Registers. */ |
| 376 | break; |
| 377 | case 0x3 << 2: |
| 378 | msg_cdbg("all sectors are protected\n"); |
| 379 | break; |
| 380 | default: |
| 381 | msg_cdbg("reserved for future use\n"); |
| 382 | break; |
| 383 | } |
| 384 | spi_prettyprint_status_register_welwip(status); |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | int spi_prettyprint_status_register_at25df_sec(struct flashchip *flash) |
| 389 | { |
| 390 | /* FIXME: We should check the security lockdown. */ |
| 391 | msg_cdbg("Ignoring security lockdown (if present)\n"); |
| 392 | msg_cdbg("Ignoring status register byte 2\n"); |
| 393 | return spi_prettyprint_status_register_at25df(flash); |
| 394 | } |
| 395 | |
| 396 | int spi_prettyprint_status_register_at25f(struct flashchip *flash) |
| 397 | { |
| 398 | uint8_t status; |
| 399 | |
| 400 | status = spi_read_status_register(); |
| 401 | msg_cdbg("Chip status register is %02x\n", status); |
| 402 | |
| 403 | spi_prettyprint_status_register_at25_srplepewpp(status); |
| 404 | msg_cdbg("Chip status register: Bit 3 " |
| 405 | "is %sset\n", (status & (1 << 3)) ? "" : "not "); |
| 406 | msg_cdbg("Chip status register: Block Protect 0 (BP0) is " |
| 407 | "%sset, %s sectors are protected\n", |
| 408 | (status & (1 << 2)) ? "" : "not ", |
| 409 | (status & (1 << 2)) ? "all" : "no"); |
| 410 | spi_prettyprint_status_register_welwip(status); |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | int spi_prettyprint_status_register_at25fs010(struct flashchip *flash) |
| 415 | { |
| 416 | uint8_t status; |
| 417 | |
| 418 | status = spi_read_status_register(); |
| 419 | msg_cdbg("Chip status register is %02x\n", status); |
| 420 | |
| 421 | msg_cdbg("Chip status register: Status Register Write Protect (WPEN) " |
| 422 | "is %sset\n", (status & (1 << 7)) ? "" : "not "); |
| 423 | msg_cdbg("Chip status register: Bit 6 / Block Protect 4 (BP4) is " |
| 424 | "%sset\n", (status & (1 << 6)) ? "" : "not "); |
| 425 | msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is " |
| 426 | "%sset\n", (status & (1 << 5)) ? "" : "not "); |
| 427 | msg_cdbg("Chip status register: Bit 4 is " |
| 428 | "%sset\n", (status & (1 << 4)) ? "" : "not "); |
| 429 | msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is " |
| 430 | "%sset\n", (status & (1 << 3)) ? "" : "not "); |
| 431 | msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is " |
| 432 | "%sset\n", (status & (1 << 2)) ? "" : "not "); |
| 433 | /* FIXME: Pretty-print detailed sector protection status. */ |
| 434 | spi_prettyprint_status_register_welwip(status); |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | int spi_prettyprint_status_register_at25fs040(struct flashchip *flash) |
| 439 | { |
| 440 | uint8_t status; |
| 441 | |
| 442 | status = spi_read_status_register(); |
| 443 | msg_cdbg("Chip status register is %02x\n", status); |
| 444 | |
| 445 | msg_cdbg("Chip status register: Status Register Write Protect (WPEN) " |
| 446 | "is %sset\n", (status & (1 << 7)) ? "" : "not "); |
| 447 | msg_cdbg("Chip status register: Bit 6 / Block Protect 4 (BP4) is " |
| 448 | "%sset\n", (status & (1 << 6)) ? "" : "not "); |
| 449 | msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is " |
| 450 | "%sset\n", (status & (1 << 5)) ? "" : "not "); |
| 451 | msg_cdbg("Chip status register: Bit 4 / Block Protect 2 (BP2) is " |
| 452 | "%sset\n", (status & (1 << 4)) ? "" : "not "); |
| 453 | msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is " |
| 454 | "%sset\n", (status & (1 << 3)) ? "" : "not "); |
| 455 | msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is " |
| 456 | "%sset\n", (status & (1 << 2)) ? "" : "not "); |
| 457 | /* FIXME: Pretty-print detailed sector protection status. */ |
| 458 | spi_prettyprint_status_register_welwip(status); |
| 459 | return 0; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* Prettyprint the status register. Works for |
| 463 | * ST M25P series |
| 464 | * MX MX25L series |
| 465 | */ |
| 466 | void spi_prettyprint_status_register_st_m25p(uint8_t status) |
| 467 | { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 468 | msg_cdbg("Chip status register: Status Register Write Disable " |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 469 | "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not "); |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 470 | msg_cdbg("Chip status register: Bit 6 is " |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 471 | "%sset\n", (status & (1 << 6)) ? "" : "not "); |
| 472 | spi_prettyprint_status_register_common(status); |
| 473 | } |
| 474 | |
| 475 | void spi_prettyprint_status_register_sst25(uint8_t status) |
| 476 | { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 477 | msg_cdbg("Chip status register: Block Protect Write Disable " |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 478 | "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not "); |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 479 | msg_cdbg("Chip status register: Auto Address Increment Programming " |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 480 | "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not "); |
| 481 | spi_prettyprint_status_register_common(status); |
| 482 | } |
| 483 | |
| 484 | /* Prettyprint the status register. Works for |
| 485 | * SST 25VF016 |
| 486 | */ |
| 487 | void spi_prettyprint_status_register_sst25vf016(uint8_t status) |
| 488 | { |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 489 | static const char *const bpt[] = { |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 490 | "none", |
| 491 | "1F0000H-1FFFFFH", |
| 492 | "1E0000H-1FFFFFH", |
| 493 | "1C0000H-1FFFFFH", |
| 494 | "180000H-1FFFFFH", |
| 495 | "100000H-1FFFFFH", |
| 496 | "all", "all" |
| 497 | }; |
| 498 | spi_prettyprint_status_register_sst25(status); |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 499 | msg_cdbg("Resulting block protection : %s\n", |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 500 | bpt[(status & 0x1c) >> 2]); |
| 501 | } |
| 502 | |
| 503 | void spi_prettyprint_status_register_sst25vf040b(uint8_t status) |
| 504 | { |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 505 | static const char *const bpt[] = { |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 506 | "none", |
| 507 | "0x70000-0x7ffff", |
| 508 | "0x60000-0x7ffff", |
| 509 | "0x40000-0x7ffff", |
| 510 | "all blocks", "all blocks", "all blocks", "all blocks" |
| 511 | }; |
| 512 | spi_prettyprint_status_register_sst25(status); |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 513 | msg_cdbg("Resulting block protection : %s\n", |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 514 | bpt[(status & 0x1c) >> 2]); |
| 515 | } |
| 516 | |
| 517 | void spi_prettyprint_status_register(struct flashchip *flash) |
| 518 | { |
| 519 | uint8_t status; |
| 520 | |
| 521 | status = spi_read_status_register(); |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 522 | msg_cdbg("Chip status register is %02x\n", status); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 523 | switch (flash->manufacture_id) { |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 524 | case AMIC_ID: |
| 525 | if ((flash->model_id & 0xff00) == 0x2000) |
| 526 | spi_prettyprint_status_register_amic_a25l(status); |
| 527 | break; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 528 | case ST_ID: |
| 529 | if (((flash->model_id & 0xff00) == 0x2000) || |
| 530 | ((flash->model_id & 0xff00) == 0x2500)) |
| 531 | spi_prettyprint_status_register_st_m25p(status); |
| 532 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 533 | case MACRONIX_ID: |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 534 | if ((flash->model_id & 0xff00) == 0x2000) |
| 535 | spi_prettyprint_status_register_st_m25p(status); |
| 536 | break; |
| 537 | case SST_ID: |
| 538 | switch (flash->model_id) { |
| 539 | case 0x2541: |
| 540 | spi_prettyprint_status_register_sst25vf016(status); |
| 541 | break; |
| 542 | case 0x8d: |
| 543 | case 0x258d: |
| 544 | spi_prettyprint_status_register_sst25vf040b(status); |
| 545 | break; |
| 546 | default: |
| 547 | spi_prettyprint_status_register_sst25(status); |
| 548 | break; |
| 549 | } |
| 550 | break; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | int spi_chip_erase_60(struct flashchip *flash) |
| 555 | { |
| 556 | int result; |
| 557 | struct spi_command cmds[] = { |
| 558 | { |
| 559 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 560 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 561 | .readcnt = 0, |
| 562 | .readarr = NULL, |
| 563 | }, { |
| 564 | .writecnt = JEDEC_CE_60_OUTSIZE, |
| 565 | .writearr = (const unsigned char[]){ JEDEC_CE_60 }, |
| 566 | .readcnt = 0, |
| 567 | .readarr = NULL, |
| 568 | }, { |
| 569 | .writecnt = 0, |
| 570 | .writearr = NULL, |
| 571 | .readcnt = 0, |
| 572 | .readarr = NULL, |
| 573 | }}; |
| 574 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 575 | result = spi_send_multicommand(cmds); |
| 576 | if (result) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 577 | msg_cerr("%s failed during command execution\n", |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 578 | __func__); |
| 579 | return result; |
| 580 | } |
| 581 | /* Wait until the Write-In-Progress bit is cleared. |
| 582 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 583 | */ |
| 584 | /* FIXME: We assume spi_read_status_register will never fail. */ |
| 585 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 586 | programmer_delay(1000 * 1000); |
| 587 | if (check_erased_range(flash, 0, flash->total_size * 1024)) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 588 | msg_cerr("ERASE FAILED!\n"); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 589 | return -1; |
| 590 | } |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | int spi_chip_erase_c7(struct flashchip *flash) |
| 595 | { |
| 596 | int result; |
| 597 | struct spi_command cmds[] = { |
| 598 | { |
| 599 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 600 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 601 | .readcnt = 0, |
| 602 | .readarr = NULL, |
| 603 | }, { |
| 604 | .writecnt = JEDEC_CE_C7_OUTSIZE, |
| 605 | .writearr = (const unsigned char[]){ JEDEC_CE_C7 }, |
| 606 | .readcnt = 0, |
| 607 | .readarr = NULL, |
| 608 | }, { |
| 609 | .writecnt = 0, |
| 610 | .writearr = NULL, |
| 611 | .readcnt = 0, |
| 612 | .readarr = NULL, |
| 613 | }}; |
| 614 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 615 | result = spi_send_multicommand(cmds); |
| 616 | if (result) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 617 | msg_cerr("%s failed during command execution\n", __func__); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 618 | return result; |
| 619 | } |
| 620 | /* Wait until the Write-In-Progress bit is cleared. |
| 621 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 622 | */ |
| 623 | /* FIXME: We assume spi_read_status_register will never fail. */ |
| 624 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 625 | programmer_delay(1000 * 1000); |
| 626 | if (check_erased_range(flash, 0, flash->total_size * 1024)) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 627 | msg_cerr("ERASE FAILED!\n"); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 628 | return -1; |
| 629 | } |
| 630 | return 0; |
| 631 | } |
| 632 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 633 | int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
| 634 | { |
| 635 | int result; |
| 636 | struct spi_command cmds[] = { |
| 637 | { |
| 638 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 639 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 640 | .readcnt = 0, |
| 641 | .readarr = NULL, |
| 642 | }, { |
| 643 | .writecnt = JEDEC_BE_52_OUTSIZE, |
| 644 | .writearr = (const unsigned char[]){ |
| 645 | JEDEC_BE_52, |
| 646 | (addr >> 16) & 0xff, |
| 647 | (addr >> 8) & 0xff, |
| 648 | (addr & 0xff) |
| 649 | }, |
| 650 | .readcnt = 0, |
| 651 | .readarr = NULL, |
| 652 | }, { |
| 653 | .writecnt = 0, |
| 654 | .writearr = NULL, |
| 655 | .readcnt = 0, |
| 656 | .readarr = NULL, |
| 657 | }}; |
| 658 | |
| 659 | result = spi_send_multicommand(cmds); |
| 660 | if (result) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 661 | msg_cerr("%s failed during command execution at address 0x%x\n", |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 662 | __func__, addr); |
| 663 | return result; |
| 664 | } |
| 665 | /* Wait until the Write-In-Progress bit is cleared. |
| 666 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 667 | */ |
| 668 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 669 | programmer_delay(100 * 1000); |
| 670 | if (check_erased_range(flash, addr, blocklen)) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 671 | msg_cerr("ERASE FAILED!\n"); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 672 | return -1; |
| 673 | } |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | /* Block size is usually |
| 678 | * 64k for Macronix |
| 679 | * 32k for SST |
| 680 | * 4-32k non-uniform for EON |
| 681 | */ |
| 682 | int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
| 683 | { |
| 684 | int result; |
| 685 | struct spi_command cmds[] = { |
| 686 | { |
| 687 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 688 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 689 | .readcnt = 0, |
| 690 | .readarr = NULL, |
| 691 | }, { |
| 692 | .writecnt = JEDEC_BE_D8_OUTSIZE, |
| 693 | .writearr = (const unsigned char[]){ |
| 694 | JEDEC_BE_D8, |
| 695 | (addr >> 16) & 0xff, |
| 696 | (addr >> 8) & 0xff, |
| 697 | (addr & 0xff) |
| 698 | }, |
| 699 | .readcnt = 0, |
| 700 | .readarr = NULL, |
| 701 | }, { |
| 702 | .writecnt = 0, |
| 703 | .writearr = NULL, |
| 704 | .readcnt = 0, |
| 705 | .readarr = NULL, |
| 706 | }}; |
| 707 | |
| 708 | result = spi_send_multicommand(cmds); |
| 709 | if (result) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 710 | msg_cerr("%s failed during command execution at address 0x%x\n", |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 711 | __func__, addr); |
| 712 | return result; |
| 713 | } |
| 714 | /* Wait until the Write-In-Progress bit is cleared. |
| 715 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 716 | */ |
| 717 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 718 | programmer_delay(100 * 1000); |
| 719 | if (check_erased_range(flash, addr, blocklen)) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 720 | msg_cerr("ERASE FAILED!\n"); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 721 | return -1; |
| 722 | } |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | /* Block size is usually |
| 727 | * 4k for PMC |
| 728 | */ |
| 729 | int spi_block_erase_d7(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
| 730 | { |
| 731 | int result; |
| 732 | struct spi_command cmds[] = { |
| 733 | { |
| 734 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 735 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 736 | .readcnt = 0, |
| 737 | .readarr = NULL, |
| 738 | }, { |
| 739 | .writecnt = JEDEC_BE_D7_OUTSIZE, |
| 740 | .writearr = (const unsigned char[]){ |
| 741 | JEDEC_BE_D7, |
| 742 | (addr >> 16) & 0xff, |
| 743 | (addr >> 8) & 0xff, |
| 744 | (addr & 0xff) |
| 745 | }, |
| 746 | .readcnt = 0, |
| 747 | .readarr = NULL, |
| 748 | }, { |
| 749 | .writecnt = 0, |
| 750 | .writearr = NULL, |
| 751 | .readcnt = 0, |
| 752 | .readarr = NULL, |
| 753 | }}; |
| 754 | |
| 755 | result = spi_send_multicommand(cmds); |
| 756 | if (result) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 757 | msg_cerr("%s failed during command execution at address 0x%x\n", |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 758 | __func__, addr); |
| 759 | return result; |
| 760 | } |
| 761 | /* Wait until the Write-In-Progress bit is cleared. |
| 762 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 763 | */ |
| 764 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 765 | programmer_delay(100 * 1000); |
| 766 | if (check_erased_range(flash, addr, blocklen)) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 767 | msg_cerr("ERASE FAILED!\n"); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 768 | return -1; |
| 769 | } |
| 770 | return 0; |
| 771 | } |
| 772 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 773 | /* Sector size is usually 4k, though Macronix eliteflash has 64k */ |
| 774 | int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
| 775 | { |
Stefan Reinauer | cce56d5 | 2010-11-22 18:22:21 -0800 | [diff] [blame] | 776 | int result, retries; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 777 | struct spi_command cmds[] = { |
| 778 | { |
| 779 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 780 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 781 | .readcnt = 0, |
| 782 | .readarr = NULL, |
| 783 | }, { |
| 784 | .writecnt = JEDEC_SE_OUTSIZE, |
| 785 | .writearr = (const unsigned char[]){ |
| 786 | JEDEC_SE, |
| 787 | (addr >> 16) & 0xff, |
| 788 | (addr >> 8) & 0xff, |
| 789 | (addr & 0xff) |
| 790 | }, |
| 791 | .readcnt = 0, |
| 792 | .readarr = NULL, |
| 793 | }, { |
| 794 | .writecnt = 0, |
| 795 | .writearr = NULL, |
| 796 | .readcnt = 0, |
| 797 | .readarr = NULL, |
| 798 | }}; |
| 799 | |
Stefan Reinauer | cce56d5 | 2010-11-22 18:22:21 -0800 | [diff] [blame] | 800 | retries = 5; |
| 801 | do { |
| 802 | result = spi_send_multicommand(cmds); |
| 803 | if (result) |
| 804 | programmer_delay(10 * 1000); |
| 805 | } while (result && retries--); |
| 806 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 807 | if (result) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 808 | msg_cerr("%s failed during command execution at address 0x%x\n", |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 809 | __func__, addr); |
| 810 | return result; |
| 811 | } |
| 812 | /* Wait until the Write-In-Progress bit is cleared. |
| 813 | * This usually takes 15-800 ms, so wait in 10 ms steps. |
| 814 | */ |
| 815 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 816 | programmer_delay(10 * 1000); |
| 817 | if (check_erased_range(flash, addr, blocklen)) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 818 | msg_cerr("ERASE FAILED!\n"); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 819 | return -1; |
| 820 | } |
| 821 | return 0; |
| 822 | } |
| 823 | |
| 824 | int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
| 825 | { |
| 826 | if ((addr != 0) || (blocklen != flash->total_size * 1024)) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 827 | msg_cerr("%s called with incorrect arguments\n", |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 828 | __func__); |
| 829 | return -1; |
| 830 | } |
| 831 | return spi_chip_erase_60(flash); |
| 832 | } |
| 833 | |
| 834 | int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
| 835 | { |
| 836 | if ((addr != 0) || (blocklen != flash->total_size * 1024)) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 837 | msg_cerr("%s called with incorrect arguments\n", |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 838 | __func__); |
| 839 | return -1; |
| 840 | } |
| 841 | return spi_chip_erase_c7(flash); |
| 842 | } |
| 843 | |
| 844 | int spi_write_status_enable(void) |
| 845 | { |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 846 | static const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR }; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 847 | int result; |
| 848 | |
| 849 | /* Send EWSR (Enable Write Status Register). */ |
| 850 | result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL); |
| 851 | |
| 852 | if (result) |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 853 | msg_cerr("%s failed\n", __func__); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 854 | |
| 855 | return result; |
| 856 | } |
| 857 | |
| 858 | /* |
| 859 | * This is according the SST25VF016 datasheet, who knows it is more |
| 860 | * generic that this... |
| 861 | */ |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 862 | static int spi_write_status_register_ewsr(struct flashchip *flash, int status) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 863 | { |
| 864 | int result; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 865 | int i = 0; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 866 | struct spi_command cmds[] = { |
| 867 | { |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 868 | /* WRSR requires either EWSR or WREN depending on chip type. */ |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 869 | .writecnt = JEDEC_EWSR_OUTSIZE, |
| 870 | .writearr = (const unsigned char[]){ JEDEC_EWSR }, |
| 871 | .readcnt = 0, |
| 872 | .readarr = NULL, |
| 873 | }, { |
| 874 | .writecnt = JEDEC_WRSR_OUTSIZE, |
| 875 | .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status }, |
| 876 | .readcnt = 0, |
| 877 | .readarr = NULL, |
| 878 | }, { |
| 879 | .writecnt = 0, |
| 880 | .writearr = NULL, |
| 881 | .readcnt = 0, |
| 882 | .readarr = NULL, |
| 883 | }}; |
| 884 | |
| 885 | result = spi_send_multicommand(cmds); |
| 886 | if (result) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 887 | msg_cerr("%s failed during command execution\n", |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 888 | __func__); |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 889 | /* No point in waiting for the command to complete if execution |
| 890 | * failed. |
| 891 | */ |
| 892 | return result; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 893 | } |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 894 | /* WRSR performs a self-timed erase before the changes take effect. |
| 895 | * This may take 50-85 ms in most cases, and some chips apparently |
| 896 | * allow running RDSR only once. Therefore pick an initial delay of |
| 897 | * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed. |
| 898 | */ |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 899 | programmer_delay(100 * 1000); |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 900 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) { |
| 901 | if (++i > 490) { |
| 902 | msg_cerr("Error: WIP bit after WRSR never cleared\n"); |
| 903 | return TIMEOUT_ERROR; |
| 904 | } |
| 905 | programmer_delay(10 * 1000); |
| 906 | } |
| 907 | return 0; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 908 | } |
| 909 | |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 910 | static int spi_write_status_register_wren(struct flashchip *flash, int status) |
| 911 | { |
| 912 | int result; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 913 | int i = 0; |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 914 | struct spi_command cmds[] = { |
| 915 | { |
| 916 | /* WRSR requires either EWSR or WREN depending on chip type. */ |
| 917 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 918 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 919 | .readcnt = 0, |
| 920 | .readarr = NULL, |
| 921 | }, { |
| 922 | .writecnt = JEDEC_WRSR_OUTSIZE, |
| 923 | .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status }, |
| 924 | .readcnt = 0, |
| 925 | .readarr = NULL, |
| 926 | }, { |
| 927 | .writecnt = 0, |
| 928 | .writearr = NULL, |
| 929 | .readcnt = 0, |
| 930 | .readarr = NULL, |
| 931 | }}; |
| 932 | |
| 933 | result = spi_send_multicommand(cmds); |
| 934 | if (result) { |
| 935 | msg_cerr("%s failed during command execution\n", |
| 936 | __func__); |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 937 | /* No point in waiting for the command to complete if execution |
| 938 | * failed. |
| 939 | */ |
| 940 | return result; |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 941 | } |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 942 | /* WRSR performs a self-timed erase before the changes take effect. |
| 943 | * This may take 50-85 ms in most cases, and some chips apparently |
| 944 | * allow running RDSR only once. Therefore pick an initial delay of |
| 945 | * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed. |
| 946 | */ |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 947 | programmer_delay(100 * 1000); |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 948 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) { |
| 949 | if (++i > 490) { |
| 950 | msg_cerr("Error: WIP bit after WRSR never cleared\n"); |
| 951 | return TIMEOUT_ERROR; |
| 952 | } |
| 953 | programmer_delay(10 * 1000); |
| 954 | } |
| 955 | return 0; |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | int spi_write_status_register(struct flashchip *flash, int status) |
| 959 | { |
| 960 | int ret = 1; |
| 961 | |
| 962 | if (!(flash->feature_bits & (FEATURE_WRSR_WREN | FEATURE_WRSR_EWSR))) { |
| 963 | msg_cdbg("Missing status register write definition, assuming " |
| 964 | "EWSR is needed\n"); |
| 965 | flash->feature_bits |= FEATURE_WRSR_EWSR; |
| 966 | } |
| 967 | if (flash->feature_bits & FEATURE_WRSR_WREN) |
| 968 | ret = spi_write_status_register_wren(flash, status); |
| 969 | if (ret && (flash->feature_bits & FEATURE_WRSR_EWSR)) |
| 970 | ret = spi_write_status_register_ewsr(flash, status); |
| 971 | return ret; |
| 972 | } |
| 973 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 974 | int spi_byte_program(int addr, uint8_t databyte) |
| 975 | { |
| 976 | int result; |
| 977 | struct spi_command cmds[] = { |
| 978 | { |
| 979 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 980 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 981 | .readcnt = 0, |
| 982 | .readarr = NULL, |
| 983 | }, { |
| 984 | .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE, |
| 985 | .writearr = (const unsigned char[]){ |
| 986 | JEDEC_BYTE_PROGRAM, |
| 987 | (addr >> 16) & 0xff, |
| 988 | (addr >> 8) & 0xff, |
| 989 | (addr & 0xff), |
| 990 | databyte |
| 991 | }, |
| 992 | .readcnt = 0, |
| 993 | .readarr = NULL, |
| 994 | }, { |
| 995 | .writecnt = 0, |
| 996 | .writearr = NULL, |
| 997 | .readcnt = 0, |
| 998 | .readarr = NULL, |
| 999 | }}; |
| 1000 | |
| 1001 | result = spi_send_multicommand(cmds); |
| 1002 | if (result) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 1003 | msg_cerr("%s failed during command execution at address 0x%x\n", |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1004 | __func__, addr); |
| 1005 | } |
| 1006 | return result; |
| 1007 | } |
| 1008 | |
| 1009 | int spi_nbyte_program(int addr, uint8_t *bytes, int len) |
| 1010 | { |
| 1011 | int result; |
| 1012 | /* FIXME: Switch to malloc based on len unless that kills speed. */ |
| 1013 | unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = { |
| 1014 | JEDEC_BYTE_PROGRAM, |
| 1015 | (addr >> 16) & 0xff, |
| 1016 | (addr >> 8) & 0xff, |
| 1017 | (addr >> 0) & 0xff, |
| 1018 | }; |
| 1019 | struct spi_command cmds[] = { |
| 1020 | { |
| 1021 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 1022 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 1023 | .readcnt = 0, |
| 1024 | .readarr = NULL, |
| 1025 | }, { |
| 1026 | .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len, |
| 1027 | .writearr = cmd, |
| 1028 | .readcnt = 0, |
| 1029 | .readarr = NULL, |
| 1030 | }, { |
| 1031 | .writecnt = 0, |
| 1032 | .writearr = NULL, |
| 1033 | .readcnt = 0, |
| 1034 | .readarr = NULL, |
| 1035 | }}; |
| 1036 | |
| 1037 | if (!len) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 1038 | msg_cerr("%s called for zero-length write\n", __func__); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1039 | return 1; |
| 1040 | } |
| 1041 | if (len > 256) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 1042 | msg_cerr("%s called for too long a write\n", __func__); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1043 | return 1; |
| 1044 | } |
| 1045 | |
| 1046 | memcpy(&cmd[4], bytes, len); |
| 1047 | |
| 1048 | result = spi_send_multicommand(cmds); |
| 1049 | if (result) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 1050 | msg_cerr("%s failed during command execution at address 0x%x\n", |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1051 | __func__, addr); |
| 1052 | } |
| 1053 | return result; |
| 1054 | } |
| 1055 | |
David Hendricks | bf36f09 | 2010-11-02 23:39:29 -0700 | [diff] [blame] | 1056 | int spi_restore_status(struct flashchip *flash, uint8_t status) |
| 1057 | { |
| 1058 | msg_cdbg("restoring chip status (0x%02x)\n", status); |
| 1059 | return spi_write_status_register(flash, status); |
| 1060 | } |
| 1061 | |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1062 | /* A generic brute-force block protection disable works like this: |
| 1063 | * Write 0x00 to the status register. Check if any locks are still set (that |
| 1064 | * part is chip specific). Repeat once. |
| 1065 | */ |
| 1066 | int spi_disable_blockprotect(struct flashchip *flash) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1067 | { |
| 1068 | uint8_t status; |
| 1069 | int result; |
| 1070 | |
| 1071 | status = spi_read_status_register(); |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1072 | /* If block protection is disabled, stop here. */ |
| 1073 | if ((status & 0x3c) == 0) |
| 1074 | return 0; |
| 1075 | |
David Hendricks | bf36f09 | 2010-11-02 23:39:29 -0700 | [diff] [blame] | 1076 | /* restore status register content upon exit */ |
| 1077 | register_chip_restore(spi_restore_status, flash, status); |
| 1078 | |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1079 | msg_cdbg("Some block protection in effect, disabling\n"); |
| 1080 | result = spi_write_status_register(flash, status & ~0x3c); |
| 1081 | if (result) { |
| 1082 | msg_cerr("spi_write_status_register failed\n"); |
| 1083 | return result; |
| 1084 | } |
| 1085 | status = spi_read_status_register(); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1086 | if ((status & 0x3c) != 0) { |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1087 | msg_cerr("Block protection could not be disabled!\n"); |
| 1088 | return 1; |
| 1089 | } |
| 1090 | return 0; |
| 1091 | } |
| 1092 | |
| 1093 | int spi_disable_blockprotect_at25df(struct flashchip *flash) |
| 1094 | { |
| 1095 | uint8_t status; |
| 1096 | int result; |
| 1097 | |
| 1098 | status = spi_read_status_register(); |
| 1099 | /* If block protection is disabled, stop here. */ |
| 1100 | if ((status & (3 << 2)) == 0) |
| 1101 | return 0; |
| 1102 | |
| 1103 | msg_cdbg("Some block protection in effect, disabling\n"); |
| 1104 | if (status & (1 << 7)) { |
| 1105 | msg_cdbg("Need to disable Sector Protection Register Lock\n"); |
| 1106 | if ((status & (1 << 4)) == 0) { |
| 1107 | msg_cerr("WP# pin is active, disabling " |
| 1108 | "write protection is impossible.\n"); |
| 1109 | return 1; |
| 1110 | } |
| 1111 | /* All bits except bit 7 (SPRL) are readonly. */ |
| 1112 | result = spi_write_status_register(flash, status & ~(1 << 7)); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1113 | if (result) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 1114 | msg_cerr("spi_write_status_register failed\n"); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1115 | return result; |
| 1116 | } |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1117 | |
| 1118 | } |
| 1119 | /* Global unprotect. Make sure to mask SPRL as well. */ |
| 1120 | result = spi_write_status_register(flash, status & ~0xbc); |
| 1121 | if (result) { |
| 1122 | msg_cerr("spi_write_status_register failed\n"); |
| 1123 | return result; |
| 1124 | } |
| 1125 | status = spi_read_status_register(); |
| 1126 | if ((status & (3 << 2)) != 0) { |
| 1127 | msg_cerr("Block protection could not be disabled!\n"); |
| 1128 | return 1; |
| 1129 | } |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | int spi_disable_blockprotect_at25df_sec(struct flashchip *flash) |
| 1134 | { |
| 1135 | /* FIXME: We should check the security lockdown. */ |
| 1136 | msg_cinfo("Ignoring security lockdown (if present)\n"); |
| 1137 | return spi_disable_blockprotect_at25df(flash); |
| 1138 | } |
| 1139 | |
| 1140 | int spi_disable_blockprotect_at25f(struct flashchip *flash) |
| 1141 | { |
| 1142 | /* spi_disable_blockprotect_at25df is not really the right way to do |
| 1143 | * this, but the side effects of said function work here as well. |
| 1144 | */ |
| 1145 | return spi_disable_blockprotect_at25df(flash); |
| 1146 | } |
| 1147 | |
| 1148 | int spi_disable_blockprotect_at25fs010(struct flashchip *flash) |
| 1149 | { |
| 1150 | uint8_t status; |
| 1151 | int result; |
| 1152 | |
| 1153 | status = spi_read_status_register(); |
| 1154 | /* If block protection is disabled, stop here. */ |
| 1155 | if ((status & 0x6c) == 0) |
| 1156 | return 0; |
| 1157 | |
| 1158 | msg_cdbg("Some block protection in effect, disabling\n"); |
| 1159 | if (status & (1 << 7)) { |
| 1160 | msg_cdbg("Need to disable Status Register Write Protect\n"); |
| 1161 | /* Clear bit 7 (WPEN). */ |
| 1162 | result = spi_write_status_register(flash, status & ~(1 << 7)); |
| 1163 | if (result) { |
| 1164 | msg_cerr("spi_write_status_register failed\n"); |
| 1165 | return result; |
| 1166 | } |
| 1167 | } |
| 1168 | /* Global unprotect. Make sure to mask WPEN as well. */ |
| 1169 | result = spi_write_status_register(flash, status & ~0xec); |
| 1170 | if (result) { |
| 1171 | msg_cerr("spi_write_status_register failed\n"); |
| 1172 | return result; |
| 1173 | } |
| 1174 | status = spi_read_status_register(); |
| 1175 | if ((status & 0x6c) != 0) { |
| 1176 | msg_cerr("Block protection could not be disabled!\n"); |
| 1177 | return 1; |
| 1178 | } |
| 1179 | return 0; |
| 1180 | } |
| 1181 | int spi_disable_blockprotect_at25fs040(struct flashchip *flash) |
| 1182 | { |
| 1183 | uint8_t status; |
| 1184 | int result; |
| 1185 | |
| 1186 | status = spi_read_status_register(); |
| 1187 | /* If block protection is disabled, stop here. */ |
| 1188 | if ((status & 0x7c) == 0) |
| 1189 | return 0; |
| 1190 | |
| 1191 | msg_cdbg("Some block protection in effect, disabling\n"); |
| 1192 | if (status & (1 << 7)) { |
| 1193 | msg_cdbg("Need to disable Status Register Write Protect\n"); |
| 1194 | /* Clear bit 7 (WPEN). */ |
| 1195 | result = spi_write_status_register(flash, status & ~(1 << 7)); |
| 1196 | if (result) { |
| 1197 | msg_cerr("spi_write_status_register failed\n"); |
| 1198 | return result; |
| 1199 | } |
| 1200 | } |
| 1201 | /* Global unprotect. Make sure to mask WPEN as well. */ |
| 1202 | result = spi_write_status_register(flash, status & ~0xfc); |
| 1203 | if (result) { |
| 1204 | msg_cerr("spi_write_status_register failed\n"); |
| 1205 | return result; |
| 1206 | } |
| 1207 | status = spi_read_status_register(); |
| 1208 | if ((status & 0x7c) != 0) { |
| 1209 | msg_cerr("Block protection could not be disabled!\n"); |
| 1210 | return 1; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1211 | } |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | int spi_nbyte_read(int address, uint8_t *bytes, int len) |
| 1216 | { |
| 1217 | const unsigned char cmd[JEDEC_READ_OUTSIZE] = { |
| 1218 | JEDEC_READ, |
| 1219 | (address >> 16) & 0xff, |
| 1220 | (address >> 8) & 0xff, |
| 1221 | (address >> 0) & 0xff, |
| 1222 | }; |
| 1223 | |
| 1224 | /* Send Read */ |
| 1225 | return spi_send_command(sizeof(cmd), len, cmd, bytes); |
| 1226 | } |
| 1227 | |
| 1228 | /* |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 1229 | * Read a part of the flash chip. |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1230 | * FIXME: Use the chunk code from Michael Karcher instead. |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1231 | * Each page is read separately in chunks with a maximum size of chunksize. |
| 1232 | */ |
| 1233 | int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize) |
| 1234 | { |
| 1235 | int rc = 0; |
| 1236 | int i, j, starthere, lenhere; |
| 1237 | int page_size = flash->page_size; |
| 1238 | int toread; |
| 1239 | |
| 1240 | /* Warning: This loop has a very unusual condition and body. |
| 1241 | * The loop needs to go through each page with at least one affected |
| 1242 | * byte. The lowest page number is (start / page_size) since that |
| 1243 | * division rounds down. The highest page number we want is the page |
| 1244 | * where the last byte of the range lives. That last byte has the |
| 1245 | * address (start + len - 1), thus the highest page number is |
| 1246 | * (start + len - 1) / page_size. Since we want to include that last |
| 1247 | * page as well, the loop condition uses <=. |
| 1248 | */ |
| 1249 | for (i = start / page_size; i <= (start + len - 1) / page_size; i++) { |
| 1250 | /* Byte position of the first byte in the range in this page. */ |
| 1251 | /* starthere is an offset to the base address of the chip. */ |
| 1252 | starthere = max(start, i * page_size); |
| 1253 | /* Length of bytes in the range in this page. */ |
| 1254 | lenhere = min(start + len, (i + 1) * page_size) - starthere; |
| 1255 | for (j = 0; j < lenhere; j += chunksize) { |
| 1256 | toread = min(chunksize, lenhere - j); |
| 1257 | rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread); |
| 1258 | if (rc) |
| 1259 | break; |
| 1260 | } |
| 1261 | if (rc) |
| 1262 | break; |
| 1263 | } |
| 1264 | |
| 1265 | return rc; |
| 1266 | } |
| 1267 | |
| 1268 | /* |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 1269 | * Write a part of the flash chip. |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1270 | * FIXME: Use the chunk code from Michael Karcher instead. |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 1271 | * Each page is written separately in chunks with a maximum size of chunksize. |
| 1272 | */ |
| 1273 | int spi_write_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize) |
| 1274 | { |
| 1275 | int rc = 0; |
| 1276 | int i, j, starthere, lenhere; |
| 1277 | /* FIXME: page_size is the wrong variable. We need max_writechunk_size |
| 1278 | * in struct flashchip to do this properly. All chips using |
| 1279 | * spi_chip_write_256 have page_size set to max_writechunk_size, so |
| 1280 | * we're OK for now. |
| 1281 | */ |
| 1282 | int page_size = flash->page_size; |
| 1283 | int towrite; |
| 1284 | |
| 1285 | /* Warning: This loop has a very unusual condition and body. |
| 1286 | * The loop needs to go through each page with at least one affected |
| 1287 | * byte. The lowest page number is (start / page_size) since that |
| 1288 | * division rounds down. The highest page number we want is the page |
| 1289 | * where the last byte of the range lives. That last byte has the |
| 1290 | * address (start + len - 1), thus the highest page number is |
| 1291 | * (start + len - 1) / page_size. Since we want to include that last |
| 1292 | * page as well, the loop condition uses <=. |
| 1293 | */ |
| 1294 | for (i = start / page_size; i <= (start + len - 1) / page_size; i++) { |
| 1295 | /* Byte position of the first byte in the range in this page. */ |
| 1296 | /* starthere is an offset to the base address of the chip. */ |
| 1297 | starthere = max(start, i * page_size); |
| 1298 | /* Length of bytes in the range in this page. */ |
| 1299 | lenhere = min(start + len, (i + 1) * page_size) - starthere; |
| 1300 | for (j = 0; j < lenhere; j += chunksize) { |
| 1301 | towrite = min(chunksize, lenhere - j); |
| 1302 | rc = spi_nbyte_program(starthere + j, buf + starthere - start + j, towrite); |
| 1303 | if (rc) |
| 1304 | break; |
| 1305 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 1306 | programmer_delay(10); |
| 1307 | } |
| 1308 | if (rc) |
| 1309 | break; |
| 1310 | } |
| 1311 | |
| 1312 | return rc; |
| 1313 | } |
| 1314 | |
| 1315 | /* |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1316 | * Program chip using byte programming. (SLOW!) |
| 1317 | * This is for chips which can only handle one byte writes |
| 1318 | * and for chips where memory mapped programming is impossible |
| 1319 | * (e.g. due to size constraints in IT87* for over 512 kB) |
| 1320 | */ |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1321 | /* real chunksize is 1, logical chunksize is 1 */ |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1322 | int spi_chip_write_1(struct flashchip *flash, uint8_t *buf, int start, int len) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1323 | { |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1324 | int i, result = 0; |
| 1325 | |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1326 | for (i = start; i < start + len; i++) { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1327 | result = spi_byte_program(i, buf[i - start]); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1328 | if (result) |
| 1329 | return 1; |
| 1330 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 1331 | programmer_delay(10); |
| 1332 | } |
| 1333 | |
| 1334 | return 0; |
| 1335 | } |
| 1336 | |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1337 | int spi_aai_write(struct flashchip *flash, uint8_t *buf, int start, int len) |
| 1338 | { |
| 1339 | uint32_t pos = start; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1340 | int result; |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1341 | unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = { |
| 1342 | JEDEC_AAI_WORD_PROGRAM, |
| 1343 | }; |
| 1344 | struct spi_command cmds[] = { |
| 1345 | { |
| 1346 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 1347 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 1348 | .readcnt = 0, |
| 1349 | .readarr = NULL, |
| 1350 | }, { |
| 1351 | .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE, |
| 1352 | .writearr = (const unsigned char[]){ |
| 1353 | JEDEC_AAI_WORD_PROGRAM, |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1354 | (start >> 16) & 0xff, |
| 1355 | (start >> 8) & 0xff, |
| 1356 | (start & 0xff), |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1357 | buf[0], |
| 1358 | buf[1] |
| 1359 | }, |
| 1360 | .readcnt = 0, |
| 1361 | .readarr = NULL, |
| 1362 | }, { |
| 1363 | .writecnt = 0, |
| 1364 | .writearr = NULL, |
| 1365 | .readcnt = 0, |
| 1366 | .readarr = NULL, |
| 1367 | }}; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1368 | |
| 1369 | switch (spi_controller) { |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 1370 | #if CONFIG_INTERNAL == 1 |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1371 | #if defined(__i386__) || defined(__x86_64__) |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1372 | case SPI_CONTROLLER_IT87XX: |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1373 | case SPI_CONTROLLER_WBSIO: |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1374 | msg_perr("%s: impossible with this SPI controller," |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1375 | " degrading to byte program\n", __func__); |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1376 | return spi_chip_write_1(flash, buf, start, len); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1377 | #endif |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1378 | #endif |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1379 | default: |
| 1380 | break; |
| 1381 | } |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1382 | |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1383 | /* The even start address and even length requirements can be either |
| 1384 | * honored outside this function, or we can call spi_byte_program |
| 1385 | * for the first and/or last byte and use AAI for the rest. |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1386 | * FIXME: Move this to generic code. |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1387 | */ |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1388 | /* The data sheet requires a start address with the low bit cleared. */ |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1389 | if (start % 2) { |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1390 | msg_cerr("%s: start address not even! Please report a bug at " |
| 1391 | "flashrom@flashrom.org\n", __func__); |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1392 | if (spi_chip_write_1(flash, buf, start, start % 2)) |
| 1393 | return SPI_GENERIC_ERROR; |
| 1394 | pos += start % 2; |
| 1395 | cmds[1].writearr = (const unsigned char[]){ |
| 1396 | JEDEC_AAI_WORD_PROGRAM, |
| 1397 | (pos >> 16) & 0xff, |
| 1398 | (pos >> 8) & 0xff, |
| 1399 | (pos & 0xff), |
| 1400 | buf[pos - start], |
| 1401 | buf[pos - start + 1] |
| 1402 | }; |
| 1403 | /* Do not return an error for now. */ |
| 1404 | //return SPI_GENERIC_ERROR; |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1405 | } |
| 1406 | /* The data sheet requires total AAI write length to be even. */ |
| 1407 | if (len % 2) { |
| 1408 | msg_cerr("%s: total write length not even! Please report a " |
| 1409 | "bug at flashrom@flashrom.org\n", __func__); |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1410 | /* Do not return an error for now. */ |
| 1411 | //return SPI_GENERIC_ERROR; |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1414 | |
| 1415 | result = spi_send_multicommand(cmds); |
| 1416 | if (result) { |
| 1417 | msg_cerr("%s failed during start command execution\n", |
| 1418 | __func__); |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1419 | /* FIXME: Should we send WRDI here as well to make sure the chip |
| 1420 | * is not in AAI mode? |
| 1421 | */ |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1422 | return result; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1423 | } |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1424 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 1425 | programmer_delay(10); |
| 1426 | |
| 1427 | /* We already wrote 2 bytes in the multicommand step. */ |
| 1428 | pos += 2; |
| 1429 | |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1430 | /* Are there at least two more bytes to write? */ |
| 1431 | while (pos < start + len - 1) { |
| 1432 | cmd[1] = buf[pos++ - start]; |
| 1433 | cmd[2] = buf[pos++ - start]; |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1434 | spi_send_command(JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL); |
| 1435 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 1436 | programmer_delay(10); |
| 1437 | } |
| 1438 | |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1439 | /* Use WRDI to exit AAI mode. This needs to be done before issuing any |
| 1440 | * other non-AAI command. |
| 1441 | */ |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1442 | spi_write_disable(); |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1443 | |
| 1444 | /* Write remaining byte (if any). */ |
| 1445 | if (pos < start + len) { |
| 1446 | if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2)) |
| 1447 | return SPI_GENERIC_ERROR; |
| 1448 | pos += pos % 2; |
| 1449 | } |
| 1450 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1451 | return 0; |
| 1452 | } |