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