snvs_lpgpr.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2015 Pengutronix, Steffen Trumtrar <kernel@pengutronix.de>
  3. * Copyright (c) 2017 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation.
  8. */
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/module.h>
  11. #include <linux/nvmem-provider.h>
  12. #include <linux/of_device.h>
  13. #include <linux/regmap.h>
  14. #define IMX6Q_SNVS_HPLR 0x00
  15. #define IMX6Q_SNVS_LPLR 0x34
  16. #define IMX6Q_SNVS_LPGPR 0x68
  17. #define IMX7D_SNVS_HPLR 0x00
  18. #define IMX7D_SNVS_LPLR 0x34
  19. #define IMX7D_SNVS_LPGPR 0x90
  20. #define IMX_GPR_SL BIT(5)
  21. #define IMX_GPR_HL BIT(5)
  22. struct snvs_lpgpr_cfg {
  23. int offset;
  24. int offset_hplr;
  25. int offset_lplr;
  26. int size;
  27. };
  28. struct snvs_lpgpr_priv {
  29. struct device_d *dev;
  30. struct regmap *regmap;
  31. struct nvmem_config cfg;
  32. const struct snvs_lpgpr_cfg *dcfg;
  33. };
  34. static const struct snvs_lpgpr_cfg snvs_lpgpr_cfg_imx6q = {
  35. .offset = IMX6Q_SNVS_LPGPR,
  36. .offset_hplr = IMX6Q_SNVS_HPLR,
  37. .offset_lplr = IMX6Q_SNVS_LPLR,
  38. .size = 4,
  39. };
  40. static const struct snvs_lpgpr_cfg snvs_lpgpr_cfg_imx7d = {
  41. .offset = IMX7D_SNVS_LPGPR,
  42. .offset_hplr = IMX7D_SNVS_HPLR,
  43. .offset_lplr = IMX7D_SNVS_LPLR,
  44. .size = 16,
  45. };
  46. static int snvs_lpgpr_write(void *context, unsigned int offset, void *val,
  47. size_t bytes)
  48. {
  49. struct snvs_lpgpr_priv *priv = context;
  50. const struct snvs_lpgpr_cfg *dcfg = priv->dcfg;
  51. unsigned int lock_reg;
  52. int ret;
  53. ret = regmap_read(priv->regmap, dcfg->offset_hplr, &lock_reg);
  54. if (ret < 0)
  55. return ret;
  56. if (lock_reg & IMX_GPR_SL)
  57. return -EPERM;
  58. ret = regmap_read(priv->regmap, dcfg->offset_lplr, &lock_reg);
  59. if (ret < 0)
  60. return ret;
  61. if (lock_reg & IMX_GPR_HL)
  62. return -EPERM;
  63. return regmap_bulk_write(priv->regmap, dcfg->offset + offset, val,
  64. bytes / 4);
  65. }
  66. static int snvs_lpgpr_read(void *context, unsigned int offset, void *val,
  67. size_t bytes)
  68. {
  69. struct snvs_lpgpr_priv *priv = context;
  70. const struct snvs_lpgpr_cfg *dcfg = priv->dcfg;
  71. return regmap_bulk_read(priv->regmap, dcfg->offset + offset,
  72. val, bytes / 4);
  73. }
  74. static int snvs_lpgpr_probe(struct platform_device *pdev)
  75. {
  76. struct device *dev = &pdev->dev;
  77. struct device_node *node = dev->of_node;
  78. struct device_node *syscon_node;
  79. struct snvs_lpgpr_priv *priv;
  80. struct nvmem_config *cfg;
  81. struct nvmem_device *nvmem;
  82. const struct snvs_lpgpr_cfg *dcfg;
  83. if (!node)
  84. return -ENOENT;
  85. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  86. if (!priv)
  87. return -ENOMEM;
  88. dcfg = of_device_get_match_data(dev);
  89. if (!dcfg)
  90. return -EINVAL;
  91. syscon_node = of_get_parent(node);
  92. if (!syscon_node)
  93. return -ENODEV;
  94. priv->regmap = syscon_node_to_regmap(syscon_node);
  95. of_node_put(syscon_node);
  96. if (IS_ERR(priv->regmap))
  97. return PTR_ERR(priv->regmap);
  98. priv->dcfg = dcfg;
  99. cfg = &priv->cfg;
  100. cfg->priv = priv;
  101. cfg->name = dev_name(dev);
  102. cfg->dev = dev;
  103. cfg->stride = 4;
  104. cfg->word_size = 4;
  105. cfg->size = dcfg->size,
  106. cfg->owner = THIS_MODULE;
  107. cfg->reg_read = snvs_lpgpr_read;
  108. cfg->reg_write = snvs_lpgpr_write;
  109. nvmem = devm_nvmem_register(dev, cfg);
  110. return PTR_ERR_OR_ZERO(nvmem);
  111. }
  112. static const struct of_device_id snvs_lpgpr_dt_ids[] = {
  113. { .compatible = "fsl,imx6q-snvs-lpgpr", .data = &snvs_lpgpr_cfg_imx6q },
  114. { .compatible = "fsl,imx6ul-snvs-lpgpr",
  115. .data = &snvs_lpgpr_cfg_imx6q },
  116. { .compatible = "fsl,imx7d-snvs-lpgpr", .data = &snvs_lpgpr_cfg_imx7d },
  117. { },
  118. };
  119. MODULE_DEVICE_TABLE(of, snvs_lpgpr_dt_ids);
  120. static struct platform_driver snvs_lpgpr_driver = {
  121. .probe = snvs_lpgpr_probe,
  122. .driver = {
  123. .name = "snvs_lpgpr",
  124. .of_match_table = snvs_lpgpr_dt_ids,
  125. },
  126. };
  127. module_platform_driver(snvs_lpgpr_driver);
  128. MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
  129. MODULE_DESCRIPTION("Low Power General Purpose Register in i.MX6 and i.MX7 Secure Non-Volatile Storage");
  130. MODULE_LICENSE("GPL v2");