xref: /openbmc/u-boot/drivers/mtd/nand/raw/sunxi_nand.c (revision 592cd5defd4f71d34ffcbd8dd3326bc10f662e20)
1*a430fa06SMiquel Raynal // SPDX-License-Identifier: GPL-2.0+
2*a430fa06SMiquel Raynal /*
3*a430fa06SMiquel Raynal  * Copyright (C) 2013 Boris BREZILLON <b.brezillon.dev@gmail.com>
4*a430fa06SMiquel Raynal  * Copyright (C) 2015 Roy Spliet <r.spliet@ultimaker.com>
5*a430fa06SMiquel Raynal  *
6*a430fa06SMiquel Raynal  * Derived from:
7*a430fa06SMiquel Raynal  *	https://github.com/yuq/sunxi-nfc-mtd
8*a430fa06SMiquel Raynal  *	Copyright (C) 2013 Qiang Yu <yuq825@gmail.com>
9*a430fa06SMiquel Raynal  *
10*a430fa06SMiquel Raynal  *	https://github.com/hno/Allwinner-Info
11*a430fa06SMiquel Raynal  *	Copyright (C) 2013 Henrik Nordström <Henrik Nordström>
12*a430fa06SMiquel Raynal  *
13*a430fa06SMiquel Raynal  *	Copyright (C) 2013 Dmitriy B. <rzk333@gmail.com>
14*a430fa06SMiquel Raynal  *	Copyright (C) 2013 Sergey Lapin <slapin@ossfans.org>
15*a430fa06SMiquel Raynal  *
16*a430fa06SMiquel Raynal  * This program is free software; you can redistribute it and/or modify
17*a430fa06SMiquel Raynal  * it under the terms of the GNU General Public License as published by
18*a430fa06SMiquel Raynal  * the Free Software Foundation; either version 2 of the License, or
19*a430fa06SMiquel Raynal  * (at your option) any later version.
20*a430fa06SMiquel Raynal  *
21*a430fa06SMiquel Raynal  * This program is distributed in the hope that it will be useful,
22*a430fa06SMiquel Raynal  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23*a430fa06SMiquel Raynal  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24*a430fa06SMiquel Raynal  * GNU General Public License for more details.
25*a430fa06SMiquel Raynal  */
26*a430fa06SMiquel Raynal 
27*a430fa06SMiquel Raynal #include <common.h>
28*a430fa06SMiquel Raynal #include <fdtdec.h>
29*a430fa06SMiquel Raynal #include <memalign.h>
30*a430fa06SMiquel Raynal #include <nand.h>
31*a430fa06SMiquel Raynal 
32*a430fa06SMiquel Raynal #include <linux/kernel.h>
33*a430fa06SMiquel Raynal #include <linux/mtd/mtd.h>
34*a430fa06SMiquel Raynal #include <linux/mtd/rawnand.h>
35*a430fa06SMiquel Raynal #include <linux/mtd/partitions.h>
36*a430fa06SMiquel Raynal #include <linux/io.h>
37*a430fa06SMiquel Raynal 
38*a430fa06SMiquel Raynal #include <asm/gpio.h>
39*a430fa06SMiquel Raynal #include <asm/arch/clock.h>
40*a430fa06SMiquel Raynal 
41*a430fa06SMiquel Raynal DECLARE_GLOBAL_DATA_PTR;
42*a430fa06SMiquel Raynal 
43*a430fa06SMiquel Raynal #define NFC_REG_CTL		0x0000
44*a430fa06SMiquel Raynal #define NFC_REG_ST		0x0004
45*a430fa06SMiquel Raynal #define NFC_REG_INT		0x0008
46*a430fa06SMiquel Raynal #define NFC_REG_TIMING_CTL	0x000C
47*a430fa06SMiquel Raynal #define NFC_REG_TIMING_CFG	0x0010
48*a430fa06SMiquel Raynal #define NFC_REG_ADDR_LOW	0x0014
49*a430fa06SMiquel Raynal #define NFC_REG_ADDR_HIGH	0x0018
50*a430fa06SMiquel Raynal #define NFC_REG_SECTOR_NUM	0x001C
51*a430fa06SMiquel Raynal #define NFC_REG_CNT		0x0020
52*a430fa06SMiquel Raynal #define NFC_REG_CMD		0x0024
53*a430fa06SMiquel Raynal #define NFC_REG_RCMD_SET	0x0028
54*a430fa06SMiquel Raynal #define NFC_REG_WCMD_SET	0x002C
55*a430fa06SMiquel Raynal #define NFC_REG_IO_DATA		0x0030
56*a430fa06SMiquel Raynal #define NFC_REG_ECC_CTL		0x0034
57*a430fa06SMiquel Raynal #define NFC_REG_ECC_ST		0x0038
58*a430fa06SMiquel Raynal #define NFC_REG_DEBUG		0x003C
59*a430fa06SMiquel Raynal #define NFC_REG_ECC_ERR_CNT(x)	((0x0040 + (x)) & ~0x3)
60*a430fa06SMiquel Raynal #define NFC_REG_USER_DATA(x)	(0x0050 + ((x) * 4))
61*a430fa06SMiquel Raynal #define NFC_REG_SPARE_AREA	0x00A0
62*a430fa06SMiquel Raynal #define NFC_REG_PAT_ID		0x00A4
63*a430fa06SMiquel Raynal #define NFC_RAM0_BASE		0x0400
64*a430fa06SMiquel Raynal #define NFC_RAM1_BASE		0x0800
65*a430fa06SMiquel Raynal 
66*a430fa06SMiquel Raynal /* define bit use in NFC_CTL */
67*a430fa06SMiquel Raynal #define NFC_EN			BIT(0)
68*a430fa06SMiquel Raynal #define NFC_RESET		BIT(1)
69*a430fa06SMiquel Raynal #define NFC_BUS_WIDTH_MSK	BIT(2)
70*a430fa06SMiquel Raynal #define NFC_BUS_WIDTH_8		(0 << 2)
71*a430fa06SMiquel Raynal #define NFC_BUS_WIDTH_16	(1 << 2)
72*a430fa06SMiquel Raynal #define NFC_RB_SEL_MSK		BIT(3)
73*a430fa06SMiquel Raynal #define NFC_RB_SEL(x)		((x) << 3)
74*a430fa06SMiquel Raynal #define NFC_CE_SEL_MSK		(0x7 << 24)
75*a430fa06SMiquel Raynal #define NFC_CE_SEL(x)		((x) << 24)
76*a430fa06SMiquel Raynal #define NFC_CE_CTL		BIT(6)
77*a430fa06SMiquel Raynal #define NFC_PAGE_SHIFT_MSK	(0xf << 8)
78*a430fa06SMiquel Raynal #define NFC_PAGE_SHIFT(x)	(((x) < 10 ? 0 : (x) - 10) << 8)
79*a430fa06SMiquel Raynal #define NFC_SAM			BIT(12)
80*a430fa06SMiquel Raynal #define NFC_RAM_METHOD		BIT(14)
81*a430fa06SMiquel Raynal #define NFC_DEBUG_CTL		BIT(31)
82*a430fa06SMiquel Raynal 
83*a430fa06SMiquel Raynal /* define bit use in NFC_ST */
84*a430fa06SMiquel Raynal #define NFC_RB_B2R		BIT(0)
85*a430fa06SMiquel Raynal #define NFC_CMD_INT_FLAG	BIT(1)
86*a430fa06SMiquel Raynal #define NFC_DMA_INT_FLAG	BIT(2)
87*a430fa06SMiquel Raynal #define NFC_CMD_FIFO_STATUS	BIT(3)
88*a430fa06SMiquel Raynal #define NFC_STA			BIT(4)
89*a430fa06SMiquel Raynal #define NFC_NATCH_INT_FLAG	BIT(5)
90*a430fa06SMiquel Raynal #define NFC_RB_STATE(x)		BIT(x + 8)
91*a430fa06SMiquel Raynal 
92*a430fa06SMiquel Raynal /* define bit use in NFC_INT */
93*a430fa06SMiquel Raynal #define NFC_B2R_INT_ENABLE	BIT(0)
94*a430fa06SMiquel Raynal #define NFC_CMD_INT_ENABLE	BIT(1)
95*a430fa06SMiquel Raynal #define NFC_DMA_INT_ENABLE	BIT(2)
96*a430fa06SMiquel Raynal #define NFC_INT_MASK		(NFC_B2R_INT_ENABLE | \
97*a430fa06SMiquel Raynal 				 NFC_CMD_INT_ENABLE | \
98*a430fa06SMiquel Raynal 				 NFC_DMA_INT_ENABLE)
99*a430fa06SMiquel Raynal 
100*a430fa06SMiquel Raynal /* define bit use in NFC_TIMING_CTL */
101*a430fa06SMiquel Raynal #define NFC_TIMING_CTL_EDO	BIT(8)
102*a430fa06SMiquel Raynal 
103*a430fa06SMiquel Raynal /* define NFC_TIMING_CFG register layout */
104*a430fa06SMiquel Raynal #define NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD)		\
105*a430fa06SMiquel Raynal 	(((tWB) & 0x3) | (((tADL) & 0x3) << 2) |		\
106*a430fa06SMiquel Raynal 	(((tWHR) & 0x3) << 4) | (((tRHW) & 0x3) << 6) |		\
107*a430fa06SMiquel Raynal 	(((tCAD) & 0x7) << 8))
108*a430fa06SMiquel Raynal 
109*a430fa06SMiquel Raynal /* define bit use in NFC_CMD */
110*a430fa06SMiquel Raynal #define NFC_CMD_LOW_BYTE_MSK	0xff
111*a430fa06SMiquel Raynal #define NFC_CMD_HIGH_BYTE_MSK	(0xff << 8)
112*a430fa06SMiquel Raynal #define NFC_CMD(x)		(x)
113*a430fa06SMiquel Raynal #define NFC_ADR_NUM_MSK		(0x7 << 16)
114*a430fa06SMiquel Raynal #define NFC_ADR_NUM(x)		(((x) - 1) << 16)
115*a430fa06SMiquel Raynal #define NFC_SEND_ADR		BIT(19)
116*a430fa06SMiquel Raynal #define NFC_ACCESS_DIR		BIT(20)
117*a430fa06SMiquel Raynal #define NFC_DATA_TRANS		BIT(21)
118*a430fa06SMiquel Raynal #define NFC_SEND_CMD1		BIT(22)
119*a430fa06SMiquel Raynal #define NFC_WAIT_FLAG		BIT(23)
120*a430fa06SMiquel Raynal #define NFC_SEND_CMD2		BIT(24)
121*a430fa06SMiquel Raynal #define NFC_SEQ			BIT(25)
122*a430fa06SMiquel Raynal #define NFC_DATA_SWAP_METHOD	BIT(26)
123*a430fa06SMiquel Raynal #define NFC_ROW_AUTO_INC	BIT(27)
124*a430fa06SMiquel Raynal #define NFC_SEND_CMD3		BIT(28)
125*a430fa06SMiquel Raynal #define NFC_SEND_CMD4		BIT(29)
126*a430fa06SMiquel Raynal #define NFC_CMD_TYPE_MSK	(0x3 << 30)
127*a430fa06SMiquel Raynal #define NFC_NORMAL_OP		(0 << 30)
128*a430fa06SMiquel Raynal #define NFC_ECC_OP		(1 << 30)
129*a430fa06SMiquel Raynal #define NFC_PAGE_OP		(2 << 30)
130*a430fa06SMiquel Raynal 
131*a430fa06SMiquel Raynal /* define bit use in NFC_RCMD_SET */
132*a430fa06SMiquel Raynal #define NFC_READ_CMD_MSK	0xff
133*a430fa06SMiquel Raynal #define NFC_RND_READ_CMD0_MSK	(0xff << 8)
134*a430fa06SMiquel Raynal #define NFC_RND_READ_CMD1_MSK	(0xff << 16)
135*a430fa06SMiquel Raynal 
136*a430fa06SMiquel Raynal /* define bit use in NFC_WCMD_SET */
137*a430fa06SMiquel Raynal #define NFC_PROGRAM_CMD_MSK	0xff
138*a430fa06SMiquel Raynal #define NFC_RND_WRITE_CMD_MSK	(0xff << 8)
139*a430fa06SMiquel Raynal #define NFC_READ_CMD0_MSK	(0xff << 16)
140*a430fa06SMiquel Raynal #define NFC_READ_CMD1_MSK	(0xff << 24)
141*a430fa06SMiquel Raynal 
142*a430fa06SMiquel Raynal /* define bit use in NFC_ECC_CTL */
143*a430fa06SMiquel Raynal #define NFC_ECC_EN		BIT(0)
144*a430fa06SMiquel Raynal #define NFC_ECC_PIPELINE	BIT(3)
145*a430fa06SMiquel Raynal #define NFC_ECC_EXCEPTION	BIT(4)
146*a430fa06SMiquel Raynal #define NFC_ECC_BLOCK_SIZE_MSK	BIT(5)
147*a430fa06SMiquel Raynal #define NFC_ECC_BLOCK_512	(1 << 5)
148*a430fa06SMiquel Raynal #define NFC_RANDOM_EN		BIT(9)
149*a430fa06SMiquel Raynal #define NFC_RANDOM_DIRECTION	BIT(10)
150*a430fa06SMiquel Raynal #define NFC_ECC_MODE_MSK	(0xf << 12)
151*a430fa06SMiquel Raynal #define NFC_ECC_MODE(x)		((x) << 12)
152*a430fa06SMiquel Raynal #define NFC_RANDOM_SEED_MSK	(0x7fff << 16)
153*a430fa06SMiquel Raynal #define NFC_RANDOM_SEED(x)	((x) << 16)
154*a430fa06SMiquel Raynal 
155*a430fa06SMiquel Raynal /* define bit use in NFC_ECC_ST */
156*a430fa06SMiquel Raynal #define NFC_ECC_ERR(x)		BIT(x)
157*a430fa06SMiquel Raynal #define NFC_ECC_PAT_FOUND(x)	BIT(x + 16)
158*a430fa06SMiquel Raynal #define NFC_ECC_ERR_CNT(b, x)	(((x) >> ((b) * 8)) & 0xff)
159*a430fa06SMiquel Raynal 
160*a430fa06SMiquel Raynal #define NFC_DEFAULT_TIMEOUT_MS	1000
161*a430fa06SMiquel Raynal 
162*a430fa06SMiquel Raynal #define NFC_SRAM_SIZE		1024
163*a430fa06SMiquel Raynal 
164*a430fa06SMiquel Raynal #define NFC_MAX_CS		7
165*a430fa06SMiquel Raynal 
166*a430fa06SMiquel Raynal /*
167*a430fa06SMiquel Raynal  * Ready/Busy detection type: describes the Ready/Busy detection modes
168*a430fa06SMiquel Raynal  *
169*a430fa06SMiquel Raynal  * @RB_NONE:	no external detection available, rely on STATUS command
170*a430fa06SMiquel Raynal  *		and software timeouts
171*a430fa06SMiquel Raynal  * @RB_NATIVE:	use sunxi NAND controller Ready/Busy support. The Ready/Busy
172*a430fa06SMiquel Raynal  *		pin of the NAND flash chip must be connected to one of the
173*a430fa06SMiquel Raynal  *		native NAND R/B pins (those which can be muxed to the NAND
174*a430fa06SMiquel Raynal  *		Controller)
175*a430fa06SMiquel Raynal  * @RB_GPIO:	use a simple GPIO to handle Ready/Busy status. The Ready/Busy
176*a430fa06SMiquel Raynal  *		pin of the NAND flash chip must be connected to a GPIO capable
177*a430fa06SMiquel Raynal  *		pin.
178*a430fa06SMiquel Raynal  */
179*a430fa06SMiquel Raynal enum sunxi_nand_rb_type {
180*a430fa06SMiquel Raynal 	RB_NONE,
181*a430fa06SMiquel Raynal 	RB_NATIVE,
182*a430fa06SMiquel Raynal 	RB_GPIO,
183*a430fa06SMiquel Raynal };
184*a430fa06SMiquel Raynal 
185*a430fa06SMiquel Raynal /*
186*a430fa06SMiquel Raynal  * Ready/Busy structure: stores information related to Ready/Busy detection
187*a430fa06SMiquel Raynal  *
188*a430fa06SMiquel Raynal  * @type:	the Ready/Busy detection mode
189*a430fa06SMiquel Raynal  * @info:	information related to the R/B detection mode. Either a gpio
190*a430fa06SMiquel Raynal  *		id or a native R/B id (those supported by the NAND controller).
191*a430fa06SMiquel Raynal  */
192*a430fa06SMiquel Raynal struct sunxi_nand_rb {
193*a430fa06SMiquel Raynal 	enum sunxi_nand_rb_type type;
194*a430fa06SMiquel Raynal 	union {
195*a430fa06SMiquel Raynal 		struct gpio_desc gpio;
196*a430fa06SMiquel Raynal 		int nativeid;
197*a430fa06SMiquel Raynal 	} info;
198*a430fa06SMiquel Raynal };
199*a430fa06SMiquel Raynal 
200*a430fa06SMiquel Raynal /*
201*a430fa06SMiquel Raynal  * Chip Select structure: stores information related to NAND Chip Select
202*a430fa06SMiquel Raynal  *
203*a430fa06SMiquel Raynal  * @cs:		the NAND CS id used to communicate with a NAND Chip
204*a430fa06SMiquel Raynal  * @rb:		the Ready/Busy description
205*a430fa06SMiquel Raynal  */
206*a430fa06SMiquel Raynal struct sunxi_nand_chip_sel {
207*a430fa06SMiquel Raynal 	u8 cs;
208*a430fa06SMiquel Raynal 	struct sunxi_nand_rb rb;
209*a430fa06SMiquel Raynal };
210*a430fa06SMiquel Raynal 
211*a430fa06SMiquel Raynal /*
212*a430fa06SMiquel Raynal  * sunxi HW ECC infos: stores information related to HW ECC support
213*a430fa06SMiquel Raynal  *
214*a430fa06SMiquel Raynal  * @mode:	the sunxi ECC mode field deduced from ECC requirements
215*a430fa06SMiquel Raynal  * @layout:	the OOB layout depending on the ECC requirements and the
216*a430fa06SMiquel Raynal  *		selected ECC mode
217*a430fa06SMiquel Raynal  */
218*a430fa06SMiquel Raynal struct sunxi_nand_hw_ecc {
219*a430fa06SMiquel Raynal 	int mode;
220*a430fa06SMiquel Raynal 	struct nand_ecclayout layout;
221*a430fa06SMiquel Raynal };
222*a430fa06SMiquel Raynal 
223*a430fa06SMiquel Raynal /*
224*a430fa06SMiquel Raynal  * NAND chip structure: stores NAND chip device related information
225*a430fa06SMiquel Raynal  *
226*a430fa06SMiquel Raynal  * @node:		used to store NAND chips into a list
227*a430fa06SMiquel Raynal  * @nand:		base NAND chip structure
228*a430fa06SMiquel Raynal  * @mtd:		base MTD structure
229*a430fa06SMiquel Raynal  * @clk_rate:		clk_rate required for this NAND chip
230*a430fa06SMiquel Raynal  * @timing_cfg		TIMING_CFG register value for this NAND chip
231*a430fa06SMiquel Raynal  * @selected:		current active CS
232*a430fa06SMiquel Raynal  * @nsels:		number of CS lines required by the NAND chip
233*a430fa06SMiquel Raynal  * @sels:		array of CS lines descriptions
234*a430fa06SMiquel Raynal  */
235*a430fa06SMiquel Raynal struct sunxi_nand_chip {
236*a430fa06SMiquel Raynal 	struct list_head node;
237*a430fa06SMiquel Raynal 	struct nand_chip nand;
238*a430fa06SMiquel Raynal 	unsigned long clk_rate;
239*a430fa06SMiquel Raynal 	u32 timing_cfg;
240*a430fa06SMiquel Raynal 	u32 timing_ctl;
241*a430fa06SMiquel Raynal 	int selected;
242*a430fa06SMiquel Raynal 	int addr_cycles;
243*a430fa06SMiquel Raynal 	u32 addr[2];
244*a430fa06SMiquel Raynal 	int cmd_cycles;
245*a430fa06SMiquel Raynal 	u8 cmd[2];
246*a430fa06SMiquel Raynal 	int nsels;
247*a430fa06SMiquel Raynal 	struct sunxi_nand_chip_sel sels[0];
248*a430fa06SMiquel Raynal };
249*a430fa06SMiquel Raynal 
to_sunxi_nand(struct nand_chip * nand)250*a430fa06SMiquel Raynal static inline struct sunxi_nand_chip *to_sunxi_nand(struct nand_chip *nand)
251*a430fa06SMiquel Raynal {
252*a430fa06SMiquel Raynal 	return container_of(nand, struct sunxi_nand_chip, nand);
253*a430fa06SMiquel Raynal }
254*a430fa06SMiquel Raynal 
255*a430fa06SMiquel Raynal /*
256*a430fa06SMiquel Raynal  * NAND Controller structure: stores sunxi NAND controller information
257*a430fa06SMiquel Raynal  *
258*a430fa06SMiquel Raynal  * @controller:		base controller structure
259*a430fa06SMiquel Raynal  * @dev:		parent device (used to print error messages)
260*a430fa06SMiquel Raynal  * @regs:		NAND controller registers
261*a430fa06SMiquel Raynal  * @ahb_clk:		NAND Controller AHB clock
262*a430fa06SMiquel Raynal  * @mod_clk:		NAND Controller mod clock
263*a430fa06SMiquel Raynal  * @assigned_cs:	bitmask describing already assigned CS lines
264*a430fa06SMiquel Raynal  * @clk_rate:		NAND controller current clock rate
265*a430fa06SMiquel Raynal  * @chips:		a list containing all the NAND chips attached to
266*a430fa06SMiquel Raynal  *			this NAND controller
267*a430fa06SMiquel Raynal  * @complete:		a completion object used to wait for NAND
268*a430fa06SMiquel Raynal  *			controller events
269*a430fa06SMiquel Raynal  */
270*a430fa06SMiquel Raynal struct sunxi_nfc {
271*a430fa06SMiquel Raynal 	struct nand_hw_control controller;
272*a430fa06SMiquel Raynal 	struct device *dev;
273*a430fa06SMiquel Raynal 	void __iomem *regs;
274*a430fa06SMiquel Raynal 	struct clk *ahb_clk;
275*a430fa06SMiquel Raynal 	struct clk *mod_clk;
276*a430fa06SMiquel Raynal 	unsigned long assigned_cs;
277*a430fa06SMiquel Raynal 	unsigned long clk_rate;
278*a430fa06SMiquel Raynal 	struct list_head chips;
279*a430fa06SMiquel Raynal };
280*a430fa06SMiquel Raynal 
to_sunxi_nfc(struct nand_hw_control * ctrl)281*a430fa06SMiquel Raynal static inline struct sunxi_nfc *to_sunxi_nfc(struct nand_hw_control *ctrl)
282*a430fa06SMiquel Raynal {
283*a430fa06SMiquel Raynal 	return container_of(ctrl, struct sunxi_nfc, controller);
284*a430fa06SMiquel Raynal }
285*a430fa06SMiquel Raynal 
sunxi_nfc_set_clk_rate(unsigned long hz)286*a430fa06SMiquel Raynal static void sunxi_nfc_set_clk_rate(unsigned long hz)
287*a430fa06SMiquel Raynal {
288*a430fa06SMiquel Raynal 	struct sunxi_ccm_reg *const ccm =
289*a430fa06SMiquel Raynal 	(struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
290*a430fa06SMiquel Raynal 	int div_m, div_n;
291*a430fa06SMiquel Raynal 
292*a430fa06SMiquel Raynal 	div_m = (clock_get_pll6() + hz - 1) / hz;
293*a430fa06SMiquel Raynal 	for (div_n = 0; div_n < 3 && div_m > 16; div_n++) {
294*a430fa06SMiquel Raynal 		if (div_m % 2)
295*a430fa06SMiquel Raynal 			div_m++;
296*a430fa06SMiquel Raynal 		div_m >>= 1;
297*a430fa06SMiquel Raynal 	}
298*a430fa06SMiquel Raynal 	if (div_m > 16)
299*a430fa06SMiquel Raynal 		div_m = 16;
300*a430fa06SMiquel Raynal 
301*a430fa06SMiquel Raynal 	/* config mod clock */
302*a430fa06SMiquel Raynal 	writel(CCM_NAND_CTRL_ENABLE | CCM_NAND_CTRL_PLL6 |
303*a430fa06SMiquel Raynal 	       CCM_NAND_CTRL_N(div_n) | CCM_NAND_CTRL_M(div_m),
304*a430fa06SMiquel Raynal 	       &ccm->nand0_clk_cfg);
305*a430fa06SMiquel Raynal 
306*a430fa06SMiquel Raynal 	/* gate on nand clock */
307*a430fa06SMiquel Raynal 	setbits_le32(&ccm->ahb_gate0, (1 << AHB_GATE_OFFSET_NAND0));
308*a430fa06SMiquel Raynal #ifdef CONFIG_MACH_SUN9I
309*a430fa06SMiquel Raynal 	setbits_le32(&ccm->ahb_gate1, (1 << AHB_GATE_OFFSET_DMA));
310*a430fa06SMiquel Raynal #else
311*a430fa06SMiquel Raynal 	setbits_le32(&ccm->ahb_gate0, (1 << AHB_GATE_OFFSET_DMA));
312*a430fa06SMiquel Raynal #endif
313*a430fa06SMiquel Raynal }
314*a430fa06SMiquel Raynal 
sunxi_nfc_wait_int(struct sunxi_nfc * nfc,u32 flags,unsigned int timeout_ms)315*a430fa06SMiquel Raynal static int sunxi_nfc_wait_int(struct sunxi_nfc *nfc, u32 flags,
316*a430fa06SMiquel Raynal 			      unsigned int timeout_ms)
317*a430fa06SMiquel Raynal {
318*a430fa06SMiquel Raynal 	unsigned int timeout_ticks;
319*a430fa06SMiquel Raynal 	u32 time_start, status;
320*a430fa06SMiquel Raynal 	int ret = -ETIMEDOUT;
321*a430fa06SMiquel Raynal 
322*a430fa06SMiquel Raynal 	if (!timeout_ms)
323*a430fa06SMiquel Raynal 		timeout_ms = NFC_DEFAULT_TIMEOUT_MS;
324*a430fa06SMiquel Raynal 
325*a430fa06SMiquel Raynal 	timeout_ticks = (timeout_ms * CONFIG_SYS_HZ) / 1000;
326*a430fa06SMiquel Raynal 
327*a430fa06SMiquel Raynal 	time_start = get_timer(0);
328*a430fa06SMiquel Raynal 
329*a430fa06SMiquel Raynal 	do {
330*a430fa06SMiquel Raynal 		status = readl(nfc->regs + NFC_REG_ST);
331*a430fa06SMiquel Raynal 		if ((status & flags) == flags) {
332*a430fa06SMiquel Raynal 			ret = 0;
333*a430fa06SMiquel Raynal 			break;
334*a430fa06SMiquel Raynal 		}
335*a430fa06SMiquel Raynal 
336*a430fa06SMiquel Raynal 		udelay(1);
337*a430fa06SMiquel Raynal 	} while (get_timer(time_start) < timeout_ticks);
338*a430fa06SMiquel Raynal 
339*a430fa06SMiquel Raynal 	writel(status & flags, nfc->regs + NFC_REG_ST);
340*a430fa06SMiquel Raynal 
341*a430fa06SMiquel Raynal 	return ret;
342*a430fa06SMiquel Raynal }
343*a430fa06SMiquel Raynal 
sunxi_nfc_wait_cmd_fifo_empty(struct sunxi_nfc * nfc)344*a430fa06SMiquel Raynal static int sunxi_nfc_wait_cmd_fifo_empty(struct sunxi_nfc *nfc)
345*a430fa06SMiquel Raynal {
346*a430fa06SMiquel Raynal 	unsigned long timeout = (CONFIG_SYS_HZ *
347*a430fa06SMiquel Raynal 				 NFC_DEFAULT_TIMEOUT_MS) / 1000;
348*a430fa06SMiquel Raynal 	u32 time_start;
349*a430fa06SMiquel Raynal 
350*a430fa06SMiquel Raynal 	time_start = get_timer(0);
351*a430fa06SMiquel Raynal 	do {
352*a430fa06SMiquel Raynal 		if (!(readl(nfc->regs + NFC_REG_ST) & NFC_CMD_FIFO_STATUS))
353*a430fa06SMiquel Raynal 			return 0;
354*a430fa06SMiquel Raynal 	} while (get_timer(time_start) < timeout);
355*a430fa06SMiquel Raynal 
356*a430fa06SMiquel Raynal 	dev_err(nfc->dev, "wait for empty cmd FIFO timedout\n");
357*a430fa06SMiquel Raynal 	return -ETIMEDOUT;
358*a430fa06SMiquel Raynal }
359*a430fa06SMiquel Raynal 
sunxi_nfc_rst(struct sunxi_nfc * nfc)360*a430fa06SMiquel Raynal static int sunxi_nfc_rst(struct sunxi_nfc *nfc)
361*a430fa06SMiquel Raynal {
362*a430fa06SMiquel Raynal 	unsigned long timeout = (CONFIG_SYS_HZ *
363*a430fa06SMiquel Raynal 				 NFC_DEFAULT_TIMEOUT_MS) / 1000;
364*a430fa06SMiquel Raynal 	u32 time_start;
365*a430fa06SMiquel Raynal 
366*a430fa06SMiquel Raynal 	writel(0, nfc->regs + NFC_REG_ECC_CTL);
367*a430fa06SMiquel Raynal 	writel(NFC_RESET, nfc->regs + NFC_REG_CTL);
368*a430fa06SMiquel Raynal 
369*a430fa06SMiquel Raynal 	time_start = get_timer(0);
370*a430fa06SMiquel Raynal 	do {
371*a430fa06SMiquel Raynal 		if (!(readl(nfc->regs + NFC_REG_CTL) & NFC_RESET))
372*a430fa06SMiquel Raynal 			return 0;
373*a430fa06SMiquel Raynal 	} while (get_timer(time_start) < timeout);
374*a430fa06SMiquel Raynal 
375*a430fa06SMiquel Raynal 	dev_err(nfc->dev, "wait for NAND controller reset timedout\n");
376*a430fa06SMiquel Raynal 	return -ETIMEDOUT;
377*a430fa06SMiquel Raynal }
378*a430fa06SMiquel Raynal 
sunxi_nfc_dev_ready(struct mtd_info * mtd)379*a430fa06SMiquel Raynal static int sunxi_nfc_dev_ready(struct mtd_info *mtd)
380*a430fa06SMiquel Raynal {
381*a430fa06SMiquel Raynal 	struct nand_chip *nand = mtd_to_nand(mtd);
382*a430fa06SMiquel Raynal 	struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
383*a430fa06SMiquel Raynal 	struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
384*a430fa06SMiquel Raynal 	struct sunxi_nand_rb *rb;
385*a430fa06SMiquel Raynal 	unsigned long timeo = (sunxi_nand->nand.state == FL_ERASING ? 400 : 20);
386*a430fa06SMiquel Raynal 	int ret;
387*a430fa06SMiquel Raynal 
388*a430fa06SMiquel Raynal 	if (sunxi_nand->selected < 0)
389*a430fa06SMiquel Raynal 		return 0;
390*a430fa06SMiquel Raynal 
391*a430fa06SMiquel Raynal 	rb = &sunxi_nand->sels[sunxi_nand->selected].rb;
392*a430fa06SMiquel Raynal 
393*a430fa06SMiquel Raynal 	switch (rb->type) {
394*a430fa06SMiquel Raynal 	case RB_NATIVE:
395*a430fa06SMiquel Raynal 		ret = !!(readl(nfc->regs + NFC_REG_ST) &
396*a430fa06SMiquel Raynal 			 NFC_RB_STATE(rb->info.nativeid));
397*a430fa06SMiquel Raynal 		if (ret)
398*a430fa06SMiquel Raynal 			break;
399*a430fa06SMiquel Raynal 
400*a430fa06SMiquel Raynal 		sunxi_nfc_wait_int(nfc, NFC_RB_B2R, timeo);
401*a430fa06SMiquel Raynal 		ret = !!(readl(nfc->regs + NFC_REG_ST) &
402*a430fa06SMiquel Raynal 			 NFC_RB_STATE(rb->info.nativeid));
403*a430fa06SMiquel Raynal 		break;
404*a430fa06SMiquel Raynal 	case RB_GPIO:
405*a430fa06SMiquel Raynal 		ret = dm_gpio_get_value(&rb->info.gpio);
406*a430fa06SMiquel Raynal 		break;
407*a430fa06SMiquel Raynal 	case RB_NONE:
408*a430fa06SMiquel Raynal 	default:
409*a430fa06SMiquel Raynal 		ret = 0;
410*a430fa06SMiquel Raynal 		dev_err(nfc->dev, "cannot check R/B NAND status!\n");
411*a430fa06SMiquel Raynal 		break;
412*a430fa06SMiquel Raynal 	}
413*a430fa06SMiquel Raynal 
414*a430fa06SMiquel Raynal 	return ret;
415*a430fa06SMiquel Raynal }
416*a430fa06SMiquel Raynal 
sunxi_nfc_select_chip(struct mtd_info * mtd,int chip)417*a430fa06SMiquel Raynal static void sunxi_nfc_select_chip(struct mtd_info *mtd, int chip)
418*a430fa06SMiquel Raynal {
419*a430fa06SMiquel Raynal 	struct nand_chip *nand = mtd_to_nand(mtd);
420*a430fa06SMiquel Raynal 	struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
421*a430fa06SMiquel Raynal 	struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
422*a430fa06SMiquel Raynal 	struct sunxi_nand_chip_sel *sel;
423*a430fa06SMiquel Raynal 	u32 ctl;
424*a430fa06SMiquel Raynal 
425*a430fa06SMiquel Raynal 	if (chip > 0 && chip >= sunxi_nand->nsels)
426*a430fa06SMiquel Raynal 		return;
427*a430fa06SMiquel Raynal 
428*a430fa06SMiquel Raynal 	if (chip == sunxi_nand->selected)
429*a430fa06SMiquel Raynal 		return;
430*a430fa06SMiquel Raynal 
431*a430fa06SMiquel Raynal 	ctl = readl(nfc->regs + NFC_REG_CTL) &
432*a430fa06SMiquel Raynal 	      ~(NFC_PAGE_SHIFT_MSK | NFC_CE_SEL_MSK | NFC_RB_SEL_MSK | NFC_EN);
433*a430fa06SMiquel Raynal 
434*a430fa06SMiquel Raynal 	if (chip >= 0) {
435*a430fa06SMiquel Raynal 		sel = &sunxi_nand->sels[chip];
436*a430fa06SMiquel Raynal 
437*a430fa06SMiquel Raynal 		ctl |= NFC_CE_SEL(sel->cs) | NFC_EN |
438*a430fa06SMiquel Raynal 		       NFC_PAGE_SHIFT(nand->page_shift - 10);
439*a430fa06SMiquel Raynal 		if (sel->rb.type == RB_NONE) {
440*a430fa06SMiquel Raynal 			nand->dev_ready = NULL;
441*a430fa06SMiquel Raynal 		} else {
442*a430fa06SMiquel Raynal 			nand->dev_ready = sunxi_nfc_dev_ready;
443*a430fa06SMiquel Raynal 			if (sel->rb.type == RB_NATIVE)
444*a430fa06SMiquel Raynal 				ctl |= NFC_RB_SEL(sel->rb.info.nativeid);
445*a430fa06SMiquel Raynal 		}
446*a430fa06SMiquel Raynal 
447*a430fa06SMiquel Raynal 		writel(mtd->writesize, nfc->regs + NFC_REG_SPARE_AREA);
448*a430fa06SMiquel Raynal 
449*a430fa06SMiquel Raynal 		if (nfc->clk_rate != sunxi_nand->clk_rate) {
450*a430fa06SMiquel Raynal 			sunxi_nfc_set_clk_rate(sunxi_nand->clk_rate);
451*a430fa06SMiquel Raynal 			nfc->clk_rate = sunxi_nand->clk_rate;
452*a430fa06SMiquel Raynal 		}
453*a430fa06SMiquel Raynal 	}
454*a430fa06SMiquel Raynal 
455*a430fa06SMiquel Raynal 	writel(sunxi_nand->timing_ctl, nfc->regs + NFC_REG_TIMING_CTL);
456*a430fa06SMiquel Raynal 	writel(sunxi_nand->timing_cfg, nfc->regs + NFC_REG_TIMING_CFG);
457*a430fa06SMiquel Raynal 	writel(ctl, nfc->regs + NFC_REG_CTL);
458*a430fa06SMiquel Raynal 
459*a430fa06SMiquel Raynal 	sunxi_nand->selected = chip;
460*a430fa06SMiquel Raynal }
461*a430fa06SMiquel Raynal 
sunxi_nfc_read_buf(struct mtd_info * mtd,uint8_t * buf,int len)462*a430fa06SMiquel Raynal static void sunxi_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
463*a430fa06SMiquel Raynal {
464*a430fa06SMiquel Raynal 	struct nand_chip *nand = mtd_to_nand(mtd);
465*a430fa06SMiquel Raynal 	struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
466*a430fa06SMiquel Raynal 	struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
467*a430fa06SMiquel Raynal 	int ret;
468*a430fa06SMiquel Raynal 	int cnt;
469*a430fa06SMiquel Raynal 	int offs = 0;
470*a430fa06SMiquel Raynal 	u32 tmp;
471*a430fa06SMiquel Raynal 
472*a430fa06SMiquel Raynal 	while (len > offs) {
473*a430fa06SMiquel Raynal 		cnt = min(len - offs, NFC_SRAM_SIZE);
474*a430fa06SMiquel Raynal 
475*a430fa06SMiquel Raynal 		ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
476*a430fa06SMiquel Raynal 		if (ret)
477*a430fa06SMiquel Raynal 			break;
478*a430fa06SMiquel Raynal 
479*a430fa06SMiquel Raynal 		writel(cnt, nfc->regs + NFC_REG_CNT);
480*a430fa06SMiquel Raynal 		tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD;
481*a430fa06SMiquel Raynal 		writel(tmp, nfc->regs + NFC_REG_CMD);
482*a430fa06SMiquel Raynal 
483*a430fa06SMiquel Raynal 		ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
484*a430fa06SMiquel Raynal 		if (ret)
485*a430fa06SMiquel Raynal 			break;
486*a430fa06SMiquel Raynal 
487*a430fa06SMiquel Raynal 		if (buf)
488*a430fa06SMiquel Raynal 			memcpy_fromio(buf + offs, nfc->regs + NFC_RAM0_BASE,
489*a430fa06SMiquel Raynal 				      cnt);
490*a430fa06SMiquel Raynal 		offs += cnt;
491*a430fa06SMiquel Raynal 	}
492*a430fa06SMiquel Raynal }
493*a430fa06SMiquel Raynal 
sunxi_nfc_write_buf(struct mtd_info * mtd,const uint8_t * buf,int len)494*a430fa06SMiquel Raynal static void sunxi_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
495*a430fa06SMiquel Raynal 				int len)
496*a430fa06SMiquel Raynal {
497*a430fa06SMiquel Raynal 	struct nand_chip *nand = mtd_to_nand(mtd);
498*a430fa06SMiquel Raynal 	struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
499*a430fa06SMiquel Raynal 	struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
500*a430fa06SMiquel Raynal 	int ret;
501*a430fa06SMiquel Raynal 	int cnt;
502*a430fa06SMiquel Raynal 	int offs = 0;
503*a430fa06SMiquel Raynal 	u32 tmp;
504*a430fa06SMiquel Raynal 
505*a430fa06SMiquel Raynal 	while (len > offs) {
506*a430fa06SMiquel Raynal 		cnt = min(len - offs, NFC_SRAM_SIZE);
507*a430fa06SMiquel Raynal 
508*a430fa06SMiquel Raynal 		ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
509*a430fa06SMiquel Raynal 		if (ret)
510*a430fa06SMiquel Raynal 			break;
511*a430fa06SMiquel Raynal 
512*a430fa06SMiquel Raynal 		writel(cnt, nfc->regs + NFC_REG_CNT);
513*a430fa06SMiquel Raynal 		memcpy_toio(nfc->regs + NFC_RAM0_BASE, buf + offs, cnt);
514*a430fa06SMiquel Raynal 		tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD |
515*a430fa06SMiquel Raynal 		      NFC_ACCESS_DIR;
516*a430fa06SMiquel Raynal 		writel(tmp, nfc->regs + NFC_REG_CMD);
517*a430fa06SMiquel Raynal 
518*a430fa06SMiquel Raynal 		ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
519*a430fa06SMiquel Raynal 		if (ret)
520*a430fa06SMiquel Raynal 			break;
521*a430fa06SMiquel Raynal 
522*a430fa06SMiquel Raynal 		offs += cnt;
523*a430fa06SMiquel Raynal 	}
524*a430fa06SMiquel Raynal }
525*a430fa06SMiquel Raynal 
sunxi_nfc_read_byte(struct mtd_info * mtd)526*a430fa06SMiquel Raynal static uint8_t sunxi_nfc_read_byte(struct mtd_info *mtd)
527*a430fa06SMiquel Raynal {
528*a430fa06SMiquel Raynal 	uint8_t ret;
529*a430fa06SMiquel Raynal 
530*a430fa06SMiquel Raynal 	sunxi_nfc_read_buf(mtd, &ret, 1);
531*a430fa06SMiquel Raynal 
532*a430fa06SMiquel Raynal 	return ret;
533*a430fa06SMiquel Raynal }
534*a430fa06SMiquel Raynal 
sunxi_nfc_cmd_ctrl(struct mtd_info * mtd,int dat,unsigned int ctrl)535*a430fa06SMiquel Raynal static void sunxi_nfc_cmd_ctrl(struct mtd_info *mtd, int dat,
536*a430fa06SMiquel Raynal 			       unsigned int ctrl)
537*a430fa06SMiquel Raynal {
538*a430fa06SMiquel Raynal 	struct nand_chip *nand = mtd_to_nand(mtd);
539*a430fa06SMiquel Raynal 	struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
540*a430fa06SMiquel Raynal 	struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
541*a430fa06SMiquel Raynal 	int ret;
542*a430fa06SMiquel Raynal 	u32 tmp;
543*a430fa06SMiquel Raynal 
544*a430fa06SMiquel Raynal 	ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
545*a430fa06SMiquel Raynal 	if (ret)
546*a430fa06SMiquel Raynal 		return;
547*a430fa06SMiquel Raynal 
548*a430fa06SMiquel Raynal 	if (ctrl & NAND_CTRL_CHANGE) {
549*a430fa06SMiquel Raynal 		tmp = readl(nfc->regs + NFC_REG_CTL);
550*a430fa06SMiquel Raynal 		if (ctrl & NAND_NCE)
551*a430fa06SMiquel Raynal 			tmp |= NFC_CE_CTL;
552*a430fa06SMiquel Raynal 		else
553*a430fa06SMiquel Raynal 			tmp &= ~NFC_CE_CTL;
554*a430fa06SMiquel Raynal 		writel(tmp, nfc->regs + NFC_REG_CTL);
555*a430fa06SMiquel Raynal 	}
556*a430fa06SMiquel Raynal 
557*a430fa06SMiquel Raynal 	if (dat == NAND_CMD_NONE && (ctrl & NAND_NCE) &&
558*a430fa06SMiquel Raynal 	    !(ctrl & (NAND_CLE | NAND_ALE))) {
559*a430fa06SMiquel Raynal 		u32 cmd = 0;
560*a430fa06SMiquel Raynal 
561*a430fa06SMiquel Raynal 		if (!sunxi_nand->addr_cycles && !sunxi_nand->cmd_cycles)
562*a430fa06SMiquel Raynal 			return;
563*a430fa06SMiquel Raynal 
564*a430fa06SMiquel Raynal 		if (sunxi_nand->cmd_cycles--)
565*a430fa06SMiquel Raynal 			cmd |= NFC_SEND_CMD1 | sunxi_nand->cmd[0];
566*a430fa06SMiquel Raynal 
567*a430fa06SMiquel Raynal 		if (sunxi_nand->cmd_cycles--) {
568*a430fa06SMiquel Raynal 			cmd |= NFC_SEND_CMD2;
569*a430fa06SMiquel Raynal 			writel(sunxi_nand->cmd[1],
570*a430fa06SMiquel Raynal 			       nfc->regs + NFC_REG_RCMD_SET);
571*a430fa06SMiquel Raynal 		}
572*a430fa06SMiquel Raynal 
573*a430fa06SMiquel Raynal 		sunxi_nand->cmd_cycles = 0;
574*a430fa06SMiquel Raynal 
575*a430fa06SMiquel Raynal 		if (sunxi_nand->addr_cycles) {
576*a430fa06SMiquel Raynal 			cmd |= NFC_SEND_ADR |
577*a430fa06SMiquel Raynal 			       NFC_ADR_NUM(sunxi_nand->addr_cycles);
578*a430fa06SMiquel Raynal 			writel(sunxi_nand->addr[0],
579*a430fa06SMiquel Raynal 			       nfc->regs + NFC_REG_ADDR_LOW);
580*a430fa06SMiquel Raynal 		}
581*a430fa06SMiquel Raynal 
582*a430fa06SMiquel Raynal 		if (sunxi_nand->addr_cycles > 4)
583*a430fa06SMiquel Raynal 			writel(sunxi_nand->addr[1],
584*a430fa06SMiquel Raynal 			       nfc->regs + NFC_REG_ADDR_HIGH);
585*a430fa06SMiquel Raynal 
586*a430fa06SMiquel Raynal 		writel(cmd, nfc->regs + NFC_REG_CMD);
587*a430fa06SMiquel Raynal 		sunxi_nand->addr[0] = 0;
588*a430fa06SMiquel Raynal 		sunxi_nand->addr[1] = 0;
589*a430fa06SMiquel Raynal 		sunxi_nand->addr_cycles = 0;
590*a430fa06SMiquel Raynal 		sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
591*a430fa06SMiquel Raynal 	}
592*a430fa06SMiquel Raynal 
593*a430fa06SMiquel Raynal 	if (ctrl & NAND_CLE) {
594*a430fa06SMiquel Raynal 		sunxi_nand->cmd[sunxi_nand->cmd_cycles++] = dat;
595*a430fa06SMiquel Raynal 	} else if (ctrl & NAND_ALE) {
596*a430fa06SMiquel Raynal 		sunxi_nand->addr[sunxi_nand->addr_cycles / 4] |=
597*a430fa06SMiquel Raynal 				dat << ((sunxi_nand->addr_cycles % 4) * 8);
598*a430fa06SMiquel Raynal 		sunxi_nand->addr_cycles++;
599*a430fa06SMiquel Raynal 	}
600*a430fa06SMiquel Raynal }
601*a430fa06SMiquel Raynal 
602*a430fa06SMiquel Raynal /* These seed values have been extracted from Allwinner's BSP */
603*a430fa06SMiquel Raynal static const u16 sunxi_nfc_randomizer_page_seeds[] = {
604*a430fa06SMiquel Raynal 	0x2b75, 0x0bd0, 0x5ca3, 0x62d1, 0x1c93, 0x07e9, 0x2162, 0x3a72,
605*a430fa06SMiquel Raynal 	0x0d67, 0x67f9, 0x1be7, 0x077d, 0x032f, 0x0dac, 0x2716, 0x2436,
606*a430fa06SMiquel Raynal 	0x7922, 0x1510, 0x3860, 0x5287, 0x480f, 0x4252, 0x1789, 0x5a2d,
607*a430fa06SMiquel Raynal 	0x2a49, 0x5e10, 0x437f, 0x4b4e, 0x2f45, 0x216e, 0x5cb7, 0x7130,
608*a430fa06SMiquel Raynal 	0x2a3f, 0x60e4, 0x4dc9, 0x0ef0, 0x0f52, 0x1bb9, 0x6211, 0x7a56,
609*a430fa06SMiquel Raynal 	0x226d, 0x4ea7, 0x6f36, 0x3692, 0x38bf, 0x0c62, 0x05eb, 0x4c55,
610*a430fa06SMiquel Raynal 	0x60f4, 0x728c, 0x3b6f, 0x2037, 0x7f69, 0x0936, 0x651a, 0x4ceb,
611*a430fa06SMiquel Raynal 	0x6218, 0x79f3, 0x383f, 0x18d9, 0x4f05, 0x5c82, 0x2912, 0x6f17,
612*a430fa06SMiquel Raynal 	0x6856, 0x5938, 0x1007, 0x61ab, 0x3e7f, 0x57c2, 0x542f, 0x4f62,
613*a430fa06SMiquel Raynal 	0x7454, 0x2eac, 0x7739, 0x42d4, 0x2f90, 0x435a, 0x2e52, 0x2064,
614*a430fa06SMiquel Raynal 	0x637c, 0x66ad, 0x2c90, 0x0bad, 0x759c, 0x0029, 0x0986, 0x7126,
615*a430fa06SMiquel Raynal 	0x1ca7, 0x1605, 0x386a, 0x27f5, 0x1380, 0x6d75, 0x24c3, 0x0f8e,
616*a430fa06SMiquel Raynal 	0x2b7a, 0x1418, 0x1fd1, 0x7dc1, 0x2d8e, 0x43af, 0x2267, 0x7da3,
617*a430fa06SMiquel Raynal 	0x4e3d, 0x1338, 0x50db, 0x454d, 0x764d, 0x40a3, 0x42e6, 0x262b,
618*a430fa06SMiquel Raynal 	0x2d2e, 0x1aea, 0x2e17, 0x173d, 0x3a6e, 0x71bf, 0x25f9, 0x0a5d,
619*a430fa06SMiquel Raynal 	0x7c57, 0x0fbe, 0x46ce, 0x4939, 0x6b17, 0x37bb, 0x3e91, 0x76db,
620*a430fa06SMiquel Raynal };
621*a430fa06SMiquel Raynal 
622*a430fa06SMiquel Raynal /*
623*a430fa06SMiquel Raynal  * sunxi_nfc_randomizer_ecc512_seeds and sunxi_nfc_randomizer_ecc1024_seeds
624*a430fa06SMiquel Raynal  * have been generated using
625*a430fa06SMiquel Raynal  * sunxi_nfc_randomizer_step(seed, (step_size * 8) + 15), which is what
626*a430fa06SMiquel Raynal  * the randomizer engine does internally before de/scrambling OOB data.
627*a430fa06SMiquel Raynal  *
628*a430fa06SMiquel Raynal  * Those tables are statically defined to avoid calculating randomizer state
629*a430fa06SMiquel Raynal  * at runtime.
630*a430fa06SMiquel Raynal  */
631*a430fa06SMiquel Raynal static const u16 sunxi_nfc_randomizer_ecc512_seeds[] = {
632*a430fa06SMiquel Raynal 	0x3346, 0x367f, 0x1f18, 0x769a, 0x4f64, 0x068c, 0x2ef1, 0x6b64,
633*a430fa06SMiquel Raynal 	0x28a9, 0x15d7, 0x30f8, 0x3659, 0x53db, 0x7c5f, 0x71d4, 0x4409,
634*a430fa06SMiquel Raynal 	0x26eb, 0x03cc, 0x655d, 0x47d4, 0x4daa, 0x0877, 0x712d, 0x3617,
635*a430fa06SMiquel Raynal 	0x3264, 0x49aa, 0x7f9e, 0x588e, 0x4fbc, 0x7176, 0x7f91, 0x6c6d,
636*a430fa06SMiquel Raynal 	0x4b95, 0x5fb7, 0x3844, 0x4037, 0x0184, 0x081b, 0x0ee8, 0x5b91,
637*a430fa06SMiquel Raynal 	0x293d, 0x1f71, 0x0e6f, 0x402b, 0x5122, 0x1e52, 0x22be, 0x3d2d,
638*a430fa06SMiquel Raynal 	0x75bc, 0x7c60, 0x6291, 0x1a2f, 0x61d4, 0x74aa, 0x4140, 0x29ab,
639*a430fa06SMiquel Raynal 	0x472d, 0x2852, 0x017e, 0x15e8, 0x5ec2, 0x17cf, 0x7d0f, 0x06b8,
640*a430fa06SMiquel Raynal 	0x117a, 0x6b94, 0x789b, 0x3126, 0x6ac5, 0x5be7, 0x150f, 0x51f8,
641*a430fa06SMiquel Raynal 	0x7889, 0x0aa5, 0x663d, 0x77e8, 0x0b87, 0x3dcb, 0x360d, 0x218b,
642*a430fa06SMiquel Raynal 	0x512f, 0x7dc9, 0x6a4d, 0x630a, 0x3547, 0x1dd2, 0x5aea, 0x69a5,
643*a430fa06SMiquel Raynal 	0x7bfa, 0x5e4f, 0x1519, 0x6430, 0x3a0e, 0x5eb3, 0x5425, 0x0c7a,
644*a430fa06SMiquel Raynal 	0x5540, 0x3670, 0x63c1, 0x31e9, 0x5a39, 0x2de7, 0x5979, 0x2891,
645*a430fa06SMiquel Raynal 	0x1562, 0x014b, 0x5b05, 0x2756, 0x5a34, 0x13aa, 0x6cb5, 0x2c36,
646*a430fa06SMiquel Raynal 	0x5e72, 0x1306, 0x0861, 0x15ef, 0x1ee8, 0x5a37, 0x7ac4, 0x45dd,
647*a430fa06SMiquel Raynal 	0x44c4, 0x7266, 0x2f41, 0x3ccc, 0x045e, 0x7d40, 0x7c66, 0x0fa0,
648*a430fa06SMiquel Raynal };
649*a430fa06SMiquel Raynal 
650*a430fa06SMiquel Raynal static const u16 sunxi_nfc_randomizer_ecc1024_seeds[] = {
651*a430fa06SMiquel Raynal 	0x2cf5, 0x35f1, 0x63a4, 0x5274, 0x2bd2, 0x778b, 0x7285, 0x32b6,
652*a430fa06SMiquel Raynal 	0x6a5c, 0x70d6, 0x757d, 0x6769, 0x5375, 0x1e81, 0x0cf3, 0x3982,
653*a430fa06SMiquel Raynal 	0x6787, 0x042a, 0x6c49, 0x1925, 0x56a8, 0x40a9, 0x063e, 0x7bd9,
654*a430fa06SMiquel Raynal 	0x4dbf, 0x55ec, 0x672e, 0x7334, 0x5185, 0x4d00, 0x232a, 0x7e07,
655*a430fa06SMiquel Raynal 	0x445d, 0x6b92, 0x528f, 0x4255, 0x53ba, 0x7d82, 0x2a2e, 0x3a4e,
656*a430fa06SMiquel Raynal 	0x75eb, 0x450c, 0x6844, 0x1b5d, 0x581a, 0x4cc6, 0x0379, 0x37b2,
657*a430fa06SMiquel Raynal 	0x419f, 0x0e92, 0x6b27, 0x5624, 0x01e3, 0x07c1, 0x44a5, 0x130c,
658*a430fa06SMiquel Raynal 	0x13e8, 0x5910, 0x0876, 0x60c5, 0x54e3, 0x5b7f, 0x2269, 0x509f,
659*a430fa06SMiquel Raynal 	0x7665, 0x36fd, 0x3e9a, 0x0579, 0x6295, 0x14ef, 0x0a81, 0x1bcc,
660*a430fa06SMiquel Raynal 	0x4b16, 0x64db, 0x0514, 0x4f07, 0x0591, 0x3576, 0x6853, 0x0d9e,
661*a430fa06SMiquel Raynal 	0x259f, 0x38b7, 0x64fb, 0x3094, 0x4693, 0x6ddd, 0x29bb, 0x0bc8,
662*a430fa06SMiquel Raynal 	0x3f47, 0x490e, 0x0c0e, 0x7933, 0x3c9e, 0x5840, 0x398d, 0x3e68,
663*a430fa06SMiquel Raynal 	0x4af1, 0x71f5, 0x57cf, 0x1121, 0x64eb, 0x3579, 0x15ac, 0x584d,
664*a430fa06SMiquel Raynal 	0x5f2a, 0x47e2, 0x6528, 0x6eac, 0x196e, 0x6b96, 0x0450, 0x0179,
665*a430fa06SMiquel Raynal 	0x609c, 0x06e1, 0x4626, 0x42c7, 0x273e, 0x486f, 0x0705, 0x1601,
666*a430fa06SMiquel Raynal 	0x145b, 0x407e, 0x062b, 0x57a5, 0x53f9, 0x5659, 0x4410, 0x3ccd,
667*a430fa06SMiquel Raynal };
668*a430fa06SMiquel Raynal 
sunxi_nfc_randomizer_step(u16 state,int count)669*a430fa06SMiquel Raynal static u16 sunxi_nfc_randomizer_step(u16 state, int count)
670*a430fa06SMiquel Raynal {
671*a430fa06SMiquel Raynal 	state &= 0x7fff;
672*a430fa06SMiquel Raynal 
673*a430fa06SMiquel Raynal 	/*
674*a430fa06SMiquel Raynal 	 * This loop is just a simple implementation of a Fibonacci LFSR using
675*a430fa06SMiquel Raynal 	 * the x16 + x15 + 1 polynomial.
676*a430fa06SMiquel Raynal 	 */
677*a430fa06SMiquel Raynal 	while (count--)
678*a430fa06SMiquel Raynal 		state = ((state >> 1) |
679*a430fa06SMiquel Raynal 			 (((state ^ (state >> 1)) & 1) << 14)) & 0x7fff;
680*a430fa06SMiquel Raynal 
681*a430fa06SMiquel Raynal 	return state;
682*a430fa06SMiquel Raynal }
683*a430fa06SMiquel Raynal 
sunxi_nfc_randomizer_state(struct mtd_info * mtd,int page,bool ecc)684*a430fa06SMiquel Raynal static u16 sunxi_nfc_randomizer_state(struct mtd_info *mtd, int page, bool ecc)
685*a430fa06SMiquel Raynal {
686*a430fa06SMiquel Raynal 	const u16 *seeds = sunxi_nfc_randomizer_page_seeds;
687*a430fa06SMiquel Raynal 	int mod = mtd->erasesize / mtd->writesize;
688*a430fa06SMiquel Raynal 
689*a430fa06SMiquel Raynal 	if (mod > ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds))
690*a430fa06SMiquel Raynal 		mod = ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds);
691*a430fa06SMiquel Raynal 
692*a430fa06SMiquel Raynal 	if (ecc) {
693*a430fa06SMiquel Raynal 		if (mtd->ecc_step_size == 512)
694*a430fa06SMiquel Raynal 			seeds = sunxi_nfc_randomizer_ecc512_seeds;
695*a430fa06SMiquel Raynal 		else
696*a430fa06SMiquel Raynal 			seeds = sunxi_nfc_randomizer_ecc1024_seeds;
697*a430fa06SMiquel Raynal 	}
698*a430fa06SMiquel Raynal 
699*a430fa06SMiquel Raynal 	return seeds[page % mod];
700*a430fa06SMiquel Raynal }
701*a430fa06SMiquel Raynal 
sunxi_nfc_randomizer_config(struct mtd_info * mtd,int page,bool ecc)702*a430fa06SMiquel Raynal static void sunxi_nfc_randomizer_config(struct mtd_info *mtd,
703*a430fa06SMiquel Raynal 					int page, bool ecc)
704*a430fa06SMiquel Raynal {
705*a430fa06SMiquel Raynal 	struct nand_chip *nand = mtd_to_nand(mtd);
706*a430fa06SMiquel Raynal 	struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
707*a430fa06SMiquel Raynal 	u32 ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL);
708*a430fa06SMiquel Raynal 	u16 state;
709*a430fa06SMiquel Raynal 
710*a430fa06SMiquel Raynal 	if (!(nand->options & NAND_NEED_SCRAMBLING))
711*a430fa06SMiquel Raynal 		return;
712*a430fa06SMiquel Raynal 
713*a430fa06SMiquel Raynal 	ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL);
714*a430fa06SMiquel Raynal 	state = sunxi_nfc_randomizer_state(mtd, page, ecc);
715*a430fa06SMiquel Raynal 	ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_RANDOM_SEED_MSK;
716*a430fa06SMiquel Raynal 	writel(ecc_ctl | NFC_RANDOM_SEED(state), nfc->regs + NFC_REG_ECC_CTL);
717*a430fa06SMiquel Raynal }
718*a430fa06SMiquel Raynal 
sunxi_nfc_randomizer_enable(struct mtd_info * mtd)719*a430fa06SMiquel Raynal static void sunxi_nfc_randomizer_enable(struct mtd_info *mtd)
720*a430fa06SMiquel Raynal {
721*a430fa06SMiquel Raynal 	struct nand_chip *nand = mtd_to_nand(mtd);
722*a430fa06SMiquel Raynal 	struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
723*a430fa06SMiquel Raynal 
724*a430fa06SMiquel Raynal 	if (!(nand->options & NAND_NEED_SCRAMBLING))
725*a430fa06SMiquel Raynal 		return;
726*a430fa06SMiquel Raynal 
727*a430fa06SMiquel Raynal 	writel(readl(nfc->regs + NFC_REG_ECC_CTL) | NFC_RANDOM_EN,
728*a430fa06SMiquel Raynal 	       nfc->regs + NFC_REG_ECC_CTL);
729*a430fa06SMiquel Raynal }
730*a430fa06SMiquel Raynal 
sunxi_nfc_randomizer_disable(struct mtd_info * mtd)731*a430fa06SMiquel Raynal static void sunxi_nfc_randomizer_disable(struct mtd_info *mtd)
732*a430fa06SMiquel Raynal {
733*a430fa06SMiquel Raynal 	struct nand_chip *nand = mtd_to_nand(mtd);
734*a430fa06SMiquel Raynal 	struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
735*a430fa06SMiquel Raynal 
736*a430fa06SMiquel Raynal 	if (!(nand->options & NAND_NEED_SCRAMBLING))
737*a430fa06SMiquel Raynal 		return;
738*a430fa06SMiquel Raynal 
739*a430fa06SMiquel Raynal 	writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_RANDOM_EN,
740*a430fa06SMiquel Raynal 	       nfc->regs + NFC_REG_ECC_CTL);
741*a430fa06SMiquel Raynal }
742*a430fa06SMiquel Raynal 
sunxi_nfc_randomize_bbm(struct mtd_info * mtd,int page,u8 * bbm)743*a430fa06SMiquel Raynal static void sunxi_nfc_randomize_bbm(struct mtd_info *mtd, int page, u8 *bbm)
744*a430fa06SMiquel Raynal {
745*a430fa06SMiquel Raynal 	u16 state = sunxi_nfc_randomizer_state(mtd, page, true);
746*a430fa06SMiquel Raynal 
747*a430fa06SMiquel Raynal 	bbm[0] ^= state;
748*a430fa06SMiquel Raynal 	bbm[1] ^= sunxi_nfc_randomizer_step(state, 8);
749*a430fa06SMiquel Raynal }
750*a430fa06SMiquel Raynal 
sunxi_nfc_randomizer_write_buf(struct mtd_info * mtd,const uint8_t * buf,int len,bool ecc,int page)751*a430fa06SMiquel Raynal static void sunxi_nfc_randomizer_write_buf(struct mtd_info *mtd,
752*a430fa06SMiquel Raynal 					   const uint8_t *buf, int len,
753*a430fa06SMiquel Raynal 					   bool ecc, int page)
754*a430fa06SMiquel Raynal {
755*a430fa06SMiquel Raynal 	sunxi_nfc_randomizer_config(mtd, page, ecc);
756*a430fa06SMiquel Raynal 	sunxi_nfc_randomizer_enable(mtd);
757*a430fa06SMiquel Raynal 	sunxi_nfc_write_buf(mtd, buf, len);
758*a430fa06SMiquel Raynal 	sunxi_nfc_randomizer_disable(mtd);
759*a430fa06SMiquel Raynal }
760*a430fa06SMiquel Raynal 
sunxi_nfc_randomizer_read_buf(struct mtd_info * mtd,uint8_t * buf,int len,bool ecc,int page)761*a430fa06SMiquel Raynal static void sunxi_nfc_randomizer_read_buf(struct mtd_info *mtd, uint8_t *buf,
762*a430fa06SMiquel Raynal 					  int len, bool ecc, int page)
763*a430fa06SMiquel Raynal {
764*a430fa06SMiquel Raynal 	sunxi_nfc_randomizer_config(mtd, page, ecc);
765*a430fa06SMiquel Raynal 	sunxi_nfc_randomizer_enable(mtd);
766*a430fa06SMiquel Raynal 	sunxi_nfc_read_buf(mtd, buf, len);
767*a430fa06SMiquel Raynal 	sunxi_nfc_randomizer_disable(mtd);
768*a430fa06SMiquel Raynal }
769*a430fa06SMiquel Raynal 
sunxi_nfc_hw_ecc_enable(struct mtd_info * mtd)770*a430fa06SMiquel Raynal static void sunxi_nfc_hw_ecc_enable(struct mtd_info *mtd)
771*a430fa06SMiquel Raynal {
772*a430fa06SMiquel Raynal 	struct nand_chip *nand = mtd_to_nand(mtd);
773*a430fa06SMiquel Raynal 	struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
774*a430fa06SMiquel Raynal 	struct sunxi_nand_hw_ecc *data = nand->ecc.priv;
775*a430fa06SMiquel Raynal 	u32 ecc_ctl;
776*a430fa06SMiquel Raynal 
777*a430fa06SMiquel Raynal 	ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL);
778*a430fa06SMiquel Raynal 	ecc_ctl &= ~(NFC_ECC_MODE_MSK | NFC_ECC_PIPELINE |
779*a430fa06SMiquel Raynal 		     NFC_ECC_BLOCK_SIZE_MSK);
780*a430fa06SMiquel Raynal 	ecc_ctl |= NFC_ECC_EN | NFC_ECC_MODE(data->mode) | NFC_ECC_EXCEPTION;
781*a430fa06SMiquel Raynal 
782*a430fa06SMiquel Raynal 	if (nand->ecc.size == 512)
783*a430fa06SMiquel Raynal 		ecc_ctl |= NFC_ECC_BLOCK_512;
784*a430fa06SMiquel Raynal 
785*a430fa06SMiquel Raynal 	writel(ecc_ctl, nfc->regs + NFC_REG_ECC_CTL);
786*a430fa06SMiquel Raynal }
787*a430fa06SMiquel Raynal 
sunxi_nfc_hw_ecc_disable(struct mtd_info * mtd)788*a430fa06SMiquel Raynal static void sunxi_nfc_hw_ecc_disable(struct mtd_info *mtd)
789*a430fa06SMiquel Raynal {
790*a430fa06SMiquel Raynal 	struct nand_chip *nand = mtd_to_nand(mtd);
791*a430fa06SMiquel Raynal 	struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
792*a430fa06SMiquel Raynal 
793*a430fa06SMiquel Raynal 	writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_ECC_EN,
794*a430fa06SMiquel Raynal 	       nfc->regs + NFC_REG_ECC_CTL);
795*a430fa06SMiquel Raynal }
796*a430fa06SMiquel Raynal 
sunxi_nfc_user_data_to_buf(u32 user_data,u8 * buf)797*a430fa06SMiquel Raynal static inline void sunxi_nfc_user_data_to_buf(u32 user_data, u8 *buf)
798*a430fa06SMiquel Raynal {
799*a430fa06SMiquel Raynal 	buf[0] = user_data;
800*a430fa06SMiquel Raynal 	buf[1] = user_data >> 8;
801*a430fa06SMiquel Raynal 	buf[2] = user_data >> 16;
802*a430fa06SMiquel Raynal 	buf[3] = user_data >> 24;
803*a430fa06SMiquel Raynal }
804*a430fa06SMiquel Raynal 
sunxi_nfc_hw_ecc_read_chunk(struct mtd_info * mtd,u8 * data,int data_off,u8 * oob,int oob_off,int * cur_off,unsigned int * max_bitflips,bool bbm,int page)805*a430fa06SMiquel Raynal static int sunxi_nfc_hw_ecc_read_chunk(struct mtd_info *mtd,
806*a430fa06SMiquel Raynal 				       u8 *data, int data_off,
807*a430fa06SMiquel Raynal 				       u8 *oob, int oob_off,
808*a430fa06SMiquel Raynal 				       int *cur_off,
809*a430fa06SMiquel Raynal 				       unsigned int *max_bitflips,
810*a430fa06SMiquel Raynal 				       bool bbm, int page)
811*a430fa06SMiquel Raynal {
812*a430fa06SMiquel Raynal 	struct nand_chip *nand = mtd_to_nand(mtd);
813*a430fa06SMiquel Raynal 	struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
814*a430fa06SMiquel Raynal 	struct nand_ecc_ctrl *ecc = &nand->ecc;
815*a430fa06SMiquel Raynal 	int raw_mode = 0;
816*a430fa06SMiquel Raynal 	u32 status;
817*a430fa06SMiquel Raynal 	int ret;
818*a430fa06SMiquel Raynal 
819*a430fa06SMiquel Raynal 	if (*cur_off != data_off)
820*a430fa06SMiquel Raynal 		nand->cmdfunc(mtd, NAND_CMD_RNDOUT, data_off, -1);
821*a430fa06SMiquel Raynal 
822*a430fa06SMiquel Raynal 	sunxi_nfc_randomizer_read_buf(mtd, NULL, ecc->size, false, page);
823*a430fa06SMiquel Raynal 
824*a430fa06SMiquel Raynal 	if (data_off + ecc->size != oob_off)
825*a430fa06SMiquel Raynal 		nand->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_off, -1);
826*a430fa06SMiquel Raynal 
827*a430fa06SMiquel Raynal 	ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
828*a430fa06SMiquel Raynal 	if (ret)
829*a430fa06SMiquel Raynal 		return ret;
830*a430fa06SMiquel Raynal 
831*a430fa06SMiquel Raynal 	sunxi_nfc_randomizer_enable(mtd);
832*a430fa06SMiquel Raynal 	writel(NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | NFC_ECC_OP,
833*a430fa06SMiquel Raynal 	       nfc->regs + NFC_REG_CMD);
834*a430fa06SMiquel Raynal 
835*a430fa06SMiquel Raynal 	ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
836*a430fa06SMiquel Raynal 	sunxi_nfc_randomizer_disable(mtd);
837*a430fa06SMiquel Raynal 	if (ret)
838*a430fa06SMiquel Raynal 		return ret;
839*a430fa06SMiquel Raynal 
840*a430fa06SMiquel Raynal 	*cur_off = oob_off + ecc->bytes + 4;
841*a430fa06SMiquel Raynal 
842*a430fa06SMiquel Raynal 	status = readl(nfc->regs + NFC_REG_ECC_ST);
843*a430fa06SMiquel Raynal 	if (status & NFC_ECC_PAT_FOUND(0)) {
844*a430fa06SMiquel Raynal 		u8 pattern = 0xff;
845*a430fa06SMiquel Raynal 
846*a430fa06SMiquel Raynal 		if (unlikely(!(readl(nfc->regs + NFC_REG_PAT_ID) & 0x1)))
847*a430fa06SMiquel Raynal 			pattern = 0x0;
848*a430fa06SMiquel Raynal 
849*a430fa06SMiquel Raynal 		memset(data, pattern, ecc->size);
850*a430fa06SMiquel Raynal 		memset(oob, pattern, ecc->bytes + 4);
851*a430fa06SMiquel Raynal 
852*a430fa06SMiquel Raynal 		return 1;
853*a430fa06SMiquel Raynal 	}
854*a430fa06SMiquel Raynal 
855*a430fa06SMiquel Raynal 	ret = NFC_ECC_ERR_CNT(0, readl(nfc->regs + NFC_REG_ECC_ERR_CNT(0)));
856*a430fa06SMiquel Raynal 
857*a430fa06SMiquel Raynal 	memcpy_fromio(data, nfc->regs + NFC_RAM0_BASE, ecc->size);
858*a430fa06SMiquel Raynal 
859*a430fa06SMiquel Raynal 	nand->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_off, -1);
860*a430fa06SMiquel Raynal 	sunxi_nfc_randomizer_read_buf(mtd, oob, ecc->bytes + 4, true, page);
861*a430fa06SMiquel Raynal 
862*a430fa06SMiquel Raynal 	if (status & NFC_ECC_ERR(0)) {
863*a430fa06SMiquel Raynal 		/*
864*a430fa06SMiquel Raynal 		 * Re-read the data with the randomizer disabled to identify
865*a430fa06SMiquel Raynal 		 * bitflips in erased pages.
866*a430fa06SMiquel Raynal 		 */
867*a430fa06SMiquel Raynal 		if (nand->options & NAND_NEED_SCRAMBLING) {
868*a430fa06SMiquel Raynal 			nand->cmdfunc(mtd, NAND_CMD_RNDOUT, data_off, -1);
869*a430fa06SMiquel Raynal 			nand->read_buf(mtd, data, ecc->size);
870*a430fa06SMiquel Raynal 			nand->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_off, -1);
871*a430fa06SMiquel Raynal 			nand->read_buf(mtd, oob, ecc->bytes + 4);
872*a430fa06SMiquel Raynal 		}
873*a430fa06SMiquel Raynal 
874*a430fa06SMiquel Raynal 		ret = nand_check_erased_ecc_chunk(data,	ecc->size,
875*a430fa06SMiquel Raynal 						  oob, ecc->bytes + 4,
876*a430fa06SMiquel Raynal 						  NULL, 0, ecc->strength);
877*a430fa06SMiquel Raynal 		if (ret >= 0)
878*a430fa06SMiquel Raynal 			raw_mode = 1;
879*a430fa06SMiquel Raynal 	} else {
880*a430fa06SMiquel Raynal 		/*
881*a430fa06SMiquel Raynal 		 * The engine protects 4 bytes of OOB data per chunk.
882*a430fa06SMiquel Raynal 		 * Retrieve the corrected OOB bytes.
883*a430fa06SMiquel Raynal 		 */
884*a430fa06SMiquel Raynal 		sunxi_nfc_user_data_to_buf(readl(nfc->regs +
885*a430fa06SMiquel Raynal 						 NFC_REG_USER_DATA(0)),
886*a430fa06SMiquel Raynal 					   oob);
887*a430fa06SMiquel Raynal 
888*a430fa06SMiquel Raynal 		/* De-randomize the Bad Block Marker. */
889*a430fa06SMiquel Raynal 		if (bbm && nand->options & NAND_NEED_SCRAMBLING)
890*a430fa06SMiquel Raynal 			sunxi_nfc_randomize_bbm(mtd, page, oob);
891*a430fa06SMiquel Raynal 	}
892*a430fa06SMiquel Raynal 
893*a430fa06SMiquel Raynal 	if (ret < 0) {
894*a430fa06SMiquel Raynal 		mtd->ecc_stats.failed++;
895*a430fa06SMiquel Raynal 	} else {
896*a430fa06SMiquel Raynal 		mtd->ecc_stats.corrected += ret;
897*a430fa06SMiquel Raynal 		*max_bitflips = max_t(unsigned int, *max_bitflips, ret);
898*a430fa06SMiquel Raynal 	}
899*a430fa06SMiquel Raynal 
900*a430fa06SMiquel Raynal 	return raw_mode;
901*a430fa06SMiquel Raynal }
902*a430fa06SMiquel Raynal 
sunxi_nfc_hw_ecc_read_extra_oob(struct mtd_info * mtd,u8 * oob,int * cur_off,bool randomize,int page)903*a430fa06SMiquel Raynal static void sunxi_nfc_hw_ecc_read_extra_oob(struct mtd_info *mtd,
904*a430fa06SMiquel Raynal 					    u8 *oob, int *cur_off,
905*a430fa06SMiquel Raynal 					    bool randomize, int page)
906*a430fa06SMiquel Raynal {
907*a430fa06SMiquel Raynal 	struct nand_chip *nand = mtd_to_nand(mtd);
908*a430fa06SMiquel Raynal 	struct nand_ecc_ctrl *ecc = &nand->ecc;
909*a430fa06SMiquel Raynal 	int offset = ((ecc->bytes + 4) * ecc->steps);
910*a430fa06SMiquel Raynal 	int len = mtd->oobsize - offset;
911*a430fa06SMiquel Raynal 
912*a430fa06SMiquel Raynal 	if (len <= 0)
913*a430fa06SMiquel Raynal 		return;
914*a430fa06SMiquel Raynal 
915*a430fa06SMiquel Raynal 	if (*cur_off != offset)
916*a430fa06SMiquel Raynal 		nand->cmdfunc(mtd, NAND_CMD_RNDOUT,
917*a430fa06SMiquel Raynal 			      offset + mtd->writesize, -1);
918*a430fa06SMiquel Raynal 
919*a430fa06SMiquel Raynal 	if (!randomize)
920*a430fa06SMiquel Raynal 		sunxi_nfc_read_buf(mtd, oob + offset, len);
921*a430fa06SMiquel Raynal 	else
922*a430fa06SMiquel Raynal 		sunxi_nfc_randomizer_read_buf(mtd, oob + offset, len,
923*a430fa06SMiquel Raynal 					      false, page);
924*a430fa06SMiquel Raynal 
925*a430fa06SMiquel Raynal 	*cur_off = mtd->oobsize + mtd->writesize;
926*a430fa06SMiquel Raynal }
927*a430fa06SMiquel Raynal 
sunxi_nfc_buf_to_user_data(const u8 * buf)928*a430fa06SMiquel Raynal static inline u32 sunxi_nfc_buf_to_user_data(const u8 *buf)
929*a430fa06SMiquel Raynal {
930*a430fa06SMiquel Raynal 	return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
931*a430fa06SMiquel Raynal }
932*a430fa06SMiquel Raynal 
sunxi_nfc_hw_ecc_write_chunk(struct mtd_info * mtd,const u8 * data,int data_off,const u8 * oob,int oob_off,int * cur_off,bool bbm,int page)933*a430fa06SMiquel Raynal static int sunxi_nfc_hw_ecc_write_chunk(struct mtd_info *mtd,
934*a430fa06SMiquel Raynal 					const u8 *data, int data_off,
935*a430fa06SMiquel Raynal 					const u8 *oob, int oob_off,
936*a430fa06SMiquel Raynal 					int *cur_off, bool bbm,
937*a430fa06SMiquel Raynal 					int page)
938*a430fa06SMiquel Raynal {
939*a430fa06SMiquel Raynal 	struct nand_chip *nand = mtd_to_nand(mtd);
940*a430fa06SMiquel Raynal 	struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
941*a430fa06SMiquel Raynal 	struct nand_ecc_ctrl *ecc = &nand->ecc;
942*a430fa06SMiquel Raynal 	int ret;
943*a430fa06SMiquel Raynal 
944*a430fa06SMiquel Raynal 	if (data_off != *cur_off)
945*a430fa06SMiquel Raynal 		nand->cmdfunc(mtd, NAND_CMD_RNDIN, data_off, -1);
946*a430fa06SMiquel Raynal 
947*a430fa06SMiquel Raynal 	sunxi_nfc_randomizer_write_buf(mtd, data, ecc->size, false, page);
948*a430fa06SMiquel Raynal 
949*a430fa06SMiquel Raynal 	/* Fill OOB data in */
950*a430fa06SMiquel Raynal 	if ((nand->options & NAND_NEED_SCRAMBLING) && bbm) {
951*a430fa06SMiquel Raynal 		u8 user_data[4];
952*a430fa06SMiquel Raynal 
953*a430fa06SMiquel Raynal 		memcpy(user_data, oob, 4);
954*a430fa06SMiquel Raynal 		sunxi_nfc_randomize_bbm(mtd, page, user_data);
955*a430fa06SMiquel Raynal 		writel(sunxi_nfc_buf_to_user_data(user_data),
956*a430fa06SMiquel Raynal 		       nfc->regs + NFC_REG_USER_DATA(0));
957*a430fa06SMiquel Raynal 	} else {
958*a430fa06SMiquel Raynal 		writel(sunxi_nfc_buf_to_user_data(oob),
959*a430fa06SMiquel Raynal 		       nfc->regs + NFC_REG_USER_DATA(0));
960*a430fa06SMiquel Raynal 	}
961*a430fa06SMiquel Raynal 
962*a430fa06SMiquel Raynal 	if (data_off + ecc->size != oob_off)
963*a430fa06SMiquel Raynal 		nand->cmdfunc(mtd, NAND_CMD_RNDIN, oob_off, -1);
964*a430fa06SMiquel Raynal 
965*a430fa06SMiquel Raynal 	ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
966*a430fa06SMiquel Raynal 	if (ret)
967*a430fa06SMiquel Raynal 		return ret;
968*a430fa06SMiquel Raynal 
969*a430fa06SMiquel Raynal 	sunxi_nfc_randomizer_enable(mtd);
970*a430fa06SMiquel Raynal 	writel(NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD |
971*a430fa06SMiquel Raynal 	       NFC_ACCESS_DIR | NFC_ECC_OP,
972*a430fa06SMiquel Raynal 	       nfc->regs + NFC_REG_CMD);
973*a430fa06SMiquel Raynal 
974*a430fa06SMiquel Raynal 	ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
975*a430fa06SMiquel Raynal 	sunxi_nfc_randomizer_disable(mtd);
976*a430fa06SMiquel Raynal 	if (ret)
977*a430fa06SMiquel Raynal 		return ret;
978*a430fa06SMiquel Raynal 
979*a430fa06SMiquel Raynal 	*cur_off = oob_off + ecc->bytes + 4;
980*a430fa06SMiquel Raynal 
981*a430fa06SMiquel Raynal 	return 0;
982*a430fa06SMiquel Raynal }
983*a430fa06SMiquel Raynal 
sunxi_nfc_hw_ecc_write_extra_oob(struct mtd_info * mtd,u8 * oob,int * cur_off,int page)984*a430fa06SMiquel Raynal static void sunxi_nfc_hw_ecc_write_extra_oob(struct mtd_info *mtd,
985*a430fa06SMiquel Raynal 					     u8 *oob, int *cur_off,
986*a430fa06SMiquel Raynal 					     int page)
987*a430fa06SMiquel Raynal {
988*a430fa06SMiquel Raynal 	struct nand_chip *nand = mtd_to_nand(mtd);
989*a430fa06SMiquel Raynal 	struct nand_ecc_ctrl *ecc = &nand->ecc;
990*a430fa06SMiquel Raynal 	int offset = ((ecc->bytes + 4) * ecc->steps);
991*a430fa06SMiquel Raynal 	int len = mtd->oobsize - offset;
992*a430fa06SMiquel Raynal 
993*a430fa06SMiquel Raynal 	if (len <= 0)
994*a430fa06SMiquel Raynal 		return;
995*a430fa06SMiquel Raynal 
996*a430fa06SMiquel Raynal 	if (*cur_off != offset)
997*a430fa06SMiquel Raynal 		nand->cmdfunc(mtd, NAND_CMD_RNDIN,
998*a430fa06SMiquel Raynal 			      offset + mtd->writesize, -1);
999*a430fa06SMiquel Raynal 
1000*a430fa06SMiquel Raynal 	sunxi_nfc_randomizer_write_buf(mtd, oob + offset, len, false, page);
1001*a430fa06SMiquel Raynal 
1002*a430fa06SMiquel Raynal 	*cur_off = mtd->oobsize + mtd->writesize;
1003*a430fa06SMiquel Raynal }
1004*a430fa06SMiquel Raynal 
sunxi_nfc_hw_ecc_read_page(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)1005*a430fa06SMiquel Raynal static int sunxi_nfc_hw_ecc_read_page(struct mtd_info *mtd,
1006*a430fa06SMiquel Raynal 				      struct nand_chip *chip, uint8_t *buf,
1007*a430fa06SMiquel Raynal 				      int oob_required, int page)
1008*a430fa06SMiquel Raynal {
1009*a430fa06SMiquel Raynal 	struct nand_ecc_ctrl *ecc = &chip->ecc;
1010*a430fa06SMiquel Raynal 	unsigned int max_bitflips = 0;
1011*a430fa06SMiquel Raynal 	int ret, i, cur_off = 0;
1012*a430fa06SMiquel Raynal 	bool raw_mode = false;
1013*a430fa06SMiquel Raynal 
1014*a430fa06SMiquel Raynal 	sunxi_nfc_hw_ecc_enable(mtd);
1015*a430fa06SMiquel Raynal 
1016*a430fa06SMiquel Raynal 	for (i = 0; i < ecc->steps; i++) {
1017*a430fa06SMiquel Raynal 		int data_off = i * ecc->size;
1018*a430fa06SMiquel Raynal 		int oob_off = i * (ecc->bytes + 4);
1019*a430fa06SMiquel Raynal 		u8 *data = buf + data_off;
1020*a430fa06SMiquel Raynal 		u8 *oob = chip->oob_poi + oob_off;
1021*a430fa06SMiquel Raynal 
1022*a430fa06SMiquel Raynal 		ret = sunxi_nfc_hw_ecc_read_chunk(mtd, data, data_off, oob,
1023*a430fa06SMiquel Raynal 						  oob_off + mtd->writesize,
1024*a430fa06SMiquel Raynal 						  &cur_off, &max_bitflips,
1025*a430fa06SMiquel Raynal 						  !i, page);
1026*a430fa06SMiquel Raynal 		if (ret < 0)
1027*a430fa06SMiquel Raynal 			return ret;
1028*a430fa06SMiquel Raynal 		else if (ret)
1029*a430fa06SMiquel Raynal 			raw_mode = true;
1030*a430fa06SMiquel Raynal 	}
1031*a430fa06SMiquel Raynal 
1032*a430fa06SMiquel Raynal 	if (oob_required)
1033*a430fa06SMiquel Raynal 		sunxi_nfc_hw_ecc_read_extra_oob(mtd, chip->oob_poi, &cur_off,
1034*a430fa06SMiquel Raynal 						!raw_mode, page);
1035*a430fa06SMiquel Raynal 
1036*a430fa06SMiquel Raynal 	sunxi_nfc_hw_ecc_disable(mtd);
1037*a430fa06SMiquel Raynal 
1038*a430fa06SMiquel Raynal 	return max_bitflips;
1039*a430fa06SMiquel Raynal }
1040*a430fa06SMiquel Raynal 
sunxi_nfc_hw_ecc_read_subpage(struct mtd_info * mtd,struct nand_chip * chip,uint32_t data_offs,uint32_t readlen,uint8_t * bufpoi,int page)1041*a430fa06SMiquel Raynal static int sunxi_nfc_hw_ecc_read_subpage(struct mtd_info *mtd,
1042*a430fa06SMiquel Raynal 					 struct nand_chip *chip,
1043*a430fa06SMiquel Raynal 					 uint32_t data_offs, uint32_t readlen,
1044*a430fa06SMiquel Raynal 					 uint8_t *bufpoi, int page)
1045*a430fa06SMiquel Raynal {
1046*a430fa06SMiquel Raynal 	struct nand_ecc_ctrl *ecc = &chip->ecc;
1047*a430fa06SMiquel Raynal 	int ret, i, cur_off = 0;
1048*a430fa06SMiquel Raynal 	unsigned int max_bitflips = 0;
1049*a430fa06SMiquel Raynal 
1050*a430fa06SMiquel Raynal 	sunxi_nfc_hw_ecc_enable(mtd);
1051*a430fa06SMiquel Raynal 
1052*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1053*a430fa06SMiquel Raynal 	for (i = data_offs / ecc->size;
1054*a430fa06SMiquel Raynal 	     i < DIV_ROUND_UP(data_offs + readlen, ecc->size); i++) {
1055*a430fa06SMiquel Raynal 		int data_off = i * ecc->size;
1056*a430fa06SMiquel Raynal 		int oob_off = i * (ecc->bytes + 4);
1057*a430fa06SMiquel Raynal 		u8 *data = bufpoi + data_off;
1058*a430fa06SMiquel Raynal 		u8 *oob = chip->oob_poi + oob_off;
1059*a430fa06SMiquel Raynal 
1060*a430fa06SMiquel Raynal 		ret = sunxi_nfc_hw_ecc_read_chunk(mtd, data, data_off,
1061*a430fa06SMiquel Raynal 			oob, oob_off + mtd->writesize,
1062*a430fa06SMiquel Raynal 			&cur_off, &max_bitflips, !i, page);
1063*a430fa06SMiquel Raynal 		if (ret < 0)
1064*a430fa06SMiquel Raynal 			return ret;
1065*a430fa06SMiquel Raynal 	}
1066*a430fa06SMiquel Raynal 
1067*a430fa06SMiquel Raynal 	sunxi_nfc_hw_ecc_disable(mtd);
1068*a430fa06SMiquel Raynal 
1069*a430fa06SMiquel Raynal 	return max_bitflips;
1070*a430fa06SMiquel Raynal }
1071*a430fa06SMiquel Raynal 
sunxi_nfc_hw_ecc_write_page(struct mtd_info * mtd,struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)1072*a430fa06SMiquel Raynal static int sunxi_nfc_hw_ecc_write_page(struct mtd_info *mtd,
1073*a430fa06SMiquel Raynal 				       struct nand_chip *chip,
1074*a430fa06SMiquel Raynal 				       const uint8_t *buf, int oob_required,
1075*a430fa06SMiquel Raynal 				       int page)
1076*a430fa06SMiquel Raynal {
1077*a430fa06SMiquel Raynal 	struct nand_ecc_ctrl *ecc = &chip->ecc;
1078*a430fa06SMiquel Raynal 	int ret, i, cur_off = 0;
1079*a430fa06SMiquel Raynal 
1080*a430fa06SMiquel Raynal 	sunxi_nfc_hw_ecc_enable(mtd);
1081*a430fa06SMiquel Raynal 
1082*a430fa06SMiquel Raynal 	for (i = 0; i < ecc->steps; i++) {
1083*a430fa06SMiquel Raynal 		int data_off = i * ecc->size;
1084*a430fa06SMiquel Raynal 		int oob_off = i * (ecc->bytes + 4);
1085*a430fa06SMiquel Raynal 		const u8 *data = buf + data_off;
1086*a430fa06SMiquel Raynal 		const u8 *oob = chip->oob_poi + oob_off;
1087*a430fa06SMiquel Raynal 
1088*a430fa06SMiquel Raynal 		ret = sunxi_nfc_hw_ecc_write_chunk(mtd, data, data_off, oob,
1089*a430fa06SMiquel Raynal 						   oob_off + mtd->writesize,
1090*a430fa06SMiquel Raynal 						   &cur_off, !i, page);
1091*a430fa06SMiquel Raynal 		if (ret)
1092*a430fa06SMiquel Raynal 			return ret;
1093*a430fa06SMiquel Raynal 	}
1094*a430fa06SMiquel Raynal 
1095*a430fa06SMiquel Raynal 	if (oob_required || (chip->options & NAND_NEED_SCRAMBLING))
1096*a430fa06SMiquel Raynal 		sunxi_nfc_hw_ecc_write_extra_oob(mtd, chip->oob_poi,
1097*a430fa06SMiquel Raynal 						 &cur_off, page);
1098*a430fa06SMiquel Raynal 
1099*a430fa06SMiquel Raynal 	sunxi_nfc_hw_ecc_disable(mtd);
1100*a430fa06SMiquel Raynal 
1101*a430fa06SMiquel Raynal 	return 0;
1102*a430fa06SMiquel Raynal }
1103*a430fa06SMiquel Raynal 
sunxi_nfc_hw_ecc_write_subpage(struct mtd_info * mtd,struct nand_chip * chip,u32 data_offs,u32 data_len,const u8 * buf,int oob_required,int page)1104*a430fa06SMiquel Raynal static int sunxi_nfc_hw_ecc_write_subpage(struct mtd_info *mtd,
1105*a430fa06SMiquel Raynal 					  struct nand_chip *chip,
1106*a430fa06SMiquel Raynal 					  u32 data_offs, u32 data_len,
1107*a430fa06SMiquel Raynal 					  const u8 *buf, int oob_required,
1108*a430fa06SMiquel Raynal 					  int page)
1109*a430fa06SMiquel Raynal {
1110*a430fa06SMiquel Raynal 	struct nand_ecc_ctrl *ecc = &chip->ecc;
1111*a430fa06SMiquel Raynal 	int ret, i, cur_off = 0;
1112*a430fa06SMiquel Raynal 
1113*a430fa06SMiquel Raynal 	sunxi_nfc_hw_ecc_enable(mtd);
1114*a430fa06SMiquel Raynal 
1115*a430fa06SMiquel Raynal 	for (i = data_offs / ecc->size;
1116*a430fa06SMiquel Raynal 	     i < DIV_ROUND_UP(data_offs + data_len, ecc->size); i++) {
1117*a430fa06SMiquel Raynal 		int data_off = i * ecc->size;
1118*a430fa06SMiquel Raynal 		int oob_off = i * (ecc->bytes + 4);
1119*a430fa06SMiquel Raynal 		const u8 *data = buf + data_off;
1120*a430fa06SMiquel Raynal 		const u8 *oob = chip->oob_poi + oob_off;
1121*a430fa06SMiquel Raynal 
1122*a430fa06SMiquel Raynal 		ret = sunxi_nfc_hw_ecc_write_chunk(mtd, data, data_off, oob,
1123*a430fa06SMiquel Raynal 						   oob_off + mtd->writesize,
1124*a430fa06SMiquel Raynal 						   &cur_off, !i, page);
1125*a430fa06SMiquel Raynal 		if (ret)
1126*a430fa06SMiquel Raynal 			return ret;
1127*a430fa06SMiquel Raynal 	}
1128*a430fa06SMiquel Raynal 
1129*a430fa06SMiquel Raynal 	sunxi_nfc_hw_ecc_disable(mtd);
1130*a430fa06SMiquel Raynal 
1131*a430fa06SMiquel Raynal 	return 0;
1132*a430fa06SMiquel Raynal }
1133*a430fa06SMiquel Raynal 
sunxi_nfc_hw_syndrome_ecc_read_page(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)1134*a430fa06SMiquel Raynal static int sunxi_nfc_hw_syndrome_ecc_read_page(struct mtd_info *mtd,
1135*a430fa06SMiquel Raynal 					       struct nand_chip *chip,
1136*a430fa06SMiquel Raynal 					       uint8_t *buf, int oob_required,
1137*a430fa06SMiquel Raynal 					       int page)
1138*a430fa06SMiquel Raynal {
1139*a430fa06SMiquel Raynal 	struct nand_ecc_ctrl *ecc = &chip->ecc;
1140*a430fa06SMiquel Raynal 	unsigned int max_bitflips = 0;
1141*a430fa06SMiquel Raynal 	int ret, i, cur_off = 0;
1142*a430fa06SMiquel Raynal 	bool raw_mode = false;
1143*a430fa06SMiquel Raynal 
1144*a430fa06SMiquel Raynal 	sunxi_nfc_hw_ecc_enable(mtd);
1145*a430fa06SMiquel Raynal 
1146*a430fa06SMiquel Raynal 	for (i = 0; i < ecc->steps; i++) {
1147*a430fa06SMiquel Raynal 		int data_off = i * (ecc->size + ecc->bytes + 4);
1148*a430fa06SMiquel Raynal 		int oob_off = data_off + ecc->size;
1149*a430fa06SMiquel Raynal 		u8 *data = buf + (i * ecc->size);
1150*a430fa06SMiquel Raynal 		u8 *oob = chip->oob_poi + (i * (ecc->bytes + 4));
1151*a430fa06SMiquel Raynal 
1152*a430fa06SMiquel Raynal 		ret = sunxi_nfc_hw_ecc_read_chunk(mtd, data, data_off, oob,
1153*a430fa06SMiquel Raynal 						  oob_off, &cur_off,
1154*a430fa06SMiquel Raynal 						  &max_bitflips, !i, page);
1155*a430fa06SMiquel Raynal 		if (ret < 0)
1156*a430fa06SMiquel Raynal 			return ret;
1157*a430fa06SMiquel Raynal 		else if (ret)
1158*a430fa06SMiquel Raynal 			raw_mode = true;
1159*a430fa06SMiquel Raynal 	}
1160*a430fa06SMiquel Raynal 
1161*a430fa06SMiquel Raynal 	if (oob_required)
1162*a430fa06SMiquel Raynal 		sunxi_nfc_hw_ecc_read_extra_oob(mtd, chip->oob_poi, &cur_off,
1163*a430fa06SMiquel Raynal 						!raw_mode, page);
1164*a430fa06SMiquel Raynal 
1165*a430fa06SMiquel Raynal 	sunxi_nfc_hw_ecc_disable(mtd);
1166*a430fa06SMiquel Raynal 
1167*a430fa06SMiquel Raynal 	return max_bitflips;
1168*a430fa06SMiquel Raynal }
1169*a430fa06SMiquel Raynal 
sunxi_nfc_hw_syndrome_ecc_write_page(struct mtd_info * mtd,struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)1170*a430fa06SMiquel Raynal static int sunxi_nfc_hw_syndrome_ecc_write_page(struct mtd_info *mtd,
1171*a430fa06SMiquel Raynal 						struct nand_chip *chip,
1172*a430fa06SMiquel Raynal 						const uint8_t *buf,
1173*a430fa06SMiquel Raynal 						int oob_required, int page)
1174*a430fa06SMiquel Raynal {
1175*a430fa06SMiquel Raynal 	struct nand_ecc_ctrl *ecc = &chip->ecc;
1176*a430fa06SMiquel Raynal 	int ret, i, cur_off = 0;
1177*a430fa06SMiquel Raynal 
1178*a430fa06SMiquel Raynal 	sunxi_nfc_hw_ecc_enable(mtd);
1179*a430fa06SMiquel Raynal 
1180*a430fa06SMiquel Raynal 	for (i = 0; i < ecc->steps; i++) {
1181*a430fa06SMiquel Raynal 		int data_off = i * (ecc->size + ecc->bytes + 4);
1182*a430fa06SMiquel Raynal 		int oob_off = data_off + ecc->size;
1183*a430fa06SMiquel Raynal 		const u8 *data = buf + (i * ecc->size);
1184*a430fa06SMiquel Raynal 		const u8 *oob = chip->oob_poi + (i * (ecc->bytes + 4));
1185*a430fa06SMiquel Raynal 
1186*a430fa06SMiquel Raynal 		ret = sunxi_nfc_hw_ecc_write_chunk(mtd, data, data_off,
1187*a430fa06SMiquel Raynal 						   oob, oob_off, &cur_off,
1188*a430fa06SMiquel Raynal 						   false, page);
1189*a430fa06SMiquel Raynal 		if (ret)
1190*a430fa06SMiquel Raynal 			return ret;
1191*a430fa06SMiquel Raynal 	}
1192*a430fa06SMiquel Raynal 
1193*a430fa06SMiquel Raynal 	if (oob_required || (chip->options & NAND_NEED_SCRAMBLING))
1194*a430fa06SMiquel Raynal 		sunxi_nfc_hw_ecc_write_extra_oob(mtd, chip->oob_poi,
1195*a430fa06SMiquel Raynal 						 &cur_off, page);
1196*a430fa06SMiquel Raynal 
1197*a430fa06SMiquel Raynal 	sunxi_nfc_hw_ecc_disable(mtd);
1198*a430fa06SMiquel Raynal 
1199*a430fa06SMiquel Raynal 	return 0;
1200*a430fa06SMiquel Raynal }
1201*a430fa06SMiquel Raynal 
1202*a430fa06SMiquel Raynal static const s32 tWB_lut[] = {6, 12, 16, 20};
1203*a430fa06SMiquel Raynal static const s32 tRHW_lut[] = {4, 8, 12, 20};
1204*a430fa06SMiquel Raynal 
_sunxi_nand_lookup_timing(const s32 * lut,int lut_size,u32 duration,u32 clk_period)1205*a430fa06SMiquel Raynal static int _sunxi_nand_lookup_timing(const s32 *lut, int lut_size, u32 duration,
1206*a430fa06SMiquel Raynal 		u32 clk_period)
1207*a430fa06SMiquel Raynal {
1208*a430fa06SMiquel Raynal 	u32 clk_cycles = DIV_ROUND_UP(duration, clk_period);
1209*a430fa06SMiquel Raynal 	int i;
1210*a430fa06SMiquel Raynal 
1211*a430fa06SMiquel Raynal 	for (i = 0; i < lut_size; i++) {
1212*a430fa06SMiquel Raynal 		if (clk_cycles <= lut[i])
1213*a430fa06SMiquel Raynal 			return i;
1214*a430fa06SMiquel Raynal 	}
1215*a430fa06SMiquel Raynal 
1216*a430fa06SMiquel Raynal 	/* Doesn't fit */
1217*a430fa06SMiquel Raynal 	return -EINVAL;
1218*a430fa06SMiquel Raynal }
1219*a430fa06SMiquel Raynal 
1220*a430fa06SMiquel Raynal #define sunxi_nand_lookup_timing(l, p, c) \
1221*a430fa06SMiquel Raynal 			_sunxi_nand_lookup_timing(l, ARRAY_SIZE(l), p, c)
1222*a430fa06SMiquel Raynal 
sunxi_nand_chip_set_timings(struct sunxi_nand_chip * chip,const struct nand_sdr_timings * timings)1223*a430fa06SMiquel Raynal static int sunxi_nand_chip_set_timings(struct sunxi_nand_chip *chip,
1224*a430fa06SMiquel Raynal 				       const struct nand_sdr_timings *timings)
1225*a430fa06SMiquel Raynal {
1226*a430fa06SMiquel Raynal 	u32 min_clk_period = 0;
1227*a430fa06SMiquel Raynal 	s32 tWB, tADL, tWHR, tRHW, tCAD;
1228*a430fa06SMiquel Raynal 
1229*a430fa06SMiquel Raynal 	/* T1 <=> tCLS */
1230*a430fa06SMiquel Raynal 	if (timings->tCLS_min > min_clk_period)
1231*a430fa06SMiquel Raynal 		min_clk_period = timings->tCLS_min;
1232*a430fa06SMiquel Raynal 
1233*a430fa06SMiquel Raynal 	/* T2 <=> tCLH */
1234*a430fa06SMiquel Raynal 	if (timings->tCLH_min > min_clk_period)
1235*a430fa06SMiquel Raynal 		min_clk_period = timings->tCLH_min;
1236*a430fa06SMiquel Raynal 
1237*a430fa06SMiquel Raynal 	/* T3 <=> tCS */
1238*a430fa06SMiquel Raynal 	if (timings->tCS_min > min_clk_period)
1239*a430fa06SMiquel Raynal 		min_clk_period = timings->tCS_min;
1240*a430fa06SMiquel Raynal 
1241*a430fa06SMiquel Raynal 	/* T4 <=> tCH */
1242*a430fa06SMiquel Raynal 	if (timings->tCH_min > min_clk_period)
1243*a430fa06SMiquel Raynal 		min_clk_period = timings->tCH_min;
1244*a430fa06SMiquel Raynal 
1245*a430fa06SMiquel Raynal 	/* T5 <=> tWP */
1246*a430fa06SMiquel Raynal 	if (timings->tWP_min > min_clk_period)
1247*a430fa06SMiquel Raynal 		min_clk_period = timings->tWP_min;
1248*a430fa06SMiquel Raynal 
1249*a430fa06SMiquel Raynal 	/* T6 <=> tWH */
1250*a430fa06SMiquel Raynal 	if (timings->tWH_min > min_clk_period)
1251*a430fa06SMiquel Raynal 		min_clk_period = timings->tWH_min;
1252*a430fa06SMiquel Raynal 
1253*a430fa06SMiquel Raynal 	/* T7 <=> tALS */
1254*a430fa06SMiquel Raynal 	if (timings->tALS_min > min_clk_period)
1255*a430fa06SMiquel Raynal 		min_clk_period = timings->tALS_min;
1256*a430fa06SMiquel Raynal 
1257*a430fa06SMiquel Raynal 	/* T8 <=> tDS */
1258*a430fa06SMiquel Raynal 	if (timings->tDS_min > min_clk_period)
1259*a430fa06SMiquel Raynal 		min_clk_period = timings->tDS_min;
1260*a430fa06SMiquel Raynal 
1261*a430fa06SMiquel Raynal 	/* T9 <=> tDH */
1262*a430fa06SMiquel Raynal 	if (timings->tDH_min > min_clk_period)
1263*a430fa06SMiquel Raynal 		min_clk_period = timings->tDH_min;
1264*a430fa06SMiquel Raynal 
1265*a430fa06SMiquel Raynal 	/* T10 <=> tRR */
1266*a430fa06SMiquel Raynal 	if (timings->tRR_min > (min_clk_period * 3))
1267*a430fa06SMiquel Raynal 		min_clk_period = DIV_ROUND_UP(timings->tRR_min, 3);
1268*a430fa06SMiquel Raynal 
1269*a430fa06SMiquel Raynal 	/* T11 <=> tALH */
1270*a430fa06SMiquel Raynal 	if (timings->tALH_min > min_clk_period)
1271*a430fa06SMiquel Raynal 		min_clk_period = timings->tALH_min;
1272*a430fa06SMiquel Raynal 
1273*a430fa06SMiquel Raynal 	/* T12 <=> tRP */
1274*a430fa06SMiquel Raynal 	if (timings->tRP_min > min_clk_period)
1275*a430fa06SMiquel Raynal 		min_clk_period = timings->tRP_min;
1276*a430fa06SMiquel Raynal 
1277*a430fa06SMiquel Raynal 	/* T13 <=> tREH */
1278*a430fa06SMiquel Raynal 	if (timings->tREH_min > min_clk_period)
1279*a430fa06SMiquel Raynal 		min_clk_period = timings->tREH_min;
1280*a430fa06SMiquel Raynal 
1281*a430fa06SMiquel Raynal 	/* T14 <=> tRC */
1282*a430fa06SMiquel Raynal 	if (timings->tRC_min > (min_clk_period * 2))
1283*a430fa06SMiquel Raynal 		min_clk_period = DIV_ROUND_UP(timings->tRC_min, 2);
1284*a430fa06SMiquel Raynal 
1285*a430fa06SMiquel Raynal 	/* T15 <=> tWC */
1286*a430fa06SMiquel Raynal 	if (timings->tWC_min > (min_clk_period * 2))
1287*a430fa06SMiquel Raynal 		min_clk_period = DIV_ROUND_UP(timings->tWC_min, 2);
1288*a430fa06SMiquel Raynal 
1289*a430fa06SMiquel Raynal 	/* T16 - T19 + tCAD */
1290*a430fa06SMiquel Raynal 	tWB  = sunxi_nand_lookup_timing(tWB_lut, timings->tWB_max,
1291*a430fa06SMiquel Raynal 					min_clk_period);
1292*a430fa06SMiquel Raynal 	if (tWB < 0) {
1293*a430fa06SMiquel Raynal 		dev_err(nfc->dev, "unsupported tWB\n");
1294*a430fa06SMiquel Raynal 		return tWB;
1295*a430fa06SMiquel Raynal 	}
1296*a430fa06SMiquel Raynal 
1297*a430fa06SMiquel Raynal 	tADL = DIV_ROUND_UP(timings->tADL_min, min_clk_period) >> 3;
1298*a430fa06SMiquel Raynal 	if (tADL > 3) {
1299*a430fa06SMiquel Raynal 		dev_err(nfc->dev, "unsupported tADL\n");
1300*a430fa06SMiquel Raynal 		return -EINVAL;
1301*a430fa06SMiquel Raynal 	}
1302*a430fa06SMiquel Raynal 
1303*a430fa06SMiquel Raynal 	tWHR = DIV_ROUND_UP(timings->tWHR_min, min_clk_period) >> 3;
1304*a430fa06SMiquel Raynal 	if (tWHR > 3) {
1305*a430fa06SMiquel Raynal 		dev_err(nfc->dev, "unsupported tWHR\n");
1306*a430fa06SMiquel Raynal 		return -EINVAL;
1307*a430fa06SMiquel Raynal 	}
1308*a430fa06SMiquel Raynal 
1309*a430fa06SMiquel Raynal 	tRHW = sunxi_nand_lookup_timing(tRHW_lut, timings->tRHW_min,
1310*a430fa06SMiquel Raynal 					min_clk_period);
1311*a430fa06SMiquel Raynal 	if (tRHW < 0) {
1312*a430fa06SMiquel Raynal 		dev_err(nfc->dev, "unsupported tRHW\n");
1313*a430fa06SMiquel Raynal 		return tRHW;
1314*a430fa06SMiquel Raynal 	}
1315*a430fa06SMiquel Raynal 
1316*a430fa06SMiquel Raynal 	/*
1317*a430fa06SMiquel Raynal 	 * TODO: according to ONFI specs this value only applies for DDR NAND,
1318*a430fa06SMiquel Raynal 	 * but Allwinner seems to set this to 0x7. Mimic them for now.
1319*a430fa06SMiquel Raynal 	 */
1320*a430fa06SMiquel Raynal 	tCAD = 0x7;
1321*a430fa06SMiquel Raynal 
1322*a430fa06SMiquel Raynal 	/* TODO: A83 has some more bits for CDQSS, CS, CLHZ, CCS, WC */
1323*a430fa06SMiquel Raynal 	chip->timing_cfg = NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD);
1324*a430fa06SMiquel Raynal 
1325*a430fa06SMiquel Raynal 	/*
1326*a430fa06SMiquel Raynal 	 * ONFI specification 3.1, paragraph 4.15.2 dictates that EDO data
1327*a430fa06SMiquel Raynal 	 * output cycle timings shall be used if the host drives tRC less than
1328*a430fa06SMiquel Raynal 	 * 30 ns.
1329*a430fa06SMiquel Raynal 	 */
1330*a430fa06SMiquel Raynal 	chip->timing_ctl = (timings->tRC_min < 30000) ? NFC_TIMING_CTL_EDO : 0;
1331*a430fa06SMiquel Raynal 
1332*a430fa06SMiquel Raynal 	/* Convert min_clk_period from picoseconds to nanoseconds */
1333*a430fa06SMiquel Raynal 	min_clk_period = DIV_ROUND_UP(min_clk_period, 1000);
1334*a430fa06SMiquel Raynal 
1335*a430fa06SMiquel Raynal 	/*
1336*a430fa06SMiquel Raynal 	 * Convert min_clk_period into a clk frequency, then get the
1337*a430fa06SMiquel Raynal 	 * appropriate rate for the NAND controller IP given this formula
1338*a430fa06SMiquel Raynal 	 * (specified in the datasheet):
1339*a430fa06SMiquel Raynal 	 * nand clk_rate = min_clk_rate
1340*a430fa06SMiquel Raynal 	 */
1341*a430fa06SMiquel Raynal 	chip->clk_rate = 1000000000L / min_clk_period;
1342*a430fa06SMiquel Raynal 
1343*a430fa06SMiquel Raynal 	return 0;
1344*a430fa06SMiquel Raynal }
1345*a430fa06SMiquel Raynal 
sunxi_nand_chip_init_timings(struct sunxi_nand_chip * chip)1346*a430fa06SMiquel Raynal static int sunxi_nand_chip_init_timings(struct sunxi_nand_chip *chip)
1347*a430fa06SMiquel Raynal {
1348*a430fa06SMiquel Raynal 	struct mtd_info *mtd = nand_to_mtd(&chip->nand);
1349*a430fa06SMiquel Raynal 	const struct nand_sdr_timings *timings;
1350*a430fa06SMiquel Raynal 	int ret;
1351*a430fa06SMiquel Raynal 	int mode;
1352*a430fa06SMiquel Raynal 
1353*a430fa06SMiquel Raynal 	mode = onfi_get_async_timing_mode(&chip->nand);
1354*a430fa06SMiquel Raynal 	if (mode == ONFI_TIMING_MODE_UNKNOWN) {
1355*a430fa06SMiquel Raynal 		mode = chip->nand.onfi_timing_mode_default;
1356*a430fa06SMiquel Raynal 	} else {
1357*a430fa06SMiquel Raynal 		uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {};
1358*a430fa06SMiquel Raynal 		int i;
1359*a430fa06SMiquel Raynal 
1360*a430fa06SMiquel Raynal 		mode = fls(mode) - 1;
1361*a430fa06SMiquel Raynal 		if (mode < 0)
1362*a430fa06SMiquel Raynal 			mode = 0;
1363*a430fa06SMiquel Raynal 
1364*a430fa06SMiquel Raynal 		feature[0] = mode;
1365*a430fa06SMiquel Raynal 		for (i = 0; i < chip->nsels; i++) {
1366*a430fa06SMiquel Raynal 			chip->nand.select_chip(mtd, i);
1367*a430fa06SMiquel Raynal 			ret = chip->nand.onfi_set_features(mtd,
1368*a430fa06SMiquel Raynal 						&chip->nand,
1369*a430fa06SMiquel Raynal 						ONFI_FEATURE_ADDR_TIMING_MODE,
1370*a430fa06SMiquel Raynal 						feature);
1371*a430fa06SMiquel Raynal 			chip->nand.select_chip(mtd, -1);
1372*a430fa06SMiquel Raynal 			if (ret && ret != -ENOTSUPP)
1373*a430fa06SMiquel Raynal 				return ret;
1374*a430fa06SMiquel Raynal 		}
1375*a430fa06SMiquel Raynal 	}
1376*a430fa06SMiquel Raynal 
1377*a430fa06SMiquel Raynal 	timings = onfi_async_timing_mode_to_sdr_timings(mode);
1378*a430fa06SMiquel Raynal 	if (IS_ERR(timings))
1379*a430fa06SMiquel Raynal 		return PTR_ERR(timings);
1380*a430fa06SMiquel Raynal 
1381*a430fa06SMiquel Raynal 	return sunxi_nand_chip_set_timings(chip, timings);
1382*a430fa06SMiquel Raynal }
1383*a430fa06SMiquel Raynal 
sunxi_nand_hw_common_ecc_ctrl_init(struct mtd_info * mtd,struct nand_ecc_ctrl * ecc)1384*a430fa06SMiquel Raynal static int sunxi_nand_hw_common_ecc_ctrl_init(struct mtd_info *mtd,
1385*a430fa06SMiquel Raynal 					      struct nand_ecc_ctrl *ecc)
1386*a430fa06SMiquel Raynal {
1387*a430fa06SMiquel Raynal 	static const u8 strengths[] = { 16, 24, 28, 32, 40, 48, 56, 60, 64 };
1388*a430fa06SMiquel Raynal 	struct sunxi_nand_hw_ecc *data;
1389*a430fa06SMiquel Raynal 	struct nand_ecclayout *layout;
1390*a430fa06SMiquel Raynal 	int nsectors;
1391*a430fa06SMiquel Raynal 	int ret;
1392*a430fa06SMiquel Raynal 	int i;
1393*a430fa06SMiquel Raynal 
1394*a430fa06SMiquel Raynal 	data = kzalloc(sizeof(*data), GFP_KERNEL);
1395*a430fa06SMiquel Raynal 	if (!data)
1396*a430fa06SMiquel Raynal 		return -ENOMEM;
1397*a430fa06SMiquel Raynal 
1398*a430fa06SMiquel Raynal 	if (ecc->size != 512 && ecc->size != 1024)
1399*a430fa06SMiquel Raynal 		return -EINVAL;
1400*a430fa06SMiquel Raynal 
1401*a430fa06SMiquel Raynal 	/* Prefer 1k ECC chunk over 512 ones */
1402*a430fa06SMiquel Raynal 	if (ecc->size == 512 && mtd->writesize > 512) {
1403*a430fa06SMiquel Raynal 		ecc->size = 1024;
1404*a430fa06SMiquel Raynal 		ecc->strength *= 2;
1405*a430fa06SMiquel Raynal 	}
1406*a430fa06SMiquel Raynal 
1407*a430fa06SMiquel Raynal 	/* Add ECC info retrieval from DT */
1408*a430fa06SMiquel Raynal 	for (i = 0; i < ARRAY_SIZE(strengths); i++) {
1409*a430fa06SMiquel Raynal 		if (ecc->strength <= strengths[i]) {
1410*a430fa06SMiquel Raynal 			/*
1411*a430fa06SMiquel Raynal 			 * Update ecc->strength value with the actual strength
1412*a430fa06SMiquel Raynal 			 * that will be used by the ECC engine.
1413*a430fa06SMiquel Raynal 			 */
1414*a430fa06SMiquel Raynal 			ecc->strength = strengths[i];
1415*a430fa06SMiquel Raynal 			break;
1416*a430fa06SMiquel Raynal 		}
1417*a430fa06SMiquel Raynal 	}
1418*a430fa06SMiquel Raynal 
1419*a430fa06SMiquel Raynal 	if (i >= ARRAY_SIZE(strengths)) {
1420*a430fa06SMiquel Raynal 		dev_err(nfc->dev, "unsupported strength\n");
1421*a430fa06SMiquel Raynal 		ret = -ENOTSUPP;
1422*a430fa06SMiquel Raynal 		goto err;
1423*a430fa06SMiquel Raynal 	}
1424*a430fa06SMiquel Raynal 
1425*a430fa06SMiquel Raynal 	data->mode = i;
1426*a430fa06SMiquel Raynal 
1427*a430fa06SMiquel Raynal 	/* HW ECC always request ECC bytes for 1024 bytes blocks */
1428*a430fa06SMiquel Raynal 	ecc->bytes = DIV_ROUND_UP(ecc->strength * fls(8 * 1024), 8);
1429*a430fa06SMiquel Raynal 
1430*a430fa06SMiquel Raynal 	/* HW ECC always work with even numbers of ECC bytes */
1431*a430fa06SMiquel Raynal 	ecc->bytes = ALIGN(ecc->bytes, 2);
1432*a430fa06SMiquel Raynal 
1433*a430fa06SMiquel Raynal 	layout = &data->layout;
1434*a430fa06SMiquel Raynal 	nsectors = mtd->writesize / ecc->size;
1435*a430fa06SMiquel Raynal 
1436*a430fa06SMiquel Raynal 	if (mtd->oobsize < ((ecc->bytes + 4) * nsectors)) {
1437*a430fa06SMiquel Raynal 		ret = -EINVAL;
1438*a430fa06SMiquel Raynal 		goto err;
1439*a430fa06SMiquel Raynal 	}
1440*a430fa06SMiquel Raynal 
1441*a430fa06SMiquel Raynal 	layout->eccbytes = (ecc->bytes * nsectors);
1442*a430fa06SMiquel Raynal 
1443*a430fa06SMiquel Raynal 	ecc->layout = layout;
1444*a430fa06SMiquel Raynal 	ecc->priv = data;
1445*a430fa06SMiquel Raynal 
1446*a430fa06SMiquel Raynal 	return 0;
1447*a430fa06SMiquel Raynal 
1448*a430fa06SMiquel Raynal err:
1449*a430fa06SMiquel Raynal 	kfree(data);
1450*a430fa06SMiquel Raynal 
1451*a430fa06SMiquel Raynal 	return ret;
1452*a430fa06SMiquel Raynal }
1453*a430fa06SMiquel Raynal 
1454*a430fa06SMiquel Raynal #ifndef __UBOOT__
sunxi_nand_hw_common_ecc_ctrl_cleanup(struct nand_ecc_ctrl * ecc)1455*a430fa06SMiquel Raynal static void sunxi_nand_hw_common_ecc_ctrl_cleanup(struct nand_ecc_ctrl *ecc)
1456*a430fa06SMiquel Raynal {
1457*a430fa06SMiquel Raynal 	kfree(ecc->priv);
1458*a430fa06SMiquel Raynal }
1459*a430fa06SMiquel Raynal #endif /* __UBOOT__ */
1460*a430fa06SMiquel Raynal 
sunxi_nand_hw_ecc_ctrl_init(struct mtd_info * mtd,struct nand_ecc_ctrl * ecc)1461*a430fa06SMiquel Raynal static int sunxi_nand_hw_ecc_ctrl_init(struct mtd_info *mtd,
1462*a430fa06SMiquel Raynal 				       struct nand_ecc_ctrl *ecc)
1463*a430fa06SMiquel Raynal {
1464*a430fa06SMiquel Raynal 	struct nand_ecclayout *layout;
1465*a430fa06SMiquel Raynal 	int nsectors;
1466*a430fa06SMiquel Raynal 	int i, j;
1467*a430fa06SMiquel Raynal 	int ret;
1468*a430fa06SMiquel Raynal 
1469*a430fa06SMiquel Raynal 	ret = sunxi_nand_hw_common_ecc_ctrl_init(mtd, ecc);
1470*a430fa06SMiquel Raynal 	if (ret)
1471*a430fa06SMiquel Raynal 		return ret;
1472*a430fa06SMiquel Raynal 
1473*a430fa06SMiquel Raynal 	ecc->read_page = sunxi_nfc_hw_ecc_read_page;
1474*a430fa06SMiquel Raynal 	ecc->write_page = sunxi_nfc_hw_ecc_write_page;
1475*a430fa06SMiquel Raynal 	ecc->read_subpage = sunxi_nfc_hw_ecc_read_subpage;
1476*a430fa06SMiquel Raynal 	ecc->write_subpage = sunxi_nfc_hw_ecc_write_subpage;
1477*a430fa06SMiquel Raynal 	layout = ecc->layout;
1478*a430fa06SMiquel Raynal 	nsectors = mtd->writesize / ecc->size;
1479*a430fa06SMiquel Raynal 
1480*a430fa06SMiquel Raynal 	for (i = 0; i < nsectors; i++) {
1481*a430fa06SMiquel Raynal 		if (i) {
1482*a430fa06SMiquel Raynal 			layout->oobfree[i].offset =
1483*a430fa06SMiquel Raynal 				layout->oobfree[i - 1].offset +
1484*a430fa06SMiquel Raynal 				layout->oobfree[i - 1].length +
1485*a430fa06SMiquel Raynal 				ecc->bytes;
1486*a430fa06SMiquel Raynal 			layout->oobfree[i].length = 4;
1487*a430fa06SMiquel Raynal 		} else {
1488*a430fa06SMiquel Raynal 			/*
1489*a430fa06SMiquel Raynal 			 * The first 2 bytes are used for BB markers, hence we
1490*a430fa06SMiquel Raynal 			 * only have 2 bytes available in the first user data
1491*a430fa06SMiquel Raynal 			 * section.
1492*a430fa06SMiquel Raynal 			 */
1493*a430fa06SMiquel Raynal 			layout->oobfree[i].length = 2;
1494*a430fa06SMiquel Raynal 			layout->oobfree[i].offset = 2;
1495*a430fa06SMiquel Raynal 		}
1496*a430fa06SMiquel Raynal 
1497*a430fa06SMiquel Raynal 		for (j = 0; j < ecc->bytes; j++)
1498*a430fa06SMiquel Raynal 			layout->eccpos[(ecc->bytes * i) + j] =
1499*a430fa06SMiquel Raynal 					layout->oobfree[i].offset +
1500*a430fa06SMiquel Raynal 					layout->oobfree[i].length + j;
1501*a430fa06SMiquel Raynal 	}
1502*a430fa06SMiquel Raynal 
1503*a430fa06SMiquel Raynal 	if (mtd->oobsize > (ecc->bytes + 4) * nsectors) {
1504*a430fa06SMiquel Raynal 		layout->oobfree[nsectors].offset =
1505*a430fa06SMiquel Raynal 				layout->oobfree[nsectors - 1].offset +
1506*a430fa06SMiquel Raynal 				layout->oobfree[nsectors - 1].length +
1507*a430fa06SMiquel Raynal 				ecc->bytes;
1508*a430fa06SMiquel Raynal 		layout->oobfree[nsectors].length = mtd->oobsize -
1509*a430fa06SMiquel Raynal 				((ecc->bytes + 4) * nsectors);
1510*a430fa06SMiquel Raynal 	}
1511*a430fa06SMiquel Raynal 
1512*a430fa06SMiquel Raynal 	return 0;
1513*a430fa06SMiquel Raynal }
1514*a430fa06SMiquel Raynal 
sunxi_nand_hw_syndrome_ecc_ctrl_init(struct mtd_info * mtd,struct nand_ecc_ctrl * ecc)1515*a430fa06SMiquel Raynal static int sunxi_nand_hw_syndrome_ecc_ctrl_init(struct mtd_info *mtd,
1516*a430fa06SMiquel Raynal 						struct nand_ecc_ctrl *ecc)
1517*a430fa06SMiquel Raynal {
1518*a430fa06SMiquel Raynal 	struct nand_ecclayout *layout;
1519*a430fa06SMiquel Raynal 	int nsectors;
1520*a430fa06SMiquel Raynal 	int i;
1521*a430fa06SMiquel Raynal 	int ret;
1522*a430fa06SMiquel Raynal 
1523*a430fa06SMiquel Raynal 	ret = sunxi_nand_hw_common_ecc_ctrl_init(mtd, ecc);
1524*a430fa06SMiquel Raynal 	if (ret)
1525*a430fa06SMiquel Raynal 		return ret;
1526*a430fa06SMiquel Raynal 
1527*a430fa06SMiquel Raynal 	ecc->prepad = 4;
1528*a430fa06SMiquel Raynal 	ecc->read_page = sunxi_nfc_hw_syndrome_ecc_read_page;
1529*a430fa06SMiquel Raynal 	ecc->write_page = sunxi_nfc_hw_syndrome_ecc_write_page;
1530*a430fa06SMiquel Raynal 
1531*a430fa06SMiquel Raynal 	layout = ecc->layout;
1532*a430fa06SMiquel Raynal 	nsectors = mtd->writesize / ecc->size;
1533*a430fa06SMiquel Raynal 
1534*a430fa06SMiquel Raynal 	for (i = 0; i < (ecc->bytes * nsectors); i++)
1535*a430fa06SMiquel Raynal 		layout->eccpos[i] = i;
1536*a430fa06SMiquel Raynal 
1537*a430fa06SMiquel Raynal 	layout->oobfree[0].length = mtd->oobsize - i;
1538*a430fa06SMiquel Raynal 	layout->oobfree[0].offset = i;
1539*a430fa06SMiquel Raynal 
1540*a430fa06SMiquel Raynal 	return 0;
1541*a430fa06SMiquel Raynal }
1542*a430fa06SMiquel Raynal 
1543*a430fa06SMiquel Raynal #ifndef __UBOOT__
sunxi_nand_ecc_cleanup(struct nand_ecc_ctrl * ecc)1544*a430fa06SMiquel Raynal static void sunxi_nand_ecc_cleanup(struct nand_ecc_ctrl *ecc)
1545*a430fa06SMiquel Raynal {
1546*a430fa06SMiquel Raynal 	switch (ecc->mode) {
1547*a430fa06SMiquel Raynal 	case NAND_ECC_HW:
1548*a430fa06SMiquel Raynal 	case NAND_ECC_HW_SYNDROME:
1549*a430fa06SMiquel Raynal 		sunxi_nand_hw_common_ecc_ctrl_cleanup(ecc);
1550*a430fa06SMiquel Raynal 		break;
1551*a430fa06SMiquel Raynal 	case NAND_ECC_NONE:
1552*a430fa06SMiquel Raynal 		kfree(ecc->layout);
1553*a430fa06SMiquel Raynal 	default:
1554*a430fa06SMiquel Raynal 		break;
1555*a430fa06SMiquel Raynal 	}
1556*a430fa06SMiquel Raynal }
1557*a430fa06SMiquel Raynal #endif /* __UBOOT__ */
1558*a430fa06SMiquel Raynal 
sunxi_nand_ecc_init(struct mtd_info * mtd,struct nand_ecc_ctrl * ecc)1559*a430fa06SMiquel Raynal static int sunxi_nand_ecc_init(struct mtd_info *mtd, struct nand_ecc_ctrl *ecc)
1560*a430fa06SMiquel Raynal {
1561*a430fa06SMiquel Raynal 	struct nand_chip *nand = mtd_to_nand(mtd);
1562*a430fa06SMiquel Raynal 	int ret;
1563*a430fa06SMiquel Raynal 
1564*a430fa06SMiquel Raynal 	if (!ecc->size) {
1565*a430fa06SMiquel Raynal 		ecc->size = nand->ecc_step_ds;
1566*a430fa06SMiquel Raynal 		ecc->strength = nand->ecc_strength_ds;
1567*a430fa06SMiquel Raynal 	}
1568*a430fa06SMiquel Raynal 
1569*a430fa06SMiquel Raynal 	if (!ecc->size || !ecc->strength)
1570*a430fa06SMiquel Raynal 		return -EINVAL;
1571*a430fa06SMiquel Raynal 
1572*a430fa06SMiquel Raynal 	switch (ecc->mode) {
1573*a430fa06SMiquel Raynal 	case NAND_ECC_SOFT_BCH:
1574*a430fa06SMiquel Raynal 		break;
1575*a430fa06SMiquel Raynal 	case NAND_ECC_HW:
1576*a430fa06SMiquel Raynal 		ret = sunxi_nand_hw_ecc_ctrl_init(mtd, ecc);
1577*a430fa06SMiquel Raynal 		if (ret)
1578*a430fa06SMiquel Raynal 			return ret;
1579*a430fa06SMiquel Raynal 		break;
1580*a430fa06SMiquel Raynal 	case NAND_ECC_HW_SYNDROME:
1581*a430fa06SMiquel Raynal 		ret = sunxi_nand_hw_syndrome_ecc_ctrl_init(mtd, ecc);
1582*a430fa06SMiquel Raynal 		if (ret)
1583*a430fa06SMiquel Raynal 			return ret;
1584*a430fa06SMiquel Raynal 		break;
1585*a430fa06SMiquel Raynal 	case NAND_ECC_NONE:
1586*a430fa06SMiquel Raynal 		ecc->layout = kzalloc(sizeof(*ecc->layout), GFP_KERNEL);
1587*a430fa06SMiquel Raynal 		if (!ecc->layout)
1588*a430fa06SMiquel Raynal 			return -ENOMEM;
1589*a430fa06SMiquel Raynal 		ecc->layout->oobfree[0].length = mtd->oobsize;
1590*a430fa06SMiquel Raynal 	case NAND_ECC_SOFT:
1591*a430fa06SMiquel Raynal 		break;
1592*a430fa06SMiquel Raynal 	default:
1593*a430fa06SMiquel Raynal 		return -EINVAL;
1594*a430fa06SMiquel Raynal 	}
1595*a430fa06SMiquel Raynal 
1596*a430fa06SMiquel Raynal 	return 0;
1597*a430fa06SMiquel Raynal }
1598*a430fa06SMiquel Raynal 
sunxi_nand_chip_init(int node,struct sunxi_nfc * nfc,int devnum)1599*a430fa06SMiquel Raynal static int sunxi_nand_chip_init(int node, struct sunxi_nfc *nfc, int devnum)
1600*a430fa06SMiquel Raynal {
1601*a430fa06SMiquel Raynal 	const struct nand_sdr_timings *timings;
1602*a430fa06SMiquel Raynal 	const void *blob = gd->fdt_blob;
1603*a430fa06SMiquel Raynal 	struct sunxi_nand_chip *chip;
1604*a430fa06SMiquel Raynal 	struct mtd_info *mtd;
1605*a430fa06SMiquel Raynal 	struct nand_chip *nand;
1606*a430fa06SMiquel Raynal 	int nsels;
1607*a430fa06SMiquel Raynal 	int ret;
1608*a430fa06SMiquel Raynal 	int i;
1609*a430fa06SMiquel Raynal 	u32 cs[8], rb[8];
1610*a430fa06SMiquel Raynal 
1611*a430fa06SMiquel Raynal 	if (!fdt_getprop(blob, node, "reg", &nsels))
1612*a430fa06SMiquel Raynal 		return -EINVAL;
1613*a430fa06SMiquel Raynal 
1614*a430fa06SMiquel Raynal 	nsels /= sizeof(u32);
1615*a430fa06SMiquel Raynal 	if (!nsels || nsels > 8) {
1616*a430fa06SMiquel Raynal 		dev_err(dev, "invalid reg property size\n");
1617*a430fa06SMiquel Raynal 		return -EINVAL;
1618*a430fa06SMiquel Raynal 	}
1619*a430fa06SMiquel Raynal 
1620*a430fa06SMiquel Raynal 	chip = kzalloc(sizeof(*chip) +
1621*a430fa06SMiquel Raynal 		       (nsels * sizeof(struct sunxi_nand_chip_sel)),
1622*a430fa06SMiquel Raynal 		       GFP_KERNEL);
1623*a430fa06SMiquel Raynal 	if (!chip) {
1624*a430fa06SMiquel Raynal 		dev_err(dev, "could not allocate chip\n");
1625*a430fa06SMiquel Raynal 		return -ENOMEM;
1626*a430fa06SMiquel Raynal 	}
1627*a430fa06SMiquel Raynal 
1628*a430fa06SMiquel Raynal 	chip->nsels = nsels;
1629*a430fa06SMiquel Raynal 	chip->selected = -1;
1630*a430fa06SMiquel Raynal 
1631*a430fa06SMiquel Raynal 	for (i = 0; i < nsels; i++) {
1632*a430fa06SMiquel Raynal 		cs[i] = -1;
1633*a430fa06SMiquel Raynal 		rb[i] = -1;
1634*a430fa06SMiquel Raynal 	}
1635*a430fa06SMiquel Raynal 
1636*a430fa06SMiquel Raynal 	ret = fdtdec_get_int_array(gd->fdt_blob, node, "reg", cs, nsels);
1637*a430fa06SMiquel Raynal 	if (ret) {
1638*a430fa06SMiquel Raynal 		dev_err(dev, "could not retrieve reg property: %d\n", ret);
1639*a430fa06SMiquel Raynal 		return ret;
1640*a430fa06SMiquel Raynal 	}
1641*a430fa06SMiquel Raynal 
1642*a430fa06SMiquel Raynal 	ret = fdtdec_get_int_array(gd->fdt_blob, node, "allwinner,rb", rb,
1643*a430fa06SMiquel Raynal 				   nsels);
1644*a430fa06SMiquel Raynal 	if (ret) {
1645*a430fa06SMiquel Raynal 		dev_err(dev, "could not retrieve reg property: %d\n", ret);
1646*a430fa06SMiquel Raynal 		return ret;
1647*a430fa06SMiquel Raynal 	}
1648*a430fa06SMiquel Raynal 
1649*a430fa06SMiquel Raynal 	for (i = 0; i < nsels; i++) {
1650*a430fa06SMiquel Raynal 		int tmp = cs[i];
1651*a430fa06SMiquel Raynal 
1652*a430fa06SMiquel Raynal 		if (tmp > NFC_MAX_CS) {
1653*a430fa06SMiquel Raynal 			dev_err(dev,
1654*a430fa06SMiquel Raynal 				"invalid reg value: %u (max CS = 7)\n",
1655*a430fa06SMiquel Raynal 				tmp);
1656*a430fa06SMiquel Raynal 			return -EINVAL;
1657*a430fa06SMiquel Raynal 		}
1658*a430fa06SMiquel Raynal 
1659*a430fa06SMiquel Raynal 		if (test_and_set_bit(tmp, &nfc->assigned_cs)) {
1660*a430fa06SMiquel Raynal 			dev_err(dev, "CS %d already assigned\n", tmp);
1661*a430fa06SMiquel Raynal 			return -EINVAL;
1662*a430fa06SMiquel Raynal 		}
1663*a430fa06SMiquel Raynal 
1664*a430fa06SMiquel Raynal 		chip->sels[i].cs = tmp;
1665*a430fa06SMiquel Raynal 
1666*a430fa06SMiquel Raynal 		tmp = rb[i];
1667*a430fa06SMiquel Raynal 		if (tmp >= 0 && tmp < 2) {
1668*a430fa06SMiquel Raynal 			chip->sels[i].rb.type = RB_NATIVE;
1669*a430fa06SMiquel Raynal 			chip->sels[i].rb.info.nativeid = tmp;
1670*a430fa06SMiquel Raynal 		} else {
1671*a430fa06SMiquel Raynal 			ret = gpio_request_by_name_nodev(offset_to_ofnode(node),
1672*a430fa06SMiquel Raynal 						"rb-gpios", i,
1673*a430fa06SMiquel Raynal 						&chip->sels[i].rb.info.gpio,
1674*a430fa06SMiquel Raynal 						GPIOD_IS_IN);
1675*a430fa06SMiquel Raynal 			if (ret)
1676*a430fa06SMiquel Raynal 				chip->sels[i].rb.type = RB_GPIO;
1677*a430fa06SMiquel Raynal 			else
1678*a430fa06SMiquel Raynal 				chip->sels[i].rb.type = RB_NONE;
1679*a430fa06SMiquel Raynal 		}
1680*a430fa06SMiquel Raynal 	}
1681*a430fa06SMiquel Raynal 
1682*a430fa06SMiquel Raynal 	timings = onfi_async_timing_mode_to_sdr_timings(0);
1683*a430fa06SMiquel Raynal 	if (IS_ERR(timings)) {
1684*a430fa06SMiquel Raynal 		ret = PTR_ERR(timings);
1685*a430fa06SMiquel Raynal 		dev_err(dev,
1686*a430fa06SMiquel Raynal 			"could not retrieve timings for ONFI mode 0: %d\n",
1687*a430fa06SMiquel Raynal 			ret);
1688*a430fa06SMiquel Raynal 		return ret;
1689*a430fa06SMiquel Raynal 	}
1690*a430fa06SMiquel Raynal 
1691*a430fa06SMiquel Raynal 	ret = sunxi_nand_chip_set_timings(chip, timings);
1692*a430fa06SMiquel Raynal 	if (ret) {
1693*a430fa06SMiquel Raynal 		dev_err(dev, "could not configure chip timings: %d\n", ret);
1694*a430fa06SMiquel Raynal 		return ret;
1695*a430fa06SMiquel Raynal 	}
1696*a430fa06SMiquel Raynal 
1697*a430fa06SMiquel Raynal 	nand = &chip->nand;
1698*a430fa06SMiquel Raynal 	/* Default tR value specified in the ONFI spec (chapter 4.15.1) */
1699*a430fa06SMiquel Raynal 	nand->chip_delay = 200;
1700*a430fa06SMiquel Raynal 	nand->controller = &nfc->controller;
1701*a430fa06SMiquel Raynal 	/*
1702*a430fa06SMiquel Raynal 	 * Set the ECC mode to the default value in case nothing is specified
1703*a430fa06SMiquel Raynal 	 * in the DT.
1704*a430fa06SMiquel Raynal 	 */
1705*a430fa06SMiquel Raynal 	nand->ecc.mode = NAND_ECC_HW;
1706*a430fa06SMiquel Raynal 	nand->flash_node = node;
1707*a430fa06SMiquel Raynal 	nand->select_chip = sunxi_nfc_select_chip;
1708*a430fa06SMiquel Raynal 	nand->cmd_ctrl = sunxi_nfc_cmd_ctrl;
1709*a430fa06SMiquel Raynal 	nand->read_buf = sunxi_nfc_read_buf;
1710*a430fa06SMiquel Raynal 	nand->write_buf = sunxi_nfc_write_buf;
1711*a430fa06SMiquel Raynal 	nand->read_byte = sunxi_nfc_read_byte;
1712*a430fa06SMiquel Raynal 
1713*a430fa06SMiquel Raynal 	mtd = nand_to_mtd(nand);
1714*a430fa06SMiquel Raynal 	ret = nand_scan_ident(mtd, nsels, NULL);
1715*a430fa06SMiquel Raynal 	if (ret)
1716*a430fa06SMiquel Raynal 		return ret;
1717*a430fa06SMiquel Raynal 
1718*a430fa06SMiquel Raynal 	if (nand->bbt_options & NAND_BBT_USE_FLASH)
1719*a430fa06SMiquel Raynal 		nand->bbt_options |= NAND_BBT_NO_OOB;
1720*a430fa06SMiquel Raynal 
1721*a430fa06SMiquel Raynal 	if (nand->options & NAND_NEED_SCRAMBLING)
1722*a430fa06SMiquel Raynal 		nand->options |= NAND_NO_SUBPAGE_WRITE;
1723*a430fa06SMiquel Raynal 
1724*a430fa06SMiquel Raynal 	nand->options |= NAND_SUBPAGE_READ;
1725*a430fa06SMiquel Raynal 
1726*a430fa06SMiquel Raynal 	ret = sunxi_nand_chip_init_timings(chip);
1727*a430fa06SMiquel Raynal 	if (ret) {
1728*a430fa06SMiquel Raynal 		dev_err(dev, "could not configure chip timings: %d\n", ret);
1729*a430fa06SMiquel Raynal 		return ret;
1730*a430fa06SMiquel Raynal 	}
1731*a430fa06SMiquel Raynal 
1732*a430fa06SMiquel Raynal 	ret = sunxi_nand_ecc_init(mtd, &nand->ecc);
1733*a430fa06SMiquel Raynal 	if (ret) {
1734*a430fa06SMiquel Raynal 		dev_err(dev, "ECC init failed: %d\n", ret);
1735*a430fa06SMiquel Raynal 		return ret;
1736*a430fa06SMiquel Raynal 	}
1737*a430fa06SMiquel Raynal 
1738*a430fa06SMiquel Raynal 	ret = nand_scan_tail(mtd);
1739*a430fa06SMiquel Raynal 	if (ret) {
1740*a430fa06SMiquel Raynal 		dev_err(dev, "nand_scan_tail failed: %d\n", ret);
1741*a430fa06SMiquel Raynal 		return ret;
1742*a430fa06SMiquel Raynal 	}
1743*a430fa06SMiquel Raynal 
1744*a430fa06SMiquel Raynal 	ret = nand_register(devnum, mtd);
1745*a430fa06SMiquel Raynal 	if (ret) {
1746*a430fa06SMiquel Raynal 		dev_err(dev, "failed to register mtd device: %d\n", ret);
1747*a430fa06SMiquel Raynal 		return ret;
1748*a430fa06SMiquel Raynal 	}
1749*a430fa06SMiquel Raynal 
1750*a430fa06SMiquel Raynal 	list_add_tail(&chip->node, &nfc->chips);
1751*a430fa06SMiquel Raynal 
1752*a430fa06SMiquel Raynal 	return 0;
1753*a430fa06SMiquel Raynal }
1754*a430fa06SMiquel Raynal 
sunxi_nand_chips_init(int node,struct sunxi_nfc * nfc)1755*a430fa06SMiquel Raynal static int sunxi_nand_chips_init(int node, struct sunxi_nfc *nfc)
1756*a430fa06SMiquel Raynal {
1757*a430fa06SMiquel Raynal 	const void *blob = gd->fdt_blob;
1758*a430fa06SMiquel Raynal 	int nand_node;
1759*a430fa06SMiquel Raynal 	int ret, i = 0;
1760*a430fa06SMiquel Raynal 
1761*a430fa06SMiquel Raynal 	for (nand_node = fdt_first_subnode(blob, node); nand_node >= 0;
1762*a430fa06SMiquel Raynal 	     nand_node = fdt_next_subnode(blob, nand_node))
1763*a430fa06SMiquel Raynal 		i++;
1764*a430fa06SMiquel Raynal 
1765*a430fa06SMiquel Raynal 	if (i > 8) {
1766*a430fa06SMiquel Raynal 		dev_err(dev, "too many NAND chips: %d (max = 8)\n", i);
1767*a430fa06SMiquel Raynal 		return -EINVAL;
1768*a430fa06SMiquel Raynal 	}
1769*a430fa06SMiquel Raynal 
1770*a430fa06SMiquel Raynal 	i = 0;
1771*a430fa06SMiquel Raynal 	for (nand_node = fdt_first_subnode(blob, node); nand_node >= 0;
1772*a430fa06SMiquel Raynal 	     nand_node = fdt_next_subnode(blob, nand_node)) {
1773*a430fa06SMiquel Raynal 		ret = sunxi_nand_chip_init(nand_node, nfc, i++);
1774*a430fa06SMiquel Raynal 		if (ret)
1775*a430fa06SMiquel Raynal 			return ret;
1776*a430fa06SMiquel Raynal 	}
1777*a430fa06SMiquel Raynal 
1778*a430fa06SMiquel Raynal 	return 0;
1779*a430fa06SMiquel Raynal }
1780*a430fa06SMiquel Raynal 
1781*a430fa06SMiquel Raynal #ifndef __UBOOT__
sunxi_nand_chips_cleanup(struct sunxi_nfc * nfc)1782*a430fa06SMiquel Raynal static void sunxi_nand_chips_cleanup(struct sunxi_nfc *nfc)
1783*a430fa06SMiquel Raynal {
1784*a430fa06SMiquel Raynal 	struct sunxi_nand_chip *chip;
1785*a430fa06SMiquel Raynal 
1786*a430fa06SMiquel Raynal 	while (!list_empty(&nfc->chips)) {
1787*a430fa06SMiquel Raynal 		chip = list_first_entry(&nfc->chips, struct sunxi_nand_chip,
1788*a430fa06SMiquel Raynal 					node);
1789*a430fa06SMiquel Raynal 		nand_release(&chip->mtd);
1790*a430fa06SMiquel Raynal 		sunxi_nand_ecc_cleanup(&chip->nand.ecc);
1791*a430fa06SMiquel Raynal 		list_del(&chip->node);
1792*a430fa06SMiquel Raynal 		kfree(chip);
1793*a430fa06SMiquel Raynal 	}
1794*a430fa06SMiquel Raynal }
1795*a430fa06SMiquel Raynal #endif /* __UBOOT__ */
1796*a430fa06SMiquel Raynal 
sunxi_nand_init(void)1797*a430fa06SMiquel Raynal void sunxi_nand_init(void)
1798*a430fa06SMiquel Raynal {
1799*a430fa06SMiquel Raynal 	const void *blob = gd->fdt_blob;
1800*a430fa06SMiquel Raynal 	struct sunxi_nfc *nfc;
1801*a430fa06SMiquel Raynal 	fdt_addr_t regs;
1802*a430fa06SMiquel Raynal 	int node;
1803*a430fa06SMiquel Raynal 	int ret;
1804*a430fa06SMiquel Raynal 
1805*a430fa06SMiquel Raynal 	nfc = kzalloc(sizeof(*nfc), GFP_KERNEL);
1806*a430fa06SMiquel Raynal 	if (!nfc)
1807*a430fa06SMiquel Raynal 		return;
1808*a430fa06SMiquel Raynal 
1809*a430fa06SMiquel Raynal 	spin_lock_init(&nfc->controller.lock);
1810*a430fa06SMiquel Raynal 	init_waitqueue_head(&nfc->controller.wq);
1811*a430fa06SMiquel Raynal 	INIT_LIST_HEAD(&nfc->chips);
1812*a430fa06SMiquel Raynal 
1813*a430fa06SMiquel Raynal 	node = fdtdec_next_compatible(blob, 0, COMPAT_SUNXI_NAND);
1814*a430fa06SMiquel Raynal 	if (node < 0) {
1815*a430fa06SMiquel Raynal 		pr_err("unable to find nfc node in device tree\n");
1816*a430fa06SMiquel Raynal 		goto err;
1817*a430fa06SMiquel Raynal 	}
1818*a430fa06SMiquel Raynal 
1819*a430fa06SMiquel Raynal 	if (!fdtdec_get_is_enabled(blob, node)) {
1820*a430fa06SMiquel Raynal 		pr_err("nfc disabled in device tree\n");
1821*a430fa06SMiquel Raynal 		goto err;
1822*a430fa06SMiquel Raynal 	}
1823*a430fa06SMiquel Raynal 
1824*a430fa06SMiquel Raynal 	regs = fdtdec_get_addr(blob, node, "reg");
1825*a430fa06SMiquel Raynal 	if (regs == FDT_ADDR_T_NONE) {
1826*a430fa06SMiquel Raynal 		pr_err("unable to find nfc address in device tree\n");
1827*a430fa06SMiquel Raynal 		goto err;
1828*a430fa06SMiquel Raynal 	}
1829*a430fa06SMiquel Raynal 
1830*a430fa06SMiquel Raynal 	nfc->regs = (void *)regs;
1831*a430fa06SMiquel Raynal 
1832*a430fa06SMiquel Raynal 	ret = sunxi_nfc_rst(nfc);
1833*a430fa06SMiquel Raynal 	if (ret)
1834*a430fa06SMiquel Raynal 		goto err;
1835*a430fa06SMiquel Raynal 
1836*a430fa06SMiquel Raynal 	ret = sunxi_nand_chips_init(node, nfc);
1837*a430fa06SMiquel Raynal 	if (ret) {
1838*a430fa06SMiquel Raynal 		dev_err(dev, "failed to init nand chips\n");
1839*a430fa06SMiquel Raynal 		goto err;
1840*a430fa06SMiquel Raynal 	}
1841*a430fa06SMiquel Raynal 
1842*a430fa06SMiquel Raynal 	return;
1843*a430fa06SMiquel Raynal 
1844*a430fa06SMiquel Raynal err:
1845*a430fa06SMiquel Raynal 	kfree(nfc);
1846*a430fa06SMiquel Raynal }
1847*a430fa06SMiquel Raynal 
1848*a430fa06SMiquel Raynal MODULE_LICENSE("GPL v2");
1849*a430fa06SMiquel Raynal MODULE_AUTHOR("Boris BREZILLON");
1850*a430fa06SMiquel Raynal MODULE_DESCRIPTION("Allwinner NAND Flash Controller driver");
1851