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