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