tree: drop last paragraph of GPL copyright header
[coreboot.git] / src / soc / intel / broadwell / spi.c
blobe07b42513deff3b21404c7591ec6950a1336a622
1 /*
2 * Copyright (C) 2014 Google Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 /* This file is derived from the flashrom project. */
15 #include <stdint.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <bootstate.h>
19 #include <delay.h>
20 #include <arch/io.h>
21 #include <console/console.h>
22 #include <device/pci_ids.h>
23 #include <spi-generic.h>
24 #include <soc/pci_devs.h>
25 #include <soc/rcba.h>
26 #include <soc/spi.h>
28 #ifdef __SMM__
29 #define pci_read_config_byte(dev, reg, targ)\
30 *(targ) = pci_read_config8(dev, reg)
31 #define pci_read_config_word(dev, reg, targ)\
32 *(targ) = pci_read_config16(dev, reg)
33 #define pci_read_config_dword(dev, reg, targ)\
34 *(targ) = pci_read_config32(dev, reg)
35 #define pci_write_config_byte(dev, reg, val)\
36 pci_write_config8(dev, reg, val)
37 #define pci_write_config_word(dev, reg, val)\
38 pci_write_config16(dev, reg, val)
39 #define pci_write_config_dword(dev, reg, val)\
40 pci_write_config32(dev, reg, val)
41 #else /* !__SMM__ */
42 #include <device/device.h>
43 #include <device/pci.h>
44 #define pci_read_config_byte(dev, reg, targ)\
45 *(targ) = pci_read_config8(dev, reg)
46 #define pci_read_config_word(dev, reg, targ)\
47 *(targ) = pci_read_config16(dev, reg)
48 #define pci_read_config_dword(dev, reg, targ)\
49 *(targ) = pci_read_config32(dev, reg)
50 #define pci_write_config_byte(dev, reg, val)\
51 pci_write_config8(dev, reg, val)
52 #define pci_write_config_word(dev, reg, val)\
53 pci_write_config16(dev, reg, val)
54 #define pci_write_config_dword(dev, reg, val)\
55 pci_write_config32(dev, reg, val)
56 #endif /* !__SMM__ */
58 typedef struct spi_slave ich_spi_slave;
60 static int ichspi_lock = 0;
62 typedef struct ich9_spi_regs {
63 uint32_t bfpr;
64 uint16_t hsfs;
65 uint16_t hsfc;
66 uint32_t faddr;
67 uint32_t _reserved0;
68 uint32_t fdata[16];
69 uint32_t frap;
70 uint32_t freg[5];
71 uint32_t _reserved1[3];
72 uint32_t pr[5];
73 uint32_t _reserved2[2];
74 uint8_t ssfs;
75 uint8_t ssfc[3];
76 uint16_t preop;
77 uint16_t optype;
78 uint8_t opmenu[8];
79 uint32_t bbar;
80 uint8_t _reserved3[12];
81 uint32_t fdoc;
82 uint32_t fdod;
83 uint8_t _reserved4[8];
84 uint32_t afc;
85 uint32_t lvscc;
86 uint32_t uvscc;
87 uint8_t _reserved5[4];
88 uint32_t fpb;
89 uint8_t _reserved6[28];
90 uint32_t srdl;
91 uint32_t srdc;
92 uint32_t srd;
93 } __attribute__((packed)) ich9_spi_regs;
95 typedef struct ich_spi_controller {
96 int locked;
98 uint8_t *opmenu;
99 int menubytes;
100 uint16_t *preop;
101 uint16_t *optype;
102 uint32_t *addr;
103 uint8_t *data;
104 unsigned databytes;
105 uint8_t *status;
106 uint16_t *control;
107 uint32_t *bbar;
108 } ich_spi_controller;
110 static ich_spi_controller cntlr;
112 enum {
113 SPIS_SCIP = 0x0001,
114 SPIS_GRANT = 0x0002,
115 SPIS_CDS = 0x0004,
116 SPIS_FCERR = 0x0008,
117 SSFS_AEL = 0x0010,
118 SPIS_LOCK = 0x8000,
119 SPIS_RESERVED_MASK = 0x7ff0,
120 SSFS_RESERVED_MASK = 0x7fe2
123 enum {
124 SPIC_SCGO = 0x000002,
125 SPIC_ACS = 0x000004,
126 SPIC_SPOP = 0x000008,
127 SPIC_DBC = 0x003f00,
128 SPIC_DS = 0x004000,
129 SPIC_SME = 0x008000,
130 SSFC_SCF_MASK = 0x070000,
131 SSFC_RESERVED = 0xf80000
134 enum {
135 HSFS_FDONE = 0x0001,
136 HSFS_FCERR = 0x0002,
137 HSFS_AEL = 0x0004,
138 HSFS_BERASE_MASK = 0x0018,
139 HSFS_BERASE_SHIFT = 3,
140 HSFS_SCIP = 0x0020,
141 HSFS_FDOPSS = 0x2000,
142 HSFS_FDV = 0x4000,
143 HSFS_FLOCKDN = 0x8000
146 enum {
147 HSFC_FGO = 0x0001,
148 HSFC_FCYCLE_MASK = 0x0006,
149 HSFC_FCYCLE_SHIFT = 1,
150 HSFC_FDBC_MASK = 0x3f00,
151 HSFC_FDBC_SHIFT = 8,
152 HSFC_FSMIE = 0x8000
155 enum {
156 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
157 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
158 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
159 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
162 #if CONFIG_DEBUG_SPI_FLASH
164 static u8 readb_(const void *addr)
166 u8 v = read8(addr);
167 printk(BIOS_DEBUG, "read %2.2x from %4.4x\n",
168 v, ((unsigned) addr & 0xffff) - 0xf020);
169 return v;
172 static u16 readw_(const void *addr)
174 u16 v = read16(addr);
175 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
176 v, ((unsigned) addr & 0xffff) - 0xf020);
177 return v;
180 static u32 readl_(const void *addr)
182 u32 v = read32(addr);
183 printk(BIOS_DEBUG, "read %8.8x from %4.4x\n",
184 v, ((unsigned) addr & 0xffff) - 0xf020);
185 return v;
188 static void writeb_(u8 b, const void *addr)
190 write8(addr, b);
191 printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n",
192 b, ((unsigned) addr & 0xffff) - 0xf020);
195 static void writew_(u16 b, const void *addr)
197 write16(addr, b);
198 printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n",
199 b, ((unsigned) addr & 0xffff) - 0xf020);
202 static void writel_(u32 b, const void *addr)
204 write32(addr, b);
205 printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n",
206 b, ((unsigned) addr & 0xffff) - 0xf020);
209 #else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
211 #define readb_(a) read8(a)
212 #define readw_(a) read16(a)
213 #define readl_(a) read32(a)
214 #define writeb_(val, addr) write8(addr, val)
215 #define writew_(val, addr) write16(addr, val)
216 #define writel_(val, addr) write32(addr, val)
218 #endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
220 static void write_reg(const void *value, void *dest, uint32_t size)
222 const uint8_t *bvalue = value;
223 uint8_t *bdest = dest;
225 while (size >= 4) {
226 writel_(*(const uint32_t *)bvalue, bdest);
227 bdest += 4; bvalue += 4; size -= 4;
229 while (size) {
230 writeb_(*bvalue, bdest);
231 bdest++; bvalue++; size--;
235 static void read_reg(const void *src, void *value, uint32_t size)
237 const uint8_t *bsrc = src;
238 uint8_t *bvalue = value;
240 while (size >= 4) {
241 *(uint32_t *)bvalue = readl_(bsrc);
242 bsrc += 4; bvalue += 4; size -= 4;
244 while (size) {
245 *bvalue = readb_(bsrc);
246 bsrc++; bvalue++; size--;
250 static void ich_set_bbar(uint32_t minaddr)
252 const uint32_t bbar_mask = 0x00ffff00;
253 uint32_t ichspi_bbar;
255 minaddr &= bbar_mask;
256 ichspi_bbar = readl_(cntlr.bbar) & ~bbar_mask;
257 ichspi_bbar |= minaddr;
258 writel_(ichspi_bbar, cntlr.bbar);
261 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs)
263 ich_spi_slave *slave = malloc(sizeof(*slave));
265 if (!slave) {
266 printk(BIOS_DEBUG, "ICH SPI: Bad allocation\n");
267 return NULL;
270 memset(slave, 0, sizeof(*slave));
272 slave->bus = bus;
273 slave->cs = cs;
274 return slave;
277 void spi_init(void)
279 uint8_t *rcrb; /* Root Complex Register Block */
280 uint32_t rcba; /* Root Complex Base Address */
281 uint8_t bios_cntl;
282 device_t dev = PCH_DEV_LPC;
283 ich9_spi_regs *ich9_spi;
285 pci_read_config_dword(dev, 0xf0, &rcba);
286 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
287 rcrb = (uint8_t *)(rcba & 0xffffc000);
288 ich9_spi = (ich9_spi_regs *)(rcrb + 0x3800);
289 ichspi_lock = readw_(&ich9_spi->hsfs) & HSFS_FLOCKDN;
290 cntlr.opmenu = ich9_spi->opmenu;
291 cntlr.menubytes = sizeof(ich9_spi->opmenu);
292 cntlr.optype = &ich9_spi->optype;
293 cntlr.addr = &ich9_spi->faddr;
294 cntlr.data = (uint8_t *)ich9_spi->fdata;
295 cntlr.databytes = sizeof(ich9_spi->fdata);
296 cntlr.status = &ich9_spi->ssfs;
297 cntlr.control = (uint16_t *)ich9_spi->ssfc;
298 cntlr.bbar = &ich9_spi->bbar;
299 cntlr.preop = &ich9_spi->preop;
300 ich_set_bbar(0);
302 /* Disable the BIOS write protect so write commands are allowed. */
303 pci_read_config_byte(dev, 0xdc, &bios_cntl);
304 bios_cntl &= ~(1 << 5);
305 pci_write_config_byte(dev, 0xdc, bios_cntl | 0x1);
308 static void spi_init_cb(void *unused)
310 spi_init();
313 BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
315 int spi_claim_bus(struct spi_slave *slave)
317 /* Handled by ICH automatically. */
318 return 0;
321 void spi_release_bus(struct spi_slave *slave)
323 /* Handled by ICH automatically. */
326 typedef struct spi_transaction {
327 const uint8_t *out;
328 uint32_t bytesout;
329 uint8_t *in;
330 uint32_t bytesin;
331 uint8_t type;
332 uint8_t opcode;
333 uint32_t offset;
334 } spi_transaction;
336 static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
338 trans->out += bytes;
339 trans->bytesout -= bytes;
342 static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
344 trans->in += bytes;
345 trans->bytesin -= bytes;
348 static void spi_setup_type(spi_transaction *trans)
350 trans->type = 0xFF;
352 /* Try to guess spi type from read/write sizes. */
353 if (trans->bytesin == 0) {
354 if (trans->bytesout > 4)
356 * If bytesin = 0 and bytesout > 4, we presume this is
357 * a write data operation, which is accompanied by an
358 * address.
360 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
361 else
362 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
363 return;
366 if (trans->bytesout == 1) { /* and bytesin is > 0 */
367 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
368 return;
371 if (trans->bytesout == 4) { /* and bytesin is > 0 */
372 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
375 /* Fast read command is called with 5 bytes instead of 4 */
376 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
377 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
378 --trans->bytesout;
382 static int spi_setup_opcode(spi_transaction *trans)
384 uint16_t optypes;
385 uint8_t opmenu[cntlr.menubytes];
387 trans->opcode = trans->out[0];
388 spi_use_out(trans, 1);
389 if (!ichspi_lock) {
390 /* The lock is off, so just use index 0. */
391 writeb_(trans->opcode, cntlr.opmenu);
392 optypes = readw_(cntlr.optype);
393 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
394 writew_(optypes, cntlr.optype);
395 return 0;
396 } else {
397 /* The lock is on. See if what we need is on the menu. */
398 uint8_t optype;
399 uint16_t opcode_index;
401 /* Write Enable is handled as atomic prefix */
402 if (trans->opcode == SPI_OPCODE_WREN)
403 return 0;
405 read_reg(cntlr.opmenu, opmenu, sizeof(opmenu));
406 for (opcode_index = 0; opcode_index < cntlr.menubytes;
407 opcode_index++) {
408 if (opmenu[opcode_index] == trans->opcode)
409 break;
412 if (opcode_index == cntlr.menubytes) {
413 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
414 trans->opcode);
415 return -1;
418 optypes = readw_(cntlr.optype);
419 optype = (optypes >> (opcode_index * 2)) & 0x3;
420 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
421 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
422 trans->bytesout >= 3) {
423 /* We guessed wrong earlier. Fix it up. */
424 trans->type = optype;
426 if (optype != trans->type) {
427 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
428 optype);
429 return -1;
431 return opcode_index;
435 static int spi_setup_offset(spi_transaction *trans)
437 /* Separate the SPI address and data. */
438 switch (trans->type) {
439 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
440 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
441 return 0;
442 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
443 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
444 trans->offset = ((uint32_t)trans->out[0] << 16) |
445 ((uint32_t)trans->out[1] << 8) |
446 ((uint32_t)trans->out[2] << 0);
447 spi_use_out(trans, 3);
448 return 1;
449 default:
450 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
451 return -1;
456 * Wait for up to 60ms til status register bit(s) turn 1 (in case wait_til_set
457 * below is True) or 0. In case the wait was for the bit(s) to set - write
458 * those bits back, which would cause resetting them.
460 * Return the last read status value on success or -1 on failure.
462 static int ich_status_poll(u16 bitmask, int wait_til_set)
464 int timeout = 6000; /* This will result in 60 ms */
465 u16 status = 0;
467 while (timeout--) {
468 status = readw_(cntlr.status);
469 if (wait_til_set ^ ((status & bitmask) == 0)) {
470 if (wait_til_set)
471 writew_((status & bitmask), cntlr.status);
472 return status;
474 udelay(10);
477 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, expected %x\n",
478 status, bitmask);
479 return -1;
482 unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len)
484 return min(cntlr.databytes, buf_len);
487 int spi_xfer(struct spi_slave *slave, const void *dout,
488 unsigned int bytesout, void *din, unsigned int bytesin)
490 uint16_t control;
491 int16_t opcode_index;
492 int with_address;
493 int status;
495 spi_transaction trans = {
496 dout, bytesout,
497 din, bytesin,
498 0xff, 0xff, 0
501 /* There has to always at least be an opcode. */
502 if (!bytesout || !dout) {
503 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
504 return -1;
506 /* Make sure if we read something we have a place to put it. */
507 if (bytesin != 0 && !din) {
508 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
509 return -1;
512 if (ich_status_poll(SPIS_SCIP, 0) == -1)
513 return -1;
515 writew_(SPIS_CDS | SPIS_FCERR, cntlr.status);
517 spi_setup_type(&trans);
518 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
519 return -1;
520 if ((with_address = spi_setup_offset(&trans)) < 0)
521 return -1;
523 if (trans.opcode == SPI_OPCODE_WREN) {
525 * Treat Write Enable as Atomic Pre-Op if possible
526 * in order to prevent the Management Engine from
527 * issuing a transaction between WREN and DATA.
529 if (!ichspi_lock)
530 writew_(trans.opcode, cntlr.preop);
531 return 0;
534 /* Preset control fields */
535 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
537 /* Issue atomic preop cycle if needed */
538 if (readw_(cntlr.preop))
539 control |= SPIC_ACS;
541 if (!trans.bytesout && !trans.bytesin) {
542 /* SPI addresses are 24 bit only */
543 if (with_address)
544 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
547 * This is a 'no data' command (like Write Enable), its
548 * bytesout size was 1, decremented to zero while executing
549 * spi_setup_opcode() above. Tell the chip to send the
550 * command.
552 writew_(control, cntlr.control);
554 /* wait for the result */
555 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
556 if (status == -1)
557 return -1;
559 if (status & SPIS_FCERR) {
560 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
561 return -1;
564 return 0;
568 * Check if this is a write command attempting to transfer more bytes
569 * than the controller can handle. Iterations for writes are not
570 * supported here because each SPI write command needs to be preceded
571 * and followed by other SPI commands, and this sequence is controlled
572 * by the SPI chip driver.
574 if (trans.bytesout > cntlr.databytes) {
575 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
576 " CONTROLLER_PAGE_LIMIT?\n");
577 return -1;
581 * Read or write up to databytes bytes at a time until everything has
582 * been sent.
584 while (trans.bytesout || trans.bytesin) {
585 uint32_t data_length;
587 /* SPI addresses are 24 bit only */
588 /* http://www.intel.com/content/dam/www/public/us/en/documents/datasheets/pentium-n3520-j2850-celeron-n2920-n2820-n2815-n2806-j1850-j1750-datasheet.pdf */
589 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
591 if (trans.bytesout)
592 data_length = min(trans.bytesout, cntlr.databytes);
593 else
594 data_length = min(trans.bytesin, cntlr.databytes);
596 /* Program data into FDATA0 to N */
597 if (trans.bytesout) {
598 write_reg(trans.out, cntlr.data, data_length);
599 spi_use_out(&trans, data_length);
600 if (with_address)
601 trans.offset += data_length;
604 /* Add proper control fields' values */
605 control &= ~((cntlr.databytes - 1) << 8);
606 control |= SPIC_DS;
607 control |= (data_length - 1) << 8;
609 /* write it */
610 writew_(control, cntlr.control);
612 /* Wait for Cycle Done Status or Flash Cycle Error. */
613 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
614 if (status == -1)
615 return -1;
617 if (status & SPIS_FCERR) {
618 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
619 return -1;
622 if (trans.bytesin) {
623 read_reg(cntlr.data, trans.in, data_length);
624 spi_use_in(&trans, data_length);
625 if (with_address)
626 trans.offset += data_length;
630 /* Clear atomic preop now that xfer is done */
631 writew_(0, cntlr.preop);
633 return 0;
636 /* Use first empty Protected Range Register to cover region of flash */
637 int spi_flash_protect(u32 start, u32 size)
639 u32 end = start + size - 1;
640 u32 reg;
641 int prr;
643 /* Find first empty PRR */
644 for (prr = 0; prr < SPI_PRR_MAX; prr++) {
645 reg = SPIBAR32(SPI_PRR(prr));
646 if (reg == 0)
647 break;
649 if (prr >= SPI_PRR_MAX) {
650 printk(BIOS_ERR, "ERROR: No SPI PRR free!\n");
651 return -1;
654 /* Set protected range base and limit */
655 reg = ((end >> SPI_PRR_SHIFT) & SPI_PRR_MASK);
656 reg <<= SPI_PRR_LIMIT_SHIFT;
657 reg |= ((start >> SPI_PRR_SHIFT) & SPI_PRR_MASK);
658 reg |= SPI_PRR_WPE;
660 /* Set the PRR register and verify it is protected */
661 SPIBAR32(SPI_PRR(prr)) = reg;
662 reg = SPIBAR32(SPI_PRR(prr));
663 if (!(reg & SPI_PRR_WPE)) {
664 printk(BIOS_ERR, "ERROR: Unable to set SPI PRR %d\n", prr);
665 return -1;
668 printk(BIOS_INFO, "%s: PRR %d is enabled for range 0x%08x-0x%08x\n",
669 __func__, prr, start, end);
670 return 0;