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
11 #include <bootstage.h>
14 #include <dt-structs.h>
21 #include <spi_flash.h>
24 #include <asm/fast_spi.h>
27 #include <linux/sizes.h>
32 #define debug_trace(fmt, args...) debug(fmt, ##args)
34 #define debug_trace(x, args...)
37 struct ich_spi_platdata {
38 #if CONFIG_IS_ENABLED(OF_PLATDATA)
39 struct dtd_intel_fast_spi dtplat;
41 enum ich_version ich_version; /* Controller version, 7 or 9 */
42 bool lockdown; /* lock down controller settings? */
43 ulong mmio_base; /* Base of MMIO registers */
44 pci_dev_t bdf; /* PCI address used by of-platdata */
45 bool hwseq; /* Use hardware sequencing (not s/w) */
48 static u8 ich_readb(struct ich_spi_priv *priv, int reg)
50 u8 value = readb(priv->base + reg);
52 debug_trace("read %2.2x from %4.4x\n", value, reg);
57 static u16 ich_readw(struct ich_spi_priv *priv, int reg)
59 u16 value = readw(priv->base + reg);
61 debug_trace("read %4.4x from %4.4x\n", value, reg);
66 static u32 ich_readl(struct ich_spi_priv *priv, int reg)
68 u32 value = readl(priv->base + reg);
70 debug_trace("read %8.8x from %4.4x\n", value, reg);
75 static void ich_writeb(struct ich_spi_priv *priv, u8 value, int reg)
77 writeb(value, priv->base + reg);
78 debug_trace("wrote %2.2x to %4.4x\n", value, reg);
81 static void ich_writew(struct ich_spi_priv *priv, u16 value, int reg)
83 writew(value, priv->base + reg);
84 debug_trace("wrote %4.4x to %4.4x\n", value, reg);
87 static void ich_writel(struct ich_spi_priv *priv, u32 value, int reg)
89 writel(value, priv->base + reg);
90 debug_trace("wrote %8.8x to %4.4x\n", value, reg);
93 static void write_reg(struct ich_spi_priv *priv, const void *value,
94 int dest_reg, uint32_t size)
96 memcpy_toio(priv->base + dest_reg, value, size);
99 static void read_reg(struct ich_spi_priv *priv, int src_reg, void *value,
102 memcpy_fromio(value, priv->base + src_reg, size);
105 static void ich_set_bbar(struct ich_spi_priv *ctlr, uint32_t minaddr)
107 const uint32_t bbar_mask = 0x00ffff00;
108 uint32_t ichspi_bbar;
111 minaddr &= bbar_mask;
112 ichspi_bbar = ich_readl(ctlr, ctlr->bbar) & ~bbar_mask;
113 ichspi_bbar |= minaddr;
114 ich_writel(ctlr, ichspi_bbar, ctlr->bbar);
118 /* @return 1 if the SPI flash supports the 33MHz speed */
119 static bool ich9_can_do_33mhz(struct udevice *dev)
121 struct ich_spi_priv *priv = dev_get_priv(dev);
124 if (!CONFIG_IS_ENABLED(PCI))
126 /* Observe SPI Descriptor Component Section 0 */
127 dm_pci_write_config32(priv->pch, 0xb0, 0x1000);
129 /* Extract the Write/Erase SPI Frequency from descriptor */
130 dm_pci_read_config32(priv->pch, 0xb4, &fdod);
132 /* Bits 23:21 have the fast read clock frequency, 0=20MHz, 1=33MHz */
133 speed = (fdod >> 21) & 7;
138 static void spi_lock_down(struct ich_spi_platdata *plat, void *sbase)
140 if (plat->ich_version == ICHV_7) {
141 struct ich7_spi_regs *ich7_spi = sbase;
143 setbits_le16(&ich7_spi->spis, SPIS_LOCK);
144 } else if (plat->ich_version == ICHV_9) {
145 struct ich9_spi_regs *ich9_spi = sbase;
147 setbits_le16(&ich9_spi->hsfs, HSFS_FLOCKDN);
151 static bool spi_lock_status(struct ich_spi_platdata *plat, void *sbase)
155 if (plat->ich_version == ICHV_7) {
156 struct ich7_spi_regs *ich7_spi = sbase;
158 lock = readw(&ich7_spi->spis) & SPIS_LOCK;
159 } else if (plat->ich_version == ICHV_9) {
160 struct ich9_spi_regs *ich9_spi = sbase;
162 lock = readw(&ich9_spi->hsfs) & HSFS_FLOCKDN;
168 static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans,
172 uint8_t opmenu[ctlr->menubytes];
175 /* The lock is off, so just use index 0. */
176 ich_writeb(ctlr, trans->opcode, ctlr->opmenu);
177 optypes = ich_readw(ctlr, ctlr->optype);
178 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
179 ich_writew(ctlr, optypes, ctlr->optype);
182 /* The lock is on. See if what we need is on the menu. */
184 uint16_t opcode_index;
186 /* Write Enable is handled as atomic prefix */
187 if (trans->opcode == SPI_OPCODE_WREN)
190 read_reg(ctlr, ctlr->opmenu, opmenu, sizeof(opmenu));
191 for (opcode_index = 0; opcode_index < ctlr->menubytes;
193 if (opmenu[opcode_index] == trans->opcode)
197 if (opcode_index == ctlr->menubytes) {
198 debug("ICH SPI: Opcode %x not found\n", trans->opcode);
202 optypes = ich_readw(ctlr, ctlr->optype);
203 optype = (optypes >> (opcode_index * 2)) & 0x3;
205 if (optype != trans->type) {
206 debug("ICH SPI: Transaction doesn't fit type %d\n",
215 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
216 * below is true) or 0. In case the wait was for the bit(s) to set - write
217 * those bits back, which would cause resetting them.
219 * Return the last read status value on success or -1 on failure.
221 static int ich_status_poll(struct ich_spi_priv *ctlr, u16 bitmask,
224 int timeout = 600000; /* This will result in 6s */
228 status = ich_readw(ctlr, ctlr->status);
229 if (wait_til_set ^ ((status & bitmask) == 0)) {
231 ich_writew(ctlr, status & bitmask,
238 debug("ICH SPI: SCIP timeout, read %x, expected %x, wts %x %x\n",
239 status, bitmask, wait_til_set, status & bitmask);
244 static void ich_spi_config_opcode(struct udevice *dev)
246 struct ich_spi_priv *ctlr = dev_get_priv(dev);
249 * PREOP, OPTYPE, OPMENU1/OPMENU2 registers can be locked down
250 * to prevent accidental or intentional writes. Before they get
251 * locked down, these registers should be initialized properly.
253 ich_writew(ctlr, SPI_OPPREFIX, ctlr->preop);
254 ich_writew(ctlr, SPI_OPTYPE, ctlr->optype);
255 ich_writel(ctlr, SPI_OPMENU_LOWER, ctlr->opmenu);
256 ich_writel(ctlr, SPI_OPMENU_UPPER, ctlr->opmenu + sizeof(u32));
259 static int ich_spi_exec_op_swseq(struct spi_slave *slave,
260 const struct spi_mem_op *op)
262 struct udevice *bus = dev_get_parent(slave->dev);
263 struct ich_spi_platdata *plat = dev_get_platdata(bus);
264 struct ich_spi_priv *ctlr = dev_get_priv(bus);
266 int16_t opcode_index;
269 struct spi_trans *trans = &ctlr->trans;
270 bool lock = spi_lock_status(plat, ctlr->base);
277 if (op->data.nbytes) {
278 if (op->data.dir == SPI_MEM_DATA_IN) {
279 trans->in = op->data.buf.in;
280 trans->bytesin = op->data.nbytes;
282 trans->out = op->data.buf.out;
283 trans->bytesout = op->data.nbytes;
287 if (trans->opcode != op->cmd.opcode)
288 trans->opcode = op->cmd.opcode;
290 if (lock && trans->opcode == SPI_OPCODE_WRDIS)
293 if (trans->opcode == SPI_OPCODE_WREN) {
295 * Treat Write Enable as Atomic Pre-Op if possible
296 * in order to prevent the Management Engine from
297 * issuing a transaction between WREN and DATA.
300 ich_writew(ctlr, trans->opcode, ctlr->preop);
304 ret = ich_status_poll(ctlr, SPIS_SCIP, 0);
308 if (plat->ich_version == ICHV_7)
309 ich_writew(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
311 ich_writeb(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
313 /* Try to guess spi transaction type */
314 if (op->data.dir == SPI_MEM_DATA_OUT) {
316 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
318 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
321 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
323 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
325 /* Special erase case handling */
326 if (op->addr.nbytes && !op->data.buswidth)
327 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
329 opcode_index = spi_setup_opcode(ctlr, trans, lock);
330 if (opcode_index < 0)
333 if (op->addr.nbytes) {
334 trans->offset = op->addr.val;
338 if (ctlr->speed && ctlr->max_speed >= 33000000) {
341 byte = ich_readb(ctlr, ctlr->speed);
342 if (ctlr->cur_speed >= 33000000)
343 byte |= SSFC_SCF_33MHZ;
345 byte &= ~SSFC_SCF_33MHZ;
346 ich_writeb(ctlr, byte, ctlr->speed);
349 /* Preset control fields */
350 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
352 /* Issue atomic preop cycle if needed */
353 if (ich_readw(ctlr, ctlr->preop))
356 if (!trans->bytesout && !trans->bytesin) {
357 /* SPI addresses are 24 bit only */
359 ich_writel(ctlr, trans->offset & 0x00FFFFFF,
363 * This is a 'no data' command (like Write Enable), its
364 * bitesout size was 1, decremented to zero while executing
365 * spi_setup_opcode() above. Tell the chip to send the
368 ich_writew(ctlr, control, ctlr->control);
370 /* wait for the result */
371 status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
375 if (status & SPIS_FCERR) {
376 debug("ICH SPI: Command transaction error\n");
383 while (trans->bytesout || trans->bytesin) {
384 uint32_t data_length;
386 /* SPI addresses are 24 bit only */
387 ich_writel(ctlr, trans->offset & 0x00FFFFFF, ctlr->addr);
390 data_length = min(trans->bytesout, ctlr->databytes);
392 data_length = min(trans->bytesin, ctlr->databytes);
394 /* Program data into FDATA0 to N */
395 if (trans->bytesout) {
396 write_reg(ctlr, trans->out, ctlr->data, data_length);
397 trans->bytesout -= data_length;
400 /* Add proper control fields' values */
401 control &= ~((ctlr->databytes - 1) << 8);
403 control |= (data_length - 1) << 8;
406 ich_writew(ctlr, control, ctlr->control);
408 /* Wait for Cycle Done Status or Flash Cycle Error */
409 status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
413 if (status & SPIS_FCERR) {
414 debug("ICH SPI: Data transaction error %x\n", status);
418 if (trans->bytesin) {
419 read_reg(ctlr, ctlr->data, trans->in, data_length);
420 trans->bytesin -= data_length;
424 /* Clear atomic preop now that xfer is done */
426 ich_writew(ctlr, 0, ctlr->preop);
432 * Ensure read/write xfer len is not greater than SPIBAR_FDATA_FIFO_SIZE and
433 * that the operation does not cross page boundary.
435 static uint get_xfer_len(u32 offset, int len, int page_size)
437 uint xfer_len = min(len, SPIBAR_FDATA_FIFO_SIZE);
438 uint bytes_left = ALIGN(offset, page_size) - offset;
441 xfer_len = min(xfer_len, bytes_left);
446 /* Fill FDATAn FIFO in preparation for a write transaction */
447 static void fill_xfer_fifo(struct fast_spi_regs *regs, const void *data,
450 memcpy(regs->fdata, data, len);
453 /* Drain FDATAn FIFO after a read transaction populates data */
454 static void drain_xfer_fifo(struct fast_spi_regs *regs, void *dest, uint len)
456 memcpy(dest, regs->fdata, len);
459 /* Fire up a transfer using the hardware sequencer */
460 static void start_hwseq_xfer(struct fast_spi_regs *regs, uint hsfsts_cycle,
461 uint offset, uint len)
463 /* Make sure all W1C status bits get cleared */
466 hsfsts = readl(®s->hsfsts_ctl);
467 hsfsts &= ~(HSFSTS_FCYCLE_MASK | HSFSTS_FDBC_MASK);
468 hsfsts |= HSFSTS_AEL | HSFSTS_FCERR | HSFSTS_FDONE;
470 /* Set up transaction parameters */
471 hsfsts |= hsfsts_cycle << HSFSTS_FCYCLE_SHIFT;
472 hsfsts |= ((len - 1) << HSFSTS_FDBC_SHIFT) & HSFSTS_FDBC_MASK;
473 hsfsts |= HSFSTS_FGO;
475 writel(offset, ®s->faddr);
476 writel(hsfsts, ®s->hsfsts_ctl);
479 static int wait_for_hwseq_xfer(struct fast_spi_regs *regs, uint offset)
484 start = get_timer(0);
486 hsfsts = readl(®s->hsfsts_ctl);
487 if (hsfsts & HSFSTS_FCERR) {
488 debug("SPI transaction error at offset %x HSFSTS = %08x\n",
492 if (hsfsts & HSFSTS_AEL)
495 if (hsfsts & HSFSTS_FDONE)
497 } while (get_timer(start) < SPIBAR_HWSEQ_XFER_TIMEOUT_MS);
499 debug("SPI transaction timeout at offset %x HSFSTS = %08x, timer %d\n",
500 offset, hsfsts, (uint)get_timer(start));
506 * exec_sync_hwseq_xfer() - Execute flash transfer by hardware sequencing
508 * This waits until complete or timeout
510 * @regs: SPI registers
511 * @hsfsts_cycle: Cycle type (enum hsfsts_cycle_t)
512 * @offset: Offset to access
513 * @len: Number of bytes to transfer (can be 0)
514 * @return 0 if OK, -EIO on flash-cycle error (FCERR), -EPERM on access error
515 * (AEL), -ETIMEDOUT on timeout
517 static int exec_sync_hwseq_xfer(struct fast_spi_regs *regs, uint hsfsts_cycle,
518 uint offset, uint len)
520 start_hwseq_xfer(regs, hsfsts_cycle, offset, len);
522 return wait_for_hwseq_xfer(regs, offset);
525 static int ich_spi_exec_op_hwseq(struct spi_slave *slave,
526 const struct spi_mem_op *op)
528 struct spi_flash *flash = dev_get_uclass_priv(slave->dev);
529 struct udevice *bus = dev_get_parent(slave->dev);
530 struct ich_spi_priv *priv = dev_get_priv(bus);
531 struct fast_spi_regs *regs = priv->base;
540 offset = op->addr.val;
541 len = op->data.nbytes;
543 switch (op->cmd.opcode) {
545 cycle = HSFSTS_CYCLE_RDID;
547 case SPINOR_OP_READ_FAST:
548 cycle = HSFSTS_CYCLE_READ;
551 cycle = HSFSTS_CYCLE_WRITE;
554 /* Nothing needs to be done */
557 cycle = HSFSTS_CYCLE_WR_STATUS;
560 cycle = HSFSTS_CYCLE_RD_STATUS;
563 return 0; /* ignore */
564 case SPINOR_OP_BE_4K:
565 cycle = HSFSTS_CYCLE_4K_ERASE;
566 ret = exec_sync_hwseq_xfer(regs, cycle, offset, 0);
569 debug("Unknown cycle %x\n", op->cmd.opcode);
573 out = op->data.dir == SPI_MEM_DATA_OUT;
574 buf = out ? (u8 *)op->data.buf.out : op->data.buf.in;
575 page_size = flash->page_size ? : 256;
578 uint xfer_len = get_xfer_len(offset, len, page_size);
581 fill_xfer_fifo(regs, buf, xfer_len);
583 ret = exec_sync_hwseq_xfer(regs, cycle, offset, xfer_len);
588 drain_xfer_fifo(regs, buf, xfer_len);
598 static int ich_spi_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
600 struct udevice *bus = dev_get_parent(slave->dev);
601 struct ich_spi_platdata *plat = dev_get_platdata(bus);
604 bootstage_start(BOOTSTAGE_ID_ACCUM_SPI, "fast_spi");
606 ret = ich_spi_exec_op_hwseq(slave, op);
608 ret = ich_spi_exec_op_swseq(slave, op);
609 bootstage_accum(BOOTSTAGE_ID_ACCUM_SPI);
614 static int ich_get_mmap_bus(struct udevice *bus, ulong *map_basep,
615 uint *map_sizep, uint *offsetp)
619 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
620 struct pci_child_platdata *pplat = dev_get_parent_platdata(bus);
622 spi_bdf = pplat->devfn;
624 struct ich_spi_platdata *plat = dev_get_platdata(bus);
627 * We cannot rely on plat->bdf being set up yet since this method can
628 * be called before the device is probed. Use the of-platdata directly
631 spi_bdf = pci_ofplat_get_devfn(plat->dtplat.reg[0]);
634 return fast_spi_get_bios_mmap(spi_bdf, map_basep, map_sizep, offsetp);
637 static int ich_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep,
640 struct udevice *bus = dev_get_parent(dev);
642 return ich_get_mmap_bus(bus, map_basep, map_sizep, offsetp);
645 static int ich_spi_adjust_size(struct spi_slave *slave, struct spi_mem_op *op)
647 unsigned int page_offset;
648 int addr = op->addr.val;
649 unsigned int byte_count = op->data.nbytes;
651 if (hweight32(ICH_BOUNDARY) == 1) {
652 page_offset = addr & (ICH_BOUNDARY - 1);
656 page_offset = do_div(aux, ICH_BOUNDARY);
659 if (op->data.dir == SPI_MEM_DATA_IN) {
660 if (slave->max_read_size) {
661 op->data.nbytes = min(ICH_BOUNDARY - page_offset,
662 slave->max_read_size);
664 } else if (slave->max_write_size) {
665 op->data.nbytes = min(ICH_BOUNDARY - page_offset,
666 slave->max_write_size);
669 op->data.nbytes = min(op->data.nbytes, byte_count);
674 static int ich_protect_lockdown(struct udevice *dev)
676 struct ich_spi_platdata *plat = dev_get_platdata(dev);
677 struct ich_spi_priv *priv = dev_get_priv(dev);
680 /* Disable the BIOS write protect so write commands are allowed */
682 ret = pch_set_spi_protect(priv->pch, false);
683 if (ret == -ENOSYS) {
686 bios_cntl = ich_readb(priv, priv->bcr);
687 bios_cntl &= ~BIT(5); /* clear Enable InSMM_STS (EISS) */
688 bios_cntl |= 1; /* Write Protect Disable (WPD) */
689 ich_writeb(priv, bios_cntl, priv->bcr);
691 debug("%s: Failed to disable write-protect: err=%d\n",
696 /* Lock down SPI controller settings if required */
697 if (plat->lockdown) {
698 ich_spi_config_opcode(dev);
699 spi_lock_down(plat, priv->base);
705 static int ich_init_controller(struct udevice *dev,
706 struct ich_spi_platdata *plat,
707 struct ich_spi_priv *ctlr)
709 if (spl_phase() == PHASE_TPL) {
710 struct ich_spi_platdata *plat = dev_get_platdata(dev);
713 ret = fast_spi_early_init(plat->bdf, plat->mmio_base);
718 ctlr->base = (void *)plat->mmio_base;
719 if (plat->ich_version == ICHV_7) {
720 struct ich7_spi_regs *ich7_spi = ctlr->base;
722 ctlr->opmenu = offsetof(struct ich7_spi_regs, opmenu);
723 ctlr->menubytes = sizeof(ich7_spi->opmenu);
724 ctlr->optype = offsetof(struct ich7_spi_regs, optype);
725 ctlr->addr = offsetof(struct ich7_spi_regs, spia);
726 ctlr->data = offsetof(struct ich7_spi_regs, spid);
727 ctlr->databytes = sizeof(ich7_spi->spid);
728 ctlr->status = offsetof(struct ich7_spi_regs, spis);
729 ctlr->control = offsetof(struct ich7_spi_regs, spic);
730 ctlr->bbar = offsetof(struct ich7_spi_regs, bbar);
731 ctlr->preop = offsetof(struct ich7_spi_regs, preop);
732 } else if (plat->ich_version == ICHV_9) {
733 struct ich9_spi_regs *ich9_spi = ctlr->base;
735 ctlr->opmenu = offsetof(struct ich9_spi_regs, opmenu);
736 ctlr->menubytes = sizeof(ich9_spi->opmenu);
737 ctlr->optype = offsetof(struct ich9_spi_regs, optype);
738 ctlr->addr = offsetof(struct ich9_spi_regs, faddr);
739 ctlr->data = offsetof(struct ich9_spi_regs, fdata);
740 ctlr->databytes = sizeof(ich9_spi->fdata);
741 ctlr->status = offsetof(struct ich9_spi_regs, ssfs);
742 ctlr->control = offsetof(struct ich9_spi_regs, ssfc);
743 ctlr->speed = ctlr->control + 2;
744 ctlr->bbar = offsetof(struct ich9_spi_regs, bbar);
745 ctlr->preop = offsetof(struct ich9_spi_regs, preop);
746 ctlr->bcr = offsetof(struct ich9_spi_regs, bcr);
747 ctlr->pr = &ich9_spi->pr[0];
748 } else if (plat->ich_version == ICHV_APL) {
750 debug("ICH SPI: Unrecognised ICH version %d\n",
755 /* Work out the maximum speed we can support */
756 ctlr->max_speed = 20000000;
757 if (plat->ich_version == ICHV_9 && ich9_can_do_33mhz(dev))
758 ctlr->max_speed = 33000000;
759 debug("ICH SPI: Version ID %d detected at %lx, speed %ld\n",
760 plat->ich_version, plat->mmio_base, ctlr->max_speed);
762 ich_set_bbar(ctlr, 0);
767 static int ich_cache_bios_region(struct udevice *dev)
775 ret = ich_get_mmap_bus(dev, &map_base, &map_size, &offset);
779 /* Don't use WRBACK since we are not supposed to write to SPI flash */
780 base = SZ_4G - map_size;
781 mtrr_set_next_var(MTRR_TYPE_WRPROT, base, map_size);
782 log_debug("BIOS cache base=%lx, size=%x\n", base, (uint)map_size);
787 static int ich_spi_probe(struct udevice *dev)
789 struct ich_spi_platdata *plat = dev_get_platdata(dev);
790 struct ich_spi_priv *priv = dev_get_priv(dev);
793 ret = ich_init_controller(dev, plat, priv);
797 if (spl_phase() == PHASE_TPL) {
798 /* Cache the BIOS to speed things up */
799 ret = ich_cache_bios_region(dev);
803 ret = ich_protect_lockdown(dev);
807 priv->cur_speed = priv->max_speed;
812 static int ich_spi_remove(struct udevice *bus)
815 * Configure SPI controller so that the Linux MTD driver can fully
816 * access the SPI NOR chip
818 ich_spi_config_opcode(bus);
823 static int ich_spi_set_speed(struct udevice *bus, uint speed)
825 struct ich_spi_priv *priv = dev_get_priv(bus);
827 priv->cur_speed = speed;
832 static int ich_spi_set_mode(struct udevice *bus, uint mode)
834 debug("%s: mode=%d\n", __func__, mode);
839 static int ich_spi_child_pre_probe(struct udevice *dev)
841 struct udevice *bus = dev_get_parent(dev);
842 struct ich_spi_platdata *plat = dev_get_platdata(bus);
843 struct ich_spi_priv *priv = dev_get_priv(bus);
844 struct spi_slave *slave = dev_get_parent_priv(dev);
847 * Yes this controller can only write a small number of bytes at
848 * once! The limit is typically 64 bytes. For hardware sequencing a
849 * a loop is used to get around this.
852 slave->max_write_size = priv->databytes;
854 * ICH 7 SPI controller only supports array read command
855 * and byte program command for SST flash
857 if (plat->ich_version == ICHV_7)
858 slave->mode = SPI_RX_SLOW | SPI_TX_BYTE;
863 static int ich_spi_ofdata_to_platdata(struct udevice *dev)
865 struct ich_spi_platdata *plat = dev_get_platdata(dev);
867 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
868 struct ich_spi_priv *priv = dev_get_priv(dev);
870 /* Find a PCH if there is one */
871 uclass_first_device(UCLASS_PCH, &priv->pch);
873 priv->pch = dev_get_parent(dev);
875 plat->ich_version = dev_get_driver_data(dev);
876 plat->lockdown = dev_read_bool(dev, "intel,spi-lock-down");
877 if (plat->ich_version == ICHV_APL) {
878 plat->mmio_base = dm_pci_read_bar32(dev, 0);
880 /* SBASE is similar */
881 pch_get_spi_base(priv->pch, &plat->mmio_base);
884 * Use an int so that the property is present in of-platdata even
887 plat->hwseq = dev_read_u32_default(dev, "intel,hardware-seq", 0);
889 plat->ich_version = ICHV_APL;
890 plat->mmio_base = plat->dtplat.early_regs[0];
891 plat->bdf = pci_ofplat_get_devfn(plat->dtplat.reg[0]);
892 plat->hwseq = plat->dtplat.intel_hardware_seq;
894 debug("%s: mmio_base=%lx\n", __func__, plat->mmio_base);
899 static const struct spi_controller_mem_ops ich_controller_mem_ops = {
900 .adjust_op_size = ich_spi_adjust_size,
902 .exec_op = ich_spi_exec_op,
905 static const struct dm_spi_ops ich_spi_ops = {
906 /* xfer is not supported */
907 .set_speed = ich_spi_set_speed,
908 .set_mode = ich_spi_set_mode,
909 .mem_ops = &ich_controller_mem_ops,
910 .get_mmap = ich_get_mmap,
912 * cs_info is not needed, since we require all chip selects to be
913 * in the device tree explicitly
917 static const struct udevice_id ich_spi_ids[] = {
918 { .compatible = "intel,ich7-spi", ICHV_7 },
919 { .compatible = "intel,ich9-spi", ICHV_9 },
920 { .compatible = "intel,fast-spi", ICHV_APL },
924 U_BOOT_DRIVER(intel_fast_spi) = {
925 .name = "intel_fast_spi",
927 .of_match = ich_spi_ids,
929 .ofdata_to_platdata = ich_spi_ofdata_to_platdata,
930 .platdata_auto_alloc_size = sizeof(struct ich_spi_platdata),
931 .priv_auto_alloc_size = sizeof(struct ich_spi_priv),
932 .child_pre_probe = ich_spi_child_pre_probe,
933 .probe = ich_spi_probe,
934 .remove = ich_spi_remove,
935 .flags = DM_FLAG_OS_PREPARE,