xref: /linux/arch/sh/kernel/cpu/sh3/clock-sh3.c (revision 597473720f4dc69749542bfcfed4a927a43d935e)
1*5ab5d57cSKuninori Morimoto // SPDX-License-Identifier: GPL-2.0
236ddf31bSPaul Mundt /*
336ddf31bSPaul Mundt  * arch/sh/kernel/cpu/sh3/clock-sh3.c
436ddf31bSPaul Mundt  *
536ddf31bSPaul Mundt  * Generic SH-3 support for the clock framework
636ddf31bSPaul Mundt  *
736ddf31bSPaul Mundt  *  Copyright (C) 2005  Paul Mundt
836ddf31bSPaul Mundt  *
936ddf31bSPaul Mundt  * FRQCR parsing hacked out of arch/sh/kernel/time.c
1036ddf31bSPaul Mundt  *
1136ddf31bSPaul Mundt  *  Copyright (C) 1999  Tetsuya Okada & Niibe Yutaka
1236ddf31bSPaul Mundt  *  Copyright (C) 2000  Philipp Rumpf <prumpf@tux.org>
1336ddf31bSPaul Mundt  *  Copyright (C) 2002, 2003, 2004  Paul Mundt
1436ddf31bSPaul Mundt  *  Copyright (C) 2002  M. R. Brown  <mrbrown@linux-sh.org>
1536ddf31bSPaul Mundt  */
1636ddf31bSPaul Mundt #include <linux/init.h>
1736ddf31bSPaul Mundt #include <linux/kernel.h>
1836ddf31bSPaul Mundt #include <asm/clock.h>
1936ddf31bSPaul Mundt #include <asm/freq.h>
2036ddf31bSPaul Mundt #include <asm/io.h>
2136ddf31bSPaul Mundt 
2236ddf31bSPaul Mundt static int stc_multipliers[] = { 1, 2, 3, 4, 6, 1, 1, 1 };
2336ddf31bSPaul Mundt static int ifc_divisors[]    = { 1, 2, 3, 4, 1, 1, 1, 1 };
2436ddf31bSPaul Mundt static int pfc_divisors[]    = { 1, 2, 3, 4, 6, 1, 1, 1 };
2536ddf31bSPaul Mundt 
master_clk_init(struct clk * clk)2636ddf31bSPaul Mundt static void master_clk_init(struct clk *clk)
2736ddf31bSPaul Mundt {
289d56dd3bSPaul Mundt 	int frqcr = __raw_readw(FRQCR);
2936ddf31bSPaul Mundt 	int idx = ((frqcr & 0x2000) >> 11) | (frqcr & 0x0003);
3036ddf31bSPaul Mundt 
3136ddf31bSPaul Mundt 	clk->rate *= pfc_divisors[idx];
3236ddf31bSPaul Mundt }
3336ddf31bSPaul Mundt 
3443ebacd0SMagnus Damm static struct sh_clk_ops sh3_master_clk_ops = {
3536ddf31bSPaul Mundt 	.init		= master_clk_init,
3636ddf31bSPaul Mundt };
3736ddf31bSPaul Mundt 
module_clk_recalc(struct clk * clk)38b68d8201SPaul Mundt static unsigned long module_clk_recalc(struct clk *clk)
3936ddf31bSPaul Mundt {
409d56dd3bSPaul Mundt 	int frqcr = __raw_readw(FRQCR);
4136ddf31bSPaul Mundt 	int idx = ((frqcr & 0x2000) >> 11) | (frqcr & 0x0003);
4236ddf31bSPaul Mundt 
43b68d8201SPaul Mundt 	return clk->parent->rate / pfc_divisors[idx];
4436ddf31bSPaul Mundt }
4536ddf31bSPaul Mundt 
4643ebacd0SMagnus Damm static struct sh_clk_ops sh3_module_clk_ops = {
4736ddf31bSPaul Mundt 	.recalc		= module_clk_recalc,
4836ddf31bSPaul Mundt };
4936ddf31bSPaul Mundt 
bus_clk_recalc(struct clk * clk)50b68d8201SPaul Mundt static unsigned long bus_clk_recalc(struct clk *clk)
5136ddf31bSPaul Mundt {
529d56dd3bSPaul Mundt 	int frqcr = __raw_readw(FRQCR);
5336ddf31bSPaul Mundt 	int idx = ((frqcr & 0x8000) >> 13) | ((frqcr & 0x0030) >> 4);
5436ddf31bSPaul Mundt 
55b68d8201SPaul Mundt 	return clk->parent->rate / stc_multipliers[idx];
5636ddf31bSPaul Mundt }
5736ddf31bSPaul Mundt 
5843ebacd0SMagnus Damm static struct sh_clk_ops sh3_bus_clk_ops = {
5936ddf31bSPaul Mundt 	.recalc		= bus_clk_recalc,
6036ddf31bSPaul Mundt };
6136ddf31bSPaul Mundt 
cpu_clk_recalc(struct clk * clk)62b68d8201SPaul Mundt static unsigned long cpu_clk_recalc(struct clk *clk)
6336ddf31bSPaul Mundt {
649d56dd3bSPaul Mundt 	int frqcr = __raw_readw(FRQCR);
6536ddf31bSPaul Mundt 	int idx = ((frqcr & 0x4000) >> 12) | ((frqcr & 0x000c) >> 2);
6636ddf31bSPaul Mundt 
67b68d8201SPaul Mundt 	return clk->parent->rate / ifc_divisors[idx];
6836ddf31bSPaul Mundt }
6936ddf31bSPaul Mundt 
7043ebacd0SMagnus Damm static struct sh_clk_ops sh3_cpu_clk_ops = {
7136ddf31bSPaul Mundt 	.recalc		= cpu_clk_recalc,
7236ddf31bSPaul Mundt };
7336ddf31bSPaul Mundt 
7443ebacd0SMagnus Damm static struct sh_clk_ops *sh3_clk_ops[] = {
7536ddf31bSPaul Mundt 	&sh3_master_clk_ops,
7636ddf31bSPaul Mundt 	&sh3_module_clk_ops,
7736ddf31bSPaul Mundt 	&sh3_bus_clk_ops,
7836ddf31bSPaul Mundt 	&sh3_cpu_clk_ops,
7936ddf31bSPaul Mundt };
8036ddf31bSPaul Mundt 
arch_init_clk_ops(struct sh_clk_ops ** ops,int idx)8143ebacd0SMagnus Damm void __init arch_init_clk_ops(struct sh_clk_ops **ops, int idx)
8236ddf31bSPaul Mundt {
8336ddf31bSPaul Mundt 	if (idx < ARRAY_SIZE(sh3_clk_ops))
8436ddf31bSPaul Mundt 		*ops = sh3_clk_ops[idx];
8536ddf31bSPaul Mundt }
8636ddf31bSPaul Mundt 
87