xref: /openbmc/linux/drivers/nvmem/meson-efuse.c (revision 3a35093a)
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 
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 
22 	return meson_sm_call_read(fw, (u8 *)val, bytes, SM_EFUSE_READ, offset,
23 				  bytes, 0, 0, 0);
24 }
25 
26 static int meson_efuse_write(void *context, unsigned int offset,
27 			     void *val, size_t bytes)
28 {
29 	struct meson_sm_firmware *fw = context;
30 
31 	return meson_sm_call_write(fw, (u8 *)val, bytes, SM_EFUSE_WRITE, offset,
32 				   bytes, 0, 0, 0);
33 }
34 
35 static const struct of_device_id meson_efuse_match[] = {
36 	{ .compatible = "amlogic,meson-gxbb-efuse", },
37 	{ /* sentinel */ },
38 };
39 MODULE_DEVICE_TABLE(of, meson_efuse_match);
40 
41 static int meson_efuse_probe(struct platform_device *pdev)
42 {
43 	struct device *dev = &pdev->dev;
44 	struct meson_sm_firmware *fw;
45 	struct device_node *sm_np;
46 	struct nvmem_device *nvmem;
47 	struct nvmem_config *econfig;
48 	struct clk *clk;
49 	unsigned int size;
50 	int ret;
51 
52 	sm_np = of_parse_phandle(pdev->dev.of_node, "secure-monitor", 0);
53 	if (!sm_np) {
54 		dev_err(&pdev->dev, "no secure-monitor node\n");
55 		return -ENODEV;
56 	}
57 
58 	fw = meson_sm_get(sm_np);
59 	of_node_put(sm_np);
60 	if (!fw)
61 		return -EPROBE_DEFER;
62 
63 	clk = devm_clk_get(dev, NULL);
64 	if (IS_ERR(clk)) {
65 		ret = PTR_ERR(clk);
66 		if (ret != -EPROBE_DEFER)
67 			dev_err(dev, "failed to get efuse gate");
68 		return ret;
69 	}
70 
71 	ret = clk_prepare_enable(clk);
72 	if (ret) {
73 		dev_err(dev, "failed to enable gate");
74 		return ret;
75 	}
76 
77 	ret = devm_add_action_or_reset(dev,
78 				       (void(*)(void *))clk_disable_unprepare,
79 				       clk);
80 	if (ret) {
81 		dev_err(dev, "failed to add disable callback");
82 		return ret;
83 	}
84 
85 	if (meson_sm_call(fw, SM_EFUSE_USER_MAX, &size, 0, 0, 0, 0, 0) < 0) {
86 		dev_err(dev, "failed to get max user");
87 		return -EINVAL;
88 	}
89 
90 	econfig = devm_kzalloc(dev, sizeof(*econfig), GFP_KERNEL);
91 	if (!econfig)
92 		return -ENOMEM;
93 
94 	econfig->dev = dev;
95 	econfig->name = dev_name(dev);
96 	econfig->stride = 1;
97 	econfig->word_size = 1;
98 	econfig->reg_read = meson_efuse_read;
99 	econfig->reg_write = meson_efuse_write;
100 	econfig->size = size;
101 	econfig->priv = fw;
102 
103 	nvmem = devm_nvmem_register(&pdev->dev, econfig);
104 
105 	return PTR_ERR_OR_ZERO(nvmem);
106 }
107 
108 static struct platform_driver meson_efuse_driver = {
109 	.probe = meson_efuse_probe,
110 	.driver = {
111 		.name = "meson-efuse",
112 		.of_match_table = meson_efuse_match,
113 	},
114 };
115 
116 module_platform_driver(meson_efuse_driver);
117 
118 MODULE_AUTHOR("Carlo Caione <carlo@endlessm.com>");
119 MODULE_DESCRIPTION("Amlogic Meson GX NVMEM driver");
120 MODULE_LICENSE("GPL v2");
121