2016-02-02 12:11:32 +00:00
|
|
|
/*
|
2016-07-19 12:56:13 +00:00
|
|
|
* Copyright (C) 2016 Socionext Inc.
|
|
|
|
* Author: Masahiro Yamada <yamada.masahiro@socionext.com>
|
2016-02-02 12:11:32 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2016-10-17 13:18:01 +00:00
|
|
|
#include <clk-uclass.h>
|
|
|
|
#include <dm/device.h>
|
2016-02-02 12:11:32 +00:00
|
|
|
#include <linux/bitops.h>
|
|
|
|
#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 {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
static int uniphier_clk_enable(struct clk *clk)
|
2016-09-21 22:42:21 +00:00
|
|
|
{
|
2016-10-17 13:18:01 +00:00
|
|
|
struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
|
|
|
|
unsigned long id = clk->id;
|
|
|
|
const struct uniphier_clk_gate_data *p;
|
2016-09-21 22:42:21 +00:00
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
for (p = priv->data->gate; p->id != UNIPHIER_CLK_ID_END; p++) {
|
|
|
|
u32 val;
|
2016-09-21 22:42:21 +00:00
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
if (p->id != id)
|
|
|
|
continue;
|
2016-09-21 22:42:21 +00:00
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
val = readl(priv->base + p->reg);
|
|
|
|
val |= BIT(p->bit);
|
|
|
|
writel(val, priv->base + p->reg);
|
2016-09-21 22:42:21 +00:00
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_err(priv->dev, "clk_id=%lu was not handled\n", id);
|
|
|
|
return -EINVAL;
|
2016-09-21 22:42:21 +00:00
|
|
|
}
|
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
static const struct uniphier_clk_mux_data *
|
|
|
|
uniphier_clk_get_mux_data(struct uniphier_clk_priv *priv, unsigned long id)
|
2016-02-02 12:11:32 +00:00
|
|
|
{
|
2016-10-17 13:18:01 +00:00
|
|
|
const struct uniphier_clk_mux_data *p;
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
for (p = priv->data->mux; p->id != UNIPHIER_CLK_ID_END; p++) {
|
|
|
|
if (p->id == id)
|
|
|
|
return p;
|
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
|
|
|
}
|
|
|
|
|
2016-06-17 15:44:00 +00:00
|
|
|
static ulong uniphier_clk_get_rate(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);
|
2016-10-17 13:18:01 +00:00
|
|
|
const struct uniphier_clk_mux_data *mux;
|
|
|
|
u32 val;
|
2016-02-02 12:11:32 +00:00
|
|
|
int i;
|
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
mux = uniphier_clk_get_mux_data(priv, clk->id);
|
|
|
|
if (!mux)
|
|
|
|
return 0;
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
if (!mux->nr_muxs) /* fixed-rate */
|
|
|
|
return mux->rates[0];
|
|
|
|
|
|
|
|
val = readl(priv->base + mux->reg);
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
for (i = 0; i < mux->nr_muxs; i++)
|
|
|
|
if ((mux->masks[i] & val) == mux->vals[i])
|
|
|
|
return mux->rates[i];
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
return -EINVAL;
|
2016-02-02 12:11:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 15:44:00 +00:00
|
|
|
static ulong uniphier_clk_set_rate(struct clk *clk, ulong rate)
|
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);
|
2016-10-17 13:18:01 +00:00
|
|
|
const struct uniphier_clk_mux_data *mux;
|
|
|
|
u32 val;
|
|
|
|
int i, best_rate_id = -1;
|
2016-02-02 12:11:32 +00:00
|
|
|
ulong best_rate = 0;
|
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
mux = uniphier_clk_get_mux_data(priv, clk->id);
|
|
|
|
if (!mux)
|
|
|
|
return 0;
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
if (!mux->nr_muxs) /* fixed-rate */
|
|
|
|
return mux->rates[0];
|
2016-02-02 12:11:32 +00:00
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
/* first, decide the best match rate */
|
|
|
|
for (i = 0; i < mux->nr_muxs; i++) {
|
|
|
|
if (mux->rates[i] > best_rate && mux->rates[i] <= rate) {
|
|
|
|
best_rate = mux->rates[i];
|
|
|
|
best_rate_id = i;
|
|
|
|
}
|
2016-02-02 12:11:32 +00:00
|
|
|
}
|
|
|
|
|
2016-10-17 13:18:01 +00:00
|
|
|
if (best_rate_id < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
val = readl(priv->base + mux->reg);
|
|
|
|
val &= ~mux->masks[best_rate_id];
|
|
|
|
val |= mux->vals[best_rate_id];
|
|
|
|
writel(val, priv->base + mux->reg);
|
2016-02-02 12:11:32 +00:00
|
|
|
|
|
|
|
debug("%s: requested rate = %lu, set rate = %lu\n", __func__,
|
|
|
|
rate, best_rate);
|
|
|
|
|
|
|
|
return best_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct clk_ops uniphier_clk_ops = {
|
|
|
|
.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;
|
|
|
|
|
|
|
|
addr = dev_get_addr(dev->parent);
|
|
|
|
if (addr == FDT_ADDR_T_NONE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
priv->base = devm_ioremap(dev, addr, SZ_4K);
|
|
|
|
if (!priv->base)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
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[] = {
|
|
|
|
{
|
2016-09-21 22:42:22 +00:00
|
|
|
.compatible = "socionext,uniphier-sld3-mio-clock",
|
2016-09-21 22:42:21 +00:00
|
|
|
.data = (ulong)&uniphier_mio_clk_data,
|
|
|
|
},
|
|
|
|
{
|
2016-09-21 22:42:22 +00:00
|
|
|
.compatible = "socionext,uniphier-ld4-mio-clock",
|
2016-09-21 22:42:21 +00:00
|
|
|
.data = (ulong)&uniphier_mio_clk_data,
|
|
|
|
},
|
|
|
|
{
|
2016-09-21 22:42:22 +00:00
|
|
|
.compatible = "socionext,uniphier-pro4-mio-clock",
|
2016-09-21 22:42:21 +00:00
|
|
|
.data = (ulong)&uniphier_mio_clk_data,
|
|
|
|
},
|
|
|
|
{
|
2016-09-21 22:42:22 +00:00
|
|
|
.compatible = "socionext,uniphier-sld8-mio-clock",
|
2016-09-21 22:42:21 +00:00
|
|
|
.data = (ulong)&uniphier_mio_clk_data,
|
|
|
|
},
|
|
|
|
{
|
2017-01-27 21:53:41 +00:00
|
|
|
.compatible = "socionext,uniphier-pro5-sd-clock",
|
2016-09-21 22:42:21 +00:00
|
|
|
.data = (ulong)&uniphier_mio_clk_data,
|
|
|
|
},
|
|
|
|
{
|
2017-01-27 21:53:41 +00:00
|
|
|
.compatible = "socionext,uniphier-pxs2-sd-clock",
|
2016-09-21 22:42:21 +00:00
|
|
|
.data = (ulong)&uniphier_mio_clk_data,
|
|
|
|
},
|
|
|
|
{
|
2016-09-21 22:42:22 +00:00
|
|
|
.compatible = "socionext,uniphier-ld11-mio-clock",
|
2016-09-21 22:42:21 +00:00
|
|
|
.data = (ulong)&uniphier_mio_clk_data,
|
|
|
|
},
|
|
|
|
{
|
2017-01-27 21:53:41 +00:00
|
|
|
.compatible = "socionext,uniphier-ld20-sd-clock",
|
2016-09-21 22:42:21 +00:00
|
|
|
.data = (ulong)&uniphier_mio_clk_data,
|
|
|
|
},
|
|
|
|
{ /* 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,
|
|
|
|
};
|