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