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