softrst.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2014 MundoReader S.L.
  3. * Author: Heiko Stuebner <heiko@sntech.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/slab.h>
  16. #include <linux/io.h>
  17. #include <linux/reset-controller.h>
  18. #include <linux/spinlock.h>
  19. #include "clk.h"
  20. struct rockchip_softrst {
  21. struct reset_controller_dev rcdev;
  22. void __iomem *reg_base;
  23. int num_regs;
  24. int num_per_reg;
  25. u8 flags;
  26. spinlock_t lock;
  27. };
  28. static int rockchip_softrst_assert(struct reset_controller_dev *rcdev,
  29. unsigned long id)
  30. {
  31. struct rockchip_softrst *softrst = container_of(rcdev,
  32. struct rockchip_softrst,
  33. rcdev);
  34. int bank = id / softrst->num_per_reg;
  35. int offset = id % softrst->num_per_reg;
  36. if (softrst->flags & ROCKCHIP_SOFTRST_HIWORD_MASK) {
  37. writel(BIT(offset) | (BIT(offset) << 16),
  38. softrst->reg_base + (bank * 4));
  39. } else {
  40. unsigned long flags;
  41. u32 reg;
  42. spin_lock_irqsave(&softrst->lock, flags);
  43. reg = readl(softrst->reg_base + (bank * 4));
  44. writel(reg | BIT(offset), softrst->reg_base + (bank * 4));
  45. spin_unlock_irqrestore(&softrst->lock, flags);
  46. }
  47. return 0;
  48. }
  49. static int rockchip_softrst_deassert(struct reset_controller_dev *rcdev,
  50. unsigned long id)
  51. {
  52. struct rockchip_softrst *softrst = container_of(rcdev,
  53. struct rockchip_softrst,
  54. rcdev);
  55. int bank = id / softrst->num_per_reg;
  56. int offset = id % softrst->num_per_reg;
  57. if (softrst->flags & ROCKCHIP_SOFTRST_HIWORD_MASK) {
  58. writel((BIT(offset) << 16), softrst->reg_base + (bank * 4));
  59. } else {
  60. unsigned long flags;
  61. u32 reg;
  62. spin_lock_irqsave(&softrst->lock, flags);
  63. reg = readl(softrst->reg_base + (bank * 4));
  64. writel(reg & ~BIT(offset), softrst->reg_base + (bank * 4));
  65. spin_unlock_irqrestore(&softrst->lock, flags);
  66. }
  67. return 0;
  68. }
  69. static const struct reset_control_ops rockchip_softrst_ops = {
  70. .assert = rockchip_softrst_assert,
  71. .deassert = rockchip_softrst_deassert,
  72. };
  73. void __init rockchip_register_softrst(struct device_node *np,
  74. unsigned int num_regs,
  75. void __iomem *base, u8 flags)
  76. {
  77. struct rockchip_softrst *softrst;
  78. int ret;
  79. softrst = kzalloc(sizeof(*softrst), GFP_KERNEL);
  80. if (!softrst)
  81. return;
  82. spin_lock_init(&softrst->lock);
  83. softrst->reg_base = base;
  84. softrst->flags = flags;
  85. softrst->num_regs = num_regs;
  86. softrst->num_per_reg = (flags & ROCKCHIP_SOFTRST_HIWORD_MASK) ? 16
  87. : 32;
  88. softrst->rcdev.owner = THIS_MODULE;
  89. softrst->rcdev.nr_resets = num_regs * softrst->num_per_reg;
  90. softrst->rcdev.ops = &rockchip_softrst_ops;
  91. softrst->rcdev.of_node = np;
  92. ret = reset_controller_register(&softrst->rcdev);
  93. if (ret) {
  94. pr_err("%s: could not register reset controller, %d\n",
  95. __func__, ret);
  96. kfree(softrst);
  97. }
  98. };