clk-lpc18xx-ccu.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Clk driver for NXP LPC18xx/LPC43xx Clock Control Unit (CCU)
  4. *
  5. * Copyright (C) 2015 Joachim Eastwood <[email protected]>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/io.h>
  10. #include <linux/kernel.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/slab.h>
  14. #include <linux/string.h>
  15. #include <dt-bindings/clock/lpc18xx-ccu.h>
  16. /* Bit defines for CCU branch configuration register */
  17. #define LPC18XX_CCU_RUN BIT(0)
  18. #define LPC18XX_CCU_AUTO BIT(1)
  19. #define LPC18XX_CCU_DIV BIT(5)
  20. #define LPC18XX_CCU_DIVSTAT BIT(27)
  21. /* CCU branch feature bits */
  22. #define CCU_BRANCH_IS_BUS BIT(0)
  23. #define CCU_BRANCH_HAVE_DIV2 BIT(1)
  24. struct lpc18xx_branch_clk_data {
  25. const char **name;
  26. int num;
  27. };
  28. struct lpc18xx_clk_branch {
  29. const char *base_name;
  30. const char *name;
  31. u16 offset;
  32. u16 flags;
  33. struct clk *clk;
  34. struct clk_gate gate;
  35. };
  36. static struct lpc18xx_clk_branch clk_branches[] = {
  37. {"base_apb3_clk", "apb3_bus", CLK_APB3_BUS, CCU_BRANCH_IS_BUS},
  38. {"base_apb3_clk", "apb3_i2c1", CLK_APB3_I2C1, 0},
  39. {"base_apb3_clk", "apb3_dac", CLK_APB3_DAC, 0},
  40. {"base_apb3_clk", "apb3_adc0", CLK_APB3_ADC0, 0},
  41. {"base_apb3_clk", "apb3_adc1", CLK_APB3_ADC1, 0},
  42. {"base_apb3_clk", "apb3_can0", CLK_APB3_CAN0, 0},
  43. {"base_apb1_clk", "apb1_bus", CLK_APB1_BUS, CCU_BRANCH_IS_BUS},
  44. {"base_apb1_clk", "apb1_mc_pwm", CLK_APB1_MOTOCON_PWM, 0},
  45. {"base_apb1_clk", "apb1_i2c0", CLK_APB1_I2C0, 0},
  46. {"base_apb1_clk", "apb1_i2s", CLK_APB1_I2S, 0},
  47. {"base_apb1_clk", "apb1_can1", CLK_APB1_CAN1, 0},
  48. {"base_spifi_clk", "spifi", CLK_SPIFI, 0},
  49. {"base_cpu_clk", "cpu_bus", CLK_CPU_BUS, CCU_BRANCH_IS_BUS},
  50. {"base_cpu_clk", "cpu_spifi", CLK_CPU_SPIFI, 0},
  51. {"base_cpu_clk", "cpu_gpio", CLK_CPU_GPIO, 0},
  52. {"base_cpu_clk", "cpu_lcd", CLK_CPU_LCD, 0},
  53. {"base_cpu_clk", "cpu_ethernet", CLK_CPU_ETHERNET, 0},
  54. {"base_cpu_clk", "cpu_usb0", CLK_CPU_USB0, 0},
  55. {"base_cpu_clk", "cpu_emc", CLK_CPU_EMC, 0},
  56. {"base_cpu_clk", "cpu_sdio", CLK_CPU_SDIO, 0},
  57. {"base_cpu_clk", "cpu_dma", CLK_CPU_DMA, 0},
  58. {"base_cpu_clk", "cpu_core", CLK_CPU_CORE, 0},
  59. {"base_cpu_clk", "cpu_sct", CLK_CPU_SCT, 0},
  60. {"base_cpu_clk", "cpu_usb1", CLK_CPU_USB1, 0},
  61. {"base_cpu_clk", "cpu_emcdiv", CLK_CPU_EMCDIV, CCU_BRANCH_HAVE_DIV2},
  62. {"base_cpu_clk", "cpu_flasha", CLK_CPU_FLASHA, CCU_BRANCH_HAVE_DIV2},
  63. {"base_cpu_clk", "cpu_flashb", CLK_CPU_FLASHB, CCU_BRANCH_HAVE_DIV2},
  64. {"base_cpu_clk", "cpu_m0app", CLK_CPU_M0APP, CCU_BRANCH_HAVE_DIV2},
  65. {"base_cpu_clk", "cpu_adchs", CLK_CPU_ADCHS, CCU_BRANCH_HAVE_DIV2},
  66. {"base_cpu_clk", "cpu_eeprom", CLK_CPU_EEPROM, CCU_BRANCH_HAVE_DIV2},
  67. {"base_cpu_clk", "cpu_wwdt", CLK_CPU_WWDT, 0},
  68. {"base_cpu_clk", "cpu_uart0", CLK_CPU_UART0, 0},
  69. {"base_cpu_clk", "cpu_uart1", CLK_CPU_UART1, 0},
  70. {"base_cpu_clk", "cpu_ssp0", CLK_CPU_SSP0, 0},
  71. {"base_cpu_clk", "cpu_timer0", CLK_CPU_TIMER0, 0},
  72. {"base_cpu_clk", "cpu_timer1", CLK_CPU_TIMER1, 0},
  73. {"base_cpu_clk", "cpu_scu", CLK_CPU_SCU, 0},
  74. {"base_cpu_clk", "cpu_creg", CLK_CPU_CREG, 0},
  75. {"base_cpu_clk", "cpu_ritimer", CLK_CPU_RITIMER, 0},
  76. {"base_cpu_clk", "cpu_uart2", CLK_CPU_UART2, 0},
  77. {"base_cpu_clk", "cpu_uart3", CLK_CPU_UART3, 0},
  78. {"base_cpu_clk", "cpu_timer2", CLK_CPU_TIMER2, 0},
  79. {"base_cpu_clk", "cpu_timer3", CLK_CPU_TIMER3, 0},
  80. {"base_cpu_clk", "cpu_ssp1", CLK_CPU_SSP1, 0},
  81. {"base_cpu_clk", "cpu_qei", CLK_CPU_QEI, 0},
  82. {"base_periph_clk", "periph_bus", CLK_PERIPH_BUS, CCU_BRANCH_IS_BUS},
  83. {"base_periph_clk", "periph_core", CLK_PERIPH_CORE, 0},
  84. {"base_periph_clk", "periph_sgpio", CLK_PERIPH_SGPIO, 0},
  85. {"base_usb0_clk", "usb0", CLK_USB0, 0},
  86. {"base_usb1_clk", "usb1", CLK_USB1, 0},
  87. {"base_spi_clk", "spi", CLK_SPI, 0},
  88. {"base_adchs_clk", "adchs", CLK_ADCHS, 0},
  89. {"base_audio_clk", "audio", CLK_AUDIO, 0},
  90. {"base_uart3_clk", "apb2_uart3", CLK_APB2_UART3, 0},
  91. {"base_uart2_clk", "apb2_uart2", CLK_APB2_UART2, 0},
  92. {"base_uart1_clk", "apb0_uart1", CLK_APB0_UART1, 0},
  93. {"base_uart0_clk", "apb0_uart0", CLK_APB0_UART0, 0},
  94. {"base_ssp1_clk", "apb2_ssp1", CLK_APB2_SSP1, 0},
  95. {"base_ssp0_clk", "apb0_ssp0", CLK_APB0_SSP0, 0},
  96. {"base_sdio_clk", "sdio", CLK_SDIO, 0},
  97. };
  98. static struct clk *lpc18xx_ccu_branch_clk_get(struct of_phandle_args *clkspec,
  99. void *data)
  100. {
  101. struct lpc18xx_branch_clk_data *clk_data = data;
  102. unsigned int offset = clkspec->args[0];
  103. int i, j;
  104. for (i = 0; i < ARRAY_SIZE(clk_branches); i++) {
  105. if (clk_branches[i].offset != offset)
  106. continue;
  107. for (j = 0; j < clk_data->num; j++) {
  108. if (!strcmp(clk_branches[i].base_name, clk_data->name[j]))
  109. return clk_branches[i].clk;
  110. }
  111. }
  112. pr_err("%s: invalid clock offset %d\n", __func__, offset);
  113. return ERR_PTR(-EINVAL);
  114. }
  115. static int lpc18xx_ccu_gate_endisable(struct clk_hw *hw, bool enable)
  116. {
  117. struct clk_gate *gate = to_clk_gate(hw);
  118. u32 val;
  119. /*
  120. * Divider field is write only, so divider stat field must
  121. * be read so divider field can be set accordingly.
  122. */
  123. val = readl(gate->reg);
  124. if (val & LPC18XX_CCU_DIVSTAT)
  125. val |= LPC18XX_CCU_DIV;
  126. if (enable) {
  127. val |= LPC18XX_CCU_RUN;
  128. } else {
  129. /*
  130. * To safely disable a branch clock a squence of two separate
  131. * writes must be used. First write should set the AUTO bit
  132. * and the next write should clear the RUN bit.
  133. */
  134. val |= LPC18XX_CCU_AUTO;
  135. writel(val, gate->reg);
  136. val &= ~LPC18XX_CCU_RUN;
  137. }
  138. writel(val, gate->reg);
  139. return 0;
  140. }
  141. static int lpc18xx_ccu_gate_enable(struct clk_hw *hw)
  142. {
  143. return lpc18xx_ccu_gate_endisable(hw, true);
  144. }
  145. static void lpc18xx_ccu_gate_disable(struct clk_hw *hw)
  146. {
  147. lpc18xx_ccu_gate_endisable(hw, false);
  148. }
  149. static int lpc18xx_ccu_gate_is_enabled(struct clk_hw *hw)
  150. {
  151. const struct clk_hw *parent;
  152. /*
  153. * The branch clock registers are only accessible
  154. * if the base (parent) clock is enabled. Register
  155. * access with a disabled base clock will hang the
  156. * system.
  157. */
  158. parent = clk_hw_get_parent(hw);
  159. if (!parent)
  160. return 0;
  161. if (!clk_hw_is_enabled(parent))
  162. return 0;
  163. return clk_gate_ops.is_enabled(hw);
  164. }
  165. static const struct clk_ops lpc18xx_ccu_gate_ops = {
  166. .enable = lpc18xx_ccu_gate_enable,
  167. .disable = lpc18xx_ccu_gate_disable,
  168. .is_enabled = lpc18xx_ccu_gate_is_enabled,
  169. };
  170. static void lpc18xx_ccu_register_branch_gate_div(struct lpc18xx_clk_branch *branch,
  171. void __iomem *reg_base,
  172. const char *parent)
  173. {
  174. const struct clk_ops *div_ops = NULL;
  175. struct clk_divider *div = NULL;
  176. struct clk_hw *div_hw = NULL;
  177. if (branch->flags & CCU_BRANCH_HAVE_DIV2) {
  178. div = kzalloc(sizeof(*div), GFP_KERNEL);
  179. if (!div)
  180. return;
  181. div->reg = branch->offset + reg_base;
  182. div->flags = CLK_DIVIDER_READ_ONLY;
  183. div->shift = 27;
  184. div->width = 1;
  185. div_hw = &div->hw;
  186. div_ops = &clk_divider_ro_ops;
  187. }
  188. branch->gate.reg = branch->offset + reg_base;
  189. branch->gate.bit_idx = 0;
  190. branch->clk = clk_register_composite(NULL, branch->name, &parent, 1,
  191. NULL, NULL,
  192. div_hw, div_ops,
  193. &branch->gate.hw, &lpc18xx_ccu_gate_ops, 0);
  194. if (IS_ERR(branch->clk)) {
  195. kfree(div);
  196. pr_warn("%s: failed to register %s\n", __func__, branch->name);
  197. return;
  198. }
  199. /* Grab essential branch clocks for CPU and SDRAM */
  200. switch (branch->offset) {
  201. case CLK_CPU_EMC:
  202. case CLK_CPU_CORE:
  203. case CLK_CPU_CREG:
  204. case CLK_CPU_EMCDIV:
  205. clk_prepare_enable(branch->clk);
  206. }
  207. }
  208. static void lpc18xx_ccu_register_branch_clks(void __iomem *reg_base,
  209. const char *base_name)
  210. {
  211. const char *parent = base_name;
  212. int i;
  213. for (i = 0; i < ARRAY_SIZE(clk_branches); i++) {
  214. if (strcmp(clk_branches[i].base_name, base_name))
  215. continue;
  216. lpc18xx_ccu_register_branch_gate_div(&clk_branches[i], reg_base,
  217. parent);
  218. if (clk_branches[i].flags & CCU_BRANCH_IS_BUS)
  219. parent = clk_branches[i].name;
  220. }
  221. }
  222. static void __init lpc18xx_ccu_init(struct device_node *np)
  223. {
  224. struct lpc18xx_branch_clk_data *clk_data;
  225. void __iomem *reg_base;
  226. int i, ret;
  227. reg_base = of_iomap(np, 0);
  228. if (!reg_base) {
  229. pr_warn("%s: failed to map address range\n", __func__);
  230. return;
  231. }
  232. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  233. if (!clk_data) {
  234. iounmap(reg_base);
  235. return;
  236. }
  237. clk_data->num = of_property_count_strings(np, "clock-names");
  238. clk_data->name = kcalloc(clk_data->num, sizeof(char *), GFP_KERNEL);
  239. if (!clk_data->name) {
  240. iounmap(reg_base);
  241. kfree(clk_data);
  242. return;
  243. }
  244. for (i = 0; i < clk_data->num; i++) {
  245. ret = of_property_read_string_index(np, "clock-names", i,
  246. &clk_data->name[i]);
  247. if (ret) {
  248. pr_warn("%s: failed to get clock name at idx %d\n",
  249. __func__, i);
  250. continue;
  251. }
  252. lpc18xx_ccu_register_branch_clks(reg_base, clk_data->name[i]);
  253. }
  254. of_clk_add_provider(np, lpc18xx_ccu_branch_clk_get, clk_data);
  255. }
  256. CLK_OF_DECLARE(lpc18xx_ccu, "nxp,lpc1850-ccu", lpc18xx_ccu_init);