clk-h8s2678.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * H8S2678 clock driver
  3. *
  4. * Copyright 2015 Yoshinori Sato <ysato@users.sourceforge.jp>
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/clkdev.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/err.h>
  10. #include <linux/device.h>
  11. #include <linux/of_address.h>
  12. static DEFINE_SPINLOCK(clklock);
  13. #define MAX_FREQ 33333333
  14. #define MIN_FREQ 8000000
  15. struct pll_clock {
  16. struct clk_hw hw;
  17. void __iomem *sckcr;
  18. void __iomem *pllcr;
  19. };
  20. #define to_pll_clock(_hw) container_of(_hw, struct pll_clock, hw)
  21. static unsigned long pll_recalc_rate(struct clk_hw *hw,
  22. unsigned long parent_rate)
  23. {
  24. struct pll_clock *pll_clock = to_pll_clock(hw);
  25. int mul = 1 << (ctrl_inb((unsigned long)pll_clock->pllcr) & 3);
  26. return parent_rate * mul;
  27. }
  28. static long pll_round_rate(struct clk_hw *hw, unsigned long rate,
  29. unsigned long *prate)
  30. {
  31. int i, m = -1;
  32. long offset[3];
  33. if (rate > MAX_FREQ)
  34. rate = MAX_FREQ;
  35. if (rate < MIN_FREQ)
  36. rate = MIN_FREQ;
  37. for (i = 0; i < 3; i++)
  38. offset[i] = abs(rate - (*prate * (1 << i)));
  39. for (i = 0; i < 3; i++)
  40. if (m < 0)
  41. m = i;
  42. else
  43. m = (offset[i] < offset[m])?i:m;
  44. return *prate * (1 << m);
  45. }
  46. static int pll_set_rate(struct clk_hw *hw, unsigned long rate,
  47. unsigned long parent_rate)
  48. {
  49. int pll;
  50. unsigned char val;
  51. unsigned long flags;
  52. struct pll_clock *pll_clock = to_pll_clock(hw);
  53. pll = ((rate / parent_rate) / 2) & 0x03;
  54. spin_lock_irqsave(&clklock, flags);
  55. val = ctrl_inb((unsigned long)pll_clock->sckcr);
  56. val |= 0x08;
  57. ctrl_outb(val, (unsigned long)pll_clock->sckcr);
  58. val = ctrl_inb((unsigned long)pll_clock->pllcr);
  59. val &= ~0x03;
  60. val |= pll;
  61. ctrl_outb(val, (unsigned long)pll_clock->pllcr);
  62. spin_unlock_irqrestore(&clklock, flags);
  63. return 0;
  64. }
  65. static const struct clk_ops pll_ops = {
  66. .recalc_rate = pll_recalc_rate,
  67. .round_rate = pll_round_rate,
  68. .set_rate = pll_set_rate,
  69. };
  70. static void __init h8s2678_pll_clk_setup(struct device_node *node)
  71. {
  72. unsigned int num_parents;
  73. struct clk *clk;
  74. const char *clk_name = node->name;
  75. const char *parent_name;
  76. struct pll_clock *pll_clock;
  77. struct clk_init_data init;
  78. num_parents = of_clk_get_parent_count(node);
  79. if (num_parents < 1) {
  80. pr_err("%s: no parent found", clk_name);
  81. return;
  82. }
  83. pll_clock = kzalloc(sizeof(struct pll_clock), GFP_KERNEL);
  84. if (!pll_clock) {
  85. pr_err("%s: failed to alloc memory", clk_name);
  86. return;
  87. }
  88. pll_clock->sckcr = of_iomap(node, 0);
  89. if (pll_clock->sckcr == NULL) {
  90. pr_err("%s: failed to map divide register", clk_name);
  91. goto free_clock;
  92. }
  93. pll_clock->pllcr = of_iomap(node, 1);
  94. if (pll_clock->pllcr == NULL) {
  95. pr_err("%s: failed to map multiply register", clk_name);
  96. goto unmap_sckcr;
  97. }
  98. parent_name = of_clk_get_parent_name(node, 0);
  99. init.name = clk_name;
  100. init.ops = &pll_ops;
  101. init.flags = CLK_IS_BASIC;
  102. init.parent_names = &parent_name;
  103. init.num_parents = 1;
  104. pll_clock->hw.init = &init;
  105. clk = clk_register(NULL, &pll_clock->hw);
  106. if (IS_ERR(clk)) {
  107. pr_err("%s: failed to register %s div clock (%ld)\n",
  108. __func__, clk_name, PTR_ERR(clk));
  109. goto unmap_pllcr;
  110. }
  111. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  112. return;
  113. unmap_pllcr:
  114. iounmap(pll_clock->pllcr);
  115. unmap_sckcr:
  116. iounmap(pll_clock->sckcr);
  117. free_clock:
  118. kfree(pll_clock);
  119. }
  120. CLK_OF_DECLARE(h8s2678_div_clk, "renesas,h8s2678-pll-clock",
  121. h8s2678_pll_clk_setup);