mxs-ocotp.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Freescale MXS On-Chip OTP driver
  3. *
  4. * Copyright (C) 2015 Stefan Wahren <stefan.wahren@i2se.com>
  5. *
  6. * Based on the driver from Huang Shijie and Christoph G. Baumann
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/delay.h>
  21. #include <linux/device.h>
  22. #include <linux/err.h>
  23. #include <linux/io.h>
  24. #include <linux/module.h>
  25. #include <linux/nvmem-provider.h>
  26. #include <linux/of_device.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <linux/stmp_device.h>
  30. /* OCOTP registers and bits */
  31. #define BM_OCOTP_CTRL_RD_BANK_OPEN BIT(12)
  32. #define BM_OCOTP_CTRL_ERROR BIT(9)
  33. #define BM_OCOTP_CTRL_BUSY BIT(8)
  34. #define OCOTP_TIMEOUT 10000
  35. #define OCOTP_DATA_OFFSET 0x20
  36. struct mxs_ocotp {
  37. struct clk *clk;
  38. void __iomem *base;
  39. struct nvmem_device *nvmem;
  40. };
  41. static int mxs_ocotp_wait(struct mxs_ocotp *otp)
  42. {
  43. int timeout = OCOTP_TIMEOUT;
  44. unsigned int status = 0;
  45. while (timeout--) {
  46. status = readl(otp->base);
  47. if (!(status & (BM_OCOTP_CTRL_BUSY | BM_OCOTP_CTRL_ERROR)))
  48. break;
  49. cpu_relax();
  50. }
  51. if (status & BM_OCOTP_CTRL_BUSY)
  52. return -EBUSY;
  53. else if (status & BM_OCOTP_CTRL_ERROR)
  54. return -EIO;
  55. return 0;
  56. }
  57. static int mxs_ocotp_read(void *context, unsigned int offset,
  58. void *val, size_t bytes)
  59. {
  60. struct mxs_ocotp *otp = context;
  61. u32 *buf = val;
  62. int ret;
  63. ret = clk_enable(otp->clk);
  64. if (ret)
  65. return ret;
  66. writel(BM_OCOTP_CTRL_ERROR, otp->base + STMP_OFFSET_REG_CLR);
  67. ret = mxs_ocotp_wait(otp);
  68. if (ret)
  69. goto disable_clk;
  70. /* open OCOTP banks for read */
  71. writel(BM_OCOTP_CTRL_RD_BANK_OPEN, otp->base + STMP_OFFSET_REG_SET);
  72. /* approximately wait 33 hclk cycles */
  73. udelay(1);
  74. ret = mxs_ocotp_wait(otp);
  75. if (ret)
  76. goto close_banks;
  77. while (bytes) {
  78. if ((offset < OCOTP_DATA_OFFSET) || (offset % 16)) {
  79. /* fill up non-data register */
  80. *buf++ = 0;
  81. } else {
  82. *buf++ = readl(otp->base + offset);
  83. }
  84. bytes -= 4;
  85. offset += 4;
  86. }
  87. close_banks:
  88. /* close banks for power saving */
  89. writel(BM_OCOTP_CTRL_RD_BANK_OPEN, otp->base + STMP_OFFSET_REG_CLR);
  90. disable_clk:
  91. clk_disable(otp->clk);
  92. return ret;
  93. }
  94. static struct nvmem_config ocotp_config = {
  95. .name = "mxs-ocotp",
  96. .stride = 16,
  97. .word_size = 4,
  98. .owner = THIS_MODULE,
  99. .reg_read = mxs_ocotp_read,
  100. };
  101. struct mxs_data {
  102. int size;
  103. };
  104. static const struct mxs_data imx23_data = {
  105. .size = 0x220,
  106. };
  107. static const struct mxs_data imx28_data = {
  108. .size = 0x2a0,
  109. };
  110. static const struct of_device_id mxs_ocotp_match[] = {
  111. { .compatible = "fsl,imx23-ocotp", .data = &imx23_data },
  112. { .compatible = "fsl,imx28-ocotp", .data = &imx28_data },
  113. { /* sentinel */},
  114. };
  115. MODULE_DEVICE_TABLE(of, mxs_ocotp_match);
  116. static int mxs_ocotp_probe(struct platform_device *pdev)
  117. {
  118. struct device *dev = &pdev->dev;
  119. const struct mxs_data *data;
  120. struct mxs_ocotp *otp;
  121. struct resource *res;
  122. const struct of_device_id *match;
  123. int ret;
  124. match = of_match_device(dev->driver->of_match_table, dev);
  125. if (!match || !match->data)
  126. return -EINVAL;
  127. otp = devm_kzalloc(dev, sizeof(*otp), GFP_KERNEL);
  128. if (!otp)
  129. return -ENOMEM;
  130. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  131. otp->base = devm_ioremap_resource(dev, res);
  132. if (IS_ERR(otp->base))
  133. return PTR_ERR(otp->base);
  134. otp->clk = devm_clk_get(&pdev->dev, NULL);
  135. if (IS_ERR(otp->clk))
  136. return PTR_ERR(otp->clk);
  137. ret = clk_prepare(otp->clk);
  138. if (ret < 0) {
  139. dev_err(dev, "failed to prepare clk: %d\n", ret);
  140. return ret;
  141. }
  142. data = match->data;
  143. ocotp_config.size = data->size;
  144. ocotp_config.priv = otp;
  145. ocotp_config.dev = dev;
  146. otp->nvmem = nvmem_register(&ocotp_config);
  147. if (IS_ERR(otp->nvmem)) {
  148. ret = PTR_ERR(otp->nvmem);
  149. goto err_clk;
  150. }
  151. platform_set_drvdata(pdev, otp);
  152. return 0;
  153. err_clk:
  154. clk_unprepare(otp->clk);
  155. return ret;
  156. }
  157. static int mxs_ocotp_remove(struct platform_device *pdev)
  158. {
  159. struct mxs_ocotp *otp = platform_get_drvdata(pdev);
  160. clk_unprepare(otp->clk);
  161. return nvmem_unregister(otp->nvmem);
  162. }
  163. static struct platform_driver mxs_ocotp_driver = {
  164. .probe = mxs_ocotp_probe,
  165. .remove = mxs_ocotp_remove,
  166. .driver = {
  167. .name = "mxs-ocotp",
  168. .of_match_table = mxs_ocotp_match,
  169. },
  170. };
  171. module_platform_driver(mxs_ocotp_driver);
  172. MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
  173. MODULE_DESCRIPTION("driver for OCOTP in i.MX23/i.MX28");
  174. MODULE_LICENSE("GPL v2");