clk-periph.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2011-2012 Calxeda, Inc.
  3. * Copyright (C) 2012-2013 Altera Corporation <www.altera.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * Based from clk-highbank.c
  16. *
  17. */
  18. #include <linux/slab.h>
  19. #include <linux/clk-provider.h>
  20. #include <linux/io.h>
  21. #include <linux/of.h>
  22. #include "clk.h"
  23. #define to_socfpga_periph_clk(p) container_of(p, struct socfpga_periph_clk, hw.hw)
  24. static unsigned long clk_periclk_recalc_rate(struct clk_hw *hwclk,
  25. unsigned long parent_rate)
  26. {
  27. struct socfpga_periph_clk *socfpgaclk = to_socfpga_periph_clk(hwclk);
  28. u32 div, val;
  29. if (socfpgaclk->fixed_div) {
  30. div = socfpgaclk->fixed_div;
  31. } else {
  32. if (socfpgaclk->div_reg) {
  33. val = readl(socfpgaclk->div_reg) >> socfpgaclk->shift;
  34. val &= GENMASK(socfpgaclk->width - 1, 0);
  35. parent_rate /= (val + 1);
  36. }
  37. div = ((readl(socfpgaclk->hw.reg) & 0x1ff) + 1);
  38. }
  39. return parent_rate / div;
  40. }
  41. static u8 clk_periclk_get_parent(struct clk_hw *hwclk)
  42. {
  43. u32 clk_src;
  44. clk_src = readl(clk_mgr_base_addr + CLKMGR_DBCTRL);
  45. return clk_src & 0x1;
  46. }
  47. static const struct clk_ops periclk_ops = {
  48. .recalc_rate = clk_periclk_recalc_rate,
  49. .get_parent = clk_periclk_get_parent,
  50. };
  51. static __init void __socfpga_periph_init(struct device_node *node,
  52. const struct clk_ops *ops)
  53. {
  54. u32 reg;
  55. struct clk *clk;
  56. struct socfpga_periph_clk *periph_clk;
  57. const char *clk_name = node->name;
  58. const char *parent_name[SOCFPGA_MAX_PARENTS];
  59. struct clk_init_data init;
  60. int rc;
  61. u32 fixed_div;
  62. u32 div_reg[3];
  63. of_property_read_u32(node, "reg", &reg);
  64. periph_clk = kzalloc(sizeof(*periph_clk), GFP_KERNEL);
  65. if (WARN_ON(!periph_clk))
  66. return;
  67. periph_clk->hw.reg = clk_mgr_base_addr + reg;
  68. rc = of_property_read_u32_array(node, "div-reg", div_reg, 3);
  69. if (!rc) {
  70. periph_clk->div_reg = clk_mgr_base_addr + div_reg[0];
  71. periph_clk->shift = div_reg[1];
  72. periph_clk->width = div_reg[2];
  73. } else {
  74. periph_clk->div_reg = NULL;
  75. }
  76. rc = of_property_read_u32(node, "fixed-divider", &fixed_div);
  77. if (rc)
  78. periph_clk->fixed_div = 0;
  79. else
  80. periph_clk->fixed_div = fixed_div;
  81. of_property_read_string(node, "clock-output-names", &clk_name);
  82. init.name = clk_name;
  83. init.ops = ops;
  84. init.flags = 0;
  85. init.num_parents = of_clk_parent_fill(node, parent_name,
  86. SOCFPGA_MAX_PARENTS);
  87. init.parent_names = parent_name;
  88. periph_clk->hw.hw.init = &init;
  89. clk = clk_register(NULL, &periph_clk->hw.hw);
  90. if (WARN_ON(IS_ERR(clk))) {
  91. kfree(periph_clk);
  92. return;
  93. }
  94. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  95. }
  96. void __init socfpga_periph_init(struct device_node *node)
  97. {
  98. __socfpga_periph_init(node, &periclk_ops);
  99. }