blob: 13db224a97b718975008301d35798bea2e247f98 [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>
Furquan Shaikhde705fa2017-04-19 19:27:28 -070022#include <commonlib/helpers.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070023#include <delay.h>
24#include <arch/io.h>
25#include <console/console.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070026#include <device/pci_ids.h>
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010027#include <device/pci.h>
28#include <spi_flash.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070029
Zheng Bao600784e2013-02-07 17:30:23 +080030#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070031
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010032#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
33#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
34#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
35#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
36
37
Duncan Laurie181bbdd2012-06-23 16:53:57 -070038#ifdef __SMM__
Kyösti Mälkkib4a45dc2013-07-26 08:53:59 +030039#include <arch/io.h>
Duncan Laurie181bbdd2012-06-23 16:53:57 -070040#define pci_read_config_byte(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030041 *(targ) = pci_read_config8(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070042#define pci_read_config_word(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030043 *(targ) = pci_read_config16(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070044#define pci_read_config_dword(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030045 *(targ) = pci_read_config32(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070046#define pci_write_config_byte(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030047 pci_write_config8(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070048#define pci_write_config_word(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030049 pci_write_config16(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070050#define pci_write_config_dword(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030051 pci_write_config32(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070052#else /* !__SMM__ */
53#include <device/device.h>
54#include <device/pci.h>
Duncan Laurie181bbdd2012-06-23 16:53:57 -070055#define pci_read_config_byte(dev, reg, targ)\
56 *(targ) = pci_read_config8(dev, reg)
57#define pci_read_config_word(dev, reg, targ)\
58 *(targ) = pci_read_config16(dev, reg)
59#define pci_read_config_dword(dev, reg, targ)\
60 *(targ) = pci_read_config32(dev, reg)
61#define pci_write_config_byte(dev, reg, val)\
62 pci_write_config8(dev, reg, val)
63#define pci_write_config_word(dev, reg, val)\
64 pci_write_config16(dev, reg, val)
65#define pci_write_config_dword(dev, reg, val)\
66 pci_write_config32(dev, reg, val)
67#endif /* !__SMM__ */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070068
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010069static int spi_is_multichip(void);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010070
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
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700289void spi_init(void)
290{
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700291 uint8_t *rcrb; /* Root Complex Register Block */
292 uint32_t rcba; /* Root Complex Base Address */
293 uint8_t bios_cntl;
Stefan Reinauer0c32c972012-07-10 13:26:59 -0700294 device_t dev;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100295 ich9_spi_regs *ich9_spi;
296 uint16_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700297
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700298#ifdef __SMM__
299 dev = PCI_DEV(0, 31, 0);
300#else
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700301 dev = dev_find_slot(0, PCI_DEVFN(31, 0));
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700302#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700303
304 pci_read_config_dword(dev, 0xf0, &rcba);
305 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
306 rcrb = (uint8_t *)(rcba & 0xffffc000);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100307 ich9_spi = (ich9_spi_regs *)(rcrb + 0x3800);
308 cntlr.ich9_spi = ich9_spi;
309 hsfs = readw_(&ich9_spi->hsfs);
310 ichspi_lock = hsfs & HSFS_FLOCKDN;
311 cntlr.hsfs = hsfs;
312 cntlr.opmenu = ich9_spi->opmenu;
313 cntlr.menubytes = sizeof(ich9_spi->opmenu);
314 cntlr.optype = &ich9_spi->optype;
315 cntlr.addr = &ich9_spi->faddr;
316 cntlr.data = (uint8_t *)ich9_spi->fdata;
317 cntlr.databytes = sizeof(ich9_spi->fdata);
318 cntlr.status = &ich9_spi->ssfs;
319 cntlr.control = (uint16_t *)ich9_spi->ssfc;
320 cntlr.bbar = &ich9_spi->bbar;
321 cntlr.preop = &ich9_spi->preop;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700322
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100323 if (cntlr.hsfs & HSFS_FDV)
324 {
325 writel_ (4, &ich9_spi->fdoc);
326 cntlr.flmap0 = readl_(&ich9_spi->fdod);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700327 }
328
329 ich_set_bbar(0);
330
331 /* Disable the BIOS write protect so write commands are allowed. */
332 pci_read_config_byte(dev, 0xdc, &bios_cntl);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100333 /* Deassert SMM BIOS Write Protect Disable. */
334 bios_cntl &= ~(1 << 5);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700335 pci_write_config_byte(dev, 0xdc, bios_cntl | 0x1);
336}
Aaron Durbin4d3de7e2015-09-02 17:34:04 -0500337
David Hendricksf2612a12014-04-13 16:27:02 -0700338static void spi_init_cb(void *unused)
339{
340 spi_init();
341}
342
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500343BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700344
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700345typedef struct spi_transaction {
346 const uint8_t *out;
347 uint32_t bytesout;
348 uint8_t *in;
349 uint32_t bytesin;
350 uint8_t type;
351 uint8_t opcode;
352 uint32_t offset;
353} spi_transaction;
354
355static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
356{
357 trans->out += bytes;
358 trans->bytesout -= bytes;
359}
360
361static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
362{
363 trans->in += bytes;
364 trans->bytesin -= bytes;
365}
366
367static void spi_setup_type(spi_transaction *trans)
368{
369 trans->type = 0xFF;
370
371 /* Try to guess spi type from read/write sizes. */
372 if (trans->bytesin == 0) {
373 if (trans->bytesout > 4)
374 /*
375 * If bytesin = 0 and bytesout > 4, we presume this is
376 * a write data operation, which is accompanied by an
377 * address.
378 */
379 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
380 else
381 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
382 return;
383 }
384
385 if (trans->bytesout == 1) { /* and bytesin is > 0 */
386 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
387 return;
388 }
389
390 if (trans->bytesout == 4) { /* and bytesin is > 0 */
391 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
392 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700393
394 /* Fast read command is called with 5 bytes instead of 4 */
395 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
396 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
397 --trans->bytesout;
398 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700399}
400
401static int spi_setup_opcode(spi_transaction *trans)
402{
403 uint16_t optypes;
404 uint8_t opmenu[cntlr.menubytes];
405
406 trans->opcode = trans->out[0];
407 spi_use_out(trans, 1);
408 if (!ichspi_lock) {
409 /* The lock is off, so just use index 0. */
410 writeb_(trans->opcode, cntlr.opmenu);
411 optypes = readw_(cntlr.optype);
412 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
413 writew_(optypes, cntlr.optype);
414 return 0;
415 } else {
416 /* The lock is on. See if what we need is on the menu. */
417 uint8_t optype;
418 uint16_t opcode_index;
419
Duncan Lauriea2f1b952012-08-27 11:10:43 -0700420 /* Write Enable is handled as atomic prefix */
421 if (trans->opcode == SPI_OPCODE_WREN)
422 return 0;
423
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700424 read_reg(cntlr.opmenu, opmenu, sizeof(opmenu));
425 for (opcode_index = 0; opcode_index < cntlr.menubytes;
426 opcode_index++) {
427 if (opmenu[opcode_index] == trans->opcode)
428 break;
429 }
430
431 if (opcode_index == cntlr.menubytes) {
432 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
433 trans->opcode);
434 return -1;
435 }
436
437 optypes = readw_(cntlr.optype);
438 optype = (optypes >> (opcode_index * 2)) & 0x3;
439 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
440 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
441 trans->bytesout >= 3) {
442 /* We guessed wrong earlier. Fix it up. */
443 trans->type = optype;
444 }
445 if (optype != trans->type) {
446 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
447 optype);
448 return -1;
449 }
450 return opcode_index;
451 }
452}
453
454static int spi_setup_offset(spi_transaction *trans)
455{
456 /* Separate the SPI address and data. */
457 switch (trans->type) {
458 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
459 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
460 return 0;
461 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
462 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
463 trans->offset = ((uint32_t)trans->out[0] << 16) |
464 ((uint32_t)trans->out[1] << 8) |
465 ((uint32_t)trans->out[2] << 0);
466 spi_use_out(trans, 3);
467 return 1;
468 default:
469 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
470 return -1;
471 }
472}
473
474/*
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200475 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700476 * below is True) or 0. In case the wait was for the bit(s) to set - write
477 * those bits back, which would cause resetting them.
478 *
479 * Return the last read status value on success or -1 on failure.
480 */
481static int ich_status_poll(u16 bitmask, int wait_til_set)
482{
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200483 int timeout = 600000; /* This will result in 6 seconds */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700484 u16 status = 0;
485
486 while (timeout--) {
487 status = readw_(cntlr.status);
488 if (wait_til_set ^ ((status & bitmask) == 0)) {
489 if (wait_til_set)
490 writew_((status & bitmask), cntlr.status);
491 return status;
492 }
493 udelay(10);
494 }
495
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200496 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700497 status, bitmask);
498 return -1;
499}
500
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100501static int spi_is_multichip (void)
502{
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100503 if (!(cntlr.hsfs & HSFS_FDV))
504 return 0;
505 return !!((cntlr.flmap0 >> 8) & 3);
506}
507
Furquan Shaikh94f86992016-12-01 07:12:32 -0800508static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800509 size_t bytesout, void *din, size_t bytesin)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700510{
511 uint16_t control;
512 int16_t opcode_index;
513 int with_address;
514 int status;
515
516 spi_transaction trans = {
Gabe Black93d9f922014-03-27 21:52:43 -0700517 dout, bytesout,
518 din, bytesin,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700519 0xff, 0xff, 0
520 };
521
522 /* There has to always at least be an opcode. */
Gabe Black93d9f922014-03-27 21:52:43 -0700523 if (!bytesout || !dout) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700524 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
525 return -1;
526 }
527 /* Make sure if we read something we have a place to put it. */
Gabe Black93d9f922014-03-27 21:52:43 -0700528 if (bytesin != 0 && !din) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700529 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
530 return -1;
531 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700532
533 if (ich_status_poll(SPIS_SCIP, 0) == -1)
534 return -1;
535
536 writew_(SPIS_CDS | SPIS_FCERR, cntlr.status);
537
538 spi_setup_type(&trans);
539 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
540 return -1;
541 if ((with_address = spi_setup_offset(&trans)) < 0)
542 return -1;
543
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700544 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700545 /*
546 * Treat Write Enable as Atomic Pre-Op if possible
547 * in order to prevent the Management Engine from
548 * issuing a transaction between WREN and DATA.
549 */
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700550 if (!ichspi_lock)
551 writew_(trans.opcode, cntlr.preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700552 return 0;
553 }
554
555 /* Preset control fields */
556 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
557
558 /* Issue atomic preop cycle if needed */
559 if (readw_(cntlr.preop))
560 control |= SPIC_ACS;
561
562 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700563 /* SPI addresses are 24 bit only */
564 if (with_address)
565 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
566
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700567 /*
568 * This is a 'no data' command (like Write Enable), its
569 * bitesout size was 1, decremented to zero while executing
570 * spi_setup_opcode() above. Tell the chip to send the
571 * command.
572 */
573 writew_(control, cntlr.control);
574
575 /* wait for the result */
576 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
577 if (status == -1)
578 return -1;
579
580 if (status & SPIS_FCERR) {
581 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
582 return -1;
583 }
584
585 return 0;
586 }
587
588 /*
Paul Menzel94782972013-06-29 11:41:27 +0200589 * Check if this is a write command attempting to transfer more bytes
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700590 * than the controller can handle. Iterations for writes are not
591 * supported here because each SPI write command needs to be preceded
592 * and followed by other SPI commands, and this sequence is controlled
593 * by the SPI chip driver.
594 */
595 if (trans.bytesout > cntlr.databytes) {
596 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
Kyösti Mälkki11104952014-06-29 16:17:33 +0300597 " spi_crop_chunk()?\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700598 return -1;
599 }
600
601 /*
602 * Read or write up to databytes bytes at a time until everything has
603 * been sent.
604 */
605 while (trans.bytesout || trans.bytesin) {
606 uint32_t data_length;
607
608 /* SPI addresses are 24 bit only */
609 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
610
611 if (trans.bytesout)
612 data_length = min(trans.bytesout, cntlr.databytes);
613 else
614 data_length = min(trans.bytesin, cntlr.databytes);
615
616 /* Program data into FDATA0 to N */
617 if (trans.bytesout) {
618 write_reg(trans.out, cntlr.data, data_length);
619 spi_use_out(&trans, data_length);
620 if (with_address)
621 trans.offset += data_length;
622 }
623
624 /* Add proper control fields' values */
625 control &= ~((cntlr.databytes - 1) << 8);
626 control |= SPIC_DS;
627 control |= (data_length - 1) << 8;
628
629 /* write it */
630 writew_(control, cntlr.control);
631
632 /* Wait for Cycle Done Status or Flash Cycle Error. */
633 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
634 if (status == -1)
635 return -1;
636
637 if (status & SPIS_FCERR) {
638 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
639 return -1;
640 }
641
642 if (trans.bytesin) {
643 read_reg(cntlr.data, trans.in, data_length);
644 spi_use_in(&trans, data_length);
645 if (with_address)
646 trans.offset += data_length;
647 }
648 }
649
650 /* Clear atomic preop now that xfer is done */
651 writew_(0, cntlr.preop);
652
653 return 0;
654}
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100655
656/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
657static void ich_hwseq_set_addr(uint32_t addr)
658{
659 uint32_t addr_old = readl_(&cntlr.ich9_spi->faddr) & ~0x01FFFFFF;
660 writel_((addr & 0x01FFFFFF) | addr_old, &cntlr.ich9_spi->faddr);
661}
662
663/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
664 Resets all error flags in HSFS.
665 Returns 0 if the cycle completes successfully without errors within
666 timeout us, 1 on errors. */
667static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
668 unsigned int len)
669{
670 uint16_t hsfs;
671 uint32_t addr;
672
673 timeout /= 8; /* scale timeout duration to counter */
674 while ((((hsfs = readw_(&cntlr.ich9_spi->hsfs)) &
675 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
676 --timeout) {
677 udelay(8);
678 }
679 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
680
681 if (!timeout) {
682 uint16_t hsfc;
683 addr = readl_(&cntlr.ich9_spi->faddr) & 0x01FFFFFF;
684 hsfc = readw_(&cntlr.ich9_spi->hsfc);
685 printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and "
686 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
687 addr, addr + len - 1, addr, len - 1,
688 hsfc, hsfs);
689 return 1;
690 }
691
692 if (hsfs & HSFS_FCERR) {
693 uint16_t hsfc;
694 addr = readl_(&cntlr.ich9_spi->faddr) & 0x01FFFFFF;
695 hsfc = readw_(&cntlr.ich9_spi->hsfc);
696 printk(BIOS_ERR, "Transaction error between offset 0x%08x and "
697 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
698 addr, addr + len - 1, addr, len - 1,
699 hsfc, hsfs);
700 return 1;
701 }
702 return 0;
703}
704
705
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800706static int ich_hwseq_erase(const struct spi_flash *flash, u32 offset,
707 size_t len)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100708{
709 u32 start, end, erase_size;
710 int ret;
711 uint16_t hsfc;
712 uint16_t timeout = 1000 * 60;
713
714 erase_size = flash->sector_size;
715 if (offset % erase_size || len % erase_size) {
716 printk(BIOS_ERR, "SF: Erase offset/length not multiple of erase size\n");
717 return -1;
718 }
719
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800720 ret = spi_claim_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100721 if (ret) {
722 printk(BIOS_ERR, "SF: Unable to claim SPI bus\n");
723 return ret;
724 }
725
726 start = offset;
727 end = start + len;
728
729 while (offset < end) {
730 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
731 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
732
733 ich_hwseq_set_addr(offset);
734
735 offset += erase_size;
736
737 hsfc = readw_(&cntlr.ich9_spi->hsfc);
738 hsfc &= ~HSFC_FCYCLE; /* clear operation */
739 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
740 hsfc |= HSFC_FGO; /* start */
741 writew_(hsfc, &cntlr.ich9_spi->hsfc);
742 if (ich_hwseq_wait_for_cycle_complete(timeout, len))
743 {
744 printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size);
745 ret = -1;
746 goto out;
747 }
748 }
749
750 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
751
752out:
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800753 spi_release_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100754 return ret;
755}
756
757static void ich_read_data(uint8_t *data, int len)
758{
759 int i;
760 uint32_t temp32 = 0;
761
762 for (i = 0; i < len; i++) {
763 if ((i % 4) == 0)
764 temp32 = readl_(cntlr.data + i);
765
766 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
767 }
768}
769
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800770static int ich_hwseq_read(const struct spi_flash *flash, u32 addr, size_t len,
771 void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100772{
773 uint16_t hsfc;
774 uint16_t timeout = 100 * 60;
775 uint8_t block_len;
776
777 if (addr + len > flash->size) {
778 printk (BIOS_ERR,
779 "Attempt to read %x-%x which is out of chip\n",
780 (unsigned) addr,
781 (unsigned) addr+(unsigned) len);
782 return -1;
783 }
784
785 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
786 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
787
788 while (len > 0) {
789 block_len = min(len, cntlr.databytes);
790 if (block_len > (~addr & 0xff))
791 block_len = (~addr & 0xff) + 1;
792 ich_hwseq_set_addr(addr);
793 hsfc = readw_(&cntlr.ich9_spi->hsfc);
794 hsfc &= ~HSFC_FCYCLE; /* set read operation */
795 hsfc &= ~HSFC_FDBC; /* clear byte count */
796 /* set byte count */
797 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
798 hsfc |= HSFC_FGO; /* start */
799 writew_(hsfc, &cntlr.ich9_spi->hsfc);
800
801 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
802 return 1;
803 ich_read_data(buf, block_len);
804 addr += block_len;
805 buf += block_len;
806 len -= block_len;
807 }
808 return 0;
809}
810
811/* Fill len bytes from the data array into the fdata/spid registers.
812 *
813 * Note that using len > flash->pgm->spi.max_data_write will trash the registers
814 * following the data registers.
815 */
816static void ich_fill_data(const uint8_t *data, int len)
817{
818 uint32_t temp32 = 0;
819 int i;
820
821 if (len <= 0)
822 return;
823
824 for (i = 0; i < len; i++) {
825 if ((i % 4) == 0)
826 temp32 = 0;
827
828 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
829
830 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
831 writel_(temp32, cntlr.data + (i - (i % 4)));
832 }
833 i--;
834 if ((i % 4) != 3) /* Write remaining data to regs. */
835 writel_(temp32, cntlr.data + (i - (i % 4)));
836}
837
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800838static int ich_hwseq_write(const struct spi_flash *flash, u32 addr, size_t len,
839 const void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100840{
841 uint16_t hsfc;
842 uint16_t timeout = 100 * 60;
843 uint8_t block_len;
844 uint32_t start = addr;
845
846 if (addr + len > flash->size) {
847 printk (BIOS_ERR,
848 "Attempt to write 0x%x-0x%x which is out of chip\n",
849 (unsigned)addr, (unsigned) (addr+len));
850 return -1;
851 }
852
853 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
854 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
855
856 while (len > 0) {
857 block_len = min(len, cntlr.databytes);
858 if (block_len > (~addr & 0xff))
859 block_len = (~addr & 0xff) + 1;
860
861 ich_hwseq_set_addr(addr);
862
863 ich_fill_data(buf, block_len);
864 hsfc = readw_(&cntlr.ich9_spi->hsfc);
865 hsfc &= ~HSFC_FCYCLE; /* clear operation */
866 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
867 hsfc &= ~HSFC_FDBC; /* clear byte count */
868 /* set byte count */
869 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
870 hsfc |= HSFC_FGO; /* start */
871 writew_(hsfc, &cntlr.ich9_spi->hsfc);
872
873 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
874 {
875 printk (BIOS_ERR, "SF: write failure at %x\n",
876 addr);
877 return -1;
878 }
879 addr += block_len;
880 buf += block_len;
881 len -= block_len;
882 }
883 printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n",
884 (unsigned) (addr - start), start);
885 return 0;
886}
887
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700888static const struct spi_flash_ops spi_flash_ops = {
889 .read = ich_hwseq_read,
890 .write = ich_hwseq_write,
891 .erase = ich_hwseq_erase,
892};
893
Furquan Shaikha1491572017-05-17 19:14:06 -0700894static int spi_flash_programmer_probe(const struct spi_slave *spi,
895 struct spi_flash *flash)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100896{
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100897 uint32_t flcomp;
898
Furquan Shaikha1491572017-05-17 19:14:06 -0700899 /* Try generic probing first if spi_is_multichip returns 0. */
900 if (!spi_is_multichip() && !spi_flash_generic_probe(spi, flash))
901 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100902
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800903 memcpy(&flash->spi, spi, sizeof(*spi));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100904 flash->name = "Opaque HW-sequencing";
905
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100906 ich_hwseq_set_addr (0);
907 switch ((cntlr.hsfs >> 3) & 3)
908 {
909 case 0:
910 flash->sector_size = 256;
911 break;
912 case 1:
913 flash->sector_size = 4096;
914 break;
915 case 2:
916 flash->sector_size = 8192;
917 break;
918 case 3:
919 flash->sector_size = 65536;
920 break;
921 }
922
923 writel_ (0x1000, &cntlr.ich9_spi->fdoc);
924 flcomp = readl_(&cntlr.ich9_spi->fdod);
925
926 flash->size = 1 << (19 + (flcomp & 7));
927
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700928 flash->ops = &spi_flash_ops;
929
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100930 if ((cntlr.hsfs & HSFS_FDV) && ((cntlr.flmap0 >> 8) & 3))
931 flash->size += 1 << (19 + ((flcomp >> 3) & 7));
932 printk (BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
933
Furquan Shaikh30221b42017-05-15 14:35:15 -0700934 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100935}
Furquan Shaikha1491572017-05-17 19:14:06 -0700936
937static const struct spi_ctrlr spi_ctrlr = {
938 .xfer = spi_ctrlr_xfer,
939 .xfer_vector = spi_xfer_two_vectors,
940 .max_xfer_size = member_size(ich9_spi_regs, fdata),
941 .flash_probe = spi_flash_programmer_probe,
942};
943
Furquan Shaikh2cd03f12017-05-18 14:58:32 -0700944const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
945 {
946 .ctrlr = &spi_ctrlr,
947 .bus_start = 0,
948 .bus_end = 0,
949 },
950};
951
952const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);