clk.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "mmc_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. rc = of_property_read_u32(node, "reg", &reg);
  107. if (WARN_ON(rc))
  108. return NULL;
  109. socfpga_clk = kzalloc(sizeof(*socfpga_clk), GFP_KERNEL);
  110. if (WARN_ON(!socfpga_clk))
  111. return NULL;
  112. socfpga_clk->hw.reg = clk_mgr_base_addr + reg;
  113. rc = of_property_read_u32(node, "fixed-divider", &fixed_div);
  114. if (rc)
  115. socfpga_clk->fixed_div = 0;
  116. else
  117. socfpga_clk->fixed_div = fixed_div;
  118. of_property_read_string(node, "clock-output-names", &clk_name);
  119. init.name = clk_name;
  120. init.ops = ops;
  121. init.flags = 0;
  122. parent_name = of_clk_get_parent_name(node, 0);
  123. init.parent_names = &parent_name;
  124. init.num_parents = 1;
  125. socfpga_clk->hw.hw.init = &init;
  126. if (streq(clk_name, "main_pll") ||
  127. streq(clk_name, "periph_pll") ||
  128. streq(clk_name, "sdram_pll")) {
  129. socfpga_clk->hw.bit_idx = SOCFPGA_PLL_EXT_ENA;
  130. clk_pll_ops.enable = clk_gate_ops.enable;
  131. clk_pll_ops.disable = clk_gate_ops.disable;
  132. }
  133. clk = clk_register(NULL, &socfpga_clk->hw.hw);
  134. if (WARN_ON(IS_ERR(clk))) {
  135. kfree(socfpga_clk);
  136. return NULL;
  137. }
  138. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  139. return clk;
  140. }
  141. static u8 socfpga_clk_get_parent(struct clk_hw *hwclk)
  142. {
  143. u32 l4_src;
  144. u32 perpll_src;
  145. if (streq(hwclk->init->name, SOCFPGA_L4_MP_CLK)) {
  146. l4_src = readl(clk_mgr_base_addr + CLKMGR_L4SRC);
  147. return l4_src &= 0x1;
  148. }
  149. if (streq(hwclk->init->name, SOCFPGA_L4_SP_CLK)) {
  150. l4_src = readl(clk_mgr_base_addr + CLKMGR_L4SRC);
  151. return !!(l4_src & 2);
  152. }
  153. perpll_src = readl(clk_mgr_base_addr + CLKMGR_PERPLL_SRC);
  154. if (streq(hwclk->init->name, SOCFPGA_MMC_CLK))
  155. return perpll_src &= 0x3;
  156. if (streq(hwclk->init->name, SOCFPGA_NAND_CLK) ||
  157. streq(hwclk->init->name, SOCFPGA_NAND_X_CLK))
  158. return (perpll_src >> 2) & 3;
  159. /* QSPI clock */
  160. return (perpll_src >> 4) & 3;
  161. }
  162. static int socfpga_clk_set_parent(struct clk_hw *hwclk, u8 parent)
  163. {
  164. u32 src_reg;
  165. if (streq(hwclk->init->name, SOCFPGA_L4_MP_CLK)) {
  166. src_reg = readl(clk_mgr_base_addr + CLKMGR_L4SRC);
  167. src_reg &= ~0x1;
  168. src_reg |= parent;
  169. writel(src_reg, clk_mgr_base_addr + CLKMGR_L4SRC);
  170. } else if (streq(hwclk->init->name, SOCFPGA_L4_SP_CLK)) {
  171. src_reg = readl(clk_mgr_base_addr + CLKMGR_L4SRC);
  172. src_reg &= ~0x2;
  173. src_reg |= (parent << 1);
  174. writel(src_reg, clk_mgr_base_addr + CLKMGR_L4SRC);
  175. } else {
  176. src_reg = readl(clk_mgr_base_addr + CLKMGR_PERPLL_SRC);
  177. if (streq(hwclk->init->name, SOCFPGA_MMC_CLK)) {
  178. src_reg &= ~0x3;
  179. src_reg |= parent;
  180. } else if (streq(hwclk->init->name, SOCFPGA_NAND_CLK) ||
  181. streq(hwclk->init->name, SOCFPGA_NAND_X_CLK)) {
  182. src_reg &= ~0xC;
  183. src_reg |= (parent << 2);
  184. } else {/* QSPI clock */
  185. src_reg &= ~0x30;
  186. src_reg |= (parent << 4);
  187. }
  188. writel(src_reg, clk_mgr_base_addr + CLKMGR_PERPLL_SRC);
  189. }
  190. return 0;
  191. }
  192. static unsigned long socfpga_clk_recalc_rate(struct clk_hw *hwclk,
  193. unsigned long parent_rate)
  194. {
  195. struct socfpga_clk *socfpgaclk = to_socfpga_clk(hwclk);
  196. u32 div = 1, val;
  197. if (socfpgaclk->fixed_div)
  198. div = socfpgaclk->fixed_div;
  199. else if (socfpgaclk->div_reg) {
  200. val = readl(socfpgaclk->div_reg) >> socfpgaclk->shift;
  201. val &= div_mask(socfpgaclk->width);
  202. if (streq(hwclk->init->name, SOCFPGA_DB_CLK))
  203. div = val + 1;
  204. else
  205. div = (1 << val);
  206. }
  207. return parent_rate / div;
  208. }
  209. static struct clk_ops gateclk_ops = {
  210. .recalc_rate = socfpga_clk_recalc_rate,
  211. .get_parent = socfpga_clk_get_parent,
  212. .set_parent = socfpga_clk_set_parent,
  213. };
  214. static void __init socfpga_gate_clk_init(struct device_node *node,
  215. const struct clk_ops *ops)
  216. {
  217. u32 clk_gate[2];
  218. u32 div_reg[3];
  219. u32 fixed_div;
  220. struct clk *clk;
  221. struct socfpga_clk *socfpga_clk;
  222. const char *clk_name = node->name;
  223. const char *parent_name[SOCFGPA_MAX_PARENTS];
  224. struct clk_init_data init;
  225. int rc;
  226. int i = 0;
  227. socfpga_clk = kzalloc(sizeof(*socfpga_clk), GFP_KERNEL);
  228. if (WARN_ON(!socfpga_clk))
  229. return;
  230. rc = of_property_read_u32_array(node, "clk-gate", clk_gate, 2);
  231. if (rc)
  232. clk_gate[0] = 0;
  233. if (clk_gate[0]) {
  234. socfpga_clk->hw.reg = clk_mgr_base_addr + clk_gate[0];
  235. socfpga_clk->hw.bit_idx = clk_gate[1];
  236. gateclk_ops.enable = clk_gate_ops.enable;
  237. gateclk_ops.disable = clk_gate_ops.disable;
  238. }
  239. rc = of_property_read_u32(node, "fixed-divider", &fixed_div);
  240. if (rc)
  241. socfpga_clk->fixed_div = 0;
  242. else
  243. socfpga_clk->fixed_div = fixed_div;
  244. rc = of_property_read_u32_array(node, "div-reg", div_reg, 3);
  245. if (!rc) {
  246. socfpga_clk->div_reg = clk_mgr_base_addr + div_reg[0];
  247. socfpga_clk->shift = div_reg[1];
  248. socfpga_clk->width = div_reg[2];
  249. } else {
  250. socfpga_clk->div_reg = 0;
  251. }
  252. of_property_read_string(node, "clock-output-names", &clk_name);
  253. init.name = clk_name;
  254. init.ops = ops;
  255. init.flags = 0;
  256. while (i < SOCFGPA_MAX_PARENTS && (parent_name[i] =
  257. of_clk_get_parent_name(node, i)) != NULL)
  258. i++;
  259. init.parent_names = parent_name;
  260. init.num_parents = i;
  261. socfpga_clk->hw.hw.init = &init;
  262. clk = clk_register(NULL, &socfpga_clk->hw.hw);
  263. if (WARN_ON(IS_ERR(clk))) {
  264. kfree(socfpga_clk);
  265. return;
  266. }
  267. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  268. if (WARN_ON(rc))
  269. return;
  270. }
  271. static void __init socfpga_pll_init(struct device_node *node)
  272. {
  273. socfpga_clk_init(node, &clk_pll_ops);
  274. }
  275. CLK_OF_DECLARE(socfpga_pll, "altr,socfpga-pll-clock", socfpga_pll_init);
  276. static void __init socfpga_periph_init(struct device_node *node)
  277. {
  278. socfpga_clk_init(node, &periclk_ops);
  279. }
  280. CLK_OF_DECLARE(socfpga_periph, "altr,socfpga-perip-clk", socfpga_periph_init);
  281. static void __init socfpga_gate_init(struct device_node *node)
  282. {
  283. socfpga_gate_clk_init(node, &gateclk_ops);
  284. }
  285. CLK_OF_DECLARE(socfpga_gate, "altr,socfpga-gate-clk", socfpga_gate_init);
  286. void __init socfpga_init_clocks(void)
  287. {
  288. struct clk *clk;
  289. int ret;
  290. clk = clk_register_fixed_factor(NULL, "smp_twd", "mpuclk", 0, 1, 4);
  291. ret = clk_register_clkdev(clk, NULL, "smp_twd");
  292. if (ret)
  293. pr_err("smp_twd alias not registered\n");
  294. }