2018-05-16 08:50:40 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-06-01 11:13:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Endless Mobile, Inc.
|
|
|
|
* Author: Carlo Caione <carlo@endlessm.com>
|
|
|
|
*
|
2018-02-19 11:21:39 +00:00
|
|
|
* Copyright (c) 2018 Baylibre, SAS.
|
|
|
|
* Author: Jerome Brunet <jbrunet@baylibre.com>
|
2015-06-01 11:13:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In the most basic form, a Meson PLL is composed as follows:
|
|
|
|
*
|
|
|
|
* PLL
|
2018-08-01 14:00:52 +00:00
|
|
|
* +--------------------------------+
|
|
|
|
* | |
|
|
|
|
* | +--+ |
|
|
|
|
* in >>-----[ /N ]--->| | +-----+ |
|
|
|
|
* | | |------| DCO |---->> out
|
|
|
|
* | +--------->| | +--v--+ |
|
|
|
|
* | | +--+ | |
|
|
|
|
* | | | |
|
|
|
|
* | +--[ *(M + (F/Fmax) ]<--+ |
|
|
|
|
* | |
|
|
|
|
* +--------------------------------+
|
2015-06-01 11:13:53 +00:00
|
|
|
*
|
2018-08-01 14:00:52 +00:00
|
|
|
* out = in * (m + frac / frac_max) / n
|
2015-06-01 11:13:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
2018-02-19 11:21:39 +00:00
|
|
|
static unsigned long __pll_params_to_rate(unsigned long parent_rate,
|
|
|
|
const struct pll_rate_table *pllt,
|
|
|
|
u16 frac,
|
|
|
|
struct meson_clk_pll_data *pll)
|
|
|
|
{
|
|
|
|
u64 rate = (u64)parent_rate * pllt->m;
|
|
|
|
|
|
|
|
if (frac && MESON_PARM_APPLICABLE(&pll->frac)) {
|
|
|
|
u64 frac_rate = (u64)parent_rate * frac;
|
|
|
|
|
|
|
|
rate += DIV_ROUND_UP_ULL(frac_rate,
|
|
|
|
(1 << pll->frac.width));
|
|
|
|
}
|
|
|
|
|
2018-08-01 14:00:52 +00:00
|
|
|
return DIV_ROUND_UP_ULL(rate, pllt->n);
|
2018-02-19 11:21:39 +00:00
|
|
|
}
|
|
|
|
|
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-02-19 11:21:39 +00:00
|
|
|
struct pll_rate_table pllt;
|
|
|
|
u16 frac;
|
2015-06-01 11:13:53 +00:00
|
|
|
|
2018-02-19 11:21:39 +00:00
|
|
|
pllt.n = meson_parm_read(clk->map, &pll->n);
|
|
|
|
pllt.m = meson_parm_read(clk->map, &pll->m);
|
2015-06-01 11:13:53 +00:00
|
|
|
|
2018-02-19 11:21:39 +00:00
|
|
|
frac = MESON_PARM_APPLICABLE(&pll->frac) ?
|
|
|
|
meson_parm_read(clk->map, &pll->frac) :
|
|
|
|
0;
|
2018-01-19 15:55:23 +00:00
|
|
|
|
2018-02-19 11:21:39 +00:00
|
|
|
return __pll_params_to_rate(parent_rate, &pllt, frac, pll);
|
2015-06-01 11:13:53 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 11:21:39 +00:00
|
|
|
static u16 __pll_params_with_frac(unsigned long rate,
|
|
|
|
unsigned long parent_rate,
|
|
|
|
const struct pll_rate_table *pllt,
|
|
|
|
struct meson_clk_pll_data *pll)
|
2015-06-01 11:13:53 +00:00
|
|
|
{
|
2018-02-19 11:21:39 +00:00
|
|
|
u16 frac_max = (1 << pll->frac.width);
|
|
|
|
u64 val = (u64)rate * pllt->n;
|
2018-01-19 15:55:21 +00:00
|
|
|
|
2018-02-19 11:21:41 +00:00
|
|
|
if (pll->flags & CLK_MESON_PLL_ROUND_CLOSEST)
|
|
|
|
val = DIV_ROUND_CLOSEST_ULL(val * frac_max, parent_rate);
|
|
|
|
else
|
|
|
|
val = div_u64(val * frac_max, parent_rate);
|
|
|
|
|
2018-02-19 11:21:39 +00:00
|
|
|
val -= pllt->m * frac_max;
|
2015-06-01 11:13:53 +00:00
|
|
|
|
2018-02-19 11:21:39 +00:00
|
|
|
return min((u16)val, (u16)(frac_max - 1));
|
2015-06-01 11:13:53 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
static const struct pll_rate_table *
|
2018-02-19 11:21:39 +00:00
|
|
|
meson_clk_get_pll_settings(unsigned long rate,
|
|
|
|
struct meson_clk_pll_data *pll)
|
2015-06-01 11:13:53 +00:00
|
|
|
{
|
2018-02-19 11:21:39 +00:00
|
|
|
const struct pll_rate_table *table = pll->table;
|
|
|
|
unsigned int i = 0;
|
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-19 11:21:39 +00:00
|
|
|
/* Find the first table element exceeding rate */
|
|
|
|
while (table[i].rate && table[i].rate <= rate)
|
|
|
|
i++;
|
|
|
|
|
2018-02-19 11:21:41 +00:00
|
|
|
if (i != 0) {
|
|
|
|
if (MESON_PARM_APPLICABLE(&pll->frac) ||
|
|
|
|
!(pll->flags & CLK_MESON_PLL_ROUND_CLOSEST) ||
|
|
|
|
(abs(rate - table[i - 1].rate) <
|
|
|
|
abs(rate - table[i].rate)))
|
|
|
|
i--;
|
|
|
|
}
|
2018-02-19 11:21:39 +00:00
|
|
|
|
|
|
|
return (struct pll_rate_table *)&table[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
static long meson_clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long *parent_rate)
|
|
|
|
{
|
|
|
|
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 =
|
|
|
|
meson_clk_get_pll_settings(rate, pll);
|
|
|
|
u16 frac;
|
|
|
|
|
|
|
|
if (!pllt)
|
|
|
|
return meson_clk_pll_recalc_rate(hw, *parent_rate);
|
|
|
|
|
|
|
|
if (!MESON_PARM_APPLICABLE(&pll->frac)
|
|
|
|
|| rate == pllt->rate)
|
|
|
|
return pllt->rate;
|
2018-02-12 14:58:42 +00:00
|
|
|
|
2018-02-19 11:21:39 +00:00
|
|
|
/*
|
|
|
|
* The rate provided by the setting is not an exact match, let's
|
|
|
|
* try to improve the result using the fractional parameter
|
|
|
|
*/
|
|
|
|
frac = __pll_params_with_frac(rate, *parent_rate, pllt, pll);
|
|
|
|
|
|
|
|
return __pll_params_to_rate(*parent_rate, pllt, frac, pll);
|
2015-06-01 11:13:53 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2018-02-19 11:21:38 +00:00
|
|
|
int delay = 24000000;
|
2018-02-12 14:58:42 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
2018-08-01 14:00:50 +00:00
|
|
|
static int meson_clk_pll_enable(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_regmap *clk = to_clk_regmap(hw);
|
|
|
|
struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
|
|
|
|
|
|
|
|
/* Make sure the pll is in reset */
|
|
|
|
meson_parm_write(clk->map, &pll->rst, 1);
|
|
|
|
|
|
|
|
/* Enable the pll */
|
|
|
|
meson_parm_write(clk->map, &pll->en, 1);
|
|
|
|
|
|
|
|
/* Take the pll out reset */
|
|
|
|
meson_parm_write(clk->map, &pll->rst, 0);
|
|
|
|
|
|
|
|
if (meson_clk_pll_wait_lock(hw))
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void meson_clk_pll_disable(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_regmap *clk = to_clk_regmap(hw);
|
|
|
|
struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
|
|
|
|
|
|
|
|
/* Put the pll is in reset */
|
|
|
|
meson_parm_write(clk->map, &pll->rst, 1);
|
|
|
|
|
|
|
|
/* Disable the pll */
|
|
|
|
meson_parm_write(clk->map, &pll->en, 0);
|
|
|
|
}
|
|
|
|
|
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;
|
2018-08-01 14:00:50 +00:00
|
|
|
unsigned int enabled;
|
2015-06-01 11:13:53 +00:00
|
|
|
unsigned long old_rate;
|
2018-02-19 11:21:39 +00:00
|
|
|
u16 frac = 0;
|
2015-06-01 11:13:53 +00:00
|
|
|
|
|
|
|
if (parent_rate == 0 || rate == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
old_rate = rate;
|
|
|
|
|
2018-02-19 11:21:39 +00:00
|
|
|
pllt = meson_clk_get_pll_settings(rate, pll);
|
2018-02-12 14:58:42 +00:00
|
|
|
if (!pllt)
|
2015-06-01 11:13:53 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2018-08-01 14:00:50 +00:00
|
|
|
enabled = meson_parm_read(clk->map, &pll->en);
|
|
|
|
if (enabled)
|
|
|
|
meson_clk_pll_disable(hw);
|
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);
|
2016-06-07 01:08:15 +00:00
|
|
|
|
2018-02-12 14:58:42 +00:00
|
|
|
|
2018-02-19 11:21:39 +00:00
|
|
|
if (MESON_PARM_APPLICABLE(&pll->frac)) {
|
|
|
|
frac = __pll_params_with_frac(rate, parent_rate, pllt, pll);
|
|
|
|
meson_parm_write(clk->map, &pll->frac, frac);
|
|
|
|
}
|
2018-02-12 14:58:42 +00:00
|
|
|
|
2018-08-01 14:00:50 +00:00
|
|
|
/* If the pll is stopped, bail out now */
|
|
|
|
if (!enabled)
|
|
|
|
return 0;
|
2017-03-22 10:32:23 +00:00
|
|
|
|
2018-08-01 14:00:50 +00:00
|
|
|
if (meson_clk_pll_enable(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,
|
2018-08-01 14:00:50 +00:00
|
|
|
.enable = meson_clk_pll_enable,
|
|
|
|
.disable = meson_clk_pll_disable
|
2015-06-01 11:13:53 +00:00
|
|
|
};
|
|
|
|
|
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,
|
|
|
|
};
|