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