blob: 3b7842de7d1351e1099573e1632cc4d5376ff7d5 [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
Arthur Heymans026863b2019-11-21 08:24:02 +010018#define __SIMPLE_DEVICE__
19
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070020/* This file is derived from the flashrom project. */
Matt DeVillier4721e472019-05-18 16:05:00 -050021#include <arch/early_variables.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070022#include <stdint.h>
23#include <stdlib.h>
24#include <string.h>
David Hendricksf2612a12014-04-13 16:27:02 -070025#include <bootstate.h>
Furquan Shaikhde705fa2017-04-19 19:27:28 -070026#include <commonlib/helpers.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070027#include <delay.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020028#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020029#include <device/pci_ops.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070030#include <console/console.h>
Kyösti Mälkki7ba14402019-02-07 12:44:00 +020031#include <device/device.h>
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010032#include <device/pci.h>
33#include <spi_flash.h>
Zheng Bao600784e2013-02-07 17:30:23 +080034#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070035
Arthur Heymans92185e32019-05-28 13:06:34 +020036#include "spi.h"
37
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010038#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
39#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
40#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
41#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
42
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010043static int spi_is_multichip(void);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010044
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020045struct ich7_spi_regs {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070046 uint16_t spis;
47 uint16_t spic;
48 uint32_t spia;
49 uint64_t spid[8];
50 uint64_t _pad;
51 uint32_t bbar;
52 uint16_t preop;
53 uint16_t optype;
54 uint8_t opmenu[8];
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +010055 uint32_t pbr[3];
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020056} __packed;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070057
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020058struct ich9_spi_regs {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070059 uint32_t bfpr;
60 uint16_t hsfs;
61 uint16_t hsfc;
62 uint32_t faddr;
63 uint32_t _reserved0;
64 uint32_t fdata[16];
65 uint32_t frap;
66 uint32_t freg[5];
67 uint32_t _reserved1[3];
68 uint32_t pr[5];
69 uint32_t _reserved2[2];
70 uint8_t ssfs;
71 uint8_t ssfc[3];
72 uint16_t preop;
73 uint16_t optype;
74 uint8_t opmenu[8];
75 uint32_t bbar;
76 uint8_t _reserved3[12];
77 uint32_t fdoc;
78 uint32_t fdod;
79 uint8_t _reserved4[8];
80 uint32_t afc;
81 uint32_t lvscc;
82 uint32_t uvscc;
83 uint8_t _reserved5[4];
84 uint32_t fpb;
85 uint8_t _reserved6[28];
86 uint32_t srdl;
87 uint32_t srdc;
88 uint32_t srd;
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020089} __packed;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070090
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020091struct ich_spi_controller {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070092 int locked;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010093 uint32_t flmap0;
Stefan Tauner327205d2018-08-26 13:53:16 +020094 uint32_t flcomp;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010095 uint32_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070096
Arthur Heymans21c5d432019-06-15 18:23:29 +020097 union {
98 struct ich9_spi_regs *ich9_spi;
99 struct ich7_spi_regs *ich7_spi;
100 };
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700101 uint8_t *opmenu;
102 int menubytes;
103 uint16_t *preop;
104 uint16_t *optype;
105 uint32_t *addr;
106 uint8_t *data;
Martin Rothff744bf2019-10-23 21:46:03 -0600107 unsigned int databytes;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700108 uint8_t *status;
109 uint16_t *control;
110 uint32_t *bbar;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100111 uint32_t *fpr;
112 uint8_t fpr_max;
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200113};
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700114
Matt DeVillier4721e472019-05-18 16:05:00 -0500115static struct ich_spi_controller g_cntlr CAR_GLOBAL;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700116
117enum {
118 SPIS_SCIP = 0x0001,
119 SPIS_GRANT = 0x0002,
120 SPIS_CDS = 0x0004,
121 SPIS_FCERR = 0x0008,
122 SSFS_AEL = 0x0010,
123 SPIS_LOCK = 0x8000,
124 SPIS_RESERVED_MASK = 0x7ff0,
125 SSFS_RESERVED_MASK = 0x7fe2
126};
127
128enum {
129 SPIC_SCGO = 0x000002,
130 SPIC_ACS = 0x000004,
131 SPIC_SPOP = 0x000008,
132 SPIC_DBC = 0x003f00,
133 SPIC_DS = 0x004000,
134 SPIC_SME = 0x008000,
135 SSFC_SCF_MASK = 0x070000,
136 SSFC_RESERVED = 0xf80000
137};
138
139enum {
140 HSFS_FDONE = 0x0001,
141 HSFS_FCERR = 0x0002,
142 HSFS_AEL = 0x0004,
143 HSFS_BERASE_MASK = 0x0018,
144 HSFS_BERASE_SHIFT = 3,
145 HSFS_SCIP = 0x0020,
146 HSFS_FDOPSS = 0x2000,
147 HSFS_FDV = 0x4000,
148 HSFS_FLOCKDN = 0x8000
149};
150
151enum {
152 HSFC_FGO = 0x0001,
153 HSFC_FCYCLE_MASK = 0x0006,
154 HSFC_FCYCLE_SHIFT = 1,
155 HSFC_FDBC_MASK = 0x3f00,
156 HSFC_FDBC_SHIFT = 8,
157 HSFC_FSMIE = 0x8000
158};
159
160enum {
161 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
162 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
163 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
164 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
165};
166
Julius Wernercd49cce2019-03-05 16:53:33 -0800167#if CONFIG(DEBUG_SPI_FLASH)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700168
169static u8 readb_(const void *addr)
170{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800171 u8 v = read8(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100172
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700173 printk(BIOS_DEBUG, "read %2.2x from %4.4x\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600174 v, ((unsigned int) addr & 0xffff) - 0xf020);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700175 return v;
176}
177
178static u16 readw_(const void *addr)
179{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800180 u16 v = read16(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100181
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700182 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600183 v, ((unsigned int) addr & 0xffff) - 0xf020);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700184 return v;
185}
186
187static u32 readl_(const void *addr)
188{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800189 u32 v = read32(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100190
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700191 printk(BIOS_DEBUG, "read %8.8x from %4.4x\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600192 v, ((unsigned int) addr & 0xffff) - 0xf020);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700193 return v;
194}
195
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800196static void writeb_(u8 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700197{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800198 write8(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700199 printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600200 b, ((unsigned int) addr & 0xffff) - 0xf020);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700201}
202
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800203static void writew_(u16 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700204{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800205 write16(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700206 printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600207 b, ((unsigned int) addr & 0xffff) - 0xf020);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700208}
209
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800210static void writel_(u32 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700211{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800212 write32(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700213 printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600214 b, ((unsigned int) addr & 0xffff) - 0xf020);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700215}
216
217#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
218
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800219#define readb_(a) read8(a)
220#define readw_(a) read16(a)
221#define readl_(a) read32(a)
222#define writeb_(val, addr) write8(addr, val)
223#define writew_(val, addr) write16(addr, val)
224#define writel_(val, addr) write32(addr, val)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700225
226#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
227
228static void write_reg(const void *value, void *dest, uint32_t size)
229{
230 const uint8_t *bvalue = value;
231 uint8_t *bdest = dest;
232
233 while (size >= 4) {
234 writel_(*(const uint32_t *)bvalue, bdest);
235 bdest += 4; bvalue += 4; size -= 4;
236 }
237 while (size) {
238 writeb_(*bvalue, bdest);
239 bdest++; bvalue++; size--;
240 }
241}
242
243static void read_reg(const void *src, void *value, uint32_t size)
244{
245 const uint8_t *bsrc = src;
246 uint8_t *bvalue = value;
247
248 while (size >= 4) {
249 *(uint32_t *)bvalue = readl_(bsrc);
250 bsrc += 4; bvalue += 4; size -= 4;
251 }
252 while (size) {
253 *bvalue = readb_(bsrc);
254 bsrc++; bvalue++; size--;
255 }
256}
257
258static void ich_set_bbar(uint32_t minaddr)
259{
Matt DeVillier4721e472019-05-18 16:05:00 -0500260 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700261 const uint32_t bbar_mask = 0x00ffff00;
262 uint32_t ichspi_bbar;
263
264 minaddr &= bbar_mask;
Arthur Heymans02c99712018-03-28 18:49:27 +0200265 ichspi_bbar = readl_(cntlr->bbar) & ~bbar_mask;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700266 ichspi_bbar |= minaddr;
Arthur Heymans02c99712018-03-28 18:49:27 +0200267 writel_(ichspi_bbar, cntlr->bbar);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700268}
269
Jacob Garber9172b692019-06-26 16:18:16 -0600270#if CONFIG(SOUTHBRIDGE_INTEL_I82801GX)
271#define MENU_BYTES member_size(struct ich7_spi_regs, opmenu)
272#else
273#define MENU_BYTES member_size(struct ich9_spi_regs, opmenu)
274#endif
275
Arthur Heymans47a66032019-10-25 23:43:14 +0200276#define RCBA 0xf0
277#define SBASE 0x54
278
Arthur Heymans47a66032019-10-25 23:43:14 +0200279static void *get_spi_bar(pci_devfn_t dev)
Arthur Heymans47a66032019-10-25 23:43:14 +0200280{
281 uintptr_t rcba; /* Root Complex Register Block */
282 uintptr_t sbase;
283
284 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) {
285 rcba = pci_read_config32(dev, RCBA);
286 return (void *)((rcba & 0xffffc000) + 0x3020);
287 }
288 if (CONFIG(SOUTHBRIDGE_INTEL_COMMON_SPI_SILVERMONT)) {
289 sbase = pci_read_config32(dev, SBASE);
290 sbase &= ~0x1ff;
291 return (void *)sbase;
292 }
293 if (CONFIG(SOUTHBRIDGE_INTEL_COMMON_SPI_ICH9)) {
294 rcba = pci_read_config32(dev, RCBA);
295 return (void *)((rcba & 0xffffc000) + 0x3800);
296 }
297}
298
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700299void spi_init(void)
300{
Matt DeVillier4721e472019-05-18 16:05:00 -0500301 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700302 uint8_t bios_cntl;
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200303 struct ich9_spi_regs *ich9_spi;
304 struct ich7_spi_regs *ich7_spi;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100305 uint16_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700306
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200307 pci_devfn_t dev = PCI_DEV(0, 31, 0);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700308
Julius Wernercd49cce2019-03-05 16:53:33 -0800309 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) {
Arthur Heymans47a66032019-10-25 23:43:14 +0200310 ich7_spi = get_spi_bar(dev);
Arthur Heymans21c5d432019-06-15 18:23:29 +0200311 cntlr->ich7_spi = ich7_spi;
Arthur Heymans02c99712018-03-28 18:49:27 +0200312 cntlr->opmenu = ich7_spi->opmenu;
313 cntlr->menubytes = sizeof(ich7_spi->opmenu);
314 cntlr->optype = &ich7_spi->optype;
315 cntlr->addr = &ich7_spi->spia;
316 cntlr->data = (uint8_t *)ich7_spi->spid;
317 cntlr->databytes = sizeof(ich7_spi->spid);
318 cntlr->status = (uint8_t *)&ich7_spi->spis;
Arthur Heymans02c99712018-03-28 18:49:27 +0200319 cntlr->control = &ich7_spi->spic;
320 cntlr->bbar = &ich7_spi->bbar;
321 cntlr->preop = &ich7_spi->preop;
322 cntlr->fpr = &ich7_spi->pbr[0];
323 cntlr->fpr_max = 3;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200324 } else {
Arthur Heymans47a66032019-10-25 23:43:14 +0200325 ich9_spi = get_spi_bar(dev);
Arthur Heymans02c99712018-03-28 18:49:27 +0200326 cntlr->ich9_spi = ich9_spi;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200327 hsfs = readw_(&ich9_spi->hsfs);
Arthur Heymans02c99712018-03-28 18:49:27 +0200328 cntlr->hsfs = hsfs;
329 cntlr->opmenu = ich9_spi->opmenu;
330 cntlr->menubytes = sizeof(ich9_spi->opmenu);
331 cntlr->optype = &ich9_spi->optype;
332 cntlr->addr = &ich9_spi->faddr;
333 cntlr->data = (uint8_t *)ich9_spi->fdata;
334 cntlr->databytes = sizeof(ich9_spi->fdata);
335 cntlr->status = &ich9_spi->ssfs;
336 cntlr->control = (uint16_t *)ich9_spi->ssfc;
337 cntlr->bbar = &ich9_spi->bbar;
338 cntlr->preop = &ich9_spi->preop;
339 cntlr->fpr = &ich9_spi->pr[0];
340 cntlr->fpr_max = 5;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700341
Arthur Heymans02c99712018-03-28 18:49:27 +0200342 if (cntlr->hsfs & HSFS_FDV) {
Patrick Georgic88828d2018-11-26 10:42:59 +0100343 writel_(4, &ich9_spi->fdoc);
Arthur Heymans02c99712018-03-28 18:49:27 +0200344 cntlr->flmap0 = readl_(&ich9_spi->fdod);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100345 writel_(0x1000, &ich9_spi->fdoc);
Stefan Tauner327205d2018-08-26 13:53:16 +0200346 cntlr->flcomp = readl_(&ich9_spi->fdod);
Arthur Heymansc88e3702017-08-20 20:50:17 +0200347 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700348 }
349
350 ich_set_bbar(0);
351
Arthur Heymans47a66032019-10-25 23:43:14 +0200352 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX) || CONFIG(SOUTHBRIDGE_INTEL_COMMON_SPI_ICH9)) {
353 /* Disable the BIOS write protect so write commands are allowed. */
354 bios_cntl = pci_read_config8(dev, 0xdc);
355 /* Deassert SMM BIOS Write Protect Disable. */
356 bios_cntl &= ~(1 << 5);
357 pci_write_config8(dev, 0xdc, bios_cntl | 0x1);
358 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700359}
Aaron Durbin4d3de7e2015-09-02 17:34:04 -0500360
Arthur Heymans816aaba2019-06-11 11:10:25 +0200361static int spi_locked(void)
362{
363 struct ich_spi_controller *cntlr = &g_cntlr;
364 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) {
365 return !!(readw_(&cntlr->ich7_spi->spis) & HSFS_FLOCKDN);
366 } else {
Jacob Garber36749742019-07-02 11:08:53 -0600367 return !!(readw_(&cntlr->ich9_spi->hsfs) & HSFS_FLOCKDN);
Arthur Heymans816aaba2019-06-11 11:10:25 +0200368 }
369}
370
David Hendricksf2612a12014-04-13 16:27:02 -0700371static void spi_init_cb(void *unused)
372{
373 spi_init();
374}
375
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500376BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700377
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700378typedef struct spi_transaction {
379 const uint8_t *out;
380 uint32_t bytesout;
381 uint8_t *in;
382 uint32_t bytesin;
383 uint8_t type;
384 uint8_t opcode;
385 uint32_t offset;
386} spi_transaction;
387
Martin Rothff744bf2019-10-23 21:46:03 -0600388static inline void spi_use_out(spi_transaction *trans, unsigned int bytes)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700389{
390 trans->out += bytes;
391 trans->bytesout -= bytes;
392}
393
Martin Rothff744bf2019-10-23 21:46:03 -0600394static inline void spi_use_in(spi_transaction *trans, unsigned int bytes)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700395{
396 trans->in += bytes;
397 trans->bytesin -= bytes;
398}
399
400static void spi_setup_type(spi_transaction *trans)
401{
402 trans->type = 0xFF;
403
404 /* Try to guess spi type from read/write sizes. */
405 if (trans->bytesin == 0) {
406 if (trans->bytesout > 4)
407 /*
408 * If bytesin = 0 and bytesout > 4, we presume this is
409 * a write data operation, which is accompanied by an
410 * address.
411 */
412 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
413 else
414 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
415 return;
416 }
417
418 if (trans->bytesout == 1) { /* and bytesin is > 0 */
419 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
420 return;
421 }
422
423 if (trans->bytesout == 4) { /* and bytesin is > 0 */
424 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
425 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700426
427 /* Fast read command is called with 5 bytes instead of 4 */
428 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
429 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
430 --trans->bytesout;
431 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700432}
433
434static int spi_setup_opcode(spi_transaction *trans)
435{
Matt DeVillier4721e472019-05-18 16:05:00 -0500436 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700437 uint16_t optypes;
Jacob Garber9172b692019-06-26 16:18:16 -0600438 uint8_t opmenu[MENU_BYTES];
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700439
440 trans->opcode = trans->out[0];
441 spi_use_out(trans, 1);
Arthur Heymans816aaba2019-06-11 11:10:25 +0200442 if (!spi_locked()) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700443 /* The lock is off, so just use index 0. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200444 writeb_(trans->opcode, cntlr->opmenu);
445 optypes = readw_(cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700446 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
Arthur Heymans02c99712018-03-28 18:49:27 +0200447 writew_(optypes, cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700448 return 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700449 }
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100450
451 /* The lock is on. See if what we need is on the menu. */
452 uint8_t optype;
453 uint16_t opcode_index;
454
455 /* Write Enable is handled as atomic prefix */
456 if (trans->opcode == SPI_OPCODE_WREN)
457 return 0;
458
459 read_reg(cntlr->opmenu, opmenu, sizeof(opmenu));
Jacob Garber9172b692019-06-26 16:18:16 -0600460 for (opcode_index = 0; opcode_index < ARRAY_SIZE(opmenu); opcode_index++) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100461 if (opmenu[opcode_index] == trans->opcode)
462 break;
463 }
464
Jacob Garber9172b692019-06-26 16:18:16 -0600465 if (opcode_index == ARRAY_SIZE(opmenu)) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100466 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
467 trans->opcode);
468 return -1;
469 }
470
471 optypes = readw_(cntlr->optype);
472 optype = (optypes >> (opcode_index * 2)) & 0x3;
473 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
474 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
475 trans->bytesout >= 3) {
476 /* We guessed wrong earlier. Fix it up. */
477 trans->type = optype;
478 }
479 if (optype != trans->type) {
480 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
481 optype);
482 return -1;
483 }
484 return opcode_index;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700485}
486
487static int spi_setup_offset(spi_transaction *trans)
488{
489 /* Separate the SPI address and data. */
490 switch (trans->type) {
491 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
492 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
493 return 0;
494 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
495 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
496 trans->offset = ((uint32_t)trans->out[0] << 16) |
497 ((uint32_t)trans->out[1] << 8) |
498 ((uint32_t)trans->out[2] << 0);
499 spi_use_out(trans, 3);
500 return 1;
501 default:
502 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
503 return -1;
504 }
505}
506
507/*
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200508 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700509 * below is True) or 0. In case the wait was for the bit(s) to set - write
510 * those bits back, which would cause resetting them.
511 *
512 * Return the last read status value on success or -1 on failure.
513 */
514static int ich_status_poll(u16 bitmask, int wait_til_set)
515{
Matt DeVillier4721e472019-05-18 16:05:00 -0500516 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200517 int timeout = 600000; /* This will result in 6 seconds */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700518 u16 status = 0;
519
520 while (timeout--) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200521 status = readw_(cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700522 if (wait_til_set ^ ((status & bitmask) == 0)) {
523 if (wait_til_set)
Arthur Heymans02c99712018-03-28 18:49:27 +0200524 writew_((status & bitmask), cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700525 return status;
526 }
527 udelay(10);
528 }
529
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200530 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700531 status, bitmask);
532 return -1;
533}
534
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100535static int spi_is_multichip(void)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100536{
Matt DeVillier4721e472019-05-18 16:05:00 -0500537 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Arthur Heymans02c99712018-03-28 18:49:27 +0200538 if (!(cntlr->hsfs & HSFS_FDV))
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100539 return 0;
Arthur Heymans02c99712018-03-28 18:49:27 +0200540 return !!((cntlr->flmap0 >> 8) & 3);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100541}
542
Furquan Shaikh94f86992016-12-01 07:12:32 -0800543static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800544 size_t bytesout, void *din, size_t bytesin)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700545{
Matt DeVillier4721e472019-05-18 16:05:00 -0500546 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700547 uint16_t control;
548 int16_t opcode_index;
549 int with_address;
550 int status;
551
552 spi_transaction trans = {
Gabe Black93d9f922014-03-27 21:52:43 -0700553 dout, bytesout,
554 din, bytesin,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700555 0xff, 0xff, 0
556 };
557
558 /* There has to always at least be an opcode. */
Gabe Black93d9f922014-03-27 21:52:43 -0700559 if (!bytesout || !dout) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700560 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
561 return -1;
562 }
563 /* Make sure if we read something we have a place to put it. */
Gabe Black93d9f922014-03-27 21:52:43 -0700564 if (bytesin != 0 && !din) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700565 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
566 return -1;
567 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700568
569 if (ich_status_poll(SPIS_SCIP, 0) == -1)
570 return -1;
571
Arthur Heymans02c99712018-03-28 18:49:27 +0200572 writew_(SPIS_CDS | SPIS_FCERR, cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700573
574 spi_setup_type(&trans);
575 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
576 return -1;
577 if ((with_address = spi_setup_offset(&trans)) < 0)
578 return -1;
579
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700580 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700581 /*
582 * Treat Write Enable as Atomic Pre-Op if possible
583 * in order to prevent the Management Engine from
584 * issuing a transaction between WREN and DATA.
585 */
Arthur Heymans816aaba2019-06-11 11:10:25 +0200586 if (!spi_locked())
Arthur Heymans02c99712018-03-28 18:49:27 +0200587 writew_(trans.opcode, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700588 return 0;
589 }
590
591 /* Preset control fields */
592 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
593
594 /* Issue atomic preop cycle if needed */
Arthur Heymans02c99712018-03-28 18:49:27 +0200595 if (readw_(cntlr->preop))
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700596 control |= SPIC_ACS;
597
598 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700599 /* SPI addresses are 24 bit only */
600 if (with_address)
Arthur Heymans02c99712018-03-28 18:49:27 +0200601 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700602
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700603 /*
604 * This is a 'no data' command (like Write Enable), its
605 * bitesout size was 1, decremented to zero while executing
606 * spi_setup_opcode() above. Tell the chip to send the
607 * command.
608 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200609 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700610
611 /* wait for the result */
612 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
613 if (status == -1)
614 return -1;
615
616 if (status & SPIS_FCERR) {
617 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
618 return -1;
619 }
620
Werner Zehf13a6f92018-11-14 10:55:52 +0100621 goto spi_xfer_exit;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700622 }
623
624 /*
Paul Menzel94782972013-06-29 11:41:27 +0200625 * Check if this is a write command attempting to transfer more bytes
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700626 * than the controller can handle. Iterations for writes are not
627 * supported here because each SPI write command needs to be preceded
628 * and followed by other SPI commands, and this sequence is controlled
629 * by the SPI chip driver.
630 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200631 if (trans.bytesout > cntlr->databytes) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700632 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
Kyösti Mälkki11104952014-06-29 16:17:33 +0300633 " spi_crop_chunk()?\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700634 return -1;
635 }
636
637 /*
638 * Read or write up to databytes bytes at a time until everything has
639 * been sent.
640 */
641 while (trans.bytesout || trans.bytesin) {
642 uint32_t data_length;
643
644 /* SPI addresses are 24 bit only */
Arthur Heymans02c99712018-03-28 18:49:27 +0200645 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700646
647 if (trans.bytesout)
Arthur Heymans02c99712018-03-28 18:49:27 +0200648 data_length = min(trans.bytesout, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700649 else
Arthur Heymans02c99712018-03-28 18:49:27 +0200650 data_length = min(trans.bytesin, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700651
652 /* Program data into FDATA0 to N */
653 if (trans.bytesout) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200654 write_reg(trans.out, cntlr->data, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700655 spi_use_out(&trans, data_length);
656 if (with_address)
657 trans.offset += data_length;
658 }
659
660 /* Add proper control fields' values */
Arthur Heymans02c99712018-03-28 18:49:27 +0200661 control &= ~((cntlr->databytes - 1) << 8);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700662 control |= SPIC_DS;
663 control |= (data_length - 1) << 8;
664
665 /* write it */
Arthur Heymans02c99712018-03-28 18:49:27 +0200666 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700667
668 /* Wait for Cycle Done Status or Flash Cycle Error. */
669 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
670 if (status == -1)
671 return -1;
672
673 if (status & SPIS_FCERR) {
674 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
675 return -1;
676 }
677
678 if (trans.bytesin) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200679 read_reg(cntlr->data, trans.in, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700680 spi_use_in(&trans, data_length);
681 if (with_address)
682 trans.offset += data_length;
683 }
684 }
685
Werner Zehf13a6f92018-11-14 10:55:52 +0100686spi_xfer_exit:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700687 /* Clear atomic preop now that xfer is done */
Arthur Heymans02c99712018-03-28 18:49:27 +0200688 writew_(0, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700689
690 return 0;
691}
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100692
693/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
694static void ich_hwseq_set_addr(uint32_t addr)
695{
Matt DeVillier4721e472019-05-18 16:05:00 -0500696 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Arthur Heymans02c99712018-03-28 18:49:27 +0200697 uint32_t addr_old = readl_(&cntlr->ich9_spi->faddr) & ~0x01FFFFFF;
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100698
Arthur Heymans02c99712018-03-28 18:49:27 +0200699 writel_((addr & 0x01FFFFFF) | addr_old, &cntlr->ich9_spi->faddr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100700}
701
702/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
703 Resets all error flags in HSFS.
704 Returns 0 if the cycle completes successfully without errors within
705 timeout us, 1 on errors. */
706static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
707 unsigned int len)
708{
Matt DeVillier4721e472019-05-18 16:05:00 -0500709 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100710 uint16_t hsfs;
711 uint32_t addr;
712
713 timeout /= 8; /* scale timeout duration to counter */
Arthur Heymans02c99712018-03-28 18:49:27 +0200714 while ((((hsfs = readw_(&cntlr->ich9_spi->hsfs)) &
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100715 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
716 --timeout) {
717 udelay(8);
718 }
Arthur Heymans02c99712018-03-28 18:49:27 +0200719 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100720
721 if (!timeout) {
722 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200723 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
724 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100725 printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and "
726 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
727 addr, addr + len - 1, addr, len - 1,
728 hsfc, hsfs);
729 return 1;
730 }
731
732 if (hsfs & HSFS_FCERR) {
733 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200734 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
735 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100736 printk(BIOS_ERR, "Transaction error between offset 0x%08x and "
737 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
738 addr, addr + len - 1, addr, len - 1,
739 hsfc, hsfs);
740 return 1;
741 }
742 return 0;
743}
744
745
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800746static int ich_hwseq_erase(const struct spi_flash *flash, u32 offset,
747 size_t len)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100748{
Matt DeVillier4721e472019-05-18 16:05:00 -0500749 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100750 u32 start, end, erase_size;
751 int ret;
752 uint16_t hsfc;
Uwe Poeche17362be2019-07-17 14:27:13 +0200753 unsigned int timeout = 1000 * SPI_FLASH_SECTOR_ERASE_TIMEOUT_MS;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100754
755 erase_size = flash->sector_size;
756 if (offset % erase_size || len % erase_size) {
757 printk(BIOS_ERR, "SF: Erase offset/length not multiple of erase size\n");
758 return -1;
759 }
760
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800761 ret = spi_claim_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100762 if (ret) {
763 printk(BIOS_ERR, "SF: Unable to claim SPI bus\n");
764 return ret;
765 }
766
767 start = offset;
768 end = start + len;
769
770 while (offset < end) {
771 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
Arthur Heymans02c99712018-03-28 18:49:27 +0200772 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100773
774 ich_hwseq_set_addr(offset);
775
776 offset += erase_size;
777
Arthur Heymans02c99712018-03-28 18:49:27 +0200778 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100779 hsfc &= ~HSFC_FCYCLE; /* clear operation */
780 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
781 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200782 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100783 if (ich_hwseq_wait_for_cycle_complete(timeout, len)) {
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100784 printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size);
785 ret = -1;
786 goto out;
787 }
788 }
789
790 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
791
792out:
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800793 spi_release_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100794 return ret;
795}
796
797static void ich_read_data(uint8_t *data, int len)
798{
Matt DeVillier4721e472019-05-18 16:05:00 -0500799 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100800 int i;
801 uint32_t temp32 = 0;
802
803 for (i = 0; i < len; i++) {
804 if ((i % 4) == 0)
Arthur Heymans02c99712018-03-28 18:49:27 +0200805 temp32 = readl_(cntlr->data + i);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100806
807 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
808 }
809}
810
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800811static int ich_hwseq_read(const struct spi_flash *flash, u32 addr, size_t len,
812 void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100813{
Matt DeVillier4721e472019-05-18 16:05:00 -0500814 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100815 uint16_t hsfc;
816 uint16_t timeout = 100 * 60;
817 uint8_t block_len;
818
819 if (addr + len > flash->size) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100820 printk(BIOS_ERR,
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100821 "Attempt to read %x-%x which is out of chip\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600822 (unsigned int) addr,
823 (unsigned int) addr+(unsigned int) len);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100824 return -1;
825 }
826
827 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200828 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100829
830 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200831 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100832 if (block_len > (~addr & 0xff))
833 block_len = (~addr & 0xff) + 1;
834 ich_hwseq_set_addr(addr);
Arthur Heymans02c99712018-03-28 18:49:27 +0200835 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100836 hsfc &= ~HSFC_FCYCLE; /* set read operation */
837 hsfc &= ~HSFC_FDBC; /* clear byte count */
838 /* set byte count */
839 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
840 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200841 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100842
843 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
844 return 1;
845 ich_read_data(buf, block_len);
846 addr += block_len;
847 buf += block_len;
848 len -= block_len;
849 }
850 return 0;
851}
852
853/* Fill len bytes from the data array into the fdata/spid registers.
854 *
855 * Note that using len > flash->pgm->spi.max_data_write will trash the registers
856 * following the data registers.
857 */
858static void ich_fill_data(const uint8_t *data, int len)
859{
Matt DeVillier4721e472019-05-18 16:05:00 -0500860 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100861 uint32_t temp32 = 0;
862 int i;
863
864 if (len <= 0)
865 return;
866
867 for (i = 0; i < len; i++) {
868 if ((i % 4) == 0)
869 temp32 = 0;
870
871 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
872
873 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200874 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100875 }
876 i--;
877 if ((i % 4) != 3) /* Write remaining data to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200878 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100879}
880
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800881static int ich_hwseq_write(const struct spi_flash *flash, u32 addr, size_t len,
882 const void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100883{
Matt DeVillier4721e472019-05-18 16:05:00 -0500884 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100885 uint16_t hsfc;
886 uint16_t timeout = 100 * 60;
887 uint8_t block_len;
888 uint32_t start = addr;
889
890 if (addr + len > flash->size) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100891 printk(BIOS_ERR,
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100892 "Attempt to write 0x%x-0x%x which is out of chip\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600893 (unsigned int)addr, (unsigned int) (addr+len));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100894 return -1;
895 }
896
897 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200898 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100899
900 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200901 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100902 if (block_len > (~addr & 0xff))
903 block_len = (~addr & 0xff) + 1;
904
905 ich_hwseq_set_addr(addr);
906
907 ich_fill_data(buf, block_len);
Arthur Heymans02c99712018-03-28 18:49:27 +0200908 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100909 hsfc &= ~HSFC_FCYCLE; /* clear operation */
910 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
911 hsfc &= ~HSFC_FDBC; /* clear byte count */
912 /* set byte count */
913 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
914 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200915 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100916
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100917 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len)) {
918 printk(BIOS_ERR, "SF: write failure at %x\n",
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100919 addr);
920 return -1;
921 }
922 addr += block_len;
923 buf += block_len;
924 len -= block_len;
925 }
926 printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n",
Martin Rothff744bf2019-10-23 21:46:03 -0600927 (unsigned int) (addr - start), start);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100928 return 0;
929}
930
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700931static const struct spi_flash_ops spi_flash_ops = {
932 .read = ich_hwseq_read,
933 .write = ich_hwseq_write,
934 .erase = ich_hwseq_erase,
935};
936
Furquan Shaikha1491572017-05-17 19:14:06 -0700937static int spi_flash_programmer_probe(const struct spi_slave *spi,
938 struct spi_flash *flash)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100939{
Matt DeVillier4721e472019-05-18 16:05:00 -0500940 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100941
Julius Wernercd49cce2019-03-05 16:53:33 -0800942 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX))
Arthur Heymansc88e3702017-08-20 20:50:17 +0200943 return spi_flash_generic_probe(spi, flash);
944
Furquan Shaikha1491572017-05-17 19:14:06 -0700945 /* Try generic probing first if spi_is_multichip returns 0. */
946 if (!spi_is_multichip() && !spi_flash_generic_probe(spi, flash))
947 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100948
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800949 memcpy(&flash->spi, spi, sizeof(*spi));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100950 flash->name = "Opaque HW-sequencing";
951
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100952 ich_hwseq_set_addr(0);
953 switch ((cntlr->hsfs >> 3) & 3) {
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100954 case 0:
955 flash->sector_size = 256;
956 break;
957 case 1:
958 flash->sector_size = 4096;
959 break;
960 case 2:
961 flash->sector_size = 8192;
962 break;
963 case 3:
964 flash->sector_size = 65536;
965 break;
966 }
967
Stefan Tauner327205d2018-08-26 13:53:16 +0200968 flash->size = 1 << (19 + (cntlr->flcomp & 7));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100969
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700970 flash->ops = &spi_flash_ops;
971
Arthur Heymans02c99712018-03-28 18:49:27 +0200972 if ((cntlr->hsfs & HSFS_FDV) && ((cntlr->flmap0 >> 8) & 3))
Stefan Tauner327205d2018-08-26 13:53:16 +0200973 flash->size += 1 << (19 + ((cntlr->flcomp >> 3) & 7));
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100974 printk(BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100975
Furquan Shaikh30221b42017-05-15 14:35:15 -0700976 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100977}
Furquan Shaikha1491572017-05-17 19:14:06 -0700978
Aaron Durbin851dde82018-04-19 21:15:25 -0600979static int xfer_vectors(const struct spi_slave *slave,
980 struct spi_op vectors[], size_t count)
981{
982 return spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer);
983}
984
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100985#define SPI_FPR_SHIFT 12
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100986#define ICH7_SPI_FPR_MASK 0xfff
987#define ICH9_SPI_FPR_MASK 0x1fff
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100988#define SPI_FPR_BASE_SHIFT 0
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100989#define ICH7_SPI_FPR_LIMIT_SHIFT 12
990#define ICH9_SPI_FPR_LIMIT_SHIFT 16
991#define ICH9_SPI_FPR_RPE (1 << 15) /* Read Protect */
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100992#define SPI_FPR_WPE (1 << 31) /* Write Protect */
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100993
994static u32 spi_fpr(u32 base, u32 limit)
995{
996 u32 ret;
997 u32 mask, limit_shift;
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100998
Julius Wernercd49cce2019-03-05 16:53:33 -0800999 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001000 mask = ICH7_SPI_FPR_MASK;
1001 limit_shift = ICH7_SPI_FPR_LIMIT_SHIFT;
1002 } else {
1003 mask = ICH9_SPI_FPR_MASK;
1004 limit_shift = ICH9_SPI_FPR_LIMIT_SHIFT;
1005 }
1006 ret = ((limit >> SPI_FPR_SHIFT) & mask) << limit_shift;
1007 ret |= ((base >> SPI_FPR_SHIFT) & mask) << SPI_FPR_BASE_SHIFT;
1008 return ret;
1009}
1010
1011/*
1012 * Protect range of SPI flash defined by [start, start+size-1] using Flash
1013 * Protected Range (FPR) register if available.
1014 * Returns 0 on success, -1 on failure of programming fpr registers.
1015 */
1016static int spi_flash_protect(const struct spi_flash *flash,
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301017 const struct region *region,
1018 const enum ctrlr_prot_type type)
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001019{
Matt DeVillier4721e472019-05-18 16:05:00 -05001020 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001021 u32 start = region_offset(region);
1022 u32 end = start + region_sz(region) - 1;
1023 u32 reg;
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301024 u32 protect_mask = 0;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001025 int fpr;
1026 uint32_t *fpr_base;
1027
Arthur Heymans02c99712018-03-28 18:49:27 +02001028 fpr_base = cntlr->fpr;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001029
1030 /* Find first empty FPR */
Arthur Heymans02c99712018-03-28 18:49:27 +02001031 for (fpr = 0; fpr < cntlr->fpr_max; fpr++) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001032 reg = read32(&fpr_base[fpr]);
1033 if (reg == 0)
1034 break;
1035 }
1036
Arthur Heymans02c99712018-03-28 18:49:27 +02001037 if (fpr == cntlr->fpr_max) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001038 printk(BIOS_ERR, "ERROR: No SPI FPR free!\n");
1039 return -1;
1040 }
1041
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301042 switch (type) {
1043 case WRITE_PROTECT:
1044 protect_mask |= SPI_FPR_WPE;
1045 break;
1046 case READ_PROTECT:
Julius Wernercd49cce2019-03-05 16:53:33 -08001047 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX))
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301048 return -1;
1049 protect_mask |= ICH9_SPI_FPR_RPE;
1050 break;
1051 case READ_WRITE_PROTECT:
Julius Wernercd49cce2019-03-05 16:53:33 -08001052 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX))
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301053 return -1;
1054 protect_mask |= (ICH9_SPI_FPR_RPE | SPI_FPR_WPE);
1055 break;
1056 default:
1057 printk(BIOS_ERR, "ERROR: Seeking invalid protection!\n");
1058 return -1;
1059 }
1060
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001061 /* Set protected range base and limit */
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301062 reg = spi_fpr(start, end) | protect_mask;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001063
1064 /* Set the FPR register and verify it is protected */
1065 write32(&fpr_base[fpr], reg);
Arthur Heymansf9572012019-06-11 11:15:10 +02001066 if (reg != read32(&fpr_base[fpr])) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001067 printk(BIOS_ERR, "ERROR: Unable to set SPI FPR %d\n", fpr);
1068 return -1;
1069 }
1070
1071 printk(BIOS_INFO, "%s: FPR %d is enabled for range 0x%08x-0x%08x\n",
1072 __func__, fpr, start, end);
1073 return 0;
1074}
1075
Arthur Heymans92185e32019-05-28 13:06:34 +02001076void spi_finalize_ops(void)
1077{
Matt DeVillier4721e472019-05-18 16:05:00 -05001078 struct ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Arthur Heymans92185e32019-05-28 13:06:34 +02001079 u16 spi_opprefix;
1080 u16 optype = 0;
Arthur Heymans50b4f782019-09-23 11:49:17 +02001081 struct intel_swseq_spi_config spi_config_default = {
Arthur Heymans92185e32019-05-28 13:06:34 +02001082 {0x06, 0x50}, /* OPPREFIXES: EWSR and WREN */
Arthur Heymans50b4f782019-09-23 11:49:17 +02001083 { /* OPCODE and OPTYPE */
Arthur Heymans92185e32019-05-28 13:06:34 +02001084 {0x01, WRITE_NO_ADDR}, /* WRSR: Write Status Register */
1085 {0x02, WRITE_WITH_ADDR}, /* BYPR: Byte Program */
1086 {0x03, READ_WITH_ADDR}, /* READ: Read Data */
1087 {0x05, READ_NO_ADDR}, /* RDSR: Read Status Register */
1088 {0x20, WRITE_WITH_ADDR}, /* SE20: Sector Erase 0x20 */
1089 {0x9f, READ_NO_ADDR}, /* RDID: Read ID */
1090 {0xd8, WRITE_WITH_ADDR}, /* BED8: Block Erase 0xd8 */
1091 {0x0b, READ_WITH_ADDR}, /* FAST: Fast Read */
1092 }
1093 };
Arthur Heymans50b4f782019-09-23 11:49:17 +02001094 struct intel_swseq_spi_config spi_config_aai_write = {
1095 {0x06, 0x50}, /* OPPREFIXES: EWSR and WREN */
1096 { /* OPCODE and OPTYPE */
1097 {0x01, WRITE_NO_ADDR}, /* WRSR: Write Status Register */
1098 {0x02, WRITE_WITH_ADDR}, /* BYPR: Byte Program */
1099 {0x03, READ_WITH_ADDR}, /* READ: Read Data */
1100 {0x05, READ_NO_ADDR}, /* RDSR: Read Status Register */
1101 {0x20, WRITE_WITH_ADDR}, /* SE20: Sector Erase 0x20 */
1102 {0x9f, READ_NO_ADDR}, /* RDID: Read ID */
1103 {0xad, WRITE_NO_ADDR}, /* Auto Address Increment Word Program */
1104 {0x04, WRITE_NO_ADDR} /* Write Disable */
1105 }
1106 };
1107 const struct spi_flash *flash = boot_device_spi_flash();
1108 struct intel_swseq_spi_config *spi_config = &spi_config_default;
Arthur Heymans92185e32019-05-28 13:06:34 +02001109 int i;
1110
Arthur Heymans50b4f782019-09-23 11:49:17 +02001111 /*
1112 * Some older SST SPI flashes support AAI write but use 0xaf opcde for
1113 * that. Flashrom uses the byte program opcode to write those flashes,
1114 * so this configuration is fine too. SST25VF064C (id = 0x4b) is an
1115 * exception.
1116 */
1117 if (flash && flash->vendor == VENDOR_ID_SST && (flash->model & 0x00ff) != 0x4b)
1118 spi_config = &spi_config_aai_write;
1119
Arthur Heymans92185e32019-05-28 13:06:34 +02001120 if (spi_locked())
1121 return;
1122
Arthur Heymans50b4f782019-09-23 11:49:17 +02001123 intel_southbridge_override_spi(spi_config);
Arthur Heymans92185e32019-05-28 13:06:34 +02001124
Arthur Heymans50b4f782019-09-23 11:49:17 +02001125 spi_opprefix = spi_config->opprefixes[0]
1126 | (spi_config->opprefixes[1] << 8);
Arthur Heymans92185e32019-05-28 13:06:34 +02001127 writew_(spi_opprefix, cntlr->preop);
Arthur Heymans50b4f782019-09-23 11:49:17 +02001128 for (i = 0; i < ARRAY_SIZE(spi_config->ops); i++) {
1129 optype |= (spi_config->ops[i].type & 3) << (i * 2);
1130 writeb_(spi_config->ops[i].op, &cntlr->opmenu[i]);
Arthur Heymans92185e32019-05-28 13:06:34 +02001131 }
Nico Hubereaeb0b72019-07-27 13:45:58 +02001132 writew_(optype, cntlr->optype);
Arthur Heymans92185e32019-05-28 13:06:34 +02001133}
1134
1135__weak void intel_southbridge_override_spi(struct intel_swseq_spi_config *spi_config)
1136{
1137}
1138
Furquan Shaikha1491572017-05-17 19:14:06 -07001139static const struct spi_ctrlr spi_ctrlr = {
Aaron Durbin851dde82018-04-19 21:15:25 -06001140 .xfer_vector = xfer_vectors,
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +02001141 .max_xfer_size = member_size(struct ich9_spi_regs, fdata),
Furquan Shaikha1491572017-05-17 19:14:06 -07001142 .flash_probe = spi_flash_programmer_probe,
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001143 .flash_protect = spi_flash_protect,
Furquan Shaikha1491572017-05-17 19:14:06 -07001144};
1145
Furquan Shaikh2cd03f12017-05-18 14:58:32 -07001146const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
1147 {
1148 .ctrlr = &spi_ctrlr,
1149 .bus_start = 0,
1150 .bus_end = 0,
1151 },
1152};
1153
1154const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);