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