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