clk-r8a7779.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * r8a7779 Core CPG Clocks
  3. *
  4. * Copyright (C) 2013, 2014 Horms Solutions Ltd.
  5. *
  6. * Contact: Simon Horman <horms@verge.net.au>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/clk/renesas.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/soc/renesas/rcar-rst.h>
  21. #include <dt-bindings/clock/r8a7779-clock.h>
  22. #define CPG_NUM_CLOCKS (R8A7779_CLK_OUT + 1)
  23. struct r8a7779_cpg {
  24. struct clk_onecell_data data;
  25. spinlock_t lock;
  26. void __iomem *reg;
  27. };
  28. /* -----------------------------------------------------------------------------
  29. * CPG Clock Data
  30. */
  31. /*
  32. * MD1 = 1 MD1 = 0
  33. * (PLLA = 1500) (PLLA = 1600)
  34. * (MHz) (MHz)
  35. *------------------------------------------------+--------------------
  36. * clkz 1000 (2/3) 800 (1/2)
  37. * clkzs 250 (1/6) 200 (1/8)
  38. * clki 750 (1/2) 800 (1/2)
  39. * clks 250 (1/6) 200 (1/8)
  40. * clks1 125 (1/12) 100 (1/16)
  41. * clks3 187.5 (1/8) 200 (1/8)
  42. * clks4 93.7 (1/16) 100 (1/16)
  43. * clkp 62.5 (1/24) 50 (1/32)
  44. * clkg 62.5 (1/24) 66.6 (1/24)
  45. * clkb, CLKOUT
  46. * (MD2 = 0) 62.5 (1/24) 66.6 (1/24)
  47. * (MD2 = 1) 41.6 (1/36) 50 (1/32)
  48. */
  49. #define CPG_CLK_CONFIG_INDEX(md) (((md) & (BIT(2)|BIT(1))) >> 1)
  50. struct cpg_clk_config {
  51. unsigned int z_mult;
  52. unsigned int z_div;
  53. unsigned int zs_and_s_div;
  54. unsigned int s1_div;
  55. unsigned int p_div;
  56. unsigned int b_and_out_div;
  57. };
  58. static const struct cpg_clk_config cpg_clk_configs[4] __initconst = {
  59. { 1, 2, 8, 16, 32, 24 },
  60. { 2, 3, 6, 12, 24, 24 },
  61. { 1, 2, 8, 16, 32, 32 },
  62. { 2, 3, 6, 12, 24, 36 },
  63. };
  64. /*
  65. * MD PLLA Ratio
  66. * 12 11
  67. *------------------------
  68. * 0 0 x42
  69. * 0 1 x48
  70. * 1 0 x56
  71. * 1 1 x64
  72. */
  73. #define CPG_PLLA_MULT_INDEX(md) (((md) & (BIT(12)|BIT(11))) >> 11)
  74. static const unsigned int cpg_plla_mult[4] __initconst = { 42, 48, 56, 64 };
  75. /* -----------------------------------------------------------------------------
  76. * Initialization
  77. */
  78. static struct clk * __init
  79. r8a7779_cpg_register_clock(struct device_node *np, struct r8a7779_cpg *cpg,
  80. const struct cpg_clk_config *config,
  81. unsigned int plla_mult, const char *name)
  82. {
  83. const char *parent_name = "plla";
  84. unsigned int mult = 1;
  85. unsigned int div = 1;
  86. if (!strcmp(name, "plla")) {
  87. parent_name = of_clk_get_parent_name(np, 0);
  88. mult = plla_mult;
  89. } else if (!strcmp(name, "z")) {
  90. div = config->z_div;
  91. mult = config->z_mult;
  92. } else if (!strcmp(name, "zs") || !strcmp(name, "s")) {
  93. div = config->zs_and_s_div;
  94. } else if (!strcmp(name, "s1")) {
  95. div = config->s1_div;
  96. } else if (!strcmp(name, "p")) {
  97. div = config->p_div;
  98. } else if (!strcmp(name, "b") || !strcmp(name, "out")) {
  99. div = config->b_and_out_div;
  100. } else {
  101. return ERR_PTR(-EINVAL);
  102. }
  103. return clk_register_fixed_factor(NULL, name, parent_name, 0, mult, div);
  104. }
  105. static void __init r8a7779_cpg_clocks_init(struct device_node *np)
  106. {
  107. const struct cpg_clk_config *config;
  108. struct r8a7779_cpg *cpg;
  109. struct clk **clks;
  110. unsigned int i, plla_mult;
  111. int num_clks;
  112. u32 mode;
  113. if (rcar_rst_read_mode_pins(&mode))
  114. return;
  115. num_clks = of_property_count_strings(np, "clock-output-names");
  116. if (num_clks < 0) {
  117. pr_err("%s: failed to count clocks\n", __func__);
  118. return;
  119. }
  120. cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);
  121. clks = kzalloc(CPG_NUM_CLOCKS * sizeof(*clks), GFP_KERNEL);
  122. if (cpg == NULL || clks == NULL) {
  123. /* We're leaking memory on purpose, there's no point in cleaning
  124. * up as the system won't boot anyway.
  125. */
  126. return;
  127. }
  128. spin_lock_init(&cpg->lock);
  129. cpg->data.clks = clks;
  130. cpg->data.clk_num = num_clks;
  131. config = &cpg_clk_configs[CPG_CLK_CONFIG_INDEX(mode)];
  132. plla_mult = cpg_plla_mult[CPG_PLLA_MULT_INDEX(mode)];
  133. for (i = 0; i < num_clks; ++i) {
  134. const char *name;
  135. struct clk *clk;
  136. of_property_read_string_index(np, "clock-output-names", i,
  137. &name);
  138. clk = r8a7779_cpg_register_clock(np, cpg, config,
  139. plla_mult, name);
  140. if (IS_ERR(clk))
  141. pr_err("%s: failed to register %s %s clock (%ld)\n",
  142. __func__, np->name, name, PTR_ERR(clk));
  143. else
  144. cpg->data.clks[i] = clk;
  145. }
  146. of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
  147. cpg_mstp_add_clk_domain(np);
  148. }
  149. CLK_OF_DECLARE(r8a7779_cpg_clks, "renesas,r8a7779-cpg-clocks",
  150. r8a7779_cpg_clocks_init);