imx-ocotp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * i.MX6 OCOTP fusebox driver
  4. *
  5. * Copyright (c) 2015 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
  6. *
  7. * Copyright 2019 NXP
  8. *
  9. * Based on the barebox ocotp driver,
  10. * Copyright (c) 2010 Baruch Siach <baruch@tkos.co.il>,
  11. * Orex Computed Radiography
  12. *
  13. * Write support based on the fsl_otp driver,
  14. * Copyright (C) 2010-2013 Freescale Semiconductor, Inc
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/device.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/nvmem-provider.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/delay.h>
  25. #define IMX_OCOTP_OFFSET_B0W0 0x400 /* Offset from base address of the
  26. * OTP Bank0 Word0
  27. */
  28. #define IMX_OCOTP_OFFSET_PER_WORD 0x10 /* Offset between the start addr
  29. * of two consecutive OTP words.
  30. */
  31. #define IMX_OCOTP_ADDR_CTRL 0x0000
  32. #define IMX_OCOTP_ADDR_CTRL_SET 0x0004
  33. #define IMX_OCOTP_ADDR_CTRL_CLR 0x0008
  34. #define IMX_OCOTP_ADDR_TIMING 0x0010
  35. #define IMX_OCOTP_ADDR_DATA0 0x0020
  36. #define IMX_OCOTP_ADDR_DATA1 0x0030
  37. #define IMX_OCOTP_ADDR_DATA2 0x0040
  38. #define IMX_OCOTP_ADDR_DATA3 0x0050
  39. #define IMX_OCOTP_BM_CTRL_ADDR 0x000000FF
  40. #define IMX_OCOTP_BM_CTRL_BUSY 0x00000100
  41. #define IMX_OCOTP_BM_CTRL_ERROR 0x00000200
  42. #define IMX_OCOTP_BM_CTRL_REL_SHADOWS 0x00000400
  43. #define IMX_OCOTP_BM_CTRL_ADDR_8MP 0x000001FF
  44. #define IMX_OCOTP_BM_CTRL_BUSY_8MP 0x00000200
  45. #define IMX_OCOTP_BM_CTRL_ERROR_8MP 0x00000400
  46. #define IMX_OCOTP_BM_CTRL_REL_SHADOWS_8MP 0x00000800
  47. #define IMX_OCOTP_BM_CTRL_DEFAULT \
  48. { \
  49. .bm_addr = IMX_OCOTP_BM_CTRL_ADDR, \
  50. .bm_busy = IMX_OCOTP_BM_CTRL_BUSY, \
  51. .bm_error = IMX_OCOTP_BM_CTRL_ERROR, \
  52. .bm_rel_shadows = IMX_OCOTP_BM_CTRL_REL_SHADOWS,\
  53. }
  54. #define IMX_OCOTP_BM_CTRL_8MP \
  55. { \
  56. .bm_addr = IMX_OCOTP_BM_CTRL_ADDR_8MP, \
  57. .bm_busy = IMX_OCOTP_BM_CTRL_BUSY_8MP, \
  58. .bm_error = IMX_OCOTP_BM_CTRL_ERROR_8MP, \
  59. .bm_rel_shadows = IMX_OCOTP_BM_CTRL_REL_SHADOWS_8MP,\
  60. }
  61. #define TIMING_STROBE_PROG_US 10 /* Min time to blow a fuse */
  62. #define TIMING_STROBE_READ_NS 37 /* Min time before read */
  63. #define TIMING_RELAX_NS 17
  64. #define DEF_FSOURCE 1001 /* > 1000 ns */
  65. #define DEF_STROBE_PROG 10000 /* IPG clocks */
  66. #define IMX_OCOTP_WR_UNLOCK 0x3E770000
  67. #define IMX_OCOTP_READ_LOCKED_VAL 0xBADABADA
  68. static DEFINE_MUTEX(ocotp_mutex);
  69. struct ocotp_priv {
  70. struct device *dev;
  71. struct clk *clk;
  72. void __iomem *base;
  73. const struct ocotp_params *params;
  74. struct nvmem_config *config;
  75. };
  76. struct ocotp_ctrl_reg {
  77. u32 bm_addr;
  78. u32 bm_busy;
  79. u32 bm_error;
  80. u32 bm_rel_shadows;
  81. };
  82. struct ocotp_params {
  83. unsigned int nregs;
  84. unsigned int bank_address_words;
  85. void (*set_timing)(struct ocotp_priv *priv);
  86. struct ocotp_ctrl_reg ctrl;
  87. };
  88. static int imx_ocotp_wait_for_busy(struct ocotp_priv *priv, u32 flags)
  89. {
  90. int count;
  91. u32 c, mask;
  92. u32 bm_ctrl_busy, bm_ctrl_error;
  93. void __iomem *base = priv->base;
  94. bm_ctrl_busy = priv->params->ctrl.bm_busy;
  95. bm_ctrl_error = priv->params->ctrl.bm_error;
  96. mask = bm_ctrl_busy | bm_ctrl_error | flags;
  97. for (count = 10000; count >= 0; count--) {
  98. c = readl(base + IMX_OCOTP_ADDR_CTRL);
  99. if (!(c & mask))
  100. break;
  101. cpu_relax();
  102. }
  103. if (count < 0) {
  104. /* HW_OCOTP_CTRL[ERROR] will be set under the following
  105. * conditions:
  106. * - A write is performed to a shadow register during a shadow
  107. * reload (essentially, while HW_OCOTP_CTRL[RELOAD_SHADOWS] is
  108. * set. In addition, the contents of the shadow register shall
  109. * not be updated.
  110. * - A write is performed to a shadow register which has been
  111. * locked.
  112. * - A read is performed to from a shadow register which has
  113. * been read locked.
  114. * - A program is performed to a fuse word which has been locked
  115. * - A read is performed to from a fuse word which has been read
  116. * locked.
  117. */
  118. if (c & bm_ctrl_error)
  119. return -EPERM;
  120. return -ETIMEDOUT;
  121. }
  122. return 0;
  123. }
  124. static void imx_ocotp_clr_err_if_set(struct ocotp_priv *priv)
  125. {
  126. u32 c, bm_ctrl_error;
  127. void __iomem *base = priv->base;
  128. bm_ctrl_error = priv->params->ctrl.bm_error;
  129. c = readl(base + IMX_OCOTP_ADDR_CTRL);
  130. if (!(c & bm_ctrl_error))
  131. return;
  132. writel(bm_ctrl_error, base + IMX_OCOTP_ADDR_CTRL_CLR);
  133. }
  134. static int imx_ocotp_read(void *context, unsigned int offset,
  135. void *val, size_t bytes)
  136. {
  137. struct ocotp_priv *priv = context;
  138. unsigned int count;
  139. u8 *buf, *p;
  140. int i, ret;
  141. u32 index, num_bytes;
  142. index = offset >> 2;
  143. num_bytes = round_up((offset % 4) + bytes, 4);
  144. count = num_bytes >> 2;
  145. if (count > (priv->params->nregs - index))
  146. count = priv->params->nregs - index;
  147. p = kzalloc(num_bytes, GFP_KERNEL);
  148. if (!p)
  149. return -ENOMEM;
  150. mutex_lock(&ocotp_mutex);
  151. buf = p;
  152. ret = clk_prepare_enable(priv->clk);
  153. if (ret < 0) {
  154. mutex_unlock(&ocotp_mutex);
  155. dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
  156. kfree(p);
  157. return ret;
  158. }
  159. ret = imx_ocotp_wait_for_busy(priv, 0);
  160. if (ret < 0) {
  161. dev_err(priv->dev, "timeout during read setup\n");
  162. goto read_end;
  163. }
  164. for (i = index; i < (index + count); i++) {
  165. *(u32 *)buf = readl(priv->base + IMX_OCOTP_OFFSET_B0W0 +
  166. i * IMX_OCOTP_OFFSET_PER_WORD);
  167. /* 47.3.1.2
  168. * For "read locked" registers 0xBADABADA will be returned and
  169. * HW_OCOTP_CTRL[ERROR] will be set. It must be cleared by
  170. * software before any new write, read or reload access can be
  171. * issued
  172. */
  173. if (*((u32 *)buf) == IMX_OCOTP_READ_LOCKED_VAL)
  174. imx_ocotp_clr_err_if_set(priv);
  175. buf += 4;
  176. }
  177. index = offset % 4;
  178. memcpy(val, &p[index], bytes);
  179. read_end:
  180. clk_disable_unprepare(priv->clk);
  181. mutex_unlock(&ocotp_mutex);
  182. kfree(p);
  183. return ret;
  184. }
  185. static int imx_ocotp_cell_pp(void *context, const char *id, int index,
  186. unsigned int offset, void *data, size_t bytes)
  187. {
  188. u8 *buf = data;
  189. int i;
  190. /* Deal with some post processing of nvmem cell data */
  191. if (id && !strcmp(id, "mac-address"))
  192. for (i = 0; i < bytes / 2; i++)
  193. swap(buf[i], buf[bytes - i - 1]);
  194. return 0;
  195. }
  196. static void imx_ocotp_set_imx6_timing(struct ocotp_priv *priv)
  197. {
  198. unsigned long clk_rate;
  199. unsigned long strobe_read, relax, strobe_prog;
  200. u32 timing;
  201. /* 47.3.1.3.1
  202. * Program HW_OCOTP_TIMING[STROBE_PROG] and HW_OCOTP_TIMING[RELAX]
  203. * fields with timing values to match the current frequency of the
  204. * ipg_clk. OTP writes will work at maximum bus frequencies as long
  205. * as the HW_OCOTP_TIMING parameters are set correctly.
  206. *
  207. * Note: there are minimum timings required to ensure an OTP fuse burns
  208. * correctly that are independent of the ipg_clk. Those values are not
  209. * formally documented anywhere however, working from the minimum
  210. * timings given in u-boot we can say:
  211. *
  212. * - Minimum STROBE_PROG time is 10 microseconds. Intuitively 10
  213. * microseconds feels about right as representative of a minimum time
  214. * to physically burn out a fuse.
  215. *
  216. * - Minimum STROBE_READ i.e. the time to wait post OTP fuse burn before
  217. * performing another read is 37 nanoseconds
  218. *
  219. * - Minimum RELAX timing is 17 nanoseconds. This final RELAX minimum
  220. * timing is not entirely clear the documentation says "This
  221. * count value specifies the time to add to all default timing
  222. * parameters other than the Tpgm and Trd. It is given in number
  223. * of ipg_clk periods." where Tpgm and Trd refer to STROBE_PROG
  224. * and STROBE_READ respectively. What the other timing parameters
  225. * are though, is not specified. Experience shows a zero RELAX
  226. * value will mess up a re-load of the shadow registers post OTP
  227. * burn.
  228. */
  229. clk_rate = clk_get_rate(priv->clk);
  230. relax = DIV_ROUND_UP(clk_rate * TIMING_RELAX_NS, 1000000000) - 1;
  231. strobe_read = DIV_ROUND_UP(clk_rate * TIMING_STROBE_READ_NS,
  232. 1000000000);
  233. strobe_read += 2 * (relax + 1) - 1;
  234. strobe_prog = DIV_ROUND_CLOSEST(clk_rate * TIMING_STROBE_PROG_US,
  235. 1000000);
  236. strobe_prog += 2 * (relax + 1) - 1;
  237. timing = readl(priv->base + IMX_OCOTP_ADDR_TIMING) & 0x0FC00000;
  238. timing |= strobe_prog & 0x00000FFF;
  239. timing |= (relax << 12) & 0x0000F000;
  240. timing |= (strobe_read << 16) & 0x003F0000;
  241. writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
  242. }
  243. static void imx_ocotp_set_imx7_timing(struct ocotp_priv *priv)
  244. {
  245. unsigned long clk_rate;
  246. u64 fsource, strobe_prog;
  247. u32 timing;
  248. /* i.MX 7Solo Applications Processor Reference Manual, Rev. 0.1
  249. * 6.4.3.3
  250. */
  251. clk_rate = clk_get_rate(priv->clk);
  252. fsource = DIV_ROUND_UP_ULL((u64)clk_rate * DEF_FSOURCE,
  253. NSEC_PER_SEC) + 1;
  254. strobe_prog = DIV_ROUND_CLOSEST_ULL((u64)clk_rate * DEF_STROBE_PROG,
  255. NSEC_PER_SEC) + 1;
  256. timing = strobe_prog & 0x00000FFF;
  257. timing |= (fsource << 12) & 0x000FF000;
  258. writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
  259. }
  260. static int imx_ocotp_write(void *context, unsigned int offset, void *val,
  261. size_t bytes)
  262. {
  263. struct ocotp_priv *priv = context;
  264. u32 *buf = val;
  265. int ret;
  266. u32 ctrl;
  267. u8 waddr;
  268. u8 word = 0;
  269. /* allow only writing one complete OTP word at a time */
  270. if ((bytes != priv->config->word_size) ||
  271. (offset % priv->config->word_size))
  272. return -EINVAL;
  273. mutex_lock(&ocotp_mutex);
  274. ret = clk_prepare_enable(priv->clk);
  275. if (ret < 0) {
  276. mutex_unlock(&ocotp_mutex);
  277. dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
  278. return ret;
  279. }
  280. /* Setup the write timing values */
  281. priv->params->set_timing(priv);
  282. /* 47.3.1.3.2
  283. * Check that HW_OCOTP_CTRL[BUSY] and HW_OCOTP_CTRL[ERROR] are clear.
  284. * Overlapped accesses are not supported by the controller. Any pending
  285. * write or reload must be completed before a write access can be
  286. * requested.
  287. */
  288. ret = imx_ocotp_wait_for_busy(priv, 0);
  289. if (ret < 0) {
  290. dev_err(priv->dev, "timeout during timing setup\n");
  291. goto write_end;
  292. }
  293. /* 47.3.1.3.3
  294. * Write the requested address to HW_OCOTP_CTRL[ADDR] and program the
  295. * unlock code into HW_OCOTP_CTRL[WR_UNLOCK]. This must be programmed
  296. * for each write access. The lock code is documented in the register
  297. * description. Both the unlock code and address can be written in the
  298. * same operation.
  299. */
  300. if (priv->params->bank_address_words != 0) {
  301. /*
  302. * In banked/i.MX7 mode the OTP register bank goes into waddr
  303. * see i.MX 7Solo Applications Processor Reference Manual, Rev.
  304. * 0.1 section 6.4.3.1
  305. */
  306. offset = offset / priv->config->word_size;
  307. waddr = offset / priv->params->bank_address_words;
  308. word = offset & (priv->params->bank_address_words - 1);
  309. } else {
  310. /*
  311. * Non-banked i.MX6 mode.
  312. * OTP write/read address specifies one of 128 word address
  313. * locations
  314. */
  315. waddr = offset / 4;
  316. }
  317. ctrl = readl(priv->base + IMX_OCOTP_ADDR_CTRL);
  318. ctrl &= ~priv->params->ctrl.bm_addr;
  319. ctrl |= waddr & priv->params->ctrl.bm_addr;
  320. ctrl |= IMX_OCOTP_WR_UNLOCK;
  321. writel(ctrl, priv->base + IMX_OCOTP_ADDR_CTRL);
  322. /* 47.3.1.3.4
  323. * Write the data to the HW_OCOTP_DATA register. This will automatically
  324. * set HW_OCOTP_CTRL[BUSY] and clear HW_OCOTP_CTRL[WR_UNLOCK]. To
  325. * protect programming same OTP bit twice, before program OCOTP will
  326. * automatically read fuse value in OTP and use read value to mask
  327. * program data. The controller will use masked program data to program
  328. * a 32-bit word in the OTP per the address in HW_OCOTP_CTRL[ADDR]. Bit
  329. * fields with 1's will result in that OTP bit being programmed. Bit
  330. * fields with 0's will be ignored. At the same time that the write is
  331. * accepted, the controller makes an internal copy of
  332. * HW_OCOTP_CTRL[ADDR] which cannot be updated until the next write
  333. * sequence is initiated. This copy guarantees that erroneous writes to
  334. * HW_OCOTP_CTRL[ADDR] will not affect an active write operation. It
  335. * should also be noted that during the programming HW_OCOTP_DATA will
  336. * shift right (with zero fill). This shifting is required to program
  337. * the OTP serially. During the write operation, HW_OCOTP_DATA cannot be
  338. * modified.
  339. * Note: on i.MX7 there are four data fields to write for banked write
  340. * with the fuse blowing operation only taking place after data0
  341. * has been written. This is why data0 must always be the last
  342. * register written.
  343. */
  344. if (priv->params->bank_address_words != 0) {
  345. /* Banked/i.MX7 mode */
  346. switch (word) {
  347. case 0:
  348. writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
  349. writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
  350. writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
  351. writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0);
  352. break;
  353. case 1:
  354. writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA1);
  355. writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
  356. writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
  357. writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
  358. break;
  359. case 2:
  360. writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
  361. writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA2);
  362. writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
  363. writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
  364. break;
  365. case 3:
  366. writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
  367. writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
  368. writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA3);
  369. writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
  370. break;
  371. }
  372. } else {
  373. /* Non-banked i.MX6 mode */
  374. writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0);
  375. }
  376. /* 47.4.1.4.5
  377. * Once complete, the controller will clear BUSY. A write request to a
  378. * protected or locked region will result in no OTP access and no
  379. * setting of HW_OCOTP_CTRL[BUSY]. In addition HW_OCOTP_CTRL[ERROR] will
  380. * be set. It must be cleared by software before any new write access
  381. * can be issued.
  382. */
  383. ret = imx_ocotp_wait_for_busy(priv, 0);
  384. if (ret < 0) {
  385. if (ret == -EPERM) {
  386. dev_err(priv->dev, "failed write to locked region");
  387. imx_ocotp_clr_err_if_set(priv);
  388. } else {
  389. dev_err(priv->dev, "timeout during data write\n");
  390. }
  391. goto write_end;
  392. }
  393. /* 47.3.1.4
  394. * Write Postamble: Due to internal electrical characteristics of the
  395. * OTP during writes, all OTP operations following a write must be
  396. * separated by 2 us after the clearing of HW_OCOTP_CTRL_BUSY following
  397. * the write.
  398. */
  399. udelay(2);
  400. /* reload all shadow registers */
  401. writel(priv->params->ctrl.bm_rel_shadows,
  402. priv->base + IMX_OCOTP_ADDR_CTRL_SET);
  403. ret = imx_ocotp_wait_for_busy(priv,
  404. priv->params->ctrl.bm_rel_shadows);
  405. if (ret < 0)
  406. dev_err(priv->dev, "timeout during shadow register reload\n");
  407. write_end:
  408. clk_disable_unprepare(priv->clk);
  409. mutex_unlock(&ocotp_mutex);
  410. return ret < 0 ? ret : bytes;
  411. }
  412. static struct nvmem_config imx_ocotp_nvmem_config = {
  413. .name = "imx-ocotp",
  414. .read_only = false,
  415. .word_size = 4,
  416. .stride = 1,
  417. .reg_read = imx_ocotp_read,
  418. .reg_write = imx_ocotp_write,
  419. };
  420. static const struct ocotp_params imx6q_params = {
  421. .nregs = 128,
  422. .bank_address_words = 0,
  423. .set_timing = imx_ocotp_set_imx6_timing,
  424. .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
  425. };
  426. static const struct ocotp_params imx6sl_params = {
  427. .nregs = 64,
  428. .bank_address_words = 0,
  429. .set_timing = imx_ocotp_set_imx6_timing,
  430. .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
  431. };
  432. static const struct ocotp_params imx6sll_params = {
  433. .nregs = 80,
  434. .bank_address_words = 0,
  435. .set_timing = imx_ocotp_set_imx6_timing,
  436. .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
  437. };
  438. static const struct ocotp_params imx6sx_params = {
  439. .nregs = 128,
  440. .bank_address_words = 0,
  441. .set_timing = imx_ocotp_set_imx6_timing,
  442. .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
  443. };
  444. static const struct ocotp_params imx6ul_params = {
  445. .nregs = 144,
  446. .bank_address_words = 0,
  447. .set_timing = imx_ocotp_set_imx6_timing,
  448. .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
  449. };
  450. static const struct ocotp_params imx6ull_params = {
  451. .nregs = 80,
  452. .bank_address_words = 0,
  453. .set_timing = imx_ocotp_set_imx6_timing,
  454. .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
  455. };
  456. static const struct ocotp_params imx7d_params = {
  457. .nregs = 64,
  458. .bank_address_words = 4,
  459. .set_timing = imx_ocotp_set_imx7_timing,
  460. .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
  461. };
  462. static const struct ocotp_params imx7ulp_params = {
  463. .nregs = 256,
  464. .bank_address_words = 0,
  465. .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
  466. };
  467. static const struct ocotp_params imx8mq_params = {
  468. .nregs = 256,
  469. .bank_address_words = 0,
  470. .set_timing = imx_ocotp_set_imx6_timing,
  471. .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
  472. };
  473. static const struct ocotp_params imx8mm_params = {
  474. .nregs = 256,
  475. .bank_address_words = 0,
  476. .set_timing = imx_ocotp_set_imx6_timing,
  477. .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
  478. };
  479. static const struct ocotp_params imx8mn_params = {
  480. .nregs = 256,
  481. .bank_address_words = 0,
  482. .set_timing = imx_ocotp_set_imx6_timing,
  483. .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
  484. };
  485. static const struct ocotp_params imx8mp_params = {
  486. .nregs = 384,
  487. .bank_address_words = 0,
  488. .set_timing = imx_ocotp_set_imx6_timing,
  489. .ctrl = IMX_OCOTP_BM_CTRL_8MP,
  490. };
  491. static const struct of_device_id imx_ocotp_dt_ids[] = {
  492. { .compatible = "fsl,imx6q-ocotp", .data = &imx6q_params },
  493. { .compatible = "fsl,imx6sl-ocotp", .data = &imx6sl_params },
  494. { .compatible = "fsl,imx6sx-ocotp", .data = &imx6sx_params },
  495. { .compatible = "fsl,imx6ul-ocotp", .data = &imx6ul_params },
  496. { .compatible = "fsl,imx6ull-ocotp", .data = &imx6ull_params },
  497. { .compatible = "fsl,imx7d-ocotp", .data = &imx7d_params },
  498. { .compatible = "fsl,imx6sll-ocotp", .data = &imx6sll_params },
  499. { .compatible = "fsl,imx7ulp-ocotp", .data = &imx7ulp_params },
  500. { .compatible = "fsl,imx8mq-ocotp", .data = &imx8mq_params },
  501. { .compatible = "fsl,imx8mm-ocotp", .data = &imx8mm_params },
  502. { .compatible = "fsl,imx8mn-ocotp", .data = &imx8mn_params },
  503. { .compatible = "fsl,imx8mp-ocotp", .data = &imx8mp_params },
  504. { },
  505. };
  506. MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
  507. static void imx_ocotp_fixup_dt_cell_info(struct nvmem_device *nvmem,
  508. struct nvmem_cell_info *cell)
  509. {
  510. cell->read_post_process = imx_ocotp_cell_pp;
  511. }
  512. static int imx_ocotp_probe(struct platform_device *pdev)
  513. {
  514. struct device *dev = &pdev->dev;
  515. struct ocotp_priv *priv;
  516. struct nvmem_device *nvmem;
  517. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  518. if (!priv)
  519. return -ENOMEM;
  520. priv->dev = dev;
  521. priv->base = devm_platform_ioremap_resource(pdev, 0);
  522. if (IS_ERR(priv->base))
  523. return PTR_ERR(priv->base);
  524. priv->clk = devm_clk_get(dev, NULL);
  525. if (IS_ERR(priv->clk))
  526. return PTR_ERR(priv->clk);
  527. priv->params = of_device_get_match_data(&pdev->dev);
  528. imx_ocotp_nvmem_config.add_legacy_fixed_of_cells = true;
  529. imx_ocotp_nvmem_config.size = 4 * priv->params->nregs;
  530. imx_ocotp_nvmem_config.dev = dev;
  531. imx_ocotp_nvmem_config.priv = priv;
  532. imx_ocotp_nvmem_config.fixup_dt_cell_info = &imx_ocotp_fixup_dt_cell_info;
  533. priv->config = &imx_ocotp_nvmem_config;
  534. clk_prepare_enable(priv->clk);
  535. imx_ocotp_clr_err_if_set(priv);
  536. clk_disable_unprepare(priv->clk);
  537. nvmem = devm_nvmem_register(dev, &imx_ocotp_nvmem_config);
  538. return PTR_ERR_OR_ZERO(nvmem);
  539. }
  540. static struct platform_driver imx_ocotp_driver = {
  541. .probe = imx_ocotp_probe,
  542. .driver = {
  543. .name = "imx_ocotp",
  544. .of_match_table = imx_ocotp_dt_ids,
  545. },
  546. };
  547. module_platform_driver(imx_ocotp_driver);
  548. MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
  549. MODULE_DESCRIPTION("i.MX6/i.MX7 OCOTP fuse box driver");
  550. MODULE_LICENSE("GPL v2");