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