clk-oxnas.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2010 Broadcom
  3. * Copyright (C) 2012 Stephen Warren
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/clkdev.h>
  19. #include <linux/clk-provider.h>
  20. #include <linux/of.h>
  21. #include <linux/delay.h>
  22. #include <linux/stringify.h>
  23. #include <linux/reset.h>
  24. #include <linux/io.h>
  25. #include <mach/hardware.h>
  26. #include <mach/utils.h>
  27. #define MHZ (1000 * 1000)
  28. struct clk_oxnas_pllb {
  29. struct clk_hw hw;
  30. struct device_node *devnode;
  31. struct reset_control *rstc;
  32. };
  33. #define to_clk_oxnas_pllb(_hw) container_of(_hw, struct clk_oxnas_pllb, hw)
  34. static unsigned long plla_clk_recalc_rate(struct clk_hw *hw,
  35. unsigned long parent_rate)
  36. {
  37. unsigned long fin = parent_rate;
  38. unsigned long pll0;
  39. unsigned long fbdiv, refdiv, outdiv;
  40. pll0 = readl_relaxed(SYS_CTRL_PLLA_CTRL0);
  41. refdiv = (pll0 >> PLLA_REFDIV_SHIFT) & PLLA_REFDIV_MASK;
  42. refdiv += 1;
  43. outdiv = (pll0 >> PLLA_OUTDIV_SHIFT) & PLLA_OUTDIV_MASK;
  44. outdiv += 1;
  45. fbdiv = readl_relaxed(SYS_CTRL_PLLA_CTRL1);
  46. /* seems we will not be here when pll is bypassed, so ignore this
  47. * case */
  48. return fin / MHZ * fbdiv / (refdiv * outdiv) / 32768 * MHZ;
  49. }
  50. static const char *pll_clk_parents[] = {
  51. "oscillator",
  52. };
  53. static struct clk_ops plla_ops = {
  54. .recalc_rate = plla_clk_recalc_rate,
  55. };
  56. static struct clk_init_data clk_plla_init = {
  57. .name = "plla",
  58. .ops = &plla_ops,
  59. .parent_names = pll_clk_parents,
  60. .num_parents = ARRAY_SIZE(pll_clk_parents),
  61. };
  62. static struct clk_hw plla_hw = {
  63. .init = &clk_plla_init,
  64. };
  65. static int pllb_clk_is_prepared(struct clk_hw *hw)
  66. {
  67. struct clk_oxnas_pllb *pllb = to_clk_oxnas_pllb(hw);
  68. return !!pllb->rstc;
  69. }
  70. static int pllb_clk_prepare(struct clk_hw *hw)
  71. {
  72. struct clk_oxnas_pllb *pllb = to_clk_oxnas_pllb(hw);
  73. pllb->rstc = of_reset_control_get(pllb->devnode, NULL);
  74. return IS_ERR(pllb->rstc) ? PTR_ERR(pllb->rstc) : 0;
  75. }
  76. static void pllb_clk_unprepare(struct clk_hw *hw)
  77. {
  78. struct clk_oxnas_pllb *pllb = to_clk_oxnas_pllb(hw);
  79. BUG_ON(IS_ERR(pllb->rstc));
  80. reset_control_put(pllb->rstc);
  81. pllb->rstc = NULL;
  82. }
  83. static int pllb_clk_enable(struct clk_hw *hw)
  84. {
  85. struct clk_oxnas_pllb *pllb = to_clk_oxnas_pllb(hw);
  86. BUG_ON(IS_ERR(pllb->rstc));
  87. /* put PLL into bypass */
  88. oxnas_register_set_mask(SEC_CTRL_PLLB_CTRL0, BIT(PLLB_BYPASS));
  89. wmb();
  90. udelay(10);
  91. reset_control_assert(pllb->rstc);
  92. udelay(10);
  93. /* set PLL B control information */
  94. writel((1 << PLLB_ENSAT) | (1 << PLLB_OUTDIV) | (2 << PLLB_REFDIV),
  95. SEC_CTRL_PLLB_CTRL0);
  96. reset_control_deassert(pllb->rstc);
  97. udelay(100);
  98. oxnas_register_clear_mask(SEC_CTRL_PLLB_CTRL0, BIT(PLLB_BYPASS));
  99. return 0;
  100. }
  101. static void pllb_clk_disable(struct clk_hw *hw)
  102. {
  103. struct clk_oxnas_pllb *pllb = to_clk_oxnas_pllb(hw);
  104. BUG_ON(IS_ERR(pllb->rstc));
  105. /* put PLL into bypass */
  106. oxnas_register_set_mask(SEC_CTRL_PLLB_CTRL0, BIT(PLLB_BYPASS));
  107. wmb();
  108. udelay(10);
  109. reset_control_assert(pllb->rstc);
  110. }
  111. static struct clk_ops pllb_ops = {
  112. .prepare = pllb_clk_prepare,
  113. .unprepare = pllb_clk_unprepare,
  114. .is_prepared = pllb_clk_is_prepared,
  115. .enable = pllb_clk_enable,
  116. .disable = pllb_clk_disable,
  117. };
  118. static struct clk_init_data clk_pllb_init = {
  119. .name = "pllb",
  120. .ops = &pllb_ops,
  121. .parent_names = pll_clk_parents,
  122. .num_parents = ARRAY_SIZE(pll_clk_parents),
  123. };
  124. /* standard gate clock */
  125. struct clk_std {
  126. struct clk_hw hw;
  127. signed char bit;
  128. };
  129. #define NUM_STD_CLKS 17
  130. #define to_stdclk(_hw) container_of(_hw, struct clk_std, hw)
  131. static int std_clk_is_enabled(struct clk_hw *hw)
  132. {
  133. struct clk_std *std = to_stdclk(hw);
  134. return readl_relaxed(SYSCTRL_CLK_STAT) & BIT(std->bit);
  135. }
  136. static int std_clk_enable(struct clk_hw *hw)
  137. {
  138. struct clk_std *std = to_stdclk(hw);
  139. writel(BIT(std->bit), SYS_CTRL_CLK_SET_CTRL);
  140. return 0;
  141. }
  142. static void std_clk_disable(struct clk_hw *hw)
  143. {
  144. struct clk_std *std = to_stdclk(hw);
  145. writel(BIT(std->bit), SYS_CTRL_CLK_CLR_CTRL);
  146. }
  147. static struct clk_ops std_clk_ops = {
  148. .enable = std_clk_enable,
  149. .disable = std_clk_disable,
  150. .is_enabled = std_clk_is_enabled,
  151. };
  152. static const char *std_clk_parents[] = {
  153. "oscillator",
  154. };
  155. static const char *eth_parents[] = {
  156. "gmacclk",
  157. };
  158. #define DECLARE_STD_CLKP(__clk, __bit, __parent) \
  159. static struct clk_init_data clk_##__clk##_init = { \
  160. .name = __stringify(__clk), \
  161. .ops = &std_clk_ops, \
  162. .parent_names = __parent, \
  163. .num_parents = ARRAY_SIZE(__parent), \
  164. }; \
  165. \
  166. static struct clk_std clk_##__clk = { \
  167. .bit = __bit, \
  168. .hw = { \
  169. .init = &clk_##__clk##_init, \
  170. }, \
  171. }
  172. #define DECLARE_STD_CLK(__clk, __bit) DECLARE_STD_CLKP(__clk, __bit, \
  173. std_clk_parents)
  174. DECLARE_STD_CLK(leon, 0);
  175. DECLARE_STD_CLK(dma_sgdma, 1);
  176. DECLARE_STD_CLK(cipher, 2);
  177. DECLARE_STD_CLK(sd, 3);
  178. DECLARE_STD_CLK(sata, 4);
  179. DECLARE_STD_CLK(audio, 5);
  180. DECLARE_STD_CLK(usbmph, 6);
  181. DECLARE_STD_CLKP(etha, 7, eth_parents);
  182. DECLARE_STD_CLK(pciea, 8);
  183. DECLARE_STD_CLK(static, 9);
  184. DECLARE_STD_CLK(ethb, 10);
  185. DECLARE_STD_CLK(pcieb, 11);
  186. DECLARE_STD_CLK(ref600, 12);
  187. DECLARE_STD_CLK(usbdev, 13);
  188. struct clk_hw *std_clk_hw_tbl[] = {
  189. &clk_leon.hw,
  190. &clk_dma_sgdma.hw,
  191. &clk_cipher.hw,
  192. &clk_sd.hw,
  193. &clk_sata.hw,
  194. &clk_audio.hw,
  195. &clk_usbmph.hw,
  196. &clk_etha.hw,
  197. &clk_pciea.hw,
  198. &clk_static.hw,
  199. &clk_ethb.hw,
  200. &clk_pcieb.hw,
  201. &clk_ref600.hw,
  202. &clk_usbdev.hw,
  203. };
  204. struct clk *std_clk_tbl[ARRAY_SIZE(std_clk_hw_tbl)];
  205. static struct clk_onecell_data std_clk_data;
  206. void __init oxnas_init_stdclk(struct device_node *np)
  207. {
  208. int i;
  209. for (i = 0; i < ARRAY_SIZE(std_clk_hw_tbl); i++) {
  210. std_clk_tbl[i] = clk_register(NULL, std_clk_hw_tbl[i]);
  211. BUG_ON(IS_ERR(std_clk_tbl[i]));
  212. }
  213. std_clk_data.clks = std_clk_tbl;
  214. std_clk_data.clk_num = ARRAY_SIZE(std_clk_tbl);
  215. of_clk_add_provider(np, of_clk_src_onecell_get, &std_clk_data);
  216. }
  217. CLK_OF_DECLARE(oxnas_pllstd, "plxtech,nas782x-stdclk", oxnas_init_stdclk);
  218. void __init oxnas_init_plla(struct device_node *np)
  219. {
  220. struct clk *clk;
  221. clk = clk_register(NULL, &plla_hw);
  222. BUG_ON(IS_ERR(clk));
  223. /* mark it as enabled */
  224. clk_prepare_enable(clk);
  225. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  226. }
  227. CLK_OF_DECLARE(oxnas_plla, "plxtech,nas782x-plla", oxnas_init_plla);
  228. void __init oxnas_init_pllb(struct device_node *np)
  229. {
  230. struct clk *clk;
  231. struct clk_oxnas_pllb *pllb;
  232. pllb = kmalloc(sizeof(*pllb), GFP_KERNEL);
  233. BUG_ON(!pllb);
  234. pllb->hw.init = &clk_pllb_init;
  235. pllb->devnode = np;
  236. pllb->rstc = NULL;
  237. clk = clk_register(NULL, &pllb->hw);
  238. BUG_ON(IS_ERR(clk));
  239. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  240. }
  241. CLK_OF_DECLARE(oxnas_pllb, "plxtech,nas782x-pllb", oxnas_init_pllb);