18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2019 Macronix International Co., Ltd.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author:
68c2ecf20Sopenharmony_ci *	Mason Yang <masonccyang@mxic.com.tw>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/clk.h>
108c2ecf20Sopenharmony_ci#include <linux/io.h>
118c2ecf20Sopenharmony_ci#include <linux/iopoll.h>
128c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/mtd/mtd.h>
158c2ecf20Sopenharmony_ci#include <linux/mtd/rawnand.h>
168c2ecf20Sopenharmony_ci#include <linux/mtd/nand_ecc.h>
178c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include "internals.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define HC_CFG			0x0
228c2ecf20Sopenharmony_ci#define HC_CFG_IF_CFG(x)	((x) << 27)
238c2ecf20Sopenharmony_ci#define HC_CFG_DUAL_SLAVE	BIT(31)
248c2ecf20Sopenharmony_ci#define HC_CFG_INDIVIDUAL	BIT(30)
258c2ecf20Sopenharmony_ci#define HC_CFG_NIO(x)		(((x) / 4) << 27)
268c2ecf20Sopenharmony_ci#define HC_CFG_TYPE(s, t)	((t) << (23 + ((s) * 2)))
278c2ecf20Sopenharmony_ci#define HC_CFG_TYPE_SPI_NOR	0
288c2ecf20Sopenharmony_ci#define HC_CFG_TYPE_SPI_NAND	1
298c2ecf20Sopenharmony_ci#define HC_CFG_TYPE_SPI_RAM	2
308c2ecf20Sopenharmony_ci#define HC_CFG_TYPE_RAW_NAND	3
318c2ecf20Sopenharmony_ci#define HC_CFG_SLV_ACT(x)	((x) << 21)
328c2ecf20Sopenharmony_ci#define HC_CFG_CLK_PH_EN	BIT(20)
338c2ecf20Sopenharmony_ci#define HC_CFG_CLK_POL_INV	BIT(19)
348c2ecf20Sopenharmony_ci#define HC_CFG_BIG_ENDIAN	BIT(18)
358c2ecf20Sopenharmony_ci#define HC_CFG_DATA_PASS	BIT(17)
368c2ecf20Sopenharmony_ci#define HC_CFG_IDLE_SIO_LVL(x)	((x) << 16)
378c2ecf20Sopenharmony_ci#define HC_CFG_MAN_START_EN	BIT(3)
388c2ecf20Sopenharmony_ci#define HC_CFG_MAN_START	BIT(2)
398c2ecf20Sopenharmony_ci#define HC_CFG_MAN_CS_EN	BIT(1)
408c2ecf20Sopenharmony_ci#define HC_CFG_MAN_CS_ASSERT	BIT(0)
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define INT_STS			0x4
438c2ecf20Sopenharmony_ci#define INT_STS_EN		0x8
448c2ecf20Sopenharmony_ci#define INT_SIG_EN		0xc
458c2ecf20Sopenharmony_ci#define INT_STS_ALL		GENMASK(31, 0)
468c2ecf20Sopenharmony_ci#define INT_RDY_PIN		BIT(26)
478c2ecf20Sopenharmony_ci#define INT_RDY_SR		BIT(25)
488c2ecf20Sopenharmony_ci#define INT_LNR_SUSP		BIT(24)
498c2ecf20Sopenharmony_ci#define INT_ECC_ERR		BIT(17)
508c2ecf20Sopenharmony_ci#define INT_CRC_ERR		BIT(16)
518c2ecf20Sopenharmony_ci#define INT_LWR_DIS		BIT(12)
528c2ecf20Sopenharmony_ci#define INT_LRD_DIS		BIT(11)
538c2ecf20Sopenharmony_ci#define INT_SDMA_INT		BIT(10)
548c2ecf20Sopenharmony_ci#define INT_DMA_FINISH		BIT(9)
558c2ecf20Sopenharmony_ci#define INT_RX_NOT_FULL		BIT(3)
568c2ecf20Sopenharmony_ci#define INT_RX_NOT_EMPTY	BIT(2)
578c2ecf20Sopenharmony_ci#define INT_TX_NOT_FULL		BIT(1)
588c2ecf20Sopenharmony_ci#define INT_TX_EMPTY		BIT(0)
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define HC_EN			0x10
618c2ecf20Sopenharmony_ci#define HC_EN_BIT		BIT(0)
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define TXD(x)			(0x14 + ((x) * 4))
648c2ecf20Sopenharmony_ci#define RXD			0x24
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define SS_CTRL(s)		(0x30 + ((s) * 4))
678c2ecf20Sopenharmony_ci#define LRD_CFG			0x44
688c2ecf20Sopenharmony_ci#define LWR_CFG			0x80
698c2ecf20Sopenharmony_ci#define RWW_CFG			0x70
708c2ecf20Sopenharmony_ci#define OP_READ			BIT(23)
718c2ecf20Sopenharmony_ci#define OP_DUMMY_CYC(x)		((x) << 17)
728c2ecf20Sopenharmony_ci#define OP_ADDR_BYTES(x)	((x) << 14)
738c2ecf20Sopenharmony_ci#define OP_CMD_BYTES(x)		(((x) - 1) << 13)
748c2ecf20Sopenharmony_ci#define OP_OCTA_CRC_EN		BIT(12)
758c2ecf20Sopenharmony_ci#define OP_DQS_EN		BIT(11)
768c2ecf20Sopenharmony_ci#define OP_ENHC_EN		BIT(10)
778c2ecf20Sopenharmony_ci#define OP_PREAMBLE_EN		BIT(9)
788c2ecf20Sopenharmony_ci#define OP_DATA_DDR		BIT(8)
798c2ecf20Sopenharmony_ci#define OP_DATA_BUSW(x)		((x) << 6)
808c2ecf20Sopenharmony_ci#define OP_ADDR_DDR		BIT(5)
818c2ecf20Sopenharmony_ci#define OP_ADDR_BUSW(x)		((x) << 3)
828c2ecf20Sopenharmony_ci#define OP_CMD_DDR		BIT(2)
838c2ecf20Sopenharmony_ci#define OP_CMD_BUSW(x)		(x)
848c2ecf20Sopenharmony_ci#define OP_BUSW_1		0
858c2ecf20Sopenharmony_ci#define OP_BUSW_2		1
868c2ecf20Sopenharmony_ci#define OP_BUSW_4		2
878c2ecf20Sopenharmony_ci#define OP_BUSW_8		3
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define OCTA_CRC		0x38
908c2ecf20Sopenharmony_ci#define OCTA_CRC_IN_EN(s)	BIT(3 + ((s) * 16))
918c2ecf20Sopenharmony_ci#define OCTA_CRC_CHUNK(s, x)	((fls((x) / 32)) << (1 + ((s) * 16)))
928c2ecf20Sopenharmony_ci#define OCTA_CRC_OUT_EN(s)	BIT(0 + ((s) * 16))
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci#define ONFI_DIN_CNT(s)		(0x3c + (s))
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#define LRD_CTRL		0x48
978c2ecf20Sopenharmony_ci#define RWW_CTRL		0x74
988c2ecf20Sopenharmony_ci#define LWR_CTRL		0x84
998c2ecf20Sopenharmony_ci#define LMODE_EN		BIT(31)
1008c2ecf20Sopenharmony_ci#define LMODE_SLV_ACT(x)	((x) << 21)
1018c2ecf20Sopenharmony_ci#define LMODE_CMD1(x)		((x) << 8)
1028c2ecf20Sopenharmony_ci#define LMODE_CMD0(x)		(x)
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci#define LRD_ADDR		0x4c
1058c2ecf20Sopenharmony_ci#define LWR_ADDR		0x88
1068c2ecf20Sopenharmony_ci#define LRD_RANGE		0x50
1078c2ecf20Sopenharmony_ci#define LWR_RANGE		0x8c
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#define AXI_SLV_ADDR		0x54
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci#define DMAC_RD_CFG		0x58
1128c2ecf20Sopenharmony_ci#define DMAC_WR_CFG		0x94
1138c2ecf20Sopenharmony_ci#define DMAC_CFG_PERIPH_EN	BIT(31)
1148c2ecf20Sopenharmony_ci#define DMAC_CFG_ALLFLUSH_EN	BIT(30)
1158c2ecf20Sopenharmony_ci#define DMAC_CFG_LASTFLUSH_EN	BIT(29)
1168c2ecf20Sopenharmony_ci#define DMAC_CFG_QE(x)		(((x) + 1) << 16)
1178c2ecf20Sopenharmony_ci#define DMAC_CFG_BURST_LEN(x)	(((x) + 1) << 12)
1188c2ecf20Sopenharmony_ci#define DMAC_CFG_BURST_SZ(x)	((x) << 8)
1198c2ecf20Sopenharmony_ci#define DMAC_CFG_DIR_READ	BIT(1)
1208c2ecf20Sopenharmony_ci#define DMAC_CFG_START		BIT(0)
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci#define DMAC_RD_CNT		0x5c
1238c2ecf20Sopenharmony_ci#define DMAC_WR_CNT		0x98
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci#define SDMA_ADDR		0x60
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci#define DMAM_CFG		0x64
1288c2ecf20Sopenharmony_ci#define DMAM_CFG_START		BIT(31)
1298c2ecf20Sopenharmony_ci#define DMAM_CFG_CONT		BIT(30)
1308c2ecf20Sopenharmony_ci#define DMAM_CFG_SDMA_GAP(x)	(fls((x) / 8192) << 2)
1318c2ecf20Sopenharmony_ci#define DMAM_CFG_DIR_READ	BIT(1)
1328c2ecf20Sopenharmony_ci#define DMAM_CFG_EN		BIT(0)
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#define DMAM_CNT		0x68
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci#define LNR_TIMER_TH		0x6c
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci#define RDM_CFG0		0x78
1398c2ecf20Sopenharmony_ci#define RDM_CFG0_POLY(x)	(x)
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci#define RDM_CFG1		0x7c
1428c2ecf20Sopenharmony_ci#define RDM_CFG1_RDM_EN		BIT(31)
1438c2ecf20Sopenharmony_ci#define RDM_CFG1_SEED(x)	(x)
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci#define LWR_SUSP_CTRL		0x90
1468c2ecf20Sopenharmony_ci#define LWR_SUSP_CTRL_EN	BIT(31)
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci#define DMAS_CTRL		0x9c
1498c2ecf20Sopenharmony_ci#define DMAS_CTRL_EN		BIT(31)
1508c2ecf20Sopenharmony_ci#define DMAS_CTRL_DIR_READ	BIT(30)
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci#define DATA_STROB		0xa0
1538c2ecf20Sopenharmony_ci#define DATA_STROB_EDO_EN	BIT(2)
1548c2ecf20Sopenharmony_ci#define DATA_STROB_INV_POL	BIT(1)
1558c2ecf20Sopenharmony_ci#define DATA_STROB_DELAY_2CYC	BIT(0)
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci#define IDLY_CODE(x)		(0xa4 + ((x) * 4))
1588c2ecf20Sopenharmony_ci#define IDLY_CODE_VAL(x, v)	((v) << (((x) % 4) * 8))
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci#define GPIO			0xc4
1618c2ecf20Sopenharmony_ci#define GPIO_PT(x)		BIT(3 + ((x) * 16))
1628c2ecf20Sopenharmony_ci#define GPIO_RESET(x)		BIT(2 + ((x) * 16))
1638c2ecf20Sopenharmony_ci#define GPIO_HOLDB(x)		BIT(1 + ((x) * 16))
1648c2ecf20Sopenharmony_ci#define GPIO_WPB(x)		BIT((x) * 16)
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci#define HC_VER			0xd0
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci#define HW_TEST(x)		(0xe0 + ((x) * 4))
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci#define MXIC_NFC_MAX_CLK_HZ	50000000
1718c2ecf20Sopenharmony_ci#define IRQ_TIMEOUT		1000
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistruct mxic_nand_ctlr {
1748c2ecf20Sopenharmony_ci	struct clk *ps_clk;
1758c2ecf20Sopenharmony_ci	struct clk *send_clk;
1768c2ecf20Sopenharmony_ci	struct clk *send_dly_clk;
1778c2ecf20Sopenharmony_ci	struct completion complete;
1788c2ecf20Sopenharmony_ci	void __iomem *regs;
1798c2ecf20Sopenharmony_ci	struct nand_controller controller;
1808c2ecf20Sopenharmony_ci	struct device *dev;
1818c2ecf20Sopenharmony_ci	struct nand_chip chip;
1828c2ecf20Sopenharmony_ci};
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic int mxic_nfc_clk_enable(struct mxic_nand_ctlr *nfc)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	int ret;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(nfc->ps_clk);
1898c2ecf20Sopenharmony_ci	if (ret)
1908c2ecf20Sopenharmony_ci		return ret;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(nfc->send_clk);
1938c2ecf20Sopenharmony_ci	if (ret)
1948c2ecf20Sopenharmony_ci		goto err_ps_clk;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(nfc->send_dly_clk);
1978c2ecf20Sopenharmony_ci	if (ret)
1988c2ecf20Sopenharmony_ci		goto err_send_dly_clk;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	return ret;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cierr_send_dly_clk:
2038c2ecf20Sopenharmony_ci	clk_disable_unprepare(nfc->send_clk);
2048c2ecf20Sopenharmony_cierr_ps_clk:
2058c2ecf20Sopenharmony_ci	clk_disable_unprepare(nfc->ps_clk);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	return ret;
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistatic void mxic_nfc_clk_disable(struct mxic_nand_ctlr *nfc)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	clk_disable_unprepare(nfc->send_clk);
2138c2ecf20Sopenharmony_ci	clk_disable_unprepare(nfc->send_dly_clk);
2148c2ecf20Sopenharmony_ci	clk_disable_unprepare(nfc->ps_clk);
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cistatic void mxic_nfc_set_input_delay(struct mxic_nand_ctlr *nfc, u8 idly_code)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	writel(IDLY_CODE_VAL(0, idly_code) |
2208c2ecf20Sopenharmony_ci	       IDLY_CODE_VAL(1, idly_code) |
2218c2ecf20Sopenharmony_ci	       IDLY_CODE_VAL(2, idly_code) |
2228c2ecf20Sopenharmony_ci	       IDLY_CODE_VAL(3, idly_code),
2238c2ecf20Sopenharmony_ci	       nfc->regs + IDLY_CODE(0));
2248c2ecf20Sopenharmony_ci	writel(IDLY_CODE_VAL(4, idly_code) |
2258c2ecf20Sopenharmony_ci	       IDLY_CODE_VAL(5, idly_code) |
2268c2ecf20Sopenharmony_ci	       IDLY_CODE_VAL(6, idly_code) |
2278c2ecf20Sopenharmony_ci	       IDLY_CODE_VAL(7, idly_code),
2288c2ecf20Sopenharmony_ci	       nfc->regs + IDLY_CODE(1));
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cistatic int mxic_nfc_clk_setup(struct mxic_nand_ctlr *nfc, unsigned long freq)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	int ret;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	ret = clk_set_rate(nfc->send_clk, freq);
2368c2ecf20Sopenharmony_ci	if (ret)
2378c2ecf20Sopenharmony_ci		return ret;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	ret = clk_set_rate(nfc->send_dly_clk, freq);
2408c2ecf20Sopenharmony_ci	if (ret)
2418c2ecf20Sopenharmony_ci		return ret;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	/*
2448c2ecf20Sopenharmony_ci	 * A constant delay range from 0x0 ~ 0x1F for input delay,
2458c2ecf20Sopenharmony_ci	 * the unit is 78 ps, the max input delay is 2.418 ns.
2468c2ecf20Sopenharmony_ci	 */
2478c2ecf20Sopenharmony_ci	mxic_nfc_set_input_delay(nfc, 0xf);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	/*
2508c2ecf20Sopenharmony_ci	 * Phase degree = 360 * freq * output-delay
2518c2ecf20Sopenharmony_ci	 * where output-delay is a constant value 1 ns in FPGA.
2528c2ecf20Sopenharmony_ci	 *
2538c2ecf20Sopenharmony_ci	 * Get Phase degree = 360 * freq * 1 ns
2548c2ecf20Sopenharmony_ci	 *                  = 360 * freq * 1 sec / 1000000000
2558c2ecf20Sopenharmony_ci	 *                  = 9 * freq / 25000000
2568c2ecf20Sopenharmony_ci	 */
2578c2ecf20Sopenharmony_ci	ret = clk_set_phase(nfc->send_dly_clk, 9 * freq / 25000000);
2588c2ecf20Sopenharmony_ci	if (ret)
2598c2ecf20Sopenharmony_ci		return ret;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	return 0;
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cistatic int mxic_nfc_set_freq(struct mxic_nand_ctlr *nfc, unsigned long freq)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	int ret;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	if (freq > MXIC_NFC_MAX_CLK_HZ)
2698c2ecf20Sopenharmony_ci		freq = MXIC_NFC_MAX_CLK_HZ;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	mxic_nfc_clk_disable(nfc);
2728c2ecf20Sopenharmony_ci	ret = mxic_nfc_clk_setup(nfc, freq);
2738c2ecf20Sopenharmony_ci	if (ret)
2748c2ecf20Sopenharmony_ci		return ret;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	ret = mxic_nfc_clk_enable(nfc);
2778c2ecf20Sopenharmony_ci	if (ret)
2788c2ecf20Sopenharmony_ci		return ret;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	return 0;
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cistatic irqreturn_t mxic_nfc_isr(int irq, void *dev_id)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	struct mxic_nand_ctlr *nfc = dev_id;
2868c2ecf20Sopenharmony_ci	u32 sts;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	sts = readl(nfc->regs + INT_STS);
2898c2ecf20Sopenharmony_ci	if (sts & INT_RDY_PIN)
2908c2ecf20Sopenharmony_ci		complete(&nfc->complete);
2918c2ecf20Sopenharmony_ci	else
2928c2ecf20Sopenharmony_ci		return IRQ_NONE;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_cistatic void mxic_nfc_hw_init(struct mxic_nand_ctlr *nfc)
2988c2ecf20Sopenharmony_ci{
2998c2ecf20Sopenharmony_ci	writel(HC_CFG_NIO(8) | HC_CFG_TYPE(1, HC_CFG_TYPE_RAW_NAND) |
3008c2ecf20Sopenharmony_ci	       HC_CFG_SLV_ACT(0) | HC_CFG_MAN_CS_EN |
3018c2ecf20Sopenharmony_ci	       HC_CFG_IDLE_SIO_LVL(1), nfc->regs + HC_CFG);
3028c2ecf20Sopenharmony_ci	writel(INT_STS_ALL, nfc->regs + INT_STS_EN);
3038c2ecf20Sopenharmony_ci	writel(INT_RDY_PIN, nfc->regs + INT_SIG_EN);
3048c2ecf20Sopenharmony_ci	writel(0x0, nfc->regs + ONFI_DIN_CNT(0));
3058c2ecf20Sopenharmony_ci	writel(0, nfc->regs + LRD_CFG);
3068c2ecf20Sopenharmony_ci	writel(0, nfc->regs + LRD_CTRL);
3078c2ecf20Sopenharmony_ci	writel(0x0, nfc->regs + HC_EN);
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic void mxic_nfc_cs_enable(struct mxic_nand_ctlr *nfc)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	writel(readl(nfc->regs + HC_CFG) | HC_CFG_MAN_CS_EN,
3138c2ecf20Sopenharmony_ci	       nfc->regs + HC_CFG);
3148c2ecf20Sopenharmony_ci	writel(HC_CFG_MAN_CS_ASSERT | readl(nfc->regs + HC_CFG),
3158c2ecf20Sopenharmony_ci	       nfc->regs + HC_CFG);
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_cistatic void mxic_nfc_cs_disable(struct mxic_nand_ctlr *nfc)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	writel(~HC_CFG_MAN_CS_ASSERT & readl(nfc->regs + HC_CFG),
3218c2ecf20Sopenharmony_ci	       nfc->regs + HC_CFG);
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic int  mxic_nfc_wait_ready(struct nand_chip *chip)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	struct mxic_nand_ctlr *nfc = nand_get_controller_data(chip);
3278c2ecf20Sopenharmony_ci	int ret;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	ret = wait_for_completion_timeout(&nfc->complete,
3308c2ecf20Sopenharmony_ci					  msecs_to_jiffies(IRQ_TIMEOUT));
3318c2ecf20Sopenharmony_ci	if (!ret) {
3328c2ecf20Sopenharmony_ci		dev_err(nfc->dev, "nand device timeout\n");
3338c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
3348c2ecf20Sopenharmony_ci	}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	return 0;
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic int mxic_nfc_data_xfer(struct mxic_nand_ctlr *nfc, const void *txbuf,
3408c2ecf20Sopenharmony_ci			      void *rxbuf, unsigned int len)
3418c2ecf20Sopenharmony_ci{
3428c2ecf20Sopenharmony_ci	unsigned int pos = 0;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	while (pos < len) {
3458c2ecf20Sopenharmony_ci		unsigned int nbytes = len - pos;
3468c2ecf20Sopenharmony_ci		u32 data = 0xffffffff;
3478c2ecf20Sopenharmony_ci		u32 sts;
3488c2ecf20Sopenharmony_ci		int ret;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci		if (nbytes > 4)
3518c2ecf20Sopenharmony_ci			nbytes = 4;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci		if (txbuf)
3548c2ecf20Sopenharmony_ci			memcpy(&data, txbuf + pos, nbytes);
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci		ret = readl_poll_timeout(nfc->regs + INT_STS, sts,
3578c2ecf20Sopenharmony_ci					 sts & INT_TX_EMPTY, 0, USEC_PER_SEC);
3588c2ecf20Sopenharmony_ci		if (ret)
3598c2ecf20Sopenharmony_ci			return ret;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci		writel(data, nfc->regs + TXD(nbytes % 4));
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci		ret = readl_poll_timeout(nfc->regs + INT_STS, sts,
3648c2ecf20Sopenharmony_ci					 sts & INT_TX_EMPTY, 0, USEC_PER_SEC);
3658c2ecf20Sopenharmony_ci		if (ret)
3668c2ecf20Sopenharmony_ci			return ret;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci		ret = readl_poll_timeout(nfc->regs + INT_STS, sts,
3698c2ecf20Sopenharmony_ci					 sts & INT_RX_NOT_EMPTY, 0,
3708c2ecf20Sopenharmony_ci					 USEC_PER_SEC);
3718c2ecf20Sopenharmony_ci		if (ret)
3728c2ecf20Sopenharmony_ci			return ret;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci		data = readl(nfc->regs + RXD);
3758c2ecf20Sopenharmony_ci		if (rxbuf) {
3768c2ecf20Sopenharmony_ci			data >>= (8 * (4 - nbytes));
3778c2ecf20Sopenharmony_ci			memcpy(rxbuf + pos, &data, nbytes);
3788c2ecf20Sopenharmony_ci		}
3798c2ecf20Sopenharmony_ci		if (readl(nfc->regs + INT_STS) & INT_RX_NOT_EMPTY)
3808c2ecf20Sopenharmony_ci			dev_warn(nfc->dev, "RX FIFO not empty\n");
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci		pos += nbytes;
3838c2ecf20Sopenharmony_ci	}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	return 0;
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cistatic int mxic_nfc_exec_op(struct nand_chip *chip,
3898c2ecf20Sopenharmony_ci			    const struct nand_operation *op, bool check_only)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	struct mxic_nand_ctlr *nfc = nand_get_controller_data(chip);
3928c2ecf20Sopenharmony_ci	const struct nand_op_instr *instr = NULL;
3938c2ecf20Sopenharmony_ci	int ret = 0;
3948c2ecf20Sopenharmony_ci	unsigned int op_id;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	if (check_only)
3978c2ecf20Sopenharmony_ci		return 0;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	mxic_nfc_cs_enable(nfc);
4008c2ecf20Sopenharmony_ci	init_completion(&nfc->complete);
4018c2ecf20Sopenharmony_ci	for (op_id = 0; op_id < op->ninstrs; op_id++) {
4028c2ecf20Sopenharmony_ci		instr = &op->instrs[op_id];
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci		switch (instr->type) {
4058c2ecf20Sopenharmony_ci		case NAND_OP_CMD_INSTR:
4068c2ecf20Sopenharmony_ci			writel(0, nfc->regs + HC_EN);
4078c2ecf20Sopenharmony_ci			writel(HC_EN_BIT, nfc->regs + HC_EN);
4088c2ecf20Sopenharmony_ci			writel(OP_CMD_BUSW(OP_BUSW_8) |  OP_DUMMY_CYC(0x3F) |
4098c2ecf20Sopenharmony_ci			       OP_CMD_BYTES(0), nfc->regs + SS_CTRL(0));
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci			ret = mxic_nfc_data_xfer(nfc,
4128c2ecf20Sopenharmony_ci						 &instr->ctx.cmd.opcode,
4138c2ecf20Sopenharmony_ci						 NULL, 1);
4148c2ecf20Sopenharmony_ci			break;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci		case NAND_OP_ADDR_INSTR:
4178c2ecf20Sopenharmony_ci			writel(OP_ADDR_BUSW(OP_BUSW_8) | OP_DUMMY_CYC(0x3F) |
4188c2ecf20Sopenharmony_ci			       OP_ADDR_BYTES(instr->ctx.addr.naddrs),
4198c2ecf20Sopenharmony_ci			       nfc->regs + SS_CTRL(0));
4208c2ecf20Sopenharmony_ci			ret = mxic_nfc_data_xfer(nfc,
4218c2ecf20Sopenharmony_ci						 instr->ctx.addr.addrs, NULL,
4228c2ecf20Sopenharmony_ci						 instr->ctx.addr.naddrs);
4238c2ecf20Sopenharmony_ci			break;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci		case NAND_OP_DATA_IN_INSTR:
4268c2ecf20Sopenharmony_ci			writel(0x0, nfc->regs + ONFI_DIN_CNT(0));
4278c2ecf20Sopenharmony_ci			writel(OP_DATA_BUSW(OP_BUSW_8) | OP_DUMMY_CYC(0x3F) |
4288c2ecf20Sopenharmony_ci			       OP_READ, nfc->regs + SS_CTRL(0));
4298c2ecf20Sopenharmony_ci			ret = mxic_nfc_data_xfer(nfc, NULL,
4308c2ecf20Sopenharmony_ci						 instr->ctx.data.buf.in,
4318c2ecf20Sopenharmony_ci						 instr->ctx.data.len);
4328c2ecf20Sopenharmony_ci			break;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci		case NAND_OP_DATA_OUT_INSTR:
4358c2ecf20Sopenharmony_ci			writel(instr->ctx.data.len,
4368c2ecf20Sopenharmony_ci			       nfc->regs + ONFI_DIN_CNT(0));
4378c2ecf20Sopenharmony_ci			writel(OP_DATA_BUSW(OP_BUSW_8) | OP_DUMMY_CYC(0x3F),
4388c2ecf20Sopenharmony_ci			       nfc->regs + SS_CTRL(0));
4398c2ecf20Sopenharmony_ci			ret = mxic_nfc_data_xfer(nfc,
4408c2ecf20Sopenharmony_ci						 instr->ctx.data.buf.out, NULL,
4418c2ecf20Sopenharmony_ci						 instr->ctx.data.len);
4428c2ecf20Sopenharmony_ci			break;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci		case NAND_OP_WAITRDY_INSTR:
4458c2ecf20Sopenharmony_ci			ret = mxic_nfc_wait_ready(chip);
4468c2ecf20Sopenharmony_ci			break;
4478c2ecf20Sopenharmony_ci		}
4488c2ecf20Sopenharmony_ci	}
4498c2ecf20Sopenharmony_ci	mxic_nfc_cs_disable(nfc);
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	return ret;
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_cistatic int mxic_nfc_setup_interface(struct nand_chip *chip, int chipnr,
4558c2ecf20Sopenharmony_ci				    const struct nand_interface_config *conf)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	struct mxic_nand_ctlr *nfc = nand_get_controller_data(chip);
4588c2ecf20Sopenharmony_ci	const struct nand_sdr_timings *sdr;
4598c2ecf20Sopenharmony_ci	unsigned long freq;
4608c2ecf20Sopenharmony_ci	int ret;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	sdr = nand_get_sdr_timings(conf);
4638c2ecf20Sopenharmony_ci	if (IS_ERR(sdr))
4648c2ecf20Sopenharmony_ci		return PTR_ERR(sdr);
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	if (chipnr == NAND_DATA_IFACE_CHECK_ONLY)
4678c2ecf20Sopenharmony_ci		return 0;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	freq = NSEC_PER_SEC / (sdr->tRC_min / 1000);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	ret =  mxic_nfc_set_freq(nfc, freq);
4728c2ecf20Sopenharmony_ci	if (ret)
4738c2ecf20Sopenharmony_ci		dev_err(nfc->dev, "set freq:%ld failed\n", freq);
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	if (sdr->tRC_min < 30000)
4768c2ecf20Sopenharmony_ci		writel(DATA_STROB_EDO_EN, nfc->regs + DATA_STROB);
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	return 0;
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_cistatic const struct nand_controller_ops mxic_nand_controller_ops = {
4828c2ecf20Sopenharmony_ci	.exec_op = mxic_nfc_exec_op,
4838c2ecf20Sopenharmony_ci	.setup_interface = mxic_nfc_setup_interface,
4848c2ecf20Sopenharmony_ci};
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_cistatic int mxic_nfc_probe(struct platform_device *pdev)
4878c2ecf20Sopenharmony_ci{
4888c2ecf20Sopenharmony_ci	struct device_node *nand_np, *np = pdev->dev.of_node;
4898c2ecf20Sopenharmony_ci	struct mtd_info *mtd;
4908c2ecf20Sopenharmony_ci	struct mxic_nand_ctlr *nfc;
4918c2ecf20Sopenharmony_ci	struct nand_chip *nand_chip;
4928c2ecf20Sopenharmony_ci	int err;
4938c2ecf20Sopenharmony_ci	int irq;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	nfc = devm_kzalloc(&pdev->dev, sizeof(struct mxic_nand_ctlr),
4968c2ecf20Sopenharmony_ci			   GFP_KERNEL);
4978c2ecf20Sopenharmony_ci	if (!nfc)
4988c2ecf20Sopenharmony_ci		return -ENOMEM;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	nfc->ps_clk = devm_clk_get(&pdev->dev, "ps");
5018c2ecf20Sopenharmony_ci	if (IS_ERR(nfc->ps_clk))
5028c2ecf20Sopenharmony_ci		return PTR_ERR(nfc->ps_clk);
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	nfc->send_clk = devm_clk_get(&pdev->dev, "send");
5058c2ecf20Sopenharmony_ci	if (IS_ERR(nfc->send_clk))
5068c2ecf20Sopenharmony_ci		return PTR_ERR(nfc->send_clk);
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	nfc->send_dly_clk = devm_clk_get(&pdev->dev, "send_dly");
5098c2ecf20Sopenharmony_ci	if (IS_ERR(nfc->send_dly_clk))
5108c2ecf20Sopenharmony_ci		return PTR_ERR(nfc->send_dly_clk);
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	nfc->regs = devm_platform_ioremap_resource(pdev, 0);
5138c2ecf20Sopenharmony_ci	if (IS_ERR(nfc->regs))
5148c2ecf20Sopenharmony_ci		return PTR_ERR(nfc->regs);
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	nand_chip = &nfc->chip;
5178c2ecf20Sopenharmony_ci	mtd = nand_to_mtd(nand_chip);
5188c2ecf20Sopenharmony_ci	mtd->dev.parent = &pdev->dev;
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	for_each_child_of_node(np, nand_np)
5218c2ecf20Sopenharmony_ci		nand_set_flash_node(nand_chip, nand_np);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	nand_chip->priv = nfc;
5248c2ecf20Sopenharmony_ci	nfc->dev = &pdev->dev;
5258c2ecf20Sopenharmony_ci	nfc->controller.ops = &mxic_nand_controller_ops;
5268c2ecf20Sopenharmony_ci	nand_controller_init(&nfc->controller);
5278c2ecf20Sopenharmony_ci	nand_chip->controller = &nfc->controller;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
5308c2ecf20Sopenharmony_ci	if (irq < 0)
5318c2ecf20Sopenharmony_ci		return irq;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	mxic_nfc_hw_init(nfc);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	err = devm_request_irq(&pdev->dev, irq, mxic_nfc_isr,
5368c2ecf20Sopenharmony_ci			       0, "mxic-nfc", nfc);
5378c2ecf20Sopenharmony_ci	if (err)
5388c2ecf20Sopenharmony_ci		goto fail;
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	err = nand_scan(nand_chip, 1);
5418c2ecf20Sopenharmony_ci	if (err)
5428c2ecf20Sopenharmony_ci		goto fail;
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	err = mtd_device_register(mtd, NULL, 0);
5458c2ecf20Sopenharmony_ci	if (err)
5468c2ecf20Sopenharmony_ci		goto fail;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, nfc);
5498c2ecf20Sopenharmony_ci	return 0;
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_cifail:
5528c2ecf20Sopenharmony_ci	mxic_nfc_clk_disable(nfc);
5538c2ecf20Sopenharmony_ci	return err;
5548c2ecf20Sopenharmony_ci}
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_cistatic int mxic_nfc_remove(struct platform_device *pdev)
5578c2ecf20Sopenharmony_ci{
5588c2ecf20Sopenharmony_ci	struct mxic_nand_ctlr *nfc = platform_get_drvdata(pdev);
5598c2ecf20Sopenharmony_ci	struct nand_chip *chip = &nfc->chip;
5608c2ecf20Sopenharmony_ci	int ret;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	ret = mtd_device_unregister(nand_to_mtd(chip));
5638c2ecf20Sopenharmony_ci	WARN_ON(ret);
5648c2ecf20Sopenharmony_ci	nand_cleanup(chip);
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	mxic_nfc_clk_disable(nfc);
5678c2ecf20Sopenharmony_ci	return 0;
5688c2ecf20Sopenharmony_ci}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_cistatic const struct of_device_id mxic_nfc_of_ids[] = {
5718c2ecf20Sopenharmony_ci	{ .compatible = "mxic,multi-itfc-v009-nand-controller", },
5728c2ecf20Sopenharmony_ci	{},
5738c2ecf20Sopenharmony_ci};
5748c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mxic_nfc_of_ids);
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_cistatic struct platform_driver mxic_nfc_driver = {
5778c2ecf20Sopenharmony_ci	.probe = mxic_nfc_probe,
5788c2ecf20Sopenharmony_ci	.remove = mxic_nfc_remove,
5798c2ecf20Sopenharmony_ci	.driver = {
5808c2ecf20Sopenharmony_ci		.name = "mxic-nfc",
5818c2ecf20Sopenharmony_ci		.of_match_table = mxic_nfc_of_ids,
5828c2ecf20Sopenharmony_ci	},
5838c2ecf20Sopenharmony_ci};
5848c2ecf20Sopenharmony_cimodule_platform_driver(mxic_nfc_driver);
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ciMODULE_AUTHOR("Mason Yang <masonccyang@mxic.com.tw>");
5878c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Macronix raw NAND controller driver");
5888c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
589