Martin Roth | 5856240 | 2015-10-11 10:36:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The Chromium OS Authors. |
| 3 | * Copyright (C) 2013, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; either version 2 of |
| 8 | * the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but without any warranty; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Martin Roth | 5856240 | 2015-10-11 10:36:26 +0200 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | /* This file is derived from the flashrom project. */ |
| 17 | #include <stdint.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 20 | #include <compiler.h> |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 21 | #include <commonlib/helpers.h> |
Martin Roth | 5856240 | 2015-10-11 10:36:26 +0200 | [diff] [blame] | 22 | #include <delay.h> |
| 23 | #include <arch/io.h> |
| 24 | #include <console/console.h> |
| 25 | #include <device/pci_ids.h> |
| 26 | |
Furquan Shaikh | 52896c6 | 2016-11-22 11:43:58 -0800 | [diff] [blame] | 27 | #include <spi_flash.h> |
Martin Roth | 5856240 | 2015-10-11 10:36:26 +0200 | [diff] [blame] | 28 | #include <spi-generic.h> |
| 29 | |
| 30 | static int ich_status_poll(u16 bitmask, int wait_til_set); |
| 31 | |
| 32 | #ifdef __SMM__ |
Kyösti Mälkki | b4a45dc | 2013-07-26 08:53:59 +0300 | [diff] [blame] | 33 | #include <arch/io.h> |
Martin Roth | 5856240 | 2015-10-11 10:36:26 +0200 | [diff] [blame] | 34 | #define pci_read_config_byte(dev, reg, targ)\ |
| 35 | *(targ) = pci_read_config8(dev, reg) |
| 36 | #define pci_read_config_word(dev, reg, targ)\ |
| 37 | *(targ) = pci_read_config16(dev, reg) |
| 38 | #define pci_read_config_dword(dev, reg, targ)\ |
| 39 | *(targ) = pci_read_config32(dev, reg) |
| 40 | #define pci_write_config_byte(dev, reg, val)\ |
| 41 | pci_write_config8(dev, reg, val) |
| 42 | #define pci_write_config_word(dev, reg, val)\ |
| 43 | pci_write_config16(dev, reg, val) |
| 44 | #define pci_write_config_dword(dev, reg, val)\ |
| 45 | pci_write_config32(dev, reg, val) |
| 46 | #else /* !__SMM__ */ |
| 47 | #include <device/device.h> |
| 48 | #include <device/pci.h> |
| 49 | #define pci_read_config_byte(dev, reg, targ)\ |
| 50 | *(targ) = pci_read_config8(dev, reg) |
| 51 | #define pci_read_config_word(dev, reg, targ)\ |
| 52 | *(targ) = pci_read_config16(dev, reg) |
| 53 | #define pci_read_config_dword(dev, reg, targ)\ |
| 54 | *(targ) = pci_read_config32(dev, reg) |
| 55 | #define pci_write_config_byte(dev, reg, val)\ |
| 56 | pci_write_config8(dev, reg, val) |
| 57 | #define pci_write_config_word(dev, reg, val)\ |
| 58 | pci_write_config16(dev, reg, val) |
| 59 | #define pci_write_config_dword(dev, reg, val)\ |
| 60 | pci_write_config32(dev, reg, val) |
| 61 | #endif /* !__SMM__ */ |
| 62 | |
| 63 | typedef struct spi_slave ich_spi_slave; |
| 64 | |
| 65 | static int ichspi_lock = 0; |
| 66 | |
| 67 | typedef struct ich7_spi_regs { |
| 68 | uint16_t spis; |
| 69 | uint16_t spic; |
| 70 | uint32_t spia; |
| 71 | uint64_t spid[8]; |
| 72 | uint64_t _pad; |
| 73 | uint32_t bbar; |
| 74 | uint16_t preop; |
| 75 | uint16_t optype; |
| 76 | uint8_t opmenu[8]; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 77 | } __packed ich7_spi_regs; |
Martin Roth | 5856240 | 2015-10-11 10:36:26 +0200 | [diff] [blame] | 78 | |
| 79 | typedef struct ich9_spi_regs { |
| 80 | uint32_t bfpr; // 0 |
| 81 | uint16_t hsfs; // 4 |
| 82 | uint16_t hsfc; // 6 |
| 83 | uint32_t faddr; // 8 |
| 84 | uint32_t _reserved0; // 0xC |
| 85 | uint32_t fdata[16]; // 0x10 |
| 86 | uint32_t frap; // 0x50 |
| 87 | uint32_t freg[5]; // 0x54 |
| 88 | uint32_t _reserved1[3]; // 0x67 |
| 89 | uint32_t pr[5]; // 0x74 |
| 90 | uint32_t _reserved2[2]; // 0x88 |
| 91 | uint8_t ssfs; // 0x90 |
| 92 | uint8_t ssfc[3]; // 0x91 |
| 93 | uint16_t preop; // 0x94 |
| 94 | uint16_t optype; // 0x96 |
| 95 | uint8_t opmenu[8]; // 0x98 |
| 96 | uint32_t bbar; // 0xB0 |
| 97 | uint8_t _reserved3[12]; |
| 98 | uint32_t fdoc; |
| 99 | uint32_t fdod; |
| 100 | uint8_t _reserved4[8]; |
| 101 | uint32_t afc; |
| 102 | uint32_t lvscc; |
| 103 | uint32_t uvscc; |
| 104 | uint8_t _reserved5[4]; |
| 105 | uint32_t fpb; |
| 106 | uint8_t _reserved6[28]; |
| 107 | uint32_t srdl; |
| 108 | uint32_t srdc; |
| 109 | uint32_t srd; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 110 | } __packed ich9_spi_regs; |
Martin Roth | 5856240 | 2015-10-11 10:36:26 +0200 | [diff] [blame] | 111 | |
| 112 | typedef struct ich10_spi_regs { |
| 113 | uint32_t bfpr; |
| 114 | uint16_t hsfs; |
| 115 | uint16_t hsfc; |
| 116 | uint32_t faddr; |
| 117 | uint32_t _reserved0; |
| 118 | uint32_t fdata[16]; |
| 119 | uint32_t fracc; |
| 120 | uint32_t freg[5]; |
| 121 | uint32_t _reserved1[3]; |
| 122 | uint32_t pr[5]; |
| 123 | uint32_t _reserved2[2]; |
| 124 | uint8_t ssfs; |
| 125 | uint8_t ssfc[3]; |
| 126 | uint16_t preop; |
| 127 | uint16_t optype; |
| 128 | uint8_t opmenu[8]; |
| 129 | uint8_t _reserved3[16]; |
| 130 | uint32_t fdoc; |
| 131 | uint32_t fdod; |
| 132 | uint8_t _reserved4[8]; |
| 133 | uint32_t afc; |
| 134 | uint32_t lvscc; |
| 135 | uint32_t uvscc; |
| 136 | uint8_t _reserved5[4]; |
| 137 | uint32_t fpb; |
| 138 | uint8_t _reserved6[36]; |
| 139 | uint32_t scs; |
| 140 | uint32_t bcr; |
| 141 | uint32_t tcgc; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 142 | } __packed ich10_spi_regs; |
Martin Roth | 5856240 | 2015-10-11 10:36:26 +0200 | [diff] [blame] | 143 | |
| 144 | typedef struct ich_spi_controller { |
| 145 | int locked; |
| 146 | |
| 147 | uint8_t *opmenu; |
| 148 | int menubytes; |
| 149 | uint16_t *preop; |
| 150 | uint16_t *optype; |
| 151 | uint32_t *addr; |
| 152 | uint8_t *data; |
| 153 | unsigned databytes; |
| 154 | uint8_t *status; |
| 155 | uint16_t *control; |
| 156 | uint32_t *bbar; |
| 157 | uint8_t *bcr; |
| 158 | } ich_spi_controller; |
| 159 | |
| 160 | static ich_spi_controller cntlr; |
| 161 | |
| 162 | enum { |
| 163 | SPIS_SCIP = 0x0001, |
| 164 | SPIS_GRANT = 0x0002, |
| 165 | SPIS_CDS = 0x0004, |
| 166 | SPIS_FCERR = 0x0008, |
| 167 | SSFS_AEL = 0x0010, |
| 168 | SPIS_LOCK = 0x8000, |
| 169 | SPIS_RESERVED_MASK = 0x7ff0, |
| 170 | SSFS_RESERVED_MASK = 0x7fe2 |
| 171 | }; |
| 172 | |
| 173 | enum { |
| 174 | SPIC_SCGO = 0x000002, |
| 175 | SPIC_ACS = 0x000004, |
| 176 | SPIC_SPOP = 0x000008, |
| 177 | SPIC_DBC = 0x003f00, |
| 178 | SPIC_DS = 0x004000, |
| 179 | SPIC_SME = 0x008000, |
| 180 | SSFC_SCF_MASK = 0x070000, |
| 181 | SSFC_RESERVED = 0xf80000 |
| 182 | }; |
| 183 | |
| 184 | enum { |
| 185 | HSFS_FDONE = 0x0001, |
| 186 | HSFS_FCERR = 0x0002, |
| 187 | HSFS_AEL = 0x0004, |
| 188 | HSFS_BERASE_MASK = 0x0018, |
| 189 | HSFS_BERASE_SHIFT = 3, |
| 190 | HSFS_SCIP = 0x0020, |
| 191 | HSFS_FDOPSS = 0x2000, |
| 192 | HSFS_FDV = 0x4000, |
| 193 | HSFS_FLOCKDN = 0x8000 |
| 194 | }; |
| 195 | |
| 196 | enum { |
| 197 | HSFC_FGO = 0x0001, |
| 198 | HSFC_FCYCLE_MASK = 0x0006, |
| 199 | HSFC_FCYCLE_SHIFT = 1, |
| 200 | HSFC_FDBC_MASK = 0x3f00, |
| 201 | HSFC_FDBC_SHIFT = 8, |
| 202 | HSFC_FSMIE = 0x8000 |
| 203 | }; |
| 204 | |
| 205 | enum { |
| 206 | BCR_BIOSWE = 0x0001, |
| 207 | BCR_BLE = 0x0002, |
| 208 | BCR_SRC_MASK = 0x000c, |
| 209 | BCR_SRC_SHIFT = 0x0002, |
| 210 | BCR_SRC_NO_PREF = 0x0000, |
| 211 | BCR_SRC_NO_PREF_CACHE = 0x0004, |
| 212 | BCR_SRC_EN_PREF_CACHE = 0x0008, |
| 213 | BCR_TSS = 0x0010, |
| 214 | BCR_SMMBWP = 0x0020, |
| 215 | BCR_RESERVED_MASK = 0xffc0 |
| 216 | }; |
| 217 | |
| 218 | enum { |
| 219 | SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0, |
| 220 | SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1, |
| 221 | SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2, |
| 222 | SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3, |
| 223 | }; |
| 224 | |
| 225 | #if IS_ENABLED(CONFIG_DEBUG_SPI_FLASH) |
| 226 | |
| 227 | static u8 readb_(const void *addr) |
| 228 | { |
| 229 | u8 v = read8(addr); |
| 230 | printk(BIOS_DEBUG, "read %2.2x from %4.4x\n", |
| 231 | v, ((unsigned) addr & 0xffff) - 0xf020); |
| 232 | return v; |
| 233 | } |
| 234 | |
| 235 | static u16 readw_(const void *addr) |
| 236 | { |
| 237 | u16 v = read16(addr); |
| 238 | printk(BIOS_DEBUG, "read %4.4x from %4.4x\n", |
| 239 | v, ((unsigned) addr & 0xffff) - 0xf020); |
| 240 | return v; |
| 241 | } |
| 242 | |
| 243 | static u32 readl_(const void *addr) |
| 244 | { |
| 245 | u32 v = read32(addr); |
| 246 | printk(BIOS_DEBUG, "read %8.8x from %4.4x\n", |
| 247 | v, ((unsigned) addr & 0xffff) - 0xf020); |
| 248 | return v; |
| 249 | } |
| 250 | |
| 251 | static void writeb_(u8 b, const void *addr) |
| 252 | { |
| 253 | write8(addr, b); |
| 254 | printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n", |
| 255 | b, ((unsigned) addr & 0xffff) - 0xf020); |
| 256 | } |
| 257 | |
| 258 | static void writew_(u16 b, const void *addr) |
| 259 | { |
| 260 | write16(addr, b); |
| 261 | printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n", |
| 262 | b, ((unsigned) addr & 0xffff) - 0xf020); |
| 263 | } |
| 264 | |
| 265 | static void writel_(u32 b, const void *addr) |
| 266 | { |
| 267 | write32((unsigned long)addr, b); |
| 268 | printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n", |
| 269 | b, ((unsigned) addr & 0xffff) - 0xf020); |
| 270 | } |
| 271 | |
| 272 | #else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */ |
| 273 | |
| 274 | #define readb_(a) read8(a) |
| 275 | #define readw_(a) read16(a) |
| 276 | #define readl_(a) read32(a) |
| 277 | #define writeb_(val, addr) write8(addr, val) |
| 278 | #define writew_(val, addr) write16(addr, val) |
| 279 | #define writel_(val, addr) write32(addr, val) |
| 280 | |
| 281 | #endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */ |
| 282 | |
| 283 | static void write_reg(const void *value, void *dest, uint32_t size) |
| 284 | { |
| 285 | const uint8_t *bvalue = value; |
| 286 | uint8_t *bdest = dest; |
| 287 | |
| 288 | while (size >= 4) { |
| 289 | writel_(*(const uint32_t *)bvalue, bdest); |
| 290 | bdest += 4; bvalue += 4; size -= 4; |
| 291 | } |
| 292 | while (size) { |
| 293 | writeb_(*bvalue, bdest); |
| 294 | bdest++; bvalue++; size--; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | static void read_reg(const void *src, void *value, uint32_t size) |
| 299 | { |
| 300 | const uint8_t *bsrc = src; |
| 301 | uint8_t *bvalue = value; |
| 302 | |
| 303 | while (size >= 4) { |
| 304 | *(uint32_t *)bvalue = readl_(bsrc); |
| 305 | bsrc += 4; bvalue += 4; size -= 4; |
| 306 | } |
| 307 | while (size) { |
| 308 | *bvalue = readb_(bsrc); |
| 309 | bsrc++; bvalue++; size--; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | static void ich_set_bbar(uint32_t minaddr) |
| 314 | { |
| 315 | const uint32_t bbar_mask = 0x00ffff00; |
| 316 | uint32_t ichspi_bbar; |
| 317 | |
| 318 | if (cntlr.bbar == NULL) |
| 319 | return; |
| 320 | |
| 321 | minaddr &= bbar_mask; |
| 322 | ichspi_bbar = readl_(cntlr.bbar) & ~bbar_mask; |
| 323 | ichspi_bbar |= minaddr; |
| 324 | writel_(ichspi_bbar, cntlr.bbar); |
| 325 | } |
| 326 | |
Martin Roth | 5856240 | 2015-10-11 10:36:26 +0200 | [diff] [blame] | 327 | /* |
| 328 | * Check if this device ID matches one of supported Intel SOC devices. |
| 329 | * |
| 330 | * Return the ICH version if there is a match, or zero otherwise. |
| 331 | */ |
| 332 | static inline int get_ich_version(uint16_t device_id) |
| 333 | { |
| 334 | |
| 335 | if ((device_id >= PCI_DEVICE_ID_INTEL_RANGELEY_LPC_MIN && |
| 336 | device_id <= PCI_DEVICE_ID_INTEL_RANGELEY_LPC_MAX)) |
| 337 | return 10; |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | void spi_init(void) |
| 343 | { |
| 344 | int ich_version = 0; |
| 345 | uint8_t bios_cntl; |
Elyes HAOUAS | cbcdb3e | 2018-05-13 13:22:58 +0200 | [diff] [blame] | 346 | struct device *dev; |
Martin Roth | 5856240 | 2015-10-11 10:36:26 +0200 | [diff] [blame] | 347 | uint32_t ids; |
| 348 | uint16_t vendor_id, device_id; |
| 349 | |
| 350 | #ifdef __SMM__ |
| 351 | dev = PCI_DEV(0, 31, 0); |
| 352 | #else |
| 353 | dev = dev_find_slot(0, PCI_DEVFN(31, 0)); |
| 354 | #endif |
| 355 | pci_read_config_dword(dev, 0, &ids); |
| 356 | vendor_id = ids; |
| 357 | device_id = (ids >> 16); |
| 358 | |
| 359 | if (vendor_id != PCI_VENDOR_ID_INTEL) { |
| 360 | printk(BIOS_DEBUG, "SPI: No SOC found.\n"); |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | ich_version = get_ich_version(device_id); |
| 365 | |
| 366 | if (!ich_version) { |
| 367 | printk(BIOS_DEBUG, "SPI: No known SOC found.\n"); |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | switch (ich_version) { |
| 372 | case 10: |
| 373 | { |
| 374 | uint8_t *spibase; /* SPI Base Address */ |
| 375 | uint32_t sbase; /* SPI Base Address Register */ |
| 376 | pci_read_config_dword(dev, 0x54, &sbase); |
| 377 | /* Bits 31-9 are the base address, 8-4 are reserved, 3-0 are used. */ |
| 378 | spibase = (uint8_t *)(sbase & 0xffffff00); |
| 379 | ich10_spi_regs *ich10_spi = |
| 380 | (ich10_spi_regs *)(spibase); |
| 381 | ichspi_lock = readw_(&ich10_spi->hsfs) & HSFS_FLOCKDN; |
| 382 | cntlr.opmenu = ich10_spi->opmenu; |
| 383 | cntlr.menubytes = sizeof(ich10_spi->opmenu); |
| 384 | cntlr.optype = &ich10_spi->optype; |
| 385 | cntlr.addr = &ich10_spi->faddr; |
| 386 | cntlr.data = (uint8_t *)ich10_spi->fdata; |
| 387 | cntlr.databytes = sizeof(ich10_spi->fdata); |
| 388 | cntlr.status = &ich10_spi->ssfs; |
| 389 | cntlr.control = (uint16_t *)ich10_spi->ssfc; |
| 390 | cntlr.bbar = NULL; |
| 391 | cntlr.preop = &ich10_spi->preop; |
| 392 | cntlr.bcr = (uint8_t *)&ich10_spi->bcr; |
| 393 | break; |
| 394 | } |
| 395 | default: |
| 396 | printk(BIOS_DEBUG, "ICH SPI: Unrecognized ICH version %d.\n", ich_version); |
| 397 | } |
| 398 | |
| 399 | ich_set_bbar(0); |
| 400 | |
| 401 | /* Disable the BIOS write protect so write commands are allowed. */ |
| 402 | switch (ich_version) { |
| 403 | case 10: |
| 404 | { |
| 405 | /* Deassert SMM BIOS write protect(SMM BWP) and assert enable flash write(BIOSWE) */ |
| 406 | bios_cntl = readb_(cntlr.bcr); |
| 407 | bios_cntl &= ~BCR_SMMBWP; |
| 408 | bios_cntl |= BCR_BIOSWE; |
| 409 | writeb_(bios_cntl, cntlr.bcr); |
| 410 | break; |
| 411 | } |
| 412 | |
| 413 | default: |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | |
Martin Roth | 5856240 | 2015-10-11 10:36:26 +0200 | [diff] [blame] | 418 | typedef struct spi_transaction { |
| 419 | const uint8_t *out; |
| 420 | uint32_t bytesout; |
| 421 | uint8_t *in; |
| 422 | uint32_t bytesin; |
| 423 | uint8_t type; |
| 424 | uint8_t opcode; |
| 425 | uint32_t offset; |
| 426 | } spi_transaction; |
| 427 | |
| 428 | static inline void spi_use_out(spi_transaction *trans, unsigned bytes) |
| 429 | { |
| 430 | trans->out += bytes; |
| 431 | trans->bytesout -= bytes; |
| 432 | } |
| 433 | |
| 434 | static inline void spi_use_in(spi_transaction *trans, unsigned bytes) |
| 435 | { |
| 436 | trans->in += bytes; |
| 437 | trans->bytesin -= bytes; |
| 438 | } |
| 439 | |
| 440 | static void spi_setup_type(spi_transaction *trans) |
| 441 | { |
| 442 | trans->type = 0xFF; |
| 443 | |
| 444 | /* Try to guess spi type from read/write sizes. */ |
| 445 | if (trans->bytesin == 0) { |
| 446 | if (trans->bytesout > 4) |
| 447 | /* |
| 448 | * If bytesin = 0 and bytesout > 4, we presume this is |
| 449 | * a write data operation, which is accompanied by an |
| 450 | * address. |
| 451 | */ |
| 452 | trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS; |
| 453 | else |
| 454 | trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS; |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | if (trans->bytesout == 1) { /* and bytesin is > 0 */ |
| 459 | trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS; |
| 460 | return; |
| 461 | } |
| 462 | |
| 463 | if (trans->bytesout == 4) { /* and bytesin is > 0 */ |
| 464 | trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS; |
| 465 | } |
| 466 | |
| 467 | /* Fast read command is called with 5 bytes instead of 4 */ |
| 468 | if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) { |
| 469 | trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS; |
| 470 | --trans->bytesout; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | static int spi_setup_opcode(spi_transaction *trans) |
| 475 | { |
| 476 | uint16_t optypes; |
| 477 | uint8_t opmenu[cntlr.menubytes]; |
| 478 | |
| 479 | trans->opcode = trans->out[0]; |
| 480 | spi_use_out(trans, 1); |
| 481 | if (!ichspi_lock) { |
| 482 | /* The lock is off, so just use index 0. */ |
| 483 | writeb_(trans->opcode, cntlr.opmenu); |
| 484 | optypes = readw_(cntlr.optype); |
| 485 | optypes = (optypes & 0xfffc) | (trans->type & 0x3); |
| 486 | writew_(optypes, cntlr.optype); |
| 487 | return 0; |
| 488 | } else { |
| 489 | /* The lock is on. See if what we need is on the menu. */ |
| 490 | uint8_t optype; |
| 491 | uint16_t opcode_index; |
| 492 | |
| 493 | /* Write Enable is handled as atomic prefix */ |
| 494 | if (trans->opcode == SPI_OPCODE_WREN) |
| 495 | return 0; |
| 496 | |
| 497 | read_reg(cntlr.opmenu, opmenu, sizeof(opmenu)); |
| 498 | for (opcode_index = 0; opcode_index < cntlr.menubytes; |
| 499 | opcode_index++) { |
| 500 | if (opmenu[opcode_index] == trans->opcode) |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | if (opcode_index == cntlr.menubytes) { |
| 505 | printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n", |
| 506 | trans->opcode); |
| 507 | return -1; |
| 508 | } |
| 509 | |
| 510 | optypes = readw_(cntlr.optype); |
| 511 | optype = (optypes >> (opcode_index * 2)) & 0x3; |
| 512 | if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS && |
| 513 | optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS && |
| 514 | trans->bytesout >= 3) { |
| 515 | /* We guessed wrong earlier. Fix it up. */ |
| 516 | trans->type = optype; |
| 517 | } |
| 518 | if (optype != trans->type) { |
| 519 | printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n", |
| 520 | optype); |
| 521 | return -1; |
| 522 | } |
| 523 | return opcode_index; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | static int spi_setup_offset(spi_transaction *trans) |
| 528 | { |
| 529 | /* Separate the SPI address and data. */ |
| 530 | switch (trans->type) { |
| 531 | case SPI_OPCODE_TYPE_READ_NO_ADDRESS: |
| 532 | case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS: |
| 533 | return 0; |
| 534 | case SPI_OPCODE_TYPE_READ_WITH_ADDRESS: |
| 535 | case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS: |
| 536 | trans->offset = ((uint32_t)trans->out[0] << 16) | |
| 537 | ((uint32_t)trans->out[1] << 8) | |
| 538 | ((uint32_t)trans->out[2] << 0); |
| 539 | spi_use_out(trans, 3); |
| 540 | return 1; |
| 541 | default: |
| 542 | printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type); |
| 543 | return -1; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * Wait for up to 60ms til status register bit(s) turn 1 (in case wait_til_set |
| 549 | * below is True) or 0. In case the wait was for the bit(s) to set - write |
| 550 | * those bits back, which would cause resetting them. |
| 551 | * |
| 552 | * Return the last read status value on success or -1 on failure. |
| 553 | */ |
| 554 | static int ich_status_poll(u16 bitmask, int wait_til_set) |
| 555 | { |
| 556 | int timeout = 60000; /* This will result in 600 ms */ |
| 557 | u16 status = 0; |
| 558 | |
| 559 | while (timeout--) { |
| 560 | status = readw_(cntlr.status); |
| 561 | if (wait_til_set ^ ((status & bitmask) == 0)) { |
| 562 | if (wait_til_set) |
| 563 | writew_((status & bitmask), cntlr.status); |
| 564 | return status; |
| 565 | } |
| 566 | udelay(10); |
| 567 | } |
| 568 | |
| 569 | printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, expected %x\n", |
| 570 | status, bitmask); |
| 571 | return -1; |
| 572 | } |
| 573 | |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 574 | 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] | 575 | size_t bytesout, void *din, size_t bytesin) |
Martin Roth | 5856240 | 2015-10-11 10:36:26 +0200 | [diff] [blame] | 576 | { |
| 577 | uint16_t control; |
| 578 | int16_t opcode_index; |
| 579 | int with_address; |
| 580 | int status; |
| 581 | |
| 582 | spi_transaction trans = { |
| 583 | dout, bytesout, |
| 584 | din, bytesin, |
| 585 | 0xff, 0xff, 0 |
| 586 | }; |
| 587 | |
| 588 | /* There has to always at least be an opcode. */ |
| 589 | if (!bytesout || !dout) { |
| 590 | printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n"); |
| 591 | return -1; |
| 592 | } |
| 593 | /* Make sure if we read something we have a place to put it. */ |
| 594 | if (bytesin != 0 && !din) { |
| 595 | printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n"); |
| 596 | return -1; |
| 597 | } |
| 598 | |
| 599 | if (ich_status_poll(SPIS_SCIP, 0) == -1) |
| 600 | return -1; |
| 601 | |
| 602 | writew_(SPIS_CDS | SPIS_FCERR, cntlr.status); |
| 603 | |
| 604 | spi_setup_type(&trans); |
| 605 | if ((opcode_index = spi_setup_opcode(&trans)) < 0) |
| 606 | return -1; |
| 607 | if ((with_address = spi_setup_offset(&trans)) < 0) |
| 608 | return -1; |
| 609 | |
| 610 | if (!ichspi_lock && trans.opcode == SPI_OPCODE_WREN) { |
| 611 | /* |
| 612 | * Treat Write Enable as Atomic Pre-Op if possible |
| 613 | * in order to prevent the Management Engine from |
| 614 | * issuing a transaction between WREN and DATA. |
| 615 | */ |
| 616 | writew_(trans.opcode, cntlr.preop); |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | /* Preset control fields */ |
| 621 | control = SPIC_SCGO | ((opcode_index & 0x07) << 4); |
| 622 | |
| 623 | /* Issue atomic preop cycle if needed */ |
| 624 | if (readw_(cntlr.preop)) |
| 625 | control |= SPIC_ACS; |
| 626 | |
| 627 | if (!trans.bytesout && !trans.bytesin) { |
| 628 | /* SPI addresses are 24 bit only */ |
| 629 | if (with_address) |
| 630 | writel_(trans.offset & 0x00FFFFFF, cntlr.addr); |
| 631 | |
| 632 | /* |
| 633 | * This is a 'no data' command (like Write Enable), its |
| 634 | * bitesout size was 1, decremented to zero while executing |
| 635 | * spi_setup_opcode() above. Tell the chip to send the |
| 636 | * command. |
| 637 | */ |
| 638 | writew_(control, cntlr.control); |
| 639 | |
| 640 | /* wait for the result */ |
| 641 | status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1); |
| 642 | if (status == -1) |
| 643 | return -1; |
| 644 | |
| 645 | if (status & SPIS_FCERR) { |
| 646 | printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n"); |
| 647 | return -1; |
| 648 | } |
| 649 | |
| 650 | goto spi_xfer_exit; |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * Check if this is a write command attempting to transfer more bytes |
| 655 | * than the controller can handle. Iterations for writes are not |
| 656 | * supported here because each SPI write command needs to be preceded |
| 657 | * and followed by other SPI commands, and this sequence is controlled |
| 658 | * by the SPI chip driver. |
| 659 | */ |
| 660 | if (trans.bytesout > cntlr.databytes) { |
| 661 | printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use" |
| 662 | " spi_crop_chunk()?\n"); |
| 663 | return -1; |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | * Read or write up to databytes bytes at a time until everything has |
| 668 | * been sent. |
| 669 | */ |
| 670 | while (trans.bytesout || trans.bytesin) { |
| 671 | uint32_t data_length; |
| 672 | |
| 673 | /* SPI addresses are 24 bit only */ |
| 674 | writel_(trans.offset & 0x00FFFFFF, cntlr.addr); |
| 675 | |
| 676 | if (trans.bytesout) |
| 677 | data_length = min(trans.bytesout, cntlr.databytes); |
| 678 | else |
| 679 | data_length = min(trans.bytesin, cntlr.databytes); |
| 680 | |
| 681 | /* Program data into FDATA0 to N */ |
| 682 | if (trans.bytesout) { |
| 683 | write_reg(trans.out, cntlr.data, data_length); |
| 684 | spi_use_out(&trans, data_length); |
| 685 | if (with_address) |
| 686 | trans.offset += data_length; |
| 687 | } |
| 688 | |
| 689 | /* Add proper control fields' values */ |
| 690 | control &= ~((cntlr.databytes - 1) << 8); |
| 691 | control |= SPIC_DS; |
| 692 | control |= (data_length - 1) << 8; |
| 693 | |
| 694 | /* write it */ |
| 695 | writew_(control, cntlr.control); |
| 696 | |
| 697 | /* Wait for Cycle Done Status or Flash Cycle Error. */ |
| 698 | status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1); |
| 699 | if (status == -1) |
| 700 | return -1; |
| 701 | |
| 702 | if (status & SPIS_FCERR) { |
| 703 | printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n"); |
| 704 | return -1; |
| 705 | } |
| 706 | |
| 707 | if (trans.bytesin) { |
| 708 | read_reg(cntlr.data, trans.in, data_length); |
| 709 | spi_use_in(&trans, data_length); |
| 710 | if (with_address) |
| 711 | trans.offset += data_length; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | spi_xfer_exit: |
| 716 | /* Clear atomic preop now that xfer is done */ |
| 717 | writew_(0, cntlr.preop); |
| 718 | |
| 719 | return 0; |
| 720 | } |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 721 | |
Aaron Durbin | 851dde8 | 2018-04-19 21:15:25 -0600 | [diff] [blame] | 722 | static int xfer_vectors(const struct spi_slave *slave, |
| 723 | struct spi_op vectors[], size_t count) |
| 724 | { |
| 725 | return spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer); |
| 726 | } |
| 727 | |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 728 | static const struct spi_ctrlr spi_ctrlr = { |
Aaron Durbin | 851dde8 | 2018-04-19 21:15:25 -0600 | [diff] [blame] | 729 | .xfer_vector = xfer_vectors, |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 730 | .max_xfer_size = member_size(ich10_spi_regs, fdata), |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 731 | }; |
| 732 | |
Furquan Shaikh | 2cd03f1 | 2017-05-18 14:58:32 -0700 | [diff] [blame] | 733 | const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = { |
| 734 | { |
| 735 | .ctrlr = &spi_ctrlr, |
| 736 | .bus_start = 0, |
| 737 | .bus_end = 0, |
| 738 | }, |
| 739 | }; |
| 740 | |
| 741 | const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map); |