1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel PCH/PCU SPI flash driver.
5 * Copyright (C) 2016, Intel Corporation
11 #include <linux/iopoll.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/sizes.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/mtd/partitions.h>
17 #include <linux/mtd/spi-nor.h>
18 #include <linux/platform_data/intel-spi.h>
20 #include "intel-spi.h"
22 /* Offsets are from @ispi->base */
25 #define HSFSTS_CTL 0x04
26 #define HSFSTS_CTL_FSMIE BIT(31)
27 #define HSFSTS_CTL_FDBC_SHIFT 24
28 #define HSFSTS_CTL_FDBC_MASK (0x3f << HSFSTS_CTL_FDBC_SHIFT)
30 #define HSFSTS_CTL_FCYCLE_SHIFT 17
31 #define HSFSTS_CTL_FCYCLE_MASK (0x0f << HSFSTS_CTL_FCYCLE_SHIFT)
32 /* HW sequencer opcodes */
33 #define HSFSTS_CTL_FCYCLE_READ (0x00 << HSFSTS_CTL_FCYCLE_SHIFT)
34 #define HSFSTS_CTL_FCYCLE_WRITE (0x02 << HSFSTS_CTL_FCYCLE_SHIFT)
35 #define HSFSTS_CTL_FCYCLE_ERASE (0x03 << HSFSTS_CTL_FCYCLE_SHIFT)
36 #define HSFSTS_CTL_FCYCLE_ERASE_64K (0x04 << HSFSTS_CTL_FCYCLE_SHIFT)
37 #define HSFSTS_CTL_FCYCLE_RDID (0x06 << HSFSTS_CTL_FCYCLE_SHIFT)
38 #define HSFSTS_CTL_FCYCLE_WRSR (0x07 << HSFSTS_CTL_FCYCLE_SHIFT)
39 #define HSFSTS_CTL_FCYCLE_RDSR (0x08 << HSFSTS_CTL_FCYCLE_SHIFT)
41 #define HSFSTS_CTL_FGO BIT(16)
42 #define HSFSTS_CTL_FLOCKDN BIT(15)
43 #define HSFSTS_CTL_FDV BIT(14)
44 #define HSFSTS_CTL_SCIP BIT(5)
45 #define HSFSTS_CTL_AEL BIT(2)
46 #define HSFSTS_CTL_FCERR BIT(1)
47 #define HSFSTS_CTL_FDONE BIT(0)
51 #define FDATA(n) (0x10 + ((n) * 4))
55 #define FREG(n) (0x54 + ((n) * 4))
56 #define FREG_BASE_MASK 0x3fff
57 #define FREG_LIMIT_SHIFT 16
58 #define FREG_LIMIT_MASK (0x03fff << FREG_LIMIT_SHIFT)
60 /* Offset is from @ispi->pregs */
61 #define PR(n) ((n) * 4)
62 #define PR_WPE BIT(31)
63 #define PR_LIMIT_SHIFT 16
64 #define PR_LIMIT_MASK (0x3fff << PR_LIMIT_SHIFT)
65 #define PR_RPE BIT(15)
66 #define PR_BASE_MASK 0x3fff
68 /* Offsets are from @ispi->sregs */
69 #define SSFSTS_CTL 0x00
70 #define SSFSTS_CTL_FSMIE BIT(23)
71 #define SSFSTS_CTL_DS BIT(22)
72 #define SSFSTS_CTL_DBC_SHIFT 16
73 #define SSFSTS_CTL_SPOP BIT(11)
74 #define SSFSTS_CTL_ACS BIT(10)
75 #define SSFSTS_CTL_SCGO BIT(9)
76 #define SSFSTS_CTL_COP_SHIFT 12
77 #define SSFSTS_CTL_FRS BIT(7)
78 #define SSFSTS_CTL_DOFRS BIT(6)
79 #define SSFSTS_CTL_AEL BIT(4)
80 #define SSFSTS_CTL_FCERR BIT(3)
81 #define SSFSTS_CTL_FDONE BIT(2)
82 #define SSFSTS_CTL_SCIP BIT(0)
84 #define PREOP_OPTYPE 0x04
88 #define OPTYPE_READ_NO_ADDR 0
89 #define OPTYPE_WRITE_NO_ADDR 1
90 #define OPTYPE_READ_WITH_ADDR 2
91 #define OPTYPE_WRITE_WITH_ADDR 3
95 #define BYT_SSFSTS_CTL 0x90
97 #define BYT_BCR_WPD BIT(0)
98 #define BYT_FREG_NUM 5
102 #define LPT_SSFSTS_CTL 0x90
103 #define LPT_FREG_NUM 5
107 #define BXT_SSFSTS_CTL 0xa0
108 #define BXT_FREG_NUM 12
113 #define ERASE_OPCODE_SHIFT 8
114 #define ERASE_OPCODE_MASK (0xff << ERASE_OPCODE_SHIFT)
115 #define ERASE_64K_OPCODE_SHIFT 16
116 #define ERASE_64K_OPCODE_MASK (0xff << ERASE_OPCODE_SHIFT)
118 #define INTEL_SPI_TIMEOUT 5000 /* ms */
119 #define INTEL_SPI_FIFO_SZ 64
122 * struct intel_spi - Driver private data
123 * @dev: Device pointer
124 * @info: Pointer to board specific info
125 * @nor: SPI NOR layer structure
126 * @base: Beginning of MMIO space
127 * @pregs: Start of protection registers
128 * @sregs: Start of software sequencer registers
129 * @nregions: Maximum number of regions
130 * @pr_num: Maximum number of protected range registers
131 * @writeable: Is the chip writeable
132 * @locked: Is SPI setting locked
133 * @swseq_reg: Use SW sequencer in register reads/writes
134 * @swseq_erase: Use SW sequencer in erase operation
135 * @erase_64k: 64k erase supported
136 * @atomic_preopcode: Holds preopcode when atomic sequence is requested
137 * @opcodes: Opcodes which are supported. This are programmed by BIOS
138 * before it locks down the controller.
142 const struct intel_spi_boardinfo *info;
158 static bool writeable;
159 module_param(writeable, bool, 0);
160 MODULE_PARM_DESC(writeable, "Enable write access to SPI flash chip (default=0)");
162 static void intel_spi_dump_regs(struct intel_spi *ispi)
167 dev_dbg(ispi->dev, "BFPREG=0x%08x\n", readl(ispi->base + BFPREG));
169 value = readl(ispi->base + HSFSTS_CTL);
170 dev_dbg(ispi->dev, "HSFSTS_CTL=0x%08x\n", value);
171 if (value & HSFSTS_CTL_FLOCKDN)
172 dev_dbg(ispi->dev, "-> Locked\n");
174 dev_dbg(ispi->dev, "FADDR=0x%08x\n", readl(ispi->base + FADDR));
175 dev_dbg(ispi->dev, "DLOCK=0x%08x\n", readl(ispi->base + DLOCK));
177 for (i = 0; i < 16; i++)
178 dev_dbg(ispi->dev, "FDATA(%d)=0x%08x\n",
179 i, readl(ispi->base + FDATA(i)));
181 dev_dbg(ispi->dev, "FRACC=0x%08x\n", readl(ispi->base + FRACC));
183 for (i = 0; i < ispi->nregions; i++)
184 dev_dbg(ispi->dev, "FREG(%d)=0x%08x\n", i,
185 readl(ispi->base + FREG(i)));
186 for (i = 0; i < ispi->pr_num; i++)
187 dev_dbg(ispi->dev, "PR(%d)=0x%08x\n", i,
188 readl(ispi->pregs + PR(i)));
190 value = readl(ispi->sregs + SSFSTS_CTL);
191 dev_dbg(ispi->dev, "SSFSTS_CTL=0x%08x\n", value);
192 dev_dbg(ispi->dev, "PREOP_OPTYPE=0x%08x\n",
193 readl(ispi->sregs + PREOP_OPTYPE));
194 dev_dbg(ispi->dev, "OPMENU0=0x%08x\n", readl(ispi->sregs + OPMENU0));
195 dev_dbg(ispi->dev, "OPMENU1=0x%08x\n", readl(ispi->sregs + OPMENU1));
197 if (ispi->info->type == INTEL_SPI_BYT)
198 dev_dbg(ispi->dev, "BCR=0x%08x\n", readl(ispi->base + BYT_BCR));
200 dev_dbg(ispi->dev, "LVSCC=0x%08x\n", readl(ispi->base + LVSCC));
201 dev_dbg(ispi->dev, "UVSCC=0x%08x\n", readl(ispi->base + UVSCC));
203 dev_dbg(ispi->dev, "Protected regions:\n");
204 for (i = 0; i < ispi->pr_num; i++) {
207 value = readl(ispi->pregs + PR(i));
208 if (!(value & (PR_WPE | PR_RPE)))
211 limit = (value & PR_LIMIT_MASK) >> PR_LIMIT_SHIFT;
212 base = value & PR_BASE_MASK;
214 dev_dbg(ispi->dev, " %02d base: 0x%08x limit: 0x%08x [%c%c]\n",
215 i, base << 12, (limit << 12) | 0xfff,
216 value & PR_WPE ? 'W' : '.',
217 value & PR_RPE ? 'R' : '.');
220 dev_dbg(ispi->dev, "Flash regions:\n");
221 for (i = 0; i < ispi->nregions; i++) {
222 u32 region, base, limit;
224 region = readl(ispi->base + FREG(i));
225 base = region & FREG_BASE_MASK;
226 limit = (region & FREG_LIMIT_MASK) >> FREG_LIMIT_SHIFT;
228 if (base >= limit || (i > 0 && limit == 0))
229 dev_dbg(ispi->dev, " %02d disabled\n", i);
231 dev_dbg(ispi->dev, " %02d base: 0x%08x limit: 0x%08x\n",
232 i, base << 12, (limit << 12) | 0xfff);
235 dev_dbg(ispi->dev, "Using %cW sequencer for register access\n",
236 ispi->swseq_reg ? 'S' : 'H');
237 dev_dbg(ispi->dev, "Using %cW sequencer for erase operation\n",
238 ispi->swseq_erase ? 'S' : 'H');
241 /* Reads max INTEL_SPI_FIFO_SZ bytes from the device fifo */
242 static int intel_spi_read_block(struct intel_spi *ispi, void *buf, size_t size)
247 if (size > INTEL_SPI_FIFO_SZ)
251 bytes = min_t(size_t, size, 4);
252 memcpy_fromio(buf, ispi->base + FDATA(i), bytes);
261 /* Writes max INTEL_SPI_FIFO_SZ bytes to the device fifo */
262 static int intel_spi_write_block(struct intel_spi *ispi, const void *buf,
268 if (size > INTEL_SPI_FIFO_SZ)
272 bytes = min_t(size_t, size, 4);
273 memcpy_toio(ispi->base + FDATA(i), buf, bytes);
282 static int intel_spi_wait_hw_busy(struct intel_spi *ispi)
286 return readl_poll_timeout(ispi->base + HSFSTS_CTL, val,
287 !(val & HSFSTS_CTL_SCIP), 40,
288 INTEL_SPI_TIMEOUT * 1000);
291 static int intel_spi_wait_sw_busy(struct intel_spi *ispi)
295 return readl_poll_timeout(ispi->sregs + SSFSTS_CTL, val,
296 !(val & SSFSTS_CTL_SCIP), 40,
297 INTEL_SPI_TIMEOUT * 1000);
300 static int intel_spi_init(struct intel_spi *ispi)
302 u32 opmenu0, opmenu1, lvscc, uvscc, val;
305 switch (ispi->info->type) {
307 ispi->sregs = ispi->base + BYT_SSFSTS_CTL;
308 ispi->pregs = ispi->base + BYT_PR;
309 ispi->nregions = BYT_FREG_NUM;
310 ispi->pr_num = BYT_PR_NUM;
311 ispi->swseq_reg = true;
314 /* Disable write protection */
315 val = readl(ispi->base + BYT_BCR);
316 if (!(val & BYT_BCR_WPD)) {
318 writel(val, ispi->base + BYT_BCR);
319 val = readl(ispi->base + BYT_BCR);
322 ispi->writeable = !!(val & BYT_BCR_WPD);
328 ispi->sregs = ispi->base + LPT_SSFSTS_CTL;
329 ispi->pregs = ispi->base + LPT_PR;
330 ispi->nregions = LPT_FREG_NUM;
331 ispi->pr_num = LPT_PR_NUM;
332 ispi->swseq_reg = true;
336 ispi->sregs = ispi->base + BXT_SSFSTS_CTL;
337 ispi->pregs = ispi->base + BXT_PR;
338 ispi->nregions = BXT_FREG_NUM;
339 ispi->pr_num = BXT_PR_NUM;
340 ispi->erase_64k = true;
347 /* Disable #SMI generation from HW sequencer */
348 val = readl(ispi->base + HSFSTS_CTL);
349 val &= ~HSFSTS_CTL_FSMIE;
350 writel(val, ispi->base + HSFSTS_CTL);
353 * Determine whether erase operation should use HW or SW sequencer.
355 * The HW sequencer has a predefined list of opcodes, with only the
356 * erase opcode being programmable in LVSCC and UVSCC registers.
357 * If these registers don't contain a valid erase opcode, erase
358 * cannot be done using HW sequencer.
360 lvscc = readl(ispi->base + LVSCC);
361 uvscc = readl(ispi->base + UVSCC);
362 if (!(lvscc & ERASE_OPCODE_MASK) || !(uvscc & ERASE_OPCODE_MASK))
363 ispi->swseq_erase = true;
364 /* SPI controller on Intel BXT supports 64K erase opcode */
365 if (ispi->info->type == INTEL_SPI_BXT && !ispi->swseq_erase)
366 if (!(lvscc & ERASE_64K_OPCODE_MASK) ||
367 !(uvscc & ERASE_64K_OPCODE_MASK))
368 ispi->erase_64k = false;
371 * Some controllers can only do basic operations using hardware
372 * sequencer. All other operations are supposed to be carried out
373 * using software sequencer.
375 if (ispi->swseq_reg) {
376 /* Disable #SMI generation from SW sequencer */
377 val = readl(ispi->sregs + SSFSTS_CTL);
378 val &= ~SSFSTS_CTL_FSMIE;
379 writel(val, ispi->sregs + SSFSTS_CTL);
382 /* Check controller's lock status */
383 val = readl(ispi->base + HSFSTS_CTL);
384 ispi->locked = !!(val & HSFSTS_CTL_FLOCKDN);
388 * BIOS programs allowed opcodes and then locks down the
389 * register. So read back what opcodes it decided to support.
390 * That's the set we are going to support as well.
392 opmenu0 = readl(ispi->sregs + OPMENU0);
393 opmenu1 = readl(ispi->sregs + OPMENU1);
395 if (opmenu0 && opmenu1) {
396 for (i = 0; i < ARRAY_SIZE(ispi->opcodes) / 2; i++) {
397 ispi->opcodes[i] = opmenu0 >> i * 8;
398 ispi->opcodes[i + 4] = opmenu1 >> i * 8;
403 intel_spi_dump_regs(ispi);
408 static int intel_spi_opcode_index(struct intel_spi *ispi, u8 opcode, int optype)
414 for (i = 0; i < ARRAY_SIZE(ispi->opcodes); i++)
415 if (ispi->opcodes[i] == opcode)
421 /* The lock is off, so just use index 0 */
422 writel(opcode, ispi->sregs + OPMENU0);
423 preop = readw(ispi->sregs + PREOP_OPTYPE);
424 writel(optype << 16 | preop, ispi->sregs + PREOP_OPTYPE);
429 static int intel_spi_hw_cycle(struct intel_spi *ispi, u8 opcode, int len)
434 val = readl(ispi->base + HSFSTS_CTL);
435 val &= ~(HSFSTS_CTL_FCYCLE_MASK | HSFSTS_CTL_FDBC_MASK);
439 val |= HSFSTS_CTL_FCYCLE_RDID;
442 val |= HSFSTS_CTL_FCYCLE_WRSR;
445 val |= HSFSTS_CTL_FCYCLE_RDSR;
451 if (len > INTEL_SPI_FIFO_SZ)
454 val |= (len - 1) << HSFSTS_CTL_FDBC_SHIFT;
455 val |= HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
456 val |= HSFSTS_CTL_FGO;
457 writel(val, ispi->base + HSFSTS_CTL);
459 ret = intel_spi_wait_hw_busy(ispi);
463 status = readl(ispi->base + HSFSTS_CTL);
464 if (status & HSFSTS_CTL_FCERR)
466 else if (status & HSFSTS_CTL_AEL)
472 static int intel_spi_sw_cycle(struct intel_spi *ispi, u8 opcode, int len,
479 ret = intel_spi_opcode_index(ispi, opcode, optype);
483 if (len > INTEL_SPI_FIFO_SZ)
487 * Always clear it after each SW sequencer operation regardless
488 * of whether it is successful or not.
490 atomic_preopcode = ispi->atomic_preopcode;
491 ispi->atomic_preopcode = 0;
493 /* Only mark 'Data Cycle' bit when there is data to be transferred */
495 val = ((len - 1) << SSFSTS_CTL_DBC_SHIFT) | SSFSTS_CTL_DS;
496 val |= ret << SSFSTS_CTL_COP_SHIFT;
497 val |= SSFSTS_CTL_FCERR | SSFSTS_CTL_FDONE;
498 val |= SSFSTS_CTL_SCGO;
499 if (atomic_preopcode) {
503 case OPTYPE_WRITE_NO_ADDR:
504 case OPTYPE_WRITE_WITH_ADDR:
505 /* Pick matching preopcode for the atomic sequence */
506 preop = readw(ispi->sregs + PREOP_OPTYPE);
507 if ((preop & 0xff) == atomic_preopcode)
509 else if ((preop >> 8) == atomic_preopcode)
510 val |= SSFSTS_CTL_SPOP;
514 /* Enable atomic sequence */
515 val |= SSFSTS_CTL_ACS;
523 writel(val, ispi->sregs + SSFSTS_CTL);
525 ret = intel_spi_wait_sw_busy(ispi);
529 status = readl(ispi->sregs + SSFSTS_CTL);
530 if (status & SSFSTS_CTL_FCERR)
532 else if (status & SSFSTS_CTL_AEL)
538 static int intel_spi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
540 struct intel_spi *ispi = nor->priv;
543 /* Address of the first chip */
544 writel(0, ispi->base + FADDR);
547 ret = intel_spi_sw_cycle(ispi, opcode, len,
548 OPTYPE_READ_NO_ADDR);
550 ret = intel_spi_hw_cycle(ispi, opcode, len);
555 return intel_spi_read_block(ispi, buf, len);
558 static int intel_spi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
560 struct intel_spi *ispi = nor->priv;
564 * This is handled with atomic operation and preop code in Intel
565 * controller so we only verify that it is available. If the
566 * controller is not locked, program the opcode to the PREOP
567 * register for later use.
569 * When hardware sequencer is used there is no need to program
570 * any opcodes (it handles them automatically as part of a command).
572 if (opcode == SPINOR_OP_WREN) {
575 if (!ispi->swseq_reg)
578 preop = readw(ispi->sregs + PREOP_OPTYPE);
579 if ((preop & 0xff) != opcode && (preop >> 8) != opcode) {
582 writel(opcode, ispi->sregs + PREOP_OPTYPE);
586 * This enables atomic sequence on next SW sycle. Will
587 * be cleared after next operation.
589 ispi->atomic_preopcode = opcode;
593 writel(0, ispi->base + FADDR);
595 /* Write the value beforehand */
596 ret = intel_spi_write_block(ispi, buf, len);
601 return intel_spi_sw_cycle(ispi, opcode, len,
602 OPTYPE_WRITE_NO_ADDR);
603 return intel_spi_hw_cycle(ispi, opcode, len);
606 static ssize_t intel_spi_read(struct spi_nor *nor, loff_t from, size_t len,
609 struct intel_spi *ispi = nor->priv;
610 size_t block_size, retlen = 0;
615 * Atomic sequence is not expected with HW sequencer reads. Make
616 * sure it is cleared regardless.
618 if (WARN_ON_ONCE(ispi->atomic_preopcode))
619 ispi->atomic_preopcode = 0;
621 switch (nor->read_opcode) {
623 case SPINOR_OP_READ_FAST:
624 case SPINOR_OP_READ_4B:
625 case SPINOR_OP_READ_FAST_4B:
632 block_size = min_t(size_t, len, INTEL_SPI_FIFO_SZ);
634 /* Read cannot cross 4K boundary */
635 block_size = min_t(loff_t, from + block_size,
636 round_up(from + 1, SZ_4K)) - from;
638 writel(from, ispi->base + FADDR);
640 val = readl(ispi->base + HSFSTS_CTL);
641 val &= ~(HSFSTS_CTL_FDBC_MASK | HSFSTS_CTL_FCYCLE_MASK);
642 val |= HSFSTS_CTL_AEL | HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
643 val |= (block_size - 1) << HSFSTS_CTL_FDBC_SHIFT;
644 val |= HSFSTS_CTL_FCYCLE_READ;
645 val |= HSFSTS_CTL_FGO;
646 writel(val, ispi->base + HSFSTS_CTL);
648 ret = intel_spi_wait_hw_busy(ispi);
652 status = readl(ispi->base + HSFSTS_CTL);
653 if (status & HSFSTS_CTL_FCERR)
655 else if (status & HSFSTS_CTL_AEL)
659 dev_err(ispi->dev, "read error: %llx: %#x\n", from,
664 ret = intel_spi_read_block(ispi, read_buf, block_size);
670 retlen += block_size;
671 read_buf += block_size;
677 static ssize_t intel_spi_write(struct spi_nor *nor, loff_t to, size_t len,
678 const u_char *write_buf)
680 struct intel_spi *ispi = nor->priv;
681 size_t block_size, retlen = 0;
685 /* Not needed with HW sequencer write, make sure it is cleared */
686 ispi->atomic_preopcode = 0;
689 block_size = min_t(size_t, len, INTEL_SPI_FIFO_SZ);
691 /* Write cannot cross 4K boundary */
692 block_size = min_t(loff_t, to + block_size,
693 round_up(to + 1, SZ_4K)) - to;
695 writel(to, ispi->base + FADDR);
697 val = readl(ispi->base + HSFSTS_CTL);
698 val &= ~(HSFSTS_CTL_FDBC_MASK | HSFSTS_CTL_FCYCLE_MASK);
699 val |= HSFSTS_CTL_AEL | HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
700 val |= (block_size - 1) << HSFSTS_CTL_FDBC_SHIFT;
701 val |= HSFSTS_CTL_FCYCLE_WRITE;
703 ret = intel_spi_write_block(ispi, write_buf, block_size);
705 dev_err(ispi->dev, "failed to write block\n");
709 /* Start the write now */
710 val |= HSFSTS_CTL_FGO;
711 writel(val, ispi->base + HSFSTS_CTL);
713 ret = intel_spi_wait_hw_busy(ispi);
715 dev_err(ispi->dev, "timeout\n");
719 status = readl(ispi->base + HSFSTS_CTL);
720 if (status & HSFSTS_CTL_FCERR)
722 else if (status & HSFSTS_CTL_AEL)
726 dev_err(ispi->dev, "write error: %llx: %#x\n", to,
733 retlen += block_size;
734 write_buf += block_size;
740 static int intel_spi_erase(struct spi_nor *nor, loff_t offs)
742 size_t erase_size, len = nor->mtd.erasesize;
743 struct intel_spi *ispi = nor->priv;
744 u32 val, status, cmd;
747 /* If the hardware can do 64k erase use that when possible */
748 if (len >= SZ_64K && ispi->erase_64k) {
749 cmd = HSFSTS_CTL_FCYCLE_ERASE_64K;
752 cmd = HSFSTS_CTL_FCYCLE_ERASE;
756 if (ispi->swseq_erase) {
758 writel(offs, ispi->base + FADDR);
760 ret = intel_spi_sw_cycle(ispi, nor->erase_opcode,
761 0, OPTYPE_WRITE_WITH_ADDR);
772 /* Not needed with HW sequencer erase, make sure it is cleared */
773 ispi->atomic_preopcode = 0;
776 writel(offs, ispi->base + FADDR);
778 val = readl(ispi->base + HSFSTS_CTL);
779 val &= ~(HSFSTS_CTL_FDBC_MASK | HSFSTS_CTL_FCYCLE_MASK);
780 val |= HSFSTS_CTL_AEL | HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
782 val |= HSFSTS_CTL_FGO;
783 writel(val, ispi->base + HSFSTS_CTL);
785 ret = intel_spi_wait_hw_busy(ispi);
789 status = readl(ispi->base + HSFSTS_CTL);
790 if (status & HSFSTS_CTL_FCERR)
792 else if (status & HSFSTS_CTL_AEL)
802 static bool intel_spi_is_protected(const struct intel_spi *ispi,
803 unsigned int base, unsigned int limit)
807 for (i = 0; i < ispi->pr_num; i++) {
808 u32 pr_base, pr_limit, pr_value;
810 pr_value = readl(ispi->pregs + PR(i));
811 if (!(pr_value & (PR_WPE | PR_RPE)))
814 pr_limit = (pr_value & PR_LIMIT_MASK) >> PR_LIMIT_SHIFT;
815 pr_base = pr_value & PR_BASE_MASK;
817 if (pr_base >= base && pr_limit <= limit)
825 * There will be a single partition holding all enabled flash regions. We
828 static void intel_spi_fill_partition(struct intel_spi *ispi,
829 struct mtd_partition *part)
834 memset(part, 0, sizeof(*part));
836 /* Start from the mandatory descriptor region */
841 * Now try to find where this partition ends based on the flash
844 for (i = 1; i < ispi->nregions; i++) {
845 u32 region, base, limit;
847 region = readl(ispi->base + FREG(i));
848 base = region & FREG_BASE_MASK;
849 limit = (region & FREG_LIMIT_MASK) >> FREG_LIMIT_SHIFT;
851 if (base >= limit || limit == 0)
855 * If any of the regions have protection bits set, make the
856 * whole partition read-only to be on the safe side.
858 if (intel_spi_is_protected(ispi, base, limit))
859 ispi->writeable = false;
861 end = (limit << 12) + 4096;
862 if (end > part->size)
867 struct intel_spi *intel_spi_probe(struct device *dev,
868 struct resource *mem, const struct intel_spi_boardinfo *info)
870 const struct spi_nor_hwcaps hwcaps = {
871 .mask = SNOR_HWCAPS_READ |
872 SNOR_HWCAPS_READ_FAST |
875 struct mtd_partition part;
876 struct intel_spi *ispi;
880 return ERR_PTR(-EINVAL);
882 ispi = devm_kzalloc(dev, sizeof(*ispi), GFP_KERNEL);
884 return ERR_PTR(-ENOMEM);
886 ispi->base = devm_ioremap_resource(dev, mem);
887 if (IS_ERR(ispi->base))
888 return ERR_CAST(ispi->base);
892 ispi->writeable = info->writeable;
894 ret = intel_spi_init(ispi);
898 ispi->nor.dev = ispi->dev;
899 ispi->nor.priv = ispi;
900 ispi->nor.read_reg = intel_spi_read_reg;
901 ispi->nor.write_reg = intel_spi_write_reg;
902 ispi->nor.read = intel_spi_read;
903 ispi->nor.write = intel_spi_write;
904 ispi->nor.erase = intel_spi_erase;
906 ret = spi_nor_scan(&ispi->nor, NULL, &hwcaps);
908 dev_info(dev, "failed to locate the chip\n");
912 intel_spi_fill_partition(ispi, &part);
914 /* Prevent writes if not explicitly enabled */
915 if (!ispi->writeable || !writeable)
916 ispi->nor.mtd.flags &= ~MTD_WRITEABLE;
918 ret = mtd_device_register(&ispi->nor.mtd, &part, 1);
924 EXPORT_SYMBOL_GPL(intel_spi_probe);
926 int intel_spi_remove(struct intel_spi *ispi)
928 return mtd_device_unregister(&ispi->nor.mtd);
930 EXPORT_SYMBOL_GPL(intel_spi_remove);
932 MODULE_DESCRIPTION("Intel PCH/PCU SPI flash core driver");
934 MODULE_LICENSE("GPL v2");