lpc18xx_otp.c 3.2 KB

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