armada-37xx-periph.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Marvell Armada 37xx SoC Peripheral clocks
  4. *
  5. * Copyright (C) 2016 Marvell
  6. *
  7. * Gregory CLEMENT <gregory.clement@free-electrons.com>
  8. *
  9. * Most of the peripheral clocks can be modelled like this:
  10. * _____ _______ _______
  11. * TBG-A-P --| | | | | | ______
  12. * TBG-B-P --| Mux |--| /div1 |--| /div2 |--| Gate |--> perip_clk
  13. * TBG-A-S --| | | | | | |______|
  14. * TBG-B-S --|_____| |_______| |_______|
  15. *
  16. * However some clocks may use only one or two block or and use the
  17. * xtal clock as parent.
  18. */
  19. #include <linux/clk-provider.h>
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/regmap.h>
  25. #include <linux/slab.h>
  26. #define TBG_SEL 0x0
  27. #define DIV_SEL0 0x4
  28. #define DIV_SEL1 0x8
  29. #define DIV_SEL2 0xC
  30. #define CLK_SEL 0x10
  31. #define CLK_DIS 0x14
  32. #define ARMADA_37XX_DVFS_LOAD_1 1
  33. #define LOAD_LEVEL_NR 4
  34. #define ARMADA_37XX_NB_L0L1 0x18
  35. #define ARMADA_37XX_NB_L2L3 0x1C
  36. #define ARMADA_37XX_NB_TBG_DIV_OFF 13
  37. #define ARMADA_37XX_NB_TBG_DIV_MASK 0x7
  38. #define ARMADA_37XX_NB_CLK_SEL_OFF 11
  39. #define ARMADA_37XX_NB_CLK_SEL_MASK 0x1
  40. #define ARMADA_37XX_NB_TBG_SEL_OFF 9
  41. #define ARMADA_37XX_NB_TBG_SEL_MASK 0x3
  42. #define ARMADA_37XX_NB_CONFIG_SHIFT 16
  43. #define ARMADA_37XX_NB_DYN_MOD 0x24
  44. #define ARMADA_37XX_NB_DFS_EN 31
  45. #define ARMADA_37XX_NB_CPU_LOAD 0x30
  46. #define ARMADA_37XX_NB_CPU_LOAD_MASK 0x3
  47. #define ARMADA_37XX_DVFS_LOAD_0 0
  48. #define ARMADA_37XX_DVFS_LOAD_1 1
  49. #define ARMADA_37XX_DVFS_LOAD_2 2
  50. #define ARMADA_37XX_DVFS_LOAD_3 3
  51. struct clk_periph_driver_data {
  52. struct clk_hw_onecell_data *hw_data;
  53. spinlock_t lock;
  54. };
  55. struct clk_double_div {
  56. struct clk_hw hw;
  57. void __iomem *reg1;
  58. u8 shift1;
  59. void __iomem *reg2;
  60. u8 shift2;
  61. };
  62. struct clk_pm_cpu {
  63. struct clk_hw hw;
  64. void __iomem *reg_mux;
  65. u8 shift_mux;
  66. u32 mask_mux;
  67. void __iomem *reg_div;
  68. u8 shift_div;
  69. struct regmap *nb_pm_base;
  70. unsigned long l1_expiration;
  71. };
  72. #define to_clk_double_div(_hw) container_of(_hw, struct clk_double_div, hw)
  73. #define to_clk_pm_cpu(_hw) container_of(_hw, struct clk_pm_cpu, hw)
  74. struct clk_periph_data {
  75. const char *name;
  76. const char * const *parent_names;
  77. int num_parents;
  78. struct clk_hw *mux_hw;
  79. struct clk_hw *rate_hw;
  80. struct clk_hw *gate_hw;
  81. struct clk_hw *muxrate_hw;
  82. bool is_double_div;
  83. };
  84. static const struct clk_div_table clk_table6[] = {
  85. { .val = 1, .div = 1, },
  86. { .val = 2, .div = 2, },
  87. { .val = 3, .div = 3, },
  88. { .val = 4, .div = 4, },
  89. { .val = 5, .div = 5, },
  90. { .val = 6, .div = 6, },
  91. { .val = 0, .div = 0, }, /* last entry */
  92. };
  93. static const struct clk_div_table clk_table1[] = {
  94. { .val = 0, .div = 1, },
  95. { .val = 1, .div = 2, },
  96. { .val = 0, .div = 0, }, /* last entry */
  97. };
  98. static const struct clk_div_table clk_table2[] = {
  99. { .val = 0, .div = 2, },
  100. { .val = 1, .div = 4, },
  101. { .val = 0, .div = 0, }, /* last entry */
  102. };
  103. static const struct clk_ops clk_double_div_ops;
  104. static const struct clk_ops clk_pm_cpu_ops;
  105. #define PERIPH_GATE(_name, _bit) \
  106. struct clk_gate gate_##_name = { \
  107. .reg = (void *)CLK_DIS, \
  108. .bit_idx = _bit, \
  109. .hw.init = &(struct clk_init_data){ \
  110. .ops = &clk_gate_ops, \
  111. } \
  112. };
  113. #define PERIPH_MUX(_name, _shift) \
  114. struct clk_mux mux_##_name = { \
  115. .reg = (void *)TBG_SEL, \
  116. .shift = _shift, \
  117. .mask = 3, \
  118. .hw.init = &(struct clk_init_data){ \
  119. .ops = &clk_mux_ro_ops, \
  120. } \
  121. };
  122. #define PERIPH_DOUBLEDIV(_name, _reg1, _reg2, _shift1, _shift2) \
  123. struct clk_double_div rate_##_name = { \
  124. .reg1 = (void *)_reg1, \
  125. .reg2 = (void *)_reg2, \
  126. .shift1 = _shift1, \
  127. .shift2 = _shift2, \
  128. .hw.init = &(struct clk_init_data){ \
  129. .ops = &clk_double_div_ops, \
  130. } \
  131. };
  132. #define PERIPH_DIV(_name, _reg, _shift, _table) \
  133. struct clk_divider rate_##_name = { \
  134. .reg = (void *)_reg, \
  135. .table = _table, \
  136. .shift = _shift, \
  137. .hw.init = &(struct clk_init_data){ \
  138. .ops = &clk_divider_ro_ops, \
  139. } \
  140. };
  141. #define PERIPH_PM_CPU(_name, _shift1, _reg, _shift2) \
  142. struct clk_pm_cpu muxrate_##_name = { \
  143. .reg_mux = (void *)TBG_SEL, \
  144. .mask_mux = 3, \
  145. .shift_mux = _shift1, \
  146. .reg_div = (void *)_reg, \
  147. .shift_div = _shift2, \
  148. .hw.init = &(struct clk_init_data){ \
  149. .ops = &clk_pm_cpu_ops, \
  150. } \
  151. };
  152. #define PERIPH_CLK_FULL_DD(_name, _bit, _shift, _reg1, _reg2, _shift1, _shift2)\
  153. static PERIPH_GATE(_name, _bit); \
  154. static PERIPH_MUX(_name, _shift); \
  155. static PERIPH_DOUBLEDIV(_name, _reg1, _reg2, _shift1, _shift2);
  156. #define PERIPH_CLK_FULL(_name, _bit, _shift, _reg, _shift1, _table) \
  157. static PERIPH_GATE(_name, _bit); \
  158. static PERIPH_MUX(_name, _shift); \
  159. static PERIPH_DIV(_name, _reg, _shift1, _table);
  160. #define PERIPH_CLK_GATE_DIV(_name, _bit, _reg, _shift, _table) \
  161. static PERIPH_GATE(_name, _bit); \
  162. static PERIPH_DIV(_name, _reg, _shift, _table);
  163. #define PERIPH_CLK_MUX_DD(_name, _shift, _reg1, _reg2, _shift1, _shift2)\
  164. static PERIPH_MUX(_name, _shift); \
  165. static PERIPH_DOUBLEDIV(_name, _reg1, _reg2, _shift1, _shift2);
  166. #define REF_CLK_FULL(_name) \
  167. { .name = #_name, \
  168. .parent_names = (const char *[]){ "TBG-A-P", \
  169. "TBG-B-P", "TBG-A-S", "TBG-B-S"}, \
  170. .num_parents = 4, \
  171. .mux_hw = &mux_##_name.hw, \
  172. .gate_hw = &gate_##_name.hw, \
  173. .rate_hw = &rate_##_name.hw, \
  174. }
  175. #define REF_CLK_FULL_DD(_name) \
  176. { .name = #_name, \
  177. .parent_names = (const char *[]){ "TBG-A-P", \
  178. "TBG-B-P", "TBG-A-S", "TBG-B-S"}, \
  179. .num_parents = 4, \
  180. .mux_hw = &mux_##_name.hw, \
  181. .gate_hw = &gate_##_name.hw, \
  182. .rate_hw = &rate_##_name.hw, \
  183. .is_double_div = true, \
  184. }
  185. #define REF_CLK_GATE(_name, _parent_name) \
  186. { .name = #_name, \
  187. .parent_names = (const char *[]){ _parent_name}, \
  188. .num_parents = 1, \
  189. .gate_hw = &gate_##_name.hw, \
  190. }
  191. #define REF_CLK_GATE_DIV(_name, _parent_name) \
  192. { .name = #_name, \
  193. .parent_names = (const char *[]){ _parent_name}, \
  194. .num_parents = 1, \
  195. .gate_hw = &gate_##_name.hw, \
  196. .rate_hw = &rate_##_name.hw, \
  197. }
  198. #define REF_CLK_PM_CPU(_name) \
  199. { .name = #_name, \
  200. .parent_names = (const char *[]){ "TBG-A-P", \
  201. "TBG-B-P", "TBG-A-S", "TBG-B-S"}, \
  202. .num_parents = 4, \
  203. .muxrate_hw = &muxrate_##_name.hw, \
  204. }
  205. #define REF_CLK_MUX_DD(_name) \
  206. { .name = #_name, \
  207. .parent_names = (const char *[]){ "TBG-A-P", \
  208. "TBG-B-P", "TBG-A-S", "TBG-B-S"}, \
  209. .num_parents = 4, \
  210. .mux_hw = &mux_##_name.hw, \
  211. .rate_hw = &rate_##_name.hw, \
  212. .is_double_div = true, \
  213. }
  214. /* NB periph clocks */
  215. PERIPH_CLK_FULL_DD(mmc, 2, 0, DIV_SEL2, DIV_SEL2, 16, 13);
  216. PERIPH_CLK_FULL_DD(sata_host, 3, 2, DIV_SEL2, DIV_SEL2, 10, 7);
  217. PERIPH_CLK_FULL_DD(sec_at, 6, 4, DIV_SEL1, DIV_SEL1, 3, 0);
  218. PERIPH_CLK_FULL_DD(sec_dap, 7, 6, DIV_SEL1, DIV_SEL1, 9, 6);
  219. PERIPH_CLK_FULL_DD(tscem, 8, 8, DIV_SEL1, DIV_SEL1, 15, 12);
  220. PERIPH_CLK_FULL(tscem_tmx, 10, 10, DIV_SEL1, 18, clk_table6);
  221. static PERIPH_GATE(avs, 11);
  222. PERIPH_CLK_FULL_DD(pwm, 13, 14, DIV_SEL0, DIV_SEL0, 3, 0);
  223. PERIPH_CLK_FULL_DD(sqf, 12, 12, DIV_SEL1, DIV_SEL1, 27, 24);
  224. static PERIPH_GATE(i2c_2, 16);
  225. static PERIPH_GATE(i2c_1, 17);
  226. PERIPH_CLK_GATE_DIV(ddr_phy, 19, DIV_SEL0, 18, clk_table2);
  227. PERIPH_CLK_FULL_DD(ddr_fclk, 21, 16, DIV_SEL0, DIV_SEL0, 15, 12);
  228. PERIPH_CLK_FULL(trace, 22, 18, DIV_SEL0, 20, clk_table6);
  229. PERIPH_CLK_FULL(counter, 23, 20, DIV_SEL0, 23, clk_table6);
  230. PERIPH_CLK_FULL_DD(eip97, 24, 24, DIV_SEL2, DIV_SEL2, 22, 19);
  231. static PERIPH_PM_CPU(cpu, 22, DIV_SEL0, 28);
  232. static struct clk_periph_data data_nb[] = {
  233. REF_CLK_FULL_DD(mmc),
  234. REF_CLK_FULL_DD(sata_host),
  235. REF_CLK_FULL_DD(sec_at),
  236. REF_CLK_FULL_DD(sec_dap),
  237. REF_CLK_FULL_DD(tscem),
  238. REF_CLK_FULL(tscem_tmx),
  239. REF_CLK_GATE(avs, "xtal"),
  240. REF_CLK_FULL_DD(sqf),
  241. REF_CLK_FULL_DD(pwm),
  242. REF_CLK_GATE(i2c_2, "xtal"),
  243. REF_CLK_GATE(i2c_1, "xtal"),
  244. REF_CLK_GATE_DIV(ddr_phy, "TBG-A-S"),
  245. REF_CLK_FULL_DD(ddr_fclk),
  246. REF_CLK_FULL(trace),
  247. REF_CLK_FULL(counter),
  248. REF_CLK_FULL_DD(eip97),
  249. REF_CLK_PM_CPU(cpu),
  250. { },
  251. };
  252. /* SB periph clocks */
  253. PERIPH_CLK_MUX_DD(gbe_50, 6, DIV_SEL2, DIV_SEL2, 6, 9);
  254. PERIPH_CLK_MUX_DD(gbe_core, 8, DIV_SEL1, DIV_SEL1, 18, 21);
  255. PERIPH_CLK_MUX_DD(gbe_125, 10, DIV_SEL1, DIV_SEL1, 6, 9);
  256. static PERIPH_GATE(gbe1_50, 0);
  257. static PERIPH_GATE(gbe0_50, 1);
  258. static PERIPH_GATE(gbe1_125, 2);
  259. static PERIPH_GATE(gbe0_125, 3);
  260. PERIPH_CLK_GATE_DIV(gbe1_core, 4, DIV_SEL1, 13, clk_table1);
  261. PERIPH_CLK_GATE_DIV(gbe0_core, 5, DIV_SEL1, 14, clk_table1);
  262. PERIPH_CLK_GATE_DIV(gbe_bm, 12, DIV_SEL1, 0, clk_table1);
  263. PERIPH_CLK_FULL_DD(sdio, 11, 14, DIV_SEL0, DIV_SEL0, 3, 6);
  264. PERIPH_CLK_FULL_DD(usb32_usb2_sys, 16, 16, DIV_SEL0, DIV_SEL0, 9, 12);
  265. PERIPH_CLK_FULL_DD(usb32_ss_sys, 17, 18, DIV_SEL0, DIV_SEL0, 15, 18);
  266. static struct clk_periph_data data_sb[] = {
  267. REF_CLK_MUX_DD(gbe_50),
  268. REF_CLK_MUX_DD(gbe_core),
  269. REF_CLK_MUX_DD(gbe_125),
  270. REF_CLK_GATE(gbe1_50, "gbe_50"),
  271. REF_CLK_GATE(gbe0_50, "gbe_50"),
  272. REF_CLK_GATE(gbe1_125, "gbe_125"),
  273. REF_CLK_GATE(gbe0_125, "gbe_125"),
  274. REF_CLK_GATE_DIV(gbe1_core, "gbe_core"),
  275. REF_CLK_GATE_DIV(gbe0_core, "gbe_core"),
  276. REF_CLK_GATE_DIV(gbe_bm, "gbe_core"),
  277. REF_CLK_FULL_DD(sdio),
  278. REF_CLK_FULL_DD(usb32_usb2_sys),
  279. REF_CLK_FULL_DD(usb32_ss_sys),
  280. { },
  281. };
  282. static unsigned int get_div(void __iomem *reg, int shift)
  283. {
  284. u32 val;
  285. val = (readl(reg) >> shift) & 0x7;
  286. if (val > 6)
  287. return 0;
  288. return val;
  289. }
  290. static unsigned long clk_double_div_recalc_rate(struct clk_hw *hw,
  291. unsigned long parent_rate)
  292. {
  293. struct clk_double_div *double_div = to_clk_double_div(hw);
  294. unsigned int div;
  295. div = get_div(double_div->reg1, double_div->shift1);
  296. div *= get_div(double_div->reg2, double_div->shift2);
  297. return DIV_ROUND_UP_ULL((u64)parent_rate, div);
  298. }
  299. static const struct clk_ops clk_double_div_ops = {
  300. .recalc_rate = clk_double_div_recalc_rate,
  301. };
  302. static void armada_3700_pm_dvfs_update_regs(unsigned int load_level,
  303. unsigned int *reg,
  304. unsigned int *offset)
  305. {
  306. if (load_level <= ARMADA_37XX_DVFS_LOAD_1)
  307. *reg = ARMADA_37XX_NB_L0L1;
  308. else
  309. *reg = ARMADA_37XX_NB_L2L3;
  310. if (load_level == ARMADA_37XX_DVFS_LOAD_0 ||
  311. load_level == ARMADA_37XX_DVFS_LOAD_2)
  312. *offset += ARMADA_37XX_NB_CONFIG_SHIFT;
  313. }
  314. static bool armada_3700_pm_dvfs_is_enabled(struct regmap *base)
  315. {
  316. unsigned int val, reg = ARMADA_37XX_NB_DYN_MOD;
  317. if (IS_ERR(base))
  318. return false;
  319. regmap_read(base, reg, &val);
  320. return !!(val & BIT(ARMADA_37XX_NB_DFS_EN));
  321. }
  322. static unsigned int armada_3700_pm_dvfs_get_cpu_div(struct regmap *base)
  323. {
  324. unsigned int reg = ARMADA_37XX_NB_CPU_LOAD;
  325. unsigned int offset = ARMADA_37XX_NB_TBG_DIV_OFF;
  326. unsigned int load_level, div;
  327. /*
  328. * This function is always called after the function
  329. * armada_3700_pm_dvfs_is_enabled, so no need to check again
  330. * if the base is valid.
  331. */
  332. regmap_read(base, reg, &load_level);
  333. /*
  334. * The register and the offset inside this register accessed to
  335. * read the current divider depend on the load level
  336. */
  337. load_level &= ARMADA_37XX_NB_CPU_LOAD_MASK;
  338. armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
  339. regmap_read(base, reg, &div);
  340. return (div >> offset) & ARMADA_37XX_NB_TBG_DIV_MASK;
  341. }
  342. static unsigned int armada_3700_pm_dvfs_get_cpu_parent(struct regmap *base)
  343. {
  344. unsigned int reg = ARMADA_37XX_NB_CPU_LOAD;
  345. unsigned int offset = ARMADA_37XX_NB_TBG_SEL_OFF;
  346. unsigned int load_level, sel;
  347. /*
  348. * This function is always called after the function
  349. * armada_3700_pm_dvfs_is_enabled, so no need to check again
  350. * if the base is valid
  351. */
  352. regmap_read(base, reg, &load_level);
  353. /*
  354. * The register and the offset inside this register accessed to
  355. * read the current divider depend on the load level
  356. */
  357. load_level &= ARMADA_37XX_NB_CPU_LOAD_MASK;
  358. armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
  359. regmap_read(base, reg, &sel);
  360. return (sel >> offset) & ARMADA_37XX_NB_TBG_SEL_MASK;
  361. }
  362. static u8 clk_pm_cpu_get_parent(struct clk_hw *hw)
  363. {
  364. struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
  365. u32 val;
  366. if (armada_3700_pm_dvfs_is_enabled(pm_cpu->nb_pm_base)) {
  367. val = armada_3700_pm_dvfs_get_cpu_parent(pm_cpu->nb_pm_base);
  368. } else {
  369. val = readl(pm_cpu->reg_mux) >> pm_cpu->shift_mux;
  370. val &= pm_cpu->mask_mux;
  371. }
  372. return val;
  373. }
  374. static unsigned long clk_pm_cpu_recalc_rate(struct clk_hw *hw,
  375. unsigned long parent_rate)
  376. {
  377. struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
  378. unsigned int div;
  379. if (armada_3700_pm_dvfs_is_enabled(pm_cpu->nb_pm_base))
  380. div = armada_3700_pm_dvfs_get_cpu_div(pm_cpu->nb_pm_base);
  381. else
  382. div = get_div(pm_cpu->reg_div, pm_cpu->shift_div);
  383. return DIV_ROUND_UP_ULL((u64)parent_rate, div);
  384. }
  385. static long clk_pm_cpu_round_rate(struct clk_hw *hw, unsigned long rate,
  386. unsigned long *parent_rate)
  387. {
  388. struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
  389. struct regmap *base = pm_cpu->nb_pm_base;
  390. unsigned int div = *parent_rate / rate;
  391. unsigned int load_level;
  392. /* only available when DVFS is enabled */
  393. if (!armada_3700_pm_dvfs_is_enabled(base))
  394. return -EINVAL;
  395. for (load_level = 0; load_level < LOAD_LEVEL_NR; load_level++) {
  396. unsigned int reg, val, offset = ARMADA_37XX_NB_TBG_DIV_OFF;
  397. armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
  398. regmap_read(base, reg, &val);
  399. val >>= offset;
  400. val &= ARMADA_37XX_NB_TBG_DIV_MASK;
  401. if (val == div)
  402. /*
  403. * We found a load level matching the target
  404. * divider, switch to this load level and
  405. * return.
  406. */
  407. return *parent_rate / div;
  408. }
  409. /* We didn't find any valid divider */
  410. return -EINVAL;
  411. }
  412. /*
  413. * Workaround when base CPU frequnecy is 1000 or 1200 MHz
  414. *
  415. * Switching the CPU from the L2 or L3 frequencies (250/300 or 200 MHz
  416. * respectively) to L0 frequency (1/1.2 GHz) requires a significant
  417. * amount of time to let VDD stabilize to the appropriate
  418. * voltage. This amount of time is large enough that it cannot be
  419. * covered by the hardware countdown register. Due to this, the CPU
  420. * might start operating at L0 before the voltage is stabilized,
  421. * leading to CPU stalls.
  422. *
  423. * To work around this problem, we prevent switching directly from the
  424. * L2/L3 frequencies to the L0 frequency, and instead switch to the L1
  425. * frequency in-between. The sequence therefore becomes:
  426. * 1. First switch from L2/L3 (200/250/300 MHz) to L1 (500/600 MHz)
  427. * 2. Sleep 20ms for stabling VDD voltage
  428. * 3. Then switch from L1 (500/600 MHz) to L0 (1000/1200 MHz).
  429. */
  430. static void clk_pm_cpu_set_rate_wa(struct clk_pm_cpu *pm_cpu,
  431. unsigned int new_level, unsigned long rate,
  432. struct regmap *base)
  433. {
  434. unsigned int cur_level;
  435. regmap_read(base, ARMADA_37XX_NB_CPU_LOAD, &cur_level);
  436. cur_level &= ARMADA_37XX_NB_CPU_LOAD_MASK;
  437. if (cur_level == new_level)
  438. return;
  439. /*
  440. * System wants to go to L1 on its own. If we are going from L2/L3,
  441. * remember when 20ms will expire. If from L0, set the value so that
  442. * next switch to L0 won't have to wait.
  443. */
  444. if (new_level == ARMADA_37XX_DVFS_LOAD_1) {
  445. if (cur_level == ARMADA_37XX_DVFS_LOAD_0)
  446. pm_cpu->l1_expiration = jiffies;
  447. else
  448. pm_cpu->l1_expiration = jiffies + msecs_to_jiffies(20);
  449. return;
  450. }
  451. /*
  452. * If we are setting to L2/L3, just invalidate L1 expiration time,
  453. * sleeping is not needed.
  454. */
  455. if (rate < 1000*1000*1000)
  456. goto invalidate_l1_exp;
  457. /*
  458. * We are going to L0 with rate >= 1GHz. Check whether we have been at
  459. * L1 for long enough time. If not, go to L1 for 20ms.
  460. */
  461. if (pm_cpu->l1_expiration && jiffies >= pm_cpu->l1_expiration)
  462. goto invalidate_l1_exp;
  463. regmap_update_bits(base, ARMADA_37XX_NB_CPU_LOAD,
  464. ARMADA_37XX_NB_CPU_LOAD_MASK,
  465. ARMADA_37XX_DVFS_LOAD_1);
  466. msleep(20);
  467. invalidate_l1_exp:
  468. pm_cpu->l1_expiration = 0;
  469. }
  470. static int clk_pm_cpu_set_rate(struct clk_hw *hw, unsigned long rate,
  471. unsigned long parent_rate)
  472. {
  473. struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
  474. struct regmap *base = pm_cpu->nb_pm_base;
  475. unsigned int div = parent_rate / rate;
  476. unsigned int load_level;
  477. /* only available when DVFS is enabled */
  478. if (!armada_3700_pm_dvfs_is_enabled(base))
  479. return -EINVAL;
  480. for (load_level = 0; load_level < LOAD_LEVEL_NR; load_level++) {
  481. unsigned int reg, mask, val,
  482. offset = ARMADA_37XX_NB_TBG_DIV_OFF;
  483. armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
  484. regmap_read(base, reg, &val);
  485. val >>= offset;
  486. val &= ARMADA_37XX_NB_TBG_DIV_MASK;
  487. if (val == div) {
  488. /*
  489. * We found a load level matching the target
  490. * divider, switch to this load level and
  491. * return.
  492. */
  493. reg = ARMADA_37XX_NB_CPU_LOAD;
  494. mask = ARMADA_37XX_NB_CPU_LOAD_MASK;
  495. /* Apply workaround when base CPU frequency is 1000 or 1200 MHz */
  496. if (parent_rate >= 1000*1000*1000)
  497. clk_pm_cpu_set_rate_wa(pm_cpu, load_level, rate, base);
  498. regmap_update_bits(base, reg, mask, load_level);
  499. return rate;
  500. }
  501. }
  502. /* We didn't find any valid divider */
  503. return -EINVAL;
  504. }
  505. static const struct clk_ops clk_pm_cpu_ops = {
  506. .get_parent = clk_pm_cpu_get_parent,
  507. .round_rate = clk_pm_cpu_round_rate,
  508. .set_rate = clk_pm_cpu_set_rate,
  509. .recalc_rate = clk_pm_cpu_recalc_rate,
  510. };
  511. static const struct of_device_id armada_3700_periph_clock_of_match[] = {
  512. { .compatible = "marvell,armada-3700-periph-clock-nb",
  513. .data = data_nb, },
  514. { .compatible = "marvell,armada-3700-periph-clock-sb",
  515. .data = data_sb, },
  516. { }
  517. };
  518. static int armada_3700_add_composite_clk(const struct clk_periph_data *data,
  519. void __iomem *reg, spinlock_t *lock,
  520. struct device *dev, struct clk_hw **hw)
  521. {
  522. const struct clk_ops *mux_ops = NULL, *gate_ops = NULL,
  523. *rate_ops = NULL;
  524. struct clk_hw *mux_hw = NULL, *gate_hw = NULL, *rate_hw = NULL;
  525. if (data->mux_hw) {
  526. struct clk_mux *mux;
  527. mux_hw = data->mux_hw;
  528. mux = to_clk_mux(mux_hw);
  529. mux->lock = lock;
  530. mux_ops = mux_hw->init->ops;
  531. mux->reg = reg + (u64)mux->reg;
  532. }
  533. if (data->gate_hw) {
  534. struct clk_gate *gate;
  535. gate_hw = data->gate_hw;
  536. gate = to_clk_gate(gate_hw);
  537. gate->lock = lock;
  538. gate_ops = gate_hw->init->ops;
  539. gate->reg = reg + (u64)gate->reg;
  540. gate->flags = CLK_GATE_SET_TO_DISABLE;
  541. }
  542. if (data->rate_hw) {
  543. rate_hw = data->rate_hw;
  544. rate_ops = rate_hw->init->ops;
  545. if (data->is_double_div) {
  546. struct clk_double_div *rate;
  547. rate = to_clk_double_div(rate_hw);
  548. rate->reg1 = reg + (u64)rate->reg1;
  549. rate->reg2 = reg + (u64)rate->reg2;
  550. } else {
  551. struct clk_divider *rate = to_clk_divider(rate_hw);
  552. const struct clk_div_table *clkt;
  553. int table_size = 0;
  554. rate->reg = reg + (u64)rate->reg;
  555. for (clkt = rate->table; clkt->div; clkt++)
  556. table_size++;
  557. rate->width = order_base_2(table_size);
  558. rate->lock = lock;
  559. }
  560. }
  561. if (data->muxrate_hw) {
  562. struct clk_pm_cpu *pmcpu_clk;
  563. struct clk_hw *muxrate_hw = data->muxrate_hw;
  564. struct regmap *map;
  565. pmcpu_clk = to_clk_pm_cpu(muxrate_hw);
  566. pmcpu_clk->reg_mux = reg + (u64)pmcpu_clk->reg_mux;
  567. pmcpu_clk->reg_div = reg + (u64)pmcpu_clk->reg_div;
  568. mux_hw = muxrate_hw;
  569. rate_hw = muxrate_hw;
  570. mux_ops = muxrate_hw->init->ops;
  571. rate_ops = muxrate_hw->init->ops;
  572. map = syscon_regmap_lookup_by_compatible(
  573. "marvell,armada-3700-nb-pm");
  574. pmcpu_clk->nb_pm_base = map;
  575. }
  576. *hw = clk_hw_register_composite(dev, data->name, data->parent_names,
  577. data->num_parents, mux_hw,
  578. mux_ops, rate_hw, rate_ops,
  579. gate_hw, gate_ops, CLK_IGNORE_UNUSED);
  580. return PTR_ERR_OR_ZERO(*hw);
  581. }
  582. static int armada_3700_periph_clock_probe(struct platform_device *pdev)
  583. {
  584. struct clk_periph_driver_data *driver_data;
  585. struct device_node *np = pdev->dev.of_node;
  586. const struct clk_periph_data *data;
  587. struct device *dev = &pdev->dev;
  588. int num_periph = 0, i, ret;
  589. struct resource *res;
  590. void __iomem *reg;
  591. data = of_device_get_match_data(dev);
  592. if (!data)
  593. return -ENODEV;
  594. while (data[num_periph].name)
  595. num_periph++;
  596. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  597. reg = devm_ioremap_resource(dev, res);
  598. if (IS_ERR(reg))
  599. return PTR_ERR(reg);
  600. driver_data = devm_kzalloc(dev, sizeof(*driver_data), GFP_KERNEL);
  601. if (!driver_data)
  602. return -ENOMEM;
  603. driver_data->hw_data = devm_kzalloc(dev,
  604. struct_size(driver_data->hw_data,
  605. hws, num_periph),
  606. GFP_KERNEL);
  607. if (!driver_data->hw_data)
  608. return -ENOMEM;
  609. driver_data->hw_data->num = num_periph;
  610. spin_lock_init(&driver_data->lock);
  611. for (i = 0; i < num_periph; i++) {
  612. struct clk_hw **hw = &driver_data->hw_data->hws[i];
  613. if (armada_3700_add_composite_clk(&data[i], reg,
  614. &driver_data->lock, dev, hw))
  615. dev_err(dev, "Can't register periph clock %s\n",
  616. data[i].name);
  617. }
  618. ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get,
  619. driver_data->hw_data);
  620. if (ret) {
  621. for (i = 0; i < num_periph; i++)
  622. clk_hw_unregister(driver_data->hw_data->hws[i]);
  623. return ret;
  624. }
  625. platform_set_drvdata(pdev, driver_data);
  626. return 0;
  627. }
  628. static int armada_3700_periph_clock_remove(struct platform_device *pdev)
  629. {
  630. struct clk_periph_driver_data *data = platform_get_drvdata(pdev);
  631. struct clk_hw_onecell_data *hw_data = data->hw_data;
  632. int i;
  633. of_clk_del_provider(pdev->dev.of_node);
  634. for (i = 0; i < hw_data->num; i++)
  635. clk_hw_unregister(hw_data->hws[i]);
  636. return 0;
  637. }
  638. static struct platform_driver armada_3700_periph_clock_driver = {
  639. .probe = armada_3700_periph_clock_probe,
  640. .remove = armada_3700_periph_clock_remove,
  641. .driver = {
  642. .name = "marvell-armada-3700-periph-clock",
  643. .of_match_table = armada_3700_periph_clock_of_match,
  644. },
  645. };
  646. builtin_platform_driver(armada_3700_periph_clock_driver);