162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * arch/sh/kernel/cpu/sh4/clock-sh7757.c
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * SH7757 support for the clock framework
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci *  Copyright (C) 2009-2010  Renesas Solutions Corp.
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci#include <linux/init.h>
1062306a36Sopenharmony_ci#include <linux/kernel.h>
1162306a36Sopenharmony_ci#include <linux/io.h>
1262306a36Sopenharmony_ci#include <linux/clkdev.h>
1362306a36Sopenharmony_ci#include <asm/clock.h>
1462306a36Sopenharmony_ci#include <asm/freq.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/*
1762306a36Sopenharmony_ci * Default rate for the root input clock, reset this with clk_set_rate()
1862306a36Sopenharmony_ci * from the platform code.
1962306a36Sopenharmony_ci */
2062306a36Sopenharmony_cistatic struct clk extal_clk = {
2162306a36Sopenharmony_ci	.rate		= 48000000,
2262306a36Sopenharmony_ci};
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistatic unsigned long pll_recalc(struct clk *clk)
2562306a36Sopenharmony_ci{
2662306a36Sopenharmony_ci	int multiplier;
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci	multiplier = test_mode_pin(MODE_PIN0) ? 24 : 16;
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci	return clk->parent->rate * multiplier;
3162306a36Sopenharmony_ci}
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cistatic struct sh_clk_ops pll_clk_ops = {
3462306a36Sopenharmony_ci	.recalc		= pll_recalc,
3562306a36Sopenharmony_ci};
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cistatic struct clk pll_clk = {
3862306a36Sopenharmony_ci	.ops		= &pll_clk_ops,
3962306a36Sopenharmony_ci	.parent		= &extal_clk,
4062306a36Sopenharmony_ci	.flags		= CLK_ENABLE_ON_INIT,
4162306a36Sopenharmony_ci};
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistatic struct clk *clks[] = {
4462306a36Sopenharmony_ci	&extal_clk,
4562306a36Sopenharmony_ci	&pll_clk,
4662306a36Sopenharmony_ci};
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistatic unsigned int div2[] = { 1, 1, 2, 1, 1, 4, 1, 6,
4962306a36Sopenharmony_ci			       1, 1, 1, 16, 1, 24, 1, 1 };
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_cistatic struct clk_div_mult_table div4_div_mult_table = {
5262306a36Sopenharmony_ci	.divisors = div2,
5362306a36Sopenharmony_ci	.nr_divisors = ARRAY_SIZE(div2),
5462306a36Sopenharmony_ci};
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistatic struct clk_div4_table div4_table = {
5762306a36Sopenharmony_ci	.div_mult_table = &div4_div_mult_table,
5862306a36Sopenharmony_ci};
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cienum { DIV4_I, DIV4_SH, DIV4_P, DIV4_NR };
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci#define DIV4(_bit, _mask, _flags) \
6362306a36Sopenharmony_ci  SH_CLK_DIV4(&pll_clk, FRQCR, _bit, _mask, _flags)
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_cistruct clk div4_clks[DIV4_NR] = {
6662306a36Sopenharmony_ci	/*
6762306a36Sopenharmony_ci	 * P clock is always enable, because some P clock modules is used
6862306a36Sopenharmony_ci	 * by Host PC.
6962306a36Sopenharmony_ci	 */
7062306a36Sopenharmony_ci	[DIV4_P] = DIV4(0, 0x2800, CLK_ENABLE_ON_INIT),
7162306a36Sopenharmony_ci	[DIV4_SH] = DIV4(12, 0x00a0, CLK_ENABLE_ON_INIT),
7262306a36Sopenharmony_ci	[DIV4_I] = DIV4(20, 0x0004, CLK_ENABLE_ON_INIT),
7362306a36Sopenharmony_ci};
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci#define MSTPCR0		0xffc80030
7662306a36Sopenharmony_ci#define MSTPCR1		0xffc80034
7762306a36Sopenharmony_ci#define MSTPCR2		0xffc10028
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_cienum { MSTP004, MSTP000, MSTP127, MSTP114, MSTP113, MSTP112,
8062306a36Sopenharmony_ci       MSTP111, MSTP110, MSTP103, MSTP102, MSTP220,
8162306a36Sopenharmony_ci       MSTP_NR };
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cistatic struct clk mstp_clks[MSTP_NR] = {
8462306a36Sopenharmony_ci	/* MSTPCR0 */
8562306a36Sopenharmony_ci	[MSTP004] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 4, 0),
8662306a36Sopenharmony_ci	[MSTP000] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 0, 0),
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	/* MSTPCR1 */
8962306a36Sopenharmony_ci	[MSTP127] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 27, 0),
9062306a36Sopenharmony_ci	[MSTP114] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 14, 0),
9162306a36Sopenharmony_ci	[MSTP113] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 13, 0),
9262306a36Sopenharmony_ci	[MSTP112] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 12, 0),
9362306a36Sopenharmony_ci	[MSTP111] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 11, 0),
9462306a36Sopenharmony_ci	[MSTP110] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 10, 0),
9562306a36Sopenharmony_ci	[MSTP103] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 3, 0),
9662306a36Sopenharmony_ci	[MSTP102] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 2, 0),
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	/* MSTPCR2 */
9962306a36Sopenharmony_ci	[MSTP220] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR2, 20, 0),
10062306a36Sopenharmony_ci};
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_cistatic struct clk_lookup lookups[] = {
10362306a36Sopenharmony_ci	/* main clocks */
10462306a36Sopenharmony_ci	CLKDEV_CON_ID("extal", &extal_clk),
10562306a36Sopenharmony_ci	CLKDEV_CON_ID("pll_clk", &pll_clk),
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	/* DIV4 clocks */
10862306a36Sopenharmony_ci	CLKDEV_CON_ID("peripheral_clk", &div4_clks[DIV4_P]),
10962306a36Sopenharmony_ci	CLKDEV_CON_ID("shyway_clk", &div4_clks[DIV4_SH]),
11062306a36Sopenharmony_ci	CLKDEV_CON_ID("cpu_clk", &div4_clks[DIV4_I]),
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	/* MSTP32 clocks */
11362306a36Sopenharmony_ci	CLKDEV_DEV_ID("sh_mobile_sdhi.0", &mstp_clks[MSTP004]),
11462306a36Sopenharmony_ci	CLKDEV_CON_ID("riic0", &mstp_clks[MSTP000]),
11562306a36Sopenharmony_ci	CLKDEV_CON_ID("riic1", &mstp_clks[MSTP000]),
11662306a36Sopenharmony_ci	CLKDEV_CON_ID("riic2", &mstp_clks[MSTP000]),
11762306a36Sopenharmony_ci	CLKDEV_CON_ID("riic3", &mstp_clks[MSTP000]),
11862306a36Sopenharmony_ci	CLKDEV_CON_ID("riic4", &mstp_clks[MSTP000]),
11962306a36Sopenharmony_ci	CLKDEV_CON_ID("riic5", &mstp_clks[MSTP000]),
12062306a36Sopenharmony_ci	CLKDEV_CON_ID("riic6", &mstp_clks[MSTP000]),
12162306a36Sopenharmony_ci	CLKDEV_CON_ID("riic7", &mstp_clks[MSTP000]),
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	CLKDEV_ICK_ID("fck", "sh-tmu.0", &mstp_clks[MSTP113]),
12462306a36Sopenharmony_ci	CLKDEV_ICK_ID("fck", "sh-tmu.1", &mstp_clks[MSTP114]),
12562306a36Sopenharmony_ci	CLKDEV_ICK_ID("fck", "sh-sci.2", &mstp_clks[MSTP112]),
12662306a36Sopenharmony_ci	CLKDEV_ICK_ID("fck", "sh-sci.1", &mstp_clks[MSTP111]),
12762306a36Sopenharmony_ci	CLKDEV_ICK_ID("fck", "sh-sci.0", &mstp_clks[MSTP110]),
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	CLKDEV_CON_ID("usb_fck", &mstp_clks[MSTP103]),
13062306a36Sopenharmony_ci	CLKDEV_DEV_ID("renesas_usbhs.0", &mstp_clks[MSTP102]),
13162306a36Sopenharmony_ci	CLKDEV_CON_ID("mmc0", &mstp_clks[MSTP220]),
13262306a36Sopenharmony_ci	CLKDEV_DEV_ID("rspi.2", &mstp_clks[MSTP127]),
13362306a36Sopenharmony_ci};
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ciint __init arch_clk_init(void)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci	int i, ret = 0;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(clks); i++)
14062306a36Sopenharmony_ci		ret |= clk_register(clks[i]);
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	clkdev_add_table(lookups, ARRAY_SIZE(lookups));
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	if (!ret)
14562306a36Sopenharmony_ci		ret = sh_clk_div4_register(div4_clks, ARRAY_SIZE(div4_clks),
14662306a36Sopenharmony_ci					   &div4_table);
14762306a36Sopenharmony_ci	if (!ret)
14862306a36Sopenharmony_ci		ret = sh_clk_mstp_register(mstp_clks, MSTP_NR);
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	return ret;
15162306a36Sopenharmony_ci}
15262306a36Sopenharmony_ci
153