vf610-ocotp.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (C) 2015 Toradex AG.
  3. *
  4. * Author: Sanchayan Maity <[email protected]>
  5. *
  6. * Based on the barebox ocotp driver,
  7. * Copyright (c) 2010 Baruch Siach <[email protected]>
  8. * Orex Computed Radiography
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 and
  12. * only version 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/delay.h>
  21. #include <linux/device.h>
  22. #include <linux/io.h>
  23. #include <linux/module.h>
  24. #include <linux/nvmem-provider.h>
  25. #include <linux/of.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. /* OCOTP Register Offsets */
  29. #define OCOTP_CTRL_REG 0x00
  30. #define OCOTP_CTRL_SET 0x04
  31. #define OCOTP_CTRL_CLR 0x08
  32. #define OCOTP_TIMING 0x10
  33. #define OCOTP_DATA 0x20
  34. #define OCOTP_READ_CTRL_REG 0x30
  35. #define OCOTP_READ_FUSE_DATA 0x40
  36. /* OCOTP Register bits and masks */
  37. #define OCOTP_CTRL_WR_UNLOCK 16
  38. #define OCOTP_CTRL_WR_UNLOCK_KEY 0x3E77
  39. #define OCOTP_CTRL_WR_UNLOCK_MASK GENMASK(31, 16)
  40. #define OCOTP_CTRL_ADDR 0
  41. #define OCOTP_CTRL_ADDR_MASK GENMASK(6, 0)
  42. #define OCOTP_CTRL_RELOAD_SHADOWS BIT(10)
  43. #define OCOTP_CTRL_ERR BIT(9)
  44. #define OCOTP_CTRL_BUSY BIT(8)
  45. #define OCOTP_TIMING_STROBE_READ 16
  46. #define OCOTP_TIMING_STROBE_READ_MASK GENMASK(21, 16)
  47. #define OCOTP_TIMING_RELAX 12
  48. #define OCOTP_TIMING_RELAX_MASK GENMASK(15, 12)
  49. #define OCOTP_TIMING_STROBE_PROG 0
  50. #define OCOTP_TIMING_STROBE_PROG_MASK GENMASK(11, 0)
  51. #define OCOTP_READ_CTRL_READ_FUSE 0x1
  52. #define VF610_OCOTP_TIMEOUT 100000
  53. #define BF(value, field) (((value) << field) & field##_MASK)
  54. #define DEF_RELAX 20
  55. static const int base_to_fuse_addr_mappings[][2] = {
  56. {0x400, 0x00},
  57. {0x410, 0x01},
  58. {0x420, 0x02},
  59. {0x450, 0x05},
  60. {0x4F0, 0x0F},
  61. {0x600, 0x20},
  62. {0x610, 0x21},
  63. {0x620, 0x22},
  64. {0x630, 0x23},
  65. {0x640, 0x24},
  66. {0x650, 0x25},
  67. {0x660, 0x26},
  68. {0x670, 0x27},
  69. {0x6F0, 0x2F},
  70. {0x880, 0x38},
  71. {0x890, 0x39},
  72. {0x8A0, 0x3A},
  73. {0x8B0, 0x3B},
  74. {0x8C0, 0x3C},
  75. {0x8D0, 0x3D},
  76. {0x8E0, 0x3E},
  77. {0x8F0, 0x3F},
  78. {0xC80, 0x78},
  79. {0xC90, 0x79},
  80. {0xCA0, 0x7A},
  81. {0xCB0, 0x7B},
  82. {0xCC0, 0x7C},
  83. {0xCD0, 0x7D},
  84. {0xCE0, 0x7E},
  85. {0xCF0, 0x7F},
  86. };
  87. struct vf610_ocotp {
  88. void __iomem *base;
  89. struct clk *clk;
  90. struct device *dev;
  91. struct nvmem_device *nvmem;
  92. int timing;
  93. };
  94. static int vf610_ocotp_wait_busy(void __iomem *base)
  95. {
  96. int timeout = VF610_OCOTP_TIMEOUT;
  97. while ((readl(base) & OCOTP_CTRL_BUSY) && --timeout)
  98. udelay(10);
  99. if (!timeout) {
  100. writel(OCOTP_CTRL_ERR, base + OCOTP_CTRL_CLR);
  101. return -ETIMEDOUT;
  102. }
  103. udelay(10);
  104. return 0;
  105. }
  106. static int vf610_ocotp_calculate_timing(struct vf610_ocotp *ocotp_dev)
  107. {
  108. u32 clk_rate;
  109. u32 relax, strobe_read, strobe_prog;
  110. u32 timing;
  111. clk_rate = clk_get_rate(ocotp_dev->clk);
  112. /* Refer section OTP read/write timing parameters in TRM */
  113. relax = clk_rate / (1000000000 / DEF_RELAX) - 1;
  114. strobe_prog = clk_rate / (1000000000 / 10000) + 2 * (DEF_RELAX + 1) - 1;
  115. strobe_read = clk_rate / (1000000000 / 40) + 2 * (DEF_RELAX + 1) - 1;
  116. timing = BF(relax, OCOTP_TIMING_RELAX);
  117. timing |= BF(strobe_read, OCOTP_TIMING_STROBE_READ);
  118. timing |= BF(strobe_prog, OCOTP_TIMING_STROBE_PROG);
  119. return timing;
  120. }
  121. static int vf610_get_fuse_address(int base_addr_offset)
  122. {
  123. int i;
  124. for (i = 0; i < ARRAY_SIZE(base_to_fuse_addr_mappings); i++) {
  125. if (base_to_fuse_addr_mappings[i][0] == base_addr_offset)
  126. return base_to_fuse_addr_mappings[i][1];
  127. }
  128. return -EINVAL;
  129. }
  130. static int vf610_ocotp_read(void *context, unsigned int offset,
  131. void *val, size_t bytes)
  132. {
  133. struct vf610_ocotp *ocotp = context;
  134. void __iomem *base = ocotp->base;
  135. u32 reg, *buf = val;
  136. int fuse_addr;
  137. int ret;
  138. while (bytes > 0) {
  139. fuse_addr = vf610_get_fuse_address(offset);
  140. if (fuse_addr > 0) {
  141. writel(ocotp->timing, base + OCOTP_TIMING);
  142. ret = vf610_ocotp_wait_busy(base + OCOTP_CTRL_REG);
  143. if (ret)
  144. return ret;
  145. reg = readl(base + OCOTP_CTRL_REG);
  146. reg &= ~OCOTP_CTRL_ADDR_MASK;
  147. reg &= ~OCOTP_CTRL_WR_UNLOCK_MASK;
  148. reg |= BF(fuse_addr, OCOTP_CTRL_ADDR);
  149. writel(reg, base + OCOTP_CTRL_REG);
  150. writel(OCOTP_READ_CTRL_READ_FUSE,
  151. base + OCOTP_READ_CTRL_REG);
  152. ret = vf610_ocotp_wait_busy(base + OCOTP_CTRL_REG);
  153. if (ret)
  154. return ret;
  155. if (readl(base) & OCOTP_CTRL_ERR) {
  156. dev_dbg(ocotp->dev, "Error reading from fuse address %x\n",
  157. fuse_addr);
  158. writel(OCOTP_CTRL_ERR, base + OCOTP_CTRL_CLR);
  159. }
  160. /*
  161. * In case of error, we do not abort and expect to read
  162. * 0xBADABADA as mentioned by the TRM. We just read this
  163. * value and return.
  164. */
  165. *buf = readl(base + OCOTP_READ_FUSE_DATA);
  166. } else {
  167. *buf = 0;
  168. }
  169. buf++;
  170. bytes -= 4;
  171. offset += 4;
  172. }
  173. return 0;
  174. }
  175. static struct nvmem_config ocotp_config = {
  176. .name = "ocotp",
  177. .owner = THIS_MODULE,
  178. .stride = 4,
  179. .word_size = 4,
  180. .reg_read = vf610_ocotp_read,
  181. };
  182. static const struct of_device_id ocotp_of_match[] = {
  183. { .compatible = "fsl,vf610-ocotp", },
  184. {/* sentinel */},
  185. };
  186. MODULE_DEVICE_TABLE(of, ocotp_of_match);
  187. static int vf610_ocotp_remove(struct platform_device *pdev)
  188. {
  189. struct vf610_ocotp *ocotp_dev = platform_get_drvdata(pdev);
  190. return nvmem_unregister(ocotp_dev->nvmem);
  191. }
  192. static int vf610_ocotp_probe(struct platform_device *pdev)
  193. {
  194. struct device *dev = &pdev->dev;
  195. struct resource *res;
  196. struct vf610_ocotp *ocotp_dev;
  197. ocotp_dev = devm_kzalloc(&pdev->dev,
  198. sizeof(struct vf610_ocotp), GFP_KERNEL);
  199. if (!ocotp_dev)
  200. return -ENOMEM;
  201. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  202. ocotp_dev->base = devm_ioremap_resource(dev, res);
  203. if (IS_ERR(ocotp_dev->base))
  204. return PTR_ERR(ocotp_dev->base);
  205. ocotp_dev->clk = devm_clk_get(dev, NULL);
  206. if (IS_ERR(ocotp_dev->clk)) {
  207. dev_err(dev, "failed getting clock, err = %ld\n",
  208. PTR_ERR(ocotp_dev->clk));
  209. return PTR_ERR(ocotp_dev->clk);
  210. }
  211. ocotp_config.size = resource_size(res);
  212. ocotp_config.priv = ocotp_dev;
  213. ocotp_config.dev = dev;
  214. ocotp_dev->nvmem = nvmem_register(&ocotp_config);
  215. if (IS_ERR(ocotp_dev->nvmem))
  216. return PTR_ERR(ocotp_dev->nvmem);
  217. ocotp_dev->dev = dev;
  218. platform_set_drvdata(pdev, ocotp_dev);
  219. ocotp_dev->timing = vf610_ocotp_calculate_timing(ocotp_dev);
  220. return 0;
  221. }
  222. static struct platform_driver vf610_ocotp_driver = {
  223. .probe = vf610_ocotp_probe,
  224. .remove = vf610_ocotp_remove,
  225. .driver = {
  226. .name = "vf610-ocotp",
  227. .of_match_table = ocotp_of_match,
  228. },
  229. };
  230. module_platform_driver(vf610_ocotp_driver);
  231. MODULE_AUTHOR("Sanchayan Maity <[email protected]>");
  232. MODULE_DESCRIPTION("Vybrid OCOTP driver");
  233. MODULE_LICENSE("GPL v2");