imx-ocotp.c 14 KB

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