blob: fe3bf2a6a0edc56dfd005f67f87a08224eb372b1 [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
Werner Zehf13a6f92018-11-14 10:55:52 +01005 * Copyright (C) 2018 Siemens AG
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07006 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07007 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but without any warranty; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070016 */
17
18/* This file is derived from the flashrom project. */
Arthur Heymans02c99712018-03-28 18:49:27 +020019#include <arch/early_variables.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070020#include <stdint.h>
21#include <stdlib.h>
22#include <string.h>
David Hendricksf2612a12014-04-13 16:27:02 -070023#include <bootstate.h>
Furquan Shaikhde705fa2017-04-19 19:27:28 -070024#include <commonlib/helpers.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070025#include <delay.h>
26#include <arch/io.h>
27#include <console/console.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070028#include <device/pci_ids.h>
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010029#include <device/pci.h>
30#include <spi_flash.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070031
Zheng Bao600784e2013-02-07 17:30:23 +080032#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070033
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +010034#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
35#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
36#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
37#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
38
39
Duncan Laurie181bbdd2012-06-23 16:53:57 -070040#ifdef __SMM__
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>
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
Arthur Heymans02c99712018-03-28 18:49:27 +020073static int g_ichspi_lock CAR_GLOBAL = 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070074
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];
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +010085 uint32_t pbr[3];
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;
Stefan Tauner327205d2018-08-26 13:53:16 +0200124 uint32_t flcomp;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100125 uint32_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700126
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100127 ich9_spi_regs *ich9_spi;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700128 uint8_t *opmenu;
129 int menubytes;
130 uint16_t *preop;
131 uint16_t *optype;
132 uint32_t *addr;
133 uint8_t *data;
134 unsigned databytes;
135 uint8_t *status;
136 uint16_t *control;
137 uint32_t *bbar;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100138 uint32_t *fpr;
139 uint8_t fpr_max;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700140} ich_spi_controller;
141
Arthur Heymans02c99712018-03-28 18:49:27 +0200142static ich_spi_controller g_cntlr CAR_GLOBAL;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700143
144enum {
145 SPIS_SCIP = 0x0001,
146 SPIS_GRANT = 0x0002,
147 SPIS_CDS = 0x0004,
148 SPIS_FCERR = 0x0008,
149 SSFS_AEL = 0x0010,
150 SPIS_LOCK = 0x8000,
151 SPIS_RESERVED_MASK = 0x7ff0,
152 SSFS_RESERVED_MASK = 0x7fe2
153};
154
155enum {
156 SPIC_SCGO = 0x000002,
157 SPIC_ACS = 0x000004,
158 SPIC_SPOP = 0x000008,
159 SPIC_DBC = 0x003f00,
160 SPIC_DS = 0x004000,
161 SPIC_SME = 0x008000,
162 SSFC_SCF_MASK = 0x070000,
163 SSFC_RESERVED = 0xf80000
164};
165
166enum {
167 HSFS_FDONE = 0x0001,
168 HSFS_FCERR = 0x0002,
169 HSFS_AEL = 0x0004,
170 HSFS_BERASE_MASK = 0x0018,
171 HSFS_BERASE_SHIFT = 3,
172 HSFS_SCIP = 0x0020,
173 HSFS_FDOPSS = 0x2000,
174 HSFS_FDV = 0x4000,
175 HSFS_FLOCKDN = 0x8000
176};
177
178enum {
179 HSFC_FGO = 0x0001,
180 HSFC_FCYCLE_MASK = 0x0006,
181 HSFC_FCYCLE_SHIFT = 1,
182 HSFC_FDBC_MASK = 0x3f00,
183 HSFC_FDBC_SHIFT = 8,
184 HSFC_FSMIE = 0x8000
185};
186
187enum {
188 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
189 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
190 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
191 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
192};
193
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600194#if IS_ENABLED(CONFIG_DEBUG_SPI_FLASH)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700195
196static u8 readb_(const void *addr)
197{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800198 u8 v = read8(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100199
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700200 printk(BIOS_DEBUG, "read %2.2x from %4.4x\n",
201 v, ((unsigned) addr & 0xffff) - 0xf020);
202 return v;
203}
204
205static u16 readw_(const void *addr)
206{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800207 u16 v = read16(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100208
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700209 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
210 v, ((unsigned) addr & 0xffff) - 0xf020);
211 return v;
212}
213
214static u32 readl_(const void *addr)
215{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800216 u32 v = read32(addr);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100217
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{
Arthur Heymans02c99712018-03-28 18:49:27 +0200287 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700288 const uint32_t bbar_mask = 0x00ffff00;
289 uint32_t ichspi_bbar;
290
291 minaddr &= bbar_mask;
Arthur Heymans02c99712018-03-28 18:49:27 +0200292 ichspi_bbar = readl_(cntlr->bbar) & ~bbar_mask;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700293 ichspi_bbar |= minaddr;
Arthur Heymans02c99712018-03-28 18:49:27 +0200294 writel_(ichspi_bbar, cntlr->bbar);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700295}
296
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700297void spi_init(void)
298{
Arthur Heymans02c99712018-03-28 18:49:27 +0200299 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700300 uint8_t *rcrb; /* Root Complex Register Block */
301 uint32_t rcba; /* Root Complex Base Address */
302 uint8_t bios_cntl;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100303 ich9_spi_regs *ich9_spi;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200304 ich7_spi_regs *ich7_spi;
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100305 uint16_t hsfs;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700306
Arthur Heymans02c99712018-03-28 18:49:27 +0200307#ifdef __SIMPLE_DEVICE__
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200308 pci_devfn_t dev = PCI_DEV(0, 31, 0);
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700309#else
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200310 struct device *dev = dev_find_slot(0, PCI_DEVFN(31, 0));
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700311#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700312
313 pci_read_config_dword(dev, 0xf0, &rcba);
314 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
315 rcrb = (uint8_t *)(rcba & 0xffffc000);
Arthur Heymansc88e3702017-08-20 20:50:17 +0200316 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX)) {
317 ich7_spi = (ich7_spi_regs *)(rcrb + 0x3020);
Arthur Heymans02c99712018-03-28 18:49:27 +0200318 cntlr->opmenu = ich7_spi->opmenu;
319 cntlr->menubytes = sizeof(ich7_spi->opmenu);
320 cntlr->optype = &ich7_spi->optype;
321 cntlr->addr = &ich7_spi->spia;
322 cntlr->data = (uint8_t *)ich7_spi->spid;
323 cntlr->databytes = sizeof(ich7_spi->spid);
324 cntlr->status = (uint8_t *)&ich7_spi->spis;
325 car_set_var(g_ichspi_lock, readw_(&ich7_spi->spis) & HSFS_FLOCKDN);
326 cntlr->control = &ich7_spi->spic;
327 cntlr->bbar = &ich7_spi->bbar;
328 cntlr->preop = &ich7_spi->preop;
329 cntlr->fpr = &ich7_spi->pbr[0];
330 cntlr->fpr_max = 3;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200331 } else {
332 ich9_spi = (ich9_spi_regs *)(rcrb + 0x3800);
Arthur Heymans02c99712018-03-28 18:49:27 +0200333 cntlr->ich9_spi = ich9_spi;
Arthur Heymansc88e3702017-08-20 20:50:17 +0200334 hsfs = readw_(&ich9_spi->hsfs);
Arthur Heymans02c99712018-03-28 18:49:27 +0200335 car_set_var(g_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;
347 cntlr->fpr = &ich9_spi->pr[0];
348 cntlr->fpr_max = 5;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700349
Arthur Heymans02c99712018-03-28 18:49:27 +0200350 if (cntlr->hsfs & HSFS_FDV) {
Patrick Georgic88828d2018-11-26 10:42:59 +0100351 writel_(4, &ich9_spi->fdoc);
Arthur Heymans02c99712018-03-28 18:49:27 +0200352 cntlr->flmap0 = readl_(&ich9_spi->fdod);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100353 writel_(0x1000, &ich9_spi->fdoc);
Stefan Tauner327205d2018-08-26 13:53:16 +0200354 cntlr->flcomp = readl_(&ich9_spi->fdod);
Arthur Heymansc88e3702017-08-20 20:50:17 +0200355 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700356 }
357
358 ich_set_bbar(0);
359
360 /* Disable the BIOS write protect so write commands are allowed. */
361 pci_read_config_byte(dev, 0xdc, &bios_cntl);
Vladimir Serbinenko42c4a9d2014-02-16 17:13:19 +0100362 /* Deassert SMM BIOS Write Protect Disable. */
363 bios_cntl &= ~(1 << 5);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700364 pci_write_config_byte(dev, 0xdc, bios_cntl | 0x1);
365}
Aaron Durbin4d3de7e2015-09-02 17:34:04 -0500366
David Hendricksf2612a12014-04-13 16:27:02 -0700367static void spi_init_cb(void *unused)
368{
369 spi_init();
370}
371
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500372BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700373
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700374typedef struct spi_transaction {
375 const uint8_t *out;
376 uint32_t bytesout;
377 uint8_t *in;
378 uint32_t bytesin;
379 uint8_t type;
380 uint8_t opcode;
381 uint32_t offset;
382} spi_transaction;
383
384static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
385{
386 trans->out += bytes;
387 trans->bytesout -= bytes;
388}
389
390static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
391{
392 trans->in += bytes;
393 trans->bytesin -= bytes;
394}
395
396static void spi_setup_type(spi_transaction *trans)
397{
398 trans->type = 0xFF;
399
400 /* Try to guess spi type from read/write sizes. */
401 if (trans->bytesin == 0) {
402 if (trans->bytesout > 4)
403 /*
404 * If bytesin = 0 and bytesout > 4, we presume this is
405 * a write data operation, which is accompanied by an
406 * address.
407 */
408 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
409 else
410 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
411 return;
412 }
413
414 if (trans->bytesout == 1) { /* and bytesin is > 0 */
415 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
416 return;
417 }
418
419 if (trans->bytesout == 4) { /* and bytesin is > 0 */
420 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
421 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700422
423 /* Fast read command is called with 5 bytes instead of 4 */
424 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
425 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
426 --trans->bytesout;
427 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700428}
429
430static int spi_setup_opcode(spi_transaction *trans)
431{
Arthur Heymans02c99712018-03-28 18:49:27 +0200432 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700433 uint16_t optypes;
Arthur Heymans02c99712018-03-28 18:49:27 +0200434 uint8_t opmenu[cntlr->menubytes];
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700435
436 trans->opcode = trans->out[0];
437 spi_use_out(trans, 1);
Arthur Heymans02c99712018-03-28 18:49:27 +0200438 if (!car_get_var(g_ichspi_lock)) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700439 /* The lock is off, so just use index 0. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200440 writeb_(trans->opcode, cntlr->opmenu);
441 optypes = readw_(cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700442 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
Arthur Heymans02c99712018-03-28 18:49:27 +0200443 writew_(optypes, cntlr->optype);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700444 return 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700445 }
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100446
447 /* The lock is on. See if what we need is on the menu. */
448 uint8_t optype;
449 uint16_t opcode_index;
450
451 /* Write Enable is handled as atomic prefix */
452 if (trans->opcode == SPI_OPCODE_WREN)
453 return 0;
454
455 read_reg(cntlr->opmenu, opmenu, sizeof(opmenu));
456 for (opcode_index = 0; opcode_index < cntlr->menubytes;
457 opcode_index++) {
458 if (opmenu[opcode_index] == trans->opcode)
459 break;
460 }
461
462 if (opcode_index == cntlr->menubytes) {
463 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
464 trans->opcode);
465 return -1;
466 }
467
468 optypes = readw_(cntlr->optype);
469 optype = (optypes >> (opcode_index * 2)) & 0x3;
470 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
471 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
472 trans->bytesout >= 3) {
473 /* We guessed wrong earlier. Fix it up. */
474 trans->type = optype;
475 }
476 if (optype != trans->type) {
477 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
478 optype);
479 return -1;
480 }
481 return opcode_index;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700482}
483
484static int spi_setup_offset(spi_transaction *trans)
485{
486 /* Separate the SPI address and data. */
487 switch (trans->type) {
488 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
489 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
490 return 0;
491 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
492 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
493 trans->offset = ((uint32_t)trans->out[0] << 16) |
494 ((uint32_t)trans->out[1] << 8) |
495 ((uint32_t)trans->out[2] << 0);
496 spi_use_out(trans, 3);
497 return 1;
498 default:
499 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
500 return -1;
501 }
502}
503
504/*
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200505 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700506 * below is True) or 0. In case the wait was for the bit(s) to set - write
507 * those bits back, which would cause resetting them.
508 *
509 * Return the last read status value on success or -1 on failure.
510 */
511static int ich_status_poll(u16 bitmask, int wait_til_set)
512{
Arthur Heymans02c99712018-03-28 18:49:27 +0200513 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200514 int timeout = 600000; /* This will result in 6 seconds */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700515 u16 status = 0;
516
517 while (timeout--) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200518 status = readw_(cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700519 if (wait_til_set ^ ((status & bitmask) == 0)) {
520 if (wait_til_set)
Arthur Heymans02c99712018-03-28 18:49:27 +0200521 writew_((status & bitmask), cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700522 return status;
523 }
524 udelay(10);
525 }
526
Philipp Deppenwiese93643e32014-10-17 16:10:32 +0200527 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, bitmask %x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700528 status, bitmask);
529 return -1;
530}
531
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100532static int spi_is_multichip(void)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100533{
Arthur Heymans02c99712018-03-28 18:49:27 +0200534 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
535 if (!(cntlr->hsfs & HSFS_FDV))
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100536 return 0;
Arthur Heymans02c99712018-03-28 18:49:27 +0200537 return !!((cntlr->flmap0 >> 8) & 3);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100538}
539
Furquan Shaikh94f86992016-12-01 07:12:32 -0800540static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800541 size_t bytesout, void *din, size_t bytesin)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700542{
Arthur Heymans02c99712018-03-28 18:49:27 +0200543 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700544 uint16_t control;
545 int16_t opcode_index;
546 int with_address;
547 int status;
548
549 spi_transaction trans = {
Gabe Black93d9f922014-03-27 21:52:43 -0700550 dout, bytesout,
551 din, bytesin,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700552 0xff, 0xff, 0
553 };
554
555 /* There has to always at least be an opcode. */
Gabe Black93d9f922014-03-27 21:52:43 -0700556 if (!bytesout || !dout) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700557 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
558 return -1;
559 }
560 /* Make sure if we read something we have a place to put it. */
Gabe Black93d9f922014-03-27 21:52:43 -0700561 if (bytesin != 0 && !din) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700562 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
563 return -1;
564 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700565
566 if (ich_status_poll(SPIS_SCIP, 0) == -1)
567 return -1;
568
Arthur Heymans02c99712018-03-28 18:49:27 +0200569 writew_(SPIS_CDS | SPIS_FCERR, cntlr->status);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700570
571 spi_setup_type(&trans);
572 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
573 return -1;
574 if ((with_address = spi_setup_offset(&trans)) < 0)
575 return -1;
576
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700577 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700578 /*
579 * Treat Write Enable as Atomic Pre-Op if possible
580 * in order to prevent the Management Engine from
581 * issuing a transaction between WREN and DATA.
582 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200583 if (!car_get_var(g_ichspi_lock))
584 writew_(trans.opcode, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700585 return 0;
586 }
587
588 /* Preset control fields */
589 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
590
591 /* Issue atomic preop cycle if needed */
Arthur Heymans02c99712018-03-28 18:49:27 +0200592 if (readw_(cntlr->preop))
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700593 control |= SPIC_ACS;
594
595 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700596 /* SPI addresses are 24 bit only */
597 if (with_address)
Arthur Heymans02c99712018-03-28 18:49:27 +0200598 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700599
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700600 /*
601 * This is a 'no data' command (like Write Enable), its
602 * bitesout size was 1, decremented to zero while executing
603 * spi_setup_opcode() above. Tell the chip to send the
604 * command.
605 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200606 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700607
608 /* wait for the result */
609 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
610 if (status == -1)
611 return -1;
612
613 if (status & SPIS_FCERR) {
614 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
615 return -1;
616 }
617
Werner Zehf13a6f92018-11-14 10:55:52 +0100618 goto spi_xfer_exit;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700619 }
620
621 /*
Paul Menzel94782972013-06-29 11:41:27 +0200622 * Check if this is a write command attempting to transfer more bytes
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700623 * than the controller can handle. Iterations for writes are not
624 * supported here because each SPI write command needs to be preceded
625 * and followed by other SPI commands, and this sequence is controlled
626 * by the SPI chip driver.
627 */
Arthur Heymans02c99712018-03-28 18:49:27 +0200628 if (trans.bytesout > cntlr->databytes) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700629 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
Kyösti Mälkki11104952014-06-29 16:17:33 +0300630 " spi_crop_chunk()?\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700631 return -1;
632 }
633
634 /*
635 * Read or write up to databytes bytes at a time until everything has
636 * been sent.
637 */
638 while (trans.bytesout || trans.bytesin) {
639 uint32_t data_length;
640
641 /* SPI addresses are 24 bit only */
Arthur Heymans02c99712018-03-28 18:49:27 +0200642 writel_(trans.offset & 0x00FFFFFF, cntlr->addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700643
644 if (trans.bytesout)
Arthur Heymans02c99712018-03-28 18:49:27 +0200645 data_length = min(trans.bytesout, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700646 else
Arthur Heymans02c99712018-03-28 18:49:27 +0200647 data_length = min(trans.bytesin, cntlr->databytes);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700648
649 /* Program data into FDATA0 to N */
650 if (trans.bytesout) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200651 write_reg(trans.out, cntlr->data, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700652 spi_use_out(&trans, data_length);
653 if (with_address)
654 trans.offset += data_length;
655 }
656
657 /* Add proper control fields' values */
Arthur Heymans02c99712018-03-28 18:49:27 +0200658 control &= ~((cntlr->databytes - 1) << 8);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700659 control |= SPIC_DS;
660 control |= (data_length - 1) << 8;
661
662 /* write it */
Arthur Heymans02c99712018-03-28 18:49:27 +0200663 writew_(control, cntlr->control);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700664
665 /* Wait for Cycle Done Status or Flash Cycle Error. */
666 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
667 if (status == -1)
668 return -1;
669
670 if (status & SPIS_FCERR) {
671 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
672 return -1;
673 }
674
675 if (trans.bytesin) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200676 read_reg(cntlr->data, trans.in, data_length);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700677 spi_use_in(&trans, data_length);
678 if (with_address)
679 trans.offset += data_length;
680 }
681 }
682
Werner Zehf13a6f92018-11-14 10:55:52 +0100683spi_xfer_exit:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700684 /* Clear atomic preop now that xfer is done */
Arthur Heymans02c99712018-03-28 18:49:27 +0200685 writew_(0, cntlr->preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700686
687 return 0;
688}
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100689
690/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
691static void ich_hwseq_set_addr(uint32_t addr)
692{
Arthur Heymans02c99712018-03-28 18:49:27 +0200693 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
694 uint32_t addr_old = readl_(&cntlr->ich9_spi->faddr) & ~0x01FFFFFF;
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100695
Arthur Heymans02c99712018-03-28 18:49:27 +0200696 writel_((addr & 0x01FFFFFF) | addr_old, &cntlr->ich9_spi->faddr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100697}
698
699/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
700 Resets all error flags in HSFS.
701 Returns 0 if the cycle completes successfully without errors within
702 timeout us, 1 on errors. */
703static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
704 unsigned int len)
705{
Arthur Heymans02c99712018-03-28 18:49:27 +0200706 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100707 uint16_t hsfs;
708 uint32_t addr;
709
710 timeout /= 8; /* scale timeout duration to counter */
Arthur Heymans02c99712018-03-28 18:49:27 +0200711 while ((((hsfs = readw_(&cntlr->ich9_spi->hsfs)) &
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100712 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
713 --timeout) {
714 udelay(8);
715 }
Arthur Heymans02c99712018-03-28 18:49:27 +0200716 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100717
718 if (!timeout) {
719 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200720 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
721 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100722 printk(BIOS_ERR, "Transaction timeout between offset 0x%08x and "
723 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
724 addr, addr + len - 1, addr, len - 1,
725 hsfc, hsfs);
726 return 1;
727 }
728
729 if (hsfs & HSFS_FCERR) {
730 uint16_t hsfc;
Arthur Heymans02c99712018-03-28 18:49:27 +0200731 addr = readl_(&cntlr->ich9_spi->faddr) & 0x01FFFFFF;
732 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100733 printk(BIOS_ERR, "Transaction error between offset 0x%08x and "
734 "0x%08x (= 0x%08x + %d) HSFC=%x HSFS=%x!\n",
735 addr, addr + len - 1, addr, len - 1,
736 hsfc, hsfs);
737 return 1;
738 }
739 return 0;
740}
741
742
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800743static int ich_hwseq_erase(const struct spi_flash *flash, u32 offset,
744 size_t len)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100745{
Arthur Heymans02c99712018-03-28 18:49:27 +0200746 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100747 u32 start, end, erase_size;
748 int ret;
749 uint16_t hsfc;
750 uint16_t timeout = 1000 * 60;
751
752 erase_size = flash->sector_size;
753 if (offset % erase_size || len % erase_size) {
754 printk(BIOS_ERR, "SF: Erase offset/length not multiple of erase size\n");
755 return -1;
756 }
757
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800758 ret = spi_claim_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100759 if (ret) {
760 printk(BIOS_ERR, "SF: Unable to claim SPI bus\n");
761 return ret;
762 }
763
764 start = offset;
765 end = start + len;
766
767 while (offset < end) {
768 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
Arthur Heymans02c99712018-03-28 18:49:27 +0200769 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100770
771 ich_hwseq_set_addr(offset);
772
773 offset += erase_size;
774
Arthur Heymans02c99712018-03-28 18:49:27 +0200775 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100776 hsfc &= ~HSFC_FCYCLE; /* clear operation */
777 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
778 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200779 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100780 if (ich_hwseq_wait_for_cycle_complete(timeout, len)) {
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100781 printk(BIOS_ERR, "SF: Erase failed at %x\n", offset - erase_size);
782 ret = -1;
783 goto out;
784 }
785 }
786
787 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
788
789out:
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800790 spi_release_bus(&flash->spi);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100791 return ret;
792}
793
794static void ich_read_data(uint8_t *data, int len)
795{
Arthur Heymans02c99712018-03-28 18:49:27 +0200796 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100797 int i;
798 uint32_t temp32 = 0;
799
800 for (i = 0; i < len; i++) {
801 if ((i % 4) == 0)
Arthur Heymans02c99712018-03-28 18:49:27 +0200802 temp32 = readl_(cntlr->data + i);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100803
804 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
805 }
806}
807
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800808static int ich_hwseq_read(const struct spi_flash *flash, u32 addr, size_t len,
809 void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100810{
Arthur Heymans02c99712018-03-28 18:49:27 +0200811 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100812 uint16_t hsfc;
813 uint16_t timeout = 100 * 60;
814 uint8_t block_len;
815
816 if (addr + len > flash->size) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100817 printk(BIOS_ERR,
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100818 "Attempt to read %x-%x which is out of chip\n",
819 (unsigned) addr,
820 (unsigned) addr+(unsigned) len);
821 return -1;
822 }
823
824 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200825 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100826
827 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200828 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100829 if (block_len > (~addr & 0xff))
830 block_len = (~addr & 0xff) + 1;
831 ich_hwseq_set_addr(addr);
Arthur Heymans02c99712018-03-28 18:49:27 +0200832 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100833 hsfc &= ~HSFC_FCYCLE; /* set read operation */
834 hsfc &= ~HSFC_FDBC; /* clear byte count */
835 /* set byte count */
836 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
837 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200838 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100839
840 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
841 return 1;
842 ich_read_data(buf, block_len);
843 addr += block_len;
844 buf += block_len;
845 len -= block_len;
846 }
847 return 0;
848}
849
850/* Fill len bytes from the data array into the fdata/spid registers.
851 *
852 * Note that using len > flash->pgm->spi.max_data_write will trash the registers
853 * following the data registers.
854 */
855static void ich_fill_data(const uint8_t *data, int len)
856{
Arthur Heymans02c99712018-03-28 18:49:27 +0200857 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100858 uint32_t temp32 = 0;
859 int i;
860
861 if (len <= 0)
862 return;
863
864 for (i = 0; i < len; i++) {
865 if ((i % 4) == 0)
866 temp32 = 0;
867
868 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
869
870 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200871 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100872 }
873 i--;
874 if ((i % 4) != 3) /* Write remaining data to regs. */
Arthur Heymans02c99712018-03-28 18:49:27 +0200875 writel_(temp32, cntlr->data + (i - (i % 4)));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100876}
877
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800878static int ich_hwseq_write(const struct spi_flash *flash, u32 addr, size_t len,
879 const void *buf)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100880{
Arthur Heymans02c99712018-03-28 18:49:27 +0200881 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100882 uint16_t hsfc;
883 uint16_t timeout = 100 * 60;
884 uint8_t block_len;
885 uint32_t start = addr;
886
887 if (addr + len > flash->size) {
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100888 printk(BIOS_ERR,
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100889 "Attempt to write 0x%x-0x%x which is out of chip\n",
890 (unsigned)addr, (unsigned) (addr+len));
891 return -1;
892 }
893
894 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
Arthur Heymans02c99712018-03-28 18:49:27 +0200895 writew_(readw_(&cntlr->ich9_spi->hsfs), &cntlr->ich9_spi->hsfs);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100896
897 while (len > 0) {
Arthur Heymans02c99712018-03-28 18:49:27 +0200898 block_len = min(len, cntlr->databytes);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100899 if (block_len > (~addr & 0xff))
900 block_len = (~addr & 0xff) + 1;
901
902 ich_hwseq_set_addr(addr);
903
904 ich_fill_data(buf, block_len);
Arthur Heymans02c99712018-03-28 18:49:27 +0200905 hsfc = readw_(&cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100906 hsfc &= ~HSFC_FCYCLE; /* clear operation */
907 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
908 hsfc &= ~HSFC_FDBC; /* clear byte count */
909 /* set byte count */
910 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
911 hsfc |= HSFC_FGO; /* start */
Arthur Heymans02c99712018-03-28 18:49:27 +0200912 writew_(hsfc, &cntlr->ich9_spi->hsfc);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100913
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100914 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len)) {
915 printk(BIOS_ERR, "SF: write failure at %x\n",
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100916 addr);
917 return -1;
918 }
919 addr += block_len;
920 buf += block_len;
921 len -= block_len;
922 }
923 printk(BIOS_DEBUG, "SF: Successfully written %u bytes @ %#x\n",
924 (unsigned) (addr - start), start);
925 return 0;
926}
927
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700928static const struct spi_flash_ops spi_flash_ops = {
929 .read = ich_hwseq_read,
930 .write = ich_hwseq_write,
931 .erase = ich_hwseq_erase,
932};
933
Furquan Shaikha1491572017-05-17 19:14:06 -0700934static int spi_flash_programmer_probe(const struct spi_slave *spi,
935 struct spi_flash *flash)
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100936{
Arthur Heymans02c99712018-03-28 18:49:27 +0200937 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100938
Arthur Heymansc88e3702017-08-20 20:50:17 +0200939 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX))
940 return spi_flash_generic_probe(spi, flash);
941
Furquan Shaikha1491572017-05-17 19:14:06 -0700942 /* Try generic probing first if spi_is_multichip returns 0. */
943 if (!spi_is_multichip() && !spi_flash_generic_probe(spi, flash))
944 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100945
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800946 memcpy(&flash->spi, spi, sizeof(*spi));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100947 flash->name = "Opaque HW-sequencing";
948
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100949 ich_hwseq_set_addr(0);
950 switch ((cntlr->hsfs >> 3) & 3) {
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100951 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
Stefan Tauner327205d2018-08-26 13:53:16 +0200965 flash->size = 1 << (19 + (cntlr->flcomp & 7));
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100966
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700967 flash->ops = &spi_flash_ops;
968
Arthur Heymans02c99712018-03-28 18:49:27 +0200969 if ((cntlr->hsfs & HSFS_FDV) && ((cntlr->flmap0 >> 8) & 3))
Stefan Tauner327205d2018-08-26 13:53:16 +0200970 flash->size += 1 << (19 + ((cntlr->flcomp >> 3) & 7));
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100971 printk(BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100972
Furquan Shaikh30221b42017-05-15 14:35:15 -0700973 return 0;
Vladimir Serbinenkof3c7a9c2014-01-04 21:00:38 +0100974}
Furquan Shaikha1491572017-05-17 19:14:06 -0700975
Aaron Durbin851dde82018-04-19 21:15:25 -0600976static int xfer_vectors(const struct spi_slave *slave,
977 struct spi_op vectors[], size_t count)
978{
979 return spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer);
980}
981
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100982#define SPI_FPR_SHIFT 12
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100983#define ICH7_SPI_FPR_MASK 0xfff
984#define ICH9_SPI_FPR_MASK 0x1fff
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100985#define SPI_FPR_BASE_SHIFT 0
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100986#define ICH7_SPI_FPR_LIMIT_SHIFT 12
987#define ICH9_SPI_FPR_LIMIT_SHIFT 16
988#define ICH9_SPI_FPR_RPE (1 << 15) /* Read Protect */
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100989#define SPI_FPR_WPE (1 << 31) /* Write Protect */
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100990
991static u32 spi_fpr(u32 base, u32 limit)
992{
993 u32 ret;
994 u32 mask, limit_shift;
Elyes HAOUASad19c2f2018-11-26 15:57:30 +0100995
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +0100996 if (IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX)) {
997 mask = ICH7_SPI_FPR_MASK;
998 limit_shift = ICH7_SPI_FPR_LIMIT_SHIFT;
999 } else {
1000 mask = ICH9_SPI_FPR_MASK;
1001 limit_shift = ICH9_SPI_FPR_LIMIT_SHIFT;
1002 }
1003 ret = ((limit >> SPI_FPR_SHIFT) & mask) << limit_shift;
1004 ret |= ((base >> SPI_FPR_SHIFT) & mask) << SPI_FPR_BASE_SHIFT;
1005 return ret;
1006}
1007
1008/*
1009 * Protect range of SPI flash defined by [start, start+size-1] using Flash
1010 * Protected Range (FPR) register if available.
1011 * Returns 0 on success, -1 on failure of programming fpr registers.
1012 */
1013static int spi_flash_protect(const struct spi_flash *flash,
1014 const struct region *region)
1015{
Arthur Heymans02c99712018-03-28 18:49:27 +02001016 ich_spi_controller *cntlr = car_get_var_ptr(&g_cntlr);
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001017 u32 start = region_offset(region);
1018 u32 end = start + region_sz(region) - 1;
1019 u32 reg;
1020 int fpr;
1021 uint32_t *fpr_base;
1022
Arthur Heymans02c99712018-03-28 18:49:27 +02001023 fpr_base = cntlr->fpr;
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001024
1025 /* Find first empty FPR */
Arthur Heymans02c99712018-03-28 18:49:27 +02001026 for (fpr = 0; fpr < cntlr->fpr_max; fpr++) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001027 reg = read32(&fpr_base[fpr]);
1028 if (reg == 0)
1029 break;
1030 }
1031
Arthur Heymans02c99712018-03-28 18:49:27 +02001032 if (fpr == cntlr->fpr_max) {
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001033 printk(BIOS_ERR, "ERROR: No SPI FPR free!\n");
1034 return -1;
1035 }
1036
1037 /* Set protected range base and limit */
1038 reg = spi_fpr(start, end) | SPI_FPR_WPE;
1039
1040 /* Set the FPR register and verify it is protected */
1041 write32(&fpr_base[fpr], reg);
1042 reg = read32(&fpr_base[fpr]);
1043 if (!(reg & SPI_FPR_WPE)) {
1044 printk(BIOS_ERR, "ERROR: Unable to set SPI FPR %d\n", fpr);
1045 return -1;
1046 }
1047
1048 printk(BIOS_INFO, "%s: FPR %d is enabled for range 0x%08x-0x%08x\n",
1049 __func__, fpr, start, end);
1050 return 0;
1051}
1052
Furquan Shaikha1491572017-05-17 19:14:06 -07001053static const struct spi_ctrlr spi_ctrlr = {
Aaron Durbin851dde82018-04-19 21:15:25 -06001054 .xfer_vector = xfer_vectors,
Furquan Shaikha1491572017-05-17 19:14:06 -07001055 .max_xfer_size = member_size(ich9_spi_regs, fdata),
1056 .flash_probe = spi_flash_programmer_probe,
Arthur Heymans11fcb2bc2018-01-07 20:46:31 +01001057 .flash_protect = spi_flash_protect,
Furquan Shaikha1491572017-05-17 19:14:06 -07001058};
1059
Furquan Shaikh2cd03f12017-05-18 14:58:32 -07001060const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
1061 {
1062 .ctrlr = &spi_ctrlr,
1063 .bus_start = 0,
1064 .bus_end = 0,
1065 },
1066};
1067
1068const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);