blob: 53b998219297ef51865f2c3c454c6558f1ad4af1 [file] [log] [blame]
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
3 *
4 * See file CREDITS for list of people who contributed to this
5 * project.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but without any warranty; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
23/* This file is derived from the flashrom project. */
24#include <stdint.h>
25#include <stdlib.h>
26#include <string.h>
27#include <delay.h>
28#include <arch/io.h>
29#include <console/console.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070030#include <device/pci_ids.h>
31
32#include <spi.h>
33
34#define min(a, b) ((a)<(b)?(a):(b))
35
Duncan Laurie181bbdd2012-06-23 16:53:57 -070036#ifdef __SMM__
37#include <arch/romcc_io.h>
38#include <northbridge/intel/sandybridge/pcie_config.c>
Duncan Laurie181bbdd2012-06-23 16:53:57 -070039#define pci_read_config_byte(dev, reg, targ)\
40 *(targ) = pcie_read_config8(dev, reg)
41#define pci_read_config_word(dev, reg, targ)\
42 *(targ) = pcie_read_config16(dev, reg)
43#define pci_read_config_dword(dev, reg, targ)\
44 *(targ) = pcie_read_config32(dev, reg)
45#define pci_write_config_byte(dev, reg, val)\
46 pcie_write_config8(dev, reg, val)
47#define pci_write_config_word(dev, reg, val)\
48 pcie_write_config16(dev, reg, val)
49#define pci_write_config_dword(dev, reg, val)\
50 pcie_write_config32(dev, reg, val)
51#else /* !__SMM__ */
52#include <device/device.h>
53#include <device/pci.h>
Duncan Laurie181bbdd2012-06-23 16:53:57 -070054#define pci_read_config_byte(dev, reg, targ)\
55 *(targ) = pci_read_config8(dev, reg)
56#define pci_read_config_word(dev, reg, targ)\
57 *(targ) = pci_read_config16(dev, reg)
58#define pci_read_config_dword(dev, reg, targ)\
59 *(targ) = pci_read_config32(dev, reg)
60#define pci_write_config_byte(dev, reg, val)\
61 pci_write_config8(dev, reg, val)
62#define pci_write_config_word(dev, reg, val)\
63 pci_write_config16(dev, reg, val)
64#define pci_write_config_dword(dev, reg, val)\
65 pci_write_config32(dev, reg, val)
66#endif /* !__SMM__ */
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070067
68typedef struct spi_slave ich_spi_slave;
69
70static int ichspi_lock = 0;
71
72typedef struct ich7_spi_regs {
73 uint16_t spis;
74 uint16_t spic;
75 uint32_t spia;
76 uint64_t spid[8];
77 uint64_t _pad;
78 uint32_t bbar;
79 uint16_t preop;
80 uint16_t optype;
81 uint8_t opmenu[8];
82} __attribute__((packed)) ich7_spi_regs;
83
84typedef struct ich9_spi_regs {
85 uint32_t bfpr;
86 uint16_t hsfs;
87 uint16_t hsfc;
88 uint32_t faddr;
89 uint32_t _reserved0;
90 uint32_t fdata[16];
91 uint32_t frap;
92 uint32_t freg[5];
93 uint32_t _reserved1[3];
94 uint32_t pr[5];
95 uint32_t _reserved2[2];
96 uint8_t ssfs;
97 uint8_t ssfc[3];
98 uint16_t preop;
99 uint16_t optype;
100 uint8_t opmenu[8];
101 uint32_t bbar;
102 uint8_t _reserved3[12];
103 uint32_t fdoc;
104 uint32_t fdod;
105 uint8_t _reserved4[8];
106 uint32_t afc;
107 uint32_t lvscc;
108 uint32_t uvscc;
109 uint8_t _reserved5[4];
110 uint32_t fpb;
111 uint8_t _reserved6[28];
112 uint32_t srdl;
113 uint32_t srdc;
114 uint32_t srd;
115} __attribute__((packed)) ich9_spi_regs;
116
117typedef struct ich_spi_controller {
118 int locked;
119
120 uint8_t *opmenu;
121 int menubytes;
122 uint16_t *preop;
123 uint16_t *optype;
124 uint32_t *addr;
125 uint8_t *data;
126 unsigned databytes;
127 uint8_t *status;
128 uint16_t *control;
129 uint32_t *bbar;
130} ich_spi_controller;
131
132static ich_spi_controller cntlr;
133
134enum {
135 SPIS_SCIP = 0x0001,
136 SPIS_GRANT = 0x0002,
137 SPIS_CDS = 0x0004,
138 SPIS_FCERR = 0x0008,
139 SSFS_AEL = 0x0010,
140 SPIS_LOCK = 0x8000,
141 SPIS_RESERVED_MASK = 0x7ff0,
142 SSFS_RESERVED_MASK = 0x7fe2
143};
144
145enum {
146 SPIC_SCGO = 0x000002,
147 SPIC_ACS = 0x000004,
148 SPIC_SPOP = 0x000008,
149 SPIC_DBC = 0x003f00,
150 SPIC_DS = 0x004000,
151 SPIC_SME = 0x008000,
152 SSFC_SCF_MASK = 0x070000,
153 SSFC_RESERVED = 0xf80000
154};
155
156enum {
157 HSFS_FDONE = 0x0001,
158 HSFS_FCERR = 0x0002,
159 HSFS_AEL = 0x0004,
160 HSFS_BERASE_MASK = 0x0018,
161 HSFS_BERASE_SHIFT = 3,
162 HSFS_SCIP = 0x0020,
163 HSFS_FDOPSS = 0x2000,
164 HSFS_FDV = 0x4000,
165 HSFS_FLOCKDN = 0x8000
166};
167
168enum {
169 HSFC_FGO = 0x0001,
170 HSFC_FCYCLE_MASK = 0x0006,
171 HSFC_FCYCLE_SHIFT = 1,
172 HSFC_FDBC_MASK = 0x3f00,
173 HSFC_FDBC_SHIFT = 8,
174 HSFC_FSMIE = 0x8000
175};
176
177enum {
178 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
179 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
180 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
181 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
182};
183
184#if CONFIG_DEBUG_SPI_FLASH
185
186static u8 readb_(const void *addr)
187{
Stefan Reinauer14b23a62012-05-22 15:24:51 -0700188 u8 v = read8((unsigned long)addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700189 printk(BIOS_DEBUG, "read %2.2x from %4.4x\n",
190 v, ((unsigned) addr & 0xffff) - 0xf020);
191 return v;
192}
193
194static u16 readw_(const void *addr)
195{
Stefan Reinauer14b23a62012-05-22 15:24:51 -0700196 u16 v = read16((unsigned long)addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700197 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
198 v, ((unsigned) addr & 0xffff) - 0xf020);
199 return v;
200}
201
202static u32 readl_(const void *addr)
203{
Stefan Reinauer14b23a62012-05-22 15:24:51 -0700204 u32 v = read32((unsigned long)addr);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700205 printk(BIOS_DEBUG, "read %8.8x from %4.4x\n",
206 v, ((unsigned) addr & 0xffff) - 0xf020);
207 return v;
208}
209
210static void writeb_(u8 b, const void *addr)
211{
Stefan Reinauer14b23a62012-05-22 15:24:51 -0700212 write8((unsigned long)addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700213 printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n",
214 b, ((unsigned) addr & 0xffff) - 0xf020);
215}
216
217static void writew_(u16 b, const void *addr)
218{
Stefan Reinauer14b23a62012-05-22 15:24:51 -0700219 write16((unsigned long)addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700220 printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n",
221 b, ((unsigned) addr & 0xffff) - 0xf020);
222}
223
224static void writel_(u32 b, const void *addr)
225{
Stefan Reinauer14b23a62012-05-22 15:24:51 -0700226 write32((unsigned long)addr, b);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700227 printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n",
228 b, ((unsigned) addr & 0xffff) - 0xf020);
229}
230
231#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
232
233#define readb_(a) read8((uint32_t)a)
234#define readw_(a) read16((uint32_t)a)
235#define readl_(a) read32((uint32_t)a)
236#define writeb_(val, addr) write8((uint32_t)addr, val)
237#define writew_(val, addr) write16((uint32_t)addr, val)
238#define writel_(val, addr) write32((uint32_t)addr, val)
239
240#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
241
242static void write_reg(const void *value, void *dest, uint32_t size)
243{
244 const uint8_t *bvalue = value;
245 uint8_t *bdest = dest;
246
247 while (size >= 4) {
248 writel_(*(const uint32_t *)bvalue, bdest);
249 bdest += 4; bvalue += 4; size -= 4;
250 }
251 while (size) {
252 writeb_(*bvalue, bdest);
253 bdest++; bvalue++; size--;
254 }
255}
256
257static void read_reg(const void *src, void *value, uint32_t size)
258{
259 const uint8_t *bsrc = src;
260 uint8_t *bvalue = value;
261
262 while (size >= 4) {
263 *(uint32_t *)bvalue = readl_(bsrc);
264 bsrc += 4; bvalue += 4; size -= 4;
265 }
266 while (size) {
267 *bvalue = readb_(bsrc);
268 bsrc++; bvalue++; size--;
269 }
270}
271
272static void ich_set_bbar(uint32_t minaddr)
273{
274 const uint32_t bbar_mask = 0x00ffff00;
275 uint32_t ichspi_bbar;
276
277 minaddr &= bbar_mask;
278 ichspi_bbar = readl_(cntlr.bbar) & ~bbar_mask;
279 ichspi_bbar |= minaddr;
280 writel_(ichspi_bbar, cntlr.bbar);
281}
282
283int spi_cs_is_valid(unsigned int bus, unsigned int cs)
284{
285 printk(BIOS_DEBUG, "spi_cs_is_valid used but not implemented\n");
286 return 0;
287}
288
289struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
290 unsigned int max_hz, unsigned int mode)
291{
292 ich_spi_slave *slave = malloc(sizeof(*slave));
293
294 if (!slave) {
295 printk(BIOS_DEBUG, "ICH SPI: Bad allocation\n");
296 return NULL;
297 }
298
299 memset(slave, 0, sizeof(*slave));
300
301 slave->bus = bus;
302 slave->cs = cs;
303 return slave;
304}
305
Stefan Reinauer691c9f02012-05-23 11:18:35 -0700306/*
307 * Check if this device ID matches one of supported Intel PCH devices.
308 *
309 * Return the ICH version if there is a match, or zero otherwise.
310 */
311static inline int get_ich_version(uint16_t device_id)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700312{
Stefan Reinauer691c9f02012-05-23 11:18:35 -0700313 if (device_id == PCI_DEVICE_ID_INTEL_TGP_LPC)
314 return 7;
315
316 if ((device_id >= PCI_DEVICE_ID_INTEL_COUGARPOINT_LPC_MIN &&
317 device_id <= PCI_DEVICE_ID_INTEL_COUGARPOINT_LPC_MAX) ||
318 (device_id >= PCI_DEVICE_ID_INTEL_PANTHERPOINT_LPC_MIN &&
319 device_id <= PCI_DEVICE_ID_INTEL_PANTHERPOINT_LPC_MAX))
320 return 9;
321
322 return 0;
323}
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700324
325void spi_init(void)
326{
327 int ich_version = 0;
328
329 uint8_t *rcrb; /* Root Complex Register Block */
330 uint32_t rcba; /* Root Complex Base Address */
331 uint8_t bios_cntl;
Stefan Reinauer0c32c972012-07-10 13:26:59 -0700332 device_t dev;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700333 uint32_t ids;
334 uint16_t vendor_id, device_id;
335
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700336#ifdef __SMM__
337 dev = PCI_DEV(0, 31, 0);
338#else
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700339 dev = dev_find_slot(0, PCI_DEVFN(31, 0));
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700340#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700341 pci_read_config_dword(dev, 0, &ids);
342 vendor_id = ids;
343 device_id = (ids >> 16);
344
345 if (vendor_id != PCI_VENDOR_ID_INTEL) {
346 printk(BIOS_DEBUG, "ICH SPI: No ICH found.\n");
347 return;
348 }
349
Stefan Reinauer691c9f02012-05-23 11:18:35 -0700350 ich_version = get_ich_version(device_id);
351
352 if (!ich_version) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700353 printk(BIOS_DEBUG, "ICH SPI: No known ICH found.\n");
354 return;
355 }
356
357 pci_read_config_dword(dev, 0xf0, &rcba);
358 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
359 rcrb = (uint8_t *)(rcba & 0xffffc000);
360 switch (ich_version) {
361 case 7:
362 {
363 const uint16_t ich7_spibar_offset = 0x3020;
364 ich7_spi_regs *ich7_spi =
365 (ich7_spi_regs *)(rcrb + ich7_spibar_offset);
366
367 ichspi_lock = readw_(&ich7_spi->spis) & SPIS_LOCK;
368 cntlr.opmenu = ich7_spi->opmenu;
369 cntlr.menubytes = sizeof(ich7_spi->opmenu);
370 cntlr.optype = &ich7_spi->optype;
371 cntlr.addr = &ich7_spi->spia;
372 cntlr.data = (uint8_t *)ich7_spi->spid;
373 cntlr.databytes = sizeof(ich7_spi->spid);
374 cntlr.status = (uint8_t *)&ich7_spi->spis;
375 cntlr.control = &ich7_spi->spic;
376 cntlr.bbar = &ich7_spi->bbar;
377 cntlr.preop = &ich7_spi->preop;
378 break;
379 }
380 case 9:
381 {
382 const uint16_t ich9_spibar_offset = 0x3800;
383 ich9_spi_regs *ich9_spi =
384 (ich9_spi_regs *)(rcrb + ich9_spibar_offset);
385 ichspi_lock = readw_(&ich9_spi->hsfs) & HSFS_FLOCKDN;
386 cntlr.opmenu = ich9_spi->opmenu;
387 cntlr.menubytes = sizeof(ich9_spi->opmenu);
388 cntlr.optype = &ich9_spi->optype;
389 cntlr.addr = &ich9_spi->faddr;
390 cntlr.data = (uint8_t *)ich9_spi->fdata;
391 cntlr.databytes = sizeof(ich9_spi->fdata);
392 cntlr.status = &ich9_spi->ssfs;
393 cntlr.control = (uint16_t *)ich9_spi->ssfc;
394 cntlr.bbar = &ich9_spi->bbar;
395 cntlr.preop = &ich9_spi->preop;
396 break;
397 }
398 default:
399 printk(BIOS_DEBUG, "ICH SPI: Unrecognized ICH version %d.\n", ich_version);
400 }
401
402 ich_set_bbar(0);
403
404 /* Disable the BIOS write protect so write commands are allowed. */
405 pci_read_config_byte(dev, 0xdc, &bios_cntl);
406 switch (ich_version) {
407 case 9:
408 /* Deassert SMM BIOS Write Protect Disable. */
409 bios_cntl &= ~(1 << 5);
410 break;
411
412 default:
413 break;
414 }
415 pci_write_config_byte(dev, 0xdc, bios_cntl | 0x1);
416}
417
418int spi_claim_bus(struct spi_slave *slave)
419{
420 /* Handled by ICH automatically. */
421 return 0;
422}
423
424void spi_release_bus(struct spi_slave *slave)
425{
426 /* Handled by ICH automatically. */
427}
428
429void spi_cs_activate(struct spi_slave *slave)
430{
431 /* Handled by ICH automatically. */
432}
433
434void spi_cs_deactivate(struct spi_slave *slave)
435{
436 /* Handled by ICH automatically. */
437}
438
439typedef struct spi_transaction {
440 const uint8_t *out;
441 uint32_t bytesout;
442 uint8_t *in;
443 uint32_t bytesin;
444 uint8_t type;
445 uint8_t opcode;
446 uint32_t offset;
447} spi_transaction;
448
449static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
450{
451 trans->out += bytes;
452 trans->bytesout -= bytes;
453}
454
455static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
456{
457 trans->in += bytes;
458 trans->bytesin -= bytes;
459}
460
461static void spi_setup_type(spi_transaction *trans)
462{
463 trans->type = 0xFF;
464
465 /* Try to guess spi type from read/write sizes. */
466 if (trans->bytesin == 0) {
467 if (trans->bytesout > 4)
468 /*
469 * If bytesin = 0 and bytesout > 4, we presume this is
470 * a write data operation, which is accompanied by an
471 * address.
472 */
473 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
474 else
475 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
476 return;
477 }
478
479 if (trans->bytesout == 1) { /* and bytesin is > 0 */
480 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
481 return;
482 }
483
484 if (trans->bytesout == 4) { /* and bytesin is > 0 */
485 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
486 }
487}
488
489static int spi_setup_opcode(spi_transaction *trans)
490{
491 uint16_t optypes;
492 uint8_t opmenu[cntlr.menubytes];
493
494 trans->opcode = trans->out[0];
495 spi_use_out(trans, 1);
496 if (!ichspi_lock) {
497 /* The lock is off, so just use index 0. */
498 writeb_(trans->opcode, cntlr.opmenu);
499 optypes = readw_(cntlr.optype);
500 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
501 writew_(optypes, cntlr.optype);
502 return 0;
503 } else {
504 /* The lock is on. See if what we need is on the menu. */
505 uint8_t optype;
506 uint16_t opcode_index;
507
Duncan Lauriea2f1b952012-08-27 11:10:43 -0700508 /* Write Enable is handled as atomic prefix */
509 if (trans->opcode == SPI_OPCODE_WREN)
510 return 0;
511
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700512 read_reg(cntlr.opmenu, opmenu, sizeof(opmenu));
513 for (opcode_index = 0; opcode_index < cntlr.menubytes;
514 opcode_index++) {
515 if (opmenu[opcode_index] == trans->opcode)
516 break;
517 }
518
519 if (opcode_index == cntlr.menubytes) {
520 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
521 trans->opcode);
522 return -1;
523 }
524
525 optypes = readw_(cntlr.optype);
526 optype = (optypes >> (opcode_index * 2)) & 0x3;
527 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
528 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
529 trans->bytesout >= 3) {
530 /* We guessed wrong earlier. Fix it up. */
531 trans->type = optype;
532 }
533 if (optype != trans->type) {
534 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
535 optype);
536 return -1;
537 }
538 return opcode_index;
539 }
540}
541
542static int spi_setup_offset(spi_transaction *trans)
543{
544 /* Separate the SPI address and data. */
545 switch (trans->type) {
546 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
547 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
548 return 0;
549 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
550 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
551 trans->offset = ((uint32_t)trans->out[0] << 16) |
552 ((uint32_t)trans->out[1] << 8) |
553 ((uint32_t)trans->out[2] << 0);
554 spi_use_out(trans, 3);
555 return 1;
556 default:
557 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
558 return -1;
559 }
560}
561
562/*
563 * Wait for up to 60ms til status register bit(s) turn 1 (in case wait_til_set
564 * below is True) or 0. In case the wait was for the bit(s) to set - write
565 * those bits back, which would cause resetting them.
566 *
567 * Return the last read status value on success or -1 on failure.
568 */
569static int ich_status_poll(u16 bitmask, int wait_til_set)
570{
571 int timeout = 6000; /* This will result in 60 ms */
572 u16 status = 0;
573
574 while (timeout--) {
575 status = readw_(cntlr.status);
576 if (wait_til_set ^ ((status & bitmask) == 0)) {
577 if (wait_til_set)
578 writew_((status & bitmask), cntlr.status);
579 return status;
580 }
581 udelay(10);
582 }
583
584 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, expected %x\n",
585 status, bitmask);
586 return -1;
587}
588
589int spi_xfer(struct spi_slave *slave, const void *dout,
590 unsigned int bitsout, void *din, unsigned int bitsin)
591{
592 uint16_t control;
593 int16_t opcode_index;
594 int with_address;
595 int status;
596
597 spi_transaction trans = {
598 dout, bitsout / 8,
599 din, bitsin / 8,
600 0xff, 0xff, 0
601 };
602
603 /* There has to always at least be an opcode. */
604 if (!bitsout || !dout) {
605 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
606 return -1;
607 }
608 /* Make sure if we read something we have a place to put it. */
609 if (bitsin != 0 && !din) {
610 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
611 return -1;
612 }
613 /* Right now we don't support writing partial bytes. */
614 if (bitsout % 8 || bitsin % 8) {
615 printk(BIOS_DEBUG, "ICH SPI: Accessing partial bytes not supported\n");
616 return -1;
617 }
618
619 if (ich_status_poll(SPIS_SCIP, 0) == -1)
620 return -1;
621
622 writew_(SPIS_CDS | SPIS_FCERR, cntlr.status);
623
624 spi_setup_type(&trans);
625 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
626 return -1;
627 if ((with_address = spi_setup_offset(&trans)) < 0)
628 return -1;
629
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700630 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700631 /*
632 * Treat Write Enable as Atomic Pre-Op if possible
633 * in order to prevent the Management Engine from
634 * issuing a transaction between WREN and DATA.
635 */
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700636 if (!ichspi_lock)
637 writew_(trans.opcode, cntlr.preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700638 return 0;
639 }
640
641 /* Preset control fields */
642 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
643
644 /* Issue atomic preop cycle if needed */
645 if (readw_(cntlr.preop))
646 control |= SPIC_ACS;
647
648 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700649 /* SPI addresses are 24 bit only */
650 if (with_address)
651 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
652
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700653 /*
654 * This is a 'no data' command (like Write Enable), its
655 * bitesout size was 1, decremented to zero while executing
656 * spi_setup_opcode() above. Tell the chip to send the
657 * command.
658 */
659 writew_(control, cntlr.control);
660
661 /* wait for the result */
662 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
663 if (status == -1)
664 return -1;
665
666 if (status & SPIS_FCERR) {
667 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
668 return -1;
669 }
670
671 return 0;
672 }
673
674 /*
675 * Check if this is a write command atempting to transfer more bytes
676 * than the controller can handle. Iterations for writes are not
677 * supported here because each SPI write command needs to be preceded
678 * and followed by other SPI commands, and this sequence is controlled
679 * by the SPI chip driver.
680 */
681 if (trans.bytesout > cntlr.databytes) {
682 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
683 " CONTROLLER_PAGE_LIMIT?\n");
684 return -1;
685 }
686
687 /*
688 * Read or write up to databytes bytes at a time until everything has
689 * been sent.
690 */
691 while (trans.bytesout || trans.bytesin) {
692 uint32_t data_length;
693
694 /* SPI addresses are 24 bit only */
695 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
696
697 if (trans.bytesout)
698 data_length = min(trans.bytesout, cntlr.databytes);
699 else
700 data_length = min(trans.bytesin, cntlr.databytes);
701
702 /* Program data into FDATA0 to N */
703 if (trans.bytesout) {
704 write_reg(trans.out, cntlr.data, data_length);
705 spi_use_out(&trans, data_length);
706 if (with_address)
707 trans.offset += data_length;
708 }
709
710 /* Add proper control fields' values */
711 control &= ~((cntlr.databytes - 1) << 8);
712 control |= SPIC_DS;
713 control |= (data_length - 1) << 8;
714
715 /* write it */
716 writew_(control, cntlr.control);
717
718 /* Wait for Cycle Done Status or Flash Cycle Error. */
719 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
720 if (status == -1)
721 return -1;
722
723 if (status & SPIS_FCERR) {
724 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
725 return -1;
726 }
727
728 if (trans.bytesin) {
729 read_reg(cntlr.data, trans.in, data_length);
730 spi_use_in(&trans, data_length);
731 if (with_address)
732 trans.offset += data_length;
733 }
734 }
735
736 /* Clear atomic preop now that xfer is done */
737 writew_(0, cntlr.preop);
738
739 return 0;
740}