clk-pll.c 6.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * 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>
37
#include <linux/math64.h>
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <linux/module.h>
#include <linux/of_address.h>
#include <linux/slab.h>
#include <linux/string.h>

#include "clkc.h"

#define MESON_PLL_RESET				BIT(29)
#define MESON_PLL_LOCK				BIT(31)

#define to_meson_clk_pll(_hw) container_of(_hw, struct meson_clk_pll, hw)

static unsigned long meson_clk_pll_recalc_rate(struct clk_hw *hw,
						unsigned long parent_rate)
{
	struct meson_clk_pll *pll = to_meson_clk_pll(hw);
	struct parm *p;
55
	u64 rate;
56
	u16 n, m, frac = 0, od, od2 = 0, od3 = 0;
57 58
	u32 reg;

59
	p = &pll->n;
60 61 62
	reg = readl(pll->base + p->reg_off);
	n = PARM_GET(p->width, p->shift, reg);

63
	p = &pll->m;
64 65 66
	reg = readl(pll->base + p->reg_off);
	m = PARM_GET(p->width, p->shift, reg);

67
	p = &pll->od;
68 69 70
	reg = readl(pll->base + p->reg_off);
	od = PARM_GET(p->width, p->shift, reg);

71 72 73 74 75 76
	p = &pll->od2;
	if (p->width) {
		reg = readl(pll->base + p->reg_off);
		od2 = PARM_GET(p->width, p->shift, reg);
	}

77 78 79 80 81 82 83
	p = &pll->od3;
	if (p->width) {
		reg = readl(pll->base + p->reg_off);
		od3 = PARM_GET(p->width, p->shift, reg);
	}

	rate = (u64)m * parent_rate;
84

85 86 87 88
	p = &pll->frac;
	if (p->width) {
		reg = readl(pll->base + p->reg_off);
		frac = PARM_GET(p->width, p->shift, reg);
89

90
		rate += mul_u64_u32_shr(parent_rate, frac, p->width);
91 92
	}

93
	return div_u64(rate, n) >> od >> od2 >> od3;
94 95 96 97 98 99
}

static long meson_clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
				     unsigned long *parent_rate)
{
	struct meson_clk_pll *pll = to_meson_clk_pll(hw);
100
	const struct pll_rate_table *rate_table = pll->rate_table;
101 102
	int i;

103 104 105 106 107 108 109
	/*
	 * if the table is missing, just return the current rate
	 * since we don't have the other available frequencies
	 */
	if (!rate_table)
		return meson_clk_pll_recalc_rate(hw, *parent_rate);

110 111 112 113 114 115 116 117 118 119 120 121
	for (i = 0; i < pll->rate_count; i++) {
		if (rate <= rate_table[i].rate)
			return rate_table[i].rate;
	}

	/* else return the smallest value */
	return rate_table[0].rate;
}

