bcm-ocotp.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (C) 2016 Broadcom
  3. #include <linux/acpi.h>
  4. #include <linux/delay.h>
  5. #include <linux/device.h>
  6. #include <linux/io.h>
  7. #include <linux/module.h>
  8. #include <linux/nvmem-provider.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. /*
  12. * # of tries for OTP Status. The time to execute a command varies. The slowest
  13. * commands are writes which also vary based on the # of bits turned on. Writing
  14. * 0xffffffff takes ~3800 us.
  15. */
  16. #define OTPC_RETRIES 5000
  17. /* Sequence to enable OTP program */
  18. #define OTPC_PROG_EN_SEQ { 0xf, 0x4, 0x8, 0xd }
  19. /* OTPC Commands */
  20. #define OTPC_CMD_READ 0x0
  21. #define OTPC_CMD_OTP_PROG_ENABLE 0x2
  22. #define OTPC_CMD_OTP_PROG_DISABLE 0x3
  23. #define OTPC_CMD_PROGRAM 0x8
  24. /* OTPC Status Bits */
  25. #define OTPC_STAT_CMD_DONE BIT(1)
  26. #define OTPC_STAT_PROG_OK BIT(2)
  27. /* OTPC register definition */
  28. #define OTPC_MODE_REG_OFFSET 0x0
  29. #define OTPC_MODE_REG_OTPC_MODE 0
  30. #define OTPC_COMMAND_OFFSET 0x4
  31. #define OTPC_COMMAND_COMMAND_WIDTH 6
  32. #define OTPC_CMD_START_OFFSET 0x8
  33. #define OTPC_CMD_START_START 0
  34. #define OTPC_CPU_STATUS_OFFSET 0xc
  35. #define OTPC_CPUADDR_REG_OFFSET 0x28
  36. #define OTPC_CPUADDR_REG_OTPC_CPU_ADDRESS_WIDTH 16
  37. #define OTPC_CPU_WRITE_REG_OFFSET 0x2c
  38. #define OTPC_CMD_MASK (BIT(OTPC_COMMAND_COMMAND_WIDTH) - 1)
  39. #define OTPC_ADDR_MASK (BIT(OTPC_CPUADDR_REG_OTPC_CPU_ADDRESS_WIDTH) - 1)
  40. struct otpc_map {
  41. /* in words. */
  42. u32 otpc_row_size;
  43. /* 128 bit row / 4 words support. */
  44. u16 data_r_offset[4];
  45. /* 128 bit row / 4 words support. */
  46. u16 data_w_offset[4];
  47. };
  48. static struct otpc_map otp_map = {
  49. .otpc_row_size = 1,
  50. .data_r_offset = {0x10},
  51. .data_w_offset = {0x2c},
  52. };
  53. static struct otpc_map otp_map_v2 = {
  54. .otpc_row_size = 2,
  55. .data_r_offset = {0x10, 0x5c},
  56. .data_w_offset = {0x2c, 0x64},
  57. };
  58. struct otpc_priv {
  59. struct device *dev;
  60. void __iomem *base;
  61. const struct otpc_map *map;
  62. struct nvmem_config *config;
  63. };
  64. static inline void set_command(void __iomem *base, u32 command)
  65. {
  66. writel(command & OTPC_CMD_MASK, base + OTPC_COMMAND_OFFSET);
  67. }
  68. static inline void set_cpu_address(void __iomem *base, u32 addr)
  69. {
  70. writel(addr & OTPC_ADDR_MASK, base + OTPC_CPUADDR_REG_OFFSET);
  71. }
  72. static inline void set_start_bit(void __iomem *base)
  73. {
  74. writel(1 << OTPC_CMD_START_START, base + OTPC_CMD_START_OFFSET);
  75. }
  76. static inline void reset_start_bit(void __iomem *base)
  77. {
  78. writel(0, base + OTPC_CMD_START_OFFSET);
  79. }
  80. static inline void write_cpu_data(void __iomem *base, u32 value)
  81. {
  82. writel(value, base + OTPC_CPU_WRITE_REG_OFFSET);
  83. }
  84. static int poll_cpu_status(void __iomem *base, u32 value)
  85. {
  86. u32 status;
  87. u32 retries;
  88. for (retries = 0; retries < OTPC_RETRIES; retries++) {
  89. status = readl(base + OTPC_CPU_STATUS_OFFSET);
  90. if (status & value)
  91. break;
  92. udelay(1);
  93. }
  94. if (retries == OTPC_RETRIES)
  95. return -EAGAIN;
  96. return 0;
  97. }
  98. static int enable_ocotp_program(void __iomem *base)
  99. {
  100. static const u32 vals[] = OTPC_PROG_EN_SEQ;
  101. int i;
  102. int ret;
  103. /* Write the magic sequence to enable programming */
  104. set_command(base, OTPC_CMD_OTP_PROG_ENABLE);
  105. for (i = 0; i < ARRAY_SIZE(vals); i++) {
  106. write_cpu_data(base, vals[i]);
  107. set_start_bit(base);
  108. ret = poll_cpu_status(base, OTPC_STAT_CMD_DONE);
  109. reset_start_bit(base);
  110. if (ret)
  111. return ret;
  112. }
  113. return poll_cpu_status(base, OTPC_STAT_PROG_OK);
  114. }
  115. static int disable_ocotp_program(void __iomem *base)
  116. {
  117. int ret;
  118. set_command(base, OTPC_CMD_OTP_PROG_DISABLE);
  119. set_start_bit(base);
  120. ret = poll_cpu_status(base, OTPC_STAT_PROG_OK);
  121. reset_start_bit(base);
  122. return ret;
  123. }
  124. static int bcm_otpc_read(void *context, unsigned int offset, void *val,
  125. size_t bytes)
  126. {
  127. struct otpc_priv *priv = context;
  128. u32 *buf = val;
  129. u32 bytes_read;
  130. u32 address = offset / priv->config->word_size;
  131. int i, ret;
  132. for (bytes_read = 0; bytes_read < bytes;) {
  133. set_command(priv->base, OTPC_CMD_READ);
  134. set_cpu_address(priv->base, address++);
  135. set_start_bit(priv->base);
  136. ret = poll_cpu_status(priv->base, OTPC_STAT_CMD_DONE);
  137. if (ret) {
  138. dev_err(priv->dev, "otp read error: 0x%x", ret);
  139. return -EIO;
  140. }
  141. for (i = 0; i < priv->map->otpc_row_size; i++) {
  142. *buf++ = readl(priv->base +
  143. priv->map->data_r_offset[i]);
  144. bytes_read += sizeof(*buf);
  145. }
  146. reset_start_bit(priv->base);
  147. }
  148. return 0;
  149. }
  150. static int bcm_otpc_write(void *context, unsigned int offset, void *val,
  151. size_t bytes)
  152. {
  153. struct otpc_priv *priv = context;
  154. u32 *buf = val;
  155. u32 bytes_written;
  156. u32 address = offset / priv->config->word_size;
  157. int i, ret;
  158. if (offset % priv->config->word_size)
  159. return -EINVAL;
  160. ret = enable_ocotp_program(priv->base);
  161. if (ret)
  162. return -EIO;
  163. for (bytes_written = 0; bytes_written < bytes;) {
  164. set_command(priv->base, OTPC_CMD_PROGRAM);
  165. set_cpu_address(priv->base, address++);
  166. for (i = 0; i < priv->map->otpc_row_size; i++) {
  167. writel(*buf, priv->base + priv->map->data_w_offset[i]);
  168. buf++;
  169. bytes_written += sizeof(*buf);
  170. }
  171. set_start_bit(priv->base);
  172. ret = poll_cpu_status(priv->base, OTPC_STAT_CMD_DONE);
  173. reset_start_bit(priv->base);
  174. if (ret) {
  175. dev_err(priv->dev, "otp write error: 0x%x", ret);
  176. return -EIO;
  177. }
  178. }
  179. disable_ocotp_program(priv->base);
  180. return 0;
  181. }
  182. static struct nvmem_config bcm_otpc_nvmem_config = {
  183. .name = "bcm-ocotp",
  184. .read_only = false,
  185. .word_size = 4,
  186. .stride = 4,
  187. .reg_read = bcm_otpc_read,
  188. .reg_write = bcm_otpc_write,
  189. };
  190. static const struct of_device_id bcm_otpc_dt_ids[] = {
  191. { .compatible = "brcm,ocotp", .data = &otp_map },
  192. { .compatible = "brcm,ocotp-v2", .data = &otp_map_v2 },
  193. { },
  194. };
  195. MODULE_DEVICE_TABLE(of, bcm_otpc_dt_ids);
  196. static const struct acpi_device_id bcm_otpc_acpi_ids[] __maybe_unused = {
  197. { .id = "BRCM0700", .driver_data = (kernel_ulong_t)&otp_map },
  198. { .id = "BRCM0701", .driver_data = (kernel_ulong_t)&otp_map_v2 },
  199. { /* sentinel */ }
  200. };
  201. MODULE_DEVICE_TABLE(acpi, bcm_otpc_acpi_ids);
  202. static int bcm_otpc_probe(struct platform_device *pdev)
  203. {
  204. struct device *dev = &pdev->dev;
  205. struct otpc_priv *priv;
  206. struct nvmem_device *nvmem;
  207. int err;
  208. u32 num_words;
  209. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  210. if (!priv)
  211. return -ENOMEM;
  212. priv->map = device_get_match_data(dev);
  213. if (!priv->map)
  214. return -ENODEV;
  215. /* Get OTP base address register. */
  216. priv->base = devm_platform_ioremap_resource(pdev, 0);
  217. if (IS_ERR(priv->base)) {
  218. dev_err(dev, "unable to map I/O memory\n");
  219. return PTR_ERR(priv->base);
  220. }
  221. /* Enable CPU access to OTPC. */
  222. writel(readl(priv->base + OTPC_MODE_REG_OFFSET) |
  223. BIT(OTPC_MODE_REG_OTPC_MODE),
  224. priv->base + OTPC_MODE_REG_OFFSET);
  225. reset_start_bit(priv->base);
  226. /* Read size of memory in words. */
  227. err = device_property_read_u32(dev, "brcm,ocotp-size", &num_words);
  228. if (err) {
  229. dev_err(dev, "size parameter not specified\n");
  230. return -EINVAL;
  231. } else if (num_words == 0) {
  232. dev_err(dev, "size must be > 0\n");
  233. return -EINVAL;
  234. }
  235. bcm_otpc_nvmem_config.size = 4 * num_words;
  236. bcm_otpc_nvmem_config.dev = dev;
  237. bcm_otpc_nvmem_config.priv = priv;
  238. if (priv->map == &otp_map_v2) {
  239. bcm_otpc_nvmem_config.word_size = 8;
  240. bcm_otpc_nvmem_config.stride = 8;
  241. }
  242. priv->config = &bcm_otpc_nvmem_config;
  243. nvmem = devm_nvmem_register(dev, &bcm_otpc_nvmem_config);
  244. if (IS_ERR(nvmem)) {
  245. dev_err(dev, "error registering nvmem config\n");
  246. return PTR_ERR(nvmem);
  247. }
  248. return 0;
  249. }
  250. static struct platform_driver bcm_otpc_driver = {
  251. .probe = bcm_otpc_probe,
  252. .driver = {
  253. .name = "brcm-otpc",
  254. .of_match_table = bcm_otpc_dt_ids,
  255. .acpi_match_table = ACPI_PTR(bcm_otpc_acpi_ids),
  256. },
  257. };
  258. module_platform_driver(bcm_otpc_driver);
  259. MODULE_DESCRIPTION("Broadcom OTPC driver");
  260. MODULE_LICENSE("GPL v2");