2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon.dev@gmail.com>
5 * https://github.com/yuq/sunxi-nfc-mtd
6 * Copyright (C) 2013 Qiang Yu <yuq825@gmail.com>
8 * https://github.com/hno/Allwinner-Info
9 * Copyright (C) 2013 Henrik Nordström <Henrik Nordström>
11 * Copyright (C) 2013 Dmitriy B. <rzk333@gmail.com>
12 * Copyright (C) 2013 Sergey Lapin <slapin@ossfans.org>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
25 #include <linux/dma-mapping.h>
26 #include <linux/slab.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/platform_device.h>
31 #include <linux/of_device.h>
32 #include <linux/of_gpio.h>
33 #include <linux/of_mtd.h>
34 #include <linux/mtd/mtd.h>
35 #include <linux/mtd/nand.h>
36 #include <linux/mtd/partitions.h>
37 #include <linux/clk.h>
38 #include <linux/delay.h>
39 #include <linux/dmaengine.h>
40 #include <linux/gpio.h>
41 #include <linux/interrupt.h>
44 #define NFC_REG_CTL 0x0000
45 #define NFC_REG_ST 0x0004
46 #define NFC_REG_INT 0x0008
47 #define NFC_REG_TIMING_CTL 0x000C
48 #define NFC_REG_TIMING_CFG 0x0010
49 #define NFC_REG_ADDR_LOW 0x0014
50 #define NFC_REG_ADDR_HIGH 0x0018
51 #define NFC_REG_SECTOR_NUM 0x001C
52 #define NFC_REG_CNT 0x0020
53 #define NFC_REG_CMD 0x0024
54 #define NFC_REG_RCMD_SET 0x0028
55 #define NFC_REG_WCMD_SET 0x002C
56 #define NFC_REG_IO_DATA 0x0030
57 #define NFC_REG_ECC_CTL 0x0034
58 #define NFC_REG_ECC_ST 0x0038
59 #define NFC_REG_DEBUG 0x003C
60 #define NFC_REG_ECC_ERR_CNT(x) ((0x0040 + (x)) & ~0x3)
61 #define NFC_REG_USER_DATA(x) (0x0050 + ((x) * 4))
62 #define NFC_REG_SPARE_AREA 0x00A0
63 #define NFC_REG_PAT_ID 0x00A4
64 #define NFC_RAM0_BASE 0x0400
65 #define NFC_RAM1_BASE 0x0800
67 /* define bit use in NFC_CTL */
69 #define NFC_RESET BIT(1)
70 #define NFC_BUS_WIDTH_MSK BIT(2)
71 #define NFC_BUS_WIDTH_8 (0 << 2)
72 #define NFC_BUS_WIDTH_16 (1 << 2)
73 #define NFC_RB_SEL_MSK BIT(3)
74 #define NFC_RB_SEL(x) ((x) << 3)
75 #define NFC_CE_SEL_MSK GENMASK(26, 24)
76 #define NFC_CE_SEL(x) ((x) << 24)
77 #define NFC_CE_CTL BIT(6)
78 #define NFC_PAGE_SHIFT_MSK GENMASK(11, 8)
79 #define NFC_PAGE_SHIFT(x) (((x) < 10 ? 0 : (x) - 10) << 8)
80 #define NFC_SAM BIT(12)
81 #define NFC_RAM_METHOD BIT(14)
82 #define NFC_DEBUG_CTL BIT(31)
84 /* define bit use in NFC_ST */
85 #define NFC_RB_B2R BIT(0)
86 #define NFC_CMD_INT_FLAG BIT(1)
87 #define NFC_DMA_INT_FLAG BIT(2)
88 #define NFC_CMD_FIFO_STATUS BIT(3)
89 #define NFC_STA BIT(4)
90 #define NFC_NATCH_INT_FLAG BIT(5)
91 #define NFC_RB_STATE(x) BIT(x + 8)
93 /* define bit use in NFC_INT */
94 #define NFC_B2R_INT_ENABLE BIT(0)
95 #define NFC_CMD_INT_ENABLE BIT(1)
96 #define NFC_DMA_INT_ENABLE BIT(2)
97 #define NFC_INT_MASK (NFC_B2R_INT_ENABLE | \
98 NFC_CMD_INT_ENABLE | \
101 /* define bit use in NFC_TIMING_CTL */
102 #define NFC_TIMING_CTL_EDO BIT(8)
104 /* define NFC_TIMING_CFG register layout */
105 #define NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD) \
106 (((tWB) & 0x3) | (((tADL) & 0x3) << 2) | \
107 (((tWHR) & 0x3) << 4) | (((tRHW) & 0x3) << 6) | \
108 (((tCAD) & 0x7) << 8))
110 /* define bit use in NFC_CMD */
111 #define NFC_CMD_LOW_BYTE_MSK GENMASK(7, 0)
112 #define NFC_CMD_HIGH_BYTE_MSK GENMASK(15, 8)
113 #define NFC_CMD(x) (x)
114 #define NFC_ADR_NUM_MSK GENMASK(18, 16)
115 #define NFC_ADR_NUM(x) (((x) - 1) << 16)
116 #define NFC_SEND_ADR BIT(19)
117 #define NFC_ACCESS_DIR BIT(20)
118 #define NFC_DATA_TRANS BIT(21)
119 #define NFC_SEND_CMD1 BIT(22)
120 #define NFC_WAIT_FLAG BIT(23)
121 #define NFC_SEND_CMD2 BIT(24)
122 #define NFC_SEQ BIT(25)
123 #define NFC_DATA_SWAP_METHOD BIT(26)
124 #define NFC_ROW_AUTO_INC BIT(27)
125 #define NFC_SEND_CMD3 BIT(28)
126 #define NFC_SEND_CMD4 BIT(29)
127 #define NFC_CMD_TYPE_MSK GENMASK(31, 30)
128 #define NFC_NORMAL_OP (0 << 30)
129 #define NFC_ECC_OP (1 << 30)
130 #define NFC_PAGE_OP (2 << 30)
132 /* define bit use in NFC_RCMD_SET */
133 #define NFC_READ_CMD_MSK GENMASK(7, 0)
134 #define NFC_RND_READ_CMD0_MSK GENMASK(15, 8)
135 #define NFC_RND_READ_CMD1_MSK GENMASK(23, 16)
137 /* define bit use in NFC_WCMD_SET */
138 #define NFC_PROGRAM_CMD_MSK GENMASK(7, 0)
139 #define NFC_RND_WRITE_CMD_MSK GENMASK(15, 8)
140 #define NFC_READ_CMD0_MSK GENMASK(23, 16)
141 #define NFC_READ_CMD1_MSK GENMASK(31, 24)
143 /* define bit use in NFC_ECC_CTL */
144 #define NFC_ECC_EN BIT(0)
145 #define NFC_ECC_PIPELINE BIT(3)
146 #define NFC_ECC_EXCEPTION BIT(4)
147 #define NFC_ECC_BLOCK_SIZE_MSK BIT(5)
148 #define NFC_RANDOM_EN BIT(9)
149 #define NFC_RANDOM_DIRECTION BIT(10)
150 #define NFC_ECC_MODE_MSK GENMASK(15, 12)
151 #define NFC_ECC_MODE(x) ((x) << 12)
152 #define NFC_RANDOM_SEED_MSK GENMASK(30, 16)
153 #define NFC_RANDOM_SEED(x) ((x) << 16)
155 /* define bit use in NFC_ECC_ST */
156 #define NFC_ECC_ERR(x) BIT(x)
157 #define NFC_ECC_PAT_FOUND(x) BIT(x + 16)
158 #define NFC_ECC_ERR_CNT(b, x) (((x) >> ((b) * 8)) & 0xff)
160 #define NFC_DEFAULT_TIMEOUT_MS 1000
162 #define NFC_SRAM_SIZE 1024
167 * Ready/Busy detection type: describes the Ready/Busy detection modes
169 * @RB_NONE: no external detection available, rely on STATUS command
170 * and software timeouts
171 * @RB_NATIVE: use sunxi NAND controller Ready/Busy support. The Ready/Busy
172 * pin of the NAND flash chip must be connected to one of the
173 * native NAND R/B pins (those which can be muxed to the NAND
175 * @RB_GPIO: use a simple GPIO to handle Ready/Busy status. The Ready/Busy
176 * pin of the NAND flash chip must be connected to a GPIO capable
179 enum sunxi_nand_rb_type
{
186 * Ready/Busy structure: stores information related to Ready/Busy detection
188 * @type: the Ready/Busy detection mode
189 * @info: information related to the R/B detection mode. Either a gpio
190 * id or a native R/B id (those supported by the NAND controller).
192 struct sunxi_nand_rb
{
193 enum sunxi_nand_rb_type type
;
201 * Chip Select structure: stores information related to NAND Chip Select
203 * @cs: the NAND CS id used to communicate with a NAND Chip
204 * @rb: the Ready/Busy description
206 struct sunxi_nand_chip_sel
{
208 struct sunxi_nand_rb rb
;
212 * sunxi HW ECC infos: stores information related to HW ECC support
214 * @mode: the sunxi ECC mode field deduced from ECC requirements
215 * @layout: the OOB layout depending on the ECC requirements and the
218 struct sunxi_nand_hw_ecc
{
220 struct nand_ecclayout layout
;
224 * NAND chip structure: stores NAND chip device related information
226 * @node: used to store NAND chips into a list
227 * @nand: base NAND chip structure
228 * @mtd: base MTD structure
229 * @clk_rate: clk_rate required for this NAND chip
230 * @timing_cfg TIMING_CFG register value for this NAND chip
231 * @selected: current active CS
232 * @nsels: number of CS lines required by the NAND chip
233 * @sels: array of CS lines descriptions
235 struct sunxi_nand_chip
{
236 struct list_head node
;
237 struct nand_chip nand
;
238 unsigned long clk_rate
;
243 struct sunxi_nand_chip_sel sels
[0];
246 static inline struct sunxi_nand_chip
*to_sunxi_nand(struct nand_chip
*nand
)
248 return container_of(nand
, struct sunxi_nand_chip
, nand
);
252 * NAND Controller structure: stores sunxi NAND controller information
254 * @controller: base controller structure
255 * @dev: parent device (used to print error messages)
256 * @regs: NAND controller registers
257 * @ahb_clk: NAND Controller AHB clock
258 * @mod_clk: NAND Controller mod clock
259 * @assigned_cs: bitmask describing already assigned CS lines
260 * @clk_rate: NAND controller current clock rate
261 * @chips: a list containing all the NAND chips attached to
262 * this NAND controller
263 * @complete: a completion object used to wait for NAND
267 struct nand_hw_control controller
;
272 unsigned long assigned_cs
;
273 unsigned long clk_rate
;
274 struct list_head chips
;
275 struct completion complete
;
278 static inline struct sunxi_nfc
*to_sunxi_nfc(struct nand_hw_control
*ctrl
)
280 return container_of(ctrl
, struct sunxi_nfc
, controller
);
283 static irqreturn_t
sunxi_nfc_interrupt(int irq
, void *dev_id
)
285 struct sunxi_nfc
*nfc
= dev_id
;
286 u32 st
= readl(nfc
->regs
+ NFC_REG_ST
);
287 u32 ien
= readl(nfc
->regs
+ NFC_REG_INT
);
292 if ((ien
& st
) == ien
)
293 complete(&nfc
->complete
);
295 writel(st
& NFC_INT_MASK
, nfc
->regs
+ NFC_REG_ST
);
296 writel(~st
& ien
& NFC_INT_MASK
, nfc
->regs
+ NFC_REG_INT
);
301 static int sunxi_nfc_wait_int(struct sunxi_nfc
*nfc
, u32 flags
,
302 unsigned int timeout_ms
)
304 init_completion(&nfc
->complete
);
306 writel(flags
, nfc
->regs
+ NFC_REG_INT
);
309 timeout_ms
= NFC_DEFAULT_TIMEOUT_MS
;
311 if (!wait_for_completion_timeout(&nfc
->complete
,
312 msecs_to_jiffies(timeout_ms
))) {
313 dev_err(nfc
->dev
, "wait interrupt timedout\n");
320 static int sunxi_nfc_wait_cmd_fifo_empty(struct sunxi_nfc
*nfc
)
322 unsigned long timeout
= jiffies
+
323 msecs_to_jiffies(NFC_DEFAULT_TIMEOUT_MS
);
326 if (!(readl(nfc
->regs
+ NFC_REG_ST
) & NFC_CMD_FIFO_STATUS
))
328 } while (time_before(jiffies
, timeout
));
330 dev_err(nfc
->dev
, "wait for empty cmd FIFO timedout\n");
334 static int sunxi_nfc_rst(struct sunxi_nfc
*nfc
)
336 unsigned long timeout
= jiffies
+
337 msecs_to_jiffies(NFC_DEFAULT_TIMEOUT_MS
);
339 writel(0, nfc
->regs
+ NFC_REG_ECC_CTL
);
340 writel(NFC_RESET
, nfc
->regs
+ NFC_REG_CTL
);
343 if (!(readl(nfc
->regs
+ NFC_REG_CTL
) & NFC_RESET
))
345 } while (time_before(jiffies
, timeout
));
347 dev_err(nfc
->dev
, "wait for NAND controller reset timedout\n");
351 static int sunxi_nfc_dev_ready(struct mtd_info
*mtd
)
353 struct nand_chip
*nand
= mtd_to_nand(mtd
);
354 struct sunxi_nand_chip
*sunxi_nand
= to_sunxi_nand(nand
);
355 struct sunxi_nfc
*nfc
= to_sunxi_nfc(sunxi_nand
->nand
.controller
);
356 struct sunxi_nand_rb
*rb
;
357 unsigned long timeo
= (sunxi_nand
->nand
.state
== FL_ERASING
? 400 : 20);
360 if (sunxi_nand
->selected
< 0)
363 rb
= &sunxi_nand
->sels
[sunxi_nand
->selected
].rb
;
367 ret
= !!(readl(nfc
->regs
+ NFC_REG_ST
) &
368 NFC_RB_STATE(rb
->info
.nativeid
));
372 sunxi_nfc_wait_int(nfc
, NFC_RB_B2R
, timeo
);
373 ret
= !!(readl(nfc
->regs
+ NFC_REG_ST
) &
374 NFC_RB_STATE(rb
->info
.nativeid
));
377 ret
= gpio_get_value(rb
->info
.gpio
);
382 dev_err(nfc
->dev
, "cannot check R/B NAND status!\n");
389 static void sunxi_nfc_select_chip(struct mtd_info
*mtd
, int chip
)
391 struct nand_chip
*nand
= mtd_to_nand(mtd
);
392 struct sunxi_nand_chip
*sunxi_nand
= to_sunxi_nand(nand
);
393 struct sunxi_nfc
*nfc
= to_sunxi_nfc(sunxi_nand
->nand
.controller
);
394 struct sunxi_nand_chip_sel
*sel
;
397 if (chip
> 0 && chip
>= sunxi_nand
->nsels
)
400 if (chip
== sunxi_nand
->selected
)
403 ctl
= readl(nfc
->regs
+ NFC_REG_CTL
) &
404 ~(NFC_PAGE_SHIFT_MSK
| NFC_CE_SEL_MSK
| NFC_RB_SEL_MSK
| NFC_EN
);
407 sel
= &sunxi_nand
->sels
[chip
];
409 ctl
|= NFC_CE_SEL(sel
->cs
) | NFC_EN
|
410 NFC_PAGE_SHIFT(nand
->page_shift
- 10);
411 if (sel
->rb
.type
== RB_NONE
) {
412 nand
->dev_ready
= NULL
;
414 nand
->dev_ready
= sunxi_nfc_dev_ready
;
415 if (sel
->rb
.type
== RB_NATIVE
)
416 ctl
|= NFC_RB_SEL(sel
->rb
.info
.nativeid
);
419 writel(mtd
->writesize
, nfc
->regs
+ NFC_REG_SPARE_AREA
);
421 if (nfc
->clk_rate
!= sunxi_nand
->clk_rate
) {
422 clk_set_rate(nfc
->mod_clk
, sunxi_nand
->clk_rate
);
423 nfc
->clk_rate
= sunxi_nand
->clk_rate
;
427 writel(sunxi_nand
->timing_ctl
, nfc
->regs
+ NFC_REG_TIMING_CTL
);
428 writel(sunxi_nand
->timing_cfg
, nfc
->regs
+ NFC_REG_TIMING_CFG
);
429 writel(ctl
, nfc
->regs
+ NFC_REG_CTL
);
431 sunxi_nand
->selected
= chip
;
434 static void sunxi_nfc_read_buf(struct mtd_info
*mtd
, uint8_t *buf
, int len
)
436 struct nand_chip
*nand
= mtd_to_nand(mtd
);
437 struct sunxi_nand_chip
*sunxi_nand
= to_sunxi_nand(nand
);
438 struct sunxi_nfc
*nfc
= to_sunxi_nfc(sunxi_nand
->nand
.controller
);
445 cnt
= min(len
- offs
, NFC_SRAM_SIZE
);
447 ret
= sunxi_nfc_wait_cmd_fifo_empty(nfc
);
451 writel(cnt
, nfc
->regs
+ NFC_REG_CNT
);
452 tmp
= NFC_DATA_TRANS
| NFC_DATA_SWAP_METHOD
;
453 writel(tmp
, nfc
->regs
+ NFC_REG_CMD
);
455 ret
= sunxi_nfc_wait_int(nfc
, NFC_CMD_INT_FLAG
, 0);
460 memcpy_fromio(buf
+ offs
, nfc
->regs
+ NFC_RAM0_BASE
,
466 static void sunxi_nfc_write_buf(struct mtd_info
*mtd
, const uint8_t *buf
,
469 struct nand_chip
*nand
= mtd_to_nand(mtd
);
470 struct sunxi_nand_chip
*sunxi_nand
= to_sunxi_nand(nand
);
471 struct sunxi_nfc
*nfc
= to_sunxi_nfc(sunxi_nand
->nand
.controller
);
478 cnt
= min(len
- offs
, NFC_SRAM_SIZE
);
480 ret
= sunxi_nfc_wait_cmd_fifo_empty(nfc
);
484 writel(cnt
, nfc
->regs
+ NFC_REG_CNT
);
485 memcpy_toio(nfc
->regs
+ NFC_RAM0_BASE
, buf
+ offs
, cnt
);
486 tmp
= NFC_DATA_TRANS
| NFC_DATA_SWAP_METHOD
|
488 writel(tmp
, nfc
->regs
+ NFC_REG_CMD
);
490 ret
= sunxi_nfc_wait_int(nfc
, NFC_CMD_INT_FLAG
, 0);
498 static uint8_t sunxi_nfc_read_byte(struct mtd_info
*mtd
)
502 sunxi_nfc_read_buf(mtd
, &ret
, 1);
507 static void sunxi_nfc_cmd_ctrl(struct mtd_info
*mtd
, int dat
,
510 struct nand_chip
*nand
= mtd_to_nand(mtd
);
511 struct sunxi_nand_chip
*sunxi_nand
= to_sunxi_nand(nand
);
512 struct sunxi_nfc
*nfc
= to_sunxi_nfc(sunxi_nand
->nand
.controller
);
516 ret
= sunxi_nfc_wait_cmd_fifo_empty(nfc
);
520 if (ctrl
& NAND_CTRL_CHANGE
) {
521 tmp
= readl(nfc
->regs
+ NFC_REG_CTL
);
526 writel(tmp
, nfc
->regs
+ NFC_REG_CTL
);
529 if (dat
== NAND_CMD_NONE
)
532 if (ctrl
& NAND_CLE
) {
533 writel(NFC_SEND_CMD1
| dat
, nfc
->regs
+ NFC_REG_CMD
);
535 writel(dat
, nfc
->regs
+ NFC_REG_ADDR_LOW
);
536 writel(NFC_SEND_ADR
, nfc
->regs
+ NFC_REG_CMD
);
539 sunxi_nfc_wait_int(nfc
, NFC_CMD_INT_FLAG
, 0);
542 /* These seed values have been extracted from Allwinner's BSP */
543 static const u16 sunxi_nfc_randomizer_page_seeds
[] = {
544 0x2b75, 0x0bd0, 0x5ca3, 0x62d1, 0x1c93, 0x07e9, 0x2162, 0x3a72,
545 0x0d67, 0x67f9, 0x1be7, 0x077d, 0x032f, 0x0dac, 0x2716, 0x2436,
546 0x7922, 0x1510, 0x3860, 0x5287, 0x480f, 0x4252, 0x1789, 0x5a2d,
547 0x2a49, 0x5e10, 0x437f, 0x4b4e, 0x2f45, 0x216e, 0x5cb7, 0x7130,
548 0x2a3f, 0x60e4, 0x4dc9, 0x0ef0, 0x0f52, 0x1bb9, 0x6211, 0x7a56,
549 0x226d, 0x4ea7, 0x6f36, 0x3692, 0x38bf, 0x0c62, 0x05eb, 0x4c55,
550 0x60f4, 0x728c, 0x3b6f, 0x2037, 0x7f69, 0x0936, 0x651a, 0x4ceb,
551 0x6218, 0x79f3, 0x383f, 0x18d9, 0x4f05, 0x5c82, 0x2912, 0x6f17,
552 0x6856, 0x5938, 0x1007, 0x61ab, 0x3e7f, 0x57c2, 0x542f, 0x4f62,
553 0x7454, 0x2eac, 0x7739, 0x42d4, 0x2f90, 0x435a, 0x2e52, 0x2064,
554 0x637c, 0x66ad, 0x2c90, 0x0bad, 0x759c, 0x0029, 0x0986, 0x7126,
555 0x1ca7, 0x1605, 0x386a, 0x27f5, 0x1380, 0x6d75, 0x24c3, 0x0f8e,
556 0x2b7a, 0x1418, 0x1fd1, 0x7dc1, 0x2d8e, 0x43af, 0x2267, 0x7da3,
557 0x4e3d, 0x1338, 0x50db, 0x454d, 0x764d, 0x40a3, 0x42e6, 0x262b,
558 0x2d2e, 0x1aea, 0x2e17, 0x173d, 0x3a6e, 0x71bf, 0x25f9, 0x0a5d,
559 0x7c57, 0x0fbe, 0x46ce, 0x4939, 0x6b17, 0x37bb, 0x3e91, 0x76db,
563 * sunxi_nfc_randomizer_ecc512_seeds and sunxi_nfc_randomizer_ecc1024_seeds
564 * have been generated using
565 * sunxi_nfc_randomizer_step(seed, (step_size * 8) + 15), which is what
566 * the randomizer engine does internally before de/scrambling OOB data.
568 * Those tables are statically defined to avoid calculating randomizer state
571 static const u16 sunxi_nfc_randomizer_ecc512_seeds
[] = {
572 0x3346, 0x367f, 0x1f18, 0x769a, 0x4f64, 0x068c, 0x2ef1, 0x6b64,
573 0x28a9, 0x15d7, 0x30f8, 0x3659, 0x53db, 0x7c5f, 0x71d4, 0x4409,
574 0x26eb, 0x03cc, 0x655d, 0x47d4, 0x4daa, 0x0877, 0x712d, 0x3617,
575 0x3264, 0x49aa, 0x7f9e, 0x588e, 0x4fbc, 0x7176, 0x7f91, 0x6c6d,
576 0x4b95, 0x5fb7, 0x3844, 0x4037, 0x0184, 0x081b, 0x0ee8, 0x5b91,
577 0x293d, 0x1f71, 0x0e6f, 0x402b, 0x5122, 0x1e52, 0x22be, 0x3d2d,
578 0x75bc, 0x7c60, 0x6291, 0x1a2f, 0x61d4, 0x74aa, 0x4140, 0x29ab,
579 0x472d, 0x2852, 0x017e, 0x15e8, 0x5ec2, 0x17cf, 0x7d0f, 0x06b8,
580 0x117a, 0x6b94, 0x789b, 0x3126, 0x6ac5, 0x5be7, 0x150f, 0x51f8,
581 0x7889, 0x0aa5, 0x663d, 0x77e8, 0x0b87, 0x3dcb, 0x360d, 0x218b,
582 0x512f, 0x7dc9, 0x6a4d, 0x630a, 0x3547, 0x1dd2, 0x5aea, 0x69a5,
583 0x7bfa, 0x5e4f, 0x1519, 0x6430, 0x3a0e, 0x5eb3, 0x5425, 0x0c7a,
584 0x5540, 0x3670, 0x63c1, 0x31e9, 0x5a39, 0x2de7, 0x5979, 0x2891,
585 0x1562, 0x014b, 0x5b05, 0x2756, 0x5a34, 0x13aa, 0x6cb5, 0x2c36,
586 0x5e72, 0x1306, 0x0861, 0x15ef, 0x1ee8, 0x5a37, 0x7ac4, 0x45dd,
587 0x44c4, 0x7266, 0x2f41, 0x3ccc, 0x045e, 0x7d40, 0x7c66, 0x0fa0,
590 static const u16 sunxi_nfc_randomizer_ecc1024_seeds
[] = {
591 0x2cf5, 0x35f1, 0x63a4, 0x5274, 0x2bd2, 0x778b, 0x7285, 0x32b6,
592 0x6a5c, 0x70d6, 0x757d, 0x6769, 0x5375, 0x1e81, 0x0cf3, 0x3982,
593 0x6787, 0x042a, 0x6c49, 0x1925, 0x56a8, 0x40a9, 0x063e, 0x7bd9,
594 0x4dbf, 0x55ec, 0x672e, 0x7334, 0x5185, 0x4d00, 0x232a, 0x7e07,
595 0x445d, 0x6b92, 0x528f, 0x4255, 0x53ba, 0x7d82, 0x2a2e, 0x3a4e,
596 0x75eb, 0x450c, 0x6844, 0x1b5d, 0x581a, 0x4cc6, 0x0379, 0x37b2,
597 0x419f, 0x0e92, 0x6b27, 0x5624, 0x01e3, 0x07c1, 0x44a5, 0x130c,
598 0x13e8, 0x5910, 0x0876, 0x60c5, 0x54e3, 0x5b7f, 0x2269, 0x509f,
599 0x7665, 0x36fd, 0x3e9a, 0x0579, 0x6295, 0x14ef, 0x0a81, 0x1bcc,
600 0x4b16, 0x64db, 0x0514, 0x4f07, 0x0591, 0x3576, 0x6853, 0x0d9e,
601 0x259f, 0x38b7, 0x64fb, 0x3094, 0x4693, 0x6ddd, 0x29bb, 0x0bc8,
602 0x3f47, 0x490e, 0x0c0e, 0x7933, 0x3c9e, 0x5840, 0x398d, 0x3e68,
603 0x4af1, 0x71f5, 0x57cf, 0x1121, 0x64eb, 0x3579, 0x15ac, 0x584d,
604 0x5f2a, 0x47e2, 0x6528, 0x6eac, 0x196e, 0x6b96, 0x0450, 0x0179,
605 0x609c, 0x06e1, 0x4626, 0x42c7, 0x273e, 0x486f, 0x0705, 0x1601,
606 0x145b, 0x407e, 0x062b, 0x57a5, 0x53f9, 0x5659, 0x4410, 0x3ccd,
609 static u16
sunxi_nfc_randomizer_step(u16 state
, int count
)
614 * This loop is just a simple implementation of a Fibonacci LFSR using
615 * the x16 + x15 + 1 polynomial.
618 state
= ((state
>> 1) |
619 (((state
^ (state
>> 1)) & 1) << 14)) & 0x7fff;
624 static u16
sunxi_nfc_randomizer_state(struct mtd_info
*mtd
, int page
, bool ecc
)
626 const u16
*seeds
= sunxi_nfc_randomizer_page_seeds
;
627 int mod
= mtd_div_by_ws(mtd
->erasesize
, mtd
);
629 if (mod
> ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds
))
630 mod
= ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds
);
633 if (mtd
->ecc_step_size
== 512)
634 seeds
= sunxi_nfc_randomizer_ecc512_seeds
;
636 seeds
= sunxi_nfc_randomizer_ecc1024_seeds
;
639 return seeds
[page
% mod
];
642 static void sunxi_nfc_randomizer_config(struct mtd_info
*mtd
,
645 struct nand_chip
*nand
= mtd_to_nand(mtd
);
646 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
647 u32 ecc_ctl
= readl(nfc
->regs
+ NFC_REG_ECC_CTL
);
650 if (!(nand
->options
& NAND_NEED_SCRAMBLING
))
653 ecc_ctl
= readl(nfc
->regs
+ NFC_REG_ECC_CTL
);
654 state
= sunxi_nfc_randomizer_state(mtd
, page
, ecc
);
655 ecc_ctl
= readl(nfc
->regs
+ NFC_REG_ECC_CTL
) & ~NFC_RANDOM_SEED_MSK
;
656 writel(ecc_ctl
| NFC_RANDOM_SEED(state
), nfc
->regs
+ NFC_REG_ECC_CTL
);
659 static void sunxi_nfc_randomizer_enable(struct mtd_info
*mtd
)
661 struct nand_chip
*nand
= mtd_to_nand(mtd
);
662 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
664 if (!(nand
->options
& NAND_NEED_SCRAMBLING
))
667 writel(readl(nfc
->regs
+ NFC_REG_ECC_CTL
) | NFC_RANDOM_EN
,
668 nfc
->regs
+ NFC_REG_ECC_CTL
);
671 static void sunxi_nfc_randomizer_disable(struct mtd_info
*mtd
)
673 struct nand_chip
*nand
= mtd_to_nand(mtd
);
674 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
676 if (!(nand
->options
& NAND_NEED_SCRAMBLING
))
679 writel(readl(nfc
->regs
+ NFC_REG_ECC_CTL
) & ~NFC_RANDOM_EN
,
680 nfc
->regs
+ NFC_REG_ECC_CTL
);
683 static void sunxi_nfc_randomize_bbm(struct mtd_info
*mtd
, int page
, u8
*bbm
)
685 u16 state
= sunxi_nfc_randomizer_state(mtd
, page
, true);
688 bbm
[1] ^= sunxi_nfc_randomizer_step(state
, 8);
691 static void sunxi_nfc_randomizer_write_buf(struct mtd_info
*mtd
,
692 const uint8_t *buf
, int len
,
695 sunxi_nfc_randomizer_config(mtd
, page
, ecc
);
696 sunxi_nfc_randomizer_enable(mtd
);
697 sunxi_nfc_write_buf(mtd
, buf
, len
);
698 sunxi_nfc_randomizer_disable(mtd
);
701 static void sunxi_nfc_randomizer_read_buf(struct mtd_info
*mtd
, uint8_t *buf
,
702 int len
, bool ecc
, int page
)
704 sunxi_nfc_randomizer_config(mtd
, page
, ecc
);
705 sunxi_nfc_randomizer_enable(mtd
);
706 sunxi_nfc_read_buf(mtd
, buf
, len
);
707 sunxi_nfc_randomizer_disable(mtd
);
710 static void sunxi_nfc_hw_ecc_enable(struct mtd_info
*mtd
)
712 struct nand_chip
*nand
= mtd_to_nand(mtd
);
713 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
714 struct sunxi_nand_hw_ecc
*data
= nand
->ecc
.priv
;
717 ecc_ctl
= readl(nfc
->regs
+ NFC_REG_ECC_CTL
);
718 ecc_ctl
&= ~(NFC_ECC_MODE_MSK
| NFC_ECC_PIPELINE
|
719 NFC_ECC_BLOCK_SIZE_MSK
);
720 ecc_ctl
|= NFC_ECC_EN
| NFC_ECC_MODE(data
->mode
) | NFC_ECC_EXCEPTION
;
722 writel(ecc_ctl
, nfc
->regs
+ NFC_REG_ECC_CTL
);
725 static void sunxi_nfc_hw_ecc_disable(struct mtd_info
*mtd
)
727 struct nand_chip
*nand
= mtd_to_nand(mtd
);
728 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
730 writel(readl(nfc
->regs
+ NFC_REG_ECC_CTL
) & ~NFC_ECC_EN
,
731 nfc
->regs
+ NFC_REG_ECC_CTL
);
734 static inline void sunxi_nfc_user_data_to_buf(u32 user_data
, u8
*buf
)
737 buf
[1] = user_data
>> 8;
738 buf
[2] = user_data
>> 16;
739 buf
[3] = user_data
>> 24;
742 static int sunxi_nfc_hw_ecc_read_chunk(struct mtd_info
*mtd
,
743 u8
*data
, int data_off
,
744 u8
*oob
, int oob_off
,
746 unsigned int *max_bitflips
,
749 struct nand_chip
*nand
= mtd_to_nand(mtd
);
750 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
751 struct nand_ecc_ctrl
*ecc
= &nand
->ecc
;
756 if (*cur_off
!= data_off
)
757 nand
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, data_off
, -1);
759 sunxi_nfc_randomizer_read_buf(mtd
, NULL
, ecc
->size
, false, page
);
761 if (data_off
+ ecc
->size
!= oob_off
)
762 nand
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, oob_off
, -1);
764 ret
= sunxi_nfc_wait_cmd_fifo_empty(nfc
);
768 sunxi_nfc_randomizer_enable(mtd
);
769 writel(NFC_DATA_TRANS
| NFC_DATA_SWAP_METHOD
| NFC_ECC_OP
,
770 nfc
->regs
+ NFC_REG_CMD
);
772 ret
= sunxi_nfc_wait_int(nfc
, NFC_CMD_INT_FLAG
, 0);
773 sunxi_nfc_randomizer_disable(mtd
);
777 *cur_off
= oob_off
+ ecc
->bytes
+ 4;
779 status
= readl(nfc
->regs
+ NFC_REG_ECC_ST
);
780 if (status
& NFC_ECC_PAT_FOUND(0)) {
783 if (unlikely(!(readl(nfc
->regs
+ NFC_REG_PAT_ID
) & 0x1)))
786 memset(data
, pattern
, ecc
->size
);
787 memset(oob
, pattern
, ecc
->bytes
+ 4);
792 ret
= NFC_ECC_ERR_CNT(0, readl(nfc
->regs
+ NFC_REG_ECC_ERR_CNT(0)));
794 memcpy_fromio(data
, nfc
->regs
+ NFC_RAM0_BASE
, ecc
->size
);
796 nand
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, oob_off
, -1);
797 sunxi_nfc_randomizer_read_buf(mtd
, oob
, ecc
->bytes
+ 4, true, page
);
799 if (status
& NFC_ECC_ERR(0)) {
801 * Re-read the data with the randomizer disabled to identify
802 * bitflips in erased pages.
804 if (nand
->options
& NAND_NEED_SCRAMBLING
) {
805 nand
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, data_off
, -1);
806 nand
->read_buf(mtd
, data
, ecc
->size
);
807 nand
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, oob_off
, -1);
808 nand
->read_buf(mtd
, oob
, ecc
->bytes
+ 4);
811 ret
= nand_check_erased_ecc_chunk(data
, ecc
->size
,
813 NULL
, 0, ecc
->strength
);
818 * The engine protects 4 bytes of OOB data per chunk.
819 * Retrieve the corrected OOB bytes.
821 sunxi_nfc_user_data_to_buf(readl(nfc
->regs
+ NFC_REG_USER_DATA(0)),
824 /* De-randomize the Bad Block Marker. */
825 if (bbm
&& nand
->options
& NAND_NEED_SCRAMBLING
)
826 sunxi_nfc_randomize_bbm(mtd
, page
, oob
);
830 mtd
->ecc_stats
.failed
++;
832 mtd
->ecc_stats
.corrected
+= ret
;
833 *max_bitflips
= max_t(unsigned int, *max_bitflips
, ret
);
839 static void sunxi_nfc_hw_ecc_read_extra_oob(struct mtd_info
*mtd
,
840 u8
*oob
, int *cur_off
,
841 bool randomize
, int page
)
843 struct nand_chip
*nand
= mtd_to_nand(mtd
);
844 struct nand_ecc_ctrl
*ecc
= &nand
->ecc
;
845 int offset
= ((ecc
->bytes
+ 4) * ecc
->steps
);
846 int len
= mtd
->oobsize
- offset
;
851 if (*cur_off
!= offset
)
852 nand
->cmdfunc(mtd
, NAND_CMD_RNDOUT
,
853 offset
+ mtd
->writesize
, -1);
856 sunxi_nfc_read_buf(mtd
, oob
+ offset
, len
);
858 sunxi_nfc_randomizer_read_buf(mtd
, oob
+ offset
, len
,
861 *cur_off
= mtd
->oobsize
+ mtd
->writesize
;
864 static inline u32
sunxi_nfc_buf_to_user_data(const u8
*buf
)
866 return buf
[0] | (buf
[1] << 8) | (buf
[2] << 16) | (buf
[3] << 24);
869 static int sunxi_nfc_hw_ecc_write_chunk(struct mtd_info
*mtd
,
870 const u8
*data
, int data_off
,
871 const u8
*oob
, int oob_off
,
872 int *cur_off
, bool bbm
,
875 struct nand_chip
*nand
= mtd_to_nand(mtd
);
876 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
877 struct nand_ecc_ctrl
*ecc
= &nand
->ecc
;
880 if (data_off
!= *cur_off
)
881 nand
->cmdfunc(mtd
, NAND_CMD_RNDIN
, data_off
, -1);
883 sunxi_nfc_randomizer_write_buf(mtd
, data
, ecc
->size
, false, page
);
885 /* Fill OOB data in */
886 if ((nand
->options
& NAND_NEED_SCRAMBLING
) && bbm
) {
889 memcpy(user_data
, oob
, 4);
890 sunxi_nfc_randomize_bbm(mtd
, page
, user_data
);
891 writel(sunxi_nfc_buf_to_user_data(user_data
),
892 nfc
->regs
+ NFC_REG_USER_DATA(0));
894 writel(sunxi_nfc_buf_to_user_data(oob
),
895 nfc
->regs
+ NFC_REG_USER_DATA(0));
898 if (data_off
+ ecc
->size
!= oob_off
)
899 nand
->cmdfunc(mtd
, NAND_CMD_RNDIN
, oob_off
, -1);
901 ret
= sunxi_nfc_wait_cmd_fifo_empty(nfc
);
905 sunxi_nfc_randomizer_enable(mtd
);
906 writel(NFC_DATA_TRANS
| NFC_DATA_SWAP_METHOD
|
907 NFC_ACCESS_DIR
| NFC_ECC_OP
,
908 nfc
->regs
+ NFC_REG_CMD
);
910 ret
= sunxi_nfc_wait_int(nfc
, NFC_CMD_INT_FLAG
, 0);
911 sunxi_nfc_randomizer_disable(mtd
);
915 *cur_off
= oob_off
+ ecc
->bytes
+ 4;
920 static void sunxi_nfc_hw_ecc_write_extra_oob(struct mtd_info
*mtd
,
921 u8
*oob
, int *cur_off
,
924 struct nand_chip
*nand
= mtd_to_nand(mtd
);
925 struct nand_ecc_ctrl
*ecc
= &nand
->ecc
;
926 int offset
= ((ecc
->bytes
+ 4) * ecc
->steps
);
927 int len
= mtd
->oobsize
- offset
;
932 if (*cur_off
!= offset
)
933 nand
->cmdfunc(mtd
, NAND_CMD_RNDIN
,
934 offset
+ mtd
->writesize
, -1);
936 sunxi_nfc_randomizer_write_buf(mtd
, oob
+ offset
, len
, false, page
);
938 *cur_off
= mtd
->oobsize
+ mtd
->writesize
;
941 static int sunxi_nfc_hw_ecc_read_page(struct mtd_info
*mtd
,
942 struct nand_chip
*chip
, uint8_t *buf
,
943 int oob_required
, int page
)
945 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
946 unsigned int max_bitflips
= 0;
947 int ret
, i
, cur_off
= 0;
948 bool raw_mode
= false;
950 sunxi_nfc_hw_ecc_enable(mtd
);
952 for (i
= 0; i
< ecc
->steps
; i
++) {
953 int data_off
= i
* ecc
->size
;
954 int oob_off
= i
* (ecc
->bytes
+ 4);
955 u8
*data
= buf
+ data_off
;
956 u8
*oob
= chip
->oob_poi
+ oob_off
;
958 ret
= sunxi_nfc_hw_ecc_read_chunk(mtd
, data
, data_off
, oob
,
959 oob_off
+ mtd
->writesize
,
960 &cur_off
, &max_bitflips
,
969 sunxi_nfc_hw_ecc_read_extra_oob(mtd
, chip
->oob_poi
, &cur_off
,
972 sunxi_nfc_hw_ecc_disable(mtd
);
977 static int sunxi_nfc_hw_ecc_write_page(struct mtd_info
*mtd
,
978 struct nand_chip
*chip
,
979 const uint8_t *buf
, int oob_required
,
982 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
983 int ret
, i
, cur_off
= 0;
985 sunxi_nfc_hw_ecc_enable(mtd
);
987 for (i
= 0; i
< ecc
->steps
; i
++) {
988 int data_off
= i
* ecc
->size
;
989 int oob_off
= i
* (ecc
->bytes
+ 4);
990 const u8
*data
= buf
+ data_off
;
991 const u8
*oob
= chip
->oob_poi
+ oob_off
;
993 ret
= sunxi_nfc_hw_ecc_write_chunk(mtd
, data
, data_off
, oob
,
994 oob_off
+ mtd
->writesize
,
1000 if (oob_required
|| (chip
->options
& NAND_NEED_SCRAMBLING
))
1001 sunxi_nfc_hw_ecc_write_extra_oob(mtd
, chip
->oob_poi
,
1004 sunxi_nfc_hw_ecc_disable(mtd
);
1009 static int sunxi_nfc_hw_syndrome_ecc_read_page(struct mtd_info
*mtd
,
1010 struct nand_chip
*chip
,
1011 uint8_t *buf
, int oob_required
,
1014 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1015 unsigned int max_bitflips
= 0;
1016 int ret
, i
, cur_off
= 0;
1017 bool raw_mode
= false;
1019 sunxi_nfc_hw_ecc_enable(mtd
);
1021 for (i
= 0; i
< ecc
->steps
; i
++) {
1022 int data_off
= i
* (ecc
->size
+ ecc
->bytes
+ 4);
1023 int oob_off
= data_off
+ ecc
->size
;
1024 u8
*data
= buf
+ (i
* ecc
->size
);
1025 u8
*oob
= chip
->oob_poi
+ (i
* (ecc
->bytes
+ 4));
1027 ret
= sunxi_nfc_hw_ecc_read_chunk(mtd
, data
, data_off
, oob
,
1029 &max_bitflips
, !i
, page
);
1037 sunxi_nfc_hw_ecc_read_extra_oob(mtd
, chip
->oob_poi
, &cur_off
,
1040 sunxi_nfc_hw_ecc_disable(mtd
);
1042 return max_bitflips
;
1045 static int sunxi_nfc_hw_syndrome_ecc_write_page(struct mtd_info
*mtd
,
1046 struct nand_chip
*chip
,
1048 int oob_required
, int page
)
1050 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1051 int ret
, i
, cur_off
= 0;
1053 sunxi_nfc_hw_ecc_enable(mtd
);
1055 for (i
= 0; i
< ecc
->steps
; i
++) {
1056 int data_off
= i
* (ecc
->size
+ ecc
->bytes
+ 4);
1057 int oob_off
= data_off
+ ecc
->size
;
1058 const u8
*data
= buf
+ (i
* ecc
->size
);
1059 const u8
*oob
= chip
->oob_poi
+ (i
* (ecc
->bytes
+ 4));
1061 ret
= sunxi_nfc_hw_ecc_write_chunk(mtd
, data
, data_off
,
1062 oob
, oob_off
, &cur_off
,
1068 if (oob_required
|| (chip
->options
& NAND_NEED_SCRAMBLING
))
1069 sunxi_nfc_hw_ecc_write_extra_oob(mtd
, chip
->oob_poi
,
1072 sunxi_nfc_hw_ecc_disable(mtd
);
1077 static const s32 tWB_lut
[] = {6, 12, 16, 20};
1078 static const s32 tRHW_lut
[] = {4, 8, 12, 20};
1080 static int _sunxi_nand_lookup_timing(const s32
*lut
, int lut_size
, u32 duration
,
1083 u32 clk_cycles
= DIV_ROUND_UP(duration
, clk_period
);
1086 for (i
= 0; i
< lut_size
; i
++) {
1087 if (clk_cycles
<= lut
[i
])
1095 #define sunxi_nand_lookup_timing(l, p, c) \
1096 _sunxi_nand_lookup_timing(l, ARRAY_SIZE(l), p, c)
1098 static int sunxi_nand_chip_set_timings(struct sunxi_nand_chip
*chip
,
1099 const struct nand_sdr_timings
*timings
)
1101 struct sunxi_nfc
*nfc
= to_sunxi_nfc(chip
->nand
.controller
);
1102 u32 min_clk_period
= 0;
1103 s32 tWB
, tADL
, tWHR
, tRHW
, tCAD
;
1106 if (timings
->tCLS_min
> min_clk_period
)
1107 min_clk_period
= timings
->tCLS_min
;
1110 if (timings
->tCLH_min
> min_clk_period
)
1111 min_clk_period
= timings
->tCLH_min
;
1114 if (timings
->tCS_min
> min_clk_period
)
1115 min_clk_period
= timings
->tCS_min
;
1118 if (timings
->tCH_min
> min_clk_period
)
1119 min_clk_period
= timings
->tCH_min
;
1122 if (timings
->tWP_min
> min_clk_period
)
1123 min_clk_period
= timings
->tWP_min
;
1126 if (timings
->tWH_min
> min_clk_period
)
1127 min_clk_period
= timings
->tWH_min
;
1130 if (timings
->tALS_min
> min_clk_period
)
1131 min_clk_period
= timings
->tALS_min
;
1134 if (timings
->tDS_min
> min_clk_period
)
1135 min_clk_period
= timings
->tDS_min
;
1138 if (timings
->tDH_min
> min_clk_period
)
1139 min_clk_period
= timings
->tDH_min
;
1142 if (timings
->tRR_min
> (min_clk_period
* 3))
1143 min_clk_period
= DIV_ROUND_UP(timings
->tRR_min
, 3);
1146 if (timings
->tALH_min
> min_clk_period
)
1147 min_clk_period
= timings
->tALH_min
;
1150 if (timings
->tRP_min
> min_clk_period
)
1151 min_clk_period
= timings
->tRP_min
;
1154 if (timings
->tREH_min
> min_clk_period
)
1155 min_clk_period
= timings
->tREH_min
;
1158 if (timings
->tRC_min
> (min_clk_period
* 2))
1159 min_clk_period
= DIV_ROUND_UP(timings
->tRC_min
, 2);
1162 if (timings
->tWC_min
> (min_clk_period
* 2))
1163 min_clk_period
= DIV_ROUND_UP(timings
->tWC_min
, 2);
1165 /* T16 - T19 + tCAD */
1166 tWB
= sunxi_nand_lookup_timing(tWB_lut
, timings
->tWB_max
,
1169 dev_err(nfc
->dev
, "unsupported tWB\n");
1173 tADL
= DIV_ROUND_UP(timings
->tADL_min
, min_clk_period
) >> 3;
1175 dev_err(nfc
->dev
, "unsupported tADL\n");
1179 tWHR
= DIV_ROUND_UP(timings
->tWHR_min
, min_clk_period
) >> 3;
1181 dev_err(nfc
->dev
, "unsupported tWHR\n");
1185 tRHW
= sunxi_nand_lookup_timing(tRHW_lut
, timings
->tRHW_min
,
1188 dev_err(nfc
->dev
, "unsupported tRHW\n");
1193 * TODO: according to ONFI specs this value only applies for DDR NAND,
1194 * but Allwinner seems to set this to 0x7. Mimic them for now.
1198 /* TODO: A83 has some more bits for CDQSS, CS, CLHZ, CCS, WC */
1199 chip
->timing_cfg
= NFC_TIMING_CFG(tWB
, tADL
, tWHR
, tRHW
, tCAD
);
1202 * ONFI specification 3.1, paragraph 4.15.2 dictates that EDO data
1203 * output cycle timings shall be used if the host drives tRC less than
1206 chip
->timing_ctl
= (timings
->tRC_min
< 30000) ? NFC_TIMING_CTL_EDO
: 0;
1208 /* Convert min_clk_period from picoseconds to nanoseconds */
1209 min_clk_period
= DIV_ROUND_UP(min_clk_period
, 1000);
1212 * Convert min_clk_period into a clk frequency, then get the
1213 * appropriate rate for the NAND controller IP given this formula
1214 * (specified in the datasheet):
1215 * nand clk_rate = 2 * min_clk_rate
1217 chip
->clk_rate
= (2 * NSEC_PER_SEC
) / min_clk_period
;
1222 static int sunxi_nand_chip_init_timings(struct sunxi_nand_chip
*chip
,
1223 struct device_node
*np
)
1225 struct mtd_info
*mtd
= nand_to_mtd(&chip
->nand
);
1226 const struct nand_sdr_timings
*timings
;
1230 mode
= onfi_get_async_timing_mode(&chip
->nand
);
1231 if (mode
== ONFI_TIMING_MODE_UNKNOWN
) {
1232 mode
= chip
->nand
.onfi_timing_mode_default
;
1234 uint8_t feature
[ONFI_SUBFEATURE_PARAM_LEN
] = {};
1237 mode
= fls(mode
) - 1;
1242 for (i
= 0; i
< chip
->nsels
; i
++) {
1243 chip
->nand
.select_chip(mtd
, i
);
1244 ret
= chip
->nand
.onfi_set_features(mtd
, &chip
->nand
,
1245 ONFI_FEATURE_ADDR_TIMING_MODE
,
1247 chip
->nand
.select_chip(mtd
, -1);
1253 timings
= onfi_async_timing_mode_to_sdr_timings(mode
);
1254 if (IS_ERR(timings
))
1255 return PTR_ERR(timings
);
1257 return sunxi_nand_chip_set_timings(chip
, timings
);
1260 static int sunxi_nand_hw_common_ecc_ctrl_init(struct mtd_info
*mtd
,
1261 struct nand_ecc_ctrl
*ecc
,
1262 struct device_node
*np
)
1264 static const u8 strengths
[] = { 16, 24, 28, 32, 40, 48, 56, 60, 64 };
1265 struct nand_chip
*nand
= mtd_to_nand(mtd
);
1266 struct sunxi_nand_chip
*sunxi_nand
= to_sunxi_nand(nand
);
1267 struct sunxi_nfc
*nfc
= to_sunxi_nfc(sunxi_nand
->nand
.controller
);
1268 struct sunxi_nand_hw_ecc
*data
;
1269 struct nand_ecclayout
*layout
;
1274 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1278 /* Add ECC info retrieval from DT */
1279 for (i
= 0; i
< ARRAY_SIZE(strengths
); i
++) {
1280 if (ecc
->strength
<= strengths
[i
])
1284 if (i
>= ARRAY_SIZE(strengths
)) {
1285 dev_err(nfc
->dev
, "unsupported strength\n");
1292 /* HW ECC always request ECC bytes for 1024 bytes blocks */
1293 ecc
->bytes
= DIV_ROUND_UP(ecc
->strength
* fls(8 * 1024), 8);
1295 /* HW ECC always work with even numbers of ECC bytes */
1296 ecc
->bytes
= ALIGN(ecc
->bytes
, 2);
1298 layout
= &data
->layout
;
1299 nsectors
= mtd
->writesize
/ ecc
->size
;
1301 if (mtd
->oobsize
< ((ecc
->bytes
+ 4) * nsectors
)) {
1306 layout
->eccbytes
= (ecc
->bytes
* nsectors
);
1308 ecc
->layout
= layout
;
1319 static void sunxi_nand_hw_common_ecc_ctrl_cleanup(struct nand_ecc_ctrl
*ecc
)
1324 static int sunxi_nand_hw_ecc_ctrl_init(struct mtd_info
*mtd
,
1325 struct nand_ecc_ctrl
*ecc
,
1326 struct device_node
*np
)
1328 struct nand_ecclayout
*layout
;
1333 ret
= sunxi_nand_hw_common_ecc_ctrl_init(mtd
, ecc
, np
);
1337 ecc
->read_page
= sunxi_nfc_hw_ecc_read_page
;
1338 ecc
->write_page
= sunxi_nfc_hw_ecc_write_page
;
1339 layout
= ecc
->layout
;
1340 nsectors
= mtd
->writesize
/ ecc
->size
;
1342 for (i
= 0; i
< nsectors
; i
++) {
1344 layout
->oobfree
[i
].offset
=
1345 layout
->oobfree
[i
- 1].offset
+
1346 layout
->oobfree
[i
- 1].length
+
1348 layout
->oobfree
[i
].length
= 4;
1351 * The first 2 bytes are used for BB markers, hence we
1352 * only have 2 bytes available in the first user data
1355 layout
->oobfree
[i
].length
= 2;
1356 layout
->oobfree
[i
].offset
= 2;
1359 for (j
= 0; j
< ecc
->bytes
; j
++)
1360 layout
->eccpos
[(ecc
->bytes
* i
) + j
] =
1361 layout
->oobfree
[i
].offset
+
1362 layout
->oobfree
[i
].length
+ j
;
1365 if (mtd
->oobsize
> (ecc
->bytes
+ 4) * nsectors
) {
1366 layout
->oobfree
[nsectors
].offset
=
1367 layout
->oobfree
[nsectors
- 1].offset
+
1368 layout
->oobfree
[nsectors
- 1].length
+
1370 layout
->oobfree
[nsectors
].length
= mtd
->oobsize
-
1371 ((ecc
->bytes
+ 4) * nsectors
);
1377 static int sunxi_nand_hw_syndrome_ecc_ctrl_init(struct mtd_info
*mtd
,
1378 struct nand_ecc_ctrl
*ecc
,
1379 struct device_node
*np
)
1381 struct nand_ecclayout
*layout
;
1386 ret
= sunxi_nand_hw_common_ecc_ctrl_init(mtd
, ecc
, np
);
1391 ecc
->read_page
= sunxi_nfc_hw_syndrome_ecc_read_page
;
1392 ecc
->write_page
= sunxi_nfc_hw_syndrome_ecc_write_page
;
1394 layout
= ecc
->layout
;
1395 nsectors
= mtd
->writesize
/ ecc
->size
;
1397 for (i
= 0; i
< (ecc
->bytes
* nsectors
); i
++)
1398 layout
->eccpos
[i
] = i
;
1400 layout
->oobfree
[0].length
= mtd
->oobsize
- i
;
1401 layout
->oobfree
[0].offset
= i
;
1406 static void sunxi_nand_ecc_cleanup(struct nand_ecc_ctrl
*ecc
)
1408 switch (ecc
->mode
) {
1410 case NAND_ECC_HW_SYNDROME
:
1411 sunxi_nand_hw_common_ecc_ctrl_cleanup(ecc
);
1420 static int sunxi_nand_ecc_init(struct mtd_info
*mtd
, struct nand_ecc_ctrl
*ecc
,
1421 struct device_node
*np
)
1423 struct nand_chip
*nand
= mtd_to_nand(mtd
);
1427 ecc
->size
= nand
->ecc_step_ds
;
1428 ecc
->strength
= nand
->ecc_strength_ds
;
1431 if (!ecc
->size
|| !ecc
->strength
)
1434 switch (ecc
->mode
) {
1435 case NAND_ECC_SOFT_BCH
:
1438 ret
= sunxi_nand_hw_ecc_ctrl_init(mtd
, ecc
, np
);
1442 case NAND_ECC_HW_SYNDROME
:
1443 ret
= sunxi_nand_hw_syndrome_ecc_ctrl_init(mtd
, ecc
, np
);
1448 ecc
->layout
= kzalloc(sizeof(*ecc
->layout
), GFP_KERNEL
);
1451 ecc
->layout
->oobfree
[0].length
= mtd
->oobsize
;
1461 static int sunxi_nand_chip_init(struct device
*dev
, struct sunxi_nfc
*nfc
,
1462 struct device_node
*np
)
1464 const struct nand_sdr_timings
*timings
;
1465 struct sunxi_nand_chip
*chip
;
1466 struct mtd_info
*mtd
;
1467 struct nand_chip
*nand
;
1473 if (!of_get_property(np
, "reg", &nsels
))
1476 nsels
/= sizeof(u32
);
1478 dev_err(dev
, "invalid reg property size\n");
1482 chip
= devm_kzalloc(dev
,
1484 (nsels
* sizeof(struct sunxi_nand_chip_sel
)),
1487 dev_err(dev
, "could not allocate chip\n");
1491 chip
->nsels
= nsels
;
1492 chip
->selected
= -1;
1494 for (i
= 0; i
< nsels
; i
++) {
1495 ret
= of_property_read_u32_index(np
, "reg", i
, &tmp
);
1497 dev_err(dev
, "could not retrieve reg property: %d\n",
1502 if (tmp
> NFC_MAX_CS
) {
1504 "invalid reg value: %u (max CS = 7)\n",
1509 if (test_and_set_bit(tmp
, &nfc
->assigned_cs
)) {
1510 dev_err(dev
, "CS %d already assigned\n", tmp
);
1514 chip
->sels
[i
].cs
= tmp
;
1516 if (!of_property_read_u32_index(np
, "allwinner,rb", i
, &tmp
) &&
1518 chip
->sels
[i
].rb
.type
= RB_NATIVE
;
1519 chip
->sels
[i
].rb
.info
.nativeid
= tmp
;
1521 ret
= of_get_named_gpio(np
, "rb-gpios", i
);
1524 chip
->sels
[i
].rb
.type
= RB_GPIO
;
1525 chip
->sels
[i
].rb
.info
.gpio
= tmp
;
1526 ret
= devm_gpio_request(dev
, tmp
, "nand-rb");
1530 ret
= gpio_direction_input(tmp
);
1534 chip
->sels
[i
].rb
.type
= RB_NONE
;
1539 timings
= onfi_async_timing_mode_to_sdr_timings(0);
1540 if (IS_ERR(timings
)) {
1541 ret
= PTR_ERR(timings
);
1543 "could not retrieve timings for ONFI mode 0: %d\n",
1548 ret
= sunxi_nand_chip_set_timings(chip
, timings
);
1550 dev_err(dev
, "could not configure chip timings: %d\n", ret
);
1555 /* Default tR value specified in the ONFI spec (chapter 4.15.1) */
1556 nand
->chip_delay
= 200;
1557 nand
->controller
= &nfc
->controller
;
1559 * Set the ECC mode to the default value in case nothing is specified
1562 nand
->ecc
.mode
= NAND_ECC_HW
;
1563 nand_set_flash_node(nand
, np
);
1564 nand
->select_chip
= sunxi_nfc_select_chip
;
1565 nand
->cmd_ctrl
= sunxi_nfc_cmd_ctrl
;
1566 nand
->read_buf
= sunxi_nfc_read_buf
;
1567 nand
->write_buf
= sunxi_nfc_write_buf
;
1568 nand
->read_byte
= sunxi_nfc_read_byte
;
1570 mtd
= nand_to_mtd(nand
);
1571 mtd
->dev
.parent
= dev
;
1573 ret
= nand_scan_ident(mtd
, nsels
, NULL
);
1577 if (nand
->bbt_options
& NAND_BBT_USE_FLASH
)
1578 nand
->bbt_options
|= NAND_BBT_NO_OOB
;
1580 if (nand
->options
& NAND_NEED_SCRAMBLING
)
1581 nand
->options
|= NAND_NO_SUBPAGE_WRITE
;
1583 ret
= sunxi_nand_chip_init_timings(chip
, np
);
1585 dev_err(dev
, "could not configure chip timings: %d\n", ret
);
1589 ret
= sunxi_nand_ecc_init(mtd
, &nand
->ecc
, np
);
1591 dev_err(dev
, "ECC init failed: %d\n", ret
);
1595 ret
= nand_scan_tail(mtd
);
1597 dev_err(dev
, "nand_scan_tail failed: %d\n", ret
);
1601 ret
= mtd_device_register(mtd
, NULL
, 0);
1603 dev_err(dev
, "failed to register mtd device: %d\n", ret
);
1608 list_add_tail(&chip
->node
, &nfc
->chips
);
1613 static int sunxi_nand_chips_init(struct device
*dev
, struct sunxi_nfc
*nfc
)
1615 struct device_node
*np
= dev
->of_node
;
1616 struct device_node
*nand_np
;
1617 int nchips
= of_get_child_count(np
);
1621 dev_err(dev
, "too many NAND chips: %d (max = 8)\n", nchips
);
1625 for_each_child_of_node(np
, nand_np
) {
1626 ret
= sunxi_nand_chip_init(dev
, nfc
, nand_np
);
1628 of_node_put(nand_np
);
1636 static void sunxi_nand_chips_cleanup(struct sunxi_nfc
*nfc
)
1638 struct sunxi_nand_chip
*chip
;
1640 while (!list_empty(&nfc
->chips
)) {
1641 chip
= list_first_entry(&nfc
->chips
, struct sunxi_nand_chip
,
1643 nand_release(nand_to_mtd(&chip
->nand
));
1644 sunxi_nand_ecc_cleanup(&chip
->nand
.ecc
);
1645 list_del(&chip
->node
);
1649 static int sunxi_nfc_probe(struct platform_device
*pdev
)
1651 struct device
*dev
= &pdev
->dev
;
1653 struct sunxi_nfc
*nfc
;
1657 nfc
= devm_kzalloc(dev
, sizeof(*nfc
), GFP_KERNEL
);
1662 spin_lock_init(&nfc
->controller
.lock
);
1663 init_waitqueue_head(&nfc
->controller
.wq
);
1664 INIT_LIST_HEAD(&nfc
->chips
);
1666 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1667 nfc
->regs
= devm_ioremap_resource(dev
, r
);
1668 if (IS_ERR(nfc
->regs
))
1669 return PTR_ERR(nfc
->regs
);
1671 irq
= platform_get_irq(pdev
, 0);
1673 dev_err(dev
, "failed to retrieve irq\n");
1677 nfc
->ahb_clk
= devm_clk_get(dev
, "ahb");
1678 if (IS_ERR(nfc
->ahb_clk
)) {
1679 dev_err(dev
, "failed to retrieve ahb clk\n");
1680 return PTR_ERR(nfc
->ahb_clk
);
1683 ret
= clk_prepare_enable(nfc
->ahb_clk
);
1687 nfc
->mod_clk
= devm_clk_get(dev
, "mod");
1688 if (IS_ERR(nfc
->mod_clk
)) {
1689 dev_err(dev
, "failed to retrieve mod clk\n");
1690 ret
= PTR_ERR(nfc
->mod_clk
);
1691 goto out_ahb_clk_unprepare
;
1694 ret
= clk_prepare_enable(nfc
->mod_clk
);
1696 goto out_ahb_clk_unprepare
;
1698 ret
= sunxi_nfc_rst(nfc
);
1700 goto out_mod_clk_unprepare
;
1702 writel(0, nfc
->regs
+ NFC_REG_INT
);
1703 ret
= devm_request_irq(dev
, irq
, sunxi_nfc_interrupt
,
1704 0, "sunxi-nand", nfc
);
1706 goto out_mod_clk_unprepare
;
1708 platform_set_drvdata(pdev
, nfc
);
1710 ret
= sunxi_nand_chips_init(dev
, nfc
);
1712 dev_err(dev
, "failed to init nand chips\n");
1713 goto out_mod_clk_unprepare
;
1718 out_mod_clk_unprepare
:
1719 clk_disable_unprepare(nfc
->mod_clk
);
1720 out_ahb_clk_unprepare
:
1721 clk_disable_unprepare(nfc
->ahb_clk
);
1726 static int sunxi_nfc_remove(struct platform_device
*pdev
)
1728 struct sunxi_nfc
*nfc
= platform_get_drvdata(pdev
);
1730 sunxi_nand_chips_cleanup(nfc
);
1735 static const struct of_device_id sunxi_nfc_ids
[] = {
1736 { .compatible
= "allwinner,sun4i-a10-nand" },
1739 MODULE_DEVICE_TABLE(of
, sunxi_nfc_ids
);
1741 static struct platform_driver sunxi_nfc_driver
= {
1743 .name
= "sunxi_nand",
1744 .of_match_table
= sunxi_nfc_ids
,
1746 .probe
= sunxi_nfc_probe
,
1747 .remove
= sunxi_nfc_remove
,
1749 module_platform_driver(sunxi_nfc_driver
);
1751 MODULE_LICENSE("GPL v2");
1752 MODULE_AUTHOR("Boris BREZILLON");
1753 MODULE_DESCRIPTION("Allwinner NAND Flash Controller driver");
1754 MODULE_ALIAS("platform:sunxi_nand");