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