blob: afc288a36a0406a32b301c62d2c2ffe8d622e931 [file] [log] [blame]
Lee Leahy77ff0b12015-05-05 15:07:29 -07001/*
2 * Copyright (c) 2013 Google Inc.
Lee Leahy32471722015-04-20 15:20:28 -07003 * Copyright (C) 2015 Intel Corp.
Lee Leahy77ff0b12015-05-05 15:07:29 -07004 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but without any warranty; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Patrick Georgi25509ee2015-03-26 15:17:45 +010020 * Foundation, Inc.
Lee Leahy77ff0b12015-05-05 15:07:29 -070021 */
22
23/* This file is derived from the flashrom project. */
Lee Leahy32471722015-04-20 15:20:28 -070024#include <arch/io.h>
25#include <bootstate.h>
26#include <console/console.h>
27#include <delay.h>
28#include <device/pci_ids.h>
Lee Leahyacb9c0b2015-07-02 11:55:18 -070029#include <rules.h>
Lee Leahy32471722015-04-20 15:20:28 -070030#include <soc/lpc.h>
31#include <soc/pci_devs.h>
32#include <spi_flash.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070033#include <stdint.h>
34#include <stdlib.h>
35#include <string.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070036
Lee Leahyacb9c0b2015-07-02 11:55:18 -070037#if ENV_SMM
Lee Leahy77ff0b12015-05-05 15:07:29 -070038#define pci_read_config_byte(dev, reg, targ)\
39 *(targ) = pci_read_config8(dev, reg)
40#define pci_read_config_word(dev, reg, targ)\
41 *(targ) = pci_read_config16(dev, reg)
42#define pci_read_config_dword(dev, reg, targ)\
43 *(targ) = pci_read_config32(dev, reg)
44#define pci_write_config_byte(dev, reg, val)\
45 pci_write_config8(dev, reg, val)
46#define pci_write_config_word(dev, reg, val)\
47 pci_write_config16(dev, reg, val)
48#define pci_write_config_dword(dev, reg, val)\
49 pci_write_config32(dev, reg, val)
Lee Leahyacb9c0b2015-07-02 11:55:18 -070050#else /* ENV_SMM */
Lee Leahy77ff0b12015-05-05 15:07:29 -070051#include <device/device.h>
52#include <device/pci.h>
53#define pci_read_config_byte(dev, reg, targ)\
54 *(targ) = pci_read_config8(dev, reg)
55#define pci_read_config_word(dev, reg, targ)\
56 *(targ) = pci_read_config16(dev, reg)
57#define pci_read_config_dword(dev, reg, targ)\
58 *(targ) = pci_read_config32(dev, reg)
59#define pci_write_config_byte(dev, reg, val)\
60 pci_write_config8(dev, reg, val)
61#define pci_write_config_word(dev, reg, val)\
62 pci_write_config16(dev, reg, val)
63#define pci_write_config_dword(dev, reg, val)\
64 pci_write_config32(dev, reg, val)
Lee Leahyacb9c0b2015-07-02 11:55:18 -070065#endif /* ENV_SMM */
Lee Leahy77ff0b12015-05-05 15:07:29 -070066
67typedef struct spi_slave ich_spi_slave;
68
69static int ichspi_lock = 0;
70
71typedef struct ich9_spi_regs {
72 uint32_t bfpr;
73 uint16_t hsfs;
74 uint16_t hsfc;
75 uint32_t faddr;
76 uint32_t _reserved0;
77 uint32_t fdata[16];
78 uint32_t frap;
79 uint32_t freg[5];
80 uint32_t _reserved1[3];
81 uint32_t pr[5];
82 uint32_t _reserved2[2];
83 uint8_t ssfs;
84 uint8_t ssfc[3];
85 uint16_t preop;
86 uint16_t optype;
87 uint8_t opmenu[8];
Lee Leahy77ff0b12015-05-05 15:07:29 -070088} __attribute__((packed)) ich9_spi_regs;
89
90typedef struct ich_spi_controller {
91 int locked;
92
93 uint8_t *opmenu;
94 int menubytes;
95 uint16_t *preop;
96 uint16_t *optype;
97 uint32_t *addr;
98 uint8_t *data;
99 unsigned databytes;
100 uint8_t *status;
101 uint16_t *control;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700102} ich_spi_controller;
103
104static ich_spi_controller cntlr;
105
106enum {
107 SPIS_SCIP = 0x0001,
108 SPIS_GRANT = 0x0002,
109 SPIS_CDS = 0x0004,
110 SPIS_FCERR = 0x0008,
111 SSFS_AEL = 0x0010,
112 SPIS_LOCK = 0x8000,
113 SPIS_RESERVED_MASK = 0x7ff0,
114 SSFS_RESERVED_MASK = 0x7fe2
115};
116
117enum {
118 SPIC_SCGO = 0x000002,
119 SPIC_ACS = 0x000004,
120 SPIC_SPOP = 0x000008,
121 SPIC_DBC = 0x003f00,
122 SPIC_DS = 0x004000,
123 SPIC_SME = 0x008000,
124 SSFC_SCF_MASK = 0x070000,
125 SSFC_RESERVED = 0xf80000
126};
127
128enum {
129 HSFS_FDONE = 0x0001,
130 HSFS_FCERR = 0x0002,
131 HSFS_AEL = 0x0004,
132 HSFS_BERASE_MASK = 0x0018,
133 HSFS_BERASE_SHIFT = 3,
134 HSFS_SCIP = 0x0020,
135 HSFS_FDOPSS = 0x2000,
136 HSFS_FDV = 0x4000,
137 HSFS_FLOCKDN = 0x8000
138};
139
140enum {
141 HSFC_FGO = 0x0001,
142 HSFC_FCYCLE_MASK = 0x0006,
143 HSFC_FCYCLE_SHIFT = 1,
144 HSFC_FDBC_MASK = 0x3f00,
145 HSFC_FDBC_SHIFT = 8,
146 HSFC_FSMIE = 0x8000
147};
148
149enum {
150 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
151 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
152 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
153 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
154};
155
Lee Leahy32471722015-04-20 15:20:28 -0700156#if IS_ENABLED(CONFIG_DEBUG_SPI_FLASH)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700157
Lee Leahy32471722015-04-20 15:20:28 -0700158static u8 readb_(void *addr)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700159{
Lee Leahy32471722015-04-20 15:20:28 -0700160 u8 v = read8(addr);
161 printk(BIOS_DEBUG, "0x%p --> 0x%2.2x\n", addr, v);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700162 return v;
163}
164
Lee Leahy32471722015-04-20 15:20:28 -0700165static u16 readw_(void *addr)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700166{
Lee Leahy32471722015-04-20 15:20:28 -0700167 u16 v = read16(addr);
168 printk(BIOS_DEBUG, "0x%p --> 0x%4.4x\n", addr, v);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700169 return v;
170}
171
Lee Leahy32471722015-04-20 15:20:28 -0700172static u32 readl_(void *addr)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700173{
Lee Leahy32471722015-04-20 15:20:28 -0700174 u32 v = read32(addr);
175 printk(BIOS_DEBUG, "0x%p --> 0x%8.8x\n", addr, v);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700176 return v;
177}
178
Lee Leahy32471722015-04-20 15:20:28 -0700179static void writeb_(u8 b, void *addr)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700180{
Lee Leahy32471722015-04-20 15:20:28 -0700181 printk(BIOS_DEBUG, "0x%p <-- 0x%2.2x\n", addr, b);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700182 write8(addr, b);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700183}
184
Lee Leahy32471722015-04-20 15:20:28 -0700185static void writew_(u16 b, void *addr)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700186{
Lee Leahy32471722015-04-20 15:20:28 -0700187 printk(BIOS_DEBUG, "0x%p <-- 0x%4.4x\n", addr, b);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700188 write16(addr, b);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700189}
190
Lee Leahy32471722015-04-20 15:20:28 -0700191static void writel_(u32 b, void *addr)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700192{
Lee Leahy32471722015-04-20 15:20:28 -0700193 printk(BIOS_DEBUG, "0x%p <-- 0x%8.8x\n", addr, b);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700194 write32(addr, b);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700195}
196
197#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
198
199#define readb_(a) read8(a)
200#define readw_(a) read16(a)
201#define readl_(a) read32(a)
202#define writeb_(val, addr) write8(addr, val)
203#define writew_(val, addr) write16(addr, val)
204#define writel_(val, addr) write32(addr, val)
205
206#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
207
208static void write_reg(const void *value, void *dest, uint32_t size)
209{
210 const uint8_t *bvalue = value;
211 uint8_t *bdest = dest;
212
213 while (size >= 4) {
214 writel_(*(const uint32_t *)bvalue, bdest);
215 bdest += 4; bvalue += 4; size -= 4;
216 }
217 while (size) {
218 writeb_(*bvalue, bdest);
219 bdest++; bvalue++; size--;
220 }
221}
222
Lee Leahy32471722015-04-20 15:20:28 -0700223static void read_reg(void *src, void *value, uint32_t size)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700224{
Lee Leahy32471722015-04-20 15:20:28 -0700225 uint8_t *bsrc = src;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700226 uint8_t *bvalue = value;
227
228 while (size >= 4) {
229 *(uint32_t *)bvalue = readl_(bsrc);
230 bsrc += 4; bvalue += 4; size -= 4;
231 }
232 while (size) {
233 *bvalue = readb_(bsrc);
234 bsrc++; bvalue++; size--;
235 }
236}
237
Lee Leahy77ff0b12015-05-05 15:07:29 -0700238struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs)
239{
240 ich_spi_slave *slave = malloc(sizeof(*slave));
241
242 if (!slave) {
Lee Leahy32471722015-04-20 15:20:28 -0700243 printk(BIOS_ERR, "ICH SPI: Bad allocation\n");
Lee Leahy77ff0b12015-05-05 15:07:29 -0700244 return NULL;
245 }
246
247 memset(slave, 0, sizeof(*slave));
248
249 slave->bus = bus;
250 slave->cs = cs;
251 return slave;
252}
253
254static ich9_spi_regs *spi_regs(void)
255{
256 device_t dev;
257 uint32_t sbase;
258
Lee Leahyacb9c0b2015-07-02 11:55:18 -0700259#if ENV_SMM
Lee Leahy77ff0b12015-05-05 15:07:29 -0700260 dev = PCI_DEV(0, LPC_DEV, LPC_FUNC);
261#else
262 dev = dev_find_slot(0, PCI_DEVFN(LPC_DEV, LPC_FUNC));
263#endif
Lee Leahy32471722015-04-20 15:20:28 -0700264 if (!dev) {
265 printk(BIOS_ERR, "%s: PCI device not found", __func__);
266 return NULL;
267 }
268
Lee Leahy77ff0b12015-05-05 15:07:29 -0700269 pci_read_config_dword(dev, SBASE, &sbase);
270 sbase &= ~0x1ff;
271
272 return (void *)sbase;
273}
274
275void spi_init(void)
276{
Lee Leahy32471722015-04-20 15:20:28 -0700277 ich9_spi_regs *ich9_spi;
278
279 ich9_spi = spi_regs();
280 if (!ich9_spi) {
281 printk(BIOS_ERR, "Not initialising spi as %s returned NULL\n",
282 __func__);
283 return;
284 }
Lee Leahy77ff0b12015-05-05 15:07:29 -0700285
286 ichspi_lock = readw_(&ich9_spi->hsfs) & HSFS_FLOCKDN;
287 cntlr.opmenu = ich9_spi->opmenu;
288 cntlr.menubytes = sizeof(ich9_spi->opmenu);
289 cntlr.optype = &ich9_spi->optype;
290 cntlr.addr = &ich9_spi->faddr;
291 cntlr.data = (uint8_t *)ich9_spi->fdata;
292 cntlr.databytes = sizeof(ich9_spi->fdata);
293 cntlr.status = &ich9_spi->ssfs;
294 cntlr.control = (uint16_t *)ich9_spi->ssfc;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700295 cntlr.preop = &ich9_spi->preop;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700296}
297
Lee Leahy32471722015-04-20 15:20:28 -0700298#if ENV_RAMSTAGE
299
Lee Leahy77ff0b12015-05-05 15:07:29 -0700300static void spi_init_cb(void *unused)
301{
302 spi_init();
303}
304
305BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Lee Leahy32471722015-04-20 15:20:28 -0700306
307#endif /* ENV_RAMSTAGE */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700308
309int spi_claim_bus(struct spi_slave *slave)
310{
311 /* Handled by ICH automatically. */
312 return 0;
313}
314
315void spi_release_bus(struct spi_slave *slave)
316{
317 /* Handled by ICH automatically. */
318}
319
320typedef struct spi_transaction {
321 const uint8_t *out;
322 uint32_t bytesout;
323 uint8_t *in;
324 uint32_t bytesin;
325 uint8_t type;
326 uint8_t opcode;
327 uint32_t offset;
328} spi_transaction;
329
330static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
331{
332 trans->out += bytes;
333 trans->bytesout -= bytes;
334}
335
336static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
337{
338 trans->in += bytes;
339 trans->bytesin -= bytes;
340}
341
342static void spi_setup_type(spi_transaction *trans)
343{
344 trans->type = 0xFF;
345
346 /* Try to guess spi type from read/write sizes. */
347 if (trans->bytesin == 0) {
348 if (trans->bytesout > 4)
349 /*
350 * If bytesin = 0 and bytesout > 4, we presume this is
351 * a write data operation, which is accompanied by an
352 * address.
353 */
354 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
355 else
356 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
357 return;
358 }
359
360 if (trans->bytesout == 1) { /* and bytesin is > 0 */
361 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
362 return;
363 }
364
365 if (trans->bytesout == 4) { /* and bytesin is > 0 */
366 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
367 }
368
369 /* Fast read command is called with 5 bytes instead of 4 */
370 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
371 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
372 --trans->bytesout;
373 }
374}
375
376static int spi_setup_opcode(spi_transaction *trans)
377{
378 uint16_t optypes;
379 uint8_t opmenu[cntlr.menubytes];
380
381 trans->opcode = trans->out[0];
382 spi_use_out(trans, 1);
383 if (!ichspi_lock) {
384 /* The lock is off, so just use index 0. */
385 writeb_(trans->opcode, cntlr.opmenu);
386 optypes = readw_(cntlr.optype);
387 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
388 writew_(optypes, cntlr.optype);
389 return 0;
390 } else {
391 /* The lock is on. See if what we need is on the menu. */
392 uint8_t optype;
393 uint16_t opcode_index;
394
395 /* Write Enable is handled as atomic prefix */
396 if (trans->opcode == SPI_OPCODE_WREN)
397 return 0;
398
399 read_reg(cntlr.opmenu, opmenu, sizeof(opmenu));
400 for (opcode_index = 0; opcode_index < cntlr.menubytes;
401 opcode_index++) {
402 if (opmenu[opcode_index] == trans->opcode)
403 break;
404 }
405
406 if (opcode_index == cntlr.menubytes) {
407 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
408 trans->opcode);
409 return -1;
410 }
411
412 optypes = readw_(cntlr.optype);
413 optype = (optypes >> (opcode_index * 2)) & 0x3;
414 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
415 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
416 trans->bytesout >= 3) {
417 /* We guessed wrong earlier. Fix it up. */
418 trans->type = optype;
419 }
420 if (optype != trans->type) {
421 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
422 optype);
423 return -1;
424 }
425 return opcode_index;
426 }
427}
428
429static int spi_setup_offset(spi_transaction *trans)
430{
431 /* Separate the SPI address and data. */
432 switch (trans->type) {
433 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
434 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
435 return 0;
436 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
437 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
438 trans->offset = ((uint32_t)trans->out[0] << 16) |
439 ((uint32_t)trans->out[1] << 8) |
440 ((uint32_t)trans->out[2] << 0);
441 spi_use_out(trans, 3);
442 return 1;
443 default:
Lee Leahy32471722015-04-20 15:20:28 -0700444 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n",
445 trans->type);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700446 return -1;
447 }
448}
449
450/*
Lee Leahy32471722015-04-20 15:20:28 -0700451 * Wait for up to 400ms til status register bit(s) turn 1 (in case wait_til_set
Lee Leahy77ff0b12015-05-05 15:07:29 -0700452 * below is True) or 0. In case the wait was for the bit(s) to set - write
453 * those bits back, which would cause resetting them.
454 *
455 * Return the last read status value on success or -1 on failure.
456 */
457static int ich_status_poll(u16 bitmask, int wait_til_set)
458{
459 int timeout = 40000; /* This will result in 400 ms */
460 u16 status = 0;
461
Lee Leahy32471722015-04-20 15:20:28 -0700462 wait_til_set &= 1;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700463 while (timeout--) {
464 status = readw_(cntlr.status);
465 if (wait_til_set ^ ((status & bitmask) == 0)) {
466 if (wait_til_set)
467 writew_((status & bitmask), cntlr.status);
468 return status;
469 }
470 udelay(10);
471 }
472
Lee Leahy32471722015-04-20 15:20:28 -0700473 printk(BIOS_ERR, "ICH SPI: SCIP timeout, read %x, expected %x\n",
Lee Leahy77ff0b12015-05-05 15:07:29 -0700474 status, bitmask);
475 return -1;
476}
477
478unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len)
479{
480 return min(cntlr.databytes, buf_len);
481}
482
483int spi_xfer(struct spi_slave *slave, const void *dout,
484 unsigned int bytesout, void *din, unsigned int bytesin)
485{
486 uint16_t control;
487 int16_t opcode_index;
488 int with_address;
489 int status;
490
491 spi_transaction trans = {
492 dout, bytesout,
493 din, bytesin,
494 0xff, 0xff, 0
495 };
496
497 /* There has to always at least be an opcode. */
498 if (!bytesout || !dout) {
499 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
500 return -1;
501 }
502 /* Make sure if we read something we have a place to put it. */
503 if (bytesin != 0 && !din) {
504 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
505 return -1;
506 }
507
508 if (ich_status_poll(SPIS_SCIP, 0) == -1)
509 return -1;
510
511 writew_(SPIS_CDS | SPIS_FCERR, cntlr.status);
512
513 spi_setup_type(&trans);
Lee Leahy32471722015-04-20 15:20:28 -0700514 opcode_index = spi_setup_opcode(&trans);
515 if (opcode_index < 0)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700516 return -1;
Lee Leahy32471722015-04-20 15:20:28 -0700517 with_address = spi_setup_offset(&trans);
518 if (with_address < 0)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700519 return -1;
520
521 if (trans.opcode == SPI_OPCODE_WREN) {
522 /*
523 * Treat Write Enable as Atomic Pre-Op if possible
524 * in order to prevent the Management Engine from
525 * issuing a transaction between WREN and DATA.
526 */
527 if (!ichspi_lock)
528 writew_(trans.opcode, cntlr.preop);
529 return 0;
530 }
531
532 /* Preset control fields */
533 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
534
535 /* Issue atomic preop cycle if needed */
536 if (readw_(cntlr.preop))
537 control |= SPIC_ACS;
538
539 if (!trans.bytesout && !trans.bytesin) {
540 /* SPI addresses are 24 bit only */
541 if (with_address)
542 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
543
544 /*
545 * This is a 'no data' command (like Write Enable), its
546 * bytesout size was 1, decremented to zero while executing
547 * spi_setup_opcode() above. Tell the chip to send the
548 * command.
549 */
550 writew_(control, cntlr.control);
551
552 /* wait for the result */
553 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
554 if (status == -1)
555 return -1;
556
557 if (status & SPIS_FCERR) {
Lee Leahy32471722015-04-20 15:20:28 -0700558 printk(BIOS_ERR, "ICH SPI: Command transaction error\n");
Lee Leahy77ff0b12015-05-05 15:07:29 -0700559 return -1;
560 }
561
562 return 0;
563 }
564
565 /*
566 * Check if this is a write command attempting to transfer more bytes
567 * than the controller can handle. Iterations for writes are not
568 * supported here because each SPI write command needs to be preceded
569 * and followed by other SPI commands, and this sequence is controlled
570 * by the SPI chip driver.
571 */
572 if (trans.bytesout > cntlr.databytes) {
Lee Leahy32471722015-04-20 15:20:28 -0700573 printk(BIOS_DEBUG,
574 "ICH SPI: Too much to write. Does your SPI chip driver use"
575 " CONTROLLER_PAGE_LIMIT?\n");
Lee Leahy77ff0b12015-05-05 15:07:29 -0700576 return -1;
577 }
578
579 /*
580 * Read or write up to databytes bytes at a time until everything has
581 * been sent.
582 */
583 while (trans.bytesout || trans.bytesin) {
584 uint32_t data_length;
585
586 /* SPI addresses are 24 bit only */
587 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
588
589 if (trans.bytesout)
590 data_length = min(trans.bytesout, cntlr.databytes);
591 else
592 data_length = min(trans.bytesin, cntlr.databytes);
593
594 /* Program data into FDATA0 to N */
595 if (trans.bytesout) {
596 write_reg(trans.out, cntlr.data, data_length);
597 spi_use_out(&trans, data_length);
598 if (with_address)
599 trans.offset += data_length;
600 }
601
602 /* Add proper control fields' values */
603 control &= ~((cntlr.databytes - 1) << 8);
604 control |= SPIC_DS;
605 control |= (data_length - 1) << 8;
606
607 /* write it */
608 writew_(control, cntlr.control);
609
610 /* Wait for Cycle Done Status or Flash Cycle Error. */
611 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
612 if (status == -1)
613 return -1;
614
615 if (status & SPIS_FCERR) {
Lee Leahy32471722015-04-20 15:20:28 -0700616 printk(BIOS_ERR, "ICH SPI: Data transaction error\n");
Lee Leahy77ff0b12015-05-05 15:07:29 -0700617 return -1;
618 }
619
620 if (trans.bytesin) {
621 read_reg(cntlr.data, trans.in, data_length);
622 spi_use_in(&trans, data_length);
623 if (with_address)
624 trans.offset += data_length;
625 }
626 }
627
628 /* Clear atomic preop now that xfer is done */
629 writew_(0, cntlr.preop);
630
631 return 0;
632}