blob: 86b335107be3ee62fc96aa0c441a153189331d7f [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 -0700232static ich9_spi_regs *spi_regs(void)
233{
234 device_t dev;
235 uint32_t sbase;
236
Lee Leahyacb9c0b2015-07-02 11:55:18 -0700237#if ENV_SMM
Lee Leahy77ff0b12015-05-05 15:07:29 -0700238 dev = PCI_DEV(0, LPC_DEV, LPC_FUNC);
239#else
240 dev = dev_find_slot(0, PCI_DEVFN(LPC_DEV, LPC_FUNC));
241#endif
Lee Leahy32471722015-04-20 15:20:28 -0700242 if (!dev) {
243 printk(BIOS_ERR, "%s: PCI device not found", __func__);
244 return NULL;
245 }
246
Lee Leahy77ff0b12015-05-05 15:07:29 -0700247 pci_read_config_dword(dev, SBASE, &sbase);
248 sbase &= ~0x1ff;
249
250 return (void *)sbase;
251}
252
253void spi_init(void)
254{
Lee Leahy32471722015-04-20 15:20:28 -0700255 ich9_spi_regs *ich9_spi;
256
257 ich9_spi = spi_regs();
258 if (!ich9_spi) {
259 printk(BIOS_ERR, "Not initialising spi as %s returned NULL\n",
260 __func__);
261 return;
262 }
Lee Leahy77ff0b12015-05-05 15:07:29 -0700263
264 ichspi_lock = readw_(&ich9_spi->hsfs) & HSFS_FLOCKDN;
265 cntlr.opmenu = ich9_spi->opmenu;
266 cntlr.menubytes = sizeof(ich9_spi->opmenu);
267 cntlr.optype = &ich9_spi->optype;
268 cntlr.addr = &ich9_spi->faddr;
269 cntlr.data = (uint8_t *)ich9_spi->fdata;
270 cntlr.databytes = sizeof(ich9_spi->fdata);
271 cntlr.status = &ich9_spi->ssfs;
272 cntlr.control = (uint16_t *)ich9_spi->ssfc;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700273 cntlr.preop = &ich9_spi->preop;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700274}
275
Lee Leahy77ff0b12015-05-05 15:07:29 -0700276static void spi_init_cb(void *unused)
277{
278 spi_init();
279}
280
281BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Lee Leahy32471722015-04-20 15:20:28 -0700282
Lee Leahy77ff0b12015-05-05 15:07:29 -0700283typedef struct spi_transaction {
284 const uint8_t *out;
285 uint32_t bytesout;
286 uint8_t *in;
287 uint32_t bytesin;
288 uint8_t type;
289 uint8_t opcode;
290 uint32_t offset;
291} spi_transaction;
292
293static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
294{
295 trans->out += bytes;
296 trans->bytesout -= bytes;
297}
298
299static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
300{
301 trans->in += bytes;
302 trans->bytesin -= bytes;
303}
304
305static void spi_setup_type(spi_transaction *trans)
306{
307 trans->type = 0xFF;
308
309 /* Try to guess spi type from read/write sizes. */
310 if (trans->bytesin == 0) {
311 if (trans->bytesout > 4)
312 /*
313 * If bytesin = 0 and bytesout > 4, we presume this is
314 * a write data operation, which is accompanied by an
315 * address.
316 */
317 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
318 else
319 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
320 return;
321 }
322
323 if (trans->bytesout == 1) { /* and bytesin is > 0 */
324 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
325 return;
326 }
327
328 if (trans->bytesout == 4) { /* and bytesin is > 0 */
329 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
330 }
331
332 /* Fast read command is called with 5 bytes instead of 4 */
333 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
334 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
335 --trans->bytesout;
336 }
337}
338
339static int spi_setup_opcode(spi_transaction *trans)
340{
341 uint16_t optypes;
342 uint8_t opmenu[cntlr.menubytes];
343
344 trans->opcode = trans->out[0];
345 spi_use_out(trans, 1);
346 if (!ichspi_lock) {
347 /* The lock is off, so just use index 0. */
348 writeb_(trans->opcode, cntlr.opmenu);
349 optypes = readw_(cntlr.optype);
350 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
351 writew_(optypes, cntlr.optype);
352 return 0;
353 } else {
354 /* The lock is on. See if what we need is on the menu. */
355 uint8_t optype;
356 uint16_t opcode_index;
357
358 /* Write Enable is handled as atomic prefix */
359 if (trans->opcode == SPI_OPCODE_WREN)
360 return 0;
361
362 read_reg(cntlr.opmenu, opmenu, sizeof(opmenu));
363 for (opcode_index = 0; opcode_index < cntlr.menubytes;
364 opcode_index++) {
365 if (opmenu[opcode_index] == trans->opcode)
366 break;
367 }
368
369 if (opcode_index == cntlr.menubytes) {
370 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
371 trans->opcode);
372 return -1;
373 }
374
375 optypes = readw_(cntlr.optype);
376 optype = (optypes >> (opcode_index * 2)) & 0x3;
377 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
378 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
379 trans->bytesout >= 3) {
380 /* We guessed wrong earlier. Fix it up. */
381 trans->type = optype;
382 }
383 if (optype != trans->type) {
384 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
385 optype);
386 return -1;
387 }
388 return opcode_index;
389 }
390}
391
392static int spi_setup_offset(spi_transaction *trans)
393{
394 /* Separate the SPI address and data. */
395 switch (trans->type) {
396 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
397 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
398 return 0;
399 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
400 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
401 trans->offset = ((uint32_t)trans->out[0] << 16) |
402 ((uint32_t)trans->out[1] << 8) |
403 ((uint32_t)trans->out[2] << 0);
404 spi_use_out(trans, 3);
405 return 1;
406 default:
Lee Leahy32471722015-04-20 15:20:28 -0700407 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n",
408 trans->type);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700409 return -1;
410 }
411}
412
413/*
Lee Leahy32471722015-04-20 15:20:28 -0700414 * Wait for up to 400ms til status register bit(s) turn 1 (in case wait_til_set
Lee Leahy77ff0b12015-05-05 15:07:29 -0700415 * below is True) or 0. In case the wait was for the bit(s) to set - write
416 * those bits back, which would cause resetting them.
417 *
418 * Return the last read status value on success or -1 on failure.
419 */
420static int ich_status_poll(u16 bitmask, int wait_til_set)
421{
422 int timeout = 40000; /* This will result in 400 ms */
423 u16 status = 0;
424
Lee Leahy32471722015-04-20 15:20:28 -0700425 wait_til_set &= 1;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700426 while (timeout--) {
427 status = readw_(cntlr.status);
428 if (wait_til_set ^ ((status & bitmask) == 0)) {
429 if (wait_til_set)
430 writew_((status & bitmask), cntlr.status);
431 return status;
432 }
433 udelay(10);
434 }
435
Lee Leahy32471722015-04-20 15:20:28 -0700436 printk(BIOS_ERR, "ICH SPI: SCIP timeout, read %x, expected %x\n",
Lee Leahy77ff0b12015-05-05 15:07:29 -0700437 status, bitmask);
438 return -1;
439}
440
441unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len)
442{
443 return min(cntlr.databytes, buf_len);
444}
445
Furquan Shaikh94f86992016-12-01 07:12:32 -0800446static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
447 size_t bytesout, void *din, size_t bytesin)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700448{
449 uint16_t control;
450 int16_t opcode_index;
451 int with_address;
452 int status;
453
454 spi_transaction trans = {
455 dout, bytesout,
456 din, bytesin,
457 0xff, 0xff, 0
458 };
459
460 /* There has to always at least be an opcode. */
461 if (!bytesout || !dout) {
462 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
463 return -1;
464 }
465 /* Make sure if we read something we have a place to put it. */
466 if (bytesin != 0 && !din) {
467 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
468 return -1;
469 }
470
471 if (ich_status_poll(SPIS_SCIP, 0) == -1)
472 return -1;
473
474 writew_(SPIS_CDS | SPIS_FCERR, cntlr.status);
475
476 spi_setup_type(&trans);
Lee Leahy32471722015-04-20 15:20:28 -0700477 opcode_index = spi_setup_opcode(&trans);
478 if (opcode_index < 0)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700479 return -1;
Lee Leahy32471722015-04-20 15:20:28 -0700480 with_address = spi_setup_offset(&trans);
481 if (with_address < 0)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700482 return -1;
483
484 if (trans.opcode == SPI_OPCODE_WREN) {
485 /*
486 * Treat Write Enable as Atomic Pre-Op if possible
487 * in order to prevent the Management Engine from
488 * issuing a transaction between WREN and DATA.
489 */
490 if (!ichspi_lock)
491 writew_(trans.opcode, cntlr.preop);
492 return 0;
493 }
494
495 /* Preset control fields */
496 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
497
498 /* Issue atomic preop cycle if needed */
499 if (readw_(cntlr.preop))
500 control |= SPIC_ACS;
501
502 if (!trans.bytesout && !trans.bytesin) {
503 /* SPI addresses are 24 bit only */
504 if (with_address)
505 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
506
507 /*
508 * This is a 'no data' command (like Write Enable), its
509 * bytesout size was 1, decremented to zero while executing
510 * spi_setup_opcode() above. Tell the chip to send the
511 * command.
512 */
513 writew_(control, cntlr.control);
514
515 /* wait for the result */
516 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
517 if (status == -1)
518 return -1;
519
520 if (status & SPIS_FCERR) {
Lee Leahy32471722015-04-20 15:20:28 -0700521 printk(BIOS_ERR, "ICH SPI: Command transaction error\n");
Lee Leahy77ff0b12015-05-05 15:07:29 -0700522 return -1;
523 }
524
525 return 0;
526 }
527
528 /*
529 * Check if this is a write command attempting to transfer more bytes
530 * than the controller can handle. Iterations for writes are not
531 * supported here because each SPI write command needs to be preceded
532 * and followed by other SPI commands, and this sequence is controlled
533 * by the SPI chip driver.
534 */
535 if (trans.bytesout > cntlr.databytes) {
Lee Leahy32471722015-04-20 15:20:28 -0700536 printk(BIOS_DEBUG,
537 "ICH SPI: Too much to write. Does your SPI chip driver use"
538 " CONTROLLER_PAGE_LIMIT?\n");
Lee Leahy77ff0b12015-05-05 15:07:29 -0700539 return -1;
540 }
541
542 /*
543 * Read or write up to databytes bytes at a time until everything has
544 * been sent.
545 */
546 while (trans.bytesout || trans.bytesin) {
547 uint32_t data_length;
548
549 /* SPI addresses are 24 bit only */
550 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
551
552 if (trans.bytesout)
553 data_length = min(trans.bytesout, cntlr.databytes);
554 else
555 data_length = min(trans.bytesin, cntlr.databytes);
556
557 /* Program data into FDATA0 to N */
558 if (trans.bytesout) {
559 write_reg(trans.out, cntlr.data, data_length);
560 spi_use_out(&trans, data_length);
561 if (with_address)
562 trans.offset += data_length;
563 }
564
565 /* Add proper control fields' values */
566 control &= ~((cntlr.databytes - 1) << 8);
567 control |= SPIC_DS;
568 control |= (data_length - 1) << 8;
569
570 /* write it */
571 writew_(control, cntlr.control);
572
573 /* Wait for Cycle Done Status or Flash Cycle Error. */
574 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
575 if (status == -1)
576 return -1;
577
578 if (status & SPIS_FCERR) {
Lee Leahy32471722015-04-20 15:20:28 -0700579 printk(BIOS_ERR, "ICH SPI: Data transaction error\n");
Lee Leahy77ff0b12015-05-05 15:07:29 -0700580 return -1;
581 }
582
583 if (trans.bytesin) {
584 read_reg(cntlr.data, trans.in, data_length);
585 spi_use_in(&trans, data_length);
586 if (with_address)
587 trans.offset += data_length;
588 }
589 }
590
591 /* Clear atomic preop now that xfer is done */
592 writew_(0, cntlr.preop);
593
594 return 0;
595}
Furquan Shaikh94f86992016-12-01 07:12:32 -0800596
597static const struct spi_ctrlr spi_ctrlr = {
598 .xfer = spi_ctrlr_xfer,
Furquan Shaikhc2973d12016-11-29 22:07:42 -0800599 .xfer_vector = spi_xfer_two_vectors,
Furquan Shaikh94f86992016-12-01 07:12:32 -0800600};
601
602int spi_setup_slave(unsigned int bus, unsigned int cs, struct spi_slave *slave)
603{
604 slave->bus = bus;
605 slave->cs = cs;
606 slave->ctrlr = &spi_ctrlr;
607 return 0;
608}