snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007, 2008, 2009, 2010 Carl-Daniel Hailfinger |
snelson | 8913d08 | 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. |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Contains the common SPI chip driver functions |
| 19 | */ |
| 20 | |
Nico Huber | 4c8a956 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 21 | #include <stddef.h> |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 22 | #include <string.h> |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 23 | #include <stdbool.h> |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 24 | #include "flash.h" |
| 25 | #include "flashchips.h" |
| 26 | #include "chipdrivers.h" |
hailfinger | 428f685 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 27 | #include "programmer.h" |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 28 | #include "spi.h" |
Boris Baykov | 1a2f532 | 2016-06-11 18:29:00 +0200 | [diff] [blame] | 29 | #include "spi4ba.h" |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 30 | |
David Hendricks | 57b7524 | 2015-11-20 15:54:07 -0800 | [diff] [blame] | 31 | enum id_type { |
| 32 | RDID, |
| 33 | RDID4, |
| 34 | REMS, |
| 35 | // RES1, /* TODO */ |
| 36 | RES2, |
Nikolai Artemiev | 4702c7c | 2020-08-31 12:49:50 +1000 | [diff] [blame^] | 37 | RES3, |
David Hendricks | 57b7524 | 2015-11-20 15:54:07 -0800 | [diff] [blame] | 38 | NUM_ID_TYPES, |
| 39 | }; |
| 40 | |
| 41 | static struct { |
| 42 | int is_cached; |
| 43 | unsigned char bytes[4]; /* enough to hold largest ID type */ |
| 44 | } id_cache[NUM_ID_TYPES]; |
| 45 | |
| 46 | void clear_spi_id_cache(void) |
| 47 | { |
| 48 | memset(id_cache, 0, sizeof(id_cache)); |
| 49 | return; |
| 50 | } |
| 51 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 52 | static int spi_rdid(struct flashctx *flash, unsigned char *readarr, int bytes) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 53 | { |
krause | 2eb7621 | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 54 | static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID }; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 55 | int ret; |
| 56 | int i; |
| 57 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 58 | ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 59 | if (ret) |
| 60 | return ret; |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 61 | msg_cspew("RDID returned"); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 62 | for (i = 0; i < bytes; i++) |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 63 | msg_cspew(" 0x%02x", readarr[i]); |
| 64 | msg_cspew(". "); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 68 | static int spi_rems(struct flashctx *flash, unsigned char *readarr) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 69 | { |
Edward O'Callaghan | dfb7154 | 2020-05-14 18:41:42 +1000 | [diff] [blame] | 70 | static const unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, }; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 71 | int ret; |
| 72 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 73 | ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 74 | if (ret) |
| 75 | return ret; |
stefanct | 371e7e8 | 2011-07-07 19:56:58 +0000 | [diff] [blame] | 76 | msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 80 | static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 81 | { |
Edward O'Callaghan | dfb7154 | 2020-05-14 18:41:42 +1000 | [diff] [blame] | 82 | static const unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, }; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 83 | int ret; |
hailfinger | cb0564e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 84 | int i; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 85 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 86 | ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 87 | if (ret) |
| 88 | return ret; |
hailfinger | cb0564e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 89 | msg_cspew("RES returned"); |
| 90 | for (i = 0; i < bytes; i++) |
| 91 | msg_cspew(" 0x%02x", readarr[i]); |
| 92 | msg_cspew(". "); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 96 | int spi_write_enable(struct flashctx *flash) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 97 | { |
krause | 2eb7621 | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 98 | static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN }; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 99 | int result; |
| 100 | |
| 101 | /* Send WREN (Write Enable) */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 102 | result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 103 | |
| 104 | if (result) |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 105 | msg_cerr("%s failed\n", __func__); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 106 | |
| 107 | return result; |
| 108 | } |
| 109 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 110 | int spi_write_disable(struct flashctx *flash) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 111 | { |
krause | 2eb7621 | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 112 | static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI }; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 113 | |
| 114 | /* Send WRDI (Write Disable) */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 115 | return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 116 | } |
| 117 | |
David Hendricks | 7f7c711 | 2012-10-11 17:15:48 -0700 | [diff] [blame] | 118 | static void rdid_get_ids(unsigned char *readarr, |
| 119 | int bytes, uint32_t *id1, uint32_t *id2) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 120 | { |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 121 | if (!oddparity(readarr[0])) |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 122 | msg_cdbg("RDID byte 0 parity violation. "); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 123 | |
hailfinger | cb0564e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 124 | /* Check if this is a continuation vendor ID. |
| 125 | * FIXME: Handle continuation device IDs. |
| 126 | */ |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 127 | if (readarr[0] == 0x7f) { |
| 128 | if (!oddparity(readarr[1])) |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 129 | msg_cdbg("RDID byte 1 parity violation. "); |
David Hendricks | 7f7c711 | 2012-10-11 17:15:48 -0700 | [diff] [blame] | 130 | *id1 = (readarr[0] << 8) | readarr[1]; |
| 131 | *id2 = readarr[2]; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 132 | if (bytes > 3) { |
David Hendricks | 7f7c711 | 2012-10-11 17:15:48 -0700 | [diff] [blame] | 133 | *id2 <<= 8; |
| 134 | *id2 |= readarr[3]; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 135 | } |
| 136 | } else { |
David Hendricks | 7f7c711 | 2012-10-11 17:15:48 -0700 | [diff] [blame] | 137 | *id1 = readarr[0]; |
| 138 | *id2 = (readarr[1] << 8) | readarr[2]; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 139 | } |
David Hendricks | 7f7c711 | 2012-10-11 17:15:48 -0700 | [diff] [blame] | 140 | } |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 141 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 142 | static int compare_id(struct flashctx *flash, uint32_t id1, uint32_t id2) |
David Hendricks | 7f7c711 | 2012-10-11 17:15:48 -0700 | [diff] [blame] | 143 | { |
| 144 | msg_cdbg("id1 0x%02x, id2 0x%02x\n", id1, id2); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 145 | |
Edward O'Callaghan | 71e2314 | 2019-03-03 23:08:22 +1100 | [diff] [blame] | 146 | if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 147 | return 1; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 148 | |
| 149 | /* Test if this is a pure vendor match. */ |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 150 | if (id1 == flash->chip->manufacture_id && |
| 151 | GENERIC_DEVICE_ID == flash->chip->model_id) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 152 | return 1; |
| 153 | |
| 154 | /* Test if there is any vendor ID. */ |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 155 | if (GENERIC_MANUF_ID == flash->chip->manufacture_id && |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 156 | id1 != 0xff) |
| 157 | return 1; |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Edward O'Callaghan | c2a12a9 | 2020-05-14 17:58:32 +1000 | [diff] [blame] | 162 | static int probe_spi_rdid_generic(struct flashctx *flash, int bytes) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 163 | { |
David Hendricks | 57b7524 | 2015-11-20 15:54:07 -0800 | [diff] [blame] | 164 | uint32_t id1, id2; |
Edward O'Callaghan | c2a12a9 | 2020-05-14 17:58:32 +1000 | [diff] [blame] | 165 | enum id_type idty = bytes == 3 ? RDID : RDID4; |
David Hendricks | 7f7c711 | 2012-10-11 17:15:48 -0700 | [diff] [blame] | 166 | |
Edward O'Callaghan | c2a12a9 | 2020-05-14 17:58:32 +1000 | [diff] [blame] | 167 | if (!id_cache[idty].is_cached) { |
| 168 | const int ret = spi_rdid(flash, id_cache[idty].bytes, bytes); |
| 169 | if (ret == SPI_INVALID_LENGTH) |
| 170 | msg_cinfo("%d byte RDID not supported on this SPI controller\n", bytes); |
| 171 | if (ret) |
David Hendricks | 7f7c711 | 2012-10-11 17:15:48 -0700 | [diff] [blame] | 172 | return 0; |
Edward O'Callaghan | c2a12a9 | 2020-05-14 17:58:32 +1000 | [diff] [blame] | 173 | id_cache[idty].is_cached = 1; |
David Hendricks | 7f7c711 | 2012-10-11 17:15:48 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Edward O'Callaghan | c2a12a9 | 2020-05-14 17:58:32 +1000 | [diff] [blame] | 176 | rdid_get_ids(id_cache[idty].bytes, bytes, &id1, &id2); |
David Hendricks | 7f7c711 | 2012-10-11 17:15:48 -0700 | [diff] [blame] | 177 | return compare_id(flash, id1, id2); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Edward O'Callaghan | c2a12a9 | 2020-05-14 17:58:32 +1000 | [diff] [blame] | 180 | int probe_spi_rdid(struct flashctx *flash) |
| 181 | { |
| 182 | return probe_spi_rdid_generic(flash, 3); |
| 183 | } |
| 184 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 185 | int probe_spi_rdid4(struct flashctx *flash) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 186 | { |
Edward O'Callaghan | c2a12a9 | 2020-05-14 17:58:32 +1000 | [diff] [blame] | 187 | return probe_spi_rdid_generic(flash, 4); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 190 | int probe_spi_rems(struct flashctx *flash) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 191 | { |
David Hendricks | 57b7524 | 2015-11-20 15:54:07 -0800 | [diff] [blame] | 192 | uint32_t id1, id2; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 193 | |
David Hendricks | 57b7524 | 2015-11-20 15:54:07 -0800 | [diff] [blame] | 194 | if (!id_cache[REMS].is_cached) { |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 195 | if (spi_rems(flash, id_cache[REMS].bytes)) |
David Hendricks | 7f7c711 | 2012-10-11 17:15:48 -0700 | [diff] [blame] | 196 | return 0; |
David Hendricks | 57b7524 | 2015-11-20 15:54:07 -0800 | [diff] [blame] | 197 | id_cache[REMS].is_cached = 1; |
stefanct | 9e6b98a | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 198 | } |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 199 | |
David Hendricks | 57b7524 | 2015-11-20 15:54:07 -0800 | [diff] [blame] | 200 | id1 = id_cache[REMS].bytes[0]; |
| 201 | id2 = id_cache[REMS].bytes[1]; |
David Hendricks | 7f7c711 | 2012-10-11 17:15:48 -0700 | [diff] [blame] | 202 | return compare_id(flash, id1, id2); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 205 | int probe_spi_res1(struct flashctx *flash) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 206 | { |
krause | 2eb7621 | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 207 | static const unsigned char allff[] = {0xff, 0xff, 0xff}; |
| 208 | static const unsigned char all00[] = {0x00, 0x00, 0x00}; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 209 | unsigned char readarr[3]; |
| 210 | uint32_t id2; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 211 | |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 212 | /* We only want one-byte RES if RDID and REMS are unusable. */ |
| 213 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 214 | /* Check if RDID is usable and does not return 0xff 0xff 0xff or |
| 215 | * 0x00 0x00 0x00. In that case, RES is pointless. |
| 216 | */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 217 | if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) && |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 218 | memcmp(readarr, all00, 3)) { |
| 219 | msg_cdbg("Ignoring RES in favour of RDID.\n"); |
| 220 | return 0; |
| 221 | } |
| 222 | /* Check if REMS is usable and does not return 0xff 0xff or |
| 223 | * 0x00 0x00. In that case, RES is pointless. |
| 224 | */ |
Edward O'Callaghan | 3eaadb0 | 2019-10-14 16:08:23 +1100 | [diff] [blame] | 225 | if (!spi_rems(flash, readarr) && |
| 226 | memcmp(readarr, allff, JEDEC_REMS_INSIZE) && |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 227 | memcmp(readarr, all00, JEDEC_REMS_INSIZE)) { |
| 228 | msg_cdbg("Ignoring RES in favour of REMS.\n"); |
| 229 | return 0; |
| 230 | } |
| 231 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 232 | if (spi_res(flash, readarr, 1)) { |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 233 | return 0; |
stefanct | 9e6b98a | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 234 | } |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 235 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 236 | id2 = readarr[0]; |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 237 | |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 238 | msg_cdbg("%s: id 0x%x\n", __func__, id2); |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 239 | |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 240 | if (id2 != flash->chip->model_id) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 241 | return 0; |
| 242 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 243 | return 1; |
| 244 | } |
| 245 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 246 | int probe_spi_res2(struct flashctx *flash) |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 247 | { |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 248 | uint32_t id1, id2; |
| 249 | |
David Hendricks | 57b7524 | 2015-11-20 15:54:07 -0800 | [diff] [blame] | 250 | if (!id_cache[RES2].is_cached) { |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 251 | if (spi_res(flash, id_cache[RES2].bytes, 2)) |
David Hendricks | 57b7524 | 2015-11-20 15:54:07 -0800 | [diff] [blame] | 252 | return 0; |
| 253 | id_cache[RES2].is_cached = 1; |
stefanct | 9e6b98a | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 254 | } |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 255 | |
David Hendricks | 57b7524 | 2015-11-20 15:54:07 -0800 | [diff] [blame] | 256 | id1 = id_cache[RES2].bytes[0]; |
| 257 | id2 = id_cache[RES2].bytes[1]; |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 258 | msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2); |
| 259 | |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 260 | if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id) |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 261 | return 0; |
| 262 | |
hailfinger | 59a8357 | 2010-05-28 17:07:57 +0000 | [diff] [blame] | 263 | return 1; |
| 264 | } |
| 265 | |
Nikolai Artemiev | 4702c7c | 2020-08-31 12:49:50 +1000 | [diff] [blame^] | 266 | int probe_spi_res3(struct flashctx *flash) |
| 267 | { |
| 268 | uint32_t id1, id2; |
| 269 | |
| 270 | if (!id_cache[RES3].is_cached) { |
| 271 | if (spi_res(flash, id_cache[RES3].bytes, 3)) |
| 272 | return 0; |
| 273 | id_cache[RES3].is_cached = 1; |
| 274 | } |
| 275 | |
| 276 | id1 = (id_cache[RES3].bytes[0] << 8) | id_cache[RES3].bytes[1]; |
| 277 | id2 = id_cache[RES3].bytes[3]; |
| 278 | msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2); |
| 279 | |
| 280 | if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id) |
| 281 | return 0; |
| 282 | |
| 283 | return 1; |
| 284 | } |
| 285 | |
| 286 | /* Only used for some Atmel chips. */ |
| 287 | int probe_spi_at25f(struct flashctx *flash) |
| 288 | { |
| 289 | static const unsigned char cmd[AT25F_RDID_OUTSIZE] = { AT25F_RDID }; |
| 290 | unsigned char readarr[AT25F_RDID_INSIZE]; |
| 291 | uint32_t id1; |
| 292 | uint32_t id2; |
| 293 | |
| 294 | if (spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr)) |
| 295 | return 0; |
| 296 | |
| 297 | id1 = readarr[0]; |
| 298 | id2 = readarr[1]; |
| 299 | |
| 300 | msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2); |
| 301 | |
| 302 | if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id) |
| 303 | return 1; |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 308 | static int spi_poll_wip(struct flashctx *const flash, const unsigned int poll_delay) |
| 309 | { |
| 310 | /* FIXME: We can't tell if spi_read_status_register() failed. */ |
| 311 | /* FIXME: We don't time out. */ |
| 312 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
| 313 | programmer_delay(poll_delay); |
| 314 | /* FIXME: Check the status register for errors. */ |
| 315 | return 0; |
| 316 | } |
| 317 | |
Nico Huber | 4c8a956 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 318 | /** |
| 319 | * Execute WREN plus another one byte `op`, optionally poll WIP afterwards. |
| 320 | * |
| 321 | * @param flash the flash chip's context |
| 322 | * @param op the operation to execute |
| 323 | * @param poll_delay interval in us for polling WIP, don't poll if zero |
| 324 | * @return 0 on success, non-zero otherwise |
| 325 | */ |
| 326 | static int spi_simple_write_cmd(struct flashctx *const flash, const uint8_t op, const unsigned int poll_delay) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 327 | { |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 328 | struct spi_command cmds[] = { |
| 329 | { |
Nico Huber | 4c8a956 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 330 | .writecnt = 1, |
| 331 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 332 | }, { |
Nico Huber | 4c8a956 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 333 | .writecnt = 1, |
| 334 | .writearr = (const unsigned char[]){ op }, |
| 335 | }, |
| 336 | NULL_SPI_CMD, |
| 337 | }; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 338 | |
Nico Huber | 4c8a956 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 339 | const int result = spi_send_multicommand(flash, cmds); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 340 | if (result) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 341 | msg_cerr("%s failed during command execution\n", __func__); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 342 | return result; |
| 343 | } |
| 344 | /* Wait until the Write-In-Progress bit is cleared. |
| 345 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 346 | */ |
Nico Huber | 4c8a956 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 347 | |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 348 | const int status = poll_delay ? spi_poll_wip(flash, poll_delay) : 0; |
| 349 | |
| 350 | return result ? result : status; |
| 351 | } |
| 352 | |
Edward O'Callaghan | a74ffcd | 2019-06-17 14:59:55 +1000 | [diff] [blame] | 353 | static int spi_set_extended_address(struct flashctx *const flash, const uint8_t addr_high) |
| 354 | { |
| 355 | if (flash->address_high_byte != addr_high && |
| 356 | spi_write_extended_address_register(flash, addr_high)) |
| 357 | return -1; |
| 358 | flash->address_high_byte = addr_high; |
| 359 | return 0; |
| 360 | } |
| 361 | |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 362 | static int spi_prepare_address(struct flashctx *const flash, uint8_t cmd_buf[], |
| 363 | const bool native_4ba, const unsigned int addr) |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 364 | { |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 365 | if (native_4ba || flash->in_4ba_mode) { |
Edward O'Callaghan | a6673bd | 2019-06-24 15:22:28 +1000 | [diff] [blame] | 366 | if (!spi_master_4ba(flash)) { |
| 367 | msg_cwarn("4-byte address requested but master can't handle 4-byte addresses.\n"); |
| 368 | return -1; |
| 369 | } |
Edward O'Callaghan | a74ffcd | 2019-06-17 14:59:55 +1000 | [diff] [blame] | 370 | cmd_buf[1] = (addr >> 24) & 0xff; |
| 371 | cmd_buf[2] = (addr >> 16) & 0xff; |
| 372 | cmd_buf[3] = (addr >> 8) & 0xff; |
| 373 | cmd_buf[4] = (addr >> 0) & 0xff; |
| 374 | return 4; |
| 375 | } else { |
| 376 | if (flash->chip->feature_bits & FEATURE_4BA_EXT_ADDR) { |
| 377 | if (spi_set_extended_address(flash, addr >> 24)) |
| 378 | return -1; |
Edward O'Callaghan | a6673bd | 2019-06-24 15:22:28 +1000 | [diff] [blame] | 379 | } else if (addr >> 24) { |
| 380 | msg_cerr("Can't handle 4-byte address for opcode '0x%02x'\n" |
| 381 | "with this chip/programmer combination.\n", cmd_buf[0]); |
Edward O'Callaghan | 3eaadb0 | 2019-10-14 16:08:23 +1100 | [diff] [blame] | 382 | return -1; |
Edward O'Callaghan | a74ffcd | 2019-06-17 14:59:55 +1000 | [diff] [blame] | 383 | } |
| 384 | cmd_buf[1] = (addr >> 16) & 0xff; |
| 385 | cmd_buf[2] = (addr >> 8) & 0xff; |
| 386 | cmd_buf[3] = (addr >> 0) & 0xff; |
| 387 | return 3; |
| 388 | } |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Execute WREN plus another `op` that takes an address and |
| 393 | * optional data, poll WIP afterwards. |
| 394 | * |
| 395 | * @param flash the flash chip's context |
| 396 | * @param op the operation to execute |
Edward O'Callaghan | 3eaadb0 | 2019-10-14 16:08:23 +1100 | [diff] [blame] | 397 | * @param native_4ba whether `op` always takes a 4-byte address |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 398 | * @param addr the address parameter to `op` |
| 399 | * @param out_bytes bytes to send after the address, |
| 400 | * may be NULL if and only if `out_bytes` is 0 |
| 401 | * @param out_bytes number of bytes to send, 256 at most, may be zero |
| 402 | * @param poll_delay interval in us for polling WIP |
| 403 | * @return 0 on success, non-zero otherwise |
| 404 | */ |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 405 | static int spi_write_cmd(struct flashctx *const flash, const uint8_t op, |
| 406 | const bool native_4ba, const unsigned int addr, |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 407 | const uint8_t *const out_bytes, const size_t out_len, |
| 408 | const unsigned int poll_delay) |
| 409 | { |
| 410 | uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN + 256]; |
| 411 | struct spi_command cmds[] = { |
| 412 | { |
| 413 | .writecnt = 1, |
| 414 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 415 | }, { |
| 416 | .writearr = cmd, |
| 417 | }, |
| 418 | NULL_SPI_CMD, |
| 419 | }; |
| 420 | |
| 421 | cmd[0] = op; |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 422 | const int addr_len = spi_prepare_address(flash, cmd, native_4ba, addr); |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 423 | if (addr_len < 0) |
| 424 | return 1; |
| 425 | |
| 426 | if (1 + addr_len + out_len > sizeof(cmd)) { |
| 427 | msg_cerr("%s called for too long a write\n", __func__); |
| 428 | return 1; |
| 429 | } |
Angel Pons | 6bfd9e6 | 2020-03-31 15:32:10 +0200 | [diff] [blame] | 430 | if (!out_bytes && out_len > 0) |
| 431 | return 1; |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 432 | |
| 433 | memcpy(cmd + 1 + addr_len, out_bytes, out_len); |
| 434 | cmds[1].writecnt = 1 + addr_len + out_len; |
| 435 | |
| 436 | const int result = spi_send_multicommand(flash, cmds); |
| 437 | if (result) |
| 438 | msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr); |
| 439 | |
| 440 | const int status = spi_poll_wip(flash, poll_delay); |
| 441 | |
| 442 | return result ? result : status; |
Nico Huber | 4c8a956 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | int spi_chip_erase_60(struct flashctx *flash) |
| 446 | { |
| 447 | /* This usually takes 1-85s, so wait in 1s steps. */ |
| 448 | return spi_simple_write_cmd(flash, 0x60, 1000 * 1000); |
| 449 | } |
| 450 | |
| 451 | int spi_chip_erase_62(struct flashctx *flash) |
| 452 | { |
| 453 | /* This usually takes 2-5s, so wait in 100ms steps. */ |
| 454 | return spi_simple_write_cmd(flash, 0x62, 100 * 1000); |
| 455 | } |
| 456 | |
| 457 | int spi_chip_erase_c7(struct flashctx *flash) |
| 458 | { |
| 459 | /* This usually takes 1-85s, so wait in 1s steps. */ |
| 460 | return spi_simple_write_cmd(flash, 0xc7, 1000 * 1000); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Edward O'Callaghan | 3eaadb0 | 2019-10-14 16:08:23 +1100 | [diff] [blame] | 463 | int spi_block_erase_52(struct flashctx *flash, unsigned int addr, |
| 464 | unsigned int blocklen) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 465 | { |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 466 | /* This usually takes 100-4000ms, so wait in 100ms steps. */ |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 467 | return spi_write_cmd(flash, 0x52, false, addr, NULL, 0, 100 * 1000); |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 468 | } |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 469 | |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 470 | /* Block size is usually |
| 471 | * 32M (one die) for Micron |
| 472 | */ |
| 473 | int spi_block_erase_c4(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 474 | { |
| 475 | /* This usually takes 240-480s, so wait in 500ms steps. */ |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 476 | return spi_write_cmd(flash, 0xc4, false, addr, NULL, 0, 500 * 1000); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* Block size is usually |
| 480 | * 64k for Macronix |
| 481 | * 32k for SST |
| 482 | * 4-32k non-uniform for EON |
| 483 | */ |
Edward O'Callaghan | 3eaadb0 | 2019-10-14 16:08:23 +1100 | [diff] [blame] | 484 | int spi_block_erase_d8(struct flashctx *flash, unsigned int addr, |
| 485 | unsigned int blocklen) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 486 | { |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 487 | /* This usually takes 100-4000ms, so wait in 100ms steps. */ |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 488 | return spi_write_cmd(flash, 0xd8, false, addr, NULL, 0, 100 * 1000); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /* Block size is usually |
| 492 | * 4k for PMC |
| 493 | */ |
Edward O'Callaghan | 3eaadb0 | 2019-10-14 16:08:23 +1100 | [diff] [blame] | 494 | int spi_block_erase_d7(struct flashctx *flash, unsigned int addr, |
| 495 | unsigned int blocklen) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 496 | { |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 497 | /* This usually takes 100-4000ms, so wait in 100ms steps. */ |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 498 | return spi_write_cmd(flash, 0xd7, false, addr, NULL, 0, 100 * 1000); |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 499 | } |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 500 | |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 501 | /* Page erase (usually 256B blocks) */ |
| 502 | int spi_block_erase_db(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 503 | { |
| 504 | /* This takes up to 20ms usually (on worn out devices |
| 505 | up to the 0.5s range), so wait in 1ms steps. */ |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 506 | return spi_write_cmd(flash, 0xdb, false, addr, NULL, 0, 1 * 1000); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 507 | } |
| 508 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 509 | /* Sector size is usually 4k, though Macronix eliteflash has 64k */ |
Edward O'Callaghan | 3eaadb0 | 2019-10-14 16:08:23 +1100 | [diff] [blame] | 510 | int spi_block_erase_20(struct flashctx *flash, unsigned int addr, |
| 511 | unsigned int blocklen) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 512 | { |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 513 | /* This usually takes 15-800ms, so wait in 10ms steps. */ |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 514 | return spi_write_cmd(flash, 0x20, false, addr, NULL, 0, 10 * 1000); |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 515 | } |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 516 | |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 517 | int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 518 | { |
| 519 | /* This usually takes 10ms, so wait in 1ms steps. */ |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 520 | return spi_write_cmd(flash, 0x50, false, addr, NULL, 0, 1 * 1000); |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 521 | } |
Stefan Reinauer | cce56d5 | 2010-11-22 18:22:21 -0800 | [diff] [blame] | 522 | |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 523 | int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 524 | { |
| 525 | /* This usually takes 8ms, so wait in 1ms steps. */ |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 526 | return spi_write_cmd(flash, 0x81, false, addr, NULL, 0, 1 * 1000); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Edward O'Callaghan | 3eaadb0 | 2019-10-14 16:08:23 +1100 | [diff] [blame] | 529 | int spi_block_erase_60(struct flashctx *flash, unsigned int addr, |
| 530 | unsigned int blocklen) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 531 | { |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 532 | if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 533 | msg_cerr("%s called with incorrect arguments\n", |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 534 | __func__); |
| 535 | return -1; |
| 536 | } |
| 537 | return spi_chip_erase_60(flash); |
| 538 | } |
| 539 | |
Alan Green | 5d70973 | 2019-09-16 12:32:25 +1000 | [diff] [blame] | 540 | int spi_block_erase_62(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 541 | { |
| 542 | if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) { |
| 543 | msg_cerr("%s called with incorrect arguments\n", |
| 544 | __func__); |
| 545 | return -1; |
| 546 | } |
| 547 | return spi_chip_erase_62(flash); |
| 548 | } |
| 549 | |
Edward O'Callaghan | 3eaadb0 | 2019-10-14 16:08:23 +1100 | [diff] [blame] | 550 | int spi_block_erase_c7(struct flashctx *flash, unsigned int addr, |
| 551 | unsigned int blocklen) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 552 | { |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 553 | if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 554 | msg_cerr("%s called with incorrect arguments\n", |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 555 | __func__); |
| 556 | return -1; |
| 557 | } |
| 558 | return spi_chip_erase_c7(flash); |
| 559 | } |
| 560 | |
Edward O'Callaghan | 94934e8 | 2019-06-19 17:44:19 +1000 | [diff] [blame] | 561 | /* Erase 4 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) |
| 562 | JEDEC_SE_4BA (21h) instruction is new for 4-bytes addressing flash chips. |
| 563 | The presence of this instruction for an exact chip should be checked |
| 564 | by its datasheet or from SFDP 4-Bytes Address Instruction Table (JESD216B). */ |
| 565 | int spi_block_erase_21(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 566 | { |
| 567 | /* This usually takes 15-800ms, so wait in 10ms steps. */ |
| 568 | return spi_write_cmd(flash, 0x21, true, addr, NULL, 0, 10 * 1000); |
| 569 | } |
| 570 | |
| 571 | /* Erase 32 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) |
| 572 | JEDEC_BE_5C_4BA (5Ch) instruction is new for 4-bytes addressing flash chips. |
| 573 | The presence of this instruction for an exact chip should be checked |
| 574 | by its datasheet or from SFDP 4-Bytes Address Instruction Table (JESD216B). */ |
| 575 | int spi_block_erase_5c(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 576 | { |
| 577 | /* This usually takes 100-4000ms, so wait in 100ms steps. */ |
| 578 | return spi_write_cmd(flash, 0x5c, true, addr, NULL, 0, 100 * 1000); |
| 579 | } |
| 580 | |
| 581 | /* Erase 64 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) |
| 582 | JEDEC_BE_DC_4BA (DCh) instruction is new for 4-bytes addressing flash chips. |
| 583 | The presence of this instruction for an exact chip should be checked |
| 584 | by its datasheet or from SFDP 4-Bytes Address Instruction Table (JESD216B). */ |
| 585 | int spi_block_erase_dc(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 586 | { |
| 587 | /* This usually takes 100-4000ms, so wait in 100ms steps. */ |
| 588 | return spi_write_cmd(flash, 0xdc, true, addr, NULL, 0, 100 * 1000); |
| 589 | } |
| 590 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 591 | int spi_write_status_register_wren(const struct flashctx *flash, int status) |
hailfinger | c33d473 | 2010-07-29 13:09:18 +0000 | [diff] [blame] | 592 | { |
| 593 | int result; |
hailfinger | ee9ee13 | 2010-10-08 00:37:55 +0000 | [diff] [blame] | 594 | int i = 0; |
hailfinger | c33d473 | 2010-07-29 13:09:18 +0000 | [diff] [blame] | 595 | struct spi_command cmds[] = { |
| 596 | { |
| 597 | /* WRSR requires either EWSR or WREN depending on chip type. */ |
| 598 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 599 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 600 | .readcnt = 0, |
| 601 | .readarr = NULL, |
| 602 | }, { |
| 603 | .writecnt = JEDEC_WRSR_OUTSIZE, |
| 604 | .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status }, |
| 605 | .readcnt = 0, |
| 606 | .readarr = NULL, |
| 607 | }, { |
| 608 | .writecnt = 0, |
| 609 | .writearr = NULL, |
| 610 | .readcnt = 0, |
| 611 | .readarr = NULL, |
| 612 | }}; |
| 613 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 614 | result = spi_send_multicommand(flash, cmds); |
hailfinger | c33d473 | 2010-07-29 13:09:18 +0000 | [diff] [blame] | 615 | if (result) { |
| 616 | msg_cerr("%s failed during command execution\n", |
| 617 | __func__); |
hailfinger | ee9ee13 | 2010-10-08 00:37:55 +0000 | [diff] [blame] | 618 | /* No point in waiting for the command to complete if execution |
| 619 | * failed. |
| 620 | */ |
| 621 | return result; |
hailfinger | c33d473 | 2010-07-29 13:09:18 +0000 | [diff] [blame] | 622 | } |
hailfinger | ee9ee13 | 2010-10-08 00:37:55 +0000 | [diff] [blame] | 623 | /* WRSR performs a self-timed erase before the changes take effect. |
| 624 | * This may take 50-85 ms in most cases, and some chips apparently |
| 625 | * allow running RDSR only once. Therefore pick an initial delay of |
| 626 | * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed. |
| 627 | */ |
hailfinger | c33d473 | 2010-07-29 13:09:18 +0000 | [diff] [blame] | 628 | programmer_delay(100 * 1000); |
Edward O'Callaghan | 8b5e473 | 2019-03-05 15:27:53 +1100 | [diff] [blame] | 629 | while (spi_read_status_register(flash) & SPI_SR_WIP) { |
hailfinger | ee9ee13 | 2010-10-08 00:37:55 +0000 | [diff] [blame] | 630 | if (++i > 490) { |
| 631 | msg_cerr("Error: WIP bit after WRSR never cleared\n"); |
| 632 | return TIMEOUT_ERROR; |
| 633 | } |
| 634 | programmer_delay(10 * 1000); |
| 635 | } |
| 636 | return 0; |
hailfinger | c33d473 | 2010-07-29 13:09:18 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 639 | int spi_byte_program(struct flashctx *flash, unsigned int addr, uint8_t databyte) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 640 | { |
| 641 | int result; |
| 642 | struct spi_command cmds[] = { |
| 643 | { |
| 644 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 645 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 646 | .readcnt = 0, |
| 647 | .readarr = NULL, |
| 648 | }, { |
| 649 | .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE, |
| 650 | .writearr = (const unsigned char[]){ |
| 651 | JEDEC_BYTE_PROGRAM, |
| 652 | (addr >> 16) & 0xff, |
| 653 | (addr >> 8) & 0xff, |
| 654 | (addr & 0xff), |
| 655 | databyte |
| 656 | }, |
| 657 | .readcnt = 0, |
| 658 | .readarr = NULL, |
| 659 | }, { |
| 660 | .writecnt = 0, |
| 661 | .writearr = NULL, |
| 662 | .readcnt = 0, |
| 663 | .readarr = NULL, |
| 664 | }}; |
| 665 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 666 | result = spi_send_multicommand(flash, cmds); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 667 | if (result) { |
snelson | fc007bb | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 668 | msg_cerr("%s failed during command execution at address 0x%x\n", |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 669 | __func__, addr); |
| 670 | } |
| 671 | return result; |
| 672 | } |
| 673 | |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 674 | static int spi_nbyte_program(struct flashctx *flash, unsigned int addr, const uint8_t *bytes, unsigned int len) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 675 | { |
Edward O'Callaghan | a6673bd | 2019-06-24 15:22:28 +1000 | [diff] [blame] | 676 | const bool native_4ba = flash->chip->feature_bits & FEATURE_4BA_WRITE && spi_master_4ba(flash); |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 677 | const uint8_t op = native_4ba ? JEDEC_BYTE_PROGRAM_4BA : JEDEC_BYTE_PROGRAM; |
| 678 | return spi_write_cmd(flash, op, native_4ba, addr, bytes, len, 10); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Edward O'Callaghan | 3eaadb0 | 2019-10-14 16:08:23 +1100 | [diff] [blame] | 681 | int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes, |
| 682 | unsigned int len) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 683 | { |
Edward O'Callaghan | a6673bd | 2019-06-24 15:22:28 +1000 | [diff] [blame] | 684 | const bool native_4ba = flash->chip->feature_bits & FEATURE_4BA_READ && spi_master_4ba(flash); |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 685 | uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN] = { native_4ba ? JEDEC_READ_4BA : JEDEC_READ, }; |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 686 | |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 687 | const int addr_len = spi_prepare_address(flash, cmd, native_4ba, address); |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 688 | if (addr_len < 0) |
| 689 | return 1; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 690 | |
| 691 | /* Send Read */ |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 692 | return spi_send_command(flash, 1 + addr_len, len, cmd, bytes); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /* |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 696 | * Read a part of the flash chip. |
hailfinger | c7d06c6 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 697 | * FIXME: Use the chunk code from Michael Karcher instead. |
Edward O'Callaghan | d825ac0 | 2019-07-26 21:36:16 +1000 | [diff] [blame] | 698 | * Each naturally aligned area is read separately in chunks with a maximum size of chunksize. |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 699 | */ |
Edward O'Callaghan | 3eaadb0 | 2019-10-14 16:08:23 +1100 | [diff] [blame] | 700 | int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 701 | unsigned int len, unsigned int chunksize) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 702 | { |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 703 | int rc = 0, chunk_status = 0; |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 704 | unsigned int i, j, starthere, lenhere, toread; |
Edward O'Callaghan | d825ac0 | 2019-07-26 21:36:16 +1000 | [diff] [blame] | 705 | /* Limit for multi-die 4-byte-addressing chips. */ |
| 706 | unsigned int area_size = min(flash->chip->total_size * 1024, 16 * 1024 * 1024); |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 707 | |
| 708 | /* Warning: This loop has a very unusual condition and body. |
Edward O'Callaghan | d825ac0 | 2019-07-26 21:36:16 +1000 | [diff] [blame] | 709 | * The loop needs to go through each area with at least one affected |
| 710 | * byte. The lowest area number is (start / area_size) since that |
| 711 | * division rounds down. The highest area number we want is the area |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 712 | * where the last byte of the range lives. That last byte has the |
Edward O'Callaghan | d825ac0 | 2019-07-26 21:36:16 +1000 | [diff] [blame] | 713 | * address (start + len - 1), thus the highest area number is |
| 714 | * (start + len - 1) / area_size. Since we want to include that last |
| 715 | * area as well, the loop condition uses <=. |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 716 | */ |
Edward O'Callaghan | d825ac0 | 2019-07-26 21:36:16 +1000 | [diff] [blame] | 717 | for (i = start / area_size; i <= (start + len - 1) / area_size; i++) { |
| 718 | /* Byte position of the first byte in the range in this area. */ |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 719 | /* starthere is an offset to the base address of the chip. */ |
Edward O'Callaghan | d825ac0 | 2019-07-26 21:36:16 +1000 | [diff] [blame] | 720 | starthere = max(start, i * area_size); |
| 721 | /* Length of bytes in the range in this area. */ |
| 722 | lenhere = min(start + len, (i + 1) * area_size) - starthere; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 723 | for (j = 0; j < lenhere; j += chunksize) { |
| 724 | toread = min(chunksize, lenhere - j); |
Edward O'Callaghan | 4fe3a97 | 2019-06-19 16:56:10 +1000 | [diff] [blame] | 725 | chunk_status = spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread); |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 726 | if (chunk_status) { |
| 727 | if (ignore_error(chunk_status)) { |
| 728 | /* fill this chunk with 0xff bytes and |
| 729 | let caller know about the error */ |
| 730 | memset(buf + starthere - start + j, 0xff, toread); |
| 731 | rc = chunk_status; |
| 732 | chunk_status = 0; |
| 733 | continue; |
| 734 | } else { |
| 735 | rc = chunk_status; |
| 736 | break; |
| 737 | } |
| 738 | } |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 739 | } |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 740 | if (chunk_status) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 741 | break; |
| 742 | } |
| 743 | |
| 744 | return rc; |
| 745 | } |
| 746 | |
| 747 | /* |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 748 | * Write a part of the flash chip. |
hailfinger | c7d06c6 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 749 | * FIXME: Use the chunk code from Michael Karcher instead. |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 750 | * Each page is written separately in chunks with a maximum size of chunksize. |
| 751 | */ |
Edward O'Callaghan | 3eaadb0 | 2019-10-14 16:08:23 +1100 | [diff] [blame] | 752 | int spi_write_chunked(struct flashctx *flash, const uint8_t *buf, unsigned int start, |
| 753 | unsigned int len, unsigned int chunksize) |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 754 | { |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 755 | unsigned int i, j, starthere, lenhere, towrite; |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 756 | /* FIXME: page_size is the wrong variable. We need max_writechunk_size |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 757 | * in struct flashctx to do this properly. All chips using |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 758 | * spi_chip_write_256 have page_size set to max_writechunk_size, so |
| 759 | * we're OK for now. |
| 760 | */ |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 761 | unsigned int page_size = flash->chip->page_size; |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 762 | |
| 763 | /* Warning: This loop has a very unusual condition and body. |
| 764 | * The loop needs to go through each page with at least one affected |
| 765 | * byte. The lowest page number is (start / page_size) since that |
| 766 | * division rounds down. The highest page number we want is the page |
| 767 | * where the last byte of the range lives. That last byte has the |
| 768 | * address (start + len - 1), thus the highest page number is |
| 769 | * (start + len - 1) / page_size. Since we want to include that last |
| 770 | * page as well, the loop condition uses <=. |
| 771 | */ |
| 772 | for (i = start / page_size; i <= (start + len - 1) / page_size; i++) { |
| 773 | /* Byte position of the first byte in the range in this page. */ |
| 774 | /* starthere is an offset to the base address of the chip. */ |
| 775 | starthere = max(start, i * page_size); |
| 776 | /* Length of bytes in the range in this page. */ |
| 777 | lenhere = min(start + len, (i + 1) * page_size) - starthere; |
| 778 | for (j = 0; j < lenhere; j += chunksize) { |
Edward O'Callaghan | 4fe3a97 | 2019-06-19 16:56:10 +1000 | [diff] [blame] | 779 | int rc; |
Edward O'Callaghan | 3eaadb0 | 2019-10-14 16:08:23 +1100 | [diff] [blame] | 780 | |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 781 | towrite = min(chunksize, lenhere - j); |
Edward O'Callaghan | 4fe3a97 | 2019-06-19 16:56:10 +1000 | [diff] [blame] | 782 | rc = spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite); |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 783 | if (rc) |
Edward O'Callaghan | 4fe3a97 | 2019-06-19 16:56:10 +1000 | [diff] [blame] | 784 | return rc; |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 785 | } |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Edward O'Callaghan | 4fe3a97 | 2019-06-19 16:56:10 +1000 | [diff] [blame] | 788 | return 0; |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | /* |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 792 | * Program chip using byte programming. (SLOW!) |
| 793 | * This is for chips which can only handle one byte writes |
| 794 | * and for chips where memory mapped programming is impossible |
| 795 | * (e.g. due to size constraints in IT87* for over 512 kB) |
| 796 | */ |
hailfinger | c7d06c6 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 797 | /* real chunksize is 1, logical chunksize is 1 */ |
Patrick Georgi | ab8353e | 2017-02-03 18:32:01 +0100 | [diff] [blame] | 798 | int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 799 | { |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 800 | unsigned int i; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 801 | |
hailfinger | c7d06c6 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 802 | for (i = start; i < start + len; i++) { |
Edward O'Callaghan | 4fe3a97 | 2019-06-19 16:56:10 +1000 | [diff] [blame] | 803 | if (spi_nbyte_program(flash, i, buf + i - start, 1)) |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 804 | return 1; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 805 | } |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 806 | return 0; |
| 807 | } |
| 808 | |
Patrick Georgi | ab8353e | 2017-02-03 18:32:01 +0100 | [diff] [blame] | 809 | int spi_aai_write(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
hailfinger | c7d06c6 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 810 | { |
| 811 | uint32_t pos = start; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 812 | int result; |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 813 | unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = { |
| 814 | JEDEC_AAI_WORD_PROGRAM, |
| 815 | }; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 816 | |
hailfinger | c7d06c6 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 817 | /* The even start address and even length requirements can be either |
| 818 | * honored outside this function, or we can call spi_byte_program |
| 819 | * for the first and/or last byte and use AAI for the rest. |
hailfinger | 71e1bd4 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 820 | * FIXME: Move this to generic code. |
hailfinger | c7d06c6 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 821 | */ |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 822 | /* The data sheet requires a start address with the low bit cleared. */ |
hailfinger | c7d06c6 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 823 | if (start % 2) { |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 824 | msg_cerr("%s: start address not even! Please report a bug at " |
| 825 | "flashrom@flashrom.org\n", __func__); |
hailfinger | 71e1bd4 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 826 | if (spi_chip_write_1(flash, buf, start, start % 2)) |
| 827 | return SPI_GENERIC_ERROR; |
| 828 | pos += start % 2; |
| 829 | /* Do not return an error for now. */ |
| 830 | //return SPI_GENERIC_ERROR; |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 831 | } |
| 832 | /* The data sheet requires total AAI write length to be even. */ |
| 833 | if (len % 2) { |
| 834 | msg_cerr("%s: total write length not even! Please report a " |
| 835 | "bug at flashrom@flashrom.org\n", __func__); |
hailfinger | 71e1bd4 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 836 | /* Do not return an error for now. */ |
| 837 | //return SPI_GENERIC_ERROR; |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Edward O'Callaghan | 031831d | 2019-06-19 16:27:43 +1000 | [diff] [blame] | 840 | result = spi_write_cmd(flash, JEDEC_AAI_WORD_PROGRAM, false, start, buf + pos - start, 2, 10); |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 841 | if (result) |
Edward O'Callaghan | 633cbd6 | 2019-06-17 15:43:56 +1000 | [diff] [blame] | 842 | goto bailout; |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 843 | |
| 844 | /* We already wrote 2 bytes in the multicommand step. */ |
| 845 | pos += 2; |
| 846 | |
hailfinger | 71e1bd4 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 847 | /* Are there at least two more bytes to write? */ |
| 848 | while (pos < start + len - 1) { |
hailfinger | def852d | 2010-10-27 22:07:11 +0000 | [diff] [blame] | 849 | cmd[1] = buf[pos++ - start]; |
| 850 | cmd[2] = buf[pos++ - start]; |
Edward O'Callaghan | 633cbd6 | 2019-06-17 15:43:56 +1000 | [diff] [blame] | 851 | result = spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL); |
Edward O'Callaghan | 3eaadb0 | 2019-10-14 16:08:23 +1100 | [diff] [blame] | 852 | if (result != 0) { |
Edward O'Callaghan | 633cbd6 | 2019-06-17 15:43:56 +1000 | [diff] [blame] | 853 | msg_cerr("%s failed during followup AAI command execution: %d\n", __func__, result); |
| 854 | goto bailout; |
| 855 | } |
Edward O'Callaghan | e5190df | 2019-06-17 15:23:26 +1000 | [diff] [blame] | 856 | if (spi_poll_wip(flash, 10)) |
| 857 | goto bailout; |
hailfinger | 19db092 | 2010-06-20 10:41:35 +0000 | [diff] [blame] | 858 | } |
| 859 | |
hailfinger | 71e1bd4 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 860 | /* Use WRDI to exit AAI mode. This needs to be done before issuing any |
| 861 | * other non-AAI command. |
| 862 | */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 863 | spi_write_disable(flash); |
hailfinger | 71e1bd4 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 864 | |
| 865 | /* Write remaining byte (if any). */ |
| 866 | if (pos < start + len) { |
hailfinger | def852d | 2010-10-27 22:07:11 +0000 | [diff] [blame] | 867 | if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2)) |
hailfinger | 71e1bd4 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 868 | return SPI_GENERIC_ERROR; |
hailfinger | 71e1bd4 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 869 | } |
| 870 | |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 871 | return 0; |
Edward O'Callaghan | 633cbd6 | 2019-06-17 15:43:56 +1000 | [diff] [blame] | 872 | |
| 873 | bailout: |
| 874 | spi_write_disable(flash); |
| 875 | return SPI_GENERIC_ERROR; |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 876 | } |