2 * Intel PCH/PCU SPI flash driver.
4 * Copyright (C) 2016, Intel Corporation
5 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/err.h>
14 #include <linux/iopoll.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/sizes.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/partitions.h>
20 #include <linux/mtd/spi-nor.h>
21 #include <linux/platform_data/intel-spi.h>
23 #include "intel-spi.h"
25 /* Offsets are from @ispi->base */
28 #define HSFSTS_CTL 0x04
29 #define HSFSTS_CTL_FSMIE BIT(31)
30 #define HSFSTS_CTL_FDBC_SHIFT 24
31 #define HSFSTS_CTL_FDBC_MASK (0x3f << HSFSTS_CTL_FDBC_SHIFT)
33 #define HSFSTS_CTL_FCYCLE_SHIFT 17
34 #define HSFSTS_CTL_FCYCLE_MASK (0x0f << HSFSTS_CTL_FCYCLE_SHIFT)
35 /* HW sequencer opcodes */
36 #define HSFSTS_CTL_FCYCLE_READ (0x00 << HSFSTS_CTL_FCYCLE_SHIFT)
37 #define HSFSTS_CTL_FCYCLE_WRITE (0x02 << HSFSTS_CTL_FCYCLE_SHIFT)
38 #define HSFSTS_CTL_FCYCLE_ERASE (0x03 << HSFSTS_CTL_FCYCLE_SHIFT)
39 #define HSFSTS_CTL_FCYCLE_ERASE_64K (0x04 << HSFSTS_CTL_FCYCLE_SHIFT)
40 #define HSFSTS_CTL_FCYCLE_RDID (0x06 << HSFSTS_CTL_FCYCLE_SHIFT)
41 #define HSFSTS_CTL_FCYCLE_WRSR (0x07 << HSFSTS_CTL_FCYCLE_SHIFT)
42 #define HSFSTS_CTL_FCYCLE_RDSR (0x08 << HSFSTS_CTL_FCYCLE_SHIFT)
44 #define HSFSTS_CTL_FGO BIT(16)
45 #define HSFSTS_CTL_FLOCKDN BIT(15)
46 #define HSFSTS_CTL_FDV BIT(14)
47 #define HSFSTS_CTL_SCIP BIT(5)
48 #define HSFSTS_CTL_AEL BIT(2)
49 #define HSFSTS_CTL_FCERR BIT(1)
50 #define HSFSTS_CTL_FDONE BIT(0)
54 #define FDATA(n) (0x10 + ((n) * 4))
58 #define FREG(n) (0x54 + ((n) * 4))
59 #define FREG_BASE_MASK 0x3fff
60 #define FREG_LIMIT_SHIFT 16
61 #define FREG_LIMIT_MASK (0x03fff << FREG_LIMIT_SHIFT)
63 /* Offset is from @ispi->pregs */
64 #define PR(n) ((n) * 4)
65 #define PR_WPE BIT(31)
66 #define PR_LIMIT_SHIFT 16
67 #define PR_LIMIT_MASK (0x3fff << PR_LIMIT_SHIFT)
68 #define PR_RPE BIT(15)
69 #define PR_BASE_MASK 0x3fff
71 /* Offsets are from @ispi->sregs */
72 #define SSFSTS_CTL 0x00
73 #define SSFSTS_CTL_FSMIE BIT(23)
74 #define SSFSTS_CTL_DS BIT(22)
75 #define SSFSTS_CTL_DBC_SHIFT 16
76 #define SSFSTS_CTL_SPOP BIT(11)
77 #define SSFSTS_CTL_ACS BIT(10)
78 #define SSFSTS_CTL_SCGO BIT(9)
79 #define SSFSTS_CTL_COP_SHIFT 12
80 #define SSFSTS_CTL_FRS BIT(7)
81 #define SSFSTS_CTL_DOFRS BIT(6)
82 #define SSFSTS_CTL_AEL BIT(4)
83 #define SSFSTS_CTL_FCERR BIT(3)
84 #define SSFSTS_CTL_FDONE BIT(2)
85 #define SSFSTS_CTL_SCIP BIT(0)
87 #define PREOP_OPTYPE 0x04
91 #define OPTYPE_READ_NO_ADDR 0
92 #define OPTYPE_WRITE_NO_ADDR 1
93 #define OPTYPE_READ_WITH_ADDR 2
94 #define OPTYPE_WRITE_WITH_ADDR 3
98 #define BYT_SSFSTS_CTL 0x90
100 #define BYT_BCR_WPD BIT(0)
101 #define BYT_FREG_NUM 5
105 #define LPT_SSFSTS_CTL 0x90
106 #define LPT_FREG_NUM 5
110 #define BXT_SSFSTS_CTL 0xa0
111 #define BXT_FREG_NUM 12
116 #define ERASE_OPCODE_SHIFT 8
117 #define ERASE_OPCODE_MASK (0xff << ERASE_OPCODE_SHIFT)
118 #define ERASE_64K_OPCODE_SHIFT 16
119 #define ERASE_64K_OPCODE_MASK (0xff << ERASE_OPCODE_SHIFT)
121 #define INTEL_SPI_TIMEOUT 5000 /* ms */
122 #define INTEL_SPI_FIFO_SZ 64
125 * struct intel_spi - Driver private data
126 * @dev: Device pointer
127 * @info: Pointer to board specific info
128 * @nor: SPI NOR layer structure
129 * @base: Beginning of MMIO space
130 * @pregs: Start of protection registers
131 * @sregs: Start of software sequencer registers
132 * @nregions: Maximum number of regions
133 * @pr_num: Maximum number of protected range registers
134 * @writeable: Is the chip writeable
135 * @locked: Is SPI setting locked
136 * @swseq_reg: Use SW sequencer in register reads/writes
137 * @swseq_erase: Use SW sequencer in erase operation
138 * @erase_64k: 64k erase supported
139 * @atomic_preopcode: Holds preopcode when atomic sequence is requested
140 * @opcodes: Opcodes which are supported. This are programmed by BIOS
141 * before it locks down the controller.
145 const struct intel_spi_boardinfo
*info
;
161 static bool writeable
;
162 module_param(writeable
, bool, 0);
163 MODULE_PARM_DESC(writeable
, "Enable write access to SPI flash chip (default=0)");
165 static void intel_spi_dump_regs(struct intel_spi
*ispi
)
170 dev_dbg(ispi
->dev
, "BFPREG=0x%08x\n", readl(ispi
->base
+ BFPREG
));
172 value
= readl(ispi
->base
+ HSFSTS_CTL
);
173 dev_dbg(ispi
->dev
, "HSFSTS_CTL=0x%08x\n", value
);
174 if (value
& HSFSTS_CTL_FLOCKDN
)
175 dev_dbg(ispi
->dev
, "-> Locked\n");
177 dev_dbg(ispi
->dev
, "FADDR=0x%08x\n", readl(ispi
->base
+ FADDR
));
178 dev_dbg(ispi
->dev
, "DLOCK=0x%08x\n", readl(ispi
->base
+ DLOCK
));
180 for (i
= 0; i
< 16; i
++)
181 dev_dbg(ispi
->dev
, "FDATA(%d)=0x%08x\n",
182 i
, readl(ispi
->base
+ FDATA(i
)));
184 dev_dbg(ispi
->dev
, "FRACC=0x%08x\n", readl(ispi
->base
+ FRACC
));
186 for (i
= 0; i
< ispi
->nregions
; i
++)
187 dev_dbg(ispi
->dev
, "FREG(%d)=0x%08x\n", i
,
188 readl(ispi
->base
+ FREG(i
)));
189 for (i
= 0; i
< ispi
->pr_num
; i
++)
190 dev_dbg(ispi
->dev
, "PR(%d)=0x%08x\n", i
,
191 readl(ispi
->pregs
+ PR(i
)));
193 value
= readl(ispi
->sregs
+ SSFSTS_CTL
);
194 dev_dbg(ispi
->dev
, "SSFSTS_CTL=0x%08x\n", value
);
195 dev_dbg(ispi
->dev
, "PREOP_OPTYPE=0x%08x\n",
196 readl(ispi
->sregs
+ PREOP_OPTYPE
));
197 dev_dbg(ispi
->dev
, "OPMENU0=0x%08x\n", readl(ispi
->sregs
+ OPMENU0
));
198 dev_dbg(ispi
->dev
, "OPMENU1=0x%08x\n", readl(ispi
->sregs
+ OPMENU1
));
200 if (ispi
->info
->type
== INTEL_SPI_BYT
)
201 dev_dbg(ispi
->dev
, "BCR=0x%08x\n", readl(ispi
->base
+ BYT_BCR
));
203 dev_dbg(ispi
->dev
, "LVSCC=0x%08x\n", readl(ispi
->base
+ LVSCC
));
204 dev_dbg(ispi
->dev
, "UVSCC=0x%08x\n", readl(ispi
->base
+ UVSCC
));
206 dev_dbg(ispi
->dev
, "Protected regions:\n");
207 for (i
= 0; i
< ispi
->pr_num
; i
++) {
210 value
= readl(ispi
->pregs
+ PR(i
));
211 if (!(value
& (PR_WPE
| PR_RPE
)))
214 limit
= (value
& PR_LIMIT_MASK
) >> PR_LIMIT_SHIFT
;
215 base
= value
& PR_BASE_MASK
;
217 dev_dbg(ispi
->dev
, " %02d base: 0x%08x limit: 0x%08x [%c%c]\n",
218 i
, base
<< 12, (limit
<< 12) | 0xfff,
219 value
& PR_WPE
? 'W' : '.',
220 value
& PR_RPE
? 'R' : '.');
223 dev_dbg(ispi
->dev
, "Flash regions:\n");
224 for (i
= 0; i
< ispi
->nregions
; i
++) {
225 u32 region
, base
, limit
;
227 region
= readl(ispi
->base
+ FREG(i
));
228 base
= region
& FREG_BASE_MASK
;
229 limit
= (region
& FREG_LIMIT_MASK
) >> FREG_LIMIT_SHIFT
;
231 if (base
>= limit
|| (i
> 0 && limit
== 0))
232 dev_dbg(ispi
->dev
, " %02d disabled\n", i
);
234 dev_dbg(ispi
->dev
, " %02d base: 0x%08x limit: 0x%08x\n",
235 i
, base
<< 12, (limit
<< 12) | 0xfff);
238 dev_dbg(ispi
->dev
, "Using %cW sequencer for register access\n",
239 ispi
->swseq_reg
? 'S' : 'H');
240 dev_dbg(ispi
->dev
, "Using %cW sequencer for erase operation\n",
241 ispi
->swseq_erase
? 'S' : 'H');
244 /* Reads max INTEL_SPI_FIFO_SZ bytes from the device fifo */
245 static int intel_spi_read_block(struct intel_spi
*ispi
, void *buf
, size_t size
)
250 if (size
> INTEL_SPI_FIFO_SZ
)
254 bytes
= min_t(size_t, size
, 4);
255 memcpy_fromio(buf
, ispi
->base
+ FDATA(i
), bytes
);
264 /* Writes max INTEL_SPI_FIFO_SZ bytes to the device fifo */
265 static int intel_spi_write_block(struct intel_spi
*ispi
, const void *buf
,
271 if (size
> INTEL_SPI_FIFO_SZ
)
275 bytes
= min_t(size_t, size
, 4);
276 memcpy_toio(ispi
->base
+ FDATA(i
), buf
, bytes
);
285 static int intel_spi_wait_hw_busy(struct intel_spi
*ispi
)
289 return readl_poll_timeout(ispi
->base
+ HSFSTS_CTL
, val
,
290 !(val
& HSFSTS_CTL_SCIP
), 40,
291 INTEL_SPI_TIMEOUT
* 1000);
294 static int intel_spi_wait_sw_busy(struct intel_spi
*ispi
)
298 return readl_poll_timeout(ispi
->sregs
+ SSFSTS_CTL
, val
,
299 !(val
& SSFSTS_CTL_SCIP
), 40,
300 INTEL_SPI_TIMEOUT
* 1000);
303 static int intel_spi_init(struct intel_spi
*ispi
)
305 u32 opmenu0
, opmenu1
, lvscc
, uvscc
, val
;
308 switch (ispi
->info
->type
) {
310 ispi
->sregs
= ispi
->base
+ BYT_SSFSTS_CTL
;
311 ispi
->pregs
= ispi
->base
+ BYT_PR
;
312 ispi
->nregions
= BYT_FREG_NUM
;
313 ispi
->pr_num
= BYT_PR_NUM
;
314 ispi
->swseq_reg
= true;
317 /* Disable write protection */
318 val
= readl(ispi
->base
+ BYT_BCR
);
319 if (!(val
& BYT_BCR_WPD
)) {
321 writel(val
, ispi
->base
+ BYT_BCR
);
322 val
= readl(ispi
->base
+ BYT_BCR
);
325 ispi
->writeable
= !!(val
& BYT_BCR_WPD
);
331 ispi
->sregs
= ispi
->base
+ LPT_SSFSTS_CTL
;
332 ispi
->pregs
= ispi
->base
+ LPT_PR
;
333 ispi
->nregions
= LPT_FREG_NUM
;
334 ispi
->pr_num
= LPT_PR_NUM
;
335 ispi
->swseq_reg
= true;
339 ispi
->sregs
= ispi
->base
+ BXT_SSFSTS_CTL
;
340 ispi
->pregs
= ispi
->base
+ BXT_PR
;
341 ispi
->nregions
= BXT_FREG_NUM
;
342 ispi
->pr_num
= BXT_PR_NUM
;
343 ispi
->erase_64k
= true;
350 /* Disable #SMI generation from HW sequencer */
351 val
= readl(ispi
->base
+ HSFSTS_CTL
);
352 val
&= ~HSFSTS_CTL_FSMIE
;
353 writel(val
, ispi
->base
+ HSFSTS_CTL
);
356 * Determine whether erase operation should use HW or SW sequencer.
358 * The HW sequencer has a predefined list of opcodes, with only the
359 * erase opcode being programmable in LVSCC and UVSCC registers.
360 * If these registers don't contain a valid erase opcode, erase
361 * cannot be done using HW sequencer.
363 lvscc
= readl(ispi
->base
+ LVSCC
);
364 uvscc
= readl(ispi
->base
+ UVSCC
);
365 if (!(lvscc
& ERASE_OPCODE_MASK
) || !(uvscc
& ERASE_OPCODE_MASK
))
366 ispi
->swseq_erase
= true;
367 /* SPI controller on Intel BXT supports 64K erase opcode */
368 if (ispi
->info
->type
== INTEL_SPI_BXT
&& !ispi
->swseq_erase
)
369 if (!(lvscc
& ERASE_64K_OPCODE_MASK
) ||
370 !(uvscc
& ERASE_64K_OPCODE_MASK
))
371 ispi
->erase_64k
= false;
374 * Some controllers can only do basic operations using hardware
375 * sequencer. All other operations are supposed to be carried out
376 * using software sequencer.
378 if (ispi
->swseq_reg
) {
379 /* Disable #SMI generation from SW sequencer */
380 val
= readl(ispi
->sregs
+ SSFSTS_CTL
);
381 val
&= ~SSFSTS_CTL_FSMIE
;
382 writel(val
, ispi
->sregs
+ SSFSTS_CTL
);
385 /* Check controller's lock status */
386 val
= readl(ispi
->base
+ HSFSTS_CTL
);
387 ispi
->locked
= !!(val
& HSFSTS_CTL_FLOCKDN
);
391 * BIOS programs allowed opcodes and then locks down the
392 * register. So read back what opcodes it decided to support.
393 * That's the set we are going to support as well.
395 opmenu0
= readl(ispi
->sregs
+ OPMENU0
);
396 opmenu1
= readl(ispi
->sregs
+ OPMENU1
);
398 if (opmenu0
&& opmenu1
) {
399 for (i
= 0; i
< ARRAY_SIZE(ispi
->opcodes
) / 2; i
++) {
400 ispi
->opcodes
[i
] = opmenu0
>> i
* 8;
401 ispi
->opcodes
[i
+ 4] = opmenu1
>> i
* 8;
406 intel_spi_dump_regs(ispi
);
411 static int intel_spi_opcode_index(struct intel_spi
*ispi
, u8 opcode
, int optype
)
417 for (i
= 0; i
< ARRAY_SIZE(ispi
->opcodes
); i
++)
418 if (ispi
->opcodes
[i
] == opcode
)
424 /* The lock is off, so just use index 0 */
425 writel(opcode
, ispi
->sregs
+ OPMENU0
);
426 preop
= readw(ispi
->sregs
+ PREOP_OPTYPE
);
427 writel(optype
<< 16 | preop
, ispi
->sregs
+ PREOP_OPTYPE
);
432 static int intel_spi_hw_cycle(struct intel_spi
*ispi
, u8 opcode
, int len
)
437 val
= readl(ispi
->base
+ HSFSTS_CTL
);
438 val
&= ~(HSFSTS_CTL_FCYCLE_MASK
| HSFSTS_CTL_FDBC_MASK
);
442 val
|= HSFSTS_CTL_FCYCLE_RDID
;
445 val
|= HSFSTS_CTL_FCYCLE_WRSR
;
448 val
|= HSFSTS_CTL_FCYCLE_RDSR
;
454 if (len
> INTEL_SPI_FIFO_SZ
)
457 val
|= (len
- 1) << HSFSTS_CTL_FDBC_SHIFT
;
458 val
|= HSFSTS_CTL_FCERR
| HSFSTS_CTL_FDONE
;
459 val
|= HSFSTS_CTL_FGO
;
460 writel(val
, ispi
->base
+ HSFSTS_CTL
);
462 ret
= intel_spi_wait_hw_busy(ispi
);
466 status
= readl(ispi
->base
+ HSFSTS_CTL
);
467 if (status
& HSFSTS_CTL_FCERR
)
469 else if (status
& HSFSTS_CTL_AEL
)
475 static int intel_spi_sw_cycle(struct intel_spi
*ispi
, u8 opcode
, int len
,
482 ret
= intel_spi_opcode_index(ispi
, opcode
, optype
);
486 if (len
> INTEL_SPI_FIFO_SZ
)
490 * Always clear it after each SW sequencer operation regardless
491 * of whether it is successful or not.
493 atomic_preopcode
= ispi
->atomic_preopcode
;
494 ispi
->atomic_preopcode
= 0;
496 /* Only mark 'Data Cycle' bit when there is data to be transferred */
498 val
= ((len
- 1) << SSFSTS_CTL_DBC_SHIFT
) | SSFSTS_CTL_DS
;
499 val
|= ret
<< SSFSTS_CTL_COP_SHIFT
;
500 val
|= SSFSTS_CTL_FCERR
| SSFSTS_CTL_FDONE
;
501 val
|= SSFSTS_CTL_SCGO
;
502 if (atomic_preopcode
) {
506 case OPTYPE_WRITE_NO_ADDR
:
507 case OPTYPE_WRITE_WITH_ADDR
:
508 /* Pick matching preopcode for the atomic sequence */
509 preop
= readw(ispi
->sregs
+ PREOP_OPTYPE
);
510 if ((preop
& 0xff) == atomic_preopcode
)
512 else if ((preop
>> 8) == atomic_preopcode
)
513 val
|= SSFSTS_CTL_SPOP
;
517 /* Enable atomic sequence */
518 val
|= SSFSTS_CTL_ACS
;
526 writel(val
, ispi
->sregs
+ SSFSTS_CTL
);
528 ret
= intel_spi_wait_sw_busy(ispi
);
532 status
= readl(ispi
->sregs
+ SSFSTS_CTL
);
533 if (status
& SSFSTS_CTL_FCERR
)
535 else if (status
& SSFSTS_CTL_AEL
)
541 static int intel_spi_read_reg(struct spi_nor
*nor
, u8 opcode
, u8
*buf
, int len
)
543 struct intel_spi
*ispi
= nor
->priv
;
546 /* Address of the first chip */
547 writel(0, ispi
->base
+ FADDR
);
550 ret
= intel_spi_sw_cycle(ispi
, opcode
, len
,
551 OPTYPE_READ_NO_ADDR
);
553 ret
= intel_spi_hw_cycle(ispi
, opcode
, len
);
558 return intel_spi_read_block(ispi
, buf
, len
);
561 static int intel_spi_write_reg(struct spi_nor
*nor
, u8 opcode
, u8
*buf
, int len
)
563 struct intel_spi
*ispi
= nor
->priv
;
567 * This is handled with atomic operation and preop code in Intel
568 * controller so we only verify that it is available. If the
569 * controller is not locked, program the opcode to the PREOP
570 * register for later use.
572 * When hardware sequencer is used there is no need to program
573 * any opcodes (it handles them automatically as part of a command).
575 if (opcode
== SPINOR_OP_WREN
) {
578 if (!ispi
->swseq_reg
)
581 preop
= readw(ispi
->sregs
+ PREOP_OPTYPE
);
582 if ((preop
& 0xff) != opcode
&& (preop
>> 8) != opcode
) {
585 writel(opcode
, ispi
->sregs
+ PREOP_OPTYPE
);
589 * This enables atomic sequence on next SW sycle. Will
590 * be cleared after next operation.
592 ispi
->atomic_preopcode
= opcode
;
596 writel(0, ispi
->base
+ FADDR
);
598 /* Write the value beforehand */
599 ret
= intel_spi_write_block(ispi
, buf
, len
);
604 return intel_spi_sw_cycle(ispi
, opcode
, len
,
605 OPTYPE_WRITE_NO_ADDR
);
606 return intel_spi_hw_cycle(ispi
, opcode
, len
);
609 static ssize_t
intel_spi_read(struct spi_nor
*nor
, loff_t from
, size_t len
,
612 struct intel_spi
*ispi
= nor
->priv
;
613 size_t block_size
, retlen
= 0;
618 * Atomic sequence is not expected with HW sequencer reads. Make
619 * sure it is cleared regardless.
621 if (WARN_ON_ONCE(ispi
->atomic_preopcode
))
622 ispi
->atomic_preopcode
= 0;
624 switch (nor
->read_opcode
) {
626 case SPINOR_OP_READ_FAST
:
633 block_size
= min_t(size_t, len
, INTEL_SPI_FIFO_SZ
);
635 /* Read cannot cross 4K boundary */
636 block_size
= min_t(loff_t
, from
+ block_size
,
637 round_up(from
+ 1, SZ_4K
)) - from
;
639 writel(from
, ispi
->base
+ FADDR
);
641 val
= readl(ispi
->base
+ HSFSTS_CTL
);
642 val
&= ~(HSFSTS_CTL_FDBC_MASK
| HSFSTS_CTL_FCYCLE_MASK
);
643 val
|= HSFSTS_CTL_AEL
| HSFSTS_CTL_FCERR
| HSFSTS_CTL_FDONE
;
644 val
|= (block_size
- 1) << HSFSTS_CTL_FDBC_SHIFT
;
645 val
|= HSFSTS_CTL_FCYCLE_READ
;
646 val
|= HSFSTS_CTL_FGO
;
647 writel(val
, ispi
->base
+ HSFSTS_CTL
);
649 ret
= intel_spi_wait_hw_busy(ispi
);
653 status
= readl(ispi
->base
+ HSFSTS_CTL
);
654 if (status
& HSFSTS_CTL_FCERR
)
656 else if (status
& HSFSTS_CTL_AEL
)
660 dev_err(ispi
->dev
, "read error: %llx: %#x\n", from
,
665 ret
= intel_spi_read_block(ispi
, read_buf
, block_size
);
671 retlen
+= block_size
;
672 read_buf
+= block_size
;
678 static ssize_t
intel_spi_write(struct spi_nor
*nor
, loff_t to
, size_t len
,
679 const u_char
*write_buf
)
681 struct intel_spi
*ispi
= nor
->priv
;
682 size_t block_size
, retlen
= 0;
686 /* Not needed with HW sequencer write, make sure it is cleared */
687 ispi
->atomic_preopcode
= 0;
690 block_size
= min_t(size_t, len
, INTEL_SPI_FIFO_SZ
);
692 /* Write cannot cross 4K boundary */
693 block_size
= min_t(loff_t
, to
+ block_size
,
694 round_up(to
+ 1, SZ_4K
)) - to
;
696 writel(to
, ispi
->base
+ FADDR
);
698 val
= readl(ispi
->base
+ HSFSTS_CTL
);
699 val
&= ~(HSFSTS_CTL_FDBC_MASK
| HSFSTS_CTL_FCYCLE_MASK
);
700 val
|= HSFSTS_CTL_AEL
| HSFSTS_CTL_FCERR
| HSFSTS_CTL_FDONE
;
701 val
|= (block_size
- 1) << HSFSTS_CTL_FDBC_SHIFT
;
702 val
|= HSFSTS_CTL_FCYCLE_WRITE
;
704 ret
= intel_spi_write_block(ispi
, write_buf
, block_size
);
706 dev_err(ispi
->dev
, "failed to write block\n");
710 /* Start the write now */
711 val
|= HSFSTS_CTL_FGO
;
712 writel(val
, ispi
->base
+ HSFSTS_CTL
);
714 ret
= intel_spi_wait_hw_busy(ispi
);
716 dev_err(ispi
->dev
, "timeout\n");
720 status
= readl(ispi
->base
+ HSFSTS_CTL
);
721 if (status
& HSFSTS_CTL_FCERR
)
723 else if (status
& HSFSTS_CTL_AEL
)
727 dev_err(ispi
->dev
, "write error: %llx: %#x\n", to
,
734 retlen
+= block_size
;
735 write_buf
+= block_size
;
741 static int intel_spi_erase(struct spi_nor
*nor
, loff_t offs
)
743 size_t erase_size
, len
= nor
->mtd
.erasesize
;
744 struct intel_spi
*ispi
= nor
->priv
;
745 u32 val
, status
, cmd
;
748 /* If the hardware can do 64k erase use that when possible */
749 if (len
>= SZ_64K
&& ispi
->erase_64k
) {
750 cmd
= HSFSTS_CTL_FCYCLE_ERASE_64K
;
753 cmd
= HSFSTS_CTL_FCYCLE_ERASE
;
757 if (ispi
->swseq_erase
) {
759 writel(offs
, ispi
->base
+ FADDR
);
761 ret
= intel_spi_sw_cycle(ispi
, nor
->erase_opcode
,
762 0, OPTYPE_WRITE_WITH_ADDR
);
773 /* Not needed with HW sequencer erase, make sure it is cleared */
774 ispi
->atomic_preopcode
= 0;
777 writel(offs
, ispi
->base
+ FADDR
);
779 val
= readl(ispi
->base
+ HSFSTS_CTL
);
780 val
&= ~(HSFSTS_CTL_FDBC_MASK
| HSFSTS_CTL_FCYCLE_MASK
);
781 val
|= HSFSTS_CTL_AEL
| HSFSTS_CTL_FCERR
| HSFSTS_CTL_FDONE
;
783 val
|= HSFSTS_CTL_FGO
;
784 writel(val
, ispi
->base
+ HSFSTS_CTL
);
786 ret
= intel_spi_wait_hw_busy(ispi
);
790 status
= readl(ispi
->base
+ HSFSTS_CTL
);
791 if (status
& HSFSTS_CTL_FCERR
)
793 else if (status
& HSFSTS_CTL_AEL
)
803 static bool intel_spi_is_protected(const struct intel_spi
*ispi
,
804 unsigned int base
, unsigned int limit
)
808 for (i
= 0; i
< ispi
->pr_num
; i
++) {
809 u32 pr_base
, pr_limit
, pr_value
;
811 pr_value
= readl(ispi
->pregs
+ PR(i
));
812 if (!(pr_value
& (PR_WPE
| PR_RPE
)))
815 pr_limit
= (pr_value
& PR_LIMIT_MASK
) >> PR_LIMIT_SHIFT
;
816 pr_base
= pr_value
& PR_BASE_MASK
;
818 if (pr_base
>= base
&& pr_limit
<= limit
)
826 * There will be a single partition holding all enabled flash regions. We
829 static void intel_spi_fill_partition(struct intel_spi
*ispi
,
830 struct mtd_partition
*part
)
835 memset(part
, 0, sizeof(*part
));
837 /* Start from the mandatory descriptor region */
842 * Now try to find where this partition ends based on the flash
845 for (i
= 1; i
< ispi
->nregions
; i
++) {
846 u32 region
, base
, limit
;
848 region
= readl(ispi
->base
+ FREG(i
));
849 base
= region
& FREG_BASE_MASK
;
850 limit
= (region
& FREG_LIMIT_MASK
) >> FREG_LIMIT_SHIFT
;
852 if (base
>= limit
|| limit
== 0)
856 * If any of the regions have protection bits set, make the
857 * whole partition read-only to be on the safe side.
859 if (intel_spi_is_protected(ispi
, base
, limit
))
860 ispi
->writeable
= false;
862 end
= (limit
<< 12) + 4096;
863 if (end
> part
->size
)
868 struct intel_spi
*intel_spi_probe(struct device
*dev
,
869 struct resource
*mem
, const struct intel_spi_boardinfo
*info
)
871 const struct spi_nor_hwcaps hwcaps
= {
872 .mask
= SNOR_HWCAPS_READ
|
873 SNOR_HWCAPS_READ_FAST
|
876 struct mtd_partition part
;
877 struct intel_spi
*ispi
;
881 return ERR_PTR(-EINVAL
);
883 ispi
= devm_kzalloc(dev
, sizeof(*ispi
), GFP_KERNEL
);
885 return ERR_PTR(-ENOMEM
);
887 ispi
->base
= devm_ioremap_resource(dev
, mem
);
888 if (IS_ERR(ispi
->base
))
889 return ERR_CAST(ispi
->base
);
893 ispi
->writeable
= info
->writeable
;
895 ret
= intel_spi_init(ispi
);
899 ispi
->nor
.dev
= ispi
->dev
;
900 ispi
->nor
.priv
= ispi
;
901 ispi
->nor
.read_reg
= intel_spi_read_reg
;
902 ispi
->nor
.write_reg
= intel_spi_write_reg
;
903 ispi
->nor
.read
= intel_spi_read
;
904 ispi
->nor
.write
= intel_spi_write
;
905 ispi
->nor
.erase
= intel_spi_erase
;
907 ret
= spi_nor_scan(&ispi
->nor
, NULL
, &hwcaps
);
909 dev_info(dev
, "failed to locate the chip\n");
913 intel_spi_fill_partition(ispi
, &part
);
915 /* Prevent writes if not explicitly enabled */
916 if (!ispi
->writeable
|| !writeable
)
917 ispi
->nor
.mtd
.flags
&= ~MTD_WRITEABLE
;
919 ret
= mtd_device_register(&ispi
->nor
.mtd
, &part
, 1);
925 EXPORT_SYMBOL_GPL(intel_spi_probe
);
927 int intel_spi_remove(struct intel_spi
*ispi
)
929 return mtd_device_unregister(&ispi
->nor
.mtd
);
931 EXPORT_SYMBOL_GPL(intel_spi_remove
);
933 MODULE_DESCRIPTION("Intel PCH/PCU SPI flash core driver");
934 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
935 MODULE_LICENSE("GPL v2");