s32g-ocotp-nvmem.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2023-2025 NXP
  4. */
  5. #include <linux/device.h>
  6. #include <linux/io.h>
  7. #include <linux/module.h>
  8. #include <linux/nvmem-provider.h>
  9. #include <linux/of.h>
  10. #include <linux/of_device.h>
  11. #include <linux/platform_device.h>
  12. struct s32g_ocotp_priv {
  13. struct device *dev;
  14. void __iomem *base;
  15. };
  16. static int s32g_ocotp_read(void *context, unsigned int offset,
  17. void *val, size_t bytes)
  18. {
  19. struct s32g_ocotp_priv *s32g_data = context;
  20. u32 *dst = val;
  21. while (bytes >= sizeof(u32)) {
  22. *dst++ = ioread32(s32g_data->base + offset);
  23. bytes -= sizeof(u32);
  24. offset += sizeof(u32);
  25. }
  26. return 0;
  27. }
  28. static struct nvmem_keepout s32g_keepouts[] = {
  29. { .start = 0, .end = 520 },
  30. { .start = 540, .end = 564 },
  31. { .start = 596, .end = 664 },
  32. { .start = 668, .end = 676 },
  33. { .start = 684, .end = 732 },
  34. { .start = 744, .end = 864 },
  35. { .start = 908, .end = 924 },
  36. { .start = 928, .end = 936 },
  37. { .start = 948, .end = 964 },
  38. { .start = 968, .end = 976 },
  39. { .start = 984, .end = 1012 },
  40. };
  41. static struct nvmem_config s32g_ocotp_nvmem_config = {
  42. .name = "s32g-ocotp",
  43. .add_legacy_fixed_of_cells = true,
  44. .read_only = true,
  45. .word_size = 4,
  46. .reg_read = s32g_ocotp_read,
  47. .keepout = s32g_keepouts,
  48. .nkeepout = ARRAY_SIZE(s32g_keepouts),
  49. };
  50. static const struct of_device_id ocotp_of_match[] = {
  51. { .compatible = "nxp,s32g2-ocotp" },
  52. { /* sentinel */ }
  53. };
  54. static int s32g_ocotp_probe(struct platform_device *pdev)
  55. {
  56. struct s32g_ocotp_priv *s32g_data;
  57. struct device *dev = &pdev->dev;
  58. struct nvmem_device *nvmem;
  59. struct resource *res;
  60. s32g_data = devm_kzalloc(dev, sizeof(*s32g_data), GFP_KERNEL);
  61. if (!s32g_data)
  62. return -ENOMEM;
  63. s32g_data->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  64. if (IS_ERR(s32g_data->base))
  65. return dev_err_probe(dev, PTR_ERR(s32g_data->base),
  66. "Cannot map OCOTP device.\n");
  67. s32g_data->dev = dev;
  68. s32g_ocotp_nvmem_config.dev = dev;
  69. s32g_ocotp_nvmem_config.priv = s32g_data;
  70. s32g_ocotp_nvmem_config.size = resource_size(res);
  71. nvmem = devm_nvmem_register(dev, &s32g_ocotp_nvmem_config);
  72. return PTR_ERR_OR_ZERO(nvmem);
  73. }
  74. static struct platform_driver s32g_ocotp_driver = {
  75. .probe = s32g_ocotp_probe,
  76. .driver = {
  77. .name = "s32g-ocotp",
  78. .of_match_table = ocotp_of_match,
  79. },
  80. };
  81. module_platform_driver(s32g_ocotp_driver);
  82. MODULE_AUTHOR("NXP");
  83. MODULE_DESCRIPTION("S32G OCOTP driver");
  84. MODULE_LICENSE("GPL");