811-v6.1-0008-nvmem-lan9662-otp-add-support.patch 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. From 9e8f208ad5229ddda97cd4a83ecf89c735d99592 Mon Sep 17 00:00:00 2001
  2. From: Horatiu Vultur <[email protected]>
  3. Date: Fri, 16 Sep 2022 13:20:59 +0100
  4. Subject: [PATCH] nvmem: lan9662-otp: add support
  5. Add support for OTP controller available on LAN9662. The OTPC controls
  6. the access to a non-volatile memory. The size of the memory is 8KB.
  7. The OTPC can access the memory based on an offset.
  8. Implement both the read and the write functionality.
  9. Signed-off-by: Horatiu Vultur <[email protected]>
  10. Signed-off-by: Srinivas Kandagatla <[email protected]>
  11. Link: https://lore.kernel.org/r/[email protected]
  12. Signed-off-by: Greg Kroah-Hartman <[email protected]>
  13. ---
  14. drivers/nvmem/Kconfig | 8 ++
  15. drivers/nvmem/Makefile | 2 +
  16. drivers/nvmem/lan9662-otpc.c | 222 +++++++++++++++++++++++++++++++++++
  17. 3 files changed, 232 insertions(+)
  18. create mode 100644 drivers/nvmem/lan9662-otpc.c
  19. --- a/drivers/nvmem/Kconfig
  20. +++ b/drivers/nvmem/Kconfig
  21. @@ -98,6 +98,14 @@ config NVMEM_JZ4780_EFUSE
  22. To compile this driver as a module, choose M here: the module
  23. will be called nvmem_jz4780_efuse.
  24. +config NVMEM_LAN9662_OTPC
  25. + tristate "Microchip LAN9662 OTP controller support"
  26. + depends on SOC_LAN966 || COMPILE_TEST
  27. + depends on HAS_IOMEM
  28. + help
  29. + This driver enables the OTP controller available on Microchip LAN9662
  30. + SoCs. It controls the access to the OTP memory connected to it.
  31. +
  32. config NVMEM_LAYERSCAPE_SFP
  33. tristate "Layerscape SFP (Security Fuse Processor) support"
  34. depends on ARCH_LAYERSCAPE || COMPILE_TEST
  35. --- a/drivers/nvmem/Makefile
  36. +++ b/drivers/nvmem/Makefile
  37. @@ -21,6 +21,8 @@ obj-$(CONFIG_NVMEM_IMX_OCOTP_SCU) += nvm
  38. nvmem-imx-ocotp-scu-y := imx-ocotp-scu.o
  39. obj-$(CONFIG_NVMEM_JZ4780_EFUSE) += nvmem_jz4780_efuse.o
  40. nvmem_jz4780_efuse-y := jz4780-efuse.o
  41. +obj-$(CONFIG_NVMEM_LAN9662_OTPC) += nvmem-lan9662-otpc.o
  42. +nvmem-lan9662-otpc-y := lan9662-otpc.o
  43. obj-$(CONFIG_NVMEM_LAYERSCAPE_SFP) += nvmem-layerscape-sfp.o
  44. nvmem-layerscape-sfp-y := layerscape-sfp.o
  45. obj-$(CONFIG_NVMEM_LPC18XX_EEPROM) += nvmem_lpc18xx_eeprom.o
  46. --- /dev/null
  47. +++ b/drivers/nvmem/lan9662-otpc.c
  48. @@ -0,0 +1,222 @@
  49. +// SPDX-License-Identifier: GPL-2.0
  50. +
  51. +#include <linux/iopoll.h>
  52. +#include <linux/module.h>
  53. +#include <linux/nvmem-provider.h>
  54. +#include <linux/of.h>
  55. +#include <linux/platform_device.h>
  56. +
  57. +#define OTP_OTP_PWR_DN(t) (t + 0x00)
  58. +#define OTP_OTP_PWR_DN_OTP_PWRDN_N BIT(0)
  59. +#define OTP_OTP_ADDR_HI(t) (t + 0x04)
  60. +#define OTP_OTP_ADDR_LO(t) (t + 0x08)
  61. +#define OTP_OTP_PRGM_DATA(t) (t + 0x10)
  62. +#define OTP_OTP_PRGM_MODE(t) (t + 0x14)
  63. +#define OTP_OTP_PRGM_MODE_OTP_PGM_MODE_BYTE BIT(0)
  64. +#define OTP_OTP_RD_DATA(t) (t + 0x18)
  65. +#define OTP_OTP_FUNC_CMD(t) (t + 0x20)
  66. +#define OTP_OTP_FUNC_CMD_OTP_PROGRAM BIT(1)
  67. +#define OTP_OTP_FUNC_CMD_OTP_READ BIT(0)
  68. +#define OTP_OTP_CMD_GO(t) (t + 0x28)
  69. +#define OTP_OTP_CMD_GO_OTP_GO BIT(0)
  70. +#define OTP_OTP_PASS_FAIL(t) (t + 0x2c)
  71. +#define OTP_OTP_PASS_FAIL_OTP_READ_PROHIBITED BIT(3)
  72. +#define OTP_OTP_PASS_FAIL_OTP_WRITE_PROHIBITED BIT(2)
  73. +#define OTP_OTP_PASS_FAIL_OTP_FAIL BIT(0)
  74. +#define OTP_OTP_STATUS(t) (t + 0x30)
  75. +#define OTP_OTP_STATUS_OTP_CPUMPEN BIT(1)
  76. +#define OTP_OTP_STATUS_OTP_BUSY BIT(0)
  77. +
  78. +#define OTP_MEM_SIZE 8192
  79. +#define OTP_SLEEP_US 10
  80. +#define OTP_TIMEOUT_US 500000
  81. +
  82. +struct lan9662_otp {
  83. + struct device *dev;
  84. + void __iomem *base;
  85. +};
  86. +
  87. +static bool lan9662_otp_wait_flag_clear(void __iomem *reg, u32 flag)
  88. +{
  89. + u32 val;
  90. +
  91. + return readl_poll_timeout(reg, val, !(val & flag),
  92. + OTP_SLEEP_US, OTP_TIMEOUT_US);
  93. +}
  94. +
  95. +static int lan9662_otp_power(struct lan9662_otp *otp, bool up)
  96. +{
  97. + void __iomem *pwrdn = OTP_OTP_PWR_DN(otp->base);
  98. +
  99. + if (up) {
  100. + writel(readl(pwrdn) & ~OTP_OTP_PWR_DN_OTP_PWRDN_N, pwrdn);
  101. + if (lan9662_otp_wait_flag_clear(OTP_OTP_STATUS(otp->base),
  102. + OTP_OTP_STATUS_OTP_CPUMPEN))
  103. + return -ETIMEDOUT;
  104. + } else {
  105. + writel(readl(pwrdn) | OTP_OTP_PWR_DN_OTP_PWRDN_N, pwrdn);
  106. + }
  107. +
  108. + return 0;
  109. +}
  110. +
  111. +static int lan9662_otp_execute(struct lan9662_otp *otp)
  112. +{
  113. + if (lan9662_otp_wait_flag_clear(OTP_OTP_CMD_GO(otp->base),
  114. + OTP_OTP_CMD_GO_OTP_GO))
  115. + return -ETIMEDOUT;
  116. +
  117. + if (lan9662_otp_wait_flag_clear(OTP_OTP_STATUS(otp->base),
  118. + OTP_OTP_STATUS_OTP_BUSY))
  119. + return -ETIMEDOUT;
  120. +
  121. + return 0;
  122. +}
  123. +
  124. +static void lan9662_otp_set_address(struct lan9662_otp *otp, u32 offset)
  125. +{
  126. + writel(0xff & (offset >> 8), OTP_OTP_ADDR_HI(otp->base));
  127. + writel(0xff & offset, OTP_OTP_ADDR_LO(otp->base));
  128. +}
  129. +
  130. +static int lan9662_otp_read_byte(struct lan9662_otp *otp, u32 offset, u8 *dst)
  131. +{
  132. + u32 pass;
  133. + int rc;
  134. +
  135. + lan9662_otp_set_address(otp, offset);
  136. + writel(OTP_OTP_FUNC_CMD_OTP_READ, OTP_OTP_FUNC_CMD(otp->base));
  137. + writel(OTP_OTP_CMD_GO_OTP_GO, OTP_OTP_CMD_GO(otp->base));
  138. + rc = lan9662_otp_execute(otp);
  139. + if (!rc) {
  140. + pass = readl(OTP_OTP_PASS_FAIL(otp->base));
  141. + if (pass & OTP_OTP_PASS_FAIL_OTP_READ_PROHIBITED)
  142. + return -EACCES;
  143. + *dst = (u8) readl(OTP_OTP_RD_DATA(otp->base));
  144. + }
  145. + return rc;
  146. +}
  147. +
  148. +static int lan9662_otp_write_byte(struct lan9662_otp *otp, u32 offset, u8 data)
  149. +{
  150. + u32 pass;
  151. + int rc;
  152. +
  153. + lan9662_otp_set_address(otp, offset);
  154. + writel(OTP_OTP_PRGM_MODE_OTP_PGM_MODE_BYTE, OTP_OTP_PRGM_MODE(otp->base));
  155. + writel(data, OTP_OTP_PRGM_DATA(otp->base));
  156. + writel(OTP_OTP_FUNC_CMD_OTP_PROGRAM, OTP_OTP_FUNC_CMD(otp->base));
  157. + writel(OTP_OTP_CMD_GO_OTP_GO, OTP_OTP_CMD_GO(otp->base));
  158. +
  159. + rc = lan9662_otp_execute(otp);
  160. + if (!rc) {
  161. + pass = readl(OTP_OTP_PASS_FAIL(otp->base));
  162. + if (pass & OTP_OTP_PASS_FAIL_OTP_WRITE_PROHIBITED)
  163. + return -EACCES;
  164. + if (pass & OTP_OTP_PASS_FAIL_OTP_FAIL)
  165. + return -EIO;
  166. + }
  167. + return rc;
  168. +}
  169. +
  170. +static int lan9662_otp_read(void *context, unsigned int offset,
  171. + void *_val, size_t bytes)
  172. +{
  173. + struct lan9662_otp *otp = context;
  174. + u8 *val = _val;
  175. + uint8_t data;
  176. + int i, rc = 0;
  177. +
  178. + lan9662_otp_power(otp, true);
  179. + for (i = 0; i < bytes; i++) {
  180. + rc = lan9662_otp_read_byte(otp, offset + i, &data);
  181. + if (rc < 0)
  182. + break;
  183. + *val++ = data;
  184. + }
  185. + lan9662_otp_power(otp, false);
  186. +
  187. + return rc;
  188. +}
  189. +
  190. +static int lan9662_otp_write(void *context, unsigned int offset,
  191. + void *_val, size_t bytes)
  192. +{
  193. + struct lan9662_otp *otp = context;
  194. + u8 *val = _val;
  195. + u8 data, newdata;
  196. + int i, rc = 0;
  197. +
  198. + lan9662_otp_power(otp, true);
  199. + for (i = 0; i < bytes; i++) {
  200. + /* Skip zero bytes */
  201. + if (val[i]) {
  202. + rc = lan9662_otp_read_byte(otp, offset + i, &data);
  203. + if (rc < 0)
  204. + break;
  205. +
  206. + newdata = data | val[i];
  207. + if (newdata == data)
  208. + continue;
  209. +
  210. + rc = lan9662_otp_write_byte(otp, offset + i,
  211. + newdata);
  212. + if (rc < 0)
  213. + break;
  214. + }
  215. + }
  216. + lan9662_otp_power(otp, false);
  217. +
  218. + return rc;
  219. +}
  220. +
  221. +static struct nvmem_config otp_config = {
  222. + .name = "lan9662-otp",
  223. + .stride = 1,
  224. + .word_size = 1,
  225. + .reg_read = lan9662_otp_read,
  226. + .reg_write = lan9662_otp_write,
  227. + .size = OTP_MEM_SIZE,
  228. +};
  229. +
  230. +static int lan9662_otp_probe(struct platform_device *pdev)
  231. +{
  232. + struct device *dev = &pdev->dev;
  233. + struct nvmem_device *nvmem;
  234. + struct lan9662_otp *otp;
  235. +
  236. + otp = devm_kzalloc(&pdev->dev, sizeof(*otp), GFP_KERNEL);
  237. + if (!otp)
  238. + return -ENOMEM;
  239. +
  240. + otp->dev = dev;
  241. + otp->base = devm_platform_ioremap_resource(pdev, 0);
  242. + if (IS_ERR(otp->base))
  243. + return PTR_ERR(otp->base);
  244. +
  245. + otp_config.priv = otp;
  246. + otp_config.dev = dev;
  247. +
  248. + nvmem = devm_nvmem_register(dev, &otp_config);
  249. +
  250. + return PTR_ERR_OR_ZERO(nvmem);
  251. +}
  252. +
  253. +static const struct of_device_id lan9662_otp_match[] = {
  254. + { .compatible = "microchip,lan9662-otp", },
  255. + { },
  256. +};
  257. +MODULE_DEVICE_TABLE(of, lan9662_otp_match);
  258. +
  259. +static struct platform_driver lan9662_otp_driver = {
  260. + .probe = lan9662_otp_probe,
  261. + .driver = {
  262. + .name = "lan9662-otp",
  263. + .of_match_table = lan9662_otp_match,
  264. + },
  265. +};
  266. +module_platform_driver(lan9662_otp_driver);
  267. +
  268. +MODULE_AUTHOR("Horatiu Vultur <[email protected]>");
  269. +MODULE_DESCRIPTION("lan9662 OTP driver");
  270. +MODULE_LICENSE("GPL");