blob: 96b580e52fb6b60aee0dcd27fa46441675f068ba [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>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070028#include <device/pci_ids.h>
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010029#include <device/pci.h>
30#include <spi_flash.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070031
Zheng Bao600784e2013-02-07 17:30:23 +080032#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070033
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010034#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
35#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
36#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
37#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
38
39
Duncan Laurie181bbdd2012-06-23 16:53:57 -070040#ifdef __SMM__
Duncan Laurie181bbdd2012-06-23 16:53:57 -070041#define pci_read_config_byte(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030042 *(targ) = pci_read_config8(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070043#define pci_read_config_word(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030044 *(targ) = pci_read_config16(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070045#define pci_read_config_dword(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030046 *(targ) = pci_read_config32(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070047#define pci_write_config_byte(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030048 pci_write_config8(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070049#define pci_write_config_word(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030050 pci_write_config16(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070051#define pci_write_config_dword(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030052 pci_write_config32(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070053#else /* !__SMM__ */
54#include <device/device.h>
Duncan Laurie181bbdd2012-06-23 16:53:57 -070055#define pci_read_config_byte(dev, reg, targ)\
56 *(targ) = pci_read_config8(dev, reg)
57#define pci_read_config_word(dev, reg, targ)\
58 *(targ) = pci_read_config16(dev, reg)
59#define pci_read_config_dword(dev, reg, targ)\
60 *(targ) = pci_read_config32(dev, reg)
61#define pci_write_config_byte(dev, reg, val)\
62 pci_write_config8(dev, reg, val)
63#define pci_write_config_word(dev, reg, val)\
64 pci_write_config16(dev, reg, val)
65#define pci_write_config_dword(dev, reg, val)\
66 pci_write_config32(dev, reg, val)
67#endif /* !__SMM__ */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070068
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010069static int spi_is_multichip(void);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010070
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070071typedef struct spi_slave ich_spi_slave;
72
Arthur Heymans02c99712018-03-28 18:49:27 +020073static int g_ichspi_lock CAR_GLOBAL = 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070074
75typedef struct ich7_spi_regs {
76 uint16_t spis;
77 uint16_t spic;
78 uint32_t spia;
79 uint64_t spid[8];
80 uint64_t _pad;
81 uint32_t bbar;
82 uint16_t preop;
83 uint16_t optype;
84 uint8_t opmenu[8];
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +010085 uint32_t pbr[3];
Stefan Reinauer6a001132017-07-13 02:20:27 +020086} __packed ich7_spi_regs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070087
88typedef struct ich9_spi_regs {
89 uint32_t bfpr;
90 uint16_t hsfs;
91 uint16_t hsfc;
92 uint32_t faddr;
93 uint32_t _reserved0;
94 uint32_t fdata[16];
95 uint32_t frap;
96 uint32_t freg[5];
97 uint32_t _reserved1[3];
98 uint32_t pr[5];
99 uint32_t _reserved2[2];
100 uint8_t ssfs;
101 uint8_t ssfc[3];
102 uint16_t preop;
103 uint16_t optype;
104 uint8_t opmenu[8];
105 uint32_t bbar;
106 uint8_t _reserved3[12];
107 uint32_t fdoc;
108 uint32_t fdod;
109 uint8_t _reserved4[8];
110 uint32_t afc;
111 uint32_t lvscc;
112 uint32_t uvscc;
113 uint8_t _reserved5[4];
114 uint32_t fpb;
115 uint8_t _reserved6[28];
116 uint32_t srdl;
117 uint32_t srdc;
118 uint32_t srd;
Stefan Reinauer6a001132017-07-13 02:20:27 +0200119} __packed ich9_spi_regs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700120
121typedef struct ich_spi_controller {
122 int locked;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100123 uint32_t flmap0;
124 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);
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);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700206 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
207 v, ((unsigned) addr & 0xffff) - 0xf020);
208 return v;
209}
210
211static u32 readl_(const void *addr)
212{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800213 u32 v = read32(addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700214 printk(BIOS_DEBUG, "read %8.8x from %4.4x\n",
215 v, ((unsigned) addr & 0xffff) - 0xf020);
216 return v;
217}
218
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800219static void writeb_(u8 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700220{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800221 write8(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700222 printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n",
223 b, ((unsigned) addr & 0xffff) - 0xf020);
224}
225
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800226static void writew_(u16 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700227{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800228 write16(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700229 printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n",
230 b, ((unsigned) addr & 0xffff) - 0xf020);
231}
232
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800233static void writel_(u32 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700234{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800235 write32(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700236 printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n",
237 b, ((unsigned) addr & 0xffff) - 0xf020);
238}
239
240#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
241
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800242#define readb_(a) read8(a)
243#define readw_(a) read16(a)
244#define readl_(a) read32(a)
245#define writeb_(val, addr) write8(addr, val)
246#define writew_(val, addr) write16(addr, val)
247#define writel_(val, addr) write32(addr, val)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700248
249#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
250
251static void write_reg(const void *value, void *dest, uint32_t size)
252{
253 const uint8_t *bvalue = value;
254 uint8_t *bdest = dest;
255
256 while (size >= 4) {
257 writel_(*(const uint32_t *)bvalue, bdest);
258 bdest += 4; bvalue += 4; size -= 4;
259 }
260 while (size) {
261 writeb_(*bvalue, bdest);
262 bdest++; bvalue++; size--;
263 }
264}
265
266static void read_reg(const void *src, void *value, uint32_t size)
267{
268 const uint8_t *bsrc = src;
269 uint8_t *bvalue = value;
270
271 while (size >= 4) {
272 *(uint32_t *)bvalue = readl_(bsrc);
273 bsrc += 4; bvalue += 4; size -= 4;
274 }
275 while (size) {
276 *bvalue = readb_(bsrc);
277 bsrc++; bvalue++; size--;
278 }
279}
280
281static void ich_set_bbar(uint32_t minaddr)
282{
Arthur Heymans02c99712018-03-28 18:49:27 +0200283 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700284 const uint32_t bbar_mask = 0x00ffff00;
285 uint32_t ichspi_bbar;
286
287 minaddr &= bbar_mask;
Arthur Heymans02c99712018-03-28 18:49:27 +0200288 ichspi_bbar = readl_(cntlr->bbar) & ~bbar_mask;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700289 ichspi_bbar |= minaddr;
Arthur Heymans02c99712018-03-28 18:49:27 +0200290 writel_(ichspi_bbar, cntlr->bbar);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700291}
292
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700293void spi_init(void)
294{
Arthur Heymans02c99712018-03-28 18:49:27 +0200295 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700296 uint8_t *rcrb; /* Root Complex Register Block */
297 uint32_t rcba; /* Root Complex Base Address */
298 uint8_t bios_cntl;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100299 ich9_spi_regs *ich9_spi;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200300 ich7_spi_regs *ich7_spi;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100301 uint16_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700302
Arthur Heymans02c99712018-03-28 18:49:27 +0200303#ifdef __SIMPLE_DEVICE__
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200304 pci_devfn_t dev = PCI_DEV(0, 31, 0);
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700305#else
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200306 struct device *dev = dev_find_slot(0, PCI_DEVFN(31, 0));
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700307#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700308
309 pci_read_config_dword(dev, 0xf0, &rcba);
310 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
311 rcrb = (uint8_t *)(rcba & 0xffffc000);
Arthur Heymansc88e3702017-08-20 20:50:17 +0200312 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX)) {
313 ich7_spi = (ich7_spi_regs *)(rcrb + 0x3020);
Arthur Heymans02c99712018-03-28 18:49:27 +0200314 cntlr->opmenu = ich7_spi->opmenu;
315 cntlr->menubytes = sizeof(ich7_spi->opmenu);
316 cntlr->optype = &ich7_spi->optype;
317 cntlr->addr = &ich7_spi->spia;
318 cntlr->data = (uint8_t *)ich7_spi->spid;
319 cntlr->databytes = sizeof(ich7_spi->spid);
320 cntlr->status = (uint8_t *)&ich7_spi->spis;
321 car_set_var(g_ichspi_lock, readw_(&ich7_spi->spis) & HSFS_FLOCKDN);
322 cntlr->control = &ich7_spi->spic;
323 cntlr->bbar = &ich7_spi->bbar;
324 cntlr->preop = &ich7_spi->preop;
325 cntlr->fpr = &ich7_spi->pbr[0];
326 cntlr->fpr_max = 3;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200327 } else {
328 ich9_spi = (ich9_spi_regs *)(rcrb + 0x3800);
Arthur Heymans02c99712018-03-28 18:49:27 +0200329 cntlr->ich9_spi = ich9_spi;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200330 hsfs = readw_(&ich9_spi->hsfs);
Arthur Heymans02c99712018-03-28 18:49:27 +0200331 car_set_var(g_ichspi_lock, hsfs & HSFS_FLOCKDN);
332 cntlr->hsfs = hsfs;
333 cntlr->opmenu = ich9_spi->opmenu;
334 cntlr->menubytes = sizeof(ich9_spi->opmenu);
335 cntlr->optype = &ich9_spi->optype;
336 cntlr->addr = &ich9_spi->faddr;
337 cntlr->data = (uint8_t *)ich9_spi->fdata;
338 cntlr->databytes = sizeof(ich9_spi->fdata);
339 cntlr->status = &ich9_spi->ssfs;
340 cntlr->control = (uint16_t *)ich9_spi->ssfc;
341 cntlr->bbar = &ich9_spi->bbar;
342 cntlr->preop = &ich9_spi->preop;
343 cntlr->fpr = &ich9_spi->pr[0];
344 cntlr->fpr_max = 5;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700345
Arthur Heymans02c99712018-03-28 18:49:27 +0200346 if (cntlr->hsfs & HSFS_FDV) {
Arthur Heymansc88e3702017-08-20 20:50:17 +0200347 writel_ (4, &ich9_spi->fdoc);
Arthur Heymans02c99712018-03-28 18:49:27 +0200348 cntlr->flmap0 = readl_(&ich9_spi->fdod);
Arthur Heymansc88e3702017-08-20 20:50:17 +0200349 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700350 }
351
352 ich_set_bbar(0);
353
354 /* Disable the BIOS write protect so write commands are allowed. */
355 pci_read_config_byte(dev, 0xdc, &bios_cntl);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100356 /* Deassert SMM BIOS Write Protect Disable. */
357 bios_cntl &= ~(1 << 5);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700358 pci_write_config_byte(dev, 0xdc, bios_cntl | 0x1);
359}
Aaron Durbin4d3de7e2015-09-02 17:34:04 -0500360
David Hendricksf2612a12014-04-13 16:27:02 -0700361static void spi_init_cb(void *unused)
362{
363 spi_init();
364}
365
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500366BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700367
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700368typedef struct spi_transaction {
369 const uint8_t *out;
370 uint32_t bytesout;
371 uint8_t *in;
372 uint32_t bytesin;
373 uint8_t type;
374 uint8_t opcode;
375 uint32_t offset;
376} spi_transaction;
377
378static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
379{
380 trans->out += bytes;
381 trans->bytesout -= bytes;
382}
383
384static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
385{
386 trans->in += bytes;
387 trans->bytesin -= bytes;
388}
389
390static void spi_setup_type(spi_transaction *trans)
391{
392 trans->type = 0xFF;
393
394 /* Try to guess spi type from read/write sizes. */
395 if (trans->bytesin == 0) {
396 if (trans->bytesout > 4)
397 /*
398 * If bytesin = 0 and bytesout > 4, we presume this is
399 * a write data operation, which is accompanied by an
400 * address.
401 */
402 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
403 else
404 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
405 return;
406 }
407
408 if (trans->bytesout == 1) { /* and bytesin is > 0 */
409 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
410 return;
411 }
412
413 if (trans->bytesout == 4) { /* and bytesin is > 0 */
414 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
415 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700416
417 /* Fast read command is called with 5 bytes instead of 4 */
418 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
419 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
420 --trans->bytesout;
421 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700422}
423
424static int spi_setup_opcode(spi_transaction *trans)
425{
Arthur Heymans02c99712018-03-28 18:49:27 +0200426 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700427 uint16_t optypes;
Arthur Heymans02c99712018-03-28 18:49:27 +0200428 uint8_t opmenu[cntlr->menubytes];
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700429
430 trans->opcode = trans->out[0];
431 spi_use_out(trans, 1);
Arthur Heymans02c99712018-03-28 18:49:27 +0200432 if (!car_get_var(g_ichspi_lock)) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700433 /* The lock is off, so just use index 0. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200434 writeb_(trans->opcode, cntlr->opmenu);
435 optypes = readw_(cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700436 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
Arthur Heymans02c99712018-03-28 18:49:27 +0200437 writew_(optypes, cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700438 return 0;
439 } else {
440 /* The lock is on. See if what we need is on the menu. */
441 uint8_t optype;
442 uint16_t opcode_index;
443
Duncan Lauriea2f1b952012-08-27 11:10:43 -0700444 /* Write Enable is handled as atomic prefix */
445 if (trans->opcode == SPI_OPCODE_WREN)
446 return 0;
447
Arthur Heymans02c99712018-03-28 18:49:27 +0200448 read_reg(cntlr->opmenu, opmenu, sizeof(opmenu));
449 for (opcode_index = 0; opcode_index < cntlr->menubytes;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700450 opcode_index++) {
451 if (opmenu[opcode_index] == trans->opcode)
452 break;
453 }
454
Arthur Heymans02c99712018-03-28 18:49:27 +0200455 if (opcode_index == cntlr->menubytes) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700456 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
457 trans->opcode);
458 return -1;
459 }
460
Arthur Heymans02c99712018-03-28 18:49:27 +0200461 optypes = readw_(cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700462 optype = (optypes >> (opcode_index * 2)) & 0x3;
463 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
464 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
465 trans->bytesout >= 3) {
466 /* We guessed wrong earlier. Fix it up. */
467 trans->type = optype;
468 }
469 if (optype != trans->type) {
470 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
471 optype);
472 return -1;
473 }
474 return opcode_index;
475 }
476}
477
478static int spi_setup_offset(spi_transaction *trans)
479{
480 /* Separate the SPI address and data. */
481 switch (trans->type) {
482 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
483 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
484 return 0;
485 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
486 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
487 trans->offset = ((uint32_t)trans->out[0] << 16) |
488 ((uint32_t)trans->out[1] << 8) |
489 ((uint32_t)trans->out[2] << 0);
490 spi_use_out(trans, 3);
491 return 1;
492 default:
493 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
494 return -1;
495 }
496}
497
498/*
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200499 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700500 * below is True) or 0. In case the wait was for the bit(s) to set - write
501 * those bits back, which would cause resetting them.
502 *
503 * Return the last read status value on success or -1 on failure.
504 */
505static int ich_status_poll(u16 bitmask, int wait_til_set)
506{
Arthur Heymans02c99712018-03-28 18:49:27 +0200507 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200508 int timeout = 600000; /* This will result in 6 seconds */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700509 u16 status = 0;
510
511 while (timeout--) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200512 status = readw_(cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700513 if (wait_til_set ^ ((status & bitmask) == 0)) {
514 if (wait_til_set)
Arthur Heymans02c99712018-03-28 18:49:27 +0200515 writew_((status & bitmask), cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700516 return status;
517 }
518 udelay(10);
519 }
520
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200521 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700522 status, bitmask);
523 return -1;
524}
525
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100526static int spi_is_multichip (void)
527{
Arthur Heymans02c99712018-03-28 18:49:27 +0200528 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
529 if (!(cntlr->hsfs & HSFS_FDV))
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100530 return 0;
Arthur Heymans02c99712018-03-28 18:49:27 +0200531 return !!((cntlr->flmap0 >> 8) & 3);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100532}
533
Furquan Shaikh94f86992016-12-01 07:12:32 -0800534static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800535 size_t bytesout, void *din, size_t bytesin)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700536{
Arthur Heymans02c99712018-03-28 18:49:27 +0200537 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700538 uint16_t control;
539 int16_t opcode_index;
540 int with_address;
541 int status;
542
543 spi_transaction trans = {
Gabe Black93d9f922014-03-27 21:52:43 -0700544 dout, bytesout,
545 din, bytesin,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700546 0xff, 0xff, 0
547 };
548
549 /* There has to always at least be an opcode. */
Gabe Black93d9f922014-03-27 21:52:43 -0700550 if (!bytesout || !dout) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700551 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
552 return -1;
553 }
554 /* Make sure if we read something we have a place to put it. */
Gabe Black93d9f922014-03-27 21:52:43 -0700555 if (bytesin != 0 && !din) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700556 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
557 return -1;
558 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700559
560 if (ich_status_poll(SPIS_SCIP, 0) == -1)
561 return -1;
562
Arthur Heymans02c99712018-03-28 18:49:27 +0200563 writew_(SPIS_CDS | SPIS_FCERR, cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700564
565 spi_setup_type(&trans);
566 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
567 return -1;
568 if ((with_address = spi_setup_offset(&trans)) < 0)
569 return -1;
570
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700571 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700572 /*
573 * Treat Write Enable as Atomic Pre-Op if possible
574 * in order to prevent the Management Engine from
575 * issuing a transaction between WREN and DATA.
576 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200577 if (!car_get_var(g_ichspi_lock))
578 writew_(trans.opcode, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700579 return 0;
580 }
581
582 /* Preset control fields */
583 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
584
585 /* Issue atomic preop cycle if needed */
Arthur Heymans02c99712018-03-28 18:49:27 +0200586 if (readw_(cntlr->preop))
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700587 control |= SPIC_ACS;
588
589 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700590 /* SPI addresses are 24 bit only */
591 if (with_address)
Arthur Heymans02c99712018-03-28 18:49:27 +0200592 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700593
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700594 /*
595 * This is a 'no data' command (like Write Enable), its
596 * bitesout size was 1, decremented to zero while executing
597 * spi_setup_opcode() above. Tell the chip to send the
598 * command.
599 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200600 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700601
602 /* wait for the result */
603 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
604 if (status == -1)
605 return -1;
606
607 if (status & SPIS_FCERR) {
608 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
609 return -1;
610 }
611
Werner Zehf13a6f92018-11-14 10:55:52 +0100612 goto spi_xfer_exit;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700613 }
614
615 /*
Paul Menzel94782972013-06-29 11:41:27 +0200616 * Check if this is a write command attempting to transfer more bytes
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700617 * than the controller can handle. Iterations for writes are not
618 * supported here because each SPI write command needs to be preceded
619 * and followed by other SPI commands, and this sequence is controlled
620 * by the SPI chip driver.
621 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200622 if (trans.bytesout > cntlr->databytes) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700623 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
Kyösti Mälkki11104952014-06-29 16:17:33 +0300624 " spi_crop_chunk()?\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700625 return -1;
626 }
627
628 /*
629 * Read or write up to databytes bytes at a time until everything has
630 * been sent.
631 */
632 while (trans.bytesout || trans.bytesin) {
633 uint32_t data_length;
634
635 /* SPI addresses are 24 bit only */
Arthur Heymans02c99712018-03-28 18:49:27 +0200636 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700637
638 if (trans.bytesout)
Arthur Heymans02c99712018-03-28 18:49:27 +0200639 data_length = min(trans.bytesout, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700640 else
Arthur Heymans02c99712018-03-28 18:49:27 +0200641 data_length = min(trans.bytesin, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700642
643 /* Program data into FDATA0 to N */
644 if (trans.bytesout) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200645 write_reg(trans.out, cntlr->data, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700646 spi_use_out(&trans, data_length);
647 if (with_address)
648 trans.offset += data_length;
649 }
650
651 /* Add proper control fields' values */
Arthur Heymans02c99712018-03-28 18:49:27 +0200652 control &= ~((cntlr->databytes - 1) << 8);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700653 control |= SPIC_DS;
654 control |= (data_length - 1) << 8;
655
656 /* write it */
Arthur Heymans02c99712018-03-28 18:49:27 +0200657 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700658
659 /* Wait for Cycle Done Status or Flash Cycle Error. */
660 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
661 if (status == -1)
662 return -1;
663
664 if (status & SPIS_FCERR) {
665 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
666 return -1;
667 }
668
669 if (trans.bytesin) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200670 read_reg(cntlr->data, trans.in, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700671 spi_use_in(&trans, data_length);
672 if (with_address)
673 trans.offset += data_length;
674 }
675 }
676
Werner Zehf13a6f92018-11-14 10:55:52 +0100677spi_xfer_exit:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700678 /* Clear atomic preop now that xfer is done */
Arthur Heymans02c99712018-03-28 18:49:27 +0200679 writew_(0, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700680
681 return 0;
682}
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100683
684/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
685static void ich_hwseq_set_addr(uint32_t addr)
686{
Arthur Heymans02c99712018-03-28 18:49:27 +0200687 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
688 uint32_t addr_old = readl_(&cntlr->ich9_spi->faddr) & ~0x01FFFFFF;
689 writel_((addr & 0x01FFFFFF) | addr_old, &cntlr->ich9_spi->faddr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100690}
691
692/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
693 Resets all error flags in HSFS.
694 Returns 0 if the cycle completes successfully without errors within
695 timeout us, 1 on errors. */
696static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
697 unsigned int len)
698{
Arthur Heymans02c99712018-03-28 18:49:27 +0200699 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100700 uint16_t hsfs;
701 uint32_t addr;
702
703 timeout /= 8; /* scale timeout duration to counter */
Arthur Heymans02c99712018-03-28 18:49:27 +0200704 while ((((hsfs = readw_(&cntlr->ich9_spi->hsfs)) &
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100705 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
706 --timeout) {
707 udelay(8);
708 }
Arthur Heymans02c99712018-03-28 18:49:27 +0200709 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100710
711 if (!timeout) {
712 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200713 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
714 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100715 printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and "
716 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
717 addr, addr + len - 1, addr, len - 1,
718 hsfc, hsfs);
719 return 1;
720 }
721
722 if (hsfs & HSFS_FCERR) {
723 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200724 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
725 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100726 printk(BIOS_ERR, "Transaction error between offset 0x%08x and "
727 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
728 addr, addr + len - 1, addr, len - 1,
729 hsfc, hsfs);
730 return 1;
731 }
732 return 0;
733}
734
735
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800736static int ich_hwseq_erase(const struct spi_flash *flash, u32 offset,
737 size_t len)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100738{
Arthur Heymans02c99712018-03-28 18:49:27 +0200739 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100740 u32 start, end, erase_size;
741 int ret;
742 uint16_t hsfc;
743 uint16_t timeout = 1000 * 60;
744
745 erase_size = flash->sector_size;
746 if (offset % erase_size || len % erase_size) {
747 printk(BIOS_ERR, "SF: Erase offset/length not multiple of erase size\n");
748 return -1;
749 }
750
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800751 ret = spi_claim_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100752 if (ret) {
753 printk(BIOS_ERR, "SF: Unable to claim SPI bus\n");
754 return ret;
755 }
756
757 start = offset;
758 end = start + len;
759
760 while (offset < end) {
761 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
Arthur Heymans02c99712018-03-28 18:49:27 +0200762 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100763
764 ich_hwseq_set_addr(offset);
765
766 offset += erase_size;
767
Arthur Heymans02c99712018-03-28 18:49:27 +0200768 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100769 hsfc &= ~HSFC_FCYCLE; /* clear operation */
770 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
771 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200772 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100773 if (ich_hwseq_wait_for_cycle_complete(timeout, len))
774 {
775 printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size);
776 ret = -1;
777 goto out;
778 }
779 }
780
781 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
782
783out:
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800784 spi_release_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100785 return ret;
786}
787
788static void ich_read_data(uint8_t *data, int len)
789{
Arthur Heymans02c99712018-03-28 18:49:27 +0200790 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100791 int i;
792 uint32_t temp32 = 0;
793
794 for (i = 0; i < len; i++) {
795 if ((i % 4) == 0)
Arthur Heymans02c99712018-03-28 18:49:27 +0200796 temp32 = readl_(cntlr->data + i);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100797
798 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
799 }
800}
801
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800802static int ich_hwseq_read(const struct spi_flash *flash, u32 addr, size_t len,
803 void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100804{
Arthur Heymans02c99712018-03-28 18:49:27 +0200805 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100806 uint16_t hsfc;
807 uint16_t timeout = 100 * 60;
808 uint8_t block_len;
809
810 if (addr + len > flash->size) {
811 printk (BIOS_ERR,
812 "Attempt to read %x-%x which is out of chip\n",
813 (unsigned) addr,
814 (unsigned) addr+(unsigned) len);
815 return -1;
816 }
817
818 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200819 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100820
821 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200822 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100823 if (block_len > (~addr & 0xff))
824 block_len = (~addr & 0xff) + 1;
825 ich_hwseq_set_addr(addr);
Arthur Heymans02c99712018-03-28 18:49:27 +0200826 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100827 hsfc &= ~HSFC_FCYCLE; /* set read operation */
828 hsfc &= ~HSFC_FDBC; /* clear byte count */
829 /* set byte count */
830 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
831 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200832 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100833
834 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
835 return 1;
836 ich_read_data(buf, block_len);
837 addr += block_len;
838 buf += block_len;
839 len -= block_len;
840 }
841 return 0;
842}
843
844/* Fill len bytes from the data array into the fdata/spid registers.
845 *
846 * Note that using len > flash->pgm->spi.max_data_write will trash the registers
847 * following the data registers.
848 */
849static void ich_fill_data(const uint8_t *data, int len)
850{
Arthur Heymans02c99712018-03-28 18:49:27 +0200851 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100852 uint32_t temp32 = 0;
853 int i;
854
855 if (len <= 0)
856 return;
857
858 for (i = 0; i < len; i++) {
859 if ((i % 4) == 0)
860 temp32 = 0;
861
862 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
863
864 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200865 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100866 }
867 i--;
868 if ((i % 4) != 3) /* Write remaining data 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
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800872static int ich_hwseq_write(const struct spi_flash *flash, u32 addr, size_t len,
873 const void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100874{
Arthur Heymans02c99712018-03-28 18:49:27 +0200875 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100876 uint16_t hsfc;
877 uint16_t timeout = 100 * 60;
878 uint8_t block_len;
879 uint32_t start = addr;
880
881 if (addr + len > flash->size) {
882 printk (BIOS_ERR,
883 "Attempt to write 0x%x-0x%x which is out of chip\n",
884 (unsigned)addr, (unsigned) (addr+len));
885 return -1;
886 }
887
888 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200889 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100890
891 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200892 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100893 if (block_len > (~addr & 0xff))
894 block_len = (~addr & 0xff) + 1;
895
896 ich_hwseq_set_addr(addr);
897
898 ich_fill_data(buf, block_len);
Arthur Heymans02c99712018-03-28 18:49:27 +0200899 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100900 hsfc &= ~HSFC_FCYCLE; /* clear operation */
901 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
902 hsfc &= ~HSFC_FDBC; /* clear byte count */
903 /* set byte count */
904 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
905 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200906 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100907
908 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
909 {
910 printk (BIOS_ERR, "SF: write failure at %x\n",
911 addr);
912 return -1;
913 }
914 addr += block_len;
915 buf += block_len;
916 len -= block_len;
917 }
918 printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n",
919 (unsigned) (addr - start), start);
920 return 0;
921}
922
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700923static const struct spi_flash_ops spi_flash_ops = {
924 .read = ich_hwseq_read,
925 .write = ich_hwseq_write,
926 .erase = ich_hwseq_erase,
927};
928
Furquan Shaikha1491572017-05-17 19:14:06 -0700929static int spi_flash_programmer_probe(const struct spi_slave *spi,
930 struct spi_flash *flash)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100931{
Arthur Heymans02c99712018-03-28 18:49:27 +0200932 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100933 uint32_t flcomp;
934
Arthur Heymansc88e3702017-08-20 20:50:17 +0200935 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX))
936 return spi_flash_generic_probe(spi, flash);
937
Furquan Shaikha1491572017-05-17 19:14:06 -0700938 /* Try generic probing first if spi_is_multichip returns 0. */
939 if (!spi_is_multichip() && !spi_flash_generic_probe(spi, flash))
940 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100941
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800942 memcpy(&flash->spi, spi, sizeof(*spi));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100943 flash->name = "Opaque HW-sequencing";
944
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100945 ich_hwseq_set_addr (0);
Arthur Heymans02c99712018-03-28 18:49:27 +0200946 switch ((cntlr->hsfs >> 3) & 3)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100947 {
948 case 0:
949 flash->sector_size = 256;
950 break;
951 case 1:
952 flash->sector_size = 4096;
953 break;
954 case 2:
955 flash->sector_size = 8192;
956 break;
957 case 3:
958 flash->sector_size = 65536;
959 break;
960 }
961
Arthur Heymans02c99712018-03-28 18:49:27 +0200962 writel_ (0x1000, &cntlr->ich9_spi->fdoc);
963 flcomp = readl_(&cntlr->ich9_spi->fdod);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100964
965 flash->size = 1 << (19 + (flcomp & 7));
966
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700967 flash->ops = &spi_flash_ops;
968
Arthur Heymans02c99712018-03-28 18:49:27 +0200969 if ((cntlr->hsfs & HSFS_FDV) && ((cntlr->flmap0 >> 8) & 3))
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100970 flash->size += 1 << (19 + ((flcomp >> 3) & 7));
971 printk (BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
972
Furquan Shaikh30221b42017-05-15 14:35:15 -0700973 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100974}
Furquan Shaikha1491572017-05-17 19:14:06 -0700975
Aaron Durbin851dde82018-04-19 21:15:25 -0600976static int xfer_vectors(const struct spi_slave *slave,
977 struct spi_op vectors[], size_t count)
978{
979 return spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer);
980}
981
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100982#define SPI_FPR_SHIFT 12
983#define ICH7_SPI_FPR_MASK 0xfff
984#define ICH9_SPI_FPR_MASK 0x1fff
985#define SPI_FPR_BASE_SHIFT 0
986#define ICH7_SPI_FPR_LIMIT_SHIFT 12
987#define ICH9_SPI_FPR_LIMIT_SHIFT 16
988#define ICH9_SPI_FPR_RPE (1 << 15) /* Read Protect */
989#define SPI_FPR_WPE (1 << 31) /* Write Protect */
990
991static u32 spi_fpr(u32 base, u32 limit)
992{
993 u32 ret;
994 u32 mask, limit_shift;
995 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);