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