clk-gate.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/mfd/syscon.h>
  22. #include <linux/of.h>
  23. #include <linux/regmap.h>
  24. #include "clk.h"
  25. #define SOCFPGA_L4_MP_CLK "l4_mp_clk"
  26. #define SOCFPGA_L4_SP_CLK "l4_sp_clk"
  27. #define SOCFPGA_NAND_CLK "nand_clk"
  28. #define SOCFPGA_NAND_X_CLK "nand_x_clk"
  29. #define SOCFPGA_MMC_CLK "sdmmc_clk"
  30. #define SOCFPGA_GPIO_DB_CLK_OFFSET 0xA8
  31. #define to_socfpga_gate_clk(p) container_of(p, struct socfpga_gate_clk, hw.hw)
  32. /* SDMMC Group for System Manager defines */
  33. #define SYSMGR_SDMMCGRP_CTRL_OFFSET 0x108
  34. static u8 socfpga_clk_get_parent(struct clk_hw *hwclk)
  35. {
  36. u32 l4_src;
  37. u32 perpll_src;
  38. if (streq(hwclk->init->name, SOCFPGA_L4_MP_CLK)) {
  39. l4_src = readl(clk_mgr_base_addr + CLKMGR_L4SRC);
  40. return l4_src &= 0x1;
  41. }
  42. if (streq(hwclk->init->name, SOCFPGA_L4_SP_CLK)) {
  43. l4_src = readl(clk_mgr_base_addr + CLKMGR_L4SRC);
  44. return !!(l4_src & 2);
  45. }
  46. perpll_src = readl(clk_mgr_base_addr + CLKMGR_PERPLL_SRC);
  47. if (streq(hwclk->init->name, SOCFPGA_MMC_CLK))
  48. return perpll_src &= 0x3;
  49. if (streq(hwclk->init->name, SOCFPGA_NAND_CLK) ||
  50. streq(hwclk->init->name, SOCFPGA_NAND_X_CLK))
  51. return (perpll_src >> 2) & 3;
  52. /* QSPI clock */
  53. return (perpll_src >> 4) & 3;
  54. }
  55. static int socfpga_clk_set_parent(struct clk_hw *hwclk, u8 parent)
  56. {
  57. u32 src_reg;
  58. if (streq(hwclk->init->name, SOCFPGA_L4_MP_CLK)) {
  59. src_reg = readl(clk_mgr_base_addr + CLKMGR_L4SRC);
  60. src_reg &= ~0x1;
  61. src_reg |= parent;
  62. writel(src_reg, clk_mgr_base_addr + CLKMGR_L4SRC);
  63. } else if (streq(hwclk->init->name, SOCFPGA_L4_SP_CLK)) {
  64. src_reg = readl(clk_mgr_base_addr + CLKMGR_L4SRC);
  65. src_reg &= ~0x2;
  66. src_reg |= (parent << 1);
  67. writel(src_reg, clk_mgr_base_addr + CLKMGR_L4SRC);
  68. } else {
  69. src_reg = readl(clk_mgr_base_addr + CLKMGR_PERPLL_SRC);
  70. if (streq(hwclk->init->name, SOCFPGA_MMC_CLK)) {
  71. src_reg &= ~0x3;
  72. src_reg |= parent;
  73. } else if (streq(hwclk->init->name, SOCFPGA_NAND_CLK) ||
  74. streq(hwclk->init->name, SOCFPGA_NAND_X_CLK)) {
  75. src_reg &= ~0xC;
  76. src_reg |= (parent << 2);
  77. } else {/* QSPI clock */
  78. src_reg &= ~0x30;
  79. src_reg |= (parent << 4);
  80. }
  81. writel(src_reg, clk_mgr_base_addr + CLKMGR_PERPLL_SRC);
  82. }
  83. return 0;
  84. }
  85. static unsigned long socfpga_clk_recalc_rate(struct clk_hw *hwclk,
  86. unsigned long parent_rate)
  87. {
  88. struct socfpga_gate_clk *socfpgaclk = to_socfpga_gate_clk(hwclk);
  89. u32 div = 1, val;
  90. if (socfpgaclk->fixed_div)
  91. div = socfpgaclk->fixed_div;
  92. else if (socfpgaclk->div_reg) {
  93. val = readl(socfpgaclk->div_reg) >> socfpgaclk->shift;
  94. val &= GENMASK(socfpgaclk->width - 1, 0);
  95. /* Check for GPIO_DB_CLK by its offset */
  96. if ((int) socfpgaclk->div_reg & SOCFPGA_GPIO_DB_CLK_OFFSET)
  97. div = val + 1;
  98. else
  99. div = (1 << val);
  100. }
  101. return parent_rate / div;
  102. }
  103. static int socfpga_clk_prepare(struct clk_hw *hwclk)
  104. {
  105. struct socfpga_gate_clk *socfpgaclk = to_socfpga_gate_clk(hwclk);
  106. struct regmap *sys_mgr_base_addr;
  107. int i;
  108. u32 hs_timing;
  109. u32 clk_phase[2];
  110. if (socfpgaclk->clk_phase[0] || socfpgaclk->clk_phase[1]) {
  111. sys_mgr_base_addr = syscon_regmap_lookup_by_compatible("altr,sys-mgr");
  112. if (IS_ERR(sys_mgr_base_addr)) {
  113. pr_err("%s: failed to find altr,sys-mgr regmap!\n", __func__);
  114. return -EINVAL;
  115. }
  116. for (i = 0; i < 2; i++) {
  117. switch (socfpgaclk->clk_phase[i]) {
  118. case 0:
  119. clk_phase[i] = 0;
  120. break;
  121. case 45:
  122. clk_phase[i] = 1;
  123. break;
  124. case 90:
  125. clk_phase[i] = 2;
  126. break;
  127. case 135:
  128. clk_phase[i] = 3;
  129. break;
  130. case 180:
  131. clk_phase[i] = 4;
  132. break;
  133. case 225:
  134. clk_phase[i] = 5;
  135. break;
  136. case 270:
  137. clk_phase[i] = 6;
  138. break;
  139. case 315:
  140. clk_phase[i] = 7;
  141. break;
  142. default:
  143. clk_phase[i] = 0;
  144. break;
  145. }
  146. }
  147. hs_timing = SYSMGR_SDMMC_CTRL_SET(clk_phase[0], clk_phase[1]);
  148. regmap_write(sys_mgr_base_addr, SYSMGR_SDMMCGRP_CTRL_OFFSET,
  149. hs_timing);
  150. }
  151. return 0;
  152. }
  153. static struct clk_ops gateclk_ops = {
  154. .prepare = socfpga_clk_prepare,
  155. .recalc_rate = socfpga_clk_recalc_rate,
  156. .get_parent = socfpga_clk_get_parent,
  157. .set_parent = socfpga_clk_set_parent,
  158. };
  159. static void __init __socfpga_gate_init(struct device_node *node,
  160. const struct clk_ops *ops)
  161. {
  162. u32 clk_gate[2];
  163. u32 div_reg[3];
  164. u32 clk_phase[2];
  165. u32 fixed_div;
  166. struct clk *clk;
  167. struct socfpga_gate_clk *socfpga_clk;
  168. const char *clk_name = node->name;
  169. const char *parent_name[SOCFPGA_MAX_PARENTS];
  170. struct clk_init_data init;
  171. int rc;
  172. socfpga_clk = kzalloc(sizeof(*socfpga_clk), GFP_KERNEL);
  173. if (WARN_ON(!socfpga_clk))
  174. return;
  175. rc = of_property_read_u32_array(node, "clk-gate", clk_gate, 2);
  176. if (rc)
  177. clk_gate[0] = 0;
  178. if (clk_gate[0]) {
  179. socfpga_clk->hw.reg = clk_mgr_base_addr + clk_gate[0];
  180. socfpga_clk->hw.bit_idx = clk_gate[1];
  181. gateclk_ops.enable = clk_gate_ops.enable;
  182. gateclk_ops.disable = clk_gate_ops.disable;
  183. }
  184. rc = of_property_read_u32(node, "fixed-divider", &fixed_div);
  185. if (rc)
  186. socfpga_clk->fixed_div = 0;
  187. else
  188. socfpga_clk->fixed_div = fixed_div;
  189. rc = of_property_read_u32_array(node, "div-reg", div_reg, 3);
  190. if (!rc) {
  191. socfpga_clk->div_reg = clk_mgr_base_addr + div_reg[0];
  192. socfpga_clk->shift = div_reg[1];
  193. socfpga_clk->width = div_reg[2];
  194. } else {
  195. socfpga_clk->div_reg = NULL;
  196. }
  197. rc = of_property_read_u32_array(node, "clk-phase", clk_phase, 2);
  198. if (!rc) {
  199. socfpga_clk->clk_phase[0] = clk_phase[0];
  200. socfpga_clk->clk_phase[1] = clk_phase[1];
  201. }
  202. of_property_read_string(node, "clock-output-names", &clk_name);
  203. init.name = clk_name;
  204. init.ops = ops;
  205. init.flags = 0;
  206. init.num_parents = of_clk_parent_fill(node, parent_name, SOCFPGA_MAX_PARENTS);
  207. init.parent_names = parent_name;
  208. socfpga_clk->hw.hw.init = &init;
  209. clk = clk_register(NULL, &socfpga_clk->hw.hw);
  210. if (WARN_ON(IS_ERR(clk))) {
  211. kfree(socfpga_clk);
  212. return;
  213. }
  214. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  215. if (WARN_ON(rc))
  216. return;
  217. }
  218. void __init socfpga_gate_init(struct device_node *node)
  219. {
  220. __socfpga_gate_init(node, &gateclk_ops);
  221. }