/*
* Copyright (C) 2014 Broadcom Corporation
*
* 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 version 2.
*
* This program is distributed "as is" WITHOUT ANY WARRANTY of any
* kind, whether express or implied; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/kernel.h>
#include <linux/err.h>
#include <linux/clk-provider.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/clkdev.h>
#include <linux/of_address.h>
#include <linux/delay.h>
#include "clk-iproc.h"
#define PLL_VCO_HIGH_SHIFT 19
#define PLL_VCO_LOW_SHIFT 30
/*
* PLL MACRO_SELECT modes 0 to 5 choose pre-calculated PLL output frequencies
* from a look-up table. Mode 7 allows user to manipulate PLL clock dividers
*/
#define PLL_USER_MODE 7
/* number of delay loops waiting for PLL to lock */
#define LOCK_DELAY 100
/* number of VCO frequency bands */
#define NUM_FREQ_BANDS 8
#define NUM_KP_BANDS 3
enum kp_band {
KP_BAND_MID = 0,
KP_BAND_HIGH,
KP_BAND_HIGH_HIGH
};
static const unsigned int kp_table[NUM_KP_BANDS][NUM_FREQ_BANDS] = {
{ 5, 6, 6, 7, 7, 8, 9, 10 },
{ 4, 4, 5, 5, 6, 7, 8, 9 },
{ 4, 5, 5, 6, 7, 8, 9, 10 },
};
static const unsigned long ref_freq_table[NUM_FREQ_BANDS][2] = {
{ 10000000, 12500000 },
{ 12500000, 15000000 },
{ 15000000, 20000000 },
{ 20000000, 25000000 },
{ 25000000, 50000000 },
{ 50000000, 75000000 },
{ 75000000, 100000000 },
{ 100000000, 125000000 },
};
enum vco_freq_range {
VCO_LOW = 700000000U,
VCO_MID = 1200000000U,
VCO_HIGH = 2200000000U,
VCO_HIGH_HIGH = 3100000000U,
VCO_MAX = 4000000000U,
};
struct iproc_pll {
void __iomem *status_base;
void __iomem *control_base;
void __iomem *pwr_base;
void __iomem *asiu_base;
const struct iproc_pll_ctrl *ctrl;
const struct iproc_pll_vco_param *vco_param;
unsigned int num_vco_entries;
};
struct iproc_clk {
struct clk_hw hw;
struct iproc_pll *pll;
const struct iproc_clk_ctrl *ctrl;
};
#define to_iproc_clk(hw) container_of(hw, struct iproc_clk, hw)
static int pll_calc_param(unsigned long target_rate,
unsigned long parent_rate,
struct iproc_pll_vco_param *vco_out)
{
u64 ndiv_int, ndiv_frac, residual;
ndiv_int = target_rate / parent_rate;
if (!ndiv_int || (ndiv_int > 255))
return -EINVAL;
residual = target_rate - (ndiv_int * parent_rate);
residual <<= 20;
/*
* Add half of the divisor so the result will be rounded to closest
* instead of rounded down.
*/
residual += (parent_rate / 2);
ndiv_frac = div64_u64((u64)residual, (u64)parent_rate);
vco_out->ndiv_int = ndiv_int;
vco_out->ndiv_frac = ndiv_frac;
vco_out->pdiv = 1;
vco_out->rate = vco_out->ndiv_int * parent_rate;
residual = (u64)vco_out->ndiv_frac * (u64)parent_rate;
residual >>= 20;
vco_out->rate += residual;
return 0;
}
/*
* Based on the target frequency, find a match from the VCO frequency parameter
* table and return its index
*/
static int pll_get_rate_index(struct iproc_pll *pll, unsigned int target_rate)
{
int i;
for (i = 0; i < pll->num_vco_entries; i++)
if (target_rate == pll->vco_param[i].rate)
break;
if (i >= pll->num_vco_entries)
return -EINVAL;
return i;
}
static int get_kp(unsigned long ref_freq, enum kp_band kp_index)
{
int i;
if (ref_freq < ref_freq_table[0][0])
return -EINVAL;
for (i = 0; i < NUM_FREQ_BANDS; i++) {
if (ref_freq >= ref_freq_table[i][0] &&
ref_freq < ref_freq_table[i][1])
return kp_table[kp_index][i];
}
return -EINVAL;
}
static int pll_wait_for_lock(struct iproc_pll *pll)
{
int i;
const struct iproc_pll_ctrl *ctrl = pll->ctrl;
for (i = 0; i < LOCK_DELAY; i++) {
u32 val = readl(pll->status_base + ctrl->status.offset);
if (val & (1 << ctrl->status.shift))
return 0;
udelay(10);
}
return -EIO;
}
static void iproc_pll_write(const struct iproc_pll *pll, void __iomem *base,
const u32 offset, u32 val)
{
const struct iproc_pll_ctrl *ctrl = pll->ctrl;
writel(val,