Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Carl-Daniel Hailfinger | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007, 2008, 2009 Carl-Daniel Hailfinger |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008 coresystems GmbH |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 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 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Contains the generic SPI framework |
| 23 | */ |
| 24 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include "flash.h" |
Carl-Daniel Hailfinger | 0845464 | 2009-06-15 14:14:48 +0000 | [diff] [blame] | 27 | #include "flashchips.h" |
Carl-Daniel Hailfinger | d6cbf76 | 2008-05-13 14:58:23 +0000 | [diff] [blame] | 28 | #include "spi.h" |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 29 | |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 30 | enum spi_controller spi_controller = SPI_CONTROLLER_NONE; |
| 31 | void *spibar = NULL; |
| 32 | |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 33 | void spi_prettyprint_status_register(struct flashchip *flash); |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 34 | |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 35 | const struct spi_programmer spi_programmer[] = { |
| 36 | { /* SPI_CONTROLLER_NONE */ |
| 37 | .command = NULL, |
| 38 | .multicommand = NULL, |
| 39 | .read = NULL, |
| 40 | .write_256 = NULL, |
| 41 | }, |
| 42 | |
| 43 | { /* SPI_CONTROLLER_ICH7 */ |
| 44 | .command = ich_spi_send_command, |
| 45 | .multicommand = ich_spi_send_multicommand, |
| 46 | .read = ich_spi_read, |
| 47 | .write_256 = ich_spi_write_256, |
| 48 | }, |
| 49 | |
| 50 | { /* SPI_CONTROLLER_ICH9 */ |
| 51 | .command = ich_spi_send_command, |
| 52 | .multicommand = ich_spi_send_multicommand, |
| 53 | .read = ich_spi_read, |
| 54 | .write_256 = ich_spi_write_256, |
| 55 | }, |
| 56 | |
| 57 | { /* SPI_CONTROLLER_IT87XX */ |
| 58 | .command = it8716f_spi_send_command, |
| 59 | .multicommand = default_spi_send_multicommand, |
| 60 | .read = it8716f_spi_chip_read, |
| 61 | .write_256 = it8716f_spi_chip_write_256, |
| 62 | }, |
| 63 | |
| 64 | { /* SPI_CONTROLLER_SB600 */ |
| 65 | .command = sb600_spi_send_command, |
| 66 | .multicommand = default_spi_send_multicommand, |
| 67 | .read = sb600_spi_read, |
| 68 | .write_256 = sb600_spi_write_1, |
| 69 | }, |
| 70 | |
| 71 | { /* SPI_CONTROLLER_VIA */ |
| 72 | .command = ich_spi_send_command, |
| 73 | .multicommand = ich_spi_send_multicommand, |
| 74 | .read = ich_spi_read, |
| 75 | .write_256 = ich_spi_write_256, |
| 76 | }, |
| 77 | |
| 78 | { /* SPI_CONTROLLER_WBSIO */ |
| 79 | .command = wbsio_spi_send_command, |
| 80 | .multicommand = default_spi_send_multicommand, |
| 81 | .read = wbsio_spi_read, |
| 82 | .write_256 = wbsio_spi_write_1, |
| 83 | }, |
| 84 | |
| 85 | { /* SPI_CONTROLLER_FT2232 */ |
| 86 | .command = ft2232_spi_send_command, |
| 87 | .multicommand = default_spi_send_multicommand, |
| 88 | .read = ft2232_spi_read, |
| 89 | .write_256 = ft2232_spi_write_256, |
| 90 | }, |
| 91 | |
| 92 | { /* SPI_CONTROLLER_DUMMY */ |
| 93 | .command = dummy_spi_send_command, |
| 94 | .multicommand = default_spi_send_multicommand, |
| 95 | .read = NULL, |
| 96 | .write_256 = NULL, |
| 97 | }, |
| 98 | }; |
| 99 | |
| 100 | |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 101 | int spi_send_command(unsigned int writecnt, unsigned int readcnt, |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 102 | const unsigned char *writearr, unsigned char *readarr) |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 103 | { |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 104 | if (!spi_programmer[spi_controller].command) { |
| 105 | fprintf(stderr, "%s called, but SPI is unsupported on this " |
| 106 | "hardware. Please report a bug.\n", __func__); |
| 107 | return 1; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 108 | } |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 109 | |
| 110 | return spi_programmer[spi_controller].command(writecnt, readcnt, |
| 111 | writearr, readarr); |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 114 | int spi_send_multicommand(struct spi_command *spicommands) |
| 115 | { |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 116 | if (!spi_programmer[spi_controller].multicommand) { |
| 117 | fprintf(stderr, "%s called, but SPI is unsupported on this " |
| 118 | "hardware. Please report a bug.\n", __func__); |
| 119 | return 1; |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 120 | } |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 121 | |
| 122 | return spi_programmer[spi_controller].multicommand(spicommands); |
| 123 | } |
| 124 | |
| 125 | int default_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 126 | const unsigned char *writearr, unsigned char *readarr) |
| 127 | { |
| 128 | struct spi_command cmd[] = { |
| 129 | { |
| 130 | .writecnt = writecnt, |
| 131 | .readcnt = readcnt, |
| 132 | .writearr = writearr, |
| 133 | .readarr = readarr, |
| 134 | }, { |
| 135 | .writecnt = 0, |
| 136 | .writearr = NULL, |
| 137 | .readcnt = 0, |
| 138 | .readarr = NULL, |
| 139 | }}; |
| 140 | |
| 141 | return spi_send_multicommand(cmd); |
| 142 | } |
| 143 | |
| 144 | int default_spi_send_multicommand(struct spi_command *spicommands) |
| 145 | { |
| 146 | int result = 0; |
| 147 | while ((spicommands->writecnt || spicommands->readcnt) && !result) { |
| 148 | result = spi_send_command(spicommands->writecnt, spicommands->readcnt, |
| 149 | spicommands->writearr, spicommands->readarr); |
Carl-Daniel Hailfinger | 5b2f52f | 2009-08-03 09:35:20 +0000 | [diff] [blame] | 150 | spicommands++; |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 151 | } |
| 152 | return result; |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Rudolf Marek | 48a85e4 | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 155 | static int spi_rdid(unsigned char *readarr, int bytes) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 156 | { |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 157 | const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID }; |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 158 | int ret; |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 159 | int i; |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 160 | |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 161 | ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr); |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 162 | if (ret) |
| 163 | return ret; |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 164 | printf_debug("RDID returned"); |
| 165 | for (i = 0; i < bytes; i++) |
| 166 | printf_debug(" 0x%02x", readarr[i]); |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 167 | printf_debug(". "); |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
Carl-Daniel Hailfinger | 14e50ac | 2008-11-28 01:25:00 +0000 | [diff] [blame] | 171 | static int spi_rems(unsigned char *readarr) |
| 172 | { |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 173 | unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 }; |
| 174 | uint32_t readaddr; |
| 175 | int ret; |
Carl-Daniel Hailfinger | 14e50ac | 2008-11-28 01:25:00 +0000 | [diff] [blame] | 176 | |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 177 | ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr); |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 178 | if (ret == SPI_INVALID_ADDRESS) { |
| 179 | /* Find the lowest even address allowed for reads. */ |
| 180 | readaddr = (spi_get_valid_read_addr() + 1) & ~1; |
| 181 | cmd[1] = (readaddr >> 16) & 0xff, |
| 182 | cmd[2] = (readaddr >> 8) & 0xff, |
| 183 | cmd[3] = (readaddr >> 0) & 0xff, |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 184 | ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr); |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 185 | } |
| 186 | if (ret) |
| 187 | return ret; |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 188 | printf_debug("REMS returned %02x %02x. ", readarr[0], readarr[1]); |
Carl-Daniel Hailfinger | 14e50ac | 2008-11-28 01:25:00 +0000 | [diff] [blame] | 189 | return 0; |
| 190 | } |
| 191 | |
Carl-Daniel Hailfinger | 42c5497 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 192 | static int spi_res(unsigned char *readarr) |
| 193 | { |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 194 | unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 }; |
| 195 | uint32_t readaddr; |
| 196 | int ret; |
Carl-Daniel Hailfinger | 42c5497 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 197 | |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 198 | ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr); |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 199 | if (ret == SPI_INVALID_ADDRESS) { |
| 200 | /* Find the lowest even address allowed for reads. */ |
| 201 | readaddr = (spi_get_valid_read_addr() + 1) & ~1; |
| 202 | cmd[1] = (readaddr >> 16) & 0xff, |
| 203 | cmd[2] = (readaddr >> 8) & 0xff, |
| 204 | cmd[3] = (readaddr >> 0) & 0xff, |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 205 | ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr); |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 206 | } |
| 207 | if (ret) |
| 208 | return ret; |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 209 | printf_debug("RES returned %02x. ", readarr[0]); |
Carl-Daniel Hailfinger | 42c5497 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 213 | int spi_write_enable(void) |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 214 | { |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 215 | const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN }; |
Carl-Daniel Hailfinger | 03adbe1 | 2009-05-09 02:09:45 +0000 | [diff] [blame] | 216 | int result; |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 217 | |
| 218 | /* Send WREN (Write Enable) */ |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 219 | result = spi_send_command(sizeof(cmd), 0, cmd, NULL); |
Carl-Daniel Hailfinger | 1e63784 | 2009-05-15 00:56:22 +0000 | [diff] [blame] | 220 | |
| 221 | if (result) |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 222 | fprintf(stderr, "%s failed\n", __func__); |
Carl-Daniel Hailfinger | 1e63784 | 2009-05-15 00:56:22 +0000 | [diff] [blame] | 223 | |
Carl-Daniel Hailfinger | 03adbe1 | 2009-05-09 02:09:45 +0000 | [diff] [blame] | 224 | return result; |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 227 | int spi_write_disable(void) |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 228 | { |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 229 | const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI }; |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 230 | |
| 231 | /* Send WRDI (Write Disable) */ |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 232 | return spi_send_command(sizeof(cmd), 0, cmd, NULL); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Rudolf Marek | 48a85e4 | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 235 | static int probe_spi_rdid_generic(struct flashchip *flash, int bytes) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 236 | { |
Rudolf Marek | 48a85e4 | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 237 | unsigned char readarr[4]; |
Carl-Daniel Hailfinger | 2ad267d | 2009-05-27 11:40:08 +0000 | [diff] [blame] | 238 | uint32_t id1; |
| 239 | uint32_t id2; |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 240 | |
Rudolf Marek | 48a85e4 | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 241 | if (spi_rdid(readarr, bytes)) |
Peter Stuge | da4e5f3 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | |
| 244 | if (!oddparity(readarr[0])) |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 245 | printf_debug("RDID byte 0 parity violation. "); |
Peter Stuge | da4e5f3 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 246 | |
| 247 | /* Check if this is a continuation vendor ID */ |
| 248 | if (readarr[0] == 0x7f) { |
| 249 | if (!oddparity(readarr[1])) |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 250 | printf_debug("RDID byte 1 parity violation. "); |
Carl-Daniel Hailfinger | 2ad267d | 2009-05-27 11:40:08 +0000 | [diff] [blame] | 251 | id1 = (readarr[0] << 8) | readarr[1]; |
| 252 | id2 = readarr[2]; |
Rudolf Marek | 48a85e4 | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 253 | if (bytes > 3) { |
Carl-Daniel Hailfinger | 2ad267d | 2009-05-27 11:40:08 +0000 | [diff] [blame] | 254 | id2 <<= 8; |
| 255 | id2 |= readarr[3]; |
Rudolf Marek | 48a85e4 | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 256 | } |
Peter Stuge | da4e5f3 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 257 | } else { |
Carl-Daniel Hailfinger | 2ad267d | 2009-05-27 11:40:08 +0000 | [diff] [blame] | 258 | id1 = readarr[0]; |
| 259 | id2 = (readarr[1] << 8) | readarr[2]; |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Carl-Daniel Hailfinger | 2ad267d | 2009-05-27 11:40:08 +0000 | [diff] [blame] | 262 | printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, id1, id2); |
Peter Stuge | da4e5f3 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 263 | |
Carl-Daniel Hailfinger | 2ad267d | 2009-05-27 11:40:08 +0000 | [diff] [blame] | 264 | if (id1 == flash->manufacture_id && id2 == flash->model_id) { |
Peter Stuge | da4e5f3 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 265 | /* Print the status register to tell the |
| 266 | * user about possible write protection. |
| 267 | */ |
| 268 | spi_prettyprint_status_register(flash); |
| 269 | |
| 270 | return 1; |
| 271 | } |
| 272 | |
| 273 | /* Test if this is a pure vendor match. */ |
Carl-Daniel Hailfinger | 2ad267d | 2009-05-27 11:40:08 +0000 | [diff] [blame] | 274 | if (id1 == flash->manufacture_id && |
Peter Stuge | da4e5f3 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 275 | GENERIC_DEVICE_ID == flash->model_id) |
| 276 | return 1; |
| 277 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 281 | int probe_spi_rdid(struct flashchip *flash) |
| 282 | { |
Rudolf Marek | 48a85e4 | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 283 | return probe_spi_rdid_generic(flash, 3); |
| 284 | } |
| 285 | |
| 286 | /* support 4 bytes flash ID */ |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 287 | int probe_spi_rdid4(struct flashchip *flash) |
| 288 | { |
Rudolf Marek | 48a85e4 | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 289 | /* only some SPI chipsets support 4 bytes commands */ |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 290 | switch (spi_controller) { |
| 291 | case SPI_CONTROLLER_ICH7: |
| 292 | case SPI_CONTROLLER_ICH9: |
| 293 | case SPI_CONTROLLER_VIA: |
| 294 | case SPI_CONTROLLER_SB600: |
| 295 | case SPI_CONTROLLER_WBSIO: |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 296 | case SPI_CONTROLLER_FT2232: |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 297 | case SPI_CONTROLLER_DUMMY: |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 298 | return probe_spi_rdid_generic(flash, 4); |
| 299 | default: |
| 300 | printf_debug("4b ID not supported on this SPI controller\n"); |
| 301 | } |
| 302 | |
| 303 | return 0; |
Rudolf Marek | 48a85e4 | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Carl-Daniel Hailfinger | 14e50ac | 2008-11-28 01:25:00 +0000 | [diff] [blame] | 306 | int probe_spi_rems(struct flashchip *flash) |
| 307 | { |
| 308 | unsigned char readarr[JEDEC_REMS_INSIZE]; |
Carl-Daniel Hailfinger | 2ad267d | 2009-05-27 11:40:08 +0000 | [diff] [blame] | 309 | uint32_t id1, id2; |
Carl-Daniel Hailfinger | 14e50ac | 2008-11-28 01:25:00 +0000 | [diff] [blame] | 310 | |
| 311 | if (spi_rems(readarr)) |
| 312 | return 0; |
| 313 | |
Carl-Daniel Hailfinger | 2ad267d | 2009-05-27 11:40:08 +0000 | [diff] [blame] | 314 | id1 = readarr[0]; |
| 315 | id2 = readarr[1]; |
Carl-Daniel Hailfinger | 14e50ac | 2008-11-28 01:25:00 +0000 | [diff] [blame] | 316 | |
Carl-Daniel Hailfinger | 2ad267d | 2009-05-27 11:40:08 +0000 | [diff] [blame] | 317 | printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2); |
Carl-Daniel Hailfinger | 14e50ac | 2008-11-28 01:25:00 +0000 | [diff] [blame] | 318 | |
Carl-Daniel Hailfinger | 2ad267d | 2009-05-27 11:40:08 +0000 | [diff] [blame] | 319 | if (id1 == flash->manufacture_id && id2 == flash->model_id) { |
Carl-Daniel Hailfinger | 14e50ac | 2008-11-28 01:25:00 +0000 | [diff] [blame] | 320 | /* Print the status register to tell the |
| 321 | * user about possible write protection. |
| 322 | */ |
| 323 | spi_prettyprint_status_register(flash); |
| 324 | |
| 325 | return 1; |
| 326 | } |
| 327 | |
| 328 | /* Test if this is a pure vendor match. */ |
Carl-Daniel Hailfinger | 2ad267d | 2009-05-27 11:40:08 +0000 | [diff] [blame] | 329 | if (id1 == flash->manufacture_id && |
Carl-Daniel Hailfinger | 14e50ac | 2008-11-28 01:25:00 +0000 | [diff] [blame] | 330 | GENERIC_DEVICE_ID == flash->model_id) |
| 331 | return 1; |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
Carl-Daniel Hailfinger | 42c5497 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 336 | int probe_spi_res(struct flashchip *flash) |
| 337 | { |
| 338 | unsigned char readarr[3]; |
Carl-Daniel Hailfinger | 2ad267d | 2009-05-27 11:40:08 +0000 | [diff] [blame] | 339 | uint32_t id2; |
Peter Stuge | da4e5f3 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 340 | |
Carl-Daniel Hailfinger | 92a54ca | 2008-11-27 22:48:48 +0000 | [diff] [blame] | 341 | /* Check if RDID was successful and did not return 0xff 0xff 0xff. |
| 342 | * In that case, RES is pointless. |
| 343 | */ |
| 344 | if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) || |
| 345 | (readarr[1] != 0xff) || (readarr[2] != 0xff))) |
Peter Stuge | da4e5f3 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 346 | return 0; |
Carl-Daniel Hailfinger | 42c5497 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 347 | |
Peter Stuge | da4e5f3 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 348 | if (spi_res(readarr)) |
| 349 | return 0; |
| 350 | |
Carl-Daniel Hailfinger | 2ad267d | 2009-05-27 11:40:08 +0000 | [diff] [blame] | 351 | id2 = readarr[0]; |
| 352 | printf_debug("%s: id 0x%x\n", __FUNCTION__, id2); |
| 353 | if (id2 != flash->model_id) |
Peter Stuge | da4e5f3 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 354 | return 0; |
| 355 | |
| 356 | /* Print the status register to tell the |
| 357 | * user about possible write protection. |
| 358 | */ |
| 359 | spi_prettyprint_status_register(flash); |
| 360 | return 1; |
Carl-Daniel Hailfinger | 42c5497 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 363 | uint8_t spi_read_status_register(void) |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 364 | { |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 365 | const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR }; |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 366 | /* FIXME: No workarounds for driver/hardware bugs in generic code. */ |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 367 | unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */ |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 368 | int ret; |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 369 | |
| 370 | /* Read Status Register */ |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 371 | ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr); |
| 372 | if (ret) |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 373 | fprintf(stderr, "RDSR failed!\n"); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 374 | |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 375 | return readarr[0]; |
| 376 | } |
| 377 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 378 | /* Prettyprint the status register. Common definitions. */ |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 379 | void spi_prettyprint_status_register_common(uint8_t status) |
| 380 | { |
| 381 | printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is " |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 382 | "%sset\n", (status & (1 << 5)) ? "" : "not "); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 383 | printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is " |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 384 | "%sset\n", (status & (1 << 4)) ? "" : "not "); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 385 | printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is " |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 386 | "%sset\n", (status & (1 << 3)) ? "" : "not "); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 387 | printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is " |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 388 | "%sset\n", (status & (1 << 2)) ? "" : "not "); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 389 | printf_debug("Chip status register: Write Enable Latch (WEL) is " |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 390 | "%sset\n", (status & (1 << 1)) ? "" : "not "); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 391 | printf_debug("Chip status register: Write In Progress (WIP/BUSY) is " |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 392 | "%sset\n", (status & (1 << 0)) ? "" : "not "); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 395 | /* Prettyprint the status register. Works for |
| 396 | * ST M25P series |
| 397 | * MX MX25L series |
| 398 | */ |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 399 | void spi_prettyprint_status_register_st_m25p(uint8_t status) |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 400 | { |
| 401 | printf_debug("Chip status register: Status Register Write Disable " |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 402 | "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not "); |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 403 | printf_debug("Chip status register: Bit 6 is " |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 404 | "%sset\n", (status & (1 << 6)) ? "" : "not "); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 405 | spi_prettyprint_status_register_common(status); |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Carl-Daniel Hailfinger | 1bfd6c9 | 2009-05-06 13:59:44 +0000 | [diff] [blame] | 408 | void spi_prettyprint_status_register_sst25(uint8_t status) |
| 409 | { |
| 410 | printf_debug("Chip status register: Block Protect Write Disable " |
| 411 | "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not "); |
| 412 | printf_debug("Chip status register: Auto Address Increment Programming " |
| 413 | "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not "); |
| 414 | spi_prettyprint_status_register_common(status); |
| 415 | } |
| 416 | |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 417 | /* Prettyprint the status register. Works for |
| 418 | * SST 25VF016 |
| 419 | */ |
| 420 | void spi_prettyprint_status_register_sst25vf016(uint8_t status) |
| 421 | { |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 422 | const char *bpt[] = { |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 423 | "none", |
| 424 | "1F0000H-1FFFFFH", |
| 425 | "1E0000H-1FFFFFH", |
| 426 | "1C0000H-1FFFFFH", |
| 427 | "180000H-1FFFFFH", |
| 428 | "100000H-1FFFFFH", |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 429 | "all", "all" |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 430 | }; |
Carl-Daniel Hailfinger | 1bfd6c9 | 2009-05-06 13:59:44 +0000 | [diff] [blame] | 431 | spi_prettyprint_status_register_sst25(status); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 432 | printf_debug("Resulting block protection : %s\n", |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 433 | bpt[(status & 0x1c) >> 2]); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Peter Stuge | 5fecee4 | 2009-01-26 03:23:50 +0000 | [diff] [blame] | 436 | void spi_prettyprint_status_register_sst25vf040b(uint8_t status) |
| 437 | { |
| 438 | const char *bpt[] = { |
| 439 | "none", |
| 440 | "0x70000-0x7ffff", |
| 441 | "0x60000-0x7ffff", |
| 442 | "0x40000-0x7ffff", |
| 443 | "all blocks", "all blocks", "all blocks", "all blocks" |
| 444 | }; |
Carl-Daniel Hailfinger | 1bfd6c9 | 2009-05-06 13:59:44 +0000 | [diff] [blame] | 445 | spi_prettyprint_status_register_sst25(status); |
Peter Stuge | 5fecee4 | 2009-01-26 03:23:50 +0000 | [diff] [blame] | 446 | printf_debug("Resulting block protection : %s\n", |
Carl-Daniel Hailfinger | 1bfd6c9 | 2009-05-06 13:59:44 +0000 | [diff] [blame] | 447 | bpt[(status & 0x1c) >> 2]); |
Peter Stuge | 5fecee4 | 2009-01-26 03:23:50 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 450 | void spi_prettyprint_status_register(struct flashchip *flash) |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 451 | { |
| 452 | uint8_t status; |
| 453 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 454 | status = spi_read_status_register(); |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 455 | printf_debug("Chip status register is %02x\n", status); |
| 456 | switch (flash->manufacture_id) { |
| 457 | case ST_ID: |
Carl-Daniel Hailfinger | f43e642 | 2008-05-15 22:32:08 +0000 | [diff] [blame] | 458 | if (((flash->model_id & 0xff00) == 0x2000) || |
| 459 | ((flash->model_id & 0xff00) == 0x2500)) |
| 460 | spi_prettyprint_status_register_st_m25p(status); |
| 461 | break; |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 462 | case MX_ID: |
| 463 | if ((flash->model_id & 0xff00) == 0x2000) |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 464 | spi_prettyprint_status_register_st_m25p(status); |
| 465 | break; |
| 466 | case SST_ID: |
Peter Stuge | 5fecee4 | 2009-01-26 03:23:50 +0000 | [diff] [blame] | 467 | switch (flash->model_id) { |
| 468 | case 0x2541: |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 469 | spi_prettyprint_status_register_sst25vf016(status); |
Peter Stuge | 5fecee4 | 2009-01-26 03:23:50 +0000 | [diff] [blame] | 470 | break; |
| 471 | case 0x8d: |
| 472 | case 0x258d: |
| 473 | spi_prettyprint_status_register_sst25vf040b(status); |
| 474 | break; |
Carl-Daniel Hailfinger | 5100a8a | 2009-05-13 22:51:27 +0000 | [diff] [blame] | 475 | default: |
Carl-Daniel Hailfinger | 1bfd6c9 | 2009-05-06 13:59:44 +0000 | [diff] [blame] | 476 | spi_prettyprint_status_register_sst25(status); |
| 477 | break; |
Peter Stuge | 5fecee4 | 2009-01-26 03:23:50 +0000 | [diff] [blame] | 478 | } |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 479 | break; |
| 480 | } |
| 481 | } |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 482 | |
Carl-Daniel Hailfinger | 6afb613 | 2008-11-03 00:02:11 +0000 | [diff] [blame] | 483 | int spi_chip_erase_60(struct flashchip *flash) |
| 484 | { |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 485 | int result; |
Carl-Daniel Hailfinger | 60d7118 | 2009-07-11 19:28:36 +0000 | [diff] [blame] | 486 | struct spi_command spicommands[] = { |
| 487 | { |
| 488 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 489 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 490 | .readcnt = 0, |
| 491 | .readarr = NULL, |
| 492 | }, { |
| 493 | .writecnt = JEDEC_CE_60_OUTSIZE, |
| 494 | .writearr = (const unsigned char[]){ JEDEC_CE_60 }, |
| 495 | .readcnt = 0, |
| 496 | .readarr = NULL, |
| 497 | }, { |
| 498 | .writecnt = 0, |
| 499 | .writearr = NULL, |
| 500 | .readcnt = 0, |
| 501 | .readarr = NULL, |
| 502 | }}; |
Carl-Daniel Hailfinger | 6afb613 | 2008-11-03 00:02:11 +0000 | [diff] [blame] | 503 | |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 504 | result = spi_disable_blockprotect(); |
| 505 | if (result) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 506 | fprintf(stderr, "spi_disable_blockprotect failed\n"); |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 507 | return result; |
| 508 | } |
Carl-Daniel Hailfinger | 60d7118 | 2009-07-11 19:28:36 +0000 | [diff] [blame] | 509 | |
| 510 | result = spi_send_multicommand(spicommands); |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 511 | if (result) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 512 | fprintf(stderr, "%s failed during command execution\n", |
| 513 | __func__); |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 514 | return result; |
| 515 | } |
Carl-Daniel Hailfinger | 6afb613 | 2008-11-03 00:02:11 +0000 | [diff] [blame] | 516 | /* Wait until the Write-In-Progress bit is cleared. |
| 517 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 518 | */ |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 519 | /* FIXME: We assume spi_read_status_register will never fail. */ |
Carl-Daniel Hailfinger | 6afb613 | 2008-11-03 00:02:11 +0000 | [diff] [blame] | 520 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 521 | programmer_delay(1000 * 1000); |
Carl-Daniel Hailfinger | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 522 | if (check_erased_range(flash, 0, flash->total_size * 1024)) { |
| 523 | fprintf(stderr, "ERASE FAILED!\n"); |
| 524 | return -1; |
| 525 | } |
Carl-Daniel Hailfinger | 6afb613 | 2008-11-03 00:02:11 +0000 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 529 | int spi_chip_erase_c7(struct flashchip *flash) |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 530 | { |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 531 | int result; |
Carl-Daniel Hailfinger | 60d7118 | 2009-07-11 19:28:36 +0000 | [diff] [blame] | 532 | struct spi_command spicommands[] = { |
| 533 | { |
| 534 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 535 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 536 | .readcnt = 0, |
| 537 | .readarr = NULL, |
| 538 | }, { |
| 539 | .writecnt = JEDEC_CE_C7_OUTSIZE, |
| 540 | .writearr = (const unsigned char[]){ JEDEC_CE_C7 }, |
| 541 | .readcnt = 0, |
| 542 | .readarr = NULL, |
| 543 | }, { |
| 544 | .writecnt = 0, |
| 545 | .writearr = NULL, |
| 546 | .readcnt = 0, |
| 547 | .readarr = NULL, |
| 548 | }}; |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 549 | |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 550 | result = spi_disable_blockprotect(); |
| 551 | if (result) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 552 | fprintf(stderr, "spi_disable_blockprotect failed\n"); |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 553 | return result; |
| 554 | } |
Carl-Daniel Hailfinger | 60d7118 | 2009-07-11 19:28:36 +0000 | [diff] [blame] | 555 | |
| 556 | result = spi_send_multicommand(spicommands); |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 557 | if (result) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 558 | fprintf(stderr, "%s failed during command execution\n", __func__); |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 559 | return result; |
| 560 | } |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 561 | /* Wait until the Write-In-Progress bit is cleared. |
| 562 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 563 | */ |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 564 | /* FIXME: We assume spi_read_status_register will never fail. */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 565 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 566 | programmer_delay(1000 * 1000); |
Carl-Daniel Hailfinger | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 567 | if (check_erased_range(flash, 0, flash->total_size * 1024)) { |
| 568 | fprintf(stderr, "ERASE FAILED!\n"); |
| 569 | return -1; |
| 570 | } |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 574 | int spi_chip_erase_60_c7(struct flashchip *flash) |
| 575 | { |
| 576 | int result; |
| 577 | result = spi_chip_erase_60(flash); |
| 578 | if (result) { |
| 579 | printf_debug("spi_chip_erase_60 failed, trying c7\n"); |
| 580 | result = spi_chip_erase_c7(flash); |
| 581 | } |
| 582 | return result; |
| 583 | } |
| 584 | |
Carl-Daniel Hailfinger | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 585 | int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
Carl-Daniel Hailfinger | 6afb613 | 2008-11-03 00:02:11 +0000 | [diff] [blame] | 586 | { |
Carl-Daniel Hailfinger | 03adbe1 | 2009-05-09 02:09:45 +0000 | [diff] [blame] | 587 | int result; |
Carl-Daniel Hailfinger | 39fa9b5 | 2009-07-11 22:26:52 +0000 | [diff] [blame] | 588 | struct spi_command spicommands[] = { |
| 589 | { |
| 590 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 591 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 592 | .readcnt = 0, |
| 593 | .readarr = NULL, |
| 594 | }, { |
| 595 | .writecnt = JEDEC_BE_52_OUTSIZE, |
| 596 | .writearr = (const unsigned char[]){ JEDEC_BE_52, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) }, |
| 597 | .readcnt = 0, |
| 598 | .readarr = NULL, |
| 599 | }, { |
| 600 | .writecnt = 0, |
| 601 | .writearr = NULL, |
| 602 | .readcnt = 0, |
| 603 | .readarr = NULL, |
| 604 | }}; |
Carl-Daniel Hailfinger | 6afb613 | 2008-11-03 00:02:11 +0000 | [diff] [blame] | 605 | |
Carl-Daniel Hailfinger | 39fa9b5 | 2009-07-11 22:26:52 +0000 | [diff] [blame] | 606 | result = spi_send_multicommand(spicommands); |
| 607 | if (result) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 608 | fprintf(stderr, "%s failed during command execution\n", |
| 609 | __func__); |
Carl-Daniel Hailfinger | 03adbe1 | 2009-05-09 02:09:45 +0000 | [diff] [blame] | 610 | return result; |
Carl-Daniel Hailfinger | 39fa9b5 | 2009-07-11 22:26:52 +0000 | [diff] [blame] | 611 | } |
Carl-Daniel Hailfinger | 6afb613 | 2008-11-03 00:02:11 +0000 | [diff] [blame] | 612 | /* Wait until the Write-In-Progress bit is cleared. |
| 613 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 614 | */ |
| 615 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 616 | programmer_delay(100 * 1000); |
Carl-Daniel Hailfinger | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 617 | if (check_erased_range(flash, addr, blocklen)) { |
| 618 | fprintf(stderr, "ERASE FAILED!\n"); |
| 619 | return -1; |
| 620 | } |
Carl-Daniel Hailfinger | 6afb613 | 2008-11-03 00:02:11 +0000 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 624 | /* Block size is usually |
| 625 | * 64k for Macronix |
| 626 | * 32k for SST |
| 627 | * 4-32k non-uniform for EON |
| 628 | */ |
Carl-Daniel Hailfinger | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 629 | int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 630 | { |
Carl-Daniel Hailfinger | 03adbe1 | 2009-05-09 02:09:45 +0000 | [diff] [blame] | 631 | int result; |
Carl-Daniel Hailfinger | 39fa9b5 | 2009-07-11 22:26:52 +0000 | [diff] [blame] | 632 | struct spi_command spicommands[] = { |
| 633 | { |
| 634 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 635 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 636 | .readcnt = 0, |
| 637 | .readarr = NULL, |
| 638 | }, { |
| 639 | .writecnt = JEDEC_BE_D8_OUTSIZE, |
| 640 | .writearr = (const unsigned char[]){ JEDEC_BE_D8, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) }, |
| 641 | .readcnt = 0, |
| 642 | .readarr = NULL, |
| 643 | }, { |
| 644 | .writecnt = 0, |
| 645 | .writearr = NULL, |
| 646 | .readcnt = 0, |
| 647 | .readarr = NULL, |
| 648 | }}; |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 649 | |
Carl-Daniel Hailfinger | 39fa9b5 | 2009-07-11 22:26:52 +0000 | [diff] [blame] | 650 | result = spi_send_multicommand(spicommands); |
| 651 | if (result) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 652 | fprintf(stderr, "%s failed during command execution\n", __func__); |
Carl-Daniel Hailfinger | 03adbe1 | 2009-05-09 02:09:45 +0000 | [diff] [blame] | 653 | return result; |
Carl-Daniel Hailfinger | 39fa9b5 | 2009-07-11 22:26:52 +0000 | [diff] [blame] | 654 | } |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 655 | /* Wait until the Write-In-Progress bit is cleared. |
| 656 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 657 | */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 658 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 659 | programmer_delay(100 * 1000); |
Carl-Daniel Hailfinger | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 660 | if (check_erased_range(flash, addr, blocklen)) { |
| 661 | fprintf(stderr, "ERASE FAILED!\n"); |
| 662 | return -1; |
| 663 | } |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 664 | return 0; |
| 665 | } |
| 666 | |
Stefan Reinauer | 424ed22 | 2008-10-29 22:13:20 +0000 | [diff] [blame] | 667 | int spi_chip_erase_d8(struct flashchip *flash) |
| 668 | { |
| 669 | int i, rc = 0; |
| 670 | int total_size = flash->total_size * 1024; |
| 671 | int erase_size = 64 * 1024; |
| 672 | |
| 673 | spi_disable_blockprotect(); |
| 674 | |
| 675 | printf("Erasing chip: \n"); |
| 676 | |
| 677 | for (i = 0; i < total_size / erase_size; i++) { |
Carl-Daniel Hailfinger | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 678 | rc = spi_block_erase_d8(flash, i * erase_size, erase_size); |
Stefan Reinauer | 424ed22 | 2008-10-29 22:13:20 +0000 | [diff] [blame] | 679 | if (rc) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 680 | fprintf(stderr, "Error erasing block at 0x%x\n", i); |
Stefan Reinauer | 424ed22 | 2008-10-29 22:13:20 +0000 | [diff] [blame] | 681 | break; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | printf("\n"); |
| 686 | |
| 687 | return rc; |
| 688 | } |
| 689 | |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 690 | /* Sector size is usually 4k, though Macronix eliteflash has 64k */ |
Carl-Daniel Hailfinger | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 691 | int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 692 | { |
Carl-Daniel Hailfinger | 03adbe1 | 2009-05-09 02:09:45 +0000 | [diff] [blame] | 693 | int result; |
Carl-Daniel Hailfinger | 39fa9b5 | 2009-07-11 22:26:52 +0000 | [diff] [blame] | 694 | struct spi_command spicommands[] = { |
| 695 | { |
| 696 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 697 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 698 | .readcnt = 0, |
| 699 | .readarr = NULL, |
| 700 | }, { |
| 701 | .writecnt = JEDEC_SE_OUTSIZE, |
| 702 | .writearr = (const unsigned char[]){ JEDEC_SE, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) }, |
| 703 | .readcnt = 0, |
| 704 | .readarr = NULL, |
| 705 | }, { |
| 706 | .writecnt = 0, |
| 707 | .writearr = NULL, |
| 708 | .readcnt = 0, |
| 709 | .readarr = NULL, |
| 710 | }}; |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 711 | |
Carl-Daniel Hailfinger | 39fa9b5 | 2009-07-11 22:26:52 +0000 | [diff] [blame] | 712 | result = spi_send_multicommand(spicommands); |
| 713 | if (result) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 714 | fprintf(stderr, "%s failed during command execution\n", |
| 715 | __func__); |
Carl-Daniel Hailfinger | 03adbe1 | 2009-05-09 02:09:45 +0000 | [diff] [blame] | 716 | return result; |
Carl-Daniel Hailfinger | 39fa9b5 | 2009-07-11 22:26:52 +0000 | [diff] [blame] | 717 | } |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 718 | /* Wait until the Write-In-Progress bit is cleared. |
| 719 | * This usually takes 15-800 ms, so wait in 10 ms steps. |
| 720 | */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 721 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 722 | programmer_delay(10 * 1000); |
Carl-Daniel Hailfinger | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 723 | if (check_erased_range(flash, addr, blocklen)) { |
| 724 | fprintf(stderr, "ERASE FAILED!\n"); |
| 725 | return -1; |
| 726 | } |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 727 | return 0; |
| 728 | } |
| 729 | |
Carl-Daniel Hailfinger | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 730 | int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
| 731 | { |
| 732 | if ((addr != 0) || (blocklen != flash->total_size * 1024)) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 733 | fprintf(stderr, "%s called with incorrect arguments\n", |
| 734 | __func__); |
Carl-Daniel Hailfinger | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 735 | return -1; |
| 736 | } |
| 737 | return spi_chip_erase_60(flash); |
| 738 | } |
| 739 | |
| 740 | int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
| 741 | { |
| 742 | if ((addr != 0) || (blocklen != flash->total_size * 1024)) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 743 | fprintf(stderr, "%s called with incorrect arguments\n", |
| 744 | __func__); |
Carl-Daniel Hailfinger | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 745 | return -1; |
| 746 | } |
| 747 | return spi_chip_erase_c7(flash); |
| 748 | } |
| 749 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 750 | int spi_write_status_enable(void) |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 751 | { |
| 752 | const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR }; |
Carl-Daniel Hailfinger | 1e63784 | 2009-05-15 00:56:22 +0000 | [diff] [blame] | 753 | int result; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 754 | |
| 755 | /* Send EWSR (Enable Write Status Register). */ |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 756 | result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL); |
Carl-Daniel Hailfinger | 1e63784 | 2009-05-15 00:56:22 +0000 | [diff] [blame] | 757 | |
| 758 | if (result) |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 759 | fprintf(stderr, "%s failed\n", __func__); |
Carl-Daniel Hailfinger | 1e63784 | 2009-05-15 00:56:22 +0000 | [diff] [blame] | 760 | |
| 761 | return result; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 764 | /* |
| 765 | * This is according the SST25VF016 datasheet, who knows it is more |
| 766 | * generic that this... |
| 767 | */ |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 768 | int spi_write_status_register(int status) |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 769 | { |
Carl-Daniel Hailfinger | fcbdbbc | 2009-07-22 20:09:28 +0000 | [diff] [blame] | 770 | int result; |
| 771 | struct spi_command spicommands[] = { |
| 772 | { |
| 773 | .writecnt = JEDEC_EWSR_OUTSIZE, |
| 774 | .writearr = (const unsigned char[]){ JEDEC_EWSR }, |
| 775 | .readcnt = 0, |
| 776 | .readarr = NULL, |
| 777 | }, { |
| 778 | .writecnt = JEDEC_WRSR_OUTSIZE, |
| 779 | .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status }, |
| 780 | .readcnt = 0, |
| 781 | .readarr = NULL, |
| 782 | }, { |
| 783 | .writecnt = 0, |
| 784 | .writearr = NULL, |
| 785 | .readcnt = 0, |
| 786 | .readarr = NULL, |
| 787 | }}; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 788 | |
Carl-Daniel Hailfinger | fcbdbbc | 2009-07-22 20:09:28 +0000 | [diff] [blame] | 789 | result = spi_send_multicommand(spicommands); |
| 790 | if (result) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 791 | fprintf(stderr, "%s failed during command execution\n", |
| 792 | __func__); |
Carl-Daniel Hailfinger | fcbdbbc | 2009-07-22 20:09:28 +0000 | [diff] [blame] | 793 | } |
| 794 | return result; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Carl-Daniel Hailfinger | 2f1b36f | 2009-07-12 12:06:18 +0000 | [diff] [blame] | 797 | int spi_byte_program(int addr, uint8_t byte) |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 798 | { |
Carl-Daniel Hailfinger | 2f1b36f | 2009-07-12 12:06:18 +0000 | [diff] [blame] | 799 | int result; |
| 800 | struct spi_command spicommands[] = { |
| 801 | { |
| 802 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 803 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 804 | .readcnt = 0, |
| 805 | .readarr = NULL, |
| 806 | }, { |
| 807 | .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE, |
| 808 | .writearr = (const unsigned char[]){ JEDEC_BYTE_PROGRAM, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff), byte }, |
| 809 | .readcnt = 0, |
| 810 | .readarr = NULL, |
| 811 | }, { |
| 812 | .writecnt = 0, |
| 813 | .writearr = NULL, |
| 814 | .readcnt = 0, |
| 815 | .readarr = NULL, |
| 816 | }}; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 817 | |
Carl-Daniel Hailfinger | 2f1b36f | 2009-07-12 12:06:18 +0000 | [diff] [blame] | 818 | result = spi_send_multicommand(spicommands); |
| 819 | if (result) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 820 | fprintf(stderr, "%s failed during command execution\n", |
| 821 | __func__); |
Carl-Daniel Hailfinger | 2f1b36f | 2009-07-12 12:06:18 +0000 | [diff] [blame] | 822 | } |
| 823 | return result; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Paul Fox | eb3acef | 2009-06-12 08:10:33 +0000 | [diff] [blame] | 826 | int spi_nbyte_program(int address, uint8_t *bytes, int len) |
| 827 | { |
Carl-Daniel Hailfinger | 2f1b36f | 2009-07-12 12:06:18 +0000 | [diff] [blame] | 828 | int result; |
| 829 | /* FIXME: Switch to malloc based on len unless that kills speed. */ |
Paul Fox | eb3acef | 2009-06-12 08:10:33 +0000 | [diff] [blame] | 830 | unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = { |
| 831 | JEDEC_BYTE_PROGRAM, |
| 832 | (address >> 16) & 0xff, |
| 833 | (address >> 8) & 0xff, |
| 834 | (address >> 0) & 0xff, |
| 835 | }; |
Carl-Daniel Hailfinger | 2f1b36f | 2009-07-12 12:06:18 +0000 | [diff] [blame] | 836 | struct spi_command spicommands[] = { |
| 837 | { |
| 838 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 839 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 840 | .readcnt = 0, |
| 841 | .readarr = NULL, |
| 842 | }, { |
| 843 | .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len, |
| 844 | .writearr = cmd, |
| 845 | .readcnt = 0, |
| 846 | .readarr = NULL, |
| 847 | }, { |
| 848 | .writecnt = 0, |
| 849 | .writearr = NULL, |
| 850 | .readcnt = 0, |
| 851 | .readarr = NULL, |
| 852 | }}; |
Paul Fox | eb3acef | 2009-06-12 08:10:33 +0000 | [diff] [blame] | 853 | |
Carl-Daniel Hailfinger | 2f1b36f | 2009-07-12 12:06:18 +0000 | [diff] [blame] | 854 | if (!len) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 855 | fprintf(stderr, "%s called for zero-length write\n", __func__); |
Carl-Daniel Hailfinger | 2f1b36f | 2009-07-12 12:06:18 +0000 | [diff] [blame] | 856 | return 1; |
| 857 | } |
Paul Fox | eb3acef | 2009-06-12 08:10:33 +0000 | [diff] [blame] | 858 | if (len > 256) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 859 | fprintf(stderr, "%s called for too long a write\n", __func__); |
Paul Fox | eb3acef | 2009-06-12 08:10:33 +0000 | [diff] [blame] | 860 | return 1; |
| 861 | } |
| 862 | |
| 863 | memcpy(&cmd[4], bytes, len); |
| 864 | |
Carl-Daniel Hailfinger | 2f1b36f | 2009-07-12 12:06:18 +0000 | [diff] [blame] | 865 | result = spi_send_multicommand(spicommands); |
| 866 | if (result) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 867 | fprintf(stderr, "%s failed during command execution\n", |
| 868 | __func__); |
Carl-Daniel Hailfinger | 2f1b36f | 2009-07-12 12:06:18 +0000 | [diff] [blame] | 869 | } |
| 870 | return result; |
Paul Fox | eb3acef | 2009-06-12 08:10:33 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 873 | int spi_disable_blockprotect(void) |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 874 | { |
| 875 | uint8_t status; |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 876 | int result; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 877 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 878 | status = spi_read_status_register(); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 879 | /* If there is block protection in effect, unprotect it first. */ |
| 880 | if ((status & 0x3c) != 0) { |
| 881 | printf_debug("Some block protection in effect, disabling\n"); |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 882 | result = spi_write_status_register(status & ~0x3c); |
| 883 | if (result) { |
Carl-Daniel Hailfinger | 414bd32 | 2009-07-23 01:33:43 +0000 | [diff] [blame] | 884 | fprintf(stderr, "spi_write_status_register failed\n"); |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 885 | return result; |
| 886 | } |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 887 | } |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 888 | return 0; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Carl-Daniel Hailfinger | 598ec58 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 891 | int spi_nbyte_read(int address, uint8_t *bytes, int len) |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 892 | { |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 893 | const unsigned char cmd[JEDEC_READ_OUTSIZE] = { |
| 894 | JEDEC_READ, |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 895 | (address >> 16) & 0xff, |
| 896 | (address >> 8) & 0xff, |
| 897 | (address >> 0) & 0xff, |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 898 | }; |
| 899 | |
| 900 | /* Send Read */ |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 901 | return spi_send_command(sizeof(cmd), len, cmd, bytes); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Carl-Daniel Hailfinger | 38a059d | 2009-06-13 12:04:03 +0000 | [diff] [blame] | 904 | /* |
| 905 | * Read a complete flash chip. |
| 906 | * Each page is read separately in chunks with a maximum size of chunksize. |
| 907 | */ |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 908 | int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize) |
Carl-Daniel Hailfinger | 38a059d | 2009-06-13 12:04:03 +0000 | [diff] [blame] | 909 | { |
| 910 | int rc = 0; |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 911 | int i, j, starthere, lenhere; |
Carl-Daniel Hailfinger | 38a059d | 2009-06-13 12:04:03 +0000 | [diff] [blame] | 912 | int page_size = flash->page_size; |
| 913 | int toread; |
| 914 | |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 915 | /* Warning: This loop has a very unusual condition and body. |
| 916 | * The loop needs to go through each page with at least one affected |
| 917 | * byte. The lowest page number is (start / page_size) since that |
| 918 | * division rounds down. The highest page number we want is the page |
| 919 | * where the last byte of the range lives. That last byte has the |
| 920 | * address (start + len - 1), thus the highest page number is |
| 921 | * (start + len - 1) / page_size. Since we want to include that last |
| 922 | * page as well, the loop condition uses <=. |
| 923 | */ |
| 924 | for (i = start / page_size; i <= (start + len - 1) / page_size; i++) { |
| 925 | /* Byte position of the first byte in the range in this page. */ |
| 926 | /* starthere is an offset to the base address of the chip. */ |
| 927 | starthere = max(start, i * page_size); |
| 928 | /* Length of bytes in the range in this page. */ |
| 929 | lenhere = min(start + len, (i + 1) * page_size) - starthere; |
| 930 | for (j = 0; j < lenhere; j += chunksize) { |
| 931 | toread = min(chunksize, lenhere - j); |
| 932 | rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread); |
Carl-Daniel Hailfinger | 38a059d | 2009-06-13 12:04:03 +0000 | [diff] [blame] | 933 | if (rc) |
| 934 | break; |
| 935 | } |
| 936 | if (rc) |
| 937 | break; |
| 938 | } |
| 939 | |
| 940 | return rc; |
| 941 | } |
| 942 | |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 943 | int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 944 | { |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 945 | if (!spi_programmer[spi_controller].read) { |
| 946 | fprintf(stderr, "%s called, but SPI read is unsupported on this" |
| 947 | " hardware. Please report a bug.\n", __func__); |
| 948 | return 1; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 951 | return spi_programmer[spi_controller].read(flash, buf, start, len); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 954 | /* |
| 955 | * Program chip using byte programming. (SLOW!) |
| 956 | * This is for chips which can only handle one byte writes |
| 957 | * and for chips where memory mapped programming is impossible |
| 958 | * (e.g. due to size constraints in IT87* for over 512 kB) |
| 959 | */ |
| 960 | int spi_chip_write_1(struct flashchip *flash, uint8_t *buf) |
| 961 | { |
| 962 | int total_size = 1024 * flash->total_size; |
| 963 | int i; |
| 964 | |
| 965 | spi_disable_blockprotect(); |
Carl-Daniel Hailfinger | 116081a | 2009-08-10 02:29:21 +0000 | [diff] [blame] | 966 | /* Erase first */ |
| 967 | printf("Erasing flash before programming... "); |
| 968 | if (flash->erase(flash)) { |
| 969 | fprintf(stderr, "ERASE FAILED!\n"); |
| 970 | return -1; |
| 971 | } |
| 972 | printf("done.\n"); |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 973 | for (i = 0; i < total_size; i++) { |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 974 | spi_byte_program(i, buf[i]); |
| 975 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 976 | programmer_delay(10); |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * Program chip using page (256 bytes) programming. |
| 984 | * Some SPI masters can't do this, they use single byte programming instead. |
| 985 | */ |
Carl-Daniel Hailfinger | 8d49701 | 2009-05-09 02:34:18 +0000 | [diff] [blame] | 986 | int spi_chip_write_256(struct flashchip *flash, uint8_t *buf) |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 987 | { |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 988 | if (!spi_programmer[spi_controller].write_256) { |
| 989 | fprintf(stderr, "%s called, but SPI page write is unsupported " |
| 990 | " on this hardware. Please report a bug.\n", __func__); |
| 991 | return 1; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 994 | return spi_programmer[spi_controller].write_256(flash, buf); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 995 | } |
Peter Stuge | fd9217d | 2009-01-26 03:37:40 +0000 | [diff] [blame] | 996 | |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 997 | uint32_t spi_get_valid_read_addr(void) |
| 998 | { |
| 999 | /* Need to return BBAR for ICH chipsets. */ |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 1003 | int spi_aai_write(struct flashchip *flash, uint8_t *buf) |
| 1004 | { |
Peter Stuge | fd9217d | 2009-01-26 03:37:40 +0000 | [diff] [blame] | 1005 | uint32_t pos = 2, size = flash->total_size * 1024; |
| 1006 | unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]}; |
Carl-Daniel Hailfinger | 03adbe1 | 2009-05-09 02:09:45 +0000 | [diff] [blame] | 1007 | int result; |
| 1008 | |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 1009 | switch (spi_controller) { |
| 1010 | case SPI_CONTROLLER_WBSIO: |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 1011 | fprintf(stderr, "%s: impossible with Winbond SPI masters," |
| 1012 | " degrading to byte program\n", __func__); |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 1013 | return spi_chip_write_1(flash, buf); |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 1014 | default: |
| 1015 | break; |
Peter Stuge | fd9217d | 2009-01-26 03:37:40 +0000 | [diff] [blame] | 1016 | } |
Carl-Daniel Hailfinger | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 1017 | if (flash->erase(flash)) { |
| 1018 | fprintf(stderr, "ERASE FAILED!\n"); |
| 1019 | return -1; |
| 1020 | } |
Carl-Daniel Hailfinger | 03adbe1 | 2009-05-09 02:09:45 +0000 | [diff] [blame] | 1021 | result = spi_write_enable(); |
| 1022 | if (result) |
| 1023 | return result; |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 1024 | spi_send_command(6, 0, w, NULL); |
Peter Stuge | fd9217d | 2009-01-26 03:37:40 +0000 | [diff] [blame] | 1025 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 1026 | programmer_delay(5); /* SST25VF040B Tbp is max 10us */ |
Peter Stuge | fd9217d | 2009-01-26 03:37:40 +0000 | [diff] [blame] | 1027 | while (pos < size) { |
| 1028 | w[1] = buf[pos++]; |
| 1029 | w[2] = buf[pos++]; |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 1030 | spi_send_command(3, 0, w, NULL); |
Peter Stuge | fd9217d | 2009-01-26 03:37:40 +0000 | [diff] [blame] | 1031 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 1032 | programmer_delay(5); /* SST25VF040B Tbp is max 10us */ |
Peter Stuge | fd9217d | 2009-01-26 03:37:40 +0000 | [diff] [blame] | 1033 | } |
| 1034 | spi_write_disable(); |
| 1035 | return 0; |
| 1036 | } |