aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/oxnas/files/drivers/clk/clk-oxnas.c
blob: 4dc6c4499297c77bde21ba19f582500e9d1d2c2e (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Copyright (C) 2010 Broadcom
 * Copyright (C) 2012 Stephen Warren
 *
 * 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/>.
 */

#include <linux/clk.h>
#include <linux/clkdev.h>
#include <linux/clk-provider.h>
#include <linux/of.h>
#include <linux/delay.h>
#include <linux/stringify.h>
#include <linux/reset.h>
#include <linux/io.h>
#include <mach/hardware.h>
#include <mach/utils.h>

#define MHZ (1000 * 1000)

struct clk_oxnas_pllb {
	struct clk_hw hw;
	struct device_node *devnode;
	struct reset_control *rstc;
};

#define to_clk_oxnas_pllb(_hw) container_of(_hw, struct clk_oxnas_pllb, hw)

static unsigned long plla_clk_recalc_rate(struct clk_hw *hw,
	unsigned long parent_rate)
{
	unsigned long fin = parent_rate;
	unsigned long pll0;
	unsigned long fbdiv, refdiv, outdiv;

	pll0 = readl_relaxed(SYS_CTRL_PLLA_CTRL0);
	refdiv = (pll0 >> PLLA_REFDIV_SHIFT) & PLLA_REFDIV_MASK;
	refdiv += 1;
	outdiv = (pll0 >> PLLA_OUTDIV_SHIFT) & PLLA_OUTDIV_MASK;
	outdiv += 1;
	fbdiv = readl_relaxed(SYS_CTRL_PLLA_CTRL1);

	/* seems we will not be here when pll is bypassed, so ignore this
	 * case */

	return fin / MHZ * fbdiv / (refdiv * outdiv) / 32768 * MHZ;
}

static const char *pll_clk_parents[] = {
	"oscillator",
};

static struct clk_ops plla_ops = {
	.recalc_rate = plla_clk_recalc_rate,
};

static struct clk_init_data clk_plla_init = {
	.name = "plla",
	.ops = &plla_ops,
	.parent_names = pll_clk_parents,
	.num_parents = ARRAY_SIZE(pll_clk_parents),
};

static struct clk_hw plla_hw = {
	.init = &clk_plla_init,
};

static int pllb_clk_is_prepared(struct clk_hw *hw)
{
	struct clk_oxnas_pllb *pllb = to_clk_oxnas_pllb(hw);

	return !!pllb->rstc;
}

static int pllb_clk_prepare(struct clk_hw *hw)
{
	struct clk_oxnas_pllb *pllb = to_clk_oxnas_pllb(hw);

	pllb->rstc = of_reset_control_get(pllb->devnode, NULL);

	return IS_ERR(pllb->rstc) ? PTR_ERR(pllb->rstc) : 0;
}

static void pllb_clk_unprepare(struct clk_hw *hw)
{
	struct clk_oxnas_pllb *pllb = to_clk_oxnas_pllb(hw);

	BUG_ON(IS_ERR(pllb->rstc));

	reset_control_put(pllb->rstc);
	pllb->rstc = NULL;
}

static int pllb_clk_enable(struct clk_hw *hw)
{
	struct clk_oxnas_pllb *pllb = to_clk_oxnas_pllb(hw);

	BUG_ON(IS_ERR(pllb->rstc));

	/* put PLL into bypass */
	oxnas_register_set_mask(SEC_CTRL_PLLB_CTRL0, BIT(PLLB_BYPASS));
	wmb();
	udelay(10);
	reset_control_assert(pllb->rstc);
	udelay(10);
	/* set PLL B control information */
	writel((1 << PLLB_ENSAT) | (1 << PLLB_OUTDIV) | (2 << PLLB_REFDIV),
				SEC_CTRL_PLLB_CTRL0);
	reset_control_deassert(pllb->rstc);
	udelay(100);
	oxnas_register_clear_mask(SEC_CTRL_PLLB_CTRL0, BIT(PLLB_BYPASS));

	return 0;
}

static void pllb_clk_disable(struct clk_hw *hw)
{
	struct clk_oxnas_pllb *pllb = to_clk_oxnas_pllb(hw);

	BUG_ON(IS_ERR(pllb->rstc));

	/* put PLL into bypass */
	oxnas_register_set_mask(SEC_CTRL_PLLB_CTRL0, BIT(PLLB_BYPASS));
	wmb();
	udelay(10);

	reset_control_assert(pllb->rstc);
}

static struct clk_ops pllb_ops = {
	.prepare = pllb_clk_prepare,
	.unprepare = pllb_clk_unprepare,
	.is_prepared = pllb_clk_is_prepared,
	.enable = pllb_clk_enable,
	.disable = pllb_clk_disable,
};

static struct clk_init_data clk_pllb_init = {
	.name = "pllb",
	.ops = &pllb_ops,
	.parent_names = pll_clk_parents,
	.num_parents = ARRAY_SIZE(pll_clk_parents),
};


/* standard gate clock */
struct clk_std {
	struct clk_hw hw;
	signed char bit;
};

#define NUM_STD_CLKS 17
#define to_stdclk(_hw) container_of(_hw, struct clk_std, hw)

static int std_clk_is_enabled(struct clk_hw *hw)
{
	struct clk_std *std = to_stdclk(hw);

	return readl_relaxed(SYSCTRL_CLK_STAT) & BIT(std->bit);
}

static int std_clk_enable(struct clk_hw *hw)
{
	struct clk_std *std = to_stdclk(hw);

	writel(BIT(std->bit), SYS_CTRL_CLK_SET_CTRL);
	return 0;
}

static void std_clk_disable(struct clk_hw *hw)
{
	struct clk_std *std = to_stdclk(hw);

	writel(BIT(std->bit), SYS_CTRL_CLK_CLR_CTRL);
}

static struct clk_ops std_clk_ops = {
	.enable = std_clk_enable,
	.disable = std_clk_disable,
	.is_enabled = std_clk_is_enabled,
};

static const char *std_clk_parents[] = {
	"oscillator",
};

static const char *eth_parents[] = {
	"gmacclk",
};

#define DECLARE_STD_CLKP(__clk, __bit, __parent)	\
static struct clk_init_data clk_##__clk##_init = {	\
	.name = __stringify(__clk),			\
	.ops = &std_clk_ops,				\
	.parent_names = __parent,		\
	.num_parents = ARRAY_SIZE(__parent),	\
};							\
							\
