clk.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/clkdev.h>
  22. #include <linux/clk-provider.h>
  23. #include <linux/io.h>
  24. #include <linux/of.h>
  25. /* Clock Manager offsets */
  26. #define CLKMGR_CTRL 0x0
  27. #define CLKMGR_BYPASS 0x4
  28. #define CLKMGR_L4SRC 0x70
  29. #define CLKMGR_PERPLL_SRC 0xAC
  30. /* Clock bypass bits */
  31. #define MAINPLL_BYPASS (1<<0)
  32. #define SDRAMPLL_BYPASS (1<<1)
  33. #define SDRAMPLL_SRC_BYPASS (1<<2)
  34. #define PERPLL_BYPASS (1<<3)
  35. #define PERPLL_SRC_BYPASS (1<<4)
  36. #define SOCFPGA_PLL_BG_PWRDWN 0
  37. #define SOCFPGA_PLL_EXT_ENA 1
  38. #define SOCFPGA_PLL_PWR_DOWN 2
  39. #define SOCFPGA_PLL_DIVF_MASK 0x0000FFF8
  40. #define SOCFPGA_PLL_DIVF_SHIFT 3
  41. #define SOCFPGA_PLL_DIVQ_MASK 0x003F0000
  42. #define SOCFPGA_PLL_DIVQ_SHIFT 16
  43. #define SOCFGPA_MAX_PARENTS 3
  44. #define SOCFPGA_L4_MP_CLK "l4_mp_clk"
  45. #define SOCFPGA_L4_SP_CLK "l4_sp_clk"
  46. #define SOCFPGA_NAND_CLK "nand_clk"
  47. #define SOCFPGA_NAND_X_CLK "nand_x_clk"
  48. #define SOCFPGA_MMC_CLK "sdmmc_clk"
  49. #define SOCFPGA_DB_CLK "gpio_db_clk"
  50. #define div_mask(width) ((1 << (width)) - 1)
  51. #define streq(a, b) (strcmp((a), (b)) == 0)
  52. extern void __iomem *clk_mgr_base_addr;
  53. struct socfpga_clk {
  54. struct clk_gate hw;
  55. char *parent_name;
  56. char *clk_name;
  57. u32 fixed_div;
  58. void __iomem *div_reg;
  59. u32 width; /* only valid if div_reg != 0 */
  60. u32 shift; /* only valid if div_reg != 0 */
  61. };
  62. #define to_socfpga_clk(p) container_of(p, struct socfpga_clk, hw.hw)
  63. static unsigned long clk_pll_recalc_rate(struct clk_hw *hwclk,
  64. unsigned long parent_rate)
  65. {
  66. struct socfpga_clk *socfpgaclk = to_socfpga_clk(hwclk);
  67. unsigned long divf, divq, vco_freq, reg;
  68. unsigned long bypass;
  69. reg = readl(socfpgaclk->hw.reg);
  70. bypass = readl(clk_mgr_base_addr + CLKMGR_BYPASS);
  71. if (bypass & MAINPLL_BYPASS)
  72. return parent_rate;
  73. divf = (reg & SOCFPGA_PLL_DIVF_MASK) >> SOCFPGA_PLL_DIVF_SHIFT;
  74. divq = (reg & SOCFPGA_PLL_DIVQ_MASK) >> SOCFPGA_PLL_DIVQ_SHIFT;
  75. vco_freq = parent_rate * (divf + 1);
  76. return vco_freq / (1 + divq);
  77. }
  78. static struct clk_ops clk_pll_ops = {
  79. .recalc_rate = clk_pll_recalc_rate,
  80. };
  81. static unsigned long clk_periclk_recalc_rate(struct clk_hw *hwclk,
  82. unsigned long parent_rate)
  83. {
  84. struct socfpga_clk *socfpgaclk = to_socfpga_clk(hwclk);
  85. u32 div;
  86. if (socfpgaclk->fixed_div)
  87. div = socfpgaclk->fixed_div;
  88. else
  89. div = ((readl(socfpgaclk->hw.reg) & 0x1ff) + 1);
  90. return parent_rate / div;
  91. }
  92. static const struct clk_ops periclk_ops = {
  93. .recalc_rate = clk_periclk_recalc_rate,
  94. };
  95. static __init struct clk *socfpga_clk_init(struct device_node *node,
  96. const struct clk_ops *ops)
  97. {
  98. u32 reg;
  99. struct clk *clk;
  100. struct socfpga_clk *socfpga_clk;
  101. const char *clk_name = node->name;
  102. const char *parent_name;
  103. struct clk_init_data init;
  104. int rc;
  105. u32 fixed_div;
  106. of_property_read_u32(node, "reg", &reg);
  107. socfpga_clk = kzalloc(sizeof(*socfpga_clk), GFP_KERNEL);
  108. if (WARN_ON(!socfpga_clk))
  109. return NULL;
  110. socfpga_clk->hw.reg = clk_mgr_base_addr + reg;
  111. rc = of_property_read_u32(node, "fixed-divider", &fixed_div);
  112. if (rc)
  113. socfpga_clk->fixed_div = 0;
  114. else
  115. socfpga_clk->fixed_div = fixed_div;
  116. of_property_read_string(node, "clock-output-names", &clk_name);
  117. init.name = clk_name;
  118. init.ops = ops;
  119. init.flags = 0;
  120. parent_name = of_clk_get_parent_name(node, 0);
  121. init.parent_names = &parent_name;
  122. init.num_parents = 1;
  123. socfpga_clk->hw.hw.init = &init;
  124. if (streq(clk_name, "main_pll") ||
  125. streq(clk_name, "periph_pll") ||
  126. streq(clk_name, "sdram_pll")) {
  127. socfpga_clk->hw.bit_idx = SOCFPGA_PLL_EXT_ENA;
  128. clk_pll_ops.enable = clk_gate_ops.enable;
  129. clk_pll_ops.disable = clk_gate_ops.disable;
  130. }
  131. clk = clk_register(NULL, &socfpga_clk->hw.hw);
  132. if (WARN_ON(IS_ERR(clk))) {
  133. kfree(socfpga_clk);
  134. return NULL;
  135. }
  136. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  137. return clk;
  138. }
  139. static u8 socfpga_clk_get_parent(struct clk_hw *hwclk)
  140. {
  141. u32 l4_src;
  142. u32 perpll_src;
  143. if (streq(hwclk->init->name, SOCFPGA_L4_MP_CLK)) {
  144. l4_src = readl(clk_mgr_base_addr + CLKMGR_L4SRC);
  145. return l4_src &= 0x1;
  146. }
  147. if (streq(hwclk->init->name, SOCFPGA_L4_SP_CLK)) {
  148. l4_src = readl(clk_mgr_base_addr + CLKMGR_L4SRC);
  149. return !!(l4_src & 2);
  150. }
  151. perpll_src = readl(clk_mgr_base_addr + CLKMGR_PERPLL_SRC);
  152. if (streq(hwclk->init->name, SOCFPGA_MMC_CLK))
  153. return perpll_src &= 0x3;
  154. if (streq(hwclk->init->name, SOCFPGA_NAND_CLK) ||
  155. streq(hwclk->init->name, SOCFPGA_NAND_X_CLK))
  156. return (perpll_src >> 2) & 3;
  157. /* QSPI clock */
  158. return (perpll_src >> 4) & 3;
  159. }
  160. static int socfpga_clk_set_parent(struct clk_hw *hwclk, u8 parent)
  161. {
  162. u32 src_reg;
  163. if (streq(hwclk->init->name, SOCFPGA_L4_MP_CLK)) {
  164. src_reg = readl(clk_mgr_base_addr + CLKMGR_L4SRC);
  165. src_reg &= ~0x1;
  166. src_reg |= parent;
  167. writel(src_reg, clk_mgr_base_addr + CLKMGR_L4SRC);
  168. } else if (streq(hwclk->init->name, SOCFPGA_L4_SP_CLK)) {
  169. src_reg = readl(clk_mgr_base_addr + CLKMGR_L4SRC);
  170. src_reg &= ~0x2;
  171. src_reg |= (parent << 1);
  172. writel(src_reg, clk_mgr_base_addr + CLKMGR_L4SRC);
  173. } else {
  174. src_reg = readl(clk_mgr_base_addr + CLKMGR_PERPLL_SRC);
  175. if (streq(hwclk->init->name, SOCFPGA_MMC_CLK)) {
  176. src_reg &= ~0x3;
  177. src_reg |= parent;
  178. } else if (streq(hwclk->init->name, SOCFPGA_NAND_CLK) ||
  179. streq(hwclk->init->name, SOCFPGA_NAND_X_CLK)) {
  180. src_reg &= ~0xC;
  181. src_reg |= (parent << 2);
  182. } else {/* QSPI clock */
  183. src_reg &= ~0x30;
  184. src_reg |= (parent << 4);
  185. }
  186. writel(src_reg, clk_mgr_base_addr + CLKMGR_PERPLL_SRC);
  187. }
  188. return 0;
  189. }
  190. static unsigned long socfpga_clk_recalc_rate(struct clk_hw *hwclk,
  191. unsigned long parent_rate)
  192. {
  193. struct socfpga_clk *socfpgaclk = to_socfpga_clk(hwclk);
  194. u32 div = 1, val;
  195. if (socfpgaclk->fixed_div)
  196. div = socfpgaclk->fixed_div;
  197. else if (socfpgaclk->div_reg) {
  198. val = readl(socfpgaclk->div_reg) >> socfpgaclk->shift;
  199. val &= div_mask(socfpgaclk->width);
  200. if (streq(hwclk->init->name, SOCFPGA_DB_CLK))
  201. div = val + 1;
  202. else
  203. div = (1 << val);
  204. }
  205. return parent_rate / div;
  206. }
  207. static struct clk_ops gateclk_ops = {
  208. .recalc_rate = socfpga_clk_recalc_rate,
  209. .get_parent = socfpga_clk_get_parent,
  210. .set_parent = socfpga_clk_set_parent,
  211. };
  212. static void __init socfpga_gate_clk_init(struct device_node *node,
  213. const struct clk_ops *ops)
  214. {
  215. u32 clk_gate[2];
  216. u32 div_reg[3];
  217. u32 fixed_div;
  218. struct clk *clk;
  219. struct socfpga_clk *socfpga_clk;
  220. const char *clk_name = node->name;
  221. const char *parent_name[SOCFGPA_MAX_PARENTS];
  222. struct clk_init_data init;
  223. int rc;
  224. int i = 0;
  225. socfpga_clk = kzalloc(sizeof(*socfpga_clk), GFP_KERNEL);
  226. if (WARN_ON(!socfpga_clk))
  227. return;
  228. rc = of_property_read_u32_array(node, "clk-gate", clk_gate, 2);
  229. if (rc)
  230. clk_gate[0] = 0;
  231. if (clk_gate[0]) {
  232. socfpga_clk->hw.reg = clk_mgr_base_addr + clk_gate[0];
  233. socfpga_clk->hw.bit_idx = clk_gate[1];
  234. gateclk_ops.enable = clk_gate_ops.enable;
  235. gateclk_ops.disable = clk_gate_ops.disable;
  236. }
  237. rc = of_property_read_u32(node, "fixed-divider", &fixed_div);
  238. if (rc)
  239. socfpga_clk->fixed_div = 0;
  240. else
  241. socfpga_clk->fixed_div = fixed_div;
  242. rc = of_property_read_u32_array(node, "div-reg", div_reg, 3);
  243. if (!rc) {
  244. socfpga_clk->div_reg = clk_mgr_base_addr + div_reg[0];
  245. socfpga_clk->shift = div_reg[1];
  246. socfpga_clk->width = div_reg[2];
  247. } else {
  248. socfpga_clk->div_reg = NULL;
  249. }
  250. of_property_read_string(node, "clock-output-names", &clk_name);
  251. init.name = clk_name;
  252. init.ops = ops;
  253. init.flags = 0;
  254. while (i < SOCFGPA_MAX_PARENTS && (parent_name[i] =
  255. of_clk_get_parent_name(node, i)) != NULL)
  256. i++;
  257. init.parent_names = parent_name;
  258. init.num_parents = i;
  259. socfpga_clk->hw.hw.init = &init;
  260. clk = clk_register(NULL, &socfpga_clk->hw.hw);
  261. if (WARN_ON(IS_ERR(clk))) {
  262. kfree(socfpga_clk);
  263. return;
  264. }
  265. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  266. if (WARN_ON(rc))
  267. return;
  268. }
  269. static void __init socfpga_pll_init(struct device_node *node)
  270. {
  271. socfpga_clk_init(node, &clk_pll_ops);
  272. }
  273. CLK_OF_DECLARE(socfpga_pll, "altr,socfpga-pll-clock", socfpga_pll_init);
  274. static void __init socfpga_periph_init(struct device_node *node)
  275. {
  276. socfpga_clk_init(node, &periclk_ops);
  277. }
  278. CLK_OF_DECLARE(socfpga_periph, "altr,socfpga-perip-clk", socfpga_periph_init);
  279. static void __init socfpga_gate_init(struct device_node *node)
  280. {
  281. socfpga_gate_clk_init(node, &gateclk_ops);
  282. }
  283. CLK_OF_DECLARE(socfpga_gate, "altr,socfpga-gate-clk", socfpga_gate_init);
  284. void __init socfpga_init_clocks(void)
  285. {
  286. struct clk *clk;
  287. int ret;
  288. clk = clk_register_fixed_factor(NULL, "smp_twd", "mpuclk", 0, 1, 4);
  289. ret = clk_register_clkdev(clk, NULL, "smp_twd");
  290. if (ret)
  291. pr_err("smp_twd alias not registered\n");
  292. }