armada-37xx-periph.c 22 KB

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