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