clk-asm9260.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (c) 2014 Oleksij Rempel <linux@rempel-privat.de>.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/clkdev.h>
  18. #include <linux/err.h>
  19. #include <linux/io.h>
  20. #include <linux/clk-provider.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #include <dt-bindings/clock/alphascale,asm9260.h>
  25. #define HW_AHBCLKCTRL0 0x0020
  26. #define HW_AHBCLKCTRL1 0x0030
  27. #define HW_SYSPLLCTRL 0x0100
  28. #define HW_MAINCLKSEL 0x0120
  29. #define HW_MAINCLKUEN 0x0124
  30. #define HW_UARTCLKSEL 0x0128
  31. #define HW_UARTCLKUEN 0x012c
  32. #define HW_I2S0CLKSEL 0x0130
  33. #define HW_I2S0CLKUEN 0x0134
  34. #define HW_I2S1CLKSEL 0x0138
  35. #define HW_I2S1CLKUEN 0x013c
  36. #define HW_WDTCLKSEL 0x0160
  37. #define HW_WDTCLKUEN 0x0164
  38. #define HW_CLKOUTCLKSEL 0x0170
  39. #define HW_CLKOUTCLKUEN 0x0174
  40. #define HW_CPUCLKDIV 0x017c
  41. #define HW_SYSAHBCLKDIV 0x0180
  42. #define HW_I2S0MCLKDIV 0x0190
  43. #define HW_I2S0SCLKDIV 0x0194
  44. #define HW_I2S1MCLKDIV 0x0188
  45. #define HW_I2S1SCLKDIV 0x018c
  46. #define HW_UART0CLKDIV 0x0198
  47. #define HW_UART1CLKDIV 0x019c
  48. #define HW_UART2CLKDIV 0x01a0
  49. #define HW_UART3CLKDIV 0x01a4
  50. #define HW_UART4CLKDIV 0x01a8
  51. #define HW_UART5CLKDIV 0x01ac
  52. #define HW_UART6CLKDIV 0x01b0
  53. #define HW_UART7CLKDIV 0x01b4
  54. #define HW_UART8CLKDIV 0x01b8
  55. #define HW_UART9CLKDIV 0x01bc
  56. #define HW_SPI0CLKDIV 0x01c0
  57. #define HW_SPI1CLKDIV 0x01c4
  58. #define HW_QUADSPICLKDIV 0x01c8
  59. #define HW_SSP0CLKDIV 0x01d0
  60. #define HW_NANDCLKDIV 0x01d4
  61. #define HW_TRACECLKDIV 0x01e0
  62. #define HW_CAMMCLKDIV 0x01e8
  63. #define HW_WDTCLKDIV 0x01ec
  64. #define HW_CLKOUTCLKDIV 0x01f4
  65. #define HW_MACCLKDIV 0x01f8
  66. #define HW_LCDCLKDIV 0x01fc
  67. #define HW_ADCANACLKDIV 0x0200
  68. static struct clk_hw_onecell_data *clk_data;
  69. static DEFINE_SPINLOCK(asm9260_clk_lock);
  70. struct asm9260_div_clk {
  71. unsigned int idx;
  72. const char *name;
  73. const char *parent_name;
  74. u32 reg;
  75. };
  76. struct asm9260_gate_data {
  77. unsigned int idx;
  78. const char *name;
  79. const char *parent_name;
  80. u32 reg;
  81. u8 bit_idx;
  82. unsigned long flags;
  83. };
  84. struct asm9260_mux_clock {
  85. u8 mask;
  86. u32 *table;
  87. const char *name;
  88. const char **parent_names;
  89. u8 num_parents;
  90. unsigned long offset;
  91. unsigned long flags;
  92. };
  93. static void __iomem *base;
  94. static const struct asm9260_div_clk asm9260_div_clks[] __initconst = {
  95. { CLKID_SYS_CPU, "cpu_div", "main_gate", HW_CPUCLKDIV },
  96. { CLKID_SYS_AHB, "ahb_div", "cpu_div", HW_SYSAHBCLKDIV },
  97. /* i2s has two deviders: one for only external mclk and internal
  98. * devider for all clks. */
  99. { CLKID_SYS_I2S0M, "i2s0m_div", "i2s0_mclk", HW_I2S0MCLKDIV },
  100. { CLKID_SYS_I2S1M, "i2s1m_div", "i2s1_mclk", HW_I2S1MCLKDIV },
  101. { CLKID_SYS_I2S0S, "i2s0s_div", "i2s0_gate", HW_I2S0SCLKDIV },
  102. { CLKID_SYS_I2S1S, "i2s1s_div", "i2s0_gate", HW_I2S1SCLKDIV },
  103. { CLKID_SYS_UART0, "uart0_div", "uart_gate", HW_UART0CLKDIV },
  104. { CLKID_SYS_UART1, "uart1_div", "uart_gate", HW_UART1CLKDIV },
  105. { CLKID_SYS_UART2, "uart2_div", "uart_gate", HW_UART2CLKDIV },
  106. { CLKID_SYS_UART3, "uart3_div", "uart_gate", HW_UART3CLKDIV },
  107. { CLKID_SYS_UART4, "uart4_div", "uart_gate", HW_UART4CLKDIV },
  108. { CLKID_SYS_UART5, "uart5_div", "uart_gate", HW_UART5CLKDIV },
  109. { CLKID_SYS_UART6, "uart6_div", "uart_gate", HW_UART6CLKDIV },
  110. { CLKID_SYS_UART7, "uart7_div", "uart_gate", HW_UART7CLKDIV },
  111. { CLKID_SYS_UART8, "uart8_div", "uart_gate", HW_UART8CLKDIV },
  112. { CLKID_SYS_UART9, "uart9_div", "uart_gate", HW_UART9CLKDIV },
  113. { CLKID_SYS_SPI0, "spi0_div", "main_gate", HW_SPI0CLKDIV },
  114. { CLKID_SYS_SPI1, "spi1_div", "main_gate", HW_SPI1CLKDIV },
  115. { CLKID_SYS_QUADSPI, "quadspi_div", "main_gate", HW_QUADSPICLKDIV },
  116. { CLKID_SYS_SSP0, "ssp0_div", "main_gate", HW_SSP0CLKDIV },
  117. { CLKID_SYS_NAND, "nand_div", "main_gate", HW_NANDCLKDIV },
  118. { CLKID_SYS_TRACE, "trace_div", "main_gate", HW_TRACECLKDIV },
  119. { CLKID_SYS_CAMM, "camm_div", "main_gate", HW_CAMMCLKDIV },
  120. { CLKID_SYS_MAC, "mac_div", "main_gate", HW_MACCLKDIV },
  121. { CLKID_SYS_LCD, "lcd_div", "main_gate", HW_LCDCLKDIV },
  122. { CLKID_SYS_ADCANA, "adcana_div", "main_gate", HW_ADCANACLKDIV },
  123. { CLKID_SYS_WDT, "wdt_div", "wdt_gate", HW_WDTCLKDIV },
  124. { CLKID_SYS_CLKOUT, "clkout_div", "clkout_gate", HW_CLKOUTCLKDIV },
  125. };
  126. static const struct asm9260_gate_data asm9260_mux_gates[] __initconst = {
  127. { 0, "main_gate", "main_mux", HW_MAINCLKUEN, 0 },
  128. { 0, "uart_gate", "uart_mux", HW_UARTCLKUEN, 0 },
  129. { 0, "i2s0_gate", "i2s0_mux", HW_I2S0CLKUEN, 0 },
  130. { 0, "i2s1_gate", "i2s1_mux", HW_I2S1CLKUEN, 0 },
  131. { 0, "wdt_gate", "wdt_mux", HW_WDTCLKUEN, 0 },
  132. { 0, "clkout_gate", "clkout_mux", HW_CLKOUTCLKUEN, 0 },
  133. };
  134. static const struct asm9260_gate_data asm9260_ahb_gates[] __initconst = {
  135. /* ahb gates */
  136. { CLKID_AHB_ROM, "rom", "ahb_div",
  137. HW_AHBCLKCTRL0, 1, CLK_IGNORE_UNUSED},
  138. { CLKID_AHB_RAM, "ram", "ahb_div",
  139. HW_AHBCLKCTRL0, 2, CLK_IGNORE_UNUSED},
  140. { CLKID_AHB_GPIO, "gpio", "ahb_div",
  141. HW_AHBCLKCTRL0, 4 },
  142. { CLKID_AHB_MAC, "mac", "ahb_div",
  143. HW_AHBCLKCTRL0, 5 },
  144. { CLKID_AHB_EMI, "emi", "ahb_div",
  145. HW_AHBCLKCTRL0, 6, CLK_IGNORE_UNUSED},
  146. { CLKID_AHB_USB0, "usb0", "ahb_div",
  147. HW_AHBCLKCTRL0, 7 },
  148. { CLKID_AHB_USB1, "usb1", "ahb_div",
  149. HW_AHBCLKCTRL0, 8 },
  150. { CLKID_AHB_DMA0, "dma0", "ahb_div",
  151. HW_AHBCLKCTRL0, 9 },
  152. { CLKID_AHB_DMA1, "dma1", "ahb_div",
  153. HW_AHBCLKCTRL0, 10 },
  154. { CLKID_AHB_UART0, "uart0", "ahb_div",
  155. HW_AHBCLKCTRL0, 11 },
  156. { CLKID_AHB_UART1, "uart1", "ahb_div",
  157. HW_AHBCLKCTRL0, 12 },
  158. { CLKID_AHB_UART2, "uart2", "ahb_div",
  159. HW_AHBCLKCTRL0, 13 },
  160. { CLKID_AHB_UART3, "uart3", "ahb_div",
  161. HW_AHBCLKCTRL0, 14 },
  162. { CLKID_AHB_UART4, "uart4", "ahb_div",
  163. HW_AHBCLKCTRL0, 15 },
  164. { CLKID_AHB_UART5, "uart5", "ahb_div",
  165. HW_AHBCLKCTRL0, 16 },
  166. { CLKID_AHB_UART6, "uart6", "ahb_div",
  167. HW_AHBCLKCTRL0, 17 },
  168. { CLKID_AHB_UART7, "uart7", "ahb_div",
  169. HW_AHBCLKCTRL0, 18 },
  170. { CLKID_AHB_UART8, "uart8", "ahb_div",
  171. HW_AHBCLKCTRL0, 19 },
  172. { CLKID_AHB_UART9, "uart9", "ahb_div",
  173. HW_AHBCLKCTRL0, 20 },
  174. { CLKID_AHB_I2S0, "i2s0", "ahb_div",
  175. HW_AHBCLKCTRL0, 21 },
  176. { CLKID_AHB_I2C0, "i2c0", "ahb_div",
  177. HW_AHBCLKCTRL0, 22 },
  178. { CLKID_AHB_I2C1, "i2c1", "ahb_div",
  179. HW_AHBCLKCTRL0, 23 },
  180. { CLKID_AHB_SSP0, "ssp0", "ahb_div",
  181. HW_AHBCLKCTRL0, 24 },
  182. { CLKID_AHB_IOCONFIG, "ioconf", "ahb_div",
  183. HW_AHBCLKCTRL0, 25 },
  184. { CLKID_AHB_WDT, "wdt", "ahb_div",
  185. HW_AHBCLKCTRL0, 26 },
  186. { CLKID_AHB_CAN0, "can0", "ahb_div",
  187. HW_AHBCLKCTRL0, 27 },
  188. { CLKID_AHB_CAN1, "can1", "ahb_div",
  189. HW_AHBCLKCTRL0, 28 },
  190. { CLKID_AHB_MPWM, "mpwm", "ahb_div",
  191. HW_AHBCLKCTRL0, 29 },
  192. { CLKID_AHB_SPI0, "spi0", "ahb_div",
  193. HW_AHBCLKCTRL0, 30 },
  194. { CLKID_AHB_SPI1, "spi1", "ahb_div",
  195. HW_AHBCLKCTRL0, 31 },
  196. { CLKID_AHB_QEI, "qei", "ahb_div",
  197. HW_AHBCLKCTRL1, 0 },
  198. { CLKID_AHB_QUADSPI0, "quadspi0", "ahb_div",
  199. HW_AHBCLKCTRL1, 1 },
  200. { CLKID_AHB_CAMIF, "capmif", "ahb_div",
  201. HW_AHBCLKCTRL1, 2 },
  202. { CLKID_AHB_LCDIF, "lcdif", "ahb_div",
  203. HW_AHBCLKCTRL1, 3 },
  204. { CLKID_AHB_TIMER0, "timer0", "ahb_div",
  205. HW_AHBCLKCTRL1, 4 },
  206. { CLKID_AHB_TIMER1, "timer1", "ahb_div",
  207. HW_AHBCLKCTRL1, 5 },
  208. { CLKID_AHB_TIMER2, "timer2", "ahb_div",
  209. HW_AHBCLKCTRL1, 6 },
  210. { CLKID_AHB_TIMER3, "timer3", "ahb_div",
  211. HW_AHBCLKCTRL1, 7 },
  212. { CLKID_AHB_IRQ, "irq", "ahb_div",
  213. HW_AHBCLKCTRL1, 8, CLK_IGNORE_UNUSED},
  214. { CLKID_AHB_RTC, "rtc", "ahb_div",
  215. HW_AHBCLKCTRL1, 9 },
  216. { CLKID_AHB_NAND, "nand", "ahb_div",
  217. HW_AHBCLKCTRL1, 10 },
  218. { CLKID_AHB_ADC0, "adc0", "ahb_div",
  219. HW_AHBCLKCTRL1, 11 },
  220. { CLKID_AHB_LED, "led", "ahb_div",
  221. HW_AHBCLKCTRL1, 12 },
  222. { CLKID_AHB_DAC0, "dac0", "ahb_div",
  223. HW_AHBCLKCTRL1, 13 },
  224. { CLKID_AHB_LCD, "lcd", "ahb_div",
  225. HW_AHBCLKCTRL1, 14 },
  226. { CLKID_AHB_I2S1, "i2s1", "ahb_div",
  227. HW_AHBCLKCTRL1, 15 },
  228. { CLKID_AHB_MAC1, "mac1", "ahb_div",
  229. HW_AHBCLKCTRL1, 16 },
  230. };
  231. static const char __initdata *main_mux_p[] = { NULL, NULL };
  232. static const char __initdata *i2s0_mux_p[] = { NULL, NULL, "i2s0m_div"};
  233. static const char __initdata *i2s1_mux_p[] = { NULL, NULL, "i2s1m_div"};
  234. static const char __initdata *clkout_mux_p[] = { NULL, NULL, "rtc"};
  235. static u32 three_mux_table[] = {0, 1, 3};
  236. static struct asm9260_mux_clock asm9260_mux_clks[] __initdata = {
  237. { 1, three_mux_table, "main_mux", main_mux_p,
  238. ARRAY_SIZE(main_mux_p), HW_MAINCLKSEL, },
  239. { 1, three_mux_table, "uart_mux", main_mux_p,
  240. ARRAY_SIZE(main_mux_p), HW_UARTCLKSEL, },
  241. { 1, three_mux_table, "wdt_mux", main_mux_p,
  242. ARRAY_SIZE(main_mux_p), HW_WDTCLKSEL, },
  243. { 3, three_mux_table, "i2s0_mux", i2s0_mux_p,
  244. ARRAY_SIZE(i2s0_mux_p), HW_I2S0CLKSEL, },
  245. { 3, three_mux_table, "i2s1_mux", i2s1_mux_p,
  246. ARRAY_SIZE(i2s1_mux_p), HW_I2S1CLKSEL, },
  247. { 3, three_mux_table, "clkout_mux", clkout_mux_p,
  248. ARRAY_SIZE(clkout_mux_p), HW_CLKOUTCLKSEL, },
  249. };
  250. static void __init asm9260_acc_init(struct device_node *np)
  251. {
  252. struct clk_hw *hw;
  253. struct clk_hw **hws;
  254. const char *ref_clk, *pll_clk = "pll";
  255. u32 rate;
  256. int n;
  257. u32 accuracy = 0;
  258. clk_data = kzalloc(sizeof(*clk_data) +
  259. sizeof(*clk_data->hws) * MAX_CLKS, GFP_KERNEL);
  260. if (!clk_data)
  261. return;
  262. clk_data->num = MAX_CLKS;
  263. hws = clk_data->hws;
  264. base = of_io_request_and_map(np, 0, np->name);
  265. if (IS_ERR(base))
  266. panic("%s: unable to map resource", np->name);
  267. /* register pll */
  268. rate = (ioread32(base + HW_SYSPLLCTRL) & 0xffff) * 1000000;
  269. ref_clk = of_clk_get_parent_name(np, 0);
  270. accuracy = clk_get_accuracy(__clk_lookup(ref_clk));
  271. hw = clk_hw_register_fixed_rate_with_accuracy(NULL, pll_clk,
  272. ref_clk, 0, rate, accuracy);
  273. if (IS_ERR(hw))
  274. panic("%s: can't register REFCLK. Check DT!", np->name);
  275. for (n = 0; n < ARRAY_SIZE(asm9260_mux_clks); n++) {
  276. const struct asm9260_mux_clock *mc = &asm9260_mux_clks[n];
  277. mc->parent_names[0] = ref_clk;
  278. mc->parent_names[1] = pll_clk;
  279. hw = clk_hw_register_mux_table(NULL, mc->name, mc->parent_names,
  280. mc->num_parents, mc->flags, base + mc->offset,
  281. 0, mc->mask, 0, mc->table, &asm9260_clk_lock);
  282. }
  283. /* clock mux gate cells */
  284. for (n = 0; n < ARRAY_SIZE(asm9260_mux_gates); n++) {
  285. const struct asm9260_gate_data *gd = &asm9260_mux_gates[n];
  286. hw = clk_hw_register_gate(NULL, gd->name,
  287. gd->parent_name, gd->flags | CLK_SET_RATE_PARENT,
  288. base + gd->reg, gd->bit_idx, 0, &asm9260_clk_lock);
  289. }
  290. /* clock div cells */
  291. for (n = 0; n < ARRAY_SIZE(asm9260_div_clks); n++) {
  292. const struct asm9260_div_clk *dc = &asm9260_div_clks[n];
  293. hws[dc->idx] = clk_hw_register_divider(NULL, dc->name,
  294. dc->parent_name, CLK_SET_RATE_PARENT,
  295. base + dc->reg, 0, 8, CLK_DIVIDER_ONE_BASED,
  296. &asm9260_clk_lock);
  297. }
  298. /* clock ahb gate cells */
  299. for (n = 0; n < ARRAY_SIZE(asm9260_ahb_gates); n++) {
  300. const struct asm9260_gate_data *gd = &asm9260_ahb_gates[n];
  301. hws[gd->idx] = clk_hw_register_gate(NULL, gd->name,
  302. gd->parent_name, gd->flags, base + gd->reg,
  303. gd->bit_idx, 0, &asm9260_clk_lock);
  304. }
  305. /* check for errors on leaf clocks */
  306. for (n = 0; n < MAX_CLKS; n++) {
  307. if (!IS_ERR(hws[n]))
  308. continue;
  309. pr_err("%s: Unable to register leaf clock %d\n",
  310. np->full_name, n);
  311. goto fail;
  312. }
  313. /* register clk-provider */
  314. of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_data);
  315. return;
  316. fail:
  317. iounmap(base);
  318. }
  319. CLK_OF_DECLARE(asm9260_acc, "alphascale,asm9260-clock-controller",
  320. asm9260_acc_init);