blob: 180a629e32e06308da370fa9c091b1f465d2927e [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
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07005 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but without any warranty; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070015 */
16
17/* This file is derived from the flashrom project. */
Arthur Heymans02c99712018-03-28 18:49:27 +020018#include <arch/early_variables.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070019#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
Stefan Reinauer6a001132017-07-13 02:20:27 +020022#include <compiler.h>
David Hendricksf2612a12014-04-13 16:27:02 -070023#include <bootstate.h>
Furquan Shaikhde705fa2017-04-19 19:27:28 -070024#include <commonlib/helpers.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070025#include <delay.h>
26#include <arch/io.h>
27#include <console/console.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070028#include <device/pci_ids.h>
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010029#include <device/pci.h>
30#include <spi_flash.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070031
Zheng Bao600784e2013-02-07 17:30:23 +080032#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070033
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010034#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
35#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
36#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
37#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
38
39
Duncan Laurie181bbdd2012-06-23 16:53:57 -070040#ifdef __SMM__
Kyösti Mälkkib4a45dc2013-07-26 08:53:59 +030041#include <arch/io.h>
Duncan Laurie181bbdd2012-06-23 16:53:57 -070042#define pci_read_config_byte(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030043 *(targ) = pci_read_config8(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070044#define pci_read_config_word(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030045 *(targ) = pci_read_config16(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070046#define pci_read_config_dword(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030047 *(targ) = pci_read_config32(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070048#define pci_write_config_byte(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030049 pci_write_config8(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070050#define pci_write_config_word(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030051 pci_write_config16(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070052#define pci_write_config_dword(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030053 pci_write_config32(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070054#else /* !__SMM__ */
55#include <device/device.h>
56#include <device/pci.h>
Duncan Laurie181bbdd2012-06-23 16:53:57 -070057#define pci_read_config_byte(dev, reg, targ)\
58 *(targ) = pci_read_config8(dev, reg)
59#define pci_read_config_word(dev, reg, targ)\
60 *(targ) = pci_read_config16(dev, reg)
61#define pci_read_config_dword(dev, reg, targ)\
62 *(targ) = pci_read_config32(dev, reg)
63#define pci_write_config_byte(dev, reg, val)\
64 pci_write_config8(dev, reg, val)
65#define pci_write_config_word(dev, reg, val)\
66 pci_write_config16(dev, reg, val)
67#define pci_write_config_dword(dev, reg, val)\
68 pci_write_config32(dev, reg, val)
69#endif /* !__SMM__ */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070070
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010071static int spi_is_multichip(void);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010072
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070073typedef struct spi_slave ich_spi_slave;
74
Arthur Heymans02c99712018-03-28 18:49:27 +020075static int g_ichspi_lock CAR_GLOBAL = 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070076
77typedef struct ich7_spi_regs {
78 uint16_t spis;
79 uint16_t spic;
80 uint32_t spia;
81 uint64_t spid[8];
82 uint64_t _pad;
83 uint32_t bbar;
84 uint16_t preop;
85 uint16_t optype;
86 uint8_t opmenu[8];
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +010087 uint32_t pbr[3];
Stefan Reinauer6a001132017-07-13 02:20:27 +020088} __packed ich7_spi_regs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070089
90typedef struct ich9_spi_regs {
91 uint32_t bfpr;
92 uint16_t hsfs;
93 uint16_t hsfc;
94 uint32_t faddr;
95 uint32_t _reserved0;
96 uint32_t fdata[16];
97 uint32_t frap;
98 uint32_t freg[5];
99 uint32_t _reserved1[3];
100 uint32_t pr[5];
101 uint32_t _reserved2[2];
102 uint8_t ssfs;
103 uint8_t ssfc[3];
104 uint16_t preop;
105 uint16_t optype;
106 uint8_t opmenu[8];
107 uint32_t bbar;
108 uint8_t _reserved3[12];
109 uint32_t fdoc;
110 uint32_t fdod;
111 uint8_t _reserved4[8];
112 uint32_t afc;
113 uint32_t lvscc;
114 uint32_t uvscc;
115 uint8_t _reserved5[4];
116 uint32_t fpb;
117 uint8_t _reserved6[28];
118 uint32_t srdl;
119 uint32_t srdc;
120 uint32_t srd;
Stefan Reinauer6a001132017-07-13 02:20:27 +0200121} __packed ich9_spi_regs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700122
123typedef struct ich_spi_controller {
124 int locked;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100125 uint32_t flmap0;
126 uint32_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700127
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100128 ich9_spi_regs *ich9_spi;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700129 uint8_t *opmenu;
130 int menubytes;
131 uint16_t *preop;
132 uint16_t *optype;
133 uint32_t *addr;
134 uint8_t *data;
135 unsigned databytes;
136 uint8_t *status;
137 uint16_t *control;
138 uint32_t *bbar;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100139 uint32_t *fpr;
140 uint8_t fpr_max;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700141} ich_spi_controller;
142
Arthur Heymans02c99712018-03-28 18:49:27 +0200143static ich_spi_controller g_cntlr CAR_GLOBAL;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700144
145enum {
146 SPIS_SCIP = 0x0001,
147 SPIS_GRANT = 0x0002,
148 SPIS_CDS = 0x0004,
149 SPIS_FCERR = 0x0008,
150 SSFS_AEL = 0x0010,
151 SPIS_LOCK = 0x8000,
152 SPIS_RESERVED_MASK = 0x7ff0,
153 SSFS_RESERVED_MASK = 0x7fe2
154};
155
156enum {
157 SPIC_SCGO = 0x000002,
158 SPIC_ACS = 0x000004,
159 SPIC_SPOP = 0x000008,
160 SPIC_DBC = 0x003f00,
161 SPIC_DS = 0x004000,
162 SPIC_SME = 0x008000,
163 SSFC_SCF_MASK = 0x070000,
164 SSFC_RESERVED = 0xf80000
165};
166
167enum {
168 HSFS_FDONE = 0x0001,
169 HSFS_FCERR = 0x0002,
170 HSFS_AEL = 0x0004,
171 HSFS_BERASE_MASK = 0x0018,
172 HSFS_BERASE_SHIFT = 3,
173 HSFS_SCIP = 0x0020,
174 HSFS_FDOPSS = 0x2000,
175 HSFS_FDV = 0x4000,
176 HSFS_FLOCKDN = 0x8000
177};
178
179enum {
180 HSFC_FGO = 0x0001,
181 HSFC_FCYCLE_MASK = 0x0006,
182 HSFC_FCYCLE_SHIFT = 1,
183 HSFC_FDBC_MASK = 0x3f00,
184 HSFC_FDBC_SHIFT = 8,
185 HSFC_FSMIE = 0x8000
186};
187
188enum {
189 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
190 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
191 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
192 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
193};
194
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600195#if IS_ENABLED(CONFIG_DEBUG_SPI_FLASH)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700196
197static u8 readb_(const void *addr)
198{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800199 u8 v = read8(addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700200 printk(BIOS_DEBUG, "read %2.2x from %4.4x\n",
201 v, ((unsigned) addr & 0xffff) - 0xf020);
202 return v;
203}
204
205static u16 readw_(const void *addr)
206{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800207 u16 v = read16(addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700208 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
209 v, ((unsigned) addr & 0xffff) - 0xf020);
210 return v;
211}
212
213static u32 readl_(const void *addr)
214{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800215 u32 v = read32(addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700216 printk(BIOS_DEBUG, "read %8.8x from %4.4x\n",
217 v, ((unsigned) addr & 0xffff) - 0xf020);
218 return v;
219}
220
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800221static void writeb_(u8 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700222{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800223 write8(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700224 printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n",
225 b, ((unsigned) addr & 0xffff) - 0xf020);
226}
227
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800228static void writew_(u16 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700229{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800230 write16(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700231 printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n",
232 b, ((unsigned) addr & 0xffff) - 0xf020);
233}
234
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800235static void writel_(u32 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700236{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800237 write32(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700238 printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n",
239 b, ((unsigned) addr & 0xffff) - 0xf020);
240}
241
242#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
243
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800244#define readb_(a) read8(a)
245#define readw_(a) read16(a)
246#define readl_(a) read32(a)
247#define writeb_(val, addr) write8(addr, val)
248#define writew_(val, addr) write16(addr, val)
249#define writel_(val, addr) write32(addr, val)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700250
251#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
252
253static void write_reg(const void *value, void *dest, uint32_t size)
254{
255 const uint8_t *bvalue = value;
256 uint8_t *bdest = dest;
257
258 while (size >= 4) {
259 writel_(*(const uint32_t *)bvalue, bdest);
260 bdest += 4; bvalue += 4; size -= 4;
261 }
262 while (size) {
263 writeb_(*bvalue, bdest);
264 bdest++; bvalue++; size--;
265 }
266}
267
268static void read_reg(const void *src, void *value, uint32_t size)
269{
270 const uint8_t *bsrc = src;
271 uint8_t *bvalue = value;
272
273 while (size >= 4) {
274 *(uint32_t *)bvalue = readl_(bsrc);
275 bsrc += 4; bvalue += 4; size -= 4;
276 }
277 while (size) {
278 *bvalue = readb_(bsrc);
279 bsrc++; bvalue++; size--;
280 }
281}
282
283static void ich_set_bbar(uint32_t minaddr)
284{
Arthur Heymans02c99712018-03-28 18:49:27 +0200285 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700286 const uint32_t bbar_mask = 0x00ffff00;
287 uint32_t ichspi_bbar;
288
289 minaddr &= bbar_mask;
Arthur Heymans02c99712018-03-28 18:49:27 +0200290 ichspi_bbar = readl_(cntlr->bbar) & ~bbar_mask;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700291 ichspi_bbar |= minaddr;
Arthur Heymans02c99712018-03-28 18:49:27 +0200292 writel_(ichspi_bbar, cntlr->bbar);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700293}
294
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700295void spi_init(void)
296{
Arthur Heymans02c99712018-03-28 18:49:27 +0200297 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700298 uint8_t *rcrb; /* Root Complex Register Block */
299 uint32_t rcba; /* Root Complex Base Address */
300 uint8_t bios_cntl;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100301 ich9_spi_regs *ich9_spi;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200302 ich7_spi_regs *ich7_spi;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100303 uint16_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700304
Arthur Heymans02c99712018-03-28 18:49:27 +0200305#ifdef __SIMPLE_DEVICE__
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200306 pci_devfn_t dev = PCI_DEV(0, 31, 0);
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700307#else
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200308 struct device *dev = dev_find_slot(0, PCI_DEVFN(31, 0));
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700309#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700310
311 pci_read_config_dword(dev, 0xf0, &rcba);
312 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
313 rcrb = (uint8_t *)(rcba & 0xffffc000);
Arthur Heymansc88e3702017-08-20 20:50:17 +0200314 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX)) {
315 ich7_spi = (ich7_spi_regs *)(rcrb + 0x3020);
Arthur Heymans02c99712018-03-28 18:49:27 +0200316 cntlr->opmenu = ich7_spi->opmenu;
317 cntlr->menubytes = sizeof(ich7_spi->opmenu);
318 cntlr->optype = &ich7_spi->optype;
319 cntlr->addr = &ich7_spi->spia;
320 cntlr->data = (uint8_t *)ich7_spi->spid;
321 cntlr->databytes = sizeof(ich7_spi->spid);
322 cntlr->status = (uint8_t *)&ich7_spi->spis;
323 car_set_var(g_ichspi_lock, readw_(&ich7_spi->spis) & HSFS_FLOCKDN);
324 cntlr->control = &ich7_spi->spic;
325 cntlr->bbar = &ich7_spi->bbar;
326 cntlr->preop = &ich7_spi->preop;
327 cntlr->fpr = &ich7_spi->pbr[0];
328 cntlr->fpr_max = 3;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200329 } else {
330 ich9_spi = (ich9_spi_regs *)(rcrb + 0x3800);
Arthur Heymans02c99712018-03-28 18:49:27 +0200331 cntlr->ich9_spi = ich9_spi;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200332 hsfs = readw_(&ich9_spi->hsfs);
Arthur Heymans02c99712018-03-28 18:49:27 +0200333 car_set_var(g_ichspi_lock, hsfs & HSFS_FLOCKDN);
334 cntlr->hsfs = hsfs;
335 cntlr->opmenu = ich9_spi->opmenu;
336 cntlr->menubytes = sizeof(ich9_spi->opmenu);
337 cntlr->optype = &ich9_spi->optype;
338 cntlr->addr = &ich9_spi->faddr;
339 cntlr->data = (uint8_t *)ich9_spi->fdata;
340 cntlr->databytes = sizeof(ich9_spi->fdata);
341 cntlr->status = &ich9_spi->ssfs;
342 cntlr->control = (uint16_t *)ich9_spi->ssfc;
343 cntlr->bbar = &ich9_spi->bbar;
344 cntlr->preop = &ich9_spi->preop;
345 cntlr->fpr = &ich9_spi->pr[0];
346 cntlr->fpr_max = 5;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700347
Arthur Heymans02c99712018-03-28 18:49:27 +0200348 if (cntlr->hsfs & HSFS_FDV) {
Arthur Heymansc88e3702017-08-20 20:50:17 +0200349 writel_ (4, &ich9_spi->fdoc);
Arthur Heymans02c99712018-03-28 18:49:27 +0200350 cntlr->flmap0 = readl_(&ich9_spi->fdod);
Arthur Heymansc88e3702017-08-20 20:50:17 +0200351 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700352 }
353
354 ich_set_bbar(0);
355
356 /* Disable the BIOS write protect so write commands are allowed. */
357 pci_read_config_byte(dev, 0xdc, &bios_cntl);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100358 /* Deassert SMM BIOS Write Protect Disable. */
359 bios_cntl &= ~(1 << 5);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700360 pci_write_config_byte(dev, 0xdc, bios_cntl | 0x1);
361}
Aaron Durbin4d3de7e2015-09-02 17:34:04 -0500362
David Hendricksf2612a12014-04-13 16:27:02 -0700363static void spi_init_cb(void *unused)
364{
365 spi_init();
366}
367
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500368BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700369
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700370typedef struct spi_transaction {
371 const uint8_t *out;
372 uint32_t bytesout;
373 uint8_t *in;
374 uint32_t bytesin;
375 uint8_t type;
376 uint8_t opcode;
377 uint32_t offset;
378} spi_transaction;
379
380static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
381{
382 trans->out += bytes;
383 trans->bytesout -= bytes;
384}
385
386static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
387{
388 trans->in += bytes;
389 trans->bytesin -= bytes;
390}
391
392static void spi_setup_type(spi_transaction *trans)
393{
394 trans->type = 0xFF;
395
396 /* Try to guess spi type from read/write sizes. */
397 if (trans->bytesin == 0) {
398 if (trans->bytesout > 4)
399 /*
400 * If bytesin = 0 and bytesout > 4, we presume this is
401 * a write data operation, which is accompanied by an
402 * address.
403 */
404 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
405 else
406 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
407 return;
408 }
409
410 if (trans->bytesout == 1) { /* and bytesin is > 0 */
411 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
412 return;
413 }
414
415 if (trans->bytesout == 4) { /* and bytesin is > 0 */
416 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
417 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700418
419 /* Fast read command is called with 5 bytes instead of 4 */
420 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
421 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
422 --trans->bytesout;
423 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700424}
425
426static int spi_setup_opcode(spi_transaction *trans)
427{
Arthur Heymans02c99712018-03-28 18:49:27 +0200428 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700429 uint16_t optypes;
Arthur Heymans02c99712018-03-28 18:49:27 +0200430 uint8_t opmenu[cntlr->menubytes];
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700431
432 trans->opcode = trans->out[0];
433 spi_use_out(trans, 1);
Arthur Heymans02c99712018-03-28 18:49:27 +0200434 if (!car_get_var(g_ichspi_lock)) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700435 /* The lock is off, so just use index 0. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200436 writeb_(trans->opcode, cntlr->opmenu);
437 optypes = readw_(cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700438 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
Arthur Heymans02c99712018-03-28 18:49:27 +0200439 writew_(optypes, cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700440 return 0;
441 } else {
442 /* The lock is on. See if what we need is on the menu. */
443 uint8_t optype;
444 uint16_t opcode_index;
445
Duncan Lauriea2f1b952012-08-27 11:10:43 -0700446 /* Write Enable is handled as atomic prefix */
447 if (trans->opcode == SPI_OPCODE_WREN)
448 return 0;
449
Arthur Heymans02c99712018-03-28 18:49:27 +0200450 read_reg(cntlr->opmenu, opmenu, sizeof(opmenu));
451 for (opcode_index = 0; opcode_index < cntlr->menubytes;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700452 opcode_index++) {
453 if (opmenu[opcode_index] == trans->opcode)
454 break;
455 }
456
Arthur Heymans02c99712018-03-28 18:49:27 +0200457 if (opcode_index == cntlr->menubytes) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700458 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
459 trans->opcode);
460 return -1;
461 }
462
Arthur Heymans02c99712018-03-28 18:49:27 +0200463 optypes = readw_(cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700464 optype = (optypes >> (opcode_index * 2)) & 0x3;
465 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
466 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
467 trans->bytesout >= 3) {
468 /* We guessed wrong earlier. Fix it up. */
469 trans->type = optype;
470 }
471 if (optype != trans->type) {
472 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
473 optype);
474 return -1;
475 }
476 return opcode_index;
477 }
478}
479
480static int spi_setup_offset(spi_transaction *trans)
481{
482 /* Separate the SPI address and data. */
483 switch (trans->type) {
484 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
485 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
486 return 0;
487 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
488 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
489 trans->offset = ((uint32_t)trans->out[0] << 16) |
490 ((uint32_t)trans->out[1] << 8) |
491 ((uint32_t)trans->out[2] << 0);
492 spi_use_out(trans, 3);
493 return 1;
494 default:
495 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
496 return -1;
497 }
498}
499
500/*
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200501 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700502 * below is True) or 0. In case the wait was for the bit(s) to set - write
503 * those bits back, which would cause resetting them.
504 *
505 * Return the last read status value on success or -1 on failure.
506 */
507static int ich_status_poll(u16 bitmask, int wait_til_set)
508{
Arthur Heymans02c99712018-03-28 18:49:27 +0200509 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200510 int timeout = 600000; /* This will result in 6 seconds */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700511 u16 status = 0;
512
513 while (timeout--) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200514 status = readw_(cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700515 if (wait_til_set ^ ((status & bitmask) == 0)) {
516 if (wait_til_set)
Arthur Heymans02c99712018-03-28 18:49:27 +0200517 writew_((status & bitmask), cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700518 return status;
519 }
520 udelay(10);
521 }
522
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200523 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700524 status, bitmask);
525 return -1;
526}
527
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100528static int spi_is_multichip (void)
529{
Arthur Heymans02c99712018-03-28 18:49:27 +0200530 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
531 if (!(cntlr->hsfs & HSFS_FDV))
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100532 return 0;
Arthur Heymans02c99712018-03-28 18:49:27 +0200533 return !!((cntlr->flmap0 >> 8) & 3);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100534}
535
Furquan Shaikh94f86992016-12-01 07:12:32 -0800536static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800537 size_t bytesout, void *din, size_t bytesin)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700538{
Arthur Heymans02c99712018-03-28 18:49:27 +0200539 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700540 uint16_t control;
541 int16_t opcode_index;
542 int with_address;
543 int status;
544
545 spi_transaction trans = {
Gabe Black93d9f922014-03-27 21:52:43 -0700546 dout, bytesout,
547 din, bytesin,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700548 0xff, 0xff, 0
549 };
550
551 /* There has to always at least be an opcode. */
Gabe Black93d9f922014-03-27 21:52:43 -0700552 if (!bytesout || !dout) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700553 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
554 return -1;
555 }
556 /* Make sure if we read something we have a place to put it. */
Gabe Black93d9f922014-03-27 21:52:43 -0700557 if (bytesin != 0 && !din) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700558 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
559 return -1;
560 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700561
562 if (ich_status_poll(SPIS_SCIP, 0) == -1)
563 return -1;
564
Arthur Heymans02c99712018-03-28 18:49:27 +0200565 writew_(SPIS_CDS | SPIS_FCERR, cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700566
567 spi_setup_type(&trans);
568 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
569 return -1;
570 if ((with_address = spi_setup_offset(&trans)) < 0)
571 return -1;
572
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700573 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700574 /*
575 * Treat Write Enable as Atomic Pre-Op if possible
576 * in order to prevent the Management Engine from
577 * issuing a transaction between WREN and DATA.
578 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200579 if (!car_get_var(g_ichspi_lock))
580 writew_(trans.opcode, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700581 return 0;
582 }
583
584 /* Preset control fields */
585 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
586
587 /* Issue atomic preop cycle if needed */
Arthur Heymans02c99712018-03-28 18:49:27 +0200588 if (readw_(cntlr->preop))
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700589 control |= SPIC_ACS;
590
591 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700592 /* SPI addresses are 24 bit only */
593 if (with_address)
Arthur Heymans02c99712018-03-28 18:49:27 +0200594 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700595
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700596 /*
597 * This is a 'no data' command (like Write Enable), its
598 * bitesout size was 1, decremented to zero while executing
599 * spi_setup_opcode() above. Tell the chip to send the
600 * command.
601 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200602 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700603
604 /* wait for the result */
605 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
606 if (status == -1)
607 return -1;
608
609 if (status & SPIS_FCERR) {
610 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
611 return -1;
612 }
613
614 return 0;
615 }
616
617 /*
Paul Menzel94782972013-06-29 11:41:27 +0200618 * Check if this is a write command attempting to transfer more bytes
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700619 * than the controller can handle. Iterations for writes are not
620 * supported here because each SPI write command needs to be preceded
621 * and followed by other SPI commands, and this sequence is controlled
622 * by the SPI chip driver.
623 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200624 if (trans.bytesout > cntlr->databytes) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700625 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
Kyösti Mälkki11104952014-06-29 16:17:33 +0300626 " spi_crop_chunk()?\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700627 return -1;
628 }
629
630 /*
631 * Read or write up to databytes bytes at a time until everything has
632 * been sent.
633 */
634 while (trans.bytesout || trans.bytesin) {
635 uint32_t data_length;
636
637 /* SPI addresses are 24 bit only */
Arthur Heymans02c99712018-03-28 18:49:27 +0200638 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700639
640 if (trans.bytesout)
Arthur Heymans02c99712018-03-28 18:49:27 +0200641 data_length = min(trans.bytesout, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700642 else
Arthur Heymans02c99712018-03-28 18:49:27 +0200643 data_length = min(trans.bytesin, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700644
645 /* Program data into FDATA0 to N */
646 if (trans.bytesout) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200647 write_reg(trans.out, cntlr->data, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700648 spi_use_out(&trans, data_length);
649 if (with_address)
650 trans.offset += data_length;
651 }
652
653 /* Add proper control fields' values */
Arthur Heymans02c99712018-03-28 18:49:27 +0200654 control &= ~((cntlr->databytes - 1) << 8);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700655 control |= SPIC_DS;
656 control |= (data_length - 1) << 8;
657
658 /* write it */
Arthur Heymans02c99712018-03-28 18:49:27 +0200659 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700660
661 /* Wait for Cycle Done Status or Flash Cycle Error. */
662 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
663 if (status == -1)
664 return -1;
665
666 if (status & SPIS_FCERR) {
667 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
668 return -1;
669 }
670
671 if (trans.bytesin) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200672 read_reg(cntlr->data, trans.in, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700673 spi_use_in(&trans, data_length);
674 if (with_address)
675 trans.offset += data_length;
676 }
677 }
678
679 /* Clear atomic preop now that xfer is done */
Arthur Heymans02c99712018-03-28 18:49:27 +0200680 writew_(0, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700681
682 return 0;
683}
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100684
685/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
686static void ich_hwseq_set_addr(uint32_t addr)
687{
Arthur Heymans02c99712018-03-28 18:49:27 +0200688 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
689 uint32_t addr_old = readl_(&cntlr->ich9_spi->faddr) & ~0x01FFFFFF;
690 writel_((addr & 0x01FFFFFF) | addr_old, &cntlr->ich9_spi->faddr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100691}
692
693/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
694 Resets all error flags in HSFS.
695 Returns 0 if the cycle completes successfully without errors within
696 timeout us, 1 on errors. */
697static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
698 unsigned int len)
699{
Arthur Heymans02c99712018-03-28 18:49:27 +0200700 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100701 uint16_t hsfs;
702 uint32_t addr;
703
704 timeout /= 8; /* scale timeout duration to counter */
Arthur Heymans02c99712018-03-28 18:49:27 +0200705 while ((((hsfs = readw_(&cntlr->ich9_spi->hsfs)) &
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100706 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
707 --timeout) {
708 udelay(8);
709 }
Arthur Heymans02c99712018-03-28 18:49:27 +0200710 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100711
712 if (!timeout) {
713 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200714 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
715 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100716 printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and "
717 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
718 addr, addr + len - 1, addr, len - 1,
719 hsfc, hsfs);
720 return 1;
721 }
722
723 if (hsfs & HSFS_FCERR) {
724 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200725 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
726 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100727 printk(BIOS_ERR, "Transaction error between offset 0x%08x and "
728 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
729 addr, addr + len - 1, addr, len - 1,
730 hsfc, hsfs);
731 return 1;
732 }
733 return 0;
734}
735
736
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800737static int ich_hwseq_erase(const struct spi_flash *flash, u32 offset,
738 size_t len)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100739{
Arthur Heymans02c99712018-03-28 18:49:27 +0200740 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100741 u32 start, end, erase_size;
742 int ret;
743 uint16_t hsfc;
744 uint16_t timeout = 1000 * 60;
745
746 erase_size = flash->sector_size;
747 if (offset % erase_size || len % erase_size) {
748 printk(BIOS_ERR, "SF: Erase offset/length not multiple of erase size\n");
749 return -1;
750 }
751
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800752 ret = spi_claim_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100753 if (ret) {
754 printk(BIOS_ERR, "SF: Unable to claim SPI bus\n");
755 return ret;
756 }
757
758 start = offset;
759 end = start + len;
760
761 while (offset < end) {
762 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
Arthur Heymans02c99712018-03-28 18:49:27 +0200763 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100764
765 ich_hwseq_set_addr(offset);
766
767 offset += erase_size;
768
Arthur Heymans02c99712018-03-28 18:49:27 +0200769 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100770 hsfc &= ~HSFC_FCYCLE; /* clear operation */
771 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
772 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200773 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100774 if (ich_hwseq_wait_for_cycle_complete(timeout, len))
775 {
776 printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size);
777 ret = -1;
778 goto out;
779 }
780 }
781
782 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
783
784out:
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800785 spi_release_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100786 return ret;
787}
788
789static void ich_read_data(uint8_t *data, int len)
790{
Arthur Heymans02c99712018-03-28 18:49:27 +0200791 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100792 int i;
793 uint32_t temp32 = 0;
794
795 for (i = 0; i < len; i++) {
796 if ((i % 4) == 0)
Arthur Heymans02c99712018-03-28 18:49:27 +0200797 temp32 = readl_(cntlr->data + i);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100798
799 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
800 }
801}
802
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800803static int ich_hwseq_read(const struct spi_flash *flash, u32 addr, size_t len,
804 void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100805{
Arthur Heymans02c99712018-03-28 18:49:27 +0200806 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100807 uint16_t hsfc;
808 uint16_t timeout = 100 * 60;
809 uint8_t block_len;
810
811 if (addr + len > flash->size) {
812 printk (BIOS_ERR,
813 "Attempt to read %x-%x which is out of chip\n",
814 (unsigned) addr,
815 (unsigned) addr+(unsigned) len);
816 return -1;
817 }
818
819 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200820 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100821
822 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200823 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100824 if (block_len > (~addr & 0xff))
825 block_len = (~addr & 0xff) + 1;
826 ich_hwseq_set_addr(addr);
Arthur Heymans02c99712018-03-28 18:49:27 +0200827 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100828 hsfc &= ~HSFC_FCYCLE; /* set read operation */
829 hsfc &= ~HSFC_FDBC; /* clear byte count */
830 /* set byte count */
831 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
832 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200833 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100834
835 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
836 return 1;
837 ich_read_data(buf, block_len);
838 addr += block_len;
839 buf += block_len;
840 len -= block_len;
841 }
842 return 0;
843}
844
845/* Fill len bytes from the data array into the fdata/spid registers.
846 *
847 * Note that using len > flash->pgm->spi.max_data_write will trash the registers
848 * following the data registers.
849 */
850static void ich_fill_data(const uint8_t *data, int len)
851{
Arthur Heymans02c99712018-03-28 18:49:27 +0200852 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100853 uint32_t temp32 = 0;
854 int i;
855
856 if (len <= 0)
857 return;
858
859 for (i = 0; i < len; i++) {
860 if ((i % 4) == 0)
861 temp32 = 0;
862
863 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
864
865 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200866 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100867 }
868 i--;
869 if ((i % 4) != 3) /* Write remaining data to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200870 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100871}
872
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800873static int ich_hwseq_write(const struct spi_flash *flash, u32 addr, size_t len,
874 const void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100875{
Arthur Heymans02c99712018-03-28 18:49:27 +0200876 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100877 uint16_t hsfc;
878 uint16_t timeout = 100 * 60;
879 uint8_t block_len;
880 uint32_t start = addr;
881
882 if (addr + len > flash->size) {
883 printk (BIOS_ERR,
884 "Attempt to write 0x%x-0x%x which is out of chip\n",
885 (unsigned)addr, (unsigned) (addr+len));
886 return -1;
887 }
888
889 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200890 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100891
892 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200893 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100894 if (block_len > (~addr & 0xff))
895 block_len = (~addr & 0xff) + 1;
896
897 ich_hwseq_set_addr(addr);
898
899 ich_fill_data(buf, block_len);
Arthur Heymans02c99712018-03-28 18:49:27 +0200900 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100901 hsfc &= ~HSFC_FCYCLE; /* clear operation */
902 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
903 hsfc &= ~HSFC_FDBC; /* clear byte count */
904 /* set byte count */
905 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
906 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200907 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100908
909 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
910 {
911 printk (BIOS_ERR, "SF: write failure at %x\n",
912 addr);
913 return -1;
914 }
915 addr += block_len;
916 buf += block_len;
917 len -= block_len;
918 }
919 printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n",
920 (unsigned) (addr - start), start);
921 return 0;
922}
923
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700924static const struct spi_flash_ops spi_flash_ops = {
925 .read = ich_hwseq_read,
926 .write = ich_hwseq_write,
927 .erase = ich_hwseq_erase,
928};
929
Furquan Shaikha1491572017-05-17 19:14:06 -0700930static int spi_flash_programmer_probe(const struct spi_slave *spi,
931 struct spi_flash *flash)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100932{
Arthur Heymans02c99712018-03-28 18:49:27 +0200933 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100934 uint32_t flcomp;
935
Arthur Heymansc88e3702017-08-20 20:50:17 +0200936 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX))
937 return spi_flash_generic_probe(spi, flash);
938
Furquan Shaikha1491572017-05-17 19:14:06 -0700939 /* Try generic probing first if spi_is_multichip returns 0. */
940 if (!spi_is_multichip() && !spi_flash_generic_probe(spi, flash))
941 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100942
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800943 memcpy(&flash->spi, spi, sizeof(*spi));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100944 flash->name = "Opaque HW-sequencing";
945
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100946 ich_hwseq_set_addr (0);
Arthur Heymans02c99712018-03-28 18:49:27 +0200947 switch ((cntlr->hsfs >> 3) & 3)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100948 {
949 case 0:
950 flash->sector_size = 256;
951 break;
952 case 1:
953 flash->sector_size = 4096;
954 break;
955 case 2:
956 flash->sector_size = 8192;
957 break;
958 case 3:
959 flash->sector_size = 65536;
960 break;
961 }
962
Arthur Heymans02c99712018-03-28 18:49:27 +0200963 writel_ (0x1000, &cntlr->ich9_spi->fdoc);
964 flcomp = readl_(&cntlr->ich9_spi->fdod);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100965
966 flash->size = 1 << (19 + (flcomp & 7));
967
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700968 flash->ops = &spi_flash_ops;
969
Arthur Heymans02c99712018-03-28 18:49:27 +0200970 if ((cntlr->hsfs & HSFS_FDV) && ((cntlr->flmap0 >> 8) & 3))
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100971 flash->size += 1 << (19 + ((flcomp >> 3) & 7));
972 printk (BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
973
Furquan Shaikh30221b42017-05-15 14:35:15 -0700974 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100975}
Furquan Shaikha1491572017-05-17 19:14:06 -0700976
Aaron Durbin851dde82018-04-19 21:15:25 -0600977static int xfer_vectors(const struct spi_slave *slave,
978 struct spi_op vectors[], size_t count)
979{
980 return spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer);
981}
982
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100983#define SPI_FPR_SHIFT 12
984#define ICH7_SPI_FPR_MASK 0xfff
985#define ICH9_SPI_FPR_MASK 0x1fff
986#define SPI_FPR_BASE_SHIFT 0
987#define ICH7_SPI_FPR_LIMIT_SHIFT 12
988#define ICH9_SPI_FPR_LIMIT_SHIFT 16
989#define ICH9_SPI_FPR_RPE (1 << 15) /* Read Protect */
990#define SPI_FPR_WPE (1 << 31) /* Write Protect */
991
992static u32 spi_fpr(u32 base, u32 limit)
993{
994 u32 ret;
995 u32 mask, limit_shift;
996 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX)) {
997 mask = ICH7_SPI_FPR_MASK;
998 limit_shift = ICH7_SPI_FPR_LIMIT_SHIFT;
999 } else {
1000 mask = ICH9_SPI_FPR_MASK;
1001 limit_shift = ICH9_SPI_FPR_LIMIT_SHIFT;
1002 }
1003 ret = ((limit >> SPI_FPR_SHIFT) & mask) << limit_shift;
1004 ret |= ((base >> SPI_FPR_SHIFT) & mask) << SPI_FPR_BASE_SHIFT;
1005 return ret;
1006}
1007
1008/*
1009 * Protect range of SPI flash defined by [start, start+size-1] using Flash
1010 * Protected Range (FPR) register if available.
1011 * Returns 0 on success, -1 on failure of programming fpr registers.
1012 */
1013static int spi_flash_protect(const struct spi_flash *flash,
1014 const struct region *region)
1015{
Arthur Heymans02c99712018-03-28 18:49:27 +02001016 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001017 u32 start = region_offset(region);
1018 u32 end = start + region_sz(region) - 1;
1019 u32 reg;
1020 int fpr;
1021 uint32_t *fpr_base;
1022
Arthur Heymans02c99712018-03-28 18:49:27 +02001023 fpr_base = cntlr->fpr;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001024
1025 /* Find first empty FPR */
Arthur Heymans02c99712018-03-28 18:49:27 +02001026 for (fpr = 0; fpr < cntlr->fpr_max; fpr++) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001027 reg = read32(&fpr_base[fpr]);
1028 if (reg == 0)
1029 break;
1030 }
1031
Arthur Heymans02c99712018-03-28 18:49:27 +02001032 if (fpr == cntlr->fpr_max) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001033 printk(BIOS_ERR, "ERROR: No SPI FPR free!\n");
1034 return -1;
1035 }
1036
1037 /* Set protected range base and limit */
1038 reg = spi_fpr(start, end) | SPI_FPR_WPE;
1039
1040 /* Set the FPR register and verify it is protected */
1041 write32(&fpr_base[fpr], reg);
1042 reg = read32(&fpr_base[fpr]);
1043 if (!(reg & SPI_FPR_WPE)) {
1044 printk(BIOS_ERR, "ERROR: Unable to set SPI FPR %d\n", fpr);
1045 return -1;
1046 }
1047
1048 printk(BIOS_INFO, "%s: FPR %d is enabled for range 0x%08x-0x%08x\n",
1049 __func__, fpr, start, end);
1050 return 0;
1051}
1052
Furquan Shaikha1491572017-05-17 19:14:06 -07001053static const struct spi_ctrlr spi_ctrlr = {
Aaron Durbin851dde82018-04-19 21:15:25 -06001054 .xfer_vector = xfer_vectors,
Furquan Shaikha1491572017-05-17 19:14:06 -07001055 .max_xfer_size = member_size(ich9_spi_regs, fdata),
1056 .flash_probe = spi_flash_programmer_probe,
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001057 .flash_protect = spi_flash_protect,
Furquan Shaikha1491572017-05-17 19:14:06 -07001058};
1059
Furquan Shaikh2cd03f12017-05-18 14:58:32 -07001060const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
1061 {
1062 .ctrlr = &spi_ctrlr,
1063 .bus_start = 0,
1064 .bus_end = 0,
1065 },
1066};
1067
1068const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);