blob: 8430983d3e70d734d7192219314347f87afe2478 [file] [log] [blame]
John Crispin287e3f32012-04-17 15:53:19 +02001/*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License version 2 as published
4 * by the Free Software Foundation.
5 *
6 * Copyright (C) 2011-2012 John Crispin <blogic@openwrt.org>
7 */
8
9#include <linux/ioport.h>
10#include <linux/export.h>
11#include <linux/clkdev.h>
12#include <linux/of.h>
13#include <linux/of_platform.h>
14#include <linux/of_address.h>
15
16#include <lantiq_soc.h>
17
18#include "../clk.h"
19#include "../prom.h"
20
21/* clock control register */
22#define CGU_IFCCR 0x0018
John Crispine29b72f2012-07-22 08:55:57 +020023#define CGU_IFCCR_VR9 0x0024
John Crispin287e3f32012-04-17 15:53:19 +020024/* system clock register */
25#define CGU_SYS 0x0010
26/* pci control register */
27#define CGU_PCICR 0x0034
John Crispine29b72f2012-07-22 08:55:57 +020028#define CGU_PCICR_VR9 0x0038
John Crispin287e3f32012-04-17 15:53:19 +020029/* ephy configuration register */
30#define CGU_EPHY 0x10
31/* power control register */
32#define PMU_PWDCR 0x1C
33/* power status register */
34#define PMU_PWDSR 0x20
35/* power control register */
36#define PMU_PWDCR1 0x24
37/* power status register */
38#define PMU_PWDSR1 0x28
39/* power control register */
40#define PWDCR(x) ((x) ? (PMU_PWDCR1) : (PMU_PWDCR))
41/* power status register */
42#define PWDSR(x) ((x) ? (PMU_PWDSR1) : (PMU_PWDSR))
43
44/* clock gates that we can en/disable */
45#define PMU_USB0_P BIT(0)
46#define PMU_PCI BIT(4)
John Crispin009d6912012-04-19 16:23:14 +020047#define PMU_DMA BIT(5)
John Crispin287e3f32012-04-17 15:53:19 +020048#define PMU_USB0 BIT(6)
49#define PMU_ASC0 BIT(7)
50#define PMU_EPHY BIT(7) /* ase */
51#define PMU_SPI BIT(8)
52#define PMU_DFE BIT(9)
53#define PMU_EBU BIT(10)
54#define PMU_STP BIT(11)
John Crispin009d6912012-04-19 16:23:14 +020055#define PMU_GPT BIT(12)
John Crispin287e3f32012-04-17 15:53:19 +020056#define PMU_AHBS BIT(13) /* vr9 */
John Crispin009d6912012-04-19 16:23:14 +020057#define PMU_FPI BIT(14)
John Crispin287e3f32012-04-17 15:53:19 +020058#define PMU_AHBM BIT(15)
59#define PMU_ASC1 BIT(17)
60#define PMU_PPE_QSB BIT(18)
61#define PMU_PPE_SLL01 BIT(19)
62#define PMU_PPE_TC BIT(21)
63#define PMU_PPE_EMA BIT(22)
64#define PMU_PPE_DPLUM BIT(23)
65#define PMU_PPE_DPLUS BIT(24)
66#define PMU_USB1_P BIT(26)
67#define PMU_USB1 BIT(27)
John Crispin009d6912012-04-19 16:23:14 +020068#define PMU_SWITCH BIT(28)
John Crispin287e3f32012-04-17 15:53:19 +020069#define PMU_PPE_TOP BIT(29)
70#define PMU_GPHY BIT(30)
71#define PMU_PCIE_CLK BIT(31)
72
73#define PMU1_PCIE_PHY BIT(0)
74#define PMU1_PCIE_CTL BIT(1)
75#define PMU1_PCIE_PDI BIT(4)
76#define PMU1_PCIE_MSI BIT(5)
77
78#define pmu_w32(x, y) ltq_w32((x), pmu_membase + (y))
79#define pmu_r32(x) ltq_r32(pmu_membase + (x))
80
81static void __iomem *pmu_membase;
82void __iomem *ltq_cgu_membase;
83void __iomem *ltq_ebu_membase;
84
John Crispine29b72f2012-07-22 08:55:57 +020085static u32 ifccr = CGU_IFCCR;
86static u32 pcicr = CGU_PCICR;
87
John Crispin287e3f32012-04-17 15:53:19 +020088/* legacy function kept alive to ease clkdev transition */
89void ltq_pmu_enable(unsigned int module)
90{
91 int err = 1000000;
92
93 pmu_w32(pmu_r32(PMU_PWDCR) & ~module, PMU_PWDCR);
94 do {} while (--err && (pmu_r32(PMU_PWDSR) & module));
95
96 if (!err)
97 panic("activating PMU module failed!");
98}
99EXPORT_SYMBOL(ltq_pmu_enable);
100
101/* legacy function kept alive to ease clkdev transition */
102void ltq_pmu_disable(unsigned int module)
103{
104 pmu_w32(pmu_r32(PMU_PWDCR) | module, PMU_PWDCR);
105}
106EXPORT_SYMBOL(ltq_pmu_disable);
107
108/* enable a hw clock */
109static int cgu_enable(struct clk *clk)
110{
John Crispine29b72f2012-07-22 08:55:57 +0200111 ltq_cgu_w32(ltq_cgu_r32(ifccr) | clk->bits, ifccr);
John Crispin287e3f32012-04-17 15:53:19 +0200112 return 0;
113}
114
115/* disable a hw clock */
116static void cgu_disable(struct clk *clk)
117{
John Crispine29b72f2012-07-22 08:55:57 +0200118 ltq_cgu_w32(ltq_cgu_r32(ifccr) & ~clk->bits, ifccr);
John Crispin287e3f32012-04-17 15:53:19 +0200119}
120
121/* enable a clock gate */
122static int pmu_enable(struct clk *clk)
123{
124 int retry = 1000000;
125
126 pmu_w32(pmu_r32(PWDCR(clk->module)) & ~clk->bits,
127 PWDCR(clk->module));
128 do {} while (--retry && (pmu_r32(PWDSR(clk->module)) & clk->bits));
129
130 if (!retry)
131 panic("activating PMU module failed!\n");
132
133 return 0;
134}
135
136/* disable a clock gate */
137static void pmu_disable(struct clk *clk)
138{
139 pmu_w32(pmu_r32(PWDCR(clk->module)) | clk->bits,
140 PWDCR(clk->module));
141}
142
143/* the pci enable helper */
144static int pci_enable(struct clk *clk)
145{
John Crispine29b72f2012-07-22 08:55:57 +0200146 unsigned int val = ltq_cgu_r32(ifccr);
John Crispin287e3f32012-04-17 15:53:19 +0200147 /* set bus clock speed */
148 if (of_machine_is_compatible("lantiq,ar9")) {
John Crispine29b72f2012-07-22 08:55:57 +0200149 val &= ~0x1f00000;
John Crispin287e3f32012-04-17 15:53:19 +0200150 if (clk->rate == CLOCK_33M)
John Crispine29b72f2012-07-22 08:55:57 +0200151 val |= 0xe00000;
John Crispin287e3f32012-04-17 15:53:19 +0200152 else
John Crispine29b72f2012-07-22 08:55:57 +0200153 val |= 0x700000; /* 62.5M */
John Crispin287e3f32012-04-17 15:53:19 +0200154 } else {
John Crispine29b72f2012-07-22 08:55:57 +0200155 val &= ~0xf00000;
John Crispin287e3f32012-04-17 15:53:19 +0200156 if (clk->rate == CLOCK_33M)
John Crispine29b72f2012-07-22 08:55:57 +0200157 val |= 0x800000;
John Crispin287e3f32012-04-17 15:53:19 +0200158 else
John Crispine29b72f2012-07-22 08:55:57 +0200159 val |= 0x400000; /* 62.5M */
John Crispin287e3f32012-04-17 15:53:19 +0200160 }
John Crispine29b72f2012-07-22 08:55:57 +0200161 ltq_cgu_w32(val, ifccr);
John Crispin287e3f32012-04-17 15:53:19 +0200162 pmu_enable(clk);
163 return 0;
164}
165
166/* enable the external clock as a source */
167static int pci_ext_enable(struct clk *clk)
168{
John Crispine29b72f2012-07-22 08:55:57 +0200169 ltq_cgu_w32(ltq_cgu_r32(ifccr) & ~(1 << 16), ifccr);
170 ltq_cgu_w32((1 << 30), pcicr);
John Crispin287e3f32012-04-17 15:53:19 +0200171 return 0;
172}
173
174/* disable the external clock as a source */
175static void pci_ext_disable(struct clk *clk)
176{
John Crispine29b72f2012-07-22 08:55:57 +0200177 ltq_cgu_w32(ltq_cgu_r32(ifccr) | (1 << 16), ifccr);
178 ltq_cgu_w32((1 << 31) | (1 << 30), pcicr);
John Crispin287e3f32012-04-17 15:53:19 +0200179}
180
181/* enable a clockout source */
182static int clkout_enable(struct clk *clk)
183{
184 int i;
185
186 /* get the correct rate */
187 for (i = 0; i < 4; i++) {
188 if (clk->rates[i] == clk->rate) {
189 int shift = 14 - (2 * clk->module);
John Crispin98dbc572012-07-24 08:56:41 +0200190 int enable = 7 - clk->module;
John Crispine29b72f2012-07-22 08:55:57 +0200191 unsigned int val = ltq_cgu_r32(ifccr);
John Crispin287e3f32012-04-17 15:53:19 +0200192
John Crispine29b72f2012-07-22 08:55:57 +0200193 val &= ~(3 << shift);
194 val |= i << shift;
John Crispin98dbc572012-07-24 08:56:41 +0200195 val |= enable;
John Crispine29b72f2012-07-22 08:55:57 +0200196 ltq_cgu_w32(val, ifccr);
John Crispin287e3f32012-04-17 15:53:19 +0200197 return 0;
198 }
199 }
200 return -1;
201}
202
203/* manage the clock gates via PMU */
204static void clkdev_add_pmu(const char *dev, const char *con,
205 unsigned int module, unsigned int bits)
206{
207 struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
208
209 clk->cl.dev_id = dev;
210 clk->cl.con_id = con;
211 clk->cl.clk = clk;
212 clk->enable = pmu_enable;
213 clk->disable = pmu_disable;
214 clk->module = module;
215 clk->bits = bits;
216 clkdev_add(&clk->cl);
217}
218
219/* manage the clock generator */
220static void clkdev_add_cgu(const char *dev, const char *con,
221 unsigned int bits)
222{
223 struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
224
225 clk->cl.dev_id = dev;
226 clk->cl.con_id = con;
227 clk->cl.clk = clk;
228 clk->enable = cgu_enable;
229 clk->disable = cgu_disable;
230 clk->bits = bits;
231 clkdev_add(&clk->cl);
232}
233
234/* pci needs its own enable function as the setup is a bit more complex */
235static unsigned long valid_pci_rates[] = {CLOCK_33M, CLOCK_62_5M, 0};
236
237static void clkdev_add_pci(void)
238{
239 struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
240 struct clk *clk_ext = kzalloc(sizeof(struct clk), GFP_KERNEL);
241
242 /* main pci clock */
243 clk->cl.dev_id = "17000000.pci";
244 clk->cl.con_id = NULL;
245 clk->cl.clk = clk;
246 clk->rate = CLOCK_33M;
247 clk->rates = valid_pci_rates;
248 clk->enable = pci_enable;
249 clk->disable = pmu_disable;
250 clk->module = 0;
251 clk->bits = PMU_PCI;
252 clkdev_add(&clk->cl);
253
254 /* use internal/external bus clock */
255 clk_ext->cl.dev_id = "17000000.pci";
256 clk_ext->cl.con_id = "external";
257 clk_ext->cl.clk = clk_ext;
258 clk_ext->enable = pci_ext_enable;
259 clk_ext->disable = pci_ext_disable;
260 clkdev_add(&clk_ext->cl);
261}
262
263/* xway socs can generate clocks on gpio pins */
264static unsigned long valid_clkout_rates[4][5] = {
265 {CLOCK_32_768K, CLOCK_1_536M, CLOCK_2_5M, CLOCK_12M, 0},
266 {CLOCK_40M, CLOCK_12M, CLOCK_24M, CLOCK_48M, 0},
267 {CLOCK_25M, CLOCK_40M, CLOCK_30M, CLOCK_60M, 0},
268 {CLOCK_12M, CLOCK_50M, CLOCK_32_768K, CLOCK_25M, 0},
269};
270
271static void clkdev_add_clkout(void)
272{
273 int i;
274
275 for (i = 0; i < 4; i++) {
276 struct clk *clk;
277 char *name;
278
279 name = kzalloc(sizeof("clkout0"), GFP_KERNEL);
280 sprintf(name, "clkout%d", i);
281
282 clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
283 clk->cl.dev_id = "1f103000.cgu";
284 clk->cl.con_id = name;
285 clk->cl.clk = clk;
286 clk->rate = 0;
287 clk->rates = valid_clkout_rates[i];
288 clk->enable = clkout_enable;
289 clk->module = i;
290 clkdev_add(&clk->cl);
291 }
292}
293
294/* bring up all register ranges that we need for basic system control */
295void __init ltq_soc_init(void)
296{
297 struct resource res_pmu, res_cgu, res_ebu;
298 struct device_node *np_pmu =
299 of_find_compatible_node(NULL, NULL, "lantiq,pmu-xway");
300 struct device_node *np_cgu =
301 of_find_compatible_node(NULL, NULL, "lantiq,cgu-xway");
302 struct device_node *np_ebu =
303 of_find_compatible_node(NULL, NULL, "lantiq,ebu-xway");
304
305 /* check if all the core register ranges are available */
306 if (!np_pmu || !np_cgu || !np_ebu)
307 panic("Failed to load core nodess from devicetree");
308
309 if (of_address_to_resource(np_pmu, 0, &res_pmu) ||
310 of_address_to_resource(np_cgu, 0, &res_cgu) ||
311 of_address_to_resource(np_ebu, 0, &res_ebu))
312 panic("Failed to get core resources");
313
314 if ((request_mem_region(res_pmu.start, resource_size(&res_pmu),
315 res_pmu.name) < 0) ||
316 (request_mem_region(res_cgu.start, resource_size(&res_cgu),
317 res_cgu.name) < 0) ||
318 (request_mem_region(res_ebu.start, resource_size(&res_ebu),
319 res_ebu.name) < 0))
320 pr_err("Failed to request core reources");
321
322 pmu_membase = ioremap_nocache(res_pmu.start, resource_size(&res_pmu));
323 ltq_cgu_membase = ioremap_nocache(res_cgu.start,
324 resource_size(&res_cgu));
325 ltq_ebu_membase = ioremap_nocache(res_ebu.start,
326 resource_size(&res_ebu));
327 if (!pmu_membase || !ltq_cgu_membase || !ltq_ebu_membase)
328 panic("Failed to remap core resources");
329
330 /* make sure to unprotect the memory region where flash is located */
331 ltq_ebu_w32(ltq_ebu_r32(LTQ_EBU_BUSCON0) & ~EBU_WRDIS, LTQ_EBU_BUSCON0);
332
333 /* add our generic xway clocks */
334 clkdev_add_pmu("10000000.fpi", NULL, 0, PMU_FPI);
335 clkdev_add_pmu("1e100400.serial", NULL, 0, PMU_ASC0);
336 clkdev_add_pmu("1e100a00.gptu", NULL, 0, PMU_GPT);
337 clkdev_add_pmu("1e100bb0.stp", NULL, 0, PMU_STP);
338 clkdev_add_pmu("1e104100.dma", NULL, 0, PMU_DMA);
339 clkdev_add_pmu("1e100800.spi", NULL, 0, PMU_SPI);
340 clkdev_add_pmu("1e105300.ebu", NULL, 0, PMU_EBU);
341 clkdev_add_clkout();
342
343 /* add the soc dependent clocks */
John Crispine29b72f2012-07-22 08:55:57 +0200344 if (of_machine_is_compatible("lantiq,vr9")) {
345 ifccr = CGU_IFCCR_VR9;
346 pcicr = CGU_PCICR_VR9;
347 } else {
John Crispin287e3f32012-04-17 15:53:19 +0200348 clkdev_add_pmu("1e180000.etop", NULL, 0, PMU_PPE);
John Crispine29b72f2012-07-22 08:55:57 +0200349 }
John Crispin287e3f32012-04-17 15:53:19 +0200350
351 if (!of_machine_is_compatible("lantiq,ase")) {
352 clkdev_add_pmu("1e100c00.serial", NULL, 0, PMU_ASC1);
353 clkdev_add_pci();
354 }
355
356 if (of_machine_is_compatible("lantiq,ase")) {
357 if (ltq_cgu_r32(CGU_SYS) & (1 << 5))
358 clkdev_add_static(CLOCK_266M, CLOCK_133M, CLOCK_133M);
359 else
360 clkdev_add_static(CLOCK_133M, CLOCK_133M, CLOCK_133M);
361 clkdev_add_cgu("1e180000.etop", "ephycgu", CGU_EPHY),
362 clkdev_add_pmu("1e180000.etop", "ephy", 0, PMU_EPHY);
363 } else if (of_machine_is_compatible("lantiq,vr9")) {
364 clkdev_add_static(ltq_vr9_cpu_hz(), ltq_vr9_fpi_hz(),
365 ltq_vr9_fpi_hz());
366 clkdev_add_pmu("1d900000.pcie", "phy", 1, PMU1_PCIE_PHY);
367 clkdev_add_pmu("1d900000.pcie", "bus", 0, PMU_PCIE_CLK);
368 clkdev_add_pmu("1d900000.pcie", "msi", 1, PMU1_PCIE_MSI);
369 clkdev_add_pmu("1d900000.pcie", "pdi", 1, PMU1_PCIE_PDI);
370 clkdev_add_pmu("1d900000.pcie", "ctl", 1, PMU1_PCIE_CTL);
371 clkdev_add_pmu("1d900000.pcie", "ahb", 0, PMU_AHBM | PMU_AHBS);
372 } else if (of_machine_is_compatible("lantiq,ar9")) {
373 clkdev_add_static(ltq_ar9_cpu_hz(), ltq_ar9_fpi_hz(),
374 ltq_ar9_fpi_hz());
375 clkdev_add_pmu("1e180000.etop", "switch", 0, PMU_SWITCH);
376 } else {
377 clkdev_add_static(ltq_danube_cpu_hz(), ltq_danube_fpi_hz(),
378 ltq_danube_fpi_hz());
379 }
380}