blob: 9bc34140a97cc143eea9a48a2aa6fff9eafaaa93 [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. */
Arthur Heymans02c99712018-03-28 18:49:27 +020019#include <arch/early_variables.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070020#include <stdint.h>
21#include <stdlib.h>
22#include <string.h>
David Hendricksf2612a12014-04-13 16:27:02 -070023#include <bootstate.h>
Furquan Shaikhde705fa2017-04-19 19:27:28 -070024#include <commonlib/helpers.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070025#include <delay.h>
26#include <arch/io.h>
27#include <console/console.h>
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010028#include <device/pci.h>
29#include <spi_flash.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070030
Zheng Bao600784e2013-02-07 17:30:23 +080031#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070032
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010033#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
34#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
35#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
36#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
37
38
Duncan Laurie181bbdd2012-06-23 16:53:57 -070039#ifdef __SMM__
Duncan Laurie181bbdd2012-06-23 16:53:57 -070040#define pci_read_config_byte(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030041 *(targ) = pci_read_config8(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070042#define pci_read_config_word(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030043 *(targ) = pci_read_config16(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070044#define pci_read_config_dword(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030045 *(targ) = pci_read_config32(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070046#define pci_write_config_byte(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030047 pci_write_config8(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070048#define pci_write_config_word(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030049 pci_write_config16(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070050#define pci_write_config_dword(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030051 pci_write_config32(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070052#else /* !__SMM__ */
53#include <device/device.h>
Duncan Laurie181bbdd2012-06-23 16:53:57 -070054#define pci_read_config_byte(dev, reg, targ)\
55 *(targ) = pci_read_config8(dev, reg)
56#define pci_read_config_word(dev, reg, targ)\
57 *(targ) = pci_read_config16(dev, reg)
58#define pci_read_config_dword(dev, reg, targ)\
59 *(targ) = pci_read_config32(dev, reg)
60#define pci_write_config_byte(dev, reg, val)\
61 pci_write_config8(dev, reg, val)
62#define pci_write_config_word(dev, reg, val)\
63 pci_write_config16(dev, reg, val)
64#define pci_write_config_dword(dev, reg, val)\
65 pci_write_config32(dev, reg, val)
66#endif /* !__SMM__ */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070067
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010068static int spi_is_multichip(void);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010069
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070070typedef struct spi_slave ich_spi_slave;
71
Arthur Heymans02c99712018-03-28 18:49:27 +020072static int g_ichspi_lock CAR_GLOBAL = 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070073
74typedef struct ich7_spi_regs {
75 uint16_t spis;
76 uint16_t spic;
77 uint32_t spia;
78 uint64_t spid[8];
79 uint64_t _pad;
80 uint32_t bbar;
81 uint16_t preop;
82 uint16_t optype;
83 uint8_t opmenu[8];
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +010084 uint32_t pbr[3];
Stefan Reinauer6a001132017-07-13 02:20:27 +020085} __packed ich7_spi_regs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070086
87typedef struct ich9_spi_regs {
88 uint32_t bfpr;
89 uint16_t hsfs;
90 uint16_t hsfc;
91 uint32_t faddr;
92 uint32_t _reserved0;
93 uint32_t fdata[16];
94 uint32_t frap;
95 uint32_t freg[5];
96 uint32_t _reserved1[3];
97 uint32_t pr[5];
98 uint32_t _reserved2[2];
99 uint8_t ssfs;
100 uint8_t ssfc[3];
101 uint16_t preop;
102 uint16_t optype;
103 uint8_t opmenu[8];
104 uint32_t bbar;
105 uint8_t _reserved3[12];
106 uint32_t fdoc;
107 uint32_t fdod;
108 uint8_t _reserved4[8];
109 uint32_t afc;
110 uint32_t lvscc;
111 uint32_t uvscc;
112 uint8_t _reserved5[4];
113 uint32_t fpb;
114 uint8_t _reserved6[28];
115 uint32_t srdl;
116 uint32_t srdc;
117 uint32_t srd;
Stefan Reinauer6a001132017-07-13 02:20:27 +0200118} __packed ich9_spi_regs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700119
120typedef struct ich_spi_controller {
121 int locked;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100122 uint32_t flmap0;
Stefan Tauner327205d2018-08-26 13:53:16 +0200123 uint32_t flcomp;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100124 uint32_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700125
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100126 ich9_spi_regs *ich9_spi;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700127 uint8_t *opmenu;
128 int menubytes;
129 uint16_t *preop;
130 uint16_t *optype;
131 uint32_t *addr;
132 uint8_t *data;
133 unsigned databytes;
134 uint8_t *status;
135 uint16_t *control;
136 uint32_t *bbar;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100137 uint32_t *fpr;
138 uint8_t fpr_max;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700139} ich_spi_controller;
140
Arthur Heymans02c99712018-03-28 18:49:27 +0200141static ich_spi_controller g_cntlr CAR_GLOBAL;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700142
143enum {
144 SPIS_SCIP = 0x0001,
145 SPIS_GRANT = 0x0002,
146 SPIS_CDS = 0x0004,
147 SPIS_FCERR = 0x0008,
148 SSFS_AEL = 0x0010,
149 SPIS_LOCK = 0x8000,
150 SPIS_RESERVED_MASK = 0x7ff0,
151 SSFS_RESERVED_MASK = 0x7fe2
152};
153
154enum {
155 SPIC_SCGO = 0x000002,
156 SPIC_ACS = 0x000004,
157 SPIC_SPOP = 0x000008,
158 SPIC_DBC = 0x003f00,
159 SPIC_DS = 0x004000,
160 SPIC_SME = 0x008000,
161 SSFC_SCF_MASK = 0x070000,
162 SSFC_RESERVED = 0xf80000
163};
164
165enum {
166 HSFS_FDONE = 0x0001,
167 HSFS_FCERR = 0x0002,
168 HSFS_AEL = 0x0004,
169 HSFS_BERASE_MASK = 0x0018,
170 HSFS_BERASE_SHIFT = 3,
171 HSFS_SCIP = 0x0020,
172 HSFS_FDOPSS = 0x2000,
173 HSFS_FDV = 0x4000,
174 HSFS_FLOCKDN = 0x8000
175};
176
177enum {
178 HSFC_FGO = 0x0001,
179 HSFC_FCYCLE_MASK = 0x0006,
180 HSFC_FCYCLE_SHIFT = 1,
181 HSFC_FDBC_MASK = 0x3f00,
182 HSFC_FDBC_SHIFT = 8,
183 HSFC_FSMIE = 0x8000
184};
185
186enum {
187 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
188 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
189 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
190 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
191};
192
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600193#if IS_ENABLED(CONFIG_DEBUG_SPI_FLASH)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700194
195static u8 readb_(const void *addr)
196{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800197 u8 v = read8(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100198
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700199 printk(BIOS_DEBUG, "read %2.2x from %4.4x\n",
200 v, ((unsigned) addr & 0xffff) - 0xf020);
201 return v;
202}
203
204static u16 readw_(const void *addr)
205{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800206 u16 v = read16(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100207
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700208 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
209 v, ((unsigned) addr & 0xffff) - 0xf020);
210 return v;
211}
212
213static u32 readl_(const void *addr)
214{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800215 u32 v = read32(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100216
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700217 printk(BIOS_DEBUG, "read %8.8x from %4.4x\n",
218 v, ((unsigned) addr & 0xffff) - 0xf020);
219 return v;
220}
221
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800222static void writeb_(u8 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700223{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800224 write8(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700225 printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n",
226 b, ((unsigned) addr & 0xffff) - 0xf020);
227}
228
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800229static void writew_(u16 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700230{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800231 write16(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700232 printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n",
233 b, ((unsigned) addr & 0xffff) - 0xf020);
234}
235
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800236static void writel_(u32 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700237{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800238 write32(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700239 printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n",
240 b, ((unsigned) addr & 0xffff) - 0xf020);
241}
242
243#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
244
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800245#define readb_(a) read8(a)
246#define readw_(a) read16(a)
247#define readl_(a) read32(a)
248#define writeb_(val, addr) write8(addr, val)
249#define writew_(val, addr) write16(addr, val)
250#define writel_(val, addr) write32(addr, val)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700251
252#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
253
254static void write_reg(const void *value, void *dest, uint32_t size)
255{
256 const uint8_t *bvalue = value;
257 uint8_t *bdest = dest;
258
259 while (size >= 4) {
260 writel_(*(const uint32_t *)bvalue, bdest);
261 bdest += 4; bvalue += 4; size -= 4;
262 }
263 while (size) {
264 writeb_(*bvalue, bdest);
265 bdest++; bvalue++; size--;
266 }
267}
268
269static void read_reg(const void *src, void *value, uint32_t size)
270{
271 const uint8_t *bsrc = src;
272 uint8_t *bvalue = value;
273
274 while (size >= 4) {
275 *(uint32_t *)bvalue = readl_(bsrc);
276 bsrc += 4; bvalue += 4; size -= 4;
277 }
278 while (size) {
279 *bvalue = readb_(bsrc);
280 bsrc++; bvalue++; size--;
281 }
282}
283
284static void ich_set_bbar(uint32_t minaddr)
285{
Arthur Heymans02c99712018-03-28 18:49:27 +0200286 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700287 const uint32_t bbar_mask = 0x00ffff00;
288 uint32_t ichspi_bbar;
289
290 minaddr &= bbar_mask;
Arthur Heymans02c99712018-03-28 18:49:27 +0200291 ichspi_bbar = readl_(cntlr->bbar) & ~bbar_mask;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700292 ichspi_bbar |= minaddr;
Arthur Heymans02c99712018-03-28 18:49:27 +0200293 writel_(ichspi_bbar, cntlr->bbar);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700294}
295
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700296void spi_init(void)
297{
Arthur Heymans02c99712018-03-28 18:49:27 +0200298 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700299 uint8_t *rcrb; /* Root Complex Register Block */
300 uint32_t rcba; /* Root Complex Base Address */
301 uint8_t bios_cntl;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100302 ich9_spi_regs *ich9_spi;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200303 ich7_spi_regs *ich7_spi;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100304 uint16_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700305
Arthur Heymans02c99712018-03-28 18:49:27 +0200306#ifdef __SIMPLE_DEVICE__
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200307 pci_devfn_t dev = PCI_DEV(0, 31, 0);
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700308#else
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300309 struct device *dev = pcidev_on_root(31, 0);
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700310#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700311
312 pci_read_config_dword(dev, 0xf0, &rcba);
313 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
314 rcrb = (uint8_t *)(rcba & 0xffffc000);
Arthur Heymansc88e3702017-08-20 20:50:17 +0200315 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX)) {
316 ich7_spi = (ich7_spi_regs *)(rcrb + 0x3020);
Arthur Heymans02c99712018-03-28 18:49:27 +0200317 cntlr->opmenu = ich7_spi->opmenu;
318 cntlr->menubytes = sizeof(ich7_spi->opmenu);
319 cntlr->optype = &ich7_spi->optype;
320 cntlr->addr = &ich7_spi->spia;
321 cntlr->data = (uint8_t *)ich7_spi->spid;
322 cntlr->databytes = sizeof(ich7_spi->spid);
323 cntlr->status = (uint8_t *)&ich7_spi->spis;
324 car_set_var(g_ichspi_lock, readw_(&ich7_spi->spis) & HSFS_FLOCKDN);
325 cntlr->control = &ich7_spi->spic;
326 cntlr->bbar = &ich7_spi->bbar;
327 cntlr->preop = &ich7_spi->preop;
328 cntlr->fpr = &ich7_spi->pbr[0];
329 cntlr->fpr_max = 3;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200330 } else {
331 ich9_spi = (ich9_spi_regs *)(rcrb + 0x3800);
Arthur Heymans02c99712018-03-28 18:49:27 +0200332 cntlr->ich9_spi = ich9_spi;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200333 hsfs = readw_(&ich9_spi->hsfs);
Arthur Heymans02c99712018-03-28 18:49:27 +0200334 car_set_var(g_ichspi_lock, hsfs & HSFS_FLOCKDN);
335 cntlr->hsfs = hsfs;
336 cntlr->opmenu = ich9_spi->opmenu;
337 cntlr->menubytes = sizeof(ich9_spi->opmenu);
338 cntlr->optype = &ich9_spi->optype;
339 cntlr->addr = &ich9_spi->faddr;
340 cntlr->data = (uint8_t *)ich9_spi->fdata;
341 cntlr->databytes = sizeof(ich9_spi->fdata);
342 cntlr->status = &ich9_spi->ssfs;
343 cntlr->control = (uint16_t *)ich9_spi->ssfc;
344 cntlr->bbar = &ich9_spi->bbar;
345 cntlr->preop = &ich9_spi->preop;
346 cntlr->fpr = &ich9_spi->pr[0];
347 cntlr->fpr_max = 5;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700348
Arthur Heymans02c99712018-03-28 18:49:27 +0200349 if (cntlr->hsfs & HSFS_FDV) {
Patrick Georgic88828d2018-11-26 10:42:59 +0100350 writel_(4, &ich9_spi->fdoc);
Arthur Heymans02c99712018-03-28 18:49:27 +0200351 cntlr->flmap0 = readl_(&ich9_spi->fdod);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100352 writel_(0x1000, &ich9_spi->fdoc);
Stefan Tauner327205d2018-08-26 13:53:16 +0200353 cntlr->flcomp = readl_(&ich9_spi->fdod);
Arthur Heymansc88e3702017-08-20 20:50:17 +0200354 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700355 }
356
357 ich_set_bbar(0);
358
359 /* Disable the BIOS write protect so write commands are allowed. */
360 pci_read_config_byte(dev, 0xdc, &bios_cntl);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100361 /* Deassert SMM BIOS Write Protect Disable. */
362 bios_cntl &= ~(1 << 5);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700363 pci_write_config_byte(dev, 0xdc, bios_cntl | 0x1);
364}
Aaron Durbin4d3de7e2015-09-02 17:34:04 -0500365
David Hendricksf2612a12014-04-13 16:27:02 -0700366static void spi_init_cb(void *unused)
367{
368 spi_init();
369}
370
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500371BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700372
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700373typedef struct spi_transaction {
374 const uint8_t *out;
375 uint32_t bytesout;
376 uint8_t *in;
377 uint32_t bytesin;
378 uint8_t type;
379 uint8_t opcode;
380 uint32_t offset;
381} spi_transaction;
382
383static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
384{
385 trans->out += bytes;
386 trans->bytesout -= bytes;
387}
388
389static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
390{
391 trans->in += bytes;
392 trans->bytesin -= bytes;
393}
394
395static void spi_setup_type(spi_transaction *trans)
396{
397 trans->type = 0xFF;
398
399 /* Try to guess spi type from read/write sizes. */
400 if (trans->bytesin == 0) {
401 if (trans->bytesout > 4)
402 /*
403 * If bytesin = 0 and bytesout > 4, we presume this is
404 * a write data operation, which is accompanied by an
405 * address.
406 */
407 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
408 else
409 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
410 return;
411 }
412
413 if (trans->bytesout == 1) { /* and bytesin is > 0 */
414 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
415 return;
416 }
417
418 if (trans->bytesout == 4) { /* and bytesin is > 0 */
419 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
420 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700421
422 /* Fast read command is called with 5 bytes instead of 4 */
423 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
424 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
425 --trans->bytesout;
426 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700427}
428
429static int spi_setup_opcode(spi_transaction *trans)
430{
Arthur Heymans02c99712018-03-28 18:49:27 +0200431 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700432 uint16_t optypes;
Arthur Heymans02c99712018-03-28 18:49:27 +0200433 uint8_t opmenu[cntlr->menubytes];
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700434
435 trans->opcode = trans->out[0];
436 spi_use_out(trans, 1);
Arthur Heymans02c99712018-03-28 18:49:27 +0200437 if (!car_get_var(g_ichspi_lock)) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700438 /* The lock is off, so just use index 0. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200439 writeb_(trans->opcode, cntlr->opmenu);
440 optypes = readw_(cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700441 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
Arthur Heymans02c99712018-03-28 18:49:27 +0200442 writew_(optypes, cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700443 return 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700444 }
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100445
446 /* The lock is on. See if what we need is on the menu. */
447 uint8_t optype;
448 uint16_t opcode_index;
449
450 /* Write Enable is handled as atomic prefix */
451 if (trans->opcode == SPI_OPCODE_WREN)
452 return 0;
453
454 read_reg(cntlr->opmenu, opmenu, sizeof(opmenu));
455 for (opcode_index = 0; opcode_index < cntlr->menubytes;
456 opcode_index++) {
457 if (opmenu[opcode_index] == trans->opcode)
458 break;
459 }
460
461 if (opcode_index == cntlr->menubytes) {
462 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
463 trans->opcode);
464 return -1;
465 }
466
467 optypes = readw_(cntlr->optype);
468 optype = (optypes >> (opcode_index * 2)) & 0x3;
469 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
470 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
471 trans->bytesout >= 3) {
472 /* We guessed wrong earlier. Fix it up. */
473 trans->type = optype;
474 }
475 if (optype != trans->type) {
476 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
477 optype);
478 return -1;
479 }
480 return opcode_index;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700481}
482
483static int spi_setup_offset(spi_transaction *trans)
484{
485 /* Separate the SPI address and data. */
486 switch (trans->type) {
487 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
488 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
489 return 0;
490 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
491 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
492 trans->offset = ((uint32_t)trans->out[0] << 16) |
493 ((uint32_t)trans->out[1] << 8) |
494 ((uint32_t)trans->out[2] << 0);
495 spi_use_out(trans, 3);
496 return 1;
497 default:
498 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
499 return -1;
500 }
501}
502
503/*
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200504 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700505 * below is True) or 0. In case the wait was for the bit(s) to set - write
506 * those bits back, which would cause resetting them.
507 *
508 * Return the last read status value on success or -1 on failure.
509 */
510static int ich_status_poll(u16 bitmask, int wait_til_set)
511{
Arthur Heymans02c99712018-03-28 18:49:27 +0200512 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200513 int timeout = 600000; /* This will result in 6 seconds */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700514 u16 status = 0;
515
516 while (timeout--) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200517 status = readw_(cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700518 if (wait_til_set ^ ((status & bitmask) == 0)) {
519 if (wait_til_set)
Arthur Heymans02c99712018-03-28 18:49:27 +0200520 writew_((status & bitmask), cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700521 return status;
522 }
523 udelay(10);
524 }
525
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200526 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700527 status, bitmask);
528 return -1;
529}
530
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100531static int spi_is_multichip(void)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100532{
Arthur Heymans02c99712018-03-28 18:49:27 +0200533 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
534 if (!(cntlr->hsfs & HSFS_FDV))
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100535 return 0;
Arthur Heymans02c99712018-03-28 18:49:27 +0200536 return !!((cntlr->flmap0 >> 8) & 3);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100537}
538
Furquan Shaikh94f86992016-12-01 07:12:32 -0800539static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800540 size_t bytesout, void *din, size_t bytesin)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700541{
Arthur Heymans02c99712018-03-28 18:49:27 +0200542 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700543 uint16_t control;
544 int16_t opcode_index;
545 int with_address;
546 int status;
547
548 spi_transaction trans = {
Gabe Black93d9f922014-03-27 21:52:43 -0700549 dout, bytesout,
550 din, bytesin,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700551 0xff, 0xff, 0
552 };
553
554 /* There has to always at least be an opcode. */
Gabe Black93d9f922014-03-27 21:52:43 -0700555 if (!bytesout || !dout) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700556 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
557 return -1;
558 }
559 /* Make sure if we read something we have a place to put it. */
Gabe Black93d9f922014-03-27 21:52:43 -0700560 if (bytesin != 0 && !din) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700561 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
562 return -1;
563 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700564
565 if (ich_status_poll(SPIS_SCIP, 0) == -1)
566 return -1;
567
Arthur Heymans02c99712018-03-28 18:49:27 +0200568 writew_(SPIS_CDS | SPIS_FCERR, cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700569
570 spi_setup_type(&trans);
571 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
572 return -1;
573 if ((with_address = spi_setup_offset(&trans)) < 0)
574 return -1;
575
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700576 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700577 /*
578 * Treat Write Enable as Atomic Pre-Op if possible
579 * in order to prevent the Management Engine from
580 * issuing a transaction between WREN and DATA.
581 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200582 if (!car_get_var(g_ichspi_lock))
583 writew_(trans.opcode, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700584 return 0;
585 }
586
587 /* Preset control fields */
588 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
589
590 /* Issue atomic preop cycle if needed */
Arthur Heymans02c99712018-03-28 18:49:27 +0200591 if (readw_(cntlr->preop))
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700592 control |= SPIC_ACS;
593
594 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700595 /* SPI addresses are 24 bit only */
596 if (with_address)
Arthur Heymans02c99712018-03-28 18:49:27 +0200597 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700598
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700599 /*
600 * This is a 'no data' command (like Write Enable), its
601 * bitesout size was 1, decremented to zero while executing
602 * spi_setup_opcode() above. Tell the chip to send the
603 * command.
604 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200605 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700606
607 /* wait for the result */
608 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
609 if (status == -1)
610 return -1;
611
612 if (status & SPIS_FCERR) {
613 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
614 return -1;
615 }
616
Werner Zehf13a6f92018-11-14 10:55:52 +0100617 goto spi_xfer_exit;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700618 }
619
620 /*
Paul Menzel94782972013-06-29 11:41:27 +0200621 * Check if this is a write command attempting to transfer more bytes
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700622 * than the controller can handle. Iterations for writes are not
623 * supported here because each SPI write command needs to be preceded
624 * and followed by other SPI commands, and this sequence is controlled
625 * by the SPI chip driver.
626 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200627 if (trans.bytesout > cntlr->databytes) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700628 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
Kyösti Mälkki11104952014-06-29 16:17:33 +0300629 " spi_crop_chunk()?\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700630 return -1;
631 }
632
633 /*
634 * Read or write up to databytes bytes at a time until everything has
635 * been sent.
636 */
637 while (trans.bytesout || trans.bytesin) {
638 uint32_t data_length;
639
640 /* SPI addresses are 24 bit only */
Arthur Heymans02c99712018-03-28 18:49:27 +0200641 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700642
643 if (trans.bytesout)
Arthur Heymans02c99712018-03-28 18:49:27 +0200644 data_length = min(trans.bytesout, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700645 else
Arthur Heymans02c99712018-03-28 18:49:27 +0200646 data_length = min(trans.bytesin, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700647
648 /* Program data into FDATA0 to N */
649 if (trans.bytesout) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200650 write_reg(trans.out, cntlr->data, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700651 spi_use_out(&trans, data_length);
652 if (with_address)
653 trans.offset += data_length;
654 }
655
656 /* Add proper control fields' values */
Arthur Heymans02c99712018-03-28 18:49:27 +0200657 control &= ~((cntlr->databytes - 1) << 8);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700658 control |= SPIC_DS;
659 control |= (data_length - 1) << 8;
660
661 /* write it */
Arthur Heymans02c99712018-03-28 18:49:27 +0200662 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700663
664 /* Wait for Cycle Done Status or Flash Cycle Error. */
665 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
666 if (status == -1)
667 return -1;
668
669 if (status & SPIS_FCERR) {
670 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
671 return -1;
672 }
673
674 if (trans.bytesin) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200675 read_reg(cntlr->data, trans.in, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700676 spi_use_in(&trans, data_length);
677 if (with_address)
678 trans.offset += data_length;
679 }
680 }
681
Werner Zehf13a6f92018-11-14 10:55:52 +0100682spi_xfer_exit:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700683 /* Clear atomic preop now that xfer is done */
Arthur Heymans02c99712018-03-28 18:49:27 +0200684 writew_(0, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700685
686 return 0;
687}
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100688
689/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
690static void ich_hwseq_set_addr(uint32_t addr)
691{
Arthur Heymans02c99712018-03-28 18:49:27 +0200692 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
693 uint32_t addr_old = readl_(&cntlr->ich9_spi->faddr) & ~0x01FFFFFF;
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100694
Arthur Heymans02c99712018-03-28 18:49:27 +0200695 writel_((addr & 0x01FFFFFF) | addr_old, &cntlr->ich9_spi->faddr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100696}
697
698/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
699 Resets all error flags in HSFS.
700 Returns 0 if the cycle completes successfully without errors within
701 timeout us, 1 on errors. */
702static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
703 unsigned int len)
704{
Arthur Heymans02c99712018-03-28 18:49:27 +0200705 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100706 uint16_t hsfs;
707 uint32_t addr;
708
709 timeout /= 8; /* scale timeout duration to counter */
Arthur Heymans02c99712018-03-28 18:49:27 +0200710 while ((((hsfs = readw_(&cntlr->ich9_spi->hsfs)) &
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100711 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
712 --timeout) {
713 udelay(8);
714 }
Arthur Heymans02c99712018-03-28 18:49:27 +0200715 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100716
717 if (!timeout) {
718 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200719 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
720 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100721 printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and "
722 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
723 addr, addr + len - 1, addr, len - 1,
724 hsfc, hsfs);
725 return 1;
726 }
727
728 if (hsfs & HSFS_FCERR) {
729 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200730 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
731 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100732 printk(BIOS_ERR, "Transaction error between offset 0x%08x and "
733 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
734 addr, addr + len - 1, addr, len - 1,
735 hsfc, hsfs);
736 return 1;
737 }
738 return 0;
739}
740
741
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800742static int ich_hwseq_erase(const struct spi_flash *flash, u32 offset,
743 size_t len)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100744{
Arthur Heymans02c99712018-03-28 18:49:27 +0200745 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100746 u32 start, end, erase_size;
747 int ret;
748 uint16_t hsfc;
749 uint16_t timeout = 1000 * 60;
750
751 erase_size = flash->sector_size;
752 if (offset % erase_size || len % erase_size) {
753 printk(BIOS_ERR, "SF: Erase offset/length not multiple of erase size\n");
754 return -1;
755 }
756
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800757 ret = spi_claim_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100758 if (ret) {
759 printk(BIOS_ERR, "SF: Unable to claim SPI bus\n");
760 return ret;
761 }
762
763 start = offset;
764 end = start + len;
765
766 while (offset < end) {
767 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
Arthur Heymans02c99712018-03-28 18:49:27 +0200768 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100769
770 ich_hwseq_set_addr(offset);
771
772 offset += erase_size;
773
Arthur Heymans02c99712018-03-28 18:49:27 +0200774 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100775 hsfc &= ~HSFC_FCYCLE; /* clear operation */
776 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
777 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200778 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100779 if (ich_hwseq_wait_for_cycle_complete(timeout, len)) {
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100780 printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size);
781 ret = -1;
782 goto out;
783 }
784 }
785
786 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
787
788out:
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800789 spi_release_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100790 return ret;
791}
792
793static void ich_read_data(uint8_t *data, int len)
794{
Arthur Heymans02c99712018-03-28 18:49:27 +0200795 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100796 int i;
797 uint32_t temp32 = 0;
798
799 for (i = 0; i < len; i++) {
800 if ((i % 4) == 0)
Arthur Heymans02c99712018-03-28 18:49:27 +0200801 temp32 = readl_(cntlr->data + i);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100802
803 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
804 }
805}
806
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800807static int ich_hwseq_read(const struct spi_flash *flash, u32 addr, size_t len,
808 void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100809{
Arthur Heymans02c99712018-03-28 18:49:27 +0200810 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100811 uint16_t hsfc;
812 uint16_t timeout = 100 * 60;
813 uint8_t block_len;
814
815 if (addr + len > flash->size) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100816 printk(BIOS_ERR,
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100817 "Attempt to read %x-%x which is out of chip\n",
818 (unsigned) addr,
819 (unsigned) addr+(unsigned) len);
820 return -1;
821 }
822
823 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200824 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100825
826 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200827 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100828 if (block_len > (~addr & 0xff))
829 block_len = (~addr & 0xff) + 1;
830 ich_hwseq_set_addr(addr);
Arthur Heymans02c99712018-03-28 18:49:27 +0200831 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100832 hsfc &= ~HSFC_FCYCLE; /* set read operation */
833 hsfc &= ~HSFC_FDBC; /* clear byte count */
834 /* set byte count */
835 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
836 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200837 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100838
839 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
840 return 1;
841 ich_read_data(buf, block_len);
842 addr += block_len;
843 buf += block_len;
844 len -= block_len;
845 }
846 return 0;
847}
848
849/* Fill len bytes from the data array into the fdata/spid registers.
850 *
851 * Note that using len > flash->pgm->spi.max_data_write will trash the registers
852 * following the data registers.
853 */
854static void ich_fill_data(const uint8_t *data, int len)
855{
Arthur Heymans02c99712018-03-28 18:49:27 +0200856 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100857 uint32_t temp32 = 0;
858 int i;
859
860 if (len <= 0)
861 return;
862
863 for (i = 0; i < len; i++) {
864 if ((i % 4) == 0)
865 temp32 = 0;
866
867 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
868
869 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200870 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100871 }
872 i--;
873 if ((i % 4) != 3) /* Write remaining data 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
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800877static int ich_hwseq_write(const struct spi_flash *flash, u32 addr, size_t len,
878 const void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100879{
Arthur Heymans02c99712018-03-28 18:49:27 +0200880 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100881 uint16_t hsfc;
882 uint16_t timeout = 100 * 60;
883 uint8_t block_len;
884 uint32_t start = addr;
885
886 if (addr + len > flash->size) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100887 printk(BIOS_ERR,
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100888 "Attempt to write 0x%x-0x%x which is out of chip\n",
889 (unsigned)addr, (unsigned) (addr+len));
890 return -1;
891 }
892
893 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200894 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100895
896 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200897 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100898 if (block_len > (~addr & 0xff))
899 block_len = (~addr & 0xff) + 1;
900
901 ich_hwseq_set_addr(addr);
902
903 ich_fill_data(buf, block_len);
Arthur Heymans02c99712018-03-28 18:49:27 +0200904 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100905 hsfc &= ~HSFC_FCYCLE; /* clear operation */
906 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
907 hsfc &= ~HSFC_FDBC; /* clear byte count */
908 /* set byte count */
909 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
910 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200911 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100912
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100913 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len)) {
914 printk(BIOS_ERR, "SF: write failure at %x\n",
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100915 addr);
916 return -1;
917 }
918 addr += block_len;
919 buf += block_len;
920 len -= block_len;
921 }
922 printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n",
923 (unsigned) (addr - start), start);
924 return 0;
925}
926
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700927static const struct spi_flash_ops spi_flash_ops = {
928 .read = ich_hwseq_read,
929 .write = ich_hwseq_write,
930 .erase = ich_hwseq_erase,
931};
932
Furquan Shaikha1491572017-05-17 19:14:06 -0700933static int spi_flash_programmer_probe(const struct spi_slave *spi,
934 struct spi_flash *flash)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100935{
Arthur Heymans02c99712018-03-28 18:49:27 +0200936 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100937
Arthur Heymansc88e3702017-08-20 20:50:17 +0200938 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX))
939 return spi_flash_generic_probe(spi, flash);
940
Furquan Shaikha1491572017-05-17 19:14:06 -0700941 /* Try generic probing first if spi_is_multichip returns 0. */
942 if (!spi_is_multichip() && !spi_flash_generic_probe(spi, flash))
943 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100944
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800945 memcpy(&flash->spi, spi, sizeof(*spi));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100946 flash->name = "Opaque HW-sequencing";
947
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100948 ich_hwseq_set_addr(0);
949 switch ((cntlr->hsfs >> 3) & 3) {
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100950 case 0:
951 flash->sector_size = 256;
952 break;
953 case 1:
954 flash->sector_size = 4096;
955 break;
956 case 2:
957 flash->sector_size = 8192;
958 break;
959 case 3:
960 flash->sector_size = 65536;
961 break;
962 }
963
Stefan Tauner327205d2018-08-26 13:53:16 +0200964 flash->size = 1 << (19 + (cntlr->flcomp & 7));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100965
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700966 flash->ops = &spi_flash_ops;
967
Arthur Heymans02c99712018-03-28 18:49:27 +0200968 if ((cntlr->hsfs & HSFS_FDV) && ((cntlr->flmap0 >> 8) & 3))
Stefan Tauner327205d2018-08-26 13:53:16 +0200969 flash->size += 1 << (19 + ((cntlr->flcomp >> 3) & 7));
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100970 printk(BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100971
Furquan Shaikh30221b42017-05-15 14:35:15 -0700972 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100973}
Furquan Shaikha1491572017-05-17 19:14:06 -0700974
Aaron Durbin851dde82018-04-19 21:15:25 -0600975static int xfer_vectors(const struct spi_slave *slave,
976 struct spi_op vectors[], size_t count)
977{
978 return spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer);
979}
980
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100981#define SPI_FPR_SHIFT 12
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100982#define ICH7_SPI_FPR_MASK 0xfff
983#define ICH9_SPI_FPR_MASK 0x1fff
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100984#define SPI_FPR_BASE_SHIFT 0
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100985#define ICH7_SPI_FPR_LIMIT_SHIFT 12
986#define ICH9_SPI_FPR_LIMIT_SHIFT 16
987#define ICH9_SPI_FPR_RPE (1 << 15) /* Read Protect */
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100988#define SPI_FPR_WPE (1 << 31) /* Write Protect */
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100989
990static u32 spi_fpr(u32 base, u32 limit)
991{
992 u32 ret;
993 u32 mask, limit_shift;
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100994
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100995 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX)) {
996 mask = ICH7_SPI_FPR_MASK;
997 limit_shift = ICH7_SPI_FPR_LIMIT_SHIFT;
998 } else {
999 mask = ICH9_SPI_FPR_MASK;
1000 limit_shift = ICH9_SPI_FPR_LIMIT_SHIFT;
1001 }
1002 ret = ((limit >> SPI_FPR_SHIFT) & mask) << limit_shift;
1003 ret |= ((base >> SPI_FPR_SHIFT) & mask) << SPI_FPR_BASE_SHIFT;
1004 return ret;
1005}
1006
1007/*
1008 * Protect range of SPI flash defined by [start, start+size-1] using Flash
1009 * Protected Range (FPR) register if available.
1010 * Returns 0 on success, -1 on failure of programming fpr registers.
1011 */
1012static int spi_flash_protect(const struct spi_flash *flash,
1013 const struct region *region)
1014{
Arthur Heymans02c99712018-03-28 18:49:27 +02001015 ich_spi_controller *cntlr = car_get_var_ptr(&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;
1019 int fpr;
1020 uint32_t *fpr_base;
1021
Arthur Heymans02c99712018-03-28 18:49:27 +02001022 fpr_base = cntlr->fpr;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001023
1024 /* Find first empty FPR */
Arthur Heymans02c99712018-03-28 18:49:27 +02001025 for (fpr = 0; fpr < cntlr->fpr_max; fpr++) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001026 reg = read32(&fpr_base[fpr]);
1027 if (reg == 0)
1028 break;
1029 }
1030
Arthur Heymans02c99712018-03-28 18:49:27 +02001031 if (fpr == cntlr->fpr_max) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001032 printk(BIOS_ERR, "ERROR: No SPI FPR free!\n");
1033 return -1;
1034 }
1035
1036 /* Set protected range base and limit */
1037 reg = spi_fpr(start, end) | SPI_FPR_WPE;
1038
1039 /* Set the FPR register and verify it is protected */
1040 write32(&fpr_base[fpr], reg);
1041 reg = read32(&fpr_base[fpr]);
1042 if (!(reg & SPI_FPR_WPE)) {
1043 printk(BIOS_ERR, "ERROR: Unable to set SPI FPR %d\n", fpr);
1044 return -1;
1045 }
1046
1047 printk(BIOS_INFO, "%s: FPR %d is enabled for range 0x%08x-0x%08x\n",
1048 __func__, fpr, start, end);
1049 return 0;
1050}
1051
Furquan Shaikha1491572017-05-17 19:14:06 -07001052static const struct spi_ctrlr spi_ctrlr = {
Aaron Durbin851dde82018-04-19 21:15:25 -06001053 .xfer_vector = xfer_vectors,
Furquan Shaikha1491572017-05-17 19:14:06 -07001054 .max_xfer_size = member_size(ich9_spi_regs, fdata),
1055 .flash_probe = spi_flash_programmer_probe,
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001056 .flash_protect = spi_flash_protect,
Furquan Shaikha1491572017-05-17 19:14:06 -07001057};
1058
Furquan Shaikh2cd03f12017-05-18 14:58:32 -07001059const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
1060 {
1061 .ctrlr = &spi_ctrlr,
1062 .bus_start = 0,
1063 .bus_end = 0,
1064 },
1065};
1066
1067const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);