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