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 |
Stefan Tauner | 8b391b8 | 2011-08-09 01:49:34 +0000 | [diff] [blame] | 9 | * Copyright (C) 2011 Stefan Tauner |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 26 | #if defined(__i386__) || defined(__x86_64__) |
| 27 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 28 | #include <string.h> |
Stefan Tauner | d0c5dc2 | 2011-10-20 12:57:14 +0000 | [diff] [blame] | 29 | #include <stdlib.h> |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 30 | #include "flash.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 31 | #include "programmer.h" |
Patrick Georgi | 32508eb | 2012-07-20 20:35:14 +0000 | [diff] [blame] | 32 | #include "hwaccess.h" |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 33 | #include "spi.h" |
Stefan Tauner | 1e14639 | 2011-09-15 23:52:55 +0000 | [diff] [blame] | 34 | #include "ich_descriptors.h" |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 35 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 36 | /* ICH9 controller register definition */ |
Stefan Tauner | 5520694 | 2011-06-11 09:53:22 +0000 | [diff] [blame] | 37 | #define ICH9_REG_HSFS 0x04 /* 16 Bits Hardware Sequencing Flash Status */ |
| 38 | #define HSFS_FDONE_OFF 0 /* 0: Flash Cycle Done */ |
| 39 | #define HSFS_FDONE (0x1 << HSFS_FDONE_OFF) |
| 40 | #define HSFS_FCERR_OFF 1 /* 1: Flash Cycle Error */ |
| 41 | #define HSFS_FCERR (0x1 << HSFS_FCERR_OFF) |
| 42 | #define HSFS_AEL_OFF 2 /* 2: Access Error Log */ |
| 43 | #define HSFS_AEL (0x1 << HSFS_AEL_OFF) |
| 44 | #define HSFS_BERASE_OFF 3 /* 3-4: Block/Sector Erase Size */ |
| 45 | #define HSFS_BERASE (0x3 << HSFS_BERASE_OFF) |
| 46 | #define HSFS_SCIP_OFF 5 /* 5: SPI Cycle In Progress */ |
| 47 | #define HSFS_SCIP (0x1 << HSFS_SCIP_OFF) |
| 48 | /* 6-12: reserved */ |
| 49 | #define HSFS_FDOPSS_OFF 13 /* 13: Flash Descriptor Override Pin-Strap Status */ |
| 50 | #define HSFS_FDOPSS (0x1 << HSFS_FDOPSS_OFF) |
| 51 | #define HSFS_FDV_OFF 14 /* 14: Flash Descriptor Valid */ |
| 52 | #define HSFS_FDV (0x1 << HSFS_FDV_OFF) |
| 53 | #define HSFS_FLOCKDN_OFF 15 /* 15: Flash Configuration Lock-Down */ |
| 54 | #define HSFS_FLOCKDN (0x1 << HSFS_FLOCKDN_OFF) |
| 55 | |
| 56 | #define ICH9_REG_HSFC 0x06 /* 16 Bits Hardware Sequencing Flash Control */ |
| 57 | #define HSFC_FGO_OFF 0 /* 0: Flash Cycle Go */ |
| 58 | #define HSFC_FGO (0x1 << HSFC_FGO_OFF) |
| 59 | #define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */ |
| 60 | #define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF) |
| 61 | /* 3-7: reserved */ |
| 62 | #define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */ |
| 63 | #define HSFC_FDBC (0x3f << HSFC_FDBC_OFF) |
| 64 | /* 14: reserved */ |
| 65 | #define HSFC_SME_OFF 15 /* 15: SPI SMI# Enable */ |
| 66 | #define HSFC_SME (0x1 << HSFC_SME_OFF) |
| 67 | |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 68 | #define ICH9_REG_FADDR 0x08 /* 32 Bits */ |
| 69 | #define ICH9_REG_FDATA0 0x10 /* 64 Bytes */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 70 | |
Stefan Tauner | 29c8083 | 2011-06-12 08:14:10 +0000 | [diff] [blame] | 71 | #define ICH9_REG_FRAP 0x50 /* 32 Bytes Flash Region Access Permissions */ |
| 72 | #define ICH9_REG_FREG0 0x54 /* 32 Bytes Flash Region 0 */ |
| 73 | |
| 74 | #define ICH9_REG_PR0 0x74 /* 32 Bytes Protected Range 0 */ |
Stefan Tauner | bf69aaa | 2011-09-17 21:21:48 +0000 | [diff] [blame] | 75 | #define PR_WP_OFF 31 /* 31: write protection enable */ |
| 76 | #define PR_RP_OFF 15 /* 15: read protection enable */ |
Stefan Tauner | 29c8083 | 2011-06-12 08:14:10 +0000 | [diff] [blame] | 77 | |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 78 | #define ICH9_REG_SSFS 0x90 /* 08 Bits */ |
Stefan Tauner | 0c1ec45 | 2011-06-11 09:53:09 +0000 | [diff] [blame] | 79 | #define SSFS_SCIP_OFF 0 /* SPI Cycle In Progress */ |
| 80 | #define SSFS_SCIP (0x1 << SSFS_SCIP_OFF) |
| 81 | #define SSFS_FDONE_OFF 2 /* Cycle Done Status */ |
| 82 | #define SSFS_FDONE (0x1 << SSFS_FDONE_OFF) |
| 83 | #define SSFS_FCERR_OFF 3 /* Flash Cycle Error */ |
| 84 | #define SSFS_FCERR (0x1 << SSFS_FCERR_OFF) |
| 85 | #define SSFS_AEL_OFF 4 /* Access Error Log */ |
| 86 | #define SSFS_AEL (0x1 << SSFS_AEL_OFF) |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 87 | /* The following bits are reserved in SSFS: 1,5-7. */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 88 | #define SSFS_RESERVED_MASK 0x000000e2 |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 89 | |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 90 | #define ICH9_REG_SSFC 0x91 /* 24 Bits */ |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 91 | /* We combine SSFS and SSFC to one 32-bit word, |
Stefan Tauner | 0c1ec45 | 2011-06-11 09:53:09 +0000 | [diff] [blame] | 92 | * therefore SSFC bits are off by 8. */ |
| 93 | /* 0: reserved */ |
| 94 | #define SSFC_SCGO_OFF (1 + 8) /* 1: SPI Cycle Go */ |
| 95 | #define SSFC_SCGO (0x1 << SSFC_SCGO_OFF) |
| 96 | #define SSFC_ACS_OFF (2 + 8) /* 2: Atomic Cycle Sequence */ |
| 97 | #define SSFC_ACS (0x1 << SSFC_ACS_OFF) |
| 98 | #define SSFC_SPOP_OFF (3 + 8) /* 3: Sequence Prefix Opcode Pointer */ |
| 99 | #define SSFC_SPOP (0x1 << SSFC_SPOP_OFF) |
| 100 | #define SSFC_COP_OFF (4 + 8) /* 4-6: Cycle Opcode Pointer */ |
| 101 | #define SSFC_COP (0x7 << SSFC_COP_OFF) |
| 102 | /* 7: reserved */ |
| 103 | #define SSFC_DBC_OFF (8 + 8) /* 8-13: Data Byte Count */ |
| 104 | #define SSFC_DBC (0x3f << SSFC_DBC_OFF) |
| 105 | #define SSFC_DS_OFF (14 + 8) /* 14: Data Cycle */ |
| 106 | #define SSFC_DS (0x1 << SSFC_DS_OFF) |
| 107 | #define SSFC_SME_OFF (15 + 8) /* 15: SPI SMI# Enable */ |
| 108 | #define SSFC_SME (0x1 << SSFC_SME_OFF) |
| 109 | #define SSFC_SCF_OFF (16 + 8) /* 16-18: SPI Cycle Frequency */ |
| 110 | #define SSFC_SCF (0x7 << SSFC_SCF_OFF) |
| 111 | #define SSFC_SCF_20MHZ 0x00000000 |
| 112 | #define SSFC_SCF_33MHZ 0x01000000 |
| 113 | /* 19-23: reserved */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 114 | #define SSFC_RESERVED_MASK 0xf8008100 |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 115 | |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 116 | #define ICH9_REG_PREOP 0x94 /* 16 Bits */ |
| 117 | #define ICH9_REG_OPTYPE 0x96 /* 16 Bits */ |
| 118 | #define ICH9_REG_OPMENU 0x98 /* 64 Bits */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 119 | |
Stefan Tauner | 29c8083 | 2011-06-12 08:14:10 +0000 | [diff] [blame] | 120 | #define ICH9_REG_BBAR 0xA0 /* 32 Bits BIOS Base Address Configuration */ |
| 121 | #define BBAR_MASK 0x00ffff00 /* 8-23: Bottom of System Flash */ |
| 122 | |
Stefan Tauner | 1e14639 | 2011-09-15 23:52:55 +0000 | [diff] [blame] | 123 | #define ICH8_REG_VSCC 0xC1 /* 32 Bits Vendor Specific Component Capabilities */ |
| 124 | #define ICH9_REG_LVSCC 0xC4 /* 32 Bits Host Lower Vendor Specific Component Capabilities */ |
| 125 | #define ICH9_REG_UVSCC 0xC8 /* 32 Bits Host Upper Vendor Specific Component Capabilities */ |
| 126 | /* The individual fields of the VSCC registers are defined in the file |
| 127 | * ich_descriptors.h. The reason is that the same layout is also used in the |
| 128 | * flash descriptor to define the properties of the different flash chips |
| 129 | * supported. The BIOS (or the ME?) is responsible to populate the ICH registers |
| 130 | * with the information from the descriptor on startup depending on the actual |
| 131 | * chip(s) detected. */ |
| 132 | |
Stefan Tauner | bd649e4 | 2011-07-01 00:39:16 +0000 | [diff] [blame] | 133 | #define ICH9_REG_FPB 0xD0 /* 32 Bits Flash Partition Boundary */ |
| 134 | #define FPB_FPBA_OFF 0 /* 0-12: Block/Sector Erase Size */ |
| 135 | #define FPB_FPBA (0x1FFF << FPB_FPBA_OFF) |
| 136 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 137 | // ICH9R SPI commands |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 138 | #define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0 |
| 139 | #define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1 |
| 140 | #define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2 |
| 141 | #define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3 |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 142 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 143 | // ICH7 registers |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 144 | #define ICH7_REG_SPIS 0x00 /* 16 Bits */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 145 | #define SPIS_SCIP 0x0001 |
| 146 | #define SPIS_GRANT 0x0002 |
| 147 | #define SPIS_CDS 0x0004 |
| 148 | #define SPIS_FCERR 0x0008 |
| 149 | #define SPIS_RESERVED_MASK 0x7ff0 |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 150 | |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 151 | /* VIA SPI is compatible with ICH7, but maxdata |
| 152 | to transfer is 16 bytes. |
| 153 | |
| 154 | DATA byte count on ICH7 is 8:13, on VIA 8:11 |
| 155 | |
| 156 | bit 12 is port select CS0 CS1 |
| 157 | bit 13 is FAST READ enable |
| 158 | bit 7 is used with fast read and one shot controls CS de-assert? |
| 159 | */ |
| 160 | |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 161 | #define ICH7_REG_SPIC 0x02 /* 16 Bits */ |
| 162 | #define SPIC_SCGO 0x0002 |
| 163 | #define SPIC_ACS 0x0004 |
| 164 | #define SPIC_SPOP 0x0008 |
| 165 | #define SPIC_DS 0x4000 |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 166 | |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 167 | #define ICH7_REG_SPIA 0x04 /* 32 Bits */ |
| 168 | #define ICH7_REG_SPID0 0x08 /* 64 Bytes */ |
| 169 | #define ICH7_REG_PREOP 0x54 /* 16 Bits */ |
| 170 | #define ICH7_REG_OPTYPE 0x56 /* 16 Bits */ |
| 171 | #define ICH7_REG_OPMENU 0x58 /* 64 Bits */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 172 | |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 173 | /* ICH SPI configuration lock-down. May be set during chipset enabling. */ |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 174 | static int ichspi_lock = 0; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 175 | |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 176 | static enum ich_chipset ich_generation = CHIPSET_ICH_UNKNOWN; |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 177 | uint32_t ichspi_bbar = 0; |
| 178 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 179 | static void *ich_spibar = NULL; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 180 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 181 | typedef struct _OPCODE { |
| 182 | uint8_t opcode; //This commands spi opcode |
| 183 | uint8_t spi_type; //This commands spi type |
| 184 | uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1 |
| 185 | } OPCODE; |
| 186 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 187 | /* Suggested opcode definition: |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 188 | * Preop 1: Write Enable |
| 189 | * Preop 2: Write Status register enable |
| 190 | * |
| 191 | * OP 0: Write address |
| 192 | * OP 1: Read Address |
| 193 | * OP 2: ERASE block |
| 194 | * OP 3: Read Status register |
| 195 | * OP 4: Read ID |
| 196 | * OP 5: Write Status register |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 197 | * OP 6: chip private (read JEDEC id) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 198 | * OP 7: Chip erase |
| 199 | */ |
| 200 | typedef struct _OPCODES { |
| 201 | uint8_t preop[2]; |
| 202 | OPCODE opcode[8]; |
| 203 | } OPCODES; |
| 204 | |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 205 | static OPCODES *curopcodes = NULL; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 206 | |
| 207 | /* HW access functions */ |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 208 | static uint32_t REGREAD32(int X) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 209 | { |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 210 | return mmio_readl(ich_spibar + X); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 213 | static uint16_t REGREAD16(int X) |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 214 | { |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 215 | return mmio_readw(ich_spibar + X); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 218 | static uint16_t REGREAD8(int X) |
| 219 | { |
| 220 | return mmio_readb(ich_spibar + X); |
| 221 | } |
| 222 | |
Stefan Tauner | ccd92a1 | 2011-07-01 00:39:01 +0000 | [diff] [blame] | 223 | #define REGWRITE32(off, val) mmio_writel(val, ich_spibar+(off)) |
| 224 | #define REGWRITE16(off, val) mmio_writew(val, ich_spibar+(off)) |
| 225 | #define REGWRITE8(off, val) mmio_writeb(val, ich_spibar+(off)) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 226 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 227 | /* Common SPI functions */ |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 228 | static int find_opcode(OPCODES *op, uint8_t opcode); |
| 229 | static int find_preop(OPCODES *op, uint8_t preop); |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 230 | static int generate_opcodes(OPCODES * op); |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 231 | static int program_opcodes(OPCODES *op, int enable_undo); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 232 | static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset, |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 233 | uint8_t datalength, uint8_t * data); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 234 | |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 235 | /* for pairing opcodes with their required preop */ |
| 236 | struct preop_opcode_pair { |
| 237 | uint8_t preop; |
| 238 | uint8_t opcode; |
| 239 | }; |
| 240 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 241 | /* List of opcodes which need preopcodes and matching preopcodes. Unused. */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 242 | const struct preop_opcode_pair pops[] = { |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 243 | {JEDEC_WREN, JEDEC_BYTE_PROGRAM}, |
| 244 | {JEDEC_WREN, JEDEC_SE}, /* sector erase */ |
| 245 | {JEDEC_WREN, JEDEC_BE_52}, /* block erase */ |
| 246 | {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */ |
| 247 | {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */ |
| 248 | {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 249 | /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */ |
| 250 | {JEDEC_WREN, JEDEC_WRSR}, |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 251 | {JEDEC_EWSR, JEDEC_WRSR}, |
| 252 | {0,} |
| 253 | }; |
| 254 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 255 | /* Reasonable default configuration. Needs ad-hoc modifications if we |
| 256 | * encounter unlisted opcodes. Fun. |
| 257 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 258 | static OPCODES O_ST_M25P = { |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 259 | { |
| 260 | JEDEC_WREN, |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 261 | JEDEC_EWSR, |
| 262 | }, |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 263 | { |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 264 | {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 265 | {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 266 | {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 267 | {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] | 268 | {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] | 269 | {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 270 | {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] | 271 | {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase |
| 272 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame] | 275 | /* List of opcodes with their corresponding spi_type |
| 276 | * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode |
| 277 | * is needed which is currently not in the chipset OPCODE table |
| 278 | */ |
| 279 | static OPCODE POSSIBLE_OPCODES[] = { |
| 280 | {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte |
| 281 | {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data |
| 282 | {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector |
| 283 | {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg |
| 284 | {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature |
| 285 | {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register |
| 286 | {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID |
| 287 | {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase |
| 288 | {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase |
| 289 | {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase |
| 290 | {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment |
| 291 | }; |
| 292 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 293 | static OPCODES O_EXISTING = {}; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 294 | |
Stefan Tauner | 2a8b262 | 2011-06-11 09:53:16 +0000 | [diff] [blame] | 295 | /* pretty printing functions */ |
Stefan Tauner | 8b391b8 | 2011-08-09 01:49:34 +0000 | [diff] [blame] | 296 | static void prettyprint_opcodes(OPCODES *ops) |
Stefan Tauner | 2a8b262 | 2011-06-11 09:53:16 +0000 | [diff] [blame] | 297 | { |
Stefan Tauner | 84e1dde | 2011-09-17 19:53:11 +0000 | [diff] [blame] | 298 | OPCODE oc; |
| 299 | const char *t; |
| 300 | const char *a; |
| 301 | uint8_t i; |
| 302 | static const char *const spi_type[4] = { |
| 303 | "read w/o addr", |
| 304 | "write w/o addr", |
| 305 | "read w/ addr", |
| 306 | "write w/ addr" |
| 307 | }; |
| 308 | static const char *const atomic_type[3] = { |
| 309 | "none", |
| 310 | " 0 ", |
| 311 | " 1 " |
| 312 | }; |
| 313 | |
| 314 | if (ops == NULL) |
Stefan Tauner | 2a8b262 | 2011-06-11 09:53:16 +0000 | [diff] [blame] | 315 | return; |
| 316 | |
Stefan Tauner | 84e1dde | 2011-09-17 19:53:11 +0000 | [diff] [blame] | 317 | msg_pdbg2(" OP Type Pre-OP\n"); |
Stefan Tauner | 2a8b262 | 2011-06-11 09:53:16 +0000 | [diff] [blame] | 318 | for (i = 0; i < 8; i++) { |
| 319 | oc = ops->opcode[i]; |
Stefan Tauner | 84e1dde | 2011-09-17 19:53:11 +0000 | [diff] [blame] | 320 | t = (oc.spi_type > 3) ? "invalid" : spi_type[oc.spi_type]; |
| 321 | a = (oc.atomic > 2) ? "invalid" : atomic_type[oc.atomic]; |
| 322 | msg_pdbg2("op[%d]: 0x%02x, %s, %s\n", i, oc.opcode, t, a); |
Stefan Tauner | 2a8b262 | 2011-06-11 09:53:16 +0000 | [diff] [blame] | 323 | } |
Stefan Tauner | 84e1dde | 2011-09-17 19:53:11 +0000 | [diff] [blame] | 324 | msg_pdbg2("Pre-OP 0: 0x%02x, Pre-OP 1: 0x%02x\n", ops->preop[0], |
| 325 | ops->preop[1]); |
Stefan Tauner | 2a8b262 | 2011-06-11 09:53:16 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | #define pprint_reg(reg, bit, val, sep) msg_pdbg("%s=%d" sep, #bit, (val & reg##_##bit)>>reg##_##bit##_OFF) |
| 329 | |
Stefan Tauner | 5520694 | 2011-06-11 09:53:22 +0000 | [diff] [blame] | 330 | static void prettyprint_ich9_reg_hsfs(uint16_t reg_val) |
| 331 | { |
| 332 | msg_pdbg("HSFS: "); |
| 333 | pprint_reg(HSFS, FDONE, reg_val, ", "); |
| 334 | pprint_reg(HSFS, FCERR, reg_val, ", "); |
| 335 | pprint_reg(HSFS, AEL, reg_val, ", "); |
| 336 | pprint_reg(HSFS, BERASE, reg_val, ", "); |
| 337 | pprint_reg(HSFS, SCIP, reg_val, ", "); |
| 338 | pprint_reg(HSFS, FDOPSS, reg_val, ", "); |
| 339 | pprint_reg(HSFS, FDV, reg_val, ", "); |
| 340 | pprint_reg(HSFS, FLOCKDN, reg_val, "\n"); |
| 341 | } |
| 342 | |
| 343 | static void prettyprint_ich9_reg_hsfc(uint16_t reg_val) |
| 344 | { |
| 345 | msg_pdbg("HSFC: "); |
| 346 | pprint_reg(HSFC, FGO, reg_val, ", "); |
| 347 | pprint_reg(HSFC, FCYCLE, reg_val, ", "); |
| 348 | pprint_reg(HSFC, FDBC, reg_val, ", "); |
| 349 | pprint_reg(HSFC, SME, reg_val, "\n"); |
| 350 | } |
| 351 | |
Stefan Tauner | 2a8b262 | 2011-06-11 09:53:16 +0000 | [diff] [blame] | 352 | static void prettyprint_ich9_reg_ssfs(uint32_t reg_val) |
| 353 | { |
| 354 | msg_pdbg("SSFS: "); |
| 355 | pprint_reg(SSFS, SCIP, reg_val, ", "); |
| 356 | pprint_reg(SSFS, FDONE, reg_val, ", "); |
| 357 | pprint_reg(SSFS, FCERR, reg_val, ", "); |
| 358 | pprint_reg(SSFS, AEL, reg_val, "\n"); |
| 359 | } |
| 360 | |
| 361 | static void prettyprint_ich9_reg_ssfc(uint32_t reg_val) |
| 362 | { |
| 363 | msg_pdbg("SSFC: "); |
| 364 | pprint_reg(SSFC, SCGO, reg_val, ", "); |
| 365 | pprint_reg(SSFC, ACS, reg_val, ", "); |
| 366 | pprint_reg(SSFC, SPOP, reg_val, ", "); |
| 367 | pprint_reg(SSFC, COP, reg_val, ", "); |
| 368 | pprint_reg(SSFC, DBC, reg_val, ", "); |
| 369 | pprint_reg(SSFC, SME, reg_val, ", "); |
| 370 | pprint_reg(SSFC, SCF, reg_val, "\n"); |
| 371 | } |
| 372 | |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame] | 373 | static uint8_t lookup_spi_type(uint8_t opcode) |
| 374 | { |
| 375 | int a; |
| 376 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 377 | for (a = 0; a < ARRAY_SIZE(POSSIBLE_OPCODES); a++) { |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame] | 378 | if (POSSIBLE_OPCODES[a].opcode == opcode) |
| 379 | return POSSIBLE_OPCODES[a].spi_type; |
| 380 | } |
| 381 | |
| 382 | return 0xFF; |
| 383 | } |
| 384 | |
| 385 | static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt) |
| 386 | { |
| 387 | uint8_t spi_type; |
| 388 | |
| 389 | spi_type = lookup_spi_type(opcode); |
| 390 | if (spi_type > 3) { |
| 391 | /* Try to guess spi type from read/write sizes. |
| 392 | * The following valid writecnt/readcnt combinations exist: |
| 393 | * writecnt = 4, readcnt >= 0 |
| 394 | * writecnt = 1, readcnt >= 0 |
| 395 | * writecnt >= 4, readcnt = 0 |
| 396 | * writecnt >= 1, readcnt = 0 |
| 397 | * writecnt >= 1 is guaranteed for all commands. |
| 398 | */ |
| 399 | if (readcnt == 0) |
| 400 | /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS |
| 401 | * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data |
| 402 | * bytes are actual the address, they go to the bus anyhow |
| 403 | */ |
| 404 | spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS; |
| 405 | else if (writecnt == 1) // and readcnt is > 0 |
| 406 | spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS; |
| 407 | else if (writecnt == 4) // and readcnt is > 0 |
| 408 | spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS; |
Stefan Tauner | dc704ed | 2012-05-06 15:11:26 +0000 | [diff] [blame] | 409 | else // we have an invalid case |
| 410 | return SPI_INVALID_LENGTH; |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame] | 411 | } |
Stefan Tauner | dc704ed | 2012-05-06 15:11:26 +0000 | [diff] [blame] | 412 | int oppos = 2; // use original JEDEC_BE_D8 offset |
| 413 | curopcodes->opcode[oppos].opcode = opcode; |
| 414 | curopcodes->opcode[oppos].spi_type = spi_type; |
| 415 | program_opcodes(curopcodes, 0); |
| 416 | oppos = find_opcode(curopcodes, opcode); |
Stefan Tauner | d94d25d | 2012-07-28 03:17:15 +0000 | [diff] [blame] | 417 | msg_pdbg2("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos); |
Stefan Tauner | dc704ed | 2012-05-06 15:11:26 +0000 | [diff] [blame] | 418 | return oppos; |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 421 | static int find_opcode(OPCODES *op, uint8_t opcode) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 422 | { |
| 423 | int a; |
| 424 | |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 425 | if (op == NULL) { |
| 426 | msg_perr("\n%s: null OPCODES pointer!\n", __func__); |
| 427 | return -1; |
| 428 | } |
| 429 | |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 430 | for (a = 0; a < 8; a++) { |
| 431 | if (op->opcode[a].opcode == opcode) |
| 432 | return a; |
| 433 | } |
| 434 | |
| 435 | return -1; |
| 436 | } |
| 437 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 438 | static int find_preop(OPCODES *op, uint8_t preop) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 439 | { |
| 440 | int a; |
| 441 | |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 442 | if (op == NULL) { |
| 443 | msg_perr("\n%s: null OPCODES pointer!\n", __func__); |
| 444 | return -1; |
| 445 | } |
| 446 | |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 447 | for (a = 0; a < 2; a++) { |
| 448 | if (op->preop[a] == preop) |
| 449 | return a; |
| 450 | } |
| 451 | |
| 452 | return -1; |
| 453 | } |
| 454 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 455 | /* 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] | 456 | static int generate_opcodes(OPCODES * op) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 457 | { |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 458 | int a; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 459 | uint16_t preop, optype; |
| 460 | uint32_t opmenu[2]; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 461 | |
| 462 | if (op == NULL) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 463 | msg_perr("\n%s: null OPCODES pointer!\n", __func__); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 464 | return -1; |
| 465 | } |
| 466 | |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 467 | switch (ich_generation) { |
| 468 | case CHIPSET_ICH7: |
Stefan Tauner | 92d6a86 | 2013-10-25 00:33:37 +0000 | [diff] [blame^] | 469 | case CHIPSET_TUNNEL_CREEK: |
| 470 | case CHIPSET_CENTERTON: |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 471 | preop = REGREAD16(ICH7_REG_PREOP); |
| 472 | optype = REGREAD16(ICH7_REG_OPTYPE); |
| 473 | opmenu[0] = REGREAD32(ICH7_REG_OPMENU); |
| 474 | opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4); |
| 475 | break; |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 476 | case CHIPSET_ICH8: |
| 477 | default: /* Future version might behave the same */ |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 478 | preop = REGREAD16(ICH9_REG_PREOP); |
| 479 | optype = REGREAD16(ICH9_REG_OPTYPE); |
| 480 | opmenu[0] = REGREAD32(ICH9_REG_OPMENU); |
| 481 | opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4); |
| 482 | break; |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | op->preop[0] = (uint8_t) preop; |
| 486 | op->preop[1] = (uint8_t) (preop >> 8); |
| 487 | |
| 488 | for (a = 0; a < 8; a++) { |
| 489 | op->opcode[a].spi_type = (uint8_t) (optype & 0x3); |
| 490 | optype >>= 2; |
| 491 | } |
| 492 | |
| 493 | for (a = 0; a < 4; a++) { |
| 494 | op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff); |
| 495 | opmenu[0] >>= 8; |
| 496 | } |
| 497 | |
| 498 | for (a = 4; a < 8; a++) { |
| 499 | op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff); |
| 500 | opmenu[1] >>= 8; |
| 501 | } |
| 502 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 503 | /* No preopcodes used by default. */ |
| 504 | for (a = 0; a < 8; a++) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 505 | op->opcode[a].atomic = 0; |
| 506 | |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 510 | static int program_opcodes(OPCODES *op, int enable_undo) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 511 | { |
| 512 | uint8_t a; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 513 | uint16_t preop, optype; |
| 514 | uint32_t opmenu[2]; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 515 | |
| 516 | /* Program Prefix Opcodes */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 517 | /* 0:7 Prefix Opcode 1 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 518 | preop = (op->preop[0]); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 519 | /* 8:16 Prefix Opcode 2 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 520 | preop |= ((uint16_t) op->preop[1]) << 8; |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 521 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 522 | /* Program Opcode Types 0 - 7 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 523 | optype = 0; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 524 | for (a = 0; a < 8; a++) { |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 525 | optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 526 | } |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 527 | |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 528 | /* Program Allowable Opcodes 0 - 3 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 529 | opmenu[0] = 0; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 530 | for (a = 0; a < 4; a++) { |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 531 | opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 532 | } |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 533 | |
Stefan Tauner | 92d6a86 | 2013-10-25 00:33:37 +0000 | [diff] [blame^] | 534 | /* Program Allowable Opcodes 4 - 7 */ |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 535 | opmenu[1] = 0; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 536 | for (a = 4; a < 8; a++) { |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 537 | opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 538 | } |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 539 | |
Stefan Tauner | d94d25d | 2012-07-28 03:17:15 +0000 | [diff] [blame] | 540 | msg_pdbg2("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]); |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 541 | switch (ich_generation) { |
| 542 | case CHIPSET_ICH7: |
Stefan Tauner | 92d6a86 | 2013-10-25 00:33:37 +0000 | [diff] [blame^] | 543 | case CHIPSET_TUNNEL_CREEK: |
| 544 | case CHIPSET_CENTERTON: |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 545 | /* Register undo only for enable_undo=1, i.e. first call. */ |
| 546 | if (enable_undo) { |
| 547 | rmmio_valw(ich_spibar + ICH7_REG_PREOP); |
| 548 | rmmio_valw(ich_spibar + ICH7_REG_OPTYPE); |
| 549 | rmmio_vall(ich_spibar + ICH7_REG_OPMENU); |
| 550 | rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4); |
| 551 | } |
| 552 | mmio_writew(preop, ich_spibar + ICH7_REG_PREOP); |
| 553 | mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE); |
| 554 | mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU); |
| 555 | mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 556 | break; |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 557 | case CHIPSET_ICH8: |
| 558 | default: /* Future version might behave the same */ |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 559 | /* Register undo only for enable_undo=1, i.e. first call. */ |
| 560 | if (enable_undo) { |
| 561 | rmmio_valw(ich_spibar + ICH9_REG_PREOP); |
| 562 | rmmio_valw(ich_spibar + ICH9_REG_OPTYPE); |
| 563 | rmmio_vall(ich_spibar + ICH9_REG_OPMENU); |
| 564 | rmmio_vall(ich_spibar + ICH9_REG_OPMENU + 4); |
| 565 | } |
| 566 | mmio_writew(preop, ich_spibar + ICH9_REG_PREOP); |
| 567 | mmio_writew(optype, ich_spibar + ICH9_REG_OPTYPE); |
| 568 | mmio_writel(opmenu[0], ich_spibar + ICH9_REG_OPMENU); |
| 569 | mmio_writel(opmenu[1], ich_spibar + ICH9_REG_OPMENU + 4); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 570 | break; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 571 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 576 | /* |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 577 | * Returns -1 if at least one mandatory opcode is inaccessible, 0 otherwise. |
| 578 | * FIXME: this should also check for |
| 579 | * - at least one probing opcode (RDID (incl. AT25F variants?), REMS, RES?) |
| 580 | * - at least one erasing opcode (lots.) |
| 581 | * - at least one program opcode (BYTE_PROGRAM, AAI_WORD_PROGRAM, ...?) |
| 582 | * - necessary preops? (EWSR, WREN, ...?) |
| 583 | */ |
| 584 | static int ich_missing_opcodes() |
| 585 | { |
| 586 | uint8_t ops[] = { |
| 587 | JEDEC_READ, |
| 588 | JEDEC_RDSR, |
| 589 | 0 |
| 590 | }; |
| 591 | int i = 0; |
| 592 | while (ops[i] != 0) { |
| 593 | msg_pspew("checking for opcode 0x%02x\n", ops[i]); |
| 594 | if (find_opcode(curopcodes, ops[i]) == -1) |
| 595 | return -1; |
| 596 | i++; |
| 597 | } |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | /* |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 602 | * Try to set BBAR (BIOS Base Address Register), but read back the value in case |
| 603 | * it didn't stick. |
| 604 | */ |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 605 | static void ich_set_bbar(uint32_t min_addr) |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 606 | { |
Stefan Tauner | e27b2d4 | 2011-07-01 00:39:09 +0000 | [diff] [blame] | 607 | int bbar_off; |
Stefan Tauner | 7783f31 | 2011-09-17 21:21:42 +0000 | [diff] [blame] | 608 | switch (ich_generation) { |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 609 | case CHIPSET_ICH7: |
Stefan Tauner | 92d6a86 | 2013-10-25 00:33:37 +0000 | [diff] [blame^] | 610 | case CHIPSET_TUNNEL_CREEK: |
| 611 | case CHIPSET_CENTERTON: |
Stefan Tauner | e27b2d4 | 2011-07-01 00:39:09 +0000 | [diff] [blame] | 612 | bbar_off = 0x50; |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 613 | break; |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 614 | case CHIPSET_ICH8: |
Stefan Tauner | 7783f31 | 2011-09-17 21:21:42 +0000 | [diff] [blame] | 615 | msg_perr("BBAR offset is unknown on ICH8!\n"); |
| 616 | return; |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 617 | case CHIPSET_ICH9: |
Stefan Tauner | 7783f31 | 2011-09-17 21:21:42 +0000 | [diff] [blame] | 618 | default: /* Future version might behave the same */ |
Stefan Tauner | e27b2d4 | 2011-07-01 00:39:09 +0000 | [diff] [blame] | 619 | bbar_off = ICH9_REG_BBAR; |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 620 | break; |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 621 | } |
Stefan Tauner | e27b2d4 | 2011-07-01 00:39:09 +0000 | [diff] [blame] | 622 | |
| 623 | ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & ~BBAR_MASK; |
| 624 | if (ichspi_bbar) { |
| 625 | msg_pdbg("Reserved bits in BBAR not zero: 0x%08x\n", |
| 626 | ichspi_bbar); |
| 627 | } |
| 628 | min_addr &= BBAR_MASK; |
| 629 | ichspi_bbar |= min_addr; |
| 630 | rmmio_writel(ichspi_bbar, ich_spibar + bbar_off); |
| 631 | ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & BBAR_MASK; |
| 632 | |
| 633 | /* We don't have any option except complaining. And if the write |
| 634 | * failed, the restore will fail as well, so no problem there. |
| 635 | */ |
| 636 | if (ichspi_bbar != min_addr) |
Stefan Tauner | 7783f31 | 2011-09-17 21:21:42 +0000 | [diff] [blame] | 637 | msg_perr("Setting BBAR to 0x%08x failed! New value: 0x%08x.\n", |
| 638 | min_addr, ichspi_bbar); |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Stefan Tauner | 8b391b8 | 2011-08-09 01:49:34 +0000 | [diff] [blame] | 641 | /* Read len bytes from the fdata/spid register into the data array. |
| 642 | * |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 643 | * Note that using len > flash->pgm->spi.max_data_read will return garbage or |
Stefan Tauner | 8b391b8 | 2011-08-09 01:49:34 +0000 | [diff] [blame] | 644 | * may even crash. |
| 645 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 646 | static void ich_read_data(uint8_t *data, int len, int reg0_off) |
Stefan Tauner | 8b391b8 | 2011-08-09 01:49:34 +0000 | [diff] [blame] | 647 | { |
| 648 | int i; |
| 649 | uint32_t temp32 = 0; |
| 650 | |
| 651 | for (i = 0; i < len; i++) { |
| 652 | if ((i % 4) == 0) |
| 653 | temp32 = REGREAD32(reg0_off + i); |
| 654 | |
| 655 | data[i] = (temp32 >> ((i % 4) * 8)) & 0xff; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | /* Fill len bytes from the data array into the fdata/spid registers. |
| 660 | * |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 661 | * Note that using len > flash->pgm->spi.max_data_write will trash the registers |
Stefan Tauner | 8b391b8 | 2011-08-09 01:49:34 +0000 | [diff] [blame] | 662 | * following the data registers. |
| 663 | */ |
| 664 | static void ich_fill_data(const uint8_t *data, int len, int reg0_off) |
| 665 | { |
| 666 | uint32_t temp32 = 0; |
| 667 | int i; |
| 668 | |
| 669 | if (len <= 0) |
| 670 | return; |
| 671 | |
| 672 | for (i = 0; i < len; i++) { |
| 673 | if ((i % 4) == 0) |
| 674 | temp32 = 0; |
| 675 | |
| 676 | temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8); |
| 677 | |
| 678 | if ((i % 4) == 3) /* 32 bits are full, write them to regs. */ |
| 679 | REGWRITE32(reg0_off + (i - (i % 4)), temp32); |
| 680 | } |
| 681 | i--; |
| 682 | if ((i % 4) != 3) /* Write remaining data to regs. */ |
| 683 | REGWRITE32(reg0_off + (i - (i % 4)), temp32); |
| 684 | } |
| 685 | |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 686 | /* This function generates OPCODES from or programs OPCODES to ICH according to |
| 687 | * the chipset's SPI configuration lock. |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 688 | * |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 689 | * It should be called before ICH sends any spi command. |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 690 | */ |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 691 | static int ich_init_opcodes(void) |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 692 | { |
| 693 | int rc = 0; |
| 694 | OPCODES *curopcodes_done; |
| 695 | |
| 696 | if (curopcodes) |
| 697 | return 0; |
| 698 | |
| 699 | if (ichspi_lock) { |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 700 | msg_pdbg("Reading OPCODES... "); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 701 | curopcodes_done = &O_EXISTING; |
FENG yu ning | f041e9b | 2008-12-15 02:32:11 +0000 | [diff] [blame] | 702 | rc = generate_opcodes(curopcodes_done); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 703 | } else { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 704 | msg_pdbg("Programming OPCODES... "); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 705 | curopcodes_done = &O_ST_M25P; |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 706 | rc = program_opcodes(curopcodes_done, 1); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | if (rc) { |
| 710 | curopcodes = NULL; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 711 | msg_perr("failed\n"); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 712 | return 1; |
| 713 | } else { |
| 714 | curopcodes = curopcodes_done; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 715 | msg_pdbg("done\n"); |
Stefan Tauner | 8b391b8 | 2011-08-09 01:49:34 +0000 | [diff] [blame] | 716 | prettyprint_opcodes(curopcodes); |
FENG yu ning | c05a295 | 2008-12-08 18:16:58 +0000 | [diff] [blame] | 717 | return 0; |
| 718 | } |
| 719 | } |
| 720 | |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 721 | static int ich7_run_opcode(OPCODE op, uint32_t offset, |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 722 | uint8_t datalength, uint8_t * data, int maxdata) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 723 | { |
| 724 | int write_cmd = 0; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 725 | int timeout; |
Stefan Tauner | 8b391b8 | 2011-08-09 01:49:34 +0000 | [diff] [blame] | 726 | uint32_t temp32; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 727 | uint16_t temp16; |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 728 | uint64_t opmenu; |
| 729 | int opcode_index; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 730 | |
| 731 | /* Is it a write command? */ |
| 732 | if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) |
| 733 | || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) { |
| 734 | write_cmd = 1; |
| 735 | } |
| 736 | |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 737 | timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */ |
| 738 | while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) { |
| 739 | programmer_delay(10); |
| 740 | } |
| 741 | if (!timeout) { |
| 742 | msg_perr("Error: SCIP never cleared!\n"); |
| 743 | return 1; |
| 744 | } |
| 745 | |
Stefan Tauner | 10b3e22 | 2011-07-01 00:39:23 +0000 | [diff] [blame] | 746 | /* Program offset in flash into SPIA while preserving reserved bits. */ |
| 747 | temp32 = REGREAD32(ICH7_REG_SPIA) & ~0x00FFFFFF; |
| 748 | REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF) | temp32); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 749 | |
Stefan Tauner | 10b3e22 | 2011-07-01 00:39:23 +0000 | [diff] [blame] | 750 | /* Program data into SPID0 to N */ |
Stefan Tauner | 8b391b8 | 2011-08-09 01:49:34 +0000 | [diff] [blame] | 751 | if (write_cmd && (datalength != 0)) |
| 752 | ich_fill_data(data, datalength, ICH7_REG_SPID0); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 753 | |
| 754 | /* Assemble SPIS */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 755 | temp16 = REGREAD16(ICH7_REG_SPIS); |
| 756 | /* keep reserved bits */ |
| 757 | temp16 &= SPIS_RESERVED_MASK; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 758 | /* clear error status registers */ |
Stefan Tauner | 0c1ec45 | 2011-06-11 09:53:09 +0000 | [diff] [blame] | 759 | temp16 |= (SPIS_CDS | SPIS_FCERR); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 760 | REGWRITE16(ICH7_REG_SPIS, temp16); |
| 761 | |
| 762 | /* Assemble SPIC */ |
| 763 | temp16 = 0; |
| 764 | |
| 765 | if (datalength != 0) { |
| 766 | temp16 |= SPIC_DS; |
Rudolf Marek | 3fdbccf | 2008-06-30 21:38:30 +0000 | [diff] [blame] | 767 | temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /* Select opcode */ |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 771 | opmenu = REGREAD32(ICH7_REG_OPMENU); |
| 772 | opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32; |
| 773 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 774 | for (opcode_index = 0; opcode_index < 8; opcode_index++) { |
| 775 | if ((opmenu & 0xff) == op.opcode) { |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 776 | break; |
| 777 | } |
| 778 | opmenu >>= 8; |
| 779 | } |
| 780 | if (opcode_index == 8) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 781 | msg_pdbg("Opcode %x not found.\n", op.opcode); |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 782 | return 1; |
| 783 | } |
| 784 | temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 785 | |
Michael Karcher | 136125a | 2011-04-29 22:11:36 +0000 | [diff] [blame] | 786 | timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */ |
| 787 | /* Handle Atomic. Atomic commands include three steps: |
| 788 | - sending the preop (mainly EWSR or WREN) |
| 789 | - sending the main command |
| 790 | - waiting for the busy bit (WIP) to be cleared |
| 791 | This means the timeout must be sufficient for chip erase |
| 792 | of slow high-capacity chips. |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 793 | */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 794 | switch (op.atomic) { |
| 795 | case 2: |
| 796 | /* Select second preop. */ |
| 797 | temp16 |= SPIC_SPOP; |
| 798 | /* And fall through. */ |
| 799 | case 1: |
| 800 | /* Atomic command (preop+op) */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 801 | temp16 |= SPIC_ACS; |
Michael Karcher | 136125a | 2011-04-29 22:11:36 +0000 | [diff] [blame] | 802 | timeout = 100 * 1000 * 60; /* 60 seconds */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 803 | break; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | /* Start */ |
| 807 | temp16 |= SPIC_SCGO; |
| 808 | |
| 809 | /* write it */ |
| 810 | REGWRITE16(ICH7_REG_SPIC, temp16); |
| 811 | |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 812 | /* Wait for Cycle Done Status or Flash Cycle Error. */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 813 | while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) && |
| 814 | --timeout) { |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 815 | programmer_delay(10); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 816 | } |
| 817 | if (!timeout) { |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 818 | msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n", |
| 819 | REGREAD16(ICH7_REG_SPIS)); |
| 820 | return 1; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 823 | /* FIXME: make sure we do not needlessly cause transaction errors. */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 824 | temp16 = REGREAD16(ICH7_REG_SPIS); |
| 825 | if (temp16 & SPIS_FCERR) { |
Stefan Tauner | 8ed2934 | 2011-04-29 23:53:09 +0000 | [diff] [blame] | 826 | msg_perr("Transaction error!\n"); |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 827 | /* keep reserved bits */ |
| 828 | temp16 &= SPIS_RESERVED_MASK; |
| 829 | REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 830 | return 1; |
| 831 | } |
| 832 | |
Stefan Tauner | 8b391b8 | 2011-08-09 01:49:34 +0000 | [diff] [blame] | 833 | if ((!write_cmd) && (datalength != 0)) |
| 834 | ich_read_data(data, datalength, ICH7_REG_SPID0); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 835 | |
| 836 | return 0; |
| 837 | } |
| 838 | |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 839 | static int ich9_run_opcode(OPCODE op, uint32_t offset, |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 840 | uint8_t datalength, uint8_t * data) |
| 841 | { |
| 842 | int write_cmd = 0; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 843 | int timeout; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 844 | uint32_t temp32; |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 845 | uint64_t opmenu; |
| 846 | int opcode_index; |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 847 | |
| 848 | /* Is it a write command? */ |
| 849 | if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) |
| 850 | || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) { |
| 851 | write_cmd = 1; |
| 852 | } |
| 853 | |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 854 | timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */ |
| 855 | while ((REGREAD8(ICH9_REG_SSFS) & SSFS_SCIP) && --timeout) { |
| 856 | programmer_delay(10); |
| 857 | } |
| 858 | if (!timeout) { |
| 859 | msg_perr("Error: SCIP never cleared!\n"); |
| 860 | return 1; |
| 861 | } |
| 862 | |
Stefan Tauner | 10b3e22 | 2011-07-01 00:39:23 +0000 | [diff] [blame] | 863 | /* Program offset in flash into FADDR while preserve the reserved bits |
| 864 | * and clearing the 25. address bit which is only useable in hwseq. */ |
| 865 | temp32 = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF; |
| 866 | REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF) | temp32); |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 867 | |
| 868 | /* Program data into FDATA0 to N */ |
Stefan Tauner | 8b391b8 | 2011-08-09 01:49:34 +0000 | [diff] [blame] | 869 | if (write_cmd && (datalength != 0)) |
| 870 | ich_fill_data(data, datalength, ICH9_REG_FDATA0); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 871 | |
| 872 | /* Assemble SSFS + SSFC */ |
Helge Wagner | a319be1 | 2010-08-11 21:06:10 +0000 | [diff] [blame] | 873 | temp32 = REGREAD32(ICH9_REG_SSFS); |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 874 | /* Keep reserved bits only */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 875 | temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK; |
Stefan Tauner | 0c1ec45 | 2011-06-11 09:53:09 +0000 | [diff] [blame] | 876 | /* Clear cycle done and cycle error status registers */ |
| 877 | temp32 |= (SSFS_FDONE | SSFS_FCERR); |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 878 | REGWRITE32(ICH9_REG_SSFS, temp32); |
| 879 | |
Uwe Hermann | 4e3d0b3 | 2010-03-25 23:18:41 +0000 | [diff] [blame] | 880 | /* Use 20 MHz */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 881 | temp32 |= SSFC_SCF_20MHZ; |
| 882 | |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 883 | /* Set data byte count (DBC) and data cycle bit (DS) */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 884 | if (datalength != 0) { |
| 885 | uint32_t datatemp; |
| 886 | temp32 |= SSFC_DS; |
Stefan Tauner | 0c1ec45 | 2011-06-11 09:53:09 +0000 | [diff] [blame] | 887 | datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) & |
| 888 | SSFC_DBC); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 889 | temp32 |= datatemp; |
| 890 | } |
| 891 | |
| 892 | /* Select opcode */ |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 893 | opmenu = REGREAD32(ICH9_REG_OPMENU); |
| 894 | opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32; |
| 895 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 896 | for (opcode_index = 0; opcode_index < 8; opcode_index++) { |
| 897 | if ((opmenu & 0xff) == op.opcode) { |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 898 | break; |
| 899 | } |
| 900 | opmenu >>= 8; |
| 901 | } |
| 902 | if (opcode_index == 8) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 903 | msg_pdbg("Opcode %x not found.\n", op.opcode); |
Stefan Reinauer | 4311956 | 2008-11-02 19:51:50 +0000 | [diff] [blame] | 904 | return 1; |
| 905 | } |
| 906 | temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 907 | |
Michael Karcher | 136125a | 2011-04-29 22:11:36 +0000 | [diff] [blame] | 908 | timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */ |
| 909 | /* Handle Atomic. Atomic commands include three steps: |
| 910 | - sending the preop (mainly EWSR or WREN) |
| 911 | - sending the main command |
| 912 | - waiting for the busy bit (WIP) to be cleared |
| 913 | This means the timeout must be sufficient for chip erase |
| 914 | of slow high-capacity chips. |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 915 | */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 916 | switch (op.atomic) { |
| 917 | case 2: |
| 918 | /* Select second preop. */ |
| 919 | temp32 |= SSFC_SPOP; |
| 920 | /* And fall through. */ |
| 921 | case 1: |
| 922 | /* Atomic command (preop+op) */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 923 | temp32 |= SSFC_ACS; |
Michael Karcher | 136125a | 2011-04-29 22:11:36 +0000 | [diff] [blame] | 924 | timeout = 100 * 1000 * 60; /* 60 seconds */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 925 | break; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | /* Start */ |
| 929 | temp32 |= SSFC_SCGO; |
| 930 | |
| 931 | /* write it */ |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 932 | REGWRITE32(ICH9_REG_SSFS, temp32); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 933 | |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 934 | /* Wait for Cycle Done Status or Flash Cycle Error. */ |
Stefan Tauner | 0c1ec45 | 2011-06-11 09:53:09 +0000 | [diff] [blame] | 935 | while (((REGREAD32(ICH9_REG_SSFS) & (SSFS_FDONE | SSFS_FCERR)) == 0) && |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 936 | --timeout) { |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 937 | programmer_delay(10); |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 938 | } |
| 939 | if (!timeout) { |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 940 | msg_perr("timeout, ICH9_REG_SSFS=0x%08x\n", |
| 941 | REGREAD32(ICH9_REG_SSFS)); |
| 942 | return 1; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 945 | /* FIXME make sure we do not needlessly cause transaction errors. */ |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 946 | temp32 = REGREAD32(ICH9_REG_SSFS); |
| 947 | if (temp32 & SSFS_FCERR) { |
Stefan Tauner | 8ed2934 | 2011-04-29 23:53:09 +0000 | [diff] [blame] | 948 | msg_perr("Transaction error!\n"); |
Stefan Tauner | 2a8b262 | 2011-06-11 09:53:16 +0000 | [diff] [blame] | 949 | prettyprint_ich9_reg_ssfs(temp32); |
| 950 | prettyprint_ich9_reg_ssfc(temp32); |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 951 | /* keep reserved bits */ |
| 952 | temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK; |
| 953 | /* Clear the transaction error. */ |
| 954 | REGWRITE32(ICH9_REG_SSFS, temp32 | SSFS_FCERR); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 955 | return 1; |
| 956 | } |
| 957 | |
Stefan Tauner | 8b391b8 | 2011-08-09 01:49:34 +0000 | [diff] [blame] | 958 | if ((!write_cmd) && (datalength != 0)) |
| 959 | ich_read_data(data, datalength, ICH9_REG_FDATA0); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 960 | |
| 961 | return 0; |
| 962 | } |
| 963 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 964 | static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset, |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 965 | uint8_t datalength, uint8_t * data) |
| 966 | { |
Stefan Tauner | b2d5f6a | 2011-06-11 19:44:31 +0000 | [diff] [blame] | 967 | /* max_data_read == max_data_write for all Intel/VIA SPI masters */ |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 968 | uint8_t maxlength = flash->pgm->spi.max_data_read; |
Stefan Tauner | b2d5f6a | 2011-06-11 19:44:31 +0000 | [diff] [blame] | 969 | |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 970 | if (ich_generation == CHIPSET_ICH_UNKNOWN) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 971 | msg_perr("%s: unsupported chipset\n", __func__); |
Stefan Tauner | b2d5f6a | 2011-06-11 19:44:31 +0000 | [diff] [blame] | 972 | return -1; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 973 | } |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 974 | |
Stefan Tauner | b2d5f6a | 2011-06-11 19:44:31 +0000 | [diff] [blame] | 975 | if (datalength > maxlength) { |
| 976 | msg_perr("%s: Internal command size error for " |
| 977 | "opcode 0x%02x, got datalength=%i, want <=%i\n", |
| 978 | __func__, op.opcode, datalength, maxlength); |
| 979 | return SPI_INVALID_LENGTH; |
| 980 | } |
| 981 | |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 982 | switch (ich_generation) { |
| 983 | case CHIPSET_ICH7: |
Stefan Tauner | 92d6a86 | 2013-10-25 00:33:37 +0000 | [diff] [blame^] | 984 | case CHIPSET_TUNNEL_CREEK: |
| 985 | case CHIPSET_CENTERTON: |
Stefan Tauner | b2d5f6a | 2011-06-11 19:44:31 +0000 | [diff] [blame] | 986 | return ich7_run_opcode(op, offset, datalength, data, maxlength); |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 987 | case CHIPSET_ICH8: |
| 988 | default: /* Future version might behave the same */ |
Stefan Tauner | b2d5f6a | 2011-06-11 19:44:31 +0000 | [diff] [blame] | 989 | return ich9_run_opcode(op, offset, datalength, data); |
Stefan Tauner | b2d5f6a | 2011-06-11 19:44:31 +0000 | [diff] [blame] | 990 | } |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 993 | static int ich_spi_send_command(struct flashctx *flash, unsigned int writecnt, |
| 994 | unsigned int readcnt, |
| 995 | const unsigned char *writearr, |
| 996 | unsigned char *readarr) |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 997 | { |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 998 | int result; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 999 | int opcode_index = -1; |
| 1000 | const unsigned char cmd = *writearr; |
| 1001 | OPCODE *opcode; |
| 1002 | uint32_t addr = 0; |
| 1003 | uint8_t *data; |
| 1004 | int count; |
| 1005 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 1006 | /* find cmd in opcodes-table */ |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1007 | opcode_index = find_opcode(curopcodes, cmd); |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 1008 | if (opcode_index == -1) { |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame] | 1009 | if (!ichspi_lock) |
| 1010 | opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt); |
Stefan Tauner | dc704ed | 2012-05-06 15:11:26 +0000 | [diff] [blame] | 1011 | if (opcode_index == SPI_INVALID_LENGTH) { |
| 1012 | msg_pdbg("OPCODE 0x%02x has unsupported length, will not execute.\n", cmd); |
| 1013 | return SPI_INVALID_LENGTH; |
| 1014 | } else if (opcode_index == -1) { |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 1015 | msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n", |
| 1016 | cmd); |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame] | 1017 | return SPI_INVALID_OPCODE; |
| 1018 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | opcode = &(curopcodes->opcode[opcode_index]); |
| 1022 | |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1023 | /* The following valid writecnt/readcnt combinations exist: |
| 1024 | * writecnt = 4, readcnt >= 0 |
| 1025 | * writecnt = 1, readcnt >= 0 |
| 1026 | * writecnt >= 4, readcnt = 0 |
| 1027 | * writecnt >= 1, readcnt = 0 |
| 1028 | * writecnt >= 1 is guaranteed for all commands. |
| 1029 | */ |
| 1030 | if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) && |
| 1031 | (writecnt != 4)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1032 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1033 | "0x%02x, got writecnt=%i, want =4\n", __func__, cmd, |
| 1034 | writecnt); |
| 1035 | return SPI_INVALID_LENGTH; |
| 1036 | } |
| 1037 | if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) && |
| 1038 | (writecnt != 1)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1039 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1040 | "0x%02x, got writecnt=%i, want =1\n", __func__, cmd, |
| 1041 | writecnt); |
| 1042 | return SPI_INVALID_LENGTH; |
| 1043 | } |
| 1044 | if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) && |
| 1045 | (writecnt < 4)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1046 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1047 | "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd, |
| 1048 | writecnt); |
| 1049 | return SPI_INVALID_LENGTH; |
| 1050 | } |
| 1051 | if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) || |
| 1052 | (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) && |
| 1053 | (readcnt)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1054 | msg_perr("%s: Internal command size error for opcode " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1055 | "0x%02x, got readcnt=%i, want =0\n", __func__, cmd, |
| 1056 | readcnt); |
| 1057 | return SPI_INVALID_LENGTH; |
| 1058 | } |
| 1059 | |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 1060 | /* if opcode-type requires an address */ |
| 1061 | if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS || |
| 1062 | opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) { |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 1063 | addr = (writearr[1] << 16) | |
| 1064 | (writearr[2] << 8) | (writearr[3] << 0); |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 1065 | if (addr < ichspi_bbar) { |
| 1066 | msg_perr("%s: Address 0x%06x below allowed " |
| 1067 | "range 0x%06x-0xffffff\n", __func__, |
| 1068 | addr, ichspi_bbar); |
| 1069 | return SPI_INVALID_ADDRESS; |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 1070 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 1071 | } |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 1072 | |
Stefan Tauner | b2d5f6a | 2011-06-11 19:44:31 +0000 | [diff] [blame] | 1073 | /* Translate read/write array/count. |
| 1074 | * The maximum data length is identical for the maximum read length and |
| 1075 | * for the maximum write length excluding opcode and address. Opcode and |
| 1076 | * address are stored in separate registers, not in the data registers |
| 1077 | * and are thus not counted towards data length. The only exception |
| 1078 | * applies if the opcode definition (un)intentionally classifies said |
| 1079 | * opcode incorrectly as non-address opcode or vice versa. */ |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 1080 | if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) { |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 1081 | data = (uint8_t *) (writearr + 1); |
| 1082 | count = writecnt - 1; |
| 1083 | } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) { |
| 1084 | data = (uint8_t *) (writearr + 4); |
| 1085 | count = writecnt - 4; |
| 1086 | } else { |
| 1087 | data = (uint8_t *) readarr; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 1088 | count = readcnt; |
| 1089 | } |
Stefan Reinauer | 325b5d4 | 2008-06-27 15:18:20 +0000 | [diff] [blame] | 1090 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 1091 | result = run_opcode(flash, *opcode, addr, count, data); |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 1092 | if (result) { |
Stefan Tauner | 8ed2934 | 2011-04-29 23:53:09 +0000 | [diff] [blame] | 1093 | msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode); |
| 1094 | if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) || |
| 1095 | (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) { |
| 1096 | msg_pdbg("at address 0x%06x ", addr); |
| 1097 | } |
| 1098 | msg_pdbg("(payload length was %d).\n", count); |
| 1099 | |
| 1100 | /* Print out the data array if it contains data to write. |
| 1101 | * Errors are detected before the received data is read back into |
| 1102 | * the array so it won't make sense to print it then. */ |
| 1103 | if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) || |
| 1104 | (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) { |
| 1105 | int i; |
| 1106 | msg_pspew("The data was:\n"); |
Stefan Tauner | f382e35 | 2011-11-08 11:55:24 +0000 | [diff] [blame] | 1107 | for (i = 0; i < count; i++){ |
Stefan Tauner | 8ed2934 | 2011-04-29 23:53:09 +0000 | [diff] [blame] | 1108 | msg_pspew("%3d: 0x%02x\n", i, data[i]); |
| 1109 | } |
| 1110 | } |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 1113 | return result; |
Dominik Geyer | b46acba | 2008-05-16 12:55:55 +0000 | [diff] [blame] | 1114 | } |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 1115 | |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1116 | static struct hwseq_data { |
| 1117 | uint32_t size_comp0; |
| 1118 | uint32_t size_comp1; |
| 1119 | } hwseq_data; |
| 1120 | |
Stefan Tauner | d0c5dc2 | 2011-10-20 12:57:14 +0000 | [diff] [blame] | 1121 | /* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */ |
| 1122 | static void ich_hwseq_set_addr(uint32_t addr) |
| 1123 | { |
| 1124 | uint32_t addr_old = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF; |
| 1125 | REGWRITE32(ICH9_REG_FADDR, (addr & 0x01FFFFFF) | addr_old); |
| 1126 | } |
| 1127 | |
| 1128 | /* Sets FADDR.FLA to 'addr' and returns the erase block size in bytes |
| 1129 | * of the block containing this address. May return nonsense if the address is |
| 1130 | * not valid. The erase block size for a specific address depends on the flash |
| 1131 | * partition layout as specified by FPB and the partition properties as defined |
| 1132 | * by UVSCC and LVSCC respectively. An alternative to implement this method |
| 1133 | * would be by querying FPB and the respective VSCC register directly. |
| 1134 | */ |
| 1135 | static uint32_t ich_hwseq_get_erase_block_size(unsigned int addr) |
| 1136 | { |
| 1137 | uint8_t enc_berase; |
Stefan Tauner | 67d163d | 2013-01-15 17:37:48 +0000 | [diff] [blame] | 1138 | static const uint32_t dec_berase[4] = { |
Stefan Tauner | d0c5dc2 | 2011-10-20 12:57:14 +0000 | [diff] [blame] | 1139 | 256, |
| 1140 | 4 * 1024, |
| 1141 | 8 * 1024, |
| 1142 | 64 * 1024 |
| 1143 | }; |
| 1144 | |
| 1145 | ich_hwseq_set_addr(addr); |
| 1146 | enc_berase = (REGREAD16(ICH9_REG_HSFS) & HSFS_BERASE) >> |
| 1147 | HSFS_BERASE_OFF; |
| 1148 | return dec_berase[enc_berase]; |
| 1149 | } |
| 1150 | |
| 1151 | /* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals. |
| 1152 | Resets all error flags in HSFS. |
| 1153 | Returns 0 if the cycle completes successfully without errors within |
| 1154 | timeout us, 1 on errors. */ |
| 1155 | static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout, |
| 1156 | unsigned int len) |
| 1157 | { |
| 1158 | uint16_t hsfs; |
| 1159 | uint32_t addr; |
| 1160 | |
| 1161 | timeout /= 8; /* scale timeout duration to counter */ |
| 1162 | while ((((hsfs = REGREAD16(ICH9_REG_HSFS)) & |
| 1163 | (HSFS_FDONE | HSFS_FCERR)) == 0) && |
| 1164 | --timeout) { |
| 1165 | programmer_delay(8); |
| 1166 | } |
| 1167 | REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS)); |
| 1168 | if (!timeout) { |
| 1169 | addr = REGREAD32(ICH9_REG_FADDR) & 0x01FFFFFF; |
| 1170 | msg_perr("Timeout error between offset 0x%08x and " |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1171 | "0x%08x (= 0x%08x + %d)!\n", |
| 1172 | addr, addr + len - 1, addr, len - 1); |
Stefan Tauner | d0c5dc2 | 2011-10-20 12:57:14 +0000 | [diff] [blame] | 1173 | prettyprint_ich9_reg_hsfs(hsfs); |
| 1174 | prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC)); |
| 1175 | return 1; |
| 1176 | } |
| 1177 | |
| 1178 | if (hsfs & HSFS_FCERR) { |
| 1179 | addr = REGREAD32(ICH9_REG_FADDR) & 0x01FFFFFF; |
| 1180 | msg_perr("Transaction error between offset 0x%08x and " |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1181 | "0x%08x (= 0x%08x + %d)!\n", |
Stefan Tauner | d0c5dc2 | 2011-10-20 12:57:14 +0000 | [diff] [blame] | 1182 | addr, addr + len - 1, addr, len - 1); |
| 1183 | prettyprint_ich9_reg_hsfs(hsfs); |
| 1184 | prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC)); |
| 1185 | return 1; |
| 1186 | } |
| 1187 | return 0; |
| 1188 | } |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1189 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 1190 | static int ich_hwseq_probe(struct flashctx *flash) |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1191 | { |
| 1192 | uint32_t total_size, boundary; |
| 1193 | uint32_t erase_size_low, size_low, erase_size_high, size_high; |
| 1194 | struct block_eraser *eraser; |
| 1195 | |
| 1196 | total_size = hwseq_data.size_comp0 + hwseq_data.size_comp1; |
| 1197 | msg_cdbg("Found %d attached SPI flash chip", |
| 1198 | (hwseq_data.size_comp1 != 0) ? 2 : 1); |
| 1199 | if (hwseq_data.size_comp1 != 0) |
| 1200 | msg_cdbg("s with a combined"); |
| 1201 | else |
| 1202 | msg_cdbg(" with a"); |
| 1203 | msg_cdbg(" density of %d kB.\n", total_size / 1024); |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1204 | flash->chip->total_size = total_size / 1024; |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1205 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1206 | eraser = &(flash->chip->block_erasers[0]); |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1207 | boundary = (REGREAD32(ICH9_REG_FPB) & FPB_FPBA) << 12; |
| 1208 | size_high = total_size - boundary; |
| 1209 | erase_size_high = ich_hwseq_get_erase_block_size(boundary); |
| 1210 | |
| 1211 | if (boundary == 0) { |
| 1212 | msg_cdbg("There is only one partition containing the whole " |
| 1213 | "address space (0x%06x - 0x%06x).\n", 0, size_high-1); |
| 1214 | eraser->eraseblocks[0].size = erase_size_high; |
| 1215 | eraser->eraseblocks[0].count = size_high / erase_size_high; |
| 1216 | msg_cdbg("There are %d erase blocks with %d B each.\n", |
| 1217 | size_high / erase_size_high, erase_size_high); |
| 1218 | } else { |
| 1219 | msg_cdbg("The flash address space (0x%06x - 0x%06x) is divided " |
| 1220 | "at address 0x%06x in two partitions.\n", |
Stefan Tauner | dbac46c | 2013-08-13 22:10:41 +0000 | [diff] [blame] | 1221 | 0, total_size-1, boundary); |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1222 | size_low = total_size - size_high; |
| 1223 | erase_size_low = ich_hwseq_get_erase_block_size(0); |
| 1224 | |
| 1225 | eraser->eraseblocks[0].size = erase_size_low; |
| 1226 | eraser->eraseblocks[0].count = size_low / erase_size_low; |
| 1227 | msg_cdbg("The first partition ranges from 0x%06x to 0x%06x.\n", |
| 1228 | 0, size_low-1); |
| 1229 | msg_cdbg("In that range are %d erase blocks with %d B each.\n", |
| 1230 | size_low / erase_size_low, erase_size_low); |
| 1231 | |
| 1232 | eraser->eraseblocks[1].size = erase_size_high; |
| 1233 | eraser->eraseblocks[1].count = size_high / erase_size_high; |
| 1234 | msg_cdbg("The second partition ranges from 0x%06x to 0x%06x.\n", |
Stefan Tauner | dbac46c | 2013-08-13 22:10:41 +0000 | [diff] [blame] | 1235 | boundary, total_size-1); |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1236 | msg_cdbg("In that range are %d erase blocks with %d B each.\n", |
| 1237 | size_high / erase_size_high, erase_size_high); |
| 1238 | } |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1239 | flash->chip->tested = TEST_OK_PREW; |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1240 | return 1; |
| 1241 | } |
| 1242 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 1243 | static int ich_hwseq_block_erase(struct flashctx *flash, unsigned int addr, |
| 1244 | unsigned int len) |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1245 | { |
| 1246 | uint32_t erase_block; |
| 1247 | uint16_t hsfc; |
| 1248 | uint32_t timeout = 5000 * 1000; /* 5 s for max 64 kB */ |
| 1249 | |
| 1250 | erase_block = ich_hwseq_get_erase_block_size(addr); |
| 1251 | if (len != erase_block) { |
| 1252 | msg_cerr("Erase block size for address 0x%06x is %d B, " |
| 1253 | "but requested erase block size is %d B. " |
| 1254 | "Not erasing anything.\n", addr, erase_block, len); |
| 1255 | return -1; |
| 1256 | } |
| 1257 | |
| 1258 | /* Although the hardware supports this (it would erase the whole block |
| 1259 | * containing the address) we play safe here. */ |
| 1260 | if (addr % erase_block != 0) { |
| 1261 | msg_cerr("Erase address 0x%06x is not aligned to the erase " |
| 1262 | "block boundary (any multiple of %d). " |
| 1263 | "Not erasing anything.\n", addr, erase_block); |
| 1264 | return -1; |
| 1265 | } |
| 1266 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1267 | if (addr + len > flash->chip->total_size * 1024) { |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1268 | msg_perr("Request to erase some inaccessible memory address(es)" |
| 1269 | " (addr=0x%x, len=%d). " |
| 1270 | "Not erasing anything.\n", addr, len); |
| 1271 | return -1; |
| 1272 | } |
| 1273 | |
| 1274 | msg_pdbg("Erasing %d bytes starting at 0x%06x.\n", len, addr); |
| 1275 | |
| 1276 | /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */ |
| 1277 | REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS)); |
| 1278 | |
| 1279 | hsfc = REGREAD16(ICH9_REG_HSFC); |
| 1280 | hsfc &= ~HSFC_FCYCLE; /* clear operation */ |
| 1281 | hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */ |
| 1282 | hsfc |= HSFC_FGO; /* start */ |
| 1283 | msg_pdbg("HSFC used for block erasing: "); |
| 1284 | prettyprint_ich9_reg_hsfc(hsfc); |
| 1285 | REGWRITE16(ICH9_REG_HSFC, hsfc); |
| 1286 | |
| 1287 | if (ich_hwseq_wait_for_cycle_complete(timeout, len)) |
| 1288 | return -1; |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 1292 | static int ich_hwseq_read(struct flashctx *flash, uint8_t *buf, |
| 1293 | unsigned int addr, unsigned int len) |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1294 | { |
| 1295 | uint16_t hsfc; |
| 1296 | uint16_t timeout = 100 * 60; |
| 1297 | uint8_t block_len; |
| 1298 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1299 | if (addr + len > flash->chip->total_size * 1024) { |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1300 | msg_perr("Request to read from an inaccessible memory address " |
| 1301 | "(addr=0x%x, len=%d).\n", addr, len); |
| 1302 | return -1; |
| 1303 | } |
| 1304 | |
| 1305 | msg_pdbg("Reading %d bytes starting at 0x%06x.\n", len, addr); |
| 1306 | /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */ |
| 1307 | REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS)); |
| 1308 | |
| 1309 | while (len > 0) { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 1310 | block_len = min(len, flash->pgm->opaque.max_data_read); |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1311 | ich_hwseq_set_addr(addr); |
| 1312 | hsfc = REGREAD16(ICH9_REG_HSFC); |
| 1313 | hsfc &= ~HSFC_FCYCLE; /* set read operation */ |
| 1314 | hsfc &= ~HSFC_FDBC; /* clear byte count */ |
| 1315 | /* set byte count */ |
| 1316 | hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC); |
| 1317 | hsfc |= HSFC_FGO; /* start */ |
| 1318 | REGWRITE16(ICH9_REG_HSFC, hsfc); |
| 1319 | |
| 1320 | if (ich_hwseq_wait_for_cycle_complete(timeout, block_len)) |
| 1321 | return 1; |
| 1322 | ich_read_data(buf, block_len, ICH9_REG_FDATA0); |
| 1323 | addr += block_len; |
| 1324 | buf += block_len; |
| 1325 | len -= block_len; |
| 1326 | } |
| 1327 | return 0; |
| 1328 | } |
| 1329 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 1330 | static int ich_hwseq_write(struct flashctx *flash, uint8_t *buf, |
| 1331 | unsigned int addr, unsigned int len) |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1332 | { |
| 1333 | uint16_t hsfc; |
| 1334 | uint16_t timeout = 100 * 60; |
| 1335 | uint8_t block_len; |
| 1336 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1337 | if (addr + len > flash->chip->total_size * 1024) { |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1338 | msg_perr("Request to write to an inaccessible memory address " |
| 1339 | "(addr=0x%x, len=%d).\n", addr, len); |
| 1340 | return -1; |
| 1341 | } |
| 1342 | |
| 1343 | msg_pdbg("Writing %d bytes starting at 0x%06x.\n", len, addr); |
| 1344 | /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */ |
| 1345 | REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS)); |
| 1346 | |
| 1347 | while (len > 0) { |
| 1348 | ich_hwseq_set_addr(addr); |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 1349 | block_len = min(len, flash->pgm->opaque.max_data_write); |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1350 | ich_fill_data(buf, block_len, ICH9_REG_FDATA0); |
| 1351 | hsfc = REGREAD16(ICH9_REG_HSFC); |
| 1352 | hsfc &= ~HSFC_FCYCLE; /* clear operation */ |
| 1353 | hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */ |
| 1354 | hsfc &= ~HSFC_FDBC; /* clear byte count */ |
| 1355 | /* set byte count */ |
| 1356 | hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC); |
| 1357 | hsfc |= HSFC_FGO; /* start */ |
| 1358 | REGWRITE16(ICH9_REG_HSFC, hsfc); |
| 1359 | |
| 1360 | if (ich_hwseq_wait_for_cycle_complete(timeout, block_len)) |
| 1361 | return -1; |
| 1362 | addr += block_len; |
| 1363 | buf += block_len; |
| 1364 | len -= block_len; |
| 1365 | } |
| 1366 | return 0; |
| 1367 | } |
Stefan Tauner | d0c5dc2 | 2011-10-20 12:57:14 +0000 | [diff] [blame] | 1368 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 1369 | static int ich_spi_send_multicommand(struct flashctx *flash, |
| 1370 | struct spi_command *cmds) |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 1371 | { |
| 1372 | int ret = 0; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1373 | int i; |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 1374 | int oppos, preoppos; |
| 1375 | for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) { |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 1376 | if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) { |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1377 | /* Next command is valid. */ |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 1378 | preoppos = find_preop(curopcodes, cmds->writearr[0]); |
| 1379 | oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]); |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1380 | if ((oppos == -1) && (preoppos != -1)) { |
| 1381 | /* Current command is listed as preopcode in |
| 1382 | * ICH struct OPCODES, but next command is not |
| 1383 | * listed as opcode in that struct. |
| 1384 | * Check for command sanity, then |
| 1385 | * try to reprogram the ICH opcode list. |
| 1386 | */ |
| 1387 | if (find_preop(curopcodes, |
| 1388 | (cmds + 1)->writearr[0]) != -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1389 | msg_perr("%s: Two subsequent " |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1390 | "preopcodes 0x%02x and 0x%02x, " |
| 1391 | "ignoring the first.\n", |
| 1392 | __func__, cmds->writearr[0], |
| 1393 | (cmds + 1)->writearr[0]); |
| 1394 | continue; |
| 1395 | } |
| 1396 | /* If the chipset is locked down, we'll fail |
| 1397 | * during execution of the next command anyway. |
| 1398 | * No need to bother with fixups. |
| 1399 | */ |
| 1400 | if (!ichspi_lock) { |
Helge Wagner | 738e252 | 2010-10-05 22:06:05 +0000 | [diff] [blame] | 1401 | oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt); |
| 1402 | if (oppos == -1) |
| 1403 | continue; |
| 1404 | curopcodes->opcode[oppos].atomic = preoppos + 1; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1405 | continue; |
| 1406 | } |
| 1407 | } |
| 1408 | if ((oppos != -1) && (preoppos != -1)) { |
| 1409 | /* Current command is listed as preopcode in |
| 1410 | * ICH struct OPCODES and next command is listed |
| 1411 | * as opcode in that struct. Match them up. |
| 1412 | */ |
| 1413 | curopcodes->opcode[oppos].atomic = preoppos + 1; |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 1414 | continue; |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1415 | } |
| 1416 | /* If none of the above if-statements about oppos or |
| 1417 | * preoppos matched, this is a normal opcode. |
| 1418 | */ |
| 1419 | } |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 1420 | ret = ich_spi_send_command(flash, cmds->writecnt, cmds->readcnt, |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 1421 | cmds->writearr, cmds->readarr); |
Carl-Daniel Hailfinger | f15e1ab | 2010-02-11 11:28:37 +0000 | [diff] [blame] | 1422 | /* Reset the type of all opcodes to non-atomic. */ |
| 1423 | for (i = 0; i < 8; i++) |
| 1424 | curopcodes->opcode[i].atomic = 0; |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 1425 | } |
| 1426 | return ret; |
| 1427 | } |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1428 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1429 | #define ICH_BMWAG(x) ((x >> 24) & 0xff) |
| 1430 | #define ICH_BMRAG(x) ((x >> 16) & 0xff) |
| 1431 | #define ICH_BRWA(x) ((x >> 8) & 0xff) |
| 1432 | #define ICH_BRRA(x) ((x >> 0) & 0xff) |
| 1433 | |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1434 | /* returns 0 if region is unused or r/w */ |
| 1435 | static int ich9_handle_frap(uint32_t frap, int i) |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1436 | { |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 1437 | static const char *const access_names[4] = { |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1438 | "locked", "read-only", "write-only", "read-write" |
| 1439 | }; |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 1440 | static const char *const region_names[5] = { |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1441 | "Flash Descriptor", "BIOS", "Management Engine", |
| 1442 | "Gigabit Ethernet", "Platform Data" |
| 1443 | }; |
| 1444 | uint32_t base, limit; |
| 1445 | int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) | |
| 1446 | (((ICH_BRRA(frap) >> i) & 1) << 0); |
Stefan Tauner | 29c8083 | 2011-06-12 08:14:10 +0000 | [diff] [blame] | 1447 | int offset = ICH9_REG_FREG0 + i * 4; |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1448 | uint32_t freg = mmio_readl(ich_spibar + offset); |
| 1449 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1450 | base = ICH_FREG_BASE(freg); |
| 1451 | limit = ICH_FREG_LIMIT(freg); |
Stefan Tauner | e3adea0 | 2012-08-27 15:12:36 +0000 | [diff] [blame] | 1452 | if (base > limit || (freg == 0 && i > 0)) { |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1453 | /* this FREG is disabled */ |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1454 | msg_pdbg2("0x%02X: 0x%08x FREG%i: %s region is unused.\n", |
| 1455 | offset, freg, i, region_names[i]); |
| 1456 | return 0; |
| 1457 | } |
| 1458 | msg_pdbg("0x%02X: 0x%08x ", offset, freg); |
| 1459 | if (rwperms == 0x3) { |
| 1460 | msg_pdbg("FREG%i: %s region (0x%08x-0x%08x) is %s.\n", i, |
| 1461 | region_names[i], base, (limit | 0x0fff), |
| 1462 | access_names[rwperms]); |
| 1463 | return 0; |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Stefan Tauner | c6fa32d | 2013-01-04 22:54:07 +0000 | [diff] [blame] | 1466 | msg_pwarn("FREG%i: Warning: %s region (0x%08x-0x%08x) is %s.\n", i, |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1467 | region_names[i], base, (limit | 0x0fff), |
| 1468 | access_names[rwperms]); |
| 1469 | return 1; |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Stefan Tauner | bf69aaa | 2011-09-17 21:21:48 +0000 | [diff] [blame] | 1472 | /* In contrast to FRAP and the master section of the descriptor the bits |
| 1473 | * in the PR registers have an inverted meaning. The bits in FRAP |
| 1474 | * indicate read and write access _grant_. Here they indicate read |
| 1475 | * and write _protection_ respectively. If both bits are 0 the address |
| 1476 | * bits are ignored. |
| 1477 | */ |
| 1478 | #define ICH_PR_PERMS(pr) (((~((pr) >> PR_RP_OFF) & 1) << 0) | \ |
| 1479 | ((~((pr) >> PR_WP_OFF) & 1) << 1)) |
| 1480 | |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1481 | /* returns 0 if range is unused (i.e. r/w) */ |
| 1482 | static int ich9_handle_pr(int i) |
Stefan Tauner | bf69aaa | 2011-09-17 21:21:48 +0000 | [diff] [blame] | 1483 | { |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1484 | static const char *const access_names[3] = { |
| 1485 | "locked", "read-only", "write-only" |
Stefan Tauner | bf69aaa | 2011-09-17 21:21:48 +0000 | [diff] [blame] | 1486 | }; |
| 1487 | uint8_t off = ICH9_REG_PR0 + (i * 4); |
| 1488 | uint32_t pr = mmio_readl(ich_spibar + off); |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1489 | unsigned int rwperms = ICH_PR_PERMS(pr); |
Stefan Tauner | bf69aaa | 2011-09-17 21:21:48 +0000 | [diff] [blame] | 1490 | |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1491 | if (rwperms == 0x3) { |
| 1492 | msg_pdbg2("0x%02X: 0x%08x (PR%u is unused)\n", off, pr, i); |
| 1493 | return 0; |
| 1494 | } |
| 1495 | |
| 1496 | msg_pdbg("0x%02X: 0x%08x ", off, pr); |
Stefan Tauner | c6fa32d | 2013-01-04 22:54:07 +0000 | [diff] [blame] | 1497 | msg_pwarn("PR%u: Warning: 0x%08x-0x%08x is %s.\n", i, ICH_FREG_BASE(pr), |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1498 | ICH_FREG_LIMIT(pr) | 0x0fff, access_names[rwperms]); |
| 1499 | return 1; |
Stefan Tauner | bf69aaa | 2011-09-17 21:21:48 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Stefan Tauner | 75da80c | 2011-09-17 22:21:55 +0000 | [diff] [blame] | 1502 | /* Set/Clear the read and write protection enable bits of PR register @i |
| 1503 | * according to @read_prot and @write_prot. */ |
| 1504 | static void ich9_set_pr(int i, int read_prot, int write_prot) |
| 1505 | { |
| 1506 | void *addr = ich_spibar + ICH9_REG_PR0 + (i * 4); |
| 1507 | uint32_t old = mmio_readl(addr); |
| 1508 | uint32_t new; |
| 1509 | |
| 1510 | msg_gspew("PR%u is 0x%08x", i, old); |
| 1511 | new = old & ~((1 << PR_RP_OFF) | (1 << PR_WP_OFF)); |
| 1512 | if (read_prot) |
| 1513 | new |= (1 << PR_RP_OFF); |
| 1514 | if (write_prot) |
| 1515 | new |= (1 << PR_WP_OFF); |
| 1516 | if (old == new) { |
| 1517 | msg_gspew(" already.\n"); |
| 1518 | return; |
| 1519 | } |
| 1520 | msg_gspew(", trying to set it to 0x%08x ", new); |
| 1521 | rmmio_writel(new, addr); |
| 1522 | msg_gspew("resulted in 0x%08x.\n", mmio_readl(addr)); |
| 1523 | } |
| 1524 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 1525 | static const struct spi_programmer spi_programmer_ich7 = { |
| 1526 | .type = SPI_CONTROLLER_ICH7, |
| 1527 | .max_data_read = 64, |
| 1528 | .max_data_write = 64, |
| 1529 | .command = ich_spi_send_command, |
| 1530 | .multicommand = ich_spi_send_multicommand, |
| 1531 | .read = default_spi_read, |
| 1532 | .write_256 = default_spi_write_256, |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 1533 | .write_aai = default_spi_write_aai, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 1534 | }; |
| 1535 | |
| 1536 | static const struct spi_programmer spi_programmer_ich9 = { |
| 1537 | .type = SPI_CONTROLLER_ICH9, |
| 1538 | .max_data_read = 64, |
| 1539 | .max_data_write = 64, |
| 1540 | .command = ich_spi_send_command, |
| 1541 | .multicommand = ich_spi_send_multicommand, |
| 1542 | .read = default_spi_read, |
| 1543 | .write_256 = default_spi_write_256, |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 1544 | .write_aai = default_spi_write_aai, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 1545 | }; |
| 1546 | |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1547 | static const struct opaque_programmer opaque_programmer_ich_hwseq = { |
| 1548 | .max_data_read = 64, |
| 1549 | .max_data_write = 64, |
| 1550 | .probe = ich_hwseq_probe, |
| 1551 | .read = ich_hwseq_read, |
| 1552 | .write = ich_hwseq_write, |
| 1553 | .erase = ich_hwseq_block_erase, |
| 1554 | }; |
| 1555 | |
Stefan Tauner | 92d6a86 | 2013-10-25 00:33:37 +0000 | [diff] [blame^] | 1556 | int ich_init_spi(struct pci_dev *dev, void *spibar, enum ich_chipset ich_gen) |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1557 | { |
| 1558 | int i; |
Stefan Tauner | 92d6a86 | 2013-10-25 00:33:37 +0000 | [diff] [blame^] | 1559 | uint16_t tmp2; |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1560 | uint32_t tmp; |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1561 | char *arg; |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1562 | int ich_spi_force = 0; |
| 1563 | int ich_spi_rw_restricted = 0; |
Stefan Tauner | d0c5dc2 | 2011-10-20 12:57:14 +0000 | [diff] [blame] | 1564 | int desc_valid = 0; |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1565 | struct ich_descriptors desc = {{ 0 }}; |
| 1566 | enum ich_spi_mode { |
| 1567 | ich_auto, |
| 1568 | ich_hwseq, |
| 1569 | ich_swseq |
| 1570 | } ich_spi_mode = ich_auto; |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1571 | |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 1572 | ich_generation = ich_gen; |
Stefan Tauner | 92d6a86 | 2013-10-25 00:33:37 +0000 | [diff] [blame^] | 1573 | ich_spibar = spibar; |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1574 | |
Stefan Tauner | d0c5dc2 | 2011-10-20 12:57:14 +0000 | [diff] [blame] | 1575 | switch (ich_generation) { |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 1576 | case CHIPSET_ICH7: |
Stefan Tauner | 92d6a86 | 2013-10-25 00:33:37 +0000 | [diff] [blame^] | 1577 | case CHIPSET_TUNNEL_CREEK: |
| 1578 | case CHIPSET_CENTERTON: |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1579 | msg_pdbg("0x00: 0x%04x (SPIS)\n", |
| 1580 | mmio_readw(ich_spibar + 0)); |
| 1581 | msg_pdbg("0x02: 0x%04x (SPIC)\n", |
| 1582 | mmio_readw(ich_spibar + 2)); |
| 1583 | msg_pdbg("0x04: 0x%08x (SPIA)\n", |
| 1584 | mmio_readl(ich_spibar + 4)); |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1585 | ichspi_bbar = mmio_readl(ich_spibar + 0x50); |
| 1586 | msg_pdbg("0x50: 0x%08x (BBAR)\n", |
| 1587 | ichspi_bbar); |
| 1588 | msg_pdbg("0x54: 0x%04x (PREOP)\n", |
| 1589 | mmio_readw(ich_spibar + 0x54)); |
| 1590 | msg_pdbg("0x56: 0x%04x (OPTYPE)\n", |
| 1591 | mmio_readw(ich_spibar + 0x56)); |
| 1592 | msg_pdbg("0x58: 0x%08x (OPMENU)\n", |
| 1593 | mmio_readl(ich_spibar + 0x58)); |
| 1594 | msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", |
| 1595 | mmio_readl(ich_spibar + 0x5c)); |
Stefan Tauner | 122dd12 | 2011-07-24 15:34:56 +0000 | [diff] [blame] | 1596 | for (i = 0; i < 3; i++) { |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1597 | int offs; |
| 1598 | offs = 0x60 + (i * 4); |
| 1599 | msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs, |
| 1600 | mmio_readl(ich_spibar + offs), i); |
| 1601 | } |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1602 | if (mmio_readw(ich_spibar) & (1 << 15)) { |
Stefan Tauner | c6fa32d | 2013-01-04 22:54:07 +0000 | [diff] [blame] | 1603 | msg_pwarn("WARNING: SPI Configuration Lockdown activated.\n"); |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1604 | ichspi_lock = 1; |
| 1605 | } |
Stefan Tauner | 745f6bb | 2011-11-13 15:17:10 +0000 | [diff] [blame] | 1606 | ich_init_opcodes(); |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 1607 | ich_set_bbar(0); |
Stefan Tauner | d0c5dc2 | 2011-10-20 12:57:14 +0000 | [diff] [blame] | 1608 | register_spi_programmer(&spi_programmer_ich7); |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1609 | break; |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 1610 | case CHIPSET_ICH8: |
Stefan Tauner | d0c5dc2 | 2011-10-20 12:57:14 +0000 | [diff] [blame] | 1611 | default: /* Future version might behave the same */ |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1612 | arg = extract_programmer_param("ich_spi_mode"); |
| 1613 | if (arg && !strcmp(arg, "hwseq")) { |
| 1614 | ich_spi_mode = ich_hwseq; |
| 1615 | msg_pspew("user selected hwseq\n"); |
| 1616 | } else if (arg && !strcmp(arg, "swseq")) { |
| 1617 | ich_spi_mode = ich_swseq; |
| 1618 | msg_pspew("user selected swseq\n"); |
| 1619 | } else if (arg && !strcmp(arg, "auto")) { |
| 1620 | msg_pspew("user selected auto\n"); |
| 1621 | ich_spi_mode = ich_auto; |
| 1622 | } else if (arg && !strlen(arg)) { |
| 1623 | msg_perr("Missing argument for ich_spi_mode.\n"); |
| 1624 | free(arg); |
| 1625 | return ERROR_FATAL; |
| 1626 | } else if (arg) { |
| 1627 | msg_perr("Unknown argument for ich_spi_mode: %s\n", |
| 1628 | arg); |
| 1629 | free(arg); |
| 1630 | return ERROR_FATAL; |
| 1631 | } |
| 1632 | free(arg); |
| 1633 | |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1634 | arg = extract_programmer_param("ich_spi_force"); |
| 1635 | if (arg && !strcmp(arg, "yes")) { |
| 1636 | ich_spi_force = 1; |
| 1637 | msg_pspew("ich_spi_force enabled.\n"); |
| 1638 | } else if (arg && !strlen(arg)) { |
| 1639 | msg_perr("Missing argument for ich_spi_force.\n"); |
| 1640 | free(arg); |
| 1641 | return ERROR_FATAL; |
| 1642 | } else if (arg) { |
| 1643 | msg_perr("Unknown argument for ich_spi_force: \"%s\" " |
| 1644 | "(not \"yes\").\n", arg); |
| 1645 | free(arg); |
| 1646 | return ERROR_FATAL; |
| 1647 | } |
| 1648 | free(arg); |
| 1649 | |
Stefan Tauner | 29c8083 | 2011-06-12 08:14:10 +0000 | [diff] [blame] | 1650 | tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFS); |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1651 | msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2); |
Stefan Tauner | 5520694 | 2011-06-11 09:53:22 +0000 | [diff] [blame] | 1652 | prettyprint_ich9_reg_hsfs(tmp2); |
Stefan Tauner | 29c8083 | 2011-06-12 08:14:10 +0000 | [diff] [blame] | 1653 | if (tmp2 & HSFS_FLOCKDN) { |
Stefan Tauner | c6fa32d | 2013-01-04 22:54:07 +0000 | [diff] [blame] | 1654 | msg_pwarn("Warning: SPI Configuration Lockdown activated.\n"); |
Stefan Tauner | 5520694 | 2011-06-11 09:53:22 +0000 | [diff] [blame] | 1655 | ichspi_lock = 1; |
| 1656 | } |
Stefan Tauner | 1e14639 | 2011-09-15 23:52:55 +0000 | [diff] [blame] | 1657 | if (tmp2 & HSFS_FDV) |
Stefan Tauner | d0c5dc2 | 2011-10-20 12:57:14 +0000 | [diff] [blame] | 1658 | desc_valid = 1; |
| 1659 | if (!(tmp2 & HSFS_FDOPSS) && desc_valid) |
Stefan Tauner | d7d423b | 2012-10-20 09:13:16 +0000 | [diff] [blame] | 1660 | msg_pinfo("The Flash Descriptor Override Strap-Pin is set. Restrictions implied by\n" |
| 1661 | "the Master Section of the flash descriptor are NOT in effect. Please note\n" |
| 1662 | "that Protected Range (PR) restrictions still apply.\n"); |
Stefan Tauner | 745f6bb | 2011-11-13 15:17:10 +0000 | [diff] [blame] | 1663 | ich_init_opcodes(); |
Stefan Tauner | 5520694 | 2011-06-11 09:53:22 +0000 | [diff] [blame] | 1664 | |
Stefan Tauner | f382e35 | 2011-11-08 11:55:24 +0000 | [diff] [blame] | 1665 | if (desc_valid) { |
| 1666 | tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFC); |
| 1667 | msg_pdbg("0x06: 0x%04x (HSFC)\n", tmp2); |
| 1668 | prettyprint_ich9_reg_hsfc(tmp2); |
| 1669 | } |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1670 | |
Stefan Tauner | 5ffe65b | 2011-07-07 04:10:57 +0000 | [diff] [blame] | 1671 | tmp = mmio_readl(ich_spibar + ICH9_REG_FADDR); |
Stefan Tauner | eb58257 | 2012-09-21 12:52:50 +0000 | [diff] [blame] | 1672 | msg_pdbg2("0x08: 0x%08x (FADDR)\n", tmp); |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1673 | |
Stefan Tauner | f382e35 | 2011-11-08 11:55:24 +0000 | [diff] [blame] | 1674 | if (desc_valid) { |
| 1675 | tmp = mmio_readl(ich_spibar + ICH9_REG_FRAP); |
| 1676 | msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp); |
| 1677 | msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp)); |
| 1678 | msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp)); |
| 1679 | msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp)); |
| 1680 | msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp)); |
| 1681 | |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1682 | /* Handle FREGx and FRAP registers */ |
Stefan Tauner | f382e35 | 2011-11-08 11:55:24 +0000 | [diff] [blame] | 1683 | for (i = 0; i < 5; i++) |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1684 | ich_spi_rw_restricted |= ich9_handle_frap(tmp, i); |
Stefan Tauner | 27cb34b | 2013-06-01 00:06:12 +0000 | [diff] [blame] | 1685 | if (ich_spi_rw_restricted) |
| 1686 | msg_pwarn("Not all flash regions are freely accessible by flashrom. This is " |
| 1687 | "most likely\ndue to an active ME. Please see http://flashrom.org/ME " |
| 1688 | "for details.\n"); |
Stefan Tauner | f382e35 | 2011-11-08 11:55:24 +0000 | [diff] [blame] | 1689 | } |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1690 | |
Stefan Tauner | eb58257 | 2012-09-21 12:52:50 +0000 | [diff] [blame] | 1691 | /* Handle PR registers */ |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1692 | for (i = 0; i < 5; i++) { |
| 1693 | /* if not locked down try to disable PR locks first */ |
| 1694 | if (!ichspi_lock) |
Stefan Tauner | 75da80c | 2011-09-17 22:21:55 +0000 | [diff] [blame] | 1695 | ich9_set_pr(i, 0, 0); |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1696 | ich_spi_rw_restricted |= ich9_handle_pr(i); |
| 1697 | } |
| 1698 | |
| 1699 | if (ich_spi_rw_restricted) { |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1700 | if (!ich_spi_force) |
| 1701 | programmer_may_write = 0; |
Stefan Tauner | d7d423b | 2012-10-20 09:13:16 +0000 | [diff] [blame] | 1702 | msg_pinfo("Writes have been disabled for safety reasons. You can enforce write\n" |
| 1703 | "support with the ich_spi_force programmer option, but you will most likely\n" |
| 1704 | "harm your hardware! If you force flashrom you will get no support if\n" |
| 1705 | "something breaks. On a few mainboards it is possible to enable write\n" |
| 1706 | "access by setting a jumper (see its documentation or the board itself).\n"); |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1707 | if (ich_spi_force) |
Stefan Tauner | d7d423b | 2012-10-20 09:13:16 +0000 | [diff] [blame] | 1708 | msg_pinfo("Continuing with write support because the user forced us to!\n"); |
Stefan Tauner | 5210e72 | 2012-02-16 01:13:00 +0000 | [diff] [blame] | 1709 | } |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 1710 | |
Stefan Tauner | 29c8083 | 2011-06-12 08:14:10 +0000 | [diff] [blame] | 1711 | tmp = mmio_readl(ich_spibar + ICH9_REG_SSFS); |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 1712 | msg_pdbg("0x90: 0x%02x (SSFS)\n", tmp & 0xff); |
Stefan Tauner | 2a8b262 | 2011-06-11 09:53:16 +0000 | [diff] [blame] | 1713 | prettyprint_ich9_reg_ssfs(tmp); |
Stefan Tauner | 29c8083 | 2011-06-12 08:14:10 +0000 | [diff] [blame] | 1714 | if (tmp & SSFS_FCERR) { |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 1715 | msg_pdbg("Clearing SSFS.FCERR\n"); |
Stefan Tauner | 29c8083 | 2011-06-12 08:14:10 +0000 | [diff] [blame] | 1716 | mmio_writeb(SSFS_FCERR, ich_spibar + ICH9_REG_SSFS); |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 1717 | } |
Stefan Tauner | 2a8b262 | 2011-06-11 09:53:16 +0000 | [diff] [blame] | 1718 | msg_pdbg("0x91: 0x%06x (SSFC)\n", tmp >> 8); |
| 1719 | prettyprint_ich9_reg_ssfc(tmp); |
Carl-Daniel Hailfinger | eacbd16 | 2011-03-17 00:10:25 +0000 | [diff] [blame] | 1720 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1721 | msg_pdbg("0x94: 0x%04x (PREOP)\n", |
Stefan Tauner | 29c8083 | 2011-06-12 08:14:10 +0000 | [diff] [blame] | 1722 | mmio_readw(ich_spibar + ICH9_REG_PREOP)); |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1723 | msg_pdbg("0x96: 0x%04x (OPTYPE)\n", |
Stefan Tauner | 29c8083 | 2011-06-12 08:14:10 +0000 | [diff] [blame] | 1724 | mmio_readw(ich_spibar + ICH9_REG_OPTYPE)); |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1725 | msg_pdbg("0x98: 0x%08x (OPMENU)\n", |
Stefan Tauner | 29c8083 | 2011-06-12 08:14:10 +0000 | [diff] [blame] | 1726 | mmio_readl(ich_spibar + ICH9_REG_OPMENU)); |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1727 | msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n", |
Stefan Tauner | 29c8083 | 2011-06-12 08:14:10 +0000 | [diff] [blame] | 1728 | mmio_readl(ich_spibar + ICH9_REG_OPMENU + 4)); |
Stefan Tauner | f382e35 | 2011-11-08 11:55:24 +0000 | [diff] [blame] | 1729 | if (ich_generation == CHIPSET_ICH8 && desc_valid) { |
Stefan Tauner | 1e14639 | 2011-09-15 23:52:55 +0000 | [diff] [blame] | 1730 | tmp = mmio_readl(ich_spibar + ICH8_REG_VSCC); |
| 1731 | msg_pdbg("0xC1: 0x%08x (VSCC)\n", tmp); |
| 1732 | msg_pdbg("VSCC: "); |
| 1733 | prettyprint_ich_reg_vscc(tmp, MSG_DEBUG); |
| 1734 | } else { |
| 1735 | ichspi_bbar = mmio_readl(ich_spibar + ICH9_REG_BBAR); |
| 1736 | msg_pdbg("0xA0: 0x%08x (BBAR)\n", |
| 1737 | ichspi_bbar); |
Stefan Tauner | bd649e4 | 2011-07-01 00:39:16 +0000 | [diff] [blame] | 1738 | |
Stefan Tauner | f382e35 | 2011-11-08 11:55:24 +0000 | [diff] [blame] | 1739 | if (desc_valid) { |
| 1740 | tmp = mmio_readl(ich_spibar + ICH9_REG_LVSCC); |
| 1741 | msg_pdbg("0xC4: 0x%08x (LVSCC)\n", tmp); |
| 1742 | msg_pdbg("LVSCC: "); |
| 1743 | prettyprint_ich_reg_vscc(tmp, MSG_DEBUG); |
Stefan Tauner | 1e14639 | 2011-09-15 23:52:55 +0000 | [diff] [blame] | 1744 | |
Stefan Tauner | f382e35 | 2011-11-08 11:55:24 +0000 | [diff] [blame] | 1745 | tmp = mmio_readl(ich_spibar + ICH9_REG_UVSCC); |
| 1746 | msg_pdbg("0xC8: 0x%08x (UVSCC)\n", tmp); |
| 1747 | msg_pdbg("UVSCC: "); |
| 1748 | prettyprint_ich_reg_vscc(tmp, MSG_DEBUG); |
Stefan Tauner | 1e14639 | 2011-09-15 23:52:55 +0000 | [diff] [blame] | 1749 | |
Stefan Tauner | f382e35 | 2011-11-08 11:55:24 +0000 | [diff] [blame] | 1750 | tmp = mmio_readl(ich_spibar + ICH9_REG_FPB); |
| 1751 | msg_pdbg("0xD0: 0x%08x (FPB)\n", tmp); |
| 1752 | } |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 1753 | ich_set_bbar(0); |
Stefan Tauner | 1e14639 | 2011-09-15 23:52:55 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | msg_pdbg("\n"); |
Stefan Tauner | d0c5dc2 | 2011-10-20 12:57:14 +0000 | [diff] [blame] | 1757 | if (desc_valid) { |
Stefan Tauner | 1e14639 | 2011-09-15 23:52:55 +0000 | [diff] [blame] | 1758 | if (read_ich_descriptors_via_fdo(ich_spibar, &desc) == |
| 1759 | ICH_RET_OK) |
| 1760 | prettyprint_ich_descriptors(CHIPSET_ICH_UNKNOWN, |
| 1761 | &desc); |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1762 | /* If the descriptor is valid and indicates multiple |
| 1763 | * flash devices we need to use hwseq to be able to |
| 1764 | * access the second flash device. |
| 1765 | */ |
| 1766 | if (ich_spi_mode == ich_auto && desc.content.NC != 0) { |
| 1767 | msg_pinfo("Enabling hardware sequencing due to " |
| 1768 | "multiple flash chips detected.\n"); |
| 1769 | ich_spi_mode = ich_hwseq; |
| 1770 | } |
Stefan Tauner | 1e14639 | 2011-09-15 23:52:55 +0000 | [diff] [blame] | 1771 | } |
Stefan Tauner | 50e7c60 | 2011-11-08 10:55:54 +0000 | [diff] [blame] | 1772 | |
| 1773 | if (ich_spi_mode == ich_auto && ichspi_lock && |
| 1774 | ich_missing_opcodes()) { |
| 1775 | msg_pinfo("Enabling hardware sequencing because " |
| 1776 | "some important opcode is locked.\n"); |
| 1777 | ich_spi_mode = ich_hwseq; |
| 1778 | } |
| 1779 | |
| 1780 | if (ich_spi_mode == ich_hwseq) { |
| 1781 | if (!desc_valid) { |
| 1782 | msg_perr("Hardware sequencing was requested " |
| 1783 | "but the flash descriptor is not " |
| 1784 | "valid. Aborting.\n"); |
| 1785 | return ERROR_FATAL; |
| 1786 | } |
| 1787 | hwseq_data.size_comp0 = getFCBA_component_density(&desc, 0); |
| 1788 | hwseq_data.size_comp1 = getFCBA_component_density(&desc, 1); |
| 1789 | register_opaque_programmer(&opaque_programmer_ich_hwseq); |
| 1790 | } else { |
| 1791 | register_spi_programmer(&spi_programmer_ich9); |
| 1792 | } |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1793 | break; |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1796 | return 0; |
| 1797 | } |
| 1798 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 1799 | static const struct spi_programmer spi_programmer_via = { |
| 1800 | .type = SPI_CONTROLLER_VIA, |
| 1801 | .max_data_read = 16, |
| 1802 | .max_data_write = 16, |
| 1803 | .command = ich_spi_send_command, |
| 1804 | .multicommand = ich_spi_send_multicommand, |
| 1805 | .read = default_spi_read, |
| 1806 | .write_256 = default_spi_write_256, |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 1807 | .write_aai = default_spi_write_aai, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 1808 | }; |
| 1809 | |
Helge Wagner | dd73d83 | 2012-08-24 23:03:46 +0000 | [diff] [blame] | 1810 | int via_init_spi(struct pci_dev *dev, uint32_t mmio_base) |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1811 | { |
Carl-Daniel Hailfinger | 841d631 | 2010-11-24 23:37:22 +0000 | [diff] [blame] | 1812 | int i; |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1813 | |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 1814 | ich_spibar = rphysmap("VIA SPI MMIO registers", mmio_base, 0x70); |
| 1815 | if (ich_spibar == ERROR_PTR) |
| 1816 | return ERROR_FATAL; |
Helge Wagner | dd73d83 | 2012-08-24 23:03:46 +0000 | [diff] [blame] | 1817 | /* Do we really need no write enable? Like the LPC one at D17F0 0x40 */ |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1818 | |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1819 | /* Not sure if it speaks all these bus protocols. */ |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 1820 | internal_buses_supported = BUS_LPC | BUS_FWH; |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 1821 | ich_generation = CHIPSET_ICH7; |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 1822 | register_spi_programmer(&spi_programmer_via); |
Carl-Daniel Hailfinger | 841d631 | 2010-11-24 23:37:22 +0000 | [diff] [blame] | 1823 | |
| 1824 | msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0)); |
| 1825 | msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2)); |
| 1826 | msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4)); |
| 1827 | for (i = 0; i < 2; i++) { |
| 1828 | int offs; |
| 1829 | offs = 8 + (i * 8); |
| 1830 | msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs, |
| 1831 | mmio_readl(ich_spibar + offs), i); |
| 1832 | msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4, |
| 1833 | mmio_readl(ich_spibar + offs + 4), i); |
| 1834 | } |
| 1835 | ichspi_bbar = mmio_readl(ich_spibar + 0x50); |
| 1836 | msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar); |
| 1837 | msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54)); |
| 1838 | msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56)); |
| 1839 | msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58)); |
| 1840 | msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c)); |
| 1841 | for (i = 0; i < 3; i++) { |
| 1842 | int offs; |
| 1843 | offs = 0x60 + (i * 4); |
| 1844 | msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs, |
| 1845 | mmio_readl(ich_spibar + offs), i); |
| 1846 | } |
| 1847 | msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n", |
| 1848 | mmio_readw(ich_spibar + 0x6c)); |
| 1849 | if (mmio_readw(ich_spibar) & (1 << 15)) { |
Stefan Tauner | c6fa32d | 2013-01-04 22:54:07 +0000 | [diff] [blame] | 1850 | msg_pwarn("Warning: SPI Configuration Lockdown activated.\n"); |
Carl-Daniel Hailfinger | 841d631 | 2010-11-24 23:37:22 +0000 | [diff] [blame] | 1851 | ichspi_lock = 1; |
| 1852 | } |
| 1853 | |
Stefan Tauner | a8d838d | 2011-11-06 23:51:09 +0000 | [diff] [blame] | 1854 | ich_set_bbar(0); |
Michael Karcher | a4448d9 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 1855 | ich_init_opcodes(); |
| 1856 | |
| 1857 | return 0; |
| 1858 | } |
| 1859 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1860 | #endif |