clock.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2011 Zhang, Keguang <keguang.zhang@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/list.h>
  11. #include <linux/mutex.h>
  12. #include <linux/clk.h>
  13. #include <linux/err.h>
  14. #include <asm/clock.h>
  15. #include <asm/time.h>
  16. #include <loongson1.h>
  17. static LIST_HEAD(clocks);
  18. static DEFINE_MUTEX(clocks_mutex);
  19. struct clk *clk_get(struct device *dev, const char *name)
  20. {
  21. struct clk *c;
  22. struct clk *ret = NULL;
  23. mutex_lock(&clocks_mutex);
  24. list_for_each_entry(c, &clocks, node) {
  25. if (!strcmp(c->name, name)) {
  26. ret = c;
  27. break;
  28. }
  29. }
  30. mutex_unlock(&clocks_mutex);
  31. return ret;
  32. }
  33. EXPORT_SYMBOL(clk_get);
  34. int clk_enable(struct clk *clk)
  35. {
  36. return 0;
  37. }
  38. EXPORT_SYMBOL(clk_enable);
  39. void clk_disable(struct clk *clk)
  40. {
  41. }
  42. EXPORT_SYMBOL(clk_disable);
  43. unsigned long clk_get_rate(struct clk *clk)
  44. {
  45. return clk->rate;
  46. }
  47. EXPORT_SYMBOL(clk_get_rate);
  48. void clk_put(struct clk *clk)
  49. {
  50. }
  51. EXPORT_SYMBOL(clk_put);
  52. static void pll_clk_init(struct clk *clk)
  53. {
  54. u32 pll;
  55. pll = __raw_readl(LS1X_CLK_PLL_FREQ);
  56. clk->rate = (12 + (pll & 0x3f)) * 33 / 2
  57. + ((pll >> 8) & 0x3ff) * 33 / 1024 / 2;
  58. clk->rate *= 1000000;
  59. }
  60. static void cpu_clk_init(struct clk *clk)
  61. {
  62. u32 pll, ctrl;
  63. pll = clk_get_rate(clk->parent);
  64. ctrl = __raw_readl(LS1X_CLK_PLL_DIV) & DIV_CPU;
  65. clk->rate = pll / (ctrl >> DIV_CPU_SHIFT);
  66. }
  67. static void ddr_clk_init(struct clk *clk)
  68. {
  69. u32 pll, ctrl;
  70. pll = clk_get_rate(clk->parent);
  71. ctrl = __raw_readl(LS1X_CLK_PLL_DIV) & DIV_DDR;
  72. clk->rate = pll / (ctrl >> DIV_DDR_SHIFT);
  73. }
  74. static void dc_clk_init(struct clk *clk)
  75. {
  76. u32 pll, ctrl;
  77. pll = clk_get_rate(clk->parent);
  78. ctrl = __raw_readl(LS1X_CLK_PLL_DIV) & DIV_DC;
  79. clk->rate = pll / (ctrl >> DIV_DC_SHIFT);
  80. }
  81. static struct clk_ops pll_clk_ops = {
  82. .init = pll_clk_init,
  83. };
  84. static struct clk_ops cpu_clk_ops = {
  85. .init = cpu_clk_init,
  86. };
  87. static struct clk_ops ddr_clk_ops = {
  88. .init = ddr_clk_init,
  89. };
  90. static struct clk_ops dc_clk_ops = {
  91. .init = dc_clk_init,
  92. };
  93. static struct clk pll_clk = {
  94. .name = "pll",
  95. .ops = &pll_clk_ops,
  96. };
  97. static struct clk cpu_clk = {
  98. .name = "cpu",
  99. .parent = &pll_clk,
  100. .ops = &cpu_clk_ops,
  101. };
  102. static struct clk ddr_clk = {
  103. .name = "ddr",
  104. .parent = &pll_clk,
  105. .ops = &ddr_clk_ops,
  106. };
  107. static struct clk dc_clk = {
  108. .name = "dc",
  109. .parent = &pll_clk,
  110. .ops = &dc_clk_ops,
  111. };
  112. int clk_register(struct clk *clk)
  113. {
  114. mutex_lock(&clocks_mutex);
  115. list_add(&clk->node, &clocks);
  116. if (clk->ops->init)
  117. clk->ops->init(clk);
  118. mutex_unlock(&clocks_mutex);
  119. return 0;
  120. }
  121. EXPORT_SYMBOL(clk_register);
  122. static struct clk *ls1x_clks[] = {
  123. &pll_clk,
  124. &cpu_clk,
  125. &ddr_clk,
  126. &dc_clk,
  127. };
  128. int __init ls1x_clock_init(void)
  129. {
  130. int i;
  131. for (i = 0; i < ARRAY_SIZE(ls1x_clks); i++)
  132. clk_register(ls1x_clks[i]);
  133. return 0;
  134. }
  135. void __init plat_time_init(void)
  136. {
  137. struct clk *clk;
  138. /* Initialize LS1X clocks */
  139. ls1x_clock_init();
  140. /* setup mips r4k timer */
  141. clk = clk_get(NULL, "cpu");
  142. if (IS_ERR(clk))
  143. panic("unable to get dc clock, err=%ld", PTR_ERR(clk));
  144. mips_hpt_frequency = clk_get_rate(clk) / 2;
  145. }