1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Arasan NAND Flash Controller Driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2014 - 2020 Xilinx, Inc.
6*4882a593Smuzhiyun * Author:
7*4882a593Smuzhiyun * Miquel Raynal <miquel.raynal@bootlin.com>
8*4882a593Smuzhiyun * Original work (fully rewritten):
9*4882a593Smuzhiyun * Punnaiah Choudary Kalluri <punnaia@xilinx.com>
10*4882a593Smuzhiyun * Naga Sureshkumar Relli <nagasure@xilinx.com>
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/bch.h>
14*4882a593Smuzhiyun #include <linux/bitfield.h>
15*4882a593Smuzhiyun #include <linux/clk.h>
16*4882a593Smuzhiyun #include <linux/delay.h>
17*4882a593Smuzhiyun #include <linux/dma-mapping.h>
18*4882a593Smuzhiyun #include <linux/interrupt.h>
19*4882a593Smuzhiyun #include <linux/iopoll.h>
20*4882a593Smuzhiyun #include <linux/module.h>
21*4882a593Smuzhiyun #include <linux/mtd/mtd.h>
22*4882a593Smuzhiyun #include <linux/mtd/partitions.h>
23*4882a593Smuzhiyun #include <linux/mtd/rawnand.h>
24*4882a593Smuzhiyun #include <linux/of.h>
25*4882a593Smuzhiyun #include <linux/platform_device.h>
26*4882a593Smuzhiyun #include <linux/slab.h>
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #define PKT_REG 0x00
29*4882a593Smuzhiyun #define PKT_SIZE(x) FIELD_PREP(GENMASK(10, 0), (x))
30*4882a593Smuzhiyun #define PKT_STEPS(x) FIELD_PREP(GENMASK(23, 12), (x))
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #define MEM_ADDR1_REG 0x04
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #define MEM_ADDR2_REG 0x08
35*4882a593Smuzhiyun #define ADDR2_STRENGTH(x) FIELD_PREP(GENMASK(27, 25), (x))
36*4882a593Smuzhiyun #define ADDR2_CS(x) FIELD_PREP(GENMASK(31, 30), (x))
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #define CMD_REG 0x0C
39*4882a593Smuzhiyun #define CMD_1(x) FIELD_PREP(GENMASK(7, 0), (x))
40*4882a593Smuzhiyun #define CMD_2(x) FIELD_PREP(GENMASK(15, 8), (x))
41*4882a593Smuzhiyun #define CMD_PAGE_SIZE(x) FIELD_PREP(GENMASK(25, 23), (x))
42*4882a593Smuzhiyun #define CMD_DMA_ENABLE BIT(27)
43*4882a593Smuzhiyun #define CMD_NADDRS(x) FIELD_PREP(GENMASK(30, 28), (x))
44*4882a593Smuzhiyun #define CMD_ECC_ENABLE BIT(31)
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun #define PROG_REG 0x10
47*4882a593Smuzhiyun #define PROG_PGRD BIT(0)
48*4882a593Smuzhiyun #define PROG_ERASE BIT(2)
49*4882a593Smuzhiyun #define PROG_STATUS BIT(3)
50*4882a593Smuzhiyun #define PROG_PGPROG BIT(4)
51*4882a593Smuzhiyun #define PROG_RDID BIT(6)
52*4882a593Smuzhiyun #define PROG_RDPARAM BIT(7)
53*4882a593Smuzhiyun #define PROG_RST BIT(8)
54*4882a593Smuzhiyun #define PROG_GET_FEATURE BIT(9)
55*4882a593Smuzhiyun #define PROG_SET_FEATURE BIT(10)
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun #define INTR_STS_EN_REG 0x14
58*4882a593Smuzhiyun #define INTR_SIG_EN_REG 0x18
59*4882a593Smuzhiyun #define INTR_STS_REG 0x1C
60*4882a593Smuzhiyun #define WRITE_READY BIT(0)
61*4882a593Smuzhiyun #define READ_READY BIT(1)
62*4882a593Smuzhiyun #define XFER_COMPLETE BIT(2)
63*4882a593Smuzhiyun #define DMA_BOUNDARY BIT(6)
64*4882a593Smuzhiyun #define EVENT_MASK GENMASK(7, 0)
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun #define READY_STS_REG 0x20
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun #define DMA_ADDR0_REG 0x50
69*4882a593Smuzhiyun #define DMA_ADDR1_REG 0x24
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun #define FLASH_STS_REG 0x28
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun #define DATA_PORT_REG 0x30
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun #define ECC_CONF_REG 0x34
76*4882a593Smuzhiyun #define ECC_CONF_COL(x) FIELD_PREP(GENMASK(15, 0), (x))
77*4882a593Smuzhiyun #define ECC_CONF_LEN(x) FIELD_PREP(GENMASK(26, 16), (x))
78*4882a593Smuzhiyun #define ECC_CONF_BCH_EN BIT(27)
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun #define ECC_ERR_CNT_REG 0x38
81*4882a593Smuzhiyun #define GET_PKT_ERR_CNT(x) FIELD_GET(GENMASK(7, 0), (x))
82*4882a593Smuzhiyun #define GET_PAGE_ERR_CNT(x) FIELD_GET(GENMASK(16, 8), (x))
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun #define ECC_SP_REG 0x3C
85*4882a593Smuzhiyun #define ECC_SP_CMD1(x) FIELD_PREP(GENMASK(7, 0), (x))
86*4882a593Smuzhiyun #define ECC_SP_CMD2(x) FIELD_PREP(GENMASK(15, 8), (x))
87*4882a593Smuzhiyun #define ECC_SP_ADDRS(x) FIELD_PREP(GENMASK(30, 28), (x))
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun #define ECC_1ERR_CNT_REG 0x40
90*4882a593Smuzhiyun #define ECC_2ERR_CNT_REG 0x44
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun #define DATA_INTERFACE_REG 0x6C
93*4882a593Smuzhiyun #define DIFACE_SDR_MODE(x) FIELD_PREP(GENMASK(2, 0), (x))
94*4882a593Smuzhiyun #define DIFACE_DDR_MODE(x) FIELD_PREP(GENMASK(5, 3), (x))
95*4882a593Smuzhiyun #define DIFACE_SDR 0
96*4882a593Smuzhiyun #define DIFACE_NVDDR BIT(9)
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun #define ANFC_MAX_CS 2
99*4882a593Smuzhiyun #define ANFC_DFLT_TIMEOUT_US 1000000
100*4882a593Smuzhiyun #define ANFC_MAX_CHUNK_SIZE SZ_1M
101*4882a593Smuzhiyun #define ANFC_MAX_PARAM_SIZE SZ_4K
102*4882a593Smuzhiyun #define ANFC_MAX_STEPS SZ_2K
103*4882a593Smuzhiyun #define ANFC_MAX_PKT_SIZE (SZ_2K - 1)
104*4882a593Smuzhiyun #define ANFC_MAX_ADDR_CYC 5U
105*4882a593Smuzhiyun #define ANFC_RSVD_ECC_BYTES 21
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun #define ANFC_XLNX_SDR_DFLT_CORE_CLK 100000000
108*4882a593Smuzhiyun #define ANFC_XLNX_SDR_HS_CORE_CLK 80000000
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /**
111*4882a593Smuzhiyun * struct anfc_op - Defines how to execute an operation
112*4882a593Smuzhiyun * @pkt_reg: Packet register
113*4882a593Smuzhiyun * @addr1_reg: Memory address 1 register
114*4882a593Smuzhiyun * @addr2_reg: Memory address 2 register
115*4882a593Smuzhiyun * @cmd_reg: Command register
116*4882a593Smuzhiyun * @prog_reg: Program register
117*4882a593Smuzhiyun * @steps: Number of "packets" to read/write
118*4882a593Smuzhiyun * @rdy_timeout_ms: Timeout for waits on Ready/Busy pin
119*4882a593Smuzhiyun * @len: Data transfer length
120*4882a593Smuzhiyun * @read: Data transfer direction from the controller point of view
121*4882a593Smuzhiyun */
122*4882a593Smuzhiyun struct anfc_op {
123*4882a593Smuzhiyun u32 pkt_reg;
124*4882a593Smuzhiyun u32 addr1_reg;
125*4882a593Smuzhiyun u32 addr2_reg;
126*4882a593Smuzhiyun u32 cmd_reg;
127*4882a593Smuzhiyun u32 prog_reg;
128*4882a593Smuzhiyun int steps;
129*4882a593Smuzhiyun unsigned int rdy_timeout_ms;
130*4882a593Smuzhiyun unsigned int len;
131*4882a593Smuzhiyun bool read;
132*4882a593Smuzhiyun u8 *buf;
133*4882a593Smuzhiyun };
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun /**
136*4882a593Smuzhiyun * struct anand - Defines the NAND chip related information
137*4882a593Smuzhiyun * @node: Used to store NAND chips into a list
138*4882a593Smuzhiyun * @chip: NAND chip information structure
139*4882a593Smuzhiyun * @cs: Chip select line
140*4882a593Smuzhiyun * @rb: Ready-busy line
141*4882a593Smuzhiyun * @page_sz: Register value of the page_sz field to use
142*4882a593Smuzhiyun * @clk: Expected clock frequency to use
143*4882a593Smuzhiyun * @timings: Data interface timing mode to use
144*4882a593Smuzhiyun * @ecc_conf: Hardware ECC configuration value
145*4882a593Smuzhiyun * @strength: Register value of the ECC strength
146*4882a593Smuzhiyun * @raddr_cycles: Row address cycle information
147*4882a593Smuzhiyun * @caddr_cycles: Column address cycle information
148*4882a593Smuzhiyun * @ecc_bits: Exact number of ECC bits per syndrome
149*4882a593Smuzhiyun * @ecc_total: Total number of ECC bytes
150*4882a593Smuzhiyun * @errloc: Array of errors located with soft BCH
151*4882a593Smuzhiyun * @hw_ecc: Buffer to store syndromes computed by hardware
152*4882a593Smuzhiyun * @bch: BCH structure
153*4882a593Smuzhiyun */
154*4882a593Smuzhiyun struct anand {
155*4882a593Smuzhiyun struct list_head node;
156*4882a593Smuzhiyun struct nand_chip chip;
157*4882a593Smuzhiyun unsigned int cs;
158*4882a593Smuzhiyun unsigned int rb;
159*4882a593Smuzhiyun unsigned int page_sz;
160*4882a593Smuzhiyun unsigned long clk;
161*4882a593Smuzhiyun u32 timings;
162*4882a593Smuzhiyun u32 ecc_conf;
163*4882a593Smuzhiyun u32 strength;
164*4882a593Smuzhiyun u16 raddr_cycles;
165*4882a593Smuzhiyun u16 caddr_cycles;
166*4882a593Smuzhiyun unsigned int ecc_bits;
167*4882a593Smuzhiyun unsigned int ecc_total;
168*4882a593Smuzhiyun unsigned int *errloc;
169*4882a593Smuzhiyun u8 *hw_ecc;
170*4882a593Smuzhiyun struct bch_control *bch;
171*4882a593Smuzhiyun };
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun /**
174*4882a593Smuzhiyun * struct arasan_nfc - Defines the Arasan NAND flash controller driver instance
175*4882a593Smuzhiyun * @dev: Pointer to the device structure
176*4882a593Smuzhiyun * @base: Remapped register area
177*4882a593Smuzhiyun * @controller_clk: Pointer to the system clock
178*4882a593Smuzhiyun * @bus_clk: Pointer to the flash clock
179*4882a593Smuzhiyun * @controller: Base controller structure
180*4882a593Smuzhiyun * @chips: List of all NAND chips attached to the controller
181*4882a593Smuzhiyun * @assigned_cs: Bitmask describing already assigned CS lines
182*4882a593Smuzhiyun * @cur_clk: Current clock rate
183*4882a593Smuzhiyun */
184*4882a593Smuzhiyun struct arasan_nfc {
185*4882a593Smuzhiyun struct device *dev;
186*4882a593Smuzhiyun void __iomem *base;
187*4882a593Smuzhiyun struct clk *controller_clk;
188*4882a593Smuzhiyun struct clk *bus_clk;
189*4882a593Smuzhiyun struct nand_controller controller;
190*4882a593Smuzhiyun struct list_head chips;
191*4882a593Smuzhiyun unsigned long assigned_cs;
192*4882a593Smuzhiyun unsigned int cur_clk;
193*4882a593Smuzhiyun };
194*4882a593Smuzhiyun
to_anand(struct nand_chip * nand)195*4882a593Smuzhiyun static struct anand *to_anand(struct nand_chip *nand)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun return container_of(nand, struct anand, chip);
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
to_anfc(struct nand_controller * ctrl)200*4882a593Smuzhiyun static struct arasan_nfc *to_anfc(struct nand_controller *ctrl)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun return container_of(ctrl, struct arasan_nfc, controller);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
anfc_wait_for_event(struct arasan_nfc * nfc,unsigned int event)205*4882a593Smuzhiyun static int anfc_wait_for_event(struct arasan_nfc *nfc, unsigned int event)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun u32 val;
208*4882a593Smuzhiyun int ret;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun ret = readl_relaxed_poll_timeout(nfc->base + INTR_STS_REG, val,
211*4882a593Smuzhiyun val & event, 0,
212*4882a593Smuzhiyun ANFC_DFLT_TIMEOUT_US);
213*4882a593Smuzhiyun if (ret) {
214*4882a593Smuzhiyun dev_err(nfc->dev, "Timeout waiting for event 0x%x\n", event);
215*4882a593Smuzhiyun return -ETIMEDOUT;
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun writel_relaxed(event, nfc->base + INTR_STS_REG);
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun return 0;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun
anfc_wait_for_rb(struct arasan_nfc * nfc,struct nand_chip * chip,unsigned int timeout_ms)223*4882a593Smuzhiyun static int anfc_wait_for_rb(struct arasan_nfc *nfc, struct nand_chip *chip,
224*4882a593Smuzhiyun unsigned int timeout_ms)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun struct anand *anand = to_anand(chip);
227*4882a593Smuzhiyun u32 val;
228*4882a593Smuzhiyun int ret;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /* There is no R/B interrupt, we must poll a register */
231*4882a593Smuzhiyun ret = readl_relaxed_poll_timeout(nfc->base + READY_STS_REG, val,
232*4882a593Smuzhiyun val & BIT(anand->rb),
233*4882a593Smuzhiyun 1, timeout_ms * 1000);
234*4882a593Smuzhiyun if (ret) {
235*4882a593Smuzhiyun dev_err(nfc->dev, "Timeout waiting for R/B 0x%x\n",
236*4882a593Smuzhiyun readl_relaxed(nfc->base + READY_STS_REG));
237*4882a593Smuzhiyun return -ETIMEDOUT;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun return 0;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
anfc_trigger_op(struct arasan_nfc * nfc,struct anfc_op * nfc_op)243*4882a593Smuzhiyun static void anfc_trigger_op(struct arasan_nfc *nfc, struct anfc_op *nfc_op)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun writel_relaxed(nfc_op->pkt_reg, nfc->base + PKT_REG);
246*4882a593Smuzhiyun writel_relaxed(nfc_op->addr1_reg, nfc->base + MEM_ADDR1_REG);
247*4882a593Smuzhiyun writel_relaxed(nfc_op->addr2_reg, nfc->base + MEM_ADDR2_REG);
248*4882a593Smuzhiyun writel_relaxed(nfc_op->cmd_reg, nfc->base + CMD_REG);
249*4882a593Smuzhiyun writel_relaxed(nfc_op->prog_reg, nfc->base + PROG_REG);
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
anfc_pkt_len_config(unsigned int len,unsigned int * steps,unsigned int * pktsize)252*4882a593Smuzhiyun static int anfc_pkt_len_config(unsigned int len, unsigned int *steps,
253*4882a593Smuzhiyun unsigned int *pktsize)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun unsigned int nb, sz;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun for (nb = 1; nb < ANFC_MAX_STEPS; nb *= 2) {
258*4882a593Smuzhiyun sz = len / nb;
259*4882a593Smuzhiyun if (sz <= ANFC_MAX_PKT_SIZE)
260*4882a593Smuzhiyun break;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun if (sz * nb != len)
264*4882a593Smuzhiyun return -ENOTSUPP;
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun if (steps)
267*4882a593Smuzhiyun *steps = nb;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun if (pktsize)
270*4882a593Smuzhiyun *pktsize = sz;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun return 0;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
anfc_select_target(struct nand_chip * chip,int target)275*4882a593Smuzhiyun static int anfc_select_target(struct nand_chip *chip, int target)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun struct anand *anand = to_anand(chip);
278*4882a593Smuzhiyun struct arasan_nfc *nfc = to_anfc(chip->controller);
279*4882a593Smuzhiyun int ret;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /* Update the controller timings and the potential ECC configuration */
282*4882a593Smuzhiyun writel_relaxed(anand->timings, nfc->base + DATA_INTERFACE_REG);
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun /* Update clock frequency */
285*4882a593Smuzhiyun if (nfc->cur_clk != anand->clk) {
286*4882a593Smuzhiyun clk_disable_unprepare(nfc->bus_clk);
287*4882a593Smuzhiyun ret = clk_set_rate(nfc->bus_clk, anand->clk);
288*4882a593Smuzhiyun if (ret) {
289*4882a593Smuzhiyun dev_err(nfc->dev, "Failed to change clock rate\n");
290*4882a593Smuzhiyun return ret;
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun ret = clk_prepare_enable(nfc->bus_clk);
294*4882a593Smuzhiyun if (ret) {
295*4882a593Smuzhiyun dev_err(nfc->dev,
296*4882a593Smuzhiyun "Failed to re-enable the bus clock\n");
297*4882a593Smuzhiyun return ret;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun nfc->cur_clk = anand->clk;
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun return 0;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun /*
307*4882a593Smuzhiyun * When using the embedded hardware ECC engine, the controller is in charge of
308*4882a593Smuzhiyun * feeding the engine with, first, the ECC residue present in the data array.
309*4882a593Smuzhiyun * A typical read operation is:
310*4882a593Smuzhiyun * 1/ Assert the read operation by sending the relevant command/address cycles
311*4882a593Smuzhiyun * but targeting the column of the first ECC bytes in the OOB area instead of
312*4882a593Smuzhiyun * the main data directly.
313*4882a593Smuzhiyun * 2/ After having read the relevant number of ECC bytes, the controller uses
314*4882a593Smuzhiyun * the RNDOUT/RNDSTART commands which are set into the "ECC Spare Command
315*4882a593Smuzhiyun * Register" to move the pointer back at the beginning of the main data.
316*4882a593Smuzhiyun * 3/ It will read the content of the main area for a given size (pktsize) and
317*4882a593Smuzhiyun * will feed the ECC engine with this buffer again.
318*4882a593Smuzhiyun * 4/ The ECC engine derives the ECC bytes for the given data and compare them
319*4882a593Smuzhiyun * with the ones already received. It eventually trigger status flags and
320*4882a593Smuzhiyun * then set the "Buffer Read Ready" flag.
321*4882a593Smuzhiyun * 5/ The corrected data is then available for reading from the data port
322*4882a593Smuzhiyun * register.
323*4882a593Smuzhiyun *
324*4882a593Smuzhiyun * The hardware BCH ECC engine is known to be inconstent in BCH mode and never
325*4882a593Smuzhiyun * reports uncorrectable errors. Because of this bug, we have to use the
326*4882a593Smuzhiyun * software BCH implementation in the read path.
327*4882a593Smuzhiyun */
anfc_read_page_hw_ecc(struct nand_chip * chip,u8 * buf,int oob_required,int page)328*4882a593Smuzhiyun static int anfc_read_page_hw_ecc(struct nand_chip *chip, u8 *buf,
329*4882a593Smuzhiyun int oob_required, int page)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun struct arasan_nfc *nfc = to_anfc(chip->controller);
332*4882a593Smuzhiyun struct mtd_info *mtd = nand_to_mtd(chip);
333*4882a593Smuzhiyun struct anand *anand = to_anand(chip);
334*4882a593Smuzhiyun unsigned int len = mtd->writesize + (oob_required ? mtd->oobsize : 0);
335*4882a593Smuzhiyun unsigned int max_bitflips = 0;
336*4882a593Smuzhiyun dma_addr_t dma_addr;
337*4882a593Smuzhiyun int step, ret;
338*4882a593Smuzhiyun struct anfc_op nfc_op = {
339*4882a593Smuzhiyun .pkt_reg =
340*4882a593Smuzhiyun PKT_SIZE(chip->ecc.size) |
341*4882a593Smuzhiyun PKT_STEPS(chip->ecc.steps),
342*4882a593Smuzhiyun .addr1_reg =
343*4882a593Smuzhiyun (page & 0xFF) << (8 * (anand->caddr_cycles)) |
344*4882a593Smuzhiyun (((page >> 8) & 0xFF) << (8 * (1 + anand->caddr_cycles))),
345*4882a593Smuzhiyun .addr2_reg =
346*4882a593Smuzhiyun ((page >> 16) & 0xFF) |
347*4882a593Smuzhiyun ADDR2_STRENGTH(anand->strength) |
348*4882a593Smuzhiyun ADDR2_CS(anand->cs),
349*4882a593Smuzhiyun .cmd_reg =
350*4882a593Smuzhiyun CMD_1(NAND_CMD_READ0) |
351*4882a593Smuzhiyun CMD_2(NAND_CMD_READSTART) |
352*4882a593Smuzhiyun CMD_PAGE_SIZE(anand->page_sz) |
353*4882a593Smuzhiyun CMD_DMA_ENABLE |
354*4882a593Smuzhiyun CMD_NADDRS(anand->caddr_cycles +
355*4882a593Smuzhiyun anand->raddr_cycles),
356*4882a593Smuzhiyun .prog_reg = PROG_PGRD,
357*4882a593Smuzhiyun };
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun dma_addr = dma_map_single(nfc->dev, (void *)buf, len, DMA_FROM_DEVICE);
360*4882a593Smuzhiyun if (dma_mapping_error(nfc->dev, dma_addr)) {
361*4882a593Smuzhiyun dev_err(nfc->dev, "Buffer mapping error");
362*4882a593Smuzhiyun return -EIO;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun writel_relaxed(lower_32_bits(dma_addr), nfc->base + DMA_ADDR0_REG);
366*4882a593Smuzhiyun writel_relaxed(upper_32_bits(dma_addr), nfc->base + DMA_ADDR1_REG);
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun anfc_trigger_op(nfc, &nfc_op);
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun ret = anfc_wait_for_event(nfc, XFER_COMPLETE);
371*4882a593Smuzhiyun dma_unmap_single(nfc->dev, dma_addr, len, DMA_FROM_DEVICE);
372*4882a593Smuzhiyun if (ret) {
373*4882a593Smuzhiyun dev_err(nfc->dev, "Error reading page %d\n", page);
374*4882a593Smuzhiyun return ret;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun /* Store the raw OOB bytes as well */
378*4882a593Smuzhiyun ret = nand_change_read_column_op(chip, mtd->writesize, chip->oob_poi,
379*4882a593Smuzhiyun mtd->oobsize, 0);
380*4882a593Smuzhiyun if (ret)
381*4882a593Smuzhiyun return ret;
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun /*
384*4882a593Smuzhiyun * For each step, compute by softare the BCH syndrome over the raw data.
385*4882a593Smuzhiyun * Compare the theoretical amount of errors and compare with the
386*4882a593Smuzhiyun * hardware engine feedback.
387*4882a593Smuzhiyun */
388*4882a593Smuzhiyun for (step = 0; step < chip->ecc.steps; step++) {
389*4882a593Smuzhiyun u8 *raw_buf = &buf[step * chip->ecc.size];
390*4882a593Smuzhiyun unsigned int bit, byte;
391*4882a593Smuzhiyun int bf, i;
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun /* Extract the syndrome, it is not necessarily aligned */
394*4882a593Smuzhiyun memset(anand->hw_ecc, 0, chip->ecc.bytes);
395*4882a593Smuzhiyun nand_extract_bits(anand->hw_ecc, 0,
396*4882a593Smuzhiyun &chip->oob_poi[mtd->oobsize - anand->ecc_total],
397*4882a593Smuzhiyun anand->ecc_bits * step, anand->ecc_bits);
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun bf = bch_decode(anand->bch, raw_buf, chip->ecc.size,
400*4882a593Smuzhiyun anand->hw_ecc, NULL, NULL, anand->errloc);
401*4882a593Smuzhiyun if (!bf) {
402*4882a593Smuzhiyun continue;
403*4882a593Smuzhiyun } else if (bf > 0) {
404*4882a593Smuzhiyun for (i = 0; i < bf; i++) {
405*4882a593Smuzhiyun /* Only correct the data, not the syndrome */
406*4882a593Smuzhiyun if (anand->errloc[i] < (chip->ecc.size * 8)) {
407*4882a593Smuzhiyun bit = BIT(anand->errloc[i] & 7);
408*4882a593Smuzhiyun byte = anand->errloc[i] >> 3;
409*4882a593Smuzhiyun raw_buf[byte] ^= bit;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun mtd->ecc_stats.corrected += bf;
414*4882a593Smuzhiyun max_bitflips = max_t(unsigned int, max_bitflips, bf);
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun continue;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun bf = nand_check_erased_ecc_chunk(raw_buf, chip->ecc.size,
420*4882a593Smuzhiyun NULL, 0, NULL, 0,
421*4882a593Smuzhiyun chip->ecc.strength);
422*4882a593Smuzhiyun if (bf > 0) {
423*4882a593Smuzhiyun mtd->ecc_stats.corrected += bf;
424*4882a593Smuzhiyun max_bitflips = max_t(unsigned int, max_bitflips, bf);
425*4882a593Smuzhiyun memset(raw_buf, 0xFF, chip->ecc.size);
426*4882a593Smuzhiyun } else if (bf < 0) {
427*4882a593Smuzhiyun mtd->ecc_stats.failed++;
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun return 0;
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun
anfc_sel_read_page_hw_ecc(struct nand_chip * chip,u8 * buf,int oob_required,int page)434*4882a593Smuzhiyun static int anfc_sel_read_page_hw_ecc(struct nand_chip *chip, u8 *buf,
435*4882a593Smuzhiyun int oob_required, int page)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun int ret;
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun ret = anfc_select_target(chip, chip->cur_cs);
440*4882a593Smuzhiyun if (ret)
441*4882a593Smuzhiyun return ret;
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun return anfc_read_page_hw_ecc(chip, buf, oob_required, page);
444*4882a593Smuzhiyun };
445*4882a593Smuzhiyun
anfc_write_page_hw_ecc(struct nand_chip * chip,const u8 * buf,int oob_required,int page)446*4882a593Smuzhiyun static int anfc_write_page_hw_ecc(struct nand_chip *chip, const u8 *buf,
447*4882a593Smuzhiyun int oob_required, int page)
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun struct anand *anand = to_anand(chip);
450*4882a593Smuzhiyun struct arasan_nfc *nfc = to_anfc(chip->controller);
451*4882a593Smuzhiyun struct mtd_info *mtd = nand_to_mtd(chip);
452*4882a593Smuzhiyun unsigned int len = mtd->writesize + (oob_required ? mtd->oobsize : 0);
453*4882a593Smuzhiyun dma_addr_t dma_addr;
454*4882a593Smuzhiyun int ret;
455*4882a593Smuzhiyun struct anfc_op nfc_op = {
456*4882a593Smuzhiyun .pkt_reg =
457*4882a593Smuzhiyun PKT_SIZE(chip->ecc.size) |
458*4882a593Smuzhiyun PKT_STEPS(chip->ecc.steps),
459*4882a593Smuzhiyun .addr1_reg =
460*4882a593Smuzhiyun (page & 0xFF) << (8 * (anand->caddr_cycles)) |
461*4882a593Smuzhiyun (((page >> 8) & 0xFF) << (8 * (1 + anand->caddr_cycles))),
462*4882a593Smuzhiyun .addr2_reg =
463*4882a593Smuzhiyun ((page >> 16) & 0xFF) |
464*4882a593Smuzhiyun ADDR2_STRENGTH(anand->strength) |
465*4882a593Smuzhiyun ADDR2_CS(anand->cs),
466*4882a593Smuzhiyun .cmd_reg =
467*4882a593Smuzhiyun CMD_1(NAND_CMD_SEQIN) |
468*4882a593Smuzhiyun CMD_2(NAND_CMD_PAGEPROG) |
469*4882a593Smuzhiyun CMD_PAGE_SIZE(anand->page_sz) |
470*4882a593Smuzhiyun CMD_DMA_ENABLE |
471*4882a593Smuzhiyun CMD_NADDRS(anand->caddr_cycles +
472*4882a593Smuzhiyun anand->raddr_cycles) |
473*4882a593Smuzhiyun CMD_ECC_ENABLE,
474*4882a593Smuzhiyun .prog_reg = PROG_PGPROG,
475*4882a593Smuzhiyun };
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun writel_relaxed(anand->ecc_conf, nfc->base + ECC_CONF_REG);
478*4882a593Smuzhiyun writel_relaxed(ECC_SP_CMD1(NAND_CMD_RNDIN) |
479*4882a593Smuzhiyun ECC_SP_ADDRS(anand->caddr_cycles),
480*4882a593Smuzhiyun nfc->base + ECC_SP_REG);
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun dma_addr = dma_map_single(nfc->dev, (void *)buf, len, DMA_TO_DEVICE);
483*4882a593Smuzhiyun if (dma_mapping_error(nfc->dev, dma_addr)) {
484*4882a593Smuzhiyun dev_err(nfc->dev, "Buffer mapping error");
485*4882a593Smuzhiyun return -EIO;
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun writel_relaxed(lower_32_bits(dma_addr), nfc->base + DMA_ADDR0_REG);
489*4882a593Smuzhiyun writel_relaxed(upper_32_bits(dma_addr), nfc->base + DMA_ADDR1_REG);
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun anfc_trigger_op(nfc, &nfc_op);
492*4882a593Smuzhiyun ret = anfc_wait_for_event(nfc, XFER_COMPLETE);
493*4882a593Smuzhiyun dma_unmap_single(nfc->dev, dma_addr, len, DMA_TO_DEVICE);
494*4882a593Smuzhiyun if (ret) {
495*4882a593Smuzhiyun dev_err(nfc->dev, "Error writing page %d\n", page);
496*4882a593Smuzhiyun return ret;
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun /* Spare data is not protected */
500*4882a593Smuzhiyun if (oob_required)
501*4882a593Smuzhiyun ret = nand_write_oob_std(chip, page);
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun return ret;
504*4882a593Smuzhiyun }
505*4882a593Smuzhiyun
anfc_sel_write_page_hw_ecc(struct nand_chip * chip,const u8 * buf,int oob_required,int page)506*4882a593Smuzhiyun static int anfc_sel_write_page_hw_ecc(struct nand_chip *chip, const u8 *buf,
507*4882a593Smuzhiyun int oob_required, int page)
508*4882a593Smuzhiyun {
509*4882a593Smuzhiyun int ret;
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun ret = anfc_select_target(chip, chip->cur_cs);
512*4882a593Smuzhiyun if (ret)
513*4882a593Smuzhiyun return ret;
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun return anfc_write_page_hw_ecc(chip, buf, oob_required, page);
516*4882a593Smuzhiyun };
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun /* NAND framework ->exec_op() hooks and related helpers */
anfc_parse_instructions(struct nand_chip * chip,const struct nand_subop * subop,struct anfc_op * nfc_op)519*4882a593Smuzhiyun static int anfc_parse_instructions(struct nand_chip *chip,
520*4882a593Smuzhiyun const struct nand_subop *subop,
521*4882a593Smuzhiyun struct anfc_op *nfc_op)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun struct anand *anand = to_anand(chip);
524*4882a593Smuzhiyun const struct nand_op_instr *instr = NULL;
525*4882a593Smuzhiyun bool first_cmd = true;
526*4882a593Smuzhiyun unsigned int op_id;
527*4882a593Smuzhiyun int ret, i;
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun memset(nfc_op, 0, sizeof(*nfc_op));
530*4882a593Smuzhiyun nfc_op->addr2_reg = ADDR2_CS(anand->cs);
531*4882a593Smuzhiyun nfc_op->cmd_reg = CMD_PAGE_SIZE(anand->page_sz);
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun for (op_id = 0; op_id < subop->ninstrs; op_id++) {
534*4882a593Smuzhiyun unsigned int offset, naddrs, pktsize;
535*4882a593Smuzhiyun const u8 *addrs;
536*4882a593Smuzhiyun u8 *buf;
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun instr = &subop->instrs[op_id];
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun switch (instr->type) {
541*4882a593Smuzhiyun case NAND_OP_CMD_INSTR:
542*4882a593Smuzhiyun if (first_cmd)
543*4882a593Smuzhiyun nfc_op->cmd_reg |= CMD_1(instr->ctx.cmd.opcode);
544*4882a593Smuzhiyun else
545*4882a593Smuzhiyun nfc_op->cmd_reg |= CMD_2(instr->ctx.cmd.opcode);
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun first_cmd = false;
548*4882a593Smuzhiyun break;
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun case NAND_OP_ADDR_INSTR:
551*4882a593Smuzhiyun offset = nand_subop_get_addr_start_off(subop, op_id);
552*4882a593Smuzhiyun naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
553*4882a593Smuzhiyun addrs = &instr->ctx.addr.addrs[offset];
554*4882a593Smuzhiyun nfc_op->cmd_reg |= CMD_NADDRS(naddrs);
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun for (i = 0; i < min(ANFC_MAX_ADDR_CYC, naddrs); i++) {
557*4882a593Smuzhiyun if (i < 4)
558*4882a593Smuzhiyun nfc_op->addr1_reg |= (u32)addrs[i] << i * 8;
559*4882a593Smuzhiyun else
560*4882a593Smuzhiyun nfc_op->addr2_reg |= addrs[i];
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun break;
564*4882a593Smuzhiyun case NAND_OP_DATA_IN_INSTR:
565*4882a593Smuzhiyun nfc_op->read = true;
566*4882a593Smuzhiyun fallthrough;
567*4882a593Smuzhiyun case NAND_OP_DATA_OUT_INSTR:
568*4882a593Smuzhiyun offset = nand_subop_get_data_start_off(subop, op_id);
569*4882a593Smuzhiyun buf = instr->ctx.data.buf.in;
570*4882a593Smuzhiyun nfc_op->buf = &buf[offset];
571*4882a593Smuzhiyun nfc_op->len = nand_subop_get_data_len(subop, op_id);
572*4882a593Smuzhiyun ret = anfc_pkt_len_config(nfc_op->len, &nfc_op->steps,
573*4882a593Smuzhiyun &pktsize);
574*4882a593Smuzhiyun if (ret)
575*4882a593Smuzhiyun return ret;
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun /*
578*4882a593Smuzhiyun * Number of DATA cycles must be aligned on 4, this
579*4882a593Smuzhiyun * means the controller might read/write more than
580*4882a593Smuzhiyun * requested. This is harmless most of the time as extra
581*4882a593Smuzhiyun * DATA are discarded in the write path and read pointer
582*4882a593Smuzhiyun * adjusted in the read path.
583*4882a593Smuzhiyun *
584*4882a593Smuzhiyun * FIXME: The core should mark operations where
585*4882a593Smuzhiyun * reading/writing more is allowed so the exec_op()
586*4882a593Smuzhiyun * implementation can take the right decision when the
587*4882a593Smuzhiyun * alignment constraint is not met: adjust the number of
588*4882a593Smuzhiyun * DATA cycles when it's allowed, reject the operation
589*4882a593Smuzhiyun * otherwise.
590*4882a593Smuzhiyun */
591*4882a593Smuzhiyun nfc_op->pkt_reg |= PKT_SIZE(round_up(pktsize, 4)) |
592*4882a593Smuzhiyun PKT_STEPS(nfc_op->steps);
593*4882a593Smuzhiyun break;
594*4882a593Smuzhiyun case NAND_OP_WAITRDY_INSTR:
595*4882a593Smuzhiyun nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
596*4882a593Smuzhiyun break;
597*4882a593Smuzhiyun }
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun return 0;
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun
anfc_rw_pio_op(struct arasan_nfc * nfc,struct anfc_op * nfc_op)603*4882a593Smuzhiyun static int anfc_rw_pio_op(struct arasan_nfc *nfc, struct anfc_op *nfc_op)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun unsigned int dwords = (nfc_op->len / 4) / nfc_op->steps;
606*4882a593Smuzhiyun unsigned int last_len = nfc_op->len % 4;
607*4882a593Smuzhiyun unsigned int offset, dir;
608*4882a593Smuzhiyun u8 *buf = nfc_op->buf;
609*4882a593Smuzhiyun int ret, i;
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun for (i = 0; i < nfc_op->steps; i++) {
612*4882a593Smuzhiyun dir = nfc_op->read ? READ_READY : WRITE_READY;
613*4882a593Smuzhiyun ret = anfc_wait_for_event(nfc, dir);
614*4882a593Smuzhiyun if (ret) {
615*4882a593Smuzhiyun dev_err(nfc->dev, "PIO %s ready signal not received\n",
616*4882a593Smuzhiyun nfc_op->read ? "Read" : "Write");
617*4882a593Smuzhiyun return ret;
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun offset = i * (dwords * 4);
621*4882a593Smuzhiyun if (nfc_op->read)
622*4882a593Smuzhiyun ioread32_rep(nfc->base + DATA_PORT_REG, &buf[offset],
623*4882a593Smuzhiyun dwords);
624*4882a593Smuzhiyun else
625*4882a593Smuzhiyun iowrite32_rep(nfc->base + DATA_PORT_REG, &buf[offset],
626*4882a593Smuzhiyun dwords);
627*4882a593Smuzhiyun }
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun if (last_len) {
630*4882a593Smuzhiyun u32 remainder;
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun offset = nfc_op->len - last_len;
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun if (nfc_op->read) {
635*4882a593Smuzhiyun remainder = readl_relaxed(nfc->base + DATA_PORT_REG);
636*4882a593Smuzhiyun memcpy(&buf[offset], &remainder, last_len);
637*4882a593Smuzhiyun } else {
638*4882a593Smuzhiyun memcpy(&remainder, &buf[offset], last_len);
639*4882a593Smuzhiyun writel_relaxed(remainder, nfc->base + DATA_PORT_REG);
640*4882a593Smuzhiyun }
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun return anfc_wait_for_event(nfc, XFER_COMPLETE);
644*4882a593Smuzhiyun }
645*4882a593Smuzhiyun
anfc_misc_data_type_exec(struct nand_chip * chip,const struct nand_subop * subop,u32 prog_reg)646*4882a593Smuzhiyun static int anfc_misc_data_type_exec(struct nand_chip *chip,
647*4882a593Smuzhiyun const struct nand_subop *subop,
648*4882a593Smuzhiyun u32 prog_reg)
649*4882a593Smuzhiyun {
650*4882a593Smuzhiyun struct arasan_nfc *nfc = to_anfc(chip->controller);
651*4882a593Smuzhiyun struct anfc_op nfc_op = {};
652*4882a593Smuzhiyun int ret;
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun ret = anfc_parse_instructions(chip, subop, &nfc_op);
655*4882a593Smuzhiyun if (ret)
656*4882a593Smuzhiyun return ret;
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun nfc_op.prog_reg = prog_reg;
659*4882a593Smuzhiyun anfc_trigger_op(nfc, &nfc_op);
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun if (nfc_op.rdy_timeout_ms) {
662*4882a593Smuzhiyun ret = anfc_wait_for_rb(nfc, chip, nfc_op.rdy_timeout_ms);
663*4882a593Smuzhiyun if (ret)
664*4882a593Smuzhiyun return ret;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun return anfc_rw_pio_op(nfc, &nfc_op);
668*4882a593Smuzhiyun }
669*4882a593Smuzhiyun
anfc_param_read_type_exec(struct nand_chip * chip,const struct nand_subop * subop)670*4882a593Smuzhiyun static int anfc_param_read_type_exec(struct nand_chip *chip,
671*4882a593Smuzhiyun const struct nand_subop *subop)
672*4882a593Smuzhiyun {
673*4882a593Smuzhiyun return anfc_misc_data_type_exec(chip, subop, PROG_RDPARAM);
674*4882a593Smuzhiyun }
675*4882a593Smuzhiyun
anfc_data_read_type_exec(struct nand_chip * chip,const struct nand_subop * subop)676*4882a593Smuzhiyun static int anfc_data_read_type_exec(struct nand_chip *chip,
677*4882a593Smuzhiyun const struct nand_subop *subop)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun return anfc_misc_data_type_exec(chip, subop, PROG_PGRD);
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun
anfc_param_write_type_exec(struct nand_chip * chip,const struct nand_subop * subop)682*4882a593Smuzhiyun static int anfc_param_write_type_exec(struct nand_chip *chip,
683*4882a593Smuzhiyun const struct nand_subop *subop)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun return anfc_misc_data_type_exec(chip, subop, PROG_SET_FEATURE);
686*4882a593Smuzhiyun }
687*4882a593Smuzhiyun
anfc_data_write_type_exec(struct nand_chip * chip,const struct nand_subop * subop)688*4882a593Smuzhiyun static int anfc_data_write_type_exec(struct nand_chip *chip,
689*4882a593Smuzhiyun const struct nand_subop *subop)
690*4882a593Smuzhiyun {
691*4882a593Smuzhiyun return anfc_misc_data_type_exec(chip, subop, PROG_PGPROG);
692*4882a593Smuzhiyun }
693*4882a593Smuzhiyun
anfc_misc_zerolen_type_exec(struct nand_chip * chip,const struct nand_subop * subop,u32 prog_reg)694*4882a593Smuzhiyun static int anfc_misc_zerolen_type_exec(struct nand_chip *chip,
695*4882a593Smuzhiyun const struct nand_subop *subop,
696*4882a593Smuzhiyun u32 prog_reg)
697*4882a593Smuzhiyun {
698*4882a593Smuzhiyun struct arasan_nfc *nfc = to_anfc(chip->controller);
699*4882a593Smuzhiyun struct anfc_op nfc_op = {};
700*4882a593Smuzhiyun int ret;
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun ret = anfc_parse_instructions(chip, subop, &nfc_op);
703*4882a593Smuzhiyun if (ret)
704*4882a593Smuzhiyun return ret;
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun nfc_op.prog_reg = prog_reg;
707*4882a593Smuzhiyun anfc_trigger_op(nfc, &nfc_op);
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun ret = anfc_wait_for_event(nfc, XFER_COMPLETE);
710*4882a593Smuzhiyun if (ret)
711*4882a593Smuzhiyun return ret;
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun if (nfc_op.rdy_timeout_ms)
714*4882a593Smuzhiyun ret = anfc_wait_for_rb(nfc, chip, nfc_op.rdy_timeout_ms);
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun return ret;
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun
anfc_status_type_exec(struct nand_chip * chip,const struct nand_subop * subop)719*4882a593Smuzhiyun static int anfc_status_type_exec(struct nand_chip *chip,
720*4882a593Smuzhiyun const struct nand_subop *subop)
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun struct arasan_nfc *nfc = to_anfc(chip->controller);
723*4882a593Smuzhiyun u32 tmp;
724*4882a593Smuzhiyun int ret;
725*4882a593Smuzhiyun
726*4882a593Smuzhiyun /* See anfc_check_op() for details about this constraint */
727*4882a593Smuzhiyun if (subop->instrs[0].ctx.cmd.opcode != NAND_CMD_STATUS)
728*4882a593Smuzhiyun return -ENOTSUPP;
729*4882a593Smuzhiyun
730*4882a593Smuzhiyun ret = anfc_misc_zerolen_type_exec(chip, subop, PROG_STATUS);
731*4882a593Smuzhiyun if (ret)
732*4882a593Smuzhiyun return ret;
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun tmp = readl_relaxed(nfc->base + FLASH_STS_REG);
735*4882a593Smuzhiyun memcpy(subop->instrs[1].ctx.data.buf.in, &tmp, 1);
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun return 0;
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun
anfc_reset_type_exec(struct nand_chip * chip,const struct nand_subop * subop)740*4882a593Smuzhiyun static int anfc_reset_type_exec(struct nand_chip *chip,
741*4882a593Smuzhiyun const struct nand_subop *subop)
742*4882a593Smuzhiyun {
743*4882a593Smuzhiyun return anfc_misc_zerolen_type_exec(chip, subop, PROG_RST);
744*4882a593Smuzhiyun }
745*4882a593Smuzhiyun
anfc_erase_type_exec(struct nand_chip * chip,const struct nand_subop * subop)746*4882a593Smuzhiyun static int anfc_erase_type_exec(struct nand_chip *chip,
747*4882a593Smuzhiyun const struct nand_subop *subop)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun return anfc_misc_zerolen_type_exec(chip, subop, PROG_ERASE);
750*4882a593Smuzhiyun }
751*4882a593Smuzhiyun
anfc_wait_type_exec(struct nand_chip * chip,const struct nand_subop * subop)752*4882a593Smuzhiyun static int anfc_wait_type_exec(struct nand_chip *chip,
753*4882a593Smuzhiyun const struct nand_subop *subop)
754*4882a593Smuzhiyun {
755*4882a593Smuzhiyun struct arasan_nfc *nfc = to_anfc(chip->controller);
756*4882a593Smuzhiyun struct anfc_op nfc_op = {};
757*4882a593Smuzhiyun int ret;
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun ret = anfc_parse_instructions(chip, subop, &nfc_op);
760*4882a593Smuzhiyun if (ret)
761*4882a593Smuzhiyun return ret;
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun return anfc_wait_for_rb(nfc, chip, nfc_op.rdy_timeout_ms);
764*4882a593Smuzhiyun }
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun static const struct nand_op_parser anfc_op_parser = NAND_OP_PARSER(
767*4882a593Smuzhiyun NAND_OP_PARSER_PATTERN(
768*4882a593Smuzhiyun anfc_param_read_type_exec,
769*4882a593Smuzhiyun NAND_OP_PARSER_PAT_CMD_ELEM(false),
770*4882a593Smuzhiyun NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
771*4882a593Smuzhiyun NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
772*4882a593Smuzhiyun NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE)),
773*4882a593Smuzhiyun NAND_OP_PARSER_PATTERN(
774*4882a593Smuzhiyun anfc_param_write_type_exec,
775*4882a593Smuzhiyun NAND_OP_PARSER_PAT_CMD_ELEM(false),
776*4882a593Smuzhiyun NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
777*4882a593Smuzhiyun NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, ANFC_MAX_PARAM_SIZE)),
778*4882a593Smuzhiyun NAND_OP_PARSER_PATTERN(
779*4882a593Smuzhiyun anfc_data_read_type_exec,
780*4882a593Smuzhiyun NAND_OP_PARSER_PAT_CMD_ELEM(false),
781*4882a593Smuzhiyun NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
782*4882a593Smuzhiyun NAND_OP_PARSER_PAT_CMD_ELEM(false),
783*4882a593Smuzhiyun NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
784*4882a593Smuzhiyun NAND_OP_PARSER_PAT_DATA_IN_ELEM(true, ANFC_MAX_CHUNK_SIZE)),
785*4882a593Smuzhiyun NAND_OP_PARSER_PATTERN(
786*4882a593Smuzhiyun anfc_data_write_type_exec,
787*4882a593Smuzhiyun NAND_OP_PARSER_PAT_CMD_ELEM(false),
788*4882a593Smuzhiyun NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
789*4882a593Smuzhiyun NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, ANFC_MAX_CHUNK_SIZE),
790*4882a593Smuzhiyun NAND_OP_PARSER_PAT_CMD_ELEM(false)),
791*4882a593Smuzhiyun NAND_OP_PARSER_PATTERN(
792*4882a593Smuzhiyun anfc_reset_type_exec,
793*4882a593Smuzhiyun NAND_OP_PARSER_PAT_CMD_ELEM(false),
794*4882a593Smuzhiyun NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
795*4882a593Smuzhiyun NAND_OP_PARSER_PATTERN(
796*4882a593Smuzhiyun anfc_erase_type_exec,
797*4882a593Smuzhiyun NAND_OP_PARSER_PAT_CMD_ELEM(false),
798*4882a593Smuzhiyun NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
799*4882a593Smuzhiyun NAND_OP_PARSER_PAT_CMD_ELEM(false),
800*4882a593Smuzhiyun NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
801*4882a593Smuzhiyun NAND_OP_PARSER_PATTERN(
802*4882a593Smuzhiyun anfc_status_type_exec,
803*4882a593Smuzhiyun NAND_OP_PARSER_PAT_CMD_ELEM(false),
804*4882a593Smuzhiyun NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE)),
805*4882a593Smuzhiyun NAND_OP_PARSER_PATTERN(
806*4882a593Smuzhiyun anfc_wait_type_exec,
807*4882a593Smuzhiyun NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
808*4882a593Smuzhiyun );
809*4882a593Smuzhiyun
anfc_check_op(struct nand_chip * chip,const struct nand_operation * op)810*4882a593Smuzhiyun static int anfc_check_op(struct nand_chip *chip,
811*4882a593Smuzhiyun const struct nand_operation *op)
812*4882a593Smuzhiyun {
813*4882a593Smuzhiyun const struct nand_op_instr *instr;
814*4882a593Smuzhiyun int op_id;
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun /*
817*4882a593Smuzhiyun * The controller abstracts all the NAND operations and do not support
818*4882a593Smuzhiyun * data only operations.
819*4882a593Smuzhiyun *
820*4882a593Smuzhiyun * TODO: The nand_op_parser framework should be extended to
821*4882a593Smuzhiyun * support custom checks on DATA instructions.
822*4882a593Smuzhiyun */
823*4882a593Smuzhiyun for (op_id = 0; op_id < op->ninstrs; op_id++) {
824*4882a593Smuzhiyun instr = &op->instrs[op_id];
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun switch (instr->type) {
827*4882a593Smuzhiyun case NAND_OP_ADDR_INSTR:
828*4882a593Smuzhiyun if (instr->ctx.addr.naddrs > ANFC_MAX_ADDR_CYC)
829*4882a593Smuzhiyun return -ENOTSUPP;
830*4882a593Smuzhiyun
831*4882a593Smuzhiyun break;
832*4882a593Smuzhiyun case NAND_OP_DATA_IN_INSTR:
833*4882a593Smuzhiyun case NAND_OP_DATA_OUT_INSTR:
834*4882a593Smuzhiyun if (instr->ctx.data.len > ANFC_MAX_CHUNK_SIZE)
835*4882a593Smuzhiyun return -ENOTSUPP;
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun if (anfc_pkt_len_config(instr->ctx.data.len, 0, 0))
838*4882a593Smuzhiyun return -ENOTSUPP;
839*4882a593Smuzhiyun
840*4882a593Smuzhiyun break;
841*4882a593Smuzhiyun default:
842*4882a593Smuzhiyun break;
843*4882a593Smuzhiyun }
844*4882a593Smuzhiyun }
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun /*
847*4882a593Smuzhiyun * The controller does not allow to proceed with a CMD+DATA_IN cycle
848*4882a593Smuzhiyun * manually on the bus by reading data from the data register. Instead,
849*4882a593Smuzhiyun * the controller abstract a status read operation with its own status
850*4882a593Smuzhiyun * register after ordering a read status operation. Hence, we cannot
851*4882a593Smuzhiyun * support any CMD+DATA_IN operation other than a READ STATUS.
852*4882a593Smuzhiyun *
853*4882a593Smuzhiyun * TODO: The nand_op_parser() framework should be extended to describe
854*4882a593Smuzhiyun * fixed patterns instead of open-coding this check here.
855*4882a593Smuzhiyun */
856*4882a593Smuzhiyun if (op->ninstrs == 2 &&
857*4882a593Smuzhiyun op->instrs[0].type == NAND_OP_CMD_INSTR &&
858*4882a593Smuzhiyun op->instrs[0].ctx.cmd.opcode != NAND_CMD_STATUS &&
859*4882a593Smuzhiyun op->instrs[1].type == NAND_OP_DATA_IN_INSTR)
860*4882a593Smuzhiyun return -ENOTSUPP;
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun return nand_op_parser_exec_op(chip, &anfc_op_parser, op, true);
863*4882a593Smuzhiyun }
864*4882a593Smuzhiyun
anfc_exec_op(struct nand_chip * chip,const struct nand_operation * op,bool check_only)865*4882a593Smuzhiyun static int anfc_exec_op(struct nand_chip *chip,
866*4882a593Smuzhiyun const struct nand_operation *op,
867*4882a593Smuzhiyun bool check_only)
868*4882a593Smuzhiyun {
869*4882a593Smuzhiyun int ret;
870*4882a593Smuzhiyun
871*4882a593Smuzhiyun if (check_only)
872*4882a593Smuzhiyun return anfc_check_op(chip, op);
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun ret = anfc_select_target(chip, op->cs);
875*4882a593Smuzhiyun if (ret)
876*4882a593Smuzhiyun return ret;
877*4882a593Smuzhiyun
878*4882a593Smuzhiyun return nand_op_parser_exec_op(chip, &anfc_op_parser, op, check_only);
879*4882a593Smuzhiyun }
880*4882a593Smuzhiyun
anfc_setup_interface(struct nand_chip * chip,int target,const struct nand_interface_config * conf)881*4882a593Smuzhiyun static int anfc_setup_interface(struct nand_chip *chip, int target,
882*4882a593Smuzhiyun const struct nand_interface_config *conf)
883*4882a593Smuzhiyun {
884*4882a593Smuzhiyun struct anand *anand = to_anand(chip);
885*4882a593Smuzhiyun struct arasan_nfc *nfc = to_anfc(chip->controller);
886*4882a593Smuzhiyun struct device_node *np = nfc->dev->of_node;
887*4882a593Smuzhiyun const struct nand_sdr_timings *sdr;
888*4882a593Smuzhiyun const struct nand_nvddr_timings *nvddr;
889*4882a593Smuzhiyun
890*4882a593Smuzhiyun if (nand_interface_is_nvddr(conf)) {
891*4882a593Smuzhiyun nvddr = nand_get_nvddr_timings(conf);
892*4882a593Smuzhiyun if (IS_ERR(nvddr))
893*4882a593Smuzhiyun return PTR_ERR(nvddr);
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun /*
896*4882a593Smuzhiyun * The controller only supports data payload requests which are
897*4882a593Smuzhiyun * a multiple of 4. In practice, most data accesses are 4-byte
898*4882a593Smuzhiyun * aligned and this is not an issue. However, rounding up will
899*4882a593Smuzhiyun * simply be refused by the controller if we reached the end of
900*4882a593Smuzhiyun * the device *and* we are using the NV-DDR interface(!). In
901*4882a593Smuzhiyun * this situation, unaligned data requests ending at the device
902*4882a593Smuzhiyun * boundary will confuse the controller and cannot be performed.
903*4882a593Smuzhiyun *
904*4882a593Smuzhiyun * This is something that happens in nand_read_subpage() when
905*4882a593Smuzhiyun * selecting software ECC support and must be avoided.
906*4882a593Smuzhiyun */
907*4882a593Smuzhiyun if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT)
908*4882a593Smuzhiyun return -ENOTSUPP;
909*4882a593Smuzhiyun } else {
910*4882a593Smuzhiyun sdr = nand_get_sdr_timings(conf);
911*4882a593Smuzhiyun if (IS_ERR(sdr))
912*4882a593Smuzhiyun return PTR_ERR(sdr);
913*4882a593Smuzhiyun }
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun if (target < 0)
916*4882a593Smuzhiyun return 0;
917*4882a593Smuzhiyun
918*4882a593Smuzhiyun if (nand_interface_is_sdr(conf))
919*4882a593Smuzhiyun anand->timings = DIFACE_SDR |
920*4882a593Smuzhiyun DIFACE_SDR_MODE(conf->timings.mode);
921*4882a593Smuzhiyun else
922*4882a593Smuzhiyun anand->timings = DIFACE_NVDDR |
923*4882a593Smuzhiyun DIFACE_DDR_MODE(conf->timings.mode);
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun if (nand_interface_is_sdr(conf)) {
926*4882a593Smuzhiyun anand->clk = ANFC_XLNX_SDR_DFLT_CORE_CLK;
927*4882a593Smuzhiyun } else {
928*4882a593Smuzhiyun /* ONFI timings are defined in picoseconds */
929*4882a593Smuzhiyun anand->clk = div_u64((u64)NSEC_PER_SEC * 1000,
930*4882a593Smuzhiyun conf->timings.nvddr.tCK_min);
931*4882a593Smuzhiyun }
932*4882a593Smuzhiyun
933*4882a593Smuzhiyun /*
934*4882a593Smuzhiyun * Due to a hardware bug in the ZynqMP SoC, SDR timing modes 0-1 work
935*4882a593Smuzhiyun * with f > 90MHz (default clock is 100MHz) but signals are unstable
936*4882a593Smuzhiyun * with higher modes. Hence we decrease a little bit the clock rate to
937*4882a593Smuzhiyun * 80MHz when using SDR modes 2-5 with this SoC.
938*4882a593Smuzhiyun */
939*4882a593Smuzhiyun if (of_device_is_compatible(np, "xlnx,zynqmp-nand-controller") &&
940*4882a593Smuzhiyun nand_interface_is_sdr(conf) && conf->timings.mode >= 2)
941*4882a593Smuzhiyun anand->clk = ANFC_XLNX_SDR_HS_CORE_CLK;
942*4882a593Smuzhiyun
943*4882a593Smuzhiyun return 0;
944*4882a593Smuzhiyun }
945*4882a593Smuzhiyun
anfc_calc_hw_ecc_bytes(int step_size,int strength)946*4882a593Smuzhiyun static int anfc_calc_hw_ecc_bytes(int step_size, int strength)
947*4882a593Smuzhiyun {
948*4882a593Smuzhiyun unsigned int bch_gf_mag, ecc_bits;
949*4882a593Smuzhiyun
950*4882a593Smuzhiyun switch (step_size) {
951*4882a593Smuzhiyun case SZ_512:
952*4882a593Smuzhiyun bch_gf_mag = 13;
953*4882a593Smuzhiyun break;
954*4882a593Smuzhiyun case SZ_1K:
955*4882a593Smuzhiyun bch_gf_mag = 14;
956*4882a593Smuzhiyun break;
957*4882a593Smuzhiyun default:
958*4882a593Smuzhiyun return -EINVAL;
959*4882a593Smuzhiyun }
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun ecc_bits = bch_gf_mag * strength;
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun return DIV_ROUND_UP(ecc_bits, 8);
964*4882a593Smuzhiyun }
965*4882a593Smuzhiyun
966*4882a593Smuzhiyun static const int anfc_hw_ecc_512_strengths[] = {4, 8, 12};
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun static const int anfc_hw_ecc_1024_strengths[] = {24};
969*4882a593Smuzhiyun
970*4882a593Smuzhiyun static const struct nand_ecc_step_info anfc_hw_ecc_step_infos[] = {
971*4882a593Smuzhiyun {
972*4882a593Smuzhiyun .stepsize = SZ_512,
973*4882a593Smuzhiyun .strengths = anfc_hw_ecc_512_strengths,
974*4882a593Smuzhiyun .nstrengths = ARRAY_SIZE(anfc_hw_ecc_512_strengths),
975*4882a593Smuzhiyun },
976*4882a593Smuzhiyun {
977*4882a593Smuzhiyun .stepsize = SZ_1K,
978*4882a593Smuzhiyun .strengths = anfc_hw_ecc_1024_strengths,
979*4882a593Smuzhiyun .nstrengths = ARRAY_SIZE(anfc_hw_ecc_1024_strengths),
980*4882a593Smuzhiyun },
981*4882a593Smuzhiyun };
982*4882a593Smuzhiyun
983*4882a593Smuzhiyun static const struct nand_ecc_caps anfc_hw_ecc_caps = {
984*4882a593Smuzhiyun .stepinfos = anfc_hw_ecc_step_infos,
985*4882a593Smuzhiyun .nstepinfos = ARRAY_SIZE(anfc_hw_ecc_step_infos),
986*4882a593Smuzhiyun .calc_ecc_bytes = anfc_calc_hw_ecc_bytes,
987*4882a593Smuzhiyun };
988*4882a593Smuzhiyun
anfc_init_hw_ecc_controller(struct arasan_nfc * nfc,struct nand_chip * chip)989*4882a593Smuzhiyun static int anfc_init_hw_ecc_controller(struct arasan_nfc *nfc,
990*4882a593Smuzhiyun struct nand_chip *chip)
991*4882a593Smuzhiyun {
992*4882a593Smuzhiyun struct anand *anand = to_anand(chip);
993*4882a593Smuzhiyun struct mtd_info *mtd = nand_to_mtd(chip);
994*4882a593Smuzhiyun struct nand_ecc_ctrl *ecc = &chip->ecc;
995*4882a593Smuzhiyun unsigned int bch_prim_poly = 0, bch_gf_mag = 0, ecc_offset;
996*4882a593Smuzhiyun int ret;
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun switch (mtd->writesize) {
999*4882a593Smuzhiyun case SZ_512:
1000*4882a593Smuzhiyun case SZ_2K:
1001*4882a593Smuzhiyun case SZ_4K:
1002*4882a593Smuzhiyun case SZ_8K:
1003*4882a593Smuzhiyun case SZ_16K:
1004*4882a593Smuzhiyun break;
1005*4882a593Smuzhiyun default:
1006*4882a593Smuzhiyun dev_err(nfc->dev, "Unsupported page size %d\n", mtd->writesize);
1007*4882a593Smuzhiyun return -EINVAL;
1008*4882a593Smuzhiyun }
1009*4882a593Smuzhiyun
1010*4882a593Smuzhiyun ret = nand_ecc_choose_conf(chip, &anfc_hw_ecc_caps, mtd->oobsize);
1011*4882a593Smuzhiyun if (ret)
1012*4882a593Smuzhiyun return ret;
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun switch (ecc->strength) {
1015*4882a593Smuzhiyun case 12:
1016*4882a593Smuzhiyun anand->strength = 0x1;
1017*4882a593Smuzhiyun break;
1018*4882a593Smuzhiyun case 8:
1019*4882a593Smuzhiyun anand->strength = 0x2;
1020*4882a593Smuzhiyun break;
1021*4882a593Smuzhiyun case 4:
1022*4882a593Smuzhiyun anand->strength = 0x3;
1023*4882a593Smuzhiyun break;
1024*4882a593Smuzhiyun case 24:
1025*4882a593Smuzhiyun anand->strength = 0x4;
1026*4882a593Smuzhiyun break;
1027*4882a593Smuzhiyun default:
1028*4882a593Smuzhiyun dev_err(nfc->dev, "Unsupported strength %d\n", ecc->strength);
1029*4882a593Smuzhiyun return -EINVAL;
1030*4882a593Smuzhiyun }
1031*4882a593Smuzhiyun
1032*4882a593Smuzhiyun switch (ecc->size) {
1033*4882a593Smuzhiyun case SZ_512:
1034*4882a593Smuzhiyun bch_gf_mag = 13;
1035*4882a593Smuzhiyun bch_prim_poly = 0x201b;
1036*4882a593Smuzhiyun break;
1037*4882a593Smuzhiyun case SZ_1K:
1038*4882a593Smuzhiyun bch_gf_mag = 14;
1039*4882a593Smuzhiyun bch_prim_poly = 0x4443;
1040*4882a593Smuzhiyun break;
1041*4882a593Smuzhiyun default:
1042*4882a593Smuzhiyun dev_err(nfc->dev, "Unsupported step size %d\n", ecc->strength);
1043*4882a593Smuzhiyun return -EINVAL;
1044*4882a593Smuzhiyun }
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun mtd_set_ooblayout(mtd, nand_get_large_page_ooblayout());
1047*4882a593Smuzhiyun
1048*4882a593Smuzhiyun ecc->steps = mtd->writesize / ecc->size;
1049*4882a593Smuzhiyun ecc->algo = NAND_ECC_ALGO_BCH;
1050*4882a593Smuzhiyun anand->ecc_bits = bch_gf_mag * ecc->strength;
1051*4882a593Smuzhiyun ecc->bytes = DIV_ROUND_UP(anand->ecc_bits, 8);
1052*4882a593Smuzhiyun anand->ecc_total = DIV_ROUND_UP(anand->ecc_bits * ecc->steps, 8);
1053*4882a593Smuzhiyun ecc_offset = mtd->writesize + mtd->oobsize - anand->ecc_total;
1054*4882a593Smuzhiyun anand->ecc_conf = ECC_CONF_COL(ecc_offset) |
1055*4882a593Smuzhiyun ECC_CONF_LEN(anand->ecc_total) |
1056*4882a593Smuzhiyun ECC_CONF_BCH_EN;
1057*4882a593Smuzhiyun
1058*4882a593Smuzhiyun anand->errloc = devm_kmalloc_array(nfc->dev, ecc->strength,
1059*4882a593Smuzhiyun sizeof(*anand->errloc), GFP_KERNEL);
1060*4882a593Smuzhiyun if (!anand->errloc)
1061*4882a593Smuzhiyun return -ENOMEM;
1062*4882a593Smuzhiyun
1063*4882a593Smuzhiyun anand->hw_ecc = devm_kmalloc(nfc->dev, ecc->bytes, GFP_KERNEL);
1064*4882a593Smuzhiyun if (!anand->hw_ecc)
1065*4882a593Smuzhiyun return -ENOMEM;
1066*4882a593Smuzhiyun
1067*4882a593Smuzhiyun /* Enforce bit swapping to fit the hardware */
1068*4882a593Smuzhiyun anand->bch = bch_init(bch_gf_mag, ecc->strength, bch_prim_poly, true);
1069*4882a593Smuzhiyun if (!anand->bch)
1070*4882a593Smuzhiyun return -EINVAL;
1071*4882a593Smuzhiyun
1072*4882a593Smuzhiyun ecc->read_page = anfc_sel_read_page_hw_ecc;
1073*4882a593Smuzhiyun ecc->write_page = anfc_sel_write_page_hw_ecc;
1074*4882a593Smuzhiyun
1075*4882a593Smuzhiyun return 0;
1076*4882a593Smuzhiyun }
1077*4882a593Smuzhiyun
anfc_attach_chip(struct nand_chip * chip)1078*4882a593Smuzhiyun static int anfc_attach_chip(struct nand_chip *chip)
1079*4882a593Smuzhiyun {
1080*4882a593Smuzhiyun struct anand *anand = to_anand(chip);
1081*4882a593Smuzhiyun struct arasan_nfc *nfc = to_anfc(chip->controller);
1082*4882a593Smuzhiyun struct mtd_info *mtd = nand_to_mtd(chip);
1083*4882a593Smuzhiyun int ret = 0;
1084*4882a593Smuzhiyun
1085*4882a593Smuzhiyun if (mtd->writesize <= SZ_512)
1086*4882a593Smuzhiyun anand->caddr_cycles = 1;
1087*4882a593Smuzhiyun else
1088*4882a593Smuzhiyun anand->caddr_cycles = 2;
1089*4882a593Smuzhiyun
1090*4882a593Smuzhiyun if (chip->options & NAND_ROW_ADDR_3)
1091*4882a593Smuzhiyun anand->raddr_cycles = 3;
1092*4882a593Smuzhiyun else
1093*4882a593Smuzhiyun anand->raddr_cycles = 2;
1094*4882a593Smuzhiyun
1095*4882a593Smuzhiyun switch (mtd->writesize) {
1096*4882a593Smuzhiyun case 512:
1097*4882a593Smuzhiyun anand->page_sz = 0;
1098*4882a593Smuzhiyun break;
1099*4882a593Smuzhiyun case 1024:
1100*4882a593Smuzhiyun anand->page_sz = 5;
1101*4882a593Smuzhiyun break;
1102*4882a593Smuzhiyun case 2048:
1103*4882a593Smuzhiyun anand->page_sz = 1;
1104*4882a593Smuzhiyun break;
1105*4882a593Smuzhiyun case 4096:
1106*4882a593Smuzhiyun anand->page_sz = 2;
1107*4882a593Smuzhiyun break;
1108*4882a593Smuzhiyun case 8192:
1109*4882a593Smuzhiyun anand->page_sz = 3;
1110*4882a593Smuzhiyun break;
1111*4882a593Smuzhiyun case 16384:
1112*4882a593Smuzhiyun anand->page_sz = 4;
1113*4882a593Smuzhiyun break;
1114*4882a593Smuzhiyun default:
1115*4882a593Smuzhiyun return -EINVAL;
1116*4882a593Smuzhiyun }
1117*4882a593Smuzhiyun
1118*4882a593Smuzhiyun /* These hooks are valid for all ECC providers */
1119*4882a593Smuzhiyun chip->ecc.read_page_raw = nand_monolithic_read_page_raw;
1120*4882a593Smuzhiyun chip->ecc.write_page_raw = nand_monolithic_write_page_raw;
1121*4882a593Smuzhiyun
1122*4882a593Smuzhiyun switch (chip->ecc.engine_type) {
1123*4882a593Smuzhiyun case NAND_ECC_ENGINE_TYPE_NONE:
1124*4882a593Smuzhiyun case NAND_ECC_ENGINE_TYPE_SOFT:
1125*4882a593Smuzhiyun case NAND_ECC_ENGINE_TYPE_ON_DIE:
1126*4882a593Smuzhiyun break;
1127*4882a593Smuzhiyun case NAND_ECC_ENGINE_TYPE_ON_HOST:
1128*4882a593Smuzhiyun ret = anfc_init_hw_ecc_controller(nfc, chip);
1129*4882a593Smuzhiyun break;
1130*4882a593Smuzhiyun default:
1131*4882a593Smuzhiyun dev_err(nfc->dev, "Unsupported ECC mode: %d\n",
1132*4882a593Smuzhiyun chip->ecc.engine_type);
1133*4882a593Smuzhiyun return -EINVAL;
1134*4882a593Smuzhiyun }
1135*4882a593Smuzhiyun
1136*4882a593Smuzhiyun return ret;
1137*4882a593Smuzhiyun }
1138*4882a593Smuzhiyun
anfc_detach_chip(struct nand_chip * chip)1139*4882a593Smuzhiyun static void anfc_detach_chip(struct nand_chip *chip)
1140*4882a593Smuzhiyun {
1141*4882a593Smuzhiyun struct anand *anand = to_anand(chip);
1142*4882a593Smuzhiyun
1143*4882a593Smuzhiyun if (anand->bch)
1144*4882a593Smuzhiyun bch_free(anand->bch);
1145*4882a593Smuzhiyun }
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun static const struct nand_controller_ops anfc_ops = {
1148*4882a593Smuzhiyun .exec_op = anfc_exec_op,
1149*4882a593Smuzhiyun .setup_interface = anfc_setup_interface,
1150*4882a593Smuzhiyun .attach_chip = anfc_attach_chip,
1151*4882a593Smuzhiyun .detach_chip = anfc_detach_chip,
1152*4882a593Smuzhiyun };
1153*4882a593Smuzhiyun
anfc_chip_init(struct arasan_nfc * nfc,struct device_node * np)1154*4882a593Smuzhiyun static int anfc_chip_init(struct arasan_nfc *nfc, struct device_node *np)
1155*4882a593Smuzhiyun {
1156*4882a593Smuzhiyun struct anand *anand;
1157*4882a593Smuzhiyun struct nand_chip *chip;
1158*4882a593Smuzhiyun struct mtd_info *mtd;
1159*4882a593Smuzhiyun int cs, rb, ret;
1160*4882a593Smuzhiyun
1161*4882a593Smuzhiyun anand = devm_kzalloc(nfc->dev, sizeof(*anand), GFP_KERNEL);
1162*4882a593Smuzhiyun if (!anand)
1163*4882a593Smuzhiyun return -ENOMEM;
1164*4882a593Smuzhiyun
1165*4882a593Smuzhiyun /* We do not support multiple CS per chip yet */
1166*4882a593Smuzhiyun if (of_property_count_elems_of_size(np, "reg", sizeof(u32)) != 1) {
1167*4882a593Smuzhiyun dev_err(nfc->dev, "Invalid reg property\n");
1168*4882a593Smuzhiyun return -EINVAL;
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun
1171*4882a593Smuzhiyun ret = of_property_read_u32(np, "reg", &cs);
1172*4882a593Smuzhiyun if (ret)
1173*4882a593Smuzhiyun return ret;
1174*4882a593Smuzhiyun
1175*4882a593Smuzhiyun ret = of_property_read_u32(np, "nand-rb", &rb);
1176*4882a593Smuzhiyun if (ret)
1177*4882a593Smuzhiyun return ret;
1178*4882a593Smuzhiyun
1179*4882a593Smuzhiyun if (cs >= ANFC_MAX_CS || rb >= ANFC_MAX_CS) {
1180*4882a593Smuzhiyun dev_err(nfc->dev, "Wrong CS %d or RB %d\n", cs, rb);
1181*4882a593Smuzhiyun return -EINVAL;
1182*4882a593Smuzhiyun }
1183*4882a593Smuzhiyun
1184*4882a593Smuzhiyun if (test_and_set_bit(cs, &nfc->assigned_cs)) {
1185*4882a593Smuzhiyun dev_err(nfc->dev, "Already assigned CS %d\n", cs);
1186*4882a593Smuzhiyun return -EINVAL;
1187*4882a593Smuzhiyun }
1188*4882a593Smuzhiyun
1189*4882a593Smuzhiyun anand->cs = cs;
1190*4882a593Smuzhiyun anand->rb = rb;
1191*4882a593Smuzhiyun
1192*4882a593Smuzhiyun chip = &anand->chip;
1193*4882a593Smuzhiyun mtd = nand_to_mtd(chip);
1194*4882a593Smuzhiyun mtd->dev.parent = nfc->dev;
1195*4882a593Smuzhiyun chip->controller = &nfc->controller;
1196*4882a593Smuzhiyun chip->options = NAND_BUSWIDTH_AUTO | NAND_NO_SUBPAGE_WRITE |
1197*4882a593Smuzhiyun NAND_USES_DMA;
1198*4882a593Smuzhiyun
1199*4882a593Smuzhiyun nand_set_flash_node(chip, np);
1200*4882a593Smuzhiyun if (!mtd->name) {
1201*4882a593Smuzhiyun dev_err(nfc->dev, "NAND label property is mandatory\n");
1202*4882a593Smuzhiyun return -EINVAL;
1203*4882a593Smuzhiyun }
1204*4882a593Smuzhiyun
1205*4882a593Smuzhiyun ret = nand_scan(chip, 1);
1206*4882a593Smuzhiyun if (ret) {
1207*4882a593Smuzhiyun dev_err(nfc->dev, "Scan operation failed\n");
1208*4882a593Smuzhiyun return ret;
1209*4882a593Smuzhiyun }
1210*4882a593Smuzhiyun
1211*4882a593Smuzhiyun ret = mtd_device_register(mtd, NULL, 0);
1212*4882a593Smuzhiyun if (ret) {
1213*4882a593Smuzhiyun nand_cleanup(chip);
1214*4882a593Smuzhiyun return ret;
1215*4882a593Smuzhiyun }
1216*4882a593Smuzhiyun
1217*4882a593Smuzhiyun list_add_tail(&anand->node, &nfc->chips);
1218*4882a593Smuzhiyun
1219*4882a593Smuzhiyun return 0;
1220*4882a593Smuzhiyun }
1221*4882a593Smuzhiyun
anfc_chips_cleanup(struct arasan_nfc * nfc)1222*4882a593Smuzhiyun static void anfc_chips_cleanup(struct arasan_nfc *nfc)
1223*4882a593Smuzhiyun {
1224*4882a593Smuzhiyun struct anand *anand, *tmp;
1225*4882a593Smuzhiyun struct nand_chip *chip;
1226*4882a593Smuzhiyun int ret;
1227*4882a593Smuzhiyun
1228*4882a593Smuzhiyun list_for_each_entry_safe(anand, tmp, &nfc->chips, node) {
1229*4882a593Smuzhiyun chip = &anand->chip;
1230*4882a593Smuzhiyun ret = mtd_device_unregister(nand_to_mtd(chip));
1231*4882a593Smuzhiyun WARN_ON(ret);
1232*4882a593Smuzhiyun nand_cleanup(chip);
1233*4882a593Smuzhiyun list_del(&anand->node);
1234*4882a593Smuzhiyun }
1235*4882a593Smuzhiyun }
1236*4882a593Smuzhiyun
anfc_chips_init(struct arasan_nfc * nfc)1237*4882a593Smuzhiyun static int anfc_chips_init(struct arasan_nfc *nfc)
1238*4882a593Smuzhiyun {
1239*4882a593Smuzhiyun struct device_node *np = nfc->dev->of_node, *nand_np;
1240*4882a593Smuzhiyun int nchips = of_get_child_count(np);
1241*4882a593Smuzhiyun int ret;
1242*4882a593Smuzhiyun
1243*4882a593Smuzhiyun if (!nchips || nchips > ANFC_MAX_CS) {
1244*4882a593Smuzhiyun dev_err(nfc->dev, "Incorrect number of NAND chips (%d)\n",
1245*4882a593Smuzhiyun nchips);
1246*4882a593Smuzhiyun return -EINVAL;
1247*4882a593Smuzhiyun }
1248*4882a593Smuzhiyun
1249*4882a593Smuzhiyun for_each_child_of_node(np, nand_np) {
1250*4882a593Smuzhiyun ret = anfc_chip_init(nfc, nand_np);
1251*4882a593Smuzhiyun if (ret) {
1252*4882a593Smuzhiyun of_node_put(nand_np);
1253*4882a593Smuzhiyun anfc_chips_cleanup(nfc);
1254*4882a593Smuzhiyun break;
1255*4882a593Smuzhiyun }
1256*4882a593Smuzhiyun }
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun return ret;
1259*4882a593Smuzhiyun }
1260*4882a593Smuzhiyun
anfc_reset(struct arasan_nfc * nfc)1261*4882a593Smuzhiyun static void anfc_reset(struct arasan_nfc *nfc)
1262*4882a593Smuzhiyun {
1263*4882a593Smuzhiyun /* Disable interrupt signals */
1264*4882a593Smuzhiyun writel_relaxed(0, nfc->base + INTR_SIG_EN_REG);
1265*4882a593Smuzhiyun
1266*4882a593Smuzhiyun /* Enable interrupt status */
1267*4882a593Smuzhiyun writel_relaxed(EVENT_MASK, nfc->base + INTR_STS_EN_REG);
1268*4882a593Smuzhiyun }
1269*4882a593Smuzhiyun
anfc_probe(struct platform_device * pdev)1270*4882a593Smuzhiyun static int anfc_probe(struct platform_device *pdev)
1271*4882a593Smuzhiyun {
1272*4882a593Smuzhiyun struct arasan_nfc *nfc;
1273*4882a593Smuzhiyun int ret;
1274*4882a593Smuzhiyun
1275*4882a593Smuzhiyun nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
1276*4882a593Smuzhiyun if (!nfc)
1277*4882a593Smuzhiyun return -ENOMEM;
1278*4882a593Smuzhiyun
1279*4882a593Smuzhiyun nfc->dev = &pdev->dev;
1280*4882a593Smuzhiyun nand_controller_init(&nfc->controller);
1281*4882a593Smuzhiyun nfc->controller.ops = &anfc_ops;
1282*4882a593Smuzhiyun INIT_LIST_HEAD(&nfc->chips);
1283*4882a593Smuzhiyun
1284*4882a593Smuzhiyun nfc->base = devm_platform_ioremap_resource(pdev, 0);
1285*4882a593Smuzhiyun if (IS_ERR(nfc->base))
1286*4882a593Smuzhiyun return PTR_ERR(nfc->base);
1287*4882a593Smuzhiyun
1288*4882a593Smuzhiyun anfc_reset(nfc);
1289*4882a593Smuzhiyun
1290*4882a593Smuzhiyun nfc->controller_clk = devm_clk_get(&pdev->dev, "controller");
1291*4882a593Smuzhiyun if (IS_ERR(nfc->controller_clk))
1292*4882a593Smuzhiyun return PTR_ERR(nfc->controller_clk);
1293*4882a593Smuzhiyun
1294*4882a593Smuzhiyun nfc->bus_clk = devm_clk_get(&pdev->dev, "bus");
1295*4882a593Smuzhiyun if (IS_ERR(nfc->bus_clk))
1296*4882a593Smuzhiyun return PTR_ERR(nfc->bus_clk);
1297*4882a593Smuzhiyun
1298*4882a593Smuzhiyun ret = clk_prepare_enable(nfc->controller_clk);
1299*4882a593Smuzhiyun if (ret)
1300*4882a593Smuzhiyun return ret;
1301*4882a593Smuzhiyun
1302*4882a593Smuzhiyun ret = clk_prepare_enable(nfc->bus_clk);
1303*4882a593Smuzhiyun if (ret)
1304*4882a593Smuzhiyun goto disable_controller_clk;
1305*4882a593Smuzhiyun
1306*4882a593Smuzhiyun ret = anfc_chips_init(nfc);
1307*4882a593Smuzhiyun if (ret)
1308*4882a593Smuzhiyun goto disable_bus_clk;
1309*4882a593Smuzhiyun
1310*4882a593Smuzhiyun platform_set_drvdata(pdev, nfc);
1311*4882a593Smuzhiyun
1312*4882a593Smuzhiyun return 0;
1313*4882a593Smuzhiyun
1314*4882a593Smuzhiyun disable_bus_clk:
1315*4882a593Smuzhiyun clk_disable_unprepare(nfc->bus_clk);
1316*4882a593Smuzhiyun
1317*4882a593Smuzhiyun disable_controller_clk:
1318*4882a593Smuzhiyun clk_disable_unprepare(nfc->controller_clk);
1319*4882a593Smuzhiyun
1320*4882a593Smuzhiyun return ret;
1321*4882a593Smuzhiyun }
1322*4882a593Smuzhiyun
anfc_remove(struct platform_device * pdev)1323*4882a593Smuzhiyun static int anfc_remove(struct platform_device *pdev)
1324*4882a593Smuzhiyun {
1325*4882a593Smuzhiyun struct arasan_nfc *nfc = platform_get_drvdata(pdev);
1326*4882a593Smuzhiyun
1327*4882a593Smuzhiyun anfc_chips_cleanup(nfc);
1328*4882a593Smuzhiyun
1329*4882a593Smuzhiyun clk_disable_unprepare(nfc->bus_clk);
1330*4882a593Smuzhiyun clk_disable_unprepare(nfc->controller_clk);
1331*4882a593Smuzhiyun
1332*4882a593Smuzhiyun return 0;
1333*4882a593Smuzhiyun }
1334*4882a593Smuzhiyun
1335*4882a593Smuzhiyun static const struct of_device_id anfc_ids[] = {
1336*4882a593Smuzhiyun {
1337*4882a593Smuzhiyun .compatible = "xlnx,zynqmp-nand-controller",
1338*4882a593Smuzhiyun },
1339*4882a593Smuzhiyun {
1340*4882a593Smuzhiyun .compatible = "arasan,nfc-v3p10",
1341*4882a593Smuzhiyun },
1342*4882a593Smuzhiyun {}
1343*4882a593Smuzhiyun };
1344*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, anfc_ids);
1345*4882a593Smuzhiyun
1346*4882a593Smuzhiyun static struct platform_driver anfc_driver = {
1347*4882a593Smuzhiyun .driver = {
1348*4882a593Smuzhiyun .name = "arasan-nand-controller",
1349*4882a593Smuzhiyun .of_match_table = anfc_ids,
1350*4882a593Smuzhiyun },
1351*4882a593Smuzhiyun .probe = anfc_probe,
1352*4882a593Smuzhiyun .remove = anfc_remove,
1353*4882a593Smuzhiyun };
1354*4882a593Smuzhiyun module_platform_driver(anfc_driver);
1355*4882a593Smuzhiyun
1356*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
1357*4882a593Smuzhiyun MODULE_AUTHOR("Punnaiah Choudary Kalluri <punnaia@xilinx.com>");
1358*4882a593Smuzhiyun MODULE_AUTHOR("Naga Sureshkumar Relli <nagasure@xilinx.com>");
1359*4882a593Smuzhiyun MODULE_AUTHOR("Miquel Raynal <miquel.raynal@bootlin.com>");
1360*4882a593Smuzhiyun MODULE_DESCRIPTION("Arasan NAND Flash Controller Driver");
1361