1c01f0f1aSYoshihiro Shimoda /*
2c01f0f1aSYoshihiro Shimoda  * arch/sh/kernel/cpu/sh4/clock-sh7757.c
3c01f0f1aSYoshihiro Shimoda  *
4c01f0f1aSYoshihiro Shimoda  * SH7757 support for the clock framework
5c01f0f1aSYoshihiro Shimoda  *
656c52986SYoshihiro Shimoda  *  Copyright (C) 2009-2010  Renesas Solutions Corp.
7c01f0f1aSYoshihiro Shimoda  *
8c01f0f1aSYoshihiro Shimoda  * This file is subject to the terms and conditions of the GNU General Public
9c01f0f1aSYoshihiro Shimoda  * License.  See the file "COPYING" in the main directory of this archive
10c01f0f1aSYoshihiro Shimoda  * for more details.
11c01f0f1aSYoshihiro Shimoda  */
12c01f0f1aSYoshihiro Shimoda #include <linux/init.h>
13c01f0f1aSYoshihiro Shimoda #include <linux/kernel.h>
14c01f0f1aSYoshihiro Shimoda #include <linux/io.h>
156d803ba7SJean-Christop PLAGNIOL-VILLARD #include <linux/clkdev.h>
16c01f0f1aSYoshihiro Shimoda #include <asm/clock.h>
17c01f0f1aSYoshihiro Shimoda #include <asm/freq.h>
18c01f0f1aSYoshihiro Shimoda 
19c01f0f1aSYoshihiro Shimoda /*
2056c52986SYoshihiro Shimoda  * Default rate for the root input clock, reset this with clk_set_rate()
2156c52986SYoshihiro Shimoda  * from the platform code.
22c01f0f1aSYoshihiro Shimoda  */
2356c52986SYoshihiro Shimoda static struct clk extal_clk = {
2456c52986SYoshihiro Shimoda 	.rate		= 48000000,
2556c52986SYoshihiro Shimoda };
2656c52986SYoshihiro Shimoda 
2756c52986SYoshihiro Shimoda static unsigned long pll_recalc(struct clk *clk)
2856c52986SYoshihiro Shimoda {
2956c52986SYoshihiro Shimoda 	int multiplier;
3056c52986SYoshihiro Shimoda 
3156c52986SYoshihiro Shimoda 	multiplier = test_mode_pin(MODE_PIN0) ? 24 : 16;
3256c52986SYoshihiro Shimoda 
3356c52986SYoshihiro Shimoda 	return clk->parent->rate * multiplier;
3456c52986SYoshihiro Shimoda }
3556c52986SYoshihiro Shimoda 
3656c52986SYoshihiro Shimoda static struct clk_ops pll_clk_ops = {
3756c52986SYoshihiro Shimoda 	.recalc		= pll_recalc,
3856c52986SYoshihiro Shimoda };
3956c52986SYoshihiro Shimoda 
4056c52986SYoshihiro Shimoda static struct clk pll_clk = {
4156c52986SYoshihiro Shimoda 	.ops		= &pll_clk_ops,
4256c52986SYoshihiro Shimoda 	.parent		= &extal_clk,
4356c52986SYoshihiro Shimoda 	.flags		= CLK_ENABLE_ON_INIT,
4456c52986SYoshihiro Shimoda };
4556c52986SYoshihiro Shimoda 
4656c52986SYoshihiro Shimoda static struct clk *clks[] = {
4756c52986SYoshihiro Shimoda 	&extal_clk,
4856c52986SYoshihiro Shimoda 	&pll_clk,
4956c52986SYoshihiro Shimoda };
5056c52986SYoshihiro Shimoda 
5156c52986SYoshihiro Shimoda static unsigned int div2[] = { 1, 1, 2, 1, 1, 4, 1, 6,
5256c52986SYoshihiro Shimoda 			       1, 1, 1, 16, 1, 24, 1, 1 };
5356c52986SYoshihiro Shimoda 
5456c52986SYoshihiro Shimoda static struct clk_div_mult_table div4_div_mult_table = {
5556c52986SYoshihiro Shimoda 	.divisors = div2,
5656c52986SYoshihiro Shimoda 	.nr_divisors = ARRAY_SIZE(div2),
5756c52986SYoshihiro Shimoda };
5856c52986SYoshihiro Shimoda 
5956c52986SYoshihiro Shimoda static struct clk_div4_table div4_table = {
6056c52986SYoshihiro Shimoda 	.div_mult_table = &div4_div_mult_table,
6156c52986SYoshihiro Shimoda };
6256c52986SYoshihiro Shimoda 
6356c52986SYoshihiro Shimoda enum { DIV4_I, DIV4_SH, DIV4_P, DIV4_NR };
6456c52986SYoshihiro Shimoda 
6556c52986SYoshihiro Shimoda #define DIV4(_bit, _mask, _flags) \
6656c52986SYoshihiro Shimoda   SH_CLK_DIV4(&pll_clk, FRQCR, _bit, _mask, _flags)
6756c52986SYoshihiro Shimoda 
6856c52986SYoshihiro Shimoda struct clk div4_clks[DIV4_NR] = {
6956c52986SYoshihiro Shimoda 	/*
7056c52986SYoshihiro Shimoda 	 * P clock is always enable, because some P clock modules is used
7156c52986SYoshihiro Shimoda 	 * by Host PC.
7256c52986SYoshihiro Shimoda 	 */
7356c52986SYoshihiro Shimoda 	[DIV4_P] = DIV4(0, 0x2800, CLK_ENABLE_ON_INIT),
7456c52986SYoshihiro Shimoda 	[DIV4_SH] = DIV4(12, 0x00a0, CLK_ENABLE_ON_INIT),
7556c52986SYoshihiro Shimoda 	[DIV4_I] = DIV4(20, 0x0004, CLK_ENABLE_ON_INIT),
7656c52986SYoshihiro Shimoda };
7756c52986SYoshihiro Shimoda 
7856c52986SYoshihiro Shimoda #define MSTPCR0		0xffc80030
7956c52986SYoshihiro Shimoda #define MSTPCR1		0xffc80034
8053bc18efSYoshihiro Shimoda #define MSTPCR2		0xffc10028
8156c52986SYoshihiro Shimoda 
8256c52986SYoshihiro Shimoda enum { MSTP004, MSTP000, MSTP114, MSTP113, MSTP112,
8353bc18efSYoshihiro Shimoda        MSTP111, MSTP110, MSTP103, MSTP102, MSTP220,
8456c52986SYoshihiro Shimoda        MSTP_NR };
8556c52986SYoshihiro Shimoda 
8656c52986SYoshihiro Shimoda static struct clk mstp_clks[MSTP_NR] = {
8756c52986SYoshihiro Shimoda 	/* MSTPCR0 */
8856c52986SYoshihiro Shimoda 	[MSTP004] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 4, 0),
8956c52986SYoshihiro Shimoda 	[MSTP000] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 0, 0),
9056c52986SYoshihiro Shimoda 
9156c52986SYoshihiro Shimoda 	/* MSTPCR1 */
9256c52986SYoshihiro Shimoda 	[MSTP114] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 14, 0),
9356c52986SYoshihiro Shimoda 	[MSTP113] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 13, 0),
9456c52986SYoshihiro Shimoda 	[MSTP112] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 12, 0),
9556c52986SYoshihiro Shimoda 	[MSTP111] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 11, 0),
9656c52986SYoshihiro Shimoda 	[MSTP110] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 10, 0),
9756c52986SYoshihiro Shimoda 	[MSTP103] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 3, 0),
9856c52986SYoshihiro Shimoda 	[MSTP102] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 2, 0),
9953bc18efSYoshihiro Shimoda 
10053bc18efSYoshihiro Shimoda 	/* MSTPCR2 */
10153bc18efSYoshihiro Shimoda 	[MSTP220] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR2, 20, 0),
102c01f0f1aSYoshihiro Shimoda };
103c01f0f1aSYoshihiro Shimoda 
104f0e7f902SMagnus Damm #define CLKDEV_CON_ID(_id, _clk) { .con_id = _id, .clk = _clk }
1059b417571SKuninori Morimoto #define CLKDEV_ICK_ID(_cid, _did, _clk) { .con_id = _cid, .dev_id = _did, .clk = _clk }
106f0e7f902SMagnus Damm 
107f0e7f902SMagnus Damm static struct clk_lookup lookups[] = {
108f0e7f902SMagnus Damm 	/* main clocks */
10956c52986SYoshihiro Shimoda 	CLKDEV_CON_ID("extal", &extal_clk),
11056c52986SYoshihiro Shimoda 	CLKDEV_CON_ID("pll_clk", &pll_clk),
11156c52986SYoshihiro Shimoda 
11256c52986SYoshihiro Shimoda 	/* DIV4 clocks */
11356c52986SYoshihiro Shimoda 	CLKDEV_CON_ID("peripheral_clk", &div4_clks[DIV4_P]),
11456c52986SYoshihiro Shimoda 	CLKDEV_CON_ID("shyway_clk", &div4_clks[DIV4_SH]),
11556c52986SYoshihiro Shimoda 	CLKDEV_CON_ID("cpu_clk", &div4_clks[DIV4_I]),
11656c52986SYoshihiro Shimoda 
11756c52986SYoshihiro Shimoda 	/* MSTP32 clocks */
11856c52986SYoshihiro Shimoda 	CLKDEV_CON_ID("sdhi0", &mstp_clks[MSTP004]),
11956c52986SYoshihiro Shimoda 	CLKDEV_CON_ID("riic", &mstp_clks[MSTP000]),
1209b417571SKuninori Morimoto 
1219b417571SKuninori Morimoto 	CLKDEV_ICK_ID("tmu_fck", "sh_tmu.0", &mstp_clks[MSTP113]),
1229b417571SKuninori Morimoto 	CLKDEV_ICK_ID("tmu_fck", "sh_tmu.1", &mstp_clks[MSTP114]),
1239b417571SKuninori Morimoto 	CLKDEV_ICK_ID("sci_fck", "sh-sci.2", &mstp_clks[MSTP112]),
1249b417571SKuninori Morimoto 	CLKDEV_ICK_ID("sci_fck", "sh-sci.1", &mstp_clks[MSTP111]),
1259b417571SKuninori Morimoto 	CLKDEV_ICK_ID("sci_fck", "sh-sci.0", &mstp_clks[MSTP110]),
1269b417571SKuninori Morimoto 
12756c52986SYoshihiro Shimoda 	CLKDEV_CON_ID("usb0", &mstp_clks[MSTP102]),
12853bc18efSYoshihiro Shimoda 	CLKDEV_CON_ID("mmc0", &mstp_clks[MSTP220]),
129f0e7f902SMagnus Damm };
130f0e7f902SMagnus Damm 
13156c52986SYoshihiro Shimoda int __init arch_clk_init(void)
132c01f0f1aSYoshihiro Shimoda {
13356c52986SYoshihiro Shimoda 	int i, ret = 0;
134c01f0f1aSYoshihiro Shimoda 
13556c52986SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(clks); i++)
13656c52986SYoshihiro Shimoda 		ret |= clk_register(clks[i]);
13756c52986SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(lookups); i++)
13856c52986SYoshihiro Shimoda 		clkdev_add(&lookups[i]);
139c01f0f1aSYoshihiro Shimoda 
14056c52986SYoshihiro Shimoda 	if (!ret)
14156c52986SYoshihiro Shimoda 		ret = sh_clk_div4_register(div4_clks, ARRAY_SIZE(div4_clks),
14256c52986SYoshihiro Shimoda 					   &div4_table);
14356c52986SYoshihiro Shimoda 	if (!ret)
14456c52986SYoshihiro Shimoda 		ret = sh_clk_mstp32_register(mstp_clks, MSTP_NR);
14556c52986SYoshihiro Shimoda 
14656c52986SYoshihiro Shimoda 	return ret;
147c01f0f1aSYoshihiro Shimoda }
148c01f0f1aSYoshihiro Shimoda 
149