blob: 5d411c4594dc67c2891661b043a2f507fd5ce7c9 [file] [log] [blame]
Arseniy Krasnov55842b42024-02-11 01:39:27 +03001// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2/*
3 * Amlogic Meson Nand Flash Controller Driver
4 *
5 * Copyright (c) 2018 Amlogic, inc.
6 * Author: Liang Yang <liang.yang@amlogic.com>
7 *
8 * Copyright (c) 2023 SaluteDevices, Inc.
9 * Author: Arseniy Krasnov <avkrasnov@salutedevices.com>
10 */
11
12#include <common.h>
13#include <nand.h>
14#include <asm/io.h>
15#include <dm.h>
16#include <dm/device_compat.h>
17#include <dm/ofnode.h>
18#include <dm/uclass.h>
19#include <linux/bug.h>
20#include <linux/clk-provider.h>
21#include <linux/delay.h>
22#include <linux/dma-mapping.h>
23#include <linux/iopoll.h>
24#include <linux/mtd/mtd.h>
25#include <linux/mtd/rawnand.h>
26#include <linux/sizes.h>
27
28#define NFC_CMD_IDLE (0xc << 14)
29#define NFC_CMD_CLE (0x5 << 14)
30#define NFC_CMD_ALE (0x6 << 14)
31#define NFC_CMD_DWR (0x4 << 14)
32#define NFC_CMD_DRD (0x8 << 14)
33#define NFC_CMD_ADL ((0 << 16) | (3 << 20))
34#define NFC_CMD_ADH ((1 << 16) | (3 << 20))
35#define NFC_CMD_AIL ((2 << 16) | (3 << 20))
36#define NFC_CMD_AIH ((3 << 16) | (3 << 20))
37#define NFC_CMD_SEED ((8 << 16) | (3 << 20))
38#define NFC_CMD_M2N ((0 << 17) | (2 << 20))
39#define NFC_CMD_N2M ((1 << 17) | (2 << 20))
40#define NFC_CMD_RB BIT(20)
41#define NFC_CMD_SCRAMBLER_ENABLE BIT(19)
42#define NFC_CMD_SCRAMBLER_DISABLE 0
43#define NFC_CMD_SHORTMODE_DISABLE 0
44#define NFC_CMD_RB_INT BIT(14)
45#define NFC_CMD_RB_INT_NO_PIN ((0xb << 10) | BIT(18) | BIT(16))
46
47#define NFC_CMD_GET_SIZE(x) (((x) >> 22) & GENMASK(4, 0))
48
49#define NFC_REG_CMD 0x00
50#define NFC_REG_CFG 0x04
51#define NFC_REG_DADR 0x08
52#define NFC_REG_IADR 0x0c
53#define NFC_REG_BUF 0x10
54#define NFC_REG_INFO 0x14
55#define NFC_REG_DC 0x18
56#define NFC_REG_ADR 0x1c
57#define NFC_REG_DL 0x20
58#define NFC_REG_DH 0x24
59#define NFC_REG_CADR 0x28
60#define NFC_REG_SADR 0x2c
61#define NFC_REG_PINS 0x30
62#define NFC_REG_VER 0x38
63
64#define CMDRWGEN(cmd_dir, ran, bch, short_mode, page_size, pages) \
65 ( \
66 (cmd_dir) | \
67 (ran) | \
68 ((bch) << 14) | \
69 ((short_mode) << 13) | \
70 (((page_size) & 0x7f) << 6) | \
71 ((pages) & 0x3f) \
72 )
73
74#define GENCMDDADDRL(adl, addr) ((adl) | ((addr) & 0xffff))
75#define GENCMDDADDRH(adh, addr) ((adh) | (((addr) >> 16) & 0xffff))
76#define GENCMDIADDRL(ail, addr) ((ail) | ((addr) & 0xffff))
77#define GENCMDIADDRH(aih, addr) ((aih) | (((addr) >> 16) & 0xffff))
78
79#define DMA_DIR(dir) ((dir) ? NFC_CMD_N2M : NFC_CMD_M2N)
80
81#define ECC_CHECK_RETURN_FF -1
82
83#define NAND_CE0 (0xe << 10)
84#define NAND_CE1 (0xd << 10)
85
86#define DMA_BUSY_TIMEOUT_US 1000000
87#define CMD_DRAIN_TIMEOUT_US 1000
88#define ECC_POLL_TIMEOUT_US 15
89
90#define MAX_CE_NUM 2
91
92/* eMMC clock register, misc control */
93#define CLK_SELECT_NAND BIT(31)
94#define CLK_ALWAYS_ON_NAND BIT(24)
95#define CLK_ENABLE_VALUE 0x245
96
97#define DIRREAD 1
98#define DIRWRITE 0
99
100#define ECC_PARITY_BCH8_512B 14
101#define ECC_COMPLETE BIT(31)
102#define ECC_ERR_CNT(x) (((x) >> 24) & GENMASK(5, 0))
103#define ECC_ZERO_CNT(x) (((x) >> 16) & GENMASK(5, 0))
104#define ECC_UNCORRECTABLE 0x3f
105
106#define PER_INFO_BYTE 8
107
108#define NFC_SEND_CMD(host, cmd) \
109 (writel((cmd), (host)->reg_base + NFC_REG_CMD))
110
111#define NFC_GET_CMD(host) \
112 (readl((host)->reg_base + NFC_REG_CMD))
113
114#define NFC_CMDFIFO_SIZE(host) ((NFC_GET_CMD((host)) >> 22) & GENMASK(4, 0))
115
116#define NFC_CMD_MAKE_IDLE(ce, delay) ((ce) | NFC_CMD_IDLE | ((delay) & 0x3ff))
117#define NFC_CMD_MAKE_DRD(ce, size) ((ce) | NFC_CMD_DRD | (size))
118#define NFC_CMD_MAKE_DWR(ce, data) ((ce) | NFC_CMD_DWR | ((data) & 0xff))
119#define NFC_CMD_MAKE_CLE(ce, cmd_val) ((ce) | NFC_CMD_CLE | ((cmd_val) & 0xff))
120#define NFC_CMD_MAKE_ALE(ce, addr) ((ce) | NFC_CMD_ALE | ((addr) & 0xff))
121
122#define NAND_TWB_TIME_CYCLE 10
123
124#define NFC_DEV_READY_TICK_MAX 5000
125
126/* Both values are recommended by vendor, as the most
127 * tested with almost all SLC NAND flash. Second value
128 * could be calculated dynamically from timing parameters,
129 * but we need both values for initial start of the NAND
130 * controller (e.g. before NAND subsystem processes timings),
131 * so use hardcoded constants.
132 */
133#define NFC_DEFAULT_BUS_CYCLE 6
134#define NFC_DEFAULT_BUS_TIMING 7
135
136#define NFC_SEED_OFFSET 0xc2
137#define NFC_SEED_MASK 0x7fff
138
139#define DMA_ADDR_ALIGN 8
140
141struct meson_nfc_nand_chip {
142 struct list_head node;
143 struct nand_chip nand;
144
145 u32 bch_mode;
146 u8 *data_buf;
147 __le64 *info_buf;
148 u32 nsels;
149 u8 sels[];
150};
151
152struct meson_nfc_param {
153 u32 chip_select;
154 u32 rb_select;
155};
156
157struct meson_nfc {
158 void __iomem *reg_base;
159 void __iomem *reg_clk;
160 struct list_head chips;
161 struct meson_nfc_param param;
162 struct udevice *dev;
163 dma_addr_t daddr;
164 dma_addr_t iaddr;
165 u32 data_bytes;
166 u32 info_bytes;
167 u64 assigned_cs;
168};
169
170struct meson_nand_ecc {
171 u32 bch;
172 u32 strength;
173 u32 size;
174};
175
176enum {
177 NFC_ECC_BCH8_512 = 1,
178 NFC_ECC_BCH8_1K,
179 NFC_ECC_BCH24_1K,
180 NFC_ECC_BCH30_1K,
181 NFC_ECC_BCH40_1K,
182 NFC_ECC_BCH50_1K,
183 NFC_ECC_BCH60_1K,
184};
185
186#define MESON_ECC_DATA(b, s, sz) { .bch = (b), .strength = (s), .size = (sz) }
187
188static struct meson_nand_ecc meson_ecc[] = {
189 MESON_ECC_DATA(NFC_ECC_BCH8_512, 8, 512),
190 MESON_ECC_DATA(NFC_ECC_BCH8_1K, 8, 1024),
191};
192
193static int meson_nand_calc_ecc_bytes(int step_size, int strength)
194{
195 int ecc_bytes;
196
197 if (step_size == 512 && strength == 8)
198 return ECC_PARITY_BCH8_512B;
199
200 ecc_bytes = DIV_ROUND_UP(strength * fls(step_size * 8), 8);
201 ecc_bytes = ALIGN(ecc_bytes, 2);
202
203 return ecc_bytes;
204}
205
206static struct meson_nfc_nand_chip *to_meson_nand(struct nand_chip *nand)
207{
208 return container_of(nand, struct meson_nfc_nand_chip, nand);
209}
210
211static void meson_nfc_nand_select_chip(struct mtd_info *mtd, int chip)
212{
213 struct nand_chip *nand = mtd_to_nand(mtd);
214 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
215 struct meson_nfc *nfc = nand_get_controller_data(nand);
216
217 nfc->param.chip_select = meson_chip->sels[chip] ? NAND_CE1 : NAND_CE0;
218}
219
220static void meson_nfc_cmd_idle(struct meson_nfc *nfc, u32 time)
221{
222 writel(NFC_CMD_MAKE_IDLE(nfc->param.chip_select, time),
223 nfc->reg_base + NFC_REG_CMD);
224}
225
226static void meson_nfc_cmd_seed(const struct meson_nfc *nfc, u32 seed)
227{
228 writel(NFC_CMD_SEED | (NFC_SEED_OFFSET + (seed & NFC_SEED_MASK)),
229 nfc->reg_base + NFC_REG_CMD);
230}
231
232static void meson_nfc_cmd_access(struct nand_chip *nand, bool raw, bool dir,
233 int scrambler)
234{
235 struct mtd_info *mtd = nand_to_mtd(nand);
236 const struct meson_nfc *nfc = nand_get_controller_data(mtd_to_nand(mtd));
237 const struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
238 u32 bch = meson_chip->bch_mode, cmd;
239 int len = mtd->writesize, pagesize, pages;
240
241 pagesize = nand->ecc.size;
242
243 if (raw) {
244 len = mtd->writesize + mtd->oobsize;
245 cmd = len | scrambler | DMA_DIR(dir);
246 writel(cmd, nfc->reg_base + NFC_REG_CMD);
247 return;
248 }
249
250 pages = len / nand->ecc.size;
251
252 cmd = CMDRWGEN(DMA_DIR(dir), scrambler, bch,
253 NFC_CMD_SHORTMODE_DISABLE, pagesize, pages);
254
255 writel(cmd, nfc->reg_base + NFC_REG_CMD);
256}
257
258static void meson_nfc_drain_cmd(struct meson_nfc *nfc)
259{
260 /*
261 * Insert two commands to make sure all valid commands are finished.
262 *
263 * The Nand flash controller is designed as two stages pipleline -
264 * a) fetch and b) execute.
265 * There might be cases when the driver see command queue is empty,
266 * but the Nand flash controller still has two commands buffered,
267 * one is fetched into NFC request queue (ready to run), and another
268 * is actively executing. So pushing 2 "IDLE" commands guarantees that
269 * the pipeline is emptied.
270 */
271 meson_nfc_cmd_idle(nfc, 0);
272 meson_nfc_cmd_idle(nfc, 0);
273}
274
275static int meson_nfc_wait_cmd_finish(const struct meson_nfc *nfc,
276 unsigned int timeout_us)
277{
278 u32 cmd_size = 0;
279
280 /* wait cmd fifo is empty */
281 return readl_relaxed_poll_timeout(nfc->reg_base + NFC_REG_CMD, cmd_size,
282 !NFC_CMD_GET_SIZE(cmd_size),
283 timeout_us);
284}
285
286static int meson_nfc_wait_dma_finish(struct meson_nfc *nfc)
287{
288 meson_nfc_drain_cmd(nfc);
289
290 return meson_nfc_wait_cmd_finish(nfc, DMA_BUSY_TIMEOUT_US);
291}
292
293static u8 *meson_nfc_oob_ptr(struct nand_chip *nand, int i)
294{
295 const struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
296 int len;
297
298 len = nand->ecc.size * (i + 1) + (nand->ecc.bytes + 2) * i;
299
300 return meson_chip->data_buf + len;
301}
302
303static u8 *meson_nfc_data_ptr(struct nand_chip *nand, int i)
304{
305 const struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
306 int len, temp;
307
308 temp = nand->ecc.size + nand->ecc.bytes;
309 len = (temp + 2) * i;
310
311 return meson_chip->data_buf + len;
312}
313
314static void meson_nfc_get_data_oob(struct nand_chip *nand,
315 u8 *buf, u8 *oobbuf)
316{
317 u8 *dsrc, *osrc;
318 int i, oob_len;
319
320 oob_len = nand->ecc.bytes + 2;
321 for (i = 0; i < nand->ecc.steps; i++) {
322 if (buf) {
323 dsrc = meson_nfc_data_ptr(nand, i);
324 memcpy(buf, dsrc, nand->ecc.size);
325 buf += nand->ecc.size;
326 }
327
328 if (oobbuf) {
329 osrc = meson_nfc_oob_ptr(nand, i);
330 memcpy(oobbuf, osrc, oob_len);
331 oobbuf += oob_len;
332 }
333 }
334}
335
336static void meson_nfc_set_data_oob(struct nand_chip *nand,
337 const u8 *buf, u8 *oobbuf)
338{
339 int i, oob_len;
340
341 oob_len = nand->ecc.bytes + 2;
342 for (i = 0; i < nand->ecc.steps; i++) {
343 u8 *osrc;
344
345 if (buf) {
346 u8 *dsrc;
347
348 dsrc = meson_nfc_data_ptr(nand, i);
349 memcpy(dsrc, buf, nand->ecc.size);
350 buf += nand->ecc.size;
351 }
352
353 osrc = meson_nfc_oob_ptr(nand, i);
354 memcpy(osrc, oobbuf, oob_len);
355 oobbuf += oob_len;
356 }
357}
358
359static void meson_nfc_set_user_byte(struct nand_chip *nand, const u8 *oob_buf)
360{
361 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
362 int i, count;
363
364 for (i = 0, count = 0; i < nand->ecc.steps; i++, count += (2 + nand->ecc.bytes)) {
365 __le64 *info = &meson_chip->info_buf[i];
366
367 *info |= oob_buf[count];
368 *info |= oob_buf[count + 1] << 8;
369 }
370}
371
372static void meson_nfc_get_user_byte(struct nand_chip *nand, u8 *oob_buf)
373{
374 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
375 int i, count;
376
377 for (i = 0, count = 0; i < nand->ecc.steps; i++, count += (2 + nand->ecc.bytes)) {
378 const __le64 *info = &meson_chip->info_buf[i];
379
380 oob_buf[count] = *info;
381 oob_buf[count + 1] = *info >> 8;
382 }
383}
384
385static int meson_nfc_ecc_correct(struct nand_chip *nand, u32 *bitflips,
386 u64 *correct_bitmap)
387{
388 struct mtd_info *mtd = nand_to_mtd(nand);
389 int ret = 0, i;
390
391 for (i = 0; i < nand->ecc.steps; i++) {
392 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
393 const __le64 *info = &meson_chip->info_buf[i];
394
395 if (ECC_ERR_CNT(*info) != ECC_UNCORRECTABLE) {
396 mtd->ecc_stats.corrected += ECC_ERR_CNT(*info);
397 *bitflips = max_t(u32, *bitflips, ECC_ERR_CNT(*info));
398 *correct_bitmap |= BIT_ULL(i);
399 continue;
400 }
401
402 if ((nand->options & NAND_NEED_SCRAMBLING) &&
403 ECC_ZERO_CNT(*info) < nand->ecc.strength) {
404 mtd->ecc_stats.corrected += ECC_ZERO_CNT(*info);
405 *bitflips = max_t(u32, *bitflips,
406 ECC_ZERO_CNT(*info));
407 ret = ECC_CHECK_RETURN_FF;
408 } else {
409 ret = -EBADMSG;
410 }
411 }
412
413 return ret;
414}
415
416static int meson_nfc_dma_buffer_setup(struct nand_chip *nand, void *databuf,
417 int datalen, void *infobuf, int infolen,
418 enum dma_data_direction dir)
419{
420 struct meson_nfc *nfc = nand_get_controller_data(nand);
421 int ret;
422 u32 cmd;
423
424 nfc->daddr = dma_map_single(databuf, datalen, DMA_BIDIRECTIONAL);
425 ret = dma_mapping_error(nfc->dev, nfc->daddr);
426 if (ret)
427 return ret;
428
429 cmd = GENCMDDADDRL(NFC_CMD_ADL, nfc->daddr);
430 writel(cmd, nfc->reg_base + NFC_REG_CMD);
431
432 cmd = GENCMDDADDRH(NFC_CMD_ADH, nfc->daddr);
433 writel(cmd, nfc->reg_base + NFC_REG_CMD);
434
435 if (infobuf) {
436 nfc->iaddr = dma_map_single(infobuf, infolen,
437 DMA_BIDIRECTIONAL);
438 ret = dma_mapping_error(nfc->dev, nfc->iaddr);
439 if (ret) {
440 dma_unmap_single(nfc->daddr, datalen, dir);
441 return ret;
442 }
443
444 nfc->info_bytes = infolen;
445 cmd = GENCMDIADDRL(NFC_CMD_AIL, nfc->iaddr);
446 writel(cmd, nfc->reg_base + NFC_REG_CMD);
447
448 cmd = GENCMDIADDRH(NFC_CMD_AIH, nfc->iaddr);
449 writel(cmd, nfc->reg_base + NFC_REG_CMD);
450 }
451
452 return 0;
453}
454
455static void meson_nfc_dma_buffer_release(struct nand_chip *nand,
456 int datalen, int infolen,
457 enum dma_data_direction dir)
458{
459 struct meson_nfc *nfc = nand_get_controller_data(nand);
460
461 dma_unmap_single(nfc->daddr, datalen, dir);
462
463 if (infolen) {
464 dma_unmap_single(nfc->iaddr, infolen, dir);
465 nfc->info_bytes = 0;
466 }
467}
468
469static void meson_nfc_read_buf(struct mtd_info *mtd, u8 *buf, int size)
470{
471 struct nand_chip *nand = mtd_to_nand(mtd);
472 struct meson_nfc *nfc = nand_get_controller_data(nand);
473 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
474 u8 *dma_buf;
475 int ret;
476 u32 cmd;
477
478 if ((uintptr_t)buf % DMA_ADDR_ALIGN) {
479 unsigned long tmp_addr;
480
481 dma_buf = dma_alloc_coherent(size, &tmp_addr);
482 if (!dma_buf)
483 return;
484 } else {
485 dma_buf = buf;
486 }
487
488 ret = meson_nfc_dma_buffer_setup(nand, dma_buf, size, meson_chip->info_buf,
489 PER_INFO_BYTE, DMA_FROM_DEVICE);
490 if (ret) {
491 pr_err("Failed to setup DMA buffer %p/%p\n", dma_buf,
492 meson_chip->info_buf);
493 return;
494 }
495
496 cmd = NFC_CMD_N2M | size;
497 writel(cmd, nfc->reg_base + NFC_REG_CMD);
498
499 meson_nfc_drain_cmd(nfc);
500 meson_nfc_wait_cmd_finish(nfc, CMD_DRAIN_TIMEOUT_US);
501 meson_nfc_dma_buffer_release(nand, size, PER_INFO_BYTE, DMA_FROM_DEVICE);
502
503 if (buf != dma_buf) {
504 memcpy(buf, dma_buf, size);
505 dma_free_coherent(dma_buf);
506 }
507}
508
509static void meson_nfc_write_buf(struct mtd_info *mtd, const u8 *buf, int size)
510{
511 struct nand_chip *nand = mtd_to_nand(mtd);
512 struct meson_nfc *nfc = nand_get_controller_data(nand);
513 u8 *dma_buf;
514 int ret;
515 u32 cmd;
516
517 if ((uintptr_t)buf % DMA_ADDR_ALIGN) {
518 unsigned long tmp_addr;
519
520 dma_buf = dma_alloc_coherent(size, &tmp_addr);
521 if (!dma_buf)
522 return;
523
524 memcpy(dma_buf, buf, size);
525 } else {
526 dma_buf = (u8 *)buf;
527 }
528
529 ret = meson_nfc_dma_buffer_setup(nand, (void *)dma_buf, size, NULL,
530 0, DMA_TO_DEVICE);
531 if (ret) {
532 pr_err("Failed to setup DMA buffer %p\n", dma_buf);
533 return;
534 }
535
536 cmd = NFC_CMD_M2N | size;
537 writel(cmd, nfc->reg_base + NFC_REG_CMD);
538
539 meson_nfc_drain_cmd(nfc);
540 meson_nfc_wait_cmd_finish(nfc, CMD_DRAIN_TIMEOUT_US);
541 meson_nfc_dma_buffer_release(nand, size, 0, DMA_TO_DEVICE);
542
543 if (buf != dma_buf)
544 dma_free_coherent(dma_buf);
545}
546
547static int meson_nfc_write_page_sub(struct nand_chip *nand,
548 int page, bool raw)
549{
550 const struct mtd_info *mtd = nand_to_mtd(nand);
551 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
552 struct meson_nfc *nfc = nand_get_controller_data(nand);
553 int data_len, info_len;
554 int ret;
555 u32 cmd;
556
557 data_len = mtd->writesize + mtd->oobsize;
558 info_len = nand->ecc.steps * PER_INFO_BYTE;
559
560 ret = meson_nfc_dma_buffer_setup(nand, meson_chip->data_buf,
561 data_len, meson_chip->info_buf,
562 info_len, DMA_TO_DEVICE);
563 if (ret) {
564 pr_err("Failed to setup DMA buffer %p/%p\n",
565 meson_chip->data_buf, meson_chip->info_buf);
566 return ret;
567 }
568
569 if (nand->options & NAND_NEED_SCRAMBLING) {
570 meson_nfc_cmd_seed(nfc, page);
571 meson_nfc_cmd_access(nand, raw, DIRWRITE,
572 NFC_CMD_SCRAMBLER_ENABLE);
573 } else {
574 meson_nfc_cmd_access(nand, raw, DIRWRITE,
575 NFC_CMD_SCRAMBLER_DISABLE);
576 }
577
578 cmd = nfc->param.chip_select | NFC_CMD_CLE | NAND_CMD_PAGEPROG;
579 writel(cmd, nfc->reg_base + NFC_REG_CMD);
580
581 meson_nfc_dma_buffer_release(nand, data_len, info_len, DMA_TO_DEVICE);
582
583 return 0;
584}
585
586static int meson_nfc_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
587 const u8 *buf, int oob_required, int page)
588{
589 meson_nfc_set_data_oob(chip, buf, oob_required ? chip->oob_poi : NULL);
590
591 return meson_nfc_write_page_sub(chip, page, true);
592}
593
594static int meson_nfc_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
595 const u8 *buf, int oob_required, int page)
596{
597 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(chip);
598
599 if (buf)
600 memcpy(meson_chip->data_buf, buf, mtd->writesize);
601
602 memset(meson_chip->info_buf, 0, chip->ecc.steps * PER_INFO_BYTE);
603
604 if (oob_required)
605 meson_nfc_set_user_byte(chip, chip->oob_poi);
606
607 return meson_nfc_write_page_sub(chip, page, false);
608}
609
610static void meson_nfc_check_ecc_pages_valid(struct meson_nfc *nfc,
611 struct nand_chip *nand, bool raw)
612{
613 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
614 __le64 *info;
615 u32 neccpages;
616 int ret;
617
618 neccpages = raw ? 1 : nand->ecc.steps;
619 info = &meson_chip->info_buf[neccpages - 1];
620 do {
621 udelay(ECC_POLL_TIMEOUT_US);
622 /* info is updated by nfc dma engine*/
623 rmb();
624 invalidate_dcache_range(nfc->iaddr, nfc->iaddr + nfc->info_bytes);
625 ret = *info & ECC_COMPLETE;
626 } while (!ret);
627}
628
629static int meson_nfc_read_page_sub(struct nand_chip *nand,
630 int page, bool raw)
631{
632 struct mtd_info *mtd = nand_to_mtd(nand);
633 struct meson_nfc *nfc = nand_get_controller_data(nand);
634 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
635 u32 data_len, info_len;
636 int ret;
637
638 data_len = mtd->writesize + mtd->oobsize;
639 info_len = nand->ecc.steps * PER_INFO_BYTE;
640
641 ret = meson_nfc_dma_buffer_setup(nand, meson_chip->data_buf, data_len,
642 meson_chip->info_buf, info_len,
643 DMA_FROM_DEVICE);
644 if (ret)
645 return ret;
646
647 if (nand->options & NAND_NEED_SCRAMBLING) {
648 meson_nfc_cmd_seed(nfc, page);
649 meson_nfc_cmd_access(nand, raw, DIRREAD,
650 NFC_CMD_SCRAMBLER_ENABLE);
651 } else {
652 meson_nfc_cmd_access(nand, raw, DIRREAD,
653 NFC_CMD_SCRAMBLER_DISABLE);
654 }
655
656 meson_nfc_wait_dma_finish(nfc);
657 meson_nfc_check_ecc_pages_valid(nfc, nand, raw);
658
659 meson_nfc_dma_buffer_release(nand, data_len, info_len,
660 DMA_FROM_DEVICE);
661
662 return 0;
663}
664
665static int meson_nfc_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
666 u8 *buf, int oob_required, int page)
667{
668 int ret;
669
670 ret = meson_nfc_read_page_sub(chip, page, true);
671 if (ret)
672 return ret;
673
674 meson_nfc_get_data_oob(chip, buf, oob_required ? chip->oob_poi : NULL);
675
676 return 0;
677}
678
679static int meson_nfc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
680 u8 *buf, int oob_required, int page)
681{
682 const struct meson_nfc_nand_chip *meson_chip = to_meson_nand(chip);
683 u64 correct_bitmap = 0;
684 u32 bitflips = 0;
685 int ret;
686
687 ret = meson_nfc_read_page_sub(chip, page, false);
688 if (ret)
689 return ret;
690
691 if (oob_required)
692 meson_nfc_get_user_byte(chip, chip->oob_poi);
693
694 ret = meson_nfc_ecc_correct(chip, &bitflips, &correct_bitmap);
695
696 if (ret == ECC_CHECK_RETURN_FF) {
697 if (buf)
698 memset(buf, 0xff, mtd->writesize);
699
700 if (oob_required)
701 memset(chip->oob_poi, 0xff, mtd->oobsize);
702 } else if (ret < 0) {
703 struct nand_ecc_ctrl *ecc;
704 int i;
705
706 if ((chip->options & NAND_NEED_SCRAMBLING) || !buf) {
707 mtd->ecc_stats.failed++;
708 return bitflips;
709 }
710
711 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
712
713 ret = meson_nfc_read_page_raw(mtd, chip, buf, 1, page);
714 if (ret)
715 return ret;
716
717 ecc = &chip->ecc;
718
719 for (i = 0; i < chip->ecc.steps ; i++) {
720 u8 *data = buf + i * ecc->size;
721 u8 *oob = chip->oob_poi + i * (ecc->bytes + 2);
722
723 if (correct_bitmap & BIT_ULL(i))
724 continue;
725
726 ret = nand_check_erased_ecc_chunk(data, ecc->size,
727 oob, ecc->bytes + 2,
728 NULL, 0,
729 ecc->strength);
730 if (ret < 0) {
731 mtd->ecc_stats.failed++;
732 } else {
733 mtd->ecc_stats.corrected += ret;
734 bitflips = max_t(u32, bitflips, ret);
735 }
736 }
737 } else if (buf && buf != meson_chip->data_buf) {
738 memcpy(buf, meson_chip->data_buf, mtd->writesize);
739 }
740
741 return bitflips;
742}
743
744static int meson_nfc_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
745 int page)
746{
747 int ret;
748
749 ret = nand_read_page_op(chip, page, 0, NULL, 0);
750 if (ret)
751 return ret;
752
753 return meson_nfc_read_page_raw(mtd, chip, NULL, 1, page);
754}
755
756static int meson_nfc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
757 int page)
758{
759 int ret;
760
761 ret = nand_read_page_op(chip, page, 0, NULL, 0);
762 if (ret)
763 return ret;
764
765 return meson_nfc_read_page_hwecc(mtd, chip, NULL, 1, page);
766}
767
768static int meson_nfc_write_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
769 int page)
770{
771 int ret;
772
773 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
774 if (ret)
775 return ret;
776
777 ret = meson_nfc_write_page_raw(mtd, chip, NULL, 1, page);
778 if (ret)
779 return ret;
780
781 return nand_prog_page_end_op(chip);
782}
783
784static int meson_nfc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
785 int page)
786{
787 int ret;
788
789 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
790 if (ret)
791 return ret;
792
793 ret = meson_nfc_write_page_hwecc(mtd, chip, NULL, 1, page);
794 if (ret)
795 return ret;
796
797 return nand_prog_page_end_op(chip);
798}
799
800static void meson_nfc_nand_cmd_function(struct mtd_info *mtd, unsigned int command,
801 int column, int page_addr)
802{
803 struct nand_chip *chip = mtd_to_nand(mtd);
804
805 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
806
807 if (column != -1 || page_addr != -1) {
808 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
809
810 /* Serially input address */
811 if (column != -1) {
812 /* Adjust columns for 16 bit buswidth */
813 if (chip->options & NAND_BUSWIDTH_16 &&
814 !nand_opcode_8bits(command))
815 column >>= 1;
816
817 chip->cmd_ctrl(mtd, column, ctrl);
818 ctrl &= ~NAND_CTRL_CHANGE;
819 /* Only output a single addr cycle for 8bits
820 * opcodes.
821 */
822 if (!nand_opcode_8bits(command))
823 chip->cmd_ctrl(mtd, column >> 8, ctrl);
824 }
825
826 if (page_addr != -1) {
827 chip->cmd_ctrl(mtd, page_addr, ctrl);
828 chip->cmd_ctrl(mtd, page_addr >> 8, NAND_NCE |
829 NAND_ALE);
830 /* One more address cycle for devices > 128MiB */
831 if (chip->chipsize > SZ_128M)
832 chip->cmd_ctrl(mtd, page_addr >> 16,
833 NAND_NCE | NAND_ALE);
834 }
835
836 switch (command) {
837 case NAND_CMD_READ0:
838 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
839 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
840 fallthrough;
841 case NAND_CMD_PARAM:
842 nand_wait_ready(mtd);
843 nand_exit_status_op(chip);
844 }
845 }
846}
847
848static void meson_nfc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
849{
850 struct nand_chip *nand = mtd_to_nand(mtd);
851 struct meson_nfc *nfc = nand_get_controller_data(nand);
852
853 if (cmd == NAND_CMD_NONE)
854 return;
855
856 if (ctrl & NAND_CLE)
857 cmd = NFC_CMD_MAKE_CLE(nfc->param.chip_select, cmd);
858 else
859 cmd = NFC_CMD_MAKE_ALE(nfc->param.chip_select, cmd);
860
861 writel(cmd, nfc->reg_base + NFC_REG_CMD);
862}
863
864static void meson_nfc_wait_cmd_fifo(struct meson_nfc *nfc)
865{
866 while ((NFC_GET_CMD(nfc) >> 22) & GENMASK(4, 0))
867 ;
868}
869
870static u8 meson_nfc_nand_read_byte(struct mtd_info *mtd)
871{
872 struct nand_chip *nand = mtd_to_nand(mtd);
873 struct meson_nfc *nfc = nand_get_controller_data(nand);
874
875 writel(NFC_CMD_MAKE_DRD(nfc->param.chip_select, 0), nfc->reg_base + NFC_REG_CMD);
876
877 meson_nfc_cmd_idle(nfc, NAND_TWB_TIME_CYCLE);
878 meson_nfc_cmd_idle(nfc, 0);
879 meson_nfc_cmd_idle(nfc, 0);
880
881 meson_nfc_wait_cmd_fifo(nfc);
882
883 return readl(nfc->reg_base + NFC_REG_BUF);
884}
885
886static void meson_nfc_nand_write_byte(struct mtd_info *mtd, u8 val)
887{
888 struct nand_chip *nand = mtd_to_nand(mtd);
889 struct meson_nfc *nfc = nand_get_controller_data(nand);
890
891 meson_nfc_cmd_idle(nfc, NAND_TWB_TIME_CYCLE);
892
893 writel(NFC_CMD_MAKE_DWR(nfc->param.chip_select, val), nfc->reg_base + NFC_REG_CMD);
894
895 meson_nfc_cmd_idle(nfc, NAND_TWB_TIME_CYCLE);
896 meson_nfc_cmd_idle(nfc, 0);
897 meson_nfc_cmd_idle(nfc, 0);
898
899 meson_nfc_wait_cmd_fifo(nfc);
900}
901
902static int meson_nfc_dev_ready(struct mtd_info *mtd)
903{
904 struct nand_chip *chip = mtd_to_nand(mtd);
905 unsigned int time_out_cnt = 0;
906
907 chip->select_chip(mtd, 0);
908
909 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
910
911 do {
912 int status;
913
914 status = (int)chip->read_byte(mtd);
915 if (status & NAND_STATUS_READY)
916 break;
917 } while (time_out_cnt++ < NFC_DEV_READY_TICK_MAX);
918
919 return time_out_cnt != NFC_DEV_READY_TICK_MAX;
920}
921
922static int meson_chip_buffer_init(struct nand_chip *nand)
923{
924 const struct mtd_info *mtd = nand_to_mtd(nand);
925 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
926 u32 page_bytes, info_bytes, nsectors;
927 unsigned long tmp_addr;
928
929 nsectors = mtd->writesize / nand->ecc.size;
930
931 page_bytes = mtd->writesize + mtd->oobsize;
932 info_bytes = nsectors * PER_INFO_BYTE;
933
934 meson_chip->data_buf = dma_alloc_coherent(page_bytes, &tmp_addr);
935 if (!meson_chip->data_buf)
936 return -ENOMEM;
937
938 meson_chip->info_buf = dma_alloc_coherent(info_bytes, &tmp_addr);
939 if (!meson_chip->info_buf) {
940 dma_free_coherent(meson_chip->data_buf);
941 return -ENOMEM;
942 }
943
944 return 0;
945}
946
947static const int axg_stepinfo_strengths[] = { 8 };
948static const struct nand_ecc_step_info axg_stepinfo_1024 = {
949 .stepsize = 1024,
950 .strengths = axg_stepinfo_strengths,
951 .nstrengths = ARRAY_SIZE(axg_stepinfo_strengths)
952};
953
954static const struct nand_ecc_step_info axg_stepinfo_512 = {
955 .stepsize = 512,
956 .strengths = axg_stepinfo_strengths,
957 .nstrengths = ARRAY_SIZE(axg_stepinfo_strengths)
958};
959
960static const struct nand_ecc_step_info axg_stepinfo[] = { axg_stepinfo_1024, axg_stepinfo_512 };
961
962static const struct nand_ecc_caps meson_axg_ecc_caps = {
963 .stepinfos = axg_stepinfo,
964 .nstepinfos = ARRAY_SIZE(axg_stepinfo),
965 .calc_ecc_bytes = meson_nand_calc_ecc_bytes,
966};
967
968/*
969 * OOB layout:
970 *
971 * For ECC with 512 bytes step size:
972 * 0x00: AA AA BB BB BB BB BB BB BB BB BB BB BB BB BB BB
973 * 0x10: AA AA CC CC CC CC CC CC CC CC CC CC CC CC CC CC
974 * 0x20:
975 * 0x30:
976 *
977 * For ECC with 1024 bytes step size:
978 * 0x00: AA AA BB BB BB BB BB BB BB BB BB BB BB BB BB BB
979 * 0x10: AA AA CC CC CC CC CC CC CC CC CC CC CC CC CC CC
980 * 0x20: AA AA DD DD DD DD DD DD DD DD DD DD DD DD DD DD
981 * 0x30: AA AA EE EE EE EE EE EE EE EE EE EE EE EE EE EE
982 *
983 * AA - user bytes.
984 * BB, CC, DD, EE - ECC code bytes for each step.
985 */
986static struct nand_ecclayout nand_oob;
987
988static void meson_nfc_init_nand_oob(struct nand_chip *nand)
989{
990 int section_size = 2 + nand->ecc.bytes;
991 int i;
992 int k;
993
994 nand_oob.eccbytes = nand->ecc.steps * nand->ecc.bytes;
995 k = 0;
996
997 for (i = 0; i < nand->ecc.steps; i++) {
998 int j;
999
1000 for (j = 0; j < nand->ecc.bytes; j++)
1001 nand_oob.eccpos[k++] = (i * section_size) + 2 + j;
1002
1003 nand_oob.oobfree[i].offset = (i * section_size);
1004 nand_oob.oobfree[i].length = 2;
1005 }
1006
1007 nand_oob.oobavail = 2 * nand->ecc.steps;
1008 nand->ecc.layout = &nand_oob;
1009}
1010
1011static int meson_nfc_init_ecc(struct nand_chip *nand, ofnode node)
1012{
1013 const struct mtd_info *mtd = nand_to_mtd(nand);
1014 int ret;
1015 int i;
1016
1017 ret = nand_check_ecc_caps(nand, &meson_axg_ecc_caps, mtd->oobsize - 2);
1018 if (ret)
1019 return ret;
1020
1021 for (i = 0; i < ARRAY_SIZE(meson_ecc); i++) {
1022 if (meson_ecc[i].strength == nand->ecc.strength &&
1023 meson_ecc[i].size == nand->ecc.size) {
1024 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
1025
1026 nand->ecc.steps = mtd->writesize / nand->ecc.size;
1027 meson_chip->bch_mode = meson_ecc[i].bch;
1028
1029 meson_nfc_init_nand_oob(nand);
1030
1031 return 0;
1032 }
1033 }
1034
1035 return -EINVAL;
1036}
1037
1038static int meson_nfc_nand_chip_init(struct udevice *dev, struct meson_nfc *nfc,
1039 ofnode node)
1040{
1041 struct meson_nfc_nand_chip *meson_chip;
1042 struct nand_chip *nand;
1043 struct mtd_info *mtd;
1044 u32 cs[MAX_CE_NUM];
1045 u32 nsels;
1046 int ret;
1047 int i;
1048
1049 if (!ofnode_get_property(node, "reg", &nsels)) {
1050 dev_err(dev, "\"reg\" property is not found\n");
1051 return -ENODEV;
1052 }
1053
1054 nsels /= sizeof(u32);
1055 if (nsels >= MAX_CE_NUM) {
1056 dev_err(dev, "invalid size of CS array, max is %d\n",
1057 MAX_CE_NUM);
1058 return -EINVAL;
1059 }
1060
1061 ret = ofnode_read_u32_array(node, "reg", cs, nsels);
1062 if (ret < 0) {
1063 dev_err(dev, "failed to read \"reg\" property\n");
1064 return ret;
1065 }
1066
1067 for (i = 0; i < nsels; i++) {
1068 if (test_and_set_bit(cs[i], &nfc->assigned_cs)) {
1069 dev_err(dev, "CS %d already assigned\n", cs[i]);
1070 return -EINVAL;
1071 }
1072 }
1073
1074 meson_chip = malloc(sizeof(*meson_chip) + nsels * sizeof(meson_chip->sels[0]));
1075 if (!meson_chip) {
1076 dev_err(dev, "failed to allocate memory for chip\n");
1077 return -ENOMEM;
1078 }
1079
1080 meson_chip->nsels = nsels;
1081 nand = &meson_chip->nand;
1082
1083 nand->flash_node = node;
1084 nand_set_controller_data(nand, nfc);
1085 /* Set the driver entry points for MTD */
1086 nand->cmdfunc = meson_nfc_nand_cmd_function;
1087 nand->cmd_ctrl = meson_nfc_cmd_ctrl;
1088 nand->select_chip = meson_nfc_nand_select_chip;
1089 nand->read_byte = meson_nfc_nand_read_byte;
1090 nand->write_byte = meson_nfc_nand_write_byte;
1091 nand->dev_ready = meson_nfc_dev_ready;
1092
1093 /* Buffer read/write routines */
1094 nand->read_buf = meson_nfc_read_buf;
1095 nand->write_buf = meson_nfc_write_buf;
1096 nand->options |= NAND_NO_SUBPAGE_WRITE;
1097
1098 nand->ecc.mode = NAND_ECC_HW;
1099 nand->ecc.hwctl = NULL;
1100 nand->ecc.read_page = meson_nfc_read_page_hwecc;
1101 nand->ecc.write_page = meson_nfc_write_page_hwecc;
1102 nand->ecc.read_page_raw = meson_nfc_read_page_raw;
1103 nand->ecc.write_page_raw = meson_nfc_write_page_raw;
1104
1105 nand->ecc.read_oob = meson_nfc_read_oob;
1106 nand->ecc.write_oob = meson_nfc_write_oob;
1107 nand->ecc.read_oob_raw = meson_nfc_read_oob_raw;
1108 nand->ecc.write_oob_raw = meson_nfc_write_oob_raw;
1109
1110 nand->ecc.algo = NAND_ECC_BCH;
1111
1112 mtd = nand_to_mtd(nand);
1113
1114 ret = nand_scan_ident(mtd, 1, NULL);
1115 if (ret) {
1116 dev_err(dev, "'nand_scan_ident()' failed: %d\n", ret);
1117 goto err_chip_free;
1118 }
1119
1120 ret = meson_nfc_init_ecc(nand, node);
1121 if (ret) {
1122 dev_err(dev, "failed to init ECC settings: %d\n", ret);
1123 goto err_chip_free;
1124 }
1125
1126 ret = meson_chip_buffer_init(nand);
1127 if (ret) {
1128 dev_err(dev, "failed to init DMA buffers: %d\n", ret);
1129 goto err_chip_free;
1130 }
1131
1132 /* 'nand_scan_tail()' needs ECC parameters to be already
1133 * set and correct.
1134 */
1135 ret = nand_scan_tail(mtd);
1136 if (ret) {
1137 dev_err(dev, "'nand_scan_tail()' failed: %d\n", ret);
1138 goto err_chip_buf_free;
1139 }
1140
1141 ret = nand_register(0, mtd);
1142 if (ret) {
1143 dev_err(dev, "'nand_register()' failed: %d\n", ret);
1144 goto err_chip_buf_free;
1145 }
1146
1147 list_add_tail(&meson_chip->node, &nfc->chips);
1148
1149 return 0;
1150
1151err_chip_buf_free:
1152 dma_free_coherent(meson_chip->info_buf);
1153 dma_free_coherent(meson_chip->data_buf);
1154
1155err_chip_free:
1156 free(meson_chip);
1157
1158 return ret;
1159}
1160
1161static int meson_nfc_nand_chips_init(struct udevice *dev,
1162 struct meson_nfc *nfc)
1163{
1164 ofnode parent = dev_ofnode(dev);
1165 ofnode node;
1166
1167 ofnode_for_each_subnode(node, parent) {
1168 int ret = meson_nfc_nand_chip_init(dev, nfc, node);
1169
1170 if (ret)
1171 return ret;
1172 }
1173
1174 return 0;
1175}
1176
1177static void meson_nfc_clk_init(struct meson_nfc *nfc)
1178{
1179 u32 bus_cycle = NFC_DEFAULT_BUS_CYCLE;
1180 u32 bus_timing = NFC_DEFAULT_BUS_TIMING;
1181 u32 bus_cfg_val;
1182
1183 writel(CLK_ALWAYS_ON_NAND | CLK_SELECT_NAND | CLK_ENABLE_VALUE, nfc->reg_clk);
1184 writel(0, nfc->reg_base + NFC_REG_CFG);
1185
1186 bus_cfg_val = (((bus_cycle - 1) & 31) | ((bus_timing & 31) << 5));
1187 writel(bus_cfg_val, nfc->reg_base + NFC_REG_CFG);
1188 writel(BIT(31), nfc->reg_base + NFC_REG_CMD);
1189}
1190
1191static int meson_probe(struct udevice *dev)
1192{
1193 struct meson_nfc *nfc = dev_get_priv(dev);
1194 void *addr;
1195 int ret;
1196
1197 addr = dev_read_addr_ptr(dev);
1198 if (!addr) {
1199 dev_err(dev, "base register address not found\n");
1200 return -EINVAL;
1201 }
1202
1203 nfc->reg_base = addr;
1204
1205 addr = dev_read_addr_index_ptr(dev, 1);
1206 if (!addr) {
1207 dev_err(dev, "clk register address not found\n");
1208 return -EINVAL;
1209 }
1210
1211 nfc->reg_clk = addr;
1212 nfc->dev = dev;
1213
1214 meson_nfc_clk_init(nfc);
1215
1216 ret = meson_nfc_nand_chips_init(dev, nfc);
1217 if (ret) {
1218 dev_err(nfc->dev, "failed to init chips\n");
1219 return ret;
1220 }
1221
1222 return 0;
1223}
1224
1225static const struct udevice_id meson_nand_dt_ids[] = {
1226 {.compatible = "amlogic,meson-axg-nfc",},
1227 { /* sentinel */ }
1228};
1229
1230U_BOOT_DRIVER(meson_nand) = {
1231 .name = "meson_nand",
1232 .id = UCLASS_MTD,
1233 .of_match = meson_nand_dt_ids,
1234 .probe = meson_probe,
1235 .priv_auto = sizeof(struct meson_nfc),
1236};
1237
1238void board_nand_init(void)
1239{
1240 struct udevice *dev;
1241 int ret;
1242
1243 ret = uclass_get_device_by_driver(UCLASS_MTD,
1244 DM_DRIVER_GET(meson_nand), &dev);
1245
1246 if (ret && ret != -ENODEV)
1247 pr_err("Failed to initialize: %d\n", ret);
1248}