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. */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 159 | if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff) |
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 | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 175 | switch (flash->pgm->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. */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 215 | if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff) |
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 | 57794ac | 2012-12-29 15:04:20 +0000 | [diff] [blame] | 282 | /* Only used for some Atmel chips. */ |
| 283 | int probe_spi_at25f(struct flashctx *flash) |
| 284 | { |
| 285 | static const unsigned char cmd[AT25F_RDID_OUTSIZE] = { AT25F_RDID }; |
| 286 | unsigned char readarr[AT25F_RDID_INSIZE]; |
| 287 | uint32_t id1; |
| 288 | uint32_t id2; |
| 289 | |
| 290 | if (spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr)) |
| 291 | return 0; |
| 292 | |
| 293 | id1 = readarr[0]; |
| 294 | id2 = readarr[1]; |
| 295 | |
| 296 | msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2); |
| 297 | |
| 298 | if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id) |
| 299 | return 1; |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 304 | int spi_chip_erase_60(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 305 | { |
| 306 | int result; |
| 307 | struct spi_command cmds[] = { |
| 308 | { |
| 309 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 310 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 311 | .readcnt = 0, |
| 312 | .readarr = NULL, |
| 313 | }, { |
| 314 | .writecnt = JEDEC_CE_60_OUTSIZE, |
| 315 | .writearr = (const unsigned char[]){ JEDEC_CE_60 }, |
| 316 | .readcnt = 0, |
| 317 | .readarr = NULL, |
| 318 | }, { |
| 319 | .writecnt = 0, |
| 320 | .writearr = NULL, |
| 321 | .readcnt = 0, |
| 322 | .readarr = NULL, |
| 323 | }}; |
| 324 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 325 | result = spi_send_multicommand(flash, cmds); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 326 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 327 | msg_cerr("%s failed during command execution\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 328 | __func__); |
| 329 | return result; |
| 330 | } |
| 331 | /* Wait until the Write-In-Progress bit is cleared. |
| 332 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 333 | */ |
| 334 | /* FIXME: We assume spi_read_status_register will never fail. */ |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 335 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 336 | programmer_delay(1000 * 1000); |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 337 | /* FIXME: Check the status register for errors. */ |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 338 | return 0; |
| 339 | } |
| 340 | |
Stefan Tauner | 3c0fcd0 | 2012-09-21 12:46:56 +0000 | [diff] [blame] | 341 | int spi_chip_erase_62(struct flashctx *flash) |
| 342 | { |
| 343 | int result; |
| 344 | struct spi_command cmds[] = { |
| 345 | { |
| 346 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 347 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 348 | .readcnt = 0, |
| 349 | .readarr = NULL, |
| 350 | }, { |
| 351 | .writecnt = JEDEC_CE_62_OUTSIZE, |
| 352 | .writearr = (const unsigned char[]){ JEDEC_CE_62 }, |
| 353 | .readcnt = 0, |
| 354 | .readarr = NULL, |
| 355 | }, { |
| 356 | .writecnt = 0, |
| 357 | .writearr = NULL, |
| 358 | .readcnt = 0, |
| 359 | .readarr = NULL, |
| 360 | }}; |
| 361 | |
| 362 | result = spi_send_multicommand(flash, cmds); |
| 363 | if (result) { |
| 364 | msg_cerr("%s failed during command execution\n", |
| 365 | __func__); |
| 366 | return result; |
| 367 | } |
| 368 | /* Wait until the Write-In-Progress bit is cleared. |
| 369 | * This usually takes 2-5 s, so wait in 100 ms steps. |
| 370 | */ |
| 371 | /* FIXME: We assume spi_read_status_register will never fail. */ |
| 372 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
| 373 | programmer_delay(100 * 1000); |
| 374 | /* FIXME: Check the status register for errors. */ |
| 375 | return 0; |
| 376 | } |
| 377 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 378 | int spi_chip_erase_c7(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 379 | { |
| 380 | int result; |
| 381 | struct spi_command cmds[] = { |
| 382 | { |
| 383 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 384 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 385 | .readcnt = 0, |
| 386 | .readarr = NULL, |
| 387 | }, { |
| 388 | .writecnt = JEDEC_CE_C7_OUTSIZE, |
| 389 | .writearr = (const unsigned char[]){ JEDEC_CE_C7 }, |
| 390 | .readcnt = 0, |
| 391 | .readarr = NULL, |
| 392 | }, { |
| 393 | .writecnt = 0, |
| 394 | .writearr = NULL, |
| 395 | .readcnt = 0, |
| 396 | .readarr = NULL, |
| 397 | }}; |
| 398 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 399 | result = spi_send_multicommand(flash, cmds); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 400 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 401 | msg_cerr("%s failed during command execution\n", __func__); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 402 | return result; |
| 403 | } |
| 404 | /* Wait until the Write-In-Progress bit is cleared. |
| 405 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 406 | */ |
| 407 | /* FIXME: We assume spi_read_status_register will never fail. */ |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 408 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 409 | programmer_delay(1000 * 1000); |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 410 | /* FIXME: Check the status register for errors. */ |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 411 | return 0; |
| 412 | } |
| 413 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 414 | int spi_block_erase_52(struct flashctx *flash, unsigned int addr, |
| 415 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 416 | { |
| 417 | int result; |
| 418 | struct spi_command cmds[] = { |
| 419 | { |
| 420 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 421 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 422 | .readcnt = 0, |
| 423 | .readarr = NULL, |
| 424 | }, { |
| 425 | .writecnt = JEDEC_BE_52_OUTSIZE, |
| 426 | .writearr = (const unsigned char[]){ |
| 427 | JEDEC_BE_52, |
| 428 | (addr >> 16) & 0xff, |
| 429 | (addr >> 8) & 0xff, |
| 430 | (addr & 0xff) |
| 431 | }, |
| 432 | .readcnt = 0, |
| 433 | .readarr = NULL, |
| 434 | }, { |
| 435 | .writecnt = 0, |
| 436 | .writearr = NULL, |
| 437 | .readcnt = 0, |
| 438 | .readarr = NULL, |
| 439 | }}; |
| 440 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 441 | result = spi_send_multicommand(flash, cmds); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 442 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 443 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 444 | __func__, addr); |
| 445 | return result; |
| 446 | } |
| 447 | /* Wait until the Write-In-Progress bit is cleared. |
| 448 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 449 | */ |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 450 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 451 | programmer_delay(100 * 1000); |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 452 | /* FIXME: Check the status register for errors. */ |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | /* Block size is usually |
| 457 | * 64k for Macronix |
| 458 | * 32k for SST |
| 459 | * 4-32k non-uniform for EON |
| 460 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 461 | int spi_block_erase_d8(struct flashctx *flash, unsigned int addr, |
| 462 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 463 | { |
| 464 | int result; |
| 465 | struct spi_command cmds[] = { |
| 466 | { |
| 467 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 468 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 469 | .readcnt = 0, |
| 470 | .readarr = NULL, |
| 471 | }, { |
| 472 | .writecnt = JEDEC_BE_D8_OUTSIZE, |
| 473 | .writearr = (const unsigned char[]){ |
| 474 | JEDEC_BE_D8, |
| 475 | (addr >> 16) & 0xff, |
| 476 | (addr >> 8) & 0xff, |
| 477 | (addr & 0xff) |
| 478 | }, |
| 479 | .readcnt = 0, |
| 480 | .readarr = NULL, |
| 481 | }, { |
| 482 | .writecnt = 0, |
| 483 | .writearr = NULL, |
| 484 | .readcnt = 0, |
| 485 | .readarr = NULL, |
| 486 | }}; |
| 487 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 488 | result = spi_send_multicommand(flash, cmds); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 489 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 490 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 491 | __func__, addr); |
| 492 | return result; |
| 493 | } |
| 494 | /* Wait until the Write-In-Progress bit is cleared. |
| 495 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 496 | */ |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 497 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 498 | programmer_delay(100 * 1000); |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 499 | /* FIXME: Check the status register for errors. */ |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | /* Block size is usually |
| 504 | * 4k for PMC |
| 505 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 506 | int spi_block_erase_d7(struct flashctx *flash, unsigned int addr, |
| 507 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 508 | { |
| 509 | int result; |
| 510 | struct spi_command cmds[] = { |
| 511 | { |
| 512 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 513 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 514 | .readcnt = 0, |
| 515 | .readarr = NULL, |
| 516 | }, { |
| 517 | .writecnt = JEDEC_BE_D7_OUTSIZE, |
| 518 | .writearr = (const unsigned char[]){ |
| 519 | JEDEC_BE_D7, |
| 520 | (addr >> 16) & 0xff, |
| 521 | (addr >> 8) & 0xff, |
| 522 | (addr & 0xff) |
| 523 | }, |
| 524 | .readcnt = 0, |
| 525 | .readarr = NULL, |
| 526 | }, { |
| 527 | .writecnt = 0, |
| 528 | .writearr = NULL, |
| 529 | .readcnt = 0, |
| 530 | .readarr = NULL, |
| 531 | }}; |
| 532 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 533 | result = spi_send_multicommand(flash, cmds); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 534 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 535 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 536 | __func__, addr); |
| 537 | return result; |
| 538 | } |
| 539 | /* Wait until the Write-In-Progress bit is cleared. |
| 540 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 541 | */ |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 542 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 543 | programmer_delay(100 * 1000); |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 544 | /* FIXME: Check the status register for errors. */ |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 545 | return 0; |
| 546 | } |
| 547 | |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 548 | /* Sector size is usually 4k, though Macronix eliteflash has 64k */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 549 | int spi_block_erase_20(struct flashctx *flash, unsigned int addr, |
| 550 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 551 | { |
| 552 | int result; |
| 553 | struct spi_command cmds[] = { |
| 554 | { |
| 555 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 556 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 557 | .readcnt = 0, |
| 558 | .readarr = NULL, |
| 559 | }, { |
| 560 | .writecnt = JEDEC_SE_OUTSIZE, |
| 561 | .writearr = (const unsigned char[]){ |
| 562 | JEDEC_SE, |
| 563 | (addr >> 16) & 0xff, |
| 564 | (addr >> 8) & 0xff, |
| 565 | (addr & 0xff) |
| 566 | }, |
| 567 | .readcnt = 0, |
| 568 | .readarr = NULL, |
| 569 | }, { |
| 570 | .writecnt = 0, |
| 571 | .writearr = NULL, |
| 572 | .readcnt = 0, |
| 573 | .readarr = NULL, |
| 574 | }}; |
| 575 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 576 | result = spi_send_multicommand(flash, cmds); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 577 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 578 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 579 | __func__, addr); |
| 580 | return result; |
| 581 | } |
| 582 | /* Wait until the Write-In-Progress bit is cleared. |
| 583 | * This usually takes 15-800 ms, so wait in 10 ms steps. |
| 584 | */ |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 585 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 586 | programmer_delay(10 * 1000); |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 587 | /* FIXME: Check the status register for errors. */ |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | |
Stefan Tauner | 94b39b4 | 2012-10-27 00:06:02 +0000 | [diff] [blame] | 591 | int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 592 | { |
| 593 | int result; |
| 594 | struct spi_command cmds[] = { |
| 595 | { |
| 596 | /* .writecnt = JEDEC_WREN_OUTSIZE, |
| 597 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 598 | .readcnt = 0, |
| 599 | .readarr = NULL, |
| 600 | }, { */ |
| 601 | .writecnt = JEDEC_BE_50_OUTSIZE, |
| 602 | .writearr = (const unsigned char[]){ |
| 603 | JEDEC_BE_50, |
| 604 | (addr >> 16) & 0xff, |
| 605 | (addr >> 8) & 0xff, |
| 606 | (addr & 0xff) |
| 607 | }, |
| 608 | .readcnt = 0, |
| 609 | .readarr = NULL, |
| 610 | }, { |
| 611 | .writecnt = 0, |
| 612 | .writearr = NULL, |
| 613 | .readcnt = 0, |
| 614 | .readarr = NULL, |
| 615 | }}; |
| 616 | |
| 617 | result = spi_send_multicommand(flash, cmds); |
| 618 | if (result) { |
| 619 | msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr); |
| 620 | return result; |
| 621 | } |
| 622 | /* Wait until the Write-In-Progress bit is cleared. |
| 623 | * This usually takes 10 ms, so wait in 1 ms steps. |
| 624 | */ |
| 625 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
| 626 | programmer_delay(1 * 1000); |
| 627 | /* FIXME: Check the status register for errors. */ |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 632 | { |
| 633 | int result; |
| 634 | struct spi_command cmds[] = { |
| 635 | { |
| 636 | /* .writecnt = JEDEC_WREN_OUTSIZE, |
| 637 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 638 | .readcnt = 0, |
| 639 | .readarr = NULL, |
| 640 | }, { */ |
| 641 | .writecnt = JEDEC_BE_81_OUTSIZE, |
| 642 | .writearr = (const unsigned char[]){ |
| 643 | JEDEC_BE_81, |
| 644 | (addr >> 16) & 0xff, |
| 645 | (addr >> 8) & 0xff, |
| 646 | (addr & 0xff) |
| 647 | }, |
| 648 | .readcnt = 0, |
| 649 | .readarr = NULL, |
| 650 | }, { |
| 651 | .writecnt = 0, |
| 652 | .writearr = NULL, |
| 653 | .readcnt = 0, |
| 654 | .readarr = NULL, |
| 655 | }}; |
| 656 | |
| 657 | result = spi_send_multicommand(flash, cmds); |
| 658 | if (result) { |
| 659 | msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr); |
| 660 | return result; |
| 661 | } |
| 662 | /* Wait until the Write-In-Progress bit is cleared. |
| 663 | * This usually takes 8 ms, so wait in 1 ms steps. |
| 664 | */ |
| 665 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
| 666 | programmer_delay(1 * 1000); |
| 667 | /* FIXME: Check the status register for errors. */ |
| 668 | return 0; |
| 669 | } |
| 670 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 671 | int spi_block_erase_60(struct flashctx *flash, unsigned int addr, |
| 672 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 673 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 674 | if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 675 | msg_cerr("%s called with incorrect arguments\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 676 | __func__); |
| 677 | return -1; |
| 678 | } |
| 679 | return spi_chip_erase_60(flash); |
| 680 | } |
| 681 | |
Stefan Tauner | 3c0fcd0 | 2012-09-21 12:46:56 +0000 | [diff] [blame] | 682 | int spi_block_erase_62(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 683 | { |
| 684 | if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) { |
| 685 | msg_cerr("%s called with incorrect arguments\n", |
| 686 | __func__); |
| 687 | return -1; |
| 688 | } |
| 689 | return spi_chip_erase_62(flash); |
| 690 | } |
| 691 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 692 | int spi_block_erase_c7(struct flashctx *flash, unsigned int addr, |
| 693 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 694 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 695 | if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 696 | msg_cerr("%s called with incorrect arguments\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 697 | __func__); |
| 698 | return -1; |
| 699 | } |
| 700 | return spi_chip_erase_c7(flash); |
| 701 | } |
| 702 | |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 703 | erasefunc_t *spi_get_erasefn_from_opcode(uint8_t opcode) |
| 704 | { |
| 705 | switch(opcode){ |
| 706 | case 0xff: |
| 707 | case 0x00: |
| 708 | /* Not specified, assuming "not supported". */ |
| 709 | return NULL; |
| 710 | case 0x20: |
| 711 | return &spi_block_erase_20; |
| 712 | case 0x52: |
| 713 | return &spi_block_erase_52; |
| 714 | case 0x60: |
| 715 | return &spi_block_erase_60; |
| 716 | case 0xc7: |
| 717 | return &spi_block_erase_c7; |
| 718 | case 0xd7: |
| 719 | return &spi_block_erase_d7; |
| 720 | case 0xd8: |
| 721 | return &spi_block_erase_d8; |
| 722 | default: |
| 723 | msg_cinfo("%s: unknown erase opcode (0x%02x). Please report " |
| 724 | "this at flashrom@flashrom.org\n", __func__, opcode); |
| 725 | return NULL; |
| 726 | } |
| 727 | } |
| 728 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 729 | int spi_byte_program(struct flashctx *flash, unsigned int addr, |
| 730 | uint8_t databyte) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 731 | { |
| 732 | int result; |
| 733 | struct spi_command cmds[] = { |
| 734 | { |
| 735 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 736 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 737 | .readcnt = 0, |
| 738 | .readarr = NULL, |
| 739 | }, { |
| 740 | .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE, |
| 741 | .writearr = (const unsigned char[]){ |
| 742 | JEDEC_BYTE_PROGRAM, |
| 743 | (addr >> 16) & 0xff, |
| 744 | (addr >> 8) & 0xff, |
| 745 | (addr & 0xff), |
| 746 | databyte |
| 747 | }, |
| 748 | .readcnt = 0, |
| 749 | .readarr = NULL, |
| 750 | }, { |
| 751 | .writecnt = 0, |
| 752 | .writearr = NULL, |
| 753 | .readcnt = 0, |
| 754 | .readarr = NULL, |
| 755 | }}; |
| 756 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 757 | result = spi_send_multicommand(flash, cmds); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 758 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 759 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 760 | __func__, addr); |
| 761 | } |
| 762 | return result; |
| 763 | } |
| 764 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 765 | int spi_nbyte_program(struct flashctx *flash, unsigned int addr, uint8_t *bytes, |
| 766 | unsigned int len) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 767 | { |
| 768 | int result; |
| 769 | /* FIXME: Switch to malloc based on len unless that kills speed. */ |
| 770 | unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = { |
| 771 | JEDEC_BYTE_PROGRAM, |
| 772 | (addr >> 16) & 0xff, |
| 773 | (addr >> 8) & 0xff, |
| 774 | (addr >> 0) & 0xff, |
| 775 | }; |
| 776 | struct spi_command cmds[] = { |
| 777 | { |
| 778 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 779 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 780 | .readcnt = 0, |
| 781 | .readarr = NULL, |
| 782 | }, { |
| 783 | .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len, |
| 784 | .writearr = cmd, |
| 785 | .readcnt = 0, |
| 786 | .readarr = NULL, |
| 787 | }, { |
| 788 | .writecnt = 0, |
| 789 | .writearr = NULL, |
| 790 | .readcnt = 0, |
| 791 | .readarr = NULL, |
| 792 | }}; |
| 793 | |
| 794 | if (!len) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 795 | msg_cerr("%s called for zero-length write\n", __func__); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 796 | return 1; |
| 797 | } |
| 798 | if (len > 256) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 799 | msg_cerr("%s called for too long a write\n", __func__); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 800 | return 1; |
| 801 | } |
| 802 | |
| 803 | memcpy(&cmd[4], bytes, len); |
| 804 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 805 | result = spi_send_multicommand(flash, cmds); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 806 | if (result) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 807 | msg_cerr("%s failed during command execution at address 0x%x\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 808 | __func__, addr); |
| 809 | } |
| 810 | return result; |
| 811 | } |
| 812 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 813 | int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes, |
| 814 | unsigned int len) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 815 | { |
| 816 | const unsigned char cmd[JEDEC_READ_OUTSIZE] = { |
| 817 | JEDEC_READ, |
| 818 | (address >> 16) & 0xff, |
| 819 | (address >> 8) & 0xff, |
| 820 | (address >> 0) & 0xff, |
| 821 | }; |
| 822 | |
| 823 | /* Send Read */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 824 | return spi_send_command(flash, sizeof(cmd), len, cmd, bytes); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | /* |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 828 | * Read a part of the flash chip. |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 829 | * FIXME: Use the chunk code from Michael Karcher instead. |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 830 | * Each page is read separately in chunks with a maximum size of chunksize. |
| 831 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 832 | int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 833 | unsigned int len, unsigned int chunksize) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 834 | { |
| 835 | int rc = 0; |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 836 | unsigned int i, j, starthere, lenhere, toread; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 837 | unsigned int page_size = flash->chip->page_size; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 838 | |
| 839 | /* Warning: This loop has a very unusual condition and body. |
| 840 | * The loop needs to go through each page with at least one affected |
| 841 | * byte. The lowest page number is (start / page_size) since that |
| 842 | * division rounds down. The highest page number we want is the page |
| 843 | * where the last byte of the range lives. That last byte has the |
| 844 | * address (start + len - 1), thus the highest page number is |
| 845 | * (start + len - 1) / page_size. Since we want to include that last |
| 846 | * page as well, the loop condition uses <=. |
| 847 | */ |
| 848 | for (i = start / page_size; i <= (start + len - 1) / page_size; i++) { |
| 849 | /* Byte position of the first byte in the range in this page. */ |
| 850 | /* starthere is an offset to the base address of the chip. */ |
| 851 | starthere = max(start, i * page_size); |
| 852 | /* Length of bytes in the range in this page. */ |
| 853 | lenhere = min(start + len, (i + 1) * page_size) - starthere; |
| 854 | for (j = 0; j < lenhere; j += chunksize) { |
| 855 | toread = min(chunksize, lenhere - j); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 856 | rc = spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 857 | if (rc) |
| 858 | break; |
| 859 | } |
| 860 | if (rc) |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | return rc; |
| 865 | } |
| 866 | |
| 867 | /* |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 868 | * Write a part of the flash chip. |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 869 | * FIXME: Use the chunk code from Michael Karcher instead. |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 870 | * Each page is written separately in chunks with a maximum size of chunksize. |
| 871 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 872 | int spi_write_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 873 | unsigned int len, unsigned int chunksize) |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 874 | { |
| 875 | int rc = 0; |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 876 | unsigned int i, j, starthere, lenhere, towrite; |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 877 | /* 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] | 878 | * in struct flashctx to do this properly. All chips using |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 879 | * spi_chip_write_256 have page_size set to max_writechunk_size, so |
| 880 | * we're OK for now. |
| 881 | */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 882 | unsigned int page_size = flash->chip->page_size; |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 883 | |
| 884 | /* Warning: This loop has a very unusual condition and body. |
| 885 | * The loop needs to go through each page with at least one affected |
| 886 | * byte. The lowest page number is (start / page_size) since that |
| 887 | * division rounds down. The highest page number we want is the page |
| 888 | * where the last byte of the range lives. That last byte has the |
| 889 | * address (start + len - 1), thus the highest page number is |
| 890 | * (start + len - 1) / page_size. Since we want to include that last |
| 891 | * page as well, the loop condition uses <=. |
| 892 | */ |
| 893 | for (i = start / page_size; i <= (start + len - 1) / page_size; i++) { |
| 894 | /* Byte position of the first byte in the range in this page. */ |
| 895 | /* starthere is an offset to the base address of the chip. */ |
| 896 | starthere = max(start, i * page_size); |
| 897 | /* Length of bytes in the range in this page. */ |
| 898 | lenhere = min(start + len, (i + 1) * page_size) - starthere; |
| 899 | for (j = 0; j < lenhere; j += chunksize) { |
| 900 | towrite = min(chunksize, lenhere - j); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 901 | 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] | 902 | if (rc) |
| 903 | break; |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 904 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 905 | programmer_delay(10); |
| 906 | } |
| 907 | if (rc) |
| 908 | break; |
| 909 | } |
| 910 | |
| 911 | return rc; |
| 912 | } |
| 913 | |
| 914 | /* |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 915 | * Program chip using byte programming. (SLOW!) |
| 916 | * This is for chips which can only handle one byte writes |
| 917 | * and for chips where memory mapped programming is impossible |
| 918 | * (e.g. due to size constraints in IT87* for over 512 kB) |
| 919 | */ |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 920 | /* real chunksize is 1, logical chunksize is 1 */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 921 | int spi_chip_write_1(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 922 | unsigned int len) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 923 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 924 | unsigned int i; |
| 925 | int result = 0; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 926 | |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 927 | for (i = start; i < start + len; i++) { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 928 | result = spi_byte_program(flash, i, buf[i - start]); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 929 | if (result) |
| 930 | return 1; |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 931 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 932 | programmer_delay(10); |
| 933 | } |
| 934 | |
| 935 | return 0; |
| 936 | } |
| 937 | |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 938 | int default_spi_write_aai(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 939 | { |
| 940 | uint32_t pos = start; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 941 | int result; |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 942 | unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = { |
| 943 | JEDEC_AAI_WORD_PROGRAM, |
| 944 | }; |
| 945 | struct spi_command cmds[] = { |
| 946 | { |
| 947 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 948 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 949 | .readcnt = 0, |
| 950 | .readarr = NULL, |
| 951 | }, { |
| 952 | .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE, |
| 953 | .writearr = (const unsigned char[]){ |
| 954 | JEDEC_AAI_WORD_PROGRAM, |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 955 | (start >> 16) & 0xff, |
| 956 | (start >> 8) & 0xff, |
| 957 | (start & 0xff), |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 958 | buf[0], |
| 959 | buf[1] |
| 960 | }, |
| 961 | .readcnt = 0, |
| 962 | .readarr = NULL, |
| 963 | }, { |
| 964 | .writecnt = 0, |
| 965 | .writearr = NULL, |
| 966 | .readcnt = 0, |
| 967 | .readarr = NULL, |
| 968 | }}; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 969 | |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 970 | switch (flash->pgm->spi.type) { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 971 | #if CONFIG_INTERNAL == 1 |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 972 | #if defined(__i386__) || defined(__x86_64__) |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 973 | case SPI_CONTROLLER_IT87XX: |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 974 | case SPI_CONTROLLER_WBSIO: |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 975 | msg_perr("%s: impossible with this SPI controller," |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 976 | " degrading to byte program\n", __func__); |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 977 | return spi_chip_write_1(flash, buf, start, len); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 978 | #endif |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 979 | #endif |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 980 | default: |
| 981 | break; |
| 982 | } |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 983 | |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 984 | /* The even start address and even length requirements can be either |
| 985 | * honored outside this function, or we can call spi_byte_program |
| 986 | * 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] | 987 | * FIXME: Move this to generic code. |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 988 | */ |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 989 | /* 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] | 990 | if (start % 2) { |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 991 | msg_cerr("%s: start address not even! Please report a bug at " |
| 992 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 993 | if (spi_chip_write_1(flash, buf, start, start % 2)) |
| 994 | return SPI_GENERIC_ERROR; |
| 995 | pos += start % 2; |
Carl-Daniel Hailfinger | ccfe0ac | 2010-10-27 22:07:11 +0000 | [diff] [blame] | 996 | cmds[1].writearr = (const unsigned char[]){ |
| 997 | JEDEC_AAI_WORD_PROGRAM, |
| 998 | (pos >> 16) & 0xff, |
| 999 | (pos >> 8) & 0xff, |
| 1000 | (pos & 0xff), |
| 1001 | buf[pos - start], |
| 1002 | buf[pos - start + 1] |
| 1003 | }; |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 1004 | /* Do not return an error for now. */ |
| 1005 | //return SPI_GENERIC_ERROR; |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1006 | } |
| 1007 | /* The data sheet requires total AAI write length to be even. */ |
| 1008 | if (len % 2) { |
| 1009 | msg_cerr("%s: total write length not even! Please report a " |
| 1010 | "bug at flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 1011 | /* Do not return an error for now. */ |
| 1012 | //return SPI_GENERIC_ERROR; |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1015 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 1016 | result = spi_send_multicommand(flash, cmds); |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1017 | if (result) { |
| 1018 | msg_cerr("%s failed during start command execution\n", |
| 1019 | __func__); |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 1020 | /* FIXME: Should we send WRDI here as well to make sure the chip |
| 1021 | * is not in AAI mode? |
| 1022 | */ |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1023 | return result; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1024 | } |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 1025 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1026 | programmer_delay(10); |
| 1027 | |
| 1028 | /* We already wrote 2 bytes in the multicommand step. */ |
| 1029 | pos += 2; |
| 1030 | |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 1031 | /* Are there at least two more bytes to write? */ |
| 1032 | while (pos < start + len - 1) { |
Carl-Daniel Hailfinger | ccfe0ac | 2010-10-27 22:07:11 +0000 | [diff] [blame] | 1033 | cmd[1] = buf[pos++ - start]; |
| 1034 | cmd[2] = buf[pos++ - start]; |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 1035 | spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, |
| 1036 | cmd, NULL); |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 1037 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 1038 | programmer_delay(10); |
| 1039 | } |
| 1040 | |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 1041 | /* Use WRDI to exit AAI mode. This needs to be done before issuing any |
| 1042 | * other non-AAI command. |
| 1043 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 1044 | spi_write_disable(flash); |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 1045 | |
| 1046 | /* Write remaining byte (if any). */ |
| 1047 | if (pos < start + len) { |
Carl-Daniel Hailfinger | ccfe0ac | 2010-10-27 22:07:11 +0000 | [diff] [blame] | 1048 | 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] | 1049 | return SPI_GENERIC_ERROR; |
| 1050 | pos += pos % 2; |
| 1051 | } |
| 1052 | |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1053 | return 0; |
| 1054 | } |