clk-ls1x.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2012 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/clkdev.h>
  10. #include <linux/clk-provider.h>
  11. #include <linux/io.h>
  12. #include <linux/slab.h>
  13. #include <linux/err.h>
  14. #include <loongson1.h>
  15. #define OSC (33 * 1000000)
  16. #define DIV_APB 2
  17. static DEFINE_SPINLOCK(_lock);
  18. static int ls1x_pll_clk_enable(struct clk_hw *hw)
  19. {
  20. return 0;
  21. }
  22. static void ls1x_pll_clk_disable(struct clk_hw *hw)
  23. {
  24. }
  25. static unsigned long ls1x_pll_recalc_rate(struct clk_hw *hw,
  26. unsigned long parent_rate)
  27. {
  28. u32 pll, rate;
  29. pll = __raw_readl(LS1X_CLK_PLL_FREQ);
  30. rate = 12 + (pll & 0x3f) + (((pll >> 8) & 0x3ff) >> 10);
  31. rate *= OSC;
  32. rate >>= 1;
  33. return rate;
  34. }
  35. static const struct clk_ops ls1x_pll_clk_ops = {
  36. .enable = ls1x_pll_clk_enable,
  37. .disable = ls1x_pll_clk_disable,
  38. .recalc_rate = ls1x_pll_recalc_rate,
  39. };
  40. static struct clk *__init clk_register_pll(struct device *dev,
  41. const char *name,
  42. const char *parent_name,
  43. unsigned long flags)
  44. {
  45. struct clk_hw *hw;
  46. struct clk *clk;
  47. struct clk_init_data init;
  48. /* allocate the divider */
  49. hw = kzalloc(sizeof(struct clk_hw), GFP_KERNEL);
  50. if (!hw) {
  51. pr_err("%s: could not allocate clk_hw\n", __func__);
  52. return ERR_PTR(-ENOMEM);
  53. }
  54. init.name = name;
  55. init.ops = &ls1x_pll_clk_ops;
  56. init.flags = flags | CLK_IS_BASIC;
  57. init.parent_names = (parent_name ? &parent_name : NULL);
  58. init.num_parents = (parent_name ? 1 : 0);
  59. hw->init = &init;
  60. /* register the clock */
  61. clk = clk_register(dev, hw);
  62. if (IS_ERR(clk))
  63. kfree(hw);
  64. return clk;
  65. }
  66. static const char * const cpu_parents[] = { "cpu_clk_div", "osc_33m_clk", };
  67. static const char * const ahb_parents[] = { "ahb_clk_div", "osc_33m_clk", };
  68. static const char * const dc_parents[] = { "dc_clk_div", "osc_33m_clk", };
  69. void __init ls1x_clk_init(void)
  70. {
  71. struct clk *clk;
  72. clk = clk_register_fixed_rate(NULL, "osc_33m_clk", NULL, CLK_IS_ROOT,
  73. OSC);
  74. clk_register_clkdev(clk, "osc_33m_clk", NULL);
  75. /* clock derived from 33 MHz OSC clk */
  76. clk = clk_register_pll(NULL, "pll_clk", "osc_33m_clk", 0);
  77. clk_register_clkdev(clk, "pll_clk", NULL);
  78. /* clock derived from PLL clk */
  79. /* _____
  80. * _______________________| |
  81. * OSC ___/ | MUX |___ CPU CLK
  82. * \___ PLL ___ CPU DIV ___| |
  83. * |_____|
  84. */
  85. clk = clk_register_divider(NULL, "cpu_clk_div", "pll_clk",
  86. CLK_GET_RATE_NOCACHE, LS1X_CLK_PLL_DIV,
  87. DIV_CPU_SHIFT, DIV_CPU_WIDTH,
  88. CLK_DIVIDER_ONE_BASED |
  89. CLK_DIVIDER_ROUND_CLOSEST, &_lock);
  90. clk_register_clkdev(clk, "cpu_clk_div", NULL);
  91. clk = clk_register_mux(NULL, "cpu_clk", cpu_parents,
  92. ARRAY_SIZE(cpu_parents),
  93. CLK_SET_RATE_NO_REPARENT, LS1X_CLK_PLL_DIV,
  94. BYPASS_CPU_SHIFT, BYPASS_CPU_WIDTH, 0, &_lock);
  95. clk_register_clkdev(clk, "cpu_clk", NULL);
  96. /* _____
  97. * _______________________| |
  98. * OSC ___/ | MUX |___ DC CLK
  99. * \___ PLL ___ DC DIV ___| |
  100. * |_____|
  101. */
  102. clk = clk_register_divider(NULL, "dc_clk_div", "pll_clk",
  103. 0, LS1X_CLK_PLL_DIV, DIV_DC_SHIFT,
  104. DIV_DC_WIDTH, CLK_DIVIDER_ONE_BASED, &_lock);
  105. clk_register_clkdev(clk, "dc_clk_div", NULL);
  106. clk = clk_register_mux(NULL, "dc_clk", dc_parents,
  107. ARRAY_SIZE(dc_parents),
  108. CLK_SET_RATE_NO_REPARENT, LS1X_CLK_PLL_DIV,
  109. BYPASS_DC_SHIFT, BYPASS_DC_WIDTH, 0, &_lock);
  110. clk_register_clkdev(clk, "dc_clk", NULL);
  111. /* _____
  112. * _______________________| |
  113. * OSC ___/ | MUX |___ DDR CLK
  114. * \___ PLL ___ DDR DIV ___| |
  115. * |_____|
  116. */
  117. clk = clk_register_divider(NULL, "ahb_clk_div", "pll_clk",
  118. 0, LS1X_CLK_PLL_DIV, DIV_DDR_SHIFT,
  119. DIV_DDR_WIDTH, CLK_DIVIDER_ONE_BASED,
  120. &_lock);
  121. clk_register_clkdev(clk, "ahb_clk_div", NULL);
  122. clk = clk_register_mux(NULL, "ahb_clk", ahb_parents,
  123. ARRAY_SIZE(ahb_parents),
  124. CLK_SET_RATE_NO_REPARENT, LS1X_CLK_PLL_DIV,
  125. BYPASS_DDR_SHIFT, BYPASS_DDR_WIDTH, 0, &_lock);
  126. clk_register_clkdev(clk, "ahb_clk", NULL);
  127. clk_register_clkdev(clk, "stmmaceth", NULL);
  128. /* clock derived from AHB clk */
  129. /* APB clk is always half of the AHB clk */
  130. clk = clk_register_fixed_factor(NULL, "apb_clk", "ahb_clk", 0, 1,
  131. DIV_APB);
  132. clk_register_clkdev(clk, "apb_clk", NULL);
  133. clk_register_clkdev(clk, "ls1x_i2c", NULL);
  134. clk_register_clkdev(clk, "ls1x_pwmtimer", NULL);
  135. clk_register_clkdev(clk, "ls1x_spi", NULL);
  136. clk_register_clkdev(clk, "ls1x_wdt", NULL);
  137. clk_register_clkdev(clk, "serial8250", NULL);
  138. }