blob: 4926df9d500b103382367523e35435193459a823 [file] [log] [blame]
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +01003 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
4 * Copyright (C) 2011 Stefan Tauner
Werner Zehf13a6f92018-11-14 10:55:52 +01005 * Copyright (C) 2018 Siemens AG
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07006 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07007 * 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 Reinauer1c56d9b2012-05-10 11:27:32 -070016 */
17
18/* This file is derived from the flashrom project. */
Matt DeVillier4721e472019-05-18 16:05:00 -050019#include <arch/early_variables.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070020#include <stdint.h>
21#include <stdlib.h>
22#include <string.h>
David Hendricksf2612a12014-04-13 16:27:02 -070023#include <bootstate.h>
Furquan Shaikhde705fa2017-04-19 19:27:28 -070024#include <commonlib/helpers.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070025#include <delay.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020026#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020027#include <device/pci_ops.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070028#include <console/console.h>
Kyösti Mälkki7ba14402019-02-07 12:44:00 +020029#include <device/device.h>
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010030#include <device/pci.h>
31#include <spi_flash.h>
Zheng Bao600784e2013-02-07 17:30:23 +080032#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070033
Arthur Heymans92185e32019-05-28 13:06:34 +020034#include "spi.h"
35
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010036#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
37#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
38#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
39#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
40
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010041static int spi_is_multichip(void);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010042
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020043struct ich7_spi_regs {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070044 uint16_t spis;
45 uint16_t spic;
46 uint32_t spia;
47 uint64_t spid[8];
48 uint64_t _pad;
49 uint32_t bbar;
50 uint16_t preop;
51 uint16_t optype;
52 uint8_t opmenu[8];
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +010053 uint32_t pbr[3];
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020054} __packed;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070055
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020056struct ich9_spi_regs {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070057 uint32_t bfpr;
58 uint16_t hsfs;
59 uint16_t hsfc;
60 uint32_t faddr;
61 uint32_t _reserved0;
62 uint32_t fdata[16];
63 uint32_t frap;
64 uint32_t freg[5];
65 uint32_t _reserved1[3];
66 uint32_t pr[5];
67 uint32_t _reserved2[2];
68 uint8_t ssfs;
69 uint8_t ssfc[3];
70 uint16_t preop;
71 uint16_t optype;
72 uint8_t opmenu[8];
73 uint32_t bbar;
74 uint8_t _reserved3[12];
75 uint32_t fdoc;
76 uint32_t fdod;
77 uint8_t _reserved4[8];
78 uint32_t afc;
79 uint32_t lvscc;
80 uint32_t uvscc;
81 uint8_t _reserved5[4];
82 uint32_t fpb;
83 uint8_t _reserved6[28];
84 uint32_t srdl;
85 uint32_t srdc;
86 uint32_t srd;
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020087} __packed;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070088
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020089struct ich_spi_controller {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070090 int locked;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010091 uint32_t flmap0;
Stefan Tauner327205d2018-08-26 13:53:16 +020092 uint32_t flcomp;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010093 uint32_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070094
Arthur Heymans21c5d432019-06-15 18:23:29 +020095 union {
96 struct ich9_spi_regs *ich9_spi;
97 struct ich7_spi_regs *ich7_spi;
98 };
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070099 uint8_t *opmenu;
100 int menubytes;
101 uint16_t *preop;
102 uint16_t *optype;
103 uint32_t *addr;
104 uint8_t *data;
Martin Rothff744bf2019-10-23 21:46:03 -0600105 unsigned int databytes;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700106 uint8_t *status;
107 uint16_t *control;
108 uint32_t *bbar;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100109 uint32_t *fpr;
110 uint8_t fpr_max;
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200111};
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700112
Matt DeVillier4721e472019-05-18 16:05:00 -0500113static struct ich_spi_controller g_cntlr CAR_GLOBAL;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700114
115enum {
116 SPIS_SCIP = 0x0001,
117 SPIS_GRANT = 0x0002,
118 SPIS_CDS = 0x0004,
119 SPIS_FCERR = 0x0008,
120 SSFS_AEL = 0x0010,
121 SPIS_LOCK = 0x8000,
122 SPIS_RESERVED_MASK = 0x7ff0,
123 SSFS_RESERVED_MASK = 0x7fe2
124};
125
126enum {
127 SPIC_SCGO = 0x000002,
128 SPIC_ACS = 0x000004,
129 SPIC_SPOP = 0x000008,
130 SPIC_DBC = 0x003f00,
131 SPIC_DS = 0x004000,
132 SPIC_SME = 0x008000,
133 SSFC_SCF_MASK = 0x070000,
134 SSFC_RESERVED = 0xf80000
135};
136
137enum {
138 HSFS_FDONE = 0x0001,
139 HSFS_FCERR = 0x0002,
140 HSFS_AEL = 0x0004,
141 HSFS_BERASE_MASK = 0x0018,
142 HSFS_BERASE_SHIFT = 3,
143 HSFS_SCIP = 0x0020,
144 HSFS_FDOPSS = 0x2000,
145 HSFS_FDV = 0x4000,
146 HSFS_FLOCKDN = 0x8000
147};
148
149enum {
150 HSFC_FGO = 0x0001,
151 HSFC_FCYCLE_MASK = 0x0006,
152 HSFC_FCYCLE_SHIFT = 1,
153 HSFC_FDBC_MASK = 0x3f00,
154 HSFC_FDBC_SHIFT = 8,
155 HSFC_FSMIE = 0x8000
156};
157
158enum {
159 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
160 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
161 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
162 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
163};
164
Julius Wernercd49cce2019-03-05 16:53:33 -0800165#if CONFIG(DEBUG_SPI_FLASH)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700166
167static u8 readb_(const void *addr)
168{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800169 u8 v = read8(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100170
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700171 printk(BIOS_DEBUG, "read %2.2x from %4.4x\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600172 v, ((unsigned int) addr & 0xffff) - 0xf020);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700173 return v;
174}
175
176static u16 readw_(const void *addr)
177{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800178 u16 v = read16(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100179
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700180 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600181 v, ((unsigned int) addr & 0xffff) - 0xf020);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700182 return v;
183}
184
185static u32 readl_(const void *addr)
186{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800187 u32 v = read32(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100188
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700189 printk(BIOS_DEBUG, "read %8.8x from %4.4x\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600190 v, ((unsigned int) addr & 0xffff) - 0xf020);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700191 return v;
192}
193
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800194static void writeb_(u8 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700195{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800196 write8(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700197 printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600198 b, ((unsigned int) addr & 0xffff) - 0xf020);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700199}
200
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800201static void writew_(u16 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700202{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800203 write16(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700204 printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600205 b, ((unsigned int) addr & 0xffff) - 0xf020);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700206}
207
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800208static void writel_(u32 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700209{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800210 write32(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700211 printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600212 b, ((unsigned int) addr & 0xffff) - 0xf020);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700213}
214
215#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
216
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800217#define readb_(a) read8(a)
218#define readw_(a) read16(a)
219#define readl_(a) read32(a)
220#define writeb_(val, addr) write8(addr, val)
221#define writew_(val, addr) write16(addr, val)
222#define writel_(val, addr) write32(addr, val)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700223
224#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
225
226static void write_reg(const void *value, void *dest, uint32_t size)
227{
228 const uint8_t *bvalue = value;
229 uint8_t *bdest = dest;
230
231 while (size >= 4) {
232 writel_(*(const uint32_t *)bvalue, bdest);
233 bdest += 4; bvalue += 4; size -= 4;
234 }
235 while (size) {
236 writeb_(*bvalue, bdest);
237 bdest++; bvalue++; size--;
238 }
239}
240
241static void read_reg(const void *src, void *value, uint32_t size)
242{
243 const uint8_t *bsrc = src;
244 uint8_t *bvalue = value;
245
246 while (size >= 4) {
247 *(uint32_t *)bvalue = readl_(bsrc);
248 bsrc += 4; bvalue += 4; size -= 4;
249 }
250 while (size) {
251 *bvalue = readb_(bsrc);
252 bsrc++; bvalue++; size--;
253 }
254}
255
256static void ich_set_bbar(uint32_t minaddr)
257{
Matt DeVillier4721e472019-05-18 16:05:00 -0500258 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700259 const uint32_t bbar_mask = 0x00ffff00;
260 uint32_t ichspi_bbar;
261
262 minaddr &= bbar_mask;
Arthur Heymans02c99712018-03-28 18:49:27 +0200263 ichspi_bbar = readl_(cntlr->bbar) & ~bbar_mask;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700264 ichspi_bbar |= minaddr;
Arthur Heymans02c99712018-03-28 18:49:27 +0200265 writel_(ichspi_bbar, cntlr->bbar);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700266}
267
Jacob Garber9172b692019-06-26 16:18:16 -0600268#if CONFIG(SOUTHBRIDGE_INTEL_I82801GX)
269#define MENU_BYTES member_size(struct ich7_spi_regs, opmenu)
270#else
271#define MENU_BYTES member_size(struct ich9_spi_regs, opmenu)
272#endif
273
Arthur Heymans47a66032019-10-25 23:43:14 +0200274#define RCBA 0xf0
275#define SBASE 0x54
276
277#ifdef __SIMPLE_DEVICE__
278static void *get_spi_bar(pci_devfn_t dev)
279#else
280static void *get_spi_bar(struct device *dev)
281#endif
282{
283 uintptr_t rcba; /* Root Complex Register Block */
284 uintptr_t sbase;
285
286 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) {
287 rcba = pci_read_config32(dev, RCBA);
288 return (void *)((rcba & 0xffffc000) + 0x3020);
289 }
290 if (CONFIG(SOUTHBRIDGE_INTEL_COMMON_SPI_SILVERMONT)) {
291 sbase = pci_read_config32(dev, SBASE);
292 sbase &= ~0x1ff;
293 return (void *)sbase;
294 }
295 if (CONFIG(SOUTHBRIDGE_INTEL_COMMON_SPI_ICH9)) {
296 rcba = pci_read_config32(dev, RCBA);
297 return (void *)((rcba & 0xffffc000) + 0x3800);
298 }
299}
300
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700301void spi_init(void)
302{
Matt DeVillier4721e472019-05-18 16:05:00 -0500303 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700304 uint8_t bios_cntl;
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200305 struct ich9_spi_regs *ich9_spi;
306 struct ich7_spi_regs *ich7_spi;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100307 uint16_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700308
Arthur Heymans02c99712018-03-28 18:49:27 +0200309#ifdef __SIMPLE_DEVICE__
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200310 pci_devfn_t dev = PCI_DEV(0, 31, 0);
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700311#else
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300312 struct device *dev = pcidev_on_root(31, 0);
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700313#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700314
Julius Wernercd49cce2019-03-05 16:53:33 -0800315 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) {
Arthur Heymans47a66032019-10-25 23:43:14 +0200316 ich7_spi = get_spi_bar(dev);
Arthur Heymans21c5d432019-06-15 18:23:29 +0200317 cntlr->ich7_spi = ich7_spi;
Arthur Heymans02c99712018-03-28 18:49:27 +0200318 cntlr->opmenu = ich7_spi->opmenu;
319 cntlr->menubytes = sizeof(ich7_spi->opmenu);
320 cntlr->optype = &ich7_spi->optype;
321 cntlr->addr = &ich7_spi->spia;
322 cntlr->data = (uint8_t *)ich7_spi->spid;
323 cntlr->databytes = sizeof(ich7_spi->spid);
324 cntlr->status = (uint8_t *)&ich7_spi->spis;
Arthur Heymans02c99712018-03-28 18:49:27 +0200325 cntlr->control = &ich7_spi->spic;
326 cntlr->bbar = &ich7_spi->bbar;
327 cntlr->preop = &ich7_spi->preop;
328 cntlr->fpr = &ich7_spi->pbr[0];
329 cntlr->fpr_max = 3;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200330 } else {
Arthur Heymans47a66032019-10-25 23:43:14 +0200331 ich9_spi = get_spi_bar(dev);
Arthur Heymans02c99712018-03-28 18:49:27 +0200332 cntlr->ich9_spi = ich9_spi;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200333 hsfs = readw_(&ich9_spi->hsfs);
Arthur Heymans02c99712018-03-28 18:49:27 +0200334 cntlr->hsfs = hsfs;
335 cntlr->opmenu = ich9_spi->opmenu;
336 cntlr->menubytes = sizeof(ich9_spi->opmenu);
337 cntlr->optype = &ich9_spi->optype;
338 cntlr->addr = &ich9_spi->faddr;
339 cntlr->data = (uint8_t *)ich9_spi->fdata;
340 cntlr->databytes = sizeof(ich9_spi->fdata);
341 cntlr->status = &ich9_spi->ssfs;
342 cntlr->control = (uint16_t *)ich9_spi->ssfc;
343 cntlr->bbar = &ich9_spi->bbar;
344 cntlr->preop = &ich9_spi->preop;
345 cntlr->fpr = &ich9_spi->pr[0];
346 cntlr->fpr_max = 5;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700347
Arthur Heymans02c99712018-03-28 18:49:27 +0200348 if (cntlr->hsfs & HSFS_FDV) {
Patrick Georgic88828d2018-11-26 10:42:59 +0100349 writel_(4, &ich9_spi->fdoc);
Arthur Heymans02c99712018-03-28 18:49:27 +0200350 cntlr->flmap0 = readl_(&ich9_spi->fdod);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100351 writel_(0x1000, &ich9_spi->fdoc);
Stefan Tauner327205d2018-08-26 13:53:16 +0200352 cntlr->flcomp = readl_(&ich9_spi->fdod);
Arthur Heymansc88e3702017-08-20 20:50:17 +0200353 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700354 }
355
356 ich_set_bbar(0);
357
Arthur Heymans47a66032019-10-25 23:43:14 +0200358 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX) || CONFIG(SOUTHBRIDGE_INTEL_COMMON_SPI_ICH9)) {
359 /* Disable the BIOS write protect so write commands are allowed. */
360 bios_cntl = pci_read_config8(dev, 0xdc);
361 /* Deassert SMM BIOS Write Protect Disable. */
362 bios_cntl &= ~(1 << 5);
363 pci_write_config8(dev, 0xdc, bios_cntl | 0x1);
364 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700365}
Aaron Durbin4d3de7e2015-09-02 17:34:04 -0500366
Arthur Heymans816aaba2019-06-11 11:10:25 +0200367static int spi_locked(void)
368{
369 struct ich_spi_controller *cntlr = &g_cntlr;
370 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) {
371 return !!(readw_(&cntlr->ich7_spi->spis) & HSFS_FLOCKDN);
372 } else {
Jacob Garber36749742019-07-02 11:08:53 -0600373 return !!(readw_(&cntlr->ich9_spi->hsfs) & HSFS_FLOCKDN);
Arthur Heymans816aaba2019-06-11 11:10:25 +0200374 }
375}
376
David Hendricksf2612a12014-04-13 16:27:02 -0700377static void spi_init_cb(void *unused)
378{
379 spi_init();
380}
381
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500382BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700383
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700384typedef struct spi_transaction {
385 const uint8_t *out;
386 uint32_t bytesout;
387 uint8_t *in;
388 uint32_t bytesin;
389 uint8_t type;
390 uint8_t opcode;
391 uint32_t offset;
392} spi_transaction;
393
Martin Rothff744bf2019-10-23 21:46:03 -0600394static inline void spi_use_out(spi_transaction *trans, unsigned int bytes)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700395{
396 trans->out += bytes;
397 trans->bytesout -= bytes;
398}
399
Martin Rothff744bf2019-10-23 21:46:03 -0600400static inline void spi_use_in(spi_transaction *trans, unsigned int bytes)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700401{
402 trans->in += bytes;
403 trans->bytesin -= bytes;
404}
405
406static void spi_setup_type(spi_transaction *trans)
407{
408 trans->type = 0xFF;
409
410 /* Try to guess spi type from read/write sizes. */
411 if (trans->bytesin == 0) {
412 if (trans->bytesout > 4)
413 /*
414 * If bytesin = 0 and bytesout > 4, we presume this is
415 * a write data operation, which is accompanied by an
416 * address.
417 */
418 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
419 else
420 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
421 return;
422 }
423
424 if (trans->bytesout == 1) { /* and bytesin is > 0 */
425 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
426 return;
427 }
428
429 if (trans->bytesout == 4) { /* and bytesin is > 0 */
430 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
431 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700432
433 /* Fast read command is called with 5 bytes instead of 4 */
434 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
435 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
436 --trans->bytesout;
437 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700438}
439
440static int spi_setup_opcode(spi_transaction *trans)
441{
Matt DeVillier4721e472019-05-18 16:05:00 -0500442 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700443 uint16_t optypes;
Jacob Garber9172b692019-06-26 16:18:16 -0600444 uint8_t opmenu[MENU_BYTES];
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700445
446 trans->opcode = trans->out[0];
447 spi_use_out(trans, 1);
Arthur Heymans816aaba2019-06-11 11:10:25 +0200448 if (!spi_locked()) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700449 /* The lock is off, so just use index 0. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200450 writeb_(trans->opcode, cntlr->opmenu);
451 optypes = readw_(cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700452 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
Arthur Heymans02c99712018-03-28 18:49:27 +0200453 writew_(optypes, cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700454 return 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700455 }
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100456
457 /* The lock is on. See if what we need is on the menu. */
458 uint8_t optype;
459 uint16_t opcode_index;
460
461 /* Write Enable is handled as atomic prefix */
462 if (trans->opcode == SPI_OPCODE_WREN)
463 return 0;
464
465 read_reg(cntlr->opmenu, opmenu, sizeof(opmenu));
Jacob Garber9172b692019-06-26 16:18:16 -0600466 for (opcode_index = 0; opcode_index < ARRAY_SIZE(opmenu); opcode_index++) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100467 if (opmenu[opcode_index] == trans->opcode)
468 break;
469 }
470
Jacob Garber9172b692019-06-26 16:18:16 -0600471 if (opcode_index == ARRAY_SIZE(opmenu)) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100472 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
473 trans->opcode);
474 return -1;
475 }
476
477 optypes = readw_(cntlr->optype);
478 optype = (optypes >> (opcode_index * 2)) & 0x3;
479 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
480 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
481 trans->bytesout >= 3) {
482 /* We guessed wrong earlier. Fix it up. */
483 trans->type = optype;
484 }
485 if (optype != trans->type) {
486 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
487 optype);
488 return -1;
489 }
490 return opcode_index;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700491}
492
493static int spi_setup_offset(spi_transaction *trans)
494{
495 /* Separate the SPI address and data. */
496 switch (trans->type) {
497 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
498 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
499 return 0;
500 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
501 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
502 trans->offset = ((uint32_t)trans->out[0] << 16) |
503 ((uint32_t)trans->out[1] << 8) |
504 ((uint32_t)trans->out[2] << 0);
505 spi_use_out(trans, 3);
506 return 1;
507 default:
508 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
509 return -1;
510 }
511}
512
513/*
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200514 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700515 * below is True) or 0. In case the wait was for the bit(s) to set - write
516 * those bits back, which would cause resetting them.
517 *
518 * Return the last read status value on success or -1 on failure.
519 */
520static int ich_status_poll(u16 bitmask, int wait_til_set)
521{
Matt DeVillier4721e472019-05-18 16:05:00 -0500522 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200523 int timeout = 600000; /* This will result in 6 seconds */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700524 u16 status = 0;
525
526 while (timeout--) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200527 status = readw_(cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700528 if (wait_til_set ^ ((status & bitmask) == 0)) {
529 if (wait_til_set)
Arthur Heymans02c99712018-03-28 18:49:27 +0200530 writew_((status & bitmask), cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700531 return status;
532 }
533 udelay(10);
534 }
535
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200536 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700537 status, bitmask);
538 return -1;
539}
540
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100541static int spi_is_multichip(void)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100542{
Matt DeVillier4721e472019-05-18 16:05:00 -0500543 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Arthur Heymans02c99712018-03-28 18:49:27 +0200544 if (!(cntlr->hsfs & HSFS_FDV))
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100545 return 0;
Arthur Heymans02c99712018-03-28 18:49:27 +0200546 return !!((cntlr->flmap0 >> 8) & 3);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100547}
548
Furquan Shaikh94f86992016-12-01 07:12:32 -0800549static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800550 size_t bytesout, void *din, size_t bytesin)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700551{
Matt DeVillier4721e472019-05-18 16:05:00 -0500552 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700553 uint16_t control;
554 int16_t opcode_index;
555 int with_address;
556 int status;
557
558 spi_transaction trans = {
Gabe Black93d9f922014-03-27 21:52:43 -0700559 dout, bytesout,
560 din, bytesin,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700561 0xff, 0xff, 0
562 };
563
564 /* There has to always at least be an opcode. */
Gabe Black93d9f922014-03-27 21:52:43 -0700565 if (!bytesout || !dout) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700566 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
567 return -1;
568 }
569 /* Make sure if we read something we have a place to put it. */
Gabe Black93d9f922014-03-27 21:52:43 -0700570 if (bytesin != 0 && !din) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700571 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
572 return -1;
573 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700574
575 if (ich_status_poll(SPIS_SCIP, 0) == -1)
576 return -1;
577
Arthur Heymans02c99712018-03-28 18:49:27 +0200578 writew_(SPIS_CDS | SPIS_FCERR, cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700579
580 spi_setup_type(&trans);
581 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
582 return -1;
583 if ((with_address = spi_setup_offset(&trans)) < 0)
584 return -1;
585
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700586 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700587 /*
588 * Treat Write Enable as Atomic Pre-Op if possible
589 * in order to prevent the Management Engine from
590 * issuing a transaction between WREN and DATA.
591 */
Arthur Heymans816aaba2019-06-11 11:10:25 +0200592 if (!spi_locked())
Arthur Heymans02c99712018-03-28 18:49:27 +0200593 writew_(trans.opcode, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700594 return 0;
595 }
596
597 /* Preset control fields */
598 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
599
600 /* Issue atomic preop cycle if needed */
Arthur Heymans02c99712018-03-28 18:49:27 +0200601 if (readw_(cntlr->preop))
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700602 control |= SPIC_ACS;
603
604 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700605 /* SPI addresses are 24 bit only */
606 if (with_address)
Arthur Heymans02c99712018-03-28 18:49:27 +0200607 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700608
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700609 /*
610 * This is a 'no data' command (like Write Enable), its
611 * bitesout size was 1, decremented to zero while executing
612 * spi_setup_opcode() above. Tell the chip to send the
613 * command.
614 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200615 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700616
617 /* wait for the result */
618 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
619 if (status == -1)
620 return -1;
621
622 if (status & SPIS_FCERR) {
623 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
624 return -1;
625 }
626
Werner Zehf13a6f92018-11-14 10:55:52 +0100627 goto spi_xfer_exit;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700628 }
629
630 /*
Paul Menzel94782972013-06-29 11:41:27 +0200631 * Check if this is a write command attempting to transfer more bytes
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700632 * than the controller can handle. Iterations for writes are not
633 * supported here because each SPI write command needs to be preceded
634 * and followed by other SPI commands, and this sequence is controlled
635 * by the SPI chip driver.
636 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200637 if (trans.bytesout > cntlr->databytes) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700638 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
Kyösti Mälkki11104952014-06-29 16:17:33 +0300639 " spi_crop_chunk()?\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700640 return -1;
641 }
642
643 /*
644 * Read or write up to databytes bytes at a time until everything has
645 * been sent.
646 */
647 while (trans.bytesout || trans.bytesin) {
648 uint32_t data_length;
649
650 /* SPI addresses are 24 bit only */
Arthur Heymans02c99712018-03-28 18:49:27 +0200651 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700652
653 if (trans.bytesout)
Arthur Heymans02c99712018-03-28 18:49:27 +0200654 data_length = min(trans.bytesout, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700655 else
Arthur Heymans02c99712018-03-28 18:49:27 +0200656 data_length = min(trans.bytesin, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700657
658 /* Program data into FDATA0 to N */
659 if (trans.bytesout) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200660 write_reg(trans.out, cntlr->data, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700661 spi_use_out(&trans, data_length);
662 if (with_address)
663 trans.offset += data_length;
664 }
665
666 /* Add proper control fields' values */
Arthur Heymans02c99712018-03-28 18:49:27 +0200667 control &= ~((cntlr->databytes - 1) << 8);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700668 control |= SPIC_DS;
669 control |= (data_length - 1) << 8;
670
671 /* write it */
Arthur Heymans02c99712018-03-28 18:49:27 +0200672 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700673
674 /* Wait for Cycle Done Status or Flash Cycle Error. */
675 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
676 if (status == -1)
677 return -1;
678
679 if (status & SPIS_FCERR) {
680 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
681 return -1;
682 }
683
684 if (trans.bytesin) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200685 read_reg(cntlr->data, trans.in, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700686 spi_use_in(&trans, data_length);
687 if (with_address)
688 trans.offset += data_length;
689 }
690 }
691
Werner Zehf13a6f92018-11-14 10:55:52 +0100692spi_xfer_exit:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700693 /* Clear atomic preop now that xfer is done */
Arthur Heymans02c99712018-03-28 18:49:27 +0200694 writew_(0, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700695
696 return 0;
697}
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100698
699/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
700static void ich_hwseq_set_addr(uint32_t addr)
701{
Matt DeVillier4721e472019-05-18 16:05:00 -0500702 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Arthur Heymans02c99712018-03-28 18:49:27 +0200703 uint32_t addr_old = readl_(&cntlr->ich9_spi->faddr) & ~0x01FFFFFF;
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100704
Arthur Heymans02c99712018-03-28 18:49:27 +0200705 writel_((addr & 0x01FFFFFF) | addr_old, &cntlr->ich9_spi->faddr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100706}
707
708/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
709 Resets all error flags in HSFS.
710 Returns 0 if the cycle completes successfully without errors within
711 timeout us, 1 on errors. */
712static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
713 unsigned int len)
714{
Matt DeVillier4721e472019-05-18 16:05:00 -0500715 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100716 uint16_t hsfs;
717 uint32_t addr;
718
719 timeout /= 8; /* scale timeout duration to counter */
Arthur Heymans02c99712018-03-28 18:49:27 +0200720 while ((((hsfs = readw_(&cntlr->ich9_spi->hsfs)) &
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100721 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
722 --timeout) {
723 udelay(8);
724 }
Arthur Heymans02c99712018-03-28 18:49:27 +0200725 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100726
727 if (!timeout) {
728 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200729 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
730 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100731 printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and "
732 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
733 addr, addr + len - 1, addr, len - 1,
734 hsfc, hsfs);
735 return 1;
736 }
737
738 if (hsfs & HSFS_FCERR) {
739 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200740 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
741 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100742 printk(BIOS_ERR, "Transaction error between offset 0x%08x and "
743 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
744 addr, addr + len - 1, addr, len - 1,
745 hsfc, hsfs);
746 return 1;
747 }
748 return 0;
749}
750
751
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800752static int ich_hwseq_erase(const struct spi_flash *flash, u32 offset,
753 size_t len)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100754{
Matt DeVillier4721e472019-05-18 16:05:00 -0500755 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100756 u32 start, end, erase_size;
757 int ret;
758 uint16_t hsfc;
Uwe Poeche17362be2019-07-17 14:27:13 +0200759 unsigned int timeout = 1000 * SPI_FLASH_SECTOR_ERASE_TIMEOUT_MS;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100760
761 erase_size = flash->sector_size;
762 if (offset % erase_size || len % erase_size) {
763 printk(BIOS_ERR, "SF: Erase offset/length not multiple of erase size\n");
764 return -1;
765 }
766
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800767 ret = spi_claim_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100768 if (ret) {
769 printk(BIOS_ERR, "SF: Unable to claim SPI bus\n");
770 return ret;
771 }
772
773 start = offset;
774 end = start + len;
775
776 while (offset < end) {
777 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
Arthur Heymans02c99712018-03-28 18:49:27 +0200778 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100779
780 ich_hwseq_set_addr(offset);
781
782 offset += erase_size;
783
Arthur Heymans02c99712018-03-28 18:49:27 +0200784 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100785 hsfc &= ~HSFC_FCYCLE; /* clear operation */
786 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
787 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200788 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100789 if (ich_hwseq_wait_for_cycle_complete(timeout, len)) {
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100790 printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size);
791 ret = -1;
792 goto out;
793 }
794 }
795
796 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
797
798out:
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800799 spi_release_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100800 return ret;
801}
802
803static void ich_read_data(uint8_t *data, int len)
804{
Matt DeVillier4721e472019-05-18 16:05:00 -0500805 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100806 int i;
807 uint32_t temp32 = 0;
808
809 for (i = 0; i < len; i++) {
810 if ((i % 4) == 0)
Arthur Heymans02c99712018-03-28 18:49:27 +0200811 temp32 = readl_(cntlr->data + i);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100812
813 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
814 }
815}
816
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800817static int ich_hwseq_read(const struct spi_flash *flash, u32 addr, size_t len,
818 void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100819{
Matt DeVillier4721e472019-05-18 16:05:00 -0500820 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100821 uint16_t hsfc;
822 uint16_t timeout = 100 * 60;
823 uint8_t block_len;
824
825 if (addr + len > flash->size) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100826 printk(BIOS_ERR,
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100827 "Attempt to read %x-%x which is out of chip\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600828 (unsigned int) addr,
829 (unsigned int) addr+(unsigned int) len);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100830 return -1;
831 }
832
833 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200834 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100835
836 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200837 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100838 if (block_len > (~addr & 0xff))
839 block_len = (~addr & 0xff) + 1;
840 ich_hwseq_set_addr(addr);
Arthur Heymans02c99712018-03-28 18:49:27 +0200841 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100842 hsfc &= ~HSFC_FCYCLE; /* set read operation */
843 hsfc &= ~HSFC_FDBC; /* clear byte count */
844 /* set byte count */
845 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
846 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200847 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100848
849 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
850 return 1;
851 ich_read_data(buf, block_len);
852 addr += block_len;
853 buf += block_len;
854 len -= block_len;
855 }
856 return 0;
857}
858
859/* Fill len bytes from the data array into the fdata/spid registers.
860 *
861 * Note that using len > flash->pgm->spi.max_data_write will trash the registers
862 * following the data registers.
863 */
864static void ich_fill_data(const uint8_t *data, int len)
865{
Matt DeVillier4721e472019-05-18 16:05:00 -0500866 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100867 uint32_t temp32 = 0;
868 int i;
869
870 if (len <= 0)
871 return;
872
873 for (i = 0; i < len; i++) {
874 if ((i % 4) == 0)
875 temp32 = 0;
876
877 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
878
879 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200880 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100881 }
882 i--;
883 if ((i % 4) != 3) /* Write remaining data to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200884 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100885}
886
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800887static int ich_hwseq_write(const struct spi_flash *flash, u32 addr, size_t len,
888 const void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100889{
Matt DeVillier4721e472019-05-18 16:05:00 -0500890 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100891 uint16_t hsfc;
892 uint16_t timeout = 100 * 60;
893 uint8_t block_len;
894 uint32_t start = addr;
895
896 if (addr + len > flash->size) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100897 printk(BIOS_ERR,
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100898 "Attempt to write 0x%x-0x%x which is out of chip\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600899 (unsigned int)addr, (unsigned int) (addr+len));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100900 return -1;
901 }
902
903 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200904 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100905
906 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200907 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100908 if (block_len > (~addr & 0xff))
909 block_len = (~addr & 0xff) + 1;
910
911 ich_hwseq_set_addr(addr);
912
913 ich_fill_data(buf, block_len);
Arthur Heymans02c99712018-03-28 18:49:27 +0200914 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100915 hsfc &= ~HSFC_FCYCLE; /* clear operation */
916 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
917 hsfc &= ~HSFC_FDBC; /* clear byte count */
918 /* set byte count */
919 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
920 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200921 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100922
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100923 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len)) {
924 printk(BIOS_ERR, "SF: write failure at %x\n",
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100925 addr);
926 return -1;
927 }
928 addr += block_len;
929 buf += block_len;
930 len -= block_len;
931 }
932 printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600933 (unsigned int) (addr - start), start);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100934 return 0;
935}
936
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700937static const struct spi_flash_ops spi_flash_ops = {
938 .read = ich_hwseq_read,
939 .write = ich_hwseq_write,
940 .erase = ich_hwseq_erase,
941};
942
Furquan Shaikha1491572017-05-17 19:14:06 -0700943static int spi_flash_programmer_probe(const struct spi_slave *spi,
944 struct spi_flash *flash)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100945{
Matt DeVillier4721e472019-05-18 16:05:00 -0500946 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100947
Julius Wernercd49cce2019-03-05 16:53:33 -0800948 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX))
Arthur Heymansc88e3702017-08-20 20:50:17 +0200949 return spi_flash_generic_probe(spi, flash);
950
Furquan Shaikha1491572017-05-17 19:14:06 -0700951 /* Try generic probing first if spi_is_multichip returns 0. */
952 if (!spi_is_multichip() && !spi_flash_generic_probe(spi, flash))
953 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100954
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800955 memcpy(&flash->spi, spi, sizeof(*spi));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100956 flash->name = "Opaque HW-sequencing";
957
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100958 ich_hwseq_set_addr(0);
959 switch ((cntlr->hsfs >> 3) & 3) {
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100960 case 0:
961 flash->sector_size = 256;
962 break;
963 case 1:
964 flash->sector_size = 4096;
965 break;
966 case 2:
967 flash->sector_size = 8192;
968 break;
969 case 3:
970 flash->sector_size = 65536;
971 break;
972 }
973
Stefan Tauner327205d2018-08-26 13:53:16 +0200974 flash->size = 1 << (19 + (cntlr->flcomp & 7));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100975
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700976 flash->ops = &spi_flash_ops;
977
Arthur Heymans02c99712018-03-28 18:49:27 +0200978 if ((cntlr->hsfs & HSFS_FDV) && ((cntlr->flmap0 >> 8) & 3))
Stefan Tauner327205d2018-08-26 13:53:16 +0200979 flash->size += 1 << (19 + ((cntlr->flcomp >> 3) & 7));
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100980 printk(BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100981
Furquan Shaikh30221b42017-05-15 14:35:15 -0700982 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100983}
Furquan Shaikha1491572017-05-17 19:14:06 -0700984
Aaron Durbin851dde82018-04-19 21:15:25 -0600985static int xfer_vectors(const struct spi_slave *slave,
986 struct spi_op vectors[], size_t count)
987{
988 return spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer);
989}
990
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100991#define SPI_FPR_SHIFT 12
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100992#define ICH7_SPI_FPR_MASK 0xfff
993#define ICH9_SPI_FPR_MASK 0x1fff
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100994#define SPI_FPR_BASE_SHIFT 0
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100995#define ICH7_SPI_FPR_LIMIT_SHIFT 12
996#define ICH9_SPI_FPR_LIMIT_SHIFT 16
997#define ICH9_SPI_FPR_RPE (1 << 15) /* Read Protect */
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100998#define SPI_FPR_WPE (1 << 31) /* Write Protect */
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100999
1000static u32 spi_fpr(u32 base, u32 limit)
1001{
1002 u32 ret;
1003 u32 mask, limit_shift;
Elyes HAOUASad19c2f2018-11-26 15:57:30 +01001004
Julius Wernercd49cce2019-03-05 16:53:33 -08001005 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001006 mask = ICH7_SPI_FPR_MASK;
1007 limit_shift = ICH7_SPI_FPR_LIMIT_SHIFT;
1008 } else {
1009 mask = ICH9_SPI_FPR_MASK;
1010 limit_shift = ICH9_SPI_FPR_LIMIT_SHIFT;
1011 }
1012 ret = ((limit >> SPI_FPR_SHIFT) & mask) << limit_shift;
1013 ret |= ((base >> SPI_FPR_SHIFT) & mask) << SPI_FPR_BASE_SHIFT;
1014 return ret;
1015}
1016
1017/*
1018 * Protect range of SPI flash defined by [start, start+size-1] using Flash
1019 * Protected Range (FPR) register if available.
1020 * Returns 0 on success, -1 on failure of programming fpr registers.
1021 */
1022static int spi_flash_protect(const struct spi_flash *flash,
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301023 const struct region *region,
1024 const enum ctrlr_prot_type type)
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001025{
Matt DeVillier4721e472019-05-18 16:05:00 -05001026 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001027 u32 start = region_offset(region);
1028 u32 end = start + region_sz(region) - 1;
1029 u32 reg;
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301030 u32 protect_mask = 0;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001031 int fpr;
1032 uint32_t *fpr_base;
1033
Arthur Heymans02c99712018-03-28 18:49:27 +02001034 fpr_base = cntlr->fpr;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001035
1036 /* Find first empty FPR */
Arthur Heymans02c99712018-03-28 18:49:27 +02001037 for (fpr = 0; fpr < cntlr->fpr_max; fpr++) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001038 reg = read32(&fpr_base[fpr]);
1039 if (reg == 0)
1040 break;
1041 }
1042
Arthur Heymans02c99712018-03-28 18:49:27 +02001043 if (fpr == cntlr->fpr_max) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001044 printk(BIOS_ERR, "ERROR: No SPI FPR free!\n");
1045 return -1;
1046 }
1047
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301048 switch (type) {
1049 case WRITE_PROTECT:
1050 protect_mask |= SPI_FPR_WPE;
1051 break;
1052 case READ_PROTECT:
Julius Wernercd49cce2019-03-05 16:53:33 -08001053 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX))
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301054 return -1;
1055 protect_mask |= ICH9_SPI_FPR_RPE;
1056 break;
1057 case READ_WRITE_PROTECT:
Julius Wernercd49cce2019-03-05 16:53:33 -08001058 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX))
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301059 return -1;
1060 protect_mask |= (ICH9_SPI_FPR_RPE | SPI_FPR_WPE);
1061 break;
1062 default:
1063 printk(BIOS_ERR, "ERROR: Seeking invalid protection!\n");
1064 return -1;
1065 }
1066
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001067 /* Set protected range base and limit */
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301068 reg = spi_fpr(start, end) | protect_mask;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001069
1070 /* Set the FPR register and verify it is protected */
1071 write32(&fpr_base[fpr], reg);
Arthur Heymansf9572012019-06-11 11:15:10 +02001072 if (reg != read32(&fpr_base[fpr])) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001073 printk(BIOS_ERR, "ERROR: Unable to set SPI FPR %d\n", fpr);
1074 return -1;
1075 }
1076
1077 printk(BIOS_INFO, "%s: FPR %d is enabled for range 0x%08x-0x%08x\n",
1078 __func__, fpr, start, end);
1079 return 0;
1080}
1081
Arthur Heymans92185e32019-05-28 13:06:34 +02001082void spi_finalize_ops(void)
1083{
Matt DeVillier4721e472019-05-18 16:05:00 -05001084 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Arthur Heymans92185e32019-05-28 13:06:34 +02001085 u16 spi_opprefix;
1086 u16 optype = 0;
Arthur Heymans50b4f782019-09-23 11:49:17 +02001087 struct intel_swseq_spi_config spi_config_default = {
Arthur Heymans92185e32019-05-28 13:06:34 +02001088 {0x06, 0x50}, /* OPPREFIXES: EWSR and WREN */
Arthur Heymans50b4f782019-09-23 11:49:17 +02001089 { /* OPCODE and OPTYPE */
Arthur Heymans92185e32019-05-28 13:06:34 +02001090 {0x01, WRITE_NO_ADDR}, /* WRSR: Write Status Register */
1091 {0x02, WRITE_WITH_ADDR}, /* BYPR: Byte Program */
1092 {0x03, READ_WITH_ADDR}, /* READ: Read Data */
1093 {0x05, READ_NO_ADDR}, /* RDSR: Read Status Register */
1094 {0x20, WRITE_WITH_ADDR}, /* SE20: Sector Erase 0x20 */
1095 {0x9f, READ_NO_ADDR}, /* RDID: Read ID */
1096 {0xd8, WRITE_WITH_ADDR}, /* BED8: Block Erase 0xd8 */
1097 {0x0b, READ_WITH_ADDR}, /* FAST: Fast Read */
1098 }
1099 };
Arthur Heymans50b4f782019-09-23 11:49:17 +02001100 struct intel_swseq_spi_config spi_config_aai_write = {
1101 {0x06, 0x50}, /* OPPREFIXES: EWSR and WREN */
1102 { /* OPCODE and OPTYPE */
1103 {0x01, WRITE_NO_ADDR}, /* WRSR: Write Status Register */
1104 {0x02, WRITE_WITH_ADDR}, /* BYPR: Byte Program */
1105 {0x03, READ_WITH_ADDR}, /* READ: Read Data */
1106 {0x05, READ_NO_ADDR}, /* RDSR: Read Status Register */
1107 {0x20, WRITE_WITH_ADDR}, /* SE20: Sector Erase 0x20 */
1108 {0x9f, READ_NO_ADDR}, /* RDID: Read ID */
1109 {0xad, WRITE_NO_ADDR}, /* Auto Address Increment Word Program */
1110 {0x04, WRITE_NO_ADDR} /* Write Disable */
1111 }
1112 };
1113 const struct spi_flash *flash = boot_device_spi_flash();
1114 struct intel_swseq_spi_config *spi_config = &spi_config_default;
Arthur Heymans92185e32019-05-28 13:06:34 +02001115 int i;
1116
Arthur Heymans50b4f782019-09-23 11:49:17 +02001117 /*
1118 * Some older SST SPI flashes support AAI write but use 0xaf opcde for
1119 * that. Flashrom uses the byte program opcode to write those flashes,
1120 * so this configuration is fine too. SST25VF064C (id = 0x4b) is an
1121 * exception.
1122 */
1123 if (flash && flash->vendor == VENDOR_ID_SST && (flash->model & 0x00ff) != 0x4b)
1124 spi_config = &spi_config_aai_write;
1125
Arthur Heymans92185e32019-05-28 13:06:34 +02001126 if (spi_locked())
1127 return;
1128
Arthur Heymans50b4f782019-09-23 11:49:17 +02001129 intel_southbridge_override_spi(spi_config);
Arthur Heymans92185e32019-05-28 13:06:34 +02001130
Arthur Heymans50b4f782019-09-23 11:49:17 +02001131 spi_opprefix = spi_config->opprefixes[0]
1132 | (spi_config->opprefixes[1] << 8);
Arthur Heymans92185e32019-05-28 13:06:34 +02001133 writew_(spi_opprefix, cntlr->preop);
Arthur Heymans50b4f782019-09-23 11:49:17 +02001134 for (i = 0; i < ARRAY_SIZE(spi_config->ops); i++) {
1135 optype |= (spi_config->ops[i].type & 3) << (i * 2);
1136 writeb_(spi_config->ops[i].op, &cntlr->opmenu[i]);
Arthur Heymans92185e32019-05-28 13:06:34 +02001137 }
Nico Hubereaeb0b72019-07-27 13:45:58 +02001138 writew_(optype, cntlr->optype);
Arthur Heymans92185e32019-05-28 13:06:34 +02001139}
1140
1141__weak void intel_southbridge_override_spi(struct intel_swseq_spi_config *spi_config)
1142{
1143}
1144
Furquan Shaikha1491572017-05-17 19:14:06 -07001145static const struct spi_ctrlr spi_ctrlr = {
Aaron Durbin851dde82018-04-19 21:15:25 -06001146 .xfer_vector = xfer_vectors,
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +02001147 .max_xfer_size = member_size(struct ich9_spi_regs, fdata),
Furquan Shaikha1491572017-05-17 19:14:06 -07001148 .flash_probe = spi_flash_programmer_probe,
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001149 .flash_protect = spi_flash_protect,
Furquan Shaikha1491572017-05-17 19:14:06 -07001150};
1151
Furquan Shaikh2cd03f12017-05-18 14:58:32 -07001152const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
1153 {
1154 .ctrlr = &spi_ctrlr,
1155 .bus_start = 0,
1156 .bus_end = 0,
1157 },
1158};
1159
1160const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);