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/mtd/mtd.h>
34 #include <linux/mtd/nand.h>
35 #include <linux/mtd/partitions.h>
36 #include <linux/clk.h>
37 #include <linux/delay.h>
38 #include <linux/dmaengine.h>
39 #include <linux/gpio.h>
40 #include <linux/interrupt.h>
41 #include <linux/iopoll.h>
43 #define NFC_REG_CTL 0x0000
44 #define NFC_REG_ST 0x0004
45 #define NFC_REG_INT 0x0008
46 #define NFC_REG_TIMING_CTL 0x000C
47 #define NFC_REG_TIMING_CFG 0x0010
48 #define NFC_REG_ADDR_LOW 0x0014
49 #define NFC_REG_ADDR_HIGH 0x0018
50 #define NFC_REG_SECTOR_NUM 0x001C
51 #define NFC_REG_CNT 0x0020
52 #define NFC_REG_CMD 0x0024
53 #define NFC_REG_RCMD_SET 0x0028
54 #define NFC_REG_WCMD_SET 0x002C
55 #define NFC_REG_IO_DATA 0x0030
56 #define NFC_REG_ECC_CTL 0x0034
57 #define NFC_REG_ECC_ST 0x0038
58 #define NFC_REG_DEBUG 0x003C
59 #define NFC_REG_ECC_ERR_CNT(x) ((0x0040 + (x)) & ~0x3)
60 #define NFC_REG_USER_DATA(x) (0x0050 + ((x) * 4))
61 #define NFC_REG_SPARE_AREA 0x00A0
62 #define NFC_REG_PAT_ID 0x00A4
63 #define NFC_RAM0_BASE 0x0400
64 #define NFC_RAM1_BASE 0x0800
66 /* define bit use in NFC_CTL */
68 #define NFC_RESET BIT(1)
69 #define NFC_BUS_WIDTH_MSK BIT(2)
70 #define NFC_BUS_WIDTH_8 (0 << 2)
71 #define NFC_BUS_WIDTH_16 (1 << 2)
72 #define NFC_RB_SEL_MSK BIT(3)
73 #define NFC_RB_SEL(x) ((x) << 3)
74 #define NFC_CE_SEL_MSK GENMASK(26, 24)
75 #define NFC_CE_SEL(x) ((x) << 24)
76 #define NFC_CE_CTL BIT(6)
77 #define NFC_PAGE_SHIFT_MSK GENMASK(11, 8)
78 #define NFC_PAGE_SHIFT(x) (((x) < 10 ? 0 : (x) - 10) << 8)
79 #define NFC_SAM BIT(12)
80 #define NFC_RAM_METHOD BIT(14)
81 #define NFC_DEBUG_CTL BIT(31)
83 /* define bit use in NFC_ST */
84 #define NFC_RB_B2R BIT(0)
85 #define NFC_CMD_INT_FLAG BIT(1)
86 #define NFC_DMA_INT_FLAG BIT(2)
87 #define NFC_CMD_FIFO_STATUS BIT(3)
88 #define NFC_STA BIT(4)
89 #define NFC_NATCH_INT_FLAG BIT(5)
90 #define NFC_RB_STATE(x) BIT(x + 8)
92 /* define bit use in NFC_INT */
93 #define NFC_B2R_INT_ENABLE BIT(0)
94 #define NFC_CMD_INT_ENABLE BIT(1)
95 #define NFC_DMA_INT_ENABLE BIT(2)
96 #define NFC_INT_MASK (NFC_B2R_INT_ENABLE | \
97 NFC_CMD_INT_ENABLE | \
100 /* define bit use in NFC_TIMING_CTL */
101 #define NFC_TIMING_CTL_EDO BIT(8)
103 /* define NFC_TIMING_CFG register layout */
104 #define NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD) \
105 (((tWB) & 0x3) | (((tADL) & 0x3) << 2) | \
106 (((tWHR) & 0x3) << 4) | (((tRHW) & 0x3) << 6) | \
107 (((tCAD) & 0x7) << 8))
109 /* define bit use in NFC_CMD */
110 #define NFC_CMD_LOW_BYTE_MSK GENMASK(7, 0)
111 #define NFC_CMD_HIGH_BYTE_MSK GENMASK(15, 8)
112 #define NFC_CMD(x) (x)
113 #define NFC_ADR_NUM_MSK GENMASK(18, 16)
114 #define NFC_ADR_NUM(x) (((x) - 1) << 16)
115 #define NFC_SEND_ADR BIT(19)
116 #define NFC_ACCESS_DIR BIT(20)
117 #define NFC_DATA_TRANS BIT(21)
118 #define NFC_SEND_CMD1 BIT(22)
119 #define NFC_WAIT_FLAG BIT(23)
120 #define NFC_SEND_CMD2 BIT(24)
121 #define NFC_SEQ BIT(25)
122 #define NFC_DATA_SWAP_METHOD BIT(26)
123 #define NFC_ROW_AUTO_INC BIT(27)
124 #define NFC_SEND_CMD3 BIT(28)
125 #define NFC_SEND_CMD4 BIT(29)
126 #define NFC_CMD_TYPE_MSK GENMASK(31, 30)
127 #define NFC_NORMAL_OP (0 << 30)
128 #define NFC_ECC_OP (1 << 30)
129 #define NFC_PAGE_OP (2 << 30)
131 /* define bit use in NFC_RCMD_SET */
132 #define NFC_READ_CMD_MSK GENMASK(7, 0)
133 #define NFC_RND_READ_CMD0_MSK GENMASK(15, 8)
134 #define NFC_RND_READ_CMD1_MSK GENMASK(23, 16)
136 /* define bit use in NFC_WCMD_SET */
137 #define NFC_PROGRAM_CMD_MSK GENMASK(7, 0)
138 #define NFC_RND_WRITE_CMD_MSK GENMASK(15, 8)
139 #define NFC_READ_CMD0_MSK GENMASK(23, 16)
140 #define NFC_READ_CMD1_MSK GENMASK(31, 24)
142 /* define bit use in NFC_ECC_CTL */
143 #define NFC_ECC_EN BIT(0)
144 #define NFC_ECC_PIPELINE BIT(3)
145 #define NFC_ECC_EXCEPTION BIT(4)
146 #define NFC_ECC_BLOCK_SIZE_MSK BIT(5)
147 #define NFC_RANDOM_EN BIT(9)
148 #define NFC_RANDOM_DIRECTION BIT(10)
149 #define NFC_ECC_MODE_MSK GENMASK(15, 12)
150 #define NFC_ECC_MODE(x) ((x) << 12)
151 #define NFC_RANDOM_SEED_MSK GENMASK(30, 16)
152 #define NFC_RANDOM_SEED(x) ((x) << 16)
154 /* define bit use in NFC_ECC_ST */
155 #define NFC_ECC_ERR(x) BIT(x)
156 #define NFC_ECC_PAT_FOUND(x) BIT(x + 16)
157 #define NFC_ECC_ERR_CNT(b, x) (((x) >> (((b) % 4) * 8)) & 0xff)
159 #define NFC_DEFAULT_TIMEOUT_MS 1000
161 #define NFC_SRAM_SIZE 1024
166 * Ready/Busy detection type: describes the Ready/Busy detection modes
168 * @RB_NONE: no external detection available, rely on STATUS command
169 * and software timeouts
170 * @RB_NATIVE: use sunxi NAND controller Ready/Busy support. The Ready/Busy
171 * pin of the NAND flash chip must be connected to one of the
172 * native NAND R/B pins (those which can be muxed to the NAND
174 * @RB_GPIO: use a simple GPIO to handle Ready/Busy status. The Ready/Busy
175 * pin of the NAND flash chip must be connected to a GPIO capable
178 enum sunxi_nand_rb_type
{
185 * Ready/Busy structure: stores information related to Ready/Busy detection
187 * @type: the Ready/Busy detection mode
188 * @info: information related to the R/B detection mode. Either a gpio
189 * id or a native R/B id (those supported by the NAND controller).
191 struct sunxi_nand_rb
{
192 enum sunxi_nand_rb_type type
;
200 * Chip Select structure: stores information related to NAND Chip Select
202 * @cs: the NAND CS id used to communicate with a NAND Chip
203 * @rb: the Ready/Busy description
205 struct sunxi_nand_chip_sel
{
207 struct sunxi_nand_rb rb
;
211 * sunxi HW ECC infos: stores information related to HW ECC support
213 * @mode: the sunxi ECC mode field deduced from ECC requirements
215 struct sunxi_nand_hw_ecc
{
220 * NAND chip structure: stores NAND chip device related information
222 * @node: used to store NAND chips into a list
223 * @nand: base NAND chip structure
224 * @mtd: base MTD structure
225 * @clk_rate: clk_rate required for this NAND chip
226 * @timing_cfg TIMING_CFG register value for this NAND chip
227 * @selected: current active CS
228 * @nsels: number of CS lines required by the NAND chip
229 * @sels: array of CS lines descriptions
231 struct sunxi_nand_chip
{
232 struct list_head node
;
233 struct nand_chip nand
;
234 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_events(struct sunxi_nfc
*nfc
, u32 events
,
302 bool use_polling
, unsigned int timeout_ms
)
306 if (events
& ~NFC_INT_MASK
)
310 timeout_ms
= NFC_DEFAULT_TIMEOUT_MS
;
313 init_completion(&nfc
->complete
);
315 writel(events
, nfc
->regs
+ NFC_REG_INT
);
317 ret
= wait_for_completion_timeout(&nfc
->complete
,
318 msecs_to_jiffies(timeout_ms
));
320 writel(0, nfc
->regs
+ NFC_REG_INT
);
324 ret
= readl_poll_timeout(nfc
->regs
+ NFC_REG_ST
, status
,
325 (status
& events
) == events
, 1,
329 writel(events
& NFC_INT_MASK
, nfc
->regs
+ NFC_REG_ST
);
332 dev_err(nfc
->dev
, "wait interrupt timedout\n");
337 static int sunxi_nfc_wait_cmd_fifo_empty(struct sunxi_nfc
*nfc
)
342 ret
= readl_poll_timeout(nfc
->regs
+ NFC_REG_ST
, status
,
343 !(status
& NFC_CMD_FIFO_STATUS
), 1,
344 NFC_DEFAULT_TIMEOUT_MS
* 1000);
346 dev_err(nfc
->dev
, "wait for empty cmd FIFO timedout\n");
351 static int sunxi_nfc_rst(struct sunxi_nfc
*nfc
)
356 writel(0, nfc
->regs
+ NFC_REG_ECC_CTL
);
357 writel(NFC_RESET
, nfc
->regs
+ NFC_REG_CTL
);
359 ret
= readl_poll_timeout(nfc
->regs
+ NFC_REG_CTL
, ctl
,
360 !(ctl
& NFC_RESET
), 1,
361 NFC_DEFAULT_TIMEOUT_MS
* 1000);
363 dev_err(nfc
->dev
, "wait for NAND controller reset timedout\n");
368 static int sunxi_nfc_dev_ready(struct mtd_info
*mtd
)
370 struct nand_chip
*nand
= mtd_to_nand(mtd
);
371 struct sunxi_nand_chip
*sunxi_nand
= to_sunxi_nand(nand
);
372 struct sunxi_nfc
*nfc
= to_sunxi_nfc(sunxi_nand
->nand
.controller
);
373 struct sunxi_nand_rb
*rb
;
376 if (sunxi_nand
->selected
< 0)
379 rb
= &sunxi_nand
->sels
[sunxi_nand
->selected
].rb
;
383 ret
= !!(readl(nfc
->regs
+ NFC_REG_ST
) &
384 NFC_RB_STATE(rb
->info
.nativeid
));
387 ret
= gpio_get_value(rb
->info
.gpio
);
392 dev_err(nfc
->dev
, "cannot check R/B NAND status!\n");
399 static void sunxi_nfc_select_chip(struct mtd_info
*mtd
, int chip
)
401 struct nand_chip
*nand
= mtd_to_nand(mtd
);
402 struct sunxi_nand_chip
*sunxi_nand
= to_sunxi_nand(nand
);
403 struct sunxi_nfc
*nfc
= to_sunxi_nfc(sunxi_nand
->nand
.controller
);
404 struct sunxi_nand_chip_sel
*sel
;
407 if (chip
> 0 && chip
>= sunxi_nand
->nsels
)
410 if (chip
== sunxi_nand
->selected
)
413 ctl
= readl(nfc
->regs
+ NFC_REG_CTL
) &
414 ~(NFC_PAGE_SHIFT_MSK
| NFC_CE_SEL_MSK
| NFC_RB_SEL_MSK
| NFC_EN
);
417 sel
= &sunxi_nand
->sels
[chip
];
419 ctl
|= NFC_CE_SEL(sel
->cs
) | NFC_EN
|
420 NFC_PAGE_SHIFT(nand
->page_shift
);
421 if (sel
->rb
.type
== RB_NONE
) {
422 nand
->dev_ready
= NULL
;
424 nand
->dev_ready
= sunxi_nfc_dev_ready
;
425 if (sel
->rb
.type
== RB_NATIVE
)
426 ctl
|= NFC_RB_SEL(sel
->rb
.info
.nativeid
);
429 writel(mtd
->writesize
, nfc
->regs
+ NFC_REG_SPARE_AREA
);
431 if (nfc
->clk_rate
!= sunxi_nand
->clk_rate
) {
432 clk_set_rate(nfc
->mod_clk
, sunxi_nand
->clk_rate
);
433 nfc
->clk_rate
= sunxi_nand
->clk_rate
;
437 writel(sunxi_nand
->timing_ctl
, nfc
->regs
+ NFC_REG_TIMING_CTL
);
438 writel(sunxi_nand
->timing_cfg
, nfc
->regs
+ NFC_REG_TIMING_CFG
);
439 writel(ctl
, nfc
->regs
+ NFC_REG_CTL
);
441 sunxi_nand
->selected
= chip
;
444 static void sunxi_nfc_read_buf(struct mtd_info
*mtd
, uint8_t *buf
, int len
)
446 struct nand_chip
*nand
= mtd_to_nand(mtd
);
447 struct sunxi_nand_chip
*sunxi_nand
= to_sunxi_nand(nand
);
448 struct sunxi_nfc
*nfc
= to_sunxi_nfc(sunxi_nand
->nand
.controller
);
455 cnt
= min(len
- offs
, NFC_SRAM_SIZE
);
457 ret
= sunxi_nfc_wait_cmd_fifo_empty(nfc
);
461 writel(cnt
, nfc
->regs
+ NFC_REG_CNT
);
462 tmp
= NFC_DATA_TRANS
| NFC_DATA_SWAP_METHOD
;
463 writel(tmp
, nfc
->regs
+ NFC_REG_CMD
);
465 ret
= sunxi_nfc_wait_events(nfc
, NFC_CMD_INT_FLAG
, true, 0);
470 memcpy_fromio(buf
+ offs
, nfc
->regs
+ NFC_RAM0_BASE
,
476 static void sunxi_nfc_write_buf(struct mtd_info
*mtd
, const uint8_t *buf
,
479 struct nand_chip
*nand
= mtd_to_nand(mtd
);
480 struct sunxi_nand_chip
*sunxi_nand
= to_sunxi_nand(nand
);
481 struct sunxi_nfc
*nfc
= to_sunxi_nfc(sunxi_nand
->nand
.controller
);
488 cnt
= min(len
- offs
, NFC_SRAM_SIZE
);
490 ret
= sunxi_nfc_wait_cmd_fifo_empty(nfc
);
494 writel(cnt
, nfc
->regs
+ NFC_REG_CNT
);
495 memcpy_toio(nfc
->regs
+ NFC_RAM0_BASE
, buf
+ offs
, cnt
);
496 tmp
= NFC_DATA_TRANS
| NFC_DATA_SWAP_METHOD
|
498 writel(tmp
, nfc
->regs
+ NFC_REG_CMD
);
500 ret
= sunxi_nfc_wait_events(nfc
, NFC_CMD_INT_FLAG
, true, 0);
508 static uint8_t sunxi_nfc_read_byte(struct mtd_info
*mtd
)
512 sunxi_nfc_read_buf(mtd
, &ret
, 1);
517 static void sunxi_nfc_cmd_ctrl(struct mtd_info
*mtd
, int dat
,
520 struct nand_chip
*nand
= mtd_to_nand(mtd
);
521 struct sunxi_nand_chip
*sunxi_nand
= to_sunxi_nand(nand
);
522 struct sunxi_nfc
*nfc
= to_sunxi_nfc(sunxi_nand
->nand
.controller
);
525 ret
= sunxi_nfc_wait_cmd_fifo_empty(nfc
);
529 if (dat
== NAND_CMD_NONE
&& (ctrl
& NAND_NCE
) &&
530 !(ctrl
& (NAND_CLE
| NAND_ALE
))) {
533 if (!sunxi_nand
->addr_cycles
&& !sunxi_nand
->cmd_cycles
)
536 if (sunxi_nand
->cmd_cycles
--)
537 cmd
|= NFC_SEND_CMD1
| sunxi_nand
->cmd
[0];
539 if (sunxi_nand
->cmd_cycles
--) {
540 cmd
|= NFC_SEND_CMD2
;
541 writel(sunxi_nand
->cmd
[1],
542 nfc
->regs
+ NFC_REG_RCMD_SET
);
545 sunxi_nand
->cmd_cycles
= 0;
547 if (sunxi_nand
->addr_cycles
) {
548 cmd
|= NFC_SEND_ADR
|
549 NFC_ADR_NUM(sunxi_nand
->addr_cycles
);
550 writel(sunxi_nand
->addr
[0],
551 nfc
->regs
+ NFC_REG_ADDR_LOW
);
554 if (sunxi_nand
->addr_cycles
> 4)
555 writel(sunxi_nand
->addr
[1],
556 nfc
->regs
+ NFC_REG_ADDR_HIGH
);
558 writel(cmd
, nfc
->regs
+ NFC_REG_CMD
);
559 sunxi_nand
->addr
[0] = 0;
560 sunxi_nand
->addr
[1] = 0;
561 sunxi_nand
->addr_cycles
= 0;
562 sunxi_nfc_wait_events(nfc
, NFC_CMD_INT_FLAG
, true, 0);
565 if (ctrl
& NAND_CLE
) {
566 sunxi_nand
->cmd
[sunxi_nand
->cmd_cycles
++] = dat
;
567 } else if (ctrl
& NAND_ALE
) {
568 sunxi_nand
->addr
[sunxi_nand
->addr_cycles
/ 4] |=
569 dat
<< ((sunxi_nand
->addr_cycles
% 4) * 8);
570 sunxi_nand
->addr_cycles
++;
574 /* These seed values have been extracted from Allwinner's BSP */
575 static const u16 sunxi_nfc_randomizer_page_seeds
[] = {
576 0x2b75, 0x0bd0, 0x5ca3, 0x62d1, 0x1c93, 0x07e9, 0x2162, 0x3a72,
577 0x0d67, 0x67f9, 0x1be7, 0x077d, 0x032f, 0x0dac, 0x2716, 0x2436,
578 0x7922, 0x1510, 0x3860, 0x5287, 0x480f, 0x4252, 0x1789, 0x5a2d,
579 0x2a49, 0x5e10, 0x437f, 0x4b4e, 0x2f45, 0x216e, 0x5cb7, 0x7130,
580 0x2a3f, 0x60e4, 0x4dc9, 0x0ef0, 0x0f52, 0x1bb9, 0x6211, 0x7a56,
581 0x226d, 0x4ea7, 0x6f36, 0x3692, 0x38bf, 0x0c62, 0x05eb, 0x4c55,
582 0x60f4, 0x728c, 0x3b6f, 0x2037, 0x7f69, 0x0936, 0x651a, 0x4ceb,
583 0x6218, 0x79f3, 0x383f, 0x18d9, 0x4f05, 0x5c82, 0x2912, 0x6f17,
584 0x6856, 0x5938, 0x1007, 0x61ab, 0x3e7f, 0x57c2, 0x542f, 0x4f62,
585 0x7454, 0x2eac, 0x7739, 0x42d4, 0x2f90, 0x435a, 0x2e52, 0x2064,
586 0x637c, 0x66ad, 0x2c90, 0x0bad, 0x759c, 0x0029, 0x0986, 0x7126,
587 0x1ca7, 0x1605, 0x386a, 0x27f5, 0x1380, 0x6d75, 0x24c3, 0x0f8e,
588 0x2b7a, 0x1418, 0x1fd1, 0x7dc1, 0x2d8e, 0x43af, 0x2267, 0x7da3,
589 0x4e3d, 0x1338, 0x50db, 0x454d, 0x764d, 0x40a3, 0x42e6, 0x262b,
590 0x2d2e, 0x1aea, 0x2e17, 0x173d, 0x3a6e, 0x71bf, 0x25f9, 0x0a5d,
591 0x7c57, 0x0fbe, 0x46ce, 0x4939, 0x6b17, 0x37bb, 0x3e91, 0x76db,
595 * sunxi_nfc_randomizer_ecc512_seeds and sunxi_nfc_randomizer_ecc1024_seeds
596 * have been generated using
597 * sunxi_nfc_randomizer_step(seed, (step_size * 8) + 15), which is what
598 * the randomizer engine does internally before de/scrambling OOB data.
600 * Those tables are statically defined to avoid calculating randomizer state
603 static const u16 sunxi_nfc_randomizer_ecc512_seeds
[] = {
604 0x3346, 0x367f, 0x1f18, 0x769a, 0x4f64, 0x068c, 0x2ef1, 0x6b64,
605 0x28a9, 0x15d7, 0x30f8, 0x3659, 0x53db, 0x7c5f, 0x71d4, 0x4409,
606 0x26eb, 0x03cc, 0x655d, 0x47d4, 0x4daa, 0x0877, 0x712d, 0x3617,
607 0x3264, 0x49aa, 0x7f9e, 0x588e, 0x4fbc, 0x7176, 0x7f91, 0x6c6d,
608 0x4b95, 0x5fb7, 0x3844, 0x4037, 0x0184, 0x081b, 0x0ee8, 0x5b91,
609 0x293d, 0x1f71, 0x0e6f, 0x402b, 0x5122, 0x1e52, 0x22be, 0x3d2d,
610 0x75bc, 0x7c60, 0x6291, 0x1a2f, 0x61d4, 0x74aa, 0x4140, 0x29ab,
611 0x472d, 0x2852, 0x017e, 0x15e8, 0x5ec2, 0x17cf, 0x7d0f, 0x06b8,
612 0x117a, 0x6b94, 0x789b, 0x3126, 0x6ac5, 0x5be7, 0x150f, 0x51f8,
613 0x7889, 0x0aa5, 0x663d, 0x77e8, 0x0b87, 0x3dcb, 0x360d, 0x218b,
614 0x512f, 0x7dc9, 0x6a4d, 0x630a, 0x3547, 0x1dd2, 0x5aea, 0x69a5,
615 0x7bfa, 0x5e4f, 0x1519, 0x6430, 0x3a0e, 0x5eb3, 0x5425, 0x0c7a,
616 0x5540, 0x3670, 0x63c1, 0x31e9, 0x5a39, 0x2de7, 0x5979, 0x2891,
617 0x1562, 0x014b, 0x5b05, 0x2756, 0x5a34, 0x13aa, 0x6cb5, 0x2c36,
618 0x5e72, 0x1306, 0x0861, 0x15ef, 0x1ee8, 0x5a37, 0x7ac4, 0x45dd,
619 0x44c4, 0x7266, 0x2f41, 0x3ccc, 0x045e, 0x7d40, 0x7c66, 0x0fa0,
622 static const u16 sunxi_nfc_randomizer_ecc1024_seeds
[] = {
623 0x2cf5, 0x35f1, 0x63a4, 0x5274, 0x2bd2, 0x778b, 0x7285, 0x32b6,
624 0x6a5c, 0x70d6, 0x757d, 0x6769, 0x5375, 0x1e81, 0x0cf3, 0x3982,
625 0x6787, 0x042a, 0x6c49, 0x1925, 0x56a8, 0x40a9, 0x063e, 0x7bd9,
626 0x4dbf, 0x55ec, 0x672e, 0x7334, 0x5185, 0x4d00, 0x232a, 0x7e07,
627 0x445d, 0x6b92, 0x528f, 0x4255, 0x53ba, 0x7d82, 0x2a2e, 0x3a4e,
628 0x75eb, 0x450c, 0x6844, 0x1b5d, 0x581a, 0x4cc6, 0x0379, 0x37b2,
629 0x419f, 0x0e92, 0x6b27, 0x5624, 0x01e3, 0x07c1, 0x44a5, 0x130c,
630 0x13e8, 0x5910, 0x0876, 0x60c5, 0x54e3, 0x5b7f, 0x2269, 0x509f,
631 0x7665, 0x36fd, 0x3e9a, 0x0579, 0x6295, 0x14ef, 0x0a81, 0x1bcc,
632 0x4b16, 0x64db, 0x0514, 0x4f07, 0x0591, 0x3576, 0x6853, 0x0d9e,
633 0x259f, 0x38b7, 0x64fb, 0x3094, 0x4693, 0x6ddd, 0x29bb, 0x0bc8,
634 0x3f47, 0x490e, 0x0c0e, 0x7933, 0x3c9e, 0x5840, 0x398d, 0x3e68,
635 0x4af1, 0x71f5, 0x57cf, 0x1121, 0x64eb, 0x3579, 0x15ac, 0x584d,
636 0x5f2a, 0x47e2, 0x6528, 0x6eac, 0x196e, 0x6b96, 0x0450, 0x0179,
637 0x609c, 0x06e1, 0x4626, 0x42c7, 0x273e, 0x486f, 0x0705, 0x1601,
638 0x145b, 0x407e, 0x062b, 0x57a5, 0x53f9, 0x5659, 0x4410, 0x3ccd,
641 static u16
sunxi_nfc_randomizer_step(u16 state
, int count
)
646 * This loop is just a simple implementation of a Fibonacci LFSR using
647 * the x16 + x15 + 1 polynomial.
650 state
= ((state
>> 1) |
651 (((state
^ (state
>> 1)) & 1) << 14)) & 0x7fff;
656 static u16
sunxi_nfc_randomizer_state(struct mtd_info
*mtd
, int page
, bool ecc
)
658 const u16
*seeds
= sunxi_nfc_randomizer_page_seeds
;
659 int mod
= mtd_div_by_ws(mtd
->erasesize
, mtd
);
661 if (mod
> ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds
))
662 mod
= ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds
);
665 if (mtd
->ecc_step_size
== 512)
666 seeds
= sunxi_nfc_randomizer_ecc512_seeds
;
668 seeds
= sunxi_nfc_randomizer_ecc1024_seeds
;
671 return seeds
[page
% mod
];
674 static void sunxi_nfc_randomizer_config(struct mtd_info
*mtd
,
677 struct nand_chip
*nand
= mtd_to_nand(mtd
);
678 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
679 u32 ecc_ctl
= readl(nfc
->regs
+ NFC_REG_ECC_CTL
);
682 if (!(nand
->options
& NAND_NEED_SCRAMBLING
))
685 ecc_ctl
= readl(nfc
->regs
+ NFC_REG_ECC_CTL
);
686 state
= sunxi_nfc_randomizer_state(mtd
, page
, ecc
);
687 ecc_ctl
= readl(nfc
->regs
+ NFC_REG_ECC_CTL
) & ~NFC_RANDOM_SEED_MSK
;
688 writel(ecc_ctl
| NFC_RANDOM_SEED(state
), nfc
->regs
+ NFC_REG_ECC_CTL
);
691 static void sunxi_nfc_randomizer_enable(struct mtd_info
*mtd
)
693 struct nand_chip
*nand
= mtd_to_nand(mtd
);
694 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
696 if (!(nand
->options
& NAND_NEED_SCRAMBLING
))
699 writel(readl(nfc
->regs
+ NFC_REG_ECC_CTL
) | NFC_RANDOM_EN
,
700 nfc
->regs
+ NFC_REG_ECC_CTL
);
703 static void sunxi_nfc_randomizer_disable(struct mtd_info
*mtd
)
705 struct nand_chip
*nand
= mtd_to_nand(mtd
);
706 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
708 if (!(nand
->options
& NAND_NEED_SCRAMBLING
))
711 writel(readl(nfc
->regs
+ NFC_REG_ECC_CTL
) & ~NFC_RANDOM_EN
,
712 nfc
->regs
+ NFC_REG_ECC_CTL
);
715 static void sunxi_nfc_randomize_bbm(struct mtd_info
*mtd
, int page
, u8
*bbm
)
717 u16 state
= sunxi_nfc_randomizer_state(mtd
, page
, true);
720 bbm
[1] ^= sunxi_nfc_randomizer_step(state
, 8);
723 static void sunxi_nfc_randomizer_write_buf(struct mtd_info
*mtd
,
724 const uint8_t *buf
, int len
,
727 sunxi_nfc_randomizer_config(mtd
, page
, ecc
);
728 sunxi_nfc_randomizer_enable(mtd
);
729 sunxi_nfc_write_buf(mtd
, buf
, len
);
730 sunxi_nfc_randomizer_disable(mtd
);
733 static void sunxi_nfc_randomizer_read_buf(struct mtd_info
*mtd
, uint8_t *buf
,
734 int len
, bool ecc
, int page
)
736 sunxi_nfc_randomizer_config(mtd
, page
, ecc
);
737 sunxi_nfc_randomizer_enable(mtd
);
738 sunxi_nfc_read_buf(mtd
, buf
, len
);
739 sunxi_nfc_randomizer_disable(mtd
);
742 static void sunxi_nfc_hw_ecc_enable(struct mtd_info
*mtd
)
744 struct nand_chip
*nand
= mtd_to_nand(mtd
);
745 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
746 struct sunxi_nand_hw_ecc
*data
= nand
->ecc
.priv
;
749 ecc_ctl
= readl(nfc
->regs
+ NFC_REG_ECC_CTL
);
750 ecc_ctl
&= ~(NFC_ECC_MODE_MSK
| NFC_ECC_PIPELINE
|
751 NFC_ECC_BLOCK_SIZE_MSK
);
752 ecc_ctl
|= NFC_ECC_EN
| NFC_ECC_MODE(data
->mode
) | NFC_ECC_EXCEPTION
|
755 writel(ecc_ctl
, nfc
->regs
+ NFC_REG_ECC_CTL
);
758 static void sunxi_nfc_hw_ecc_disable(struct mtd_info
*mtd
)
760 struct nand_chip
*nand
= mtd_to_nand(mtd
);
761 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
763 writel(readl(nfc
->regs
+ NFC_REG_ECC_CTL
) & ~NFC_ECC_EN
,
764 nfc
->regs
+ NFC_REG_ECC_CTL
);
767 static inline void sunxi_nfc_user_data_to_buf(u32 user_data
, u8
*buf
)
770 buf
[1] = user_data
>> 8;
771 buf
[2] = user_data
>> 16;
772 buf
[3] = user_data
>> 24;
775 static inline u32
sunxi_nfc_buf_to_user_data(const u8
*buf
)
777 return buf
[0] | (buf
[1] << 8) | (buf
[2] << 16) | (buf
[3] << 24);
780 static void sunxi_nfc_hw_ecc_get_prot_oob_bytes(struct mtd_info
*mtd
, u8
*oob
,
781 int step
, bool bbm
, int page
)
783 struct nand_chip
*nand
= mtd_to_nand(mtd
);
784 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
786 sunxi_nfc_user_data_to_buf(readl(nfc
->regs
+ NFC_REG_USER_DATA(step
)),
789 /* De-randomize the Bad Block Marker. */
790 if (bbm
&& (nand
->options
& NAND_NEED_SCRAMBLING
))
791 sunxi_nfc_randomize_bbm(mtd
, page
, oob
);
794 static void sunxi_nfc_hw_ecc_set_prot_oob_bytes(struct mtd_info
*mtd
,
795 const u8
*oob
, int step
,
798 struct nand_chip
*nand
= mtd_to_nand(mtd
);
799 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
802 /* Randomize the Bad Block Marker. */
803 if (bbm
&& (nand
->options
& NAND_NEED_SCRAMBLING
)) {
804 memcpy(user_data
, oob
, sizeof(user_data
));
805 sunxi_nfc_randomize_bbm(mtd
, page
, user_data
);
809 writel(sunxi_nfc_buf_to_user_data(oob
),
810 nfc
->regs
+ NFC_REG_USER_DATA(step
));
813 static void sunxi_nfc_hw_ecc_update_stats(struct mtd_info
*mtd
,
814 unsigned int *max_bitflips
, int ret
)
817 mtd
->ecc_stats
.failed
++;
819 mtd
->ecc_stats
.corrected
+= ret
;
820 *max_bitflips
= max_t(unsigned int, *max_bitflips
, ret
);
824 static int sunxi_nfc_hw_ecc_correct(struct mtd_info
*mtd
, u8
*data
, u8
*oob
,
825 int step
, bool *erased
)
827 struct nand_chip
*nand
= mtd_to_nand(mtd
);
828 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
829 struct nand_ecc_ctrl
*ecc
= &nand
->ecc
;
834 status
= readl(nfc
->regs
+ NFC_REG_ECC_ST
);
836 if (status
& NFC_ECC_ERR(step
))
839 if (status
& NFC_ECC_PAT_FOUND(step
)) {
842 if (unlikely(!(readl(nfc
->regs
+ NFC_REG_PAT_ID
) & 0x1))) {
850 memset(data
, pattern
, ecc
->size
);
853 memset(oob
, pattern
, ecc
->bytes
+ 4);
858 tmp
= readl(nfc
->regs
+ NFC_REG_ECC_ERR_CNT(step
));
860 return NFC_ECC_ERR_CNT(step
, tmp
);
863 static int sunxi_nfc_hw_ecc_read_chunk(struct mtd_info
*mtd
,
864 u8
*data
, int data_off
,
865 u8
*oob
, int oob_off
,
867 unsigned int *max_bitflips
,
868 bool bbm
, bool oob_required
, int page
)
870 struct nand_chip
*nand
= mtd_to_nand(mtd
);
871 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
872 struct nand_ecc_ctrl
*ecc
= &nand
->ecc
;
877 if (*cur_off
!= data_off
)
878 nand
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, data_off
, -1);
880 sunxi_nfc_randomizer_read_buf(mtd
, NULL
, ecc
->size
, false, page
);
882 if (data_off
+ ecc
->size
!= oob_off
)
883 nand
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, oob_off
, -1);
885 ret
= sunxi_nfc_wait_cmd_fifo_empty(nfc
);
889 sunxi_nfc_randomizer_enable(mtd
);
890 writel(NFC_DATA_TRANS
| NFC_DATA_SWAP_METHOD
| NFC_ECC_OP
,
891 nfc
->regs
+ NFC_REG_CMD
);
893 ret
= sunxi_nfc_wait_events(nfc
, NFC_CMD_INT_FLAG
, true, 0);
894 sunxi_nfc_randomizer_disable(mtd
);
898 *cur_off
= oob_off
+ ecc
->bytes
+ 4;
900 ret
= sunxi_nfc_hw_ecc_correct(mtd
, data
, oob_required
? oob
: NULL
, 0,
907 * Re-read the data with the randomizer disabled to identify
908 * bitflips in erased pages.
910 if (nand
->options
& NAND_NEED_SCRAMBLING
) {
911 nand
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, data_off
, -1);
912 nand
->read_buf(mtd
, data
, ecc
->size
);
914 memcpy_fromio(data
, nfc
->regs
+ NFC_RAM0_BASE
,
918 nand
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, oob_off
, -1);
919 nand
->read_buf(mtd
, oob
, ecc
->bytes
+ 4);
921 ret
= nand_check_erased_ecc_chunk(data
, ecc
->size
,
923 NULL
, 0, ecc
->strength
);
927 memcpy_fromio(data
, nfc
->regs
+ NFC_RAM0_BASE
, ecc
->size
);
930 nand
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, oob_off
, -1);
931 sunxi_nfc_randomizer_read_buf(mtd
, oob
, ecc
->bytes
+ 4,
934 sunxi_nfc_hw_ecc_get_prot_oob_bytes(mtd
, oob
, 0,
939 sunxi_nfc_hw_ecc_update_stats(mtd
, max_bitflips
, ret
);
944 static void sunxi_nfc_hw_ecc_read_extra_oob(struct mtd_info
*mtd
,
945 u8
*oob
, int *cur_off
,
946 bool randomize
, int page
)
948 struct nand_chip
*nand
= mtd_to_nand(mtd
);
949 struct nand_ecc_ctrl
*ecc
= &nand
->ecc
;
950 int offset
= ((ecc
->bytes
+ 4) * ecc
->steps
);
951 int len
= mtd
->oobsize
- offset
;
956 if (!cur_off
|| *cur_off
!= offset
)
957 nand
->cmdfunc(mtd
, NAND_CMD_RNDOUT
,
958 offset
+ mtd
->writesize
, -1);
961 sunxi_nfc_read_buf(mtd
, oob
+ offset
, len
);
963 sunxi_nfc_randomizer_read_buf(mtd
, oob
+ offset
, len
,
967 *cur_off
= mtd
->oobsize
+ mtd
->writesize
;
970 static int sunxi_nfc_hw_ecc_write_chunk(struct mtd_info
*mtd
,
971 const u8
*data
, int data_off
,
972 const u8
*oob
, int oob_off
,
973 int *cur_off
, bool bbm
,
976 struct nand_chip
*nand
= mtd_to_nand(mtd
);
977 struct sunxi_nfc
*nfc
= to_sunxi_nfc(nand
->controller
);
978 struct nand_ecc_ctrl
*ecc
= &nand
->ecc
;
981 if (data_off
!= *cur_off
)
982 nand
->cmdfunc(mtd
, NAND_CMD_RNDIN
, data_off
, -1);
984 sunxi_nfc_randomizer_write_buf(mtd
, data
, ecc
->size
, false, page
);
986 if (data_off
+ ecc
->size
!= oob_off
)
987 nand
->cmdfunc(mtd
, NAND_CMD_RNDIN
, oob_off
, -1);
989 ret
= sunxi_nfc_wait_cmd_fifo_empty(nfc
);
993 sunxi_nfc_randomizer_enable(mtd
);
994 sunxi_nfc_hw_ecc_set_prot_oob_bytes(mtd
, oob
, 0, bbm
, page
);
996 writel(NFC_DATA_TRANS
| NFC_DATA_SWAP_METHOD
|
997 NFC_ACCESS_DIR
| NFC_ECC_OP
,
998 nfc
->regs
+ NFC_REG_CMD
);
1000 ret
= sunxi_nfc_wait_events(nfc
, NFC_CMD_INT_FLAG
, true, 0);
1001 sunxi_nfc_randomizer_disable(mtd
);
1005 *cur_off
= oob_off
+ ecc
->bytes
+ 4;
1010 static void sunxi_nfc_hw_ecc_write_extra_oob(struct mtd_info
*mtd
,
1011 u8
*oob
, int *cur_off
,
1014 struct nand_chip
*nand
= mtd_to_nand(mtd
);
1015 struct nand_ecc_ctrl
*ecc
= &nand
->ecc
;
1016 int offset
= ((ecc
->bytes
+ 4) * ecc
->steps
);
1017 int len
= mtd
->oobsize
- offset
;
1022 if (!cur_off
|| *cur_off
!= offset
)
1023 nand
->cmdfunc(mtd
, NAND_CMD_RNDIN
,
1024 offset
+ mtd
->writesize
, -1);
1026 sunxi_nfc_randomizer_write_buf(mtd
, oob
+ offset
, len
, false, page
);
1029 *cur_off
= mtd
->oobsize
+ mtd
->writesize
;
1032 static int sunxi_nfc_hw_ecc_read_page(struct mtd_info
*mtd
,
1033 struct nand_chip
*chip
, uint8_t *buf
,
1034 int oob_required
, int page
)
1036 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1037 unsigned int max_bitflips
= 0;
1038 int ret
, i
, cur_off
= 0;
1039 bool raw_mode
= false;
1041 sunxi_nfc_hw_ecc_enable(mtd
);
1043 for (i
= 0; i
< ecc
->steps
; i
++) {
1044 int data_off
= i
* ecc
->size
;
1045 int oob_off
= i
* (ecc
->bytes
+ 4);
1046 u8
*data
= buf
+ data_off
;
1047 u8
*oob
= chip
->oob_poi
+ oob_off
;
1049 ret
= sunxi_nfc_hw_ecc_read_chunk(mtd
, data
, data_off
, oob
,
1050 oob_off
+ mtd
->writesize
,
1051 &cur_off
, &max_bitflips
,
1052 !i
, oob_required
, page
);
1060 sunxi_nfc_hw_ecc_read_extra_oob(mtd
, chip
->oob_poi
, &cur_off
,
1063 sunxi_nfc_hw_ecc_disable(mtd
);
1065 return max_bitflips
;
1068 static int sunxi_nfc_hw_ecc_read_subpage(struct mtd_info
*mtd
,
1069 struct nand_chip
*chip
,
1070 u32 data_offs
, u32 readlen
,
1071 u8
*bufpoi
, int page
)
1073 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1074 int ret
, i
, cur_off
= 0;
1075 unsigned int max_bitflips
= 0;
1077 sunxi_nfc_hw_ecc_enable(mtd
);
1079 chip
->cmdfunc(mtd
, NAND_CMD_READ0
, 0, page
);
1080 for (i
= data_offs
/ ecc
->size
;
1081 i
< DIV_ROUND_UP(data_offs
+ readlen
, ecc
->size
); i
++) {
1082 int data_off
= i
* ecc
->size
;
1083 int oob_off
= i
* (ecc
->bytes
+ 4);
1084 u8
*data
= bufpoi
+ data_off
;
1085 u8
*oob
= chip
->oob_poi
+ oob_off
;
1087 ret
= sunxi_nfc_hw_ecc_read_chunk(mtd
, data
, data_off
,
1089 oob_off
+ mtd
->writesize
,
1090 &cur_off
, &max_bitflips
, !i
,
1096 sunxi_nfc_hw_ecc_disable(mtd
);
1098 return max_bitflips
;
1101 static int sunxi_nfc_hw_ecc_write_page(struct mtd_info
*mtd
,
1102 struct nand_chip
*chip
,
1103 const uint8_t *buf
, int oob_required
,
1106 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1107 int ret
, i
, cur_off
= 0;
1109 sunxi_nfc_hw_ecc_enable(mtd
);
1111 for (i
= 0; i
< ecc
->steps
; i
++) {
1112 int data_off
= i
* ecc
->size
;
1113 int oob_off
= i
* (ecc
->bytes
+ 4);
1114 const u8
*data
= buf
+ data_off
;
1115 const u8
*oob
= chip
->oob_poi
+ oob_off
;
1117 ret
= sunxi_nfc_hw_ecc_write_chunk(mtd
, data
, data_off
, oob
,
1118 oob_off
+ mtd
->writesize
,
1119 &cur_off
, !i
, page
);
1124 if (oob_required
|| (chip
->options
& NAND_NEED_SCRAMBLING
))
1125 sunxi_nfc_hw_ecc_write_extra_oob(mtd
, chip
->oob_poi
,
1128 sunxi_nfc_hw_ecc_disable(mtd
);
1133 static int sunxi_nfc_hw_syndrome_ecc_read_page(struct mtd_info
*mtd
,
1134 struct nand_chip
*chip
,
1135 uint8_t *buf
, int oob_required
,
1138 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1139 unsigned int max_bitflips
= 0;
1140 int ret
, i
, cur_off
= 0;
1141 bool raw_mode
= false;
1143 sunxi_nfc_hw_ecc_enable(mtd
);
1145 for (i
= 0; i
< ecc
->steps
; i
++) {
1146 int data_off
= i
* (ecc
->size
+ ecc
->bytes
+ 4);
1147 int oob_off
= data_off
+ ecc
->size
;
1148 u8
*data
= buf
+ (i
* ecc
->size
);
1149 u8
*oob
= chip
->oob_poi
+ (i
* (ecc
->bytes
+ 4));
1151 ret
= sunxi_nfc_hw_ecc_read_chunk(mtd
, data
, data_off
, oob
,
1163 sunxi_nfc_hw_ecc_read_extra_oob(mtd
, chip
->oob_poi
, &cur_off
,
1166 sunxi_nfc_hw_ecc_disable(mtd
);
1168 return max_bitflips
;
1171 static int sunxi_nfc_hw_syndrome_ecc_write_page(struct mtd_info
*mtd
,
1172 struct nand_chip
*chip
,
1174 int oob_required
, int page
)
1176 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1177 int ret
, i
, cur_off
= 0;
1179 sunxi_nfc_hw_ecc_enable(mtd
);
1181 for (i
= 0; i
< ecc
->steps
; i
++) {
1182 int data_off
= i
* (ecc
->size
+ ecc
->bytes
+ 4);
1183 int oob_off
= data_off
+ ecc
->size
;
1184 const u8
*data
= buf
+ (i
* ecc
->size
);
1185 const u8
*oob
= chip
->oob_poi
+ (i
* (ecc
->bytes
+ 4));
1187 ret
= sunxi_nfc_hw_ecc_write_chunk(mtd
, data
, data_off
,
1188 oob
, oob_off
, &cur_off
,
1194 if (oob_required
|| (chip
->options
& NAND_NEED_SCRAMBLING
))
1195 sunxi_nfc_hw_ecc_write_extra_oob(mtd
, chip
->oob_poi
,
1198 sunxi_nfc_hw_ecc_disable(mtd
);
1203 static int sunxi_nfc_hw_common_ecc_read_oob(struct mtd_info
*mtd
,
1204 struct nand_chip
*chip
,
1207 chip
->cmdfunc(mtd
, NAND_CMD_READ0
, 0, page
);
1211 return chip
->ecc
.read_page(mtd
, chip
, chip
->buffers
->databuf
, 1, page
);
1214 static int sunxi_nfc_hw_common_ecc_write_oob(struct mtd_info
*mtd
,
1215 struct nand_chip
*chip
,
1220 chip
->cmdfunc(mtd
, NAND_CMD_SEQIN
, 0, page
);
1224 memset(chip
->buffers
->databuf
, 0xff, mtd
->writesize
);
1225 ret
= chip
->ecc
.write_page(mtd
, chip
, chip
->buffers
->databuf
, 1, page
);
1229 /* Send command to program the OOB data */
1230 chip
->cmdfunc(mtd
, NAND_CMD_PAGEPROG
, -1, -1);
1232 status
= chip
->waitfunc(mtd
, chip
);
1234 return status
& NAND_STATUS_FAIL
? -EIO
: 0;
1237 static const s32 tWB_lut
[] = {6, 12, 16, 20};
1238 static const s32 tRHW_lut
[] = {4, 8, 12, 20};
1240 static int _sunxi_nand_lookup_timing(const s32
*lut
, int lut_size
, u32 duration
,
1243 u32 clk_cycles
= DIV_ROUND_UP(duration
, clk_period
);
1246 for (i
= 0; i
< lut_size
; i
++) {
1247 if (clk_cycles
<= lut
[i
])
1255 #define sunxi_nand_lookup_timing(l, p, c) \
1256 _sunxi_nand_lookup_timing(l, ARRAY_SIZE(l), p, c)
1258 static int sunxi_nand_chip_set_timings(struct sunxi_nand_chip
*chip
,
1259 const struct nand_sdr_timings
*timings
)
1261 struct sunxi_nfc
*nfc
= to_sunxi_nfc(chip
->nand
.controller
);
1262 u32 min_clk_period
= 0;
1263 s32 tWB
, tADL
, tWHR
, tRHW
, tCAD
;
1267 if (timings
->tCLS_min
> min_clk_period
)
1268 min_clk_period
= timings
->tCLS_min
;
1271 if (timings
->tCLH_min
> min_clk_period
)
1272 min_clk_period
= timings
->tCLH_min
;
1275 if (timings
->tCS_min
> min_clk_period
)
1276 min_clk_period
= timings
->tCS_min
;
1279 if (timings
->tCH_min
> min_clk_period
)
1280 min_clk_period
= timings
->tCH_min
;
1283 if (timings
->tWP_min
> min_clk_period
)
1284 min_clk_period
= timings
->tWP_min
;
1287 if (timings
->tWH_min
> min_clk_period
)
1288 min_clk_period
= timings
->tWH_min
;
1291 if (timings
->tALS_min
> min_clk_period
)
1292 min_clk_period
= timings
->tALS_min
;
1295 if (timings
->tDS_min
> min_clk_period
)
1296 min_clk_period
= timings
->tDS_min
;
1299 if (timings
->tDH_min
> min_clk_period
)
1300 min_clk_period
= timings
->tDH_min
;
1303 if (timings
->tRR_min
> (min_clk_period
* 3))
1304 min_clk_period
= DIV_ROUND_UP(timings
->tRR_min
, 3);
1307 if (timings
->tALH_min
> min_clk_period
)
1308 min_clk_period
= timings
->tALH_min
;
1311 if (timings
->tRP_min
> min_clk_period
)
1312 min_clk_period
= timings
->tRP_min
;
1315 if (timings
->tREH_min
> min_clk_period
)
1316 min_clk_period
= timings
->tREH_min
;
1319 if (timings
->tRC_min
> (min_clk_period
* 2))
1320 min_clk_period
= DIV_ROUND_UP(timings
->tRC_min
, 2);
1323 if (timings
->tWC_min
> (min_clk_period
* 2))
1324 min_clk_period
= DIV_ROUND_UP(timings
->tWC_min
, 2);
1326 /* T16 - T19 + tCAD */
1327 if (timings
->tWB_max
> (min_clk_period
* 20))
1328 min_clk_period
= DIV_ROUND_UP(timings
->tWB_max
, 20);
1330 if (timings
->tADL_min
> (min_clk_period
* 32))
1331 min_clk_period
= DIV_ROUND_UP(timings
->tADL_min
, 32);
1333 if (timings
->tWHR_min
> (min_clk_period
* 32))
1334 min_clk_period
= DIV_ROUND_UP(timings
->tWHR_min
, 32);
1336 if (timings
->tRHW_min
> (min_clk_period
* 20))
1337 min_clk_period
= DIV_ROUND_UP(timings
->tRHW_min
, 20);
1339 tWB
= sunxi_nand_lookup_timing(tWB_lut
, timings
->tWB_max
,
1342 dev_err(nfc
->dev
, "unsupported tWB\n");
1346 tADL
= DIV_ROUND_UP(timings
->tADL_min
, min_clk_period
) >> 3;
1348 dev_err(nfc
->dev
, "unsupported tADL\n");
1352 tWHR
= DIV_ROUND_UP(timings
->tWHR_min
, min_clk_period
) >> 3;
1354 dev_err(nfc
->dev
, "unsupported tWHR\n");
1358 tRHW
= sunxi_nand_lookup_timing(tRHW_lut
, timings
->tRHW_min
,
1361 dev_err(nfc
->dev
, "unsupported tRHW\n");
1366 * TODO: according to ONFI specs this value only applies for DDR NAND,
1367 * but Allwinner seems to set this to 0x7. Mimic them for now.
1371 /* TODO: A83 has some more bits for CDQSS, CS, CLHZ, CCS, WC */
1372 chip
->timing_cfg
= NFC_TIMING_CFG(tWB
, tADL
, tWHR
, tRHW
, tCAD
);
1374 /* Convert min_clk_period from picoseconds to nanoseconds */
1375 min_clk_period
= DIV_ROUND_UP(min_clk_period
, 1000);
1378 * Unlike what is stated in Allwinner datasheet, the clk_rate should
1379 * be set to (1 / min_clk_period), and not (2 / min_clk_period).
1380 * This new formula was verified with a scope and validated by
1381 * Allwinner engineers.
1383 chip
->clk_rate
= NSEC_PER_SEC
/ min_clk_period
;
1384 real_clk_rate
= clk_round_rate(nfc
->mod_clk
, chip
->clk_rate
);
1387 * ONFI specification 3.1, paragraph 4.15.2 dictates that EDO data
1388 * output cycle timings shall be used if the host drives tRC less than
1391 min_clk_period
= NSEC_PER_SEC
/ real_clk_rate
;
1392 chip
->timing_ctl
= ((min_clk_period
* 2) < 30) ?
1393 NFC_TIMING_CTL_EDO
: 0;
1398 static int sunxi_nand_chip_init_timings(struct sunxi_nand_chip
*chip
,
1399 struct device_node
*np
)
1401 struct mtd_info
*mtd
= nand_to_mtd(&chip
->nand
);
1402 const struct nand_sdr_timings
*timings
;
1406 mode
= onfi_get_async_timing_mode(&chip
->nand
);
1407 if (mode
== ONFI_TIMING_MODE_UNKNOWN
) {
1408 mode
= chip
->nand
.onfi_timing_mode_default
;
1410 uint8_t feature
[ONFI_SUBFEATURE_PARAM_LEN
] = {};
1413 mode
= fls(mode
) - 1;
1418 for (i
= 0; i
< chip
->nsels
; i
++) {
1419 chip
->nand
.select_chip(mtd
, i
);
1420 ret
= chip
->nand
.onfi_set_features(mtd
, &chip
->nand
,
1421 ONFI_FEATURE_ADDR_TIMING_MODE
,
1423 chip
->nand
.select_chip(mtd
, -1);
1429 timings
= onfi_async_timing_mode_to_sdr_timings(mode
);
1430 if (IS_ERR(timings
))
1431 return PTR_ERR(timings
);
1433 return sunxi_nand_chip_set_timings(chip
, timings
);
1436 static int sunxi_nand_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
1437 struct mtd_oob_region
*oobregion
)
1439 struct nand_chip
*nand
= mtd_to_nand(mtd
);
1440 struct nand_ecc_ctrl
*ecc
= &nand
->ecc
;
1442 if (section
>= ecc
->steps
)
1445 oobregion
->offset
= section
* (ecc
->bytes
+ 4) + 4;
1446 oobregion
->length
= ecc
->bytes
;
1451 static int sunxi_nand_ooblayout_free(struct mtd_info
*mtd
, int section
,
1452 struct mtd_oob_region
*oobregion
)
1454 struct nand_chip
*nand
= mtd_to_nand(mtd
);
1455 struct nand_ecc_ctrl
*ecc
= &nand
->ecc
;
1457 if (section
> ecc
->steps
)
1461 * The first 2 bytes are used for BB markers, hence we
1462 * only have 2 bytes available in the first user data
1465 if (!section
&& ecc
->mode
== NAND_ECC_HW
) {
1466 oobregion
->offset
= 2;
1467 oobregion
->length
= 2;
1472 oobregion
->offset
= section
* (ecc
->bytes
+ 4);
1474 if (section
< ecc
->steps
)
1475 oobregion
->length
= 4;
1477 oobregion
->offset
= mtd
->oobsize
- oobregion
->offset
;
1482 static const struct mtd_ooblayout_ops sunxi_nand_ooblayout_ops
= {
1483 .ecc
= sunxi_nand_ooblayout_ecc
,
1484 .free
= sunxi_nand_ooblayout_free
,
1487 static int sunxi_nand_hw_common_ecc_ctrl_init(struct mtd_info
*mtd
,
1488 struct nand_ecc_ctrl
*ecc
,
1489 struct device_node
*np
)
1491 static const u8 strengths
[] = { 16, 24, 28, 32, 40, 48, 56, 60, 64 };
1492 struct nand_chip
*nand
= mtd_to_nand(mtd
);
1493 struct sunxi_nand_chip
*sunxi_nand
= to_sunxi_nand(nand
);
1494 struct sunxi_nfc
*nfc
= to_sunxi_nfc(sunxi_nand
->nand
.controller
);
1495 struct sunxi_nand_hw_ecc
*data
;
1500 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1504 /* Add ECC info retrieval from DT */
1505 for (i
= 0; i
< ARRAY_SIZE(strengths
); i
++) {
1506 if (ecc
->strength
<= strengths
[i
])
1510 if (i
>= ARRAY_SIZE(strengths
)) {
1511 dev_err(nfc
->dev
, "unsupported strength\n");
1518 /* HW ECC always request ECC bytes for 1024 bytes blocks */
1519 ecc
->bytes
= DIV_ROUND_UP(ecc
->strength
* fls(8 * 1024), 8);
1521 /* HW ECC always work with even numbers of ECC bytes */
1522 ecc
->bytes
= ALIGN(ecc
->bytes
, 2);
1524 nsectors
= mtd
->writesize
/ ecc
->size
;
1526 if (mtd
->oobsize
< ((ecc
->bytes
+ 4) * nsectors
)) {
1531 ecc
->read_oob
= sunxi_nfc_hw_common_ecc_read_oob
;
1532 ecc
->write_oob
= sunxi_nfc_hw_common_ecc_write_oob
;
1533 mtd_set_ooblayout(mtd
, &sunxi_nand_ooblayout_ops
);
1544 static void sunxi_nand_hw_common_ecc_ctrl_cleanup(struct nand_ecc_ctrl
*ecc
)
1549 static int sunxi_nand_hw_ecc_ctrl_init(struct mtd_info
*mtd
,
1550 struct nand_ecc_ctrl
*ecc
,
1551 struct device_node
*np
)
1555 ret
= sunxi_nand_hw_common_ecc_ctrl_init(mtd
, ecc
, np
);
1559 ecc
->read_page
= sunxi_nfc_hw_ecc_read_page
;
1560 ecc
->write_page
= sunxi_nfc_hw_ecc_write_page
;
1561 ecc
->read_oob_raw
= nand_read_oob_std
;
1562 ecc
->write_oob_raw
= nand_write_oob_std
;
1563 ecc
->read_subpage
= sunxi_nfc_hw_ecc_read_subpage
;
1568 static int sunxi_nand_hw_syndrome_ecc_ctrl_init(struct mtd_info
*mtd
,
1569 struct nand_ecc_ctrl
*ecc
,
1570 struct device_node
*np
)
1574 ret
= sunxi_nand_hw_common_ecc_ctrl_init(mtd
, ecc
, np
);
1579 ecc
->read_page
= sunxi_nfc_hw_syndrome_ecc_read_page
;
1580 ecc
->write_page
= sunxi_nfc_hw_syndrome_ecc_write_page
;
1581 ecc
->read_oob_raw
= nand_read_oob_syndrome
;
1582 ecc
->write_oob_raw
= nand_write_oob_syndrome
;
1587 static void sunxi_nand_ecc_cleanup(struct nand_ecc_ctrl
*ecc
)
1589 switch (ecc
->mode
) {
1591 case NAND_ECC_HW_SYNDROME
:
1592 sunxi_nand_hw_common_ecc_ctrl_cleanup(ecc
);
1600 static int sunxi_nand_ecc_init(struct mtd_info
*mtd
, struct nand_ecc_ctrl
*ecc
,
1601 struct device_node
*np
)
1603 struct nand_chip
*nand
= mtd_to_nand(mtd
);
1607 ecc
->size
= nand
->ecc_step_ds
;
1608 ecc
->strength
= nand
->ecc_strength_ds
;
1611 if (!ecc
->size
|| !ecc
->strength
)
1614 switch (ecc
->mode
) {
1616 ret
= sunxi_nand_hw_ecc_ctrl_init(mtd
, ecc
, np
);
1620 case NAND_ECC_HW_SYNDROME
:
1621 ret
= sunxi_nand_hw_syndrome_ecc_ctrl_init(mtd
, ecc
, np
);
1635 static int sunxi_nand_chip_init(struct device
*dev
, struct sunxi_nfc
*nfc
,
1636 struct device_node
*np
)
1638 const struct nand_sdr_timings
*timings
;
1639 struct sunxi_nand_chip
*chip
;
1640 struct mtd_info
*mtd
;
1641 struct nand_chip
*nand
;
1647 if (!of_get_property(np
, "reg", &nsels
))
1650 nsels
/= sizeof(u32
);
1652 dev_err(dev
, "invalid reg property size\n");
1656 chip
= devm_kzalloc(dev
,
1658 (nsels
* sizeof(struct sunxi_nand_chip_sel
)),
1661 dev_err(dev
, "could not allocate chip\n");
1665 chip
->nsels
= nsels
;
1666 chip
->selected
= -1;
1668 for (i
= 0; i
< nsels
; i
++) {
1669 ret
= of_property_read_u32_index(np
, "reg", i
, &tmp
);
1671 dev_err(dev
, "could not retrieve reg property: %d\n",
1676 if (tmp
> NFC_MAX_CS
) {
1678 "invalid reg value: %u (max CS = 7)\n",
1683 if (test_and_set_bit(tmp
, &nfc
->assigned_cs
)) {
1684 dev_err(dev
, "CS %d already assigned\n", tmp
);
1688 chip
->sels
[i
].cs
= tmp
;
1690 if (!of_property_read_u32_index(np
, "allwinner,rb", i
, &tmp
) &&
1692 chip
->sels
[i
].rb
.type
= RB_NATIVE
;
1693 chip
->sels
[i
].rb
.info
.nativeid
= tmp
;
1695 ret
= of_get_named_gpio(np
, "rb-gpios", i
);
1698 chip
->sels
[i
].rb
.type
= RB_GPIO
;
1699 chip
->sels
[i
].rb
.info
.gpio
= tmp
;
1700 ret
= devm_gpio_request(dev
, tmp
, "nand-rb");
1704 ret
= gpio_direction_input(tmp
);
1708 chip
->sels
[i
].rb
.type
= RB_NONE
;
1714 /* Default tR value specified in the ONFI spec (chapter 4.15.1) */
1715 nand
->chip_delay
= 200;
1716 nand
->controller
= &nfc
->controller
;
1718 * Set the ECC mode to the default value in case nothing is specified
1721 nand
->ecc
.mode
= NAND_ECC_HW
;
1722 nand_set_flash_node(nand
, np
);
1723 nand
->select_chip
= sunxi_nfc_select_chip
;
1724 nand
->cmd_ctrl
= sunxi_nfc_cmd_ctrl
;
1725 nand
->read_buf
= sunxi_nfc_read_buf
;
1726 nand
->write_buf
= sunxi_nfc_write_buf
;
1727 nand
->read_byte
= sunxi_nfc_read_byte
;
1729 mtd
= nand_to_mtd(nand
);
1730 mtd
->dev
.parent
= dev
;
1732 timings
= onfi_async_timing_mode_to_sdr_timings(0);
1733 if (IS_ERR(timings
)) {
1734 ret
= PTR_ERR(timings
);
1736 "could not retrieve timings for ONFI mode 0: %d\n",
1741 ret
= sunxi_nand_chip_set_timings(chip
, timings
);
1743 dev_err(dev
, "could not configure chip timings: %d\n", ret
);
1747 ret
= nand_scan_ident(mtd
, nsels
, NULL
);
1751 if (nand
->bbt_options
& NAND_BBT_USE_FLASH
)
1752 nand
->bbt_options
|= NAND_BBT_NO_OOB
;
1754 if (nand
->options
& NAND_NEED_SCRAMBLING
)
1755 nand
->options
|= NAND_NO_SUBPAGE_WRITE
;
1757 nand
->options
|= NAND_SUBPAGE_READ
;
1759 ret
= sunxi_nand_chip_init_timings(chip
, np
);
1761 dev_err(dev
, "could not configure chip timings: %d\n", ret
);
1765 ret
= sunxi_nand_ecc_init(mtd
, &nand
->ecc
, np
);
1767 dev_err(dev
, "ECC init failed: %d\n", ret
);
1771 ret
= nand_scan_tail(mtd
);
1773 dev_err(dev
, "nand_scan_tail failed: %d\n", ret
);
1777 ret
= mtd_device_register(mtd
, NULL
, 0);
1779 dev_err(dev
, "failed to register mtd device: %d\n", ret
);
1784 list_add_tail(&chip
->node
, &nfc
->chips
);
1789 static int sunxi_nand_chips_init(struct device
*dev
, struct sunxi_nfc
*nfc
)
1791 struct device_node
*np
= dev
->of_node
;
1792 struct device_node
*nand_np
;
1793 int nchips
= of_get_child_count(np
);
1797 dev_err(dev
, "too many NAND chips: %d (max = 8)\n", nchips
);
1801 for_each_child_of_node(np
, nand_np
) {
1802 ret
= sunxi_nand_chip_init(dev
, nfc
, nand_np
);
1804 of_node_put(nand_np
);
1812 static void sunxi_nand_chips_cleanup(struct sunxi_nfc
*nfc
)
1814 struct sunxi_nand_chip
*chip
;
1816 while (!list_empty(&nfc
->chips
)) {
1817 chip
= list_first_entry(&nfc
->chips
, struct sunxi_nand_chip
,
1819 nand_release(nand_to_mtd(&chip
->nand
));
1820 sunxi_nand_ecc_cleanup(&chip
->nand
.ecc
);
1821 list_del(&chip
->node
);
1825 static int sunxi_nfc_probe(struct platform_device
*pdev
)
1827 struct device
*dev
= &pdev
->dev
;
1829 struct sunxi_nfc
*nfc
;
1833 nfc
= devm_kzalloc(dev
, sizeof(*nfc
), GFP_KERNEL
);
1838 spin_lock_init(&nfc
->controller
.lock
);
1839 init_waitqueue_head(&nfc
->controller
.wq
);
1840 INIT_LIST_HEAD(&nfc
->chips
);
1842 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1843 nfc
->regs
= devm_ioremap_resource(dev
, r
);
1844 if (IS_ERR(nfc
->regs
))
1845 return PTR_ERR(nfc
->regs
);
1847 irq
= platform_get_irq(pdev
, 0);
1849 dev_err(dev
, "failed to retrieve irq\n");
1853 nfc
->ahb_clk
= devm_clk_get(dev
, "ahb");
1854 if (IS_ERR(nfc
->ahb_clk
)) {
1855 dev_err(dev
, "failed to retrieve ahb clk\n");
1856 return PTR_ERR(nfc
->ahb_clk
);
1859 ret
= clk_prepare_enable(nfc
->ahb_clk
);
1863 nfc
->mod_clk
= devm_clk_get(dev
, "mod");
1864 if (IS_ERR(nfc
->mod_clk
)) {
1865 dev_err(dev
, "failed to retrieve mod clk\n");
1866 ret
= PTR_ERR(nfc
->mod_clk
);
1867 goto out_ahb_clk_unprepare
;
1870 ret
= clk_prepare_enable(nfc
->mod_clk
);
1872 goto out_ahb_clk_unprepare
;
1874 ret
= sunxi_nfc_rst(nfc
);
1876 goto out_mod_clk_unprepare
;
1878 writel(0, nfc
->regs
+ NFC_REG_INT
);
1879 ret
= devm_request_irq(dev
, irq
, sunxi_nfc_interrupt
,
1880 0, "sunxi-nand", nfc
);
1882 goto out_mod_clk_unprepare
;
1884 platform_set_drvdata(pdev
, nfc
);
1886 ret
= sunxi_nand_chips_init(dev
, nfc
);
1888 dev_err(dev
, "failed to init nand chips\n");
1889 goto out_mod_clk_unprepare
;
1894 out_mod_clk_unprepare
:
1895 clk_disable_unprepare(nfc
->mod_clk
);
1896 out_ahb_clk_unprepare
:
1897 clk_disable_unprepare(nfc
->ahb_clk
);
1902 static int sunxi_nfc_remove(struct platform_device
*pdev
)
1904 struct sunxi_nfc
*nfc
= platform_get_drvdata(pdev
);
1906 sunxi_nand_chips_cleanup(nfc
);
1907 clk_disable_unprepare(nfc
->mod_clk
);
1908 clk_disable_unprepare(nfc
->ahb_clk
);
1913 static const struct of_device_id sunxi_nfc_ids
[] = {
1914 { .compatible
= "allwinner,sun4i-a10-nand" },
1917 MODULE_DEVICE_TABLE(of
, sunxi_nfc_ids
);
1919 static struct platform_driver sunxi_nfc_driver
= {
1921 .name
= "sunxi_nand",
1922 .of_match_table
= sunxi_nfc_ids
,
1924 .probe
= sunxi_nfc_probe
,
1925 .remove
= sunxi_nfc_remove
,
1927 module_platform_driver(sunxi_nfc_driver
);
1929 MODULE_LICENSE("GPL v2");
1930 MODULE_AUTHOR("Boris BREZILLON");
1931 MODULE_DESCRIPTION("Allwinner NAND Flash Controller driver");
1932 MODULE_ALIAS("platform:sunxi_nand");