reset.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Hisilicon Reset Controller Driver
  3. *
  4. * Copyright (c) 2015-2016 HiSilicon Technologies Co., Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/io.h>
  20. #include <linux/of_address.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/reset-controller.h>
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. #include "reset.h"
  26. #define HISI_RESET_BIT_MASK 0x1f
  27. #define HISI_RESET_OFFSET_SHIFT 8
  28. #define HISI_RESET_OFFSET_MASK 0xffff00
  29. struct hisi_reset_controller {
  30. spinlock_t lock;
  31. void __iomem *membase;
  32. struct reset_controller_dev rcdev;
  33. };
  34. #define to_hisi_reset_controller(rcdev) \
  35. container_of(rcdev, struct hisi_reset_controller, rcdev)
  36. static int hisi_reset_of_xlate(struct reset_controller_dev *rcdev,
  37. const struct of_phandle_args *reset_spec)
  38. {
  39. u32 offset;
  40. u8 bit;
  41. offset = (reset_spec->args[0] << HISI_RESET_OFFSET_SHIFT)
  42. & HISI_RESET_OFFSET_MASK;
  43. bit = reset_spec->args[1] & HISI_RESET_BIT_MASK;
  44. return (offset | bit);
  45. }
  46. static int hisi_reset_assert(struct reset_controller_dev *rcdev,
  47. unsigned long id)
  48. {
  49. struct hisi_reset_controller *rstc = to_hisi_reset_controller(rcdev);
  50. unsigned long flags;
  51. u32 offset, reg;
  52. u8 bit;
  53. offset = (id & HISI_RESET_OFFSET_MASK) >> HISI_RESET_OFFSET_SHIFT;
  54. bit = id & HISI_RESET_BIT_MASK;
  55. spin_lock_irqsave(&rstc->lock, flags);
  56. reg = readl(rstc->membase + offset);
  57. writel(reg | BIT(bit), rstc->membase + offset);
  58. spin_unlock_irqrestore(&rstc->lock, flags);
  59. return 0;
  60. }
  61. static int hisi_reset_deassert(struct reset_controller_dev *rcdev,
  62. unsigned long id)
  63. {
  64. struct hisi_reset_controller *rstc = to_hisi_reset_controller(rcdev);
  65. unsigned long flags;
  66. u32 offset, reg;
  67. u8 bit;
  68. offset = (id & HISI_RESET_OFFSET_MASK) >> HISI_RESET_OFFSET_SHIFT;
  69. bit = id & HISI_RESET_BIT_MASK;
  70. spin_lock_irqsave(&rstc->lock, flags);
  71. reg = readl(rstc->membase + offset);
  72. writel(reg & ~BIT(bit), rstc->membase + offset);
  73. spin_unlock_irqrestore(&rstc->lock, flags);
  74. return 0;
  75. }
  76. static const struct reset_control_ops hisi_reset_ops = {
  77. .assert = hisi_reset_assert,
  78. .deassert = hisi_reset_deassert,
  79. };
  80. struct hisi_reset_controller *hisi_reset_init(struct platform_device *pdev)
  81. {
  82. struct hisi_reset_controller *rstc;
  83. struct resource *res;
  84. rstc = devm_kmalloc(&pdev->dev, sizeof(*rstc), GFP_KERNEL);
  85. if (!rstc)
  86. return NULL;
  87. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  88. rstc->membase = devm_ioremap(&pdev->dev,
  89. res->start, resource_size(res));
  90. if (!rstc->membase)
  91. return NULL;
  92. spin_lock_init(&rstc->lock);
  93. rstc->rcdev.owner = THIS_MODULE;
  94. rstc->rcdev.ops = &hisi_reset_ops;
  95. rstc->rcdev.of_node = pdev->dev.of_node;
  96. rstc->rcdev.of_reset_n_cells = 2;
  97. rstc->rcdev.of_xlate = hisi_reset_of_xlate;
  98. reset_controller_register(&rstc->rcdev);
  99. return rstc;
  100. }
  101. EXPORT_SYMBOL_GPL(hisi_reset_init);
  102. void hisi_reset_exit(struct hisi_reset_controller *rstc)
  103. {
  104. reset_controller_unregister(&rstc->rcdev);
  105. }
  106. EXPORT_SYMBOL_GPL(hisi_reset_exit);