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