lpc18xx_otp.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/platform_device.h>
  17. #include <linux/slab.h>
  18. /*
  19. * LPC18xx OTP memory contains 4 banks with 4 32-bit words. Bank 0 starts
  20. * at offset 0 from the base.
  21. *
  22. * Bank 0 contains the part ID for Flashless devices and is reseverd for
  23. * devices with Flash.
  24. * Bank 1/2 is generale purpose or AES key storage for secure devices.
  25. * Bank 3 contains control data, USB ID and generale purpose words.
  26. */
  27. #define LPC18XX_OTP_NUM_BANKS 4
  28. #define LPC18XX_OTP_WORDS_PER_BANK 4
  29. #define LPC18XX_OTP_WORD_SIZE sizeof(u32)
  30. #define LPC18XX_OTP_SIZE (LPC18XX_OTP_NUM_BANKS * \
  31. LPC18XX_OTP_WORDS_PER_BANK * \
  32. LPC18XX_OTP_WORD_SIZE)
  33. struct lpc18xx_otp {
  34. void __iomem *base;
  35. };
  36. static int lpc18xx_otp_read(void *context, unsigned int offset,
  37. void *val, size_t bytes)
  38. {
  39. struct lpc18xx_otp *otp = context;
  40. unsigned int count = bytes >> 2;
  41. u32 index = offset >> 2;
  42. u32 *buf = val;
  43. int i;
  44. if (count > (LPC18XX_OTP_SIZE - index))
  45. count = LPC18XX_OTP_SIZE - index;
  46. for (i = index; i < (index + count); i++)
  47. *buf++ = readl(otp->base + i * LPC18XX_OTP_WORD_SIZE);
  48. return 0;
  49. }
  50. static struct nvmem_config lpc18xx_otp_nvmem_config = {
  51. .name = "lpc18xx-otp",
  52. .read_only = true,
  53. .word_size = LPC18XX_OTP_WORD_SIZE,
  54. .stride = LPC18XX_OTP_WORD_SIZE,
  55. .reg_read = lpc18xx_otp_read,
  56. };
  57. static int lpc18xx_otp_probe(struct platform_device *pdev)
  58. {
  59. struct nvmem_device *nvmem;
  60. struct lpc18xx_otp *otp;
  61. otp = devm_kzalloc(&pdev->dev, sizeof(*otp), GFP_KERNEL);
  62. if (!otp)
  63. return -ENOMEM;
  64. otp->base = devm_platform_ioremap_resource(pdev, 0);
  65. if (IS_ERR(otp->base))
  66. return PTR_ERR(otp->base);
  67. lpc18xx_otp_nvmem_config.size = LPC18XX_OTP_SIZE;
  68. lpc18xx_otp_nvmem_config.dev = &pdev->dev;
  69. lpc18xx_otp_nvmem_config.priv = otp;
  70. nvmem = devm_nvmem_register(&pdev->dev, &lpc18xx_otp_nvmem_config);
  71. return PTR_ERR_OR_ZERO(nvmem);
  72. }
  73. static const struct of_device_id lpc18xx_otp_dt_ids[] = {
  74. { .compatible = "nxp,lpc1850-otp" },
  75. { },
  76. };
  77. MODULE_DEVICE_TABLE(of, lpc18xx_otp_dt_ids);
  78. static struct platform_driver lpc18xx_otp_driver = {
  79. .probe = lpc18xx_otp_probe,
  80. .driver = {
  81. .name = "lpc18xx_otp",
  82. .of_match_table = lpc18xx_otp_dt_ids,
  83. },
  84. };
  85. module_platform_driver(lpc18xx_otp_driver);
  86. MODULE_AUTHOR("Joachim Eastwoood <manabian@gmail.com>");
  87. MODULE_DESCRIPTION("NXP LPC18xx OTP NVMEM driver");
  88. MODULE_LICENSE("GPL v2");