1 // SPDX-License-Identifier: GPL-2.0-only
3 * Amlogic Meson6, Meson8 and Meson8b eFuse Driver
5 * Copyright (c) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
8 #include <linux/bitfield.h>
9 #include <linux/bitops.h>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
13 #include <linux/iopoll.h>
14 #include <linux/module.h>
15 #include <linux/nvmem-provider.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/sizes.h>
20 #include <linux/slab.h>
22 #define MESON_MX_EFUSE_CNTL1 0x04
23 #define MESON_MX_EFUSE_CNTL1_PD_ENABLE BIT(27)
24 #define MESON_MX_EFUSE_CNTL1_AUTO_RD_BUSY BIT(26)
25 #define MESON_MX_EFUSE_CNTL1_AUTO_RD_START BIT(25)
26 #define MESON_MX_EFUSE_CNTL1_AUTO_RD_ENABLE BIT(24)
27 #define MESON_MX_EFUSE_CNTL1_BYTE_WR_DATA GENMASK(23, 16)
28 #define MESON_MX_EFUSE_CNTL1_AUTO_WR_BUSY BIT(14)
29 #define MESON_MX_EFUSE_CNTL1_AUTO_WR_START BIT(13)
30 #define MESON_MX_EFUSE_CNTL1_AUTO_WR_ENABLE BIT(12)
31 #define MESON_MX_EFUSE_CNTL1_BYTE_ADDR_SET BIT(11)
32 #define MESON_MX_EFUSE_CNTL1_BYTE_ADDR_MASK GENMASK(10, 0)
34 #define MESON_MX_EFUSE_CNTL2 0x08
36 #define MESON_MX_EFUSE_CNTL4 0x10
37 #define MESON_MX_EFUSE_CNTL4_ENCRYPT_ENABLE BIT(10)
39 struct meson_mx_efuse_platform_data
{
41 unsigned int word_size
;
44 struct meson_mx_efuse
{
47 struct nvmem_device
*nvmem
;
48 struct nvmem_config config
;
51 static void meson_mx_efuse_mask_bits(struct meson_mx_efuse
*efuse
, u32 reg
,
56 data
= readl(efuse
->base
+ reg
);
60 writel(data
, efuse
->base
+ reg
);
63 static int meson_mx_efuse_hw_enable(struct meson_mx_efuse
*efuse
)
67 err
= clk_prepare_enable(efuse
->core_clk
);
71 /* power up the efuse */
72 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
73 MESON_MX_EFUSE_CNTL1_PD_ENABLE
, 0);
75 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL4
,
76 MESON_MX_EFUSE_CNTL4_ENCRYPT_ENABLE
, 0);
81 static void meson_mx_efuse_hw_disable(struct meson_mx_efuse
*efuse
)
83 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
84 MESON_MX_EFUSE_CNTL1_PD_ENABLE
,
85 MESON_MX_EFUSE_CNTL1_PD_ENABLE
);
87 clk_disable_unprepare(efuse
->core_clk
);
90 static int meson_mx_efuse_read_addr(struct meson_mx_efuse
*efuse
,
91 unsigned int addr
, u32
*value
)
96 /* write the address to read */
97 regval
= FIELD_PREP(MESON_MX_EFUSE_CNTL1_BYTE_ADDR_MASK
, addr
);
98 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
99 MESON_MX_EFUSE_CNTL1_BYTE_ADDR_MASK
, regval
);
101 /* inform the hardware that we changed the address */
102 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
103 MESON_MX_EFUSE_CNTL1_BYTE_ADDR_SET
,
104 MESON_MX_EFUSE_CNTL1_BYTE_ADDR_SET
);
105 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
106 MESON_MX_EFUSE_CNTL1_BYTE_ADDR_SET
, 0);
108 /* start the read process */
109 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
110 MESON_MX_EFUSE_CNTL1_AUTO_RD_START
,
111 MESON_MX_EFUSE_CNTL1_AUTO_RD_START
);
112 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
113 MESON_MX_EFUSE_CNTL1_AUTO_RD_START
, 0);
116 * perform a dummy read to ensure that the HW has the RD_BUSY bit set
117 * when polling for the status below.
119 readl(efuse
->base
+ MESON_MX_EFUSE_CNTL1
);
121 err
= readl_poll_timeout_atomic(efuse
->base
+ MESON_MX_EFUSE_CNTL1
,
123 (!(regval
& MESON_MX_EFUSE_CNTL1_AUTO_RD_BUSY
)),
126 dev_err(efuse
->config
.dev
,
127 "Timeout while reading efuse address %u\n", addr
);
131 *value
= readl(efuse
->base
+ MESON_MX_EFUSE_CNTL2
);
136 static int meson_mx_efuse_read(void *context
, unsigned int offset
,
137 void *buf
, size_t bytes
)
139 struct meson_mx_efuse
*efuse
= context
;
143 err
= meson_mx_efuse_hw_enable(efuse
);
147 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
148 MESON_MX_EFUSE_CNTL1_AUTO_RD_ENABLE
,
149 MESON_MX_EFUSE_CNTL1_AUTO_RD_ENABLE
);
151 for (i
= 0; i
< bytes
; i
+= efuse
->config
.word_size
) {
152 addr
= (offset
+ i
) / efuse
->config
.word_size
;
154 err
= meson_mx_efuse_read_addr(efuse
, addr
, &tmp
);
158 memcpy(buf
+ i
, &tmp
, efuse
->config
.word_size
);
161 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
162 MESON_MX_EFUSE_CNTL1_AUTO_RD_ENABLE
, 0);
164 meson_mx_efuse_hw_disable(efuse
);
169 static const struct meson_mx_efuse_platform_data meson6_efuse_data
= {
170 .name
= "meson6-efuse",
174 static const struct meson_mx_efuse_platform_data meson8_efuse_data
= {
175 .name
= "meson8-efuse",
179 static const struct meson_mx_efuse_platform_data meson8b_efuse_data
= {
180 .name
= "meson8b-efuse",
184 static const struct of_device_id meson_mx_efuse_match
[] = {
185 { .compatible
= "amlogic,meson6-efuse", .data
= &meson6_efuse_data
},
186 { .compatible
= "amlogic,meson8-efuse", .data
= &meson8_efuse_data
},
187 { .compatible
= "amlogic,meson8b-efuse", .data
= &meson8b_efuse_data
},
190 MODULE_DEVICE_TABLE(of
, meson_mx_efuse_match
);
192 static int meson_mx_efuse_probe(struct platform_device
*pdev
)
194 const struct meson_mx_efuse_platform_data
*drvdata
;
195 struct meson_mx_efuse
*efuse
;
196 struct resource
*res
;
198 drvdata
= of_device_get_match_data(&pdev
->dev
);
202 efuse
= devm_kzalloc(&pdev
->dev
, sizeof(*efuse
), GFP_KERNEL
);
206 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
207 efuse
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
208 if (IS_ERR(efuse
->base
))
209 return PTR_ERR(efuse
->base
);
211 efuse
->config
.name
= devm_kstrdup(&pdev
->dev
, drvdata
->name
,
213 efuse
->config
.owner
= THIS_MODULE
;
214 efuse
->config
.dev
= &pdev
->dev
;
215 efuse
->config
.priv
= efuse
;
216 efuse
->config
.stride
= drvdata
->word_size
;
217 efuse
->config
.word_size
= drvdata
->word_size
;
218 efuse
->config
.size
= SZ_512
;
219 efuse
->config
.read_only
= true;
220 efuse
->config
.reg_read
= meson_mx_efuse_read
;
222 efuse
->core_clk
= devm_clk_get(&pdev
->dev
, "core");
223 if (IS_ERR(efuse
->core_clk
)) {
224 dev_err(&pdev
->dev
, "Failed to get core clock\n");
225 return PTR_ERR(efuse
->core_clk
);
228 efuse
->nvmem
= devm_nvmem_register(&pdev
->dev
, &efuse
->config
);
230 return PTR_ERR_OR_ZERO(efuse
->nvmem
);
233 static struct platform_driver meson_mx_efuse_driver
= {
234 .probe
= meson_mx_efuse_probe
,
236 .name
= "meson-mx-efuse",
237 .of_match_table
= meson_mx_efuse_match
,
241 module_platform_driver(meson_mx_efuse_driver
);
243 MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
244 MODULE_DESCRIPTION("Amlogic Meson MX eFuse NVMEM driver");
245 MODULE_LICENSE("GPL v2");