1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
3 * Amlogic Meson Nand Flash Controller Driver
5 * Copyright (c) 2018 Amlogic, inc.
6 * Author: Liang Yang <liang.yang@amlogic.com>
9 #include <linux/platform_device.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/interrupt.h>
12 #include <linux/clk.h>
13 #include <linux/clk-provider.h>
14 #include <linux/mtd/rawnand.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/regmap.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 #include <linux/iopoll.h>
22 #include <linux/of_device.h>
23 #include <linux/sched/task_stack.h>
25 #define NFC_REG_CMD 0x00
26 #define NFC_CMD_IDLE (0xc << 14)
27 #define NFC_CMD_CLE (0x5 << 14)
28 #define NFC_CMD_ALE (0x6 << 14)
29 #define NFC_CMD_ADL ((0 << 16) | (3 << 20))
30 #define NFC_CMD_ADH ((1 << 16) | (3 << 20))
31 #define NFC_CMD_AIL ((2 << 16) | (3 << 20))
32 #define NFC_CMD_AIH ((3 << 16) | (3 << 20))
33 #define NFC_CMD_SEED ((8 << 16) | (3 << 20))
34 #define NFC_CMD_M2N ((0 << 17) | (2 << 20))
35 #define NFC_CMD_N2M ((1 << 17) | (2 << 20))
36 #define NFC_CMD_RB BIT(20)
37 #define NFC_CMD_SCRAMBLER_ENABLE BIT(19)
38 #define NFC_CMD_SCRAMBLER_DISABLE 0
39 #define NFC_CMD_SHORTMODE_DISABLE 0
40 #define NFC_CMD_RB_INT BIT(14)
41 #define NFC_CMD_RB_INT_NO_PIN ((0xb << 10) | BIT(18) | BIT(16))
43 #define NFC_CMD_GET_SIZE(x) (((x) >> 22) & GENMASK(4, 0))
45 #define NFC_REG_CFG 0x04
46 #define NFC_REG_DADR 0x08
47 #define NFC_REG_IADR 0x0c
48 #define NFC_REG_BUF 0x10
49 #define NFC_REG_INFO 0x14
50 #define NFC_REG_DC 0x18
51 #define NFC_REG_ADR 0x1c
52 #define NFC_REG_DL 0x20
53 #define NFC_REG_DH 0x24
54 #define NFC_REG_CADR 0x28
55 #define NFC_REG_SADR 0x2c
56 #define NFC_REG_PINS 0x30
57 #define NFC_REG_VER 0x38
59 #define NFC_RB_IRQ_EN BIT(21)
61 #define CLK_DIV_SHIFT 0
62 #define CLK_DIV_WIDTH 6
64 #define CMDRWGEN(cmd_dir, ran, bch, short_mode, page_size, pages) \
69 ((short_mode) << 13) | \
70 (((page_size) & 0x7f) << 6) | \
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))
79 #define DMA_DIR(dir) ((dir) ? NFC_CMD_N2M : NFC_CMD_M2N)
80 #define DMA_ADDR_ALIGN 8
82 #define ECC_CHECK_RETURN_FF (-1)
84 #define NAND_CE0 (0xe << 10)
85 #define NAND_CE1 (0xd << 10)
87 #define DMA_BUSY_TIMEOUT 0x100000
88 #define CMD_FIFO_EMPTY_TIMEOUT 1000
92 /* eMMC clock register, misc control */
93 #define CLK_SELECT_NAND BIT(31)
95 #define NFC_CLK_CYCLE 6
97 /* nand flash controller delay 3 ns */
98 #define NFC_DEFAULT_DELAY 3000
100 #define ROW_ADDER(page, index) (((page) >> (8 * (index))) & 0xff)
101 #define MAX_CYCLE_ADDRS 5
105 #define ECC_PARITY_BCH8_512B 14
106 #define ECC_COMPLETE BIT(31)
107 #define ECC_ERR_CNT(x) (((x) >> 24) & GENMASK(5, 0))
108 #define ECC_ZERO_CNT(x) (((x) >> 16) & GENMASK(5, 0))
109 #define ECC_UNCORRECTABLE 0x3f
111 #define PER_INFO_BYTE 8
113 #define NFC_CMD_RAW_LEN GENMASK(13, 0)
115 #define NFC_COLUMN_ADDR_0 0
116 #define NFC_COLUMN_ADDR_1 0
118 struct meson_nfc_nand_chip {
119 struct list_head node;
120 struct nand_chip nand;
121 unsigned long clk_rate;
122 unsigned long level1_divider;
135 struct meson_nand_ecc {
140 struct meson_nfc_data {
141 const struct nand_ecc_caps *ecc_caps;
144 struct meson_nfc_param {
151 u32 addrs[MAX_CYCLE_ADDRS];
162 struct nand_controller controller;
163 struct clk *core_clk;
164 struct clk *device_clk;
165 struct clk *nand_clk;
166 struct clk_divider nand_divider;
168 unsigned long clk_rate;
172 void __iomem *reg_base;
173 void __iomem *reg_clk;
174 struct completion completion;
175 struct list_head chips;
176 const struct meson_nfc_data *data;
177 struct meson_nfc_param param;
178 struct nand_timing timing;
181 struct nand_rw_cmd rw;
188 unsigned long assigned_cs;
201 #define MESON_ECC_DATA(b, s) { .bch = (b), .strength = (s)}
203 static struct meson_nand_ecc meson_ecc[] = {
204 MESON_ECC_DATA(NFC_ECC_BCH8_1K, 8),
205 MESON_ECC_DATA(NFC_ECC_BCH24_1K, 24),
206 MESON_ECC_DATA(NFC_ECC_BCH30_1K, 30),
207 MESON_ECC_DATA(NFC_ECC_BCH40_1K, 40),
208 MESON_ECC_DATA(NFC_ECC_BCH50_1K, 50),
209 MESON_ECC_DATA(NFC_ECC_BCH60_1K, 60),
212 static int meson_nand_calc_ecc_bytes(int step_size, int strength)
216 if (step_size == 512 && strength == 8)
217 return ECC_PARITY_BCH8_512B;
219 ecc_bytes = DIV_ROUND_UP(strength * fls(step_size * 8), 8);
220 ecc_bytes = ALIGN(ecc_bytes, 2);
225 NAND_ECC_CAPS_SINGLE(meson_gxl_ecc_caps,
226 meson_nand_calc_ecc_bytes, 1024, 8, 24, 30, 40, 50, 60);
227 NAND_ECC_CAPS_SINGLE(meson_axg_ecc_caps,
228 meson_nand_calc_ecc_bytes, 1024, 8);
230 static struct meson_nfc_nand_chip *to_meson_nand(struct nand_chip *nand)
232 return container_of(nand, struct meson_nfc_nand_chip, nand);
235 static void meson_nfc_select_chip(struct nand_chip *nand, int chip)
237 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
238 struct meson_nfc *nfc = nand_get_controller_data(nand);
241 if (chip < 0 || WARN_ON_ONCE(chip >= meson_chip->nsels))
244 nfc->param.chip_select = meson_chip->sels[chip] ? NAND_CE1 : NAND_CE0;
245 nfc->param.rb_select = nfc->param.chip_select;
246 nfc->timing.twb = meson_chip->twb;
247 nfc->timing.tadl = meson_chip->tadl;
248 nfc->timing.tbers_max = meson_chip->tbers_max;
250 if (nfc->clk_rate != meson_chip->clk_rate) {
251 ret = clk_set_rate(nfc->nand_clk, meson_chip->clk_rate);
253 dev_err(nfc->dev, "failed to set clock rate\n");
256 nfc->clk_rate = meson_chip->clk_rate;
258 if (nfc->bus_timing != meson_chip->bus_timing) {
259 value = (NFC_CLK_CYCLE - 1) | (meson_chip->bus_timing << 5);
260 writel(value, nfc->reg_base + NFC_REG_CFG);
261 writel((1 << 31), nfc->reg_base + NFC_REG_CMD);
262 nfc->bus_timing = meson_chip->bus_timing;
266 static void meson_nfc_cmd_idle(struct meson_nfc *nfc, u32 time)
268 writel(nfc->param.chip_select | NFC_CMD_IDLE | (time & 0x3ff),
269 nfc->reg_base + NFC_REG_CMD);
272 static void meson_nfc_cmd_seed(struct meson_nfc *nfc, u32 seed)
274 writel(NFC_CMD_SEED | (0xc2 + (seed & 0x7fff)),
275 nfc->reg_base + NFC_REG_CMD);
278 static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir,
281 struct mtd_info *mtd = nand_to_mtd(nand);
282 struct meson_nfc *nfc = nand_get_controller_data(mtd_to_nand(mtd));
283 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
284 u32 bch = meson_chip->bch_mode, cmd;
285 int len = mtd->writesize, pagesize, pages;
287 pagesize = nand->ecc.size;
290 len = mtd->writesize + mtd->oobsize;
291 cmd = len | scrambler | DMA_DIR(dir);
292 writel(cmd, nfc->reg_base + NFC_REG_CMD);
296 pages = len / nand->ecc.size;
298 cmd = CMDRWGEN(DMA_DIR(dir), scrambler, bch,
299 NFC_CMD_SHORTMODE_DISABLE, pagesize, pages);
301 writel(cmd, nfc->reg_base + NFC_REG_CMD);
304 static void meson_nfc_drain_cmd(struct meson_nfc *nfc)
307 * Insert two commands to make sure all valid commands are finished.
309 * The Nand flash controller is designed as two stages pipleline -
310 * a) fetch and b) excute.
311 * There might be cases when the driver see command queue is empty,
312 * but the Nand flash controller still has two commands buffered,
313 * one is fetched into NFC request queue (ready to run), and another
314 * is actively executing. So pushing 2 "IDLE" commands guarantees that
315 * the pipeline is emptied.
317 meson_nfc_cmd_idle(nfc, 0);
318 meson_nfc_cmd_idle(nfc, 0);
321 static int meson_nfc_wait_cmd_finish(struct meson_nfc *nfc,
322 unsigned int timeout_ms)
327 /* wait cmd fifo is empty */
328 ret = readl_relaxed_poll_timeout(nfc->reg_base + NFC_REG_CMD, cmd_size,
329 !NFC_CMD_GET_SIZE(cmd_size),
330 10, timeout_ms * 1000);
332 dev_err(nfc->dev, "wait for empty CMD FIFO time out\n");
337 static int meson_nfc_wait_dma_finish(struct meson_nfc *nfc)
339 meson_nfc_drain_cmd(nfc);
341 return meson_nfc_wait_cmd_finish(nfc, DMA_BUSY_TIMEOUT);
344 static u8 *meson_nfc_oob_ptr(struct nand_chip *nand, int i)
346 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
349 len = nand->ecc.size * (i + 1) + (nand->ecc.bytes + 2) * i;
351 return meson_chip->data_buf + len;
354 static u8 *meson_nfc_data_ptr(struct nand_chip *nand, int i)
356 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
359 temp = nand->ecc.size + nand->ecc.bytes;
360 len = (temp + 2) * i;
362 return meson_chip->data_buf + len;
365 static void meson_nfc_get_data_oob(struct nand_chip *nand,
371 oob_len = nand->ecc.bytes + 2;
372 for (i = 0; i < nand->ecc.steps; i++) {
374 dsrc = meson_nfc_data_ptr(nand, i);
375 memcpy(buf, dsrc, nand->ecc.size);
376 buf += nand->ecc.size;
378 osrc = meson_nfc_oob_ptr(nand, i);
379 memcpy(oobbuf, osrc, oob_len);
384 static void meson_nfc_set_data_oob(struct nand_chip *nand,
385 const u8 *buf, u8 *oobbuf)
390 oob_len = nand->ecc.bytes + 2;
391 for (i = 0; i < nand->ecc.steps; i++) {
393 dsrc = meson_nfc_data_ptr(nand, i);
394 memcpy(dsrc, buf, nand->ecc.size);
395 buf += nand->ecc.size;
397 osrc = meson_nfc_oob_ptr(nand, i);
398 memcpy(osrc, oobbuf, oob_len);
403 static int meson_nfc_wait_no_rb_pin(struct meson_nfc *nfc, int timeout_ms,
408 meson_nfc_cmd_idle(nfc, nfc->timing.twb);
409 meson_nfc_drain_cmd(nfc);
410 meson_nfc_wait_cmd_finish(nfc, CMD_FIFO_EMPTY_TIMEOUT);
412 cfg = readl(nfc->reg_base + NFC_REG_CFG);
413 cfg |= NFC_RB_IRQ_EN;
414 writel(cfg, nfc->reg_base + NFC_REG_CFG);
416 reinit_completion(&nfc->completion);
417 cmd = nfc->param.chip_select | NFC_CMD_CLE | NAND_CMD_STATUS;
418 writel(cmd, nfc->reg_base + NFC_REG_CMD);
420 /* use the max erase time as the maximum clock for waiting R/B */
421 cmd = NFC_CMD_RB | NFC_CMD_RB_INT_NO_PIN | nfc->timing.tbers_max;
422 writel(cmd, nfc->reg_base + NFC_REG_CMD);
424 if (!wait_for_completion_timeout(&nfc->completion,
425 msecs_to_jiffies(timeout_ms)))
428 if (need_cmd_read0) {
429 cmd = nfc->param.chip_select | NFC_CMD_CLE | NAND_CMD_READ0;
430 writel(cmd, nfc->reg_base + NFC_REG_CMD);
431 meson_nfc_drain_cmd(nfc);
432 meson_nfc_wait_cmd_finish(nfc, CMD_FIFO_EMPTY_TIMEOUT);
438 static int meson_nfc_wait_rb_pin(struct meson_nfc *nfc, int timeout_ms)
443 meson_nfc_cmd_idle(nfc, nfc->timing.twb);
444 meson_nfc_drain_cmd(nfc);
445 meson_nfc_wait_cmd_finish(nfc, CMD_FIFO_EMPTY_TIMEOUT);
447 cfg = readl(nfc->reg_base + NFC_REG_CFG);
448 cfg |= NFC_RB_IRQ_EN;
449 writel(cfg, nfc->reg_base + NFC_REG_CFG);
451 reinit_completion(&nfc->completion);
453 /* use the max erase time as the maximum clock for waiting R/B */
454 cmd = NFC_CMD_RB | NFC_CMD_RB_INT
455 | nfc->param.chip_select | nfc->timing.tbers_max;
456 writel(cmd, nfc->reg_base + NFC_REG_CMD);
458 ret = wait_for_completion_timeout(&nfc->completion,
459 msecs_to_jiffies(timeout_ms));
466 static int meson_nfc_queue_rb(struct meson_nfc *nfc, int timeout_ms,
469 if (nfc->no_rb_pin) {
470 /* This mode is used when there is no wired R/B pin.
471 * It works like 'nand_soft_waitrdy()', but instead of
472 * polling NAND_CMD_STATUS bit in the software loop,
473 * it will wait for interrupt - controllers checks IO
474 * bus and when it detects NAND_CMD_STATUS on it, it
475 * raises interrupt. After interrupt, NAND_CMD_READ0 is
476 * sent as terminator of the ready waiting procedure if
477 * needed (for all cases except page programming - this
478 * is reason of 'need_cmd_read0' flag).
480 return meson_nfc_wait_no_rb_pin(nfc, timeout_ms,
483 return meson_nfc_wait_rb_pin(nfc, timeout_ms);
487 static void meson_nfc_set_user_byte(struct nand_chip *nand, u8 *oob_buf)
489 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
493 for (i = 0, count = 0; i < nand->ecc.steps; i++, count += 2) {
494 info = &meson_chip->info_buf[i];
495 *info |= oob_buf[count];
496 *info |= oob_buf[count + 1] << 8;
500 static void meson_nfc_get_user_byte(struct nand_chip *nand, u8 *oob_buf)
502 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
506 for (i = 0, count = 0; i < nand->ecc.steps; i++, count += 2) {
507 info = &meson_chip->info_buf[i];
508 oob_buf[count] = *info;
509 oob_buf[count + 1] = *info >> 8;
513 static int meson_nfc_ecc_correct(struct nand_chip *nand, u32 *bitflips,
516 struct mtd_info *mtd = nand_to_mtd(nand);
517 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
521 for (i = 0; i < nand->ecc.steps; i++) {
522 info = &meson_chip->info_buf[i];
523 if (ECC_ERR_CNT(*info) != ECC_UNCORRECTABLE) {
524 mtd->ecc_stats.corrected += ECC_ERR_CNT(*info);
525 *bitflips = max_t(u32, *bitflips, ECC_ERR_CNT(*info));
526 *correct_bitmap |= BIT_ULL(i);
529 if ((nand->options & NAND_NEED_SCRAMBLING) &&
530 ECC_ZERO_CNT(*info) < nand->ecc.strength) {
531 mtd->ecc_stats.corrected += ECC_ZERO_CNT(*info);
532 *bitflips = max_t(u32, *bitflips,
533 ECC_ZERO_CNT(*info));
534 ret = ECC_CHECK_RETURN_FF;
542 static int meson_nfc_dma_buffer_setup(struct nand_chip *nand, void *databuf,
543 int datalen, void *infobuf, int infolen,
544 enum dma_data_direction dir)
546 struct meson_nfc *nfc = nand_get_controller_data(nand);
550 nfc->daddr = dma_map_single(nfc->dev, databuf, datalen, dir);
551 ret = dma_mapping_error(nfc->dev, nfc->daddr);
553 dev_err(nfc->dev, "DMA mapping error\n");
556 cmd = GENCMDDADDRL(NFC_CMD_ADL, nfc->daddr);
557 writel(cmd, nfc->reg_base + NFC_REG_CMD);
559 cmd = GENCMDDADDRH(NFC_CMD_ADH, nfc->daddr);
560 writel(cmd, nfc->reg_base + NFC_REG_CMD);
563 nfc->iaddr = dma_map_single(nfc->dev, infobuf, infolen, dir);
564 ret = dma_mapping_error(nfc->dev, nfc->iaddr);
566 dev_err(nfc->dev, "DMA mapping error\n");
567 dma_unmap_single(nfc->dev,
568 nfc->daddr, datalen, dir);
571 nfc->info_bytes = infolen;
572 cmd = GENCMDIADDRL(NFC_CMD_AIL, nfc->iaddr);
573 writel(cmd, nfc->reg_base + NFC_REG_CMD);
575 cmd = GENCMDIADDRH(NFC_CMD_AIH, nfc->iaddr);
576 writel(cmd, nfc->reg_base + NFC_REG_CMD);
582 static void meson_nfc_dma_buffer_release(struct nand_chip *nand,
583 int datalen, int infolen,
584 enum dma_data_direction dir)
586 struct meson_nfc *nfc = nand_get_controller_data(nand);
588 dma_unmap_single(nfc->dev, nfc->daddr, datalen, dir);
590 dma_unmap_single(nfc->dev, nfc->iaddr, infolen, dir);
595 static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len)
597 struct meson_nfc *nfc = nand_get_controller_data(nand);
602 info = kzalloc(PER_INFO_BYTE, GFP_KERNEL);
606 ret = meson_nfc_dma_buffer_setup(nand, buf, len, info,
607 PER_INFO_BYTE, DMA_FROM_DEVICE);
611 cmd = NFC_CMD_N2M | len;
612 writel(cmd, nfc->reg_base + NFC_REG_CMD);
614 meson_nfc_drain_cmd(nfc);
615 meson_nfc_wait_cmd_finish(nfc, 1000);
616 meson_nfc_dma_buffer_release(nand, len, PER_INFO_BYTE, DMA_FROM_DEVICE);
624 static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len)
626 struct meson_nfc *nfc = nand_get_controller_data(nand);
630 ret = meson_nfc_dma_buffer_setup(nand, buf, len, NULL,
635 cmd = NFC_CMD_M2N | len;
636 writel(cmd, nfc->reg_base + NFC_REG_CMD);
638 meson_nfc_drain_cmd(nfc);
639 meson_nfc_wait_cmd_finish(nfc, 1000);
640 meson_nfc_dma_buffer_release(nand, len, 0, DMA_TO_DEVICE);
645 static int meson_nfc_rw_cmd_prepare_and_execute(struct nand_chip *nand,
648 const struct nand_sdr_timings *sdr =
649 nand_get_sdr_timings(nand_get_interface_config(nand));
650 struct mtd_info *mtd = nand_to_mtd(nand);
651 struct meson_nfc *nfc = nand_get_controller_data(nand);
652 u32 *addrs = nfc->cmdfifo.rw.addrs;
653 u32 cs = nfc->param.chip_select;
654 u32 cmd0, cmd_num, row_start;
657 cmd_num = sizeof(struct nand_rw_cmd) / sizeof(int);
659 cmd0 = in ? NAND_CMD_READ0 : NAND_CMD_SEQIN;
660 nfc->cmdfifo.rw.cmd0 = cs | NFC_CMD_CLE | cmd0;
662 addrs[0] = cs | NFC_CMD_ALE | NFC_COLUMN_ADDR_0;
663 if (mtd->writesize <= 512) {
667 addrs[1] = cs | NFC_CMD_ALE | NFC_COLUMN_ADDR_1;
671 addrs[row_start] = cs | NFC_CMD_ALE | ROW_ADDER(page, 0);
672 addrs[row_start + 1] = cs | NFC_CMD_ALE | ROW_ADDER(page, 1);
674 if (nand->options & NAND_ROW_ADDR_3)
675 addrs[row_start + 2] =
676 cs | NFC_CMD_ALE | ROW_ADDER(page, 2);
683 for (i = 0; i < cmd_num; i++)
684 writel_relaxed(nfc->cmdfifo.cmd[i],
685 nfc->reg_base + NFC_REG_CMD);
688 nfc->cmdfifo.rw.cmd1 = cs | NFC_CMD_CLE | NAND_CMD_READSTART;
689 writel(nfc->cmdfifo.rw.cmd1, nfc->reg_base + NFC_REG_CMD);
690 meson_nfc_queue_rb(nfc, PSEC_TO_MSEC(sdr->tR_max), true);
692 meson_nfc_cmd_idle(nfc, nfc->timing.tadl);
698 static int meson_nfc_write_page_sub(struct nand_chip *nand,
701 const struct nand_sdr_timings *sdr =
702 nand_get_sdr_timings(nand_get_interface_config(nand));
703 struct mtd_info *mtd = nand_to_mtd(nand);
704 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
705 struct meson_nfc *nfc = nand_get_controller_data(nand);
706 int data_len, info_len;
710 meson_nfc_select_chip(nand, nand->cur_cs);
712 data_len = mtd->writesize + mtd->oobsize;
713 info_len = nand->ecc.steps * PER_INFO_BYTE;
715 ret = meson_nfc_rw_cmd_prepare_and_execute(nand, page, DIRWRITE);
719 ret = meson_nfc_dma_buffer_setup(nand, meson_chip->data_buf,
720 data_len, meson_chip->info_buf,
721 info_len, DMA_TO_DEVICE);
725 if (nand->options & NAND_NEED_SCRAMBLING) {
726 meson_nfc_cmd_seed(nfc, page);
727 meson_nfc_cmd_access(nand, raw, DIRWRITE,
728 NFC_CMD_SCRAMBLER_ENABLE);
730 meson_nfc_cmd_access(nand, raw, DIRWRITE,
731 NFC_CMD_SCRAMBLER_DISABLE);
734 cmd = nfc->param.chip_select | NFC_CMD_CLE | NAND_CMD_PAGEPROG;
735 writel(cmd, nfc->reg_base + NFC_REG_CMD);
736 meson_nfc_queue_rb(nfc, PSEC_TO_MSEC(sdr->tPROG_max), false);
738 meson_nfc_dma_buffer_release(nand, data_len, info_len, DMA_TO_DEVICE);
743 static int meson_nfc_write_page_raw(struct nand_chip *nand, const u8 *buf,
744 int oob_required, int page)
746 u8 *oob_buf = nand->oob_poi;
748 meson_nfc_set_data_oob(nand, buf, oob_buf);
750 return meson_nfc_write_page_sub(nand, page, 1);
753 static int meson_nfc_write_page_hwecc(struct nand_chip *nand,
754 const u8 *buf, int oob_required, int page)
756 struct mtd_info *mtd = nand_to_mtd(nand);
757 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
758 u8 *oob_buf = nand->oob_poi;
760 memcpy(meson_chip->data_buf, buf, mtd->writesize);
761 memset(meson_chip->info_buf, 0, nand->ecc.steps * PER_INFO_BYTE);
762 meson_nfc_set_user_byte(nand, oob_buf);
764 return meson_nfc_write_page_sub(nand, page, 0);
767 static void meson_nfc_check_ecc_pages_valid(struct meson_nfc *nfc,
768 struct nand_chip *nand, int raw)
770 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
775 neccpages = raw ? 1 : nand->ecc.steps;
776 info = &meson_chip->info_buf[neccpages - 1];
778 usleep_range(10, 15);
779 /* info is updated by nfc dma engine*/
781 dma_sync_single_for_cpu(nfc->dev, nfc->iaddr, nfc->info_bytes,
783 ret = *info & ECC_COMPLETE;
787 static int meson_nfc_read_page_sub(struct nand_chip *nand,
790 struct mtd_info *mtd = nand_to_mtd(nand);
791 struct meson_nfc *nfc = nand_get_controller_data(nand);
792 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
793 int data_len, info_len;
796 meson_nfc_select_chip(nand, nand->cur_cs);
798 data_len = mtd->writesize + mtd->oobsize;
799 info_len = nand->ecc.steps * PER_INFO_BYTE;
801 ret = meson_nfc_rw_cmd_prepare_and_execute(nand, page, DIRREAD);
805 ret = meson_nfc_dma_buffer_setup(nand, meson_chip->data_buf,
806 data_len, meson_chip->info_buf,
807 info_len, DMA_FROM_DEVICE);
811 if (nand->options & NAND_NEED_SCRAMBLING) {
812 meson_nfc_cmd_seed(nfc, page);
813 meson_nfc_cmd_access(nand, raw, DIRREAD,
814 NFC_CMD_SCRAMBLER_ENABLE);
816 meson_nfc_cmd_access(nand, raw, DIRREAD,
817 NFC_CMD_SCRAMBLER_DISABLE);
820 ret = meson_nfc_wait_dma_finish(nfc);
821 meson_nfc_check_ecc_pages_valid(nfc, nand, raw);
823 meson_nfc_dma_buffer_release(nand, data_len, info_len, DMA_FROM_DEVICE);
828 static int meson_nfc_read_page_raw(struct nand_chip *nand, u8 *buf,
829 int oob_required, int page)
831 u8 *oob_buf = nand->oob_poi;
834 ret = meson_nfc_read_page_sub(nand, page, 1);
838 meson_nfc_get_data_oob(nand, buf, oob_buf);
843 static int meson_nfc_read_page_hwecc(struct nand_chip *nand, u8 *buf,
844 int oob_required, int page)
846 struct mtd_info *mtd = nand_to_mtd(nand);
847 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
848 struct nand_ecc_ctrl *ecc = &nand->ecc;
849 u64 correct_bitmap = 0;
851 u8 *oob_buf = nand->oob_poi;
854 ret = meson_nfc_read_page_sub(nand, page, 0);
858 meson_nfc_get_user_byte(nand, oob_buf);
859 ret = meson_nfc_ecc_correct(nand, &bitflips, &correct_bitmap);
860 if (ret == ECC_CHECK_RETURN_FF) {
862 memset(buf, 0xff, mtd->writesize);
863 memset(oob_buf, 0xff, mtd->oobsize);
864 } else if (ret < 0) {
865 if ((nand->options & NAND_NEED_SCRAMBLING) || !buf) {
866 mtd->ecc_stats.failed++;
869 ret = meson_nfc_read_page_raw(nand, buf, 0, page);
873 for (i = 0; i < nand->ecc.steps ; i++) {
874 u8 *data = buf + i * ecc->size;
875 u8 *oob = nand->oob_poi + i * (ecc->bytes + 2);
877 if (correct_bitmap & BIT_ULL(i))
879 ret = nand_check_erased_ecc_chunk(data, ecc->size,
884 mtd->ecc_stats.failed++;
886 mtd->ecc_stats.corrected += ret;
887 bitflips = max_t(u32, bitflips, ret);
890 } else if (buf && buf != meson_chip->data_buf) {
891 memcpy(buf, meson_chip->data_buf, mtd->writesize);
897 static int meson_nfc_read_oob_raw(struct nand_chip *nand, int page)
899 return meson_nfc_read_page_raw(nand, NULL, 1, page);
902 static int meson_nfc_read_oob(struct nand_chip *nand, int page)
904 return meson_nfc_read_page_hwecc(nand, NULL, 1, page);
907 static bool meson_nfc_is_buffer_dma_safe(const void *buffer)
909 if ((uintptr_t)buffer % DMA_ADDR_ALIGN)
912 if (virt_addr_valid(buffer) && (!object_is_on_stack(buffer)))
918 meson_nand_op_get_dma_safe_input_buf(const struct nand_op_instr *instr)
920 if (WARN_ON(instr->type != NAND_OP_DATA_IN_INSTR))
923 if (meson_nfc_is_buffer_dma_safe(instr->ctx.data.buf.in))
924 return instr->ctx.data.buf.in;
926 return kzalloc(instr->ctx.data.len, GFP_KERNEL);
930 meson_nand_op_put_dma_safe_input_buf(const struct nand_op_instr *instr,
933 if (WARN_ON(instr->type != NAND_OP_DATA_IN_INSTR) ||
937 if (buf == instr->ctx.data.buf.in)
940 memcpy(instr->ctx.data.buf.in, buf, instr->ctx.data.len);
945 meson_nand_op_get_dma_safe_output_buf(const struct nand_op_instr *instr)
947 if (WARN_ON(instr->type != NAND_OP_DATA_OUT_INSTR))
950 if (meson_nfc_is_buffer_dma_safe(instr->ctx.data.buf.out))
951 return (void *)instr->ctx.data.buf.out;
953 return kmemdup(instr->ctx.data.buf.out,
954 instr->ctx.data.len, GFP_KERNEL);
958 meson_nand_op_put_dma_safe_output_buf(const struct nand_op_instr *instr,
961 if (WARN_ON(instr->type != NAND_OP_DATA_OUT_INSTR) ||
965 if (buf != instr->ctx.data.buf.out)
969 static int meson_nfc_check_op(struct nand_chip *chip,
970 const struct nand_operation *op)
974 for (op_id = 0; op_id < op->ninstrs; op_id++) {
975 const struct nand_op_instr *instr;
977 instr = &op->instrs[op_id];
979 switch (instr->type) {
980 case NAND_OP_DATA_IN_INSTR:
981 case NAND_OP_DATA_OUT_INSTR:
982 if (instr->ctx.data.len > NFC_CMD_RAW_LEN)
994 static int meson_nfc_exec_op(struct nand_chip *nand,
995 const struct nand_operation *op, bool check_only)
997 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
998 struct meson_nfc *nfc = nand_get_controller_data(nand);
999 const struct nand_op_instr *instr = NULL;
1001 u32 op_id, delay_idle, cmd;
1005 err = meson_nfc_check_op(nand, op);
1012 meson_nfc_select_chip(nand, op->cs);
1013 for (op_id = 0; op_id < op->ninstrs; op_id++) {
1014 instr = &op->instrs[op_id];
1015 delay_idle = DIV_ROUND_UP(PSEC_TO_NSEC(instr->delay_ns),
1016 meson_chip->level1_divider *
1018 switch (instr->type) {
1019 case NAND_OP_CMD_INSTR:
1020 cmd = nfc->param.chip_select | NFC_CMD_CLE;
1021 cmd |= instr->ctx.cmd.opcode & 0xff;
1022 writel(cmd, nfc->reg_base + NFC_REG_CMD);
1023 meson_nfc_cmd_idle(nfc, delay_idle);
1026 case NAND_OP_ADDR_INSTR:
1027 for (i = 0; i < instr->ctx.addr.naddrs; i++) {
1028 cmd = nfc->param.chip_select | NFC_CMD_ALE;
1029 cmd |= instr->ctx.addr.addrs[i] & 0xff;
1030 writel(cmd, nfc->reg_base + NFC_REG_CMD);
1032 meson_nfc_cmd_idle(nfc, delay_idle);
1035 case NAND_OP_DATA_IN_INSTR:
1036 buf = meson_nand_op_get_dma_safe_input_buf(instr);
1039 meson_nfc_read_buf(nand, buf, instr->ctx.data.len);
1040 meson_nand_op_put_dma_safe_input_buf(instr, buf);
1043 case NAND_OP_DATA_OUT_INSTR:
1044 buf = meson_nand_op_get_dma_safe_output_buf(instr);
1047 meson_nfc_write_buf(nand, buf, instr->ctx.data.len);
1048 meson_nand_op_put_dma_safe_output_buf(instr, buf);
1051 case NAND_OP_WAITRDY_INSTR:
1052 meson_nfc_queue_rb(nfc, instr->ctx.waitrdy.timeout_ms,
1054 if (instr->delay_ns)
1055 meson_nfc_cmd_idle(nfc, delay_idle);
1059 meson_nfc_wait_cmd_finish(nfc, 1000);
1063 static int meson_ooblayout_ecc(struct mtd_info *mtd, int section,
1064 struct mtd_oob_region *oobregion)
1066 struct nand_chip *nand = mtd_to_nand(mtd);
1068 if (section >= nand->ecc.steps)
1071 oobregion->offset = 2 + (section * (2 + nand->ecc.bytes));
1072 oobregion->length = nand->ecc.bytes;
1077 static int meson_ooblayout_free(struct mtd_info *mtd, int section,
1078 struct mtd_oob_region *oobregion)
1080 struct nand_chip *nand = mtd_to_nand(mtd);
1082 if (section >= nand->ecc.steps)
1085 oobregion->offset = section * (2 + nand->ecc.bytes);
1086 oobregion->length = 2;
1091 static const struct mtd_ooblayout_ops meson_ooblayout_ops = {
1092 .ecc = meson_ooblayout_ecc,
1093 .free = meson_ooblayout_free,
1096 static int meson_nfc_clk_init(struct meson_nfc *nfc)
1098 struct clk_parent_data nfc_divider_parent_data[1] = {0};
1099 struct clk_init_data init = {0};
1102 /* request core clock */
1103 nfc->core_clk = devm_clk_get(nfc->dev, "core");
1104 if (IS_ERR(nfc->core_clk)) {
1105 dev_err(nfc->dev, "failed to get core clock\n");
1106 return PTR_ERR(nfc->core_clk);
1109 nfc->device_clk = devm_clk_get(nfc->dev, "device");
1110 if (IS_ERR(nfc->device_clk)) {
1111 dev_err(nfc->dev, "failed to get device clock\n");
1112 return PTR_ERR(nfc->device_clk);
1115 init.name = devm_kasprintf(nfc->dev,
1116 GFP_KERNEL, "%s#div",
1117 dev_name(nfc->dev));
1118 init.ops = &clk_divider_ops;
1119 nfc_divider_parent_data[0].fw_name = "device";
1120 init.parent_data = nfc_divider_parent_data;
1121 init.num_parents = 1;
1122 nfc->nand_divider.reg = nfc->reg_clk;
1123 nfc->nand_divider.shift = CLK_DIV_SHIFT;
1124 nfc->nand_divider.width = CLK_DIV_WIDTH;
1125 nfc->nand_divider.hw.init = &init;
1126 nfc->nand_divider.flags = CLK_DIVIDER_ONE_BASED |
1127 CLK_DIVIDER_ROUND_CLOSEST |
1128 CLK_DIVIDER_ALLOW_ZERO;
1130 nfc->nand_clk = devm_clk_register(nfc->dev, &nfc->nand_divider.hw);
1131 if (IS_ERR(nfc->nand_clk))
1132 return PTR_ERR(nfc->nand_clk);
1134 /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
1135 writel(CLK_SELECT_NAND | readl(nfc->reg_clk),
1138 ret = clk_prepare_enable(nfc->core_clk);
1140 dev_err(nfc->dev, "failed to enable core clock\n");
1144 ret = clk_prepare_enable(nfc->device_clk);
1146 dev_err(nfc->dev, "failed to enable device clock\n");
1147 goto err_device_clk;
1150 ret = clk_prepare_enable(nfc->nand_clk);
1152 dev_err(nfc->dev, "pre enable NFC divider fail\n");
1156 ret = clk_set_rate(nfc->nand_clk, 24000000);
1158 goto err_disable_clk;
1163 clk_disable_unprepare(nfc->nand_clk);
1165 clk_disable_unprepare(nfc->device_clk);
1167 clk_disable_unprepare(nfc->core_clk);
1171 static void meson_nfc_disable_clk(struct meson_nfc *nfc)
1173 clk_disable_unprepare(nfc->nand_clk);
1174 clk_disable_unprepare(nfc->device_clk);
1175 clk_disable_unprepare(nfc->core_clk);
1178 static void meson_nfc_free_buffer(struct nand_chip *nand)
1180 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
1182 kfree(meson_chip->info_buf);
1183 kfree(meson_chip->data_buf);
1186 static int meson_chip_buffer_init(struct nand_chip *nand)
1188 struct mtd_info *mtd = nand_to_mtd(nand);
1189 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
1190 u32 page_bytes, info_bytes, nsectors;
1192 nsectors = mtd->writesize / nand->ecc.size;
1194 page_bytes = mtd->writesize + mtd->oobsize;
1195 info_bytes = nsectors * PER_INFO_BYTE;
1197 meson_chip->data_buf = kmalloc(page_bytes, GFP_KERNEL);
1198 if (!meson_chip->data_buf)
1201 meson_chip->info_buf = kmalloc(info_bytes, GFP_KERNEL);
1202 if (!meson_chip->info_buf) {
1203 kfree(meson_chip->data_buf);
1211 int meson_nfc_setup_interface(struct nand_chip *nand, int csline,
1212 const struct nand_interface_config *conf)
1214 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
1215 const struct nand_sdr_timings *timings;
1216 u32 div, bt_min, bt_max, tbers_clocks;
1218 timings = nand_get_sdr_timings(conf);
1219 if (IS_ERR(timings))
1222 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
1225 div = DIV_ROUND_UP((timings->tRC_min / 1000), NFC_CLK_CYCLE);
1226 bt_min = (timings->tREA_max + NFC_DEFAULT_DELAY) / div;
1227 bt_max = (NFC_DEFAULT_DELAY + timings->tRHOH_min +
1228 timings->tRC_min / 2) / div;
1230 meson_chip->twb = DIV_ROUND_UP(PSEC_TO_NSEC(timings->tWB_max),
1231 div * NFC_CLK_CYCLE);
1232 meson_chip->tadl = DIV_ROUND_UP(PSEC_TO_NSEC(timings->tADL_min),
1233 div * NFC_CLK_CYCLE);
1234 tbers_clocks = DIV_ROUND_UP_ULL(PSEC_TO_NSEC(timings->tBERS_max),
1235 div * NFC_CLK_CYCLE);
1236 meson_chip->tbers_max = ilog2(tbers_clocks);
1237 if (!is_power_of_2(tbers_clocks))
1238 meson_chip->tbers_max++;
1240 bt_min = DIV_ROUND_UP(bt_min, 1000);
1241 bt_max = DIV_ROUND_UP(bt_max, 1000);
1243 if (bt_max < bt_min)
1246 meson_chip->level1_divider = div;
1247 meson_chip->clk_rate = 1000000000 / meson_chip->level1_divider;
1248 meson_chip->bus_timing = (bt_min + bt_max) / 2 + 1;
1253 static int meson_nand_bch_mode(struct nand_chip *nand)
1255 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
1258 if (nand->ecc.strength > 60 || nand->ecc.strength < 8)
1261 for (i = 0; i < ARRAY_SIZE(meson_ecc); i++) {
1262 if (meson_ecc[i].strength == nand->ecc.strength) {
1263 meson_chip->bch_mode = meson_ecc[i].bch;
1271 static void meson_nand_detach_chip(struct nand_chip *nand)
1273 meson_nfc_free_buffer(nand);
1276 static int meson_nand_attach_chip(struct nand_chip *nand)
1278 struct meson_nfc *nfc = nand_get_controller_data(nand);
1279 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
1280 struct mtd_info *mtd = nand_to_mtd(nand);
1285 mtd->name = devm_kasprintf(nfc->dev, GFP_KERNEL,
1288 meson_chip->sels[0]);
1293 raw_writesize = mtd->writesize + mtd->oobsize;
1294 if (raw_writesize > NFC_CMD_RAW_LEN) {
1295 dev_err(nfc->dev, "too big write size in raw mode: %d > %ld\n",
1296 raw_writesize, NFC_CMD_RAW_LEN);
1300 if (nand->bbt_options & NAND_BBT_USE_FLASH)
1301 nand->bbt_options |= NAND_BBT_NO_OOB;
1303 nand->options |= NAND_NO_SUBPAGE_WRITE;
1305 ret = nand_ecc_choose_conf(nand, nfc->data->ecc_caps,
1308 dev_err(nfc->dev, "failed to ECC init\n");
1312 mtd_set_ooblayout(mtd, &meson_ooblayout_ops);
1314 ret = meson_nand_bch_mode(nand);
1318 nand->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
1319 nand->ecc.write_page_raw = meson_nfc_write_page_raw;
1320 nand->ecc.write_page = meson_nfc_write_page_hwecc;
1321 nand->ecc.write_oob_raw = nand_write_oob_std;
1322 nand->ecc.write_oob = nand_write_oob_std;
1324 nand->ecc.read_page_raw = meson_nfc_read_page_raw;
1325 nand->ecc.read_page = meson_nfc_read_page_hwecc;
1326 nand->ecc.read_oob_raw = meson_nfc_read_oob_raw;
1327 nand->ecc.read_oob = meson_nfc_read_oob;
1329 if (nand->options & NAND_BUSWIDTH_16) {
1330 dev_err(nfc->dev, "16bits bus width not supported");
1333 ret = meson_chip_buffer_init(nand);
1340 static const struct nand_controller_ops meson_nand_controller_ops = {
1341 .attach_chip = meson_nand_attach_chip,
1342 .detach_chip = meson_nand_detach_chip,
1343 .setup_interface = meson_nfc_setup_interface,
1344 .exec_op = meson_nfc_exec_op,
1348 meson_nfc_nand_chip_init(struct device *dev,
1349 struct meson_nfc *nfc, struct device_node *np)
1351 struct meson_nfc_nand_chip *meson_chip;
1352 struct nand_chip *nand;
1353 struct mtd_info *mtd;
1356 u32 nand_rb_val = 0;
1358 nsels = of_property_count_elems_of_size(np, "reg", sizeof(u32));
1359 if (!nsels || nsels > MAX_CE_NUM) {
1360 dev_err(dev, "invalid register property size\n");
1364 meson_chip = devm_kzalloc(dev, struct_size(meson_chip, sels, nsels),
1369 meson_chip->nsels = nsels;
1371 for (i = 0; i < nsels; i++) {
1372 ret = of_property_read_u32_index(np, "reg", i, &tmp);
1374 dev_err(dev, "could not retrieve register property: %d\n",
1379 if (test_and_set_bit(tmp, &nfc->assigned_cs)) {
1380 dev_err(dev, "CS %d already assigned\n", tmp);
1385 nand = &meson_chip->nand;
1386 nand->controller = &nfc->controller;
1387 nand->controller->ops = &meson_nand_controller_ops;
1388 nand_set_flash_node(nand, np);
1389 nand_set_controller_data(nand, nfc);
1391 nand->options |= NAND_USES_DMA;
1392 mtd = nand_to_mtd(nand);
1393 mtd->owner = THIS_MODULE;
1394 mtd->dev.parent = dev;
1396 ret = of_property_read_u32(np, "nand-rb", &nand_rb_val);
1398 nfc->no_rb_pin = true;
1405 ret = nand_scan(nand, nsels);
1409 ret = mtd_device_register(mtd, NULL, 0);
1411 dev_err(dev, "failed to register MTD device: %d\n", ret);
1416 list_add_tail(&meson_chip->node, &nfc->chips);
1421 static void meson_nfc_nand_chip_cleanup(struct meson_nfc *nfc)
1423 struct meson_nfc_nand_chip *meson_chip;
1424 struct mtd_info *mtd;
1426 while (!list_empty(&nfc->chips)) {
1427 meson_chip = list_first_entry(&nfc->chips,
1428 struct meson_nfc_nand_chip, node);
1429 mtd = nand_to_mtd(&meson_chip->nand);
1430 WARN_ON(mtd_device_unregister(mtd));
1432 nand_cleanup(&meson_chip->nand);
1433 list_del(&meson_chip->node);
1437 static int meson_nfc_nand_chips_init(struct device *dev,
1438 struct meson_nfc *nfc)
1440 struct device_node *np = dev->of_node;
1441 struct device_node *nand_np;
1444 for_each_child_of_node(np, nand_np) {
1445 ret = meson_nfc_nand_chip_init(dev, nfc, nand_np);
1447 meson_nfc_nand_chip_cleanup(nfc);
1448 of_node_put(nand_np);
1456 static irqreturn_t meson_nfc_irq(int irq, void *id)
1458 struct meson_nfc *nfc = id;
1461 cfg = readl(nfc->reg_base + NFC_REG_CFG);
1462 if (!(cfg & NFC_RB_IRQ_EN))
1465 cfg &= ~(NFC_RB_IRQ_EN);
1466 writel(cfg, nfc->reg_base + NFC_REG_CFG);
1468 complete(&nfc->completion);
1472 static const struct meson_nfc_data meson_gxl_data = {
1473 .ecc_caps = &meson_gxl_ecc_caps,
1476 static const struct meson_nfc_data meson_axg_data = {
1477 .ecc_caps = &meson_axg_ecc_caps,
1480 static const struct of_device_id meson_nfc_id_table[] = {
1482 .compatible = "amlogic,meson-gxl-nfc",
1483 .data = &meson_gxl_data,
1485 .compatible = "amlogic,meson-axg-nfc",
1486 .data = &meson_axg_data,
1490 MODULE_DEVICE_TABLE(of, meson_nfc_id_table);
1492 static int meson_nfc_probe(struct platform_device *pdev)
1494 struct device *dev = &pdev->dev;
1495 struct meson_nfc *nfc;
1498 nfc = devm_kzalloc(dev, sizeof(*nfc), GFP_KERNEL);
1502 nfc->data = of_device_get_match_data(&pdev->dev);
1506 nand_controller_init(&nfc->controller);
1507 INIT_LIST_HEAD(&nfc->chips);
1508 init_completion(&nfc->completion);
1512 nfc->reg_base = devm_platform_ioremap_resource_byname(pdev, "nfc");
1513 if (IS_ERR(nfc->reg_base))
1514 return PTR_ERR(nfc->reg_base);
1516 nfc->reg_clk = devm_platform_ioremap_resource_byname(pdev, "emmc");
1517 if (IS_ERR(nfc->reg_clk))
1518 return PTR_ERR(nfc->reg_clk);
1520 irq = platform_get_irq(pdev, 0);
1524 ret = meson_nfc_clk_init(nfc);
1526 dev_err(dev, "failed to initialize NAND clock\n");
1530 writel(0, nfc->reg_base + NFC_REG_CFG);
1531 ret = devm_request_irq(dev, irq, meson_nfc_irq, 0, dev_name(dev), nfc);
1533 dev_err(dev, "failed to request NFC IRQ\n");
1538 ret = dma_set_mask(dev, DMA_BIT_MASK(32));
1540 dev_err(dev, "failed to set DMA mask\n");
1544 platform_set_drvdata(pdev, nfc);
1546 ret = meson_nfc_nand_chips_init(dev, nfc);
1548 dev_err(dev, "failed to init NAND chips\n");
1554 meson_nfc_disable_clk(nfc);
1558 static void meson_nfc_remove(struct platform_device *pdev)
1560 struct meson_nfc *nfc = platform_get_drvdata(pdev);
1562 meson_nfc_nand_chip_cleanup(nfc);
1564 meson_nfc_disable_clk(nfc);
1567 static struct platform_driver meson_nfc_driver = {
1568 .probe = meson_nfc_probe,
1569 .remove_new = meson_nfc_remove,
1571 .name = "meson-nand",
1572 .of_match_table = meson_nfc_id_table,
1575 module_platform_driver(meson_nfc_driver);
1577 MODULE_LICENSE("Dual MIT/GPL");
1578 MODULE_AUTHOR("Liang Yang <liang.yang@amlogic.com>");
1579 MODULE_DESCRIPTION("Amlogic's Meson NAND Flash Controller driver");