clk-bm1880.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Bitmain BM1880 SoC clock driver
  4. *
  5. * Copyright (c) 2019 Linaro Ltd.
  6. * Author: Manivannan Sadhasivam <[email protected]>
  7. */
  8. #include <linux/clk-provider.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of_address.h>
  12. #include <linux/of_device.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <dt-bindings/clock/bm1880-clock.h>
  16. #define BM1880_CLK_MPLL_CTL 0x00
  17. #define BM1880_CLK_SPLL_CTL 0x04
  18. #define BM1880_CLK_FPLL_CTL 0x08
  19. #define BM1880_CLK_DDRPLL_CTL 0x0c
  20. #define BM1880_CLK_ENABLE0 0x00
  21. #define BM1880_CLK_ENABLE1 0x04
  22. #define BM1880_CLK_SELECT 0x20
  23. #define BM1880_CLK_DIV0 0x40
  24. #define BM1880_CLK_DIV1 0x44
  25. #define BM1880_CLK_DIV2 0x48
  26. #define BM1880_CLK_DIV3 0x4c
  27. #define BM1880_CLK_DIV4 0x50
  28. #define BM1880_CLK_DIV5 0x54
  29. #define BM1880_CLK_DIV6 0x58
  30. #define BM1880_CLK_DIV7 0x5c
  31. #define BM1880_CLK_DIV8 0x60
  32. #define BM1880_CLK_DIV9 0x64
  33. #define BM1880_CLK_DIV10 0x68
  34. #define BM1880_CLK_DIV11 0x6c
  35. #define BM1880_CLK_DIV12 0x70
  36. #define BM1880_CLK_DIV13 0x74
  37. #define BM1880_CLK_DIV14 0x78
  38. #define BM1880_CLK_DIV15 0x7c
  39. #define BM1880_CLK_DIV16 0x80
  40. #define BM1880_CLK_DIV17 0x84
  41. #define BM1880_CLK_DIV18 0x88
  42. #define BM1880_CLK_DIV19 0x8c
  43. #define BM1880_CLK_DIV20 0x90
  44. #define BM1880_CLK_DIV21 0x94
  45. #define BM1880_CLK_DIV22 0x98
  46. #define BM1880_CLK_DIV23 0x9c
  47. #define BM1880_CLK_DIV24 0xa0
  48. #define BM1880_CLK_DIV25 0xa4
  49. #define BM1880_CLK_DIV26 0xa8
  50. #define BM1880_CLK_DIV27 0xac
  51. #define BM1880_CLK_DIV28 0xb0
  52. #define to_bm1880_pll_clk(_hw) container_of(_hw, struct bm1880_pll_hw_clock, hw)
  53. #define to_bm1880_div_clk(_hw) container_of(_hw, struct bm1880_div_hw_clock, hw)
  54. static DEFINE_SPINLOCK(bm1880_clk_lock);
  55. struct bm1880_clock_data {
  56. void __iomem *pll_base;
  57. void __iomem *sys_base;
  58. struct clk_hw_onecell_data hw_data;
  59. };
  60. struct bm1880_gate_clock {
  61. unsigned int id;
  62. const char *name;
  63. const char *parent;
  64. u32 gate_reg;
  65. s8 gate_shift;
  66. unsigned long flags;
  67. };
  68. struct bm1880_mux_clock {
  69. unsigned int id;
  70. const char *name;
  71. const char * const *parents;
  72. s8 num_parents;
  73. u32 reg;
  74. s8 shift;
  75. unsigned long flags;
  76. };
  77. struct bm1880_div_clock {
  78. unsigned int id;
  79. const char *name;
  80. u32 reg;
  81. u8 shift;
  82. u8 width;
  83. u32 initval;
  84. const struct clk_div_table *table;
  85. unsigned long flags;
  86. };
  87. struct bm1880_div_hw_clock {
  88. struct bm1880_div_clock div;
  89. void __iomem *base;
  90. spinlock_t *lock;
  91. struct clk_hw hw;
  92. struct clk_init_data init;
  93. };
  94. struct bm1880_composite_clock {
  95. unsigned int id;
  96. const char *name;
  97. const char *parent;
  98. const char * const *parents;
  99. unsigned int num_parents;
  100. unsigned long flags;
  101. u32 gate_reg;
  102. u32 mux_reg;
  103. u32 div_reg;
  104. s8 gate_shift;
  105. s8 mux_shift;
  106. s8 div_shift;
  107. s8 div_width;
  108. s16 div_initval;
  109. const struct clk_div_table *table;
  110. };
  111. struct bm1880_pll_clock {
  112. unsigned int id;
  113. const char *name;
  114. u32 reg;
  115. unsigned long flags;
  116. };
  117. struct bm1880_pll_hw_clock {
  118. struct bm1880_pll_clock pll;
  119. void __iomem *base;
  120. struct clk_hw hw;
  121. struct clk_init_data init;
  122. };
  123. static const struct clk_ops bm1880_pll_ops;
  124. static const struct clk_ops bm1880_clk_div_ops;
  125. #define GATE_DIV(_id, _name, _parent, _gate_reg, _gate_shift, _div_reg, \
  126. _div_shift, _div_width, _div_initval, _table, \
  127. _flags) { \
  128. .id = _id, \
  129. .parent = _parent, \
  130. .name = _name, \
  131. .gate_reg = _gate_reg, \
  132. .gate_shift = _gate_shift, \
  133. .div_reg = _div_reg, \
  134. .div_shift = _div_shift, \
  135. .div_width = _div_width, \
  136. .div_initval = _div_initval, \
  137. .table = _table, \
  138. .mux_shift = -1, \
  139. .flags = _flags, \
  140. }
  141. #define GATE_MUX(_id, _name, _parents, _gate_reg, _gate_shift, \
  142. _mux_reg, _mux_shift, _flags) { \
  143. .id = _id, \
  144. .parents = _parents, \
  145. .num_parents = ARRAY_SIZE(_parents), \
  146. .name = _name, \
  147. .gate_reg = _gate_reg, \
  148. .gate_shift = _gate_shift, \
  149. .div_shift = -1, \
  150. .mux_reg = _mux_reg, \
  151. .mux_shift = _mux_shift, \
  152. .flags = _flags, \
  153. }
  154. #define CLK_PLL(_id, _name, _parent, _reg, _flags) { \
  155. .pll.id = _id, \
  156. .pll.name = _name, \
  157. .pll.reg = _reg, \
  158. .hw.init = CLK_HW_INIT_PARENTS_DATA(_name, _parent, \
  159. &bm1880_pll_ops, \
  160. _flags), \
  161. }
  162. #define CLK_DIV(_id, _name, _parent, _reg, _shift, _width, _initval, \
  163. _table, _flags) { \
  164. .div.id = _id, \
  165. .div.name = _name, \
  166. .div.reg = _reg, \
  167. .div.shift = _shift, \
  168. .div.width = _width, \
  169. .div.initval = _initval, \
  170. .div.table = _table, \
  171. .hw.init = CLK_HW_INIT_HW(_name, _parent, \
  172. &bm1880_clk_div_ops, \
  173. _flags), \
  174. }
  175. static struct clk_parent_data bm1880_pll_parent[] = {
  176. { .fw_name = "osc", .name = "osc" },
  177. };
  178. /*
  179. * All PLL clocks are marked as CRITICAL, hence they are very crucial
  180. * for the functioning of the SoC
  181. */
  182. static struct bm1880_pll_hw_clock bm1880_pll_clks[] = {
  183. CLK_PLL(BM1880_CLK_MPLL, "clk_mpll", bm1880_pll_parent,
  184. BM1880_CLK_MPLL_CTL, 0),
  185. CLK_PLL(BM1880_CLK_SPLL, "clk_spll", bm1880_pll_parent,
  186. BM1880_CLK_SPLL_CTL, 0),
  187. CLK_PLL(BM1880_CLK_FPLL, "clk_fpll", bm1880_pll_parent,
  188. BM1880_CLK_FPLL_CTL, 0),
  189. CLK_PLL(BM1880_CLK_DDRPLL, "clk_ddrpll", bm1880_pll_parent,
  190. BM1880_CLK_DDRPLL_CTL, 0),
  191. };
  192. /*
  193. * Clocks marked as CRITICAL are needed for the proper functioning
  194. * of the SoC.
  195. */
  196. static const struct bm1880_gate_clock bm1880_gate_clks[] = {
  197. { BM1880_CLK_AHB_ROM, "clk_ahb_rom", "clk_mux_axi6",
  198. BM1880_CLK_ENABLE0, 2, 0 },
  199. { BM1880_CLK_AXI_SRAM, "clk_axi_sram", "clk_axi1",
  200. BM1880_CLK_ENABLE0, 3, 0 },
  201. /*
  202. * Since this clock is sourcing the DDR memory, let's mark it as
  203. * critical to avoid gating.
  204. */
  205. { BM1880_CLK_DDR_AXI, "clk_ddr_axi", "clk_mux_axi6",
  206. BM1880_CLK_ENABLE0, 4, CLK_IS_CRITICAL },
  207. { BM1880_CLK_APB_EFUSE, "clk_apb_efuse", "clk_mux_axi6",
  208. BM1880_CLK_ENABLE0, 6, 0 },
  209. { BM1880_CLK_AXI5_EMMC, "clk_axi5_emmc", "clk_axi5",
  210. BM1880_CLK_ENABLE0, 7, 0 },
  211. { BM1880_CLK_AXI5_SD, "clk_axi5_sd", "clk_axi5",
  212. BM1880_CLK_ENABLE0, 10, 0 },
  213. { BM1880_CLK_AXI4_ETH0, "clk_axi4_eth0", "clk_axi4",
  214. BM1880_CLK_ENABLE0, 14, 0 },
  215. { BM1880_CLK_AXI4_ETH1, "clk_axi4_eth1", "clk_axi4",
  216. BM1880_CLK_ENABLE0, 16, 0 },
  217. { BM1880_CLK_AXI1_GDMA, "clk_axi1_gdma", "clk_axi1",
  218. BM1880_CLK_ENABLE0, 17, 0 },
  219. /* Don't gate GPIO clocks as it is not owned by the GPIO driver */
  220. { BM1880_CLK_APB_GPIO, "clk_apb_gpio", "clk_mux_axi6",
  221. BM1880_CLK_ENABLE0, 18, CLK_IGNORE_UNUSED },
  222. { BM1880_CLK_APB_GPIO_INTR, "clk_apb_gpio_intr", "clk_mux_axi6",
  223. BM1880_CLK_ENABLE0, 19, CLK_IGNORE_UNUSED },
  224. { BM1880_CLK_AXI1_MINER, "clk_axi1_miner", "clk_axi1",
  225. BM1880_CLK_ENABLE0, 21, 0 },
  226. { BM1880_CLK_AHB_SF, "clk_ahb_sf", "clk_mux_axi6",
  227. BM1880_CLK_ENABLE0, 22, 0 },
  228. /*
  229. * Not sure which module this clock is sourcing but gating this clock
  230. * prevents the system from booting. So, let's mark it as critical.
  231. */
  232. { BM1880_CLK_SDMA_AXI, "clk_sdma_axi", "clk_axi5",
  233. BM1880_CLK_ENABLE0, 23, CLK_IS_CRITICAL },
  234. { BM1880_CLK_APB_I2C, "clk_apb_i2c", "clk_mux_axi6",
  235. BM1880_CLK_ENABLE0, 25, 0 },
  236. { BM1880_CLK_APB_WDT, "clk_apb_wdt", "clk_mux_axi6",
  237. BM1880_CLK_ENABLE0, 26, 0 },
  238. { BM1880_CLK_APB_JPEG, "clk_apb_jpeg", "clk_axi6",
  239. BM1880_CLK_ENABLE0, 27, 0 },
  240. { BM1880_CLK_AXI5_NF, "clk_axi5_nf", "clk_axi5",
  241. BM1880_CLK_ENABLE0, 29, 0 },
  242. { BM1880_CLK_APB_NF, "clk_apb_nf", "clk_axi6",
  243. BM1880_CLK_ENABLE0, 30, 0 },
  244. { BM1880_CLK_APB_PWM, "clk_apb_pwm", "clk_mux_axi6",
  245. BM1880_CLK_ENABLE1, 0, 0 },
  246. { BM1880_CLK_RV, "clk_rv", "clk_mux_rv",
  247. BM1880_CLK_ENABLE1, 1, 0 },
  248. { BM1880_CLK_APB_SPI, "clk_apb_spi", "clk_mux_axi6",
  249. BM1880_CLK_ENABLE1, 2, 0 },
  250. { BM1880_CLK_UART_500M, "clk_uart_500m", "clk_div_uart_500m",
  251. BM1880_CLK_ENABLE1, 4, 0 },
  252. { BM1880_CLK_APB_UART, "clk_apb_uart", "clk_axi6",
  253. BM1880_CLK_ENABLE1, 5, 0 },
  254. { BM1880_CLK_APB_I2S, "clk_apb_i2s", "clk_axi6",
  255. BM1880_CLK_ENABLE1, 6, 0 },
  256. { BM1880_CLK_AXI4_USB, "clk_axi4_usb", "clk_axi4",
  257. BM1880_CLK_ENABLE1, 7, 0 },
  258. { BM1880_CLK_APB_USB, "clk_apb_usb", "clk_axi6",
  259. BM1880_CLK_ENABLE1, 8, 0 },
  260. { BM1880_CLK_12M_USB, "clk_12m_usb", "clk_div_12m_usb",
  261. BM1880_CLK_ENABLE1, 11, 0 },
  262. { BM1880_CLK_APB_VIDEO, "clk_apb_video", "clk_axi6",
  263. BM1880_CLK_ENABLE1, 12, 0 },
  264. { BM1880_CLK_APB_VPP, "clk_apb_vpp", "clk_axi6",
  265. BM1880_CLK_ENABLE1, 15, 0 },
  266. { BM1880_CLK_AXI6, "clk_axi6", "clk_mux_axi6",
  267. BM1880_CLK_ENABLE1, 21, 0 },
  268. };
  269. static const char * const clk_a53_parents[] = { "clk_spll", "clk_mpll" };
  270. static const char * const clk_rv_parents[] = { "clk_div_1_rv", "clk_div_0_rv" };
  271. static const char * const clk_axi1_parents[] = { "clk_div_1_axi1", "clk_div_0_axi1" };
  272. static const char * const clk_axi6_parents[] = { "clk_div_1_axi6", "clk_div_0_axi6" };
  273. static const struct bm1880_mux_clock bm1880_mux_clks[] = {
  274. { BM1880_CLK_MUX_RV, "clk_mux_rv", clk_rv_parents, 2,
  275. BM1880_CLK_SELECT, 1, 0 },
  276. { BM1880_CLK_MUX_AXI6, "clk_mux_axi6", clk_axi6_parents, 2,
  277. BM1880_CLK_SELECT, 3, 0 },
  278. };
  279. static const struct clk_div_table bm1880_div_table_0[] = {
  280. { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 4 },
  281. { 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 8 },
  282. { 8, 9 }, { 9, 10 }, { 10, 11 }, { 11, 12 },
  283. { 12, 13 }, { 13, 14 }, { 14, 15 }, { 15, 16 },
  284. { 16, 17 }, { 17, 18 }, { 18, 19 }, { 19, 20 },
  285. { 20, 21 }, { 21, 22 }, { 22, 23 }, { 23, 24 },
  286. { 24, 25 }, { 25, 26 }, { 26, 27 }, { 27, 28 },
  287. { 28, 29 }, { 29, 30 }, { 30, 31 }, { 31, 32 },
  288. { 0, 0 }
  289. };
  290. static const struct clk_div_table bm1880_div_table_1[] = {
  291. { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 4 },
  292. { 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 8 },
  293. { 8, 9 }, { 9, 10 }, { 10, 11 }, { 11, 12 },
  294. { 12, 13 }, { 13, 14 }, { 14, 15 }, { 15, 16 },
  295. { 16, 17 }, { 17, 18 }, { 18, 19 }, { 19, 20 },
  296. { 20, 21 }, { 21, 22 }, { 22, 23 }, { 23, 24 },
  297. { 24, 25 }, { 25, 26 }, { 26, 27 }, { 27, 28 },
  298. { 28, 29 }, { 29, 30 }, { 30, 31 }, { 31, 32 },
  299. { 127, 128 }, { 0, 0 }
  300. };
  301. static const struct clk_div_table bm1880_div_table_2[] = {
  302. { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 4 },
  303. { 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 8 },
  304. { 8, 9 }, { 9, 10 }, { 10, 11 }, { 11, 12 },
  305. { 12, 13 }, { 13, 14 }, { 14, 15 }, { 15, 16 },
  306. { 16, 17 }, { 17, 18 }, { 18, 19 }, { 19, 20 },
  307. { 20, 21 }, { 21, 22 }, { 22, 23 }, { 23, 24 },
  308. { 24, 25 }, { 25, 26 }, { 26, 27 }, { 27, 28 },
  309. { 28, 29 }, { 29, 30 }, { 30, 31 }, { 31, 32 },
  310. { 127, 128 }, { 255, 256 }, { 0, 0 }
  311. };
  312. static const struct clk_div_table bm1880_div_table_3[] = {
  313. { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 4 },
  314. { 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 8 },
  315. { 8, 9 }, { 9, 10 }, { 10, 11 }, { 11, 12 },
  316. { 12, 13 }, { 13, 14 }, { 14, 15 }, { 15, 16 },
  317. { 16, 17 }, { 17, 18 }, { 18, 19 }, { 19, 20 },
  318. { 20, 21 }, { 21, 22 }, { 22, 23 }, { 23, 24 },
  319. { 24, 25 }, { 25, 26 }, { 26, 27 }, { 27, 28 },
  320. { 28, 29 }, { 29, 30 }, { 30, 31 }, { 31, 32 },
  321. { 127, 128 }, { 255, 256 }, { 511, 512 }, { 0, 0 }
  322. };
  323. static const struct clk_div_table bm1880_div_table_4[] = {
  324. { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 4 },
  325. { 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 8 },
  326. { 8, 9 }, { 9, 10 }, { 10, 11 }, { 11, 12 },
  327. { 12, 13 }, { 13, 14 }, { 14, 15 }, { 15, 16 },
  328. { 16, 17 }, { 17, 18 }, { 18, 19 }, { 19, 20 },
  329. { 20, 21 }, { 21, 22 }, { 22, 23 }, { 23, 24 },
  330. { 24, 25 }, { 25, 26 }, { 26, 27 }, { 27, 28 },
  331. { 28, 29 }, { 29, 30 }, { 30, 31 }, { 31, 32 },
  332. { 127, 128 }, { 255, 256 }, { 511, 512 }, { 65535, 65536 },
  333. { 0, 0 }
  334. };
  335. /*
  336. * Clocks marked as CRITICAL are needed for the proper functioning
  337. * of the SoC.
  338. */
  339. static struct bm1880_div_hw_clock bm1880_div_clks[] = {
  340. CLK_DIV(BM1880_CLK_DIV_0_RV, "clk_div_0_rv", &bm1880_pll_clks[1].hw,
  341. BM1880_CLK_DIV12, 16, 5, 1, bm1880_div_table_0, 0),
  342. CLK_DIV(BM1880_CLK_DIV_1_RV, "clk_div_1_rv", &bm1880_pll_clks[2].hw,
  343. BM1880_CLK_DIV13, 16, 5, 1, bm1880_div_table_0, 0),
  344. CLK_DIV(BM1880_CLK_DIV_UART_500M, "clk_div_uart_500m", &bm1880_pll_clks[2].hw,
  345. BM1880_CLK_DIV15, 16, 7, 3, bm1880_div_table_1, 0),
  346. CLK_DIV(BM1880_CLK_DIV_0_AXI1, "clk_div_0_axi1", &bm1880_pll_clks[0].hw,
  347. BM1880_CLK_DIV21, 16, 5, 2, bm1880_div_table_0,
  348. 0),
  349. CLK_DIV(BM1880_CLK_DIV_1_AXI1, "clk_div_1_axi1", &bm1880_pll_clks[2].hw,
  350. BM1880_CLK_DIV22, 16, 5, 3, bm1880_div_table_0,
  351. 0),
  352. CLK_DIV(BM1880_CLK_DIV_0_AXI6, "clk_div_0_axi6", &bm1880_pll_clks[2].hw,
  353. BM1880_CLK_DIV27, 16, 5, 15, bm1880_div_table_0,
  354. 0),
  355. CLK_DIV(BM1880_CLK_DIV_1_AXI6, "clk_div_1_axi6", &bm1880_pll_clks[0].hw,
  356. BM1880_CLK_DIV28, 16, 5, 11, bm1880_div_table_0,
  357. 0),
  358. CLK_DIV(BM1880_CLK_DIV_12M_USB, "clk_div_12m_usb", &bm1880_pll_clks[2].hw,
  359. BM1880_CLK_DIV18, 16, 7, 125, bm1880_div_table_1, 0),
  360. };
  361. /*
  362. * Clocks marked as CRITICAL are all needed for the proper functioning
  363. * of the SoC.
  364. */
  365. static struct bm1880_composite_clock bm1880_composite_clks[] = {
  366. /*
  367. * Since clk_a53 and clk_50m_a53 clocks are sourcing the CPU core,
  368. * let's mark them as critical to avoid gating.
  369. */
  370. GATE_MUX(BM1880_CLK_A53, "clk_a53", clk_a53_parents,
  371. BM1880_CLK_ENABLE0, 0, BM1880_CLK_SELECT, 0,
  372. CLK_IS_CRITICAL),
  373. GATE_DIV(BM1880_CLK_50M_A53, "clk_50m_a53", "clk_fpll",
  374. BM1880_CLK_ENABLE0, 1, BM1880_CLK_DIV0, 16, 5, 30,
  375. bm1880_div_table_0, CLK_IS_CRITICAL),
  376. GATE_DIV(BM1880_CLK_EFUSE, "clk_efuse", "clk_fpll",
  377. BM1880_CLK_ENABLE0, 5, BM1880_CLK_DIV1, 16, 7, 60,
  378. bm1880_div_table_1, 0),
  379. GATE_DIV(BM1880_CLK_EMMC, "clk_emmc", "clk_fpll",
  380. BM1880_CLK_ENABLE0, 8, BM1880_CLK_DIV2, 16, 5, 15,
  381. bm1880_div_table_0, 0),
  382. GATE_DIV(BM1880_CLK_100K_EMMC, "clk_100k_emmc", "clk_div_12m_usb",
  383. BM1880_CLK_ENABLE0, 9, BM1880_CLK_DIV3, 16, 8, 120,
  384. bm1880_div_table_2, 0),
  385. GATE_DIV(BM1880_CLK_SD, "clk_sd", "clk_fpll",
  386. BM1880_CLK_ENABLE0, 11, BM1880_CLK_DIV4, 16, 5, 15,
  387. bm1880_div_table_0, 0),
  388. GATE_DIV(BM1880_CLK_100K_SD, "clk_100k_sd", "clk_div_12m_usb",
  389. BM1880_CLK_ENABLE0, 12, BM1880_CLK_DIV5, 16, 8, 120,
  390. bm1880_div_table_2, 0),
  391. GATE_DIV(BM1880_CLK_500M_ETH0, "clk_500m_eth0", "clk_fpll",
  392. BM1880_CLK_ENABLE0, 13, BM1880_CLK_DIV6, 16, 5, 3,
  393. bm1880_div_table_0, 0),
  394. GATE_DIV(BM1880_CLK_500M_ETH1, "clk_500m_eth1", "clk_fpll",
  395. BM1880_CLK_ENABLE0, 15, BM1880_CLK_DIV7, 16, 5, 3,
  396. bm1880_div_table_0, 0),
  397. /* Don't gate GPIO clocks as it is not owned by the GPIO driver */
  398. GATE_DIV(BM1880_CLK_GPIO_DB, "clk_gpio_db", "clk_div_12m_usb",
  399. BM1880_CLK_ENABLE0, 20, BM1880_CLK_DIV8, 16, 16, 120,
  400. bm1880_div_table_4, CLK_IGNORE_UNUSED),
  401. GATE_DIV(BM1880_CLK_SDMA_AUD, "clk_sdma_aud", "clk_fpll",
  402. BM1880_CLK_ENABLE0, 24, BM1880_CLK_DIV9, 16, 7, 61,
  403. bm1880_div_table_1, 0),
  404. GATE_DIV(BM1880_CLK_JPEG_AXI, "clk_jpeg_axi", "clk_fpll",
  405. BM1880_CLK_ENABLE0, 28, BM1880_CLK_DIV10, 16, 5, 4,
  406. bm1880_div_table_0, 0),
  407. GATE_DIV(BM1880_CLK_NF, "clk_nf", "clk_fpll",
  408. BM1880_CLK_ENABLE0, 31, BM1880_CLK_DIV11, 16, 5, 30,
  409. bm1880_div_table_0, 0),
  410. GATE_DIV(BM1880_CLK_TPU_AXI, "clk_tpu_axi", "clk_spll",
  411. BM1880_CLK_ENABLE1, 3, BM1880_CLK_DIV14, 16, 5, 1,
  412. bm1880_div_table_0, 0),
  413. GATE_DIV(BM1880_CLK_125M_USB, "clk_125m_usb", "clk_fpll",
  414. BM1880_CLK_ENABLE1, 9, BM1880_CLK_DIV16, 16, 5, 12,
  415. bm1880_div_table_0, 0),
  416. GATE_DIV(BM1880_CLK_33K_USB, "clk_33k_usb", "clk_div_12m_usb",
  417. BM1880_CLK_ENABLE1, 10, BM1880_CLK_DIV17, 16, 9, 363,
  418. bm1880_div_table_3, 0),
  419. GATE_DIV(BM1880_CLK_VIDEO_AXI, "clk_video_axi", "clk_fpll",
  420. BM1880_CLK_ENABLE1, 13, BM1880_CLK_DIV19, 16, 5, 4,
  421. bm1880_div_table_0, 0),
  422. GATE_DIV(BM1880_CLK_VPP_AXI, "clk_vpp_axi", "clk_fpll",
  423. BM1880_CLK_ENABLE1, 14, BM1880_CLK_DIV20, 16, 5, 4,
  424. bm1880_div_table_0, 0),
  425. GATE_MUX(BM1880_CLK_AXI1, "clk_axi1", clk_axi1_parents,
  426. BM1880_CLK_ENABLE1, 15, BM1880_CLK_SELECT, 2, 0),
  427. GATE_DIV(BM1880_CLK_AXI2, "clk_axi2", "clk_fpll",
  428. BM1880_CLK_ENABLE1, 17, BM1880_CLK_DIV23, 16, 5, 3,
  429. bm1880_div_table_0, 0),
  430. GATE_DIV(BM1880_CLK_AXI3, "clk_axi3", "clk_mux_rv",
  431. BM1880_CLK_ENABLE1, 18, BM1880_CLK_DIV24, 16, 5, 2,
  432. bm1880_div_table_0, 0),
  433. GATE_DIV(BM1880_CLK_AXI4, "clk_axi4", "clk_fpll",
  434. BM1880_CLK_ENABLE1, 19, BM1880_CLK_DIV25, 16, 5, 6,
  435. bm1880_div_table_0, 0),
  436. GATE_DIV(BM1880_CLK_AXI5, "clk_axi5", "clk_fpll",
  437. BM1880_CLK_ENABLE1, 20, BM1880_CLK_DIV26, 16, 5, 15,
  438. bm1880_div_table_0, 0),
  439. };
  440. static unsigned long bm1880_pll_rate_calc(u32 regval, unsigned long parent_rate)
  441. {
  442. u64 numerator;
  443. u32 fbdiv, refdiv;
  444. u32 postdiv1, postdiv2, denominator;
  445. fbdiv = (regval >> 16) & 0xfff;
  446. refdiv = regval & 0x1f;
  447. postdiv1 = (regval >> 8) & 0x7;
  448. postdiv2 = (regval >> 12) & 0x7;
  449. numerator = parent_rate * fbdiv;
  450. denominator = refdiv * postdiv1 * postdiv2;
  451. do_div(numerator, denominator);
  452. return (unsigned long)numerator;
  453. }
  454. static unsigned long bm1880_pll_recalc_rate(struct clk_hw *hw,
  455. unsigned long parent_rate)
  456. {
  457. struct bm1880_pll_hw_clock *pll_hw = to_bm1880_pll_clk(hw);
  458. unsigned long rate;
  459. u32 regval;
  460. regval = readl(pll_hw->base + pll_hw->pll.reg);
  461. rate = bm1880_pll_rate_calc(regval, parent_rate);
  462. return rate;
  463. }
  464. static const struct clk_ops bm1880_pll_ops = {
  465. .recalc_rate = bm1880_pll_recalc_rate,
  466. };
  467. static struct clk_hw *bm1880_clk_register_pll(struct bm1880_pll_hw_clock *pll_clk,
  468. void __iomem *sys_base)
  469. {
  470. struct clk_hw *hw;
  471. int err;
  472. pll_clk->base = sys_base;
  473. hw = &pll_clk->hw;
  474. err = clk_hw_register(NULL, hw);
  475. if (err)
  476. return ERR_PTR(err);
  477. return hw;
  478. }
  479. static int bm1880_clk_register_plls(struct bm1880_pll_hw_clock *clks,
  480. int num_clks,
  481. struct bm1880_clock_data *data)
  482. {
  483. struct clk_hw *hw;
  484. void __iomem *pll_base = data->pll_base;
  485. int i;
  486. for (i = 0; i < num_clks; i++) {
  487. struct bm1880_pll_hw_clock *bm1880_clk = &clks[i];
  488. hw = bm1880_clk_register_pll(bm1880_clk, pll_base);
  489. if (IS_ERR(hw)) {
  490. pr_err("%s: failed to register clock %s\n",
  491. __func__, bm1880_clk->pll.name);
  492. goto err_clk;
  493. }
  494. data->hw_data.hws[clks[i].pll.id] = hw;
  495. }
  496. return 0;
  497. err_clk:
  498. while (i--)
  499. clk_hw_unregister(data->hw_data.hws[clks[i].pll.id]);
  500. return PTR_ERR(hw);
  501. }
  502. static int bm1880_clk_register_mux(const struct bm1880_mux_clock *clks,
  503. int num_clks,
  504. struct bm1880_clock_data *data)
  505. {
  506. struct clk_hw *hw;
  507. void __iomem *sys_base = data->sys_base;
  508. int i;
  509. for (i = 0; i < num_clks; i++) {
  510. hw = clk_hw_register_mux(NULL, clks[i].name,
  511. clks[i].parents,
  512. clks[i].num_parents,
  513. clks[i].flags,
  514. sys_base + clks[i].reg,
  515. clks[i].shift, 1, 0,
  516. &bm1880_clk_lock);
  517. if (IS_ERR(hw)) {
  518. pr_err("%s: failed to register clock %s\n",
  519. __func__, clks[i].name);
  520. goto err_clk;
  521. }
  522. data->hw_data.hws[clks[i].id] = hw;
  523. }
  524. return 0;
  525. err_clk:
  526. while (i--)
  527. clk_hw_unregister_mux(data->hw_data.hws[clks[i].id]);
  528. return PTR_ERR(hw);
  529. }
  530. static unsigned long bm1880_clk_div_recalc_rate(struct clk_hw *hw,
  531. unsigned long parent_rate)
  532. {
  533. struct bm1880_div_hw_clock *div_hw = to_bm1880_div_clk(hw);
  534. struct bm1880_div_clock *div = &div_hw->div;
  535. void __iomem *reg_addr = div_hw->base + div->reg;
  536. unsigned int val;
  537. unsigned long rate;
  538. if (!(readl(reg_addr) & BIT(3))) {
  539. val = div->initval;
  540. } else {
  541. val = readl(reg_addr) >> div->shift;
  542. val &= clk_div_mask(div->width);
  543. }
  544. rate = divider_recalc_rate(hw, parent_rate, val, div->table,
  545. div->flags, div->width);
  546. return rate;
  547. }
  548. static long bm1880_clk_div_round_rate(struct clk_hw *hw, unsigned long rate,
  549. unsigned long *prate)
  550. {
  551. struct bm1880_div_hw_clock *div_hw = to_bm1880_div_clk(hw);
  552. struct bm1880_div_clock *div = &div_hw->div;
  553. void __iomem *reg_addr = div_hw->base + div->reg;
  554. if (div->flags & CLK_DIVIDER_READ_ONLY) {
  555. u32 val;
  556. val = readl(reg_addr) >> div->shift;
  557. val &= clk_div_mask(div->width);
  558. return divider_ro_round_rate(hw, rate, prate, div->table,
  559. div->width, div->flags,
  560. val);
  561. }
  562. return divider_round_rate(hw, rate, prate, div->table,
  563. div->width, div->flags);
  564. }
  565. static int bm1880_clk_div_set_rate(struct clk_hw *hw, unsigned long rate,
  566. unsigned long parent_rate)
  567. {
  568. struct bm1880_div_hw_clock *div_hw = to_bm1880_div_clk(hw);
  569. struct bm1880_div_clock *div = &div_hw->div;
  570. void __iomem *reg_addr = div_hw->base + div->reg;
  571. unsigned long flags = 0;
  572. int value;
  573. u32 val;
  574. value = divider_get_val(rate, parent_rate, div->table,
  575. div->width, div_hw->div.flags);
  576. if (value < 0)
  577. return value;
  578. if (div_hw->lock)
  579. spin_lock_irqsave(div_hw->lock, flags);
  580. else
  581. __acquire(div_hw->lock);
  582. val = readl(reg_addr);
  583. val &= ~(clk_div_mask(div->width) << div_hw->div.shift);
  584. val |= (u32)value << div->shift;
  585. writel(val, reg_addr);
  586. if (div_hw->lock)
  587. spin_unlock_irqrestore(div_hw->lock, flags);
  588. else
  589. __release(div_hw->lock);
  590. return 0;
  591. }
  592. static const struct clk_ops bm1880_clk_div_ops = {
  593. .recalc_rate = bm1880_clk_div_recalc_rate,
  594. .round_rate = bm1880_clk_div_round_rate,
  595. .set_rate = bm1880_clk_div_set_rate,
  596. };
  597. static struct clk_hw *bm1880_clk_register_div(struct bm1880_div_hw_clock *div_clk,
  598. void __iomem *sys_base)
  599. {
  600. struct clk_hw *hw;
  601. int err;
  602. div_clk->div.flags = CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO;
  603. div_clk->base = sys_base;
  604. div_clk->lock = &bm1880_clk_lock;
  605. hw = &div_clk->hw;
  606. err = clk_hw_register(NULL, hw);
  607. if (err)
  608. return ERR_PTR(err);
  609. return hw;
  610. }
  611. static int bm1880_clk_register_divs(struct bm1880_div_hw_clock *clks,
  612. int num_clks,
  613. struct bm1880_clock_data *data)
  614. {
  615. struct clk_hw *hw;
  616. void __iomem *sys_base = data->sys_base;
  617. unsigned int i, id;
  618. for (i = 0; i < num_clks; i++) {
  619. struct bm1880_div_hw_clock *bm1880_clk = &clks[i];
  620. hw = bm1880_clk_register_div(bm1880_clk, sys_base);
  621. if (IS_ERR(hw)) {
  622. pr_err("%s: failed to register clock %s\n",
  623. __func__, bm1880_clk->div.name);
  624. goto err_clk;
  625. }
  626. id = clks[i].div.id;
  627. data->hw_data.hws[id] = hw;
  628. }
  629. return 0;
  630. err_clk:
  631. while (i--)
  632. clk_hw_unregister(data->hw_data.hws[clks[i].div.id]);
  633. return PTR_ERR(hw);
  634. }
  635. static int bm1880_clk_register_gate(const struct bm1880_gate_clock *clks,
  636. int num_clks,
  637. struct bm1880_clock_data *data)
  638. {
  639. struct clk_hw *hw;
  640. void __iomem *sys_base = data->sys_base;
  641. int i;
  642. for (i = 0; i < num_clks; i++) {
  643. hw = clk_hw_register_gate(NULL, clks[i].name,
  644. clks[i].parent,
  645. clks[i].flags,
  646. sys_base + clks[i].gate_reg,
  647. clks[i].gate_shift, 0,
  648. &bm1880_clk_lock);
  649. if (IS_ERR(hw)) {
  650. pr_err("%s: failed to register clock %s\n",
  651. __func__, clks[i].name);
  652. goto err_clk;
  653. }
  654. data->hw_data.hws[clks[i].id] = hw;
  655. }
  656. return 0;
  657. err_clk:
  658. while (i--)
  659. clk_hw_unregister_gate(data->hw_data.hws[clks[i].id]);
  660. return PTR_ERR(hw);
  661. }
  662. static struct clk_hw *bm1880_clk_register_composite(struct bm1880_composite_clock *clks,
  663. void __iomem *sys_base)
  664. {
  665. struct clk_hw *hw;
  666. struct clk_mux *mux = NULL;
  667. struct clk_gate *gate = NULL;
  668. struct bm1880_div_hw_clock *div_hws = NULL;
  669. struct clk_hw *mux_hw = NULL, *gate_hw = NULL, *div_hw = NULL;
  670. const struct clk_ops *mux_ops = NULL, *gate_ops = NULL, *div_ops = NULL;
  671. const char * const *parent_names;
  672. const char *parent;
  673. int num_parents;
  674. int ret;
  675. if (clks->mux_shift >= 0) {
  676. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  677. if (!mux)
  678. return ERR_PTR(-ENOMEM);
  679. mux->reg = sys_base + clks->mux_reg;
  680. mux->mask = 1;
  681. mux->shift = clks->mux_shift;
  682. mux_hw = &mux->hw;
  683. mux_ops = &clk_mux_ops;
  684. mux->lock = &bm1880_clk_lock;
  685. parent_names = clks->parents;
  686. num_parents = clks->num_parents;
  687. } else {
  688. parent = clks->parent;
  689. parent_names = &parent;
  690. num_parents = 1;
  691. }
  692. if (clks->gate_shift >= 0) {
  693. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  694. if (!gate) {
  695. ret = -ENOMEM;
  696. goto err_out;
  697. }
  698. gate->reg = sys_base + clks->gate_reg;
  699. gate->bit_idx = clks->gate_shift;
  700. gate->lock = &bm1880_clk_lock;
  701. gate_hw = &gate->hw;
  702. gate_ops = &clk_gate_ops;
  703. }
  704. if (clks->div_shift >= 0) {
  705. div_hws = kzalloc(sizeof(*div_hws), GFP_KERNEL);
  706. if (!div_hws) {
  707. ret = -ENOMEM;
  708. goto err_out;
  709. }
  710. div_hws->base = sys_base;
  711. div_hws->div.reg = clks->div_reg;
  712. div_hws->div.shift = clks->div_shift;
  713. div_hws->div.width = clks->div_width;
  714. div_hws->div.table = clks->table;
  715. div_hws->div.initval = clks->div_initval;
  716. div_hws->lock = &bm1880_clk_lock;
  717. div_hws->div.flags = CLK_DIVIDER_ONE_BASED |
  718. CLK_DIVIDER_ALLOW_ZERO;
  719. div_hw = &div_hws->hw;
  720. div_ops = &bm1880_clk_div_ops;
  721. }
  722. hw = clk_hw_register_composite(NULL, clks->name, parent_names,
  723. num_parents, mux_hw, mux_ops, div_hw,
  724. div_ops, gate_hw, gate_ops,
  725. clks->flags);
  726. if (IS_ERR(hw)) {
  727. ret = PTR_ERR(hw);
  728. goto err_out;
  729. }
  730. return hw;
  731. err_out:
  732. kfree(div_hws);
  733. kfree(gate);
  734. kfree(mux);
  735. return ERR_PTR(ret);
  736. }
  737. static int bm1880_clk_register_composites(struct bm1880_composite_clock *clks,
  738. int num_clks,
  739. struct bm1880_clock_data *data)
  740. {
  741. struct clk_hw *hw;
  742. void __iomem *sys_base = data->sys_base;
  743. int i;
  744. for (i = 0; i < num_clks; i++) {
  745. struct bm1880_composite_clock *bm1880_clk = &clks[i];
  746. hw = bm1880_clk_register_composite(bm1880_clk, sys_base);
  747. if (IS_ERR(hw)) {
  748. pr_err("%s: failed to register clock %s\n",
  749. __func__, bm1880_clk->name);
  750. goto err_clk;
  751. }
  752. data->hw_data.hws[clks[i].id] = hw;
  753. }
  754. return 0;
  755. err_clk:
  756. while (i--)
  757. clk_hw_unregister_composite(data->hw_data.hws[clks[i].id]);
  758. return PTR_ERR(hw);
  759. }
  760. static int bm1880_clk_probe(struct platform_device *pdev)
  761. {
  762. struct bm1880_clock_data *clk_data;
  763. void __iomem *pll_base, *sys_base;
  764. struct device *dev = &pdev->dev;
  765. struct resource *res;
  766. int num_clks, i;
  767. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  768. pll_base = devm_ioremap_resource(&pdev->dev, res);
  769. if (IS_ERR(pll_base))
  770. return PTR_ERR(pll_base);
  771. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  772. sys_base = devm_ioremap_resource(&pdev->dev, res);
  773. if (IS_ERR(sys_base))
  774. return PTR_ERR(sys_base);
  775. num_clks = ARRAY_SIZE(bm1880_pll_clks) +
  776. ARRAY_SIZE(bm1880_div_clks) +
  777. ARRAY_SIZE(bm1880_mux_clks) +
  778. ARRAY_SIZE(bm1880_composite_clks) +
  779. ARRAY_SIZE(bm1880_gate_clks);
  780. clk_data = devm_kzalloc(dev, struct_size(clk_data, hw_data.hws,
  781. num_clks), GFP_KERNEL);
  782. if (!clk_data)
  783. return -ENOMEM;
  784. clk_data->pll_base = pll_base;
  785. clk_data->sys_base = sys_base;
  786. for (i = 0; i < num_clks; i++)
  787. clk_data->hw_data.hws[i] = ERR_PTR(-ENOENT);
  788. clk_data->hw_data.num = num_clks;
  789. bm1880_clk_register_plls(bm1880_pll_clks,
  790. ARRAY_SIZE(bm1880_pll_clks),
  791. clk_data);
  792. bm1880_clk_register_divs(bm1880_div_clks,
  793. ARRAY_SIZE(bm1880_div_clks),
  794. clk_data);
  795. bm1880_clk_register_mux(bm1880_mux_clks,
  796. ARRAY_SIZE(bm1880_mux_clks),
  797. clk_data);
  798. bm1880_clk_register_composites(bm1880_composite_clks,
  799. ARRAY_SIZE(bm1880_composite_clks),
  800. clk_data);
  801. bm1880_clk_register_gate(bm1880_gate_clks,
  802. ARRAY_SIZE(bm1880_gate_clks),
  803. clk_data);
  804. return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
  805. &clk_data->hw_data);
  806. }
  807. static const struct of_device_id bm1880_of_match[] = {
  808. { .compatible = "bitmain,bm1880-clk", },
  809. {}
  810. };
  811. MODULE_DEVICE_TABLE(of, bm1880_of_match);
  812. static struct platform_driver bm1880_clk_driver = {
  813. .driver = {
  814. .name = "bm1880-clk",
  815. .of_match_table = bm1880_of_match,
  816. },
  817. .probe = bm1880_clk_probe,
  818. };
  819. module_platform_driver(bm1880_clk_driver);
  820. MODULE_AUTHOR("Manivannan Sadhasivam <[email protected]>");
  821. MODULE_DESCRIPTION("Clock driver for Bitmain BM1880 SoC");
  822. MODULE_LICENSE("GPL v2");