armada-37xx-periph.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Marvell Armada 37xx SoC Peripheral clocks
  3. *
  4. * Copyright (C) 2016 Marvell
  5. *
  6. * Gregory CLEMENT <gregory.clement@free-electrons.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2 or later. This program is licensed "as is"
  10. * without any warranty of any kind, whether express or implied.
  11. *
  12. * Most of the peripheral clocks can be modelled like this:
  13. * _____ _______ _______
  14. * TBG-A-P --| | | | | | ______
  15. * TBG-B-P --| Mux |--| /div1 |--| /div2 |--| Gate |--> perip_clk
  16. * TBG-A-S --| | | | | | |______|
  17. * TBG-B-S --|_____| |_______| |_______|
  18. *
  19. * However some clocks may use only one or two block or and use the
  20. * xtal clock as parent.
  21. */
  22. #include <linux/clk-provider.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #define TBG_SEL 0x0
  28. #define DIV_SEL0 0x4
  29. #define DIV_SEL1 0x8
  30. #define DIV_SEL2 0xC
  31. #define CLK_SEL 0x10
  32. #define CLK_DIS 0x14
  33. struct clk_periph_driver_data {
  34. struct clk_hw_onecell_data *hw_data;
  35. spinlock_t lock;
  36. };
  37. struct clk_double_div {
  38. struct clk_hw hw;
  39. void __iomem *reg1;
  40. u8 shift1;
  41. void __iomem *reg2;
  42. u8 shift2;
  43. };
  44. #define to_clk_double_div(_hw) container_of(_hw, struct clk_double_div, hw)
  45. struct clk_periph_data {
  46. const char *name;
  47. const char * const *parent_names;
  48. int num_parents;
  49. struct clk_hw *mux_hw;
  50. struct clk_hw *rate_hw;
  51. struct clk_hw *gate_hw;
  52. bool is_double_div;
  53. };
  54. static const struct clk_div_table clk_table6[] = {
  55. { .val = 1, .div = 1, },
  56. { .val = 2, .div = 2, },
  57. { .val = 3, .div = 3, },
  58. { .val = 4, .div = 4, },
  59. { .val = 5, .div = 5, },
  60. { .val = 6, .div = 6, },
  61. { .val = 0, .div = 0, }, /* last entry */
  62. };
  63. static const struct clk_div_table clk_table1[] = {
  64. { .val = 0, .div = 1, },
  65. { .val = 1, .div = 2, },
  66. { .val = 0, .div = 0, }, /* last entry */
  67. };
  68. static const struct clk_div_table clk_table2[] = {
  69. { .val = 0, .div = 2, },
  70. { .val = 1, .div = 4, },
  71. { .val = 0, .div = 0, }, /* last entry */
  72. };
  73. static const struct clk_ops clk_double_div_ops;
  74. #define PERIPH_GATE(_name, _bit) \
  75. struct clk_gate gate_##_name = { \
  76. .reg = (void *)CLK_DIS, \
  77. .bit_idx = _bit, \
  78. .hw.init = &(struct clk_init_data){ \
  79. .ops = &clk_gate_ops, \
  80. } \
  81. };
  82. #define PERIPH_MUX(_name, _shift) \
  83. struct clk_mux mux_##_name = { \
  84. .reg = (void *)TBG_SEL, \
  85. .shift = _shift, \
  86. .mask = 3, \
  87. .hw.init = &(struct clk_init_data){ \
  88. .ops = &clk_mux_ro_ops, \
  89. } \
  90. };
  91. #define PERIPH_DOUBLEDIV(_name, _reg1, _reg2, _shift1, _shift2) \
  92. struct clk_double_div rate_##_name = { \
  93. .reg1 = (void *)_reg1, \
  94. .reg2 = (void *)_reg2, \
  95. .shift1 = _shift1, \
  96. .shift2 = _shift2, \
  97. .hw.init = &(struct clk_init_data){ \
  98. .ops = &clk_double_div_ops, \
  99. } \
  100. };
  101. #define PERIPH_DIV(_name, _reg, _shift, _table) \
  102. struct clk_divider rate_##_name = { \
  103. .reg = (void *)_reg, \
  104. .table = _table, \
  105. .shift = _shift, \
  106. .hw.init = &(struct clk_init_data){ \
  107. .ops = &clk_divider_ro_ops, \
  108. } \
  109. };
  110. #define PERIPH_CLK_FULL_DD(_name, _bit, _shift, _reg1, _reg2, _shift1, _shift2)\
  111. static PERIPH_GATE(_name, _bit); \
  112. static PERIPH_MUX(_name, _shift); \
  113. static PERIPH_DOUBLEDIV(_name, _reg1, _reg2, _shift1, _shift2);
  114. #define PERIPH_CLK_FULL(_name, _bit, _shift, _reg, _shift1, _table) \
  115. static PERIPH_GATE(_name, _bit); \
  116. static PERIPH_MUX(_name, _shift); \
  117. static PERIPH_DIV(_name, _reg, _shift1, _table);
  118. #define PERIPH_CLK_GATE_DIV(_name, _bit, _reg, _shift, _table) \
  119. static PERIPH_GATE(_name, _bit); \
  120. static PERIPH_DIV(_name, _reg, _shift, _table);
  121. #define PERIPH_CLK_MUX_DIV(_name, _shift, _reg, _shift_div, _table) \
  122. static PERIPH_MUX(_name, _shift); \
  123. static PERIPH_DIV(_name, _reg, _shift_div, _table);
  124. #define PERIPH_CLK_MUX_DD(_name, _shift, _reg1, _reg2, _shift1, _shift2)\
  125. static PERIPH_MUX(_name, _shift); \
  126. static PERIPH_DOUBLEDIV(_name, _reg1, _reg2, _shift1, _shift2);
  127. #define REF_CLK_FULL(_name) \
  128. { .name = #_name, \
  129. .parent_names = (const char *[]){ "TBG-A-P", \
  130. "TBG-B-P", "TBG-A-S", "TBG-B-S"}, \
  131. .num_parents = 4, \
  132. .mux_hw = &mux_##_name.hw, \
  133. .gate_hw = &gate_##_name.hw, \
  134. .rate_hw = &rate_##_name.hw, \
  135. }
  136. #define REF_CLK_FULL_DD(_name) \
  137. { .name = #_name, \
  138. .parent_names = (const char *[]){ "TBG-A-P", \
  139. "TBG-B-P", "TBG-A-S", "TBG-B-S"}, \
  140. .num_parents = 4, \
  141. .mux_hw = &mux_##_name.hw, \
  142. .gate_hw = &gate_##_name.hw, \
  143. .rate_hw = &rate_##_name.hw, \
  144. .is_double_div = true, \
  145. }
  146. #define REF_CLK_GATE(_name, _parent_name) \
  147. { .name = #_name, \
  148. .parent_names = (const char *[]){ _parent_name}, \
  149. .num_parents = 1, \
  150. .gate_hw = &gate_##_name.hw, \
  151. }
  152. #define REF_CLK_GATE_DIV(_name, _parent_name) \
  153. { .name = #_name, \
  154. .parent_names = (const char *[]){ _parent_name}, \
  155. .num_parents = 1, \
  156. .gate_hw = &gate_##_name.hw, \
  157. .rate_hw = &rate_##_name.hw, \
  158. }
  159. #define REF_CLK_MUX_DIV(_name) \
  160. { .name = #_name, \
  161. .parent_names = (const char *[]){ "TBG-A-P", \
  162. "TBG-B-P", "TBG-A-S", "TBG-B-S"}, \
  163. .num_parents = 4, \
  164. .mux_hw = &mux_##_name.hw, \
  165. .rate_hw = &rate_##_name.hw, \
  166. }
  167. #define REF_CLK_MUX_DD(_name) \
  168. { .name = #_name, \
  169. .parent_names = (const char *[]){ "TBG-A-P", \
  170. "TBG-B-P", "TBG-A-S", "TBG-B-S"}, \
  171. .num_parents = 4, \
  172. .mux_hw = &mux_##_name.hw, \
  173. .rate_hw = &rate_##_name.hw, \
  174. .is_double_div = true, \
  175. }
  176. /* NB periph clocks */
  177. PERIPH_CLK_FULL_DD(mmc, 2, 0, DIV_SEL2, DIV_SEL2, 16, 13);
  178. PERIPH_CLK_FULL_DD(sata_host, 3, 2, DIV_SEL2, DIV_SEL2, 10, 7);
  179. PERIPH_CLK_FULL_DD(sec_at, 6, 4, DIV_SEL1, DIV_SEL1, 3, 0);
  180. PERIPH_CLK_FULL_DD(sec_dap, 7, 6, DIV_SEL1, DIV_SEL1, 9, 6);
  181. PERIPH_CLK_FULL_DD(tscem, 8, 8, DIV_SEL1, DIV_SEL1, 15, 12);
  182. PERIPH_CLK_FULL(tscem_tmx, 10, 10, DIV_SEL1, 18, clk_table6);
  183. static PERIPH_GATE(avs, 11);
  184. PERIPH_CLK_FULL_DD(pwm, 13, 14, DIV_SEL0, DIV_SEL0, 3, 0);
  185. PERIPH_CLK_FULL_DD(sqf, 12, 12, DIV_SEL1, DIV_SEL1, 27, 24);
  186. static PERIPH_GATE(i2c_2, 16);
  187. static PERIPH_GATE(i2c_1, 17);
  188. PERIPH_CLK_GATE_DIV(ddr_phy, 19, DIV_SEL0, 18, clk_table2);
  189. PERIPH_CLK_FULL_DD(ddr_fclk, 21, 16, DIV_SEL0, DIV_SEL0, 15, 12);
  190. PERIPH_CLK_FULL(trace, 22, 18, DIV_SEL0, 20, clk_table6);
  191. PERIPH_CLK_FULL(counter, 23, 20, DIV_SEL0, 23, clk_table6);
  192. PERIPH_CLK_FULL_DD(eip97, 24, 24, DIV_SEL2, DIV_SEL2, 22, 19);
  193. PERIPH_CLK_MUX_DIV(cpu, 22, DIV_SEL0, 28, clk_table6);
  194. static struct clk_periph_data data_nb[] ={
  195. REF_CLK_FULL_DD(mmc),
  196. REF_CLK_FULL_DD(sata_host),
  197. REF_CLK_FULL_DD(sec_at),
  198. REF_CLK_FULL_DD(sec_dap),
  199. REF_CLK_FULL_DD(tscem),
  200. REF_CLK_FULL(tscem_tmx),
  201. REF_CLK_GATE(avs, "xtal"),
  202. REF_CLK_FULL_DD(sqf),
  203. REF_CLK_FULL_DD(pwm),
  204. REF_CLK_GATE(i2c_2, "xtal"),
  205. REF_CLK_GATE(i2c_1, "xtal"),
  206. REF_CLK_GATE_DIV(ddr_phy, "TBG-A-S"),
  207. REF_CLK_FULL_DD(ddr_fclk),
  208. REF_CLK_FULL(trace),
  209. REF_CLK_FULL(counter),
  210. REF_CLK_FULL_DD(eip97),
  211. REF_CLK_MUX_DIV(cpu),
  212. { },
  213. };
  214. /* SB periph clocks */
  215. PERIPH_CLK_MUX_DD(gbe_50, 6, DIV_SEL2, DIV_SEL2, 6, 9);
  216. PERIPH_CLK_MUX_DD(gbe_core, 8, DIV_SEL1, DIV_SEL1, 18, 21);
  217. PERIPH_CLK_MUX_DD(gbe_125, 10, DIV_SEL1, DIV_SEL1, 6, 9);
  218. static PERIPH_GATE(gbe1_50, 0);
  219. static PERIPH_GATE(gbe0_50, 1);
  220. static PERIPH_GATE(gbe1_125, 2);
  221. static PERIPH_GATE(gbe0_125, 3);
  222. PERIPH_CLK_GATE_DIV(gbe1_core, 4, DIV_SEL1, 13, clk_table1);
  223. PERIPH_CLK_GATE_DIV(gbe0_core, 5, DIV_SEL1, 14, clk_table1);
  224. PERIPH_CLK_GATE_DIV(gbe_bm, 12, DIV_SEL1, 0, clk_table1);
  225. PERIPH_CLK_FULL_DD(sdio, 11, 14, DIV_SEL0, DIV_SEL0, 3, 6);
  226. PERIPH_CLK_FULL_DD(usb32_usb2_sys, 16, 16, DIV_SEL0, DIV_SEL0, 9, 12);
  227. PERIPH_CLK_FULL_DD(usb32_ss_sys, 17, 18, DIV_SEL0, DIV_SEL0, 15, 18);
  228. static struct clk_periph_data data_sb[] = {
  229. REF_CLK_MUX_DD(gbe_50),
  230. REF_CLK_MUX_DD(gbe_core),
  231. REF_CLK_MUX_DD(gbe_125),
  232. REF_CLK_GATE(gbe1_50, "gbe_50"),
  233. REF_CLK_GATE(gbe0_50, "gbe_50"),
  234. REF_CLK_GATE(gbe1_125, "gbe_125"),
  235. REF_CLK_GATE(gbe0_125, "gbe_125"),
  236. REF_CLK_GATE_DIV(gbe1_core, "gbe_core"),
  237. REF_CLK_GATE_DIV(gbe0_core, "gbe_core"),
  238. REF_CLK_GATE_DIV(gbe_bm, "gbe_core"),
  239. REF_CLK_FULL_DD(sdio),
  240. REF_CLK_FULL_DD(usb32_usb2_sys),
  241. REF_CLK_FULL_DD(usb32_ss_sys),
  242. { },
  243. };
  244. static unsigned int get_div(void __iomem *reg, int shift)
  245. {
  246. u32 val;
  247. val = (readl(reg) >> shift) & 0x7;
  248. if (val > 6)
  249. return 0;
  250. return val;
  251. }
  252. static unsigned long clk_double_div_recalc_rate(struct clk_hw *hw,
  253. unsigned long parent_rate)
  254. {
  255. struct clk_double_div *double_div = to_clk_double_div(hw);
  256. unsigned int div;
  257. div = get_div(double_div->reg1, double_div->shift1);
  258. div *= get_div(double_div->reg2, double_div->shift2);
  259. return DIV_ROUND_UP_ULL((u64)parent_rate, div);
  260. }
  261. static const struct clk_ops clk_double_div_ops = {
  262. .recalc_rate = clk_double_div_recalc_rate,
  263. };
  264. static const struct of_device_id armada_3700_periph_clock_of_match[] = {
  265. { .compatible = "marvell,armada-3700-periph-clock-nb",
  266. .data = data_nb, },
  267. { .compatible = "marvell,armada-3700-periph-clock-sb",
  268. .data = data_sb, },
  269. { }
  270. };
  271. static int armada_3700_add_composite_clk(const struct clk_periph_data *data,
  272. void __iomem *reg, spinlock_t *lock,
  273. struct device *dev, struct clk_hw **hw)
  274. {
  275. const struct clk_ops *mux_ops = NULL, *gate_ops = NULL,
  276. *rate_ops = NULL;
  277. struct clk_hw *mux_hw = NULL, *gate_hw = NULL, *rate_hw = NULL;
  278. if (data->mux_hw) {
  279. struct clk_mux *mux;
  280. mux_hw = data->mux_hw;
  281. mux = to_clk_mux(mux_hw);
  282. mux->lock = lock;
  283. mux_ops = mux_hw->init->ops;
  284. mux->reg = reg + (u64)mux->reg;
  285. }
  286. if (data->gate_hw) {
  287. struct clk_gate *gate;
  288. gate_hw = data->gate_hw;
  289. gate = to_clk_gate(gate_hw);
  290. gate->lock = lock;
  291. gate_ops = gate_hw->init->ops;
  292. gate->reg = reg + (u64)gate->reg;
  293. gate->flags = CLK_GATE_SET_TO_DISABLE;
  294. }
  295. if (data->rate_hw) {
  296. rate_hw = data->rate_hw;
  297. rate_ops = rate_hw->init->ops;
  298. if (data->is_double_div) {
  299. struct clk_double_div *rate;
  300. rate = to_clk_double_div(rate_hw);
  301. rate->reg1 = reg + (u64)rate->reg1;
  302. rate->reg2 = reg + (u64)rate->reg2;
  303. } else {
  304. struct clk_divider *rate = to_clk_divider(rate_hw);
  305. const struct clk_div_table *clkt;
  306. int table_size = 0;
  307. rate->reg = reg + (u64)rate->reg;
  308. for (clkt = rate->table; clkt->div; clkt++)
  309. table_size++;
  310. rate->width = order_base_2(table_size);
  311. rate->lock = lock;
  312. }
  313. }
  314. *hw = clk_hw_register_composite(dev, data->name, data->parent_names,
  315. data->num_parents, mux_hw,
  316. mux_ops, rate_hw, rate_ops,
  317. gate_hw, gate_ops, CLK_IGNORE_UNUSED);
  318. if (IS_ERR(*hw))
  319. return PTR_ERR(*hw);
  320. return 0;
  321. }
  322. static int armada_3700_periph_clock_probe(struct platform_device *pdev)
  323. {
  324. struct clk_periph_driver_data *driver_data;
  325. struct device_node *np = pdev->dev.of_node;
  326. const struct clk_periph_data *data;
  327. struct device *dev = &pdev->dev;
  328. int num_periph = 0, i, ret;
  329. struct resource *res;
  330. void __iomem *reg;
  331. data = of_device_get_match_data(dev);
  332. if (!data)
  333. return -ENODEV;
  334. while (data[num_periph].name)
  335. num_periph++;
  336. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  337. reg = devm_ioremap_resource(dev, res);
  338. if (IS_ERR(reg))
  339. return PTR_ERR(reg);
  340. driver_data = devm_kzalloc(dev, sizeof(*driver_data), GFP_KERNEL);
  341. if (!driver_data)
  342. return -ENOMEM;
  343. driver_data->hw_data = devm_kzalloc(dev, sizeof(*driver_data->hw_data) +
  344. sizeof(*driver_data->hw_data->hws) * num_periph,
  345. GFP_KERNEL);
  346. if (!driver_data->hw_data)
  347. return -ENOMEM;
  348. driver_data->hw_data->num = num_periph;
  349. spin_lock_init(&driver_data->lock);
  350. for (i = 0; i < num_periph; i++) {
  351. struct clk_hw **hw = &driver_data->hw_data->hws[i];
  352. if (armada_3700_add_composite_clk(&data[i], reg,
  353. &driver_data->lock, dev, hw))
  354. dev_err(dev, "Can't register periph clock %s\n",
  355. data[i].name);
  356. }
  357. ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get,
  358. driver_data->hw_data);
  359. if (ret) {
  360. for (i = 0; i < num_periph; i++)
  361. clk_hw_unregister(driver_data->hw_data->hws[i]);
  362. return ret;
  363. }
  364. platform_set_drvdata(pdev, driver_data);
  365. return 0;
  366. }
  367. static int armada_3700_periph_clock_remove(struct platform_device *pdev)
  368. {
  369. struct clk_periph_driver_data *data = platform_get_drvdata(pdev);
  370. struct clk_hw_onecell_data *hw_data = data->hw_data;
  371. int i;
  372. of_clk_del_provider(pdev->dev.of_node);
  373. for (i = 0; i < hw_data->num; i++)
  374. clk_hw_unregister(hw_data->hws[i]);
  375. return 0;
  376. }
  377. static struct platform_driver armada_3700_periph_clock_driver = {
  378. .probe = armada_3700_periph_clock_probe,
  379. .remove = armada_3700_periph_clock_remove,
  380. .driver = {
  381. .name = "marvell-armada-3700-periph-clock",
  382. .of_match_table = armada_3700_periph_clock_of_match,
  383. },
  384. };
  385. builtin_platform_driver(armada_3700_periph_clock_driver);