bg2q.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (c) 2014 Marvell Technology Group Ltd.
  3. *
  4. * Alexandre Belloni <alexandre.belloni@free-electrons.com>
  5. * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/clk-provider.h>
  21. #include <linux/kernel.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #include <linux/slab.h>
  25. #include <dt-bindings/clock/berlin2q.h>
  26. #include "berlin2-div.h"
  27. #include "berlin2-pll.h"
  28. #include "common.h"
  29. #define REG_PINMUX0 0x0018
  30. #define REG_PINMUX5 0x002c
  31. #define REG_SYSPLLCTL0 0x0030
  32. #define REG_SYSPLLCTL4 0x0040
  33. #define REG_CLKENABLE 0x00e8
  34. #define REG_CLKSELECT0 0x00ec
  35. #define REG_CLKSELECT1 0x00f0
  36. #define REG_CLKSELECT2 0x00f4
  37. #define REG_CLKSWITCH0 0x00f8
  38. #define REG_CLKSWITCH1 0x00fc
  39. #define REG_SW_GENERIC0 0x0110
  40. #define REG_SW_GENERIC3 0x011c
  41. #define REG_SDIO0XIN_CLKCTL 0x0158
  42. #define REG_SDIO1XIN_CLKCTL 0x015c
  43. #define MAX_CLKS 28
  44. static struct clk_hw_onecell_data *clk_data;
  45. static DEFINE_SPINLOCK(lock);
  46. static void __iomem *gbase;
  47. static void __iomem *cpupll_base;
  48. enum {
  49. REFCLK,
  50. SYSPLL, CPUPLL,
  51. AVPLL_B1, AVPLL_B2, AVPLL_B3, AVPLL_B4,
  52. AVPLL_B5, AVPLL_B6, AVPLL_B7, AVPLL_B8,
  53. };
  54. static const char *clk_names[] = {
  55. [REFCLK] = "refclk",
  56. [SYSPLL] = "syspll",
  57. [CPUPLL] = "cpupll",
  58. [AVPLL_B1] = "avpll_b1",
  59. [AVPLL_B2] = "avpll_b2",
  60. [AVPLL_B3] = "avpll_b3",
  61. [AVPLL_B4] = "avpll_b4",
  62. [AVPLL_B5] = "avpll_b5",
  63. [AVPLL_B6] = "avpll_b6",
  64. [AVPLL_B7] = "avpll_b7",
  65. [AVPLL_B8] = "avpll_b8",
  66. };
  67. static const struct berlin2_pll_map bg2q_pll_map __initconst = {
  68. .vcodiv = {1, 0, 2, 0, 3, 4, 0, 6, 8},
  69. .mult = 1,
  70. .fbdiv_shift = 7,
  71. .rfdiv_shift = 2,
  72. .divsel_shift = 9,
  73. };
  74. static const u8 default_parent_ids[] = {
  75. SYSPLL, AVPLL_B4, AVPLL_B5, AVPLL_B6, AVPLL_B7, SYSPLL
  76. };
  77. static const struct berlin2_div_data bg2q_divs[] __initconst = {
  78. {
  79. .name = "sys",
  80. .parent_ids = default_parent_ids,
  81. .num_parents = ARRAY_SIZE(default_parent_ids),
  82. .map = {
  83. BERLIN2_DIV_GATE(REG_CLKENABLE, 0),
  84. BERLIN2_PLL_SELECT(REG_CLKSELECT0, 0),
  85. BERLIN2_DIV_SELECT(REG_CLKSELECT0, 3),
  86. BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 3),
  87. BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 4),
  88. BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 5),
  89. },
  90. .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
  91. .flags = CLK_IGNORE_UNUSED,
  92. },
  93. {
  94. .name = "drmfigo",
  95. .parent_ids = default_parent_ids,
  96. .num_parents = ARRAY_SIZE(default_parent_ids),
  97. .map = {
  98. BERLIN2_DIV_GATE(REG_CLKENABLE, 17),
  99. BERLIN2_PLL_SELECT(REG_CLKSELECT0, 6),
  100. BERLIN2_DIV_SELECT(REG_CLKSELECT0, 9),
  101. BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 6),
  102. BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 7),
  103. BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 8),
  104. },
  105. .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
  106. .flags = 0,
  107. },
  108. {
  109. .name = "cfg",
  110. .parent_ids = default_parent_ids,
  111. .num_parents = ARRAY_SIZE(default_parent_ids),
  112. .map = {
  113. BERLIN2_DIV_GATE(REG_CLKENABLE, 1),
  114. BERLIN2_PLL_SELECT(REG_CLKSELECT0, 12),
  115. BERLIN2_DIV_SELECT(REG_CLKSELECT0, 15),
  116. BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 9),
  117. BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 10),
  118. BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 11),
  119. },
  120. .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
  121. .flags = 0,
  122. },
  123. {
  124. .name = "gfx2d",
  125. .parent_ids = default_parent_ids,
  126. .num_parents = ARRAY_SIZE(default_parent_ids),
  127. .map = {
  128. BERLIN2_DIV_GATE(REG_CLKENABLE, 4),
  129. BERLIN2_PLL_SELECT(REG_CLKSELECT0, 18),
  130. BERLIN2_DIV_SELECT(REG_CLKSELECT0, 21),
  131. BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 12),
  132. BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 13),
  133. BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 14),
  134. },
  135. .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
  136. .flags = 0,
  137. },
  138. {
  139. .name = "zsp",
  140. .parent_ids = default_parent_ids,
  141. .num_parents = ARRAY_SIZE(default_parent_ids),
  142. .map = {
  143. BERLIN2_DIV_GATE(REG_CLKENABLE, 6),
  144. BERLIN2_PLL_SELECT(REG_CLKSELECT0, 24),
  145. BERLIN2_DIV_SELECT(REG_CLKSELECT0, 27),
  146. BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 15),
  147. BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 16),
  148. BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 17),
  149. },
  150. .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
  151. .flags = 0,
  152. },
  153. {
  154. .name = "perif",
  155. .parent_ids = default_parent_ids,
  156. .num_parents = ARRAY_SIZE(default_parent_ids),
  157. .map = {
  158. BERLIN2_DIV_GATE(REG_CLKENABLE, 7),
  159. BERLIN2_PLL_SELECT(REG_CLKSELECT1, 0),
  160. BERLIN2_DIV_SELECT(REG_CLKSELECT1, 3),
  161. BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 18),
  162. BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 19),
  163. BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 20),
  164. },
  165. .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
  166. .flags = CLK_IGNORE_UNUSED,
  167. },
  168. {
  169. .name = "pcube",
  170. .parent_ids = default_parent_ids,
  171. .num_parents = ARRAY_SIZE(default_parent_ids),
  172. .map = {
  173. BERLIN2_DIV_GATE(REG_CLKENABLE, 2),
  174. BERLIN2_PLL_SELECT(REG_CLKSELECT1, 6),
  175. BERLIN2_DIV_SELECT(REG_CLKSELECT1, 9),
  176. BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 21),
  177. BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 22),
  178. BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 23),
  179. },
  180. .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
  181. .flags = 0,
  182. },
  183. {
  184. .name = "vscope",
  185. .parent_ids = default_parent_ids,
  186. .num_parents = ARRAY_SIZE(default_parent_ids),
  187. .map = {
  188. BERLIN2_DIV_GATE(REG_CLKENABLE, 3),
  189. BERLIN2_PLL_SELECT(REG_CLKSELECT1, 12),
  190. BERLIN2_DIV_SELECT(REG_CLKSELECT1, 15),
  191. BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 24),
  192. BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 25),
  193. BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 26),
  194. },
  195. .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
  196. .flags = 0,
  197. },
  198. {
  199. .name = "nfc_ecc",
  200. .parent_ids = default_parent_ids,
  201. .num_parents = ARRAY_SIZE(default_parent_ids),
  202. .map = {
  203. BERLIN2_DIV_GATE(REG_CLKENABLE, 19),
  204. BERLIN2_PLL_SELECT(REG_CLKSELECT1, 18),
  205. BERLIN2_DIV_SELECT(REG_CLKSELECT1, 21),
  206. BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 27),
  207. BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 28),
  208. BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 29),
  209. },
  210. .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
  211. .flags = 0,
  212. },
  213. {
  214. .name = "vpp",
  215. .parent_ids = default_parent_ids,
  216. .num_parents = ARRAY_SIZE(default_parent_ids),
  217. .map = {
  218. BERLIN2_DIV_GATE(REG_CLKENABLE, 21),
  219. BERLIN2_PLL_SELECT(REG_CLKSELECT1, 24),
  220. BERLIN2_DIV_SELECT(REG_CLKSELECT1, 27),
  221. BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 30),
  222. BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 31),
  223. BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH1, 0),
  224. },
  225. .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
  226. .flags = 0,
  227. },
  228. {
  229. .name = "app",
  230. .parent_ids = default_parent_ids,
  231. .num_parents = ARRAY_SIZE(default_parent_ids),
  232. .map = {
  233. BERLIN2_DIV_GATE(REG_CLKENABLE, 20),
  234. BERLIN2_PLL_SELECT(REG_CLKSELECT2, 0),
  235. BERLIN2_DIV_SELECT(REG_CLKSELECT2, 3),
  236. BERLIN2_PLL_SWITCH(REG_CLKSWITCH1, 1),
  237. BERLIN2_DIV_SWITCH(REG_CLKSWITCH1, 2),
  238. BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH1, 3),
  239. },
  240. .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
  241. .flags = 0,
  242. },
  243. {
  244. .name = "sdio0xin",
  245. .parent_ids = default_parent_ids,
  246. .num_parents = ARRAY_SIZE(default_parent_ids),
  247. .map = {
  248. BERLIN2_SINGLE_DIV(REG_SDIO0XIN_CLKCTL),
  249. },
  250. .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
  251. .flags = 0,
  252. },
  253. {
  254. .name = "sdio1xin",
  255. .parent_ids = default_parent_ids,
  256. .num_parents = ARRAY_SIZE(default_parent_ids),
  257. .map = {
  258. BERLIN2_SINGLE_DIV(REG_SDIO1XIN_CLKCTL),
  259. },
  260. .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
  261. .flags = 0,
  262. },
  263. };
  264. static const struct berlin2_gate_data bg2q_gates[] __initconst = {
  265. { "gfx2daxi", "perif", 5 },
  266. { "geth0", "perif", 8 },
  267. { "sata", "perif", 9 },
  268. { "ahbapb", "perif", 10, CLK_IGNORE_UNUSED },
  269. { "usb0", "perif", 11 },
  270. { "usb1", "perif", 12 },
  271. { "usb2", "perif", 13 },
  272. { "usb3", "perif", 14 },
  273. { "pbridge", "perif", 15, CLK_IGNORE_UNUSED },
  274. { "sdio", "perif", 16 },
  275. { "nfc", "perif", 18 },
  276. { "pcie", "perif", 22 },
  277. };
  278. static void __init berlin2q_clock_setup(struct device_node *np)
  279. {
  280. struct device_node *parent_np = of_get_parent(np);
  281. const char *parent_names[9];
  282. struct clk *clk;
  283. struct clk_hw **hws;
  284. int n, ret;
  285. clk_data = kzalloc(sizeof(*clk_data) +
  286. sizeof(*clk_data->hws) * MAX_CLKS, GFP_KERNEL);
  287. if (!clk_data)
  288. return;
  289. clk_data->num = MAX_CLKS;
  290. hws = clk_data->hws;
  291. gbase = of_iomap(parent_np, 0);
  292. if (!gbase) {
  293. pr_err("%pOF: Unable to map global base\n", np);
  294. return;
  295. }
  296. /* BG2Q CPU PLL is not part of global registers */
  297. cpupll_base = of_iomap(parent_np, 1);
  298. if (!cpupll_base) {
  299. pr_err("%pOF: Unable to map cpupll base\n", np);
  300. iounmap(gbase);
  301. return;
  302. }
  303. /* overwrite default clock names with DT provided ones */
  304. clk = of_clk_get_by_name(np, clk_names[REFCLK]);
  305. if (!IS_ERR(clk)) {
  306. clk_names[REFCLK] = __clk_get_name(clk);
  307. clk_put(clk);
  308. }
  309. /* simple register PLLs */
  310. ret = berlin2_pll_register(&bg2q_pll_map, gbase + REG_SYSPLLCTL0,
  311. clk_names[SYSPLL], clk_names[REFCLK], 0);
  312. if (ret)
  313. goto bg2q_fail;
  314. ret = berlin2_pll_register(&bg2q_pll_map, cpupll_base,
  315. clk_names[CPUPLL], clk_names[REFCLK], 0);
  316. if (ret)
  317. goto bg2q_fail;
  318. /* TODO: add BG2Q AVPLL */
  319. /*
  320. * TODO: add reference clock bypass switches:
  321. * memPLLSWBypass, cpuPLLSWBypass, and sysPLLSWBypass
  322. */
  323. /* clock divider cells */
  324. for (n = 0; n < ARRAY_SIZE(bg2q_divs); n++) {
  325. const struct berlin2_div_data *dd = &bg2q_divs[n];
  326. int k;
  327. for (k = 0; k < dd->num_parents; k++)
  328. parent_names[k] = clk_names[dd->parent_ids[k]];
  329. hws[CLKID_SYS + n] = berlin2_div_register(&dd->map, gbase,
  330. dd->name, dd->div_flags, parent_names,
  331. dd->num_parents, dd->flags, &lock);
  332. }
  333. /* clock gate cells */
  334. for (n = 0; n < ARRAY_SIZE(bg2q_gates); n++) {
  335. const struct berlin2_gate_data *gd = &bg2q_gates[n];
  336. hws[CLKID_GFX2DAXI + n] = clk_hw_register_gate(NULL, gd->name,
  337. gd->parent_name, gd->flags, gbase + REG_CLKENABLE,
  338. gd->bit_idx, 0, &lock);
  339. }
  340. /* cpuclk divider is fixed to 1 */
  341. hws[CLKID_CPU] =
  342. clk_hw_register_fixed_factor(NULL, "cpu", clk_names[CPUPLL],
  343. 0, 1, 1);
  344. /* twdclk is derived from cpu/3 */
  345. hws[CLKID_TWD] =
  346. clk_hw_register_fixed_factor(NULL, "twd", "cpu", 0, 1, 3);
  347. /* check for errors on leaf clocks */
  348. for (n = 0; n < MAX_CLKS; n++) {
  349. if (!IS_ERR(hws[n]))
  350. continue;
  351. pr_err("%pOF: Unable to register leaf clock %d\n", np, n);
  352. goto bg2q_fail;
  353. }
  354. /* register clk-provider */
  355. of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_data);
  356. return;
  357. bg2q_fail:
  358. iounmap(cpupll_base);
  359. iounmap(gbase);
  360. }
  361. CLK_OF_DECLARE(berlin2q_clk, "marvell,berlin2q-clk",
  362. berlin2q_clock_setup);