hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2007, 2008, 2009 Carl-Daniel Hailfinger |
| 5 | * Copyright (C) 2008 Ronald Hoogenboom <ronald@zonnet.nl> |
| 6 | * Copyright (C) 2008 coresystems GmbH |
| 7 | * Copyright (C) 2010 Google Inc. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; version 2 of the License. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * Contains the ITE IT85* SPI specific routines |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 21 | * |
| 22 | * FIXME: EC firmware updates on this chip can be interrupted due to factors |
| 23 | * such as SMBus traffic. YOU MUST DISABLE any services, such as power |
| 24 | * management daemons, which can interact with the EC during firmware update. |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | #if defined(__i386__) || defined(__x86_64__) |
| 28 | |
| 29 | #include <string.h> |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | #include "flash.h" |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 33 | #include "spi.h" |
| 34 | #include "programmer.h" |
Mayur Panchal | f479686 | 2019-08-05 15:46:12 +1000 | [diff] [blame] | 35 | #include "hwaccess.h" |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 36 | |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 37 | /* Supported ECs, ITE_LAST should always be LAST member */ |
| 38 | enum ite_chip_id { |
| 39 | ITE_IT85XX, |
| 40 | ITE_IT8518, |
| 41 | ITE_LAST |
| 42 | }; |
| 43 | |
| 44 | /* chip-specific parameters */ |
| 45 | typedef struct { |
| 46 | enum ite_chip_id chip_id; |
| 47 | uint8_t port_data; |
| 48 | uint8_t port_cmd; |
| 49 | uint8_t copy_to_sram_cmd; |
| 50 | uint8_t exit_sram_cmd; |
| 51 | uint32_t exit_sram_delay; |
| 52 | } ite_chip; |
| 53 | |
| 54 | /* table of supported chips + parameters, order by ite_chip_id index */ |
| 55 | static ite_chip ite_chips[] = { |
| 56 | { ITE_IT85XX, |
| 57 | 0x60, |
| 58 | 0x64, |
| 59 | 0xB4, |
| 60 | 0xFE, |
| 61 | 0, |
| 62 | }, |
| 63 | |
| 64 | { ITE_IT8518, |
| 65 | 0x62, |
| 66 | 0x66, |
David Hendricks | 7491585 | 2012-09-27 16:39:37 -0700 | [diff] [blame] | 67 | 0xDD, /* default value, see note in it85xx_spi_send_command */ |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 68 | 0xFF, |
| 69 | 500000, |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | /* pointer to table entry of identified chip */ |
| 74 | static ite_chip *found_chip; |
| 75 | |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 76 | #define MAX_TIMEOUT 100000 |
| 77 | #define MAX_TRY 5 |
| 78 | |
hailfinger | 177cdd8 | 2011-03-08 00:23:49 +0000 | [diff] [blame] | 79 | /* Constants for I/O ports */ |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 80 | #define ITE_SUPERIO_PORT1 0x2e |
| 81 | #define ITE_SUPERIO_PORT2 0x4e |
| 82 | |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 83 | /* Constants for Logical Device registers */ |
| 84 | #define LDNSEL 0x07 |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 85 | |
| 86 | /* These are standard Super I/O 16-bit base address registers */ |
hailfinger | 177cdd8 | 2011-03-08 00:23:49 +0000 | [diff] [blame] | 87 | #define SHM_IO_BAR0 0x60 /* big-endian, this is high bits */ |
| 88 | #define SHM_IO_BAR1 0x61 |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 89 | |
hailfinger | 177cdd8 | 2011-03-08 00:23:49 +0000 | [diff] [blame] | 90 | /* The 8042 keyboard controller uses an input buffer and an output buffer to |
| 91 | * communicate with the host CPU. Both buffers are 1-byte depth. That means |
| 92 | * IBF is set to 1 when the host CPU sends a command to the input buffer |
| 93 | * of the EC. IBF is cleared to 0 once the command is read by the EC. |
| 94 | */ |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 95 | #define KB_IBF (1 << 1) /* Input Buffer Full */ |
| 96 | #define KB_OBF (1 << 0) /* Output Buffer Full */ |
| 97 | |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 98 | /* IT8502 supports two access modes: |
| 99 | * LPC_MEMORY: through the memory window in 0xFFFFFxxx (follow mode) |
| 100 | * LPC_IO: through I/O port (so called indirect memory) |
| 101 | */ |
| 102 | #undef LPC_MEMORY |
| 103 | #define LPC_IO |
| 104 | |
| 105 | #ifdef LPC_IO |
| 106 | /* macro to fill in indirect-access registers. */ |
| 107 | #define INDIRECT_A0(base, value) OUTB(value, (base) + 0) /* little-endian */ |
| 108 | #define INDIRECT_A1(base, value) OUTB(value, (base) + 1) |
| 109 | #define INDIRECT_A2(base, value) OUTB(value, (base) + 2) |
| 110 | #define INDIRECT_A3(base, value) OUTB(value, (base) + 3) |
| 111 | #define INDIRECT_READ(base) INB((base) + 4) |
| 112 | #define INDIRECT_WRITE(base, value) OUTB(value, (base) + 4) |
| 113 | #endif /* LPC_IO */ |
| 114 | |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 115 | struct it85spi_data { |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 116 | #ifdef LPC_IO |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 117 | unsigned int shm_io_base; |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 118 | #endif |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 119 | unsigned char *ce_high, *ce_low; |
| 120 | int it85xx_scratch_rom_reenter; |
| 121 | }; |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 122 | |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 123 | /* This function will poll the keyboard status register until either |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 124 | * an expected value shows up, or the timeout is reached. |
| 125 | * timeout is in usec. |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 126 | * |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 127 | * Returns: 0 -- the expected value showed up. |
| 128 | * 1 -- timeout. |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 129 | */ |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 130 | static int wait_for(const unsigned int mask, const unsigned int expected_value, |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 131 | const int timeout, const char * error_message, |
| 132 | const char * function_name, const int lineno) |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 133 | { |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 134 | int time_passed; |
| 135 | |
| 136 | for (time_passed = 0;; ++time_passed) { |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 137 | if ((INB(found_chip->port_cmd) & mask) == expected_value) |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 138 | return 0; |
| 139 | if (time_passed >= timeout) |
| 140 | break; |
| 141 | programmer_delay(1); |
| 142 | } |
| 143 | if (error_message) |
| 144 | msg_perr("%s():%d %s", function_name, lineno, error_message); |
| 145 | return 1; |
| 146 | } |
| 147 | |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 148 | /* IT8502 employs a scratch RAM when flash is being updated. Call the following |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 149 | * two functions before/after flash erase/program. */ |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 150 | static void it85xx_enter_scratch_rom(struct it85spi_data *data) |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 151 | { |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 152 | int ret, tries; |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 153 | |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 154 | if (data->it85xx_scratch_rom_reenter > 0) |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 155 | return; |
David Hendricks | 932dfe4 | 2012-09-21 14:46:41 -0700 | [diff] [blame] | 156 | |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 157 | msg_pdbg("%s: entering scratch rom mode\n", __func__); |
| 158 | |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 159 | for (tries = 0; tries < MAX_TRY; ++tries) { |
| 160 | /* Wait until IBF (input buffer) is not full. */ |
| 161 | if (wait_for(KB_IBF, 0, MAX_TIMEOUT, |
| 162 | "* timeout at waiting for IBF==0.\n", |
uwe | 44e2dc4 | 2011-07-29 20:13:45 +0000 | [diff] [blame] | 163 | __func__, __LINE__)) |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 164 | continue; |
| 165 | |
| 166 | /* Copy EC firmware to SRAM. */ |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 167 | OUTB(found_chip->copy_to_sram_cmd, found_chip->port_cmd); |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 168 | |
| 169 | /* Confirm EC has taken away the command. */ |
| 170 | if (wait_for(KB_IBF, 0, MAX_TIMEOUT, |
| 171 | "* timeout at taking command.\n", |
uwe | 44e2dc4 | 2011-07-29 20:13:45 +0000 | [diff] [blame] | 172 | __func__, __LINE__)) |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 173 | continue; |
| 174 | |
| 175 | /* Waiting for OBF (output buffer) has data. |
| 176 | * Note sometimes the replied command might be stolen by kernel |
| 177 | * ISR so that it is okay as long as the command is 0xFA. */ |
| 178 | if (wait_for(KB_OBF, KB_OBF, MAX_TIMEOUT, NULL, NULL, 0)) |
| 179 | msg_pdbg("%s():%d * timeout at waiting for OBF.\n", |
uwe | 44e2dc4 | 2011-07-29 20:13:45 +0000 | [diff] [blame] | 180 | __func__, __LINE__); |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 181 | if ((ret = INB(found_chip->port_data)) == 0xFA) { |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 182 | break; |
| 183 | } else { |
| 184 | msg_perr("%s():%d * not run on SRAM ret=%d\n", |
uwe | 44e2dc4 | 2011-07-29 20:13:45 +0000 | [diff] [blame] | 185 | __func__, __LINE__, ret); |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 186 | continue; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (tries < MAX_TRY) { |
| 191 | /* EC already runs on SRAM */ |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 192 | data->it85xx_scratch_rom_reenter++; |
uwe | 44e2dc4 | 2011-07-29 20:13:45 +0000 | [diff] [blame] | 193 | msg_pdbg("%s():%d * SUCCESS.\n", __func__, __LINE__); |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 194 | } else { |
uwe | 44e2dc4 | 2011-07-29 20:13:45 +0000 | [diff] [blame] | 195 | msg_perr("%s():%d * Max try reached.\n", __func__, __LINE__); |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 196 | } |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 199 | static void it85xx_exit_scratch_rom(struct it85spi_data *data) |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 200 | { |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 201 | int tries; |
| 202 | |
uwe | 44e2dc4 | 2011-07-29 20:13:45 +0000 | [diff] [blame] | 203 | msg_pdbg("%s():%d was called ...\n", __func__, __LINE__); |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 204 | if (data->it85xx_scratch_rom_reenter <= 0) |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 205 | return; |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 206 | |
| 207 | for (tries = 0; tries < MAX_TRY; ++tries) { |
| 208 | /* Wait until IBF (input buffer) is not full. */ |
| 209 | if (wait_for(KB_IBF, 0, MAX_TIMEOUT, |
| 210 | "* timeout at waiting for IBF==0.\n", |
uwe | 44e2dc4 | 2011-07-29 20:13:45 +0000 | [diff] [blame] | 211 | __func__, __LINE__)) |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 212 | continue; |
| 213 | |
| 214 | /* Exit SRAM. Run on flash. */ |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 215 | OUTB(found_chip->exit_sram_cmd, found_chip->port_cmd); |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 216 | |
| 217 | /* Confirm EC has taken away the command. */ |
| 218 | if (wait_for(KB_IBF, 0, MAX_TIMEOUT, |
| 219 | "* timeout at taking command.\n", |
uwe | 44e2dc4 | 2011-07-29 20:13:45 +0000 | [diff] [blame] | 220 | __func__, __LINE__)) { |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 221 | /* We cannot ensure if EC has exited update mode. |
| 222 | * If EC is in normal mode already, a further 0xFE |
| 223 | * command will reboot system. So, exit loop here. */ |
| 224 | tries = MAX_TRY; |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | if (tries < MAX_TRY) { |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 232 | data->it85xx_scratch_rom_reenter = 0; |
uwe | 44e2dc4 | 2011-07-29 20:13:45 +0000 | [diff] [blame] | 233 | msg_pdbg("%s():%d * SUCCESS.\n", __func__, __LINE__); |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 234 | } else { |
uwe | 44e2dc4 | 2011-07-29 20:13:45 +0000 | [diff] [blame] | 235 | msg_perr("%s():%d * Max try reached.\n", __func__, __LINE__); |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 236 | } |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 237 | |
| 238 | programmer_delay(found_chip->exit_sram_delay); |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 239 | } |
| 240 | |
David Hendricks | 93784b4 | 2016-08-09 17:00:38 -0700 | [diff] [blame] | 241 | static int it85xx_shutdown(void *data) |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 242 | { |
| 243 | msg_pdbg("%s():%d\n", __func__, __LINE__); |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 244 | it85xx_exit_scratch_rom(data); |
| 245 | free(data); |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 246 | |
David Hendricks | 932dfe4 | 2012-09-21 14:46:41 -0700 | [diff] [blame] | 247 | return 0; /* FIXME: Should probably return something meaningful */ |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 248 | } |
| 249 | |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 250 | /* According to ITE 8502 document, the procedure to follow mode is following: |
| 251 | * 1. write 0x00 to LPC/FWH address 0xffff_fexxh (drive CE# high) |
| 252 | * 2. write data to LPC/FWH address 0xffff_fdxxh (drive CE# low and MOSI |
| 253 | * with data) |
| 254 | * 3. read date from LPC/FWH address 0xffff_fdxxh (drive CE# low and get |
| 255 | * data from MISO) |
| 256 | */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 257 | static int it85xx_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 258 | const unsigned char *writearr, unsigned char *readarr) |
| 259 | { |
Edward O'Callaghan | 82d7a4d | 2021-01-08 12:19:23 +1100 | [diff] [blame] | 260 | unsigned i; |
David Hendricks | 7491585 | 2012-09-27 16:39:37 -0700 | [diff] [blame] | 261 | static int wdt_reset_flag_set = 0; |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 262 | struct it85spi_data *data = flash->mst->spi.data; |
David Hendricks | 7491585 | 2012-09-27 16:39:37 -0700 | [diff] [blame] | 263 | |
| 264 | if (found_chip->chip_id == ITE_IT8518) { |
| 265 | /* |
| 266 | * 0xd8 - Sets WDT reset flag to reboot EC after exiting from |
| 267 | * scratch mode. This flag will be be checked when |
| 268 | * command 0x8c is received. Use this when changing |
| 269 | * ROM content (erase / write commands). |
| 270 | * 0xdd - Same as d8, but without setting the WDT reset flag. |
| 271 | * Use this for commands that do not change EC code. |
| 272 | * |
| 273 | * If opcode will cause ROM content to change and scratch |
| 274 | * mode has been previously entered, then we need to re-enter |
| 275 | * scratch mode using 0xd8. |
| 276 | * |
| 277 | * FIXME(dhendrix): This is specific to Stout. We should use |
| 278 | * better method to apply board hacks rather than using the |
| 279 | * EC's chip ID as the condition. |
| 280 | */ |
| 281 | switch (writearr[0]) { |
| 282 | case JEDEC_BYTE_PROGRAM: |
| 283 | case JEDEC_BE_52: |
| 284 | case JEDEC_BE_D7: |
| 285 | case JEDEC_BE_D8: |
| 286 | case JEDEC_CE_60: |
| 287 | case JEDEC_CE_C7: |
| 288 | case JEDEC_SE: |
| 289 | if (!wdt_reset_flag_set) { |
| 290 | msg_pdbg("%s: changing copy_to_sram_cmd\n", |
| 291 | __func__); |
| 292 | found_chip->copy_to_sram_cmd = 0xd8; |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 293 | it85xx_exit_scratch_rom(data); |
David Hendricks | 7491585 | 2012-09-27 16:39:37 -0700 | [diff] [blame] | 294 | wdt_reset_flag_set = 1; |
| 295 | } |
| 296 | break; |
| 297 | default: |
| 298 | break; |
| 299 | } |
| 300 | } |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 301 | |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 302 | it85xx_enter_scratch_rom(data); |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 303 | /* Exit scratch ROM ONLY when programmer shuts down. Otherwise, the |
| 304 | * temporary flash state may halt the EC. |
| 305 | */ |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 306 | |
| 307 | #ifdef LPC_IO |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 308 | INDIRECT_A1(data->shm_io_base, (((unsigned long int)data->ce_high) >> 8) & 0xff); |
| 309 | INDIRECT_WRITE(data->shm_io_base, 0xFF); /* Write anything to this address.*/ |
| 310 | INDIRECT_A1(data->shm_io_base, (((unsigned long int)data->ce_low) >> 8) & 0xff); |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 311 | #endif |
| 312 | #ifdef LPC_MEMORY |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 313 | mmio_writeb(0, data->ce_high); |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 314 | #endif |
| 315 | for (i = 0; i < writecnt; ++i) { |
| 316 | #ifdef LPC_IO |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 317 | INDIRECT_WRITE(data->shm_io_base, writearr[i]); |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 318 | #endif |
| 319 | #ifdef LPC_MEMORY |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 320 | mmio_writeb(writearr[i], data->ce_low); |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 321 | #endif |
| 322 | } |
| 323 | for (i = 0; i < readcnt; ++i) { |
| 324 | #ifdef LPC_IO |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 325 | readarr[i] = INDIRECT_READ(data->shm_io_base); |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 326 | #endif |
| 327 | #ifdef LPC_MEMORY |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 328 | readarr[i] = mmio_readb(data->ce_low); |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 329 | #endif |
| 330 | } |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 331 | #ifdef LPC_IO |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 332 | INDIRECT_A1(data->shm_io_base, (((unsigned long int)data->ce_high) >> 8) & 0xff); |
| 333 | INDIRECT_WRITE(data->shm_io_base, 0xFF); /* Write anything to this address.*/ |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 334 | #endif |
| 335 | #ifdef LPC_MEMORY |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 336 | mmio_writeb(0, data->ce_high); |
hailfinger | 2b46a86 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 337 | #endif |
| 338 | |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 342 | static const struct spi_master spi_master_it8518 = { |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 343 | .max_data_read = 256, |
| 344 | .max_data_write = 256, |
| 345 | .command = it85xx_spi_send_command, |
| 346 | .multicommand = default_spi_send_multicommand, |
| 347 | .read = default_spi_read, |
| 348 | .write_256 = default_spi_write_256, |
| 349 | }; |
| 350 | |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 351 | static const struct spi_master spi_master_it85xx = { |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 352 | .max_data_read = 1, |
| 353 | .max_data_write = 1, |
| 354 | .command = it85xx_spi_send_command, |
| 355 | .multicommand = default_spi_send_multicommand, |
| 356 | .read = default_spi_read, |
| 357 | .write_256 = default_spi_write_256, |
| 358 | }; |
| 359 | |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 360 | /* it8518-specific i/o initialization */ |
Edward O'Callaghan | d22a9e7 | 2020-11-18 11:19:50 +1100 | [diff] [blame] | 361 | static void setup_it8518_io_base() |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 362 | { |
| 363 | OUTB(0x07, 0x2e); /* Set LDN to SHM */ |
| 364 | OUTB(0x0f, 0x2f); |
| 365 | OUTB(0x60, 0x2e); /* Set IO space to 0x3F0 */ |
| 366 | OUTB(0x03, 0x2f); |
| 367 | OUTB(0x61, 0x2e); |
| 368 | OUTB(0xf0, 0x2f); |
| 369 | OUTB(0x30, 0x2e); /* Enable this Logical Device */ |
| 370 | OUTB(0x01, 0x2f); |
| 371 | } |
| 372 | |
David Hendricks | 2b56e47 | 2016-10-21 19:05:21 -0700 | [diff] [blame] | 373 | static int check_params(void) |
| 374 | { |
| 375 | int ret = 0; |
| 376 | char *p = NULL; |
| 377 | |
| 378 | p = extract_programmer_param("type"); |
| 379 | if (p && strcmp(p, "ec")) { |
| 380 | msg_pdbg("it85xx only supports \"ec\" type devices\n"); |
| 381 | ret = 1; |
| 382 | } |
| 383 | |
| 384 | free(p); |
| 385 | return ret; |
| 386 | } |
| 387 | |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 388 | static int it85xx_spi_common_init(struct superio s, struct it85spi_data *data) |
Edward O'Callaghan | 9870435 | 2020-11-18 10:59:00 +1100 | [diff] [blame] | 389 | { |
| 390 | chipaddr base; |
| 391 | |
| 392 | msg_pdbg("%s():%d superio.vendor=0x%02x\n", __func__, __LINE__, |
| 393 | s.vendor); |
| 394 | |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 395 | if (register_shutdown(it85xx_shutdown, data)) { |
| 396 | free(data); |
Edward O'Callaghan | 9870435 | 2020-11-18 10:59:00 +1100 | [diff] [blame] | 397 | return 1; |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 398 | } |
Edward O'Callaghan | 9870435 | 2020-11-18 10:59:00 +1100 | [diff] [blame] | 399 | |
| 400 | #ifdef LPC_IO |
| 401 | /* Get LPCPNP of SHM. That's big-endian. */ |
| 402 | sio_write(s.port, LDNSEL, 0x0F); /* Set LDN to SHM (0x0F) */ |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 403 | data->shm_io_base = (sio_read(s.port, SHM_IO_BAR0) << 8) + |
Edward O'Callaghan | 9870435 | 2020-11-18 10:59:00 +1100 | [diff] [blame] | 404 | sio_read(s.port, SHM_IO_BAR1); |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 405 | msg_pdbg("%s():%d it85spi_data->shm_io_base=0x%04x\n", __func__, __LINE__, |
| 406 | data->shm_io_base); |
Edward O'Callaghan | 9870435 | 2020-11-18 10:59:00 +1100 | [diff] [blame] | 407 | |
| 408 | /* These pointers are not used directly. They will be send to EC's |
| 409 | * register for indirect access. */ |
| 410 | base = 0xFFFFF000; |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 411 | data->ce_high = ((unsigned char *)base) + 0xE00; /* 0xFFFFFE00 */ |
| 412 | data->ce_low = ((unsigned char *)base) + 0xD00; /* 0xFFFFFD00 */ |
Edward O'Callaghan | 9870435 | 2020-11-18 10:59:00 +1100 | [diff] [blame] | 413 | |
| 414 | /* pre-set indirect-access registers since in most of cases they are |
| 415 | * 0xFFFFxx00. */ |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 416 | INDIRECT_A0(data->shm_io_base, base & 0xFF); |
| 417 | INDIRECT_A2(data->shm_io_base, (base >> 16) & 0xFF); |
| 418 | INDIRECT_A3(data->shm_io_base, (base >> 24)); |
Edward O'Callaghan | 9870435 | 2020-11-18 10:59:00 +1100 | [diff] [blame] | 419 | #endif |
| 420 | #ifdef LPC_MEMORY |
| 421 | /* FIXME: We should block accessing that region for anything else. |
| 422 | * Major TODO here, and it will be a lot of work. |
| 423 | */ |
| 424 | base = (chipaddr)physmap("it85 communication", 0xFFFFF000, 0x1000); |
| 425 | msg_pdbg("%s():%d base=0x%08x\n", __func__, __LINE__, |
| 426 | (unsigned int)base); |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 427 | data->ce_high = (unsigned char *)(base + 0xE00); /* 0xFFFFFE00 */ |
| 428 | data->ce_low = (unsigned char *)(base + 0xD00); /* 0xFFFFFD00 */ |
Edward O'Callaghan | 9870435 | 2020-11-18 10:59:00 +1100 | [diff] [blame] | 429 | #endif |
| 430 | |
| 431 | return 0; |
| 432 | } |
| 433 | |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 434 | int it8518_spi_init(struct superio s) |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 435 | { |
| 436 | int ret; |
David Hendricks | 2b56e47 | 2016-10-21 19:05:21 -0700 | [diff] [blame] | 437 | |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 438 | struct it85spi_data *data = calloc(1, sizeof(struct it85spi_data)); |
| 439 | if (!data) { |
| 440 | msg_perr("Unable to allocate space for extra SPI master data.\n"); |
| 441 | return SPI_GENERIC_ERROR; |
| 442 | } |
| 443 | |
David Hendricks | 2b56e47 | 2016-10-21 19:05:21 -0700 | [diff] [blame] | 444 | if (check_params()) |
| 445 | return 1; |
| 446 | |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 447 | if (!(internal_buses_supported & BUS_FWH)) { |
| 448 | msg_pdbg("%s():%d buses not support FWH\n", __func__, __LINE__); |
| 449 | return 1; |
| 450 | } |
| 451 | |
| 452 | found_chip = &ite_chips[ITE_IT8518]; |
| 453 | |
| 454 | #ifdef LPC_IO |
| 455 | setup_it8518_io_base(); |
| 456 | #endif |
| 457 | |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 458 | ret = it85xx_spi_common_init(s, data); |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 459 | if (!ret) { |
| 460 | msg_pdbg("%s: internal_buses_supported=0x%x\n", __func__, |
| 461 | internal_buses_supported); |
| 462 | /* Check for FWH because IT85 listens to FWH cycles. |
| 463 | * FIXME: The big question is whether FWH cycles are necessary |
| 464 | * for communication even if LPC_IO is defined. |
| 465 | */ |
| 466 | if (internal_buses_supported & BUS_FWH) |
| 467 | msg_pdbg("Registering IT85 SPI.\n"); |
| 468 | /* FIXME: Really leave FWH enabled? We can't use this region |
| 469 | * anymore since accessing it would mess up IT85 communication. |
| 470 | * If we decide to disable FWH for this region, we should print |
| 471 | * a debug message about it. |
| 472 | */ |
| 473 | /* Set this as SPI controller and add FWH | LPC to |
| 474 | * supported buses. */ |
Edward O'Callaghan | c66827e | 2020-10-09 12:22:04 +1100 | [diff] [blame] | 475 | internal_buses_supported |= BUS_LPC | BUS_FWH; |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 476 | register_spi_master(&spi_master_it8518, data); |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 477 | } |
| 478 | return ret; |
| 479 | } |
| 480 | |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 481 | int it85xx_spi_init(struct superio s) |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 482 | { |
| 483 | int ret; |
| 484 | |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 485 | struct it85spi_data *data = calloc(1, sizeof(struct it85spi_data)); |
| 486 | if (!data) { |
| 487 | msg_perr("Unable to allocate space for extra SPI master data.\n"); |
| 488 | return SPI_GENERIC_ERROR; |
| 489 | } |
| 490 | |
David Hendricks | 2b56e47 | 2016-10-21 19:05:21 -0700 | [diff] [blame] | 491 | if (check_params()) |
| 492 | return 1; |
| 493 | |
Shawn Nematbakhsh | 3404b1a | 2012-07-26 15:19:58 -0700 | [diff] [blame] | 494 | found_chip = &ite_chips[ITE_IT85XX]; |
| 495 | |
David Hendricks | ba0827a | 2013-05-03 20:25:40 -0700 | [diff] [blame] | 496 | chipset_flash_enable(); |
| 497 | |
Anastasia Klimchuk | 39c3a35 | 2020-11-17 13:25:46 +1100 | [diff] [blame] | 498 | ret = it85xx_spi_common_init(s, data); |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 499 | msg_pdbg("FWH: %s():%d ret=%d\n", __func__, __LINE__, ret); |
| 500 | if (!ret) { |
hailfinger | 76bb7e9 | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 501 | msg_pdbg("%s: internal_buses_supported=0x%x\n", __func__, |
| 502 | internal_buses_supported); |
| 503 | /* Check for FWH because IT85 listens to FWH cycles. |
| 504 | * FIXME: The big question is whether FWH cycles are necessary |
| 505 | * for communication even if LPC_IO is defined. |
| 506 | */ |
| 507 | if (internal_buses_supported & BUS_FWH) |
| 508 | msg_pdbg("Registering IT85 SPI.\n"); |
| 509 | /* FIXME: Really leave FWH enabled? We can't use this region |
| 510 | * anymore since accessing it would mess up IT85 communication. |
| 511 | * If we decide to disable FWH for this region, we should print |
| 512 | * a debug message about it. |
| 513 | */ |
David Hendricks | ac97ece | 2011-12-15 15:27:22 -0800 | [diff] [blame] | 514 | /* Set this as SPI controller and add FWH | LPC to |
| 515 | * supported buses. */ |
Edward O'Callaghan | c66827e | 2020-10-09 12:22:04 +1100 | [diff] [blame] | 516 | internal_buses_supported |= BUS_LPC | BUS_FWH; |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 517 | register_spi_master(&spi_master_it85xx, data); |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 518 | } |
David Hendricks | 2b56e47 | 2016-10-21 19:05:21 -0700 | [diff] [blame] | 519 | |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 520 | return ret; |
| 521 | } |
| 522 | |
hailfinger | 8f09149 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 523 | #endif |