xref: /linux/drivers/nvmem/sec-qfprom.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
1c471245bSKomal Bajaj // SPDX-License-Identifier: GPL-2.0-only
2c471245bSKomal Bajaj /*
3c471245bSKomal Bajaj  * Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved.
4c471245bSKomal Bajaj  */
5c471245bSKomal Bajaj 
6c471245bSKomal Bajaj #include <linux/firmware/qcom/qcom_scm.h>
7c471245bSKomal Bajaj #include <linux/mod_devicetable.h>
8c471245bSKomal Bajaj #include <linux/nvmem-provider.h>
9c471245bSKomal Bajaj #include <linux/platform_device.h>
10c471245bSKomal Bajaj #include <linux/pm_runtime.h>
11c471245bSKomal Bajaj 
12c471245bSKomal Bajaj /**
13c471245bSKomal Bajaj  * struct sec_qfprom - structure holding secure qfprom attributes
14c471245bSKomal Bajaj  *
15c471245bSKomal Bajaj  * @base: starting physical address for secure qfprom corrected address space.
16c471245bSKomal Bajaj  * @dev: qfprom device structure.
17c471245bSKomal Bajaj  */
18c471245bSKomal Bajaj struct sec_qfprom {
19c471245bSKomal Bajaj 	phys_addr_t base;
20c471245bSKomal Bajaj 	struct device *dev;
21c471245bSKomal Bajaj };
22c471245bSKomal Bajaj 
sec_qfprom_reg_read(void * context,unsigned int reg,void * _val,size_t bytes)23c471245bSKomal Bajaj static int sec_qfprom_reg_read(void *context, unsigned int reg, void *_val, size_t bytes)
24c471245bSKomal Bajaj {
25c471245bSKomal Bajaj 	struct sec_qfprom *priv = context;
26c471245bSKomal Bajaj 	unsigned int i;
27c471245bSKomal Bajaj 	u8 *val = _val;
28c471245bSKomal Bajaj 	u32 read_val;
29c471245bSKomal Bajaj 	u8 *tmp;
30c471245bSKomal Bajaj 
31c471245bSKomal Bajaj 	for (i = 0; i < bytes; i++, reg++) {
32c471245bSKomal Bajaj 		if (i == 0 || reg % 4 == 0) {
33c471245bSKomal Bajaj 			if (qcom_scm_io_readl(priv->base + (reg & ~3), &read_val)) {
34c471245bSKomal Bajaj 				dev_err(priv->dev, "Couldn't access fuse register\n");
35c471245bSKomal Bajaj 				return -EINVAL;
36c471245bSKomal Bajaj 			}
37c471245bSKomal Bajaj 			tmp = (u8 *)&read_val;
38c471245bSKomal Bajaj 		}
39c471245bSKomal Bajaj 
40c471245bSKomal Bajaj 		val[i] = tmp[reg & 3];
41c471245bSKomal Bajaj 	}
42c471245bSKomal Bajaj 
43c471245bSKomal Bajaj 	return 0;
44c471245bSKomal Bajaj }
45c471245bSKomal Bajaj 
sec_qfprom_probe(struct platform_device * pdev)46c471245bSKomal Bajaj static int sec_qfprom_probe(struct platform_device *pdev)
47c471245bSKomal Bajaj {
48c471245bSKomal Bajaj 	struct nvmem_config econfig = {
49c471245bSKomal Bajaj 		.name = "sec-qfprom",
50*2cc3b37fSRafał Miłecki 		.add_legacy_fixed_of_cells = true,
51c471245bSKomal Bajaj 		.stride = 1,
52c471245bSKomal Bajaj 		.word_size = 1,
53c471245bSKomal Bajaj 		.id = NVMEM_DEVID_AUTO,
54c471245bSKomal Bajaj 		.reg_read = sec_qfprom_reg_read,
55c471245bSKomal Bajaj 	};
56c471245bSKomal Bajaj 	struct device *dev = &pdev->dev;
57c471245bSKomal Bajaj 	struct nvmem_device *nvmem;
58c471245bSKomal Bajaj 	struct sec_qfprom *priv;
59c471245bSKomal Bajaj 	struct resource *res;
60c471245bSKomal Bajaj 
61c471245bSKomal Bajaj 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
62c471245bSKomal Bajaj 	if (!priv)
63c471245bSKomal Bajaj 		return -ENOMEM;
64c471245bSKomal Bajaj 
65c471245bSKomal Bajaj 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
66c471245bSKomal Bajaj 	if (!res)
67c471245bSKomal Bajaj 		return -EINVAL;
68c471245bSKomal Bajaj 
69c471245bSKomal Bajaj 	priv->base = res->start;
70c471245bSKomal Bajaj 
71c471245bSKomal Bajaj 	econfig.size = resource_size(res);
72c471245bSKomal Bajaj 	econfig.dev = dev;
73c471245bSKomal Bajaj 	econfig.priv = priv;
74c471245bSKomal Bajaj 
75c471245bSKomal Bajaj 	priv->dev = dev;
76c471245bSKomal Bajaj 
77c471245bSKomal Bajaj 	nvmem = devm_nvmem_register(dev, &econfig);
78c471245bSKomal Bajaj 
79c471245bSKomal Bajaj 	return PTR_ERR_OR_ZERO(nvmem);
80c471245bSKomal Bajaj }
81c471245bSKomal Bajaj 
82c471245bSKomal Bajaj static const struct of_device_id sec_qfprom_of_match[] = {
83c471245bSKomal Bajaj 	{ .compatible = "qcom,sec-qfprom" },
84c471245bSKomal Bajaj 	{/* sentinel */},
85c471245bSKomal Bajaj };
86c471245bSKomal Bajaj MODULE_DEVICE_TABLE(of, sec_qfprom_of_match);
87c471245bSKomal Bajaj 
88c471245bSKomal Bajaj static struct platform_driver qfprom_driver = {
89c471245bSKomal Bajaj 	.probe = sec_qfprom_probe,
90c471245bSKomal Bajaj 	.driver = {
91c471245bSKomal Bajaj 		.name = "qcom_sec_qfprom",
92c471245bSKomal Bajaj 		.of_match_table = sec_qfprom_of_match,
93c471245bSKomal Bajaj 	},
94c471245bSKomal Bajaj };
95c471245bSKomal Bajaj module_platform_driver(qfprom_driver);
96c471245bSKomal Bajaj MODULE_DESCRIPTION("Qualcomm Secure QFPROM driver");
97c471245bSKomal Bajaj MODULE_LICENSE("GPL");
98