blob: 65693512f589dfa186274418c236d380cb9b8241 [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>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020025#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020026#include <device/pci_ops.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070027#include <console/console.h>
Kyösti Mälkki7ba14402019-02-07 12:44:00 +020028#include <device/device.h>
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010029#include <device/pci.h>
30#include <spi_flash.h>
Zheng Bao600784e2013-02-07 17:30:23 +080031#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070032
Arthur Heymans92185e32019-05-28 13:06:34 +020033#include "spi.h"
34
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010035#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
36#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
37#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
38#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
39
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010040static int spi_is_multichip(void);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010041
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020042struct ich7_spi_regs {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070043 uint16_t spis;
44 uint16_t spic;
45 uint32_t spia;
46 uint64_t spid[8];
47 uint64_t _pad;
48 uint32_t bbar;
49 uint16_t preop;
50 uint16_t optype;
51 uint8_t opmenu[8];
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +010052 uint32_t pbr[3];
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020053} __packed;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070054
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020055struct ich9_spi_regs {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070056 uint32_t bfpr;
57 uint16_t hsfs;
58 uint16_t hsfc;
59 uint32_t faddr;
60 uint32_t _reserved0;
61 uint32_t fdata[16];
62 uint32_t frap;
63 uint32_t freg[5];
64 uint32_t _reserved1[3];
65 uint32_t pr[5];
66 uint32_t _reserved2[2];
67 uint8_t ssfs;
68 uint8_t ssfc[3];
69 uint16_t preop;
70 uint16_t optype;
71 uint8_t opmenu[8];
72 uint32_t bbar;
73 uint8_t _reserved3[12];
74 uint32_t fdoc;
75 uint32_t fdod;
76 uint8_t _reserved4[8];
77 uint32_t afc;
78 uint32_t lvscc;
79 uint32_t uvscc;
80 uint8_t _reserved5[4];
81 uint32_t fpb;
82 uint8_t _reserved6[28];
83 uint32_t srdl;
84 uint32_t srdc;
85 uint32_t srd;
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020086} __packed;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070087
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020088struct ich_spi_controller {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070089 int locked;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010090 uint32_t flmap0;
Stefan Tauner327205d2018-08-26 13:53:16 +020091 uint32_t flcomp;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010092 uint32_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070093
Arthur Heymans21c5d432019-06-15 18:23:29 +020094 union {
95 struct ich9_spi_regs *ich9_spi;
96 struct ich7_spi_regs *ich7_spi;
97 };
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070098 uint8_t *opmenu;
99 int menubytes;
100 uint16_t *preop;
101 uint16_t *optype;
102 uint32_t *addr;
103 uint8_t *data;
104 unsigned databytes;
105 uint8_t *status;
106 uint16_t *control;
107 uint32_t *bbar;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100108 uint32_t *fpr;
109 uint8_t fpr_max;
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200110};
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700111
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200112static struct ich_spi_controller g_cntlr;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700113
114enum {
115 SPIS_SCIP = 0x0001,
116 SPIS_GRANT = 0x0002,
117 SPIS_CDS = 0x0004,
118 SPIS_FCERR = 0x0008,
119 SSFS_AEL = 0x0010,
120 SPIS_LOCK = 0x8000,
121 SPIS_RESERVED_MASK = 0x7ff0,
122 SSFS_RESERVED_MASK = 0x7fe2
123};
124
125enum {
126 SPIC_SCGO = 0x000002,
127 SPIC_ACS = 0x000004,
128 SPIC_SPOP = 0x000008,
129 SPIC_DBC = 0x003f00,
130 SPIC_DS = 0x004000,
131 SPIC_SME = 0x008000,
132 SSFC_SCF_MASK = 0x070000,
133 SSFC_RESERVED = 0xf80000
134};
135
136enum {
137 HSFS_FDONE = 0x0001,
138 HSFS_FCERR = 0x0002,
139 HSFS_AEL = 0x0004,
140 HSFS_BERASE_MASK = 0x0018,
141 HSFS_BERASE_SHIFT = 3,
142 HSFS_SCIP = 0x0020,
143 HSFS_FDOPSS = 0x2000,
144 HSFS_FDV = 0x4000,
145 HSFS_FLOCKDN = 0x8000
146};
147
148enum {
149 HSFC_FGO = 0x0001,
150 HSFC_FCYCLE_MASK = 0x0006,
151 HSFC_FCYCLE_SHIFT = 1,
152 HSFC_FDBC_MASK = 0x3f00,
153 HSFC_FDBC_SHIFT = 8,
154 HSFC_FSMIE = 0x8000
155};
156
157enum {
158 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
159 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
160 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
161 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
162};
163
Julius Wernercd49cce2019-03-05 16:53:33 -0800164#if CONFIG(DEBUG_SPI_FLASH)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700165
166static u8 readb_(const void *addr)
167{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800168 u8 v = read8(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100169
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700170 printk(BIOS_DEBUG, "read %2.2x from %4.4x\n",
171 v, ((unsigned) addr & 0xffff) - 0xf020);
172 return v;
173}
174
175static u16 readw_(const void *addr)
176{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800177 u16 v = read16(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100178
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700179 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
180 v, ((unsigned) addr & 0xffff) - 0xf020);
181 return v;
182}
183
184static u32 readl_(const void *addr)
185{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800186 u32 v = read32(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100187
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700188 printk(BIOS_DEBUG, "read %8.8x from %4.4x\n",
189 v, ((unsigned) addr & 0xffff) - 0xf020);
190 return v;
191}
192
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800193static void writeb_(u8 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700194{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800195 write8(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700196 printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n",
197 b, ((unsigned) addr & 0xffff) - 0xf020);
198}
199
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800200static void writew_(u16 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700201{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800202 write16(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700203 printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n",
204 b, ((unsigned) addr & 0xffff) - 0xf020);
205}
206
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800207static void writel_(u32 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700208{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800209 write32(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700210 printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n",
211 b, ((unsigned) addr & 0xffff) - 0xf020);
212}
213
214#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
215
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800216#define readb_(a) read8(a)
217#define readw_(a) read16(a)
218#define readl_(a) read32(a)
219#define writeb_(val, addr) write8(addr, val)
220#define writew_(val, addr) write16(addr, val)
221#define writel_(val, addr) write32(addr, val)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700222
223#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
224
225static void write_reg(const void *value, void *dest, uint32_t size)
226{
227 const uint8_t *bvalue = value;
228 uint8_t *bdest = dest;
229
230 while (size >= 4) {
231 writel_(*(const uint32_t *)bvalue, bdest);
232 bdest += 4; bvalue += 4; size -= 4;
233 }
234 while (size) {
235 writeb_(*bvalue, bdest);
236 bdest++; bvalue++; size--;
237 }
238}
239
240static void read_reg(const void *src, void *value, uint32_t size)
241{
242 const uint8_t *bsrc = src;
243 uint8_t *bvalue = value;
244
245 while (size >= 4) {
246 *(uint32_t *)bvalue = readl_(bsrc);
247 bsrc += 4; bvalue += 4; size -= 4;
248 }
249 while (size) {
250 *bvalue = readb_(bsrc);
251 bsrc++; bvalue++; size--;
252 }
253}
254
255static void ich_set_bbar(uint32_t minaddr)
256{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200257 struct ich_spi_controller *cntlr = &g_cntlr;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700258 const uint32_t bbar_mask = 0x00ffff00;
259 uint32_t ichspi_bbar;
260
261 minaddr &= bbar_mask;
Arthur Heymans02c99712018-03-28 18:49:27 +0200262 ichspi_bbar = readl_(cntlr->bbar) & ~bbar_mask;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700263 ichspi_bbar |= minaddr;
Arthur Heymans02c99712018-03-28 18:49:27 +0200264 writel_(ichspi_bbar, cntlr->bbar);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700265}
266
Jacob Garber9172b692019-06-26 16:18:16 -0600267#if CONFIG(SOUTHBRIDGE_INTEL_I82801GX)
268#define MENU_BYTES member_size(struct ich7_spi_regs, opmenu)
269#else
270#define MENU_BYTES member_size(struct ich9_spi_regs, opmenu)
271#endif
272
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700273void spi_init(void)
274{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200275 struct ich_spi_controller *cntlr = &g_cntlr;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700276 uint8_t *rcrb; /* Root Complex Register Block */
277 uint32_t rcba; /* Root Complex Base Address */
278 uint8_t bios_cntl;
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200279 struct ich9_spi_regs *ich9_spi;
280 struct ich7_spi_regs *ich7_spi;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100281 uint16_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700282
Arthur Heymans02c99712018-03-28 18:49:27 +0200283#ifdef __SIMPLE_DEVICE__
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200284 pci_devfn_t dev = PCI_DEV(0, 31, 0);
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700285#else
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300286 struct device *dev = pcidev_on_root(31, 0);
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700287#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700288
Kyösti Mälkki7ba14402019-02-07 12:44:00 +0200289 rcba = pci_read_config32(dev, 0xf0);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700290 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
291 rcrb = (uint8_t *)(rcba & 0xffffc000);
Julius Wernercd49cce2019-03-05 16:53:33 -0800292 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) {
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200293 ich7_spi = (struct ich7_spi_regs *)(rcrb + 0x3020);
Arthur Heymans21c5d432019-06-15 18:23:29 +0200294 cntlr->ich7_spi = ich7_spi;
Arthur Heymans02c99712018-03-28 18:49:27 +0200295 cntlr->opmenu = ich7_spi->opmenu;
296 cntlr->menubytes = sizeof(ich7_spi->opmenu);
297 cntlr->optype = &ich7_spi->optype;
298 cntlr->addr = &ich7_spi->spia;
299 cntlr->data = (uint8_t *)ich7_spi->spid;
300 cntlr->databytes = sizeof(ich7_spi->spid);
301 cntlr->status = (uint8_t *)&ich7_spi->spis;
Arthur Heymans02c99712018-03-28 18:49:27 +0200302 cntlr->control = &ich7_spi->spic;
303 cntlr->bbar = &ich7_spi->bbar;
304 cntlr->preop = &ich7_spi->preop;
305 cntlr->fpr = &ich7_spi->pbr[0];
306 cntlr->fpr_max = 3;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200307 } else {
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200308 ich9_spi = (struct ich9_spi_regs *)(rcrb + 0x3800);
Arthur Heymans02c99712018-03-28 18:49:27 +0200309 cntlr->ich9_spi = ich9_spi;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200310 hsfs = readw_(&ich9_spi->hsfs);
Arthur Heymans02c99712018-03-28 18:49:27 +0200311 cntlr->hsfs = hsfs;
312 cntlr->opmenu = ich9_spi->opmenu;
313 cntlr->menubytes = sizeof(ich9_spi->opmenu);
314 cntlr->optype = &ich9_spi->optype;
315 cntlr->addr = &ich9_spi->faddr;
316 cntlr->data = (uint8_t *)ich9_spi->fdata;
317 cntlr->databytes = sizeof(ich9_spi->fdata);
318 cntlr->status = &ich9_spi->ssfs;
319 cntlr->control = (uint16_t *)ich9_spi->ssfc;
320 cntlr->bbar = &ich9_spi->bbar;
321 cntlr->preop = &ich9_spi->preop;
322 cntlr->fpr = &ich9_spi->pr[0];
323 cntlr->fpr_max = 5;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700324
Arthur Heymans02c99712018-03-28 18:49:27 +0200325 if (cntlr->hsfs & HSFS_FDV) {
Patrick Georgic88828d2018-11-26 10:42:59 +0100326 writel_(4, &ich9_spi->fdoc);
Arthur Heymans02c99712018-03-28 18:49:27 +0200327 cntlr->flmap0 = readl_(&ich9_spi->fdod);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100328 writel_(0x1000, &ich9_spi->fdoc);
Stefan Tauner327205d2018-08-26 13:53:16 +0200329 cntlr->flcomp = readl_(&ich9_spi->fdod);
Arthur Heymansc88e3702017-08-20 20:50:17 +0200330 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700331 }
332
333 ich_set_bbar(0);
334
335 /* Disable the BIOS write protect so write commands are allowed. */
Kyösti Mälkki7ba14402019-02-07 12:44:00 +0200336 bios_cntl = pci_read_config8(dev, 0xdc);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100337 /* Deassert SMM BIOS Write Protect Disable. */
338 bios_cntl &= ~(1 << 5);
Kyösti Mälkki7ba14402019-02-07 12:44:00 +0200339 pci_write_config8(dev, 0xdc, bios_cntl | 0x1);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700340}
Aaron Durbin4d3de7e2015-09-02 17:34:04 -0500341
Arthur Heymans816aaba2019-06-11 11:10:25 +0200342static int spi_locked(void)
343{
344 struct ich_spi_controller *cntlr = &g_cntlr;
345 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) {
346 return !!(readw_(&cntlr->ich7_spi->spis) & HSFS_FLOCKDN);
347 } else {
Jacob Garber36749742019-07-02 11:08:53 -0600348 return !!(readw_(&cntlr->ich9_spi->hsfs) & HSFS_FLOCKDN);
Arthur Heymans816aaba2019-06-11 11:10:25 +0200349 }
350}
351
David Hendricksf2612a12014-04-13 16:27:02 -0700352static void spi_init_cb(void *unused)
353{
354 spi_init();
355}
356
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500357BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700358
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700359typedef struct spi_transaction {
360 const uint8_t *out;
361 uint32_t bytesout;
362 uint8_t *in;
363 uint32_t bytesin;
364 uint8_t type;
365 uint8_t opcode;
366 uint32_t offset;
367} spi_transaction;
368
369static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
370{
371 trans->out += bytes;
372 trans->bytesout -= bytes;
373}
374
375static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
376{
377 trans->in += bytes;
378 trans->bytesin -= bytes;
379}
380
381static void spi_setup_type(spi_transaction *trans)
382{
383 trans->type = 0xFF;
384
385 /* Try to guess spi type from read/write sizes. */
386 if (trans->bytesin == 0) {
387 if (trans->bytesout > 4)
388 /*
389 * If bytesin = 0 and bytesout > 4, we presume this is
390 * a write data operation, which is accompanied by an
391 * address.
392 */
393 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
394 else
395 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
396 return;
397 }
398
399 if (trans->bytesout == 1) { /* and bytesin is > 0 */
400 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
401 return;
402 }
403
404 if (trans->bytesout == 4) { /* and bytesin is > 0 */
405 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
406 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700407
408 /* Fast read command is called with 5 bytes instead of 4 */
409 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
410 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
411 --trans->bytesout;
412 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700413}
414
415static int spi_setup_opcode(spi_transaction *trans)
416{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200417 struct ich_spi_controller *cntlr = &g_cntlr;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700418 uint16_t optypes;
Jacob Garber9172b692019-06-26 16:18:16 -0600419 uint8_t opmenu[MENU_BYTES];
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700420
421 trans->opcode = trans->out[0];
422 spi_use_out(trans, 1);
Arthur Heymans816aaba2019-06-11 11:10:25 +0200423 if (!spi_locked()) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700424 /* The lock is off, so just use index 0. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200425 writeb_(trans->opcode, cntlr->opmenu);
426 optypes = readw_(cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700427 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
Arthur Heymans02c99712018-03-28 18:49:27 +0200428 writew_(optypes, cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700429 return 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700430 }
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100431
432 /* The lock is on. See if what we need is on the menu. */
433 uint8_t optype;
434 uint16_t opcode_index;
435
436 /* Write Enable is handled as atomic prefix */
437 if (trans->opcode == SPI_OPCODE_WREN)
438 return 0;
439
440 read_reg(cntlr->opmenu, opmenu, sizeof(opmenu));
Jacob Garber9172b692019-06-26 16:18:16 -0600441 for (opcode_index = 0; opcode_index < ARRAY_SIZE(opmenu); opcode_index++) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100442 if (opmenu[opcode_index] == trans->opcode)
443 break;
444 }
445
Jacob Garber9172b692019-06-26 16:18:16 -0600446 if (opcode_index == ARRAY_SIZE(opmenu)) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100447 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
448 trans->opcode);
449 return -1;
450 }
451
452 optypes = readw_(cntlr->optype);
453 optype = (optypes >> (opcode_index * 2)) & 0x3;
454 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
455 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
456 trans->bytesout >= 3) {
457 /* We guessed wrong earlier. Fix it up. */
458 trans->type = optype;
459 }
460 if (optype != trans->type) {
461 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
462 optype);
463 return -1;
464 }
465 return opcode_index;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700466}
467
468static int spi_setup_offset(spi_transaction *trans)
469{
470 /* Separate the SPI address and data. */
471 switch (trans->type) {
472 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
473 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
474 return 0;
475 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
476 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
477 trans->offset = ((uint32_t)trans->out[0] << 16) |
478 ((uint32_t)trans->out[1] << 8) |
479 ((uint32_t)trans->out[2] << 0);
480 spi_use_out(trans, 3);
481 return 1;
482 default:
483 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
484 return -1;
485 }
486}
487
488/*
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200489 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700490 * below is True) or 0. In case the wait was for the bit(s) to set - write
491 * those bits back, which would cause resetting them.
492 *
493 * Return the last read status value on success or -1 on failure.
494 */
495static int ich_status_poll(u16 bitmask, int wait_til_set)
496{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200497 struct ich_spi_controller *cntlr = &g_cntlr;
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200498 int timeout = 600000; /* This will result in 6 seconds */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700499 u16 status = 0;
500
501 while (timeout--) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200502 status = readw_(cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700503 if (wait_til_set ^ ((status & bitmask) == 0)) {
504 if (wait_til_set)
Arthur Heymans02c99712018-03-28 18:49:27 +0200505 writew_((status & bitmask), cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700506 return status;
507 }
508 udelay(10);
509 }
510
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200511 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700512 status, bitmask);
513 return -1;
514}
515
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100516static int spi_is_multichip(void)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100517{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200518 struct ich_spi_controller *cntlr = &g_cntlr;
Arthur Heymans02c99712018-03-28 18:49:27 +0200519 if (!(cntlr->hsfs & HSFS_FDV))
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100520 return 0;
Arthur Heymans02c99712018-03-28 18:49:27 +0200521 return !!((cntlr->flmap0 >> 8) & 3);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100522}
523
Furquan Shaikh94f86992016-12-01 07:12:32 -0800524static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800525 size_t bytesout, void *din, size_t bytesin)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700526{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200527 struct ich_spi_controller *cntlr = &g_cntlr;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700528 uint16_t control;
529 int16_t opcode_index;
530 int with_address;
531 int status;
532
533 spi_transaction trans = {
Gabe Black93d9f922014-03-27 21:52:43 -0700534 dout, bytesout,
535 din, bytesin,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700536 0xff, 0xff, 0
537 };
538
539 /* There has to always at least be an opcode. */
Gabe Black93d9f922014-03-27 21:52:43 -0700540 if (!bytesout || !dout) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700541 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
542 return -1;
543 }
544 /* Make sure if we read something we have a place to put it. */
Gabe Black93d9f922014-03-27 21:52:43 -0700545 if (bytesin != 0 && !din) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700546 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
547 return -1;
548 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700549
550 if (ich_status_poll(SPIS_SCIP, 0) == -1)
551 return -1;
552
Arthur Heymans02c99712018-03-28 18:49:27 +0200553 writew_(SPIS_CDS | SPIS_FCERR, cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700554
555 spi_setup_type(&trans);
556 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
557 return -1;
558 if ((with_address = spi_setup_offset(&trans)) < 0)
559 return -1;
560
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700561 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700562 /*
563 * Treat Write Enable as Atomic Pre-Op if possible
564 * in order to prevent the Management Engine from
565 * issuing a transaction between WREN and DATA.
566 */
Arthur Heymans816aaba2019-06-11 11:10:25 +0200567 if (!spi_locked())
Arthur Heymans02c99712018-03-28 18:49:27 +0200568 writew_(trans.opcode, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700569 return 0;
570 }
571
572 /* Preset control fields */
573 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
574
575 /* Issue atomic preop cycle if needed */
Arthur Heymans02c99712018-03-28 18:49:27 +0200576 if (readw_(cntlr->preop))
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700577 control |= SPIC_ACS;
578
579 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700580 /* SPI addresses are 24 bit only */
581 if (with_address)
Arthur Heymans02c99712018-03-28 18:49:27 +0200582 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700583
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700584 /*
585 * This is a 'no data' command (like Write Enable), its
586 * bitesout size was 1, decremented to zero while executing
587 * spi_setup_opcode() above. Tell the chip to send the
588 * command.
589 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200590 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700591
592 /* wait for the result */
593 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
594 if (status == -1)
595 return -1;
596
597 if (status & SPIS_FCERR) {
598 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
599 return -1;
600 }
601
Werner Zehf13a6f92018-11-14 10:55:52 +0100602 goto spi_xfer_exit;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700603 }
604
605 /*
Paul Menzel94782972013-06-29 11:41:27 +0200606 * Check if this is a write command attempting to transfer more bytes
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700607 * than the controller can handle. Iterations for writes are not
608 * supported here because each SPI write command needs to be preceded
609 * and followed by other SPI commands, and this sequence is controlled
610 * by the SPI chip driver.
611 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200612 if (trans.bytesout > cntlr->databytes) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700613 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
Kyösti Mälkki11104952014-06-29 16:17:33 +0300614 " spi_crop_chunk()?\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700615 return -1;
616 }
617
618 /*
619 * Read or write up to databytes bytes at a time until everything has
620 * been sent.
621 */
622 while (trans.bytesout || trans.bytesin) {
623 uint32_t data_length;
624
625 /* SPI addresses are 24 bit only */
Arthur Heymans02c99712018-03-28 18:49:27 +0200626 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700627
628 if (trans.bytesout)
Arthur Heymans02c99712018-03-28 18:49:27 +0200629 data_length = min(trans.bytesout, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700630 else
Arthur Heymans02c99712018-03-28 18:49:27 +0200631 data_length = min(trans.bytesin, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700632
633 /* Program data into FDATA0 to N */
634 if (trans.bytesout) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200635 write_reg(trans.out, cntlr->data, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700636 spi_use_out(&trans, data_length);
637 if (with_address)
638 trans.offset += data_length;
639 }
640
641 /* Add proper control fields' values */
Arthur Heymans02c99712018-03-28 18:49:27 +0200642 control &= ~((cntlr->databytes - 1) << 8);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700643 control |= SPIC_DS;
644 control |= (data_length - 1) << 8;
645
646 /* write it */
Arthur Heymans02c99712018-03-28 18:49:27 +0200647 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700648
649 /* Wait for Cycle Done Status or Flash Cycle Error. */
650 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
651 if (status == -1)
652 return -1;
653
654 if (status & SPIS_FCERR) {
655 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
656 return -1;
657 }
658
659 if (trans.bytesin) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200660 read_reg(cntlr->data, trans.in, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700661 spi_use_in(&trans, data_length);
662 if (with_address)
663 trans.offset += data_length;
664 }
665 }
666
Werner Zehf13a6f92018-11-14 10:55:52 +0100667spi_xfer_exit:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700668 /* Clear atomic preop now that xfer is done */
Arthur Heymans02c99712018-03-28 18:49:27 +0200669 writew_(0, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700670
671 return 0;
672}
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100673
674/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
675static void ich_hwseq_set_addr(uint32_t addr)
676{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200677 struct ich_spi_controller *cntlr = &g_cntlr;
Arthur Heymans02c99712018-03-28 18:49:27 +0200678 uint32_t addr_old = readl_(&cntlr->ich9_spi->faddr) & ~0x01FFFFFF;
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100679
Arthur Heymans02c99712018-03-28 18:49:27 +0200680 writel_((addr & 0x01FFFFFF) | addr_old, &cntlr->ich9_spi->faddr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100681}
682
683/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
684 Resets all error flags in HSFS.
685 Returns 0 if the cycle completes successfully without errors within
686 timeout us, 1 on errors. */
687static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
688 unsigned int len)
689{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200690 struct ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100691 uint16_t hsfs;
692 uint32_t addr;
693
694 timeout /= 8; /* scale timeout duration to counter */
Arthur Heymans02c99712018-03-28 18:49:27 +0200695 while ((((hsfs = readw_(&cntlr->ich9_spi->hsfs)) &
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100696 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
697 --timeout) {
698 udelay(8);
699 }
Arthur Heymans02c99712018-03-28 18:49:27 +0200700 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100701
702 if (!timeout) {
703 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200704 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
705 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100706 printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and "
707 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
708 addr, addr + len - 1, addr, len - 1,
709 hsfc, hsfs);
710 return 1;
711 }
712
713 if (hsfs & HSFS_FCERR) {
714 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200715 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
716 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100717 printk(BIOS_ERR, "Transaction error between offset 0x%08x and "
718 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
719 addr, addr + len - 1, addr, len - 1,
720 hsfc, hsfs);
721 return 1;
722 }
723 return 0;
724}
725
726
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800727static int ich_hwseq_erase(const struct spi_flash *flash, u32 offset,
728 size_t len)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100729{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200730 struct ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100731 u32 start, end, erase_size;
732 int ret;
733 uint16_t hsfc;
Uwe Poeche17362be2019-07-17 14:27:13 +0200734 unsigned int timeout = 1000 * SPI_FLASH_SECTOR_ERASE_TIMEOUT_MS;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100735
736 erase_size = flash->sector_size;
737 if (offset % erase_size || len % erase_size) {
738 printk(BIOS_ERR, "SF: Erase offset/length not multiple of erase size\n");
739 return -1;
740 }
741
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800742 ret = spi_claim_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100743 if (ret) {
744 printk(BIOS_ERR, "SF: Unable to claim SPI bus\n");
745 return ret;
746 }
747
748 start = offset;
749 end = start + len;
750
751 while (offset < end) {
752 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
Arthur Heymans02c99712018-03-28 18:49:27 +0200753 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100754
755 ich_hwseq_set_addr(offset);
756
757 offset += erase_size;
758
Arthur Heymans02c99712018-03-28 18:49:27 +0200759 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100760 hsfc &= ~HSFC_FCYCLE; /* clear operation */
761 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
762 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200763 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100764 if (ich_hwseq_wait_for_cycle_complete(timeout, len)) {
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100765 printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size);
766 ret = -1;
767 goto out;
768 }
769 }
770
771 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
772
773out:
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800774 spi_release_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100775 return ret;
776}
777
778static void ich_read_data(uint8_t *data, int len)
779{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200780 struct ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100781 int i;
782 uint32_t temp32 = 0;
783
784 for (i = 0; i < len; i++) {
785 if ((i % 4) == 0)
Arthur Heymans02c99712018-03-28 18:49:27 +0200786 temp32 = readl_(cntlr->data + i);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100787
788 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
789 }
790}
791
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800792static int ich_hwseq_read(const struct spi_flash *flash, u32 addr, size_t len,
793 void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100794{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200795 struct ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100796 uint16_t hsfc;
797 uint16_t timeout = 100 * 60;
798 uint8_t block_len;
799
800 if (addr + len > flash->size) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100801 printk(BIOS_ERR,
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100802 "Attempt to read %x-%x which is out of chip\n",
803 (unsigned) addr,
804 (unsigned) addr+(unsigned) len);
805 return -1;
806 }
807
808 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200809 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100810
811 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200812 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100813 if (block_len > (~addr & 0xff))
814 block_len = (~addr & 0xff) + 1;
815 ich_hwseq_set_addr(addr);
Arthur Heymans02c99712018-03-28 18:49:27 +0200816 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100817 hsfc &= ~HSFC_FCYCLE; /* set read operation */
818 hsfc &= ~HSFC_FDBC; /* clear byte count */
819 /* set byte count */
820 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
821 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200822 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100823
824 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
825 return 1;
826 ich_read_data(buf, block_len);
827 addr += block_len;
828 buf += block_len;
829 len -= block_len;
830 }
831 return 0;
832}
833
834/* Fill len bytes from the data array into the fdata/spid registers.
835 *
836 * Note that using len > flash->pgm->spi.max_data_write will trash the registers
837 * following the data registers.
838 */
839static void ich_fill_data(const uint8_t *data, int len)
840{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200841 struct ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100842 uint32_t temp32 = 0;
843 int i;
844
845 if (len <= 0)
846 return;
847
848 for (i = 0; i < len; i++) {
849 if ((i % 4) == 0)
850 temp32 = 0;
851
852 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
853
854 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200855 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100856 }
857 i--;
858 if ((i % 4) != 3) /* Write remaining data to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200859 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100860}
861
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800862static int ich_hwseq_write(const struct spi_flash *flash, u32 addr, size_t len,
863 const void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100864{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200865 struct ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100866 uint16_t hsfc;
867 uint16_t timeout = 100 * 60;
868 uint8_t block_len;
869 uint32_t start = addr;
870
871 if (addr + len > flash->size) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100872 printk(BIOS_ERR,
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100873 "Attempt to write 0x%x-0x%x which is out of chip\n",
874 (unsigned)addr, (unsigned) (addr+len));
875 return -1;
876 }
877
878 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200879 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100880
881 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200882 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100883 if (block_len > (~addr & 0xff))
884 block_len = (~addr & 0xff) + 1;
885
886 ich_hwseq_set_addr(addr);
887
888 ich_fill_data(buf, block_len);
Arthur Heymans02c99712018-03-28 18:49:27 +0200889 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100890 hsfc &= ~HSFC_FCYCLE; /* clear operation */
891 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
892 hsfc &= ~HSFC_FDBC; /* clear byte count */
893 /* set byte count */
894 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
895 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200896 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100897
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100898 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len)) {
899 printk(BIOS_ERR, "SF: write failure at %x\n",
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100900 addr);
901 return -1;
902 }
903 addr += block_len;
904 buf += block_len;
905 len -= block_len;
906 }
907 printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n",
908 (unsigned) (addr - start), start);
909 return 0;
910}
911
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700912static const struct spi_flash_ops spi_flash_ops = {
913 .read = ich_hwseq_read,
914 .write = ich_hwseq_write,
915 .erase = ich_hwseq_erase,
916};
917
Furquan Shaikha1491572017-05-17 19:14:06 -0700918static int spi_flash_programmer_probe(const struct spi_slave *spi,
919 struct spi_flash *flash)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100920{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200921 struct ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100922
Julius Wernercd49cce2019-03-05 16:53:33 -0800923 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX))
Arthur Heymansc88e3702017-08-20 20:50:17 +0200924 return spi_flash_generic_probe(spi, flash);
925
Furquan Shaikha1491572017-05-17 19:14:06 -0700926 /* Try generic probing first if spi_is_multichip returns 0. */
927 if (!spi_is_multichip() && !spi_flash_generic_probe(spi, flash))
928 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100929
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800930 memcpy(&flash->spi, spi, sizeof(*spi));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100931 flash->name = "Opaque HW-sequencing";
932
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100933 ich_hwseq_set_addr(0);
934 switch ((cntlr->hsfs >> 3) & 3) {
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100935 case 0:
936 flash->sector_size = 256;
937 break;
938 case 1:
939 flash->sector_size = 4096;
940 break;
941 case 2:
942 flash->sector_size = 8192;
943 break;
944 case 3:
945 flash->sector_size = 65536;
946 break;
947 }
948
Stefan Tauner327205d2018-08-26 13:53:16 +0200949 flash->size = 1 << (19 + (cntlr->flcomp & 7));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100950
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700951 flash->ops = &spi_flash_ops;
952
Arthur Heymans02c99712018-03-28 18:49:27 +0200953 if ((cntlr->hsfs & HSFS_FDV) && ((cntlr->flmap0 >> 8) & 3))
Stefan Tauner327205d2018-08-26 13:53:16 +0200954 flash->size += 1 << (19 + ((cntlr->flcomp >> 3) & 7));
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100955 printk(BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100956
Furquan Shaikh30221b42017-05-15 14:35:15 -0700957 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100958}
Furquan Shaikha1491572017-05-17 19:14:06 -0700959
Aaron Durbin851dde82018-04-19 21:15:25 -0600960static int xfer_vectors(const struct spi_slave *slave,
961 struct spi_op vectors[], size_t count)
962{
963 return spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer);
964}
965
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100966#define SPI_FPR_SHIFT 12
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100967#define ICH7_SPI_FPR_MASK 0xfff
968#define ICH9_SPI_FPR_MASK 0x1fff
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100969#define SPI_FPR_BASE_SHIFT 0
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100970#define ICH7_SPI_FPR_LIMIT_SHIFT 12
971#define ICH9_SPI_FPR_LIMIT_SHIFT 16
972#define ICH9_SPI_FPR_RPE (1 << 15) /* Read Protect */
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100973#define SPI_FPR_WPE (1 << 31) /* Write Protect */
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100974
975static u32 spi_fpr(u32 base, u32 limit)
976{
977 u32 ret;
978 u32 mask, limit_shift;
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100979
Julius Wernercd49cce2019-03-05 16:53:33 -0800980 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100981 mask = ICH7_SPI_FPR_MASK;
982 limit_shift = ICH7_SPI_FPR_LIMIT_SHIFT;
983 } else {
984 mask = ICH9_SPI_FPR_MASK;
985 limit_shift = ICH9_SPI_FPR_LIMIT_SHIFT;
986 }
987 ret = ((limit >> SPI_FPR_SHIFT) & mask) << limit_shift;
988 ret |= ((base >> SPI_FPR_SHIFT) & mask) << SPI_FPR_BASE_SHIFT;
989 return ret;
990}
991
992/*
993 * Protect range of SPI flash defined by [start, start+size-1] using Flash
994 * Protected Range (FPR) register if available.
995 * Returns 0 on success, -1 on failure of programming fpr registers.
996 */
997static int spi_flash_protect(const struct spi_flash *flash,
Rizwan Qureshif9f50932018-12-31 15:19:16 +0530998 const struct region *region,
999 const enum ctrlr_prot_type type)
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001000{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +02001001 struct ich_spi_controller *cntlr = &g_cntlr;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001002 u32 start = region_offset(region);
1003 u32 end = start + region_sz(region) - 1;
1004 u32 reg;
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301005 u32 protect_mask = 0;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001006 int fpr;
1007 uint32_t *fpr_base;
1008
Arthur Heymans02c99712018-03-28 18:49:27 +02001009 fpr_base = cntlr->fpr;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001010
1011 /* Find first empty FPR */
Arthur Heymans02c99712018-03-28 18:49:27 +02001012 for (fpr = 0; fpr < cntlr->fpr_max; fpr++) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001013 reg = read32(&fpr_base[fpr]);
1014 if (reg == 0)
1015 break;
1016 }
1017
Arthur Heymans02c99712018-03-28 18:49:27 +02001018 if (fpr == cntlr->fpr_max) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001019 printk(BIOS_ERR, "ERROR: No SPI FPR free!\n");
1020 return -1;
1021 }
1022
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301023 switch (type) {
1024 case WRITE_PROTECT:
1025 protect_mask |= SPI_FPR_WPE;
1026 break;
1027 case READ_PROTECT:
Julius Wernercd49cce2019-03-05 16:53:33 -08001028 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX))
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301029 return -1;
1030 protect_mask |= ICH9_SPI_FPR_RPE;
1031 break;
1032 case READ_WRITE_PROTECT:
Julius Wernercd49cce2019-03-05 16:53:33 -08001033 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX))
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301034 return -1;
1035 protect_mask |= (ICH9_SPI_FPR_RPE | SPI_FPR_WPE);
1036 break;
1037 default:
1038 printk(BIOS_ERR, "ERROR: Seeking invalid protection!\n");
1039 return -1;
1040 }
1041
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001042 /* Set protected range base and limit */
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301043 reg = spi_fpr(start, end) | protect_mask;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001044
1045 /* Set the FPR register and verify it is protected */
1046 write32(&fpr_base[fpr], reg);
Arthur Heymansf9572012019-06-11 11:15:10 +02001047 if (reg != read32(&fpr_base[fpr])) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001048 printk(BIOS_ERR, "ERROR: Unable to set SPI FPR %d\n", fpr);
1049 return -1;
1050 }
1051
1052 printk(BIOS_INFO, "%s: FPR %d is enabled for range 0x%08x-0x%08x\n",
1053 __func__, fpr, start, end);
1054 return 0;
1055}
1056
Arthur Heymans92185e32019-05-28 13:06:34 +02001057void spi_finalize_ops(void)
1058{
1059 struct ich_spi_controller *cntlr = &g_cntlr;
1060 u16 spi_opprefix;
1061 u16 optype = 0;
1062 struct intel_swseq_spi_config spi_config = {
1063 {0x06, 0x50}, /* OPPREFIXES: EWSR and WREN */
1064 { /* OPTYPE and OPCODE */
1065 {0x01, WRITE_NO_ADDR}, /* WRSR: Write Status Register */
1066 {0x02, WRITE_WITH_ADDR}, /* BYPR: Byte Program */
1067 {0x03, READ_WITH_ADDR}, /* READ: Read Data */
1068 {0x05, READ_NO_ADDR}, /* RDSR: Read Status Register */
1069 {0x20, WRITE_WITH_ADDR}, /* SE20: Sector Erase 0x20 */
1070 {0x9f, READ_NO_ADDR}, /* RDID: Read ID */
1071 {0xd8, WRITE_WITH_ADDR}, /* BED8: Block Erase 0xd8 */
1072 {0x0b, READ_WITH_ADDR}, /* FAST: Fast Read */
1073 }
1074 };
1075 int i;
1076
1077 if (spi_locked())
1078 return;
1079
1080 intel_southbridge_override_spi(&spi_config);
1081
1082 spi_opprefix = spi_config.opprefixes[0]
1083 | (spi_config.opprefixes[1] << 8);
1084 writew_(spi_opprefix, cntlr->preop);
1085 for (i = 0; i < ARRAY_SIZE(spi_config.ops); i++) {
1086 optype |= (spi_config.ops[i].type & 3) << (i * 2);
1087 writeb_(spi_config.ops[i].op, &cntlr->opmenu[i]);
1088 }
Nico Hubereaeb0b72019-07-27 13:45:58 +02001089 writew_(optype, cntlr->optype);
Arthur Heymans92185e32019-05-28 13:06:34 +02001090}
1091
1092__weak void intel_southbridge_override_spi(struct intel_swseq_spi_config *spi_config)
1093{
1094}
1095
Furquan Shaikha1491572017-05-17 19:14:06 -07001096static const struct spi_ctrlr spi_ctrlr = {
Aaron Durbin851dde82018-04-19 21:15:25 -06001097 .xfer_vector = xfer_vectors,
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +02001098 .max_xfer_size = member_size(struct ich9_spi_regs, fdata),
Furquan Shaikha1491572017-05-17 19:14:06 -07001099 .flash_probe = spi_flash_programmer_probe,
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001100 .flash_protect = spi_flash_protect,
Furquan Shaikha1491572017-05-17 19:14:06 -07001101};
1102
Furquan Shaikh2cd03f12017-05-18 14:58:32 -07001103const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
1104 {
1105 .ctrlr = &spi_ctrlr,
1106 .bus_start = 0,
1107 .bus_end = 0,
1108 },
1109};
1110
1111const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);