Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 2 | |
Arthur Heymans | 026863b | 2019-11-21 08:24:02 +0100 | [diff] [blame] | 3 | #define __SIMPLE_DEVICE__ |
| 4 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 5 | /* This file is derived from the flashrom project. */ |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 6 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 7 | #include <string.h> |
David Hendricks | f2612a1 | 2014-04-13 16:27:02 -0700 | [diff] [blame] | 8 | #include <bootstate.h> |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 9 | #include <commonlib/helpers.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 10 | #include <delay.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 11 | #include <device/mmio.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 12 | #include <device/pci_ops.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 13 | #include <console/console.h> |
Kyösti Mälkki | 7ba1440 | 2019-02-07 12:44:00 +0200 | [diff] [blame] | 14 | #include <device/device.h> |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 15 | #include <device/pci.h> |
| 16 | #include <spi_flash.h> |
Zheng Bao | 600784e | 2013-02-07 17:30:23 +0800 | [diff] [blame] | 17 | #include <spi-generic.h> |
Aaron Durbin | 4ed8e9c | 2019-12-27 14:30:51 -0700 | [diff] [blame] | 18 | #include <timer.h> |
Elyes HAOUAS | 608a75c | 2021-02-12 08:09:58 +0100 | [diff] [blame] | 19 | #include <types.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 20 | |
Arthur Heymans | 92185e3 | 2019-05-28 13:06:34 +0200 | [diff] [blame] | 21 | #include "spi.h" |
| 22 | |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 23 | #define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */ |
| 24 | #define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF) |
| 25 | #define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */ |
| 26 | #define HSFC_FDBC (0x3f << HSFC_FDBC_OFF) |
| 27 | |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 28 | static int spi_is_multichip(void); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 29 | |
Angel Pons | d21b463 | 2021-02-10 17:12:05 +0100 | [diff] [blame] | 30 | static void spi_set_smm_only_flashing(bool enable); |
| 31 | |
Arthur Heymans | a9c1a5f | 2019-06-15 18:21:58 +0200 | [diff] [blame] | 32 | struct ich7_spi_regs { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 33 | uint16_t spis; |
| 34 | uint16_t spic; |
| 35 | uint32_t spia; |
| 36 | uint64_t spid[8]; |
| 37 | uint64_t _pad; |
| 38 | uint32_t bbar; |
| 39 | uint16_t preop; |
| 40 | uint16_t optype; |
| 41 | uint8_t opmenu[8]; |
Arthur Heymans | 11fcb2bc | 2018-01-07 20:46:31 +0100 | [diff] [blame] | 42 | uint32_t pbr[3]; |
Arthur Heymans | a9c1a5f | 2019-06-15 18:21:58 +0200 | [diff] [blame] | 43 | } __packed; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 44 | |
Arthur Heymans | a9c1a5f | 2019-06-15 18:21:58 +0200 | [diff] [blame] | 45 | struct ich9_spi_regs { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 46 | uint32_t bfpr; |
| 47 | uint16_t hsfs; |
| 48 | uint16_t hsfc; |
| 49 | uint32_t faddr; |
| 50 | uint32_t _reserved0; |
| 51 | uint32_t fdata[16]; |
| 52 | uint32_t frap; |
| 53 | uint32_t freg[5]; |
| 54 | uint32_t _reserved1[3]; |
| 55 | uint32_t pr[5]; |
| 56 | uint32_t _reserved2[2]; |
| 57 | uint8_t ssfs; |
| 58 | uint8_t ssfc[3]; |
| 59 | uint16_t preop; |
| 60 | uint16_t optype; |
| 61 | uint8_t opmenu[8]; |
| 62 | uint32_t bbar; |
| 63 | uint8_t _reserved3[12]; |
| 64 | uint32_t fdoc; |
| 65 | uint32_t fdod; |
| 66 | uint8_t _reserved4[8]; |
| 67 | uint32_t afc; |
| 68 | uint32_t lvscc; |
| 69 | uint32_t uvscc; |
| 70 | uint8_t _reserved5[4]; |
| 71 | uint32_t fpb; |
| 72 | uint8_t _reserved6[28]; |
| 73 | uint32_t srdl; |
| 74 | uint32_t srdc; |
| 75 | uint32_t srd; |
Arthur Heymans | a9c1a5f | 2019-06-15 18:21:58 +0200 | [diff] [blame] | 76 | } __packed; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 77 | |
Arthur Heymans | a9c1a5f | 2019-06-15 18:21:58 +0200 | [diff] [blame] | 78 | struct ich_spi_controller { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 79 | int locked; |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 80 | uint32_t flmap0; |
Stefan Tauner | 327205d | 2018-08-26 13:53:16 +0200 | [diff] [blame] | 81 | uint32_t flcomp; |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 82 | uint32_t hsfs; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 83 | |
Arthur Heymans | 21c5d43 | 2019-06-15 18:23:29 +0200 | [diff] [blame] | 84 | union { |
| 85 | struct ich9_spi_regs *ich9_spi; |
| 86 | struct ich7_spi_regs *ich7_spi; |
| 87 | }; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 88 | uint8_t *opmenu; |
| 89 | int menubytes; |
| 90 | uint16_t *preop; |
| 91 | uint16_t *optype; |
| 92 | uint32_t *addr; |
| 93 | uint8_t *data; |
Martin Roth | ff744bf | 2019-10-23 21:46:03 -0600 | [diff] [blame] | 94 | unsigned int databytes; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 95 | uint8_t *status; |
| 96 | uint16_t *control; |
| 97 | uint32_t *bbar; |
Arthur Heymans | 11fcb2bc | 2018-01-07 20:46:31 +0100 | [diff] [blame] | 98 | uint32_t *fpr; |
| 99 | uint8_t fpr_max; |
Arthur Heymans | a9c1a5f | 2019-06-15 18:21:58 +0200 | [diff] [blame] | 100 | }; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 101 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 102 | static struct ich_spi_controller cntlr; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 103 | |
| 104 | enum { |
| 105 | SPIS_SCIP = 0x0001, |
| 106 | SPIS_GRANT = 0x0002, |
| 107 | SPIS_CDS = 0x0004, |
| 108 | SPIS_FCERR = 0x0008, |
| 109 | SSFS_AEL = 0x0010, |
| 110 | SPIS_LOCK = 0x8000, |
| 111 | SPIS_RESERVED_MASK = 0x7ff0, |
| 112 | SSFS_RESERVED_MASK = 0x7fe2 |
| 113 | }; |
| 114 | |
| 115 | enum { |
| 116 | SPIC_SCGO = 0x000002, |
| 117 | SPIC_ACS = 0x000004, |
| 118 | SPIC_SPOP = 0x000008, |
| 119 | SPIC_DBC = 0x003f00, |
| 120 | SPIC_DS = 0x004000, |
| 121 | SPIC_SME = 0x008000, |
| 122 | SSFC_SCF_MASK = 0x070000, |
| 123 | SSFC_RESERVED = 0xf80000 |
| 124 | }; |
| 125 | |
| 126 | enum { |
| 127 | HSFS_FDONE = 0x0001, |
| 128 | HSFS_FCERR = 0x0002, |
| 129 | HSFS_AEL = 0x0004, |
| 130 | HSFS_BERASE_MASK = 0x0018, |
| 131 | HSFS_BERASE_SHIFT = 3, |
| 132 | HSFS_SCIP = 0x0020, |
| 133 | HSFS_FDOPSS = 0x2000, |
| 134 | HSFS_FDV = 0x4000, |
| 135 | HSFS_FLOCKDN = 0x8000 |
| 136 | }; |
| 137 | |
| 138 | enum { |
| 139 | HSFC_FGO = 0x0001, |
| 140 | HSFC_FCYCLE_MASK = 0x0006, |
| 141 | HSFC_FCYCLE_SHIFT = 1, |
| 142 | HSFC_FDBC_MASK = 0x3f00, |
| 143 | HSFC_FDBC_SHIFT = 8, |
| 144 | HSFC_FSMIE = 0x8000 |
| 145 | }; |
| 146 | |
| 147 | enum { |
| 148 | SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0, |
| 149 | SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1, |
| 150 | SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2, |
| 151 | SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3 |
| 152 | }; |
| 153 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 154 | #if CONFIG(DEBUG_SPI_FLASH) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 155 | |
| 156 | static u8 readb_(const void *addr) |
| 157 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 158 | u8 v = read8(addr); |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 159 | |
Arthur Heymans | d7fc068 | 2022-11-08 15:31:09 +0100 | [diff] [blame] | 160 | printk(BIOS_DEBUG, "read %2.2x from %4.4lx\n", |
| 161 | v, ((uintptr_t)addr & 0xffff) - 0xf020); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 162 | return v; |
| 163 | } |
| 164 | |
| 165 | static u16 readw_(const void *addr) |
| 166 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 167 | u16 v = read16(addr); |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 168 | |
Arthur Heymans | d7fc068 | 2022-11-08 15:31:09 +0100 | [diff] [blame] | 169 | printk(BIOS_DEBUG, "read %4.4x from %4.4lx\n", |
| 170 | v, ((uintptr_t)addr & 0xffff) - 0xf020); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 171 | return v; |
| 172 | } |
| 173 | |
| 174 | static u32 readl_(const void *addr) |
| 175 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 176 | u32 v = read32(addr); |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 177 | |
Arthur Heymans | d7fc068 | 2022-11-08 15:31:09 +0100 | [diff] [blame] | 178 | printk(BIOS_DEBUG, "read %8.8x from %4.4lx\n", |
| 179 | v, ((uintptr_t)addr & 0xffff) - 0xf020); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 180 | return v; |
| 181 | } |
| 182 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 183 | static void writeb_(u8 b, void *addr) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 184 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 185 | write8(addr, b); |
Arthur Heymans | d7fc068 | 2022-11-08 15:31:09 +0100 | [diff] [blame] | 186 | printk(BIOS_DEBUG, "wrote %2.2x to %4.4lx\n", |
| 187 | b, ((uintptr_t)addr & 0xffff) - 0xf020); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 190 | static void writew_(u16 b, void *addr) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 191 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 192 | write16(addr, b); |
Arthur Heymans | d7fc068 | 2022-11-08 15:31:09 +0100 | [diff] [blame] | 193 | printk(BIOS_DEBUG, "wrote %4.4x to %4.4lx\n", |
| 194 | b, ((uintptr_t)addr & 0xffff) - 0xf020); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 197 | static void writel_(u32 b, void *addr) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 198 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 199 | write32(addr, b); |
Arthur Heymans | d7fc068 | 2022-11-08 15:31:09 +0100 | [diff] [blame] | 200 | printk(BIOS_DEBUG, "wrote %8.8x to %4.4lx\n", |
| 201 | b, ((uintptr_t)addr & 0xffff) - 0xf020); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | #else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */ |
| 205 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 206 | #define readb_(a) read8(a) |
| 207 | #define readw_(a) read16(a) |
| 208 | #define readl_(a) read32(a) |
| 209 | #define writeb_(val, addr) write8(addr, val) |
| 210 | #define writew_(val, addr) write16(addr, val) |
| 211 | #define writel_(val, addr) write32(addr, val) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 212 | |
| 213 | #endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */ |
| 214 | |
| 215 | static void write_reg(const void *value, void *dest, uint32_t size) |
| 216 | { |
| 217 | const uint8_t *bvalue = value; |
| 218 | uint8_t *bdest = dest; |
| 219 | |
| 220 | while (size >= 4) { |
| 221 | writel_(*(const uint32_t *)bvalue, bdest); |
| 222 | bdest += 4; bvalue += 4; size -= 4; |
| 223 | } |
| 224 | while (size) { |
| 225 | writeb_(*bvalue, bdest); |
| 226 | bdest++; bvalue++; size--; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | static void read_reg(const void *src, void *value, uint32_t size) |
| 231 | { |
| 232 | const uint8_t *bsrc = src; |
| 233 | uint8_t *bvalue = value; |
| 234 | |
| 235 | while (size >= 4) { |
| 236 | *(uint32_t *)bvalue = readl_(bsrc); |
| 237 | bsrc += 4; bvalue += 4; size -= 4; |
| 238 | } |
| 239 | while (size) { |
| 240 | *bvalue = readb_(bsrc); |
| 241 | bsrc++; bvalue++; size--; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | static void ich_set_bbar(uint32_t minaddr) |
| 246 | { |
| 247 | const uint32_t bbar_mask = 0x00ffff00; |
| 248 | uint32_t ichspi_bbar; |
| 249 | |
| 250 | minaddr &= bbar_mask; |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 251 | ichspi_bbar = readl_(cntlr.bbar) & ~bbar_mask; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 252 | ichspi_bbar |= minaddr; |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 253 | writel_(ichspi_bbar, cntlr.bbar); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Jacob Garber | 9172b69 | 2019-06-26 16:18:16 -0600 | [diff] [blame] | 256 | #if CONFIG(SOUTHBRIDGE_INTEL_I82801GX) |
| 257 | #define MENU_BYTES member_size(struct ich7_spi_regs, opmenu) |
| 258 | #else |
| 259 | #define MENU_BYTES member_size(struct ich9_spi_regs, opmenu) |
| 260 | #endif |
| 261 | |
Arthur Heymans | 47a6603 | 2019-10-25 23:43:14 +0200 | [diff] [blame] | 262 | #define RCBA 0xf0 |
| 263 | #define SBASE 0x54 |
| 264 | |
Arthur Heymans | 47a6603 | 2019-10-25 23:43:14 +0200 | [diff] [blame] | 265 | static void *get_spi_bar(pci_devfn_t dev) |
Arthur Heymans | 47a6603 | 2019-10-25 23:43:14 +0200 | [diff] [blame] | 266 | { |
| 267 | uintptr_t rcba; /* Root Complex Register Block */ |
| 268 | uintptr_t sbase; |
| 269 | |
| 270 | if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) { |
| 271 | rcba = pci_read_config32(dev, RCBA); |
| 272 | return (void *)((rcba & 0xffffc000) + 0x3020); |
| 273 | } |
| 274 | if (CONFIG(SOUTHBRIDGE_INTEL_COMMON_SPI_SILVERMONT)) { |
| 275 | sbase = pci_read_config32(dev, SBASE); |
| 276 | sbase &= ~0x1ff; |
| 277 | return (void *)sbase; |
| 278 | } |
| 279 | if (CONFIG(SOUTHBRIDGE_INTEL_COMMON_SPI_ICH9)) { |
| 280 | rcba = pci_read_config32(dev, RCBA); |
| 281 | return (void *)((rcba & 0xffffc000) + 0x3800); |
| 282 | } |
| 283 | } |
| 284 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 285 | void spi_init(void) |
| 286 | { |
Arthur Heymans | a9c1a5f | 2019-06-15 18:21:58 +0200 | [diff] [blame] | 287 | struct ich9_spi_regs *ich9_spi; |
| 288 | struct ich7_spi_regs *ich7_spi; |
Vladimir Serbinenko | 42c4a9d | 2014-02-16 17:13:19 +0100 | [diff] [blame] | 289 | uint16_t hsfs; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 290 | |
Elyes HAOUAS | 68c851b | 2018-06-12 22:06:09 +0200 | [diff] [blame] | 291 | pci_devfn_t dev = PCI_DEV(0, 31, 0); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 292 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 293 | if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) { |
Arthur Heymans | 47a6603 | 2019-10-25 23:43:14 +0200 | [diff] [blame] | 294 | ich7_spi = get_spi_bar(dev); |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 295 | cntlr.ich7_spi = ich7_spi; |
| 296 | cntlr.opmenu = ich7_spi->opmenu; |
| 297 | cntlr.menubytes = sizeof(ich7_spi->opmenu); |
| 298 | cntlr.optype = &ich7_spi->optype; |
| 299 | cntlr.addr = &ich7_spi->spia; |
| 300 | cntlr.data = (uint8_t *)ich7_spi->spid; |
| 301 | cntlr.databytes = sizeof(ich7_spi->spid); |
| 302 | cntlr.status = (uint8_t *)&ich7_spi->spis; |
| 303 | cntlr.control = &ich7_spi->spic; |
| 304 | cntlr.bbar = &ich7_spi->bbar; |
| 305 | cntlr.preop = &ich7_spi->preop; |
| 306 | cntlr.fpr = &ich7_spi->pbr[0]; |
| 307 | cntlr.fpr_max = 3; |
Arthur Heymans | c88e370 | 2017-08-20 20:50:17 +0200 | [diff] [blame] | 308 | } else { |
Arthur Heymans | 47a6603 | 2019-10-25 23:43:14 +0200 | [diff] [blame] | 309 | ich9_spi = get_spi_bar(dev); |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 310 | cntlr.ich9_spi = ich9_spi; |
Arthur Heymans | c88e370 | 2017-08-20 20:50:17 +0200 | [diff] [blame] | 311 | hsfs = readw_(&ich9_spi->hsfs); |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 312 | cntlr.hsfs = hsfs; |
| 313 | cntlr.opmenu = ich9_spi->opmenu; |
| 314 | cntlr.menubytes = sizeof(ich9_spi->opmenu); |
| 315 | cntlr.optype = &ich9_spi->optype; |
| 316 | cntlr.addr = &ich9_spi->faddr; |
| 317 | cntlr.data = (uint8_t *)ich9_spi->fdata; |
| 318 | cntlr.databytes = sizeof(ich9_spi->fdata); |
| 319 | cntlr.status = &ich9_spi->ssfs; |
| 320 | cntlr.control = (uint16_t *)ich9_spi->ssfc; |
| 321 | cntlr.bbar = &ich9_spi->bbar; |
| 322 | cntlr.preop = &ich9_spi->preop; |
| 323 | cntlr.fpr = &ich9_spi->pr[0]; |
| 324 | cntlr.fpr_max = 5; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 325 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 326 | if (cntlr.hsfs & HSFS_FDV) { |
Patrick Georgi | c88828d | 2018-11-26 10:42:59 +0100 | [diff] [blame] | 327 | writel_(4, &ich9_spi->fdoc); |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 328 | cntlr.flmap0 = readl_(&ich9_spi->fdod); |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 329 | writel_(0x1000, &ich9_spi->fdoc); |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 330 | cntlr.flcomp = readl_(&ich9_spi->fdod); |
Arthur Heymans | c88e370 | 2017-08-20 20:50:17 +0200 | [diff] [blame] | 331 | } |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | ich_set_bbar(0); |
| 335 | |
Angel Pons | d21b463 | 2021-02-10 17:12:05 +0100 | [diff] [blame] | 336 | /* Disable the BIOS write protect so write commands are allowed. */ |
| 337 | spi_set_smm_only_flashing(false); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 338 | } |
Aaron Durbin | 4d3de7e | 2015-09-02 17:34:04 -0500 | [diff] [blame] | 339 | |
Arthur Heymans | 816aaba | 2019-06-11 11:10:25 +0200 | [diff] [blame] | 340 | static int spi_locked(void) |
| 341 | { |
Arthur Heymans | 816aaba | 2019-06-11 11:10:25 +0200 | [diff] [blame] | 342 | if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) { |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 343 | return !!(readw_(&cntlr.ich7_spi->spis) & HSFS_FLOCKDN); |
Arthur Heymans | 816aaba | 2019-06-11 11:10:25 +0200 | [diff] [blame] | 344 | } else { |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 345 | return !!(readw_(&cntlr.ich9_spi->hsfs) & HSFS_FLOCKDN); |
Arthur Heymans | 816aaba | 2019-06-11 11:10:25 +0200 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
David Hendricks | f2612a1 | 2014-04-13 16:27:02 -0700 | [diff] [blame] | 349 | static void spi_init_cb(void *unused) |
| 350 | { |
| 351 | spi_init(); |
| 352 | } |
| 353 | |
Aaron Durbin | 9ef9d85 | 2015-03-16 17:30:09 -0500 | [diff] [blame] | 354 | BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 355 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 356 | typedef struct spi_transaction { |
| 357 | const uint8_t *out; |
| 358 | uint32_t bytesout; |
| 359 | uint8_t *in; |
| 360 | uint32_t bytesin; |
| 361 | uint8_t type; |
| 362 | uint8_t opcode; |
| 363 | uint32_t offset; |
| 364 | } spi_transaction; |
| 365 | |
Martin Roth | ff744bf | 2019-10-23 21:46:03 -0600 | [diff] [blame] | 366 | static inline void spi_use_out(spi_transaction *trans, unsigned int bytes) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 367 | { |
| 368 | trans->out += bytes; |
| 369 | trans->bytesout -= bytes; |
| 370 | } |
| 371 | |
Martin Roth | ff744bf | 2019-10-23 21:46:03 -0600 | [diff] [blame] | 372 | static inline void spi_use_in(spi_transaction *trans, unsigned int bytes) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 373 | { |
| 374 | trans->in += bytes; |
| 375 | trans->bytesin -= bytes; |
| 376 | } |
| 377 | |
| 378 | static void spi_setup_type(spi_transaction *trans) |
| 379 | { |
| 380 | trans->type = 0xFF; |
| 381 | |
| 382 | /* Try to guess spi type from read/write sizes. */ |
| 383 | if (trans->bytesin == 0) { |
| 384 | if (trans->bytesout > 4) |
| 385 | /* |
| 386 | * If bytesin = 0 and bytesout > 4, we presume this is |
| 387 | * a write data operation, which is accompanied by an |
| 388 | * address. |
| 389 | */ |
| 390 | trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS; |
| 391 | else |
| 392 | trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS; |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | if (trans->bytesout == 1) { /* and bytesin is > 0 */ |
| 397 | trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS; |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | if (trans->bytesout == 4) { /* and bytesin is > 0 */ |
| 402 | trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS; |
| 403 | } |
Duncan Laurie | 23b0053 | 2012-10-10 14:21:23 -0700 | [diff] [blame] | 404 | |
| 405 | /* Fast read command is called with 5 bytes instead of 4 */ |
| 406 | if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) { |
| 407 | trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS; |
| 408 | --trans->bytesout; |
| 409 | } |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static int spi_setup_opcode(spi_transaction *trans) |
| 413 | { |
| 414 | uint16_t optypes; |
Jacob Garber | 9172b69 | 2019-06-26 16:18:16 -0600 | [diff] [blame] | 415 | uint8_t opmenu[MENU_BYTES]; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 416 | |
| 417 | trans->opcode = trans->out[0]; |
| 418 | spi_use_out(trans, 1); |
Arthur Heymans | 816aaba | 2019-06-11 11:10:25 +0200 | [diff] [blame] | 419 | if (!spi_locked()) { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 420 | /* The lock is off, so just use index 0. */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 421 | writeb_(trans->opcode, cntlr.opmenu); |
| 422 | optypes = readw_(cntlr.optype); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 423 | optypes = (optypes & 0xfffc) | (trans->type & 0x3); |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 424 | writew_(optypes, cntlr.optype); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 425 | return 0; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 426 | } |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 427 | |
| 428 | /* The lock is on. See if what we need is on the menu. */ |
| 429 | uint8_t optype; |
| 430 | uint16_t opcode_index; |
| 431 | |
| 432 | /* Write Enable is handled as atomic prefix */ |
| 433 | if (trans->opcode == SPI_OPCODE_WREN) |
| 434 | return 0; |
| 435 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 436 | read_reg(cntlr.opmenu, opmenu, sizeof(opmenu)); |
Jacob Garber | 9172b69 | 2019-06-26 16:18:16 -0600 | [diff] [blame] | 437 | for (opcode_index = 0; opcode_index < ARRAY_SIZE(opmenu); opcode_index++) { |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 438 | if (opmenu[opcode_index] == trans->opcode) |
| 439 | break; |
| 440 | } |
| 441 | |
Jacob Garber | 9172b69 | 2019-06-26 16:18:16 -0600 | [diff] [blame] | 442 | if (opcode_index == ARRAY_SIZE(opmenu)) { |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 443 | printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n", |
| 444 | trans->opcode); |
| 445 | return -1; |
| 446 | } |
| 447 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 448 | optypes = readw_(cntlr.optype); |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 449 | optype = (optypes >> (opcode_index * 2)) & 0x3; |
| 450 | if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS && |
| 451 | optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS && |
| 452 | trans->bytesout >= 3) { |
| 453 | /* We guessed wrong earlier. Fix it up. */ |
| 454 | trans->type = optype; |
| 455 | } |
| 456 | if (optype != trans->type) { |
| 457 | printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n", |
| 458 | optype); |
| 459 | return -1; |
| 460 | } |
| 461 | return opcode_index; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static int spi_setup_offset(spi_transaction *trans) |
| 465 | { |
| 466 | /* Separate the SPI address and data. */ |
| 467 | switch (trans->type) { |
| 468 | case SPI_OPCODE_TYPE_READ_NO_ADDRESS: |
| 469 | case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS: |
| 470 | return 0; |
| 471 | case SPI_OPCODE_TYPE_READ_WITH_ADDRESS: |
| 472 | case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS: |
| 473 | trans->offset = ((uint32_t)trans->out[0] << 16) | |
| 474 | ((uint32_t)trans->out[1] << 8) | |
| 475 | ((uint32_t)trans->out[2] << 0); |
| 476 | spi_use_out(trans, 3); |
| 477 | return 1; |
| 478 | default: |
| 479 | printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type); |
| 480 | return -1; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /* |
Philipp Deppenwiese | 93643e3 | 2014-10-17 16:10:32 +0200 | [diff] [blame] | 485 | * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 486 | * below is True) or 0. In case the wait was for the bit(s) to set - write |
| 487 | * those bits back, which would cause resetting them. |
| 488 | * |
| 489 | * Return the last read status value on success or -1 on failure. |
| 490 | */ |
| 491 | static int ich_status_poll(u16 bitmask, int wait_til_set) |
| 492 | { |
Philipp Deppenwiese | 93643e3 | 2014-10-17 16:10:32 +0200 | [diff] [blame] | 493 | int timeout = 600000; /* This will result in 6 seconds */ |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 494 | u16 status = 0; |
| 495 | |
| 496 | while (timeout--) { |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 497 | status = readw_(cntlr.status); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 498 | if (wait_til_set ^ ((status & bitmask) == 0)) { |
| 499 | if (wait_til_set) |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 500 | writew_((status & bitmask), cntlr.status); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 501 | return status; |
| 502 | } |
| 503 | udelay(10); |
| 504 | } |
| 505 | |
Philipp Deppenwiese | 93643e3 | 2014-10-17 16:10:32 +0200 | [diff] [blame] | 506 | printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n", |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 507 | status, bitmask); |
| 508 | return -1; |
| 509 | } |
| 510 | |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 511 | static int spi_is_multichip(void) |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 512 | { |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 513 | if (!(cntlr.hsfs & HSFS_FDV)) |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 514 | return 0; |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 515 | return !!((cntlr.flmap0 >> 8) & 3); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 516 | } |
| 517 | |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 518 | static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout, |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 519 | size_t bytesout, void *din, size_t bytesin) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 520 | { |
| 521 | uint16_t control; |
| 522 | int16_t opcode_index; |
| 523 | int with_address; |
| 524 | int status; |
| 525 | |
| 526 | spi_transaction trans = { |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 527 | dout, bytesout, |
| 528 | din, bytesin, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 529 | 0xff, 0xff, 0 |
| 530 | }; |
| 531 | |
| 532 | /* There has to always at least be an opcode. */ |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 533 | if (!bytesout || !dout) { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 534 | printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n"); |
| 535 | return -1; |
| 536 | } |
| 537 | /* Make sure if we read something we have a place to put it. */ |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 538 | if (bytesin != 0 && !din) { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 539 | printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n"); |
| 540 | return -1; |
| 541 | } |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 542 | |
| 543 | if (ich_status_poll(SPIS_SCIP, 0) == -1) |
| 544 | return -1; |
| 545 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 546 | writew_(SPIS_CDS | SPIS_FCERR, cntlr.status); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 547 | |
| 548 | spi_setup_type(&trans); |
| 549 | if ((opcode_index = spi_setup_opcode(&trans)) < 0) |
| 550 | return -1; |
| 551 | if ((with_address = spi_setup_offset(&trans)) < 0) |
| 552 | return -1; |
| 553 | |
Duncan Laurie | 3beb6db | 2012-09-01 13:44:17 -0700 | [diff] [blame] | 554 | if (trans.opcode == SPI_OPCODE_WREN) { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 555 | /* |
| 556 | * Treat Write Enable as Atomic Pre-Op if possible |
| 557 | * in order to prevent the Management Engine from |
| 558 | * issuing a transaction between WREN and DATA. |
| 559 | */ |
Arthur Heymans | 816aaba | 2019-06-11 11:10:25 +0200 | [diff] [blame] | 560 | if (!spi_locked()) |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 561 | writew_(trans.opcode, cntlr.preop); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | /* Preset control fields */ |
| 566 | control = SPIC_SCGO | ((opcode_index & 0x07) << 4); |
| 567 | |
| 568 | /* Issue atomic preop cycle if needed */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 569 | if (readw_(cntlr.preop)) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 570 | control |= SPIC_ACS; |
| 571 | |
| 572 | if (!trans.bytesout && !trans.bytesin) { |
Duncan Laurie | 3beb6db | 2012-09-01 13:44:17 -0700 | [diff] [blame] | 573 | /* SPI addresses are 24 bit only */ |
| 574 | if (with_address) |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 575 | writel_(trans.offset & 0x00FFFFFF, cntlr.addr); |
Duncan Laurie | 3beb6db | 2012-09-01 13:44:17 -0700 | [diff] [blame] | 576 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 577 | /* |
| 578 | * This is a 'no data' command (like Write Enable), its |
| 579 | * bitesout size was 1, decremented to zero while executing |
| 580 | * spi_setup_opcode() above. Tell the chip to send the |
| 581 | * command. |
| 582 | */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 583 | writew_(control, cntlr.control); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 584 | |
| 585 | /* wait for the result */ |
| 586 | status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1); |
| 587 | if (status == -1) |
| 588 | return -1; |
| 589 | |
| 590 | if (status & SPIS_FCERR) { |
| 591 | printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n"); |
| 592 | return -1; |
| 593 | } |
| 594 | |
Werner Zeh | f13a6f9 | 2018-11-14 10:55:52 +0100 | [diff] [blame] | 595 | goto spi_xfer_exit; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /* |
Paul Menzel | 9478297 | 2013-06-29 11:41:27 +0200 | [diff] [blame] | 599 | * Check if this is a write command attempting to transfer more bytes |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 600 | * than the controller can handle. Iterations for writes are not |
| 601 | * supported here because each SPI write command needs to be preceded |
| 602 | * and followed by other SPI commands, and this sequence is controlled |
| 603 | * by the SPI chip driver. |
| 604 | */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 605 | if (trans.bytesout > cntlr.databytes) { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 606 | printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use" |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 607 | " spi_crop_chunk()?\n"); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 608 | return -1; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * Read or write up to databytes bytes at a time until everything has |
| 613 | * been sent. |
| 614 | */ |
| 615 | while (trans.bytesout || trans.bytesin) { |
| 616 | uint32_t data_length; |
| 617 | |
| 618 | /* SPI addresses are 24 bit only */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 619 | writel_(trans.offset & 0x00FFFFFF, cntlr.addr); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 620 | |
| 621 | if (trans.bytesout) |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 622 | data_length = MIN(trans.bytesout, cntlr.databytes); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 623 | else |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 624 | data_length = MIN(trans.bytesin, cntlr.databytes); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 625 | |
| 626 | /* Program data into FDATA0 to N */ |
| 627 | if (trans.bytesout) { |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 628 | write_reg(trans.out, cntlr.data, data_length); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 629 | spi_use_out(&trans, data_length); |
| 630 | if (with_address) |
| 631 | trans.offset += data_length; |
| 632 | } |
| 633 | |
| 634 | /* Add proper control fields' values */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 635 | control &= ~((cntlr.databytes - 1) << 8); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 636 | control |= SPIC_DS; |
| 637 | control |= (data_length - 1) << 8; |
| 638 | |
| 639 | /* write it */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 640 | writew_(control, cntlr.control); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 641 | |
| 642 | /* Wait for Cycle Done Status or Flash Cycle Error. */ |
| 643 | status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1); |
| 644 | if (status == -1) |
| 645 | return -1; |
| 646 | |
| 647 | if (status & SPIS_FCERR) { |
| 648 | printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n"); |
| 649 | return -1; |
| 650 | } |
| 651 | |
| 652 | if (trans.bytesin) { |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 653 | read_reg(cntlr.data, trans.in, data_length); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 654 | spi_use_in(&trans, data_length); |
| 655 | if (with_address) |
| 656 | trans.offset += data_length; |
| 657 | } |
| 658 | } |
| 659 | |
Werner Zeh | f13a6f9 | 2018-11-14 10:55:52 +0100 | [diff] [blame] | 660 | spi_xfer_exit: |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 661 | /* Clear atomic preop now that xfer is done */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 662 | writew_(0, cntlr.preop); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 663 | |
| 664 | return 0; |
| 665 | } |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 666 | |
| 667 | /* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */ |
| 668 | static void ich_hwseq_set_addr(uint32_t addr) |
| 669 | { |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 670 | uint32_t addr_old = readl_(&cntlr.ich9_spi->faddr) & ~0x01FFFFFF; |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 671 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 672 | writel_((addr & 0x01FFFFFF) | addr_old, &cntlr.ich9_spi->faddr); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals. |
| 676 | Resets all error flags in HSFS. |
| 677 | Returns 0 if the cycle completes successfully without errors within |
| 678 | timeout us, 1 on errors. */ |
| 679 | static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout, |
| 680 | unsigned int len) |
| 681 | { |
| 682 | uint16_t hsfs; |
| 683 | uint32_t addr; |
| 684 | |
| 685 | timeout /= 8; /* scale timeout duration to counter */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 686 | while ((((hsfs = readw_(&cntlr.ich9_spi->hsfs)) & |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 687 | (HSFS_FDONE | HSFS_FCERR)) == 0) && |
| 688 | --timeout) { |
| 689 | udelay(8); |
| 690 | } |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 691 | writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 692 | |
| 693 | if (!timeout) { |
| 694 | uint16_t hsfc; |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 695 | addr = readl_(&cntlr.ich9_spi->faddr) & 0x01FFFFFF; |
| 696 | hsfc = readw_(&cntlr.ich9_spi->hsfc); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 697 | printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and " |
| 698 | "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n", |
| 699 | addr, addr + len - 1, addr, len - 1, |
| 700 | hsfc, hsfs); |
| 701 | return 1; |
| 702 | } |
| 703 | |
| 704 | if (hsfs & HSFS_FCERR) { |
| 705 | uint16_t hsfc; |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 706 | addr = readl_(&cntlr.ich9_spi->faddr) & 0x01FFFFFF; |
| 707 | hsfc = readw_(&cntlr.ich9_spi->hsfc); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 708 | printk(BIOS_ERR, "Transaction error between offset 0x%08x and " |
| 709 | "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n", |
| 710 | addr, addr + len - 1, addr, len - 1, |
| 711 | hsfc, hsfs); |
| 712 | return 1; |
| 713 | } |
| 714 | return 0; |
| 715 | } |
| 716 | |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 717 | static int ich_hwseq_erase(const struct spi_flash *flash, u32 offset, |
| 718 | size_t len) |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 719 | { |
| 720 | u32 start, end, erase_size; |
| 721 | int ret; |
| 722 | uint16_t hsfc; |
Aaron Durbin | 4ed8e9c | 2019-12-27 14:30:51 -0700 | [diff] [blame] | 723 | unsigned int timeout = 1000 * USECS_PER_MSEC; /* 1 second timeout */ |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 724 | |
| 725 | erase_size = flash->sector_size; |
| 726 | if (offset % erase_size || len % erase_size) { |
| 727 | printk(BIOS_ERR, "SF: Erase offset/length not multiple of erase size\n"); |
| 728 | return -1; |
| 729 | } |
| 730 | |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 731 | ret = spi_claim_bus(&flash->spi); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 732 | if (ret) { |
| 733 | printk(BIOS_ERR, "SF: Unable to claim SPI bus\n"); |
| 734 | return ret; |
| 735 | } |
| 736 | |
| 737 | start = offset; |
| 738 | end = start + len; |
| 739 | |
| 740 | while (offset < end) { |
| 741 | /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 742 | writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 743 | |
| 744 | ich_hwseq_set_addr(offset); |
| 745 | |
| 746 | offset += erase_size; |
| 747 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 748 | hsfc = readw_(&cntlr.ich9_spi->hsfc); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 749 | hsfc &= ~HSFC_FCYCLE; /* clear operation */ |
| 750 | hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */ |
| 751 | hsfc |= HSFC_FGO; /* start */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 752 | writew_(hsfc, &cntlr.ich9_spi->hsfc); |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 753 | if (ich_hwseq_wait_for_cycle_complete(timeout, len)) { |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 754 | printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size); |
| 755 | ret = -1; |
| 756 | goto out; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start); |
| 761 | |
| 762 | out: |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 763 | spi_release_bus(&flash->spi); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | static void ich_read_data(uint8_t *data, int len) |
| 768 | { |
| 769 | int i; |
| 770 | uint32_t temp32 = 0; |
| 771 | |
| 772 | for (i = 0; i < len; i++) { |
| 773 | if ((i % 4) == 0) |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 774 | temp32 = readl_(cntlr.data + i); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 775 | |
| 776 | data[i] = (temp32 >> ((i % 4) * 8)) & 0xff; |
| 777 | } |
| 778 | } |
| 779 | |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 780 | static int ich_hwseq_read(const struct spi_flash *flash, u32 addr, size_t len, |
| 781 | void *buf) |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 782 | { |
| 783 | uint16_t hsfc; |
| 784 | uint16_t timeout = 100 * 60; |
| 785 | uint8_t block_len; |
| 786 | |
| 787 | if (addr + len > flash->size) { |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 788 | printk(BIOS_ERR, |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 789 | "Attempt to read %x-%x which is out of chip\n", |
Elyes Haouas | 723b896 | 2022-11-18 15:07:03 +0100 | [diff] [blame] | 790 | (unsigned int)addr, |
Elyes Haouas | f0ed273 | 2023-09-09 08:53:10 +0200 | [diff] [blame] | 791 | (unsigned int)addr+(unsigned int)len); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 792 | return -1; |
| 793 | } |
| 794 | |
| 795 | /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 796 | writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 797 | |
| 798 | while (len > 0) { |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 799 | block_len = MIN(len, cntlr.databytes); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 800 | if (block_len > (~addr & 0xff)) |
| 801 | block_len = (~addr & 0xff) + 1; |
| 802 | ich_hwseq_set_addr(addr); |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 803 | hsfc = readw_(&cntlr.ich9_spi->hsfc); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 804 | hsfc &= ~HSFC_FCYCLE; /* set read operation */ |
| 805 | hsfc &= ~HSFC_FDBC; /* clear byte count */ |
| 806 | /* set byte count */ |
| 807 | hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC); |
| 808 | hsfc |= HSFC_FGO; /* start */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 809 | writew_(hsfc, &cntlr.ich9_spi->hsfc); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 810 | |
| 811 | if (ich_hwseq_wait_for_cycle_complete(timeout, block_len)) |
| 812 | return 1; |
| 813 | ich_read_data(buf, block_len); |
| 814 | addr += block_len; |
| 815 | buf += block_len; |
| 816 | len -= block_len; |
| 817 | } |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | /* Fill len bytes from the data array into the fdata/spid registers. |
| 822 | * |
| 823 | * Note that using len > flash->pgm->spi.max_data_write will trash the registers |
| 824 | * following the data registers. |
| 825 | */ |
| 826 | static void ich_fill_data(const uint8_t *data, int len) |
| 827 | { |
| 828 | uint32_t temp32 = 0; |
| 829 | int i; |
| 830 | |
| 831 | if (len <= 0) |
| 832 | return; |
| 833 | |
| 834 | for (i = 0; i < len; i++) { |
| 835 | if ((i % 4) == 0) |
| 836 | temp32 = 0; |
| 837 | |
Elyes Haouas | 723b896 | 2022-11-18 15:07:03 +0100 | [diff] [blame] | 838 | temp32 |= ((uint32_t)data[i]) << ((i % 4) * 8); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 839 | |
| 840 | if ((i % 4) == 3) /* 32 bits are full, write them to regs. */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 841 | writel_(temp32, cntlr.data + (i - (i % 4))); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 842 | } |
| 843 | i--; |
| 844 | if ((i % 4) != 3) /* Write remaining data to regs. */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 845 | writel_(temp32, cntlr.data + (i - (i % 4))); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 846 | } |
| 847 | |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 848 | static int ich_hwseq_write(const struct spi_flash *flash, u32 addr, size_t len, |
| 849 | const void *buf) |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 850 | { |
| 851 | uint16_t hsfc; |
| 852 | uint16_t timeout = 100 * 60; |
| 853 | uint8_t block_len; |
| 854 | uint32_t start = addr; |
| 855 | |
| 856 | if (addr + len > flash->size) { |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 857 | printk(BIOS_ERR, |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 858 | "Attempt to write 0x%x-0x%x which is out of chip\n", |
Elyes Haouas | 723b896 | 2022-11-18 15:07:03 +0100 | [diff] [blame] | 859 | (unsigned int)addr, (unsigned int)(addr+len)); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 860 | return -1; |
| 861 | } |
| 862 | |
| 863 | /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 864 | writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 865 | |
| 866 | while (len > 0) { |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 867 | block_len = MIN(len, cntlr.databytes); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 868 | if (block_len > (~addr & 0xff)) |
| 869 | block_len = (~addr & 0xff) + 1; |
| 870 | |
| 871 | ich_hwseq_set_addr(addr); |
| 872 | |
| 873 | ich_fill_data(buf, block_len); |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 874 | hsfc = readw_(&cntlr.ich9_spi->hsfc); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 875 | hsfc &= ~HSFC_FCYCLE; /* clear operation */ |
| 876 | hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */ |
| 877 | hsfc &= ~HSFC_FDBC; /* clear byte count */ |
| 878 | /* set byte count */ |
| 879 | hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC); |
| 880 | hsfc |= HSFC_FGO; /* start */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 881 | writew_(hsfc, &cntlr.ich9_spi->hsfc); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 882 | |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 883 | if (ich_hwseq_wait_for_cycle_complete(timeout, block_len)) { |
| 884 | printk(BIOS_ERR, "SF: write failure at %x\n", |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 885 | addr); |
| 886 | return -1; |
| 887 | } |
| 888 | addr += block_len; |
| 889 | buf += block_len; |
| 890 | len -= block_len; |
| 891 | } |
| 892 | printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n", |
Elyes Haouas | 723b896 | 2022-11-18 15:07:03 +0100 | [diff] [blame] | 893 | (unsigned int)(addr - start), start); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 894 | return 0; |
| 895 | } |
| 896 | |
Furquan Shaikh | e2fc5e2 | 2017-05-17 17:26:01 -0700 | [diff] [blame] | 897 | static const struct spi_flash_ops spi_flash_ops = { |
| 898 | .read = ich_hwseq_read, |
| 899 | .write = ich_hwseq_write, |
| 900 | .erase = ich_hwseq_erase, |
| 901 | }; |
| 902 | |
Furquan Shaikh | a149157 | 2017-05-17 19:14:06 -0700 | [diff] [blame] | 903 | static int spi_flash_programmer_probe(const struct spi_slave *spi, |
| 904 | struct spi_flash *flash) |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 905 | { |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 906 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 907 | if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) |
Arthur Heymans | c88e370 | 2017-08-20 20:50:17 +0200 | [diff] [blame] | 908 | return spi_flash_generic_probe(spi, flash); |
| 909 | |
Furquan Shaikh | a149157 | 2017-05-17 19:14:06 -0700 | [diff] [blame] | 910 | /* Try generic probing first if spi_is_multichip returns 0. */ |
| 911 | if (!spi_is_multichip() && !spi_flash_generic_probe(spi, flash)) |
| 912 | return 0; |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 913 | |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 914 | memcpy(&flash->spi, spi, sizeof(*spi)); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 915 | |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 916 | ich_hwseq_set_addr(0); |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 917 | switch ((cntlr.hsfs >> 3) & 3) { |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 918 | case 0: |
| 919 | flash->sector_size = 256; |
| 920 | break; |
| 921 | case 1: |
| 922 | flash->sector_size = 4096; |
| 923 | break; |
| 924 | case 2: |
| 925 | flash->sector_size = 8192; |
| 926 | break; |
| 927 | case 3: |
| 928 | flash->sector_size = 65536; |
| 929 | break; |
| 930 | } |
| 931 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 932 | flash->size = 1 << (19 + (cntlr.flcomp & 7)); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 933 | |
Furquan Shaikh | e2fc5e2 | 2017-05-17 17:26:01 -0700 | [diff] [blame] | 934 | flash->ops = &spi_flash_ops; |
| 935 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 936 | if ((cntlr.hsfs & HSFS_FDV) && ((cntlr.flmap0 >> 8) & 3)) |
| 937 | flash->size += 1 << (19 + ((cntlr.flcomp >> 3) & 7)); |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 938 | printk(BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size); |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 939 | |
Furquan Shaikh | 30221b4 | 2017-05-15 14:35:15 -0700 | [diff] [blame] | 940 | return 0; |
Vladimir Serbinenko | f3c7a9c | 2014-01-04 21:00:38 +0100 | [diff] [blame] | 941 | } |
Furquan Shaikh | a149157 | 2017-05-17 19:14:06 -0700 | [diff] [blame] | 942 | |
Aaron Durbin | 851dde8 | 2018-04-19 21:15:25 -0600 | [diff] [blame] | 943 | static int xfer_vectors(const struct spi_slave *slave, |
| 944 | struct spi_op vectors[], size_t count) |
| 945 | { |
| 946 | return spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer); |
| 947 | } |
| 948 | |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 949 | #define SPI_FPR_SHIFT 12 |
Arthur Heymans | 11fcb2bc | 2018-01-07 20:46:31 +0100 | [diff] [blame] | 950 | #define ICH7_SPI_FPR_MASK 0xfff |
| 951 | #define ICH9_SPI_FPR_MASK 0x1fff |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 952 | #define SPI_FPR_BASE_SHIFT 0 |
Arthur Heymans | 11fcb2bc | 2018-01-07 20:46:31 +0100 | [diff] [blame] | 953 | #define ICH7_SPI_FPR_LIMIT_SHIFT 12 |
| 954 | #define ICH9_SPI_FPR_LIMIT_SHIFT 16 |
| 955 | #define ICH9_SPI_FPR_RPE (1 << 15) /* Read Protect */ |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 956 | #define SPI_FPR_WPE (1 << 31) /* Write Protect */ |
Arthur Heymans | 11fcb2bc | 2018-01-07 20:46:31 +0100 | [diff] [blame] | 957 | |
| 958 | static u32 spi_fpr(u32 base, u32 limit) |
| 959 | { |
| 960 | u32 ret; |
| 961 | u32 mask, limit_shift; |
Elyes HAOUAS | ad19c2f | 2018-11-26 15:57:30 +0100 | [diff] [blame] | 962 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 963 | if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) { |
Arthur Heymans | 11fcb2bc | 2018-01-07 20:46:31 +0100 | [diff] [blame] | 964 | mask = ICH7_SPI_FPR_MASK; |
| 965 | limit_shift = ICH7_SPI_FPR_LIMIT_SHIFT; |
| 966 | } else { |
| 967 | mask = ICH9_SPI_FPR_MASK; |
| 968 | limit_shift = ICH9_SPI_FPR_LIMIT_SHIFT; |
| 969 | } |
| 970 | ret = ((limit >> SPI_FPR_SHIFT) & mask) << limit_shift; |
| 971 | ret |= ((base >> SPI_FPR_SHIFT) & mask) << SPI_FPR_BASE_SHIFT; |
| 972 | return ret; |
| 973 | } |
| 974 | |
| 975 | /* |
| 976 | * Protect range of SPI flash defined by [start, start+size-1] using Flash |
| 977 | * Protected Range (FPR) register if available. |
| 978 | * Returns 0 on success, -1 on failure of programming fpr registers. |
| 979 | */ |
| 980 | static int spi_flash_protect(const struct spi_flash *flash, |
Rizwan Qureshi | f9f5093 | 2018-12-31 15:19:16 +0530 | [diff] [blame] | 981 | const struct region *region, |
| 982 | const enum ctrlr_prot_type type) |
Arthur Heymans | 11fcb2bc | 2018-01-07 20:46:31 +0100 | [diff] [blame] | 983 | { |
| 984 | u32 start = region_offset(region); |
| 985 | u32 end = start + region_sz(region) - 1; |
| 986 | u32 reg; |
Rizwan Qureshi | f9f5093 | 2018-12-31 15:19:16 +0530 | [diff] [blame] | 987 | u32 protect_mask = 0; |
Arthur Heymans | 11fcb2bc | 2018-01-07 20:46:31 +0100 | [diff] [blame] | 988 | int fpr; |
| 989 | uint32_t *fpr_base; |
| 990 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 991 | fpr_base = cntlr.fpr; |
Arthur Heymans | 11fcb2bc | 2018-01-07 20:46:31 +0100 | [diff] [blame] | 992 | |
| 993 | /* Find first empty FPR */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 994 | for (fpr = 0; fpr < cntlr.fpr_max; fpr++) { |
Arthur Heymans | 11fcb2bc | 2018-01-07 20:46:31 +0100 | [diff] [blame] | 995 | reg = read32(&fpr_base[fpr]); |
| 996 | if (reg == 0) |
| 997 | break; |
| 998 | } |
| 999 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 1000 | if (fpr == cntlr.fpr_max) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 1001 | printk(BIOS_ERR, "No SPI FPR free!\n"); |
Arthur Heymans | 11fcb2bc | 2018-01-07 20:46:31 +0100 | [diff] [blame] | 1002 | return -1; |
| 1003 | } |
| 1004 | |
Rizwan Qureshi | f9f5093 | 2018-12-31 15:19:16 +0530 | [diff] [blame] | 1005 | switch (type) { |
| 1006 | case WRITE_PROTECT: |
| 1007 | protect_mask |= SPI_FPR_WPE; |
| 1008 | break; |
| 1009 | case READ_PROTECT: |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 1010 | if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) |
Rizwan Qureshi | f9f5093 | 2018-12-31 15:19:16 +0530 | [diff] [blame] | 1011 | return -1; |
| 1012 | protect_mask |= ICH9_SPI_FPR_RPE; |
| 1013 | break; |
| 1014 | case READ_WRITE_PROTECT: |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 1015 | if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) |
Rizwan Qureshi | f9f5093 | 2018-12-31 15:19:16 +0530 | [diff] [blame] | 1016 | return -1; |
| 1017 | protect_mask |= (ICH9_SPI_FPR_RPE | SPI_FPR_WPE); |
| 1018 | break; |
| 1019 | default: |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 1020 | printk(BIOS_ERR, "Seeking invalid protection!\n"); |
Rizwan Qureshi | f9f5093 | 2018-12-31 15:19:16 +0530 | [diff] [blame] | 1021 | return -1; |
| 1022 | } |
| 1023 | |
Arthur Heymans | 11fcb2bc | 2018-01-07 20:46:31 +0100 | [diff] [blame] | 1024 | /* Set protected range base and limit */ |
Rizwan Qureshi | f9f5093 | 2018-12-31 15:19:16 +0530 | [diff] [blame] | 1025 | reg = spi_fpr(start, end) | protect_mask; |
Arthur Heymans | 11fcb2bc | 2018-01-07 20:46:31 +0100 | [diff] [blame] | 1026 | |
| 1027 | /* Set the FPR register and verify it is protected */ |
| 1028 | write32(&fpr_base[fpr], reg); |
Arthur Heymans | f957201 | 2019-06-11 11:15:10 +0200 | [diff] [blame] | 1029 | if (reg != read32(&fpr_base[fpr])) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 1030 | printk(BIOS_ERR, "Unable to set SPI FPR %d\n", fpr); |
Arthur Heymans | 11fcb2bc | 2018-01-07 20:46:31 +0100 | [diff] [blame] | 1031 | return -1; |
| 1032 | } |
| 1033 | |
| 1034 | printk(BIOS_INFO, "%s: FPR %d is enabled for range 0x%08x-0x%08x\n", |
| 1035 | __func__, fpr, start, end); |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
Arthur Heymans | 92185e3 | 2019-05-28 13:06:34 +0200 | [diff] [blame] | 1039 | void spi_finalize_ops(void) |
| 1040 | { |
Arthur Heymans | 92185e3 | 2019-05-28 13:06:34 +0200 | [diff] [blame] | 1041 | u16 spi_opprefix; |
| 1042 | u16 optype = 0; |
Arthur Heymans | 50b4f78 | 2019-09-23 11:49:17 +0200 | [diff] [blame] | 1043 | struct intel_swseq_spi_config spi_config_default = { |
Arthur Heymans | 92185e3 | 2019-05-28 13:06:34 +0200 | [diff] [blame] | 1044 | {0x06, 0x50}, /* OPPREFIXES: EWSR and WREN */ |
Arthur Heymans | 50b4f78 | 2019-09-23 11:49:17 +0200 | [diff] [blame] | 1045 | { /* OPCODE and OPTYPE */ |
Arthur Heymans | 92185e3 | 2019-05-28 13:06:34 +0200 | [diff] [blame] | 1046 | {0x01, WRITE_NO_ADDR}, /* WRSR: Write Status Register */ |
| 1047 | {0x02, WRITE_WITH_ADDR}, /* BYPR: Byte Program */ |
| 1048 | {0x03, READ_WITH_ADDR}, /* READ: Read Data */ |
| 1049 | {0x05, READ_NO_ADDR}, /* RDSR: Read Status Register */ |
| 1050 | {0x20, WRITE_WITH_ADDR}, /* SE20: Sector Erase 0x20 */ |
| 1051 | {0x9f, READ_NO_ADDR}, /* RDID: Read ID */ |
| 1052 | {0xd8, WRITE_WITH_ADDR}, /* BED8: Block Erase 0xd8 */ |
| 1053 | {0x0b, READ_WITH_ADDR}, /* FAST: Fast Read */ |
| 1054 | } |
| 1055 | }; |
Arthur Heymans | 50b4f78 | 2019-09-23 11:49:17 +0200 | [diff] [blame] | 1056 | struct intel_swseq_spi_config spi_config_aai_write = { |
| 1057 | {0x06, 0x50}, /* OPPREFIXES: EWSR and WREN */ |
| 1058 | { /* OPCODE and OPTYPE */ |
| 1059 | {0x01, WRITE_NO_ADDR}, /* WRSR: Write Status Register */ |
| 1060 | {0x02, WRITE_WITH_ADDR}, /* BYPR: Byte Program */ |
| 1061 | {0x03, READ_WITH_ADDR}, /* READ: Read Data */ |
| 1062 | {0x05, READ_NO_ADDR}, /* RDSR: Read Status Register */ |
| 1063 | {0x20, WRITE_WITH_ADDR}, /* SE20: Sector Erase 0x20 */ |
| 1064 | {0x9f, READ_NO_ADDR}, /* RDID: Read ID */ |
| 1065 | {0xad, WRITE_NO_ADDR}, /* Auto Address Increment Word Program */ |
| 1066 | {0x04, WRITE_NO_ADDR} /* Write Disable */ |
| 1067 | } |
| 1068 | }; |
| 1069 | const struct spi_flash *flash = boot_device_spi_flash(); |
| 1070 | struct intel_swseq_spi_config *spi_config = &spi_config_default; |
Arthur Heymans | 92185e3 | 2019-05-28 13:06:34 +0200 | [diff] [blame] | 1071 | int i; |
| 1072 | |
Arthur Heymans | 50b4f78 | 2019-09-23 11:49:17 +0200 | [diff] [blame] | 1073 | /* |
| 1074 | * Some older SST SPI flashes support AAI write but use 0xaf opcde for |
| 1075 | * that. Flashrom uses the byte program opcode to write those flashes, |
| 1076 | * so this configuration is fine too. SST25VF064C (id = 0x4b) is an |
| 1077 | * exception. |
| 1078 | */ |
| 1079 | if (flash && flash->vendor == VENDOR_ID_SST && (flash->model & 0x00ff) != 0x4b) |
| 1080 | spi_config = &spi_config_aai_write; |
| 1081 | |
Arthur Heymans | 92185e3 | 2019-05-28 13:06:34 +0200 | [diff] [blame] | 1082 | if (spi_locked()) |
| 1083 | return; |
| 1084 | |
Arthur Heymans | 50b4f78 | 2019-09-23 11:49:17 +0200 | [diff] [blame] | 1085 | intel_southbridge_override_spi(spi_config); |
Arthur Heymans | 92185e3 | 2019-05-28 13:06:34 +0200 | [diff] [blame] | 1086 | |
Arthur Heymans | 50b4f78 | 2019-09-23 11:49:17 +0200 | [diff] [blame] | 1087 | spi_opprefix = spi_config->opprefixes[0] |
| 1088 | | (spi_config->opprefixes[1] << 8); |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 1089 | writew_(spi_opprefix, cntlr.preop); |
Arthur Heymans | 50b4f78 | 2019-09-23 11:49:17 +0200 | [diff] [blame] | 1090 | for (i = 0; i < ARRAY_SIZE(spi_config->ops); i++) { |
| 1091 | optype |= (spi_config->ops[i].type & 3) << (i * 2); |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 1092 | writeb_(spi_config->ops[i].op, &cntlr.opmenu[i]); |
Arthur Heymans | 92185e3 | 2019-05-28 13:06:34 +0200 | [diff] [blame] | 1093 | } |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 1094 | writew_(optype, cntlr.optype); |
Angel Pons | d21b463 | 2021-02-10 17:12:05 +0100 | [diff] [blame] | 1095 | |
| 1096 | spi_set_smm_only_flashing(CONFIG(BOOTMEDIA_SMM_BWP)); |
Arthur Heymans | 92185e3 | 2019-05-28 13:06:34 +0200 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | __weak void intel_southbridge_override_spi(struct intel_swseq_spi_config *spi_config) |
| 1100 | { |
| 1101 | } |
| 1102 | |
Angel Pons | d21b463 | 2021-02-10 17:12:05 +0100 | [diff] [blame] | 1103 | #define BIOS_CNTL 0xdc |
| 1104 | #define BIOS_CNTL_BIOSWE (1 << 0) |
| 1105 | #define BIOS_CNTL_BLE (1 << 1) |
| 1106 | #define BIOS_CNTL_SMM_BWP (1 << 5) |
| 1107 | |
| 1108 | static void spi_set_smm_only_flashing(bool enable) |
| 1109 | { |
| 1110 | if (!(CONFIG(SOUTHBRIDGE_INTEL_I82801GX) || CONFIG(SOUTHBRIDGE_INTEL_COMMON_SPI_ICH9))) |
| 1111 | return; |
| 1112 | |
| 1113 | const pci_devfn_t dev = PCI_DEV(0, 31, 0); |
| 1114 | |
| 1115 | uint8_t bios_cntl = pci_read_config8(dev, BIOS_CNTL); |
| 1116 | |
| 1117 | if (enable) { |
| 1118 | bios_cntl &= ~BIOS_CNTL_BIOSWE; |
| 1119 | bios_cntl |= BIOS_CNTL_BLE | BIOS_CNTL_SMM_BWP; |
| 1120 | } else { |
| 1121 | bios_cntl &= ~(BIOS_CNTL_BLE | BIOS_CNTL_SMM_BWP); |
| 1122 | bios_cntl |= BIOS_CNTL_BIOSWE; |
| 1123 | } |
| 1124 | |
| 1125 | pci_write_config8(dev, BIOS_CNTL, bios_cntl); |
| 1126 | } |
| 1127 | |
Furquan Shaikh | a149157 | 2017-05-17 19:14:06 -0700 | [diff] [blame] | 1128 | static const struct spi_ctrlr spi_ctrlr = { |
Aaron Durbin | 851dde8 | 2018-04-19 21:15:25 -0600 | [diff] [blame] | 1129 | .xfer_vector = xfer_vectors, |
Arthur Heymans | a9c1a5f | 2019-06-15 18:21:58 +0200 | [diff] [blame] | 1130 | .max_xfer_size = member_size(struct ich9_spi_regs, fdata), |
Furquan Shaikh | a149157 | 2017-05-17 19:14:06 -0700 | [diff] [blame] | 1131 | .flash_probe = spi_flash_programmer_probe, |
Arthur Heymans | 11fcb2bc | 2018-01-07 20:46:31 +0100 | [diff] [blame] | 1132 | .flash_protect = spi_flash_protect, |
Furquan Shaikh | a149157 | 2017-05-17 19:14:06 -0700 | [diff] [blame] | 1133 | }; |
| 1134 | |
Furquan Shaikh | 2cd03f1 | 2017-05-18 14:58:32 -0700 | [diff] [blame] | 1135 | const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = { |
| 1136 | { |
| 1137 | .ctrlr = &spi_ctrlr, |
| 1138 | .bus_start = 0, |
| 1139 | .bus_end = 0, |
| 1140 | }, |
| 1141 | }; |
| 1142 | |
| 1143 | const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map); |