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 | |
| 48 | /* Both Chip Erase commands below should work */ |
| 49 | /* Chip Erase 0x60 */ |
| 50 | #define JEDEC_CE_1 {0x60}; |
| 51 | #define JEDEC_CE_1_OUTSIZE 0x01 |
| 52 | #define JEDEC_CE_1_INSIZE 0x00 |
| 53 | |
| 54 | /* Chip Erase 0xc7 */ |
| 55 | #define JEDEC_CE_2 {0xc7}; |
| 56 | #define JEDEC_CE_2_OUTSIZE 0x01 |
| 57 | #define JEDEC_CE_2_INSIZE 0x00 |
| 58 | |
| 59 | /* Read Status Register */ |
| 60 | #define JEDEC_RDSR {0x05}; |
| 61 | #define JEDEC_RDSR_OUTSIZE 0x01 |
| 62 | #define JEDEC_RDSR_INSIZE 0x01 |
| 63 | #define JEDEC_RDSR_BIT_WIP (0x01 << 0) |
| 64 | |
| 65 | uint16_t it8716f_flashport = 0; |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 66 | |
| 67 | /* Generic Super I/O helper functions */ |
| 68 | uint8_t regval(uint16_t port, uint8_t reg) |
| 69 | { |
| 70 | outb(reg, port); |
| 71 | return inb(port + 1); |
| 72 | } |
| 73 | |
| 74 | void regwrite(uint16_t port, uint8_t reg, uint8_t val) |
| 75 | { |
| 76 | outb(reg, port); |
| 77 | outb(val, port + 1); |
| 78 | } |
| 79 | |
| 80 | /* Helper functions for most recent ITE IT87xx Super I/O chips */ |
| 81 | #define CHIP_ID_BYTE1_REG 0x20 |
| 82 | #define CHIP_ID_BYTE2_REG 0x21 |
| 83 | static void enter_conf_mode_ite(uint16_t port) |
| 84 | { |
| 85 | outb(0x87, port); |
| 86 | outb(0x01, port); |
| 87 | outb(0x55, port); |
| 88 | if (port == ITE_SUPERIO_PORT1) |
| 89 | outb(0x55, port); |
| 90 | else |
| 91 | outb(0xaa, port); |
| 92 | } |
| 93 | |
| 94 | static void exit_conf_mode_ite(uint16_t port) |
| 95 | { |
| 96 | regwrite(port, 0x02, 0x02); |
| 97 | } |
| 98 | |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 99 | static uint16_t find_ite_spi_flash_port(uint16_t port) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 100 | { |
| 101 | uint8_t tmp = 0; |
| 102 | uint16_t id, flashport = 0; |
| 103 | |
| 104 | enter_conf_mode_ite(port); |
| 105 | |
| 106 | id = regval(port, CHIP_ID_BYTE1_REG) << 8; |
| 107 | id |= regval(port, CHIP_ID_BYTE2_REG); |
| 108 | |
| 109 | /* TODO: Handle more IT87xx if they support flash translation */ |
| 110 | if (id == 0x8716) { |
| 111 | /* NOLDN, reg 0x24, mask out lowest bit (suspend) */ |
| 112 | tmp = regval(port, 0x24) & 0xFE; |
| 113 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 114 | 0xFFFE0000, 0xFFFFFFFF, (tmp & 1 << 1) ? "en" : "dis"); |
| 115 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 116 | 0x000E0000, 0x000FFFFF, (tmp & 1 << 1) ? "en" : "dis"); |
| 117 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 118 | 0xFFEE0000, 0xFFEFFFFF, (tmp & 1 << 2) ? "en" : "dis"); |
| 119 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 120 | 0xFFF80000, 0xFFFEFFFF, (tmp & 1 << 3) ? "en" : "dis"); |
| 121 | printf("LPC write to serial flash %sabled\n", |
| 122 | (tmp & 1 << 4) ? "en" : "dis"); |
| 123 | printf("serial flash pin %i\n", (tmp & 1 << 5) ? 87 : 29); |
| 124 | /* LDN 0x7, reg 0x64/0x65 */ |
| 125 | regwrite(port, 0x07, 0x7); |
| 126 | flashport = regval(port, 0x64) << 8; |
| 127 | flashport |= regval(port, 0x65); |
| 128 | } |
| 129 | exit_conf_mode_ite(port); |
| 130 | return flashport; |
| 131 | } |
| 132 | |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 133 | int it87xx_probe_spi_flash(const char *name) |
| 134 | { |
| 135 | it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT1); |
| 136 | if (!it8716f_flashport) |
| 137 | it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT2); |
| 138 | return (!it8716f_flashport); |
| 139 | } |
| 140 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 141 | /* The IT8716F only supports commands with length 1,2,4,5 bytes including |
| 142 | command byte and can not read more than 3 bytes from the device. |
| 143 | This function expects writearr[0] to be the first byte sent to the device, |
| 144 | whereas the IT8716F splits commands internally into address and non-address |
| 145 | commands with the address in inverse wire order. That's why the register |
| 146 | ordering in case 4 and 5 may seem strange. */ |
| 147 | static int it8716f_spi_command(uint16_t port, unsigned char writecnt, unsigned char readcnt, const unsigned char *writearr, unsigned char *readarr) |
| 148 | { |
| 149 | uint8_t busy, writeenc; |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 150 | int i; |
| 151 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 152 | do { |
| 153 | busy = inb(port) & 0x80; |
| 154 | } while (busy); |
| 155 | if (readcnt > 3) { |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 156 | printf("%s called with unsupported readcnt %i.\n", |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 157 | __FUNCTION__, readcnt); |
| 158 | return 1; |
| 159 | } |
| 160 | switch (writecnt) { |
| 161 | case 1: |
| 162 | outb(writearr[0], port + 1); |
| 163 | writeenc = 0x0; |
| 164 | break; |
| 165 | case 2: |
| 166 | outb(writearr[0], port + 1); |
| 167 | outb(writearr[1], port + 7); |
| 168 | writeenc = 0x1; |
| 169 | break; |
| 170 | case 4: |
| 171 | outb(writearr[0], port + 1); |
| 172 | outb(writearr[1], port + 4); |
| 173 | outb(writearr[2], port + 3); |
| 174 | outb(writearr[3], port + 2); |
| 175 | writeenc = 0x2; |
| 176 | break; |
| 177 | case 5: |
| 178 | outb(writearr[0], port + 1); |
| 179 | outb(writearr[1], port + 4); |
| 180 | outb(writearr[2], port + 3); |
| 181 | outb(writearr[3], port + 2); |
| 182 | outb(writearr[4], port + 7); |
| 183 | writeenc = 0x3; |
| 184 | break; |
| 185 | default: |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 186 | printf("%s called with unsupported writecnt %i.\n", |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 187 | __FUNCTION__, writecnt); |
| 188 | return 1; |
| 189 | } |
| 190 | /* Start IO, 33MHz, readcnt input bytes, writecnt output bytes. Note: |
| 191 | * We can't use writecnt directly, but have to use a strange encoding |
| 192 | */ |
| 193 | outb((0x5 << 4) | ((readcnt & 0x3) << 2) | (writeenc), port); |
| 194 | do { |
| 195 | busy = inb(port) & 0x80; |
| 196 | } while (busy); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 197 | |
| 198 | for (i = 0; i < readcnt; i++) { |
| 199 | readarr[i] = inb(port + 5 + i); |
| 200 | } |
| 201 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 205 | int generic_spi_command(unsigned char writecnt, unsigned char readcnt, const unsigned char *writearr, unsigned char *readarr) |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 206 | { |
| 207 | if (it8716f_flashport) |
| 208 | return it8716f_spi_command(it8716f_flashport, writecnt, readcnt, writearr, readarr); |
| 209 | printf("%s called, but no SPI chipset detected\n", __FUNCTION__); |
| 210 | return 1; |
| 211 | } |
| 212 | |
| 213 | static int generic_spi_rdid(unsigned char *readarr) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 214 | { |
| 215 | const unsigned char cmd[] = JEDEC_RDID; |
| 216 | |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 217 | 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] | 218 | return 1; |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 219 | 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] | 220 | return 0; |
| 221 | } |
| 222 | |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 223 | void generic_spi_write_enable() |
| 224 | { |
| 225 | const unsigned char cmd[] = JEDEC_WREN; |
| 226 | |
| 227 | /* Send WREN (Write Enable) */ |
| 228 | generic_spi_command(JEDEC_WREN_OUTSIZE, JEDEC_WREN_INSIZE, cmd, NULL); |
| 229 | |
| 230 | } |
| 231 | |
| 232 | void generic_spi_write_disable() |
| 233 | { |
| 234 | const unsigned char cmd[] = JEDEC_WRDI; |
| 235 | |
| 236 | /* Send WRDI (Write Disable) */ |
| 237 | generic_spi_command(JEDEC_WRDI_OUTSIZE, JEDEC_WRDI_INSIZE, cmd, NULL); |
| 238 | } |
| 239 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 240 | int probe_spi(struct flashchip *flash) |
| 241 | { |
| 242 | unsigned char readarr[3]; |
| 243 | uint8_t manuf_id; |
| 244 | uint16_t model_id; |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 245 | if (!generic_spi_rdid(readarr)) { |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 246 | manuf_id = readarr[0]; |
| 247 | model_id = (readarr[1] << 8) | readarr[2]; |
| 248 | printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id, model_id); |
| 249 | if (manuf_id == flash->manufacture_id && model_id == flash->model_id) |
| 250 | return 1; |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 256 | uint8_t generic_spi_read_status_register() |
| 257 | { |
| 258 | const unsigned char cmd[] = JEDEC_RDSR; |
| 259 | unsigned char readarr[1]; |
| 260 | |
| 261 | /* Read Status Register */ |
| 262 | generic_spi_command(JEDEC_RDSR_OUTSIZE, JEDEC_RDSR_INSIZE, cmd, readarr); |
| 263 | return readarr[0]; |
| 264 | } |
| 265 | |
| 266 | int generic_spi_chip_erase(struct flashchip *flash) |
| 267 | { |
| 268 | const unsigned char cmd[] = JEDEC_CE_2; |
| 269 | |
| 270 | generic_spi_write_enable(); |
| 271 | /* Send CE (Chip Erase) */ |
| 272 | generic_spi_command(1, 0, cmd, NULL); |
Carl-Daniel Hailfinger | 145acec | 2007-10-18 17:56:42 +0000 | [diff] [blame^] | 273 | /* Wait until the Write-In-Progress bit is cleared */ |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 274 | while (generic_spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 275 | sleep(1); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | void it8716f_spi_page_program(int block, uint8_t *buf, uint8_t *bios) { |
| 280 | int i; |
| 281 | |
| 282 | generic_spi_write_enable(); |
| 283 | outb(0x06 , it8716f_flashport + 1); |
| 284 | outb((3 << 4), it8716f_flashport); |
| 285 | for (i = 0; i < 256; i++) { |
| 286 | bios[256 * block + i] = buf[256 * block + i]; |
| 287 | } |
| 288 | outb(0, it8716f_flashport); |
Carl-Daniel Hailfinger | 145acec | 2007-10-18 17:56:42 +0000 | [diff] [blame^] | 289 | /* Wait until the Write-In-Progress bit is cleared */ |
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 | usleep(1000); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void generic_spi_page_program(int block, uint8_t *buf, uint8_t *bios) |
| 295 | { |
| 296 | if (it8716f_flashport) |
| 297 | it8716f_spi_page_program(block, buf, bios); |
| 298 | } |
| 299 | |
| 300 | int generic_spi_chip_write(struct flashchip *flash, uint8_t *buf) { |
| 301 | int total_size = 1024 * flash->total_size; |
| 302 | int i; |
| 303 | for (i = 0; i < total_size / 256; i++) { |
| 304 | generic_spi_page_program(i, buf, (uint8_t *)flash->virtual_memory); |
| 305 | } |
| 306 | return 0; |
| 307 | } |
| 308 | |