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