clk-hi3620.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Hisilicon Hi3620 clock driver
  3. *
  4. * Copyright (c) 2012-2013 Hisilicon Limited.
  5. * Copyright (c) 2012-2013 Linaro Limited.
  6. *
  7. * Author: Haojian Zhuang <haojian.zhuang@linaro.org>
  8. * Xin Li <li.xin@linaro.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/clk-provider.h>
  27. #include <linux/io.h>
  28. #include <linux/of.h>
  29. #include <linux/of_address.h>
  30. #include <linux/of_device.h>
  31. #include <linux/slab.h>
  32. #include <dt-bindings/clock/hi3620-clock.h>
  33. #include "clk.h"
  34. /* clock parent list */
  35. static const char *const timer0_mux_p[] __initconst = { "osc32k", "timerclk01", };
  36. static const char *const timer1_mux_p[] __initconst = { "osc32k", "timerclk01", };
  37. static const char *const timer2_mux_p[] __initconst = { "osc32k", "timerclk23", };
  38. static const char *const timer3_mux_p[] __initconst = { "osc32k", "timerclk23", };
  39. static const char *const timer4_mux_p[] __initconst = { "osc32k", "timerclk45", };
  40. static const char *const timer5_mux_p[] __initconst = { "osc32k", "timerclk45", };
  41. static const char *const timer6_mux_p[] __initconst = { "osc32k", "timerclk67", };
  42. static const char *const timer7_mux_p[] __initconst = { "osc32k", "timerclk67", };
  43. static const char *const timer8_mux_p[] __initconst = { "osc32k", "timerclk89", };
  44. static const char *const timer9_mux_p[] __initconst = { "osc32k", "timerclk89", };
  45. static const char *const uart0_mux_p[] __initconst = { "osc26m", "pclk", };
  46. static const char *const uart1_mux_p[] __initconst = { "osc26m", "pclk", };
  47. static const char *const uart2_mux_p[] __initconst = { "osc26m", "pclk", };
  48. static const char *const uart3_mux_p[] __initconst = { "osc26m", "pclk", };
  49. static const char *const uart4_mux_p[] __initconst = { "osc26m", "pclk", };
  50. static const char *const spi0_mux_p[] __initconst = { "osc26m", "rclk_cfgaxi", };
  51. static const char *const spi1_mux_p[] __initconst = { "osc26m", "rclk_cfgaxi", };
  52. static const char *const spi2_mux_p[] __initconst = { "osc26m", "rclk_cfgaxi", };
  53. /* share axi parent */
  54. static const char *const saxi_mux_p[] __initconst = { "armpll3", "armpll2", };
  55. static const char *const pwm0_mux_p[] __initconst = { "osc32k", "osc26m", };
  56. static const char *const pwm1_mux_p[] __initconst = { "osc32k", "osc26m", };
  57. static const char *const sd_mux_p[] __initconst = { "armpll2", "armpll3", };
  58. static const char *const mmc1_mux_p[] __initconst = { "armpll2", "armpll3", };
  59. static const char *const mmc1_mux2_p[] __initconst = { "osc26m", "mmc1_div", };
  60. static const char *const g2d_mux_p[] __initconst = { "armpll2", "armpll3", };
  61. static const char *const venc_mux_p[] __initconst = { "armpll2", "armpll3", };
  62. static const char *const vdec_mux_p[] __initconst = { "armpll2", "armpll3", };
  63. static const char *const vpp_mux_p[] __initconst = { "armpll2", "armpll3", };
  64. static const char *const edc0_mux_p[] __initconst = { "armpll2", "armpll3", };
  65. static const char *const ldi0_mux_p[] __initconst = { "armpll2", "armpll4",
  66. "armpll3", "armpll5", };
  67. static const char *const edc1_mux_p[] __initconst = { "armpll2", "armpll3", };
  68. static const char *const ldi1_mux_p[] __initconst = { "armpll2", "armpll4",
  69. "armpll3", "armpll5", };
  70. static const char *const rclk_hsic_p[] __initconst = { "armpll3", "armpll2", };
  71. static const char *const mmc2_mux_p[] __initconst = { "armpll2", "armpll3", };
  72. static const char *const mmc3_mux_p[] __initconst = { "armpll2", "armpll3", };
  73. /* fixed rate clocks */
  74. static struct hisi_fixed_rate_clock hi3620_fixed_rate_clks[] __initdata = {
  75. { HI3620_OSC32K, "osc32k", NULL, CLK_IS_ROOT, 32768, },
  76. { HI3620_OSC26M, "osc26m", NULL, CLK_IS_ROOT, 26000000, },
  77. { HI3620_PCLK, "pclk", NULL, CLK_IS_ROOT, 26000000, },
  78. { HI3620_PLL_ARM0, "armpll0", NULL, CLK_IS_ROOT, 1600000000, },
  79. { HI3620_PLL_ARM1, "armpll1", NULL, CLK_IS_ROOT, 1600000000, },
  80. { HI3620_PLL_PERI, "armpll2", NULL, CLK_IS_ROOT, 1440000000, },
  81. { HI3620_PLL_USB, "armpll3", NULL, CLK_IS_ROOT, 1440000000, },
  82. { HI3620_PLL_HDMI, "armpll4", NULL, CLK_IS_ROOT, 1188000000, },
  83. { HI3620_PLL_GPU, "armpll5", NULL, CLK_IS_ROOT, 1300000000, },
  84. };
  85. /* fixed factor clocks */
  86. static struct hisi_fixed_factor_clock hi3620_fixed_factor_clks[] __initdata = {
  87. { HI3620_RCLK_TCXO, "rclk_tcxo", "osc26m", 1, 4, 0, },
  88. { HI3620_RCLK_CFGAXI, "rclk_cfgaxi", "armpll2", 1, 30, 0, },
  89. { HI3620_RCLK_PICO, "rclk_pico", "hsic_div", 1, 40, 0, },
  90. };
  91. static struct hisi_mux_clock hi3620_mux_clks[] __initdata = {
  92. { HI3620_TIMER0_MUX, "timer0_mux", timer0_mux_p, ARRAY_SIZE(timer0_mux_p), CLK_SET_RATE_PARENT, 0, 15, 2, 0, },
  93. { HI3620_TIMER1_MUX, "timer1_mux", timer1_mux_p, ARRAY_SIZE(timer1_mux_p), CLK_SET_RATE_PARENT, 0, 17, 2, 0, },
  94. { HI3620_TIMER2_MUX, "timer2_mux", timer2_mux_p, ARRAY_SIZE(timer2_mux_p), CLK_SET_RATE_PARENT, 0, 19, 2, 0, },
  95. { HI3620_TIMER3_MUX, "timer3_mux", timer3_mux_p, ARRAY_SIZE(timer3_mux_p), CLK_SET_RATE_PARENT, 0, 21, 2, 0, },
  96. { HI3620_TIMER4_MUX, "timer4_mux", timer4_mux_p, ARRAY_SIZE(timer4_mux_p), CLK_SET_RATE_PARENT, 0x18, 0, 2, 0, },
  97. { HI3620_TIMER5_MUX, "timer5_mux", timer5_mux_p, ARRAY_SIZE(timer5_mux_p), CLK_SET_RATE_PARENT, 0x18, 2, 2, 0, },
  98. { HI3620_TIMER6_MUX, "timer6_mux", timer6_mux_p, ARRAY_SIZE(timer6_mux_p), CLK_SET_RATE_PARENT, 0x18, 4, 2, 0, },
  99. { HI3620_TIMER7_MUX, "timer7_mux", timer7_mux_p, ARRAY_SIZE(timer7_mux_p), CLK_SET_RATE_PARENT, 0x18, 6, 2, 0, },
  100. { HI3620_TIMER8_MUX, "timer8_mux", timer8_mux_p, ARRAY_SIZE(timer8_mux_p), CLK_SET_RATE_PARENT, 0x18, 8, 2, 0, },
  101. { HI3620_TIMER9_MUX, "timer9_mux", timer9_mux_p, ARRAY_SIZE(timer9_mux_p), CLK_SET_RATE_PARENT, 0x18, 10, 2, 0, },
  102. { HI3620_UART0_MUX, "uart0_mux", uart0_mux_p, ARRAY_SIZE(uart0_mux_p), CLK_SET_RATE_PARENT, 0x100, 7, 1, CLK_MUX_HIWORD_MASK, },
  103. { HI3620_UART1_MUX, "uart1_mux", uart1_mux_p, ARRAY_SIZE(uart1_mux_p), CLK_SET_RATE_PARENT, 0x100, 8, 1, CLK_MUX_HIWORD_MASK, },
  104. { HI3620_UART2_MUX, "uart2_mux", uart2_mux_p, ARRAY_SIZE(uart2_mux_p), CLK_SET_RATE_PARENT, 0x100, 9, 1, CLK_MUX_HIWORD_MASK, },
  105. { HI3620_UART3_MUX, "uart3_mux", uart3_mux_p, ARRAY_SIZE(uart3_mux_p), CLK_SET_RATE_PARENT, 0x100, 10, 1, CLK_MUX_HIWORD_MASK, },
  106. { HI3620_UART4_MUX, "uart4_mux", uart4_mux_p, ARRAY_SIZE(uart4_mux_p), CLK_SET_RATE_PARENT, 0x100, 11, 1, CLK_MUX_HIWORD_MASK, },
  107. { HI3620_SPI0_MUX, "spi0_mux", spi0_mux_p, ARRAY_SIZE(spi0_mux_p), CLK_SET_RATE_PARENT, 0x100, 12, 1, CLK_MUX_HIWORD_MASK, },
  108. { HI3620_SPI1_MUX, "spi1_mux", spi1_mux_p, ARRAY_SIZE(spi1_mux_p), CLK_SET_RATE_PARENT, 0x100, 13, 1, CLK_MUX_HIWORD_MASK, },
  109. { HI3620_SPI2_MUX, "spi2_mux", spi2_mux_p, ARRAY_SIZE(spi2_mux_p), CLK_SET_RATE_PARENT, 0x100, 14, 1, CLK_MUX_HIWORD_MASK, },
  110. { HI3620_SAXI_MUX, "saxi_mux", saxi_mux_p, ARRAY_SIZE(saxi_mux_p), CLK_SET_RATE_PARENT, 0x100, 15, 1, CLK_MUX_HIWORD_MASK, },
  111. { HI3620_PWM0_MUX, "pwm0_mux", pwm0_mux_p, ARRAY_SIZE(pwm0_mux_p), CLK_SET_RATE_PARENT, 0x104, 10, 1, CLK_MUX_HIWORD_MASK, },
  112. { HI3620_PWM1_MUX, "pwm1_mux", pwm1_mux_p, ARRAY_SIZE(pwm1_mux_p), CLK_SET_RATE_PARENT, 0x104, 11, 1, CLK_MUX_HIWORD_MASK, },
  113. { HI3620_SD_MUX, "sd_mux", sd_mux_p, ARRAY_SIZE(sd_mux_p), CLK_SET_RATE_PARENT, 0x108, 4, 1, CLK_MUX_HIWORD_MASK, },
  114. { HI3620_MMC1_MUX, "mmc1_mux", mmc1_mux_p, ARRAY_SIZE(mmc1_mux_p), CLK_SET_RATE_PARENT, 0x108, 9, 1, CLK_MUX_HIWORD_MASK, },
  115. { HI3620_MMC1_MUX2, "mmc1_mux2", mmc1_mux2_p, ARRAY_SIZE(mmc1_mux2_p), CLK_SET_RATE_PARENT, 0x108, 10, 1, CLK_MUX_HIWORD_MASK, },
  116. { HI3620_G2D_MUX, "g2d_mux", g2d_mux_p, ARRAY_SIZE(g2d_mux_p), CLK_SET_RATE_PARENT, 0x10c, 5, 1, CLK_MUX_HIWORD_MASK, },
  117. { HI3620_VENC_MUX, "venc_mux", venc_mux_p, ARRAY_SIZE(venc_mux_p), CLK_SET_RATE_PARENT, 0x10c, 11, 1, CLK_MUX_HIWORD_MASK, },
  118. { HI3620_VDEC_MUX, "vdec_mux", vdec_mux_p, ARRAY_SIZE(vdec_mux_p), CLK_SET_RATE_PARENT, 0x110, 5, 1, CLK_MUX_HIWORD_MASK, },
  119. { HI3620_VPP_MUX, "vpp_mux", vpp_mux_p, ARRAY_SIZE(vpp_mux_p), CLK_SET_RATE_PARENT, 0x110, 11, 1, CLK_MUX_HIWORD_MASK, },
  120. { HI3620_EDC0_MUX, "edc0_mux", edc0_mux_p, ARRAY_SIZE(edc0_mux_p), CLK_SET_RATE_PARENT, 0x114, 6, 1, CLK_MUX_HIWORD_MASK, },
  121. { HI3620_LDI0_MUX, "ldi0_mux", ldi0_mux_p, ARRAY_SIZE(ldi0_mux_p), CLK_SET_RATE_PARENT, 0x114, 13, 2, CLK_MUX_HIWORD_MASK, },
  122. { HI3620_EDC1_MUX, "edc1_mux", edc1_mux_p, ARRAY_SIZE(edc1_mux_p), CLK_SET_RATE_PARENT, 0x118, 6, 1, CLK_MUX_HIWORD_MASK, },
  123. { HI3620_LDI1_MUX, "ldi1_mux", ldi1_mux_p, ARRAY_SIZE(ldi1_mux_p), CLK_SET_RATE_PARENT, 0x118, 14, 2, CLK_MUX_HIWORD_MASK, },
  124. { HI3620_RCLK_HSIC, "rclk_hsic", rclk_hsic_p, ARRAY_SIZE(rclk_hsic_p), CLK_SET_RATE_PARENT, 0x130, 2, 1, CLK_MUX_HIWORD_MASK, },
  125. { HI3620_MMC2_MUX, "mmc2_mux", mmc2_mux_p, ARRAY_SIZE(mmc2_mux_p), CLK_SET_RATE_PARENT, 0x140, 4, 1, CLK_MUX_HIWORD_MASK, },
  126. { HI3620_MMC3_MUX, "mmc3_mux", mmc3_mux_p, ARRAY_SIZE(mmc3_mux_p), CLK_SET_RATE_PARENT, 0x140, 9, 1, CLK_MUX_HIWORD_MASK, },
  127. };
  128. static struct hisi_divider_clock hi3620_div_clks[] __initdata = {
  129. { HI3620_SHAREAXI_DIV, "saxi_div", "saxi_mux", 0, 0x100, 0, 5, CLK_DIVIDER_HIWORD_MASK, NULL, },
  130. { HI3620_CFGAXI_DIV, "cfgaxi_div", "saxi_div", 0, 0x100, 5, 2, CLK_DIVIDER_HIWORD_MASK, NULL, },
  131. { HI3620_SD_DIV, "sd_div", "sd_mux", 0, 0x108, 0, 4, CLK_DIVIDER_HIWORD_MASK, NULL, },
  132. { HI3620_MMC1_DIV, "mmc1_div", "mmc1_mux", 0, 0x108, 5, 4, CLK_DIVIDER_HIWORD_MASK, NULL, },
  133. { HI3620_HSIC_DIV, "hsic_div", "rclk_hsic", 0, 0x130, 0, 2, CLK_DIVIDER_HIWORD_MASK, NULL, },
  134. { HI3620_MMC2_DIV, "mmc2_div", "mmc2_mux", 0, 0x140, 0, 4, CLK_DIVIDER_HIWORD_MASK, NULL, },
  135. { HI3620_MMC3_DIV, "mmc3_div", "mmc3_mux", 0, 0x140, 5, 4, CLK_DIVIDER_HIWORD_MASK, NULL, },
  136. };
  137. static struct hisi_gate_clock hi3620_seperated_gate_clks[] __initdata = {
  138. { HI3620_TIMERCLK01, "timerclk01", "timer_rclk01", CLK_SET_RATE_PARENT, 0x20, 0, 0, },
  139. { HI3620_TIMER_RCLK01, "timer_rclk01", "rclk_tcxo", CLK_SET_RATE_PARENT, 0x20, 1, 0, },
  140. { HI3620_TIMERCLK23, "timerclk23", "timer_rclk23", CLK_SET_RATE_PARENT, 0x20, 2, 0, },
  141. { HI3620_TIMER_RCLK23, "timer_rclk23", "rclk_tcxo", CLK_SET_RATE_PARENT, 0x20, 3, 0, },
  142. { HI3620_RTCCLK, "rtcclk", "pclk", CLK_SET_RATE_PARENT, 0x20, 5, 0, },
  143. { HI3620_KPC_CLK, "kpc_clk", "pclk", CLK_SET_RATE_PARENT, 0x20, 6, 0, },
  144. { HI3620_GPIOCLK0, "gpioclk0", "pclk", CLK_SET_RATE_PARENT, 0x20, 8, 0, },
  145. { HI3620_GPIOCLK1, "gpioclk1", "pclk", CLK_SET_RATE_PARENT, 0x20, 9, 0, },
  146. { HI3620_GPIOCLK2, "gpioclk2", "pclk", CLK_SET_RATE_PARENT, 0x20, 10, 0, },
  147. { HI3620_GPIOCLK3, "gpioclk3", "pclk", CLK_SET_RATE_PARENT, 0x20, 11, 0, },
  148. { HI3620_GPIOCLK4, "gpioclk4", "pclk", CLK_SET_RATE_PARENT, 0x20, 12, 0, },
  149. { HI3620_GPIOCLK5, "gpioclk5", "pclk", CLK_SET_RATE_PARENT, 0x20, 13, 0, },
  150. { HI3620_GPIOCLK6, "gpioclk6", "pclk", CLK_SET_RATE_PARENT, 0x20, 14, 0, },
  151. { HI3620_GPIOCLK7, "gpioclk7", "pclk", CLK_SET_RATE_PARENT, 0x20, 15, 0, },
  152. { HI3620_GPIOCLK8, "gpioclk8", "pclk", CLK_SET_RATE_PARENT, 0x20, 16, 0, },
  153. { HI3620_GPIOCLK9, "gpioclk9", "pclk", CLK_SET_RATE_PARENT, 0x20, 17, 0, },
  154. { HI3620_GPIOCLK10, "gpioclk10", "pclk", CLK_SET_RATE_PARENT, 0x20, 18, 0, },
  155. { HI3620_GPIOCLK11, "gpioclk11", "pclk", CLK_SET_RATE_PARENT, 0x20, 19, 0, },
  156. { HI3620_GPIOCLK12, "gpioclk12", "pclk", CLK_SET_RATE_PARENT, 0x20, 20, 0, },
  157. { HI3620_GPIOCLK13, "gpioclk13", "pclk", CLK_SET_RATE_PARENT, 0x20, 21, 0, },
  158. { HI3620_GPIOCLK14, "gpioclk14", "pclk", CLK_SET_RATE_PARENT, 0x20, 22, 0, },
  159. { HI3620_GPIOCLK15, "gpioclk15", "pclk", CLK_SET_RATE_PARENT, 0x20, 23, 0, },
  160. { HI3620_GPIOCLK16, "gpioclk16", "pclk", CLK_SET_RATE_PARENT, 0x20, 24, 0, },
  161. { HI3620_GPIOCLK17, "gpioclk17", "pclk", CLK_SET_RATE_PARENT, 0x20, 25, 0, },
  162. { HI3620_GPIOCLK18, "gpioclk18", "pclk", CLK_SET_RATE_PARENT, 0x20, 26, 0, },
  163. { HI3620_GPIOCLK19, "gpioclk19", "pclk", CLK_SET_RATE_PARENT, 0x20, 27, 0, },
  164. { HI3620_GPIOCLK20, "gpioclk20", "pclk", CLK_SET_RATE_PARENT, 0x20, 28, 0, },
  165. { HI3620_GPIOCLK21, "gpioclk21", "pclk", CLK_SET_RATE_PARENT, 0x20, 29, 0, },
  166. { HI3620_DPHY0_CLK, "dphy0_clk", "osc26m", CLK_SET_RATE_PARENT, 0x30, 15, 0, },
  167. { HI3620_DPHY1_CLK, "dphy1_clk", "osc26m", CLK_SET_RATE_PARENT, 0x30, 16, 0, },
  168. { HI3620_DPHY2_CLK, "dphy2_clk", "osc26m", CLK_SET_RATE_PARENT, 0x30, 17, 0, },
  169. { HI3620_USBPHY_CLK, "usbphy_clk", "rclk_pico", CLK_SET_RATE_PARENT, 0x30, 24, 0, },
  170. { HI3620_ACP_CLK, "acp_clk", "rclk_cfgaxi", CLK_SET_RATE_PARENT, 0x30, 28, 0, },
  171. { HI3620_TIMERCLK45, "timerclk45", "rclk_tcxo", CLK_SET_RATE_PARENT, 0x40, 3, 0, },
  172. { HI3620_TIMERCLK67, "timerclk67", "rclk_tcxo", CLK_SET_RATE_PARENT, 0x40, 4, 0, },
  173. { HI3620_TIMERCLK89, "timerclk89", "rclk_tcxo", CLK_SET_RATE_PARENT, 0x40, 5, 0, },
  174. { HI3620_PWMCLK0, "pwmclk0", "pwm0_mux", CLK_SET_RATE_PARENT, 0x40, 7, 0, },
  175. { HI3620_PWMCLK1, "pwmclk1", "pwm1_mux", CLK_SET_RATE_PARENT, 0x40, 8, 0, },
  176. { HI3620_UARTCLK0, "uartclk0", "uart0_mux", CLK_SET_RATE_PARENT, 0x40, 16, 0, },
  177. { HI3620_UARTCLK1, "uartclk1", "uart1_mux", CLK_SET_RATE_PARENT, 0x40, 17, 0, },
  178. { HI3620_UARTCLK2, "uartclk2", "uart2_mux", CLK_SET_RATE_PARENT, 0x40, 18, 0, },
  179. { HI3620_UARTCLK3, "uartclk3", "uart3_mux", CLK_SET_RATE_PARENT, 0x40, 19, 0, },
  180. { HI3620_UARTCLK4, "uartclk4", "uart4_mux", CLK_SET_RATE_PARENT, 0x40, 20, 0, },
  181. { HI3620_SPICLK0, "spiclk0", "spi0_mux", CLK_SET_RATE_PARENT, 0x40, 21, 0, },
  182. { HI3620_SPICLK1, "spiclk1", "spi1_mux", CLK_SET_RATE_PARENT, 0x40, 22, 0, },
  183. { HI3620_SPICLK2, "spiclk2", "spi2_mux", CLK_SET_RATE_PARENT, 0x40, 23, 0, },
  184. { HI3620_I2CCLK0, "i2cclk0", "pclk", CLK_SET_RATE_PARENT, 0x40, 24, 0, },
  185. { HI3620_I2CCLK1, "i2cclk1", "pclk", CLK_SET_RATE_PARENT, 0x40, 25, 0, },
  186. { HI3620_SCI_CLK, "sci_clk", "osc26m", CLK_SET_RATE_PARENT, 0x40, 26, 0, },
  187. { HI3620_I2CCLK2, "i2cclk2", "pclk", CLK_SET_RATE_PARENT, 0x40, 28, 0, },
  188. { HI3620_I2CCLK3, "i2cclk3", "pclk", CLK_SET_RATE_PARENT, 0x40, 29, 0, },
  189. { HI3620_DDRC_PER_CLK, "ddrc_per_clk", "rclk_cfgaxi", CLK_SET_RATE_PARENT, 0x50, 9, 0, },
  190. { HI3620_DMAC_CLK, "dmac_clk", "rclk_cfgaxi", CLK_SET_RATE_PARENT, 0x50, 10, 0, },
  191. { HI3620_USB2DVC_CLK, "usb2dvc_clk", "rclk_cfgaxi", CLK_SET_RATE_PARENT, 0x50, 17, 0, },
  192. { HI3620_SD_CLK, "sd_clk", "sd_div", CLK_SET_RATE_PARENT, 0x50, 20, 0, },
  193. { HI3620_MMC_CLK1, "mmc_clk1", "mmc1_mux2", CLK_SET_RATE_PARENT, 0x50, 21, 0, },
  194. { HI3620_MMC_CLK2, "mmc_clk2", "mmc2_div", CLK_SET_RATE_PARENT, 0x50, 22, 0, },
  195. { HI3620_MMC_CLK3, "mmc_clk3", "mmc3_div", CLK_SET_RATE_PARENT, 0x50, 23, 0, },
  196. { HI3620_MCU_CLK, "mcu_clk", "acp_clk", CLK_SET_RATE_PARENT, 0x50, 24, 0, },
  197. };
  198. static void __init hi3620_clk_init(struct device_node *np)
  199. {
  200. struct hisi_clock_data *clk_data;
  201. clk_data = hisi_clk_init(np, HI3620_NR_CLKS);
  202. if (!clk_data)
  203. return;
  204. hisi_clk_register_fixed_rate(hi3620_fixed_rate_clks,
  205. ARRAY_SIZE(hi3620_fixed_rate_clks),
  206. clk_data);
  207. hisi_clk_register_fixed_factor(hi3620_fixed_factor_clks,
  208. ARRAY_SIZE(hi3620_fixed_factor_clks),
  209. clk_data);
  210. hisi_clk_register_mux(hi3620_mux_clks, ARRAY_SIZE(hi3620_mux_clks),
  211. clk_data);
  212. hisi_clk_register_divider(hi3620_div_clks, ARRAY_SIZE(hi3620_div_clks),
  213. clk_data);
  214. hisi_clk_register_gate_sep(hi3620_seperated_gate_clks,
  215. ARRAY_SIZE(hi3620_seperated_gate_clks),
  216. clk_data);
  217. }
  218. CLK_OF_DECLARE(hi3620_clk, "hisilicon,hi3620-clock", hi3620_clk_init);
  219. struct hisi_mmc_clock {
  220. unsigned int id;
  221. const char *name;
  222. const char *parent_name;
  223. unsigned long flags;
  224. u32 clken_reg;
  225. u32 clken_bit;
  226. u32 div_reg;
  227. u32 div_off;
  228. u32 div_bits;
  229. u32 drv_reg;
  230. u32 drv_off;
  231. u32 drv_bits;
  232. u32 sam_reg;
  233. u32 sam_off;
  234. u32 sam_bits;
  235. };
  236. struct clk_mmc {
  237. struct clk_hw hw;
  238. u32 id;
  239. void __iomem *clken_reg;
  240. u32 clken_bit;
  241. void __iomem *div_reg;
  242. u32 div_off;
  243. u32 div_bits;
  244. void __iomem *drv_reg;
  245. u32 drv_off;
  246. u32 drv_bits;
  247. void __iomem *sam_reg;
  248. u32 sam_off;
  249. u32 sam_bits;
  250. };
  251. #define to_mmc(_hw) container_of(_hw, struct clk_mmc, hw)
  252. static struct hisi_mmc_clock hi3620_mmc_clks[] __initdata = {
  253. { HI3620_SD_CIUCLK, "sd_bclk1", "sd_clk", CLK_SET_RATE_PARENT, 0x1f8, 0, 0x1f8, 1, 3, 0x1f8, 4, 4, 0x1f8, 8, 4},
  254. { HI3620_MMC_CIUCLK1, "mmc_bclk1", "mmc_clk1", CLK_SET_RATE_PARENT, 0x1f8, 12, 0x1f8, 13, 3, 0x1f8, 16, 4, 0x1f8, 20, 4},
  255. { HI3620_MMC_CIUCLK2, "mmc_bclk2", "mmc_clk2", CLK_SET_RATE_PARENT, 0x1f8, 24, 0x1f8, 25, 3, 0x1f8, 28, 4, 0x1fc, 0, 4},
  256. { HI3620_MMC_CIUCLK3, "mmc_bclk3", "mmc_clk3", CLK_SET_RATE_PARENT, 0x1fc, 4, 0x1fc, 5, 3, 0x1fc, 8, 4, 0x1fc, 12, 4},
  257. };
  258. static unsigned long mmc_clk_recalc_rate(struct clk_hw *hw,
  259. unsigned long parent_rate)
  260. {
  261. switch (parent_rate) {
  262. case 26000000:
  263. return 13000000;
  264. case 180000000:
  265. return 25000000;
  266. case 360000000:
  267. return 50000000;
  268. case 720000000:
  269. return 100000000;
  270. case 1440000000:
  271. return 180000000;
  272. default:
  273. return parent_rate;
  274. }
  275. }
  276. static int mmc_clk_determine_rate(struct clk_hw *hw,
  277. struct clk_rate_request *req)
  278. {
  279. struct clk_mmc *mclk = to_mmc(hw);
  280. if ((req->rate <= 13000000) && (mclk->id == HI3620_MMC_CIUCLK1)) {
  281. req->rate = 13000000;
  282. req->best_parent_rate = 26000000;
  283. } else if (req->rate <= 26000000) {
  284. req->rate = 25000000;
  285. req->best_parent_rate = 180000000;
  286. } else if (req->rate <= 52000000) {
  287. req->rate = 50000000;
  288. req->best_parent_rate = 360000000;
  289. } else if (req->rate <= 100000000) {
  290. req->rate = 100000000;
  291. req->best_parent_rate = 720000000;
  292. } else {
  293. /* max is 180M */
  294. req->rate = 180000000;
  295. req->best_parent_rate = 1440000000;
  296. }
  297. return -EINVAL;
  298. }
  299. static u32 mmc_clk_delay(u32 val, u32 para, u32 off, u32 len)
  300. {
  301. u32 i;
  302. for (i = 0; i < len; i++) {
  303. if (para % 2)
  304. val |= 1 << (off + i);
  305. else
  306. val &= ~(1 << (off + i));
  307. para = para >> 1;
  308. }
  309. return val;
  310. }
  311. static int mmc_clk_set_timing(struct clk_hw *hw, unsigned long rate)
  312. {
  313. struct clk_mmc *mclk = to_mmc(hw);
  314. unsigned long flags;
  315. u32 sam, drv, div, val;
  316. static DEFINE_SPINLOCK(mmc_clk_lock);
  317. switch (rate) {
  318. case 13000000:
  319. sam = 3;
  320. drv = 1;
  321. div = 1;
  322. break;
  323. case 25000000:
  324. sam = 13;
  325. drv = 6;
  326. div = 6;
  327. break;
  328. case 50000000:
  329. sam = 3;
  330. drv = 6;
  331. div = 6;
  332. break;
  333. case 100000000:
  334. sam = 6;
  335. drv = 4;
  336. div = 6;
  337. break;
  338. case 180000000:
  339. sam = 6;
  340. drv = 4;
  341. div = 7;
  342. break;
  343. default:
  344. return -EINVAL;
  345. }
  346. spin_lock_irqsave(&mmc_clk_lock, flags);
  347. val = readl_relaxed(mclk->clken_reg);
  348. val &= ~(1 << mclk->clken_bit);
  349. writel_relaxed(val, mclk->clken_reg);
  350. val = readl_relaxed(mclk->sam_reg);
  351. val = mmc_clk_delay(val, sam, mclk->sam_off, mclk->sam_bits);
  352. writel_relaxed(val, mclk->sam_reg);
  353. val = readl_relaxed(mclk->drv_reg);
  354. val = mmc_clk_delay(val, drv, mclk->drv_off, mclk->drv_bits);
  355. writel_relaxed(val, mclk->drv_reg);
  356. val = readl_relaxed(mclk->div_reg);
  357. val = mmc_clk_delay(val, div, mclk->div_off, mclk->div_bits);
  358. writel_relaxed(val, mclk->div_reg);
  359. val = readl_relaxed(mclk->clken_reg);
  360. val |= 1 << mclk->clken_bit;
  361. writel_relaxed(val, mclk->clken_reg);
  362. spin_unlock_irqrestore(&mmc_clk_lock, flags);
  363. return 0;
  364. }
  365. static int mmc_clk_prepare(struct clk_hw *hw)
  366. {
  367. struct clk_mmc *mclk = to_mmc(hw);
  368. unsigned long rate;
  369. if (mclk->id == HI3620_MMC_CIUCLK1)
  370. rate = 13000000;
  371. else
  372. rate = 25000000;
  373. return mmc_clk_set_timing(hw, rate);
  374. }
  375. static int mmc_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  376. unsigned long parent_rate)
  377. {
  378. return mmc_clk_set_timing(hw, rate);
  379. }
  380. static struct clk_ops clk_mmc_ops = {
  381. .prepare = mmc_clk_prepare,
  382. .determine_rate = mmc_clk_determine_rate,
  383. .set_rate = mmc_clk_set_rate,
  384. .recalc_rate = mmc_clk_recalc_rate,
  385. };
  386. static struct clk *hisi_register_clk_mmc(struct hisi_mmc_clock *mmc_clk,
  387. void __iomem *base, struct device_node *np)
  388. {
  389. struct clk_mmc *mclk;
  390. struct clk *clk;
  391. struct clk_init_data init;
  392. mclk = kzalloc(sizeof(*mclk), GFP_KERNEL);
  393. if (!mclk) {
  394. pr_err("%s: fail to allocate mmc clk\n", __func__);
  395. return ERR_PTR(-ENOMEM);
  396. }
  397. init.name = mmc_clk->name;
  398. init.ops = &clk_mmc_ops;
  399. init.flags = mmc_clk->flags | CLK_IS_BASIC;
  400. init.parent_names = (mmc_clk->parent_name ? &mmc_clk->parent_name : NULL);
  401. init.num_parents = (mmc_clk->parent_name ? 1 : 0);
  402. mclk->hw.init = &init;
  403. mclk->id = mmc_clk->id;
  404. mclk->clken_reg = base + mmc_clk->clken_reg;
  405. mclk->clken_bit = mmc_clk->clken_bit;
  406. mclk->div_reg = base + mmc_clk->div_reg;
  407. mclk->div_off = mmc_clk->div_off;
  408. mclk->div_bits = mmc_clk->div_bits;
  409. mclk->drv_reg = base + mmc_clk->drv_reg;
  410. mclk->drv_off = mmc_clk->drv_off;
  411. mclk->drv_bits = mmc_clk->drv_bits;
  412. mclk->sam_reg = base + mmc_clk->sam_reg;
  413. mclk->sam_off = mmc_clk->sam_off;
  414. mclk->sam_bits = mmc_clk->sam_bits;
  415. clk = clk_register(NULL, &mclk->hw);
  416. if (WARN_ON(IS_ERR(clk)))
  417. kfree(mclk);
  418. return clk;
  419. }
  420. static void __init hi3620_mmc_clk_init(struct device_node *node)
  421. {
  422. void __iomem *base;
  423. int i, num = ARRAY_SIZE(hi3620_mmc_clks);
  424. struct clk_onecell_data *clk_data;
  425. if (!node) {
  426. pr_err("failed to find pctrl node in DTS\n");
  427. return;
  428. }
  429. base = of_iomap(node, 0);
  430. if (!base) {
  431. pr_err("failed to map pctrl\n");
  432. return;
  433. }
  434. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  435. if (WARN_ON(!clk_data))
  436. return;
  437. clk_data->clks = kzalloc(sizeof(struct clk *) * num, GFP_KERNEL);
  438. if (!clk_data->clks) {
  439. pr_err("%s: fail to allocate mmc clk\n", __func__);
  440. return;
  441. }
  442. for (i = 0; i < num; i++) {
  443. struct hisi_mmc_clock *mmc_clk = &hi3620_mmc_clks[i];
  444. clk_data->clks[mmc_clk->id] =
  445. hisi_register_clk_mmc(mmc_clk, base, node);
  446. }
  447. clk_data->clk_num = num;
  448. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  449. }
  450. CLK_OF_DECLARE(hi3620_mmc_clk, "hisilicon,hi3620-mmc-clock", hi3620_mmc_clk_init);