clk-sp810.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2013 ARM Limited
  5. */
  6. #include <linux/amba/sp810.h>
  7. #include <linux/slab.h>
  8. #include <linux/clk.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #define to_clk_sp810_timerclken(_hw) \
  15. container_of(_hw, struct clk_sp810_timerclken, hw)
  16. struct clk_sp810;
  17. struct clk_sp810_timerclken {
  18. struct clk_hw hw;
  19. struct clk *clk;
  20. struct clk_sp810 *sp810;
  21. int channel;
  22. };
  23. struct clk_sp810 {
  24. struct device_node *node;
  25. void __iomem *base;
  26. spinlock_t lock;
  27. struct clk_sp810_timerclken timerclken[4];
  28. };
  29. static u8 clk_sp810_timerclken_get_parent(struct clk_hw *hw)
  30. {
  31. struct clk_sp810_timerclken *timerclken = to_clk_sp810_timerclken(hw);
  32. u32 val = readl(timerclken->sp810->base + SCCTRL);
  33. return !!(val & (1 << SCCTRL_TIMERENnSEL_SHIFT(timerclken->channel)));
  34. }
  35. static int clk_sp810_timerclken_set_parent(struct clk_hw *hw, u8 index)
  36. {
  37. struct clk_sp810_timerclken *timerclken = to_clk_sp810_timerclken(hw);
  38. struct clk_sp810 *sp810 = timerclken->sp810;
  39. u32 val, shift = SCCTRL_TIMERENnSEL_SHIFT(timerclken->channel);
  40. unsigned long flags = 0;
  41. if (WARN_ON(index > 1))
  42. return -EINVAL;
  43. spin_lock_irqsave(&sp810->lock, flags);
  44. val = readl(sp810->base + SCCTRL);
  45. val &= ~(1 << shift);
  46. val |= index << shift;
  47. writel(val, sp810->base + SCCTRL);
  48. spin_unlock_irqrestore(&sp810->lock, flags);
  49. return 0;
  50. }
  51. static const struct clk_ops clk_sp810_timerclken_ops = {
  52. .determine_rate = clk_hw_determine_rate_no_reparent,
  53. .get_parent = clk_sp810_timerclken_get_parent,
  54. .set_parent = clk_sp810_timerclken_set_parent,
  55. };
  56. static struct clk *clk_sp810_timerclken_of_get(struct of_phandle_args *clkspec,
  57. void *data)
  58. {
  59. struct clk_sp810 *sp810 = data;
  60. if (WARN_ON(clkspec->args_count != 1 ||
  61. clkspec->args[0] >= ARRAY_SIZE(sp810->timerclken)))
  62. return NULL;
  63. return sp810->timerclken[clkspec->args[0]].clk;
  64. }
  65. static void __init clk_sp810_of_setup(struct device_node *node)
  66. {
  67. struct clk_sp810 *sp810 = kzalloc_obj(*sp810);
  68. const char *parent_names[2];
  69. int num = ARRAY_SIZE(parent_names);
  70. char name[12];
  71. struct clk_init_data init;
  72. static int instance;
  73. int i;
  74. bool deprecated;
  75. if (!sp810)
  76. return;
  77. if (of_clk_parent_fill(node, parent_names, num) != num) {
  78. pr_warn("Failed to obtain parent clocks for SP810!\n");
  79. kfree(sp810);
  80. return;
  81. }
  82. sp810->node = node;
  83. sp810->base = of_iomap(node, 0);
  84. spin_lock_init(&sp810->lock);
  85. init.name = name;
  86. init.ops = &clk_sp810_timerclken_ops;
  87. init.flags = 0;
  88. init.parent_names = parent_names;
  89. init.num_parents = num;
  90. deprecated = !of_property_present(node, "assigned-clock-parents");
  91. for (i = 0; i < ARRAY_SIZE(sp810->timerclken); i++) {
  92. snprintf(name, sizeof(name), "sp810_%d_%d", instance, i);
  93. sp810->timerclken[i].sp810 = sp810;
  94. sp810->timerclken[i].channel = i;
  95. sp810->timerclken[i].hw.init = &init;
  96. /*
  97. * If DT isn't setting the parent, force it to be
  98. * the 1 MHz clock without going through the framework.
  99. * We do this before clk_register() so that it can determine
  100. * the parent and setup the tree properly.
  101. */
  102. if (deprecated)
  103. init.ops->set_parent(&sp810->timerclken[i].hw, 1);
  104. sp810->timerclken[i].clk = clk_register(NULL,
  105. &sp810->timerclken[i].hw);
  106. WARN_ON(IS_ERR(sp810->timerclken[i].clk));
  107. }
  108. of_clk_add_provider(node, clk_sp810_timerclken_of_get, sp810);
  109. instance++;
  110. }
  111. CLK_OF_DECLARE(sp810, "arm,sp810", clk_sp810_of_setup);