clk-sp810.c 3.8 KB

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