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