Lines Matching +full:nand +full:- +full:ecc +full:- +full:strength
1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
3 * Amlogic Meson Nand Flash Controller Driver
10 #include <linux/dma-mapping.h>
13 #include <linux/clk-provider.h>
80 #define ECC_CHECK_RETURN_FF (-1)
95 /* nand flash controller delay 3 ns */
113 struct nand_chip nand; member
130 u32 strength; member
192 #define MESON_ECC_DATA(b, s) { .bch = (b), .strength = (s)}
203 static int meson_nand_calc_ecc_bytes(int step_size, int strength) in meson_nand_calc_ecc_bytes() argument
207 if (step_size == 512 && strength == 8) in meson_nand_calc_ecc_bytes()
210 ecc_bytes = DIV_ROUND_UP(strength * fls(step_size * 8), 8); in meson_nand_calc_ecc_bytes()
221 static struct meson_nfc_nand_chip *to_meson_nand(struct nand_chip *nand) in to_meson_nand() argument
223 return container_of(nand, struct meson_nfc_nand_chip, nand); in to_meson_nand()
226 static void meson_nfc_select_chip(struct nand_chip *nand, int chip) in meson_nfc_select_chip() argument
228 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_select_chip()
229 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_select_chip()
232 if (chip < 0 || WARN_ON_ONCE(chip >= meson_chip->nsels)) in meson_nfc_select_chip()
235 nfc->param.chip_select = meson_chip->sels[chip] ? NAND_CE1 : NAND_CE0; in meson_nfc_select_chip()
236 nfc->param.rb_select = nfc->param.chip_select; in meson_nfc_select_chip()
237 nfc->timing.twb = meson_chip->twb; in meson_nfc_select_chip()
238 nfc->timing.tadl = meson_chip->tadl; in meson_nfc_select_chip()
239 nfc->timing.tbers_max = meson_chip->tbers_max; in meson_nfc_select_chip()
241 if (nfc->clk_rate != meson_chip->clk_rate) { in meson_nfc_select_chip()
242 ret = clk_set_rate(nfc->nand_clk, meson_chip->clk_rate); in meson_nfc_select_chip()
244 dev_err(nfc->dev, "failed to set clock rate\n"); in meson_nfc_select_chip()
247 nfc->clk_rate = meson_chip->clk_rate; in meson_nfc_select_chip()
249 if (nfc->bus_timing != meson_chip->bus_timing) { in meson_nfc_select_chip()
250 value = (NFC_CLK_CYCLE - 1) | (meson_chip->bus_timing << 5); in meson_nfc_select_chip()
251 writel(value, nfc->reg_base + NFC_REG_CFG); in meson_nfc_select_chip()
252 writel((1 << 31), nfc->reg_base + NFC_REG_CMD); in meson_nfc_select_chip()
253 nfc->bus_timing = meson_chip->bus_timing; in meson_nfc_select_chip()
259 writel(nfc->param.chip_select | NFC_CMD_IDLE | (time & 0x3ff), in meson_nfc_cmd_idle()
260 nfc->reg_base + NFC_REG_CMD); in meson_nfc_cmd_idle()
266 nfc->reg_base + NFC_REG_CMD); in meson_nfc_cmd_seed()
269 static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir, in meson_nfc_cmd_access() argument
272 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nfc_cmd_access()
274 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_cmd_access()
275 u32 bch = meson_chip->bch_mode, cmd; in meson_nfc_cmd_access()
276 int len = mtd->writesize, pagesize, pages; in meson_nfc_cmd_access()
278 pagesize = nand->ecc.size; in meson_nfc_cmd_access()
281 len = mtd->writesize + mtd->oobsize; in meson_nfc_cmd_access()
283 writel(cmd, nfc->reg_base + NFC_REG_CMD); in meson_nfc_cmd_access()
287 pages = len / nand->ecc.size; in meson_nfc_cmd_access()
292 writel(cmd, nfc->reg_base + NFC_REG_CMD); in meson_nfc_cmd_access()
300 * The Nand flash controller is designed as two stages pipleline - in meson_nfc_drain_cmd()
303 * but the Nand flash controller still has two commands buffered, in meson_nfc_drain_cmd()
319 ret = readl_relaxed_poll_timeout(nfc->reg_base + NFC_REG_CMD, cmd_size, in meson_nfc_wait_cmd_finish()
323 dev_err(nfc->dev, "wait for empty CMD FIFO time out\n"); in meson_nfc_wait_cmd_finish()
335 static u8 *meson_nfc_oob_ptr(struct nand_chip *nand, int i) in meson_nfc_oob_ptr() argument
337 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_oob_ptr()
340 len = nand->ecc.size * (i + 1) + (nand->ecc.bytes + 2) * i; in meson_nfc_oob_ptr()
342 return meson_chip->data_buf + len; in meson_nfc_oob_ptr()
345 static u8 *meson_nfc_data_ptr(struct nand_chip *nand, int i) in meson_nfc_data_ptr() argument
347 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_data_ptr()
350 temp = nand->ecc.size + nand->ecc.bytes; in meson_nfc_data_ptr()
353 return meson_chip->data_buf + len; in meson_nfc_data_ptr()
356 static void meson_nfc_get_data_oob(struct nand_chip *nand, in meson_nfc_get_data_oob() argument
362 oob_len = nand->ecc.bytes + 2; in meson_nfc_get_data_oob()
363 for (i = 0; i < nand->ecc.steps; i++) { in meson_nfc_get_data_oob()
365 dsrc = meson_nfc_data_ptr(nand, i); in meson_nfc_get_data_oob()
366 memcpy(buf, dsrc, nand->ecc.size); in meson_nfc_get_data_oob()
367 buf += nand->ecc.size; in meson_nfc_get_data_oob()
369 osrc = meson_nfc_oob_ptr(nand, i); in meson_nfc_get_data_oob()
375 static void meson_nfc_set_data_oob(struct nand_chip *nand, in meson_nfc_set_data_oob() argument
381 oob_len = nand->ecc.bytes + 2; in meson_nfc_set_data_oob()
382 for (i = 0; i < nand->ecc.steps; i++) { in meson_nfc_set_data_oob()
384 dsrc = meson_nfc_data_ptr(nand, i); in meson_nfc_set_data_oob()
385 memcpy(dsrc, buf, nand->ecc.size); in meson_nfc_set_data_oob()
386 buf += nand->ecc.size; in meson_nfc_set_data_oob()
388 osrc = meson_nfc_oob_ptr(nand, i); in meson_nfc_set_data_oob()
399 meson_nfc_cmd_idle(nfc, nfc->timing.twb); in meson_nfc_queue_rb()
403 cfg = readl(nfc->reg_base + NFC_REG_CFG); in meson_nfc_queue_rb()
405 writel(cfg, nfc->reg_base + NFC_REG_CFG); in meson_nfc_queue_rb()
407 reinit_completion(&nfc->completion); in meson_nfc_queue_rb()
411 | nfc->param.chip_select | nfc->timing.tbers_max; in meson_nfc_queue_rb()
412 writel(cmd, nfc->reg_base + NFC_REG_CMD); in meson_nfc_queue_rb()
414 ret = wait_for_completion_timeout(&nfc->completion, in meson_nfc_queue_rb()
417 ret = -1; in meson_nfc_queue_rb()
422 static void meson_nfc_set_user_byte(struct nand_chip *nand, u8 *oob_buf) in meson_nfc_set_user_byte() argument
424 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_set_user_byte()
428 for (i = 0, count = 0; i < nand->ecc.steps; i++, count += 2) { in meson_nfc_set_user_byte()
429 info = &meson_chip->info_buf[i]; in meson_nfc_set_user_byte()
435 static void meson_nfc_get_user_byte(struct nand_chip *nand, u8 *oob_buf) in meson_nfc_get_user_byte() argument
437 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_get_user_byte()
441 for (i = 0, count = 0; i < nand->ecc.steps; i++, count += 2) { in meson_nfc_get_user_byte()
442 info = &meson_chip->info_buf[i]; in meson_nfc_get_user_byte()
448 static int meson_nfc_ecc_correct(struct nand_chip *nand, u32 *bitflips, in meson_nfc_ecc_correct() argument
451 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nfc_ecc_correct()
452 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_ecc_correct()
456 for (i = 0; i < nand->ecc.steps; i++) { in meson_nfc_ecc_correct()
457 info = &meson_chip->info_buf[i]; in meson_nfc_ecc_correct()
459 mtd->ecc_stats.corrected += ECC_ERR_CNT(*info); in meson_nfc_ecc_correct()
464 if ((nand->options & NAND_NEED_SCRAMBLING) && in meson_nfc_ecc_correct()
465 ECC_ZERO_CNT(*info) < nand->ecc.strength) { in meson_nfc_ecc_correct()
466 mtd->ecc_stats.corrected += ECC_ZERO_CNT(*info); in meson_nfc_ecc_correct()
471 ret = -EBADMSG; in meson_nfc_ecc_correct()
477 static int meson_nfc_dma_buffer_setup(struct nand_chip *nand, void *databuf, in meson_nfc_dma_buffer_setup() argument
481 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_dma_buffer_setup()
485 nfc->daddr = dma_map_single(nfc->dev, databuf, datalen, dir); in meson_nfc_dma_buffer_setup()
486 ret = dma_mapping_error(nfc->dev, nfc->daddr); in meson_nfc_dma_buffer_setup()
488 dev_err(nfc->dev, "DMA mapping error\n"); in meson_nfc_dma_buffer_setup()
491 cmd = GENCMDDADDRL(NFC_CMD_ADL, nfc->daddr); in meson_nfc_dma_buffer_setup()
492 writel(cmd, nfc->reg_base + NFC_REG_CMD); in meson_nfc_dma_buffer_setup()
494 cmd = GENCMDDADDRH(NFC_CMD_ADH, nfc->daddr); in meson_nfc_dma_buffer_setup()
495 writel(cmd, nfc->reg_base + NFC_REG_CMD); in meson_nfc_dma_buffer_setup()
498 nfc->iaddr = dma_map_single(nfc->dev, infobuf, infolen, dir); in meson_nfc_dma_buffer_setup()
499 ret = dma_mapping_error(nfc->dev, nfc->iaddr); in meson_nfc_dma_buffer_setup()
501 dev_err(nfc->dev, "DMA mapping error\n"); in meson_nfc_dma_buffer_setup()
502 dma_unmap_single(nfc->dev, in meson_nfc_dma_buffer_setup()
503 nfc->daddr, datalen, dir); in meson_nfc_dma_buffer_setup()
506 cmd = GENCMDIADDRL(NFC_CMD_AIL, nfc->iaddr); in meson_nfc_dma_buffer_setup()
507 writel(cmd, nfc->reg_base + NFC_REG_CMD); in meson_nfc_dma_buffer_setup()
509 cmd = GENCMDIADDRH(NFC_CMD_AIH, nfc->iaddr); in meson_nfc_dma_buffer_setup()
510 writel(cmd, nfc->reg_base + NFC_REG_CMD); in meson_nfc_dma_buffer_setup()
516 static void meson_nfc_dma_buffer_release(struct nand_chip *nand, in meson_nfc_dma_buffer_release() argument
520 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_dma_buffer_release()
522 dma_unmap_single(nfc->dev, nfc->daddr, datalen, dir); in meson_nfc_dma_buffer_release()
524 dma_unmap_single(nfc->dev, nfc->iaddr, infolen, dir); in meson_nfc_dma_buffer_release()
527 static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len) in meson_nfc_read_buf() argument
529 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_read_buf()
536 return -ENOMEM; in meson_nfc_read_buf()
538 ret = meson_nfc_dma_buffer_setup(nand, buf, len, info, in meson_nfc_read_buf()
544 writel(cmd, nfc->reg_base + NFC_REG_CMD); in meson_nfc_read_buf()
548 meson_nfc_dma_buffer_release(nand, len, PER_INFO_BYTE, DMA_FROM_DEVICE); in meson_nfc_read_buf()
556 static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len) in meson_nfc_write_buf() argument
558 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_write_buf()
562 ret = meson_nfc_dma_buffer_setup(nand, buf, len, NULL, in meson_nfc_write_buf()
568 writel(cmd, nfc->reg_base + NFC_REG_CMD); in meson_nfc_write_buf()
572 meson_nfc_dma_buffer_release(nand, len, 0, DMA_TO_DEVICE); in meson_nfc_write_buf()
577 static int meson_nfc_rw_cmd_prepare_and_execute(struct nand_chip *nand, in meson_nfc_rw_cmd_prepare_and_execute() argument
581 nand_get_sdr_timings(nand_get_interface_config(nand)); in meson_nfc_rw_cmd_prepare_and_execute()
582 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nfc_rw_cmd_prepare_and_execute()
583 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_rw_cmd_prepare_and_execute()
584 u32 *addrs = nfc->cmdfifo.rw.addrs; in meson_nfc_rw_cmd_prepare_and_execute()
585 u32 cs = nfc->param.chip_select; in meson_nfc_rw_cmd_prepare_and_execute()
592 nfc->cmdfifo.rw.cmd0 = cs | NFC_CMD_CLE | cmd0; in meson_nfc_rw_cmd_prepare_and_execute()
595 if (mtd->writesize <= 512) { in meson_nfc_rw_cmd_prepare_and_execute()
596 cmd_num--; in meson_nfc_rw_cmd_prepare_and_execute()
606 if (nand->options & NAND_ROW_ADDR_3) in meson_nfc_rw_cmd_prepare_and_execute()
610 cmd_num--; in meson_nfc_rw_cmd_prepare_and_execute()
613 cmd_num--; in meson_nfc_rw_cmd_prepare_and_execute()
616 writel_relaxed(nfc->cmdfifo.cmd[i], in meson_nfc_rw_cmd_prepare_and_execute()
617 nfc->reg_base + NFC_REG_CMD); in meson_nfc_rw_cmd_prepare_and_execute()
620 nfc->cmdfifo.rw.cmd1 = cs | NFC_CMD_CLE | NAND_CMD_READSTART; in meson_nfc_rw_cmd_prepare_and_execute()
621 writel(nfc->cmdfifo.rw.cmd1, nfc->reg_base + NFC_REG_CMD); in meson_nfc_rw_cmd_prepare_and_execute()
622 meson_nfc_queue_rb(nfc, PSEC_TO_MSEC(sdr->tR_max)); in meson_nfc_rw_cmd_prepare_and_execute()
624 meson_nfc_cmd_idle(nfc, nfc->timing.tadl); in meson_nfc_rw_cmd_prepare_and_execute()
630 static int meson_nfc_write_page_sub(struct nand_chip *nand, in meson_nfc_write_page_sub() argument
634 nand_get_sdr_timings(nand_get_interface_config(nand)); in meson_nfc_write_page_sub()
635 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nfc_write_page_sub()
636 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_write_page_sub()
637 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_write_page_sub()
642 meson_nfc_select_chip(nand, nand->cur_cs); in meson_nfc_write_page_sub()
644 data_len = mtd->writesize + mtd->oobsize; in meson_nfc_write_page_sub()
645 info_len = nand->ecc.steps * PER_INFO_BYTE; in meson_nfc_write_page_sub()
647 ret = meson_nfc_rw_cmd_prepare_and_execute(nand, page, DIRWRITE); in meson_nfc_write_page_sub()
651 ret = meson_nfc_dma_buffer_setup(nand, meson_chip->data_buf, in meson_nfc_write_page_sub()
652 data_len, meson_chip->info_buf, in meson_nfc_write_page_sub()
657 if (nand->options & NAND_NEED_SCRAMBLING) { in meson_nfc_write_page_sub()
659 meson_nfc_cmd_access(nand, raw, DIRWRITE, in meson_nfc_write_page_sub()
662 meson_nfc_cmd_access(nand, raw, DIRWRITE, in meson_nfc_write_page_sub()
666 cmd = nfc->param.chip_select | NFC_CMD_CLE | NAND_CMD_PAGEPROG; in meson_nfc_write_page_sub()
667 writel(cmd, nfc->reg_base + NFC_REG_CMD); in meson_nfc_write_page_sub()
668 meson_nfc_queue_rb(nfc, PSEC_TO_MSEC(sdr->tPROG_max)); in meson_nfc_write_page_sub()
670 meson_nfc_dma_buffer_release(nand, data_len, info_len, DMA_TO_DEVICE); in meson_nfc_write_page_sub()
675 static int meson_nfc_write_page_raw(struct nand_chip *nand, const u8 *buf, in meson_nfc_write_page_raw() argument
678 u8 *oob_buf = nand->oob_poi; in meson_nfc_write_page_raw()
680 meson_nfc_set_data_oob(nand, buf, oob_buf); in meson_nfc_write_page_raw()
682 return meson_nfc_write_page_sub(nand, page, 1); in meson_nfc_write_page_raw()
685 static int meson_nfc_write_page_hwecc(struct nand_chip *nand, in meson_nfc_write_page_hwecc() argument
688 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nfc_write_page_hwecc()
689 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_write_page_hwecc()
690 u8 *oob_buf = nand->oob_poi; in meson_nfc_write_page_hwecc()
692 memcpy(meson_chip->data_buf, buf, mtd->writesize); in meson_nfc_write_page_hwecc()
693 memset(meson_chip->info_buf, 0, nand->ecc.steps * PER_INFO_BYTE); in meson_nfc_write_page_hwecc()
694 meson_nfc_set_user_byte(nand, oob_buf); in meson_nfc_write_page_hwecc()
696 return meson_nfc_write_page_sub(nand, page, 0); in meson_nfc_write_page_hwecc()
700 struct nand_chip *nand, int raw) in meson_nfc_check_ecc_pages_valid() argument
702 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_check_ecc_pages_valid()
707 neccpages = raw ? 1 : nand->ecc.steps; in meson_nfc_check_ecc_pages_valid()
708 info = &meson_chip->info_buf[neccpages - 1]; in meson_nfc_check_ecc_pages_valid()
717 static int meson_nfc_read_page_sub(struct nand_chip *nand, in meson_nfc_read_page_sub() argument
720 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nfc_read_page_sub()
721 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_read_page_sub()
722 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_read_page_sub()
726 meson_nfc_select_chip(nand, nand->cur_cs); in meson_nfc_read_page_sub()
728 data_len = mtd->writesize + mtd->oobsize; in meson_nfc_read_page_sub()
729 info_len = nand->ecc.steps * PER_INFO_BYTE; in meson_nfc_read_page_sub()
731 ret = meson_nfc_rw_cmd_prepare_and_execute(nand, page, DIRREAD); in meson_nfc_read_page_sub()
735 ret = meson_nfc_dma_buffer_setup(nand, meson_chip->data_buf, in meson_nfc_read_page_sub()
736 data_len, meson_chip->info_buf, in meson_nfc_read_page_sub()
741 if (nand->options & NAND_NEED_SCRAMBLING) { in meson_nfc_read_page_sub()
743 meson_nfc_cmd_access(nand, raw, DIRREAD, in meson_nfc_read_page_sub()
746 meson_nfc_cmd_access(nand, raw, DIRREAD, in meson_nfc_read_page_sub()
751 meson_nfc_check_ecc_pages_valid(nfc, nand, raw); in meson_nfc_read_page_sub()
753 meson_nfc_dma_buffer_release(nand, data_len, info_len, DMA_FROM_DEVICE); in meson_nfc_read_page_sub()
758 static int meson_nfc_read_page_raw(struct nand_chip *nand, u8 *buf, in meson_nfc_read_page_raw() argument
761 u8 *oob_buf = nand->oob_poi; in meson_nfc_read_page_raw()
764 ret = meson_nfc_read_page_sub(nand, page, 1); in meson_nfc_read_page_raw()
768 meson_nfc_get_data_oob(nand, buf, oob_buf); in meson_nfc_read_page_raw()
773 static int meson_nfc_read_page_hwecc(struct nand_chip *nand, u8 *buf, in meson_nfc_read_page_hwecc() argument
776 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nfc_read_page_hwecc()
777 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_read_page_hwecc()
778 struct nand_ecc_ctrl *ecc = &nand->ecc; in meson_nfc_read_page_hwecc() local
781 u8 *oob_buf = nand->oob_poi; in meson_nfc_read_page_hwecc()
784 ret = meson_nfc_read_page_sub(nand, page, 0); in meson_nfc_read_page_hwecc()
788 meson_nfc_get_user_byte(nand, oob_buf); in meson_nfc_read_page_hwecc()
789 ret = meson_nfc_ecc_correct(nand, &bitflips, &correct_bitmap); in meson_nfc_read_page_hwecc()
792 memset(buf, 0xff, mtd->writesize); in meson_nfc_read_page_hwecc()
793 memset(oob_buf, 0xff, mtd->oobsize); in meson_nfc_read_page_hwecc()
795 if ((nand->options & NAND_NEED_SCRAMBLING) || !buf) { in meson_nfc_read_page_hwecc()
796 mtd->ecc_stats.failed++; in meson_nfc_read_page_hwecc()
799 ret = meson_nfc_read_page_raw(nand, buf, 0, page); in meson_nfc_read_page_hwecc()
803 for (i = 0; i < nand->ecc.steps ; i++) { in meson_nfc_read_page_hwecc()
804 u8 *data = buf + i * ecc->size; in meson_nfc_read_page_hwecc()
805 u8 *oob = nand->oob_poi + i * (ecc->bytes + 2); in meson_nfc_read_page_hwecc()
809 ret = nand_check_erased_ecc_chunk(data, ecc->size, in meson_nfc_read_page_hwecc()
810 oob, ecc->bytes + 2, in meson_nfc_read_page_hwecc()
812 ecc->strength); in meson_nfc_read_page_hwecc()
814 mtd->ecc_stats.failed++; in meson_nfc_read_page_hwecc()
816 mtd->ecc_stats.corrected += ret; in meson_nfc_read_page_hwecc()
820 } else if (buf && buf != meson_chip->data_buf) { in meson_nfc_read_page_hwecc()
821 memcpy(buf, meson_chip->data_buf, mtd->writesize); in meson_nfc_read_page_hwecc()
827 static int meson_nfc_read_oob_raw(struct nand_chip *nand, int page) in meson_nfc_read_oob_raw() argument
829 return meson_nfc_read_page_raw(nand, NULL, 1, page); in meson_nfc_read_oob_raw()
832 static int meson_nfc_read_oob(struct nand_chip *nand, int page) in meson_nfc_read_oob() argument
834 return meson_nfc_read_page_hwecc(nand, NULL, 1, page); in meson_nfc_read_oob()
847 if (WARN_ON(instr->type != NAND_OP_DATA_IN_INSTR)) in meson_nand_op_get_dma_safe_input_buf()
850 if (meson_nfc_is_buffer_dma_safe(instr->ctx.data.buf.in)) in meson_nand_op_get_dma_safe_input_buf()
851 return instr->ctx.data.buf.in; in meson_nand_op_get_dma_safe_input_buf()
853 return kzalloc(instr->ctx.data.len, GFP_KERNEL); in meson_nand_op_get_dma_safe_input_buf()
860 if (WARN_ON(instr->type != NAND_OP_DATA_IN_INSTR) || in meson_nand_op_put_dma_safe_input_buf()
864 if (buf == instr->ctx.data.buf.in) in meson_nand_op_put_dma_safe_input_buf()
867 memcpy(instr->ctx.data.buf.in, buf, instr->ctx.data.len); in meson_nand_op_put_dma_safe_input_buf()
874 if (WARN_ON(instr->type != NAND_OP_DATA_OUT_INSTR)) in meson_nand_op_get_dma_safe_output_buf()
877 if (meson_nfc_is_buffer_dma_safe(instr->ctx.data.buf.out)) in meson_nand_op_get_dma_safe_output_buf()
878 return (void *)instr->ctx.data.buf.out; in meson_nand_op_get_dma_safe_output_buf()
880 return kmemdup(instr->ctx.data.buf.out, in meson_nand_op_get_dma_safe_output_buf()
881 instr->ctx.data.len, GFP_KERNEL); in meson_nand_op_get_dma_safe_output_buf()
888 if (WARN_ON(instr->type != NAND_OP_DATA_OUT_INSTR) || in meson_nand_op_put_dma_safe_output_buf()
892 if (buf != instr->ctx.data.buf.out) in meson_nand_op_put_dma_safe_output_buf()
896 static int meson_nfc_exec_op(struct nand_chip *nand, in meson_nfc_exec_op() argument
899 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_exec_op()
900 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_exec_op()
909 meson_nfc_select_chip(nand, op->cs); in meson_nfc_exec_op()
910 for (op_id = 0; op_id < op->ninstrs; op_id++) { in meson_nfc_exec_op()
911 instr = &op->instrs[op_id]; in meson_nfc_exec_op()
912 delay_idle = DIV_ROUND_UP(PSEC_TO_NSEC(instr->delay_ns), in meson_nfc_exec_op()
913 meson_chip->level1_divider * in meson_nfc_exec_op()
915 switch (instr->type) { in meson_nfc_exec_op()
917 cmd = nfc->param.chip_select | NFC_CMD_CLE; in meson_nfc_exec_op()
918 cmd |= instr->ctx.cmd.opcode & 0xff; in meson_nfc_exec_op()
919 writel(cmd, nfc->reg_base + NFC_REG_CMD); in meson_nfc_exec_op()
924 for (i = 0; i < instr->ctx.addr.naddrs; i++) { in meson_nfc_exec_op()
925 cmd = nfc->param.chip_select | NFC_CMD_ALE; in meson_nfc_exec_op()
926 cmd |= instr->ctx.addr.addrs[i] & 0xff; in meson_nfc_exec_op()
927 writel(cmd, nfc->reg_base + NFC_REG_CMD); in meson_nfc_exec_op()
935 return -ENOMEM; in meson_nfc_exec_op()
936 meson_nfc_read_buf(nand, buf, instr->ctx.data.len); in meson_nfc_exec_op()
943 return -ENOMEM; in meson_nfc_exec_op()
944 meson_nfc_write_buf(nand, buf, instr->ctx.data.len); in meson_nfc_exec_op()
949 meson_nfc_queue_rb(nfc, instr->ctx.waitrdy.timeout_ms); in meson_nfc_exec_op()
950 if (instr->delay_ns) in meson_nfc_exec_op()
962 struct nand_chip *nand = mtd_to_nand(mtd); in meson_ooblayout_ecc() local
964 if (section >= nand->ecc.steps) in meson_ooblayout_ecc()
965 return -ERANGE; in meson_ooblayout_ecc()
967 oobregion->offset = 2 + (section * (2 + nand->ecc.bytes)); in meson_ooblayout_ecc()
968 oobregion->length = nand->ecc.bytes; in meson_ooblayout_ecc()
976 struct nand_chip *nand = mtd_to_nand(mtd); in meson_ooblayout_free() local
978 if (section >= nand->ecc.steps) in meson_ooblayout_free()
979 return -ERANGE; in meson_ooblayout_free()
981 oobregion->offset = section * (2 + nand->ecc.bytes); in meson_ooblayout_free()
982 oobregion->length = 2; in meson_ooblayout_free()
988 .ecc = meson_ooblayout_ecc,
999 nfc->core_clk = devm_clk_get(nfc->dev, "core"); in meson_nfc_clk_init()
1000 if (IS_ERR(nfc->core_clk)) { in meson_nfc_clk_init()
1001 dev_err(nfc->dev, "failed to get core clock\n"); in meson_nfc_clk_init()
1002 return PTR_ERR(nfc->core_clk); in meson_nfc_clk_init()
1005 nfc->device_clk = devm_clk_get(nfc->dev, "device"); in meson_nfc_clk_init()
1006 if (IS_ERR(nfc->device_clk)) { in meson_nfc_clk_init()
1007 dev_err(nfc->dev, "failed to get device clock\n"); in meson_nfc_clk_init()
1008 return PTR_ERR(nfc->device_clk); in meson_nfc_clk_init()
1011 init.name = devm_kasprintf(nfc->dev, in meson_nfc_clk_init()
1013 dev_name(nfc->dev)); in meson_nfc_clk_init()
1018 nfc->nand_divider.reg = nfc->reg_clk; in meson_nfc_clk_init()
1019 nfc->nand_divider.shift = CLK_DIV_SHIFT; in meson_nfc_clk_init()
1020 nfc->nand_divider.width = CLK_DIV_WIDTH; in meson_nfc_clk_init()
1021 nfc->nand_divider.hw.init = &init; in meson_nfc_clk_init()
1022 nfc->nand_divider.flags = CLK_DIVIDER_ONE_BASED | in meson_nfc_clk_init()
1026 nfc->nand_clk = devm_clk_register(nfc->dev, &nfc->nand_divider.hw); in meson_nfc_clk_init()
1027 if (IS_ERR(nfc->nand_clk)) in meson_nfc_clk_init()
1028 return PTR_ERR(nfc->nand_clk); in meson_nfc_clk_init()
1031 writel(CLK_SELECT_NAND | readl(nfc->reg_clk), in meson_nfc_clk_init()
1032 nfc->reg_clk); in meson_nfc_clk_init()
1034 ret = clk_prepare_enable(nfc->core_clk); in meson_nfc_clk_init()
1036 dev_err(nfc->dev, "failed to enable core clock\n"); in meson_nfc_clk_init()
1040 ret = clk_prepare_enable(nfc->device_clk); in meson_nfc_clk_init()
1042 dev_err(nfc->dev, "failed to enable device clock\n"); in meson_nfc_clk_init()
1046 ret = clk_prepare_enable(nfc->nand_clk); in meson_nfc_clk_init()
1048 dev_err(nfc->dev, "pre enable NFC divider fail\n"); in meson_nfc_clk_init()
1052 ret = clk_set_rate(nfc->nand_clk, 24000000); in meson_nfc_clk_init()
1059 clk_disable_unprepare(nfc->nand_clk); in meson_nfc_clk_init()
1061 clk_disable_unprepare(nfc->device_clk); in meson_nfc_clk_init()
1063 clk_disable_unprepare(nfc->core_clk); in meson_nfc_clk_init()
1069 clk_disable_unprepare(nfc->nand_clk); in meson_nfc_disable_clk()
1070 clk_disable_unprepare(nfc->device_clk); in meson_nfc_disable_clk()
1071 clk_disable_unprepare(nfc->core_clk); in meson_nfc_disable_clk()
1074 static void meson_nfc_free_buffer(struct nand_chip *nand) in meson_nfc_free_buffer() argument
1076 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_free_buffer()
1078 kfree(meson_chip->info_buf); in meson_nfc_free_buffer()
1079 kfree(meson_chip->data_buf); in meson_nfc_free_buffer()
1082 static int meson_chip_buffer_init(struct nand_chip *nand) in meson_chip_buffer_init() argument
1084 struct mtd_info *mtd = nand_to_mtd(nand); in meson_chip_buffer_init()
1085 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_chip_buffer_init()
1088 nsectors = mtd->writesize / nand->ecc.size; in meson_chip_buffer_init()
1090 page_bytes = mtd->writesize + mtd->oobsize; in meson_chip_buffer_init()
1093 meson_chip->data_buf = kmalloc(page_bytes, GFP_KERNEL); in meson_chip_buffer_init()
1094 if (!meson_chip->data_buf) in meson_chip_buffer_init()
1095 return -ENOMEM; in meson_chip_buffer_init()
1097 meson_chip->info_buf = kmalloc(info_bytes, GFP_KERNEL); in meson_chip_buffer_init()
1098 if (!meson_chip->info_buf) { in meson_chip_buffer_init()
1099 kfree(meson_chip->data_buf); in meson_chip_buffer_init()
1100 return -ENOMEM; in meson_chip_buffer_init()
1107 int meson_nfc_setup_interface(struct nand_chip *nand, int csline, in meson_nfc_setup_interface() argument
1110 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_setup_interface()
1116 return -ENOTSUPP; in meson_nfc_setup_interface()
1121 div = DIV_ROUND_UP((timings->tRC_min / 1000), NFC_CLK_CYCLE); in meson_nfc_setup_interface()
1122 bt_min = (timings->tREA_max + NFC_DEFAULT_DELAY) / div; in meson_nfc_setup_interface()
1123 bt_max = (NFC_DEFAULT_DELAY + timings->tRHOH_min + in meson_nfc_setup_interface()
1124 timings->tRC_min / 2) / div; in meson_nfc_setup_interface()
1126 meson_chip->twb = DIV_ROUND_UP(PSEC_TO_NSEC(timings->tWB_max), in meson_nfc_setup_interface()
1128 meson_chip->tadl = DIV_ROUND_UP(PSEC_TO_NSEC(timings->tADL_min), in meson_nfc_setup_interface()
1130 tbers_clocks = DIV_ROUND_UP_ULL(PSEC_TO_NSEC(timings->tBERS_max), in meson_nfc_setup_interface()
1132 meson_chip->tbers_max = ilog2(tbers_clocks); in meson_nfc_setup_interface()
1134 meson_chip->tbers_max++; in meson_nfc_setup_interface()
1140 return -EINVAL; in meson_nfc_setup_interface()
1142 meson_chip->level1_divider = div; in meson_nfc_setup_interface()
1143 meson_chip->clk_rate = 1000000000 / meson_chip->level1_divider; in meson_nfc_setup_interface()
1144 meson_chip->bus_timing = (bt_min + bt_max) / 2 + 1; in meson_nfc_setup_interface()
1149 static int meson_nand_bch_mode(struct nand_chip *nand) in meson_nand_bch_mode() argument
1151 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nand_bch_mode()
1154 if (nand->ecc.strength > 60 || nand->ecc.strength < 8) in meson_nand_bch_mode()
1155 return -EINVAL; in meson_nand_bch_mode()
1158 if (meson_ecc[i].strength == nand->ecc.strength) { in meson_nand_bch_mode()
1159 meson_chip->bch_mode = meson_ecc[i].bch; in meson_nand_bch_mode()
1164 return -EINVAL; in meson_nand_bch_mode()
1167 static void meson_nand_detach_chip(struct nand_chip *nand) in meson_nand_detach_chip() argument
1169 meson_nfc_free_buffer(nand); in meson_nand_detach_chip()
1172 static int meson_nand_attach_chip(struct nand_chip *nand) in meson_nand_attach_chip() argument
1174 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nand_attach_chip()
1175 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nand_attach_chip()
1176 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nand_attach_chip()
1177 int nsectors = mtd->writesize / 1024; in meson_nand_attach_chip()
1180 if (!mtd->name) { in meson_nand_attach_chip()
1181 mtd->name = devm_kasprintf(nfc->dev, GFP_KERNEL, in meson_nand_attach_chip()
1182 "%s:nand%d", in meson_nand_attach_chip()
1183 dev_name(nfc->dev), in meson_nand_attach_chip()
1184 meson_chip->sels[0]); in meson_nand_attach_chip()
1185 if (!mtd->name) in meson_nand_attach_chip()
1186 return -ENOMEM; in meson_nand_attach_chip()
1189 if (nand->bbt_options & NAND_BBT_USE_FLASH) in meson_nand_attach_chip()
1190 nand->bbt_options |= NAND_BBT_NO_OOB; in meson_nand_attach_chip()
1192 nand->options |= NAND_NO_SUBPAGE_WRITE; in meson_nand_attach_chip()
1194 ret = nand_ecc_choose_conf(nand, nfc->data->ecc_caps, in meson_nand_attach_chip()
1195 mtd->oobsize - 2 * nsectors); in meson_nand_attach_chip()
1197 dev_err(nfc->dev, "failed to ECC init\n"); in meson_nand_attach_chip()
1198 return -EINVAL; in meson_nand_attach_chip()
1203 ret = meson_nand_bch_mode(nand); in meson_nand_attach_chip()
1205 return -EINVAL; in meson_nand_attach_chip()
1207 nand->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST; in meson_nand_attach_chip()
1208 nand->ecc.write_page_raw = meson_nfc_write_page_raw; in meson_nand_attach_chip()
1209 nand->ecc.write_page = meson_nfc_write_page_hwecc; in meson_nand_attach_chip()
1210 nand->ecc.write_oob_raw = nand_write_oob_std; in meson_nand_attach_chip()
1211 nand->ecc.write_oob = nand_write_oob_std; in meson_nand_attach_chip()
1213 nand->ecc.read_page_raw = meson_nfc_read_page_raw; in meson_nand_attach_chip()
1214 nand->ecc.read_page = meson_nfc_read_page_hwecc; in meson_nand_attach_chip()
1215 nand->ecc.read_oob_raw = meson_nfc_read_oob_raw; in meson_nand_attach_chip()
1216 nand->ecc.read_oob = meson_nfc_read_oob; in meson_nand_attach_chip()
1218 if (nand->options & NAND_BUSWIDTH_16) { in meson_nand_attach_chip()
1219 dev_err(nfc->dev, "16bits bus width not supported"); in meson_nand_attach_chip()
1220 return -EINVAL; in meson_nand_attach_chip()
1222 ret = meson_chip_buffer_init(nand); in meson_nand_attach_chip()
1224 return -ENOMEM; in meson_nand_attach_chip()
1241 struct nand_chip *nand; in meson_nfc_nand_chip_init() local
1249 return -EINVAL; in meson_nfc_nand_chip_init()
1255 return -ENOMEM; in meson_nfc_nand_chip_init()
1257 meson_chip->nsels = nsels; in meson_nfc_nand_chip_init()
1267 if (test_and_set_bit(tmp, &nfc->assigned_cs)) { in meson_nfc_nand_chip_init()
1269 return -EINVAL; in meson_nfc_nand_chip_init()
1273 nand = &meson_chip->nand; in meson_nfc_nand_chip_init()
1274 nand->controller = &nfc->controller; in meson_nfc_nand_chip_init()
1275 nand->controller->ops = &meson_nand_controller_ops; in meson_nfc_nand_chip_init()
1276 nand_set_flash_node(nand, np); in meson_nfc_nand_chip_init()
1277 nand_set_controller_data(nand, nfc); in meson_nfc_nand_chip_init()
1279 nand->options |= NAND_USES_DMA; in meson_nfc_nand_chip_init()
1280 mtd = nand_to_mtd(nand); in meson_nfc_nand_chip_init()
1281 mtd->owner = THIS_MODULE; in meson_nfc_nand_chip_init()
1282 mtd->dev.parent = dev; in meson_nfc_nand_chip_init()
1284 ret = nand_scan(nand, nsels); in meson_nfc_nand_chip_init()
1291 nand_cleanup(nand); in meson_nfc_nand_chip_init()
1295 list_add_tail(&meson_chip->node, &nfc->chips); in meson_nfc_nand_chip_init()
1305 while (!list_empty(&nfc->chips)) { in meson_nfc_nand_chip_cleanup()
1306 meson_chip = list_first_entry(&nfc->chips, in meson_nfc_nand_chip_cleanup()
1308 mtd = nand_to_mtd(&meson_chip->nand); in meson_nfc_nand_chip_cleanup()
1311 nand_cleanup(&meson_chip->nand); in meson_nfc_nand_chip_cleanup()
1312 list_del(&meson_chip->node); in meson_nfc_nand_chip_cleanup()
1319 struct device_node *np = dev->of_node; in meson_nfc_nand_chips_init()
1340 cfg = readl(nfc->reg_base + NFC_REG_CFG); in meson_nfc_irq()
1345 writel(cfg, nfc->reg_base + NFC_REG_CFG); in meson_nfc_irq()
1347 complete(&nfc->completion); in meson_nfc_irq()
1361 .compatible = "amlogic,meson-gxl-nfc",
1364 .compatible = "amlogic,meson-axg-nfc",
1373 struct device *dev = &pdev->dev; in meson_nfc_probe()
1379 return -ENOMEM; in meson_nfc_probe()
1381 nfc->data = of_device_get_match_data(&pdev->dev); in meson_nfc_probe()
1382 if (!nfc->data) in meson_nfc_probe()
1383 return -ENODEV; in meson_nfc_probe()
1385 nand_controller_init(&nfc->controller); in meson_nfc_probe()
1386 INIT_LIST_HEAD(&nfc->chips); in meson_nfc_probe()
1387 init_completion(&nfc->completion); in meson_nfc_probe()
1389 nfc->dev = dev; in meson_nfc_probe()
1391 nfc->reg_base = devm_platform_ioremap_resource_byname(pdev, "nfc"); in meson_nfc_probe()
1392 if (IS_ERR(nfc->reg_base)) in meson_nfc_probe()
1393 return PTR_ERR(nfc->reg_base); in meson_nfc_probe()
1395 nfc->reg_clk = devm_platform_ioremap_resource_byname(pdev, "emmc"); in meson_nfc_probe()
1396 if (IS_ERR(nfc->reg_clk)) in meson_nfc_probe()
1397 return PTR_ERR(nfc->reg_clk); in meson_nfc_probe()
1401 return -EINVAL; in meson_nfc_probe()
1405 dev_err(dev, "failed to initialize NAND clock\n"); in meson_nfc_probe()
1409 writel(0, nfc->reg_base + NFC_REG_CFG); in meson_nfc_probe()
1413 ret = -EINVAL; in meson_nfc_probe()
1427 dev_err(dev, "failed to init NAND chips\n"); in meson_nfc_probe()
1452 .name = "meson-nand",
1460 MODULE_DESCRIPTION("Amlogic's Meson NAND Flash Controller driver");