18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2013 Heiko Stuebner <heiko@sntech.de>
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Common Clock Framework support for S3C2412 and S3C2413.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
98c2ecf20Sopenharmony_ci#include <linux/clk/samsung.h>
108c2ecf20Sopenharmony_ci#include <linux/io.h>
118c2ecf20Sopenharmony_ci#include <linux/of.h>
128c2ecf20Sopenharmony_ci#include <linux/of_address.h>
138c2ecf20Sopenharmony_ci#include <linux/reboot.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <dt-bindings/clock/s3c2412.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "clk.h"
188c2ecf20Sopenharmony_ci#include "clk-pll.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define LOCKTIME	0x00
218c2ecf20Sopenharmony_ci#define MPLLCON		0x04
228c2ecf20Sopenharmony_ci#define UPLLCON		0x08
238c2ecf20Sopenharmony_ci#define CLKCON		0x0c
248c2ecf20Sopenharmony_ci#define CLKDIVN		0x14
258c2ecf20Sopenharmony_ci#define CLKSRC		0x1c
268c2ecf20Sopenharmony_ci#define SWRST		0x30
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic void __iomem *reg_base;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci * list of controller registers to be saved and restored during a
328c2ecf20Sopenharmony_ci * suspend/resume cycle.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_cistatic unsigned long s3c2412_clk_regs[] __initdata = {
358c2ecf20Sopenharmony_ci	LOCKTIME,
368c2ecf20Sopenharmony_ci	MPLLCON,
378c2ecf20Sopenharmony_ci	UPLLCON,
388c2ecf20Sopenharmony_ci	CLKCON,
398c2ecf20Sopenharmony_ci	CLKDIVN,
408c2ecf20Sopenharmony_ci	CLKSRC,
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic struct clk_div_table divxti_d[] = {
448c2ecf20Sopenharmony_ci	{ .val = 0, .div = 1 },
458c2ecf20Sopenharmony_ci	{ .val = 1, .div = 2 },
468c2ecf20Sopenharmony_ci	{ .val = 2, .div = 4 },
478c2ecf20Sopenharmony_ci	{ .val = 3, .div = 6 },
488c2ecf20Sopenharmony_ci	{ .val = 4, .div = 8 },
498c2ecf20Sopenharmony_ci	{ .val = 5, .div = 10 },
508c2ecf20Sopenharmony_ci	{ .val = 6, .div = 12 },
518c2ecf20Sopenharmony_ci	{ .val = 7, .div = 14 },
528c2ecf20Sopenharmony_ci	{ /* sentinel */ },
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic struct samsung_div_clock s3c2412_dividers[] __initdata = {
568c2ecf20Sopenharmony_ci	DIV_T(0, "div_xti", "xti", CLKSRC, 0, 3, divxti_d),
578c2ecf20Sopenharmony_ci	DIV(0, "div_cam", "mux_cam", CLKDIVN, 16, 4),
588c2ecf20Sopenharmony_ci	DIV(0, "div_i2s", "mux_i2s", CLKDIVN, 12, 4),
598c2ecf20Sopenharmony_ci	DIV(0, "div_uart", "mux_uart", CLKDIVN, 8, 4),
608c2ecf20Sopenharmony_ci	DIV(0, "div_usb", "mux_usb", CLKDIVN, 6, 1),
618c2ecf20Sopenharmony_ci	DIV(0, "div_hclk_half", "hclk", CLKDIVN, 5, 1),
628c2ecf20Sopenharmony_ci	DIV(ARMDIV, "armdiv", "msysclk", CLKDIVN, 3, 1),
638c2ecf20Sopenharmony_ci	DIV(PCLK, "pclk", "hclk", CLKDIVN, 2, 1),
648c2ecf20Sopenharmony_ci	DIV(HCLK, "hclk", "armdiv", CLKDIVN, 0, 2),
658c2ecf20Sopenharmony_ci};
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic struct samsung_fixed_factor_clock s3c2412_ffactor[] __initdata = {
688c2ecf20Sopenharmony_ci	FFACTOR(0, "ff_hclk", "hclk", 2, 1, CLK_SET_RATE_PARENT),
698c2ecf20Sopenharmony_ci};
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/*
728c2ecf20Sopenharmony_ci * The first two use the OM[4] setting, which is not readable from
738c2ecf20Sopenharmony_ci * software, so assume it is set to xti.
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_ciPNAME(erefclk_p) = { "xti", "xti", "xti", "ext" };
768c2ecf20Sopenharmony_ciPNAME(urefclk_p) = { "xti", "xti", "xti", "ext" };
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ciPNAME(camclk_p) = { "usysclk", "hclk" };
798c2ecf20Sopenharmony_ciPNAME(usbclk_p) = { "usysclk", "hclk" };
808c2ecf20Sopenharmony_ciPNAME(i2sclk_p) = { "erefclk", "mpll" };
818c2ecf20Sopenharmony_ciPNAME(uartclk_p) = { "erefclk", "mpll" };
828c2ecf20Sopenharmony_ciPNAME(usysclk_p) = { "urefclk", "upll" };
838c2ecf20Sopenharmony_ciPNAME(msysclk_p) = { "mdivclk", "mpll" };
848c2ecf20Sopenharmony_ciPNAME(mdivclk_p) = { "xti", "div_xti" };
858c2ecf20Sopenharmony_ciPNAME(armclk_p) = { "armdiv", "hclk" };
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic struct samsung_mux_clock s3c2412_muxes[] __initdata = {
888c2ecf20Sopenharmony_ci	MUX(0, "erefclk", erefclk_p, CLKSRC, 14, 2),
898c2ecf20Sopenharmony_ci	MUX(0, "urefclk", urefclk_p, CLKSRC, 12, 2),
908c2ecf20Sopenharmony_ci	MUX(0, "mux_cam", camclk_p, CLKSRC, 11, 1),
918c2ecf20Sopenharmony_ci	MUX(0, "mux_usb", usbclk_p, CLKSRC, 10, 1),
928c2ecf20Sopenharmony_ci	MUX(0, "mux_i2s", i2sclk_p, CLKSRC, 9, 1),
938c2ecf20Sopenharmony_ci	MUX(0, "mux_uart", uartclk_p, CLKSRC, 8, 1),
948c2ecf20Sopenharmony_ci	MUX(USYSCLK, "usysclk", usysclk_p, CLKSRC, 5, 1),
958c2ecf20Sopenharmony_ci	MUX(MSYSCLK, "msysclk", msysclk_p, CLKSRC, 4, 1),
968c2ecf20Sopenharmony_ci	MUX(MDIVCLK, "mdivclk", mdivclk_p, CLKSRC, 3, 1),
978c2ecf20Sopenharmony_ci	MUX(ARMCLK, "armclk", armclk_p, CLKDIVN, 4, 1),
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic struct samsung_pll_clock s3c2412_plls[] __initdata = {
1018c2ecf20Sopenharmony_ci	PLL(pll_s3c2440_mpll, MPLL, "mpll", "xti", LOCKTIME, MPLLCON, NULL),
1028c2ecf20Sopenharmony_ci	PLL(pll_s3c2410_upll, UPLL, "upll", "urefclk", LOCKTIME, UPLLCON, NULL),
1038c2ecf20Sopenharmony_ci};
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic struct samsung_gate_clock s3c2412_gates[] __initdata = {
1068c2ecf20Sopenharmony_ci	GATE(PCLK_WDT, "wdt", "pclk", CLKCON, 28, 0, 0),
1078c2ecf20Sopenharmony_ci	GATE(PCLK_SPI, "spi", "pclk", CLKCON, 27, 0, 0),
1088c2ecf20Sopenharmony_ci	GATE(PCLK_I2S, "i2s", "pclk", CLKCON, 26, 0, 0),
1098c2ecf20Sopenharmony_ci	GATE(PCLK_I2C, "i2c", "pclk", CLKCON, 25, 0, 0),
1108c2ecf20Sopenharmony_ci	GATE(PCLK_ADC, "adc", "pclk", CLKCON, 24, 0, 0),
1118c2ecf20Sopenharmony_ci	GATE(PCLK_RTC, "rtc", "pclk", CLKCON, 23, 0, 0),
1128c2ecf20Sopenharmony_ci	GATE(PCLK_GPIO, "gpio", "pclk", CLKCON, 22, CLK_IGNORE_UNUSED, 0),
1138c2ecf20Sopenharmony_ci	GATE(PCLK_UART2, "uart2", "pclk", CLKCON, 21, 0, 0),
1148c2ecf20Sopenharmony_ci	GATE(PCLK_UART1, "uart1", "pclk", CLKCON, 20, 0, 0),
1158c2ecf20Sopenharmony_ci	GATE(PCLK_UART0, "uart0", "pclk", CLKCON, 19, 0, 0),
1168c2ecf20Sopenharmony_ci	GATE(PCLK_SDI, "sdi", "pclk", CLKCON, 18, 0, 0),
1178c2ecf20Sopenharmony_ci	GATE(PCLK_PWM, "pwm", "pclk", CLKCON, 17, 0, 0),
1188c2ecf20Sopenharmony_ci	GATE(PCLK_USBD, "usb-device", "pclk", CLKCON, 16, 0, 0),
1198c2ecf20Sopenharmony_ci	GATE(SCLK_CAM, "sclk_cam", "div_cam", CLKCON, 15, 0, 0),
1208c2ecf20Sopenharmony_ci	GATE(SCLK_UART, "sclk_uart", "div_uart", CLKCON, 14, 0, 0),
1218c2ecf20Sopenharmony_ci	GATE(SCLK_I2S, "sclk_i2s", "div_i2s", CLKCON, 13, 0, 0),
1228c2ecf20Sopenharmony_ci	GATE(SCLK_USBH, "sclk_usbh", "div_usb", CLKCON, 12, 0, 0),
1238c2ecf20Sopenharmony_ci	GATE(SCLK_USBD, "sclk_usbd", "div_usb", CLKCON, 11, 0, 0),
1248c2ecf20Sopenharmony_ci	GATE(HCLK_HALF, "hclk_half", "div_hclk_half", CLKCON, 10, CLK_IGNORE_UNUSED, 0),
1258c2ecf20Sopenharmony_ci	GATE(HCLK_X2, "hclkx2", "ff_hclk", CLKCON, 9, CLK_IGNORE_UNUSED, 0),
1268c2ecf20Sopenharmony_ci	GATE(HCLK_SDRAM, "sdram", "hclk", CLKCON, 8, CLK_IGNORE_UNUSED, 0),
1278c2ecf20Sopenharmony_ci	GATE(HCLK_USBH, "usb-host", "hclk", CLKCON, 6, 0, 0),
1288c2ecf20Sopenharmony_ci	GATE(HCLK_LCD, "lcd", "hclk", CLKCON, 5, 0, 0),
1298c2ecf20Sopenharmony_ci	GATE(HCLK_NAND, "nand", "hclk", CLKCON, 4, 0, 0),
1308c2ecf20Sopenharmony_ci	GATE(HCLK_DMA3, "dma3", "hclk", CLKCON, 3, CLK_IGNORE_UNUSED, 0),
1318c2ecf20Sopenharmony_ci	GATE(HCLK_DMA2, "dma2", "hclk", CLKCON, 2, CLK_IGNORE_UNUSED, 0),
1328c2ecf20Sopenharmony_ci	GATE(HCLK_DMA1, "dma1", "hclk", CLKCON, 1, CLK_IGNORE_UNUSED, 0),
1338c2ecf20Sopenharmony_ci	GATE(HCLK_DMA0, "dma0", "hclk", CLKCON, 0, CLK_IGNORE_UNUSED, 0),
1348c2ecf20Sopenharmony_ci};
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic struct samsung_clock_alias s3c2412_aliases[] __initdata = {
1378c2ecf20Sopenharmony_ci	ALIAS(PCLK_UART0, "s3c2412-uart.0", "uart"),
1388c2ecf20Sopenharmony_ci	ALIAS(PCLK_UART1, "s3c2412-uart.1", "uart"),
1398c2ecf20Sopenharmony_ci	ALIAS(PCLK_UART2, "s3c2412-uart.2", "uart"),
1408c2ecf20Sopenharmony_ci	ALIAS(PCLK_UART0, "s3c2412-uart.0", "clk_uart_baud2"),
1418c2ecf20Sopenharmony_ci	ALIAS(PCLK_UART1, "s3c2412-uart.1", "clk_uart_baud2"),
1428c2ecf20Sopenharmony_ci	ALIAS(PCLK_UART2, "s3c2412-uart.2", "clk_uart_baud2"),
1438c2ecf20Sopenharmony_ci	ALIAS(SCLK_UART, NULL, "clk_uart_baud3"),
1448c2ecf20Sopenharmony_ci	ALIAS(PCLK_I2C, "s3c2410-i2c.0", "i2c"),
1458c2ecf20Sopenharmony_ci	ALIAS(PCLK_ADC, NULL, "adc"),
1468c2ecf20Sopenharmony_ci	ALIAS(PCLK_RTC, NULL, "rtc"),
1478c2ecf20Sopenharmony_ci	ALIAS(PCLK_PWM, NULL, "timers"),
1488c2ecf20Sopenharmony_ci	ALIAS(HCLK_LCD, NULL, "lcd"),
1498c2ecf20Sopenharmony_ci	ALIAS(PCLK_USBD, NULL, "usb-device"),
1508c2ecf20Sopenharmony_ci	ALIAS(SCLK_USBD, NULL, "usb-bus-gadget"),
1518c2ecf20Sopenharmony_ci	ALIAS(HCLK_USBH, NULL, "usb-host"),
1528c2ecf20Sopenharmony_ci	ALIAS(SCLK_USBH, NULL, "usb-bus-host"),
1538c2ecf20Sopenharmony_ci	ALIAS(ARMCLK, NULL, "armclk"),
1548c2ecf20Sopenharmony_ci	ALIAS(HCLK, NULL, "hclk"),
1558c2ecf20Sopenharmony_ci	ALIAS(MPLL, NULL, "mpll"),
1568c2ecf20Sopenharmony_ci	ALIAS(MSYSCLK, NULL, "fclk"),
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic int s3c2412_restart(struct notifier_block *this,
1608c2ecf20Sopenharmony_ci			   unsigned long mode, void *cmd)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	/* errata "Watch-dog/Software Reset Problem" specifies that
1638c2ecf20Sopenharmony_ci	 * this reset must be done with the SYSCLK sourced from
1648c2ecf20Sopenharmony_ci	 * EXTCLK instead of FOUT to avoid a glitch in the reset
1658c2ecf20Sopenharmony_ci	 * mechanism.
1668c2ecf20Sopenharmony_ci	 *
1678c2ecf20Sopenharmony_ci	 * See the watchdog section of the S3C2412 manual for more
1688c2ecf20Sopenharmony_ci	 * information on this fix.
1698c2ecf20Sopenharmony_ci	 */
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	__raw_writel(0x00, reg_base + CLKSRC);
1728c2ecf20Sopenharmony_ci	__raw_writel(0x533C2412, reg_base + SWRST);
1738c2ecf20Sopenharmony_ci	return NOTIFY_DONE;
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic struct notifier_block s3c2412_restart_handler = {
1778c2ecf20Sopenharmony_ci	.notifier_call = s3c2412_restart,
1788c2ecf20Sopenharmony_ci	.priority = 129,
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci/*
1828c2ecf20Sopenharmony_ci * fixed rate clocks generated outside the soc
1838c2ecf20Sopenharmony_ci * Only necessary until the devicetree-move is complete
1848c2ecf20Sopenharmony_ci */
1858c2ecf20Sopenharmony_ci#define XTI	1
1868c2ecf20Sopenharmony_cistatic struct samsung_fixed_rate_clock s3c2412_common_frate_clks[] __initdata = {
1878c2ecf20Sopenharmony_ci	FRATE(XTI, "xti", NULL, 0, 0),
1888c2ecf20Sopenharmony_ci	FRATE(0, "ext", NULL, 0, 0),
1898c2ecf20Sopenharmony_ci};
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic void __init s3c2412_common_clk_register_fixed_ext(
1928c2ecf20Sopenharmony_ci		struct samsung_clk_provider *ctx,
1938c2ecf20Sopenharmony_ci		unsigned long xti_f, unsigned long ext_f)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	/* xtal alias is necessary for the current cpufreq driver */
1968c2ecf20Sopenharmony_ci	struct samsung_clock_alias xti_alias = ALIAS(XTI, NULL, "xtal");
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	s3c2412_common_frate_clks[0].fixed_rate = xti_f;
1998c2ecf20Sopenharmony_ci	s3c2412_common_frate_clks[1].fixed_rate = ext_f;
2008c2ecf20Sopenharmony_ci	samsung_clk_register_fixed_rate(ctx, s3c2412_common_frate_clks,
2018c2ecf20Sopenharmony_ci				ARRAY_SIZE(s3c2412_common_frate_clks));
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	samsung_clk_register_alias(ctx, &xti_alias, 1);
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_civoid __init s3c2412_common_clk_init(struct device_node *np, unsigned long xti_f,
2078c2ecf20Sopenharmony_ci				    unsigned long ext_f, void __iomem *base)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	struct samsung_clk_provider *ctx;
2108c2ecf20Sopenharmony_ci	int ret;
2118c2ecf20Sopenharmony_ci	reg_base = base;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	if (np) {
2148c2ecf20Sopenharmony_ci		reg_base = of_iomap(np, 0);
2158c2ecf20Sopenharmony_ci		if (!reg_base)
2168c2ecf20Sopenharmony_ci			panic("%s: failed to map registers\n", __func__);
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	ctx = samsung_clk_init(np, reg_base, NR_CLKS);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	/* Register external clocks only in non-dt cases */
2228c2ecf20Sopenharmony_ci	if (!np)
2238c2ecf20Sopenharmony_ci		s3c2412_common_clk_register_fixed_ext(ctx, xti_f, ext_f);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/* Register PLLs. */
2268c2ecf20Sopenharmony_ci	samsung_clk_register_pll(ctx, s3c2412_plls, ARRAY_SIZE(s3c2412_plls),
2278c2ecf20Sopenharmony_ci				 reg_base);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	/* Register common internal clocks. */
2308c2ecf20Sopenharmony_ci	samsung_clk_register_mux(ctx, s3c2412_muxes, ARRAY_SIZE(s3c2412_muxes));
2318c2ecf20Sopenharmony_ci	samsung_clk_register_div(ctx, s3c2412_dividers,
2328c2ecf20Sopenharmony_ci					  ARRAY_SIZE(s3c2412_dividers));
2338c2ecf20Sopenharmony_ci	samsung_clk_register_gate(ctx, s3c2412_gates,
2348c2ecf20Sopenharmony_ci					ARRAY_SIZE(s3c2412_gates));
2358c2ecf20Sopenharmony_ci	samsung_clk_register_fixed_factor(ctx, s3c2412_ffactor,
2368c2ecf20Sopenharmony_ci					  ARRAY_SIZE(s3c2412_ffactor));
2378c2ecf20Sopenharmony_ci	samsung_clk_register_alias(ctx, s3c2412_aliases,
2388c2ecf20Sopenharmony_ci				   ARRAY_SIZE(s3c2412_aliases));
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	samsung_clk_sleep_init(reg_base, s3c2412_clk_regs,
2418c2ecf20Sopenharmony_ci			       ARRAY_SIZE(s3c2412_clk_regs));
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	samsung_clk_of_add_provider(np, ctx);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	ret = register_restart_handler(&s3c2412_restart_handler);
2468c2ecf20Sopenharmony_ci	if (ret)
2478c2ecf20Sopenharmony_ci		pr_warn("cannot register restart handler, %d\n", ret);
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cistatic void __init s3c2412_clk_init(struct device_node *np)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	s3c2412_common_clk_init(np, 0, 0, NULL);
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ciCLK_OF_DECLARE(s3c2412_clk, "samsung,s3c2412-clock", s3c2412_clk_init);
255