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 |
| 33 | * |
| 34 | */ |
| 35 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 36 | #if defined(__i386__) || defined(__x86_64__) |
| 37 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 38 | #include <string.h> |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 39 | #include "flash.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 40 | #include "chipdrivers.h" |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 41 | #include "spi.h" |
| 42 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 43 | /* ICH9 controller register definition */ |
| 44 | #define ICH9_REG_FADDR 0x08 /* 32 Bits */ |
| 45 | #define ICH9_REG_FDATA0 0x10 /* 64 Bytes */ |
| 46 | |
| 47 | #define ICH9_REG_SSFS 0x90 /* 08 Bits */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 48 | #define SSFS_SCIP 0x00000001 |
| 49 | #define SSFS_CDS 0x00000004 |
| 50 | #define SSFS_FCERR 0x00000008 |
| 51 | #define SSFS_AEL 0x00000010 |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 52 | |
| 53 | #define ICH9_REG_SSFC 0x91 /* 24 Bits */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 54 | #define SSFC_SCGO 0x00000200 |
| 55 | #define SSFC_ACS 0x00000400 |
| 56 | #define SSFC_SPOP 0x00000800 |
| 57 | #define SSFC_COP 0x00001000 |
| 58 | #define SSFC_DBC 0x00010000 |
| 59 | #define SSFC_DS 0x00400000 |
| 60 | #define SSFC_SME 0x00800000 |
| 61 | #define SSFC_SCF 0x01000000 |
| 62 | #define SSFC_SCF_20MHZ 0x00000000 |
| 63 | #define SSFC_SCF_33MHZ 0x01000000 |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 64 | |
| 65 | #define ICH9_REG_PREOP 0x94 /* 16 Bits */ |
| 66 | #define ICH9_REG_OPTYPE 0x96 /* 16 Bits */ |
| 67 | #define ICH9_REG_OPMENU 0x98 /* 64 Bits */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 68 | |
| 69 | // ICH9R SPI commands |
| 70 | #define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0 |
| 71 | #define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1 |
| 72 | #define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2 |
| 73 | #define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3 |
| 74 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 75 | // ICH7 registers |
| 76 | #define ICH7_REG_SPIS 0x00 /* 16 Bits */ |
| 77 | #define SPIS_SCIP 0x00000001 |
| 78 | #define SPIS_CDS 0x00000004 |
| 79 | #define SPIS_FCERR 0x00000008 |
| 80 | |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 81 | /* VIA SPI is compatible with ICH7, but maxdata |
| 82 | to transfer is 16 bytes. |
| 83 | |
| 84 | DATA byte count on ICH7 is 8:13, on VIA 8:11 |
| 85 | |
| 86 | bit 12 is port select CS0 CS1 |
| 87 | bit 13 is FAST READ enable |
| 88 | bit 7 is used with fast read and one shot controls CS de-assert? |
| 89 | */ |
| 90 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 91 | #define ICH7_REG_SPIC 0x02 /* 16 Bits */ |
| 92 | #define SPIC_SCGO 0x0002 |
| 93 | #define SPIC_ACS 0x0004 |
| 94 | #define SPIC_SPOP 0x0008 |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 95 | #define SPIC_DS 0x4000 |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 96 | |
| 97 | #define ICH7_REG_SPIA 0x04 /* 32 Bits */ |
| 98 | #define ICH7_REG_SPID0 0x08 /* 64 Bytes */ |
| 99 | #define ICH7_REG_PREOP 0x54 /* 16 Bits */ |
| 100 | #define ICH7_REG_OPTYPE 0x56 /* 16 Bits */ |
| 101 | #define ICH7_REG_OPMENU 0x58 /* 64 Bits */ |
| 102 | |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 103 | /* ICH SPI configuration lock-down. May be set during chipset enabling. */ |
| 104 | int ichspi_lock = 0; |
| 105 | |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 106 | uint32_t ichspi_bbar = 0; |
| 107 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 108 | void *ich_spibar = NULL; |
| 109 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 110 | typedef struct _OPCODE { |
| 111 | uint8_t opcode; //This commands spi opcode |
| 112 | uint8_t spi_type; //This commands spi type |
| 113 | uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1 |
| 114 | } OPCODE; |
| 115 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 116 | /* Suggested opcode definition: |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 117 | * Preop 1: Write Enable |
| 118 | * Preop 2: Write Status register enable |
| 119 | * |
| 120 | * OP 0: Write address |
| 121 | * OP 1: Read Address |
| 122 | * OP 2: ERASE block |
| 123 | * OP 3: Read Status register |
| 124 | * OP 4: Read ID |
| 125 | * OP 5: Write Status register |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 126 | * OP 6: chip private (read JEDEC id) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 127 | * OP 7: Chip erase |
| 128 | */ |
| 129 | typedef struct _OPCODES { |
| 130 | uint8_t preop[2]; |
| 131 | OPCODE opcode[8]; |
| 132 | } OPCODES; |
| 133 | |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 134 | static OPCODES *curopcodes = NULL; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 135 | |
| 136 | /* HW access functions */ |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 137 | static uint32_t REGREAD32(int X) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 138 | { |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 139 | return mmio_readl(ich_spibar + X); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 142 | static uint16_t REGREAD16(int X) |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 143 | { |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 144 | return mmio_readw(ich_spibar + X); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 147 | #define REGWRITE32(X,Y) mmio_writel(Y, ich_spibar+X) |
| 148 | #define REGWRITE16(X,Y) mmio_writew(Y, ich_spibar+X) |
| 149 | #define REGWRITE8(X,Y) mmio_writeb(Y, ich_spibar+X) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 150 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 151 | /* Common SPI functions */ |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 152 | static int find_opcode(OPCODES *op, uint8_t opcode); |
| 153 | static int find_preop(OPCODES *op, uint8_t preop); |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 154 | static int generate_opcodes(OPCODES * op); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 155 | static int program_opcodes(OPCODES * op); |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 156 | static int run_opcode(OPCODE op, uint32_t offset, |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 157 | uint8_t datalength, uint8_t * data); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 158 | |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 159 | /* for pairing opcodes with their required preop */ |
| 160 | struct preop_opcode_pair { |
| 161 | uint8_t preop; |
| 162 | uint8_t opcode; |
| 163 | }; |
| 164 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 165 | /* List of opcodes which need preopcodes and matching preopcodes. Unused. */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 166 | const struct preop_opcode_pair pops[] = { |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 167 | {JEDEC_WREN, JEDEC_BYTE_PROGRAM}, |
| 168 | {JEDEC_WREN, JEDEC_SE}, /* sector erase */ |
| 169 | {JEDEC_WREN, JEDEC_BE_52}, /* block erase */ |
| 170 | {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */ |
| 171 | {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */ |
| 172 | {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 173 | /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */ |
| 174 | {JEDEC_WREN, JEDEC_WRSR}, |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 175 | {JEDEC_EWSR, JEDEC_WRSR}, |
| 176 | {0,} |
| 177 | }; |
| 178 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 179 | /* Reasonable default configuration. Needs ad-hoc modifications if we |
| 180 | * encounter unlisted opcodes. Fun. |
| 181 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 182 | static OPCODES O_ST_M25P = { |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 183 | { |
| 184 | JEDEC_WREN, |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 185 | JEDEC_EWSR, |
| 186 | }, |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 187 | { |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 188 | {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 189 | {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 190 | {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 191 | {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] | 192 | {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] | 193 | {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 194 | {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] | 195 | {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase |
| 196 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 199 | static OPCODES O_EXISTING = {}; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 200 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 201 | static int find_opcode(OPCODES *op, uint8_t opcode) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 202 | { |
| 203 | int a; |
| 204 | |
| 205 | for (a = 0; a < 8; a++) { |
| 206 | if (op->opcode[a].opcode == opcode) |
| 207 | return a; |
| 208 | } |
| 209 | |
| 210 | return -1; |
| 211 | } |
| 212 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 213 | static int find_preop(OPCODES *op, uint8_t preop) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 214 | { |
| 215 | int a; |
| 216 | |
| 217 | for (a = 0; a < 2; a++) { |
| 218 | if (op->preop[a] == preop) |
| 219 | return a; |
| 220 | } |
| 221 | |
| 222 | return -1; |
| 223 | } |
| 224 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 225 | /* 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] | 226 | static int generate_opcodes(OPCODES * op) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 227 | { |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 228 | int a; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 229 | uint16_t preop, optype; |
| 230 | uint32_t opmenu[2]; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 231 | |
| 232 | if (op == NULL) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 233 | msg_perr("\n%s: null OPCODES pointer!\n", __func__); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 234 | return -1; |
| 235 | } |
| 236 | |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 237 | switch (spi_controller) { |
| 238 | case SPI_CONTROLLER_ICH7: |
| 239 | case SPI_CONTROLLER_VIA: |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 240 | preop = REGREAD16(ICH7_REG_PREOP); |
| 241 | optype = REGREAD16(ICH7_REG_OPTYPE); |
| 242 | opmenu[0] = REGREAD32(ICH7_REG_OPMENU); |
| 243 | opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4); |
| 244 | break; |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 245 | case SPI_CONTROLLER_ICH9: |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 246 | preop = REGREAD16(ICH9_REG_PREOP); |
| 247 | optype = REGREAD16(ICH9_REG_OPTYPE); |
| 248 | opmenu[0] = REGREAD32(ICH9_REG_OPMENU); |
| 249 | opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4); |
| 250 | break; |
| 251 | default: |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 252 | msg_perr("%s: unsupported chipset\n", __func__); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | op->preop[0] = (uint8_t) preop; |
| 257 | op->preop[1] = (uint8_t) (preop >> 8); |
| 258 | |
| 259 | for (a = 0; a < 8; a++) { |
| 260 | op->opcode[a].spi_type = (uint8_t) (optype & 0x3); |
| 261 | optype >>= 2; |
| 262 | } |
| 263 | |
| 264 | for (a = 0; a < 4; a++) { |
| 265 | op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff); |
| 266 | opmenu[0] >>= 8; |
| 267 | } |
| 268 | |
| 269 | for (a = 4; a < 8; a++) { |
| 270 | op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff); |
| 271 | opmenu[1] >>= 8; |
| 272 | } |
| 273 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 274 | /* No preopcodes used by default. */ |
| 275 | for (a = 0; a < 8; a++) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 276 | op->opcode[a].atomic = 0; |
| 277 | |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 281 | int program_opcodes(OPCODES * op) |
| 282 | { |
| 283 | uint8_t a; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 284 | uint16_t preop, optype; |
| 285 | uint32_t opmenu[2]; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 286 | |
| 287 | /* Program Prefix Opcodes */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 288 | /* 0:7 Prefix Opcode 1 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 289 | preop = (op->preop[0]); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 290 | /* 8:16 Prefix Opcode 2 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 291 | preop |= ((uint16_t) op->preop[1]) << 8; |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 292 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 293 | /* Program Opcode Types 0 - 7 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 294 | optype = 0; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 295 | for (a = 0; a < 8; a++) { |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 296 | optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 297 | } |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 298 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 299 | /* Program Allowable Opcodes 0 - 3 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 300 | opmenu[0] = 0; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 301 | for (a = 0; a < 4; a++) { |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 302 | opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 303 | } |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 304 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 305 | /*Program Allowable Opcodes 4 - 7 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 306 | opmenu[1] = 0; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 307 | for (a = 4; a < 8; a++) { |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 308 | opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 309 | } |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 310 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 311 | 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] | 312 | switch (spi_controller) { |
| 313 | case SPI_CONTROLLER_ICH7: |
| 314 | case SPI_CONTROLLER_VIA: |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 315 | REGWRITE16(ICH7_REG_PREOP, preop); |
| 316 | REGWRITE16(ICH7_REG_OPTYPE, optype); |
| 317 | REGWRITE32(ICH7_REG_OPMENU, opmenu[0]); |
| 318 | REGWRITE32(ICH7_REG_OPMENU + 4, opmenu[1]); |
| 319 | break; |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 320 | case SPI_CONTROLLER_ICH9: |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 321 | REGWRITE16(ICH9_REG_PREOP, preop); |
| 322 | REGWRITE16(ICH9_REG_OPTYPE, optype); |
| 323 | REGWRITE32(ICH9_REG_OPMENU, opmenu[0]); |
| 324 | REGWRITE32(ICH9_REG_OPMENU + 4, opmenu[1]); |
| 325 | break; |
| 326 | default: |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 327 | msg_perr("%s: unsupported chipset\n", __func__); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 328 | return -1; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 329 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 334 | /* |
| 335 | * Try to set BBAR (BIOS Base Address Register), but read back the value in case |
| 336 | * it didn't stick. |
| 337 | */ |
| 338 | void ich_set_bbar(uint32_t minaddr) |
| 339 | { |
| 340 | switch (spi_controller) { |
| 341 | case SPI_CONTROLLER_ICH7: |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 342 | mmio_writel(minaddr, ich_spibar + 0x50); |
| 343 | ichspi_bbar = mmio_readl(ich_spibar + 0x50); |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 344 | /* We don't have any option except complaining. */ |
| 345 | if (ichspi_bbar != minaddr) |
| 346 | msg_perr("Setting BBAR failed!\n"); |
| 347 | break; |
| 348 | case SPI_CONTROLLER_ICH9: |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 349 | mmio_writel(minaddr, ich_spibar + 0xA0); |
| 350 | ichspi_bbar = mmio_readl(ich_spibar + 0xA0); |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 351 | /* We don't have any option except complaining. */ |
| 352 | if (ichspi_bbar != minaddr) |
| 353 | msg_perr("Setting BBAR failed!\n"); |
| 354 | break; |
| 355 | default: |
| 356 | /* Not sure if BBAR actually exists on VIA. */ |
| 357 | msg_pdbg("Setting BBAR is not implemented for VIA yet.\n"); |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 362 | /* This function generates OPCODES from or programs OPCODES to ICH according to |
| 363 | * the chipset's SPI configuration lock. |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 364 | * |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 365 | * It should be called before ICH sends any spi command. |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 366 | */ |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 367 | int ich_init_opcodes(void) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 368 | { |
| 369 | int rc = 0; |
| 370 | OPCODES *curopcodes_done; |
| 371 | |
| 372 | if (curopcodes) |
| 373 | return 0; |
| 374 | |
| 375 | if (ichspi_lock) { |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 376 | msg_pdbg("Reading OPCODES... "); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 377 | curopcodes_done = &O_EXISTING; |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 378 | rc = generate_opcodes(curopcodes_done); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 379 | } else { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 380 | msg_pdbg("Programming OPCODES... "); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 381 | curopcodes_done = &O_ST_M25P; |
| 382 | rc = program_opcodes(curopcodes_done); |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 383 | /* Technically not part of opcode init, but it allows opcodes |
| 384 | * to run without transaction errors by setting the lowest |
| 385 | * allowed address to zero. |
| 386 | */ |
| 387 | ich_set_bbar(0); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | if (rc) { |
| 391 | curopcodes = NULL; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 392 | msg_perr("failed\n"); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 393 | return 1; |
| 394 | } else { |
| 395 | curopcodes = curopcodes_done; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 396 | msg_pdbg("done\n"); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | } |
| 400 | |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 401 | static int ich7_run_opcode(OPCODE op, uint32_t offset, |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 402 | uint8_t datalength, uint8_t * data, int maxdata) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 403 | { |
| 404 | int write_cmd = 0; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 405 | int timeout; |
Peter Stuge | 7e2c079 | 2008-06-29 01:30:41 +0000 | [diff] [blame] | 406 | uint32_t temp32 = 0; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 407 | uint16_t temp16; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 408 | uint32_t a; |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 409 | uint64_t opmenu; |
| 410 | int opcode_index; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 411 | |
| 412 | /* Is it a write command? */ |
| 413 | if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) |
| 414 | || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) { |
| 415 | write_cmd = 1; |
| 416 | } |
| 417 | |
| 418 | /* Programm Offset in Flash into FADDR */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 419 | REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 420 | |
| 421 | /* Program data into FDATA0 to N */ |
| 422 | if (write_cmd && (datalength != 0)) { |
| 423 | temp32 = 0; |
| 424 | for (a = 0; a < datalength; a++) { |
| 425 | if ((a % 4) == 0) { |
| 426 | temp32 = 0; |
| 427 | } |
| 428 | |
| 429 | temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8); |
| 430 | |
| 431 | if ((a % 4) == 3) { |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 432 | REGWRITE32(ICH7_REG_SPID0 + (a - (a % 4)), |
| 433 | temp32); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | if (((a - 1) % 4) != 3) { |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 437 | REGWRITE32(ICH7_REG_SPID0 + |
| 438 | ((a - 1) - ((a - 1) % 4)), temp32); |
| 439 | } |
| 440 | |
| 441 | } |
| 442 | |
| 443 | /* Assemble SPIS */ |
| 444 | temp16 = 0; |
| 445 | /* clear error status registers */ |
| 446 | temp16 |= (SPIS_CDS + SPIS_FCERR); |
| 447 | REGWRITE16(ICH7_REG_SPIS, temp16); |
| 448 | |
| 449 | /* Assemble SPIC */ |
| 450 | temp16 = 0; |
| 451 | |
| 452 | if (datalength != 0) { |
| 453 | temp16 |= SPIC_DS; |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 454 | temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* Select opcode */ |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 458 | opmenu = REGREAD32(ICH7_REG_OPMENU); |
| 459 | opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32; |
| 460 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 461 | for (opcode_index = 0; opcode_index < 8; opcode_index++) { |
| 462 | if ((opmenu & 0xff) == op.opcode) { |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 463 | break; |
| 464 | } |
| 465 | opmenu >>= 8; |
| 466 | } |
| 467 | if (opcode_index == 8) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 468 | msg_pdbg("Opcode %x not found.\n", op.opcode); |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 469 | return 1; |
| 470 | } |
| 471 | temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 472 | |
| 473 | /* Handle Atomic */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 474 | switch (op.atomic) { |
| 475 | case 2: |
| 476 | /* Select second preop. */ |
| 477 | temp16 |= SPIC_SPOP; |
| 478 | /* And fall through. */ |
| 479 | case 1: |
| 480 | /* Atomic command (preop+op) */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 481 | temp16 |= SPIC_ACS; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 482 | break; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /* Start */ |
| 486 | temp16 |= SPIC_SCGO; |
| 487 | |
| 488 | /* write it */ |
| 489 | REGWRITE16(ICH7_REG_SPIC, temp16); |
| 490 | |
| 491 | /* wait for cycle complete */ |
Carl-Daniel Hailfinger | 4c24ad4 | 2009-05-09 07:24:23 +0000 | [diff] [blame] | 492 | timeout = 100 * 1000 * 60; // 60s is a looong timeout. |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 493 | while (((REGREAD16(ICH7_REG_SPIS) & SPIS_CDS) == 0) && --timeout) { |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 494 | programmer_delay(10); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 495 | } |
| 496 | if (!timeout) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 497 | msg_perr("timeout\n"); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 500 | /* FIXME: make sure we do not needlessly cause transaction errors. */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 501 | if ((REGREAD16(ICH7_REG_SPIS) & SPIS_FCERR) != 0) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 502 | msg_pdbg("Transaction error!\n"); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 503 | return 1; |
| 504 | } |
| 505 | |
| 506 | if ((!write_cmd) && (datalength != 0)) { |
| 507 | for (a = 0; a < datalength; a++) { |
| 508 | if ((a % 4) == 0) { |
| 509 | temp32 = REGREAD32(ICH7_REG_SPID0 + (a)); |
| 510 | } |
| 511 | |
| 512 | data[a] = |
| 513 | (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8))) |
| 514 | >> ((a % 4) * 8); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 521 | static int ich9_run_opcode(OPCODE op, uint32_t offset, |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 522 | uint8_t datalength, uint8_t * data) |
| 523 | { |
| 524 | int write_cmd = 0; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 525 | int timeout; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 526 | uint32_t temp32; |
| 527 | uint32_t a; |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 528 | uint64_t opmenu; |
| 529 | int opcode_index; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 530 | |
| 531 | /* Is it a write command? */ |
| 532 | if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) |
| 533 | || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) { |
| 534 | write_cmd = 1; |
| 535 | } |
| 536 | |
| 537 | /* Programm Offset in Flash into FADDR */ |
| 538 | REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */ |
| 539 | |
| 540 | /* Program data into FDATA0 to N */ |
| 541 | if (write_cmd && (datalength != 0)) { |
| 542 | temp32 = 0; |
| 543 | for (a = 0; a < datalength; a++) { |
| 544 | if ((a % 4) == 0) { |
| 545 | temp32 = 0; |
| 546 | } |
| 547 | |
| 548 | temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8); |
| 549 | |
| 550 | if ((a % 4) == 3) { |
| 551 | REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)), |
| 552 | temp32); |
| 553 | } |
| 554 | } |
| 555 | if (((a - 1) % 4) != 3) { |
| 556 | REGWRITE32(ICH9_REG_FDATA0 + |
| 557 | ((a - 1) - ((a - 1) % 4)), temp32); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 558 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /* Assemble SSFS + SSFC */ |
| 562 | temp32 = 0; |
| 563 | |
| 564 | /* clear error status registers */ |
| 565 | temp32 |= (SSFS_CDS + SSFS_FCERR); |
Uwe Hermann | 4e3d0b3 | 2010-03-25 23:18:41 +0000 | [diff] [blame] | 566 | /* Use 20 MHz */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 567 | temp32 |= SSFC_SCF_20MHZ; |
| 568 | |
| 569 | if (datalength != 0) { |
| 570 | uint32_t datatemp; |
| 571 | temp32 |= SSFC_DS; |
| 572 | datatemp = ((uint32_t) ((datalength - 1) & 0x3f)) << (8 + 8); |
| 573 | temp32 |= datatemp; |
| 574 | } |
| 575 | |
| 576 | /* Select opcode */ |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 577 | opmenu = REGREAD32(ICH9_REG_OPMENU); |
| 578 | opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32; |
| 579 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 580 | for (opcode_index = 0; opcode_index < 8; opcode_index++) { |
| 581 | if ((opmenu & 0xff) == op.opcode) { |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 582 | break; |
| 583 | } |
| 584 | opmenu >>= 8; |
| 585 | } |
| 586 | if (opcode_index == 8) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 587 | msg_pdbg("Opcode %x not found.\n", op.opcode); |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 588 | return 1; |
| 589 | } |
| 590 | temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 591 | |
| 592 | /* Handle Atomic */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 593 | switch (op.atomic) { |
| 594 | case 2: |
| 595 | /* Select second preop. */ |
| 596 | temp32 |= SSFC_SPOP; |
| 597 | /* And fall through. */ |
| 598 | case 1: |
| 599 | /* Atomic command (preop+op) */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 600 | temp32 |= SSFC_ACS; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 601 | break; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* Start */ |
| 605 | temp32 |= SSFC_SCGO; |
| 606 | |
| 607 | /* write it */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 608 | REGWRITE32(ICH9_REG_SSFS, temp32); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 609 | |
| 610 | /*wait for cycle complete */ |
Carl-Daniel Hailfinger | 4c24ad4 | 2009-05-09 07:24:23 +0000 | [diff] [blame] | 611 | timeout = 100 * 1000 * 60; // 60s is a looong timeout. |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 612 | while (((REGREAD32(ICH9_REG_SSFS) & SSFS_CDS) == 0) && --timeout) { |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 613 | programmer_delay(10); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 614 | } |
| 615 | if (!timeout) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 616 | msg_perr("timeout\n"); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 619 | /* FIXME make sure we do not needlessly cause transaction errors. */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 620 | if ((REGREAD32(ICH9_REG_SSFS) & SSFS_FCERR) != 0) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 621 | msg_pdbg("Transaction error!\n"); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 622 | return 1; |
| 623 | } |
| 624 | |
| 625 | if ((!write_cmd) && (datalength != 0)) { |
| 626 | for (a = 0; a < datalength; a++) { |
| 627 | if ((a % 4) == 0) { |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 628 | temp32 = REGREAD32(ICH9_REG_FDATA0 + (a)); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | data[a] = |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 632 | (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8))) |
| 633 | >> ((a % 4) * 8); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 640 | static int run_opcode(OPCODE op, uint32_t offset, |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 641 | uint8_t datalength, uint8_t * data) |
| 642 | { |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 643 | switch (spi_controller) { |
| 644 | case SPI_CONTROLLER_VIA: |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 645 | if (datalength > 16) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 646 | msg_perr("%s: Internal command size error for " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 647 | "opcode 0x%02x, got datalength=%i, want <=16\n", |
| 648 | __func__, op.opcode, datalength); |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 649 | return SPI_INVALID_LENGTH; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 650 | } |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 651 | return ich7_run_opcode(op, offset, datalength, data, 16); |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 652 | case SPI_CONTROLLER_ICH7: |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 653 | if (datalength > 64) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 654 | msg_perr("%s: Internal command size error for " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 655 | "opcode 0x%02x, got datalength=%i, want <=16\n", |
| 656 | __func__, op.opcode, datalength); |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 657 | return SPI_INVALID_LENGTH; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 658 | } |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 659 | return ich7_run_opcode(op, offset, datalength, data, 64); |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 660 | case SPI_CONTROLLER_ICH9: |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 661 | if (datalength > 64) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 662 | msg_perr("%s: Internal command size error for " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 663 | "opcode 0x%02x, got datalength=%i, want <=16\n", |
| 664 | __func__, op.opcode, datalength); |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 665 | return SPI_INVALID_LENGTH; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 666 | } |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 667 | return ich9_run_opcode(op, offset, datalength, data); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 668 | default: |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 669 | msg_perr("%s: unsupported chipset\n", __func__); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 670 | } |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 671 | |
| 672 | /* If we ever get here, something really weird happened */ |
| 673 | return -1; |
| 674 | } |
| 675 | |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 676 | 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] | 677 | { |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 678 | int maxdata = 64; |
| 679 | |
Carl-Daniel Hailfinger | 38a059d | 2009-06-13 12:04:03 +0000 | [diff] [blame] | 680 | if (spi_controller == SPI_CONTROLLER_VIA) |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 681 | maxdata = 16; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 682 | |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 683 | return spi_read_chunked(flash, buf, start, len, maxdata); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 686 | int ich_spi_write_256(struct flashchip *flash, uint8_t * buf) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 687 | { |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 688 | int i, ret = 0; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 689 | int total_size = flash->total_size * 1024; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 690 | int erase_size = 64 * 1024; |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 691 | int maxdata = 64; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 692 | |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 693 | if (spi_controller == SPI_CONTROLLER_VIA) |
| 694 | maxdata = 16; |
| 695 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 696 | spi_disable_blockprotect(); |
Carl-Daniel Hailfinger | 9612303 | 2009-11-25 02:07:30 +0000 | [diff] [blame] | 697 | /* Erase first */ |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 698 | msg_pinfo("Erasing flash before programming... "); |
Carl-Daniel Hailfinger | 9612303 | 2009-11-25 02:07:30 +0000 | [diff] [blame] | 699 | if (erase_flash(flash)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 700 | msg_perr("ERASE FAILED!\n"); |
Carl-Daniel Hailfinger | 9612303 | 2009-11-25 02:07:30 +0000 | [diff] [blame] | 701 | return -1; |
| 702 | } |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 703 | msg_pinfo("done.\n"); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 704 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 705 | msg_pinfo("Programming page: \n"); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 706 | for (i = 0; i < total_size / erase_size; i++) { |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 707 | ret = spi_write_chunked(flash, buf + (i * erase_size), |
| 708 | i * erase_size, erase_size, maxdata); |
| 709 | if (ret) |
| 710 | break; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 713 | msg_pinfo("\n"); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 714 | |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 715 | return ret; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 718 | int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 719 | const unsigned char *writearr, unsigned char *readarr) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 720 | { |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 721 | int result; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 722 | int opcode_index = -1; |
| 723 | const unsigned char cmd = *writearr; |
| 724 | OPCODE *opcode; |
| 725 | uint32_t addr = 0; |
| 726 | uint8_t *data; |
| 727 | int count; |
| 728 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 729 | /* find cmd in opcodes-table */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 730 | opcode_index = find_opcode(curopcodes, cmd); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 731 | if (opcode_index == -1) { |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 732 | /* FIXME: Reprogram opcodes if possible. Autodetect type of |
| 733 | * opcode by checking readcnt/writecnt. |
| 734 | */ |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 735 | msg_pdbg("Invalid OPCODE 0x%02x\n", cmd); |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 736 | return SPI_INVALID_OPCODE; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | opcode = &(curopcodes->opcode[opcode_index]); |
| 740 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 741 | /* The following valid writecnt/readcnt combinations exist: |
| 742 | * writecnt = 4, readcnt >= 0 |
| 743 | * writecnt = 1, readcnt >= 0 |
| 744 | * writecnt >= 4, readcnt = 0 |
| 745 | * writecnt >= 1, readcnt = 0 |
| 746 | * writecnt >= 1 is guaranteed for all commands. |
| 747 | */ |
| 748 | if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) && |
| 749 | (writecnt != 4)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 750 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 751 | "0x%02x, got writecnt=%i, want =4\n", __func__, cmd, |
| 752 | writecnt); |
| 753 | return SPI_INVALID_LENGTH; |
| 754 | } |
| 755 | if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) && |
| 756 | (writecnt != 1)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 757 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 758 | "0x%02x, got writecnt=%i, want =1\n", __func__, cmd, |
| 759 | writecnt); |
| 760 | return SPI_INVALID_LENGTH; |
| 761 | } |
| 762 | if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) && |
| 763 | (writecnt < 4)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 764 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 765 | "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd, |
| 766 | writecnt); |
| 767 | return SPI_INVALID_LENGTH; |
| 768 | } |
| 769 | if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) || |
| 770 | (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) && |
| 771 | (readcnt)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 772 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 773 | "0x%02x, got readcnt=%i, want =0\n", __func__, cmd, |
| 774 | readcnt); |
| 775 | return SPI_INVALID_LENGTH; |
| 776 | } |
| 777 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 778 | /* if opcode-type requires an address */ |
| 779 | if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS || |
| 780 | opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) { |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 781 | addr = (writearr[1] << 16) | |
| 782 | (writearr[2] << 8) | (writearr[3] << 0); |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 783 | switch (spi_controller) { |
| 784 | case SPI_CONTROLLER_ICH7: |
| 785 | case SPI_CONTROLLER_ICH9: |
| 786 | if (addr < ichspi_bbar) { |
| 787 | msg_perr("%s: Address 0x%06x below allowed " |
| 788 | "range 0x%06x-0xffffff\n", __func__, |
| 789 | addr, ichspi_bbar); |
| 790 | return SPI_INVALID_ADDRESS; |
| 791 | } |
| 792 | break; |
| 793 | default: |
| 794 | break; |
| 795 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 796 | } |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 797 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 798 | /* translate read/write array/count */ |
| 799 | if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) { |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 800 | data = (uint8_t *) (writearr + 1); |
| 801 | count = writecnt - 1; |
| 802 | } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) { |
| 803 | data = (uint8_t *) (writearr + 4); |
| 804 | count = writecnt - 4; |
| 805 | } else { |
| 806 | data = (uint8_t *) readarr; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 807 | count = readcnt; |
| 808 | } |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 809 | |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 810 | result = run_opcode(*opcode, addr, count, data); |
| 811 | if (result) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 812 | msg_pdbg("run OPCODE 0x%02x failed\n", opcode->opcode); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 815 | return result; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 816 | } |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 817 | |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 818 | int ich_spi_send_multicommand(struct spi_command *cmds) |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 819 | { |
| 820 | int ret = 0; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 821 | int i; |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 822 | int oppos, preoppos; |
| 823 | for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) { |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 824 | if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) { |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 825 | /* Next command is valid. */ |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 826 | preoppos = find_preop(curopcodes, cmds->writearr[0]); |
| 827 | oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]); |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 828 | if ((oppos == -1) && (preoppos != -1)) { |
| 829 | /* Current command is listed as preopcode in |
| 830 | * ICH struct OPCODES, but next command is not |
| 831 | * listed as opcode in that struct. |
| 832 | * Check for command sanity, then |
| 833 | * try to reprogram the ICH opcode list. |
| 834 | */ |
| 835 | if (find_preop(curopcodes, |
| 836 | (cmds + 1)->writearr[0]) != -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 837 | msg_perr("%s: Two subsequent " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 838 | "preopcodes 0x%02x and 0x%02x, " |
| 839 | "ignoring the first.\n", |
| 840 | __func__, cmds->writearr[0], |
| 841 | (cmds + 1)->writearr[0]); |
| 842 | continue; |
| 843 | } |
| 844 | /* If the chipset is locked down, we'll fail |
| 845 | * during execution of the next command anyway. |
| 846 | * No need to bother with fixups. |
| 847 | */ |
| 848 | if (!ichspi_lock) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 849 | msg_pdbg("%s: FIXME: Add on-the-fly" |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 850 | " reprogramming of the " |
| 851 | "chipset opcode list.\n", |
| 852 | __func__); |
| 853 | /* FIXME: Reprogram opcode menu. |
| 854 | * Find a less-useful opcode, replace it |
| 855 | * with the wanted opcode, detect optype |
| 856 | * and reprogram the opcode menu. |
| 857 | * Update oppos so the next if-statement |
| 858 | * can do something useful. |
| 859 | */ |
| 860 | //curopcodes.opcode[lessusefulindex] = (cmds + 1)->writearr[0]); |
| 861 | //update_optypes(curopcodes); |
| 862 | //program_opcodes(curopcodes); |
| 863 | //oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]); |
| 864 | continue; |
| 865 | } |
| 866 | } |
| 867 | if ((oppos != -1) && (preoppos != -1)) { |
| 868 | /* Current command is listed as preopcode in |
| 869 | * ICH struct OPCODES and next command is listed |
| 870 | * as opcode in that struct. Match them up. |
| 871 | */ |
| 872 | curopcodes->opcode[oppos].atomic = preoppos + 1; |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 873 | continue; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 874 | } |
| 875 | /* If none of the above if-statements about oppos or |
| 876 | * preoppos matched, this is a normal opcode. |
| 877 | */ |
| 878 | } |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 879 | ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt, |
| 880 | cmds->writearr, cmds->readarr); |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 881 | /* Reset the type of all opcodes to non-atomic. */ |
| 882 | for (i = 0; i < 8; i++) |
| 883 | curopcodes->opcode[i].atomic = 0; |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 884 | } |
| 885 | return ret; |
| 886 | } |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 887 | |
| 888 | #endif |