cp110-system-controller.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Marvell Armada CP110 System Controller
  4. *
  5. * Copyright (C) 2016 Marvell
  6. *
  7. * Thomas Petazzoni <[email protected]>
  8. *
  9. */
  10. /*
  11. * CP110 has 6 core clocks:
  12. *
  13. * - PLL0 (1 Ghz)
  14. * - PPv2 core (1/3 PLL0)
  15. * - x2 Core (1/2 PLL0)
  16. * - Core (1/2 x2 Core)
  17. * - SDIO (2/5 PLL0)
  18. *
  19. * - NAND clock, which is either:
  20. * - Equal to SDIO clock
  21. * - 2/5 PLL0
  22. *
  23. * CP110 has 32 gateable clocks, for the various peripherals in the IP.
  24. */
  25. #define pr_fmt(fmt) "cp110-system-controller: " fmt
  26. #include "armada_ap_cp_helper.h"
  27. #include <linux/clk-provider.h>
  28. #include <linux/mfd/syscon.h>
  29. #include <linux/init.h>
  30. #include <linux/of.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/regmap.h>
  33. #include <linux/slab.h>
  34. #define CP110_PM_CLOCK_GATING_REG 0x220
  35. #define CP110_NAND_FLASH_CLK_CTRL_REG 0x700
  36. #define NF_CLOCK_SEL_400_MASK BIT(0)
  37. enum {
  38. CP110_CLK_TYPE_CORE,
  39. CP110_CLK_TYPE_GATABLE,
  40. };
  41. #define CP110_MAX_CORE_CLOCKS 6
  42. #define CP110_MAX_GATABLE_CLOCKS 32
  43. #define CP110_CLK_NUM \
  44. (CP110_MAX_CORE_CLOCKS + CP110_MAX_GATABLE_CLOCKS)
  45. #define CP110_CORE_PLL0 0
  46. #define CP110_CORE_PPV2 1
  47. #define CP110_CORE_X2CORE 2
  48. #define CP110_CORE_CORE 3
  49. #define CP110_CORE_NAND 4
  50. #define CP110_CORE_SDIO 5
  51. /* A number of gateable clocks need special handling */
  52. #define CP110_GATE_AUDIO 0
  53. #define CP110_GATE_COMM_UNIT 1
  54. #define CP110_GATE_NAND 2
  55. #define CP110_GATE_PPV2 3
  56. #define CP110_GATE_SDIO 4
  57. #define CP110_GATE_MG 5
  58. #define CP110_GATE_MG_CORE 6
  59. #define CP110_GATE_XOR1 7
  60. #define CP110_GATE_XOR0 8
  61. #define CP110_GATE_GOP_DP 9
  62. #define CP110_GATE_PCIE_X1_0 11
  63. #define CP110_GATE_PCIE_X1_1 12
  64. #define CP110_GATE_PCIE_X4 13
  65. #define CP110_GATE_PCIE_XOR 14
  66. #define CP110_GATE_SATA 15
  67. #define CP110_GATE_SATA_USB 16
  68. #define CP110_GATE_MAIN 17
  69. #define CP110_GATE_SDMMC_GOP 18
  70. #define CP110_GATE_SLOW_IO 21
  71. #define CP110_GATE_USB3H0 22
  72. #define CP110_GATE_USB3H1 23
  73. #define CP110_GATE_USB3DEV 24
  74. #define CP110_GATE_EIP150 25
  75. #define CP110_GATE_EIP197 26
  76. static const char * const gate_base_names[] = {
  77. [CP110_GATE_AUDIO] = "audio",
  78. [CP110_GATE_COMM_UNIT] = "communit",
  79. [CP110_GATE_NAND] = "nand",
  80. [CP110_GATE_PPV2] = "ppv2",
  81. [CP110_GATE_SDIO] = "sdio",
  82. [CP110_GATE_MG] = "mg-domain",
  83. [CP110_GATE_MG_CORE] = "mg-core",
  84. [CP110_GATE_XOR1] = "xor1",
  85. [CP110_GATE_XOR0] = "xor0",
  86. [CP110_GATE_GOP_DP] = "gop-dp",
  87. [CP110_GATE_PCIE_X1_0] = "pcie_x10",
  88. [CP110_GATE_PCIE_X1_1] = "pcie_x11",
  89. [CP110_GATE_PCIE_X4] = "pcie_x4",
  90. [CP110_GATE_PCIE_XOR] = "pcie-xor",
  91. [CP110_GATE_SATA] = "sata",
  92. [CP110_GATE_SATA_USB] = "sata-usb",
  93. [CP110_GATE_MAIN] = "main",
  94. [CP110_GATE_SDMMC_GOP] = "sd-mmc-gop",
  95. [CP110_GATE_SLOW_IO] = "slow-io",
  96. [CP110_GATE_USB3H0] = "usb3h0",
  97. [CP110_GATE_USB3H1] = "usb3h1",
  98. [CP110_GATE_USB3DEV] = "usb3dev",
  99. [CP110_GATE_EIP150] = "eip150",
  100. [CP110_GATE_EIP197] = "eip197"
  101. };
  102. struct cp110_gate_clk {
  103. struct clk_hw hw;
  104. struct regmap *regmap;
  105. u8 bit_idx;
  106. };
  107. #define to_cp110_gate_clk(hw) container_of(hw, struct cp110_gate_clk, hw)
  108. static int cp110_gate_enable(struct clk_hw *hw)
  109. {
  110. struct cp110_gate_clk *gate = to_cp110_gate_clk(hw);
  111. regmap_update_bits(gate->regmap, CP110_PM_CLOCK_GATING_REG,
  112. BIT(gate->bit_idx), BIT(gate->bit_idx));
  113. return 0;
  114. }
  115. static void cp110_gate_disable(struct clk_hw *hw)
  116. {
  117. struct cp110_gate_clk *gate = to_cp110_gate_clk(hw);
  118. regmap_update_bits(gate->regmap, CP110_PM_CLOCK_GATING_REG,
  119. BIT(gate->bit_idx), 0);
  120. }
  121. static int cp110_gate_is_enabled(struct clk_hw *hw)
  122. {
  123. struct cp110_gate_clk *gate = to_cp110_gate_clk(hw);
  124. u32 val;
  125. regmap_read(gate->regmap, CP110_PM_CLOCK_GATING_REG, &val);
  126. return val & BIT(gate->bit_idx);
  127. }
  128. static const struct clk_ops cp110_gate_ops = {
  129. .enable = cp110_gate_enable,
  130. .disable = cp110_gate_disable,
  131. .is_enabled = cp110_gate_is_enabled,
  132. };
  133. static struct clk_hw *cp110_register_gate(const char *name,
  134. const char *parent_name,
  135. struct regmap *regmap, u8 bit_idx)
  136. {
  137. struct cp110_gate_clk *gate;
  138. struct clk_hw *hw;
  139. struct clk_init_data init;
  140. int ret;
  141. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  142. if (!gate)
  143. return ERR_PTR(-ENOMEM);
  144. memset(&init, 0, sizeof(init));
  145. init.name = name;
  146. init.ops = &cp110_gate_ops;
  147. init.parent_names = &parent_name;
  148. init.num_parents = 1;
  149. gate->regmap = regmap;
  150. gate->bit_idx = bit_idx;
  151. gate->hw.init = &init;
  152. hw = &gate->hw;
  153. ret = clk_hw_register(NULL, hw);
  154. if (ret) {
  155. kfree(gate);
  156. hw = ERR_PTR(ret);
  157. }
  158. return hw;
  159. }
  160. static void cp110_unregister_gate(struct clk_hw *hw)
  161. {
  162. clk_hw_unregister(hw);
  163. kfree(to_cp110_gate_clk(hw));
  164. }
  165. static struct clk_hw *cp110_of_clk_get(struct of_phandle_args *clkspec,
  166. void *data)
  167. {
  168. struct clk_hw_onecell_data *clk_data = data;
  169. unsigned int type = clkspec->args[0];
  170. unsigned int idx = clkspec->args[1];
  171. if (type == CP110_CLK_TYPE_CORE) {
  172. if (idx >= CP110_MAX_CORE_CLOCKS)
  173. return ERR_PTR(-EINVAL);
  174. return clk_data->hws[idx];
  175. } else if (type == CP110_CLK_TYPE_GATABLE) {
  176. if (idx >= CP110_MAX_GATABLE_CLOCKS)
  177. return ERR_PTR(-EINVAL);
  178. return clk_data->hws[CP110_MAX_CORE_CLOCKS + idx];
  179. }
  180. return ERR_PTR(-EINVAL);
  181. }
  182. static int cp110_syscon_common_probe(struct platform_device *pdev,
  183. struct device_node *syscon_node)
  184. {
  185. struct regmap *regmap;
  186. struct device *dev = &pdev->dev;
  187. struct device_node *np = dev->of_node;
  188. const char *ppv2_name, *pll0_name, *core_name, *x2core_name, *nand_name,
  189. *sdio_name;
  190. struct clk_hw_onecell_data *cp110_clk_data;
  191. struct clk_hw *hw, **cp110_clks;
  192. u32 nand_clk_ctrl;
  193. int i, ret;
  194. char *gate_name[ARRAY_SIZE(gate_base_names)];
  195. regmap = syscon_node_to_regmap(syscon_node);
  196. if (IS_ERR(regmap))
  197. return PTR_ERR(regmap);
  198. ret = regmap_read(regmap, CP110_NAND_FLASH_CLK_CTRL_REG,
  199. &nand_clk_ctrl);
  200. if (ret)
  201. return ret;
  202. cp110_clk_data = devm_kzalloc(dev, struct_size(cp110_clk_data, hws,
  203. CP110_CLK_NUM),
  204. GFP_KERNEL);
  205. if (!cp110_clk_data)
  206. return -ENOMEM;
  207. cp110_clks = cp110_clk_data->hws;
  208. cp110_clk_data->num = CP110_CLK_NUM;
  209. /* Register the PLL0 which is the root of the hw tree */
  210. pll0_name = ap_cp_unique_name(dev, syscon_node, "pll0");
  211. hw = clk_hw_register_fixed_rate(NULL, pll0_name, NULL, 0,
  212. 1000 * 1000 * 1000);
  213. if (IS_ERR(hw)) {
  214. ret = PTR_ERR(hw);
  215. goto fail_pll0;
  216. }
  217. cp110_clks[CP110_CORE_PLL0] = hw;
  218. /* PPv2 is PLL0/3 */
  219. ppv2_name = ap_cp_unique_name(dev, syscon_node, "ppv2-core");
  220. hw = clk_hw_register_fixed_factor(NULL, ppv2_name, pll0_name, 0, 1, 3);
  221. if (IS_ERR(hw)) {
  222. ret = PTR_ERR(hw);
  223. goto fail_ppv2;
  224. }
  225. cp110_clks[CP110_CORE_PPV2] = hw;
  226. /* X2CORE clock is PLL0/2 */
  227. x2core_name = ap_cp_unique_name(dev, syscon_node, "x2core");
  228. hw = clk_hw_register_fixed_factor(NULL, x2core_name, pll0_name,
  229. 0, 1, 2);
  230. if (IS_ERR(hw)) {
  231. ret = PTR_ERR(hw);
  232. goto fail_eip;
  233. }
  234. cp110_clks[CP110_CORE_X2CORE] = hw;
  235. /* Core clock is X2CORE/2 */
  236. core_name = ap_cp_unique_name(dev, syscon_node, "core");
  237. hw = clk_hw_register_fixed_factor(NULL, core_name, x2core_name,
  238. 0, 1, 2);
  239. if (IS_ERR(hw)) {
  240. ret = PTR_ERR(hw);
  241. goto fail_core;
  242. }
  243. cp110_clks[CP110_CORE_CORE] = hw;
  244. /* NAND can be either PLL0/2.5 or core clock */
  245. nand_name = ap_cp_unique_name(dev, syscon_node, "nand-core");
  246. if (nand_clk_ctrl & NF_CLOCK_SEL_400_MASK)
  247. hw = clk_hw_register_fixed_factor(NULL, nand_name,
  248. pll0_name, 0, 2, 5);
  249. else
  250. hw = clk_hw_register_fixed_factor(NULL, nand_name,
  251. core_name, 0, 1, 1);
  252. if (IS_ERR(hw)) {
  253. ret = PTR_ERR(hw);
  254. goto fail_nand;
  255. }
  256. cp110_clks[CP110_CORE_NAND] = hw;
  257. /* SDIO clock is PLL0/2.5 */
  258. sdio_name = ap_cp_unique_name(dev, syscon_node, "sdio-core");
  259. hw = clk_hw_register_fixed_factor(NULL, sdio_name,
  260. pll0_name, 0, 2, 5);
  261. if (IS_ERR(hw)) {
  262. ret = PTR_ERR(hw);
  263. goto fail_sdio;
  264. }
  265. cp110_clks[CP110_CORE_SDIO] = hw;
  266. /* create the unique name for all the gate clocks */
  267. for (i = 0; i < ARRAY_SIZE(gate_base_names); i++)
  268. gate_name[i] = ap_cp_unique_name(dev, syscon_node,
  269. gate_base_names[i]);
  270. for (i = 0; i < ARRAY_SIZE(gate_base_names); i++) {
  271. const char *parent;
  272. if (gate_name[i] == NULL)
  273. continue;
  274. switch (i) {
  275. case CP110_GATE_NAND:
  276. parent = nand_name;
  277. break;
  278. case CP110_GATE_MG:
  279. case CP110_GATE_GOP_DP:
  280. case CP110_GATE_PPV2:
  281. parent = ppv2_name;
  282. break;
  283. case CP110_GATE_SDIO:
  284. parent = sdio_name;
  285. break;
  286. case CP110_GATE_MAIN:
  287. case CP110_GATE_PCIE_XOR:
  288. case CP110_GATE_PCIE_X4:
  289. case CP110_GATE_EIP150:
  290. case CP110_GATE_EIP197:
  291. parent = x2core_name;
  292. break;
  293. default:
  294. parent = core_name;
  295. break;
  296. }
  297. hw = cp110_register_gate(gate_name[i], parent, regmap, i);
  298. if (IS_ERR(hw)) {
  299. ret = PTR_ERR(hw);
  300. goto fail_gate;
  301. }
  302. cp110_clks[CP110_MAX_CORE_CLOCKS + i] = hw;
  303. }
  304. ret = of_clk_add_hw_provider(np, cp110_of_clk_get, cp110_clk_data);
  305. if (ret)
  306. goto fail_clk_add;
  307. platform_set_drvdata(pdev, cp110_clks);
  308. return 0;
  309. fail_clk_add:
  310. fail_gate:
  311. for (i = 0; i < CP110_MAX_GATABLE_CLOCKS; i++) {
  312. hw = cp110_clks[CP110_MAX_CORE_CLOCKS + i];
  313. if (hw)
  314. cp110_unregister_gate(hw);
  315. }
  316. clk_hw_unregister_fixed_factor(cp110_clks[CP110_CORE_SDIO]);
  317. fail_sdio:
  318. clk_hw_unregister_fixed_factor(cp110_clks[CP110_CORE_NAND]);
  319. fail_nand:
  320. clk_hw_unregister_fixed_factor(cp110_clks[CP110_CORE_CORE]);
  321. fail_core:
  322. clk_hw_unregister_fixed_factor(cp110_clks[CP110_CORE_X2CORE]);
  323. fail_eip:
  324. clk_hw_unregister_fixed_factor(cp110_clks[CP110_CORE_PPV2]);
  325. fail_ppv2:
  326. clk_hw_unregister_fixed_rate(cp110_clks[CP110_CORE_PLL0]);
  327. fail_pll0:
  328. return ret;
  329. }
  330. static int cp110_syscon_legacy_clk_probe(struct platform_device *pdev)
  331. {
  332. dev_warn(&pdev->dev, FW_WARN "Using legacy device tree binding\n");
  333. dev_warn(&pdev->dev, FW_WARN "Update your device tree:\n");
  334. dev_warn(&pdev->dev, FW_WARN
  335. "This binding won't be supported in future kernels\n");
  336. return cp110_syscon_common_probe(pdev, pdev->dev.of_node);
  337. }
  338. static int cp110_clk_probe(struct platform_device *pdev)
  339. {
  340. return cp110_syscon_common_probe(pdev, pdev->dev.of_node->parent);
  341. }
  342. static const struct of_device_id cp110_syscon_legacy_of_match[] = {
  343. { .compatible = "marvell,cp110-system-controller0", },
  344. { }
  345. };
  346. static struct platform_driver cp110_syscon_legacy_driver = {
  347. .probe = cp110_syscon_legacy_clk_probe,
  348. .driver = {
  349. .name = "marvell-cp110-system-controller0",
  350. .of_match_table = cp110_syscon_legacy_of_match,
  351. .suppress_bind_attrs = true,
  352. },
  353. };
  354. builtin_platform_driver(cp110_syscon_legacy_driver);
  355. static const struct of_device_id cp110_clock_of_match[] = {
  356. { .compatible = "marvell,cp110-clock", },
  357. { }
  358. };
  359. static struct platform_driver cp110_clock_driver = {
  360. .probe = cp110_clk_probe,
  361. .driver = {
  362. .name = "marvell-cp110-clock",
  363. .of_match_table = cp110_clock_of_match,
  364. .suppress_bind_attrs = true,
  365. },
  366. };
  367. builtin_platform_driver(cp110_clock_driver);