clk-xgene.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. #include <asm/setup.h>
  31. /* Register SCU_PCPPLL bit fields */
  32. #define N_DIV_RD(src) (((src) & 0x000001ff))
  33. /* Register SCU_SOCPLL bit fields */
  34. #define CLKR_RD(src) (((src) & 0x07000000)>>24)
  35. #define CLKOD_RD(src) (((src) & 0x00300000)>>20)
  36. #define REGSPEC_RESET_F1_MASK 0x00010000
  37. #define CLKF_RD(src) (((src) & 0x000001ff))
  38. #define XGENE_CLK_DRIVER_VER "0.1"
  39. static DEFINE_SPINLOCK(clk_lock);
  40. static inline u32 xgene_clk_read(void *csr)
  41. {
  42. return readl_relaxed(csr);
  43. }
  44. static inline void xgene_clk_write(u32 data, void *csr)
  45. {
  46. return writel_relaxed(data, csr);
  47. }
  48. /* PLL Clock */
  49. enum xgene_pll_type {
  50. PLL_TYPE_PCP = 0,
  51. PLL_TYPE_SOC = 1,
  52. };
  53. struct xgene_clk_pll {
  54. struct clk_hw hw;
  55. const char *name;
  56. void __iomem *reg;
  57. spinlock_t *lock;
  58. u32 pll_offset;
  59. enum xgene_pll_type type;
  60. };
  61. #define to_xgene_clk_pll(_hw) container_of(_hw, struct xgene_clk_pll, hw)
  62. static int xgene_clk_pll_is_enabled(struct clk_hw *hw)
  63. {
  64. struct xgene_clk_pll *pllclk = to_xgene_clk_pll(hw);
  65. u32 data;
  66. data = xgene_clk_read(pllclk->reg + pllclk->pll_offset);
  67. pr_debug("%s pll %s\n", pllclk->name,
  68. data & REGSPEC_RESET_F1_MASK ? "disabled" : "enabled");
  69. return data & REGSPEC_RESET_F1_MASK ? 0 : 1;
  70. }
  71. static unsigned long xgene_clk_pll_recalc_rate(struct clk_hw *hw,
  72. unsigned long parent_rate)
  73. {
  74. struct xgene_clk_pll *pllclk = to_xgene_clk_pll(hw);
  75. unsigned long fref;
  76. unsigned long fvco;
  77. u32 pll;
  78. u32 nref;
  79. u32 nout;
  80. u32 nfb;
  81. pll = xgene_clk_read(pllclk->reg + pllclk->pll_offset);
  82. if (pllclk->type == PLL_TYPE_PCP) {
  83. /*
  84. * PLL VCO = Reference clock * NF
  85. * PCP PLL = PLL_VCO / 2
  86. */
  87. nout = 2;
  88. fvco = parent_rate * (N_DIV_RD(pll) + 4);
  89. } else {
  90. /*
  91. * Fref = Reference Clock / NREF;
  92. * Fvco = Fref * NFB;
  93. * Fout = Fvco / NOUT;
  94. */
  95. nref = CLKR_RD(pll) + 1;
  96. nout = CLKOD_RD(pll) + 1;
  97. nfb = CLKF_RD(pll);
  98. fref = parent_rate / nref;
  99. fvco = fref * nfb;
  100. }
  101. pr_debug("%s pll recalc rate %ld parent %ld\n", pllclk->name,
  102. fvco / nout, parent_rate);
  103. return fvco / nout;
  104. }
  105. const struct clk_ops xgene_clk_pll_ops = {
  106. .is_enabled = xgene_clk_pll_is_enabled,
  107. .recalc_rate = xgene_clk_pll_recalc_rate,
  108. };
  109. static struct clk *xgene_register_clk_pll(struct device *dev,
  110. const char *name, const char *parent_name,
  111. unsigned long flags, void __iomem *reg, u32 pll_offset,
  112. u32 type, spinlock_t *lock)
  113. {
  114. struct xgene_clk_pll *apmclk;
  115. struct clk *clk;
  116. struct clk_init_data init;
  117. /* allocate the APM clock structure */
  118. apmclk = kzalloc(sizeof(*apmclk), GFP_KERNEL);
  119. if (!apmclk) {
  120. pr_err("%s: could not allocate APM clk\n", __func__);
  121. return ERR_PTR(-ENOMEM);
  122. }
  123. init.name = name;
  124. init.ops = &xgene_clk_pll_ops;
  125. init.flags = flags;
  126. init.parent_names = parent_name ? &parent_name : NULL;
  127. init.num_parents = parent_name ? 1 : 0;
  128. apmclk->name = name;
  129. apmclk->reg = reg;
  130. apmclk->lock = lock;
  131. apmclk->pll_offset = pll_offset;
  132. apmclk->type = type;
  133. apmclk->hw.init = &init;
  134. /* Register the clock */
  135. clk = clk_register(dev, &apmclk->hw);
  136. if (IS_ERR(clk)) {
  137. pr_err("%s: could not register clk %s\n", __func__, name);
  138. kfree(apmclk);
  139. return NULL;
  140. }
  141. return clk;
  142. }
  143. static void xgene_pllclk_init(struct device_node *np, enum xgene_pll_type pll_type)
  144. {
  145. const char *clk_name = np->full_name;
  146. struct clk *clk;
  147. void *reg;
  148. reg = of_iomap(np, 0);
  149. if (reg == NULL) {
  150. pr_err("Unable to map CSR register for %s\n", np->full_name);
  151. return;
  152. }
  153. of_property_read_string(np, "clock-output-names", &clk_name);
  154. clk = xgene_register_clk_pll(NULL,
  155. clk_name, of_clk_get_parent_name(np, 0),
  156. CLK_IS_ROOT, reg, 0, pll_type, &clk_lock);
  157. if (!IS_ERR(clk)) {
  158. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  159. clk_register_clkdev(clk, clk_name, NULL);
  160. pr_debug("Add %s clock PLL\n", clk_name);
  161. }
  162. }
  163. static void xgene_socpllclk_init(struct device_node *np)
  164. {
  165. xgene_pllclk_init(np, PLL_TYPE_SOC);
  166. }
  167. static void xgene_pcppllclk_init(struct device_node *np)
  168. {
  169. xgene_pllclk_init(np, PLL_TYPE_PCP);
  170. }
  171. /* IP Clock */
  172. struct xgene_dev_parameters {
  173. void __iomem *csr_reg; /* CSR for IP clock */
  174. u32 reg_clk_offset; /* Offset to clock enable CSR */
  175. u32 reg_clk_mask; /* Mask bit for clock enable */
  176. u32 reg_csr_offset; /* Offset to CSR reset */
  177. u32 reg_csr_mask; /* Mask bit for disable CSR reset */
  178. void __iomem *divider_reg; /* CSR for divider */
  179. u32 reg_divider_offset; /* Offset to divider register */
  180. u32 reg_divider_shift; /* Bit shift to divider field */
  181. u32 reg_divider_width; /* Width of the bit to divider field */
  182. };
  183. struct xgene_clk {
  184. struct clk_hw hw;
  185. const char *name;
  186. spinlock_t *lock;
  187. struct xgene_dev_parameters param;
  188. };
  189. #define to_xgene_clk(_hw) container_of(_hw, struct xgene_clk, hw)
  190. static int xgene_clk_enable(struct clk_hw *hw)
  191. {
  192. struct xgene_clk *pclk = to_xgene_clk(hw);
  193. unsigned long flags = 0;
  194. u32 data;
  195. if (pclk->lock)
  196. spin_lock_irqsave(pclk->lock, flags);
  197. if (pclk->param.csr_reg != NULL) {
  198. pr_debug("%s clock enabled\n", pclk->name);
  199. /* First enable the clock */
  200. data = xgene_clk_read(pclk->param.csr_reg +
  201. pclk->param.reg_clk_offset);
  202. data |= pclk->param.reg_clk_mask;
  203. xgene_clk_write(data, pclk->param.csr_reg +
  204. pclk->param.reg_clk_offset);
  205. pr_debug("%s clock PADDR base 0x%016LX clk offset 0x%08X mask 0x%08X value 0x%08X\n",
  206. pclk->name, __pa(pclk->param.csr_reg),
  207. pclk->param.reg_clk_offset, pclk->param.reg_clk_mask,
  208. data);
  209. /* Second enable the CSR */
  210. data = xgene_clk_read(pclk->param.csr_reg +
  211. pclk->param.reg_csr_offset);
  212. data &= ~pclk->param.reg_csr_mask;
  213. xgene_clk_write(data, pclk->param.csr_reg +
  214. pclk->param.reg_csr_offset);
  215. pr_debug("%s CSR RESET PADDR base 0x%016LX csr offset 0x%08X mask 0x%08X value 0x%08X\n",
  216. pclk->name, __pa(pclk->param.csr_reg),
  217. pclk->param.reg_csr_offset, pclk->param.reg_csr_mask,
  218. data);
  219. }
  220. if (pclk->lock)
  221. spin_unlock_irqrestore(pclk->lock, flags);
  222. return 0;
  223. }
  224. static void xgene_clk_disable(struct clk_hw *hw)
  225. {
  226. struct xgene_clk *pclk = to_xgene_clk(hw);
  227. unsigned long flags = 0;
  228. u32 data;
  229. if (pclk->lock)
  230. spin_lock_irqsave(pclk->lock, flags);
  231. if (pclk->param.csr_reg != NULL) {
  232. pr_debug("%s clock disabled\n", pclk->name);
  233. /* First put the CSR in reset */
  234. data = xgene_clk_read(pclk->param.csr_reg +
  235. pclk->param.reg_csr_offset);
  236. data |= pclk->param.reg_csr_mask;
  237. xgene_clk_write(data, pclk->param.csr_reg +
  238. pclk->param.reg_csr_offset);
  239. /* Second disable the clock */
  240. data = xgene_clk_read(pclk->param.csr_reg +
  241. pclk->param.reg_clk_offset);
  242. data &= ~pclk->param.reg_clk_mask;
  243. xgene_clk_write(data, pclk->param.csr_reg +
  244. pclk->param.reg_clk_offset);
  245. }
  246. if (pclk->lock)
  247. spin_unlock_irqrestore(pclk->lock, flags);
  248. }
  249. static int xgene_clk_is_enabled(struct clk_hw *hw)
  250. {
  251. struct xgene_clk *pclk = to_xgene_clk(hw);
  252. u32 data = 0;
  253. if (pclk->param.csr_reg != NULL) {
  254. pr_debug("%s clock checking\n", pclk->name);
  255. data = xgene_clk_read(pclk->param.csr_reg +
  256. pclk->param.reg_clk_offset);
  257. pr_debug("%s clock is %s\n", pclk->name,
  258. data & pclk->param.reg_clk_mask ? "enabled" :
  259. "disabled");
  260. }
  261. if (pclk->param.csr_reg == NULL)
  262. return 1;
  263. return data & pclk->param.reg_clk_mask ? 1 : 0;
  264. }
  265. static unsigned long xgene_clk_recalc_rate(struct clk_hw *hw,
  266. unsigned long parent_rate)
  267. {
  268. struct xgene_clk *pclk = to_xgene_clk(hw);
  269. u32 data;
  270. if (pclk->param.divider_reg) {
  271. data = xgene_clk_read(pclk->param.divider_reg +
  272. pclk->param.reg_divider_offset);
  273. data >>= pclk->param.reg_divider_shift;
  274. data &= (1 << pclk->param.reg_divider_width) - 1;
  275. pr_debug("%s clock recalc rate %ld parent %ld\n",
  276. pclk->name, parent_rate / data, parent_rate);
  277. return parent_rate / data;
  278. } else {
  279. pr_debug("%s clock recalc rate %ld parent %ld\n",
  280. pclk->name, parent_rate, parent_rate);
  281. return parent_rate;
  282. }
  283. }
  284. static int xgene_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  285. unsigned long parent_rate)
  286. {
  287. struct xgene_clk *pclk = to_xgene_clk(hw);
  288. unsigned long flags = 0;
  289. u32 data;
  290. u32 divider;
  291. u32 divider_save;
  292. if (pclk->lock)
  293. spin_lock_irqsave(pclk->lock, flags);
  294. if (pclk->param.divider_reg) {
  295. /* Let's compute the divider */
  296. if (rate > parent_rate)
  297. rate = parent_rate;
  298. divider_save = divider = parent_rate / rate; /* Rounded down */
  299. divider &= (1 << pclk->param.reg_divider_width) - 1;
  300. divider <<= pclk->param.reg_divider_shift;
  301. /* Set new divider */
  302. data = xgene_clk_read(pclk->param.divider_reg +
  303. pclk->param.reg_divider_offset);
  304. data &= ~((1 << pclk->param.reg_divider_width) - 1);
  305. data |= divider;
  306. xgene_clk_write(data, pclk->param.divider_reg +
  307. pclk->param.reg_divider_offset);
  308. pr_debug("%s clock set rate %ld\n", pclk->name,
  309. parent_rate / divider_save);
  310. } else {
  311. divider_save = 1;
  312. }
  313. if (pclk->lock)
  314. spin_unlock_irqrestore(pclk->lock, flags);
  315. return parent_rate / divider_save;
  316. }
  317. static long xgene_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  318. unsigned long *prate)
  319. {
  320. struct xgene_clk *pclk = to_xgene_clk(hw);
  321. unsigned long parent_rate = *prate;
  322. u32 divider;
  323. if (pclk->param.divider_reg) {
  324. /* Let's compute the divider */
  325. if (rate > parent_rate)
  326. rate = parent_rate;
  327. divider = parent_rate / rate; /* Rounded down */
  328. } else {
  329. divider = 1;
  330. }
  331. return parent_rate / divider;
  332. }
  333. const struct clk_ops xgene_clk_ops = {
  334. .enable = xgene_clk_enable,
  335. .disable = xgene_clk_disable,
  336. .is_enabled = xgene_clk_is_enabled,
  337. .recalc_rate = xgene_clk_recalc_rate,
  338. .set_rate = xgene_clk_set_rate,
  339. .round_rate = xgene_clk_round_rate,
  340. };
  341. static struct clk *xgene_register_clk(struct device *dev,
  342. const char *name, const char *parent_name,
  343. struct xgene_dev_parameters *parameters, spinlock_t *lock)
  344. {
  345. struct xgene_clk *apmclk;
  346. struct clk *clk;
  347. struct clk_init_data init;
  348. int rc;
  349. /* allocate the APM clock structure */
  350. apmclk = kzalloc(sizeof(*apmclk), GFP_KERNEL);
  351. if (!apmclk) {
  352. pr_err("%s: could not allocate APM clk\n", __func__);
  353. return ERR_PTR(-ENOMEM);
  354. }
  355. init.name = name;
  356. init.ops = &xgene_clk_ops;
  357. init.flags = 0;
  358. init.parent_names = parent_name ? &parent_name : NULL;
  359. init.num_parents = parent_name ? 1 : 0;
  360. apmclk->name = name;
  361. apmclk->lock = lock;
  362. apmclk->hw.init = &init;
  363. apmclk->param = *parameters;
  364. /* Register the clock */
  365. clk = clk_register(dev, &apmclk->hw);
  366. if (IS_ERR(clk)) {
  367. pr_err("%s: could not register clk %s\n", __func__, name);
  368. kfree(apmclk);
  369. return clk;
  370. }
  371. /* Register the clock for lookup */
  372. rc = clk_register_clkdev(clk, name, NULL);
  373. if (rc != 0) {
  374. pr_err("%s: could not register lookup clk %s\n",
  375. __func__, name);
  376. }
  377. return clk;
  378. }
  379. static void __init xgene_devclk_init(struct device_node *np)
  380. {
  381. const char *clk_name = np->full_name;
  382. struct clk *clk;
  383. struct resource res;
  384. int rc;
  385. struct xgene_dev_parameters parameters;
  386. int i;
  387. /* Check if the entry is disabled */
  388. if (!of_device_is_available(np))
  389. return;
  390. /* Parse the DTS register for resource */
  391. parameters.csr_reg = NULL;
  392. parameters.divider_reg = NULL;
  393. for (i = 0; i < 2; i++) {
  394. void *map_res;
  395. rc = of_address_to_resource(np, i, &res);
  396. if (rc != 0) {
  397. if (i == 0) {
  398. pr_err("no DTS register for %s\n",
  399. np->full_name);
  400. return;
  401. }
  402. break;
  403. }
  404. map_res = of_iomap(np, i);
  405. if (map_res == NULL) {
  406. pr_err("Unable to map resource %d for %s\n",
  407. i, np->full_name);
  408. goto err;
  409. }
  410. if (strcmp(res.name, "div-reg") == 0)
  411. parameters.divider_reg = map_res;
  412. else /* if (strcmp(res->name, "csr-reg") == 0) */
  413. parameters.csr_reg = map_res;
  414. }
  415. if (of_property_read_u32(np, "csr-offset", &parameters.reg_csr_offset))
  416. parameters.reg_csr_offset = 0;
  417. if (of_property_read_u32(np, "csr-mask", &parameters.reg_csr_mask))
  418. parameters.reg_csr_mask = 0xF;
  419. if (of_property_read_u32(np, "enable-offset",
  420. &parameters.reg_clk_offset))
  421. parameters.reg_clk_offset = 0x8;
  422. if (of_property_read_u32(np, "enable-mask", &parameters.reg_clk_mask))
  423. parameters.reg_clk_mask = 0xF;
  424. if (of_property_read_u32(np, "divider-offset",
  425. &parameters.reg_divider_offset))
  426. parameters.reg_divider_offset = 0;
  427. if (of_property_read_u32(np, "divider-width",
  428. &parameters.reg_divider_width))
  429. parameters.reg_divider_width = 0;
  430. if (of_property_read_u32(np, "divider-shift",
  431. &parameters.reg_divider_shift))
  432. parameters.reg_divider_shift = 0;
  433. of_property_read_string(np, "clock-output-names", &clk_name);
  434. clk = xgene_register_clk(NULL, clk_name,
  435. of_clk_get_parent_name(np, 0), &parameters, &clk_lock);
  436. if (IS_ERR(clk))
  437. goto err;
  438. pr_debug("Add %s clock\n", clk_name);
  439. rc = of_clk_add_provider(np, of_clk_src_simple_get, clk);
  440. if (rc != 0)
  441. pr_err("%s: could register provider clk %s\n", __func__,
  442. np->full_name);
  443. return;
  444. err:
  445. if (parameters.csr_reg)
  446. iounmap(parameters.csr_reg);
  447. if (parameters.divider_reg)
  448. iounmap(parameters.divider_reg);
  449. }
  450. CLK_OF_DECLARE(xgene_socpll_clock, "apm,xgene-socpll-clock", xgene_socpllclk_init);
  451. CLK_OF_DECLARE(xgene_pcppll_clock, "apm,xgene-pcppll-clock", xgene_pcppllclk_init);
  452. CLK_OF_DECLARE(xgene_dev_clock, "apm,xgene-device-clock", xgene_devclk_init);