18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * arch/sh/kernel/cpu/sh3/clock-sh3.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Generic SH-3 support for the clock framework
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  Copyright (C) 2005  Paul Mundt
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * FRQCR parsing hacked out of arch/sh/kernel/time.c
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *  Copyright (C) 1999  Tetsuya Okada & Niibe Yutaka
128c2ecf20Sopenharmony_ci *  Copyright (C) 2000  Philipp Rumpf <prumpf@tux.org>
138c2ecf20Sopenharmony_ci *  Copyright (C) 2002, 2003, 2004  Paul Mundt
148c2ecf20Sopenharmony_ci *  Copyright (C) 2002  M. R. Brown  <mrbrown@linux-sh.org>
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci#include <linux/init.h>
178c2ecf20Sopenharmony_ci#include <linux/kernel.h>
188c2ecf20Sopenharmony_ci#include <asm/clock.h>
198c2ecf20Sopenharmony_ci#include <asm/freq.h>
208c2ecf20Sopenharmony_ci#include <asm/io.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic int stc_multipliers[] = { 1, 2, 3, 4, 6, 1, 1, 1 };
238c2ecf20Sopenharmony_cistatic int ifc_divisors[]    = { 1, 2, 3, 4, 1, 1, 1, 1 };
248c2ecf20Sopenharmony_cistatic int pfc_divisors[]    = { 1, 2, 3, 4, 6, 1, 1, 1 };
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic void master_clk_init(struct clk *clk)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	int frqcr = __raw_readw(FRQCR);
298c2ecf20Sopenharmony_ci	int idx = ((frqcr & 0x2000) >> 11) | (frqcr & 0x0003);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	clk->rate *= pfc_divisors[idx];
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic struct sh_clk_ops sh3_master_clk_ops = {
358c2ecf20Sopenharmony_ci	.init		= master_clk_init,
368c2ecf20Sopenharmony_ci};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic unsigned long module_clk_recalc(struct clk *clk)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	int frqcr = __raw_readw(FRQCR);
418c2ecf20Sopenharmony_ci	int idx = ((frqcr & 0x2000) >> 11) | (frqcr & 0x0003);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	return clk->parent->rate / pfc_divisors[idx];
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic struct sh_clk_ops sh3_module_clk_ops = {
478c2ecf20Sopenharmony_ci	.recalc		= module_clk_recalc,
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic unsigned long bus_clk_recalc(struct clk *clk)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	int frqcr = __raw_readw(FRQCR);
538c2ecf20Sopenharmony_ci	int idx = ((frqcr & 0x8000) >> 13) | ((frqcr & 0x0030) >> 4);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	return clk->parent->rate / stc_multipliers[idx];
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic struct sh_clk_ops sh3_bus_clk_ops = {
598c2ecf20Sopenharmony_ci	.recalc		= bus_clk_recalc,
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic unsigned long cpu_clk_recalc(struct clk *clk)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	int frqcr = __raw_readw(FRQCR);
658c2ecf20Sopenharmony_ci	int idx = ((frqcr & 0x4000) >> 12) | ((frqcr & 0x000c) >> 2);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	return clk->parent->rate / ifc_divisors[idx];
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic struct sh_clk_ops sh3_cpu_clk_ops = {
718c2ecf20Sopenharmony_ci	.recalc		= cpu_clk_recalc,
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic struct sh_clk_ops *sh3_clk_ops[] = {
758c2ecf20Sopenharmony_ci	&sh3_master_clk_ops,
768c2ecf20Sopenharmony_ci	&sh3_module_clk_ops,
778c2ecf20Sopenharmony_ci	&sh3_bus_clk_ops,
788c2ecf20Sopenharmony_ci	&sh3_cpu_clk_ops,
798c2ecf20Sopenharmony_ci};
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_civoid __init arch_init_clk_ops(struct sh_clk_ops **ops, int idx)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	if (idx < ARRAY_SIZE(sh3_clk_ops))
848c2ecf20Sopenharmony_ci		*ops = sh3_clk_ops[idx];
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
87