cp110-system-controller.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Marvell Armada CP110 System Controller
  3. *
  4. * Copyright (C) 2016 Marvell
  5. *
  6. * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. /*
  13. * CP110 has 5 core clocks:
  14. *
  15. * - APLL (1 Ghz)
  16. * - PPv2 core (1/3 APLL)
  17. * - EIP (1/2 APLL)
  18. * - Core (1/2 EIP)
  19. *
  20. * - NAND clock, which is either:
  21. * - Equal to the core clock
  22. * - 2/5 APLL
  23. *
  24. * CP110 has 32 gatable clocks, for the various peripherals in the
  25. * IP. They have fairly complicated parent/child relationships.
  26. */
  27. #define pr_fmt(fmt) "cp110-system-controller: " fmt
  28. #include <linux/clk-provider.h>
  29. #include <linux/mfd/syscon.h>
  30. #include <linux/module.h>
  31. #include <linux/of.h>
  32. #include <linux/of_address.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/regmap.h>
  35. #include <linux/slab.h>
  36. #define CP110_PM_CLOCK_GATING_REG 0x220
  37. #define CP110_NAND_FLASH_CLK_CTRL_REG 0x700
  38. #define NF_CLOCK_SEL_400_MASK BIT(0)
  39. enum {
  40. CP110_CLK_TYPE_CORE,
  41. CP110_CLK_TYPE_GATABLE,
  42. };
  43. #define CP110_MAX_CORE_CLOCKS 5
  44. #define CP110_MAX_GATABLE_CLOCKS 32
  45. #define CP110_CLK_NUM \
  46. (CP110_MAX_CORE_CLOCKS + CP110_MAX_GATABLE_CLOCKS)
  47. #define CP110_CORE_APLL 0
  48. #define CP110_CORE_PPV2 1
  49. #define CP110_CORE_EIP 2
  50. #define CP110_CORE_CORE 3
  51. #define CP110_CORE_NAND 4
  52. /* A number of gatable clocks need special handling */
  53. #define CP110_GATE_AUDIO 0
  54. #define CP110_GATE_COMM_UNIT 1
  55. #define CP110_GATE_NAND 2
  56. #define CP110_GATE_PPV2 3
  57. #define CP110_GATE_SDIO 4
  58. #define CP110_GATE_XOR1 7
  59. #define CP110_GATE_XOR0 8
  60. #define CP110_GATE_PCIE_X1_0 11
  61. #define CP110_GATE_PCIE_X1_1 12
  62. #define CP110_GATE_PCIE_X4 13
  63. #define CP110_GATE_PCIE_XOR 14
  64. #define CP110_GATE_SATA 15
  65. #define CP110_GATE_SATA_USB 16
  66. #define CP110_GATE_MAIN 17
  67. #define CP110_GATE_SDMMC 18
  68. #define CP110_GATE_SLOW_IO 21
  69. #define CP110_GATE_USB3H0 22
  70. #define CP110_GATE_USB3H1 23
  71. #define CP110_GATE_USB3DEV 24
  72. #define CP110_GATE_EIP150 25
  73. #define CP110_GATE_EIP197 26
  74. struct cp110_gate_clk {
  75. struct clk_hw hw;
  76. struct regmap *regmap;
  77. u8 bit_idx;
  78. };
  79. #define to_cp110_gate_clk(clk) container_of(clk, struct cp110_gate_clk, hw)
  80. static int cp110_gate_enable(struct clk_hw *hw)
  81. {
  82. struct cp110_gate_clk *gate = to_cp110_gate_clk(hw);
  83. regmap_update_bits(gate->regmap, CP110_PM_CLOCK_GATING_REG,
  84. BIT(gate->bit_idx), BIT(gate->bit_idx));
  85. return 0;
  86. }
  87. static void cp110_gate_disable(struct clk_hw *hw)
  88. {
  89. struct cp110_gate_clk *gate = to_cp110_gate_clk(hw);
  90. regmap_update_bits(gate->regmap, CP110_PM_CLOCK_GATING_REG,
  91. BIT(gate->bit_idx), 0);
  92. }
  93. static int cp110_gate_is_enabled(struct clk_hw *hw)
  94. {
  95. struct cp110_gate_clk *gate = to_cp110_gate_clk(hw);
  96. u32 val;
  97. regmap_read(gate->regmap, CP110_PM_CLOCK_GATING_REG, &val);
  98. return val & BIT(gate->bit_idx);
  99. }
  100. static const struct clk_ops cp110_gate_ops = {
  101. .enable = cp110_gate_enable,
  102. .disable = cp110_gate_disable,
  103. .is_enabled = cp110_gate_is_enabled,
  104. };
  105. static struct clk *cp110_register_gate(const char *name,
  106. const char *parent_name,
  107. struct regmap *regmap, u8 bit_idx)
  108. {
  109. struct cp110_gate_clk *gate;
  110. struct clk *clk;
  111. struct clk_init_data init;
  112. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  113. if (!gate)
  114. return ERR_PTR(-ENOMEM);
  115. memset(&init, 0, sizeof(init));
  116. init.name = name;
  117. init.ops = &cp110_gate_ops;
  118. init.parent_names = &parent_name;
  119. init.num_parents = 1;
  120. gate->regmap = regmap;
  121. gate->bit_idx = bit_idx;
  122. gate->hw.init = &init;
  123. clk = clk_register(NULL, &gate->hw);
  124. if (IS_ERR(clk))
  125. kfree(gate);
  126. return clk;
  127. }
  128. static void cp110_unregister_gate(struct clk *clk)
  129. {
  130. struct clk_hw *hw;
  131. hw = __clk_get_hw(clk);
  132. if (!hw)
  133. return;
  134. clk_unregister(clk);
  135. kfree(to_cp110_gate_clk(hw));
  136. }
  137. static struct clk *cp110_of_clk_get(struct of_phandle_args *clkspec, void *data)
  138. {
  139. struct clk_onecell_data *clk_data = data;
  140. unsigned int type = clkspec->args[0];
  141. unsigned int idx = clkspec->args[1];
  142. if (type == CP110_CLK_TYPE_CORE) {
  143. if (idx > CP110_MAX_CORE_CLOCKS)
  144. return ERR_PTR(-EINVAL);
  145. return clk_data->clks[idx];
  146. } else if (type == CP110_CLK_TYPE_GATABLE) {
  147. if (idx > CP110_MAX_GATABLE_CLOCKS)
  148. return ERR_PTR(-EINVAL);
  149. return clk_data->clks[CP110_MAX_CORE_CLOCKS + idx];
  150. }
  151. return ERR_PTR(-EINVAL);
  152. }
  153. static int cp110_syscon_clk_probe(struct platform_device *pdev)
  154. {
  155. struct regmap *regmap;
  156. struct device_node *np = pdev->dev.of_node;
  157. const char *ppv2_name, *apll_name, *core_name, *eip_name, *nand_name;
  158. struct clk_onecell_data *cp110_clk_data;
  159. struct clk *clk, **cp110_clks;
  160. u32 nand_clk_ctrl;
  161. int i, ret;
  162. regmap = syscon_node_to_regmap(np);
  163. if (IS_ERR(regmap))
  164. return PTR_ERR(regmap);
  165. ret = regmap_read(regmap, CP110_NAND_FLASH_CLK_CTRL_REG,
  166. &nand_clk_ctrl);
  167. if (ret)
  168. return ret;
  169. cp110_clks = devm_kcalloc(&pdev->dev, sizeof(struct clk *),
  170. CP110_CLK_NUM, GFP_KERNEL);
  171. if (!cp110_clks)
  172. return -ENOMEM;
  173. cp110_clk_data = devm_kzalloc(&pdev->dev,
  174. sizeof(*cp110_clk_data),
  175. GFP_KERNEL);
  176. if (!cp110_clk_data)
  177. return -ENOMEM;
  178. cp110_clk_data->clks = cp110_clks;
  179. cp110_clk_data->clk_num = CP110_CLK_NUM;
  180. /* Register the APLL which is the root of the clk tree */
  181. of_property_read_string_index(np, "core-clock-output-names",
  182. CP110_CORE_APLL, &apll_name);
  183. clk = clk_register_fixed_rate(NULL, apll_name, NULL, 0,
  184. 1000 * 1000 * 1000);
  185. if (IS_ERR(clk)) {
  186. ret = PTR_ERR(clk);
  187. goto fail0;
  188. }
  189. cp110_clks[CP110_CORE_APLL] = clk;
  190. /* PPv2 is APLL/3 */
  191. of_property_read_string_index(np, "core-clock-output-names",
  192. CP110_CORE_PPV2, &ppv2_name);
  193. clk = clk_register_fixed_factor(NULL, ppv2_name, apll_name, 0, 1, 3);
  194. if (IS_ERR(clk)) {
  195. ret = PTR_ERR(clk);
  196. goto fail1;
  197. }
  198. cp110_clks[CP110_CORE_PPV2] = clk;
  199. /* EIP clock is APLL/2 */
  200. of_property_read_string_index(np, "core-clock-output-names",
  201. CP110_CORE_EIP, &eip_name);
  202. clk = clk_register_fixed_factor(NULL, eip_name, apll_name, 0, 1, 2);
  203. if (IS_ERR(clk)) {
  204. ret = PTR_ERR(clk);
  205. goto fail2;
  206. }
  207. cp110_clks[CP110_CORE_EIP] = clk;
  208. /* Core clock is EIP/2 */
  209. of_property_read_string_index(np, "core-clock-output-names",
  210. CP110_CORE_CORE, &core_name);
  211. clk = clk_register_fixed_factor(NULL, core_name, eip_name, 0, 1, 2);
  212. if (IS_ERR(clk)) {
  213. ret = PTR_ERR(clk);
  214. goto fail3;
  215. }
  216. cp110_clks[CP110_CORE_CORE] = clk;
  217. /* NAND can be either APLL/2.5 or core clock */
  218. of_property_read_string_index(np, "core-clock-output-names",
  219. CP110_CORE_NAND, &nand_name);
  220. if (nand_clk_ctrl & NF_CLOCK_SEL_400_MASK)
  221. clk = clk_register_fixed_factor(NULL, nand_name,
  222. apll_name, 0, 2, 5);
  223. else
  224. clk = clk_register_fixed_factor(NULL, nand_name,
  225. core_name, 0, 1, 1);
  226. if (IS_ERR(clk)) {
  227. ret = PTR_ERR(clk);
  228. goto fail4;
  229. }
  230. cp110_clks[CP110_CORE_NAND] = clk;
  231. for (i = 0; i < CP110_MAX_GATABLE_CLOCKS; i++) {
  232. const char *parent, *name;
  233. int ret;
  234. ret = of_property_read_string_index(np,
  235. "gate-clock-output-names",
  236. i, &name);
  237. /* Reached the end of the list? */
  238. if (ret < 0)
  239. break;
  240. if (!strcmp(name, "none"))
  241. continue;
  242. switch (i) {
  243. case CP110_GATE_AUDIO:
  244. case CP110_GATE_COMM_UNIT:
  245. case CP110_GATE_EIP150:
  246. case CP110_GATE_EIP197:
  247. case CP110_GATE_SLOW_IO:
  248. of_property_read_string_index(np,
  249. "gate-clock-output-names",
  250. CP110_GATE_MAIN, &parent);
  251. break;
  252. case CP110_GATE_NAND:
  253. parent = nand_name;
  254. break;
  255. case CP110_GATE_PPV2:
  256. parent = ppv2_name;
  257. break;
  258. case CP110_GATE_SDIO:
  259. of_property_read_string_index(np,
  260. "gate-clock-output-names",
  261. CP110_GATE_SDMMC, &parent);
  262. break;
  263. case CP110_GATE_XOR1:
  264. case CP110_GATE_XOR0:
  265. case CP110_GATE_PCIE_X1_0:
  266. case CP110_GATE_PCIE_X1_1:
  267. case CP110_GATE_PCIE_X4:
  268. of_property_read_string_index(np,
  269. "gate-clock-output-names",
  270. CP110_GATE_PCIE_XOR, &parent);
  271. break;
  272. case CP110_GATE_SATA:
  273. case CP110_GATE_USB3H0:
  274. case CP110_GATE_USB3H1:
  275. case CP110_GATE_USB3DEV:
  276. of_property_read_string_index(np,
  277. "gate-clock-output-names",
  278. CP110_GATE_SATA_USB, &parent);
  279. break;
  280. default:
  281. parent = core_name;
  282. break;
  283. }
  284. clk = cp110_register_gate(name, parent, regmap, i);
  285. if (IS_ERR(clk)) {
  286. ret = PTR_ERR(clk);
  287. goto fail_gate;
  288. }
  289. cp110_clks[CP110_MAX_CORE_CLOCKS + i] = clk;
  290. }
  291. ret = of_clk_add_provider(np, cp110_of_clk_get, cp110_clk_data);
  292. if (ret)
  293. goto fail_clk_add;
  294. platform_set_drvdata(pdev, cp110_clks);
  295. return 0;
  296. fail_clk_add:
  297. fail_gate:
  298. for (i = 0; i < CP110_MAX_GATABLE_CLOCKS; i++) {
  299. clk = cp110_clks[CP110_MAX_CORE_CLOCKS + i];
  300. if (clk)
  301. cp110_unregister_gate(clk);
  302. }
  303. clk_unregister_fixed_factor(cp110_clks[CP110_CORE_NAND]);
  304. fail4:
  305. clk_unregister_fixed_factor(cp110_clks[CP110_CORE_CORE]);
  306. fail3:
  307. clk_unregister_fixed_factor(cp110_clks[CP110_CORE_EIP]);
  308. fail2:
  309. clk_unregister_fixed_factor(cp110_clks[CP110_CORE_PPV2]);
  310. fail1:
  311. clk_unregister_fixed_rate(cp110_clks[CP110_CORE_APLL]);
  312. fail0:
  313. return ret;
  314. }
  315. static int cp110_syscon_clk_remove(struct platform_device *pdev)
  316. {
  317. struct clk **cp110_clks = platform_get_drvdata(pdev);
  318. int i;
  319. of_clk_del_provider(pdev->dev.of_node);
  320. for (i = 0; i < CP110_MAX_GATABLE_CLOCKS; i++) {
  321. struct clk *clk = cp110_clks[CP110_MAX_CORE_CLOCKS + i];
  322. if (clk)
  323. cp110_unregister_gate(clk);
  324. }
  325. clk_unregister_fixed_factor(cp110_clks[CP110_CORE_NAND]);
  326. clk_unregister_fixed_factor(cp110_clks[CP110_CORE_CORE]);
  327. clk_unregister_fixed_factor(cp110_clks[CP110_CORE_EIP]);
  328. clk_unregister_fixed_factor(cp110_clks[CP110_CORE_PPV2]);
  329. clk_unregister_fixed_rate(cp110_clks[CP110_CORE_APLL]);
  330. return 0;
  331. }
  332. static const struct of_device_id cp110_syscon_of_match[] = {
  333. { .compatible = "marvell,cp110-system-controller0", },
  334. { }
  335. };
  336. MODULE_DEVICE_TABLE(of, armada8k_pcie_of_match);
  337. static struct platform_driver cp110_syscon_driver = {
  338. .probe = cp110_syscon_clk_probe,
  339. .remove = cp110_syscon_clk_remove,
  340. .driver = {
  341. .name = "marvell-cp110-system-controller0",
  342. .of_match_table = cp110_syscon_of_match,
  343. },
  344. };
  345. module_platform_driver(cp110_syscon_driver);
  346. MODULE_DESCRIPTION("Marvell CP110 System Controller 0 driver");
  347. MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
  348. MODULE_LICENSE("GPL");