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