clk-hix5hd2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (c) 2014 Linaro Ltd.
  3. * Copyright (c) 2014 Hisilicon Limited.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. */
  9. #include <linux/of_address.h>
  10. #include <dt-bindings/clock/hix5hd2-clock.h>
  11. #include <linux/slab.h>
  12. #include <linux/delay.h>
  13. #include "clk.h"
  14. static struct hisi_fixed_rate_clock hix5hd2_fixed_rate_clks[] __initdata = {
  15. { HIX5HD2_FIXED_1200M, "1200m", NULL, CLK_IS_ROOT, 1200000000, },
  16. { HIX5HD2_FIXED_400M, "400m", NULL, CLK_IS_ROOT, 400000000, },
  17. { HIX5HD2_FIXED_48M, "48m", NULL, CLK_IS_ROOT, 48000000, },
  18. { HIX5HD2_FIXED_24M, "24m", NULL, CLK_IS_ROOT, 24000000, },
  19. { HIX5HD2_FIXED_600M, "600m", NULL, CLK_IS_ROOT, 600000000, },
  20. { HIX5HD2_FIXED_300M, "300m", NULL, CLK_IS_ROOT, 300000000, },
  21. { HIX5HD2_FIXED_75M, "75m", NULL, CLK_IS_ROOT, 75000000, },
  22. { HIX5HD2_FIXED_200M, "200m", NULL, CLK_IS_ROOT, 200000000, },
  23. { HIX5HD2_FIXED_100M, "100m", NULL, CLK_IS_ROOT, 100000000, },
  24. { HIX5HD2_FIXED_40M, "40m", NULL, CLK_IS_ROOT, 40000000, },
  25. { HIX5HD2_FIXED_150M, "150m", NULL, CLK_IS_ROOT, 150000000, },
  26. { HIX5HD2_FIXED_1728M, "1728m", NULL, CLK_IS_ROOT, 1728000000, },
  27. { HIX5HD2_FIXED_28P8M, "28p8m", NULL, CLK_IS_ROOT, 28000000, },
  28. { HIX5HD2_FIXED_432M, "432m", NULL, CLK_IS_ROOT, 432000000, },
  29. { HIX5HD2_FIXED_345P6M, "345p6m", NULL, CLK_IS_ROOT, 345000000, },
  30. { HIX5HD2_FIXED_288M, "288m", NULL, CLK_IS_ROOT, 288000000, },
  31. { HIX5HD2_FIXED_60M, "60m", NULL, CLK_IS_ROOT, 60000000, },
  32. { HIX5HD2_FIXED_750M, "750m", NULL, CLK_IS_ROOT, 750000000, },
  33. { HIX5HD2_FIXED_500M, "500m", NULL, CLK_IS_ROOT, 500000000, },
  34. { HIX5HD2_FIXED_54M, "54m", NULL, CLK_IS_ROOT, 54000000, },
  35. { HIX5HD2_FIXED_27M, "27m", NULL, CLK_IS_ROOT, 27000000, },
  36. { HIX5HD2_FIXED_1500M, "1500m", NULL, CLK_IS_ROOT, 1500000000, },
  37. { HIX5HD2_FIXED_375M, "375m", NULL, CLK_IS_ROOT, 375000000, },
  38. { HIX5HD2_FIXED_187M, "187m", NULL, CLK_IS_ROOT, 187000000, },
  39. { HIX5HD2_FIXED_250M, "250m", NULL, CLK_IS_ROOT, 250000000, },
  40. { HIX5HD2_FIXED_125M, "125m", NULL, CLK_IS_ROOT, 125000000, },
  41. { HIX5HD2_FIXED_2P02M, "2m", NULL, CLK_IS_ROOT, 2000000, },
  42. { HIX5HD2_FIXED_50M, "50m", NULL, CLK_IS_ROOT, 50000000, },
  43. { HIX5HD2_FIXED_25M, "25m", NULL, CLK_IS_ROOT, 25000000, },
  44. { HIX5HD2_FIXED_83M, "83m", NULL, CLK_IS_ROOT, 83333333, },
  45. };
  46. static const char *const sfc_mux_p[] __initconst = {
  47. "24m", "150m", "200m", "100m", "75m", };
  48. static u32 sfc_mux_table[] = {0, 4, 5, 6, 7};
  49. static const char *const sdio_mux_p[] __initconst = {
  50. "75m", "100m", "50m", "15m", };
  51. static u32 sdio_mux_table[] = {0, 1, 2, 3};
  52. static const char *const fephy_mux_p[] __initconst = { "25m", "125m"};
  53. static u32 fephy_mux_table[] = {0, 1};
  54. static struct hisi_mux_clock hix5hd2_mux_clks[] __initdata = {
  55. { HIX5HD2_SFC_MUX, "sfc_mux", sfc_mux_p, ARRAY_SIZE(sfc_mux_p),
  56. CLK_SET_RATE_PARENT, 0x5c, 8, 3, 0, sfc_mux_table, },
  57. { HIX5HD2_MMC_MUX, "mmc_mux", sdio_mux_p, ARRAY_SIZE(sdio_mux_p),
  58. CLK_SET_RATE_PARENT, 0xa0, 8, 2, 0, sdio_mux_table, },
  59. { HIX5HD2_SD_MUX, "sd_mux", sdio_mux_p, ARRAY_SIZE(sdio_mux_p),
  60. CLK_SET_RATE_PARENT, 0x9c, 8, 2, 0, sdio_mux_table, },
  61. { HIX5HD2_FEPHY_MUX, "fephy_mux",
  62. fephy_mux_p, ARRAY_SIZE(fephy_mux_p),
  63. CLK_SET_RATE_PARENT, 0x120, 8, 2, 0, fephy_mux_table, },
  64. };
  65. static struct hisi_gate_clock hix5hd2_gate_clks[] __initdata = {
  66. /* sfc */
  67. { HIX5HD2_SFC_CLK, "clk_sfc", "sfc_mux",
  68. CLK_SET_RATE_PARENT, 0x5c, 0, 0, },
  69. { HIX5HD2_SFC_RST, "rst_sfc", "clk_sfc",
  70. CLK_SET_RATE_PARENT, 0x5c, 4, CLK_GATE_SET_TO_DISABLE, },
  71. /* sdio0 */
  72. { HIX5HD2_SD_BIU_CLK, "clk_sd_biu", "200m",
  73. CLK_SET_RATE_PARENT, 0x9c, 0, 0, },
  74. { HIX5HD2_SD_CIU_CLK, "clk_sd_ciu", "sd_mux",
  75. CLK_SET_RATE_PARENT, 0x9c, 1, 0, },
  76. { HIX5HD2_SD_CIU_RST, "rst_sd_ciu", "clk_sd_ciu",
  77. CLK_SET_RATE_PARENT, 0x9c, 4, CLK_GATE_SET_TO_DISABLE, },
  78. /* sdio1 */
  79. { HIX5HD2_MMC_BIU_CLK, "clk_mmc_biu", "200m",
  80. CLK_SET_RATE_PARENT, 0xa0, 0, 0, },
  81. { HIX5HD2_MMC_CIU_CLK, "clk_mmc_ciu", "mmc_mux",
  82. CLK_SET_RATE_PARENT, 0xa0, 1, 0, },
  83. { HIX5HD2_MMC_CIU_RST, "rst_mmc_ciu", "clk_mmc_ciu",
  84. CLK_SET_RATE_PARENT, 0xa0, 4, CLK_GATE_SET_TO_DISABLE, },
  85. /* gsf */
  86. { HIX5HD2_FWD_BUS_CLK, "clk_fwd_bus", NULL, 0, 0xcc, 0, 0, },
  87. { HIX5HD2_FWD_SYS_CLK, "clk_fwd_sys", "clk_fwd_bus", 0, 0xcc, 5, 0, },
  88. { HIX5HD2_MAC0_PHY_CLK, "clk_fephy", "clk_fwd_sys",
  89. CLK_SET_RATE_PARENT, 0x120, 0, 0, },
  90. /* wdg0 */
  91. { HIX5HD2_WDG0_CLK, "clk_wdg0", "24m",
  92. CLK_SET_RATE_PARENT, 0x178, 0, 0, },
  93. { HIX5HD2_WDG0_RST, "rst_wdg0", "clk_wdg0",
  94. CLK_SET_RATE_PARENT, 0x178, 4, CLK_GATE_SET_TO_DISABLE, },
  95. /* I2C */
  96. {HIX5HD2_I2C0_CLK, "clk_i2c0", "100m",
  97. CLK_SET_RATE_PARENT, 0x06c, 4, 0, },
  98. {HIX5HD2_I2C0_RST, "rst_i2c0", "clk_i2c0",
  99. CLK_SET_RATE_PARENT, 0x06c, 5, CLK_GATE_SET_TO_DISABLE, },
  100. {HIX5HD2_I2C1_CLK, "clk_i2c1", "100m",
  101. CLK_SET_RATE_PARENT, 0x06c, 8, 0, },
  102. {HIX5HD2_I2C1_RST, "rst_i2c1", "clk_i2c1",
  103. CLK_SET_RATE_PARENT, 0x06c, 9, CLK_GATE_SET_TO_DISABLE, },
  104. {HIX5HD2_I2C2_CLK, "clk_i2c2", "100m",
  105. CLK_SET_RATE_PARENT, 0x06c, 12, 0, },
  106. {HIX5HD2_I2C2_RST, "rst_i2c2", "clk_i2c2",
  107. CLK_SET_RATE_PARENT, 0x06c, 13, CLK_GATE_SET_TO_DISABLE, },
  108. {HIX5HD2_I2C3_CLK, "clk_i2c3", "100m",
  109. CLK_SET_RATE_PARENT, 0x06c, 16, 0, },
  110. {HIX5HD2_I2C3_RST, "rst_i2c3", "clk_i2c3",
  111. CLK_SET_RATE_PARENT, 0x06c, 17, CLK_GATE_SET_TO_DISABLE, },
  112. {HIX5HD2_I2C4_CLK, "clk_i2c4", "100m",
  113. CLK_SET_RATE_PARENT, 0x06c, 20, 0, },
  114. {HIX5HD2_I2C4_RST, "rst_i2c4", "clk_i2c4",
  115. CLK_SET_RATE_PARENT, 0x06c, 21, CLK_GATE_SET_TO_DISABLE, },
  116. {HIX5HD2_I2C5_CLK, "clk_i2c5", "100m",
  117. CLK_SET_RATE_PARENT, 0x06c, 0, 0, },
  118. {HIX5HD2_I2C5_RST, "rst_i2c5", "clk_i2c5",
  119. CLK_SET_RATE_PARENT, 0x06c, 1, CLK_GATE_SET_TO_DISABLE, },
  120. };
  121. enum hix5hd2_clk_type {
  122. TYPE_COMPLEX,
  123. TYPE_ETHER,
  124. };
  125. struct hix5hd2_complex_clock {
  126. const char *name;
  127. const char *parent_name;
  128. u32 id;
  129. u32 ctrl_reg;
  130. u32 ctrl_clk_mask;
  131. u32 ctrl_rst_mask;
  132. u32 phy_reg;
  133. u32 phy_clk_mask;
  134. u32 phy_rst_mask;
  135. enum hix5hd2_clk_type type;
  136. };
  137. struct hix5hd2_clk_complex {
  138. struct clk_hw hw;
  139. u32 id;
  140. void __iomem *ctrl_reg;
  141. u32 ctrl_clk_mask;
  142. u32 ctrl_rst_mask;
  143. void __iomem *phy_reg;
  144. u32 phy_clk_mask;
  145. u32 phy_rst_mask;
  146. };
  147. static struct hix5hd2_complex_clock hix5hd2_complex_clks[] __initdata = {
  148. {"clk_mac0", "clk_fephy", HIX5HD2_MAC0_CLK,
  149. 0xcc, 0xa, 0x500, 0x120, 0, 0x10, TYPE_ETHER},
  150. {"clk_mac1", "clk_fwd_sys", HIX5HD2_MAC1_CLK,
  151. 0xcc, 0x14, 0xa00, 0x168, 0x2, 0, TYPE_ETHER},
  152. {"clk_sata", NULL, HIX5HD2_SATA_CLK,
  153. 0xa8, 0x1f, 0x300, 0xac, 0x1, 0x0, TYPE_COMPLEX},
  154. {"clk_usb", NULL, HIX5HD2_USB_CLK,
  155. 0xb8, 0xff, 0x3f000, 0xbc, 0x7, 0x3f00, TYPE_COMPLEX},
  156. };
  157. #define to_complex_clk(_hw) container_of(_hw, struct hix5hd2_clk_complex, hw)
  158. static int clk_ether_prepare(struct clk_hw *hw)
  159. {
  160. struct hix5hd2_clk_complex *clk = to_complex_clk(hw);
  161. u32 val;
  162. val = readl_relaxed(clk->ctrl_reg);
  163. val |= clk->ctrl_clk_mask | clk->ctrl_rst_mask;
  164. writel_relaxed(val, clk->ctrl_reg);
  165. val &= ~(clk->ctrl_rst_mask);
  166. writel_relaxed(val, clk->ctrl_reg);
  167. val = readl_relaxed(clk->phy_reg);
  168. val |= clk->phy_clk_mask;
  169. val &= ~(clk->phy_rst_mask);
  170. writel_relaxed(val, clk->phy_reg);
  171. mdelay(10);
  172. val &= ~(clk->phy_clk_mask);
  173. val |= clk->phy_rst_mask;
  174. writel_relaxed(val, clk->phy_reg);
  175. mdelay(10);
  176. val |= clk->phy_clk_mask;
  177. val &= ~(clk->phy_rst_mask);
  178. writel_relaxed(val, clk->phy_reg);
  179. mdelay(30);
  180. return 0;
  181. }
  182. static void clk_ether_unprepare(struct clk_hw *hw)
  183. {
  184. struct hix5hd2_clk_complex *clk = to_complex_clk(hw);
  185. u32 val;
  186. val = readl_relaxed(clk->ctrl_reg);
  187. val &= ~(clk->ctrl_clk_mask);
  188. writel_relaxed(val, clk->ctrl_reg);
  189. }
  190. static struct clk_ops clk_ether_ops = {
  191. .prepare = clk_ether_prepare,
  192. .unprepare = clk_ether_unprepare,
  193. };
  194. static int clk_complex_enable(struct clk_hw *hw)
  195. {
  196. struct hix5hd2_clk_complex *clk = to_complex_clk(hw);
  197. u32 val;
  198. val = readl_relaxed(clk->ctrl_reg);
  199. val |= clk->ctrl_clk_mask;
  200. val &= ~(clk->ctrl_rst_mask);
  201. writel_relaxed(val, clk->ctrl_reg);
  202. val = readl_relaxed(clk->phy_reg);
  203. val |= clk->phy_clk_mask;
  204. val &= ~(clk->phy_rst_mask);
  205. writel_relaxed(val, clk->phy_reg);
  206. return 0;
  207. }
  208. static void clk_complex_disable(struct clk_hw *hw)
  209. {
  210. struct hix5hd2_clk_complex *clk = to_complex_clk(hw);
  211. u32 val;
  212. val = readl_relaxed(clk->ctrl_reg);
  213. val |= clk->ctrl_rst_mask;
  214. val &= ~(clk->ctrl_clk_mask);
  215. writel_relaxed(val, clk->ctrl_reg);
  216. val = readl_relaxed(clk->phy_reg);
  217. val |= clk->phy_rst_mask;
  218. val &= ~(clk->phy_clk_mask);
  219. writel_relaxed(val, clk->phy_reg);
  220. }
  221. static struct clk_ops clk_complex_ops = {
  222. .enable = clk_complex_enable,
  223. .disable = clk_complex_disable,
  224. };
  225. static void __init
  226. hix5hd2_clk_register_complex(struct hix5hd2_complex_clock *clks, int nums,
  227. struct hisi_clock_data *data)
  228. {
  229. void __iomem *base = data->base;
  230. int i;
  231. for (i = 0; i < nums; i++) {
  232. struct hix5hd2_clk_complex *p_clk;
  233. struct clk *clk;
  234. struct clk_init_data init;
  235. p_clk = kzalloc(sizeof(*p_clk), GFP_KERNEL);
  236. if (!p_clk)
  237. return;
  238. init.name = clks[i].name;
  239. if (clks[i].type == TYPE_ETHER)
  240. init.ops = &clk_ether_ops;
  241. else
  242. init.ops = &clk_complex_ops;
  243. init.flags = CLK_IS_BASIC;
  244. init.parent_names =
  245. (clks[i].parent_name ? &clks[i].parent_name : NULL);
  246. init.num_parents = (clks[i].parent_name ? 1 : 0);
  247. p_clk->ctrl_reg = base + clks[i].ctrl_reg;
  248. p_clk->ctrl_clk_mask = clks[i].ctrl_clk_mask;
  249. p_clk->ctrl_rst_mask = clks[i].ctrl_rst_mask;
  250. p_clk->phy_reg = base + clks[i].phy_reg;
  251. p_clk->phy_clk_mask = clks[i].phy_clk_mask;
  252. p_clk->phy_rst_mask = clks[i].phy_rst_mask;
  253. p_clk->hw.init = &init;
  254. clk = clk_register(NULL, &p_clk->hw);
  255. if (IS_ERR(clk)) {
  256. kfree(p_clk);
  257. pr_err("%s: failed to register clock %s\n",
  258. __func__, clks[i].name);
  259. continue;
  260. }
  261. data->clk_data.clks[clks[i].id] = clk;
  262. }
  263. }
  264. static void __init hix5hd2_clk_init(struct device_node *np)
  265. {
  266. struct hisi_clock_data *clk_data;
  267. clk_data = hisi_clk_init(np, HIX5HD2_NR_CLKS);
  268. if (!clk_data)
  269. return;
  270. hisi_clk_register_fixed_rate(hix5hd2_fixed_rate_clks,
  271. ARRAY_SIZE(hix5hd2_fixed_rate_clks),
  272. clk_data);
  273. hisi_clk_register_mux(hix5hd2_mux_clks, ARRAY_SIZE(hix5hd2_mux_clks),
  274. clk_data);
  275. hisi_clk_register_gate(hix5hd2_gate_clks,
  276. ARRAY_SIZE(hix5hd2_gate_clks), clk_data);
  277. hix5hd2_clk_register_complex(hix5hd2_complex_clks,
  278. ARRAY_SIZE(hix5hd2_complex_clks),
  279. clk_data);
  280. }
  281. CLK_OF_DECLARE(hix5hd2_clk, "hisilicon,hix5hd2-clock", hix5hd2_clk_init);