rcar-gen3-cpg.c 15 KB

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