blob: 7e0cc201c71ac2efee8214858811c1dc5be9556a [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. */
18#include <stdint.h>
19#include <stdlib.h>
20#include <string.h>
David Hendricksf2612a12014-04-13 16:27:02 -070021#include <bootstate.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070022#include <delay.h>
23#include <arch/io.h>
24#include <console/console.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070025#include <device/pci_ids.h>
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010026#include <device/pci.h>
27#include <spi_flash.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070028
Zheng Bao600784e2013-02-07 17:30:23 +080029#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070030
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010031#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
32#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
33#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
34#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
35
36
Duncan Laurie181bbdd2012-06-23 16:53:57 -070037#ifdef __SMM__
Kyösti Mälkki54d6abd2013-06-19 23:05:00 +030038#include <arch/pci_mmio_cfg.h>
Duncan Laurie181bbdd2012-06-23 16:53:57 -070039#define pci_read_config_byte(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030040 *(targ) = pci_read_config8(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070041#define pci_read_config_word(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030042 *(targ) = pci_read_config16(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070043#define pci_read_config_dword(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030044 *(targ) = pci_read_config32(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070045#define pci_write_config_byte(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030046 pci_write_config8(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070047#define pci_write_config_word(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030048 pci_write_config16(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070049#define pci_write_config_dword(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030050 pci_write_config32(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070051#else /* !__SMM__ */
52#include <device/device.h>
53#include <device/pci.h>
Duncan Laurie181bbdd2012-06-23 16:53:57 -070054#define pci_read_config_byte(dev, reg, targ)\
55 *(targ) = pci_read_config8(dev, reg)
56#define pci_read_config_word(dev, reg, targ)\
57 *(targ) = pci_read_config16(dev, reg)
58#define pci_read_config_dword(dev, reg, targ)\
59 *(targ) = pci_read_config32(dev, reg)
60#define pci_write_config_byte(dev, reg, val)\
61 pci_write_config8(dev, reg, val)
62#define pci_write_config_word(dev, reg, val)\
63 pci_write_config16(dev, reg, val)
64#define pci_write_config_dword(dev, reg, val)\
65 pci_write_config32(dev, reg, val)
66#endif /* !__SMM__ */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070067
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010068static int spi_is_multichip(void);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010069
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070070typedef struct spi_slave ich_spi_slave;
71
72static int ichspi_lock = 0;
73
74typedef struct ich7_spi_regs {
75 uint16_t spis;
76 uint16_t spic;
77 uint32_t spia;
78 uint64_t spid[8];
79 uint64_t _pad;
80 uint32_t bbar;
81 uint16_t preop;
82 uint16_t optype;
83 uint8_t opmenu[8];
84} __attribute__((packed)) ich7_spi_regs;
85
86typedef struct ich9_spi_regs {
87 uint32_t bfpr;
88 uint16_t hsfs;
89 uint16_t hsfc;
90 uint32_t faddr;
91 uint32_t _reserved0;
92 uint32_t fdata[16];
93 uint32_t frap;
94 uint32_t freg[5];
95 uint32_t _reserved1[3];
96 uint32_t pr[5];
97 uint32_t _reserved2[2];
98 uint8_t ssfs;
99 uint8_t ssfc[3];
100 uint16_t preop;
101 uint16_t optype;
102 uint8_t opmenu[8];
103 uint32_t bbar;
104 uint8_t _reserved3[12];
105 uint32_t fdoc;
106 uint32_t fdod;
107 uint8_t _reserved4[8];
108 uint32_t afc;
109 uint32_t lvscc;
110 uint32_t uvscc;
111 uint8_t _reserved5[4];
112 uint32_t fpb;
113 uint8_t _reserved6[28];
114 uint32_t srdl;
115 uint32_t srdc;
116 uint32_t srd;
117} __attribute__((packed)) ich9_spi_regs;
118
119typedef struct ich_spi_controller {
120 int locked;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100121 uint32_t flmap0;
122 uint32_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700123
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100124 ich9_spi_regs *ich9_spi;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700125 uint8_t *opmenu;
126 int menubytes;
127 uint16_t *preop;
128 uint16_t *optype;
129 uint32_t *addr;
130 uint8_t *data;
131 unsigned databytes;
132 uint8_t *status;
133 uint16_t *control;
134 uint32_t *bbar;
135} ich_spi_controller;
136
137static ich_spi_controller cntlr;
138
139enum {
140 SPIS_SCIP = 0x0001,
141 SPIS_GRANT = 0x0002,
142 SPIS_CDS = 0x0004,
143 SPIS_FCERR = 0x0008,
144 SSFS_AEL = 0x0010,
145 SPIS_LOCK = 0x8000,
146 SPIS_RESERVED_MASK = 0x7ff0,
147 SSFS_RESERVED_MASK = 0x7fe2
148};
149
150enum {
151 SPIC_SCGO = 0x000002,
152 SPIC_ACS = 0x000004,
153 SPIC_SPOP = 0x000008,
154 SPIC_DBC = 0x003f00,
155 SPIC_DS = 0x004000,
156 SPIC_SME = 0x008000,
157 SSFC_SCF_MASK = 0x070000,
158 SSFC_RESERVED = 0xf80000
159};
160
161enum {
162 HSFS_FDONE = 0x0001,
163 HSFS_FCERR = 0x0002,
164 HSFS_AEL = 0x0004,
165 HSFS_BERASE_MASK = 0x0018,
166 HSFS_BERASE_SHIFT = 3,
167 HSFS_SCIP = 0x0020,
168 HSFS_FDOPSS = 0x2000,
169 HSFS_FDV = 0x4000,
170 HSFS_FLOCKDN = 0x8000
171};
172
173enum {
174 HSFC_FGO = 0x0001,
175 HSFC_FCYCLE_MASK = 0x0006,
176 HSFC_FCYCLE_SHIFT = 1,
177 HSFC_FDBC_MASK = 0x3f00,
178 HSFC_FDBC_SHIFT = 8,
179 HSFC_FSMIE = 0x8000
180};
181
182enum {
183 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
184 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
185 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
186 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
187};
188
189#if CONFIG_DEBUG_SPI_FLASH
190
191static u8 readb_(const void *addr)
192{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800193 u8 v = read8(addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700194 printk(BIOS_DEBUG, "read %2.2x from %4.4x\n",
195 v, ((unsigned) addr & 0xffff) - 0xf020);
196 return v;
197}
198
199static u16 readw_(const void *addr)
200{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800201 u16 v = read16(addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700202 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
203 v, ((unsigned) addr & 0xffff) - 0xf020);
204 return v;
205}
206
207static u32 readl_(const void *addr)
208{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800209 u32 v = read32(addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700210 printk(BIOS_DEBUG, "read %8.8x from %4.4x\n",
211 v, ((unsigned) addr & 0xffff) - 0xf020);
212 return v;
213}
214
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800215static void writeb_(u8 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700216{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800217 write8(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700218 printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n",
219 b, ((unsigned) addr & 0xffff) - 0xf020);
220}
221
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800222static void writew_(u16 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700223{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800224 write16(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700225 printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n",
226 b, ((unsigned) addr & 0xffff) - 0xf020);
227}
228
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800229static void writel_(u32 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700230{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800231 write32(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700232 printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n",
233 b, ((unsigned) addr & 0xffff) - 0xf020);
234}
235
236#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
237
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800238#define readb_(a) read8(a)
239#define readw_(a) read16(a)
240#define readl_(a) read32(a)
241#define writeb_(val, addr) write8(addr, val)
242#define writew_(val, addr) write16(addr, val)
243#define writel_(val, addr) write32(addr, val)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700244
245#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
246
247static void write_reg(const void *value, void *dest, uint32_t size)
248{
249 const uint8_t *bvalue = value;
250 uint8_t *bdest = dest;
251
252 while (size >= 4) {
253 writel_(*(const uint32_t *)bvalue, bdest);
254 bdest += 4; bvalue += 4; size -= 4;
255 }
256 while (size) {
257 writeb_(*bvalue, bdest);
258 bdest++; bvalue++; size--;
259 }
260}
261
262static void read_reg(const void *src, void *value, uint32_t size)
263{
264 const uint8_t *bsrc = src;
265 uint8_t *bvalue = value;
266
267 while (size >= 4) {
268 *(uint32_t *)bvalue = readl_(bsrc);
269 bsrc += 4; bvalue += 4; size -= 4;
270 }
271 while (size) {
272 *bvalue = readb_(bsrc);
273 bsrc++; bvalue++; size--;
274 }
275}
276
277static void ich_set_bbar(uint32_t minaddr)
278{
279 const uint32_t bbar_mask = 0x00ffff00;
280 uint32_t ichspi_bbar;
281
282 minaddr &= bbar_mask;
283 ichspi_bbar = readl_(cntlr.bbar) & ~bbar_mask;
284 ichspi_bbar |= minaddr;
285 writel_(ichspi_bbar, cntlr.bbar);
286}
287
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800288int spi_setup_slave(unsigned int bus, unsigned int cs, struct spi_slave *slave)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700289{
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700290 slave->bus = bus;
291 slave->cs = cs;
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800292 return 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700293}
294
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700295void spi_init(void)
296{
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700297 uint8_t *rcrb; /* Root Complex Register Block */
298 uint32_t rcba; /* Root Complex Base Address */
299 uint8_t bios_cntl;
Stefan Reinauer0c32c972012-07-10 13:26:59 -0700300 device_t dev;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100301 ich9_spi_regs *ich9_spi;
302 uint16_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700303
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700304#ifdef __SMM__
305 dev = PCI_DEV(0, 31, 0);
306#else
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700307 dev = dev_find_slot(0, PCI_DEVFN(31, 0));
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700308#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700309
310 pci_read_config_dword(dev, 0xf0, &rcba);
311 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
312 rcrb = (uint8_t *)(rcba & 0xffffc000);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100313 ich9_spi = (ich9_spi_regs *)(rcrb + 0x3800);
314 cntlr.ich9_spi = ich9_spi;
315 hsfs = readw_(&ich9_spi->hsfs);
316 ichspi_lock = hsfs & HSFS_FLOCKDN;
317 cntlr.hsfs = hsfs;
318 cntlr.opmenu = ich9_spi->opmenu;
319 cntlr.menubytes = sizeof(ich9_spi->opmenu);
320 cntlr.optype = &ich9_spi->optype;
321 cntlr.addr = &ich9_spi->faddr;
322 cntlr.data = (uint8_t *)ich9_spi->fdata;
323 cntlr.databytes = sizeof(ich9_spi->fdata);
324 cntlr.status = &ich9_spi->ssfs;
325 cntlr.control = (uint16_t *)ich9_spi->ssfc;
326 cntlr.bbar = &ich9_spi->bbar;
327 cntlr.preop = &ich9_spi->preop;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700328
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100329 if (cntlr.hsfs & HSFS_FDV)
330 {
331 writel_ (4, &ich9_spi->fdoc);
332 cntlr.flmap0 = readl_(&ich9_spi->fdod);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700333 }
334
335 ich_set_bbar(0);
336
337 /* Disable the BIOS write protect so write commands are allowed. */
338 pci_read_config_byte(dev, 0xdc, &bios_cntl);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100339 /* Deassert SMM BIOS Write Protect Disable. */
340 bios_cntl &= ~(1 << 5);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700341 pci_write_config_byte(dev, 0xdc, bios_cntl | 0x1);
342}
Aaron Durbin4d3de7e2015-09-02 17:34:04 -0500343
David Hendricksf2612a12014-04-13 16:27:02 -0700344static void spi_init_cb(void *unused)
345{
346 spi_init();
347}
348
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500349BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700350
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800351int spi_claim_bus(const struct spi_slave *slave)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700352{
353 /* Handled by ICH automatically. */
354 return 0;
355}
356
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800357void spi_release_bus(const struct spi_slave *slave)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700358{
359 /* Handled by ICH automatically. */
360}
361
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700362typedef struct spi_transaction {
363 const uint8_t *out;
364 uint32_t bytesout;
365 uint8_t *in;
366 uint32_t bytesin;
367 uint8_t type;
368 uint8_t opcode;
369 uint32_t offset;
370} spi_transaction;
371
372static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
373{
374 trans->out += bytes;
375 trans->bytesout -= bytes;
376}
377
378static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
379{
380 trans->in += bytes;
381 trans->bytesin -= bytes;
382}
383
384static void spi_setup_type(spi_transaction *trans)
385{
386 trans->type = 0xFF;
387
388 /* Try to guess spi type from read/write sizes. */
389 if (trans->bytesin == 0) {
390 if (trans->bytesout > 4)
391 /*
392 * If bytesin = 0 and bytesout > 4, we presume this is
393 * a write data operation, which is accompanied by an
394 * address.
395 */
396 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
397 else
398 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
399 return;
400 }
401
402 if (trans->bytesout == 1) { /* and bytesin is > 0 */
403 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
404 return;
405 }
406
407 if (trans->bytesout == 4) { /* and bytesin is > 0 */
408 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
409 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700410
411 /* Fast read command is called with 5 bytes instead of 4 */
412 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
413 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
414 --trans->bytesout;
415 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700416}
417
418static int spi_setup_opcode(spi_transaction *trans)
419{
420 uint16_t optypes;
421 uint8_t opmenu[cntlr.menubytes];
422
423 trans->opcode = trans->out[0];
424 spi_use_out(trans, 1);
425 if (!ichspi_lock) {
426 /* The lock is off, so just use index 0. */
427 writeb_(trans->opcode, cntlr.opmenu);
428 optypes = readw_(cntlr.optype);
429 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
430 writew_(optypes, cntlr.optype);
431 return 0;
432 } else {
433 /* The lock is on. See if what we need is on the menu. */
434 uint8_t optype;
435 uint16_t opcode_index;
436
Duncan Lauriea2f1b952012-08-27 11:10:43 -0700437 /* Write Enable is handled as atomic prefix */
438 if (trans->opcode == SPI_OPCODE_WREN)
439 return 0;
440
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700441 read_reg(cntlr.opmenu, opmenu, sizeof(opmenu));
442 for (opcode_index = 0; opcode_index < cntlr.menubytes;
443 opcode_index++) {
444 if (opmenu[opcode_index] == trans->opcode)
445 break;
446 }
447
448 if (opcode_index == cntlr.menubytes) {
449 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
450 trans->opcode);
451 return -1;
452 }
453
454 optypes = readw_(cntlr.optype);
455 optype = (optypes >> (opcode_index * 2)) & 0x3;
456 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
457 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
458 trans->bytesout >= 3) {
459 /* We guessed wrong earlier. Fix it up. */
460 trans->type = optype;
461 }
462 if (optype != trans->type) {
463 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
464 optype);
465 return -1;
466 }
467 return opcode_index;
468 }
469}
470
471static int spi_setup_offset(spi_transaction *trans)
472{
473 /* Separate the SPI address and data. */
474 switch (trans->type) {
475 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
476 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
477 return 0;
478 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
479 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
480 trans->offset = ((uint32_t)trans->out[0] << 16) |
481 ((uint32_t)trans->out[1] << 8) |
482 ((uint32_t)trans->out[2] << 0);
483 spi_use_out(trans, 3);
484 return 1;
485 default:
486 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
487 return -1;
488 }
489}
490
491/*
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200492 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700493 * below is True) or 0. In case the wait was for the bit(s) to set - write
494 * those bits back, which would cause resetting them.
495 *
496 * Return the last read status value on success or -1 on failure.
497 */
498static int ich_status_poll(u16 bitmask, int wait_til_set)
499{
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200500 int timeout = 600000; /* This will result in 6 seconds */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700501 u16 status = 0;
502
503 while (timeout--) {
504 status = readw_(cntlr.status);
505 if (wait_til_set ^ ((status & bitmask) == 0)) {
506 if (wait_til_set)
507 writew_((status & bitmask), cntlr.status);
508 return status;
509 }
510 udelay(10);
511 }
512
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200513 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700514 status, bitmask);
515 return -1;
516}
517
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100518static int spi_is_multichip (void)
519{
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100520 if (!(cntlr.hsfs & HSFS_FDV))
521 return 0;
522 return !!((cntlr.flmap0 >> 8) & 3);
523}
524
Kyösti Mälkki11104952014-06-29 16:17:33 +0300525unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len)
526{
527 return min(cntlr.databytes, buf_len);
528}
529
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800530int spi_xfer(const struct spi_slave *slave, const void *dout,
531 size_t bytesout, void *din, size_t bytesin)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700532{
533 uint16_t control;
534 int16_t opcode_index;
535 int with_address;
536 int status;
537
538 spi_transaction trans = {
Gabe Black93d9f922014-03-27 21:52:43 -0700539 dout, bytesout,
540 din, bytesin,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700541 0xff, 0xff, 0
542 };
543
544 /* There has to always at least be an opcode. */
Gabe Black93d9f922014-03-27 21:52:43 -0700545 if (!bytesout || !dout) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700546 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
547 return -1;
548 }
549 /* Make sure if we read something we have a place to put it. */
Gabe Black93d9f922014-03-27 21:52:43 -0700550 if (bytesin != 0 && !din) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700551 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
552 return -1;
553 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700554
555 if (ich_status_poll(SPIS_SCIP, 0) == -1)
556 return -1;
557
558 writew_(SPIS_CDS | SPIS_FCERR, cntlr.status);
559
560 spi_setup_type(&trans);
561 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
562 return -1;
563 if ((with_address = spi_setup_offset(&trans)) < 0)
564 return -1;
565
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700566 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700567 /*
568 * Treat Write Enable as Atomic Pre-Op if possible
569 * in order to prevent the Management Engine from
570 * issuing a transaction between WREN and DATA.
571 */
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700572 if (!ichspi_lock)
573 writew_(trans.opcode, cntlr.preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700574 return 0;
575 }
576
577 /* Preset control fields */
578 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
579
580 /* Issue atomic preop cycle if needed */
581 if (readw_(cntlr.preop))
582 control |= SPIC_ACS;
583
584 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700585 /* SPI addresses are 24 bit only */
586 if (with_address)
587 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
588
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700589 /*
590 * This is a 'no data' command (like Write Enable), its
591 * bitesout size was 1, decremented to zero while executing
592 * spi_setup_opcode() above. Tell the chip to send the
593 * command.
594 */
595 writew_(control, cntlr.control);
596
597 /* wait for the result */
598 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
599 if (status == -1)
600 return -1;
601
602 if (status & SPIS_FCERR) {
603 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
604 return -1;
605 }
606
607 return 0;
608 }
609
610 /*
Paul Menzel94782972013-06-29 11:41:27 +0200611 * Check if this is a write command attempting to transfer more bytes
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700612 * than the controller can handle. Iterations for writes are not
613 * supported here because each SPI write command needs to be preceded
614 * and followed by other SPI commands, and this sequence is controlled
615 * by the SPI chip driver.
616 */
617 if (trans.bytesout > cntlr.databytes) {
618 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
Kyösti Mälkki11104952014-06-29 16:17:33 +0300619 " spi_crop_chunk()?\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700620 return -1;
621 }
622
623 /*
624 * Read or write up to databytes bytes at a time until everything has
625 * been sent.
626 */
627 while (trans.bytesout || trans.bytesin) {
628 uint32_t data_length;
629
630 /* SPI addresses are 24 bit only */
631 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
632
633 if (trans.bytesout)
634 data_length = min(trans.bytesout, cntlr.databytes);
635 else
636 data_length = min(trans.bytesin, cntlr.databytes);
637
638 /* Program data into FDATA0 to N */
639 if (trans.bytesout) {
640 write_reg(trans.out, cntlr.data, data_length);
641 spi_use_out(&trans, data_length);
642 if (with_address)
643 trans.offset += data_length;
644 }
645
646 /* Add proper control fields' values */
647 control &= ~((cntlr.databytes - 1) << 8);
648 control |= SPIC_DS;
649 control |= (data_length - 1) << 8;
650
651 /* write it */
652 writew_(control, cntlr.control);
653
654 /* Wait for Cycle Done Status or Flash Cycle Error. */
655 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
656 if (status == -1)
657 return -1;
658
659 if (status & SPIS_FCERR) {
660 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
661 return -1;
662 }
663
664 if (trans.bytesin) {
665 read_reg(cntlr.data, trans.in, data_length);
666 spi_use_in(&trans, data_length);
667 if (with_address)
668 trans.offset += data_length;
669 }
670 }
671
672 /* Clear atomic preop now that xfer is done */
673 writew_(0, cntlr.preop);
674
675 return 0;
676}
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100677
678/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
679static void ich_hwseq_set_addr(uint32_t addr)
680{
681 uint32_t addr_old = readl_(&cntlr.ich9_spi->faddr) & ~0x01FFFFFF;
682 writel_((addr & 0x01FFFFFF) | addr_old, &cntlr.ich9_spi->faddr);
683}
684
685/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
686 Resets all error flags in HSFS.
687 Returns 0 if the cycle completes successfully without errors within
688 timeout us, 1 on errors. */
689static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
690 unsigned int len)
691{
692 uint16_t hsfs;
693 uint32_t addr;
694
695 timeout /= 8; /* scale timeout duration to counter */
696 while ((((hsfs = readw_(&cntlr.ich9_spi->hsfs)) &
697 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
698 --timeout) {
699 udelay(8);
700 }
701 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
702
703 if (!timeout) {
704 uint16_t hsfc;
705 addr = readl_(&cntlr.ich9_spi->faddr) & 0x01FFFFFF;
706 hsfc = readw_(&cntlr.ich9_spi->hsfc);
707 printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and "
708 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
709 addr, addr + len - 1, addr, len - 1,
710 hsfc, hsfs);
711 return 1;
712 }
713
714 if (hsfs & HSFS_FCERR) {
715 uint16_t hsfc;
716 addr = readl_(&cntlr.ich9_spi->faddr) & 0x01FFFFFF;
717 hsfc = readw_(&cntlr.ich9_spi->hsfc);
718 printk(BIOS_ERR, "Transaction error between offset 0x%08x and "
719 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
720 addr, addr + len - 1, addr, len - 1,
721 hsfc, hsfs);
722 return 1;
723 }
724 return 0;
725}
726
727
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800728static int ich_hwseq_erase(const struct spi_flash *flash, u32 offset,
729 size_t len)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100730{
731 u32 start, end, erase_size;
732 int ret;
733 uint16_t hsfc;
734 uint16_t timeout = 1000 * 60;
735
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
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100742 ret = spi_claim_bus(flash->spi);
743 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 */
753 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
754
755 ich_hwseq_set_addr(offset);
756
757 offset += erase_size;
758
759 hsfc = readw_(&cntlr.ich9_spi->hsfc);
760 hsfc &= ~HSFC_FCYCLE; /* clear operation */
761 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
762 hsfc |= HSFC_FGO; /* start */
763 writew_(hsfc, &cntlr.ich9_spi->hsfc);
764 if (ich_hwseq_wait_for_cycle_complete(timeout, len))
765 {
766 printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size);
767 ret = -1;
768 goto out;
769 }
770 }
771
772 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
773
774out:
775 spi_release_bus(flash->spi);
776 return ret;
777}
778
779static void ich_read_data(uint8_t *data, int len)
780{
781 int i;
782 uint32_t temp32 = 0;
783
784 for (i = 0; i < len; i++) {
785 if ((i % 4) == 0)
786 temp32 = readl_(cntlr.data + i);
787
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{
795 uint16_t hsfc;
796 uint16_t timeout = 100 * 60;
797 uint8_t block_len;
798
799 if (addr + len > flash->size) {
800 printk (BIOS_ERR,
801 "Attempt to read %x-%x which is out of chip\n",
802 (unsigned) addr,
803 (unsigned) addr+(unsigned) len);
804 return -1;
805 }
806
807 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
808 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
809
810 while (len > 0) {
811 block_len = min(len, cntlr.databytes);
812 if (block_len > (~addr & 0xff))
813 block_len = (~addr & 0xff) + 1;
814 ich_hwseq_set_addr(addr);
815 hsfc = readw_(&cntlr.ich9_spi->hsfc);
816 hsfc &= ~HSFC_FCYCLE; /* set read operation */
817 hsfc &= ~HSFC_FDBC; /* clear byte count */
818 /* set byte count */
819 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
820 hsfc |= HSFC_FGO; /* start */
821 writew_(hsfc, &cntlr.ich9_spi->hsfc);
822
823 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
824 return 1;
825 ich_read_data(buf, block_len);
826 addr += block_len;
827 buf += block_len;
828 len -= block_len;
829 }
830 return 0;
831}
832
833/* Fill len bytes from the data array into the fdata/spid registers.
834 *
835 * Note that using len > flash->pgm->spi.max_data_write will trash the registers
836 * following the data registers.
837 */
838static void ich_fill_data(const uint8_t *data, int len)
839{
840 uint32_t temp32 = 0;
841 int i;
842
843 if (len <= 0)
844 return;
845
846 for (i = 0; i < len; i++) {
847 if ((i % 4) == 0)
848 temp32 = 0;
849
850 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
851
852 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
853 writel_(temp32, cntlr.data + (i - (i % 4)));
854 }
855 i--;
856 if ((i % 4) != 3) /* Write remaining data to regs. */
857 writel_(temp32, cntlr.data + (i - (i % 4)));
858}
859
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800860static int ich_hwseq_write(const struct spi_flash *flash, u32 addr, size_t len,
861 const void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100862{
863 uint16_t hsfc;
864 uint16_t timeout = 100 * 60;
865 uint8_t block_len;
866 uint32_t start = addr;
867
868 if (addr + len > flash->size) {
869 printk (BIOS_ERR,
870 "Attempt to write 0x%x-0x%x which is out of chip\n",
871 (unsigned)addr, (unsigned) (addr+len));
872 return -1;
873 }
874
875 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
876 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
877
878 while (len > 0) {
879 block_len = min(len, cntlr.databytes);
880 if (block_len > (~addr & 0xff))
881 block_len = (~addr & 0xff) + 1;
882
883 ich_hwseq_set_addr(addr);
884
885 ich_fill_data(buf, block_len);
886 hsfc = readw_(&cntlr.ich9_spi->hsfc);
887 hsfc &= ~HSFC_FCYCLE; /* clear operation */
888 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
889 hsfc &= ~HSFC_FDBC; /* clear byte count */
890 /* set byte count */
891 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
892 hsfc |= HSFC_FGO; /* start */
893 writew_(hsfc, &cntlr.ich9_spi->hsfc);
894
895 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
896 {
897 printk (BIOS_ERR, "SF: write failure at %x\n",
898 addr);
899 return -1;
900 }
901 addr += block_len;
902 buf += block_len;
903 len -= block_len;
904 }
905 printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n",
906 (unsigned) (addr - start), start);
907 return 0;
908}
909
910
Furquan Shaikhd2fb6ae2016-11-17 20:38:07 -0800911struct spi_flash *spi_flash_programmer_probe(struct spi_slave *spi, int force)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100912{
913 struct spi_flash *flash = NULL;
914 uint32_t flcomp;
915
Furquan Shaikhd2fb6ae2016-11-17 20:38:07 -0800916 /*
917 * Perform SPI flash probing only if:
918 * 1. spi_is_multichip returns 1 or
919 * 2. Specialized probing is forced by SPI flash driver.
920 */
921 if (!spi_is_multichip() && !force)
922 return NULL;
923
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100924 flash = malloc(sizeof(*flash));
925 if (!flash) {
926 printk(BIOS_WARNING, "SF: Failed to allocate memory\n");
927 return NULL;
928 }
929
930 flash->spi = spi;
931 flash->name = "Opaque HW-sequencing";
932
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800933 flash->internal_write = ich_hwseq_write;
934 flash->internal_erase = ich_hwseq_erase;
935 flash->internal_read = ich_hwseq_read;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100936 ich_hwseq_set_addr (0);
937 switch ((cntlr.hsfs >> 3) & 3)
938 {
939 case 0:
940 flash->sector_size = 256;
941 break;
942 case 1:
943 flash->sector_size = 4096;
944 break;
945 case 2:
946 flash->sector_size = 8192;
947 break;
948 case 3:
949 flash->sector_size = 65536;
950 break;
951 }
952
953 writel_ (0x1000, &cntlr.ich9_spi->fdoc);
954 flcomp = readl_(&cntlr.ich9_spi->fdod);
955
956 flash->size = 1 << (19 + (flcomp & 7));
957
958 if ((cntlr.hsfs & HSFS_FDV) && ((cntlr.flmap0 >> 8) & 3))
959 flash->size += 1 << (19 + ((flcomp >> 3) & 7));
960 printk (BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
961
962 return flash;
963}