bcm-ocotp.c 7.9 KB

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