18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * arch/sh/kernel/cpu/sh4a/clock-sh7785.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * SH7785 support for the clock framework
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  Copyright (C) 2007 - 2010  Paul Mundt
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci#include <linux/init.h>
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/clk.h>
128c2ecf20Sopenharmony_ci#include <linux/io.h>
138c2ecf20Sopenharmony_ci#include <linux/cpufreq.h>
148c2ecf20Sopenharmony_ci#include <linux/clkdev.h>
158c2ecf20Sopenharmony_ci#include <asm/clock.h>
168c2ecf20Sopenharmony_ci#include <asm/freq.h>
178c2ecf20Sopenharmony_ci#include <cpu/sh7785.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * Default rate for the root input clock, reset this with clk_set_rate()
218c2ecf20Sopenharmony_ci * from the platform code.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_cistatic struct clk extal_clk = {
248c2ecf20Sopenharmony_ci	.rate		= 33333333,
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic unsigned long pll_recalc(struct clk *clk)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	int multiplier;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	multiplier = test_mode_pin(MODE_PIN4) ? 36 : 72;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	return clk->parent->rate * multiplier;
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic struct sh_clk_ops pll_clk_ops = {
378c2ecf20Sopenharmony_ci	.recalc		= pll_recalc,
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic struct clk pll_clk = {
418c2ecf20Sopenharmony_ci	.ops		= &pll_clk_ops,
428c2ecf20Sopenharmony_ci	.parent		= &extal_clk,
438c2ecf20Sopenharmony_ci	.flags		= CLK_ENABLE_ON_INIT,
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic struct clk *clks[] = {
478c2ecf20Sopenharmony_ci	&extal_clk,
488c2ecf20Sopenharmony_ci	&pll_clk,
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic unsigned int div2[] = { 1, 2, 4, 6, 8, 12, 16, 18,
528c2ecf20Sopenharmony_ci			       24, 32, 36, 48 };
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic struct clk_div_mult_table div4_div_mult_table = {
558c2ecf20Sopenharmony_ci	.divisors = div2,
568c2ecf20Sopenharmony_ci	.nr_divisors = ARRAY_SIZE(div2),
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic struct clk_div4_table div4_table = {
608c2ecf20Sopenharmony_ci	.div_mult_table = &div4_div_mult_table,
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cienum { DIV4_I, DIV4_U, DIV4_SH, DIV4_B, DIV4_DDR, DIV4_GA,
648c2ecf20Sopenharmony_ci	DIV4_DU, DIV4_P, DIV4_NR };
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define DIV4(_bit, _mask, _flags) \
678c2ecf20Sopenharmony_ci  SH_CLK_DIV4(&pll_clk, FRQMR1, _bit, _mask, _flags)
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistruct clk div4_clks[DIV4_NR] = {
708c2ecf20Sopenharmony_ci	[DIV4_P] = DIV4(0, 0x0f80, 0),
718c2ecf20Sopenharmony_ci	[DIV4_DU] = DIV4(4, 0x0ff0, 0),
728c2ecf20Sopenharmony_ci	[DIV4_GA] = DIV4(8, 0x0030, 0),
738c2ecf20Sopenharmony_ci	[DIV4_DDR] = DIV4(12, 0x000c, CLK_ENABLE_ON_INIT),
748c2ecf20Sopenharmony_ci	[DIV4_B] = DIV4(16, 0x0fe0, CLK_ENABLE_ON_INIT),
758c2ecf20Sopenharmony_ci	[DIV4_SH] = DIV4(20, 0x000c, CLK_ENABLE_ON_INIT),
768c2ecf20Sopenharmony_ci	[DIV4_U] = DIV4(24, 0x000c, CLK_ENABLE_ON_INIT),
778c2ecf20Sopenharmony_ci	[DIV4_I] = DIV4(28, 0x000e, CLK_ENABLE_ON_INIT),
788c2ecf20Sopenharmony_ci};
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#define MSTPCR0		0xffc80030
818c2ecf20Sopenharmony_ci#define MSTPCR1		0xffc80034
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cienum { MSTP029, MSTP028, MSTP027, MSTP026, MSTP025, MSTP024,
848c2ecf20Sopenharmony_ci       MSTP021, MSTP020, MSTP017, MSTP016,
858c2ecf20Sopenharmony_ci       MSTP013, MSTP012, MSTP009, MSTP008, MSTP003, MSTP002,
868c2ecf20Sopenharmony_ci       MSTP119, MSTP117, MSTP105, MSTP104, MSTP100,
878c2ecf20Sopenharmony_ci       MSTP_NR };
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic struct clk mstp_clks[MSTP_NR] = {
908c2ecf20Sopenharmony_ci	/* MSTPCR0 */
918c2ecf20Sopenharmony_ci	[MSTP029] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 29, 0),
928c2ecf20Sopenharmony_ci	[MSTP028] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 28, 0),
938c2ecf20Sopenharmony_ci	[MSTP027] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 27, 0),
948c2ecf20Sopenharmony_ci	[MSTP026] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 26, 0),
958c2ecf20Sopenharmony_ci	[MSTP025] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 25, 0),
968c2ecf20Sopenharmony_ci	[MSTP024] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 24, 0),
978c2ecf20Sopenharmony_ci	[MSTP021] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 21, 0),
988c2ecf20Sopenharmony_ci	[MSTP020] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 20, 0),
998c2ecf20Sopenharmony_ci	[MSTP017] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 17, 0),
1008c2ecf20Sopenharmony_ci	[MSTP016] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 16, 0),
1018c2ecf20Sopenharmony_ci	[MSTP013] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 13, 0),
1028c2ecf20Sopenharmony_ci	[MSTP012] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 12, 0),
1038c2ecf20Sopenharmony_ci	[MSTP009] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 9, 0),
1048c2ecf20Sopenharmony_ci	[MSTP008] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 8, 0),
1058c2ecf20Sopenharmony_ci	[MSTP003] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 3, 0),
1068c2ecf20Sopenharmony_ci	[MSTP002] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 2, 0),
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	/* MSTPCR1 */
1098c2ecf20Sopenharmony_ci	[MSTP119] = SH_CLK_MSTP32(NULL, MSTPCR1, 19, 0),
1108c2ecf20Sopenharmony_ci	[MSTP117] = SH_CLK_MSTP32(NULL, MSTPCR1, 17, 0),
1118c2ecf20Sopenharmony_ci	[MSTP105] = SH_CLK_MSTP32(NULL, MSTPCR1, 5, 0),
1128c2ecf20Sopenharmony_ci	[MSTP104] = SH_CLK_MSTP32(NULL, MSTPCR1, 4, 0),
1138c2ecf20Sopenharmony_ci	[MSTP100] = SH_CLK_MSTP32(NULL, MSTPCR1, 0, 0),
1148c2ecf20Sopenharmony_ci};
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic struct clk_lookup lookups[] = {
1178c2ecf20Sopenharmony_ci	/* main clocks */
1188c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("extal", &extal_clk),
1198c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("pll_clk", &pll_clk),
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	/* DIV4 clocks */
1228c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("peripheral_clk", &div4_clks[DIV4_P]),
1238c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("du_clk", &div4_clks[DIV4_DU]),
1248c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("ga_clk", &div4_clks[DIV4_GA]),
1258c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("ddr_clk", &div4_clks[DIV4_DDR]),
1268c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("bus_clk", &div4_clks[DIV4_B]),
1278c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("shyway_clk", &div4_clks[DIV4_SH]),
1288c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("umem_clk", &div4_clks[DIV4_U]),
1298c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("cpu_clk", &div4_clks[DIV4_I]),
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	/* MSTP32 clocks */
1328c2ecf20Sopenharmony_ci	CLKDEV_ICK_ID("fck", "sh-sci.5", &mstp_clks[MSTP029]),
1338c2ecf20Sopenharmony_ci	CLKDEV_ICK_ID("fck", "sh-sci.4", &mstp_clks[MSTP028]),
1348c2ecf20Sopenharmony_ci	CLKDEV_ICK_ID("fck", "sh-sci.3", &mstp_clks[MSTP027]),
1358c2ecf20Sopenharmony_ci	CLKDEV_ICK_ID("fck", "sh-sci.2", &mstp_clks[MSTP026]),
1368c2ecf20Sopenharmony_ci	CLKDEV_ICK_ID("fck", "sh-sci.1", &mstp_clks[MSTP025]),
1378c2ecf20Sopenharmony_ci	CLKDEV_ICK_ID("fck", "sh-sci.0", &mstp_clks[MSTP024]),
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("ssi1_fck", &mstp_clks[MSTP021]),
1408c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("ssi0_fck", &mstp_clks[MSTP020]),
1418c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("hac1_fck", &mstp_clks[MSTP017]),
1428c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("hac0_fck", &mstp_clks[MSTP016]),
1438c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("mmcif_fck", &mstp_clks[MSTP013]),
1448c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("flctl_fck", &mstp_clks[MSTP012]),
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	CLKDEV_ICK_ID("fck", "sh-tmu.0", &mstp_clks[MSTP008]),
1478c2ecf20Sopenharmony_ci	CLKDEV_ICK_ID("fck", "sh-tmu.1", &mstp_clks[MSTP009]),
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("siof_fck", &mstp_clks[MSTP003]),
1508c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("hspi_fck", &mstp_clks[MSTP002]),
1518c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("hudi_fck", &mstp_clks[MSTP119]),
1528c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("ubc0", &mstp_clks[MSTP117]),
1538c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("dmac_11_6_fck", &mstp_clks[MSTP105]),
1548c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("dmac_5_0_fck", &mstp_clks[MSTP104]),
1558c2ecf20Sopenharmony_ci	CLKDEV_CON_ID("gdta_fck", &mstp_clks[MSTP100]),
1568c2ecf20Sopenharmony_ci};
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ciint __init arch_clk_init(void)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	int i, ret = 0;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(clks); i++)
1638c2ecf20Sopenharmony_ci		ret |= clk_register(clks[i]);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	clkdev_add_table(lookups, ARRAY_SIZE(lookups));
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	if (!ret)
1688c2ecf20Sopenharmony_ci		ret = sh_clk_div4_register(div4_clks, ARRAY_SIZE(div4_clks),
1698c2ecf20Sopenharmony_ci					   &div4_table);
1708c2ecf20Sopenharmony_ci	if (!ret)
1718c2ecf20Sopenharmony_ci		ret = sh_clk_mstp_register(mstp_clks, MSTP_NR);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	return ret;
1748c2ecf20Sopenharmony_ci}
175