18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2016 Sigma Designs
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/io.h>
78c2ecf20Sopenharmony_ci#include <linux/of.h>
88c2ecf20Sopenharmony_ci#include <linux/clk.h>
98c2ecf20Sopenharmony_ci#include <linux/iopoll.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/mtd/rawnand.h>
128c2ecf20Sopenharmony_ci#include <linux/dmaengine.h>
138c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
148c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/* Offsets relative to chip->base */
178c2ecf20Sopenharmony_ci#define PBUS_CMD	0
188c2ecf20Sopenharmony_ci#define PBUS_ADDR	4
198c2ecf20Sopenharmony_ci#define PBUS_DATA	8
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/* Offsets relative to reg_base */
228c2ecf20Sopenharmony_ci#define NFC_STATUS	0x00
238c2ecf20Sopenharmony_ci#define NFC_FLASH_CMD	0x04
248c2ecf20Sopenharmony_ci#define NFC_DEVICE_CFG	0x08
258c2ecf20Sopenharmony_ci#define NFC_TIMING1	0x0c
268c2ecf20Sopenharmony_ci#define NFC_TIMING2	0x10
278c2ecf20Sopenharmony_ci#define NFC_XFER_CFG	0x14
288c2ecf20Sopenharmony_ci#define NFC_PKT_0_CFG	0x18
298c2ecf20Sopenharmony_ci#define NFC_PKT_N_CFG	0x1c
308c2ecf20Sopenharmony_ci#define NFC_BB_CFG	0x20
318c2ecf20Sopenharmony_ci#define NFC_ADDR_PAGE	0x24
328c2ecf20Sopenharmony_ci#define NFC_ADDR_OFFSET	0x28
338c2ecf20Sopenharmony_ci#define NFC_XFER_STATUS	0x2c
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* NFC_STATUS values */
368c2ecf20Sopenharmony_ci#define CMD_READY	BIT(31)
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/* NFC_FLASH_CMD values */
398c2ecf20Sopenharmony_ci#define NFC_READ	1
408c2ecf20Sopenharmony_ci#define NFC_WRITE	2
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* NFC_XFER_STATUS values */
438c2ecf20Sopenharmony_ci#define PAGE_IS_EMPTY	BIT(16)
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/* Offsets relative to mem_base */
468c2ecf20Sopenharmony_ci#define METADATA	0x000
478c2ecf20Sopenharmony_ci#define ERROR_REPORT	0x1c0
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/*
508c2ecf20Sopenharmony_ci * Error reports are split in two bytes:
518c2ecf20Sopenharmony_ci * byte 0 for the first packet in the page (PKT_0)
528c2ecf20Sopenharmony_ci * byte 1 for other packets in the page (PKT_N, for N > 0)
538c2ecf20Sopenharmony_ci * ERR_COUNT_PKT_N is the max error count over all but the first packet.
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_ci#define ERR_COUNT_PKT_0(v)	(((v) >> 0) & 0x3f)
568c2ecf20Sopenharmony_ci#define ERR_COUNT_PKT_N(v)	(((v) >> 8) & 0x3f)
578c2ecf20Sopenharmony_ci#define DECODE_FAIL_PKT_0(v)	(((v) & BIT(7)) == 0)
588c2ecf20Sopenharmony_ci#define DECODE_FAIL_PKT_N(v)	(((v) & BIT(15)) == 0)
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/* Offsets relative to pbus_base */
618c2ecf20Sopenharmony_ci#define PBUS_CS_CTRL	0x83c
628c2ecf20Sopenharmony_ci#define PBUS_PAD_MODE	0x8f0
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/* PBUS_CS_CTRL values */
658c2ecf20Sopenharmony_ci#define PBUS_IORDY	BIT(31)
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/*
688c2ecf20Sopenharmony_ci * PBUS_PAD_MODE values
698c2ecf20Sopenharmony_ci * In raw mode, the driver communicates directly with the NAND chips.
708c2ecf20Sopenharmony_ci * In NFC mode, the NAND Flash controller manages the communication.
718c2ecf20Sopenharmony_ci * We use NFC mode for read and write; raw mode for everything else.
728c2ecf20Sopenharmony_ci */
738c2ecf20Sopenharmony_ci#define MODE_RAW	0
748c2ecf20Sopenharmony_ci#define MODE_NFC	BIT(31)
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#define METADATA_SIZE	4
778c2ecf20Sopenharmony_ci#define BBM_SIZE	6
788c2ecf20Sopenharmony_ci#define FIELD_ORDER	15
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#define MAX_CS		4
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistruct tango_nfc {
838c2ecf20Sopenharmony_ci	struct nand_controller hw;
848c2ecf20Sopenharmony_ci	void __iomem *reg_base;
858c2ecf20Sopenharmony_ci	void __iomem *mem_base;
868c2ecf20Sopenharmony_ci	void __iomem *pbus_base;
878c2ecf20Sopenharmony_ci	struct tango_chip *chips[MAX_CS];
888c2ecf20Sopenharmony_ci	struct dma_chan *chan;
898c2ecf20Sopenharmony_ci	int freq_kHz;
908c2ecf20Sopenharmony_ci};
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci#define to_tango_nfc(ptr) container_of(ptr, struct tango_nfc, hw)
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistruct tango_chip {
958c2ecf20Sopenharmony_ci	struct nand_chip nand_chip;
968c2ecf20Sopenharmony_ci	void __iomem *base;
978c2ecf20Sopenharmony_ci	u32 timing1;
988c2ecf20Sopenharmony_ci	u32 timing2;
998c2ecf20Sopenharmony_ci	u32 xfer_cfg;
1008c2ecf20Sopenharmony_ci	u32 pkt_0_cfg;
1018c2ecf20Sopenharmony_ci	u32 pkt_n_cfg;
1028c2ecf20Sopenharmony_ci	u32 bb_cfg;
1038c2ecf20Sopenharmony_ci};
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci#define to_tango_chip(ptr) container_of(ptr, struct tango_chip, nand_chip)
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci#define XFER_CFG(cs, page_count, steps, metadata_size)	\
1088c2ecf20Sopenharmony_ci	((cs) << 24 | (page_count) << 16 | (steps) << 8 | (metadata_size))
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci#define PKT_CFG(size, strength) ((size) << 16 | (strength))
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci#define BB_CFG(bb_offset, bb_size) ((bb_offset) << 16 | (bb_size))
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#define TIMING(t0, t1, t2, t3) ((t0) << 24 | (t1) << 16 | (t2) << 8 | (t3))
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic void tango_select_target(struct nand_chip *chip, unsigned int cs)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct tango_nfc *nfc = to_tango_nfc(chip->controller);
1198c2ecf20Sopenharmony_ci	struct tango_chip *tchip = to_tango_chip(chip);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	writel_relaxed(tchip->timing1, nfc->reg_base + NFC_TIMING1);
1228c2ecf20Sopenharmony_ci	writel_relaxed(tchip->timing2, nfc->reg_base + NFC_TIMING2);
1238c2ecf20Sopenharmony_ci	writel_relaxed(tchip->xfer_cfg, nfc->reg_base + NFC_XFER_CFG);
1248c2ecf20Sopenharmony_ci	writel_relaxed(tchip->pkt_0_cfg, nfc->reg_base + NFC_PKT_0_CFG);
1258c2ecf20Sopenharmony_ci	writel_relaxed(tchip->pkt_n_cfg, nfc->reg_base + NFC_PKT_N_CFG);
1268c2ecf20Sopenharmony_ci	writel_relaxed(tchip->bb_cfg, nfc->reg_base + NFC_BB_CFG);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic int tango_waitrdy(struct nand_chip *chip, unsigned int timeout_ms)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	struct tango_nfc *nfc = to_tango_nfc(chip->controller);
1328c2ecf20Sopenharmony_ci	u32 status;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return readl_relaxed_poll_timeout(nfc->pbus_base + PBUS_CS_CTRL,
1358c2ecf20Sopenharmony_ci					  status, status & PBUS_IORDY, 20,
1368c2ecf20Sopenharmony_ci					  timeout_ms);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic int tango_exec_instr(struct nand_chip *chip,
1408c2ecf20Sopenharmony_ci			    const struct nand_op_instr *instr)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	struct tango_chip *tchip = to_tango_chip(chip);
1438c2ecf20Sopenharmony_ci	unsigned int i;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	switch (instr->type) {
1468c2ecf20Sopenharmony_ci	case NAND_OP_CMD_INSTR:
1478c2ecf20Sopenharmony_ci		writeb_relaxed(instr->ctx.cmd.opcode, tchip->base + PBUS_CMD);
1488c2ecf20Sopenharmony_ci		return 0;
1498c2ecf20Sopenharmony_ci	case NAND_OP_ADDR_INSTR:
1508c2ecf20Sopenharmony_ci		for (i = 0; i < instr->ctx.addr.naddrs; i++)
1518c2ecf20Sopenharmony_ci			writeb_relaxed(instr->ctx.addr.addrs[i],
1528c2ecf20Sopenharmony_ci				       tchip->base + PBUS_ADDR);
1538c2ecf20Sopenharmony_ci		return 0;
1548c2ecf20Sopenharmony_ci	case NAND_OP_DATA_IN_INSTR:
1558c2ecf20Sopenharmony_ci		ioread8_rep(tchip->base + PBUS_DATA, instr->ctx.data.buf.in,
1568c2ecf20Sopenharmony_ci			    instr->ctx.data.len);
1578c2ecf20Sopenharmony_ci		return 0;
1588c2ecf20Sopenharmony_ci	case NAND_OP_DATA_OUT_INSTR:
1598c2ecf20Sopenharmony_ci		iowrite8_rep(tchip->base + PBUS_DATA, instr->ctx.data.buf.out,
1608c2ecf20Sopenharmony_ci			     instr->ctx.data.len);
1618c2ecf20Sopenharmony_ci		return 0;
1628c2ecf20Sopenharmony_ci	case NAND_OP_WAITRDY_INSTR:
1638c2ecf20Sopenharmony_ci		return tango_waitrdy(chip,
1648c2ecf20Sopenharmony_ci				     instr->ctx.waitrdy.timeout_ms);
1658c2ecf20Sopenharmony_ci	default:
1668c2ecf20Sopenharmony_ci		break;
1678c2ecf20Sopenharmony_ci	}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	return -EINVAL;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic int tango_exec_op(struct nand_chip *chip,
1738c2ecf20Sopenharmony_ci			 const struct nand_operation *op,
1748c2ecf20Sopenharmony_ci			 bool check_only)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	unsigned int i;
1778c2ecf20Sopenharmony_ci	int ret = 0;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	if (check_only)
1808c2ecf20Sopenharmony_ci		return 0;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	tango_select_target(chip, op->cs);
1838c2ecf20Sopenharmony_ci	for (i = 0; i < op->ninstrs; i++) {
1848c2ecf20Sopenharmony_ci		ret = tango_exec_instr(chip, &op->instrs[i]);
1858c2ecf20Sopenharmony_ci		if (ret)
1868c2ecf20Sopenharmony_ci			break;
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	return ret;
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/*
1938c2ecf20Sopenharmony_ci * The controller does not check for bitflips in erased pages,
1948c2ecf20Sopenharmony_ci * therefore software must check instead.
1958c2ecf20Sopenharmony_ci */
1968c2ecf20Sopenharmony_cistatic int check_erased_page(struct nand_chip *chip, u8 *buf)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
1998c2ecf20Sopenharmony_ci	u8 *meta = chip->oob_poi + BBM_SIZE;
2008c2ecf20Sopenharmony_ci	u8 *ecc = chip->oob_poi + BBM_SIZE + METADATA_SIZE;
2018c2ecf20Sopenharmony_ci	const int ecc_size = chip->ecc.bytes;
2028c2ecf20Sopenharmony_ci	const int pkt_size = chip->ecc.size;
2038c2ecf20Sopenharmony_ci	int i, res, meta_len, bitflips = 0;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	for (i = 0; i < chip->ecc.steps; ++i) {
2068c2ecf20Sopenharmony_ci		meta_len = i ? 0 : METADATA_SIZE;
2078c2ecf20Sopenharmony_ci		res = nand_check_erased_ecc_chunk(buf, pkt_size, ecc, ecc_size,
2088c2ecf20Sopenharmony_ci						  meta, meta_len,
2098c2ecf20Sopenharmony_ci						  chip->ecc.strength);
2108c2ecf20Sopenharmony_ci		if (res < 0)
2118c2ecf20Sopenharmony_ci			mtd->ecc_stats.failed++;
2128c2ecf20Sopenharmony_ci		else
2138c2ecf20Sopenharmony_ci			mtd->ecc_stats.corrected += res;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci		bitflips = max(res, bitflips);
2168c2ecf20Sopenharmony_ci		buf += pkt_size;
2178c2ecf20Sopenharmony_ci		ecc += ecc_size;
2188c2ecf20Sopenharmony_ci	}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	return bitflips;
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic int decode_error_report(struct nand_chip *chip)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	u32 status, res;
2268c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
2278c2ecf20Sopenharmony_ci	struct tango_nfc *nfc = to_tango_nfc(chip->controller);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	status = readl_relaxed(nfc->reg_base + NFC_XFER_STATUS);
2308c2ecf20Sopenharmony_ci	if (status & PAGE_IS_EMPTY)
2318c2ecf20Sopenharmony_ci		return 0;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	res = readl_relaxed(nfc->mem_base + ERROR_REPORT);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	if (DECODE_FAIL_PKT_0(res) || DECODE_FAIL_PKT_N(res))
2368c2ecf20Sopenharmony_ci		return -EBADMSG;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	/* ERR_COUNT_PKT_N is max, not sum, but that's all we have */
2398c2ecf20Sopenharmony_ci	mtd->ecc_stats.corrected +=
2408c2ecf20Sopenharmony_ci		ERR_COUNT_PKT_0(res) + ERR_COUNT_PKT_N(res);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	return max(ERR_COUNT_PKT_0(res), ERR_COUNT_PKT_N(res));
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic void tango_dma_callback(void *arg)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	complete(arg);
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cistatic int do_dma(struct tango_nfc *nfc, enum dma_data_direction dir, int cmd,
2518c2ecf20Sopenharmony_ci		  const void *buf, int len, int page)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	void __iomem *addr = nfc->reg_base + NFC_STATUS;
2548c2ecf20Sopenharmony_ci	struct dma_chan *chan = nfc->chan;
2558c2ecf20Sopenharmony_ci	struct dma_async_tx_descriptor *desc;
2568c2ecf20Sopenharmony_ci	enum dma_transfer_direction tdir;
2578c2ecf20Sopenharmony_ci	struct scatterlist sg;
2588c2ecf20Sopenharmony_ci	struct completion tx_done;
2598c2ecf20Sopenharmony_ci	int err = -EIO;
2608c2ecf20Sopenharmony_ci	u32 res, val;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	sg_init_one(&sg, buf, len);
2638c2ecf20Sopenharmony_ci	if (dma_map_sg(chan->device->dev, &sg, 1, dir) != 1)
2648c2ecf20Sopenharmony_ci		return -EIO;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	tdir = dir == DMA_TO_DEVICE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
2678c2ecf20Sopenharmony_ci	desc = dmaengine_prep_slave_sg(chan, &sg, 1, tdir, DMA_PREP_INTERRUPT);
2688c2ecf20Sopenharmony_ci	if (!desc)
2698c2ecf20Sopenharmony_ci		goto dma_unmap;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	desc->callback = tango_dma_callback;
2728c2ecf20Sopenharmony_ci	desc->callback_param = &tx_done;
2738c2ecf20Sopenharmony_ci	init_completion(&tx_done);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	writel_relaxed(MODE_NFC, nfc->pbus_base + PBUS_PAD_MODE);
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	writel_relaxed(page, nfc->reg_base + NFC_ADDR_PAGE);
2788c2ecf20Sopenharmony_ci	writel_relaxed(0, nfc->reg_base + NFC_ADDR_OFFSET);
2798c2ecf20Sopenharmony_ci	writel_relaxed(cmd, nfc->reg_base + NFC_FLASH_CMD);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	dmaengine_submit(desc);
2828c2ecf20Sopenharmony_ci	dma_async_issue_pending(chan);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	res = wait_for_completion_timeout(&tx_done, HZ);
2858c2ecf20Sopenharmony_ci	if (res > 0)
2868c2ecf20Sopenharmony_ci		err = readl_poll_timeout(addr, val, val & CMD_READY, 0, 1000);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	writel_relaxed(MODE_RAW, nfc->pbus_base + PBUS_PAD_MODE);
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_cidma_unmap:
2918c2ecf20Sopenharmony_ci	dma_unmap_sg(chan->device->dev, &sg, 1, dir);
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	return err;
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic int tango_read_page(struct nand_chip *chip, u8 *buf,
2978c2ecf20Sopenharmony_ci			   int oob_required, int page)
2988c2ecf20Sopenharmony_ci{
2998c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
3008c2ecf20Sopenharmony_ci	struct tango_nfc *nfc = to_tango_nfc(chip->controller);
3018c2ecf20Sopenharmony_ci	int err, res, len = mtd->writesize;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	tango_select_target(chip, chip->cur_cs);
3048c2ecf20Sopenharmony_ci	if (oob_required)
3058c2ecf20Sopenharmony_ci		chip->ecc.read_oob(chip, page);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	err = do_dma(nfc, DMA_FROM_DEVICE, NFC_READ, buf, len, page);
3088c2ecf20Sopenharmony_ci	if (err)
3098c2ecf20Sopenharmony_ci		return err;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	res = decode_error_report(chip);
3128c2ecf20Sopenharmony_ci	if (res < 0) {
3138c2ecf20Sopenharmony_ci		chip->ecc.read_oob_raw(chip, page);
3148c2ecf20Sopenharmony_ci		res = check_erased_page(chip, buf);
3158c2ecf20Sopenharmony_ci	}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	return res;
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_cistatic int tango_write_page(struct nand_chip *chip, const u8 *buf,
3218c2ecf20Sopenharmony_ci			    int oob_required, int page)
3228c2ecf20Sopenharmony_ci{
3238c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
3248c2ecf20Sopenharmony_ci	struct tango_nfc *nfc = to_tango_nfc(chip->controller);
3258c2ecf20Sopenharmony_ci	const struct nand_sdr_timings *timings;
3268c2ecf20Sopenharmony_ci	int err, len = mtd->writesize;
3278c2ecf20Sopenharmony_ci	u8 status;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	/* Calling tango_write_oob() would send PAGEPROG twice */
3308c2ecf20Sopenharmony_ci	if (oob_required)
3318c2ecf20Sopenharmony_ci		return -ENOTSUPP;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	tango_select_target(chip, chip->cur_cs);
3348c2ecf20Sopenharmony_ci	writel_relaxed(0xffffffff, nfc->mem_base + METADATA);
3358c2ecf20Sopenharmony_ci	err = do_dma(nfc, DMA_TO_DEVICE, NFC_WRITE, buf, len, page);
3368c2ecf20Sopenharmony_ci	if (err)
3378c2ecf20Sopenharmony_ci		return err;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	timings = nand_get_sdr_timings(nand_get_interface_config(chip));
3408c2ecf20Sopenharmony_ci	err = tango_waitrdy(chip, PSEC_TO_MSEC(timings->tR_max));
3418c2ecf20Sopenharmony_ci	if (err)
3428c2ecf20Sopenharmony_ci		return err;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	err = nand_status_op(chip, &status);
3458c2ecf20Sopenharmony_ci	if (err)
3468c2ecf20Sopenharmony_ci		return err;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	return (status & NAND_STATUS_FAIL) ? -EIO : 0;
3498c2ecf20Sopenharmony_ci}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cistatic void aux_read(struct nand_chip *chip, u8 **buf, int len, int *pos)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	*pos += len;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	if (!*buf) {
3568c2ecf20Sopenharmony_ci		/* skip over "len" bytes */
3578c2ecf20Sopenharmony_ci		nand_change_read_column_op(chip, *pos, NULL, 0, false);
3588c2ecf20Sopenharmony_ci	} else {
3598c2ecf20Sopenharmony_ci		struct tango_chip *tchip = to_tango_chip(chip);
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci		ioread8_rep(tchip->base + PBUS_DATA, *buf, len);
3628c2ecf20Sopenharmony_ci		*buf += len;
3638c2ecf20Sopenharmony_ci	}
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic void aux_write(struct nand_chip *chip, const u8 **buf, int len, int *pos)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	*pos += len;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	if (!*buf) {
3718c2ecf20Sopenharmony_ci		/* skip over "len" bytes */
3728c2ecf20Sopenharmony_ci		nand_change_write_column_op(chip, *pos, NULL, 0, false);
3738c2ecf20Sopenharmony_ci	} else {
3748c2ecf20Sopenharmony_ci		struct tango_chip *tchip = to_tango_chip(chip);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci		iowrite8_rep(tchip->base + PBUS_DATA, *buf, len);
3778c2ecf20Sopenharmony_ci		*buf += len;
3788c2ecf20Sopenharmony_ci	}
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci/*
3828c2ecf20Sopenharmony_ci * Physical page layout (not drawn to scale)
3838c2ecf20Sopenharmony_ci *
3848c2ecf20Sopenharmony_ci * NB: Bad Block Marker area splits PKT_N in two (N1, N2).
3858c2ecf20Sopenharmony_ci *
3868c2ecf20Sopenharmony_ci * +---+-----------------+-------+-----+-----------+-----+----+-------+
3878c2ecf20Sopenharmony_ci * | M |      PKT_0      | ECC_0 | ... |     N1    | BBM | N2 | ECC_N |
3888c2ecf20Sopenharmony_ci * +---+-----------------+-------+-----+-----------+-----+----+-------+
3898c2ecf20Sopenharmony_ci *
3908c2ecf20Sopenharmony_ci * Logical page layout:
3918c2ecf20Sopenharmony_ci *
3928c2ecf20Sopenharmony_ci *       +-----+---+-------+-----+-------+
3938c2ecf20Sopenharmony_ci * oob = | BBM | M | ECC_0 | ... | ECC_N |
3948c2ecf20Sopenharmony_ci *       +-----+---+-------+-----+-------+
3958c2ecf20Sopenharmony_ci *
3968c2ecf20Sopenharmony_ci *       +-----------------+-----+-----------------+
3978c2ecf20Sopenharmony_ci * buf = |      PKT_0      | ... |      PKT_N      |
3988c2ecf20Sopenharmony_ci *       +-----------------+-----+-----------------+
3998c2ecf20Sopenharmony_ci */
4008c2ecf20Sopenharmony_cistatic void raw_read(struct nand_chip *chip, u8 *buf, u8 *oob)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
4038c2ecf20Sopenharmony_ci	u8 *oob_orig = oob;
4048c2ecf20Sopenharmony_ci	const int page_size = mtd->writesize;
4058c2ecf20Sopenharmony_ci	const int ecc_size = chip->ecc.bytes;
4068c2ecf20Sopenharmony_ci	const int pkt_size = chip->ecc.size;
4078c2ecf20Sopenharmony_ci	int pos = 0; /* position within physical page */
4088c2ecf20Sopenharmony_ci	int rem = page_size; /* bytes remaining until BBM area */
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	if (oob)
4118c2ecf20Sopenharmony_ci		oob += BBM_SIZE;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	aux_read(chip, &oob, METADATA_SIZE, &pos);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	while (rem > pkt_size) {
4168c2ecf20Sopenharmony_ci		aux_read(chip, &buf, pkt_size, &pos);
4178c2ecf20Sopenharmony_ci		aux_read(chip, &oob, ecc_size, &pos);
4188c2ecf20Sopenharmony_ci		rem = page_size - pos;
4198c2ecf20Sopenharmony_ci	}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	aux_read(chip, &buf, rem, &pos);
4228c2ecf20Sopenharmony_ci	aux_read(chip, &oob_orig, BBM_SIZE, &pos);
4238c2ecf20Sopenharmony_ci	aux_read(chip, &buf, pkt_size - rem, &pos);
4248c2ecf20Sopenharmony_ci	aux_read(chip, &oob, ecc_size, &pos);
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_cistatic void raw_write(struct nand_chip *chip, const u8 *buf, const u8 *oob)
4288c2ecf20Sopenharmony_ci{
4298c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
4308c2ecf20Sopenharmony_ci	const u8 *oob_orig = oob;
4318c2ecf20Sopenharmony_ci	const int page_size = mtd->writesize;
4328c2ecf20Sopenharmony_ci	const int ecc_size = chip->ecc.bytes;
4338c2ecf20Sopenharmony_ci	const int pkt_size = chip->ecc.size;
4348c2ecf20Sopenharmony_ci	int pos = 0; /* position within physical page */
4358c2ecf20Sopenharmony_ci	int rem = page_size; /* bytes remaining until BBM area */
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	if (oob)
4388c2ecf20Sopenharmony_ci		oob += BBM_SIZE;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	aux_write(chip, &oob, METADATA_SIZE, &pos);
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	while (rem > pkt_size) {
4438c2ecf20Sopenharmony_ci		aux_write(chip, &buf, pkt_size, &pos);
4448c2ecf20Sopenharmony_ci		aux_write(chip, &oob, ecc_size, &pos);
4458c2ecf20Sopenharmony_ci		rem = page_size - pos;
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	aux_write(chip, &buf, rem, &pos);
4498c2ecf20Sopenharmony_ci	aux_write(chip, &oob_orig, BBM_SIZE, &pos);
4508c2ecf20Sopenharmony_ci	aux_write(chip, &buf, pkt_size - rem, &pos);
4518c2ecf20Sopenharmony_ci	aux_write(chip, &oob, ecc_size, &pos);
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_cistatic int tango_read_page_raw(struct nand_chip *chip, u8 *buf,
4558c2ecf20Sopenharmony_ci			       int oob_required, int page)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	tango_select_target(chip, chip->cur_cs);
4588c2ecf20Sopenharmony_ci	nand_read_page_op(chip, page, 0, NULL, 0);
4598c2ecf20Sopenharmony_ci	raw_read(chip, buf, chip->oob_poi);
4608c2ecf20Sopenharmony_ci	return 0;
4618c2ecf20Sopenharmony_ci}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_cistatic int tango_write_page_raw(struct nand_chip *chip, const u8 *buf,
4648c2ecf20Sopenharmony_ci				int oob_required, int page)
4658c2ecf20Sopenharmony_ci{
4668c2ecf20Sopenharmony_ci	tango_select_target(chip, chip->cur_cs);
4678c2ecf20Sopenharmony_ci	nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4688c2ecf20Sopenharmony_ci	raw_write(chip, buf, chip->oob_poi);
4698c2ecf20Sopenharmony_ci	return nand_prog_page_end_op(chip);
4708c2ecf20Sopenharmony_ci}
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_cistatic int tango_read_oob(struct nand_chip *chip, int page)
4738c2ecf20Sopenharmony_ci{
4748c2ecf20Sopenharmony_ci	tango_select_target(chip, chip->cur_cs);
4758c2ecf20Sopenharmony_ci	nand_read_page_op(chip, page, 0, NULL, 0);
4768c2ecf20Sopenharmony_ci	raw_read(chip, NULL, chip->oob_poi);
4778c2ecf20Sopenharmony_ci	return 0;
4788c2ecf20Sopenharmony_ci}
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_cistatic int tango_write_oob(struct nand_chip *chip, int page)
4818c2ecf20Sopenharmony_ci{
4828c2ecf20Sopenharmony_ci	tango_select_target(chip, chip->cur_cs);
4838c2ecf20Sopenharmony_ci	nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4848c2ecf20Sopenharmony_ci	raw_write(chip, NULL, chip->oob_poi);
4858c2ecf20Sopenharmony_ci	return nand_prog_page_end_op(chip);
4868c2ecf20Sopenharmony_ci}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_cistatic int oob_ecc(struct mtd_info *mtd, int idx, struct mtd_oob_region *res)
4898c2ecf20Sopenharmony_ci{
4908c2ecf20Sopenharmony_ci	struct nand_chip *chip = mtd_to_nand(mtd);
4918c2ecf20Sopenharmony_ci	struct nand_ecc_ctrl *ecc = &chip->ecc;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	if (idx >= ecc->steps)
4948c2ecf20Sopenharmony_ci		return -ERANGE;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	res->offset = BBM_SIZE + METADATA_SIZE + ecc->bytes * idx;
4978c2ecf20Sopenharmony_ci	res->length = ecc->bytes;
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	return 0;
5008c2ecf20Sopenharmony_ci}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_cistatic int oob_free(struct mtd_info *mtd, int idx, struct mtd_oob_region *res)
5038c2ecf20Sopenharmony_ci{
5048c2ecf20Sopenharmony_ci	return -ERANGE; /* no free space in spare area */
5058c2ecf20Sopenharmony_ci}
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_cistatic const struct mtd_ooblayout_ops tango_nand_ooblayout_ops = {
5088c2ecf20Sopenharmony_ci	.ecc	= oob_ecc,
5098c2ecf20Sopenharmony_ci	.free	= oob_free,
5108c2ecf20Sopenharmony_ci};
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_cistatic u32 to_ticks(int kHz, int ps)
5138c2ecf20Sopenharmony_ci{
5148c2ecf20Sopenharmony_ci	return DIV_ROUND_UP_ULL((u64)kHz * ps, NSEC_PER_SEC);
5158c2ecf20Sopenharmony_ci}
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_cistatic int tango_set_timings(struct nand_chip *chip, int csline,
5188c2ecf20Sopenharmony_ci			     const struct nand_interface_config *conf)
5198c2ecf20Sopenharmony_ci{
5208c2ecf20Sopenharmony_ci	const struct nand_sdr_timings *sdr = nand_get_sdr_timings(conf);
5218c2ecf20Sopenharmony_ci	struct tango_nfc *nfc = to_tango_nfc(chip->controller);
5228c2ecf20Sopenharmony_ci	struct tango_chip *tchip = to_tango_chip(chip);
5238c2ecf20Sopenharmony_ci	u32 Trdy, Textw, Twc, Twpw, Tacc, Thold, Trpw, Textr;
5248c2ecf20Sopenharmony_ci	int kHz = nfc->freq_kHz;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	if (IS_ERR(sdr))
5278c2ecf20Sopenharmony_ci		return PTR_ERR(sdr);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	if (csline == NAND_DATA_IFACE_CHECK_ONLY)
5308c2ecf20Sopenharmony_ci		return 0;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	Trdy = to_ticks(kHz, sdr->tCEA_max - sdr->tREA_max);
5338c2ecf20Sopenharmony_ci	Textw = to_ticks(kHz, sdr->tWB_max);
5348c2ecf20Sopenharmony_ci	Twc = to_ticks(kHz, sdr->tWC_min);
5358c2ecf20Sopenharmony_ci	Twpw = to_ticks(kHz, sdr->tWC_min - sdr->tWP_min);
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	Tacc = to_ticks(kHz, sdr->tREA_max);
5388c2ecf20Sopenharmony_ci	Thold = to_ticks(kHz, sdr->tREH_min);
5398c2ecf20Sopenharmony_ci	Trpw = to_ticks(kHz, sdr->tRC_min - sdr->tREH_min);
5408c2ecf20Sopenharmony_ci	Textr = to_ticks(kHz, sdr->tRHZ_max);
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	tchip->timing1 = TIMING(Trdy, Textw, Twc, Twpw);
5438c2ecf20Sopenharmony_ci	tchip->timing2 = TIMING(Tacc, Thold, Trpw, Textr);
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	return 0;
5468c2ecf20Sopenharmony_ci}
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_cistatic int tango_attach_chip(struct nand_chip *chip)
5498c2ecf20Sopenharmony_ci{
5508c2ecf20Sopenharmony_ci	struct nand_ecc_ctrl *ecc = &chip->ecc;
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	ecc->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
5538c2ecf20Sopenharmony_ci	ecc->algo = NAND_ECC_ALGO_BCH;
5548c2ecf20Sopenharmony_ci	ecc->bytes = DIV_ROUND_UP(ecc->strength * FIELD_ORDER, BITS_PER_BYTE);
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	ecc->read_page_raw = tango_read_page_raw;
5578c2ecf20Sopenharmony_ci	ecc->write_page_raw = tango_write_page_raw;
5588c2ecf20Sopenharmony_ci	ecc->read_page = tango_read_page;
5598c2ecf20Sopenharmony_ci	ecc->write_page = tango_write_page;
5608c2ecf20Sopenharmony_ci	ecc->read_oob = tango_read_oob;
5618c2ecf20Sopenharmony_ci	ecc->write_oob = tango_write_oob;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	return 0;
5648c2ecf20Sopenharmony_ci}
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_cistatic const struct nand_controller_ops tango_controller_ops = {
5678c2ecf20Sopenharmony_ci	.attach_chip = tango_attach_chip,
5688c2ecf20Sopenharmony_ci	.setup_interface = tango_set_timings,
5698c2ecf20Sopenharmony_ci	.exec_op = tango_exec_op,
5708c2ecf20Sopenharmony_ci};
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_cistatic int chip_init(struct device *dev, struct device_node *np)
5738c2ecf20Sopenharmony_ci{
5748c2ecf20Sopenharmony_ci	u32 cs;
5758c2ecf20Sopenharmony_ci	int err, res;
5768c2ecf20Sopenharmony_ci	struct mtd_info *mtd;
5778c2ecf20Sopenharmony_ci	struct nand_chip *chip;
5788c2ecf20Sopenharmony_ci	struct tango_chip *tchip;
5798c2ecf20Sopenharmony_ci	struct nand_ecc_ctrl *ecc;
5808c2ecf20Sopenharmony_ci	struct tango_nfc *nfc = dev_get_drvdata(dev);
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	tchip = devm_kzalloc(dev, sizeof(*tchip), GFP_KERNEL);
5838c2ecf20Sopenharmony_ci	if (!tchip)
5848c2ecf20Sopenharmony_ci		return -ENOMEM;
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	res = of_property_count_u32_elems(np, "reg");
5878c2ecf20Sopenharmony_ci	if (res < 0)
5888c2ecf20Sopenharmony_ci		return res;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	if (res != 1)
5918c2ecf20Sopenharmony_ci		return -ENOTSUPP; /* Multi-CS chips are not supported */
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	err = of_property_read_u32_index(np, "reg", 0, &cs);
5948c2ecf20Sopenharmony_ci	if (err)
5958c2ecf20Sopenharmony_ci		return err;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	if (cs >= MAX_CS)
5988c2ecf20Sopenharmony_ci		return -EINVAL;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	chip = &tchip->nand_chip;
6018c2ecf20Sopenharmony_ci	ecc = &chip->ecc;
6028c2ecf20Sopenharmony_ci	mtd = nand_to_mtd(chip);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	chip->options = NAND_USES_DMA |
6058c2ecf20Sopenharmony_ci			NAND_NO_SUBPAGE_WRITE |
6068c2ecf20Sopenharmony_ci			NAND_WAIT_TCCS;
6078c2ecf20Sopenharmony_ci	chip->controller = &nfc->hw;
6088c2ecf20Sopenharmony_ci	tchip->base = nfc->pbus_base + (cs * 256);
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	nand_set_flash_node(chip, np);
6118c2ecf20Sopenharmony_ci	mtd_set_ooblayout(mtd, &tango_nand_ooblayout_ops);
6128c2ecf20Sopenharmony_ci	mtd->dev.parent = dev;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	err = nand_scan(chip, 1);
6158c2ecf20Sopenharmony_ci	if (err)
6168c2ecf20Sopenharmony_ci		return err;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	tchip->xfer_cfg = XFER_CFG(cs, 1, ecc->steps, METADATA_SIZE);
6198c2ecf20Sopenharmony_ci	tchip->pkt_0_cfg = PKT_CFG(ecc->size + METADATA_SIZE, ecc->strength);
6208c2ecf20Sopenharmony_ci	tchip->pkt_n_cfg = PKT_CFG(ecc->size, ecc->strength);
6218c2ecf20Sopenharmony_ci	tchip->bb_cfg = BB_CFG(mtd->writesize, BBM_SIZE);
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	err = mtd_device_register(mtd, NULL, 0);
6248c2ecf20Sopenharmony_ci	if (err) {
6258c2ecf20Sopenharmony_ci		nand_cleanup(chip);
6268c2ecf20Sopenharmony_ci		return err;
6278c2ecf20Sopenharmony_ci	}
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	nfc->chips[cs] = tchip;
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	return 0;
6328c2ecf20Sopenharmony_ci}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_cistatic int tango_nand_remove(struct platform_device *pdev)
6358c2ecf20Sopenharmony_ci{
6368c2ecf20Sopenharmony_ci	struct tango_nfc *nfc = platform_get_drvdata(pdev);
6378c2ecf20Sopenharmony_ci	struct nand_chip *chip;
6388c2ecf20Sopenharmony_ci	int cs, ret;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	dma_release_channel(nfc->chan);
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	for (cs = 0; cs < MAX_CS; ++cs) {
6438c2ecf20Sopenharmony_ci		if (nfc->chips[cs]) {
6448c2ecf20Sopenharmony_ci			chip = &nfc->chips[cs]->nand_chip;
6458c2ecf20Sopenharmony_ci			ret = mtd_device_unregister(nand_to_mtd(chip));
6468c2ecf20Sopenharmony_ci			WARN_ON(ret);
6478c2ecf20Sopenharmony_ci			nand_cleanup(chip);
6488c2ecf20Sopenharmony_ci		}
6498c2ecf20Sopenharmony_ci	}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	return 0;
6528c2ecf20Sopenharmony_ci}
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_cistatic int tango_nand_probe(struct platform_device *pdev)
6558c2ecf20Sopenharmony_ci{
6568c2ecf20Sopenharmony_ci	int err;
6578c2ecf20Sopenharmony_ci	struct clk *clk;
6588c2ecf20Sopenharmony_ci	struct resource *res;
6598c2ecf20Sopenharmony_ci	struct tango_nfc *nfc;
6608c2ecf20Sopenharmony_ci	struct device_node *np;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
6638c2ecf20Sopenharmony_ci	if (!nfc)
6648c2ecf20Sopenharmony_ci		return -ENOMEM;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
6678c2ecf20Sopenharmony_ci	nfc->reg_base = devm_ioremap_resource(&pdev->dev, res);
6688c2ecf20Sopenharmony_ci	if (IS_ERR(nfc->reg_base))
6698c2ecf20Sopenharmony_ci		return PTR_ERR(nfc->reg_base);
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
6728c2ecf20Sopenharmony_ci	nfc->mem_base = devm_ioremap_resource(&pdev->dev, res);
6738c2ecf20Sopenharmony_ci	if (IS_ERR(nfc->mem_base))
6748c2ecf20Sopenharmony_ci		return PTR_ERR(nfc->mem_base);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
6778c2ecf20Sopenharmony_ci	nfc->pbus_base = devm_ioremap_resource(&pdev->dev, res);
6788c2ecf20Sopenharmony_ci	if (IS_ERR(nfc->pbus_base))
6798c2ecf20Sopenharmony_ci		return PTR_ERR(nfc->pbus_base);
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	writel_relaxed(MODE_RAW, nfc->pbus_base + PBUS_PAD_MODE);
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	clk = devm_clk_get(&pdev->dev, NULL);
6848c2ecf20Sopenharmony_ci	if (IS_ERR(clk))
6858c2ecf20Sopenharmony_ci		return PTR_ERR(clk);
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	nfc->chan = dma_request_chan(&pdev->dev, "rxtx");
6888c2ecf20Sopenharmony_ci	if (IS_ERR(nfc->chan))
6898c2ecf20Sopenharmony_ci		return PTR_ERR(nfc->chan);
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, nfc);
6928c2ecf20Sopenharmony_ci	nand_controller_init(&nfc->hw);
6938c2ecf20Sopenharmony_ci	nfc->hw.ops = &tango_controller_ops;
6948c2ecf20Sopenharmony_ci	nfc->freq_kHz = clk_get_rate(clk) / 1000;
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	for_each_child_of_node(pdev->dev.of_node, np) {
6978c2ecf20Sopenharmony_ci		err = chip_init(&pdev->dev, np);
6988c2ecf20Sopenharmony_ci		if (err) {
6998c2ecf20Sopenharmony_ci			tango_nand_remove(pdev);
7008c2ecf20Sopenharmony_ci			of_node_put(np);
7018c2ecf20Sopenharmony_ci			return err;
7028c2ecf20Sopenharmony_ci		}
7038c2ecf20Sopenharmony_ci	}
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	return 0;
7068c2ecf20Sopenharmony_ci}
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_cistatic const struct of_device_id tango_nand_ids[] = {
7098c2ecf20Sopenharmony_ci	{ .compatible = "sigma,smp8758-nand" },
7108c2ecf20Sopenharmony_ci	{ /* sentinel */ }
7118c2ecf20Sopenharmony_ci};
7128c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, tango_nand_ids);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_cistatic struct platform_driver tango_nand_driver = {
7158c2ecf20Sopenharmony_ci	.probe	= tango_nand_probe,
7168c2ecf20Sopenharmony_ci	.remove	= tango_nand_remove,
7178c2ecf20Sopenharmony_ci	.driver	= {
7188c2ecf20Sopenharmony_ci		.name		= "tango-nand",
7198c2ecf20Sopenharmony_ci		.of_match_table	= tango_nand_ids,
7208c2ecf20Sopenharmony_ci	},
7218c2ecf20Sopenharmony_ci};
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_cimodule_platform_driver(tango_nand_driver);
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
7268c2ecf20Sopenharmony_ciMODULE_AUTHOR("Sigma Designs");
7278c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Tango4 NAND Flash controller driver");
728