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