blob: e51e0023fc6ec5237624df015bb76f6adf6f0d7c [file] [log] [blame]
Neil Armstrong0bbd72b2016-04-18 12:01:35 +02001/*
2 * Copyright (C) 2010 Broadcom
3 * Copyright (C) 2012 Stephen Warren
4 * Copyright (C) 2016 Neil Armstrong <narmstrong@baylibre.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/clk-provider.h>
20#include <linux/kernel.h>
Paul Gortmaker80c63972016-07-04 17:12:15 -040021#include <linux/init.h>
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020022#include <linux/of.h>
Neil Armstrong5a9e54a2016-10-05 17:07:50 +020023#include <linux/of_device.h>
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020024#include <linux/platform_device.h>
25#include <linux/stringify.h>
26#include <linux/regmap.h>
27#include <linux/mfd/syscon.h>
28
Neil Armstrong5a9e54a2016-10-05 17:07:50 +020029#include <dt-bindings/clock/oxsemi,ox810se.h>
Neil Armstrong6df43932016-10-05 17:07:51 +020030#include <dt-bindings/clock/oxsemi,ox820.h>
Neil Armstrong5a9e54a2016-10-05 17:07:50 +020031
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020032/* Standard regmap gate clocks */
Neil Armstrong1a2cfd02016-10-05 17:07:49 +020033struct clk_oxnas_gate {
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020034 struct clk_hw hw;
Neil Armstrong5a9e54a2016-10-05 17:07:50 +020035 unsigned int bit;
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020036 struct regmap *regmap;
37};
38
Neil Armstrong5a9e54a2016-10-05 17:07:50 +020039struct oxnas_stdclk_data {
40 struct clk_hw_onecell_data *onecell_data;
41 struct clk_oxnas_gate **gates;
42 unsigned int ngates;
43 struct clk_oxnas_pll **plls;
44 unsigned int nplls;
45};
46
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020047/* Regmap offsets */
48#define CLK_STAT_REGOFFSET 0x24
49#define CLK_SET_REGOFFSET 0x2c
50#define CLK_CLR_REGOFFSET 0x30
51
Neil Armstrong1a2cfd02016-10-05 17:07:49 +020052static inline struct clk_oxnas_gate *to_clk_oxnas_gate(struct clk_hw *hw)
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020053{
Neil Armstrong1a2cfd02016-10-05 17:07:49 +020054 return container_of(hw, struct clk_oxnas_gate, hw);
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020055}
56
Neil Armstrong1a2cfd02016-10-05 17:07:49 +020057static int oxnas_clk_gate_is_enabled(struct clk_hw *hw)
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020058{
Neil Armstrong1a2cfd02016-10-05 17:07:49 +020059 struct clk_oxnas_gate *std = to_clk_oxnas_gate(hw);
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020060 int ret;
61 unsigned int val;
62
63 ret = regmap_read(std->regmap, CLK_STAT_REGOFFSET, &val);
64 if (ret < 0)
65 return ret;
66
67 return val & BIT(std->bit);
68}
69
Neil Armstrong1a2cfd02016-10-05 17:07:49 +020070static int oxnas_clk_gate_enable(struct clk_hw *hw)
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020071{
Neil Armstrong1a2cfd02016-10-05 17:07:49 +020072 struct clk_oxnas_gate *std = to_clk_oxnas_gate(hw);
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020073
74 regmap_write(std->regmap, CLK_SET_REGOFFSET, BIT(std->bit));
75
76 return 0;
77}
78
Neil Armstrong1a2cfd02016-10-05 17:07:49 +020079static void oxnas_clk_gate_disable(struct clk_hw *hw)
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020080{
Neil Armstrong1a2cfd02016-10-05 17:07:49 +020081 struct clk_oxnas_gate *std = to_clk_oxnas_gate(hw);
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020082
83 regmap_write(std->regmap, CLK_CLR_REGOFFSET, BIT(std->bit));
84}
85
Neil Armstrong1a2cfd02016-10-05 17:07:49 +020086static const struct clk_ops oxnas_clk_gate_ops = {
87 .enable = oxnas_clk_gate_enable,
88 .disable = oxnas_clk_gate_disable,
89 .is_enabled = oxnas_clk_gate_is_enabled,
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020090};
91
Neil Armstrong5a9e54a2016-10-05 17:07:50 +020092static const char *const osc_parents[] = {
Neil Armstrong0bbd72b2016-04-18 12:01:35 +020093 "oscillator",
94};
95
96static const char *const eth_parents[] = {
97 "gmacclk",
98};
99
Neil Armstrong5a9e54a2016-10-05 17:07:50 +0200100#define OXNAS_GATE(_name, _bit, _parents) \
101struct clk_oxnas_gate _name = { \
102 .bit = (_bit), \
103 .hw.init = &(struct clk_init_data) { \
104 .name = #_name, \
105 .ops = &oxnas_clk_gate_ops, \
106 .parent_names = _parents, \
107 .num_parents = ARRAY_SIZE(_parents), \
108 .flags = (CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED), \
109 }, \
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200110}
111
Neil Armstrong5a9e54a2016-10-05 17:07:50 +0200112static OXNAS_GATE(ox810se_leon, 0, osc_parents);
113static OXNAS_GATE(ox810se_dma_sgdma, 1, osc_parents);
114static OXNAS_GATE(ox810se_cipher, 2, osc_parents);
115static OXNAS_GATE(ox810se_sata, 4, osc_parents);
116static OXNAS_GATE(ox810se_audio, 5, osc_parents);
117static OXNAS_GATE(ox810se_usbmph, 6, osc_parents);
118static OXNAS_GATE(ox810se_etha, 7, eth_parents);
119static OXNAS_GATE(ox810se_pciea, 8, osc_parents);
120static OXNAS_GATE(ox810se_nand, 9, osc_parents);
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200121
Neil Armstrong5a9e54a2016-10-05 17:07:50 +0200122static struct clk_oxnas_gate *ox810se_gates[] = {
123 &ox810se_leon,
124 &ox810se_dma_sgdma,
125 &ox810se_cipher,
126 &ox810se_sata,
127 &ox810se_audio,
128 &ox810se_usbmph,
129 &ox810se_etha,
130 &ox810se_pciea,
131 &ox810se_nand,
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200132};
133
Neil Armstrong6df43932016-10-05 17:07:51 +0200134static OXNAS_GATE(ox820_leon, 0, osc_parents);
135static OXNAS_GATE(ox820_dma_sgdma, 1, osc_parents);
136static OXNAS_GATE(ox820_cipher, 2, osc_parents);
137static OXNAS_GATE(ox820_sd, 3, osc_parents);
138static OXNAS_GATE(ox820_sata, 4, osc_parents);
139static OXNAS_GATE(ox820_audio, 5, osc_parents);
140static OXNAS_GATE(ox820_usbmph, 6, osc_parents);
141static OXNAS_GATE(ox820_etha, 7, eth_parents);
142static OXNAS_GATE(ox820_pciea, 8, osc_parents);
143static OXNAS_GATE(ox820_nand, 9, osc_parents);
144static OXNAS_GATE(ox820_ethb, 10, eth_parents);
145static OXNAS_GATE(ox820_pcieb, 11, osc_parents);
146static OXNAS_GATE(ox820_ref600, 12, osc_parents);
147static OXNAS_GATE(ox820_usbdev, 13, osc_parents);
148
149static struct clk_oxnas_gate *ox820_gates[] = {
150 &ox820_leon,
151 &ox820_dma_sgdma,
152 &ox820_cipher,
153 &ox820_sd,
154 &ox820_sata,
155 &ox820_audio,
156 &ox820_usbmph,
157 &ox820_etha,
158 &ox820_pciea,
159 &ox820_nand,
160 &ox820_etha,
161 &ox820_pciea,
162 &ox820_ref600,
163 &ox820_usbdev,
164};
165
Neil Armstrong5a9e54a2016-10-05 17:07:50 +0200166static struct clk_hw_onecell_data ox810se_hw_onecell_data = {
167 .hws = {
168 [CLK_810_LEON] = &ox810se_leon.hw,
169 [CLK_810_DMA_SGDMA] = &ox810se_dma_sgdma.hw,
170 [CLK_810_CIPHER] = &ox810se_cipher.hw,
171 [CLK_810_SATA] = &ox810se_sata.hw,
172 [CLK_810_AUDIO] = &ox810se_audio.hw,
173 [CLK_810_USBMPH] = &ox810se_usbmph.hw,
174 [CLK_810_ETHA] = &ox810se_etha.hw,
175 [CLK_810_PCIEA] = &ox810se_pciea.hw,
176 [CLK_810_NAND] = &ox810se_nand.hw,
177 },
178 .num = ARRAY_SIZE(ox810se_gates),
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200179};
180
Neil Armstrong6df43932016-10-05 17:07:51 +0200181static struct clk_hw_onecell_data ox820_hw_onecell_data = {
182 .hws = {
183 [CLK_820_LEON] = &ox820_leon.hw,
184 [CLK_820_DMA_SGDMA] = &ox820_dma_sgdma.hw,
185 [CLK_820_CIPHER] = &ox820_cipher.hw,
186 [CLK_820_SD] = &ox820_sd.hw,
187 [CLK_820_SATA] = &ox820_sata.hw,
188 [CLK_820_AUDIO] = &ox820_audio.hw,
189 [CLK_820_USBMPH] = &ox820_usbmph.hw,
190 [CLK_820_ETHA] = &ox820_etha.hw,
191 [CLK_820_PCIEA] = &ox820_pciea.hw,
192 [CLK_820_NAND] = &ox820_nand.hw,
193 [CLK_820_ETHB] = &ox820_ethb.hw,
194 [CLK_820_PCIEB] = &ox820_pcieb.hw,
195 [CLK_820_REF600] = &ox820_ref600.hw,
196 [CLK_820_USBDEV] = &ox820_usbdev.hw,
197 },
198 .num = ARRAY_SIZE(ox820_gates),
199};
Neil Armstrong5a9e54a2016-10-05 17:07:50 +0200200
201static struct oxnas_stdclk_data ox810se_stdclk_data = {
202 .onecell_data = &ox810se_hw_onecell_data,
203 .gates = ox810se_gates,
204 .ngates = ARRAY_SIZE(ox810se_gates),
205};
206
Neil Armstrong6df43932016-10-05 17:07:51 +0200207static struct oxnas_stdclk_data ox820_stdclk_data = {
208 .onecell_data = &ox820_hw_onecell_data,
209 .gates = ox820_gates,
210 .ngates = ARRAY_SIZE(ox820_gates),
211};
Neil Armstrong5a9e54a2016-10-05 17:07:50 +0200212
213static const struct of_device_id oxnas_stdclk_dt_ids[] = {
214 { .compatible = "oxsemi,ox810se-stdclk", &ox810se_stdclk_data },
Neil Armstrong6df43932016-10-05 17:07:51 +0200215 { .compatible = "oxsemi,ox820-stdclk", &ox820_stdclk_data },
Neil Armstrong5a9e54a2016-10-05 17:07:50 +0200216 { }
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200217};
218
219static int oxnas_stdclk_probe(struct platform_device *pdev)
220{
221 struct device_node *np = pdev->dev.of_node;
Neil Armstrong5a9e54a2016-10-05 17:07:50 +0200222 const struct oxnas_stdclk_data *data;
223 const struct of_device_id *id;
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200224 struct regmap *regmap;
Neil Armstrong5a9e54a2016-10-05 17:07:50 +0200225 int ret;
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200226 int i;
227
Neil Armstrong5a9e54a2016-10-05 17:07:50 +0200228 id = of_match_device(oxnas_stdclk_dt_ids, &pdev->dev);
229 if (!id)
230 return -ENODEV;
231 data = id->data;
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200232
233 regmap = syscon_node_to_regmap(of_get_parent(np));
Wei Yongjuna5e9b852016-06-17 17:24:23 +0000234 if (IS_ERR(regmap)) {
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200235 dev_err(&pdev->dev, "failed to have parent regmap\n");
Wei Yongjuna5e9b852016-06-17 17:24:23 +0000236 return PTR_ERR(regmap);
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200237 }
238
Neil Armstrong5a9e54a2016-10-05 17:07:50 +0200239 for (i = 0 ; i < data->ngates ; ++i)
240 data->gates[i]->regmap = regmap;
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200241
Neil Armstrong5a9e54a2016-10-05 17:07:50 +0200242 for (i = 0; i < data->onecell_data->num; i++) {
243 if (!data->onecell_data->hws[i])
244 continue;
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200245
Neil Armstrong5a9e54a2016-10-05 17:07:50 +0200246 ret = devm_clk_hw_register(&pdev->dev,
247 data->onecell_data->hws[i]);
248 if (ret)
249 return ret;
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200250 }
251
Neil Armstrong5a9e54a2016-10-05 17:07:50 +0200252 return of_clk_add_hw_provider(np, of_clk_hw_onecell_get,
253 data->onecell_data);
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200254}
255
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200256static struct platform_driver oxnas_stdclk_driver = {
257 .probe = oxnas_stdclk_probe,
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200258 .driver = {
259 .name = "oxnas-stdclk",
Paul Gortmaker80c63972016-07-04 17:12:15 -0400260 .suppress_bind_attrs = true,
Neil Armstrong0bbd72b2016-04-18 12:01:35 +0200261 .of_match_table = oxnas_stdclk_dt_ids,
262 },
263};
Paul Gortmaker80c63972016-07-04 17:12:15 -0400264builtin_platform_driver(oxnas_stdclk_driver);