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