Path: blob/master/arch/sh/kernel/cpu/sh4/clock-sh4-202.c
37217 views
/*1* arch/sh/kernel/cpu/sh4/clock-sh4-202.c2*3* Additional SH4-202 support for the clock framework4*5* Copyright (C) 2005 Paul Mundt6*7* This file is subject to the terms and conditions of the GNU General Public8* License. See the file "COPYING" in the main directory of this archive9* for more details.10*/11#include <linux/init.h>12#include <linux/kernel.h>13#include <linux/err.h>14#include <linux/io.h>15#include <linux/clkdev.h>16#include <asm/clock.h>17#include <asm/freq.h>1819#define CPG2_FRQCR3 0xfe0a00182021static int frqcr3_divisors[] = { 1, 2, 3, 4, 6, 8, 16 };22static int frqcr3_values[] = { 0, 1, 2, 3, 4, 5, 6 };2324static unsigned long emi_clk_recalc(struct clk *clk)25{26int idx = __raw_readl(CPG2_FRQCR3) & 0x0007;27return clk->parent->rate / frqcr3_divisors[idx];28}2930static inline int frqcr3_lookup(struct clk *clk, unsigned long rate)31{32int divisor = clk->parent->rate / rate;33int i;3435for (i = 0; i < ARRAY_SIZE(frqcr3_divisors); i++)36if (frqcr3_divisors[i] == divisor)37return frqcr3_values[i];3839/* Safe fallback */40return 5;41}4243static struct clk_ops sh4202_emi_clk_ops = {44.recalc = emi_clk_recalc,45};4647static struct clk sh4202_emi_clk = {48.flags = CLK_ENABLE_ON_INIT,49.ops = &sh4202_emi_clk_ops,50};5152static unsigned long femi_clk_recalc(struct clk *clk)53{54int idx = (__raw_readl(CPG2_FRQCR3) >> 3) & 0x0007;55return clk->parent->rate / frqcr3_divisors[idx];56}5758static struct clk_ops sh4202_femi_clk_ops = {59.recalc = femi_clk_recalc,60};6162static struct clk sh4202_femi_clk = {63.flags = CLK_ENABLE_ON_INIT,64.ops = &sh4202_femi_clk_ops,65};6667static void shoc_clk_init(struct clk *clk)68{69int i;7071/*72* For some reason, the shoc_clk seems to be set to some really73* insane value at boot (values outside of the allowable frequency74* range for instance). We deal with this by scaling it back down75* to something sensible just in case.76*77* Start scaling from the high end down until we find something78* that passes rate verification..79*/80for (i = 0; i < ARRAY_SIZE(frqcr3_divisors); i++) {81int divisor = frqcr3_divisors[i];8283if (clk->ops->set_rate(clk, clk->parent->rate / divisor) == 0)84break;85}8687WARN_ON(i == ARRAY_SIZE(frqcr3_divisors)); /* Undefined clock */88}8990static unsigned long shoc_clk_recalc(struct clk *clk)91{92int idx = (__raw_readl(CPG2_FRQCR3) >> 6) & 0x0007;93return clk->parent->rate / frqcr3_divisors[idx];94}9596static int shoc_clk_verify_rate(struct clk *clk, unsigned long rate)97{98struct clk *bclk = clk_get(NULL, "bus_clk");99unsigned long bclk_rate = clk_get_rate(bclk);100101clk_put(bclk);102103if (rate > bclk_rate)104return 1;105if (rate > 66000000)106return 1;107108return 0;109}110111static int shoc_clk_set_rate(struct clk *clk, unsigned long rate)112{113unsigned long frqcr3;114unsigned int tmp;115116/* Make sure we have something sensible to switch to */117if (shoc_clk_verify_rate(clk, rate) != 0)118return -EINVAL;119120tmp = frqcr3_lookup(clk, rate);121122frqcr3 = __raw_readl(CPG2_FRQCR3);123frqcr3 &= ~(0x0007 << 6);124frqcr3 |= tmp << 6;125__raw_writel(frqcr3, CPG2_FRQCR3);126127clk->rate = clk->parent->rate / frqcr3_divisors[tmp];128129return 0;130}131132static struct clk_ops sh4202_shoc_clk_ops = {133.init = shoc_clk_init,134.recalc = shoc_clk_recalc,135.set_rate = shoc_clk_set_rate,136};137138static struct clk sh4202_shoc_clk = {139.flags = CLK_ENABLE_ON_INIT,140.ops = &sh4202_shoc_clk_ops,141};142143static struct clk *sh4202_onchip_clocks[] = {144&sh4202_emi_clk,145&sh4202_femi_clk,146&sh4202_shoc_clk,147};148149#define CLKDEV_CON_ID(_id, _clk) { .con_id = _id, .clk = _clk }150151static struct clk_lookup lookups[] = {152/* main clocks */153CLKDEV_CON_ID("emi_clk", &sh4202_emi_clk),154CLKDEV_CON_ID("femi_clk", &sh4202_femi_clk),155CLKDEV_CON_ID("shoc_clk", &sh4202_shoc_clk),156};157158int __init arch_clk_init(void)159{160struct clk *clk;161int i, ret = 0;162163cpg_clk_init();164165clk = clk_get(NULL, "master_clk");166for (i = 0; i < ARRAY_SIZE(sh4202_onchip_clocks); i++) {167struct clk *clkp = sh4202_onchip_clocks[i];168169clkp->parent = clk;170ret |= clk_register(clkp);171}172173clk_put(clk);174175clkdev_add_table(lookups, ARRAY_SIZE(lookups));176177return ret;178}179180181