blob: 394832607ed81556901a2ab94c7431e7f7ac9aa3 [file] [log] [blame]
Masahiro Yamadae4dfb052016-02-02 21:11:32 +09001/*
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +09002 * Copyright (C) 2016 Socionext Inc.
3 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadae4dfb052016-02-02 21:11:32 +09004 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
Masahiro Yamadae4dfb052016-02-02 21:11:32 +09009#include <linux/bitops.h>
10#include <linux/io.h>
Masahiro Yamada46a624f2016-03-24 22:32:39 +090011#include <linux/sizes.h>
Stephen Warrena9622432016-06-17 09:44:00 -060012#include <clk-uclass.h>
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090013#include <dm/device.h>
14
15#include "clk-uniphier.h"
16
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090017/**
18 * struct uniphier_clk_priv - private data for UniPhier clock driver
19 *
20 * @base: base address of the clock provider
21 * @socdata: SoC specific data
22 */
23struct uniphier_clk_priv {
24 void __iomem *base;
25 const struct uniphier_clk_soc_data *socdata;
26};
27
28int uniphier_clk_probe(struct udevice *dev)
29{
30 struct uniphier_clk_priv *priv = dev_get_priv(dev);
31 fdt_addr_t addr;
32
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +090033 addr = dev_get_addr(dev->parent);
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090034 if (addr == FDT_ADDR_T_NONE)
35 return -EINVAL;
36
37 priv->base = devm_ioremap(dev, addr, SZ_4K);
38 if (!priv->base)
39 return -ENOMEM;
40
41 priv->socdata = (void *)dev_get_driver_data(dev);
42
43 return 0;
44}
45
Stephen Warrena9622432016-06-17 09:44:00 -060046static int uniphier_clk_enable(struct clk *clk)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090047{
Stephen Warrena9622432016-06-17 09:44:00 -060048 struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
Masahiro Yamada630c99f2016-09-22 07:42:20 +090049 const struct uniphier_clk_gate_data *gate = priv->socdata->gate;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090050 unsigned int nr_gate = priv->socdata->nr_gate;
51 void __iomem *reg;
52 u32 mask, data, tmp;
53 int i;
54
55 for (i = 0; i < nr_gate; i++) {
Stephen Warrena9622432016-06-17 09:44:00 -060056 if (gate[i].index != clk->id)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090057 continue;
58
59 reg = priv->base + gate[i].reg;
60 mask = gate[i].mask;
61 data = gate[i].data & mask;
62
63 tmp = readl(reg);
64 tmp &= ~mask;
65 tmp |= data & mask;
66 debug("%s: %p: %08x\n", __func__, reg, tmp);
67 writel(tmp, reg);
68 }
69
70 return 0;
71}
72
Stephen Warrena9622432016-06-17 09:44:00 -060073static ulong uniphier_clk_get_rate(struct clk *clk)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090074{
Stephen Warrena9622432016-06-17 09:44:00 -060075 struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
Masahiro Yamada630c99f2016-09-22 07:42:20 +090076 const struct uniphier_clk_rate_data *rdata = priv->socdata->rate;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090077 unsigned int nr_rdata = priv->socdata->nr_rate;
78 void __iomem *reg;
79 u32 mask, data;
80 ulong matched_rate = 0;
81 int i;
82
83 for (i = 0; i < nr_rdata; i++) {
Stephen Warrena9622432016-06-17 09:44:00 -060084 if (rdata[i].index != clk->id)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090085 continue;
86
87 if (rdata[i].reg == UNIPHIER_CLK_RATE_IS_FIXED)
88 return rdata[i].rate;
89
90 reg = priv->base + rdata[i].reg;
91 mask = rdata[i].mask;
92 data = rdata[i].data & mask;
93 if ((readl(reg) & mask) == data) {
94 if (matched_rate && rdata[i].rate != matched_rate) {
95 printf("failed to get clk rate for insane register values\n");
96 return -EINVAL;
97 }
98 matched_rate = rdata[i].rate;
99 }
100 }
101
102 debug("%s: rate = %lu\n", __func__, matched_rate);
103
104 return matched_rate;
105}
106
Stephen Warrena9622432016-06-17 09:44:00 -0600107static ulong uniphier_clk_set_rate(struct clk *clk, ulong rate)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900108{
Stephen Warrena9622432016-06-17 09:44:00 -0600109 struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
Masahiro Yamada630c99f2016-09-22 07:42:20 +0900110 const struct uniphier_clk_rate_data *rdata = priv->socdata->rate;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900111 unsigned int nr_rdata = priv->socdata->nr_rate;
112 void __iomem *reg;
113 u32 mask, data, tmp;
114 ulong best_rate = 0;
115 int i;
116
117 /* first, decide the best match rate */
118 for (i = 0; i < nr_rdata; i++) {
Stephen Warrena9622432016-06-17 09:44:00 -0600119 if (rdata[i].index != clk->id)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900120 continue;
121
122 if (rdata[i].reg == UNIPHIER_CLK_RATE_IS_FIXED)
123 return 0;
124
125 if (rdata[i].rate > best_rate && rdata[i].rate <= rate)
126 best_rate = rdata[i].rate;
127 }
128
129 if (!best_rate)
130 return -ENODEV;
131
132 debug("%s: requested rate = %lu, set rate = %lu\n", __func__,
133 rate, best_rate);
134
135 /* second, really set registers */
136 for (i = 0; i < nr_rdata; i++) {
Stephen Warrena9622432016-06-17 09:44:00 -0600137 if (rdata[i].index != clk->id || rdata[i].rate != best_rate)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900138 continue;
139
140 reg = priv->base + rdata[i].reg;
141 mask = rdata[i].mask;
142 data = rdata[i].data & mask;
143
144 tmp = readl(reg);
145 tmp &= ~mask;
146 tmp |= data;
147 debug("%s: %p: %08x\n", __func__, reg, tmp);
148 writel(tmp, reg);
149 }
150
151 return best_rate;
152}
153
154const struct clk_ops uniphier_clk_ops = {
155 .enable = uniphier_clk_enable,
Stephen Warrena9622432016-06-17 09:44:00 -0600156 .get_rate = uniphier_clk_get_rate,
157 .set_rate = uniphier_clk_set_rate,
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900158};
159
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900160static const struct udevice_id uniphier_clk_match[] = {
161 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900162 .compatible = "socionext,uniphier-sld3-mio-clock",
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900163 .data = (ulong)&uniphier_mio_clk_data,
164 },
165 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900166 .compatible = "socionext,uniphier-ld4-mio-clock",
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900167 .data = (ulong)&uniphier_mio_clk_data,
168 },
169 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900170 .compatible = "socionext,uniphier-pro4-mio-clock",
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900171 .data = (ulong)&uniphier_mio_clk_data,
172 },
173 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900174 .compatible = "socionext,uniphier-sld8-mio-clock",
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900175 .data = (ulong)&uniphier_mio_clk_data,
176 },
177 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900178 .compatible = "socionext,uniphier-pro5-mio-clock",
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900179 .data = (ulong)&uniphier_mio_clk_data,
180 },
181 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900182 .compatible = "socionext,uniphier-pxs2-mio-clock",
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900183 .data = (ulong)&uniphier_mio_clk_data,
184 },
185 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900186 .compatible = "socionext,uniphier-ld11-mio-clock",
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900187 .data = (ulong)&uniphier_mio_clk_data,
188 },
189 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900190 .compatible = "socionext,uniphier-ld20-mio-clock",
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900191 .data = (ulong)&uniphier_mio_clk_data,
192 },
193 { /* sentinel */ }
194};
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900195
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900196U_BOOT_DRIVER(uniphier_clk) = {
197 .name = "uniphier-clk",
198 .id = UCLASS_CLK,
199 .of_match = uniphier_clk_match,
200 .probe = uniphier_clk_probe,
201 .priv_auto_alloc_size = sizeof(struct uniphier_clk_priv),
202 .ops = &uniphier_clk_ops,
203};