1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Spreadtrum Communications Inc.
4 #include <linux/hwspinlock.h>
5 #include <linux/module.h>
7 #include <linux/platform_device.h>
8 #include <linux/regmap.h>
9 #include <linux/nvmem-provider.h>
11 /* PMIC global registers definition */
12 #define SC27XX_MODULE_EN 0xc08
13 #define SC27XX_EFUSE_EN BIT(6)
15 /* Efuse controller registers definition */
16 #define SC27XX_EFUSE_GLB_CTRL 0x0
17 #define SC27XX_EFUSE_DATA_RD 0x4
18 #define SC27XX_EFUSE_DATA_WR 0x8
19 #define SC27XX_EFUSE_BLOCK_INDEX 0xc
20 #define SC27XX_EFUSE_MODE_CTRL 0x10
21 #define SC27XX_EFUSE_STATUS 0x14
22 #define SC27XX_EFUSE_WR_TIMING_CTRL 0x20
23 #define SC27XX_EFUSE_RD_TIMING_CTRL 0x24
24 #define SC27XX_EFUSE_EFUSE_DEB_CTRL 0x28
26 /* Mask definition for SC27XX_EFUSE_BLOCK_INDEX register */
27 #define SC27XX_EFUSE_BLOCK_MASK GENMASK(4, 0)
29 /* Bits definitions for SC27XX_EFUSE_MODE_CTRL register */
30 #define SC27XX_EFUSE_PG_START BIT(0)
31 #define SC27XX_EFUSE_RD_START BIT(1)
32 #define SC27XX_EFUSE_CLR_RDDONE BIT(2)
34 /* Bits definitions for SC27XX_EFUSE_STATUS register */
35 #define SC27XX_EFUSE_PGM_BUSY BIT(0)
36 #define SC27XX_EFUSE_READ_BUSY BIT(1)
37 #define SC27XX_EFUSE_STANDBY BIT(2)
38 #define SC27XX_EFUSE_GLOBAL_PROT BIT(3)
39 #define SC27XX_EFUSE_RD_DONE BIT(4)
41 /* Block number and block width (bytes) definitions */
42 #define SC27XX_EFUSE_BLOCK_MAX 32
43 #define SC27XX_EFUSE_BLOCK_WIDTH 2
45 /* Timeout (ms) for the trylock of hardware spinlocks */
46 #define SC27XX_EFUSE_HWLOCK_TIMEOUT 5000
48 /* Timeout (us) of polling the status */
49 #define SC27XX_EFUSE_POLL_TIMEOUT 3000000
50 #define SC27XX_EFUSE_POLL_DELAY_US 10000
54 struct regmap
*regmap
;
55 struct hwspinlock
*hwlock
;
61 * On Spreadtrum platform, we have multi-subsystems will access the unique
62 * efuse controller, so we need one hardware spinlock to synchronize between
63 * the multiple subsystems.
65 static int sc27xx_efuse_lock(struct sc27xx_efuse
*efuse
)
69 mutex_lock(&efuse
->mutex
);
71 ret
= hwspin_lock_timeout_raw(efuse
->hwlock
,
72 SC27XX_EFUSE_HWLOCK_TIMEOUT
);
74 dev_err(efuse
->dev
, "timeout to get the hwspinlock\n");
75 mutex_unlock(&efuse
->mutex
);
82 static void sc27xx_efuse_unlock(struct sc27xx_efuse
*efuse
)
84 hwspin_unlock_raw(efuse
->hwlock
);
85 mutex_unlock(&efuse
->mutex
);
88 static int sc27xx_efuse_poll_status(struct sc27xx_efuse
*efuse
, u32 bits
)
93 ret
= regmap_read_poll_timeout(efuse
->regmap
,
94 efuse
->base
+ SC27XX_EFUSE_STATUS
,
96 SC27XX_EFUSE_POLL_DELAY_US
,
97 SC27XX_EFUSE_POLL_TIMEOUT
);
99 dev_err(efuse
->dev
, "timeout to update the efuse status\n");
106 static int sc27xx_efuse_read(void *context
, u32 offset
, void *val
, size_t bytes
)
108 struct sc27xx_efuse
*efuse
= context
;
109 u32 buf
, blk_index
= offset
/ SC27XX_EFUSE_BLOCK_WIDTH
;
110 u32 blk_offset
= (offset
% SC27XX_EFUSE_BLOCK_WIDTH
) * BITS_PER_BYTE
;
113 if (blk_index
> SC27XX_EFUSE_BLOCK_MAX
||
114 bytes
> SC27XX_EFUSE_BLOCK_WIDTH
)
117 ret
= sc27xx_efuse_lock(efuse
);
121 /* Enable the efuse controller. */
122 ret
= regmap_update_bits(efuse
->regmap
, SC27XX_MODULE_EN
,
123 SC27XX_EFUSE_EN
, SC27XX_EFUSE_EN
);
128 * Before reading, we should ensure the efuse controller is in
131 ret
= sc27xx_efuse_poll_status(efuse
, SC27XX_EFUSE_STANDBY
);
135 /* Set the block address to be read. */
136 ret
= regmap_write(efuse
->regmap
,
137 efuse
->base
+ SC27XX_EFUSE_BLOCK_INDEX
,
138 blk_index
& SC27XX_EFUSE_BLOCK_MASK
);
142 /* Start reading process from efuse memory. */
143 ret
= regmap_update_bits(efuse
->regmap
,
144 efuse
->base
+ SC27XX_EFUSE_MODE_CTRL
,
145 SC27XX_EFUSE_RD_START
,
146 SC27XX_EFUSE_RD_START
);
151 * Polling the read done status to make sure the reading process
152 * is completed, that means the data can be read out now.
154 ret
= sc27xx_efuse_poll_status(efuse
, SC27XX_EFUSE_RD_DONE
);
158 /* Read data from efuse memory. */
159 ret
= regmap_read(efuse
->regmap
, efuse
->base
+ SC27XX_EFUSE_DATA_RD
,
164 /* Clear the read done flag. */
165 ret
= regmap_update_bits(efuse
->regmap
,
166 efuse
->base
+ SC27XX_EFUSE_MODE_CTRL
,
167 SC27XX_EFUSE_CLR_RDDONE
,
168 SC27XX_EFUSE_CLR_RDDONE
);
171 /* Disable the efuse controller after reading. */
172 regmap_update_bits(efuse
->regmap
, SC27XX_MODULE_EN
, SC27XX_EFUSE_EN
, 0);
174 sc27xx_efuse_unlock(efuse
);
178 memcpy(val
, &buf
, bytes
);
184 static int sc27xx_efuse_probe(struct platform_device
*pdev
)
186 struct device_node
*np
= pdev
->dev
.of_node
;
187 struct nvmem_config econfig
= { };
188 struct nvmem_device
*nvmem
;
189 struct sc27xx_efuse
*efuse
;
192 efuse
= devm_kzalloc(&pdev
->dev
, sizeof(*efuse
), GFP_KERNEL
);
196 efuse
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
197 if (!efuse
->regmap
) {
198 dev_err(&pdev
->dev
, "failed to get efuse regmap\n");
202 ret
= of_property_read_u32(np
, "reg", &efuse
->base
);
204 dev_err(&pdev
->dev
, "failed to get efuse base address\n");
208 ret
= of_hwspin_lock_get_id(np
, 0);
210 dev_err(&pdev
->dev
, "failed to get hwspinlock id\n");
214 efuse
->hwlock
= hwspin_lock_request_specific(ret
);
215 if (!efuse
->hwlock
) {
216 dev_err(&pdev
->dev
, "failed to request hwspinlock\n");
220 mutex_init(&efuse
->mutex
);
221 efuse
->dev
= &pdev
->dev
;
222 platform_set_drvdata(pdev
, efuse
);
225 econfig
.word_size
= 1;
226 econfig
.read_only
= true;
227 econfig
.name
= "sc27xx-efuse";
228 econfig
.size
= SC27XX_EFUSE_BLOCK_MAX
* SC27XX_EFUSE_BLOCK_WIDTH
;
229 econfig
.reg_read
= sc27xx_efuse_read
;
230 econfig
.priv
= efuse
;
231 econfig
.dev
= &pdev
->dev
;
232 nvmem
= devm_nvmem_register(&pdev
->dev
, &econfig
);
234 dev_err(&pdev
->dev
, "failed to register nvmem config\n");
235 hwspin_lock_free(efuse
->hwlock
);
236 return PTR_ERR(nvmem
);
242 static int sc27xx_efuse_remove(struct platform_device
*pdev
)
244 struct sc27xx_efuse
*efuse
= platform_get_drvdata(pdev
);
246 hwspin_lock_free(efuse
->hwlock
);
250 static const struct of_device_id sc27xx_efuse_of_match
[] = {
251 { .compatible
= "sprd,sc2731-efuse" },
255 static struct platform_driver sc27xx_efuse_driver
= {
256 .probe
= sc27xx_efuse_probe
,
257 .remove
= sc27xx_efuse_remove
,
259 .name
= "sc27xx-efuse",
260 .of_match_table
= sc27xx_efuse_of_match
,
264 module_platform_driver(sc27xx_efuse_driver
);
266 MODULE_AUTHOR("Freeman Liu <freeman.liu@spreadtrum.com>");
267 MODULE_DESCRIPTION("Spreadtrum SC27xx efuse driver");
268 MODULE_LICENSE("GPL v2");