aboutsummaryrefslogtreecommitdiff
path: root/drivers/clk/hisilicon/clk-hi3716.c
blob: 887ffe90fba5b349bd6433ee15e10eef05cfeea7 (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
/*
 * Copyright (c) 2013 Linaro Ltd.
 * Copyright (c) 2013 Hisilicon Limited.
 *
 * 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.
 */

#include <linux/kernel.h>
#include <linux/clk-provider.h>
#include <linux/clkdev.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/slab.h>

static DEFINE_SPINLOCK(_lock);

static void __iomem *clk_base;

struct hi3716_clk {
	struct		clk_gate gate;
	void __iomem	*reg;
	u8		reset_bit;
};

#define MAX_NUMS	10

static struct hi3716_clk *to_clk_hi3716(struct clk_hw *hw)
{
	return container_of(hw, struct hi3716_clk, gate.hw);
}

static void __init hi3716_map_io(void)
{
	struct device_node *node;

	if (clk_base)
		return;

	node = of_find_compatible_node(NULL, NULL, "hisilicon,clkbase");
	if (node)
		clk_base = of_iomap(node, 0);
	WARN_ON(!clk_base);
}

static int hi3716_clkgate_prepare(struct clk_hw *hw)
{
	struct hi3716_clk *clk = to_clk_hi3716(hw);
	unsigned long flags = 0;
	u32 reg;

	spin_lock_irqsave(&_lock, flags);

	reg = readl_relaxed(clk->reg);
	reg &= ~BIT(clk->reset_bit);
	writel_relaxed(reg, clk->reg);

	spin_unlock_irqrestore(&_lock, flags);

	return 0;
}

static void hi3716_clkgate_unprepare(struct clk_hw *hw)
{
	struct hi3716_clk *clk = to_clk_hi3716(hw);
	unsigned long flags = 0;
	u32 reg;

	spin_lock_irqsave(&_lock, flags);

	reg = readl_relaxed(clk->reg);
	reg |= BIT(clk->reset_bit);
	writel_relaxed(reg, clk->reg);

	spin_unlock_irqrestore(&_lock, flags);
}

static struct clk_ops hi3716_clkgate_ops = {
	.prepare	= hi3716_clkgate_prepare,
	.unprepare	= hi3716_clkgate_unprepare,
};

void __init hi3716_clkgate_setup(struct device_node *node)
{
	struct clk *clk;
	struct hi3716_clk *p_clk;
	struct clk_init_data init;
	const char *parent_name;
	u32 array[3];	/* reg, enable_bit, reset_bit */
	int err;

	hi3716_map_io();
	err = of_property_read_u32_array(node, "gate-reg", &array[0], 3);
	if (WARN_ON(err))
		return;

	err = of_property_read_string(node, "clock-output-names", &init.name);
	if (WARN_ON(err))
		return;

	p_clk = kzalloc(sizeof(*p_clk), GFP_KERNEL);
	if (WARN_ON(!p_clk))
		return;

	hi3716_clkgate_ops.enable = clk_gate_ops.enable;
	hi3716_clkgate_ops.disable = clk_gate_ops.disable;
	hi3716_clkgate_ops.is_enabled = clk_gate_ops.is_enabled;

	init.ops = &hi3716_clkgate_ops;
	init.flags = CLK_SET_RATE_PARENT;
	parent_name = of_clk_get_parent_name(node, 0);
	init.parent_names = &parent_name;
	init.num_parents = 1;

	p_clk->reg = p_clk->gate.reg = clk_base + array[0];
	p_clk->gate.bit_idx = array[1];
	p_clk->gate.flags = 0;
	p_clk->gate.lock = &_lock;
	p_clk->gate.hw.init = &init;
	p_clk->reset_bit = array[2];

	clk = clk_register(NULL, &p_clk->gate.hw);
	if (WARN_ON(IS_ERR(clk))) {
		kfree(p_clk);
		return;
	}

	of_clk_add_provider(node, of_clk_src_simple_get, clk);
}

static void __init hi3716_clkmux_setup(struct device_node *node)
{
	int num = 0, err;
	void __iomem *reg;
	unsigned int shift, width;
	u32 array[3];	/* reg, mux_shift, mux_width */
	u32 *table = NULL;
	const char *clk_name = node->name;
	const char *parents[MAX_NUMS];
	struct clk *clk;

	hi3716_map_io();
	err = of_property_read_string(node, "clock-output-names", &clk_name);
	if (WARN_ON(err))
		return;

	err = of_property_read_u32_array(node, "mux-reg", &array[0], 3);
	if (WARN_ON(err))
		return;

	reg = clk_base + array[0];
	shift = array[1];
	width = array[2];

	while ((num < MAX_NUMS) &&
		((parents[num] = of_clk_get_parent_name(node, num)) != NULL))
		num++;
	if (!num)
		return;

	table = kzalloc(sizeof(u32 *) * num, GFP_KERNEL);
	if (WARN_ON(!table))
		return;

	err = of_property_read_u32_array(node, "mux-table", table, num);
	if (WARN_ON(err))
		goto err;

	clk = clk_register_mux_table(NULL, clk_name, parents, num,
			CLK_SET_RATE_PARENT, reg, shift, BIT(width) - 1,
			0, table, &_lock);
	if (WARN_ON(IS_ERR(clk)))
		goto err;

	clk_register_clkdev(clk, clk_name, NULL);
	of_clk_add_provider(node, of_clk_src_simple_get, clk);
	return;

err:
	kfree(table);
	return;
}

static void __init hi3716_fixed_pll_setup(struct device_node *node)
{
	const char *clk_name, *parent_name;
	struct clk *clks[MAX_NUMS];
	u32 rate[MAX_NUMS];
	struct clk_onecell_data *clk_data;
	int i, err, nums = 0;

	nums = of_property_count_strings(node, "clock-output-names");
	if (WARN_ON((nums < 0) || (nums > MAX_NUMS)))
		return;

	err = of_property_read_u32_array(node, "clock-frequency",
						&rate[0], nums);
	WARN_ON(err);

	parent_name = of_clk_get_parent_name(node, 0);

	for (i = 0; i < nums; i++) {
		err = of_property_read_string_index(node, "clock-output-names",
				i, &clk_name);
		WARN_ON(err);

		clks[i] = clk_register_fixed_rate(NULL, clk_name,
					parent_name, 0, rate[i]);
		WARN_ON(IS_ERR(clks[i]));
	}

	clk_data = kzalloc(sizeof(*clk_data) + nums * sizeof(struct clk *),
			GFP_KERNEL);
	if (WARN_ON(!clk_data))
		return;

	memcpy(&clk_data[1], clks, nums * sizeof(struct clk *));
	clk_data->clks = (struct clk **)&clk_data[1];
	clk_data->clk_num = nums;
	of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
}

void __init hi3716_fixed_divider_setup(struct device_node *node)
{
	const char *clk_parent;
	const char *clk_name;
	u32 div[MAX_NUMS];
	struct clk *clks[MAX_NUMS];
	struct clk_onecell_data *clk_data;
	int err, i, nums = 0;

	clk_parent = of_clk_get_parent_name(node, 0);

	nums = of_property_count_strings(node, "clock-output-names");
	if (WARN_ON((nums < 0) || (nums > MAX_NUMS)))
		return;

	err = of_property_read_u32_array(node, "div-table", &div[0], nums);
	WARN_ON(err);

	for (i = 0; i < nums; i++) {
		err = of_property_read_string_index(node,
				"clock-output-names", i, &clk_name);
		WARN_ON(err);

		clks[i] = clk_register_fixed_factor(NULL, clk_name,
				clk_parent, CLK_SET_RATE_PARENT, 1, div[i]);
		WARN_ON(IS_ERR(clks[i]));
	}

	clk_data = kzalloc(sizeof(*clk_data) + nums * sizeof(struct clk *),
			GFP_KERNEL);
	if (WARN_ON(!clk_data))
		return;

	memcpy(&clk_data[1], clks, nums * sizeof(struct clk *));
	clk_data->clks = (struct clk **)&clk_data[1];
	clk_data->clk_num = nums;
	of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
}

CLK_OF_DECLARE(hi3716_fixed_rate, "fixed-clock", of_fixed_clk_setup)
CLK_OF_DECLARE(hi3716_fixed_pll, "hisilicon,hi3716-fixed-pll", hi3716_fixed_pll_setup)
CLK_OF_DECLARE(hi3716_divider, "hisilicon,hi3716-fixed-divider", hi3716_fixed_divider_setup)
CLK_OF_DECLARE(hi3716_mux, "hisilicon,hi3716-clk-mux", hi3716_clkmux_setup)
CLK_OF_DECLARE(hi3716_gate, "hisilicon,hi3716-clk-gate", hi3716_clkgate_setup)