cp110-system-controller.c 11 KB

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