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