clk-ls1x.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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
  16. static DEFINE_SPINLOCK(_lock);
  17. static int ls1x_pll_clk_enable(struct clk_hw *hw)
  18. {
  19. return 0;
  20. }
  21. static void ls1x_pll_clk_disable(struct clk_hw *hw)
  22. {
  23. }
  24. static unsigned long ls1x_pll_recalc_rate(struct clk_hw *hw,
  25. unsigned long parent_rate)
  26. {
  27. u32 pll, rate;
  28. pll = __raw_readl(LS1X_CLK_PLL_FREQ);
  29. rate = ((12 + (pll & 0x3f)) * 1000000) +
  30. ((((pll >> 8) & 0x3ff) * 1000000) >> 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, const char *parent_name, unsigned long flags)
  42. {
  43. struct clk_hw *hw;
  44. struct clk *clk;
  45. struct clk_init_data init;
  46. /* allocate the divider */
  47. hw = kzalloc(sizeof(struct clk_hw), GFP_KERNEL);
  48. if (!hw) {
  49. pr_err("%s: could not allocate clk_hw\n", __func__);
  50. return ERR_PTR(-ENOMEM);
  51. }
  52. init.name = name;
  53. init.ops = &ls1x_pll_clk_ops;
  54. init.flags = flags | CLK_IS_BASIC;
  55. init.parent_names = (parent_name ? &parent_name : NULL);
  56. init.num_parents = (parent_name ? 1 : 0);
  57. hw->init = &init;
  58. /* register the clock */
  59. clk = clk_register(dev, hw);
  60. if (IS_ERR(clk))
  61. kfree(hw);
  62. return clk;
  63. }
  64. void __init ls1x_clk_init(void)
  65. {
  66. struct clk *clk;
  67. clk = clk_register_pll(NULL, "pll_clk", NULL, CLK_IS_ROOT);
  68. clk_prepare_enable(clk);
  69. clk = clk_register_divider(NULL, "cpu_clk", "pll_clk",
  70. CLK_SET_RATE_PARENT, LS1X_CLK_PLL_DIV, DIV_CPU_SHIFT,
  71. DIV_CPU_WIDTH, CLK_DIVIDER_ONE_BASED, &_lock);
  72. clk_prepare_enable(clk);
  73. clk_register_clkdev(clk, "cpu", NULL);
  74. clk = clk_register_divider(NULL, "dc_clk", "pll_clk",
  75. CLK_SET_RATE_PARENT, LS1X_CLK_PLL_DIV, DIV_DC_SHIFT,
  76. DIV_DC_WIDTH, CLK_DIVIDER_ONE_BASED, &_lock);
  77. clk_prepare_enable(clk);
  78. clk_register_clkdev(clk, "dc", NULL);
  79. clk = clk_register_divider(NULL, "ahb_clk", "pll_clk",
  80. CLK_SET_RATE_PARENT, LS1X_CLK_PLL_DIV, DIV_DDR_SHIFT,
  81. DIV_DDR_WIDTH, CLK_DIVIDER_ONE_BASED, &_lock);
  82. clk_prepare_enable(clk);
  83. clk_register_clkdev(clk, "ahb", NULL);
  84. clk_register_clkdev(clk, "stmmaceth", NULL);
  85. clk = clk_register_fixed_factor(NULL, "apb_clk", "ahb_clk", 0, 1, 2);
  86. clk_prepare_enable(clk);
  87. clk_register_clkdev(clk, "apb", NULL);
  88. clk_register_clkdev(clk, "serial8250", NULL);
  89. }