imx-ocotp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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_DATA0 0x0020
  41. #define IMX_OCOTP_ADDR_DATA1 0x0030
  42. #define IMX_OCOTP_ADDR_DATA2 0x0040
  43. #define IMX_OCOTP_ADDR_DATA3 0x0050
  44. #define IMX_OCOTP_BM_CTRL_ADDR 0x0000007F
  45. #define IMX_OCOTP_BM_CTRL_BUSY 0x00000100
  46. #define IMX_OCOTP_BM_CTRL_ERROR 0x00000200
  47. #define IMX_OCOTP_BM_CTRL_REL_SHADOWS 0x00000400
  48. #define TIMING_STROBE_PROG_US 10 /* Min time to blow a fuse */
  49. #define TIMING_STROBE_READ_NS 37 /* Min time before read */
  50. #define TIMING_RELAX_NS 17
  51. #define DEF_FSOURCE 1001 /* > 1000 ns */
  52. #define DEF_STROBE_PROG 10000 /* IPG clocks */
  53. #define IMX_OCOTP_WR_UNLOCK 0x3E770000
  54. #define IMX_OCOTP_READ_LOCKED_VAL 0xBADABADA
  55. static DEFINE_MUTEX(ocotp_mutex);
  56. struct ocotp_priv {
  57. struct device *dev;
  58. struct clk *clk;
  59. void __iomem *base;
  60. const struct ocotp_params *params;
  61. struct nvmem_config *config;
  62. };
  63. struct ocotp_params {
  64. unsigned int nregs;
  65. unsigned int bank_address_words;
  66. void (*set_timing)(struct ocotp_priv *priv);
  67. };
  68. static int imx_ocotp_wait_for_busy(void __iomem *base, u32 flags)
  69. {
  70. int count;
  71. u32 c, mask;
  72. mask = IMX_OCOTP_BM_CTRL_BUSY | IMX_OCOTP_BM_CTRL_ERROR | flags;
  73. for (count = 10000; count >= 0; count--) {
  74. c = readl(base + IMX_OCOTP_ADDR_CTRL);
  75. if (!(c & mask))
  76. break;
  77. cpu_relax();
  78. }
  79. if (count < 0) {
  80. /* HW_OCOTP_CTRL[ERROR] will be set under the following
  81. * conditions:
  82. * - A write is performed to a shadow register during a shadow
  83. * reload (essentially, while HW_OCOTP_CTRL[RELOAD_SHADOWS] is
  84. * set. In addition, the contents of the shadow register shall
  85. * not be updated.
  86. * - A write is performed to a shadow register which has been
  87. * locked.
  88. * - A read is performed to from a shadow register which has
  89. * been read locked.
  90. * - A program is performed to a fuse word which has been locked
  91. * - A read is performed to from a fuse word which has been read
  92. * locked.
  93. */
  94. if (c & IMX_OCOTP_BM_CTRL_ERROR)
  95. return -EPERM;
  96. return -ETIMEDOUT;
  97. }
  98. return 0;
  99. }
  100. static void imx_ocotp_clr_err_if_set(void __iomem *base)
  101. {
  102. u32 c;
  103. c = readl(base + IMX_OCOTP_ADDR_CTRL);
  104. if (!(c & IMX_OCOTP_BM_CTRL_ERROR))
  105. return;
  106. writel(IMX_OCOTP_BM_CTRL_ERROR, base + IMX_OCOTP_ADDR_CTRL_CLR);
  107. }
  108. static int imx_ocotp_read(void *context, unsigned int offset,
  109. void *val, size_t bytes)
  110. {
  111. struct ocotp_priv *priv = context;
  112. unsigned int count;
  113. u32 *buf = val;
  114. int i, ret;
  115. u32 index;
  116. index = offset >> 2;
  117. count = bytes >> 2;
  118. if (count > (priv->params->nregs - index))
  119. count = priv->params->nregs - index;
  120. mutex_lock(&ocotp_mutex);
  121. ret = clk_prepare_enable(priv->clk);
  122. if (ret < 0) {
  123. mutex_unlock(&ocotp_mutex);
  124. dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
  125. return ret;
  126. }
  127. ret = imx_ocotp_wait_for_busy(priv->base, 0);
  128. if (ret < 0) {
  129. dev_err(priv->dev, "timeout during read setup\n");
  130. goto read_end;
  131. }
  132. for (i = index; i < (index + count); i++) {
  133. *buf++ = readl(priv->base + IMX_OCOTP_OFFSET_B0W0 +
  134. i * IMX_OCOTP_OFFSET_PER_WORD);
  135. /* 47.3.1.2
  136. * For "read locked" registers 0xBADABADA will be returned and
  137. * HW_OCOTP_CTRL[ERROR] will be set. It must be cleared by
  138. * software before any new write, read or reload access can be
  139. * issued
  140. */
  141. if (*(buf - 1) == IMX_OCOTP_READ_LOCKED_VAL)
  142. imx_ocotp_clr_err_if_set(priv->base);
  143. }
  144. ret = 0;
  145. read_end:
  146. clk_disable_unprepare(priv->clk);
  147. mutex_unlock(&ocotp_mutex);
  148. return ret;
  149. }
  150. static void imx_ocotp_set_imx6_timing(struct ocotp_priv *priv)
  151. {
  152. unsigned long clk_rate = 0;
  153. unsigned long strobe_read, relax, strobe_prog;
  154. u32 timing = 0;
  155. /* 47.3.1.3.1
  156. * Program HW_OCOTP_TIMING[STROBE_PROG] and HW_OCOTP_TIMING[RELAX]
  157. * fields with timing values to match the current frequency of the
  158. * ipg_clk. OTP writes will work at maximum bus frequencies as long
  159. * as the HW_OCOTP_TIMING parameters are set correctly.
  160. *
  161. * Note: there are minimum timings required to ensure an OTP fuse burns
  162. * correctly that are independent of the ipg_clk. Those values are not
  163. * formally documented anywhere however, working from the minimum
  164. * timings given in u-boot we can say:
  165. *
  166. * - Minimum STROBE_PROG time is 10 microseconds. Intuitively 10
  167. * microseconds feels about right as representative of a minimum time
  168. * to physically burn out a fuse.
  169. *
  170. * - Minimum STROBE_READ i.e. the time to wait post OTP fuse burn before
  171. * performing another read is 37 nanoseconds
  172. *
  173. * - Minimum RELAX timing is 17 nanoseconds. This final RELAX minimum
  174. * timing is not entirely clear the documentation says "This
  175. * count value specifies the time to add to all default timing
  176. * parameters other than the Tpgm and Trd. It is given in number
  177. * of ipg_clk periods." where Tpgm and Trd refer to STROBE_PROG
  178. * and STROBE_READ respectively. What the other timing parameters
  179. * are though, is not specified. Experience shows a zero RELAX
  180. * value will mess up a re-load of the shadow registers post OTP
  181. * burn.
  182. */
  183. clk_rate = clk_get_rate(priv->clk);
  184. relax = DIV_ROUND_UP(clk_rate * TIMING_RELAX_NS, 1000000000) - 1;
  185. strobe_read = DIV_ROUND_UP(clk_rate * TIMING_STROBE_READ_NS,
  186. 1000000000);
  187. strobe_read += 2 * (relax + 1) - 1;
  188. strobe_prog = DIV_ROUND_CLOSEST(clk_rate * TIMING_STROBE_PROG_US,
  189. 1000000);
  190. strobe_prog += 2 * (relax + 1) - 1;
  191. timing = readl(priv->base + IMX_OCOTP_ADDR_TIMING) & 0x0FC00000;
  192. timing |= strobe_prog & 0x00000FFF;
  193. timing |= (relax << 12) & 0x0000F000;
  194. timing |= (strobe_read << 16) & 0x003F0000;
  195. writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
  196. }
  197. static void imx_ocotp_set_imx7_timing(struct ocotp_priv *priv)
  198. {
  199. unsigned long clk_rate = 0;
  200. u64 fsource, strobe_prog;
  201. u32 timing = 0;
  202. /* i.MX 7Solo Applications Processor Reference Manual, Rev. 0.1
  203. * 6.4.3.3
  204. */
  205. clk_rate = clk_get_rate(priv->clk);
  206. fsource = DIV_ROUND_UP_ULL((u64)clk_rate * DEF_FSOURCE,
  207. NSEC_PER_SEC) + 1;
  208. strobe_prog = DIV_ROUND_CLOSEST_ULL((u64)clk_rate * DEF_STROBE_PROG,
  209. NSEC_PER_SEC) + 1;
  210. timing = strobe_prog & 0x00000FFF;
  211. timing |= (fsource << 12) & 0x000FF000;
  212. writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
  213. }
  214. static int imx_ocotp_write(void *context, unsigned int offset, void *val,
  215. size_t bytes)
  216. {
  217. struct ocotp_priv *priv = context;
  218. u32 *buf = val;
  219. int ret;
  220. u32 ctrl;
  221. u8 waddr;
  222. u8 word = 0;
  223. /* allow only writing one complete OTP word at a time */
  224. if ((bytes != priv->config->word_size) ||
  225. (offset % priv->config->word_size))
  226. return -EINVAL;
  227. mutex_lock(&ocotp_mutex);
  228. ret = clk_prepare_enable(priv->clk);
  229. if (ret < 0) {
  230. mutex_unlock(&ocotp_mutex);
  231. dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
  232. return ret;
  233. }
  234. /* Setup the write timing values */
  235. priv->params->set_timing(priv);
  236. /* 47.3.1.3.2
  237. * Check that HW_OCOTP_CTRL[BUSY] and HW_OCOTP_CTRL[ERROR] are clear.
  238. * Overlapped accesses are not supported by the controller. Any pending
  239. * write or reload must be completed before a write access can be
  240. * requested.
  241. */
  242. ret = imx_ocotp_wait_for_busy(priv->base, 0);
  243. if (ret < 0) {
  244. dev_err(priv->dev, "timeout during timing setup\n");
  245. goto write_end;
  246. }
  247. /* 47.3.1.3.3
  248. * Write the requested address to HW_OCOTP_CTRL[ADDR] and program the
  249. * unlock code into HW_OCOTP_CTRL[WR_UNLOCK]. This must be programmed
  250. * for each write access. The lock code is documented in the register
  251. * description. Both the unlock code and address can be written in the
  252. * same operation.
  253. */
  254. if (priv->params->bank_address_words != 0) {
  255. /*
  256. * In banked/i.MX7 mode the OTP register bank goes into waddr
  257. * see i.MX 7Solo Applications Processor Reference Manual, Rev.
  258. * 0.1 section 6.4.3.1
  259. */
  260. offset = offset / priv->config->word_size;
  261. waddr = offset / priv->params->bank_address_words;
  262. word = offset & (priv->params->bank_address_words - 1);
  263. } else {
  264. /*
  265. * Non-banked i.MX6 mode.
  266. * OTP write/read address specifies one of 128 word address
  267. * locations
  268. */
  269. waddr = offset / 4;
  270. }
  271. ctrl = readl(priv->base + IMX_OCOTP_ADDR_CTRL);
  272. ctrl &= ~IMX_OCOTP_BM_CTRL_ADDR;
  273. ctrl |= waddr & IMX_OCOTP_BM_CTRL_ADDR;
  274. ctrl |= IMX_OCOTP_WR_UNLOCK;
  275. writel(ctrl, priv->base + IMX_OCOTP_ADDR_CTRL);
  276. /* 47.3.1.3.4
  277. * Write the data to the HW_OCOTP_DATA register. This will automatically
  278. * set HW_OCOTP_CTRL[BUSY] and clear HW_OCOTP_CTRL[WR_UNLOCK]. To
  279. * protect programming same OTP bit twice, before program OCOTP will
  280. * automatically read fuse value in OTP and use read value to mask
  281. * program data. The controller will use masked program data to program
  282. * a 32-bit word in the OTP per the address in HW_OCOTP_CTRL[ADDR]. Bit
  283. * fields with 1's will result in that OTP bit being programmed. Bit
  284. * fields with 0's will be ignored. At the same time that the write is
  285. * accepted, the controller makes an internal copy of
  286. * HW_OCOTP_CTRL[ADDR] which cannot be updated until the next write
  287. * sequence is initiated. This copy guarantees that erroneous writes to
  288. * HW_OCOTP_CTRL[ADDR] will not affect an active write operation. It
  289. * should also be noted that during the programming HW_OCOTP_DATA will
  290. * shift right (with zero fill). This shifting is required to program
  291. * the OTP serially. During the write operation, HW_OCOTP_DATA cannot be
  292. * modified.
  293. * Note: on i.MX7 there are four data fields to write for banked write
  294. * with the fuse blowing operation only taking place after data0
  295. * has been written. This is why data0 must always be the last
  296. * register written.
  297. */
  298. if (priv->params->bank_address_words != 0) {
  299. /* Banked/i.MX7 mode */
  300. switch (word) {
  301. case 0:
  302. writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
  303. writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
  304. writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
  305. writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0);
  306. break;
  307. case 1:
  308. writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA1);
  309. writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
  310. writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
  311. writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
  312. break;
  313. case 2:
  314. writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
  315. writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA2);
  316. writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
  317. writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
  318. break;
  319. case 3:
  320. writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
  321. writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
  322. writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA3);
  323. writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
  324. break;
  325. }
  326. } else {
  327. /* Non-banked i.MX6 mode */
  328. writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0);
  329. }
  330. /* 47.4.1.4.5
  331. * Once complete, the controller will clear BUSY. A write request to a
  332. * protected or locked region will result in no OTP access and no
  333. * setting of HW_OCOTP_CTRL[BUSY]. In addition HW_OCOTP_CTRL[ERROR] will
  334. * be set. It must be cleared by software before any new write access
  335. * can be issued.
  336. */
  337. ret = imx_ocotp_wait_for_busy(priv->base, 0);
  338. if (ret < 0) {
  339. if (ret == -EPERM) {
  340. dev_err(priv->dev, "failed write to locked region");
  341. imx_ocotp_clr_err_if_set(priv->base);
  342. } else {
  343. dev_err(priv->dev, "timeout during data write\n");
  344. }
  345. goto write_end;
  346. }
  347. /* 47.3.1.4
  348. * Write Postamble: Due to internal electrical characteristics of the
  349. * OTP during writes, all OTP operations following a write must be
  350. * separated by 2 us after the clearing of HW_OCOTP_CTRL_BUSY following
  351. * the write.
  352. */
  353. udelay(2);
  354. /* reload all shadow registers */
  355. writel(IMX_OCOTP_BM_CTRL_REL_SHADOWS,
  356. priv->base + IMX_OCOTP_ADDR_CTRL_SET);
  357. ret = imx_ocotp_wait_for_busy(priv->base,
  358. IMX_OCOTP_BM_CTRL_REL_SHADOWS);
  359. if (ret < 0) {
  360. dev_err(priv->dev, "timeout during shadow register reload\n");
  361. goto write_end;
  362. }
  363. write_end:
  364. clk_disable_unprepare(priv->clk);
  365. mutex_unlock(&ocotp_mutex);
  366. if (ret < 0)
  367. return ret;
  368. return bytes;
  369. }
  370. static struct nvmem_config imx_ocotp_nvmem_config = {
  371. .name = "imx-ocotp",
  372. .read_only = false,
  373. .word_size = 4,
  374. .stride = 4,
  375. .reg_read = imx_ocotp_read,
  376. .reg_write = imx_ocotp_write,
  377. };
  378. static const struct ocotp_params imx6q_params = {
  379. .nregs = 128,
  380. .bank_address_words = 0,
  381. .set_timing = imx_ocotp_set_imx6_timing,
  382. };
  383. static const struct ocotp_params imx6sl_params = {
  384. .nregs = 64,
  385. .bank_address_words = 0,
  386. .set_timing = imx_ocotp_set_imx6_timing,
  387. };
  388. static const struct ocotp_params imx6sll_params = {
  389. .nregs = 128,
  390. .bank_address_words = 0,
  391. .set_timing = imx_ocotp_set_imx6_timing,
  392. };
  393. static const struct ocotp_params imx6sx_params = {
  394. .nregs = 128,
  395. .bank_address_words = 0,
  396. .set_timing = imx_ocotp_set_imx6_timing,
  397. };
  398. static const struct ocotp_params imx6ul_params = {
  399. .nregs = 128,
  400. .bank_address_words = 0,
  401. .set_timing = imx_ocotp_set_imx6_timing,
  402. };
  403. static const struct ocotp_params imx7d_params = {
  404. .nregs = 64,
  405. .bank_address_words = 4,
  406. .set_timing = imx_ocotp_set_imx7_timing,
  407. };
  408. static const struct of_device_id imx_ocotp_dt_ids[] = {
  409. { .compatible = "fsl,imx6q-ocotp", .data = &imx6q_params },
  410. { .compatible = "fsl,imx6sl-ocotp", .data = &imx6sl_params },
  411. { .compatible = "fsl,imx6sx-ocotp", .data = &imx6sx_params },
  412. { .compatible = "fsl,imx6ul-ocotp", .data = &imx6ul_params },
  413. { .compatible = "fsl,imx7d-ocotp", .data = &imx7d_params },
  414. { .compatible = "fsl,imx6sll-ocotp", .data = &imx6sll_params },
  415. { },
  416. };
  417. MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
  418. static int imx_ocotp_probe(struct platform_device *pdev)
  419. {
  420. struct device *dev = &pdev->dev;
  421. struct resource *res;
  422. struct ocotp_priv *priv;
  423. struct nvmem_device *nvmem;
  424. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  425. if (!priv)
  426. return -ENOMEM;
  427. priv->dev = dev;
  428. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  429. priv->base = devm_ioremap_resource(dev, res);
  430. if (IS_ERR(priv->base))
  431. return PTR_ERR(priv->base);
  432. priv->clk = devm_clk_get(dev, NULL);
  433. if (IS_ERR(priv->clk))
  434. return PTR_ERR(priv->clk);
  435. clk_prepare_enable(priv->clk);
  436. imx_ocotp_clr_err_if_set(priv->base);
  437. clk_disable_unprepare(priv->clk);
  438. priv->params = of_device_get_match_data(&pdev->dev);
  439. imx_ocotp_nvmem_config.size = 4 * priv->params->nregs;
  440. imx_ocotp_nvmem_config.dev = dev;
  441. imx_ocotp_nvmem_config.priv = priv;
  442. priv->config = &imx_ocotp_nvmem_config;
  443. nvmem = devm_nvmem_register(dev, &imx_ocotp_nvmem_config);
  444. return PTR_ERR_OR_ZERO(nvmem);
  445. }
  446. static struct platform_driver imx_ocotp_driver = {
  447. .probe = imx_ocotp_probe,
  448. .driver = {
  449. .name = "imx_ocotp",
  450. .of_match_table = imx_ocotp_dt_ids,
  451. },
  452. };
  453. module_platform_driver(imx_ocotp_driver);
  454. MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
  455. MODULE_DESCRIPTION("i.MX6/i.MX7 OCOTP fuse box driver");
  456. MODULE_LICENSE("GPL v2");