bcm-ocotp.c 7.7 KB

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