blob: 67c3a91e54e7eb2a2527847453170577b5e5b3ce [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 */
John Crispinf40e1f92012-08-16 08:25:42 +0000148 if (of_machine_is_compatible("lantiq,ar9") ||
149 of_machine_is_compatible("lantiq,vr9")) {
John Crispine29b72f2012-07-22 08:55:57 +0200150 val &= ~0x1f00000;
John Crispin287e3f32012-04-17 15:53:19 +0200151 if (clk->rate == CLOCK_33M)
John Crispine29b72f2012-07-22 08:55:57 +0200152 val |= 0xe00000;
John Crispin287e3f32012-04-17 15:53:19 +0200153 else
John Crispine29b72f2012-07-22 08:55:57 +0200154 val |= 0x700000; /* 62.5M */
John Crispin287e3f32012-04-17 15:53:19 +0200155 } else {
John Crispine29b72f2012-07-22 08:55:57 +0200156 val &= ~0xf00000;
John Crispin287e3f32012-04-17 15:53:19 +0200157 if (clk->rate == CLOCK_33M)
John Crispine29b72f2012-07-22 08:55:57 +0200158 val |= 0x800000;
John Crispin287e3f32012-04-17 15:53:19 +0200159 else
John Crispine29b72f2012-07-22 08:55:57 +0200160 val |= 0x400000; /* 62.5M */
John Crispin287e3f32012-04-17 15:53:19 +0200161 }
John Crispine29b72f2012-07-22 08:55:57 +0200162 ltq_cgu_w32(val, ifccr);
John Crispin287e3f32012-04-17 15:53:19 +0200163 pmu_enable(clk);
164 return 0;
165}
166
167/* enable the external clock as a source */
168static int pci_ext_enable(struct clk *clk)
169{
John Crispine29b72f2012-07-22 08:55:57 +0200170 ltq_cgu_w32(ltq_cgu_r32(ifccr) & ~(1 << 16), ifccr);
171 ltq_cgu_w32((1 << 30), pcicr);
John Crispin287e3f32012-04-17 15:53:19 +0200172 return 0;
173}
174
175/* disable the external clock as a source */
176static void pci_ext_disable(struct clk *clk)
177{
John Crispine29b72f2012-07-22 08:55:57 +0200178 ltq_cgu_w32(ltq_cgu_r32(ifccr) | (1 << 16), ifccr);
179 ltq_cgu_w32((1 << 31) | (1 << 30), pcicr);
John Crispin287e3f32012-04-17 15:53:19 +0200180}
181
182/* enable a clockout source */
183static int clkout_enable(struct clk *clk)
184{
185 int i;
186
187 /* get the correct rate */
188 for (i = 0; i < 4; i++) {
189 if (clk->rates[i] == clk->rate) {
190 int shift = 14 - (2 * 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;
195 ltq_cgu_w32(val, ifccr);
John Crispin287e3f32012-04-17 15:53:19 +0200196 return 0;
197 }
198 }
199 return -1;
200}
201
202/* manage the clock gates via PMU */
203static void clkdev_add_pmu(const char *dev, const char *con,
204 unsigned int module, unsigned int bits)
205{
206 struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
207
208 clk->cl.dev_id = dev;
209 clk->cl.con_id = con;
210 clk->cl.clk = clk;
211 clk->enable = pmu_enable;
212 clk->disable = pmu_disable;
213 clk->module = module;
214 clk->bits = bits;
215 clkdev_add(&clk->cl);
216}
217
218/* manage the clock generator */
219static void clkdev_add_cgu(const char *dev, const char *con,
220 unsigned int bits)
221{
222 struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
223
224 clk->cl.dev_id = dev;
225 clk->cl.con_id = con;
226 clk->cl.clk = clk;
227 clk->enable = cgu_enable;
228 clk->disable = cgu_disable;
229 clk->bits = bits;
230 clkdev_add(&clk->cl);
231}
232
233/* pci needs its own enable function as the setup is a bit more complex */
234static unsigned long valid_pci_rates[] = {CLOCK_33M, CLOCK_62_5M, 0};
235
236static void clkdev_add_pci(void)
237{
238 struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
239 struct clk *clk_ext = kzalloc(sizeof(struct clk), GFP_KERNEL);
240
241 /* main pci clock */
242 clk->cl.dev_id = "17000000.pci";
243 clk->cl.con_id = NULL;
244 clk->cl.clk = clk;
245 clk->rate = CLOCK_33M;
246 clk->rates = valid_pci_rates;
247 clk->enable = pci_enable;
248 clk->disable = pmu_disable;
249 clk->module = 0;
250 clk->bits = PMU_PCI;
251 clkdev_add(&clk->cl);
252
253 /* use internal/external bus clock */
254 clk_ext->cl.dev_id = "17000000.pci";
255 clk_ext->cl.con_id = "external";
256 clk_ext->cl.clk = clk_ext;
257 clk_ext->enable = pci_ext_enable;
258 clk_ext->disable = pci_ext_disable;
259 clkdev_add(&clk_ext->cl);
260}
261
262/* xway socs can generate clocks on gpio pins */
263static unsigned long valid_clkout_rates[4][5] = {
264 {CLOCK_32_768K, CLOCK_1_536M, CLOCK_2_5M, CLOCK_12M, 0},
265 {CLOCK_40M, CLOCK_12M, CLOCK_24M, CLOCK_48M, 0},
266 {CLOCK_25M, CLOCK_40M, CLOCK_30M, CLOCK_60M, 0},
267 {CLOCK_12M, CLOCK_50M, CLOCK_32_768K, CLOCK_25M, 0},
268};
269
270static void clkdev_add_clkout(void)
271{
272 int i;
273
274 for (i = 0; i < 4; i++) {
275 struct clk *clk;
276 char *name;
277
278 name = kzalloc(sizeof("clkout0"), GFP_KERNEL);
279 sprintf(name, "clkout%d", i);
280
281 clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
282 clk->cl.dev_id = "1f103000.cgu";
283 clk->cl.con_id = name;
284 clk->cl.clk = clk;
285 clk->rate = 0;
286 clk->rates = valid_clkout_rates[i];
287 clk->enable = clkout_enable;
288 clk->module = i;
289 clkdev_add(&clk->cl);
290 }
291}
292
293/* bring up all register ranges that we need for basic system control */
294void __init ltq_soc_init(void)
295{
296 struct resource res_pmu, res_cgu, res_ebu;
297 struct device_node *np_pmu =
298 of_find_compatible_node(NULL, NULL, "lantiq,pmu-xway");
299 struct device_node *np_cgu =
300 of_find_compatible_node(NULL, NULL, "lantiq,cgu-xway");
301 struct device_node *np_ebu =
302 of_find_compatible_node(NULL, NULL, "lantiq,ebu-xway");
303
304 /* check if all the core register ranges are available */
305 if (!np_pmu || !np_cgu || !np_ebu)
306 panic("Failed to load core nodess from devicetree");
307
308 if (of_address_to_resource(np_pmu, 0, &res_pmu) ||
309 of_address_to_resource(np_cgu, 0, &res_cgu) ||
310 of_address_to_resource(np_ebu, 0, &res_ebu))
311 panic("Failed to get core resources");
312
313 if ((request_mem_region(res_pmu.start, resource_size(&res_pmu),
314 res_pmu.name) < 0) ||
315 (request_mem_region(res_cgu.start, resource_size(&res_cgu),
316 res_cgu.name) < 0) ||
317 (request_mem_region(res_ebu.start, resource_size(&res_ebu),
318 res_ebu.name) < 0))
319 pr_err("Failed to request core reources");
320
321 pmu_membase = ioremap_nocache(res_pmu.start, resource_size(&res_pmu));
322 ltq_cgu_membase = ioremap_nocache(res_cgu.start,
323 resource_size(&res_cgu));
324 ltq_ebu_membase = ioremap_nocache(res_ebu.start,
325 resource_size(&res_ebu));
326 if (!pmu_membase || !ltq_cgu_membase || !ltq_ebu_membase)
327 panic("Failed to remap core resources");
328
329 /* make sure to unprotect the memory region where flash is located */
330 ltq_ebu_w32(ltq_ebu_r32(LTQ_EBU_BUSCON0) & ~EBU_WRDIS, LTQ_EBU_BUSCON0);
331
332 /* add our generic xway clocks */
333 clkdev_add_pmu("10000000.fpi", NULL, 0, PMU_FPI);
334 clkdev_add_pmu("1e100400.serial", NULL, 0, PMU_ASC0);
335 clkdev_add_pmu("1e100a00.gptu", NULL, 0, PMU_GPT);
336 clkdev_add_pmu("1e100bb0.stp", NULL, 0, PMU_STP);
337 clkdev_add_pmu("1e104100.dma", NULL, 0, PMU_DMA);
338 clkdev_add_pmu("1e100800.spi", NULL, 0, PMU_SPI);
339 clkdev_add_pmu("1e105300.ebu", NULL, 0, PMU_EBU);
340 clkdev_add_clkout();
341
342 /* add the soc dependent clocks */
John Crispine29b72f2012-07-22 08:55:57 +0200343 if (of_machine_is_compatible("lantiq,vr9")) {
344 ifccr = CGU_IFCCR_VR9;
345 pcicr = CGU_PCICR_VR9;
346 } else {
John Crispin287e3f32012-04-17 15:53:19 +0200347 clkdev_add_pmu("1e180000.etop", NULL, 0, PMU_PPE);
John Crispine29b72f2012-07-22 08:55:57 +0200348 }
John Crispin287e3f32012-04-17 15:53:19 +0200349
350 if (!of_machine_is_compatible("lantiq,ase")) {
351 clkdev_add_pmu("1e100c00.serial", NULL, 0, PMU_ASC1);
352 clkdev_add_pci();
353 }
354
355 if (of_machine_is_compatible("lantiq,ase")) {
356 if (ltq_cgu_r32(CGU_SYS) & (1 << 5))
357 clkdev_add_static(CLOCK_266M, CLOCK_133M, CLOCK_133M);
358 else
359 clkdev_add_static(CLOCK_133M, CLOCK_133M, CLOCK_133M);
360 clkdev_add_cgu("1e180000.etop", "ephycgu", CGU_EPHY),
361 clkdev_add_pmu("1e180000.etop", "ephy", 0, PMU_EPHY);
362 } else if (of_machine_is_compatible("lantiq,vr9")) {
363 clkdev_add_static(ltq_vr9_cpu_hz(), ltq_vr9_fpi_hz(),
364 ltq_vr9_fpi_hz());
365 clkdev_add_pmu("1d900000.pcie", "phy", 1, PMU1_PCIE_PHY);
366 clkdev_add_pmu("1d900000.pcie", "bus", 0, PMU_PCIE_CLK);
367 clkdev_add_pmu("1d900000.pcie", "msi", 1, PMU1_PCIE_MSI);
368 clkdev_add_pmu("1d900000.pcie", "pdi", 1, PMU1_PCIE_PDI);
369 clkdev_add_pmu("1d900000.pcie", "ctl", 1, PMU1_PCIE_CTL);
370 clkdev_add_pmu("1d900000.pcie", "ahb", 0, PMU_AHBM | PMU_AHBS);
371 } else if (of_machine_is_compatible("lantiq,ar9")) {
372 clkdev_add_static(ltq_ar9_cpu_hz(), ltq_ar9_fpi_hz(),
373 ltq_ar9_fpi_hz());
374 clkdev_add_pmu("1e180000.etop", "switch", 0, PMU_SWITCH);
375 } else {
376 clkdev_add_static(ltq_danube_cpu_hz(), ltq_danube_fpi_hz(),
377 ltq_danube_fpi_hz());
378 }
379}