imx-ocotp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * i.MX6 OCOTP fusebox driver
  3. *
  4. * Copyright (c) 2015 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
  5. *
  6. * Based on the barebox ocotp driver,
  7. * Copyright (c) 2010 Baruch Siach <baruch@tkos.co.il>,
  8. * Orex Computed Radiography
  9. *
  10. * Write support based on the fsl_otp driver,
  11. * Copyright (C) 2010-2013 Freescale Semiconductor, Inc
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2
  15. * as published by the Free Software Foundation.
  16. *
  17. * http://www.opensource.org/licenses/gpl-license.html
  18. * http://www.gnu.org/copyleft/gpl.html
  19. */
  20. #include <linux/clk.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/of_device.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <linux/delay.h>
  30. #define IMX_OCOTP_OFFSET_B0W0 0x400 /* Offset from base address of the
  31. * OTP Bank0 Word0
  32. */
  33. #define IMX_OCOTP_OFFSET_PER_WORD 0x10 /* Offset between the start addr
  34. * of two consecutive OTP words.
  35. */
  36. #define IMX_OCOTP_ADDR_CTRL 0x0000
  37. #define IMX_OCOTP_ADDR_CTRL_SET 0x0004
  38. #define IMX_OCOTP_ADDR_CTRL_CLR 0x0008
  39. #define IMX_OCOTP_ADDR_TIMING 0x0010
  40. #define IMX_OCOTP_ADDR_DATA 0x0020
  41. #define IMX_OCOTP_BM_CTRL_ADDR 0x0000007F
  42. #define IMX_OCOTP_BM_CTRL_BUSY 0x00000100
  43. #define IMX_OCOTP_BM_CTRL_ERROR 0x00000200
  44. #define IMX_OCOTP_BM_CTRL_REL_SHADOWS 0x00000400
  45. #define DEF_RELAX 20 /* > 16.5ns */
  46. #define IMX_OCOTP_WR_UNLOCK 0x3E770000
  47. #define IMX_OCOTP_READ_LOCKED_VAL 0xBADABADA
  48. static DEFINE_MUTEX(ocotp_mutex);
  49. struct ocotp_priv {
  50. struct device *dev;
  51. struct clk *clk;
  52. void __iomem *base;
  53. unsigned int nregs;
  54. struct nvmem_config *config;
  55. };
  56. static int imx_ocotp_wait_for_busy(void __iomem *base, u32 flags)
  57. {
  58. int count;
  59. u32 c, mask;
  60. mask = IMX_OCOTP_BM_CTRL_BUSY | IMX_OCOTP_BM_CTRL_ERROR | flags;
  61. for (count = 10000; count >= 0; count--) {
  62. c = readl(base + IMX_OCOTP_ADDR_CTRL);
  63. if (!(c & mask))
  64. break;
  65. cpu_relax();
  66. }
  67. if (count < 0) {
  68. /* HW_OCOTP_CTRL[ERROR] will be set under the following
  69. * conditions:
  70. * - A write is performed to a shadow register during a shadow
  71. * reload (essentially, while HW_OCOTP_CTRL[RELOAD_SHADOWS] is
  72. * set. In addition, the contents of the shadow register shall
  73. * not be updated.
  74. * - A write is performed to a shadow register which has been
  75. * locked.
  76. * - A read is performed to from a shadow register which has
  77. * been read locked.
  78. * - A program is performed to a fuse word which has been locked
  79. * - A read is performed to from a fuse word which has been read
  80. * locked.
  81. */
  82. if (c & IMX_OCOTP_BM_CTRL_ERROR)
  83. return -EPERM;
  84. return -ETIMEDOUT;
  85. }
  86. return 0;
  87. }
  88. static void imx_ocotp_clr_err_if_set(void __iomem *base)
  89. {
  90. u32 c;
  91. c = readl(base + IMX_OCOTP_ADDR_CTRL);
  92. if (!(c & IMX_OCOTP_BM_CTRL_ERROR))
  93. return;
  94. writel(IMX_OCOTP_BM_CTRL_ERROR, base + IMX_OCOTP_ADDR_CTRL_CLR);
  95. }
  96. static int imx_ocotp_read(void *context, unsigned int offset,
  97. void *val, size_t bytes)
  98. {
  99. struct ocotp_priv *priv = context;
  100. unsigned int count;
  101. u32 *buf = val;
  102. int i, ret;
  103. u32 index;
  104. index = offset >> 2;
  105. count = bytes >> 2;
  106. if (count > (priv->nregs - index))
  107. count = priv->nregs - index;
  108. mutex_lock(&ocotp_mutex);
  109. ret = clk_prepare_enable(priv->clk);
  110. if (ret < 0) {
  111. mutex_unlock(&ocotp_mutex);
  112. dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
  113. return ret;
  114. }
  115. ret = imx_ocotp_wait_for_busy(priv->base, 0);
  116. if (ret < 0) {
  117. dev_err(priv->dev, "timeout during read setup\n");
  118. goto read_end;
  119. }
  120. for (i = index; i < (index + count); i++) {
  121. *buf++ = readl(priv->base + IMX_OCOTP_OFFSET_B0W0 +
  122. i * IMX_OCOTP_OFFSET_PER_WORD);
  123. /* 47.3.1.2
  124. * For "read locked" registers 0xBADABADA will be returned and
  125. * HW_OCOTP_CTRL[ERROR] will be set. It must be cleared by
  126. * software before any new write, read or reload access can be
  127. * issued
  128. */
  129. if (*(buf - 1) == IMX_OCOTP_READ_LOCKED_VAL)
  130. imx_ocotp_clr_err_if_set(priv->base);
  131. }
  132. ret = 0;
  133. read_end:
  134. clk_disable_unprepare(priv->clk);
  135. mutex_unlock(&ocotp_mutex);
  136. return ret;
  137. }
  138. static int imx_ocotp_write(void *context, unsigned int offset, void *val,
  139. size_t bytes)
  140. {
  141. struct ocotp_priv *priv = context;
  142. u32 *buf = val;
  143. int ret;
  144. unsigned long clk_rate = 0;
  145. unsigned long strobe_read, relax, strobe_prog;
  146. u32 timing = 0;
  147. u32 ctrl;
  148. u8 waddr;
  149. /* allow only writing one complete OTP word at a time */
  150. if ((bytes != priv->config->word_size) ||
  151. (offset % priv->config->word_size))
  152. return -EINVAL;
  153. mutex_lock(&ocotp_mutex);
  154. ret = clk_prepare_enable(priv->clk);
  155. if (ret < 0) {
  156. mutex_unlock(&ocotp_mutex);
  157. dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
  158. return ret;
  159. }
  160. /* 47.3.1.3.1
  161. * Program HW_OCOTP_TIMING[STROBE_PROG] and HW_OCOTP_TIMING[RELAX]
  162. * fields with timing values to match the current frequency of the
  163. * ipg_clk. OTP writes will work at maximum bus frequencies as long
  164. * as the HW_OCOTP_TIMING parameters are set correctly.
  165. */
  166. clk_rate = clk_get_rate(priv->clk);
  167. relax = clk_rate / (1000000000 / DEF_RELAX) - 1;
  168. strobe_prog = clk_rate / (1000000000 / 10000) + 2 * (DEF_RELAX + 1) - 1;
  169. strobe_read = clk_rate / (1000000000 / 40) + 2 * (DEF_RELAX + 1) - 1;
  170. timing = strobe_prog & 0x00000FFF;
  171. timing |= (relax << 12) & 0x0000F000;
  172. timing |= (strobe_read << 16) & 0x003F0000;
  173. writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
  174. /* 47.3.1.3.2
  175. * Check that HW_OCOTP_CTRL[BUSY] and HW_OCOTP_CTRL[ERROR] are clear.
  176. * Overlapped accesses are not supported by the controller. Any pending
  177. * write or reload must be completed before a write access can be
  178. * requested.
  179. */
  180. ret = imx_ocotp_wait_for_busy(priv->base, 0);
  181. if (ret < 0) {
  182. dev_err(priv->dev, "timeout during timing setup\n");
  183. goto write_end;
  184. }
  185. /* 47.3.1.3.3
  186. * Write the requested address to HW_OCOTP_CTRL[ADDR] and program the
  187. * unlock code into HW_OCOTP_CTRL[WR_UNLOCK]. This must be programmed
  188. * for each write access. The lock code is documented in the register
  189. * description. Both the unlock code and address can be written in the
  190. * same operation.
  191. */
  192. /* OTP write/read address specifies one of 128 word address locations */
  193. waddr = offset / 4;
  194. ctrl = readl(priv->base + IMX_OCOTP_ADDR_CTRL);
  195. ctrl &= ~IMX_OCOTP_BM_CTRL_ADDR;
  196. ctrl |= waddr & IMX_OCOTP_BM_CTRL_ADDR;
  197. ctrl |= IMX_OCOTP_WR_UNLOCK;
  198. writel(ctrl, priv->base + IMX_OCOTP_ADDR_CTRL);
  199. /* 47.3.1.3.4
  200. * Write the data to the HW_OCOTP_DATA register. This will automatically
  201. * set HW_OCOTP_CTRL[BUSY] and clear HW_OCOTP_CTRL[WR_UNLOCK]. To
  202. * protect programming same OTP bit twice, before program OCOTP will
  203. * automatically read fuse value in OTP and use read value to mask
  204. * program data. The controller will use masked program data to program
  205. * a 32-bit word in the OTP per the address in HW_OCOTP_CTRL[ADDR]. Bit
  206. * fields with 1's will result in that OTP bit being programmed. Bit
  207. * fields with 0's will be ignored. At the same time that the write is
  208. * accepted, the controller makes an internal copy of
  209. * HW_OCOTP_CTRL[ADDR] which cannot be updated until the next write
  210. * sequence is initiated. This copy guarantees that erroneous writes to
  211. * HW_OCOTP_CTRL[ADDR] will not affect an active write operation. It
  212. * should also be noted that during the programming HW_OCOTP_DATA will
  213. * shift right (with zero fill). This shifting is required to program
  214. * the OTP serially. During the write operation, HW_OCOTP_DATA cannot be
  215. * modified.
  216. */
  217. writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA);
  218. /* 47.4.1.4.5
  219. * Once complete, the controller will clear BUSY. A write request to a
  220. * protected or locked region will result in no OTP access and no
  221. * setting of HW_OCOTP_CTRL[BUSY]. In addition HW_OCOTP_CTRL[ERROR] will
  222. * be set. It must be cleared by software before any new write access
  223. * can be issued.
  224. */
  225. ret = imx_ocotp_wait_for_busy(priv->base, 0);
  226. if (ret < 0) {
  227. if (ret == -EPERM) {
  228. dev_err(priv->dev, "failed write to locked region");
  229. imx_ocotp_clr_err_if_set(priv->base);
  230. } else {
  231. dev_err(priv->dev, "timeout during data write\n");
  232. }
  233. goto write_end;
  234. }
  235. /* 47.3.1.4
  236. * Write Postamble: Due to internal electrical characteristics of the
  237. * OTP during writes, all OTP operations following a write must be
  238. * separated by 2 us after the clearing of HW_OCOTP_CTRL_BUSY following
  239. * the write.
  240. */
  241. udelay(2);
  242. /* reload all shadow registers */
  243. writel(IMX_OCOTP_BM_CTRL_REL_SHADOWS,
  244. priv->base + IMX_OCOTP_ADDR_CTRL_SET);
  245. ret = imx_ocotp_wait_for_busy(priv->base,
  246. IMX_OCOTP_BM_CTRL_REL_SHADOWS);
  247. if (ret < 0) {
  248. dev_err(priv->dev, "timeout during shadow register reload\n");
  249. goto write_end;
  250. }
  251. write_end:
  252. clk_disable_unprepare(priv->clk);
  253. mutex_unlock(&ocotp_mutex);
  254. if (ret < 0)
  255. return ret;
  256. return bytes;
  257. }
  258. static struct nvmem_config imx_ocotp_nvmem_config = {
  259. .name = "imx-ocotp",
  260. .read_only = false,
  261. .word_size = 4,
  262. .stride = 4,
  263. .owner = THIS_MODULE,
  264. .reg_read = imx_ocotp_read,
  265. .reg_write = imx_ocotp_write,
  266. };
  267. static const struct of_device_id imx_ocotp_dt_ids[] = {
  268. { .compatible = "fsl,imx6q-ocotp", (void *)128 },
  269. { .compatible = "fsl,imx6sl-ocotp", (void *)64 },
  270. { .compatible = "fsl,imx6sx-ocotp", (void *)128 },
  271. { .compatible = "fsl,imx6ul-ocotp", (void *)128 },
  272. { .compatible = "fsl,imx7d-ocotp", (void *)64 },
  273. { },
  274. };
  275. MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
  276. static int imx_ocotp_probe(struct platform_device *pdev)
  277. {
  278. const struct of_device_id *of_id;
  279. struct device *dev = &pdev->dev;
  280. struct resource *res;
  281. struct ocotp_priv *priv;
  282. struct nvmem_device *nvmem;
  283. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  284. if (!priv)
  285. return -ENOMEM;
  286. priv->dev = dev;
  287. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  288. priv->base = devm_ioremap_resource(dev, res);
  289. if (IS_ERR(priv->base))
  290. return PTR_ERR(priv->base);
  291. priv->clk = devm_clk_get(dev, NULL);
  292. if (IS_ERR(priv->clk))
  293. return PTR_ERR(priv->clk);
  294. of_id = of_match_device(imx_ocotp_dt_ids, dev);
  295. priv->nregs = (unsigned long)of_id->data;
  296. imx_ocotp_nvmem_config.size = 4 * priv->nregs;
  297. imx_ocotp_nvmem_config.dev = dev;
  298. imx_ocotp_nvmem_config.priv = priv;
  299. priv->config = &imx_ocotp_nvmem_config;
  300. nvmem = nvmem_register(&imx_ocotp_nvmem_config);
  301. if (IS_ERR(nvmem))
  302. return PTR_ERR(nvmem);
  303. platform_set_drvdata(pdev, nvmem);
  304. return 0;
  305. }
  306. static int imx_ocotp_remove(struct platform_device *pdev)
  307. {
  308. struct nvmem_device *nvmem = platform_get_drvdata(pdev);
  309. return nvmem_unregister(nvmem);
  310. }
  311. static struct platform_driver imx_ocotp_driver = {
  312. .probe = imx_ocotp_probe,
  313. .remove = imx_ocotp_remove,
  314. .driver = {
  315. .name = "imx_ocotp",
  316. .of_match_table = imx_ocotp_dt_ids,
  317. },
  318. };
  319. module_platform_driver(imx_ocotp_driver);
  320. MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
  321. MODULE_DESCRIPTION("i.MX6 OCOTP fuse box driver");
  322. MODULE_LICENSE("GPL v2");