/*
 * (C) Copyright 2001
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <ppcboot.h>
#include <mpc8240.h>
#include <asm/processor.h>

static char pll_to_bus_factor[] =
{
	3, 0, 0, 0, 0,
	0, 0, 0, 1, 0,
	0, 0, 2, 0, 2,
	0, 3, 0, 0, 0,
	2, 0, 0, 0
};

#define PLL_STATE_REG 0xFE80002F

ulong get_bus_freq(ulong ignore)
{
	unsigned char pll = *(volatile char *) (PLL_STATE_REG);

	if (pll == 24)
	{
		return CONFIG_SYS_CLK_FREQ * 5 / 2;
	}
	else
	{
		return CONFIG_SYS_CLK_FREQ * pll_to_bus_factor[pll];
	}
}

/* ------------------------------------------------------------------------- */

/*
 * Measure CPU clock speed
 */

/* Table to convert pllratio to actual processor clock scaling factor (*10)
 */
#ifdef CONFIG_MPC8240
short pllratio_to_factor[] = {
    00, 00, 00, 10, 20, 20, 25, 00, 00, 00, 00, 00, 00, 00, 00, 00,
    00, 00, 00, 10, 00, 00, 00, 45, 30, 00, 40, 00, 00, 00, 35, 00,
};
#endif

ulong get_gclk_freq(void)
{
	uint hid1 = mfspr(HID1);

#ifdef CONFIG_MPC8240

		/* 5 bits for PLL ration on 8240
		 */
	hid1 = (hid1 >> (32-5)) & 0x1f;
#else

		/* 4 bits on everythings else
		 */
	hid1 = (hid1 >> (32-4)) & 0xf;
#endif

	return (pllratio_to_factor[hid1] * get_bus_freq(0)) / 10;
}

/* ------------------------------------------------------------------------- */

