Path: blob/master/arch/arm/mach-omap2/clkt_clksel.c
10817 views
/*1* clkt_clksel.c - OMAP2/3/4 clksel clock functions2*3* Copyright (C) 2005-2008 Texas Instruments, Inc.4* Copyright (C) 2004-2010 Nokia Corporation5*6* Contacts:7* Richard Woodruff <[email protected]>8* Paul Walmsley9*10* This program is free software; you can redistribute it and/or modify11* it under the terms of the GNU General Public License version 2 as12* published by the Free Software Foundation.13*14*15* clksel clocks are clocks that do not have a fixed parent, or that16* can divide their parent's rate, or possibly both at the same time, based17* on the contents of a hardware register bitfield.18*19* All of the various mux and divider settings can be encoded into20* struct clksel* data structures, and then these can be autogenerated21* from some hardware database for each new chip generation. This22* should avoid the need to write, review, and validate a lot of new23* clock code for each new chip, since it can be exported from the SoC24* design flow. This is now done on OMAP4.25*26* The fusion of mux and divider clocks is a software creation. In27* hardware reality, the multiplexer (parent selection) and the28* divider exist separately. XXX At some point these clksel clocks29* should be split into "divider" clocks and "mux" clocks to better30* match the hardware.31*32* (The name "clksel" comes from the name of the corresponding33* register field in the OMAP2/3 family of SoCs.)34*35* XXX Currently these clocks are only used in the OMAP2/3/4 code, but36* many of the OMAP1 clocks should be convertible to use this37* mechanism.38*/39#undef DEBUG4041#include <linux/kernel.h>42#include <linux/errno.h>43#include <linux/clk.h>44#include <linux/io.h>4546#include <plat/clock.h>4748#include "clock.h"4950/* Private functions */5152/**53* _get_clksel_by_parent() - return clksel struct for a given clk & parent54* @clk: OMAP struct clk ptr to inspect55* @src_clk: OMAP struct clk ptr of the parent clk to search for56*57* Scan the struct clksel array associated with the clock to find58* the element associated with the supplied parent clock address.59* Returns a pointer to the struct clksel on success or NULL on error.60*/61static const struct clksel *_get_clksel_by_parent(struct clk *clk,62struct clk *src_clk)63{64const struct clksel *clks;6566for (clks = clk->clksel; clks->parent; clks++)67if (clks->parent == src_clk)68break; /* Found the requested parent */6970if (!clks->parent) {71/* This indicates a data problem */72WARN(1, "clock: Could not find parent clock %s in clksel array "73"of clock %s\n", src_clk->name, clk->name);74return NULL;75}7677return clks;78}7980/**81* _get_div_and_fieldval() - find the new clksel divisor and field value to use82* @src_clk: planned new parent struct clk *83* @clk: struct clk * that is being reparented84* @field_val: pointer to a u32 to contain the register data for the divisor85*86* Given an intended new parent struct clk * @src_clk, and the struct87* clk * @clk to the clock that is being reparented, find the88* appropriate rate divisor for the new clock (returned as the return89* value), and the corresponding register bitfield data to program to90* reach that divisor (returned in the u32 pointed to by @field_val).91* Returns 0 on error, or returns the newly-selected divisor upon92* success (in this latter case, the corresponding register bitfield93* value is passed back in the variable pointed to by @field_val)94*/95static u8 _get_div_and_fieldval(struct clk *src_clk, struct clk *clk,96u32 *field_val)97{98const struct clksel *clks;99const struct clksel_rate *clkr, *max_clkr = NULL;100u8 max_div = 0;101102clks = _get_clksel_by_parent(clk, src_clk);103if (!clks)104return 0;105106/*107* Find the highest divisor (e.g., the one resulting in the108* lowest rate) to use as the default. This should avoid109* clock rates that are too high for the device. XXX A better110* solution here would be to try to determine if there is a111* divisor matching the original clock rate before the parent112* switch, and if it cannot be found, to fall back to the113* highest divisor.114*/115for (clkr = clks->rates; clkr->div; clkr++) {116if (!(clkr->flags & cpu_mask))117continue;118119if (clkr->div > max_div) {120max_div = clkr->div;121max_clkr = clkr;122}123}124125if (max_div == 0) {126/* This indicates an error in the clksel data */127WARN(1, "clock: Could not find divisor for clock %s parent %s"128"\n", clk->name, src_clk->parent->name);129return 0;130}131132*field_val = max_clkr->val;133134return max_div;135}136137/**138* _write_clksel_reg() - program a clock's clksel register in hardware139* @clk: struct clk * to program140* @v: clksel bitfield value to program (with LSB at bit 0)141*142* Shift the clksel register bitfield value @v to its appropriate143* location in the clksel register and write it in. This function144* will ensure that the write to the clksel_reg reaches its145* destination before returning -- important since PRM and CM register146* accesses can be quite slow compared to ARM cycles -- but does not147* take into account any time the hardware might take to switch the148* clock source.149*/150static void _write_clksel_reg(struct clk *clk, u32 field_val)151{152u32 v;153154v = __raw_readl(clk->clksel_reg);155v &= ~clk->clksel_mask;156v |= field_val << __ffs(clk->clksel_mask);157__raw_writel(v, clk->clksel_reg);158159v = __raw_readl(clk->clksel_reg); /* OCP barrier */160}161162/**163* _clksel_to_divisor() - turn clksel field value into integer divider164* @clk: OMAP struct clk to use165* @field_val: register field value to find166*167* Given a struct clk of a rate-selectable clksel clock, and a register field168* value to search for, find the corresponding clock divisor. The register169* field value should be pre-masked and shifted down so the LSB is at bit 0170* before calling. Returns 0 on error or returns the actual integer divisor171* upon success.172*/173static u32 _clksel_to_divisor(struct clk *clk, u32 field_val)174{175const struct clksel *clks;176const struct clksel_rate *clkr;177178clks = _get_clksel_by_parent(clk, clk->parent);179if (!clks)180return 0;181182for (clkr = clks->rates; clkr->div; clkr++) {183if (!(clkr->flags & cpu_mask))184continue;185186if (clkr->val == field_val)187break;188}189190if (!clkr->div) {191/* This indicates a data error */192WARN(1, "clock: Could not find fieldval %d for clock %s parent "193"%s\n", field_val, clk->name, clk->parent->name);194return 0;195}196197return clkr->div;198}199200/**201* _divisor_to_clksel() - turn clksel integer divisor into a field value202* @clk: OMAP struct clk to use203* @div: integer divisor to search for204*205* Given a struct clk of a rate-selectable clksel clock, and a clock206* divisor, find the corresponding register field value. Returns the207* register field value _before_ left-shifting (i.e., LSB is at bit208* 0); or returns 0xFFFFFFFF (~0) upon error.209*/210static u32 _divisor_to_clksel(struct clk *clk, u32 div)211{212const struct clksel *clks;213const struct clksel_rate *clkr;214215/* should never happen */216WARN_ON(div == 0);217218clks = _get_clksel_by_parent(clk, clk->parent);219if (!clks)220return ~0;221222for (clkr = clks->rates; clkr->div; clkr++) {223if (!(clkr->flags & cpu_mask))224continue;225226if (clkr->div == div)227break;228}229230if (!clkr->div) {231pr_err("clock: Could not find divisor %d for clock %s parent "232"%s\n", div, clk->name, clk->parent->name);233return ~0;234}235236return clkr->val;237}238239/**240* _read_divisor() - get current divisor applied to parent clock (from hdwr)241* @clk: OMAP struct clk to use.242*243* Read the current divisor register value for @clk that is programmed244* into the hardware, convert it into the actual divisor value, and245* return it; or return 0 on error.246*/247static u32 _read_divisor(struct clk *clk)248{249u32 v;250251if (!clk->clksel || !clk->clksel_mask)252return 0;253254v = __raw_readl(clk->clksel_reg);255v &= clk->clksel_mask;256v >>= __ffs(clk->clksel_mask);257258return _clksel_to_divisor(clk, v);259}260261/* Public functions */262263/**264* omap2_clksel_round_rate_div() - find divisor for the given clock and rate265* @clk: OMAP struct clk to use266* @target_rate: desired clock rate267* @new_div: ptr to where we should store the divisor268*269* Finds 'best' divider value in an array based on the source and target270* rates. The divider array must be sorted with smallest divider first.271* This function is also used by the DPLL3 M2 divider code.272*273* Returns the rounded clock rate or returns 0xffffffff on error.274*/275u32 omap2_clksel_round_rate_div(struct clk *clk, unsigned long target_rate,276u32 *new_div)277{278unsigned long test_rate;279const struct clksel *clks;280const struct clksel_rate *clkr;281u32 last_div = 0;282283if (!clk->clksel || !clk->clksel_mask)284return ~0;285286pr_debug("clock: clksel_round_rate_div: %s target_rate %ld\n",287clk->name, target_rate);288289*new_div = 1;290291clks = _get_clksel_by_parent(clk, clk->parent);292if (!clks)293return ~0;294295for (clkr = clks->rates; clkr->div; clkr++) {296if (!(clkr->flags & cpu_mask))297continue;298299/* Sanity check */300if (clkr->div <= last_div)301pr_err("clock: clksel_rate table not sorted "302"for clock %s", clk->name);303304last_div = clkr->div;305306test_rate = clk->parent->rate / clkr->div;307308if (test_rate <= target_rate)309break; /* found it */310}311312if (!clkr->div) {313pr_err("clock: Could not find divisor for target "314"rate %ld for clock %s parent %s\n", target_rate,315clk->name, clk->parent->name);316return ~0;317}318319*new_div = clkr->div;320321pr_debug("clock: new_div = %d, new_rate = %ld\n", *new_div,322(clk->parent->rate / clkr->div));323324return clk->parent->rate / clkr->div;325}326327/*328* Clocktype interface functions to the OMAP clock code329* (i.e., those used in struct clk field function pointers, etc.)330*/331332/**333* omap2_init_clksel_parent() - set a clksel clk's parent field from the hdwr334* @clk: OMAP clock struct ptr to use335*336* Given a pointer @clk to a source-selectable struct clk, read the337* hardware register and determine what its parent is currently set338* to. Update @clk's .parent field with the appropriate clk ptr. No339* return value.340*/341void omap2_init_clksel_parent(struct clk *clk)342{343const struct clksel *clks;344const struct clksel_rate *clkr;345u32 r, found = 0;346347if (!clk->clksel || !clk->clksel_mask)348return;349350r = __raw_readl(clk->clksel_reg) & clk->clksel_mask;351r >>= __ffs(clk->clksel_mask);352353for (clks = clk->clksel; clks->parent && !found; clks++) {354for (clkr = clks->rates; clkr->div && !found; clkr++) {355if (!(clkr->flags & cpu_mask))356continue;357358if (clkr->val == r) {359if (clk->parent != clks->parent) {360pr_debug("clock: inited %s parent "361"to %s (was %s)\n",362clk->name, clks->parent->name,363((clk->parent) ?364clk->parent->name : "NULL"));365clk_reparent(clk, clks->parent);366};367found = 1;368}369}370}371372/* This indicates a data error */373WARN(!found, "clock: %s: init parent: could not find regval %0x\n",374clk->name, r);375376return;377}378379/**380* omap2_clksel_recalc() - function ptr to pass via struct clk .recalc field381* @clk: struct clk *382*383* This function is intended to be called only by the clock framework.384* Each clksel clock should have its struct clk .recalc field set to this385* function. Returns the clock's current rate, based on its parent's rate386* and its current divisor setting in the hardware.387*/388unsigned long omap2_clksel_recalc(struct clk *clk)389{390unsigned long rate;391u32 div = 0;392393div = _read_divisor(clk);394if (div == 0)395return clk->rate;396397rate = clk->parent->rate / div;398399pr_debug("clock: %s: recalc'd rate is %ld (div %d)\n", clk->name,400rate, div);401402return rate;403}404405/**406* omap2_clksel_round_rate() - find rounded rate for the given clock and rate407* @clk: OMAP struct clk to use408* @target_rate: desired clock rate409*410* This function is intended to be called only by the clock framework.411* Finds best target rate based on the source clock and possible dividers.412* rates. The divider array must be sorted with smallest divider first.413*414* Returns the rounded clock rate or returns 0xffffffff on error.415*/416long omap2_clksel_round_rate(struct clk *clk, unsigned long target_rate)417{418u32 new_div;419420return omap2_clksel_round_rate_div(clk, target_rate, &new_div);421}422423/**424* omap2_clksel_set_rate() - program clock rate in hardware425* @clk: struct clk * to program rate426* @rate: target rate to program427*428* This function is intended to be called only by the clock framework.429* Program @clk's rate to @rate in the hardware. The clock can be430* either enabled or disabled when this happens, although if the clock431* is enabled, some downstream devices may glitch or behave432* unpredictably when the clock rate is changed - this depends on the433* hardware. This function does not currently check the usecount of434* the clock, so if multiple drivers are using the clock, and the rate435* is changed, they will all be affected without any notification.436* Returns -EINVAL upon error, or 0 upon success.437*/438int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)439{440u32 field_val, validrate, new_div = 0;441442if (!clk->clksel || !clk->clksel_mask)443return -EINVAL;444445validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);446if (validrate != rate)447return -EINVAL;448449field_val = _divisor_to_clksel(clk, new_div);450if (field_val == ~0)451return -EINVAL;452453_write_clksel_reg(clk, field_val);454455clk->rate = clk->parent->rate / new_div;456457pr_debug("clock: %s: set rate to %ld\n", clk->name, clk->rate);458459return 0;460}461462/*463* Clksel parent setting function - not passed in struct clk function464* pointer - instead, the OMAP clock code currently assumes that any465* parent-setting clock is a clksel clock, and calls466* omap2_clksel_set_parent() by default467*/468469/**470* omap2_clksel_set_parent() - change a clock's parent clock471* @clk: struct clk * of the child clock472* @new_parent: struct clk * of the new parent clock473*474* This function is intended to be called only by the clock framework.475* Change the parent clock of clock @clk to @new_parent. This is476* intended to be used while @clk is disabled. This function does not477* currently check the usecount of the clock, so if multiple drivers478* are using the clock, and the parent is changed, they will all be479* affected without any notification. Returns -EINVAL upon error, or480* 0 upon success.481*/482int omap2_clksel_set_parent(struct clk *clk, struct clk *new_parent)483{484u32 field_val = 0;485u32 parent_div;486487if (!clk->clksel || !clk->clksel_mask)488return -EINVAL;489490parent_div = _get_div_and_fieldval(new_parent, clk, &field_val);491if (!parent_div)492return -EINVAL;493494_write_clksel_reg(clk, field_val);495496clk_reparent(clk, new_parent);497498/* CLKSEL clocks follow their parents' rates, divided by a divisor */499clk->rate = new_parent->rate;500501if (parent_div > 0)502clk->rate /= parent_div;503504pr_debug("clock: %s: set parent to %s (new rate %ld)\n",505clk->name, clk->parent->name, clk->rate);506507return 0;508}509510511