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