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