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