clk-loongson1b.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2012-2016 Zhang, Keguang <[email protected]>
  4. */
  5. #include <linux/clkdev.h>
  6. #include <linux/clk-provider.h>
  7. #include <linux/io.h>
  8. #include <linux/err.h>
  9. #include <loongson1.h>
  10. #include "clk.h"
  11. #define OSC (33 * 1000000)
  12. #define DIV_APB 2
  13. static DEFINE_SPINLOCK(_lock);
  14. static unsigned long ls1x_pll_recalc_rate(struct clk_hw *hw,
  15. unsigned long parent_rate)
  16. {
  17. u32 pll, rate;
  18. pll = __raw_readl(LS1X_CLK_PLL_FREQ);
  19. rate = 12 + (pll & GENMASK(5, 0));
  20. rate *= OSC;
  21. rate >>= 1;
  22. return rate;
  23. }
  24. static const struct clk_ops ls1x_pll_clk_ops = {
  25. .recalc_rate = ls1x_pll_recalc_rate,
  26. };
  27. static const char *const cpu_parents[] = { "cpu_clk_div", "osc_clk", };
  28. static const char *const ahb_parents[] = { "ahb_clk_div", "osc_clk", };
  29. static const char *const dc_parents[] = { "dc_clk_div", "osc_clk", };
  30. void __init ls1x_clk_init(void)
  31. {
  32. struct clk_hw *hw;
  33. hw = clk_hw_register_fixed_rate(NULL, "osc_clk", NULL, 0, OSC);
  34. clk_hw_register_clkdev(hw, "osc_clk", NULL);
  35. /* clock derived from 33 MHz OSC clk */
  36. hw = clk_hw_register_pll(NULL, "pll_clk", "osc_clk",
  37. &ls1x_pll_clk_ops, 0);
  38. clk_hw_register_clkdev(hw, "pll_clk", NULL);
  39. /* clock derived from PLL clk */
  40. /* _____
  41. * _______________________| |
  42. * OSC ___/ | MUX |___ CPU CLK
  43. * \___ PLL ___ CPU DIV ___| |
  44. * |_____|
  45. */
  46. hw = clk_hw_register_divider(NULL, "cpu_clk_div", "pll_clk",
  47. CLK_GET_RATE_NOCACHE, LS1X_CLK_PLL_DIV,
  48. DIV_CPU_SHIFT, DIV_CPU_WIDTH,
  49. CLK_DIVIDER_ONE_BASED |
  50. CLK_DIVIDER_ROUND_CLOSEST, &_lock);
  51. clk_hw_register_clkdev(hw, "cpu_clk_div", NULL);
  52. hw = clk_hw_register_mux(NULL, "cpu_clk", cpu_parents,
  53. ARRAY_SIZE(cpu_parents),
  54. CLK_SET_RATE_NO_REPARENT, LS1X_CLK_PLL_DIV,
  55. BYPASS_CPU_SHIFT, BYPASS_CPU_WIDTH, 0, &_lock);
  56. clk_hw_register_clkdev(hw, "cpu_clk", NULL);
  57. /* _____
  58. * _______________________| |
  59. * OSC ___/ | MUX |___ DC CLK
  60. * \___ PLL ___ DC DIV ___| |
  61. * |_____|
  62. */
  63. hw = clk_hw_register_divider(NULL, "dc_clk_div", "pll_clk",
  64. 0, LS1X_CLK_PLL_DIV, DIV_DC_SHIFT,
  65. DIV_DC_WIDTH, CLK_DIVIDER_ONE_BASED, &_lock);
  66. clk_hw_register_clkdev(hw, "dc_clk_div", NULL);
  67. hw = clk_hw_register_mux(NULL, "dc_clk", dc_parents,
  68. ARRAY_SIZE(dc_parents),
  69. CLK_SET_RATE_NO_REPARENT, LS1X_CLK_PLL_DIV,
  70. BYPASS_DC_SHIFT, BYPASS_DC_WIDTH, 0, &_lock);
  71. clk_hw_register_clkdev(hw, "dc_clk", NULL);
  72. /* _____
  73. * _______________________| |
  74. * OSC ___/ | MUX |___ DDR CLK
  75. * \___ PLL ___ DDR DIV ___| |
  76. * |_____|
  77. */
  78. hw = clk_hw_register_divider(NULL, "ahb_clk_div", "pll_clk",
  79. 0, LS1X_CLK_PLL_DIV, DIV_DDR_SHIFT,
  80. DIV_DDR_WIDTH, CLK_DIVIDER_ONE_BASED,
  81. &_lock);
  82. clk_hw_register_clkdev(hw, "ahb_clk_div", NULL);
  83. hw = clk_hw_register_mux(NULL, "ahb_clk", ahb_parents,
  84. ARRAY_SIZE(ahb_parents),
  85. CLK_SET_RATE_NO_REPARENT, LS1X_CLK_PLL_DIV,
  86. BYPASS_DDR_SHIFT, BYPASS_DDR_WIDTH, 0, &_lock);
  87. clk_hw_register_clkdev(hw, "ahb_clk", NULL);
  88. clk_hw_register_clkdev(hw, "ls1x-dma", NULL);
  89. clk_hw_register_clkdev(hw, "stmmaceth", NULL);
  90. /* clock derived from AHB clk */
  91. /* APB clk is always half of the AHB clk */
  92. hw = clk_hw_register_fixed_factor(NULL, "apb_clk", "ahb_clk", 0, 1,
  93. DIV_APB);
  94. clk_hw_register_clkdev(hw, "apb_clk", NULL);
  95. clk_hw_register_clkdev(hw, "ls1x-ac97", NULL);
  96. clk_hw_register_clkdev(hw, "ls1x-i2c", NULL);
  97. clk_hw_register_clkdev(hw, "ls1x-nand", NULL);
  98. clk_hw_register_clkdev(hw, "ls1x-pwmtimer", NULL);
  99. clk_hw_register_clkdev(hw, "ls1x-spi", NULL);
  100. clk_hw_register_clkdev(hw, "ls1x-wdt", NULL);
  101. clk_hw_register_clkdev(hw, "serial8250", NULL);
  102. }