blob: 25e04974c0e1b747fa7aafcd4d362a4d4ac428e1 [file] [log] [blame]
Yifeng Zhao9e9021e2021-06-07 16:40:29 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Rockchip NAND Flash controller driver.
4 * Copyright (C) 2021 Rockchip Inc.
5 * Author: Yifeng Zhao <yifeng.zhao@rock-chips.com>
6 */
7
8#include <common.h>
9#include <asm/io.h>
10#include <clk.h>
11#include <dm.h>
12#include <dm/device_compat.h>
13#include <dm/devres.h>
14#include <fdtdec.h>
15#include <inttypes.h>
16#include <linux/delay.h>
17#include <linux/dma-direction.h>
18#include <linux/dma-mapping.h>
19#include <linux/io.h>
20#include <linux/kernel.h>
21#include <linux/mtd/mtd.h>
22#include <linux/mtd/nand.h>
23#include <linux/mtd/partitions.h>
Tom Rini3bde7e22021-09-22 14:50:35 -040024#include <linux/mtd/rawnand.h>
Yifeng Zhao9e9021e2021-06-07 16:40:29 +080025#include <memalign.h>
26#include <nand.h>
27
28/*
29 * NFC Page Data Layout:
30 * 1024 bytes data + 4Bytes sys data + 28Bytes~124Bytes ECC data +
31 * 1024 bytes data + 4Bytes sys data + 28Bytes~124Bytes ECC data +
32 * ......
33 * NAND Page Data Layout:
34 * 1024 * n data + m Bytes oob
35 * Original Bad Block Mask Location:
36 * First byte of oob(spare).
37 * nand_chip->oob_poi data layout:
38 * 4Bytes sys data + .... + 4Bytes sys data + ECC data.
39 */
40
41/* NAND controller register definition */
42#define NFC_READ (0)
43#define NFC_WRITE (1)
44
45#define NFC_FMCTL (0x00)
46#define FMCTL_CE_SEL_M 0xFF
47#define FMCTL_CE_SEL(x) (1 << (x))
48#define FMCTL_WP BIT(8)
49#define FMCTL_RDY BIT(9)
50
51#define NFC_FMWAIT (0x04)
52#define FLCTL_RST BIT(0)
53#define FLCTL_WR (1) /* 0: read, 1: write */
54#define FLCTL_XFER_ST BIT(2)
55#define FLCTL_XFER_EN BIT(3)
56#define FLCTL_ACORRECT BIT(10) /* Auto correct error bits. */
57#define FLCTL_XFER_READY BIT(20)
58#define FLCTL_XFER_SECTOR (22)
59#define FLCTL_TOG_FIX BIT(29)
60
61#define BCHCTL_BANK_M (7 << 5)
62#define BCHCTL_BANK (5)
63
64#define DMA_ST BIT(0)
65#define DMA_WR (1) /* 0: write, 1: read */
66#define DMA_EN BIT(2)
67#define DMA_AHB_SIZE (3) /* 0: 1, 1: 2, 2: 4 */
68#define DMA_BURST_SIZE (6) /* 0: 1, 3: 4, 5: 8, 7: 16 */
69#define DMA_INC_NUM (9) /* 1 - 16 */
70
71#define ECC_ERR_CNT(x, e) ((((x) >> (e).low) & (e).low_mask) |\
72 (((x) >> (e).high) & (e).high_mask) << (e).low_bn)
73#define INT_DMA BIT(0)
74#define NFC_BANK (0x800)
75#define NFC_BANK_STEP (0x100)
76#define BANK_DATA (0x00)
77#define BANK_ADDR (0x04)
78#define BANK_CMD (0x08)
79#define NFC_SRAM0 (0x1000)
80#define NFC_SRAM1 (0x1400)
81#define NFC_SRAM_SIZE (0x400)
82#define NFC_TIMEOUT_MS (500)
83#define NFC_MAX_OOB_PER_STEP 128
84#define NFC_MIN_OOB_PER_STEP 64
85#define MAX_DATA_SIZE 0xFFFC
86#define MAX_ADDRESS_CYC 6
87#define NFC_ECC_MAX_MODES 4
88#define NFC_RB_DELAY_US 50
89#define NFC_MAX_PAGE_SIZE (16 * 1024)
90#define NFC_MAX_OOB_SIZE (16 * 128)
91#define NFC_MAX_NSELS (8) /* Some Socs only have 1 or 2 CSs. */
92#define NFC_SYS_DATA_SIZE (4) /* 4 bytes sys data in oob pre 1024 data.*/
93#define RK_DEFAULT_CLOCK_RATE (150 * 1000 * 1000) /* 150 Mhz */
94#define ACCTIMING(csrw, rwpw, rwcs) ((csrw) << 12 | (rwpw) << 5 | (rwcs))
95
96enum nfc_type {
97 NFC_V6,
98 NFC_V8,
99 NFC_V9,
100};
101
102/**
103 * struct rk_ecc_cnt_status: represent a ecc status data.
104 * @err_flag_bit: error flag bit index at register.
105 * @low: ECC count low bit index at register.
106 * @low_mask: mask bit.
107 * @low_bn: ECC count low bit number.
108 * @high: ECC count high bit index at register.
109 * @high_mask: mask bit
110 */
111struct ecc_cnt_status {
112 u8 err_flag_bit;
113 u8 low;
114 u8 low_mask;
115 u8 low_bn;
116 u8 high;
117 u8 high_mask;
118};
119
120/**
121 * @type: NFC version
122 * @ecc_strengths: ECC strengths
123 * @ecc_cfgs: ECC config values
124 * @flctl_off: FLCTL register offset
125 * @bchctl_off: BCHCTL register offset
126 * @dma_data_buf_off: DMA_DATA_BUF register offset
127 * @dma_oob_buf_off: DMA_OOB_BUF register offset
128 * @dma_cfg_off: DMA_CFG register offset
129 * @dma_st_off: DMA_ST register offset
130 * @bch_st_off: BCG_ST register offset
131 * @randmz_off: RANDMZ register offset
132 * @int_en_off: interrupt enable register offset
133 * @int_clr_off: interrupt clean register offset
134 * @int_st_off: interrupt status register offset
135 * @oob0_off: oob0 register offset
136 * @oob1_off: oob1 register offset
137 * @ecc0: represent ECC0 status data
138 * @ecc1: represent ECC1 status data
139 */
140struct nfc_cfg {
141 enum nfc_type type;
142 u8 ecc_strengths[NFC_ECC_MAX_MODES];
143 u32 ecc_cfgs[NFC_ECC_MAX_MODES];
144 u32 flctl_off;
145 u32 bchctl_off;
146 u32 dma_cfg_off;
147 u32 dma_data_buf_off;
148 u32 dma_oob_buf_off;
149 u32 dma_st_off;
150 u32 bch_st_off;
151 u32 randmz_off;
152 u32 int_en_off;
153 u32 int_clr_off;
154 u32 int_st_off;
155 u32 oob0_off;
156 u32 oob1_off;
157 struct ecc_cnt_status ecc0;
158 struct ecc_cnt_status ecc1;
159};
160
161struct rk_nfc_nand_chip {
162 struct nand_chip chip;
163
164 u16 boot_blks;
165 u16 metadata_size;
166 u32 boot_ecc;
167 u32 timing;
168
169 u8 nsels;
170 u8 sels[0];
171 /* Nothing after this field. */
172};
173
174struct rk_nfc {
175 struct nand_hw_control controller;
176 const struct nfc_cfg *cfg;
177 struct udevice *dev;
178
179 struct clk *nfc_clk;
180 struct clk *ahb_clk;
181 void __iomem *regs;
182
183 int selected_bank;
184 u32 band_offset;
185 u32 cur_ecc;
186 u32 cur_timing;
187
188 u8 *page_buf;
189 u32 *oob_buf;
190
191 unsigned long assigned_cs;
192};
193
194static inline struct rk_nfc_nand_chip *rk_nfc_to_rknand(struct nand_chip *chip)
195{
196 return container_of(chip, struct rk_nfc_nand_chip, chip);
197}
198
199static inline u8 *rk_nfc_buf_to_data_ptr(struct nand_chip *chip, const u8 *p, int i)
200{
201 return (u8 *)p + i * chip->ecc.size;
202}
203
204static inline u8 *rk_nfc_buf_to_oob_ptr(struct nand_chip *chip, int i)
205{
206 u8 *poi;
207
208 poi = chip->oob_poi + i * NFC_SYS_DATA_SIZE;
209
210 return poi;
211}
212
213static inline u8 *rk_nfc_buf_to_oob_ecc_ptr(struct nand_chip *chip, int i)
214{
215 struct rk_nfc_nand_chip *rknand = rk_nfc_to_rknand(chip);
216 u8 *poi;
217
218 poi = chip->oob_poi + rknand->metadata_size + chip->ecc.bytes * i;
219
220 return poi;
221}
222
223static inline int rk_nfc_data_len(struct nand_chip *chip)
224{
225 return chip->ecc.size + chip->ecc.bytes + NFC_SYS_DATA_SIZE;
226}
227
228static inline u8 *rk_nfc_data_ptr(struct nand_chip *chip, int i)
229{
230 struct rk_nfc *nfc = nand_get_controller_data(chip);
231
232 return nfc->page_buf + i * rk_nfc_data_len(chip);
233}
234
235static inline u8 *rk_nfc_oob_ptr(struct nand_chip *chip, int i)
236{
237 struct rk_nfc *nfc = nand_get_controller_data(chip);
238
239 return nfc->page_buf + i * rk_nfc_data_len(chip) + chip->ecc.size;
240}
241
242static int rk_nfc_hw_ecc_setup(struct nand_chip *chip, u32 strength)
243{
244 struct rk_nfc *nfc = nand_get_controller_data(chip);
245 u32 reg, i;
246
247 for (i = 0; i < NFC_ECC_MAX_MODES; i++) {
248 if (strength == nfc->cfg->ecc_strengths[i]) {
249 reg = nfc->cfg->ecc_cfgs[i];
250 break;
251 }
252 }
253
254 if (i >= NFC_ECC_MAX_MODES)
255 return -EINVAL;
256
257 writel(reg, nfc->regs + nfc->cfg->bchctl_off);
258
259 /* Save chip ECC setting */
260 nfc->cur_ecc = strength;
261
262 return 0;
263}
264
265static void rk_nfc_select_chip(struct mtd_info *mtd, int cs)
266{
267 struct nand_chip *chip = mtd_to_nand(mtd);
268 struct rk_nfc *nfc = nand_get_controller_data(chip);
269 struct rk_nfc_nand_chip *rknand = rk_nfc_to_rknand(chip);
270 struct nand_ecc_ctrl *ecc = &chip->ecc;
271 u32 val;
272
273 if (cs < 0) {
274 nfc->selected_bank = -1;
275 /* Deselect the currently selected target. */
276 val = readl(nfc->regs + NFC_FMCTL);
277 val &= ~FMCTL_CE_SEL_M;
278 writel(val, nfc->regs + NFC_FMCTL);
279 return;
280 }
281
282 nfc->selected_bank = rknand->sels[cs];
283 nfc->band_offset = NFC_BANK + nfc->selected_bank * NFC_BANK_STEP;
284
285 val = readl(nfc->regs + NFC_FMCTL);
286 val &= ~FMCTL_CE_SEL_M;
287 val |= FMCTL_CE_SEL(nfc->selected_bank);
288
289 writel(val, nfc->regs + NFC_FMCTL);
290
291 /*
292 * Compare current chip timing with selected chip timing and
293 * change if needed.
294 */
295 if (nfc->cur_timing != rknand->timing) {
296 writel(rknand->timing, nfc->regs + NFC_FMWAIT);
297 nfc->cur_timing = rknand->timing;
298 }
299
300 /*
301 * Compare current chip ECC setting with selected chip ECC setting and
302 * change if needed.
303 */
304 if (nfc->cur_ecc != ecc->strength)
305 rk_nfc_hw_ecc_setup(chip, ecc->strength);
306}
307
308static inline int rk_nfc_wait_ioready(struct rk_nfc *nfc)
309{
310 u32 timeout = (CONFIG_SYS_HZ * NFC_TIMEOUT_MS) / 1000;
311 u32 time_start;
312
313 time_start = get_timer(0);
314 do {
315 if (readl(nfc->regs + NFC_FMCTL) & FMCTL_RDY)
316 return 0;
317 } while (get_timer(time_start) < timeout);
318
319 dev_err(nfc->dev, "wait for io ready timedout\n");
320 return -ETIMEDOUT;
321}
322
323static void rk_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
324{
325 struct nand_chip *chip = mtd_to_nand(mtd);
326 struct rk_nfc *nfc = nand_get_controller_data(chip);
327 void __iomem *bank_base;
328 int i = 0;
329
330 bank_base = nfc->regs + nfc->band_offset + BANK_DATA;
331
332 for (i = 0; i < len; i++)
333 buf[i] = readl(bank_base);
334}
335
336static void rk_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
337{
338 struct nand_chip *chip = mtd_to_nand(mtd);
339 struct rk_nfc *nfc = nand_get_controller_data(chip);
340 void __iomem *bank_base;
341 int i = 0;
342
343 bank_base = nfc->regs + nfc->band_offset + BANK_DATA;
344
345 for (i = 0; i < len; i++)
346 writel(buf[i], bank_base);
347}
348
349static void rk_nfc_cmd(struct mtd_info *mtd, int dat, unsigned int ctrl)
350{
351 struct nand_chip *chip = mtd_to_nand(mtd);
352 struct rk_nfc *nfc = nand_get_controller_data(chip);
353 void __iomem *bank_base;
354
355 bank_base = nfc->regs + nfc->band_offset;
356
357 if (ctrl & NAND_CTRL_CHANGE) {
358 if (ctrl & NAND_ALE)
359 bank_base += BANK_ADDR;
360 else if (ctrl & NAND_CLE)
361 bank_base += BANK_CMD;
362 chip->IO_ADDR_W = bank_base;
363 }
364
365 if (dat != NAND_CMD_NONE)
366 writel(dat & 0xFF, chip->IO_ADDR_W);
367}
368
369static uint8_t rockchip_nand_read_byte(struct mtd_info *mtd)
370{
371 uint8_t ret;
372
373 rk_nfc_read_buf(mtd, &ret, 1);
374
375 return ret;
376}
377
378static int rockchip_nand_dev_ready(struct mtd_info *mtd)
379{
380 struct nand_chip *chip = mtd_to_nand(mtd);
381 struct rk_nfc *nfc = nand_get_controller_data(chip);
382
383 if (readl(nfc->regs + NFC_FMCTL) & FMCTL_RDY)
384 return 1;
385
386 return 0;
387}
388
389static void rk_nfc_xfer_start(struct rk_nfc *nfc, u8 rw, u8 n_KB,
390 dma_addr_t dma_data, dma_addr_t dma_oob)
391{
392 u32 dma_reg, fl_reg, bch_reg;
393
394 dma_reg = DMA_ST | ((!rw) << DMA_WR) | DMA_EN | (2 << DMA_AHB_SIZE) |
395 (7 << DMA_BURST_SIZE) | (16 << DMA_INC_NUM);
396
397 fl_reg = (rw << FLCTL_WR) | FLCTL_XFER_EN | FLCTL_ACORRECT |
398 (n_KB << FLCTL_XFER_SECTOR) | FLCTL_TOG_FIX;
399
400 if (nfc->cfg->type == NFC_V6 || nfc->cfg->type == NFC_V8) {
401 bch_reg = readl_relaxed(nfc->regs + nfc->cfg->bchctl_off);
402 bch_reg = (bch_reg & (~BCHCTL_BANK_M)) |
403 (nfc->selected_bank << BCHCTL_BANK);
404 writel(bch_reg, nfc->regs + nfc->cfg->bchctl_off);
405 }
406
407 writel(dma_reg, nfc->regs + nfc->cfg->dma_cfg_off);
408 writel((u32)dma_data, nfc->regs + nfc->cfg->dma_data_buf_off);
409 writel((u32)dma_oob, nfc->regs + nfc->cfg->dma_oob_buf_off);
410 writel(fl_reg, nfc->regs + nfc->cfg->flctl_off);
411 fl_reg |= FLCTL_XFER_ST;
412 writel(fl_reg, nfc->regs + nfc->cfg->flctl_off);
413}
414
415static int rk_nfc_wait_for_xfer_done(struct rk_nfc *nfc)
416{
417 unsigned long timeout = (CONFIG_SYS_HZ * NFC_TIMEOUT_MS) / 1000;
418 void __iomem *ptr = nfc->regs + nfc->cfg->flctl_off;
419 u32 time_start;
420
421 time_start = get_timer(0);
422
423 do {
424 if (readl(ptr) & FLCTL_XFER_READY)
425 return 0;
426 } while (get_timer(time_start) < timeout);
427
428 dev_err(nfc->dev, "wait for io ready timedout\n");
429 return -ETIMEDOUT;
430}
431
432static int rk_nfc_write_page_raw(struct mtd_info *mtd,
433 struct nand_chip *chip,
434 const u8 *buf,
435 int oob_required,
436 int page)
437{
438 struct rk_nfc_nand_chip *rknand = rk_nfc_to_rknand(chip);
439 struct rk_nfc *nfc = nand_get_controller_data(chip);
440 struct nand_ecc_ctrl *ecc = &chip->ecc;
441 int i, pages_per_blk;
442
443 pages_per_blk = mtd->erasesize / mtd->writesize;
444 if ((page < (pages_per_blk * rknand->boot_blks)) &&
445 rknand->boot_ecc != ecc->strength) {
446 /*
447 * There's currently no method to notify the MTD framework that
448 * a different ECC strength is in use for the boot blocks.
449 */
450 return -EIO;
451 }
452
453 if (!buf)
454 memset(nfc->page_buf, 0xff, mtd->writesize + mtd->oobsize);
455
456 for (i = 0; i < ecc->steps; i++) {
457 /* Copy data to the NFC buffer. */
458 if (buf)
459 memcpy(rk_nfc_data_ptr(chip, i),
460 rk_nfc_buf_to_data_ptr(chip, buf, i),
461 ecc->size);
462 /*
463 * The first four bytes of OOB are reserved for the
464 * boot ROM. In some debugging cases, such as with a
465 * read, erase and write back test these 4 bytes stored
466 * in OOB also need to be written back.
467 *
468 * The function nand_block_bad detects bad blocks like:
469 *
470 * bad = chip->oob_poi[chip->badblockpos];
471 *
472 * chip->badblockpos == 0 for a large page NAND Flash,
473 * so chip->oob_poi[0] is the bad block mask (BBM).
474 *
475 * The OOB data layout on the NFC is:
476 *
477 * PA0 PA1 PA2 PA3 | BBM OOB1 OOB2 OOB3 | ...
478 *
479 * or
480 *
481 * 0xFF 0xFF 0xFF 0xFF | BBM OOB1 OOB2 OOB3 | ...
482 *
483 * The code here just swaps the first 4 bytes with the last
484 * 4 bytes without losing any data.
485 *
486 * The chip->oob_poi data layout:
487 *
488 * BBM OOB1 OOB2 OOB3 |......| PA0 PA1 PA2 PA3
489 *
490 * The rk_nfc_ooblayout_free() function already has reserved
491 * these 4 bytes with:
492 *
493 * oob_region->offset = NFC_SYS_DATA_SIZE + 2;
494 */
495 if (!i)
496 memcpy(rk_nfc_oob_ptr(chip, i),
497 rk_nfc_buf_to_oob_ptr(chip, ecc->steps - 1),
498 NFC_SYS_DATA_SIZE);
499 else
500 memcpy(rk_nfc_oob_ptr(chip, i),
501 rk_nfc_buf_to_oob_ptr(chip, i - 1),
502 NFC_SYS_DATA_SIZE);
503 /* Copy ECC data to the NFC buffer. */
504 memcpy(rk_nfc_oob_ptr(chip, i) + NFC_SYS_DATA_SIZE,
505 rk_nfc_buf_to_oob_ecc_ptr(chip, i),
506 ecc->bytes);
507 }
508
509 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
510 rk_nfc_write_buf(mtd, buf, mtd->writesize + mtd->oobsize);
511 return nand_prog_page_end_op(chip);
512}
513
514static int rk_nfc_write_page_hwecc(struct mtd_info *mtd,
515 struct nand_chip *chip,
516 const u8 *buf,
517 int oob_required,
518 int page)
519{
520 struct rk_nfc *nfc = nand_get_controller_data(chip);
521 struct rk_nfc_nand_chip *rknand = rk_nfc_to_rknand(chip);
522 struct nand_ecc_ctrl *ecc = &chip->ecc;
523 int oob_step = (ecc->bytes > 60) ? NFC_MAX_OOB_PER_STEP :
524 NFC_MIN_OOB_PER_STEP;
525 int pages_per_blk = mtd->erasesize / mtd->writesize;
526 int ret = 0, i, boot_rom_mode = 0;
527 dma_addr_t dma_data, dma_oob;
528 u32 reg;
529 u8 *oob;
530
531 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
532
533 if (buf)
534 memcpy(nfc->page_buf, buf, mtd->writesize);
535 else
536 memset(nfc->page_buf, 0xFF, mtd->writesize);
537
538 /*
539 * The first blocks (4, 8 or 16 depending on the device) are used
540 * by the boot ROM and the first 32 bits of OOB need to link to
541 * the next page address in the same block. We can't directly copy
542 * OOB data from the MTD framework, because this page address
543 * conflicts for example with the bad block marker (BBM),
544 * so we shift all OOB data including the BBM with 4 byte positions.
545 * As a consequence the OOB size available to the MTD framework is
546 * also reduced with 4 bytes.
547 *
548 * PA0 PA1 PA2 PA3 | BBM OOB1 OOB2 OOB3 | ...
549 *
550 * If a NAND is not a boot medium or the page is not a boot block,
551 * the first 4 bytes are left untouched by writing 0xFF to them.
552 *
553 * 0xFF 0xFF 0xFF 0xFF | BBM OOB1 OOB2 OOB3 | ...
554 *
555 * Configure the ECC algorithm supported by the boot ROM.
556 */
557 if (page < (pages_per_blk * rknand->boot_blks)) {
558 boot_rom_mode = 1;
559 if (rknand->boot_ecc != ecc->strength)
560 rk_nfc_hw_ecc_setup(chip, rknand->boot_ecc);
561 }
562
563 for (i = 0; i < ecc->steps; i++) {
564 if (!i) {
565 reg = 0xFFFFFFFF;
566 } else {
567 oob = chip->oob_poi + (i - 1) * NFC_SYS_DATA_SIZE;
568 reg = oob[0] | oob[1] << 8 | oob[2] << 16 |
569 oob[3] << 24;
570 }
571
572 if (!i && boot_rom_mode)
573 reg = (page & (pages_per_blk - 1)) * 4;
574
575 if (nfc->cfg->type == NFC_V9)
576 nfc->oob_buf[i] = reg;
577 else
578 nfc->oob_buf[i * (oob_step / 4)] = reg;
579 }
580
581 dma_data = dma_map_single((void *)nfc->page_buf,
582 mtd->writesize, DMA_TO_DEVICE);
583 dma_oob = dma_map_single(nfc->oob_buf,
584 ecc->steps * oob_step,
585 DMA_TO_DEVICE);
586
587 rk_nfc_xfer_start(nfc, NFC_WRITE, ecc->steps, dma_data,
588 dma_oob);
589 ret = rk_nfc_wait_for_xfer_done(nfc);
590
591 dma_unmap_single(dma_data, mtd->writesize,
592 DMA_TO_DEVICE);
593 dma_unmap_single(dma_oob, ecc->steps * oob_step,
594 DMA_TO_DEVICE);
595
596 if (boot_rom_mode && rknand->boot_ecc != ecc->strength)
597 rk_nfc_hw_ecc_setup(chip, ecc->strength);
598
599 if (ret) {
600 dev_err(nfc->dev, "write: wait transfer done timeout.\n");
601 return -ETIMEDOUT;
602 }
603
604 return nand_prog_page_end_op(chip);
605}
606
607static int rk_nfc_write_oob(struct mtd_info *mtd,
608 struct nand_chip *chip, int page)
609{
610 return rk_nfc_write_page_hwecc(mtd, chip, NULL, 1, page);
611}
612
613static int rk_nfc_read_page_raw(struct mtd_info *mtd,
614 struct nand_chip *chip,
615 u8 *buf,
616 int oob_required,
617 int page)
618{
619 struct rk_nfc_nand_chip *rknand = rk_nfc_to_rknand(chip);
620 struct rk_nfc *nfc = nand_get_controller_data(chip);
621 struct nand_ecc_ctrl *ecc = &chip->ecc;
622 int i, pages_per_blk;
623
624 pages_per_blk = mtd->erasesize / mtd->writesize;
625 if ((page < (pages_per_blk * rknand->boot_blks)) &&
626 nfc->selected_bank == 0 &&
627 rknand->boot_ecc != ecc->strength) {
628 /*
629 * There's currently no method to notify the MTD framework that
630 * a different ECC strength is in use for the boot blocks.
631 */
632 return -EIO;
633 }
634
635 nand_read_page_op(chip, page, 0, NULL, 0);
636 rk_nfc_read_buf(mtd, nfc->page_buf, mtd->writesize + mtd->oobsize);
637 for (i = 0; i < ecc->steps; i++) {
638 /*
639 * The first four bytes of OOB are reserved for the
640 * boot ROM. In some debugging cases, such as with a read,
641 * erase and write back test, these 4 bytes also must be
642 * saved somewhere, otherwise this information will be
643 * lost during a write back.
644 */
645 if (!i)
646 memcpy(rk_nfc_buf_to_oob_ptr(chip, ecc->steps - 1),
647 rk_nfc_oob_ptr(chip, i),
648 NFC_SYS_DATA_SIZE);
649 else
650 memcpy(rk_nfc_buf_to_oob_ptr(chip, i - 1),
651 rk_nfc_oob_ptr(chip, i),
652 NFC_SYS_DATA_SIZE);
653
654 /* Copy ECC data from the NFC buffer. */
655 memcpy(rk_nfc_buf_to_oob_ecc_ptr(chip, i),
656 rk_nfc_oob_ptr(chip, i) + NFC_SYS_DATA_SIZE,
657 ecc->bytes);
658
659 /* Copy data from the NFC buffer. */
660 if (buf)
661 memcpy(rk_nfc_buf_to_data_ptr(chip, buf, i),
662 rk_nfc_data_ptr(chip, i),
663 ecc->size);
664 }
665
666 return 0;
667}
668
669static int rk_nfc_read_page_hwecc(struct mtd_info *mtd,
670 struct nand_chip *chip,
671 u8 *buf,
672 int oob_required,
673 int page)
674{
675 struct rk_nfc *nfc = nand_get_controller_data(chip);
676 struct rk_nfc_nand_chip *rknand = rk_nfc_to_rknand(chip);
677 struct nand_ecc_ctrl *ecc = &chip->ecc;
678 int oob_step = (ecc->bytes > 60) ? NFC_MAX_OOB_PER_STEP :
679 NFC_MIN_OOB_PER_STEP;
680 int pages_per_blk = mtd->erasesize / mtd->writesize;
681 dma_addr_t dma_data, dma_oob;
682 int ret = 0, i, cnt, boot_rom_mode = 0;
683 int max_bitflips = 0, bch_st, ecc_fail = 0;
684 u8 *oob;
685 u32 tmp;
686
687 nand_read_page_op(chip, page, 0, NULL, 0);
688
689 dma_data = dma_map_single(nfc->page_buf,
690 mtd->writesize,
691 DMA_FROM_DEVICE);
692 dma_oob = dma_map_single(nfc->oob_buf,
693 ecc->steps * oob_step,
694 DMA_FROM_DEVICE);
695
696 /*
697 * The first blocks (4, 8 or 16 depending on the device)
698 * are used by the boot ROM.
699 * Configure the ECC algorithm supported by the boot ROM.
700 */
701 if (page < (pages_per_blk * rknand->boot_blks) &&
702 nfc->selected_bank == 0) {
703 boot_rom_mode = 1;
704 if (rknand->boot_ecc != ecc->strength)
705 rk_nfc_hw_ecc_setup(chip, rknand->boot_ecc);
706 }
707
708 rk_nfc_xfer_start(nfc, NFC_READ, ecc->steps, dma_data,
709 dma_oob);
710 ret = rk_nfc_wait_for_xfer_done(nfc);
711
712 dma_unmap_single(dma_data, mtd->writesize,
713 DMA_FROM_DEVICE);
714 dma_unmap_single(dma_oob, ecc->steps * oob_step,
715 DMA_FROM_DEVICE);
716
717 if (ret) {
718 ret = -ETIMEDOUT;
719 dev_err(nfc->dev, "read: wait transfer done timeout.\n");
720 goto timeout_err;
721 }
722
723 for (i = 1; i < ecc->steps; i++) {
724 oob = chip->oob_poi + (i - 1) * NFC_SYS_DATA_SIZE;
725 if (nfc->cfg->type == NFC_V9)
726 tmp = nfc->oob_buf[i];
727 else
728 tmp = nfc->oob_buf[i * (oob_step / 4)];
729 *oob++ = (u8)tmp;
730 *oob++ = (u8)(tmp >> 8);
731 *oob++ = (u8)(tmp >> 16);
732 *oob++ = (u8)(tmp >> 24);
733 }
734
735 for (i = 0; i < (ecc->steps / 2); i++) {
736 bch_st = readl_relaxed(nfc->regs +
737 nfc->cfg->bch_st_off + i * 4);
738 if (bch_st & BIT(nfc->cfg->ecc0.err_flag_bit) ||
739 bch_st & BIT(nfc->cfg->ecc1.err_flag_bit)) {
740 mtd->ecc_stats.failed++;
741 ecc_fail = 1;
742 } else {
743 cnt = ECC_ERR_CNT(bch_st, nfc->cfg->ecc0);
744 mtd->ecc_stats.corrected += cnt;
745 max_bitflips = max_t(u32, max_bitflips, cnt);
746
747 cnt = ECC_ERR_CNT(bch_st, nfc->cfg->ecc1);
748 mtd->ecc_stats.corrected += cnt;
749 max_bitflips = max_t(u32, max_bitflips, cnt);
750 }
751 }
752
753 if (buf)
754 memcpy(buf, nfc->page_buf, mtd->writesize);
755
756timeout_err:
757 if (boot_rom_mode && rknand->boot_ecc != ecc->strength)
758 rk_nfc_hw_ecc_setup(chip, ecc->strength);
759
760 if (ret)
761 return ret;
762
763 if (ecc_fail) {
764 dev_err(nfc->dev, "read page: %x ecc error!\n", page);
765 return 0;
766 }
767
768 return max_bitflips;
769}
770
771static int rk_nfc_read_oob(struct mtd_info *mtd,
772 struct nand_chip *chip, int page)
773{
774 return rk_nfc_read_page_hwecc(mtd, chip, NULL, 1, page);
775}
776
777static inline void rk_nfc_hw_init(struct rk_nfc *nfc)
778{
779 /* Disable flash wp. */
780 writel(FMCTL_WP, nfc->regs + NFC_FMCTL);
781 /* Config default timing 40ns at 150 Mhz NFC clock. */
782 writel(0x1081, nfc->regs + NFC_FMWAIT);
783 nfc->cur_timing = 0x1081;
784 /* Disable randomizer and DMA. */
785 writel(0, nfc->regs + nfc->cfg->randmz_off);
786 writel(0, nfc->regs + nfc->cfg->dma_cfg_off);
787 writel(FLCTL_RST, nfc->regs + nfc->cfg->flctl_off);
788}
789
790static int rk_nfc_enable_clks(struct udevice *dev, struct rk_nfc *nfc)
791{
792 int ret;
793
794 if (!IS_ERR(nfc->nfc_clk)) {
795 ret = clk_prepare_enable(nfc->nfc_clk);
796 if (ret)
797 dev_err(dev, "failed to enable NFC clk\n");
798 }
799
800 ret = clk_prepare_enable(nfc->ahb_clk);
801 if (ret) {
802 dev_err(dev, "failed to enable ahb clk\n");
803 if (!IS_ERR(nfc->nfc_clk))
804 clk_disable_unprepare(nfc->nfc_clk);
805 }
806
807 return 0;
808}
809
810static void rk_nfc_disable_clks(struct rk_nfc *nfc)
811{
812 if (!IS_ERR(nfc->nfc_clk))
813 clk_disable_unprepare(nfc->nfc_clk);
814 clk_disable_unprepare(nfc->ahb_clk);
815}
816
Yifeng Zhao9e9021e2021-06-07 16:40:29 +0800817static int rk_nfc_ecc_init(struct rk_nfc *nfc, struct nand_chip *chip)
818{
Johan Jonker65c43462023-03-13 01:28:39 +0100819 struct rk_nfc_nand_chip *rknand = rk_nfc_to_rknand(chip);
Yifeng Zhao9e9021e2021-06-07 16:40:29 +0800820 const u8 *strengths = nfc->cfg->ecc_strengths;
821 struct mtd_info *mtd = nand_to_mtd(chip);
822 struct nand_ecc_ctrl *ecc = &chip->ecc;
823 u8 max_strength, nfc_max_strength;
824 int i;
825
826 nfc_max_strength = nfc->cfg->ecc_strengths[0];
827 /* If optional dt settings not present. */
828 if (!ecc->size || !ecc->strength ||
829 ecc->strength > nfc_max_strength) {
830 chip->ecc.size = 1024;
831 ecc->steps = mtd->writesize / ecc->size;
832
833 /*
834 * HW ECC always requests the number of ECC bytes per 1024 byte
835 * blocks. The first 4 OOB bytes are reserved for sys data.
836 */
837 max_strength = ((mtd->oobsize / ecc->steps) - 4) * 8 /
838 fls(8 * 1024);
839 if (max_strength > nfc_max_strength)
840 max_strength = nfc_max_strength;
841
842 for (i = 0; i < 4; i++) {
843 if (max_strength >= strengths[i])
844 break;
845 }
846
847 if (i >= 4) {
848 dev_err(nfc->dev, "unsupported ECC strength\n");
849 return -EOPNOTSUPP;
850 }
851
852 ecc->strength = strengths[i];
853 }
854 ecc->steps = mtd->writesize / ecc->size;
855 ecc->bytes = DIV_ROUND_UP(ecc->strength * fls(8 * chip->ecc.size), 8);
856
Johan Jonker65c43462023-03-13 01:28:39 +0100857 if (ecc->bytes * ecc->steps > mtd->oobsize - rknand->metadata_size)
858 return -EINVAL;
859
860 ecc->layout = kzalloc(sizeof(*ecc->layout), GFP_KERNEL);
861 if (!ecc->layout)
862 return -ENOMEM;
863
864 ecc->layout->eccbytes = ecc->bytes * ecc->steps;
865
866 for (i = 0; i < ecc->layout->eccbytes; i++)
867 ecc->layout->eccpos[i] = rknand->metadata_size + i;
868
869 ecc->layout->oobfree[0].length = rknand->metadata_size - NFC_SYS_DATA_SIZE - 2;
870 ecc->layout->oobfree[0].offset = NFC_SYS_DATA_SIZE + 2;
871
Yifeng Zhao9e9021e2021-06-07 16:40:29 +0800872 return 0;
873}
874
875static int rk_nfc_nand_chip_init(ofnode node, struct rk_nfc *nfc, int devnum)
876{
877 struct rk_nfc_nand_chip *rknand;
878 struct udevice *dev = nfc->dev;
879 struct nand_ecc_ctrl *ecc;
880 struct nand_chip *chip;
881 struct mtd_info *mtd;
882 u32 cs[NFC_MAX_NSELS];
883 int nsels, i, ret;
884 u32 tmp;
885
886 if (!ofnode_get_property(node, "reg", &nsels))
887 return -ENODEV;
888 nsels /= sizeof(u32);
889 if (!nsels || nsels > NFC_MAX_NSELS) {
890 dev_err(dev, "invalid reg property size %d\n", nsels);
891 return -EINVAL;
892 }
893
894 rknand = kzalloc(sizeof(*rknand) + nsels * sizeof(u8), GFP_KERNEL);
895 if (!rknand)
896 return -ENOMEM;
897
898 rknand->nsels = nsels;
899 rknand->timing = nfc->cur_timing;
900
901 ret = ofnode_read_u32_array(node, "reg", cs, nsels);
902 if (ret < 0) {
903 dev_err(dev, "Could not retrieve reg property\n");
904 return -EINVAL;
905 }
906
907 for (i = 0; i < nsels; i++) {
908 if (cs[i] >= NFC_MAX_NSELS) {
909 dev_err(dev, "invalid CS: %u\n", cs[i]);
910 return -EINVAL;
911 }
912
913 if (test_and_set_bit(cs[i], &nfc->assigned_cs)) {
914 dev_err(dev, "CS %u already assigned\n", cs[i]);
915 return -EINVAL;
916 }
917
918 rknand->sels[i] = cs[i];
919 }
920
921 chip = &rknand->chip;
922 ecc = &chip->ecc;
923 ecc->mode = NAND_ECC_HW_SYNDROME;
924
925 ret = ofnode_read_u32(node, "nand-ecc-strength", &tmp);
926 ecc->strength = ret ? 0 : tmp;
927
928 ret = ofnode_read_u32(node, "nand-ecc-step-size", &tmp);
929 ecc->size = ret ? 0 : tmp;
930
931 mtd = nand_to_mtd(chip);
932 mtd->owner = THIS_MODULE;
933 mtd->dev->parent = dev;
934
935 nand_set_controller_data(chip, nfc);
936
Johan Jonkerfb1ab9f2023-03-13 01:28:53 +0100937 chip->flash_node = node;
Yifeng Zhao9e9021e2021-06-07 16:40:29 +0800938 chip->chip_delay = NFC_RB_DELAY_US;
939 chip->select_chip = rk_nfc_select_chip;
940 chip->cmd_ctrl = rk_nfc_cmd;
941 chip->read_buf = rk_nfc_read_buf;
942 chip->write_buf = rk_nfc_write_buf;
943 chip->read_byte = rockchip_nand_read_byte;
944 chip->dev_ready = rockchip_nand_dev_ready;
945 chip->controller = &nfc->controller;
946
947 chip->bbt_options = NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
948 chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER;
949
Yifeng Zhao9e9021e2021-06-07 16:40:29 +0800950 rk_nfc_hw_init(nfc);
951 ret = nand_scan_ident(mtd, nsels, NULL);
952 if (ret)
953 return ret;
954
955 ret = rk_nfc_ecc_init(nfc, chip);
956 if (ret) {
957 dev_err(dev, "rk_nfc_ecc_init failed: %d\n", ret);
958 return ret;
959 }
960
961 ret = ofnode_read_u32(node, "rockchip,boot-blks", &tmp);
962 rknand->boot_blks = ret ? 0 : tmp;
963
964 ret = ofnode_read_u32(node, "rockchip,boot-ecc-strength", &tmp);
965 rknand->boot_ecc = ret ? ecc->strength : tmp;
966
967 rknand->metadata_size = NFC_SYS_DATA_SIZE * ecc->steps;
968
969 if (rknand->metadata_size < NFC_SYS_DATA_SIZE + 2) {
970 dev_err(dev,
971 "driver needs at least %d bytes of meta data\n",
972 NFC_SYS_DATA_SIZE + 2);
973 return -EIO;
974 }
975
976 if (!nfc->page_buf) {
977 nfc->page_buf = kzalloc(NFC_MAX_PAGE_SIZE, GFP_KERNEL);
Johan Jonker65c43462023-03-13 01:28:39 +0100978 if (!nfc->page_buf) {
979 kfree(ecc->layout);
Yifeng Zhao9e9021e2021-06-07 16:40:29 +0800980 return -ENOMEM;
Johan Jonker65c43462023-03-13 01:28:39 +0100981 }
Yifeng Zhao9e9021e2021-06-07 16:40:29 +0800982 }
983
984 if (!nfc->oob_buf) {
985 nfc->oob_buf = kzalloc(NFC_MAX_OOB_SIZE, GFP_KERNEL);
986 if (!nfc->oob_buf) {
Johan Jonker65c43462023-03-13 01:28:39 +0100987 kfree(ecc->layout);
Yifeng Zhao9e9021e2021-06-07 16:40:29 +0800988 kfree(nfc->page_buf);
989 nfc->page_buf = NULL;
990 return -ENOMEM;
991 }
992 }
993
994 ecc->read_page = rk_nfc_read_page_hwecc;
995 ecc->read_page_raw = rk_nfc_read_page_raw;
996 ecc->read_oob = rk_nfc_read_oob;
997 ecc->write_page = rk_nfc_write_page_hwecc;
998 ecc->write_page_raw = rk_nfc_write_page_raw;
999 ecc->write_oob = rk_nfc_write_oob;
1000
1001 ret = nand_scan_tail(mtd);
1002 if (ret) {
1003 dev_err(dev, "nand_scan_tail failed: %d\n", ret);
1004 return ret;
1005 }
1006
1007 return nand_register(devnum, mtd);
1008}
1009
1010static int rk_nfc_nand_chips_init(struct udevice *dev, struct rk_nfc *nfc)
1011{
1012 int ret, i = 0;
1013 ofnode child;
1014
1015 ofnode_for_each_subnode(child, dev_ofnode(dev)) {
1016 ret = rk_nfc_nand_chip_init(child, nfc, i++);
1017 if (ret)
1018 return ret;
1019 }
1020
1021 return 0;
1022}
1023
1024static struct nfc_cfg nfc_v6_cfg = {
1025 .type = NFC_V6,
1026 .ecc_strengths = {60, 40, 24, 16},
1027 .ecc_cfgs = {
1028 0x00040011, 0x00040001, 0x00000011, 0x00000001,
1029 },
1030 .flctl_off = 0x08,
1031 .bchctl_off = 0x0C,
1032 .dma_cfg_off = 0x10,
1033 .dma_data_buf_off = 0x14,
1034 .dma_oob_buf_off = 0x18,
1035 .dma_st_off = 0x1C,
1036 .bch_st_off = 0x20,
1037 .randmz_off = 0x150,
1038 .int_en_off = 0x16C,
1039 .int_clr_off = 0x170,
1040 .int_st_off = 0x174,
1041 .oob0_off = 0x200,
1042 .oob1_off = 0x230,
1043 .ecc0 = {
1044 .err_flag_bit = 2,
1045 .low = 3,
1046 .low_mask = 0x1F,
1047 .low_bn = 5,
1048 .high = 27,
1049 .high_mask = 0x1,
1050 },
1051 .ecc1 = {
1052 .err_flag_bit = 15,
1053 .low = 16,
1054 .low_mask = 0x1F,
1055 .low_bn = 5,
1056 .high = 29,
1057 .high_mask = 0x1,
1058 },
1059};
1060
1061static struct nfc_cfg nfc_v8_cfg = {
1062 .type = NFC_V8,
1063 .ecc_strengths = {16, 16, 16, 16},
1064 .ecc_cfgs = {
1065 0x00000001, 0x00000001, 0x00000001, 0x00000001,
1066 },
1067 .flctl_off = 0x08,
1068 .bchctl_off = 0x0C,
1069 .dma_cfg_off = 0x10,
1070 .dma_data_buf_off = 0x14,
1071 .dma_oob_buf_off = 0x18,
1072 .dma_st_off = 0x1C,
1073 .bch_st_off = 0x20,
1074 .randmz_off = 0x150,
1075 .int_en_off = 0x16C,
1076 .int_clr_off = 0x170,
1077 .int_st_off = 0x174,
1078 .oob0_off = 0x200,
1079 .oob1_off = 0x230,
1080 .ecc0 = {
1081 .err_flag_bit = 2,
1082 .low = 3,
1083 .low_mask = 0x1F,
1084 .low_bn = 5,
1085 .high = 27,
1086 .high_mask = 0x1,
1087 },
1088 .ecc1 = {
1089 .err_flag_bit = 15,
1090 .low = 16,
1091 .low_mask = 0x1F,
1092 .low_bn = 5,
1093 .high = 29,
1094 .high_mask = 0x1,
1095 },
1096};
1097
1098static struct nfc_cfg nfc_v9_cfg = {
1099 .type = NFC_V9,
1100 .ecc_strengths = {70, 60, 40, 16},
1101 .ecc_cfgs = {
1102 0x00000001, 0x06000001, 0x04000001, 0x02000001,
1103 },
1104 .flctl_off = 0x10,
1105 .bchctl_off = 0x20,
1106 .dma_cfg_off = 0x30,
1107 .dma_data_buf_off = 0x34,
1108 .dma_oob_buf_off = 0x38,
1109 .dma_st_off = 0x3C,
1110 .bch_st_off = 0x150,
1111 .randmz_off = 0x208,
1112 .int_en_off = 0x120,
1113 .int_clr_off = 0x124,
1114 .int_st_off = 0x128,
1115 .oob0_off = 0x200,
1116 .oob1_off = 0x204,
1117 .ecc0 = {
1118 .err_flag_bit = 2,
1119 .low = 3,
1120 .low_mask = 0x7F,
1121 .low_bn = 7,
1122 .high = 0,
1123 .high_mask = 0x0,
1124 },
1125 .ecc1 = {
1126 .err_flag_bit = 18,
1127 .low = 19,
1128 .low_mask = 0x7F,
1129 .low_bn = 7,
1130 .high = 0,
1131 .high_mask = 0x0,
1132 },
1133};
1134
1135static const struct udevice_id rk_nfc_id_table[] = {
1136 {
1137 .compatible = "rockchip,px30-nfc",
1138 .data = (unsigned long)&nfc_v9_cfg
1139 },
1140 {
1141 .compatible = "rockchip,rk2928-nfc",
1142 .data = (unsigned long)&nfc_v6_cfg
1143 },
1144 {
1145 .compatible = "rockchip,rv1108-nfc",
1146 .data = (unsigned long)&nfc_v8_cfg
1147 },
Yifeng Zhao9e9021e2021-06-07 16:40:29 +08001148 { /* sentinel */ }
1149};
1150
1151static int rk_nfc_probe(struct udevice *dev)
1152{
1153 struct rk_nfc *nfc = dev_get_priv(dev);
1154 int ret = 0;
1155
1156 nfc->cfg = (void *)dev_get_driver_data(dev);
1157 nfc->dev = dev;
1158
Johan Jonkera433de02023-03-13 01:28:06 +01001159 nfc->regs = dev_read_addr_ptr(dev);
1160 if (!nfc->regs) {
1161 ret = -EINVAL;
Yifeng Zhao9e9021e2021-06-07 16:40:29 +08001162 goto release_nfc;
1163 }
1164
1165 nfc->nfc_clk = devm_clk_get(dev, "nfc");
1166 if (IS_ERR(nfc->nfc_clk)) {
1167 dev_dbg(dev, "no NFC clk\n");
1168 /* Some earlier models, such as rk3066, have no NFC clk. */
1169 }
1170
1171 nfc->ahb_clk = devm_clk_get(dev, "ahb");
1172 if (IS_ERR(nfc->ahb_clk)) {
1173 dev_err(dev, "no ahb clk\n");
1174 ret = PTR_ERR(nfc->ahb_clk);
1175 goto release_nfc;
1176 }
1177
1178 ret = rk_nfc_enable_clks(dev, nfc);
1179 if (ret)
1180 goto release_nfc;
1181
1182 spin_lock_init(&nfc->controller.lock);
1183 init_waitqueue_head(&nfc->controller.wq);
1184
1185 rk_nfc_hw_init(nfc);
1186
1187 ret = rk_nfc_nand_chips_init(dev, nfc);
1188 if (ret) {
1189 dev_err(dev, "failed to init NAND chips\n");
1190 goto clk_disable;
1191 }
1192 return 0;
1193
1194clk_disable:
1195 rk_nfc_disable_clks(nfc);
1196release_nfc:
1197 return ret;
1198}
1199
1200U_BOOT_DRIVER(rockchip_nfc) = {
1201 .name = "rockchip_nfc",
1202 .id = UCLASS_MTD,
1203 .of_match = rk_nfc_id_table,
1204 .probe = rk_nfc_probe,
1205 .priv_auto = sizeof(struct rk_nfc),
1206};
1207
1208void board_nand_init(void)
1209{
1210 struct udevice *dev;
1211 int ret;
1212
1213 ret = uclass_get_device_by_driver(UCLASS_MTD,
1214 DM_DRIVER_GET(rockchip_nfc),
1215 &dev);
1216 if (ret && ret != -ENODEV)
1217 log_err("Failed to initialize ROCKCHIP NAND controller. (error %d)\n",
1218 ret);
1219}
1220
1221int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
1222{
1223 struct mtd_info *mtd;
1224 size_t length = size;
1225
1226 mtd = get_nand_dev_by_index(0);
1227 return nand_read_skip_bad(mtd, offs, &length, NULL, size, (u_char *)dst);
1228}
1229
1230void nand_deselect(void) {}