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