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