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