162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci/*
462306a36Sopenharmony_ci * The OCOTP driver for Sunplus	SP7021
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (C) 2019 Sunplus Technology Inc., All rights reserved.
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/bitfield.h>
1062306a36Sopenharmony_ci#include <linux/clk.h>
1162306a36Sopenharmony_ci#include <linux/delay.h>
1262306a36Sopenharmony_ci#include <linux/device.h>
1362306a36Sopenharmony_ci#include <linux/io.h>
1462306a36Sopenharmony_ci#include <linux/iopoll.h>
1562306a36Sopenharmony_ci#include <linux/module.h>
1662306a36Sopenharmony_ci#include <linux/mod_devicetable.h>
1762306a36Sopenharmony_ci#include <linux/nvmem-provider.h>
1862306a36Sopenharmony_ci#include <linux/platform_device.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/*
2162306a36Sopenharmony_ci * OTP memory
2262306a36Sopenharmony_ci * Each bank contains 4 words (32 bits).
2362306a36Sopenharmony_ci * Bank 0 starts at offset 0 from the base.
2462306a36Sopenharmony_ci */
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define OTP_WORDS_PER_BANK		4
2762306a36Sopenharmony_ci#define OTP_WORD_SIZE			sizeof(u32)
2862306a36Sopenharmony_ci#define OTP_BIT_ADDR_OF_BANK		(8 * OTP_WORD_SIZE * OTP_WORDS_PER_BANK)
2962306a36Sopenharmony_ci#define QAC628_OTP_NUM_BANKS		8
3062306a36Sopenharmony_ci#define QAC628_OTP_SIZE			(QAC628_OTP_NUM_BANKS * OTP_WORDS_PER_BANK * OTP_WORD_SIZE)
3162306a36Sopenharmony_ci#define OTP_READ_TIMEOUT_US		200000
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/* HB_GPIO */
3462306a36Sopenharmony_ci#define ADDRESS_8_DATA			0x20
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci/* OTP_RX */
3762306a36Sopenharmony_ci#define OTP_CONTROL_2			0x48
3862306a36Sopenharmony_ci#define OTP_RD_PERIOD			GENMASK(15, 8)
3962306a36Sopenharmony_ci#define OTP_RD_PERIOD_MASK		~GENMASK(15, 8)
4062306a36Sopenharmony_ci#define CPU_CLOCK			FIELD_PREP(OTP_RD_PERIOD, 30)
4162306a36Sopenharmony_ci#define SEL_BAK_KEY2			BIT(5)
4262306a36Sopenharmony_ci#define SEL_BAK_KEY2_MASK		~BIT(5)
4362306a36Sopenharmony_ci#define SW_TRIM_EN			BIT(4)
4462306a36Sopenharmony_ci#define SW_TRIM_EN_MASK			~BIT(4)
4562306a36Sopenharmony_ci#define SEL_BAK_KEY			BIT(3)
4662306a36Sopenharmony_ci#define SEL_BAK_KEY_MASK		~BIT(3)
4762306a36Sopenharmony_ci#define OTP_READ			BIT(2)
4862306a36Sopenharmony_ci#define OTP_LOAD_SECURE_DATA		BIT(1)
4962306a36Sopenharmony_ci#define OTP_LOAD_SECURE_DATA_MASK	~BIT(1)
5062306a36Sopenharmony_ci#define OTP_DO_CRC			BIT(0)
5162306a36Sopenharmony_ci#define OTP_DO_CRC_MASK			~BIT(0)
5262306a36Sopenharmony_ci#define OTP_STATUS			0x4c
5362306a36Sopenharmony_ci#define OTP_READ_DONE			BIT(4)
5462306a36Sopenharmony_ci#define OTP_READ_DONE_MASK		~BIT(4)
5562306a36Sopenharmony_ci#define OTP_LOAD_SECURE_DONE_MASK	~BIT(2)
5662306a36Sopenharmony_ci#define OTP_READ_ADDRESS		0x50
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_cienum base_type {
5962306a36Sopenharmony_ci	HB_GPIO,
6062306a36Sopenharmony_ci	OTPRX,
6162306a36Sopenharmony_ci	BASEMAX,
6262306a36Sopenharmony_ci};
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_cistruct sp_ocotp_priv {
6562306a36Sopenharmony_ci	struct device *dev;
6662306a36Sopenharmony_ci	void __iomem *base[BASEMAX];
6762306a36Sopenharmony_ci	struct clk *clk;
6862306a36Sopenharmony_ci};
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_cistruct sp_ocotp_data {
7162306a36Sopenharmony_ci	int size;
7262306a36Sopenharmony_ci};
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_cistatic const struct sp_ocotp_data sp_otp_v0 = {
7562306a36Sopenharmony_ci	.size = QAC628_OTP_SIZE,
7662306a36Sopenharmony_ci};
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_cistatic int sp_otp_read_real(struct sp_ocotp_priv *otp, int addr, char *value)
7962306a36Sopenharmony_ci{
8062306a36Sopenharmony_ci	unsigned int addr_data;
8162306a36Sopenharmony_ci	unsigned int byte_shift;
8262306a36Sopenharmony_ci	unsigned int status;
8362306a36Sopenharmony_ci	int ret;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	addr_data = addr % (OTP_WORD_SIZE * OTP_WORDS_PER_BANK);
8662306a36Sopenharmony_ci	addr_data = addr_data / OTP_WORD_SIZE;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	byte_shift = addr % (OTP_WORD_SIZE * OTP_WORDS_PER_BANK);
8962306a36Sopenharmony_ci	byte_shift = byte_shift % OTP_WORD_SIZE;
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	addr = addr / (OTP_WORD_SIZE * OTP_WORDS_PER_BANK);
9262306a36Sopenharmony_ci	addr = addr * OTP_BIT_ADDR_OF_BANK;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	writel(readl(otp->base[OTPRX] + OTP_STATUS) & OTP_READ_DONE_MASK &
9562306a36Sopenharmony_ci	       OTP_LOAD_SECURE_DONE_MASK, otp->base[OTPRX] + OTP_STATUS);
9662306a36Sopenharmony_ci	writel(addr, otp->base[OTPRX] + OTP_READ_ADDRESS);
9762306a36Sopenharmony_ci	writel(readl(otp->base[OTPRX] + OTP_CONTROL_2) | OTP_READ,
9862306a36Sopenharmony_ci	       otp->base[OTPRX] + OTP_CONTROL_2);
9962306a36Sopenharmony_ci	writel(readl(otp->base[OTPRX] + OTP_CONTROL_2) & SEL_BAK_KEY2_MASK & SW_TRIM_EN_MASK
10062306a36Sopenharmony_ci	       & SEL_BAK_KEY_MASK & OTP_LOAD_SECURE_DATA_MASK & OTP_DO_CRC_MASK,
10162306a36Sopenharmony_ci	       otp->base[OTPRX] + OTP_CONTROL_2);
10262306a36Sopenharmony_ci	writel((readl(otp->base[OTPRX] + OTP_CONTROL_2) & OTP_RD_PERIOD_MASK) | CPU_CLOCK,
10362306a36Sopenharmony_ci	       otp->base[OTPRX] + OTP_CONTROL_2);
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	ret = readl_poll_timeout(otp->base[OTPRX] + OTP_STATUS, status,
10662306a36Sopenharmony_ci				 status & OTP_READ_DONE, 10, OTP_READ_TIMEOUT_US);
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	if (ret < 0)
10962306a36Sopenharmony_ci		return ret;
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	*value = (readl(otp->base[HB_GPIO] + ADDRESS_8_DATA + addr_data * OTP_WORD_SIZE)
11262306a36Sopenharmony_ci		  >> (8 * byte_shift)) & 0xff;
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	return ret;
11562306a36Sopenharmony_ci}
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_cistatic int sp_ocotp_read(void *priv, unsigned int offset, void *value, size_t bytes)
11862306a36Sopenharmony_ci{
11962306a36Sopenharmony_ci	struct sp_ocotp_priv *otp = priv;
12062306a36Sopenharmony_ci	unsigned int addr;
12162306a36Sopenharmony_ci	char *buf = value;
12262306a36Sopenharmony_ci	char val[4];
12362306a36Sopenharmony_ci	int ret;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	ret = clk_enable(otp->clk);
12662306a36Sopenharmony_ci	if (ret)
12762306a36Sopenharmony_ci		return ret;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	*buf = 0;
13062306a36Sopenharmony_ci	for (addr = offset; addr < (offset + bytes); addr++) {
13162306a36Sopenharmony_ci		ret = sp_otp_read_real(otp, addr, val);
13262306a36Sopenharmony_ci		if (ret < 0) {
13362306a36Sopenharmony_ci			dev_err(otp->dev, "OTP read fail:%d at %d", ret, addr);
13462306a36Sopenharmony_ci			goto disable_clk;
13562306a36Sopenharmony_ci		}
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci		*buf++ = *val;
13862306a36Sopenharmony_ci	}
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_cidisable_clk:
14162306a36Sopenharmony_ci	clk_disable(otp->clk);
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	return ret;
14462306a36Sopenharmony_ci}
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_cistatic struct nvmem_config sp_ocotp_nvmem_config = {
14762306a36Sopenharmony_ci	.name = "sp-ocotp",
14862306a36Sopenharmony_ci	.read_only = true,
14962306a36Sopenharmony_ci	.word_size = 1,
15062306a36Sopenharmony_ci	.size = QAC628_OTP_SIZE,
15162306a36Sopenharmony_ci	.stride = 1,
15262306a36Sopenharmony_ci	.reg_read = sp_ocotp_read,
15362306a36Sopenharmony_ci	.owner = THIS_MODULE,
15462306a36Sopenharmony_ci};
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_cistatic int sp_ocotp_probe(struct platform_device *pdev)
15762306a36Sopenharmony_ci{
15862306a36Sopenharmony_ci	struct device *dev = &pdev->dev;
15962306a36Sopenharmony_ci	struct nvmem_device *nvmem;
16062306a36Sopenharmony_ci	struct sp_ocotp_priv *otp;
16162306a36Sopenharmony_ci	struct resource *res;
16262306a36Sopenharmony_ci	int ret;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	otp = devm_kzalloc(dev, sizeof(*otp), GFP_KERNEL);
16562306a36Sopenharmony_ci	if (!otp)
16662306a36Sopenharmony_ci		return -ENOMEM;
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	otp->dev = dev;
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hb_gpio");
17162306a36Sopenharmony_ci	otp->base[HB_GPIO] = devm_ioremap_resource(dev, res);
17262306a36Sopenharmony_ci	if (IS_ERR(otp->base[HB_GPIO]))
17362306a36Sopenharmony_ci		return PTR_ERR(otp->base[HB_GPIO]);
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otprx");
17662306a36Sopenharmony_ci	otp->base[OTPRX] = devm_ioremap_resource(dev, res);
17762306a36Sopenharmony_ci	if (IS_ERR(otp->base[OTPRX]))
17862306a36Sopenharmony_ci		return PTR_ERR(otp->base[OTPRX]);
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	otp->clk = devm_clk_get(&pdev->dev, NULL);
18162306a36Sopenharmony_ci	if (IS_ERR(otp->clk))
18262306a36Sopenharmony_ci		return dev_err_probe(&pdev->dev, PTR_ERR(otp->clk),
18362306a36Sopenharmony_ci						"devm_clk_get fail\n");
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci	ret = clk_prepare(otp->clk);
18662306a36Sopenharmony_ci	if (ret < 0) {
18762306a36Sopenharmony_ci		dev_err(dev, "failed to prepare clk: %d\n", ret);
18862306a36Sopenharmony_ci		return ret;
18962306a36Sopenharmony_ci	}
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci	sp_ocotp_nvmem_config.priv = otp;
19262306a36Sopenharmony_ci	sp_ocotp_nvmem_config.dev = dev;
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci	nvmem = devm_nvmem_register(dev, &sp_ocotp_nvmem_config);
19562306a36Sopenharmony_ci	if (IS_ERR(nvmem)) {
19662306a36Sopenharmony_ci		ret = dev_err_probe(&pdev->dev, PTR_ERR(nvmem),
19762306a36Sopenharmony_ci						"register nvmem device fail\n");
19862306a36Sopenharmony_ci		goto err;
19962306a36Sopenharmony_ci	}
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci	platform_set_drvdata(pdev, nvmem);
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	dev_dbg(dev, "banks:%d x wpb:%d x wsize:%d = %d",
20462306a36Sopenharmony_ci		(int)QAC628_OTP_NUM_BANKS, (int)OTP_WORDS_PER_BANK,
20562306a36Sopenharmony_ci		(int)OTP_WORD_SIZE, (int)QAC628_OTP_SIZE);
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci	return 0;
20862306a36Sopenharmony_cierr:
20962306a36Sopenharmony_ci	clk_unprepare(otp->clk);
21062306a36Sopenharmony_ci	return ret;
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_cistatic const struct of_device_id sp_ocotp_dt_ids[] = {
21462306a36Sopenharmony_ci	{ .compatible = "sunplus,sp7021-ocotp", .data = &sp_otp_v0 },
21562306a36Sopenharmony_ci	{ }
21662306a36Sopenharmony_ci};
21762306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, sp_ocotp_dt_ids);
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_cistatic struct platform_driver sp_otp_driver = {
22062306a36Sopenharmony_ci	.probe     = sp_ocotp_probe,
22162306a36Sopenharmony_ci	.driver    = {
22262306a36Sopenharmony_ci		.name           = "sunplus,sp7021-ocotp",
22362306a36Sopenharmony_ci		.of_match_table = sp_ocotp_dt_ids,
22462306a36Sopenharmony_ci	}
22562306a36Sopenharmony_ci};
22662306a36Sopenharmony_cimodule_platform_driver(sp_otp_driver);
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ciMODULE_AUTHOR("Vincent Shih <vincent.sunplus@gmail.com>");
22962306a36Sopenharmony_ciMODULE_DESCRIPTION("Sunplus On-Chip OTP driver");
23062306a36Sopenharmony_ciMODULE_LICENSE("GPL");
23162306a36Sopenharmony_ci
232