reset.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Hisilicon Reset Controller Driver
  4. *
  5. * Copyright (c) 2015-2016 HiSilicon Technologies Co., Ltd.
  6. */
  7. #include <linux/io.h>
  8. #include <linux/of_address.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/reset-controller.h>
  11. #include <linux/slab.h>
  12. #include <linux/spinlock.h>
  13. #include "reset.h"
  14. #define HISI_RESET_BIT_MASK 0x1f
  15. #define HISI_RESET_OFFSET_SHIFT 8
  16. #define HISI_RESET_OFFSET_MASK 0xffff00
  17. struct hisi_reset_controller {
  18. spinlock_t lock;
  19. void __iomem *membase;
  20. struct reset_controller_dev rcdev;
  21. };
  22. #define to_hisi_reset_controller(rcdev) \
  23. container_of(rcdev, struct hisi_reset_controller, rcdev)
  24. static int hisi_reset_of_xlate(struct reset_controller_dev *rcdev,
  25. const struct of_phandle_args *reset_spec)
  26. {
  27. u32 offset;
  28. u8 bit;
  29. offset = (reset_spec->args[0] << HISI_RESET_OFFSET_SHIFT)
  30. & HISI_RESET_OFFSET_MASK;
  31. bit = reset_spec->args[1] & HISI_RESET_BIT_MASK;
  32. return (offset | bit);
  33. }
  34. static int hisi_reset_assert(struct reset_controller_dev *rcdev,
  35. unsigned long id)
  36. {
  37. struct hisi_reset_controller *rstc = to_hisi_reset_controller(rcdev);
  38. unsigned long flags;
  39. u32 offset, reg;
  40. u8 bit;
  41. offset = (id & HISI_RESET_OFFSET_MASK) >> HISI_RESET_OFFSET_SHIFT;
  42. bit = id & HISI_RESET_BIT_MASK;
  43. spin_lock_irqsave(&rstc->lock, flags);
  44. reg = readl(rstc->membase + offset);
  45. writel(reg | BIT(bit), rstc->membase + offset);
  46. spin_unlock_irqrestore(&rstc->lock, flags);
  47. return 0;
  48. }
  49. static int hisi_reset_deassert(struct reset_controller_dev *rcdev,
  50. unsigned long id)
  51. {
  52. struct hisi_reset_controller *rstc = to_hisi_reset_controller(rcdev);
  53. unsigned long flags;
  54. u32 offset, reg;
  55. u8 bit;
  56. offset = (id & HISI_RESET_OFFSET_MASK) >> HISI_RESET_OFFSET_SHIFT;
  57. bit = id & HISI_RESET_BIT_MASK;
  58. spin_lock_irqsave(&rstc->lock, flags);
  59. reg = readl(rstc->membase + offset);
  60. writel(reg & ~BIT(bit), rstc->membase + offset);
  61. spin_unlock_irqrestore(&rstc->lock, flags);
  62. return 0;
  63. }
  64. static const struct reset_control_ops hisi_reset_ops = {
  65. .assert = hisi_reset_assert,
  66. .deassert = hisi_reset_deassert,
  67. };
  68. struct hisi_reset_controller *hisi_reset_init(struct platform_device *pdev)
  69. {
  70. struct hisi_reset_controller *rstc;
  71. rstc = devm_kmalloc(&pdev->dev, sizeof(*rstc), GFP_KERNEL);
  72. if (!rstc)
  73. return NULL;
  74. rstc->membase = devm_platform_ioremap_resource(pdev, 0);
  75. if (IS_ERR(rstc->membase))
  76. return NULL;
  77. spin_lock_init(&rstc->lock);
  78. rstc->rcdev.owner = THIS_MODULE;
  79. rstc->rcdev.ops = &hisi_reset_ops;
  80. rstc->rcdev.of_node = pdev->dev.of_node;
  81. rstc->rcdev.of_reset_n_cells = 2;
  82. rstc->rcdev.of_xlate = hisi_reset_of_xlate;
  83. reset_controller_register(&rstc->rcdev);
  84. return rstc;
  85. }
  86. EXPORT_SYMBOL_GPL(hisi_reset_init);
  87. void hisi_reset_exit(struct hisi_reset_controller *rstc)
  88. {
  89. reset_controller_unregister(&rstc->rcdev);
  90. }
  91. EXPORT_SYMBOL_GPL(hisi_reset_exit);