blob: 4303dd0e7a6f935b7c71b2b7dfa202ecaf430ee8 [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
Zheng Bao600784e2013-02-07 17:30:23 +080032#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070033
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 }
Duncan Laurie23b00532012-10-10 14:21:23 -0700487
488 /* Fast read command is called with 5 bytes instead of 4 */
489 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
490 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
491 --trans->bytesout;
492 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700493}
494
495static int spi_setup_opcode(spi_transaction *trans)
496{
497 uint16_t optypes;
498 uint8_t opmenu[cntlr.menubytes];
499
500 trans->opcode = trans->out[0];
501 spi_use_out(trans, 1);
502 if (!ichspi_lock) {
503 /* The lock is off, so just use index 0. */
504 writeb_(trans->opcode, cntlr.opmenu);
505 optypes = readw_(cntlr.optype);
506 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
507 writew_(optypes, cntlr.optype);
508 return 0;
509 } else {
510 /* The lock is on. See if what we need is on the menu. */
511 uint8_t optype;
512 uint16_t opcode_index;
513
Duncan Lauriea2f1b952012-08-27 11:10:43 -0700514 /* Write Enable is handled as atomic prefix */
515 if (trans->opcode == SPI_OPCODE_WREN)
516 return 0;
517
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700518 read_reg(cntlr.opmenu, opmenu, sizeof(opmenu));
519 for (opcode_index = 0; opcode_index < cntlr.menubytes;
520 opcode_index++) {
521 if (opmenu[opcode_index] == trans->opcode)
522 break;
523 }
524
525 if (opcode_index == cntlr.menubytes) {
526 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
527 trans->opcode);
528 return -1;
529 }
530
531 optypes = readw_(cntlr.optype);
532 optype = (optypes >> (opcode_index * 2)) & 0x3;
533 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
534 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
535 trans->bytesout >= 3) {
536 /* We guessed wrong earlier. Fix it up. */
537 trans->type = optype;
538 }
539 if (optype != trans->type) {
540 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
541 optype);
542 return -1;
543 }
544 return opcode_index;
545 }
546}
547
548static int spi_setup_offset(spi_transaction *trans)
549{
550 /* Separate the SPI address and data. */
551 switch (trans->type) {
552 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
553 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
554 return 0;
555 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
556 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
557 trans->offset = ((uint32_t)trans->out[0] << 16) |
558 ((uint32_t)trans->out[1] << 8) |
559 ((uint32_t)trans->out[2] << 0);
560 spi_use_out(trans, 3);
561 return 1;
562 default:
563 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
564 return -1;
565 }
566}
567
568/*
569 * Wait for up to 60ms til status register bit(s) turn 1 (in case wait_til_set
570 * below is True) or 0. In case the wait was for the bit(s) to set - write
571 * those bits back, which would cause resetting them.
572 *
573 * Return the last read status value on success or -1 on failure.
574 */
575static int ich_status_poll(u16 bitmask, int wait_til_set)
576{
577 int timeout = 6000; /* This will result in 60 ms */
578 u16 status = 0;
579
580 while (timeout--) {
581 status = readw_(cntlr.status);
582 if (wait_til_set ^ ((status & bitmask) == 0)) {
583 if (wait_til_set)
584 writew_((status & bitmask), cntlr.status);
585 return status;
586 }
587 udelay(10);
588 }
589
590 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, expected %x\n",
591 status, bitmask);
592 return -1;
593}
594
595int spi_xfer(struct spi_slave *slave, const void *dout,
596 unsigned int bitsout, void *din, unsigned int bitsin)
597{
598 uint16_t control;
599 int16_t opcode_index;
600 int with_address;
601 int status;
602
603 spi_transaction trans = {
604 dout, bitsout / 8,
605 din, bitsin / 8,
606 0xff, 0xff, 0
607 };
608
609 /* There has to always at least be an opcode. */
610 if (!bitsout || !dout) {
611 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
612 return -1;
613 }
614 /* Make sure if we read something we have a place to put it. */
615 if (bitsin != 0 && !din) {
616 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
617 return -1;
618 }
619 /* Right now we don't support writing partial bytes. */
620 if (bitsout % 8 || bitsin % 8) {
621 printk(BIOS_DEBUG, "ICH SPI: Accessing partial bytes not supported\n");
622 return -1;
623 }
624
625 if (ich_status_poll(SPIS_SCIP, 0) == -1)
626 return -1;
627
628 writew_(SPIS_CDS | SPIS_FCERR, cntlr.status);
629
630 spi_setup_type(&trans);
631 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
632 return -1;
633 if ((with_address = spi_setup_offset(&trans)) < 0)
634 return -1;
635
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700636 if (trans.opcode == SPI_OPCODE_WREN) {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700637 /*
638 * Treat Write Enable as Atomic Pre-Op if possible
639 * in order to prevent the Management Engine from
640 * issuing a transaction between WREN and DATA.
641 */
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700642 if (!ichspi_lock)
643 writew_(trans.opcode, cntlr.preop);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700644 return 0;
645 }
646
647 /* Preset control fields */
648 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
649
650 /* Issue atomic preop cycle if needed */
651 if (readw_(cntlr.preop))
652 control |= SPIC_ACS;
653
654 if (!trans.bytesout && !trans.bytesin) {
Duncan Laurie3beb6db2012-09-01 13:44:17 -0700655 /* SPI addresses are 24 bit only */
656 if (with_address)
657 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
658
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700659 /*
660 * This is a 'no data' command (like Write Enable), its
661 * bitesout size was 1, decremented to zero while executing
662 * spi_setup_opcode() above. Tell the chip to send the
663 * command.
664 */
665 writew_(control, cntlr.control);
666
667 /* wait for the result */
668 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
669 if (status == -1)
670 return -1;
671
672 if (status & SPIS_FCERR) {
673 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
674 return -1;
675 }
676
677 return 0;
678 }
679
680 /*
681 * Check if this is a write command atempting to transfer more bytes
682 * than the controller can handle. Iterations for writes are not
683 * supported here because each SPI write command needs to be preceded
684 * and followed by other SPI commands, and this sequence is controlled
685 * by the SPI chip driver.
686 */
687 if (trans.bytesout > cntlr.databytes) {
688 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
689 " CONTROLLER_PAGE_LIMIT?\n");
690 return -1;
691 }
692
693 /*
694 * Read or write up to databytes bytes at a time until everything has
695 * been sent.
696 */
697 while (trans.bytesout || trans.bytesin) {
698 uint32_t data_length;
699
700 /* SPI addresses are 24 bit only */
701 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
702
703 if (trans.bytesout)
704 data_length = min(trans.bytesout, cntlr.databytes);
705 else
706 data_length = min(trans.bytesin, cntlr.databytes);
707
708 /* Program data into FDATA0 to N */
709 if (trans.bytesout) {
710 write_reg(trans.out, cntlr.data, data_length);
711 spi_use_out(&trans, data_length);
712 if (with_address)
713 trans.offset += data_length;
714 }
715
716 /* Add proper control fields' values */
717 control &= ~((cntlr.databytes - 1) << 8);
718 control |= SPIC_DS;
719 control |= (data_length - 1) << 8;
720
721 /* write it */
722 writew_(control, cntlr.control);
723
724 /* Wait for Cycle Done Status or Flash Cycle Error. */
725 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
726 if (status == -1)
727 return -1;
728
729 if (status & SPIS_FCERR) {
730 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
731 return -1;
732 }
733
734 if (trans.bytesin) {
735 read_reg(cntlr.data, trans.in, data_length);
736 spi_use_in(&trans, data_length);
737 if (with_address)
738 trans.offset += data_length;
739 }
740 }
741
742 /* Clear atomic preop now that xfer is done */
743 writew_(0, cntlr.preop);
744
745 return 0;
746}