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