Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2007 Carl-Daniel Hailfinger |
| 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" |
| 29 | |
| 30 | #define ITE_SUPERIO_PORT1 0x2e |
| 31 | #define ITE_SUPERIO_PORT2 0x4e |
| 32 | |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 33 | /* Read Electronic ID */ |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 34 | #define JEDEC_RDID {0x9f} |
| 35 | #define JEDEC_RDID_OUTSIZE 0x01 |
| 36 | #define JEDEC_RDID_INSIZE 0x03 |
| 37 | |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 38 | /* Write Enable */ |
| 39 | #define JEDEC_WREN {0x06} |
| 40 | #define JEDEC_WREN_OUTSIZE 0x01 |
| 41 | #define JEDEC_WREN_INSIZE 0x00 |
| 42 | |
| 43 | /* Write Disable */ |
| 44 | #define JEDEC_WRDI {0x04} |
| 45 | #define JEDEC_WRDI_OUTSIZE 0x01 |
| 46 | #define JEDEC_WRDI_INSIZE 0x00 |
| 47 | |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 48 | /* Chip Erase 0x60 is supported by Macronix/SST chips. */ |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 49 | #define JEDEC_CE_1 {0x60}; |
| 50 | #define JEDEC_CE_1_OUTSIZE 0x01 |
| 51 | #define JEDEC_CE_1_INSIZE 0x00 |
| 52 | |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 53 | /* Chip Erase 0xc7 is supported by EON/Macronix chips. */ |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 54 | #define JEDEC_CE_2 {0xc7}; |
| 55 | #define JEDEC_CE_2_OUTSIZE 0x01 |
| 56 | #define JEDEC_CE_2_INSIZE 0x00 |
| 57 | |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 58 | /* Block Erase 0x52 is supported by SST chips. */ |
| 59 | #define JEDEC_BE_1 {0x52}; |
| 60 | #define JEDEC_BE_1_OUTSIZE 0x04 |
| 61 | #define JEDEC_BE_1_INSIZE 0x00 |
| 62 | |
| 63 | /* Block Erase 0xd8 is supported by EON/Macronix chips. */ |
| 64 | #define JEDEC_BE_2 {0xd8}; |
| 65 | #define JEDEC_BE_2_OUTSIZE 0x04 |
| 66 | #define JEDEC_BE_2_INSIZE 0x00 |
| 67 | |
| 68 | /* Sector Erase 0x20 is supported by Macronix/SST chips. */ |
| 69 | #define JEDEC_SE {0x20}; |
| 70 | #define JEDEC_SE_OUTSIZE 0x04 |
| 71 | #define JEDEC_SE_INSIZE 0x00 |
| 72 | |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 73 | /* Read Status Register */ |
| 74 | #define JEDEC_RDSR {0x05}; |
| 75 | #define JEDEC_RDSR_OUTSIZE 0x01 |
| 76 | #define JEDEC_RDSR_INSIZE 0x01 |
| 77 | #define JEDEC_RDSR_BIT_WIP (0x01 << 0) |
| 78 | |
| 79 | uint16_t it8716f_flashport = 0; |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 80 | |
| 81 | /* Generic Super I/O helper functions */ |
| 82 | uint8_t regval(uint16_t port, uint8_t reg) |
| 83 | { |
| 84 | outb(reg, port); |
| 85 | return inb(port + 1); |
| 86 | } |
| 87 | |
| 88 | void regwrite(uint16_t port, uint8_t reg, uint8_t val) |
| 89 | { |
| 90 | outb(reg, port); |
| 91 | outb(val, port + 1); |
| 92 | } |
| 93 | |
| 94 | /* Helper functions for most recent ITE IT87xx Super I/O chips */ |
| 95 | #define CHIP_ID_BYTE1_REG 0x20 |
| 96 | #define CHIP_ID_BYTE2_REG 0x21 |
| 97 | static void enter_conf_mode_ite(uint16_t port) |
| 98 | { |
| 99 | outb(0x87, port); |
| 100 | outb(0x01, port); |
| 101 | outb(0x55, port); |
| 102 | if (port == ITE_SUPERIO_PORT1) |
| 103 | outb(0x55, port); |
| 104 | else |
| 105 | outb(0xaa, port); |
| 106 | } |
| 107 | |
| 108 | static void exit_conf_mode_ite(uint16_t port) |
| 109 | { |
| 110 | regwrite(port, 0x02, 0x02); |
| 111 | } |
| 112 | |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 113 | static uint16_t find_ite_spi_flash_port(uint16_t port) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 114 | { |
| 115 | uint8_t tmp = 0; |
| 116 | uint16_t id, flashport = 0; |
| 117 | |
| 118 | enter_conf_mode_ite(port); |
| 119 | |
| 120 | id = regval(port, CHIP_ID_BYTE1_REG) << 8; |
| 121 | id |= regval(port, CHIP_ID_BYTE2_REG); |
| 122 | |
| 123 | /* TODO: Handle more IT87xx if they support flash translation */ |
| 124 | if (id == 0x8716) { |
| 125 | /* NOLDN, reg 0x24, mask out lowest bit (suspend) */ |
| 126 | tmp = regval(port, 0x24) & 0xFE; |
| 127 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 128 | 0xFFFE0000, 0xFFFFFFFF, (tmp & 1 << 1) ? "en" : "dis"); |
| 129 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 130 | 0x000E0000, 0x000FFFFF, (tmp & 1 << 1) ? "en" : "dis"); |
| 131 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 132 | 0xFFEE0000, 0xFFEFFFFF, (tmp & 1 << 2) ? "en" : "dis"); |
| 133 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 134 | 0xFFF80000, 0xFFFEFFFF, (tmp & 1 << 3) ? "en" : "dis"); |
| 135 | printf("LPC write to serial flash %sabled\n", |
| 136 | (tmp & 1 << 4) ? "en" : "dis"); |
| 137 | printf("serial flash pin %i\n", (tmp & 1 << 5) ? 87 : 29); |
| 138 | /* LDN 0x7, reg 0x64/0x65 */ |
| 139 | regwrite(port, 0x07, 0x7); |
| 140 | flashport = regval(port, 0x64) << 8; |
| 141 | flashport |= regval(port, 0x65); |
| 142 | } |
| 143 | exit_conf_mode_ite(port); |
| 144 | return flashport; |
| 145 | } |
| 146 | |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 147 | int it87xx_probe_spi_flash(const char *name) |
| 148 | { |
| 149 | it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT1); |
| 150 | if (!it8716f_flashport) |
| 151 | it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT2); |
| 152 | return (!it8716f_flashport); |
| 153 | } |
| 154 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 155 | /* The IT8716F only supports commands with length 1,2,4,5 bytes including |
| 156 | command byte and can not read more than 3 bytes from the device. |
| 157 | This function expects writearr[0] to be the first byte sent to the device, |
| 158 | whereas the IT8716F splits commands internally into address and non-address |
| 159 | commands with the address in inverse wire order. That's why the register |
| 160 | ordering in case 4 and 5 may seem strange. */ |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 161 | 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] | 162 | { |
| 163 | uint8_t busy, writeenc; |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 164 | int i; |
| 165 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 166 | do { |
| 167 | busy = inb(port) & 0x80; |
| 168 | } while (busy); |
| 169 | if (readcnt > 3) { |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 170 | printf("%s called with unsupported readcnt %i.\n", |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 171 | __FUNCTION__, readcnt); |
| 172 | return 1; |
| 173 | } |
| 174 | switch (writecnt) { |
| 175 | case 1: |
| 176 | outb(writearr[0], port + 1); |
| 177 | writeenc = 0x0; |
| 178 | break; |
| 179 | case 2: |
| 180 | outb(writearr[0], port + 1); |
| 181 | outb(writearr[1], port + 7); |
| 182 | writeenc = 0x1; |
| 183 | break; |
| 184 | case 4: |
| 185 | outb(writearr[0], port + 1); |
| 186 | outb(writearr[1], port + 4); |
| 187 | outb(writearr[2], port + 3); |
| 188 | outb(writearr[3], port + 2); |
| 189 | writeenc = 0x2; |
| 190 | break; |
| 191 | case 5: |
| 192 | outb(writearr[0], port + 1); |
| 193 | outb(writearr[1], port + 4); |
| 194 | outb(writearr[2], port + 3); |
| 195 | outb(writearr[3], port + 2); |
| 196 | outb(writearr[4], port + 7); |
| 197 | writeenc = 0x3; |
| 198 | break; |
| 199 | default: |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 200 | printf("%s called with unsupported writecnt %i.\n", |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 201 | __FUNCTION__, writecnt); |
| 202 | return 1; |
| 203 | } |
| 204 | /* Start IO, 33MHz, readcnt input bytes, writecnt output bytes. Note: |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 205 | * 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] | 206 | */ |
| 207 | outb((0x5 << 4) | ((readcnt & 0x3) << 2) | (writeenc), port); |
| 208 | do { |
| 209 | busy = inb(port) & 0x80; |
| 210 | } while (busy); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 211 | |
| 212 | for (i = 0; i < readcnt; i++) { |
| 213 | readarr[i] = inb(port + 5 + i); |
| 214 | } |
| 215 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 219 | 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] | 220 | { |
| 221 | if (it8716f_flashport) |
| 222 | return it8716f_spi_command(it8716f_flashport, writecnt, readcnt, writearr, readarr); |
| 223 | printf("%s called, but no SPI chipset detected\n", __FUNCTION__); |
| 224 | return 1; |
| 225 | } |
| 226 | |
| 227 | static int generic_spi_rdid(unsigned char *readarr) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 228 | { |
| 229 | const unsigned char cmd[] = JEDEC_RDID; |
| 230 | |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 231 | 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] | 232 | return 1; |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 233 | printf("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] | 234 | return 0; |
| 235 | } |
| 236 | |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 237 | void generic_spi_write_enable() |
| 238 | { |
| 239 | const unsigned char cmd[] = JEDEC_WREN; |
| 240 | |
| 241 | /* Send WREN (Write Enable) */ |
| 242 | generic_spi_command(JEDEC_WREN_OUTSIZE, JEDEC_WREN_INSIZE, cmd, NULL); |
| 243 | |
| 244 | } |
| 245 | |
| 246 | void generic_spi_write_disable() |
| 247 | { |
| 248 | const unsigned char cmd[] = JEDEC_WRDI; |
| 249 | |
| 250 | /* Send WRDI (Write Disable) */ |
| 251 | generic_spi_command(JEDEC_WRDI_OUTSIZE, JEDEC_WRDI_INSIZE, cmd, NULL); |
| 252 | } |
| 253 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 254 | int probe_spi(struct flashchip *flash) |
| 255 | { |
| 256 | unsigned char readarr[3]; |
| 257 | uint8_t manuf_id; |
| 258 | uint16_t model_id; |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 259 | if (!generic_spi_rdid(readarr)) { |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 260 | manuf_id = readarr[0]; |
| 261 | model_id = (readarr[1] << 8) | readarr[2]; |
| 262 | printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id, model_id); |
| 263 | if (manuf_id == flash->manufacture_id && model_id == flash->model_id) |
| 264 | return 1; |
| 265 | } |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 270 | uint8_t generic_spi_read_status_register() |
| 271 | { |
| 272 | const unsigned char cmd[] = JEDEC_RDSR; |
| 273 | unsigned char readarr[1]; |
| 274 | |
| 275 | /* Read Status Register */ |
| 276 | generic_spi_command(JEDEC_RDSR_OUTSIZE, JEDEC_RDSR_INSIZE, cmd, readarr); |
| 277 | return readarr[0]; |
| 278 | } |
| 279 | |
| 280 | int generic_spi_chip_erase(struct flashchip *flash) |
| 281 | { |
| 282 | const unsigned char cmd[] = JEDEC_CE_2; |
| 283 | |
| 284 | generic_spi_write_enable(); |
| 285 | /* Send CE (Chip Erase) */ |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 286 | generic_spi_command(JEDEC_CE_2_OUTSIZE, JEDEC_CE_2_INSIZE, cmd, NULL); |
| 287 | /* Wait until the Write-In-Progress bit is cleared. |
| 288 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 289 | */ |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 290 | while (generic_spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 291 | sleep(1); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 292 | return 0; |
| 293 | } |
| 294 | |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 295 | /* Block size is usually |
| 296 | * 64k for Macronix |
| 297 | * 32k for SST |
| 298 | * 4-32k non-uniform for EON |
| 299 | */ |
| 300 | int generic_spi_block_erase(const struct flashchip *flash, unsigned long addr) |
| 301 | { |
| 302 | unsigned char cmd[JEDEC_BE_2_OUTSIZE] = JEDEC_BE_2; |
| 303 | |
| 304 | cmd[1] = (addr & 0x00ff0000) >> 16; |
| 305 | cmd[2] = (addr & 0x0000ff00) >> 8; |
| 306 | cmd[3] = (addr & 0x000000ff); |
| 307 | generic_spi_write_enable(); |
| 308 | /* Send BE (Block Erase) */ |
| 309 | generic_spi_command(JEDEC_BE_2_OUTSIZE, JEDEC_BE_2_INSIZE, cmd, NULL); |
| 310 | /* Wait until the Write-In-Progress bit is cleared. |
| 311 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 312 | */ |
| 313 | while (generic_spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 314 | usleep(100 * 1000); |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | /* Sector size is usually 4k, though Macronix eliteflash has 64k */ |
| 319 | int generic_spi_sector_erase(const struct flashchip *flash, unsigned long addr) |
| 320 | { |
| 321 | unsigned char cmd[JEDEC_SE_OUTSIZE] = JEDEC_SE; |
| 322 | cmd[1] = (addr & 0x00ff0000) >> 16; |
| 323 | cmd[2] = (addr & 0x0000ff00) >> 8; |
| 324 | cmd[3] = (addr & 0x000000ff); |
| 325 | |
| 326 | generic_spi_write_enable(); |
| 327 | /* Send SE (Sector Erase) */ |
| 328 | generic_spi_command(JEDEC_SE_OUTSIZE, JEDEC_SE_INSIZE, cmd, NULL); |
| 329 | /* Wait until the Write-In-Progress bit is cleared. |
| 330 | * This usually takes 15-800 ms, so wait in 10 ms steps. |
| 331 | */ |
| 332 | while (generic_spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 333 | usleep(10 * 1000); |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | /* Page size is usually 256 bytes */ |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 338 | void it8716f_spi_page_program(int block, uint8_t *buf, uint8_t *bios) { |
| 339 | int i; |
| 340 | |
| 341 | generic_spi_write_enable(); |
| 342 | outb(0x06 , it8716f_flashport + 1); |
| 343 | outb((3 << 4), it8716f_flashport); |
| 344 | for (i = 0; i < 256; i++) { |
| 345 | bios[256 * block + i] = buf[256 * block + i]; |
| 346 | } |
| 347 | outb(0, it8716f_flashport); |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 348 | /* Wait until the Write-In-Progress bit is cleared. |
| 349 | * This usually takes 1-10 ms, so wait in 1 ms steps. |
| 350 | */ |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 351 | while (generic_spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 352 | usleep(1000); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void generic_spi_page_program(int block, uint8_t *buf, uint8_t *bios) |
| 356 | { |
| 357 | if (it8716f_flashport) |
| 358 | it8716f_spi_page_program(block, buf, bios); |
| 359 | } |
| 360 | |
| 361 | int generic_spi_chip_write(struct flashchip *flash, uint8_t *buf) { |
| 362 | int total_size = 1024 * flash->total_size; |
| 363 | int i; |
| 364 | for (i = 0; i < total_size / 256; i++) { |
| 365 | generic_spi_page_program(i, buf, (uint8_t *)flash->virtual_memory); |
| 366 | } |
| 367 | return 0; |
| 368 | } |
| 369 | |