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