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 | |
Nico Huber | a3140d0 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 25 | #include <stddef.h> |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 26 | #include <string.h> |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 27 | #include <stdbool.h> |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 28 | #include "flash.h" |
| 29 | #include "flashchips.h" |
| 30 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 31 | #include "programmer.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 32 | #include "spi.h" |
Boris Baykov | 9912718 | 2016-06-11 18:29:00 +0200 | [diff] [blame] | 33 | #include "spi4ba.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 34 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 35 | static int spi_rdid(struct flashctx *flash, unsigned char *readarr, int bytes) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 36 | { |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 37 | static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID }; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 38 | int ret; |
| 39 | int i; |
| 40 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 41 | ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 42 | if (ret) |
| 43 | return ret; |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 44 | msg_cspew("RDID returned"); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 45 | for (i = 0; i < bytes; i++) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 46 | msg_cspew(" 0x%02x", readarr[i]); |
| 47 | msg_cspew(". "); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 48 | return 0; |
| 49 | } |
| 50 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 51 | static int spi_rems(struct flashctx *flash, unsigned char *readarr) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 52 | { |
| 53 | unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 }; |
| 54 | uint32_t readaddr; |
| 55 | int ret; |
| 56 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 57 | ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd, |
| 58 | readarr); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 59 | if (ret == SPI_INVALID_ADDRESS) { |
| 60 | /* Find the lowest even address allowed for reads. */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 61 | readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 62 | cmd[1] = (readaddr >> 16) & 0xff, |
| 63 | cmd[2] = (readaddr >> 8) & 0xff, |
| 64 | cmd[3] = (readaddr >> 0) & 0xff, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 65 | ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, |
| 66 | cmd, readarr); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 67 | } |
| 68 | if (ret) |
| 69 | return ret; |
Cristian Măgherușan-Stanciu | 9932c7b | 2011-07-07 19:56:58 +0000 | [diff] [blame] | 70 | msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 74 | static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 75 | { |
| 76 | unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 }; |
| 77 | uint32_t readaddr; |
| 78 | int ret; |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 79 | int i; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 80 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 81 | ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 82 | if (ret == SPI_INVALID_ADDRESS) { |
| 83 | /* Find the lowest even address allowed for reads. */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 84 | readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 85 | cmd[1] = (readaddr >> 16) & 0xff, |
| 86 | cmd[2] = (readaddr >> 8) & 0xff, |
| 87 | cmd[3] = (readaddr >> 0) & 0xff, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 88 | ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 89 | } |
| 90 | if (ret) |
| 91 | return ret; |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 92 | msg_cspew("RES returned"); |
| 93 | for (i = 0; i < bytes; i++) |
| 94 | msg_cspew(" 0x%02x", readarr[i]); |
| 95 | msg_cspew(". "); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 99 | int spi_write_enable(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 100 | { |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 101 | static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN }; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 102 | int result; |
| 103 | |
| 104 | /* Send WREN (Write Enable) */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 105 | result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 106 | |
| 107 | if (result) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 108 | msg_cerr("%s failed\n", __func__); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 109 | |
| 110 | return result; |
| 111 | } |
| 112 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 113 | int spi_write_disable(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 114 | { |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 115 | static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI }; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 116 | |
| 117 | /* Send WRDI (Write Disable) */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 118 | return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 121 | static int probe_spi_rdid_generic(struct flashctx *flash, int bytes) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 122 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 123 | const struct flashchip *chip = flash->chip; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 124 | unsigned char readarr[4]; |
| 125 | uint32_t id1; |
| 126 | uint32_t id2; |
| 127 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 128 | if (spi_rdid(flash, readarr, bytes)) { |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 129 | return 0; |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 130 | } |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 131 | |
| 132 | if (!oddparity(readarr[0])) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 133 | msg_cdbg("RDID byte 0 parity violation. "); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 134 | |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 135 | /* Check if this is a continuation vendor ID. |
| 136 | * FIXME: Handle continuation device IDs. |
| 137 | */ |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 138 | if (readarr[0] == 0x7f) { |
| 139 | if (!oddparity(readarr[1])) |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 140 | msg_cdbg("RDID byte 1 parity violation. "); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 141 | id1 = (readarr[0] << 8) | readarr[1]; |
| 142 | id2 = readarr[2]; |
| 143 | if (bytes > 3) { |
| 144 | id2 <<= 8; |
| 145 | id2 |= readarr[3]; |
| 146 | } |
| 147 | } else { |
| 148 | id1 = readarr[0]; |
| 149 | id2 = (readarr[1] << 8) | readarr[2]; |
| 150 | } |
| 151 | |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 152 | 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] | 153 | |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 154 | if (id1 == chip->manufacture_id && id2 == chip->model_id) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 155 | return 1; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 156 | |
| 157 | /* Test if this is a pure vendor match. */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 158 | if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 159 | return 1; |
| 160 | |
| 161 | /* Test if there is any vendor ID. */ |
Urja Rannikko | 0a5f6e4 | 2015-06-22 23:59:15 +0000 | [diff] [blame] | 162 | if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff && id1 != 0x00) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 163 | return 1; |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 168 | int probe_spi_rdid(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 169 | { |
| 170 | return probe_spi_rdid_generic(flash, 3); |
| 171 | } |
| 172 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 173 | int probe_spi_rdid4(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 174 | { |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 175 | /* Some SPI controllers do not support commands with writecnt=1 and |
| 176 | * readcnt=4. |
| 177 | */ |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 178 | switch (flash->mst->spi.type) { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 179 | #if CONFIG_INTERNAL == 1 |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 180 | #if defined(__i386__) || defined(__x86_64__) |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 181 | case SPI_CONTROLLER_IT87XX: |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 182 | case SPI_CONTROLLER_WBSIO: |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 183 | msg_cinfo("4 byte RDID not supported on this SPI controller\n"); |
| 184 | return 0; |
| 185 | break; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 186 | #endif |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 187 | #endif |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 188 | default: |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 189 | return probe_spi_rdid_generic(flash, 4); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 195 | int probe_spi_rems(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 196 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 197 | const struct flashchip *chip = flash->chip; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 198 | unsigned char readarr[JEDEC_REMS_INSIZE]; |
| 199 | uint32_t id1, id2; |
| 200 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 201 | if (spi_rems(flash, readarr)) { |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 202 | return 0; |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 203 | } |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 204 | |
| 205 | id1 = readarr[0]; |
| 206 | id2 = readarr[1]; |
| 207 | |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 208 | 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] | 209 | |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 210 | if (id1 == chip->manufacture_id && id2 == chip->model_id) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 211 | return 1; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 212 | |
| 213 | /* Test if this is a pure vendor match. */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 214 | if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 215 | return 1; |
| 216 | |
| 217 | /* Test if there is any vendor ID. */ |
Urja Rannikko | 0a5f6e4 | 2015-06-22 23:59:15 +0000 | [diff] [blame] | 218 | if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff && id1 != 0x00) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 219 | return 1; |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 224 | int probe_spi_res1(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 225 | { |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 226 | static const unsigned char allff[] = {0xff, 0xff, 0xff}; |
| 227 | static const unsigned char all00[] = {0x00, 0x00, 0x00}; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 228 | unsigned char readarr[3]; |
| 229 | uint32_t id2; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 230 | |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 231 | /* We only want one-byte RES if RDID and REMS are unusable. */ |
| 232 | |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 233 | /* Check if RDID is usable and does not return 0xff 0xff 0xff or |
| 234 | * 0x00 0x00 0x00. In that case, RES is pointless. |
| 235 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 236 | if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) && |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 237 | memcmp(readarr, all00, 3)) { |
| 238 | msg_cdbg("Ignoring RES in favour of RDID.\n"); |
| 239 | return 0; |
| 240 | } |
| 241 | /* Check if REMS is usable and does not return 0xff 0xff or |
| 242 | * 0x00 0x00. In that case, RES is pointless. |
| 243 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 244 | if (!spi_rems(flash, readarr) && |
| 245 | memcmp(readarr, allff, JEDEC_REMS_INSIZE) && |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 246 | memcmp(readarr, all00, JEDEC_REMS_INSIZE)) { |
| 247 | msg_cdbg("Ignoring RES in favour of REMS.\n"); |
| 248 | return 0; |
| 249 | } |
| 250 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 251 | if (spi_res(flash, readarr, 1)) { |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 252 | return 0; |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 253 | } |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 254 | |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 255 | id2 = readarr[0]; |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 256 | |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 257 | msg_cdbg("%s: id 0x%x\n", __func__, id2); |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 258 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 259 | if (id2 != flash->chip->model_id) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 260 | return 0; |
| 261 | |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 262 | return 1; |
| 263 | } |
| 264 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 265 | int probe_spi_res2(struct flashctx *flash) |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 266 | { |
| 267 | unsigned char readarr[2]; |
| 268 | uint32_t id1, id2; |
| 269 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 270 | if (spi_res(flash, readarr, 2)) { |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 271 | return 0; |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 272 | } |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 273 | |
| 274 | id1 = readarr[0]; |
| 275 | id2 = readarr[1]; |
| 276 | |
| 277 | msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2); |
| 278 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 279 | if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id) |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 280 | return 0; |
| 281 | |
Carl-Daniel Hailfinger | dc1cda1 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 282 | return 1; |
| 283 | } |
| 284 | |
Stefan Tauner | 3f5e35d | 2013-04-19 01:58:33 +0000 | [diff] [blame] | 285 | int probe_spi_res3(struct flashctx *flash) |
| 286 | { |
| 287 | unsigned char readarr[3]; |
| 288 | uint32_t id1, id2; |
| 289 | |
| 290 | if (spi_res(flash, readarr, 3)) { |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | id1 = (readarr[0] << 8) | readarr[1]; |
| 295 | id2 = readarr[2]; |
| 296 | |
| 297 | msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2); |
| 298 | |
| 299 | if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id) |
| 300 | return 0; |
| 301 | |
| 302 | return 1; |
| 303 | } |
| 304 | |
Stefan Tauner | 57794ac | 2012-12-29 15:04:20 +0000 | [diff] [blame] | 305 | /* Only used for some Atmel chips. */ |
| 306 | int probe_spi_at25f(struct flashctx *flash) |
| 307 | { |
| 308 | static const unsigned char cmd[AT25F_RDID_OUTSIZE] = { AT25F_RDID }; |
| 309 | unsigned char readarr[AT25F_RDID_INSIZE]; |
| 310 | uint32_t id1; |
| 311 | uint32_t id2; |
| 312 | |
| 313 | if (spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr)) |
| 314 | return 0; |
| 315 | |
| 316 | id1 = readarr[0]; |
| 317 | id2 = readarr[1]; |
| 318 | |
| 319 | msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2); |
| 320 | |
| 321 | if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id) |
| 322 | return 1; |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 327 | static int spi_poll_wip(struct flashctx *const flash, const unsigned int poll_delay) |
| 328 | { |
| 329 | /* FIXME: We can't tell if spi_read_status_register() failed. */ |
| 330 | /* FIXME: We don't time out. */ |
| 331 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
| 332 | programmer_delay(poll_delay); |
| 333 | /* FIXME: Check the status register for errors. */ |
| 334 | return 0; |
| 335 | } |
| 336 | |
Nico Huber | a3140d0 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 337 | /** |
| 338 | * Execute WREN plus another one byte `op`, optionally poll WIP afterwards. |
| 339 | * |
| 340 | * @param flash the flash chip's context |
| 341 | * @param op the operation to execute |
| 342 | * @param poll_delay interval in us for polling WIP, don't poll if zero |
| 343 | * @return 0 on success, non-zero otherwise |
| 344 | */ |
| 345 | static int spi_simple_write_cmd(struct flashctx *const flash, const uint8_t op, const unsigned int poll_delay) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 346 | { |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 347 | struct spi_command cmds[] = { |
| 348 | { |
Nico Huber | a3140d0 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 349 | .writecnt = 1, |
| 350 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 351 | }, { |
Nico Huber | a3140d0 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 352 | .writecnt = 1, |
| 353 | .writearr = (const unsigned char[]){ op }, |
| 354 | }, |
| 355 | NULL_SPI_CMD, |
| 356 | }; |
| 357 | |
| 358 | const int result = spi_send_multicommand(flash, cmds); |
| 359 | if (result) |
| 360 | msg_cerr("%s failed during command execution\n", __func__); |
| 361 | |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 362 | const int status = poll_delay ? spi_poll_wip(flash, poll_delay) : 0; |
Nico Huber | a3140d0 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 363 | |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 364 | return result ? result : status; |
| 365 | } |
| 366 | |
Nico Huber | f43c654 | 2017-10-14 17:47:28 +0200 | [diff] [blame] | 367 | static int spi_set_extended_address(struct flashctx *const flash, const uint8_t addr_high) |
| 368 | { |
| 369 | if (flash->address_high_byte != addr_high && |
| 370 | spi_write_extended_address_register(flash, addr_high)) |
| 371 | return -1; |
| 372 | flash->address_high_byte = addr_high; |
| 373 | return 0; |
| 374 | } |
| 375 | |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 376 | static int spi_prepare_address(struct flashctx *const flash, uint8_t cmd_buf[], |
| 377 | const bool native_4ba, const unsigned int addr) |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 378 | { |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 379 | if (native_4ba || flash->in_4ba_mode) { |
Nico Huber | f43c654 | 2017-10-14 17:47:28 +0200 | [diff] [blame] | 380 | cmd_buf[1] = (addr >> 24) & 0xff; |
| 381 | cmd_buf[2] = (addr >> 16) & 0xff; |
| 382 | cmd_buf[3] = (addr >> 8) & 0xff; |
| 383 | cmd_buf[4] = (addr >> 0) & 0xff; |
| 384 | return 4; |
| 385 | } else { |
| 386 | if (flash->chip->feature_bits & FEATURE_4BA_EXT_ADDR) { |
| 387 | if (spi_set_extended_address(flash, addr >> 24)) |
| 388 | return -1; |
| 389 | } else { |
| 390 | if (addr >> 24) |
| 391 | return -1; |
| 392 | } |
| 393 | cmd_buf[1] = (addr >> 16) & 0xff; |
| 394 | cmd_buf[2] = (addr >> 8) & 0xff; |
| 395 | cmd_buf[3] = (addr >> 0) & 0xff; |
| 396 | return 3; |
| 397 | } |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Execute WREN plus another `op` that takes an address and |
| 402 | * optional data, poll WIP afterwards. |
| 403 | * |
| 404 | * @param flash the flash chip's context |
| 405 | * @param op the operation to execute |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 406 | * @param native_4ba whether `op` always takes a 4-byte address |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 407 | * @param addr the address parameter to `op` |
| 408 | * @param out_bytes bytes to send after the address, |
| 409 | * may be NULL if and only if `out_bytes` is 0 |
| 410 | * @param out_bytes number of bytes to send, 256 at most, may be zero |
| 411 | * @param poll_delay interval in us for polling WIP |
| 412 | * @return 0 on success, non-zero otherwise |
| 413 | */ |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 414 | static int spi_write_cmd(struct flashctx *const flash, const uint8_t op, |
| 415 | const bool native_4ba, const unsigned int addr, |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 416 | const uint8_t *const out_bytes, const size_t out_len, |
| 417 | const unsigned int poll_delay) |
| 418 | { |
| 419 | uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN + 256]; |
| 420 | struct spi_command cmds[] = { |
| 421 | { |
| 422 | .writecnt = 1, |
| 423 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 424 | }, { |
| 425 | .writearr = cmd, |
| 426 | }, |
| 427 | NULL_SPI_CMD, |
| 428 | }; |
| 429 | |
| 430 | cmd[0] = op; |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 431 | const int addr_len = spi_prepare_address(flash, cmd, native_4ba, addr); |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 432 | if (addr_len < 0) |
| 433 | return 1; |
| 434 | |
| 435 | if (1 + addr_len + out_len > sizeof(cmd)) { |
| 436 | msg_cerr("%s called for too long a write\n", __func__); |
| 437 | return 1; |
| 438 | } |
| 439 | |
| 440 | memcpy(cmd + 1 + addr_len, out_bytes, out_len); |
| 441 | cmds[1].writecnt = 1 + addr_len + out_len; |
| 442 | |
| 443 | const int result = spi_send_multicommand(flash, cmds); |
| 444 | if (result) |
| 445 | msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr); |
| 446 | |
| 447 | const int status = spi_poll_wip(flash, poll_delay); |
| 448 | |
| 449 | return result ? result : status; |
Nico Huber | a3140d0 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | int spi_chip_erase_60(struct flashctx *flash) |
| 453 | { |
| 454 | /* This usually takes 1-85s, so wait in 1s steps. */ |
| 455 | return spi_simple_write_cmd(flash, 0x60, 1000 * 1000); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Stefan Tauner | 3c0fcd0 | 2012-09-21 12:46:56 +0000 | [diff] [blame] | 458 | int spi_chip_erase_62(struct flashctx *flash) |
| 459 | { |
Nico Huber | a3140d0 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 460 | /* This usually takes 2-5s, so wait in 100ms steps. */ |
| 461 | return spi_simple_write_cmd(flash, 0x62, 100 * 1000); |
Stefan Tauner | 3c0fcd0 | 2012-09-21 12:46:56 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 464 | int spi_chip_erase_c7(struct flashctx *flash) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 465 | { |
Nico Huber | a3140d0 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 466 | /* This usually takes 1-85s, so wait in 1s steps. */ |
| 467 | return spi_simple_write_cmd(flash, 0xc7, 1000 * 1000); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 470 | int spi_block_erase_52(struct flashctx *flash, unsigned int addr, |
| 471 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 472 | { |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 473 | /* This usually takes 100-4000ms, so wait in 100ms steps. */ |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 474 | return spi_write_cmd(flash, 0x52, false, addr, NULL, 0, 100 * 1000); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 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 | { |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 482 | /* This usually takes 240-480s, so wait in 500ms steps. */ |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 483 | return spi_write_cmd(flash, 0xc4, false, addr, NULL, 0, 500 * 1000); |
Nikolay Nikolaev | 6f59b0b | 2013-06-28 21:29:51 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /* Block size is usually |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 487 | * 64k for Macronix |
| 488 | * 32k for SST |
| 489 | * 4-32k non-uniform for EON |
| 490 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 491 | int spi_block_erase_d8(struct flashctx *flash, unsigned int addr, |
| 492 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 493 | { |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 494 | /* This usually takes 100-4000ms, so wait in 100ms steps. */ |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 495 | return spi_write_cmd(flash, 0xd8, false, addr, NULL, 0, 100 * 1000); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | /* Block size is usually |
| 499 | * 4k for PMC |
| 500 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 501 | int spi_block_erase_d7(struct flashctx *flash, unsigned int addr, |
| 502 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 503 | { |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 504 | /* This usually takes 100-4000ms, so wait in 100ms steps. */ |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 505 | return spi_write_cmd(flash, 0xd7, false, addr, NULL, 0, 100 * 1000); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Nikolay Nikolaev | 579f1e0 | 2013-06-28 21:28:37 +0000 | [diff] [blame] | 508 | /* Page erase (usually 256B blocks) */ |
| 509 | int spi_block_erase_db(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 510 | { |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 511 | /* This takes up to 20ms usually (on worn out devices |
| 512 | up to the 0.5s range), so wait in 1ms steps. */ |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 513 | return spi_write_cmd(flash, 0xdb, false, addr, NULL, 0, 1 * 1000); |
Nikolay Nikolaev | 579f1e0 | 2013-06-28 21:28:37 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 516 | /* Sector size is usually 4k, though Macronix eliteflash has 64k */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 517 | int spi_block_erase_20(struct flashctx *flash, unsigned int addr, |
| 518 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 519 | { |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 520 | /* This usually takes 15-800ms, so wait in 10ms steps. */ |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 521 | return spi_write_cmd(flash, 0x20, false, addr, NULL, 0, 10 * 1000); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Stefan Tauner | 94b39b4 | 2012-10-27 00:06:02 +0000 | [diff] [blame] | 524 | int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 525 | { |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 526 | /* This usually takes 10ms, so wait in 1ms steps. */ |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 527 | return spi_write_cmd(flash, 0x50, false, addr, NULL, 0, 1 * 1000); |
Stefan Tauner | 94b39b4 | 2012-10-27 00:06:02 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 531 | { |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 532 | /* This usually takes 8ms, so wait in 1ms steps. */ |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 533 | return spi_write_cmd(flash, 0x81, false, addr, NULL, 0, 1 * 1000); |
Stefan Tauner | 94b39b4 | 2012-10-27 00:06:02 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 536 | int spi_block_erase_60(struct flashctx *flash, unsigned int addr, |
| 537 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 538 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 539 | if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 540 | msg_cerr("%s called with incorrect arguments\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 541 | __func__); |
| 542 | return -1; |
| 543 | } |
| 544 | return spi_chip_erase_60(flash); |
| 545 | } |
| 546 | |
Stefan Tauner | 3c0fcd0 | 2012-09-21 12:46:56 +0000 | [diff] [blame] | 547 | int spi_block_erase_62(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 548 | { |
| 549 | if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) { |
| 550 | msg_cerr("%s called with incorrect arguments\n", |
| 551 | __func__); |
| 552 | return -1; |
| 553 | } |
| 554 | return spi_chip_erase_62(flash); |
| 555 | } |
| 556 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 557 | int spi_block_erase_c7(struct flashctx *flash, unsigned int addr, |
| 558 | unsigned int blocklen) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 559 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 560 | if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 561 | msg_cerr("%s called with incorrect arguments\n", |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 562 | __func__); |
| 563 | return -1; |
| 564 | } |
| 565 | return spi_chip_erase_c7(flash); |
| 566 | } |
| 567 | |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 568 | erasefunc_t *spi_get_erasefn_from_opcode(uint8_t opcode) |
| 569 | { |
| 570 | switch(opcode){ |
| 571 | case 0xff: |
| 572 | case 0x00: |
| 573 | /* Not specified, assuming "not supported". */ |
| 574 | return NULL; |
| 575 | case 0x20: |
| 576 | return &spi_block_erase_20; |
Stefan Tauner | 730e7e7 | 2013-05-01 14:04:19 +0000 | [diff] [blame] | 577 | case 0x50: |
| 578 | return &spi_block_erase_50; |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 579 | case 0x52: |
| 580 | return &spi_block_erase_52; |
| 581 | case 0x60: |
| 582 | return &spi_block_erase_60; |
Stefan Tauner | 730e7e7 | 2013-05-01 14:04:19 +0000 | [diff] [blame] | 583 | case 0x62: |
| 584 | return &spi_block_erase_62; |
| 585 | case 0x81: |
| 586 | return &spi_block_erase_81; |
Nikolay Nikolaev | 6f59b0b | 2013-06-28 21:29:51 +0000 | [diff] [blame] | 587 | case 0xc4: |
| 588 | return &spi_block_erase_c4; |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 589 | case 0xc7: |
| 590 | return &spi_block_erase_c7; |
| 591 | case 0xd7: |
| 592 | return &spi_block_erase_d7; |
| 593 | case 0xd8: |
| 594 | return &spi_block_erase_d8; |
Nikolay Nikolaev | 579f1e0 | 2013-06-28 21:28:37 +0000 | [diff] [blame] | 595 | case 0xdb: |
| 596 | return &spi_block_erase_db; |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 597 | default: |
| 598 | msg_cinfo("%s: unknown erase opcode (0x%02x). Please report " |
| 599 | "this at flashrom@flashrom.org\n", __func__, opcode); |
| 600 | return NULL; |
| 601 | } |
| 602 | } |
| 603 | |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 604 | static 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] | 605 | { |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 606 | const bool native_4ba = !!(flash->chip->feature_bits & FEATURE_4BA_WRITE); |
| 607 | const uint8_t op = native_4ba ? JEDEC_BYTE_PROGRAM_4BA : JEDEC_BYTE_PROGRAM; |
| 608 | return spi_write_cmd(flash, op, native_4ba, addr, bytes, len, 10); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 611 | int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes, |
| 612 | unsigned int len) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 613 | { |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 614 | const bool native_4ba = !!(flash->chip->feature_bits & FEATURE_4BA_READ); |
| 615 | uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN] = { native_4ba ? JEDEC_READ_4BA : JEDEC_READ, }; |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 616 | |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 617 | const int addr_len = spi_prepare_address(flash, cmd, native_4ba, address); |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 618 | if (addr_len < 0) |
| 619 | return 1; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 620 | |
| 621 | /* Send Read */ |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 622 | return spi_send_command(flash, 1 + addr_len, len, cmd, bytes); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /* |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 626 | * Read a part of the flash chip. |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 627 | * FIXME: Use the chunk code from Michael Karcher instead. |
Urja Rannikko | 731316a | 2017-06-15 13:32:01 +0300 | [diff] [blame] | 628 | * 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] | 629 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 630 | int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 631 | unsigned int len, unsigned int chunksize) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 632 | { |
| 633 | int rc = 0; |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 634 | unsigned int i, j, starthere, lenhere, toread; |
Urja Rannikko | 731316a | 2017-06-15 13:32:01 +0300 | [diff] [blame] | 635 | /* Limit for multi-die 4-byte-addressing chips. */ |
| 636 | 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] | 637 | |
| 638 | /* Warning: This loop has a very unusual condition and body. |
Urja Rannikko | 731316a | 2017-06-15 13:32:01 +0300 | [diff] [blame] | 639 | * The loop needs to go through each area with at least one affected |
| 640 | * byte. The lowest area number is (start / area_size) since that |
| 641 | * division rounds down. The highest area number we want is the area |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 642 | * 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] | 643 | * address (start + len - 1), thus the highest area number is |
| 644 | * (start + len - 1) / area_size. Since we want to include that last |
| 645 | * area as well, the loop condition uses <=. |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 646 | */ |
Urja Rannikko | 731316a | 2017-06-15 13:32:01 +0300 | [diff] [blame] | 647 | for (i = start / area_size; i <= (start + len - 1) / area_size; i++) { |
| 648 | /* Byte position of the first byte in the range in this area. */ |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 649 | /* starthere is an offset to the base address of the chip. */ |
Urja Rannikko | 731316a | 2017-06-15 13:32:01 +0300 | [diff] [blame] | 650 | starthere = max(start, i * area_size); |
| 651 | /* Length of bytes in the range in this area. */ |
| 652 | lenhere = min(start + len, (i + 1) * area_size) - starthere; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 653 | for (j = 0; j < lenhere; j += chunksize) { |
| 654 | toread = min(chunksize, lenhere - j); |
Nico Huber | 7a07722 | 2017-10-14 18:18:30 +0200 | [diff] [blame] | 655 | rc = spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 656 | if (rc) |
| 657 | break; |
| 658 | } |
| 659 | if (rc) |
| 660 | break; |
| 661 | } |
| 662 | |
| 663 | return rc; |
| 664 | } |
| 665 | |
| 666 | /* |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 667 | * Write a part of the flash chip. |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 668 | * FIXME: Use the chunk code from Michael Karcher instead. |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 669 | * Each page is written separately in chunks with a maximum size of chunksize. |
| 670 | */ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 671 | 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] | 672 | unsigned int len, unsigned int chunksize) |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 673 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 674 | unsigned int i, j, starthere, lenhere, towrite; |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 675 | /* 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] | 676 | * in struct flashctx to do this properly. All chips using |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 677 | * spi_chip_write_256 have page_size set to max_writechunk_size, so |
| 678 | * we're OK for now. |
| 679 | */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 680 | unsigned int page_size = flash->chip->page_size; |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 681 | |
| 682 | /* Warning: This loop has a very unusual condition and body. |
| 683 | * The loop needs to go through each page with at least one affected |
| 684 | * byte. The lowest page number is (start / page_size) since that |
| 685 | * division rounds down. The highest page number we want is the page |
| 686 | * where the last byte of the range lives. That last byte has the |
| 687 | * address (start + len - 1), thus the highest page number is |
| 688 | * (start + len - 1) / page_size. Since we want to include that last |
| 689 | * page as well, the loop condition uses <=. |
| 690 | */ |
| 691 | for (i = start / page_size; i <= (start + len - 1) / page_size; i++) { |
| 692 | /* Byte position of the first byte in the range in this page. */ |
| 693 | /* starthere is an offset to the base address of the chip. */ |
| 694 | starthere = max(start, i * page_size); |
| 695 | /* Length of bytes in the range in this page. */ |
| 696 | lenhere = min(start + len, (i + 1) * page_size) - starthere; |
| 697 | for (j = 0; j < lenhere; j += chunksize) { |
Nico Huber | 7a07722 | 2017-10-14 18:18:30 +0200 | [diff] [blame] | 698 | int rc; |
| 699 | |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 700 | towrite = min(chunksize, lenhere - j); |
Nico Huber | 7a07722 | 2017-10-14 18:18:30 +0200 | [diff] [blame] | 701 | 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] | 702 | if (rc) |
Nico Huber | 7a07722 | 2017-10-14 18:18:30 +0200 | [diff] [blame] | 703 | return rc; |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 704 | } |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Nico Huber | 7a07722 | 2017-10-14 18:18:30 +0200 | [diff] [blame] | 707 | return 0; |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /* |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 711 | * Program chip using byte programming. (SLOW!) |
| 712 | * This is for chips which can only handle one byte writes |
| 713 | * and for chips where memory mapped programming is impossible |
| 714 | * (e.g. due to size constraints in IT87* for over 512 kB) |
| 715 | */ |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 716 | /* real chunksize is 1, logical chunksize is 1 */ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 717 | 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] | 718 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 719 | unsigned int i; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 720 | |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 721 | for (i = start; i < start + len; i++) { |
Nico Huber | 7a07722 | 2017-10-14 18:18:30 +0200 | [diff] [blame] | 722 | if (spi_nbyte_program(flash, i, buf + i - start, 1)) |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 723 | return 1; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 724 | } |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 725 | return 0; |
| 726 | } |
| 727 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 728 | 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] | 729 | { |
| 730 | uint32_t pos = start; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 731 | int result; |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 732 | unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = { |
| 733 | JEDEC_AAI_WORD_PROGRAM, |
| 734 | }; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 735 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 736 | switch (flash->mst->spi.type) { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 737 | #if CONFIG_INTERNAL == 1 |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 738 | #if defined(__i386__) || defined(__x86_64__) |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 739 | case SPI_CONTROLLER_IT87XX: |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 740 | case SPI_CONTROLLER_WBSIO: |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 741 | msg_perr("%s: impossible with this SPI controller," |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 742 | " degrading to byte program\n", __func__); |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 743 | return spi_chip_write_1(flash, buf, start, len); |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 744 | #endif |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 745 | #endif |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 746 | default: |
| 747 | break; |
| 748 | } |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 749 | |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 750 | /* The even start address and even length requirements can be either |
| 751 | * honored outside this function, or we can call spi_byte_program |
| 752 | * 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] | 753 | * FIXME: Move this to generic code. |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 754 | */ |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 755 | /* 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] | 756 | if (start % 2) { |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 757 | msg_cerr("%s: start address not even! Please report a bug at " |
| 758 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 759 | if (spi_chip_write_1(flash, buf, start, start % 2)) |
| 760 | return SPI_GENERIC_ERROR; |
| 761 | pos += start % 2; |
| 762 | /* Do not return an error for now. */ |
| 763 | //return SPI_GENERIC_ERROR; |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 764 | } |
| 765 | /* The data sheet requires total AAI write length to be even. */ |
| 766 | if (len % 2) { |
| 767 | msg_cerr("%s: total write length not even! Please report a " |
| 768 | "bug at flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 769 | /* Do not return an error for now. */ |
| 770 | //return SPI_GENERIC_ERROR; |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Nico Huber | a1672f8 | 2017-10-14 18:00:20 +0200 | [diff] [blame] | 773 | result = spi_write_cmd(flash, JEDEC_AAI_WORD_PROGRAM, false, start, buf + pos - start, 2, 10); |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 774 | if (result) |
Stefan Reinauer | 87ace66 | 2014-04-26 16:12:55 +0000 | [diff] [blame] | 775 | goto bailout; |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 776 | |
| 777 | /* We already wrote 2 bytes in the multicommand step. */ |
| 778 | pos += 2; |
| 779 | |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 780 | /* Are there at least two more bytes to write? */ |
| 781 | while (pos < start + len - 1) { |
Carl-Daniel Hailfinger | ccfe0ac | 2010-10-27 22:07:11 +0000 | [diff] [blame] | 782 | cmd[1] = buf[pos++ - start]; |
| 783 | cmd[2] = buf[pos++ - start]; |
Stefan Reinauer | 87ace66 | 2014-04-26 16:12:55 +0000 | [diff] [blame] | 784 | result = spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL); |
| 785 | if (result != 0) { |
| 786 | msg_cerr("%s failed during followup AAI command execution: %d\n", __func__, result); |
| 787 | goto bailout; |
| 788 | } |
Nico Huber | 0ecbacb | 2017-10-14 16:50:43 +0200 | [diff] [blame] | 789 | if (spi_poll_wip(flash, 10)) |
| 790 | goto bailout; |
Carl-Daniel Hailfinger | 9c62d11 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Stefan Tauner | 59c4d79 | 2014-04-26 16:13:09 +0000 | [diff] [blame] | 793 | /* Use WRDI to exit AAI mode. This needs to be done before issuing any other non-AAI command. */ |
| 794 | result = spi_write_disable(flash); |
| 795 | if (result != 0) { |
| 796 | msg_cerr("%s failed to disable AAI mode.\n", __func__); |
| 797 | return SPI_GENERIC_ERROR; |
| 798 | } |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 799 | |
| 800 | /* Write remaining byte (if any). */ |
| 801 | if (pos < start + len) { |
Carl-Daniel Hailfinger | ccfe0ac | 2010-10-27 22:07:11 +0000 | [diff] [blame] | 802 | 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] | 803 | return SPI_GENERIC_ERROR; |
| 804 | pos += pos % 2; |
| 805 | } |
| 806 | |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 807 | return 0; |
Stefan Reinauer | 87ace66 | 2014-04-26 16:12:55 +0000 | [diff] [blame] | 808 | |
| 809 | bailout: |
Stefan Tauner | 59c4d79 | 2014-04-26 16:13:09 +0000 | [diff] [blame] | 810 | result = spi_write_disable(flash); |
| 811 | if (result != 0) |
| 812 | msg_cerr("%s failed to disable AAI mode.\n", __func__); |
Stefan Reinauer | 87ace66 | 2014-04-26 16:12:55 +0000 | [diff] [blame] | 813 | return SPI_GENERIC_ERROR; |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 814 | } |