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