reset.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <linux/slab.h>
  2. #include <linux/io.h>
  3. #include <linux/of.h>
  4. #include <linux/of_address.h>
  5. #include <linux/reset-controller.h>
  6. #include "reset.h"
  7. #define rcdev_to_unit(rcdev) container_of(rcdev, struct mmp_clk_reset_unit, rcdev)
  8. static int mmp_of_reset_xlate(struct reset_controller_dev *rcdev,
  9. const struct of_phandle_args *reset_spec)
  10. {
  11. struct mmp_clk_reset_unit *unit = rcdev_to_unit(rcdev);
  12. struct mmp_clk_reset_cell *cell;
  13. int i;
  14. if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
  15. return -EINVAL;
  16. for (i = 0; i < rcdev->nr_resets; i++) {
  17. cell = &unit->cells[i];
  18. if (cell->clk_id == reset_spec->args[0])
  19. break;
  20. }
  21. if (i == rcdev->nr_resets)
  22. return -EINVAL;
  23. return i;
  24. }
  25. static int mmp_clk_reset_assert(struct reset_controller_dev *rcdev,
  26. unsigned long id)
  27. {
  28. struct mmp_clk_reset_unit *unit = rcdev_to_unit(rcdev);
  29. struct mmp_clk_reset_cell *cell;
  30. unsigned long flags = 0;
  31. u32 val;
  32. cell = &unit->cells[id];
  33. if (cell->lock)
  34. spin_lock_irqsave(cell->lock, flags);
  35. val = readl(cell->reg);
  36. val |= cell->bits;
  37. writel(val, cell->reg);
  38. if (cell->lock)
  39. spin_unlock_irqrestore(cell->lock, flags);
  40. return 0;
  41. }
  42. static int mmp_clk_reset_deassert(struct reset_controller_dev *rcdev,
  43. unsigned long id)
  44. {
  45. struct mmp_clk_reset_unit *unit = rcdev_to_unit(rcdev);
  46. struct mmp_clk_reset_cell *cell;
  47. unsigned long flags = 0;
  48. u32 val;
  49. cell = &unit->cells[id];
  50. if (cell->lock)
  51. spin_lock_irqsave(cell->lock, flags);
  52. val = readl(cell->reg);
  53. val &= ~cell->bits;
  54. writel(val, cell->reg);
  55. if (cell->lock)
  56. spin_unlock_irqrestore(cell->lock, flags);
  57. return 0;
  58. }
  59. static struct reset_control_ops mmp_clk_reset_ops = {
  60. .assert = mmp_clk_reset_assert,
  61. .deassert = mmp_clk_reset_deassert,
  62. };
  63. void mmp_clk_reset_register(struct device_node *np,
  64. struct mmp_clk_reset_cell *cells, int nr_resets)
  65. {
  66. struct mmp_clk_reset_unit *unit;
  67. unit = kzalloc(sizeof(*unit), GFP_KERNEL);
  68. if (!unit)
  69. return;
  70. unit->cells = cells;
  71. unit->rcdev.of_reset_n_cells = 1;
  72. unit->rcdev.nr_resets = nr_resets;
  73. unit->rcdev.ops = &mmp_clk_reset_ops;
  74. unit->rcdev.of_node = np;
  75. unit->rcdev.of_xlate = mmp_of_reset_xlate;
  76. reset_controller_register(&unit->rcdev);
  77. }