vf610-ocotp.c 7.3 KB

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