blob: 1d3ebf649e99ca724ab00747c8059df7b4f70c24 [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
Patrick Georgib890a122015-03-26 15:17:45 +010021 * Foundation, Inc.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070022 */
23
24/* This file is derived from the flashrom project. */
25#include <stdint.h>
26#include <stdlib.h>
27#include <string.h>
David Hendricksf2612a12014-04-13 16:27:02 -070028#include <bootstate.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070029#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{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800201 u8 v = read8(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{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800209 u16 v = read16(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{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800217 u32 v = read32(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
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800223static void writeb_(u8 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700224{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800225 write8(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
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800230static void writew_(u16 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700231{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800232 write16(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
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800237static void writel_(u32 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700238{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800239 write32(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
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800246#define readb_(a) read8(a)
247#define readw_(a) read16(a)
248#define readl_(a) read32(a)
249#define writeb_(val, addr) write8(addr, val)
250#define writew_(val, addr) write16(addr, val)
251#define writel_(val, addr) write32(addr, val)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700252
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
Gabe Black1e187352014-03-27 20:37:03 -0700296struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700297{
298 ich_spi_slave *slave = malloc(sizeof(*slave));
299
300 if (!slave) {
301 printk(BIOS_DEBUG, "ICH SPI: Bad allocation\n");
302 return NULL;
303 }
304
305 memset(slave, 0, sizeof(*slave));
306
307 slave->bus = bus;
308 slave->cs = cs;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100309 slave->force_programmer_specific = spi_is_multichip ();
310 slave->programmer_specific_probe = spi_flash_hwseq;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700311 return slave;
312}
313
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700314void spi_init(void)
315{
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700316 uint8_t *rcrb; /* Root Complex Register Block */
317 uint32_t rcba; /* Root Complex Base Address */
318 uint8_t bios_cntl;
Stefan Reinauer0c32c972012-07-10 13:26:59 -0700319 device_t dev;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100320 ich9_spi_regs *ich9_spi;
321 uint16_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700322
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700323#ifdef __SMM__
324 dev = PCI_DEV(0, 31, 0);
325#else
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700326 dev = dev_find_slot(0, PCI_DEVFN(31, 0));
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700327#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700328
329 pci_read_config_dword(dev, 0xf0, &rcba);
330 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
331 rcrb = (uint8_t *)(rcba & 0xffffc000);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100332 ich9_spi = (ich9_spi_regs *)(rcrb + 0x3800);
333 cntlr.ich9_spi = ich9_spi;
334 hsfs = readw_(&ich9_spi->hsfs);
335 ichspi_lock = hsfs & HSFS_FLOCKDN;
336 cntlr.hsfs = hsfs;
337 cntlr.opmenu = ich9_spi->opmenu;
338 cntlr.menubytes = sizeof(ich9_spi->opmenu);
339 cntlr.optype = &ich9_spi->optype;
340 cntlr.addr = &ich9_spi->faddr;
341 cntlr.data = (uint8_t *)ich9_spi->fdata;
342 cntlr.databytes = sizeof(ich9_spi->fdata);
343 cntlr.status = &ich9_spi->ssfs;
344 cntlr.control = (uint16_t *)ich9_spi->ssfc;
345 cntlr.bbar = &ich9_spi->bbar;
346 cntlr.preop = &ich9_spi->preop;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700347
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100348 if (cntlr.hsfs & HSFS_FDV)
349 {
350 writel_ (4, &ich9_spi->fdoc);
351 cntlr.flmap0 = readl_(&ich9_spi->fdod);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700352 }
353
354 ich_set_bbar(0);
355
356 /* Disable the BIOS write protect so write commands are allowed. */
357 pci_read_config_byte(dev, 0xdc, &bios_cntl);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100358 /* Deassert SMM BIOS Write Protect Disable. */
359 bios_cntl &= ~(1 << 5);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700360 pci_write_config_byte(dev, 0xdc, bios_cntl | 0x1);
361}
Stefan Reinauerc3d15a72015-03-16 16:50:27 -0700362#ifndef __SMM__
David Hendricksf2612a12014-04-13 16:27:02 -0700363static void spi_init_cb(void *unused)
364{
365 spi_init();
366}
367
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500368BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Stefan Reinauerc3d15a72015-03-16 16:50:27 -0700369#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700370
371int spi_claim_bus(struct spi_slave *slave)
372{
373 /* Handled by ICH automatically. */
374 return 0;
375}
376
377void spi_release_bus(struct spi_slave *slave)
378{
379 /* Handled by ICH automatically. */
380}
381
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700382typedef struct spi_transaction {
383 const uint8_t *out;
384 uint32_t bytesout;
385 uint8_t *in;
386 uint32_t bytesin;
387 uint8_t type;
388 uint8_t opcode;
389 uint32_t offset;
390} spi_transaction;
391
392static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
393{
394 trans->out += bytes;
395 trans->bytesout -= bytes;
396}
397
398static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
399{
400 trans->in += bytes;
401 trans->bytesin -= bytes;
402}
403
404static void spi_setup_type(spi_transaction *trans)
405{
406 trans->type = 0xFF;
407
408 /* Try to guess spi type from read/write sizes. */
409 if (trans->bytesin == 0) {
410 if (trans->bytesout > 4)
411 /*
412 * If bytesin = 0 and bytesout > 4, we presume this is
413 * a write data operation, which is accompanied by an
414 * address.
415 */
416 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
417 else
418 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
419 return;
420 }
421
422 if (trans->bytesout == 1) { /* and bytesin is > 0 */
423 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
424 return;
425 }
426
427 if (trans->bytesout == 4) { /* and bytesin is > 0 */
428 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
429 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700430
431 /* Fast read command is called with 5 bytes instead of 4 */
432 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
433 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
434 --trans->bytesout;
435 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700436}
437
438static int spi_setup_opcode(spi_transaction *trans)
439{
440 uint16_t optypes;
441 uint8_t opmenu[cntlr.menubytes];
442
443 trans->opcode = trans->out[0];
444 spi_use_out(trans, 1);
445 if (!ichspi_lock) {
446 /* The lock is off, so just use index 0. */
447 writeb_(trans->opcode, cntlr.opmenu);
448 optypes = readw_(cntlr.optype);
449 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
450 writew_(optypes, cntlr.optype);
451 return 0;
452 } else {
453 /* The lock is on. See if what we need is on the menu. */
454 uint8_t optype;
455 uint16_t opcode_index;
456
Duncan Lauriea2f1b952012-08-27 11:10:43 -0700457 /* Write Enable is handled as atomic prefix */
458 if (trans->opcode == SPI_OPCODE_WREN)
459 return 0;
460
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700461 read_reg(cntlr.opmenu, opmenu, sizeof(opmenu));
462 for (opcode_index = 0; opcode_index < cntlr.menubytes;
463 opcode_index++) {
464 if (opmenu[opcode_index] == trans->opcode)
465 break;
466 }
467
468 if (opcode_index == cntlr.menubytes) {
469 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
470 trans->opcode);
471 return -1;
472 }
473
474 optypes = readw_(cntlr.optype);
475 optype = (optypes >> (opcode_index * 2)) & 0x3;
476 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
477 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
478 trans->bytesout >= 3) {
479 /* We guessed wrong earlier. Fix it up. */
480 trans->type = optype;
481 }
482 if (optype != trans->type) {
483 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
484 optype);
485 return -1;
486 }
487 return opcode_index;
488 }
489}
490
491static int spi_setup_offset(spi_transaction *trans)
492{
493 /* Separate the SPI address and data. */
494 switch (trans->type) {
495 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
496 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
497 return 0;
498 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
499 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
500 trans->offset = ((uint32_t)trans->out[0] << 16) |
501 ((uint32_t)trans->out[1] << 8) |
502 ((uint32_t)trans->out[2] << 0);
503 spi_use_out(trans, 3);
504 return 1;
505 default:
506 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
507 return -1;
508 }
509}
510
511/*
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200512 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700513 * below is True) or 0. In case the wait was for the bit(s) to set - write
514 * those bits back, which would cause resetting them.
515 *
516 * Return the last read status value on success or -1 on failure.
517 */
518static int ich_status_poll(u16 bitmask, int wait_til_set)
519{
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200520 int timeout = 600000; /* This will result in 6 seconds */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700521 u16 status = 0;
522
523 while (timeout--) {
524 status = readw_(cntlr.status);
525 if (wait_til_set ^ ((status & bitmask) == 0)) {
526 if (wait_til_set)
527 writew_((status & bitmask), cntlr.status);
528 return status;
529 }
530 udelay(10);
531 }
532
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200533 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700534 status, bitmask);
535 return -1;
536}
537
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100538static int spi_is_multichip (void)
539{
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100540 if (!(cntlr.hsfs & HSFS_FDV))
541 return 0;
542 return !!((cntlr.flmap0 >> 8) & 3);
543}
544
Kyösti Mälkki11104952014-06-29 16:17:33 +0300545unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len)
546{
547 return min(cntlr.databytes, buf_len);
548}
549
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700550int spi_xfer(struct spi_slave *slave, const void *dout,
Gabe Black93d9f922014-03-27 21:52:43 -0700551 unsigned int bytesout, void *din, unsigned int bytesin)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700552{
553 uint16_t control;
554 int16_t opcode_index;
555 int with_address;
556 int status;
557
558 spi_transaction trans = {
Gabe Black93d9f922014-03-27 21:52:43 -0700559 dout, bytesout,
560 din, bytesin,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700561 0xff, 0xff, 0
562 };
563
564 /* There has to always at least be an opcode. */
Gabe Black93d9f922014-03-27 21:52:43 -0700565 if (!bytesout || !dout) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700566 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
567 return -1;
568 }
569 /* Make sure if we read something we have a place to put it. */
Gabe Black93d9f922014-03-27 21:52:43 -0700570 if (bytesin != 0 && !din) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700571 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
572 return -1;
573 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700574
575 if (ich_status_poll(SPIS_SCIP, 0) == -1)
576 return -1;
577
578 writew_(SPIS_CDS | SPIS_FCERR, cntlr.status);
579
580 spi_setup_type(&trans);
581 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
582 return -1;
583 if ((with_address = spi_setup_offset(&trans)) < 0)
584 return -1;
585
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700586 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700587 /*
588 * Treat Write Enable as Atomic Pre-Op if possible
589 * in order to prevent the Management Engine from
590 * issuing a transaction between WREN and DATA.
591 */
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700592 if (!ichspi_lock)
593 writew_(trans.opcode, cntlr.preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700594 return 0;
595 }
596
597 /* Preset control fields */
598 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
599
600 /* Issue atomic preop cycle if needed */
601 if (readw_(cntlr.preop))
602 control |= SPIC_ACS;
603
604 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700605 /* SPI addresses are 24 bit only */
606 if (with_address)
607 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
608
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700609 /*
610 * This is a 'no data' command (like Write Enable), its
611 * bitesout size was 1, decremented to zero while executing
612 * spi_setup_opcode() above. Tell the chip to send the
613 * command.
614 */
615 writew_(control, cntlr.control);
616
617 /* wait for the result */
618 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
619 if (status == -1)
620 return -1;
621
622 if (status & SPIS_FCERR) {
623 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
624 return -1;
625 }
626
627 return 0;
628 }
629
630 /*
Paul Menzel94782972013-06-29 11:41:27 +0200631 * Check if this is a write command attempting to transfer more bytes
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700632 * than the controller can handle. Iterations for writes are not
633 * supported here because each SPI write command needs to be preceded
634 * and followed by other SPI commands, and this sequence is controlled
635 * by the SPI chip driver.
636 */
637 if (trans.bytesout > cntlr.databytes) {
638 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
Kyösti Mälkki11104952014-06-29 16:17:33 +0300639 " spi_crop_chunk()?\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700640 return -1;
641 }
642
643 /*
644 * Read or write up to databytes bytes at a time until everything has
645 * been sent.
646 */
647 while (trans.bytesout || trans.bytesin) {
648 uint32_t data_length;
649
650 /* SPI addresses are 24 bit only */
651 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
652
653 if (trans.bytesout)
654 data_length = min(trans.bytesout, cntlr.databytes);
655 else
656 data_length = min(trans.bytesin, cntlr.databytes);
657
658 /* Program data into FDATA0 to N */
659 if (trans.bytesout) {
660 write_reg(trans.out, cntlr.data, data_length);
661 spi_use_out(&trans, data_length);
662 if (with_address)
663 trans.offset += data_length;
664 }
665
666 /* Add proper control fields' values */
667 control &= ~((cntlr.databytes - 1) << 8);
668 control |= SPIC_DS;
669 control |= (data_length - 1) << 8;
670
671 /* write it */
672 writew_(control, cntlr.control);
673
674 /* Wait for Cycle Done Status or Flash Cycle Error. */
675 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
676 if (status == -1)
677 return -1;
678
679 if (status & SPIS_FCERR) {
680 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
681 return -1;
682 }
683
684 if (trans.bytesin) {
685 read_reg(cntlr.data, trans.in, data_length);
686 spi_use_in(&trans, data_length);
687 if (with_address)
688 trans.offset += data_length;
689 }
690 }
691
692 /* Clear atomic preop now that xfer is done */
693 writew_(0, cntlr.preop);
694
695 return 0;
696}
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100697
698/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
699static void ich_hwseq_set_addr(uint32_t addr)
700{
701 uint32_t addr_old = readl_(&cntlr.ich9_spi->faddr) & ~0x01FFFFFF;
702 writel_((addr & 0x01FFFFFF) | addr_old, &cntlr.ich9_spi->faddr);
703}
704
705/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
706 Resets all error flags in HSFS.
707 Returns 0 if the cycle completes successfully without errors within
708 timeout us, 1 on errors. */
709static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
710 unsigned int len)
711{
712 uint16_t hsfs;
713 uint32_t addr;
714
715 timeout /= 8; /* scale timeout duration to counter */
716 while ((((hsfs = readw_(&cntlr.ich9_spi->hsfs)) &
717 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
718 --timeout) {
719 udelay(8);
720 }
721 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
722
723 if (!timeout) {
724 uint16_t hsfc;
725 addr = readl_(&cntlr.ich9_spi->faddr) & 0x01FFFFFF;
726 hsfc = readw_(&cntlr.ich9_spi->hsfc);
727 printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and "
728 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
729 addr, addr + len - 1, addr, len - 1,
730 hsfc, hsfs);
731 return 1;
732 }
733
734 if (hsfs & HSFS_FCERR) {
735 uint16_t hsfc;
736 addr = readl_(&cntlr.ich9_spi->faddr) & 0x01FFFFFF;
737 hsfc = readw_(&cntlr.ich9_spi->hsfc);
738 printk(BIOS_ERR, "Transaction error between offset 0x%08x and "
739 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
740 addr, addr + len - 1, addr, len - 1,
741 hsfc, hsfs);
742 return 1;
743 }
744 return 0;
745}
746
747
748static int ich_hwseq_erase(struct spi_flash *flash, u32 offset, size_t len)
749{
750 u32 start, end, erase_size;
751 int ret;
752 uint16_t hsfc;
753 uint16_t timeout = 1000 * 60;
754
755 erase_size = flash->sector_size;
756 if (offset % erase_size || len % erase_size) {
757 printk(BIOS_ERR, "SF: Erase offset/length not multiple of erase size\n");
758 return -1;
759 }
760
761 flash->spi->rw = SPI_WRITE_FLAG;
762 ret = spi_claim_bus(flash->spi);
763 if (ret) {
764 printk(BIOS_ERR, "SF: Unable to claim SPI bus\n");
765 return ret;
766 }
767
768 start = offset;
769 end = start + len;
770
771 while (offset < end) {
772 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
773 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
774
775 ich_hwseq_set_addr(offset);
776
777 offset += erase_size;
778
779 hsfc = readw_(&cntlr.ich9_spi->hsfc);
780 hsfc &= ~HSFC_FCYCLE; /* clear operation */
781 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
782 hsfc |= HSFC_FGO; /* start */
783 writew_(hsfc, &cntlr.ich9_spi->hsfc);
784 if (ich_hwseq_wait_for_cycle_complete(timeout, len))
785 {
786 printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size);
787 ret = -1;
788 goto out;
789 }
790 }
791
792 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
793
794out:
795 spi_release_bus(flash->spi);
796 return ret;
797}
798
799static void ich_read_data(uint8_t *data, int len)
800{
801 int i;
802 uint32_t temp32 = 0;
803
804 for (i = 0; i < len; i++) {
805 if ((i % 4) == 0)
806 temp32 = readl_(cntlr.data + i);
807
808 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
809 }
810}
811
812static int ich_hwseq_read(struct spi_flash *flash,
813 u32 addr, size_t len, void *buf)
814{
815 uint16_t hsfc;
816 uint16_t timeout = 100 * 60;
817 uint8_t block_len;
818
819 if (addr + len > flash->size) {
820 printk (BIOS_ERR,
821 "Attempt to read %x-%x which is out of chip\n",
822 (unsigned) addr,
823 (unsigned) addr+(unsigned) len);
824 return -1;
825 }
826
827 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
828 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
829
830 while (len > 0) {
831 block_len = min(len, cntlr.databytes);
832 if (block_len > (~addr & 0xff))
833 block_len = (~addr & 0xff) + 1;
834 ich_hwseq_set_addr(addr);
835 hsfc = readw_(&cntlr.ich9_spi->hsfc);
836 hsfc &= ~HSFC_FCYCLE; /* set read operation */
837 hsfc &= ~HSFC_FDBC; /* clear byte count */
838 /* set byte count */
839 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
840 hsfc |= HSFC_FGO; /* start */
841 writew_(hsfc, &cntlr.ich9_spi->hsfc);
842
843 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
844 return 1;
845 ich_read_data(buf, block_len);
846 addr += block_len;
847 buf += block_len;
848 len -= block_len;
849 }
850 return 0;
851}
852
853/* Fill len bytes from the data array into the fdata/spid registers.
854 *
855 * Note that using len > flash->pgm->spi.max_data_write will trash the registers
856 * following the data registers.
857 */
858static void ich_fill_data(const uint8_t *data, int len)
859{
860 uint32_t temp32 = 0;
861 int i;
862
863 if (len <= 0)
864 return;
865
866 for (i = 0; i < len; i++) {
867 if ((i % 4) == 0)
868 temp32 = 0;
869
870 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
871
872 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
873 writel_(temp32, cntlr.data + (i - (i % 4)));
874 }
875 i--;
876 if ((i % 4) != 3) /* Write remaining data to regs. */
877 writel_(temp32, cntlr.data + (i - (i % 4)));
878}
879
880static int ich_hwseq_write(struct spi_flash *flash,
881 u32 addr, size_t len, const void *buf)
882{
883 uint16_t hsfc;
884 uint16_t timeout = 100 * 60;
885 uint8_t block_len;
886 uint32_t start = addr;
887
888 if (addr + len > flash->size) {
889 printk (BIOS_ERR,
890 "Attempt to write 0x%x-0x%x which is out of chip\n",
891 (unsigned)addr, (unsigned) (addr+len));
892 return -1;
893 }
894
895 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
896 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
897
898 while (len > 0) {
899 block_len = min(len, cntlr.databytes);
900 if (block_len > (~addr & 0xff))
901 block_len = (~addr & 0xff) + 1;
902
903 ich_hwseq_set_addr(addr);
904
905 ich_fill_data(buf, block_len);
906 hsfc = readw_(&cntlr.ich9_spi->hsfc);
907 hsfc &= ~HSFC_FCYCLE; /* clear operation */
908 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
909 hsfc &= ~HSFC_FDBC; /* clear byte count */
910 /* set byte count */
911 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
912 hsfc |= HSFC_FGO; /* start */
913 writew_(hsfc, &cntlr.ich9_spi->hsfc);
914
915 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
916 {
917 printk (BIOS_ERR, "SF: write failure at %x\n",
918 addr);
919 return -1;
920 }
921 addr += block_len;
922 buf += block_len;
923 len -= block_len;
924 }
925 printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n",
926 (unsigned) (addr - start), start);
927 return 0;
928}
929
930
931static struct spi_flash *spi_flash_hwseq(struct spi_slave *spi)
932{
933 struct spi_flash *flash = NULL;
934 uint32_t flcomp;
935
936 flash = malloc(sizeof(*flash));
937 if (!flash) {
938 printk(BIOS_WARNING, "SF: Failed to allocate memory\n");
939 return NULL;
940 }
941
942 flash->spi = spi;
943 flash->name = "Opaque HW-sequencing";
944
945 flash->write = ich_hwseq_write;
946 flash->erase = ich_hwseq_erase;
947 flash->read = ich_hwseq_read;
948 ich_hwseq_set_addr (0);
949 switch ((cntlr.hsfs >> 3) & 3)
950 {
951 case 0:
952 flash->sector_size = 256;
953 break;
954 case 1:
955 flash->sector_size = 4096;
956 break;
957 case 2:
958 flash->sector_size = 8192;
959 break;
960 case 3:
961 flash->sector_size = 65536;
962 break;
963 }
964
965 writel_ (0x1000, &cntlr.ich9_spi->fdoc);
966 flcomp = readl_(&cntlr.ich9_spi->fdod);
967
968 flash->size = 1 << (19 + (flcomp & 7));
969
970 if ((cntlr.hsfs & HSFS_FDV) && ((cntlr.flmap0 >> 8) & 3))
971 flash->size += 1 << (19 + ((flcomp >> 3) & 7));
972 printk (BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
973
974 return flash;
975}