clk-hi3559a.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Hisilicon Hi3559A clock driver
  4. *
  5. * Copyright (c) 2019-2020, Huawei Tech. Co., Ltd.
  6. *
  7. * Author: Dongjiu Geng <[email protected]>
  8. */
  9. #include <linux/clk-provider.h>
  10. #include <linux/module.h>
  11. #include <linux/of_device.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <dt-bindings/clock/hi3559av100-clock.h>
  15. #include "clk.h"
  16. #include "crg.h"
  17. #include "reset.h"
  18. #define CRG_BASE_ADDR 0x18020000
  19. #define PLL_MASK_WIDTH 24
  20. struct hi3559av100_pll_clock {
  21. u32 id;
  22. const char *name;
  23. const char *parent_name;
  24. const u32 ctrl_reg1;
  25. const u8 frac_shift;
  26. const u8 frac_width;
  27. const u8 postdiv1_shift;
  28. const u8 postdiv1_width;
  29. const u8 postdiv2_shift;
  30. const u8 postdiv2_width;
  31. const u32 ctrl_reg2;
  32. const u8 fbdiv_shift;
  33. const u8 fbdiv_width;
  34. const u8 refdiv_shift;
  35. const u8 refdiv_width;
  36. };
  37. struct hi3559av100_clk_pll {
  38. struct clk_hw hw;
  39. u32 id;
  40. void __iomem *ctrl_reg1;
  41. u8 frac_shift;
  42. u8 frac_width;
  43. u8 postdiv1_shift;
  44. u8 postdiv1_width;
  45. u8 postdiv2_shift;
  46. u8 postdiv2_width;
  47. void __iomem *ctrl_reg2;
  48. u8 fbdiv_shift;
  49. u8 fbdiv_width;
  50. u8 refdiv_shift;
  51. u8 refdiv_width;
  52. };
  53. /* soc clk config */
  54. static const struct hisi_fixed_rate_clock hi3559av100_fixed_rate_clks_crg[] = {
  55. { HI3559AV100_FIXED_1188M, "1188m", NULL, 0, 1188000000, },
  56. { HI3559AV100_FIXED_1000M, "1000m", NULL, 0, 1000000000, },
  57. { HI3559AV100_FIXED_842M, "842m", NULL, 0, 842000000, },
  58. { HI3559AV100_FIXED_792M, "792m", NULL, 0, 792000000, },
  59. { HI3559AV100_FIXED_750M, "750m", NULL, 0, 750000000, },
  60. { HI3559AV100_FIXED_710M, "710m", NULL, 0, 710000000, },
  61. { HI3559AV100_FIXED_680M, "680m", NULL, 0, 680000000, },
  62. { HI3559AV100_FIXED_667M, "667m", NULL, 0, 667000000, },
  63. { HI3559AV100_FIXED_631M, "631m", NULL, 0, 631000000, },
  64. { HI3559AV100_FIXED_600M, "600m", NULL, 0, 600000000, },
  65. { HI3559AV100_FIXED_568M, "568m", NULL, 0, 568000000, },
  66. { HI3559AV100_FIXED_500M, "500m", NULL, 0, 500000000, },
  67. { HI3559AV100_FIXED_475M, "475m", NULL, 0, 475000000, },
  68. { HI3559AV100_FIXED_428M, "428m", NULL, 0, 428000000, },
  69. { HI3559AV100_FIXED_400M, "400m", NULL, 0, 400000000, },
  70. { HI3559AV100_FIXED_396M, "396m", NULL, 0, 396000000, },
  71. { HI3559AV100_FIXED_300M, "300m", NULL, 0, 300000000, },
  72. { HI3559AV100_FIXED_250M, "250m", NULL, 0, 250000000, },
  73. { HI3559AV100_FIXED_200M, "200m", NULL, 0, 200000000, },
  74. { HI3559AV100_FIXED_198M, "198m", NULL, 0, 198000000, },
  75. { HI3559AV100_FIXED_187p5M, "187p5m", NULL, 0, 187500000, },
  76. { HI3559AV100_FIXED_150M, "150m", NULL, 0, 150000000, },
  77. { HI3559AV100_FIXED_148p5M, "148p5m", NULL, 0, 1485000000, },
  78. { HI3559AV100_FIXED_125M, "125m", NULL, 0, 125000000, },
  79. { HI3559AV100_FIXED_107M, "107m", NULL, 0, 107000000, },
  80. { HI3559AV100_FIXED_100M, "100m", NULL, 0, 100000000, },
  81. { HI3559AV100_FIXED_99M, "99m", NULL, 0, 99000000, },
  82. { HI3559AV100_FIXED_75M, "75m", NULL, 0, 75000000, },
  83. { HI3559AV100_FIXED_74p25M, "74p25m", NULL, 0, 74250000, },
  84. { HI3559AV100_FIXED_72M, "72m", NULL, 0, 72000000, },
  85. { HI3559AV100_FIXED_60M, "60m", NULL, 0, 60000000, },
  86. { HI3559AV100_FIXED_54M, "54m", NULL, 0, 54000000, },
  87. { HI3559AV100_FIXED_50M, "50m", NULL, 0, 50000000, },
  88. { HI3559AV100_FIXED_49p5M, "49p5m", NULL, 0, 49500000, },
  89. { HI3559AV100_FIXED_37p125M, "37p125m", NULL, 0, 37125000, },
  90. { HI3559AV100_FIXED_36M, "36m", NULL, 0, 36000000, },
  91. { HI3559AV100_FIXED_32p4M, "32p4m", NULL, 0, 32400000, },
  92. { HI3559AV100_FIXED_27M, "27m", NULL, 0, 27000000, },
  93. { HI3559AV100_FIXED_25M, "25m", NULL, 0, 25000000, },
  94. { HI3559AV100_FIXED_24M, "24m", NULL, 0, 24000000, },
  95. { HI3559AV100_FIXED_12M, "12m", NULL, 0, 12000000, },
  96. { HI3559AV100_FIXED_3M, "3m", NULL, 0, 3000000, },
  97. { HI3559AV100_FIXED_1p6M, "1p6m", NULL, 0, 1600000, },
  98. { HI3559AV100_FIXED_400K, "400k", NULL, 0, 400000, },
  99. { HI3559AV100_FIXED_100K, "100k", NULL, 0, 100000, },
  100. };
  101. static const char *fmc_mux_p[] = {
  102. "24m", "75m", "125m", "150m", "200m", "250m", "300m", "400m"
  103. };
  104. static const char *mmc_mux_p[] = {
  105. "100k", "25m", "49p5m", "99m", "187p5m", "150m", "198m", "400k"
  106. };
  107. static const char *sysapb_mux_p[] = {
  108. "24m", "50m",
  109. };
  110. static const char *sysbus_mux_p[] = {
  111. "24m", "300m"
  112. };
  113. static const char *uart_mux_p[] = { "50m", "24m", "3m" };
  114. static const char *a73_clksel_mux_p[] = {
  115. "24m", "apll", "1000m"
  116. };
  117. static const u32 fmc_mux_table[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  118. static const u32 mmc_mux_table[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  119. static const u32 sysapb_mux_table[] = { 0, 1 };
  120. static const u32 sysbus_mux_table[] = { 0, 1 };
  121. static const u32 uart_mux_table[] = { 0, 1, 2 };
  122. static const u32 a73_clksel_mux_table[] = { 0, 1, 2 };
  123. static struct hisi_mux_clock hi3559av100_mux_clks_crg[] = {
  124. {
  125. HI3559AV100_FMC_MUX, "fmc_mux", fmc_mux_p, ARRAY_SIZE(fmc_mux_p),
  126. CLK_SET_RATE_PARENT, 0x170, 2, 3, 0, fmc_mux_table,
  127. },
  128. {
  129. HI3559AV100_MMC0_MUX, "mmc0_mux", mmc_mux_p, ARRAY_SIZE(mmc_mux_p),
  130. CLK_SET_RATE_PARENT, 0x1a8, 24, 3, 0, mmc_mux_table,
  131. },
  132. {
  133. HI3559AV100_MMC1_MUX, "mmc1_mux", mmc_mux_p, ARRAY_SIZE(mmc_mux_p),
  134. CLK_SET_RATE_PARENT, 0x1ec, 24, 3, 0, mmc_mux_table,
  135. },
  136. {
  137. HI3559AV100_MMC2_MUX, "mmc2_mux", mmc_mux_p, ARRAY_SIZE(mmc_mux_p),
  138. CLK_SET_RATE_PARENT, 0x214, 24, 3, 0, mmc_mux_table,
  139. },
  140. {
  141. HI3559AV100_MMC3_MUX, "mmc3_mux", mmc_mux_p, ARRAY_SIZE(mmc_mux_p),
  142. CLK_SET_RATE_PARENT, 0x23c, 24, 3, 0, mmc_mux_table,
  143. },
  144. {
  145. HI3559AV100_SYSAPB_MUX, "sysapb_mux", sysapb_mux_p, ARRAY_SIZE(sysapb_mux_p),
  146. CLK_SET_RATE_PARENT, 0xe8, 3, 1, 0, sysapb_mux_table
  147. },
  148. {
  149. HI3559AV100_SYSBUS_MUX, "sysbus_mux", sysbus_mux_p, ARRAY_SIZE(sysbus_mux_p),
  150. CLK_SET_RATE_PARENT, 0xe8, 0, 1, 0, sysbus_mux_table
  151. },
  152. {
  153. HI3559AV100_UART_MUX, "uart_mux", uart_mux_p, ARRAY_SIZE(uart_mux_p),
  154. CLK_SET_RATE_PARENT, 0x198, 28, 2, 0, uart_mux_table
  155. },
  156. {
  157. HI3559AV100_A73_MUX, "a73_mux", a73_clksel_mux_p, ARRAY_SIZE(a73_clksel_mux_p),
  158. CLK_SET_RATE_PARENT, 0xe4, 0, 2, 0, a73_clksel_mux_table
  159. },
  160. };
  161. static struct hisi_gate_clock hi3559av100_gate_clks[] = {
  162. {
  163. HI3559AV100_FMC_CLK, "clk_fmc", "fmc_mux",
  164. CLK_SET_RATE_PARENT, 0x170, 1, 0,
  165. },
  166. {
  167. HI3559AV100_MMC0_CLK, "clk_mmc0", "mmc0_mux",
  168. CLK_SET_RATE_PARENT, 0x1a8, 28, 0,
  169. },
  170. {
  171. HI3559AV100_MMC1_CLK, "clk_mmc1", "mmc1_mux",
  172. CLK_SET_RATE_PARENT, 0x1ec, 28, 0,
  173. },
  174. {
  175. HI3559AV100_MMC2_CLK, "clk_mmc2", "mmc2_mux",
  176. CLK_SET_RATE_PARENT, 0x214, 28, 0,
  177. },
  178. {
  179. HI3559AV100_MMC3_CLK, "clk_mmc3", "mmc3_mux",
  180. CLK_SET_RATE_PARENT, 0x23c, 28, 0,
  181. },
  182. {
  183. HI3559AV100_UART0_CLK, "clk_uart0", "uart_mux",
  184. CLK_SET_RATE_PARENT, 0x198, 23, 0,
  185. },
  186. {
  187. HI3559AV100_UART1_CLK, "clk_uart1", "uart_mux",
  188. CLK_SET_RATE_PARENT, 0x198, 24, 0,
  189. },
  190. {
  191. HI3559AV100_UART2_CLK, "clk_uart2", "uart_mux",
  192. CLK_SET_RATE_PARENT, 0x198, 25, 0,
  193. },
  194. {
  195. HI3559AV100_UART3_CLK, "clk_uart3", "uart_mux",
  196. CLK_SET_RATE_PARENT, 0x198, 26, 0,
  197. },
  198. {
  199. HI3559AV100_UART4_CLK, "clk_uart4", "uart_mux",
  200. CLK_SET_RATE_PARENT, 0x198, 27, 0,
  201. },
  202. {
  203. HI3559AV100_ETH_CLK, "clk_eth", NULL,
  204. CLK_SET_RATE_PARENT, 0x0174, 1, 0,
  205. },
  206. {
  207. HI3559AV100_ETH_MACIF_CLK, "clk_eth_macif", NULL,
  208. CLK_SET_RATE_PARENT, 0x0174, 5, 0,
  209. },
  210. {
  211. HI3559AV100_ETH1_CLK, "clk_eth1", NULL,
  212. CLK_SET_RATE_PARENT, 0x0174, 3, 0,
  213. },
  214. {
  215. HI3559AV100_ETH1_MACIF_CLK, "clk_eth1_macif", NULL,
  216. CLK_SET_RATE_PARENT, 0x0174, 7, 0,
  217. },
  218. {
  219. HI3559AV100_I2C0_CLK, "clk_i2c0", "50m",
  220. CLK_SET_RATE_PARENT, 0x01a0, 16, 0,
  221. },
  222. {
  223. HI3559AV100_I2C1_CLK, "clk_i2c1", "50m",
  224. CLK_SET_RATE_PARENT, 0x01a0, 17, 0,
  225. },
  226. {
  227. HI3559AV100_I2C2_CLK, "clk_i2c2", "50m",
  228. CLK_SET_RATE_PARENT, 0x01a0, 18, 0,
  229. },
  230. {
  231. HI3559AV100_I2C3_CLK, "clk_i2c3", "50m",
  232. CLK_SET_RATE_PARENT, 0x01a0, 19, 0,
  233. },
  234. {
  235. HI3559AV100_I2C4_CLK, "clk_i2c4", "50m",
  236. CLK_SET_RATE_PARENT, 0x01a0, 20, 0,
  237. },
  238. {
  239. HI3559AV100_I2C5_CLK, "clk_i2c5", "50m",
  240. CLK_SET_RATE_PARENT, 0x01a0, 21, 0,
  241. },
  242. {
  243. HI3559AV100_I2C6_CLK, "clk_i2c6", "50m",
  244. CLK_SET_RATE_PARENT, 0x01a0, 22, 0,
  245. },
  246. {
  247. HI3559AV100_I2C7_CLK, "clk_i2c7", "50m",
  248. CLK_SET_RATE_PARENT, 0x01a0, 23, 0,
  249. },
  250. {
  251. HI3559AV100_I2C8_CLK, "clk_i2c8", "50m",
  252. CLK_SET_RATE_PARENT, 0x01a0, 24, 0,
  253. },
  254. {
  255. HI3559AV100_I2C9_CLK, "clk_i2c9", "50m",
  256. CLK_SET_RATE_PARENT, 0x01a0, 25, 0,
  257. },
  258. {
  259. HI3559AV100_I2C10_CLK, "clk_i2c10", "50m",
  260. CLK_SET_RATE_PARENT, 0x01a0, 26, 0,
  261. },
  262. {
  263. HI3559AV100_I2C11_CLK, "clk_i2c11", "50m",
  264. CLK_SET_RATE_PARENT, 0x01a0, 27, 0,
  265. },
  266. {
  267. HI3559AV100_SPI0_CLK, "clk_spi0", "100m",
  268. CLK_SET_RATE_PARENT, 0x0198, 16, 0,
  269. },
  270. {
  271. HI3559AV100_SPI1_CLK, "clk_spi1", "100m",
  272. CLK_SET_RATE_PARENT, 0x0198, 17, 0,
  273. },
  274. {
  275. HI3559AV100_SPI2_CLK, "clk_spi2", "100m",
  276. CLK_SET_RATE_PARENT, 0x0198, 18, 0,
  277. },
  278. {
  279. HI3559AV100_SPI3_CLK, "clk_spi3", "100m",
  280. CLK_SET_RATE_PARENT, 0x0198, 19, 0,
  281. },
  282. {
  283. HI3559AV100_SPI4_CLK, "clk_spi4", "100m",
  284. CLK_SET_RATE_PARENT, 0x0198, 20, 0,
  285. },
  286. {
  287. HI3559AV100_SPI5_CLK, "clk_spi5", "100m",
  288. CLK_SET_RATE_PARENT, 0x0198, 21, 0,
  289. },
  290. {
  291. HI3559AV100_SPI6_CLK, "clk_spi6", "100m",
  292. CLK_SET_RATE_PARENT, 0x0198, 22, 0,
  293. },
  294. {
  295. HI3559AV100_EDMAC_AXICLK, "axi_clk_edmac", NULL,
  296. CLK_SET_RATE_PARENT, 0x16c, 6, 0,
  297. },
  298. {
  299. HI3559AV100_EDMAC_CLK, "clk_edmac", NULL,
  300. CLK_SET_RATE_PARENT, 0x16c, 5, 0,
  301. },
  302. {
  303. HI3559AV100_EDMAC1_AXICLK, "axi_clk_edmac1", NULL,
  304. CLK_SET_RATE_PARENT, 0x16c, 9, 0,
  305. },
  306. {
  307. HI3559AV100_EDMAC1_CLK, "clk_edmac1", NULL,
  308. CLK_SET_RATE_PARENT, 0x16c, 8, 0,
  309. },
  310. {
  311. HI3559AV100_VDMAC_CLK, "clk_vdmac", NULL,
  312. CLK_SET_RATE_PARENT, 0x14c, 5, 0,
  313. },
  314. };
  315. static struct hi3559av100_pll_clock hi3559av100_pll_clks[] = {
  316. {
  317. HI3559AV100_APLL_CLK, "apll", NULL, 0x0, 0, 24, 24, 3, 28, 3,
  318. 0x4, 0, 12, 12, 6
  319. },
  320. {
  321. HI3559AV100_GPLL_CLK, "gpll", NULL, 0x20, 0, 24, 24, 3, 28, 3,
  322. 0x24, 0, 12, 12, 6
  323. },
  324. };
  325. #define to_pll_clk(_hw) container_of(_hw, struct hi3559av100_clk_pll, hw)
  326. static void hi3559av100_calc_pll(u32 *frac_val, u32 *postdiv1_val,
  327. u32 *postdiv2_val,
  328. u32 *fbdiv_val, u32 *refdiv_val, u64 rate)
  329. {
  330. u64 rem;
  331. *postdiv1_val = 2;
  332. *postdiv2_val = 1;
  333. rate = rate * ((*postdiv1_val) * (*postdiv2_val));
  334. *frac_val = 0;
  335. rem = do_div(rate, 1000000);
  336. rem = do_div(rate, PLL_MASK_WIDTH);
  337. *fbdiv_val = rate;
  338. *refdiv_val = 1;
  339. rem = rem * (1 << PLL_MASK_WIDTH);
  340. do_div(rem, PLL_MASK_WIDTH);
  341. *frac_val = rem;
  342. }
  343. static int clk_pll_set_rate(struct clk_hw *hw,
  344. unsigned long rate,
  345. unsigned long parent_rate)
  346. {
  347. struct hi3559av100_clk_pll *clk = to_pll_clk(hw);
  348. u32 frac_val, postdiv1_val, postdiv2_val, fbdiv_val, refdiv_val;
  349. u32 val;
  350. postdiv1_val = postdiv2_val = 0;
  351. hi3559av100_calc_pll(&frac_val, &postdiv1_val, &postdiv2_val,
  352. &fbdiv_val, &refdiv_val, (u64)rate);
  353. val = readl_relaxed(clk->ctrl_reg1);
  354. val &= ~(((1 << clk->frac_width) - 1) << clk->frac_shift);
  355. val &= ~(((1 << clk->postdiv1_width) - 1) << clk->postdiv1_shift);
  356. val &= ~(((1 << clk->postdiv2_width) - 1) << clk->postdiv2_shift);
  357. val |= frac_val << clk->frac_shift;
  358. val |= postdiv1_val << clk->postdiv1_shift;
  359. val |= postdiv2_val << clk->postdiv2_shift;
  360. writel_relaxed(val, clk->ctrl_reg1);
  361. val = readl_relaxed(clk->ctrl_reg2);
  362. val &= ~(((1 << clk->fbdiv_width) - 1) << clk->fbdiv_shift);
  363. val &= ~(((1 << clk->refdiv_width) - 1) << clk->refdiv_shift);
  364. val |= fbdiv_val << clk->fbdiv_shift;
  365. val |= refdiv_val << clk->refdiv_shift;
  366. writel_relaxed(val, clk->ctrl_reg2);
  367. return 0;
  368. }
  369. static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
  370. unsigned long parent_rate)
  371. {
  372. struct hi3559av100_clk_pll *clk = to_pll_clk(hw);
  373. u64 frac_val, fbdiv_val, refdiv_val;
  374. u32 postdiv1_val, postdiv2_val;
  375. u32 val;
  376. u64 tmp, rate;
  377. val = readl_relaxed(clk->ctrl_reg1);
  378. val = val >> clk->frac_shift;
  379. val &= ((1 << clk->frac_width) - 1);
  380. frac_val = val;
  381. val = readl_relaxed(clk->ctrl_reg1);
  382. val = val >> clk->postdiv1_shift;
  383. val &= ((1 << clk->postdiv1_width) - 1);
  384. postdiv1_val = val;
  385. val = readl_relaxed(clk->ctrl_reg1);
  386. val = val >> clk->postdiv2_shift;
  387. val &= ((1 << clk->postdiv2_width) - 1);
  388. postdiv2_val = val;
  389. val = readl_relaxed(clk->ctrl_reg2);
  390. val = val >> clk->fbdiv_shift;
  391. val &= ((1 << clk->fbdiv_width) - 1);
  392. fbdiv_val = val;
  393. val = readl_relaxed(clk->ctrl_reg2);
  394. val = val >> clk->refdiv_shift;
  395. val &= ((1 << clk->refdiv_width) - 1);
  396. refdiv_val = val;
  397. /* rate = 24000000 * (fbdiv + frac / (1<<24) ) / refdiv */
  398. rate = 0;
  399. tmp = 24000000 * fbdiv_val + (24000000 * frac_val) / (1 << 24);
  400. rate += tmp;
  401. do_div(rate, refdiv_val);
  402. do_div(rate, postdiv1_val * postdiv2_val);
  403. return rate;
  404. }
  405. static const struct clk_ops hisi_clk_pll_ops = {
  406. .set_rate = clk_pll_set_rate,
  407. .recalc_rate = clk_pll_recalc_rate,
  408. };
  409. static void hisi_clk_register_pll(struct hi3559av100_pll_clock *clks,
  410. int nums, struct hisi_clock_data *data, struct device *dev)
  411. {
  412. void __iomem *base = data->base;
  413. struct hi3559av100_clk_pll *p_clk = NULL;
  414. struct clk *clk = NULL;
  415. struct clk_init_data init;
  416. int i;
  417. p_clk = devm_kzalloc(dev, sizeof(*p_clk) * nums, GFP_KERNEL);
  418. if (!p_clk)
  419. return;
  420. for (i = 0; i < nums; i++) {
  421. init.name = clks[i].name;
  422. init.flags = 0;
  423. init.parent_names =
  424. (clks[i].parent_name ? &clks[i].parent_name : NULL);
  425. init.num_parents = (clks[i].parent_name ? 1 : 0);
  426. init.ops = &hisi_clk_pll_ops;
  427. p_clk->ctrl_reg1 = base + clks[i].ctrl_reg1;
  428. p_clk->frac_shift = clks[i].frac_shift;
  429. p_clk->frac_width = clks[i].frac_width;
  430. p_clk->postdiv1_shift = clks[i].postdiv1_shift;
  431. p_clk->postdiv1_width = clks[i].postdiv1_width;
  432. p_clk->postdiv2_shift = clks[i].postdiv2_shift;
  433. p_clk->postdiv2_width = clks[i].postdiv2_width;
  434. p_clk->ctrl_reg2 = base + clks[i].ctrl_reg2;
  435. p_clk->fbdiv_shift = clks[i].fbdiv_shift;
  436. p_clk->fbdiv_width = clks[i].fbdiv_width;
  437. p_clk->refdiv_shift = clks[i].refdiv_shift;
  438. p_clk->refdiv_width = clks[i].refdiv_width;
  439. p_clk->hw.init = &init;
  440. clk = clk_register(NULL, &p_clk->hw);
  441. if (IS_ERR(clk)) {
  442. devm_kfree(dev, p_clk);
  443. dev_err(dev, "%s: failed to register clock %s\n",
  444. __func__, clks[i].name);
  445. continue;
  446. }
  447. data->clk_data.clks[clks[i].id] = clk;
  448. p_clk++;
  449. }
  450. }
  451. static struct hisi_clock_data *hi3559av100_clk_register(
  452. struct platform_device *pdev)
  453. {
  454. struct hisi_clock_data *clk_data;
  455. int ret;
  456. clk_data = hisi_clk_alloc(pdev, HI3559AV100_CRG_NR_CLKS);
  457. if (!clk_data)
  458. return ERR_PTR(-ENOMEM);
  459. ret = hisi_clk_register_fixed_rate(hi3559av100_fixed_rate_clks_crg,
  460. ARRAY_SIZE(hi3559av100_fixed_rate_clks_crg), clk_data);
  461. if (ret)
  462. return ERR_PTR(ret);
  463. hisi_clk_register_pll(hi3559av100_pll_clks,
  464. ARRAY_SIZE(hi3559av100_pll_clks), clk_data, &pdev->dev);
  465. ret = hisi_clk_register_mux(hi3559av100_mux_clks_crg,
  466. ARRAY_SIZE(hi3559av100_mux_clks_crg), clk_data);
  467. if (ret)
  468. goto unregister_fixed_rate;
  469. ret = hisi_clk_register_gate(hi3559av100_gate_clks,
  470. ARRAY_SIZE(hi3559av100_gate_clks), clk_data);
  471. if (ret)
  472. goto unregister_mux;
  473. ret = of_clk_add_provider(pdev->dev.of_node,
  474. of_clk_src_onecell_get, &clk_data->clk_data);
  475. if (ret)
  476. goto unregister_gate;
  477. return clk_data;
  478. unregister_gate:
  479. hisi_clk_unregister_gate(hi3559av100_gate_clks,
  480. ARRAY_SIZE(hi3559av100_gate_clks), clk_data);
  481. unregister_mux:
  482. hisi_clk_unregister_mux(hi3559av100_mux_clks_crg,
  483. ARRAY_SIZE(hi3559av100_mux_clks_crg), clk_data);
  484. unregister_fixed_rate:
  485. hisi_clk_unregister_fixed_rate(hi3559av100_fixed_rate_clks_crg,
  486. ARRAY_SIZE(hi3559av100_fixed_rate_clks_crg), clk_data);
  487. return ERR_PTR(ret);
  488. }
  489. static void hi3559av100_clk_unregister(struct platform_device *pdev)
  490. {
  491. struct hisi_crg_dev *crg = platform_get_drvdata(pdev);
  492. of_clk_del_provider(pdev->dev.of_node);
  493. hisi_clk_unregister_gate(hi3559av100_gate_clks,
  494. ARRAY_SIZE(hi3559av100_gate_clks), crg->clk_data);
  495. hisi_clk_unregister_mux(hi3559av100_mux_clks_crg,
  496. ARRAY_SIZE(hi3559av100_mux_clks_crg), crg->clk_data);
  497. hisi_clk_unregister_fixed_rate(hi3559av100_fixed_rate_clks_crg,
  498. ARRAY_SIZE(hi3559av100_fixed_rate_clks_crg), crg->clk_data);
  499. }
  500. static const struct hisi_crg_funcs hi3559av100_crg_funcs = {
  501. .register_clks = hi3559av100_clk_register,
  502. .unregister_clks = hi3559av100_clk_unregister,
  503. };
  504. static struct hisi_fixed_rate_clock hi3559av100_shub_fixed_rate_clks[] = {
  505. { HI3559AV100_SHUB_SOURCE_SOC_24M, "clk_source_24M", NULL, 0, 24000000UL, },
  506. { HI3559AV100_SHUB_SOURCE_SOC_200M, "clk_source_200M", NULL, 0, 200000000UL, },
  507. { HI3559AV100_SHUB_SOURCE_SOC_300M, "clk_source_300M", NULL, 0, 300000000UL, },
  508. { HI3559AV100_SHUB_SOURCE_PLL, "clk_source_PLL", NULL, 0, 192000000UL, },
  509. { HI3559AV100_SHUB_I2C0_CLK, "clk_shub_i2c0", NULL, 0, 48000000UL, },
  510. { HI3559AV100_SHUB_I2C1_CLK, "clk_shub_i2c1", NULL, 0, 48000000UL, },
  511. { HI3559AV100_SHUB_I2C2_CLK, "clk_shub_i2c2", NULL, 0, 48000000UL, },
  512. { HI3559AV100_SHUB_I2C3_CLK, "clk_shub_i2c3", NULL, 0, 48000000UL, },
  513. { HI3559AV100_SHUB_I2C4_CLK, "clk_shub_i2c4", NULL, 0, 48000000UL, },
  514. { HI3559AV100_SHUB_I2C5_CLK, "clk_shub_i2c5", NULL, 0, 48000000UL, },
  515. { HI3559AV100_SHUB_I2C6_CLK, "clk_shub_i2c6", NULL, 0, 48000000UL, },
  516. { HI3559AV100_SHUB_I2C7_CLK, "clk_shub_i2c7", NULL, 0, 48000000UL, },
  517. { HI3559AV100_SHUB_UART_CLK_32K, "clk_uart_32K", NULL, 0, 32000UL, },
  518. };
  519. /* shub mux clk */
  520. static u32 shub_source_clk_mux_table[] = {0, 1, 2, 3};
  521. static const char *shub_source_clk_mux_p[] = {
  522. "clk_source_24M", "clk_source_200M", "clk_source_300M", "clk_source_PLL"
  523. };
  524. static u32 shub_uart_source_clk_mux_table[] = {0, 1, 2, 3};
  525. static const char *shub_uart_source_clk_mux_p[] = {
  526. "clk_uart_32K", "clk_uart_div_clk", "clk_uart_div_clk", "clk_source_24M"
  527. };
  528. static struct hisi_mux_clock hi3559av100_shub_mux_clks[] = {
  529. {
  530. HI3559AV100_SHUB_SOURCE_CLK, "shub_clk", shub_source_clk_mux_p,
  531. ARRAY_SIZE(shub_source_clk_mux_p),
  532. 0, 0x0, 0, 2, 0, shub_source_clk_mux_table,
  533. },
  534. {
  535. HI3559AV100_SHUB_UART_SOURCE_CLK, "shub_uart_source_clk",
  536. shub_uart_source_clk_mux_p, ARRAY_SIZE(shub_uart_source_clk_mux_p),
  537. 0, 0x1c, 28, 2, 0, shub_uart_source_clk_mux_table,
  538. },
  539. };
  540. /* shub div clk */
  541. static struct clk_div_table shub_spi_clk_table[] = {{0, 8}, {1, 4}, {2, 2}, {/*sentinel*/}};
  542. static struct clk_div_table shub_uart_div_clk_table[] = {{1, 8}, {2, 4}, {/*sentinel*/}};
  543. static struct hisi_divider_clock hi3559av100_shub_div_clks[] = {
  544. { HI3559AV100_SHUB_SPI_SOURCE_CLK, "clk_spi_clk", "shub_clk", 0, 0x20, 24, 2,
  545. CLK_DIVIDER_ALLOW_ZERO, shub_spi_clk_table,
  546. },
  547. { HI3559AV100_SHUB_UART_DIV_CLK, "clk_uart_div_clk", "shub_clk", 0, 0x1c, 28, 2,
  548. CLK_DIVIDER_ALLOW_ZERO, shub_uart_div_clk_table,
  549. },
  550. };
  551. /* shub gate clk */
  552. static struct hisi_gate_clock hi3559av100_shub_gate_clks[] = {
  553. {
  554. HI3559AV100_SHUB_SPI0_CLK, "clk_shub_spi0", "clk_spi_clk",
  555. 0, 0x20, 1, 0,
  556. },
  557. {
  558. HI3559AV100_SHUB_SPI1_CLK, "clk_shub_spi1", "clk_spi_clk",
  559. 0, 0x20, 5, 0,
  560. },
  561. {
  562. HI3559AV100_SHUB_SPI2_CLK, "clk_shub_spi2", "clk_spi_clk",
  563. 0, 0x20, 9, 0,
  564. },
  565. {
  566. HI3559AV100_SHUB_UART0_CLK, "clk_shub_uart0", "shub_uart_source_clk",
  567. 0, 0x1c, 1, 0,
  568. },
  569. {
  570. HI3559AV100_SHUB_UART1_CLK, "clk_shub_uart1", "shub_uart_source_clk",
  571. 0, 0x1c, 5, 0,
  572. },
  573. {
  574. HI3559AV100_SHUB_UART2_CLK, "clk_shub_uart2", "shub_uart_source_clk",
  575. 0, 0x1c, 9, 0,
  576. },
  577. {
  578. HI3559AV100_SHUB_UART3_CLK, "clk_shub_uart3", "shub_uart_source_clk",
  579. 0, 0x1c, 13, 0,
  580. },
  581. {
  582. HI3559AV100_SHUB_UART4_CLK, "clk_shub_uart4", "shub_uart_source_clk",
  583. 0, 0x1c, 17, 0,
  584. },
  585. {
  586. HI3559AV100_SHUB_UART5_CLK, "clk_shub_uart5", "shub_uart_source_clk",
  587. 0, 0x1c, 21, 0,
  588. },
  589. {
  590. HI3559AV100_SHUB_UART6_CLK, "clk_shub_uart6", "shub_uart_source_clk",
  591. 0, 0x1c, 25, 0,
  592. },
  593. {
  594. HI3559AV100_SHUB_EDMAC_CLK, "clk_shub_dmac", "shub_clk",
  595. 0, 0x24, 4, 0,
  596. },
  597. };
  598. static int hi3559av100_shub_default_clk_set(void)
  599. {
  600. void __iomem *crg_base;
  601. unsigned int val;
  602. crg_base = ioremap(CRG_BASE_ADDR, SZ_4K);
  603. /* SSP: 192M/2 */
  604. val = readl_relaxed(crg_base + 0x20);
  605. val |= (0x2 << 24);
  606. writel_relaxed(val, crg_base + 0x20);
  607. /* UART: 192M/8 */
  608. val = readl_relaxed(crg_base + 0x1C);
  609. val |= (0x1 << 28);
  610. writel_relaxed(val, crg_base + 0x1C);
  611. iounmap(crg_base);
  612. crg_base = NULL;
  613. return 0;
  614. }
  615. static struct hisi_clock_data *hi3559av100_shub_clk_register(
  616. struct platform_device *pdev)
  617. {
  618. struct hisi_clock_data *clk_data = NULL;
  619. int ret;
  620. hi3559av100_shub_default_clk_set();
  621. clk_data = hisi_clk_alloc(pdev, HI3559AV100_SHUB_NR_CLKS);
  622. if (!clk_data)
  623. return ERR_PTR(-ENOMEM);
  624. ret = hisi_clk_register_fixed_rate(hi3559av100_shub_fixed_rate_clks,
  625. ARRAY_SIZE(hi3559av100_shub_fixed_rate_clks), clk_data);
  626. if (ret)
  627. return ERR_PTR(ret);
  628. ret = hisi_clk_register_mux(hi3559av100_shub_mux_clks,
  629. ARRAY_SIZE(hi3559av100_shub_mux_clks), clk_data);
  630. if (ret)
  631. goto unregister_fixed_rate;
  632. ret = hisi_clk_register_divider(hi3559av100_shub_div_clks,
  633. ARRAY_SIZE(hi3559av100_shub_div_clks), clk_data);
  634. if (ret)
  635. goto unregister_mux;
  636. ret = hisi_clk_register_gate(hi3559av100_shub_gate_clks,
  637. ARRAY_SIZE(hi3559av100_shub_gate_clks), clk_data);
  638. if (ret)
  639. goto unregister_factor;
  640. ret = of_clk_add_provider(pdev->dev.of_node,
  641. of_clk_src_onecell_get, &clk_data->clk_data);
  642. if (ret)
  643. goto unregister_gate;
  644. return clk_data;
  645. unregister_gate:
  646. hisi_clk_unregister_gate(hi3559av100_shub_gate_clks,
  647. ARRAY_SIZE(hi3559av100_shub_gate_clks), clk_data);
  648. unregister_factor:
  649. hisi_clk_unregister_divider(hi3559av100_shub_div_clks,
  650. ARRAY_SIZE(hi3559av100_shub_div_clks), clk_data);
  651. unregister_mux:
  652. hisi_clk_unregister_mux(hi3559av100_shub_mux_clks,
  653. ARRAY_SIZE(hi3559av100_shub_mux_clks), clk_data);
  654. unregister_fixed_rate:
  655. hisi_clk_unregister_fixed_rate(hi3559av100_shub_fixed_rate_clks,
  656. ARRAY_SIZE(hi3559av100_shub_fixed_rate_clks), clk_data);
  657. return ERR_PTR(ret);
  658. }
  659. static void hi3559av100_shub_clk_unregister(struct platform_device *pdev)
  660. {
  661. struct hisi_crg_dev *crg = platform_get_drvdata(pdev);
  662. of_clk_del_provider(pdev->dev.of_node);
  663. hisi_clk_unregister_gate(hi3559av100_shub_gate_clks,
  664. ARRAY_SIZE(hi3559av100_shub_gate_clks), crg->clk_data);
  665. hisi_clk_unregister_divider(hi3559av100_shub_div_clks,
  666. ARRAY_SIZE(hi3559av100_shub_div_clks), crg->clk_data);
  667. hisi_clk_unregister_mux(hi3559av100_shub_mux_clks,
  668. ARRAY_SIZE(hi3559av100_shub_mux_clks), crg->clk_data);
  669. hisi_clk_unregister_fixed_rate(hi3559av100_shub_fixed_rate_clks,
  670. ARRAY_SIZE(hi3559av100_shub_fixed_rate_clks), crg->clk_data);
  671. }
  672. static const struct hisi_crg_funcs hi3559av100_shub_crg_funcs = {
  673. .register_clks = hi3559av100_shub_clk_register,
  674. .unregister_clks = hi3559av100_shub_clk_unregister,
  675. };
  676. static const struct of_device_id hi3559av100_crg_match_table[] = {
  677. {
  678. .compatible = "hisilicon,hi3559av100-clock",
  679. .data = &hi3559av100_crg_funcs
  680. },
  681. {
  682. .compatible = "hisilicon,hi3559av100-shub-clock",
  683. .data = &hi3559av100_shub_crg_funcs
  684. },
  685. { }
  686. };
  687. MODULE_DEVICE_TABLE(of, hi3559av100_crg_match_table);
  688. static int hi3559av100_crg_probe(struct platform_device *pdev)
  689. {
  690. struct hisi_crg_dev *crg;
  691. crg = devm_kmalloc(&pdev->dev, sizeof(*crg), GFP_KERNEL);
  692. if (!crg)
  693. return -ENOMEM;
  694. crg->funcs = of_device_get_match_data(&pdev->dev);
  695. if (!crg->funcs)
  696. return -ENOENT;
  697. crg->rstc = hisi_reset_init(pdev);
  698. if (!crg->rstc)
  699. return -ENOMEM;
  700. crg->clk_data = crg->funcs->register_clks(pdev);
  701. if (IS_ERR(crg->clk_data)) {
  702. hisi_reset_exit(crg->rstc);
  703. return PTR_ERR(crg->clk_data);
  704. }
  705. platform_set_drvdata(pdev, crg);
  706. return 0;
  707. }
  708. static int hi3559av100_crg_remove(struct platform_device *pdev)
  709. {
  710. struct hisi_crg_dev *crg = platform_get_drvdata(pdev);
  711. hisi_reset_exit(crg->rstc);
  712. crg->funcs->unregister_clks(pdev);
  713. return 0;
  714. }
  715. static struct platform_driver hi3559av100_crg_driver = {
  716. .probe = hi3559av100_crg_probe,
  717. .remove = hi3559av100_crg_remove,
  718. .driver = {
  719. .name = "hi3559av100-clock",
  720. .of_match_table = hi3559av100_crg_match_table,
  721. },
  722. };
  723. static int __init hi3559av100_crg_init(void)
  724. {
  725. return platform_driver_register(&hi3559av100_crg_driver);
  726. }
  727. core_initcall(hi3559av100_crg_init);
  728. static void __exit hi3559av100_crg_exit(void)
  729. {
  730. platform_driver_unregister(&hi3559av100_crg_driver);
  731. }
  732. module_exit(hi3559av100_crg_exit);
  733. MODULE_LICENSE("GPL v2");
  734. MODULE_DESCRIPTION("HiSilicon Hi3559AV100 CRG Driver");