1 // SPDX-License-Identifier: GPL-2.0-only
3 * NXP LPC18xx/43xx OTP memory NVMEM driver
5 * Copyright (c) 2016 Joachim Eastwood <manabian@gmail.com>
7 * Based on the imx ocotp driver,
8 * Copyright (c) 2015 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
10 * TODO: add support for writing OTP register via API in boot ROM.
14 #include <linux/module.h>
15 #include <linux/nvmem-provider.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
21 * LPC18xx OTP memory contains 4 banks with 4 32-bit words. Bank 0 starts
22 * at offset 0 from the base.
24 * Bank 0 contains the part ID for Flashless devices and is reseverd for
26 * Bank 1/2 is generale purpose or AES key storage for secure devices.
27 * Bank 3 contains control data, USB ID and generale purpose words.
29 #define LPC18XX_OTP_NUM_BANKS 4
30 #define LPC18XX_OTP_WORDS_PER_BANK 4
31 #define LPC18XX_OTP_WORD_SIZE sizeof(u32)
32 #define LPC18XX_OTP_SIZE (LPC18XX_OTP_NUM_BANKS * \
33 LPC18XX_OTP_WORDS_PER_BANK * \
34 LPC18XX_OTP_WORD_SIZE)
40 static int lpc18xx_otp_read(void *context, unsigned int offset,
41 void *val, size_t bytes)
43 struct lpc18xx_otp *otp = context;
44 unsigned int count = bytes >> 2;
45 u32 index = offset >> 2;
49 if (count > (LPC18XX_OTP_SIZE - index))
50 count = LPC18XX_OTP_SIZE - index;
52 for (i = index; i < (index + count); i++)
53 *buf++ = readl(otp->base + i * LPC18XX_OTP_WORD_SIZE);
58 static struct nvmem_config lpc18xx_otp_nvmem_config = {
59 .name = "lpc18xx-otp",
61 .word_size = LPC18XX_OTP_WORD_SIZE,
62 .stride = LPC18XX_OTP_WORD_SIZE,
63 .reg_read = lpc18xx_otp_read,
66 static int lpc18xx_otp_probe(struct platform_device *pdev)
68 struct nvmem_device *nvmem;
69 struct lpc18xx_otp *otp;
71 otp = devm_kzalloc(&pdev->dev, sizeof(*otp), GFP_KERNEL);
75 otp->base = devm_platform_ioremap_resource(pdev, 0);
76 if (IS_ERR(otp->base))
77 return PTR_ERR(otp->base);
79 lpc18xx_otp_nvmem_config.size = LPC18XX_OTP_SIZE;
80 lpc18xx_otp_nvmem_config.dev = &pdev->dev;
81 lpc18xx_otp_nvmem_config.priv = otp;
83 nvmem = devm_nvmem_register(&pdev->dev, &lpc18xx_otp_nvmem_config);
85 return PTR_ERR_OR_ZERO(nvmem);
88 static const struct of_device_id lpc18xx_otp_dt_ids[] = {
89 { .compatible = "nxp,lpc1850-otp" },
92 MODULE_DEVICE_TABLE(of, lpc18xx_otp_dt_ids);
94 static struct platform_driver lpc18xx_otp_driver = {
95 .probe = lpc18xx_otp_probe,
97 .name = "lpc18xx_otp",
98 .of_match_table = lpc18xx_otp_dt_ids,
101 module_platform_driver(lpc18xx_otp_driver);
103 MODULE_AUTHOR("Joachim Eastwoood <manabian@gmail.com>");
104 MODULE_DESCRIPTION("NXP LPC18xx OTP NVMEM driver");
105 MODULE_LICENSE("GPL v2");