clk-loongson1c.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2016 Yang Ling <[email protected]>
  4. */
  5. #include <linux/clkdev.h>
  6. #include <linux/clk-provider.h>
  7. #include <linux/io.h>
  8. #include <loongson1.h>
  9. #include "clk.h"
  10. #define OSC (24 * 1000000)
  11. #define DIV_APB 1
  12. static DEFINE_SPINLOCK(_lock);
  13. static unsigned long ls1x_pll_recalc_rate(struct clk_hw *hw,
  14. unsigned long parent_rate)
  15. {
  16. u32 pll, rate;
  17. pll = __raw_readl(LS1X_CLK_PLL_FREQ);
  18. rate = ((pll >> 8) & 0xff) + ((pll >> 16) & 0xff);
  19. rate *= OSC;
  20. rate >>= 2;
  21. return rate;
  22. }
  23. static const struct clk_ops ls1x_pll_clk_ops = {
  24. .recalc_rate = ls1x_pll_recalc_rate,
  25. };
  26. static const struct clk_div_table ahb_div_table[] = {
  27. [0] = { .val = 0, .div = 2 },
  28. [1] = { .val = 1, .div = 4 },
  29. [2] = { .val = 2, .div = 3 },
  30. [3] = { .val = 3, .div = 3 },
  31. [4] = { /* sentinel */ }
  32. };
  33. void __init ls1x_clk_init(void)
  34. {
  35. struct clk_hw *hw;
  36. hw = clk_hw_register_fixed_rate(NULL, "osc_clk", NULL, 0, OSC);
  37. clk_hw_register_clkdev(hw, "osc_clk", NULL);
  38. /* clock derived from 24 MHz OSC clk */
  39. hw = clk_hw_register_pll(NULL, "pll_clk", "osc_clk",
  40. &ls1x_pll_clk_ops, 0);
  41. clk_hw_register_clkdev(hw, "pll_clk", NULL);
  42. hw = clk_hw_register_divider(NULL, "cpu_clk_div", "pll_clk",
  43. CLK_GET_RATE_NOCACHE, LS1X_CLK_PLL_DIV,
  44. DIV_CPU_SHIFT, DIV_CPU_WIDTH,
  45. CLK_DIVIDER_ONE_BASED |
  46. CLK_DIVIDER_ROUND_CLOSEST, &_lock);
  47. clk_hw_register_clkdev(hw, "cpu_clk_div", NULL);
  48. hw = clk_hw_register_fixed_factor(NULL, "cpu_clk", "cpu_clk_div",
  49. 0, 1, 1);
  50. clk_hw_register_clkdev(hw, "cpu_clk", NULL);
  51. hw = clk_hw_register_divider(NULL, "dc_clk_div", "pll_clk",
  52. 0, LS1X_CLK_PLL_DIV, DIV_DC_SHIFT,
  53. DIV_DC_WIDTH, CLK_DIVIDER_ONE_BASED, &_lock);
  54. clk_hw_register_clkdev(hw, "dc_clk_div", NULL);
  55. hw = clk_hw_register_fixed_factor(NULL, "dc_clk", "dc_clk_div",
  56. 0, 1, 1);
  57. clk_hw_register_clkdev(hw, "dc_clk", NULL);
  58. hw = clk_hw_register_divider_table(NULL, "ahb_clk_div", "cpu_clk_div",
  59. 0, LS1X_CLK_PLL_FREQ, DIV_DDR_SHIFT,
  60. DIV_DDR_WIDTH, CLK_DIVIDER_ALLOW_ZERO,
  61. ahb_div_table, &_lock);
  62. clk_hw_register_clkdev(hw, "ahb_clk_div", NULL);
  63. hw = clk_hw_register_fixed_factor(NULL, "ahb_clk", "ahb_clk_div",
  64. 0, 1, 1);
  65. clk_hw_register_clkdev(hw, "ahb_clk", NULL);
  66. clk_hw_register_clkdev(hw, "ls1x-dma", NULL);
  67. clk_hw_register_clkdev(hw, "stmmaceth", NULL);
  68. /* clock derived from AHB clk */
  69. hw = clk_hw_register_fixed_factor(NULL, "apb_clk", "ahb_clk", 0, 1,
  70. DIV_APB);
  71. clk_hw_register_clkdev(hw, "apb_clk", NULL);
  72. clk_hw_register_clkdev(hw, "ls1x-ac97", NULL);
  73. clk_hw_register_clkdev(hw, "ls1x-i2c", NULL);
  74. clk_hw_register_clkdev(hw, "ls1x-nand", NULL);
  75. clk_hw_register_clkdev(hw, "ls1x-pwmtimer", NULL);
  76. clk_hw_register_clkdev(hw, "ls1x-spi", NULL);
  77. clk_hw_register_clkdev(hw, "ls1x-wdt", NULL);
  78. clk_hw_register_clkdev(hw, "serial8250", NULL);
  79. }