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