clk-sp810.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. .get_parent = clk_sp810_timerclken_get_parent,
  53. .set_parent = clk_sp810_timerclken_set_parent,
  54. };
  55. static struct clk *clk_sp810_timerclken_of_get(struct of_phandle_args *clkspec,
  56. void *data)
  57. {
  58. struct clk_sp810 *sp810 = data;
  59. if (WARN_ON(clkspec->args_count != 1 ||
  60. clkspec->args[0] >= ARRAY_SIZE(sp810->timerclken)))
  61. return NULL;
  62. return sp810->timerclken[clkspec->args[0]].clk;
  63. }
  64. static void __init clk_sp810_of_setup(struct device_node *node)
  65. {
  66. struct clk_sp810 *sp810 = kzalloc(sizeof(*sp810), GFP_KERNEL);
  67. const char *parent_names[2];
  68. int num = ARRAY_SIZE(parent_names);
  69. char name[12];
  70. struct clk_init_data init;
  71. static int instance;
  72. int i;
  73. bool deprecated;
  74. if (!sp810)
  75. return;
  76. if (of_clk_parent_fill(node, parent_names, num) != num) {
  77. pr_warn("Failed to obtain parent clocks for SP810!\n");
  78. kfree(sp810);
  79. return;
  80. }
  81. sp810->node = node;
  82. sp810->base = of_iomap(node, 0);
  83. spin_lock_init(&sp810->lock);
  84. init.name = name;
  85. init.ops = &clk_sp810_timerclken_ops;
  86. init.flags = 0;
  87. init.parent_names = parent_names;
  88. init.num_parents = num;
  89. deprecated = !of_find_property(node, "assigned-clock-parents", NULL);
  90. for (i = 0; i < ARRAY_SIZE(sp810->timerclken); i++) {
  91. snprintf(name, sizeof(name), "sp810_%d_%d", instance, i);
  92. sp810->timerclken[i].sp810 = sp810;
  93. sp810->timerclken[i].channel = i;
  94. sp810->timerclken[i].hw.init = &init;
  95. /*
  96. * If DT isn't setting the parent, force it to be
  97. * the 1 MHz clock without going through the framework.
  98. * We do this before clk_register() so that it can determine
  99. * the parent and setup the tree properly.
  100. */
  101. if (deprecated)
  102. init.ops->set_parent(&sp810->timerclken[i].hw, 1);
  103. sp810->timerclken[i].clk = clk_register(NULL,
  104. &sp810->timerclken[i].hw);
  105. WARN_ON(IS_ERR(sp810->timerclken[i].clk));
  106. }
  107. of_clk_add_provider(node, clk_sp810_timerclken_of_get, sp810);
  108. instance++;
  109. }
  110. CLK_OF_DECLARE(sp810, "arm,sp810", clk_sp810_of_setup);