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