lpc18xx_otp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * NXP LPC18xx/43xx OTP memory NVMEM driver
  4. *
  5. * Copyright (c) 2016 Joachim Eastwood <manabian@gmail.com>
  6. *
  7. * Based on the imx ocotp driver,
  8. * Copyright (c) 2015 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
  9. *
  10. * TODO: add support for writing OTP register via API in boot ROM.
  11. */
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/nvmem-provider.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. /*
  20. * LPC18xx OTP memory contains 4 banks with 4 32-bit words. Bank 0 starts
  21. * at offset 0 from the base.
  22. *
  23. * Bank 0 contains the part ID for Flashless devices and is reseverd for
  24. * devices with Flash.
  25. * Bank 1/2 is generale purpose or AES key storage for secure devices.
  26. * Bank 3 contains control data, USB ID and generale purpose words.
  27. */
  28. #define LPC18XX_OTP_NUM_BANKS 4
  29. #define LPC18XX_OTP_WORDS_PER_BANK 4
  30. #define LPC18XX_OTP_WORD_SIZE sizeof(u32)
  31. #define LPC18XX_OTP_SIZE (LPC18XX_OTP_NUM_BANKS * \
  32. LPC18XX_OTP_WORDS_PER_BANK * \
  33. LPC18XX_OTP_WORD_SIZE)
  34. struct lpc18xx_otp {
  35. void __iomem *base;
  36. };
  37. static int lpc18xx_otp_read(void *context, unsigned int offset,
  38. void *val, size_t bytes)
  39. {
  40. struct lpc18xx_otp *otp = context;
  41. unsigned int count = bytes >> 2;
  42. u32 index = offset >> 2;
  43. u32 *buf = val;
  44. int i;
  45. if (count > (LPC18XX_OTP_SIZE - index))
  46. count = LPC18XX_OTP_SIZE - index;
  47. for (i = index; i < (index + count); i++)
  48. *buf++ = readl(otp->base + i * LPC18XX_OTP_WORD_SIZE);
  49. return 0;
  50. }
  51. static struct nvmem_config lpc18xx_otp_nvmem_config = {
  52. .name = "lpc18xx-otp",
  53. .read_only = true,
  54. .word_size = LPC18XX_OTP_WORD_SIZE,
  55. .stride = LPC18XX_OTP_WORD_SIZE,
  56. .reg_read = lpc18xx_otp_read,
  57. };
  58. static int lpc18xx_otp_probe(struct platform_device *pdev)
  59. {
  60. struct nvmem_device *nvmem;
  61. struct lpc18xx_otp *otp;
  62. struct resource *res;
  63. otp = devm_kzalloc(&pdev->dev, sizeof(*otp), GFP_KERNEL);
  64. if (!otp)
  65. return -ENOMEM;
  66. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  67. otp->base = devm_ioremap_resource(&pdev->dev, res);
  68. if (IS_ERR(otp->base))
  69. return PTR_ERR(otp->base);
  70. lpc18xx_otp_nvmem_config.size = LPC18XX_OTP_SIZE;
  71. lpc18xx_otp_nvmem_config.dev = &pdev->dev;
  72. lpc18xx_otp_nvmem_config.priv = otp;
  73. nvmem = devm_nvmem_register(&pdev->dev, &lpc18xx_otp_nvmem_config);
  74. return PTR_ERR_OR_ZERO(nvmem);
  75. }
  76. static const struct of_device_id lpc18xx_otp_dt_ids[] = {
  77. { .compatible = "nxp,lpc1850-otp" },
  78. { },
  79. };
  80. MODULE_DEVICE_TABLE(of, lpc18xx_otp_dt_ids);
  81. static struct platform_driver lpc18xx_otp_driver = {
  82. .probe = lpc18xx_otp_probe,
  83. .driver = {
  84. .name = "lpc18xx_otp",
  85. .of_match_table = lpc18xx_otp_dt_ids,
  86. },
  87. };
  88. module_platform_driver(lpc18xx_otp_driver);
  89. MODULE_AUTHOR("Joachim Eastwoood <manabian@gmail.com>");
  90. MODULE_DESCRIPTION("NXP LPC18xx OTP NVMEM driver");
  91. MODULE_LICENSE("GPL v2");