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