clkgen-pll.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. * Copyright (C) 2014 STMicroelectronics (R&D) Limited
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. */
  10. /*
  11. * Authors:
  12. * Stephen Gallimore <stephen.gallimore@st.com>,
  13. * Pankaj Dev <pankaj.dev@st.com>.
  14. */
  15. #include <linux/slab.h>
  16. #include <linux/of_address.h>
  17. #include <linux/clk-provider.h>
  18. #include "clkgen.h"
  19. static DEFINE_SPINLOCK(clkgena_c32_odf_lock);
  20. /*
  21. * Common PLL configuration register bits for PLL800 and PLL1600 C65
  22. */
  23. #define C65_MDIV_PLL800_MASK (0xff)
  24. #define C65_MDIV_PLL1600_MASK (0x7)
  25. #define C65_NDIV_MASK (0xff)
  26. #define C65_PDIV_MASK (0x7)
  27. /*
  28. * PLL configuration register bits for PLL3200 C32
  29. */
  30. #define C32_NDIV_MASK (0xff)
  31. #define C32_IDF_MASK (0x7)
  32. #define C32_ODF_MASK (0x3f)
  33. #define C32_LDF_MASK (0x7f)
  34. #define C32_MAX_ODFS (4)
  35. struct clkgen_pll_data {
  36. struct clkgen_field pdn_status;
  37. struct clkgen_field locked_status;
  38. struct clkgen_field mdiv;
  39. struct clkgen_field ndiv;
  40. struct clkgen_field pdiv;
  41. struct clkgen_field idf;
  42. struct clkgen_field ldf;
  43. unsigned int num_odfs;
  44. struct clkgen_field odf[C32_MAX_ODFS];
  45. struct clkgen_field odf_gate[C32_MAX_ODFS];
  46. const struct clk_ops *ops;
  47. };
  48. static const struct clk_ops st_pll1600c65_ops;
  49. static const struct clk_ops st_pll800c65_ops;
  50. static const struct clk_ops stm_pll3200c32_ops;
  51. static const struct clk_ops st_pll1200c32_ops;
  52. static const struct clkgen_pll_data st_pll1600c65_ax = {
  53. .pdn_status = CLKGEN_FIELD(0x0, 0x1, 19),
  54. .locked_status = CLKGEN_FIELD(0x0, 0x1, 31),
  55. .mdiv = CLKGEN_FIELD(0x0, C65_MDIV_PLL1600_MASK, 0),
  56. .ndiv = CLKGEN_FIELD(0x0, C65_NDIV_MASK, 8),
  57. .ops = &st_pll1600c65_ops
  58. };
  59. static const struct clkgen_pll_data st_pll800c65_ax = {
  60. .pdn_status = CLKGEN_FIELD(0x0, 0x1, 19),
  61. .locked_status = CLKGEN_FIELD(0x0, 0x1, 31),
  62. .mdiv = CLKGEN_FIELD(0x0, C65_MDIV_PLL800_MASK, 0),
  63. .ndiv = CLKGEN_FIELD(0x0, C65_NDIV_MASK, 8),
  64. .pdiv = CLKGEN_FIELD(0x0, C65_PDIV_MASK, 16),
  65. .ops = &st_pll800c65_ops
  66. };
  67. static const struct clkgen_pll_data st_pll3200c32_a1x_0 = {
  68. .pdn_status = CLKGEN_FIELD(0x0, 0x1, 31),
  69. .locked_status = CLKGEN_FIELD(0x4, 0x1, 31),
  70. .ndiv = CLKGEN_FIELD(0x0, C32_NDIV_MASK, 0x0),
  71. .idf = CLKGEN_FIELD(0x4, C32_IDF_MASK, 0x0),
  72. .num_odfs = 4,
  73. .odf = { CLKGEN_FIELD(0x54, C32_ODF_MASK, 4),
  74. CLKGEN_FIELD(0x54, C32_ODF_MASK, 10),
  75. CLKGEN_FIELD(0x54, C32_ODF_MASK, 16),
  76. CLKGEN_FIELD(0x54, C32_ODF_MASK, 22) },
  77. .odf_gate = { CLKGEN_FIELD(0x54, 0x1, 0),
  78. CLKGEN_FIELD(0x54, 0x1, 1),
  79. CLKGEN_FIELD(0x54, 0x1, 2),
  80. CLKGEN_FIELD(0x54, 0x1, 3) },
  81. .ops = &stm_pll3200c32_ops,
  82. };
  83. static const struct clkgen_pll_data st_pll3200c32_a1x_1 = {
  84. .pdn_status = CLKGEN_FIELD(0xC, 0x1, 31),
  85. .locked_status = CLKGEN_FIELD(0x10, 0x1, 31),
  86. .ndiv = CLKGEN_FIELD(0xC, C32_NDIV_MASK, 0x0),
  87. .idf = CLKGEN_FIELD(0x10, C32_IDF_MASK, 0x0),
  88. .num_odfs = 4,
  89. .odf = { CLKGEN_FIELD(0x58, C32_ODF_MASK, 4),
  90. CLKGEN_FIELD(0x58, C32_ODF_MASK, 10),
  91. CLKGEN_FIELD(0x58, C32_ODF_MASK, 16),
  92. CLKGEN_FIELD(0x58, C32_ODF_MASK, 22) },
  93. .odf_gate = { CLKGEN_FIELD(0x58, 0x1, 0),
  94. CLKGEN_FIELD(0x58, 0x1, 1),
  95. CLKGEN_FIELD(0x58, 0x1, 2),
  96. CLKGEN_FIELD(0x58, 0x1, 3) },
  97. .ops = &stm_pll3200c32_ops,
  98. };
  99. /* 415 specific */
  100. static const struct clkgen_pll_data st_pll3200c32_a9_415 = {
  101. .pdn_status = CLKGEN_FIELD(0x0, 0x1, 0),
  102. .locked_status = CLKGEN_FIELD(0x6C, 0x1, 0),
  103. .ndiv = CLKGEN_FIELD(0x0, C32_NDIV_MASK, 9),
  104. .idf = CLKGEN_FIELD(0x0, C32_IDF_MASK, 22),
  105. .num_odfs = 1,
  106. .odf = { CLKGEN_FIELD(0x0, C32_ODF_MASK, 3) },
  107. .odf_gate = { CLKGEN_FIELD(0x0, 0x1, 28) },
  108. .ops = &stm_pll3200c32_ops,
  109. };
  110. static const struct clkgen_pll_data st_pll3200c32_ddr_415 = {
  111. .pdn_status = CLKGEN_FIELD(0x0, 0x1, 0),
  112. .locked_status = CLKGEN_FIELD(0x100, 0x1, 0),
  113. .ndiv = CLKGEN_FIELD(0x8, C32_NDIV_MASK, 0),
  114. .idf = CLKGEN_FIELD(0x0, C32_IDF_MASK, 25),
  115. .num_odfs = 2,
  116. .odf = { CLKGEN_FIELD(0x8, C32_ODF_MASK, 8),
  117. CLKGEN_FIELD(0x8, C32_ODF_MASK, 14) },
  118. .odf_gate = { CLKGEN_FIELD(0x4, 0x1, 28),
  119. CLKGEN_FIELD(0x4, 0x1, 29) },
  120. .ops = &stm_pll3200c32_ops,
  121. };
  122. static const struct clkgen_pll_data st_pll1200c32_gpu_415 = {
  123. .pdn_status = CLKGEN_FIELD(0x144, 0x1, 3),
  124. .locked_status = CLKGEN_FIELD(0x168, 0x1, 0),
  125. .ldf = CLKGEN_FIELD(0x0, C32_LDF_MASK, 3),
  126. .idf = CLKGEN_FIELD(0x0, C32_IDF_MASK, 0),
  127. .num_odfs = 0,
  128. .odf = { CLKGEN_FIELD(0x0, C32_ODF_MASK, 10) },
  129. .ops = &st_pll1200c32_ops,
  130. };
  131. /* 416 specific */
  132. static const struct clkgen_pll_data st_pll3200c32_a9_416 = {
  133. .pdn_status = CLKGEN_FIELD(0x0, 0x1, 0),
  134. .locked_status = CLKGEN_FIELD(0x6C, 0x1, 0),
  135. .ndiv = CLKGEN_FIELD(0x8, C32_NDIV_MASK, 0),
  136. .idf = CLKGEN_FIELD(0x0, C32_IDF_MASK, 25),
  137. .num_odfs = 1,
  138. .odf = { CLKGEN_FIELD(0x8, C32_ODF_MASK, 8) },
  139. .odf_gate = { CLKGEN_FIELD(0x4, 0x1, 28) },
  140. .ops = &stm_pll3200c32_ops,
  141. };
  142. static const struct clkgen_pll_data st_pll3200c32_ddr_416 = {
  143. .pdn_status = CLKGEN_FIELD(0x0, 0x1, 0),
  144. .locked_status = CLKGEN_FIELD(0x10C, 0x1, 0),
  145. .ndiv = CLKGEN_FIELD(0x8, C32_NDIV_MASK, 0),
  146. .idf = CLKGEN_FIELD(0x0, C32_IDF_MASK, 25),
  147. .num_odfs = 2,
  148. .odf = { CLKGEN_FIELD(0x8, C32_ODF_MASK, 8),
  149. CLKGEN_FIELD(0x8, C32_ODF_MASK, 14) },
  150. .odf_gate = { CLKGEN_FIELD(0x4, 0x1, 28),
  151. CLKGEN_FIELD(0x4, 0x1, 29) },
  152. .ops = &stm_pll3200c32_ops,
  153. };
  154. static const struct clkgen_pll_data st_pll1200c32_gpu_416 = {
  155. .pdn_status = CLKGEN_FIELD(0x8E4, 0x1, 3),
  156. .locked_status = CLKGEN_FIELD(0x90C, 0x1, 0),
  157. .ldf = CLKGEN_FIELD(0x0, C32_LDF_MASK, 3),
  158. .idf = CLKGEN_FIELD(0x0, C32_IDF_MASK, 0),
  159. .num_odfs = 0,
  160. .odf = { CLKGEN_FIELD(0x0, C32_ODF_MASK, 10) },
  161. .ops = &st_pll1200c32_ops,
  162. };
  163. static const struct clkgen_pll_data st_pll3200c32_407_a0 = {
  164. /* 407 A0 */
  165. .pdn_status = CLKGEN_FIELD(0x2a0, 0x1, 8),
  166. .locked_status = CLKGEN_FIELD(0x2a0, 0x1, 24),
  167. .ndiv = CLKGEN_FIELD(0x2a4, C32_NDIV_MASK, 16),
  168. .idf = CLKGEN_FIELD(0x2a4, C32_IDF_MASK, 0x0),
  169. .num_odfs = 1,
  170. .odf = { CLKGEN_FIELD(0x2b4, C32_ODF_MASK, 0) },
  171. .odf_gate = { CLKGEN_FIELD(0x2b4, 0x1, 6) },
  172. .ops = &stm_pll3200c32_ops,
  173. };
  174. static const struct clkgen_pll_data st_pll3200c32_407_c0_0 = {
  175. /* 407 C0 PLL0 */
  176. .pdn_status = CLKGEN_FIELD(0x2a0, 0x1, 8),
  177. .locked_status = CLKGEN_FIELD(0x2a0, 0x1, 24),
  178. .ndiv = CLKGEN_FIELD(0x2a4, C32_NDIV_MASK, 16),
  179. .idf = CLKGEN_FIELD(0x2a4, C32_IDF_MASK, 0x0),
  180. .num_odfs = 1,
  181. .odf = { CLKGEN_FIELD(0x2b4, C32_ODF_MASK, 0) },
  182. .odf_gate = { CLKGEN_FIELD(0x2b4, 0x1, 6) },
  183. .ops = &stm_pll3200c32_ops,
  184. };
  185. static const struct clkgen_pll_data st_pll3200c32_407_c0_1 = {
  186. /* 407 C0 PLL1 */
  187. .pdn_status = CLKGEN_FIELD(0x2c8, 0x1, 8),
  188. .locked_status = CLKGEN_FIELD(0x2c8, 0x1, 24),
  189. .ndiv = CLKGEN_FIELD(0x2cc, C32_NDIV_MASK, 16),
  190. .idf = CLKGEN_FIELD(0x2cc, C32_IDF_MASK, 0x0),
  191. .num_odfs = 1,
  192. .odf = { CLKGEN_FIELD(0x2dc, C32_ODF_MASK, 0) },
  193. .odf_gate = { CLKGEN_FIELD(0x2dc, 0x1, 6) },
  194. .ops = &stm_pll3200c32_ops,
  195. };
  196. static const struct clkgen_pll_data st_pll3200c32_407_a9 = {
  197. /* 407 A9 */
  198. .pdn_status = CLKGEN_FIELD(0x1a8, 0x1, 0),
  199. .locked_status = CLKGEN_FIELD(0x87c, 0x1, 0),
  200. .ndiv = CLKGEN_FIELD(0x1b0, C32_NDIV_MASK, 0),
  201. .idf = CLKGEN_FIELD(0x1a8, C32_IDF_MASK, 25),
  202. .num_odfs = 1,
  203. .odf = { CLKGEN_FIELD(0x1b0, C32_ODF_MASK, 8) },
  204. .odf_gate = { CLKGEN_FIELD(0x1ac, 0x1, 28) },
  205. .ops = &stm_pll3200c32_ops,
  206. };
  207. /**
  208. * DOC: Clock Generated by PLL, rate set and enabled by bootloader
  209. *
  210. * Traits of this clock:
  211. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  212. * enable - clk_enable/disable only ensures parent is enabled
  213. * rate - rate is fixed. No clk_set_rate support
  214. * parent - fixed parent. No clk_set_parent support
  215. */
  216. /**
  217. * PLL clock that is integrated in the ClockGenA instances on the STiH415
  218. * and STiH416.
  219. *
  220. * @hw: handle between common and hardware-specific interfaces.
  221. * @type: PLL instance type.
  222. * @regs_base: base of the PLL configuration register(s).
  223. *
  224. */
  225. struct clkgen_pll {
  226. struct clk_hw hw;
  227. struct clkgen_pll_data *data;
  228. void __iomem *regs_base;
  229. };
  230. #define to_clkgen_pll(_hw) container_of(_hw, struct clkgen_pll, hw)
  231. static int clkgen_pll_is_locked(struct clk_hw *hw)
  232. {
  233. struct clkgen_pll *pll = to_clkgen_pll(hw);
  234. u32 locked = CLKGEN_READ(pll, locked_status);
  235. return !!locked;
  236. }
  237. static int clkgen_pll_is_enabled(struct clk_hw *hw)
  238. {
  239. struct clkgen_pll *pll = to_clkgen_pll(hw);
  240. u32 poweroff = CLKGEN_READ(pll, pdn_status);
  241. return !poweroff;
  242. }
  243. static unsigned long recalc_stm_pll800c65(struct clk_hw *hw,
  244. unsigned long parent_rate)
  245. {
  246. struct clkgen_pll *pll = to_clkgen_pll(hw);
  247. unsigned long mdiv, ndiv, pdiv;
  248. unsigned long rate;
  249. uint64_t res;
  250. if (!clkgen_pll_is_enabled(hw) || !clkgen_pll_is_locked(hw))
  251. return 0;
  252. pdiv = CLKGEN_READ(pll, pdiv);
  253. mdiv = CLKGEN_READ(pll, mdiv);
  254. ndiv = CLKGEN_READ(pll, ndiv);
  255. if (!mdiv)
  256. mdiv++; /* mdiv=0 or 1 => MDIV=1 */
  257. res = (uint64_t)2 * (uint64_t)parent_rate * (uint64_t)ndiv;
  258. rate = (unsigned long)div64_u64(res, mdiv * (1 << pdiv));
  259. pr_debug("%s:%s rate %lu\n", __clk_get_name(hw->clk), __func__, rate);
  260. return rate;
  261. }
  262. static unsigned long recalc_stm_pll1600c65(struct clk_hw *hw,
  263. unsigned long parent_rate)
  264. {
  265. struct clkgen_pll *pll = to_clkgen_pll(hw);
  266. unsigned long mdiv, ndiv;
  267. unsigned long rate;
  268. if (!clkgen_pll_is_enabled(hw) || !clkgen_pll_is_locked(hw))
  269. return 0;
  270. mdiv = CLKGEN_READ(pll, mdiv);
  271. ndiv = CLKGEN_READ(pll, ndiv);
  272. if (!mdiv)
  273. mdiv = 1;
  274. /* Note: input is divided by 1000 to avoid overflow */
  275. rate = ((2 * (parent_rate / 1000) * ndiv) / mdiv) * 1000;
  276. pr_debug("%s:%s rate %lu\n", __clk_get_name(hw->clk), __func__, rate);
  277. return rate;
  278. }
  279. static unsigned long recalc_stm_pll3200c32(struct clk_hw *hw,
  280. unsigned long parent_rate)
  281. {
  282. struct clkgen_pll *pll = to_clkgen_pll(hw);
  283. unsigned long ndiv, idf;
  284. unsigned long rate = 0;
  285. if (!clkgen_pll_is_enabled(hw) || !clkgen_pll_is_locked(hw))
  286. return 0;
  287. ndiv = CLKGEN_READ(pll, ndiv);
  288. idf = CLKGEN_READ(pll, idf);
  289. if (idf)
  290. /* Note: input is divided to avoid overflow */
  291. rate = ((2 * (parent_rate/1000) * ndiv) / idf) * 1000;
  292. pr_debug("%s:%s rate %lu\n", __clk_get_name(hw->clk), __func__, rate);
  293. return rate;
  294. }
  295. static unsigned long recalc_stm_pll1200c32(struct clk_hw *hw,
  296. unsigned long parent_rate)
  297. {
  298. struct clkgen_pll *pll = to_clkgen_pll(hw);
  299. unsigned long odf, ldf, idf;
  300. unsigned long rate;
  301. if (!clkgen_pll_is_enabled(hw) || !clkgen_pll_is_locked(hw))
  302. return 0;
  303. odf = CLKGEN_READ(pll, odf[0]);
  304. ldf = CLKGEN_READ(pll, ldf);
  305. idf = CLKGEN_READ(pll, idf);
  306. if (!idf) /* idf==0 means 1 */
  307. idf = 1;
  308. if (!odf) /* odf==0 means 1 */
  309. odf = 1;
  310. /* Note: input is divided by 1000 to avoid overflow */
  311. rate = (((parent_rate / 1000) * ldf) / (odf * idf)) * 1000;
  312. pr_debug("%s:%s rate %lu\n", __clk_get_name(hw->clk), __func__, rate);
  313. return rate;
  314. }
  315. static const struct clk_ops st_pll1600c65_ops = {
  316. .is_enabled = clkgen_pll_is_enabled,
  317. .recalc_rate = recalc_stm_pll1600c65,
  318. };
  319. static const struct clk_ops st_pll800c65_ops = {
  320. .is_enabled = clkgen_pll_is_enabled,
  321. .recalc_rate = recalc_stm_pll800c65,
  322. };
  323. static const struct clk_ops stm_pll3200c32_ops = {
  324. .is_enabled = clkgen_pll_is_enabled,
  325. .recalc_rate = recalc_stm_pll3200c32,
  326. };
  327. static const struct clk_ops st_pll1200c32_ops = {
  328. .is_enabled = clkgen_pll_is_enabled,
  329. .recalc_rate = recalc_stm_pll1200c32,
  330. };
  331. static struct clk * __init clkgen_pll_register(const char *parent_name,
  332. struct clkgen_pll_data *pll_data,
  333. void __iomem *reg,
  334. const char *clk_name)
  335. {
  336. struct clkgen_pll *pll;
  337. struct clk *clk;
  338. struct clk_init_data init;
  339. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  340. if (!pll)
  341. return ERR_PTR(-ENOMEM);
  342. init.name = clk_name;
  343. init.ops = pll_data->ops;
  344. init.flags = CLK_IS_BASIC;
  345. init.parent_names = &parent_name;
  346. init.num_parents = 1;
  347. pll->data = pll_data;
  348. pll->regs_base = reg;
  349. pll->hw.init = &init;
  350. clk = clk_register(NULL, &pll->hw);
  351. if (IS_ERR(clk)) {
  352. kfree(pll);
  353. return clk;
  354. }
  355. pr_debug("%s: parent %s rate %lu\n",
  356. __clk_get_name(clk),
  357. __clk_get_name(clk_get_parent(clk)),
  358. clk_get_rate(clk));
  359. return clk;
  360. }
  361. static struct clk * __init clkgen_c65_lsdiv_register(const char *parent_name,
  362. const char *clk_name)
  363. {
  364. struct clk *clk;
  365. clk = clk_register_fixed_factor(NULL, clk_name, parent_name, 0, 1, 2);
  366. if (IS_ERR(clk))
  367. return clk;
  368. pr_debug("%s: parent %s rate %lu\n",
  369. __clk_get_name(clk),
  370. __clk_get_name(clk_get_parent(clk)),
  371. clk_get_rate(clk));
  372. return clk;
  373. }
  374. static void __iomem * __init clkgen_get_register_base(
  375. struct device_node *np)
  376. {
  377. struct device_node *pnode;
  378. void __iomem *reg = NULL;
  379. pnode = of_get_parent(np);
  380. if (!pnode)
  381. return NULL;
  382. reg = of_iomap(pnode, 0);
  383. of_node_put(pnode);
  384. return reg;
  385. }
  386. #define CLKGENAx_PLL0_OFFSET 0x0
  387. #define CLKGENAx_PLL1_OFFSET 0x4
  388. static void __init clkgena_c65_pll_setup(struct device_node *np)
  389. {
  390. const int num_pll_outputs = 3;
  391. struct clk_onecell_data *clk_data;
  392. const char *parent_name;
  393. void __iomem *reg;
  394. const char *clk_name;
  395. parent_name = of_clk_get_parent_name(np, 0);
  396. if (!parent_name)
  397. return;
  398. reg = clkgen_get_register_base(np);
  399. if (!reg)
  400. return;
  401. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  402. if (!clk_data)
  403. return;
  404. clk_data->clk_num = num_pll_outputs;
  405. clk_data->clks = kzalloc(clk_data->clk_num * sizeof(struct clk *),
  406. GFP_KERNEL);
  407. if (!clk_data->clks)
  408. goto err;
  409. if (of_property_read_string_index(np, "clock-output-names",
  410. 0, &clk_name))
  411. goto err;
  412. /*
  413. * PLL0 HS (high speed) output
  414. */
  415. clk_data->clks[0] = clkgen_pll_register(parent_name,
  416. (struct clkgen_pll_data *) &st_pll1600c65_ax,
  417. reg + CLKGENAx_PLL0_OFFSET, clk_name);
  418. if (IS_ERR(clk_data->clks[0]))
  419. goto err;
  420. if (of_property_read_string_index(np, "clock-output-names",
  421. 1, &clk_name))
  422. goto err;
  423. /*
  424. * PLL0 LS (low speed) output, which is a fixed divide by 2 of the
  425. * high speed output.
  426. */
  427. clk_data->clks[1] = clkgen_c65_lsdiv_register(__clk_get_name
  428. (clk_data->clks[0]),
  429. clk_name);
  430. if (IS_ERR(clk_data->clks[1]))
  431. goto err;
  432. if (of_property_read_string_index(np, "clock-output-names",
  433. 2, &clk_name))
  434. goto err;
  435. /*
  436. * PLL1 output
  437. */
  438. clk_data->clks[2] = clkgen_pll_register(parent_name,
  439. (struct clkgen_pll_data *) &st_pll800c65_ax,
  440. reg + CLKGENAx_PLL1_OFFSET, clk_name);
  441. if (IS_ERR(clk_data->clks[2]))
  442. goto err;
  443. of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
  444. return;
  445. err:
  446. kfree(clk_data->clks);
  447. kfree(clk_data);
  448. }
  449. CLK_OF_DECLARE(clkgena_c65_plls,
  450. "st,clkgena-plls-c65", clkgena_c65_pll_setup);
  451. static struct clk * __init clkgen_odf_register(const char *parent_name,
  452. void __iomem *reg,
  453. struct clkgen_pll_data *pll_data,
  454. int odf,
  455. spinlock_t *odf_lock,
  456. const char *odf_name)
  457. {
  458. struct clk *clk;
  459. unsigned long flags;
  460. struct clk_gate *gate;
  461. struct clk_divider *div;
  462. flags = CLK_GET_RATE_NOCACHE | CLK_SET_RATE_GATE;
  463. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  464. if (!gate)
  465. return ERR_PTR(-ENOMEM);
  466. gate->flags = CLK_GATE_SET_TO_DISABLE;
  467. gate->reg = reg + pll_data->odf_gate[odf].offset;
  468. gate->bit_idx = pll_data->odf_gate[odf].shift;
  469. gate->lock = odf_lock;
  470. div = kzalloc(sizeof(*div), GFP_KERNEL);
  471. if (!div) {
  472. kfree(gate);
  473. return ERR_PTR(-ENOMEM);
  474. }
  475. div->flags = CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO;
  476. div->reg = reg + pll_data->odf[odf].offset;
  477. div->shift = pll_data->odf[odf].shift;
  478. div->width = fls(pll_data->odf[odf].mask);
  479. div->lock = odf_lock;
  480. clk = clk_register_composite(NULL, odf_name, &parent_name, 1,
  481. NULL, NULL,
  482. &div->hw, &clk_divider_ops,
  483. &gate->hw, &clk_gate_ops,
  484. flags);
  485. if (IS_ERR(clk))
  486. return clk;
  487. pr_debug("%s: parent %s rate %lu\n",
  488. __clk_get_name(clk),
  489. __clk_get_name(clk_get_parent(clk)),
  490. clk_get_rate(clk));
  491. return clk;
  492. }
  493. static const struct of_device_id c32_pll_of_match[] = {
  494. {
  495. .compatible = "st,plls-c32-a1x-0",
  496. .data = &st_pll3200c32_a1x_0,
  497. },
  498. {
  499. .compatible = "st,plls-c32-a1x-1",
  500. .data = &st_pll3200c32_a1x_1,
  501. },
  502. {
  503. .compatible = "st,stih415-plls-c32-a9",
  504. .data = &st_pll3200c32_a9_415,
  505. },
  506. {
  507. .compatible = "st,stih415-plls-c32-ddr",
  508. .data = &st_pll3200c32_ddr_415,
  509. },
  510. {
  511. .compatible = "st,stih416-plls-c32-a9",
  512. .data = &st_pll3200c32_a9_416,
  513. },
  514. {
  515. .compatible = "st,stih416-plls-c32-ddr",
  516. .data = &st_pll3200c32_ddr_416,
  517. },
  518. {
  519. .compatible = "st,stih407-plls-c32-a0",
  520. .data = &st_pll3200c32_407_a0,
  521. },
  522. {
  523. .compatible = "st,stih407-plls-c32-c0_0",
  524. .data = &st_pll3200c32_407_c0_0,
  525. },
  526. {
  527. .compatible = "st,stih407-plls-c32-c0_1",
  528. .data = &st_pll3200c32_407_c0_1,
  529. },
  530. {
  531. .compatible = "st,stih407-plls-c32-a9",
  532. .data = &st_pll3200c32_407_a9,
  533. },
  534. {}
  535. };
  536. static void __init clkgen_c32_pll_setup(struct device_node *np)
  537. {
  538. const struct of_device_id *match;
  539. struct clk *clk;
  540. const char *parent_name, *pll_name;
  541. void __iomem *pll_base;
  542. int num_odfs, odf;
  543. struct clk_onecell_data *clk_data;
  544. struct clkgen_pll_data *data;
  545. match = of_match_node(c32_pll_of_match, np);
  546. if (!match) {
  547. pr_err("%s: No matching data\n", __func__);
  548. return;
  549. }
  550. data = (struct clkgen_pll_data *) match->data;
  551. parent_name = of_clk_get_parent_name(np, 0);
  552. if (!parent_name)
  553. return;
  554. pll_base = clkgen_get_register_base(np);
  555. if (!pll_base)
  556. return;
  557. clk = clkgen_pll_register(parent_name, data, pll_base, np->name);
  558. if (IS_ERR(clk))
  559. return;
  560. pll_name = __clk_get_name(clk);
  561. num_odfs = data->num_odfs;
  562. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  563. if (!clk_data)
  564. return;
  565. clk_data->clk_num = num_odfs;
  566. clk_data->clks = kzalloc(clk_data->clk_num * sizeof(struct clk *),
  567. GFP_KERNEL);
  568. if (!clk_data->clks)
  569. goto err;
  570. for (odf = 0; odf < num_odfs; odf++) {
  571. struct clk *clk;
  572. const char *clk_name;
  573. if (of_property_read_string_index(np, "clock-output-names",
  574. odf, &clk_name))
  575. return;
  576. clk = clkgen_odf_register(pll_name, pll_base, data,
  577. odf, &clkgena_c32_odf_lock, clk_name);
  578. if (IS_ERR(clk))
  579. goto err;
  580. clk_data->clks[odf] = clk;
  581. }
  582. of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
  583. return;
  584. err:
  585. kfree(pll_name);
  586. kfree(clk_data->clks);
  587. kfree(clk_data);
  588. }
  589. CLK_OF_DECLARE(clkgen_c32_pll, "st,clkgen-plls-c32", clkgen_c32_pll_setup);
  590. static const struct of_device_id c32_gpu_pll_of_match[] = {
  591. {
  592. .compatible = "st,stih415-gpu-pll-c32",
  593. .data = &st_pll1200c32_gpu_415,
  594. },
  595. {
  596. .compatible = "st,stih416-gpu-pll-c32",
  597. .data = &st_pll1200c32_gpu_416,
  598. },
  599. {}
  600. };
  601. static void __init clkgengpu_c32_pll_setup(struct device_node *np)
  602. {
  603. const struct of_device_id *match;
  604. struct clk *clk;
  605. const char *parent_name;
  606. void __iomem *reg;
  607. const char *clk_name;
  608. struct clkgen_pll_data *data;
  609. match = of_match_node(c32_gpu_pll_of_match, np);
  610. if (!match) {
  611. pr_err("%s: No matching data\n", __func__);
  612. return;
  613. }
  614. data = (struct clkgen_pll_data *)match->data;
  615. parent_name = of_clk_get_parent_name(np, 0);
  616. if (!parent_name)
  617. return;
  618. reg = clkgen_get_register_base(np);
  619. if (!reg)
  620. return;
  621. if (of_property_read_string_index(np, "clock-output-names",
  622. 0, &clk_name))
  623. return;
  624. /*
  625. * PLL 1200MHz output
  626. */
  627. clk = clkgen_pll_register(parent_name, data, reg, clk_name);
  628. if (!IS_ERR(clk))
  629. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  630. return;
  631. }
  632. CLK_OF_DECLARE(clkgengpu_c32_pll,
  633. "st,clkgengpu-pll-c32", clkgengpu_c32_pll_setup);