blob: 60c0b8dd6eef88f56d22602293de18042bf165d3 [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. */
19#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
David Hendricksf2612a12014-04-13 16:27:02 -070022#include <bootstate.h>
Furquan Shaikhde705fa2017-04-19 19:27:28 -070023#include <commonlib/helpers.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070024#include <delay.h>
25#include <arch/io.h>
26#include <console/console.h>
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010027#include <device/pci.h>
28#include <spi_flash.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070029
Zheng Bao600784e2013-02-07 17:30:23 +080030#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070031
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010032#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
33#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
34#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
35#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
36
37
Duncan Laurie181bbdd2012-06-23 16:53:57 -070038#ifdef __SMM__
Duncan Laurie181bbdd2012-06-23 16:53:57 -070039#define pci_read_config_byte(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030040 *(targ) = pci_read_config8(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070041#define pci_read_config_word(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030042 *(targ) = pci_read_config16(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070043#define pci_read_config_dword(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030044 *(targ) = pci_read_config32(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070045#define pci_write_config_byte(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030046 pci_write_config8(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070047#define pci_write_config_word(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030048 pci_write_config16(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070049#define pci_write_config_dword(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030050 pci_write_config32(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070051#else /* !__SMM__ */
52#include <device/device.h>
Duncan Laurie181bbdd2012-06-23 16:53:57 -070053#define pci_read_config_byte(dev, reg, targ)\
54 *(targ) = pci_read_config8(dev, reg)
55#define pci_read_config_word(dev, reg, targ)\
56 *(targ) = pci_read_config16(dev, reg)
57#define pci_read_config_dword(dev, reg, targ)\
58 *(targ) = pci_read_config32(dev, reg)
59#define pci_write_config_byte(dev, reg, val)\
60 pci_write_config8(dev, reg, val)
61#define pci_write_config_word(dev, reg, val)\
62 pci_write_config16(dev, reg, val)
63#define pci_write_config_dword(dev, reg, val)\
64 pci_write_config32(dev, reg, val)
65#endif /* !__SMM__ */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070066
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010067static int spi_is_multichip(void);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010068
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070069typedef struct spi_slave ich_spi_slave;
70
Arthur Heymansf751aee2018-12-29 13:35:26 +010071static int g_ichspi_lock = 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070072
73typedef struct ich7_spi_regs {
74 uint16_t spis;
75 uint16_t spic;
76 uint32_t spia;
77 uint64_t spid[8];
78 uint64_t _pad;
79 uint32_t bbar;
80 uint16_t preop;
81 uint16_t optype;
82 uint8_t opmenu[8];
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +010083 uint32_t pbr[3];
Stefan Reinauer6a001132017-07-13 02:20:27 +020084} __packed ich7_spi_regs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070085
86typedef struct ich9_spi_regs {
87 uint32_t bfpr;
88 uint16_t hsfs;
89 uint16_t hsfc;
90 uint32_t faddr;
91 uint32_t _reserved0;
92 uint32_t fdata[16];
93 uint32_t frap;
94 uint32_t freg[5];
95 uint32_t _reserved1[3];
96 uint32_t pr[5];
97 uint32_t _reserved2[2];
98 uint8_t ssfs;
99 uint8_t ssfc[3];
100 uint16_t preop;
101 uint16_t optype;
102 uint8_t opmenu[8];
103 uint32_t bbar;
104 uint8_t _reserved3[12];
105 uint32_t fdoc;
106 uint32_t fdod;
107 uint8_t _reserved4[8];
108 uint32_t afc;
109 uint32_t lvscc;
110 uint32_t uvscc;
111 uint8_t _reserved5[4];
112 uint32_t fpb;
113 uint8_t _reserved6[28];
114 uint32_t srdl;
115 uint32_t srdc;
116 uint32_t srd;
Stefan Reinauer6a001132017-07-13 02:20:27 +0200117} __packed ich9_spi_regs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700118
119typedef struct ich_spi_controller {
120 int locked;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100121 uint32_t flmap0;
Stefan Tauner327205d2018-08-26 13:53:16 +0200122 uint32_t flcomp;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100123 uint32_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700124
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100125 ich9_spi_regs *ich9_spi;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700126 uint8_t *opmenu;
127 int menubytes;
128 uint16_t *preop;
129 uint16_t *optype;
130 uint32_t *addr;
131 uint8_t *data;
132 unsigned databytes;
133 uint8_t *status;
134 uint16_t *control;
135 uint32_t *bbar;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100136 uint32_t *fpr;
137 uint8_t fpr_max;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700138} ich_spi_controller;
139
Arthur Heymansf751aee2018-12-29 13:35:26 +0100140static ich_spi_controller g_cntlr;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700141
142enum {
143 SPIS_SCIP = 0x0001,
144 SPIS_GRANT = 0x0002,
145 SPIS_CDS = 0x0004,
146 SPIS_FCERR = 0x0008,
147 SSFS_AEL = 0x0010,
148 SPIS_LOCK = 0x8000,
149 SPIS_RESERVED_MASK = 0x7ff0,
150 SSFS_RESERVED_MASK = 0x7fe2
151};
152
153enum {
154 SPIC_SCGO = 0x000002,
155 SPIC_ACS = 0x000004,
156 SPIC_SPOP = 0x000008,
157 SPIC_DBC = 0x003f00,
158 SPIC_DS = 0x004000,
159 SPIC_SME = 0x008000,
160 SSFC_SCF_MASK = 0x070000,
161 SSFC_RESERVED = 0xf80000
162};
163
164enum {
165 HSFS_FDONE = 0x0001,
166 HSFS_FCERR = 0x0002,
167 HSFS_AEL = 0x0004,
168 HSFS_BERASE_MASK = 0x0018,
169 HSFS_BERASE_SHIFT = 3,
170 HSFS_SCIP = 0x0020,
171 HSFS_FDOPSS = 0x2000,
172 HSFS_FDV = 0x4000,
173 HSFS_FLOCKDN = 0x8000
174};
175
176enum {
177 HSFC_FGO = 0x0001,
178 HSFC_FCYCLE_MASK = 0x0006,
179 HSFC_FCYCLE_SHIFT = 1,
180 HSFC_FDBC_MASK = 0x3f00,
181 HSFC_FDBC_SHIFT = 8,
182 HSFC_FSMIE = 0x8000
183};
184
185enum {
186 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
187 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
188 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
189 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
190};
191
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600192#if IS_ENABLED(CONFIG_DEBUG_SPI_FLASH)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700193
194static u8 readb_(const void *addr)
195{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800196 u8 v = read8(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100197
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700198 printk(BIOS_DEBUG, "read %2.2x from %4.4x\n",
199 v, ((unsigned) addr & 0xffff) - 0xf020);
200 return v;
201}
202
203static u16 readw_(const void *addr)
204{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800205 u16 v = read16(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100206
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700207 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
208 v, ((unsigned) addr & 0xffff) - 0xf020);
209 return v;
210}
211
212static u32 readl_(const void *addr)
213{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800214 u32 v = read32(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100215
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700216 printk(BIOS_DEBUG, "read %8.8x from %4.4x\n",
217 v, ((unsigned) addr & 0xffff) - 0xf020);
218 return v;
219}
220
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800221static void writeb_(u8 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700222{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800223 write8(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700224 printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n",
225 b, ((unsigned) addr & 0xffff) - 0xf020);
226}
227
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800228static void writew_(u16 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700229{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800230 write16(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700231 printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n",
232 b, ((unsigned) addr & 0xffff) - 0xf020);
233}
234
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800235static void writel_(u32 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700236{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800237 write32(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700238 printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n",
239 b, ((unsigned) addr & 0xffff) - 0xf020);
240}
241
242#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
243
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800244#define readb_(a) read8(a)
245#define readw_(a) read16(a)
246#define readl_(a) read32(a)
247#define writeb_(val, addr) write8(addr, val)
248#define writew_(val, addr) write16(addr, val)
249#define writel_(val, addr) write32(addr, val)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700250
251#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
252
253static void write_reg(const void *value, void *dest, uint32_t size)
254{
255 const uint8_t *bvalue = value;
256 uint8_t *bdest = dest;
257
258 while (size >= 4) {
259 writel_(*(const uint32_t *)bvalue, bdest);
260 bdest += 4; bvalue += 4; size -= 4;
261 }
262 while (size) {
263 writeb_(*bvalue, bdest);
264 bdest++; bvalue++; size--;
265 }
266}
267
268static void read_reg(const void *src, void *value, uint32_t size)
269{
270 const uint8_t *bsrc = src;
271 uint8_t *bvalue = value;
272
273 while (size >= 4) {
274 *(uint32_t *)bvalue = readl_(bsrc);
275 bsrc += 4; bvalue += 4; size -= 4;
276 }
277 while (size) {
278 *bvalue = readb_(bsrc);
279 bsrc++; bvalue++; size--;
280 }
281}
282
283static void ich_set_bbar(uint32_t minaddr)
284{
Arthur Heymansf751aee2018-12-29 13:35:26 +0100285 ich_spi_controller *cntlr = &g_cntlr;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700286 const uint32_t bbar_mask = 0x00ffff00;
287 uint32_t ichspi_bbar;
288
289 minaddr &= bbar_mask;
Arthur Heymans02c99712018-03-28 18:49:27 +0200290 ichspi_bbar = readl_(cntlr->bbar) & ~bbar_mask;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700291 ichspi_bbar |= minaddr;
Arthur Heymans02c99712018-03-28 18:49:27 +0200292 writel_(ichspi_bbar, cntlr->bbar);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700293}
294
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700295void spi_init(void)
296{
Arthur Heymansf751aee2018-12-29 13:35:26 +0100297 ich_spi_controller *cntlr = &g_cntlr;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700298 uint8_t *rcrb; /* Root Complex Register Block */
299 uint32_t rcba; /* Root Complex Base Address */
300 uint8_t bios_cntl;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100301 ich9_spi_regs *ich9_spi;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200302 ich7_spi_regs *ich7_spi;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100303 uint16_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700304
Arthur Heymans02c99712018-03-28 18:49:27 +0200305#ifdef __SIMPLE_DEVICE__
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200306 pci_devfn_t dev = PCI_DEV(0, 31, 0);
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700307#else
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300308 struct device *dev = pcidev_on_root(31, 0);
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700309#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700310
311 pci_read_config_dword(dev, 0xf0, &rcba);
312 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
313 rcrb = (uint8_t *)(rcba & 0xffffc000);
Arthur Heymansc88e3702017-08-20 20:50:17 +0200314 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX)) {
315 ich7_spi = (ich7_spi_regs *)(rcrb + 0x3020);
Arthur Heymans02c99712018-03-28 18:49:27 +0200316 cntlr->opmenu = ich7_spi->opmenu;
317 cntlr->menubytes = sizeof(ich7_spi->opmenu);
318 cntlr->optype = &ich7_spi->optype;
319 cntlr->addr = &ich7_spi->spia;
320 cntlr->data = (uint8_t *)ich7_spi->spid;
321 cntlr->databytes = sizeof(ich7_spi->spid);
322 cntlr->status = (uint8_t *)&ich7_spi->spis;
Arthur Heymansf751aee2018-12-29 13:35:26 +0100323 g_ichspi_lock = readw_(&ich7_spi->spis) & HSFS_FLOCKDN;
Arthur Heymans02c99712018-03-28 18:49:27 +0200324 cntlr->control = &ich7_spi->spic;
325 cntlr->bbar = &ich7_spi->bbar;
326 cntlr->preop = &ich7_spi->preop;
327 cntlr->fpr = &ich7_spi->pbr[0];
328 cntlr->fpr_max = 3;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200329 } else {
330 ich9_spi = (ich9_spi_regs *)(rcrb + 0x3800);
Arthur Heymans02c99712018-03-28 18:49:27 +0200331 cntlr->ich9_spi = ich9_spi;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200332 hsfs = readw_(&ich9_spi->hsfs);
Arthur Heymansf751aee2018-12-29 13:35:26 +0100333 g_ichspi_lock = hsfs & HSFS_FLOCKDN;
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
358 /* Disable the BIOS write protect so write commands are allowed. */
359 pci_read_config_byte(dev, 0xdc, &bios_cntl);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100360 /* Deassert SMM BIOS Write Protect Disable. */
361 bios_cntl &= ~(1 << 5);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700362 pci_write_config_byte(dev, 0xdc, bios_cntl | 0x1);
363}
Aaron Durbin4d3de7e2015-09-02 17:34:04 -0500364
David Hendricksf2612a12014-04-13 16:27:02 -0700365static void spi_init_cb(void *unused)
366{
367 spi_init();
368}
369
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500370BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700371
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700372typedef struct spi_transaction {
373 const uint8_t *out;
374 uint32_t bytesout;
375 uint8_t *in;
376 uint32_t bytesin;
377 uint8_t type;
378 uint8_t opcode;
379 uint32_t offset;
380} spi_transaction;
381
382static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
383{
384 trans->out += bytes;
385 trans->bytesout -= bytes;
386}
387
388static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
389{
390 trans->in += bytes;
391 trans->bytesin -= bytes;
392}
393
394static void spi_setup_type(spi_transaction *trans)
395{
396 trans->type = 0xFF;
397
398 /* Try to guess spi type from read/write sizes. */
399 if (trans->bytesin == 0) {
400 if (trans->bytesout > 4)
401 /*
402 * If bytesin = 0 and bytesout > 4, we presume this is
403 * a write data operation, which is accompanied by an
404 * address.
405 */
406 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
407 else
408 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
409 return;
410 }
411
412 if (trans->bytesout == 1) { /* and bytesin is > 0 */
413 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
414 return;
415 }
416
417 if (trans->bytesout == 4) { /* and bytesin is > 0 */
418 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
419 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700420
421 /* Fast read command is called with 5 bytes instead of 4 */
422 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
423 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
424 --trans->bytesout;
425 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700426}
427
428static int spi_setup_opcode(spi_transaction *trans)
429{
Arthur Heymansf751aee2018-12-29 13:35:26 +0100430 ich_spi_controller *cntlr = &g_cntlr;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700431 uint16_t optypes;
Arthur Heymans02c99712018-03-28 18:49:27 +0200432 uint8_t opmenu[cntlr->menubytes];
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700433
434 trans->opcode = trans->out[0];
435 spi_use_out(trans, 1);
Arthur Heymansf751aee2018-12-29 13:35:26 +0100436 if (!g_ichspi_lock) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700437 /* The lock is off, so just use index 0. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200438 writeb_(trans->opcode, cntlr->opmenu);
439 optypes = readw_(cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700440 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
Arthur Heymans02c99712018-03-28 18:49:27 +0200441 writew_(optypes, cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700442 return 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700443 }
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100444
445 /* The lock is on. See if what we need is on the menu. */
446 uint8_t optype;
447 uint16_t opcode_index;
448
449 /* Write Enable is handled as atomic prefix */
450 if (trans->opcode == SPI_OPCODE_WREN)
451 return 0;
452
453 read_reg(cntlr->opmenu, opmenu, sizeof(opmenu));
454 for (opcode_index = 0; opcode_index < cntlr->menubytes;
455 opcode_index++) {
456 if (opmenu[opcode_index] == trans->opcode)
457 break;
458 }
459
460 if (opcode_index == cntlr->menubytes) {
461 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
462 trans->opcode);
463 return -1;
464 }
465
466 optypes = readw_(cntlr->optype);
467 optype = (optypes >> (opcode_index * 2)) & 0x3;
468 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
469 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
470 trans->bytesout >= 3) {
471 /* We guessed wrong earlier. Fix it up. */
472 trans->type = optype;
473 }
474 if (optype != trans->type) {
475 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
476 optype);
477 return -1;
478 }
479 return opcode_index;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700480}
481
482static int spi_setup_offset(spi_transaction *trans)
483{
484 /* Separate the SPI address and data. */
485 switch (trans->type) {
486 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
487 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
488 return 0;
489 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
490 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
491 trans->offset = ((uint32_t)trans->out[0] << 16) |
492 ((uint32_t)trans->out[1] << 8) |
493 ((uint32_t)trans->out[2] << 0);
494 spi_use_out(trans, 3);
495 return 1;
496 default:
497 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
498 return -1;
499 }
500}
501
502/*
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200503 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700504 * below is True) or 0. In case the wait was for the bit(s) to set - write
505 * those bits back, which would cause resetting them.
506 *
507 * Return the last read status value on success or -1 on failure.
508 */
509static int ich_status_poll(u16 bitmask, int wait_til_set)
510{
Arthur Heymansf751aee2018-12-29 13:35:26 +0100511 ich_spi_controller *cntlr = &g_cntlr;
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200512 int timeout = 600000; /* This will result in 6 seconds */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700513 u16 status = 0;
514
515 while (timeout--) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200516 status = readw_(cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700517 if (wait_til_set ^ ((status & bitmask) == 0)) {
518 if (wait_til_set)
Arthur Heymans02c99712018-03-28 18:49:27 +0200519 writew_((status & bitmask), cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700520 return status;
521 }
522 udelay(10);
523 }
524
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200525 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700526 status, bitmask);
527 return -1;
528}
529
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100530static int spi_is_multichip(void)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100531{
Arthur Heymansf751aee2018-12-29 13:35:26 +0100532 ich_spi_controller *cntlr = &g_cntlr;
Arthur Heymans02c99712018-03-28 18:49:27 +0200533 if (!(cntlr->hsfs & HSFS_FDV))
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100534 return 0;
Arthur Heymans02c99712018-03-28 18:49:27 +0200535 return !!((cntlr->flmap0 >> 8) & 3);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100536}
537
Furquan Shaikh94f86992016-12-01 07:12:32 -0800538static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800539 size_t bytesout, void *din, size_t bytesin)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700540{
Arthur Heymansf751aee2018-12-29 13:35:26 +0100541 ich_spi_controller *cntlr = &g_cntlr;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700542 uint16_t control;
543 int16_t opcode_index;
544 int with_address;
545 int status;
546
547 spi_transaction trans = {
Gabe Black93d9f922014-03-27 21:52:43 -0700548 dout, bytesout,
549 din, bytesin,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700550 0xff, 0xff, 0
551 };
552
553 /* There has to always at least be an opcode. */
Gabe Black93d9f922014-03-27 21:52:43 -0700554 if (!bytesout || !dout) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700555 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
556 return -1;
557 }
558 /* Make sure if we read something we have a place to put it. */
Gabe Black93d9f922014-03-27 21:52:43 -0700559 if (bytesin != 0 && !din) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700560 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
561 return -1;
562 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700563
564 if (ich_status_poll(SPIS_SCIP, 0) == -1)
565 return -1;
566
Arthur Heymans02c99712018-03-28 18:49:27 +0200567 writew_(SPIS_CDS | SPIS_FCERR, cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700568
569 spi_setup_type(&trans);
570 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
571 return -1;
572 if ((with_address = spi_setup_offset(&trans)) < 0)
573 return -1;
574
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700575 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700576 /*
577 * Treat Write Enable as Atomic Pre-Op if possible
578 * in order to prevent the Management Engine from
579 * issuing a transaction between WREN and DATA.
580 */
Arthur Heymansf751aee2018-12-29 13:35:26 +0100581 if (!g_ichspi_lock)
Arthur Heymans02c99712018-03-28 18:49:27 +0200582 writew_(trans.opcode, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700583 return 0;
584 }
585
586 /* Preset control fields */
587 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
588
589 /* Issue atomic preop cycle if needed */
Arthur Heymans02c99712018-03-28 18:49:27 +0200590 if (readw_(cntlr->preop))
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700591 control |= SPIC_ACS;
592
593 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700594 /* SPI addresses are 24 bit only */
595 if (with_address)
Arthur Heymans02c99712018-03-28 18:49:27 +0200596 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700597
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700598 /*
599 * This is a 'no data' command (like Write Enable), its
600 * bitesout size was 1, decremented to zero while executing
601 * spi_setup_opcode() above. Tell the chip to send the
602 * command.
603 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200604 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700605
606 /* wait for the result */
607 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
608 if (status == -1)
609 return -1;
610
611 if (status & SPIS_FCERR) {
612 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
613 return -1;
614 }
615
Werner Zehf13a6f92018-11-14 10:55:52 +0100616 goto spi_xfer_exit;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700617 }
618
619 /*
Paul Menzel94782972013-06-29 11:41:27 +0200620 * Check if this is a write command attempting to transfer more bytes
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700621 * than the controller can handle. Iterations for writes are not
622 * supported here because each SPI write command needs to be preceded
623 * and followed by other SPI commands, and this sequence is controlled
624 * by the SPI chip driver.
625 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200626 if (trans.bytesout > cntlr->databytes) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700627 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
Kyösti Mälkki11104952014-06-29 16:17:33 +0300628 " spi_crop_chunk()?\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700629 return -1;
630 }
631
632 /*
633 * Read or write up to databytes bytes at a time until everything has
634 * been sent.
635 */
636 while (trans.bytesout || trans.bytesin) {
637 uint32_t data_length;
638
639 /* SPI addresses are 24 bit only */
Arthur Heymans02c99712018-03-28 18:49:27 +0200640 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700641
642 if (trans.bytesout)
Arthur Heymans02c99712018-03-28 18:49:27 +0200643 data_length = min(trans.bytesout, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700644 else
Arthur Heymans02c99712018-03-28 18:49:27 +0200645 data_length = min(trans.bytesin, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700646
647 /* Program data into FDATA0 to N */
648 if (trans.bytesout) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200649 write_reg(trans.out, cntlr->data, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700650 spi_use_out(&trans, data_length);
651 if (with_address)
652 trans.offset += data_length;
653 }
654
655 /* Add proper control fields' values */
Arthur Heymans02c99712018-03-28 18:49:27 +0200656 control &= ~((cntlr->databytes - 1) << 8);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700657 control |= SPIC_DS;
658 control |= (data_length - 1) << 8;
659
660 /* write it */
Arthur Heymans02c99712018-03-28 18:49:27 +0200661 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700662
663 /* Wait for Cycle Done Status or Flash Cycle Error. */
664 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
665 if (status == -1)
666 return -1;
667
668 if (status & SPIS_FCERR) {
669 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
670 return -1;
671 }
672
673 if (trans.bytesin) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200674 read_reg(cntlr->data, trans.in, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700675 spi_use_in(&trans, data_length);
676 if (with_address)
677 trans.offset += data_length;
678 }
679 }
680
Werner Zehf13a6f92018-11-14 10:55:52 +0100681spi_xfer_exit:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700682 /* Clear atomic preop now that xfer is done */
Arthur Heymans02c99712018-03-28 18:49:27 +0200683 writew_(0, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700684
685 return 0;
686}
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100687
688/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
689static void ich_hwseq_set_addr(uint32_t addr)
690{
Arthur Heymansf751aee2018-12-29 13:35:26 +0100691 ich_spi_controller *cntlr = &g_cntlr;
Arthur Heymans02c99712018-03-28 18:49:27 +0200692 uint32_t addr_old = readl_(&cntlr->ich9_spi->faddr) & ~0x01FFFFFF;
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100693
Arthur Heymans02c99712018-03-28 18:49:27 +0200694 writel_((addr & 0x01FFFFFF) | addr_old, &cntlr->ich9_spi->faddr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100695}
696
697/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
698 Resets all error flags in HSFS.
699 Returns 0 if the cycle completes successfully without errors within
700 timeout us, 1 on errors. */
701static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
702 unsigned int len)
703{
Arthur Heymansf751aee2018-12-29 13:35:26 +0100704 ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100705 uint16_t hsfs;
706 uint32_t addr;
707
708 timeout /= 8; /* scale timeout duration to counter */
Arthur Heymans02c99712018-03-28 18:49:27 +0200709 while ((((hsfs = readw_(&cntlr->ich9_spi->hsfs)) &
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100710 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
711 --timeout) {
712 udelay(8);
713 }
Arthur Heymans02c99712018-03-28 18:49:27 +0200714 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100715
716 if (!timeout) {
717 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200718 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
719 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100720 printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and "
721 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
722 addr, addr + len - 1, addr, len - 1,
723 hsfc, hsfs);
724 return 1;
725 }
726
727 if (hsfs & HSFS_FCERR) {
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 error 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 return 0;
738}
739
740
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800741static int ich_hwseq_erase(const struct spi_flash *flash, u32 offset,
742 size_t len)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100743{
Arthur Heymansf751aee2018-12-29 13:35:26 +0100744 ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100745 u32 start, end, erase_size;
746 int ret;
747 uint16_t hsfc;
748 uint16_t timeout = 1000 * 60;
749
750 erase_size = flash->sector_size;
751 if (offset % erase_size || len % erase_size) {
752 printk(BIOS_ERR, "SF: Erase offset/length not multiple of erase size\n");
753 return -1;
754 }
755
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800756 ret = spi_claim_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100757 if (ret) {
758 printk(BIOS_ERR, "SF: Unable to claim SPI bus\n");
759 return ret;
760 }
761
762 start = offset;
763 end = start + len;
764
765 while (offset < end) {
766 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
Arthur Heymans02c99712018-03-28 18:49:27 +0200767 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100768
769 ich_hwseq_set_addr(offset);
770
771 offset += erase_size;
772
Arthur Heymans02c99712018-03-28 18:49:27 +0200773 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100774 hsfc &= ~HSFC_FCYCLE; /* clear operation */
775 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
776 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200777 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100778 if (ich_hwseq_wait_for_cycle_complete(timeout, len)) {
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100779 printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size);
780 ret = -1;
781 goto out;
782 }
783 }
784
785 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
786
787out:
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800788 spi_release_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100789 return ret;
790}
791
792static void ich_read_data(uint8_t *data, int len)
793{
Arthur Heymansf751aee2018-12-29 13:35:26 +0100794 ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100795 int i;
796 uint32_t temp32 = 0;
797
798 for (i = 0; i < len; i++) {
799 if ((i % 4) == 0)
Arthur Heymans02c99712018-03-28 18:49:27 +0200800 temp32 = readl_(cntlr->data + i);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100801
802 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
803 }
804}
805
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800806static int ich_hwseq_read(const struct spi_flash *flash, u32 addr, size_t len,
807 void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100808{
Arthur Heymansf751aee2018-12-29 13:35:26 +0100809 ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100810 uint16_t hsfc;
811 uint16_t timeout = 100 * 60;
812 uint8_t block_len;
813
814 if (addr + len > flash->size) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100815 printk(BIOS_ERR,
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100816 "Attempt to read %x-%x which is out of chip\n",
817 (unsigned) addr,
818 (unsigned) addr+(unsigned) len);
819 return -1;
820 }
821
822 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200823 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100824
825 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200826 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100827 if (block_len > (~addr & 0xff))
828 block_len = (~addr & 0xff) + 1;
829 ich_hwseq_set_addr(addr);
Arthur Heymans02c99712018-03-28 18:49:27 +0200830 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100831 hsfc &= ~HSFC_FCYCLE; /* set read operation */
832 hsfc &= ~HSFC_FDBC; /* clear byte count */
833 /* set byte count */
834 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
835 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200836 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100837
838 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
839 return 1;
840 ich_read_data(buf, block_len);
841 addr += block_len;
842 buf += block_len;
843 len -= block_len;
844 }
845 return 0;
846}
847
848/* Fill len bytes from the data array into the fdata/spid registers.
849 *
850 * Note that using len > flash->pgm->spi.max_data_write will trash the registers
851 * following the data registers.
852 */
853static void ich_fill_data(const uint8_t *data, int len)
854{
Arthur Heymansf751aee2018-12-29 13:35:26 +0100855 ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100856 uint32_t temp32 = 0;
857 int i;
858
859 if (len <= 0)
860 return;
861
862 for (i = 0; i < len; i++) {
863 if ((i % 4) == 0)
864 temp32 = 0;
865
866 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
867
868 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200869 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100870 }
871 i--;
872 if ((i % 4) != 3) /* Write remaining data to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200873 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100874}
875
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800876static int ich_hwseq_write(const struct spi_flash *flash, u32 addr, size_t len,
877 const void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100878{
Arthur Heymansf751aee2018-12-29 13:35:26 +0100879 ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100880 uint16_t hsfc;
881 uint16_t timeout = 100 * 60;
882 uint8_t block_len;
883 uint32_t start = addr;
884
885 if (addr + len > flash->size) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100886 printk(BIOS_ERR,
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100887 "Attempt to write 0x%x-0x%x which is out of chip\n",
888 (unsigned)addr, (unsigned) (addr+len));
889 return -1;
890 }
891
892 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200893 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100894
895 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200896 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100897 if (block_len > (~addr & 0xff))
898 block_len = (~addr & 0xff) + 1;
899
900 ich_hwseq_set_addr(addr);
901
902 ich_fill_data(buf, block_len);
Arthur Heymans02c99712018-03-28 18:49:27 +0200903 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100904 hsfc &= ~HSFC_FCYCLE; /* clear operation */
905 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
906 hsfc &= ~HSFC_FDBC; /* clear byte count */
907 /* set byte count */
908 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
909 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200910 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100911
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100912 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len)) {
913 printk(BIOS_ERR, "SF: write failure at %x\n",
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100914 addr);
915 return -1;
916 }
917 addr += block_len;
918 buf += block_len;
919 len -= block_len;
920 }
921 printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n",
922 (unsigned) (addr - start), start);
923 return 0;
924}
925
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700926static const struct spi_flash_ops spi_flash_ops = {
927 .read = ich_hwseq_read,
928 .write = ich_hwseq_write,
929 .erase = ich_hwseq_erase,
930};
931
Furquan Shaikha1491572017-05-17 19:14:06 -0700932static int spi_flash_programmer_probe(const struct spi_slave *spi,
933 struct spi_flash *flash)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100934{
Arthur Heymansf751aee2018-12-29 13:35:26 +0100935 ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100936
Arthur Heymansc88e3702017-08-20 20:50:17 +0200937 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX))
938 return spi_flash_generic_probe(spi, flash);
939
Furquan Shaikha1491572017-05-17 19:14:06 -0700940 /* Try generic probing first if spi_is_multichip returns 0. */
941 if (!spi_is_multichip() && !spi_flash_generic_probe(spi, flash))
942 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100943
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800944 memcpy(&flash->spi, spi, sizeof(*spi));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100945 flash->name = "Opaque HW-sequencing";
946
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100947 ich_hwseq_set_addr(0);
948 switch ((cntlr->hsfs >> 3) & 3) {
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100949 case 0:
950 flash->sector_size = 256;
951 break;
952 case 1:
953 flash->sector_size = 4096;
954 break;
955 case 2:
956 flash->sector_size = 8192;
957 break;
958 case 3:
959 flash->sector_size = 65536;
960 break;
961 }
962
Stefan Tauner327205d2018-08-26 13:53:16 +0200963 flash->size = 1 << (19 + (cntlr->flcomp & 7));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100964
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700965 flash->ops = &spi_flash_ops;
966
Arthur Heymans02c99712018-03-28 18:49:27 +0200967 if ((cntlr->hsfs & HSFS_FDV) && ((cntlr->flmap0 >> 8) & 3))
Stefan Tauner327205d2018-08-26 13:53:16 +0200968 flash->size += 1 << (19 + ((cntlr->flcomp >> 3) & 7));
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100969 printk(BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100970
Furquan Shaikh30221b42017-05-15 14:35:15 -0700971 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100972}
Furquan Shaikha1491572017-05-17 19:14:06 -0700973
Aaron Durbin851dde82018-04-19 21:15:25 -0600974static int xfer_vectors(const struct spi_slave *slave,
975 struct spi_op vectors[], size_t count)
976{
977 return spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer);
978}
979
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100980#define SPI_FPR_SHIFT 12
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100981#define ICH7_SPI_FPR_MASK 0xfff
982#define ICH9_SPI_FPR_MASK 0x1fff
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100983#define SPI_FPR_BASE_SHIFT 0
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100984#define ICH7_SPI_FPR_LIMIT_SHIFT 12
985#define ICH9_SPI_FPR_LIMIT_SHIFT 16
986#define ICH9_SPI_FPR_RPE (1 << 15) /* Read Protect */
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100987#define SPI_FPR_WPE (1 << 31) /* Write Protect */
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100988
989static u32 spi_fpr(u32 base, u32 limit)
990{
991 u32 ret;
992 u32 mask, limit_shift;
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100993
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100994 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX)) {
995 mask = ICH7_SPI_FPR_MASK;
996 limit_shift = ICH7_SPI_FPR_LIMIT_SHIFT;
997 } else {
998 mask = ICH9_SPI_FPR_MASK;
999 limit_shift = ICH9_SPI_FPR_LIMIT_SHIFT;
1000 }
1001 ret = ((limit >> SPI_FPR_SHIFT) & mask) << limit_shift;
1002 ret |= ((base >> SPI_FPR_SHIFT) & mask) << SPI_FPR_BASE_SHIFT;
1003 return ret;
1004}
1005
1006/*
1007 * Protect range of SPI flash defined by [start, start+size-1] using Flash
1008 * Protected Range (FPR) register if available.
1009 * Returns 0 on success, -1 on failure of programming fpr registers.
1010 */
1011static int spi_flash_protect(const struct spi_flash *flash,
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301012 const struct region *region,
1013 const enum ctrlr_prot_type type)
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001014{
Arthur Heymansf751aee2018-12-29 13:35:26 +01001015 ich_spi_controller *cntlr = &g_cntlr;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001016 u32 start = region_offset(region);
1017 u32 end = start + region_sz(region) - 1;
1018 u32 reg;
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301019 u32 protect_mask = 0;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001020 int fpr;
1021 uint32_t *fpr_base;
1022
Arthur Heymans02c99712018-03-28 18:49:27 +02001023 fpr_base = cntlr->fpr;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001024
1025 /* Find first empty FPR */
Arthur Heymans02c99712018-03-28 18:49:27 +02001026 for (fpr = 0; fpr < cntlr->fpr_max; fpr++) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001027 reg = read32(&fpr_base[fpr]);
1028 if (reg == 0)
1029 break;
1030 }
1031
Arthur Heymans02c99712018-03-28 18:49:27 +02001032 if (fpr == cntlr->fpr_max) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001033 printk(BIOS_ERR, "ERROR: No SPI FPR free!\n");
1034 return -1;
1035 }
1036
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301037 switch (type) {
1038 case WRITE_PROTECT:
1039 protect_mask |= SPI_FPR_WPE;
1040 break;
1041 case READ_PROTECT:
1042 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX))
1043 return -1;
1044 protect_mask |= ICH9_SPI_FPR_RPE;
1045 break;
1046 case READ_WRITE_PROTECT:
1047 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX))
1048 return -1;
1049 protect_mask |= (ICH9_SPI_FPR_RPE | SPI_FPR_WPE);
1050 break;
1051 default:
1052 printk(BIOS_ERR, "ERROR: Seeking invalid protection!\n");
1053 return -1;
1054 }
1055
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001056 /* Set protected range base and limit */
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301057 reg = spi_fpr(start, end) | protect_mask;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001058
1059 /* Set the FPR register and verify it is protected */
1060 write32(&fpr_base[fpr], reg);
1061 reg = read32(&fpr_base[fpr]);
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301062 if (!(reg & protect_mask)) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001063 printk(BIOS_ERR, "ERROR: Unable to set SPI FPR %d\n", fpr);
1064 return -1;
1065 }
1066
1067 printk(BIOS_INFO, "%s: FPR %d is enabled for range 0x%08x-0x%08x\n",
1068 __func__, fpr, start, end);
1069 return 0;
1070}
1071
Furquan Shaikha1491572017-05-17 19:14:06 -07001072static const struct spi_ctrlr spi_ctrlr = {
Aaron Durbin851dde82018-04-19 21:15:25 -06001073 .xfer_vector = xfer_vectors,
Furquan Shaikha1491572017-05-17 19:14:06 -07001074 .max_xfer_size = member_size(ich9_spi_regs, fdata),
1075 .flash_probe = spi_flash_programmer_probe,
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001076 .flash_protect = spi_flash_protect,
Furquan Shaikha1491572017-05-17 19:14:06 -07001077};
1078
Furquan Shaikh2cd03f12017-05-18 14:58:32 -07001079const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
1080 {
1081 .ctrlr = &spi_ctrlr,
1082 .bus_start = 0,
1083 .bus_end = 0,
1084 },
1085};
1086
1087const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);