1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel PCH/PCU SPI flash driver.
5 * Copyright (C) 2016 - 2022, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
9 #include <linux/iopoll.h>
10 #include <linux/module.h>
12 #include <linux/mtd/partitions.h>
13 #include <linux/mtd/spi-nor.h>
15 #include <linux/spi/flash.h>
16 #include <linux/spi/spi.h>
17 #include <linux/spi/spi-mem.h>
19 #include "spi-intel.h"
21 /* Offsets are from @ispi->base */
24 #define HSFSTS_CTL 0x04
25 #define HSFSTS_CTL_FSMIE BIT(31)
26 #define HSFSTS_CTL_FDBC_SHIFT 24
27 #define HSFSTS_CTL_FDBC_MASK (0x3f << HSFSTS_CTL_FDBC_SHIFT)
29 #define HSFSTS_CTL_FCYCLE_SHIFT 17
30 #define HSFSTS_CTL_FCYCLE_MASK (0x0f << HSFSTS_CTL_FCYCLE_SHIFT)
31 /* HW sequencer opcodes */
32 #define HSFSTS_CTL_FCYCLE_READ (0x00 << HSFSTS_CTL_FCYCLE_SHIFT)
33 #define HSFSTS_CTL_FCYCLE_WRITE (0x02 << HSFSTS_CTL_FCYCLE_SHIFT)
34 #define HSFSTS_CTL_FCYCLE_ERASE (0x03 << HSFSTS_CTL_FCYCLE_SHIFT)
35 #define HSFSTS_CTL_FCYCLE_ERASE_64K (0x04 << HSFSTS_CTL_FCYCLE_SHIFT)
36 #define HSFSTS_CTL_FCYCLE_RDID (0x06 << HSFSTS_CTL_FCYCLE_SHIFT)
37 #define HSFSTS_CTL_FCYCLE_WRSR (0x07 << HSFSTS_CTL_FCYCLE_SHIFT)
38 #define HSFSTS_CTL_FCYCLE_RDSR (0x08 << HSFSTS_CTL_FCYCLE_SHIFT)
40 #define HSFSTS_CTL_FGO BIT(16)
41 #define HSFSTS_CTL_FLOCKDN BIT(15)
42 #define HSFSTS_CTL_FDV BIT(14)
43 #define HSFSTS_CTL_SCIP BIT(5)
44 #define HSFSTS_CTL_AEL BIT(2)
45 #define HSFSTS_CTL_FCERR BIT(1)
46 #define HSFSTS_CTL_FDONE BIT(0)
50 #define FDATA(n) (0x10 + ((n) * 4))
54 #define FREG(n) (0x54 + ((n) * 4))
55 #define FREG_BASE_MASK 0x3fff
56 #define FREG_LIMIT_SHIFT 16
57 #define FREG_LIMIT_MASK (0x03fff << FREG_LIMIT_SHIFT)
59 /* Offset is from @ispi->pregs */
60 #define PR(n) ((n) * 4)
61 #define PR_WPE BIT(31)
62 #define PR_LIMIT_SHIFT 16
63 #define PR_LIMIT_MASK (0x3fff << PR_LIMIT_SHIFT)
64 #define PR_RPE BIT(15)
65 #define PR_BASE_MASK 0x3fff
67 /* Offsets are from @ispi->sregs */
68 #define SSFSTS_CTL 0x00
69 #define SSFSTS_CTL_FSMIE BIT(23)
70 #define SSFSTS_CTL_DS BIT(22)
71 #define SSFSTS_CTL_DBC_SHIFT 16
72 #define SSFSTS_CTL_SPOP BIT(11)
73 #define SSFSTS_CTL_ACS BIT(10)
74 #define SSFSTS_CTL_SCGO BIT(9)
75 #define SSFSTS_CTL_COP_SHIFT 12
76 #define SSFSTS_CTL_FRS BIT(7)
77 #define SSFSTS_CTL_DOFRS BIT(6)
78 #define SSFSTS_CTL_AEL BIT(4)
79 #define SSFSTS_CTL_FCERR BIT(3)
80 #define SSFSTS_CTL_FDONE BIT(2)
81 #define SSFSTS_CTL_SCIP BIT(0)
83 #define PREOP_OPTYPE 0x04
87 #define OPTYPE_READ_NO_ADDR 0
88 #define OPTYPE_WRITE_NO_ADDR 1
89 #define OPTYPE_READ_WITH_ADDR 2
90 #define OPTYPE_WRITE_WITH_ADDR 3
94 #define BYT_SSFSTS_CTL 0x90
95 #define BYT_FREG_NUM 5
99 #define LPT_SSFSTS_CTL 0x90
100 #define LPT_FREG_NUM 5
104 #define BXT_SSFSTS_CTL 0xa0
105 #define BXT_FREG_NUM 12
109 #define CNL_FREG_NUM 6
114 #define ERASE_OPCODE_SHIFT 8
115 #define ERASE_OPCODE_MASK (0xff << ERASE_OPCODE_SHIFT)
116 #define ERASE_64K_OPCODE_SHIFT 16
117 #define ERASE_64K_OPCODE_MASK (0xff << ERASE_OPCODE_SHIFT)
119 /* Flash descriptor fields */
120 #define FLVALSIG_MAGIC 0x0ff0a55a
121 #define FLMAP0_NC_MASK GENMASK(9, 8)
122 #define FLMAP0_NC_SHIFT 8
123 #define FLMAP0_FCBA_MASK GENMASK(7, 0)
125 #define FLCOMP_C0DEN_MASK GENMASK(3, 0)
126 #define FLCOMP_C0DEN_512K 0x00
127 #define FLCOMP_C0DEN_1M 0x01
128 #define FLCOMP_C0DEN_2M 0x02
129 #define FLCOMP_C0DEN_4M 0x03
130 #define FLCOMP_C0DEN_8M 0x04
131 #define FLCOMP_C0DEN_16M 0x05
132 #define FLCOMP_C0DEN_32M 0x06
133 #define FLCOMP_C0DEN_64M 0x07
135 #define INTEL_SPI_TIMEOUT 5000 /* ms */
136 #define INTEL_SPI_FIFO_SZ 64
139 * struct intel_spi - Driver private data
140 * @dev: Device pointer
141 * @info: Pointer to board specific info
142 * @base: Beginning of MMIO space
143 * @pregs: Start of protection registers
144 * @sregs: Start of software sequencer registers
145 * @master: Pointer to the SPI controller structure
146 * @nregions: Maximum number of regions
147 * @pr_num: Maximum number of protected range registers
148 * @chip0_size: Size of the first flash chip in bytes
149 * @locked: Is SPI setting locked
150 * @swseq_reg: Use SW sequencer in register reads/writes
151 * @swseq_erase: Use SW sequencer in erase operation
152 * @atomic_preopcode: Holds preopcode when atomic sequence is requested
153 * @opcodes: Opcodes which are supported. This are programmed by BIOS
154 * before it locks down the controller.
155 * @mem_ops: Pointer to SPI MEM ops supported by the controller
159 const struct intel_spi_boardinfo *info;
163 struct spi_controller *master;
172 const struct intel_spi_mem_op *mem_ops;
175 struct intel_spi_mem_op {
176 struct spi_mem_op mem_op;
178 int (*exec_op)(struct intel_spi *ispi,
179 const struct spi_mem *mem,
180 const struct intel_spi_mem_op *iop,
181 const struct spi_mem_op *op);
184 static bool writeable;
185 module_param(writeable, bool, 0);
186 MODULE_PARM_DESC(writeable, "Enable write access to SPI flash chip (default=0)");
188 static void intel_spi_dump_regs(struct intel_spi *ispi)
193 dev_dbg(ispi->dev, "BFPREG=0x%08x\n", readl(ispi->base + BFPREG));
195 value = readl(ispi->base + HSFSTS_CTL);
196 dev_dbg(ispi->dev, "HSFSTS_CTL=0x%08x\n", value);
197 if (value & HSFSTS_CTL_FLOCKDN)
198 dev_dbg(ispi->dev, "-> Locked\n");
200 dev_dbg(ispi->dev, "FADDR=0x%08x\n", readl(ispi->base + FADDR));
201 dev_dbg(ispi->dev, "DLOCK=0x%08x\n", readl(ispi->base + DLOCK));
203 for (i = 0; i < 16; i++)
204 dev_dbg(ispi->dev, "FDATA(%d)=0x%08x\n",
205 i, readl(ispi->base + FDATA(i)));
207 dev_dbg(ispi->dev, "FRACC=0x%08x\n", readl(ispi->base + FRACC));
209 for (i = 0; i < ispi->nregions; i++)
210 dev_dbg(ispi->dev, "FREG(%d)=0x%08x\n", i,
211 readl(ispi->base + FREG(i)));
212 for (i = 0; i < ispi->pr_num; i++)
213 dev_dbg(ispi->dev, "PR(%d)=0x%08x\n", i,
214 readl(ispi->pregs + PR(i)));
217 value = readl(ispi->sregs + SSFSTS_CTL);
218 dev_dbg(ispi->dev, "SSFSTS_CTL=0x%08x\n", value);
219 dev_dbg(ispi->dev, "PREOP_OPTYPE=0x%08x\n",
220 readl(ispi->sregs + PREOP_OPTYPE));
221 dev_dbg(ispi->dev, "OPMENU0=0x%08x\n",
222 readl(ispi->sregs + OPMENU0));
223 dev_dbg(ispi->dev, "OPMENU1=0x%08x\n",
224 readl(ispi->sregs + OPMENU1));
227 dev_dbg(ispi->dev, "LVSCC=0x%08x\n", readl(ispi->base + LVSCC));
228 dev_dbg(ispi->dev, "UVSCC=0x%08x\n", readl(ispi->base + UVSCC));
230 dev_dbg(ispi->dev, "Protected regions:\n");
231 for (i = 0; i < ispi->pr_num; i++) {
234 value = readl(ispi->pregs + PR(i));
235 if (!(value & (PR_WPE | PR_RPE)))
238 limit = (value & PR_LIMIT_MASK) >> PR_LIMIT_SHIFT;
239 base = value & PR_BASE_MASK;
241 dev_dbg(ispi->dev, " %02d base: 0x%08x limit: 0x%08x [%c%c]\n",
242 i, base << 12, (limit << 12) | 0xfff,
243 value & PR_WPE ? 'W' : '.', value & PR_RPE ? 'R' : '.');
246 dev_dbg(ispi->dev, "Flash regions:\n");
247 for (i = 0; i < ispi->nregions; i++) {
248 u32 region, base, limit;
250 region = readl(ispi->base + FREG(i));
251 base = region & FREG_BASE_MASK;
252 limit = (region & FREG_LIMIT_MASK) >> FREG_LIMIT_SHIFT;
254 if (base >= limit || (i > 0 && limit == 0))
255 dev_dbg(ispi->dev, " %02d disabled\n", i);
257 dev_dbg(ispi->dev, " %02d base: 0x%08x limit: 0x%08x\n",
258 i, base << 12, (limit << 12) | 0xfff);
261 dev_dbg(ispi->dev, "Using %cW sequencer for register access\n",
262 ispi->swseq_reg ? 'S' : 'H');
263 dev_dbg(ispi->dev, "Using %cW sequencer for erase operation\n",
264 ispi->swseq_erase ? 'S' : 'H');
267 /* Reads max INTEL_SPI_FIFO_SZ bytes from the device fifo */
268 static int intel_spi_read_block(struct intel_spi *ispi, void *buf, size_t size)
273 if (size > INTEL_SPI_FIFO_SZ)
277 bytes = min_t(size_t, size, 4);
278 memcpy_fromio(buf, ispi->base + FDATA(i), bytes);
287 /* Writes max INTEL_SPI_FIFO_SZ bytes to the device fifo */
288 static int intel_spi_write_block(struct intel_spi *ispi, const void *buf,
294 if (size > INTEL_SPI_FIFO_SZ)
298 bytes = min_t(size_t, size, 4);
299 memcpy_toio(ispi->base + FDATA(i), buf, bytes);
308 static int intel_spi_wait_hw_busy(struct intel_spi *ispi)
312 return readl_poll_timeout(ispi->base + HSFSTS_CTL, val,
313 !(val & HSFSTS_CTL_SCIP), 0,
314 INTEL_SPI_TIMEOUT * 1000);
317 static int intel_spi_wait_sw_busy(struct intel_spi *ispi)
321 return readl_poll_timeout(ispi->sregs + SSFSTS_CTL, val,
322 !(val & SSFSTS_CTL_SCIP), 0,
323 INTEL_SPI_TIMEOUT * 1000);
326 static bool intel_spi_set_writeable(struct intel_spi *ispi)
328 if (!ispi->info->set_writeable)
331 return ispi->info->set_writeable(ispi->base, ispi->info->data);
334 static int intel_spi_opcode_index(struct intel_spi *ispi, u8 opcode, int optype)
340 for (i = 0; i < ARRAY_SIZE(ispi->opcodes); i++)
341 if (ispi->opcodes[i] == opcode)
347 /* The lock is off, so just use index 0 */
348 writel(opcode, ispi->sregs + OPMENU0);
349 preop = readw(ispi->sregs + PREOP_OPTYPE);
350 writel(optype << 16 | preop, ispi->sregs + PREOP_OPTYPE);
355 static int intel_spi_hw_cycle(struct intel_spi *ispi, u8 opcode, size_t len)
360 val = readl(ispi->base + HSFSTS_CTL);
361 val &= ~(HSFSTS_CTL_FCYCLE_MASK | HSFSTS_CTL_FDBC_MASK);
365 val |= HSFSTS_CTL_FCYCLE_RDID;
368 val |= HSFSTS_CTL_FCYCLE_WRSR;
371 val |= HSFSTS_CTL_FCYCLE_RDSR;
377 if (len > INTEL_SPI_FIFO_SZ)
380 val |= (len - 1) << HSFSTS_CTL_FDBC_SHIFT;
381 val |= HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
382 val |= HSFSTS_CTL_FGO;
383 writel(val, ispi->base + HSFSTS_CTL);
385 ret = intel_spi_wait_hw_busy(ispi);
389 status = readl(ispi->base + HSFSTS_CTL);
390 if (status & HSFSTS_CTL_FCERR)
392 else if (status & HSFSTS_CTL_AEL)
398 static int intel_spi_sw_cycle(struct intel_spi *ispi, u8 opcode, size_t len,
405 ret = intel_spi_opcode_index(ispi, opcode, optype);
409 if (len > INTEL_SPI_FIFO_SZ)
413 * Always clear it after each SW sequencer operation regardless
414 * of whether it is successful or not.
416 atomic_preopcode = ispi->atomic_preopcode;
417 ispi->atomic_preopcode = 0;
419 /* Only mark 'Data Cycle' bit when there is data to be transferred */
421 val = ((len - 1) << SSFSTS_CTL_DBC_SHIFT) | SSFSTS_CTL_DS;
422 val |= ret << SSFSTS_CTL_COP_SHIFT;
423 val |= SSFSTS_CTL_FCERR | SSFSTS_CTL_FDONE;
424 val |= SSFSTS_CTL_SCGO;
425 if (atomic_preopcode) {
429 case OPTYPE_WRITE_NO_ADDR:
430 case OPTYPE_WRITE_WITH_ADDR:
431 /* Pick matching preopcode for the atomic sequence */
432 preop = readw(ispi->sregs + PREOP_OPTYPE);
433 if ((preop & 0xff) == atomic_preopcode)
435 else if ((preop >> 8) == atomic_preopcode)
436 val |= SSFSTS_CTL_SPOP;
440 /* Enable atomic sequence */
441 val |= SSFSTS_CTL_ACS;
448 writel(val, ispi->sregs + SSFSTS_CTL);
450 ret = intel_spi_wait_sw_busy(ispi);
454 status = readl(ispi->sregs + SSFSTS_CTL);
455 if (status & SSFSTS_CTL_FCERR)
457 else if (status & SSFSTS_CTL_AEL)
463 static u32 intel_spi_chip_addr(const struct intel_spi *ispi,
464 const struct spi_mem *mem)
466 /* Pick up the correct start address */
469 return mem->spi->chip_select == 1 ? ispi->chip0_size : 0;
472 static int intel_spi_read_reg(struct intel_spi *ispi, const struct spi_mem *mem,
473 const struct intel_spi_mem_op *iop,
474 const struct spi_mem_op *op)
476 size_t nbytes = op->data.nbytes;
477 u8 opcode = op->cmd.opcode;
480 writel(intel_spi_chip_addr(ispi, mem), ispi->base + FADDR);
483 ret = intel_spi_sw_cycle(ispi, opcode, nbytes,
484 OPTYPE_READ_NO_ADDR);
486 ret = intel_spi_hw_cycle(ispi, opcode, nbytes);
491 return intel_spi_read_block(ispi, op->data.buf.in, nbytes);
494 static int intel_spi_write_reg(struct intel_spi *ispi, const struct spi_mem *mem,
495 const struct intel_spi_mem_op *iop,
496 const struct spi_mem_op *op)
498 size_t nbytes = op->data.nbytes;
499 u8 opcode = op->cmd.opcode;
503 * This is handled with atomic operation and preop code in Intel
504 * controller so we only verify that it is available. If the
505 * controller is not locked, program the opcode to the PREOP
506 * register for later use.
508 * When hardware sequencer is used there is no need to program
509 * any opcodes (it handles them automatically as part of a command).
511 if (opcode == SPINOR_OP_WREN) {
514 if (!ispi->swseq_reg)
517 preop = readw(ispi->sregs + PREOP_OPTYPE);
518 if ((preop & 0xff) != opcode && (preop >> 8) != opcode) {
521 writel(opcode, ispi->sregs + PREOP_OPTYPE);
525 * This enables atomic sequence on next SW sycle. Will
526 * be cleared after next operation.
528 ispi->atomic_preopcode = opcode;
533 * We hope that HW sequencer will do the right thing automatically and
534 * with the SW sequencer we cannot use preopcode anyway, so just ignore
535 * the Write Disable operation and pretend it was completed
538 if (opcode == SPINOR_OP_WRDI)
541 writel(intel_spi_chip_addr(ispi, mem), ispi->base + FADDR);
543 /* Write the value beforehand */
544 ret = intel_spi_write_block(ispi, op->data.buf.out, nbytes);
549 return intel_spi_sw_cycle(ispi, opcode, nbytes,
550 OPTYPE_WRITE_NO_ADDR);
551 return intel_spi_hw_cycle(ispi, opcode, nbytes);
554 static int intel_spi_read(struct intel_spi *ispi, const struct spi_mem *mem,
555 const struct intel_spi_mem_op *iop,
556 const struct spi_mem_op *op)
558 u32 addr = intel_spi_chip_addr(ispi, mem) + op->addr.val;
559 size_t block_size, nbytes = op->data.nbytes;
560 void *read_buf = op->data.buf.in;
565 * Atomic sequence is not expected with HW sequencer reads. Make
566 * sure it is cleared regardless.
568 if (WARN_ON_ONCE(ispi->atomic_preopcode))
569 ispi->atomic_preopcode = 0;
572 block_size = min_t(size_t, nbytes, INTEL_SPI_FIFO_SZ);
574 /* Read cannot cross 4K boundary */
575 block_size = min_t(loff_t, addr + block_size,
576 round_up(addr + 1, SZ_4K)) - addr;
578 writel(addr, ispi->base + FADDR);
580 val = readl(ispi->base + HSFSTS_CTL);
581 val &= ~(HSFSTS_CTL_FDBC_MASK | HSFSTS_CTL_FCYCLE_MASK);
582 val |= HSFSTS_CTL_AEL | HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
583 val |= (block_size - 1) << HSFSTS_CTL_FDBC_SHIFT;
584 val |= HSFSTS_CTL_FCYCLE_READ;
585 val |= HSFSTS_CTL_FGO;
586 writel(val, ispi->base + HSFSTS_CTL);
588 ret = intel_spi_wait_hw_busy(ispi);
592 status = readl(ispi->base + HSFSTS_CTL);
593 if (status & HSFSTS_CTL_FCERR)
595 else if (status & HSFSTS_CTL_AEL)
599 dev_err(ispi->dev, "read error: %x: %#x\n", addr, status);
603 ret = intel_spi_read_block(ispi, read_buf, block_size);
607 nbytes -= block_size;
609 read_buf += block_size;
615 static int intel_spi_write(struct intel_spi *ispi, const struct spi_mem *mem,
616 const struct intel_spi_mem_op *iop,
617 const struct spi_mem_op *op)
619 u32 addr = intel_spi_chip_addr(ispi, mem) + op->addr.val;
620 size_t block_size, nbytes = op->data.nbytes;
621 const void *write_buf = op->data.buf.out;
625 /* Not needed with HW sequencer write, make sure it is cleared */
626 ispi->atomic_preopcode = 0;
629 block_size = min_t(size_t, nbytes, INTEL_SPI_FIFO_SZ);
631 /* Write cannot cross 4K boundary */
632 block_size = min_t(loff_t, addr + block_size,
633 round_up(addr + 1, SZ_4K)) - addr;
635 writel(addr, ispi->base + FADDR);
637 val = readl(ispi->base + HSFSTS_CTL);
638 val &= ~(HSFSTS_CTL_FDBC_MASK | HSFSTS_CTL_FCYCLE_MASK);
639 val |= HSFSTS_CTL_AEL | HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
640 val |= (block_size - 1) << HSFSTS_CTL_FDBC_SHIFT;
641 val |= HSFSTS_CTL_FCYCLE_WRITE;
643 ret = intel_spi_write_block(ispi, write_buf, block_size);
645 dev_err(ispi->dev, "failed to write block\n");
649 /* Start the write now */
650 val |= HSFSTS_CTL_FGO;
651 writel(val, ispi->base + HSFSTS_CTL);
653 ret = intel_spi_wait_hw_busy(ispi);
655 dev_err(ispi->dev, "timeout\n");
659 status = readl(ispi->base + HSFSTS_CTL);
660 if (status & HSFSTS_CTL_FCERR)
662 else if (status & HSFSTS_CTL_AEL)
666 dev_err(ispi->dev, "write error: %x: %#x\n", addr, status);
670 nbytes -= block_size;
672 write_buf += block_size;
678 static int intel_spi_erase(struct intel_spi *ispi, const struct spi_mem *mem,
679 const struct intel_spi_mem_op *iop,
680 const struct spi_mem_op *op)
682 u32 addr = intel_spi_chip_addr(ispi, mem) + op->addr.val;
683 u8 opcode = op->cmd.opcode;
687 writel(addr, ispi->base + FADDR);
689 if (ispi->swseq_erase)
690 return intel_spi_sw_cycle(ispi, opcode, 0,
691 OPTYPE_WRITE_WITH_ADDR);
693 /* Not needed with HW sequencer erase, make sure it is cleared */
694 ispi->atomic_preopcode = 0;
696 val = readl(ispi->base + HSFSTS_CTL);
697 val &= ~(HSFSTS_CTL_FDBC_MASK | HSFSTS_CTL_FCYCLE_MASK);
698 val |= HSFSTS_CTL_AEL | HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
699 val |= HSFSTS_CTL_FGO;
700 val |= iop->replacement_op;
701 writel(val, ispi->base + HSFSTS_CTL);
703 ret = intel_spi_wait_hw_busy(ispi);
707 status = readl(ispi->base + HSFSTS_CTL);
708 if (status & HSFSTS_CTL_FCERR)
710 if (status & HSFSTS_CTL_AEL)
716 static bool intel_spi_cmp_mem_op(const struct intel_spi_mem_op *iop,
717 const struct spi_mem_op *op)
719 if (iop->mem_op.cmd.nbytes != op->cmd.nbytes ||
720 iop->mem_op.cmd.buswidth != op->cmd.buswidth ||
721 iop->mem_op.cmd.dtr != op->cmd.dtr ||
722 iop->mem_op.cmd.opcode != op->cmd.opcode)
725 if (iop->mem_op.addr.nbytes != op->addr.nbytes ||
726 iop->mem_op.addr.dtr != op->addr.dtr)
729 if (iop->mem_op.data.dir != op->data.dir ||
730 iop->mem_op.data.dtr != op->data.dtr)
733 if (iop->mem_op.data.dir != SPI_MEM_NO_DATA) {
734 if (iop->mem_op.data.buswidth != op->data.buswidth)
741 static const struct intel_spi_mem_op *
742 intel_spi_match_mem_op(struct intel_spi *ispi, const struct spi_mem_op *op)
744 const struct intel_spi_mem_op *iop;
746 for (iop = ispi->mem_ops; iop->mem_op.cmd.opcode; iop++) {
747 if (intel_spi_cmp_mem_op(iop, op))
751 return iop->mem_op.cmd.opcode ? iop : NULL;
754 static bool intel_spi_supports_mem_op(struct spi_mem *mem,
755 const struct spi_mem_op *op)
757 struct intel_spi *ispi = spi_master_get_devdata(mem->spi->master);
758 const struct intel_spi_mem_op *iop;
760 iop = intel_spi_match_mem_op(ispi, op);
762 dev_dbg(ispi->dev, "%#x not supported\n", op->cmd.opcode);
767 * For software sequencer check that the opcode is actually
768 * present in the opmenu if it is locked.
770 if (ispi->swseq_reg && ispi->locked) {
773 /* Check if it is in the locked opcodes list */
774 for (i = 0; i < ARRAY_SIZE(ispi->opcodes); i++) {
775 if (ispi->opcodes[i] == op->cmd.opcode)
779 dev_dbg(ispi->dev, "%#x not supported\n", op->cmd.opcode);
786 static int intel_spi_exec_mem_op(struct spi_mem *mem, const struct spi_mem_op *op)
788 struct intel_spi *ispi = spi_master_get_devdata(mem->spi->master);
789 const struct intel_spi_mem_op *iop;
791 iop = intel_spi_match_mem_op(ispi, op);
795 return iop->exec_op(ispi, mem, iop, op);
798 static const char *intel_spi_get_name(struct spi_mem *mem)
800 const struct intel_spi *ispi = spi_master_get_devdata(mem->spi->master);
803 * Return name of the flash controller device to be compatible
804 * with the MTD version.
806 return dev_name(ispi->dev);
809 static int intel_spi_dirmap_create(struct spi_mem_dirmap_desc *desc)
811 struct intel_spi *ispi = spi_master_get_devdata(desc->mem->spi->master);
812 const struct intel_spi_mem_op *iop;
814 iop = intel_spi_match_mem_op(ispi, &desc->info.op_tmpl);
818 desc->priv = (void *)iop;
822 static ssize_t intel_spi_dirmap_read(struct spi_mem_dirmap_desc *desc, u64 offs,
823 size_t len, void *buf)
825 struct intel_spi *ispi = spi_master_get_devdata(desc->mem->spi->master);
826 const struct intel_spi_mem_op *iop = desc->priv;
827 struct spi_mem_op op = desc->info.op_tmpl;
830 /* Fill in the gaps */
832 op.data.nbytes = len;
833 op.data.buf.in = buf;
835 ret = iop->exec_op(ispi, desc->mem, iop, &op);
836 return ret ? ret : len;
839 static ssize_t intel_spi_dirmap_write(struct spi_mem_dirmap_desc *desc, u64 offs,
840 size_t len, const void *buf)
842 struct intel_spi *ispi = spi_master_get_devdata(desc->mem->spi->master);
843 const struct intel_spi_mem_op *iop = desc->priv;
844 struct spi_mem_op op = desc->info.op_tmpl;
848 op.data.nbytes = len;
849 op.data.buf.out = buf;
851 ret = iop->exec_op(ispi, desc->mem, iop, &op);
852 return ret ? ret : len;
855 static const struct spi_controller_mem_ops intel_spi_mem_ops = {
856 .supports_op = intel_spi_supports_mem_op,
857 .exec_op = intel_spi_exec_mem_op,
858 .get_name = intel_spi_get_name,
859 .dirmap_create = intel_spi_dirmap_create,
860 .dirmap_read = intel_spi_dirmap_read,
861 .dirmap_write = intel_spi_dirmap_write,
864 #define INTEL_SPI_OP_ADDR(__nbytes) \
866 .nbytes = __nbytes, \
869 #define INTEL_SPI_OP_NO_DATA \
871 .dir = SPI_MEM_NO_DATA, \
874 #define INTEL_SPI_OP_DATA_IN(__buswidth) \
876 .dir = SPI_MEM_DATA_IN, \
877 .buswidth = __buswidth, \
880 #define INTEL_SPI_OP_DATA_OUT(__buswidth) \
882 .dir = SPI_MEM_DATA_OUT, \
883 .buswidth = __buswidth, \
886 #define INTEL_SPI_MEM_OP(__cmd, __addr, __data, __exec_op) \
893 .exec_op = __exec_op, \
896 #define INTEL_SPI_MEM_OP_REPL(__cmd, __addr, __data, __exec_op, __repl) \
903 .exec_op = __exec_op, \
904 .replacement_op = __repl, \
908 * The controller handles pretty much everything internally based on the
909 * SFDP data but we want to make sure we only support the operations
910 * actually possible. Only check buswidth and transfer direction, the
911 * core validates data.
913 #define INTEL_SPI_GENERIC_OPS \
914 /* Status register operations */ \
915 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDID, 1), \
916 SPI_MEM_OP_NO_ADDR, \
917 INTEL_SPI_OP_DATA_IN(1), \
918 intel_spi_read_reg), \
919 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR, 1), \
920 SPI_MEM_OP_NO_ADDR, \
921 INTEL_SPI_OP_DATA_IN(1), \
922 intel_spi_read_reg), \
923 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRSR, 1), \
924 SPI_MEM_OP_NO_ADDR, \
925 INTEL_SPI_OP_DATA_OUT(1), \
926 intel_spi_write_reg), \
928 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ, 1), \
929 INTEL_SPI_OP_ADDR(3), \
930 INTEL_SPI_OP_DATA_IN(1), \
932 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ, 1), \
933 INTEL_SPI_OP_ADDR(3), \
934 INTEL_SPI_OP_DATA_IN(2), \
936 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ, 1), \
937 INTEL_SPI_OP_ADDR(3), \
938 INTEL_SPI_OP_DATA_IN(4), \
940 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ, 1), \
941 INTEL_SPI_OP_ADDR(4), \
942 INTEL_SPI_OP_DATA_IN(1), \
944 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ, 1), \
945 INTEL_SPI_OP_ADDR(4), \
946 INTEL_SPI_OP_DATA_IN(2), \
948 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ, 1), \
949 INTEL_SPI_OP_ADDR(4), \
950 INTEL_SPI_OP_DATA_IN(4), \
953 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST, 1), \
954 INTEL_SPI_OP_ADDR(3), \
955 INTEL_SPI_OP_DATA_IN(1), \
957 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST, 1), \
958 INTEL_SPI_OP_ADDR(3), \
959 INTEL_SPI_OP_DATA_IN(2), \
961 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST, 1), \
962 INTEL_SPI_OP_ADDR(3), \
963 INTEL_SPI_OP_DATA_IN(4), \
965 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST, 1), \
966 INTEL_SPI_OP_ADDR(4), \
967 INTEL_SPI_OP_DATA_IN(1), \
969 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST, 1), \
970 INTEL_SPI_OP_ADDR(4), \
971 INTEL_SPI_OP_DATA_IN(2), \
973 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST, 1), \
974 INTEL_SPI_OP_ADDR(4), \
975 INTEL_SPI_OP_DATA_IN(4), \
977 /* Read with 4-byte address opcode */ \
978 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_4B, 1), \
979 INTEL_SPI_OP_ADDR(4), \
980 INTEL_SPI_OP_DATA_IN(1), \
982 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_4B, 1), \
983 INTEL_SPI_OP_ADDR(4), \
984 INTEL_SPI_OP_DATA_IN(2), \
986 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_4B, 1), \
987 INTEL_SPI_OP_ADDR(4), \
988 INTEL_SPI_OP_DATA_IN(4), \
990 /* Fast read with 4-byte address opcode */ \
991 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST_4B, 1), \
992 INTEL_SPI_OP_ADDR(4), \
993 INTEL_SPI_OP_DATA_IN(1), \
995 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST_4B, 1), \
996 INTEL_SPI_OP_ADDR(4), \
997 INTEL_SPI_OP_DATA_IN(2), \
999 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST_4B, 1), \
1000 INTEL_SPI_OP_ADDR(4), \
1001 INTEL_SPI_OP_DATA_IN(4), \
1003 /* Write operations */ \
1004 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_PP, 1), \
1005 INTEL_SPI_OP_ADDR(3), \
1006 INTEL_SPI_OP_DATA_OUT(1), \
1008 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_PP, 1), \
1009 INTEL_SPI_OP_ADDR(4), \
1010 INTEL_SPI_OP_DATA_OUT(1), \
1012 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_PP_4B, 1), \
1013 INTEL_SPI_OP_ADDR(4), \
1014 INTEL_SPI_OP_DATA_OUT(1), \
1016 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WREN, 1), \
1017 SPI_MEM_OP_NO_ADDR, \
1018 SPI_MEM_OP_NO_DATA, \
1019 intel_spi_write_reg), \
1020 INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRDI, 1), \
1021 SPI_MEM_OP_NO_ADDR, \
1022 SPI_MEM_OP_NO_DATA, \
1023 intel_spi_write_reg), \
1024 /* Erase operations */ \
1025 INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_BE_4K, 1), \
1026 INTEL_SPI_OP_ADDR(3), \
1027 SPI_MEM_OP_NO_DATA, \
1029 HSFSTS_CTL_FCYCLE_ERASE), \
1030 INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_BE_4K, 1), \
1031 INTEL_SPI_OP_ADDR(4), \
1032 SPI_MEM_OP_NO_DATA, \
1034 HSFSTS_CTL_FCYCLE_ERASE), \
1035 INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_BE_4K_4B, 1), \
1036 INTEL_SPI_OP_ADDR(4), \
1037 SPI_MEM_OP_NO_DATA, \
1039 HSFSTS_CTL_FCYCLE_ERASE) \
1041 static const struct intel_spi_mem_op generic_mem_ops[] = {
1042 INTEL_SPI_GENERIC_OPS,
1046 static const struct intel_spi_mem_op erase_64k_mem_ops[] = {
1047 INTEL_SPI_GENERIC_OPS,
1048 /* 64k sector erase operations */
1049 INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_SE, 1),
1050 INTEL_SPI_OP_ADDR(3),
1053 HSFSTS_CTL_FCYCLE_ERASE_64K),
1054 INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_SE, 1),
1055 INTEL_SPI_OP_ADDR(4),
1058 HSFSTS_CTL_FCYCLE_ERASE_64K),
1059 INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_SE_4B, 1),
1060 INTEL_SPI_OP_ADDR(4),
1063 HSFSTS_CTL_FCYCLE_ERASE_64K),
1067 static int intel_spi_init(struct intel_spi *ispi)
1069 u32 opmenu0, opmenu1, lvscc, uvscc, val;
1070 bool erase_64k = false;
1073 switch (ispi->info->type) {
1075 ispi->sregs = ispi->base + BYT_SSFSTS_CTL;
1076 ispi->pregs = ispi->base + BYT_PR;
1077 ispi->nregions = BYT_FREG_NUM;
1078 ispi->pr_num = BYT_PR_NUM;
1079 ispi->swseq_reg = true;
1083 ispi->sregs = ispi->base + LPT_SSFSTS_CTL;
1084 ispi->pregs = ispi->base + LPT_PR;
1085 ispi->nregions = LPT_FREG_NUM;
1086 ispi->pr_num = LPT_PR_NUM;
1087 ispi->swseq_reg = true;
1091 ispi->sregs = ispi->base + BXT_SSFSTS_CTL;
1092 ispi->pregs = ispi->base + BXT_PR;
1093 ispi->nregions = BXT_FREG_NUM;
1094 ispi->pr_num = BXT_PR_NUM;
1100 ispi->pregs = ispi->base + CNL_PR;
1101 ispi->nregions = CNL_FREG_NUM;
1102 ispi->pr_num = CNL_PR_NUM;
1110 /* Try to disable write protection if user asked to do so */
1111 if (writeable && !intel_spi_set_writeable(ispi)) {
1112 dev_warn(ispi->dev, "can't disable chip write protection\n");
1116 /* Disable #SMI generation from HW sequencer */
1117 val = readl(ispi->base + HSFSTS_CTL);
1118 val &= ~HSFSTS_CTL_FSMIE;
1119 writel(val, ispi->base + HSFSTS_CTL);
1122 * Determine whether erase operation should use HW or SW sequencer.
1124 * The HW sequencer has a predefined list of opcodes, with only the
1125 * erase opcode being programmable in LVSCC and UVSCC registers.
1126 * If these registers don't contain a valid erase opcode, erase
1127 * cannot be done using HW sequencer.
1129 lvscc = readl(ispi->base + LVSCC);
1130 uvscc = readl(ispi->base + UVSCC);
1131 if (!(lvscc & ERASE_OPCODE_MASK) || !(uvscc & ERASE_OPCODE_MASK))
1132 ispi->swseq_erase = true;
1133 /* SPI controller on Intel BXT supports 64K erase opcode */
1134 if (ispi->info->type == INTEL_SPI_BXT && !ispi->swseq_erase)
1135 if (!(lvscc & ERASE_64K_OPCODE_MASK) ||
1136 !(uvscc & ERASE_64K_OPCODE_MASK))
1139 if (!ispi->sregs && (ispi->swseq_reg || ispi->swseq_erase)) {
1140 dev_err(ispi->dev, "software sequencer not supported, but required\n");
1145 * Some controllers can only do basic operations using hardware
1146 * sequencer. All other operations are supposed to be carried out
1147 * using software sequencer.
1149 if (ispi->swseq_reg) {
1150 /* Disable #SMI generation from SW sequencer */
1151 val = readl(ispi->sregs + SSFSTS_CTL);
1152 val &= ~SSFSTS_CTL_FSMIE;
1153 writel(val, ispi->sregs + SSFSTS_CTL);
1156 /* Check controller's lock status */
1157 val = readl(ispi->base + HSFSTS_CTL);
1158 ispi->locked = !!(val & HSFSTS_CTL_FLOCKDN);
1160 if (ispi->locked && ispi->sregs) {
1162 * BIOS programs allowed opcodes and then locks down the
1163 * register. So read back what opcodes it decided to support.
1164 * That's the set we are going to support as well.
1166 opmenu0 = readl(ispi->sregs + OPMENU0);
1167 opmenu1 = readl(ispi->sregs + OPMENU1);
1169 if (opmenu0 && opmenu1) {
1170 for (i = 0; i < ARRAY_SIZE(ispi->opcodes) / 2; i++) {
1171 ispi->opcodes[i] = opmenu0 >> i * 8;
1172 ispi->opcodes[i + 4] = opmenu1 >> i * 8;
1178 dev_dbg(ispi->dev, "Using erase_64k memory operations");
1179 ispi->mem_ops = erase_64k_mem_ops;
1181 dev_dbg(ispi->dev, "Using generic memory operations");
1182 ispi->mem_ops = generic_mem_ops;
1185 intel_spi_dump_regs(ispi);
1189 static bool intel_spi_is_protected(const struct intel_spi *ispi,
1190 unsigned int base, unsigned int limit)
1194 for (i = 0; i < ispi->pr_num; i++) {
1195 u32 pr_base, pr_limit, pr_value;
1197 pr_value = readl(ispi->pregs + PR(i));
1198 if (!(pr_value & (PR_WPE | PR_RPE)))
1201 pr_limit = (pr_value & PR_LIMIT_MASK) >> PR_LIMIT_SHIFT;
1202 pr_base = pr_value & PR_BASE_MASK;
1204 if (pr_base >= base && pr_limit <= limit)
1212 * There will be a single partition holding all enabled flash regions. We
1215 static void intel_spi_fill_partition(struct intel_spi *ispi,
1216 struct mtd_partition *part)
1221 memset(part, 0, sizeof(*part));
1223 /* Start from the mandatory descriptor region */
1225 part->name = "BIOS";
1228 * Now try to find where this partition ends based on the flash
1231 for (i = 1; i < ispi->nregions; i++) {
1232 u32 region, base, limit;
1234 region = readl(ispi->base + FREG(i));
1235 base = region & FREG_BASE_MASK;
1236 limit = (region & FREG_LIMIT_MASK) >> FREG_LIMIT_SHIFT;
1238 if (base >= limit || limit == 0)
1242 * If any of the regions have protection bits set, make the
1243 * whole partition read-only to be on the safe side.
1245 * Also if the user did not ask the chip to be writeable
1248 if (!writeable || intel_spi_is_protected(ispi, base, limit))
1249 part->mask_flags |= MTD_WRITEABLE;
1251 end = (limit << 12) + 4096;
1252 if (end > part->size)
1257 static int intel_spi_read_desc(struct intel_spi *ispi)
1259 struct spi_mem_op op =
1260 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ, 0),
1261 SPI_MEM_OP_ADDR(3, 0, 0),
1262 SPI_MEM_OP_NO_DUMMY,
1263 SPI_MEM_OP_DATA_IN(0, NULL, 0));
1264 u32 buf[2], nc, fcba, flcomp;
1268 op.data.buf.in = buf;
1269 op.data.nbytes = sizeof(buf);
1271 ret = intel_spi_read(ispi, NULL, NULL, &op);
1273 dev_warn(ispi->dev, "failed to read descriptor\n");
1277 dev_dbg(ispi->dev, "FLVALSIG=0x%08x\n", buf[0]);
1278 dev_dbg(ispi->dev, "FLMAP0=0x%08x\n", buf[1]);
1280 if (buf[0] != FLVALSIG_MAGIC) {
1281 dev_warn(ispi->dev, "descriptor signature not valid\n");
1285 fcba = (buf[1] & FLMAP0_FCBA_MASK) << 4;
1286 dev_dbg(ispi->dev, "FCBA=%#x\n", fcba);
1289 op.data.buf.in = &flcomp;
1290 op.data.nbytes = sizeof(flcomp);
1292 ret = intel_spi_read(ispi, NULL, NULL, &op);
1294 dev_warn(ispi->dev, "failed to read FLCOMP\n");
1298 dev_dbg(ispi->dev, "FLCOMP=0x%08x\n", flcomp);
1300 switch (flcomp & FLCOMP_C0DEN_MASK) {
1301 case FLCOMP_C0DEN_512K:
1302 ispi->chip0_size = SZ_512K;
1304 case FLCOMP_C0DEN_1M:
1305 ispi->chip0_size = SZ_1M;
1307 case FLCOMP_C0DEN_2M:
1308 ispi->chip0_size = SZ_2M;
1310 case FLCOMP_C0DEN_4M:
1311 ispi->chip0_size = SZ_4M;
1313 case FLCOMP_C0DEN_8M:
1314 ispi->chip0_size = SZ_8M;
1316 case FLCOMP_C0DEN_16M:
1317 ispi->chip0_size = SZ_16M;
1319 case FLCOMP_C0DEN_32M:
1320 ispi->chip0_size = SZ_32M;
1322 case FLCOMP_C0DEN_64M:
1323 ispi->chip0_size = SZ_64M;
1329 dev_dbg(ispi->dev, "chip0 size %zd KB\n", ispi->chip0_size / SZ_1K);
1331 nc = (buf[1] & FLMAP0_NC_MASK) >> FLMAP0_NC_SHIFT;
1333 ispi->master->num_chipselect = 1;
1335 ispi->master->num_chipselect = 2;
1339 dev_dbg(ispi->dev, "%u flash components found\n",
1340 ispi->master->num_chipselect);
1344 static int intel_spi_populate_chip(struct intel_spi *ispi)
1346 struct flash_platform_data *pdata;
1347 struct spi_board_info chip;
1350 pdata = devm_kzalloc(ispi->dev, sizeof(*pdata), GFP_KERNEL);
1354 pdata->nr_parts = 1;
1355 pdata->parts = devm_kcalloc(ispi->dev, pdata->nr_parts,
1356 sizeof(*pdata->parts), GFP_KERNEL);
1360 intel_spi_fill_partition(ispi, pdata->parts);
1362 memset(&chip, 0, sizeof(chip));
1363 snprintf(chip.modalias, 8, "spi-nor");
1364 chip.platform_data = pdata;
1366 if (!spi_new_device(ispi->master, &chip))
1369 /* Add the second chip if present */
1370 if (ispi->master->num_chipselect < 2)
1373 ret = intel_spi_read_desc(ispi);
1377 chip.platform_data = NULL;
1378 chip.chip_select = 1;
1380 if (!spi_new_device(ispi->master, &chip))
1386 * intel_spi_probe() - Probe the Intel SPI flash controller
1387 * @dev: Pointer to the parent device
1388 * @mem: MMIO resource
1389 * @info: Platform specific information
1391 * Probes Intel SPI flash controller and creates the flash chip device.
1392 * Returns %0 on success and negative errno in case of failure.
1394 int intel_spi_probe(struct device *dev, struct resource *mem,
1395 const struct intel_spi_boardinfo *info)
1397 struct spi_controller *master;
1398 struct intel_spi *ispi;
1401 master = devm_spi_alloc_master(dev, sizeof(*ispi));
1405 master->mem_ops = &intel_spi_mem_ops;
1407 ispi = spi_master_get_devdata(master);
1409 ispi->base = devm_ioremap_resource(dev, mem);
1410 if (IS_ERR(ispi->base))
1411 return PTR_ERR(ispi->base);
1414 ispi->master = master;
1417 ret = intel_spi_init(ispi);
1421 ret = devm_spi_register_master(dev, master);
1425 return intel_spi_populate_chip(ispi);
1427 EXPORT_SYMBOL_GPL(intel_spi_probe);
1429 MODULE_DESCRIPTION("Intel PCH/PCU SPI flash core driver");
1430 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1431 MODULE_LICENSE("GPL v2");