hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2008 Stefan Wildemann <stefan.wildemann@kontron.com> |
| 5 | * Copyright (C) 2008 Claus Gindhart <claus.gindhart@kontron.com> |
| 6 | * Copyright (C) 2008 Dominik Geyer <dominik.geyer@kontron.com> |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 7 | * Copyright (C) 2008 coresystems GmbH <info@coresystems.de> |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 8 | * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * This module is designed for supporting the devices |
| 27 | * ST M25P40 |
| 28 | * ST M25P80 |
| 29 | * ST M25P16 |
| 30 | * ST M25P32 already tested |
| 31 | * ST M25P64 |
| 32 | * AT 25DF321 already tested |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 33 | * ... and many more SPI flash devices |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 34 | * |
| 35 | */ |
| 36 | |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 37 | #if defined(__i386__) || defined(__x86_64__) |
| 38 | |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 39 | #include <string.h> |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 40 | #include "flash.h" |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 41 | #include "chipdrivers.h" |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 42 | #include "programmer.h" |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 43 | #include "spi.h" |
| 44 | |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 45 | /* ICH9 controller register definition */ |
| 46 | #define ICH9_REG_FADDR 0x08 /* 32 Bits */ |
| 47 | #define ICH9_REG_FDATA0 0x10 /* 64 Bytes */ |
| 48 | |
| 49 | #define ICH9_REG_SSFS 0x90 /* 08 Bits */ |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 50 | #define SSFS_SCIP 0x00000001 |
| 51 | #define SSFS_CDS 0x00000004 |
| 52 | #define SSFS_FCERR 0x00000008 |
| 53 | #define SSFS_AEL 0x00000010 |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 54 | |
| 55 | #define ICH9_REG_SSFC 0x91 /* 24 Bits */ |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 56 | #define SSFC_SCGO 0x00000200 |
| 57 | #define SSFC_ACS 0x00000400 |
| 58 | #define SSFC_SPOP 0x00000800 |
| 59 | #define SSFC_COP 0x00001000 |
| 60 | #define SSFC_DBC 0x00010000 |
| 61 | #define SSFC_DS 0x00400000 |
| 62 | #define SSFC_SME 0x00800000 |
| 63 | #define SSFC_SCF 0x01000000 |
| 64 | #define SSFC_SCF_20MHZ 0x00000000 |
| 65 | #define SSFC_SCF_33MHZ 0x01000000 |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 66 | |
| 67 | #define ICH9_REG_PREOP 0x94 /* 16 Bits */ |
| 68 | #define ICH9_REG_OPTYPE 0x96 /* 16 Bits */ |
| 69 | #define ICH9_REG_OPMENU 0x98 /* 64 Bits */ |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 70 | |
| 71 | // ICH9R SPI commands |
| 72 | #define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0 |
| 73 | #define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1 |
| 74 | #define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2 |
| 75 | #define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3 |
| 76 | |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 77 | // ICH7 registers |
| 78 | #define ICH7_REG_SPIS 0x00 /* 16 Bits */ |
| 79 | #define SPIS_SCIP 0x00000001 |
| 80 | #define SPIS_CDS 0x00000004 |
| 81 | #define SPIS_FCERR 0x00000008 |
| 82 | |
ruik | 9bc51c0 | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 83 | /* VIA SPI is compatible with ICH7, but maxdata |
| 84 | to transfer is 16 bytes. |
| 85 | |
| 86 | DATA byte count on ICH7 is 8:13, on VIA 8:11 |
| 87 | |
| 88 | bit 12 is port select CS0 CS1 |
| 89 | bit 13 is FAST READ enable |
| 90 | bit 7 is used with fast read and one shot controls CS de-assert? |
| 91 | */ |
| 92 | |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 93 | #define ICH7_REG_SPIC 0x02 /* 16 Bits */ |
| 94 | #define SPIC_SCGO 0x0002 |
| 95 | #define SPIC_ACS 0x0004 |
| 96 | #define SPIC_SPOP 0x0008 |
ruik | 9bc51c0 | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 97 | #define SPIC_DS 0x4000 |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 98 | |
| 99 | #define ICH7_REG_SPIA 0x04 /* 32 Bits */ |
| 100 | #define ICH7_REG_SPID0 0x08 /* 64 Bytes */ |
| 101 | #define ICH7_REG_PREOP 0x54 /* 16 Bits */ |
| 102 | #define ICH7_REG_OPTYPE 0x56 /* 16 Bits */ |
| 103 | #define ICH7_REG_OPMENU 0x58 /* 64 Bits */ |
| 104 | |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 105 | /* ICH SPI configuration lock-down. May be set during chipset enabling. */ |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 106 | static int ichspi_lock = 0; |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 107 | |
hailfinger | b767c12 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 108 | uint32_t ichspi_bbar = 0; |
| 109 | |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 110 | static void *ich_spibar = NULL; |
| 111 | |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 112 | typedef struct _OPCODE { |
| 113 | uint8_t opcode; //This commands spi opcode |
| 114 | uint8_t spi_type; //This commands spi type |
| 115 | uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1 |
| 116 | } OPCODE; |
| 117 | |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 118 | /* Suggested opcode definition: |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 119 | * Preop 1: Write Enable |
| 120 | * Preop 2: Write Status register enable |
| 121 | * |
| 122 | * OP 0: Write address |
| 123 | * OP 1: Read Address |
| 124 | * OP 2: ERASE block |
| 125 | * OP 3: Read Status register |
| 126 | * OP 4: Read ID |
| 127 | * OP 5: Write Status register |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 128 | * OP 6: chip private (read JEDEC id) |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 129 | * OP 7: Chip erase |
| 130 | */ |
| 131 | typedef struct _OPCODES { |
| 132 | uint8_t preop[2]; |
| 133 | OPCODE opcode[8]; |
| 134 | } OPCODES; |
| 135 | |
stepan | 8f46dd6 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 136 | static OPCODES *curopcodes = NULL; |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 137 | |
| 138 | /* HW access functions */ |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 139 | static uint32_t REGREAD32(int X) |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 140 | { |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 141 | return mmio_readl(ich_spibar + X); |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 142 | } |
| 143 | |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 144 | static uint16_t REGREAD16(int X) |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 145 | { |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 146 | return mmio_readw(ich_spibar + X); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 147 | } |
| 148 | |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 149 | #define REGWRITE32(X,Y) mmio_writel(Y, ich_spibar+X) |
| 150 | #define REGWRITE16(X,Y) mmio_writew(Y, ich_spibar+X) |
| 151 | #define REGWRITE8(X,Y) mmio_writeb(Y, ich_spibar+X) |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 152 | |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 153 | /* Common SPI functions */ |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 154 | static int find_opcode(OPCODES *op, uint8_t opcode); |
| 155 | static int find_preop(OPCODES *op, uint8_t preop); |
stuge | 7a65155 | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 156 | static int generate_opcodes(OPCODES * op); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 157 | static int program_opcodes(OPCODES * op); |
stepan | 82c65bd | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 158 | static int run_opcode(OPCODE op, uint32_t offset, |
stepan | 8f46dd6 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 159 | uint8_t datalength, uint8_t * data); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 160 | |
stuge | 7a65155 | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 161 | /* for pairing opcodes with their required preop */ |
| 162 | struct preop_opcode_pair { |
| 163 | uint8_t preop; |
| 164 | uint8_t opcode; |
| 165 | }; |
| 166 | |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 167 | /* List of opcodes which need preopcodes and matching preopcodes. Unused. */ |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 168 | const struct preop_opcode_pair pops[] = { |
stuge | 7a65155 | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 169 | {JEDEC_WREN, JEDEC_BYTE_PROGRAM}, |
| 170 | {JEDEC_WREN, JEDEC_SE}, /* sector erase */ |
| 171 | {JEDEC_WREN, JEDEC_BE_52}, /* block erase */ |
| 172 | {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */ |
| 173 | {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */ |
| 174 | {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */ |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 175 | /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */ |
| 176 | {JEDEC_WREN, JEDEC_WRSR}, |
stuge | 7a65155 | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 177 | {JEDEC_EWSR, JEDEC_WRSR}, |
| 178 | {0,} |
| 179 | }; |
| 180 | |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 181 | /* Reasonable default configuration. Needs ad-hoc modifications if we |
| 182 | * encounter unlisted opcodes. Fun. |
| 183 | */ |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 184 | static OPCODES O_ST_M25P = { |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 185 | { |
| 186 | JEDEC_WREN, |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 187 | JEDEC_EWSR, |
| 188 | }, |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 189 | { |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 190 | {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte |
stepan | 8f46dd6 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 191 | {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data |
David Hendricks | 813dd7a | 2010-08-26 21:27:17 -0700 | [diff] [blame] | 192 | {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector |
stepan | 8f46dd6 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 193 | {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg |
hailfinger | e092f84 | 2009-05-26 21:25:08 +0000 | [diff] [blame] | 194 | {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 195 | {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register |
stepan | 8f46dd6 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 196 | {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 197 | {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase |
| 198 | } |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 201 | /* List of opcodes with their corresponding spi_type |
| 202 | * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode |
| 203 | * is needed which is currently not in the chipset OPCODE table |
| 204 | */ |
| 205 | static OPCODE POSSIBLE_OPCODES[] = { |
| 206 | {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte |
| 207 | {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data |
| 208 | {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector |
| 209 | {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg |
| 210 | {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature |
| 211 | {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register |
| 212 | {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID |
| 213 | {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase |
| 214 | {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase |
| 215 | {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase |
| 216 | {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment |
| 217 | }; |
| 218 | |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 219 | static OPCODES O_EXISTING = {}; |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 220 | |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 221 | static uint8_t lookup_spi_type(uint8_t opcode) |
| 222 | { |
| 223 | int a; |
| 224 | |
| 225 | for (a = 0; a < sizeof(POSSIBLE_OPCODES)/sizeof(POSSIBLE_OPCODES[0]); a++) { |
| 226 | if (POSSIBLE_OPCODES[a].opcode == opcode) |
| 227 | return POSSIBLE_OPCODES[a].spi_type; |
| 228 | } |
| 229 | |
| 230 | return 0xFF; |
| 231 | } |
| 232 | |
| 233 | static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt) |
| 234 | { |
| 235 | uint8_t spi_type; |
| 236 | |
| 237 | spi_type = lookup_spi_type(opcode); |
| 238 | if (spi_type > 3) { |
| 239 | /* Try to guess spi type from read/write sizes. |
| 240 | * The following valid writecnt/readcnt combinations exist: |
| 241 | * writecnt = 4, readcnt >= 0 |
| 242 | * writecnt = 1, readcnt >= 0 |
| 243 | * writecnt >= 4, readcnt = 0 |
| 244 | * writecnt >= 1, readcnt = 0 |
| 245 | * writecnt >= 1 is guaranteed for all commands. |
| 246 | */ |
| 247 | if (readcnt == 0) |
| 248 | /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS |
| 249 | * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data |
| 250 | * bytes are actual the address, they go to the bus anyhow |
| 251 | */ |
| 252 | spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS; |
| 253 | else if (writecnt == 1) // and readcnt is > 0 |
| 254 | spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS; |
| 255 | else if (writecnt == 4) // and readcnt is > 0 |
| 256 | spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS; |
| 257 | // else we have an invalid case, will be handled below |
| 258 | } |
| 259 | if (spi_type <= 3) { |
| 260 | int oppos=2; // use original JEDEC_BE_D8 offset |
| 261 | curopcodes->opcode[oppos].opcode = opcode; |
| 262 | curopcodes->opcode[oppos].spi_type = spi_type; |
| 263 | program_opcodes(curopcodes); |
| 264 | oppos = find_opcode(curopcodes, opcode); |
| 265 | msg_pdbg ("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos); |
| 266 | return oppos; |
| 267 | } |
| 268 | return -1; |
| 269 | } |
| 270 | |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 271 | static int find_opcode(OPCODES *op, uint8_t opcode) |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 272 | { |
| 273 | int a; |
| 274 | |
| 275 | for (a = 0; a < 8; a++) { |
| 276 | if (op->opcode[a].opcode == opcode) |
| 277 | return a; |
| 278 | } |
| 279 | |
| 280 | return -1; |
| 281 | } |
| 282 | |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 283 | static int find_preop(OPCODES *op, uint8_t preop) |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 284 | { |
| 285 | int a; |
| 286 | |
| 287 | for (a = 0; a < 2; a++) { |
| 288 | if (op->preop[a] == preop) |
| 289 | return a; |
| 290 | } |
| 291 | |
| 292 | return -1; |
| 293 | } |
| 294 | |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 295 | /* Create a struct OPCODES based on what we find in the locked down chipset. */ |
stuge | 7a65155 | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 296 | static int generate_opcodes(OPCODES * op) |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 297 | { |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 298 | int a; |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 299 | uint16_t preop, optype; |
| 300 | uint32_t opmenu[2]; |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 301 | |
| 302 | if (op == NULL) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 303 | msg_perr("\n%s: null OPCODES pointer!\n", __func__); |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 304 | return -1; |
| 305 | } |
| 306 | |
hailfinger | 4016746 | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 307 | switch (spi_controller) { |
| 308 | case SPI_CONTROLLER_ICH7: |
| 309 | case SPI_CONTROLLER_VIA: |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 310 | preop = REGREAD16(ICH7_REG_PREOP); |
| 311 | optype = REGREAD16(ICH7_REG_OPTYPE); |
| 312 | opmenu[0] = REGREAD32(ICH7_REG_OPMENU); |
| 313 | opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4); |
| 314 | break; |
hailfinger | 4016746 | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 315 | case SPI_CONTROLLER_ICH9: |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 316 | preop = REGREAD16(ICH9_REG_PREOP); |
| 317 | optype = REGREAD16(ICH9_REG_OPTYPE); |
| 318 | opmenu[0] = REGREAD32(ICH9_REG_OPMENU); |
| 319 | opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4); |
| 320 | break; |
| 321 | default: |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 322 | msg_perr("%s: unsupported chipset\n", __func__); |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | op->preop[0] = (uint8_t) preop; |
| 327 | op->preop[1] = (uint8_t) (preop >> 8); |
| 328 | |
| 329 | for (a = 0; a < 8; a++) { |
| 330 | op->opcode[a].spi_type = (uint8_t) (optype & 0x3); |
| 331 | optype >>= 2; |
| 332 | } |
| 333 | |
| 334 | for (a = 0; a < 4; a++) { |
| 335 | op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff); |
| 336 | opmenu[0] >>= 8; |
| 337 | } |
| 338 | |
| 339 | for (a = 4; a < 8; a++) { |
| 340 | op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff); |
| 341 | opmenu[1] >>= 8; |
| 342 | } |
| 343 | |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 344 | /* No preopcodes used by default. */ |
| 345 | for (a = 0; a < 8; a++) |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 346 | op->opcode[a].atomic = 0; |
| 347 | |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 351 | int program_opcodes(OPCODES * op) |
| 352 | { |
| 353 | uint8_t a; |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 354 | uint16_t preop, optype; |
| 355 | uint32_t opmenu[2]; |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 356 | |
| 357 | /* Program Prefix Opcodes */ |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 358 | /* 0:7 Prefix Opcode 1 */ |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 359 | preop = (op->preop[0]); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 360 | /* 8:16 Prefix Opcode 2 */ |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 361 | preop |= ((uint16_t) op->preop[1]) << 8; |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 362 | |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 363 | /* Program Opcode Types 0 - 7 */ |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 364 | optype = 0; |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 365 | for (a = 0; a < 8; a++) { |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 366 | optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 367 | } |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 368 | |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 369 | /* Program Allowable Opcodes 0 - 3 */ |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 370 | opmenu[0] = 0; |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 371 | for (a = 0; a < 4; a++) { |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 372 | opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 373 | } |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 374 | |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 375 | /*Program Allowable Opcodes 4 - 7 */ |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 376 | opmenu[1] = 0; |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 377 | for (a = 4; a < 8; a++) { |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 378 | opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 379 | } |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 380 | |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 381 | msg_pdbg("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]); |
hailfinger | 4016746 | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 382 | switch (spi_controller) { |
| 383 | case SPI_CONTROLLER_ICH7: |
| 384 | case SPI_CONTROLLER_VIA: |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 385 | REGWRITE16(ICH7_REG_PREOP, preop); |
| 386 | REGWRITE16(ICH7_REG_OPTYPE, optype); |
| 387 | REGWRITE32(ICH7_REG_OPMENU, opmenu[0]); |
| 388 | REGWRITE32(ICH7_REG_OPMENU + 4, opmenu[1]); |
| 389 | break; |
hailfinger | 4016746 | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 390 | case SPI_CONTROLLER_ICH9: |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 391 | REGWRITE16(ICH9_REG_PREOP, preop); |
| 392 | REGWRITE16(ICH9_REG_OPTYPE, optype); |
| 393 | REGWRITE32(ICH9_REG_OPMENU, opmenu[0]); |
| 394 | REGWRITE32(ICH9_REG_OPMENU + 4, opmenu[1]); |
| 395 | break; |
| 396 | default: |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 397 | msg_perr("%s: unsupported chipset\n", __func__); |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 398 | return -1; |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 399 | } |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
hailfinger | b767c12 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 404 | /* |
| 405 | * Try to set BBAR (BIOS Base Address Register), but read back the value in case |
| 406 | * it didn't stick. |
| 407 | */ |
| 408 | void ich_set_bbar(uint32_t minaddr) |
| 409 | { |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 410 | #define BBAR_MASK 0x00ffff00 |
| 411 | minaddr &= BBAR_MASK; |
hailfinger | b767c12 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 412 | switch (spi_controller) { |
| 413 | case SPI_CONTROLLER_ICH7: |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 414 | case SPI_CONTROLLER_VIA: |
| 415 | ichspi_bbar = mmio_readl(ich_spibar + 0x50) & ~BBAR_MASK; |
| 416 | if (ichspi_bbar) |
| 417 | msg_pdbg("Reserved bits in BBAR not zero: 0x%04x", |
| 418 | ichspi_bbar); |
| 419 | ichspi_bbar |= minaddr; |
| 420 | mmio_writel(ichspi_bbar, ich_spibar + 0x50); |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 421 | ichspi_bbar = mmio_readl(ich_spibar + 0x50); |
hailfinger | b767c12 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 422 | /* We don't have any option except complaining. */ |
| 423 | if (ichspi_bbar != minaddr) |
| 424 | msg_perr("Setting BBAR failed!\n"); |
| 425 | break; |
| 426 | case SPI_CONTROLLER_ICH9: |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 427 | ichspi_bbar = mmio_readl(ich_spibar + 0xA0) & ~BBAR_MASK; |
| 428 | if (ichspi_bbar) |
| 429 | msg_pdbg("Reserved bits in BBAR not zero: 0x%04x", |
| 430 | ichspi_bbar); |
| 431 | ichspi_bbar |= minaddr; |
| 432 | mmio_writel(ichspi_bbar, ich_spibar + 0xA0); |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 433 | ichspi_bbar = mmio_readl(ich_spibar + 0xA0); |
hailfinger | b767c12 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 434 | /* We don't have any option except complaining. */ |
| 435 | if (ichspi_bbar != minaddr) |
| 436 | msg_perr("Setting BBAR failed!\n"); |
| 437 | break; |
| 438 | default: |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 439 | msg_perr("Unknown chipset for BBAR setting!\n"); |
hailfinger | b767c12 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 440 | break; |
| 441 | } |
| 442 | } |
| 443 | |
stuge | 7a65155 | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 444 | /* This function generates OPCODES from or programs OPCODES to ICH according to |
| 445 | * the chipset's SPI configuration lock. |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 446 | * |
stuge | 7a65155 | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 447 | * It should be called before ICH sends any spi command. |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 448 | */ |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 449 | static int ich_init_opcodes(void) |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 450 | { |
| 451 | int rc = 0; |
| 452 | OPCODES *curopcodes_done; |
| 453 | |
| 454 | if (curopcodes) |
| 455 | return 0; |
| 456 | |
| 457 | if (ichspi_lock) { |
hailfinger | b767c12 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 458 | msg_pdbg("Reading OPCODES... "); |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 459 | curopcodes_done = &O_EXISTING; |
stuge | 7a65155 | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 460 | rc = generate_opcodes(curopcodes_done); |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 461 | } else { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 462 | msg_pdbg("Programming OPCODES... "); |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 463 | curopcodes_done = &O_ST_M25P; |
| 464 | rc = program_opcodes(curopcodes_done); |
hailfinger | b767c12 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 465 | /* Technically not part of opcode init, but it allows opcodes |
| 466 | * to run without transaction errors by setting the lowest |
| 467 | * allowed address to zero. |
| 468 | */ |
| 469 | ich_set_bbar(0); |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | if (rc) { |
| 473 | curopcodes = NULL; |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 474 | msg_perr("failed\n"); |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 475 | return 1; |
| 476 | } else { |
| 477 | curopcodes = curopcodes_done; |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 478 | msg_pdbg("done\n"); |
stepan | e1a13b9 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 479 | return 0; |
| 480 | } |
| 481 | } |
| 482 | |
stepan | 82c65bd | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 483 | static int ich7_run_opcode(OPCODE op, uint32_t offset, |
ruik | 9bc51c0 | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 484 | uint8_t datalength, uint8_t * data, int maxdata) |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 485 | { |
| 486 | int write_cmd = 0; |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 487 | int timeout; |
stuge | 4f5b6e4 | 2008-06-29 01:30:41 +0000 | [diff] [blame] | 488 | uint32_t temp32 = 0; |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 489 | uint16_t temp16; |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 490 | uint32_t a; |
stepan | 82c65bd | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 491 | uint64_t opmenu; |
| 492 | int opcode_index; |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 493 | |
| 494 | /* Is it a write command? */ |
| 495 | if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) |
| 496 | || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) { |
| 497 | write_cmd = 1; |
| 498 | } |
| 499 | |
| 500 | /* Programm Offset in Flash into FADDR */ |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 501 | REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */ |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 502 | |
| 503 | /* Program data into FDATA0 to N */ |
| 504 | if (write_cmd && (datalength != 0)) { |
| 505 | temp32 = 0; |
| 506 | for (a = 0; a < datalength; a++) { |
| 507 | if ((a % 4) == 0) { |
| 508 | temp32 = 0; |
| 509 | } |
| 510 | |
| 511 | temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8); |
| 512 | |
| 513 | if ((a % 4) == 3) { |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 514 | REGWRITE32(ICH7_REG_SPID0 + (a - (a % 4)), |
| 515 | temp32); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | if (((a - 1) % 4) != 3) { |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 519 | REGWRITE32(ICH7_REG_SPID0 + |
| 520 | ((a - 1) - ((a - 1) % 4)), temp32); |
| 521 | } |
| 522 | |
| 523 | } |
| 524 | |
| 525 | /* Assemble SPIS */ |
| 526 | temp16 = 0; |
| 527 | /* clear error status registers */ |
| 528 | temp16 |= (SPIS_CDS + SPIS_FCERR); |
| 529 | REGWRITE16(ICH7_REG_SPIS, temp16); |
| 530 | |
| 531 | /* Assemble SPIC */ |
| 532 | temp16 = 0; |
| 533 | |
| 534 | if (datalength != 0) { |
| 535 | temp16 |= SPIC_DS; |
ruik | 9bc51c0 | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 536 | temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8; |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /* Select opcode */ |
stepan | 82c65bd | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 540 | opmenu = REGREAD32(ICH7_REG_OPMENU); |
| 541 | opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32; |
| 542 | |
uwe | 5e931bc | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 543 | for (opcode_index = 0; opcode_index < 8; opcode_index++) { |
| 544 | if ((opmenu & 0xff) == op.opcode) { |
stepan | 82c65bd | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 545 | break; |
| 546 | } |
| 547 | opmenu >>= 8; |
| 548 | } |
| 549 | if (opcode_index == 8) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 550 | msg_pdbg("Opcode %x not found.\n", op.opcode); |
stepan | 82c65bd | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 551 | return 1; |
| 552 | } |
| 553 | temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4; |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 554 | |
| 555 | /* Handle Atomic */ |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 556 | switch (op.atomic) { |
| 557 | case 2: |
| 558 | /* Select second preop. */ |
| 559 | temp16 |= SPIC_SPOP; |
| 560 | /* And fall through. */ |
| 561 | case 1: |
| 562 | /* Atomic command (preop+op) */ |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 563 | temp16 |= SPIC_ACS; |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 564 | break; |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | /* Start */ |
| 568 | temp16 |= SPIC_SCGO; |
| 569 | |
| 570 | /* write it */ |
| 571 | REGWRITE16(ICH7_REG_SPIC, temp16); |
| 572 | |
Louis Yung-Chieh Lo | d2129f1 | 2011-03-16 14:53:12 +0800 | [diff] [blame] | 573 | /* Original timeout is 60 minutes, which is too excessive. |
| 574 | * Reduce to 30 secs for chip full erase (around 10 secs). |
| 575 | * We also exit the loop if the error bit is set. |
| 576 | */ |
| 577 | timeout = 100 * 1000 * 30; |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 578 | while (((REGREAD16(ICH7_REG_SPIS) & SPIS_CDS) == 0) && --timeout) { |
hailfinger | e5829f6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 579 | programmer_delay(10); |
Louis Yung-Chieh Lo | d2129f1 | 2011-03-16 14:53:12 +0800 | [diff] [blame] | 580 | if (REGREAD16(ICH7_REG_SPIS) & SPIS_FCERR) |
| 581 | break; /* Transaction error */ |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 582 | } |
| 583 | if (!timeout) { |
Louis Yung-Chieh Lo | d2129f1 | 2011-03-16 14:53:12 +0800 | [diff] [blame] | 584 | msg_perr("ICH7 CDS timeout\n"); |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 585 | } |
| 586 | |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 587 | /* FIXME: make sure we do not needlessly cause transaction errors. */ |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 588 | if ((REGREAD16(ICH7_REG_SPIS) & SPIS_FCERR) != 0) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 589 | msg_pdbg("Transaction error!\n"); |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 590 | return 1; |
| 591 | } |
| 592 | |
| 593 | if ((!write_cmd) && (datalength != 0)) { |
| 594 | for (a = 0; a < datalength; a++) { |
| 595 | if ((a % 4) == 0) { |
| 596 | temp32 = REGREAD32(ICH7_REG_SPID0 + (a)); |
| 597 | } |
| 598 | |
| 599 | data[a] = |
| 600 | (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8))) |
| 601 | >> ((a % 4) * 8); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
stepan | 82c65bd | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 608 | static int ich9_run_opcode(OPCODE op, uint32_t offset, |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 609 | uint8_t datalength, uint8_t * data) |
| 610 | { |
| 611 | int write_cmd = 0; |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 612 | int timeout; |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 613 | uint32_t temp32; |
| 614 | uint32_t a; |
stepan | 82c65bd | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 615 | uint64_t opmenu; |
| 616 | int opcode_index; |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 617 | |
| 618 | /* Is it a write command? */ |
| 619 | if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) |
| 620 | || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) { |
| 621 | write_cmd = 1; |
| 622 | } |
| 623 | |
| 624 | /* Programm Offset in Flash into FADDR */ |
| 625 | REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */ |
| 626 | |
| 627 | /* Program data into FDATA0 to N */ |
| 628 | if (write_cmd && (datalength != 0)) { |
| 629 | temp32 = 0; |
| 630 | for (a = 0; a < datalength; a++) { |
| 631 | if ((a % 4) == 0) { |
| 632 | temp32 = 0; |
| 633 | } |
| 634 | |
| 635 | temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8); |
| 636 | |
| 637 | if ((a % 4) == 3) { |
| 638 | REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)), |
| 639 | temp32); |
| 640 | } |
| 641 | } |
| 642 | if (((a - 1) % 4) != 3) { |
| 643 | REGWRITE32(ICH9_REG_FDATA0 + |
| 644 | ((a - 1) - ((a - 1) % 4)), temp32); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 645 | } |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* Assemble SSFS + SSFC */ |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 649 | /* keep reserved bits (23-19,7,0) */ |
| 650 | temp32 = REGREAD32(ICH9_REG_SSFS); |
| 651 | temp32 &= 0xF8008100; |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 652 | |
| 653 | /* clear error status registers */ |
| 654 | temp32 |= (SSFS_CDS + SSFS_FCERR); |
uwe | 3a3ab2f | 2010-03-25 23:18:41 +0000 | [diff] [blame] | 655 | /* Use 20 MHz */ |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 656 | temp32 |= SSFC_SCF_20MHZ; |
| 657 | |
| 658 | if (datalength != 0) { |
| 659 | uint32_t datatemp; |
| 660 | temp32 |= SSFC_DS; |
| 661 | datatemp = ((uint32_t) ((datalength - 1) & 0x3f)) << (8 + 8); |
| 662 | temp32 |= datatemp; |
| 663 | } |
| 664 | |
| 665 | /* Select opcode */ |
stepan | 82c65bd | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 666 | opmenu = REGREAD32(ICH9_REG_OPMENU); |
| 667 | opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32; |
| 668 | |
uwe | 5e931bc | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 669 | for (opcode_index = 0; opcode_index < 8; opcode_index++) { |
| 670 | if ((opmenu & 0xff) == op.opcode) { |
stepan | 82c65bd | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 671 | break; |
| 672 | } |
| 673 | opmenu >>= 8; |
| 674 | } |
| 675 | if (opcode_index == 8) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 676 | msg_pdbg("Opcode %x not found.\n", op.opcode); |
stepan | 82c65bd | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 677 | return 1; |
| 678 | } |
| 679 | temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 680 | |
| 681 | /* Handle Atomic */ |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 682 | switch (op.atomic) { |
| 683 | case 2: |
| 684 | /* Select second preop. */ |
| 685 | temp32 |= SSFC_SPOP; |
| 686 | /* And fall through. */ |
| 687 | case 1: |
| 688 | /* Atomic command (preop+op) */ |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 689 | temp32 |= SSFC_ACS; |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 690 | break; |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /* Start */ |
| 694 | temp32 |= SSFC_SCGO; |
| 695 | |
| 696 | /* write it */ |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 697 | REGWRITE32(ICH9_REG_SSFS, temp32); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 698 | |
| 699 | /*wait for cycle complete */ |
hailfinger | 7d94ef5 | 2009-05-09 07:24:23 +0000 | [diff] [blame] | 700 | timeout = 100 * 1000 * 60; // 60s is a looong timeout. |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 701 | while (((REGREAD32(ICH9_REG_SSFS) & SSFS_CDS) == 0) && --timeout) { |
hailfinger | e5829f6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 702 | programmer_delay(10); |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 703 | } |
| 704 | if (!timeout) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 705 | msg_perr("timeout\n"); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 706 | } |
| 707 | |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 708 | /* FIXME make sure we do not needlessly cause transaction errors. */ |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 709 | if ((REGREAD32(ICH9_REG_SSFS) & SSFS_FCERR) != 0) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 710 | msg_pdbg("Transaction error!\n"); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 711 | return 1; |
| 712 | } |
| 713 | |
| 714 | if ((!write_cmd) && (datalength != 0)) { |
| 715 | for (a = 0; a < datalength; a++) { |
| 716 | if ((a % 4) == 0) { |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 717 | temp32 = REGREAD32(ICH9_REG_FDATA0 + (a)); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | data[a] = |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 721 | (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8))) |
| 722 | >> ((a % 4) * 8); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | |
stepan | 82c65bd | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 729 | static int run_opcode(OPCODE op, uint32_t offset, |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 730 | uint8_t datalength, uint8_t * data) |
| 731 | { |
hailfinger | 4016746 | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 732 | switch (spi_controller) { |
| 733 | case SPI_CONTROLLER_VIA: |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 734 | if (datalength > 16) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 735 | msg_perr("%s: Internal command size error for " |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 736 | "opcode 0x%02x, got datalength=%i, want <=16\n", |
| 737 | __func__, op.opcode, datalength); |
hailfinger | 9c290a7 | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 738 | return SPI_INVALID_LENGTH; |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 739 | } |
stepan | 82c65bd | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 740 | return ich7_run_opcode(op, offset, datalength, data, 16); |
hailfinger | 4016746 | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 741 | case SPI_CONTROLLER_ICH7: |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 742 | if (datalength > 64) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 743 | msg_perr("%s: Internal command size error for " |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 744 | "opcode 0x%02x, got datalength=%i, want <=16\n", |
| 745 | __func__, op.opcode, datalength); |
hailfinger | 9c290a7 | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 746 | return SPI_INVALID_LENGTH; |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 747 | } |
stepan | 82c65bd | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 748 | return ich7_run_opcode(op, offset, datalength, data, 64); |
hailfinger | 4016746 | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 749 | case SPI_CONTROLLER_ICH9: |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 750 | if (datalength > 64) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 751 | msg_perr("%s: Internal command size error for " |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 752 | "opcode 0x%02x, got datalength=%i, want <=16\n", |
| 753 | __func__, op.opcode, datalength); |
hailfinger | 9c290a7 | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 754 | return SPI_INVALID_LENGTH; |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 755 | } |
stepan | 82c65bd | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 756 | return ich9_run_opcode(op, offset, datalength, data); |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 757 | default: |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 758 | msg_perr("%s: unsupported chipset\n", __func__); |
stepan | 3bdf618 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 759 | } |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 760 | |
| 761 | /* If we ever get here, something really weird happened */ |
| 762 | return -1; |
| 763 | } |
| 764 | |
hailfinger | 0f08b7a | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 765 | int ich_spi_read(struct flashchip *flash, uint8_t * buf, int start, int len) |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 766 | { |
ruik | 9bc51c0 | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 767 | int maxdata = 64; |
| 768 | |
hailfinger | 7b41474 | 2009-06-13 12:04:03 +0000 | [diff] [blame] | 769 | if (spi_controller == SPI_CONTROLLER_VIA) |
ruik | 9bc51c0 | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 770 | maxdata = 16; |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 771 | |
hailfinger | 0f08b7a | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 772 | return spi_read_chunked(flash, buf, start, len, maxdata); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 773 | } |
| 774 | |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 775 | int ich_spi_write_256(struct flashchip *flash, uint8_t * buf, int start, int len) |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 776 | { |
ruik | 9bc51c0 | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 777 | int maxdata = 64; |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 778 | |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 779 | if (spi_controller == SPI_CONTROLLER_VIA) |
| 780 | maxdata = 16; |
| 781 | |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 782 | return spi_write_chunked(flash, buf, start, len, maxdata); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 783 | } |
| 784 | |
hailfinger | 68002c2 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 785 | int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
stepan | 8f46dd6 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 786 | const unsigned char *writearr, unsigned char *readarr) |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 787 | { |
hailfinger | 9c290a7 | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 788 | int result; |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 789 | int opcode_index = -1; |
| 790 | const unsigned char cmd = *writearr; |
| 791 | OPCODE *opcode; |
| 792 | uint32_t addr = 0; |
| 793 | uint8_t *data; |
| 794 | int count; |
| 795 | |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 796 | /* find cmd in opcodes-table */ |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 797 | opcode_index = find_opcode(curopcodes, cmd); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 798 | if (opcode_index == -1) { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 799 | if (!ichspi_lock) |
| 800 | opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt); |
| 801 | if (opcode_index == -1) { |
| 802 | msg_pdbg("Invalid OPCODE 0x%02x\n", cmd); |
| 803 | return SPI_INVALID_OPCODE; |
| 804 | } |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | opcode = &(curopcodes->opcode[opcode_index]); |
| 808 | |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 809 | /* The following valid writecnt/readcnt combinations exist: |
| 810 | * writecnt = 4, readcnt >= 0 |
| 811 | * writecnt = 1, readcnt >= 0 |
| 812 | * writecnt >= 4, readcnt = 0 |
| 813 | * writecnt >= 1, readcnt = 0 |
| 814 | * writecnt >= 1 is guaranteed for all commands. |
| 815 | */ |
| 816 | if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) && |
| 817 | (writecnt != 4)) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 818 | msg_perr("%s: Internal command size error for opcode " |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 819 | "0x%02x, got writecnt=%i, want =4\n", __func__, cmd, |
| 820 | writecnt); |
| 821 | return SPI_INVALID_LENGTH; |
| 822 | } |
| 823 | if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) && |
| 824 | (writecnt != 1)) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 825 | msg_perr("%s: Internal command size error for opcode " |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 826 | "0x%02x, got writecnt=%i, want =1\n", __func__, cmd, |
| 827 | writecnt); |
| 828 | return SPI_INVALID_LENGTH; |
| 829 | } |
| 830 | if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) && |
| 831 | (writecnt < 4)) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 832 | msg_perr("%s: Internal command size error for opcode " |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 833 | "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd, |
| 834 | writecnt); |
| 835 | return SPI_INVALID_LENGTH; |
| 836 | } |
| 837 | if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) || |
| 838 | (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) && |
| 839 | (readcnt)) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 840 | msg_perr("%s: Internal command size error for opcode " |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 841 | "0x%02x, got readcnt=%i, want =0\n", __func__, cmd, |
| 842 | readcnt); |
| 843 | return SPI_INVALID_LENGTH; |
| 844 | } |
| 845 | |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 846 | /* if opcode-type requires an address */ |
| 847 | if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS || |
| 848 | opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) { |
stepan | 8f46dd6 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 849 | addr = (writearr[1] << 16) | |
| 850 | (writearr[2] << 8) | (writearr[3] << 0); |
hailfinger | b767c12 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 851 | switch (spi_controller) { |
| 852 | case SPI_CONTROLLER_ICH7: |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 853 | case SPI_CONTROLLER_VIA: |
hailfinger | b767c12 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 854 | case SPI_CONTROLLER_ICH9: |
| 855 | if (addr < ichspi_bbar) { |
| 856 | msg_perr("%s: Address 0x%06x below allowed " |
| 857 | "range 0x%06x-0xffffff\n", __func__, |
| 858 | addr, ichspi_bbar); |
| 859 | return SPI_INVALID_ADDRESS; |
| 860 | } |
| 861 | break; |
| 862 | default: |
| 863 | break; |
| 864 | } |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 865 | } |
stepan | 8f46dd6 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 866 | |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 867 | /* translate read/write array/count */ |
| 868 | if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) { |
stepan | 8f46dd6 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 869 | data = (uint8_t *) (writearr + 1); |
| 870 | count = writecnt - 1; |
| 871 | } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) { |
| 872 | data = (uint8_t *) (writearr + 4); |
| 873 | count = writecnt - 4; |
| 874 | } else { |
| 875 | data = (uint8_t *) readarr; |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 876 | count = readcnt; |
| 877 | } |
stepan | 8f46dd6 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 878 | |
hailfinger | 9c290a7 | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 879 | result = run_opcode(*opcode, addr, count, data); |
| 880 | if (result) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 881 | msg_pdbg("run OPCODE 0x%02x failed\n", opcode->opcode); |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 882 | } |
| 883 | |
hailfinger | 9c290a7 | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 884 | return result; |
hailfinger | 82e7ddb | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 885 | } |
hailfinger | 948b81f | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 886 | |
hailfinger | bb09211 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 887 | int ich_spi_send_multicommand(struct spi_command *cmds) |
hailfinger | 948b81f | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 888 | { |
| 889 | int ret = 0; |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 890 | int i; |
hailfinger | bb09211 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 891 | int oppos, preoppos; |
| 892 | for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) { |
hailfinger | bb09211 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 893 | if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) { |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 894 | /* Next command is valid. */ |
hailfinger | bb09211 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 895 | preoppos = find_preop(curopcodes, cmds->writearr[0]); |
| 896 | oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]); |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 897 | if ((oppos == -1) && (preoppos != -1)) { |
| 898 | /* Current command is listed as preopcode in |
| 899 | * ICH struct OPCODES, but next command is not |
| 900 | * listed as opcode in that struct. |
| 901 | * Check for command sanity, then |
| 902 | * try to reprogram the ICH opcode list. |
| 903 | */ |
| 904 | if (find_preop(curopcodes, |
| 905 | (cmds + 1)->writearr[0]) != -1) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 906 | msg_perr("%s: Two subsequent " |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 907 | "preopcodes 0x%02x and 0x%02x, " |
| 908 | "ignoring the first.\n", |
| 909 | __func__, cmds->writearr[0], |
| 910 | (cmds + 1)->writearr[0]); |
| 911 | continue; |
| 912 | } |
| 913 | /* If the chipset is locked down, we'll fail |
| 914 | * during execution of the next command anyway. |
| 915 | * No need to bother with fixups. |
| 916 | */ |
| 917 | if (!ichspi_lock) { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 918 | oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt); |
| 919 | if (oppos == -1) |
| 920 | continue; |
| 921 | curopcodes->opcode[oppos].atomic = preoppos + 1; |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 922 | continue; |
| 923 | } |
| 924 | } |
| 925 | if ((oppos != -1) && (preoppos != -1)) { |
| 926 | /* Current command is listed as preopcode in |
| 927 | * ICH struct OPCODES and next command is listed |
| 928 | * as opcode in that struct. Match them up. |
| 929 | */ |
| 930 | curopcodes->opcode[oppos].atomic = preoppos + 1; |
hailfinger | bb09211 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 931 | continue; |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 932 | } |
| 933 | /* If none of the above if-statements about oppos or |
| 934 | * preoppos matched, this is a normal opcode. |
| 935 | */ |
| 936 | } |
hailfinger | bb09211 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 937 | ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt, |
| 938 | cmds->writearr, cmds->readarr); |
hailfinger | 82e3249 | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 939 | /* Reset the type of all opcodes to non-atomic. */ |
| 940 | for (i = 0; i < 8; i++) |
| 941 | curopcodes->opcode[i].atomic = 0; |
hailfinger | 948b81f | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 942 | } |
| 943 | return ret; |
| 944 | } |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 945 | |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 946 | #define ICH_BMWAG(x) ((x >> 24) & 0xff) |
| 947 | #define ICH_BMRAG(x) ((x >> 16) & 0xff) |
| 948 | #define ICH_BRWA(x) ((x >> 8) & 0xff) |
| 949 | #define ICH_BRRA(x) ((x >> 0) & 0xff) |
| 950 | |
| 951 | #define ICH_FREG_BASE(x) ((x >> 0) & 0x1fff) |
| 952 | #define ICH_FREG_LIMIT(x) ((x >> 16) & 0x1fff) |
| 953 | |
| 954 | static void do_ich9_spi_frap(uint32_t frap, int i) |
| 955 | { |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 956 | static const char *const access_names[4] = { |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 957 | "locked", "read-only", "write-only", "read-write" |
| 958 | }; |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 959 | static const char *const region_names[5] = { |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 960 | "Flash Descriptor", "BIOS", "Management Engine", |
| 961 | "Gigabit Ethernet", "Platform Data" |
| 962 | }; |
| 963 | uint32_t base, limit; |
| 964 | int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) | |
| 965 | (((ICH_BRRA(frap) >> i) & 1) << 0); |
| 966 | int offset = 0x54 + i * 4; |
| 967 | uint32_t freg = mmio_readl(ich_spibar + offset); |
| 968 | |
| 969 | msg_pdbg("0x%02X: 0x%08x (FREG%i: %s)\n", |
| 970 | offset, freg, i, region_names[i]); |
| 971 | |
| 972 | base = ICH_FREG_BASE(freg); |
| 973 | limit = ICH_FREG_LIMIT(freg); |
| 974 | if (base == 0x1fff && limit == 0) { |
| 975 | /* this FREG is disabled */ |
| 976 | msg_pdbg("%s region is unused.\n", region_names[i]); |
| 977 | return; |
| 978 | } |
| 979 | |
| 980 | msg_pdbg("0x%08x-0x%08x is %s\n", |
| 981 | (base << 12), (limit << 12) | 0x0fff, |
| 982 | access_names[rwperms]); |
| 983 | } |
| 984 | |
| 985 | int ich_init_spi(struct pci_dev *dev, uint32_t base, void *rcrb, |
| 986 | int ich_generation) |
| 987 | { |
| 988 | int i; |
| 989 | uint8_t old, new; |
| 990 | uint16_t spibar_offset, tmp2; |
| 991 | uint32_t tmp; |
| 992 | |
| 993 | buses_supported |= CHIP_BUSTYPE_SPI; |
| 994 | switch (ich_generation) { |
| 995 | case 7: |
| 996 | spi_controller = SPI_CONTROLLER_ICH7; |
| 997 | spibar_offset = 0x3020; |
| 998 | break; |
| 999 | case 8: |
| 1000 | spi_controller = SPI_CONTROLLER_ICH9; |
| 1001 | spibar_offset = 0x3020; |
| 1002 | break; |
| 1003 | case 9: |
| 1004 | case 10: |
| 1005 | default: /* Future version might behave the same */ |
| 1006 | spi_controller = SPI_CONTROLLER_ICH9; |
| 1007 | spibar_offset = 0x3800; |
| 1008 | break; |
| 1009 | } |
| 1010 | |
| 1011 | /* SPIBAR is at RCRB+0x3020 for ICH[78] and RCRB+0x3800 for ICH9. */ |
| 1012 | msg_pdbg("SPIBAR = 0x%x + 0x%04x\n", base, spibar_offset); |
| 1013 | |
| 1014 | /* Assign Virtual Address */ |
| 1015 | ich_spibar = rcrb + spibar_offset; |
| 1016 | |
| 1017 | switch (spi_controller) { |
| 1018 | case SPI_CONTROLLER_ICH7: |
| 1019 | msg_pdbg("0x00: 0x%04x (SPIS)\n", |
| 1020 | mmio_readw(ich_spibar + 0)); |
| 1021 | msg_pdbg("0x02: 0x%04x (SPIC)\n", |
| 1022 | mmio_readw(ich_spibar + 2)); |
| 1023 | msg_pdbg("0x04: 0x%08x (SPIA)\n", |
| 1024 | mmio_readl(ich_spibar + 4)); |
| 1025 | for (i = 0; i < 8; i++) { |
| 1026 | int offs; |
| 1027 | offs = 8 + (i * 8); |
| 1028 | msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs, |
| 1029 | mmio_readl(ich_spibar + offs), i); |
| 1030 | msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4, |
| 1031 | mmio_readl(ich_spibar + offs + 4), i); |
| 1032 | } |
| 1033 | ichspi_bbar = mmio_readl(ich_spibar + 0x50); |
| 1034 | msg_pdbg("0x50: 0x%08x (BBAR)\n", |
| 1035 | ichspi_bbar); |
| 1036 | msg_pdbg("0x54: 0x%04x (PREOP)\n", |
| 1037 | mmio_readw(ich_spibar + 0x54)); |
| 1038 | msg_pdbg("0x56: 0x%04x (OPTYPE)\n", |
| 1039 | mmio_readw(ich_spibar + 0x56)); |
| 1040 | msg_pdbg("0x58: 0x%08x (OPMENU)\n", |
| 1041 | mmio_readl(ich_spibar + 0x58)); |
| 1042 | msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", |
| 1043 | mmio_readl(ich_spibar + 0x5c)); |
| 1044 | for (i = 0; i < 4; i++) { |
| 1045 | int offs; |
| 1046 | offs = 0x60 + (i * 4); |
| 1047 | msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs, |
| 1048 | mmio_readl(ich_spibar + offs), i); |
| 1049 | } |
| 1050 | msg_pdbg("\n"); |
| 1051 | if (mmio_readw(ich_spibar) & (1 << 15)) { |
Louis Yung-Chieh Lo | c13054a | 2010-11-05 10:10:34 +0800 | [diff] [blame] | 1052 | msg_perr("WARNING: SPI Configuration Lockdown activated.\n"); |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1053 | ichspi_lock = 1; |
| 1054 | } |
| 1055 | ich_init_opcodes(); |
| 1056 | break; |
| 1057 | case SPI_CONTROLLER_ICH9: |
| 1058 | tmp2 = mmio_readw(ich_spibar + 4); |
| 1059 | msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2); |
| 1060 | msg_pdbg("FLOCKDN %i, ", (tmp2 >> 15 & 1)); |
| 1061 | msg_pdbg("FDV %i, ", (tmp2 >> 14) & 1); |
| 1062 | msg_pdbg("FDOPSS %i, ", (tmp2 >> 13) & 1); |
| 1063 | msg_pdbg("SCIP %i, ", (tmp2 >> 5) & 1); |
| 1064 | msg_pdbg("BERASE %i, ", (tmp2 >> 3) & 3); |
| 1065 | msg_pdbg("AEL %i, ", (tmp2 >> 2) & 1); |
| 1066 | msg_pdbg("FCERR %i, ", (tmp2 >> 1) & 1); |
| 1067 | msg_pdbg("FDONE %i\n", (tmp2 >> 0) & 1); |
| 1068 | |
| 1069 | tmp = mmio_readl(ich_spibar + 0x50); |
| 1070 | msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp); |
| 1071 | msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp)); |
| 1072 | msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp)); |
| 1073 | msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp)); |
| 1074 | msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp)); |
| 1075 | |
| 1076 | /* print out the FREGx registers along with FRAP access bits */ |
| 1077 | for(i = 0; i < 5; i++) |
| 1078 | do_ich9_spi_frap(tmp, i); |
| 1079 | |
| 1080 | msg_pdbg("0x74: 0x%08x (PR0)\n", |
| 1081 | mmio_readl(ich_spibar + 0x74)); |
| 1082 | msg_pdbg("0x78: 0x%08x (PR1)\n", |
| 1083 | mmio_readl(ich_spibar + 0x78)); |
| 1084 | msg_pdbg("0x7C: 0x%08x (PR2)\n", |
| 1085 | mmio_readl(ich_spibar + 0x7C)); |
| 1086 | msg_pdbg("0x80: 0x%08x (PR3)\n", |
| 1087 | mmio_readl(ich_spibar + 0x80)); |
| 1088 | msg_pdbg("0x84: 0x%08x (PR4)\n", |
| 1089 | mmio_readl(ich_spibar + 0x84)); |
| 1090 | msg_pdbg("0x90: 0x%08x (SSFS, SSFC)\n", |
| 1091 | mmio_readl(ich_spibar + 0x90)); |
| 1092 | msg_pdbg("0x94: 0x%04x (PREOP)\n", |
| 1093 | mmio_readw(ich_spibar + 0x94)); |
| 1094 | msg_pdbg("0x96: 0x%04x (OPTYPE)\n", |
| 1095 | mmio_readw(ich_spibar + 0x96)); |
| 1096 | msg_pdbg("0x98: 0x%08x (OPMENU)\n", |
| 1097 | mmio_readl(ich_spibar + 0x98)); |
| 1098 | msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n", |
| 1099 | mmio_readl(ich_spibar + 0x9C)); |
| 1100 | ichspi_bbar = mmio_readl(ich_spibar + 0xA0); |
| 1101 | msg_pdbg("0xA0: 0x%08x (BBAR)\n", |
| 1102 | ichspi_bbar); |
| 1103 | msg_pdbg("0xB0: 0x%08x (FDOC)\n", |
| 1104 | mmio_readl(ich_spibar + 0xB0)); |
| 1105 | if (tmp2 & (1 << 15)) { |
Louis Yung-Chieh Lo | c13054a | 2010-11-05 10:10:34 +0800 | [diff] [blame] | 1106 | msg_perr("WARNING: SPI Configuration Lockdown activated.\n"); |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1107 | ichspi_lock = 1; |
| 1108 | } |
| 1109 | ich_init_opcodes(); |
| 1110 | break; |
| 1111 | default: |
| 1112 | /* Nothing */ |
| 1113 | break; |
| 1114 | } |
| 1115 | |
| 1116 | old = pci_read_byte(dev, 0xdc); |
| 1117 | msg_pdbg("SPI Read Configuration: "); |
| 1118 | new = (old >> 2) & 0x3; |
| 1119 | switch (new) { |
| 1120 | case 0: |
| 1121 | case 1: |
| 1122 | case 2: |
| 1123 | msg_pdbg("prefetching %sabled, caching %sabled, ", |
| 1124 | (new & 0x2) ? "en" : "dis", |
| 1125 | (new & 0x1) ? "dis" : "en"); |
| 1126 | break; |
| 1127 | default: |
| 1128 | msg_pdbg("invalid prefetching/caching settings, "); |
| 1129 | break; |
| 1130 | } |
| 1131 | return 0; |
| 1132 | } |
| 1133 | |
| 1134 | int via_init_spi(struct pci_dev *dev) |
| 1135 | { |
| 1136 | uint32_t mmio_base; |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 1137 | int i; |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1138 | |
| 1139 | mmio_base = (pci_read_long(dev, 0xbc)) << 8; |
| 1140 | msg_pdbg("MMIO base at = 0x%x\n", mmio_base); |
| 1141 | ich_spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70); |
| 1142 | |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1143 | /* Not sure if it speaks all these bus protocols. */ |
| 1144 | buses_supported = CHIP_BUSTYPE_LPC | CHIP_BUSTYPE_FWH | CHIP_BUSTYPE_SPI; |
| 1145 | spi_controller = SPI_CONTROLLER_VIA; |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 1146 | |
| 1147 | msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0)); |
| 1148 | msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2)); |
| 1149 | msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4)); |
| 1150 | for (i = 0; i < 2; i++) { |
| 1151 | int offs; |
| 1152 | offs = 8 + (i * 8); |
| 1153 | msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs, |
| 1154 | mmio_readl(ich_spibar + offs), i); |
| 1155 | msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4, |
| 1156 | mmio_readl(ich_spibar + offs + 4), i); |
| 1157 | } |
| 1158 | ichspi_bbar = mmio_readl(ich_spibar + 0x50); |
| 1159 | msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar); |
| 1160 | msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54)); |
| 1161 | msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56)); |
| 1162 | msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58)); |
| 1163 | msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c)); |
| 1164 | for (i = 0; i < 3; i++) { |
| 1165 | int offs; |
| 1166 | offs = 0x60 + (i * 4); |
| 1167 | msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs, |
| 1168 | mmio_readl(ich_spibar + offs), i); |
| 1169 | } |
| 1170 | msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n", |
| 1171 | mmio_readw(ich_spibar + 0x6c)); |
| 1172 | if (mmio_readw(ich_spibar) & (1 << 15)) { |
| 1173 | msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n"); |
| 1174 | ichspi_lock = 1; |
| 1175 | } |
| 1176 | |
David Hendricks | 82fd8ae | 2010-08-04 14:34:54 -0700 | [diff] [blame] | 1177 | ich_init_opcodes(); |
| 1178 | |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1182 | #endif |