pll.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PLL clock driver for TI Davinci SoCs
  4. *
  5. * Copyright (C) 2018 David Lechner <david@lechnology.com>
  6. *
  7. * Based on arch/arm/mach-davinci/clock.c
  8. * Copyright (C) 2006-2007 Texas Instruments.
  9. * Copyright (C) 2008-2009 Deep Root Systems, LLC
  10. */
  11. #include <linux/clk-provider.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/notifier.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_data/clk-davinci-pll.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/regmap.h>
  25. #include <linux/slab.h>
  26. #include <linux/types.h>
  27. #include "pll.h"
  28. #define MAX_NAME_SIZE 20
  29. #define OSCIN_CLK_NAME "oscin"
  30. #define REVID 0x000
  31. #define PLLCTL 0x100
  32. #define OCSEL 0x104
  33. #define PLLSECCTL 0x108
  34. #define PLLM 0x110
  35. #define PREDIV 0x114
  36. #define PLLDIV1 0x118
  37. #define PLLDIV2 0x11c
  38. #define PLLDIV3 0x120
  39. #define OSCDIV 0x124
  40. #define POSTDIV 0x128
  41. #define BPDIV 0x12c
  42. #define PLLCMD 0x138
  43. #define PLLSTAT 0x13c
  44. #define ALNCTL 0x140
  45. #define DCHANGE 0x144
  46. #define CKEN 0x148
  47. #define CKSTAT 0x14c
  48. #define SYSTAT 0x150
  49. #define PLLDIV4 0x160
  50. #define PLLDIV5 0x164
  51. #define PLLDIV6 0x168
  52. #define PLLDIV7 0x16c
  53. #define PLLDIV8 0x170
  54. #define PLLDIV9 0x174
  55. #define PLLCTL_PLLEN BIT(0)
  56. #define PLLCTL_PLLPWRDN BIT(1)
  57. #define PLLCTL_PLLRST BIT(3)
  58. #define PLLCTL_PLLDIS BIT(4)
  59. #define PLLCTL_PLLENSRC BIT(5)
  60. #define PLLCTL_CLKMODE BIT(8)
  61. /* shared by most *DIV registers */
  62. #define DIV_RATIO_SHIFT 0
  63. #define DIV_RATIO_WIDTH 5
  64. #define DIV_ENABLE_SHIFT 15
  65. #define PLLCMD_GOSET BIT(0)
  66. #define PLLSTAT_GOSTAT BIT(0)
  67. #define CKEN_OBSCLK_SHIFT 1
  68. #define CKEN_AUXEN_SHIFT 0
  69. /*
  70. * OMAP-L138 system reference guide recommends a wait for 4 OSCIN/CLKIN
  71. * cycles to ensure that the PLLC has switched to bypass mode. Delay of 1us
  72. * ensures we are good for all > 4MHz OSCIN/CLKIN inputs. Typically the input
  73. * is ~25MHz. Units are micro seconds.
  74. */
  75. #define PLL_BYPASS_TIME 1
  76. /* From OMAP-L138 datasheet table 6-4. Units are micro seconds */
  77. #define PLL_RESET_TIME 1
  78. /*
  79. * From OMAP-L138 datasheet table 6-4; assuming prediv = 1, sqrt(pllm) = 4
  80. * Units are micro seconds.
  81. */
  82. #define PLL_LOCK_TIME 20
  83. /**
  84. * struct davinci_pll_clk - Main PLL clock (aka PLLOUT)
  85. * @hw: clk_hw for the pll
  86. * @base: Base memory address
  87. * @pllm_min: The minimum allowable PLLM[PLLM] value
  88. * @pllm_max: The maxiumum allowable PLLM[PLLM] value
  89. * @pllm_mask: Bitmask for PLLM[PLLM] value
  90. */
  91. struct davinci_pll_clk {
  92. struct clk_hw hw;
  93. void __iomem *base;
  94. u32 pllm_min;
  95. u32 pllm_max;
  96. u32 pllm_mask;
  97. };
  98. #define to_davinci_pll_clk(_hw) \
  99. container_of((_hw), struct davinci_pll_clk, hw)
  100. static unsigned long davinci_pll_recalc_rate(struct clk_hw *hw,
  101. unsigned long parent_rate)
  102. {
  103. struct davinci_pll_clk *pll = to_davinci_pll_clk(hw);
  104. unsigned long rate = parent_rate;
  105. u32 mult;
  106. mult = readl(pll->base + PLLM) & pll->pllm_mask;
  107. rate *= mult + 1;
  108. return rate;
  109. }
  110. static int davinci_pll_determine_rate(struct clk_hw *hw,
  111. struct clk_rate_request *req)
  112. {
  113. struct davinci_pll_clk *pll = to_davinci_pll_clk(hw);
  114. struct clk_hw *parent = req->best_parent_hw;
  115. unsigned long parent_rate = req->best_parent_rate;
  116. unsigned long rate = req->rate;
  117. unsigned long best_rate, r;
  118. u32 mult;
  119. /* there is a limited range of valid outputs (see datasheet) */
  120. if (rate < req->min_rate)
  121. return -EINVAL;
  122. rate = min(rate, req->max_rate);
  123. mult = rate / parent_rate;
  124. best_rate = parent_rate * mult;
  125. /* easy case when there is no PREDIV */
  126. if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
  127. if (best_rate < req->min_rate)
  128. return -EINVAL;
  129. if (mult < pll->pllm_min || mult > pll->pllm_max)
  130. return -EINVAL;
  131. req->rate = best_rate;
  132. return 0;
  133. }
  134. /* see if the PREDIV clock can help us */
  135. best_rate = 0;
  136. for (mult = pll->pllm_min; mult <= pll->pllm_max; mult++) {
  137. parent_rate = clk_hw_round_rate(parent, rate / mult);
  138. r = parent_rate * mult;
  139. if (r < req->min_rate)
  140. continue;
  141. if (r > rate || r > req->max_rate)
  142. break;
  143. if (r > best_rate) {
  144. best_rate = r;
  145. req->rate = best_rate;
  146. req->best_parent_rate = parent_rate;
  147. if (best_rate == rate)
  148. break;
  149. }
  150. }
  151. return 0;
  152. }
  153. static int davinci_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  154. unsigned long parent_rate)
  155. {
  156. struct davinci_pll_clk *pll = to_davinci_pll_clk(hw);
  157. u32 mult;
  158. mult = rate / parent_rate;
  159. writel(mult - 1, pll->base + PLLM);
  160. return 0;
  161. }
  162. #ifdef CONFIG_DEBUG_FS
  163. static int davinci_pll_debug_init(struct clk_hw *hw, struct dentry *dentry);
  164. #else
  165. #define davinci_pll_debug_init NULL
  166. #endif
  167. static const struct clk_ops davinci_pll_ops = {
  168. .recalc_rate = davinci_pll_recalc_rate,
  169. .determine_rate = davinci_pll_determine_rate,
  170. .set_rate = davinci_pll_set_rate,
  171. .debug_init = davinci_pll_debug_init,
  172. };
  173. /* PLLM works differently on DM365 */
  174. static unsigned long dm365_pll_recalc_rate(struct clk_hw *hw,
  175. unsigned long parent_rate)
  176. {
  177. struct davinci_pll_clk *pll = to_davinci_pll_clk(hw);
  178. unsigned long rate = parent_rate;
  179. u32 mult;
  180. mult = readl(pll->base + PLLM) & pll->pllm_mask;
  181. rate *= mult * 2;
  182. return rate;
  183. }
  184. static const struct clk_ops dm365_pll_ops = {
  185. .recalc_rate = dm365_pll_recalc_rate,
  186. .debug_init = davinci_pll_debug_init,
  187. };
  188. /**
  189. * davinci_pll_div_register - common *DIV clock implementation
  190. * @name: the clock name
  191. * @parent_name: the parent clock name
  192. * @reg: the *DIV register
  193. * @fixed: if true, the divider is a fixed value
  194. * @flags: bitmap of CLK_* flags from clock-provider.h
  195. */
  196. static struct clk *davinci_pll_div_register(struct device *dev,
  197. const char *name,
  198. const char *parent_name,
  199. void __iomem *reg,
  200. bool fixed, u32 flags)
  201. {
  202. const char * const *parent_names = parent_name ? &parent_name : NULL;
  203. int num_parents = parent_name ? 1 : 0;
  204. const struct clk_ops *divider_ops = &clk_divider_ops;
  205. struct clk_gate *gate;
  206. struct clk_divider *divider;
  207. gate = devm_kzalloc(dev, sizeof(*gate), GFP_KERNEL);
  208. if (!gate)
  209. return ERR_PTR(-ENOMEM);
  210. gate->reg = reg;
  211. gate->bit_idx = DIV_ENABLE_SHIFT;
  212. divider = devm_kzalloc(dev, sizeof(*divider), GFP_KERNEL);
  213. if (!divider)
  214. return ERR_PTR(-ENOMEM);
  215. divider->reg = reg;
  216. divider->shift = DIV_RATIO_SHIFT;
  217. divider->width = DIV_RATIO_WIDTH;
  218. if (fixed) {
  219. divider->flags |= CLK_DIVIDER_READ_ONLY;
  220. divider_ops = &clk_divider_ro_ops;
  221. }
  222. return clk_register_composite(dev, name, parent_names, num_parents,
  223. NULL, NULL, &divider->hw, divider_ops,
  224. &gate->hw, &clk_gate_ops, flags);
  225. }
  226. struct davinci_pllen_clk {
  227. struct clk_hw hw;
  228. void __iomem *base;
  229. };
  230. #define to_davinci_pllen_clk(_hw) \
  231. container_of((_hw), struct davinci_pllen_clk, hw)
  232. static const struct clk_ops davinci_pllen_ops = {
  233. /* this clocks just uses the clock notification feature */
  234. };
  235. /*
  236. * The PLL has to be switched into bypass mode while we are chaning the rate,
  237. * so we do that on the PLLEN clock since it is the end of the line. This will
  238. * switch to bypass before any of the parent clocks (PREDIV, PLL, POSTDIV) are
  239. * changed and will switch back to the PLL after the changes have been made.
  240. */
  241. static int davinci_pllen_rate_change(struct notifier_block *nb,
  242. unsigned long flags, void *data)
  243. {
  244. struct clk_notifier_data *cnd = data;
  245. struct clk_hw *hw = __clk_get_hw(cnd->clk);
  246. struct davinci_pllen_clk *pll = to_davinci_pllen_clk(hw);
  247. u32 ctrl;
  248. ctrl = readl(pll->base + PLLCTL);
  249. if (flags == PRE_RATE_CHANGE) {
  250. /* Switch the PLL to bypass mode */
  251. ctrl &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN);
  252. writel(ctrl, pll->base + PLLCTL);
  253. udelay(PLL_BYPASS_TIME);
  254. /* Reset and enable PLL */
  255. ctrl &= ~(PLLCTL_PLLRST | PLLCTL_PLLDIS);
  256. writel(ctrl, pll->base + PLLCTL);
  257. } else {
  258. udelay(PLL_RESET_TIME);
  259. /* Bring PLL out of reset */
  260. ctrl |= PLLCTL_PLLRST;
  261. writel(ctrl, pll->base + PLLCTL);
  262. udelay(PLL_LOCK_TIME);
  263. /* Remove PLL from bypass mode */
  264. ctrl |= PLLCTL_PLLEN;
  265. writel(ctrl, pll->base + PLLCTL);
  266. }
  267. return NOTIFY_OK;
  268. }
  269. static struct davinci_pll_platform_data *davinci_pll_get_pdata(struct device *dev)
  270. {
  271. struct davinci_pll_platform_data *pdata = dev_get_platdata(dev);
  272. /*
  273. * Platform data is optional, so allocate a new struct if one was not
  274. * provided. For device tree, this will always be the case.
  275. */
  276. if (!pdata)
  277. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  278. if (!pdata)
  279. return NULL;
  280. /* for device tree, we need to fill in the struct */
  281. if (dev->of_node)
  282. pdata->cfgchip =
  283. syscon_regmap_lookup_by_compatible("ti,da830-cfgchip");
  284. return pdata;
  285. }
  286. static struct notifier_block davinci_pllen_notifier = {
  287. .notifier_call = davinci_pllen_rate_change,
  288. };
  289. /**
  290. * davinci_pll_clk_register - Register a PLL clock
  291. * @info: The device-specific clock info
  292. * @parent_name: The parent clock name
  293. * @base: The PLL's memory region
  294. *
  295. * This creates a series of clocks that represent the PLL.
  296. *
  297. * OSCIN > [PREDIV >] PLL > [POSTDIV >] PLLEN
  298. *
  299. * - OSCIN is the parent clock (on secondary PLL, may come from primary PLL)
  300. * - PREDIV and POSTDIV are optional (depends on the PLL controller)
  301. * - PLL is the PLL output (aka PLLOUT)
  302. * - PLLEN is the bypass multiplexer
  303. *
  304. * Returns: The PLLOUT clock or a negative error code.
  305. */
  306. struct clk *davinci_pll_clk_register(struct device *dev,
  307. const struct davinci_pll_clk_info *info,
  308. const char *parent_name,
  309. void __iomem *base)
  310. {
  311. struct davinci_pll_platform_data *pdata;
  312. char prediv_name[MAX_NAME_SIZE];
  313. char pllout_name[MAX_NAME_SIZE];
  314. char postdiv_name[MAX_NAME_SIZE];
  315. char pllen_name[MAX_NAME_SIZE];
  316. struct clk_init_data init;
  317. struct davinci_pll_clk *pllout;
  318. struct davinci_pllen_clk *pllen;
  319. struct clk *pllout_clk, *clk;
  320. pdata = davinci_pll_get_pdata(dev);
  321. if (!pdata)
  322. return ERR_PTR(-ENOMEM);
  323. if (info->flags & PLL_HAS_CLKMODE) {
  324. /*
  325. * If a PLL has PLLCTL[CLKMODE], then it is the primary PLL.
  326. * We register a clock named "oscin" that serves as the internal
  327. * "input clock" domain shared by both PLLs (if there are 2)
  328. * and will be the parent clock to the AUXCLK, SYSCLKBP and
  329. * OBSCLK domains. NB: The various TRMs use "OSCIN" to mean
  330. * a number of different things. In this driver we use it to
  331. * mean the signal after the PLLCTL[CLKMODE] switch.
  332. */
  333. clk = clk_register_fixed_factor(dev, OSCIN_CLK_NAME,
  334. parent_name, 0, 1, 1);
  335. if (IS_ERR(clk))
  336. return clk;
  337. parent_name = OSCIN_CLK_NAME;
  338. }
  339. if (info->flags & PLL_HAS_PREDIV) {
  340. bool fixed = info->flags & PLL_PREDIV_FIXED_DIV;
  341. u32 flags = 0;
  342. snprintf(prediv_name, MAX_NAME_SIZE, "%s_prediv", info->name);
  343. if (info->flags & PLL_PREDIV_ALWAYS_ENABLED)
  344. flags |= CLK_IS_CRITICAL;
  345. /* Some? DM355 chips don't correctly report the PREDIV value */
  346. if (info->flags & PLL_PREDIV_FIXED8)
  347. clk = clk_register_fixed_factor(dev, prediv_name,
  348. parent_name, flags, 1, 8);
  349. else
  350. clk = davinci_pll_div_register(dev, prediv_name,
  351. parent_name, base + PREDIV, fixed, flags);
  352. if (IS_ERR(clk))
  353. return clk;
  354. parent_name = prediv_name;
  355. }
  356. /* Unlock writing to PLL registers */
  357. if (info->unlock_reg) {
  358. if (IS_ERR_OR_NULL(pdata->cfgchip))
  359. dev_warn(dev, "Failed to get CFGCHIP (%ld)\n",
  360. PTR_ERR(pdata->cfgchip));
  361. else
  362. regmap_write_bits(pdata->cfgchip, info->unlock_reg,
  363. info->unlock_mask, 0);
  364. }
  365. pllout = devm_kzalloc(dev, sizeof(*pllout), GFP_KERNEL);
  366. if (!pllout)
  367. return ERR_PTR(-ENOMEM);
  368. snprintf(pllout_name, MAX_NAME_SIZE, "%s_pllout", info->name);
  369. init.name = pllout_name;
  370. if (info->flags & PLL_PLLM_2X)
  371. init.ops = &dm365_pll_ops;
  372. else
  373. init.ops = &davinci_pll_ops;
  374. init.parent_names = &parent_name;
  375. init.num_parents = 1;
  376. init.flags = 0;
  377. if (info->flags & PLL_HAS_PREDIV)
  378. init.flags |= CLK_SET_RATE_PARENT;
  379. pllout->hw.init = &init;
  380. pllout->base = base;
  381. pllout->pllm_mask = info->pllm_mask;
  382. pllout->pllm_min = info->pllm_min;
  383. pllout->pllm_max = info->pllm_max;
  384. pllout_clk = devm_clk_register(dev, &pllout->hw);
  385. if (IS_ERR(pllout_clk))
  386. return pllout_clk;
  387. clk_hw_set_rate_range(&pllout->hw, info->pllout_min_rate,
  388. info->pllout_max_rate);
  389. parent_name = pllout_name;
  390. if (info->flags & PLL_HAS_POSTDIV) {
  391. bool fixed = info->flags & PLL_POSTDIV_FIXED_DIV;
  392. u32 flags = CLK_SET_RATE_PARENT;
  393. snprintf(postdiv_name, MAX_NAME_SIZE, "%s_postdiv", info->name);
  394. if (info->flags & PLL_POSTDIV_ALWAYS_ENABLED)
  395. flags |= CLK_IS_CRITICAL;
  396. clk = davinci_pll_div_register(dev, postdiv_name, parent_name,
  397. base + POSTDIV, fixed, flags);
  398. if (IS_ERR(clk))
  399. return clk;
  400. parent_name = postdiv_name;
  401. }
  402. pllen = devm_kzalloc(dev, sizeof(*pllout), GFP_KERNEL);
  403. if (!pllen)
  404. return ERR_PTR(-ENOMEM);
  405. snprintf(pllen_name, MAX_NAME_SIZE, "%s_pllen", info->name);
  406. init.name = pllen_name;
  407. init.ops = &davinci_pllen_ops;
  408. init.parent_names = &parent_name;
  409. init.num_parents = 1;
  410. init.flags = CLK_SET_RATE_PARENT;
  411. pllen->hw.init = &init;
  412. pllen->base = base;
  413. clk = devm_clk_register(dev, &pllen->hw);
  414. if (IS_ERR(clk))
  415. return clk;
  416. clk_notifier_register(clk, &davinci_pllen_notifier);
  417. return pllout_clk;
  418. }
  419. /**
  420. * davinci_pll_auxclk_register - Register bypass clock (AUXCLK)
  421. * @name: The clock name
  422. * @base: The PLL memory region
  423. */
  424. struct clk *davinci_pll_auxclk_register(struct device *dev,
  425. const char *name,
  426. void __iomem *base)
  427. {
  428. return clk_register_gate(dev, name, OSCIN_CLK_NAME, 0, base + CKEN,
  429. CKEN_AUXEN_SHIFT, 0, NULL);
  430. }
  431. /**
  432. * davinci_pll_sysclkbp_clk_register - Register bypass divider clock (SYSCLKBP)
  433. * @name: The clock name
  434. * @base: The PLL memory region
  435. */
  436. struct clk *davinci_pll_sysclkbp_clk_register(struct device *dev,
  437. const char *name,
  438. void __iomem *base)
  439. {
  440. return clk_register_divider(dev, name, OSCIN_CLK_NAME, 0, base + BPDIV,
  441. DIV_RATIO_SHIFT, DIV_RATIO_WIDTH,
  442. CLK_DIVIDER_READ_ONLY, NULL);
  443. }
  444. /**
  445. * davinci_pll_obsclk_register - Register oscillator divider clock (OBSCLK)
  446. * @info: The clock info
  447. * @base: The PLL memory region
  448. */
  449. struct clk *
  450. davinci_pll_obsclk_register(struct device *dev,
  451. const struct davinci_pll_obsclk_info *info,
  452. void __iomem *base)
  453. {
  454. struct clk_mux *mux;
  455. struct clk_gate *gate;
  456. struct clk_divider *divider;
  457. u32 oscdiv;
  458. mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
  459. if (!mux)
  460. return ERR_PTR(-ENOMEM);
  461. mux->reg = base + OCSEL;
  462. mux->table = info->table;
  463. mux->mask = info->ocsrc_mask;
  464. gate = devm_kzalloc(dev, sizeof(*gate), GFP_KERNEL);
  465. if (!gate)
  466. return ERR_PTR(-ENOMEM);
  467. gate->reg = base + CKEN;
  468. gate->bit_idx = CKEN_OBSCLK_SHIFT;
  469. divider = devm_kzalloc(dev, sizeof(*divider), GFP_KERNEL);
  470. if (!divider)
  471. return ERR_PTR(-ENOMEM);
  472. divider->reg = base + OSCDIV;
  473. divider->shift = DIV_RATIO_SHIFT;
  474. divider->width = DIV_RATIO_WIDTH;
  475. /* make sure divider is enabled just in case bootloader disabled it */
  476. oscdiv = readl(base + OSCDIV);
  477. oscdiv |= BIT(DIV_ENABLE_SHIFT);
  478. writel(oscdiv, base + OSCDIV);
  479. return clk_register_composite(dev, info->name, info->parent_names,
  480. info->num_parents,
  481. &mux->hw, &clk_mux_ops,
  482. &divider->hw, &clk_divider_ops,
  483. &gate->hw, &clk_gate_ops, 0);
  484. }
  485. /* The PLL SYSCLKn clocks have a mechanism for synchronizing rate changes. */
  486. static int davinci_pll_sysclk_rate_change(struct notifier_block *nb,
  487. unsigned long flags, void *data)
  488. {
  489. struct clk_notifier_data *cnd = data;
  490. struct clk_hw *hw = __clk_get_hw(clk_get_parent(cnd->clk));
  491. struct davinci_pllen_clk *pll = to_davinci_pllen_clk(hw);
  492. u32 pllcmd, pllstat;
  493. switch (flags) {
  494. case POST_RATE_CHANGE:
  495. /* apply the changes */
  496. pllcmd = readl(pll->base + PLLCMD);
  497. pllcmd |= PLLCMD_GOSET;
  498. writel(pllcmd, pll->base + PLLCMD);
  499. /* fallthrough */
  500. case PRE_RATE_CHANGE:
  501. /* Wait until for outstanding changes to take effect */
  502. do {
  503. pllstat = readl(pll->base + PLLSTAT);
  504. } while (pllstat & PLLSTAT_GOSTAT);
  505. break;
  506. }
  507. return NOTIFY_OK;
  508. }
  509. static struct notifier_block davinci_pll_sysclk_notifier = {
  510. .notifier_call = davinci_pll_sysclk_rate_change,
  511. };
  512. /**
  513. * davinci_pll_sysclk_register - Register divider clocks (SYSCLKn)
  514. * @info: The clock info
  515. * @base: The PLL memory region
  516. */
  517. struct clk *
  518. davinci_pll_sysclk_register(struct device *dev,
  519. const struct davinci_pll_sysclk_info *info,
  520. void __iomem *base)
  521. {
  522. const struct clk_ops *divider_ops = &clk_divider_ops;
  523. struct clk_gate *gate;
  524. struct clk_divider *divider;
  525. struct clk *clk;
  526. u32 reg;
  527. u32 flags = 0;
  528. /* PLLDIVn registers are not entirely consecutive */
  529. if (info->id < 4)
  530. reg = PLLDIV1 + 4 * (info->id - 1);
  531. else
  532. reg = PLLDIV4 + 4 * (info->id - 4);
  533. gate = devm_kzalloc(dev, sizeof(*gate), GFP_KERNEL);
  534. if (!gate)
  535. return ERR_PTR(-ENOMEM);
  536. gate->reg = base + reg;
  537. gate->bit_idx = DIV_ENABLE_SHIFT;
  538. divider = devm_kzalloc(dev, sizeof(*divider), GFP_KERNEL);
  539. if (!divider)
  540. return ERR_PTR(-ENOMEM);
  541. divider->reg = base + reg;
  542. divider->shift = DIV_RATIO_SHIFT;
  543. divider->width = info->ratio_width;
  544. divider->flags = 0;
  545. if (info->flags & SYSCLK_FIXED_DIV) {
  546. divider->flags |= CLK_DIVIDER_READ_ONLY;
  547. divider_ops = &clk_divider_ro_ops;
  548. }
  549. /* Only the ARM clock can change the parent PLL rate */
  550. if (info->flags & SYSCLK_ARM_RATE)
  551. flags |= CLK_SET_RATE_PARENT;
  552. if (info->flags & SYSCLK_ALWAYS_ENABLED)
  553. flags |= CLK_IS_CRITICAL;
  554. clk = clk_register_composite(dev, info->name, &info->parent_name, 1,
  555. NULL, NULL, &divider->hw, divider_ops,
  556. &gate->hw, &clk_gate_ops, flags);
  557. if (IS_ERR(clk))
  558. return clk;
  559. clk_notifier_register(clk, &davinci_pll_sysclk_notifier);
  560. return clk;
  561. }
  562. int of_davinci_pll_init(struct device *dev,
  563. const struct davinci_pll_clk_info *info,
  564. const struct davinci_pll_obsclk_info *obsclk_info,
  565. const struct davinci_pll_sysclk_info **div_info,
  566. u8 max_sysclk_id,
  567. void __iomem *base)
  568. {
  569. struct device_node *node = dev->of_node;
  570. struct device_node *child;
  571. const char *parent_name;
  572. struct clk *clk;
  573. if (info->flags & PLL_HAS_CLKMODE)
  574. parent_name = of_clk_get_parent_name(node, 0);
  575. else
  576. parent_name = OSCIN_CLK_NAME;
  577. clk = davinci_pll_clk_register(dev, info, parent_name, base);
  578. if (IS_ERR(clk)) {
  579. dev_err(dev, "failed to register %s\n", info->name);
  580. return PTR_ERR(clk);
  581. }
  582. child = of_get_child_by_name(node, "pllout");
  583. if (of_device_is_available(child))
  584. of_clk_add_provider(child, of_clk_src_simple_get, clk);
  585. of_node_put(child);
  586. child = of_get_child_by_name(node, "sysclk");
  587. if (of_device_is_available(child)) {
  588. struct clk_onecell_data *clk_data;
  589. struct clk **clks;
  590. int n_clks = max_sysclk_id + 1;
  591. int i;
  592. clk_data = devm_kzalloc(dev, sizeof(*clk_data), GFP_KERNEL);
  593. if (!clk_data)
  594. return -ENOMEM;
  595. clks = devm_kmalloc_array(dev, n_clks, sizeof(*clks), GFP_KERNEL);
  596. if (!clks)
  597. return -ENOMEM;
  598. clk_data->clks = clks;
  599. clk_data->clk_num = n_clks;
  600. for (i = 0; i < n_clks; i++)
  601. clks[i] = ERR_PTR(-ENOENT);
  602. for (; *div_info; div_info++) {
  603. clk = davinci_pll_sysclk_register(dev, *div_info, base);
  604. if (IS_ERR(clk))
  605. dev_warn(dev, "failed to register %s (%ld)\n",
  606. (*div_info)->name, PTR_ERR(clk));
  607. else
  608. clks[(*div_info)->id] = clk;
  609. }
  610. of_clk_add_provider(child, of_clk_src_onecell_get, clk_data);
  611. }
  612. of_node_put(child);
  613. child = of_get_child_by_name(node, "auxclk");
  614. if (of_device_is_available(child)) {
  615. char child_name[MAX_NAME_SIZE];
  616. snprintf(child_name, MAX_NAME_SIZE, "%s_auxclk", info->name);
  617. clk = davinci_pll_auxclk_register(dev, child_name, base);
  618. if (IS_ERR(clk))
  619. dev_warn(dev, "failed to register %s (%ld)\n",
  620. child_name, PTR_ERR(clk));
  621. else
  622. of_clk_add_provider(child, of_clk_src_simple_get, clk);
  623. }
  624. of_node_put(child);
  625. child = of_get_child_by_name(node, "obsclk");
  626. if (of_device_is_available(child)) {
  627. if (obsclk_info)
  628. clk = davinci_pll_obsclk_register(dev, obsclk_info, base);
  629. else
  630. clk = ERR_PTR(-EINVAL);
  631. if (IS_ERR(clk))
  632. dev_warn(dev, "failed to register obsclk (%ld)\n",
  633. PTR_ERR(clk));
  634. else
  635. of_clk_add_provider(child, of_clk_src_simple_get, clk);
  636. }
  637. of_node_put(child);
  638. return 0;
  639. }
  640. static const struct of_device_id davinci_pll_of_match[] = {
  641. { .compatible = "ti,da850-pll0", .data = of_da850_pll0_init },
  642. { .compatible = "ti,da850-pll1", .data = of_da850_pll1_init },
  643. { }
  644. };
  645. static const struct platform_device_id davinci_pll_id_table[] = {
  646. { .name = "da830-pll", .driver_data = (kernel_ulong_t)da830_pll_init },
  647. { .name = "da850-pll0", .driver_data = (kernel_ulong_t)da850_pll0_init },
  648. { .name = "da850-pll1", .driver_data = (kernel_ulong_t)da850_pll1_init },
  649. { .name = "dm355-pll1", .driver_data = (kernel_ulong_t)dm355_pll1_init },
  650. { .name = "dm355-pll2", .driver_data = (kernel_ulong_t)dm355_pll2_init },
  651. { .name = "dm365-pll1", .driver_data = (kernel_ulong_t)dm365_pll1_init },
  652. { .name = "dm365-pll2", .driver_data = (kernel_ulong_t)dm365_pll2_init },
  653. { .name = "dm644x-pll1", .driver_data = (kernel_ulong_t)dm644x_pll1_init },
  654. { .name = "dm644x-pll2", .driver_data = (kernel_ulong_t)dm644x_pll2_init },
  655. { .name = "dm646x-pll1", .driver_data = (kernel_ulong_t)dm646x_pll1_init },
  656. { .name = "dm646x-pll2", .driver_data = (kernel_ulong_t)dm646x_pll2_init },
  657. { }
  658. };
  659. typedef int (*davinci_pll_init)(struct device *dev, void __iomem *base);
  660. static int davinci_pll_probe(struct platform_device *pdev)
  661. {
  662. struct device *dev = &pdev->dev;
  663. const struct of_device_id *of_id;
  664. davinci_pll_init pll_init = NULL;
  665. struct resource *res;
  666. void __iomem *base;
  667. of_id = of_match_device(davinci_pll_of_match, dev);
  668. if (of_id)
  669. pll_init = of_id->data;
  670. else if (pdev->id_entry)
  671. pll_init = (void *)pdev->id_entry->driver_data;
  672. if (!pll_init) {
  673. dev_err(dev, "unable to find driver data\n");
  674. return -EINVAL;
  675. }
  676. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  677. base = devm_ioremap_resource(dev, res);
  678. if (IS_ERR(base))
  679. return PTR_ERR(base);
  680. return pll_init(dev, base);
  681. }
  682. static struct platform_driver davinci_pll_driver = {
  683. .probe = davinci_pll_probe,
  684. .driver = {
  685. .name = "davinci-pll-clk",
  686. .of_match_table = davinci_pll_of_match,
  687. },
  688. .id_table = davinci_pll_id_table,
  689. };
  690. static int __init davinci_pll_driver_init(void)
  691. {
  692. return platform_driver_register(&davinci_pll_driver);
  693. }
  694. /* has to be postcore_initcall because PSC devices depend on PLL parent clocks */
  695. postcore_initcall(davinci_pll_driver_init);
  696. #ifdef CONFIG_DEBUG_FS
  697. #include <linux/debugfs.h>
  698. #define DEBUG_REG(n) \
  699. { \
  700. .name = #n, \
  701. .offset = n, \
  702. }
  703. static const struct debugfs_reg32 davinci_pll_regs[] = {
  704. DEBUG_REG(REVID),
  705. DEBUG_REG(PLLCTL),
  706. DEBUG_REG(OCSEL),
  707. DEBUG_REG(PLLSECCTL),
  708. DEBUG_REG(PLLM),
  709. DEBUG_REG(PREDIV),
  710. DEBUG_REG(PLLDIV1),
  711. DEBUG_REG(PLLDIV2),
  712. DEBUG_REG(PLLDIV3),
  713. DEBUG_REG(OSCDIV),
  714. DEBUG_REG(POSTDIV),
  715. DEBUG_REG(BPDIV),
  716. DEBUG_REG(PLLCMD),
  717. DEBUG_REG(PLLSTAT),
  718. DEBUG_REG(ALNCTL),
  719. DEBUG_REG(DCHANGE),
  720. DEBUG_REG(CKEN),
  721. DEBUG_REG(CKSTAT),
  722. DEBUG_REG(SYSTAT),
  723. DEBUG_REG(PLLDIV4),
  724. DEBUG_REG(PLLDIV5),
  725. DEBUG_REG(PLLDIV6),
  726. DEBUG_REG(PLLDIV7),
  727. DEBUG_REG(PLLDIV8),
  728. DEBUG_REG(PLLDIV9),
  729. };
  730. static int davinci_pll_debug_init(struct clk_hw *hw, struct dentry *dentry)
  731. {
  732. struct davinci_pll_clk *pll = to_davinci_pll_clk(hw);
  733. struct debugfs_regset32 *regset;
  734. struct dentry *d;
  735. regset = kzalloc(sizeof(*regset), GFP_KERNEL);
  736. if (!regset)
  737. return -ENOMEM;
  738. regset->regs = davinci_pll_regs;
  739. regset->nregs = ARRAY_SIZE(davinci_pll_regs);
  740. regset->base = pll->base;
  741. d = debugfs_create_regset32("registers", 0400, dentry, regset);
  742. if (IS_ERR(d)) {
  743. kfree(regset);
  744. return PTR_ERR(d);
  745. }
  746. return 0;
  747. }
  748. #endif