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 device_node *sm_np;
52 struct nvmem_device *nvmem;
53 struct nvmem_config *econfig;
54 struct clk *clk;
55 unsigned int size;
56
57 sm_np = of_parse_phandle(pdev->dev.of_node, "secure-monitor", 0);
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 of_node_put(sm_np);
65 if (!fw)
66 return -EPROBE_DEFER;
67
68 clk = devm_clk_get_enabled(dev, NULL);
69 if (IS_ERR(clk))
70 return dev_err_probe(dev, PTR_ERR(clk), "failed to get efuse gate");
71
72 if (meson_sm_call(fw, SM_EFUSE_USER_MAX, &size, 0, 0, 0, 0, 0) < 0) {
73 dev_err(dev, "failed to get max user");
74 return -EINVAL;
75 }
76
77 econfig = devm_kzalloc(dev, sizeof(*econfig), GFP_KERNEL);
78 if (!econfig)
79 return -ENOMEM;
80
81 econfig->dev = dev;
82 econfig->name = dev_name(dev);
83 econfig->add_legacy_fixed_of_cells = true;
84 econfig->stride = 1;
85 econfig->word_size = 1;
86 econfig->reg_read = meson_efuse_read;
87 econfig->reg_write = meson_efuse_write;
88 econfig->size = size;
89 econfig->priv = fw;
90
91 nvmem = devm_nvmem_register(&pdev->dev, econfig);
92
93 return PTR_ERR_OR_ZERO(nvmem);
94 }
95
96 static struct platform_driver meson_efuse_driver = {
97 .probe = meson_efuse_probe,
98 .driver = {
99 .name = "meson-efuse",
100 .of_match_table = meson_efuse_match,
101 },
102 };
103
104 module_platform_driver(meson_efuse_driver);
105
106 MODULE_AUTHOR("Carlo Caione <carlo@endlessm.com>");
107 MODULE_DESCRIPTION("Amlogic Meson GX NVMEM driver");
108 MODULE_LICENSE("GPL v2");
109