static struct clk_std clk_##__clk = {			\
	.bit = __bit,					\
	.hw = {						\
		.init = &clk_##__clk##_init,		\
	},						\
}

#define DECLARE_STD_CLK(__clk, __bit) DECLARE_STD_CLKP(__clk, __bit, \
							std_clk_parents)

DECLARE_STD_CLK(leon, 0);
DECLARE_STD_CLK(dma_sgdma, 1);
DECLARE_STD_CLK(cipher, 2);
DECLARE_STD_CLK(sd, 3);
DECLARE_STD_CLK(sata, 4);
DECLARE_STD_CLK(audio, 5);
DECLARE_STD_CLK(usbmph, 6);
DECLARE_STD_CLKP(etha, 7, eth_parents);
DECLARE_STD_CLK(pciea, 8);
DECLARE_STD_CLK(static, 9);
DECLARE_STD_CLK(ethb, 10);
DECLARE_STD_CLK(pcieb, 11);
DECLARE_STD_CLK(ref600, 12);
DECLARE_STD_CLK(usbdev, 13);

struct clk_hw *std_clk_hw_tbl[] = {
	&clk_leon.hw,
	&clk_dma_sgdma.hw,
	&clk_cipher.hw,
	&clk_sd.hw,
	&clk_sata.hw,
	&clk_audio.hw,
	&clk_usbmph.hw,
	&clk_etha.hw,
	&clk_pciea.hw,
	&clk_static.hw,
	&clk_ethb.hw,
	&clk_pcieb.hw,
	&clk_ref600.hw,
	&clk_usbdev.hw,
};

struct clk *std_clk_tbl[ARRAY_SIZE(std_clk_hw_tbl)];

static struct clk_onecell_data std_clk_data;

void __init oxnas_init_stdclk(struct device_node *np)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(std_clk_hw_tbl); i++) {
		std_clk_tbl[i] = clk_register(NULL, std_clk_hw_tbl[i]);
		BUG_ON(IS_ERR(std_clk_tbl[i]));
	}
	std_clk_data.clks = std_clk_tbl;
	std_clk_data.clk_num = ARRAY_SIZE(std_clk_tbl);
	of_clk_add_provider(np, of_clk_src_onecell_get, &std_clk_data);
}
CLK_OF_DECLARE(oxnas_pllstd, "plxtech,nas782x-stdclk", oxnas_init_stdclk);

void __init oxnas_init_plla(struct device_node *np)
{
	struct clk *clk;

	clk = clk_register(NULL, &plla_hw);
	BUG_ON(IS_ERR(clk));
	/* mark it as enabled */
	clk_prepare_enable(clk);
	of_clk_add_provider(np, of_clk_src_simple_get, clk);
}
CLK_OF_DECLARE(oxnas_plla, "plxtech,nas782x-plla", oxnas_init_plla);

void __init oxnas_init_pllb(struct device_node *np)
{
	struct clk *clk;
	struct clk_oxnas_pllb *pllb;

	pllb = kmalloc(sizeof(*pllb), GFP_KERNEL);
	BUG_ON(!pllb);

	pllb->hw.init = &clk_pllb_init;
	pllb->devnode = np;
	pllb->rstc = NULL;

	clk = clk_register(NULL, &pllb->hw);
	BUG_ON(IS_ERR(clk));
	of_clk_add_provider(np, of_clk_src_simple_get, clk);
}
CLK_OF_DECLARE(oxnas_pllb, "plxtech,nas782x-pllb", oxnas_init_pllb);