/*
* arch/sh/kernel/cpu/sh4a/clock-sh7722.c
*
* SH7343, SH7722 & SH7366 support for the clock framework
*
* Copyright (c) 2006-2007 Nomad Global Solutions Inc
* Based on code for sh7343 by Paul Mundt
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file "COPYING" in the main directory of this archive
* for more details.
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/io.h>
#include <linux/errno.h>
#include <linux/stringify.h>
#include <asm/clock.h>
#include <asm/freq.h>
#define N (-1)
#define NM (-2)
#define ROUND_NEAREST 0
#define ROUND_DOWN -1
#define ROUND_UP +1
static int adjust_algos[][3] = {
{}, /* NO_CHANGE */
{ NM, N, 1 }, /* N:1, N:1 */
{ 3, 2, 2 }, /* 3:2:2 */
{ 5, 2, 2 }, /* 5:2:2 */
{ N, 1, 1 }, /* N:1:1 */
{ N, 1 }, /* N:1 */
{ N, 1 }, /* N:1 */
{ 3, 2 },
{ 4, 3 },
{ 5, 4 },
{ N, 1 }
};
static unsigned long adjust_pair_of_clocks(unsigned long r1, unsigned long r2,
int m1, int m2, int round_flag)
{
unsigned long rem, div;
int the_one = 0;
pr_debug( "Actual values: r1 = %ld\n", r1);
pr_debug( "...............r2 = %ld\n", r2);
if (m1 == m2) {
r2 = r1;
pr_debug( "setting equal rates: r2 now %ld\n", r2);
} else if ((m2 == N && m1 == 1) ||
(m2 == NM && m1 == N)) { /* N:1 or NM:N */
pr_debug( "Setting rates as 1:N (N:N*M)\n");
rem = r2 % r1;
pr_debug( "...remainder = %ld\n", rem);
if (rem) {
div = r2 / r1;
pr_debug( "...div = %ld\n", div);
switch (round_flag) {
case ROUND_NEAREST:
the_one = rem >= r1/2 ? 1 : 0; break;
case ROUND_UP:
the_one = 1; break;
case ROUND_DOWN:
the_one = 0; break;
}
r2 = r1 * (div + the_one);
pr_debug( "...setting r2 to %ld\n", r2);
}
} else if ((m2 == 1 && m1 == N) ||
(m2 == N && m1 == NM)) { /* 1:N or N:NM */
pr_debug( "Setting rates as N:1 (N*M:N)\n");
rem = r1 % r2;
pr_debug( "...remainder = %ld\n", rem);
if (rem) {
div = r1 / r2;
pr_debug( "...div = %ld\n", div);
switch (round_flag) {
case ROUND_NEAREST:
the_one = rem > r2/2 ? 1 : 0; break;
case ROUND_UP:
the_one = 0; break;
case ROUND_DOWN:
the_one = 1; break;
}
r2 = r1 / (div + the_one);
pr_debug( "...setting r2 to %ld\n", r2);
}
} else { /* value:value */
pr_debug( "Setting rates as %d:%d\n", m1, m2);
div = r1 / m1;
r2 = div * m2;
pr_debug( "...div = %ld\n", div);
pr_debug( "...setting r2 to %ld\n", r2);
}
return r2;
}
static void adjust_clocks(int originate, int *l, unsigned long v[],
int n_in_line)
{
int x;
pr_debug( "Go down from %d...\n", originate);
/* go up recalculation clocks */
for (x = originate; x>0; x -- )
v[x-1] = adjust_pair_of_clocks(v[x], v[x-1],
l[x], l[x-1],
ROUND_UP);
pr_debug( "Go up from %d...\n", originate);
/* go down recalculation clocks */
for (x = originate; x<n_in_line - 1; x ++ )
v[x+1] = adjust_pair_of_clocks(v[x], v[x+1],
l[x], l[x+1],
ROUND_UP);
}
/*
* SH7722 uses a common set of multipliers and divisors, so this
* is quite simple..
*/
#if defined(CONFIG_CPU_SUBTYPE_SH7724)
#define STCPLL(frqcr) ((((frqcr >> 24) & 0x3f) + 1) * 2)
#else
#define STCPLL(frqcr) (((frqcr >> 24) & 0x1f) + 1)
#endif
/*
* Instead of having two separate multipliers/divisors set, like this:
*
* static int multipliers[] = { 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
* static int divisors[] = { 1, 3, 2, 5, 3, 4, 5, 6, 8, 10, 12, 16, 20 };
*
* I created the divisors2 array, which is used to calculate rate like
* rate = parent * 2 / divisors2[ divisor ];
*/
#if defined(CONFIG_CPU_SUBTYPE_SH7724)
static int divisors2[] = { 4, 1, 8, 12, 16, 24, 32, 1, 48, 64, 72, 96, 1, 144