xref: /openbmc/linux/drivers/nvmem/zynqmp_nvmem.c (revision 26e2fe4c)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2019 Xilinx, Inc.
4  */
5 
6 #include <linux/module.h>
7 #include <linux/nvmem-provider.h>
8 #include <linux/of.h>
9 #include <linux/platform_device.h>
10 #include <linux/firmware/xlnx-zynqmp.h>
11 
12 #define SILICON_REVISION_MASK 0xF
13 
14 struct zynqmp_nvmem_data {
15 	struct device *dev;
16 	struct nvmem_device *nvmem;
17 };
18 
zynqmp_nvmem_read(void * context,unsigned int offset,void * val,size_t bytes)19 static int zynqmp_nvmem_read(void *context, unsigned int offset,
20 			     void *val, size_t bytes)
21 {
22 	int ret;
23 	int idcode, version;
24 	struct zynqmp_nvmem_data *priv = context;
25 
26 	ret = zynqmp_pm_get_chipid(&idcode, &version);
27 	if (ret < 0)
28 		return ret;
29 
30 	dev_dbg(priv->dev, "Read chipid val %x %x\n", idcode, version);
31 	*(int *)val = version & SILICON_REVISION_MASK;
32 
33 	return 0;
34 }
35 
36 static struct nvmem_config econfig = {
37 	.name = "zynqmp-nvmem",
38 	.owner = THIS_MODULE,
39 	.word_size = 1,
40 	.size = 1,
41 	.read_only = true,
42 };
43 
44 static const struct of_device_id zynqmp_nvmem_match[] = {
45 	{ .compatible = "xlnx,zynqmp-nvmem-fw", },
46 	{ /* sentinel */ },
47 };
48 MODULE_DEVICE_TABLE(of, zynqmp_nvmem_match);
49 
zynqmp_nvmem_probe(struct platform_device * pdev)50 static int zynqmp_nvmem_probe(struct platform_device *pdev)
51 {
52 	struct device *dev = &pdev->dev;
53 	struct zynqmp_nvmem_data *priv;
54 
55 	priv = devm_kzalloc(dev, sizeof(struct zynqmp_nvmem_data), GFP_KERNEL);
56 	if (!priv)
57 		return -ENOMEM;
58 
59 	priv->dev = dev;
60 	econfig.dev = dev;
61 	econfig.add_legacy_fixed_of_cells = true;
62 	econfig.reg_read = zynqmp_nvmem_read;
63 	econfig.priv = priv;
64 
65 	priv->nvmem = devm_nvmem_register(dev, &econfig);
66 
67 	return PTR_ERR_OR_ZERO(priv->nvmem);
68 }
69 
70 static struct platform_driver zynqmp_nvmem_driver = {
71 	.probe = zynqmp_nvmem_probe,
72 	.driver = {
73 		.name = "zynqmp-nvmem",
74 		.of_match_table = zynqmp_nvmem_match,
75 	},
76 };
77 
78 module_platform_driver(zynqmp_nvmem_driver);
79 
80 MODULE_AUTHOR("Michal Simek <michal.simek@amd.com>, Nava kishore Manne <nava.kishore.manne@amd.com>");
81 MODULE_DESCRIPTION("ZynqMP NVMEM driver");
82 MODULE_LICENSE("GPL");
83