sec-qfprom.c 2.2 KB

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