clk-xgene.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * clk-xgene.c - AppliedMicro X-Gene Clock Interface
  3. *
  4. * Copyright (c) 2013, Applied Micro Circuits Corporation
  5. * Author: Loc Ho <lho@apm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/io.h>
  26. #include <linux/of.h>
  27. #include <linux/clkdev.h>
  28. #include <linux/clk-provider.h>
  29. #include <linux/of_address.h>
  30. /* Register SCU_PCPPLL bit fields */
  31. #define N_DIV_RD(src) ((src) & 0x000001ff)
  32. #define SC_N_DIV_RD(src) ((src) & 0x0000007f)
  33. #define SC_OUTDIV2(src) (((src) & 0x00000100) >> 8)
  34. /* Register SCU_SOCPLL bit fields */
  35. #define CLKR_RD(src) (((src) & 0x07000000)>>24)
  36. #define CLKOD_RD(src) (((src) & 0x00300000)>>20)
  37. #define REGSPEC_RESET_F1_MASK 0x00010000
  38. #define CLKF_RD(src) (((src) & 0x000001ff))
  39. #define XGENE_CLK_DRIVER_VER "0.1"
  40. static DEFINE_SPINLOCK(clk_lock);
  41. static inline u32 xgene_clk_read(void __iomem *csr)
  42. {
  43. return readl_relaxed(csr);
  44. }
  45. static inline void xgene_clk_write(u32 data, void __iomem *csr)
  46. {
  47. writel_relaxed(data, csr);
  48. }
  49. /* PLL Clock */
  50. enum xgene_pll_type {
  51. PLL_TYPE_PCP = 0,
  52. PLL_TYPE_SOC = 1,
  53. };
  54. struct xgene_clk_pll {
  55. struct clk_hw hw;
  56. void __iomem *reg;
  57. spinlock_t *lock;
  58. u32 pll_offset;
  59. enum xgene_pll_type type;
  60. int version;
  61. };
  62. #define to_xgene_clk_pll(_hw) container_of(_hw, struct xgene_clk_pll, hw)
  63. static int xgene_clk_pll_is_enabled(struct clk_hw *hw)
  64. {
  65. struct xgene_clk_pll *pllclk = to_xgene_clk_pll(hw);
  66. u32 data;
  67. data = xgene_clk_read(pllclk->reg + pllclk->pll_offset);
  68. pr_debug("%s pll %s\n", clk_hw_get_name(hw),
  69. data & REGSPEC_RESET_F1_MASK ? "disabled" : "enabled");
  70. return data & REGSPEC_RESET_F1_MASK ? 0 : 1;
  71. }
  72. static unsigned long xgene_clk_pll_recalc_rate(struct clk_hw *hw,
  73. unsigned long parent_rate)
  74. {
  75. struct xgene_clk_pll *pllclk = to_xgene_clk_pll(hw);
  76. unsigned long fref;
  77. unsigned long fvco;
  78. u32 pll;
  79. u32 nref;
  80. u32 nout;
  81. u32 nfb;
  82. pll = xgene_clk_read(pllclk->reg + pllclk->pll_offset);
  83. if (pllclk->version <= 1) {
  84. if (pllclk->type == PLL_TYPE_PCP) {
  85. /*
  86. * PLL VCO = Reference clock * NF
  87. * PCP PLL = PLL_VCO / 2
  88. */
  89. nout = 2;
  90. fvco = parent_rate * (N_DIV_RD(pll) + 4);
  91. } else {
  92. /*
  93. * Fref = Reference Clock / NREF;
  94. * Fvco = Fref * NFB;
  95. * Fout = Fvco / NOUT;
  96. */
  97. nref = CLKR_RD(pll) + 1;
  98. nout = CLKOD_RD(pll) + 1;
  99. nfb = CLKF_RD(pll);
  100. fref = parent_rate / nref;
  101. fvco = fref * nfb;
  102. }
  103. } else {
  104. /*
  105. * fvco = Reference clock * FBDIVC
  106. * PLL freq = fvco / NOUT
  107. */
  108. nout = SC_OUTDIV2(pll) ? 2 : 3;
  109. fvco = parent_rate * SC_N_DIV_RD(pll);
  110. }
  111. pr_debug("%s pll recalc rate %ld parent %ld version %d\n",
  112. clk_hw_get_name(hw), fvco / nout, parent_rate,
  113. pllclk->version);
  114. return fvco / nout;
  115. }
  116. static const struct clk_ops xgene_clk_pll_ops = {
  117. .is_enabled = xgene_clk_pll_is_enabled,
  118. .recalc_rate = xgene_clk_pll_recalc_rate,
  119. };
  120. static struct clk *xgene_register_clk_pll(struct device *dev,
  121. const char *name, const char *parent_name,
  122. unsigned long flags, void __iomem *reg, u32 pll_offset,
  123. u32 type, spinlock_t *lock, int version)
  124. {
  125. struct xgene_clk_pll *apmclk;
  126. struct clk *clk;
  127. struct clk_init_data init;
  128. /* allocate the APM clock structure */
  129. apmclk = kzalloc(sizeof(*apmclk), GFP_KERNEL);
  130. if (!apmclk) {
  131. pr_err("%s: could not allocate APM clk\n", __func__);
  132. return ERR_PTR(-ENOMEM);
  133. }
  134. init.name = name;
  135. init.ops = &xgene_clk_pll_ops;
  136. init.flags = flags;
  137. init.parent_names = parent_name ? &parent_name : NULL;
  138. init.num_parents = parent_name ? 1 : 0;
  139. apmclk->version = version;
  140. apmclk->reg = reg;
  141. apmclk->lock = lock;
  142. apmclk->pll_offset = pll_offset;
  143. apmclk->type = type;
  144. apmclk->hw.init = &init;
  145. /* Register the clock */
  146. clk = clk_register(dev, &apmclk->hw);
  147. if (IS_ERR(clk)) {
  148. pr_err("%s: could not register clk %s\n", __func__, name);
  149. kfree(apmclk);
  150. return NULL;
  151. }
  152. return clk;
  153. }
  154. static int xgene_pllclk_version(struct device_node *np)
  155. {
  156. if (of_device_is_compatible(np, "apm,xgene-socpll-clock"))
  157. return 1;
  158. if (of_device_is_compatible(np, "apm,xgene-pcppll-clock"))
  159. return 1;
  160. return 2;
  161. }
  162. static void xgene_pllclk_init(struct device_node *np, enum xgene_pll_type pll_type)
  163. {
  164. const char *clk_name = np->full_name;
  165. struct clk *clk;
  166. void __iomem *reg;
  167. int version = xgene_pllclk_version(np);
  168. reg = of_iomap(np, 0);
  169. if (reg == NULL) {
  170. pr_err("Unable to map CSR register for %s\n", np->full_name);
  171. return;
  172. }
  173. of_property_read_string(np, "clock-output-names", &clk_name);
  174. clk = xgene_register_clk_pll(NULL,
  175. clk_name, of_clk_get_parent_name(np, 0),
  176. 0, reg, 0, pll_type, &clk_lock,
  177. version);
  178. if (!IS_ERR(clk)) {
  179. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  180. clk_register_clkdev(clk, clk_name, NULL);
  181. pr_debug("Add %s clock PLL\n", clk_name);
  182. }
  183. }
  184. static void xgene_socpllclk_init(struct device_node *np)
  185. {
  186. xgene_pllclk_init(np, PLL_TYPE_SOC);
  187. }
  188. static void xgene_pcppllclk_init(struct device_node *np)
  189. {
  190. xgene_pllclk_init(np, PLL_TYPE_PCP);
  191. }
  192. /**
  193. * struct xgene_clk_pmd - PMD clock
  194. *
  195. * @hw: handle between common and hardware-specific interfaces
  196. * @reg: register containing the fractional scale multiplier (scaler)
  197. * @shift: shift to the unit bit field
  198. * @denom: 1/denominator unit
  199. * @lock: register lock
  200. * Flags:
  201. * XGENE_CLK_PMD_SCALE_INVERTED - By default the scaler is the value read
  202. * from the register plus one. For example,
  203. * 0 for (0 + 1) / denom,
  204. * 1 for (1 + 1) / denom and etc.
  205. * If this flag is set, it is
  206. * 0 for (denom - 0) / denom,
  207. * 1 for (denom - 1) / denom and etc.
  208. *
  209. */
  210. struct xgene_clk_pmd {
  211. struct clk_hw hw;
  212. void __iomem *reg;
  213. u8 shift;
  214. u32 mask;
  215. u64 denom;
  216. u32 flags;
  217. spinlock_t *lock;
  218. };
  219. #define to_xgene_clk_pmd(_hw) container_of(_hw, struct xgene_clk_pmd, hw)
  220. #define XGENE_CLK_PMD_SCALE_INVERTED BIT(0)
  221. #define XGENE_CLK_PMD_SHIFT 8
  222. #define XGENE_CLK_PMD_WIDTH 3
  223. static unsigned long xgene_clk_pmd_recalc_rate(struct clk_hw *hw,
  224. unsigned long parent_rate)
  225. {
  226. struct xgene_clk_pmd *fd = to_xgene_clk_pmd(hw);
  227. unsigned long flags = 0;
  228. u64 ret, scale;
  229. u32 val;
  230. if (fd->lock)
  231. spin_lock_irqsave(fd->lock, flags);
  232. else
  233. __acquire(fd->lock);
  234. val = clk_readl(fd->reg);
  235. if (fd->lock)
  236. spin_unlock_irqrestore(fd->lock, flags);
  237. else
  238. __release(fd->lock);
  239. ret = (u64)parent_rate;
  240. scale = (val & fd->mask) >> fd->shift;
  241. if (fd->flags & XGENE_CLK_PMD_SCALE_INVERTED)
  242. scale = fd->denom - scale;
  243. else
  244. scale++;
  245. /* freq = parent_rate * scaler / denom */
  246. do_div(ret, fd->denom);
  247. ret *= scale;
  248. if (ret == 0)
  249. ret = (u64)parent_rate;
  250. return ret;
  251. }
  252. static long xgene_clk_pmd_round_rate(struct clk_hw *hw, unsigned long rate,
  253. unsigned long *parent_rate)
  254. {
  255. struct xgene_clk_pmd *fd = to_xgene_clk_pmd(hw);
  256. u64 ret, scale;
  257. if (!rate || rate >= *parent_rate)
  258. return *parent_rate;
  259. /* freq = parent_rate * scaler / denom */
  260. ret = rate * fd->denom;
  261. scale = DIV_ROUND_UP_ULL(ret, *parent_rate);
  262. ret = (u64)*parent_rate * scale;
  263. do_div(ret, fd->denom);
  264. return ret;
  265. }
  266. static int xgene_clk_pmd_set_rate(struct clk_hw *hw, unsigned long rate,
  267. unsigned long parent_rate)
  268. {
  269. struct xgene_clk_pmd *fd = to_xgene_clk_pmd(hw);
  270. unsigned long flags = 0;
  271. u64 scale, ret;
  272. u32 val;
  273. /*
  274. * Compute the scaler:
  275. *
  276. * freq = parent_rate * scaler / denom, or
  277. * scaler = freq * denom / parent_rate
  278. */
  279. ret = rate * fd->denom;
  280. scale = DIV_ROUND_UP_ULL(ret, (u64)parent_rate);
  281. /* Check if inverted */
  282. if (fd->flags & XGENE_CLK_PMD_SCALE_INVERTED)
  283. scale = fd->denom - scale;
  284. else
  285. scale--;
  286. if (fd->lock)
  287. spin_lock_irqsave(fd->lock, flags);
  288. else
  289. __acquire(fd->lock);
  290. val = clk_readl(fd->reg);
  291. val &= ~fd->mask;
  292. val |= (scale << fd->shift);
  293. clk_writel(val, fd->reg);
  294. if (fd->lock)
  295. spin_unlock_irqrestore(fd->lock, flags);
  296. else
  297. __release(fd->lock);
  298. return 0;
  299. }
  300. static const struct clk_ops xgene_clk_pmd_ops = {
  301. .recalc_rate = xgene_clk_pmd_recalc_rate,
  302. .round_rate = xgene_clk_pmd_round_rate,
  303. .set_rate = xgene_clk_pmd_set_rate,
  304. };
  305. static struct clk *
  306. xgene_register_clk_pmd(struct device *dev,
  307. const char *name, const char *parent_name,
  308. unsigned long flags, void __iomem *reg, u8 shift,
  309. u8 width, u64 denom, u32 clk_flags, spinlock_t *lock)
  310. {
  311. struct xgene_clk_pmd *fd;
  312. struct clk_init_data init;
  313. struct clk *clk;
  314. fd = kzalloc(sizeof(*fd), GFP_KERNEL);
  315. if (!fd)
  316. return ERR_PTR(-ENOMEM);
  317. init.name = name;
  318. init.ops = &xgene_clk_pmd_ops;
  319. init.flags = flags;
  320. init.parent_names = parent_name ? &parent_name : NULL;
  321. init.num_parents = parent_name ? 1 : 0;
  322. fd->reg = reg;
  323. fd->shift = shift;
  324. fd->mask = (BIT(width) - 1) << shift;
  325. fd->denom = denom;
  326. fd->flags = clk_flags;
  327. fd->lock = lock;
  328. fd->hw.init = &init;
  329. clk = clk_register(dev, &fd->hw);
  330. if (IS_ERR(clk)) {
  331. pr_err("%s: could not register clk %s\n", __func__, name);
  332. kfree(fd);
  333. return NULL;
  334. }
  335. return clk;
  336. }
  337. static void xgene_pmdclk_init(struct device_node *np)
  338. {
  339. const char *clk_name = np->full_name;
  340. void __iomem *csr_reg;
  341. struct resource res;
  342. struct clk *clk;
  343. u64 denom;
  344. u32 flags = 0;
  345. int rc;
  346. /* Check if the entry is disabled */
  347. if (!of_device_is_available(np))
  348. return;
  349. /* Parse the DTS register for resource */
  350. rc = of_address_to_resource(np, 0, &res);
  351. if (rc != 0) {
  352. pr_err("no DTS register for %s\n", np->full_name);
  353. return;
  354. }
  355. csr_reg = of_iomap(np, 0);
  356. if (!csr_reg) {
  357. pr_err("Unable to map resource for %s\n", np->full_name);
  358. return;
  359. }
  360. of_property_read_string(np, "clock-output-names", &clk_name);
  361. denom = BIT(XGENE_CLK_PMD_WIDTH);
  362. flags |= XGENE_CLK_PMD_SCALE_INVERTED;
  363. clk = xgene_register_clk_pmd(NULL, clk_name,
  364. of_clk_get_parent_name(np, 0), 0,
  365. csr_reg, XGENE_CLK_PMD_SHIFT,
  366. XGENE_CLK_PMD_WIDTH, denom,
  367. flags, &clk_lock);
  368. if (!IS_ERR(clk)) {
  369. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  370. clk_register_clkdev(clk, clk_name, NULL);
  371. pr_debug("Add %s clock\n", clk_name);
  372. } else {
  373. if (csr_reg)
  374. iounmap(csr_reg);
  375. }
  376. }
  377. /* IP Clock */
  378. struct xgene_dev_parameters {
  379. void __iomem *csr_reg; /* CSR for IP clock */
  380. u32 reg_clk_offset; /* Offset to clock enable CSR */
  381. u32 reg_clk_mask; /* Mask bit for clock enable */
  382. u32 reg_csr_offset; /* Offset to CSR reset */
  383. u32 reg_csr_mask; /* Mask bit for disable CSR reset */
  384. void __iomem *divider_reg; /* CSR for divider */
  385. u32 reg_divider_offset; /* Offset to divider register */
  386. u32 reg_divider_shift; /* Bit shift to divider field */
  387. u32 reg_divider_width; /* Width of the bit to divider field */
  388. };
  389. struct xgene_clk {
  390. struct clk_hw hw;
  391. spinlock_t *lock;
  392. struct xgene_dev_parameters param;
  393. };
  394. #define to_xgene_clk(_hw) container_of(_hw, struct xgene_clk, hw)
  395. static int xgene_clk_enable(struct clk_hw *hw)
  396. {
  397. struct xgene_clk *pclk = to_xgene_clk(hw);
  398. unsigned long flags = 0;
  399. u32 data;
  400. if (pclk->lock)
  401. spin_lock_irqsave(pclk->lock, flags);
  402. if (pclk->param.csr_reg != NULL) {
  403. pr_debug("%s clock enabled\n", clk_hw_get_name(hw));
  404. /* First enable the clock */
  405. data = xgene_clk_read(pclk->param.csr_reg +
  406. pclk->param.reg_clk_offset);
  407. data |= pclk->param.reg_clk_mask;
  408. xgene_clk_write(data, pclk->param.csr_reg +
  409. pclk->param.reg_clk_offset);
  410. pr_debug("%s clk offset 0x%08X mask 0x%08X value 0x%08X\n",
  411. clk_hw_get_name(hw),
  412. pclk->param.reg_clk_offset, pclk->param.reg_clk_mask,
  413. data);
  414. /* Second enable the CSR */
  415. data = xgene_clk_read(pclk->param.csr_reg +
  416. pclk->param.reg_csr_offset);
  417. data &= ~pclk->param.reg_csr_mask;
  418. xgene_clk_write(data, pclk->param.csr_reg +
  419. pclk->param.reg_csr_offset);
  420. pr_debug("%s csr offset 0x%08X mask 0x%08X value 0x%08X\n",
  421. clk_hw_get_name(hw),
  422. pclk->param.reg_csr_offset, pclk->param.reg_csr_mask,
  423. data);
  424. }
  425. if (pclk->lock)
  426. spin_unlock_irqrestore(pclk->lock, flags);
  427. return 0;
  428. }
  429. static void xgene_clk_disable(struct clk_hw *hw)
  430. {
  431. struct xgene_clk *pclk = to_xgene_clk(hw);
  432. unsigned long flags = 0;
  433. u32 data;
  434. if (pclk->lock)
  435. spin_lock_irqsave(pclk->lock, flags);
  436. if (pclk->param.csr_reg != NULL) {
  437. pr_debug("%s clock disabled\n", clk_hw_get_name(hw));
  438. /* First put the CSR in reset */
  439. data = xgene_clk_read(pclk->param.csr_reg +
  440. pclk->param.reg_csr_offset);
  441. data |= pclk->param.reg_csr_mask;
  442. xgene_clk_write(data, pclk->param.csr_reg +
  443. pclk->param.reg_csr_offset);
  444. /* Second disable the clock */
  445. data = xgene_clk_read(pclk->param.csr_reg +
  446. pclk->param.reg_clk_offset);
  447. data &= ~pclk->param.reg_clk_mask;
  448. xgene_clk_write(data, pclk->param.csr_reg +
  449. pclk->param.reg_clk_offset);
  450. }
  451. if (pclk->lock)
  452. spin_unlock_irqrestore(pclk->lock, flags);
  453. }
  454. static int xgene_clk_is_enabled(struct clk_hw *hw)
  455. {
  456. struct xgene_clk *pclk = to_xgene_clk(hw);
  457. u32 data = 0;
  458. if (pclk->param.csr_reg != NULL) {
  459. pr_debug("%s clock checking\n", clk_hw_get_name(hw));
  460. data = xgene_clk_read(pclk->param.csr_reg +
  461. pclk->param.reg_clk_offset);
  462. pr_debug("%s clock is %s\n", clk_hw_get_name(hw),
  463. data & pclk->param.reg_clk_mask ? "enabled" :
  464. "disabled");
  465. }
  466. if (pclk->param.csr_reg == NULL)
  467. return 1;
  468. return data & pclk->param.reg_clk_mask ? 1 : 0;
  469. }
  470. static unsigned long xgene_clk_recalc_rate(struct clk_hw *hw,
  471. unsigned long parent_rate)
  472. {
  473. struct xgene_clk *pclk = to_xgene_clk(hw);
  474. u32 data;
  475. if (pclk->param.divider_reg) {
  476. data = xgene_clk_read(pclk->param.divider_reg +
  477. pclk->param.reg_divider_offset);
  478. data >>= pclk->param.reg_divider_shift;
  479. data &= (1 << pclk->param.reg_divider_width) - 1;
  480. pr_debug("%s clock recalc rate %ld parent %ld\n",
  481. clk_hw_get_name(hw),
  482. parent_rate / data, parent_rate);
  483. return parent_rate / data;
  484. } else {
  485. pr_debug("%s clock recalc rate %ld parent %ld\n",
  486. clk_hw_get_name(hw), parent_rate, parent_rate);
  487. return parent_rate;
  488. }
  489. }
  490. static int xgene_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  491. unsigned long parent_rate)
  492. {
  493. struct xgene_clk *pclk = to_xgene_clk(hw);
  494. unsigned long flags = 0;
  495. u32 data;
  496. u32 divider;
  497. u32 divider_save;
  498. if (pclk->lock)
  499. spin_lock_irqsave(pclk->lock, flags);
  500. if (pclk->param.divider_reg) {
  501. /* Let's compute the divider */
  502. if (rate > parent_rate)
  503. rate = parent_rate;
  504. divider_save = divider = parent_rate / rate; /* Rounded down */
  505. divider &= (1 << pclk->param.reg_divider_width) - 1;
  506. divider <<= pclk->param.reg_divider_shift;
  507. /* Set new divider */
  508. data = xgene_clk_read(pclk->param.divider_reg +
  509. pclk->param.reg_divider_offset);
  510. data &= ~(((1 << pclk->param.reg_divider_width) - 1)
  511. << pclk->param.reg_divider_shift);
  512. data |= divider;
  513. xgene_clk_write(data, pclk->param.divider_reg +
  514. pclk->param.reg_divider_offset);
  515. pr_debug("%s clock set rate %ld\n", clk_hw_get_name(hw),
  516. parent_rate / divider_save);
  517. } else {
  518. divider_save = 1;
  519. }
  520. if (pclk->lock)
  521. spin_unlock_irqrestore(pclk->lock, flags);
  522. return parent_rate / divider_save;
  523. }
  524. static long xgene_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  525. unsigned long *prate)
  526. {
  527. struct xgene_clk *pclk = to_xgene_clk(hw);
  528. unsigned long parent_rate = *prate;
  529. u32 divider;
  530. if (pclk->param.divider_reg) {
  531. /* Let's compute the divider */
  532. if (rate > parent_rate)
  533. rate = parent_rate;
  534. divider = parent_rate / rate; /* Rounded down */
  535. } else {
  536. divider = 1;
  537. }
  538. return parent_rate / divider;
  539. }
  540. static const struct clk_ops xgene_clk_ops = {
  541. .enable = xgene_clk_enable,
  542. .disable = xgene_clk_disable,
  543. .is_enabled = xgene_clk_is_enabled,
  544. .recalc_rate = xgene_clk_recalc_rate,
  545. .set_rate = xgene_clk_set_rate,
  546. .round_rate = xgene_clk_round_rate,
  547. };
  548. static struct clk *xgene_register_clk(struct device *dev,
  549. const char *name, const char *parent_name,
  550. struct xgene_dev_parameters *parameters, spinlock_t *lock)
  551. {
  552. struct xgene_clk *apmclk;
  553. struct clk *clk;
  554. struct clk_init_data init;
  555. int rc;
  556. /* allocate the APM clock structure */
  557. apmclk = kzalloc(sizeof(*apmclk), GFP_KERNEL);
  558. if (!apmclk) {
  559. pr_err("%s: could not allocate APM clk\n", __func__);
  560. return ERR_PTR(-ENOMEM);
  561. }
  562. init.name = name;
  563. init.ops = &xgene_clk_ops;
  564. init.flags = 0;
  565. init.parent_names = parent_name ? &parent_name : NULL;
  566. init.num_parents = parent_name ? 1 : 0;
  567. apmclk->lock = lock;
  568. apmclk->hw.init = &init;
  569. apmclk->param = *parameters;
  570. /* Register the clock */
  571. clk = clk_register(dev, &apmclk->hw);
  572. if (IS_ERR(clk)) {
  573. pr_err("%s: could not register clk %s\n", __func__, name);
  574. kfree(apmclk);
  575. return clk;
  576. }
  577. /* Register the clock for lookup */
  578. rc = clk_register_clkdev(clk, name, NULL);
  579. if (rc != 0) {
  580. pr_err("%s: could not register lookup clk %s\n",
  581. __func__, name);
  582. }
  583. return clk;
  584. }
  585. static void __init xgene_devclk_init(struct device_node *np)
  586. {
  587. const char *clk_name = np->full_name;
  588. struct clk *clk;
  589. struct resource res;
  590. int rc;
  591. struct xgene_dev_parameters parameters;
  592. int i;
  593. /* Check if the entry is disabled */
  594. if (!of_device_is_available(np))
  595. return;
  596. /* Parse the DTS register for resource */
  597. parameters.csr_reg = NULL;
  598. parameters.divider_reg = NULL;
  599. for (i = 0; i < 2; i++) {
  600. void __iomem *map_res;
  601. rc = of_address_to_resource(np, i, &res);
  602. if (rc != 0) {
  603. if (i == 0) {
  604. pr_err("no DTS register for %s\n",
  605. np->full_name);
  606. return;
  607. }
  608. break;
  609. }
  610. map_res = of_iomap(np, i);
  611. if (map_res == NULL) {
  612. pr_err("Unable to map resource %d for %s\n",
  613. i, np->full_name);
  614. goto err;
  615. }
  616. if (strcmp(res.name, "div-reg") == 0)
  617. parameters.divider_reg = map_res;
  618. else /* if (strcmp(res->name, "csr-reg") == 0) */
  619. parameters.csr_reg = map_res;
  620. }
  621. if (of_property_read_u32(np, "csr-offset", &parameters.reg_csr_offset))
  622. parameters.reg_csr_offset = 0;
  623. if (of_property_read_u32(np, "csr-mask", &parameters.reg_csr_mask))
  624. parameters.reg_csr_mask = 0xF;
  625. if (of_property_read_u32(np, "enable-offset",
  626. &parameters.reg_clk_offset))
  627. parameters.reg_clk_offset = 0x8;
  628. if (of_property_read_u32(np, "enable-mask", &parameters.reg_clk_mask))
  629. parameters.reg_clk_mask = 0xF;
  630. if (of_property_read_u32(np, "divider-offset",
  631. &parameters.reg_divider_offset))
  632. parameters.reg_divider_offset = 0;
  633. if (of_property_read_u32(np, "divider-width",
  634. &parameters.reg_divider_width))
  635. parameters.reg_divider_width = 0;
  636. if (of_property_read_u32(np, "divider-shift",
  637. &parameters.reg_divider_shift))
  638. parameters.reg_divider_shift = 0;
  639. of_property_read_string(np, "clock-output-names", &clk_name);
  640. clk = xgene_register_clk(NULL, clk_name,
  641. of_clk_get_parent_name(np, 0), &parameters, &clk_lock);
  642. if (IS_ERR(clk))
  643. goto err;
  644. pr_debug("Add %s clock\n", clk_name);
  645. rc = of_clk_add_provider(np, of_clk_src_simple_get, clk);
  646. if (rc != 0)
  647. pr_err("%s: could register provider clk %s\n", __func__,
  648. np->full_name);
  649. return;
  650. err:
  651. if (parameters.csr_reg)
  652. iounmap(parameters.csr_reg);
  653. if (parameters.divider_reg)
  654. iounmap(parameters.divider_reg);
  655. }
  656. CLK_OF_DECLARE(xgene_socpll_clock, "apm,xgene-socpll-clock", xgene_socpllclk_init);
  657. CLK_OF_DECLARE(xgene_pcppll_clock, "apm,xgene-pcppll-clock", xgene_pcppllclk_init);
  658. CLK_OF_DECLARE(xgene_pmd_clock, "apm,xgene-pmd-clock", xgene_pmdclk_init);
  659. CLK_OF_DECLARE(xgene_socpll_v2_clock, "apm,xgene-socpll-v2-clock",
  660. xgene_socpllclk_init);
  661. CLK_OF_DECLARE(xgene_pcppll_v2_clock, "apm,xgene-pcppll-v2-clock",
  662. xgene_pcppllclk_init);
  663. CLK_OF_DECLARE(xgene_dev_clock, "apm,xgene-device-clock", xgene_devclk_init);