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