clk-rz.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * rz Core CPG Clocks
  3. *
  4. * Copyright (C) 2013 Ideas On Board SPRL
  5. * Copyright (C) 2014 Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. */
  11. #include <linux/clk-provider.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/slab.h>
  17. struct rz_cpg {
  18. struct clk_onecell_data data;
  19. void __iomem *reg;
  20. };
  21. #define CPG_FRQCR 0x10
  22. #define CPG_FRQCR2 0x14
  23. /* -----------------------------------------------------------------------------
  24. * Initialization
  25. */
  26. static struct clk * __init
  27. rz_cpg_register_clock(struct device_node *np, struct rz_cpg *cpg, const char *name)
  28. {
  29. u32 val;
  30. unsigned mult;
  31. static const unsigned frqcr_tab[4] = { 3, 2, 0, 1 };
  32. if (strcmp(name, "pll") == 0) {
  33. /* FIXME: cpg_mode should be read from GPIO. But no GPIO support yet */
  34. unsigned cpg_mode = 0; /* hardcoded to EXTAL for now */
  35. const char *parent_name = of_clk_get_parent_name(np, cpg_mode);
  36. mult = cpg_mode ? (32 / 4) : 30;
  37. return clk_register_fixed_factor(NULL, name, parent_name, 0, mult, 1);
  38. }
  39. /* If mapping regs failed, skip non-pll clocks. System will boot anyhow */
  40. if (!cpg->reg)
  41. return ERR_PTR(-ENXIO);
  42. /* FIXME:"i" and "g" are variable clocks with non-integer dividers (e.g. 2/3)
  43. * and the constraint that always g <= i. To get the rz platform started,
  44. * let them run at fixed current speed and implement the details later.
  45. */
  46. if (strcmp(name, "i") == 0)
  47. val = (clk_readl(cpg->reg + CPG_FRQCR) >> 8) & 3;
  48. else if (strcmp(name, "g") == 0)
  49. val = clk_readl(cpg->reg + CPG_FRQCR2) & 3;
  50. else
  51. return ERR_PTR(-EINVAL);
  52. mult = frqcr_tab[val];
  53. return clk_register_fixed_factor(NULL, name, "pll", 0, mult, 3);
  54. }
  55. static void __init rz_cpg_clocks_init(struct device_node *np)
  56. {
  57. struct rz_cpg *cpg;
  58. struct clk **clks;
  59. unsigned i;
  60. int num_clks;
  61. num_clks = of_property_count_strings(np, "clock-output-names");
  62. if (WARN(num_clks <= 0, "can't count CPG clocks\n"))
  63. return;
  64. cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);
  65. clks = kzalloc(num_clks * sizeof(*clks), GFP_KERNEL);
  66. BUG_ON(!cpg || !clks);
  67. cpg->data.clks = clks;
  68. cpg->data.clk_num = num_clks;
  69. cpg->reg = of_iomap(np, 0);
  70. for (i = 0; i < num_clks; ++i) {
  71. const char *name;
  72. struct clk *clk;
  73. of_property_read_string_index(np, "clock-output-names", i, &name);
  74. clk = rz_cpg_register_clock(np, cpg, name);
  75. if (IS_ERR(clk))
  76. pr_err("%s: failed to register %s %s clock (%ld)\n",
  77. __func__, np->name, name, PTR_ERR(clk));
  78. else
  79. cpg->data.clks[i] = clk;
  80. }
  81. of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
  82. }
  83. CLK_OF_DECLARE(rz_cpg_clks, "renesas,rz-cpg-clocks", rz_cpg_clocks_init);