clk-pll.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. /*
  2. * Copyright (c) 2013 Samsung Electronics Co., Ltd.
  3. * Copyright (c) 2013 Linaro Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This file contains the utility functions to register the pll clocks.
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/hrtimer.h>
  13. #include <linux/delay.h>
  14. #include <linux/slab.h>
  15. #include <linux/clkdev.h>
  16. #include "clk.h"
  17. #include "clk-pll.h"
  18. #define PLL_TIMEOUT_MS 10
  19. struct samsung_clk_pll {
  20. struct clk_hw hw;
  21. void __iomem *lock_reg;
  22. void __iomem *con_reg;
  23. /* PLL enable control bit offset in @con_reg register */
  24. unsigned short enable_offs;
  25. /* PLL lock status bit offset in @con_reg register */
  26. unsigned short lock_offs;
  27. enum samsung_pll_type type;
  28. unsigned int rate_count;
  29. const struct samsung_pll_rate_table *rate_table;
  30. };
  31. #define to_clk_pll(_hw) container_of(_hw, struct samsung_clk_pll, hw)
  32. static const struct samsung_pll_rate_table *samsung_get_pll_settings(
  33. struct samsung_clk_pll *pll, unsigned long rate)
  34. {
  35. const struct samsung_pll_rate_table *rate_table = pll->rate_table;
  36. int i;
  37. for (i = 0; i < pll->rate_count; i++) {
  38. if (rate == rate_table[i].rate)
  39. return &rate_table[i];
  40. }
  41. return NULL;
  42. }
  43. static long samsung_pll_round_rate(struct clk_hw *hw,
  44. unsigned long drate, unsigned long *prate)
  45. {
  46. struct samsung_clk_pll *pll = to_clk_pll(hw);
  47. const struct samsung_pll_rate_table *rate_table = pll->rate_table;
  48. int i;
  49. /* Assumming rate_table is in descending order */
  50. for (i = 0; i < pll->rate_count; i++) {
  51. if (drate >= rate_table[i].rate)
  52. return rate_table[i].rate;
  53. }
  54. /* return minimum supported value */
  55. return rate_table[i - 1].rate;
  56. }
  57. static int samsung_pll3xxx_enable(struct clk_hw *hw)
  58. {
  59. struct samsung_clk_pll *pll = to_clk_pll(hw);
  60. u32 tmp;
  61. tmp = readl_relaxed(pll->con_reg);
  62. tmp |= BIT(pll->enable_offs);
  63. writel_relaxed(tmp, pll->con_reg);
  64. /* wait lock time */
  65. do {
  66. cpu_relax();
  67. tmp = readl_relaxed(pll->con_reg);
  68. } while (!(tmp & BIT(pll->lock_offs)));
  69. return 0;
  70. }
  71. static void samsung_pll3xxx_disable(struct clk_hw *hw)
  72. {
  73. struct samsung_clk_pll *pll = to_clk_pll(hw);
  74. u32 tmp;
  75. tmp = readl_relaxed(pll->con_reg);
  76. tmp &= ~BIT(pll->enable_offs);
  77. writel_relaxed(tmp, pll->con_reg);
  78. }
  79. /*
  80. * PLL2126 Clock Type
  81. */
  82. #define PLL2126_MDIV_MASK (0xff)
  83. #define PLL2126_PDIV_MASK (0x3f)
  84. #define PLL2126_SDIV_MASK (0x3)
  85. #define PLL2126_MDIV_SHIFT (16)
  86. #define PLL2126_PDIV_SHIFT (8)
  87. #define PLL2126_SDIV_SHIFT (0)
  88. static unsigned long samsung_pll2126_recalc_rate(struct clk_hw *hw,
  89. unsigned long parent_rate)
  90. {
  91. struct samsung_clk_pll *pll = to_clk_pll(hw);
  92. u32 pll_con, mdiv, pdiv, sdiv;
  93. u64 fvco = parent_rate;
  94. pll_con = readl_relaxed(pll->con_reg);
  95. mdiv = (pll_con >> PLL2126_MDIV_SHIFT) & PLL2126_MDIV_MASK;
  96. pdiv = (pll_con >> PLL2126_PDIV_SHIFT) & PLL2126_PDIV_MASK;
  97. sdiv = (pll_con >> PLL2126_SDIV_SHIFT) & PLL2126_SDIV_MASK;
  98. fvco *= (mdiv + 8);
  99. do_div(fvco, (pdiv + 2) << sdiv);
  100. return (unsigned long)fvco;
  101. }
  102. static const struct clk_ops samsung_pll2126_clk_ops = {
  103. .recalc_rate = samsung_pll2126_recalc_rate,
  104. };
  105. /*
  106. * PLL3000 Clock Type
  107. */
  108. #define PLL3000_MDIV_MASK (0xff)
  109. #define PLL3000_PDIV_MASK (0x3)
  110. #define PLL3000_SDIV_MASK (0x3)
  111. #define PLL3000_MDIV_SHIFT (16)
  112. #define PLL3000_PDIV_SHIFT (8)
  113. #define PLL3000_SDIV_SHIFT (0)
  114. static unsigned long samsung_pll3000_recalc_rate(struct clk_hw *hw,
  115. unsigned long parent_rate)
  116. {
  117. struct samsung_clk_pll *pll = to_clk_pll(hw);
  118. u32 pll_con, mdiv, pdiv, sdiv;
  119. u64 fvco = parent_rate;
  120. pll_con = readl_relaxed(pll->con_reg);
  121. mdiv = (pll_con >> PLL3000_MDIV_SHIFT) & PLL3000_MDIV_MASK;
  122. pdiv = (pll_con >> PLL3000_PDIV_SHIFT) & PLL3000_PDIV_MASK;
  123. sdiv = (pll_con >> PLL3000_SDIV_SHIFT) & PLL3000_SDIV_MASK;
  124. fvco *= (2 * (mdiv + 8));
  125. do_div(fvco, pdiv << sdiv);
  126. return (unsigned long)fvco;
  127. }
  128. static const struct clk_ops samsung_pll3000_clk_ops = {
  129. .recalc_rate = samsung_pll3000_recalc_rate,
  130. };
  131. /*
  132. * PLL35xx Clock Type
  133. */
  134. /* Maximum lock time can be 270 * PDIV cycles */
  135. #define PLL35XX_LOCK_FACTOR (270)
  136. #define PLL35XX_MDIV_MASK (0x3FF)
  137. #define PLL35XX_PDIV_MASK (0x3F)
  138. #define PLL35XX_SDIV_MASK (0x7)
  139. #define PLL35XX_MDIV_SHIFT (16)
  140. #define PLL35XX_PDIV_SHIFT (8)
  141. #define PLL35XX_SDIV_SHIFT (0)
  142. #define PLL35XX_LOCK_STAT_SHIFT (29)
  143. #define PLL35XX_ENABLE_SHIFT (31)
  144. static unsigned long samsung_pll35xx_recalc_rate(struct clk_hw *hw,
  145. unsigned long parent_rate)
  146. {
  147. struct samsung_clk_pll *pll = to_clk_pll(hw);
  148. u32 mdiv, pdiv, sdiv, pll_con;
  149. u64 fvco = parent_rate;
  150. pll_con = readl_relaxed(pll->con_reg);
  151. mdiv = (pll_con >> PLL35XX_MDIV_SHIFT) & PLL35XX_MDIV_MASK;
  152. pdiv = (pll_con >> PLL35XX_PDIV_SHIFT) & PLL35XX_PDIV_MASK;
  153. sdiv = (pll_con >> PLL35XX_SDIV_SHIFT) & PLL35XX_SDIV_MASK;
  154. fvco *= mdiv;
  155. do_div(fvco, (pdiv << sdiv));
  156. return (unsigned long)fvco;
  157. }
  158. static inline bool samsung_pll35xx_mp_change(
  159. const struct samsung_pll_rate_table *rate, u32 pll_con)
  160. {
  161. u32 old_mdiv, old_pdiv;
  162. old_mdiv = (pll_con >> PLL35XX_MDIV_SHIFT) & PLL35XX_MDIV_MASK;
  163. old_pdiv = (pll_con >> PLL35XX_PDIV_SHIFT) & PLL35XX_PDIV_MASK;
  164. return (rate->mdiv != old_mdiv || rate->pdiv != old_pdiv);
  165. }
  166. static int samsung_pll35xx_set_rate(struct clk_hw *hw, unsigned long drate,
  167. unsigned long prate)
  168. {
  169. struct samsung_clk_pll *pll = to_clk_pll(hw);
  170. const struct samsung_pll_rate_table *rate;
  171. u32 tmp;
  172. /* Get required rate settings from table */
  173. rate = samsung_get_pll_settings(pll, drate);
  174. if (!rate) {
  175. pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
  176. drate, clk_hw_get_name(hw));
  177. return -EINVAL;
  178. }
  179. tmp = readl_relaxed(pll->con_reg);
  180. if (!(samsung_pll35xx_mp_change(rate, tmp))) {
  181. /* If only s change, change just s value only*/
  182. tmp &= ~(PLL35XX_SDIV_MASK << PLL35XX_SDIV_SHIFT);
  183. tmp |= rate->sdiv << PLL35XX_SDIV_SHIFT;
  184. writel_relaxed(tmp, pll->con_reg);
  185. return 0;
  186. }
  187. /* Set PLL lock time. */
  188. writel_relaxed(rate->pdiv * PLL35XX_LOCK_FACTOR,
  189. pll->lock_reg);
  190. /* Change PLL PMS values */
  191. tmp &= ~((PLL35XX_MDIV_MASK << PLL35XX_MDIV_SHIFT) |
  192. (PLL35XX_PDIV_MASK << PLL35XX_PDIV_SHIFT) |
  193. (PLL35XX_SDIV_MASK << PLL35XX_SDIV_SHIFT));
  194. tmp |= (rate->mdiv << PLL35XX_MDIV_SHIFT) |
  195. (rate->pdiv << PLL35XX_PDIV_SHIFT) |
  196. (rate->sdiv << PLL35XX_SDIV_SHIFT);
  197. writel_relaxed(tmp, pll->con_reg);
  198. /* Wait until the PLL is locked if it is enabled. */
  199. if (tmp & BIT(pll->enable_offs)) {
  200. do {
  201. cpu_relax();
  202. tmp = readl_relaxed(pll->con_reg);
  203. } while (!(tmp & BIT(pll->lock_offs)));
  204. }
  205. return 0;
  206. }
  207. static const struct clk_ops samsung_pll35xx_clk_ops = {
  208. .recalc_rate = samsung_pll35xx_recalc_rate,
  209. .round_rate = samsung_pll_round_rate,
  210. .set_rate = samsung_pll35xx_set_rate,
  211. .enable = samsung_pll3xxx_enable,
  212. .disable = samsung_pll3xxx_disable,
  213. };
  214. static const struct clk_ops samsung_pll35xx_clk_min_ops = {
  215. .recalc_rate = samsung_pll35xx_recalc_rate,
  216. };
  217. /*
  218. * PLL36xx Clock Type
  219. */
  220. /* Maximum lock time can be 3000 * PDIV cycles */
  221. #define PLL36XX_LOCK_FACTOR (3000)
  222. #define PLL36XX_KDIV_MASK (0xFFFF)
  223. #define PLL36XX_MDIV_MASK (0x1FF)
  224. #define PLL36XX_PDIV_MASK (0x3F)
  225. #define PLL36XX_SDIV_MASK (0x7)
  226. #define PLL36XX_MDIV_SHIFT (16)
  227. #define PLL36XX_PDIV_SHIFT (8)
  228. #define PLL36XX_SDIV_SHIFT (0)
  229. #define PLL36XX_KDIV_SHIFT (0)
  230. #define PLL36XX_LOCK_STAT_SHIFT (29)
  231. #define PLL36XX_ENABLE_SHIFT (31)
  232. static unsigned long samsung_pll36xx_recalc_rate(struct clk_hw *hw,
  233. unsigned long parent_rate)
  234. {
  235. struct samsung_clk_pll *pll = to_clk_pll(hw);
  236. u32 mdiv, pdiv, sdiv, pll_con0, pll_con1;
  237. s16 kdiv;
  238. u64 fvco = parent_rate;
  239. pll_con0 = readl_relaxed(pll->con_reg);
  240. pll_con1 = readl_relaxed(pll->con_reg + 4);
  241. mdiv = (pll_con0 >> PLL36XX_MDIV_SHIFT) & PLL36XX_MDIV_MASK;
  242. pdiv = (pll_con0 >> PLL36XX_PDIV_SHIFT) & PLL36XX_PDIV_MASK;
  243. sdiv = (pll_con0 >> PLL36XX_SDIV_SHIFT) & PLL36XX_SDIV_MASK;
  244. kdiv = (s16)(pll_con1 & PLL36XX_KDIV_MASK);
  245. fvco *= (mdiv << 16) + kdiv;
  246. do_div(fvco, (pdiv << sdiv));
  247. fvco >>= 16;
  248. return (unsigned long)fvco;
  249. }
  250. static inline bool samsung_pll36xx_mpk_change(
  251. const struct samsung_pll_rate_table *rate, u32 pll_con0, u32 pll_con1)
  252. {
  253. u32 old_mdiv, old_pdiv, old_kdiv;
  254. old_mdiv = (pll_con0 >> PLL36XX_MDIV_SHIFT) & PLL36XX_MDIV_MASK;
  255. old_pdiv = (pll_con0 >> PLL36XX_PDIV_SHIFT) & PLL36XX_PDIV_MASK;
  256. old_kdiv = (pll_con1 >> PLL36XX_KDIV_SHIFT) & PLL36XX_KDIV_MASK;
  257. return (rate->mdiv != old_mdiv || rate->pdiv != old_pdiv ||
  258. rate->kdiv != old_kdiv);
  259. }
  260. static int samsung_pll36xx_set_rate(struct clk_hw *hw, unsigned long drate,
  261. unsigned long parent_rate)
  262. {
  263. struct samsung_clk_pll *pll = to_clk_pll(hw);
  264. u32 tmp, pll_con0, pll_con1;
  265. const struct samsung_pll_rate_table *rate;
  266. rate = samsung_get_pll_settings(pll, drate);
  267. if (!rate) {
  268. pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
  269. drate, clk_hw_get_name(hw));
  270. return -EINVAL;
  271. }
  272. pll_con0 = readl_relaxed(pll->con_reg);
  273. pll_con1 = readl_relaxed(pll->con_reg + 4);
  274. if (!(samsung_pll36xx_mpk_change(rate, pll_con0, pll_con1))) {
  275. /* If only s change, change just s value only*/
  276. pll_con0 &= ~(PLL36XX_SDIV_MASK << PLL36XX_SDIV_SHIFT);
  277. pll_con0 |= (rate->sdiv << PLL36XX_SDIV_SHIFT);
  278. writel_relaxed(pll_con0, pll->con_reg);
  279. return 0;
  280. }
  281. /* Set PLL lock time. */
  282. writel_relaxed(rate->pdiv * PLL36XX_LOCK_FACTOR, pll->lock_reg);
  283. /* Change PLL PMS values */
  284. pll_con0 &= ~((PLL36XX_MDIV_MASK << PLL36XX_MDIV_SHIFT) |
  285. (PLL36XX_PDIV_MASK << PLL36XX_PDIV_SHIFT) |
  286. (PLL36XX_SDIV_MASK << PLL36XX_SDIV_SHIFT));
  287. pll_con0 |= (rate->mdiv << PLL36XX_MDIV_SHIFT) |
  288. (rate->pdiv << PLL36XX_PDIV_SHIFT) |
  289. (rate->sdiv << PLL36XX_SDIV_SHIFT);
  290. writel_relaxed(pll_con0, pll->con_reg);
  291. pll_con1 &= ~(PLL36XX_KDIV_MASK << PLL36XX_KDIV_SHIFT);
  292. pll_con1 |= rate->kdiv << PLL36XX_KDIV_SHIFT;
  293. writel_relaxed(pll_con1, pll->con_reg + 4);
  294. /* wait_lock_time */
  295. if (pll_con0 & BIT(pll->enable_offs)) {
  296. do {
  297. cpu_relax();
  298. tmp = readl_relaxed(pll->con_reg);
  299. } while (!(tmp & BIT(pll->lock_offs)));
  300. }
  301. return 0;
  302. }
  303. static const struct clk_ops samsung_pll36xx_clk_ops = {
  304. .recalc_rate = samsung_pll36xx_recalc_rate,
  305. .set_rate = samsung_pll36xx_set_rate,
  306. .round_rate = samsung_pll_round_rate,
  307. .enable = samsung_pll3xxx_enable,
  308. .disable = samsung_pll3xxx_disable,
  309. };
  310. static const struct clk_ops samsung_pll36xx_clk_min_ops = {
  311. .recalc_rate = samsung_pll36xx_recalc_rate,
  312. };
  313. /*
  314. * PLL45xx Clock Type
  315. */
  316. #define PLL4502_LOCK_FACTOR 400
  317. #define PLL4508_LOCK_FACTOR 240
  318. #define PLL45XX_MDIV_MASK (0x3FF)
  319. #define PLL45XX_PDIV_MASK (0x3F)
  320. #define PLL45XX_SDIV_MASK (0x7)
  321. #define PLL45XX_AFC_MASK (0x1F)
  322. #define PLL45XX_MDIV_SHIFT (16)
  323. #define PLL45XX_PDIV_SHIFT (8)
  324. #define PLL45XX_SDIV_SHIFT (0)
  325. #define PLL45XX_AFC_SHIFT (0)
  326. #define PLL45XX_ENABLE BIT(31)
  327. #define PLL45XX_LOCKED BIT(29)
  328. static unsigned long samsung_pll45xx_recalc_rate(struct clk_hw *hw,
  329. unsigned long parent_rate)
  330. {
  331. struct samsung_clk_pll *pll = to_clk_pll(hw);
  332. u32 mdiv, pdiv, sdiv, pll_con;
  333. u64 fvco = parent_rate;
  334. pll_con = readl_relaxed(pll->con_reg);
  335. mdiv = (pll_con >> PLL45XX_MDIV_SHIFT) & PLL45XX_MDIV_MASK;
  336. pdiv = (pll_con >> PLL45XX_PDIV_SHIFT) & PLL45XX_PDIV_MASK;
  337. sdiv = (pll_con >> PLL45XX_SDIV_SHIFT) & PLL45XX_SDIV_MASK;
  338. if (pll->type == pll_4508)
  339. sdiv = sdiv - 1;
  340. fvco *= mdiv;
  341. do_div(fvco, (pdiv << sdiv));
  342. return (unsigned long)fvco;
  343. }
  344. static bool samsung_pll45xx_mp_change(u32 pll_con0, u32 pll_con1,
  345. const struct samsung_pll_rate_table *rate)
  346. {
  347. u32 old_mdiv, old_pdiv, old_afc;
  348. old_mdiv = (pll_con0 >> PLL45XX_MDIV_SHIFT) & PLL45XX_MDIV_MASK;
  349. old_pdiv = (pll_con0 >> PLL45XX_PDIV_SHIFT) & PLL45XX_PDIV_MASK;
  350. old_afc = (pll_con1 >> PLL45XX_AFC_SHIFT) & PLL45XX_AFC_MASK;
  351. return (old_mdiv != rate->mdiv || old_pdiv != rate->pdiv
  352. || old_afc != rate->afc);
  353. }
  354. static int samsung_pll45xx_set_rate(struct clk_hw *hw, unsigned long drate,
  355. unsigned long prate)
  356. {
  357. struct samsung_clk_pll *pll = to_clk_pll(hw);
  358. const struct samsung_pll_rate_table *rate;
  359. u32 con0, con1;
  360. ktime_t start;
  361. /* Get required rate settings from table */
  362. rate = samsung_get_pll_settings(pll, drate);
  363. if (!rate) {
  364. pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
  365. drate, clk_hw_get_name(hw));
  366. return -EINVAL;
  367. }
  368. con0 = readl_relaxed(pll->con_reg);
  369. con1 = readl_relaxed(pll->con_reg + 0x4);
  370. if (!(samsung_pll45xx_mp_change(con0, con1, rate))) {
  371. /* If only s change, change just s value only*/
  372. con0 &= ~(PLL45XX_SDIV_MASK << PLL45XX_SDIV_SHIFT);
  373. con0 |= rate->sdiv << PLL45XX_SDIV_SHIFT;
  374. writel_relaxed(con0, pll->con_reg);
  375. return 0;
  376. }
  377. /* Set PLL PMS values. */
  378. con0 &= ~((PLL45XX_MDIV_MASK << PLL45XX_MDIV_SHIFT) |
  379. (PLL45XX_PDIV_MASK << PLL45XX_PDIV_SHIFT) |
  380. (PLL45XX_SDIV_MASK << PLL45XX_SDIV_SHIFT));
  381. con0 |= (rate->mdiv << PLL45XX_MDIV_SHIFT) |
  382. (rate->pdiv << PLL45XX_PDIV_SHIFT) |
  383. (rate->sdiv << PLL45XX_SDIV_SHIFT);
  384. /* Set PLL AFC value. */
  385. con1 = readl_relaxed(pll->con_reg + 0x4);
  386. con1 &= ~(PLL45XX_AFC_MASK << PLL45XX_AFC_SHIFT);
  387. con1 |= (rate->afc << PLL45XX_AFC_SHIFT);
  388. /* Set PLL lock time. */
  389. switch (pll->type) {
  390. case pll_4502:
  391. writel_relaxed(rate->pdiv * PLL4502_LOCK_FACTOR, pll->lock_reg);
  392. break;
  393. case pll_4508:
  394. writel_relaxed(rate->pdiv * PLL4508_LOCK_FACTOR, pll->lock_reg);
  395. break;
  396. default:
  397. break;
  398. }
  399. /* Set new configuration. */
  400. writel_relaxed(con1, pll->con_reg + 0x4);
  401. writel_relaxed(con0, pll->con_reg);
  402. /* Wait for locking. */
  403. start = ktime_get();
  404. while (!(readl_relaxed(pll->con_reg) & PLL45XX_LOCKED)) {
  405. ktime_t delta = ktime_sub(ktime_get(), start);
  406. if (ktime_to_ms(delta) > PLL_TIMEOUT_MS) {
  407. pr_err("%s: could not lock PLL %s\n",
  408. __func__, clk_hw_get_name(hw));
  409. return -EFAULT;
  410. }
  411. cpu_relax();
  412. }
  413. return 0;
  414. }
  415. static const struct clk_ops samsung_pll45xx_clk_ops = {
  416. .recalc_rate = samsung_pll45xx_recalc_rate,
  417. .round_rate = samsung_pll_round_rate,
  418. .set_rate = samsung_pll45xx_set_rate,
  419. };
  420. static const struct clk_ops samsung_pll45xx_clk_min_ops = {
  421. .recalc_rate = samsung_pll45xx_recalc_rate,
  422. };
  423. /*
  424. * PLL46xx Clock Type
  425. */
  426. #define PLL46XX_LOCK_FACTOR 3000
  427. #define PLL46XX_VSEL_MASK (1)
  428. #define PLL46XX_MDIV_MASK (0x1FF)
  429. #define PLL1460X_MDIV_MASK (0x3FF)
  430. #define PLL46XX_PDIV_MASK (0x3F)
  431. #define PLL46XX_SDIV_MASK (0x7)
  432. #define PLL46XX_VSEL_SHIFT (27)
  433. #define PLL46XX_MDIV_SHIFT (16)
  434. #define PLL46XX_PDIV_SHIFT (8)
  435. #define PLL46XX_SDIV_SHIFT (0)
  436. #define PLL46XX_KDIV_MASK (0xFFFF)
  437. #define PLL4650C_KDIV_MASK (0xFFF)
  438. #define PLL46XX_KDIV_SHIFT (0)
  439. #define PLL46XX_MFR_MASK (0x3F)
  440. #define PLL46XX_MRR_MASK (0x1F)
  441. #define PLL46XX_KDIV_SHIFT (0)
  442. #define PLL46XX_MFR_SHIFT (16)
  443. #define PLL46XX_MRR_SHIFT (24)
  444. #define PLL46XX_ENABLE BIT(31)
  445. #define PLL46XX_LOCKED BIT(29)
  446. #define PLL46XX_VSEL BIT(27)
  447. static unsigned long samsung_pll46xx_recalc_rate(struct clk_hw *hw,
  448. unsigned long parent_rate)
  449. {
  450. struct samsung_clk_pll *pll = to_clk_pll(hw);
  451. u32 mdiv, pdiv, sdiv, kdiv, pll_con0, pll_con1, shift;
  452. u64 fvco = parent_rate;
  453. pll_con0 = readl_relaxed(pll->con_reg);
  454. pll_con1 = readl_relaxed(pll->con_reg + 4);
  455. mdiv = (pll_con0 >> PLL46XX_MDIV_SHIFT) & ((pll->type == pll_1460x) ?
  456. PLL1460X_MDIV_MASK : PLL46XX_MDIV_MASK);
  457. pdiv = (pll_con0 >> PLL46XX_PDIV_SHIFT) & PLL46XX_PDIV_MASK;
  458. sdiv = (pll_con0 >> PLL46XX_SDIV_SHIFT) & PLL46XX_SDIV_MASK;
  459. kdiv = pll->type == pll_4650c ? pll_con1 & PLL4650C_KDIV_MASK :
  460. pll_con1 & PLL46XX_KDIV_MASK;
  461. shift = ((pll->type == pll_4600) || (pll->type == pll_1460x)) ? 16 : 10;
  462. fvco *= (mdiv << shift) + kdiv;
  463. do_div(fvco, (pdiv << sdiv));
  464. fvco >>= shift;
  465. return (unsigned long)fvco;
  466. }
  467. static bool samsung_pll46xx_mpk_change(u32 pll_con0, u32 pll_con1,
  468. const struct samsung_pll_rate_table *rate)
  469. {
  470. u32 old_mdiv, old_pdiv, old_kdiv;
  471. old_mdiv = (pll_con0 >> PLL46XX_MDIV_SHIFT) & PLL46XX_MDIV_MASK;
  472. old_pdiv = (pll_con0 >> PLL46XX_PDIV_SHIFT) & PLL46XX_PDIV_MASK;
  473. old_kdiv = (pll_con1 >> PLL46XX_KDIV_SHIFT) & PLL46XX_KDIV_MASK;
  474. return (old_mdiv != rate->mdiv || old_pdiv != rate->pdiv
  475. || old_kdiv != rate->kdiv);
  476. }
  477. static int samsung_pll46xx_set_rate(struct clk_hw *hw, unsigned long drate,
  478. unsigned long prate)
  479. {
  480. struct samsung_clk_pll *pll = to_clk_pll(hw);
  481. const struct samsung_pll_rate_table *rate;
  482. u32 con0, con1, lock;
  483. ktime_t start;
  484. /* Get required rate settings from table */
  485. rate = samsung_get_pll_settings(pll, drate);
  486. if (!rate) {
  487. pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
  488. drate, clk_hw_get_name(hw));
  489. return -EINVAL;
  490. }
  491. con0 = readl_relaxed(pll->con_reg);
  492. con1 = readl_relaxed(pll->con_reg + 0x4);
  493. if (!(samsung_pll46xx_mpk_change(con0, con1, rate))) {
  494. /* If only s change, change just s value only*/
  495. con0 &= ~(PLL46XX_SDIV_MASK << PLL46XX_SDIV_SHIFT);
  496. con0 |= rate->sdiv << PLL46XX_SDIV_SHIFT;
  497. writel_relaxed(con0, pll->con_reg);
  498. return 0;
  499. }
  500. /* Set PLL lock time. */
  501. lock = rate->pdiv * PLL46XX_LOCK_FACTOR;
  502. if (lock > 0xffff)
  503. /* Maximum lock time bitfield is 16-bit. */
  504. lock = 0xffff;
  505. /* Set PLL PMS and VSEL values. */
  506. if (pll->type == pll_1460x) {
  507. con0 &= ~((PLL1460X_MDIV_MASK << PLL46XX_MDIV_SHIFT) |
  508. (PLL46XX_PDIV_MASK << PLL46XX_PDIV_SHIFT) |
  509. (PLL46XX_SDIV_MASK << PLL46XX_SDIV_SHIFT));
  510. } else {
  511. con0 &= ~((PLL46XX_MDIV_MASK << PLL46XX_MDIV_SHIFT) |
  512. (PLL46XX_PDIV_MASK << PLL46XX_PDIV_SHIFT) |
  513. (PLL46XX_SDIV_MASK << PLL46XX_SDIV_SHIFT) |
  514. (PLL46XX_VSEL_MASK << PLL46XX_VSEL_SHIFT));
  515. con0 |= rate->vsel << PLL46XX_VSEL_SHIFT;
  516. }
  517. con0 |= (rate->mdiv << PLL46XX_MDIV_SHIFT) |
  518. (rate->pdiv << PLL46XX_PDIV_SHIFT) |
  519. (rate->sdiv << PLL46XX_SDIV_SHIFT);
  520. /* Set PLL K, MFR and MRR values. */
  521. con1 = readl_relaxed(pll->con_reg + 0x4);
  522. con1 &= ~((PLL46XX_KDIV_MASK << PLL46XX_KDIV_SHIFT) |
  523. (PLL46XX_MFR_MASK << PLL46XX_MFR_SHIFT) |
  524. (PLL46XX_MRR_MASK << PLL46XX_MRR_SHIFT));
  525. con1 |= (rate->kdiv << PLL46XX_KDIV_SHIFT) |
  526. (rate->mfr << PLL46XX_MFR_SHIFT) |
  527. (rate->mrr << PLL46XX_MRR_SHIFT);
  528. /* Write configuration to PLL */
  529. writel_relaxed(lock, pll->lock_reg);
  530. writel_relaxed(con0, pll->con_reg);
  531. writel_relaxed(con1, pll->con_reg + 0x4);
  532. /* Wait for locking. */
  533. start = ktime_get();
  534. while (!(readl_relaxed(pll->con_reg) & PLL46XX_LOCKED)) {
  535. ktime_t delta = ktime_sub(ktime_get(), start);
  536. if (ktime_to_ms(delta) > PLL_TIMEOUT_MS) {
  537. pr_err("%s: could not lock PLL %s\n",
  538. __func__, clk_hw_get_name(hw));
  539. return -EFAULT;
  540. }
  541. cpu_relax();
  542. }
  543. return 0;
  544. }
  545. static const struct clk_ops samsung_pll46xx_clk_ops = {
  546. .recalc_rate = samsung_pll46xx_recalc_rate,
  547. .round_rate = samsung_pll_round_rate,
  548. .set_rate = samsung_pll46xx_set_rate,
  549. };
  550. static const struct clk_ops samsung_pll46xx_clk_min_ops = {
  551. .recalc_rate = samsung_pll46xx_recalc_rate,
  552. };
  553. /*
  554. * PLL6552 Clock Type
  555. */
  556. #define PLL6552_MDIV_MASK 0x3ff
  557. #define PLL6552_PDIV_MASK 0x3f
  558. #define PLL6552_SDIV_MASK 0x7
  559. #define PLL6552_MDIV_SHIFT 16
  560. #define PLL6552_MDIV_SHIFT_2416 14
  561. #define PLL6552_PDIV_SHIFT 8
  562. #define PLL6552_PDIV_SHIFT_2416 5
  563. #define PLL6552_SDIV_SHIFT 0
  564. static unsigned long samsung_pll6552_recalc_rate(struct clk_hw *hw,
  565. unsigned long parent_rate)
  566. {
  567. struct samsung_clk_pll *pll = to_clk_pll(hw);
  568. u32 mdiv, pdiv, sdiv, pll_con;
  569. u64 fvco = parent_rate;
  570. pll_con = readl_relaxed(pll->con_reg);
  571. if (pll->type == pll_6552_s3c2416) {
  572. mdiv = (pll_con >> PLL6552_MDIV_SHIFT_2416) & PLL6552_MDIV_MASK;
  573. pdiv = (pll_con >> PLL6552_PDIV_SHIFT_2416) & PLL6552_PDIV_MASK;
  574. } else {
  575. mdiv = (pll_con >> PLL6552_MDIV_SHIFT) & PLL6552_MDIV_MASK;
  576. pdiv = (pll_con >> PLL6552_PDIV_SHIFT) & PLL6552_PDIV_MASK;
  577. }
  578. sdiv = (pll_con >> PLL6552_SDIV_SHIFT) & PLL6552_SDIV_MASK;
  579. fvco *= mdiv;
  580. do_div(fvco, (pdiv << sdiv));
  581. return (unsigned long)fvco;
  582. }
  583. static const struct clk_ops samsung_pll6552_clk_ops = {
  584. .recalc_rate = samsung_pll6552_recalc_rate,
  585. };
  586. /*
  587. * PLL6553 Clock Type
  588. */
  589. #define PLL6553_MDIV_MASK 0xff
  590. #define PLL6553_PDIV_MASK 0x3f
  591. #define PLL6553_SDIV_MASK 0x7
  592. #define PLL6553_KDIV_MASK 0xffff
  593. #define PLL6553_MDIV_SHIFT 16
  594. #define PLL6553_PDIV_SHIFT 8
  595. #define PLL6553_SDIV_SHIFT 0
  596. #define PLL6553_KDIV_SHIFT 0
  597. static unsigned long samsung_pll6553_recalc_rate(struct clk_hw *hw,
  598. unsigned long parent_rate)
  599. {
  600. struct samsung_clk_pll *pll = to_clk_pll(hw);
  601. u32 mdiv, pdiv, sdiv, kdiv, pll_con0, pll_con1;
  602. u64 fvco = parent_rate;
  603. pll_con0 = readl_relaxed(pll->con_reg);
  604. pll_con1 = readl_relaxed(pll->con_reg + 0x4);
  605. mdiv = (pll_con0 >> PLL6553_MDIV_SHIFT) & PLL6553_MDIV_MASK;
  606. pdiv = (pll_con0 >> PLL6553_PDIV_SHIFT) & PLL6553_PDIV_MASK;
  607. sdiv = (pll_con0 >> PLL6553_SDIV_SHIFT) & PLL6553_SDIV_MASK;
  608. kdiv = (pll_con1 >> PLL6553_KDIV_SHIFT) & PLL6553_KDIV_MASK;
  609. fvco *= (mdiv << 16) + kdiv;
  610. do_div(fvco, (pdiv << sdiv));
  611. fvco >>= 16;
  612. return (unsigned long)fvco;
  613. }
  614. static const struct clk_ops samsung_pll6553_clk_ops = {
  615. .recalc_rate = samsung_pll6553_recalc_rate,
  616. };
  617. /*
  618. * PLL Clock Type of S3C24XX before S3C2443
  619. */
  620. #define PLLS3C2410_MDIV_MASK (0xff)
  621. #define PLLS3C2410_PDIV_MASK (0x1f)
  622. #define PLLS3C2410_SDIV_MASK (0x3)
  623. #define PLLS3C2410_MDIV_SHIFT (12)
  624. #define PLLS3C2410_PDIV_SHIFT (4)
  625. #define PLLS3C2410_SDIV_SHIFT (0)
  626. #define PLLS3C2410_ENABLE_REG_OFFSET 0x10
  627. static unsigned long samsung_s3c2410_pll_recalc_rate(struct clk_hw *hw,
  628. unsigned long parent_rate)
  629. {
  630. struct samsung_clk_pll *pll = to_clk_pll(hw);
  631. u32 pll_con, mdiv, pdiv, sdiv;
  632. u64 fvco = parent_rate;
  633. pll_con = readl_relaxed(pll->con_reg);
  634. mdiv = (pll_con >> PLLS3C2410_MDIV_SHIFT) & PLLS3C2410_MDIV_MASK;
  635. pdiv = (pll_con >> PLLS3C2410_PDIV_SHIFT) & PLLS3C2410_PDIV_MASK;
  636. sdiv = (pll_con >> PLLS3C2410_SDIV_SHIFT) & PLLS3C2410_SDIV_MASK;
  637. fvco *= (mdiv + 8);
  638. do_div(fvco, (pdiv + 2) << sdiv);
  639. return (unsigned int)fvco;
  640. }
  641. static unsigned long samsung_s3c2440_mpll_recalc_rate(struct clk_hw *hw,
  642. unsigned long parent_rate)
  643. {
  644. struct samsung_clk_pll *pll = to_clk_pll(hw);
  645. u32 pll_con, mdiv, pdiv, sdiv;
  646. u64 fvco = parent_rate;
  647. pll_con = readl_relaxed(pll->con_reg);
  648. mdiv = (pll_con >> PLLS3C2410_MDIV_SHIFT) & PLLS3C2410_MDIV_MASK;
  649. pdiv = (pll_con >> PLLS3C2410_PDIV_SHIFT) & PLLS3C2410_PDIV_MASK;
  650. sdiv = (pll_con >> PLLS3C2410_SDIV_SHIFT) & PLLS3C2410_SDIV_MASK;
  651. fvco *= (2 * (mdiv + 8));
  652. do_div(fvco, (pdiv + 2) << sdiv);
  653. return (unsigned int)fvco;
  654. }
  655. static int samsung_s3c2410_pll_set_rate(struct clk_hw *hw, unsigned long drate,
  656. unsigned long prate)
  657. {
  658. struct samsung_clk_pll *pll = to_clk_pll(hw);
  659. const struct samsung_pll_rate_table *rate;
  660. u32 tmp;
  661. /* Get required rate settings from table */
  662. rate = samsung_get_pll_settings(pll, drate);
  663. if (!rate) {
  664. pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
  665. drate, clk_hw_get_name(hw));
  666. return -EINVAL;
  667. }
  668. tmp = readl_relaxed(pll->con_reg);
  669. /* Change PLL PMS values */
  670. tmp &= ~((PLLS3C2410_MDIV_MASK << PLLS3C2410_MDIV_SHIFT) |
  671. (PLLS3C2410_PDIV_MASK << PLLS3C2410_PDIV_SHIFT) |
  672. (PLLS3C2410_SDIV_MASK << PLLS3C2410_SDIV_SHIFT));
  673. tmp |= (rate->mdiv << PLLS3C2410_MDIV_SHIFT) |
  674. (rate->pdiv << PLLS3C2410_PDIV_SHIFT) |
  675. (rate->sdiv << PLLS3C2410_SDIV_SHIFT);
  676. writel_relaxed(tmp, pll->con_reg);
  677. /* Time to settle according to the manual */
  678. udelay(300);
  679. return 0;
  680. }
  681. static int samsung_s3c2410_pll_enable(struct clk_hw *hw, int bit, bool enable)
  682. {
  683. struct samsung_clk_pll *pll = to_clk_pll(hw);
  684. u32 pll_en = readl_relaxed(pll->lock_reg + PLLS3C2410_ENABLE_REG_OFFSET);
  685. u32 pll_en_orig = pll_en;
  686. if (enable)
  687. pll_en &= ~BIT(bit);
  688. else
  689. pll_en |= BIT(bit);
  690. writel_relaxed(pll_en, pll->lock_reg + PLLS3C2410_ENABLE_REG_OFFSET);
  691. /* if we started the UPLL, then allow to settle */
  692. if (enable && (pll_en_orig & BIT(bit)))
  693. udelay(300);
  694. return 0;
  695. }
  696. static int samsung_s3c2410_mpll_enable(struct clk_hw *hw)
  697. {
  698. return samsung_s3c2410_pll_enable(hw, 5, true);
  699. }
  700. static void samsung_s3c2410_mpll_disable(struct clk_hw *hw)
  701. {
  702. samsung_s3c2410_pll_enable(hw, 5, false);
  703. }
  704. static int samsung_s3c2410_upll_enable(struct clk_hw *hw)
  705. {
  706. return samsung_s3c2410_pll_enable(hw, 7, true);
  707. }
  708. static void samsung_s3c2410_upll_disable(struct clk_hw *hw)
  709. {
  710. samsung_s3c2410_pll_enable(hw, 7, false);
  711. }
  712. static const struct clk_ops samsung_s3c2410_mpll_clk_min_ops = {
  713. .recalc_rate = samsung_s3c2410_pll_recalc_rate,
  714. .enable = samsung_s3c2410_mpll_enable,
  715. .disable = samsung_s3c2410_mpll_disable,
  716. };
  717. static const struct clk_ops samsung_s3c2410_upll_clk_min_ops = {
  718. .recalc_rate = samsung_s3c2410_pll_recalc_rate,
  719. .enable = samsung_s3c2410_upll_enable,
  720. .disable = samsung_s3c2410_upll_disable,
  721. };
  722. static const struct clk_ops samsung_s3c2440_mpll_clk_min_ops = {
  723. .recalc_rate = samsung_s3c2440_mpll_recalc_rate,
  724. .enable = samsung_s3c2410_mpll_enable,
  725. .disable = samsung_s3c2410_mpll_disable,
  726. };
  727. static const struct clk_ops samsung_s3c2410_mpll_clk_ops = {
  728. .recalc_rate = samsung_s3c2410_pll_recalc_rate,
  729. .enable = samsung_s3c2410_mpll_enable,
  730. .disable = samsung_s3c2410_mpll_disable,
  731. .round_rate = samsung_pll_round_rate,
  732. .set_rate = samsung_s3c2410_pll_set_rate,
  733. };
  734. static const struct clk_ops samsung_s3c2410_upll_clk_ops = {
  735. .recalc_rate = samsung_s3c2410_pll_recalc_rate,
  736. .enable = samsung_s3c2410_upll_enable,
  737. .disable = samsung_s3c2410_upll_disable,
  738. .round_rate = samsung_pll_round_rate,
  739. .set_rate = samsung_s3c2410_pll_set_rate,
  740. };
  741. static const struct clk_ops samsung_s3c2440_mpll_clk_ops = {
  742. .recalc_rate = samsung_s3c2440_mpll_recalc_rate,
  743. .enable = samsung_s3c2410_mpll_enable,
  744. .disable = samsung_s3c2410_mpll_disable,
  745. .round_rate = samsung_pll_round_rate,
  746. .set_rate = samsung_s3c2410_pll_set_rate,
  747. };
  748. /*
  749. * PLL2550x Clock Type
  750. */
  751. #define PLL2550X_R_MASK (0x1)
  752. #define PLL2550X_P_MASK (0x3F)
  753. #define PLL2550X_M_MASK (0x3FF)
  754. #define PLL2550X_S_MASK (0x7)
  755. #define PLL2550X_R_SHIFT (20)
  756. #define PLL2550X_P_SHIFT (14)
  757. #define PLL2550X_M_SHIFT (4)
  758. #define PLL2550X_S_SHIFT (0)
  759. static unsigned long samsung_pll2550x_recalc_rate(struct clk_hw *hw,
  760. unsigned long parent_rate)
  761. {
  762. struct samsung_clk_pll *pll = to_clk_pll(hw);
  763. u32 r, p, m, s, pll_stat;
  764. u64 fvco = parent_rate;
  765. pll_stat = readl_relaxed(pll->con_reg);
  766. r = (pll_stat >> PLL2550X_R_SHIFT) & PLL2550X_R_MASK;
  767. if (!r)
  768. return 0;
  769. p = (pll_stat >> PLL2550X_P_SHIFT) & PLL2550X_P_MASK;
  770. m = (pll_stat >> PLL2550X_M_SHIFT) & PLL2550X_M_MASK;
  771. s = (pll_stat >> PLL2550X_S_SHIFT) & PLL2550X_S_MASK;
  772. fvco *= m;
  773. do_div(fvco, (p << s));
  774. return (unsigned long)fvco;
  775. }
  776. static const struct clk_ops samsung_pll2550x_clk_ops = {
  777. .recalc_rate = samsung_pll2550x_recalc_rate,
  778. };
  779. /*
  780. * PLL2550xx Clock Type
  781. */
  782. /* Maximum lock time can be 270 * PDIV cycles */
  783. #define PLL2550XX_LOCK_FACTOR 270
  784. #define PLL2550XX_M_MASK 0x3FF
  785. #define PLL2550XX_P_MASK 0x3F
  786. #define PLL2550XX_S_MASK 0x7
  787. #define PLL2550XX_LOCK_STAT_MASK 0x1
  788. #define PLL2550XX_M_SHIFT 9
  789. #define PLL2550XX_P_SHIFT 3
  790. #define PLL2550XX_S_SHIFT 0
  791. #define PLL2550XX_LOCK_STAT_SHIFT 21
  792. static unsigned long samsung_pll2550xx_recalc_rate(struct clk_hw *hw,
  793. unsigned long parent_rate)
  794. {
  795. struct samsung_clk_pll *pll = to_clk_pll(hw);
  796. u32 mdiv, pdiv, sdiv, pll_con;
  797. u64 fvco = parent_rate;
  798. pll_con = readl_relaxed(pll->con_reg);
  799. mdiv = (pll_con >> PLL2550XX_M_SHIFT) & PLL2550XX_M_MASK;
  800. pdiv = (pll_con >> PLL2550XX_P_SHIFT) & PLL2550XX_P_MASK;
  801. sdiv = (pll_con >> PLL2550XX_S_SHIFT) & PLL2550XX_S_MASK;
  802. fvco *= mdiv;
  803. do_div(fvco, (pdiv << sdiv));
  804. return (unsigned long)fvco;
  805. }
  806. static inline bool samsung_pll2550xx_mp_change(u32 mdiv, u32 pdiv, u32 pll_con)
  807. {
  808. u32 old_mdiv, old_pdiv;
  809. old_mdiv = (pll_con >> PLL2550XX_M_SHIFT) & PLL2550XX_M_MASK;
  810. old_pdiv = (pll_con >> PLL2550XX_P_SHIFT) & PLL2550XX_P_MASK;
  811. return mdiv != old_mdiv || pdiv != old_pdiv;
  812. }
  813. static int samsung_pll2550xx_set_rate(struct clk_hw *hw, unsigned long drate,
  814. unsigned long prate)
  815. {
  816. struct samsung_clk_pll *pll = to_clk_pll(hw);
  817. const struct samsung_pll_rate_table *rate;
  818. u32 tmp;
  819. /* Get required rate settings from table */
  820. rate = samsung_get_pll_settings(pll, drate);
  821. if (!rate) {
  822. pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
  823. drate, clk_hw_get_name(hw));
  824. return -EINVAL;
  825. }
  826. tmp = readl_relaxed(pll->con_reg);
  827. if (!(samsung_pll2550xx_mp_change(rate->mdiv, rate->pdiv, tmp))) {
  828. /* If only s change, change just s value only*/
  829. tmp &= ~(PLL2550XX_S_MASK << PLL2550XX_S_SHIFT);
  830. tmp |= rate->sdiv << PLL2550XX_S_SHIFT;
  831. writel_relaxed(tmp, pll->con_reg);
  832. return 0;
  833. }
  834. /* Set PLL lock time. */
  835. writel_relaxed(rate->pdiv * PLL2550XX_LOCK_FACTOR, pll->lock_reg);
  836. /* Change PLL PMS values */
  837. tmp &= ~((PLL2550XX_M_MASK << PLL2550XX_M_SHIFT) |
  838. (PLL2550XX_P_MASK << PLL2550XX_P_SHIFT) |
  839. (PLL2550XX_S_MASK << PLL2550XX_S_SHIFT));
  840. tmp |= (rate->mdiv << PLL2550XX_M_SHIFT) |
  841. (rate->pdiv << PLL2550XX_P_SHIFT) |
  842. (rate->sdiv << PLL2550XX_S_SHIFT);
  843. writel_relaxed(tmp, pll->con_reg);
  844. /* wait_lock_time */
  845. do {
  846. cpu_relax();
  847. tmp = readl_relaxed(pll->con_reg);
  848. } while (!(tmp & (PLL2550XX_LOCK_STAT_MASK
  849. << PLL2550XX_LOCK_STAT_SHIFT)));
  850. return 0;
  851. }
  852. static const struct clk_ops samsung_pll2550xx_clk_ops = {
  853. .recalc_rate = samsung_pll2550xx_recalc_rate,
  854. .round_rate = samsung_pll_round_rate,
  855. .set_rate = samsung_pll2550xx_set_rate,
  856. };
  857. static const struct clk_ops samsung_pll2550xx_clk_min_ops = {
  858. .recalc_rate = samsung_pll2550xx_recalc_rate,
  859. };
  860. /*
  861. * PLL2650x Clock Type
  862. */
  863. /* Maximum lock time can be 3000 * PDIV cycles */
  864. #define PLL2650X_LOCK_FACTOR 3000
  865. #define PLL2650X_M_MASK 0x1ff
  866. #define PLL2650X_P_MASK 0x3f
  867. #define PLL2650X_S_MASK 0x7
  868. #define PLL2650X_K_MASK 0xffff
  869. #define PLL2650X_LOCK_STAT_MASK 0x1
  870. #define PLL2650X_M_SHIFT 16
  871. #define PLL2650X_P_SHIFT 8
  872. #define PLL2650X_S_SHIFT 0
  873. #define PLL2650X_K_SHIFT 0
  874. #define PLL2650X_LOCK_STAT_SHIFT 29
  875. #define PLL2650X_PLL_ENABLE_SHIFT 31
  876. static unsigned long samsung_pll2650x_recalc_rate(struct clk_hw *hw,
  877. unsigned long parent_rate)
  878. {
  879. struct samsung_clk_pll *pll = to_clk_pll(hw);
  880. u64 fout = parent_rate;
  881. u32 mdiv, pdiv, sdiv, pll_con0, pll_con1;
  882. s16 kdiv;
  883. pll_con0 = readl_relaxed(pll->con_reg);
  884. mdiv = (pll_con0 >> PLL2650X_M_SHIFT) & PLL2650X_M_MASK;
  885. pdiv = (pll_con0 >> PLL2650X_P_SHIFT) & PLL2650X_P_MASK;
  886. sdiv = (pll_con0 >> PLL2650X_S_SHIFT) & PLL2650X_S_MASK;
  887. pll_con1 = readl_relaxed(pll->con_reg + 4);
  888. kdiv = (s16)((pll_con1 >> PLL2650X_K_SHIFT) & PLL2650X_K_MASK);
  889. fout *= (mdiv << 16) + kdiv;
  890. do_div(fout, (pdiv << sdiv));
  891. fout >>= 16;
  892. return (unsigned long)fout;
  893. }
  894. static int samsung_pll2650x_set_rate(struct clk_hw *hw, unsigned long drate,
  895. unsigned long prate)
  896. {
  897. struct samsung_clk_pll *pll = to_clk_pll(hw);
  898. const struct samsung_pll_rate_table *rate;
  899. u32 con0, con1;
  900. /* Get required rate settings from table */
  901. rate = samsung_get_pll_settings(pll, drate);
  902. if (!rate) {
  903. pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
  904. drate, clk_hw_get_name(hw));
  905. return -EINVAL;
  906. }
  907. con0 = readl_relaxed(pll->con_reg);
  908. con1 = readl_relaxed(pll->con_reg + 4);
  909. /* Set PLL lock time. */
  910. writel_relaxed(rate->pdiv * PLL2650X_LOCK_FACTOR, pll->lock_reg);
  911. /* Change PLL PMS values */
  912. con0 &= ~((PLL2650X_M_MASK << PLL2650X_M_SHIFT) |
  913. (PLL2650X_P_MASK << PLL2650X_P_SHIFT) |
  914. (PLL2650X_S_MASK << PLL2650X_S_SHIFT));
  915. con0 |= (rate->mdiv << PLL2650X_M_SHIFT) |
  916. (rate->pdiv << PLL2650X_P_SHIFT) |
  917. (rate->sdiv << PLL2650X_S_SHIFT);
  918. con0 |= (1 << PLL2650X_PLL_ENABLE_SHIFT);
  919. writel_relaxed(con0, pll->con_reg);
  920. con1 &= ~(PLL2650X_K_MASK << PLL2650X_K_SHIFT);
  921. con1 |= ((rate->kdiv & PLL2650X_K_MASK) << PLL2650X_K_SHIFT);
  922. writel_relaxed(con1, pll->con_reg + 4);
  923. do {
  924. cpu_relax();
  925. con0 = readl_relaxed(pll->con_reg);
  926. } while (!(con0 & (PLL2650X_LOCK_STAT_MASK
  927. << PLL2650X_LOCK_STAT_SHIFT)));
  928. return 0;
  929. }
  930. static const struct clk_ops samsung_pll2650x_clk_ops = {
  931. .recalc_rate = samsung_pll2650x_recalc_rate,
  932. .round_rate = samsung_pll_round_rate,
  933. .set_rate = samsung_pll2650x_set_rate,
  934. };
  935. static const struct clk_ops samsung_pll2650x_clk_min_ops = {
  936. .recalc_rate = samsung_pll2650x_recalc_rate,
  937. };
  938. /*
  939. * PLL2650XX Clock Type
  940. */
  941. /* Maximum lock time can be 3000 * PDIV cycles */
  942. #define PLL2650XX_LOCK_FACTOR 3000
  943. #define PLL2650XX_MDIV_SHIFT 9
  944. #define PLL2650XX_PDIV_SHIFT 3
  945. #define PLL2650XX_SDIV_SHIFT 0
  946. #define PLL2650XX_KDIV_SHIFT 0
  947. #define PLL2650XX_MDIV_MASK 0x1ff
  948. #define PLL2650XX_PDIV_MASK 0x3f
  949. #define PLL2650XX_SDIV_MASK 0x7
  950. #define PLL2650XX_KDIV_MASK 0xffff
  951. #define PLL2650XX_PLL_ENABLE_SHIFT 23
  952. #define PLL2650XX_PLL_LOCKTIME_SHIFT 21
  953. #define PLL2650XX_PLL_FOUTMASK_SHIFT 31
  954. static unsigned long samsung_pll2650xx_recalc_rate(struct clk_hw *hw,
  955. unsigned long parent_rate)
  956. {
  957. struct samsung_clk_pll *pll = to_clk_pll(hw);
  958. u32 mdiv, pdiv, sdiv, pll_con0, pll_con2;
  959. s16 kdiv;
  960. u64 fvco = parent_rate;
  961. pll_con0 = readl_relaxed(pll->con_reg);
  962. pll_con2 = readl_relaxed(pll->con_reg + 8);
  963. mdiv = (pll_con0 >> PLL2650XX_MDIV_SHIFT) & PLL2650XX_MDIV_MASK;
  964. pdiv = (pll_con0 >> PLL2650XX_PDIV_SHIFT) & PLL2650XX_PDIV_MASK;
  965. sdiv = (pll_con0 >> PLL2650XX_SDIV_SHIFT) & PLL2650XX_SDIV_MASK;
  966. kdiv = (s16)(pll_con2 & PLL2650XX_KDIV_MASK);
  967. fvco *= (mdiv << 16) + kdiv;
  968. do_div(fvco, (pdiv << sdiv));
  969. fvco >>= 16;
  970. return (unsigned long)fvco;
  971. }
  972. static int samsung_pll2650xx_set_rate(struct clk_hw *hw, unsigned long drate,
  973. unsigned long parent_rate)
  974. {
  975. struct samsung_clk_pll *pll = to_clk_pll(hw);
  976. u32 tmp, pll_con0, pll_con2;
  977. const struct samsung_pll_rate_table *rate;
  978. rate = samsung_get_pll_settings(pll, drate);
  979. if (!rate) {
  980. pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
  981. drate, clk_hw_get_name(hw));
  982. return -EINVAL;
  983. }
  984. pll_con0 = readl_relaxed(pll->con_reg);
  985. pll_con2 = readl_relaxed(pll->con_reg + 8);
  986. /* Change PLL PMS values */
  987. pll_con0 &= ~(PLL2650XX_MDIV_MASK << PLL2650XX_MDIV_SHIFT |
  988. PLL2650XX_PDIV_MASK << PLL2650XX_PDIV_SHIFT |
  989. PLL2650XX_SDIV_MASK << PLL2650XX_SDIV_SHIFT);
  990. pll_con0 |= rate->mdiv << PLL2650XX_MDIV_SHIFT;
  991. pll_con0 |= rate->pdiv << PLL2650XX_PDIV_SHIFT;
  992. pll_con0 |= rate->sdiv << PLL2650XX_SDIV_SHIFT;
  993. pll_con0 |= 1 << PLL2650XX_PLL_ENABLE_SHIFT;
  994. pll_con0 |= 1 << PLL2650XX_PLL_FOUTMASK_SHIFT;
  995. pll_con2 &= ~(PLL2650XX_KDIV_MASK << PLL2650XX_KDIV_SHIFT);
  996. pll_con2 |= ((~(rate->kdiv) + 1) & PLL2650XX_KDIV_MASK)
  997. << PLL2650XX_KDIV_SHIFT;
  998. /* Set PLL lock time. */
  999. writel_relaxed(PLL2650XX_LOCK_FACTOR * rate->pdiv, pll->lock_reg);
  1000. writel_relaxed(pll_con0, pll->con_reg);
  1001. writel_relaxed(pll_con2, pll->con_reg + 8);
  1002. do {
  1003. tmp = readl_relaxed(pll->con_reg);
  1004. } while (!(tmp & (0x1 << PLL2650XX_PLL_LOCKTIME_SHIFT)));
  1005. return 0;
  1006. }
  1007. static const struct clk_ops samsung_pll2650xx_clk_ops = {
  1008. .recalc_rate = samsung_pll2650xx_recalc_rate,
  1009. .set_rate = samsung_pll2650xx_set_rate,
  1010. .round_rate = samsung_pll_round_rate,
  1011. };
  1012. static const struct clk_ops samsung_pll2650xx_clk_min_ops = {
  1013. .recalc_rate = samsung_pll2650xx_recalc_rate,
  1014. };
  1015. static void __init _samsung_clk_register_pll(struct samsung_clk_provider *ctx,
  1016. const struct samsung_pll_clock *pll_clk,
  1017. void __iomem *base)
  1018. {
  1019. struct samsung_clk_pll *pll;
  1020. struct clk_init_data init;
  1021. int ret, len;
  1022. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  1023. if (!pll) {
  1024. pr_err("%s: could not allocate pll clk %s\n",
  1025. __func__, pll_clk->name);
  1026. return;
  1027. }
  1028. init.name = pll_clk->name;
  1029. init.flags = pll_clk->flags;
  1030. init.parent_names = &pll_clk->parent_name;
  1031. init.num_parents = 1;
  1032. if (pll_clk->rate_table) {
  1033. /* find count of rates in rate_table */
  1034. for (len = 0; pll_clk->rate_table[len].rate != 0; )
  1035. len++;
  1036. pll->rate_count = len;
  1037. pll->rate_table = kmemdup(pll_clk->rate_table,
  1038. pll->rate_count *
  1039. sizeof(struct samsung_pll_rate_table),
  1040. GFP_KERNEL);
  1041. WARN(!pll->rate_table,
  1042. "%s: could not allocate rate table for %s\n",
  1043. __func__, pll_clk->name);
  1044. }
  1045. switch (pll_clk->type) {
  1046. case pll_2126:
  1047. init.ops = &samsung_pll2126_clk_ops;
  1048. break;
  1049. case pll_3000:
  1050. init.ops = &samsung_pll3000_clk_ops;
  1051. break;
  1052. /* clk_ops for 35xx and 2550 are similar */
  1053. case pll_35xx:
  1054. case pll_2550:
  1055. case pll_1450x:
  1056. case pll_1451x:
  1057. case pll_1452x:
  1058. pll->enable_offs = PLL35XX_ENABLE_SHIFT;
  1059. pll->lock_offs = PLL35XX_LOCK_STAT_SHIFT;
  1060. if (!pll->rate_table)
  1061. init.ops = &samsung_pll35xx_clk_min_ops;
  1062. else
  1063. init.ops = &samsung_pll35xx_clk_ops;
  1064. break;
  1065. case pll_4500:
  1066. init.ops = &samsung_pll45xx_clk_min_ops;
  1067. break;
  1068. case pll_4502:
  1069. case pll_4508:
  1070. if (!pll->rate_table)
  1071. init.ops = &samsung_pll45xx_clk_min_ops;
  1072. else
  1073. init.ops = &samsung_pll45xx_clk_ops;
  1074. break;
  1075. /* clk_ops for 36xx and 2650 are similar */
  1076. case pll_36xx:
  1077. case pll_2650:
  1078. pll->enable_offs = PLL36XX_ENABLE_SHIFT;
  1079. pll->lock_offs = PLL36XX_LOCK_STAT_SHIFT;
  1080. if (!pll->rate_table)
  1081. init.ops = &samsung_pll36xx_clk_min_ops;
  1082. else
  1083. init.ops = &samsung_pll36xx_clk_ops;
  1084. break;
  1085. case pll_6552:
  1086. case pll_6552_s3c2416:
  1087. init.ops = &samsung_pll6552_clk_ops;
  1088. break;
  1089. case pll_6553:
  1090. init.ops = &samsung_pll6553_clk_ops;
  1091. break;
  1092. case pll_4600:
  1093. case pll_4650:
  1094. case pll_4650c:
  1095. case pll_1460x:
  1096. if (!pll->rate_table)
  1097. init.ops = &samsung_pll46xx_clk_min_ops;
  1098. else
  1099. init.ops = &samsung_pll46xx_clk_ops;
  1100. break;
  1101. case pll_s3c2410_mpll:
  1102. if (!pll->rate_table)
  1103. init.ops = &samsung_s3c2410_mpll_clk_min_ops;
  1104. else
  1105. init.ops = &samsung_s3c2410_mpll_clk_ops;
  1106. break;
  1107. case pll_s3c2410_upll:
  1108. if (!pll->rate_table)
  1109. init.ops = &samsung_s3c2410_upll_clk_min_ops;
  1110. else
  1111. init.ops = &samsung_s3c2410_upll_clk_ops;
  1112. break;
  1113. case pll_s3c2440_mpll:
  1114. if (!pll->rate_table)
  1115. init.ops = &samsung_s3c2440_mpll_clk_min_ops;
  1116. else
  1117. init.ops = &samsung_s3c2440_mpll_clk_ops;
  1118. break;
  1119. case pll_2550x:
  1120. init.ops = &samsung_pll2550x_clk_ops;
  1121. break;
  1122. case pll_2550xx:
  1123. if (!pll->rate_table)
  1124. init.ops = &samsung_pll2550xx_clk_min_ops;
  1125. else
  1126. init.ops = &samsung_pll2550xx_clk_ops;
  1127. break;
  1128. case pll_2650x:
  1129. if (!pll->rate_table)
  1130. init.ops = &samsung_pll2650x_clk_min_ops;
  1131. else
  1132. init.ops = &samsung_pll2650x_clk_ops;
  1133. break;
  1134. case pll_2650xx:
  1135. if (!pll->rate_table)
  1136. init.ops = &samsung_pll2650xx_clk_min_ops;
  1137. else
  1138. init.ops = &samsung_pll2650xx_clk_ops;
  1139. break;
  1140. default:
  1141. pr_warn("%s: Unknown pll type for pll clk %s\n",
  1142. __func__, pll_clk->name);
  1143. }
  1144. pll->hw.init = &init;
  1145. pll->type = pll_clk->type;
  1146. pll->lock_reg = base + pll_clk->lock_offset;
  1147. pll->con_reg = base + pll_clk->con_offset;
  1148. ret = clk_hw_register(NULL, &pll->hw);
  1149. if (ret) {
  1150. pr_err("%s: failed to register pll clock %s : %d\n",
  1151. __func__, pll_clk->name, ret);
  1152. kfree(pll);
  1153. return;
  1154. }
  1155. samsung_clk_add_lookup(ctx, &pll->hw, pll_clk->id);
  1156. if (!pll_clk->alias)
  1157. return;
  1158. ret = clk_hw_register_clkdev(&pll->hw, pll_clk->alias,
  1159. pll_clk->dev_name);
  1160. if (ret)
  1161. pr_err("%s: failed to register lookup for %s : %d",
  1162. __func__, pll_clk->name, ret);
  1163. }
  1164. void __init samsung_clk_register_pll(struct samsung_clk_provider *ctx,
  1165. const struct samsung_pll_clock *pll_list,
  1166. unsigned int nr_pll, void __iomem *base)
  1167. {
  1168. int cnt;
  1169. for (cnt = 0; cnt < nr_pll; cnt++)
  1170. _samsung_clk_register_pll(ctx, &pll_list[cnt], base);
  1171. }