clk-lpc18xx-ccu.c 8.8 KB

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