blob: 16fb46503792f039193b83ec8c8fd1ff69a72909 [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>
29#include <soc/lpc.h>
30#include <soc/pci_devs.h>
31#include <spi_flash.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070032#include <stdint.h>
33#include <stdlib.h>
34#include <string.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070035
36#ifdef __SMM__
37#define pci_read_config_byte(dev, reg, targ)\
38 *(targ) = pci_read_config8(dev, reg)
39#define pci_read_config_word(dev, reg, targ)\
40 *(targ) = pci_read_config16(dev, reg)
41#define pci_read_config_dword(dev, reg, targ)\
42 *(targ) = pci_read_config32(dev, reg)
43#define pci_write_config_byte(dev, reg, val)\
44 pci_write_config8(dev, reg, val)
45#define pci_write_config_word(dev, reg, val)\
46 pci_write_config16(dev, reg, val)
47#define pci_write_config_dword(dev, reg, val)\
48 pci_write_config32(dev, reg, val)
49#else /* !__SMM__ */
50#include <device/device.h>
51#include <device/pci.h>
52#define pci_read_config_byte(dev, reg, targ)\
53 *(targ) = pci_read_config8(dev, reg)
54#define pci_read_config_word(dev, reg, targ)\
55 *(targ) = pci_read_config16(dev, reg)
56#define pci_read_config_dword(dev, reg, targ)\
57 *(targ) = pci_read_config32(dev, reg)
58#define pci_write_config_byte(dev, reg, val)\
59 pci_write_config8(dev, reg, val)
60#define pci_write_config_word(dev, reg, val)\
61 pci_write_config16(dev, reg, val)
62#define pci_write_config_dword(dev, reg, val)\
63 pci_write_config32(dev, reg, val)
64#endif /* !__SMM__ */
65
66typedef struct spi_slave ich_spi_slave;
67
68static int ichspi_lock = 0;
69
70typedef struct ich9_spi_regs {
71 uint32_t bfpr;
72 uint16_t hsfs;
73 uint16_t hsfc;
74 uint32_t faddr;
75 uint32_t _reserved0;
76 uint32_t fdata[16];
77 uint32_t frap;
78 uint32_t freg[5];
79 uint32_t _reserved1[3];
80 uint32_t pr[5];
81 uint32_t _reserved2[2];
82 uint8_t ssfs;
83 uint8_t ssfc[3];
84 uint16_t preop;
85 uint16_t optype;
86 uint8_t opmenu[8];
Lee Leahy77ff0b12015-05-05 15:07:29 -070087} __attribute__((packed)) ich9_spi_regs;
88
89typedef struct ich_spi_controller {
90 int locked;
91
92 uint8_t *opmenu;
93 int menubytes;
94 uint16_t *preop;
95 uint16_t *optype;
96 uint32_t *addr;
97 uint8_t *data;
98 unsigned databytes;
99 uint8_t *status;
100 uint16_t *control;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700101} ich_spi_controller;
102
103static ich_spi_controller cntlr;
104
105enum {
106 SPIS_SCIP = 0x0001,
107 SPIS_GRANT = 0x0002,
108 SPIS_CDS = 0x0004,
109 SPIS_FCERR = 0x0008,
110 SSFS_AEL = 0x0010,
111 SPIS_LOCK = 0x8000,
112 SPIS_RESERVED_MASK = 0x7ff0,
113 SSFS_RESERVED_MASK = 0x7fe2
114};
115
116enum {
117 SPIC_SCGO = 0x000002,
118 SPIC_ACS = 0x000004,
119 SPIC_SPOP = 0x000008,
120 SPIC_DBC = 0x003f00,
121 SPIC_DS = 0x004000,
122 SPIC_SME = 0x008000,
123 SSFC_SCF_MASK = 0x070000,
124 SSFC_RESERVED = 0xf80000
125};
126
127enum {
128 HSFS_FDONE = 0x0001,
129 HSFS_FCERR = 0x0002,
130 HSFS_AEL = 0x0004,
131 HSFS_BERASE_MASK = 0x0018,
132 HSFS_BERASE_SHIFT = 3,
133 HSFS_SCIP = 0x0020,
134 HSFS_FDOPSS = 0x2000,
135 HSFS_FDV = 0x4000,
136 HSFS_FLOCKDN = 0x8000
137};
138
139enum {
140 HSFC_FGO = 0x0001,
141 HSFC_FCYCLE_MASK = 0x0006,
142 HSFC_FCYCLE_SHIFT = 1,
143 HSFC_FDBC_MASK = 0x3f00,
144 HSFC_FDBC_SHIFT = 8,
145 HSFC_FSMIE = 0x8000
146};
147
148enum {
149 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
150 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
151 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
152 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
153};
154
Lee Leahy32471722015-04-20 15:20:28 -0700155#if IS_ENABLED(CONFIG_DEBUG_SPI_FLASH)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700156
Lee Leahy32471722015-04-20 15:20:28 -0700157static u8 readb_(void *addr)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700158{
Lee Leahy32471722015-04-20 15:20:28 -0700159 u8 v = read8(addr);
160 printk(BIOS_DEBUG, "0x%p --> 0x%2.2x\n", addr, v);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700161 return v;
162}
163
Lee Leahy32471722015-04-20 15:20:28 -0700164static u16 readw_(void *addr)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700165{
Lee Leahy32471722015-04-20 15:20:28 -0700166 u16 v = read16(addr);
167 printk(BIOS_DEBUG, "0x%p --> 0x%4.4x\n", addr, v);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700168 return v;
169}
170
Lee Leahy32471722015-04-20 15:20:28 -0700171static u32 readl_(void *addr)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700172{
Lee Leahy32471722015-04-20 15:20:28 -0700173 u32 v = read32(addr);
174 printk(BIOS_DEBUG, "0x%p --> 0x%8.8x\n", addr, v);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700175 return v;
176}
177
Lee Leahy32471722015-04-20 15:20:28 -0700178static void writeb_(u8 b, void *addr)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700179{
Lee Leahy32471722015-04-20 15:20:28 -0700180 printk(BIOS_DEBUG, "0x%p <-- 0x%2.2x\n", addr, b);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700181 write8(addr, b);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700182}
183
Lee Leahy32471722015-04-20 15:20:28 -0700184static void writew_(u16 b, void *addr)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700185{
Lee Leahy32471722015-04-20 15:20:28 -0700186 printk(BIOS_DEBUG, "0x%p <-- 0x%4.4x\n", addr, b);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700187 write16(addr, b);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700188}
189
Lee Leahy32471722015-04-20 15:20:28 -0700190static void writel_(u32 b, void *addr)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700191{
Lee Leahy32471722015-04-20 15:20:28 -0700192 printk(BIOS_DEBUG, "0x%p <-- 0x%8.8x\n", addr, b);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700193 write32(addr, b);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700194}
195
196#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
197
198#define readb_(a) read8(a)
199#define readw_(a) read16(a)
200#define readl_(a) read32(a)
201#define writeb_(val, addr) write8(addr, val)
202#define writew_(val, addr) write16(addr, val)
203#define writel_(val, addr) write32(addr, val)
204
205#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
206
207static void write_reg(const void *value, void *dest, uint32_t size)
208{
209 const uint8_t *bvalue = value;
210 uint8_t *bdest = dest;
211
212 while (size >= 4) {
213 writel_(*(const uint32_t *)bvalue, bdest);
214 bdest += 4; bvalue += 4; size -= 4;
215 }
216 while (size) {
217 writeb_(*bvalue, bdest);
218 bdest++; bvalue++; size--;
219 }
220}
221
Lee Leahy32471722015-04-20 15:20:28 -0700222static void read_reg(void *src, void *value, uint32_t size)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700223{
Lee Leahy32471722015-04-20 15:20:28 -0700224 uint8_t *bsrc = src;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700225 uint8_t *bvalue = value;
226
227 while (size >= 4) {
228 *(uint32_t *)bvalue = readl_(bsrc);
229 bsrc += 4; bvalue += 4; size -= 4;
230 }
231 while (size) {
232 *bvalue = readb_(bsrc);
233 bsrc++; bvalue++; size--;
234 }
235}
236
Lee Leahy77ff0b12015-05-05 15:07:29 -0700237struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs)
238{
239 ich_spi_slave *slave = malloc(sizeof(*slave));
240
241 if (!slave) {
Lee Leahy32471722015-04-20 15:20:28 -0700242 printk(BIOS_ERR, "ICH SPI: Bad allocation\n");
Lee Leahy77ff0b12015-05-05 15:07:29 -0700243 return NULL;
244 }
245
246 memset(slave, 0, sizeof(*slave));
247
248 slave->bus = bus;
249 slave->cs = cs;
250 return slave;
251}
252
253static ich9_spi_regs *spi_regs(void)
254{
255 device_t dev;
256 uint32_t sbase;
257
258#ifdef __SMM__
259 dev = PCI_DEV(0, LPC_DEV, LPC_FUNC);
260#else
261 dev = dev_find_slot(0, PCI_DEVFN(LPC_DEV, LPC_FUNC));
262#endif
Lee Leahy32471722015-04-20 15:20:28 -0700263 if (!dev) {
264 printk(BIOS_ERR, "%s: PCI device not found", __func__);
265 return NULL;
266 }
267
Lee Leahy77ff0b12015-05-05 15:07:29 -0700268 pci_read_config_dword(dev, SBASE, &sbase);
269 sbase &= ~0x1ff;
270
271 return (void *)sbase;
272}
273
274void spi_init(void)
275{
Lee Leahy32471722015-04-20 15:20:28 -0700276 ich9_spi_regs *ich9_spi;
277
278 ich9_spi = spi_regs();
279 if (!ich9_spi) {
280 printk(BIOS_ERR, "Not initialising spi as %s returned NULL\n",
281 __func__);
282 return;
283 }
Lee Leahy77ff0b12015-05-05 15:07:29 -0700284
285 ichspi_lock = readw_(&ich9_spi->hsfs) & HSFS_FLOCKDN;
286 cntlr.opmenu = ich9_spi->opmenu;
287 cntlr.menubytes = sizeof(ich9_spi->opmenu);
288 cntlr.optype = &ich9_spi->optype;
289 cntlr.addr = &ich9_spi->faddr;
290 cntlr.data = (uint8_t *)ich9_spi->fdata;
291 cntlr.databytes = sizeof(ich9_spi->fdata);
292 cntlr.status = &ich9_spi->ssfs;
293 cntlr.control = (uint16_t *)ich9_spi->ssfc;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700294 cntlr.preop = &ich9_spi->preop;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700295}
296
Lee Leahy32471722015-04-20 15:20:28 -0700297#if ENV_RAMSTAGE
298
Lee Leahy77ff0b12015-05-05 15:07:29 -0700299static void spi_init_cb(void *unused)
300{
301 spi_init();
302}
303
304BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Lee Leahy32471722015-04-20 15:20:28 -0700305
306#endif /* ENV_RAMSTAGE */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700307
308int spi_claim_bus(struct spi_slave *slave)
309{
310 /* Handled by ICH automatically. */
311 return 0;
312}
313
314void spi_release_bus(struct spi_slave *slave)
315{
316 /* Handled by ICH automatically. */
317}
318
319typedef struct spi_transaction {
320 const uint8_t *out;
321 uint32_t bytesout;
322 uint8_t *in;
323 uint32_t bytesin;
324 uint8_t type;
325 uint8_t opcode;
326 uint32_t offset;
327} spi_transaction;
328
329static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
330{
331 trans->out += bytes;
332 trans->bytesout -= bytes;
333}
334
335static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
336{
337 trans->in += bytes;
338 trans->bytesin -= bytes;
339}
340
341static void spi_setup_type(spi_transaction *trans)
342{
343 trans->type = 0xFF;
344
345 /* Try to guess spi type from read/write sizes. */
346 if (trans->bytesin == 0) {
347 if (trans->bytesout > 4)
348 /*
349 * If bytesin = 0 and bytesout > 4, we presume this is
350 * a write data operation, which is accompanied by an
351 * address.
352 */
353 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
354 else
355 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
356 return;
357 }
358
359 if (trans->bytesout == 1) { /* and bytesin is > 0 */
360 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
361 return;
362 }
363
364 if (trans->bytesout == 4) { /* and bytesin is > 0 */
365 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
366 }
367
368 /* Fast read command is called with 5 bytes instead of 4 */
369 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
370 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
371 --trans->bytesout;
372 }
373}
374
375static int spi_setup_opcode(spi_transaction *trans)
376{
377 uint16_t optypes;
378 uint8_t opmenu[cntlr.menubytes];
379
380 trans->opcode = trans->out[0];
381 spi_use_out(trans, 1);
382 if (!ichspi_lock) {
383 /* The lock is off, so just use index 0. */
384 writeb_(trans->opcode, cntlr.opmenu);
385 optypes = readw_(cntlr.optype);
386 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
387 writew_(optypes, cntlr.optype);
388 return 0;
389 } else {
390 /* The lock is on. See if what we need is on the menu. */
391 uint8_t optype;
392 uint16_t opcode_index;
393
394 /* Write Enable is handled as atomic prefix */
395 if (trans->opcode == SPI_OPCODE_WREN)
396 return 0;
397
398 read_reg(cntlr.opmenu, opmenu, sizeof(opmenu));
399 for (opcode_index = 0; opcode_index < cntlr.menubytes;
400 opcode_index++) {
401 if (opmenu[opcode_index] == trans->opcode)
402 break;
403 }
404
405 if (opcode_index == cntlr.menubytes) {
406 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
407 trans->opcode);
408 return -1;
409 }
410
411 optypes = readw_(cntlr.optype);
412 optype = (optypes >> (opcode_index * 2)) & 0x3;
413 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
414 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
415 trans->bytesout >= 3) {
416 /* We guessed wrong earlier. Fix it up. */
417 trans->type = optype;
418 }
419 if (optype != trans->type) {
420 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
421 optype);
422 return -1;
423 }
424 return opcode_index;
425 }
426}
427
428static int spi_setup_offset(spi_transaction *trans)
429{
430 /* Separate the SPI address and data. */
431 switch (trans->type) {
432 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
433 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
434 return 0;
435 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
436 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
437 trans->offset = ((uint32_t)trans->out[0] << 16) |
438 ((uint32_t)trans->out[1] << 8) |
439 ((uint32_t)trans->out[2] << 0);
440 spi_use_out(trans, 3);
441 return 1;
442 default:
Lee Leahy32471722015-04-20 15:20:28 -0700443 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n",
444 trans->type);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700445 return -1;
446 }
447}
448
449/*
Lee Leahy32471722015-04-20 15:20:28 -0700450 * Wait for up to 400ms til status register bit(s) turn 1 (in case wait_til_set
Lee Leahy77ff0b12015-05-05 15:07:29 -0700451 * below is True) or 0. In case the wait was for the bit(s) to set - write
452 * those bits back, which would cause resetting them.
453 *
454 * Return the last read status value on success or -1 on failure.
455 */
456static int ich_status_poll(u16 bitmask, int wait_til_set)
457{
458 int timeout = 40000; /* This will result in 400 ms */
459 u16 status = 0;
460
Lee Leahy32471722015-04-20 15:20:28 -0700461 wait_til_set &= 1;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700462 while (timeout--) {
463 status = readw_(cntlr.status);
464 if (wait_til_set ^ ((status & bitmask) == 0)) {
465 if (wait_til_set)
466 writew_((status & bitmask), cntlr.status);
467 return status;
468 }
469 udelay(10);
470 }
471
Lee Leahy32471722015-04-20 15:20:28 -0700472 printk(BIOS_ERR, "ICH SPI: SCIP timeout, read %x, expected %x\n",
Lee Leahy77ff0b12015-05-05 15:07:29 -0700473 status, bitmask);
474 return -1;
475}
476
477unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len)
478{
479 return min(cntlr.databytes, buf_len);
480}
481
482int spi_xfer(struct spi_slave *slave, const void *dout,
483 unsigned int bytesout, void *din, unsigned int bytesin)
484{
485 uint16_t control;
486 int16_t opcode_index;
487 int with_address;
488 int status;
489
490 spi_transaction trans = {
491 dout, bytesout,
492 din, bytesin,
493 0xff, 0xff, 0
494 };
495
496 /* There has to always at least be an opcode. */
497 if (!bytesout || !dout) {
498 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
499 return -1;
500 }
501 /* Make sure if we read something we have a place to put it. */
502 if (bytesin != 0 && !din) {
503 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
504 return -1;
505 }
506
507 if (ich_status_poll(SPIS_SCIP, 0) == -1)
508 return -1;
509
510 writew_(SPIS_CDS | SPIS_FCERR, cntlr.status);
511
512 spi_setup_type(&trans);
Lee Leahy32471722015-04-20 15:20:28 -0700513 opcode_index = spi_setup_opcode(&trans);
514 if (opcode_index < 0)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700515 return -1;
Lee Leahy32471722015-04-20 15:20:28 -0700516 with_address = spi_setup_offset(&trans);
517 if (with_address < 0)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700518 return -1;
519
520 if (trans.opcode == SPI_OPCODE_WREN) {
521 /*
522 * Treat Write Enable as Atomic Pre-Op if possible
523 * in order to prevent the Management Engine from
524 * issuing a transaction between WREN and DATA.
525 */
526 if (!ichspi_lock)
527 writew_(trans.opcode, cntlr.preop);
528 return 0;
529 }
530
531 /* Preset control fields */
532 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
533
534 /* Issue atomic preop cycle if needed */
535 if (readw_(cntlr.preop))
536 control |= SPIC_ACS;
537
538 if (!trans.bytesout && !trans.bytesin) {
539 /* SPI addresses are 24 bit only */
540 if (with_address)
541 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
542
543 /*
544 * This is a 'no data' command (like Write Enable), its
545 * bytesout size was 1, decremented to zero while executing
546 * spi_setup_opcode() above. Tell the chip to send the
547 * command.
548 */
549 writew_(control, cntlr.control);
550
551 /* wait for the result */
552 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
553 if (status == -1)
554 return -1;
555
556 if (status & SPIS_FCERR) {
Lee Leahy32471722015-04-20 15:20:28 -0700557 printk(BIOS_ERR, "ICH SPI: Command transaction error\n");
Lee Leahy77ff0b12015-05-05 15:07:29 -0700558 return -1;
559 }
560
561 return 0;
562 }
563
564 /*
565 * Check if this is a write command attempting to transfer more bytes
566 * than the controller can handle. Iterations for writes are not
567 * supported here because each SPI write command needs to be preceded
568 * and followed by other SPI commands, and this sequence is controlled
569 * by the SPI chip driver.
570 */
571 if (trans.bytesout > cntlr.databytes) {
Lee Leahy32471722015-04-20 15:20:28 -0700572 printk(BIOS_DEBUG,
573 "ICH SPI: Too much to write. Does your SPI chip driver use"
574 " CONTROLLER_PAGE_LIMIT?\n");
Lee Leahy77ff0b12015-05-05 15:07:29 -0700575 return -1;
576 }
577
578 /*
579 * Read or write up to databytes bytes at a time until everything has
580 * been sent.
581 */
582 while (trans.bytesout || trans.bytesin) {
583 uint32_t data_length;
584
585 /* SPI addresses are 24 bit only */
586 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
587
588 if (trans.bytesout)
589 data_length = min(trans.bytesout, cntlr.databytes);
590 else
591 data_length = min(trans.bytesin, cntlr.databytes);
592
593 /* Program data into FDATA0 to N */
594 if (trans.bytesout) {
595 write_reg(trans.out, cntlr.data, data_length);
596 spi_use_out(&trans, data_length);
597 if (with_address)
598 trans.offset += data_length;
599 }
600
601 /* Add proper control fields' values */
602 control &= ~((cntlr.databytes - 1) << 8);
603 control |= SPIC_DS;
604 control |= (data_length - 1) << 8;
605
606 /* write it */
607 writew_(control, cntlr.control);
608
609 /* Wait for Cycle Done Status or Flash Cycle Error. */
610 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
611 if (status == -1)
612 return -1;
613
614 if (status & SPIS_FCERR) {
Lee Leahy32471722015-04-20 15:20:28 -0700615 printk(BIOS_ERR, "ICH SPI: Data transaction error\n");
Lee Leahy77ff0b12015-05-05 15:07:29 -0700616 return -1;
617 }
618
619 if (trans.bytesin) {
620 read_reg(cntlr.data, trans.in, data_length);
621 spi_use_in(&trans, data_length);
622 if (with_address)
623 trans.offset += data_length;
624 }
625 }
626
627 /* Clear atomic preop now that xfer is done */
628 writew_(0, cntlr.preop);
629
630 return 0;
631}