blob: 8cb5e627d57045790ecc903b92a00876332cf48a [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>
Stefan Reinauer6a001132017-07-13 02:20:27 +020021#include <compiler.h>
David Hendricksf2612a12014-04-13 16:27:02 -070022#include <bootstate.h>
Furquan Shaikhde705fa2017-04-19 19:27:28 -070023#include <commonlib/helpers.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070024#include <delay.h>
25#include <arch/io.h>
26#include <console/console.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070027#include <device/pci_ids.h>
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010028#include <device/pci.h>
29#include <spi_flash.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070030
Zheng Bao600784e2013-02-07 17:30:23 +080031#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070032
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010033#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
34#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
35#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
36#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
37
38
Duncan Laurie181bbdd2012-06-23 16:53:57 -070039#ifdef __SMM__
Kyösti Mälkkib4a45dc2013-07-26 08:53:59 +030040#include <arch/io.h>
Duncan Laurie181bbdd2012-06-23 16:53:57 -070041#define pci_read_config_byte(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030042 *(targ) = pci_read_config8(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070043#define pci_read_config_word(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030044 *(targ) = pci_read_config16(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070045#define pci_read_config_dword(dev, reg, targ)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030046 *(targ) = pci_read_config32(dev, reg)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070047#define pci_write_config_byte(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030048 pci_write_config8(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070049#define pci_write_config_word(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030050 pci_write_config16(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070051#define pci_write_config_dword(dev, reg, val)\
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030052 pci_write_config32(dev, reg, val)
Duncan Laurie181bbdd2012-06-23 16:53:57 -070053#else /* !__SMM__ */
54#include <device/device.h>
55#include <device/pci.h>
Duncan Laurie181bbdd2012-06-23 16:53:57 -070056#define pci_read_config_byte(dev, reg, targ)\
57 *(targ) = pci_read_config8(dev, reg)
58#define pci_read_config_word(dev, reg, targ)\
59 *(targ) = pci_read_config16(dev, reg)
60#define pci_read_config_dword(dev, reg, targ)\
61 *(targ) = pci_read_config32(dev, reg)
62#define pci_write_config_byte(dev, reg, val)\
63 pci_write_config8(dev, reg, val)
64#define pci_write_config_word(dev, reg, val)\
65 pci_write_config16(dev, reg, val)
66#define pci_write_config_dword(dev, reg, val)\
67 pci_write_config32(dev, reg, val)
68#endif /* !__SMM__ */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070069
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010070static int spi_is_multichip(void);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010071
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070072typedef struct spi_slave ich_spi_slave;
73
74static int ichspi_lock = 0;
75
76typedef struct ich7_spi_regs {
77 uint16_t spis;
78 uint16_t spic;
79 uint32_t spia;
80 uint64_t spid[8];
81 uint64_t _pad;
82 uint32_t bbar;
83 uint16_t preop;
84 uint16_t optype;
85 uint8_t opmenu[8];
Stefan Reinauer6a001132017-07-13 02:20:27 +020086} __packed ich7_spi_regs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070087
88typedef struct ich9_spi_regs {
89 uint32_t bfpr;
90 uint16_t hsfs;
91 uint16_t hsfc;
92 uint32_t faddr;
93 uint32_t _reserved0;
94 uint32_t fdata[16];
95 uint32_t frap;
96 uint32_t freg[5];
97 uint32_t _reserved1[3];
98 uint32_t pr[5];
99 uint32_t _reserved2[2];
100 uint8_t ssfs;
101 uint8_t ssfc[3];
102 uint16_t preop;
103 uint16_t optype;
104 uint8_t opmenu[8];
105 uint32_t bbar;
106 uint8_t _reserved3[12];
107 uint32_t fdoc;
108 uint32_t fdod;
109 uint8_t _reserved4[8];
110 uint32_t afc;
111 uint32_t lvscc;
112 uint32_t uvscc;
113 uint8_t _reserved5[4];
114 uint32_t fpb;
115 uint8_t _reserved6[28];
116 uint32_t srdl;
117 uint32_t srdc;
118 uint32_t srd;
Stefan Reinauer6a001132017-07-13 02:20:27 +0200119} __packed ich9_spi_regs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700120
121typedef struct ich_spi_controller {
122 int locked;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100123 uint32_t flmap0;
124 uint32_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700125
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100126 ich9_spi_regs *ich9_spi;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700127 uint8_t *opmenu;
128 int menubytes;
129 uint16_t *preop;
130 uint16_t *optype;
131 uint32_t *addr;
132 uint8_t *data;
133 unsigned databytes;
134 uint8_t *status;
135 uint16_t *control;
136 uint32_t *bbar;
137} ich_spi_controller;
138
139static ich_spi_controller cntlr;
140
141enum {
142 SPIS_SCIP = 0x0001,
143 SPIS_GRANT = 0x0002,
144 SPIS_CDS = 0x0004,
145 SPIS_FCERR = 0x0008,
146 SSFS_AEL = 0x0010,
147 SPIS_LOCK = 0x8000,
148 SPIS_RESERVED_MASK = 0x7ff0,
149 SSFS_RESERVED_MASK = 0x7fe2
150};
151
152enum {
153 SPIC_SCGO = 0x000002,
154 SPIC_ACS = 0x000004,
155 SPIC_SPOP = 0x000008,
156 SPIC_DBC = 0x003f00,
157 SPIC_DS = 0x004000,
158 SPIC_SME = 0x008000,
159 SSFC_SCF_MASK = 0x070000,
160 SSFC_RESERVED = 0xf80000
161};
162
163enum {
164 HSFS_FDONE = 0x0001,
165 HSFS_FCERR = 0x0002,
166 HSFS_AEL = 0x0004,
167 HSFS_BERASE_MASK = 0x0018,
168 HSFS_BERASE_SHIFT = 3,
169 HSFS_SCIP = 0x0020,
170 HSFS_FDOPSS = 0x2000,
171 HSFS_FDV = 0x4000,
172 HSFS_FLOCKDN = 0x8000
173};
174
175enum {
176 HSFC_FGO = 0x0001,
177 HSFC_FCYCLE_MASK = 0x0006,
178 HSFC_FCYCLE_SHIFT = 1,
179 HSFC_FDBC_MASK = 0x3f00,
180 HSFC_FDBC_SHIFT = 8,
181 HSFC_FSMIE = 0x8000
182};
183
184enum {
185 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
186 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
187 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
188 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
189};
190
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600191#if IS_ENABLED(CONFIG_DEBUG_SPI_FLASH)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700192
193static u8 readb_(const void *addr)
194{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800195 u8 v = read8(addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700196 printk(BIOS_DEBUG, "read %2.2x from %4.4x\n",
197 v, ((unsigned) addr & 0xffff) - 0xf020);
198 return v;
199}
200
201static u16 readw_(const void *addr)
202{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800203 u16 v = read16(addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700204 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
205 v, ((unsigned) addr & 0xffff) - 0xf020);
206 return v;
207}
208
209static u32 readl_(const void *addr)
210{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800211 u32 v = read32(addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700212 printk(BIOS_DEBUG, "read %8.8x from %4.4x\n",
213 v, ((unsigned) addr & 0xffff) - 0xf020);
214 return v;
215}
216
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800217static void writeb_(u8 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700218{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800219 write8(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700220 printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n",
221 b, ((unsigned) addr & 0xffff) - 0xf020);
222}
223
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800224static void writew_(u16 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700225{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800226 write16(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700227 printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n",
228 b, ((unsigned) addr & 0xffff) - 0xf020);
229}
230
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800231static void writel_(u32 b, void *addr)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700232{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800233 write32(addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700234 printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n",
235 b, ((unsigned) addr & 0xffff) - 0xf020);
236}
237
238#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
239
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800240#define readb_(a) read8(a)
241#define readw_(a) read16(a)
242#define readl_(a) read32(a)
243#define writeb_(val, addr) write8(addr, val)
244#define writew_(val, addr) write16(addr, val)
245#define writel_(val, addr) write32(addr, val)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700246
247#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
248
249static void write_reg(const void *value, void *dest, uint32_t size)
250{
251 const uint8_t *bvalue = value;
252 uint8_t *bdest = dest;
253
254 while (size >= 4) {
255 writel_(*(const uint32_t *)bvalue, bdest);
256 bdest += 4; bvalue += 4; size -= 4;
257 }
258 while (size) {
259 writeb_(*bvalue, bdest);
260 bdest++; bvalue++; size--;
261 }
262}
263
264static void read_reg(const void *src, void *value, uint32_t size)
265{
266 const uint8_t *bsrc = src;
267 uint8_t *bvalue = value;
268
269 while (size >= 4) {
270 *(uint32_t *)bvalue = readl_(bsrc);
271 bsrc += 4; bvalue += 4; size -= 4;
272 }
273 while (size) {
274 *bvalue = readb_(bsrc);
275 bsrc++; bvalue++; size--;
276 }
277}
278
279static void ich_set_bbar(uint32_t minaddr)
280{
281 const uint32_t bbar_mask = 0x00ffff00;
282 uint32_t ichspi_bbar;
283
284 minaddr &= bbar_mask;
285 ichspi_bbar = readl_(cntlr.bbar) & ~bbar_mask;
286 ichspi_bbar |= minaddr;
287 writel_(ichspi_bbar, cntlr.bbar);
288}
289
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700290void spi_init(void)
291{
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700292 uint8_t *rcrb; /* Root Complex Register Block */
293 uint32_t rcba; /* Root Complex Base Address */
294 uint8_t bios_cntl;
Stefan Reinauer0c32c972012-07-10 13:26:59 -0700295 device_t dev;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100296 ich9_spi_regs *ich9_spi;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200297 ich7_spi_regs *ich7_spi;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100298 uint16_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700299
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700300#ifdef __SMM__
301 dev = PCI_DEV(0, 31, 0);
302#else
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700303 dev = dev_find_slot(0, PCI_DEVFN(31, 0));
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700304#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700305
306 pci_read_config_dword(dev, 0xf0, &rcba);
307 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
308 rcrb = (uint8_t *)(rcba & 0xffffc000);
Arthur Heymansc88e3702017-08-20 20:50:17 +0200309 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX)) {
310 ich7_spi = (ich7_spi_regs *)(rcrb + 0x3020);
311 cntlr.opmenu = ich7_spi->opmenu;
312 cntlr.menubytes = sizeof(ich7_spi->opmenu);
313 cntlr.optype = &ich7_spi->optype;
314 cntlr.addr = &ich7_spi->spia;
315 cntlr.data = (uint8_t *)ich7_spi->spid;
316 cntlr.databytes = sizeof(ich7_spi->spid);
317 cntlr.status = (uint8_t *)&ich7_spi->spis;
318 ichspi_lock = readw_(&ich7_spi->spis) & HSFS_FLOCKDN;
319 cntlr.control = &ich7_spi->spic;
320 cntlr.bbar = &ich7_spi->bbar;
321 cntlr.preop = &ich7_spi->preop;
322 } else {
323 ich9_spi = (ich9_spi_regs *)(rcrb + 0x3800);
324 cntlr.ich9_spi = ich9_spi;
325 hsfs = readw_(&ich9_spi->hsfs);
326 ichspi_lock = hsfs & HSFS_FLOCKDN;
327 cntlr.hsfs = hsfs;
328 cntlr.opmenu = ich9_spi->opmenu;
329 cntlr.menubytes = sizeof(ich9_spi->opmenu);
330 cntlr.optype = &ich9_spi->optype;
331 cntlr.addr = &ich9_spi->faddr;
332 cntlr.data = (uint8_t *)ich9_spi->fdata;
333 cntlr.databytes = sizeof(ich9_spi->fdata);
334 cntlr.status = &ich9_spi->ssfs;
335 cntlr.control = (uint16_t *)ich9_spi->ssfc;
336 cntlr.bbar = &ich9_spi->bbar;
337 cntlr.preop = &ich9_spi->preop;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700338
Arthur Heymansc88e3702017-08-20 20:50:17 +0200339 if (cntlr.hsfs & HSFS_FDV) {
340 writel_ (4, &ich9_spi->fdoc);
341 cntlr.flmap0 = readl_(&ich9_spi->fdod);
342 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700343 }
344
345 ich_set_bbar(0);
346
347 /* Disable the BIOS write protect so write commands are allowed. */
348 pci_read_config_byte(dev, 0xdc, &bios_cntl);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100349 /* Deassert SMM BIOS Write Protect Disable. */
350 bios_cntl &= ~(1 << 5);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700351 pci_write_config_byte(dev, 0xdc, bios_cntl | 0x1);
352}
Aaron Durbin4d3de7e2015-09-02 17:34:04 -0500353
David Hendricksf2612a12014-04-13 16:27:02 -0700354static void spi_init_cb(void *unused)
355{
356 spi_init();
357}
358
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500359BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700360
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700361typedef struct spi_transaction {
362 const uint8_t *out;
363 uint32_t bytesout;
364 uint8_t *in;
365 uint32_t bytesin;
366 uint8_t type;
367 uint8_t opcode;
368 uint32_t offset;
369} spi_transaction;
370
371static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
372{
373 trans->out += bytes;
374 trans->bytesout -= bytes;
375}
376
377static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
378{
379 trans->in += bytes;
380 trans->bytesin -= bytes;
381}
382
383static void spi_setup_type(spi_transaction *trans)
384{
385 trans->type = 0xFF;
386
387 /* Try to guess spi type from read/write sizes. */
388 if (trans->bytesin == 0) {
389 if (trans->bytesout > 4)
390 /*
391 * If bytesin = 0 and bytesout > 4, we presume this is
392 * a write data operation, which is accompanied by an
393 * address.
394 */
395 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
396 else
397 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
398 return;
399 }
400
401 if (trans->bytesout == 1) { /* and bytesin is > 0 */
402 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
403 return;
404 }
405
406 if (trans->bytesout == 4) { /* and bytesin is > 0 */
407 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
408 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700409
410 /* Fast read command is called with 5 bytes instead of 4 */
411 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
412 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
413 --trans->bytesout;
414 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700415}
416
417static int spi_setup_opcode(spi_transaction *trans)
418{
419 uint16_t optypes;
420 uint8_t opmenu[cntlr.menubytes];
421
422 trans->opcode = trans->out[0];
423 spi_use_out(trans, 1);
424 if (!ichspi_lock) {
425 /* The lock is off, so just use index 0. */
426 writeb_(trans->opcode, cntlr.opmenu);
427 optypes = readw_(cntlr.optype);
428 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
429 writew_(optypes, cntlr.optype);
430 return 0;
431 } else {
432 /* The lock is on. See if what we need is on the menu. */
433 uint8_t optype;
434 uint16_t opcode_index;
435
Duncan Lauriea2f1b952012-08-27 11:10:43 -0700436 /* Write Enable is handled as atomic prefix */
437 if (trans->opcode == SPI_OPCODE_WREN)
438 return 0;
439
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700440 read_reg(cntlr.opmenu, opmenu, sizeof(opmenu));
441 for (opcode_index = 0; opcode_index < cntlr.menubytes;
442 opcode_index++) {
443 if (opmenu[opcode_index] == trans->opcode)
444 break;
445 }
446
447 if (opcode_index == cntlr.menubytes) {
448 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
449 trans->opcode);
450 return -1;
451 }
452
453 optypes = readw_(cntlr.optype);
454 optype = (optypes >> (opcode_index * 2)) & 0x3;
455 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
456 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
457 trans->bytesout >= 3) {
458 /* We guessed wrong earlier. Fix it up. */
459 trans->type = optype;
460 }
461 if (optype != trans->type) {
462 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
463 optype);
464 return -1;
465 }
466 return opcode_index;
467 }
468}
469
470static int spi_setup_offset(spi_transaction *trans)
471{
472 /* Separate the SPI address and data. */
473 switch (trans->type) {
474 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
475 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
476 return 0;
477 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
478 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
479 trans->offset = ((uint32_t)trans->out[0] << 16) |
480 ((uint32_t)trans->out[1] << 8) |
481 ((uint32_t)trans->out[2] << 0);
482 spi_use_out(trans, 3);
483 return 1;
484 default:
485 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
486 return -1;
487 }
488}
489
490/*
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200491 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700492 * below is True) or 0. In case the wait was for the bit(s) to set - write
493 * those bits back, which would cause resetting them.
494 *
495 * Return the last read status value on success or -1 on failure.
496 */
497static int ich_status_poll(u16 bitmask, int wait_til_set)
498{
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200499 int timeout = 600000; /* This will result in 6 seconds */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700500 u16 status = 0;
501
502 while (timeout--) {
503 status = readw_(cntlr.status);
504 if (wait_til_set ^ ((status & bitmask) == 0)) {
505 if (wait_til_set)
506 writew_((status & bitmask), cntlr.status);
507 return status;
508 }
509 udelay(10);
510 }
511
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200512 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700513 status, bitmask);
514 return -1;
515}
516
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100517static int spi_is_multichip (void)
518{
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100519 if (!(cntlr.hsfs & HSFS_FDV))
520 return 0;
521 return !!((cntlr.flmap0 >> 8) & 3);
522}
523
Furquan Shaikh94f86992016-12-01 07:12:32 -0800524static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800525 size_t bytesout, void *din, size_t bytesin)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700526{
527 uint16_t control;
528 int16_t opcode_index;
529 int with_address;
530 int status;
531
532 spi_transaction trans = {
Gabe Black93d9f922014-03-27 21:52:43 -0700533 dout, bytesout,
534 din, bytesin,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700535 0xff, 0xff, 0
536 };
537
538 /* There has to always at least be an opcode. */
Gabe Black93d9f922014-03-27 21:52:43 -0700539 if (!bytesout || !dout) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700540 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
541 return -1;
542 }
543 /* Make sure if we read something we have a place to put it. */
Gabe Black93d9f922014-03-27 21:52:43 -0700544 if (bytesin != 0 && !din) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700545 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
546 return -1;
547 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700548
549 if (ich_status_poll(SPIS_SCIP, 0) == -1)
550 return -1;
551
552 writew_(SPIS_CDS | SPIS_FCERR, cntlr.status);
553
554 spi_setup_type(&trans);
555 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
556 return -1;
557 if ((with_address = spi_setup_offset(&trans)) < 0)
558 return -1;
559
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700560 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700561 /*
562 * Treat Write Enable as Atomic Pre-Op if possible
563 * in order to prevent the Management Engine from
564 * issuing a transaction between WREN and DATA.
565 */
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700566 if (!ichspi_lock)
567 writew_(trans.opcode, cntlr.preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700568 return 0;
569 }
570
571 /* Preset control fields */
572 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
573
574 /* Issue atomic preop cycle if needed */
575 if (readw_(cntlr.preop))
576 control |= SPIC_ACS;
577
578 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700579 /* SPI addresses are 24 bit only */
580 if (with_address)
581 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
582
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700583 /*
584 * This is a 'no data' command (like Write Enable), its
585 * bitesout size was 1, decremented to zero while executing
586 * spi_setup_opcode() above. Tell the chip to send the
587 * command.
588 */
589 writew_(control, cntlr.control);
590
591 /* wait for the result */
592 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
593 if (status == -1)
594 return -1;
595
596 if (status & SPIS_FCERR) {
597 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
598 return -1;
599 }
600
601 return 0;
602 }
603
604 /*
Paul Menzel94782972013-06-29 11:41:27 +0200605 * Check if this is a write command attempting to transfer more bytes
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700606 * than the controller can handle. Iterations for writes are not
607 * supported here because each SPI write command needs to be preceded
608 * and followed by other SPI commands, and this sequence is controlled
609 * by the SPI chip driver.
610 */
611 if (trans.bytesout > cntlr.databytes) {
612 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
Kyösti Mälkki11104952014-06-29 16:17:33 +0300613 " spi_crop_chunk()?\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700614 return -1;
615 }
616
617 /*
618 * Read or write up to databytes bytes at a time until everything has
619 * been sent.
620 */
621 while (trans.bytesout || trans.bytesin) {
622 uint32_t data_length;
623
624 /* SPI addresses are 24 bit only */
625 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
626
627 if (trans.bytesout)
628 data_length = min(trans.bytesout, cntlr.databytes);
629 else
630 data_length = min(trans.bytesin, cntlr.databytes);
631
632 /* Program data into FDATA0 to N */
633 if (trans.bytesout) {
634 write_reg(trans.out, cntlr.data, data_length);
635 spi_use_out(&trans, data_length);
636 if (with_address)
637 trans.offset += data_length;
638 }
639
640 /* Add proper control fields' values */
641 control &= ~((cntlr.databytes - 1) << 8);
642 control |= SPIC_DS;
643 control |= (data_length - 1) << 8;
644
645 /* write it */
646 writew_(control, cntlr.control);
647
648 /* Wait for Cycle Done Status or Flash Cycle Error. */
649 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
650 if (status == -1)
651 return -1;
652
653 if (status & SPIS_FCERR) {
654 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
655 return -1;
656 }
657
658 if (trans.bytesin) {
659 read_reg(cntlr.data, trans.in, data_length);
660 spi_use_in(&trans, data_length);
661 if (with_address)
662 trans.offset += data_length;
663 }
664 }
665
666 /* Clear atomic preop now that xfer is done */
667 writew_(0, cntlr.preop);
668
669 return 0;
670}
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100671
672/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
673static void ich_hwseq_set_addr(uint32_t addr)
674{
675 uint32_t addr_old = readl_(&cntlr.ich9_spi->faddr) & ~0x01FFFFFF;
676 writel_((addr & 0x01FFFFFF) | addr_old, &cntlr.ich9_spi->faddr);
677}
678
679/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
680 Resets all error flags in HSFS.
681 Returns 0 if the cycle completes successfully without errors within
682 timeout us, 1 on errors. */
683static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
684 unsigned int len)
685{
686 uint16_t hsfs;
687 uint32_t addr;
688
689 timeout /= 8; /* scale timeout duration to counter */
690 while ((((hsfs = readw_(&cntlr.ich9_spi->hsfs)) &
691 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
692 --timeout) {
693 udelay(8);
694 }
695 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
696
697 if (!timeout) {
698 uint16_t hsfc;
699 addr = readl_(&cntlr.ich9_spi->faddr) & 0x01FFFFFF;
700 hsfc = readw_(&cntlr.ich9_spi->hsfc);
701 printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and "
702 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
703 addr, addr + len - 1, addr, len - 1,
704 hsfc, hsfs);
705 return 1;
706 }
707
708 if (hsfs & HSFS_FCERR) {
709 uint16_t hsfc;
710 addr = readl_(&cntlr.ich9_spi->faddr) & 0x01FFFFFF;
711 hsfc = readw_(&cntlr.ich9_spi->hsfc);
712 printk(BIOS_ERR, "Transaction error between offset 0x%08x and "
713 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
714 addr, addr + len - 1, addr, len - 1,
715 hsfc, hsfs);
716 return 1;
717 }
718 return 0;
719}
720
721
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800722static int ich_hwseq_erase(const struct spi_flash *flash, u32 offset,
723 size_t len)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100724{
725 u32 start, end, erase_size;
726 int ret;
727 uint16_t hsfc;
728 uint16_t timeout = 1000 * 60;
729
730 erase_size = flash->sector_size;
731 if (offset % erase_size || len % erase_size) {
732 printk(BIOS_ERR, "SF: Erase offset/length not multiple of erase size\n");
733 return -1;
734 }
735
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800736 ret = spi_claim_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100737 if (ret) {
738 printk(BIOS_ERR, "SF: Unable to claim SPI bus\n");
739 return ret;
740 }
741
742 start = offset;
743 end = start + len;
744
745 while (offset < end) {
746 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
747 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
748
749 ich_hwseq_set_addr(offset);
750
751 offset += erase_size;
752
753 hsfc = readw_(&cntlr.ich9_spi->hsfc);
754 hsfc &= ~HSFC_FCYCLE; /* clear operation */
755 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
756 hsfc |= HSFC_FGO; /* start */
757 writew_(hsfc, &cntlr.ich9_spi->hsfc);
758 if (ich_hwseq_wait_for_cycle_complete(timeout, len))
759 {
760 printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size);
761 ret = -1;
762 goto out;
763 }
764 }
765
766 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
767
768out:
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800769 spi_release_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100770 return ret;
771}
772
773static void ich_read_data(uint8_t *data, int len)
774{
775 int i;
776 uint32_t temp32 = 0;
777
778 for (i = 0; i < len; i++) {
779 if ((i % 4) == 0)
780 temp32 = readl_(cntlr.data + i);
781
782 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
783 }
784}
785
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800786static int ich_hwseq_read(const struct spi_flash *flash, u32 addr, size_t len,
787 void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100788{
789 uint16_t hsfc;
790 uint16_t timeout = 100 * 60;
791 uint8_t block_len;
792
793 if (addr + len > flash->size) {
794 printk (BIOS_ERR,
795 "Attempt to read %x-%x which is out of chip\n",
796 (unsigned) addr,
797 (unsigned) addr+(unsigned) len);
798 return -1;
799 }
800
801 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
802 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
803
804 while (len > 0) {
805 block_len = min(len, cntlr.databytes);
806 if (block_len > (~addr & 0xff))
807 block_len = (~addr & 0xff) + 1;
808 ich_hwseq_set_addr(addr);
809 hsfc = readw_(&cntlr.ich9_spi->hsfc);
810 hsfc &= ~HSFC_FCYCLE; /* set read operation */
811 hsfc &= ~HSFC_FDBC; /* clear byte count */
812 /* set byte count */
813 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
814 hsfc |= HSFC_FGO; /* start */
815 writew_(hsfc, &cntlr.ich9_spi->hsfc);
816
817 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
818 return 1;
819 ich_read_data(buf, block_len);
820 addr += block_len;
821 buf += block_len;
822 len -= block_len;
823 }
824 return 0;
825}
826
827/* Fill len bytes from the data array into the fdata/spid registers.
828 *
829 * Note that using len > flash->pgm->spi.max_data_write will trash the registers
830 * following the data registers.
831 */
832static void ich_fill_data(const uint8_t *data, int len)
833{
834 uint32_t temp32 = 0;
835 int i;
836
837 if (len <= 0)
838 return;
839
840 for (i = 0; i < len; i++) {
841 if ((i % 4) == 0)
842 temp32 = 0;
843
844 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
845
846 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
847 writel_(temp32, cntlr.data + (i - (i % 4)));
848 }
849 i--;
850 if ((i % 4) != 3) /* Write remaining data to regs. */
851 writel_(temp32, cntlr.data + (i - (i % 4)));
852}
853
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800854static int ich_hwseq_write(const struct spi_flash *flash, u32 addr, size_t len,
855 const void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100856{
857 uint16_t hsfc;
858 uint16_t timeout = 100 * 60;
859 uint8_t block_len;
860 uint32_t start = addr;
861
862 if (addr + len > flash->size) {
863 printk (BIOS_ERR,
864 "Attempt to write 0x%x-0x%x which is out of chip\n",
865 (unsigned)addr, (unsigned) (addr+len));
866 return -1;
867 }
868
869 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
870 writew_(readw_(&cntlr.ich9_spi->hsfs), &cntlr.ich9_spi->hsfs);
871
872 while (len > 0) {
873 block_len = min(len, cntlr.databytes);
874 if (block_len > (~addr & 0xff))
875 block_len = (~addr & 0xff) + 1;
876
877 ich_hwseq_set_addr(addr);
878
879 ich_fill_data(buf, block_len);
880 hsfc = readw_(&cntlr.ich9_spi->hsfc);
881 hsfc &= ~HSFC_FCYCLE; /* clear operation */
882 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
883 hsfc &= ~HSFC_FDBC; /* clear byte count */
884 /* set byte count */
885 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
886 hsfc |= HSFC_FGO; /* start */
887 writew_(hsfc, &cntlr.ich9_spi->hsfc);
888
889 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
890 {
891 printk (BIOS_ERR, "SF: write failure at %x\n",
892 addr);
893 return -1;
894 }
895 addr += block_len;
896 buf += block_len;
897 len -= block_len;
898 }
899 printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n",
900 (unsigned) (addr - start), start);
901 return 0;
902}
903
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700904static const struct spi_flash_ops spi_flash_ops = {
905 .read = ich_hwseq_read,
906 .write = ich_hwseq_write,
907 .erase = ich_hwseq_erase,
908};
909
Furquan Shaikha1491572017-05-17 19:14:06 -0700910static int spi_flash_programmer_probe(const struct spi_slave *spi,
911 struct spi_flash *flash)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100912{
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100913 uint32_t flcomp;
914
Arthur Heymansc88e3702017-08-20 20:50:17 +0200915 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX))
916 return spi_flash_generic_probe(spi, flash);
917
Furquan Shaikha1491572017-05-17 19:14:06 -0700918 /* Try generic probing first if spi_is_multichip returns 0. */
919 if (!spi_is_multichip() && !spi_flash_generic_probe(spi, flash))
920 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100921
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800922 memcpy(&flash->spi, spi, sizeof(*spi));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100923 flash->name = "Opaque HW-sequencing";
924
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100925 ich_hwseq_set_addr (0);
926 switch ((cntlr.hsfs >> 3) & 3)
927 {
928 case 0:
929 flash->sector_size = 256;
930 break;
931 case 1:
932 flash->sector_size = 4096;
933 break;
934 case 2:
935 flash->sector_size = 8192;
936 break;
937 case 3:
938 flash->sector_size = 65536;
939 break;
940 }
941
942 writel_ (0x1000, &cntlr.ich9_spi->fdoc);
943 flcomp = readl_(&cntlr.ich9_spi->fdod);
944
945 flash->size = 1 << (19 + (flcomp & 7));
946
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700947 flash->ops = &spi_flash_ops;
948
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100949 if ((cntlr.hsfs & HSFS_FDV) && ((cntlr.flmap0 >> 8) & 3))
950 flash->size += 1 << (19 + ((flcomp >> 3) & 7));
951 printk (BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
952
Furquan Shaikh30221b42017-05-15 14:35:15 -0700953 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100954}
Furquan Shaikha1491572017-05-17 19:14:06 -0700955
Aaron Durbin851dde82018-04-19 21:15:25 -0600956static int xfer_vectors(const struct spi_slave *slave,
957 struct spi_op vectors[], size_t count)
958{
959 return spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer);
960}
961
Furquan Shaikha1491572017-05-17 19:14:06 -0700962static const struct spi_ctrlr spi_ctrlr = {
Aaron Durbin851dde82018-04-19 21:15:25 -0600963 .xfer_vector = xfer_vectors,
Furquan Shaikha1491572017-05-17 19:14:06 -0700964 .max_xfer_size = member_size(ich9_spi_regs, fdata),
965 .flash_probe = spi_flash_programmer_probe,
966};
967
Furquan Shaikh2cd03f12017-05-18 14:58:32 -0700968const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
969 {
970 .ctrlr = &spi_ctrlr,
971 .bus_start = 0,
972 .bus_end = 0,
973 },
974};
975
976const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);