cp110-system-controller.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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/init.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_MG 5
  59. #define CP110_GATE_MG_CORE 6
  60. #define CP110_GATE_XOR1 7
  61. #define CP110_GATE_XOR0 8
  62. #define CP110_GATE_GOP_DP 9
  63. #define CP110_GATE_PCIE_X1_0 11
  64. #define CP110_GATE_PCIE_X1_1 12
  65. #define CP110_GATE_PCIE_X4 13
  66. #define CP110_GATE_PCIE_XOR 14
  67. #define CP110_GATE_SATA 15
  68. #define CP110_GATE_SATA_USB 16
  69. #define CP110_GATE_MAIN 17
  70. #define CP110_GATE_SDMMC_GOP 18
  71. #define CP110_GATE_SLOW_IO 21
  72. #define CP110_GATE_USB3H0 22
  73. #define CP110_GATE_USB3H1 23
  74. #define CP110_GATE_USB3DEV 24
  75. #define CP110_GATE_EIP150 25
  76. #define CP110_GATE_EIP197 26
  77. struct cp110_gate_clk {
  78. struct clk_hw hw;
  79. struct regmap *regmap;
  80. u8 bit_idx;
  81. };
  82. #define to_cp110_gate_clk(hw) container_of(hw, struct cp110_gate_clk, hw)
  83. static int cp110_gate_enable(struct clk_hw *hw)
  84. {
  85. struct cp110_gate_clk *gate = to_cp110_gate_clk(hw);
  86. regmap_update_bits(gate->regmap, CP110_PM_CLOCK_GATING_REG,
  87. BIT(gate->bit_idx), BIT(gate->bit_idx));
  88. return 0;
  89. }
  90. static void cp110_gate_disable(struct clk_hw *hw)
  91. {
  92. struct cp110_gate_clk *gate = to_cp110_gate_clk(hw);
  93. regmap_update_bits(gate->regmap, CP110_PM_CLOCK_GATING_REG,
  94. BIT(gate->bit_idx), 0);
  95. }
  96. static int cp110_gate_is_enabled(struct clk_hw *hw)
  97. {
  98. struct cp110_gate_clk *gate = to_cp110_gate_clk(hw);
  99. u32 val;
  100. regmap_read(gate->regmap, CP110_PM_CLOCK_GATING_REG, &val);
  101. return val & BIT(gate->bit_idx);
  102. }
  103. static const struct clk_ops cp110_gate_ops = {
  104. .enable = cp110_gate_enable,
  105. .disable = cp110_gate_disable,
  106. .is_enabled = cp110_gate_is_enabled,
  107. };
  108. static struct clk_hw *cp110_register_gate(const char *name,
  109. const char *parent_name,
  110. struct regmap *regmap, u8 bit_idx)
  111. {
  112. struct cp110_gate_clk *gate;
  113. struct clk_hw *hw;
  114. struct clk_init_data init;
  115. int ret;
  116. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  117. if (!gate)
  118. return ERR_PTR(-ENOMEM);
  119. memset(&init, 0, sizeof(init));
  120. init.name = name;
  121. init.ops = &cp110_gate_ops;
  122. init.parent_names = &parent_name;
  123. init.num_parents = 1;
  124. gate->regmap = regmap;
  125. gate->bit_idx = bit_idx;
  126. gate->hw.init = &init;
  127. hw = &gate->hw;
  128. ret = clk_hw_register(NULL, hw);
  129. if (ret) {
  130. kfree(gate);
  131. hw = ERR_PTR(ret);
  132. }
  133. return hw;
  134. }
  135. static void cp110_unregister_gate(struct clk_hw *hw)
  136. {
  137. clk_hw_unregister(hw);
  138. kfree(to_cp110_gate_clk(hw));
  139. }
  140. static struct clk_hw *cp110_of_clk_get(struct of_phandle_args *clkspec,
  141. void *data)
  142. {
  143. struct clk_hw_onecell_data *clk_data = data;
  144. unsigned int type = clkspec->args[0];
  145. unsigned int idx = clkspec->args[1];
  146. if (type == CP110_CLK_TYPE_CORE) {
  147. if (idx > CP110_MAX_CORE_CLOCKS)
  148. return ERR_PTR(-EINVAL);
  149. return clk_data->hws[idx];
  150. } else if (type == CP110_CLK_TYPE_GATABLE) {
  151. if (idx > CP110_MAX_GATABLE_CLOCKS)
  152. return ERR_PTR(-EINVAL);
  153. return clk_data->hws[CP110_MAX_CORE_CLOCKS + idx];
  154. }
  155. return ERR_PTR(-EINVAL);
  156. }
  157. static int cp110_syscon_clk_probe(struct platform_device *pdev)
  158. {
  159. struct regmap *regmap;
  160. struct device_node *np = pdev->dev.of_node;
  161. const char *ppv2_name, *apll_name, *core_name, *eip_name, *nand_name;
  162. struct clk_hw_onecell_data *cp110_clk_data;
  163. struct clk_hw *hw, **cp110_clks;
  164. u32 nand_clk_ctrl;
  165. int i, ret;
  166. regmap = syscon_node_to_regmap(np);
  167. if (IS_ERR(regmap))
  168. return PTR_ERR(regmap);
  169. ret = regmap_read(regmap, CP110_NAND_FLASH_CLK_CTRL_REG,
  170. &nand_clk_ctrl);
  171. if (ret)
  172. return ret;
  173. cp110_clk_data = devm_kzalloc(&pdev->dev, sizeof(*cp110_clk_data) +
  174. sizeof(struct clk_hw *) * CP110_CLK_NUM,
  175. GFP_KERNEL);
  176. if (!cp110_clk_data)
  177. return -ENOMEM;
  178. cp110_clks = cp110_clk_data->hws;
  179. cp110_clk_data->num = CP110_CLK_NUM;
  180. /* Register the APLL which is the root of the hw tree */
  181. of_property_read_string_index(np, "core-clock-output-names",
  182. CP110_CORE_APLL, &apll_name);
  183. hw = clk_hw_register_fixed_rate(NULL, apll_name, NULL, 0,
  184. 1000 * 1000 * 1000);
  185. if (IS_ERR(hw)) {
  186. ret = PTR_ERR(hw);
  187. goto fail0;
  188. }
  189. cp110_clks[CP110_CORE_APLL] = hw;
  190. /* PPv2 is APLL/3 */
  191. of_property_read_string_index(np, "core-clock-output-names",
  192. CP110_CORE_PPV2, &ppv2_name);
  193. hw = clk_hw_register_fixed_factor(NULL, ppv2_name, apll_name, 0, 1, 3);
  194. if (IS_ERR(hw)) {
  195. ret = PTR_ERR(hw);
  196. goto fail1;
  197. }
  198. cp110_clks[CP110_CORE_PPV2] = hw;
  199. /* EIP clock is APLL/2 */
  200. of_property_read_string_index(np, "core-clock-output-names",
  201. CP110_CORE_EIP, &eip_name);
  202. hw = clk_hw_register_fixed_factor(NULL, eip_name, apll_name, 0, 1, 2);
  203. if (IS_ERR(hw)) {
  204. ret = PTR_ERR(hw);
  205. goto fail2;
  206. }
  207. cp110_clks[CP110_CORE_EIP] = hw;
  208. /* Core clock is EIP/2 */
  209. of_property_read_string_index(np, "core-clock-output-names",
  210. CP110_CORE_CORE, &core_name);
  211. hw = clk_hw_register_fixed_factor(NULL, core_name, eip_name, 0, 1, 2);
  212. if (IS_ERR(hw)) {
  213. ret = PTR_ERR(hw);
  214. goto fail3;
  215. }
  216. cp110_clks[CP110_CORE_CORE] = hw;
  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. hw = clk_hw_register_fixed_factor(NULL, nand_name,
  222. apll_name, 0, 2, 5);
  223. else
  224. hw = clk_hw_register_fixed_factor(NULL, nand_name,
  225. core_name, 0, 1, 1);
  226. if (IS_ERR(hw)) {
  227. ret = PTR_ERR(hw);
  228. goto fail4;
  229. }
  230. cp110_clks[CP110_CORE_NAND] = hw;
  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_MG:
  253. of_property_read_string_index(np,
  254. "gate-clock-output-names",
  255. CP110_GATE_MG_CORE, &parent);
  256. break;
  257. case CP110_GATE_NAND:
  258. parent = nand_name;
  259. break;
  260. case CP110_GATE_PPV2:
  261. parent = ppv2_name;
  262. break;
  263. case CP110_GATE_SDIO:
  264. case CP110_GATE_GOP_DP:
  265. of_property_read_string_index(np,
  266. "gate-clock-output-names",
  267. CP110_GATE_SDMMC_GOP, &parent);
  268. break;
  269. case CP110_GATE_XOR1:
  270. case CP110_GATE_XOR0:
  271. case CP110_GATE_PCIE_X1_0:
  272. case CP110_GATE_PCIE_X1_1:
  273. case CP110_GATE_PCIE_X4:
  274. of_property_read_string_index(np,
  275. "gate-clock-output-names",
  276. CP110_GATE_PCIE_XOR, &parent);
  277. break;
  278. case CP110_GATE_SATA:
  279. case CP110_GATE_USB3H0:
  280. case CP110_GATE_USB3H1:
  281. case CP110_GATE_USB3DEV:
  282. of_property_read_string_index(np,
  283. "gate-clock-output-names",
  284. CP110_GATE_SATA_USB, &parent);
  285. break;
  286. default:
  287. parent = core_name;
  288. break;
  289. }
  290. hw = cp110_register_gate(name, parent, regmap, i);
  291. if (IS_ERR(hw)) {
  292. ret = PTR_ERR(hw);
  293. goto fail_gate;
  294. }
  295. cp110_clks[CP110_MAX_CORE_CLOCKS + i] = hw;
  296. }
  297. ret = of_clk_add_hw_provider(np, cp110_of_clk_get, cp110_clk_data);
  298. if (ret)
  299. goto fail_clk_add;
  300. platform_set_drvdata(pdev, cp110_clks);
  301. return 0;
  302. fail_clk_add:
  303. fail_gate:
  304. for (i = 0; i < CP110_MAX_GATABLE_CLOCKS; i++) {
  305. hw = cp110_clks[CP110_MAX_CORE_CLOCKS + i];
  306. if (hw)
  307. cp110_unregister_gate(hw);
  308. }
  309. clk_hw_unregister_fixed_factor(cp110_clks[CP110_CORE_NAND]);
  310. fail4:
  311. clk_hw_unregister_fixed_factor(cp110_clks[CP110_CORE_CORE]);
  312. fail3:
  313. clk_hw_unregister_fixed_factor(cp110_clks[CP110_CORE_EIP]);
  314. fail2:
  315. clk_hw_unregister_fixed_factor(cp110_clks[CP110_CORE_PPV2]);
  316. fail1:
  317. clk_hw_unregister_fixed_rate(cp110_clks[CP110_CORE_APLL]);
  318. fail0:
  319. return ret;
  320. }
  321. static const struct of_device_id cp110_syscon_of_match[] = {
  322. { .compatible = "marvell,cp110-system-controller0", },
  323. { }
  324. };
  325. static struct platform_driver cp110_syscon_driver = {
  326. .probe = cp110_syscon_clk_probe,
  327. .driver = {
  328. .name = "marvell-cp110-system-controller0",
  329. .of_match_table = cp110_syscon_of_match,
  330. .suppress_bind_attrs = true,
  331. },
  332. };
  333. builtin_platform_driver(cp110_syscon_driver);