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