Blame view

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