clk-s3c2412.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (c) 2013 Heiko Stuebner <heiko@sntech.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Common Clock Framework support for S3C2412 and S3C2413.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/syscore_ops.h>
  16. #include <linux/reboot.h>
  17. #include <dt-bindings/clock/s3c2412.h>
  18. #include "clk.h"
  19. #include "clk-pll.h"
  20. #define LOCKTIME 0x00
  21. #define MPLLCON 0x04
  22. #define UPLLCON 0x08
  23. #define CLKCON 0x0c
  24. #define CLKDIVN 0x14
  25. #define CLKSRC 0x1c
  26. #define SWRST 0x30
  27. /* list of PLLs to be registered */
  28. enum s3c2412_plls {
  29. mpll, upll,
  30. };
  31. static void __iomem *reg_base;
  32. #ifdef CONFIG_PM_SLEEP
  33. static struct samsung_clk_reg_dump *s3c2412_save;
  34. /*
  35. * list of controller registers to be saved and restored during a
  36. * suspend/resume cycle.
  37. */
  38. static unsigned long s3c2412_clk_regs[] __initdata = {
  39. LOCKTIME,
  40. MPLLCON,
  41. UPLLCON,
  42. CLKCON,
  43. CLKDIVN,
  44. CLKSRC,
  45. };
  46. static int s3c2412_clk_suspend(void)
  47. {
  48. samsung_clk_save(reg_base, s3c2412_save,
  49. ARRAY_SIZE(s3c2412_clk_regs));
  50. return 0;
  51. }
  52. static void s3c2412_clk_resume(void)
  53. {
  54. samsung_clk_restore(reg_base, s3c2412_save,
  55. ARRAY_SIZE(s3c2412_clk_regs));
  56. }
  57. static struct syscore_ops s3c2412_clk_syscore_ops = {
  58. .suspend = s3c2412_clk_suspend,
  59. .resume = s3c2412_clk_resume,
  60. };
  61. static void s3c2412_clk_sleep_init(void)
  62. {
  63. s3c2412_save = samsung_clk_alloc_reg_dump(s3c2412_clk_regs,
  64. ARRAY_SIZE(s3c2412_clk_regs));
  65. if (!s3c2412_save) {
  66. pr_warn("%s: failed to allocate sleep save data, no sleep support!\n",
  67. __func__);
  68. return;
  69. }
  70. register_syscore_ops(&s3c2412_clk_syscore_ops);
  71. return;
  72. }
  73. #else
  74. static void s3c2412_clk_sleep_init(void) {}
  75. #endif
  76. static struct clk_div_table divxti_d[] = {
  77. { .val = 0, .div = 1 },
  78. { .val = 1, .div = 2 },
  79. { .val = 2, .div = 4 },
  80. { .val = 3, .div = 6 },
  81. { .val = 4, .div = 8 },
  82. { .val = 5, .div = 10 },
  83. { .val = 6, .div = 12 },
  84. { .val = 7, .div = 14 },
  85. { /* sentinel */ },
  86. };
  87. struct samsung_div_clock s3c2412_dividers[] __initdata = {
  88. DIV_T(0, "div_xti", "xti", CLKSRC, 0, 3, divxti_d),
  89. DIV(0, "div_cam", "mux_cam", CLKDIVN, 16, 4),
  90. DIV(0, "div_i2s", "mux_i2s", CLKDIVN, 12, 4),
  91. DIV(0, "div_uart", "mux_uart", CLKDIVN, 8, 4),
  92. DIV(0, "div_usb", "mux_usb", CLKDIVN, 6, 1),
  93. DIV(0, "div_hclk_half", "hclk", CLKDIVN, 5, 1),
  94. DIV(ARMDIV, "armdiv", "msysclk", CLKDIVN, 3, 1),
  95. DIV(PCLK, "pclk", "hclk", CLKDIVN, 2, 1),
  96. DIV(HCLK, "hclk", "armdiv", CLKDIVN, 0, 2),
  97. };
  98. struct samsung_fixed_factor_clock s3c2412_ffactor[] __initdata = {
  99. FFACTOR(0, "ff_hclk", "hclk", 2, 1, CLK_SET_RATE_PARENT),
  100. };
  101. /*
  102. * The first two use the OM[4] setting, which is not readable from
  103. * software, so assume it is set to xti.
  104. */
  105. PNAME(erefclk_p) = { "xti", "xti", "xti", "ext" };
  106. PNAME(urefclk_p) = { "xti", "xti", "xti", "ext" };
  107. PNAME(camclk_p) = { "usysclk", "hclk" };
  108. PNAME(usbclk_p) = { "usysclk", "hclk" };
  109. PNAME(i2sclk_p) = { "erefclk", "mpll" };
  110. PNAME(uartclk_p) = { "erefclk", "mpll" };
  111. PNAME(usysclk_p) = { "urefclk", "upll" };
  112. PNAME(msysclk_p) = { "mdivclk", "mpll" };
  113. PNAME(mdivclk_p) = { "xti", "div_xti" };
  114. PNAME(armclk_p) = { "armdiv", "hclk" };
  115. struct samsung_mux_clock s3c2412_muxes[] __initdata = {
  116. MUX(0, "erefclk", erefclk_p, CLKSRC, 14, 2),
  117. MUX(0, "urefclk", urefclk_p, CLKSRC, 12, 2),
  118. MUX(0, "mux_cam", camclk_p, CLKSRC, 11, 1),
  119. MUX(0, "mux_usb", usbclk_p, CLKSRC, 10, 1),
  120. MUX(0, "mux_i2s", i2sclk_p, CLKSRC, 9, 1),
  121. MUX(0, "mux_uart", uartclk_p, CLKSRC, 8, 1),
  122. MUX(USYSCLK, "usysclk", usysclk_p, CLKSRC, 5, 1),
  123. MUX(MSYSCLK, "msysclk", msysclk_p, CLKSRC, 4, 1),
  124. MUX(MDIVCLK, "mdivclk", mdivclk_p, CLKSRC, 3, 1),
  125. MUX(ARMCLK, "armclk", armclk_p, CLKDIVN, 4, 1),
  126. };
  127. static struct samsung_pll_clock s3c2412_plls[] __initdata = {
  128. [mpll] = PLL(pll_s3c2440_mpll, MPLL, "mpll", "xti",
  129. LOCKTIME, MPLLCON, NULL),
  130. [upll] = PLL(pll_s3c2410_upll, UPLL, "upll", "urefclk",
  131. LOCKTIME, UPLLCON, NULL),
  132. };
  133. struct samsung_gate_clock s3c2412_gates[] __initdata = {
  134. GATE(PCLK_WDT, "wdt", "pclk", CLKCON, 28, 0, 0),
  135. GATE(PCLK_SPI, "spi", "pclk", CLKCON, 27, 0, 0),
  136. GATE(PCLK_I2S, "i2s", "pclk", CLKCON, 26, 0, 0),
  137. GATE(PCLK_I2C, "i2c", "pclk", CLKCON, 25, 0, 0),
  138. GATE(PCLK_ADC, "adc", "pclk", CLKCON, 24, 0, 0),
  139. GATE(PCLK_RTC, "rtc", "pclk", CLKCON, 23, 0, 0),
  140. GATE(PCLK_GPIO, "gpio", "pclk", CLKCON, 22, CLK_IGNORE_UNUSED, 0),
  141. GATE(PCLK_UART2, "uart2", "pclk", CLKCON, 21, 0, 0),
  142. GATE(PCLK_UART1, "uart1", "pclk", CLKCON, 20, 0, 0),
  143. GATE(PCLK_UART0, "uart0", "pclk", CLKCON, 19, 0, 0),
  144. GATE(PCLK_SDI, "sdi", "pclk", CLKCON, 18, 0, 0),
  145. GATE(PCLK_PWM, "pwm", "pclk", CLKCON, 17, 0, 0),
  146. GATE(PCLK_USBD, "usb-device", "pclk", CLKCON, 16, 0, 0),
  147. GATE(SCLK_CAM, "sclk_cam", "div_cam", CLKCON, 15, 0, 0),
  148. GATE(SCLK_UART, "sclk_uart", "div_uart", CLKCON, 14, 0, 0),
  149. GATE(SCLK_I2S, "sclk_i2s", "div_i2s", CLKCON, 13, 0, 0),
  150. GATE(SCLK_USBH, "sclk_usbh", "div_usb", CLKCON, 12, 0, 0),
  151. GATE(SCLK_USBD, "sclk_usbd", "div_usb", CLKCON, 11, 0, 0),
  152. GATE(HCLK_HALF, "hclk_half", "div_hclk_half", CLKCON, 10, CLK_IGNORE_UNUSED, 0),
  153. GATE(HCLK_X2, "hclkx2", "ff_hclk", CLKCON, 9, CLK_IGNORE_UNUSED, 0),
  154. GATE(HCLK_SDRAM, "sdram", "hclk", CLKCON, 8, CLK_IGNORE_UNUSED, 0),
  155. GATE(HCLK_USBH, "usb-host", "hclk", CLKCON, 6, 0, 0),
  156. GATE(HCLK_LCD, "lcd", "hclk", CLKCON, 5, 0, 0),
  157. GATE(HCLK_NAND, "nand", "hclk", CLKCON, 4, 0, 0),
  158. GATE(HCLK_DMA3, "dma3", "hclk", CLKCON, 3, CLK_IGNORE_UNUSED, 0),
  159. GATE(HCLK_DMA2, "dma2", "hclk", CLKCON, 2, CLK_IGNORE_UNUSED, 0),
  160. GATE(HCLK_DMA1, "dma1", "hclk", CLKCON, 1, CLK_IGNORE_UNUSED, 0),
  161. GATE(HCLK_DMA0, "dma0", "hclk", CLKCON, 0, CLK_IGNORE_UNUSED, 0),
  162. };
  163. struct samsung_clock_alias s3c2412_aliases[] __initdata = {
  164. ALIAS(PCLK_UART0, "s3c2412-uart.0", "uart"),
  165. ALIAS(PCLK_UART1, "s3c2412-uart.1", "uart"),
  166. ALIAS(PCLK_UART2, "s3c2412-uart.2", "uart"),
  167. ALIAS(PCLK_UART0, "s3c2412-uart.0", "clk_uart_baud2"),
  168. ALIAS(PCLK_UART1, "s3c2412-uart.1", "clk_uart_baud2"),
  169. ALIAS(PCLK_UART2, "s3c2412-uart.2", "clk_uart_baud2"),
  170. ALIAS(SCLK_UART, NULL, "clk_uart_baud3"),
  171. ALIAS(PCLK_I2C, "s3c2410-i2c.0", "i2c"),
  172. ALIAS(PCLK_ADC, NULL, "adc"),
  173. ALIAS(PCLK_RTC, NULL, "rtc"),
  174. ALIAS(PCLK_PWM, NULL, "timers"),
  175. ALIAS(HCLK_LCD, NULL, "lcd"),
  176. ALIAS(PCLK_USBD, NULL, "usb-device"),
  177. ALIAS(SCLK_USBD, NULL, "usb-bus-gadget"),
  178. ALIAS(HCLK_USBH, NULL, "usb-host"),
  179. ALIAS(SCLK_USBH, NULL, "usb-bus-host"),
  180. ALIAS(ARMCLK, NULL, "armclk"),
  181. ALIAS(HCLK, NULL, "hclk"),
  182. ALIAS(MPLL, NULL, "mpll"),
  183. ALIAS(MSYSCLK, NULL, "fclk"),
  184. };
  185. static int s3c2412_restart(struct notifier_block *this,
  186. unsigned long mode, void *cmd)
  187. {
  188. /* errata "Watch-dog/Software Reset Problem" specifies that
  189. * this reset must be done with the SYSCLK sourced from
  190. * EXTCLK instead of FOUT to avoid a glitch in the reset
  191. * mechanism.
  192. *
  193. * See the watchdog section of the S3C2412 manual for more
  194. * information on this fix.
  195. */
  196. __raw_writel(0x00, reg_base + CLKSRC);
  197. __raw_writel(0x533C2412, reg_base + SWRST);
  198. return NOTIFY_DONE;
  199. }
  200. static struct notifier_block s3c2412_restart_handler = {
  201. .notifier_call = s3c2412_restart,
  202. .priority = 129,
  203. };
  204. /*
  205. * fixed rate clocks generated outside the soc
  206. * Only necessary until the devicetree-move is complete
  207. */
  208. #define XTI 1
  209. struct samsung_fixed_rate_clock s3c2412_common_frate_clks[] __initdata = {
  210. FRATE(XTI, "xti", NULL, CLK_IS_ROOT, 0),
  211. FRATE(0, "ext", NULL, CLK_IS_ROOT, 0),
  212. };
  213. static void __init s3c2412_common_clk_register_fixed_ext(
  214. struct samsung_clk_provider *ctx,
  215. unsigned long xti_f, unsigned long ext_f)
  216. {
  217. /* xtal alias is necessary for the current cpufreq driver */
  218. struct samsung_clock_alias xti_alias = ALIAS(XTI, NULL, "xtal");
  219. s3c2412_common_frate_clks[0].fixed_rate = xti_f;
  220. s3c2412_common_frate_clks[1].fixed_rate = ext_f;
  221. samsung_clk_register_fixed_rate(ctx, s3c2412_common_frate_clks,
  222. ARRAY_SIZE(s3c2412_common_frate_clks));
  223. samsung_clk_register_alias(ctx, &xti_alias, 1);
  224. }
  225. void __init s3c2412_common_clk_init(struct device_node *np, unsigned long xti_f,
  226. unsigned long ext_f, void __iomem *base)
  227. {
  228. struct samsung_clk_provider *ctx;
  229. int ret;
  230. reg_base = base;
  231. if (np) {
  232. reg_base = of_iomap(np, 0);
  233. if (!reg_base)
  234. panic("%s: failed to map registers\n", __func__);
  235. }
  236. ctx = samsung_clk_init(np, reg_base, NR_CLKS);
  237. if (!ctx)
  238. panic("%s: unable to allocate context.\n", __func__);
  239. /* Register external clocks only in non-dt cases */
  240. if (!np)
  241. s3c2412_common_clk_register_fixed_ext(ctx, xti_f, ext_f);
  242. /* Register PLLs. */
  243. samsung_clk_register_pll(ctx, s3c2412_plls, ARRAY_SIZE(s3c2412_plls),
  244. reg_base);
  245. /* Register common internal clocks. */
  246. samsung_clk_register_mux(ctx, s3c2412_muxes, ARRAY_SIZE(s3c2412_muxes));
  247. samsung_clk_register_div(ctx, s3c2412_dividers,
  248. ARRAY_SIZE(s3c2412_dividers));
  249. samsung_clk_register_gate(ctx, s3c2412_gates,
  250. ARRAY_SIZE(s3c2412_gates));
  251. samsung_clk_register_fixed_factor(ctx, s3c2412_ffactor,
  252. ARRAY_SIZE(s3c2412_ffactor));
  253. samsung_clk_register_alias(ctx, s3c2412_aliases,
  254. ARRAY_SIZE(s3c2412_aliases));
  255. s3c2412_clk_sleep_init();
  256. samsung_clk_of_add_provider(np, ctx);
  257. ret = register_restart_handler(&s3c2412_restart_handler);
  258. if (ret)
  259. pr_warn("cannot register restart handler, %d\n", ret);
  260. }
  261. static void __init s3c2412_clk_init(struct device_node *np)
  262. {
  263. s3c2412_common_clk_init(np, 0, 0, 0);
  264. }
  265. CLK_OF_DECLARE(s3c2412_clk, "samsung,s3c2412-clock", s3c2412_clk_init);