blob: e0414220c6edead59d31763cb331b12558fdb045 [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);
69static struct spi_flash *spi_flash_hwseq(struct spi_slave *spi);
70
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070071typedef struct spi_slave ich_spi_slave;
72
73static int ichspi_lock = 0;
74
75typedef struct ich7_spi_regs {
76 uint16_t spis;
77 uint16_t spic;
78 uint32_t spia;
79 uint64_t spid[8];
80 uint64_t _pad;
81 uint32_t bbar;
82 uint16_t preop;
83 uint16_t optype;
84 uint8_t opmenu[8];
85} __attribute__((packed)) ich7_spi_regs;
86
87typedef struct ich9_spi_regs {
88 uint32_t bfpr;
89 uint16_t hsfs;
90 uint16_t hsfc;
91 uint32_t faddr;
92 uint32_t _reserved0;
93 uint32_t fdata[16];
94 uint32_t frap;
95 uint32_t freg[5];
96 uint32_t _reserved1[3];
97 uint32_t pr[5];
98 uint32_t _reserved2[2];
99 uint8_t ssfs;
100 uint8_t ssfc[3];
101 uint16_t preop;
102 uint16_t optype;
103 uint8_t opmenu[8];
104 uint32_t bbar;
105 uint8_t _reserved3[12];
106 uint32_t fdoc;
107 uint32_t fdod;
108 uint8_t _reserved4[8];
109 uint32_t afc;
110 uint32_t lvscc;
111 uint32_t uvscc;
112 uint8_t _reserved5[4];
113 uint32_t fpb;
114 uint8_t _reserved6[28];
115 uint32_t srdl;
116 uint32_t srdc;
117 uint32_t srd;
118} __attribute__((packed)) ich9_spi_regs;
119
120typedef struct ich_spi_controller {
121 int locked;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100122 uint32_t flmap0;
123 uint32_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700124
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100125 ich9_spi_regs *ich9_spi;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700126 uint8_t *opmenu;
127 int menubytes;
128 uint16_t *preop;
129 uint16_t *optype;
130 uint32_t *addr;
131 uint8_t *data;
132 unsigned databytes;
133 uint8_t *status;
134 uint16_t *control;
135 uint32_t *bbar;
136} ich_spi_controller;
137
138static ich_spi_controller cntlr;
139
140enum {
141 SPIS_SCIP = 0x0001,
142 SPIS_GRANT = 0x0002,
143 SPIS_CDS = 0x0004,
144 SPIS_FCERR = 0x0008,
145 SSFS_AEL = 0x0010,
146 SPIS_LOCK = 0x8000,
147 SPIS_RESERVED_MASK = 0x7ff0,
148 SSFS_RESERVED_MASK = 0x7fe2
149};
150
151enum {
152 SPIC_SCGO = 0x000002,
153 SPIC_ACS = 0x000004,
154 SPIC_SPOP = 0x000008,
155 SPIC_DBC = 0x003f00,
156 SPIC_DS = 0x004000,
157 SPIC_SME = 0x008000,
158 SSFC_SCF_MASK = 0x070000,
159 SSFC_RESERVED = 0xf80000
160};
161
162enum {
163 HSFS_FDONE = 0x0001,
164 HSFS_FCERR = 0x0002,
165 HSFS_AEL = 0x0004,
166 HSFS_BERASE_MASK = 0x0018,
167 HSFS_BERASE_SHIFT = 3,
168 HSFS_SCIP = 0x0020,
169 HSFS_FDOPSS = 0x2000,
170 HSFS_FDV = 0x4000,
171 HSFS_FLOCKDN = 0x8000
172};
173
174enum {
175 HSFC_FGO = 0x0001,
176 HSFC_FCYCLE_MASK = 0x0006,
177 HSFC_FCYCLE_SHIFT = 1,
178 HSFC_FDBC_MASK = 0x3f00,
179 HSFC_FDBC_SHIFT = 8,
180 HSFC_FSMIE = 0x8000
181};
182
183enum {
184 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
185 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
186 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
187 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
188};
189
190#if CONFIG_DEBUG_SPI_FLASH
191
192static u8 readb_(const void *addr)
193{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800194 u8 v = read8(addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700195 printk(BIOS_DEBUG, "read %2.2x from %4.4x\n",
196 v, ((unsigned) addr & 0xffff) - 0xf020);
197 return v;
198}
199
200static u16 readw_(const void *addr)
201{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800202 u16 v = read16(addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700203 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
204 v, ((unsigned) addr & 0xffff) - 0xf020);
205 return v;
206}
207
208static u32 readl_(const void *addr)
209{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800210 u32 v = read32(addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700211 printk(BIOS_DEBUG, "read %8.8x from %4.4x\n",
212 v, ((unsigned) addr & 0xffff) - 0xf020);
213 return v;
214}
215
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800216static void writeb_(u8 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700217{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800218 write8(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700219 printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n",
220 b, ((unsigned) addr & 0xffff) - 0xf020);
221}
222
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800223static void writew_(u16 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700224{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800225 write16(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700226 printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n",
227 b, ((unsigned) addr & 0xffff) - 0xf020);
228}
229
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800230static void writel_(u32 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700231{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800232 write32(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700233 printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n",
234 b, ((unsigned) addr & 0xffff) - 0xf020);
235}
236
237#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
238
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800239#define readb_(a) read8(a)
240#define readw_(a) read16(a)
241#define readl_(a) read32(a)
242#define writeb_(val, addr) write8(addr, val)
243#define writew_(val, addr) write16(addr, val)
244#define writel_(val, addr) write32(addr, val)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700245
246#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
247
248static void write_reg(const void *value, void *dest, uint32_t size)
249{
250 const uint8_t *bvalue = value;
251 uint8_t *bdest = dest;
252
253 while (size >= 4) {
254 writel_(*(const uint32_t *)bvalue, bdest);
255 bdest += 4; bvalue += 4; size -= 4;
256 }
257 while (size) {
258 writeb_(*bvalue, bdest);
259 bdest++; bvalue++; size--;
260 }
261}
262
263static void read_reg(const void *src, void *value, uint32_t size)
264{
265 const uint8_t *bsrc = src;
266 uint8_t *bvalue = value;
267
268 while (size >= 4) {
269 *(uint32_t *)bvalue = readl_(bsrc);
270 bsrc += 4; bvalue += 4; size -= 4;
271 }
272 while (size) {
273 *bvalue = readb_(bsrc);
274 bsrc++; bvalue++; size--;
275 }
276}
277
278static void ich_set_bbar(uint32_t minaddr)
279{
280 const uint32_t bbar_mask = 0x00ffff00;
281 uint32_t ichspi_bbar;
282
283 minaddr &= bbar_mask;
284 ichspi_bbar = readl_(cntlr.bbar) & ~bbar_mask;
285 ichspi_bbar |= minaddr;
286 writel_(ichspi_bbar, cntlr.bbar);
287}
288
Gabe Black1e187352014-03-27 20:37:03 -0700289struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700290{
291 ich_spi_slave *slave = malloc(sizeof(*slave));
292
293 if (!slave) {
294 printk(BIOS_DEBUG, "ICH SPI: Bad allocation\n");
295 return NULL;
296 }
297
298 memset(slave, 0, sizeof(*slave));
299
300 slave->bus = bus;
301 slave->cs = cs;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100302 slave->force_programmer_specific = spi_is_multichip ();
303 slave->programmer_specific_probe = spi_flash_hwseq;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700304 return slave;
305}
306
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700307void spi_init(void)
308{
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700309 uint8_t *rcrb; /* Root Complex Register Block */
310 uint32_t rcba; /* Root Complex Base Address */
311 uint8_t bios_cntl;
Stefan Reinauer0c32c972012-07-10 13:26:59 -0700312 device_t dev;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100313 ich9_spi_regs *ich9_spi;
314 uint16_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700315
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700316#ifdef __SMM__
317 dev = PCI_DEV(0, 31, 0);
318#else
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700319 dev = dev_find_slot(0, PCI_DEVFN(31, 0));
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700320#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700321
322 pci_read_config_dword(dev, 0xf0, &rcba);
323 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
324 rcrb = (uint8_t *)(rcba & 0xffffc000);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100325 ich9_spi = (ich9_spi_regs *)(rcrb + 0x3800);
326 cntlr.ich9_spi = ich9_spi;
327 hsfs = readw_(&ich9_spi->hsfs);
328 ichspi_lock = hsfs & HSFS_FLOCKDN;
329 cntlr.hsfs = hsfs;
330 cntlr.opmenu = ich9_spi->opmenu;
331 cntlr.menubytes = sizeof(ich9_spi->opmenu);
332 cntlr.optype = &ich9_spi->optype;
333 cntlr.addr = &ich9_spi->faddr;
334 cntlr.data = (uint8_t *)ich9_spi->fdata;
335 cntlr.databytes = sizeof(ich9_spi->fdata);
336 cntlr.status = &ich9_spi->ssfs;
337 cntlr.control = (uint16_t *)ich9_spi->ssfc;
338 cntlr.bbar = &ich9_spi->bbar;
339 cntlr.preop = &ich9_spi->preop;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700340
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100341 if (cntlr.hsfs & HSFS_FDV)
342 {
343 writel_ (4, &ich9_spi->fdoc);
344 cntlr.flmap0 = readl_(&ich9_spi->fdod);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700345 }
346
347 ich_set_bbar(0);
348
349 /* Disable the BIOS write protect so write commands are allowed. */
350 pci_read_config_byte(dev, 0xdc, &bios_cntl);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100351 /* Deassert SMM BIOS Write Protect Disable. */
352 bios_cntl &= ~(1 << 5);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700353 pci_write_config_byte(dev, 0xdc, bios_cntl | 0x1);
354}
Aaron Durbin4d3de7e2015-09-02 17:34:04 -0500355
David Hendricksf2612a12014-04-13 16:27:02 -0700356static void spi_init_cb(void *unused)
357{
358 spi_init();
359}
360
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500361BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700362
363int spi_claim_bus(struct spi_slave *slave)
364{
365 /* Handled by ICH automatically. */
366 return 0;
367}
368
369void spi_release_bus(struct spi_slave *slave)
370{
371 /* Handled by ICH automatically. */
372}
373
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700374typedef struct spi_transaction {
375 const uint8_t *out;
376 uint32_t bytesout;
377 uint8_t *in;
378 uint32_t bytesin;
379 uint8_t type;
380 uint8_t opcode;
381 uint32_t offset;
382} spi_transaction;
383
384static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
385{
386 trans->out += bytes;
387 trans->bytesout -= bytes;
388}
389
390static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
391{
392 trans->in += bytes;
393 trans->bytesin -= bytes;
394}
395
396static void spi_setup_type(spi_transaction *trans)
397{
398 trans->type = 0xFF;
399
400 /* Try to guess spi type from read/write sizes. */
401 if (trans->bytesin == 0) {
402 if (trans->bytesout > 4)
403 /*
404 * If bytesin = 0 and bytesout > 4, we presume this is
405 * a write data operation, which is accompanied by an
406 * address.
407 */
408 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
409 else
410 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
411 return;
412 }
413
414 if (trans->bytesout == 1) { /* and bytesin is > 0 */
415 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
416 return;
417 }
418
419 if (trans->bytesout == 4) { /* and bytesin is > 0 */
420 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
421 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700422
423 /* Fast read command is called with 5 bytes instead of 4 */
424 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
425 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
426 --trans->bytesout;
427 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700428}
429
430static int spi_setup_opcode(spi_transaction *trans)
431{
432 uint16_t optypes;
433 uint8_t opmenu[cntlr.menubytes];
434
435 trans->opcode = trans->out[0];
436 spi_use_out(trans, 1);
437 if (!ichspi_lock) {
438 /* The lock is off, so just use index 0. */
439 writeb_(trans->opcode, cntlr.opmenu);
440 optypes = readw_(cntlr.optype);
441 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
442 writew_(optypes, cntlr.optype);
443 return 0;
444 } else {
445 /* The lock is on. See if what we need is on the menu. */
446 uint8_t optype;
447 uint16_t opcode_index;
448
Duncan Lauriea2f1b952012-08-27 11:10:43 -0700449 /* Write Enable is handled as atomic prefix */
450 if (trans->opcode == SPI_OPCODE_WREN)
451 return 0;
452
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700453 read_reg(cntlr.opmenu, opmenu, sizeof(opmenu));
454 for (opcode_index = 0; opcode_index < cntlr.menubytes;
455 opcode_index++) {
456 if (opmenu[opcode_index] == trans->opcode)
457 break;
458 }
459
460 if (opcode_index == cntlr.menubytes) {
461 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
462 trans->opcode);
463 return -1;
464 }
465
466 optypes = readw_(cntlr.optype);
467 optype = (optypes >> (opcode_index * 2)) & 0x3;
468 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
469 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
470 trans->bytesout >= 3) {
471 /* We guessed wrong earlier. Fix it up. */
472 trans->type = optype;
473 }
474 if (optype != trans->type) {
475 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
476 optype);
477 return -1;
478 }
479 return opcode_index;
480 }
481}
482
483static int spi_setup_offset(spi_transaction *trans)
484{
485 /* Separate the SPI address and data. */
486 switch (trans->type) {
487 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
488 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
489 return 0;
490 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
491 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
492 trans->offset = ((uint32_t)trans->out[0] << 16) |
493 ((uint32_t)trans->out[1] << 8) |
494 ((uint32_t)trans->out[2] << 0);
495 spi_use_out(trans, 3);
496 return 1;
497 default:
498 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
499 return -1;
500 }
501}
502
503/*
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200504 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700505 * below is True) or 0. In case the wait was for the bit(s) to set - write
506 * those bits back, which would cause resetting them.
507 *
508 * Return the last read status value on success or -1 on failure.
509 */
510static int ich_status_poll(u16 bitmask, int wait_til_set)
511{
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200512 int timeout = 600000; /* This will result in 6 seconds */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700513 u16 status = 0;
514
515 while (timeout--) {
516 status = readw_(cntlr.status);
517 if (wait_til_set ^ ((status & bitmask) == 0)) {
518 if (wait_til_set)
519 writew_((status & bitmask), cntlr.status);
520 return status;
521 }
522 udelay(10);
523 }
524
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200525 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700526 status, bitmask);
527 return -1;
528}
529
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100530static int spi_is_multichip (void)
531{
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100532 if (!(cntlr.hsfs & HSFS_FDV))
533 return 0;
534 return !!((cntlr.flmap0 >> 8) & 3);
535}
536
Kyösti Mälkki11104952014-06-29 16:17:33 +0300537unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len)
538{
539 return min(cntlr.databytes, buf_len);
540}
541
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700542int spi_xfer(struct spi_slave *slave, const void *dout,
Gabe Black93d9f922014-03-27 21:52:43 -0700543 unsigned int bytesout, void *din, unsigned int bytesin)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700544{
545 uint16_t control;
546 int16_t opcode_index;
547 int with_address;
548 int status;
549
550 spi_transaction trans = {
Gabe Black93d9f922014-03-27 21:52:43 -0700551 dout, bytesout,
552 din, bytesin,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700553 0xff, 0xff, 0
554 };
555
556 /* There has to always at least be an opcode. */
Gabe Black93d9f922014-03-27 21:52:43 -0700557 if (!bytesout || !dout) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700558 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
559 return -1;
560 }
561 /* Make sure if we read something we have a place to put it. */
Gabe Black93d9f922014-03-27 21:52:43 -0700562 if (bytesin != 0 && !din) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700563 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
564 return -1;
565 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700566
567 if (ich_status_poll(SPIS_SCIP, 0) == -1)
568 return -1;
569
570 writew_(SPIS_CDS | SPIS_FCERR, cntlr.status);
571
572 spi_setup_type(&trans);
573 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
574 return -1;
575 if ((with_address = spi_setup_offset(&trans)) < 0)
576 return -1;
577
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700578 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700579 /*
580 * Treat Write Enable as Atomic Pre-Op if possible
581 * in order to prevent the Management Engine from
582 * issuing a transaction between WREN and DATA.
583 */
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700584 if (!ichspi_lock)
585 writew_(trans.opcode, cntlr.preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700586 return 0;
587 }
588
589 /* Preset control fields */
590 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
591
592 /* Issue atomic preop cycle if needed */
593 if (readw_(cntlr.preop))
594 control |= SPIC_ACS;
595
596 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700597 /* SPI addresses are 24 bit only */
598 if (with_address)
599 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
600
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700601 /*
602 * This is a 'no data' command (like Write Enable), its
603 * bitesout size was 1, decremented to zero while executing
604 * spi_setup_opcode() above. Tell the chip to send the
605 * command.
606 */
607 writew_(control, cntlr.control);
608
609 /* wait for the result */
610 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
611 if (status == -1)
612 return -1;
613
614 if (status & SPIS_FCERR) {
615 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
616 return -1;
617 }
618
619 return 0;
620 }
621
622 /*
Paul Menzel94782972013-06-29 11:41:27 +0200623 * Check if this is a write command attempting to transfer more bytes
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700624 * than the controller can handle. Iterations for writes are not
625 * supported here because each SPI write command needs to be preceded
626 * and followed by other SPI commands, and this sequence is controlled
627 * by the SPI chip driver.
628 */
629 if (trans.bytesout > cntlr.databytes) {
630 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
Kyösti Mälkki11104952014-06-29 16:17:33 +0300631 " spi_crop_chunk()?\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700632 return -1;
633 }
634
635 /*
636 * Read or write up to databytes bytes at a time until everything has
637 * been sent.
638 */
639 while (trans.bytesout || trans.bytesin) {
640 uint32_t data_length;
641
642 /* SPI addresses are 24 bit only */
643 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
644
645 if (trans.bytesout)
646 data_length = min(trans.bytesout, cntlr.databytes);
647 else
648 data_length = min(trans.bytesin, cntlr.databytes);
649
650 /* Program data into FDATA0 to N */
651 if (trans.bytesout) {
652 write_reg(trans.out, cntlr.data, data_length);
653 spi_use_out(&trans, data_length);
654 if (with_address)
655 trans.offset += data_length;
656 }
657
658 /* Add proper control fields' values */
659 control &= ~((cntlr.databytes - 1) << 8);
660 control |= SPIC_DS;
661 control |= (data_length - 1) << 8;
662
663 /* write it */
664 writew_(control, cntlr.control);
665
666 /* Wait for Cycle Done Status or Flash Cycle Error. */
667 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
668 if (status == -1)
669 return -1;
670
671 if (status & SPIS_FCERR) {
672 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
673 return -1;
674 }
675
676 if (trans.bytesin) {
677 read_reg(cntlr.data, trans.in, data_length);
678 spi_use_in(&trans, data_length);
679 if (with_address)
680 trans.offset += data_length;
681 }
682 }
683
684 /* Clear atomic preop now that xfer is done */
685 writew_(0, cntlr.preop);
686
687 return 0;
688}
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100689
690/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
691static void ich_hwseq_set_addr(uint32_t addr)
692{
693 uint32_t addr_old = readl_(&cntlr.ich9_spi->faddr) & ~0x01FFFFFF;
694 writel_((addr & 0x01FFFFFF) | addr_old, &cntlr.ich9_spi->faddr);
695}
696
697/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
698 Resets all error flags in HSFS.
699 Returns 0 if the cycle completes successfully without errors within
700 timeout us, 1 on errors. */
701static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
702 unsigned int len)
703{
704 uint16_t hsfs;
705 uint32_t addr;
706
707 timeout /= 8; /* scale timeout duration to counter */
708 while ((((hsfs = readw_(&cntlr.ich9_spi->hsfs)) &
709 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
710 --timeout) {
711 udelay(8);
712 }
713 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
714
715 if (!timeout) {
716 uint16_t hsfc;
717 addr = readl_(&cntlr.ich9_spi->faddr) & 0x01FFFFFF;
718 hsfc = readw_(&cntlr.ich9_spi->hsfc);
719 printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and "
720 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
721 addr, addr + len - 1, addr, len - 1,
722 hsfc, hsfs);
723 return 1;
724 }
725
726 if (hsfs & HSFS_FCERR) {
727 uint16_t hsfc;
728 addr = readl_(&cntlr.ich9_spi->faddr) & 0x01FFFFFF;
729 hsfc = readw_(&cntlr.ich9_spi->hsfc);
730 printk(BIOS_ERR, "Transaction error between offset 0x%08x and "
731 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
732 addr, addr + len - 1, addr, len - 1,
733 hsfc, hsfs);
734 return 1;
735 }
736 return 0;
737}
738
739
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800740static int ich_hwseq_erase(const struct spi_flash *flash, u32 offset,
741 size_t len)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100742{
743 u32 start, end, erase_size;
744 int ret;
745 uint16_t hsfc;
746 uint16_t timeout = 1000 * 60;
747
748 erase_size = flash->sector_size;
749 if (offset % erase_size || len % erase_size) {
750 printk(BIOS_ERR, "SF: Erase offset/length not multiple of erase size\n");
751 return -1;
752 }
753
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100754 ret = spi_claim_bus(flash->spi);
755 if (ret) {
756 printk(BIOS_ERR, "SF: Unable to claim SPI bus\n");
757 return ret;
758 }
759
760 start = offset;
761 end = start + len;
762
763 while (offset < end) {
764 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
765 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
766
767 ich_hwseq_set_addr(offset);
768
769 offset += erase_size;
770
771 hsfc = readw_(&cntlr.ich9_spi->hsfc);
772 hsfc &= ~HSFC_FCYCLE; /* clear operation */
773 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
774 hsfc |= HSFC_FGO; /* start */
775 writew_(hsfc, &cntlr.ich9_spi->hsfc);
776 if (ich_hwseq_wait_for_cycle_complete(timeout, len))
777 {
778 printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size);
779 ret = -1;
780 goto out;
781 }
782 }
783
784 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
785
786out:
787 spi_release_bus(flash->spi);
788 return ret;
789}
790
791static void ich_read_data(uint8_t *data, int len)
792{
793 int i;
794 uint32_t temp32 = 0;
795
796 for (i = 0; i < len; i++) {
797 if ((i % 4) == 0)
798 temp32 = readl_(cntlr.data + i);
799
800 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
801 }
802}
803
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800804static int ich_hwseq_read(const struct spi_flash *flash, u32 addr, size_t len,
805 void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100806{
807 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) */
820 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
821
822 while (len > 0) {
823 block_len = min(len, cntlr.databytes);
824 if (block_len > (~addr & 0xff))
825 block_len = (~addr & 0xff) + 1;
826 ich_hwseq_set_addr(addr);
827 hsfc = readw_(&cntlr.ich9_spi->hsfc);
828 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 */
833 writew_(hsfc, &cntlr.ich9_spi->hsfc);
834
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{
852 uint32_t temp32 = 0;
853 int i;
854
855 if (len <= 0)
856 return;
857
858 for (i = 0; i < len; i++) {
859 if ((i % 4) == 0)
860 temp32 = 0;
861
862 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
863
864 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
865 writel_(temp32, cntlr.data + (i - (i % 4)));
866 }
867 i--;
868 if ((i % 4) != 3) /* Write remaining data to regs. */
869 writel_(temp32, cntlr.data + (i - (i % 4)));
870}
871
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800872static int ich_hwseq_write(const struct spi_flash *flash, u32 addr, size_t len,
873 const void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100874{
875 uint16_t hsfc;
876 uint16_t timeout = 100 * 60;
877 uint8_t block_len;
878 uint32_t start = addr;
879
880 if (addr + len > flash->size) {
881 printk (BIOS_ERR,
882 "Attempt to write 0x%x-0x%x which is out of chip\n",
883 (unsigned)addr, (unsigned) (addr+len));
884 return -1;
885 }
886
887 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
888 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
889
890 while (len > 0) {
891 block_len = min(len, cntlr.databytes);
892 if (block_len > (~addr & 0xff))
893 block_len = (~addr & 0xff) + 1;
894
895 ich_hwseq_set_addr(addr);
896
897 ich_fill_data(buf, block_len);
898 hsfc = readw_(&cntlr.ich9_spi->hsfc);
899 hsfc &= ~HSFC_FCYCLE; /* clear operation */
900 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
901 hsfc &= ~HSFC_FDBC; /* clear byte count */
902 /* set byte count */
903 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
904 hsfc |= HSFC_FGO; /* start */
905 writew_(hsfc, &cntlr.ich9_spi->hsfc);
906
907 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
908 {
909 printk (BIOS_ERR, "SF: write failure at %x\n",
910 addr);
911 return -1;
912 }
913 addr += block_len;
914 buf += block_len;
915 len -= block_len;
916 }
917 printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n",
918 (unsigned) (addr - start), start);
919 return 0;
920}
921
922
923static struct spi_flash *spi_flash_hwseq(struct spi_slave *spi)
924{
925 struct spi_flash *flash = NULL;
926 uint32_t flcomp;
927
928 flash = malloc(sizeof(*flash));
929 if (!flash) {
930 printk(BIOS_WARNING, "SF: Failed to allocate memory\n");
931 return NULL;
932 }
933
934 flash->spi = spi;
935 flash->name = "Opaque HW-sequencing";
936
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800937 flash->internal_write = ich_hwseq_write;
938 flash->internal_erase = ich_hwseq_erase;
939 flash->internal_read = ich_hwseq_read;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100940 ich_hwseq_set_addr (0);
941 switch ((cntlr.hsfs >> 3) & 3)
942 {
943 case 0:
944 flash->sector_size = 256;
945 break;
946 case 1:
947 flash->sector_size = 4096;
948 break;
949 case 2:
950 flash->sector_size = 8192;
951 break;
952 case 3:
953 flash->sector_size = 65536;
954 break;
955 }
956
957 writel_ (0x1000, &cntlr.ich9_spi->fdoc);
958 flcomp = readl_(&cntlr.ich9_spi->fdod);
959
960 flash->size = 1 << (19 + (flcomp & 7));
961
962 if ((cntlr.hsfs & HSFS_FDV) && ((cntlr.flmap0 >> 8) & 3))
963 flash->size += 1 << (19 + ((flcomp >> 3) & 7));
964 printk (BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
965
966 return flash;
967}