Lines Matching full:nfc
6 * https://github.com/yuq/sunxi-nfc-mtd
164 * @rb: the Ready/Busy pin ID. -1 means no R/B pin connected to the NFC
262 struct sunxi_nfc *nfc = dev_id; in sunxi_nfc_interrupt() local
263 u32 st = readl(nfc->regs + NFC_REG_ST); in sunxi_nfc_interrupt()
264 u32 ien = readl(nfc->regs + NFC_REG_INT); in sunxi_nfc_interrupt()
270 complete(&nfc->complete); in sunxi_nfc_interrupt()
272 writel(st & NFC_INT_MASK, nfc->regs + NFC_REG_ST); in sunxi_nfc_interrupt()
273 writel(~st & ien & NFC_INT_MASK, nfc->regs + NFC_REG_INT); in sunxi_nfc_interrupt()
278 static int sunxi_nfc_wait_events(struct sunxi_nfc *nfc, u32 events, in sunxi_nfc_wait_events() argument
290 init_completion(&nfc->complete); in sunxi_nfc_wait_events()
292 writel(events, nfc->regs + NFC_REG_INT); in sunxi_nfc_wait_events()
294 ret = wait_for_completion_timeout(&nfc->complete, in sunxi_nfc_wait_events()
301 writel(0, nfc->regs + NFC_REG_INT); in sunxi_nfc_wait_events()
305 ret = readl_poll_timeout(nfc->regs + NFC_REG_ST, status, in sunxi_nfc_wait_events()
310 writel(events & NFC_INT_MASK, nfc->regs + NFC_REG_ST); in sunxi_nfc_wait_events()
313 dev_err(nfc->dev, "wait interrupt timedout\n"); in sunxi_nfc_wait_events()
318 static int sunxi_nfc_wait_cmd_fifo_empty(struct sunxi_nfc *nfc) in sunxi_nfc_wait_cmd_fifo_empty() argument
323 ret = readl_poll_timeout(nfc->regs + NFC_REG_ST, status, in sunxi_nfc_wait_cmd_fifo_empty()
327 dev_err(nfc->dev, "wait for empty cmd FIFO timedout\n"); in sunxi_nfc_wait_cmd_fifo_empty()
332 static int sunxi_nfc_rst(struct sunxi_nfc *nfc) in sunxi_nfc_rst() argument
337 writel(0, nfc->regs + NFC_REG_ECC_CTL); in sunxi_nfc_rst()
338 writel(NFC_RESET, nfc->regs + NFC_REG_CTL); in sunxi_nfc_rst()
340 ret = readl_poll_timeout(nfc->regs + NFC_REG_CTL, ctl, in sunxi_nfc_rst()
344 dev_err(nfc->dev, "wait for NAND controller reset timedout\n"); in sunxi_nfc_rst()
349 static int sunxi_nfc_dma_op_prepare(struct sunxi_nfc *nfc, const void *buf, in sunxi_nfc_dma_op_prepare() argument
365 ret = dma_map_sg(nfc->dev, sg, 1, ddir); in sunxi_nfc_dma_op_prepare()
369 if (!nfc->caps->has_mdma) { in sunxi_nfc_dma_op_prepare()
370 dmad = dmaengine_prep_slave_sg(nfc->dmac, sg, 1, tdir, DMA_CTRL_ACK); in sunxi_nfc_dma_op_prepare()
377 writel(readl(nfc->regs + NFC_REG_CTL) | NFC_RAM_METHOD, in sunxi_nfc_dma_op_prepare()
378 nfc->regs + NFC_REG_CTL); in sunxi_nfc_dma_op_prepare()
379 writel(nchunks, nfc->regs + NFC_REG_SECTOR_NUM); in sunxi_nfc_dma_op_prepare()
380 writel(chunksize, nfc->regs + NFC_REG_CNT); in sunxi_nfc_dma_op_prepare()
382 if (nfc->caps->has_mdma) { in sunxi_nfc_dma_op_prepare()
383 writel(readl(nfc->regs + NFC_REG_CTL) & ~NFC_DMA_TYPE_NORMAL, in sunxi_nfc_dma_op_prepare()
384 nfc->regs + NFC_REG_CTL); in sunxi_nfc_dma_op_prepare()
385 writel(chunksize * nchunks, nfc->regs + NFC_REG_MDMA_CNT); in sunxi_nfc_dma_op_prepare()
386 writel(sg_dma_address(sg), nfc->regs + NFC_REG_MDMA_ADDR); in sunxi_nfc_dma_op_prepare()
398 writel(readl(nfc->regs + NFC_REG_CTL) & ~NFC_RAM_METHOD, in sunxi_nfc_dma_op_prepare()
399 nfc->regs + NFC_REG_CTL); in sunxi_nfc_dma_op_prepare()
402 dma_unmap_sg(nfc->dev, sg, 1, ddir); in sunxi_nfc_dma_op_prepare()
406 static void sunxi_nfc_dma_op_cleanup(struct sunxi_nfc *nfc, in sunxi_nfc_dma_op_cleanup() argument
410 dma_unmap_sg(nfc->dev, sg, 1, ddir); in sunxi_nfc_dma_op_cleanup()
411 writel(readl(nfc->regs + NFC_REG_CTL) & ~NFC_RAM_METHOD, in sunxi_nfc_dma_op_cleanup()
412 nfc->regs + NFC_REG_CTL); in sunxi_nfc_dma_op_cleanup()
419 struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); in sunxi_nfc_select_chip() local
426 ctl = readl(nfc->regs + NFC_REG_CTL) & in sunxi_nfc_select_chip()
434 writel(mtd->writesize, nfc->regs + NFC_REG_SPARE_AREA); in sunxi_nfc_select_chip()
436 if (nfc->clk_rate != sunxi_nand->clk_rate) { in sunxi_nfc_select_chip()
437 clk_set_rate(nfc->mod_clk, sunxi_nand->clk_rate); in sunxi_nfc_select_chip()
438 nfc->clk_rate = sunxi_nand->clk_rate; in sunxi_nfc_select_chip()
441 writel(sunxi_nand->timing_ctl, nfc->regs + NFC_REG_TIMING_CTL); in sunxi_nfc_select_chip()
442 writel(sunxi_nand->timing_cfg, nfc->regs + NFC_REG_TIMING_CFG); in sunxi_nfc_select_chip()
443 writel(ctl, nfc->regs + NFC_REG_CTL); in sunxi_nfc_select_chip()
449 struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); in sunxi_nfc_read_buf() local
460 ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); in sunxi_nfc_read_buf()
464 writel(cnt, nfc->regs + NFC_REG_CNT); in sunxi_nfc_read_buf()
466 writel(tmp, nfc->regs + NFC_REG_CMD); in sunxi_nfc_read_buf()
472 ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, poll, 0); in sunxi_nfc_read_buf()
477 memcpy_fromio(buf + offs, nfc->regs + NFC_RAM0_BASE, in sunxi_nfc_read_buf()
487 struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); in sunxi_nfc_write_buf() local
498 ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); in sunxi_nfc_write_buf()
502 writel(cnt, nfc->regs + NFC_REG_CNT); in sunxi_nfc_write_buf()
503 memcpy_toio(nfc->regs + NFC_RAM0_BASE, buf + offs, cnt); in sunxi_nfc_write_buf()
506 writel(tmp, nfc->regs + NFC_REG_CMD); in sunxi_nfc_write_buf()
512 ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, poll, 0); in sunxi_nfc_write_buf()
625 struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); in sunxi_nfc_randomizer_config() local
626 u32 ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL); in sunxi_nfc_randomizer_config()
632 ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL); in sunxi_nfc_randomizer_config()
634 ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_RANDOM_SEED_MSK; in sunxi_nfc_randomizer_config()
635 writel(ecc_ctl | NFC_RANDOM_SEED(state), nfc->regs + NFC_REG_ECC_CTL); in sunxi_nfc_randomizer_config()
640 struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); in sunxi_nfc_randomizer_enable() local
645 writel(readl(nfc->regs + NFC_REG_ECC_CTL) | NFC_RANDOM_EN, in sunxi_nfc_randomizer_enable()
646 nfc->regs + NFC_REG_ECC_CTL); in sunxi_nfc_randomizer_enable()
651 struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); in sunxi_nfc_randomizer_disable() local
656 writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_RANDOM_EN, in sunxi_nfc_randomizer_disable()
657 nfc->regs + NFC_REG_ECC_CTL); in sunxi_nfc_randomizer_disable()
690 struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); in sunxi_nfc_hw_ecc_enable() local
692 writel(sunxi_nand->ecc.ecc_ctl, nfc->regs + NFC_REG_ECC_CTL); in sunxi_nfc_hw_ecc_enable()
697 struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); in sunxi_nfc_hw_ecc_disable() local
699 writel(0, nfc->regs + NFC_REG_ECC_CTL); in sunxi_nfc_hw_ecc_disable()
718 struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); in sunxi_nfc_hw_ecc_get_prot_oob_bytes() local
720 sunxi_nfc_user_data_to_buf(readl(nfc->regs + NFC_REG_USER_DATA(step)), in sunxi_nfc_hw_ecc_get_prot_oob_bytes()
732 struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); in sunxi_nfc_hw_ecc_set_prot_oob_bytes() local
743 nfc->regs + NFC_REG_USER_DATA(step)); in sunxi_nfc_hw_ecc_set_prot_oob_bytes()
762 struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); in sunxi_nfc_hw_ecc_correct() local
774 if (unlikely(!(readl(nfc->regs + NFC_REG_PAT_ID) & 0x1))) { in sunxi_nfc_hw_ecc_correct()
790 tmp = readl(nfc->regs + NFC_REG_ECC_ERR_CNT(step)); in sunxi_nfc_hw_ecc_correct()
802 struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); in sunxi_nfc_hw_ecc_read_chunk() local
816 ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); in sunxi_nfc_hw_ecc_read_chunk()
822 nfc->regs + NFC_REG_CMD); in sunxi_nfc_hw_ecc_read_chunk()
824 ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, false, 0); in sunxi_nfc_hw_ecc_read_chunk()
832 readl(nfc->regs + NFC_REG_ECC_ST), in sunxi_nfc_hw_ecc_read_chunk()
846 memcpy_fromio(data, nfc->regs + NFC_RAM0_BASE, in sunxi_nfc_hw_ecc_read_chunk()
858 memcpy_fromio(data, nfc->regs + NFC_RAM0_BASE, ecc->size); in sunxi_nfc_hw_ecc_read_chunk()
907 struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); in sunxi_nfc_hw_ecc_read_chunks_dma() local
915 ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); in sunxi_nfc_hw_ecc_read_chunks_dma()
919 ret = sunxi_nfc_dma_op_prepare(nfc, buf, ecc->size, nchunks, in sunxi_nfc_hw_ecc_read_chunks_dma()
929 NAND_CMD_READSTART, nfc->regs + NFC_REG_RCMD_SET); in sunxi_nfc_hw_ecc_read_chunks_dma()
933 if (nfc->caps->has_mdma) in sunxi_nfc_hw_ecc_read_chunks_dma()
936 dma_async_issue_pending(nfc->dmac); in sunxi_nfc_hw_ecc_read_chunks_dma()
939 nfc->regs + NFC_REG_CMD); in sunxi_nfc_hw_ecc_read_chunks_dma()
941 ret = sunxi_nfc_wait_events(nfc, wait, false, 0); in sunxi_nfc_hw_ecc_read_chunks_dma()
942 if (ret && !nfc->caps->has_mdma) in sunxi_nfc_hw_ecc_read_chunks_dma()
943 dmaengine_terminate_all(nfc->dmac); in sunxi_nfc_hw_ecc_read_chunks_dma()
948 sunxi_nfc_dma_op_cleanup(nfc, DMA_FROM_DEVICE, &sg); in sunxi_nfc_hw_ecc_read_chunks_dma()
953 status = readl(nfc->regs + NFC_REG_ECC_ST); in sunxi_nfc_hw_ecc_read_chunks_dma()
1036 struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); in sunxi_nfc_hw_ecc_write_chunk() local
1048 ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); in sunxi_nfc_hw_ecc_write_chunk()
1057 nfc->regs + NFC_REG_CMD); in sunxi_nfc_hw_ecc_write_chunk()
1059 ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, false, 0); in sunxi_nfc_hw_ecc_write_chunk()
1280 struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); in sunxi_nfc_hw_ecc_write_page_dma() local
1288 ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); in sunxi_nfc_hw_ecc_write_page_dma()
1292 ret = sunxi_nfc_dma_op_prepare(nfc, buf, ecc->size, ecc->steps, in sunxi_nfc_hw_ecc_write_page_dma()
1310 nfc->regs + NFC_REG_WCMD_SET); in sunxi_nfc_hw_ecc_write_page_dma()
1314 if (nfc->caps->has_mdma) in sunxi_nfc_hw_ecc_write_page_dma()
1317 dma_async_issue_pending(nfc->dmac); in sunxi_nfc_hw_ecc_write_page_dma()
1321 nfc->regs + NFC_REG_CMD); in sunxi_nfc_hw_ecc_write_page_dma()
1323 ret = sunxi_nfc_wait_events(nfc, wait, false, 0); in sunxi_nfc_hw_ecc_write_page_dma()
1324 if (ret && !nfc->caps->has_mdma) in sunxi_nfc_hw_ecc_write_page_dma()
1325 dmaengine_terminate_all(nfc->dmac); in sunxi_nfc_hw_ecc_write_page_dma()
1330 sunxi_nfc_dma_op_cleanup(nfc, DMA_TO_DEVICE, &sg); in sunxi_nfc_hw_ecc_write_page_dma()
1393 struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); in sunxi_nfc_setup_interface() local
1493 dev_err(nfc->dev, "unsupported tWB\n"); in sunxi_nfc_setup_interface()
1499 dev_err(nfc->dev, "unsupported tADL\n"); in sunxi_nfc_setup_interface()
1505 dev_err(nfc->dev, "unsupported tWHR\n"); in sunxi_nfc_setup_interface()
1512 dev_err(nfc->dev, "unsupported tRHW\n"); in sunxi_nfc_setup_interface()
1538 real_clk_rate = clk_round_rate(nfc->mod_clk, sunxi_nand->clk_rate); in sunxi_nfc_setup_interface()
1540 dev_err(nfc->dev, "Unable to round clk %lu\n", in sunxi_nfc_setup_interface()
1623 struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); in sunxi_nand_hw_ecc_ctrl_init() local
1680 dev_err(nfc->dev, "unsupported strength\n"); in sunxi_nand_hw_ecc_ctrl_init()
1699 if (nfc->dmac || nfc->caps->has_mdma) { in sunxi_nand_hw_ecc_ctrl_init()
1767 struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); in sunxi_nfc_exec_subop() local
1813 memcpy_toio(nfc->regs + NFC_RAM0_BASE, in sunxi_nfc_exec_subop()
1828 ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); in sunxi_nfc_exec_subop()
1833 writel(addrs[0], nfc->regs + NFC_REG_ADDR_LOW); in sunxi_nfc_exec_subop()
1834 writel(addrs[1], nfc->regs + NFC_REG_ADDR_HIGH); in sunxi_nfc_exec_subop()
1839 nfc->regs + in sunxi_nfc_exec_subop()
1844 writel(cnt, nfc->regs + NFC_REG_CNT); in sunxi_nfc_exec_subop()
1846 writel(cmd, nfc->regs + NFC_REG_CMD); in sunxi_nfc_exec_subop()
1848 ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, in sunxi_nfc_exec_subop()
1855 memcpy_fromio(inbuf, nfc->regs + NFC_RAM0_BASE, cnt); in sunxi_nfc_exec_subop()
1920 static void sunxi_nand_chips_cleanup(struct sunxi_nfc *nfc) in sunxi_nand_chips_cleanup() argument
1926 while (!list_empty(&nfc->chips)) { in sunxi_nand_chips_cleanup()
1927 sunxi_nand = list_first_entry(&nfc->chips, in sunxi_nand_chips_cleanup()
1938 static int sunxi_nand_chip_init(struct device *dev, struct sunxi_nfc *nfc, in sunxi_nand_chip_init() argument
1980 if (test_and_set_bit(tmp, &nfc->assigned_cs)) { in sunxi_nand_chip_init()
1996 nand->controller = &nfc->controller; in sunxi_nand_chip_init()
2020 list_add_tail(&sunxi_nand->node, &nfc->chips); in sunxi_nand_chip_init()
2025 static int sunxi_nand_chips_init(struct device *dev, struct sunxi_nfc *nfc) in sunxi_nand_chips_init() argument
2032 ret = sunxi_nand_chip_init(dev, nfc, nand_np); in sunxi_nand_chips_init()
2035 sunxi_nand_chips_cleanup(nfc); in sunxi_nand_chips_init()
2043 static int sunxi_nfc_dma_init(struct sunxi_nfc *nfc, struct resource *r) in sunxi_nfc_dma_init() argument
2047 if (nfc->caps->has_mdma) in sunxi_nfc_dma_init()
2050 nfc->dmac = dma_request_chan(nfc->dev, "rxtx"); in sunxi_nfc_dma_init()
2051 if (IS_ERR(nfc->dmac)) { in sunxi_nfc_dma_init()
2052 ret = PTR_ERR(nfc->dmac); in sunxi_nfc_dma_init()
2057 dev_warn(nfc->dev, "failed to request rxtx DMA channel: %d\n", ret); in sunxi_nfc_dma_init()
2058 nfc->dmac = NULL; in sunxi_nfc_dma_init()
2062 dmac_cfg.src_addr = r->start + nfc->caps->reg_io_data; in sunxi_nfc_dma_init()
2066 dmac_cfg.src_maxburst = nfc->caps->dma_maxburst; in sunxi_nfc_dma_init()
2067 dmac_cfg.dst_maxburst = nfc->caps->dma_maxburst; in sunxi_nfc_dma_init()
2068 dmaengine_slave_config(nfc->dmac, &dmac_cfg); in sunxi_nfc_dma_init()
2077 struct sunxi_nfc *nfc; in sunxi_nfc_probe() local
2081 nfc = devm_kzalloc(dev, sizeof(*nfc), GFP_KERNEL); in sunxi_nfc_probe()
2082 if (!nfc) in sunxi_nfc_probe()
2085 nfc->dev = dev; in sunxi_nfc_probe()
2086 nand_controller_init(&nfc->controller); in sunxi_nfc_probe()
2087 INIT_LIST_HEAD(&nfc->chips); in sunxi_nfc_probe()
2089 nfc->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &r); in sunxi_nfc_probe()
2090 if (IS_ERR(nfc->regs)) in sunxi_nfc_probe()
2091 return PTR_ERR(nfc->regs); in sunxi_nfc_probe()
2097 nfc->ahb_clk = devm_clk_get_enabled(dev, "ahb"); in sunxi_nfc_probe()
2098 if (IS_ERR(nfc->ahb_clk)) { in sunxi_nfc_probe()
2100 return PTR_ERR(nfc->ahb_clk); in sunxi_nfc_probe()
2103 nfc->mod_clk = devm_clk_get_enabled(dev, "mod"); in sunxi_nfc_probe()
2104 if (IS_ERR(nfc->mod_clk)) { in sunxi_nfc_probe()
2106 return PTR_ERR(nfc->mod_clk); in sunxi_nfc_probe()
2109 nfc->reset = devm_reset_control_get_optional_exclusive(dev, "ahb"); in sunxi_nfc_probe()
2110 if (IS_ERR(nfc->reset)) in sunxi_nfc_probe()
2111 return PTR_ERR(nfc->reset); in sunxi_nfc_probe()
2113 ret = reset_control_deassert(nfc->reset); in sunxi_nfc_probe()
2119 nfc->caps = of_device_get_match_data(&pdev->dev); in sunxi_nfc_probe()
2120 if (!nfc->caps) { in sunxi_nfc_probe()
2125 ret = sunxi_nfc_rst(nfc); in sunxi_nfc_probe()
2129 writel(0, nfc->regs + NFC_REG_INT); in sunxi_nfc_probe()
2131 0, "sunxi-nand", nfc); in sunxi_nfc_probe()
2135 ret = sunxi_nfc_dma_init(nfc, r); in sunxi_nfc_probe()
2140 platform_set_drvdata(pdev, nfc); in sunxi_nfc_probe()
2142 ret = sunxi_nand_chips_init(dev, nfc); in sunxi_nfc_probe()
2151 if (nfc->dmac) in sunxi_nfc_probe()
2152 dma_release_channel(nfc->dmac); in sunxi_nfc_probe()
2154 reset_control_assert(nfc->reset); in sunxi_nfc_probe()
2161 struct sunxi_nfc *nfc = platform_get_drvdata(pdev); in sunxi_nfc_remove() local
2163 sunxi_nand_chips_cleanup(nfc); in sunxi_nfc_remove()
2165 reset_control_assert(nfc->reset); in sunxi_nfc_remove()
2167 if (nfc->dmac) in sunxi_nfc_remove()
2168 dma_release_channel(nfc->dmac); in sunxi_nfc_remove()