Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007, 2008, 2009, 2010 Carl-Daniel Hailfinger |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008 coresystems GmbH |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Contains the common SPI chip driver functions |
| 23 | */ |
| 24 | |
| 25 | #include <string.h> |
| 26 | #include "flash.h" |
| 27 | #include "flashchips.h" |
| 28 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 29 | #include "programmer.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 30 | #include "spi.h" |
| 31 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 32 | static int spi_rdid(struct flashctx *flash, unsigned char *readarr, int bytes) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 33 | { |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 34 | static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID }; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 35 | int ret; |
| 36 | int i; |
| 37 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 38 | ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 39 | if (ret) |
| 40 | return ret; |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 41 | msg_cspew("RDID returned"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 42 | for (i = 0; i < bytes; i++) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 43 | msg_cspew(" 0x%02x", readarr[i]); |
| 44 | msg_cspew(". "); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 48 | static int spi_rems(struct flashctx *flash, unsigned char *readarr) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 49 | { |
| 50 | unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 }; |
| 51 | uint32_t readaddr; |
| 52 | int ret; |
| 53 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 54 | ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd, |
| 55 | readarr); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 56 | if (ret == SPI_INVALID_ADDRESS) { |
| 57 | /* Find the lowest even address allowed for reads. */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 58 | readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 59 | cmd[1] = (readaddr >> 16) & 0xff, |
| 60 | cmd[2] = (readaddr >> 8) & 0xff, |
| 61 | cmd[3] = (readaddr >> 0) & 0xff, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 62 | ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, |
| 63 | cmd, readarr); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 64 | } |
| 65 | if (ret) |
| 66 | return ret; |
Cristian Măgherușan-Stanciu | 9932c7b | 2011-07-07 19:56:58 +0000 | [diff] [blame] | 67 | msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 71 | static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 72 | { |
| 73 | unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 }; |
| 74 | uint32_t readaddr; |
| 75 | int ret; |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 76 | int i; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 77 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 78 | ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 79 | if (ret == SPI_INVALID_ADDRESS) { |
| 80 | /* Find the lowest even address allowed for reads. */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 81 | readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 82 | cmd[1] = (readaddr >> 16) & 0xff, |
| 83 | cmd[2] = (readaddr >> 8) & 0xff, |
| 84 | cmd[3] = (readaddr >> 0) & 0xff, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 85 | ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 86 | } |
| 87 | if (ret) |
| 88 | return ret; |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 89 | msg_cspew("RES returned"); |
| 90 | for (i = 0; i < bytes; i++) |
| 91 | msg_cspew(" 0x%02x", readarr[i]); |
| 92 | msg_cspew(". "); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 96 | int spi_write_enable(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 97 | { |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 98 | static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN }; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 99 | int result; |
| 100 | |
| 101 | /* Send WREN (Write Enable) */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 102 | result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 103 | |
| 104 | if (result) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 105 | msg_cerr("%s failed\n", __func__); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 106 | |
| 107 | return result; |
| 108 | } |
| 109 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 110 | int spi_write_disable(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 111 | { |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 112 | static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI }; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 113 | |
| 114 | /* Send WRDI (Write Disable) */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 115 | return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 118 | static int probe_spi_rdid_generic(struct flashctx *flash, int bytes) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 119 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 120 | const struct flashchip *chip = flash->chip; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 121 | unsigned char readarr[4]; |
| 122 | uint32_t id1; |
| 123 | uint32_t id2; |
| 124 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 125 | if (spi_rdid(flash, readarr, bytes)) { |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 126 | return 0; |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 127 | } |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 128 | |
| 129 | if (!oddparity(readarr[0])) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 130 | msg_cdbg("RDID byte 0 parity violation. "); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 131 | |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 132 | /* Check if this is a continuation vendor ID. |
| 133 | * FIXME: Handle continuation device IDs. |
| 134 | */ |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 135 | if (readarr[0] == 0x7f) { |
| 136 | if (!oddparity(readarr[1])) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 137 | msg_cdbg("RDID byte 1 parity violation. "); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 138 | id1 = (readarr[0] << 8) | readarr[1]; |
| 139 | id2 = readarr[2]; |
| 140 | if (bytes > 3) { |
| 141 | id2 <<= 8; |
| 142 | id2 |= readarr[3]; |
| 143 | } |
| 144 | } else { |
| 145 | id1 = readarr[0]; |
| 146 | id2 = (readarr[1] << 8) | readarr[2]; |
| 147 | } |
| 148 | |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 149 | msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 150 | |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 151 | if (id1 == chip->manufacture_id && id2 == chip->model_id) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 152 | return 1; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 153 | |
| 154 | /* Test if this is a pure vendor match. */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 155 | if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 156 | return 1; |
| 157 | |
| 158 | /* Test if there is any vendor ID. */ |
Urja Rannikko | 0a5f6e4 | 2015-06-22 23:59:15 +0000 | [diff] [blame] | 159 | if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff && id1 != 0x00) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 160 | return 1; |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 165 | int probe_spi_rdid(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 166 | { |
| 167 | return probe_spi_rdid_generic(flash, 3); |
| 168 | } |
| 169 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 170 | int probe_spi_rdid4(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 171 | { |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 172 | /* Some SPI controllers do not support commands with writecnt=1 and |
| 173 | * readcnt=4. |
| 174 | */ |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 175 | switch (flash->mst->spi.type) { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 176 | #if CONFIG_INTERNAL == 1 |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 177 | #if defined(__i386__) || defined(__x86_64__) |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 178 | case SPI_CONTROLLER_IT87XX: |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 179 | case SPI_CONTROLLER_WBSIO: |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 180 | msg_cinfo("4 byte RDID not supported on this SPI controller\n"); |
| 181 | return 0; |
| 182 | break; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 183 | #endif |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 184 | #endif |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 185 | default: |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 186 | return probe_spi_rdid_generic(flash, 4); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 192 | int probe_spi_rems(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 193 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 194 | const struct flashchip *chip = flash->chip; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 195 | unsigned char readarr[JEDEC_REMS_INSIZE]; |
| 196 | uint32_t id1, id2; |
| 197 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 198 | if (spi_rems(flash, readarr)) { |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 199 | return 0; |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 200 | } |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 201 | |
| 202 | id1 = readarr[0]; |
| 203 | id2 = readarr[1]; |
| 204 | |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 205 | 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] | 206 | |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 207 | if (id1 == chip->manufacture_id && id2 == chip->model_id) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 208 | return 1; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 209 | |
| 210 | /* Test if this is a pure vendor match. */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 211 | if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 212 | return 1; |
| 213 | |
| 214 | /* Test if there is any vendor ID. */ |
Urja Rannikko | 0a5f6e4 | 2015-06-22 23:59:15 +0000 | [diff] [blame] | 215 | if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff && id1 != 0x00) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 216 | return 1; |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 221 | int probe_spi_res1(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 222 | { |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 223 | static const unsigned char allff[] = {0xff, 0xff, 0xff}; |
| 224 | static const unsigned char all00[] = {0x00, 0x00, 0x00}; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 225 | unsigned char readarr[3]; |
| 226 | uint32_t id2; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 227 | |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 228 | /* We only want one-byte RES if RDID and REMS are unusable. */ |
| 229 | |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 230 | /* Check if RDID is usable and does not return 0xff 0xff 0xff or |
| 231 | * 0x00 0x00 0x00. In that case, RES is pointless. |
| 232 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 233 | if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) && |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 234 | memcmp(readarr, all00, 3)) { |
| 235 | msg_cdbg("Ignoring RES in favour of RDID.\n"); |
| 236 | return 0; |
| 237 | } |
| 238 | /* Check if REMS is usable and does not return 0xff 0xff or |
| 239 | * 0x00 0x00. In that case, RES is pointless. |
| 240 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 241 | if (!spi_rems(flash, readarr) && |
| 242 | memcmp(readarr, allff, JEDEC_REMS_INSIZE) && |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 243 | memcmp(readarr, all00, JEDEC_REMS_INSIZE)) { |
| 244 | msg_cdbg("Ignoring RES in favour of REMS.\n"); |
| 245 | return 0; |
| 246 | } |
| 247 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 248 | if (spi_res(flash, readarr, 1)) { |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 249 | return 0; |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 250 | } |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 251 | |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 252 | id2 = readarr[0]; |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 253 | |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 254 | msg_cdbg("%s: id 0x%x\n", __func__, id2); |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 255 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 256 | if (id2 != flash->chip->model_id) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 257 | return 0; |
| 258 | |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 259 | return 1; |
| 260 | } |
| 261 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 262 | int probe_spi_res2(struct flashctx *flash) |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 263 | { |
| 264 | unsigned char readarr[2]; |
| 265 | uint32_t id1, id2; |
| 266 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 267 | if (spi_res(flash, readarr, 2)) { |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 268 | return 0; |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 269 | } |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 270 | |
| 271 | id1 = readarr[0]; |
| 272 | id2 = readarr[1]; |
| 273 | |
| 274 | msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2); |
| 275 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 276 | if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id) |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 277 | return 0; |
| 278 | |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 279 | return 1; |
| 280 | } |
| 281 | |
Stefan Tauner | 3f5e35d | 2013-04-19 01:58:33 +0000 | [diff] [blame] | 282 | int probe_spi_res3(struct flashctx *flash) |
| 283 | { |
| 284 | unsigned char readarr[3]; |
| 285 | uint32_t id1, id2; |
| 286 | |
| 287 | if (spi_res(flash, readarr, 3)) { |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | id1 = (readarr[0] << 8) | readarr[1]; |
| 292 | id2 = readarr[2]; |
| 293 | |
| 294 | msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2); |
| 295 | |
| 296 | if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id) |
| 297 | return 0; |
| 298 | |
| 299 | return 1; |
| 300 | } |
| 301 | |
Stefan Tauner | 57794ac | 2012-12-29 15:04:20 +0000 | [diff] [blame] | 302 | /* Only used for some Atmel chips. */ |
| 303 | int probe_spi_at25f(struct flashctx *flash) |
| 304 | { |
| 305 | static const unsigned char cmd[AT25F_RDID_OUTSIZE] = { AT25F_RDID }; |
| 306 | unsigned char readarr[AT25F_RDID_INSIZE]; |
| 307 | uint32_t id1; |
| 308 | uint32_t id2; |
| 309 | |
| 310 | if (spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr)) |
| 311 | return 0; |
| 312 | |
| 313 | id1 = readarr[0]; |
| 314 | id2 = readarr[1]; |
| 315 | |
| 316 | msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2); |
| 317 | |
| 318 | if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id) |
| 319 | return 1; |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 324 | int spi_chip_erase_60(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 325 | { |
| 326 | int result; |
| 327 | struct spi_command cmds[] = { |
| 328 | { |
| 329 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 330 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 331 | .readcnt = 0, |
| 332 | .readarr = NULL, |
| 333 | }, { |
| 334 | .writecnt = JEDEC_CE_60_OUTSIZE, |
| 335 | .writearr = (const unsigned char[]){ JEDEC_CE_60 }, |
| 336 | .readcnt = 0, |
| 337 | .readarr = NULL, |
| 338 | }, { |
| 339 | .writecnt = 0, |
| 340 | .writearr = NULL, |
| 341 | .readcnt = 0, |
| 342 | .readarr = NULL, |
| 343 | }}; |
| 344 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 345 | result = spi_send_multicommand(flash, cmds); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 346 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 347 | msg_cerr("%s failed during command execution\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 348 | __func__); |
| 349 | return result; |
| 350 | } |
| 351 | /* Wait until the Write-In-Progress bit is cleared. |
| 352 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 353 | */ |
| 354 | /* FIXME: We assume spi_read_status_register will never fail. */ |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 355 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 356 | programmer_delay(1000 * 1000); |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 357 | /* FIXME: Check the status register for errors. */ |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
Stefan Tauner | 3c0fcd0 | 2012-09-21 12:46:56 +0000 | [diff] [blame] | 361 | int spi_chip_erase_62(struct flashctx *flash) |
| 362 | { |
| 363 | int result; |
| 364 | struct spi_command cmds[] = { |
| 365 | { |
| 366 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 367 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 368 | .readcnt = 0, |
| 369 | .readarr = NULL, |
| 370 | }, { |
| 371 | .writecnt = JEDEC_CE_62_OUTSIZE, |
| 372 | .writearr = (const unsigned char[]){ JEDEC_CE_62 }, |
| 373 | .readcnt = 0, |
| 374 | .readarr = NULL, |
| 375 | }, { |
| 376 | .writecnt = 0, |
| 377 | .writearr = NULL, |
| 378 | .readcnt = 0, |
| 379 | .readarr = NULL, |
| 380 | }}; |
| 381 | |
| 382 | result = spi_send_multicommand(flash, cmds); |
| 383 | if (result) { |
| 384 | msg_cerr("%s failed during command execution\n", |
| 385 | __func__); |
| 386 | return result; |
| 387 | } |
| 388 | /* Wait until the Write-In-Progress bit is cleared. |
| 389 | * This usually takes 2-5 s, so wait in 100 ms steps. |
| 390 | */ |
| 391 | /* FIXME: We assume spi_read_status_register will never fail. */ |
| 392 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
| 393 | programmer_delay(100 * 1000); |
| 394 | /* FIXME: Check the status register for errors. */ |
| 395 | return 0; |
| 396 | } |
| 397 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 398 | int spi_chip_erase_c7(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 399 | { |
| 400 | int result; |
| 401 | struct spi_command cmds[] = { |
| 402 | { |
| 403 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 404 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 405 | .readcnt = 0, |
| 406 | .readarr = NULL, |
| 407 | }, { |
| 408 | .writecnt = JEDEC_CE_C7_OUTSIZE, |
| 409 | .writearr = (const unsigned char[]){ JEDEC_CE_C7 }, |
| 410 | .readcnt = 0, |
| 411 | .readarr = NULL, |
| 412 | }, { |
| 413 | .writecnt = 0, |
| 414 | .writearr = NULL, |
| 415 | .readcnt = 0, |
| 416 | .readarr = NULL, |
| 417 | }}; |
| 418 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 419 | result = spi_send_multicommand(flash, cmds); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 420 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 421 | msg_cerr("%s failed during command execution\n", __func__); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 422 | return result; |
| 423 | } |
| 424 | /* Wait until the Write-In-Progress bit is cleared. |
| 425 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 426 | */ |
| 427 | /* FIXME: We assume spi_read_status_register will never fail. */ |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 428 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 429 | programmer_delay(1000 * 1000); |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 430 | /* FIXME: Check the status register for errors. */ |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 434 | int spi_block_erase_52(struct flashctx *flash, unsigned int addr, |
| 435 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 436 | { |
| 437 | int result; |
| 438 | struct spi_command cmds[] = { |
| 439 | { |
| 440 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 441 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 442 | .readcnt = 0, |
| 443 | .readarr = NULL, |
| 444 | }, { |
| 445 | .writecnt = JEDEC_BE_52_OUTSIZE, |
| 446 | .writearr = (const unsigned char[]){ |
| 447 | JEDEC_BE_52, |
| 448 | (addr >> 16) & 0xff, |
| 449 | (addr >> 8) & 0xff, |
| 450 | (addr & 0xff) |
| 451 | }, |
| 452 | .readcnt = 0, |
| 453 | .readarr = NULL, |
| 454 | }, { |
| 455 | .writecnt = 0, |
| 456 | .writearr = NULL, |
| 457 | .readcnt = 0, |
| 458 | .readarr = NULL, |
| 459 | }}; |
| 460 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 461 | result = spi_send_multicommand(flash, cmds); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 462 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 463 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 464 | __func__, addr); |
| 465 | return result; |
| 466 | } |
| 467 | /* Wait until the Write-In-Progress bit is cleared. |
| 468 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 469 | */ |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 470 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 471 | programmer_delay(100 * 1000); |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 472 | /* FIXME: Check the status register for errors. */ |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | /* Block size is usually |
Nikolay Nikolaev | 6f59b0b | 2013-06-28 21:29:51 +0000 | [diff] [blame] | 477 | * 32M (one die) for Micron |
| 478 | */ |
| 479 | int spi_block_erase_c4(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 480 | { |
| 481 | int result; |
| 482 | struct spi_command cmds[] = { |
| 483 | { |
| 484 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 485 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 486 | .readcnt = 0, |
| 487 | .readarr = NULL, |
| 488 | }, { |
| 489 | .writecnt = JEDEC_BE_C4_OUTSIZE, |
| 490 | .writearr = (const unsigned char[]){ |
| 491 | JEDEC_BE_C4, |
| 492 | (addr >> 16) & 0xff, |
| 493 | (addr >> 8) & 0xff, |
| 494 | (addr & 0xff) |
| 495 | }, |
| 496 | .readcnt = 0, |
| 497 | .readarr = NULL, |
| 498 | }, { |
| 499 | .writecnt = 0, |
| 500 | .writearr = NULL, |
| 501 | .readcnt = 0, |
| 502 | .readarr = NULL, |
| 503 | }}; |
| 504 | |
| 505 | result = spi_send_multicommand(flash, cmds); |
| 506 | if (result) { |
| 507 | msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr); |
| 508 | return result; |
| 509 | } |
| 510 | /* Wait until the Write-In-Progress bit is cleared. |
| 511 | * This usually takes 240-480 s, so wait in 500 ms steps. |
| 512 | */ |
| 513 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
| 514 | programmer_delay(500 * 1000 * 1000); |
| 515 | /* FIXME: Check the status register for errors. */ |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | /* Block size is usually |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 520 | * 64k for Macronix |
| 521 | * 32k for SST |
| 522 | * 4-32k non-uniform for EON |
| 523 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 524 | int spi_block_erase_d8(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_D8_OUTSIZE, |
| 536 | .writearr = (const unsigned char[]){ |
| 537 | JEDEC_BE_D8, |
| 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 | */ |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 560 | while (spi_read_status_register(flash) & SPI_SR_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 | * 4k for PMC |
| 568 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 569 | int spi_block_erase_d7(struct flashctx *flash, unsigned int addr, |
| 570 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 571 | { |
| 572 | int result; |
| 573 | struct spi_command cmds[] = { |
| 574 | { |
| 575 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 576 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 577 | .readcnt = 0, |
| 578 | .readarr = NULL, |
| 579 | }, { |
| 580 | .writecnt = JEDEC_BE_D7_OUTSIZE, |
| 581 | .writearr = (const unsigned char[]){ |
| 582 | JEDEC_BE_D7, |
| 583 | (addr >> 16) & 0xff, |
| 584 | (addr >> 8) & 0xff, |
| 585 | (addr & 0xff) |
| 586 | }, |
| 587 | .readcnt = 0, |
| 588 | .readarr = NULL, |
| 589 | }, { |
| 590 | .writecnt = 0, |
| 591 | .writearr = NULL, |
| 592 | .readcnt = 0, |
| 593 | .readarr = NULL, |
| 594 | }}; |
| 595 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 596 | result = spi_send_multicommand(flash, cmds); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 597 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 598 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 599 | __func__, addr); |
| 600 | return result; |
| 601 | } |
| 602 | /* Wait until the Write-In-Progress bit is cleared. |
| 603 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 604 | */ |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 605 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 606 | programmer_delay(100 * 1000); |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 607 | /* FIXME: Check the status register for errors. */ |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 608 | return 0; |
| 609 | } |
| 610 | |
Nikolay Nikolaev | 579f1e0 | 2013-06-28 21:28:37 +0000 | [diff] [blame] | 611 | /* Page erase (usually 256B blocks) */ |
| 612 | int spi_block_erase_db(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 613 | { |
| 614 | int result; |
| 615 | struct spi_command cmds[] = { |
| 616 | { |
| 617 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 618 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 619 | .readcnt = 0, |
| 620 | .readarr = NULL, |
| 621 | }, { |
| 622 | .writecnt = JEDEC_PE_OUTSIZE, |
| 623 | .writearr = (const unsigned char[]){ |
| 624 | JEDEC_PE, |
| 625 | (addr >> 16) & 0xff, |
| 626 | (addr >> 8) & 0xff, |
| 627 | (addr & 0xff) |
| 628 | }, |
| 629 | .readcnt = 0, |
| 630 | .readarr = NULL, |
| 631 | }, { |
| 632 | .writecnt = 0, |
| 633 | .writearr = NULL, |
| 634 | .readcnt = 0, |
| 635 | .readarr = NULL, |
| 636 | } }; |
| 637 | |
| 638 | result = spi_send_multicommand(flash, cmds); |
| 639 | if (result) { |
| 640 | msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr); |
| 641 | return result; |
| 642 | } |
| 643 | |
| 644 | /* Wait until the Write-In-Progress bit is cleared. |
| 645 | * This takes up to 20 ms usually (on worn out devices up to the 0.5s range), so wait in 1 ms steps. */ |
| 646 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
| 647 | programmer_delay(1 * 1000); |
| 648 | /* FIXME: Check the status register for errors. */ |
| 649 | return 0; |
| 650 | } |
| 651 | |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 652 | /* Sector size is usually 4k, though Macronix eliteflash has 64k */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 653 | int spi_block_erase_20(struct flashctx *flash, unsigned int addr, |
| 654 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 655 | { |
| 656 | int result; |
| 657 | struct spi_command cmds[] = { |
| 658 | { |
| 659 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 660 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 661 | .readcnt = 0, |
| 662 | .readarr = NULL, |
| 663 | }, { |
| 664 | .writecnt = JEDEC_SE_OUTSIZE, |
| 665 | .writearr = (const unsigned char[]){ |
| 666 | JEDEC_SE, |
| 667 | (addr >> 16) & 0xff, |
| 668 | (addr >> 8) & 0xff, |
| 669 | (addr & 0xff) |
| 670 | }, |
| 671 | .readcnt = 0, |
| 672 | .readarr = NULL, |
| 673 | }, { |
| 674 | .writecnt = 0, |
| 675 | .writearr = NULL, |
| 676 | .readcnt = 0, |
| 677 | .readarr = NULL, |
| 678 | }}; |
| 679 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 680 | result = spi_send_multicommand(flash, cmds); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 681 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 682 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 683 | __func__, addr); |
| 684 | return result; |
| 685 | } |
| 686 | /* Wait until the Write-In-Progress bit is cleared. |
| 687 | * This usually takes 15-800 ms, so wait in 10 ms steps. |
| 688 | */ |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 689 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 690 | programmer_delay(10 * 1000); |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 691 | /* FIXME: Check the status register for errors. */ |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 692 | return 0; |
| 693 | } |
| 694 | |
Stefan Tauner | 94b39b4 | 2012-10-27 00:06:02 +0000 | [diff] [blame] | 695 | int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 696 | { |
| 697 | int result; |
| 698 | struct spi_command cmds[] = { |
| 699 | { |
| 700 | /* .writecnt = JEDEC_WREN_OUTSIZE, |
| 701 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 702 | .readcnt = 0, |
| 703 | .readarr = NULL, |
| 704 | }, { */ |
| 705 | .writecnt = JEDEC_BE_50_OUTSIZE, |
| 706 | .writearr = (const unsigned char[]){ |
| 707 | JEDEC_BE_50, |
| 708 | (addr >> 16) & 0xff, |
| 709 | (addr >> 8) & 0xff, |
| 710 | (addr & 0xff) |
| 711 | }, |
| 712 | .readcnt = 0, |
| 713 | .readarr = NULL, |
| 714 | }, { |
| 715 | .writecnt = 0, |
| 716 | .writearr = NULL, |
| 717 | .readcnt = 0, |
| 718 | .readarr = NULL, |
| 719 | }}; |
| 720 | |
| 721 | result = spi_send_multicommand(flash, cmds); |
| 722 | if (result) { |
| 723 | msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr); |
| 724 | return result; |
| 725 | } |
| 726 | /* Wait until the Write-In-Progress bit is cleared. |
| 727 | * This usually takes 10 ms, so wait in 1 ms steps. |
| 728 | */ |
| 729 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
| 730 | programmer_delay(1 * 1000); |
| 731 | /* FIXME: Check the status register for errors. */ |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 736 | { |
| 737 | int result; |
| 738 | struct spi_command cmds[] = { |
| 739 | { |
| 740 | /* .writecnt = JEDEC_WREN_OUTSIZE, |
| 741 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 742 | .readcnt = 0, |
| 743 | .readarr = NULL, |
| 744 | }, { */ |
| 745 | .writecnt = JEDEC_BE_81_OUTSIZE, |
| 746 | .writearr = (const unsigned char[]){ |
| 747 | JEDEC_BE_81, |
| 748 | (addr >> 16) & 0xff, |
| 749 | (addr >> 8) & 0xff, |
| 750 | (addr & 0xff) |
| 751 | }, |
| 752 | .readcnt = 0, |
| 753 | .readarr = NULL, |
| 754 | }, { |
| 755 | .writecnt = 0, |
| 756 | .writearr = NULL, |
| 757 | .readcnt = 0, |
| 758 | .readarr = NULL, |
| 759 | }}; |
| 760 | |
| 761 | result = spi_send_multicommand(flash, cmds); |
| 762 | if (result) { |
| 763 | msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr); |
| 764 | return result; |
| 765 | } |
| 766 | /* Wait until the Write-In-Progress bit is cleared. |
| 767 | * This usually takes 8 ms, so wait in 1 ms steps. |
| 768 | */ |
| 769 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
| 770 | programmer_delay(1 * 1000); |
| 771 | /* FIXME: Check the status register for errors. */ |
| 772 | return 0; |
| 773 | } |
| 774 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 775 | int spi_block_erase_60(struct flashctx *flash, unsigned int addr, |
| 776 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 777 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 778 | if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 779 | msg_cerr("%s called with incorrect arguments\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 780 | __func__); |
| 781 | return -1; |
| 782 | } |
| 783 | return spi_chip_erase_60(flash); |
| 784 | } |
| 785 | |
Stefan Tauner | 3c0fcd0 | 2012-09-21 12:46:56 +0000 | [diff] [blame] | 786 | int spi_block_erase_62(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 787 | { |
| 788 | if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) { |
| 789 | msg_cerr("%s called with incorrect arguments\n", |
| 790 | __func__); |
| 791 | return -1; |
| 792 | } |
| 793 | return spi_chip_erase_62(flash); |
| 794 | } |
| 795 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 796 | int spi_block_erase_c7(struct flashctx *flash, unsigned int addr, |
| 797 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 798 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 799 | if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 800 | msg_cerr("%s called with incorrect arguments\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 801 | __func__); |
| 802 | return -1; |
| 803 | } |
| 804 | return spi_chip_erase_c7(flash); |
| 805 | } |
| 806 | |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 807 | erasefunc_t *spi_get_erasefn_from_opcode(uint8_t opcode) |
| 808 | { |
| 809 | switch(opcode){ |
| 810 | case 0xff: |
| 811 | case 0x00: |
| 812 | /* Not specified, assuming "not supported". */ |
| 813 | return NULL; |
| 814 | case 0x20: |
| 815 | return &spi_block_erase_20; |
Stefan Tauner | 730e7e7 | 2013-05-01 14:04:19 +0000 | [diff] [blame] | 816 | case 0x50: |
| 817 | return &spi_block_erase_50; |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 818 | case 0x52: |
| 819 | return &spi_block_erase_52; |
| 820 | case 0x60: |
| 821 | return &spi_block_erase_60; |
Stefan Tauner | 730e7e7 | 2013-05-01 14:04:19 +0000 | [diff] [blame] | 822 | case 0x62: |
| 823 | return &spi_block_erase_62; |
| 824 | case 0x81: |
| 825 | return &spi_block_erase_81; |
Nikolay Nikolaev | 6f59b0b | 2013-06-28 21:29:51 +0000 | [diff] [blame] | 826 | case 0xc4: |
| 827 | return &spi_block_erase_c4; |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 828 | case 0xc7: |
| 829 | return &spi_block_erase_c7; |
| 830 | case 0xd7: |
| 831 | return &spi_block_erase_d7; |
| 832 | case 0xd8: |
| 833 | return &spi_block_erase_d8; |
Nikolay Nikolaev | 579f1e0 | 2013-06-28 21:28:37 +0000 | [diff] [blame] | 834 | case 0xdb: |
| 835 | return &spi_block_erase_db; |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 836 | default: |
| 837 | msg_cinfo("%s: unknown erase opcode (0x%02x). Please report " |
| 838 | "this at flashrom@flashrom.org\n", __func__, opcode); |
| 839 | return NULL; |
| 840 | } |
| 841 | } |
| 842 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 843 | int spi_byte_program(struct flashctx *flash, unsigned int addr, |
| 844 | uint8_t databyte) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 845 | { |
| 846 | int result; |
| 847 | struct spi_command cmds[] = { |
| 848 | { |
| 849 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 850 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 851 | .readcnt = 0, |
| 852 | .readarr = NULL, |
| 853 | }, { |
| 854 | .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE, |
| 855 | .writearr = (const unsigned char[]){ |
| 856 | JEDEC_BYTE_PROGRAM, |
| 857 | (addr >> 16) & 0xff, |
| 858 | (addr >> 8) & 0xff, |
| 859 | (addr & 0xff), |
| 860 | databyte |
| 861 | }, |
| 862 | .readcnt = 0, |
| 863 | .readarr = NULL, |
| 864 | }, { |
| 865 | .writecnt = 0, |
| 866 | .writearr = NULL, |
| 867 | .readcnt = 0, |
| 868 | .readarr = NULL, |
| 869 | }}; |
| 870 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 871 | result = spi_send_multicommand(flash, cmds); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 872 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 873 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 874 | __func__, addr); |
| 875 | } |
| 876 | return result; |
| 877 | } |
| 878 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 879 | int spi_nbyte_program(struct flashctx *flash, unsigned int addr, const uint8_t *bytes, unsigned int len) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 880 | { |
| 881 | int result; |
| 882 | /* FIXME: Switch to malloc based on len unless that kills speed. */ |
| 883 | unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = { |
| 884 | JEDEC_BYTE_PROGRAM, |
| 885 | (addr >> 16) & 0xff, |
| 886 | (addr >> 8) & 0xff, |
| 887 | (addr >> 0) & 0xff, |
| 888 | }; |
| 889 | struct spi_command cmds[] = { |
| 890 | { |
| 891 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 892 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 893 | .readcnt = 0, |
| 894 | .readarr = NULL, |
| 895 | }, { |
| 896 | .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len, |
| 897 | .writearr = cmd, |
| 898 | .readcnt = 0, |
| 899 | .readarr = NULL, |
| 900 | }, { |
| 901 | .writecnt = 0, |
| 902 | .writearr = NULL, |
| 903 | .readcnt = 0, |
| 904 | .readarr = NULL, |
| 905 | }}; |
| 906 | |
| 907 | if (!len) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 908 | msg_cerr("%s called for zero-length write\n", __func__); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 909 | return 1; |
| 910 | } |
| 911 | if (len > 256) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 912 | msg_cerr("%s called for too long a write\n", __func__); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 913 | return 1; |
| 914 | } |
| 915 | |
| 916 | memcpy(&cmd[4], bytes, len); |
| 917 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 918 | result = spi_send_multicommand(flash, cmds); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 919 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 920 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 921 | __func__, addr); |
| 922 | } |
| 923 | return result; |
| 924 | } |
| 925 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 926 | int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes, |
| 927 | unsigned int len) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 928 | { |
| 929 | const unsigned char cmd[JEDEC_READ_OUTSIZE] = { |
| 930 | JEDEC_READ, |
| 931 | (address >> 16) & 0xff, |
| 932 | (address >> 8) & 0xff, |
| 933 | (address >> 0) & 0xff, |
| 934 | }; |
| 935 | |
| 936 | /* Send Read */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 937 | return spi_send_command(flash, sizeof(cmd), len, cmd, bytes); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | /* |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 941 | * Read a part of the flash chip. |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 942 | * FIXME: Use the chunk code from Michael Karcher instead. |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 943 | * Each page is read separately in chunks with a maximum size of chunksize. |
| 944 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 945 | int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 946 | unsigned int len, unsigned int chunksize) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 947 | { |
| 948 | int rc = 0; |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 949 | unsigned int i, j, starthere, lenhere, toread; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 950 | unsigned int page_size = flash->chip->page_size; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 951 | |
| 952 | /* Warning: This loop has a very unusual condition and body. |
| 953 | * The loop needs to go through each page with at least one affected |
| 954 | * byte. The lowest page number is (start / page_size) since that |
| 955 | * division rounds down. The highest page number we want is the page |
| 956 | * where the last byte of the range lives. That last byte has the |
| 957 | * address (start + len - 1), thus the highest page number is |
| 958 | * (start + len - 1) / page_size. Since we want to include that last |
| 959 | * page as well, the loop condition uses <=. |
| 960 | */ |
| 961 | for (i = start / page_size; i <= (start + len - 1) / page_size; i++) { |
| 962 | /* Byte position of the first byte in the range in this page. */ |
| 963 | /* starthere is an offset to the base address of the chip. */ |
| 964 | starthere = max(start, i * page_size); |
| 965 | /* Length of bytes in the range in this page. */ |
| 966 | lenhere = min(start + len, (i + 1) * page_size) - starthere; |
| 967 | for (j = 0; j < lenhere; j += chunksize) { |
| 968 | toread = min(chunksize, lenhere - j); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 969 | rc = spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 970 | if (rc) |
| 971 | break; |
| 972 | } |
| 973 | if (rc) |
| 974 | break; |
| 975 | } |
| 976 | |
| 977 | return rc; |
| 978 | } |
| 979 | |
| 980 | /* |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 981 | * Write a part of the flash chip. |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 982 | * FIXME: Use the chunk code from Michael Karcher instead. |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 983 | * Each page is written separately in chunks with a maximum size of chunksize. |
| 984 | */ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 985 | int spi_write_chunked(struct flashctx *flash, const uint8_t *buf, unsigned int start, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 986 | unsigned int len, unsigned int chunksize) |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 987 | { |
| 988 | int rc = 0; |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 989 | unsigned int i, j, starthere, lenhere, towrite; |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 990 | /* 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] | 991 | * in struct flashctx to do this properly. All chips using |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 992 | * spi_chip_write_256 have page_size set to max_writechunk_size, so |
| 993 | * we're OK for now. |
| 994 | */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 995 | unsigned int page_size = flash->chip->page_size; |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 996 | |
| 997 | /* Warning: This loop has a very unusual condition and body. |
| 998 | * The loop needs to go through each page with at least one affected |
| 999 | * byte. The lowest page number is (start / page_size) since that |
| 1000 | * division rounds down. The highest page number we want is the page |
| 1001 | * where the last byte of the range lives. That last byte has the |
| 1002 | * address (start + len - 1), thus the highest page number is |
| 1003 | * (start + len - 1) / page_size. Since we want to include that last |
| 1004 | * page as well, the loop condition uses <=. |
| 1005 | */ |
| 1006 | for (i = start / page_size; i <= (start + len - 1) / page_size; i++) { |
| 1007 | /* Byte position of the first byte in the range in this page. */ |
| 1008 | /* starthere is an offset to the base address of the chip. */ |
| 1009 | starthere = max(start, i * page_size); |
| 1010 | /* Length of bytes in the range in this page. */ |
| 1011 | lenhere = min(start + len, (i + 1) * page_size) - starthere; |
| 1012 | for (j = 0; j < lenhere; j += chunksize) { |
| 1013 | towrite = min(chunksize, lenhere - j); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 1014 | 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] | 1015 | if (rc) |
| 1016 | break; |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 1017 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 1018 | programmer_delay(10); |
| 1019 | } |
| 1020 | if (rc) |
| 1021 | break; |
| 1022 | } |
| 1023 | |
| 1024 | return rc; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1028 | * Program chip using byte programming. (SLOW!) |
| 1029 | * This is for chips which can only handle one byte writes |
| 1030 | * and for chips where memory mapped programming is impossible |
| 1031 | * (e.g. due to size constraints in IT87* for over 512 kB) |
| 1032 | */ |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 1033 | /* real chunksize is 1, logical chunksize is 1 */ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 1034 | int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1035 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 1036 | unsigned int i; |
| 1037 | int result = 0; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1038 | |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 1039 | for (i = start; i < start + len; i++) { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 1040 | result = spi_byte_program(flash, i, buf[i - start]); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1041 | if (result) |
| 1042 | return 1; |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 1043 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1044 | programmer_delay(10); |
| 1045 | } |
| 1046 | |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 1050 | int default_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 1051 | { |
| 1052 | uint32_t pos = start; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1053 | int result; |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1054 | unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = { |
| 1055 | JEDEC_AAI_WORD_PROGRAM, |
| 1056 | }; |
| 1057 | struct spi_command cmds[] = { |
| 1058 | { |
| 1059 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 1060 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 1061 | .readcnt = 0, |
| 1062 | .readarr = NULL, |
| 1063 | }, { |
| 1064 | .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE, |
| 1065 | .writearr = (const unsigned char[]){ |
| 1066 | JEDEC_AAI_WORD_PROGRAM, |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 1067 | (start >> 16) & 0xff, |
| 1068 | (start >> 8) & 0xff, |
| 1069 | (start & 0xff), |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1070 | buf[0], |
| 1071 | buf[1] |
| 1072 | }, |
| 1073 | .readcnt = 0, |
| 1074 | .readarr = NULL, |
| 1075 | }, { |
| 1076 | .writecnt = 0, |
| 1077 | .writearr = NULL, |
| 1078 | .readcnt = 0, |
| 1079 | .readarr = NULL, |
| 1080 | }}; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1081 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 1082 | switch (flash->mst->spi.type) { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 1083 | #if CONFIG_INTERNAL == 1 |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1084 | #if defined(__i386__) || defined(__x86_64__) |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1085 | case SPI_CONTROLLER_IT87XX: |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1086 | case SPI_CONTROLLER_WBSIO: |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 1087 | msg_perr("%s: impossible with this SPI controller," |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1088 | " degrading to byte program\n", __func__); |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 1089 | return spi_chip_write_1(flash, buf, start, len); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1090 | #endif |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1091 | #endif |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1092 | default: |
| 1093 | break; |
| 1094 | } |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1095 | |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 1096 | /* The even start address and even length requirements can be either |
| 1097 | * honored outside this function, or we can call spi_byte_program |
| 1098 | * 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] | 1099 | * FIXME: Move this to generic code. |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 1100 | */ |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1101 | /* 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] | 1102 | if (start % 2) { |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1103 | msg_cerr("%s: start address not even! Please report a bug at " |
| 1104 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 1105 | if (spi_chip_write_1(flash, buf, start, start % 2)) |
| 1106 | return SPI_GENERIC_ERROR; |
| 1107 | pos += start % 2; |
Carl-Daniel Hailfinger | ccfe0ac | 2010-10-27 22:07:11 +0000 | [diff] [blame] | 1108 | cmds[1].writearr = (const unsigned char[]){ |
| 1109 | JEDEC_AAI_WORD_PROGRAM, |
| 1110 | (pos >> 16) & 0xff, |
| 1111 | (pos >> 8) & 0xff, |
| 1112 | (pos & 0xff), |
| 1113 | buf[pos - start], |
| 1114 | buf[pos - start + 1] |
| 1115 | }; |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 1116 | /* Do not return an error for now. */ |
| 1117 | //return SPI_GENERIC_ERROR; |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1118 | } |
| 1119 | /* The data sheet requires total AAI write length to be even. */ |
| 1120 | if (len % 2) { |
| 1121 | msg_cerr("%s: total write length not even! Please report a " |
| 1122 | "bug at flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 1123 | /* Do not return an error for now. */ |
| 1124 | //return SPI_GENERIC_ERROR; |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1127 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 1128 | result = spi_send_multicommand(flash, cmds); |
Stefan Reinauer | 87ace66 | 2014-04-26 16:12:55 +0000 | [diff] [blame] | 1129 | if (result != 0) { |
| 1130 | msg_cerr("%s failed during start command execution: %d\n", __func__, result); |
| 1131 | goto bailout; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1132 | } |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 1133 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1134 | programmer_delay(10); |
| 1135 | |
| 1136 | /* We already wrote 2 bytes in the multicommand step. */ |
| 1137 | pos += 2; |
| 1138 | |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 1139 | /* Are there at least two more bytes to write? */ |
| 1140 | while (pos < start + len - 1) { |
Carl-Daniel Hailfinger | ccfe0ac | 2010-10-27 22:07:11 +0000 | [diff] [blame] | 1141 | cmd[1] = buf[pos++ - start]; |
| 1142 | cmd[2] = buf[pos++ - start]; |
Stefan Reinauer | 87ace66 | 2014-04-26 16:12:55 +0000 | [diff] [blame] | 1143 | result = spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL); |
| 1144 | if (result != 0) { |
| 1145 | msg_cerr("%s failed during followup AAI command execution: %d\n", __func__, result); |
| 1146 | goto bailout; |
| 1147 | } |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 1148 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1149 | programmer_delay(10); |
| 1150 | } |
| 1151 | |
Stefan Tauner | 59c4d79 | 2014-04-26 16:13:09 +0000 | [diff] [blame] | 1152 | /* Use WRDI to exit AAI mode. This needs to be done before issuing any other non-AAI command. */ |
| 1153 | result = spi_write_disable(flash); |
| 1154 | if (result != 0) { |
| 1155 | msg_cerr("%s failed to disable AAI mode.\n", __func__); |
| 1156 | return SPI_GENERIC_ERROR; |
| 1157 | } |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 1158 | |
| 1159 | /* Write remaining byte (if any). */ |
| 1160 | if (pos < start + len) { |
Carl-Daniel Hailfinger | ccfe0ac | 2010-10-27 22:07:11 +0000 | [diff] [blame] | 1161 | 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] | 1162 | return SPI_GENERIC_ERROR; |
| 1163 | pos += pos % 2; |
| 1164 | } |
| 1165 | |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1166 | return 0; |
Stefan Reinauer | 87ace66 | 2014-04-26 16:12:55 +0000 | [diff] [blame] | 1167 | |
| 1168 | bailout: |
Stefan Tauner | 59c4d79 | 2014-04-26 16:13:09 +0000 | [diff] [blame] | 1169 | result = spi_write_disable(flash); |
| 1170 | if (result != 0) |
| 1171 | msg_cerr("%s failed to disable AAI mode.\n", __func__); |
Stefan Reinauer | 87ace66 | 2014-04-26 16:12:55 +0000 | [diff] [blame] | 1172 | return SPI_GENERIC_ERROR; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1173 | } |