2 * Copyright (c) 2011-12 The Chromium OS Authors.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but without any warranty; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19 * This file is derived from the flashrom project.
31 #define SPI_OPCODE_WREN 0x06
32 #define SPI_OPCODE_FAST_READ 0x0b
35 pci_dev_t dev; /* PCI device number */
36 int ich_version; /* Controller version, 7 or 9 */
41 void *base; /* Base of register set */
50 uint32_t *pr; /* only for ich9 */
51 uint8_t *speed; /* pointer to speed control */
52 ulong max_speed; /* Maximum bus speed in MHz */
57 static inline struct ich_spi_slave *to_ich_spi(struct spi_slave *slave)
59 return container_of(slave, struct ich_spi_slave, slave);
62 static unsigned int ich_reg(const void *addr)
64 return (unsigned)(addr - ctlr.base) & 0xffff;
67 static u8 ich_readb(const void *addr)
69 u8 value = readb(addr);
71 debug("read %2.2x from %4.4x\n", value, ich_reg(addr));
76 static u16 ich_readw(const void *addr)
78 u16 value = readw(addr);
80 debug("read %4.4x from %4.4x\n", value, ich_reg(addr));
85 static u32 ich_readl(const void *addr)
87 u32 value = readl(addr);
89 debug("read %8.8x from %4.4x\n", value, ich_reg(addr));
94 static void ich_writeb(u8 value, void *addr)
97 debug("wrote %2.2x to %4.4x\n", value, ich_reg(addr));
100 static void ich_writew(u16 value, void *addr)
103 debug("wrote %4.4x to %4.4x\n", value, ich_reg(addr));
106 static void ich_writel(u32 value, void *addr)
109 debug("wrote %8.8x to %4.4x\n", value, ich_reg(addr));
112 static void write_reg(const void *value, void *dest, uint32_t size)
114 memcpy_toio(dest, value, size);
117 static void read_reg(const void *src, void *value, uint32_t size)
119 memcpy_fromio(value, src, size);
122 static void ich_set_bbar(struct ich_ctlr *ctlr, uint32_t minaddr)
124 const uint32_t bbar_mask = 0x00ffff00;
125 uint32_t ichspi_bbar;
127 minaddr &= bbar_mask;
128 ichspi_bbar = ich_readl(ctlr->bbar) & ~bbar_mask;
129 ichspi_bbar |= minaddr;
130 ich_writel(ichspi_bbar, ctlr->bbar);
133 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
135 puts("spi_cs_is_valid used but not implemented\n");
139 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
140 unsigned int max_hz, unsigned int mode)
142 struct ich_spi_slave *ich;
144 ich = spi_alloc_slave(struct ich_spi_slave, bus, cs);
146 puts("ICH SPI: Out of memory\n");
151 * Yes this controller can only write a small number of bytes at
152 * once! The limit is typically 64 bytes.
154 ich->slave.max_write_size = ctlr.databytes;
160 void spi_free_slave(struct spi_slave *slave)
162 struct ich_spi_slave *ich = to_ich_spi(slave);
168 * Check if this device ID matches one of supported Intel PCH devices.
170 * Return the ICH version if there is a match, or zero otherwise.
172 static int get_ich_version(uint16_t device_id)
174 if (device_id == PCI_DEVICE_ID_INTEL_TGP_LPC)
177 if ((device_id >= PCI_DEVICE_ID_INTEL_COUGARPOINT_LPC_MIN &&
178 device_id <= PCI_DEVICE_ID_INTEL_COUGARPOINT_LPC_MAX) ||
179 (device_id >= PCI_DEVICE_ID_INTEL_PANTHERPOINT_LPC_MIN &&
180 device_id <= PCI_DEVICE_ID_INTEL_PANTHERPOINT_LPC_MAX))
186 /* @return 1 if the SPI flash supports the 33MHz speed */
187 static int ich9_can_do_33mhz(pci_dev_t dev)
191 /* Observe SPI Descriptor Component Section 0 */
192 pci_write_config_dword(dev, 0xb0, 0x1000);
194 /* Extract the Write/Erase SPI Frequency from descriptor */
195 pci_read_config_dword(dev, 0xb4, &fdod);
197 /* Bits 23:21 have the fast read clock frequency, 0=20MHz, 1=33MHz */
198 speed = (fdod >> 21) & 7;
203 static int ich_find_spi_controller(pci_dev_t *devp, int *ich_versionp)
205 int last_bus = pci_last_busno();
208 if (last_bus == -1) {
209 debug("No PCI busses?\n");
213 for (bus = 0; bus <= last_bus; bus++) {
214 uint16_t vendor_id, device_id;
218 dev = PCI_BDF(bus, 31, 0);
219 pci_read_config_dword(dev, 0, &ids);
221 device_id = ids >> 16;
223 if (vendor_id == PCI_VENDOR_ID_INTEL) {
225 *ich_versionp = get_ich_version(device_id);
230 debug("ICH SPI: No ICH found.\n");
234 static int ich_init_controller(struct ich_ctlr *ctlr)
236 uint8_t *rcrb; /* Root Complex Register Block */
237 uint32_t rcba; /* Root Complex Base Address */
239 pci_read_config_dword(ctlr->dev, 0xf0, &rcba);
240 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
241 rcrb = (uint8_t *)(rcba & 0xffffc000);
242 if (ctlr->ich_version == 7) {
243 struct ich7_spi_regs *ich7_spi;
245 ich7_spi = (struct ich7_spi_regs *)(rcrb + 0x3020);
246 ctlr->ichspi_lock = ich_readw(&ich7_spi->spis) & SPIS_LOCK;
247 ctlr->opmenu = ich7_spi->opmenu;
248 ctlr->menubytes = sizeof(ich7_spi->opmenu);
249 ctlr->optype = &ich7_spi->optype;
250 ctlr->addr = &ich7_spi->spia;
251 ctlr->data = (uint8_t *)ich7_spi->spid;
252 ctlr->databytes = sizeof(ich7_spi->spid);
253 ctlr->status = (uint8_t *)&ich7_spi->spis;
254 ctlr->control = &ich7_spi->spic;
255 ctlr->bbar = &ich7_spi->bbar;
256 ctlr->preop = &ich7_spi->preop;
257 ctlr->base = ich7_spi;
258 } else if (ctlr->ich_version == 9) {
259 struct ich9_spi_regs *ich9_spi;
261 ich9_spi = (struct ich9_spi_regs *)(rcrb + 0x3800);
262 ctlr->ichspi_lock = ich_readw(&ich9_spi->hsfs) & HSFS_FLOCKDN;
263 ctlr->opmenu = ich9_spi->opmenu;
264 ctlr->menubytes = sizeof(ich9_spi->opmenu);
265 ctlr->optype = &ich9_spi->optype;
266 ctlr->addr = &ich9_spi->faddr;
267 ctlr->data = (uint8_t *)ich9_spi->fdata;
268 ctlr->databytes = sizeof(ich9_spi->fdata);
269 ctlr->status = &ich9_spi->ssfs;
270 ctlr->control = (uint16_t *)ich9_spi->ssfc;
271 ctlr->speed = ich9_spi->ssfc + 2;
272 ctlr->bbar = &ich9_spi->bbar;
273 ctlr->preop = &ich9_spi->preop;
274 ctlr->pr = &ich9_spi->pr[0];
275 ctlr->base = ich9_spi;
277 debug("ICH SPI: Unrecognized ICH version %d.\n",
281 debug("ICH SPI: Version %d detected\n", ctlr->ich_version);
283 /* Work out the maximum speed we can support */
284 ctlr->max_speed = 20000000;
285 if (ctlr->ich_version == 9 && ich9_can_do_33mhz(ctlr->dev))
286 ctlr->max_speed = 33000000;
288 ich_set_bbar(ctlr, 0);
297 if (ich_find_spi_controller(&ctlr.dev, &ctlr.ich_version)) {
298 printf("ICH SPI: Cannot find device\n");
302 if (ich_init_controller(&ctlr)) {
303 printf("ICH SPI: Cannot setup controller\n");
308 * Disable the BIOS write protect so write commands are allowed. On
309 * v9, deassert SMM BIOS Write Protect Disable.
311 pci_read_config_byte(ctlr.dev, 0xdc, &bios_cntl);
312 if (ctlr.ich_version == 9)
313 bios_cntl &= ~(1 << 5);
314 pci_write_config_byte(ctlr.dev, 0xdc, bios_cntl | 0x1);
317 int spi_claim_bus(struct spi_slave *slave)
319 /* Handled by ICH automatically. */
323 void spi_release_bus(struct spi_slave *slave)
325 /* Handled by ICH automatically. */
328 void spi_cs_activate(struct spi_slave *slave)
330 /* Handled by ICH automatically. */
333 void spi_cs_deactivate(struct spi_slave *slave)
335 /* Handled by ICH automatically. */
338 static inline void spi_use_out(struct spi_trans *trans, unsigned bytes)
341 trans->bytesout -= bytes;
344 static inline void spi_use_in(struct spi_trans *trans, unsigned bytes)
347 trans->bytesin -= bytes;
350 static void spi_setup_type(struct spi_trans *trans, int data_bytes)
354 /* Try to guess spi type from read/write sizes. */
355 if (trans->bytesin == 0) {
356 if (trans->bytesout + data_bytes > 4)
358 * If bytesin = 0 and bytesout > 4, we presume this is
359 * a write data operation, which is accompanied by an
362 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
364 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
368 if (trans->bytesout == 1) { /* and bytesin is > 0 */
369 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
373 if (trans->bytesout == 4) /* and bytesin is > 0 */
374 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
376 /* Fast read command is called with 5 bytes instead of 4 */
377 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
378 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
383 static int spi_setup_opcode(struct spi_trans *trans)
386 uint8_t opmenu[ctlr.menubytes];
388 trans->opcode = trans->out[0];
389 spi_use_out(trans, 1);
390 if (!ctlr.ichspi_lock) {
391 /* The lock is off, so just use index 0. */
392 ich_writeb(trans->opcode, ctlr.opmenu);
393 optypes = ich_readw(ctlr.optype);
394 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
395 ich_writew(optypes, ctlr.optype);
398 /* The lock is on. See if what we need is on the menu. */
400 uint16_t opcode_index;
402 /* Write Enable is handled as atomic prefix */
403 if (trans->opcode == SPI_OPCODE_WREN)
406 read_reg(ctlr.opmenu, opmenu, sizeof(opmenu));
407 for (opcode_index = 0; opcode_index < ctlr.menubytes;
409 if (opmenu[opcode_index] == trans->opcode)
413 if (opcode_index == ctlr.menubytes) {
414 printf("ICH SPI: Opcode %x not found\n",
419 optypes = ich_readw(ctlr.optype);
420 optype = (optypes >> (opcode_index * 2)) & 0x3;
421 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
422 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
423 trans->bytesout >= 3) {
424 /* We guessed wrong earlier. Fix it up. */
425 trans->type = optype;
427 if (optype != trans->type) {
428 printf("ICH SPI: Transaction doesn't fit type %d\n",
436 static int spi_setup_offset(struct spi_trans *trans)
438 /* Separate the SPI address and data. */
439 switch (trans->type) {
440 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
441 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
443 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
444 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
445 trans->offset = ((uint32_t)trans->out[0] << 16) |
446 ((uint32_t)trans->out[1] << 8) |
447 ((uint32_t)trans->out[2] << 0);
448 spi_use_out(trans, 3);
451 printf("Unrecognized SPI transaction type %#x\n", trans->type);
457 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
458 * below is true) or 0. In case the wait was for the bit(s) to set - write
459 * those bits back, which would cause resetting them.
461 * Return the last read status value on success or -1 on failure.
463 static int ich_status_poll(u16 bitmask, int wait_til_set)
465 int timeout = 600000; /* This will result in 6s */
469 status = ich_readw(ctlr.status);
470 if (wait_til_set ^ ((status & bitmask) == 0)) {
472 ich_writew((status & bitmask), ctlr.status);
478 printf("ICH SPI: SCIP timeout, read %x, expected %x\n",
484 int spi_xfer(struct spi_slave *slave, const void *dout,
485 unsigned int bitsout, void *din, unsigned int bitsin)
487 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
488 void *din, unsigned long flags)
490 struct ich_spi_slave *ich = to_ich_spi(slave);
492 int16_t opcode_index;
495 int bytes = bitlen / 8;
496 struct spi_trans *trans = &ich->trans;
497 unsigned type = flags & (SPI_XFER_BEGIN | SPI_XFER_END);
499 /* Align read transactions to 64-byte boundaries */
500 char buff[ctlr.databytes];
502 /* Ee don't support writing partial bytes. */
504 debug("ICH SPI: Accessing partial bytes not supported\n");
508 /* An empty end transaction can be ignored */
509 if (type == SPI_XFER_END && !dout && !din)
512 if (type & SPI_XFER_BEGIN)
513 memset(trans, '\0', sizeof(*trans));
515 /* Dp we need to come back later to finish it? */
516 if (dout && type == SPI_XFER_BEGIN) {
517 if (bytes > ICH_MAX_CMD_LEN) {
518 debug("ICH SPI: Command length limit exceeded\n");
521 memcpy(trans->cmd, dout, bytes);
522 trans->cmd_len = bytes;
523 debug("ICH SPI: Saved %d bytes\n", bytes);
528 * We process a 'middle' spi_xfer() call, which has no
529 * SPI_XFER_BEGIN/END, as an independent transaction as if it had
530 * an end. We therefore repeat the command. This is because ICH
531 * seems to have no support for this, or because interest (in digging
532 * out the details and creating a special case in the code) is low.
534 if (trans->cmd_len) {
535 trans->out = trans->cmd;
536 trans->bytesout = trans->cmd_len;
538 debug("ICH SPI: Using %d bytes\n", trans->cmd_len);
541 trans->bytesout = dout ? bytes : 0;
545 trans->bytesin = din ? bytes : 0;
547 /* There has to always at least be an opcode. */
548 if (!trans->bytesout) {
549 debug("ICH SPI: No opcode for transfer\n");
553 if (ich_status_poll(SPIS_SCIP, 0) == -1)
556 ich_writew(SPIS_CDS | SPIS_FCERR, ctlr.status);
558 spi_setup_type(trans, using_cmd ? bytes : 0);
559 opcode_index = spi_setup_opcode(trans);
560 if (opcode_index < 0)
562 with_address = spi_setup_offset(trans);
563 if (with_address < 0)
566 if (trans->opcode == SPI_OPCODE_WREN) {
568 * Treat Write Enable as Atomic Pre-Op if possible
569 * in order to prevent the Management Engine from
570 * issuing a transaction between WREN and DATA.
572 if (!ctlr.ichspi_lock)
573 ich_writew(trans->opcode, ctlr.preop);
577 if (ctlr.speed && ctlr.max_speed >= 33000000) {
580 byte = ich_readb(ctlr.speed);
581 if (ich->speed >= 33000000)
582 byte |= SSFC_SCF_33MHZ;
584 byte &= ~SSFC_SCF_33MHZ;
585 ich_writeb(byte, ctlr.speed);
588 /* See if we have used up the command data */
589 if (using_cmd && dout && bytes) {
591 trans->bytesout = bytes;
592 debug("ICH SPI: Moving to data, %d bytes\n", bytes);
595 /* Preset control fields */
596 control = ich_readw(ctlr.control);
597 control &= ~SSFC_RESERVED;
598 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
600 /* Issue atomic preop cycle if needed */
601 if (ich_readw(ctlr.preop))
604 if (!trans->bytesout && !trans->bytesin) {
605 /* SPI addresses are 24 bit only */
607 ich_writel(trans->offset & 0x00FFFFFF, ctlr.addr);
610 * This is a 'no data' command (like Write Enable), its
611 * bitesout size was 1, decremented to zero while executing
612 * spi_setup_opcode() above. Tell the chip to send the
615 ich_writew(control, ctlr.control);
617 /* wait for the result */
618 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
622 if (status & SPIS_FCERR) {
623 debug("ICH SPI: Command transaction error\n");
631 * Check if this is a write command atempting to transfer more bytes
632 * than the controller can handle. Iterations for writes are not
633 * supported here because each SPI write command needs to be preceded
634 * and followed by other SPI commands, and this sequence is controlled
635 * by the SPI chip driver.
637 if (trans->bytesout > ctlr.databytes) {
638 debug("ICH SPI: Too much to write. This should be prevented by the driver's max_write_size?\n");
643 * Read or write up to databytes bytes at a time until everything has
646 while (trans->bytesout || trans->bytesin) {
647 uint32_t data_length;
648 uint32_t aligned_offset;
651 aligned_offset = trans->offset & ~(ctlr.databytes - 1);
652 diff = trans->offset - aligned_offset;
654 /* SPI addresses are 24 bit only */
655 ich_writel(aligned_offset & 0x00FFFFFF, ctlr.addr);
658 data_length = min(trans->bytesout, ctlr.databytes);
660 data_length = min(trans->bytesin, ctlr.databytes);
662 /* Program data into FDATA0 to N */
663 if (trans->bytesout) {
664 write_reg(trans->out, ctlr.data, data_length);
665 spi_use_out(trans, data_length);
667 trans->offset += data_length;
670 /* Add proper control fields' values */
671 control &= ~((ctlr.databytes - 1) << 8);
673 control |= (data_length - 1) << 8;
676 ich_writew(control, ctlr.control);
678 /* Wait for Cycle Done Status or Flash Cycle Error. */
679 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
683 if (status & SPIS_FCERR) {
684 debug("ICH SPI: Data transaction error\n");
688 if (trans->bytesin) {
691 read_reg(ctlr.data, buff, ctlr.databytes);
692 memcpy(trans->in, buff + diff, data_length);
694 read_reg(ctlr.data, trans->in, data_length);
696 spi_use_in(trans, data_length);
698 trans->offset += data_length;
702 /* Clear atomic preop now that xfer is done */
703 ich_writew(0, ctlr.preop);
710 * This uses the SPI controller from the Intel Cougar Point and Panther Point
711 * PCH to write-protect portions of the SPI flash until reboot. The changes
712 * don't actually take effect until the HSFS[FLOCKDN] bit is set, but that's
715 int spi_write_protect_region(uint32_t lower_limit, uint32_t length, int hint)
718 uint32_t upper_limit;
721 printf("%s: operation not supported on this chipset\n",
727 lower_limit > (0xFFFFFFFFUL - length) + 1 ||
728 hint < 0 || hint > 4) {
729 printf("%s(0x%x, 0x%x, %d): invalid args\n", __func__,
730 lower_limit, length, hint);
734 upper_limit = lower_limit + length - 1;
737 * Determine bits to write, as follows:
738 * 31 Write-protection enable (includes erase operation)
740 * 28:16 Upper Limit (FLA address bits 24:12, with 11:0 == 0xfff)
741 * 15 Read-protection enable
743 * 12:0 Lower Limit (FLA address bits 24:12, with 11:0 == 0x000)
745 tmplong = 0x80000000 |
746 ((upper_limit & 0x01fff000) << 4) |
747 ((lower_limit & 0x01fff000) >> 12);
749 printf("%s: writing 0x%08x to %p\n", __func__, tmplong,
751 ctlr.pr[hint] = tmplong;