blob: 2ea9a24142188f51744de4f13c064bec20cc5d45 [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 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but without any warranty; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25/* This file is derived from the flashrom project. */
26#include <stdint.h>
27#include <stdlib.h>
28#include <string.h>
29#include <delay.h>
30#include <arch/io.h>
31#include <console/console.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070032#include <device/pci_ids.h>
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010033#include <device/pci.h>
34#include <spi_flash.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070035
Zheng Bao600784e2013-02-07 17:30:23 +080036#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070037
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010038#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
39#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
40#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
41#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
42
43
Duncan Laurie181bbdd2012-06-23 16:53:57 -070044#ifdef __SMM__
Kyösti Mälkki54d6abd2013-06-19 23:05:00 +030045#include <arch/pci_mmio_cfg.h>
Duncan Laurie181bbdd2012-06-23 16:53:57 -070046#define pci_read_config_byte(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030047 *(targ) = pci_read_config8(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070048#define pci_read_config_word(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030049 *(targ) = pci_read_config16(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070050#define pci_read_config_dword(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030051 *(targ) = pci_read_config32(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070052#define pci_write_config_byte(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030053 pci_write_config8(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070054#define pci_write_config_word(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030055 pci_write_config16(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070056#define pci_write_config_dword(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030057 pci_write_config32(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070058#else /* !__SMM__ */
59#include <device/device.h>
60#include <device/pci.h>
Duncan Laurie181bbdd2012-06-23 16:53:57 -070061#define pci_read_config_byte(dev, reg, targ)\
62 *(targ) = pci_read_config8(dev, reg)
63#define pci_read_config_word(dev, reg, targ)\
64 *(targ) = pci_read_config16(dev, reg)
65#define pci_read_config_dword(dev, reg, targ)\
66 *(targ) = pci_read_config32(dev, reg)
67#define pci_write_config_byte(dev, reg, val)\
68 pci_write_config8(dev, reg, val)
69#define pci_write_config_word(dev, reg, val)\
70 pci_write_config16(dev, reg, val)
71#define pci_write_config_dword(dev, reg, val)\
72 pci_write_config32(dev, reg, val)
73#endif /* !__SMM__ */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070074
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010075static int spi_is_multichip(void);
76static struct spi_flash *spi_flash_hwseq(struct spi_slave *spi);
77
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070078typedef struct spi_slave ich_spi_slave;
79
80static int ichspi_lock = 0;
81
82typedef struct ich7_spi_regs {
83 uint16_t spis;
84 uint16_t spic;
85 uint32_t spia;
86 uint64_t spid[8];
87 uint64_t _pad;
88 uint32_t bbar;
89 uint16_t preop;
90 uint16_t optype;
91 uint8_t opmenu[8];
92} __attribute__((packed)) ich7_spi_regs;
93
94typedef struct ich9_spi_regs {
95 uint32_t bfpr;
96 uint16_t hsfs;
97 uint16_t hsfc;
98 uint32_t faddr;
99 uint32_t _reserved0;
100 uint32_t fdata[16];
101 uint32_t frap;
102 uint32_t freg[5];
103 uint32_t _reserved1[3];
104 uint32_t pr[5];
105 uint32_t _reserved2[2];
106 uint8_t ssfs;
107 uint8_t ssfc[3];
108 uint16_t preop;
109 uint16_t optype;
110 uint8_t opmenu[8];
111 uint32_t bbar;
112 uint8_t _reserved3[12];
113 uint32_t fdoc;
114 uint32_t fdod;
115 uint8_t _reserved4[8];
116 uint32_t afc;
117 uint32_t lvscc;
118 uint32_t uvscc;
119 uint8_t _reserved5[4];
120 uint32_t fpb;
121 uint8_t _reserved6[28];
122 uint32_t srdl;
123 uint32_t srdc;
124 uint32_t srd;
125} __attribute__((packed)) ich9_spi_regs;
126
127typedef struct ich_spi_controller {
128 int locked;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100129 uint32_t flmap0;
130 uint32_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700131
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100132 ich9_spi_regs *ich9_spi;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700133 uint8_t *opmenu;
134 int menubytes;
135 uint16_t *preop;
136 uint16_t *optype;
137 uint32_t *addr;
138 uint8_t *data;
139 unsigned databytes;
140 uint8_t *status;
141 uint16_t *control;
142 uint32_t *bbar;
143} ich_spi_controller;
144
145static ich_spi_controller cntlr;
146
147enum {
148 SPIS_SCIP = 0x0001,
149 SPIS_GRANT = 0x0002,
150 SPIS_CDS = 0x0004,
151 SPIS_FCERR = 0x0008,
152 SSFS_AEL = 0x0010,
153 SPIS_LOCK = 0x8000,
154 SPIS_RESERVED_MASK = 0x7ff0,
155 SSFS_RESERVED_MASK = 0x7fe2
156};
157
158enum {
159 SPIC_SCGO = 0x000002,
160 SPIC_ACS = 0x000004,
161 SPIC_SPOP = 0x000008,
162 SPIC_DBC = 0x003f00,
163 SPIC_DS = 0x004000,
164 SPIC_SME = 0x008000,
165 SSFC_SCF_MASK = 0x070000,
166 SSFC_RESERVED = 0xf80000
167};
168
169enum {
170 HSFS_FDONE = 0x0001,
171 HSFS_FCERR = 0x0002,
172 HSFS_AEL = 0x0004,
173 HSFS_BERASE_MASK = 0x0018,
174 HSFS_BERASE_SHIFT = 3,
175 HSFS_SCIP = 0x0020,
176 HSFS_FDOPSS = 0x2000,
177 HSFS_FDV = 0x4000,
178 HSFS_FLOCKDN = 0x8000
179};
180
181enum {
182 HSFC_FGO = 0x0001,
183 HSFC_FCYCLE_MASK = 0x0006,
184 HSFC_FCYCLE_SHIFT = 1,
185 HSFC_FDBC_MASK = 0x3f00,
186 HSFC_FDBC_SHIFT = 8,
187 HSFC_FSMIE = 0x8000
188};
189
190enum {
191 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
192 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
193 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
194 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
195};
196
197#if CONFIG_DEBUG_SPI_FLASH
198
199static u8 readb_(const void *addr)
200{
Stefan Reinauer14b23a62012-05-22 15:24:51 -0700201 u8 v = read8((unsigned long)addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700202 printk(BIOS_DEBUG, "read %2.2x from %4.4x\n",
203 v, ((unsigned) addr & 0xffff) - 0xf020);
204 return v;
205}
206
207static u16 readw_(const void *addr)
208{
Stefan Reinauer14b23a62012-05-22 15:24:51 -0700209 u16 v = read16((unsigned long)addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700210 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
211 v, ((unsigned) addr & 0xffff) - 0xf020);
212 return v;
213}
214
215static u32 readl_(const void *addr)
216{
Stefan Reinauer14b23a62012-05-22 15:24:51 -0700217 u32 v = read32((unsigned long)addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700218 printk(BIOS_DEBUG, "read %8.8x from %4.4x\n",
219 v, ((unsigned) addr & 0xffff) - 0xf020);
220 return v;
221}
222
223static void writeb_(u8 b, const void *addr)
224{
Stefan Reinauer14b23a62012-05-22 15:24:51 -0700225 write8((unsigned long)addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700226 printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n",
227 b, ((unsigned) addr & 0xffff) - 0xf020);
228}
229
230static void writew_(u16 b, const void *addr)
231{
Stefan Reinauer14b23a62012-05-22 15:24:51 -0700232 write16((unsigned long)addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700233 printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n",
234 b, ((unsigned) addr & 0xffff) - 0xf020);
235}
236
237static void writel_(u32 b, const void *addr)
238{
Stefan Reinauer14b23a62012-05-22 15:24:51 -0700239 write32((unsigned long)addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700240 printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n",
241 b, ((unsigned) addr & 0xffff) - 0xf020);
242}
243
244#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
245
246#define readb_(a) read8((uint32_t)a)
247#define readw_(a) read16((uint32_t)a)
248#define readl_(a) read32((uint32_t)a)
249#define writeb_(val, addr) write8((uint32_t)addr, val)
250#define writew_(val, addr) write16((uint32_t)addr, val)
251#define writel_(val, addr) write32((uint32_t)addr, val)
252
253#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
254
255static void write_reg(const void *value, void *dest, uint32_t size)
256{
257 const uint8_t *bvalue = value;
258 uint8_t *bdest = dest;
259
260 while (size >= 4) {
261 writel_(*(const uint32_t *)bvalue, bdest);
262 bdest += 4; bvalue += 4; size -= 4;
263 }
264 while (size) {
265 writeb_(*bvalue, bdest);
266 bdest++; bvalue++; size--;
267 }
268}
269
270static void read_reg(const void *src, void *value, uint32_t size)
271{
272 const uint8_t *bsrc = src;
273 uint8_t *bvalue = value;
274
275 while (size >= 4) {
276 *(uint32_t *)bvalue = readl_(bsrc);
277 bsrc += 4; bvalue += 4; size -= 4;
278 }
279 while (size) {
280 *bvalue = readb_(bsrc);
281 bsrc++; bvalue++; size--;
282 }
283}
284
285static void ich_set_bbar(uint32_t minaddr)
286{
287 const uint32_t bbar_mask = 0x00ffff00;
288 uint32_t ichspi_bbar;
289
290 minaddr &= bbar_mask;
291 ichspi_bbar = readl_(cntlr.bbar) & ~bbar_mask;
292 ichspi_bbar |= minaddr;
293 writel_(ichspi_bbar, cntlr.bbar);
294}
295
296int spi_cs_is_valid(unsigned int bus, unsigned int cs)
297{
298 printk(BIOS_DEBUG, "spi_cs_is_valid used but not implemented\n");
299 return 0;
300}
301
Gabe Black1e187352014-03-27 20:37:03 -0700302struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700303{
304 ich_spi_slave *slave = malloc(sizeof(*slave));
305
306 if (!slave) {
307 printk(BIOS_DEBUG, "ICH SPI: Bad allocation\n");
308 return NULL;
309 }
310
311 memset(slave, 0, sizeof(*slave));
312
313 slave->bus = bus;
314 slave->cs = cs;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100315 slave->force_programmer_specific = spi_is_multichip ();
316 slave->programmer_specific_probe = spi_flash_hwseq;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700317 return slave;
318}
319
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700320void spi_init(void)
321{
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700322 uint8_t *rcrb; /* Root Complex Register Block */
323 uint32_t rcba; /* Root Complex Base Address */
324 uint8_t bios_cntl;
Stefan Reinauer0c32c972012-07-10 13:26:59 -0700325 device_t dev;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100326 ich9_spi_regs *ich9_spi;
327 uint16_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700328
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700329#ifdef __SMM__
330 dev = PCI_DEV(0, 31, 0);
331#else
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700332 dev = dev_find_slot(0, PCI_DEVFN(31, 0));
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700333#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700334
335 pci_read_config_dword(dev, 0xf0, &rcba);
336 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
337 rcrb = (uint8_t *)(rcba & 0xffffc000);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100338 ich9_spi = (ich9_spi_regs *)(rcrb + 0x3800);
339 cntlr.ich9_spi = ich9_spi;
340 hsfs = readw_(&ich9_spi->hsfs);
341 ichspi_lock = hsfs & HSFS_FLOCKDN;
342 cntlr.hsfs = hsfs;
343 cntlr.opmenu = ich9_spi->opmenu;
344 cntlr.menubytes = sizeof(ich9_spi->opmenu);
345 cntlr.optype = &ich9_spi->optype;
346 cntlr.addr = &ich9_spi->faddr;
347 cntlr.data = (uint8_t *)ich9_spi->fdata;
348 cntlr.databytes = sizeof(ich9_spi->fdata);
349 cntlr.status = &ich9_spi->ssfs;
350 cntlr.control = (uint16_t *)ich9_spi->ssfc;
351 cntlr.bbar = &ich9_spi->bbar;
352 cntlr.preop = &ich9_spi->preop;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700353
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100354 if (cntlr.hsfs & HSFS_FDV)
355 {
356 writel_ (4, &ich9_spi->fdoc);
357 cntlr.flmap0 = readl_(&ich9_spi->fdod);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700358 }
359
360 ich_set_bbar(0);
361
362 /* Disable the BIOS write protect so write commands are allowed. */
363 pci_read_config_byte(dev, 0xdc, &bios_cntl);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100364 /* Deassert SMM BIOS Write Protect Disable. */
365 bios_cntl &= ~(1 << 5);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700366 pci_write_config_byte(dev, 0xdc, bios_cntl | 0x1);
367}
368
369int spi_claim_bus(struct spi_slave *slave)
370{
371 /* Handled by ICH automatically. */
372 return 0;
373}
374
375void spi_release_bus(struct spi_slave *slave)
376{
377 /* Handled by ICH automatically. */
378}
379
380void spi_cs_activate(struct spi_slave *slave)
381{
382 /* Handled by ICH automatically. */
383}
384
385void spi_cs_deactivate(struct spi_slave *slave)
386{
387 /* Handled by ICH automatically. */
388}
389
390typedef struct spi_transaction {
391 const uint8_t *out;
392 uint32_t bytesout;
393 uint8_t *in;
394 uint32_t bytesin;
395 uint8_t type;
396 uint8_t opcode;
397 uint32_t offset;
398} spi_transaction;
399
400static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
401{
402 trans->out += bytes;
403 trans->bytesout -= bytes;
404}
405
406static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
407{
408 trans->in += bytes;
409 trans->bytesin -= bytes;
410}
411
412static void spi_setup_type(spi_transaction *trans)
413{
414 trans->type = 0xFF;
415
416 /* Try to guess spi type from read/write sizes. */
417 if (trans->bytesin == 0) {
418 if (trans->bytesout > 4)
419 /*
420 * If bytesin = 0 and bytesout > 4, we presume this is
421 * a write data operation, which is accompanied by an
422 * address.
423 */
424 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
425 else
426 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
427 return;
428 }
429
430 if (trans->bytesout == 1) { /* and bytesin is > 0 */
431 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
432 return;
433 }
434
435 if (trans->bytesout == 4) { /* and bytesin is > 0 */
436 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
437 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700438
439 /* Fast read command is called with 5 bytes instead of 4 */
440 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
441 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
442 --trans->bytesout;
443 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700444}
445
446static int spi_setup_opcode(spi_transaction *trans)
447{
448 uint16_t optypes;
449 uint8_t opmenu[cntlr.menubytes];
450
451 trans->opcode = trans->out[0];
452 spi_use_out(trans, 1);
453 if (!ichspi_lock) {
454 /* The lock is off, so just use index 0. */
455 writeb_(trans->opcode, cntlr.opmenu);
456 optypes = readw_(cntlr.optype);
457 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
458 writew_(optypes, cntlr.optype);
459 return 0;
460 } else {
461 /* The lock is on. See if what we need is on the menu. */
462 uint8_t optype;
463 uint16_t opcode_index;
464
Duncan Lauriea2f1b952012-08-27 11:10:43 -0700465 /* Write Enable is handled as atomic prefix */
466 if (trans->opcode == SPI_OPCODE_WREN)
467 return 0;
468
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700469 read_reg(cntlr.opmenu, opmenu, sizeof(opmenu));
470 for (opcode_index = 0; opcode_index < cntlr.menubytes;
471 opcode_index++) {
472 if (opmenu[opcode_index] == trans->opcode)
473 break;
474 }
475
476 if (opcode_index == cntlr.menubytes) {
477 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
478 trans->opcode);
479 return -1;
480 }
481
482 optypes = readw_(cntlr.optype);
483 optype = (optypes >> (opcode_index * 2)) & 0x3;
484 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
485 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
486 trans->bytesout >= 3) {
487 /* We guessed wrong earlier. Fix it up. */
488 trans->type = optype;
489 }
490 if (optype != trans->type) {
491 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
492 optype);
493 return -1;
494 }
495 return opcode_index;
496 }
497}
498
499static int spi_setup_offset(spi_transaction *trans)
500{
501 /* Separate the SPI address and data. */
502 switch (trans->type) {
503 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
504 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
505 return 0;
506 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
507 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
508 trans->offset = ((uint32_t)trans->out[0] << 16) |
509 ((uint32_t)trans->out[1] << 8) |
510 ((uint32_t)trans->out[2] << 0);
511 spi_use_out(trans, 3);
512 return 1;
513 default:
514 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
515 return -1;
516 }
517}
518
519/*
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200520 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700521 * below is True) or 0. In case the wait was for the bit(s) to set - write
522 * those bits back, which would cause resetting them.
523 *
524 * Return the last read status value on success or -1 on failure.
525 */
526static int ich_status_poll(u16 bitmask, int wait_til_set)
527{
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200528 int timeout = 600000; /* This will result in 6 seconds */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700529 u16 status = 0;
530
531 while (timeout--) {
532 status = readw_(cntlr.status);
533 if (wait_til_set ^ ((status & bitmask) == 0)) {
534 if (wait_til_set)
535 writew_((status & bitmask), cntlr.status);
536 return status;
537 }
538 udelay(10);
539 }
540
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200541 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700542 status, bitmask);
543 return -1;
544}
545
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100546static int spi_is_multichip (void)
547{
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100548 if (!(cntlr.hsfs & HSFS_FDV))
549 return 0;
550 return !!((cntlr.flmap0 >> 8) & 3);
551}
552
Kyösti Mälkki11104952014-06-29 16:17:33 +0300553unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len)
554{
555 return min(cntlr.databytes, buf_len);
556}
557
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700558int spi_xfer(struct spi_slave *slave, const void *dout,
Gabe Black93d9f922014-03-27 21:52:43 -0700559 unsigned int bytesout, void *din, unsigned int bytesin)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700560{
561 uint16_t control;
562 int16_t opcode_index;
563 int with_address;
564 int status;
565
566 spi_transaction trans = {
Gabe Black93d9f922014-03-27 21:52:43 -0700567 dout, bytesout,
568 din, bytesin,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700569 0xff, 0xff, 0
570 };
571
572 /* There has to always at least be an opcode. */
Gabe Black93d9f922014-03-27 21:52:43 -0700573 if (!bytesout || !dout) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700574 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
575 return -1;
576 }
577 /* Make sure if we read something we have a place to put it. */
Gabe Black93d9f922014-03-27 21:52:43 -0700578 if (bytesin != 0 && !din) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700579 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
580 return -1;
581 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700582
583 if (ich_status_poll(SPIS_SCIP, 0) == -1)
584 return -1;
585
586 writew_(SPIS_CDS | SPIS_FCERR, cntlr.status);
587
588 spi_setup_type(&trans);
589 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
590 return -1;
591 if ((with_address = spi_setup_offset(&trans)) < 0)
592 return -1;
593
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700594 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700595 /*
596 * Treat Write Enable as Atomic Pre-Op if possible
597 * in order to prevent the Management Engine from
598 * issuing a transaction between WREN and DATA.
599 */
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700600 if (!ichspi_lock)
601 writew_(trans.opcode, cntlr.preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700602 return 0;
603 }
604
605 /* Preset control fields */
606 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
607
608 /* Issue atomic preop cycle if needed */
609 if (readw_(cntlr.preop))
610 control |= SPIC_ACS;
611
612 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700613 /* SPI addresses are 24 bit only */
614 if (with_address)
615 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
616
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700617 /*
618 * This is a 'no data' command (like Write Enable), its
619 * bitesout size was 1, decremented to zero while executing
620 * spi_setup_opcode() above. Tell the chip to send the
621 * command.
622 */
623 writew_(control, cntlr.control);
624
625 /* wait for the result */
626 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
627 if (status == -1)
628 return -1;
629
630 if (status & SPIS_FCERR) {
631 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
632 return -1;
633 }
634
635 return 0;
636 }
637
638 /*
Paul Menzel94782972013-06-29 11:41:27 +0200639 * Check if this is a write command attempting to transfer more bytes
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700640 * than the controller can handle. Iterations for writes are not
641 * supported here because each SPI write command needs to be preceded
642 * and followed by other SPI commands, and this sequence is controlled
643 * by the SPI chip driver.
644 */
645 if (trans.bytesout > cntlr.databytes) {
646 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
Kyösti Mälkki11104952014-06-29 16:17:33 +0300647 " spi_crop_chunk()?\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700648 return -1;
649 }
650
651 /*
652 * Read or write up to databytes bytes at a time until everything has
653 * been sent.
654 */
655 while (trans.bytesout || trans.bytesin) {
656 uint32_t data_length;
657
658 /* SPI addresses are 24 bit only */
659 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
660
661 if (trans.bytesout)
662 data_length = min(trans.bytesout, cntlr.databytes);
663 else
664 data_length = min(trans.bytesin, cntlr.databytes);
665
666 /* Program data into FDATA0 to N */
667 if (trans.bytesout) {
668 write_reg(trans.out, cntlr.data, data_length);
669 spi_use_out(&trans, data_length);
670 if (with_address)
671 trans.offset += data_length;
672 }
673
674 /* Add proper control fields' values */
675 control &= ~((cntlr.databytes - 1) << 8);
676 control |= SPIC_DS;
677 control |= (data_length - 1) << 8;
678
679 /* write it */
680 writew_(control, cntlr.control);
681
682 /* Wait for Cycle Done Status or Flash Cycle Error. */
683 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
684 if (status == -1)
685 return -1;
686
687 if (status & SPIS_FCERR) {
688 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
689 return -1;
690 }
691
692 if (trans.bytesin) {
693 read_reg(cntlr.data, trans.in, data_length);
694 spi_use_in(&trans, data_length);
695 if (with_address)
696 trans.offset += data_length;
697 }
698 }
699
700 /* Clear atomic preop now that xfer is done */
701 writew_(0, cntlr.preop);
702
703 return 0;
704}
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100705
706/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
707static void ich_hwseq_set_addr(uint32_t addr)
708{
709 uint32_t addr_old = readl_(&cntlr.ich9_spi->faddr) & ~0x01FFFFFF;
710 writel_((addr & 0x01FFFFFF) | addr_old, &cntlr.ich9_spi->faddr);
711}
712
713/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
714 Resets all error flags in HSFS.
715 Returns 0 if the cycle completes successfully without errors within
716 timeout us, 1 on errors. */
717static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
718 unsigned int len)
719{
720 uint16_t hsfs;
721 uint32_t addr;
722
723 timeout /= 8; /* scale timeout duration to counter */
724 while ((((hsfs = readw_(&cntlr.ich9_spi->hsfs)) &
725 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
726 --timeout) {
727 udelay(8);
728 }
729 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
730
731 if (!timeout) {
732 uint16_t hsfc;
733 addr = readl_(&cntlr.ich9_spi->faddr) & 0x01FFFFFF;
734 hsfc = readw_(&cntlr.ich9_spi->hsfc);
735 printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and "
736 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
737 addr, addr + len - 1, addr, len - 1,
738 hsfc, hsfs);
739 return 1;
740 }
741
742 if (hsfs & HSFS_FCERR) {
743 uint16_t hsfc;
744 addr = readl_(&cntlr.ich9_spi->faddr) & 0x01FFFFFF;
745 hsfc = readw_(&cntlr.ich9_spi->hsfc);
746 printk(BIOS_ERR, "Transaction error between offset 0x%08x and "
747 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
748 addr, addr + len - 1, addr, len - 1,
749 hsfc, hsfs);
750 return 1;
751 }
752 return 0;
753}
754
755
756static int ich_hwseq_erase(struct spi_flash *flash, u32 offset, size_t len)
757{
758 u32 start, end, erase_size;
759 int ret;
760 uint16_t hsfc;
761 uint16_t timeout = 1000 * 60;
762
763 erase_size = flash->sector_size;
764 if (offset % erase_size || len % erase_size) {
765 printk(BIOS_ERR, "SF: Erase offset/length not multiple of erase size\n");
766 return -1;
767 }
768
769 flash->spi->rw = SPI_WRITE_FLAG;
770 ret = spi_claim_bus(flash->spi);
771 if (ret) {
772 printk(BIOS_ERR, "SF: Unable to claim SPI bus\n");
773 return ret;
774 }
775
776 start = offset;
777 end = start + len;
778
779 while (offset < end) {
780 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
781 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
782
783 ich_hwseq_set_addr(offset);
784
785 offset += erase_size;
786
787 hsfc = readw_(&cntlr.ich9_spi->hsfc);
788 hsfc &= ~HSFC_FCYCLE; /* clear operation */
789 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
790 hsfc |= HSFC_FGO; /* start */
791 writew_(hsfc, &cntlr.ich9_spi->hsfc);
792 if (ich_hwseq_wait_for_cycle_complete(timeout, len))
793 {
794 printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size);
795 ret = -1;
796 goto out;
797 }
798 }
799
800 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
801
802out:
803 spi_release_bus(flash->spi);
804 return ret;
805}
806
807static void ich_read_data(uint8_t *data, int len)
808{
809 int i;
810 uint32_t temp32 = 0;
811
812 for (i = 0; i < len; i++) {
813 if ((i % 4) == 0)
814 temp32 = readl_(cntlr.data + i);
815
816 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
817 }
818}
819
820static int ich_hwseq_read(struct spi_flash *flash,
821 u32 addr, size_t len, void *buf)
822{
823 uint16_t hsfc;
824 uint16_t timeout = 100 * 60;
825 uint8_t block_len;
826
827 if (addr + len > flash->size) {
828 printk (BIOS_ERR,
829 "Attempt to read %x-%x which is out of chip\n",
830 (unsigned) addr,
831 (unsigned) addr+(unsigned) len);
832 return -1;
833 }
834
835 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
836 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
837
838 while (len > 0) {
839 block_len = min(len, cntlr.databytes);
840 if (block_len > (~addr & 0xff))
841 block_len = (~addr & 0xff) + 1;
842 ich_hwseq_set_addr(addr);
843 hsfc = readw_(&cntlr.ich9_spi->hsfc);
844 hsfc &= ~HSFC_FCYCLE; /* set read operation */
845 hsfc &= ~HSFC_FDBC; /* clear byte count */
846 /* set byte count */
847 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
848 hsfc |= HSFC_FGO; /* start */
849 writew_(hsfc, &cntlr.ich9_spi->hsfc);
850
851 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
852 return 1;
853 ich_read_data(buf, block_len);
854 addr += block_len;
855 buf += block_len;
856 len -= block_len;
857 }
858 return 0;
859}
860
861/* Fill len bytes from the data array into the fdata/spid registers.
862 *
863 * Note that using len > flash->pgm->spi.max_data_write will trash the registers
864 * following the data registers.
865 */
866static void ich_fill_data(const uint8_t *data, int len)
867{
868 uint32_t temp32 = 0;
869 int i;
870
871 if (len <= 0)
872 return;
873
874 for (i = 0; i < len; i++) {
875 if ((i % 4) == 0)
876 temp32 = 0;
877
878 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
879
880 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
881 writel_(temp32, cntlr.data + (i - (i % 4)));
882 }
883 i--;
884 if ((i % 4) != 3) /* Write remaining data to regs. */
885 writel_(temp32, cntlr.data + (i - (i % 4)));
886}
887
888static int ich_hwseq_write(struct spi_flash *flash,
889 u32 addr, size_t len, const void *buf)
890{
891 uint16_t hsfc;
892 uint16_t timeout = 100 * 60;
893 uint8_t block_len;
894 uint32_t start = addr;
895
896 if (addr + len > flash->size) {
897 printk (BIOS_ERR,
898 "Attempt to write 0x%x-0x%x which is out of chip\n",
899 (unsigned)addr, (unsigned) (addr+len));
900 return -1;
901 }
902
903 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
904 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
905
906 while (len > 0) {
907 block_len = min(len, cntlr.databytes);
908 if (block_len > (~addr & 0xff))
909 block_len = (~addr & 0xff) + 1;
910
911 ich_hwseq_set_addr(addr);
912
913 ich_fill_data(buf, block_len);
914 hsfc = readw_(&cntlr.ich9_spi->hsfc);
915 hsfc &= ~HSFC_FCYCLE; /* clear operation */
916 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
917 hsfc &= ~HSFC_FDBC; /* clear byte count */
918 /* set byte count */
919 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
920 hsfc |= HSFC_FGO; /* start */
921 writew_(hsfc, &cntlr.ich9_spi->hsfc);
922
923 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
924 {
925 printk (BIOS_ERR, "SF: write failure at %x\n",
926 addr);
927 return -1;
928 }
929 addr += block_len;
930 buf += block_len;
931 len -= block_len;
932 }
933 printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n",
934 (unsigned) (addr - start), start);
935 return 0;
936}
937
938
939static struct spi_flash *spi_flash_hwseq(struct spi_slave *spi)
940{
941 struct spi_flash *flash = NULL;
942 uint32_t flcomp;
943
944 flash = malloc(sizeof(*flash));
945 if (!flash) {
946 printk(BIOS_WARNING, "SF: Failed to allocate memory\n");
947 return NULL;
948 }
949
950 flash->spi = spi;
951 flash->name = "Opaque HW-sequencing";
952
953 flash->write = ich_hwseq_write;
954 flash->erase = ich_hwseq_erase;
955 flash->read = ich_hwseq_read;
956 ich_hwseq_set_addr (0);
957 switch ((cntlr.hsfs >> 3) & 3)
958 {
959 case 0:
960 flash->sector_size = 256;
961 break;
962 case 1:
963 flash->sector_size = 4096;
964 break;
965 case 2:
966 flash->sector_size = 8192;
967 break;
968 case 3:
969 flash->sector_size = 65536;
970 break;
971 }
972
973 writel_ (0x1000, &cntlr.ich9_spi->fdoc);
974 flcomp = readl_(&cntlr.ich9_spi->fdod);
975
976 flash->size = 1 << (19 + (flcomp & 7));
977
978 if ((cntlr.hsfs & HSFS_FDV) && ((cntlr.flmap0 >> 8) & 3))
979 flash->size += 1 << (19 + ((flcomp >> 3) & 7));
980 printk (BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
981
982 return flash;
983}