1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011-12 The Chromium OS Authors.
5 * This file is derived from the flashrom project.
8 #define LOG_CATEGORY UCLASS_SPI
13 #include <dt-structs.h>
26 #define debug_trace(fmt, args...) debug(fmt, ##args)
28 #define debug_trace(x, args...)
31 struct ich_spi_platdata {
32 #if CONFIG_IS_ENABLED(OF_PLATDATA)
33 struct dtd_intel_fast_spi dtplat;
35 enum ich_version ich_version; /* Controller version, 7 or 9 */
36 bool lockdown; /* lock down controller settings? */
37 ulong mmio_base; /* Base of MMIO registers */
38 pci_dev_t bdf; /* PCI address used by of-platdata */
41 static u8 ich_readb(struct ich_spi_priv *priv, int reg)
43 u8 value = readb(priv->base + reg);
45 debug_trace("read %2.2x from %4.4x\n", value, reg);
50 static u16 ich_readw(struct ich_spi_priv *priv, int reg)
52 u16 value = readw(priv->base + reg);
54 debug_trace("read %4.4x from %4.4x\n", value, reg);
59 static u32 ich_readl(struct ich_spi_priv *priv, int reg)
61 u32 value = readl(priv->base + reg);
63 debug_trace("read %8.8x from %4.4x\n", value, reg);
68 static void ich_writeb(struct ich_spi_priv *priv, u8 value, int reg)
70 writeb(value, priv->base + reg);
71 debug_trace("wrote %2.2x to %4.4x\n", value, reg);
74 static void ich_writew(struct ich_spi_priv *priv, u16 value, int reg)
76 writew(value, priv->base + reg);
77 debug_trace("wrote %4.4x to %4.4x\n", value, reg);
80 static void ich_writel(struct ich_spi_priv *priv, u32 value, int reg)
82 writel(value, priv->base + reg);
83 debug_trace("wrote %8.8x to %4.4x\n", value, reg);
86 static void write_reg(struct ich_spi_priv *priv, const void *value,
87 int dest_reg, uint32_t size)
89 memcpy_toio(priv->base + dest_reg, value, size);
92 static void read_reg(struct ich_spi_priv *priv, int src_reg, void *value,
95 memcpy_fromio(value, priv->base + src_reg, size);
98 static void ich_set_bbar(struct ich_spi_priv *ctlr, uint32_t minaddr)
100 const uint32_t bbar_mask = 0x00ffff00;
101 uint32_t ichspi_bbar;
103 minaddr &= bbar_mask;
104 ichspi_bbar = ich_readl(ctlr, ctlr->bbar) & ~bbar_mask;
105 ichspi_bbar |= minaddr;
106 ich_writel(ctlr, ichspi_bbar, ctlr->bbar);
109 /* @return 1 if the SPI flash supports the 33MHz speed */
110 static bool ich9_can_do_33mhz(struct udevice *dev)
112 struct ich_spi_priv *priv = dev_get_priv(dev);
115 /* Observe SPI Descriptor Component Section 0 */
116 dm_pci_write_config32(priv->pch, 0xb0, 0x1000);
118 /* Extract the Write/Erase SPI Frequency from descriptor */
119 dm_pci_read_config32(priv->pch, 0xb4, &fdod);
121 /* Bits 23:21 have the fast read clock frequency, 0=20MHz, 1=33MHz */
122 speed = (fdod >> 21) & 7;
127 static void spi_lock_down(struct ich_spi_platdata *plat, void *sbase)
129 if (plat->ich_version == ICHV_7) {
130 struct ich7_spi_regs *ich7_spi = sbase;
132 setbits_le16(&ich7_spi->spis, SPIS_LOCK);
133 } else if (plat->ich_version == ICHV_9) {
134 struct ich9_spi_regs *ich9_spi = sbase;
136 setbits_le16(&ich9_spi->hsfs, HSFS_FLOCKDN);
140 static bool spi_lock_status(struct ich_spi_platdata *plat, void *sbase)
144 if (plat->ich_version == ICHV_7) {
145 struct ich7_spi_regs *ich7_spi = sbase;
147 lock = readw(&ich7_spi->spis) & SPIS_LOCK;
148 } else if (plat->ich_version == ICHV_9) {
149 struct ich9_spi_regs *ich9_spi = sbase;
151 lock = readw(&ich9_spi->hsfs) & HSFS_FLOCKDN;
157 static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans,
161 uint8_t opmenu[ctlr->menubytes];
164 /* The lock is off, so just use index 0. */
165 ich_writeb(ctlr, trans->opcode, ctlr->opmenu);
166 optypes = ich_readw(ctlr, ctlr->optype);
167 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
168 ich_writew(ctlr, optypes, ctlr->optype);
171 /* The lock is on. See if what we need is on the menu. */
173 uint16_t opcode_index;
175 /* Write Enable is handled as atomic prefix */
176 if (trans->opcode == SPI_OPCODE_WREN)
179 read_reg(ctlr, ctlr->opmenu, opmenu, sizeof(opmenu));
180 for (opcode_index = 0; opcode_index < ctlr->menubytes;
182 if (opmenu[opcode_index] == trans->opcode)
186 if (opcode_index == ctlr->menubytes) {
187 debug("ICH SPI: Opcode %x not found\n", trans->opcode);
191 optypes = ich_readw(ctlr, ctlr->optype);
192 optype = (optypes >> (opcode_index * 2)) & 0x3;
194 if (optype != trans->type) {
195 debug("ICH SPI: Transaction doesn't fit type %d\n",
204 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
205 * below is true) or 0. In case the wait was for the bit(s) to set - write
206 * those bits back, which would cause resetting them.
208 * Return the last read status value on success or -1 on failure.
210 static int ich_status_poll(struct ich_spi_priv *ctlr, u16 bitmask,
213 int timeout = 600000; /* This will result in 6s */
217 status = ich_readw(ctlr, ctlr->status);
218 if (wait_til_set ^ ((status & bitmask) == 0)) {
220 ich_writew(ctlr, status & bitmask,
227 debug("ICH SPI: SCIP timeout, read %x, expected %x, wts %x %x\n",
228 status, bitmask, wait_til_set, status & bitmask);
233 static void ich_spi_config_opcode(struct udevice *dev)
235 struct ich_spi_priv *ctlr = dev_get_priv(dev);
238 * PREOP, OPTYPE, OPMENU1/OPMENU2 registers can be locked down
239 * to prevent accidental or intentional writes. Before they get
240 * locked down, these registers should be initialized properly.
242 ich_writew(ctlr, SPI_OPPREFIX, ctlr->preop);
243 ich_writew(ctlr, SPI_OPTYPE, ctlr->optype);
244 ich_writel(ctlr, SPI_OPMENU_LOWER, ctlr->opmenu);
245 ich_writel(ctlr, SPI_OPMENU_UPPER, ctlr->opmenu + sizeof(u32));
248 static int ich_spi_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
250 struct udevice *bus = dev_get_parent(slave->dev);
251 struct ich_spi_platdata *plat = dev_get_platdata(bus);
252 struct ich_spi_priv *ctlr = dev_get_priv(bus);
254 int16_t opcode_index;
257 struct spi_trans *trans = &ctlr->trans;
258 bool lock = spi_lock_status(plat, ctlr->base);
265 if (op->data.nbytes) {
266 if (op->data.dir == SPI_MEM_DATA_IN) {
267 trans->in = op->data.buf.in;
268 trans->bytesin = op->data.nbytes;
270 trans->out = op->data.buf.out;
271 trans->bytesout = op->data.nbytes;
275 if (trans->opcode != op->cmd.opcode)
276 trans->opcode = op->cmd.opcode;
278 if (lock && trans->opcode == SPI_OPCODE_WRDIS)
281 if (trans->opcode == SPI_OPCODE_WREN) {
283 * Treat Write Enable as Atomic Pre-Op if possible
284 * in order to prevent the Management Engine from
285 * issuing a transaction between WREN and DATA.
288 ich_writew(ctlr, trans->opcode, ctlr->preop);
292 ret = ich_status_poll(ctlr, SPIS_SCIP, 0);
296 if (plat->ich_version == ICHV_7)
297 ich_writew(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
299 ich_writeb(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
301 /* Try to guess spi transaction type */
302 if (op->data.dir == SPI_MEM_DATA_OUT) {
304 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
306 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
309 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
311 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
313 /* Special erase case handling */
314 if (op->addr.nbytes && !op->data.buswidth)
315 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
317 opcode_index = spi_setup_opcode(ctlr, trans, lock);
318 if (opcode_index < 0)
321 if (op->addr.nbytes) {
322 trans->offset = op->addr.val;
326 if (ctlr->speed && ctlr->max_speed >= 33000000) {
329 byte = ich_readb(ctlr, ctlr->speed);
330 if (ctlr->cur_speed >= 33000000)
331 byte |= SSFC_SCF_33MHZ;
333 byte &= ~SSFC_SCF_33MHZ;
334 ich_writeb(ctlr, byte, ctlr->speed);
337 /* Preset control fields */
338 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
340 /* Issue atomic preop cycle if needed */
341 if (ich_readw(ctlr, ctlr->preop))
344 if (!trans->bytesout && !trans->bytesin) {
345 /* SPI addresses are 24 bit only */
347 ich_writel(ctlr, trans->offset & 0x00FFFFFF,
351 * This is a 'no data' command (like Write Enable), its
352 * bitesout size was 1, decremented to zero while executing
353 * spi_setup_opcode() above. Tell the chip to send the
356 ich_writew(ctlr, control, ctlr->control);
358 /* wait for the result */
359 status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
363 if (status & SPIS_FCERR) {
364 debug("ICH SPI: Command transaction error\n");
371 while (trans->bytesout || trans->bytesin) {
372 uint32_t data_length;
374 /* SPI addresses are 24 bit only */
375 ich_writel(ctlr, trans->offset & 0x00FFFFFF, ctlr->addr);
378 data_length = min(trans->bytesout, ctlr->databytes);
380 data_length = min(trans->bytesin, ctlr->databytes);
382 /* Program data into FDATA0 to N */
383 if (trans->bytesout) {
384 write_reg(ctlr, trans->out, ctlr->data, data_length);
385 trans->bytesout -= data_length;
388 /* Add proper control fields' values */
389 control &= ~((ctlr->databytes - 1) << 8);
391 control |= (data_length - 1) << 8;
394 ich_writew(ctlr, control, ctlr->control);
396 /* Wait for Cycle Done Status or Flash Cycle Error */
397 status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
401 if (status & SPIS_FCERR) {
402 debug("ICH SPI: Data transaction error %x\n", status);
406 if (trans->bytesin) {
407 read_reg(ctlr, ctlr->data, trans->in, data_length);
408 trans->bytesin -= data_length;
412 /* Clear atomic preop now that xfer is done */
414 ich_writew(ctlr, 0, ctlr->preop);
419 static int ich_spi_adjust_size(struct spi_slave *slave, struct spi_mem_op *op)
421 unsigned int page_offset;
422 int addr = op->addr.val;
423 unsigned int byte_count = op->data.nbytes;
425 if (hweight32(ICH_BOUNDARY) == 1) {
426 page_offset = addr & (ICH_BOUNDARY - 1);
430 page_offset = do_div(aux, ICH_BOUNDARY);
433 if (op->data.dir == SPI_MEM_DATA_IN) {
434 if (slave->max_read_size) {
435 op->data.nbytes = min(ICH_BOUNDARY - page_offset,
436 slave->max_read_size);
438 } else if (slave->max_write_size) {
439 op->data.nbytes = min(ICH_BOUNDARY - page_offset,
440 slave->max_write_size);
443 op->data.nbytes = min(op->data.nbytes, byte_count);
448 static int ich_protect_lockdown(struct udevice *dev)
450 struct ich_spi_platdata *plat = dev_get_platdata(dev);
451 struct ich_spi_priv *priv = dev_get_priv(dev);
454 /* Disable the BIOS write protect so write commands are allowed */
456 ret = pch_set_spi_protect(priv->pch, false);
457 if (ret == -ENOSYS) {
460 bios_cntl = ich_readb(priv, priv->bcr);
461 bios_cntl &= ~BIT(5); /* clear Enable InSMM_STS (EISS) */
462 bios_cntl |= 1; /* Write Protect Disable (WPD) */
463 ich_writeb(priv, bios_cntl, priv->bcr);
465 debug("%s: Failed to disable write-protect: err=%d\n",
470 /* Lock down SPI controller settings if required */
471 if (plat->lockdown) {
472 ich_spi_config_opcode(dev);
473 spi_lock_down(plat, priv->base);
479 static int ich_init_controller(struct udevice *dev,
480 struct ich_spi_platdata *plat,
481 struct ich_spi_priv *ctlr)
483 ctlr->base = (void *)plat->mmio_base;
484 if (plat->ich_version == ICHV_7) {
485 struct ich7_spi_regs *ich7_spi = ctlr->base;
487 ctlr->opmenu = offsetof(struct ich7_spi_regs, opmenu);
488 ctlr->menubytes = sizeof(ich7_spi->opmenu);
489 ctlr->optype = offsetof(struct ich7_spi_regs, optype);
490 ctlr->addr = offsetof(struct ich7_spi_regs, spia);
491 ctlr->data = offsetof(struct ich7_spi_regs, spid);
492 ctlr->databytes = sizeof(ich7_spi->spid);
493 ctlr->status = offsetof(struct ich7_spi_regs, spis);
494 ctlr->control = offsetof(struct ich7_spi_regs, spic);
495 ctlr->bbar = offsetof(struct ich7_spi_regs, bbar);
496 ctlr->preop = offsetof(struct ich7_spi_regs, preop);
497 } else if (plat->ich_version == ICHV_9) {
498 struct ich9_spi_regs *ich9_spi = ctlr->base;
500 ctlr->opmenu = offsetof(struct ich9_spi_regs, opmenu);
501 ctlr->menubytes = sizeof(ich9_spi->opmenu);
502 ctlr->optype = offsetof(struct ich9_spi_regs, optype);
503 ctlr->addr = offsetof(struct ich9_spi_regs, faddr);
504 ctlr->data = offsetof(struct ich9_spi_regs, fdata);
505 ctlr->databytes = sizeof(ich9_spi->fdata);
506 ctlr->status = offsetof(struct ich9_spi_regs, ssfs);
507 ctlr->control = offsetof(struct ich9_spi_regs, ssfc);
508 ctlr->speed = ctlr->control + 2;
509 ctlr->bbar = offsetof(struct ich9_spi_regs, bbar);
510 ctlr->preop = offsetof(struct ich9_spi_regs, preop);
511 ctlr->bcr = offsetof(struct ich9_spi_regs, bcr);
512 ctlr->pr = &ich9_spi->pr[0];
514 debug("ICH SPI: Unrecognised ICH version %d\n",
519 /* Work out the maximum speed we can support */
520 ctlr->max_speed = 20000000;
521 if (plat->ich_version == ICHV_9 && ich9_can_do_33mhz(dev))
522 ctlr->max_speed = 33000000;
523 debug("ICH SPI: Version ID %d detected at %lx, speed %ld\n",
524 plat->ich_version, plat->mmio_base, ctlr->max_speed);
526 ich_set_bbar(ctlr, 0);
531 static int ich_spi_probe(struct udevice *dev)
533 struct ich_spi_platdata *plat = dev_get_platdata(dev);
534 struct ich_spi_priv *priv = dev_get_priv(dev);
537 ret = ich_init_controller(dev, plat, priv);
541 ret = ich_protect_lockdown(dev);
545 priv->cur_speed = priv->max_speed;
550 static int ich_spi_remove(struct udevice *bus)
553 * Configure SPI controller so that the Linux MTD driver can fully
554 * access the SPI NOR chip
556 ich_spi_config_opcode(bus);
561 static int ich_spi_set_speed(struct udevice *bus, uint speed)
563 struct ich_spi_priv *priv = dev_get_priv(bus);
565 priv->cur_speed = speed;
570 static int ich_spi_set_mode(struct udevice *bus, uint mode)
572 debug("%s: mode=%d\n", __func__, mode);
577 static int ich_spi_child_pre_probe(struct udevice *dev)
579 struct udevice *bus = dev_get_parent(dev);
580 struct ich_spi_platdata *plat = dev_get_platdata(bus);
581 struct ich_spi_priv *priv = dev_get_priv(bus);
582 struct spi_slave *slave = dev_get_parent_priv(dev);
585 * Yes this controller can only write a small number of bytes at
586 * once! The limit is typically 64 bytes.
588 slave->max_write_size = priv->databytes;
590 * ICH 7 SPI controller only supports array read command
591 * and byte program command for SST flash
593 if (plat->ich_version == ICHV_7)
594 slave->mode = SPI_RX_SLOW | SPI_TX_BYTE;
599 static int ich_spi_ofdata_to_platdata(struct udevice *dev)
601 struct ich_spi_platdata *plat = dev_get_platdata(dev);
603 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
604 struct ich_spi_priv *priv = dev_get_priv(dev);
606 /* Find a PCH if there is one */
607 uclass_first_device(UCLASS_PCH, &priv->pch);
609 priv->pch = dev_get_parent(dev);
611 plat->ich_version = dev_get_driver_data(dev);
612 plat->lockdown = dev_read_bool(dev, "intel,spi-lock-down");
613 pch_get_spi_base(priv->pch, &plat->mmio_base);
615 plat->ich_version = ICHV_APL;
616 plat->mmio_base = plat->dtplat.early_regs[0];
617 plat->bdf = pci_ofplat_get_devfn(plat->dtplat.reg[0]);
619 debug("%s: mmio_base=%lx\n", __func__, plat->mmio_base);
624 static const struct spi_controller_mem_ops ich_controller_mem_ops = {
625 .adjust_op_size = ich_spi_adjust_size,
627 .exec_op = ich_spi_exec_op,
630 static const struct dm_spi_ops ich_spi_ops = {
631 /* xfer is not supported */
632 .set_speed = ich_spi_set_speed,
633 .set_mode = ich_spi_set_mode,
634 .mem_ops = &ich_controller_mem_ops,
636 * cs_info is not needed, since we require all chip selects to be
637 * in the device tree explicitly
641 static const struct udevice_id ich_spi_ids[] = {
642 { .compatible = "intel,ich7-spi", ICHV_7 },
643 { .compatible = "intel,ich9-spi", ICHV_9 },
647 U_BOOT_DRIVER(intel_fast_spi) = {
648 .name = "intel_fast_spi",
650 .of_match = ich_spi_ids,
652 .ofdata_to_platdata = ich_spi_ofdata_to_platdata,
653 .platdata_auto_alloc_size = sizeof(struct ich_spi_platdata),
654 .priv_auto_alloc_size = sizeof(struct ich_spi_priv),
655 .child_pre_probe = ich_spi_child_pre_probe,
656 .probe = ich_spi_probe,
657 .remove = ich_spi_remove,
658 .flags = DM_FLAG_OS_PREPARE,