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