2015-06-01 11:13:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Endless Mobile, Inc.
|
|
|
|
* Author: Carlo Caione <carlo@endlessm.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In the most basic form, a Meson PLL is composed as follows:
|
|
|
|
*
|
|
|
|
* PLL
|
|
|
|
* +------------------------------+
|
|
|
|
* | |
|
|
|
|
* in -----[ /N ]---[ *M ]---[ >>OD ]----->> out
|
|
|
|
* | ^ ^ |
|
|
|
|
* +------------------------------+
|
|
|
|
* | |
|
|
|
|
* FREF VCO
|
|
|
|
*
|
|
|
|
* out = (in * M / N) >> OD
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk-provider.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/io.h>
|
2018-01-19 15:55:23 +00:00
|
|
|
#include <linux/math64.h>
|
2015-06-01 11:13:53 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of_address.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
|
|
|
|
#include "clkc.h"
|
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
static inline struct meson_clk_pll_data *
|
|
|
|
meson_clk_pll_data(struct clk_regmap *clk)
|
|
|
|
{
|
|
|
|
return (struct meson_clk_pll_data *)clk->data;
|
|
|
|
}
|
2015-06-01 11:13:53 +00:00
|
|
|
|
|
|
|
static unsigned long meson_clk_pll_recalc_rate(struct clk_hw *hw,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
2018-02-12 14:58:42 +00:00
|
|
|
struct clk_regmap *clk = to_clk_regmap(hw);
|
|
|
|
struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
|
2018-01-19 15:55:23 +00:00
|
|
|
u64 rate;
|
2018-01-19 15:55:25 +00:00
|
|
|
u16 n, m, frac = 0, od, od2 = 0, od3 = 0;
|
2015-06-01 11:13:53 +00:00
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
n = meson_parm_read(clk->map, &pll->n);
|
|
|
|
m = meson_parm_read(clk->map, &pll->m);
|
|
|
|
od = meson_parm_read(clk->map, &pll->od);
|
2015-06-01 11:13:53 +00:00
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
if (MESON_PARM_APPLICABLE(&pll->od2))
|
|
|
|
od2 = meson_parm_read(clk->map, &pll->od2);
|
2015-06-01 11:13:53 +00:00
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
if (MESON_PARM_APPLICABLE(&pll->od3))
|
|
|
|
od3 = meson_parm_read(clk->map, &pll->od3);
|
2018-01-19 15:55:25 +00:00
|
|
|
|
|
|
|
rate = (u64)m * parent_rate;
|
2018-01-19 15:55:23 +00:00
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
if (MESON_PARM_APPLICABLE(&pll->frac)) {
|
|
|
|
frac = meson_parm_read(clk->map, &pll->frac);
|
2015-06-01 11:13:53 +00:00
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
rate += mul_u64_u32_shr(parent_rate, frac, pll->frac.width);
|
2018-01-19 15:55:23 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 15:55:25 +00:00
|
|
|
return div_u64(rate, n) >> od >> od2 >> od3;
|
2015-06-01 11:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static long meson_clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long *parent_rate)
|
|
|
|
{
|
2018-02-12 14:58:42 +00:00
|
|
|
struct clk_regmap *clk = to_clk_regmap(hw);
|
|
|
|
struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
|
|
|
|
const struct pll_rate_table *pllt;
|
2015-06-01 11:13:53 +00:00
|
|
|
|
2018-01-19 15:55:21 +00:00
|
|
|
/*
|
|
|
|
* if the table is missing, just return the current rate
|
|
|
|
* since we don't have the other available frequencies
|
|
|
|
*/
|
2018-02-12 14:58:42 +00:00
|
|
|
if (!pll->table)
|
2018-01-19 15:55:21 +00:00
|
|
|
return meson_clk_pll_recalc_rate(hw, *parent_rate);
|
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
for (pllt = pll->table; pllt->rate; pllt++) {
|
|
|
|
if (rate <= pllt->rate)
|
|
|
|
return pllt->rate;
|
2015-06-01 11:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* else return the smallest value */
|
2018-02-12 14:58:42 +00:00
|
|
|
return pll->table[0].rate;
|
2015-06-01 11:13:53 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
static const struct pll_rate_table *
|
|
|
|
meson_clk_get_pll_settings(const struct pll_rate_table *table,
|
|
|
|
unsigned long rate)
|
2015-06-01 11:13:53 +00:00
|
|
|
{
|
2018-02-12 14:58:42 +00:00
|
|
|
const struct pll_rate_table *pllt;
|
2015-06-01 11:13:53 +00:00
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
if (!table)
|
2018-01-19 15:55:21 +00:00
|
|
|
return NULL;
|
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
for (pllt = table; pllt->rate; pllt++) {
|
|
|
|
if (rate == pllt->rate)
|
|
|
|
return pllt;
|
2015-06-01 11:13:53 +00:00
|
|
|
}
|
2018-02-12 14:58:42 +00:00
|
|
|
|
2015-06-01 11:13:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
static int meson_clk_pll_wait_lock(struct clk_hw *hw)
|
2017-03-22 10:32:23 +00:00
|
|
|
{
|
2018-02-12 14:58:42 +00:00
|
|
|
struct clk_regmap *clk = to_clk_regmap(hw);
|
|
|
|
struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
|
|
|
|
int delay = pll->flags & CLK_MESON_PLL_LOCK_LOOP_RST ?
|
|
|
|
100 : 24000000;
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* Specific wait loop for GXL/GXM GP0 PLL */
|
|
|
|
if (pll->flags & CLK_MESON_PLL_LOCK_LOOP_RST) {
|
|
|
|
/* Procedure taken from the vendor kernel */
|
|
|
|
meson_parm_write(clk->map, &pll->rst, 1);
|
|
|
|
udelay(10);
|
|
|
|
meson_parm_write(clk->map, &pll->rst, 0);
|
|
|
|
mdelay(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the clock locked now ? */
|
|
|
|
if (meson_parm_read(clk->map, &pll->l))
|
2017-03-22 10:32:23 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-06-01 11:13:53 +00:00
|
|
|
delay--;
|
2018-02-12 14:58:42 +00:00
|
|
|
} while (delay > 0);
|
|
|
|
|
2015-06-01 11:13:53 +00:00
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
static void meson_clk_pll_init(struct clk_hw *hw)
|
2017-03-22 10:32:23 +00:00
|
|
|
{
|
2018-02-12 14:58:42 +00:00
|
|
|
struct clk_regmap *clk = to_clk_regmap(hw);
|
|
|
|
struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
|
|
|
|
|
|
|
|
if (pll->init_count) {
|
|
|
|
meson_parm_write(clk->map, &pll->rst, 1);
|
|
|
|
regmap_multi_reg_write(clk->map, pll->init_regs,
|
|
|
|
pll->init_count);
|
|
|
|
meson_parm_write(clk->map, &pll->rst, 0);
|
|
|
|
}
|
2017-03-22 10:32:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 11:13:53 +00:00
|
|
|
static int meson_clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
2018-02-12 14:58:42 +00:00
|
|
|
struct clk_regmap *clk = to_clk_regmap(hw);
|
|
|
|
struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
|
|
|
|
const struct pll_rate_table *pllt;
|
2015-06-01 11:13:53 +00:00
|
|
|
unsigned long old_rate;
|
|
|
|
|
|
|
|
if (parent_rate == 0 || rate == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
old_rate = rate;
|
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
pllt = meson_clk_get_pll_settings(pll->table, rate);
|
|
|
|
if (!pllt)
|
2015-06-01 11:13:53 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
/* Put the pll in reset to write the params */
|
|
|
|
meson_parm_write(clk->map, &pll->rst, 1);
|
2016-06-07 01:08:15 +00:00
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
meson_parm_write(clk->map, &pll->n, pllt->n);
|
|
|
|
meson_parm_write(clk->map, &pll->m, pllt->m);
|
|
|
|
meson_parm_write(clk->map, &pll->od, pllt->od);
|
2018-01-19 15:55:25 +00:00
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
if (MESON_PARM_APPLICABLE(&pll->od2))
|
|
|
|
meson_parm_write(clk->map, &pll->od2, pllt->od2);
|
2016-06-07 01:08:15 +00:00
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
if (MESON_PARM_APPLICABLE(&pll->od3))
|
|
|
|
meson_parm_write(clk->map, &pll->od3, pllt->od3);
|
|
|
|
|
|
|
|
if (MESON_PARM_APPLICABLE(&pll->frac))
|
|
|
|
meson_parm_write(clk->map, &pll->frac, pllt->frac);
|
|
|
|
|
|
|
|
/* make sure the reset is cleared at this point */
|
|
|
|
meson_parm_write(clk->map, &pll->rst, 0);
|
2017-03-22 10:32:23 +00:00
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
if (meson_clk_pll_wait_lock(hw)) {
|
2015-06-01 11:13:53 +00:00
|
|
|
pr_warn("%s: pll did not lock, trying to restore old rate %lu\n",
|
|
|
|
__func__, old_rate);
|
2018-02-12 14:58:42 +00:00
|
|
|
/*
|
|
|
|
* FIXME: Do we really need/want this HACK ?
|
|
|
|
* It looks unsafe. what happens if the clock gets into a
|
|
|
|
* broken state and we can't lock back on the old_rate ? Looks
|
|
|
|
* like an infinite recursion is possible
|
|
|
|
*/
|
2015-06-01 11:13:53 +00:00
|
|
|
meson_clk_pll_set_rate(hw, old_rate, parent_rate);
|
|
|
|
}
|
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
return 0;
|
2015-06-01 11:13:53 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 19:01:42 +00:00
|
|
|
const struct clk_ops meson_clk_pll_ops = {
|
2018-02-12 14:58:42 +00:00
|
|
|
.init = meson_clk_pll_init,
|
2015-06-01 11:13:53 +00:00
|
|
|
.recalc_rate = meson_clk_pll_recalc_rate,
|
|
|
|
.round_rate = meson_clk_pll_round_rate,
|
|
|
|
.set_rate = meson_clk_pll_set_rate,
|
|
|
|
};
|
|
|
|
|
2016-04-28 19:01:42 +00:00
|
|
|
const struct clk_ops meson_clk_pll_ro_ops = {
|
2015-06-01 11:13:53 +00:00
|
|
|
.recalc_rate = meson_clk_pll_recalc_rate,
|
|
|
|
};
|