stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007, 2008 Carl-Daniel Hailfinger |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008 coresystems GmbH |
stepan | d4b1375 | 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" |
hailfinger | 7803156 | 2008-05-13 14:58:23 +0000 | [diff] [blame] | 30 | #include "spi.h" |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 31 | |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 32 | |
| 33 | void spi_prettyprint_status_register(struct flashchip *flash); |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 34 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 35 | int spi_command(unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr) |
hailfinger | 35cc816 | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 36 | { |
| 37 | if (it8716f_flashport) |
hailfinger | 8d8beb5 | 2008-05-10 23:40:51 +0000 | [diff] [blame] | 38 | return it8716f_spi_command(writecnt, readcnt, writearr, readarr); |
ruik | 9bc51c0 | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 39 | else if ((ich7_detected) || (viaspi_detected)) |
| 40 | return ich_spi_command(writecnt, readcnt, writearr, readarr); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 41 | else if (ich9_detected) |
| 42 | return ich_spi_command(writecnt, readcnt, writearr, readarr); |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 43 | printf_debug("%s called, but no SPI chipset detected\n", __FUNCTION__); |
hailfinger | 35cc816 | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 44 | return 1; |
| 45 | } |
| 46 | |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame^] | 47 | static int spi_rdid(unsigned char *readarr, int bytes) |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 48 | { |
hailfinger | eb5dfbd | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 49 | const unsigned char cmd[JEDEC_RDID_OUTSIZE] = {JEDEC_RDID}; |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 50 | |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame^] | 51 | if (spi_command(JEDEC_RDID_OUTSIZE, bytes, cmd, readarr)) |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 52 | return 1; |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 53 | printf_debug("RDID returned %02x %02x %02x.\n", readarr[0], readarr[1], readarr[2]); |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
hailfinger | 8289312 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 57 | static int spi_res(unsigned char *readarr) |
| 58 | { |
| 59 | const unsigned char cmd[JEDEC_RES_OUTSIZE] = {JEDEC_RES, 0, 0, 0}; |
| 60 | |
| 61 | if (spi_command(JEDEC_RES_OUTSIZE, JEDEC_RES_INSIZE, cmd, readarr)) |
| 62 | return 1; |
| 63 | printf_debug("RES returned %02x.\n", readarr[0]); |
| 64 | return 0; |
| 65 | } |
| 66 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 67 | void spi_write_enable() |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 68 | { |
hailfinger | eb5dfbd | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 69 | const unsigned char cmd[JEDEC_WREN_OUTSIZE] = {JEDEC_WREN}; |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 70 | |
| 71 | /* Send WREN (Write Enable) */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 72 | spi_command(JEDEC_WREN_OUTSIZE, JEDEC_WREN_INSIZE, cmd, NULL); |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 73 | } |
| 74 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 75 | void spi_write_disable() |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 76 | { |
hailfinger | eb5dfbd | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 77 | const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = {JEDEC_WRDI}; |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 78 | |
| 79 | /* Send WRDI (Write Disable) */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 80 | spi_command(JEDEC_WRDI_OUTSIZE, JEDEC_WRDI_INSIZE, cmd, NULL); |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 81 | } |
| 82 | |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame^] | 83 | static int probe_spi_rdid_generic(struct flashchip *flash, int bytes) |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 84 | { |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame^] | 85 | unsigned char readarr[4]; |
hailfinger | 492e317 | 2008-02-06 22:07:58 +0000 | [diff] [blame] | 86 | uint32_t manuf_id; |
| 87 | uint32_t model_id; |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 88 | |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame^] | 89 | if (spi_rdid(readarr, bytes)) |
stuge | 7be6683 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 90 | return 0; |
| 91 | |
| 92 | if (!oddparity(readarr[0])) |
| 93 | printf_debug("RDID byte 0 parity violation.\n"); |
| 94 | |
| 95 | /* Check if this is a continuation vendor ID */ |
| 96 | if (readarr[0] == 0x7f) { |
| 97 | if (!oddparity(readarr[1])) |
| 98 | printf_debug("RDID byte 1 parity violation.\n"); |
| 99 | manuf_id = (readarr[0] << 8) | readarr[1]; |
| 100 | model_id = readarr[2]; |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame^] | 101 | if (bytes > 3) { |
| 102 | model_id <<= 8; |
| 103 | model_id |= readarr[3]; |
| 104 | } |
stuge | 7be6683 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 105 | } else { |
| 106 | manuf_id = readarr[0]; |
| 107 | model_id = (readarr[1] << 8) | readarr[2]; |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 108 | } |
| 109 | |
stuge | 7be6683 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 110 | printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id, model_id); |
| 111 | |
| 112 | if (manuf_id == flash->manufacture_id && |
| 113 | model_id == flash->model_id) { |
| 114 | /* Print the status register to tell the |
| 115 | * user about possible write protection. |
| 116 | */ |
| 117 | spi_prettyprint_status_register(flash); |
| 118 | |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | /* Test if this is a pure vendor match. */ |
| 123 | if (manuf_id == flash->manufacture_id && |
| 124 | GENERIC_DEVICE_ID == flash->model_id) |
| 125 | return 1; |
| 126 | |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame^] | 130 | int probe_spi_rdid(struct flashchip *flash) { |
| 131 | return probe_spi_rdid_generic(flash, 3); |
| 132 | } |
| 133 | |
| 134 | /* support 4 bytes flash ID */ |
| 135 | int probe_spi_rdid4(struct flashchip *flash) { |
| 136 | |
| 137 | /* only some SPI chipsets support 4 bytes commands */ |
| 138 | if (!((ich7_detected) || (ich9_detected) || (viaspi_detected))) |
| 139 | return 0; |
| 140 | return probe_spi_rdid_generic(flash, 4); |
| 141 | } |
| 142 | |
hailfinger | 8289312 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 143 | int probe_spi_res(struct flashchip *flash) |
| 144 | { |
| 145 | unsigned char readarr[3]; |
| 146 | uint32_t model_id; |
stuge | 7be6683 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 147 | |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame^] | 148 | if (spi_rdid(readarr, 3)) |
hailfinger | 8289312 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 149 | /* We couldn't issue RDID, it's pointless to try RES. */ |
| 150 | return 0; |
hailfinger | 8289312 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 151 | |
stuge | 7be6683 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 152 | /* Check if RDID returns 0xff 0xff 0xff, then we use RES. */ |
| 153 | if ((readarr[0] != 0xff) || (readarr[1] != 0xff) || |
| 154 | (readarr[2] != 0xff)) |
| 155 | return 0; |
hailfinger | 8289312 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 156 | |
stuge | 7be6683 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 157 | if (spi_res(readarr)) |
| 158 | return 0; |
| 159 | |
| 160 | model_id = readarr[0]; |
| 161 | printf_debug("%s: id 0x%x\n", __FUNCTION__, model_id); |
| 162 | if (model_id != flash->model_id) |
| 163 | return 0; |
| 164 | |
| 165 | /* Print the status register to tell the |
| 166 | * user about possible write protection. |
| 167 | */ |
| 168 | spi_prettyprint_status_register(flash); |
| 169 | return 1; |
hailfinger | 8289312 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 170 | } |
| 171 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 172 | uint8_t spi_read_status_register() |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 173 | { |
hailfinger | eb5dfbd | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 174 | const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = {JEDEC_RDSR}; |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 175 | unsigned char readarr[1]; |
| 176 | |
| 177 | /* Read Status Register */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 178 | spi_command(JEDEC_RDSR_OUTSIZE, JEDEC_RDSR_INSIZE, cmd, readarr); |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 179 | return readarr[0]; |
| 180 | } |
| 181 | |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 182 | /* Prettyprint the status register. Common definitions. |
| 183 | */ |
| 184 | void spi_prettyprint_status_register_common(uint8_t status) |
| 185 | { |
| 186 | printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is " |
| 187 | "%sset\n", (status & (1 << 5)) ? "" : "not "); |
| 188 | printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is " |
| 189 | "%sset\n", (status & (1 << 4)) ? "" : "not "); |
| 190 | printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is " |
| 191 | "%sset\n", (status & (1 << 3)) ? "" : "not "); |
| 192 | printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is " |
| 193 | "%sset\n", (status & (1 << 2)) ? "" : "not "); |
| 194 | printf_debug("Chip status register: Write Enable Latch (WEL) is " |
| 195 | "%sset\n", (status & (1 << 1)) ? "" : "not "); |
| 196 | printf_debug("Chip status register: Write In Progress (WIP/BUSY) is " |
| 197 | "%sset\n", (status & (1 << 0)) ? "" : "not "); |
| 198 | } |
| 199 | |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 200 | /* Prettyprint the status register. Works for |
| 201 | * ST M25P series |
| 202 | * MX MX25L series |
| 203 | */ |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 204 | void spi_prettyprint_status_register_st_m25p(uint8_t status) |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 205 | { |
| 206 | printf_debug("Chip status register: Status Register Write Disable " |
| 207 | "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not "); |
| 208 | printf_debug("Chip status register: Bit 6 is " |
| 209 | "%sset\n", (status & (1 << 6)) ? "" : "not "); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 210 | spi_prettyprint_status_register_common(status); |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 211 | } |
| 212 | |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 213 | /* Prettyprint the status register. Works for |
| 214 | * SST 25VF016 |
| 215 | */ |
| 216 | void spi_prettyprint_status_register_sst25vf016(uint8_t status) |
| 217 | { |
hailfinger | 9cd4cf1 | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 218 | const char *bpt[] = { |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 219 | "none", |
| 220 | "1F0000H-1FFFFFH", |
| 221 | "1E0000H-1FFFFFH", |
| 222 | "1C0000H-1FFFFFH", |
| 223 | "180000H-1FFFFFH", |
| 224 | "100000H-1FFFFFH", |
hailfinger | 9cd4cf1 | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 225 | "all", "all" |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 226 | }; |
| 227 | printf_debug("Chip status register: Block Protect Write Disable " |
| 228 | "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not "); |
| 229 | printf_debug("Chip status register: Auto Address Increment Programming " |
| 230 | "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not "); |
| 231 | spi_prettyprint_status_register_common(status); |
| 232 | printf_debug("Resulting block protection : %s\n", |
| 233 | bpt[(status & 0x1c) >> 2]); |
| 234 | } |
| 235 | |
| 236 | void spi_prettyprint_status_register(struct flashchip *flash) |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 237 | { |
| 238 | uint8_t status; |
| 239 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 240 | status = spi_read_status_register(); |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 241 | printf_debug("Chip status register is %02x\n", status); |
| 242 | switch (flash->manufacture_id) { |
| 243 | case ST_ID: |
hailfinger | 8b86913 | 2008-05-15 22:32:08 +0000 | [diff] [blame] | 244 | if (((flash->model_id & 0xff00) == 0x2000) || |
| 245 | ((flash->model_id & 0xff00) == 0x2500)) |
| 246 | spi_prettyprint_status_register_st_m25p(status); |
| 247 | break; |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 248 | case MX_ID: |
| 249 | if ((flash->model_id & 0xff00) == 0x2000) |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 250 | spi_prettyprint_status_register_st_m25p(status); |
| 251 | break; |
| 252 | case SST_ID: |
| 253 | if (flash->model_id == SST_25VF016B) |
| 254 | spi_prettyprint_status_register_sst25vf016(status); |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 255 | break; |
| 256 | } |
| 257 | } |
| 258 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 259 | int spi_chip_erase_c7(struct flashchip *flash) |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 260 | { |
hailfinger | eb5dfbd | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 261 | const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = {JEDEC_CE_C7}; |
hailfinger | a969856 | 2007-12-16 21:15:27 +0000 | [diff] [blame] | 262 | |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 263 | spi_disable_blockprotect(); |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 264 | spi_write_enable(); |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 265 | /* Send CE (Chip Erase) */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 266 | spi_command(JEDEC_CE_C7_OUTSIZE, JEDEC_CE_C7_INSIZE, cmd, NULL); |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 267 | /* Wait until the Write-In-Progress bit is cleared. |
| 268 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 269 | */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 270 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 271 | sleep(1); |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 275 | /* Block size is usually |
| 276 | * 64k for Macronix |
| 277 | * 32k for SST |
| 278 | * 4-32k non-uniform for EON |
| 279 | */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 280 | int spi_block_erase_d8(const struct flashchip *flash, unsigned long addr) |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 281 | { |
hailfinger | eb5dfbd | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 282 | unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = {JEDEC_BE_D8}; |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 283 | |
| 284 | cmd[1] = (addr & 0x00ff0000) >> 16; |
| 285 | cmd[2] = (addr & 0x0000ff00) >> 8; |
| 286 | cmd[3] = (addr & 0x000000ff); |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 287 | spi_write_enable(); |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 288 | /* Send BE (Block Erase) */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 289 | spi_command(JEDEC_BE_D8_OUTSIZE, JEDEC_BE_D8_INSIZE, cmd, NULL); |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 290 | /* Wait until the Write-In-Progress bit is cleared. |
| 291 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 292 | */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 293 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 294 | usleep(100 * 1000); |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | /* Sector size is usually 4k, though Macronix eliteflash has 64k */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 299 | int spi_sector_erase(const struct flashchip *flash, unsigned long addr) |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 300 | { |
hailfinger | eb5dfbd | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 301 | unsigned char cmd[JEDEC_SE_OUTSIZE] = {JEDEC_SE}; |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 302 | cmd[1] = (addr & 0x00ff0000) >> 16; |
| 303 | cmd[2] = (addr & 0x0000ff00) >> 8; |
| 304 | cmd[3] = (addr & 0x000000ff); |
| 305 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 306 | spi_write_enable(); |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 307 | /* Send SE (Sector Erase) */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 308 | spi_command(JEDEC_SE_OUTSIZE, JEDEC_SE_INSIZE, cmd, NULL); |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 309 | /* Wait until the Write-In-Progress bit is cleared. |
| 310 | * This usually takes 15-800 ms, so wait in 10 ms steps. |
| 311 | */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 312 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 313 | usleep(10 * 1000); |
| 314 | return 0; |
| 315 | } |
| 316 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 317 | void spi_page_program(int block, uint8_t *buf, uint8_t *bios) |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 318 | { |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 319 | if (it8716f_flashport) { |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 320 | it8716f_spi_page_program(block, buf, bios); |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 321 | return; |
| 322 | } |
| 323 | printf_debug("%s called, but no SPI chipset detected\n", __FUNCTION__); |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 324 | } |
| 325 | |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 326 | /* |
| 327 | * This is according the SST25VF016 datasheet, who knows it is more |
| 328 | * generic that this... |
| 329 | */ |
| 330 | void spi_write_status_register(int status) |
| 331 | { |
hailfinger | eb5dfbd | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 332 | const unsigned char cmd[JEDEC_WRSR_OUTSIZE] = {JEDEC_WRSR, (unsigned char)status}; |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 333 | |
| 334 | /* Send WRSR (Write Status Register) */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 335 | spi_command(JEDEC_WRSR_OUTSIZE, JEDEC_WRSR_INSIZE, cmd, NULL); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void spi_byte_program(int address, uint8_t byte) |
| 339 | { |
| 340 | const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {JEDEC_BYTE_PROGRAM, |
| 341 | (address>>16)&0xff, |
| 342 | (address>>8)&0xff, |
| 343 | (address>>0)&0xff, |
| 344 | byte |
| 345 | }; |
| 346 | |
| 347 | /* Send Byte-Program */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 348 | spi_command(JEDEC_BYTE_PROGRAM_OUTSIZE, JEDEC_BYTE_PROGRAM_INSIZE, cmd, NULL); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void spi_disable_blockprotect(void) |
| 352 | { |
| 353 | uint8_t status; |
| 354 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 355 | status = spi_read_status_register(); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 356 | /* If there is block protection in effect, unprotect it first. */ |
| 357 | if ((status & 0x3c) != 0) { |
| 358 | printf_debug("Some block protection in effect, disabling\n"); |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 359 | spi_write_enable(); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 360 | spi_write_status_register(status & ~0x3c); |
| 361 | } |
| 362 | } |
| 363 | |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 364 | void spi_nbyte_read(int address, uint8_t *bytes, int len) |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 365 | { |
| 366 | const unsigned char cmd[JEDEC_READ_OUTSIZE] = {JEDEC_READ, |
hailfinger | 9cd4cf1 | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 367 | (address >> 16) & 0xff, |
| 368 | (address >> 8) & 0xff, |
| 369 | (address >> 0) & 0xff, |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 370 | }; |
| 371 | |
| 372 | /* Send Read */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 373 | spi_command(JEDEC_READ_OUTSIZE, len, cmd, bytes); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 374 | } |
| 375 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 376 | int spi_chip_read(struct flashchip *flash, uint8_t *buf) |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 377 | { |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 378 | if (it8716f_flashport) |
| 379 | return it8716f_spi_chip_read(flash, buf); |
ruik | 9bc51c0 | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 380 | else if ((ich7_detected) || (viaspi_detected)) |
| 381 | return ich_spi_read(flash, buf); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 382 | else if (ich9_detected) |
| 383 | return ich_spi_read(flash, buf); |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 384 | printf_debug("%s called, but no SPI chipset detected\n", __FUNCTION__); |
| 385 | return 1; |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 386 | } |
| 387 | |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 388 | int spi_chip_write(struct flashchip *flash, uint8_t *buf) |
| 389 | { |
| 390 | if (it8716f_flashport) |
| 391 | return it8716f_spi_chip_write(flash, buf); |
ruik | 9bc51c0 | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 392 | else if ((ich7_detected) || (viaspi_detected)) |
| 393 | return ich_spi_write(flash, buf); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 394 | else if (ich9_detected) |
| 395 | return ich_spi_write(flash, buf); |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 396 | printf_debug("%s called, but no SPI chipset detected\n", __FUNCTION__); |
| 397 | return 1; |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 398 | } |
| 399 | |