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