2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-02-02 12:11:32 +00:00
|
|
|
/*
|
2017-10-13 10:21:59 +00:00
|
|
|
* Copyright (C) 2016-2017 Socionext Inc.
|
2016-07-19 12:56:13 +00:00
|
|
|
* Author: Masahiro Yamada <yamada.masahiro@socionext.com>
|
2016-02-02 12:11:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2016-10-17 13:18:01 +00:00
|
|
|
#include <clk-uclass.h>
|
2017-05-17 23:18:03 +00:00
|
|
|
#include <dm.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <dm/device_compat.h>
|
2016-02-02 12:11:32 +00:00
|
|
|
#include <linux/bitops.h>
|
2020-05-10 17:40:08 +00:00
|
|
|
#include <linux/bug.h>
|
2016-02-02 12:11:32 +00:00
|
|
|
#include <linux/io.h>
|
2016-03-24 13:32:39 +00:00
|
|
|
#include <linux/sizes.h>
|
2016-02-02 12:11:32 +00:00
|
|
|
|
|
|
|
#include "clk-uniphier.h"
|
|
|
|
|
2016-09-21 22:42:21 +00:00
|
|
|
/**
|
|
|
|
* struct uniphier_clk_priv - private data for UniPhier clock driver
|
|
|
|
*
|
|
|
|
* @base: base address of the clock provider
|
2016-10-17 13:18:01 +00:00
|
|
|
* @data: SoC specific data
|
2016-09-21 22:42:21 +00:00
|
|
|
*/
|
|
|
|
struct uniphier_clk_priv {
|
2017-10-13 10:21:59 +00:00
|
|
|
struct udevice *dev;
|
2016-09-21 22:42:21 +00:00
|
|
|
void __iomem *base;
|
2016-10-17 13:18:01 +00:00
|
|
|
const struct uniphier_clk_data *data;
|
2016-09-21 22:42:21 +00:00
|
|
|
};
|
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
static void uniphier_clk_gate_enable(struct uniphier_clk_priv *priv,
|
|
|
|
const struct uniphier_clk_gate_data *gate)
|
2016-09-21 22:42:21 +00:00
|
|
|
{
|
2017-10-13 10:21:59 +00:00
|
|
|
u32 val;
|
2016-09-21 22:42:21 +00:00
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
val = readl(priv->base + gate->reg);
|
|
|
|
val |= BIT(gate->bit);
|
|
|
|
writel(val, priv->base + gate->reg);
|
|
|
|
}
|
2016-09-21 22:42:21 +00:00
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
static void uniphier_clk_mux_set_parent(struct uniphier_clk_priv *priv,
|
|
|
|
const struct uniphier_clk_mux_data *mux,
|
|
|
|
u8 id)
|
|
|
|
{
|
|
|
|
u32 val;
|
|
|
|
int i;
|
2016-09-21 22:42:21 +00:00
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
for (i = 0; i < mux->num_parents; i++) {
|
|
|
|
if (mux->parent_ids[i] != id)
|
|
|
|
continue;
|
2016-09-21 22:42:21 +00:00
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
val = readl(priv->base + mux->reg);
|
|
|
|
val &= ~mux->masks[i];
|
|
|
|
val |= mux->vals[i];
|
|
|
|
writel(val, priv->base + mux->reg);
|
|
|
|
return;
|
2016-10-17 13:18:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
WARN_ON(1);
|
2016-09-21 22:42:21 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
static u8 uniphier_clk_mux_get_parent(struct uniphier_clk_priv *priv,
|
|
|
|
const struct uniphier_clk_mux_data *mux)
|
2016-02-02 12:11:32 +00:00
|
|
|
{
|
2017-10-13 10:21:59 +00:00
|
|
|
u32 val;
|
|
|
|
int i;
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
val = readl(priv->base + mux->reg);
|
|
|
|
|
|
|
|
for (i = 0; i < mux->num_parents; i++)
|
|
|
|
if ((mux->masks[i] & val) == mux->vals[i])
|
|
|
|
return mux->parent_ids[i];
|
|
|
|
|
|
|
|
dev_err(priv->dev, "invalid mux setting\n");
|
|
|
|
|
|
|
|
return UNIPHIER_CLK_ID_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct uniphier_clk_data *uniphier_clk_get_data(
|
|
|
|
struct uniphier_clk_priv *priv, u8 id)
|
|
|
|
{
|
|
|
|
const struct uniphier_clk_data *data;
|
|
|
|
|
|
|
|
for (data = priv->data; data->type != UNIPHIER_CLK_TYPE_END; data++)
|
|
|
|
if (data->id == id)
|
|
|
|
return data;
|
|
|
|
|
|
|
|
dev_err(priv->dev, "id=%u not found\n", id);
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
return NULL;
|
2016-02-02 12:11:32 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
static const struct uniphier_clk_data *uniphier_clk_get_parent_data(
|
|
|
|
struct uniphier_clk_priv *priv,
|
|
|
|
const struct uniphier_clk_data *data)
|
2016-02-02 12:11:32 +00:00
|
|
|
{
|
2017-10-13 10:21:59 +00:00
|
|
|
const struct uniphier_clk_data *parent_data;
|
|
|
|
u8 parent_id = UNIPHIER_CLK_ID_INVALID;
|
|
|
|
|
|
|
|
switch (data->type) {
|
|
|
|
case UNIPHIER_CLK_TYPE_GATE:
|
|
|
|
parent_id = data->data.gate.parent_id;
|
|
|
|
break;
|
|
|
|
case UNIPHIER_CLK_TYPE_MUX:
|
|
|
|
parent_id = uniphier_clk_mux_get_parent(priv, &data->data.mux);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
if (parent_id == UNIPHIER_CLK_ID_INVALID)
|
|
|
|
return NULL;
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
parent_data = uniphier_clk_get_data(priv, parent_id);
|
2016-10-17 13:18:01 +00:00
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
WARN_ON(!parent_data);
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
return parent_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __uniphier_clk_enable(struct uniphier_clk_priv *priv,
|
|
|
|
const struct uniphier_clk_data *data)
|
|
|
|
{
|
|
|
|
const struct uniphier_clk_data *parent_data;
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
if (data->type == UNIPHIER_CLK_TYPE_GATE)
|
|
|
|
uniphier_clk_gate_enable(priv, &data->data.gate);
|
|
|
|
|
|
|
|
parent_data = uniphier_clk_get_parent_data(priv, data);
|
|
|
|
if (!parent_data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
return __uniphier_clk_enable(priv, parent_data);
|
2016-02-02 12:11:32 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
static int uniphier_clk_enable(struct clk *clk)
|
2016-02-02 12:11:32 +00:00
|
|
|
{
|
2016-06-17 15:44:00 +00:00
|
|
|
struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
|
2017-10-13 10:21:59 +00:00
|
|
|
const struct uniphier_clk_data *data;
|
|
|
|
|
|
|
|
data = uniphier_clk_get_data(priv, clk->id);
|
|
|
|
if (!data)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
__uniphier_clk_enable(priv, data);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long __uniphier_clk_get_rate(
|
|
|
|
struct uniphier_clk_priv *priv,
|
|
|
|
const struct uniphier_clk_data *data)
|
|
|
|
{
|
|
|
|
const struct uniphier_clk_data *parent_data;
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
if (data->type == UNIPHIER_CLK_TYPE_FIXED_RATE)
|
|
|
|
return data->data.rate.fixed_rate;
|
|
|
|
|
|
|
|
parent_data = uniphier_clk_get_parent_data(priv, data);
|
|
|
|
if (!parent_data)
|
2016-10-17 13:18:01 +00:00
|
|
|
return 0;
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
return __uniphier_clk_get_rate(priv, parent_data);
|
|
|
|
}
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
static unsigned long uniphier_clk_get_rate(struct clk *clk)
|
|
|
|
{
|
|
|
|
struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
|
|
|
|
const struct uniphier_clk_data *data;
|
|
|
|
|
|
|
|
data = uniphier_clk_get_data(priv, clk->id);
|
|
|
|
if (!data)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
return __uniphier_clk_get_rate(priv, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long __uniphier_clk_set_rate(
|
|
|
|
struct uniphier_clk_priv *priv,
|
|
|
|
const struct uniphier_clk_data *data,
|
|
|
|
unsigned long rate, bool set)
|
|
|
|
{
|
|
|
|
const struct uniphier_clk_data *best_parent_data = NULL;
|
|
|
|
const struct uniphier_clk_data *parent_data;
|
|
|
|
unsigned long best_rate = 0;
|
|
|
|
unsigned long parent_rate;
|
|
|
|
u8 parent_id;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (data->type == UNIPHIER_CLK_TYPE_FIXED_RATE)
|
|
|
|
return data->data.rate.fixed_rate;
|
|
|
|
|
|
|
|
if (data->type == UNIPHIER_CLK_TYPE_GATE) {
|
|
|
|
parent_data = uniphier_clk_get_parent_data(priv, data);
|
|
|
|
if (!parent_data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return __uniphier_clk_set_rate(priv, parent_data, rate, set);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WARN_ON(data->type != UNIPHIER_CLK_TYPE_MUX))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
for (i = 0; i < data->data.mux.num_parents; i++) {
|
|
|
|
parent_id = data->data.mux.parent_ids[i];
|
|
|
|
parent_data = uniphier_clk_get_data(priv, parent_id);
|
|
|
|
if (WARN_ON(!parent_data))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
parent_rate = __uniphier_clk_set_rate(priv, parent_data, rate,
|
|
|
|
false);
|
|
|
|
|
|
|
|
if (parent_rate <= rate && best_rate < parent_rate) {
|
|
|
|
best_rate = parent_rate;
|
|
|
|
best_parent_data = parent_data;
|
2016-10-17 13:18:01 +00:00
|
|
|
}
|
2016-02-02 12:11:32 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
dev_dbg(priv->dev, "id=%u, best_rate=%lu\n", data->id, best_rate);
|
|
|
|
|
|
|
|
if (!best_parent_data)
|
2016-10-17 13:18:01 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
if (!set)
|
|
|
|
return best_rate;
|
|
|
|
|
|
|
|
uniphier_clk_mux_set_parent(priv, &data->data.mux,
|
|
|
|
best_parent_data->id);
|
|
|
|
|
|
|
|
return best_rate = __uniphier_clk_set_rate(priv, best_parent_data,
|
|
|
|
rate, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long uniphier_clk_set_rate(struct clk *clk, ulong rate)
|
|
|
|
{
|
|
|
|
struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
|
|
|
|
const struct uniphier_clk_data *data;
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
data = uniphier_clk_get_data(priv, clk->id);
|
|
|
|
if (!data)
|
|
|
|
return -ENODEV;
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
return __uniphier_clk_set_rate(priv, data, rate, true);
|
2016-02-02 12:11:32 +00:00
|
|
|
}
|
|
|
|
|
2017-06-22 07:42:04 +00:00
|
|
|
static const struct clk_ops uniphier_clk_ops = {
|
2016-02-02 12:11:32 +00:00
|
|
|
.enable = uniphier_clk_enable,
|
2016-06-17 15:44:00 +00:00
|
|
|
.get_rate = uniphier_clk_get_rate,
|
|
|
|
.set_rate = uniphier_clk_set_rate,
|
2016-02-02 12:11:32 +00:00
|
|
|
};
|
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
static int uniphier_clk_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct uniphier_clk_priv *priv = dev_get_priv(dev);
|
|
|
|
fdt_addr_t addr;
|
|
|
|
|
2020-07-17 05:36:48 +00:00
|
|
|
addr = dev_read_addr(dev->parent);
|
2016-10-17 13:18:01 +00:00
|
|
|
if (addr == FDT_ADDR_T_NONE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
priv->base = devm_ioremap(dev, addr, SZ_4K);
|
|
|
|
if (!priv->base)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-10-13 10:21:59 +00:00
|
|
|
priv->dev = dev;
|
2016-10-17 13:18:01 +00:00
|
|
|
priv->data = (void *)dev_get_driver_data(dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-21 22:42:21 +00:00
|
|
|
static const struct udevice_id uniphier_clk_match[] = {
|
2017-08-28 16:06:15 +00:00
|
|
|
/* System clock */
|
|
|
|
{
|
|
|
|
.compatible = "socionext,uniphier-ld4-clock",
|
2017-10-13 10:21:59 +00:00
|
|
|
.data = (ulong)uniphier_pxs2_sys_clk_data,
|
2017-08-28 16:06:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.compatible = "socionext,uniphier-pro4-clock",
|
2017-10-13 10:21:59 +00:00
|
|
|
.data = (ulong)uniphier_pxs2_sys_clk_data,
|
2017-08-28 16:06:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.compatible = "socionext,uniphier-sld8-clock",
|
2017-10-13 10:21:59 +00:00
|
|
|
.data = (ulong)uniphier_pxs2_sys_clk_data,
|
2017-08-28 16:06:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.compatible = "socionext,uniphier-pro5-clock",
|
2017-10-13 10:21:59 +00:00
|
|
|
.data = (ulong)uniphier_pxs2_sys_clk_data,
|
2017-08-28 16:06:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.compatible = "socionext,uniphier-pxs2-clock",
|
2017-10-13 10:21:59 +00:00
|
|
|
.data = (ulong)uniphier_pxs2_sys_clk_data,
|
2017-08-28 16:06:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.compatible = "socionext,uniphier-ld11-clock",
|
2017-10-13 10:21:59 +00:00
|
|
|
.data = (ulong)uniphier_ld20_sys_clk_data,
|
2017-08-28 16:06:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.compatible = "socionext,uniphier-ld20-clock",
|
2017-10-13 10:21:59 +00:00
|
|
|
.data = (ulong)uniphier_ld20_sys_clk_data,
|
2017-08-28 16:06:15 +00:00
|
|
|
},
|
2017-10-13 10:22:00 +00:00
|
|
|
{
|
|
|
|
.compatible = "socionext,uniphier-pxs3-clock",
|
|
|
|
.data = (ulong)uniphier_pxs3_sys_clk_data,
|
|
|
|
},
|
2017-08-28 16:06:15 +00:00
|
|
|
/* Media I/O clock */
|
2016-09-21 22:42:21 +00:00
|
|
|
{
|
2016-09-21 22:42:22 +00:00
|
|
|
.compatible = "socionext,uniphier-ld4-mio-clock",
|
2017-10-13 10:21:59 +00:00
|
|
|
.data = (ulong)uniphier_mio_clk_data,
|
2016-09-21 22:42:21 +00:00
|
|
|
},
|
|
|
|
{
|
2016-09-21 22:42:22 +00:00
|
|
|
.compatible = "socionext,uniphier-pro4-mio-clock",
|
2017-10-13 10:21:59 +00:00
|
|
|
.data = (ulong)uniphier_mio_clk_data,
|
2016-09-21 22:42:21 +00:00
|
|
|
},
|
|
|
|
{
|
2016-09-21 22:42:22 +00:00
|
|
|
.compatible = "socionext,uniphier-sld8-mio-clock",
|
2017-10-13 10:21:59 +00:00
|
|
|
.data = (ulong)uniphier_mio_clk_data,
|
2016-09-21 22:42:21 +00:00
|
|
|
},
|
|
|
|
{
|
2017-01-27 21:53:41 +00:00
|
|
|
.compatible = "socionext,uniphier-pro5-sd-clock",
|
2017-10-13 10:21:59 +00:00
|
|
|
.data = (ulong)uniphier_mio_clk_data,
|
2016-09-21 22:42:21 +00:00
|
|
|
},
|
|
|
|
{
|
2017-01-27 21:53:41 +00:00
|
|
|
.compatible = "socionext,uniphier-pxs2-sd-clock",
|
2017-10-13 10:21:59 +00:00
|
|
|
.data = (ulong)uniphier_mio_clk_data,
|
2016-09-21 22:42:21 +00:00
|
|
|
},
|
|
|
|
{
|
2016-09-21 22:42:22 +00:00
|
|
|
.compatible = "socionext,uniphier-ld11-mio-clock",
|
2017-10-13 10:21:59 +00:00
|
|
|
.data = (ulong)uniphier_mio_clk_data,
|
2016-09-21 22:42:21 +00:00
|
|
|
},
|
|
|
|
{
|
2017-01-27 21:53:41 +00:00
|
|
|
.compatible = "socionext,uniphier-ld20-sd-clock",
|
2017-10-13 10:21:59 +00:00
|
|
|
.data = (ulong)uniphier_mio_clk_data,
|
2016-09-21 22:42:21 +00:00
|
|
|
},
|
2017-10-13 10:22:00 +00:00
|
|
|
{
|
|
|
|
.compatible = "socionext,uniphier-pxs3-sd-clock",
|
|
|
|
.data = (ulong)uniphier_mio_clk_data,
|
|
|
|
},
|
2016-09-21 22:42:21 +00:00
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2016-09-21 22:42:21 +00:00
|
|
|
U_BOOT_DRIVER(uniphier_clk) = {
|
|
|
|
.name = "uniphier-clk",
|
|
|
|
.id = UCLASS_CLK,
|
|
|
|
.of_match = uniphier_clk_match,
|
|
|
|
.probe = uniphier_clk_probe,
|
|
|
|
.priv_auto_alloc_size = sizeof(struct uniphier_clk_priv),
|
|
|
|
.ops = &uniphier_clk_ops,
|
|
|
|
};
|