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