1e23e5a05SMika Westerberg // SPDX-License-Identifier: GPL-2.0-only
2e23e5a05SMika Westerberg /*
3e23e5a05SMika Westerberg * Intel PCH/PCU SPI flash driver.
4e23e5a05SMika Westerberg *
5e23e5a05SMika Westerberg * Copyright (C) 2016 - 2022, Intel Corporation
6e23e5a05SMika Westerberg * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
7e23e5a05SMika Westerberg */
8e23e5a05SMika Westerberg
9e23e5a05SMika Westerberg #include <linux/iopoll.h>
10e23e5a05SMika Westerberg #include <linux/module.h>
11e23e5a05SMika Westerberg
12e23e5a05SMika Westerberg #include <linux/mtd/partitions.h>
13e23e5a05SMika Westerberg #include <linux/mtd/spi-nor.h>
14e23e5a05SMika Westerberg
15e23e5a05SMika Westerberg #include <linux/spi/flash.h>
16e23e5a05SMika Westerberg #include <linux/spi/spi.h>
17e23e5a05SMika Westerberg #include <linux/spi/spi-mem.h>
18e23e5a05SMika Westerberg
19e23e5a05SMika Westerberg #include "spi-intel.h"
20e23e5a05SMika Westerberg
21e23e5a05SMika Westerberg /* Offsets are from @ispi->base */
22e23e5a05SMika Westerberg #define BFPREG 0x00
23e23e5a05SMika Westerberg
24e23e5a05SMika Westerberg #define HSFSTS_CTL 0x04
25e23e5a05SMika Westerberg #define HSFSTS_CTL_FSMIE BIT(31)
26e23e5a05SMika Westerberg #define HSFSTS_CTL_FDBC_SHIFT 24
27e23e5a05SMika Westerberg #define HSFSTS_CTL_FDBC_MASK (0x3f << HSFSTS_CTL_FDBC_SHIFT)
28e23e5a05SMika Westerberg
29e23e5a05SMika Westerberg #define HSFSTS_CTL_FCYCLE_SHIFT 17
30e23e5a05SMika Westerberg #define HSFSTS_CTL_FCYCLE_MASK (0x0f << HSFSTS_CTL_FCYCLE_SHIFT)
31e23e5a05SMika Westerberg /* HW sequencer opcodes */
32e23e5a05SMika Westerberg #define HSFSTS_CTL_FCYCLE_READ (0x00 << HSFSTS_CTL_FCYCLE_SHIFT)
33e23e5a05SMika Westerberg #define HSFSTS_CTL_FCYCLE_WRITE (0x02 << HSFSTS_CTL_FCYCLE_SHIFT)
34e23e5a05SMika Westerberg #define HSFSTS_CTL_FCYCLE_ERASE (0x03 << HSFSTS_CTL_FCYCLE_SHIFT)
35e23e5a05SMika Westerberg #define HSFSTS_CTL_FCYCLE_ERASE_64K (0x04 << HSFSTS_CTL_FCYCLE_SHIFT)
36ec4a04aaSMika Westerberg #define HSFSTS_CTL_FCYCLE_RDSFDP (0x05 << HSFSTS_CTL_FCYCLE_SHIFT)
37e23e5a05SMika Westerberg #define HSFSTS_CTL_FCYCLE_RDID (0x06 << HSFSTS_CTL_FCYCLE_SHIFT)
38e23e5a05SMika Westerberg #define HSFSTS_CTL_FCYCLE_WRSR (0x07 << HSFSTS_CTL_FCYCLE_SHIFT)
39e23e5a05SMika Westerberg #define HSFSTS_CTL_FCYCLE_RDSR (0x08 << HSFSTS_CTL_FCYCLE_SHIFT)
40e23e5a05SMika Westerberg
41e23e5a05SMika Westerberg #define HSFSTS_CTL_FGO BIT(16)
42e23e5a05SMika Westerberg #define HSFSTS_CTL_FLOCKDN BIT(15)
43e23e5a05SMika Westerberg #define HSFSTS_CTL_FDV BIT(14)
44e23e5a05SMika Westerberg #define HSFSTS_CTL_SCIP BIT(5)
45e23e5a05SMika Westerberg #define HSFSTS_CTL_AEL BIT(2)
46e23e5a05SMika Westerberg #define HSFSTS_CTL_FCERR BIT(1)
47e23e5a05SMika Westerberg #define HSFSTS_CTL_FDONE BIT(0)
48e23e5a05SMika Westerberg
49e23e5a05SMika Westerberg #define FADDR 0x08
50e23e5a05SMika Westerberg #define DLOCK 0x0c
51e23e5a05SMika Westerberg #define FDATA(n) (0x10 + ((n) * 4))
52e23e5a05SMika Westerberg
53e23e5a05SMika Westerberg #define FRACC 0x50
54e23e5a05SMika Westerberg
55e23e5a05SMika Westerberg #define FREG(n) (0x54 + ((n) * 4))
5692a66cbfSMika Westerberg #define FREG_BASE_MASK GENMASK(14, 0)
57e23e5a05SMika Westerberg #define FREG_LIMIT_SHIFT 16
5892a66cbfSMika Westerberg #define FREG_LIMIT_MASK GENMASK(30, 16)
59e23e5a05SMika Westerberg
60e23e5a05SMika Westerberg /* Offset is from @ispi->pregs */
61e23e5a05SMika Westerberg #define PR(n) ((n) * 4)
62e23e5a05SMika Westerberg #define PR_WPE BIT(31)
63e23e5a05SMika Westerberg #define PR_LIMIT_SHIFT 16
6492a66cbfSMika Westerberg #define PR_LIMIT_MASK GENMASK(30, 16)
65e23e5a05SMika Westerberg #define PR_RPE BIT(15)
6692a66cbfSMika Westerberg #define PR_BASE_MASK GENMASK(14, 0)
67e23e5a05SMika Westerberg
68e23e5a05SMika Westerberg /* Offsets are from @ispi->sregs */
69e23e5a05SMika Westerberg #define SSFSTS_CTL 0x00
70e23e5a05SMika Westerberg #define SSFSTS_CTL_FSMIE BIT(23)
71e23e5a05SMika Westerberg #define SSFSTS_CTL_DS BIT(22)
72e23e5a05SMika Westerberg #define SSFSTS_CTL_DBC_SHIFT 16
73e23e5a05SMika Westerberg #define SSFSTS_CTL_SPOP BIT(11)
74e23e5a05SMika Westerberg #define SSFSTS_CTL_ACS BIT(10)
75e23e5a05SMika Westerberg #define SSFSTS_CTL_SCGO BIT(9)
76e23e5a05SMika Westerberg #define SSFSTS_CTL_COP_SHIFT 12
77e23e5a05SMika Westerberg #define SSFSTS_CTL_FRS BIT(7)
78e23e5a05SMika Westerberg #define SSFSTS_CTL_DOFRS BIT(6)
79e23e5a05SMika Westerberg #define SSFSTS_CTL_AEL BIT(4)
80e23e5a05SMika Westerberg #define SSFSTS_CTL_FCERR BIT(3)
81e23e5a05SMika Westerberg #define SSFSTS_CTL_FDONE BIT(2)
82e23e5a05SMika Westerberg #define SSFSTS_CTL_SCIP BIT(0)
83e23e5a05SMika Westerberg
84e23e5a05SMika Westerberg #define PREOP_OPTYPE 0x04
85e23e5a05SMika Westerberg #define OPMENU0 0x08
86e23e5a05SMika Westerberg #define OPMENU1 0x0c
87e23e5a05SMika Westerberg
88e23e5a05SMika Westerberg #define OPTYPE_READ_NO_ADDR 0
89e23e5a05SMika Westerberg #define OPTYPE_WRITE_NO_ADDR 1
90e23e5a05SMika Westerberg #define OPTYPE_READ_WITH_ADDR 2
91e23e5a05SMika Westerberg #define OPTYPE_WRITE_WITH_ADDR 3
92e23e5a05SMika Westerberg
93e23e5a05SMika Westerberg /* CPU specifics */
94e23e5a05SMika Westerberg #define BYT_PR 0x74
95e23e5a05SMika Westerberg #define BYT_SSFSTS_CTL 0x90
96e23e5a05SMika Westerberg #define BYT_FREG_NUM 5
97e23e5a05SMika Westerberg #define BYT_PR_NUM 5
98e23e5a05SMika Westerberg
99e23e5a05SMika Westerberg #define LPT_PR 0x74
100e23e5a05SMika Westerberg #define LPT_SSFSTS_CTL 0x90
101e23e5a05SMika Westerberg #define LPT_FREG_NUM 5
102e23e5a05SMika Westerberg #define LPT_PR_NUM 5
103e23e5a05SMika Westerberg
104e23e5a05SMika Westerberg #define BXT_PR 0x84
105e23e5a05SMika Westerberg #define BXT_SSFSTS_CTL 0xa0
106e23e5a05SMika Westerberg #define BXT_FREG_NUM 12
107b4c58d54SMauro Lima #define BXT_PR_NUM 5
108e23e5a05SMika Westerberg
109e23e5a05SMika Westerberg #define CNL_PR 0x84
110e23e5a05SMika Westerberg #define CNL_FREG_NUM 6
111e23e5a05SMika Westerberg #define CNL_PR_NUM 5
112e23e5a05SMika Westerberg
113e23e5a05SMika Westerberg #define LVSCC 0xc4
114e23e5a05SMika Westerberg #define UVSCC 0xc8
115e23e5a05SMika Westerberg #define ERASE_OPCODE_SHIFT 8
116e23e5a05SMika Westerberg #define ERASE_OPCODE_MASK (0xff << ERASE_OPCODE_SHIFT)
117e23e5a05SMika Westerberg #define ERASE_64K_OPCODE_SHIFT 16
1186a43cd02SMauro Lima #define ERASE_64K_OPCODE_MASK (0xff << ERASE_64K_OPCODE_SHIFT)
119e23e5a05SMika Westerberg
1203f03c618SMika Westerberg /* Flash descriptor fields */
1213f03c618SMika Westerberg #define FLVALSIG_MAGIC 0x0ff0a55a
1223f03c618SMika Westerberg #define FLMAP0_NC_MASK GENMASK(9, 8)
1233f03c618SMika Westerberg #define FLMAP0_NC_SHIFT 8
1243f03c618SMika Westerberg #define FLMAP0_FCBA_MASK GENMASK(7, 0)
1253f03c618SMika Westerberg
1263f03c618SMika Westerberg #define FLCOMP_C0DEN_MASK GENMASK(3, 0)
1273f03c618SMika Westerberg #define FLCOMP_C0DEN_512K 0x00
1283f03c618SMika Westerberg #define FLCOMP_C0DEN_1M 0x01
1293f03c618SMika Westerberg #define FLCOMP_C0DEN_2M 0x02
1303f03c618SMika Westerberg #define FLCOMP_C0DEN_4M 0x03
1313f03c618SMika Westerberg #define FLCOMP_C0DEN_8M 0x04
1323f03c618SMika Westerberg #define FLCOMP_C0DEN_16M 0x05
1333f03c618SMika Westerberg #define FLCOMP_C0DEN_32M 0x06
1343f03c618SMika Westerberg #define FLCOMP_C0DEN_64M 0x07
1353f03c618SMika Westerberg
136e23e5a05SMika Westerberg #define INTEL_SPI_TIMEOUT 5000 /* ms */
137e23e5a05SMika Westerberg #define INTEL_SPI_FIFO_SZ 64
138e23e5a05SMika Westerberg
139e23e5a05SMika Westerberg /**
140e23e5a05SMika Westerberg * struct intel_spi - Driver private data
141e23e5a05SMika Westerberg * @dev: Device pointer
142e23e5a05SMika Westerberg * @info: Pointer to board specific info
143e23e5a05SMika Westerberg * @base: Beginning of MMIO space
144e23e5a05SMika Westerberg * @pregs: Start of protection registers
145e23e5a05SMika Westerberg * @sregs: Start of software sequencer registers
146*5fa0ade1SYang Yingliang * @host: Pointer to the SPI controller structure
147e23e5a05SMika Westerberg * @nregions: Maximum number of regions
148e23e5a05SMika Westerberg * @pr_num: Maximum number of protected range registers
1493f03c618SMika Westerberg * @chip0_size: Size of the first flash chip in bytes
150e23e5a05SMika Westerberg * @locked: Is SPI setting locked
151e23e5a05SMika Westerberg * @swseq_reg: Use SW sequencer in register reads/writes
152e23e5a05SMika Westerberg * @swseq_erase: Use SW sequencer in erase operation
153e23e5a05SMika Westerberg * @atomic_preopcode: Holds preopcode when atomic sequence is requested
154e23e5a05SMika Westerberg * @opcodes: Opcodes which are supported. This are programmed by BIOS
155e23e5a05SMika Westerberg * before it locks down the controller.
156e23e5a05SMika Westerberg * @mem_ops: Pointer to SPI MEM ops supported by the controller
157e23e5a05SMika Westerberg */
158e23e5a05SMika Westerberg struct intel_spi {
159e23e5a05SMika Westerberg struct device *dev;
160e23e5a05SMika Westerberg const struct intel_spi_boardinfo *info;
161e23e5a05SMika Westerberg void __iomem *base;
162e23e5a05SMika Westerberg void __iomem *pregs;
163e23e5a05SMika Westerberg void __iomem *sregs;
164*5fa0ade1SYang Yingliang struct spi_controller *host;
165e23e5a05SMika Westerberg size_t nregions;
166e23e5a05SMika Westerberg size_t pr_num;
1673f03c618SMika Westerberg size_t chip0_size;
168e23e5a05SMika Westerberg bool locked;
169e23e5a05SMika Westerberg bool swseq_reg;
170e23e5a05SMika Westerberg bool swseq_erase;
171e23e5a05SMika Westerberg u8 atomic_preopcode;
172e23e5a05SMika Westerberg u8 opcodes[8];
173e23e5a05SMika Westerberg const struct intel_spi_mem_op *mem_ops;
174e23e5a05SMika Westerberg };
175e23e5a05SMika Westerberg
176e23e5a05SMika Westerberg struct intel_spi_mem_op {
177e23e5a05SMika Westerberg struct spi_mem_op mem_op;
178e23e5a05SMika Westerberg u32 replacement_op;
179e23e5a05SMika Westerberg int (*exec_op)(struct intel_spi *ispi,
1803f03c618SMika Westerberg const struct spi_mem *mem,
181e23e5a05SMika Westerberg const struct intel_spi_mem_op *iop,
182e23e5a05SMika Westerberg const struct spi_mem_op *op);
183e23e5a05SMika Westerberg };
184e23e5a05SMika Westerberg
185e23e5a05SMika Westerberg static bool writeable;
186e23e5a05SMika Westerberg module_param(writeable, bool, 0);
187e23e5a05SMika Westerberg MODULE_PARM_DESC(writeable, "Enable write access to SPI flash chip (default=0)");
188e23e5a05SMika Westerberg
intel_spi_dump_regs(struct intel_spi * ispi)189e23e5a05SMika Westerberg static void intel_spi_dump_regs(struct intel_spi *ispi)
190e23e5a05SMika Westerberg {
191e23e5a05SMika Westerberg u32 value;
192e23e5a05SMika Westerberg int i;
193e23e5a05SMika Westerberg
194e23e5a05SMika Westerberg dev_dbg(ispi->dev, "BFPREG=0x%08x\n", readl(ispi->base + BFPREG));
195e23e5a05SMika Westerberg
196e23e5a05SMika Westerberg value = readl(ispi->base + HSFSTS_CTL);
197e23e5a05SMika Westerberg dev_dbg(ispi->dev, "HSFSTS_CTL=0x%08x\n", value);
198e23e5a05SMika Westerberg if (value & HSFSTS_CTL_FLOCKDN)
199e23e5a05SMika Westerberg dev_dbg(ispi->dev, "-> Locked\n");
200e23e5a05SMika Westerberg
201e23e5a05SMika Westerberg dev_dbg(ispi->dev, "FADDR=0x%08x\n", readl(ispi->base + FADDR));
202e23e5a05SMika Westerberg dev_dbg(ispi->dev, "DLOCK=0x%08x\n", readl(ispi->base + DLOCK));
203e23e5a05SMika Westerberg
204e23e5a05SMika Westerberg for (i = 0; i < 16; i++)
205e23e5a05SMika Westerberg dev_dbg(ispi->dev, "FDATA(%d)=0x%08x\n",
206e23e5a05SMika Westerberg i, readl(ispi->base + FDATA(i)));
207e23e5a05SMika Westerberg
208e23e5a05SMika Westerberg dev_dbg(ispi->dev, "FRACC=0x%08x\n", readl(ispi->base + FRACC));
209e23e5a05SMika Westerberg
210e23e5a05SMika Westerberg for (i = 0; i < ispi->nregions; i++)
211e23e5a05SMika Westerberg dev_dbg(ispi->dev, "FREG(%d)=0x%08x\n", i,
212e23e5a05SMika Westerberg readl(ispi->base + FREG(i)));
213e23e5a05SMika Westerberg for (i = 0; i < ispi->pr_num; i++)
214e23e5a05SMika Westerberg dev_dbg(ispi->dev, "PR(%d)=0x%08x\n", i,
215e23e5a05SMika Westerberg readl(ispi->pregs + PR(i)));
216e23e5a05SMika Westerberg
217e23e5a05SMika Westerberg if (ispi->sregs) {
218e23e5a05SMika Westerberg value = readl(ispi->sregs + SSFSTS_CTL);
219e23e5a05SMika Westerberg dev_dbg(ispi->dev, "SSFSTS_CTL=0x%08x\n", value);
220e23e5a05SMika Westerberg dev_dbg(ispi->dev, "PREOP_OPTYPE=0x%08x\n",
221e23e5a05SMika Westerberg readl(ispi->sregs + PREOP_OPTYPE));
222e23e5a05SMika Westerberg dev_dbg(ispi->dev, "OPMENU0=0x%08x\n",
223e23e5a05SMika Westerberg readl(ispi->sregs + OPMENU0));
224e23e5a05SMika Westerberg dev_dbg(ispi->dev, "OPMENU1=0x%08x\n",
225e23e5a05SMika Westerberg readl(ispi->sregs + OPMENU1));
226e23e5a05SMika Westerberg }
227e23e5a05SMika Westerberg
228e23e5a05SMika Westerberg dev_dbg(ispi->dev, "LVSCC=0x%08x\n", readl(ispi->base + LVSCC));
229e23e5a05SMika Westerberg dev_dbg(ispi->dev, "UVSCC=0x%08x\n", readl(ispi->base + UVSCC));
230e23e5a05SMika Westerberg
231e23e5a05SMika Westerberg dev_dbg(ispi->dev, "Protected regions:\n");
232e23e5a05SMika Westerberg for (i = 0; i < ispi->pr_num; i++) {
233e23e5a05SMika Westerberg u32 base, limit;
234e23e5a05SMika Westerberg
235e23e5a05SMika Westerberg value = readl(ispi->pregs + PR(i));
236e23e5a05SMika Westerberg if (!(value & (PR_WPE | PR_RPE)))
237e23e5a05SMika Westerberg continue;
238e23e5a05SMika Westerberg
239e23e5a05SMika Westerberg limit = (value & PR_LIMIT_MASK) >> PR_LIMIT_SHIFT;
240e23e5a05SMika Westerberg base = value & PR_BASE_MASK;
241e23e5a05SMika Westerberg
242e23e5a05SMika Westerberg dev_dbg(ispi->dev, " %02d base: 0x%08x limit: 0x%08x [%c%c]\n",
243e23e5a05SMika Westerberg i, base << 12, (limit << 12) | 0xfff,
244e23e5a05SMika Westerberg value & PR_WPE ? 'W' : '.', value & PR_RPE ? 'R' : '.');
245e23e5a05SMika Westerberg }
246e23e5a05SMika Westerberg
247e23e5a05SMika Westerberg dev_dbg(ispi->dev, "Flash regions:\n");
248e23e5a05SMika Westerberg for (i = 0; i < ispi->nregions; i++) {
249e23e5a05SMika Westerberg u32 region, base, limit;
250e23e5a05SMika Westerberg
251e23e5a05SMika Westerberg region = readl(ispi->base + FREG(i));
252e23e5a05SMika Westerberg base = region & FREG_BASE_MASK;
253e23e5a05SMika Westerberg limit = (region & FREG_LIMIT_MASK) >> FREG_LIMIT_SHIFT;
254e23e5a05SMika Westerberg
255e23e5a05SMika Westerberg if (base >= limit || (i > 0 && limit == 0))
256e23e5a05SMika Westerberg dev_dbg(ispi->dev, " %02d disabled\n", i);
257e23e5a05SMika Westerberg else
258e23e5a05SMika Westerberg dev_dbg(ispi->dev, " %02d base: 0x%08x limit: 0x%08x\n",
259e23e5a05SMika Westerberg i, base << 12, (limit << 12) | 0xfff);
260e23e5a05SMika Westerberg }
261e23e5a05SMika Westerberg
262e23e5a05SMika Westerberg dev_dbg(ispi->dev, "Using %cW sequencer for register access\n",
263e23e5a05SMika Westerberg ispi->swseq_reg ? 'S' : 'H');
264e23e5a05SMika Westerberg dev_dbg(ispi->dev, "Using %cW sequencer for erase operation\n",
265e23e5a05SMika Westerberg ispi->swseq_erase ? 'S' : 'H');
266e23e5a05SMika Westerberg }
267e23e5a05SMika Westerberg
268e23e5a05SMika Westerberg /* Reads max INTEL_SPI_FIFO_SZ bytes from the device fifo */
intel_spi_read_block(struct intel_spi * ispi,void * buf,size_t size)269e23e5a05SMika Westerberg static int intel_spi_read_block(struct intel_spi *ispi, void *buf, size_t size)
270e23e5a05SMika Westerberg {
271e23e5a05SMika Westerberg size_t bytes;
272e23e5a05SMika Westerberg int i = 0;
273e23e5a05SMika Westerberg
274e23e5a05SMika Westerberg if (size > INTEL_SPI_FIFO_SZ)
275e23e5a05SMika Westerberg return -EINVAL;
276e23e5a05SMika Westerberg
277e23e5a05SMika Westerberg while (size > 0) {
278e23e5a05SMika Westerberg bytes = min_t(size_t, size, 4);
279e23e5a05SMika Westerberg memcpy_fromio(buf, ispi->base + FDATA(i), bytes);
280e23e5a05SMika Westerberg size -= bytes;
281e23e5a05SMika Westerberg buf += bytes;
282e23e5a05SMika Westerberg i++;
283e23e5a05SMika Westerberg }
284e23e5a05SMika Westerberg
285e23e5a05SMika Westerberg return 0;
286e23e5a05SMika Westerberg }
287e23e5a05SMika Westerberg
288e23e5a05SMika Westerberg /* Writes max INTEL_SPI_FIFO_SZ bytes to the device fifo */
intel_spi_write_block(struct intel_spi * ispi,const void * buf,size_t size)289e23e5a05SMika Westerberg static int intel_spi_write_block(struct intel_spi *ispi, const void *buf,
290e23e5a05SMika Westerberg size_t size)
291e23e5a05SMika Westerberg {
292e23e5a05SMika Westerberg size_t bytes;
293e23e5a05SMika Westerberg int i = 0;
294e23e5a05SMika Westerberg
295e23e5a05SMika Westerberg if (size > INTEL_SPI_FIFO_SZ)
296e23e5a05SMika Westerberg return -EINVAL;
297e23e5a05SMika Westerberg
298e23e5a05SMika Westerberg while (size > 0) {
299e23e5a05SMika Westerberg bytes = min_t(size_t, size, 4);
300e23e5a05SMika Westerberg memcpy_toio(ispi->base + FDATA(i), buf, bytes);
301e23e5a05SMika Westerberg size -= bytes;
302e23e5a05SMika Westerberg buf += bytes;
303e23e5a05SMika Westerberg i++;
304e23e5a05SMika Westerberg }
305e23e5a05SMika Westerberg
306e23e5a05SMika Westerberg return 0;
307e23e5a05SMika Westerberg }
308e23e5a05SMika Westerberg
intel_spi_wait_hw_busy(struct intel_spi * ispi)309e23e5a05SMika Westerberg static int intel_spi_wait_hw_busy(struct intel_spi *ispi)
310e23e5a05SMika Westerberg {
311e23e5a05SMika Westerberg u32 val;
312e23e5a05SMika Westerberg
313e23e5a05SMika Westerberg return readl_poll_timeout(ispi->base + HSFSTS_CTL, val,
314e23e5a05SMika Westerberg !(val & HSFSTS_CTL_SCIP), 0,
315e23e5a05SMika Westerberg INTEL_SPI_TIMEOUT * 1000);
316e23e5a05SMika Westerberg }
317e23e5a05SMika Westerberg
intel_spi_wait_sw_busy(struct intel_spi * ispi)318e23e5a05SMika Westerberg static int intel_spi_wait_sw_busy(struct intel_spi *ispi)
319e23e5a05SMika Westerberg {
320e23e5a05SMika Westerberg u32 val;
321e23e5a05SMika Westerberg
322e23e5a05SMika Westerberg return readl_poll_timeout(ispi->sregs + SSFSTS_CTL, val,
323e23e5a05SMika Westerberg !(val & SSFSTS_CTL_SCIP), 0,
324e23e5a05SMika Westerberg INTEL_SPI_TIMEOUT * 1000);
325e23e5a05SMika Westerberg }
326e23e5a05SMika Westerberg
intel_spi_set_writeable(struct intel_spi * ispi)327e23e5a05SMika Westerberg static bool intel_spi_set_writeable(struct intel_spi *ispi)
328e23e5a05SMika Westerberg {
329e23e5a05SMika Westerberg if (!ispi->info->set_writeable)
330e23e5a05SMika Westerberg return false;
331e23e5a05SMika Westerberg
332e23e5a05SMika Westerberg return ispi->info->set_writeable(ispi->base, ispi->info->data);
333e23e5a05SMika Westerberg }
334e23e5a05SMika Westerberg
intel_spi_opcode_index(struct intel_spi * ispi,u8 opcode,int optype)335e23e5a05SMika Westerberg static int intel_spi_opcode_index(struct intel_spi *ispi, u8 opcode, int optype)
336e23e5a05SMika Westerberg {
337e23e5a05SMika Westerberg int i;
338e23e5a05SMika Westerberg int preop;
339e23e5a05SMika Westerberg
340e23e5a05SMika Westerberg if (ispi->locked) {
341e23e5a05SMika Westerberg for (i = 0; i < ARRAY_SIZE(ispi->opcodes); i++)
342e23e5a05SMika Westerberg if (ispi->opcodes[i] == opcode)
343e23e5a05SMika Westerberg return i;
344e23e5a05SMika Westerberg
345e23e5a05SMika Westerberg return -EINVAL;
346e23e5a05SMika Westerberg }
347e23e5a05SMika Westerberg
348e23e5a05SMika Westerberg /* The lock is off, so just use index 0 */
349e23e5a05SMika Westerberg writel(opcode, ispi->sregs + OPMENU0);
350e23e5a05SMika Westerberg preop = readw(ispi->sregs + PREOP_OPTYPE);
351e23e5a05SMika Westerberg writel(optype << 16 | preop, ispi->sregs + PREOP_OPTYPE);
352e23e5a05SMika Westerberg
353e23e5a05SMika Westerberg return 0;
354e23e5a05SMika Westerberg }
355e23e5a05SMika Westerberg
intel_spi_hw_cycle(struct intel_spi * ispi,const struct intel_spi_mem_op * iop,size_t len)356f73f6bd2SMika Westerberg static int intel_spi_hw_cycle(struct intel_spi *ispi,
357f73f6bd2SMika Westerberg const struct intel_spi_mem_op *iop, size_t len)
358e23e5a05SMika Westerberg {
359e23e5a05SMika Westerberg u32 val, status;
360e23e5a05SMika Westerberg int ret;
361e23e5a05SMika Westerberg
362f73f6bd2SMika Westerberg if (!iop->replacement_op)
363f73f6bd2SMika Westerberg return -EINVAL;
364f73f6bd2SMika Westerberg
365e23e5a05SMika Westerberg val = readl(ispi->base + HSFSTS_CTL);
366e23e5a05SMika Westerberg val &= ~(HSFSTS_CTL_FCYCLE_MASK | HSFSTS_CTL_FDBC_MASK);
367e23e5a05SMika Westerberg val |= (len - 1) << HSFSTS_CTL_FDBC_SHIFT;
368e23e5a05SMika Westerberg val |= HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
369e23e5a05SMika Westerberg val |= HSFSTS_CTL_FGO;
370f73f6bd2SMika Westerberg val |= iop->replacement_op;
371e23e5a05SMika Westerberg writel(val, ispi->base + HSFSTS_CTL);
372e23e5a05SMika Westerberg
373e23e5a05SMika Westerberg ret = intel_spi_wait_hw_busy(ispi);
374e23e5a05SMika Westerberg if (ret)
375e23e5a05SMika Westerberg return ret;
376e23e5a05SMika Westerberg
377e23e5a05SMika Westerberg status = readl(ispi->base + HSFSTS_CTL);
378e23e5a05SMika Westerberg if (status & HSFSTS_CTL_FCERR)
379e23e5a05SMika Westerberg return -EIO;
380e23e5a05SMika Westerberg else if (status & HSFSTS_CTL_AEL)
381e23e5a05SMika Westerberg return -EACCES;
382e23e5a05SMika Westerberg
383e23e5a05SMika Westerberg return 0;
384e23e5a05SMika Westerberg }
385e23e5a05SMika Westerberg
intel_spi_sw_cycle(struct intel_spi * ispi,u8 opcode,size_t len,int optype)386e23e5a05SMika Westerberg static int intel_spi_sw_cycle(struct intel_spi *ispi, u8 opcode, size_t len,
387e23e5a05SMika Westerberg int optype)
388e23e5a05SMika Westerberg {
389e23e5a05SMika Westerberg u32 val = 0, status;
390e23e5a05SMika Westerberg u8 atomic_preopcode;
391e23e5a05SMika Westerberg int ret;
392e23e5a05SMika Westerberg
393e23e5a05SMika Westerberg ret = intel_spi_opcode_index(ispi, opcode, optype);
394e23e5a05SMika Westerberg if (ret < 0)
395e23e5a05SMika Westerberg return ret;
396e23e5a05SMika Westerberg
397e23e5a05SMika Westerberg /*
398e23e5a05SMika Westerberg * Always clear it after each SW sequencer operation regardless
399e23e5a05SMika Westerberg * of whether it is successful or not.
400e23e5a05SMika Westerberg */
401e23e5a05SMika Westerberg atomic_preopcode = ispi->atomic_preopcode;
402e23e5a05SMika Westerberg ispi->atomic_preopcode = 0;
403e23e5a05SMika Westerberg
404e23e5a05SMika Westerberg /* Only mark 'Data Cycle' bit when there is data to be transferred */
405e23e5a05SMika Westerberg if (len > 0)
406e23e5a05SMika Westerberg val = ((len - 1) << SSFSTS_CTL_DBC_SHIFT) | SSFSTS_CTL_DS;
407e23e5a05SMika Westerberg val |= ret << SSFSTS_CTL_COP_SHIFT;
408e23e5a05SMika Westerberg val |= SSFSTS_CTL_FCERR | SSFSTS_CTL_FDONE;
409e23e5a05SMika Westerberg val |= SSFSTS_CTL_SCGO;
410e23e5a05SMika Westerberg if (atomic_preopcode) {
411e23e5a05SMika Westerberg u16 preop;
412e23e5a05SMika Westerberg
413e23e5a05SMika Westerberg switch (optype) {
414e23e5a05SMika Westerberg case OPTYPE_WRITE_NO_ADDR:
415e23e5a05SMika Westerberg case OPTYPE_WRITE_WITH_ADDR:
416e23e5a05SMika Westerberg /* Pick matching preopcode for the atomic sequence */
417e23e5a05SMika Westerberg preop = readw(ispi->sregs + PREOP_OPTYPE);
418e23e5a05SMika Westerberg if ((preop & 0xff) == atomic_preopcode)
419e23e5a05SMika Westerberg ; /* Do nothing */
420e23e5a05SMika Westerberg else if ((preop >> 8) == atomic_preopcode)
421e23e5a05SMika Westerberg val |= SSFSTS_CTL_SPOP;
422e23e5a05SMika Westerberg else
423e23e5a05SMika Westerberg return -EINVAL;
424e23e5a05SMika Westerberg
425e23e5a05SMika Westerberg /* Enable atomic sequence */
426e23e5a05SMika Westerberg val |= SSFSTS_CTL_ACS;
427e23e5a05SMika Westerberg break;
428e23e5a05SMika Westerberg
429e23e5a05SMika Westerberg default:
430e23e5a05SMika Westerberg return -EINVAL;
431e23e5a05SMika Westerberg }
432e23e5a05SMika Westerberg }
433e23e5a05SMika Westerberg writel(val, ispi->sregs + SSFSTS_CTL);
434e23e5a05SMika Westerberg
435e23e5a05SMika Westerberg ret = intel_spi_wait_sw_busy(ispi);
436e23e5a05SMika Westerberg if (ret)
437e23e5a05SMika Westerberg return ret;
438e23e5a05SMika Westerberg
439e23e5a05SMika Westerberg status = readl(ispi->sregs + SSFSTS_CTL);
440e23e5a05SMika Westerberg if (status & SSFSTS_CTL_FCERR)
441e23e5a05SMika Westerberg return -EIO;
442e23e5a05SMika Westerberg else if (status & SSFSTS_CTL_AEL)
443e23e5a05SMika Westerberg return -EACCES;
444e23e5a05SMika Westerberg
445e23e5a05SMika Westerberg return 0;
446e23e5a05SMika Westerberg }
447e23e5a05SMika Westerberg
intel_spi_chip_addr(const struct intel_spi * ispi,const struct spi_mem * mem)4483f03c618SMika Westerberg static u32 intel_spi_chip_addr(const struct intel_spi *ispi,
4493f03c618SMika Westerberg const struct spi_mem *mem)
4503f03c618SMika Westerberg {
4513f03c618SMika Westerberg /* Pick up the correct start address */
4523f03c618SMika Westerberg if (!mem)
4533f03c618SMika Westerberg return 0;
4549e264f3fSAmit Kumar Mahapatra via Alsa-devel return (spi_get_chipselect(mem->spi, 0) == 1) ? ispi->chip0_size : 0;
4553f03c618SMika Westerberg }
4563f03c618SMika Westerberg
intel_spi_read_reg(struct intel_spi * ispi,const struct spi_mem * mem,const struct intel_spi_mem_op * iop,const struct spi_mem_op * op)4573f03c618SMika Westerberg static int intel_spi_read_reg(struct intel_spi *ispi, const struct spi_mem *mem,
458e23e5a05SMika Westerberg const struct intel_spi_mem_op *iop,
459e23e5a05SMika Westerberg const struct spi_mem_op *op)
460e23e5a05SMika Westerberg {
46143f173e7SMika Westerberg u32 addr = intel_spi_chip_addr(ispi, mem) + op->addr.val;
462e23e5a05SMika Westerberg size_t nbytes = op->data.nbytes;
463e23e5a05SMika Westerberg u8 opcode = op->cmd.opcode;
464e23e5a05SMika Westerberg int ret;
465e23e5a05SMika Westerberg
46643f173e7SMika Westerberg writel(addr, ispi->base + FADDR);
467e23e5a05SMika Westerberg
468e23e5a05SMika Westerberg if (ispi->swseq_reg)
469e23e5a05SMika Westerberg ret = intel_spi_sw_cycle(ispi, opcode, nbytes,
470e23e5a05SMika Westerberg OPTYPE_READ_NO_ADDR);
471e23e5a05SMika Westerberg else
472f73f6bd2SMika Westerberg ret = intel_spi_hw_cycle(ispi, iop, nbytes);
473e23e5a05SMika Westerberg
474e23e5a05SMika Westerberg if (ret)
475e23e5a05SMika Westerberg return ret;
476e23e5a05SMika Westerberg
477e23e5a05SMika Westerberg return intel_spi_read_block(ispi, op->data.buf.in, nbytes);
478e23e5a05SMika Westerberg }
479e23e5a05SMika Westerberg
intel_spi_write_reg(struct intel_spi * ispi,const struct spi_mem * mem,const struct intel_spi_mem_op * iop,const struct spi_mem_op * op)4803f03c618SMika Westerberg static int intel_spi_write_reg(struct intel_spi *ispi, const struct spi_mem *mem,
481e23e5a05SMika Westerberg const struct intel_spi_mem_op *iop,
482e23e5a05SMika Westerberg const struct spi_mem_op *op)
483e23e5a05SMika Westerberg {
48443f173e7SMika Westerberg u32 addr = intel_spi_chip_addr(ispi, mem) + op->addr.val;
485e23e5a05SMika Westerberg size_t nbytes = op->data.nbytes;
486e23e5a05SMika Westerberg u8 opcode = op->cmd.opcode;
487e23e5a05SMika Westerberg int ret;
488e23e5a05SMika Westerberg
489e23e5a05SMika Westerberg /*
490e23e5a05SMika Westerberg * This is handled with atomic operation and preop code in Intel
491e23e5a05SMika Westerberg * controller so we only verify that it is available. If the
492e23e5a05SMika Westerberg * controller is not locked, program the opcode to the PREOP
493e23e5a05SMika Westerberg * register for later use.
494e23e5a05SMika Westerberg *
495e23e5a05SMika Westerberg * When hardware sequencer is used there is no need to program
496e23e5a05SMika Westerberg * any opcodes (it handles them automatically as part of a command).
497e23e5a05SMika Westerberg */
498e23e5a05SMika Westerberg if (opcode == SPINOR_OP_WREN) {
499e23e5a05SMika Westerberg u16 preop;
500e23e5a05SMika Westerberg
501e23e5a05SMika Westerberg if (!ispi->swseq_reg)
502e23e5a05SMika Westerberg return 0;
503e23e5a05SMika Westerberg
504e23e5a05SMika Westerberg preop = readw(ispi->sregs + PREOP_OPTYPE);
505e23e5a05SMika Westerberg if ((preop & 0xff) != opcode && (preop >> 8) != opcode) {
506e23e5a05SMika Westerberg if (ispi->locked)
507e23e5a05SMika Westerberg return -EINVAL;
508e23e5a05SMika Westerberg writel(opcode, ispi->sregs + PREOP_OPTYPE);
509e23e5a05SMika Westerberg }
510e23e5a05SMika Westerberg
511e23e5a05SMika Westerberg /*
512e23e5a05SMika Westerberg * This enables atomic sequence on next SW sycle. Will
513e23e5a05SMika Westerberg * be cleared after next operation.
514e23e5a05SMika Westerberg */
515e23e5a05SMika Westerberg ispi->atomic_preopcode = opcode;
516e23e5a05SMika Westerberg return 0;
517e23e5a05SMika Westerberg }
518e23e5a05SMika Westerberg
519e23e5a05SMika Westerberg /*
520e23e5a05SMika Westerberg * We hope that HW sequencer will do the right thing automatically and
521e23e5a05SMika Westerberg * with the SW sequencer we cannot use preopcode anyway, so just ignore
522e23e5a05SMika Westerberg * the Write Disable operation and pretend it was completed
523e23e5a05SMika Westerberg * successfully.
524e23e5a05SMika Westerberg */
525e23e5a05SMika Westerberg if (opcode == SPINOR_OP_WRDI)
526e23e5a05SMika Westerberg return 0;
527e23e5a05SMika Westerberg
52843f173e7SMika Westerberg writel(addr, ispi->base + FADDR);
529e23e5a05SMika Westerberg
530e23e5a05SMika Westerberg /* Write the value beforehand */
531e23e5a05SMika Westerberg ret = intel_spi_write_block(ispi, op->data.buf.out, nbytes);
532e23e5a05SMika Westerberg if (ret)
533e23e5a05SMika Westerberg return ret;
534e23e5a05SMika Westerberg
535e23e5a05SMika Westerberg if (ispi->swseq_reg)
536e23e5a05SMika Westerberg return intel_spi_sw_cycle(ispi, opcode, nbytes,
537e23e5a05SMika Westerberg OPTYPE_WRITE_NO_ADDR);
538f73f6bd2SMika Westerberg return intel_spi_hw_cycle(ispi, iop, nbytes);
539e23e5a05SMika Westerberg }
540e23e5a05SMika Westerberg
intel_spi_read(struct intel_spi * ispi,const struct spi_mem * mem,const struct intel_spi_mem_op * iop,const struct spi_mem_op * op)5413f03c618SMika Westerberg static int intel_spi_read(struct intel_spi *ispi, const struct spi_mem *mem,
542e23e5a05SMika Westerberg const struct intel_spi_mem_op *iop,
543e23e5a05SMika Westerberg const struct spi_mem_op *op)
544e23e5a05SMika Westerberg {
5453f03c618SMika Westerberg u32 addr = intel_spi_chip_addr(ispi, mem) + op->addr.val;
546e23e5a05SMika Westerberg size_t block_size, nbytes = op->data.nbytes;
5473f03c618SMika Westerberg void *read_buf = op->data.buf.in;
548e23e5a05SMika Westerberg u32 val, status;
549e23e5a05SMika Westerberg int ret;
550e23e5a05SMika Westerberg
551e23e5a05SMika Westerberg /*
552e23e5a05SMika Westerberg * Atomic sequence is not expected with HW sequencer reads. Make
553e23e5a05SMika Westerberg * sure it is cleared regardless.
554e23e5a05SMika Westerberg */
555e23e5a05SMika Westerberg if (WARN_ON_ONCE(ispi->atomic_preopcode))
556e23e5a05SMika Westerberg ispi->atomic_preopcode = 0;
557e23e5a05SMika Westerberg
558e23e5a05SMika Westerberg while (nbytes > 0) {
559e23e5a05SMika Westerberg block_size = min_t(size_t, nbytes, INTEL_SPI_FIFO_SZ);
560e23e5a05SMika Westerberg
561e23e5a05SMika Westerberg /* Read cannot cross 4K boundary */
562e23e5a05SMika Westerberg block_size = min_t(loff_t, addr + block_size,
563e23e5a05SMika Westerberg round_up(addr + 1, SZ_4K)) - addr;
564e23e5a05SMika Westerberg
565e23e5a05SMika Westerberg writel(addr, ispi->base + FADDR);
566e23e5a05SMika Westerberg
567e23e5a05SMika Westerberg val = readl(ispi->base + HSFSTS_CTL);
568e23e5a05SMika Westerberg val &= ~(HSFSTS_CTL_FDBC_MASK | HSFSTS_CTL_FCYCLE_MASK);
569e23e5a05SMika Westerberg val |= HSFSTS_CTL_AEL | HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
570e23e5a05SMika Westerberg val |= (block_size - 1) << HSFSTS_CTL_FDBC_SHIFT;
571e23e5a05SMika Westerberg val |= HSFSTS_CTL_FCYCLE_READ;
572e23e5a05SMika Westerberg val |= HSFSTS_CTL_FGO;
573e23e5a05SMika Westerberg writel(val, ispi->base + HSFSTS_CTL);
574e23e5a05SMika Westerberg
575e23e5a05SMika Westerberg ret = intel_spi_wait_hw_busy(ispi);
576e23e5a05SMika Westerberg if (ret)
577e23e5a05SMika Westerberg return ret;
578e23e5a05SMika Westerberg
579e23e5a05SMika Westerberg status = readl(ispi->base + HSFSTS_CTL);
580e23e5a05SMika Westerberg if (status & HSFSTS_CTL_FCERR)
581e23e5a05SMika Westerberg ret = -EIO;
582e23e5a05SMika Westerberg else if (status & HSFSTS_CTL_AEL)
583e23e5a05SMika Westerberg ret = -EACCES;
584e23e5a05SMika Westerberg
585e23e5a05SMika Westerberg if (ret < 0) {
586e23e5a05SMika Westerberg dev_err(ispi->dev, "read error: %x: %#x\n", addr, status);
587e23e5a05SMika Westerberg return ret;
588e23e5a05SMika Westerberg }
589e23e5a05SMika Westerberg
590e23e5a05SMika Westerberg ret = intel_spi_read_block(ispi, read_buf, block_size);
591e23e5a05SMika Westerberg if (ret)
592e23e5a05SMika Westerberg return ret;
593e23e5a05SMika Westerberg
594e23e5a05SMika Westerberg nbytes -= block_size;
595e23e5a05SMika Westerberg addr += block_size;
596e23e5a05SMika Westerberg read_buf += block_size;
597e23e5a05SMika Westerberg }
598e23e5a05SMika Westerberg
599e23e5a05SMika Westerberg return 0;
600e23e5a05SMika Westerberg }
601e23e5a05SMika Westerberg
intel_spi_write(struct intel_spi * ispi,const struct spi_mem * mem,const struct intel_spi_mem_op * iop,const struct spi_mem_op * op)6023f03c618SMika Westerberg static int intel_spi_write(struct intel_spi *ispi, const struct spi_mem *mem,
603e23e5a05SMika Westerberg const struct intel_spi_mem_op *iop,
604e23e5a05SMika Westerberg const struct spi_mem_op *op)
605e23e5a05SMika Westerberg {
6063f03c618SMika Westerberg u32 addr = intel_spi_chip_addr(ispi, mem) + op->addr.val;
607e23e5a05SMika Westerberg size_t block_size, nbytes = op->data.nbytes;
608e23e5a05SMika Westerberg const void *write_buf = op->data.buf.out;
609e23e5a05SMika Westerberg u32 val, status;
610e23e5a05SMika Westerberg int ret;
611e23e5a05SMika Westerberg
612e23e5a05SMika Westerberg /* Not needed with HW sequencer write, make sure it is cleared */
613e23e5a05SMika Westerberg ispi->atomic_preopcode = 0;
614e23e5a05SMika Westerberg
615e23e5a05SMika Westerberg while (nbytes > 0) {
616e23e5a05SMika Westerberg block_size = min_t(size_t, nbytes, INTEL_SPI_FIFO_SZ);
617e23e5a05SMika Westerberg
618e23e5a05SMika Westerberg /* Write cannot cross 4K boundary */
619e23e5a05SMika Westerberg block_size = min_t(loff_t, addr + block_size,
620e23e5a05SMika Westerberg round_up(addr + 1, SZ_4K)) - addr;
621e23e5a05SMika Westerberg
622e23e5a05SMika Westerberg writel(addr, ispi->base + FADDR);
623e23e5a05SMika Westerberg
624e23e5a05SMika Westerberg val = readl(ispi->base + HSFSTS_CTL);
625e23e5a05SMika Westerberg val &= ~(HSFSTS_CTL_FDBC_MASK | HSFSTS_CTL_FCYCLE_MASK);
626e23e5a05SMika Westerberg val |= HSFSTS_CTL_AEL | HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
627e23e5a05SMika Westerberg val |= (block_size - 1) << HSFSTS_CTL_FDBC_SHIFT;
628e23e5a05SMika Westerberg val |= HSFSTS_CTL_FCYCLE_WRITE;
629e23e5a05SMika Westerberg
630e23e5a05SMika Westerberg ret = intel_spi_write_block(ispi, write_buf, block_size);
631e23e5a05SMika Westerberg if (ret) {
632e23e5a05SMika Westerberg dev_err(ispi->dev, "failed to write block\n");
633e23e5a05SMika Westerberg return ret;
634e23e5a05SMika Westerberg }
635e23e5a05SMika Westerberg
636e23e5a05SMika Westerberg /* Start the write now */
637e23e5a05SMika Westerberg val |= HSFSTS_CTL_FGO;
638e23e5a05SMika Westerberg writel(val, ispi->base + HSFSTS_CTL);
639e23e5a05SMika Westerberg
640e23e5a05SMika Westerberg ret = intel_spi_wait_hw_busy(ispi);
641e23e5a05SMika Westerberg if (ret) {
642e23e5a05SMika Westerberg dev_err(ispi->dev, "timeout\n");
643e23e5a05SMika Westerberg return ret;
644e23e5a05SMika Westerberg }
645e23e5a05SMika Westerberg
646e23e5a05SMika Westerberg status = readl(ispi->base + HSFSTS_CTL);
647e23e5a05SMika Westerberg if (status & HSFSTS_CTL_FCERR)
648e23e5a05SMika Westerberg ret = -EIO;
649e23e5a05SMika Westerberg else if (status & HSFSTS_CTL_AEL)
650e23e5a05SMika Westerberg ret = -EACCES;
651e23e5a05SMika Westerberg
652e23e5a05SMika Westerberg if (ret < 0) {
653e23e5a05SMika Westerberg dev_err(ispi->dev, "write error: %x: %#x\n", addr, status);
654e23e5a05SMika Westerberg return ret;
655e23e5a05SMika Westerberg }
656e23e5a05SMika Westerberg
657e23e5a05SMika Westerberg nbytes -= block_size;
658e23e5a05SMika Westerberg addr += block_size;
659e23e5a05SMika Westerberg write_buf += block_size;
660e23e5a05SMika Westerberg }
661e23e5a05SMika Westerberg
662e23e5a05SMika Westerberg return 0;
663e23e5a05SMika Westerberg }
664e23e5a05SMika Westerberg
intel_spi_erase(struct intel_spi * ispi,const struct spi_mem * mem,const struct intel_spi_mem_op * iop,const struct spi_mem_op * op)6653f03c618SMika Westerberg static int intel_spi_erase(struct intel_spi *ispi, const struct spi_mem *mem,
666e23e5a05SMika Westerberg const struct intel_spi_mem_op *iop,
667e23e5a05SMika Westerberg const struct spi_mem_op *op)
668e23e5a05SMika Westerberg {
6693f03c618SMika Westerberg u32 addr = intel_spi_chip_addr(ispi, mem) + op->addr.val;
670e23e5a05SMika Westerberg u8 opcode = op->cmd.opcode;
671e23e5a05SMika Westerberg u32 val, status;
672e23e5a05SMika Westerberg int ret;
673e23e5a05SMika Westerberg
674e23e5a05SMika Westerberg writel(addr, ispi->base + FADDR);
675e23e5a05SMika Westerberg
676e23e5a05SMika Westerberg if (ispi->swseq_erase)
677e23e5a05SMika Westerberg return intel_spi_sw_cycle(ispi, opcode, 0,
678e23e5a05SMika Westerberg OPTYPE_WRITE_WITH_ADDR);
679e23e5a05SMika Westerberg
680e23e5a05SMika Westerberg /* Not needed with HW sequencer erase, make sure it is cleared */
681e23e5a05SMika Westerberg ispi->atomic_preopcode = 0;
682e23e5a05SMika Westerberg
683e23e5a05SMika Westerberg val = readl(ispi->base + HSFSTS_CTL);
684e23e5a05SMika Westerberg val &= ~(HSFSTS_CTL_FDBC_MASK | HSFSTS_CTL_FCYCLE_MASK);
685e23e5a05SMika Westerberg val |= HSFSTS_CTL_AEL | HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
686e23e5a05SMika Westerberg val |= HSFSTS_CTL_FGO;
687e23e5a05SMika Westerberg val |= iop->replacement_op;
688e23e5a05SMika Westerberg writel(val, ispi->base + HSFSTS_CTL);
689e23e5a05SMika Westerberg
690e23e5a05SMika Westerberg ret = intel_spi_wait_hw_busy(ispi);
691e23e5a05SMika Westerberg if (ret)
692e23e5a05SMika Westerberg return ret;
693e23e5a05SMika Westerberg
694e23e5a05SMika Westerberg status = readl(ispi->base + HSFSTS_CTL);
695e23e5a05SMika Westerberg if (status & HSFSTS_CTL_FCERR)
696e23e5a05SMika Westerberg return -EIO;
697e23e5a05SMika Westerberg if (status & HSFSTS_CTL_AEL)
698e23e5a05SMika Westerberg return -EACCES;
699e23e5a05SMika Westerberg
700e23e5a05SMika Westerberg return 0;
701e23e5a05SMika Westerberg }
702e23e5a05SMika Westerberg
intel_spi_adjust_op_size(struct spi_mem * mem,struct spi_mem_op * op)7038a9a784fSMika Westerberg static int intel_spi_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
7048a9a784fSMika Westerberg {
7058a9a784fSMika Westerberg op->data.nbytes = clamp_val(op->data.nbytes, 0, INTEL_SPI_FIFO_SZ);
7068a9a784fSMika Westerberg return 0;
7078a9a784fSMika Westerberg }
7088a9a784fSMika Westerberg
intel_spi_cmp_mem_op(const struct intel_spi_mem_op * iop,const struct spi_mem_op * op)709e23e5a05SMika Westerberg static bool intel_spi_cmp_mem_op(const struct intel_spi_mem_op *iop,
710e23e5a05SMika Westerberg const struct spi_mem_op *op)
711e23e5a05SMika Westerberg {
712e23e5a05SMika Westerberg if (iop->mem_op.cmd.nbytes != op->cmd.nbytes ||
713e23e5a05SMika Westerberg iop->mem_op.cmd.buswidth != op->cmd.buswidth ||
714e23e5a05SMika Westerberg iop->mem_op.cmd.dtr != op->cmd.dtr ||
715e23e5a05SMika Westerberg iop->mem_op.cmd.opcode != op->cmd.opcode)
716e23e5a05SMika Westerberg return false;
717e23e5a05SMika Westerberg
718e23e5a05SMika Westerberg if (iop->mem_op.addr.nbytes != op->addr.nbytes ||
719e23e5a05SMika Westerberg iop->mem_op.addr.dtr != op->addr.dtr)
720e23e5a05SMika Westerberg return false;
721e23e5a05SMika Westerberg
722e23e5a05SMika Westerberg if (iop->mem_op.data.dir != op->data.dir ||
723e23e5a05SMika Westerberg iop->mem_op.data.dtr != op->data.dtr)
724e23e5a05SMika Westerberg return false;
725e23e5a05SMika Westerberg
726e23e5a05SMika Westerberg if (iop->mem_op.data.dir != SPI_MEM_NO_DATA) {
727e23e5a05SMika Westerberg if (iop->mem_op.data.buswidth != op->data.buswidth)
728e23e5a05SMika Westerberg return false;
729e23e5a05SMika Westerberg }
730e23e5a05SMika Westerberg
731e23e5a05SMika Westerberg return true;
732e23e5a05SMika Westerberg }
733e23e5a05SMika Westerberg
734e23e5a05SMika Westerberg static const struct intel_spi_mem_op *
intel_spi_match_mem_op(struct intel_spi * ispi,const struct spi_mem_op * op)735e23e5a05SMika Westerberg intel_spi_match_mem_op(struct intel_spi *ispi, const struct spi_mem_op *op)
736e23e5a05SMika Westerberg {
737e23e5a05SMika Westerberg const struct intel_spi_mem_op *iop;
738e23e5a05SMika Westerberg
739e23e5a05SMika Westerberg for (iop = ispi->mem_ops; iop->mem_op.cmd.opcode; iop++) {
740e23e5a05SMika Westerberg if (intel_spi_cmp_mem_op(iop, op))
741e23e5a05SMika Westerberg break;
742e23e5a05SMika Westerberg }
743e23e5a05SMika Westerberg
744e23e5a05SMika Westerberg return iop->mem_op.cmd.opcode ? iop : NULL;
745e23e5a05SMika Westerberg }
746e23e5a05SMika Westerberg
intel_spi_supports_mem_op(struct spi_mem * mem,const struct spi_mem_op * op)747e23e5a05SMika Westerberg static bool intel_spi_supports_mem_op(struct spi_mem *mem,
748e23e5a05SMika Westerberg const struct spi_mem_op *op)
749e23e5a05SMika Westerberg {
750*5fa0ade1SYang Yingliang struct intel_spi *ispi = spi_controller_get_devdata(mem->spi->controller);
751e23e5a05SMika Westerberg const struct intel_spi_mem_op *iop;
752e23e5a05SMika Westerberg
753e23e5a05SMika Westerberg iop = intel_spi_match_mem_op(ispi, op);
754e23e5a05SMika Westerberg if (!iop) {
755e23e5a05SMika Westerberg dev_dbg(ispi->dev, "%#x not supported\n", op->cmd.opcode);
756e23e5a05SMika Westerberg return false;
757e23e5a05SMika Westerberg }
758e23e5a05SMika Westerberg
759e23e5a05SMika Westerberg /*
760e23e5a05SMika Westerberg * For software sequencer check that the opcode is actually
761e23e5a05SMika Westerberg * present in the opmenu if it is locked.
762e23e5a05SMika Westerberg */
763e23e5a05SMika Westerberg if (ispi->swseq_reg && ispi->locked) {
764e23e5a05SMika Westerberg int i;
765e23e5a05SMika Westerberg
766e23e5a05SMika Westerberg /* Check if it is in the locked opcodes list */
767e23e5a05SMika Westerberg for (i = 0; i < ARRAY_SIZE(ispi->opcodes); i++) {
768e23e5a05SMika Westerberg if (ispi->opcodes[i] == op->cmd.opcode)
769e23e5a05SMika Westerberg return true;
770e23e5a05SMika Westerberg }
771e23e5a05SMika Westerberg
772e23e5a05SMika Westerberg dev_dbg(ispi->dev, "%#x not supported\n", op->cmd.opcode);
773e23e5a05SMika Westerberg return false;
774e23e5a05SMika Westerberg }
775e23e5a05SMika Westerberg
776e23e5a05SMika Westerberg return true;
777e23e5a05SMika Westerberg }
778e23e5a05SMika Westerberg
intel_spi_exec_mem_op(struct spi_mem * mem,const struct spi_mem_op * op)779e23e5a05SMika Westerberg static int intel_spi_exec_mem_op(struct spi_mem *mem, const struct spi_mem_op *op)
780e23e5a05SMika Westerberg {
781*5fa0ade1SYang Yingliang struct intel_spi *ispi = spi_controller_get_devdata(mem->spi->controller);
782e23e5a05SMika Westerberg const struct intel_spi_mem_op *iop;
783e23e5a05SMika Westerberg
784e23e5a05SMika Westerberg iop = intel_spi_match_mem_op(ispi, op);
785e23e5a05SMika Westerberg if (!iop)
786e23e5a05SMika Westerberg return -EOPNOTSUPP;
787e23e5a05SMika Westerberg
7883f03c618SMika Westerberg return iop->exec_op(ispi, mem, iop, op);
789e23e5a05SMika Westerberg }
790e23e5a05SMika Westerberg
intel_spi_get_name(struct spi_mem * mem)791e23e5a05SMika Westerberg static const char *intel_spi_get_name(struct spi_mem *mem)
792e23e5a05SMika Westerberg {
793*5fa0ade1SYang Yingliang const struct intel_spi *ispi = spi_controller_get_devdata(mem->spi->controller);
794e23e5a05SMika Westerberg
795e23e5a05SMika Westerberg /*
796e23e5a05SMika Westerberg * Return name of the flash controller device to be compatible
797e23e5a05SMika Westerberg * with the MTD version.
798e23e5a05SMika Westerberg */
799e23e5a05SMika Westerberg return dev_name(ispi->dev);
800e23e5a05SMika Westerberg }
801e23e5a05SMika Westerberg
intel_spi_dirmap_create(struct spi_mem_dirmap_desc * desc)802c2b5a40cSMika Westerberg static int intel_spi_dirmap_create(struct spi_mem_dirmap_desc *desc)
803c2b5a40cSMika Westerberg {
804*5fa0ade1SYang Yingliang struct intel_spi *ispi = spi_controller_get_devdata(desc->mem->spi->controller);
805c2b5a40cSMika Westerberg const struct intel_spi_mem_op *iop;
806c2b5a40cSMika Westerberg
807c2b5a40cSMika Westerberg iop = intel_spi_match_mem_op(ispi, &desc->info.op_tmpl);
808c2b5a40cSMika Westerberg if (!iop)
809c2b5a40cSMika Westerberg return -EOPNOTSUPP;
810c2b5a40cSMika Westerberg
811c2b5a40cSMika Westerberg desc->priv = (void *)iop;
812c2b5a40cSMika Westerberg return 0;
813c2b5a40cSMika Westerberg }
814c2b5a40cSMika Westerberg
intel_spi_dirmap_read(struct spi_mem_dirmap_desc * desc,u64 offs,size_t len,void * buf)815c2b5a40cSMika Westerberg static ssize_t intel_spi_dirmap_read(struct spi_mem_dirmap_desc *desc, u64 offs,
816c2b5a40cSMika Westerberg size_t len, void *buf)
817c2b5a40cSMika Westerberg {
818*5fa0ade1SYang Yingliang struct intel_spi *ispi = spi_controller_get_devdata(desc->mem->spi->controller);
819c2b5a40cSMika Westerberg const struct intel_spi_mem_op *iop = desc->priv;
820c2b5a40cSMika Westerberg struct spi_mem_op op = desc->info.op_tmpl;
821c2b5a40cSMika Westerberg int ret;
822c2b5a40cSMika Westerberg
823c2b5a40cSMika Westerberg /* Fill in the gaps */
824c2b5a40cSMika Westerberg op.addr.val = offs;
825c2b5a40cSMika Westerberg op.data.nbytes = len;
826c2b5a40cSMika Westerberg op.data.buf.in = buf;
827c2b5a40cSMika Westerberg
8283f03c618SMika Westerberg ret = iop->exec_op(ispi, desc->mem, iop, &op);
829c2b5a40cSMika Westerberg return ret ? ret : len;
830c2b5a40cSMika Westerberg }
831c2b5a40cSMika Westerberg
intel_spi_dirmap_write(struct spi_mem_dirmap_desc * desc,u64 offs,size_t len,const void * buf)832c2b5a40cSMika Westerberg static ssize_t intel_spi_dirmap_write(struct spi_mem_dirmap_desc *desc, u64 offs,
833c2b5a40cSMika Westerberg size_t len, const void *buf)
834c2b5a40cSMika Westerberg {
835*5fa0ade1SYang Yingliang struct intel_spi *ispi = spi_controller_get_devdata(desc->mem->spi->controller);
836c2b5a40cSMika Westerberg const struct intel_spi_mem_op *iop = desc->priv;
837c2b5a40cSMika Westerberg struct spi_mem_op op = desc->info.op_tmpl;
838c2b5a40cSMika Westerberg int ret;
839c2b5a40cSMika Westerberg
840c2b5a40cSMika Westerberg op.addr.val = offs;
841c2b5a40cSMika Westerberg op.data.nbytes = len;
842c2b5a40cSMika Westerberg op.data.buf.out = buf;
843c2b5a40cSMika Westerberg
8443f03c618SMika Westerberg ret = iop->exec_op(ispi, desc->mem, iop, &op);
845c2b5a40cSMika Westerberg return ret ? ret : len;
846c2b5a40cSMika Westerberg }
847c2b5a40cSMika Westerberg
848e23e5a05SMika Westerberg static const struct spi_controller_mem_ops intel_spi_mem_ops = {
8498a9a784fSMika Westerberg .adjust_op_size = intel_spi_adjust_op_size,
850e23e5a05SMika Westerberg .supports_op = intel_spi_supports_mem_op,
851e23e5a05SMika Westerberg .exec_op = intel_spi_exec_mem_op,
852e23e5a05SMika Westerberg .get_name = intel_spi_get_name,
853c2b5a40cSMika Westerberg .dirmap_create = intel_spi_dirmap_create,
854c2b5a40cSMika Westerberg .dirmap_read = intel_spi_dirmap_read,
855c2b5a40cSMika Westerberg .dirmap_write = intel_spi_dirmap_write,
856e23e5a05SMika Westerberg };
857e23e5a05SMika Westerberg
858e23e5a05SMika Westerberg #define INTEL_SPI_OP_ADDR(__nbytes) \
859e23e5a05SMika Westerberg { \
860e23e5a05SMika Westerberg .nbytes = __nbytes, \
861e23e5a05SMika Westerberg }
862e23e5a05SMika Westerberg
863e23e5a05SMika Westerberg #define INTEL_SPI_OP_NO_DATA \
864e23e5a05SMika Westerberg { \
865e23e5a05SMika Westerberg .dir = SPI_MEM_NO_DATA, \
866e23e5a05SMika Westerberg }
867e23e5a05SMika Westerberg
868e23e5a05SMika Westerberg #define INTEL_SPI_OP_DATA_IN(__buswidth) \
869e23e5a05SMika Westerberg { \
870e23e5a05SMika Westerberg .dir = SPI_MEM_DATA_IN, \
871e23e5a05SMika Westerberg .buswidth = __buswidth, \
872e23e5a05SMika Westerberg }
873e23e5a05SMika Westerberg
874e23e5a05SMika Westerberg #define INTEL_SPI_OP_DATA_OUT(__buswidth) \
875e23e5a05SMika Westerberg { \
876e23e5a05SMika Westerberg .dir = SPI_MEM_DATA_OUT, \
877e23e5a05SMika Westerberg .buswidth = __buswidth, \
878e23e5a05SMika Westerberg }
879e23e5a05SMika Westerberg
880e23e5a05SMika Westerberg #define INTEL_SPI_MEM_OP(__cmd, __addr, __data, __exec_op) \
881e23e5a05SMika Westerberg { \
882e23e5a05SMika Westerberg .mem_op = { \
883e23e5a05SMika Westerberg .cmd = __cmd, \
884e23e5a05SMika Westerberg .addr = __addr, \
885e23e5a05SMika Westerberg .data = __data, \
886e23e5a05SMika Westerberg }, \
887e23e5a05SMika Westerberg .exec_op = __exec_op, \
888e23e5a05SMika Westerberg }
889e23e5a05SMika Westerberg
890e23e5a05SMika Westerberg #define INTEL_SPI_MEM_OP_REPL(__cmd, __addr, __data, __exec_op, __repl) \
891e23e5a05SMika Westerberg { \
892e23e5a05SMika Westerberg .mem_op = { \
893e23e5a05SMika Westerberg .cmd = __cmd, \
894e23e5a05SMika Westerberg .addr = __addr, \
895e23e5a05SMika Westerberg .data = __data, \
896e23e5a05SMika Westerberg }, \
897e23e5a05SMika Westerberg .exec_op = __exec_op, \
898e23e5a05SMika Westerberg .replacement_op = __repl, \
899e23e5a05SMika Westerberg }
900e23e5a05SMika Westerberg
901e23e5a05SMika Westerberg /*
902e23e5a05SMika Westerberg * The controller handles pretty much everything internally based on the
903e23e5a05SMika Westerberg * SFDP data but we want to make sure we only support the operations
904e23e5a05SMika Westerberg * actually possible. Only check buswidth and transfer direction, the
905e23e5a05SMika Westerberg * core validates data.
906e23e5a05SMika Westerberg */
907e23e5a05SMika Westerberg #define INTEL_SPI_GENERIC_OPS \
908e23e5a05SMika Westerberg /* Status register operations */ \
909f73f6bd2SMika Westerberg INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_RDID, 1), \
910e23e5a05SMika Westerberg SPI_MEM_OP_NO_ADDR, \
911e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(1), \
912f73f6bd2SMika Westerberg intel_spi_read_reg, \
913f73f6bd2SMika Westerberg HSFSTS_CTL_FCYCLE_RDID), \
914f73f6bd2SMika Westerberg INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_RDSR, 1), \
915e23e5a05SMika Westerberg SPI_MEM_OP_NO_ADDR, \
916e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(1), \
917f73f6bd2SMika Westerberg intel_spi_read_reg, \
918f73f6bd2SMika Westerberg HSFSTS_CTL_FCYCLE_RDSR), \
919f73f6bd2SMika Westerberg INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_WRSR, 1), \
920e23e5a05SMika Westerberg SPI_MEM_OP_NO_ADDR, \
921e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_OUT(1), \
922f73f6bd2SMika Westerberg intel_spi_write_reg, \
923f73f6bd2SMika Westerberg HSFSTS_CTL_FCYCLE_WRSR), \
924ec4a04aaSMika Westerberg INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_RDSFDP, 1), \
925ec4a04aaSMika Westerberg INTEL_SPI_OP_ADDR(3), \
926ec4a04aaSMika Westerberg INTEL_SPI_OP_DATA_IN(1), \
927ec4a04aaSMika Westerberg intel_spi_read_reg, \
928ec4a04aaSMika Westerberg HSFSTS_CTL_FCYCLE_RDSFDP), \
929e23e5a05SMika Westerberg /* Normal read */ \
930e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ, 1), \
931e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(3), \
932e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(1), \
933e23e5a05SMika Westerberg intel_spi_read), \
934e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ, 1), \
935e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(3), \
936e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(2), \
937e23e5a05SMika Westerberg intel_spi_read), \
938e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ, 1), \
939e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(3), \
940e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(4), \
941e23e5a05SMika Westerberg intel_spi_read), \
942e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ, 1), \
943e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
944e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(1), \
945e23e5a05SMika Westerberg intel_spi_read), \
946e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ, 1), \
947e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
948e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(2), \
949e23e5a05SMika Westerberg intel_spi_read), \
950e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ, 1), \
951e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
952e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(4), \
953e23e5a05SMika Westerberg intel_spi_read), \
954e23e5a05SMika Westerberg /* Fast read */ \
955e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST, 1), \
956e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(3), \
957e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(1), \
958e23e5a05SMika Westerberg intel_spi_read), \
959e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST, 1), \
960e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(3), \
961e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(2), \
962e23e5a05SMika Westerberg intel_spi_read), \
963e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST, 1), \
964e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(3), \
965e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(4), \
966e23e5a05SMika Westerberg intel_spi_read), \
967e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST, 1), \
968e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
969e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(1), \
970e23e5a05SMika Westerberg intel_spi_read), \
971e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST, 1), \
972e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
973e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(2), \
974e23e5a05SMika Westerberg intel_spi_read), \
975e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST, 1), \
976e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
977e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(4), \
978e23e5a05SMika Westerberg intel_spi_read), \
979e23e5a05SMika Westerberg /* Read with 4-byte address opcode */ \
980e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_4B, 1), \
981e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
982e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(1), \
983e23e5a05SMika Westerberg intel_spi_read), \
984e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_4B, 1), \
985e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
986e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(2), \
987e23e5a05SMika Westerberg intel_spi_read), \
988e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_4B, 1), \
989e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
990e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(4), \
991e23e5a05SMika Westerberg intel_spi_read), \
992e23e5a05SMika Westerberg /* Fast read with 4-byte address opcode */ \
993e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST_4B, 1), \
994e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
995e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(1), \
996e23e5a05SMika Westerberg intel_spi_read), \
997e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST_4B, 1), \
998e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
999e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(2), \
1000e23e5a05SMika Westerberg intel_spi_read), \
1001e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ_FAST_4B, 1), \
1002e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
1003e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_IN(4), \
1004e23e5a05SMika Westerberg intel_spi_read), \
1005e23e5a05SMika Westerberg /* Write operations */ \
1006e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_PP, 1), \
1007e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(3), \
1008e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_OUT(1), \
1009e23e5a05SMika Westerberg intel_spi_write), \
1010e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_PP, 1), \
1011e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
1012e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_OUT(1), \
1013e23e5a05SMika Westerberg intel_spi_write), \
1014e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_PP_4B, 1), \
1015e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
1016e23e5a05SMika Westerberg INTEL_SPI_OP_DATA_OUT(1), \
1017e23e5a05SMika Westerberg intel_spi_write), \
1018e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WREN, 1), \
1019e23e5a05SMika Westerberg SPI_MEM_OP_NO_ADDR, \
1020e23e5a05SMika Westerberg SPI_MEM_OP_NO_DATA, \
1021e23e5a05SMika Westerberg intel_spi_write_reg), \
1022e23e5a05SMika Westerberg INTEL_SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRDI, 1), \
1023e23e5a05SMika Westerberg SPI_MEM_OP_NO_ADDR, \
1024e23e5a05SMika Westerberg SPI_MEM_OP_NO_DATA, \
1025e23e5a05SMika Westerberg intel_spi_write_reg), \
1026e23e5a05SMika Westerberg /* Erase operations */ \
1027e23e5a05SMika Westerberg INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_BE_4K, 1), \
1028e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(3), \
1029e23e5a05SMika Westerberg SPI_MEM_OP_NO_DATA, \
1030e23e5a05SMika Westerberg intel_spi_erase, \
1031e23e5a05SMika Westerberg HSFSTS_CTL_FCYCLE_ERASE), \
1032e23e5a05SMika Westerberg INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_BE_4K, 1), \
1033e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
1034e23e5a05SMika Westerberg SPI_MEM_OP_NO_DATA, \
1035e23e5a05SMika Westerberg intel_spi_erase, \
1036e23e5a05SMika Westerberg HSFSTS_CTL_FCYCLE_ERASE), \
1037e23e5a05SMika Westerberg INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_BE_4K_4B, 1), \
1038e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4), \
1039e23e5a05SMika Westerberg SPI_MEM_OP_NO_DATA, \
1040e23e5a05SMika Westerberg intel_spi_erase, \
1041e23e5a05SMika Westerberg HSFSTS_CTL_FCYCLE_ERASE) \
1042e23e5a05SMika Westerberg
1043e23e5a05SMika Westerberg static const struct intel_spi_mem_op generic_mem_ops[] = {
1044e23e5a05SMika Westerberg INTEL_SPI_GENERIC_OPS,
1045e23e5a05SMika Westerberg { },
1046e23e5a05SMika Westerberg };
1047e23e5a05SMika Westerberg
1048e23e5a05SMika Westerberg static const struct intel_spi_mem_op erase_64k_mem_ops[] = {
1049e23e5a05SMika Westerberg INTEL_SPI_GENERIC_OPS,
1050e23e5a05SMika Westerberg /* 64k sector erase operations */
1051e23e5a05SMika Westerberg INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_SE, 1),
1052e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(3),
1053e23e5a05SMika Westerberg SPI_MEM_OP_NO_DATA,
1054e23e5a05SMika Westerberg intel_spi_erase,
1055e23e5a05SMika Westerberg HSFSTS_CTL_FCYCLE_ERASE_64K),
1056e23e5a05SMika Westerberg INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_SE, 1),
1057e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4),
1058e23e5a05SMika Westerberg SPI_MEM_OP_NO_DATA,
1059e23e5a05SMika Westerberg intel_spi_erase,
1060e23e5a05SMika Westerberg HSFSTS_CTL_FCYCLE_ERASE_64K),
1061e23e5a05SMika Westerberg INTEL_SPI_MEM_OP_REPL(SPI_MEM_OP_CMD(SPINOR_OP_SE_4B, 1),
1062e23e5a05SMika Westerberg INTEL_SPI_OP_ADDR(4),
1063e23e5a05SMika Westerberg SPI_MEM_OP_NO_DATA,
1064e23e5a05SMika Westerberg intel_spi_erase,
1065e23e5a05SMika Westerberg HSFSTS_CTL_FCYCLE_ERASE_64K),
1066e23e5a05SMika Westerberg { },
1067e23e5a05SMika Westerberg };
1068e23e5a05SMika Westerberg
intel_spi_init(struct intel_spi * ispi)1069e23e5a05SMika Westerberg static int intel_spi_init(struct intel_spi *ispi)
1070e23e5a05SMika Westerberg {
1071e23e5a05SMika Westerberg u32 opmenu0, opmenu1, lvscc, uvscc, val;
1072e23e5a05SMika Westerberg bool erase_64k = false;
1073e23e5a05SMika Westerberg int i;
1074e23e5a05SMika Westerberg
1075e23e5a05SMika Westerberg switch (ispi->info->type) {
1076e23e5a05SMika Westerberg case INTEL_SPI_BYT:
1077e23e5a05SMika Westerberg ispi->sregs = ispi->base + BYT_SSFSTS_CTL;
1078e23e5a05SMika Westerberg ispi->pregs = ispi->base + BYT_PR;
1079e23e5a05SMika Westerberg ispi->nregions = BYT_FREG_NUM;
1080e23e5a05SMika Westerberg ispi->pr_num = BYT_PR_NUM;
1081e23e5a05SMika Westerberg ispi->swseq_reg = true;
1082e23e5a05SMika Westerberg break;
1083e23e5a05SMika Westerberg
1084e23e5a05SMika Westerberg case INTEL_SPI_LPT:
1085e23e5a05SMika Westerberg ispi->sregs = ispi->base + LPT_SSFSTS_CTL;
1086e23e5a05SMika Westerberg ispi->pregs = ispi->base + LPT_PR;
1087e23e5a05SMika Westerberg ispi->nregions = LPT_FREG_NUM;
1088e23e5a05SMika Westerberg ispi->pr_num = LPT_PR_NUM;
1089e23e5a05SMika Westerberg ispi->swseq_reg = true;
1090e23e5a05SMika Westerberg break;
1091e23e5a05SMika Westerberg
1092e23e5a05SMika Westerberg case INTEL_SPI_BXT:
1093e23e5a05SMika Westerberg ispi->sregs = ispi->base + BXT_SSFSTS_CTL;
1094e23e5a05SMika Westerberg ispi->pregs = ispi->base + BXT_PR;
1095e23e5a05SMika Westerberg ispi->nregions = BXT_FREG_NUM;
1096e23e5a05SMika Westerberg ispi->pr_num = BXT_PR_NUM;
1097e23e5a05SMika Westerberg erase_64k = true;
1098e23e5a05SMika Westerberg break;
1099e23e5a05SMika Westerberg
1100e23e5a05SMika Westerberg case INTEL_SPI_CNL:
1101e23e5a05SMika Westerberg ispi->sregs = NULL;
1102e23e5a05SMika Westerberg ispi->pregs = ispi->base + CNL_PR;
1103e23e5a05SMika Westerberg ispi->nregions = CNL_FREG_NUM;
1104e23e5a05SMika Westerberg ispi->pr_num = CNL_PR_NUM;
11051d895be1SMika Westerberg erase_64k = true;
1106e23e5a05SMika Westerberg break;
1107e23e5a05SMika Westerberg
1108e23e5a05SMika Westerberg default:
1109e23e5a05SMika Westerberg return -EINVAL;
1110e23e5a05SMika Westerberg }
1111e23e5a05SMika Westerberg
1112e23e5a05SMika Westerberg /* Try to disable write protection if user asked to do so */
1113e23e5a05SMika Westerberg if (writeable && !intel_spi_set_writeable(ispi)) {
1114e23e5a05SMika Westerberg dev_warn(ispi->dev, "can't disable chip write protection\n");
1115e23e5a05SMika Westerberg writeable = false;
1116e23e5a05SMika Westerberg }
1117e23e5a05SMika Westerberg
1118e23e5a05SMika Westerberg /* Disable #SMI generation from HW sequencer */
1119e23e5a05SMika Westerberg val = readl(ispi->base + HSFSTS_CTL);
1120e23e5a05SMika Westerberg val &= ~HSFSTS_CTL_FSMIE;
1121e23e5a05SMika Westerberg writel(val, ispi->base + HSFSTS_CTL);
1122e23e5a05SMika Westerberg
1123e23e5a05SMika Westerberg /*
1124e23e5a05SMika Westerberg * Determine whether erase operation should use HW or SW sequencer.
1125e23e5a05SMika Westerberg *
1126e23e5a05SMika Westerberg * The HW sequencer has a predefined list of opcodes, with only the
1127e23e5a05SMika Westerberg * erase opcode being programmable in LVSCC and UVSCC registers.
1128e23e5a05SMika Westerberg * If these registers don't contain a valid erase opcode, erase
1129e23e5a05SMika Westerberg * cannot be done using HW sequencer.
1130e23e5a05SMika Westerberg */
1131e23e5a05SMika Westerberg lvscc = readl(ispi->base + LVSCC);
1132e23e5a05SMika Westerberg uvscc = readl(ispi->base + UVSCC);
1133e23e5a05SMika Westerberg if (!(lvscc & ERASE_OPCODE_MASK) || !(uvscc & ERASE_OPCODE_MASK))
1134e23e5a05SMika Westerberg ispi->swseq_erase = true;
1135e23e5a05SMika Westerberg /* SPI controller on Intel BXT supports 64K erase opcode */
1136e23e5a05SMika Westerberg if (ispi->info->type == INTEL_SPI_BXT && !ispi->swseq_erase)
1137e23e5a05SMika Westerberg if (!(lvscc & ERASE_64K_OPCODE_MASK) ||
1138e23e5a05SMika Westerberg !(uvscc & ERASE_64K_OPCODE_MASK))
1139e23e5a05SMika Westerberg erase_64k = false;
1140e23e5a05SMika Westerberg
1141e23e5a05SMika Westerberg if (!ispi->sregs && (ispi->swseq_reg || ispi->swseq_erase)) {
1142e23e5a05SMika Westerberg dev_err(ispi->dev, "software sequencer not supported, but required\n");
1143e23e5a05SMika Westerberg return -EINVAL;
1144e23e5a05SMika Westerberg }
1145e23e5a05SMika Westerberg
1146e23e5a05SMika Westerberg /*
1147e23e5a05SMika Westerberg * Some controllers can only do basic operations using hardware
1148e23e5a05SMika Westerberg * sequencer. All other operations are supposed to be carried out
1149e23e5a05SMika Westerberg * using software sequencer.
1150e23e5a05SMika Westerberg */
1151e23e5a05SMika Westerberg if (ispi->swseq_reg) {
1152e23e5a05SMika Westerberg /* Disable #SMI generation from SW sequencer */
1153e23e5a05SMika Westerberg val = readl(ispi->sregs + SSFSTS_CTL);
1154e23e5a05SMika Westerberg val &= ~SSFSTS_CTL_FSMIE;
1155e23e5a05SMika Westerberg writel(val, ispi->sregs + SSFSTS_CTL);
1156e23e5a05SMika Westerberg }
1157e23e5a05SMika Westerberg
1158e23e5a05SMika Westerberg /* Check controller's lock status */
1159e23e5a05SMika Westerberg val = readl(ispi->base + HSFSTS_CTL);
1160e23e5a05SMika Westerberg ispi->locked = !!(val & HSFSTS_CTL_FLOCKDN);
1161e23e5a05SMika Westerberg
1162e23e5a05SMika Westerberg if (ispi->locked && ispi->sregs) {
1163e23e5a05SMika Westerberg /*
1164e23e5a05SMika Westerberg * BIOS programs allowed opcodes and then locks down the
1165e23e5a05SMika Westerberg * register. So read back what opcodes it decided to support.
1166e23e5a05SMika Westerberg * That's the set we are going to support as well.
1167e23e5a05SMika Westerberg */
1168e23e5a05SMika Westerberg opmenu0 = readl(ispi->sregs + OPMENU0);
1169e23e5a05SMika Westerberg opmenu1 = readl(ispi->sregs + OPMENU1);
1170e23e5a05SMika Westerberg
1171e23e5a05SMika Westerberg if (opmenu0 && opmenu1) {
1172e23e5a05SMika Westerberg for (i = 0; i < ARRAY_SIZE(ispi->opcodes) / 2; i++) {
1173e23e5a05SMika Westerberg ispi->opcodes[i] = opmenu0 >> i * 8;
1174e23e5a05SMika Westerberg ispi->opcodes[i + 4] = opmenu1 >> i * 8;
1175e23e5a05SMika Westerberg }
1176e23e5a05SMika Westerberg }
1177e23e5a05SMika Westerberg }
1178e23e5a05SMika Westerberg
1179e23e5a05SMika Westerberg if (erase_64k) {
1180e23e5a05SMika Westerberg dev_dbg(ispi->dev, "Using erase_64k memory operations");
1181e23e5a05SMika Westerberg ispi->mem_ops = erase_64k_mem_ops;
1182e23e5a05SMika Westerberg } else {
1183e23e5a05SMika Westerberg dev_dbg(ispi->dev, "Using generic memory operations");
1184e23e5a05SMika Westerberg ispi->mem_ops = generic_mem_ops;
1185e23e5a05SMika Westerberg }
1186e23e5a05SMika Westerberg
1187e23e5a05SMika Westerberg intel_spi_dump_regs(ispi);
1188e23e5a05SMika Westerberg return 0;
1189e23e5a05SMika Westerberg }
1190e23e5a05SMika Westerberg
intel_spi_is_protected(const struct intel_spi * ispi,unsigned int base,unsigned int limit)1191e23e5a05SMika Westerberg static bool intel_spi_is_protected(const struct intel_spi *ispi,
1192e23e5a05SMika Westerberg unsigned int base, unsigned int limit)
1193e23e5a05SMika Westerberg {
1194e23e5a05SMika Westerberg int i;
1195e23e5a05SMika Westerberg
1196e23e5a05SMika Westerberg for (i = 0; i < ispi->pr_num; i++) {
1197e23e5a05SMika Westerberg u32 pr_base, pr_limit, pr_value;
1198e23e5a05SMika Westerberg
1199e23e5a05SMika Westerberg pr_value = readl(ispi->pregs + PR(i));
1200e23e5a05SMika Westerberg if (!(pr_value & (PR_WPE | PR_RPE)))
1201e23e5a05SMika Westerberg continue;
1202e23e5a05SMika Westerberg
1203e23e5a05SMika Westerberg pr_limit = (pr_value & PR_LIMIT_MASK) >> PR_LIMIT_SHIFT;
1204e23e5a05SMika Westerberg pr_base = pr_value & PR_BASE_MASK;
1205e23e5a05SMika Westerberg
1206e23e5a05SMika Westerberg if (pr_base >= base && pr_limit <= limit)
1207e23e5a05SMika Westerberg return true;
1208e23e5a05SMika Westerberg }
1209e23e5a05SMika Westerberg
1210e23e5a05SMika Westerberg return false;
1211e23e5a05SMika Westerberg }
1212e23e5a05SMika Westerberg
1213e23e5a05SMika Westerberg /*
1214e23e5a05SMika Westerberg * There will be a single partition holding all enabled flash regions. We
1215e23e5a05SMika Westerberg * call this "BIOS".
1216e23e5a05SMika Westerberg */
intel_spi_fill_partition(struct intel_spi * ispi,struct mtd_partition * part)1217e23e5a05SMika Westerberg static void intel_spi_fill_partition(struct intel_spi *ispi,
1218e23e5a05SMika Westerberg struct mtd_partition *part)
1219e23e5a05SMika Westerberg {
1220e23e5a05SMika Westerberg u64 end;
1221e23e5a05SMika Westerberg int i;
1222e23e5a05SMika Westerberg
1223e23e5a05SMika Westerberg memset(part, 0, sizeof(*part));
1224e23e5a05SMika Westerberg
1225e23e5a05SMika Westerberg /* Start from the mandatory descriptor region */
1226e23e5a05SMika Westerberg part->size = 4096;
1227e23e5a05SMika Westerberg part->name = "BIOS";
1228e23e5a05SMika Westerberg
1229e23e5a05SMika Westerberg /*
1230e23e5a05SMika Westerberg * Now try to find where this partition ends based on the flash
1231e23e5a05SMika Westerberg * region registers.
1232e23e5a05SMika Westerberg */
1233e23e5a05SMika Westerberg for (i = 1; i < ispi->nregions; i++) {
1234e23e5a05SMika Westerberg u32 region, base, limit;
1235e23e5a05SMika Westerberg
1236e23e5a05SMika Westerberg region = readl(ispi->base + FREG(i));
1237e23e5a05SMika Westerberg base = region & FREG_BASE_MASK;
1238e23e5a05SMika Westerberg limit = (region & FREG_LIMIT_MASK) >> FREG_LIMIT_SHIFT;
1239e23e5a05SMika Westerberg
1240e23e5a05SMika Westerberg if (base >= limit || limit == 0)
1241e23e5a05SMika Westerberg continue;
1242e23e5a05SMika Westerberg
1243e23e5a05SMika Westerberg /*
1244e23e5a05SMika Westerberg * If any of the regions have protection bits set, make the
1245e23e5a05SMika Westerberg * whole partition read-only to be on the safe side.
1246e23e5a05SMika Westerberg *
1247e23e5a05SMika Westerberg * Also if the user did not ask the chip to be writeable
1248e23e5a05SMika Westerberg * mask the bit too.
1249e23e5a05SMika Westerberg */
1250e23e5a05SMika Westerberg if (!writeable || intel_spi_is_protected(ispi, base, limit))
1251e23e5a05SMika Westerberg part->mask_flags |= MTD_WRITEABLE;
1252e23e5a05SMika Westerberg
1253e23e5a05SMika Westerberg end = (limit << 12) + 4096;
1254e23e5a05SMika Westerberg if (end > part->size)
1255e23e5a05SMika Westerberg part->size = end;
1256e23e5a05SMika Westerberg }
1257e23e5a05SMika Westerberg }
1258e23e5a05SMika Westerberg
intel_spi_read_desc(struct intel_spi * ispi)12593f03c618SMika Westerberg static int intel_spi_read_desc(struct intel_spi *ispi)
12603f03c618SMika Westerberg {
12613f03c618SMika Westerberg struct spi_mem_op op =
12623f03c618SMika Westerberg SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ, 0),
12633f03c618SMika Westerberg SPI_MEM_OP_ADDR(3, 0, 0),
12643f03c618SMika Westerberg SPI_MEM_OP_NO_DUMMY,
12653f03c618SMika Westerberg SPI_MEM_OP_DATA_IN(0, NULL, 0));
12663f03c618SMika Westerberg u32 buf[2], nc, fcba, flcomp;
12673f03c618SMika Westerberg ssize_t ret;
12683f03c618SMika Westerberg
12693f03c618SMika Westerberg op.addr.val = 0x10;
12703f03c618SMika Westerberg op.data.buf.in = buf;
12713f03c618SMika Westerberg op.data.nbytes = sizeof(buf);
12723f03c618SMika Westerberg
12733f03c618SMika Westerberg ret = intel_spi_read(ispi, NULL, NULL, &op);
12743f03c618SMika Westerberg if (ret) {
12753f03c618SMika Westerberg dev_warn(ispi->dev, "failed to read descriptor\n");
12763f03c618SMika Westerberg return ret;
12773f03c618SMika Westerberg }
12783f03c618SMika Westerberg
12793f03c618SMika Westerberg dev_dbg(ispi->dev, "FLVALSIG=0x%08x\n", buf[0]);
12803f03c618SMika Westerberg dev_dbg(ispi->dev, "FLMAP0=0x%08x\n", buf[1]);
12813f03c618SMika Westerberg
12823f03c618SMika Westerberg if (buf[0] != FLVALSIG_MAGIC) {
12833f03c618SMika Westerberg dev_warn(ispi->dev, "descriptor signature not valid\n");
12843f03c618SMika Westerberg return -ENODEV;
12853f03c618SMika Westerberg }
12863f03c618SMika Westerberg
12873f03c618SMika Westerberg fcba = (buf[1] & FLMAP0_FCBA_MASK) << 4;
12883f03c618SMika Westerberg dev_dbg(ispi->dev, "FCBA=%#x\n", fcba);
12893f03c618SMika Westerberg
12903f03c618SMika Westerberg op.addr.val = fcba;
12913f03c618SMika Westerberg op.data.buf.in = &flcomp;
12923f03c618SMika Westerberg op.data.nbytes = sizeof(flcomp);
12933f03c618SMika Westerberg
12943f03c618SMika Westerberg ret = intel_spi_read(ispi, NULL, NULL, &op);
12953f03c618SMika Westerberg if (ret) {
12963f03c618SMika Westerberg dev_warn(ispi->dev, "failed to read FLCOMP\n");
12973f03c618SMika Westerberg return -ENODEV;
12983f03c618SMika Westerberg }
12993f03c618SMika Westerberg
13003f03c618SMika Westerberg dev_dbg(ispi->dev, "FLCOMP=0x%08x\n", flcomp);
13013f03c618SMika Westerberg
13023f03c618SMika Westerberg switch (flcomp & FLCOMP_C0DEN_MASK) {
13033f03c618SMika Westerberg case FLCOMP_C0DEN_512K:
13043f03c618SMika Westerberg ispi->chip0_size = SZ_512K;
13053f03c618SMika Westerberg break;
13063f03c618SMika Westerberg case FLCOMP_C0DEN_1M:
13073f03c618SMika Westerberg ispi->chip0_size = SZ_1M;
13083f03c618SMika Westerberg break;
13093f03c618SMika Westerberg case FLCOMP_C0DEN_2M:
13103f03c618SMika Westerberg ispi->chip0_size = SZ_2M;
13113f03c618SMika Westerberg break;
13123f03c618SMika Westerberg case FLCOMP_C0DEN_4M:
13133f03c618SMika Westerberg ispi->chip0_size = SZ_4M;
13143f03c618SMika Westerberg break;
13153f03c618SMika Westerberg case FLCOMP_C0DEN_8M:
13163f03c618SMika Westerberg ispi->chip0_size = SZ_8M;
13173f03c618SMika Westerberg break;
13183f03c618SMika Westerberg case FLCOMP_C0DEN_16M:
13193f03c618SMika Westerberg ispi->chip0_size = SZ_16M;
13203f03c618SMika Westerberg break;
13213f03c618SMika Westerberg case FLCOMP_C0DEN_32M:
13223f03c618SMika Westerberg ispi->chip0_size = SZ_32M;
13233f03c618SMika Westerberg break;
13243f03c618SMika Westerberg case FLCOMP_C0DEN_64M:
13253f03c618SMika Westerberg ispi->chip0_size = SZ_64M;
13263f03c618SMika Westerberg break;
13273f03c618SMika Westerberg default:
13283f03c618SMika Westerberg return -EINVAL;
13293f03c618SMika Westerberg }
13303f03c618SMika Westerberg
13313f03c618SMika Westerberg dev_dbg(ispi->dev, "chip0 size %zd KB\n", ispi->chip0_size / SZ_1K);
13323f03c618SMika Westerberg
13333f03c618SMika Westerberg nc = (buf[1] & FLMAP0_NC_MASK) >> FLMAP0_NC_SHIFT;
13343f03c618SMika Westerberg if (!nc)
1335*5fa0ade1SYang Yingliang ispi->host->num_chipselect = 1;
13363f03c618SMika Westerberg else if (nc == 1)
1337*5fa0ade1SYang Yingliang ispi->host->num_chipselect = 2;
13383f03c618SMika Westerberg else
13393f03c618SMika Westerberg return -EINVAL;
13403f03c618SMika Westerberg
13413f03c618SMika Westerberg dev_dbg(ispi->dev, "%u flash components found\n",
1342*5fa0ade1SYang Yingliang ispi->host->num_chipselect);
13433f03c618SMika Westerberg return 0;
13443f03c618SMika Westerberg }
13453f03c618SMika Westerberg
intel_spi_populate_chip(struct intel_spi * ispi)1346e23e5a05SMika Westerberg static int intel_spi_populate_chip(struct intel_spi *ispi)
1347e23e5a05SMika Westerberg {
1348e23e5a05SMika Westerberg struct flash_platform_data *pdata;
1349e23e5a05SMika Westerberg struct spi_board_info chip;
13503f03c618SMika Westerberg int ret;
1351e23e5a05SMika Westerberg
1352e23e5a05SMika Westerberg pdata = devm_kzalloc(ispi->dev, sizeof(*pdata), GFP_KERNEL);
1353e23e5a05SMika Westerberg if (!pdata)
1354e23e5a05SMika Westerberg return -ENOMEM;
1355e23e5a05SMika Westerberg
1356e23e5a05SMika Westerberg pdata->nr_parts = 1;
13571f19a2d1SChristophe JAILLET pdata->parts = devm_kcalloc(ispi->dev, pdata->nr_parts,
13581f19a2d1SChristophe JAILLET sizeof(*pdata->parts), GFP_KERNEL);
1359e23e5a05SMika Westerberg if (!pdata->parts)
1360e23e5a05SMika Westerberg return -ENOMEM;
1361e23e5a05SMika Westerberg
1362e23e5a05SMika Westerberg intel_spi_fill_partition(ispi, pdata->parts);
1363e23e5a05SMika Westerberg
1364e23e5a05SMika Westerberg memset(&chip, 0, sizeof(chip));
1365e23e5a05SMika Westerberg snprintf(chip.modalias, 8, "spi-nor");
1366e23e5a05SMika Westerberg chip.platform_data = pdata;
1367e23e5a05SMika Westerberg
1368*5fa0ade1SYang Yingliang if (!spi_new_device(ispi->host, &chip))
13693f03c618SMika Westerberg return -ENODEV;
13703f03c618SMika Westerberg
13713f03c618SMika Westerberg ret = intel_spi_read_desc(ispi);
13723f03c618SMika Westerberg if (ret)
13733f03c618SMika Westerberg return ret;
13743f03c618SMika Westerberg
1375574fbb95SMika Westerberg /* Add the second chip if present */
1376*5fa0ade1SYang Yingliang if (ispi->host->num_chipselect < 2)
1377574fbb95SMika Westerberg return 0;
1378574fbb95SMika Westerberg
13793f03c618SMika Westerberg chip.platform_data = NULL;
13803f03c618SMika Westerberg chip.chip_select = 1;
13813f03c618SMika Westerberg
1382*5fa0ade1SYang Yingliang if (!spi_new_device(ispi->host, &chip))
13833f03c618SMika Westerberg return -ENODEV;
13843f03c618SMika Westerberg return 0;
1385e23e5a05SMika Westerberg }
1386e23e5a05SMika Westerberg
1387e23e5a05SMika Westerberg /**
1388e23e5a05SMika Westerberg * intel_spi_probe() - Probe the Intel SPI flash controller
1389e23e5a05SMika Westerberg * @dev: Pointer to the parent device
1390e23e5a05SMika Westerberg * @mem: MMIO resource
13914bbaa857SMika Westerberg * @info: Platform specific information
1392e23e5a05SMika Westerberg *
1393e23e5a05SMika Westerberg * Probes Intel SPI flash controller and creates the flash chip device.
1394e23e5a05SMika Westerberg * Returns %0 on success and negative errno in case of failure.
1395e23e5a05SMika Westerberg */
intel_spi_probe(struct device * dev,struct resource * mem,const struct intel_spi_boardinfo * info)1396e23e5a05SMika Westerberg int intel_spi_probe(struct device *dev, struct resource *mem,
1397e23e5a05SMika Westerberg const struct intel_spi_boardinfo *info)
1398e23e5a05SMika Westerberg {
1399*5fa0ade1SYang Yingliang struct spi_controller *host;
1400e23e5a05SMika Westerberg struct intel_spi *ispi;
1401e23e5a05SMika Westerberg int ret;
1402e23e5a05SMika Westerberg
1403*5fa0ade1SYang Yingliang host = devm_spi_alloc_host(dev, sizeof(*ispi));
1404*5fa0ade1SYang Yingliang if (!host)
1405e23e5a05SMika Westerberg return -ENOMEM;
1406e23e5a05SMika Westerberg
1407*5fa0ade1SYang Yingliang host->mem_ops = &intel_spi_mem_ops;
1408e23e5a05SMika Westerberg
1409*5fa0ade1SYang Yingliang ispi = spi_controller_get_devdata(host);
1410e23e5a05SMika Westerberg
1411e23e5a05SMika Westerberg ispi->base = devm_ioremap_resource(dev, mem);
1412e23e5a05SMika Westerberg if (IS_ERR(ispi->base))
1413e23e5a05SMika Westerberg return PTR_ERR(ispi->base);
1414e23e5a05SMika Westerberg
1415e23e5a05SMika Westerberg ispi->dev = dev;
1416*5fa0ade1SYang Yingliang ispi->host = host;
1417e23e5a05SMika Westerberg ispi->info = info;
1418e23e5a05SMika Westerberg
1419e23e5a05SMika Westerberg ret = intel_spi_init(ispi);
1420e23e5a05SMika Westerberg if (ret)
1421e23e5a05SMika Westerberg return ret;
1422e23e5a05SMika Westerberg
1423*5fa0ade1SYang Yingliang ret = devm_spi_register_controller(dev, host);
1424e23e5a05SMika Westerberg if (ret)
1425e23e5a05SMika Westerberg return ret;
1426e23e5a05SMika Westerberg
1427e23e5a05SMika Westerberg return intel_spi_populate_chip(ispi);
1428e23e5a05SMika Westerberg }
1429e23e5a05SMika Westerberg EXPORT_SYMBOL_GPL(intel_spi_probe);
1430e23e5a05SMika Westerberg
1431e23e5a05SMika Westerberg MODULE_DESCRIPTION("Intel PCH/PCU SPI flash core driver");
1432e23e5a05SMika Westerberg MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1433e23e5a05SMika Westerberg MODULE_LICENSE("GPL v2");
1434