hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
hailfinger | 4500b08 | 2009-07-11 18:05:42 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007, 2008, 2009 Carl-Daniel Hailfinger |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008 Ronald Hoogenboom <ronald@zonnet.nl> |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 6 | * Copyright (C) 2008 coresystems GmbH |
hailfinger | 2c361e4 | 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. |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | /* |
| 19 | * Contains the ITE IT87* SPI specific routines |
| 20 | */ |
| 21 | |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 22 | #if defined(__i386__) || defined(__x86_64__) |
| 23 | |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 24 | #include <string.h> |
hailfinger | 4500b08 | 2009-07-11 18:05:42 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
Patrick Georgi | 048dbdb | 2017-04-11 20:45:07 +0200 | [diff] [blame] | 26 | #include <errno.h> |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 27 | #include "flash.h" |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 28 | #include "chipdrivers.h" |
hailfinger | 428f685 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 29 | #include "programmer.h" |
Patrick Georgi | 048dbdb | 2017-04-11 20:45:07 +0200 | [diff] [blame] | 30 | #include "hwaccess.h" |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 31 | #include "spi.h" |
| 32 | |
| 33 | #define ITE_SUPERIO_PORT1 0x2e |
| 34 | #define ITE_SUPERIO_PORT2 0x4e |
| 35 | |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 36 | #define CHIP_ID_BYTE1_REG 0x20 |
| 37 | #define CHIP_ID_BYTE2_REG 0x21 |
| 38 | #define CHIP_VER_REG 0x22 |
| 39 | |
Edward O'Callaghan | c1d3adc | 2020-11-17 18:33:45 +1100 | [diff] [blame] | 40 | struct it8716f_spi_data { |
Edward O'Callaghan | 5cc200a | 2020-11-17 18:38:15 +1100 | [diff] [blame] | 41 | uint16_t flashport; |
Edward O'Callaghan | c1d3adc | 2020-11-17 18:33:45 +1100 | [diff] [blame] | 42 | /* use fast 33MHz SPI (<>0) or slow 16MHz (0) */ |
| 43 | int fast_spi; |
| 44 | }; |
| 45 | |
| 46 | static int get_data_from_context(const struct flashctx *flash, struct it8716f_spi_data **data) |
| 47 | { |
| 48 | if (!flash || !flash->mst || !flash->mst->spi.data) { |
| 49 | msg_perr("Unable to extract fd from flash context.\n"); |
| 50 | return SPI_GENERIC_ERROR; |
| 51 | } |
| 52 | *data = (struct it8716f_spi_data *)flash->mst->spi.data; |
| 53 | |
| 54 | return 0; |
| 55 | } |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 56 | |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 57 | /* Helper functions for most recent ITE IT87xx Super I/O chips */ |
hailfinger | 7bac0e5 | 2009-05-25 23:26:50 +0000 | [diff] [blame] | 58 | void enter_conf_mode_ite(uint16_t port) |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 59 | { |
hailfinger | e1f062f | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 60 | OUTB(0x87, port); |
| 61 | OUTB(0x01, port); |
| 62 | OUTB(0x55, port); |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 63 | if (port == ITE_SUPERIO_PORT1) |
hailfinger | e1f062f | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 64 | OUTB(0x55, port); |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 65 | else |
hailfinger | e1f062f | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 66 | OUTB(0xaa, port); |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 67 | } |
| 68 | |
hailfinger | 7bac0e5 | 2009-05-25 23:26:50 +0000 | [diff] [blame] | 69 | void exit_conf_mode_ite(uint16_t port) |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 70 | { |
hailfinger | 7bac0e5 | 2009-05-25 23:26:50 +0000 | [diff] [blame] | 71 | sio_write(port, 0x02, 0x02); |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Edward O'Callaghan | 9505295 | 2020-05-09 22:22:37 +1000 | [diff] [blame] | 74 | static uint16_t probe_id_ite(uint16_t port) |
hailfinger | c236f9e | 2009-12-22 23:42:04 +0000 | [diff] [blame] | 75 | { |
| 76 | uint16_t id; |
| 77 | |
| 78 | enter_conf_mode_ite(port); |
| 79 | id = sio_read(port, CHIP_ID_BYTE1_REG) << 8; |
| 80 | id |= sio_read(port, CHIP_ID_BYTE2_REG); |
| 81 | exit_conf_mode_ite(port); |
| 82 | |
| 83 | return id; |
| 84 | } |
| 85 | |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 86 | void probe_superio_ite(void) |
hailfinger | c236f9e | 2009-12-22 23:42:04 +0000 | [diff] [blame] | 87 | { |
Patrick Georgi | 8ddfee9 | 2017-03-20 14:54:28 +0100 | [diff] [blame] | 88 | struct superio s = {0}; |
hailfinger | c236f9e | 2009-12-22 23:42:04 +0000 | [diff] [blame] | 89 | uint16_t ite_ports[] = {ITE_SUPERIO_PORT1, ITE_SUPERIO_PORT2, 0}; |
| 90 | uint16_t *i = ite_ports; |
| 91 | |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 92 | s.vendor = SUPERIO_VENDOR_ITE; |
hailfinger | c236f9e | 2009-12-22 23:42:04 +0000 | [diff] [blame] | 93 | for (; *i; i++) { |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 94 | s.port = *i; |
| 95 | s.model = probe_id_ite(s.port); |
| 96 | switch (s.model >> 8) { |
hailfinger | c236f9e | 2009-12-22 23:42:04 +0000 | [diff] [blame] | 97 | case 0x82: |
| 98 | case 0x86: |
| 99 | case 0x87: |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 100 | /* FIXME: Print revision for all models? */ |
Patrick Georgi | 0548299 | 2017-03-20 21:56:33 +0100 | [diff] [blame] | 101 | msg_pdbg("Found ITE Super I/O, ID 0x%04hx on port 0x%x\n", s.model, s.port); |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 102 | register_superio(s); |
| 103 | break; |
| 104 | case 0x85: |
Patrick Georgi | 0548299 | 2017-03-20 21:56:33 +0100 | [diff] [blame] | 105 | msg_pdbg("Found ITE EC, ID 0x%04hx, Rev 0x%02x on port 0x%x.\n", |
Edward O'Callaghan | 9505295 | 2020-05-09 22:22:37 +1000 | [diff] [blame] | 106 | s.model, sio_read(s.port, CHIP_VER_REG), s.port); |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 107 | register_superio(s); |
| 108 | break; |
hailfinger | c236f9e | 2009-12-22 23:42:04 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 112 | return; |
hailfinger | c236f9e | 2009-12-22 23:42:04 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 115 | /* Page size is usually 256 bytes */ |
| 116 | static int it8716f_spi_page_program(struct flashctx *flash, const uint8_t *buf, unsigned int start) |
| 117 | { |
| 118 | unsigned int i; |
| 119 | int result; |
| 120 | chipaddr bios = flash->virtual_memory; |
Edward O'Callaghan | c1d3adc | 2020-11-17 18:33:45 +1100 | [diff] [blame] | 121 | struct it8716f_spi_data *data; |
| 122 | |
| 123 | if (get_data_from_context(flash, &data) < 0) |
| 124 | return SPI_GENERIC_ERROR; |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 125 | |
| 126 | result = spi_write_enable(flash); |
| 127 | if (result) |
| 128 | return result; |
| 129 | /* FIXME: The command below seems to be redundant or wrong. */ |
Edward O'Callaghan | 5cc200a | 2020-11-17 18:38:15 +1100 | [diff] [blame] | 130 | OUTB(0x06, data->flashport + 1); |
| 131 | OUTB(((2 + (data->fast_spi ? 1 : 0)) << 4), data->flashport); |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 132 | for (i = 0; i < flash->chip->page_size; i++) |
| 133 | mmio_writeb(buf[i], (void *)(bios + start + i)); |
Edward O'Callaghan | 5cc200a | 2020-11-17 18:38:15 +1100 | [diff] [blame] | 134 | OUTB(0, data->flashport); |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 135 | /* Wait until the Write-In-Progress bit is cleared. |
| 136 | * This usually takes 1-10 ms, so wait in 1 ms steps. |
| 137 | */ |
| 138 | while (spi_read_status_register(flash) & SPI_SR_WIP) |
| 139 | programmer_delay(1000); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * The IT8716F only supports commands with length 1,2,4,5 bytes including |
| 145 | * command byte and can not read more than 3 bytes from the device. |
| 146 | * |
| 147 | * This function expects writearr[0] to be the first byte sent to the device, |
| 148 | * whereas the IT8716F splits commands internally into address and non-address |
| 149 | * commands with the address in inverse wire order. That's why the register |
| 150 | * ordering in case 4 and 5 may seem strange. |
| 151 | */ |
Patrick Georgi | 0548299 | 2017-03-20 21:56:33 +0100 | [diff] [blame] | 152 | static int it8716f_spi_send_command(const struct flashctx *flash, |
| 153 | unsigned int writecnt, unsigned int readcnt, |
| 154 | const unsigned char *writearr, |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 155 | unsigned char *readarr) |
| 156 | { |
| 157 | uint8_t busy, writeenc; |
Edward O'Callaghan | c1d3adc | 2020-11-17 18:33:45 +1100 | [diff] [blame] | 158 | struct it8716f_spi_data *data; |
| 159 | |
| 160 | if (get_data_from_context(flash, &data) < 0) |
| 161 | return SPI_GENERIC_ERROR; |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 162 | |
| 163 | do { |
Edward O'Callaghan | 5cc200a | 2020-11-17 18:38:15 +1100 | [diff] [blame] | 164 | busy = INB(data->flashport) & 0x80; |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 165 | } while (busy); |
| 166 | if (readcnt > 3) { |
| 167 | msg_pinfo("%s called with unsupported readcnt %i.\n", |
| 168 | __func__, readcnt); |
| 169 | return SPI_INVALID_LENGTH; |
| 170 | } |
| 171 | switch (writecnt) { |
| 172 | case 1: |
Edward O'Callaghan | 5cc200a | 2020-11-17 18:38:15 +1100 | [diff] [blame] | 173 | OUTB(writearr[0], data->flashport + 1); |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 174 | writeenc = 0x0; |
| 175 | break; |
| 176 | case 2: |
Edward O'Callaghan | 5cc200a | 2020-11-17 18:38:15 +1100 | [diff] [blame] | 177 | OUTB(writearr[0], data->flashport + 1); |
| 178 | OUTB(writearr[1], data->flashport + 7); |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 179 | writeenc = 0x1; |
| 180 | break; |
| 181 | case 4: |
Edward O'Callaghan | 5cc200a | 2020-11-17 18:38:15 +1100 | [diff] [blame] | 182 | OUTB(writearr[0], data->flashport + 1); |
| 183 | OUTB(writearr[1], data->flashport + 4); |
| 184 | OUTB(writearr[2], data->flashport + 3); |
| 185 | OUTB(writearr[3], data->flashport + 2); |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 186 | writeenc = 0x2; |
| 187 | break; |
| 188 | case 5: |
Edward O'Callaghan | 5cc200a | 2020-11-17 18:38:15 +1100 | [diff] [blame] | 189 | OUTB(writearr[0], data->flashport + 1); |
| 190 | OUTB(writearr[1], data->flashport + 4); |
| 191 | OUTB(writearr[2], data->flashport + 3); |
| 192 | OUTB(writearr[3], data->flashport + 2); |
| 193 | OUTB(writearr[4], data->flashport + 7); |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 194 | writeenc = 0x3; |
| 195 | break; |
| 196 | default: |
| 197 | msg_pinfo("%s called with unsupported writecnt %i.\n", |
| 198 | __func__, writecnt); |
| 199 | return SPI_INVALID_LENGTH; |
| 200 | } |
| 201 | /* |
| 202 | * Start IO, 33 or 16 MHz, readcnt input bytes, writecnt output bytes. |
| 203 | * Note: |
| 204 | * We can't use writecnt directly, but have to use a strange encoding. |
| 205 | */ |
Edward O'Callaghan | c1d3adc | 2020-11-17 18:33:45 +1100 | [diff] [blame] | 206 | OUTB(((0x4 + (data->fast_spi ? 1 : 0)) << 4) |
Edward O'Callaghan | 5cc200a | 2020-11-17 18:38:15 +1100 | [diff] [blame] | 207 | | ((readcnt & 0x3) << 2) | (writeenc), data->flashport); |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 208 | |
| 209 | if (readcnt > 0) { |
| 210 | unsigned int i; |
| 211 | |
| 212 | do { |
Edward O'Callaghan | 5cc200a | 2020-11-17 18:38:15 +1100 | [diff] [blame] | 213 | busy = INB(data->flashport) & 0x80; |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 214 | } while (busy); |
| 215 | |
| 216 | for (i = 0; i < readcnt; i++) |
Edward O'Callaghan | 5cc200a | 2020-11-17 18:38:15 +1100 | [diff] [blame] | 217 | readarr[i] = INB(data->flashport + 5 + i); |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles |
| 225 | * Need to read this big flash using firmware cycles 3 byte at a time. |
| 226 | */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 227 | static int it8716f_spi_chip_read(struct flashctx *flash, uint8_t *buf, |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 228 | unsigned int start, unsigned int len) |
| 229 | { |
Edward O'Callaghan | c1d3adc | 2020-11-17 18:33:45 +1100 | [diff] [blame] | 230 | struct it8716f_spi_data *data; |
| 231 | |
| 232 | if (get_data_from_context(flash, &data) < 0) |
| 233 | return SPI_GENERIC_ERROR; |
| 234 | |
| 235 | data->fast_spi = 0; |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 236 | |
| 237 | /* FIXME: Check if someone explicitly requested to use IT87 SPI although |
| 238 | * the mainboard does not use IT87 SPI translation. This should be done |
| 239 | * via a programmer parameter for the internal programmer. |
| 240 | */ |
| 241 | if ((flash->chip->total_size * 1024 > 512 * 1024)) { |
| 242 | default_spi_read(flash, buf, start, len); |
| 243 | } else { |
| 244 | mmio_readn((void *)(flash->virtual_memory + start), buf, len); |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
Patrick Georgi | ab8353e | 2017-02-03 18:32:01 +0100 | [diff] [blame] | 250 | static int it8716f_spi_chip_write_256(struct flashctx *flash, const uint8_t *buf, |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 251 | unsigned int start, unsigned int len) |
| 252 | { |
| 253 | const struct flashchip *chip = flash->chip; |
| 254 | /* |
| 255 | * IT8716F only allows maximum of 512 kb SPI chip size for memory |
| 256 | * mapped access. It also can't write more than 1+3+256 bytes at once, |
| 257 | * so page_size > 256 bytes needs a fallback. |
| 258 | * FIXME: Split too big page writes into chunks IT87* can handle instead |
| 259 | * of degrading to single-byte program. |
| 260 | * FIXME: Check if someone explicitly requested to use IT87 SPI although |
| 261 | * the mainboard does not use IT87 SPI translation. This should be done |
| 262 | * via a programmer parameter for the internal programmer. |
| 263 | */ |
| 264 | if ((chip->total_size * 1024 > 512 * 1024) || (chip->page_size > 256)) { |
| 265 | spi_chip_write_1(flash, buf, start, len); |
| 266 | } else { |
| 267 | unsigned int lenhere; |
| 268 | |
| 269 | if (start % chip->page_size) { |
| 270 | /* start to the end of the page or to start + len, |
| 271 | * whichever is smaller. |
| 272 | */ |
| 273 | lenhere = min(len, chip->page_size - start % chip->page_size); |
| 274 | spi_chip_write_1(flash, buf, start, lenhere); |
| 275 | start += lenhere; |
| 276 | len -= lenhere; |
| 277 | buf += lenhere; |
| 278 | } |
| 279 | |
| 280 | while (len >= chip->page_size) { |
| 281 | it8716f_spi_page_program(flash, buf, start); |
| 282 | start += chip->page_size; |
| 283 | len -= chip->page_size; |
| 284 | buf += chip->page_size; |
| 285 | } |
| 286 | if (len) |
| 287 | spi_chip_write_1(flash, buf, start, len); |
| 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | } |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 292 | |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 293 | static const struct spi_master spi_master_it87xx = { |
Edward O'Callaghan | 3941bee | 2020-10-09 13:00:17 +1100 | [diff] [blame] | 294 | .max_data_read = 3, |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 295 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 296 | .command = it8716f_spi_send_command, |
| 297 | .multicommand = default_spi_send_multicommand, |
| 298 | .read = it8716f_spi_chip_read, |
| 299 | .write_256 = it8716f_spi_chip_write_256, |
Edward O'Callaghan | 04ac730 | 2020-05-14 18:03:40 +1000 | [diff] [blame] | 300 | .write_aai = spi_chip_write_1, |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 301 | }; |
| 302 | |
Edward O'Callaghan | c1d3adc | 2020-11-17 18:33:45 +1100 | [diff] [blame] | 303 | |
| 304 | static int it8716f_shutdown(void *data) |
| 305 | { |
| 306 | free(data); |
| 307 | return 0; |
| 308 | } |
| 309 | |
hailfinger | c73ce6e | 2010-07-10 16:56:32 +0000 | [diff] [blame] | 310 | static uint16_t it87spi_probe(uint16_t port) |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 311 | { |
| 312 | uint8_t tmp = 0; |
hailfinger | c236f9e | 2009-12-22 23:42:04 +0000 | [diff] [blame] | 313 | uint16_t flashport = 0; |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 314 | |
hailfinger | c73ce6e | 2010-07-10 16:56:32 +0000 | [diff] [blame] | 315 | enter_conf_mode_ite(port); |
Edward O'Callaghan | 9505295 | 2020-05-09 22:22:37 +1000 | [diff] [blame] | 316 | |
| 317 | char *param = extract_programmer_param("dualbiosindex"); |
| 318 | if (param != NULL) { |
| 319 | sio_write(port, 0x07, 0x07); /* Select GPIO LDN */ |
| 320 | tmp = sio_read(port, 0xEF); |
| 321 | if (*param == '\0') { /* Print current setting only. */ |
| 322 | free(param); |
| 323 | } else { |
| 324 | char *dualbiosindex_suffix; |
| 325 | errno = 0; |
| 326 | long chip_index = strtol(param, &dualbiosindex_suffix, 0); |
Edward O'Callaghan | 9505295 | 2020-05-09 22:22:37 +1000 | [diff] [blame] | 327 | if (errno != 0 || *dualbiosindex_suffix != '\0' || chip_index < 0 || chip_index > 1) { |
| 328 | msg_perr("DualBIOS: Invalid chip index requested - choose 0 or 1.\n"); |
Angel Pons | ace5074 | 2020-10-19 14:20:36 +0200 | [diff] [blame] | 329 | free(param); |
Edward O'Callaghan | 9505295 | 2020-05-09 22:22:37 +1000 | [diff] [blame] | 330 | exit_conf_mode_ite(port); |
| 331 | return 1; |
| 332 | } |
Angel Pons | ace5074 | 2020-10-19 14:20:36 +0200 | [diff] [blame] | 333 | free(param); |
Edward O'Callaghan | 9505295 | 2020-05-09 22:22:37 +1000 | [diff] [blame] | 334 | if (chip_index != (tmp & 1)) { |
| 335 | msg_pdbg("DualBIOS: Previous chip index: %d\n", tmp & 1); |
| 336 | sio_write(port, 0xEF, (tmp & 0xFE) | chip_index); |
| 337 | tmp = sio_read(port, 0xEF); |
| 338 | if ((tmp & 1) != chip_index) { |
| 339 | msg_perr("DualBIOS: Chip selection failed.\n"); |
| 340 | exit_conf_mode_ite(port); |
| 341 | return 1; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | msg_pinfo("DualBIOS: Selected chip: %d\n", tmp & 1); |
| 346 | } |
| 347 | |
hailfinger | c73ce6e | 2010-07-10 16:56:32 +0000 | [diff] [blame] | 348 | /* NOLDN, reg 0x24, mask out lowest bit (suspend) */ |
| 349 | tmp = sio_read(port, 0x24) & 0xFE; |
hailfinger | 969e2f3 | 2011-09-08 00:00:29 +0000 | [diff] [blame] | 350 | /* Check if LPC->SPI translation is active. */ |
| 351 | if (!(tmp & 0x0e)) { |
hailfinger | c73ce6e | 2010-07-10 16:56:32 +0000 | [diff] [blame] | 352 | msg_pdbg("No IT87* serial flash segment enabled.\n"); |
| 353 | exit_conf_mode_ite(port); |
| 354 | /* Nothing to do. */ |
David Hendricks | 5e79c9f | 2013-11-04 22:05:08 -0800 | [diff] [blame] | 355 | return 1; |
hailfinger | c73ce6e | 2010-07-10 16:56:32 +0000 | [diff] [blame] | 356 | } |
| 357 | msg_pdbg("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 358 | 0xFFFE0000, 0xFFFFFFFF, (tmp & 1 << 1) ? "en" : "dis"); |
| 359 | msg_pdbg("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 360 | 0x000E0000, 0x000FFFFF, (tmp & 1 << 1) ? "en" : "dis"); |
| 361 | msg_pdbg("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 362 | 0xFFEE0000, 0xFFEFFFFF, (tmp & 1 << 2) ? "en" : "dis"); |
| 363 | msg_pdbg("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 364 | 0xFFF80000, 0xFFFEFFFF, (tmp & 1 << 3) ? "en" : "dis"); |
| 365 | msg_pdbg("LPC write to serial flash %sabled\n", |
| 366 | (tmp & 1 << 4) ? "en" : "dis"); |
| 367 | /* The LPC->SPI force write enable below only makes sense for |
| 368 | * non-programmer mode. |
| 369 | */ |
| 370 | /* If any serial flash segment is enabled, enable writing. */ |
| 371 | if ((tmp & 0xe) && (!(tmp & 1 << 4))) { |
| 372 | msg_pdbg("Enabling LPC write to serial flash\n"); |
| 373 | tmp |= 1 << 4; |
| 374 | sio_write(port, 0x24, tmp); |
| 375 | } |
| 376 | msg_pdbg("Serial flash pin %i\n", (tmp & 1 << 5) ? 87 : 29); |
| 377 | /* LDN 0x7, reg 0x64/0x65 */ |
| 378 | sio_write(port, 0x07, 0x7); |
| 379 | flashport = sio_read(port, 0x64) << 8; |
| 380 | flashport |= sio_read(port, 0x65); |
| 381 | msg_pdbg("Serial flash port 0x%04x\n", flashport); |
| 382 | /* Non-default port requested? */ |
Patrick Georgi | 048dbdb | 2017-04-11 20:45:07 +0200 | [diff] [blame] | 383 | param = extract_programmer_param("it87spiport"); |
| 384 | if (param) { |
hailfinger | c73ce6e | 2010-07-10 16:56:32 +0000 | [diff] [blame] | 385 | char *endptr = NULL; |
| 386 | unsigned long forced_flashport; |
Patrick Georgi | 048dbdb | 2017-04-11 20:45:07 +0200 | [diff] [blame] | 387 | forced_flashport = strtoul(param, &endptr, 0); |
hailfinger | c73ce6e | 2010-07-10 16:56:32 +0000 | [diff] [blame] | 388 | /* Port 0, port >0x1000, unaligned ports and garbage strings |
| 389 | * are rejected. |
hailfinger | ddd5d7b | 2010-03-25 02:50:40 +0000 | [diff] [blame] | 390 | */ |
hailfinger | c73ce6e | 2010-07-10 16:56:32 +0000 | [diff] [blame] | 391 | if (!forced_flashport || (forced_flashport >= 0x1000) || |
| 392 | (forced_flashport & 0x7) || (*endptr != '\0')) { |
| 393 | /* Using ports below 0x100 is a really bad idea, and |
| 394 | * should only be done if no port between 0x100 and |
| 395 | * 0xff8 works due to routing issues. |
| 396 | */ |
| 397 | msg_perr("Error: it87spiport specified, but no valid " |
| 398 | "port specified.\nPort must be a multiple of " |
| 399 | "0x8 and lie between 0x100 and 0xff8.\n"); |
Patrick Georgi | 048dbdb | 2017-04-11 20:45:07 +0200 | [diff] [blame] | 400 | exit_conf_mode_ite(port); |
| 401 | free(param); |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 402 | return 1; |
hailfinger | c73ce6e | 2010-07-10 16:56:32 +0000 | [diff] [blame] | 403 | } else { |
| 404 | flashport = (uint16_t)forced_flashport; |
| 405 | msg_pinfo("Forcing serial flash port 0x%04x\n", |
| 406 | flashport); |
| 407 | sio_write(port, 0x64, (flashport >> 8)); |
| 408 | sio_write(port, 0x65, (flashport & 0xff)); |
hailfinger | 4500b08 | 2009-07-11 18:05:42 +0000 | [diff] [blame] | 409 | } |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 410 | } |
Patrick Georgi | 048dbdb | 2017-04-11 20:45:07 +0200 | [diff] [blame] | 411 | free(param); |
hailfinger | c73ce6e | 2010-07-10 16:56:32 +0000 | [diff] [blame] | 412 | exit_conf_mode_ite(port); |
Edward O'Callaghan | c1d3adc | 2020-11-17 18:33:45 +1100 | [diff] [blame] | 413 | |
| 414 | struct it8716f_spi_data *data = calloc(1, sizeof(struct it8716f_spi_data)); |
| 415 | if (!data) { |
| 416 | msg_perr("Unable to allocate space for extra SPI master data.\n"); |
| 417 | return SPI_GENERIC_ERROR; |
| 418 | } |
| 419 | |
Edward O'Callaghan | 5cc200a | 2020-11-17 18:38:15 +1100 | [diff] [blame] | 420 | data->flashport = flashport; |
Edward O'Callaghan | c1d3adc | 2020-11-17 18:33:45 +1100 | [diff] [blame] | 421 | data->fast_spi = 1; |
Edward O'Callaghan | c1d3adc | 2020-11-17 18:33:45 +1100 | [diff] [blame] | 422 | |
| 423 | register_shutdown(it8716f_shutdown, data); |
| 424 | |
hailfinger | 76bb7e9 | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 425 | if (internal_buses_supported & BUS_SPI) |
hailfinger | c73ce6e | 2010-07-10 16:56:32 +0000 | [diff] [blame] | 426 | msg_pdbg("Overriding chipset SPI with IT87 SPI.\n"); |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 427 | /* FIXME: Add the SPI bus or replace the other buses with it? */ |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 428 | register_spi_master(&spi_master_it87xx, data); |
hailfinger | c73ce6e | 2010-07-10 16:56:32 +0000 | [diff] [blame] | 429 | return 0; |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 430 | } |
| 431 | |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 432 | int init_superio_ite(void) |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 433 | { |
Patrick Georgi | 048dbdb | 2017-04-11 20:45:07 +0200 | [diff] [blame] | 434 | int i; |
| 435 | int ret = 0; |
| 436 | int chips_found = 0; |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 437 | |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 438 | for (i = 0; i < superio_count; i++) { |
| 439 | if (superios[i].vendor != SUPERIO_VENDOR_ITE) |
| 440 | continue; |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 441 | |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 442 | switch (superios[i].model) { |
| 443 | case 0x8500: |
| 444 | case 0x8502: |
| 445 | case 0x8510: |
| 446 | case 0x8511: |
| 447 | case 0x8512: |
| 448 | /* FIXME: This should be enabled, but we need a check |
| 449 | * for laptop whitelisting due to the amount of things |
| 450 | * which can go wrong if the EC firmware does not |
| 451 | * implement the interface we want. |
| 452 | */ |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 453 | if (!it85xx_spi_init(superios[i])) |
David Hendricks | 5e79c9f | 2013-11-04 22:05:08 -0800 | [diff] [blame] | 454 | chips_found++; |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 455 | break; |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 456 | case 0x8518: |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 457 | if (!it8518_spi_init(superios[i])) |
David Hendricks | 5e79c9f | 2013-11-04 22:05:08 -0800 | [diff] [blame] | 458 | chips_found++; |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 459 | break; |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 460 | case 0x8705: |
David Hendricks | 5e79c9f | 2013-11-04 22:05:08 -0800 | [diff] [blame] | 461 | if (!it8705f_write_enable(superios[i].port)) |
| 462 | chips_found++; |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 463 | break; |
| 464 | case 0x8716: |
| 465 | case 0x8718: |
| 466 | case 0x8720: |
Edward O'Callaghan | 9505295 | 2020-05-09 22:22:37 +1000 | [diff] [blame] | 467 | case 0x8728: |
David Hendricks | 5e79c9f | 2013-11-04 22:05:08 -0800 | [diff] [blame] | 468 | if (!it87spi_probe(superios[i].port)) |
| 469 | chips_found++; |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 470 | break; |
| 471 | default: |
Patrick Georgi | 048dbdb | 2017-04-11 20:45:07 +0200 | [diff] [blame] | 472 | msg_pdbg2("Super I/O ID 0x%04hx is not on the list of flash-capable controllers.\n", |
| 473 | superios[i].model); |
hailfinger | 94e090c | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 474 | } |
hailfinger | d9f5da2 | 2009-06-28 10:57:58 +0000 | [diff] [blame] | 475 | } |
David Hendricks | 5e79c9f | 2013-11-04 22:05:08 -0800 | [diff] [blame] | 476 | |
| 477 | if (chips_found == 0) { |
| 478 | ret = 1; /* failed to probe/initialize/enable chip */ |
| 479 | } else if (chips_found == 1) { |
| 480 | ret = 0; /* success */ |
| 481 | } else { |
| 482 | msg_pdbg("%s: Found %d programmable ECs/SuperIOs, aborting.\n", |
| 483 | __func__, chips_found); |
| 484 | ret = 1; |
| 485 | } |
hailfinger | a916b42 | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 486 | return ret; |
hailfinger | 26e212b | 2009-05-31 18:00:57 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Edward O'Callaghan | 85726e5 | 2020-11-17 18:11:47 +1100 | [diff] [blame] | 489 | #endif /* defined(__i386__) || defined(__x86_64__) */ |