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