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