reset-mpfs.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PolarFire SoC (MPFS) Peripheral Clock Reset Controller
  4. *
  5. * Author: Conor Dooley <[email protected]>
  6. * Copyright (c) 2022 Microchip Technology Inc. and its subsidiaries.
  7. *
  8. */
  9. #include <linux/auxiliary_bus.h>
  10. #include <linux/delay.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/reset-controller.h>
  14. #include <dt-bindings/clock/microchip,mpfs-clock.h>
  15. #include <soc/microchip/mpfs.h>
  16. /*
  17. * The ENVM reset is the lowest bit in the register & I am using the CLK_FOO
  18. * defines in the dt to make things easier to configure - so this is accounting
  19. * for the offset of 3 there.
  20. */
  21. #define MPFS_PERIPH_OFFSET CLK_ENVM
  22. #define MPFS_NUM_RESETS 30u
  23. #define MPFS_SLEEP_MIN_US 100
  24. #define MPFS_SLEEP_MAX_US 200
  25. /* block concurrent access to the soft reset register */
  26. static DEFINE_SPINLOCK(mpfs_reset_lock);
  27. /*
  28. * Peripheral clock resets
  29. */
  30. static int mpfs_assert(struct reset_controller_dev *rcdev, unsigned long id)
  31. {
  32. unsigned long flags;
  33. u32 reg;
  34. spin_lock_irqsave(&mpfs_reset_lock, flags);
  35. reg = mpfs_reset_read(rcdev->dev);
  36. reg |= BIT(id);
  37. mpfs_reset_write(rcdev->dev, reg);
  38. spin_unlock_irqrestore(&mpfs_reset_lock, flags);
  39. return 0;
  40. }
  41. static int mpfs_deassert(struct reset_controller_dev *rcdev, unsigned long id)
  42. {
  43. unsigned long flags;
  44. u32 reg;
  45. spin_lock_irqsave(&mpfs_reset_lock, flags);
  46. reg = mpfs_reset_read(rcdev->dev);
  47. reg &= ~BIT(id);
  48. mpfs_reset_write(rcdev->dev, reg);
  49. spin_unlock_irqrestore(&mpfs_reset_lock, flags);
  50. return 0;
  51. }
  52. static int mpfs_status(struct reset_controller_dev *rcdev, unsigned long id)
  53. {
  54. u32 reg = mpfs_reset_read(rcdev->dev);
  55. /*
  56. * It is safe to return here as MPFS_NUM_RESETS makes sure the sign bit
  57. * is never hit.
  58. */
  59. return (reg & BIT(id));
  60. }
  61. static int mpfs_reset(struct reset_controller_dev *rcdev, unsigned long id)
  62. {
  63. mpfs_assert(rcdev, id);
  64. usleep_range(MPFS_SLEEP_MIN_US, MPFS_SLEEP_MAX_US);
  65. mpfs_deassert(rcdev, id);
  66. return 0;
  67. }
  68. static const struct reset_control_ops mpfs_reset_ops = {
  69. .reset = mpfs_reset,
  70. .assert = mpfs_assert,
  71. .deassert = mpfs_deassert,
  72. .status = mpfs_status,
  73. };
  74. static int mpfs_reset_xlate(struct reset_controller_dev *rcdev,
  75. const struct of_phandle_args *reset_spec)
  76. {
  77. unsigned int index = reset_spec->args[0];
  78. /*
  79. * CLK_RESERVED does not map to a clock, but it does map to a reset,
  80. * so it has to be accounted for here. It is the reset for the fabric,
  81. * so if this reset gets called - do not reset it.
  82. */
  83. if (index == CLK_RESERVED) {
  84. dev_err(rcdev->dev, "Resetting the fabric is not supported\n");
  85. return -EINVAL;
  86. }
  87. if (index < MPFS_PERIPH_OFFSET || index >= (MPFS_PERIPH_OFFSET + rcdev->nr_resets)) {
  88. dev_err(rcdev->dev, "Invalid reset index %u\n", index);
  89. return -EINVAL;
  90. }
  91. return index - MPFS_PERIPH_OFFSET;
  92. }
  93. static int mpfs_reset_probe(struct auxiliary_device *adev,
  94. const struct auxiliary_device_id *id)
  95. {
  96. struct device *dev = &adev->dev;
  97. struct reset_controller_dev *rcdev;
  98. rcdev = devm_kzalloc(dev, sizeof(*rcdev), GFP_KERNEL);
  99. if (!rcdev)
  100. return -ENOMEM;
  101. rcdev->dev = dev;
  102. rcdev->dev->parent = dev->parent;
  103. rcdev->ops = &mpfs_reset_ops;
  104. rcdev->of_node = dev->parent->of_node;
  105. rcdev->of_reset_n_cells = 1;
  106. rcdev->of_xlate = mpfs_reset_xlate;
  107. rcdev->nr_resets = MPFS_NUM_RESETS;
  108. return devm_reset_controller_register(dev, rcdev);
  109. }
  110. static const struct auxiliary_device_id mpfs_reset_ids[] = {
  111. {
  112. .name = "clk_mpfs.reset-mpfs",
  113. },
  114. { }
  115. };
  116. MODULE_DEVICE_TABLE(auxiliary, mpfs_reset_ids);
  117. static struct auxiliary_driver mpfs_reset_driver = {
  118. .probe = mpfs_reset_probe,
  119. .id_table = mpfs_reset_ids,
  120. };
  121. module_auxiliary_driver(mpfs_reset_driver);
  122. MODULE_DESCRIPTION("Microchip PolarFire SoC Reset Driver");
  123. MODULE_AUTHOR("Conor Dooley <[email protected]>");
  124. MODULE_LICENSE("GPL");
  125. MODULE_IMPORT_NS(MCHP_CLK_MPFS);