rcar-gen3-cpg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * R-Car Gen3 Clock Pulse Generator
  3. *
  4. * Copyright (C) 2015-2016 Glider bvba
  5. *
  6. * Based on clk-rcar-gen3.c
  7. *
  8. * Copyright (C) 2015 Renesas Electronics Corp.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. */
  14. #include <linux/bug.h>
  15. #include <linux/bitfield.h>
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/device.h>
  19. #include <linux/err.h>
  20. #include <linux/init.h>
  21. #include <linux/io.h>
  22. #include <linux/pm.h>
  23. #include <linux/slab.h>
  24. #include <linux/sys_soc.h>
  25. #include "renesas-cpg-mssr.h"
  26. #include "rcar-gen3-cpg.h"
  27. #define CPG_PLL0CR 0x00d8
  28. #define CPG_PLL2CR 0x002c
  29. #define CPG_PLL4CR 0x01f4
  30. struct cpg_simple_notifier {
  31. struct notifier_block nb;
  32. void __iomem *reg;
  33. u32 saved;
  34. };
  35. static int cpg_simple_notifier_call(struct notifier_block *nb,
  36. unsigned long action, void *data)
  37. {
  38. struct cpg_simple_notifier *csn =
  39. container_of(nb, struct cpg_simple_notifier, nb);
  40. switch (action) {
  41. case PM_EVENT_SUSPEND:
  42. csn->saved = readl(csn->reg);
  43. return NOTIFY_OK;
  44. case PM_EVENT_RESUME:
  45. writel(csn->saved, csn->reg);
  46. return NOTIFY_OK;
  47. }
  48. return NOTIFY_DONE;
  49. }
  50. static void cpg_simple_notifier_register(struct raw_notifier_head *notifiers,
  51. struct cpg_simple_notifier *csn)
  52. {
  53. csn->nb.notifier_call = cpg_simple_notifier_call;
  54. raw_notifier_chain_register(notifiers, &csn->nb);
  55. }
  56. /*
  57. * Z Clock & Z2 Clock
  58. *
  59. * Traits of this clock:
  60. * prepare - clk_prepare only ensures that parents are prepared
  61. * enable - clk_enable only ensures that parents are enabled
  62. * rate - rate is adjustable. clk->rate = (parent->rate * mult / 32 ) / 2
  63. * parent - fixed parent. No clk_set_parent support
  64. */
  65. #define CPG_FRQCRB 0x00000004
  66. #define CPG_FRQCRB_KICK BIT(31)
  67. #define CPG_FRQCRC 0x000000e0
  68. #define CPG_FRQCRC_ZFC_MASK GENMASK(12, 8)
  69. #define CPG_FRQCRC_Z2FC_MASK GENMASK(4, 0)
  70. struct cpg_z_clk {
  71. struct clk_hw hw;
  72. void __iomem *reg;
  73. void __iomem *kick_reg;
  74. unsigned long mask;
  75. };
  76. #define to_z_clk(_hw) container_of(_hw, struct cpg_z_clk, hw)
  77. static unsigned long cpg_z_clk_recalc_rate(struct clk_hw *hw,
  78. unsigned long parent_rate)
  79. {
  80. struct cpg_z_clk *zclk = to_z_clk(hw);
  81. unsigned int mult;
  82. u32 val;
  83. val = readl(zclk->reg) & zclk->mask;
  84. mult = 32 - (val >> __ffs(zclk->mask));
  85. /* Factor of 2 is for fixed divider */
  86. return DIV_ROUND_CLOSEST_ULL((u64)parent_rate * mult, 32 * 2);
  87. }
  88. static long cpg_z_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  89. unsigned long *parent_rate)
  90. {
  91. /* Factor of 2 is for fixed divider */
  92. unsigned long prate = *parent_rate / 2;
  93. unsigned int mult;
  94. mult = div_u64(rate * 32ULL, prate);
  95. mult = clamp(mult, 1U, 32U);
  96. return (u64)prate * mult / 32;
  97. }
  98. static int cpg_z_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  99. unsigned long parent_rate)
  100. {
  101. struct cpg_z_clk *zclk = to_z_clk(hw);
  102. unsigned int mult;
  103. unsigned int i;
  104. u32 val, kick;
  105. /* Factor of 2 is for fixed divider */
  106. mult = DIV_ROUND_CLOSEST_ULL(rate * 32ULL * 2, parent_rate);
  107. mult = clamp(mult, 1U, 32U);
  108. if (readl(zclk->kick_reg) & CPG_FRQCRB_KICK)
  109. return -EBUSY;
  110. val = readl(zclk->reg) & ~zclk->mask;
  111. val |= ((32 - mult) << __ffs(zclk->mask)) & zclk->mask;
  112. writel(val, zclk->reg);
  113. /*
  114. * Set KICK bit in FRQCRB to update hardware setting and wait for
  115. * clock change completion.
  116. */
  117. kick = readl(zclk->kick_reg);
  118. kick |= CPG_FRQCRB_KICK;
  119. writel(kick, zclk->kick_reg);
  120. /*
  121. * Note: There is no HW information about the worst case latency.
  122. *
  123. * Using experimental measurements, it seems that no more than
  124. * ~10 iterations are needed, independently of the CPU rate.
  125. * Since this value might be dependent of external xtal rate, pll1
  126. * rate or even the other emulation clocks rate, use 1000 as a
  127. * "super" safe value.
  128. */
  129. for (i = 1000; i; i--) {
  130. if (!(readl(zclk->kick_reg) & CPG_FRQCRB_KICK))
  131. return 0;
  132. cpu_relax();
  133. }
  134. return -ETIMEDOUT;
  135. }
  136. static const struct clk_ops cpg_z_clk_ops = {
  137. .recalc_rate = cpg_z_clk_recalc_rate,
  138. .round_rate = cpg_z_clk_round_rate,
  139. .set_rate = cpg_z_clk_set_rate,
  140. };
  141. static struct clk * __init cpg_z_clk_register(const char *name,
  142. const char *parent_name,
  143. void __iomem *reg,
  144. unsigned long mask)
  145. {
  146. struct clk_init_data init;
  147. struct cpg_z_clk *zclk;
  148. struct clk *clk;
  149. zclk = kzalloc(sizeof(*zclk), GFP_KERNEL);
  150. if (!zclk)
  151. return ERR_PTR(-ENOMEM);
  152. init.name = name;
  153. init.ops = &cpg_z_clk_ops;
  154. init.flags = 0;
  155. init.parent_names = &parent_name;
  156. init.num_parents = 1;
  157. zclk->reg = reg + CPG_FRQCRC;
  158. zclk->kick_reg = reg + CPG_FRQCRB;
  159. zclk->hw.init = &init;
  160. zclk->mask = mask;
  161. clk = clk_register(NULL, &zclk->hw);
  162. if (IS_ERR(clk))
  163. kfree(zclk);
  164. return clk;
  165. }
  166. /*
  167. * SDn Clock
  168. */
  169. #define CPG_SD_STP_HCK BIT(9)
  170. #define CPG_SD_STP_CK BIT(8)
  171. #define CPG_SD_STP_MASK (CPG_SD_STP_HCK | CPG_SD_STP_CK)
  172. #define CPG_SD_FC_MASK (0x7 << 2 | 0x3 << 0)
  173. #define CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) \
  174. { \
  175. .val = ((stp_hck) ? CPG_SD_STP_HCK : 0) | \
  176. ((stp_ck) ? CPG_SD_STP_CK : 0) | \
  177. ((sd_srcfc) << 2) | \
  178. ((sd_fc) << 0), \
  179. .div = (sd_div), \
  180. }
  181. struct sd_div_table {
  182. u32 val;
  183. unsigned int div;
  184. };
  185. struct sd_clock {
  186. struct clk_hw hw;
  187. const struct sd_div_table *div_table;
  188. struct cpg_simple_notifier csn;
  189. unsigned int div_num;
  190. unsigned int div_min;
  191. unsigned int div_max;
  192. unsigned int cur_div_idx;
  193. };
  194. /* SDn divider
  195. * sd_srcfc sd_fc div
  196. * stp_hck stp_ck (div) (div) = sd_srcfc x sd_fc
  197. *-------------------------------------------------------------------
  198. * 0 0 0 (1) 1 (4) 4
  199. * 0 0 1 (2) 1 (4) 8
  200. * 1 0 2 (4) 1 (4) 16
  201. * 1 0 3 (8) 1 (4) 32
  202. * 1 0 4 (16) 1 (4) 64
  203. * 0 0 0 (1) 0 (2) 2
  204. * 0 0 1 (2) 0 (2) 4
  205. * 1 0 2 (4) 0 (2) 8
  206. * 1 0 3 (8) 0 (2) 16
  207. * 1 0 4 (16) 0 (2) 32
  208. */
  209. static const struct sd_div_table cpg_sd_div_table[] = {
  210. /* CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) */
  211. CPG_SD_DIV_TABLE_DATA(0, 0, 0, 1, 4),
  212. CPG_SD_DIV_TABLE_DATA(0, 0, 1, 1, 8),
  213. CPG_SD_DIV_TABLE_DATA(1, 0, 2, 1, 16),
  214. CPG_SD_DIV_TABLE_DATA(1, 0, 3, 1, 32),
  215. CPG_SD_DIV_TABLE_DATA(1, 0, 4, 1, 64),
  216. CPG_SD_DIV_TABLE_DATA(0, 0, 0, 0, 2),
  217. CPG_SD_DIV_TABLE_DATA(0, 0, 1, 0, 4),
  218. CPG_SD_DIV_TABLE_DATA(1, 0, 2, 0, 8),
  219. CPG_SD_DIV_TABLE_DATA(1, 0, 3, 0, 16),
  220. CPG_SD_DIV_TABLE_DATA(1, 0, 4, 0, 32),
  221. };
  222. #define to_sd_clock(_hw) container_of(_hw, struct sd_clock, hw)
  223. static int cpg_sd_clock_enable(struct clk_hw *hw)
  224. {
  225. struct sd_clock *clock = to_sd_clock(hw);
  226. u32 val = readl(clock->csn.reg);
  227. val &= ~(CPG_SD_STP_MASK);
  228. val |= clock->div_table[clock->cur_div_idx].val & CPG_SD_STP_MASK;
  229. writel(val, clock->csn.reg);
  230. return 0;
  231. }
  232. static void cpg_sd_clock_disable(struct clk_hw *hw)
  233. {
  234. struct sd_clock *clock = to_sd_clock(hw);
  235. writel(readl(clock->csn.reg) | CPG_SD_STP_MASK, clock->csn.reg);
  236. }
  237. static int cpg_sd_clock_is_enabled(struct clk_hw *hw)
  238. {
  239. struct sd_clock *clock = to_sd_clock(hw);
  240. return !(readl(clock->csn.reg) & CPG_SD_STP_MASK);
  241. }
  242. static unsigned long cpg_sd_clock_recalc_rate(struct clk_hw *hw,
  243. unsigned long parent_rate)
  244. {
  245. struct sd_clock *clock = to_sd_clock(hw);
  246. return DIV_ROUND_CLOSEST(parent_rate,
  247. clock->div_table[clock->cur_div_idx].div);
  248. }
  249. static unsigned int cpg_sd_clock_calc_div(struct sd_clock *clock,
  250. unsigned long rate,
  251. unsigned long parent_rate)
  252. {
  253. unsigned int div;
  254. if (!rate)
  255. rate = 1;
  256. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  257. return clamp_t(unsigned int, div, clock->div_min, clock->div_max);
  258. }
  259. static long cpg_sd_clock_round_rate(struct clk_hw *hw, unsigned long rate,
  260. unsigned long *parent_rate)
  261. {
  262. struct sd_clock *clock = to_sd_clock(hw);
  263. unsigned int div = cpg_sd_clock_calc_div(clock, rate, *parent_rate);
  264. return DIV_ROUND_CLOSEST(*parent_rate, div);
  265. }
  266. static int cpg_sd_clock_set_rate(struct clk_hw *hw, unsigned long rate,
  267. unsigned long parent_rate)
  268. {
  269. struct sd_clock *clock = to_sd_clock(hw);
  270. unsigned int div = cpg_sd_clock_calc_div(clock, rate, parent_rate);
  271. u32 val;
  272. unsigned int i;
  273. for (i = 0; i < clock->div_num; i++)
  274. if (div == clock->div_table[i].div)
  275. break;
  276. if (i >= clock->div_num)
  277. return -EINVAL;
  278. clock->cur_div_idx = i;
  279. val = readl(clock->csn.reg);
  280. val &= ~(CPG_SD_STP_MASK | CPG_SD_FC_MASK);
  281. val |= clock->div_table[i].val & (CPG_SD_STP_MASK | CPG_SD_FC_MASK);
  282. writel(val, clock->csn.reg);
  283. return 0;
  284. }
  285. static const struct clk_ops cpg_sd_clock_ops = {
  286. .enable = cpg_sd_clock_enable,
  287. .disable = cpg_sd_clock_disable,
  288. .is_enabled = cpg_sd_clock_is_enabled,
  289. .recalc_rate = cpg_sd_clock_recalc_rate,
  290. .round_rate = cpg_sd_clock_round_rate,
  291. .set_rate = cpg_sd_clock_set_rate,
  292. };
  293. static struct clk * __init cpg_sd_clk_register(const struct cpg_core_clk *core,
  294. void __iomem *base, const char *parent_name,
  295. struct raw_notifier_head *notifiers)
  296. {
  297. struct clk_init_data init;
  298. struct sd_clock *clock;
  299. struct clk *clk;
  300. unsigned int i;
  301. u32 val;
  302. clock = kzalloc(sizeof(*clock), GFP_KERNEL);
  303. if (!clock)
  304. return ERR_PTR(-ENOMEM);
  305. init.name = core->name;
  306. init.ops = &cpg_sd_clock_ops;
  307. init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
  308. init.parent_names = &parent_name;
  309. init.num_parents = 1;
  310. clock->csn.reg = base + core->offset;
  311. clock->hw.init = &init;
  312. clock->div_table = cpg_sd_div_table;
  313. clock->div_num = ARRAY_SIZE(cpg_sd_div_table);
  314. val = readl(clock->csn.reg) & ~CPG_SD_FC_MASK;
  315. val |= CPG_SD_STP_MASK | (clock->div_table[0].val & CPG_SD_FC_MASK);
  316. writel(val, clock->csn.reg);
  317. clock->div_max = clock->div_table[0].div;
  318. clock->div_min = clock->div_max;
  319. for (i = 1; i < clock->div_num; i++) {
  320. clock->div_max = max(clock->div_max, clock->div_table[i].div);
  321. clock->div_min = min(clock->div_min, clock->div_table[i].div);
  322. }
  323. clk = clk_register(NULL, &clock->hw);
  324. if (IS_ERR(clk))
  325. goto free_clock;
  326. cpg_simple_notifier_register(notifiers, &clock->csn);
  327. return clk;
  328. free_clock:
  329. kfree(clock);
  330. return clk;
  331. }
  332. static const struct rcar_gen3_cpg_pll_config *cpg_pll_config __initdata;
  333. static unsigned int cpg_clk_extalr __initdata;
  334. static u32 cpg_mode __initdata;
  335. static u32 cpg_quirks __initdata;
  336. #define PLL_ERRATA BIT(0) /* Missing PLL0/2/4 post-divider */
  337. #define RCKCR_CKSEL BIT(1) /* Manual RCLK parent selection */
  338. static const struct soc_device_attribute cpg_quirks_match[] __initconst = {
  339. {
  340. .soc_id = "r8a7795", .revision = "ES1.0",
  341. .data = (void *)(PLL_ERRATA | RCKCR_CKSEL),
  342. },
  343. {
  344. .soc_id = "r8a7795", .revision = "ES1.*",
  345. .data = (void *)RCKCR_CKSEL,
  346. },
  347. {
  348. .soc_id = "r8a7796", .revision = "ES1.0",
  349. .data = (void *)RCKCR_CKSEL,
  350. },
  351. { /* sentinel */ }
  352. };
  353. struct clk * __init rcar_gen3_cpg_clk_register(struct device *dev,
  354. const struct cpg_core_clk *core, const struct cpg_mssr_info *info,
  355. struct clk **clks, void __iomem *base,
  356. struct raw_notifier_head *notifiers)
  357. {
  358. const struct clk *parent;
  359. unsigned int mult = 1;
  360. unsigned int div = 1;
  361. u32 value;
  362. parent = clks[core->parent & 0xffff]; /* CLK_TYPE_PE uses high bits */
  363. if (IS_ERR(parent))
  364. return ERR_CAST(parent);
  365. switch (core->type) {
  366. case CLK_TYPE_GEN3_MAIN:
  367. div = cpg_pll_config->extal_div;
  368. break;
  369. case CLK_TYPE_GEN3_PLL0:
  370. /*
  371. * PLL0 is a configurable multiplier clock. Register it as a
  372. * fixed factor clock for now as there's no generic multiplier
  373. * clock implementation and we currently have no need to change
  374. * the multiplier value.
  375. */
  376. value = readl(base + CPG_PLL0CR);
  377. mult = (((value >> 24) & 0x7f) + 1) * 2;
  378. if (cpg_quirks & PLL_ERRATA)
  379. mult *= 2;
  380. break;
  381. case CLK_TYPE_GEN3_PLL1:
  382. mult = cpg_pll_config->pll1_mult;
  383. div = cpg_pll_config->pll1_div;
  384. break;
  385. case CLK_TYPE_GEN3_PLL2:
  386. /*
  387. * PLL2 is a configurable multiplier clock. Register it as a
  388. * fixed factor clock for now as there's no generic multiplier
  389. * clock implementation and we currently have no need to change
  390. * the multiplier value.
  391. */
  392. value = readl(base + CPG_PLL2CR);
  393. mult = (((value >> 24) & 0x7f) + 1) * 2;
  394. if (cpg_quirks & PLL_ERRATA)
  395. mult *= 2;
  396. break;
  397. case CLK_TYPE_GEN3_PLL3:
  398. mult = cpg_pll_config->pll3_mult;
  399. div = cpg_pll_config->pll3_div;
  400. break;
  401. case CLK_TYPE_GEN3_PLL4:
  402. /*
  403. * PLL4 is a configurable multiplier clock. Register it as a
  404. * fixed factor clock for now as there's no generic multiplier
  405. * clock implementation and we currently have no need to change
  406. * the multiplier value.
  407. */
  408. value = readl(base + CPG_PLL4CR);
  409. mult = (((value >> 24) & 0x7f) + 1) * 2;
  410. if (cpg_quirks & PLL_ERRATA)
  411. mult *= 2;
  412. break;
  413. case CLK_TYPE_GEN3_SD:
  414. return cpg_sd_clk_register(core, base, __clk_get_name(parent),
  415. notifiers);
  416. case CLK_TYPE_GEN3_R:
  417. if (cpg_quirks & RCKCR_CKSEL) {
  418. struct cpg_simple_notifier *csn;
  419. csn = kzalloc(sizeof(*csn), GFP_KERNEL);
  420. if (!csn)
  421. return ERR_PTR(-ENOMEM);
  422. csn->reg = base + CPG_RCKCR;
  423. /*
  424. * RINT is default.
  425. * Only if EXTALR is populated, we switch to it.
  426. */
  427. value = readl(csn->reg) & 0x3f;
  428. if (clk_get_rate(clks[cpg_clk_extalr])) {
  429. parent = clks[cpg_clk_extalr];
  430. value |= BIT(15);
  431. }
  432. writel(value, csn->reg);
  433. cpg_simple_notifier_register(notifiers, csn);
  434. break;
  435. }
  436. /* Select parent clock of RCLK by MD28 */
  437. if (cpg_mode & BIT(28))
  438. parent = clks[cpg_clk_extalr];
  439. break;
  440. case CLK_TYPE_GEN3_PE:
  441. /*
  442. * Peripheral clock with a fixed divider, selectable between
  443. * clean and spread spectrum parents using MD12
  444. */
  445. if (cpg_mode & BIT(12)) {
  446. /* Clean */
  447. div = core->div & 0xffff;
  448. } else {
  449. /* SCCG */
  450. parent = clks[core->parent >> 16];
  451. if (IS_ERR(parent))
  452. return ERR_CAST(parent);
  453. div = core->div >> 16;
  454. }
  455. mult = 1;
  456. break;
  457. case CLK_TYPE_GEN3_Z:
  458. return cpg_z_clk_register(core->name, __clk_get_name(parent),
  459. base, CPG_FRQCRC_ZFC_MASK);
  460. case CLK_TYPE_GEN3_Z2:
  461. return cpg_z_clk_register(core->name, __clk_get_name(parent),
  462. base, CPG_FRQCRC_Z2FC_MASK);
  463. default:
  464. return ERR_PTR(-EINVAL);
  465. }
  466. return clk_register_fixed_factor(NULL, core->name,
  467. __clk_get_name(parent), 0, mult, div);
  468. }
  469. int __init rcar_gen3_cpg_init(const struct rcar_gen3_cpg_pll_config *config,
  470. unsigned int clk_extalr, u32 mode)
  471. {
  472. const struct soc_device_attribute *attr;
  473. cpg_pll_config = config;
  474. cpg_clk_extalr = clk_extalr;
  475. cpg_mode = mode;
  476. attr = soc_device_match(cpg_quirks_match);
  477. if (attr)
  478. cpg_quirks = (uintptr_t)attr->data;
  479. pr_debug("%s: mode = 0x%x quirks = 0x%x\n", __func__, mode, cpg_quirks);
  480. return 0;
  481. }