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