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