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/platform_device.h>
18 #include <linux/sizes.h>
19 #include <linux/slab.h>
21 #define MESON_MX_EFUSE_CNTL1 0x04
22 #define MESON_MX_EFUSE_CNTL1_PD_ENABLE BIT(27)
23 #define MESON_MX_EFUSE_CNTL1_AUTO_RD_BUSY BIT(26)
24 #define MESON_MX_EFUSE_CNTL1_AUTO_RD_START BIT(25)
25 #define MESON_MX_EFUSE_CNTL1_AUTO_RD_ENABLE BIT(24)
26 #define MESON_MX_EFUSE_CNTL1_BYTE_WR_DATA GENMASK(23, 16)
27 #define MESON_MX_EFUSE_CNTL1_AUTO_WR_BUSY BIT(14)
28 #define MESON_MX_EFUSE_CNTL1_AUTO_WR_START BIT(13)
29 #define MESON_MX_EFUSE_CNTL1_AUTO_WR_ENABLE BIT(12)
30 #define MESON_MX_EFUSE_CNTL1_BYTE_ADDR_SET BIT(11)
31 #define MESON_MX_EFUSE_CNTL1_BYTE_ADDR_MASK GENMASK(10, 0)
33 #define MESON_MX_EFUSE_CNTL2 0x08
35 #define MESON_MX_EFUSE_CNTL4 0x10
36 #define MESON_MX_EFUSE_CNTL4_ENCRYPT_ENABLE BIT(10)
38 struct meson_mx_efuse_platform_data
{
40 unsigned int word_size
;
43 struct meson_mx_efuse
{
46 struct nvmem_config config
;
49 static void meson_mx_efuse_mask_bits(struct meson_mx_efuse
*efuse
, u32 reg
,
54 data
= readl(efuse
->base
+ reg
);
58 writel(data
, efuse
->base
+ reg
);
61 static int meson_mx_efuse_hw_enable(struct meson_mx_efuse
*efuse
)
65 err
= clk_prepare_enable(efuse
->core_clk
);
69 /* power up the efuse */
70 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
71 MESON_MX_EFUSE_CNTL1_PD_ENABLE
, 0);
73 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL4
,
74 MESON_MX_EFUSE_CNTL4_ENCRYPT_ENABLE
, 0);
79 static void meson_mx_efuse_hw_disable(struct meson_mx_efuse
*efuse
)
81 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
82 MESON_MX_EFUSE_CNTL1_PD_ENABLE
,
83 MESON_MX_EFUSE_CNTL1_PD_ENABLE
);
85 clk_disable_unprepare(efuse
->core_clk
);
88 static int meson_mx_efuse_read_addr(struct meson_mx_efuse
*efuse
,
89 unsigned int addr
, u32
*value
)
94 /* write the address to read */
95 regval
= FIELD_PREP(MESON_MX_EFUSE_CNTL1_BYTE_ADDR_MASK
, addr
);
96 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
97 MESON_MX_EFUSE_CNTL1_BYTE_ADDR_MASK
, regval
);
99 /* inform the hardware that we changed the address */
100 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
101 MESON_MX_EFUSE_CNTL1_BYTE_ADDR_SET
,
102 MESON_MX_EFUSE_CNTL1_BYTE_ADDR_SET
);
103 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
104 MESON_MX_EFUSE_CNTL1_BYTE_ADDR_SET
, 0);
106 /* start the read process */
107 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
108 MESON_MX_EFUSE_CNTL1_AUTO_RD_START
,
109 MESON_MX_EFUSE_CNTL1_AUTO_RD_START
);
110 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
111 MESON_MX_EFUSE_CNTL1_AUTO_RD_START
, 0);
114 * perform a dummy read to ensure that the HW has the RD_BUSY bit set
115 * when polling for the status below.
117 readl(efuse
->base
+ MESON_MX_EFUSE_CNTL1
);
119 err
= readl_poll_timeout_atomic(efuse
->base
+ MESON_MX_EFUSE_CNTL1
,
121 (!(regval
& MESON_MX_EFUSE_CNTL1_AUTO_RD_BUSY
)),
124 dev_err(efuse
->config
.dev
,
125 "Timeout while reading efuse address %u\n", addr
);
129 *value
= readl(efuse
->base
+ MESON_MX_EFUSE_CNTL2
);
134 static int meson_mx_efuse_read(void *context
, unsigned int offset
,
135 void *buf
, size_t bytes
)
137 struct meson_mx_efuse
*efuse
= context
;
141 err
= meson_mx_efuse_hw_enable(efuse
);
145 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
146 MESON_MX_EFUSE_CNTL1_AUTO_RD_ENABLE
,
147 MESON_MX_EFUSE_CNTL1_AUTO_RD_ENABLE
);
149 for (i
= 0; i
< bytes
; i
+= efuse
->config
.word_size
) {
150 addr
= (offset
+ i
) / efuse
->config
.word_size
;
152 err
= meson_mx_efuse_read_addr(efuse
, addr
, &tmp
);
156 memcpy(buf
+ i
, &tmp
,
157 min_t(size_t, bytes
- i
, efuse
->config
.word_size
));
160 meson_mx_efuse_mask_bits(efuse
, MESON_MX_EFUSE_CNTL1
,
161 MESON_MX_EFUSE_CNTL1_AUTO_RD_ENABLE
, 0);
163 meson_mx_efuse_hw_disable(efuse
);
168 static const struct meson_mx_efuse_platform_data meson6_efuse_data
= {
169 .name
= "meson6-efuse",
173 static const struct meson_mx_efuse_platform_data meson8_efuse_data
= {
174 .name
= "meson8-efuse",
178 static const struct meson_mx_efuse_platform_data meson8b_efuse_data
= {
179 .name
= "meson8b-efuse",
183 static const struct of_device_id meson_mx_efuse_match
[] = {
184 { .compatible
= "amlogic,meson6-efuse", .data
= &meson6_efuse_data
},
185 { .compatible
= "amlogic,meson8-efuse", .data
= &meson8_efuse_data
},
186 { .compatible
= "amlogic,meson8b-efuse", .data
= &meson8b_efuse_data
},
189 MODULE_DEVICE_TABLE(of
, meson_mx_efuse_match
);
191 static int meson_mx_efuse_probe(struct platform_device
*pdev
)
193 const struct meson_mx_efuse_platform_data
*drvdata
;
194 struct meson_mx_efuse
*efuse
;
195 struct nvmem_device
*nvmem
;
197 drvdata
= of_device_get_match_data(&pdev
->dev
);
201 efuse
= devm_kzalloc(&pdev
->dev
, sizeof(*efuse
), GFP_KERNEL
);
205 efuse
->base
= devm_platform_ioremap_resource(pdev
, 0);
206 if (IS_ERR(efuse
->base
))
207 return PTR_ERR(efuse
->base
);
209 efuse
->config
.name
= drvdata
->name
;
210 efuse
->config
.owner
= THIS_MODULE
;
211 efuse
->config
.dev
= &pdev
->dev
;
212 efuse
->config
.priv
= efuse
;
213 efuse
->config
.add_legacy_fixed_of_cells
= true;
214 efuse
->config
.stride
= drvdata
->word_size
;
215 efuse
->config
.word_size
= drvdata
->word_size
;
216 efuse
->config
.size
= SZ_512
;
217 efuse
->config
.read_only
= true;
218 efuse
->config
.reg_read
= meson_mx_efuse_read
;
220 efuse
->core_clk
= devm_clk_get(&pdev
->dev
, "core");
221 if (IS_ERR(efuse
->core_clk
)) {
222 dev_err(&pdev
->dev
, "Failed to get core clock\n");
223 return PTR_ERR(efuse
->core_clk
);
226 nvmem
= devm_nvmem_register(&pdev
->dev
, &efuse
->config
);
228 return PTR_ERR_OR_ZERO(nvmem
);
231 static struct platform_driver meson_mx_efuse_driver
= {
232 .probe
= meson_mx_efuse_probe
,
234 .name
= "meson-mx-efuse",
235 .of_match_table
= meson_mx_efuse_match
,
239 module_platform_driver(meson_mx_efuse_driver
);
241 MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
242 MODULE_DESCRIPTION("Amlogic Meson MX eFuse NVMEM driver");
243 MODULE_LICENSE("GPL v2");