clk-rz.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/clk/renesas.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/slab.h>
  18. struct rz_cpg {
  19. struct clk_onecell_data data;
  20. void __iomem *reg;
  21. };
  22. #define CPG_FRQCR 0x10
  23. #define CPG_FRQCR2 0x14
  24. #define PPR0 0xFCFE3200
  25. #define PIBC0 0xFCFE7000
  26. #define MD_CLK(x) ((x >> 2) & 1) /* P0_2 */
  27. /* -----------------------------------------------------------------------------
  28. * Initialization
  29. */
  30. static u16 __init rz_cpg_read_mode_pins(void)
  31. {
  32. void __iomem *ppr0, *pibc0;
  33. u16 modes;
  34. ppr0 = ioremap_nocache(PPR0, 2);
  35. pibc0 = ioremap_nocache(PIBC0, 2);
  36. BUG_ON(!ppr0 || !pibc0);
  37. iowrite16(4, pibc0); /* enable input buffer */
  38. modes = ioread16(ppr0);
  39. iounmap(ppr0);
  40. iounmap(pibc0);
  41. return modes;
  42. }
  43. static struct clk * __init
  44. rz_cpg_register_clock(struct device_node *np, struct rz_cpg *cpg, const char *name)
  45. {
  46. u32 val;
  47. unsigned mult;
  48. static const unsigned frqcr_tab[4] = { 3, 2, 0, 1 };
  49. if (strcmp(name, "pll") == 0) {
  50. unsigned int cpg_mode = MD_CLK(rz_cpg_read_mode_pins());
  51. const char *parent_name = of_clk_get_parent_name(np, cpg_mode);
  52. mult = cpg_mode ? (32 / 4) : 30;
  53. return clk_register_fixed_factor(NULL, name, parent_name, 0, mult, 1);
  54. }
  55. /* If mapping regs failed, skip non-pll clocks. System will boot anyhow */
  56. if (!cpg->reg)
  57. return ERR_PTR(-ENXIO);
  58. /* FIXME:"i" and "g" are variable clocks with non-integer dividers (e.g. 2/3)
  59. * and the constraint that always g <= i. To get the rz platform started,
  60. * let them run at fixed current speed and implement the details later.
  61. */
  62. if (strcmp(name, "i") == 0)
  63. val = (clk_readl(cpg->reg + CPG_FRQCR) >> 8) & 3;
  64. else if (strcmp(name, "g") == 0)
  65. val = clk_readl(cpg->reg + CPG_FRQCR2) & 3;
  66. else
  67. return ERR_PTR(-EINVAL);
  68. mult = frqcr_tab[val];
  69. return clk_register_fixed_factor(NULL, name, "pll", 0, mult, 3);
  70. }
  71. static void __init rz_cpg_clocks_init(struct device_node *np)
  72. {
  73. struct rz_cpg *cpg;
  74. struct clk **clks;
  75. unsigned i;
  76. int num_clks;
  77. num_clks = of_property_count_strings(np, "clock-output-names");
  78. if (WARN(num_clks <= 0, "can't count CPG clocks\n"))
  79. return;
  80. cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);
  81. clks = kzalloc(num_clks * sizeof(*clks), GFP_KERNEL);
  82. BUG_ON(!cpg || !clks);
  83. cpg->data.clks = clks;
  84. cpg->data.clk_num = num_clks;
  85. cpg->reg = of_iomap(np, 0);
  86. for (i = 0; i < num_clks; ++i) {
  87. const char *name;
  88. struct clk *clk;
  89. of_property_read_string_index(np, "clock-output-names", i, &name);
  90. clk = rz_cpg_register_clock(np, cpg, name);
  91. if (IS_ERR(clk))
  92. pr_err("%s: failed to register %s %s clock (%ld)\n",
  93. __func__, np->name, name, PTR_ERR(clk));
  94. else
  95. cpg->data.clks[i] = clk;
  96. }
  97. of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
  98. cpg_mstp_add_clk_domain(np);
  99. }
  100. CLK_OF_DECLARE(rz_cpg_clks, "renesas,rz-cpg-clocks", rz_cpg_clocks_init);