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