imx-ocotp.c 19 KB

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