imx-ocotp.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/device.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/nvmem-provider.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/regmap.h>
  25. #include <linux/slab.h>
  26. struct ocotp_priv {
  27. struct device *dev;
  28. void __iomem *base;
  29. unsigned int nregs;
  30. };
  31. static int imx_ocotp_read(void *context, const void *reg, size_t reg_size,
  32. void *val, size_t val_size)
  33. {
  34. struct ocotp_priv *priv = context;
  35. unsigned int offset = *(u32 *)reg;
  36. unsigned int count;
  37. int i;
  38. u32 index;
  39. index = offset >> 2;
  40. count = val_size >> 2;
  41. if (count > (priv->nregs - index))
  42. count = priv->nregs - index;
  43. for (i = index; i < (index + count); i++) {
  44. *(u32 *)val = readl(priv->base + 0x400 + i * 0x10);
  45. val += 4;
  46. }
  47. return (i - index) * 4;
  48. }
  49. static int imx_ocotp_write(void *context, const void *data, size_t count)
  50. {
  51. /* Not implemented */
  52. return 0;
  53. }
  54. static struct regmap_bus imx_ocotp_bus = {
  55. .read = imx_ocotp_read,
  56. .write = imx_ocotp_write,
  57. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  58. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  59. };
  60. static bool imx_ocotp_writeable_reg(struct device *dev, unsigned int reg)
  61. {
  62. return false;
  63. }
  64. static struct regmap_config imx_ocotp_regmap_config = {
  65. .reg_bits = 32,
  66. .val_bits = 32,
  67. .reg_stride = 4,
  68. .writeable_reg = imx_ocotp_writeable_reg,
  69. .name = "imx-ocotp",
  70. };
  71. static struct nvmem_config imx_ocotp_nvmem_config = {
  72. .name = "imx-ocotp",
  73. .read_only = true,
  74. .owner = THIS_MODULE,
  75. };
  76. static const struct of_device_id imx_ocotp_dt_ids[] = {
  77. { .compatible = "fsl,imx6q-ocotp", (void *)128 },
  78. { .compatible = "fsl,imx6sl-ocotp", (void *)64 },
  79. { .compatible = "fsl,imx6sx-ocotp", (void *)128 },
  80. { },
  81. };
  82. MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
  83. static int imx_ocotp_probe(struct platform_device *pdev)
  84. {
  85. const struct of_device_id *of_id;
  86. struct device *dev = &pdev->dev;
  87. struct resource *res;
  88. struct regmap *regmap;
  89. struct ocotp_priv *priv;
  90. struct nvmem_device *nvmem;
  91. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  92. if (!priv)
  93. return -ENOMEM;
  94. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  95. priv->base = devm_ioremap_resource(dev, res);
  96. if (IS_ERR(priv->base))
  97. return PTR_ERR(priv->base);
  98. of_id = of_match_device(imx_ocotp_dt_ids, dev);
  99. priv->nregs = (unsigned int)of_id->data;
  100. imx_ocotp_regmap_config.max_register = 4 * priv->nregs - 4;
  101. regmap = devm_regmap_init(dev, &imx_ocotp_bus, priv,
  102. &imx_ocotp_regmap_config);
  103. if (IS_ERR(regmap)) {
  104. dev_err(dev, "regmap init failed\n");
  105. return PTR_ERR(regmap);
  106. }
  107. imx_ocotp_nvmem_config.dev = dev;
  108. nvmem = nvmem_register(&imx_ocotp_nvmem_config);
  109. if (IS_ERR(nvmem))
  110. return PTR_ERR(nvmem);
  111. platform_set_drvdata(pdev, nvmem);
  112. return 0;
  113. }
  114. static int imx_ocotp_remove(struct platform_device *pdev)
  115. {
  116. struct nvmem_device *nvmem = platform_get_drvdata(pdev);
  117. return nvmem_unregister(nvmem);
  118. }
  119. static struct platform_driver imx_ocotp_driver = {
  120. .probe = imx_ocotp_probe,
  121. .remove = imx_ocotp_remove,
  122. .driver = {
  123. .name = "imx_ocotp",
  124. .of_match_table = imx_ocotp_dt_ids,
  125. },
  126. };
  127. module_platform_driver(imx_ocotp_driver);
  128. MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
  129. MODULE_DESCRIPTION("i.MX6 OCOTP fuse box driver");
  130. MODULE_LICENSE("GPL v2");