blob: e8c8f01407c54586258102d07fb183f157466eb1 [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>
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
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010039static int spi_is_multichip(void);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010040
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020041struct ich7_spi_regs {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070042 uint16_t spis;
43 uint16_t spic;
44 uint32_t spia;
45 uint64_t spid[8];
46 uint64_t _pad;
47 uint32_t bbar;
48 uint16_t preop;
49 uint16_t optype;
50 uint8_t opmenu[8];
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +010051 uint32_t pbr[3];
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020052} __packed;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070053
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020054struct ich9_spi_regs {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070055 uint32_t bfpr;
56 uint16_t hsfs;
57 uint16_t hsfc;
58 uint32_t faddr;
59 uint32_t _reserved0;
60 uint32_t fdata[16];
61 uint32_t frap;
62 uint32_t freg[5];
63 uint32_t _reserved1[3];
64 uint32_t pr[5];
65 uint32_t _reserved2[2];
66 uint8_t ssfs;
67 uint8_t ssfc[3];
68 uint16_t preop;
69 uint16_t optype;
70 uint8_t opmenu[8];
71 uint32_t bbar;
72 uint8_t _reserved3[12];
73 uint32_t fdoc;
74 uint32_t fdod;
75 uint8_t _reserved4[8];
76 uint32_t afc;
77 uint32_t lvscc;
78 uint32_t uvscc;
79 uint8_t _reserved5[4];
80 uint32_t fpb;
81 uint8_t _reserved6[28];
82 uint32_t srdl;
83 uint32_t srdc;
84 uint32_t srd;
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020085} __packed;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070086
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +020087struct ich_spi_controller {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070088 int locked;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010089 uint32_t flmap0;
Stefan Tauner327205d2018-08-26 13:53:16 +020090 uint32_t flcomp;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010091 uint32_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070092
Arthur Heymans21c5d432019-06-15 18:23:29 +020093 union {
94 struct ich9_spi_regs *ich9_spi;
95 struct ich7_spi_regs *ich7_spi;
96 };
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070097 uint8_t *opmenu;
98 int menubytes;
99 uint16_t *preop;
100 uint16_t *optype;
101 uint32_t *addr;
102 uint8_t *data;
103 unsigned databytes;
104 uint8_t *status;
105 uint16_t *control;
106 uint32_t *bbar;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100107 uint32_t *fpr;
108 uint8_t fpr_max;
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200109};
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700110
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200111static struct ich_spi_controller g_cntlr;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700112
113enum {
114 SPIS_SCIP = 0x0001,
115 SPIS_GRANT = 0x0002,
116 SPIS_CDS = 0x0004,
117 SPIS_FCERR = 0x0008,
118 SSFS_AEL = 0x0010,
119 SPIS_LOCK = 0x8000,
120 SPIS_RESERVED_MASK = 0x7ff0,
121 SSFS_RESERVED_MASK = 0x7fe2
122};
123
124enum {
125 SPIC_SCGO = 0x000002,
126 SPIC_ACS = 0x000004,
127 SPIC_SPOP = 0x000008,
128 SPIC_DBC = 0x003f00,
129 SPIC_DS = 0x004000,
130 SPIC_SME = 0x008000,
131 SSFC_SCF_MASK = 0x070000,
132 SSFC_RESERVED = 0xf80000
133};
134
135enum {
136 HSFS_FDONE = 0x0001,
137 HSFS_FCERR = 0x0002,
138 HSFS_AEL = 0x0004,
139 HSFS_BERASE_MASK = 0x0018,
140 HSFS_BERASE_SHIFT = 3,
141 HSFS_SCIP = 0x0020,
142 HSFS_FDOPSS = 0x2000,
143 HSFS_FDV = 0x4000,
144 HSFS_FLOCKDN = 0x8000
145};
146
147enum {
148 HSFC_FGO = 0x0001,
149 HSFC_FCYCLE_MASK = 0x0006,
150 HSFC_FCYCLE_SHIFT = 1,
151 HSFC_FDBC_MASK = 0x3f00,
152 HSFC_FDBC_SHIFT = 8,
153 HSFC_FSMIE = 0x8000
154};
155
156enum {
157 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
158 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
159 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
160 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
161};
162
Julius Wernercd49cce2019-03-05 16:53:33 -0800163#if CONFIG(DEBUG_SPI_FLASH)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700164
165static u8 readb_(const void *addr)
166{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800167 u8 v = read8(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100168
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700169 printk(BIOS_DEBUG, "read %2.2x from %4.4x\n",
170 v, ((unsigned) addr & 0xffff) - 0xf020);
171 return v;
172}
173
174static u16 readw_(const void *addr)
175{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800176 u16 v = read16(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100177
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700178 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
179 v, ((unsigned) addr & 0xffff) - 0xf020);
180 return v;
181}
182
183static u32 readl_(const void *addr)
184{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800185 u32 v = read32(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100186
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700187 printk(BIOS_DEBUG, "read %8.8x from %4.4x\n",
188 v, ((unsigned) addr & 0xffff) - 0xf020);
189 return v;
190}
191
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800192static void writeb_(u8 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700193{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800194 write8(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700195 printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n",
196 b, ((unsigned) addr & 0xffff) - 0xf020);
197}
198
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800199static void writew_(u16 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700200{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800201 write16(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700202 printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n",
203 b, ((unsigned) addr & 0xffff) - 0xf020);
204}
205
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800206static void writel_(u32 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700207{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800208 write32(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700209 printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n",
210 b, ((unsigned) addr & 0xffff) - 0xf020);
211}
212
213#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
214
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800215#define readb_(a) read8(a)
216#define readw_(a) read16(a)
217#define readl_(a) read32(a)
218#define writeb_(val, addr) write8(addr, val)
219#define writew_(val, addr) write16(addr, val)
220#define writel_(val, addr) write32(addr, val)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700221
222#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
223
224static void write_reg(const void *value, void *dest, uint32_t size)
225{
226 const uint8_t *bvalue = value;
227 uint8_t *bdest = dest;
228
229 while (size >= 4) {
230 writel_(*(const uint32_t *)bvalue, bdest);
231 bdest += 4; bvalue += 4; size -= 4;
232 }
233 while (size) {
234 writeb_(*bvalue, bdest);
235 bdest++; bvalue++; size--;
236 }
237}
238
239static void read_reg(const void *src, void *value, uint32_t size)
240{
241 const uint8_t *bsrc = src;
242 uint8_t *bvalue = value;
243
244 while (size >= 4) {
245 *(uint32_t *)bvalue = readl_(bsrc);
246 bsrc += 4; bvalue += 4; size -= 4;
247 }
248 while (size) {
249 *bvalue = readb_(bsrc);
250 bsrc++; bvalue++; size--;
251 }
252}
253
254static void ich_set_bbar(uint32_t minaddr)
255{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200256 struct ich_spi_controller *cntlr = &g_cntlr;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700257 const uint32_t bbar_mask = 0x00ffff00;
258 uint32_t ichspi_bbar;
259
260 minaddr &= bbar_mask;
Arthur Heymans02c99712018-03-28 18:49:27 +0200261 ichspi_bbar = readl_(cntlr->bbar) & ~bbar_mask;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700262 ichspi_bbar |= minaddr;
Arthur Heymans02c99712018-03-28 18:49:27 +0200263 writel_(ichspi_bbar, cntlr->bbar);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700264}
265
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700266void spi_init(void)
267{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200268 struct ich_spi_controller *cntlr = &g_cntlr;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700269 uint8_t *rcrb; /* Root Complex Register Block */
270 uint32_t rcba; /* Root Complex Base Address */
271 uint8_t bios_cntl;
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200272 struct ich9_spi_regs *ich9_spi;
273 struct ich7_spi_regs *ich7_spi;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100274 uint16_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700275
Arthur Heymans02c99712018-03-28 18:49:27 +0200276#ifdef __SIMPLE_DEVICE__
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200277 pci_devfn_t dev = PCI_DEV(0, 31, 0);
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700278#else
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300279 struct device *dev = pcidev_on_root(31, 0);
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700280#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700281
Kyösti Mälkki7ba14402019-02-07 12:44:00 +0200282 rcba = pci_read_config32(dev, 0xf0);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700283 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
284 rcrb = (uint8_t *)(rcba & 0xffffc000);
Julius Wernercd49cce2019-03-05 16:53:33 -0800285 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) {
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200286 ich7_spi = (struct ich7_spi_regs *)(rcrb + 0x3020);
Arthur Heymans21c5d432019-06-15 18:23:29 +0200287 cntlr->ich7_spi = ich7_spi;
Arthur Heymans02c99712018-03-28 18:49:27 +0200288 cntlr->opmenu = ich7_spi->opmenu;
289 cntlr->menubytes = sizeof(ich7_spi->opmenu);
290 cntlr->optype = &ich7_spi->optype;
291 cntlr->addr = &ich7_spi->spia;
292 cntlr->data = (uint8_t *)ich7_spi->spid;
293 cntlr->databytes = sizeof(ich7_spi->spid);
294 cntlr->status = (uint8_t *)&ich7_spi->spis;
Arthur Heymans02c99712018-03-28 18:49:27 +0200295 cntlr->control = &ich7_spi->spic;
296 cntlr->bbar = &ich7_spi->bbar;
297 cntlr->preop = &ich7_spi->preop;
298 cntlr->fpr = &ich7_spi->pbr[0];
299 cntlr->fpr_max = 3;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200300 } else {
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200301 ich9_spi = (struct ich9_spi_regs *)(rcrb + 0x3800);
Arthur Heymans02c99712018-03-28 18:49:27 +0200302 cntlr->ich9_spi = ich9_spi;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200303 hsfs = readw_(&ich9_spi->hsfs);
Arthur Heymans02c99712018-03-28 18:49:27 +0200304 cntlr->hsfs = hsfs;
305 cntlr->opmenu = ich9_spi->opmenu;
306 cntlr->menubytes = sizeof(ich9_spi->opmenu);
307 cntlr->optype = &ich9_spi->optype;
308 cntlr->addr = &ich9_spi->faddr;
309 cntlr->data = (uint8_t *)ich9_spi->fdata;
310 cntlr->databytes = sizeof(ich9_spi->fdata);
311 cntlr->status = &ich9_spi->ssfs;
312 cntlr->control = (uint16_t *)ich9_spi->ssfc;
313 cntlr->bbar = &ich9_spi->bbar;
314 cntlr->preop = &ich9_spi->preop;
315 cntlr->fpr = &ich9_spi->pr[0];
316 cntlr->fpr_max = 5;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700317
Arthur Heymans02c99712018-03-28 18:49:27 +0200318 if (cntlr->hsfs & HSFS_FDV) {
Patrick Georgic88828d2018-11-26 10:42:59 +0100319 writel_(4, &ich9_spi->fdoc);
Arthur Heymans02c99712018-03-28 18:49:27 +0200320 cntlr->flmap0 = readl_(&ich9_spi->fdod);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100321 writel_(0x1000, &ich9_spi->fdoc);
Stefan Tauner327205d2018-08-26 13:53:16 +0200322 cntlr->flcomp = readl_(&ich9_spi->fdod);
Arthur Heymansc88e3702017-08-20 20:50:17 +0200323 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700324 }
325
326 ich_set_bbar(0);
327
328 /* Disable the BIOS write protect so write commands are allowed. */
Kyösti Mälkki7ba14402019-02-07 12:44:00 +0200329 bios_cntl = pci_read_config8(dev, 0xdc);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100330 /* Deassert SMM BIOS Write Protect Disable. */
331 bios_cntl &= ~(1 << 5);
Kyösti Mälkki7ba14402019-02-07 12:44:00 +0200332 pci_write_config8(dev, 0xdc, bios_cntl | 0x1);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700333}
Aaron Durbin4d3de7e2015-09-02 17:34:04 -0500334
Arthur Heymans816aaba2019-06-11 11:10:25 +0200335static int spi_locked(void)
336{
337 struct ich_spi_controller *cntlr = &g_cntlr;
338 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) {
339 return !!(readw_(&cntlr->ich7_spi->spis) & HSFS_FLOCKDN);
340 } else {
341 return !!(readw_(&cntlr->ich9_spi->hsfs) | HSFS_FLOCKDN);
342 }
343}
344
David Hendricksf2612a12014-04-13 16:27:02 -0700345static void spi_init_cb(void *unused)
346{
347 spi_init();
348}
349
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500350BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700351
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700352typedef struct spi_transaction {
353 const uint8_t *out;
354 uint32_t bytesout;
355 uint8_t *in;
356 uint32_t bytesin;
357 uint8_t type;
358 uint8_t opcode;
359 uint32_t offset;
360} spi_transaction;
361
362static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
363{
364 trans->out += bytes;
365 trans->bytesout -= bytes;
366}
367
368static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
369{
370 trans->in += bytes;
371 trans->bytesin -= bytes;
372}
373
374static void spi_setup_type(spi_transaction *trans)
375{
376 trans->type = 0xFF;
377
378 /* Try to guess spi type from read/write sizes. */
379 if (trans->bytesin == 0) {
380 if (trans->bytesout > 4)
381 /*
382 * If bytesin = 0 and bytesout > 4, we presume this is
383 * a write data operation, which is accompanied by an
384 * address.
385 */
386 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
387 else
388 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
389 return;
390 }
391
392 if (trans->bytesout == 1) { /* and bytesin is > 0 */
393 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
394 return;
395 }
396
397 if (trans->bytesout == 4) { /* and bytesin is > 0 */
398 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
399 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700400
401 /* Fast read command is called with 5 bytes instead of 4 */
402 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
403 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
404 --trans->bytesout;
405 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700406}
407
408static int spi_setup_opcode(spi_transaction *trans)
409{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200410 struct ich_spi_controller *cntlr = &g_cntlr;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700411 uint16_t optypes;
Arthur Heymans02c99712018-03-28 18:49:27 +0200412 uint8_t opmenu[cntlr->menubytes];
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700413
414 trans->opcode = trans->out[0];
415 spi_use_out(trans, 1);
Arthur Heymans816aaba2019-06-11 11:10:25 +0200416 if (!spi_locked()) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700417 /* The lock is off, so just use index 0. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200418 writeb_(trans->opcode, cntlr->opmenu);
419 optypes = readw_(cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700420 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
Arthur Heymans02c99712018-03-28 18:49:27 +0200421 writew_(optypes, cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700422 return 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700423 }
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100424
425 /* The lock is on. See if what we need is on the menu. */
426 uint8_t optype;
427 uint16_t opcode_index;
428
429 /* Write Enable is handled as atomic prefix */
430 if (trans->opcode == SPI_OPCODE_WREN)
431 return 0;
432
433 read_reg(cntlr->opmenu, opmenu, sizeof(opmenu));
434 for (opcode_index = 0; opcode_index < cntlr->menubytes;
435 opcode_index++) {
436 if (opmenu[opcode_index] == trans->opcode)
437 break;
438 }
439
440 if (opcode_index == cntlr->menubytes) {
441 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
442 trans->opcode);
443 return -1;
444 }
445
446 optypes = readw_(cntlr->optype);
447 optype = (optypes >> (opcode_index * 2)) & 0x3;
448 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
449 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
450 trans->bytesout >= 3) {
451 /* We guessed wrong earlier. Fix it up. */
452 trans->type = optype;
453 }
454 if (optype != trans->type) {
455 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
456 optype);
457 return -1;
458 }
459 return opcode_index;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700460}
461
462static int spi_setup_offset(spi_transaction *trans)
463{
464 /* Separate the SPI address and data. */
465 switch (trans->type) {
466 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
467 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
468 return 0;
469 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
470 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
471 trans->offset = ((uint32_t)trans->out[0] << 16) |
472 ((uint32_t)trans->out[1] << 8) |
473 ((uint32_t)trans->out[2] << 0);
474 spi_use_out(trans, 3);
475 return 1;
476 default:
477 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
478 return -1;
479 }
480}
481
482/*
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200483 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700484 * below is True) or 0. In case the wait was for the bit(s) to set - write
485 * those bits back, which would cause resetting them.
486 *
487 * Return the last read status value on success or -1 on failure.
488 */
489static int ich_status_poll(u16 bitmask, int wait_til_set)
490{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200491 struct ich_spi_controller *cntlr = &g_cntlr;
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200492 int timeout = 600000; /* This will result in 6 seconds */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700493 u16 status = 0;
494
495 while (timeout--) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200496 status = readw_(cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700497 if (wait_til_set ^ ((status & bitmask) == 0)) {
498 if (wait_til_set)
Arthur Heymans02c99712018-03-28 18:49:27 +0200499 writew_((status & bitmask), cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700500 return status;
501 }
502 udelay(10);
503 }
504
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200505 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700506 status, bitmask);
507 return -1;
508}
509
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100510static int spi_is_multichip(void)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100511{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200512 struct ich_spi_controller *cntlr = &g_cntlr;
Arthur Heymans02c99712018-03-28 18:49:27 +0200513 if (!(cntlr->hsfs & HSFS_FDV))
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100514 return 0;
Arthur Heymans02c99712018-03-28 18:49:27 +0200515 return !!((cntlr->flmap0 >> 8) & 3);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100516}
517
Furquan Shaikh94f86992016-12-01 07:12:32 -0800518static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800519 size_t bytesout, void *din, size_t bytesin)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700520{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200521 struct ich_spi_controller *cntlr = &g_cntlr;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700522 uint16_t control;
523 int16_t opcode_index;
524 int with_address;
525 int status;
526
527 spi_transaction trans = {
Gabe Black93d9f922014-03-27 21:52:43 -0700528 dout, bytesout,
529 din, bytesin,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700530 0xff, 0xff, 0
531 };
532
533 /* There has to always at least be an opcode. */
Gabe Black93d9f922014-03-27 21:52:43 -0700534 if (!bytesout || !dout) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700535 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
536 return -1;
537 }
538 /* Make sure if we read something we have a place to put it. */
Gabe Black93d9f922014-03-27 21:52:43 -0700539 if (bytesin != 0 && !din) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700540 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
541 return -1;
542 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700543
544 if (ich_status_poll(SPIS_SCIP, 0) == -1)
545 return -1;
546
Arthur Heymans02c99712018-03-28 18:49:27 +0200547 writew_(SPIS_CDS | SPIS_FCERR, cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700548
549 spi_setup_type(&trans);
550 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
551 return -1;
552 if ((with_address = spi_setup_offset(&trans)) < 0)
553 return -1;
554
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700555 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700556 /*
557 * Treat Write Enable as Atomic Pre-Op if possible
558 * in order to prevent the Management Engine from
559 * issuing a transaction between WREN and DATA.
560 */
Arthur Heymans816aaba2019-06-11 11:10:25 +0200561 if (!spi_locked())
Arthur Heymans02c99712018-03-28 18:49:27 +0200562 writew_(trans.opcode, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700563 return 0;
564 }
565
566 /* Preset control fields */
567 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
568
569 /* Issue atomic preop cycle if needed */
Arthur Heymans02c99712018-03-28 18:49:27 +0200570 if (readw_(cntlr->preop))
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700571 control |= SPIC_ACS;
572
573 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700574 /* SPI addresses are 24 bit only */
575 if (with_address)
Arthur Heymans02c99712018-03-28 18:49:27 +0200576 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700577
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700578 /*
579 * This is a 'no data' command (like Write Enable), its
580 * bitesout size was 1, decremented to zero while executing
581 * spi_setup_opcode() above. Tell the chip to send the
582 * command.
583 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200584 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700585
586 /* wait for the result */
587 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
588 if (status == -1)
589 return -1;
590
591 if (status & SPIS_FCERR) {
592 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
593 return -1;
594 }
595
Werner Zehf13a6f92018-11-14 10:55:52 +0100596 goto spi_xfer_exit;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700597 }
598
599 /*
Paul Menzel94782972013-06-29 11:41:27 +0200600 * Check if this is a write command attempting to transfer more bytes
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700601 * than the controller can handle. Iterations for writes are not
602 * supported here because each SPI write command needs to be preceded
603 * and followed by other SPI commands, and this sequence is controlled
604 * by the SPI chip driver.
605 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200606 if (trans.bytesout > cntlr->databytes) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700607 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
Kyösti Mälkki11104952014-06-29 16:17:33 +0300608 " spi_crop_chunk()?\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700609 return -1;
610 }
611
612 /*
613 * Read or write up to databytes bytes at a time until everything has
614 * been sent.
615 */
616 while (trans.bytesout || trans.bytesin) {
617 uint32_t data_length;
618
619 /* SPI addresses are 24 bit only */
Arthur Heymans02c99712018-03-28 18:49:27 +0200620 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700621
622 if (trans.bytesout)
Arthur Heymans02c99712018-03-28 18:49:27 +0200623 data_length = min(trans.bytesout, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700624 else
Arthur Heymans02c99712018-03-28 18:49:27 +0200625 data_length = min(trans.bytesin, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700626
627 /* Program data into FDATA0 to N */
628 if (trans.bytesout) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200629 write_reg(trans.out, cntlr->data, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700630 spi_use_out(&trans, data_length);
631 if (with_address)
632 trans.offset += data_length;
633 }
634
635 /* Add proper control fields' values */
Arthur Heymans02c99712018-03-28 18:49:27 +0200636 control &= ~((cntlr->databytes - 1) << 8);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700637 control |= SPIC_DS;
638 control |= (data_length - 1) << 8;
639
640 /* write it */
Arthur Heymans02c99712018-03-28 18:49:27 +0200641 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700642
643 /* Wait for Cycle Done Status or Flash Cycle Error. */
644 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
645 if (status == -1)
646 return -1;
647
648 if (status & SPIS_FCERR) {
649 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
650 return -1;
651 }
652
653 if (trans.bytesin) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200654 read_reg(cntlr->data, trans.in, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700655 spi_use_in(&trans, data_length);
656 if (with_address)
657 trans.offset += data_length;
658 }
659 }
660
Werner Zehf13a6f92018-11-14 10:55:52 +0100661spi_xfer_exit:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700662 /* Clear atomic preop now that xfer is done */
Arthur Heymans02c99712018-03-28 18:49:27 +0200663 writew_(0, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700664
665 return 0;
666}
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100667
668/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
669static void ich_hwseq_set_addr(uint32_t addr)
670{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200671 struct ich_spi_controller *cntlr = &g_cntlr;
Arthur Heymans02c99712018-03-28 18:49:27 +0200672 uint32_t addr_old = readl_(&cntlr->ich9_spi->faddr) & ~0x01FFFFFF;
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100673
Arthur Heymans02c99712018-03-28 18:49:27 +0200674 writel_((addr & 0x01FFFFFF) | addr_old, &cntlr->ich9_spi->faddr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100675}
676
677/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
678 Resets all error flags in HSFS.
679 Returns 0 if the cycle completes successfully without errors within
680 timeout us, 1 on errors. */
681static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
682 unsigned int len)
683{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200684 struct ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100685 uint16_t hsfs;
686 uint32_t addr;
687
688 timeout /= 8; /* scale timeout duration to counter */
Arthur Heymans02c99712018-03-28 18:49:27 +0200689 while ((((hsfs = readw_(&cntlr->ich9_spi->hsfs)) &
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100690 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
691 --timeout) {
692 udelay(8);
693 }
Arthur Heymans02c99712018-03-28 18:49:27 +0200694 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100695
696 if (!timeout) {
697 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200698 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
699 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100700 printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and "
701 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
702 addr, addr + len - 1, addr, len - 1,
703 hsfc, hsfs);
704 return 1;
705 }
706
707 if (hsfs & HSFS_FCERR) {
708 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200709 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
710 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100711 printk(BIOS_ERR, "Transaction error between offset 0x%08x and "
712 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
713 addr, addr + len - 1, addr, len - 1,
714 hsfc, hsfs);
715 return 1;
716 }
717 return 0;
718}
719
720
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800721static int ich_hwseq_erase(const struct spi_flash *flash, u32 offset,
722 size_t len)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100723{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200724 struct ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100725 u32 start, end, erase_size;
726 int ret;
727 uint16_t hsfc;
728 uint16_t timeout = 1000 * 60;
729
730 erase_size = flash->sector_size;
731 if (offset % erase_size || len % erase_size) {
732 printk(BIOS_ERR, "SF: Erase offset/length not multiple of erase size\n");
733 return -1;
734 }
735
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800736 ret = spi_claim_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100737 if (ret) {
738 printk(BIOS_ERR, "SF: Unable to claim SPI bus\n");
739 return ret;
740 }
741
742 start = offset;
743 end = start + len;
744
745 while (offset < end) {
746 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
Arthur Heymans02c99712018-03-28 18:49:27 +0200747 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100748
749 ich_hwseq_set_addr(offset);
750
751 offset += erase_size;
752
Arthur Heymans02c99712018-03-28 18:49:27 +0200753 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100754 hsfc &= ~HSFC_FCYCLE; /* clear operation */
755 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
756 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200757 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100758 if (ich_hwseq_wait_for_cycle_complete(timeout, len)) {
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100759 printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size);
760 ret = -1;
761 goto out;
762 }
763 }
764
765 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
766
767out:
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800768 spi_release_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100769 return ret;
770}
771
772static void ich_read_data(uint8_t *data, int len)
773{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200774 struct ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100775 int i;
776 uint32_t temp32 = 0;
777
778 for (i = 0; i < len; i++) {
779 if ((i % 4) == 0)
Arthur Heymans02c99712018-03-28 18:49:27 +0200780 temp32 = readl_(cntlr->data + i);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100781
782 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
783 }
784}
785
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800786static int ich_hwseq_read(const struct spi_flash *flash, u32 addr, size_t len,
787 void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100788{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200789 struct ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100790 uint16_t hsfc;
791 uint16_t timeout = 100 * 60;
792 uint8_t block_len;
793
794 if (addr + len > flash->size) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100795 printk(BIOS_ERR,
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100796 "Attempt to read %x-%x which is out of chip\n",
797 (unsigned) addr,
798 (unsigned) addr+(unsigned) len);
799 return -1;
800 }
801
802 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200803 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100804
805 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200806 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100807 if (block_len > (~addr & 0xff))
808 block_len = (~addr & 0xff) + 1;
809 ich_hwseq_set_addr(addr);
Arthur Heymans02c99712018-03-28 18:49:27 +0200810 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100811 hsfc &= ~HSFC_FCYCLE; /* set read operation */
812 hsfc &= ~HSFC_FDBC; /* clear byte count */
813 /* set byte count */
814 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
815 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200816 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100817
818 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
819 return 1;
820 ich_read_data(buf, block_len);
821 addr += block_len;
822 buf += block_len;
823 len -= block_len;
824 }
825 return 0;
826}
827
828/* Fill len bytes from the data array into the fdata/spid registers.
829 *
830 * Note that using len > flash->pgm->spi.max_data_write will trash the registers
831 * following the data registers.
832 */
833static void ich_fill_data(const uint8_t *data, int len)
834{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200835 struct ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100836 uint32_t temp32 = 0;
837 int i;
838
839 if (len <= 0)
840 return;
841
842 for (i = 0; i < len; i++) {
843 if ((i % 4) == 0)
844 temp32 = 0;
845
846 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
847
848 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200849 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100850 }
851 i--;
852 if ((i % 4) != 3) /* Write remaining data to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200853 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100854}
855
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800856static int ich_hwseq_write(const struct spi_flash *flash, u32 addr, size_t len,
857 const void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100858{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200859 struct ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100860 uint16_t hsfc;
861 uint16_t timeout = 100 * 60;
862 uint8_t block_len;
863 uint32_t start = addr;
864
865 if (addr + len > flash->size) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100866 printk(BIOS_ERR,
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100867 "Attempt to write 0x%x-0x%x which is out of chip\n",
868 (unsigned)addr, (unsigned) (addr+len));
869 return -1;
870 }
871
872 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200873 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100874
875 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200876 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100877 if (block_len > (~addr & 0xff))
878 block_len = (~addr & 0xff) + 1;
879
880 ich_hwseq_set_addr(addr);
881
882 ich_fill_data(buf, block_len);
Arthur Heymans02c99712018-03-28 18:49:27 +0200883 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100884 hsfc &= ~HSFC_FCYCLE; /* clear operation */
885 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
886 hsfc &= ~HSFC_FDBC; /* clear byte count */
887 /* set byte count */
888 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
889 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200890 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100891
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100892 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len)) {
893 printk(BIOS_ERR, "SF: write failure at %x\n",
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100894 addr);
895 return -1;
896 }
897 addr += block_len;
898 buf += block_len;
899 len -= block_len;
900 }
901 printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n",
902 (unsigned) (addr - start), start);
903 return 0;
904}
905
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700906static const struct spi_flash_ops spi_flash_ops = {
907 .read = ich_hwseq_read,
908 .write = ich_hwseq_write,
909 .erase = ich_hwseq_erase,
910};
911
Furquan Shaikha1491572017-05-17 19:14:06 -0700912static int spi_flash_programmer_probe(const struct spi_slave *spi,
913 struct spi_flash *flash)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100914{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200915 struct ich_spi_controller *cntlr = &g_cntlr;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100916
Julius Wernercd49cce2019-03-05 16:53:33 -0800917 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX))
Arthur Heymansc88e3702017-08-20 20:50:17 +0200918 return spi_flash_generic_probe(spi, flash);
919
Furquan Shaikha1491572017-05-17 19:14:06 -0700920 /* Try generic probing first if spi_is_multichip returns 0. */
921 if (!spi_is_multichip() && !spi_flash_generic_probe(spi, flash))
922 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100923
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800924 memcpy(&flash->spi, spi, sizeof(*spi));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100925 flash->name = "Opaque HW-sequencing";
926
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100927 ich_hwseq_set_addr(0);
928 switch ((cntlr->hsfs >> 3) & 3) {
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100929 case 0:
930 flash->sector_size = 256;
931 break;
932 case 1:
933 flash->sector_size = 4096;
934 break;
935 case 2:
936 flash->sector_size = 8192;
937 break;
938 case 3:
939 flash->sector_size = 65536;
940 break;
941 }
942
Stefan Tauner327205d2018-08-26 13:53:16 +0200943 flash->size = 1 << (19 + (cntlr->flcomp & 7));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100944
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700945 flash->ops = &spi_flash_ops;
946
Arthur Heymans02c99712018-03-28 18:49:27 +0200947 if ((cntlr->hsfs & HSFS_FDV) && ((cntlr->flmap0 >> 8) & 3))
Stefan Tauner327205d2018-08-26 13:53:16 +0200948 flash->size += 1 << (19 + ((cntlr->flcomp >> 3) & 7));
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100949 printk(BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100950
Furquan Shaikh30221b42017-05-15 14:35:15 -0700951 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100952}
Furquan Shaikha1491572017-05-17 19:14:06 -0700953
Aaron Durbin851dde82018-04-19 21:15:25 -0600954static int xfer_vectors(const struct spi_slave *slave,
955 struct spi_op vectors[], size_t count)
956{
957 return spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer);
958}
959
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100960#define SPI_FPR_SHIFT 12
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100961#define ICH7_SPI_FPR_MASK 0xfff
962#define ICH9_SPI_FPR_MASK 0x1fff
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100963#define SPI_FPR_BASE_SHIFT 0
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100964#define ICH7_SPI_FPR_LIMIT_SHIFT 12
965#define ICH9_SPI_FPR_LIMIT_SHIFT 16
966#define ICH9_SPI_FPR_RPE (1 << 15) /* Read Protect */
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100967#define SPI_FPR_WPE (1 << 31) /* Write Protect */
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100968
969static u32 spi_fpr(u32 base, u32 limit)
970{
971 u32 ret;
972 u32 mask, limit_shift;
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100973
Julius Wernercd49cce2019-03-05 16:53:33 -0800974 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX)) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100975 mask = ICH7_SPI_FPR_MASK;
976 limit_shift = ICH7_SPI_FPR_LIMIT_SHIFT;
977 } else {
978 mask = ICH9_SPI_FPR_MASK;
979 limit_shift = ICH9_SPI_FPR_LIMIT_SHIFT;
980 }
981 ret = ((limit >> SPI_FPR_SHIFT) & mask) << limit_shift;
982 ret |= ((base >> SPI_FPR_SHIFT) & mask) << SPI_FPR_BASE_SHIFT;
983 return ret;
984}
985
986/*
987 * Protect range of SPI flash defined by [start, start+size-1] using Flash
988 * Protected Range (FPR) register if available.
989 * Returns 0 on success, -1 on failure of programming fpr registers.
990 */
991static int spi_flash_protect(const struct spi_flash *flash,
Rizwan Qureshif9f50932018-12-31 15:19:16 +0530992 const struct region *region,
993 const enum ctrlr_prot_type type)
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100994{
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +0200995 struct ich_spi_controller *cntlr = &g_cntlr;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100996 u32 start = region_offset(region);
997 u32 end = start + region_sz(region) - 1;
998 u32 reg;
Rizwan Qureshif9f50932018-12-31 15:19:16 +0530999 u32 protect_mask = 0;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001000 int fpr;
1001 uint32_t *fpr_base;
1002
Arthur Heymans02c99712018-03-28 18:49:27 +02001003 fpr_base = cntlr->fpr;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001004
1005 /* Find first empty FPR */
Arthur Heymans02c99712018-03-28 18:49:27 +02001006 for (fpr = 0; fpr < cntlr->fpr_max; fpr++) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001007 reg = read32(&fpr_base[fpr]);
1008 if (reg == 0)
1009 break;
1010 }
1011
Arthur Heymans02c99712018-03-28 18:49:27 +02001012 if (fpr == cntlr->fpr_max) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001013 printk(BIOS_ERR, "ERROR: No SPI FPR free!\n");
1014 return -1;
1015 }
1016
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301017 switch (type) {
1018 case WRITE_PROTECT:
1019 protect_mask |= SPI_FPR_WPE;
1020 break;
1021 case READ_PROTECT:
Julius Wernercd49cce2019-03-05 16:53:33 -08001022 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX))
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301023 return -1;
1024 protect_mask |= ICH9_SPI_FPR_RPE;
1025 break;
1026 case READ_WRITE_PROTECT:
Julius Wernercd49cce2019-03-05 16:53:33 -08001027 if (CONFIG(SOUTHBRIDGE_INTEL_I82801GX))
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301028 return -1;
1029 protect_mask |= (ICH9_SPI_FPR_RPE | SPI_FPR_WPE);
1030 break;
1031 default:
1032 printk(BIOS_ERR, "ERROR: Seeking invalid protection!\n");
1033 return -1;
1034 }
1035
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001036 /* Set protected range base and limit */
Rizwan Qureshif9f50932018-12-31 15:19:16 +05301037 reg = spi_fpr(start, end) | protect_mask;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001038
1039 /* Set the FPR register and verify it is protected */
1040 write32(&fpr_base[fpr], reg);
Arthur Heymansf9572012019-06-11 11:15:10 +02001041 if (reg != read32(&fpr_base[fpr])) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001042 printk(BIOS_ERR, "ERROR: Unable to set SPI FPR %d\n", fpr);
1043 return -1;
1044 }
1045
1046 printk(BIOS_INFO, "%s: FPR %d is enabled for range 0x%08x-0x%08x\n",
1047 __func__, fpr, start, end);
1048 return 0;
1049}
1050
Furquan Shaikha1491572017-05-17 19:14:06 -07001051static const struct spi_ctrlr spi_ctrlr = {
Aaron Durbin851dde82018-04-19 21:15:25 -06001052 .xfer_vector = xfer_vectors,
Arthur Heymansa9c1a5f2019-06-15 18:21:58 +02001053 .max_xfer_size = member_size(struct ich9_spi_regs, fdata),
Furquan Shaikha1491572017-05-17 19:14:06 -07001054 .flash_probe = spi_flash_programmer_probe,
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001055 .flash_protect = spi_flash_protect,
Furquan Shaikha1491572017-05-17 19:14:06 -07001056};
1057
Furquan Shaikh2cd03f12017-05-18 14:58:32 -07001058const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
1059 {
1060 .ctrlr = &spi_ctrlr,
1061 .bus_start = 0,
1062 .bus_end = 0,
1063 },
1064};
1065
1066const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);