1197b88feSMiquel Raynal // SPDX-License-Identifier: GPL-2.0
2197b88feSMiquel Raynal /*
3197b88feSMiquel Raynal * Arasan NAND Flash Controller Driver
4197b88feSMiquel Raynal *
5197b88feSMiquel Raynal * Copyright (C) 2014 - 2020 Xilinx, Inc.
6197b88feSMiquel Raynal * Author:
7197b88feSMiquel Raynal * Miquel Raynal <miquel.raynal@bootlin.com>
8197b88feSMiquel Raynal * Original work (fully rewritten):
9197b88feSMiquel Raynal * Punnaiah Choudary Kalluri <punnaia@xilinx.com>
10197b88feSMiquel Raynal * Naga Sureshkumar Relli <nagasure@xilinx.com>
11197b88feSMiquel Raynal */
12197b88feSMiquel Raynal
1388ffef1bSMiquel Raynal #include <linux/bch.h>
14197b88feSMiquel Raynal #include <linux/bitfield.h>
15197b88feSMiquel Raynal #include <linux/clk.h>
16197b88feSMiquel Raynal #include <linux/delay.h>
17197b88feSMiquel Raynal #include <linux/dma-mapping.h>
18acbd3d09SMiquel Raynal #include <linux/gpio/consumer.h>
19197b88feSMiquel Raynal #include <linux/interrupt.h>
20197b88feSMiquel Raynal #include <linux/iopoll.h>
21197b88feSMiquel Raynal #include <linux/module.h>
22197b88feSMiquel Raynal #include <linux/mtd/mtd.h>
23197b88feSMiquel Raynal #include <linux/mtd/partitions.h>
24197b88feSMiquel Raynal #include <linux/mtd/rawnand.h>
25197b88feSMiquel Raynal #include <linux/of.h>
26197b88feSMiquel Raynal #include <linux/platform_device.h>
27197b88feSMiquel Raynal #include <linux/slab.h>
28197b88feSMiquel Raynal
29197b88feSMiquel Raynal #define PKT_REG 0x00
30197b88feSMiquel Raynal #define PKT_SIZE(x) FIELD_PREP(GENMASK(10, 0), (x))
31197b88feSMiquel Raynal #define PKT_STEPS(x) FIELD_PREP(GENMASK(23, 12), (x))
32197b88feSMiquel Raynal
33197b88feSMiquel Raynal #define MEM_ADDR1_REG 0x04
34197b88feSMiquel Raynal
35197b88feSMiquel Raynal #define MEM_ADDR2_REG 0x08
36197b88feSMiquel Raynal #define ADDR2_STRENGTH(x) FIELD_PREP(GENMASK(27, 25), (x))
37197b88feSMiquel Raynal #define ADDR2_CS(x) FIELD_PREP(GENMASK(31, 30), (x))
38197b88feSMiquel Raynal
39197b88feSMiquel Raynal #define CMD_REG 0x0C
40197b88feSMiquel Raynal #define CMD_1(x) FIELD_PREP(GENMASK(7, 0), (x))
41197b88feSMiquel Raynal #define CMD_2(x) FIELD_PREP(GENMASK(15, 8), (x))
42197b88feSMiquel Raynal #define CMD_PAGE_SIZE(x) FIELD_PREP(GENMASK(25, 23), (x))
43197b88feSMiquel Raynal #define CMD_DMA_ENABLE BIT(27)
44197b88feSMiquel Raynal #define CMD_NADDRS(x) FIELD_PREP(GENMASK(30, 28), (x))
45197b88feSMiquel Raynal #define CMD_ECC_ENABLE BIT(31)
46197b88feSMiquel Raynal
47197b88feSMiquel Raynal #define PROG_REG 0x10
48197b88feSMiquel Raynal #define PROG_PGRD BIT(0)
49197b88feSMiquel Raynal #define PROG_ERASE BIT(2)
50197b88feSMiquel Raynal #define PROG_STATUS BIT(3)
51197b88feSMiquel Raynal #define PROG_PGPROG BIT(4)
52197b88feSMiquel Raynal #define PROG_RDID BIT(6)
53197b88feSMiquel Raynal #define PROG_RDPARAM BIT(7)
54197b88feSMiquel Raynal #define PROG_RST BIT(8)
55197b88feSMiquel Raynal #define PROG_GET_FEATURE BIT(9)
56197b88feSMiquel Raynal #define PROG_SET_FEATURE BIT(10)
5710938a08SMiquel Raynal #define PROG_CHG_RD_COL_ENH BIT(14)
58197b88feSMiquel Raynal
59197b88feSMiquel Raynal #define INTR_STS_EN_REG 0x14
60197b88feSMiquel Raynal #define INTR_SIG_EN_REG 0x18
61197b88feSMiquel Raynal #define INTR_STS_REG 0x1C
62197b88feSMiquel Raynal #define WRITE_READY BIT(0)
63197b88feSMiquel Raynal #define READ_READY BIT(1)
64197b88feSMiquel Raynal #define XFER_COMPLETE BIT(2)
65197b88feSMiquel Raynal #define DMA_BOUNDARY BIT(6)
66197b88feSMiquel Raynal #define EVENT_MASK GENMASK(7, 0)
67197b88feSMiquel Raynal
68197b88feSMiquel Raynal #define READY_STS_REG 0x20
69197b88feSMiquel Raynal
70197b88feSMiquel Raynal #define DMA_ADDR0_REG 0x50
71197b88feSMiquel Raynal #define DMA_ADDR1_REG 0x24
72197b88feSMiquel Raynal
73197b88feSMiquel Raynal #define FLASH_STS_REG 0x28
74197b88feSMiquel Raynal
7576e12c10SMiquel Raynal #define TIMING_REG 0x2C
7676e12c10SMiquel Raynal #define TCCS_TIME_500NS 0
7776e12c10SMiquel Raynal #define TCCS_TIME_300NS 3
7876e12c10SMiquel Raynal #define TCCS_TIME_200NS 2
7976e12c10SMiquel Raynal #define TCCS_TIME_100NS 1
8076e12c10SMiquel Raynal #define FAST_TCAD BIT(2)
8176e12c10SMiquel Raynal #define DQS_BUFF_SEL_IN(x) FIELD_PREP(GENMASK(6, 3), (x))
8276e12c10SMiquel Raynal #define DQS_BUFF_SEL_OUT(x) FIELD_PREP(GENMASK(18, 15), (x))
8376e12c10SMiquel Raynal
84197b88feSMiquel Raynal #define DATA_PORT_REG 0x30
85197b88feSMiquel Raynal
86197b88feSMiquel Raynal #define ECC_CONF_REG 0x34
87197b88feSMiquel Raynal #define ECC_CONF_COL(x) FIELD_PREP(GENMASK(15, 0), (x))
88197b88feSMiquel Raynal #define ECC_CONF_LEN(x) FIELD_PREP(GENMASK(26, 16), (x))
89197b88feSMiquel Raynal #define ECC_CONF_BCH_EN BIT(27)
90197b88feSMiquel Raynal
91197b88feSMiquel Raynal #define ECC_ERR_CNT_REG 0x38
92197b88feSMiquel Raynal #define GET_PKT_ERR_CNT(x) FIELD_GET(GENMASK(7, 0), (x))
93197b88feSMiquel Raynal #define GET_PAGE_ERR_CNT(x) FIELD_GET(GENMASK(16, 8), (x))
94197b88feSMiquel Raynal
95197b88feSMiquel Raynal #define ECC_SP_REG 0x3C
96197b88feSMiquel Raynal #define ECC_SP_CMD1(x) FIELD_PREP(GENMASK(7, 0), (x))
97197b88feSMiquel Raynal #define ECC_SP_CMD2(x) FIELD_PREP(GENMASK(15, 8), (x))
98197b88feSMiquel Raynal #define ECC_SP_ADDRS(x) FIELD_PREP(GENMASK(30, 28), (x))
99197b88feSMiquel Raynal
100197b88feSMiquel Raynal #define ECC_1ERR_CNT_REG 0x40
101197b88feSMiquel Raynal #define ECC_2ERR_CNT_REG 0x44
102197b88feSMiquel Raynal
103197b88feSMiquel Raynal #define DATA_INTERFACE_REG 0x6C
104197b88feSMiquel Raynal #define DIFACE_SDR_MODE(x) FIELD_PREP(GENMASK(2, 0), (x))
105698ddeb8SMiquel Raynal #define DIFACE_DDR_MODE(x) FIELD_PREP(GENMASK(5, 3), (x))
106197b88feSMiquel Raynal #define DIFACE_SDR 0
107197b88feSMiquel Raynal #define DIFACE_NVDDR BIT(9)
108197b88feSMiquel Raynal
109197b88feSMiquel Raynal #define ANFC_MAX_CS 2
110197b88feSMiquel Raynal #define ANFC_DFLT_TIMEOUT_US 1000000
111197b88feSMiquel Raynal #define ANFC_MAX_CHUNK_SIZE SZ_1M
112197b88feSMiquel Raynal #define ANFC_MAX_PARAM_SIZE SZ_4K
113197b88feSMiquel Raynal #define ANFC_MAX_STEPS SZ_2K
114197b88feSMiquel Raynal #define ANFC_MAX_PKT_SIZE (SZ_2K - 1)
115197b88feSMiquel Raynal #define ANFC_MAX_ADDR_CYC 5U
116197b88feSMiquel Raynal #define ANFC_RSVD_ECC_BYTES 21
117197b88feSMiquel Raynal
118197b88feSMiquel Raynal #define ANFC_XLNX_SDR_DFLT_CORE_CLK 100000000
119197b88feSMiquel Raynal #define ANFC_XLNX_SDR_HS_CORE_CLK 80000000
120197b88feSMiquel Raynal
121acbd3d09SMiquel Raynal static struct gpio_desc *anfc_default_cs_array[2] = {NULL, NULL};
122acbd3d09SMiquel Raynal
123197b88feSMiquel Raynal /**
124197b88feSMiquel Raynal * struct anfc_op - Defines how to execute an operation
125197b88feSMiquel Raynal * @pkt_reg: Packet register
126197b88feSMiquel Raynal * @addr1_reg: Memory address 1 register
127197b88feSMiquel Raynal * @addr2_reg: Memory address 2 register
128197b88feSMiquel Raynal * @cmd_reg: Command register
129197b88feSMiquel Raynal * @prog_reg: Program register
130197b88feSMiquel Raynal * @steps: Number of "packets" to read/write
131197b88feSMiquel Raynal * @rdy_timeout_ms: Timeout for waits on Ready/Busy pin
132197b88feSMiquel Raynal * @len: Data transfer length
133197b88feSMiquel Raynal * @read: Data transfer direction from the controller point of view
134b489681bSLee Jones * @buf: Data buffer
135197b88feSMiquel Raynal */
136197b88feSMiquel Raynal struct anfc_op {
137197b88feSMiquel Raynal u32 pkt_reg;
138197b88feSMiquel Raynal u32 addr1_reg;
139197b88feSMiquel Raynal u32 addr2_reg;
140197b88feSMiquel Raynal u32 cmd_reg;
141197b88feSMiquel Raynal u32 prog_reg;
142197b88feSMiquel Raynal int steps;
143197b88feSMiquel Raynal unsigned int rdy_timeout_ms;
144197b88feSMiquel Raynal unsigned int len;
145197b88feSMiquel Raynal bool read;
146197b88feSMiquel Raynal u8 *buf;
147197b88feSMiquel Raynal };
148197b88feSMiquel Raynal
149197b88feSMiquel Raynal /**
150197b88feSMiquel Raynal * struct anand - Defines the NAND chip related information
151197b88feSMiquel Raynal * @node: Used to store NAND chips into a list
152197b88feSMiquel Raynal * @chip: NAND chip information structure
153197b88feSMiquel Raynal * @rb: Ready-busy line
154197b88feSMiquel Raynal * @page_sz: Register value of the page_sz field to use
155197b88feSMiquel Raynal * @clk: Expected clock frequency to use
15623739c34SMiquel Raynal * @data_iface: Data interface timing mode to use
15776e12c10SMiquel Raynal * @timings: NV-DDR specific timings to use
158197b88feSMiquel Raynal * @ecc_conf: Hardware ECC configuration value
159197b88feSMiquel Raynal * @strength: Register value of the ECC strength
160197b88feSMiquel Raynal * @raddr_cycles: Row address cycle information
161197b88feSMiquel Raynal * @caddr_cycles: Column address cycle information
16288ffef1bSMiquel Raynal * @ecc_bits: Exact number of ECC bits per syndrome
16388ffef1bSMiquel Raynal * @ecc_total: Total number of ECC bytes
16488ffef1bSMiquel Raynal * @errloc: Array of errors located with soft BCH
16588ffef1bSMiquel Raynal * @hw_ecc: Buffer to store syndromes computed by hardware
16688ffef1bSMiquel Raynal * @bch: BCH structure
167acbd3d09SMiquel Raynal * @cs_idx: Array of chip-select for this device, values are indexes
168acbd3d09SMiquel Raynal * of the controller structure @gpio_cs array
169acbd3d09SMiquel Raynal * @ncs_idx: Size of the @cs_idx array
170197b88feSMiquel Raynal */
171197b88feSMiquel Raynal struct anand {
172197b88feSMiquel Raynal struct list_head node;
173197b88feSMiquel Raynal struct nand_chip chip;
174197b88feSMiquel Raynal unsigned int rb;
175197b88feSMiquel Raynal unsigned int page_sz;
176197b88feSMiquel Raynal unsigned long clk;
17723739c34SMiquel Raynal u32 data_iface;
17876e12c10SMiquel Raynal u32 timings;
179197b88feSMiquel Raynal u32 ecc_conf;
180197b88feSMiquel Raynal u32 strength;
181197b88feSMiquel Raynal u16 raddr_cycles;
182197b88feSMiquel Raynal u16 caddr_cycles;
18388ffef1bSMiquel Raynal unsigned int ecc_bits;
18488ffef1bSMiquel Raynal unsigned int ecc_total;
18588ffef1bSMiquel Raynal unsigned int *errloc;
18688ffef1bSMiquel Raynal u8 *hw_ecc;
18788ffef1bSMiquel Raynal struct bch_control *bch;
188acbd3d09SMiquel Raynal int *cs_idx;
189acbd3d09SMiquel Raynal int ncs_idx;
190197b88feSMiquel Raynal };
191197b88feSMiquel Raynal
192197b88feSMiquel Raynal /**
193197b88feSMiquel Raynal * struct arasan_nfc - Defines the Arasan NAND flash controller driver instance
194197b88feSMiquel Raynal * @dev: Pointer to the device structure
195197b88feSMiquel Raynal * @base: Remapped register area
196197b88feSMiquel Raynal * @controller_clk: Pointer to the system clock
197197b88feSMiquel Raynal * @bus_clk: Pointer to the flash clock
198197b88feSMiquel Raynal * @controller: Base controller structure
199197b88feSMiquel Raynal * @chips: List of all NAND chips attached to the controller
200197b88feSMiquel Raynal * @cur_clk: Current clock rate
201acbd3d09SMiquel Raynal * @cs_array: CS array. Native CS are left empty, the other cells are
202acbd3d09SMiquel Raynal * populated with their corresponding GPIO descriptor.
203acbd3d09SMiquel Raynal * @ncs: Size of @cs_array
204acbd3d09SMiquel Raynal * @cur_cs: Index in @cs_array of the currently in use CS
205acbd3d09SMiquel Raynal * @native_cs: Currently selected native CS
206acbd3d09SMiquel Raynal * @spare_cs: Native CS that is not wired (may be selected when a GPIO
207acbd3d09SMiquel Raynal * CS is in use)
208197b88feSMiquel Raynal */
209197b88feSMiquel Raynal struct arasan_nfc {
210197b88feSMiquel Raynal struct device *dev;
211197b88feSMiquel Raynal void __iomem *base;
212197b88feSMiquel Raynal struct clk *controller_clk;
213197b88feSMiquel Raynal struct clk *bus_clk;
214197b88feSMiquel Raynal struct nand_controller controller;
215197b88feSMiquel Raynal struct list_head chips;
216197b88feSMiquel Raynal unsigned int cur_clk;
217acbd3d09SMiquel Raynal struct gpio_desc **cs_array;
218acbd3d09SMiquel Raynal unsigned int ncs;
219acbd3d09SMiquel Raynal int cur_cs;
220acbd3d09SMiquel Raynal unsigned int native_cs;
221acbd3d09SMiquel Raynal unsigned int spare_cs;
222197b88feSMiquel Raynal };
223197b88feSMiquel Raynal
to_anand(struct nand_chip * nand)224197b88feSMiquel Raynal static struct anand *to_anand(struct nand_chip *nand)
225197b88feSMiquel Raynal {
226197b88feSMiquel Raynal return container_of(nand, struct anand, chip);
227197b88feSMiquel Raynal }
228197b88feSMiquel Raynal
to_anfc(struct nand_controller * ctrl)229197b88feSMiquel Raynal static struct arasan_nfc *to_anfc(struct nand_controller *ctrl)
230197b88feSMiquel Raynal {
231197b88feSMiquel Raynal return container_of(ctrl, struct arasan_nfc, controller);
232197b88feSMiquel Raynal }
233197b88feSMiquel Raynal
anfc_wait_for_event(struct arasan_nfc * nfc,unsigned int event)234197b88feSMiquel Raynal static int anfc_wait_for_event(struct arasan_nfc *nfc, unsigned int event)
235197b88feSMiquel Raynal {
236197b88feSMiquel Raynal u32 val;
237197b88feSMiquel Raynal int ret;
238197b88feSMiquel Raynal
239197b88feSMiquel Raynal ret = readl_relaxed_poll_timeout(nfc->base + INTR_STS_REG, val,
240197b88feSMiquel Raynal val & event, 0,
241197b88feSMiquel Raynal ANFC_DFLT_TIMEOUT_US);
242197b88feSMiquel Raynal if (ret) {
243197b88feSMiquel Raynal dev_err(nfc->dev, "Timeout waiting for event 0x%x\n", event);
244197b88feSMiquel Raynal return -ETIMEDOUT;
245197b88feSMiquel Raynal }
246197b88feSMiquel Raynal
247197b88feSMiquel Raynal writel_relaxed(event, nfc->base + INTR_STS_REG);
248197b88feSMiquel Raynal
249197b88feSMiquel Raynal return 0;
250197b88feSMiquel Raynal }
251197b88feSMiquel Raynal
anfc_wait_for_rb(struct arasan_nfc * nfc,struct nand_chip * chip,unsigned int timeout_ms)252197b88feSMiquel Raynal static int anfc_wait_for_rb(struct arasan_nfc *nfc, struct nand_chip *chip,
253197b88feSMiquel Raynal unsigned int timeout_ms)
254197b88feSMiquel Raynal {
255197b88feSMiquel Raynal struct anand *anand = to_anand(chip);
256197b88feSMiquel Raynal u32 val;
257197b88feSMiquel Raynal int ret;
258197b88feSMiquel Raynal
259197b88feSMiquel Raynal /* There is no R/B interrupt, we must poll a register */
260197b88feSMiquel Raynal ret = readl_relaxed_poll_timeout(nfc->base + READY_STS_REG, val,
261197b88feSMiquel Raynal val & BIT(anand->rb),
262197b88feSMiquel Raynal 1, timeout_ms * 1000);
263197b88feSMiquel Raynal if (ret) {
264197b88feSMiquel Raynal dev_err(nfc->dev, "Timeout waiting for R/B 0x%x\n",
265197b88feSMiquel Raynal readl_relaxed(nfc->base + READY_STS_REG));
266197b88feSMiquel Raynal return -ETIMEDOUT;
267197b88feSMiquel Raynal }
268197b88feSMiquel Raynal
269197b88feSMiquel Raynal return 0;
270197b88feSMiquel Raynal }
271197b88feSMiquel Raynal
anfc_trigger_op(struct arasan_nfc * nfc,struct anfc_op * nfc_op)272197b88feSMiquel Raynal static void anfc_trigger_op(struct arasan_nfc *nfc, struct anfc_op *nfc_op)
273197b88feSMiquel Raynal {
274197b88feSMiquel Raynal writel_relaxed(nfc_op->pkt_reg, nfc->base + PKT_REG);
275197b88feSMiquel Raynal writel_relaxed(nfc_op->addr1_reg, nfc->base + MEM_ADDR1_REG);
276197b88feSMiquel Raynal writel_relaxed(nfc_op->addr2_reg, nfc->base + MEM_ADDR2_REG);
277197b88feSMiquel Raynal writel_relaxed(nfc_op->cmd_reg, nfc->base + CMD_REG);
278197b88feSMiquel Raynal writel_relaxed(nfc_op->prog_reg, nfc->base + PROG_REG);
279197b88feSMiquel Raynal }
280197b88feSMiquel Raynal
anfc_pkt_len_config(unsigned int len,unsigned int * steps,unsigned int * pktsize)281197b88feSMiquel Raynal static int anfc_pkt_len_config(unsigned int len, unsigned int *steps,
282197b88feSMiquel Raynal unsigned int *pktsize)
283197b88feSMiquel Raynal {
284197b88feSMiquel Raynal unsigned int nb, sz;
285197b88feSMiquel Raynal
286197b88feSMiquel Raynal for (nb = 1; nb < ANFC_MAX_STEPS; nb *= 2) {
287197b88feSMiquel Raynal sz = len / nb;
288197b88feSMiquel Raynal if (sz <= ANFC_MAX_PKT_SIZE)
289197b88feSMiquel Raynal break;
290197b88feSMiquel Raynal }
291197b88feSMiquel Raynal
292197b88feSMiquel Raynal if (sz * nb != len)
293197b88feSMiquel Raynal return -ENOTSUPP;
294197b88feSMiquel Raynal
295197b88feSMiquel Raynal if (steps)
296197b88feSMiquel Raynal *steps = nb;
297197b88feSMiquel Raynal
298197b88feSMiquel Raynal if (pktsize)
299197b88feSMiquel Raynal *pktsize = sz;
300197b88feSMiquel Raynal
301197b88feSMiquel Raynal return 0;
302197b88feSMiquel Raynal }
303197b88feSMiquel Raynal
anfc_is_gpio_cs(struct arasan_nfc * nfc,int nfc_cs)304acbd3d09SMiquel Raynal static bool anfc_is_gpio_cs(struct arasan_nfc *nfc, int nfc_cs)
305acbd3d09SMiquel Raynal {
306acbd3d09SMiquel Raynal return nfc_cs >= 0 && nfc->cs_array[nfc_cs];
307acbd3d09SMiquel Raynal }
308acbd3d09SMiquel Raynal
anfc_relative_to_absolute_cs(struct anand * anand,int num)309acbd3d09SMiquel Raynal static int anfc_relative_to_absolute_cs(struct anand *anand, int num)
310acbd3d09SMiquel Raynal {
311acbd3d09SMiquel Raynal return anand->cs_idx[num];
312acbd3d09SMiquel Raynal }
313acbd3d09SMiquel Raynal
anfc_assert_cs(struct arasan_nfc * nfc,unsigned int nfc_cs_idx)314acbd3d09SMiquel Raynal static void anfc_assert_cs(struct arasan_nfc *nfc, unsigned int nfc_cs_idx)
315acbd3d09SMiquel Raynal {
316acbd3d09SMiquel Raynal /* CS did not change: do nothing */
317acbd3d09SMiquel Raynal if (nfc->cur_cs == nfc_cs_idx)
318acbd3d09SMiquel Raynal return;
319acbd3d09SMiquel Raynal
320acbd3d09SMiquel Raynal /* Deassert the previous CS if it was a GPIO */
321acbd3d09SMiquel Raynal if (anfc_is_gpio_cs(nfc, nfc->cur_cs))
322acbd3d09SMiquel Raynal gpiod_set_value_cansleep(nfc->cs_array[nfc->cur_cs], 1);
323acbd3d09SMiquel Raynal
324acbd3d09SMiquel Raynal /* Assert the new one */
325acbd3d09SMiquel Raynal if (anfc_is_gpio_cs(nfc, nfc_cs_idx)) {
326acbd3d09SMiquel Raynal nfc->native_cs = nfc->spare_cs;
327acbd3d09SMiquel Raynal gpiod_set_value_cansleep(nfc->cs_array[nfc_cs_idx], 0);
328acbd3d09SMiquel Raynal } else {
329acbd3d09SMiquel Raynal nfc->native_cs = nfc_cs_idx;
330acbd3d09SMiquel Raynal }
331acbd3d09SMiquel Raynal
332acbd3d09SMiquel Raynal nfc->cur_cs = nfc_cs_idx;
333acbd3d09SMiquel Raynal }
334acbd3d09SMiquel Raynal
anfc_select_target(struct nand_chip * chip,int target)335b5437c7bSMiquel Raynal static int anfc_select_target(struct nand_chip *chip, int target)
336b5437c7bSMiquel Raynal {
337b5437c7bSMiquel Raynal struct anand *anand = to_anand(chip);
338b5437c7bSMiquel Raynal struct arasan_nfc *nfc = to_anfc(chip->controller);
339acbd3d09SMiquel Raynal unsigned int nfc_cs_idx = anfc_relative_to_absolute_cs(anand, target);
340b5437c7bSMiquel Raynal int ret;
341b5437c7bSMiquel Raynal
342acbd3d09SMiquel Raynal anfc_assert_cs(nfc, nfc_cs_idx);
343acbd3d09SMiquel Raynal
344b5437c7bSMiquel Raynal /* Update the controller timings and the potential ECC configuration */
34523739c34SMiquel Raynal writel_relaxed(anand->data_iface, nfc->base + DATA_INTERFACE_REG);
34676e12c10SMiquel Raynal writel_relaxed(anand->timings, nfc->base + TIMING_REG);
347b5437c7bSMiquel Raynal
348b5437c7bSMiquel Raynal /* Update clock frequency */
349b5437c7bSMiquel Raynal if (nfc->cur_clk != anand->clk) {
3507499bfeeSAmit Kumar Mahapatra clk_disable_unprepare(nfc->bus_clk);
3517499bfeeSAmit Kumar Mahapatra ret = clk_set_rate(nfc->bus_clk, anand->clk);
352b5437c7bSMiquel Raynal if (ret) {
353b5437c7bSMiquel Raynal dev_err(nfc->dev, "Failed to change clock rate\n");
354b5437c7bSMiquel Raynal return ret;
355b5437c7bSMiquel Raynal }
356b5437c7bSMiquel Raynal
3577499bfeeSAmit Kumar Mahapatra ret = clk_prepare_enable(nfc->bus_clk);
358b5437c7bSMiquel Raynal if (ret) {
359b5437c7bSMiquel Raynal dev_err(nfc->dev,
3607499bfeeSAmit Kumar Mahapatra "Failed to re-enable the bus clock\n");
361b5437c7bSMiquel Raynal return ret;
362b5437c7bSMiquel Raynal }
363b5437c7bSMiquel Raynal
364b5437c7bSMiquel Raynal nfc->cur_clk = anand->clk;
365b5437c7bSMiquel Raynal }
366b5437c7bSMiquel Raynal
367b5437c7bSMiquel Raynal return 0;
368b5437c7bSMiquel Raynal }
369b5437c7bSMiquel Raynal
37088ffef1bSMiquel Raynal /*
37188ffef1bSMiquel Raynal * When using the embedded hardware ECC engine, the controller is in charge of
37288ffef1bSMiquel Raynal * feeding the engine with, first, the ECC residue present in the data array.
37388ffef1bSMiquel Raynal * A typical read operation is:
37488ffef1bSMiquel Raynal * 1/ Assert the read operation by sending the relevant command/address cycles
37588ffef1bSMiquel Raynal * but targeting the column of the first ECC bytes in the OOB area instead of
37688ffef1bSMiquel Raynal * the main data directly.
37788ffef1bSMiquel Raynal * 2/ After having read the relevant number of ECC bytes, the controller uses
37888ffef1bSMiquel Raynal * the RNDOUT/RNDSTART commands which are set into the "ECC Spare Command
37988ffef1bSMiquel Raynal * Register" to move the pointer back at the beginning of the main data.
38088ffef1bSMiquel Raynal * 3/ It will read the content of the main area for a given size (pktsize) and
38188ffef1bSMiquel Raynal * will feed the ECC engine with this buffer again.
38288ffef1bSMiquel Raynal * 4/ The ECC engine derives the ECC bytes for the given data and compare them
38388ffef1bSMiquel Raynal * with the ones already received. It eventually trigger status flags and
38488ffef1bSMiquel Raynal * then set the "Buffer Read Ready" flag.
38588ffef1bSMiquel Raynal * 5/ The corrected data is then available for reading from the data port
38688ffef1bSMiquel Raynal * register.
38788ffef1bSMiquel Raynal *
38888ffef1bSMiquel Raynal * The hardware BCH ECC engine is known to be inconstent in BCH mode and never
38988ffef1bSMiquel Raynal * reports uncorrectable errors. Because of this bug, we have to use the
39088ffef1bSMiquel Raynal * software BCH implementation in the read path.
39188ffef1bSMiquel Raynal */
anfc_read_page_hw_ecc(struct nand_chip * chip,u8 * buf,int oob_required,int page)39288ffef1bSMiquel Raynal static int anfc_read_page_hw_ecc(struct nand_chip *chip, u8 *buf,
39388ffef1bSMiquel Raynal int oob_required, int page)
39488ffef1bSMiquel Raynal {
39588ffef1bSMiquel Raynal struct arasan_nfc *nfc = to_anfc(chip->controller);
39688ffef1bSMiquel Raynal struct mtd_info *mtd = nand_to_mtd(chip);
39788ffef1bSMiquel Raynal struct anand *anand = to_anand(chip);
39888ffef1bSMiquel Raynal unsigned int len = mtd->writesize + (oob_required ? mtd->oobsize : 0);
39988ffef1bSMiquel Raynal unsigned int max_bitflips = 0;
40088ffef1bSMiquel Raynal dma_addr_t dma_addr;
40188ffef1bSMiquel Raynal int step, ret;
40288ffef1bSMiquel Raynal struct anfc_op nfc_op = {
40388ffef1bSMiquel Raynal .pkt_reg =
40488ffef1bSMiquel Raynal PKT_SIZE(chip->ecc.size) |
40588ffef1bSMiquel Raynal PKT_STEPS(chip->ecc.steps),
40688ffef1bSMiquel Raynal .addr1_reg =
40788ffef1bSMiquel Raynal (page & 0xFF) << (8 * (anand->caddr_cycles)) |
40888ffef1bSMiquel Raynal (((page >> 8) & 0xFF) << (8 * (1 + anand->caddr_cycles))),
40988ffef1bSMiquel Raynal .addr2_reg =
41088ffef1bSMiquel Raynal ((page >> 16) & 0xFF) |
41188ffef1bSMiquel Raynal ADDR2_STRENGTH(anand->strength) |
412acbd3d09SMiquel Raynal ADDR2_CS(nfc->native_cs),
41388ffef1bSMiquel Raynal .cmd_reg =
41488ffef1bSMiquel Raynal CMD_1(NAND_CMD_READ0) |
41588ffef1bSMiquel Raynal CMD_2(NAND_CMD_READSTART) |
41688ffef1bSMiquel Raynal CMD_PAGE_SIZE(anand->page_sz) |
41788ffef1bSMiquel Raynal CMD_DMA_ENABLE |
41888ffef1bSMiquel Raynal CMD_NADDRS(anand->caddr_cycles +
41988ffef1bSMiquel Raynal anand->raddr_cycles),
42088ffef1bSMiquel Raynal .prog_reg = PROG_PGRD,
42188ffef1bSMiquel Raynal };
42288ffef1bSMiquel Raynal
42388ffef1bSMiquel Raynal dma_addr = dma_map_single(nfc->dev, (void *)buf, len, DMA_FROM_DEVICE);
42488ffef1bSMiquel Raynal if (dma_mapping_error(nfc->dev, dma_addr)) {
42588ffef1bSMiquel Raynal dev_err(nfc->dev, "Buffer mapping error");
42688ffef1bSMiquel Raynal return -EIO;
42788ffef1bSMiquel Raynal }
42888ffef1bSMiquel Raynal
42988ffef1bSMiquel Raynal writel_relaxed(lower_32_bits(dma_addr), nfc->base + DMA_ADDR0_REG);
43088ffef1bSMiquel Raynal writel_relaxed(upper_32_bits(dma_addr), nfc->base + DMA_ADDR1_REG);
43188ffef1bSMiquel Raynal
43288ffef1bSMiquel Raynal anfc_trigger_op(nfc, &nfc_op);
43388ffef1bSMiquel Raynal
43488ffef1bSMiquel Raynal ret = anfc_wait_for_event(nfc, XFER_COMPLETE);
43588ffef1bSMiquel Raynal dma_unmap_single(nfc->dev, dma_addr, len, DMA_FROM_DEVICE);
43688ffef1bSMiquel Raynal if (ret) {
43788ffef1bSMiquel Raynal dev_err(nfc->dev, "Error reading page %d\n", page);
43888ffef1bSMiquel Raynal return ret;
43988ffef1bSMiquel Raynal }
44088ffef1bSMiquel Raynal
44188ffef1bSMiquel Raynal /* Store the raw OOB bytes as well */
44288ffef1bSMiquel Raynal ret = nand_change_read_column_op(chip, mtd->writesize, chip->oob_poi,
44388ffef1bSMiquel Raynal mtd->oobsize, 0);
44488ffef1bSMiquel Raynal if (ret)
44588ffef1bSMiquel Raynal return ret;
44688ffef1bSMiquel Raynal
44788ffef1bSMiquel Raynal /*
44888ffef1bSMiquel Raynal * For each step, compute by softare the BCH syndrome over the raw data.
44988ffef1bSMiquel Raynal * Compare the theoretical amount of errors and compare with the
45088ffef1bSMiquel Raynal * hardware engine feedback.
45188ffef1bSMiquel Raynal */
45288ffef1bSMiquel Raynal for (step = 0; step < chip->ecc.steps; step++) {
45388ffef1bSMiquel Raynal u8 *raw_buf = &buf[step * chip->ecc.size];
45488ffef1bSMiquel Raynal unsigned int bit, byte;
45588ffef1bSMiquel Raynal int bf, i;
45688ffef1bSMiquel Raynal
45788ffef1bSMiquel Raynal /* Extract the syndrome, it is not necessarily aligned */
45888ffef1bSMiquel Raynal memset(anand->hw_ecc, 0, chip->ecc.bytes);
45988ffef1bSMiquel Raynal nand_extract_bits(anand->hw_ecc, 0,
46088ffef1bSMiquel Raynal &chip->oob_poi[mtd->oobsize - anand->ecc_total],
46188ffef1bSMiquel Raynal anand->ecc_bits * step, anand->ecc_bits);
46288ffef1bSMiquel Raynal
46388ffef1bSMiquel Raynal bf = bch_decode(anand->bch, raw_buf, chip->ecc.size,
46488ffef1bSMiquel Raynal anand->hw_ecc, NULL, NULL, anand->errloc);
46588ffef1bSMiquel Raynal if (!bf) {
46688ffef1bSMiquel Raynal continue;
46788ffef1bSMiquel Raynal } else if (bf > 0) {
46888ffef1bSMiquel Raynal for (i = 0; i < bf; i++) {
46988ffef1bSMiquel Raynal /* Only correct the data, not the syndrome */
47088ffef1bSMiquel Raynal if (anand->errloc[i] < (chip->ecc.size * 8)) {
47188ffef1bSMiquel Raynal bit = BIT(anand->errloc[i] & 7);
47288ffef1bSMiquel Raynal byte = anand->errloc[i] >> 3;
47388ffef1bSMiquel Raynal raw_buf[byte] ^= bit;
47488ffef1bSMiquel Raynal }
47588ffef1bSMiquel Raynal }
47688ffef1bSMiquel Raynal
47788ffef1bSMiquel Raynal mtd->ecc_stats.corrected += bf;
47888ffef1bSMiquel Raynal max_bitflips = max_t(unsigned int, max_bitflips, bf);
47988ffef1bSMiquel Raynal
48088ffef1bSMiquel Raynal continue;
48188ffef1bSMiquel Raynal }
48288ffef1bSMiquel Raynal
48388ffef1bSMiquel Raynal bf = nand_check_erased_ecc_chunk(raw_buf, chip->ecc.size,
48488ffef1bSMiquel Raynal NULL, 0, NULL, 0,
48588ffef1bSMiquel Raynal chip->ecc.strength);
48688ffef1bSMiquel Raynal if (bf > 0) {
48788ffef1bSMiquel Raynal mtd->ecc_stats.corrected += bf;
48888ffef1bSMiquel Raynal max_bitflips = max_t(unsigned int, max_bitflips, bf);
48988ffef1bSMiquel Raynal memset(raw_buf, 0xFF, chip->ecc.size);
49088ffef1bSMiquel Raynal } else if (bf < 0) {
49188ffef1bSMiquel Raynal mtd->ecc_stats.failed++;
49288ffef1bSMiquel Raynal }
49388ffef1bSMiquel Raynal }
49488ffef1bSMiquel Raynal
49588ffef1bSMiquel Raynal return 0;
49688ffef1bSMiquel Raynal }
49788ffef1bSMiquel Raynal
anfc_sel_read_page_hw_ecc(struct nand_chip * chip,u8 * buf,int oob_required,int page)498b5437c7bSMiquel Raynal static int anfc_sel_read_page_hw_ecc(struct nand_chip *chip, u8 *buf,
499b5437c7bSMiquel Raynal int oob_required, int page)
500b5437c7bSMiquel Raynal {
501b5437c7bSMiquel Raynal int ret;
502b5437c7bSMiquel Raynal
503b5437c7bSMiquel Raynal ret = anfc_select_target(chip, chip->cur_cs);
504b5437c7bSMiquel Raynal if (ret)
505b5437c7bSMiquel Raynal return ret;
506b5437c7bSMiquel Raynal
507b5437c7bSMiquel Raynal return anfc_read_page_hw_ecc(chip, buf, oob_required, page);
508b5437c7bSMiquel Raynal };
509b5437c7bSMiquel Raynal
anfc_write_page_hw_ecc(struct nand_chip * chip,const u8 * buf,int oob_required,int page)51088ffef1bSMiquel Raynal static int anfc_write_page_hw_ecc(struct nand_chip *chip, const u8 *buf,
51188ffef1bSMiquel Raynal int oob_required, int page)
51288ffef1bSMiquel Raynal {
51388ffef1bSMiquel Raynal struct anand *anand = to_anand(chip);
51488ffef1bSMiquel Raynal struct arasan_nfc *nfc = to_anfc(chip->controller);
51588ffef1bSMiquel Raynal struct mtd_info *mtd = nand_to_mtd(chip);
51688ffef1bSMiquel Raynal unsigned int len = mtd->writesize + (oob_required ? mtd->oobsize : 0);
51788ffef1bSMiquel Raynal dma_addr_t dma_addr;
5183a4a893dSMiquel Raynal u8 status;
51988ffef1bSMiquel Raynal int ret;
52088ffef1bSMiquel Raynal struct anfc_op nfc_op = {
52188ffef1bSMiquel Raynal .pkt_reg =
52288ffef1bSMiquel Raynal PKT_SIZE(chip->ecc.size) |
52388ffef1bSMiquel Raynal PKT_STEPS(chip->ecc.steps),
52488ffef1bSMiquel Raynal .addr1_reg =
52588ffef1bSMiquel Raynal (page & 0xFF) << (8 * (anand->caddr_cycles)) |
52688ffef1bSMiquel Raynal (((page >> 8) & 0xFF) << (8 * (1 + anand->caddr_cycles))),
52788ffef1bSMiquel Raynal .addr2_reg =
52888ffef1bSMiquel Raynal ((page >> 16) & 0xFF) |
52988ffef1bSMiquel Raynal ADDR2_STRENGTH(anand->strength) |
530acbd3d09SMiquel Raynal ADDR2_CS(nfc->native_cs),
53188ffef1bSMiquel Raynal .cmd_reg =
53288ffef1bSMiquel Raynal CMD_1(NAND_CMD_SEQIN) |
53388ffef1bSMiquel Raynal CMD_2(NAND_CMD_PAGEPROG) |
53488ffef1bSMiquel Raynal CMD_PAGE_SIZE(anand->page_sz) |
53588ffef1bSMiquel Raynal CMD_DMA_ENABLE |
53688ffef1bSMiquel Raynal CMD_NADDRS(anand->caddr_cycles +
53788ffef1bSMiquel Raynal anand->raddr_cycles) |
53888ffef1bSMiquel Raynal CMD_ECC_ENABLE,
53988ffef1bSMiquel Raynal .prog_reg = PROG_PGPROG,
54088ffef1bSMiquel Raynal };
54188ffef1bSMiquel Raynal
54288ffef1bSMiquel Raynal writel_relaxed(anand->ecc_conf, nfc->base + ECC_CONF_REG);
54388ffef1bSMiquel Raynal writel_relaxed(ECC_SP_CMD1(NAND_CMD_RNDIN) |
54488ffef1bSMiquel Raynal ECC_SP_ADDRS(anand->caddr_cycles),
54588ffef1bSMiquel Raynal nfc->base + ECC_SP_REG);
54688ffef1bSMiquel Raynal
54788ffef1bSMiquel Raynal dma_addr = dma_map_single(nfc->dev, (void *)buf, len, DMA_TO_DEVICE);
54888ffef1bSMiquel Raynal if (dma_mapping_error(nfc->dev, dma_addr)) {
54988ffef1bSMiquel Raynal dev_err(nfc->dev, "Buffer mapping error");
55088ffef1bSMiquel Raynal return -EIO;
55188ffef1bSMiquel Raynal }
55288ffef1bSMiquel Raynal
55388ffef1bSMiquel Raynal writel_relaxed(lower_32_bits(dma_addr), nfc->base + DMA_ADDR0_REG);
55488ffef1bSMiquel Raynal writel_relaxed(upper_32_bits(dma_addr), nfc->base + DMA_ADDR1_REG);
55588ffef1bSMiquel Raynal
55688ffef1bSMiquel Raynal anfc_trigger_op(nfc, &nfc_op);
55788ffef1bSMiquel Raynal ret = anfc_wait_for_event(nfc, XFER_COMPLETE);
55888ffef1bSMiquel Raynal dma_unmap_single(nfc->dev, dma_addr, len, DMA_TO_DEVICE);
55988ffef1bSMiquel Raynal if (ret) {
56088ffef1bSMiquel Raynal dev_err(nfc->dev, "Error writing page %d\n", page);
56188ffef1bSMiquel Raynal return ret;
56288ffef1bSMiquel Raynal }
56388ffef1bSMiquel Raynal
56488ffef1bSMiquel Raynal /* Spare data is not protected */
5653a4a893dSMiquel Raynal if (oob_required) {
56688ffef1bSMiquel Raynal ret = nand_write_oob_std(chip, page);
5673a4a893dSMiquel Raynal if (ret)
56888ffef1bSMiquel Raynal return ret;
56988ffef1bSMiquel Raynal }
57088ffef1bSMiquel Raynal
5713a4a893dSMiquel Raynal /* Check write status on the chip side */
5723a4a893dSMiquel Raynal ret = nand_status_op(chip, &status);
5733a4a893dSMiquel Raynal if (ret)
5743a4a893dSMiquel Raynal return ret;
5753a4a893dSMiquel Raynal
5763a4a893dSMiquel Raynal if (status & NAND_STATUS_FAIL)
5773a4a893dSMiquel Raynal return -EIO;
5783a4a893dSMiquel Raynal
5793a4a893dSMiquel Raynal return 0;
5803a4a893dSMiquel Raynal }
5813a4a893dSMiquel Raynal
anfc_sel_write_page_hw_ecc(struct nand_chip * chip,const u8 * buf,int oob_required,int page)582b5437c7bSMiquel Raynal static int anfc_sel_write_page_hw_ecc(struct nand_chip *chip, const u8 *buf,
583b5437c7bSMiquel Raynal int oob_required, int page)
584b5437c7bSMiquel Raynal {
585b5437c7bSMiquel Raynal int ret;
586b5437c7bSMiquel Raynal
587b5437c7bSMiquel Raynal ret = anfc_select_target(chip, chip->cur_cs);
588b5437c7bSMiquel Raynal if (ret)
589b5437c7bSMiquel Raynal return ret;
590b5437c7bSMiquel Raynal
591b5437c7bSMiquel Raynal return anfc_write_page_hw_ecc(chip, buf, oob_required, page);
592b5437c7bSMiquel Raynal };
593b5437c7bSMiquel Raynal
594197b88feSMiquel Raynal /* 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)595197b88feSMiquel Raynal static int anfc_parse_instructions(struct nand_chip *chip,
596197b88feSMiquel Raynal const struct nand_subop *subop,
597197b88feSMiquel Raynal struct anfc_op *nfc_op)
598197b88feSMiquel Raynal {
599acbd3d09SMiquel Raynal struct arasan_nfc *nfc = to_anfc(chip->controller);
600197b88feSMiquel Raynal struct anand *anand = to_anand(chip);
601197b88feSMiquel Raynal const struct nand_op_instr *instr = NULL;
602197b88feSMiquel Raynal bool first_cmd = true;
603197b88feSMiquel Raynal unsigned int op_id;
604197b88feSMiquel Raynal int ret, i;
605197b88feSMiquel Raynal
606197b88feSMiquel Raynal memset(nfc_op, 0, sizeof(*nfc_op));
607acbd3d09SMiquel Raynal nfc_op->addr2_reg = ADDR2_CS(nfc->native_cs);
608197b88feSMiquel Raynal nfc_op->cmd_reg = CMD_PAGE_SIZE(anand->page_sz);
609197b88feSMiquel Raynal
610197b88feSMiquel Raynal for (op_id = 0; op_id < subop->ninstrs; op_id++) {
611197b88feSMiquel Raynal unsigned int offset, naddrs, pktsize;
612197b88feSMiquel Raynal const u8 *addrs;
613197b88feSMiquel Raynal u8 *buf;
614197b88feSMiquel Raynal
615197b88feSMiquel Raynal instr = &subop->instrs[op_id];
616197b88feSMiquel Raynal
617197b88feSMiquel Raynal switch (instr->type) {
618197b88feSMiquel Raynal case NAND_OP_CMD_INSTR:
619197b88feSMiquel Raynal if (first_cmd)
620197b88feSMiquel Raynal nfc_op->cmd_reg |= CMD_1(instr->ctx.cmd.opcode);
621197b88feSMiquel Raynal else
622197b88feSMiquel Raynal nfc_op->cmd_reg |= CMD_2(instr->ctx.cmd.opcode);
623197b88feSMiquel Raynal
624197b88feSMiquel Raynal first_cmd = false;
625197b88feSMiquel Raynal break;
626197b88feSMiquel Raynal
627197b88feSMiquel Raynal case NAND_OP_ADDR_INSTR:
628197b88feSMiquel Raynal offset = nand_subop_get_addr_start_off(subop, op_id);
629197b88feSMiquel Raynal naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
630197b88feSMiquel Raynal addrs = &instr->ctx.addr.addrs[offset];
631197b88feSMiquel Raynal nfc_op->cmd_reg |= CMD_NADDRS(naddrs);
632197b88feSMiquel Raynal
633197b88feSMiquel Raynal for (i = 0; i < min(ANFC_MAX_ADDR_CYC, naddrs); i++) {
634197b88feSMiquel Raynal if (i < 4)
635197b88feSMiquel Raynal nfc_op->addr1_reg |= (u32)addrs[i] << i * 8;
636197b88feSMiquel Raynal else
637197b88feSMiquel Raynal nfc_op->addr2_reg |= addrs[i];
638197b88feSMiquel Raynal }
639197b88feSMiquel Raynal
640197b88feSMiquel Raynal break;
641197b88feSMiquel Raynal case NAND_OP_DATA_IN_INSTR:
642197b88feSMiquel Raynal nfc_op->read = true;
643197b88feSMiquel Raynal fallthrough;
644197b88feSMiquel Raynal case NAND_OP_DATA_OUT_INSTR:
645197b88feSMiquel Raynal offset = nand_subop_get_data_start_off(subop, op_id);
646197b88feSMiquel Raynal buf = instr->ctx.data.buf.in;
647197b88feSMiquel Raynal nfc_op->buf = &buf[offset];
648197b88feSMiquel Raynal nfc_op->len = nand_subop_get_data_len(subop, op_id);
649197b88feSMiquel Raynal ret = anfc_pkt_len_config(nfc_op->len, &nfc_op->steps,
650197b88feSMiquel Raynal &pktsize);
651197b88feSMiquel Raynal if (ret)
652197b88feSMiquel Raynal return ret;
653197b88feSMiquel Raynal
654197b88feSMiquel Raynal /*
655197b88feSMiquel Raynal * Number of DATA cycles must be aligned on 4, this
656197b88feSMiquel Raynal * means the controller might read/write more than
657197b88feSMiquel Raynal * requested. This is harmless most of the time as extra
658197b88feSMiquel Raynal * DATA are discarded in the write path and read pointer
659197b88feSMiquel Raynal * adjusted in the read path.
660197b88feSMiquel Raynal *
661197b88feSMiquel Raynal * FIXME: The core should mark operations where
662197b88feSMiquel Raynal * reading/writing more is allowed so the exec_op()
663197b88feSMiquel Raynal * implementation can take the right decision when the
664197b88feSMiquel Raynal * alignment constraint is not met: adjust the number of
665197b88feSMiquel Raynal * DATA cycles when it's allowed, reject the operation
666197b88feSMiquel Raynal * otherwise.
667197b88feSMiquel Raynal */
668197b88feSMiquel Raynal nfc_op->pkt_reg |= PKT_SIZE(round_up(pktsize, 4)) |
669197b88feSMiquel Raynal PKT_STEPS(nfc_op->steps);
670197b88feSMiquel Raynal break;
671197b88feSMiquel Raynal case NAND_OP_WAITRDY_INSTR:
672197b88feSMiquel Raynal nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
673197b88feSMiquel Raynal break;
674197b88feSMiquel Raynal }
675197b88feSMiquel Raynal }
676197b88feSMiquel Raynal
677197b88feSMiquel Raynal return 0;
678197b88feSMiquel Raynal }
679197b88feSMiquel Raynal
anfc_rw_pio_op(struct arasan_nfc * nfc,struct anfc_op * nfc_op)680197b88feSMiquel Raynal static int anfc_rw_pio_op(struct arasan_nfc *nfc, struct anfc_op *nfc_op)
681197b88feSMiquel Raynal {
682197b88feSMiquel Raynal unsigned int dwords = (nfc_op->len / 4) / nfc_op->steps;
683197b88feSMiquel Raynal unsigned int last_len = nfc_op->len % 4;
684197b88feSMiquel Raynal unsigned int offset, dir;
685197b88feSMiquel Raynal u8 *buf = nfc_op->buf;
686197b88feSMiquel Raynal int ret, i;
687197b88feSMiquel Raynal
688197b88feSMiquel Raynal for (i = 0; i < nfc_op->steps; i++) {
689197b88feSMiquel Raynal dir = nfc_op->read ? READ_READY : WRITE_READY;
690197b88feSMiquel Raynal ret = anfc_wait_for_event(nfc, dir);
691197b88feSMiquel Raynal if (ret) {
692197b88feSMiquel Raynal dev_err(nfc->dev, "PIO %s ready signal not received\n",
693197b88feSMiquel Raynal nfc_op->read ? "Read" : "Write");
694197b88feSMiquel Raynal return ret;
695197b88feSMiquel Raynal }
696197b88feSMiquel Raynal
697197b88feSMiquel Raynal offset = i * (dwords * 4);
698197b88feSMiquel Raynal if (nfc_op->read)
699197b88feSMiquel Raynal ioread32_rep(nfc->base + DATA_PORT_REG, &buf[offset],
700197b88feSMiquel Raynal dwords);
701197b88feSMiquel Raynal else
702197b88feSMiquel Raynal iowrite32_rep(nfc->base + DATA_PORT_REG, &buf[offset],
703197b88feSMiquel Raynal dwords);
704197b88feSMiquel Raynal }
705197b88feSMiquel Raynal
706197b88feSMiquel Raynal if (last_len) {
707197b88feSMiquel Raynal u32 remainder;
708197b88feSMiquel Raynal
709197b88feSMiquel Raynal offset = nfc_op->len - last_len;
710197b88feSMiquel Raynal
711197b88feSMiquel Raynal if (nfc_op->read) {
712197b88feSMiquel Raynal remainder = readl_relaxed(nfc->base + DATA_PORT_REG);
713197b88feSMiquel Raynal memcpy(&buf[offset], &remainder, last_len);
714197b88feSMiquel Raynal } else {
715197b88feSMiquel Raynal memcpy(&remainder, &buf[offset], last_len);
716197b88feSMiquel Raynal writel_relaxed(remainder, nfc->base + DATA_PORT_REG);
717197b88feSMiquel Raynal }
718197b88feSMiquel Raynal }
719197b88feSMiquel Raynal
720197b88feSMiquel Raynal return anfc_wait_for_event(nfc, XFER_COMPLETE);
721197b88feSMiquel Raynal }
722197b88feSMiquel Raynal
anfc_misc_data_type_exec(struct nand_chip * chip,const struct nand_subop * subop,u32 prog_reg)723197b88feSMiquel Raynal static int anfc_misc_data_type_exec(struct nand_chip *chip,
724197b88feSMiquel Raynal const struct nand_subop *subop,
725197b88feSMiquel Raynal u32 prog_reg)
726197b88feSMiquel Raynal {
727197b88feSMiquel Raynal struct arasan_nfc *nfc = to_anfc(chip->controller);
728197b88feSMiquel Raynal struct anfc_op nfc_op = {};
729197b88feSMiquel Raynal int ret;
730197b88feSMiquel Raynal
731197b88feSMiquel Raynal ret = anfc_parse_instructions(chip, subop, &nfc_op);
732197b88feSMiquel Raynal if (ret)
733197b88feSMiquel Raynal return ret;
734197b88feSMiquel Raynal
735197b88feSMiquel Raynal nfc_op.prog_reg = prog_reg;
736197b88feSMiquel Raynal anfc_trigger_op(nfc, &nfc_op);
737197b88feSMiquel Raynal
738197b88feSMiquel Raynal if (nfc_op.rdy_timeout_ms) {
739197b88feSMiquel Raynal ret = anfc_wait_for_rb(nfc, chip, nfc_op.rdy_timeout_ms);
740197b88feSMiquel Raynal if (ret)
741197b88feSMiquel Raynal return ret;
742197b88feSMiquel Raynal }
743197b88feSMiquel Raynal
744197b88feSMiquel Raynal return anfc_rw_pio_op(nfc, &nfc_op);
745197b88feSMiquel Raynal }
746197b88feSMiquel Raynal
anfc_param_read_type_exec(struct nand_chip * chip,const struct nand_subop * subop)747197b88feSMiquel Raynal static int anfc_param_read_type_exec(struct nand_chip *chip,
748197b88feSMiquel Raynal const struct nand_subop *subop)
749197b88feSMiquel Raynal {
750197b88feSMiquel Raynal return anfc_misc_data_type_exec(chip, subop, PROG_RDPARAM);
751197b88feSMiquel Raynal }
752197b88feSMiquel Raynal
anfc_data_read_type_exec(struct nand_chip * chip,const struct nand_subop * subop)753197b88feSMiquel Raynal static int anfc_data_read_type_exec(struct nand_chip *chip,
754197b88feSMiquel Raynal const struct nand_subop *subop)
755197b88feSMiquel Raynal {
75610938a08SMiquel Raynal u32 prog_reg = PROG_PGRD;
75710938a08SMiquel Raynal
75810938a08SMiquel Raynal /*
75910938a08SMiquel Raynal * Experience shows that while in SDR mode sending a CHANGE READ COLUMN
76010938a08SMiquel Raynal * command through the READ PAGE "type" always works fine, when in
76110938a08SMiquel Raynal * NV-DDR mode the same command simply fails. However, it was also
76210938a08SMiquel Raynal * spotted that any CHANGE READ COLUMN command sent through the CHANGE
76310938a08SMiquel Raynal * READ COLUMN ENHANCED "type" would correctly work in both cases (SDR
76410938a08SMiquel Raynal * and NV-DDR). So, for simplicity, let's program the controller with
76510938a08SMiquel Raynal * the CHANGE READ COLUMN ENHANCED "type" whenever we are requested to
76610938a08SMiquel Raynal * perform a CHANGE READ COLUMN operation.
76710938a08SMiquel Raynal */
76810938a08SMiquel Raynal if (subop->instrs[0].ctx.cmd.opcode == NAND_CMD_RNDOUT &&
76910938a08SMiquel Raynal subop->instrs[2].ctx.cmd.opcode == NAND_CMD_RNDOUTSTART)
77010938a08SMiquel Raynal prog_reg = PROG_CHG_RD_COL_ENH;
77110938a08SMiquel Raynal
77210938a08SMiquel Raynal return anfc_misc_data_type_exec(chip, subop, prog_reg);
773197b88feSMiquel Raynal }
774197b88feSMiquel Raynal
anfc_param_write_type_exec(struct nand_chip * chip,const struct nand_subop * subop)775197b88feSMiquel Raynal static int anfc_param_write_type_exec(struct nand_chip *chip,
776197b88feSMiquel Raynal const struct nand_subop *subop)
777197b88feSMiquel Raynal {
778197b88feSMiquel Raynal return anfc_misc_data_type_exec(chip, subop, PROG_SET_FEATURE);
779197b88feSMiquel Raynal }
780197b88feSMiquel Raynal
anfc_data_write_type_exec(struct nand_chip * chip,const struct nand_subop * subop)781197b88feSMiquel Raynal static int anfc_data_write_type_exec(struct nand_chip *chip,
782197b88feSMiquel Raynal const struct nand_subop *subop)
783197b88feSMiquel Raynal {
784197b88feSMiquel Raynal return anfc_misc_data_type_exec(chip, subop, PROG_PGPROG);
785197b88feSMiquel Raynal }
786197b88feSMiquel Raynal
anfc_misc_zerolen_type_exec(struct nand_chip * chip,const struct nand_subop * subop,u32 prog_reg)787197b88feSMiquel Raynal static int anfc_misc_zerolen_type_exec(struct nand_chip *chip,
788197b88feSMiquel Raynal const struct nand_subop *subop,
789197b88feSMiquel Raynal u32 prog_reg)
790197b88feSMiquel Raynal {
791197b88feSMiquel Raynal struct arasan_nfc *nfc = to_anfc(chip->controller);
792197b88feSMiquel Raynal struct anfc_op nfc_op = {};
793197b88feSMiquel Raynal int ret;
794197b88feSMiquel Raynal
795197b88feSMiquel Raynal ret = anfc_parse_instructions(chip, subop, &nfc_op);
796197b88feSMiquel Raynal if (ret)
797197b88feSMiquel Raynal return ret;
798197b88feSMiquel Raynal
799197b88feSMiquel Raynal nfc_op.prog_reg = prog_reg;
800197b88feSMiquel Raynal anfc_trigger_op(nfc, &nfc_op);
801197b88feSMiquel Raynal
802197b88feSMiquel Raynal ret = anfc_wait_for_event(nfc, XFER_COMPLETE);
803197b88feSMiquel Raynal if (ret)
804197b88feSMiquel Raynal return ret;
805197b88feSMiquel Raynal
806197b88feSMiquel Raynal if (nfc_op.rdy_timeout_ms)
807197b88feSMiquel Raynal ret = anfc_wait_for_rb(nfc, chip, nfc_op.rdy_timeout_ms);
808197b88feSMiquel Raynal
809197b88feSMiquel Raynal return ret;
810197b88feSMiquel Raynal }
811197b88feSMiquel Raynal
anfc_status_type_exec(struct nand_chip * chip,const struct nand_subop * subop)812197b88feSMiquel Raynal static int anfc_status_type_exec(struct nand_chip *chip,
813197b88feSMiquel Raynal const struct nand_subop *subop)
814197b88feSMiquel Raynal {
815197b88feSMiquel Raynal struct arasan_nfc *nfc = to_anfc(chip->controller);
816197b88feSMiquel Raynal u32 tmp;
817197b88feSMiquel Raynal int ret;
818197b88feSMiquel Raynal
819197b88feSMiquel Raynal /* See anfc_check_op() for details about this constraint */
820197b88feSMiquel Raynal if (subop->instrs[0].ctx.cmd.opcode != NAND_CMD_STATUS)
821197b88feSMiquel Raynal return -ENOTSUPP;
822197b88feSMiquel Raynal
823197b88feSMiquel Raynal ret = anfc_misc_zerolen_type_exec(chip, subop, PROG_STATUS);
824197b88feSMiquel Raynal if (ret)
825197b88feSMiquel Raynal return ret;
826197b88feSMiquel Raynal
827197b88feSMiquel Raynal tmp = readl_relaxed(nfc->base + FLASH_STS_REG);
828197b88feSMiquel Raynal memcpy(subop->instrs[1].ctx.data.buf.in, &tmp, 1);
829197b88feSMiquel Raynal
830197b88feSMiquel Raynal return 0;
831197b88feSMiquel Raynal }
832197b88feSMiquel Raynal
anfc_reset_type_exec(struct nand_chip * chip,const struct nand_subop * subop)833197b88feSMiquel Raynal static int anfc_reset_type_exec(struct nand_chip *chip,
834197b88feSMiquel Raynal const struct nand_subop *subop)
835197b88feSMiquel Raynal {
836197b88feSMiquel Raynal return anfc_misc_zerolen_type_exec(chip, subop, PROG_RST);
837197b88feSMiquel Raynal }
838197b88feSMiquel Raynal
anfc_erase_type_exec(struct nand_chip * chip,const struct nand_subop * subop)839197b88feSMiquel Raynal static int anfc_erase_type_exec(struct nand_chip *chip,
840197b88feSMiquel Raynal const struct nand_subop *subop)
841197b88feSMiquel Raynal {
842197b88feSMiquel Raynal return anfc_misc_zerolen_type_exec(chip, subop, PROG_ERASE);
843197b88feSMiquel Raynal }
844197b88feSMiquel Raynal
anfc_wait_type_exec(struct nand_chip * chip,const struct nand_subop * subop)845197b88feSMiquel Raynal static int anfc_wait_type_exec(struct nand_chip *chip,
846197b88feSMiquel Raynal const struct nand_subop *subop)
847197b88feSMiquel Raynal {
848197b88feSMiquel Raynal struct arasan_nfc *nfc = to_anfc(chip->controller);
849197b88feSMiquel Raynal struct anfc_op nfc_op = {};
850197b88feSMiquel Raynal int ret;
851197b88feSMiquel Raynal
852197b88feSMiquel Raynal ret = anfc_parse_instructions(chip, subop, &nfc_op);
853197b88feSMiquel Raynal if (ret)
854197b88feSMiquel Raynal return ret;
855197b88feSMiquel Raynal
856197b88feSMiquel Raynal return anfc_wait_for_rb(nfc, chip, nfc_op.rdy_timeout_ms);
857197b88feSMiquel Raynal }
858197b88feSMiquel Raynal
859197b88feSMiquel Raynal static const struct nand_op_parser anfc_op_parser = NAND_OP_PARSER(
860197b88feSMiquel Raynal NAND_OP_PARSER_PATTERN(
861197b88feSMiquel Raynal anfc_param_read_type_exec,
862197b88feSMiquel Raynal NAND_OP_PARSER_PAT_CMD_ELEM(false),
863197b88feSMiquel Raynal NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
864197b88feSMiquel Raynal NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
865197b88feSMiquel Raynal NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE)),
866197b88feSMiquel Raynal NAND_OP_PARSER_PATTERN(
867197b88feSMiquel Raynal anfc_param_write_type_exec,
868197b88feSMiquel Raynal NAND_OP_PARSER_PAT_CMD_ELEM(false),
869197b88feSMiquel Raynal NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
870197b88feSMiquel Raynal NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, ANFC_MAX_PARAM_SIZE)),
871197b88feSMiquel Raynal NAND_OP_PARSER_PATTERN(
872197b88feSMiquel Raynal anfc_data_read_type_exec,
873197b88feSMiquel Raynal NAND_OP_PARSER_PAT_CMD_ELEM(false),
874197b88feSMiquel Raynal NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
875197b88feSMiquel Raynal NAND_OP_PARSER_PAT_CMD_ELEM(false),
876197b88feSMiquel Raynal NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
877197b88feSMiquel Raynal NAND_OP_PARSER_PAT_DATA_IN_ELEM(true, ANFC_MAX_CHUNK_SIZE)),
878197b88feSMiquel Raynal NAND_OP_PARSER_PATTERN(
879197b88feSMiquel Raynal anfc_data_write_type_exec,
880197b88feSMiquel Raynal NAND_OP_PARSER_PAT_CMD_ELEM(false),
881197b88feSMiquel Raynal NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
882197b88feSMiquel Raynal NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, ANFC_MAX_CHUNK_SIZE),
883197b88feSMiquel Raynal NAND_OP_PARSER_PAT_CMD_ELEM(false)),
884197b88feSMiquel Raynal NAND_OP_PARSER_PATTERN(
885197b88feSMiquel Raynal anfc_reset_type_exec,
886197b88feSMiquel Raynal NAND_OP_PARSER_PAT_CMD_ELEM(false),
887197b88feSMiquel Raynal NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
888197b88feSMiquel Raynal NAND_OP_PARSER_PATTERN(
889197b88feSMiquel Raynal anfc_erase_type_exec,
890197b88feSMiquel Raynal NAND_OP_PARSER_PAT_CMD_ELEM(false),
891197b88feSMiquel Raynal NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
892197b88feSMiquel Raynal NAND_OP_PARSER_PAT_CMD_ELEM(false),
893197b88feSMiquel Raynal NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
894197b88feSMiquel Raynal NAND_OP_PARSER_PATTERN(
895197b88feSMiquel Raynal anfc_status_type_exec,
896197b88feSMiquel Raynal NAND_OP_PARSER_PAT_CMD_ELEM(false),
897197b88feSMiquel Raynal NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE)),
898197b88feSMiquel Raynal NAND_OP_PARSER_PATTERN(
899197b88feSMiquel Raynal anfc_wait_type_exec,
900197b88feSMiquel Raynal NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
901197b88feSMiquel Raynal );
902197b88feSMiquel Raynal
anfc_check_op(struct nand_chip * chip,const struct nand_operation * op)903197b88feSMiquel Raynal static int anfc_check_op(struct nand_chip *chip,
904197b88feSMiquel Raynal const struct nand_operation *op)
905197b88feSMiquel Raynal {
906197b88feSMiquel Raynal const struct nand_op_instr *instr;
907197b88feSMiquel Raynal int op_id;
908197b88feSMiquel Raynal
909197b88feSMiquel Raynal /*
910197b88feSMiquel Raynal * The controller abstracts all the NAND operations and do not support
911197b88feSMiquel Raynal * data only operations.
912197b88feSMiquel Raynal *
913197b88feSMiquel Raynal * TODO: The nand_op_parser framework should be extended to
914197b88feSMiquel Raynal * support custom checks on DATA instructions.
915197b88feSMiquel Raynal */
916197b88feSMiquel Raynal for (op_id = 0; op_id < op->ninstrs; op_id++) {
917197b88feSMiquel Raynal instr = &op->instrs[op_id];
918197b88feSMiquel Raynal
919197b88feSMiquel Raynal switch (instr->type) {
920197b88feSMiquel Raynal case NAND_OP_ADDR_INSTR:
921197b88feSMiquel Raynal if (instr->ctx.addr.naddrs > ANFC_MAX_ADDR_CYC)
922197b88feSMiquel Raynal return -ENOTSUPP;
923197b88feSMiquel Raynal
924197b88feSMiquel Raynal break;
925197b88feSMiquel Raynal case NAND_OP_DATA_IN_INSTR:
926197b88feSMiquel Raynal case NAND_OP_DATA_OUT_INSTR:
927197b88feSMiquel Raynal if (instr->ctx.data.len > ANFC_MAX_CHUNK_SIZE)
928197b88feSMiquel Raynal return -ENOTSUPP;
929197b88feSMiquel Raynal
93037ea9f16SGONG, Ruiqi if (anfc_pkt_len_config(instr->ctx.data.len, NULL, NULL))
931197b88feSMiquel Raynal return -ENOTSUPP;
932197b88feSMiquel Raynal
933197b88feSMiquel Raynal break;
934197b88feSMiquel Raynal default:
935197b88feSMiquel Raynal break;
936197b88feSMiquel Raynal }
937197b88feSMiquel Raynal }
938197b88feSMiquel Raynal
939197b88feSMiquel Raynal /*
940197b88feSMiquel Raynal * The controller does not allow to proceed with a CMD+DATA_IN cycle
941197b88feSMiquel Raynal * manually on the bus by reading data from the data register. Instead,
942197b88feSMiquel Raynal * the controller abstract a status read operation with its own status
943197b88feSMiquel Raynal * register after ordering a read status operation. Hence, we cannot
944197b88feSMiquel Raynal * support any CMD+DATA_IN operation other than a READ STATUS.
945197b88feSMiquel Raynal *
946197b88feSMiquel Raynal * TODO: The nand_op_parser() framework should be extended to describe
947197b88feSMiquel Raynal * fixed patterns instead of open-coding this check here.
948197b88feSMiquel Raynal */
949197b88feSMiquel Raynal if (op->ninstrs == 2 &&
950197b88feSMiquel Raynal op->instrs[0].type == NAND_OP_CMD_INSTR &&
951197b88feSMiquel Raynal op->instrs[0].ctx.cmd.opcode != NAND_CMD_STATUS &&
952197b88feSMiquel Raynal op->instrs[1].type == NAND_OP_DATA_IN_INSTR)
953197b88feSMiquel Raynal return -ENOTSUPP;
954197b88feSMiquel Raynal
955197b88feSMiquel Raynal return nand_op_parser_exec_op(chip, &anfc_op_parser, op, true);
956197b88feSMiquel Raynal }
957197b88feSMiquel Raynal
anfc_exec_op(struct nand_chip * chip,const struct nand_operation * op,bool check_only)958197b88feSMiquel Raynal static int anfc_exec_op(struct nand_chip *chip,
959197b88feSMiquel Raynal const struct nand_operation *op,
960197b88feSMiquel Raynal bool check_only)
961197b88feSMiquel Raynal {
962197b88feSMiquel Raynal int ret;
963197b88feSMiquel Raynal
964197b88feSMiquel Raynal if (check_only)
965197b88feSMiquel Raynal return anfc_check_op(chip, op);
966197b88feSMiquel Raynal
967197b88feSMiquel Raynal ret = anfc_select_target(chip, op->cs);
968197b88feSMiquel Raynal if (ret)
969197b88feSMiquel Raynal return ret;
970197b88feSMiquel Raynal
971197b88feSMiquel Raynal return nand_op_parser_exec_op(chip, &anfc_op_parser, op, check_only);
972197b88feSMiquel Raynal }
973197b88feSMiquel Raynal
anfc_setup_interface(struct nand_chip * chip,int target,const struct nand_interface_config * conf)9744c46667bSMiquel Raynal static int anfc_setup_interface(struct nand_chip *chip, int target,
9754c46667bSMiquel Raynal const struct nand_interface_config *conf)
976197b88feSMiquel Raynal {
977197b88feSMiquel Raynal struct anand *anand = to_anand(chip);
978197b88feSMiquel Raynal struct arasan_nfc *nfc = to_anfc(chip->controller);
979197b88feSMiquel Raynal struct device_node *np = nfc->dev->of_node;
9804dd7ef97SMiquel Raynal const struct nand_sdr_timings *sdr;
9814edde603SMiquel Raynal const struct nand_nvddr_timings *nvddr;
98276e12c10SMiquel Raynal unsigned int tccs_min, dqs_mode, fast_tcad;
9834dd7ef97SMiquel Raynal
9844edde603SMiquel Raynal if (nand_interface_is_nvddr(conf)) {
9854edde603SMiquel Raynal nvddr = nand_get_nvddr_timings(conf);
9864edde603SMiquel Raynal if (IS_ERR(nvddr))
9874edde603SMiquel Raynal return PTR_ERR(nvddr);
9884edde603SMiquel Raynal } else {
9894dd7ef97SMiquel Raynal sdr = nand_get_sdr_timings(conf);
9904dd7ef97SMiquel Raynal if (IS_ERR(sdr))
9914dd7ef97SMiquel Raynal return PTR_ERR(sdr);
9924edde603SMiquel Raynal }
993197b88feSMiquel Raynal
994197b88feSMiquel Raynal if (target < 0)
995197b88feSMiquel Raynal return 0;
996197b88feSMiquel Raynal
99776e12c10SMiquel Raynal if (nand_interface_is_sdr(conf)) {
99823739c34SMiquel Raynal anand->data_iface = DIFACE_SDR |
9994edde603SMiquel Raynal DIFACE_SDR_MODE(conf->timings.mode);
100076e12c10SMiquel Raynal anand->timings = 0;
100176e12c10SMiquel Raynal } else {
100223739c34SMiquel Raynal anand->data_iface = DIFACE_NVDDR |
10034edde603SMiquel Raynal DIFACE_DDR_MODE(conf->timings.mode);
10044edde603SMiquel Raynal
100576e12c10SMiquel Raynal if (conf->timings.nvddr.tCCS_min <= 100000)
100676e12c10SMiquel Raynal tccs_min = TCCS_TIME_100NS;
100776e12c10SMiquel Raynal else if (conf->timings.nvddr.tCCS_min <= 200000)
100876e12c10SMiquel Raynal tccs_min = TCCS_TIME_200NS;
100976e12c10SMiquel Raynal else if (conf->timings.nvddr.tCCS_min <= 300000)
101076e12c10SMiquel Raynal tccs_min = TCCS_TIME_300NS;
101176e12c10SMiquel Raynal else
101276e12c10SMiquel Raynal tccs_min = TCCS_TIME_500NS;
101376e12c10SMiquel Raynal
101476e12c10SMiquel Raynal fast_tcad = 0;
101576e12c10SMiquel Raynal if (conf->timings.nvddr.tCAD_min < 45000)
101676e12c10SMiquel Raynal fast_tcad = FAST_TCAD;
101776e12c10SMiquel Raynal
101876e12c10SMiquel Raynal switch (conf->timings.mode) {
101976e12c10SMiquel Raynal case 5:
102076e12c10SMiquel Raynal case 4:
102176e12c10SMiquel Raynal dqs_mode = 2;
102276e12c10SMiquel Raynal break;
102376e12c10SMiquel Raynal case 3:
102476e12c10SMiquel Raynal dqs_mode = 3;
102576e12c10SMiquel Raynal break;
102676e12c10SMiquel Raynal case 2:
102776e12c10SMiquel Raynal dqs_mode = 4;
102876e12c10SMiquel Raynal break;
102976e12c10SMiquel Raynal case 1:
103076e12c10SMiquel Raynal dqs_mode = 5;
103176e12c10SMiquel Raynal break;
103276e12c10SMiquel Raynal case 0:
103376e12c10SMiquel Raynal default:
103476e12c10SMiquel Raynal dqs_mode = 6;
103576e12c10SMiquel Raynal break;
103676e12c10SMiquel Raynal }
103776e12c10SMiquel Raynal
103876e12c10SMiquel Raynal anand->timings = tccs_min | fast_tcad |
103976e12c10SMiquel Raynal DQS_BUFF_SEL_IN(dqs_mode) |
104076e12c10SMiquel Raynal DQS_BUFF_SEL_OUT(dqs_mode);
104176e12c10SMiquel Raynal }
104276e12c10SMiquel Raynal
1043e16eceeaSOlga Kitaina if (nand_interface_is_sdr(conf)) {
1044197b88feSMiquel Raynal anand->clk = ANFC_XLNX_SDR_DFLT_CORE_CLK;
1045e16eceeaSOlga Kitaina } else {
1046e16eceeaSOlga Kitaina /* ONFI timings are defined in picoseconds */
1047e16eceeaSOlga Kitaina anand->clk = div_u64((u64)NSEC_PER_SEC * 1000,
1048e16eceeaSOlga Kitaina conf->timings.nvddr.tCK_min);
1049e16eceeaSOlga Kitaina }
1050197b88feSMiquel Raynal
1051197b88feSMiquel Raynal /*
1052197b88feSMiquel Raynal * Due to a hardware bug in the ZynqMP SoC, SDR timing modes 0-1 work
1053197b88feSMiquel Raynal * with f > 90MHz (default clock is 100MHz) but signals are unstable
1054197b88feSMiquel Raynal * with higher modes. Hence we decrease a little bit the clock rate to
10554edde603SMiquel Raynal * 80MHz when using SDR modes 2-5 with this SoC.
1056197b88feSMiquel Raynal */
1057197b88feSMiquel Raynal if (of_device_is_compatible(np, "xlnx,zynqmp-nand-controller") &&
10584edde603SMiquel Raynal nand_interface_is_sdr(conf) && conf->timings.mode >= 2)
1059197b88feSMiquel Raynal anand->clk = ANFC_XLNX_SDR_HS_CORE_CLK;
1060197b88feSMiquel Raynal
1061197b88feSMiquel Raynal return 0;
1062197b88feSMiquel Raynal }
1063197b88feSMiquel Raynal
anfc_calc_hw_ecc_bytes(int step_size,int strength)106488ffef1bSMiquel Raynal static int anfc_calc_hw_ecc_bytes(int step_size, int strength)
106588ffef1bSMiquel Raynal {
106688ffef1bSMiquel Raynal unsigned int bch_gf_mag, ecc_bits;
106788ffef1bSMiquel Raynal
106888ffef1bSMiquel Raynal switch (step_size) {
106988ffef1bSMiquel Raynal case SZ_512:
107088ffef1bSMiquel Raynal bch_gf_mag = 13;
107188ffef1bSMiquel Raynal break;
107288ffef1bSMiquel Raynal case SZ_1K:
107388ffef1bSMiquel Raynal bch_gf_mag = 14;
107488ffef1bSMiquel Raynal break;
107588ffef1bSMiquel Raynal default:
107688ffef1bSMiquel Raynal return -EINVAL;
107788ffef1bSMiquel Raynal }
107888ffef1bSMiquel Raynal
107988ffef1bSMiquel Raynal ecc_bits = bch_gf_mag * strength;
108088ffef1bSMiquel Raynal
108188ffef1bSMiquel Raynal return DIV_ROUND_UP(ecc_bits, 8);
108288ffef1bSMiquel Raynal }
108388ffef1bSMiquel Raynal
108488ffef1bSMiquel Raynal static const int anfc_hw_ecc_512_strengths[] = {4, 8, 12};
108588ffef1bSMiquel Raynal
108688ffef1bSMiquel Raynal static const int anfc_hw_ecc_1024_strengths[] = {24};
108788ffef1bSMiquel Raynal
108888ffef1bSMiquel Raynal static const struct nand_ecc_step_info anfc_hw_ecc_step_infos[] = {
108988ffef1bSMiquel Raynal {
109088ffef1bSMiquel Raynal .stepsize = SZ_512,
109188ffef1bSMiquel Raynal .strengths = anfc_hw_ecc_512_strengths,
109288ffef1bSMiquel Raynal .nstrengths = ARRAY_SIZE(anfc_hw_ecc_512_strengths),
109388ffef1bSMiquel Raynal },
109488ffef1bSMiquel Raynal {
109588ffef1bSMiquel Raynal .stepsize = SZ_1K,
109688ffef1bSMiquel Raynal .strengths = anfc_hw_ecc_1024_strengths,
109788ffef1bSMiquel Raynal .nstrengths = ARRAY_SIZE(anfc_hw_ecc_1024_strengths),
109888ffef1bSMiquel Raynal },
109988ffef1bSMiquel Raynal };
110088ffef1bSMiquel Raynal
110188ffef1bSMiquel Raynal static const struct nand_ecc_caps anfc_hw_ecc_caps = {
110288ffef1bSMiquel Raynal .stepinfos = anfc_hw_ecc_step_infos,
110388ffef1bSMiquel Raynal .nstepinfos = ARRAY_SIZE(anfc_hw_ecc_step_infos),
110488ffef1bSMiquel Raynal .calc_ecc_bytes = anfc_calc_hw_ecc_bytes,
110588ffef1bSMiquel Raynal };
110688ffef1bSMiquel Raynal
anfc_init_hw_ecc_controller(struct arasan_nfc * nfc,struct nand_chip * chip)110788ffef1bSMiquel Raynal static int anfc_init_hw_ecc_controller(struct arasan_nfc *nfc,
110888ffef1bSMiquel Raynal struct nand_chip *chip)
110988ffef1bSMiquel Raynal {
111088ffef1bSMiquel Raynal struct anand *anand = to_anand(chip);
111188ffef1bSMiquel Raynal struct mtd_info *mtd = nand_to_mtd(chip);
111288ffef1bSMiquel Raynal struct nand_ecc_ctrl *ecc = &chip->ecc;
111388ffef1bSMiquel Raynal unsigned int bch_prim_poly = 0, bch_gf_mag = 0, ecc_offset;
111488ffef1bSMiquel Raynal int ret;
111588ffef1bSMiquel Raynal
111688ffef1bSMiquel Raynal switch (mtd->writesize) {
111788ffef1bSMiquel Raynal case SZ_512:
111888ffef1bSMiquel Raynal case SZ_2K:
111988ffef1bSMiquel Raynal case SZ_4K:
112088ffef1bSMiquel Raynal case SZ_8K:
112188ffef1bSMiquel Raynal case SZ_16K:
112288ffef1bSMiquel Raynal break;
112388ffef1bSMiquel Raynal default:
112488ffef1bSMiquel Raynal dev_err(nfc->dev, "Unsupported page size %d\n", mtd->writesize);
112588ffef1bSMiquel Raynal return -EINVAL;
112688ffef1bSMiquel Raynal }
112788ffef1bSMiquel Raynal
112888ffef1bSMiquel Raynal ret = nand_ecc_choose_conf(chip, &anfc_hw_ecc_caps, mtd->oobsize);
112988ffef1bSMiquel Raynal if (ret)
113088ffef1bSMiquel Raynal return ret;
113188ffef1bSMiquel Raynal
113288ffef1bSMiquel Raynal switch (ecc->strength) {
113388ffef1bSMiquel Raynal case 12:
113488ffef1bSMiquel Raynal anand->strength = 0x1;
113588ffef1bSMiquel Raynal break;
113688ffef1bSMiquel Raynal case 8:
113788ffef1bSMiquel Raynal anand->strength = 0x2;
113888ffef1bSMiquel Raynal break;
113988ffef1bSMiquel Raynal case 4:
114088ffef1bSMiquel Raynal anand->strength = 0x3;
114188ffef1bSMiquel Raynal break;
114288ffef1bSMiquel Raynal case 24:
114388ffef1bSMiquel Raynal anand->strength = 0x4;
114488ffef1bSMiquel Raynal break;
114588ffef1bSMiquel Raynal default:
114688ffef1bSMiquel Raynal dev_err(nfc->dev, "Unsupported strength %d\n", ecc->strength);
114788ffef1bSMiquel Raynal return -EINVAL;
114888ffef1bSMiquel Raynal }
114988ffef1bSMiquel Raynal
115088ffef1bSMiquel Raynal switch (ecc->size) {
115188ffef1bSMiquel Raynal case SZ_512:
115288ffef1bSMiquel Raynal bch_gf_mag = 13;
115388ffef1bSMiquel Raynal bch_prim_poly = 0x201b;
115488ffef1bSMiquel Raynal break;
115588ffef1bSMiquel Raynal case SZ_1K:
115688ffef1bSMiquel Raynal bch_gf_mag = 14;
115788ffef1bSMiquel Raynal bch_prim_poly = 0x4443;
115888ffef1bSMiquel Raynal break;
115988ffef1bSMiquel Raynal default:
116088ffef1bSMiquel Raynal dev_err(nfc->dev, "Unsupported step size %d\n", ecc->strength);
116188ffef1bSMiquel Raynal return -EINVAL;
116288ffef1bSMiquel Raynal }
116388ffef1bSMiquel Raynal
11641e3b37aaSMiquel Raynal mtd_set_ooblayout(mtd, nand_get_large_page_ooblayout());
116588ffef1bSMiquel Raynal
116688ffef1bSMiquel Raynal ecc->steps = mtd->writesize / ecc->size;
1167e0a564aeSMiquel Raynal ecc->algo = NAND_ECC_ALGO_BCH;
116888ffef1bSMiquel Raynal anand->ecc_bits = bch_gf_mag * ecc->strength;
116988ffef1bSMiquel Raynal ecc->bytes = DIV_ROUND_UP(anand->ecc_bits, 8);
117088ffef1bSMiquel Raynal anand->ecc_total = DIV_ROUND_UP(anand->ecc_bits * ecc->steps, 8);
117188ffef1bSMiquel Raynal ecc_offset = mtd->writesize + mtd->oobsize - anand->ecc_total;
117288ffef1bSMiquel Raynal anand->ecc_conf = ECC_CONF_COL(ecc_offset) |
117388ffef1bSMiquel Raynal ECC_CONF_LEN(anand->ecc_total) |
117488ffef1bSMiquel Raynal ECC_CONF_BCH_EN;
117588ffef1bSMiquel Raynal
117688ffef1bSMiquel Raynal anand->errloc = devm_kmalloc_array(nfc->dev, ecc->strength,
117788ffef1bSMiquel Raynal sizeof(*anand->errloc), GFP_KERNEL);
117888ffef1bSMiquel Raynal if (!anand->errloc)
117988ffef1bSMiquel Raynal return -ENOMEM;
118088ffef1bSMiquel Raynal
118188ffef1bSMiquel Raynal anand->hw_ecc = devm_kmalloc(nfc->dev, ecc->bytes, GFP_KERNEL);
118288ffef1bSMiquel Raynal if (!anand->hw_ecc)
118388ffef1bSMiquel Raynal return -ENOMEM;
118488ffef1bSMiquel Raynal
118588ffef1bSMiquel Raynal /* Enforce bit swapping to fit the hardware */
118688ffef1bSMiquel Raynal anand->bch = bch_init(bch_gf_mag, ecc->strength, bch_prim_poly, true);
118788ffef1bSMiquel Raynal if (!anand->bch)
118888ffef1bSMiquel Raynal return -EINVAL;
118988ffef1bSMiquel Raynal
1190b5437c7bSMiquel Raynal ecc->read_page = anfc_sel_read_page_hw_ecc;
1191b5437c7bSMiquel Raynal ecc->write_page = anfc_sel_write_page_hw_ecc;
119288ffef1bSMiquel Raynal
119388ffef1bSMiquel Raynal return 0;
119488ffef1bSMiquel Raynal }
119588ffef1bSMiquel Raynal
anfc_attach_chip(struct nand_chip * chip)1196197b88feSMiquel Raynal static int anfc_attach_chip(struct nand_chip *chip)
1197197b88feSMiquel Raynal {
1198197b88feSMiquel Raynal struct anand *anand = to_anand(chip);
1199197b88feSMiquel Raynal struct arasan_nfc *nfc = to_anfc(chip->controller);
1200197b88feSMiquel Raynal struct mtd_info *mtd = nand_to_mtd(chip);
1201197b88feSMiquel Raynal int ret = 0;
1202197b88feSMiquel Raynal
1203197b88feSMiquel Raynal if (mtd->writesize <= SZ_512)
1204197b88feSMiquel Raynal anand->caddr_cycles = 1;
1205197b88feSMiquel Raynal else
1206197b88feSMiquel Raynal anand->caddr_cycles = 2;
1207197b88feSMiquel Raynal
1208197b88feSMiquel Raynal if (chip->options & NAND_ROW_ADDR_3)
1209197b88feSMiquel Raynal anand->raddr_cycles = 3;
1210197b88feSMiquel Raynal else
1211197b88feSMiquel Raynal anand->raddr_cycles = 2;
1212197b88feSMiquel Raynal
1213197b88feSMiquel Raynal switch (mtd->writesize) {
1214197b88feSMiquel Raynal case 512:
1215197b88feSMiquel Raynal anand->page_sz = 0;
1216197b88feSMiquel Raynal break;
1217197b88feSMiquel Raynal case 1024:
1218197b88feSMiquel Raynal anand->page_sz = 5;
1219197b88feSMiquel Raynal break;
1220197b88feSMiquel Raynal case 2048:
1221197b88feSMiquel Raynal anand->page_sz = 1;
1222197b88feSMiquel Raynal break;
1223197b88feSMiquel Raynal case 4096:
1224197b88feSMiquel Raynal anand->page_sz = 2;
1225197b88feSMiquel Raynal break;
1226197b88feSMiquel Raynal case 8192:
1227197b88feSMiquel Raynal anand->page_sz = 3;
1228197b88feSMiquel Raynal break;
1229197b88feSMiquel Raynal case 16384:
1230197b88feSMiquel Raynal anand->page_sz = 4;
1231197b88feSMiquel Raynal break;
1232197b88feSMiquel Raynal default:
1233197b88feSMiquel Raynal return -EINVAL;
1234197b88feSMiquel Raynal }
1235197b88feSMiquel Raynal
1236197b88feSMiquel Raynal /* These hooks are valid for all ECC providers */
1237197b88feSMiquel Raynal chip->ecc.read_page_raw = nand_monolithic_read_page_raw;
1238197b88feSMiquel Raynal chip->ecc.write_page_raw = nand_monolithic_write_page_raw;
1239197b88feSMiquel Raynal
1240bace41f8SMiquel Raynal switch (chip->ecc.engine_type) {
1241bace41f8SMiquel Raynal case NAND_ECC_ENGINE_TYPE_NONE:
1242bace41f8SMiquel Raynal case NAND_ECC_ENGINE_TYPE_SOFT:
1243bace41f8SMiquel Raynal case NAND_ECC_ENGINE_TYPE_ON_DIE:
1244197b88feSMiquel Raynal break;
1245bace41f8SMiquel Raynal case NAND_ECC_ENGINE_TYPE_ON_HOST:
124688ffef1bSMiquel Raynal ret = anfc_init_hw_ecc_controller(nfc, chip);
124788ffef1bSMiquel Raynal break;
1248197b88feSMiquel Raynal default:
1249197b88feSMiquel Raynal dev_err(nfc->dev, "Unsupported ECC mode: %d\n",
1250bace41f8SMiquel Raynal chip->ecc.engine_type);
1251197b88feSMiquel Raynal return -EINVAL;
1252197b88feSMiquel Raynal }
1253197b88feSMiquel Raynal
1254197b88feSMiquel Raynal return ret;
1255197b88feSMiquel Raynal }
1256197b88feSMiquel Raynal
anfc_detach_chip(struct nand_chip * chip)125788ffef1bSMiquel Raynal static void anfc_detach_chip(struct nand_chip *chip)
125888ffef1bSMiquel Raynal {
125988ffef1bSMiquel Raynal struct anand *anand = to_anand(chip);
126088ffef1bSMiquel Raynal
126188ffef1bSMiquel Raynal if (anand->bch)
126288ffef1bSMiquel Raynal bch_free(anand->bch);
126388ffef1bSMiquel Raynal }
126488ffef1bSMiquel Raynal
1265197b88feSMiquel Raynal static const struct nand_controller_ops anfc_ops = {
1266197b88feSMiquel Raynal .exec_op = anfc_exec_op,
12674c46667bSMiquel Raynal .setup_interface = anfc_setup_interface,
1268197b88feSMiquel Raynal .attach_chip = anfc_attach_chip,
126988ffef1bSMiquel Raynal .detach_chip = anfc_detach_chip,
1270197b88feSMiquel Raynal };
1271197b88feSMiquel Raynal
anfc_chip_init(struct arasan_nfc * nfc,struct device_node * np)1272197b88feSMiquel Raynal static int anfc_chip_init(struct arasan_nfc *nfc, struct device_node *np)
1273197b88feSMiquel Raynal {
1274197b88feSMiquel Raynal struct anand *anand;
1275197b88feSMiquel Raynal struct nand_chip *chip;
1276197b88feSMiquel Raynal struct mtd_info *mtd;
1277acbd3d09SMiquel Raynal int rb, ret, i;
1278197b88feSMiquel Raynal
1279197b88feSMiquel Raynal anand = devm_kzalloc(nfc->dev, sizeof(*anand), GFP_KERNEL);
1280197b88feSMiquel Raynal if (!anand)
1281197b88feSMiquel Raynal return -ENOMEM;
1282197b88feSMiquel Raynal
1283acbd3d09SMiquel Raynal /* Chip-select init */
1284acbd3d09SMiquel Raynal anand->ncs_idx = of_property_count_elems_of_size(np, "reg", sizeof(u32));
1285acbd3d09SMiquel Raynal if (anand->ncs_idx <= 0 || anand->ncs_idx > nfc->ncs) {
1286197b88feSMiquel Raynal dev_err(nfc->dev, "Invalid reg property\n");
1287197b88feSMiquel Raynal return -EINVAL;
1288197b88feSMiquel Raynal }
1289197b88feSMiquel Raynal
1290acbd3d09SMiquel Raynal anand->cs_idx = devm_kcalloc(nfc->dev, anand->ncs_idx,
1291acbd3d09SMiquel Raynal sizeof(*anand->cs_idx), GFP_KERNEL);
1292acbd3d09SMiquel Raynal if (!anand->cs_idx)
1293acbd3d09SMiquel Raynal return -ENOMEM;
1294197b88feSMiquel Raynal
1295acbd3d09SMiquel Raynal for (i = 0; i < anand->ncs_idx; i++) {
1296acbd3d09SMiquel Raynal ret = of_property_read_u32_index(np, "reg", i,
1297acbd3d09SMiquel Raynal &anand->cs_idx[i]);
1298acbd3d09SMiquel Raynal if (ret) {
1299acbd3d09SMiquel Raynal dev_err(nfc->dev, "invalid CS property: %d\n", ret);
1300acbd3d09SMiquel Raynal return ret;
1301acbd3d09SMiquel Raynal }
1302acbd3d09SMiquel Raynal }
1303acbd3d09SMiquel Raynal
1304acbd3d09SMiquel Raynal /* Ready-busy init */
1305197b88feSMiquel Raynal ret = of_property_read_u32(np, "nand-rb", &rb);
1306197b88feSMiquel Raynal if (ret)
1307197b88feSMiquel Raynal return ret;
1308197b88feSMiquel Raynal
1309acbd3d09SMiquel Raynal if (rb >= ANFC_MAX_CS) {
1310acbd3d09SMiquel Raynal dev_err(nfc->dev, "Wrong RB %d\n", rb);
1311197b88feSMiquel Raynal return -EINVAL;
1312197b88feSMiquel Raynal }
1313197b88feSMiquel Raynal
1314197b88feSMiquel Raynal anand->rb = rb;
1315197b88feSMiquel Raynal
1316197b88feSMiquel Raynal chip = &anand->chip;
1317197b88feSMiquel Raynal mtd = nand_to_mtd(chip);
1318197b88feSMiquel Raynal mtd->dev.parent = nfc->dev;
1319197b88feSMiquel Raynal chip->controller = &nfc->controller;
1320197b88feSMiquel Raynal chip->options = NAND_BUSWIDTH_AUTO | NAND_NO_SUBPAGE_WRITE |
1321197b88feSMiquel Raynal NAND_USES_DMA;
1322197b88feSMiquel Raynal
1323197b88feSMiquel Raynal nand_set_flash_node(chip, np);
1324197b88feSMiquel Raynal if (!mtd->name) {
1325197b88feSMiquel Raynal dev_err(nfc->dev, "NAND label property is mandatory\n");
1326197b88feSMiquel Raynal return -EINVAL;
1327197b88feSMiquel Raynal }
1328197b88feSMiquel Raynal
1329acbd3d09SMiquel Raynal ret = nand_scan(chip, anand->ncs_idx);
1330197b88feSMiquel Raynal if (ret) {
1331197b88feSMiquel Raynal dev_err(nfc->dev, "Scan operation failed\n");
1332197b88feSMiquel Raynal return ret;
1333197b88feSMiquel Raynal }
1334197b88feSMiquel Raynal
1335197b88feSMiquel Raynal ret = mtd_device_register(mtd, NULL, 0);
1336197b88feSMiquel Raynal if (ret) {
1337197b88feSMiquel Raynal nand_cleanup(chip);
1338197b88feSMiquel Raynal return ret;
1339197b88feSMiquel Raynal }
1340197b88feSMiquel Raynal
1341197b88feSMiquel Raynal list_add_tail(&anand->node, &nfc->chips);
1342197b88feSMiquel Raynal
1343197b88feSMiquel Raynal return 0;
1344197b88feSMiquel Raynal }
1345197b88feSMiquel Raynal
anfc_chips_cleanup(struct arasan_nfc * nfc)1346197b88feSMiquel Raynal static void anfc_chips_cleanup(struct arasan_nfc *nfc)
1347197b88feSMiquel Raynal {
1348197b88feSMiquel Raynal struct anand *anand, *tmp;
1349197b88feSMiquel Raynal struct nand_chip *chip;
1350197b88feSMiquel Raynal int ret;
1351197b88feSMiquel Raynal
1352197b88feSMiquel Raynal list_for_each_entry_safe(anand, tmp, &nfc->chips, node) {
1353197b88feSMiquel Raynal chip = &anand->chip;
1354197b88feSMiquel Raynal ret = mtd_device_unregister(nand_to_mtd(chip));
1355197b88feSMiquel Raynal WARN_ON(ret);
1356197b88feSMiquel Raynal nand_cleanup(chip);
1357197b88feSMiquel Raynal list_del(&anand->node);
1358197b88feSMiquel Raynal }
1359197b88feSMiquel Raynal }
1360197b88feSMiquel Raynal
anfc_chips_init(struct arasan_nfc * nfc)1361197b88feSMiquel Raynal static int anfc_chips_init(struct arasan_nfc *nfc)
1362197b88feSMiquel Raynal {
1363197b88feSMiquel Raynal struct device_node *np = nfc->dev->of_node, *nand_np;
1364197b88feSMiquel Raynal int nchips = of_get_child_count(np);
1365197b88feSMiquel Raynal int ret;
1366197b88feSMiquel Raynal
1367acbd3d09SMiquel Raynal if (!nchips) {
1368197b88feSMiquel Raynal dev_err(nfc->dev, "Incorrect number of NAND chips (%d)\n",
1369197b88feSMiquel Raynal nchips);
1370197b88feSMiquel Raynal return -EINVAL;
1371197b88feSMiquel Raynal }
1372197b88feSMiquel Raynal
1373197b88feSMiquel Raynal for_each_child_of_node(np, nand_np) {
1374197b88feSMiquel Raynal ret = anfc_chip_init(nfc, nand_np);
1375197b88feSMiquel Raynal if (ret) {
1376197b88feSMiquel Raynal of_node_put(nand_np);
1377197b88feSMiquel Raynal anfc_chips_cleanup(nfc);
1378197b88feSMiquel Raynal break;
1379197b88feSMiquel Raynal }
1380197b88feSMiquel Raynal }
1381197b88feSMiquel Raynal
1382197b88feSMiquel Raynal return ret;
1383197b88feSMiquel Raynal }
1384197b88feSMiquel Raynal
anfc_reset(struct arasan_nfc * nfc)1385197b88feSMiquel Raynal static void anfc_reset(struct arasan_nfc *nfc)
1386197b88feSMiquel Raynal {
1387197b88feSMiquel Raynal /* Disable interrupt signals */
1388197b88feSMiquel Raynal writel_relaxed(0, nfc->base + INTR_SIG_EN_REG);
1389197b88feSMiquel Raynal
1390197b88feSMiquel Raynal /* Enable interrupt status */
1391197b88feSMiquel Raynal writel_relaxed(EVENT_MASK, nfc->base + INTR_STS_EN_REG);
1392acbd3d09SMiquel Raynal
1393acbd3d09SMiquel Raynal nfc->cur_cs = -1;
1394acbd3d09SMiquel Raynal }
1395acbd3d09SMiquel Raynal
anfc_parse_cs(struct arasan_nfc * nfc)1396acbd3d09SMiquel Raynal static int anfc_parse_cs(struct arasan_nfc *nfc)
1397acbd3d09SMiquel Raynal {
1398acbd3d09SMiquel Raynal int ret;
1399acbd3d09SMiquel Raynal
1400acbd3d09SMiquel Raynal /* Check the gpio-cs property */
1401acbd3d09SMiquel Raynal ret = rawnand_dt_parse_gpio_cs(nfc->dev, &nfc->cs_array, &nfc->ncs);
1402acbd3d09SMiquel Raynal if (ret)
1403acbd3d09SMiquel Raynal return ret;
1404acbd3d09SMiquel Raynal
1405acbd3d09SMiquel Raynal /*
1406acbd3d09SMiquel Raynal * The controller native CS cannot be both disabled at the same time.
1407acbd3d09SMiquel Raynal * Hence, only one native CS can be used if GPIO CS are needed, so that
1408acbd3d09SMiquel Raynal * the other is selected when a non-native CS must be asserted (not
1409acbd3d09SMiquel Raynal * wired physically or configured as GPIO instead of NAND CS). In this
1410acbd3d09SMiquel Raynal * case, the "not" chosen CS is assigned to nfc->spare_cs and selected
1411acbd3d09SMiquel Raynal * whenever a GPIO CS must be asserted.
1412acbd3d09SMiquel Raynal */
1413649ba9c1SMaciej Andrzejewski if (nfc->cs_array) {
1414649ba9c1SMaciej Andrzejewski if (nfc->ncs > 2 && !nfc->cs_array[0] && !nfc->cs_array[1]) {
1415acbd3d09SMiquel Raynal dev_err(nfc->dev,
1416acbd3d09SMiquel Raynal "Assign a single native CS when using GPIOs\n");
1417acbd3d09SMiquel Raynal return -EINVAL;
1418acbd3d09SMiquel Raynal }
1419acbd3d09SMiquel Raynal
1420acbd3d09SMiquel Raynal if (nfc->cs_array[0])
1421acbd3d09SMiquel Raynal nfc->spare_cs = 0;
1422acbd3d09SMiquel Raynal else
1423acbd3d09SMiquel Raynal nfc->spare_cs = 1;
1424acbd3d09SMiquel Raynal }
1425acbd3d09SMiquel Raynal
1426acbd3d09SMiquel Raynal if (!nfc->cs_array) {
1427acbd3d09SMiquel Raynal nfc->cs_array = anfc_default_cs_array;
1428acbd3d09SMiquel Raynal nfc->ncs = ANFC_MAX_CS;
1429acbd3d09SMiquel Raynal return 0;
1430acbd3d09SMiquel Raynal }
1431acbd3d09SMiquel Raynal
1432acbd3d09SMiquel Raynal return 0;
1433197b88feSMiquel Raynal }
1434197b88feSMiquel Raynal
anfc_probe(struct platform_device * pdev)1435197b88feSMiquel Raynal static int anfc_probe(struct platform_device *pdev)
1436197b88feSMiquel Raynal {
1437197b88feSMiquel Raynal struct arasan_nfc *nfc;
1438197b88feSMiquel Raynal int ret;
1439197b88feSMiquel Raynal
1440197b88feSMiquel Raynal nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
1441197b88feSMiquel Raynal if (!nfc)
1442197b88feSMiquel Raynal return -ENOMEM;
1443197b88feSMiquel Raynal
1444197b88feSMiquel Raynal nfc->dev = &pdev->dev;
1445197b88feSMiquel Raynal nand_controller_init(&nfc->controller);
1446197b88feSMiquel Raynal nfc->controller.ops = &anfc_ops;
1447197b88feSMiquel Raynal INIT_LIST_HEAD(&nfc->chips);
1448197b88feSMiquel Raynal
1449197b88feSMiquel Raynal nfc->base = devm_platform_ioremap_resource(pdev, 0);
1450197b88feSMiquel Raynal if (IS_ERR(nfc->base))
1451197b88feSMiquel Raynal return PTR_ERR(nfc->base);
1452197b88feSMiquel Raynal
1453197b88feSMiquel Raynal anfc_reset(nfc);
1454197b88feSMiquel Raynal
1455a36201acSLi Zetao nfc->controller_clk = devm_clk_get_enabled(&pdev->dev, "controller");
1456197b88feSMiquel Raynal if (IS_ERR(nfc->controller_clk))
1457197b88feSMiquel Raynal return PTR_ERR(nfc->controller_clk);
1458197b88feSMiquel Raynal
1459a36201acSLi Zetao nfc->bus_clk = devm_clk_get_enabled(&pdev->dev, "bus");
1460197b88feSMiquel Raynal if (IS_ERR(nfc->bus_clk))
1461197b88feSMiquel Raynal return PTR_ERR(nfc->bus_clk);
1462197b88feSMiquel Raynal
1463a36201acSLi Zetao ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
1464197b88feSMiquel Raynal if (ret)
1465197b88feSMiquel Raynal return ret;
1466197b88feSMiquel Raynal
1467acbd3d09SMiquel Raynal ret = anfc_parse_cs(nfc);
1468acbd3d09SMiquel Raynal if (ret)
1469a36201acSLi Zetao return ret;
1470acbd3d09SMiquel Raynal
1471197b88feSMiquel Raynal ret = anfc_chips_init(nfc);
1472197b88feSMiquel Raynal if (ret)
1473a36201acSLi Zetao return ret;
1474197b88feSMiquel Raynal
1475197b88feSMiquel Raynal platform_set_drvdata(pdev, nfc);
1476197b88feSMiquel Raynal
1477197b88feSMiquel Raynal return 0;
1478197b88feSMiquel Raynal }
1479197b88feSMiquel Raynal
anfc_remove(struct platform_device * pdev)1480ec185b18SUwe Kleine-König static void anfc_remove(struct platform_device *pdev)
1481197b88feSMiquel Raynal {
1482*36094da8SMaciej Andrzejewski int i;
1483197b88feSMiquel Raynal struct arasan_nfc *nfc = platform_get_drvdata(pdev);
1484197b88feSMiquel Raynal
1485*36094da8SMaciej Andrzejewski for (i = 0; i < nfc->ncs; i++) {
1486*36094da8SMaciej Andrzejewski if (nfc->cs_array[i]) {
1487*36094da8SMaciej Andrzejewski gpiod_put(nfc->cs_array[i]);
1488*36094da8SMaciej Andrzejewski }
1489*36094da8SMaciej Andrzejewski }
1490*36094da8SMaciej Andrzejewski
1491197b88feSMiquel Raynal anfc_chips_cleanup(nfc);
1492197b88feSMiquel Raynal }
1493197b88feSMiquel Raynal
1494197b88feSMiquel Raynal static const struct of_device_id anfc_ids[] = {
1495197b88feSMiquel Raynal {
1496197b88feSMiquel Raynal .compatible = "xlnx,zynqmp-nand-controller",
1497197b88feSMiquel Raynal },
1498197b88feSMiquel Raynal {
1499197b88feSMiquel Raynal .compatible = "arasan,nfc-v3p10",
1500197b88feSMiquel Raynal },
1501197b88feSMiquel Raynal {}
1502197b88feSMiquel Raynal };
1503197b88feSMiquel Raynal MODULE_DEVICE_TABLE(of, anfc_ids);
1504197b88feSMiquel Raynal
1505197b88feSMiquel Raynal static struct platform_driver anfc_driver = {
1506197b88feSMiquel Raynal .driver = {
1507197b88feSMiquel Raynal .name = "arasan-nand-controller",
1508197b88feSMiquel Raynal .of_match_table = anfc_ids,
1509197b88feSMiquel Raynal },
1510197b88feSMiquel Raynal .probe = anfc_probe,
1511ec185b18SUwe Kleine-König .remove_new = anfc_remove,
1512197b88feSMiquel Raynal };
1513197b88feSMiquel Raynal module_platform_driver(anfc_driver);
1514197b88feSMiquel Raynal
1515197b88feSMiquel Raynal MODULE_LICENSE("GPL v2");
1516197b88feSMiquel Raynal MODULE_AUTHOR("Punnaiah Choudary Kalluri <punnaia@xilinx.com>");
1517197b88feSMiquel Raynal MODULE_AUTHOR("Naga Sureshkumar Relli <nagasure@xilinx.com>");
1518197b88feSMiquel Raynal MODULE_AUTHOR("Miquel Raynal <miquel.raynal@bootlin.com>");
1519197b88feSMiquel Raynal MODULE_DESCRIPTION("Arasan NAND Flash Controller Driver");
1520