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. { },
  67. };
  68. MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
  69. static int imx_ocotp_probe(struct platform_device *pdev)
  70. {
  71. const struct of_device_id *of_id;
  72. struct device *dev = &pdev->dev;
  73. struct resource *res;
  74. struct ocotp_priv *priv;
  75. struct nvmem_device *nvmem;
  76. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  77. if (!priv)
  78. return -ENOMEM;
  79. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  80. priv->base = devm_ioremap_resource(dev, res);
  81. if (IS_ERR(priv->base))
  82. return PTR_ERR(priv->base);
  83. priv->clk = devm_clk_get(&pdev->dev, NULL);
  84. if (IS_ERR(priv->clk))
  85. return PTR_ERR(priv->clk);
  86. of_id = of_match_device(imx_ocotp_dt_ids, dev);
  87. priv->nregs = (unsigned long)of_id->data;
  88. imx_ocotp_nvmem_config.size = 4 * priv->nregs;
  89. imx_ocotp_nvmem_config.dev = dev;
  90. imx_ocotp_nvmem_config.priv = priv;
  91. nvmem = nvmem_register(&imx_ocotp_nvmem_config);
  92. if (IS_ERR(nvmem))
  93. return PTR_ERR(nvmem);
  94. platform_set_drvdata(pdev, nvmem);
  95. return 0;
  96. }
  97. static int imx_ocotp_remove(struct platform_device *pdev)
  98. {
  99. struct nvmem_device *nvmem = platform_get_drvdata(pdev);
  100. return nvmem_unregister(nvmem);
  101. }
  102. static struct platform_driver imx_ocotp_driver = {
  103. .probe = imx_ocotp_probe,
  104. .remove = imx_ocotp_remove,
  105. .driver = {
  106. .name = "imx_ocotp",
  107. .of_match_table = imx_ocotp_dt_ids,
  108. },
  109. };
  110. module_platform_driver(imx_ocotp_driver);
  111. MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
  112. MODULE_DESCRIPTION("i.MX6 OCOTP fuse box driver");
  113. MODULE_LICENSE("GPL v2");