1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Amlogic Meson GX eFuse Driver
4 *
5 * Copyright (c) 2016 Endless Computers, Inc.
6 * Author: Carlo Caione <carlo@endlessm.com>
7 */
8
9 #include <linux/clk.h>
10 #include <linux/module.h>
11 #include <linux/nvmem-provider.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14
15 #include <linux/firmware/meson/meson_sm.h>
16
meson_efuse_read(void * context,unsigned int offset,void * val,size_t bytes)17 static int meson_efuse_read(void *context, unsigned int offset,
18 void *val, size_t bytes)
19 {
20 struct meson_sm_firmware *fw = context;
21 int ret;
22
23 ret = meson_sm_call_read(fw, (u8 *)val, bytes, SM_EFUSE_READ, offset,
24 bytes, 0, 0, 0);
25
26 return ret < 0 ? ret : 0;
27 }
28
meson_efuse_write(void * context,unsigned int offset,void * val,size_t bytes)29 static int meson_efuse_write(void *context, unsigned int offset,
30 void *val, size_t bytes)
31 {
32 struct meson_sm_firmware *fw = context;
33 int ret;
34
35 ret = meson_sm_call_write(fw, (u8 *)val, bytes, SM_EFUSE_WRITE, offset,
36 bytes, 0, 0, 0);
37
38 return ret < 0 ? ret : 0;
39 }
40
41 static const struct of_device_id meson_efuse_match[] = {
42 { .compatible = "amlogic,meson-gxbb-efuse", },
43 { /* sentinel */ },
44 };
45 MODULE_DEVICE_TABLE(of, meson_efuse_match);
46
meson_efuse_probe(struct platform_device * pdev)47 static int meson_efuse_probe(struct platform_device *pdev)
48 {
49 struct device *dev = &pdev->dev;
50 struct meson_sm_firmware *fw;
51 struct nvmem_device *nvmem;
52 struct nvmem_config *econfig;
53 struct clk *clk;
54 unsigned int size;
55 struct device_node *sm_np __free(device_node) =
56 of_parse_phandle(pdev->dev.of_node, "secure-monitor", 0);
57
58 if (!sm_np) {
59 dev_err(&pdev->dev, "no secure-monitor node\n");
60 return -ENODEV;
61 }
62
63 fw = meson_sm_get(sm_np);
64 if (!fw)
65 return -EPROBE_DEFER;
66
67 clk = devm_clk_get_enabled(dev, NULL);
68 if (IS_ERR(clk))
69 return dev_err_probe(dev, PTR_ERR(clk), "failed to get efuse gate");
70
71 if (meson_sm_call(fw, SM_EFUSE_USER_MAX, &size, 0, 0, 0, 0, 0) < 0) {
72 dev_err(dev, "failed to get max user");
73 return -EINVAL;
74 }
75
76 econfig = devm_kzalloc(dev, sizeof(*econfig), GFP_KERNEL);
77 if (!econfig)
78 return -ENOMEM;
79
80 econfig->dev = dev;
81 econfig->name = dev_name(dev);
82 econfig->add_legacy_fixed_of_cells = true;
83 econfig->stride = 1;
84 econfig->word_size = 1;
85 econfig->reg_read = meson_efuse_read;
86 econfig->reg_write = meson_efuse_write;
87 econfig->size = size;
88 econfig->priv = fw;
89
90 nvmem = devm_nvmem_register(&pdev->dev, econfig);
91
92 return PTR_ERR_OR_ZERO(nvmem);
93 }
94
95 static struct platform_driver meson_efuse_driver = {
96 .probe = meson_efuse_probe,
97 .driver = {
98 .name = "meson-efuse",
99 .of_match_table = meson_efuse_match,
100 },
101 };
102
103 module_platform_driver(meson_efuse_driver);
104
105 MODULE_AUTHOR("Carlo Caione <carlo@endlessm.com>");
106 MODULE_DESCRIPTION("Amlogic Meson GX NVMEM driver");
107 MODULE_LICENSE("GPL v2");
108