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 | void spi_prettyprint_status_register(struct flashchip *flash); |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 33 | |
uwe | fa98ca1 | 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) |
hailfinger | 35cc816 | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 36 | { |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 37 | switch (flashbus) { |
| 38 | case BUS_TYPE_IT87XX_SPI: |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 39 | return it8716f_spi_command(writecnt, readcnt, writearr, |
| 40 | readarr); |
stepan | 3bdf618 | 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 | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 44 | return ich_spi_command(writecnt, readcnt, writearr, readarr); |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 45 | default: |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 46 | printf_debug |
| 47 | ("%s called, but no SPI chipset/strapping detected\n", |
| 48 | __FUNCTION__); |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 49 | } |
hailfinger | 35cc816 | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 50 | return 1; |
| 51 | } |
| 52 | |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 53 | static int spi_rdid(unsigned char *readarr, int bytes) |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 54 | { |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 55 | const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID }; |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 56 | |
stuge | 494b4eb | 2008-07-07 06:38:51 +0000 | [diff] [blame] | 57 | if (spi_command(sizeof(cmd), bytes, cmd, readarr)) |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 58 | return 1; |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 59 | printf_debug("RDID returned %02x %02x %02x.\n", readarr[0], readarr[1], |
| 60 | readarr[2]); |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
hailfinger | 8289312 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 64 | static int spi_res(unsigned char *readarr) |
| 65 | { |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 66 | const unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 }; |
hailfinger | 8289312 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 67 | |
stuge | 494b4eb | 2008-07-07 06:38:51 +0000 | [diff] [blame] | 68 | if (spi_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr)) |
hailfinger | 8289312 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 69 | return 1; |
| 70 | printf_debug("RES returned %02x.\n", readarr[0]); |
| 71 | return 0; |
| 72 | } |
| 73 | |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 74 | int spi_write_enable() |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 75 | { |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 76 | const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN }; |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 77 | |
| 78 | /* Send WREN (Write Enable) */ |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 79 | return spi_command(sizeof(cmd), 0, cmd, NULL); |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 80 | } |
| 81 | |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 82 | int spi_write_disable() |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 83 | { |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 84 | const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI }; |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 85 | |
| 86 | /* Send WRDI (Write Disable) */ |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 87 | return spi_command(sizeof(cmd), 0, cmd, NULL); |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 88 | } |
| 89 | |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 90 | static int probe_spi_rdid_generic(struct flashchip *flash, int bytes) |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 91 | { |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 92 | unsigned char readarr[4]; |
hailfinger | 492e317 | 2008-02-06 22:07:58 +0000 | [diff] [blame] | 93 | uint32_t manuf_id; |
| 94 | uint32_t model_id; |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 95 | |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 96 | if (spi_rdid(readarr, bytes)) |
stuge | 7be6683 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 97 | return 0; |
| 98 | |
| 99 | if (!oddparity(readarr[0])) |
| 100 | printf_debug("RDID byte 0 parity violation.\n"); |
| 101 | |
| 102 | /* Check if this is a continuation vendor ID */ |
| 103 | if (readarr[0] == 0x7f) { |
| 104 | if (!oddparity(readarr[1])) |
| 105 | printf_debug("RDID byte 1 parity violation.\n"); |
| 106 | manuf_id = (readarr[0] << 8) | readarr[1]; |
| 107 | model_id = readarr[2]; |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 108 | if (bytes > 3) { |
| 109 | model_id <<= 8; |
| 110 | model_id |= readarr[3]; |
| 111 | } |
stuge | 7be6683 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 112 | } else { |
| 113 | manuf_id = readarr[0]; |
| 114 | model_id = (readarr[1] << 8) | readarr[2]; |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 115 | } |
| 116 | |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 117 | printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id, |
| 118 | model_id); |
stuge | 7be6683 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 119 | |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 120 | if (manuf_id == flash->manufacture_id && model_id == flash->model_id) { |
stuge | 7be6683 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 121 | /* Print the status register to tell the |
| 122 | * user about possible write protection. |
| 123 | */ |
| 124 | spi_prettyprint_status_register(flash); |
| 125 | |
| 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | /* Test if this is a pure vendor match. */ |
| 130 | if (manuf_id == flash->manufacture_id && |
| 131 | GENERIC_DEVICE_ID == flash->model_id) |
| 132 | return 1; |
| 133 | |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 134 | return 0; |
| 135 | } |
| 136 | |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 137 | int probe_spi_rdid(struct flashchip *flash) |
| 138 | { |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 139 | return probe_spi_rdid_generic(flash, 3); |
| 140 | } |
| 141 | |
| 142 | /* support 4 bytes flash ID */ |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 143 | int probe_spi_rdid4(struct flashchip *flash) |
| 144 | { |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 145 | /* only some SPI chipsets support 4 bytes commands */ |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 146 | switch (flashbus) { |
| 147 | case BUS_TYPE_ICH7_SPI: |
| 148 | case BUS_TYPE_ICH9_SPI: |
| 149 | case BUS_TYPE_VIA_SPI: |
| 150 | return probe_spi_rdid_generic(flash, 4); |
| 151 | default: |
| 152 | printf_debug("4b ID not supported on this SPI controller\n"); |
| 153 | } |
| 154 | |
| 155 | return 0; |
ruik | dbe18ee | 2008-06-30 21:45:17 +0000 | [diff] [blame] | 156 | } |
| 157 | |
hailfinger | 8289312 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 158 | int probe_spi_res(struct flashchip *flash) |
| 159 | { |
| 160 | unsigned char readarr[3]; |
| 161 | uint32_t model_id; |
stuge | 7be6683 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 162 | |
hailfinger | 915cc85 | 2008-11-27 22:48:48 +0000 | [diff] [blame^] | 163 | /* Check if RDID was successful and did not return 0xff 0xff 0xff. |
| 164 | * In that case, RES is pointless. |
| 165 | */ |
| 166 | if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) || |
| 167 | (readarr[1] != 0xff) || (readarr[2] != 0xff))) |
stuge | 7be6683 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 168 | return 0; |
hailfinger | 8289312 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 169 | |
stuge | 7be6683 | 2008-06-24 01:22:03 +0000 | [diff] [blame] | 170 | if (spi_res(readarr)) |
| 171 | return 0; |
| 172 | |
| 173 | model_id = readarr[0]; |
| 174 | printf_debug("%s: id 0x%x\n", __FUNCTION__, model_id); |
| 175 | if (model_id != flash->model_id) |
| 176 | return 0; |
| 177 | |
| 178 | /* Print the status register to tell the |
| 179 | * user about possible write protection. |
| 180 | */ |
| 181 | spi_prettyprint_status_register(flash); |
| 182 | return 1; |
hailfinger | 8289312 | 2008-05-15 03:19:49 +0000 | [diff] [blame] | 183 | } |
| 184 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 185 | uint8_t spi_read_status_register() |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 186 | { |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 187 | const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR }; |
stuge | 494b4eb | 2008-07-07 06:38:51 +0000 | [diff] [blame] | 188 | unsigned char readarr[JEDEC_RDSR_INSIZE]; |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 189 | |
| 190 | /* Read Status Register */ |
stuge | 494b4eb | 2008-07-07 06:38:51 +0000 | [diff] [blame] | 191 | spi_command(sizeof(cmd), sizeof(readarr), cmd, readarr); |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 192 | return readarr[0]; |
| 193 | } |
| 194 | |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 195 | /* Prettyprint the status register. Common definitions. |
| 196 | */ |
| 197 | void spi_prettyprint_status_register_common(uint8_t status) |
| 198 | { |
| 199 | printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is " |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 200 | "%sset\n", (status & (1 << 5)) ? "" : "not "); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 201 | printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is " |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 202 | "%sset\n", (status & (1 << 4)) ? "" : "not "); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 203 | printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is " |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 204 | "%sset\n", (status & (1 << 3)) ? "" : "not "); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 205 | printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is " |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 206 | "%sset\n", (status & (1 << 2)) ? "" : "not "); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 207 | printf_debug("Chip status register: Write Enable Latch (WEL) is " |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 208 | "%sset\n", (status & (1 << 1)) ? "" : "not "); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 209 | printf_debug("Chip status register: Write In Progress (WIP/BUSY) is " |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 210 | "%sset\n", (status & (1 << 0)) ? "" : "not "); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 211 | } |
| 212 | |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 213 | /* Prettyprint the status register. Works for |
| 214 | * ST M25P series |
| 215 | * MX MX25L series |
| 216 | */ |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 217 | void spi_prettyprint_status_register_st_m25p(uint8_t status) |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 218 | { |
| 219 | printf_debug("Chip status register: Status Register Write Disable " |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 220 | "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not "); |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 221 | printf_debug("Chip status register: Bit 6 is " |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 222 | "%sset\n", (status & (1 << 6)) ? "" : "not "); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 223 | spi_prettyprint_status_register_common(status); |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 224 | } |
| 225 | |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 226 | /* Prettyprint the status register. Works for |
| 227 | * SST 25VF016 |
| 228 | */ |
| 229 | void spi_prettyprint_status_register_sst25vf016(uint8_t status) |
| 230 | { |
hailfinger | 9cd4cf1 | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 231 | const char *bpt[] = { |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 232 | "none", |
| 233 | "1F0000H-1FFFFFH", |
| 234 | "1E0000H-1FFFFFH", |
| 235 | "1C0000H-1FFFFFH", |
| 236 | "180000H-1FFFFFH", |
| 237 | "100000H-1FFFFFH", |
hailfinger | 9cd4cf1 | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 238 | "all", "all" |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 239 | }; |
| 240 | printf_debug("Chip status register: Block Protect Write Disable " |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 241 | "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not "); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 242 | printf_debug("Chip status register: Auto Address Increment Programming " |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 243 | "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not "); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 244 | spi_prettyprint_status_register_common(status); |
| 245 | printf_debug("Resulting block protection : %s\n", |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 246 | bpt[(status & 0x1c) >> 2]); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void spi_prettyprint_status_register(struct flashchip *flash) |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 250 | { |
| 251 | uint8_t status; |
| 252 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 253 | status = spi_read_status_register(); |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 254 | printf_debug("Chip status register is %02x\n", status); |
| 255 | switch (flash->manufacture_id) { |
| 256 | case ST_ID: |
hailfinger | 8b86913 | 2008-05-15 22:32:08 +0000 | [diff] [blame] | 257 | if (((flash->model_id & 0xff00) == 0x2000) || |
| 258 | ((flash->model_id & 0xff00) == 0x2500)) |
| 259 | spi_prettyprint_status_register_st_m25p(status); |
| 260 | break; |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 261 | case MX_ID: |
| 262 | if ((flash->model_id & 0xff00) == 0x2000) |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 263 | spi_prettyprint_status_register_st_m25p(status); |
| 264 | break; |
| 265 | case SST_ID: |
| 266 | if (flash->model_id == SST_25VF016B) |
| 267 | spi_prettyprint_status_register_sst25vf016(status); |
hailfinger | f1961cb | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 268 | break; |
| 269 | } |
| 270 | } |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 271 | |
hailfinger | ffcf81a | 2008-11-03 00:02:11 +0000 | [diff] [blame] | 272 | int spi_chip_erase_60(struct flashchip *flash) |
| 273 | { |
| 274 | const unsigned char cmd[JEDEC_CE_60_OUTSIZE] = {JEDEC_CE_60}; |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 275 | int result; |
hailfinger | ffcf81a | 2008-11-03 00:02:11 +0000 | [diff] [blame] | 276 | |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 277 | result = spi_disable_blockprotect(); |
| 278 | if (result) { |
| 279 | printf_debug("spi_disable_blockprotect failed\n"); |
| 280 | return result; |
| 281 | } |
| 282 | result = spi_write_enable(); |
| 283 | if (result) { |
| 284 | printf_debug("spi_write_enable failed\n"); |
| 285 | return result; |
| 286 | } |
hailfinger | ffcf81a | 2008-11-03 00:02:11 +0000 | [diff] [blame] | 287 | /* Send CE (Chip Erase) */ |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 288 | result = spi_command(sizeof(cmd), 0, cmd, NULL); |
| 289 | if (result) { |
| 290 | printf_debug("spi_chip_erase_60 failed sending erase\n"); |
| 291 | return result; |
| 292 | } |
hailfinger | ffcf81a | 2008-11-03 00:02:11 +0000 | [diff] [blame] | 293 | /* Wait until the Write-In-Progress bit is cleared. |
| 294 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 295 | */ |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 296 | /* FIXME: We assume spi_read_status_register will never fail. */ |
hailfinger | ffcf81a | 2008-11-03 00:02:11 +0000 | [diff] [blame] | 297 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 298 | sleep(1); |
| 299 | return 0; |
| 300 | } |
| 301 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 302 | int spi_chip_erase_c7(struct flashchip *flash) |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 303 | { |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 304 | const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = { JEDEC_CE_C7 }; |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 305 | int result; |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 306 | |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 307 | result = spi_disable_blockprotect(); |
| 308 | if (result) { |
| 309 | printf_debug("spi_disable_blockprotect failed\n"); |
| 310 | return result; |
| 311 | } |
| 312 | result = spi_write_enable(); |
| 313 | if (result) { |
| 314 | printf_debug("spi_write_enable failed\n"); |
| 315 | return result; |
| 316 | } |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 317 | /* Send CE (Chip Erase) */ |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 318 | result = spi_command(sizeof(cmd), 0, cmd, NULL); |
| 319 | if (result) { |
| 320 | printf_debug("spi_chip_erase_60 failed sending erase\n"); |
| 321 | return result; |
| 322 | } |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 323 | /* Wait until the Write-In-Progress bit is cleared. |
| 324 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 325 | */ |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 326 | /* FIXME: We assume spi_read_status_register will never fail. */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 327 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 328 | sleep(1); |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 332 | int spi_chip_erase_60_c7(struct flashchip *flash) |
| 333 | { |
| 334 | int result; |
| 335 | result = spi_chip_erase_60(flash); |
| 336 | if (result) { |
| 337 | printf_debug("spi_chip_erase_60 failed, trying c7\n"); |
| 338 | result = spi_chip_erase_c7(flash); |
| 339 | } |
| 340 | return result; |
| 341 | } |
| 342 | |
hailfinger | ffcf81a | 2008-11-03 00:02:11 +0000 | [diff] [blame] | 343 | int spi_block_erase_52(const struct flashchip *flash, unsigned long addr) |
| 344 | { |
| 345 | unsigned char cmd[JEDEC_BE_52_OUTSIZE] = {JEDEC_BE_52}; |
| 346 | |
| 347 | cmd[1] = (addr & 0x00ff0000) >> 16; |
| 348 | cmd[2] = (addr & 0x0000ff00) >> 8; |
| 349 | cmd[3] = (addr & 0x000000ff); |
| 350 | spi_write_enable(); |
| 351 | /* Send BE (Block Erase) */ |
| 352 | spi_command(sizeof(cmd), 0, cmd, NULL); |
| 353 | /* Wait until the Write-In-Progress bit is cleared. |
| 354 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 355 | */ |
| 356 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 357 | usleep(100 * 1000); |
| 358 | return 0; |
| 359 | } |
| 360 | |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 361 | /* Block size is usually |
| 362 | * 64k for Macronix |
| 363 | * 32k for SST |
| 364 | * 4-32k non-uniform for EON |
| 365 | */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 366 | int spi_block_erase_d8(const struct flashchip *flash, unsigned long addr) |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 367 | { |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 368 | unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = { JEDEC_BE_D8 }; |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 369 | |
| 370 | cmd[1] = (addr & 0x00ff0000) >> 16; |
| 371 | cmd[2] = (addr & 0x0000ff00) >> 8; |
| 372 | cmd[3] = (addr & 0x000000ff); |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 373 | spi_write_enable(); |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 374 | /* Send BE (Block Erase) */ |
stuge | 494b4eb | 2008-07-07 06:38:51 +0000 | [diff] [blame] | 375 | spi_command(sizeof(cmd), 0, cmd, NULL); |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 376 | /* Wait until the Write-In-Progress bit is cleared. |
| 377 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 378 | */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 379 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 380 | usleep(100 * 1000); |
| 381 | return 0; |
| 382 | } |
| 383 | |
stepan | 0f7bff0 | 2008-10-29 22:13:20 +0000 | [diff] [blame] | 384 | int spi_chip_erase_d8(struct flashchip *flash) |
| 385 | { |
| 386 | int i, rc = 0; |
| 387 | int total_size = flash->total_size * 1024; |
| 388 | int erase_size = 64 * 1024; |
| 389 | |
| 390 | spi_disable_blockprotect(); |
| 391 | |
| 392 | printf("Erasing chip: \n"); |
| 393 | |
| 394 | for (i = 0; i < total_size / erase_size; i++) { |
| 395 | rc = spi_block_erase_d8(flash, i * erase_size); |
| 396 | if (rc) { |
| 397 | printf("Error erasing block at 0x%x\n", i); |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | printf("\n"); |
| 403 | |
| 404 | return rc; |
| 405 | } |
| 406 | |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 407 | /* Sector size is usually 4k, though Macronix eliteflash has 64k */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 408 | int spi_sector_erase(const struct flashchip *flash, unsigned long addr) |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 409 | { |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 410 | unsigned char cmd[JEDEC_SE_OUTSIZE] = { JEDEC_SE }; |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 411 | cmd[1] = (addr & 0x00ff0000) >> 16; |
| 412 | cmd[2] = (addr & 0x0000ff00) >> 8; |
| 413 | cmd[3] = (addr & 0x000000ff); |
| 414 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 415 | spi_write_enable(); |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 416 | /* Send SE (Sector Erase) */ |
stuge | 494b4eb | 2008-07-07 06:38:51 +0000 | [diff] [blame] | 417 | spi_command(sizeof(cmd), 0, cmd, NULL); |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 418 | /* Wait until the Write-In-Progress bit is cleared. |
| 419 | * This usually takes 15-800 ms, so wait in 10 ms steps. |
| 420 | */ |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 421 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
hailfinger | 1b24dbb | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 422 | usleep(10 * 1000); |
| 423 | return 0; |
| 424 | } |
| 425 | |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 426 | /* |
| 427 | * This is according the SST25VF016 datasheet, who knows it is more |
| 428 | * generic that this... |
| 429 | */ |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 430 | int spi_write_status_register(int status) |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 431 | { |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 432 | const unsigned char cmd[JEDEC_WRSR_OUTSIZE] = |
| 433 | { JEDEC_WRSR, (unsigned char)status }; |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 434 | |
| 435 | /* Send WRSR (Write Status Register) */ |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 436 | return spi_command(sizeof(cmd), 0, cmd, NULL); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | void spi_byte_program(int address, uint8_t byte) |
| 440 | { |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 441 | const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = { |
| 442 | JEDEC_BYTE_PROGRAM, |
| 443 | (address >> 16) & 0xff, |
| 444 | (address >> 8) & 0xff, |
| 445 | (address >> 0) & 0xff, |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 446 | byte |
| 447 | }; |
| 448 | |
| 449 | /* Send Byte-Program */ |
stuge | 494b4eb | 2008-07-07 06:38:51 +0000 | [diff] [blame] | 450 | spi_command(sizeof(cmd), 0, cmd, NULL); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 451 | } |
| 452 | |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 453 | int spi_disable_blockprotect(void) |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 454 | { |
| 455 | uint8_t status; |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 456 | int result; |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 457 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 458 | status = spi_read_status_register(); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 459 | /* If there is block protection in effect, unprotect it first. */ |
| 460 | if ((status & 0x3c) != 0) { |
| 461 | printf_debug("Some block protection in effect, disabling\n"); |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 462 | result = spi_write_enable(); |
| 463 | if (result) { |
| 464 | printf_debug("spi_write_enable failed\n"); |
| 465 | return result; |
| 466 | } |
| 467 | result = spi_write_status_register(status & ~0x3c); |
| 468 | if (result) { |
| 469 | printf_debug("spi_write_status_register failed\n"); |
| 470 | return result; |
| 471 | } |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 472 | } |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 473 | return 0; |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 474 | } |
| 475 | |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 476 | int spi_nbyte_read(int address, uint8_t *bytes, int len) |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 477 | { |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 478 | const unsigned char cmd[JEDEC_READ_OUTSIZE] = { |
| 479 | JEDEC_READ, |
hailfinger | 9cd4cf1 | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 480 | (address >> 16) & 0xff, |
| 481 | (address >> 8) & 0xff, |
| 482 | (address >> 0) & 0xff, |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 483 | }; |
| 484 | |
| 485 | /* Send Read */ |
hailfinger | c1b2e91 | 2008-11-18 00:41:02 +0000 | [diff] [blame] | 486 | return spi_command(sizeof(cmd), len, cmd, bytes); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 487 | } |
| 488 | |
stuge | 2bb6ab3 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 489 | int spi_chip_read(struct flashchip *flash, uint8_t *buf) |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 490 | { |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 491 | switch (flashbus) { |
| 492 | case BUS_TYPE_IT87XX_SPI: |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 493 | return it8716f_spi_chip_read(flash, buf); |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 494 | case BUS_TYPE_ICH7_SPI: |
| 495 | case BUS_TYPE_ICH9_SPI: |
| 496 | case BUS_TYPE_VIA_SPI: |
ruik | 9bc51c0 | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 497 | return ich_spi_read(flash, buf); |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 498 | default: |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 499 | printf_debug |
| 500 | ("%s called, but no SPI chipset/strapping detected\n", |
| 501 | __FUNCTION__); |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 502 | } |
| 503 | |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 504 | return 1; |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 505 | } |
| 506 | |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 507 | int spi_chip_write(struct flashchip *flash, uint8_t *buf) |
| 508 | { |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 509 | switch (flashbus) { |
| 510 | case BUS_TYPE_IT87XX_SPI: |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 511 | return it8716f_spi_chip_write(flash, buf); |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 512 | case BUS_TYPE_ICH7_SPI: |
| 513 | case BUS_TYPE_ICH9_SPI: |
| 514 | case BUS_TYPE_VIA_SPI: |
ruik | 9bc51c0 | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 515 | return ich_spi_write(flash, buf); |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 516 | default: |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 517 | printf_debug |
| 518 | ("%s called, but no SPI chipset/strapping detected\n", |
| 519 | __FUNCTION__); |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 520 | } |
| 521 | |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 522 | return 1; |
hailfinger | f71c0ac | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 523 | } |