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