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