imx-ocotp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * i.MX6 OCOTP fusebox driver
  3. *
  4. * Copyright (c) 2015 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
  5. *
  6. * Based on the barebox ocotp driver,
  7. * Copyright (c) 2010 Baruch Siach <baruch@tkos.co.il>,
  8. * Orex Computed Radiography
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation.
  13. *
  14. * http://www.opensource.org/licenses/gpl-license.html
  15. * http://www.gnu.org/copyleft/gpl.html
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/device.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/nvmem-provider.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. struct ocotp_priv {
  27. struct device *dev;
  28. struct clk *clk;
  29. void __iomem *base;
  30. unsigned int nregs;
  31. };
  32. static int imx_ocotp_read(void *context, unsigned int offset,
  33. void *val, size_t bytes)
  34. {
  35. struct ocotp_priv *priv = context;
  36. unsigned int count;
  37. u32 *buf = val;
  38. int i, ret;
  39. u32 index;
  40. index = offset >> 2;
  41. count = bytes >> 2;
  42. if (count > (priv->nregs - index))
  43. count = priv->nregs - index;
  44. ret = clk_prepare_enable(priv->clk);
  45. if (ret < 0) {
  46. dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
  47. return ret;
  48. }
  49. for (i = index; i < (index + count); i++)
  50. *buf++ = readl(priv->base + 0x400 + i * 0x10);
  51. clk_disable_unprepare(priv->clk);
  52. return 0;
  53. }
  54. static struct nvmem_config imx_ocotp_nvmem_config = {
  55. .name = "imx-ocotp",
  56. .read_only = true,
  57. .word_size = 4,
  58. .stride = 4,
  59. .owner = THIS_MODULE,
  60. .reg_read = imx_ocotp_read,
  61. };
  62. static const struct of_device_id imx_ocotp_dt_ids[] = {
  63. { .compatible = "fsl,imx6q-ocotp", (void *)128 },
  64. { .compatible = "fsl,imx6sl-ocotp", (void *)64 },
  65. { .compatible = "fsl,imx6sx-ocotp", (void *)128 },
  66. { .compatible = "fsl,imx6ul-ocotp", (void *)128 },
  67. { },
  68. };
  69. MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
  70. static int imx_ocotp_probe(struct platform_device *pdev)
  71. {
  72. const struct of_device_id *of_id;
  73. struct device *dev = &pdev->dev;
  74. struct resource *res;
  75. struct ocotp_priv *priv;
  76. struct nvmem_device *nvmem;
  77. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  78. if (!priv)
  79. return -ENOMEM;
  80. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  81. priv->base = devm_ioremap_resource(dev, res);
  82. if (IS_ERR(priv->base))
  83. return PTR_ERR(priv->base);
  84. priv->clk = devm_clk_get(&pdev->dev, NULL);
  85. if (IS_ERR(priv->clk))
  86. return PTR_ERR(priv->clk);
  87. of_id = of_match_device(imx_ocotp_dt_ids, dev);
  88. priv->nregs = (unsigned long)of_id->data;
  89. imx_ocotp_nvmem_config.size = 4 * priv->nregs;
  90. imx_ocotp_nvmem_config.dev = dev;
  91. imx_ocotp_nvmem_config.priv = priv;
  92. nvmem = nvmem_register(&imx_ocotp_nvmem_config);
  93. if (IS_ERR(nvmem))
  94. return PTR_ERR(nvmem);
  95. platform_set_drvdata(pdev, nvmem);
  96. return 0;
  97. }
  98. static int imx_ocotp_remove(struct platform_device *pdev)
  99. {
  100. struct nvmem_device *nvmem = platform_get_drvdata(pdev);
  101. return nvmem_unregister(nvmem);
  102. }
  103. static struct platform_driver imx_ocotp_driver = {
  104. .probe = imx_ocotp_probe,
  105. .remove = imx_ocotp_remove,
  106. .driver = {
  107. .name = "imx_ocotp",
  108. .of_match_table = imx_ocotp_dt_ids,
  109. },
  110. };
  111. module_platform_driver(imx_ocotp_driver);
  112. MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
  113. MODULE_DESCRIPTION("i.MX6 OCOTP fuse box driver");
  114. MODULE_LICENSE("GPL v2");