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