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