Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007, 2008, 2009, 2010 Carl-Daniel Hailfinger |
Sean Nelson | 14ba668 | 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" |
| 29 | #include "spi.h" |
| 30 | |
| 31 | void spi_prettyprint_status_register(struct flashchip *flash); |
| 32 | |
| 33 | static int spi_rdid(unsigned char *readarr, int bytes) |
| 34 | { |
| 35 | const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID }; |
| 36 | int ret; |
| 37 | int i; |
| 38 | |
| 39 | ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr); |
| 40 | if (ret) |
| 41 | return ret; |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 42 | msg_cspew("RDID returned"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 43 | for (i = 0; i < bytes; i++) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 44 | msg_cspew(" 0x%02x", readarr[i]); |
| 45 | msg_cspew(". "); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static int spi_rems(unsigned char *readarr) |
| 50 | { |
| 51 | unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 }; |
| 52 | uint32_t readaddr; |
| 53 | int ret; |
| 54 | |
| 55 | ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr); |
| 56 | if (ret == SPI_INVALID_ADDRESS) { |
| 57 | /* Find the lowest even address allowed for reads. */ |
| 58 | readaddr = (spi_get_valid_read_addr() + 1) & ~1; |
| 59 | cmd[1] = (readaddr >> 16) & 0xff, |
| 60 | cmd[2] = (readaddr >> 8) & 0xff, |
| 61 | cmd[3] = (readaddr >> 0) & 0xff, |
| 62 | ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr); |
| 63 | } |
| 64 | if (ret) |
| 65 | return ret; |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 66 | msg_cspew("REMS returned %02x %02x. ", readarr[0], readarr[1]); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int spi_res(unsigned char *readarr) |
| 71 | { |
| 72 | unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 }; |
| 73 | uint32_t readaddr; |
| 74 | int ret; |
| 75 | |
| 76 | ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr); |
| 77 | if (ret == SPI_INVALID_ADDRESS) { |
| 78 | /* Find the lowest even address allowed for reads. */ |
| 79 | readaddr = (spi_get_valid_read_addr() + 1) & ~1; |
| 80 | cmd[1] = (readaddr >> 16) & 0xff, |
| 81 | cmd[2] = (readaddr >> 8) & 0xff, |
| 82 | cmd[3] = (readaddr >> 0) & 0xff, |
| 83 | ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr); |
| 84 | } |
| 85 | if (ret) |
| 86 | return ret; |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 87 | msg_cspew("RES returned %02x. ", readarr[0]); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int spi_write_enable(void) |
| 92 | { |
| 93 | const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN }; |
| 94 | int result; |
| 95 | |
| 96 | /* Send WREN (Write Enable) */ |
| 97 | result = spi_send_command(sizeof(cmd), 0, cmd, NULL); |
| 98 | |
| 99 | if (result) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 100 | msg_cerr("%s failed\n", __func__); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 101 | |
| 102 | return result; |
| 103 | } |
| 104 | |
| 105 | int spi_write_disable(void) |
| 106 | { |
| 107 | const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI }; |
| 108 | |
| 109 | /* Send WRDI (Write Disable) */ |
| 110 | return spi_send_command(sizeof(cmd), 0, cmd, NULL); |
| 111 | } |
| 112 | |
| 113 | static int probe_spi_rdid_generic(struct flashchip *flash, int bytes) |
| 114 | { |
| 115 | unsigned char readarr[4]; |
| 116 | uint32_t id1; |
| 117 | uint32_t id2; |
| 118 | |
| 119 | if (spi_rdid(readarr, bytes)) |
| 120 | return 0; |
| 121 | |
| 122 | if (!oddparity(readarr[0])) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 123 | msg_cdbg("RDID byte 0 parity violation. "); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 124 | |
| 125 | /* Check if this is a continuation vendor ID */ |
| 126 | if (readarr[0] == 0x7f) { |
| 127 | if (!oddparity(readarr[1])) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 128 | msg_cdbg("RDID byte 1 parity violation. "); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 129 | id1 = (readarr[0] << 8) | readarr[1]; |
| 130 | id2 = readarr[2]; |
| 131 | if (bytes > 3) { |
| 132 | id2 <<= 8; |
| 133 | id2 |= readarr[3]; |
| 134 | } |
| 135 | } else { |
| 136 | id1 = readarr[0]; |
| 137 | id2 = (readarr[1] << 8) | readarr[2]; |
| 138 | } |
| 139 | |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 140 | msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 141 | |
| 142 | if (id1 == flash->manufacture_id && id2 == flash->model_id) { |
| 143 | /* Print the status register to tell the |
| 144 | * user about possible write protection. |
| 145 | */ |
| 146 | spi_prettyprint_status_register(flash); |
| 147 | |
| 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | /* Test if this is a pure vendor match. */ |
| 152 | if (id1 == flash->manufacture_id && |
| 153 | GENERIC_DEVICE_ID == flash->model_id) |
| 154 | return 1; |
| 155 | |
| 156 | /* Test if there is any vendor ID. */ |
| 157 | if (GENERIC_MANUF_ID == flash->manufacture_id && |
| 158 | id1 != 0xff) |
| 159 | return 1; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | int probe_spi_rdid(struct flashchip *flash) |
| 165 | { |
| 166 | return probe_spi_rdid_generic(flash, 3); |
| 167 | } |
| 168 | |
| 169 | /* support 4 bytes flash ID */ |
| 170 | int probe_spi_rdid4(struct flashchip *flash) |
| 171 | { |
| 172 | /* only some SPI chipsets support 4 bytes commands */ |
| 173 | switch (spi_controller) { |
| 174 | #if INTERNAL_SUPPORT == 1 |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame^] | 175 | #if defined(__i386__) || defined(__x86_64__) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 176 | case SPI_CONTROLLER_ICH7: |
| 177 | case SPI_CONTROLLER_ICH9: |
| 178 | case SPI_CONTROLLER_VIA: |
| 179 | case SPI_CONTROLLER_SB600: |
| 180 | case SPI_CONTROLLER_WBSIO: |
| 181 | #endif |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame^] | 182 | #endif |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 183 | #if FT2232_SPI_SUPPORT == 1 |
| 184 | case SPI_CONTROLLER_FT2232: |
| 185 | #endif |
| 186 | #if DUMMY_SUPPORT == 1 |
| 187 | case SPI_CONTROLLER_DUMMY: |
| 188 | #endif |
| 189 | #if BUSPIRATE_SPI_SUPPORT == 1 |
| 190 | case SPI_CONTROLLER_BUSPIRATE: |
| 191 | #endif |
| 192 | #if DEDIPROG_SUPPORT == 1 |
| 193 | case SPI_CONTROLLER_DEDIPROG: |
| 194 | #endif |
| 195 | return probe_spi_rdid_generic(flash, 4); |
| 196 | default: |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 197 | msg_cinfo("4b ID not supported on this SPI controller\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | int probe_spi_rems(struct flashchip *flash) |
| 204 | { |
| 205 | unsigned char readarr[JEDEC_REMS_INSIZE]; |
| 206 | uint32_t id1, id2; |
| 207 | |
| 208 | if (spi_rems(readarr)) |
| 209 | return 0; |
| 210 | |
| 211 | id1 = readarr[0]; |
| 212 | id2 = readarr[1]; |
| 213 | |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 214 | msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 215 | |
| 216 | if (id1 == flash->manufacture_id && id2 == flash->model_id) { |
| 217 | /* Print the status register to tell the |
| 218 | * user about possible write protection. |
| 219 | */ |
| 220 | spi_prettyprint_status_register(flash); |
| 221 | |
| 222 | return 1; |
| 223 | } |
| 224 | |
| 225 | /* Test if this is a pure vendor match. */ |
| 226 | if (id1 == flash->manufacture_id && |
| 227 | GENERIC_DEVICE_ID == flash->model_id) |
| 228 | return 1; |
| 229 | |
| 230 | /* Test if there is any vendor ID. */ |
| 231 | if (GENERIC_MANUF_ID == flash->manufacture_id && |
| 232 | id1 != 0xff) |
| 233 | return 1; |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | int probe_spi_res(struct flashchip *flash) |
| 239 | { |
| 240 | unsigned char readarr[3]; |
| 241 | uint32_t id2; |
| 242 | const unsigned char allff[] = {0xff, 0xff, 0xff}; |
| 243 | const unsigned char all00[] = {0x00, 0x00, 0x00}; |
| 244 | |
| 245 | /* Check if RDID is usable and does not return 0xff 0xff 0xff or |
| 246 | * 0x00 0x00 0x00. In that case, RES is pointless. |
| 247 | */ |
| 248 | if (!spi_rdid(readarr, 3) && memcmp(readarr, allff, 3) && |
| 249 | memcmp(readarr, all00, 3)) { |
| 250 | msg_cdbg("Ignoring RES in favour of RDID.\n"); |
| 251 | return 0; |
| 252 | } |
| 253 | /* Check if REMS is usable and does not return 0xff 0xff or |
| 254 | * 0x00 0x00. In that case, RES is pointless. |
| 255 | */ |
| 256 | if (!spi_rems(readarr) && memcmp(readarr, allff, JEDEC_REMS_INSIZE) && |
| 257 | memcmp(readarr, all00, JEDEC_REMS_INSIZE)) { |
| 258 | msg_cdbg("Ignoring RES in favour of REMS.\n"); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | if (spi_res(readarr)) |
| 263 | return 0; |
| 264 | |
| 265 | /* FIXME: Handle the case where RES gives a 2-byte response. */ |
| 266 | id2 = readarr[0]; |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 267 | msg_cdbg("%s: id 0x%x\n", __func__, id2); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 268 | if (id2 != flash->model_id) |
| 269 | return 0; |
| 270 | |
| 271 | /* Print the status register to tell the |
| 272 | * user about possible write protection. |
| 273 | */ |
| 274 | spi_prettyprint_status_register(flash); |
| 275 | return 1; |
| 276 | } |
| 277 | |
| 278 | uint8_t spi_read_status_register(void) |
| 279 | { |
| 280 | const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR }; |
| 281 | /* FIXME: No workarounds for driver/hardware bugs in generic code. */ |
| 282 | unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */ |
| 283 | int ret; |
| 284 | |
| 285 | /* Read Status Register */ |
| 286 | ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr); |
| 287 | if (ret) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 288 | msg_cerr("RDSR failed!\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 289 | |
| 290 | return readarr[0]; |
| 291 | } |
| 292 | |
| 293 | /* Prettyprint the status register. Common definitions. */ |
| 294 | void spi_prettyprint_status_register_common(uint8_t status) |
| 295 | { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 296 | msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is " |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 297 | "%sset\n", (status & (1 << 5)) ? "" : "not "); |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 298 | msg_cdbg("Chip status register: Bit 4 / Block Protect 2 (BP2) is " |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 299 | "%sset\n", (status & (1 << 4)) ? "" : "not "); |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 300 | msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is " |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 301 | "%sset\n", (status & (1 << 3)) ? "" : "not "); |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 302 | msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is " |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 303 | "%sset\n", (status & (1 << 2)) ? "" : "not "); |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 304 | msg_cdbg("Chip status register: Write Enable Latch (WEL) is " |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 305 | "%sset\n", (status & (1 << 1)) ? "" : "not "); |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 306 | msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is " |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 307 | "%sset\n", (status & (1 << 0)) ? "" : "not "); |
| 308 | } |
| 309 | |
| 310 | /* Prettyprint the status register. Works for |
| 311 | * ST M25P series |
| 312 | * MX MX25L series |
| 313 | */ |
| 314 | void spi_prettyprint_status_register_st_m25p(uint8_t status) |
| 315 | { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 316 | msg_cdbg("Chip status register: Status Register Write Disable " |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 317 | "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not "); |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 318 | msg_cdbg("Chip status register: Bit 6 is " |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 319 | "%sset\n", (status & (1 << 6)) ? "" : "not "); |
| 320 | spi_prettyprint_status_register_common(status); |
| 321 | } |
| 322 | |
| 323 | void spi_prettyprint_status_register_sst25(uint8_t status) |
| 324 | { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 325 | msg_cdbg("Chip status register: Block Protect Write Disable " |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 326 | "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not "); |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 327 | msg_cdbg("Chip status register: Auto Address Increment Programming " |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 328 | "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not "); |
| 329 | spi_prettyprint_status_register_common(status); |
| 330 | } |
| 331 | |
| 332 | /* Prettyprint the status register. Works for |
| 333 | * SST 25VF016 |
| 334 | */ |
| 335 | void spi_prettyprint_status_register_sst25vf016(uint8_t status) |
| 336 | { |
| 337 | const char *bpt[] = { |
| 338 | "none", |
| 339 | "1F0000H-1FFFFFH", |
| 340 | "1E0000H-1FFFFFH", |
| 341 | "1C0000H-1FFFFFH", |
| 342 | "180000H-1FFFFFH", |
| 343 | "100000H-1FFFFFH", |
| 344 | "all", "all" |
| 345 | }; |
| 346 | spi_prettyprint_status_register_sst25(status); |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 347 | msg_cdbg("Resulting block protection : %s\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 348 | bpt[(status & 0x1c) >> 2]); |
| 349 | } |
| 350 | |
| 351 | void spi_prettyprint_status_register_sst25vf040b(uint8_t status) |
| 352 | { |
| 353 | const char *bpt[] = { |
| 354 | "none", |
| 355 | "0x70000-0x7ffff", |
| 356 | "0x60000-0x7ffff", |
| 357 | "0x40000-0x7ffff", |
| 358 | "all blocks", "all blocks", "all blocks", "all blocks" |
| 359 | }; |
| 360 | spi_prettyprint_status_register_sst25(status); |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 361 | msg_cdbg("Resulting block protection : %s\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 362 | bpt[(status & 0x1c) >> 2]); |
| 363 | } |
| 364 | |
| 365 | void spi_prettyprint_status_register(struct flashchip *flash) |
| 366 | { |
| 367 | uint8_t status; |
| 368 | |
| 369 | status = spi_read_status_register(); |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 370 | msg_cdbg("Chip status register is %02x\n", status); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 371 | switch (flash->manufacture_id) { |
| 372 | case ST_ID: |
| 373 | if (((flash->model_id & 0xff00) == 0x2000) || |
| 374 | ((flash->model_id & 0xff00) == 0x2500)) |
| 375 | spi_prettyprint_status_register_st_m25p(status); |
| 376 | break; |
| 377 | case MX_ID: |
| 378 | if ((flash->model_id & 0xff00) == 0x2000) |
| 379 | spi_prettyprint_status_register_st_m25p(status); |
| 380 | break; |
| 381 | case SST_ID: |
| 382 | switch (flash->model_id) { |
| 383 | case 0x2541: |
| 384 | spi_prettyprint_status_register_sst25vf016(status); |
| 385 | break; |
| 386 | case 0x8d: |
| 387 | case 0x258d: |
| 388 | spi_prettyprint_status_register_sst25vf040b(status); |
| 389 | break; |
| 390 | default: |
| 391 | spi_prettyprint_status_register_sst25(status); |
| 392 | break; |
| 393 | } |
| 394 | break; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | int spi_chip_erase_60(struct flashchip *flash) |
| 399 | { |
| 400 | int result; |
| 401 | struct spi_command cmds[] = { |
| 402 | { |
| 403 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 404 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 405 | .readcnt = 0, |
| 406 | .readarr = NULL, |
| 407 | }, { |
| 408 | .writecnt = JEDEC_CE_60_OUTSIZE, |
| 409 | .writearr = (const unsigned char[]){ JEDEC_CE_60 }, |
| 410 | .readcnt = 0, |
| 411 | .readarr = NULL, |
| 412 | }, { |
| 413 | .writecnt = 0, |
| 414 | .writearr = NULL, |
| 415 | .readcnt = 0, |
| 416 | .readarr = NULL, |
| 417 | }}; |
| 418 | |
| 419 | result = spi_disable_blockprotect(); |
| 420 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 421 | msg_cerr("spi_disable_blockprotect failed\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 422 | return result; |
| 423 | } |
| 424 | |
| 425 | result = spi_send_multicommand(cmds); |
| 426 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 427 | msg_cerr("%s failed during command execution\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 428 | __func__); |
| 429 | return result; |
| 430 | } |
| 431 | /* Wait until the Write-In-Progress bit is cleared. |
| 432 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 433 | */ |
| 434 | /* FIXME: We assume spi_read_status_register will never fail. */ |
| 435 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 436 | programmer_delay(1000 * 1000); |
| 437 | if (check_erased_range(flash, 0, flash->total_size * 1024)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 438 | msg_cerr("ERASE FAILED!\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 439 | return -1; |
| 440 | } |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | int spi_chip_erase_c7(struct flashchip *flash) |
| 445 | { |
| 446 | int result; |
| 447 | struct spi_command cmds[] = { |
| 448 | { |
| 449 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 450 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 451 | .readcnt = 0, |
| 452 | .readarr = NULL, |
| 453 | }, { |
| 454 | .writecnt = JEDEC_CE_C7_OUTSIZE, |
| 455 | .writearr = (const unsigned char[]){ JEDEC_CE_C7 }, |
| 456 | .readcnt = 0, |
| 457 | .readarr = NULL, |
| 458 | }, { |
| 459 | .writecnt = 0, |
| 460 | .writearr = NULL, |
| 461 | .readcnt = 0, |
| 462 | .readarr = NULL, |
| 463 | }}; |
| 464 | |
| 465 | result = spi_disable_blockprotect(); |
| 466 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 467 | msg_cerr("spi_disable_blockprotect failed\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 468 | return result; |
| 469 | } |
| 470 | |
| 471 | result = spi_send_multicommand(cmds); |
| 472 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 473 | msg_cerr("%s failed during command execution\n", __func__); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 474 | return result; |
| 475 | } |
| 476 | /* Wait until the Write-In-Progress bit is cleared. |
| 477 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 478 | */ |
| 479 | /* FIXME: We assume spi_read_status_register will never fail. */ |
| 480 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 481 | programmer_delay(1000 * 1000); |
| 482 | if (check_erased_range(flash, 0, flash->total_size * 1024)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 483 | msg_cerr("ERASE FAILED!\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 484 | return -1; |
| 485 | } |
| 486 | return 0; |
| 487 | } |
| 488 | |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 489 | int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
| 490 | { |
| 491 | int result; |
| 492 | struct spi_command cmds[] = { |
| 493 | { |
| 494 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 495 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 496 | .readcnt = 0, |
| 497 | .readarr = NULL, |
| 498 | }, { |
| 499 | .writecnt = JEDEC_BE_52_OUTSIZE, |
| 500 | .writearr = (const unsigned char[]){ |
| 501 | JEDEC_BE_52, |
| 502 | (addr >> 16) & 0xff, |
| 503 | (addr >> 8) & 0xff, |
| 504 | (addr & 0xff) |
| 505 | }, |
| 506 | .readcnt = 0, |
| 507 | .readarr = NULL, |
| 508 | }, { |
| 509 | .writecnt = 0, |
| 510 | .writearr = NULL, |
| 511 | .readcnt = 0, |
| 512 | .readarr = NULL, |
| 513 | }}; |
| 514 | |
| 515 | result = spi_send_multicommand(cmds); |
| 516 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 517 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 518 | __func__, addr); |
| 519 | return result; |
| 520 | } |
| 521 | /* Wait until the Write-In-Progress bit is cleared. |
| 522 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 523 | */ |
| 524 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 525 | programmer_delay(100 * 1000); |
| 526 | if (check_erased_range(flash, addr, blocklen)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 527 | msg_cerr("ERASE FAILED!\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 528 | return -1; |
| 529 | } |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | /* Block size is usually |
| 534 | * 64k for Macronix |
| 535 | * 32k for SST |
| 536 | * 4-32k non-uniform for EON |
| 537 | */ |
| 538 | int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
| 539 | { |
| 540 | int result; |
| 541 | struct spi_command cmds[] = { |
| 542 | { |
| 543 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 544 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 545 | .readcnt = 0, |
| 546 | .readarr = NULL, |
| 547 | }, { |
| 548 | .writecnt = JEDEC_BE_D8_OUTSIZE, |
| 549 | .writearr = (const unsigned char[]){ |
| 550 | JEDEC_BE_D8, |
| 551 | (addr >> 16) & 0xff, |
| 552 | (addr >> 8) & 0xff, |
| 553 | (addr & 0xff) |
| 554 | }, |
| 555 | .readcnt = 0, |
| 556 | .readarr = NULL, |
| 557 | }, { |
| 558 | .writecnt = 0, |
| 559 | .writearr = NULL, |
| 560 | .readcnt = 0, |
| 561 | .readarr = NULL, |
| 562 | }}; |
| 563 | |
| 564 | result = spi_send_multicommand(cmds); |
| 565 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 566 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 567 | __func__, addr); |
| 568 | return result; |
| 569 | } |
| 570 | /* Wait until the Write-In-Progress bit is cleared. |
| 571 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 572 | */ |
| 573 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 574 | programmer_delay(100 * 1000); |
| 575 | if (check_erased_range(flash, addr, blocklen)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 576 | msg_cerr("ERASE FAILED!\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 577 | return -1; |
| 578 | } |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | /* Block size is usually |
| 583 | * 4k for PMC |
| 584 | */ |
| 585 | int spi_block_erase_d7(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
| 586 | { |
| 587 | int result; |
| 588 | struct spi_command cmds[] = { |
| 589 | { |
| 590 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 591 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 592 | .readcnt = 0, |
| 593 | .readarr = NULL, |
| 594 | }, { |
| 595 | .writecnt = JEDEC_BE_D7_OUTSIZE, |
| 596 | .writearr = (const unsigned char[]){ |
| 597 | JEDEC_BE_D7, |
| 598 | (addr >> 16) & 0xff, |
| 599 | (addr >> 8) & 0xff, |
| 600 | (addr & 0xff) |
| 601 | }, |
| 602 | .readcnt = 0, |
| 603 | .readarr = NULL, |
| 604 | }, { |
| 605 | .writecnt = 0, |
| 606 | .writearr = NULL, |
| 607 | .readcnt = 0, |
| 608 | .readarr = NULL, |
| 609 | }}; |
| 610 | |
| 611 | result = spi_send_multicommand(cmds); |
| 612 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 613 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 614 | __func__, addr); |
| 615 | return result; |
| 616 | } |
| 617 | /* Wait until the Write-In-Progress bit is cleared. |
| 618 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 619 | */ |
| 620 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 621 | programmer_delay(100 * 1000); |
| 622 | if (check_erased_range(flash, addr, blocklen)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 623 | msg_cerr("ERASE FAILED!\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 624 | return -1; |
| 625 | } |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | int spi_chip_erase_d8(struct flashchip *flash) |
| 630 | { |
| 631 | int i, rc = 0; |
| 632 | int total_size = flash->total_size * 1024; |
| 633 | int erase_size = 64 * 1024; |
| 634 | |
| 635 | spi_disable_blockprotect(); |
| 636 | |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 637 | msg_cinfo("Erasing chip: \n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 638 | |
| 639 | for (i = 0; i < total_size / erase_size; i++) { |
| 640 | rc = spi_block_erase_d8(flash, i * erase_size, erase_size); |
| 641 | if (rc) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 642 | msg_cerr("Error erasing block at 0x%x\n", i); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 643 | break; |
| 644 | } |
| 645 | } |
| 646 | |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 647 | msg_cinfo("\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 648 | |
| 649 | return rc; |
| 650 | } |
| 651 | |
| 652 | /* Sector size is usually 4k, though Macronix eliteflash has 64k */ |
| 653 | int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
| 654 | { |
| 655 | int result; |
| 656 | struct spi_command cmds[] = { |
| 657 | { |
| 658 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 659 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 660 | .readcnt = 0, |
| 661 | .readarr = NULL, |
| 662 | }, { |
| 663 | .writecnt = JEDEC_SE_OUTSIZE, |
| 664 | .writearr = (const unsigned char[]){ |
| 665 | JEDEC_SE, |
| 666 | (addr >> 16) & 0xff, |
| 667 | (addr >> 8) & 0xff, |
| 668 | (addr & 0xff) |
| 669 | }, |
| 670 | .readcnt = 0, |
| 671 | .readarr = NULL, |
| 672 | }, { |
| 673 | .writecnt = 0, |
| 674 | .writearr = NULL, |
| 675 | .readcnt = 0, |
| 676 | .readarr = NULL, |
| 677 | }}; |
| 678 | |
| 679 | result = spi_send_multicommand(cmds); |
| 680 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 681 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 682 | __func__, addr); |
| 683 | return result; |
| 684 | } |
| 685 | /* Wait until the Write-In-Progress bit is cleared. |
| 686 | * This usually takes 15-800 ms, so wait in 10 ms steps. |
| 687 | */ |
| 688 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 689 | programmer_delay(10 * 1000); |
| 690 | if (check_erased_range(flash, addr, blocklen)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 691 | msg_cerr("ERASE FAILED!\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 692 | return -1; |
| 693 | } |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
| 698 | { |
| 699 | if ((addr != 0) || (blocklen != flash->total_size * 1024)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 700 | msg_cerr("%s called with incorrect arguments\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 701 | __func__); |
| 702 | return -1; |
| 703 | } |
| 704 | return spi_chip_erase_60(flash); |
| 705 | } |
| 706 | |
| 707 | int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
| 708 | { |
| 709 | if ((addr != 0) || (blocklen != flash->total_size * 1024)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 710 | msg_cerr("%s called with incorrect arguments\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 711 | __func__); |
| 712 | return -1; |
| 713 | } |
| 714 | return spi_chip_erase_c7(flash); |
| 715 | } |
| 716 | |
| 717 | int spi_write_status_enable(void) |
| 718 | { |
| 719 | const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR }; |
| 720 | int result; |
| 721 | |
| 722 | /* Send EWSR (Enable Write Status Register). */ |
| 723 | result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL); |
| 724 | |
| 725 | if (result) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 726 | msg_cerr("%s failed\n", __func__); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 727 | |
| 728 | return result; |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * This is according the SST25VF016 datasheet, who knows it is more |
| 733 | * generic that this... |
| 734 | */ |
| 735 | int spi_write_status_register(int status) |
| 736 | { |
| 737 | int result; |
| 738 | struct spi_command cmds[] = { |
| 739 | { |
| 740 | /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */ |
| 741 | .writecnt = JEDEC_EWSR_OUTSIZE, |
| 742 | .writearr = (const unsigned char[]){ JEDEC_EWSR }, |
| 743 | .readcnt = 0, |
| 744 | .readarr = NULL, |
| 745 | }, { |
| 746 | .writecnt = JEDEC_WRSR_OUTSIZE, |
| 747 | .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status }, |
| 748 | .readcnt = 0, |
| 749 | .readarr = NULL, |
| 750 | }, { |
| 751 | .writecnt = 0, |
| 752 | .writearr = NULL, |
| 753 | .readcnt = 0, |
| 754 | .readarr = NULL, |
| 755 | }}; |
| 756 | |
| 757 | result = spi_send_multicommand(cmds); |
| 758 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 759 | msg_cerr("%s failed during command execution\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 760 | __func__); |
| 761 | } |
| 762 | return result; |
| 763 | } |
| 764 | |
| 765 | int spi_byte_program(int addr, uint8_t databyte) |
| 766 | { |
| 767 | int result; |
| 768 | struct spi_command cmds[] = { |
| 769 | { |
| 770 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 771 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 772 | .readcnt = 0, |
| 773 | .readarr = NULL, |
| 774 | }, { |
| 775 | .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE, |
| 776 | .writearr = (const unsigned char[]){ |
| 777 | JEDEC_BYTE_PROGRAM, |
| 778 | (addr >> 16) & 0xff, |
| 779 | (addr >> 8) & 0xff, |
| 780 | (addr & 0xff), |
| 781 | databyte |
| 782 | }, |
| 783 | .readcnt = 0, |
| 784 | .readarr = NULL, |
| 785 | }, { |
| 786 | .writecnt = 0, |
| 787 | .writearr = NULL, |
| 788 | .readcnt = 0, |
| 789 | .readarr = NULL, |
| 790 | }}; |
| 791 | |
| 792 | result = spi_send_multicommand(cmds); |
| 793 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 794 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 795 | __func__, addr); |
| 796 | } |
| 797 | return result; |
| 798 | } |
| 799 | |
| 800 | int spi_nbyte_program(int addr, uint8_t *bytes, int len) |
| 801 | { |
| 802 | int result; |
| 803 | /* FIXME: Switch to malloc based on len unless that kills speed. */ |
| 804 | unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = { |
| 805 | JEDEC_BYTE_PROGRAM, |
| 806 | (addr >> 16) & 0xff, |
| 807 | (addr >> 8) & 0xff, |
| 808 | (addr >> 0) & 0xff, |
| 809 | }; |
| 810 | struct spi_command cmds[] = { |
| 811 | { |
| 812 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 813 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 814 | .readcnt = 0, |
| 815 | .readarr = NULL, |
| 816 | }, { |
| 817 | .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len, |
| 818 | .writearr = cmd, |
| 819 | .readcnt = 0, |
| 820 | .readarr = NULL, |
| 821 | }, { |
| 822 | .writecnt = 0, |
| 823 | .writearr = NULL, |
| 824 | .readcnt = 0, |
| 825 | .readarr = NULL, |
| 826 | }}; |
| 827 | |
| 828 | if (!len) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 829 | msg_cerr("%s called for zero-length write\n", __func__); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 830 | return 1; |
| 831 | } |
| 832 | if (len > 256) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 833 | msg_cerr("%s called for too long a write\n", __func__); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 834 | return 1; |
| 835 | } |
| 836 | |
| 837 | memcpy(&cmd[4], bytes, len); |
| 838 | |
| 839 | result = spi_send_multicommand(cmds); |
| 840 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 841 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 842 | __func__, addr); |
| 843 | } |
| 844 | return result; |
| 845 | } |
| 846 | |
| 847 | int spi_disable_blockprotect(void) |
| 848 | { |
| 849 | uint8_t status; |
| 850 | int result; |
| 851 | |
| 852 | status = spi_read_status_register(); |
| 853 | /* If there is block protection in effect, unprotect it first. */ |
| 854 | if ((status & 0x3c) != 0) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 855 | msg_cdbg("Some block protection in effect, disabling\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 856 | result = spi_write_status_register(status & ~0x3c); |
| 857 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 858 | msg_cerr("spi_write_status_register failed\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 859 | return result; |
| 860 | } |
| 861 | } |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | int spi_nbyte_read(int address, uint8_t *bytes, int len) |
| 866 | { |
| 867 | const unsigned char cmd[JEDEC_READ_OUTSIZE] = { |
| 868 | JEDEC_READ, |
| 869 | (address >> 16) & 0xff, |
| 870 | (address >> 8) & 0xff, |
| 871 | (address >> 0) & 0xff, |
| 872 | }; |
| 873 | |
| 874 | /* Send Read */ |
| 875 | return spi_send_command(sizeof(cmd), len, cmd, bytes); |
| 876 | } |
| 877 | |
| 878 | /* |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 879 | * Read a part of the flash chip. |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 880 | * Each page is read separately in chunks with a maximum size of chunksize. |
| 881 | */ |
| 882 | int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize) |
| 883 | { |
| 884 | int rc = 0; |
| 885 | int i, j, starthere, lenhere; |
| 886 | int page_size = flash->page_size; |
| 887 | int toread; |
| 888 | |
| 889 | /* Warning: This loop has a very unusual condition and body. |
| 890 | * The loop needs to go through each page with at least one affected |
| 891 | * byte. The lowest page number is (start / page_size) since that |
| 892 | * division rounds down. The highest page number we want is the page |
| 893 | * where the last byte of the range lives. That last byte has the |
| 894 | * address (start + len - 1), thus the highest page number is |
| 895 | * (start + len - 1) / page_size. Since we want to include that last |
| 896 | * page as well, the loop condition uses <=. |
| 897 | */ |
| 898 | for (i = start / page_size; i <= (start + len - 1) / page_size; i++) { |
| 899 | /* Byte position of the first byte in the range in this page. */ |
| 900 | /* starthere is an offset to the base address of the chip. */ |
| 901 | starthere = max(start, i * page_size); |
| 902 | /* Length of bytes in the range in this page. */ |
| 903 | lenhere = min(start + len, (i + 1) * page_size) - starthere; |
| 904 | for (j = 0; j < lenhere; j += chunksize) { |
| 905 | toread = min(chunksize, lenhere - j); |
| 906 | rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread); |
| 907 | if (rc) |
| 908 | break; |
| 909 | } |
| 910 | if (rc) |
| 911 | break; |
| 912 | } |
| 913 | |
| 914 | return rc; |
| 915 | } |
| 916 | |
| 917 | /* |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 918 | * Write a part of the flash chip. |
| 919 | * Each page is written separately in chunks with a maximum size of chunksize. |
| 920 | */ |
| 921 | int spi_write_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize) |
| 922 | { |
| 923 | int rc = 0; |
| 924 | int i, j, starthere, lenhere; |
| 925 | /* FIXME: page_size is the wrong variable. We need max_writechunk_size |
| 926 | * in struct flashchip to do this properly. All chips using |
| 927 | * spi_chip_write_256 have page_size set to max_writechunk_size, so |
| 928 | * we're OK for now. |
| 929 | */ |
| 930 | int page_size = flash->page_size; |
| 931 | int towrite; |
| 932 | |
| 933 | /* Warning: This loop has a very unusual condition and body. |
| 934 | * The loop needs to go through each page with at least one affected |
| 935 | * byte. The lowest page number is (start / page_size) since that |
| 936 | * division rounds down. The highest page number we want is the page |
| 937 | * where the last byte of the range lives. That last byte has the |
| 938 | * address (start + len - 1), thus the highest page number is |
| 939 | * (start + len - 1) / page_size. Since we want to include that last |
| 940 | * page as well, the loop condition uses <=. |
| 941 | */ |
| 942 | for (i = start / page_size; i <= (start + len - 1) / page_size; i++) { |
| 943 | /* Byte position of the first byte in the range in this page. */ |
| 944 | /* starthere is an offset to the base address of the chip. */ |
| 945 | starthere = max(start, i * page_size); |
| 946 | /* Length of bytes in the range in this page. */ |
| 947 | lenhere = min(start + len, (i + 1) * page_size) - starthere; |
| 948 | for (j = 0; j < lenhere; j += chunksize) { |
| 949 | towrite = min(chunksize, lenhere - j); |
| 950 | rc = spi_nbyte_program(starthere + j, buf + starthere - start + j, towrite); |
| 951 | if (rc) |
| 952 | break; |
| 953 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 954 | programmer_delay(10); |
| 955 | } |
| 956 | if (rc) |
| 957 | break; |
| 958 | } |
| 959 | |
| 960 | return rc; |
| 961 | } |
| 962 | |
| 963 | /* |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 964 | * Program chip using byte programming. (SLOW!) |
| 965 | * This is for chips which can only handle one byte writes |
| 966 | * and for chips where memory mapped programming is impossible |
| 967 | * (e.g. due to size constraints in IT87* for over 512 kB) |
| 968 | */ |
| 969 | int spi_chip_write_1(struct flashchip *flash, uint8_t *buf) |
| 970 | { |
| 971 | int total_size = 1024 * flash->total_size; |
| 972 | int i, result = 0; |
| 973 | |
| 974 | spi_disable_blockprotect(); |
| 975 | /* Erase first */ |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 976 | msg_cinfo("Erasing flash before programming... "); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 977 | if (erase_flash(flash)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 978 | msg_cerr("ERASE FAILED!\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 979 | return -1; |
| 980 | } |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 981 | msg_cinfo("done.\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 982 | for (i = 0; i < total_size; i++) { |
| 983 | result = spi_byte_program(i, buf[i]); |
| 984 | if (result) |
| 985 | return 1; |
| 986 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 987 | programmer_delay(10); |
| 988 | } |
| 989 | |
| 990 | return 0; |
| 991 | } |
| 992 | |
| 993 | int spi_aai_write(struct flashchip *flash, uint8_t *buf) |
| 994 | { |
| 995 | uint32_t pos = 2, size = flash->total_size * 1024; |
| 996 | unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]}; |
| 997 | int result; |
| 998 | |
| 999 | switch (spi_controller) { |
| 1000 | #if INTERNAL_SUPPORT == 1 |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame^] | 1001 | #if defined(__i386__) || defined(__x86_64__) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1002 | case SPI_CONTROLLER_WBSIO: |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 1003 | msg_cerr("%s: impossible with Winbond SPI masters," |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1004 | " degrading to byte program\n", __func__); |
| 1005 | return spi_chip_write_1(flash, buf); |
| 1006 | #endif |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame^] | 1007 | #endif |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1008 | default: |
| 1009 | break; |
| 1010 | } |
| 1011 | if (erase_flash(flash)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 1012 | msg_cerr("ERASE FAILED!\n"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1013 | return -1; |
| 1014 | } |
| 1015 | /* FIXME: This will fail on ICH/VIA SPI. */ |
| 1016 | result = spi_write_enable(); |
| 1017 | if (result) |
| 1018 | return result; |
| 1019 | spi_send_command(6, 0, w, NULL); |
| 1020 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 1021 | programmer_delay(5); /* SST25VF040B Tbp is max 10us */ |
| 1022 | while (pos < size) { |
| 1023 | w[1] = buf[pos++]; |
| 1024 | w[2] = buf[pos++]; |
| 1025 | spi_send_command(3, 0, w, NULL); |
| 1026 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 1027 | programmer_delay(5); /* SST25VF040B Tbp is max 10us */ |
| 1028 | } |
| 1029 | spi_write_disable(); |
| 1030 | return 0; |
| 1031 | } |