clock.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. unsigned long clk_get_rate(struct clk *clk)
  35. {
  36. return clk->rate;
  37. }
  38. EXPORT_SYMBOL(clk_get_rate);
  39. static void pll_clk_init(struct clk *clk)
  40. {
  41. u32 pll;
  42. pll = __raw_readl(LS1X_CLK_PLL_FREQ);
  43. clk->rate = (12 + (pll & 0x3f)) * 33 / 2
  44. + ((pll >> 8) & 0x3ff) * 33 / 1024 / 2;
  45. clk->rate *= 1000000;
  46. }
  47. static void cpu_clk_init(struct clk *clk)
  48. {
  49. u32 pll, ctrl;
  50. pll = clk_get_rate(clk->parent);
  51. ctrl = __raw_readl(LS1X_CLK_PLL_DIV) & DIV_CPU;
  52. clk->rate = pll / (ctrl >> DIV_CPU_SHIFT);
  53. }
  54. static void ddr_clk_init(struct clk *clk)
  55. {
  56. u32 pll, ctrl;
  57. pll = clk_get_rate(clk->parent);
  58. ctrl = __raw_readl(LS1X_CLK_PLL_DIV) & DIV_DDR;
  59. clk->rate = pll / (ctrl >> DIV_DDR_SHIFT);
  60. }
  61. static void dc_clk_init(struct clk *clk)
  62. {
  63. u32 pll, ctrl;
  64. pll = clk_get_rate(clk->parent);
  65. ctrl = __raw_readl(LS1X_CLK_PLL_DIV) & DIV_DC;
  66. clk->rate = pll / (ctrl >> DIV_DC_SHIFT);
  67. }
  68. static struct clk_ops pll_clk_ops = {
  69. .init = pll_clk_init,
  70. };
  71. static struct clk_ops cpu_clk_ops = {
  72. .init = cpu_clk_init,
  73. };
  74. static struct clk_ops ddr_clk_ops = {
  75. .init = ddr_clk_init,
  76. };
  77. static struct clk_ops dc_clk_ops = {
  78. .init = dc_clk_init,
  79. };
  80. static struct clk pll_clk = {
  81. .name = "pll",
  82. .ops = &pll_clk_ops,
  83. };
  84. static struct clk cpu_clk = {
  85. .name = "cpu",
  86. .parent = &pll_clk,
  87. .ops = &cpu_clk_ops,
  88. };
  89. static struct clk ddr_clk = {
  90. .name = "ddr",
  91. .parent = &pll_clk,
  92. .ops = &ddr_clk_ops,
  93. };
  94. static struct clk dc_clk = {
  95. .name = "dc",
  96. .parent = &pll_clk,
  97. .ops = &dc_clk_ops,
  98. };
  99. int clk_register(struct clk *clk)
  100. {
  101. mutex_lock(&clocks_mutex);
  102. list_add(&clk->node, &clocks);
  103. if (clk->ops->init)
  104. clk->ops->init(clk);
  105. mutex_unlock(&clocks_mutex);
  106. return 0;
  107. }
  108. EXPORT_SYMBOL(clk_register);
  109. static struct clk *ls1x_clks[] = {
  110. &pll_clk,
  111. &cpu_clk,
  112. &ddr_clk,
  113. &dc_clk,
  114. };
  115. int __init ls1x_clock_init(void)
  116. {
  117. int i;
  118. for (i = 0; i < ARRAY_SIZE(ls1x_clks); i++)
  119. clk_register(ls1x_clks[i]);
  120. return 0;
  121. }
  122. void __init plat_time_init(void)
  123. {
  124. struct clk *clk;
  125. /* Initialize LS1X clocks */
  126. ls1x_clock_init();
  127. /* setup mips r4k timer */
  128. clk = clk_get(NULL, "cpu");
  129. if (IS_ERR(clk))
  130. panic("unable to get dc clock, err=%ld", PTR_ERR(clk));
  131. mips_hpt_frequency = clk_get_rate(clk) / 2;
  132. }