reset-stm32mp1.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics 2018 - All Rights Reserved
  4. * Author: Gabriel Fernandez <gabriel.fernandez@st.com> for STMicroelectronics.
  5. */
  6. #include <linux/device.h>
  7. #include <linux/err.h>
  8. #include <linux/io.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/reset-controller.h>
  12. #define CLR_OFFSET 0x4
  13. struct stm32_reset_data {
  14. struct reset_controller_dev rcdev;
  15. void __iomem *membase;
  16. };
  17. static inline struct stm32_reset_data *
  18. to_stm32_reset_data(struct reset_controller_dev *rcdev)
  19. {
  20. return container_of(rcdev, struct stm32_reset_data, rcdev);
  21. }
  22. static int stm32_reset_update(struct reset_controller_dev *rcdev,
  23. unsigned long id, bool assert)
  24. {
  25. struct stm32_reset_data *data = to_stm32_reset_data(rcdev);
  26. int reg_width = sizeof(u32);
  27. int bank = id / (reg_width * BITS_PER_BYTE);
  28. int offset = id % (reg_width * BITS_PER_BYTE);
  29. void __iomem *addr;
  30. addr = data->membase + (bank * reg_width);
  31. if (!assert)
  32. addr += CLR_OFFSET;
  33. writel(BIT(offset), addr);
  34. return 0;
  35. }
  36. static int stm32_reset_assert(struct reset_controller_dev *rcdev,
  37. unsigned long id)
  38. {
  39. return stm32_reset_update(rcdev, id, true);
  40. }
  41. static int stm32_reset_deassert(struct reset_controller_dev *rcdev,
  42. unsigned long id)
  43. {
  44. return stm32_reset_update(rcdev, id, false);
  45. }
  46. static int stm32_reset_status(struct reset_controller_dev *rcdev,
  47. unsigned long id)
  48. {
  49. struct stm32_reset_data *data = to_stm32_reset_data(rcdev);
  50. int reg_width = sizeof(u32);
  51. int bank = id / (reg_width * BITS_PER_BYTE);
  52. int offset = id % (reg_width * BITS_PER_BYTE);
  53. u32 reg;
  54. reg = readl(data->membase + (bank * reg_width));
  55. return !!(reg & BIT(offset));
  56. }
  57. static const struct reset_control_ops stm32_reset_ops = {
  58. .assert = stm32_reset_assert,
  59. .deassert = stm32_reset_deassert,
  60. .status = stm32_reset_status,
  61. };
  62. static const struct of_device_id stm32_reset_dt_ids[] = {
  63. { .compatible = "st,stm32mp1-rcc"},
  64. { /* sentinel */ },
  65. };
  66. static int stm32_reset_probe(struct platform_device *pdev)
  67. {
  68. struct device *dev = &pdev->dev;
  69. struct stm32_reset_data *data;
  70. void __iomem *membase;
  71. struct resource *res;
  72. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  73. if (!data)
  74. return -ENOMEM;
  75. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  76. membase = devm_ioremap_resource(dev, res);
  77. if (IS_ERR(membase))
  78. return PTR_ERR(membase);
  79. data->membase = membase;
  80. data->rcdev.owner = THIS_MODULE;
  81. data->rcdev.nr_resets = resource_size(res) * BITS_PER_BYTE;
  82. data->rcdev.ops = &stm32_reset_ops;
  83. data->rcdev.of_node = dev->of_node;
  84. return devm_reset_controller_register(dev, &data->rcdev);
  85. }
  86. static struct platform_driver stm32_reset_driver = {
  87. .probe = stm32_reset_probe,
  88. .driver = {
  89. .name = "stm32mp1-reset",
  90. .of_match_table = stm32_reset_dt_ids,
  91. },
  92. };
  93. builtin_platform_driver(stm32_reset_driver);