Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2007, 2008 Carl-Daniel Hailfinger |
| 5 | * Copyright (C) 2008 Ronald Hoogenboom <ronald@zonnet.nl> |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 6 | * Copyright (C) 2008 coresystems GmbH |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * Contains the ITE IT87* SPI specific routines |
| 24 | */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <pci/pci.h> |
| 28 | #include <stdint.h> |
| 29 | #include <string.h> |
| 30 | #include "flash.h" |
| 31 | #include "spi.h" |
| 32 | |
| 33 | #define ITE_SUPERIO_PORT1 0x2e |
| 34 | #define ITE_SUPERIO_PORT2 0x4e |
| 35 | |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 36 | uint16_t it8716f_flashport = 0; |
| 37 | /* use fast 33MHz SPI (<>0) or slow 16MHz (0) */ |
| 38 | int fast_spi = 1; |
| 39 | |
| 40 | /* Generic Super I/O helper functions */ |
| 41 | uint8_t regval(uint16_t port, uint8_t reg) |
| 42 | { |
Andriy Gapon | 65c1b86 | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 43 | OUTB(reg, port); |
| 44 | return INB(port + 1); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void regwrite(uint16_t port, uint8_t reg, uint8_t val) |
| 48 | { |
Andriy Gapon | 65c1b86 | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 49 | OUTB(reg, port); |
| 50 | OUTB(val, port + 1); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* Helper functions for most recent ITE IT87xx Super I/O chips */ |
| 54 | #define CHIP_ID_BYTE1_REG 0x20 |
| 55 | #define CHIP_ID_BYTE2_REG 0x21 |
| 56 | static void enter_conf_mode_ite(uint16_t port) |
| 57 | { |
Andriy Gapon | 65c1b86 | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 58 | OUTB(0x87, port); |
| 59 | OUTB(0x01, port); |
| 60 | OUTB(0x55, port); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 61 | if (port == ITE_SUPERIO_PORT1) |
Andriy Gapon | 65c1b86 | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 62 | OUTB(0x55, port); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 63 | else |
Andriy Gapon | 65c1b86 | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 64 | OUTB(0xaa, port); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static void exit_conf_mode_ite(uint16_t port) |
| 68 | { |
| 69 | regwrite(port, 0x02, 0x02); |
| 70 | } |
| 71 | |
| 72 | static uint16_t find_ite_spi_flash_port(uint16_t port) |
| 73 | { |
| 74 | uint8_t tmp = 0; |
| 75 | uint16_t id, flashport = 0; |
| 76 | |
| 77 | enter_conf_mode_ite(port); |
| 78 | |
| 79 | id = regval(port, CHIP_ID_BYTE1_REG) << 8; |
| 80 | id |= regval(port, CHIP_ID_BYTE2_REG); |
| 81 | |
| 82 | /* TODO: Handle more IT87xx if they support flash translation */ |
| 83 | if (id == 0x8716) { |
| 84 | /* NOLDN, reg 0x24, mask out lowest bit (suspend) */ |
| 85 | tmp = regval(port, 0x24) & 0xFE; |
| 86 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 87 | 0xFFFE0000, 0xFFFFFFFF, (tmp & 1 << 1) ? "en" : "dis"); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 88 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 89 | 0x000E0000, 0x000FFFFF, (tmp & 1 << 1) ? "en" : "dis"); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 90 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 91 | 0xFFEE0000, 0xFFEFFFFF, (tmp & 1 << 2) ? "en" : "dis"); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 92 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 93 | 0xFFF80000, 0xFFFEFFFF, (tmp & 1 << 3) ? "en" : "dis"); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 94 | printf("LPC write to serial flash %sabled\n", |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 95 | (tmp & 1 << 4) ? "en" : "dis"); |
Carl-Daniel Hailfinger | 337df1d | 2008-05-16 00:19:52 +0000 | [diff] [blame] | 96 | /* If any serial flash segment is enabled, enable writing. */ |
| 97 | if ((tmp & 0xe) && (!(tmp & 1 << 4))) { |
| 98 | printf("Enabling LPC write to serial flash\n"); |
| 99 | tmp |= 1 << 4; |
| 100 | regwrite(port, 0x24, tmp); |
| 101 | } |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 102 | printf("serial flash pin %i\n", (tmp & 1 << 5) ? 87 : 29); |
| 103 | /* LDN 0x7, reg 0x64/0x65 */ |
| 104 | regwrite(port, 0x07, 0x7); |
| 105 | flashport = regval(port, 0x64) << 8; |
| 106 | flashport |= regval(port, 0x65); |
| 107 | } |
| 108 | exit_conf_mode_ite(port); |
| 109 | return flashport; |
| 110 | } |
| 111 | |
| 112 | int it87xx_probe_spi_flash(const char *name) |
| 113 | { |
| 114 | it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT1); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 115 | |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 116 | if (!it8716f_flashport) |
| 117 | it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT2); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 118 | |
| 119 | if (it8716f_flashport) |
| 120 | flashbus = BUS_TYPE_IT87XX_SPI; |
| 121 | |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 122 | return (!it8716f_flashport); |
| 123 | } |
| 124 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 125 | /* |
| 126 | * The IT8716F only supports commands with length 1,2,4,5 bytes including |
| 127 | * command byte and can not read more than 3 bytes from the device. |
| 128 | * |
| 129 | * This function expects writearr[0] to be the first byte sent to the device, |
| 130 | * whereas the IT8716F splits commands internally into address and non-address |
| 131 | * commands with the address in inverse wire order. That's why the register |
| 132 | * ordering in case 4 and 5 may seem strange. |
| 133 | */ |
| 134 | int it8716f_spi_command(unsigned int writecnt, unsigned int readcnt, |
| 135 | const unsigned char *writearr, unsigned char *readarr) |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 136 | { |
| 137 | uint8_t busy, writeenc; |
| 138 | int i; |
| 139 | |
| 140 | do { |
Andriy Gapon | 65c1b86 | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 141 | busy = INB(it8716f_flashport) & 0x80; |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 142 | } while (busy); |
| 143 | if (readcnt > 3) { |
| 144 | printf("%s called with unsupported readcnt %i.\n", |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 145 | __FUNCTION__, readcnt); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 146 | return 1; |
| 147 | } |
| 148 | switch (writecnt) { |
| 149 | case 1: |
Andriy Gapon | 65c1b86 | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 150 | OUTB(writearr[0], it8716f_flashport + 1); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 151 | writeenc = 0x0; |
| 152 | break; |
| 153 | case 2: |
Andriy Gapon | 65c1b86 | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 154 | OUTB(writearr[0], it8716f_flashport + 1); |
| 155 | OUTB(writearr[1], it8716f_flashport + 7); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 156 | writeenc = 0x1; |
| 157 | break; |
| 158 | case 4: |
Andriy Gapon | 65c1b86 | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 159 | OUTB(writearr[0], it8716f_flashport + 1); |
| 160 | OUTB(writearr[1], it8716f_flashport + 4); |
| 161 | OUTB(writearr[2], it8716f_flashport + 3); |
| 162 | OUTB(writearr[3], it8716f_flashport + 2); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 163 | writeenc = 0x2; |
| 164 | break; |
| 165 | case 5: |
Andriy Gapon | 65c1b86 | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 166 | OUTB(writearr[0], it8716f_flashport + 1); |
| 167 | OUTB(writearr[1], it8716f_flashport + 4); |
| 168 | OUTB(writearr[2], it8716f_flashport + 3); |
| 169 | OUTB(writearr[3], it8716f_flashport + 2); |
| 170 | OUTB(writearr[4], it8716f_flashport + 7); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 171 | writeenc = 0x3; |
| 172 | break; |
| 173 | default: |
| 174 | printf("%s called with unsupported writecnt %i.\n", |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 175 | __FUNCTION__, writecnt); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 176 | return 1; |
| 177 | } |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 178 | /* |
| 179 | * Start IO, 33 or 16 MHz, readcnt input bytes, writecnt output bytes. |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 180 | * Note: |
| 181 | * We can't use writecnt directly, but have to use a strange encoding. |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 182 | */ |
| 183 | OUTB(((0x4 + (fast_spi ? 1 : 0)) << 4) |
| 184 | | ((readcnt & 0x3) << 2) | (writeenc), it8716f_flashport); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 185 | |
| 186 | if (readcnt > 0) { |
| 187 | do { |
Andriy Gapon | 65c1b86 | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 188 | busy = INB(it8716f_flashport) & 0x80; |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 189 | } while (busy); |
| 190 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 191 | for (i = 0; i < readcnt; i++) |
Andriy Gapon | 65c1b86 | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 192 | readarr[i] = INB(it8716f_flashport + 5 + i); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | /* Page size is usually 256 bytes */ |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 199 | static void it8716f_spi_page_program(int block, uint8_t *buf, uint8_t *bios) |
| 200 | { |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 201 | int i; |
| 202 | |
| 203 | spi_write_enable(); |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 204 | OUTB(0x06, it8716f_flashport + 1); |
Andriy Gapon | 65c1b86 | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 205 | OUTB(((2 + (fast_spi ? 1 : 0)) << 4), it8716f_flashport); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 206 | for (i = 0; i < 256; i++) { |
| 207 | bios[256 * block + i] = buf[256 * block + i]; |
| 208 | } |
Andriy Gapon | 65c1b86 | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 209 | OUTB(0, it8716f_flashport); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 210 | /* Wait until the Write-In-Progress bit is cleared. |
| 211 | * This usually takes 1-10 ms, so wait in 1 ms steps. |
| 212 | */ |
| 213 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 214 | usleep(1000); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles |
| 219 | * Program chip using firmware cycle byte programming. (SLOW!) |
| 220 | */ |
| 221 | int it8716f_over512k_spi_chip_write(struct flashchip *flash, uint8_t *buf) |
| 222 | { |
| 223 | int total_size = 1024 * flash->total_size; |
| 224 | int i; |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 225 | |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 226 | fast_spi = 0; |
| 227 | |
| 228 | spi_disable_blockprotect(); |
| 229 | for (i = 0; i < total_size; i++) { |
| 230 | spi_write_enable(); |
| 231 | spi_byte_program(i, buf[i]); |
| 232 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 233 | myusec_delay(10); |
| 234 | } |
| 235 | /* resume normal ops... */ |
Andriy Gapon | 65c1b86 | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 236 | OUTB(0x20, it8716f_flashport); |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 237 | |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles |
| 243 | * Need to read this big flash using firmware cycles 3 byte at a time. |
| 244 | */ |
| 245 | int it8716f_spi_chip_read(struct flashchip *flash, uint8_t *buf) |
| 246 | { |
| 247 | int total_size = 1024 * flash->total_size; |
| 248 | int i; |
| 249 | fast_spi = 0; |
| 250 | |
| 251 | if (total_size > 512 * 1024) { |
| 252 | for (i = 0; i < total_size; i += 3) { |
| 253 | int toread = 3; |
| 254 | if (total_size - i < toread) |
| 255 | toread = total_size - i; |
| 256 | spi_nbyte_read(i, buf + i, toread); |
| 257 | } |
| 258 | } else { |
| 259 | memcpy(buf, (const char *)flash->virtual_memory, total_size); |
| 260 | } |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 261 | |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 265 | int it8716f_spi_chip_write(struct flashchip *flash, uint8_t *buf) |
| 266 | { |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 267 | int total_size = 1024 * flash->total_size; |
| 268 | int i; |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 269 | |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 270 | if (total_size > 512 * 1024) { |
| 271 | it8716f_over512k_spi_chip_write(flash, buf); |
| 272 | } else { |
| 273 | for (i = 0; i < total_size / 256; i++) { |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 274 | it8716f_spi_page_program(i, buf, |
| 275 | (uint8_t *)flash->virtual_memory); |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 276 | } |
| 277 | } |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 278 | |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 279 | return 0; |
| 280 | } |