mxs-ocotp.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/regmap.h>
  29. #include <linux/slab.h>
  30. #include <linux/stmp_device.h>
  31. /* OCOTP registers and bits */
  32. #define BM_OCOTP_CTRL_RD_BANK_OPEN BIT(12)
  33. #define BM_OCOTP_CTRL_ERROR BIT(9)
  34. #define BM_OCOTP_CTRL_BUSY BIT(8)
  35. #define OCOTP_TIMEOUT 10000
  36. #define OCOTP_DATA_OFFSET 0x20
  37. struct mxs_ocotp {
  38. struct clk *clk;
  39. void __iomem *base;
  40. struct nvmem_device *nvmem;
  41. };
  42. static int mxs_ocotp_wait(struct mxs_ocotp *otp)
  43. {
  44. int timeout = OCOTP_TIMEOUT;
  45. unsigned int status = 0;
  46. while (timeout--) {
  47. status = readl(otp->base);
  48. if (!(status & (BM_OCOTP_CTRL_BUSY | BM_OCOTP_CTRL_ERROR)))
  49. break;
  50. cpu_relax();
  51. }
  52. if (status & BM_OCOTP_CTRL_BUSY)
  53. return -EBUSY;
  54. else if (status & BM_OCOTP_CTRL_ERROR)
  55. return -EIO;
  56. return 0;
  57. }
  58. static int mxs_ocotp_read(void *context, const void *reg, size_t reg_size,
  59. void *val, size_t val_size)
  60. {
  61. struct mxs_ocotp *otp = context;
  62. unsigned int offset = *(u32 *)reg;
  63. u32 *buf = val;
  64. int ret;
  65. ret = clk_enable(otp->clk);
  66. if (ret)
  67. return ret;
  68. writel(BM_OCOTP_CTRL_ERROR, otp->base + STMP_OFFSET_REG_CLR);
  69. ret = mxs_ocotp_wait(otp);
  70. if (ret)
  71. goto disable_clk;
  72. /* open OCOTP banks for read */
  73. writel(BM_OCOTP_CTRL_RD_BANK_OPEN, otp->base + STMP_OFFSET_REG_SET);
  74. /* approximately wait 33 hclk cycles */
  75. udelay(1);
  76. ret = mxs_ocotp_wait(otp);
  77. if (ret)
  78. goto close_banks;
  79. while (val_size >= reg_size) {
  80. if ((offset < OCOTP_DATA_OFFSET) || (offset % 16)) {
  81. /* fill up non-data register */
  82. *buf = 0;
  83. } else {
  84. *buf = readl(otp->base + offset);
  85. }
  86. buf++;
  87. val_size -= reg_size;
  88. offset += reg_size;
  89. }
  90. close_banks:
  91. /* close banks for power saving */
  92. writel(BM_OCOTP_CTRL_RD_BANK_OPEN, otp->base + STMP_OFFSET_REG_CLR);
  93. disable_clk:
  94. clk_disable(otp->clk);
  95. return ret;
  96. }
  97. static int mxs_ocotp_write(void *context, const void *data, size_t count)
  98. {
  99. /* We don't want to support writing */
  100. return 0;
  101. }
  102. static bool mxs_ocotp_writeable_reg(struct device *dev, unsigned int reg)
  103. {
  104. return false;
  105. }
  106. static struct nvmem_config ocotp_config = {
  107. .name = "mxs-ocotp",
  108. .owner = THIS_MODULE,
  109. };
  110. static const struct regmap_range imx23_ranges[] = {
  111. regmap_reg_range(OCOTP_DATA_OFFSET, 0x210),
  112. };
  113. static const struct regmap_access_table imx23_access = {
  114. .yes_ranges = imx23_ranges,
  115. .n_yes_ranges = ARRAY_SIZE(imx23_ranges),
  116. };
  117. static const struct regmap_range imx28_ranges[] = {
  118. regmap_reg_range(OCOTP_DATA_OFFSET, 0x290),
  119. };
  120. static const struct regmap_access_table imx28_access = {
  121. .yes_ranges = imx28_ranges,
  122. .n_yes_ranges = ARRAY_SIZE(imx28_ranges),
  123. };
  124. static struct regmap_bus mxs_ocotp_bus = {
  125. .read = mxs_ocotp_read,
  126. .write = mxs_ocotp_write, /* make regmap_init() happy */
  127. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  128. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  129. };
  130. static struct regmap_config mxs_ocotp_config = {
  131. .reg_bits = 32,
  132. .val_bits = 32,
  133. .reg_stride = 16,
  134. .writeable_reg = mxs_ocotp_writeable_reg,
  135. };
  136. static const struct of_device_id mxs_ocotp_match[] = {
  137. { .compatible = "fsl,imx23-ocotp", .data = &imx23_access },
  138. { .compatible = "fsl,imx28-ocotp", .data = &imx28_access },
  139. { /* sentinel */},
  140. };
  141. MODULE_DEVICE_TABLE(of, mxs_ocotp_match);
  142. static int mxs_ocotp_probe(struct platform_device *pdev)
  143. {
  144. struct device *dev = &pdev->dev;
  145. struct mxs_ocotp *otp;
  146. struct resource *res;
  147. const struct of_device_id *match;
  148. struct regmap *regmap;
  149. const struct regmap_access_table *access;
  150. int ret;
  151. match = of_match_device(dev->driver->of_match_table, dev);
  152. if (!match || !match->data)
  153. return -EINVAL;
  154. otp = devm_kzalloc(dev, sizeof(*otp), GFP_KERNEL);
  155. if (!otp)
  156. return -ENOMEM;
  157. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  158. otp->base = devm_ioremap_resource(dev, res);
  159. if (IS_ERR(otp->base))
  160. return PTR_ERR(otp->base);
  161. otp->clk = devm_clk_get(&pdev->dev, NULL);
  162. if (IS_ERR(otp->clk))
  163. return PTR_ERR(otp->clk);
  164. ret = clk_prepare(otp->clk);
  165. if (ret < 0) {
  166. dev_err(dev, "failed to prepare clk: %d\n", ret);
  167. return ret;
  168. }
  169. access = match->data;
  170. mxs_ocotp_config.rd_table = access;
  171. mxs_ocotp_config.max_register = access->yes_ranges[0].range_max;
  172. regmap = devm_regmap_init(dev, &mxs_ocotp_bus, otp, &mxs_ocotp_config);
  173. if (IS_ERR(regmap)) {
  174. dev_err(dev, "regmap init failed\n");
  175. ret = PTR_ERR(regmap);
  176. goto err_clk;
  177. }
  178. ocotp_config.dev = dev;
  179. otp->nvmem = nvmem_register(&ocotp_config);
  180. if (IS_ERR(otp->nvmem)) {
  181. ret = PTR_ERR(otp->nvmem);
  182. goto err_clk;
  183. }
  184. platform_set_drvdata(pdev, otp);
  185. return 0;
  186. err_clk:
  187. clk_unprepare(otp->clk);
  188. return ret;
  189. }
  190. static int mxs_ocotp_remove(struct platform_device *pdev)
  191. {
  192. struct mxs_ocotp *otp = platform_get_drvdata(pdev);
  193. clk_unprepare(otp->clk);
  194. return nvmem_unregister(otp->nvmem);
  195. }
  196. static struct platform_driver mxs_ocotp_driver = {
  197. .probe = mxs_ocotp_probe,
  198. .remove = mxs_ocotp_remove,
  199. .driver = {
  200. .name = "mxs-ocotp",
  201. .of_match_table = mxs_ocotp_match,
  202. },
  203. };
  204. module_platform_driver(mxs_ocotp_driver);
  205. MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
  206. MODULE_DESCRIPTION("driver for OCOTP in i.MX23/i.MX28");
  207. MODULE_LICENSE("GPL v2");