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