mxs-ocotp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Freescale MXS On-Chip OTP driver
  4. *
  5. * Copyright (C) 2015 Stefan Wahren <stefan.wahren@i2se.com>
  6. *
  7. * Based on the driver from Huang Shijie and Christoph G. Baumann
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/nvmem-provider.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/stmp_device.h>
  20. /* OCOTP registers and bits */
  21. #define BM_OCOTP_CTRL_RD_BANK_OPEN BIT(12)
  22. #define BM_OCOTP_CTRL_ERROR BIT(9)
  23. #define BM_OCOTP_CTRL_BUSY BIT(8)
  24. #define OCOTP_TIMEOUT 10000
  25. #define OCOTP_DATA_OFFSET 0x20
  26. struct mxs_ocotp {
  27. struct clk *clk;
  28. void __iomem *base;
  29. struct nvmem_device *nvmem;
  30. };
  31. static int mxs_ocotp_wait(struct mxs_ocotp *otp)
  32. {
  33. int timeout = OCOTP_TIMEOUT;
  34. unsigned int status = 0;
  35. while (timeout--) {
  36. status = readl(otp->base);
  37. if (!(status & (BM_OCOTP_CTRL_BUSY | BM_OCOTP_CTRL_ERROR)))
  38. break;
  39. cpu_relax();
  40. }
  41. if (status & BM_OCOTP_CTRL_BUSY)
  42. return -EBUSY;
  43. else if (status & BM_OCOTP_CTRL_ERROR)
  44. return -EIO;
  45. return 0;
  46. }
  47. static int mxs_ocotp_read(void *context, unsigned int offset,
  48. void *val, size_t bytes)
  49. {
  50. struct mxs_ocotp *otp = context;
  51. u32 *buf = val;
  52. int ret;
  53. ret = clk_enable(otp->clk);
  54. if (ret)
  55. return ret;
  56. writel(BM_OCOTP_CTRL_ERROR, otp->base + STMP_OFFSET_REG_CLR);
  57. ret = mxs_ocotp_wait(otp);
  58. if (ret)
  59. goto disable_clk;
  60. /* open OCOTP banks for read */
  61. writel(BM_OCOTP_CTRL_RD_BANK_OPEN, otp->base + STMP_OFFSET_REG_SET);
  62. /* approximately wait 33 hclk cycles */
  63. udelay(1);
  64. ret = mxs_ocotp_wait(otp);
  65. if (ret)
  66. goto close_banks;
  67. while (bytes) {
  68. if ((offset < OCOTP_DATA_OFFSET) || (offset % 16)) {
  69. /* fill up non-data register */
  70. *buf++ = 0;
  71. } else {
  72. *buf++ = readl(otp->base + offset);
  73. }
  74. bytes -= 4;
  75. offset += 4;
  76. }
  77. close_banks:
  78. /* close banks for power saving */
  79. writel(BM_OCOTP_CTRL_RD_BANK_OPEN, otp->base + STMP_OFFSET_REG_CLR);
  80. disable_clk:
  81. clk_disable(otp->clk);
  82. return ret;
  83. }
  84. static struct nvmem_config ocotp_config = {
  85. .name = "mxs-ocotp",
  86. .stride = 16,
  87. .word_size = 4,
  88. .reg_read = mxs_ocotp_read,
  89. };
  90. struct mxs_data {
  91. int size;
  92. };
  93. static const struct mxs_data imx23_data = {
  94. .size = 0x220,
  95. };
  96. static const struct mxs_data imx28_data = {
  97. .size = 0x2a0,
  98. };
  99. static const struct of_device_id mxs_ocotp_match[] = {
  100. { .compatible = "fsl,imx23-ocotp", .data = &imx23_data },
  101. { .compatible = "fsl,imx28-ocotp", .data = &imx28_data },
  102. { /* sentinel */},
  103. };
  104. MODULE_DEVICE_TABLE(of, mxs_ocotp_match);
  105. static void mxs_ocotp_action(void *data)
  106. {
  107. clk_unprepare(data);
  108. }
  109. static int mxs_ocotp_probe(struct platform_device *pdev)
  110. {
  111. struct device *dev = &pdev->dev;
  112. const struct mxs_data *data;
  113. struct mxs_ocotp *otp;
  114. const struct of_device_id *match;
  115. int ret;
  116. match = of_match_device(dev->driver->of_match_table, dev);
  117. if (!match || !match->data)
  118. return -EINVAL;
  119. otp = devm_kzalloc(dev, sizeof(*otp), GFP_KERNEL);
  120. if (!otp)
  121. return -ENOMEM;
  122. otp->base = devm_platform_ioremap_resource(pdev, 0);
  123. if (IS_ERR(otp->base))
  124. return PTR_ERR(otp->base);
  125. otp->clk = devm_clk_get(&pdev->dev, NULL);
  126. if (IS_ERR(otp->clk))
  127. return PTR_ERR(otp->clk);
  128. ret = clk_prepare(otp->clk);
  129. if (ret < 0) {
  130. dev_err(dev, "failed to prepare clk: %d\n", ret);
  131. return ret;
  132. }
  133. ret = devm_add_action_or_reset(&pdev->dev, mxs_ocotp_action, otp->clk);
  134. if (ret)
  135. return ret;
  136. data = match->data;
  137. ocotp_config.size = data->size;
  138. ocotp_config.priv = otp;
  139. ocotp_config.dev = dev;
  140. otp->nvmem = devm_nvmem_register(dev, &ocotp_config);
  141. if (IS_ERR(otp->nvmem))
  142. return PTR_ERR(otp->nvmem);
  143. platform_set_drvdata(pdev, otp);
  144. return 0;
  145. }
  146. static struct platform_driver mxs_ocotp_driver = {
  147. .probe = mxs_ocotp_probe,
  148. .driver = {
  149. .name = "mxs-ocotp",
  150. .of_match_table = mxs_ocotp_match,
  151. },
  152. };
  153. module_platform_driver(mxs_ocotp_driver);
  154. MODULE_AUTHOR("Stefan Wahren <wahrenst@gmx.net");
  155. MODULE_DESCRIPTION("driver for OCOTP in i.MX23/i.MX28");
  156. MODULE_LICENSE("GPL v2");