Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2008 Stefan Wildemann <stefan.wildemann@kontron.com> |
| 5 | * Copyright (C) 2008 Claus Gindhart <claus.gindhart@kontron.com> |
| 6 | * Copyright (C) 2008 Dominik Geyer <dominik.geyer@kontron.com> |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 7 | * Copyright (C) 2008 coresystems GmbH <info@coresystems.de> |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 8 | * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * This module is designed for supporting the devices |
| 27 | * ST M25P40 |
| 28 | * ST M25P80 |
| 29 | * ST M25P16 |
| 30 | * ST M25P32 already tested |
| 31 | * ST M25P64 |
| 32 | * AT 25DF321 already tested |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame] | 33 | * ... and many more SPI flash devices |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 34 | * |
| 35 | */ |
| 36 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 37 | #if defined(__i386__) || defined(__x86_64__) |
| 38 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 39 | #include <string.h> |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 40 | #include "flash.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 41 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 42 | #include "programmer.h" |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 43 | #include "spi.h" |
| 44 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 45 | /* ICH9 controller register definition */ |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 46 | #define ICH9_REG_FADDR 0x08 /* 32 Bits */ |
| 47 | #define ICH9_REG_FDATA0 0x10 /* 64 Bytes */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 48 | |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 49 | #define ICH9_REG_SSFS 0x90 /* 08 Bits */ |
Stefan Tauner | 0c1ec45 | 2011-06-11 09:53:09 +0000 | [diff] [blame^] | 50 | #define SSFS_SCIP_OFF 0 /* SPI Cycle In Progress */ |
| 51 | #define SSFS_SCIP (0x1 << SSFS_SCIP_OFF) |
| 52 | #define SSFS_FDONE_OFF 2 /* Cycle Done Status */ |
| 53 | #define SSFS_FDONE (0x1 << SSFS_FDONE_OFF) |
| 54 | #define SSFS_FCERR_OFF 3 /* Flash Cycle Error */ |
| 55 | #define SSFS_FCERR (0x1 << SSFS_FCERR_OFF) |
| 56 | #define SSFS_AEL_OFF 4 /* Access Error Log */ |
| 57 | #define SSFS_AEL (0x1 << SSFS_AEL_OFF) |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 58 | /* The following bits are reserved in SSFS: 1,5-7. */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 59 | #define SSFS_RESERVED_MASK 0x000000e2 |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 60 | |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 61 | #define ICH9_REG_SSFC 0x91 /* 24 Bits */ |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 62 | /* We combine SSFS and SSFC to one 32-bit word, |
Stefan Tauner | 0c1ec45 | 2011-06-11 09:53:09 +0000 | [diff] [blame^] | 63 | * therefore SSFC bits are off by 8. */ |
| 64 | /* 0: reserved */ |
| 65 | #define SSFC_SCGO_OFF (1 + 8) /* 1: SPI Cycle Go */ |
| 66 | #define SSFC_SCGO (0x1 << SSFC_SCGO_OFF) |
| 67 | #define SSFC_ACS_OFF (2 + 8) /* 2: Atomic Cycle Sequence */ |
| 68 | #define SSFC_ACS (0x1 << SSFC_ACS_OFF) |
| 69 | #define SSFC_SPOP_OFF (3 + 8) /* 3: Sequence Prefix Opcode Pointer */ |
| 70 | #define SSFC_SPOP (0x1 << SSFC_SPOP_OFF) |
| 71 | #define SSFC_COP_OFF (4 + 8) /* 4-6: Cycle Opcode Pointer */ |
| 72 | #define SSFC_COP (0x7 << SSFC_COP_OFF) |
| 73 | /* 7: reserved */ |
| 74 | #define SSFC_DBC_OFF (8 + 8) /* 8-13: Data Byte Count */ |
| 75 | #define SSFC_DBC (0x3f << SSFC_DBC_OFF) |
| 76 | #define SSFC_DS_OFF (14 + 8) /* 14: Data Cycle */ |
| 77 | #define SSFC_DS (0x1 << SSFC_DS_OFF) |
| 78 | #define SSFC_SME_OFF (15 + 8) /* 15: SPI SMI# Enable */ |
| 79 | #define SSFC_SME (0x1 << SSFC_SME_OFF) |
| 80 | #define SSFC_SCF_OFF (16 + 8) /* 16-18: SPI Cycle Frequency */ |
| 81 | #define SSFC_SCF (0x7 << SSFC_SCF_OFF) |
| 82 | #define SSFC_SCF_20MHZ 0x00000000 |
| 83 | #define SSFC_SCF_33MHZ 0x01000000 |
| 84 | /* 19-23: reserved */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 85 | #define SSFC_RESERVED_MASK 0xf8008100 |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 86 | |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 87 | #define ICH9_REG_PREOP 0x94 /* 16 Bits */ |
| 88 | #define ICH9_REG_OPTYPE 0x96 /* 16 Bits */ |
| 89 | #define ICH9_REG_OPMENU 0x98 /* 64 Bits */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 90 | |
| 91 | // ICH9R SPI commands |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 92 | #define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0 |
| 93 | #define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1 |
| 94 | #define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2 |
| 95 | #define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3 |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 96 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 97 | // ICH7 registers |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 98 | #define ICH7_REG_SPIS 0x00 /* 16 Bits */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 99 | #define SPIS_SCIP 0x0001 |
| 100 | #define SPIS_GRANT 0x0002 |
| 101 | #define SPIS_CDS 0x0004 |
| 102 | #define SPIS_FCERR 0x0008 |
| 103 | #define SPIS_RESERVED_MASK 0x7ff0 |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 104 | |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 105 | /* VIA SPI is compatible with ICH7, but maxdata |
| 106 | to transfer is 16 bytes. |
| 107 | |
| 108 | DATA byte count on ICH7 is 8:13, on VIA 8:11 |
| 109 | |
| 110 | bit 12 is port select CS0 CS1 |
| 111 | bit 13 is FAST READ enable |
| 112 | bit 7 is used with fast read and one shot controls CS de-assert? |
| 113 | */ |
| 114 | |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 115 | #define ICH7_REG_SPIC 0x02 /* 16 Bits */ |
| 116 | #define SPIC_SCGO 0x0002 |
| 117 | #define SPIC_ACS 0x0004 |
| 118 | #define SPIC_SPOP 0x0008 |
| 119 | #define SPIC_DS 0x4000 |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 120 | |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 121 | #define ICH7_REG_SPIA 0x04 /* 32 Bits */ |
| 122 | #define ICH7_REG_SPID0 0x08 /* 64 Bytes */ |
| 123 | #define ICH7_REG_PREOP 0x54 /* 16 Bits */ |
| 124 | #define ICH7_REG_OPTYPE 0x56 /* 16 Bits */ |
| 125 | #define ICH7_REG_OPMENU 0x58 /* 64 Bits */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 126 | |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 127 | /* ICH SPI configuration lock-down. May be set during chipset enabling. */ |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 128 | static int ichspi_lock = 0; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 129 | |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 130 | uint32_t ichspi_bbar = 0; |
| 131 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 132 | static void *ich_spibar = NULL; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 133 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 134 | typedef struct _OPCODE { |
| 135 | uint8_t opcode; //This commands spi opcode |
| 136 | uint8_t spi_type; //This commands spi type |
| 137 | uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1 |
| 138 | } OPCODE; |
| 139 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 140 | /* Suggested opcode definition: |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 141 | * Preop 1: Write Enable |
| 142 | * Preop 2: Write Status register enable |
| 143 | * |
| 144 | * OP 0: Write address |
| 145 | * OP 1: Read Address |
| 146 | * OP 2: ERASE block |
| 147 | * OP 3: Read Status register |
| 148 | * OP 4: Read ID |
| 149 | * OP 5: Write Status register |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 150 | * OP 6: chip private (read JEDEC id) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 151 | * OP 7: Chip erase |
| 152 | */ |
| 153 | typedef struct _OPCODES { |
| 154 | uint8_t preop[2]; |
| 155 | OPCODE opcode[8]; |
| 156 | } OPCODES; |
| 157 | |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 158 | static OPCODES *curopcodes = NULL; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 159 | |
| 160 | /* HW access functions */ |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 161 | static uint32_t REGREAD32(int X) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 162 | { |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 163 | return mmio_readl(ich_spibar + X); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 166 | static uint16_t REGREAD16(int X) |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 167 | { |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 168 | return mmio_readw(ich_spibar + X); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 171 | static uint16_t REGREAD8(int X) |
| 172 | { |
| 173 | return mmio_readb(ich_spibar + X); |
| 174 | } |
| 175 | |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 176 | #define REGWRITE32(off,val) mmio_writel(val, ich_spibar+off) |
| 177 | #define REGWRITE16(off,val) mmio_writew(val, ich_spibar+off) |
| 178 | #define REGWRITE8(off,val) mmio_writeb(val, ich_spibar+off) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 179 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 180 | /* Common SPI functions */ |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 181 | static int find_opcode(OPCODES *op, uint8_t opcode); |
| 182 | static int find_preop(OPCODES *op, uint8_t preop); |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 183 | static int generate_opcodes(OPCODES * op); |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 184 | static int program_opcodes(OPCODES *op, int enable_undo); |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 185 | static int run_opcode(OPCODE op, uint32_t offset, |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 186 | uint8_t datalength, uint8_t * data); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 187 | |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 188 | /* for pairing opcodes with their required preop */ |
| 189 | struct preop_opcode_pair { |
| 190 | uint8_t preop; |
| 191 | uint8_t opcode; |
| 192 | }; |
| 193 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 194 | /* List of opcodes which need preopcodes and matching preopcodes. Unused. */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 195 | const struct preop_opcode_pair pops[] = { |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 196 | {JEDEC_WREN, JEDEC_BYTE_PROGRAM}, |
| 197 | {JEDEC_WREN, JEDEC_SE}, /* sector erase */ |
| 198 | {JEDEC_WREN, JEDEC_BE_52}, /* block erase */ |
| 199 | {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */ |
| 200 | {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */ |
| 201 | {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 202 | /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */ |
| 203 | {JEDEC_WREN, JEDEC_WRSR}, |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 204 | {JEDEC_EWSR, JEDEC_WRSR}, |
| 205 | {0,} |
| 206 | }; |
| 207 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 208 | /* Reasonable default configuration. Needs ad-hoc modifications if we |
| 209 | * encounter unlisted opcodes. Fun. |
| 210 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 211 | static OPCODES O_ST_M25P = { |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 212 | { |
| 213 | JEDEC_WREN, |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 214 | JEDEC_EWSR, |
| 215 | }, |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 216 | { |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 217 | {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 218 | {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 219 | {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 220 | {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] | 221 | {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 222 | {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 223 | {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 224 | {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase |
| 225 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame] | 228 | /* List of opcodes with their corresponding spi_type |
| 229 | * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode |
| 230 | * is needed which is currently not in the chipset OPCODE table |
| 231 | */ |
| 232 | static OPCODE POSSIBLE_OPCODES[] = { |
| 233 | {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte |
| 234 | {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data |
| 235 | {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector |
| 236 | {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg |
| 237 | {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature |
| 238 | {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register |
| 239 | {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID |
| 240 | {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase |
| 241 | {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase |
| 242 | {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase |
| 243 | {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment |
| 244 | }; |
| 245 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 246 | static OPCODES O_EXISTING = {}; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 247 | |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame] | 248 | static uint8_t lookup_spi_type(uint8_t opcode) |
| 249 | { |
| 250 | int a; |
| 251 | |
| 252 | for (a = 0; a < sizeof(POSSIBLE_OPCODES)/sizeof(POSSIBLE_OPCODES[0]); a++) { |
| 253 | if (POSSIBLE_OPCODES[a].opcode == opcode) |
| 254 | return POSSIBLE_OPCODES[a].spi_type; |
| 255 | } |
| 256 | |
| 257 | return 0xFF; |
| 258 | } |
| 259 | |
| 260 | static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt) |
| 261 | { |
| 262 | uint8_t spi_type; |
| 263 | |
| 264 | spi_type = lookup_spi_type(opcode); |
| 265 | if (spi_type > 3) { |
| 266 | /* Try to guess spi type from read/write sizes. |
| 267 | * The following valid writecnt/readcnt combinations exist: |
| 268 | * writecnt = 4, readcnt >= 0 |
| 269 | * writecnt = 1, readcnt >= 0 |
| 270 | * writecnt >= 4, readcnt = 0 |
| 271 | * writecnt >= 1, readcnt = 0 |
| 272 | * writecnt >= 1 is guaranteed for all commands. |
| 273 | */ |
| 274 | if (readcnt == 0) |
| 275 | /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS |
| 276 | * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data |
| 277 | * bytes are actual the address, they go to the bus anyhow |
| 278 | */ |
| 279 | spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS; |
| 280 | else if (writecnt == 1) // and readcnt is > 0 |
| 281 | spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS; |
| 282 | else if (writecnt == 4) // and readcnt is > 0 |
| 283 | spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS; |
| 284 | // else we have an invalid case, will be handled below |
| 285 | } |
| 286 | if (spi_type <= 3) { |
| 287 | int oppos=2; // use original JEDEC_BE_D8 offset |
| 288 | curopcodes->opcode[oppos].opcode = opcode; |
| 289 | curopcodes->opcode[oppos].spi_type = spi_type; |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 290 | program_opcodes(curopcodes, 0); |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame] | 291 | oppos = find_opcode(curopcodes, opcode); |
| 292 | msg_pdbg ("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos); |
| 293 | return oppos; |
| 294 | } |
| 295 | return -1; |
| 296 | } |
| 297 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 298 | static int find_opcode(OPCODES *op, uint8_t opcode) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 299 | { |
| 300 | int a; |
| 301 | |
| 302 | for (a = 0; a < 8; a++) { |
| 303 | if (op->opcode[a].opcode == opcode) |
| 304 | return a; |
| 305 | } |
| 306 | |
| 307 | return -1; |
| 308 | } |
| 309 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 310 | static int find_preop(OPCODES *op, uint8_t preop) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 311 | { |
| 312 | int a; |
| 313 | |
| 314 | for (a = 0; a < 2; a++) { |
| 315 | if (op->preop[a] == preop) |
| 316 | return a; |
| 317 | } |
| 318 | |
| 319 | return -1; |
| 320 | } |
| 321 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 322 | /* Create a struct OPCODES based on what we find in the locked down chipset. */ |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 323 | static int generate_opcodes(OPCODES * op) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 324 | { |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 325 | int a; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 326 | uint16_t preop, optype; |
| 327 | uint32_t opmenu[2]; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 328 | |
| 329 | if (op == NULL) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 330 | msg_perr("\n%s: null OPCODES pointer!\n", __func__); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 331 | return -1; |
| 332 | } |
| 333 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 334 | switch (spi_programmer->type) { |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 335 | case SPI_CONTROLLER_ICH7: |
| 336 | case SPI_CONTROLLER_VIA: |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 337 | preop = REGREAD16(ICH7_REG_PREOP); |
| 338 | optype = REGREAD16(ICH7_REG_OPTYPE); |
| 339 | opmenu[0] = REGREAD32(ICH7_REG_OPMENU); |
| 340 | opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4); |
| 341 | break; |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 342 | case SPI_CONTROLLER_ICH9: |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 343 | preop = REGREAD16(ICH9_REG_PREOP); |
| 344 | optype = REGREAD16(ICH9_REG_OPTYPE); |
| 345 | opmenu[0] = REGREAD32(ICH9_REG_OPMENU); |
| 346 | opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4); |
| 347 | break; |
| 348 | default: |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 349 | msg_perr("%s: unsupported chipset\n", __func__); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 350 | return -1; |
| 351 | } |
| 352 | |
| 353 | op->preop[0] = (uint8_t) preop; |
| 354 | op->preop[1] = (uint8_t) (preop >> 8); |
| 355 | |
| 356 | for (a = 0; a < 8; a++) { |
| 357 | op->opcode[a].spi_type = (uint8_t) (optype & 0x3); |
| 358 | optype >>= 2; |
| 359 | } |
| 360 | |
| 361 | for (a = 0; a < 4; a++) { |
| 362 | op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff); |
| 363 | opmenu[0] >>= 8; |
| 364 | } |
| 365 | |
| 366 | for (a = 4; a < 8; a++) { |
| 367 | op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff); |
| 368 | opmenu[1] >>= 8; |
| 369 | } |
| 370 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 371 | /* No preopcodes used by default. */ |
| 372 | for (a = 0; a < 8; a++) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 373 | op->opcode[a].atomic = 0; |
| 374 | |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 375 | return 0; |
| 376 | } |
| 377 | |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 378 | static int program_opcodes(OPCODES *op, int enable_undo) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 379 | { |
| 380 | uint8_t a; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 381 | uint16_t preop, optype; |
| 382 | uint32_t opmenu[2]; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 383 | |
| 384 | /* Program Prefix Opcodes */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 385 | /* 0:7 Prefix Opcode 1 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 386 | preop = (op->preop[0]); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 387 | /* 8:16 Prefix Opcode 2 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 388 | preop |= ((uint16_t) op->preop[1]) << 8; |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 389 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 390 | /* Program Opcode Types 0 - 7 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 391 | optype = 0; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 392 | for (a = 0; a < 8; a++) { |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 393 | optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 394 | } |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 395 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 396 | /* Program Allowable Opcodes 0 - 3 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 397 | opmenu[0] = 0; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 398 | for (a = 0; a < 4; a++) { |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 399 | opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 400 | } |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 401 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 402 | /*Program Allowable Opcodes 4 - 7 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 403 | opmenu[1] = 0; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 404 | for (a = 4; a < 8; a++) { |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 405 | opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 406 | } |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 407 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 408 | msg_pdbg("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]); |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 409 | switch (spi_programmer->type) { |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 410 | case SPI_CONTROLLER_ICH7: |
| 411 | case SPI_CONTROLLER_VIA: |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 412 | /* Register undo only for enable_undo=1, i.e. first call. */ |
| 413 | if (enable_undo) { |
| 414 | rmmio_valw(ich_spibar + ICH7_REG_PREOP); |
| 415 | rmmio_valw(ich_spibar + ICH7_REG_OPTYPE); |
| 416 | rmmio_vall(ich_spibar + ICH7_REG_OPMENU); |
| 417 | rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4); |
| 418 | } |
| 419 | mmio_writew(preop, ich_spibar + ICH7_REG_PREOP); |
| 420 | mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE); |
| 421 | mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU); |
| 422 | mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 423 | break; |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 424 | case SPI_CONTROLLER_ICH9: |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 425 | /* Register undo only for enable_undo=1, i.e. first call. */ |
| 426 | if (enable_undo) { |
| 427 | rmmio_valw(ich_spibar + ICH9_REG_PREOP); |
| 428 | rmmio_valw(ich_spibar + ICH9_REG_OPTYPE); |
| 429 | rmmio_vall(ich_spibar + ICH9_REG_OPMENU); |
| 430 | rmmio_vall(ich_spibar + ICH9_REG_OPMENU + 4); |
| 431 | } |
| 432 | mmio_writew(preop, ich_spibar + ICH9_REG_PREOP); |
| 433 | mmio_writew(optype, ich_spibar + ICH9_REG_OPTYPE); |
| 434 | mmio_writel(opmenu[0], ich_spibar + ICH9_REG_OPMENU); |
| 435 | mmio_writel(opmenu[1], ich_spibar + ICH9_REG_OPMENU + 4); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 436 | break; |
| 437 | default: |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 438 | msg_perr("%s: unsupported chipset\n", __func__); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 439 | return -1; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 440 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 445 | /* |
| 446 | * Try to set BBAR (BIOS Base Address Register), but read back the value in case |
| 447 | * it didn't stick. |
| 448 | */ |
| 449 | void ich_set_bbar(uint32_t minaddr) |
| 450 | { |
Carl-Daniel Hailfinger | 841d631 | 2010-11-24 23:37:22 +0000 | [diff] [blame] | 451 | #define BBAR_MASK 0x00ffff00 |
| 452 | minaddr &= BBAR_MASK; |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 453 | switch (spi_programmer->type) { |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 454 | case SPI_CONTROLLER_ICH7: |
Carl-Daniel Hailfinger | 841d631 | 2010-11-24 23:37:22 +0000 | [diff] [blame] | 455 | case SPI_CONTROLLER_VIA: |
| 456 | ichspi_bbar = mmio_readl(ich_spibar + 0x50) & ~BBAR_MASK; |
| 457 | if (ichspi_bbar) |
| 458 | msg_pdbg("Reserved bits in BBAR not zero: 0x%04x", |
| 459 | ichspi_bbar); |
| 460 | ichspi_bbar |= minaddr; |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 461 | rmmio_writel(ichspi_bbar, ich_spibar + 0x50); |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 462 | ichspi_bbar = mmio_readl(ich_spibar + 0x50); |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 463 | /* We don't have any option except complaining. And if the write |
| 464 | * failed, the restore will fail as well, so no problem there. |
| 465 | */ |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 466 | if (ichspi_bbar != minaddr) |
| 467 | msg_perr("Setting BBAR failed!\n"); |
| 468 | break; |
| 469 | case SPI_CONTROLLER_ICH9: |
Carl-Daniel Hailfinger | 841d631 | 2010-11-24 23:37:22 +0000 | [diff] [blame] | 470 | ichspi_bbar = mmio_readl(ich_spibar + 0xA0) & ~BBAR_MASK; |
| 471 | if (ichspi_bbar) |
| 472 | msg_pdbg("Reserved bits in BBAR not zero: 0x%04x", |
| 473 | ichspi_bbar); |
| 474 | ichspi_bbar |= minaddr; |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 475 | rmmio_writel(ichspi_bbar, ich_spibar + 0xA0); |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 476 | ichspi_bbar = mmio_readl(ich_spibar + 0xA0); |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 477 | /* We don't have any option except complaining. And if the write |
| 478 | * failed, the restore will fail as well, so no problem there. |
| 479 | */ |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 480 | if (ichspi_bbar != minaddr) |
| 481 | msg_perr("Setting BBAR failed!\n"); |
| 482 | break; |
| 483 | default: |
Carl-Daniel Hailfinger | 841d631 | 2010-11-24 23:37:22 +0000 | [diff] [blame] | 484 | msg_perr("Unknown chipset for BBAR setting!\n"); |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 485 | break; |
| 486 | } |
| 487 | } |
| 488 | |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 489 | /* This function generates OPCODES from or programs OPCODES to ICH according to |
| 490 | * the chipset's SPI configuration lock. |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 491 | * |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 492 | * It should be called before ICH sends any spi command. |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 493 | */ |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 494 | static int ich_init_opcodes(void) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 495 | { |
| 496 | int rc = 0; |
| 497 | OPCODES *curopcodes_done; |
| 498 | |
| 499 | if (curopcodes) |
| 500 | return 0; |
| 501 | |
| 502 | if (ichspi_lock) { |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 503 | msg_pdbg("Reading OPCODES... "); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 504 | curopcodes_done = &O_EXISTING; |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 505 | rc = generate_opcodes(curopcodes_done); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 506 | } else { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 507 | msg_pdbg("Programming OPCODES... "); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 508 | curopcodes_done = &O_ST_M25P; |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 509 | rc = program_opcodes(curopcodes_done, 1); |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 510 | /* Technically not part of opcode init, but it allows opcodes |
| 511 | * to run without transaction errors by setting the lowest |
| 512 | * allowed address to zero. |
| 513 | */ |
| 514 | ich_set_bbar(0); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | if (rc) { |
| 518 | curopcodes = NULL; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 519 | msg_perr("failed\n"); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 520 | return 1; |
| 521 | } else { |
| 522 | curopcodes = curopcodes_done; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 523 | msg_pdbg("done\n"); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 524 | return 0; |
| 525 | } |
| 526 | } |
| 527 | |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 528 | static int ich7_run_opcode(OPCODE op, uint32_t offset, |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 529 | uint8_t datalength, uint8_t * data, int maxdata) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 530 | { |
| 531 | int write_cmd = 0; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 532 | int timeout; |
Peter Stuge | 7e2c079 | 2008-06-29 01:30:41 +0000 | [diff] [blame] | 533 | uint32_t temp32 = 0; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 534 | uint16_t temp16; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 535 | uint32_t a; |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 536 | uint64_t opmenu; |
| 537 | int opcode_index; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 538 | |
| 539 | /* Is it a write command? */ |
| 540 | if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) |
| 541 | || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) { |
| 542 | write_cmd = 1; |
| 543 | } |
| 544 | |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 545 | timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */ |
| 546 | while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) { |
| 547 | programmer_delay(10); |
| 548 | } |
| 549 | if (!timeout) { |
| 550 | msg_perr("Error: SCIP never cleared!\n"); |
| 551 | return 1; |
| 552 | } |
| 553 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 554 | /* Programm Offset in Flash into FADDR */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 555 | REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 556 | |
| 557 | /* Program data into FDATA0 to N */ |
| 558 | if (write_cmd && (datalength != 0)) { |
| 559 | temp32 = 0; |
| 560 | for (a = 0; a < datalength; a++) { |
| 561 | if ((a % 4) == 0) { |
| 562 | temp32 = 0; |
| 563 | } |
| 564 | |
| 565 | temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8); |
| 566 | |
| 567 | if ((a % 4) == 3) { |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 568 | REGWRITE32(ICH7_REG_SPID0 + (a - (a % 4)), |
| 569 | temp32); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | if (((a - 1) % 4) != 3) { |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 573 | REGWRITE32(ICH7_REG_SPID0 + |
| 574 | ((a - 1) - ((a - 1) % 4)), temp32); |
| 575 | } |
| 576 | |
| 577 | } |
| 578 | |
| 579 | /* Assemble SPIS */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 580 | temp16 = REGREAD16(ICH7_REG_SPIS); |
| 581 | /* keep reserved bits */ |
| 582 | temp16 &= SPIS_RESERVED_MASK; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 583 | /* clear error status registers */ |
Stefan Tauner | 0c1ec45 | 2011-06-11 09:53:09 +0000 | [diff] [blame^] | 584 | temp16 |= (SPIS_CDS | SPIS_FCERR); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 585 | REGWRITE16(ICH7_REG_SPIS, temp16); |
| 586 | |
| 587 | /* Assemble SPIC */ |
| 588 | temp16 = 0; |
| 589 | |
| 590 | if (datalength != 0) { |
| 591 | temp16 |= SPIC_DS; |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 592 | temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /* Select opcode */ |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 596 | opmenu = REGREAD32(ICH7_REG_OPMENU); |
| 597 | opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32; |
| 598 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 599 | for (opcode_index = 0; opcode_index < 8; opcode_index++) { |
| 600 | if ((opmenu & 0xff) == op.opcode) { |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 601 | break; |
| 602 | } |
| 603 | opmenu >>= 8; |
| 604 | } |
| 605 | if (opcode_index == 8) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 606 | msg_pdbg("Opcode %x not found.\n", op.opcode); |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 607 | return 1; |
| 608 | } |
| 609 | temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 610 | |
Michael Karcher | 136125a | 2011-04-29 22:11:36 +0000 | [diff] [blame] | 611 | timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */ |
| 612 | /* Handle Atomic. Atomic commands include three steps: |
| 613 | - sending the preop (mainly EWSR or WREN) |
| 614 | - sending the main command |
| 615 | - waiting for the busy bit (WIP) to be cleared |
| 616 | This means the timeout must be sufficient for chip erase |
| 617 | of slow high-capacity chips. |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 618 | */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 619 | switch (op.atomic) { |
| 620 | case 2: |
| 621 | /* Select second preop. */ |
| 622 | temp16 |= SPIC_SPOP; |
| 623 | /* And fall through. */ |
| 624 | case 1: |
| 625 | /* Atomic command (preop+op) */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 626 | temp16 |= SPIC_ACS; |
Michael Karcher | 136125a | 2011-04-29 22:11:36 +0000 | [diff] [blame] | 627 | timeout = 100 * 1000 * 60; /* 60 seconds */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 628 | break; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /* Start */ |
| 632 | temp16 |= SPIC_SCGO; |
| 633 | |
| 634 | /* write it */ |
| 635 | REGWRITE16(ICH7_REG_SPIC, temp16); |
| 636 | |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 637 | /* Wait for Cycle Done Status or Flash Cycle Error. */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 638 | while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) && |
| 639 | --timeout) { |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 640 | programmer_delay(10); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 641 | } |
| 642 | if (!timeout) { |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 643 | msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n", |
| 644 | REGREAD16(ICH7_REG_SPIS)); |
| 645 | return 1; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 648 | /* FIXME: make sure we do not needlessly cause transaction errors. */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 649 | temp16 = REGREAD16(ICH7_REG_SPIS); |
| 650 | if (temp16 & SPIS_FCERR) { |
Stefan Tauner | 8ed2934 | 2011-04-29 23:53:09 +0000 | [diff] [blame] | 651 | msg_perr("Transaction error!\n"); |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 652 | /* keep reserved bits */ |
| 653 | temp16 &= SPIS_RESERVED_MASK; |
| 654 | REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 655 | return 1; |
| 656 | } |
| 657 | |
| 658 | if ((!write_cmd) && (datalength != 0)) { |
| 659 | for (a = 0; a < datalength; a++) { |
| 660 | if ((a % 4) == 0) { |
| 661 | temp32 = REGREAD32(ICH7_REG_SPID0 + (a)); |
| 662 | } |
| 663 | |
| 664 | data[a] = |
| 665 | (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8))) |
| 666 | >> ((a % 4) * 8); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 673 | static int ich9_run_opcode(OPCODE op, uint32_t offset, |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 674 | uint8_t datalength, uint8_t * data) |
| 675 | { |
| 676 | int write_cmd = 0; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 677 | int timeout; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 678 | uint32_t temp32; |
| 679 | uint32_t a; |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 680 | uint64_t opmenu; |
| 681 | int opcode_index; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 682 | |
| 683 | /* Is it a write command? */ |
| 684 | if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) |
| 685 | || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) { |
| 686 | write_cmd = 1; |
| 687 | } |
| 688 | |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 689 | timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */ |
| 690 | while ((REGREAD8(ICH9_REG_SSFS) & SSFS_SCIP) && --timeout) { |
| 691 | programmer_delay(10); |
| 692 | } |
| 693 | if (!timeout) { |
| 694 | msg_perr("Error: SCIP never cleared!\n"); |
| 695 | return 1; |
| 696 | } |
| 697 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 698 | /* Programm Offset in Flash into FADDR */ |
| 699 | REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */ |
| 700 | |
| 701 | /* Program data into FDATA0 to N */ |
| 702 | if (write_cmd && (datalength != 0)) { |
| 703 | temp32 = 0; |
| 704 | for (a = 0; a < datalength; a++) { |
| 705 | if ((a % 4) == 0) { |
| 706 | temp32 = 0; |
| 707 | } |
| 708 | |
| 709 | temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8); |
| 710 | |
| 711 | if ((a % 4) == 3) { |
| 712 | REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)), |
| 713 | temp32); |
| 714 | } |
| 715 | } |
| 716 | if (((a - 1) % 4) != 3) { |
| 717 | REGWRITE32(ICH9_REG_FDATA0 + |
| 718 | ((a - 1) - ((a - 1) % 4)), temp32); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 719 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /* Assemble SSFS + SSFC */ |
Helge Wagner | a319be1 | 2010-08-11 21:06:10 +0000 | [diff] [blame] | 723 | temp32 = REGREAD32(ICH9_REG_SSFS); |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 724 | /* Keep reserved bits only */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 725 | temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK; |
Stefan Tauner | 0c1ec45 | 2011-06-11 09:53:09 +0000 | [diff] [blame^] | 726 | /* Clear cycle done and cycle error status registers */ |
| 727 | temp32 |= (SSFS_FDONE | SSFS_FCERR); |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 728 | REGWRITE32(ICH9_REG_SSFS, temp32); |
| 729 | |
Uwe Hermann | 4e3d0b3 | 2010-03-25 23:18:41 +0000 | [diff] [blame] | 730 | /* Use 20 MHz */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 731 | temp32 |= SSFC_SCF_20MHZ; |
| 732 | |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 733 | /* Set data byte count (DBC) and data cycle bit (DS) */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 734 | if (datalength != 0) { |
| 735 | uint32_t datatemp; |
| 736 | temp32 |= SSFC_DS; |
Stefan Tauner | 0c1ec45 | 2011-06-11 09:53:09 +0000 | [diff] [blame^] | 737 | datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) & |
| 738 | SSFC_DBC); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 739 | temp32 |= datatemp; |
| 740 | } |
| 741 | |
| 742 | /* Select opcode */ |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 743 | opmenu = REGREAD32(ICH9_REG_OPMENU); |
| 744 | opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32; |
| 745 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 746 | for (opcode_index = 0; opcode_index < 8; opcode_index++) { |
| 747 | if ((opmenu & 0xff) == op.opcode) { |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 748 | break; |
| 749 | } |
| 750 | opmenu >>= 8; |
| 751 | } |
| 752 | if (opcode_index == 8) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 753 | msg_pdbg("Opcode %x not found.\n", op.opcode); |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 754 | return 1; |
| 755 | } |
| 756 | temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 757 | |
Michael Karcher | 136125a | 2011-04-29 22:11:36 +0000 | [diff] [blame] | 758 | timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */ |
| 759 | /* Handle Atomic. Atomic commands include three steps: |
| 760 | - sending the preop (mainly EWSR or WREN) |
| 761 | - sending the main command |
| 762 | - waiting for the busy bit (WIP) to be cleared |
| 763 | This means the timeout must be sufficient for chip erase |
| 764 | of slow high-capacity chips. |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 765 | */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 766 | switch (op.atomic) { |
| 767 | case 2: |
| 768 | /* Select second preop. */ |
| 769 | temp32 |= SSFC_SPOP; |
| 770 | /* And fall through. */ |
| 771 | case 1: |
| 772 | /* Atomic command (preop+op) */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 773 | temp32 |= SSFC_ACS; |
Michael Karcher | 136125a | 2011-04-29 22:11:36 +0000 | [diff] [blame] | 774 | timeout = 100 * 1000 * 60; /* 60 seconds */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 775 | break; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /* Start */ |
| 779 | temp32 |= SSFC_SCGO; |
| 780 | |
| 781 | /* write it */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 782 | REGWRITE32(ICH9_REG_SSFS, temp32); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 783 | |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 784 | /* Wait for Cycle Done Status or Flash Cycle Error. */ |
Stefan Tauner | 0c1ec45 | 2011-06-11 09:53:09 +0000 | [diff] [blame^] | 785 | while (((REGREAD32(ICH9_REG_SSFS) & (SSFS_FDONE | SSFS_FCERR)) == 0) && |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 786 | --timeout) { |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 787 | programmer_delay(10); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 788 | } |
| 789 | if (!timeout) { |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 790 | msg_perr("timeout, ICH9_REG_SSFS=0x%08x\n", |
| 791 | REGREAD32(ICH9_REG_SSFS)); |
| 792 | return 1; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 795 | /* FIXME make sure we do not needlessly cause transaction errors. */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 796 | temp32 = REGREAD32(ICH9_REG_SSFS); |
| 797 | if (temp32 & SSFS_FCERR) { |
Stefan Tauner | 8ed2934 | 2011-04-29 23:53:09 +0000 | [diff] [blame] | 798 | msg_perr("Transaction error!\n"); |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 799 | /* keep reserved bits */ |
| 800 | temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK; |
| 801 | /* Clear the transaction error. */ |
| 802 | REGWRITE32(ICH9_REG_SSFS, temp32 | SSFS_FCERR); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 803 | return 1; |
| 804 | } |
| 805 | |
| 806 | if ((!write_cmd) && (datalength != 0)) { |
| 807 | for (a = 0; a < datalength; a++) { |
| 808 | if ((a % 4) == 0) { |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 809 | temp32 = REGREAD32(ICH9_REG_FDATA0 + (a)); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | data[a] = |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 813 | (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8))) |
| 814 | >> ((a % 4) * 8); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
| 818 | return 0; |
| 819 | } |
| 820 | |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 821 | static int run_opcode(OPCODE op, uint32_t offset, |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 822 | uint8_t datalength, uint8_t * data) |
| 823 | { |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 824 | switch (spi_programmer->type) { |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 825 | case SPI_CONTROLLER_VIA: |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 826 | if (datalength > 16) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 827 | msg_perr("%s: Internal command size error for " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 828 | "opcode 0x%02x, got datalength=%i, want <=16\n", |
| 829 | __func__, op.opcode, datalength); |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 830 | return SPI_INVALID_LENGTH; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 831 | } |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 832 | return ich7_run_opcode(op, offset, datalength, data, 16); |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 833 | case SPI_CONTROLLER_ICH7: |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 834 | if (datalength > 64) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 835 | msg_perr("%s: Internal command size error for " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 836 | "opcode 0x%02x, got datalength=%i, want <=16\n", |
| 837 | __func__, op.opcode, datalength); |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 838 | return SPI_INVALID_LENGTH; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 839 | } |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 840 | return ich7_run_opcode(op, offset, datalength, data, 64); |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 841 | case SPI_CONTROLLER_ICH9: |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 842 | if (datalength > 64) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 843 | msg_perr("%s: Internal command size error for " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 844 | "opcode 0x%02x, got datalength=%i, want <=16\n", |
| 845 | __func__, op.opcode, datalength); |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 846 | return SPI_INVALID_LENGTH; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 847 | } |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 848 | return ich9_run_opcode(op, offset, datalength, data); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 849 | default: |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 850 | msg_perr("%s: unsupported chipset\n", __func__); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 851 | } |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 852 | |
| 853 | /* If we ever get here, something really weird happened */ |
| 854 | return -1; |
| 855 | } |
| 856 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 857 | static int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 858 | const unsigned char *writearr, unsigned char *readarr) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 859 | { |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 860 | int result; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 861 | int opcode_index = -1; |
| 862 | const unsigned char cmd = *writearr; |
| 863 | OPCODE *opcode; |
| 864 | uint32_t addr = 0; |
| 865 | uint8_t *data; |
| 866 | int count; |
| 867 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 868 | /* find cmd in opcodes-table */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 869 | opcode_index = find_opcode(curopcodes, cmd); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 870 | if (opcode_index == -1) { |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame] | 871 | if (!ichspi_lock) |
| 872 | opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt); |
| 873 | if (opcode_index == -1) { |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 874 | msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n", |
| 875 | cmd); |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame] | 876 | return SPI_INVALID_OPCODE; |
| 877 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | opcode = &(curopcodes->opcode[opcode_index]); |
| 881 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 882 | /* The following valid writecnt/readcnt combinations exist: |
| 883 | * writecnt = 4, readcnt >= 0 |
| 884 | * writecnt = 1, readcnt >= 0 |
| 885 | * writecnt >= 4, readcnt = 0 |
| 886 | * writecnt >= 1, readcnt = 0 |
| 887 | * writecnt >= 1 is guaranteed for all commands. |
| 888 | */ |
| 889 | if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) && |
| 890 | (writecnt != 4)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 891 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 892 | "0x%02x, got writecnt=%i, want =4\n", __func__, cmd, |
| 893 | writecnt); |
| 894 | return SPI_INVALID_LENGTH; |
| 895 | } |
| 896 | if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) && |
| 897 | (writecnt != 1)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 898 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 899 | "0x%02x, got writecnt=%i, want =1\n", __func__, cmd, |
| 900 | writecnt); |
| 901 | return SPI_INVALID_LENGTH; |
| 902 | } |
| 903 | if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) && |
| 904 | (writecnt < 4)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 905 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 906 | "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd, |
| 907 | writecnt); |
| 908 | return SPI_INVALID_LENGTH; |
| 909 | } |
| 910 | if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) || |
| 911 | (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) && |
| 912 | (readcnt)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 913 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 914 | "0x%02x, got readcnt=%i, want =0\n", __func__, cmd, |
| 915 | readcnt); |
| 916 | return SPI_INVALID_LENGTH; |
| 917 | } |
| 918 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 919 | /* if opcode-type requires an address */ |
| 920 | if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS || |
| 921 | opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) { |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 922 | addr = (writearr[1] << 16) | |
| 923 | (writearr[2] << 8) | (writearr[3] << 0); |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 924 | switch (spi_programmer->type) { |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 925 | case SPI_CONTROLLER_ICH7: |
Carl-Daniel Hailfinger | 841d631 | 2010-11-24 23:37:22 +0000 | [diff] [blame] | 926 | case SPI_CONTROLLER_VIA: |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 927 | case SPI_CONTROLLER_ICH9: |
| 928 | if (addr < ichspi_bbar) { |
| 929 | msg_perr("%s: Address 0x%06x below allowed " |
| 930 | "range 0x%06x-0xffffff\n", __func__, |
| 931 | addr, ichspi_bbar); |
| 932 | return SPI_INVALID_ADDRESS; |
| 933 | } |
| 934 | break; |
| 935 | default: |
| 936 | break; |
| 937 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 938 | } |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 939 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 940 | /* translate read/write array/count */ |
| 941 | if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) { |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 942 | data = (uint8_t *) (writearr + 1); |
| 943 | count = writecnt - 1; |
| 944 | } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) { |
| 945 | data = (uint8_t *) (writearr + 4); |
| 946 | count = writecnt - 4; |
| 947 | } else { |
| 948 | data = (uint8_t *) readarr; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 949 | count = readcnt; |
| 950 | } |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 951 | |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 952 | result = run_opcode(*opcode, addr, count, data); |
| 953 | if (result) { |
Stefan Tauner | 8ed2934 | 2011-04-29 23:53:09 +0000 | [diff] [blame] | 954 | msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode); |
| 955 | if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) || |
| 956 | (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) { |
| 957 | msg_pdbg("at address 0x%06x ", addr); |
| 958 | } |
| 959 | msg_pdbg("(payload length was %d).\n", count); |
| 960 | |
| 961 | /* Print out the data array if it contains data to write. |
| 962 | * Errors are detected before the received data is read back into |
| 963 | * the array so it won't make sense to print it then. */ |
| 964 | if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) || |
| 965 | (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) { |
| 966 | int i; |
| 967 | msg_pspew("The data was:\n"); |
| 968 | for(i=0; i<count; i++){ |
| 969 | msg_pspew("%3d: 0x%02x\n", i, data[i]); |
| 970 | } |
| 971 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 974 | return result; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 975 | } |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 976 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 977 | static int ich_spi_send_multicommand(struct spi_command *cmds) |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 978 | { |
| 979 | int ret = 0; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 980 | int i; |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 981 | int oppos, preoppos; |
| 982 | for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) { |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 983 | if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) { |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 984 | /* Next command is valid. */ |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 985 | preoppos = find_preop(curopcodes, cmds->writearr[0]); |
| 986 | oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]); |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 987 | if ((oppos == -1) && (preoppos != -1)) { |
| 988 | /* Current command is listed as preopcode in |
| 989 | * ICH struct OPCODES, but next command is not |
| 990 | * listed as opcode in that struct. |
| 991 | * Check for command sanity, then |
| 992 | * try to reprogram the ICH opcode list. |
| 993 | */ |
| 994 | if (find_preop(curopcodes, |
| 995 | (cmds + 1)->writearr[0]) != -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 996 | msg_perr("%s: Two subsequent " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 997 | "preopcodes 0x%02x and 0x%02x, " |
| 998 | "ignoring the first.\n", |
| 999 | __func__, cmds->writearr[0], |
| 1000 | (cmds + 1)->writearr[0]); |
| 1001 | continue; |
| 1002 | } |
| 1003 | /* If the chipset is locked down, we'll fail |
| 1004 | * during execution of the next command anyway. |
| 1005 | * No need to bother with fixups. |
| 1006 | */ |
| 1007 | if (!ichspi_lock) { |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame] | 1008 | oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt); |
| 1009 | if (oppos == -1) |
| 1010 | continue; |
| 1011 | curopcodes->opcode[oppos].atomic = preoppos + 1; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1012 | continue; |
| 1013 | } |
| 1014 | } |
| 1015 | if ((oppos != -1) && (preoppos != -1)) { |
| 1016 | /* Current command is listed as preopcode in |
| 1017 | * ICH struct OPCODES and next command is listed |
| 1018 | * as opcode in that struct. Match them up. |
| 1019 | */ |
| 1020 | curopcodes->opcode[oppos].atomic = preoppos + 1; |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 1021 | continue; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1022 | } |
| 1023 | /* If none of the above if-statements about oppos or |
| 1024 | * preoppos matched, this is a normal opcode. |
| 1025 | */ |
| 1026 | } |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 1027 | ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt, |
| 1028 | cmds->writearr, cmds->readarr); |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1029 | /* Reset the type of all opcodes to non-atomic. */ |
| 1030 | for (i = 0; i < 8; i++) |
| 1031 | curopcodes->opcode[i].atomic = 0; |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 1032 | } |
| 1033 | return ret; |
| 1034 | } |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1035 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1036 | #define ICH_BMWAG(x) ((x >> 24) & 0xff) |
| 1037 | #define ICH_BMRAG(x) ((x >> 16) & 0xff) |
| 1038 | #define ICH_BRWA(x) ((x >> 8) & 0xff) |
| 1039 | #define ICH_BRRA(x) ((x >> 0) & 0xff) |
| 1040 | |
| 1041 | #define ICH_FREG_BASE(x) ((x >> 0) & 0x1fff) |
| 1042 | #define ICH_FREG_LIMIT(x) ((x >> 16) & 0x1fff) |
| 1043 | |
| 1044 | static void do_ich9_spi_frap(uint32_t frap, int i) |
| 1045 | { |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 1046 | static const char *const access_names[4] = { |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1047 | "locked", "read-only", "write-only", "read-write" |
| 1048 | }; |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 1049 | static const char *const region_names[5] = { |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1050 | "Flash Descriptor", "BIOS", "Management Engine", |
| 1051 | "Gigabit Ethernet", "Platform Data" |
| 1052 | }; |
| 1053 | uint32_t base, limit; |
| 1054 | int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) | |
| 1055 | (((ICH_BRRA(frap) >> i) & 1) << 0); |
| 1056 | int offset = 0x54 + i * 4; |
| 1057 | uint32_t freg = mmio_readl(ich_spibar + offset); |
| 1058 | |
| 1059 | msg_pdbg("0x%02X: 0x%08x (FREG%i: %s)\n", |
| 1060 | offset, freg, i, region_names[i]); |
| 1061 | |
| 1062 | base = ICH_FREG_BASE(freg); |
| 1063 | limit = ICH_FREG_LIMIT(freg); |
Joshua Roys | d172ecd | 2011-05-26 13:30:51 +0000 | [diff] [blame] | 1064 | if (base > limit) { |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1065 | /* this FREG is disabled */ |
| 1066 | msg_pdbg("%s region is unused.\n", region_names[i]); |
| 1067 | return; |
| 1068 | } |
| 1069 | |
| 1070 | msg_pdbg("0x%08x-0x%08x is %s\n", |
| 1071 | (base << 12), (limit << 12) | 0x0fff, |
| 1072 | access_names[rwperms]); |
| 1073 | } |
| 1074 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 1075 | static const struct spi_programmer spi_programmer_ich7 = { |
| 1076 | .type = SPI_CONTROLLER_ICH7, |
| 1077 | .max_data_read = 64, |
| 1078 | .max_data_write = 64, |
| 1079 | .command = ich_spi_send_command, |
| 1080 | .multicommand = ich_spi_send_multicommand, |
| 1081 | .read = default_spi_read, |
| 1082 | .write_256 = default_spi_write_256, |
| 1083 | }; |
| 1084 | |
| 1085 | static const struct spi_programmer spi_programmer_ich9 = { |
| 1086 | .type = SPI_CONTROLLER_ICH9, |
| 1087 | .max_data_read = 64, |
| 1088 | .max_data_write = 64, |
| 1089 | .command = ich_spi_send_command, |
| 1090 | .multicommand = ich_spi_send_multicommand, |
| 1091 | .read = default_spi_read, |
| 1092 | .write_256 = default_spi_write_256, |
| 1093 | }; |
| 1094 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1095 | int ich_init_spi(struct pci_dev *dev, uint32_t base, void *rcrb, |
| 1096 | int ich_generation) |
| 1097 | { |
| 1098 | int i; |
| 1099 | uint8_t old, new; |
| 1100 | uint16_t spibar_offset, tmp2; |
| 1101 | uint32_t tmp; |
| 1102 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1103 | switch (ich_generation) { |
| 1104 | case 7: |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 1105 | register_spi_programmer(&spi_programmer_ich7); |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1106 | spibar_offset = 0x3020; |
| 1107 | break; |
| 1108 | case 8: |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 1109 | register_spi_programmer(&spi_programmer_ich9); |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1110 | spibar_offset = 0x3020; |
| 1111 | break; |
| 1112 | case 9: |
| 1113 | case 10: |
| 1114 | default: /* Future version might behave the same */ |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 1115 | register_spi_programmer(&spi_programmer_ich9); |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1116 | spibar_offset = 0x3800; |
| 1117 | break; |
| 1118 | } |
| 1119 | |
| 1120 | /* SPIBAR is at RCRB+0x3020 for ICH[78] and RCRB+0x3800 for ICH9. */ |
| 1121 | msg_pdbg("SPIBAR = 0x%x + 0x%04x\n", base, spibar_offset); |
| 1122 | |
| 1123 | /* Assign Virtual Address */ |
| 1124 | ich_spibar = rcrb + spibar_offset; |
| 1125 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 1126 | switch (spi_programmer->type) { |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1127 | case SPI_CONTROLLER_ICH7: |
| 1128 | msg_pdbg("0x00: 0x%04x (SPIS)\n", |
| 1129 | mmio_readw(ich_spibar + 0)); |
| 1130 | msg_pdbg("0x02: 0x%04x (SPIC)\n", |
| 1131 | mmio_readw(ich_spibar + 2)); |
| 1132 | msg_pdbg("0x04: 0x%08x (SPIA)\n", |
| 1133 | mmio_readl(ich_spibar + 4)); |
| 1134 | for (i = 0; i < 8; i++) { |
| 1135 | int offs; |
| 1136 | offs = 8 + (i * 8); |
| 1137 | msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs, |
| 1138 | mmio_readl(ich_spibar + offs), i); |
| 1139 | msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4, |
| 1140 | mmio_readl(ich_spibar + offs + 4), i); |
| 1141 | } |
| 1142 | ichspi_bbar = mmio_readl(ich_spibar + 0x50); |
| 1143 | msg_pdbg("0x50: 0x%08x (BBAR)\n", |
| 1144 | ichspi_bbar); |
| 1145 | msg_pdbg("0x54: 0x%04x (PREOP)\n", |
| 1146 | mmio_readw(ich_spibar + 0x54)); |
| 1147 | msg_pdbg("0x56: 0x%04x (OPTYPE)\n", |
| 1148 | mmio_readw(ich_spibar + 0x56)); |
| 1149 | msg_pdbg("0x58: 0x%08x (OPMENU)\n", |
| 1150 | mmio_readl(ich_spibar + 0x58)); |
| 1151 | msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", |
| 1152 | mmio_readl(ich_spibar + 0x5c)); |
| 1153 | for (i = 0; i < 4; i++) { |
| 1154 | int offs; |
| 1155 | offs = 0x60 + (i * 4); |
| 1156 | msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs, |
| 1157 | mmio_readl(ich_spibar + offs), i); |
| 1158 | } |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1159 | if (mmio_readw(ich_spibar) & (1 << 15)) { |
| 1160 | msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n"); |
| 1161 | ichspi_lock = 1; |
| 1162 | } |
| 1163 | ich_init_opcodes(); |
| 1164 | break; |
| 1165 | case SPI_CONTROLLER_ICH9: |
| 1166 | tmp2 = mmio_readw(ich_spibar + 4); |
| 1167 | msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2); |
| 1168 | msg_pdbg("FLOCKDN %i, ", (tmp2 >> 15 & 1)); |
| 1169 | msg_pdbg("FDV %i, ", (tmp2 >> 14) & 1); |
| 1170 | msg_pdbg("FDOPSS %i, ", (tmp2 >> 13) & 1); |
| 1171 | msg_pdbg("SCIP %i, ", (tmp2 >> 5) & 1); |
| 1172 | msg_pdbg("BERASE %i, ", (tmp2 >> 3) & 3); |
| 1173 | msg_pdbg("AEL %i, ", (tmp2 >> 2) & 1); |
| 1174 | msg_pdbg("FCERR %i, ", (tmp2 >> 1) & 1); |
| 1175 | msg_pdbg("FDONE %i\n", (tmp2 >> 0) & 1); |
| 1176 | |
| 1177 | tmp = mmio_readl(ich_spibar + 0x50); |
| 1178 | msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp); |
| 1179 | msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp)); |
| 1180 | msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp)); |
| 1181 | msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp)); |
| 1182 | msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp)); |
| 1183 | |
| 1184 | /* print out the FREGx registers along with FRAP access bits */ |
| 1185 | for(i = 0; i < 5; i++) |
| 1186 | do_ich9_spi_frap(tmp, i); |
| 1187 | |
| 1188 | msg_pdbg("0x74: 0x%08x (PR0)\n", |
| 1189 | mmio_readl(ich_spibar + 0x74)); |
| 1190 | msg_pdbg("0x78: 0x%08x (PR1)\n", |
| 1191 | mmio_readl(ich_spibar + 0x78)); |
| 1192 | msg_pdbg("0x7C: 0x%08x (PR2)\n", |
| 1193 | mmio_readl(ich_spibar + 0x7C)); |
| 1194 | msg_pdbg("0x80: 0x%08x (PR3)\n", |
| 1195 | mmio_readl(ich_spibar + 0x80)); |
| 1196 | msg_pdbg("0x84: 0x%08x (PR4)\n", |
| 1197 | mmio_readl(ich_spibar + 0x84)); |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 1198 | |
| 1199 | tmp = mmio_readl(ich_spibar + 0x90); |
| 1200 | msg_pdbg("0x90: 0x%02x (SSFS)\n", tmp & 0xff); |
| 1201 | msg_pdbg("AEL %i, ", (tmp >> 4) & 1); |
| 1202 | msg_pdbg("FCERR %i, ", (tmp >> 3) & 1); |
| 1203 | msg_pdbg("FDONE %i, ", (tmp >> 2) & 1); |
| 1204 | msg_pdbg("SCIP %i\n", (tmp >> 0) & 1); |
| 1205 | if (tmp & (1 << 3)) { |
| 1206 | msg_pdbg("Clearing SSFS.FCERR\n"); |
| 1207 | mmio_writeb(1 << 3, ich_spibar + 0x90); |
| 1208 | } |
| 1209 | tmp >>= 8; |
| 1210 | msg_pdbg("0x91: 0x%06x (SSFC)\n", tmp); |
| 1211 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1212 | msg_pdbg("0x94: 0x%04x (PREOP)\n", |
| 1213 | mmio_readw(ich_spibar + 0x94)); |
| 1214 | msg_pdbg("0x96: 0x%04x (OPTYPE)\n", |
| 1215 | mmio_readw(ich_spibar + 0x96)); |
| 1216 | msg_pdbg("0x98: 0x%08x (OPMENU)\n", |
| 1217 | mmio_readl(ich_spibar + 0x98)); |
| 1218 | msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n", |
| 1219 | mmio_readl(ich_spibar + 0x9C)); |
| 1220 | ichspi_bbar = mmio_readl(ich_spibar + 0xA0); |
| 1221 | msg_pdbg("0xA0: 0x%08x (BBAR)\n", |
| 1222 | ichspi_bbar); |
| 1223 | msg_pdbg("0xB0: 0x%08x (FDOC)\n", |
| 1224 | mmio_readl(ich_spibar + 0xB0)); |
| 1225 | if (tmp2 & (1 << 15)) { |
| 1226 | msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n"); |
| 1227 | ichspi_lock = 1; |
| 1228 | } |
| 1229 | ich_init_opcodes(); |
| 1230 | break; |
| 1231 | default: |
| 1232 | /* Nothing */ |
| 1233 | break; |
| 1234 | } |
| 1235 | |
| 1236 | old = pci_read_byte(dev, 0xdc); |
| 1237 | msg_pdbg("SPI Read Configuration: "); |
| 1238 | new = (old >> 2) & 0x3; |
| 1239 | switch (new) { |
| 1240 | case 0: |
| 1241 | case 1: |
| 1242 | case 2: |
| 1243 | msg_pdbg("prefetching %sabled, caching %sabled, ", |
| 1244 | (new & 0x2) ? "en" : "dis", |
| 1245 | (new & 0x1) ? "dis" : "en"); |
| 1246 | break; |
| 1247 | default: |
| 1248 | msg_pdbg("invalid prefetching/caching settings, "); |
| 1249 | break; |
| 1250 | } |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 1254 | static const struct spi_programmer spi_programmer_via = { |
| 1255 | .type = SPI_CONTROLLER_VIA, |
| 1256 | .max_data_read = 16, |
| 1257 | .max_data_write = 16, |
| 1258 | .command = ich_spi_send_command, |
| 1259 | .multicommand = ich_spi_send_multicommand, |
| 1260 | .read = default_spi_read, |
| 1261 | .write_256 = default_spi_write_256, |
| 1262 | }; |
| 1263 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1264 | int via_init_spi(struct pci_dev *dev) |
| 1265 | { |
| 1266 | uint32_t mmio_base; |
Carl-Daniel Hailfinger | 841d631 | 2010-11-24 23:37:22 +0000 | [diff] [blame] | 1267 | int i; |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1268 | |
| 1269 | mmio_base = (pci_read_long(dev, 0xbc)) << 8; |
| 1270 | msg_pdbg("MMIO base at = 0x%x\n", mmio_base); |
| 1271 | ich_spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70); |
| 1272 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1273 | /* Not sure if it speaks all these bus protocols. */ |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 1274 | buses_supported = CHIP_BUSTYPE_LPC | CHIP_BUSTYPE_FWH; |
| 1275 | register_spi_programmer(&spi_programmer_via); |
Carl-Daniel Hailfinger | 841d631 | 2010-11-24 23:37:22 +0000 | [diff] [blame] | 1276 | |
| 1277 | msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0)); |
| 1278 | msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2)); |
| 1279 | msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4)); |
| 1280 | for (i = 0; i < 2; i++) { |
| 1281 | int offs; |
| 1282 | offs = 8 + (i * 8); |
| 1283 | msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs, |
| 1284 | mmio_readl(ich_spibar + offs), i); |
| 1285 | msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4, |
| 1286 | mmio_readl(ich_spibar + offs + 4), i); |
| 1287 | } |
| 1288 | ichspi_bbar = mmio_readl(ich_spibar + 0x50); |
| 1289 | msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar); |
| 1290 | msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54)); |
| 1291 | msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56)); |
| 1292 | msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58)); |
| 1293 | msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c)); |
| 1294 | for (i = 0; i < 3; i++) { |
| 1295 | int offs; |
| 1296 | offs = 0x60 + (i * 4); |
| 1297 | msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs, |
| 1298 | mmio_readl(ich_spibar + offs), i); |
| 1299 | } |
| 1300 | msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n", |
| 1301 | mmio_readw(ich_spibar + 0x6c)); |
| 1302 | if (mmio_readw(ich_spibar) & (1 << 15)) { |
| 1303 | msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n"); |
| 1304 | ichspi_lock = 1; |
| 1305 | } |
| 1306 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1307 | ich_init_opcodes(); |
| 1308 | |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1312 | #endif |