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 |
| 5 | * Copyright (C) 2008 Ronald Hoogenboom <ronald@zonnet.nl> |
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" |
| 30 | |
| 31 | #define ITE_SUPERIO_PORT1 0x2e |
| 32 | #define ITE_SUPERIO_PORT2 0x4e |
| 33 | |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 34 | /* Read Electronic ID */ |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 35 | #define JEDEC_RDID {0x9f} |
| 36 | #define JEDEC_RDID_OUTSIZE 0x01 |
| 37 | #define JEDEC_RDID_INSIZE 0x03 |
| 38 | |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 39 | /* Write Enable */ |
| 40 | #define JEDEC_WREN {0x06} |
| 41 | #define JEDEC_WREN_OUTSIZE 0x01 |
| 42 | #define JEDEC_WREN_INSIZE 0x00 |
| 43 | |
| 44 | /* Write Disable */ |
| 45 | #define JEDEC_WRDI {0x04} |
| 46 | #define JEDEC_WRDI_OUTSIZE 0x01 |
| 47 | #define JEDEC_WRDI_INSIZE 0x00 |
| 48 | |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 49 | /* Chip Erase 0x60 is supported by Macronix/SST chips. */ |
Carl-Daniel Hailfinger | 21c7890 | 2007-12-17 14:33:32 +0000 | [diff] [blame] | 50 | #define JEDEC_CE_60 {0x60}; |
| 51 | #define JEDEC_CE_60_OUTSIZE 0x01 |
| 52 | #define JEDEC_CE_60_INSIZE 0x00 |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 53 | |
Carl-Daniel Hailfinger | 21c7890 | 2007-12-17 14:33:32 +0000 | [diff] [blame] | 54 | /* Chip Erase 0xc7 is supported by ST/EON/Macronix chips. */ |
| 55 | #define JEDEC_CE_C7 {0xc7}; |
| 56 | #define JEDEC_CE_C7_OUTSIZE 0x01 |
| 57 | #define JEDEC_CE_C7_INSIZE 0x00 |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 58 | |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 59 | /* Block Erase 0x52 is supported by SST chips. */ |
Carl-Daniel Hailfinger | 21c7890 | 2007-12-17 14:33:32 +0000 | [diff] [blame] | 60 | #define JEDEC_BE_52 {0x52}; |
| 61 | #define JEDEC_BE_52_OUTSIZE 0x04 |
| 62 | #define JEDEC_BE_52_INSIZE 0x00 |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 63 | |
| 64 | /* Block Erase 0xd8 is supported by EON/Macronix chips. */ |
Carl-Daniel Hailfinger | 21c7890 | 2007-12-17 14:33:32 +0000 | [diff] [blame] | 65 | #define JEDEC_BE_D8 {0xd8}; |
| 66 | #define JEDEC_BE_D8_OUTSIZE 0x04 |
| 67 | #define JEDEC_BE_D8_INSIZE 0x00 |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 68 | |
| 69 | /* Sector Erase 0x20 is supported by Macronix/SST chips. */ |
| 70 | #define JEDEC_SE {0x20}; |
| 71 | #define JEDEC_SE_OUTSIZE 0x04 |
| 72 | #define JEDEC_SE_INSIZE 0x00 |
| 73 | |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 74 | /* Read Status Register */ |
| 75 | #define JEDEC_RDSR {0x05}; |
| 76 | #define JEDEC_RDSR_OUTSIZE 0x01 |
| 77 | #define JEDEC_RDSR_INSIZE 0x01 |
| 78 | #define JEDEC_RDSR_BIT_WIP (0x01 << 0) |
| 79 | |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 80 | /* Write Status Register */ |
| 81 | #define JEDEC_WRSR 0x01 |
| 82 | #define JEDEC_WRSR_OUTSIZE 0x02 |
| 83 | #define JEDEC_WRSR_INSIZE 0x00 |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 84 | |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 85 | /* Read the memory */ |
| 86 | #define JEDEC_READ 0x03 |
| 87 | #define JEDEC_READ_OUTSIZE 0x04 |
| 88 | /* JEDEC_READ_INSIZE : any length */ |
| 89 | |
| 90 | /* Write memory byte */ |
| 91 | #define JEDEC_BYTE_PROGRAM 0x02 |
| 92 | #define JEDEC_BYTE_PROGRAM_OUTSIZE 0x05 |
| 93 | #define JEDEC_BYTE_PROGRAM_INSIZE 0x00 |
| 94 | |
| 95 | uint16_t it8716f_flashport = 0; |
| 96 | /* use fast 33MHz SPI (<>0) or slow 16MHz (0) */ |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 97 | int fast_spi = 1; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 98 | |
| 99 | void spi_prettyprint_status_register(struct flashchip *flash); |
| 100 | void spi_disable_blockprotect(void); |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 101 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 102 | /* Generic Super I/O helper functions */ |
| 103 | uint8_t regval(uint16_t port, uint8_t reg) |
| 104 | { |
| 105 | outb(reg, port); |
| 106 | return inb(port + 1); |
| 107 | } |
| 108 | |
| 109 | void regwrite(uint16_t port, uint8_t reg, uint8_t val) |
| 110 | { |
| 111 | outb(reg, port); |
| 112 | outb(val, port + 1); |
| 113 | } |
| 114 | |
| 115 | /* Helper functions for most recent ITE IT87xx Super I/O chips */ |
| 116 | #define CHIP_ID_BYTE1_REG 0x20 |
| 117 | #define CHIP_ID_BYTE2_REG 0x21 |
| 118 | static void enter_conf_mode_ite(uint16_t port) |
| 119 | { |
| 120 | outb(0x87, port); |
| 121 | outb(0x01, port); |
| 122 | outb(0x55, port); |
| 123 | if (port == ITE_SUPERIO_PORT1) |
| 124 | outb(0x55, port); |
| 125 | else |
| 126 | outb(0xaa, port); |
| 127 | } |
| 128 | |
| 129 | static void exit_conf_mode_ite(uint16_t port) |
| 130 | { |
| 131 | regwrite(port, 0x02, 0x02); |
| 132 | } |
| 133 | |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 134 | static uint16_t find_ite_spi_flash_port(uint16_t port) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 135 | { |
| 136 | uint8_t tmp = 0; |
| 137 | uint16_t id, flashport = 0; |
| 138 | |
| 139 | enter_conf_mode_ite(port); |
| 140 | |
| 141 | id = regval(port, CHIP_ID_BYTE1_REG) << 8; |
| 142 | id |= regval(port, CHIP_ID_BYTE2_REG); |
| 143 | |
| 144 | /* TODO: Handle more IT87xx if they support flash translation */ |
| 145 | if (id == 0x8716) { |
| 146 | /* NOLDN, reg 0x24, mask out lowest bit (suspend) */ |
| 147 | tmp = regval(port, 0x24) & 0xFE; |
| 148 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 149 | 0xFFFE0000, 0xFFFFFFFF, (tmp & 1 << 1) ? "en" : "dis"); |
| 150 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 151 | 0x000E0000, 0x000FFFFF, (tmp & 1 << 1) ? "en" : "dis"); |
| 152 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 153 | 0xFFEE0000, 0xFFEFFFFF, (tmp & 1 << 2) ? "en" : "dis"); |
| 154 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 155 | 0xFFF80000, 0xFFFEFFFF, (tmp & 1 << 3) ? "en" : "dis"); |
| 156 | printf("LPC write to serial flash %sabled\n", |
| 157 | (tmp & 1 << 4) ? "en" : "dis"); |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 158 | printf("serial flash pin %i\n", (tmp & 1 << 5) ? 87 : 29); |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 159 | /* LDN 0x7, reg 0x64/0x65 */ |
| 160 | regwrite(port, 0x07, 0x7); |
| 161 | flashport = regval(port, 0x64) << 8; |
| 162 | flashport |= regval(port, 0x65); |
| 163 | } |
| 164 | exit_conf_mode_ite(port); |
| 165 | return flashport; |
| 166 | } |
| 167 | |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 168 | int it87xx_probe_spi_flash(const char *name) |
| 169 | { |
| 170 | it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT1); |
| 171 | if (!it8716f_flashport) |
| 172 | it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT2); |
| 173 | return (!it8716f_flashport); |
| 174 | } |
| 175 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 176 | /* The IT8716F only supports commands with length 1,2,4,5 bytes including |
| 177 | command byte and can not read more than 3 bytes from the device. |
| 178 | This function expects writearr[0] to be the first byte sent to the device, |
| 179 | whereas the IT8716F splits commands internally into address and non-address |
| 180 | commands with the address in inverse wire order. That's why the register |
| 181 | ordering in case 4 and 5 may seem strange. */ |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 182 | static int it8716f_spi_command(uint16_t port, unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 183 | { |
| 184 | uint8_t busy, writeenc; |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 185 | int i; |
| 186 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 187 | do { |
| 188 | busy = inb(port) & 0x80; |
| 189 | } while (busy); |
| 190 | if (readcnt > 3) { |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 191 | printf("%s called with unsupported readcnt %i.\n", |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 192 | __FUNCTION__, readcnt); |
| 193 | return 1; |
| 194 | } |
| 195 | switch (writecnt) { |
| 196 | case 1: |
| 197 | outb(writearr[0], port + 1); |
| 198 | writeenc = 0x0; |
| 199 | break; |
| 200 | case 2: |
| 201 | outb(writearr[0], port + 1); |
| 202 | outb(writearr[1], port + 7); |
| 203 | writeenc = 0x1; |
| 204 | break; |
| 205 | case 4: |
| 206 | outb(writearr[0], port + 1); |
| 207 | outb(writearr[1], port + 4); |
| 208 | outb(writearr[2], port + 3); |
| 209 | outb(writearr[3], port + 2); |
| 210 | writeenc = 0x2; |
| 211 | break; |
| 212 | case 5: |
| 213 | outb(writearr[0], port + 1); |
| 214 | outb(writearr[1], port + 4); |
| 215 | outb(writearr[2], port + 3); |
| 216 | outb(writearr[3], port + 2); |
| 217 | outb(writearr[4], port + 7); |
| 218 | writeenc = 0x3; |
| 219 | break; |
| 220 | default: |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 221 | printf("%s called with unsupported writecnt %i.\n", |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 222 | __FUNCTION__, writecnt); |
| 223 | return 1; |
| 224 | } |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 225 | /* Start IO, 33 or 16 MHz, readcnt input bytes, writecnt output bytes. |
| 226 | * Note: |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 227 | * We can't use writecnt directly, but have to use a strange encoding. |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 228 | */ |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 229 | outb(((0x4 + (fast_spi ? 1 : 0)) << 4) | ((readcnt & 0x3) << 2) | (writeenc), port); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 230 | |
Ronald Hoogenboom | d4554c5 | 2008-01-21 23:55:08 +0000 | [diff] [blame] | 231 | if (readcnt > 0) { |
| 232 | do { |
| 233 | busy = inb(port) & 0x80; |
| 234 | } while (busy); |
| 235 | |
| 236 | for (i = 0; i < readcnt; i++) { |
| 237 | readarr[i] = inb(port + 5 + i); |
| 238 | } |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 244 | int generic_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] | 245 | { |
| 246 | if (it8716f_flashport) |
| 247 | return it8716f_spi_command(it8716f_flashport, writecnt, readcnt, writearr, readarr); |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 248 | printf_debug("%s called, but no SPI chipset detected\n", __FUNCTION__); |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 249 | return 1; |
| 250 | } |
| 251 | |
| 252 | static int generic_spi_rdid(unsigned char *readarr) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 253 | { |
| 254 | const unsigned char cmd[] = JEDEC_RDID; |
| 255 | |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 256 | if (generic_spi_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, cmd, readarr)) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 257 | return 1; |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 258 | 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] | 259 | return 0; |
| 260 | } |
| 261 | |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 262 | void generic_spi_write_enable() |
| 263 | { |
| 264 | const unsigned char cmd[] = JEDEC_WREN; |
| 265 | |
| 266 | /* Send WREN (Write Enable) */ |
| 267 | generic_spi_command(JEDEC_WREN_OUTSIZE, JEDEC_WREN_INSIZE, cmd, NULL); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void generic_spi_write_disable() |
| 271 | { |
| 272 | const unsigned char cmd[] = JEDEC_WRDI; |
| 273 | |
| 274 | /* Send WRDI (Write Disable) */ |
| 275 | generic_spi_command(JEDEC_WRDI_OUTSIZE, JEDEC_WRDI_INSIZE, cmd, NULL); |
| 276 | } |
| 277 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 278 | int probe_spi(struct flashchip *flash) |
| 279 | { |
| 280 | unsigned char readarr[3]; |
Carl-Daniel Hailfinger | 1263d2a | 2008-02-06 22:07:58 +0000 | [diff] [blame] | 281 | uint32_t manuf_id; |
| 282 | uint32_t model_id; |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 283 | if (!generic_spi_rdid(readarr)) { |
Carl-Daniel Hailfinger | 1263d2a | 2008-02-06 22:07:58 +0000 | [diff] [blame] | 284 | /* Check if this is a continuation vendor ID */ |
| 285 | if (readarr[0] == 0x7f) { |
| 286 | manuf_id = (readarr[0] << 8) | readarr[1]; |
| 287 | model_id = readarr[2]; |
| 288 | } else { |
| 289 | manuf_id = readarr[0]; |
| 290 | model_id = (readarr[1] << 8) | readarr[2]; |
| 291 | } |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 292 | 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] | 293 | if (manuf_id == flash->manufacture_id && |
| 294 | model_id == flash->model_id) { |
| 295 | /* Print the status register to tell the |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 296 | * user about possible write protection. |
| 297 | */ |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 298 | spi_prettyprint_status_register(flash); |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 299 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 300 | return 1; |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 301 | } |
Carl-Daniel Hailfinger | e973b05 | 2008-01-04 16:22:09 +0000 | [diff] [blame] | 302 | /* Test if this is a pure vendor match. */ |
| 303 | if (manuf_id == flash->manufacture_id && |
| 304 | GENERIC_DEVICE_ID == flash->model_id) |
| 305 | return 1; |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 311 | uint8_t generic_spi_read_status_register() |
| 312 | { |
| 313 | const unsigned char cmd[] = JEDEC_RDSR; |
| 314 | unsigned char readarr[1]; |
| 315 | |
| 316 | /* Read Status Register */ |
| 317 | generic_spi_command(JEDEC_RDSR_OUTSIZE, JEDEC_RDSR_INSIZE, cmd, readarr); |
| 318 | return readarr[0]; |
| 319 | } |
| 320 | |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 321 | /* Prettyprint the status register. Common definitions. |
| 322 | */ |
| 323 | void spi_prettyprint_status_register_common(uint8_t status) |
| 324 | { |
| 325 | printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is " |
| 326 | "%sset\n", (status & (1 << 5)) ? "" : "not "); |
| 327 | printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is " |
| 328 | "%sset\n", (status & (1 << 4)) ? "" : "not "); |
| 329 | printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is " |
| 330 | "%sset\n", (status & (1 << 3)) ? "" : "not "); |
| 331 | printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is " |
| 332 | "%sset\n", (status & (1 << 2)) ? "" : "not "); |
| 333 | printf_debug("Chip status register: Write Enable Latch (WEL) is " |
| 334 | "%sset\n", (status & (1 << 1)) ? "" : "not "); |
| 335 | printf_debug("Chip status register: Write In Progress (WIP/BUSY) is " |
| 336 | "%sset\n", (status & (1 << 0)) ? "" : "not "); |
| 337 | } |
| 338 | |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 339 | /* Prettyprint the status register. Works for |
| 340 | * ST M25P series |
| 341 | * MX MX25L series |
| 342 | */ |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 343 | void spi_prettyprint_status_register_st_m25p(uint8_t status) |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 344 | { |
| 345 | printf_debug("Chip status register: Status Register Write Disable " |
| 346 | "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not "); |
| 347 | printf_debug("Chip status register: Bit 6 is " |
| 348 | "%sset\n", (status & (1 << 6)) ? "" : "not "); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 349 | spi_prettyprint_status_register_common(status); |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 352 | /* Prettyprint the status register. Works for |
| 353 | * SST 25VF016 |
| 354 | */ |
| 355 | void spi_prettyprint_status_register_sst25vf016(uint8_t status) |
| 356 | { |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 357 | const char *bpt[] = { |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 358 | "none", |
| 359 | "1F0000H-1FFFFFH", |
| 360 | "1E0000H-1FFFFFH", |
| 361 | "1C0000H-1FFFFFH", |
| 362 | "180000H-1FFFFFH", |
| 363 | "100000H-1FFFFFH", |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 364 | "all", "all" |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 365 | }; |
| 366 | printf_debug("Chip status register: Block Protect Write Disable " |
| 367 | "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not "); |
| 368 | printf_debug("Chip status register: Auto Address Increment Programming " |
| 369 | "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not "); |
| 370 | spi_prettyprint_status_register_common(status); |
| 371 | printf_debug("Resulting block protection : %s\n", |
| 372 | bpt[(status & 0x1c) >> 2]); |
| 373 | } |
| 374 | |
| 375 | void spi_prettyprint_status_register(struct flashchip *flash) |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 376 | { |
| 377 | uint8_t status; |
| 378 | |
| 379 | status = generic_spi_read_status_register(); |
| 380 | printf_debug("Chip status register is %02x\n", status); |
| 381 | switch (flash->manufacture_id) { |
| 382 | case ST_ID: |
| 383 | case MX_ID: |
| 384 | if ((flash->model_id & 0xff00) == 0x2000) |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 385 | spi_prettyprint_status_register_st_m25p(status); |
| 386 | break; |
| 387 | case SST_ID: |
| 388 | if (flash->model_id == SST_25VF016B) |
| 389 | spi_prettyprint_status_register_sst25vf016(status); |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 390 | break; |
| 391 | } |
| 392 | } |
| 393 | |
Carl-Daniel Hailfinger | 21c7890 | 2007-12-17 14:33:32 +0000 | [diff] [blame] | 394 | int generic_spi_chip_erase_c7(struct flashchip *flash) |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 395 | { |
Carl-Daniel Hailfinger | 21c7890 | 2007-12-17 14:33:32 +0000 | [diff] [blame] | 396 | const unsigned char cmd[] = JEDEC_CE_C7; |
Carl-Daniel Hailfinger | f5df46f | 2007-12-16 21:15:27 +0000 | [diff] [blame] | 397 | |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 398 | spi_disable_blockprotect(); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 399 | generic_spi_write_enable(); |
| 400 | /* Send CE (Chip Erase) */ |
Carl-Daniel Hailfinger | 21c7890 | 2007-12-17 14:33:32 +0000 | [diff] [blame] | 401 | generic_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] | 402 | /* Wait until the Write-In-Progress bit is cleared. |
| 403 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 404 | */ |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 405 | while (generic_spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 406 | sleep(1); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 410 | /* Block size is usually |
| 411 | * 64k for Macronix |
| 412 | * 32k for SST |
| 413 | * 4-32k non-uniform for EON |
| 414 | */ |
Carl-Daniel Hailfinger | 21c7890 | 2007-12-17 14:33:32 +0000 | [diff] [blame] | 415 | int generic_spi_block_erase_d8(const struct flashchip *flash, unsigned long addr) |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 416 | { |
Carl-Daniel Hailfinger | 21c7890 | 2007-12-17 14:33:32 +0000 | [diff] [blame] | 417 | unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = JEDEC_BE_D8; |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 418 | |
| 419 | cmd[1] = (addr & 0x00ff0000) >> 16; |
| 420 | cmd[2] = (addr & 0x0000ff00) >> 8; |
| 421 | cmd[3] = (addr & 0x000000ff); |
| 422 | generic_spi_write_enable(); |
| 423 | /* Send BE (Block Erase) */ |
Carl-Daniel Hailfinger | 21c7890 | 2007-12-17 14:33:32 +0000 | [diff] [blame] | 424 | generic_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] | 425 | /* Wait until the Write-In-Progress bit is cleared. |
| 426 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 427 | */ |
| 428 | while (generic_spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 429 | usleep(100 * 1000); |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | /* Sector size is usually 4k, though Macronix eliteflash has 64k */ |
| 434 | int generic_spi_sector_erase(const struct flashchip *flash, unsigned long addr) |
| 435 | { |
| 436 | unsigned char cmd[JEDEC_SE_OUTSIZE] = JEDEC_SE; |
| 437 | cmd[1] = (addr & 0x00ff0000) >> 16; |
| 438 | cmd[2] = (addr & 0x0000ff00) >> 8; |
| 439 | cmd[3] = (addr & 0x000000ff); |
| 440 | |
| 441 | generic_spi_write_enable(); |
| 442 | /* Send SE (Sector Erase) */ |
| 443 | generic_spi_command(JEDEC_SE_OUTSIZE, JEDEC_SE_INSIZE, cmd, NULL); |
| 444 | /* Wait until the Write-In-Progress bit is cleared. |
| 445 | * This usually takes 15-800 ms, so wait in 10 ms steps. |
| 446 | */ |
| 447 | while (generic_spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 448 | usleep(10 * 1000); |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | /* Page size is usually 256 bytes */ |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 453 | void it8716f_spi_page_program(int block, uint8_t *buf, uint8_t *bios) { |
| 454 | int i; |
| 455 | |
| 456 | generic_spi_write_enable(); |
| 457 | outb(0x06 , it8716f_flashport + 1); |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 458 | outb(((2 + (fast_spi ? 1 : 0)) << 4), it8716f_flashport); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 459 | for (i = 0; i < 256; i++) { |
| 460 | bios[256 * block + i] = buf[256 * block + i]; |
| 461 | } |
| 462 | outb(0, it8716f_flashport); |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 463 | /* Wait until the Write-In-Progress bit is cleared. |
| 464 | * This usually takes 1-10 ms, so wait in 1 ms steps. |
| 465 | */ |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 466 | while (generic_spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 467 | usleep(1000); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | void generic_spi_page_program(int block, uint8_t *buf, uint8_t *bios) |
| 471 | { |
| 472 | if (it8716f_flashport) |
| 473 | it8716f_spi_page_program(block, buf, bios); |
| 474 | } |
| 475 | |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 476 | /* |
| 477 | * This is according the SST25VF016 datasheet, who knows it is more |
| 478 | * generic that this... |
| 479 | */ |
| 480 | void spi_write_status_register(int status) |
| 481 | { |
| 482 | const unsigned char cmd[] = {JEDEC_WRSR, (unsigned char)status}; |
| 483 | |
| 484 | /* Send WRSR (Write Status Register) */ |
| 485 | generic_spi_command(JEDEC_WRSR_OUTSIZE, JEDEC_WRSR_INSIZE, cmd, NULL); |
| 486 | } |
| 487 | |
| 488 | void spi_byte_program(int address, uint8_t byte) |
| 489 | { |
| 490 | const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {JEDEC_BYTE_PROGRAM, |
| 491 | (address>>16)&0xff, |
| 492 | (address>>8)&0xff, |
| 493 | (address>>0)&0xff, |
| 494 | byte |
| 495 | }; |
| 496 | |
| 497 | /* Send Byte-Program */ |
| 498 | generic_spi_command(JEDEC_BYTE_PROGRAM_OUTSIZE, JEDEC_BYTE_PROGRAM_INSIZE, cmd, NULL); |
| 499 | } |
| 500 | |
| 501 | void spi_disable_blockprotect(void) |
| 502 | { |
| 503 | uint8_t status; |
| 504 | |
| 505 | status = generic_spi_read_status_register(); |
| 506 | /* If there is block protection in effect, unprotect it first. */ |
| 507 | if ((status & 0x3c) != 0) { |
| 508 | printf_debug("Some block protection in effect, disabling\n"); |
| 509 | generic_spi_write_enable(); |
| 510 | spi_write_status_register(status & ~0x3c); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles |
| 516 | * Program chip using firmware cycle byte programming. (SLOW!) |
| 517 | */ |
| 518 | int it8716f_over512k_spi_chip_write(struct flashchip *flash, uint8_t *buf) |
| 519 | { |
| 520 | int total_size = 1024 * flash->total_size; |
| 521 | int i; |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 522 | fast_spi = 0; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 523 | |
| 524 | spi_disable_blockprotect(); |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 525 | for (i = 0; i < total_size; i++) { |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 526 | generic_spi_write_enable(); |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 527 | spi_byte_program(i, buf[i]); |
| 528 | while (generic_spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 529 | myusec_delay(10); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 530 | } |
| 531 | /* resume normal ops... */ |
| 532 | outb(0x20, it8716f_flashport); |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | void spi_3byte_read(int address, uint8_t *bytes, int len) |
| 537 | { |
| 538 | const unsigned char cmd[JEDEC_READ_OUTSIZE] = {JEDEC_READ, |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 539 | (address >> 16) & 0xff, |
| 540 | (address >> 8) & 0xff, |
| 541 | (address >> 0) & 0xff, |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 542 | }; |
| 543 | |
| 544 | /* Send Read */ |
| 545 | generic_spi_command(JEDEC_READ_OUTSIZE, len, cmd, bytes); |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles |
| 550 | * Need to read this big flash using firmware cycles 3 byte at a time. |
| 551 | */ |
| 552 | int generic_spi_chip_read(struct flashchip *flash, uint8_t *buf) |
| 553 | { |
| 554 | int total_size = 1024 * flash->total_size; |
| 555 | int i; |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 556 | fast_spi = 0; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 557 | |
| 558 | if (total_size > 512 * 1024) { |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 559 | for (i = 0; i < total_size; i += 3) { |
| 560 | int toread = 3; |
| 561 | if (total_size - i < toread) |
| 562 | toread = total_size - i; |
| 563 | spi_3byte_read(i, buf + i, toread); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 564 | } |
| 565 | } else { |
| 566 | memcpy(buf, (const char *)flash->virtual_memory, total_size); |
| 567 | } |
| 568 | return 0; |
| 569 | } |
| 570 | |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 571 | int generic_spi_chip_write(struct flashchip *flash, uint8_t *buf) { |
| 572 | int total_size = 1024 * flash->total_size; |
| 573 | int i; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 574 | if (total_size > 512 * 1024) { |
| 575 | it8716f_over512k_spi_chip_write(flash, buf); |
| 576 | } else { |
| 577 | for (i = 0; i < total_size / 256; i++) { |
| 578 | generic_spi_page_program(i, buf, (uint8_t *)flash->virtual_memory); |
| 579 | } |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 580 | } |
| 581 | return 0; |
| 582 | } |
| 583 | |