kirkwood.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Marvell Kirkwood SoC clocks
  3. *
  4. * Copyright (C) 2012 Marvell
  5. *
  6. * Gregory CLEMENT <gregory.clement@free-electrons.com>
  7. * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
  8. * Andrew Lunn <andrew@lunn.ch>
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/slab.h>
  16. #include <linux/clk-provider.h>
  17. #include <linux/io.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include "common.h"
  21. /*
  22. * Core Clocks
  23. *
  24. * Kirkwood PLL sample-at-reset configuration
  25. * (6180 has different SAR layout than other Kirkwood SoCs)
  26. *
  27. * SAR0[4:3,22,1] : CPU frequency (6281,6292,6282)
  28. * 4 = 600 MHz
  29. * 6 = 800 MHz
  30. * 7 = 1000 MHz
  31. * 9 = 1200 MHz
  32. * 12 = 1500 MHz
  33. * 13 = 1600 MHz
  34. * 14 = 1800 MHz
  35. * 15 = 2000 MHz
  36. * others reserved.
  37. *
  38. * SAR0[19,10:9] : CPU to L2 Clock divider ratio (6281,6292,6282)
  39. * 1 = (1/2) * CPU
  40. * 3 = (1/3) * CPU
  41. * 5 = (1/4) * CPU
  42. * others reserved.
  43. *
  44. * SAR0[8:5] : CPU to DDR DRAM Clock divider ratio (6281,6292,6282)
  45. * 2 = (1/2) * CPU
  46. * 4 = (1/3) * CPU
  47. * 6 = (1/4) * CPU
  48. * 7 = (2/9) * CPU
  49. * 8 = (1/5) * CPU
  50. * 9 = (1/6) * CPU
  51. * others reserved.
  52. *
  53. * SAR0[4:2] : Kirkwood 6180 cpu/l2/ddr clock configuration (6180 only)
  54. * 5 = [CPU = 600 MHz, L2 = (1/2) * CPU, DDR = 200 MHz = (1/3) * CPU]
  55. * 6 = [CPU = 800 MHz, L2 = (1/2) * CPU, DDR = 200 MHz = (1/4) * CPU]
  56. * 7 = [CPU = 1000 MHz, L2 = (1/2) * CPU, DDR = 200 MHz = (1/5) * CPU]
  57. * others reserved.
  58. *
  59. * SAR0[21] : TCLK frequency
  60. * 0 = 200 MHz
  61. * 1 = 166 MHz
  62. * others reserved.
  63. */
  64. #define SAR_KIRKWOOD_CPU_FREQ(x) \
  65. (((x & (1 << 1)) >> 1) | \
  66. ((x & (1 << 22)) >> 21) | \
  67. ((x & (3 << 3)) >> 1))
  68. #define SAR_KIRKWOOD_L2_RATIO(x) \
  69. (((x & (3 << 9)) >> 9) | \
  70. (((x & (1 << 19)) >> 17)))
  71. #define SAR_KIRKWOOD_DDR_RATIO 5
  72. #define SAR_KIRKWOOD_DDR_RATIO_MASK 0xf
  73. #define SAR_MV88F6180_CLK 2
  74. #define SAR_MV88F6180_CLK_MASK 0x7
  75. #define SAR_KIRKWOOD_TCLK_FREQ 21
  76. #define SAR_KIRKWOOD_TCLK_FREQ_MASK 0x1
  77. enum { KIRKWOOD_CPU_TO_L2, KIRKWOOD_CPU_TO_DDR };
  78. static const struct coreclk_ratio kirkwood_coreclk_ratios[] __initconst = {
  79. { .id = KIRKWOOD_CPU_TO_L2, .name = "l2clk", },
  80. { .id = KIRKWOOD_CPU_TO_DDR, .name = "ddrclk", }
  81. };
  82. static u32 __init kirkwood_get_tclk_freq(void __iomem *sar)
  83. {
  84. u32 opt = (readl(sar) >> SAR_KIRKWOOD_TCLK_FREQ) &
  85. SAR_KIRKWOOD_TCLK_FREQ_MASK;
  86. return (opt) ? 166666667 : 200000000;
  87. }
  88. static const u32 kirkwood_cpu_freqs[] __initconst = {
  89. 0, 0, 0, 0,
  90. 600000000,
  91. 0,
  92. 800000000,
  93. 1000000000,
  94. 0,
  95. 1200000000,
  96. 0, 0,
  97. 1500000000,
  98. 1600000000,
  99. 1800000000,
  100. 2000000000
  101. };
  102. static u32 __init kirkwood_get_cpu_freq(void __iomem *sar)
  103. {
  104. u32 opt = SAR_KIRKWOOD_CPU_FREQ(readl(sar));
  105. return kirkwood_cpu_freqs[opt];
  106. }
  107. static const int kirkwood_cpu_l2_ratios[8][2] __initconst = {
  108. { 0, 1 }, { 1, 2 }, { 0, 1 }, { 1, 3 },
  109. { 0, 1 }, { 1, 4 }, { 0, 1 }, { 0, 1 }
  110. };
  111. static const int kirkwood_cpu_ddr_ratios[16][2] __initconst = {
  112. { 0, 1 }, { 0, 1 }, { 1, 2 }, { 0, 1 },
  113. { 1, 3 }, { 0, 1 }, { 1, 4 }, { 2, 9 },
  114. { 1, 5 }, { 1, 6 }, { 0, 1 }, { 0, 1 },
  115. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }
  116. };
  117. static void __init kirkwood_get_clk_ratio(
  118. void __iomem *sar, int id, int *mult, int *div)
  119. {
  120. switch (id) {
  121. case KIRKWOOD_CPU_TO_L2:
  122. {
  123. u32 opt = SAR_KIRKWOOD_L2_RATIO(readl(sar));
  124. *mult = kirkwood_cpu_l2_ratios[opt][0];
  125. *div = kirkwood_cpu_l2_ratios[opt][1];
  126. break;
  127. }
  128. case KIRKWOOD_CPU_TO_DDR:
  129. {
  130. u32 opt = (readl(sar) >> SAR_KIRKWOOD_DDR_RATIO) &
  131. SAR_KIRKWOOD_DDR_RATIO_MASK;
  132. *mult = kirkwood_cpu_ddr_ratios[opt][0];
  133. *div = kirkwood_cpu_ddr_ratios[opt][1];
  134. break;
  135. }
  136. }
  137. }
  138. static const u32 mv88f6180_cpu_freqs[] __initconst = {
  139. 0, 0, 0, 0, 0,
  140. 600000000,
  141. 800000000,
  142. 1000000000
  143. };
  144. static u32 __init mv88f6180_get_cpu_freq(void __iomem *sar)
  145. {
  146. u32 opt = (readl(sar) >> SAR_MV88F6180_CLK) & SAR_MV88F6180_CLK_MASK;
  147. return mv88f6180_cpu_freqs[opt];
  148. }
  149. static const int mv88f6180_cpu_ddr_ratios[8][2] __initconst = {
  150. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  151. { 0, 1 }, { 1, 3 }, { 1, 4 }, { 1, 5 }
  152. };
  153. static void __init mv88f6180_get_clk_ratio(
  154. void __iomem *sar, int id, int *mult, int *div)
  155. {
  156. switch (id) {
  157. case KIRKWOOD_CPU_TO_L2:
  158. {
  159. /* mv88f6180 has a fixed 1:2 CPU-to-L2 ratio */
  160. *mult = 1;
  161. *div = 2;
  162. break;
  163. }
  164. case KIRKWOOD_CPU_TO_DDR:
  165. {
  166. u32 opt = (readl(sar) >> SAR_MV88F6180_CLK) &
  167. SAR_MV88F6180_CLK_MASK;
  168. *mult = mv88f6180_cpu_ddr_ratios[opt][0];
  169. *div = mv88f6180_cpu_ddr_ratios[opt][1];
  170. break;
  171. }
  172. }
  173. }
  174. static const struct coreclk_soc_desc kirkwood_coreclks = {
  175. .get_tclk_freq = kirkwood_get_tclk_freq,
  176. .get_cpu_freq = kirkwood_get_cpu_freq,
  177. .get_clk_ratio = kirkwood_get_clk_ratio,
  178. .ratios = kirkwood_coreclk_ratios,
  179. .num_ratios = ARRAY_SIZE(kirkwood_coreclk_ratios),
  180. };
  181. static const struct coreclk_soc_desc mv88f6180_coreclks = {
  182. .get_tclk_freq = kirkwood_get_tclk_freq,
  183. .get_cpu_freq = mv88f6180_get_cpu_freq,
  184. .get_clk_ratio = mv88f6180_get_clk_ratio,
  185. .ratios = kirkwood_coreclk_ratios,
  186. .num_ratios = ARRAY_SIZE(kirkwood_coreclk_ratios),
  187. };
  188. /*
  189. * Clock Gating Control
  190. */
  191. static const struct clk_gating_soc_desc kirkwood_gating_desc[] __initconst = {
  192. { "ge0", NULL, 0, 0 },
  193. { "pex0", NULL, 2, 0 },
  194. { "usb0", NULL, 3, 0 },
  195. { "sdio", NULL, 4, 0 },
  196. { "tsu", NULL, 5, 0 },
  197. { "runit", NULL, 7, 0 },
  198. { "xor0", NULL, 8, 0 },
  199. { "audio", NULL, 9, 0 },
  200. { "sata0", NULL, 14, 0 },
  201. { "sata1", NULL, 15, 0 },
  202. { "xor1", NULL, 16, 0 },
  203. { "crypto", NULL, 17, 0 },
  204. { "pex1", NULL, 18, 0 },
  205. { "ge1", NULL, 19, 0 },
  206. { "tdm", NULL, 20, 0 },
  207. { }
  208. };
  209. /*
  210. * Clock Muxing Control
  211. */
  212. struct clk_muxing_soc_desc {
  213. const char *name;
  214. const char **parents;
  215. int num_parents;
  216. int shift;
  217. int width;
  218. unsigned long flags;
  219. };
  220. struct clk_muxing_ctrl {
  221. spinlock_t *lock;
  222. struct clk **muxes;
  223. int num_muxes;
  224. };
  225. static const char *powersave_parents[] = {
  226. "cpuclk",
  227. "ddrclk",
  228. };
  229. static const struct clk_muxing_soc_desc kirkwood_mux_desc[] __initconst = {
  230. { "powersave", powersave_parents, ARRAY_SIZE(powersave_parents),
  231. 11, 1, 0 },
  232. };
  233. #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw)
  234. static struct clk *clk_muxing_get_src(
  235. struct of_phandle_args *clkspec, void *data)
  236. {
  237. struct clk_muxing_ctrl *ctrl = (struct clk_muxing_ctrl *)data;
  238. int n;
  239. if (clkspec->args_count < 1)
  240. return ERR_PTR(-EINVAL);
  241. for (n = 0; n < ctrl->num_muxes; n++) {
  242. struct clk_mux *mux =
  243. to_clk_mux(__clk_get_hw(ctrl->muxes[n]));
  244. if (clkspec->args[0] == mux->shift)
  245. return ctrl->muxes[n];
  246. }
  247. return ERR_PTR(-ENODEV);
  248. }
  249. static void __init kirkwood_clk_muxing_setup(struct device_node *np,
  250. const struct clk_muxing_soc_desc *desc)
  251. {
  252. struct clk_muxing_ctrl *ctrl;
  253. void __iomem *base;
  254. int n;
  255. base = of_iomap(np, 0);
  256. if (WARN_ON(!base))
  257. return;
  258. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  259. if (WARN_ON(!ctrl))
  260. goto ctrl_out;
  261. /* lock must already be initialized */
  262. ctrl->lock = &ctrl_gating_lock;
  263. /* Count, allocate, and register clock muxes */
  264. for (n = 0; desc[n].name;)
  265. n++;
  266. ctrl->num_muxes = n;
  267. ctrl->muxes = kcalloc(ctrl->num_muxes, sizeof(struct clk *),
  268. GFP_KERNEL);
  269. if (WARN_ON(!ctrl->muxes))
  270. goto muxes_out;
  271. for (n = 0; n < ctrl->num_muxes; n++) {
  272. ctrl->muxes[n] = clk_register_mux(NULL, desc[n].name,
  273. desc[n].parents, desc[n].num_parents,
  274. desc[n].flags, base, desc[n].shift,
  275. desc[n].width, desc[n].flags, ctrl->lock);
  276. WARN_ON(IS_ERR(ctrl->muxes[n]));
  277. }
  278. of_clk_add_provider(np, clk_muxing_get_src, ctrl);
  279. return;
  280. muxes_out:
  281. kfree(ctrl);
  282. ctrl_out:
  283. iounmap(base);
  284. }
  285. static void __init kirkwood_clk_init(struct device_node *np)
  286. {
  287. struct device_node *cgnp =
  288. of_find_compatible_node(NULL, NULL, "marvell,kirkwood-gating-clock");
  289. if (of_device_is_compatible(np, "marvell,mv88f6180-core-clock"))
  290. mvebu_coreclk_setup(np, &mv88f6180_coreclks);
  291. else
  292. mvebu_coreclk_setup(np, &kirkwood_coreclks);
  293. if (cgnp) {
  294. mvebu_clk_gating_setup(cgnp, kirkwood_gating_desc);
  295. kirkwood_clk_muxing_setup(cgnp, kirkwood_mux_desc);
  296. }
  297. }
  298. CLK_OF_DECLARE(kirkwood_clk, "marvell,kirkwood-core-clock",
  299. kirkwood_clk_init);
  300. CLK_OF_DECLARE(mv88f6180_clk, "marvell,mv88f6180-core-clock",
  301. kirkwood_clk_init);