xref: /linux/drivers/nvmem/sc27xx-efuse.c (revision 36ec807b627b4c0a0a382f0ae48eac7187d14b2b)
119c54468SFreeman Liu // SPDX-License-Identifier: GPL-2.0
219c54468SFreeman Liu // Copyright (C) 2018 Spreadtrum Communications Inc.
319c54468SFreeman Liu 
419c54468SFreeman Liu #include <linux/hwspinlock.h>
519c54468SFreeman Liu #include <linux/module.h>
619c54468SFreeman Liu #include <linux/of.h>
719c54468SFreeman Liu #include <linux/platform_device.h>
819c54468SFreeman Liu #include <linux/regmap.h>
919c54468SFreeman Liu #include <linux/nvmem-provider.h>
1019c54468SFreeman Liu 
1119c54468SFreeman Liu /* PMIC global registers definition */
1219c54468SFreeman Liu #define SC27XX_MODULE_EN		0xc08
132eef018eSFreeman Liu #define SC2730_MODULE_EN		0x1808
1419c54468SFreeman Liu #define SC27XX_EFUSE_EN			BIT(6)
1519c54468SFreeman Liu 
1619c54468SFreeman Liu /* Efuse controller registers definition */
1719c54468SFreeman Liu #define SC27XX_EFUSE_GLB_CTRL		0x0
1819c54468SFreeman Liu #define SC27XX_EFUSE_DATA_RD		0x4
1919c54468SFreeman Liu #define SC27XX_EFUSE_DATA_WR		0x8
2019c54468SFreeman Liu #define SC27XX_EFUSE_BLOCK_INDEX	0xc
2119c54468SFreeman Liu #define SC27XX_EFUSE_MODE_CTRL		0x10
2219c54468SFreeman Liu #define SC27XX_EFUSE_STATUS		0x14
2319c54468SFreeman Liu #define SC27XX_EFUSE_WR_TIMING_CTRL	0x20
2419c54468SFreeman Liu #define SC27XX_EFUSE_RD_TIMING_CTRL	0x24
2519c54468SFreeman Liu #define SC27XX_EFUSE_EFUSE_DEB_CTRL	0x28
2619c54468SFreeman Liu 
2719c54468SFreeman Liu /* Mask definition for SC27XX_EFUSE_BLOCK_INDEX register */
2819c54468SFreeman Liu #define SC27XX_EFUSE_BLOCK_MASK		GENMASK(4, 0)
2919c54468SFreeman Liu 
3019c54468SFreeman Liu /* Bits definitions for SC27XX_EFUSE_MODE_CTRL register */
3119c54468SFreeman Liu #define SC27XX_EFUSE_PG_START		BIT(0)
3219c54468SFreeman Liu #define SC27XX_EFUSE_RD_START		BIT(1)
3319c54468SFreeman Liu #define SC27XX_EFUSE_CLR_RDDONE		BIT(2)
3419c54468SFreeman Liu 
3519c54468SFreeman Liu /* Bits definitions for SC27XX_EFUSE_STATUS register */
3619c54468SFreeman Liu #define SC27XX_EFUSE_PGM_BUSY		BIT(0)
3719c54468SFreeman Liu #define SC27XX_EFUSE_READ_BUSY		BIT(1)
3819c54468SFreeman Liu #define SC27XX_EFUSE_STANDBY		BIT(2)
3919c54468SFreeman Liu #define SC27XX_EFUSE_GLOBAL_PROT	BIT(3)
4019c54468SFreeman Liu #define SC27XX_EFUSE_RD_DONE		BIT(4)
4119c54468SFreeman Liu 
4219c54468SFreeman Liu /* Block number and block width (bytes) definitions */
4319c54468SFreeman Liu #define SC27XX_EFUSE_BLOCK_MAX		32
4419c54468SFreeman Liu #define SC27XX_EFUSE_BLOCK_WIDTH	2
4519c54468SFreeman Liu 
4619c54468SFreeman Liu /* Timeout (ms) for the trylock of hardware spinlocks */
4719c54468SFreeman Liu #define SC27XX_EFUSE_HWLOCK_TIMEOUT	5000
4819c54468SFreeman Liu 
4919c54468SFreeman Liu /* Timeout (us) of polling the status */
5019c54468SFreeman Liu #define SC27XX_EFUSE_POLL_TIMEOUT	3000000
5119c54468SFreeman Liu #define SC27XX_EFUSE_POLL_DELAY_US	10000
5219c54468SFreeman Liu 
532eef018eSFreeman Liu /*
542eef018eSFreeman Liu  * Since different PMICs of SC27xx series can have different
552eef018eSFreeman Liu  * address , we should save address in the device data structure.
562eef018eSFreeman Liu  */
572eef018eSFreeman Liu struct sc27xx_efuse_variant_data {
582eef018eSFreeman Liu 	u32 module_en;
592eef018eSFreeman Liu };
602eef018eSFreeman Liu 
6119c54468SFreeman Liu struct sc27xx_efuse {
6219c54468SFreeman Liu 	struct device *dev;
6319c54468SFreeman Liu 	struct regmap *regmap;
6419c54468SFreeman Liu 	struct hwspinlock *hwlock;
6519c54468SFreeman Liu 	struct mutex mutex;
6619c54468SFreeman Liu 	u32 base;
672eef018eSFreeman Liu 	const struct sc27xx_efuse_variant_data *var_data;
682eef018eSFreeman Liu };
692eef018eSFreeman Liu 
702eef018eSFreeman Liu static const struct sc27xx_efuse_variant_data sc2731_edata = {
712eef018eSFreeman Liu 	.module_en = SC27XX_MODULE_EN,
722eef018eSFreeman Liu };
732eef018eSFreeman Liu 
742eef018eSFreeman Liu static const struct sc27xx_efuse_variant_data sc2730_edata = {
752eef018eSFreeman Liu 	.module_en = SC2730_MODULE_EN,
7619c54468SFreeman Liu };
7719c54468SFreeman Liu 
7819c54468SFreeman Liu /*
7919c54468SFreeman Liu  * On Spreadtrum platform, we have multi-subsystems will access the unique
8019c54468SFreeman Liu  * efuse controller, so we need one hardware spinlock to synchronize between
8119c54468SFreeman Liu  * the multiple subsystems.
8219c54468SFreeman Liu  */
8319c54468SFreeman Liu static int sc27xx_efuse_lock(struct sc27xx_efuse *efuse)
8419c54468SFreeman Liu {
8519c54468SFreeman Liu 	int ret;
8619c54468SFreeman Liu 
8719c54468SFreeman Liu 	mutex_lock(&efuse->mutex);
8819c54468SFreeman Liu 
8919c54468SFreeman Liu 	ret = hwspin_lock_timeout_raw(efuse->hwlock,
9019c54468SFreeman Liu 				      SC27XX_EFUSE_HWLOCK_TIMEOUT);
9119c54468SFreeman Liu 	if (ret) {
9219c54468SFreeman Liu 		dev_err(efuse->dev, "timeout to get the hwspinlock\n");
9319c54468SFreeman Liu 		mutex_unlock(&efuse->mutex);
9419c54468SFreeman Liu 		return ret;
9519c54468SFreeman Liu 	}
9619c54468SFreeman Liu 
9719c54468SFreeman Liu 	return 0;
9819c54468SFreeman Liu }
9919c54468SFreeman Liu 
10019c54468SFreeman Liu static void sc27xx_efuse_unlock(struct sc27xx_efuse *efuse)
10119c54468SFreeman Liu {
10219c54468SFreeman Liu 	hwspin_unlock_raw(efuse->hwlock);
10319c54468SFreeman Liu 	mutex_unlock(&efuse->mutex);
10419c54468SFreeman Liu }
10519c54468SFreeman Liu 
10619c54468SFreeman Liu static int sc27xx_efuse_poll_status(struct sc27xx_efuse *efuse, u32 bits)
10719c54468SFreeman Liu {
10819c54468SFreeman Liu 	int ret;
10919c54468SFreeman Liu 	u32 val;
11019c54468SFreeman Liu 
11119c54468SFreeman Liu 	ret = regmap_read_poll_timeout(efuse->regmap,
11219c54468SFreeman Liu 				       efuse->base + SC27XX_EFUSE_STATUS,
11319c54468SFreeman Liu 				       val, (val & bits),
11419c54468SFreeman Liu 				       SC27XX_EFUSE_POLL_DELAY_US,
11519c54468SFreeman Liu 				       SC27XX_EFUSE_POLL_TIMEOUT);
11619c54468SFreeman Liu 	if (ret) {
11719c54468SFreeman Liu 		dev_err(efuse->dev, "timeout to update the efuse status\n");
11819c54468SFreeman Liu 		return ret;
11919c54468SFreeman Liu 	}
12019c54468SFreeman Liu 
12119c54468SFreeman Liu 	return 0;
12219c54468SFreeman Liu }
12319c54468SFreeman Liu 
12419c54468SFreeman Liu static int sc27xx_efuse_read(void *context, u32 offset, void *val, size_t bytes)
12519c54468SFreeman Liu {
12619c54468SFreeman Liu 	struct sc27xx_efuse *efuse = context;
127996e39bbSFreeman Liu 	u32 buf, blk_index = offset / SC27XX_EFUSE_BLOCK_WIDTH;
128996e39bbSFreeman Liu 	u32 blk_offset = (offset % SC27XX_EFUSE_BLOCK_WIDTH) * BITS_PER_BYTE;
12919c54468SFreeman Liu 	int ret;
13019c54468SFreeman Liu 
131996e39bbSFreeman Liu 	if (blk_index > SC27XX_EFUSE_BLOCK_MAX ||
132996e39bbSFreeman Liu 	    bytes > SC27XX_EFUSE_BLOCK_WIDTH)
13319c54468SFreeman Liu 		return -EINVAL;
13419c54468SFreeman Liu 
13519c54468SFreeman Liu 	ret = sc27xx_efuse_lock(efuse);
13619c54468SFreeman Liu 	if (ret)
13719c54468SFreeman Liu 		return ret;
13819c54468SFreeman Liu 
13919c54468SFreeman Liu 	/* Enable the efuse controller. */
1402eef018eSFreeman Liu 	ret = regmap_update_bits(efuse->regmap, efuse->var_data->module_en,
14119c54468SFreeman Liu 				 SC27XX_EFUSE_EN, SC27XX_EFUSE_EN);
14219c54468SFreeman Liu 	if (ret)
14319c54468SFreeman Liu 		goto unlock_efuse;
14419c54468SFreeman Liu 
14519c54468SFreeman Liu 	/*
14619c54468SFreeman Liu 	 * Before reading, we should ensure the efuse controller is in
14719c54468SFreeman Liu 	 * standby state.
14819c54468SFreeman Liu 	 */
14919c54468SFreeman Liu 	ret = sc27xx_efuse_poll_status(efuse, SC27XX_EFUSE_STANDBY);
15019c54468SFreeman Liu 	if (ret)
15119c54468SFreeman Liu 		goto disable_efuse;
15219c54468SFreeman Liu 
15319c54468SFreeman Liu 	/* Set the block address to be read. */
15419c54468SFreeman Liu 	ret = regmap_write(efuse->regmap,
15519c54468SFreeman Liu 			   efuse->base + SC27XX_EFUSE_BLOCK_INDEX,
156996e39bbSFreeman Liu 			   blk_index & SC27XX_EFUSE_BLOCK_MASK);
15719c54468SFreeman Liu 	if (ret)
15819c54468SFreeman Liu 		goto disable_efuse;
15919c54468SFreeman Liu 
16019c54468SFreeman Liu 	/* Start reading process from efuse memory. */
16119c54468SFreeman Liu 	ret = regmap_update_bits(efuse->regmap,
16219c54468SFreeman Liu 				 efuse->base + SC27XX_EFUSE_MODE_CTRL,
16319c54468SFreeman Liu 				 SC27XX_EFUSE_RD_START,
16419c54468SFreeman Liu 				 SC27XX_EFUSE_RD_START);
16519c54468SFreeman Liu 	if (ret)
16619c54468SFreeman Liu 		goto disable_efuse;
16719c54468SFreeman Liu 
16819c54468SFreeman Liu 	/*
16919c54468SFreeman Liu 	 * Polling the read done status to make sure the reading process
17019c54468SFreeman Liu 	 * is completed, that means the data can be read out now.
17119c54468SFreeman Liu 	 */
17219c54468SFreeman Liu 	ret = sc27xx_efuse_poll_status(efuse, SC27XX_EFUSE_RD_DONE);
17319c54468SFreeman Liu 	if (ret)
17419c54468SFreeman Liu 		goto disable_efuse;
17519c54468SFreeman Liu 
17619c54468SFreeman Liu 	/* Read data from efuse memory. */
17719c54468SFreeman Liu 	ret = regmap_read(efuse->regmap, efuse->base + SC27XX_EFUSE_DATA_RD,
17819c54468SFreeman Liu 			  &buf);
17919c54468SFreeman Liu 	if (ret)
18019c54468SFreeman Liu 		goto disable_efuse;
18119c54468SFreeman Liu 
18219c54468SFreeman Liu 	/* Clear the read done flag. */
18319c54468SFreeman Liu 	ret = regmap_update_bits(efuse->regmap,
18419c54468SFreeman Liu 				 efuse->base + SC27XX_EFUSE_MODE_CTRL,
18519c54468SFreeman Liu 				 SC27XX_EFUSE_CLR_RDDONE,
18619c54468SFreeman Liu 				 SC27XX_EFUSE_CLR_RDDONE);
18719c54468SFreeman Liu 
18819c54468SFreeman Liu disable_efuse:
18919c54468SFreeman Liu 	/* Disable the efuse controller after reading. */
1902eef018eSFreeman Liu 	regmap_update_bits(efuse->regmap, efuse->var_data->module_en, SC27XX_EFUSE_EN, 0);
19119c54468SFreeman Liu unlock_efuse:
19219c54468SFreeman Liu 	sc27xx_efuse_unlock(efuse);
19319c54468SFreeman Liu 
194996e39bbSFreeman Liu 	if (!ret) {
195996e39bbSFreeman Liu 		buf >>= blk_offset;
19619c54468SFreeman Liu 		memcpy(val, &buf, bytes);
197996e39bbSFreeman Liu 	}
19819c54468SFreeman Liu 
19919c54468SFreeman Liu 	return ret;
20019c54468SFreeman Liu }
20119c54468SFreeman Liu 
20219c54468SFreeman Liu static int sc27xx_efuse_probe(struct platform_device *pdev)
20319c54468SFreeman Liu {
20419c54468SFreeman Liu 	struct device_node *np = pdev->dev.of_node;
20519c54468SFreeman Liu 	struct nvmem_config econfig = { };
20619c54468SFreeman Liu 	struct nvmem_device *nvmem;
20719c54468SFreeman Liu 	struct sc27xx_efuse *efuse;
20819c54468SFreeman Liu 	int ret;
20919c54468SFreeman Liu 
21019c54468SFreeman Liu 	efuse = devm_kzalloc(&pdev->dev, sizeof(*efuse), GFP_KERNEL);
21119c54468SFreeman Liu 	if (!efuse)
21219c54468SFreeman Liu 		return -ENOMEM;
21319c54468SFreeman Liu 
21419c54468SFreeman Liu 	efuse->regmap = dev_get_regmap(pdev->dev.parent, NULL);
21519c54468SFreeman Liu 	if (!efuse->regmap) {
21619c54468SFreeman Liu 		dev_err(&pdev->dev, "failed to get efuse regmap\n");
21719c54468SFreeman Liu 		return -ENODEV;
21819c54468SFreeman Liu 	}
21919c54468SFreeman Liu 
22019c54468SFreeman Liu 	ret = of_property_read_u32(np, "reg", &efuse->base);
22119c54468SFreeman Liu 	if (ret) {
22219c54468SFreeman Liu 		dev_err(&pdev->dev, "failed to get efuse base address\n");
22319c54468SFreeman Liu 		return ret;
22419c54468SFreeman Liu 	}
22519c54468SFreeman Liu 
22619c54468SFreeman Liu 	ret = of_hwspin_lock_get_id(np, 0);
22719c54468SFreeman Liu 	if (ret < 0) {
22819c54468SFreeman Liu 		dev_err(&pdev->dev, "failed to get hwspinlock id\n");
22919c54468SFreeman Liu 		return ret;
23019c54468SFreeman Liu 	}
23119c54468SFreeman Liu 
2321e6d8e5fSBaolin Wang 	efuse->hwlock = devm_hwspin_lock_request_specific(&pdev->dev, ret);
23319c54468SFreeman Liu 	if (!efuse->hwlock) {
23419c54468SFreeman Liu 		dev_err(&pdev->dev, "failed to request hwspinlock\n");
23519c54468SFreeman Liu 		return -ENXIO;
23619c54468SFreeman Liu 	}
23719c54468SFreeman Liu 
23819c54468SFreeman Liu 	mutex_init(&efuse->mutex);
23919c54468SFreeman Liu 	efuse->dev = &pdev->dev;
2402eef018eSFreeman Liu 	efuse->var_data = of_device_get_match_data(&pdev->dev);
24119c54468SFreeman Liu 
24219c54468SFreeman Liu 	econfig.stride = 1;
24319c54468SFreeman Liu 	econfig.word_size = 1;
24419c54468SFreeman Liu 	econfig.read_only = true;
24519c54468SFreeman Liu 	econfig.name = "sc27xx-efuse";
24619c54468SFreeman Liu 	econfig.size = SC27XX_EFUSE_BLOCK_MAX * SC27XX_EFUSE_BLOCK_WIDTH;
24719c54468SFreeman Liu 	econfig.reg_read = sc27xx_efuse_read;
24819c54468SFreeman Liu 	econfig.priv = efuse;
24919c54468SFreeman Liu 	econfig.dev = &pdev->dev;
2502cc3b37fSRafał Miłecki 	econfig.add_legacy_fixed_of_cells = true;
25119c54468SFreeman Liu 	nvmem = devm_nvmem_register(&pdev->dev, &econfig);
25219c54468SFreeman Liu 	if (IS_ERR(nvmem)) {
25319c54468SFreeman Liu 		dev_err(&pdev->dev, "failed to register nvmem config\n");
25419c54468SFreeman Liu 		return PTR_ERR(nvmem);
25519c54468SFreeman Liu 	}
25619c54468SFreeman Liu 
25719c54468SFreeman Liu 	return 0;
25819c54468SFreeman Liu }
25919c54468SFreeman Liu 
26019c54468SFreeman Liu static const struct of_device_id sc27xx_efuse_of_match[] = {
2612eef018eSFreeman Liu 	{ .compatible = "sprd,sc2731-efuse", .data = &sc2731_edata},
2622eef018eSFreeman Liu 	{ .compatible = "sprd,sc2730-efuse", .data = &sc2730_edata},
26319c54468SFreeman Liu 	{ }
26419c54468SFreeman Liu };
265*dc3d88adSKrzysztof Kozlowski MODULE_DEVICE_TABLE(of, sc27xx_efuse_of_match);
26619c54468SFreeman Liu 
26719c54468SFreeman Liu static struct platform_driver sc27xx_efuse_driver = {
26819c54468SFreeman Liu 	.probe = sc27xx_efuse_probe,
26919c54468SFreeman Liu 	.driver = {
27019c54468SFreeman Liu 		.name = "sc27xx-efuse",
27119c54468SFreeman Liu 		.of_match_table = sc27xx_efuse_of_match,
27219c54468SFreeman Liu 	},
27319c54468SFreeman Liu };
27419c54468SFreeman Liu 
27519c54468SFreeman Liu module_platform_driver(sc27xx_efuse_driver);
27619c54468SFreeman Liu 
27719c54468SFreeman Liu MODULE_AUTHOR("Freeman Liu <freeman.liu@spreadtrum.com>");
27819c54468SFreeman Liu MODULE_DESCRIPTION("Spreadtrum SC27xx efuse driver");
27919c54468SFreeman Liu MODULE_LICENSE("GPL v2");
280