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