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