clk-main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/clk/at91_pmc.h>
  13. #include <linux/delay.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/io.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/irq.h>
  20. #include <linux/sched.h>
  21. #include <linux/wait.h>
  22. #include "pmc.h"
  23. #define SLOW_CLOCK_FREQ 32768
  24. #define MAINF_DIV 16
  25. #define MAINFRDY_TIMEOUT (((MAINF_DIV + 1) * USEC_PER_SEC) / \
  26. SLOW_CLOCK_FREQ)
  27. #define MAINF_LOOP_MIN_WAIT (USEC_PER_SEC / SLOW_CLOCK_FREQ)
  28. #define MAINF_LOOP_MAX_WAIT MAINFRDY_TIMEOUT
  29. struct clk_main {
  30. struct clk_hw hw;
  31. struct at91_pmc *pmc;
  32. unsigned long rate;
  33. unsigned int irq;
  34. wait_queue_head_t wait;
  35. };
  36. #define to_clk_main(hw) container_of(hw, struct clk_main, hw)
  37. static irqreturn_t clk_main_irq_handler(int irq, void *dev_id)
  38. {
  39. struct clk_main *clkmain = (struct clk_main *)dev_id;
  40. wake_up(&clkmain->wait);
  41. disable_irq_nosync(clkmain->irq);
  42. return IRQ_HANDLED;
  43. }
  44. static int clk_main_prepare(struct clk_hw *hw)
  45. {
  46. struct clk_main *clkmain = to_clk_main(hw);
  47. struct at91_pmc *pmc = clkmain->pmc;
  48. unsigned long halt_time, timeout;
  49. u32 tmp;
  50. while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS)) {
  51. enable_irq(clkmain->irq);
  52. wait_event(clkmain->wait,
  53. pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS);
  54. }
  55. if (clkmain->rate)
  56. return 0;
  57. timeout = jiffies + usecs_to_jiffies(MAINFRDY_TIMEOUT);
  58. do {
  59. halt_time = jiffies;
  60. tmp = pmc_read(pmc, AT91_CKGR_MCFR);
  61. if (tmp & AT91_PMC_MAINRDY)
  62. return 0;
  63. usleep_range(MAINF_LOOP_MIN_WAIT, MAINF_LOOP_MAX_WAIT);
  64. } while (time_before(halt_time, timeout));
  65. return 0;
  66. }
  67. static int clk_main_is_prepared(struct clk_hw *hw)
  68. {
  69. struct clk_main *clkmain = to_clk_main(hw);
  70. return !!(pmc_read(clkmain->pmc, AT91_PMC_SR) & AT91_PMC_MOSCS);
  71. }
  72. static unsigned long clk_main_recalc_rate(struct clk_hw *hw,
  73. unsigned long parent_rate)
  74. {
  75. u32 tmp;
  76. struct clk_main *clkmain = to_clk_main(hw);
  77. struct at91_pmc *pmc = clkmain->pmc;
  78. if (clkmain->rate)
  79. return clkmain->rate;
  80. tmp = pmc_read(pmc, AT91_CKGR_MCFR) & AT91_PMC_MAINF;
  81. clkmain->rate = (tmp * parent_rate) / MAINF_DIV;
  82. return clkmain->rate;
  83. }
  84. static const struct clk_ops main_ops = {
  85. .prepare = clk_main_prepare,
  86. .is_prepared = clk_main_is_prepared,
  87. .recalc_rate = clk_main_recalc_rate,
  88. };
  89. static struct clk * __init
  90. at91_clk_register_main(struct at91_pmc *pmc,
  91. unsigned int irq,
  92. const char *name,
  93. const char *parent_name,
  94. unsigned long rate)
  95. {
  96. int ret;
  97. struct clk_main *clkmain;
  98. struct clk *clk = NULL;
  99. struct clk_init_data init;
  100. if (!pmc || !irq || !name)
  101. return ERR_PTR(-EINVAL);
  102. if (!rate && !parent_name)
  103. return ERR_PTR(-EINVAL);
  104. clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL);
  105. if (!clkmain)
  106. return ERR_PTR(-ENOMEM);
  107. init.name = name;
  108. init.ops = &main_ops;
  109. init.parent_names = parent_name ? &parent_name : NULL;
  110. init.num_parents = parent_name ? 1 : 0;
  111. init.flags = parent_name ? 0 : CLK_IS_ROOT;
  112. clkmain->hw.init = &init;
  113. clkmain->rate = rate;
  114. clkmain->pmc = pmc;
  115. clkmain->irq = irq;
  116. init_waitqueue_head(&clkmain->wait);
  117. irq_set_status_flags(clkmain->irq, IRQ_NOAUTOEN);
  118. ret = request_irq(clkmain->irq, clk_main_irq_handler,
  119. IRQF_TRIGGER_HIGH, "clk-main", clkmain);
  120. if (ret)
  121. return ERR_PTR(ret);
  122. clk = clk_register(NULL, &clkmain->hw);
  123. if (IS_ERR(clk)) {
  124. free_irq(clkmain->irq, clkmain);
  125. kfree(clkmain);
  126. }
  127. return clk;
  128. }
  129. static void __init
  130. of_at91_clk_main_setup(struct device_node *np, struct at91_pmc *pmc)
  131. {
  132. struct clk *clk;
  133. unsigned int irq;
  134. const char *parent_name;
  135. const char *name = np->name;
  136. u32 rate = 0;
  137. parent_name = of_clk_get_parent_name(np, 0);
  138. of_property_read_string(np, "clock-output-names", &name);
  139. of_property_read_u32(np, "clock-frequency", &rate);
  140. irq = irq_of_parse_and_map(np, 0);
  141. if (!irq)
  142. return;
  143. clk = at91_clk_register_main(pmc, irq, name, parent_name, rate);
  144. if (IS_ERR(clk))
  145. return;
  146. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  147. }
  148. void __init of_at91rm9200_clk_main_setup(struct device_node *np,
  149. struct at91_pmc *pmc)
  150. {
  151. of_at91_clk_main_setup(np, pmc);
  152. }