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