mxs-ocotp.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. .reg_read = mxs_ocotp_read,
  99. };
  100. struct mxs_data {
  101. int size;
  102. };
  103. static const struct mxs_data imx23_data = {
  104. .size = 0x220,
  105. };
  106. static const struct mxs_data imx28_data = {
  107. .size = 0x2a0,
  108. };
  109. static const struct of_device_id mxs_ocotp_match[] = {
  110. { .compatible = "fsl,imx23-ocotp", .data = &imx23_data },
  111. { .compatible = "fsl,imx28-ocotp", .data = &imx28_data },
  112. { /* sentinel */},
  113. };
  114. MODULE_DEVICE_TABLE(of, mxs_ocotp_match);
  115. static int mxs_ocotp_probe(struct platform_device *pdev)
  116. {
  117. struct device *dev = &pdev->dev;
  118. const struct mxs_data *data;
  119. struct mxs_ocotp *otp;
  120. struct resource *res;
  121. const struct of_device_id *match;
  122. int ret;
  123. match = of_match_device(dev->driver->of_match_table, dev);
  124. if (!match || !match->data)
  125. return -EINVAL;
  126. otp = devm_kzalloc(dev, sizeof(*otp), GFP_KERNEL);
  127. if (!otp)
  128. return -ENOMEM;
  129. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  130. otp->base = devm_ioremap_resource(dev, res);
  131. if (IS_ERR(otp->base))
  132. return PTR_ERR(otp->base);
  133. otp->clk = devm_clk_get(&pdev->dev, NULL);
  134. if (IS_ERR(otp->clk))
  135. return PTR_ERR(otp->clk);
  136. ret = clk_prepare(otp->clk);
  137. if (ret < 0) {
  138. dev_err(dev, "failed to prepare clk: %d\n", ret);
  139. return ret;
  140. }
  141. data = match->data;
  142. ocotp_config.size = data->size;
  143. ocotp_config.priv = otp;
  144. ocotp_config.dev = dev;
  145. otp->nvmem = nvmem_register(&ocotp_config);
  146. if (IS_ERR(otp->nvmem)) {
  147. ret = PTR_ERR(otp->nvmem);
  148. goto err_clk;
  149. }
  150. platform_set_drvdata(pdev, otp);
  151. return 0;
  152. err_clk:
  153. clk_unprepare(otp->clk);
  154. return ret;
  155. }
  156. static int mxs_ocotp_remove(struct platform_device *pdev)
  157. {
  158. struct mxs_ocotp *otp = platform_get_drvdata(pdev);
  159. clk_unprepare(otp->clk);
  160. return nvmem_unregister(otp->nvmem);
  161. }
  162. static struct platform_driver mxs_ocotp_driver = {
  163. .probe = mxs_ocotp_probe,
  164. .remove = mxs_ocotp_remove,
  165. .driver = {
  166. .name = "mxs-ocotp",
  167. .of_match_table = mxs_ocotp_match,
  168. },
  169. };
  170. module_platform_driver(mxs_ocotp_driver);
  171. MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
  172. MODULE_DESCRIPTION("driver for OCOTP in i.MX23/i.MX28");
  173. MODULE_LICENSE("GPL v2");