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