lan9662-otpc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/iopoll.h>
  3. #include <linux/module.h>
  4. #include <linux/nvmem-provider.h>
  5. #include <linux/of.h>
  6. #include <linux/platform_device.h>
  7. #define OTP_OTP_PWR_DN(t) (t + 0x00)
  8. #define OTP_OTP_PWR_DN_OTP_PWRDN_N BIT(0)
  9. #define OTP_OTP_ADDR_HI(t) (t + 0x04)
  10. #define OTP_OTP_ADDR_LO(t) (t + 0x08)
  11. #define OTP_OTP_PRGM_DATA(t) (t + 0x10)
  12. #define OTP_OTP_PRGM_MODE(t) (t + 0x14)
  13. #define OTP_OTP_PRGM_MODE_OTP_PGM_MODE_BYTE BIT(0)
  14. #define OTP_OTP_RD_DATA(t) (t + 0x18)
  15. #define OTP_OTP_FUNC_CMD(t) (t + 0x20)
  16. #define OTP_OTP_FUNC_CMD_OTP_PROGRAM BIT(1)
  17. #define OTP_OTP_FUNC_CMD_OTP_READ BIT(0)
  18. #define OTP_OTP_CMD_GO(t) (t + 0x28)
  19. #define OTP_OTP_CMD_GO_OTP_GO BIT(0)
  20. #define OTP_OTP_PASS_FAIL(t) (t + 0x2c)
  21. #define OTP_OTP_PASS_FAIL_OTP_READ_PROHIBITED BIT(3)
  22. #define OTP_OTP_PASS_FAIL_OTP_WRITE_PROHIBITED BIT(2)
  23. #define OTP_OTP_PASS_FAIL_OTP_FAIL BIT(0)
  24. #define OTP_OTP_STATUS(t) (t + 0x30)
  25. #define OTP_OTP_STATUS_OTP_CPUMPEN BIT(1)
  26. #define OTP_OTP_STATUS_OTP_BUSY BIT(0)
  27. #define OTP_MEM_SIZE 8192
  28. #define OTP_SLEEP_US 10
  29. #define OTP_TIMEOUT_US 500000
  30. struct lan9662_otp {
  31. struct device *dev;
  32. void __iomem *base;
  33. };
  34. static int lan9662_otp_wait_flag_clear(void __iomem *reg, u32 flag)
  35. {
  36. u32 val;
  37. return readl_poll_timeout(reg, val, !(val & flag),
  38. OTP_SLEEP_US, OTP_TIMEOUT_US);
  39. }
  40. static int lan9662_otp_power(struct lan9662_otp *otp, bool up)
  41. {
  42. void __iomem *pwrdn = OTP_OTP_PWR_DN(otp->base);
  43. if (up) {
  44. writel(readl(pwrdn) & ~OTP_OTP_PWR_DN_OTP_PWRDN_N, pwrdn);
  45. if (lan9662_otp_wait_flag_clear(OTP_OTP_STATUS(otp->base),
  46. OTP_OTP_STATUS_OTP_CPUMPEN))
  47. return -ETIMEDOUT;
  48. } else {
  49. writel(readl(pwrdn) | OTP_OTP_PWR_DN_OTP_PWRDN_N, pwrdn);
  50. }
  51. return 0;
  52. }
  53. static int lan9662_otp_execute(struct lan9662_otp *otp)
  54. {
  55. if (lan9662_otp_wait_flag_clear(OTP_OTP_CMD_GO(otp->base),
  56. OTP_OTP_CMD_GO_OTP_GO))
  57. return -ETIMEDOUT;
  58. if (lan9662_otp_wait_flag_clear(OTP_OTP_STATUS(otp->base),
  59. OTP_OTP_STATUS_OTP_BUSY))
  60. return -ETIMEDOUT;
  61. return 0;
  62. }
  63. static void lan9662_otp_set_address(struct lan9662_otp *otp, u32 offset)
  64. {
  65. writel(0xff & (offset >> 8), OTP_OTP_ADDR_HI(otp->base));
  66. writel(0xff & offset, OTP_OTP_ADDR_LO(otp->base));
  67. }
  68. static int lan9662_otp_read_byte(struct lan9662_otp *otp, u32 offset, u8 *dst)
  69. {
  70. u32 pass;
  71. int rc;
  72. lan9662_otp_set_address(otp, offset);
  73. writel(OTP_OTP_FUNC_CMD_OTP_READ, OTP_OTP_FUNC_CMD(otp->base));
  74. writel(OTP_OTP_CMD_GO_OTP_GO, OTP_OTP_CMD_GO(otp->base));
  75. rc = lan9662_otp_execute(otp);
  76. if (!rc) {
  77. pass = readl(OTP_OTP_PASS_FAIL(otp->base));
  78. if (pass & OTP_OTP_PASS_FAIL_OTP_READ_PROHIBITED)
  79. return -EACCES;
  80. *dst = (u8) readl(OTP_OTP_RD_DATA(otp->base));
  81. }
  82. return rc;
  83. }
  84. static int lan9662_otp_write_byte(struct lan9662_otp *otp, u32 offset, u8 data)
  85. {
  86. u32 pass;
  87. int rc;
  88. lan9662_otp_set_address(otp, offset);
  89. writel(OTP_OTP_PRGM_MODE_OTP_PGM_MODE_BYTE, OTP_OTP_PRGM_MODE(otp->base));
  90. writel(data, OTP_OTP_PRGM_DATA(otp->base));
  91. writel(OTP_OTP_FUNC_CMD_OTP_PROGRAM, OTP_OTP_FUNC_CMD(otp->base));
  92. writel(OTP_OTP_CMD_GO_OTP_GO, OTP_OTP_CMD_GO(otp->base));
  93. rc = lan9662_otp_execute(otp);
  94. if (!rc) {
  95. pass = readl(OTP_OTP_PASS_FAIL(otp->base));
  96. if (pass & OTP_OTP_PASS_FAIL_OTP_WRITE_PROHIBITED)
  97. return -EACCES;
  98. if (pass & OTP_OTP_PASS_FAIL_OTP_FAIL)
  99. return -EIO;
  100. }
  101. return rc;
  102. }
  103. static int lan9662_otp_read(void *context, unsigned int offset,
  104. void *_val, size_t bytes)
  105. {
  106. struct lan9662_otp *otp = context;
  107. u8 *val = _val;
  108. uint8_t data;
  109. int i, rc = 0;
  110. lan9662_otp_power(otp, true);
  111. for (i = 0; i < bytes; i++) {
  112. rc = lan9662_otp_read_byte(otp, offset + i, &data);
  113. if (rc < 0)
  114. break;
  115. *val++ = data;
  116. }
  117. lan9662_otp_power(otp, false);
  118. return rc;
  119. }
  120. static int lan9662_otp_write(void *context, unsigned int offset,
  121. void *_val, size_t bytes)
  122. {
  123. struct lan9662_otp *otp = context;
  124. u8 *val = _val;
  125. u8 data, newdata;
  126. int i, rc = 0;
  127. lan9662_otp_power(otp, true);
  128. for (i = 0; i < bytes; i++) {
  129. /* Skip zero bytes */
  130. if (val[i]) {
  131. rc = lan9662_otp_read_byte(otp, offset + i, &data);
  132. if (rc < 0)
  133. break;
  134. newdata = data | val[i];
  135. if (newdata == data)
  136. continue;
  137. rc = lan9662_otp_write_byte(otp, offset + i,
  138. newdata);
  139. if (rc < 0)
  140. break;
  141. }
  142. }
  143. lan9662_otp_power(otp, false);
  144. return rc;
  145. }
  146. static struct nvmem_config otp_config = {
  147. .name = "lan9662-otp",
  148. .stride = 1,
  149. .word_size = 1,
  150. .reg_read = lan9662_otp_read,
  151. .reg_write = lan9662_otp_write,
  152. .size = OTP_MEM_SIZE,
  153. };
  154. static int lan9662_otp_probe(struct platform_device *pdev)
  155. {
  156. struct device *dev = &pdev->dev;
  157. struct nvmem_device *nvmem;
  158. struct lan9662_otp *otp;
  159. otp = devm_kzalloc(&pdev->dev, sizeof(*otp), GFP_KERNEL);
  160. if (!otp)
  161. return -ENOMEM;
  162. otp->dev = dev;
  163. otp->base = devm_platform_ioremap_resource(pdev, 0);
  164. if (IS_ERR(otp->base))
  165. return PTR_ERR(otp->base);
  166. otp_config.priv = otp;
  167. otp_config.dev = dev;
  168. nvmem = devm_nvmem_register(dev, &otp_config);
  169. return PTR_ERR_OR_ZERO(nvmem);
  170. }
  171. static const struct of_device_id lan9662_otp_match[] = {
  172. { .compatible = "microchip,lan9662-otpc", },
  173. { },
  174. };
  175. MODULE_DEVICE_TABLE(of, lan9662_otp_match);
  176. static struct platform_driver lan9662_otp_driver = {
  177. .probe = lan9662_otp_probe,
  178. .driver = {
  179. .name = "lan9662-otp",
  180. .of_match_table = lan9662_otp_match,
  181. },
  182. };
  183. module_platform_driver(lan9662_otp_driver);
  184. MODULE_AUTHOR("Horatiu Vultur <[email protected]>");
  185. MODULE_DESCRIPTION("lan9662 OTP driver");
  186. MODULE_LICENSE("GPL");