14640fa18SNava kishore Manne // SPDX-License-Identifier: GPL-2.0+
24640fa18SNava kishore Manne /*
34640fa18SNava kishore Manne * Copyright (C) 2019 Xilinx, Inc.
44640fa18SNava kishore Manne */
54640fa18SNava kishore Manne
64640fa18SNava kishore Manne #include <linux/module.h>
74640fa18SNava kishore Manne #include <linux/nvmem-provider.h>
84640fa18SNava kishore Manne #include <linux/of.h>
94640fa18SNava kishore Manne #include <linux/platform_device.h>
104640fa18SNava kishore Manne #include <linux/firmware/xlnx-zynqmp.h>
114640fa18SNava kishore Manne
124640fa18SNava kishore Manne #define SILICON_REVISION_MASK 0xF
134640fa18SNava kishore Manne
144640fa18SNava kishore Manne struct zynqmp_nvmem_data {
154640fa18SNava kishore Manne struct device *dev;
164640fa18SNava kishore Manne struct nvmem_device *nvmem;
174640fa18SNava kishore Manne };
184640fa18SNava kishore Manne
zynqmp_nvmem_read(void * context,unsigned int offset,void * val,size_t bytes)194640fa18SNava kishore Manne static int zynqmp_nvmem_read(void *context, unsigned int offset,
204640fa18SNava kishore Manne void *val, size_t bytes)
214640fa18SNava kishore Manne {
224640fa18SNava kishore Manne int ret;
234640fa18SNava kishore Manne int idcode, version;
244640fa18SNava kishore Manne struct zynqmp_nvmem_data *priv = context;
254640fa18SNava kishore Manne
2621cd93baSRajan Vaja ret = zynqmp_pm_get_chipid(&idcode, &version);
274640fa18SNava kishore Manne if (ret < 0)
284640fa18SNava kishore Manne return ret;
294640fa18SNava kishore Manne
304640fa18SNava kishore Manne dev_dbg(priv->dev, "Read chipid val %x %x\n", idcode, version);
314640fa18SNava kishore Manne *(int *)val = version & SILICON_REVISION_MASK;
324640fa18SNava kishore Manne
334640fa18SNava kishore Manne return 0;
344640fa18SNava kishore Manne }
354640fa18SNava kishore Manne
364640fa18SNava kishore Manne static struct nvmem_config econfig = {
374640fa18SNava kishore Manne .name = "zynqmp-nvmem",
384640fa18SNava kishore Manne .owner = THIS_MODULE,
394640fa18SNava kishore Manne .word_size = 1,
404640fa18SNava kishore Manne .size = 1,
414640fa18SNava kishore Manne .read_only = true,
424640fa18SNava kishore Manne };
434640fa18SNava kishore Manne
444640fa18SNava kishore Manne static const struct of_device_id zynqmp_nvmem_match[] = {
454640fa18SNava kishore Manne { .compatible = "xlnx,zynqmp-nvmem-fw", },
464640fa18SNava kishore Manne { /* sentinel */ },
474640fa18SNava kishore Manne };
484640fa18SNava kishore Manne MODULE_DEVICE_TABLE(of, zynqmp_nvmem_match);
494640fa18SNava kishore Manne
zynqmp_nvmem_probe(struct platform_device * pdev)504640fa18SNava kishore Manne static int zynqmp_nvmem_probe(struct platform_device *pdev)
514640fa18SNava kishore Manne {
524640fa18SNava kishore Manne struct device *dev = &pdev->dev;
534640fa18SNava kishore Manne struct zynqmp_nvmem_data *priv;
544640fa18SNava kishore Manne
554640fa18SNava kishore Manne priv = devm_kzalloc(dev, sizeof(struct zynqmp_nvmem_data), GFP_KERNEL);
564640fa18SNava kishore Manne if (!priv)
574640fa18SNava kishore Manne return -ENOMEM;
584640fa18SNava kishore Manne
594640fa18SNava kishore Manne priv->dev = dev;
604640fa18SNava kishore Manne econfig.dev = dev;
61*26e2fe4cSRafał Miłecki econfig.add_legacy_fixed_of_cells = true;
624640fa18SNava kishore Manne econfig.reg_read = zynqmp_nvmem_read;
634640fa18SNava kishore Manne econfig.priv = priv;
644640fa18SNava kishore Manne
654640fa18SNava kishore Manne priv->nvmem = devm_nvmem_register(dev, &econfig);
664640fa18SNava kishore Manne
674640fa18SNava kishore Manne return PTR_ERR_OR_ZERO(priv->nvmem);
684640fa18SNava kishore Manne }
694640fa18SNava kishore Manne
704640fa18SNava kishore Manne static struct platform_driver zynqmp_nvmem_driver = {
714640fa18SNava kishore Manne .probe = zynqmp_nvmem_probe,
724640fa18SNava kishore Manne .driver = {
734640fa18SNava kishore Manne .name = "zynqmp-nvmem",
744640fa18SNava kishore Manne .of_match_table = zynqmp_nvmem_match,
754640fa18SNava kishore Manne },
764640fa18SNava kishore Manne };
774640fa18SNava kishore Manne
784640fa18SNava kishore Manne module_platform_driver(zynqmp_nvmem_driver);
794640fa18SNava kishore Manne
8097344089SMichal Simek MODULE_AUTHOR("Michal Simek <michal.simek@amd.com>, Nava kishore Manne <nava.kishore.manne@amd.com>");
814640fa18SNava kishore Manne MODULE_DESCRIPTION("ZynqMP NVMEM driver");
824640fa18SNava kishore Manne MODULE_LICENSE("GPL");
83