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