18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Ingenic JZ4740 SoC CGU driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2015 Imagination Technologies
68c2ecf20Sopenharmony_ci * Author: Paul Burton <paul.burton@mips.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci#include <linux/of.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <dt-bindings/clock/jz4740-cgu.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "cgu.h"
178c2ecf20Sopenharmony_ci#include "pm.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* CGU register offsets */
208c2ecf20Sopenharmony_ci#define CGU_REG_CPCCR		0x00
218c2ecf20Sopenharmony_ci#define CGU_REG_LCR		0x04
228c2ecf20Sopenharmony_ci#define CGU_REG_CPPCR		0x10
238c2ecf20Sopenharmony_ci#define CGU_REG_CLKGR		0x20
248c2ecf20Sopenharmony_ci#define CGU_REG_SCR		0x24
258c2ecf20Sopenharmony_ci#define CGU_REG_I2SCDR		0x60
268c2ecf20Sopenharmony_ci#define CGU_REG_LPCDR		0x64
278c2ecf20Sopenharmony_ci#define CGU_REG_MSCCDR		0x68
288c2ecf20Sopenharmony_ci#define CGU_REG_UHCCDR		0x6c
298c2ecf20Sopenharmony_ci#define CGU_REG_SSICDR		0x74
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* bits within a PLL control register */
328c2ecf20Sopenharmony_ci#define PLLCTL_M_SHIFT		23
338c2ecf20Sopenharmony_ci#define PLLCTL_M_MASK		(0x1ff << PLLCTL_M_SHIFT)
348c2ecf20Sopenharmony_ci#define PLLCTL_N_SHIFT		18
358c2ecf20Sopenharmony_ci#define PLLCTL_N_MASK		(0x1f << PLLCTL_N_SHIFT)
368c2ecf20Sopenharmony_ci#define PLLCTL_OD_SHIFT		16
378c2ecf20Sopenharmony_ci#define PLLCTL_OD_MASK		(0x3 << PLLCTL_OD_SHIFT)
388c2ecf20Sopenharmony_ci#define PLLCTL_STABLE		(1 << 10)
398c2ecf20Sopenharmony_ci#define PLLCTL_BYPASS		(1 << 9)
408c2ecf20Sopenharmony_ci#define PLLCTL_ENABLE		(1 << 8)
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* bits within the LCR register */
438c2ecf20Sopenharmony_ci#define LCR_SLEEP		(1 << 0)
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/* bits within the CLKGR register */
468c2ecf20Sopenharmony_ci#define CLKGR_UDC		(1 << 11)
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic struct ingenic_cgu *cgu;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic const s8 pll_od_encoding[4] = {
518c2ecf20Sopenharmony_ci	0x0, 0x1, -1, 0x3,
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic const u8 jz4740_cgu_cpccr_div_table[] = {
558c2ecf20Sopenharmony_ci	1, 2, 3, 4, 6, 8, 12, 16, 24, 32,
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic const u8 jz4740_cgu_pll_half_div_table[] = {
598c2ecf20Sopenharmony_ci	2, 1,
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic const struct ingenic_cgu_clk_info jz4740_cgu_clocks[] = {
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/* External clocks */
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	[JZ4740_CLK_EXT] = { "ext", CGU_CLK_EXT },
678c2ecf20Sopenharmony_ci	[JZ4740_CLK_RTC] = { "rtc", CGU_CLK_EXT },
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	[JZ4740_CLK_PLL] = {
708c2ecf20Sopenharmony_ci		"pll", CGU_CLK_PLL,
718c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
728c2ecf20Sopenharmony_ci		.pll = {
738c2ecf20Sopenharmony_ci			.reg = CGU_REG_CPPCR,
748c2ecf20Sopenharmony_ci			.rate_multiplier = 1,
758c2ecf20Sopenharmony_ci			.m_shift = 23,
768c2ecf20Sopenharmony_ci			.m_bits = 9,
778c2ecf20Sopenharmony_ci			.m_offset = 2,
788c2ecf20Sopenharmony_ci			.n_shift = 18,
798c2ecf20Sopenharmony_ci			.n_bits = 5,
808c2ecf20Sopenharmony_ci			.n_offset = 2,
818c2ecf20Sopenharmony_ci			.od_shift = 16,
828c2ecf20Sopenharmony_ci			.od_bits = 2,
838c2ecf20Sopenharmony_ci			.od_max = 4,
848c2ecf20Sopenharmony_ci			.od_encoding = pll_od_encoding,
858c2ecf20Sopenharmony_ci			.stable_bit = 10,
868c2ecf20Sopenharmony_ci			.bypass_reg = CGU_REG_CPPCR,
878c2ecf20Sopenharmony_ci			.bypass_bit = 9,
888c2ecf20Sopenharmony_ci			.enable_bit = 8,
898c2ecf20Sopenharmony_ci		},
908c2ecf20Sopenharmony_ci	},
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	/* Muxes & dividers */
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	[JZ4740_CLK_PLL_HALF] = {
958c2ecf20Sopenharmony_ci		"pll half", CGU_CLK_DIV,
968c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
978c2ecf20Sopenharmony_ci		.div = {
988c2ecf20Sopenharmony_ci			CGU_REG_CPCCR, 21, 1, 1, -1, -1, -1,
998c2ecf20Sopenharmony_ci			jz4740_cgu_pll_half_div_table,
1008c2ecf20Sopenharmony_ci		},
1018c2ecf20Sopenharmony_ci	},
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	[JZ4740_CLK_CCLK] = {
1048c2ecf20Sopenharmony_ci		"cclk", CGU_CLK_DIV,
1058c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
1068c2ecf20Sopenharmony_ci		.div = {
1078c2ecf20Sopenharmony_ci			CGU_REG_CPCCR, 0, 1, 4, 22, -1, -1,
1088c2ecf20Sopenharmony_ci			jz4740_cgu_cpccr_div_table,
1098c2ecf20Sopenharmony_ci		},
1108c2ecf20Sopenharmony_ci	},
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	[JZ4740_CLK_HCLK] = {
1138c2ecf20Sopenharmony_ci		"hclk", CGU_CLK_DIV,
1148c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
1158c2ecf20Sopenharmony_ci		.div = {
1168c2ecf20Sopenharmony_ci			CGU_REG_CPCCR, 4, 1, 4, 22, -1, -1,
1178c2ecf20Sopenharmony_ci			jz4740_cgu_cpccr_div_table,
1188c2ecf20Sopenharmony_ci		},
1198c2ecf20Sopenharmony_ci	},
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	[JZ4740_CLK_PCLK] = {
1228c2ecf20Sopenharmony_ci		"pclk", CGU_CLK_DIV,
1238c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
1248c2ecf20Sopenharmony_ci		.div = {
1258c2ecf20Sopenharmony_ci			CGU_REG_CPCCR, 8, 1, 4, 22, -1, -1,
1268c2ecf20Sopenharmony_ci			jz4740_cgu_cpccr_div_table,
1278c2ecf20Sopenharmony_ci		},
1288c2ecf20Sopenharmony_ci	},
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	[JZ4740_CLK_MCLK] = {
1318c2ecf20Sopenharmony_ci		"mclk", CGU_CLK_DIV,
1328c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
1338c2ecf20Sopenharmony_ci		.div = {
1348c2ecf20Sopenharmony_ci			CGU_REG_CPCCR, 12, 1, 4, 22, -1, -1,
1358c2ecf20Sopenharmony_ci			jz4740_cgu_cpccr_div_table,
1368c2ecf20Sopenharmony_ci		},
1378c2ecf20Sopenharmony_ci	},
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	[JZ4740_CLK_LCD] = {
1408c2ecf20Sopenharmony_ci		"lcd", CGU_CLK_DIV | CGU_CLK_GATE,
1418c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
1428c2ecf20Sopenharmony_ci		.div = {
1438c2ecf20Sopenharmony_ci			CGU_REG_CPCCR, 16, 1, 5, 22, -1, -1,
1448c2ecf20Sopenharmony_ci			jz4740_cgu_cpccr_div_table,
1458c2ecf20Sopenharmony_ci		},
1468c2ecf20Sopenharmony_ci		.gate = { CGU_REG_CLKGR, 10 },
1478c2ecf20Sopenharmony_ci	},
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	[JZ4740_CLK_LCD_PCLK] = {
1508c2ecf20Sopenharmony_ci		"lcd_pclk", CGU_CLK_DIV,
1518c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
1528c2ecf20Sopenharmony_ci		.div = { CGU_REG_LPCDR, 0, 1, 11, -1, -1, -1 },
1538c2ecf20Sopenharmony_ci	},
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	[JZ4740_CLK_I2S] = {
1568c2ecf20Sopenharmony_ci		"i2s", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE,
1578c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 },
1588c2ecf20Sopenharmony_ci		.mux = { CGU_REG_CPCCR, 31, 1 },
1598c2ecf20Sopenharmony_ci		.div = { CGU_REG_I2SCDR, 0, 1, 9, -1, -1, -1 },
1608c2ecf20Sopenharmony_ci		.gate = { CGU_REG_CLKGR, 6 },
1618c2ecf20Sopenharmony_ci	},
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	[JZ4740_CLK_SPI] = {
1648c2ecf20Sopenharmony_ci		"spi", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE,
1658c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL, -1, -1 },
1668c2ecf20Sopenharmony_ci		.mux = { CGU_REG_SSICDR, 31, 1 },
1678c2ecf20Sopenharmony_ci		.div = { CGU_REG_SSICDR, 0, 1, 4, -1, -1, -1 },
1688c2ecf20Sopenharmony_ci		.gate = { CGU_REG_CLKGR, 4 },
1698c2ecf20Sopenharmony_ci	},
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	[JZ4740_CLK_MMC] = {
1728c2ecf20Sopenharmony_ci		"mmc", CGU_CLK_DIV | CGU_CLK_GATE,
1738c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
1748c2ecf20Sopenharmony_ci		.div = { CGU_REG_MSCCDR, 0, 1, 5, -1, -1, -1 },
1758c2ecf20Sopenharmony_ci		.gate = { CGU_REG_CLKGR, 7 },
1768c2ecf20Sopenharmony_ci	},
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	[JZ4740_CLK_UHC] = {
1798c2ecf20Sopenharmony_ci		"uhc", CGU_CLK_DIV | CGU_CLK_GATE,
1808c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
1818c2ecf20Sopenharmony_ci		.div = { CGU_REG_UHCCDR, 0, 1, 4, -1, -1, -1 },
1828c2ecf20Sopenharmony_ci		.gate = { CGU_REG_CLKGR, 14 },
1838c2ecf20Sopenharmony_ci	},
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	[JZ4740_CLK_UDC] = {
1868c2ecf20Sopenharmony_ci		"udc", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE,
1878c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 },
1888c2ecf20Sopenharmony_ci		.mux = { CGU_REG_CPCCR, 29, 1 },
1898c2ecf20Sopenharmony_ci		.div = { CGU_REG_CPCCR, 23, 1, 6, -1, -1, -1 },
1908c2ecf20Sopenharmony_ci		.gate = { CGU_REG_SCR, 6, true },
1918c2ecf20Sopenharmony_ci	},
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	/* Gate-only clocks */
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	[JZ4740_CLK_UART0] = {
1968c2ecf20Sopenharmony_ci		"uart0", CGU_CLK_GATE,
1978c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
1988c2ecf20Sopenharmony_ci		.gate = { CGU_REG_CLKGR, 0 },
1998c2ecf20Sopenharmony_ci	},
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	[JZ4740_CLK_UART1] = {
2028c2ecf20Sopenharmony_ci		"uart1", CGU_CLK_GATE,
2038c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
2048c2ecf20Sopenharmony_ci		.gate = { CGU_REG_CLKGR, 15 },
2058c2ecf20Sopenharmony_ci	},
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	[JZ4740_CLK_DMA] = {
2088c2ecf20Sopenharmony_ci		"dma", CGU_CLK_GATE,
2098c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_PCLK, -1, -1, -1 },
2108c2ecf20Sopenharmony_ci		.gate = { CGU_REG_CLKGR, 12 },
2118c2ecf20Sopenharmony_ci	},
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	[JZ4740_CLK_IPU] = {
2148c2ecf20Sopenharmony_ci		"ipu", CGU_CLK_GATE,
2158c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_PCLK, -1, -1, -1 },
2168c2ecf20Sopenharmony_ci		.gate = { CGU_REG_CLKGR, 13 },
2178c2ecf20Sopenharmony_ci	},
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	[JZ4740_CLK_ADC] = {
2208c2ecf20Sopenharmony_ci		"adc", CGU_CLK_GATE,
2218c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
2228c2ecf20Sopenharmony_ci		.gate = { CGU_REG_CLKGR, 8 },
2238c2ecf20Sopenharmony_ci	},
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	[JZ4740_CLK_I2C] = {
2268c2ecf20Sopenharmony_ci		"i2c", CGU_CLK_GATE,
2278c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
2288c2ecf20Sopenharmony_ci		.gate = { CGU_REG_CLKGR, 3 },
2298c2ecf20Sopenharmony_ci	},
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	[JZ4740_CLK_AIC] = {
2328c2ecf20Sopenharmony_ci		"aic", CGU_CLK_GATE,
2338c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
2348c2ecf20Sopenharmony_ci		.gate = { CGU_REG_CLKGR, 5 },
2358c2ecf20Sopenharmony_ci	},
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	[JZ4740_CLK_TCU] = {
2388c2ecf20Sopenharmony_ci		"tcu", CGU_CLK_GATE,
2398c2ecf20Sopenharmony_ci		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
2408c2ecf20Sopenharmony_ci		.gate = { CGU_REG_CLKGR, 1 },
2418c2ecf20Sopenharmony_ci	},
2428c2ecf20Sopenharmony_ci};
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic void __init jz4740_cgu_init(struct device_node *np)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	int retval;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	cgu = ingenic_cgu_new(jz4740_cgu_clocks,
2498c2ecf20Sopenharmony_ci			      ARRAY_SIZE(jz4740_cgu_clocks), np);
2508c2ecf20Sopenharmony_ci	if (!cgu) {
2518c2ecf20Sopenharmony_ci		pr_err("%s: failed to initialise CGU\n", __func__);
2528c2ecf20Sopenharmony_ci		return;
2538c2ecf20Sopenharmony_ci	}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	retval = ingenic_cgu_register_clocks(cgu);
2568c2ecf20Sopenharmony_ci	if (retval)
2578c2ecf20Sopenharmony_ci		pr_err("%s: failed to register CGU Clocks\n", __func__);
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	ingenic_cgu_register_syscore_ops(cgu);
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ciCLK_OF_DECLARE_DRIVER(jz4740_cgu, "ingenic,jz4740-cgu", jz4740_cgu_init);
262