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