1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel PCH/PCU SPI flash driver.
5 * Copyright (C) 2016, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
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
112 #define CNL_FREG_NUM 6
117 #define ERASE_OPCODE_SHIFT 8
118 #define ERASE_OPCODE_MASK (0xff << ERASE_OPCODE_SHIFT)
119 #define ERASE_64K_OPCODE_SHIFT 16
120 #define ERASE_64K_OPCODE_MASK (0xff << ERASE_OPCODE_SHIFT)
122 #define INTEL_SPI_TIMEOUT 5000 /* ms */
123 #define INTEL_SPI_FIFO_SZ 64
126 * struct intel_spi - Driver private data
127 * @dev: Device pointer
128 * @info: Pointer to board specific info
129 * @nor: SPI NOR layer structure
130 * @base: Beginning of MMIO space
131 * @pregs: Start of protection registers
132 * @sregs: Start of software sequencer registers
133 * @nregions: Maximum number of regions
134 * @pr_num: Maximum number of protected range registers
135 * @writeable: Is the chip writeable
136 * @locked: Is SPI setting locked
137 * @swseq_reg: Use SW sequencer in register reads/writes
138 * @swseq_erase: Use SW sequencer in erase operation
139 * @erase_64k: 64k erase supported
140 * @atomic_preopcode: Holds preopcode when atomic sequence is requested
141 * @opcodes: Opcodes which are supported. This are programmed by BIOS
142 * before it locks down the controller.
146 const struct intel_spi_boardinfo
*info
;
162 static bool writeable
;
163 module_param(writeable
, bool, 0);
164 MODULE_PARM_DESC(writeable
, "Enable write access to SPI flash chip (default=0)");
166 static void intel_spi_dump_regs(struct intel_spi
*ispi
)
171 dev_dbg(ispi
->dev
, "BFPREG=0x%08x\n", readl(ispi
->base
+ BFPREG
));
173 value
= readl(ispi
->base
+ HSFSTS_CTL
);
174 dev_dbg(ispi
->dev
, "HSFSTS_CTL=0x%08x\n", value
);
175 if (value
& HSFSTS_CTL_FLOCKDN
)
176 dev_dbg(ispi
->dev
, "-> Locked\n");
178 dev_dbg(ispi
->dev
, "FADDR=0x%08x\n", readl(ispi
->base
+ FADDR
));
179 dev_dbg(ispi
->dev
, "DLOCK=0x%08x\n", readl(ispi
->base
+ DLOCK
));
181 for (i
= 0; i
< 16; i
++)
182 dev_dbg(ispi
->dev
, "FDATA(%d)=0x%08x\n",
183 i
, readl(ispi
->base
+ FDATA(i
)));
185 dev_dbg(ispi
->dev
, "FRACC=0x%08x\n", readl(ispi
->base
+ FRACC
));
187 for (i
= 0; i
< ispi
->nregions
; i
++)
188 dev_dbg(ispi
->dev
, "FREG(%d)=0x%08x\n", i
,
189 readl(ispi
->base
+ FREG(i
)));
190 for (i
= 0; i
< ispi
->pr_num
; i
++)
191 dev_dbg(ispi
->dev
, "PR(%d)=0x%08x\n", i
,
192 readl(ispi
->pregs
+ PR(i
)));
195 value
= readl(ispi
->sregs
+ SSFSTS_CTL
);
196 dev_dbg(ispi
->dev
, "SSFSTS_CTL=0x%08x\n", value
);
197 dev_dbg(ispi
->dev
, "PREOP_OPTYPE=0x%08x\n",
198 readl(ispi
->sregs
+ PREOP_OPTYPE
));
199 dev_dbg(ispi
->dev
, "OPMENU0=0x%08x\n",
200 readl(ispi
->sregs
+ OPMENU0
));
201 dev_dbg(ispi
->dev
, "OPMENU1=0x%08x\n",
202 readl(ispi
->sregs
+ OPMENU1
));
205 if (ispi
->info
->type
== INTEL_SPI_BYT
)
206 dev_dbg(ispi
->dev
, "BCR=0x%08x\n", readl(ispi
->base
+ BYT_BCR
));
208 dev_dbg(ispi
->dev
, "LVSCC=0x%08x\n", readl(ispi
->base
+ LVSCC
));
209 dev_dbg(ispi
->dev
, "UVSCC=0x%08x\n", readl(ispi
->base
+ UVSCC
));
211 dev_dbg(ispi
->dev
, "Protected regions:\n");
212 for (i
= 0; i
< ispi
->pr_num
; i
++) {
215 value
= readl(ispi
->pregs
+ PR(i
));
216 if (!(value
& (PR_WPE
| PR_RPE
)))
219 limit
= (value
& PR_LIMIT_MASK
) >> PR_LIMIT_SHIFT
;
220 base
= value
& PR_BASE_MASK
;
222 dev_dbg(ispi
->dev
, " %02d base: 0x%08x limit: 0x%08x [%c%c]\n",
223 i
, base
<< 12, (limit
<< 12) | 0xfff,
224 value
& PR_WPE
? 'W' : '.',
225 value
& PR_RPE
? 'R' : '.');
228 dev_dbg(ispi
->dev
, "Flash regions:\n");
229 for (i
= 0; i
< ispi
->nregions
; i
++) {
230 u32 region
, base
, limit
;
232 region
= readl(ispi
->base
+ FREG(i
));
233 base
= region
& FREG_BASE_MASK
;
234 limit
= (region
& FREG_LIMIT_MASK
) >> FREG_LIMIT_SHIFT
;
236 if (base
>= limit
|| (i
> 0 && limit
== 0))
237 dev_dbg(ispi
->dev
, " %02d disabled\n", i
);
239 dev_dbg(ispi
->dev
, " %02d base: 0x%08x limit: 0x%08x\n",
240 i
, base
<< 12, (limit
<< 12) | 0xfff);
243 dev_dbg(ispi
->dev
, "Using %cW sequencer for register access\n",
244 ispi
->swseq_reg
? 'S' : 'H');
245 dev_dbg(ispi
->dev
, "Using %cW sequencer for erase operation\n",
246 ispi
->swseq_erase
? 'S' : 'H');
249 /* Reads max INTEL_SPI_FIFO_SZ bytes from the device fifo */
250 static int intel_spi_read_block(struct intel_spi
*ispi
, void *buf
, size_t size
)
255 if (size
> INTEL_SPI_FIFO_SZ
)
259 bytes
= min_t(size_t, size
, 4);
260 memcpy_fromio(buf
, ispi
->base
+ FDATA(i
), bytes
);
269 /* Writes max INTEL_SPI_FIFO_SZ bytes to the device fifo */
270 static int intel_spi_write_block(struct intel_spi
*ispi
, const void *buf
,
276 if (size
> INTEL_SPI_FIFO_SZ
)
280 bytes
= min_t(size_t, size
, 4);
281 memcpy_toio(ispi
->base
+ FDATA(i
), buf
, bytes
);
290 static int intel_spi_wait_hw_busy(struct intel_spi
*ispi
)
294 return readl_poll_timeout(ispi
->base
+ HSFSTS_CTL
, val
,
295 !(val
& HSFSTS_CTL_SCIP
), 40,
296 INTEL_SPI_TIMEOUT
* 1000);
299 static int intel_spi_wait_sw_busy(struct intel_spi
*ispi
)
303 return readl_poll_timeout(ispi
->sregs
+ SSFSTS_CTL
, val
,
304 !(val
& SSFSTS_CTL_SCIP
), 40,
305 INTEL_SPI_TIMEOUT
* 1000);
308 static int intel_spi_init(struct intel_spi
*ispi
)
310 u32 opmenu0
, opmenu1
, lvscc
, uvscc
, val
;
313 switch (ispi
->info
->type
) {
315 ispi
->sregs
= ispi
->base
+ BYT_SSFSTS_CTL
;
316 ispi
->pregs
= ispi
->base
+ BYT_PR
;
317 ispi
->nregions
= BYT_FREG_NUM
;
318 ispi
->pr_num
= BYT_PR_NUM
;
319 ispi
->swseq_reg
= true;
322 /* Disable write protection */
323 val
= readl(ispi
->base
+ BYT_BCR
);
324 if (!(val
& BYT_BCR_WPD
)) {
326 writel(val
, ispi
->base
+ BYT_BCR
);
327 val
= readl(ispi
->base
+ BYT_BCR
);
330 ispi
->writeable
= !!(val
& BYT_BCR_WPD
);
336 ispi
->sregs
= ispi
->base
+ LPT_SSFSTS_CTL
;
337 ispi
->pregs
= ispi
->base
+ LPT_PR
;
338 ispi
->nregions
= LPT_FREG_NUM
;
339 ispi
->pr_num
= LPT_PR_NUM
;
340 ispi
->swseq_reg
= true;
344 ispi
->sregs
= ispi
->base
+ BXT_SSFSTS_CTL
;
345 ispi
->pregs
= ispi
->base
+ BXT_PR
;
346 ispi
->nregions
= BXT_FREG_NUM
;
347 ispi
->pr_num
= BXT_PR_NUM
;
348 ispi
->erase_64k
= true;
353 ispi
->pregs
= ispi
->base
+ CNL_PR
;
354 ispi
->nregions
= CNL_FREG_NUM
;
355 ispi
->pr_num
= CNL_PR_NUM
;
362 /* Disable #SMI generation from HW sequencer */
363 val
= readl(ispi
->base
+ HSFSTS_CTL
);
364 val
&= ~HSFSTS_CTL_FSMIE
;
365 writel(val
, ispi
->base
+ HSFSTS_CTL
);
368 * Determine whether erase operation should use HW or SW sequencer.
370 * The HW sequencer has a predefined list of opcodes, with only the
371 * erase opcode being programmable in LVSCC and UVSCC registers.
372 * If these registers don't contain a valid erase opcode, erase
373 * cannot be done using HW sequencer.
375 lvscc
= readl(ispi
->base
+ LVSCC
);
376 uvscc
= readl(ispi
->base
+ UVSCC
);
377 if (!(lvscc
& ERASE_OPCODE_MASK
) || !(uvscc
& ERASE_OPCODE_MASK
))
378 ispi
->swseq_erase
= true;
379 /* SPI controller on Intel BXT supports 64K erase opcode */
380 if (ispi
->info
->type
== INTEL_SPI_BXT
&& !ispi
->swseq_erase
)
381 if (!(lvscc
& ERASE_64K_OPCODE_MASK
) ||
382 !(uvscc
& ERASE_64K_OPCODE_MASK
))
383 ispi
->erase_64k
= false;
385 if (ispi
->sregs
== NULL
&& (ispi
->swseq_reg
|| ispi
->swseq_erase
)) {
386 dev_err(ispi
->dev
, "software sequencer not supported, but required\n");
391 * Some controllers can only do basic operations using hardware
392 * sequencer. All other operations are supposed to be carried out
393 * using software sequencer.
395 if (ispi
->swseq_reg
) {
396 /* Disable #SMI generation from SW sequencer */
397 val
= readl(ispi
->sregs
+ SSFSTS_CTL
);
398 val
&= ~SSFSTS_CTL_FSMIE
;
399 writel(val
, ispi
->sregs
+ SSFSTS_CTL
);
402 /* Check controller's lock status */
403 val
= readl(ispi
->base
+ HSFSTS_CTL
);
404 ispi
->locked
= !!(val
& HSFSTS_CTL_FLOCKDN
);
406 if (ispi
->locked
&& ispi
->sregs
) {
408 * BIOS programs allowed opcodes and then locks down the
409 * register. So read back what opcodes it decided to support.
410 * That's the set we are going to support as well.
412 opmenu0
= readl(ispi
->sregs
+ OPMENU0
);
413 opmenu1
= readl(ispi
->sregs
+ OPMENU1
);
415 if (opmenu0
&& opmenu1
) {
416 for (i
= 0; i
< ARRAY_SIZE(ispi
->opcodes
) / 2; i
++) {
417 ispi
->opcodes
[i
] = opmenu0
>> i
* 8;
418 ispi
->opcodes
[i
+ 4] = opmenu1
>> i
* 8;
423 intel_spi_dump_regs(ispi
);
428 static int intel_spi_opcode_index(struct intel_spi
*ispi
, u8 opcode
, int optype
)
434 for (i
= 0; i
< ARRAY_SIZE(ispi
->opcodes
); i
++)
435 if (ispi
->opcodes
[i
] == opcode
)
441 /* The lock is off, so just use index 0 */
442 writel(opcode
, ispi
->sregs
+ OPMENU0
);
443 preop
= readw(ispi
->sregs
+ PREOP_OPTYPE
);
444 writel(optype
<< 16 | preop
, ispi
->sregs
+ PREOP_OPTYPE
);
449 static int intel_spi_hw_cycle(struct intel_spi
*ispi
, u8 opcode
, size_t len
)
454 val
= readl(ispi
->base
+ HSFSTS_CTL
);
455 val
&= ~(HSFSTS_CTL_FCYCLE_MASK
| HSFSTS_CTL_FDBC_MASK
);
459 val
|= HSFSTS_CTL_FCYCLE_RDID
;
462 val
|= HSFSTS_CTL_FCYCLE_WRSR
;
465 val
|= HSFSTS_CTL_FCYCLE_RDSR
;
471 if (len
> INTEL_SPI_FIFO_SZ
)
474 val
|= (len
- 1) << HSFSTS_CTL_FDBC_SHIFT
;
475 val
|= HSFSTS_CTL_FCERR
| HSFSTS_CTL_FDONE
;
476 val
|= HSFSTS_CTL_FGO
;
477 writel(val
, ispi
->base
+ HSFSTS_CTL
);
479 ret
= intel_spi_wait_hw_busy(ispi
);
483 status
= readl(ispi
->base
+ HSFSTS_CTL
);
484 if (status
& HSFSTS_CTL_FCERR
)
486 else if (status
& HSFSTS_CTL_AEL
)
492 static int intel_spi_sw_cycle(struct intel_spi
*ispi
, u8 opcode
, size_t len
,
499 ret
= intel_spi_opcode_index(ispi
, opcode
, optype
);
503 if (len
> INTEL_SPI_FIFO_SZ
)
507 * Always clear it after each SW sequencer operation regardless
508 * of whether it is successful or not.
510 atomic_preopcode
= ispi
->atomic_preopcode
;
511 ispi
->atomic_preopcode
= 0;
513 /* Only mark 'Data Cycle' bit when there is data to be transferred */
515 val
= ((len
- 1) << SSFSTS_CTL_DBC_SHIFT
) | SSFSTS_CTL_DS
;
516 val
|= ret
<< SSFSTS_CTL_COP_SHIFT
;
517 val
|= SSFSTS_CTL_FCERR
| SSFSTS_CTL_FDONE
;
518 val
|= SSFSTS_CTL_SCGO
;
519 if (atomic_preopcode
) {
523 case OPTYPE_WRITE_NO_ADDR
:
524 case OPTYPE_WRITE_WITH_ADDR
:
525 /* Pick matching preopcode for the atomic sequence */
526 preop
= readw(ispi
->sregs
+ PREOP_OPTYPE
);
527 if ((preop
& 0xff) == atomic_preopcode
)
529 else if ((preop
>> 8) == atomic_preopcode
)
530 val
|= SSFSTS_CTL_SPOP
;
534 /* Enable atomic sequence */
535 val
|= SSFSTS_CTL_ACS
;
543 writel(val
, ispi
->sregs
+ SSFSTS_CTL
);
545 ret
= intel_spi_wait_sw_busy(ispi
);
549 status
= readl(ispi
->sregs
+ SSFSTS_CTL
);
550 if (status
& SSFSTS_CTL_FCERR
)
552 else if (status
& SSFSTS_CTL_AEL
)
558 static int intel_spi_read_reg(struct spi_nor
*nor
, u8 opcode
, u8
*buf
,
561 struct intel_spi
*ispi
= nor
->priv
;
564 /* Address of the first chip */
565 writel(0, ispi
->base
+ FADDR
);
568 ret
= intel_spi_sw_cycle(ispi
, opcode
, len
,
569 OPTYPE_READ_NO_ADDR
);
571 ret
= intel_spi_hw_cycle(ispi
, opcode
, len
);
576 return intel_spi_read_block(ispi
, buf
, len
);
579 static int intel_spi_write_reg(struct spi_nor
*nor
, u8 opcode
, const u8
*buf
,
582 struct intel_spi
*ispi
= nor
->priv
;
586 * This is handled with atomic operation and preop code in Intel
587 * controller so we only verify that it is available. If the
588 * controller is not locked, program the opcode to the PREOP
589 * register for later use.
591 * When hardware sequencer is used there is no need to program
592 * any opcodes (it handles them automatically as part of a command).
594 if (opcode
== SPINOR_OP_WREN
) {
597 if (!ispi
->swseq_reg
)
600 preop
= readw(ispi
->sregs
+ PREOP_OPTYPE
);
601 if ((preop
& 0xff) != opcode
&& (preop
>> 8) != opcode
) {
604 writel(opcode
, ispi
->sregs
+ PREOP_OPTYPE
);
608 * This enables atomic sequence on next SW sycle. Will
609 * be cleared after next operation.
611 ispi
->atomic_preopcode
= opcode
;
615 writel(0, ispi
->base
+ FADDR
);
617 /* Write the value beforehand */
618 ret
= intel_spi_write_block(ispi
, buf
, len
);
623 return intel_spi_sw_cycle(ispi
, opcode
, len
,
624 OPTYPE_WRITE_NO_ADDR
);
625 return intel_spi_hw_cycle(ispi
, opcode
, len
);
628 static ssize_t
intel_spi_read(struct spi_nor
*nor
, loff_t from
, size_t len
,
631 struct intel_spi
*ispi
= nor
->priv
;
632 size_t block_size
, retlen
= 0;
637 * Atomic sequence is not expected with HW sequencer reads. Make
638 * sure it is cleared regardless.
640 if (WARN_ON_ONCE(ispi
->atomic_preopcode
))
641 ispi
->atomic_preopcode
= 0;
643 switch (nor
->read_opcode
) {
645 case SPINOR_OP_READ_FAST
:
646 case SPINOR_OP_READ_4B
:
647 case SPINOR_OP_READ_FAST_4B
:
654 block_size
= min_t(size_t, len
, INTEL_SPI_FIFO_SZ
);
656 /* Read cannot cross 4K boundary */
657 block_size
= min_t(loff_t
, from
+ block_size
,
658 round_up(from
+ 1, SZ_4K
)) - from
;
660 writel(from
, ispi
->base
+ FADDR
);
662 val
= readl(ispi
->base
+ HSFSTS_CTL
);
663 val
&= ~(HSFSTS_CTL_FDBC_MASK
| HSFSTS_CTL_FCYCLE_MASK
);
664 val
|= HSFSTS_CTL_AEL
| HSFSTS_CTL_FCERR
| HSFSTS_CTL_FDONE
;
665 val
|= (block_size
- 1) << HSFSTS_CTL_FDBC_SHIFT
;
666 val
|= HSFSTS_CTL_FCYCLE_READ
;
667 val
|= HSFSTS_CTL_FGO
;
668 writel(val
, ispi
->base
+ HSFSTS_CTL
);
670 ret
= intel_spi_wait_hw_busy(ispi
);
674 status
= readl(ispi
->base
+ HSFSTS_CTL
);
675 if (status
& HSFSTS_CTL_FCERR
)
677 else if (status
& HSFSTS_CTL_AEL
)
681 dev_err(ispi
->dev
, "read error: %llx: %#x\n", from
,
686 ret
= intel_spi_read_block(ispi
, read_buf
, block_size
);
692 retlen
+= block_size
;
693 read_buf
+= block_size
;
699 static ssize_t
intel_spi_write(struct spi_nor
*nor
, loff_t to
, size_t len
,
700 const u_char
*write_buf
)
702 struct intel_spi
*ispi
= nor
->priv
;
703 size_t block_size
, retlen
= 0;
707 /* Not needed with HW sequencer write, make sure it is cleared */
708 ispi
->atomic_preopcode
= 0;
711 block_size
= min_t(size_t, len
, INTEL_SPI_FIFO_SZ
);
713 /* Write cannot cross 4K boundary */
714 block_size
= min_t(loff_t
, to
+ block_size
,
715 round_up(to
+ 1, SZ_4K
)) - to
;
717 writel(to
, ispi
->base
+ FADDR
);
719 val
= readl(ispi
->base
+ HSFSTS_CTL
);
720 val
&= ~(HSFSTS_CTL_FDBC_MASK
| HSFSTS_CTL_FCYCLE_MASK
);
721 val
|= HSFSTS_CTL_AEL
| HSFSTS_CTL_FCERR
| HSFSTS_CTL_FDONE
;
722 val
|= (block_size
- 1) << HSFSTS_CTL_FDBC_SHIFT
;
723 val
|= HSFSTS_CTL_FCYCLE_WRITE
;
725 ret
= intel_spi_write_block(ispi
, write_buf
, block_size
);
727 dev_err(ispi
->dev
, "failed to write block\n");
731 /* Start the write now */
732 val
|= HSFSTS_CTL_FGO
;
733 writel(val
, ispi
->base
+ HSFSTS_CTL
);
735 ret
= intel_spi_wait_hw_busy(ispi
);
737 dev_err(ispi
->dev
, "timeout\n");
741 status
= readl(ispi
->base
+ HSFSTS_CTL
);
742 if (status
& HSFSTS_CTL_FCERR
)
744 else if (status
& HSFSTS_CTL_AEL
)
748 dev_err(ispi
->dev
, "write error: %llx: %#x\n", to
,
755 retlen
+= block_size
;
756 write_buf
+= block_size
;
762 static int intel_spi_erase(struct spi_nor
*nor
, loff_t offs
)
764 size_t erase_size
, len
= nor
->mtd
.erasesize
;
765 struct intel_spi
*ispi
= nor
->priv
;
766 u32 val
, status
, cmd
;
769 /* If the hardware can do 64k erase use that when possible */
770 if (len
>= SZ_64K
&& ispi
->erase_64k
) {
771 cmd
= HSFSTS_CTL_FCYCLE_ERASE_64K
;
774 cmd
= HSFSTS_CTL_FCYCLE_ERASE
;
778 if (ispi
->swseq_erase
) {
780 writel(offs
, ispi
->base
+ FADDR
);
782 ret
= intel_spi_sw_cycle(ispi
, nor
->erase_opcode
,
783 0, OPTYPE_WRITE_WITH_ADDR
);
794 /* Not needed with HW sequencer erase, make sure it is cleared */
795 ispi
->atomic_preopcode
= 0;
798 writel(offs
, ispi
->base
+ FADDR
);
800 val
= readl(ispi
->base
+ HSFSTS_CTL
);
801 val
&= ~(HSFSTS_CTL_FDBC_MASK
| HSFSTS_CTL_FCYCLE_MASK
);
802 val
|= HSFSTS_CTL_AEL
| HSFSTS_CTL_FCERR
| HSFSTS_CTL_FDONE
;
804 val
|= HSFSTS_CTL_FGO
;
805 writel(val
, ispi
->base
+ HSFSTS_CTL
);
807 ret
= intel_spi_wait_hw_busy(ispi
);
811 status
= readl(ispi
->base
+ HSFSTS_CTL
);
812 if (status
& HSFSTS_CTL_FCERR
)
814 else if (status
& HSFSTS_CTL_AEL
)
824 static bool intel_spi_is_protected(const struct intel_spi
*ispi
,
825 unsigned int base
, unsigned int limit
)
829 for (i
= 0; i
< ispi
->pr_num
; i
++) {
830 u32 pr_base
, pr_limit
, pr_value
;
832 pr_value
= readl(ispi
->pregs
+ PR(i
));
833 if (!(pr_value
& (PR_WPE
| PR_RPE
)))
836 pr_limit
= (pr_value
& PR_LIMIT_MASK
) >> PR_LIMIT_SHIFT
;
837 pr_base
= pr_value
& PR_BASE_MASK
;
839 if (pr_base
>= base
&& pr_limit
<= limit
)
847 * There will be a single partition holding all enabled flash regions. We
850 static void intel_spi_fill_partition(struct intel_spi
*ispi
,
851 struct mtd_partition
*part
)
856 memset(part
, 0, sizeof(*part
));
858 /* Start from the mandatory descriptor region */
863 * Now try to find where this partition ends based on the flash
866 for (i
= 1; i
< ispi
->nregions
; i
++) {
867 u32 region
, base
, limit
;
869 region
= readl(ispi
->base
+ FREG(i
));
870 base
= region
& FREG_BASE_MASK
;
871 limit
= (region
& FREG_LIMIT_MASK
) >> FREG_LIMIT_SHIFT
;
873 if (base
>= limit
|| limit
== 0)
877 * If any of the regions have protection bits set, make the
878 * whole partition read-only to be on the safe side.
880 if (intel_spi_is_protected(ispi
, base
, limit
))
881 ispi
->writeable
= false;
883 end
= (limit
<< 12) + 4096;
884 if (end
> part
->size
)
889 static const struct spi_nor_controller_ops intel_spi_controller_ops
= {
890 .read_reg
= intel_spi_read_reg
,
891 .write_reg
= intel_spi_write_reg
,
892 .read
= intel_spi_read
,
893 .write
= intel_spi_write
,
894 .erase
= intel_spi_erase
,
897 struct intel_spi
*intel_spi_probe(struct device
*dev
,
898 struct resource
*mem
, const struct intel_spi_boardinfo
*info
)
900 const struct spi_nor_hwcaps hwcaps
= {
901 .mask
= SNOR_HWCAPS_READ
|
902 SNOR_HWCAPS_READ_FAST
|
905 struct mtd_partition part
;
906 struct intel_spi
*ispi
;
910 return ERR_PTR(-EINVAL
);
912 ispi
= devm_kzalloc(dev
, sizeof(*ispi
), GFP_KERNEL
);
914 return ERR_PTR(-ENOMEM
);
916 ispi
->base
= devm_ioremap_resource(dev
, mem
);
917 if (IS_ERR(ispi
->base
))
918 return ERR_CAST(ispi
->base
);
922 ispi
->writeable
= info
->writeable
;
924 ret
= intel_spi_init(ispi
);
928 ispi
->nor
.dev
= ispi
->dev
;
929 ispi
->nor
.priv
= ispi
;
930 ispi
->nor
.controller_ops
= &intel_spi_controller_ops
;
932 ret
= spi_nor_scan(&ispi
->nor
, NULL
, &hwcaps
);
934 dev_info(dev
, "failed to locate the chip\n");
938 intel_spi_fill_partition(ispi
, &part
);
940 /* Prevent writes if not explicitly enabled */
941 if (!ispi
->writeable
|| !writeable
)
942 ispi
->nor
.mtd
.flags
&= ~MTD_WRITEABLE
;
944 ret
= mtd_device_register(&ispi
->nor
.mtd
, &part
, 1);
950 EXPORT_SYMBOL_GPL(intel_spi_probe
);
952 int intel_spi_remove(struct intel_spi
*ispi
)
954 return mtd_device_unregister(&ispi
->nor
.mtd
);
956 EXPORT_SYMBOL_GPL(intel_spi_remove
);
958 MODULE_DESCRIPTION("Intel PCH/PCU SPI flash core driver");
959 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
960 MODULE_LICENSE("GPL v2");