Dominik Geyer | b46acba | 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> |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 7 | * Copyright (C) 2008 coresystems GmbH <info@coresystems.de> |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 8 | * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger |
Dominik Geyer | b46acba | 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 |
Dominik Geyer | b46acba | 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 |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame^] | 33 | * ... and many more SPI flash devices |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 34 | * |
| 35 | */ |
| 36 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 37 | #if defined(__i386__) || defined(__x86_64__) |
| 38 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 39 | #include <string.h> |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 40 | #include "flash.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 41 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 42 | #include "programmer.h" |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 43 | #include "spi.h" |
| 44 | |
Stefan Reinauer | a9424d5 | 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 */ |
Dominik Geyer | b46acba | 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 |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 54 | |
| 55 | #define ICH9_REG_SSFC 0x91 /* 24 Bits */ |
Dominik Geyer | b46acba | 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 |
Stefan Reinauer | a9424d5 | 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 */ |
Dominik Geyer | b46acba | 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 | |
Stefan Reinauer | a9424d5 | 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 | |
Rudolf Marek | 3fdbccf | 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 | |
Stefan Reinauer | a9424d5 | 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 |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 97 | #define SPIC_DS 0x4000 |
Stefan Reinauer | a9424d5 | 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 | |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 105 | /* ICH SPI configuration lock-down. May be set during chipset enabling. */ |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 106 | static int ichspi_lock = 0; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 107 | |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 108 | uint32_t ichspi_bbar = 0; |
| 109 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 110 | static void *ich_spibar = NULL; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 111 | |
Dominik Geyer | b46acba | 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 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 118 | /* Suggested opcode definition: |
Dominik Geyer | b46acba | 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 |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 128 | * OP 6: chip private (read JEDEC id) |
Dominik Geyer | b46acba | 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 | |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 136 | static OPCODES *curopcodes = NULL; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 137 | |
| 138 | /* HW access functions */ |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 139 | static uint32_t REGREAD32(int X) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 140 | { |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 141 | return mmio_readl(ich_spibar + X); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 144 | static uint16_t REGREAD16(int X) |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 145 | { |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 146 | return mmio_readw(ich_spibar + X); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [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) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 152 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 153 | /* Common SPI functions */ |
Uwe Hermann | 09e04f7 | 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); |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 156 | static int generate_opcodes(OPCODES * op); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 157 | static int program_opcodes(OPCODES * op); |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 158 | static int run_opcode(OPCODE op, uint32_t offset, |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 159 | uint8_t datalength, uint8_t * data); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 160 | |
FENG yu ning | f041e9b | 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 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 167 | /* List of opcodes which need preopcodes and matching preopcodes. Unused. */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 168 | const struct preop_opcode_pair pops[] = { |
FENG yu ning | f041e9b | 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 */ |
Carl-Daniel Hailfinger | f15e1ab | 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}, |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 177 | {JEDEC_EWSR, JEDEC_WRSR}, |
| 178 | {0,} |
| 179 | }; |
| 180 | |
Carl-Daniel Hailfinger | f15e1ab | 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 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 184 | static OPCODES O_ST_M25P = { |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 185 | { |
| 186 | JEDEC_WREN, |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 187 | JEDEC_EWSR, |
| 188 | }, |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 189 | { |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 190 | {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 191 | {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 192 | {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 193 | {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg |
Carl-Daniel Hailfinger | 15aa7c6 | 2009-05-26 21:25:08 +0000 | [diff] [blame] | 194 | {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 195 | {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 196 | {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 197 | {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase |
| 198 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [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 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 219 | static OPCODES O_EXISTING = {}; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 220 | |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [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 Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 271 | static int find_opcode(OPCODES *op, uint8_t opcode) |
FENG yu ning | c05a295 | 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 Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 283 | static int find_preop(OPCODES *op, uint8_t preop) |
FENG yu ning | c05a295 | 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 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 295 | /* Create a struct OPCODES based on what we find in the locked down chipset. */ |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 296 | static int generate_opcodes(OPCODES * op) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 297 | { |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 298 | int a; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 299 | uint16_t preop, optype; |
| 300 | uint32_t opmenu[2]; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 301 | |
| 302 | if (op == NULL) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 303 | msg_perr("\n%s: null OPCODES pointer!\n", __func__); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 304 | return -1; |
| 305 | } |
| 306 | |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 307 | switch (spi_controller) { |
| 308 | case SPI_CONTROLLER_ICH7: |
| 309 | case SPI_CONTROLLER_VIA: |
FENG yu ning | c05a295 | 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; |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 315 | case SPI_CONTROLLER_ICH9: |
FENG yu ning | c05a295 | 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: |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 322 | msg_perr("%s: unsupported chipset\n", __func__); |
FENG yu ning | c05a295 | 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 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 344 | /* No preopcodes used by default. */ |
| 345 | for (a = 0; a < 8; a++) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 346 | op->opcode[a].atomic = 0; |
| 347 | |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 351 | int program_opcodes(OPCODES * op) |
| 352 | { |
| 353 | uint8_t a; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 354 | uint16_t preop, optype; |
| 355 | uint32_t opmenu[2]; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 356 | |
| 357 | /* Program Prefix Opcodes */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 358 | /* 0:7 Prefix Opcode 1 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 359 | preop = (op->preop[0]); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 360 | /* 8:16 Prefix Opcode 2 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 361 | preop |= ((uint16_t) op->preop[1]) << 8; |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 362 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 363 | /* Program Opcode Types 0 - 7 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 364 | optype = 0; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 365 | for (a = 0; a < 8; a++) { |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 366 | optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 367 | } |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 368 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 369 | /* Program Allowable Opcodes 0 - 3 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 370 | opmenu[0] = 0; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 371 | for (a = 0; a < 4; a++) { |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 372 | opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 373 | } |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 374 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 375 | /*Program Allowable Opcodes 4 - 7 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 376 | opmenu[1] = 0; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 377 | for (a = 4; a < 8; a++) { |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 378 | opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 379 | } |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 380 | |
Sean Nelson | 316a29f | 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]); |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 382 | switch (spi_controller) { |
| 383 | case SPI_CONTROLLER_ICH7: |
| 384 | case SPI_CONTROLLER_VIA: |
Stefan Reinauer | 2cb94e1 | 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; |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 390 | case SPI_CONTROLLER_ICH9: |
Stefan Reinauer | 2cb94e1 | 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: |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 397 | msg_perr("%s: unsupported chipset\n", __func__); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 398 | return -1; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 399 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
Carl-Daniel Hailfinger | 80f3d05 | 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 | { |
| 410 | switch (spi_controller) { |
| 411 | case SPI_CONTROLLER_ICH7: |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 412 | mmio_writel(minaddr, ich_spibar + 0x50); |
| 413 | ichspi_bbar = mmio_readl(ich_spibar + 0x50); |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 414 | /* We don't have any option except complaining. */ |
| 415 | if (ichspi_bbar != minaddr) |
| 416 | msg_perr("Setting BBAR failed!\n"); |
| 417 | break; |
| 418 | case SPI_CONTROLLER_ICH9: |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 419 | mmio_writel(minaddr, ich_spibar + 0xA0); |
| 420 | ichspi_bbar = mmio_readl(ich_spibar + 0xA0); |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 421 | /* We don't have any option except complaining. */ |
| 422 | if (ichspi_bbar != minaddr) |
| 423 | msg_perr("Setting BBAR failed!\n"); |
| 424 | break; |
| 425 | default: |
| 426 | /* Not sure if BBAR actually exists on VIA. */ |
| 427 | msg_pdbg("Setting BBAR is not implemented for VIA yet.\n"); |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 432 | /* This function generates OPCODES from or programs OPCODES to ICH according to |
| 433 | * the chipset's SPI configuration lock. |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 434 | * |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 435 | * It should be called before ICH sends any spi command. |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 436 | */ |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 437 | static int ich_init_opcodes(void) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 438 | { |
| 439 | int rc = 0; |
| 440 | OPCODES *curopcodes_done; |
| 441 | |
| 442 | if (curopcodes) |
| 443 | return 0; |
| 444 | |
| 445 | if (ichspi_lock) { |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 446 | msg_pdbg("Reading OPCODES... "); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 447 | curopcodes_done = &O_EXISTING; |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 448 | rc = generate_opcodes(curopcodes_done); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 449 | } else { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 450 | msg_pdbg("Programming OPCODES... "); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 451 | curopcodes_done = &O_ST_M25P; |
| 452 | rc = program_opcodes(curopcodes_done); |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 453 | /* Technically not part of opcode init, but it allows opcodes |
| 454 | * to run without transaction errors by setting the lowest |
| 455 | * allowed address to zero. |
| 456 | */ |
| 457 | ich_set_bbar(0); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | if (rc) { |
| 461 | curopcodes = NULL; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 462 | msg_perr("failed\n"); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 463 | return 1; |
| 464 | } else { |
| 465 | curopcodes = curopcodes_done; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 466 | msg_pdbg("done\n"); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | } |
| 470 | |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 471 | static int ich7_run_opcode(OPCODE op, uint32_t offset, |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 472 | uint8_t datalength, uint8_t * data, int maxdata) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 473 | { |
| 474 | int write_cmd = 0; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 475 | int timeout; |
Peter Stuge | 7e2c079 | 2008-06-29 01:30:41 +0000 | [diff] [blame] | 476 | uint32_t temp32 = 0; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 477 | uint16_t temp16; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 478 | uint32_t a; |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 479 | uint64_t opmenu; |
| 480 | int opcode_index; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 481 | |
| 482 | /* Is it a write command? */ |
| 483 | if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) |
| 484 | || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) { |
| 485 | write_cmd = 1; |
| 486 | } |
| 487 | |
| 488 | /* Programm Offset in Flash into FADDR */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 489 | REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 490 | |
| 491 | /* Program data into FDATA0 to N */ |
| 492 | if (write_cmd && (datalength != 0)) { |
| 493 | temp32 = 0; |
| 494 | for (a = 0; a < datalength; a++) { |
| 495 | if ((a % 4) == 0) { |
| 496 | temp32 = 0; |
| 497 | } |
| 498 | |
| 499 | temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8); |
| 500 | |
| 501 | if ((a % 4) == 3) { |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 502 | REGWRITE32(ICH7_REG_SPID0 + (a - (a % 4)), |
| 503 | temp32); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | if (((a - 1) % 4) != 3) { |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 507 | REGWRITE32(ICH7_REG_SPID0 + |
| 508 | ((a - 1) - ((a - 1) % 4)), temp32); |
| 509 | } |
| 510 | |
| 511 | } |
| 512 | |
| 513 | /* Assemble SPIS */ |
| 514 | temp16 = 0; |
| 515 | /* clear error status registers */ |
| 516 | temp16 |= (SPIS_CDS + SPIS_FCERR); |
| 517 | REGWRITE16(ICH7_REG_SPIS, temp16); |
| 518 | |
| 519 | /* Assemble SPIC */ |
| 520 | temp16 = 0; |
| 521 | |
| 522 | if (datalength != 0) { |
| 523 | temp16 |= SPIC_DS; |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 524 | temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /* Select opcode */ |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 528 | opmenu = REGREAD32(ICH7_REG_OPMENU); |
| 529 | opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32; |
| 530 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 531 | for (opcode_index = 0; opcode_index < 8; opcode_index++) { |
| 532 | if ((opmenu & 0xff) == op.opcode) { |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 533 | break; |
| 534 | } |
| 535 | opmenu >>= 8; |
| 536 | } |
| 537 | if (opcode_index == 8) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 538 | msg_pdbg("Opcode %x not found.\n", op.opcode); |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 539 | return 1; |
| 540 | } |
| 541 | temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 542 | |
| 543 | /* Handle Atomic */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 544 | switch (op.atomic) { |
| 545 | case 2: |
| 546 | /* Select second preop. */ |
| 547 | temp16 |= SPIC_SPOP; |
| 548 | /* And fall through. */ |
| 549 | case 1: |
| 550 | /* Atomic command (preop+op) */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 551 | temp16 |= SPIC_ACS; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 552 | break; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /* Start */ |
| 556 | temp16 |= SPIC_SCGO; |
| 557 | |
| 558 | /* write it */ |
| 559 | REGWRITE16(ICH7_REG_SPIC, temp16); |
| 560 | |
| 561 | /* wait for cycle complete */ |
Carl-Daniel Hailfinger | 4c24ad4 | 2009-05-09 07:24:23 +0000 | [diff] [blame] | 562 | timeout = 100 * 1000 * 60; // 60s is a looong timeout. |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 563 | while (((REGREAD16(ICH7_REG_SPIS) & SPIS_CDS) == 0) && --timeout) { |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 564 | programmer_delay(10); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 565 | } |
| 566 | if (!timeout) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 567 | msg_perr("timeout\n"); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 570 | /* FIXME: make sure we do not needlessly cause transaction errors. */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 571 | if ((REGREAD16(ICH7_REG_SPIS) & SPIS_FCERR) != 0) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 572 | msg_pdbg("Transaction error!\n"); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 573 | return 1; |
| 574 | } |
| 575 | |
| 576 | if ((!write_cmd) && (datalength != 0)) { |
| 577 | for (a = 0; a < datalength; a++) { |
| 578 | if ((a % 4) == 0) { |
| 579 | temp32 = REGREAD32(ICH7_REG_SPID0 + (a)); |
| 580 | } |
| 581 | |
| 582 | data[a] = |
| 583 | (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8))) |
| 584 | >> ((a % 4) * 8); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | return 0; |
| 589 | } |
| 590 | |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 591 | static int ich9_run_opcode(OPCODE op, uint32_t offset, |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 592 | uint8_t datalength, uint8_t * data) |
| 593 | { |
| 594 | int write_cmd = 0; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 595 | int timeout; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 596 | uint32_t temp32; |
| 597 | uint32_t a; |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 598 | uint64_t opmenu; |
| 599 | int opcode_index; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 600 | |
| 601 | /* Is it a write command? */ |
| 602 | if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) |
| 603 | || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) { |
| 604 | write_cmd = 1; |
| 605 | } |
| 606 | |
| 607 | /* Programm Offset in Flash into FADDR */ |
| 608 | REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */ |
| 609 | |
| 610 | /* Program data into FDATA0 to N */ |
| 611 | if (write_cmd && (datalength != 0)) { |
| 612 | temp32 = 0; |
| 613 | for (a = 0; a < datalength; a++) { |
| 614 | if ((a % 4) == 0) { |
| 615 | temp32 = 0; |
| 616 | } |
| 617 | |
| 618 | temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8); |
| 619 | |
| 620 | if ((a % 4) == 3) { |
| 621 | REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)), |
| 622 | temp32); |
| 623 | } |
| 624 | } |
| 625 | if (((a - 1) % 4) != 3) { |
| 626 | REGWRITE32(ICH9_REG_FDATA0 + |
| 627 | ((a - 1) - ((a - 1) % 4)), temp32); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 628 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /* Assemble SSFS + SSFC */ |
Helge Wagner | a319be1 | 2010-08-11 21:06:10 +0000 | [diff] [blame] | 632 | /* keep reserved bits (23-19,7,0) */ |
| 633 | temp32 = REGREAD32(ICH9_REG_SSFS); |
| 634 | temp32 &= 0xF8008100; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 635 | |
| 636 | /* clear error status registers */ |
| 637 | temp32 |= (SSFS_CDS + SSFS_FCERR); |
Uwe Hermann | 4e3d0b3 | 2010-03-25 23:18:41 +0000 | [diff] [blame] | 638 | /* Use 20 MHz */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 639 | temp32 |= SSFC_SCF_20MHZ; |
| 640 | |
| 641 | if (datalength != 0) { |
| 642 | uint32_t datatemp; |
| 643 | temp32 |= SSFC_DS; |
| 644 | datatemp = ((uint32_t) ((datalength - 1) & 0x3f)) << (8 + 8); |
| 645 | temp32 |= datatemp; |
| 646 | } |
| 647 | |
| 648 | /* Select opcode */ |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 649 | opmenu = REGREAD32(ICH9_REG_OPMENU); |
| 650 | opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32; |
| 651 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 652 | for (opcode_index = 0; opcode_index < 8; opcode_index++) { |
| 653 | if ((opmenu & 0xff) == op.opcode) { |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 654 | break; |
| 655 | } |
| 656 | opmenu >>= 8; |
| 657 | } |
| 658 | if (opcode_index == 8) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 659 | msg_pdbg("Opcode %x not found.\n", op.opcode); |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 660 | return 1; |
| 661 | } |
| 662 | temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 663 | |
| 664 | /* Handle Atomic */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 665 | switch (op.atomic) { |
| 666 | case 2: |
| 667 | /* Select second preop. */ |
| 668 | temp32 |= SSFC_SPOP; |
| 669 | /* And fall through. */ |
| 670 | case 1: |
| 671 | /* Atomic command (preop+op) */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 672 | temp32 |= SSFC_ACS; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 673 | break; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /* Start */ |
| 677 | temp32 |= SSFC_SCGO; |
| 678 | |
| 679 | /* write it */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 680 | REGWRITE32(ICH9_REG_SSFS, temp32); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 681 | |
| 682 | /*wait for cycle complete */ |
Carl-Daniel Hailfinger | 4c24ad4 | 2009-05-09 07:24:23 +0000 | [diff] [blame] | 683 | timeout = 100 * 1000 * 60; // 60s is a looong timeout. |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 684 | while (((REGREAD32(ICH9_REG_SSFS) & SSFS_CDS) == 0) && --timeout) { |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 685 | programmer_delay(10); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 686 | } |
| 687 | if (!timeout) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 688 | msg_perr("timeout\n"); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 691 | /* FIXME make sure we do not needlessly cause transaction errors. */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 692 | if ((REGREAD32(ICH9_REG_SSFS) & SSFS_FCERR) != 0) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 693 | msg_pdbg("Transaction error!\n"); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 694 | return 1; |
| 695 | } |
| 696 | |
| 697 | if ((!write_cmd) && (datalength != 0)) { |
| 698 | for (a = 0; a < datalength; a++) { |
| 699 | if ((a % 4) == 0) { |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 700 | temp32 = REGREAD32(ICH9_REG_FDATA0 + (a)); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | data[a] = |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 704 | (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8))) |
| 705 | >> ((a % 4) * 8); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
| 709 | return 0; |
| 710 | } |
| 711 | |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 712 | static int run_opcode(OPCODE op, uint32_t offset, |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 713 | uint8_t datalength, uint8_t * data) |
| 714 | { |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 715 | switch (spi_controller) { |
| 716 | case SPI_CONTROLLER_VIA: |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 717 | if (datalength > 16) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 718 | msg_perr("%s: Internal command size error for " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 719 | "opcode 0x%02x, got datalength=%i, want <=16\n", |
| 720 | __func__, op.opcode, datalength); |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 721 | return SPI_INVALID_LENGTH; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 722 | } |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 723 | return ich7_run_opcode(op, offset, datalength, data, 16); |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 724 | case SPI_CONTROLLER_ICH7: |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 725 | if (datalength > 64) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 726 | msg_perr("%s: Internal command size error for " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 727 | "opcode 0x%02x, got datalength=%i, want <=16\n", |
| 728 | __func__, op.opcode, datalength); |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 729 | return SPI_INVALID_LENGTH; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 730 | } |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 731 | return ich7_run_opcode(op, offset, datalength, data, 64); |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 732 | case SPI_CONTROLLER_ICH9: |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 733 | if (datalength > 64) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 734 | msg_perr("%s: Internal command size error for " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 735 | "opcode 0x%02x, got datalength=%i, want <=16\n", |
| 736 | __func__, op.opcode, datalength); |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 737 | return SPI_INVALID_LENGTH; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 738 | } |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 739 | return ich9_run_opcode(op, offset, datalength, data); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 740 | default: |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 741 | msg_perr("%s: unsupported chipset\n", __func__); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 742 | } |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 743 | |
| 744 | /* If we ever get here, something really weird happened */ |
| 745 | return -1; |
| 746 | } |
| 747 | |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 748 | int ich_spi_read(struct flashchip *flash, uint8_t * buf, int start, int len) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 749 | { |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 750 | int maxdata = 64; |
| 751 | |
Carl-Daniel Hailfinger | 38a059d | 2009-06-13 12:04:03 +0000 | [diff] [blame] | 752 | if (spi_controller == SPI_CONTROLLER_VIA) |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 753 | maxdata = 16; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 754 | |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 755 | return spi_read_chunked(flash, buf, start, len, maxdata); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 758 | int ich_spi_write_256(struct flashchip *flash, uint8_t * buf, int start, int len) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 759 | { |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 760 | int maxdata = 64; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 761 | |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 762 | if (spi_controller == SPI_CONTROLLER_VIA) |
| 763 | maxdata = 16; |
| 764 | |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 765 | return spi_write_chunked(flash, buf, start, len, maxdata); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 768 | int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 769 | const unsigned char *writearr, unsigned char *readarr) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 770 | { |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 771 | int result; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 772 | int opcode_index = -1; |
| 773 | const unsigned char cmd = *writearr; |
| 774 | OPCODE *opcode; |
| 775 | uint32_t addr = 0; |
| 776 | uint8_t *data; |
| 777 | int count; |
| 778 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 779 | /* find cmd in opcodes-table */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 780 | opcode_index = find_opcode(curopcodes, cmd); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 781 | if (opcode_index == -1) { |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame^] | 782 | if (!ichspi_lock) |
| 783 | opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt); |
| 784 | if (opcode_index == -1) { |
| 785 | msg_pdbg("Invalid OPCODE 0x%02x\n", cmd); |
| 786 | return SPI_INVALID_OPCODE; |
| 787 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | opcode = &(curopcodes->opcode[opcode_index]); |
| 791 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 792 | /* The following valid writecnt/readcnt combinations exist: |
| 793 | * writecnt = 4, readcnt >= 0 |
| 794 | * writecnt = 1, readcnt >= 0 |
| 795 | * writecnt >= 4, readcnt = 0 |
| 796 | * writecnt >= 1, readcnt = 0 |
| 797 | * writecnt >= 1 is guaranteed for all commands. |
| 798 | */ |
| 799 | if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) && |
| 800 | (writecnt != 4)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 801 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 802 | "0x%02x, got writecnt=%i, want =4\n", __func__, cmd, |
| 803 | writecnt); |
| 804 | return SPI_INVALID_LENGTH; |
| 805 | } |
| 806 | if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) && |
| 807 | (writecnt != 1)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 808 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 809 | "0x%02x, got writecnt=%i, want =1\n", __func__, cmd, |
| 810 | writecnt); |
| 811 | return SPI_INVALID_LENGTH; |
| 812 | } |
| 813 | if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) && |
| 814 | (writecnt < 4)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 815 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 816 | "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd, |
| 817 | writecnt); |
| 818 | return SPI_INVALID_LENGTH; |
| 819 | } |
| 820 | if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) || |
| 821 | (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) && |
| 822 | (readcnt)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 823 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 824 | "0x%02x, got readcnt=%i, want =0\n", __func__, cmd, |
| 825 | readcnt); |
| 826 | return SPI_INVALID_LENGTH; |
| 827 | } |
| 828 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 829 | /* if opcode-type requires an address */ |
| 830 | if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS || |
| 831 | opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) { |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 832 | addr = (writearr[1] << 16) | |
| 833 | (writearr[2] << 8) | (writearr[3] << 0); |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 834 | switch (spi_controller) { |
| 835 | case SPI_CONTROLLER_ICH7: |
| 836 | case SPI_CONTROLLER_ICH9: |
| 837 | if (addr < ichspi_bbar) { |
| 838 | msg_perr("%s: Address 0x%06x below allowed " |
| 839 | "range 0x%06x-0xffffff\n", __func__, |
| 840 | addr, ichspi_bbar); |
| 841 | return SPI_INVALID_ADDRESS; |
| 842 | } |
| 843 | break; |
| 844 | default: |
| 845 | break; |
| 846 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 847 | } |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 848 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 849 | /* translate read/write array/count */ |
| 850 | if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) { |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 851 | data = (uint8_t *) (writearr + 1); |
| 852 | count = writecnt - 1; |
| 853 | } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) { |
| 854 | data = (uint8_t *) (writearr + 4); |
| 855 | count = writecnt - 4; |
| 856 | } else { |
| 857 | data = (uint8_t *) readarr; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 858 | count = readcnt; |
| 859 | } |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 860 | |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 861 | result = run_opcode(*opcode, addr, count, data); |
| 862 | if (result) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 863 | msg_pdbg("run OPCODE 0x%02x failed\n", opcode->opcode); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 866 | return result; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 867 | } |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 868 | |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 869 | int ich_spi_send_multicommand(struct spi_command *cmds) |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 870 | { |
| 871 | int ret = 0; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 872 | int i; |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 873 | int oppos, preoppos; |
| 874 | for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) { |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 875 | if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) { |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 876 | /* Next command is valid. */ |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 877 | preoppos = find_preop(curopcodes, cmds->writearr[0]); |
| 878 | oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]); |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 879 | if ((oppos == -1) && (preoppos != -1)) { |
| 880 | /* Current command is listed as preopcode in |
| 881 | * ICH struct OPCODES, but next command is not |
| 882 | * listed as opcode in that struct. |
| 883 | * Check for command sanity, then |
| 884 | * try to reprogram the ICH opcode list. |
| 885 | */ |
| 886 | if (find_preop(curopcodes, |
| 887 | (cmds + 1)->writearr[0]) != -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 888 | msg_perr("%s: Two subsequent " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 889 | "preopcodes 0x%02x and 0x%02x, " |
| 890 | "ignoring the first.\n", |
| 891 | __func__, cmds->writearr[0], |
| 892 | (cmds + 1)->writearr[0]); |
| 893 | continue; |
| 894 | } |
| 895 | /* If the chipset is locked down, we'll fail |
| 896 | * during execution of the next command anyway. |
| 897 | * No need to bother with fixups. |
| 898 | */ |
| 899 | if (!ichspi_lock) { |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame^] | 900 | oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt); |
| 901 | if (oppos == -1) |
| 902 | continue; |
| 903 | curopcodes->opcode[oppos].atomic = preoppos + 1; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 904 | continue; |
| 905 | } |
| 906 | } |
| 907 | if ((oppos != -1) && (preoppos != -1)) { |
| 908 | /* Current command is listed as preopcode in |
| 909 | * ICH struct OPCODES and next command is listed |
| 910 | * as opcode in that struct. Match them up. |
| 911 | */ |
| 912 | curopcodes->opcode[oppos].atomic = preoppos + 1; |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 913 | continue; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 914 | } |
| 915 | /* If none of the above if-statements about oppos or |
| 916 | * preoppos matched, this is a normal opcode. |
| 917 | */ |
| 918 | } |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 919 | ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt, |
| 920 | cmds->writearr, cmds->readarr); |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 921 | /* Reset the type of all opcodes to non-atomic. */ |
| 922 | for (i = 0; i < 8; i++) |
| 923 | curopcodes->opcode[i].atomic = 0; |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 924 | } |
| 925 | return ret; |
| 926 | } |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 927 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 928 | #define ICH_BMWAG(x) ((x >> 24) & 0xff) |
| 929 | #define ICH_BMRAG(x) ((x >> 16) & 0xff) |
| 930 | #define ICH_BRWA(x) ((x >> 8) & 0xff) |
| 931 | #define ICH_BRRA(x) ((x >> 0) & 0xff) |
| 932 | |
| 933 | #define ICH_FREG_BASE(x) ((x >> 0) & 0x1fff) |
| 934 | #define ICH_FREG_LIMIT(x) ((x >> 16) & 0x1fff) |
| 935 | |
| 936 | static void do_ich9_spi_frap(uint32_t frap, int i) |
| 937 | { |
| 938 | const char *access_names[4] = { |
| 939 | "locked", "read-only", "write-only", "read-write" |
| 940 | }; |
| 941 | const char *region_names[5] = { |
| 942 | "Flash Descriptor", "BIOS", "Management Engine", |
| 943 | "Gigabit Ethernet", "Platform Data" |
| 944 | }; |
| 945 | uint32_t base, limit; |
| 946 | int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) | |
| 947 | (((ICH_BRRA(frap) >> i) & 1) << 0); |
| 948 | int offset = 0x54 + i * 4; |
| 949 | uint32_t freg = mmio_readl(ich_spibar + offset); |
| 950 | |
| 951 | msg_pdbg("0x%02X: 0x%08x (FREG%i: %s)\n", |
| 952 | offset, freg, i, region_names[i]); |
| 953 | |
| 954 | base = ICH_FREG_BASE(freg); |
| 955 | limit = ICH_FREG_LIMIT(freg); |
| 956 | if (base == 0x1fff && limit == 0) { |
| 957 | /* this FREG is disabled */ |
| 958 | msg_pdbg("%s region is unused.\n", region_names[i]); |
| 959 | return; |
| 960 | } |
| 961 | |
| 962 | msg_pdbg("0x%08x-0x%08x is %s\n", |
| 963 | (base << 12), (limit << 12) | 0x0fff, |
| 964 | access_names[rwperms]); |
| 965 | } |
| 966 | |
| 967 | int ich_init_spi(struct pci_dev *dev, uint32_t base, void *rcrb, |
| 968 | int ich_generation) |
| 969 | { |
| 970 | int i; |
| 971 | uint8_t old, new; |
| 972 | uint16_t spibar_offset, tmp2; |
| 973 | uint32_t tmp; |
| 974 | |
| 975 | buses_supported |= CHIP_BUSTYPE_SPI; |
| 976 | switch (ich_generation) { |
| 977 | case 7: |
| 978 | spi_controller = SPI_CONTROLLER_ICH7; |
| 979 | spibar_offset = 0x3020; |
| 980 | break; |
| 981 | case 8: |
| 982 | spi_controller = SPI_CONTROLLER_ICH9; |
| 983 | spibar_offset = 0x3020; |
| 984 | break; |
| 985 | case 9: |
| 986 | case 10: |
| 987 | default: /* Future version might behave the same */ |
| 988 | spi_controller = SPI_CONTROLLER_ICH9; |
| 989 | spibar_offset = 0x3800; |
| 990 | break; |
| 991 | } |
| 992 | |
| 993 | /* SPIBAR is at RCRB+0x3020 for ICH[78] and RCRB+0x3800 for ICH9. */ |
| 994 | msg_pdbg("SPIBAR = 0x%x + 0x%04x\n", base, spibar_offset); |
| 995 | |
| 996 | /* Assign Virtual Address */ |
| 997 | ich_spibar = rcrb + spibar_offset; |
| 998 | |
| 999 | switch (spi_controller) { |
| 1000 | case SPI_CONTROLLER_ICH7: |
| 1001 | msg_pdbg("0x00: 0x%04x (SPIS)\n", |
| 1002 | mmio_readw(ich_spibar + 0)); |
| 1003 | msg_pdbg("0x02: 0x%04x (SPIC)\n", |
| 1004 | mmio_readw(ich_spibar + 2)); |
| 1005 | msg_pdbg("0x04: 0x%08x (SPIA)\n", |
| 1006 | mmio_readl(ich_spibar + 4)); |
| 1007 | for (i = 0; i < 8; i++) { |
| 1008 | int offs; |
| 1009 | offs = 8 + (i * 8); |
| 1010 | msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs, |
| 1011 | mmio_readl(ich_spibar + offs), i); |
| 1012 | msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4, |
| 1013 | mmio_readl(ich_spibar + offs + 4), i); |
| 1014 | } |
| 1015 | ichspi_bbar = mmio_readl(ich_spibar + 0x50); |
| 1016 | msg_pdbg("0x50: 0x%08x (BBAR)\n", |
| 1017 | ichspi_bbar); |
| 1018 | msg_pdbg("0x54: 0x%04x (PREOP)\n", |
| 1019 | mmio_readw(ich_spibar + 0x54)); |
| 1020 | msg_pdbg("0x56: 0x%04x (OPTYPE)\n", |
| 1021 | mmio_readw(ich_spibar + 0x56)); |
| 1022 | msg_pdbg("0x58: 0x%08x (OPMENU)\n", |
| 1023 | mmio_readl(ich_spibar + 0x58)); |
| 1024 | msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", |
| 1025 | mmio_readl(ich_spibar + 0x5c)); |
| 1026 | for (i = 0; i < 4; i++) { |
| 1027 | int offs; |
| 1028 | offs = 0x60 + (i * 4); |
| 1029 | msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs, |
| 1030 | mmio_readl(ich_spibar + offs), i); |
| 1031 | } |
| 1032 | msg_pdbg("\n"); |
| 1033 | if (mmio_readw(ich_spibar) & (1 << 15)) { |
| 1034 | msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n"); |
| 1035 | ichspi_lock = 1; |
| 1036 | } |
| 1037 | ich_init_opcodes(); |
| 1038 | break; |
| 1039 | case SPI_CONTROLLER_ICH9: |
| 1040 | tmp2 = mmio_readw(ich_spibar + 4); |
| 1041 | msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2); |
| 1042 | msg_pdbg("FLOCKDN %i, ", (tmp2 >> 15 & 1)); |
| 1043 | msg_pdbg("FDV %i, ", (tmp2 >> 14) & 1); |
| 1044 | msg_pdbg("FDOPSS %i, ", (tmp2 >> 13) & 1); |
| 1045 | msg_pdbg("SCIP %i, ", (tmp2 >> 5) & 1); |
| 1046 | msg_pdbg("BERASE %i, ", (tmp2 >> 3) & 3); |
| 1047 | msg_pdbg("AEL %i, ", (tmp2 >> 2) & 1); |
| 1048 | msg_pdbg("FCERR %i, ", (tmp2 >> 1) & 1); |
| 1049 | msg_pdbg("FDONE %i\n", (tmp2 >> 0) & 1); |
| 1050 | |
| 1051 | tmp = mmio_readl(ich_spibar + 0x50); |
| 1052 | msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp); |
| 1053 | msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp)); |
| 1054 | msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp)); |
| 1055 | msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp)); |
| 1056 | msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp)); |
| 1057 | |
| 1058 | /* print out the FREGx registers along with FRAP access bits */ |
| 1059 | for(i = 0; i < 5; i++) |
| 1060 | do_ich9_spi_frap(tmp, i); |
| 1061 | |
| 1062 | msg_pdbg("0x74: 0x%08x (PR0)\n", |
| 1063 | mmio_readl(ich_spibar + 0x74)); |
| 1064 | msg_pdbg("0x78: 0x%08x (PR1)\n", |
| 1065 | mmio_readl(ich_spibar + 0x78)); |
| 1066 | msg_pdbg("0x7C: 0x%08x (PR2)\n", |
| 1067 | mmio_readl(ich_spibar + 0x7C)); |
| 1068 | msg_pdbg("0x80: 0x%08x (PR3)\n", |
| 1069 | mmio_readl(ich_spibar + 0x80)); |
| 1070 | msg_pdbg("0x84: 0x%08x (PR4)\n", |
| 1071 | mmio_readl(ich_spibar + 0x84)); |
| 1072 | msg_pdbg("0x90: 0x%08x (SSFS, SSFC)\n", |
| 1073 | mmio_readl(ich_spibar + 0x90)); |
| 1074 | msg_pdbg("0x94: 0x%04x (PREOP)\n", |
| 1075 | mmio_readw(ich_spibar + 0x94)); |
| 1076 | msg_pdbg("0x96: 0x%04x (OPTYPE)\n", |
| 1077 | mmio_readw(ich_spibar + 0x96)); |
| 1078 | msg_pdbg("0x98: 0x%08x (OPMENU)\n", |
| 1079 | mmio_readl(ich_spibar + 0x98)); |
| 1080 | msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n", |
| 1081 | mmio_readl(ich_spibar + 0x9C)); |
| 1082 | ichspi_bbar = mmio_readl(ich_spibar + 0xA0); |
| 1083 | msg_pdbg("0xA0: 0x%08x (BBAR)\n", |
| 1084 | ichspi_bbar); |
| 1085 | msg_pdbg("0xB0: 0x%08x (FDOC)\n", |
| 1086 | mmio_readl(ich_spibar + 0xB0)); |
| 1087 | if (tmp2 & (1 << 15)) { |
| 1088 | msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n"); |
| 1089 | ichspi_lock = 1; |
| 1090 | } |
| 1091 | ich_init_opcodes(); |
| 1092 | break; |
| 1093 | default: |
| 1094 | /* Nothing */ |
| 1095 | break; |
| 1096 | } |
| 1097 | |
| 1098 | old = pci_read_byte(dev, 0xdc); |
| 1099 | msg_pdbg("SPI Read Configuration: "); |
| 1100 | new = (old >> 2) & 0x3; |
| 1101 | switch (new) { |
| 1102 | case 0: |
| 1103 | case 1: |
| 1104 | case 2: |
| 1105 | msg_pdbg("prefetching %sabled, caching %sabled, ", |
| 1106 | (new & 0x2) ? "en" : "dis", |
| 1107 | (new & 0x1) ? "dis" : "en"); |
| 1108 | break; |
| 1109 | default: |
| 1110 | msg_pdbg("invalid prefetching/caching settings, "); |
| 1111 | break; |
| 1112 | } |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | int via_init_spi(struct pci_dev *dev) |
| 1117 | { |
| 1118 | uint32_t mmio_base; |
| 1119 | |
| 1120 | mmio_base = (pci_read_long(dev, 0xbc)) << 8; |
| 1121 | msg_pdbg("MMIO base at = 0x%x\n", mmio_base); |
| 1122 | ich_spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70); |
| 1123 | |
| 1124 | msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n", |
| 1125 | mmio_readw(ich_spibar + 0x6c)); |
| 1126 | |
| 1127 | /* Not sure if it speaks all these bus protocols. */ |
| 1128 | buses_supported = CHIP_BUSTYPE_LPC | CHIP_BUSTYPE_FWH | CHIP_BUSTYPE_SPI; |
| 1129 | spi_controller = SPI_CONTROLLER_VIA; |
| 1130 | ich_init_opcodes(); |
| 1131 | |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1135 | #endif |