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