reset-mpfs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PolarFire SoC (MPFS) Peripheral Clock Reset Controller
  4. *
  5. * Author: Conor Dooley <conor.dooley@microchip.com>
  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/io.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/reset-controller.h>
  17. #include <dt-bindings/clock/microchip,mpfs-clock.h>
  18. #include <soc/microchip/mpfs.h>
  19. /*
  20. * The ENVM reset is the lowest bit in the register & I am using the CLK_FOO
  21. * defines in the dt to make things easier to configure - so this is accounting
  22. * for the offset of 3 there.
  23. */
  24. #define MPFS_PERIPH_OFFSET CLK_ENVM
  25. #define MPFS_NUM_RESETS 30u
  26. #define MPFS_SLEEP_MIN_US 100
  27. #define MPFS_SLEEP_MAX_US 200
  28. /* block concurrent access to the soft reset register */
  29. static DEFINE_SPINLOCK(mpfs_reset_lock);
  30. struct mpfs_reset {
  31. void __iomem *base;
  32. struct reset_controller_dev rcdev;
  33. };
  34. static inline struct mpfs_reset *to_mpfs_reset(struct reset_controller_dev *rcdev)
  35. {
  36. return container_of(rcdev, struct mpfs_reset, rcdev);
  37. }
  38. /*
  39. * Peripheral clock resets
  40. */
  41. static int mpfs_assert(struct reset_controller_dev *rcdev, unsigned long id)
  42. {
  43. struct mpfs_reset *rst = to_mpfs_reset(rcdev);
  44. unsigned long flags;
  45. u32 reg;
  46. spin_lock_irqsave(&mpfs_reset_lock, flags);
  47. reg = readl(rst->base);
  48. reg |= BIT(id);
  49. writel(reg, rst->base);
  50. spin_unlock_irqrestore(&mpfs_reset_lock, flags);
  51. return 0;
  52. }
  53. static int mpfs_deassert(struct reset_controller_dev *rcdev, unsigned long id)
  54. {
  55. struct mpfs_reset *rst = to_mpfs_reset(rcdev);
  56. unsigned long flags;
  57. u32 reg;
  58. spin_lock_irqsave(&mpfs_reset_lock, flags);
  59. reg = readl(rst->base);
  60. reg &= ~BIT(id);
  61. writel(reg, rst->base);
  62. spin_unlock_irqrestore(&mpfs_reset_lock, flags);
  63. return 0;
  64. }
  65. static int mpfs_status(struct reset_controller_dev *rcdev, unsigned long id)
  66. {
  67. struct mpfs_reset *rst = to_mpfs_reset(rcdev);
  68. u32 reg = readl(rst->base);
  69. /*
  70. * It is safe to return here as MPFS_NUM_RESETS makes sure the sign bit
  71. * is never hit.
  72. */
  73. return (reg & BIT(id));
  74. }
  75. static int mpfs_reset(struct reset_controller_dev *rcdev, unsigned long id)
  76. {
  77. mpfs_assert(rcdev, id);
  78. usleep_range(MPFS_SLEEP_MIN_US, MPFS_SLEEP_MAX_US);
  79. mpfs_deassert(rcdev, id);
  80. return 0;
  81. }
  82. static const struct reset_control_ops mpfs_reset_ops = {
  83. .reset = mpfs_reset,
  84. .assert = mpfs_assert,
  85. .deassert = mpfs_deassert,
  86. .status = mpfs_status,
  87. };
  88. static int mpfs_reset_xlate(struct reset_controller_dev *rcdev,
  89. const struct of_phandle_args *reset_spec)
  90. {
  91. unsigned int index = reset_spec->args[0];
  92. /*
  93. * CLK_RESERVED does not map to a clock, but it does map to a reset,
  94. * so it has to be accounted for here. It is the reset for the fabric,
  95. * so if this reset gets called - do not reset it.
  96. */
  97. if (index == CLK_RESERVED) {
  98. dev_err(rcdev->dev, "Resetting the fabric is not supported\n");
  99. return -EINVAL;
  100. }
  101. if (index < MPFS_PERIPH_OFFSET || index >= (MPFS_PERIPH_OFFSET + rcdev->nr_resets)) {
  102. dev_err(rcdev->dev, "Invalid reset index %u\n", index);
  103. return -EINVAL;
  104. }
  105. return index - MPFS_PERIPH_OFFSET;
  106. }
  107. static int mpfs_reset_probe(struct auxiliary_device *adev,
  108. const struct auxiliary_device_id *id)
  109. {
  110. struct device *dev = &adev->dev;
  111. struct reset_controller_dev *rcdev;
  112. struct mpfs_reset *rst;
  113. rst = devm_kzalloc(dev, sizeof(*rst), GFP_KERNEL);
  114. if (!rst)
  115. return -ENOMEM;
  116. rst->base = (void __iomem *)adev->dev.platform_data;
  117. rcdev = &rst->rcdev;
  118. rcdev->dev = dev;
  119. rcdev->dev->parent = dev->parent;
  120. rcdev->ops = &mpfs_reset_ops;
  121. rcdev->of_node = dev->parent->of_node;
  122. rcdev->of_reset_n_cells = 1;
  123. rcdev->of_xlate = mpfs_reset_xlate;
  124. rcdev->nr_resets = MPFS_NUM_RESETS;
  125. return devm_reset_controller_register(dev, rcdev);
  126. }
  127. static void mpfs_reset_unregister_adev(void *_adev)
  128. {
  129. struct auxiliary_device *adev = _adev;
  130. auxiliary_device_delete(adev);
  131. auxiliary_device_uninit(adev);
  132. }
  133. static void mpfs_reset_adev_release(struct device *dev)
  134. {
  135. struct auxiliary_device *adev = to_auxiliary_dev(dev);
  136. kfree(adev);
  137. }
  138. static struct auxiliary_device *mpfs_reset_adev_alloc(struct device *clk_dev)
  139. {
  140. struct auxiliary_device *adev;
  141. int ret;
  142. adev = kzalloc(sizeof(*adev), GFP_KERNEL);
  143. if (!adev)
  144. return ERR_PTR(-ENOMEM);
  145. adev->name = "reset-mpfs";
  146. adev->dev.parent = clk_dev;
  147. adev->dev.release = mpfs_reset_adev_release;
  148. adev->id = 666u;
  149. ret = auxiliary_device_init(adev);
  150. if (ret) {
  151. kfree(adev);
  152. return ERR_PTR(ret);
  153. }
  154. return adev;
  155. }
  156. int mpfs_reset_controller_register(struct device *clk_dev, void __iomem *base)
  157. {
  158. struct auxiliary_device *adev;
  159. int ret;
  160. adev = mpfs_reset_adev_alloc(clk_dev);
  161. if (IS_ERR(adev))
  162. return PTR_ERR(adev);
  163. ret = auxiliary_device_add(adev);
  164. if (ret) {
  165. auxiliary_device_uninit(adev);
  166. return ret;
  167. }
  168. adev->dev.platform_data = (__force void *)base;
  169. return devm_add_action_or_reset(clk_dev, mpfs_reset_unregister_adev, adev);
  170. }
  171. EXPORT_SYMBOL_NS_GPL(mpfs_reset_controller_register, MCHP_CLK_MPFS);
  172. static const struct auxiliary_device_id mpfs_reset_ids[] = {
  173. {
  174. .name = "reset_mpfs.reset-mpfs",
  175. },
  176. { }
  177. };
  178. MODULE_DEVICE_TABLE(auxiliary, mpfs_reset_ids);
  179. static struct auxiliary_driver mpfs_reset_driver = {
  180. .probe = mpfs_reset_probe,
  181. .id_table = mpfs_reset_ids,
  182. };
  183. module_auxiliary_driver(mpfs_reset_driver);
  184. MODULE_DESCRIPTION("Microchip PolarFire SoC Reset Driver");
  185. MODULE_AUTHOR("Conor Dooley <conor.dooley@microchip.com>");
  186. MODULE_IMPORT_NS(MCHP_CLK_MPFS);