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