static const struct pll_rate_table *meson_clk_get_pll_settings(struct meson_clk_pll *pll,
							       unsigned long rate)
{
122
	const struct pll_rate_table *rate_table = pll->rate_table;
123 124
	int i;

125 126 127
	if (!rate_table)
		return NULL;

128 129 130 131 132 133 134
	for (i = 0; i < pll->rate_count; i++) {
		if (rate == rate_table[i].rate)
			return &rate_table[i];
	}
	return NULL;
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/* Specific wait loop for GXL/GXM GP0 PLL */
static int meson_clk_pll_wait_lock_reset(struct meson_clk_pll *pll,
					 struct parm *p_n)
{
	int delay = 100;
	u32 reg;

	while (delay > 0) {
		reg = readl(pll->base + p_n->reg_off);
		writel(reg | MESON_PLL_RESET, pll->base + p_n->reg_off);
		udelay(10);
		writel(reg & ~MESON_PLL_RESET, pll->base + p_n->reg_off);

		/* This delay comes from AMLogic tree clk-gp0-gxl driver */
		mdelay(1);

		reg = readl(pll->base + p_n->reg_off);
		if (reg & MESON_PLL_LOCK)
			return 0;
		delay--;
	}
	return -ETIMEDOUT;
}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
static int meson_clk_pll_wait_lock(struct meson_clk_pll *pll,
				   struct parm *p_n)
{
	int delay = 24000000;
	u32 reg;

	while (delay > 0) {
		reg = readl(pll->base + p_n->reg_off);

		if (reg & MESON_PLL_LOCK)
			return 0;
		delay--;
	}
	return -ETIMEDOUT;
}

175 176 177 178 179 180 181 182 183
static void meson_clk_pll_init_params(struct meson_clk_pll *pll)
{
	int i;

	for (i = 0 ; i < pll->params.params_count ; ++i)
		writel(pll->params.params_table[i].value,
		       pll->base + pll->params.params_table[i].reg_off);
}

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
static int meson_clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
				  unsigned long parent_rate)
{
	struct meson_clk_pll *pll = to_meson_clk_pll(hw);
	struct parm *p;
	const struct pll_rate_table *rate_set;
	unsigned long old_rate;
	int ret = 0;
	u32 reg;

	if (parent_rate == 0 || rate == 0)
		return -EINVAL;

	old_rate = rate;

	rate_set = meson_clk_get_pll_settings(pll, rate);
	if (!rate_set)
		return -EINVAL;

203 204 205 206
	/* Initialize the PLL in a clean state if specified */
	if (pll->params.params_count)
		meson_clk_pll_init_params(pll);

207
	/* PLL reset */
208
	p = &pll->n;
209
	reg = readl(pll->base + p->reg_off);
210 211 212
	/* If no_init_reset is provided, avoid resetting at this point */
	if (!pll->params.no_init_reset)
		writel(reg | MESON_PLL_RESET, pll->base + p->reg_off);
213 214 215 216

	reg = PARM_SET(p->width, p->shift, reg, rate_set->n);
	writel(reg, pll->base + p->reg_off);

217
	p = &pll->m;
218 219 220 221
	reg = readl(pll->base + p->reg_off);
	reg = PARM_SET(p->width, p->shift, reg, rate_set->m);
	writel(reg, pll->base + p->reg_off);

222
	p = &pll->od;
223 224 225 226
	reg = readl(pll->base + p->reg_off);
	reg = PARM_SET(p->width, p->shift, reg, rate_set->od);
	writel(reg, pll->base + p->reg_off);

227 228 229 230 231 232 233
	p = &pll->od2;
	if (p->width) {
		reg = readl(pll->base + p->reg_off);
		reg = PARM_SET(p->width, p->shift, reg, rate_set->od2);
		writel(reg, pll->base + p->reg_off);
	}

234 235 236 237 238 239 240
	p = &pll->od3;
	if (p->width) {
		reg = readl(pll->base + p->reg_off);
		reg = PARM_SET(p->width, p->shift, reg, rate_set->od3);
		writel(reg, pll->base + p->reg_off);
	}

241 242 243 244 245 246 247
	p = &pll->frac;
	if (p->width) {
		reg = readl(pll->base + p->reg_off);
		reg = PARM_SET(p->width, p->shift, reg, rate_set->frac);
		writel(reg, pll->base + p->reg_off);
	}

248
	p = &pll->n;
249 250 251 252 253 254 255 256 257 258 259
	/* If clear_reset_for_lock is provided, remove the reset bit here */
	if (pll->params.clear_reset_for_lock) {
		reg = readl(pll->base + p->reg_off);
		writel(reg & ~MESON_PLL_RESET, pll->base + p->reg_off);
	}

	/* If reset_lock_loop, use a special loop including resetting */
	if (pll->params.reset_lock_loop)
		ret = meson_clk_pll_wait_lock_reset(pll, p);
	else
		ret = meson_clk_pll_wait_lock(pll, p);
260 261 262 263 264 265 266 267 268
	if (ret) {
		pr_warn("%s: pll did not lock, trying to restore old rate %lu\n",
			__func__, old_rate);
		meson_clk_pll_set_rate(hw, old_rate, parent_rate);
	}

	return ret;
}

269
const struct clk_ops meson_clk_pll_ops = {
270 271 272 273 274
	.recalc_rate	= meson_clk_pll_recalc_rate,
	.round_rate	= meson_clk_pll_round_rate,
	.set_rate	= meson_clk_pll_set_rate,
};

275
const struct clk_ops meson_clk_pll_ro_ops = {
276 277
	.recalc_rate	= meson_clk_pll_recalc_rate,
};