2 * Copyright 2003-2011 NetLogic Microsystems, Inc. (NetLogic). All rights
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the NetLogic
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * THIS SOFTWARE IS PROVIDED BY NETLOGIC ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL NETLOGIC OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/types.h>
36 #include <linux/kernel.h>
38 #include <linux/delay.h>
40 #include <asm/mipsregs.h>
43 #include <asm/netlogic/common.h>
44 #include <asm/netlogic/haldefs.h>
45 #include <asm/netlogic/xlp-hal/iomap.h>
46 #include <asm/netlogic/xlp-hal/xlp.h>
47 #include <asm/netlogic/xlp-hal/bridge.h>
48 #include <asm/netlogic/xlp-hal/pic.h>
49 #include <asm/netlogic/xlp-hal/sys.h>
51 /* Main initialization */
52 void nlm_node_init(int node
)
54 struct nlm_soc_info
*nodep
;
56 nodep
= nlm_get_node(node
);
58 nodep
->coremask
= 1; /* node 0, boot cpu */
59 nodep
->sysbase
= nlm_get_sys_regbase(node
);
60 nodep
->picbase
= nlm_get_pic_regbase(node
);
61 nodep
->ebase
= read_c0_ebase() & (~((1 << 12) - 1));
63 nodep
->socbus
= xlp9xx_get_socbus(node
);
66 spin_lock_init(&nodep
->piclock
);
69 static int xlp9xx_irq_to_irt(int irq
)
74 case PIC_9XX_XHCI_0_IRQ
:
76 case PIC_9XX_XHCI_1_IRQ
:
88 case PIC_PCIE_LINK_LEGACY_IRQ(0):
89 case PIC_PCIE_LINK_LEGACY_IRQ(1):
90 case PIC_PCIE_LINK_LEGACY_IRQ(2):
91 case PIC_PCIE_LINK_LEGACY_IRQ(3):
92 return 191 + irq
- PIC_PCIE_LINK_LEGACY_IRQ_BASE
;
97 static int xlp_irq_to_irt(int irq
)
105 devoff
= XLP_IO_UART0_OFFSET(0);
108 devoff
= XLP_IO_UART1_OFFSET(0);
111 devoff
= XLP_IO_MMC_OFFSET(0);
113 case PIC_I2C_0_IRQ
: /* I2C will be fixed up */
118 devoff
= XLP2XX_IO_I2C_OFFSET(0);
120 devoff
= XLP_IO_I2C0_OFFSET(0);
123 devoff
= XLP_IO_SATA_OFFSET(0);
126 devoff
= XLP_IO_GPIO_OFFSET(0);
129 devoff
= XLP_IO_NAND_OFFSET(0);
132 devoff
= XLP_IO_SPI_OFFSET(0);
135 if (cpu_is_xlpii()) {
137 /* XLP2XX has three XHCI USB controller */
138 case PIC_2XX_XHCI_0_IRQ
:
139 devoff
= XLP2XX_IO_USB_XHCI0_OFFSET(0);
141 case PIC_2XX_XHCI_1_IRQ
:
142 devoff
= XLP2XX_IO_USB_XHCI1_OFFSET(0);
144 case PIC_2XX_XHCI_2_IRQ
:
145 devoff
= XLP2XX_IO_USB_XHCI2_OFFSET(0);
151 devoff
= XLP_IO_USB_EHCI0_OFFSET(0);
154 devoff
= XLP_IO_USB_EHCI1_OFFSET(0);
157 devoff
= XLP_IO_USB_OHCI0_OFFSET(0);
160 devoff
= XLP_IO_USB_OHCI1_OFFSET(0);
163 devoff
= XLP_IO_USB_OHCI2_OFFSET(0);
166 devoff
= XLP_IO_USB_OHCI3_OFFSET(0);
173 pcibase
= nlm_pcicfg_base(devoff
);
174 irt
= nlm_read_reg(pcibase
, XLP_PCI_IRTINFO_REG
) & 0xffff;
175 /* HW weirdness, I2C IRT entry has to be fixed up */
178 irt
= irt
+ 1; break;
180 irt
= irt
+ 2; break;
182 irt
= irt
+ 3; break;
184 } else if (irq
>= PIC_PCIE_LINK_LEGACY_IRQ(0) &&
185 irq
<= PIC_PCIE_LINK_LEGACY_IRQ(3)) {
186 /* HW bug, PCI IRT entries are bad on early silicon, fix */
187 irt
= PIC_IRT_PCIE_LINK_INDEX(irq
-
188 PIC_PCIE_LINK_LEGACY_IRQ_BASE
);
195 int nlm_irq_to_irt(int irq
)
197 /* return -2 for irqs without 1-1 mapping */
198 if (irq
>= PIC_PCIE_LINK_MSI_IRQ(0) && irq
<= PIC_PCIE_LINK_MSI_IRQ(3))
200 if (irq
>= PIC_PCIE_MSIX_IRQ(0) && irq
<= PIC_PCIE_MSIX_IRQ(3))
204 return xlp9xx_irq_to_irt(irq
);
206 return xlp_irq_to_irt(irq
);
209 static unsigned int nlm_xlp2_get_core_frequency(int node
, int core
)
211 unsigned int pll_post_div
, ctrl_val0
, ctrl_val1
, denom
;
212 uint64_t num
, sysbase
, clockbase
;
214 if (cpu_is_xlp9xx()) {
215 clockbase
= nlm_get_clock_regbase(node
);
216 ctrl_val0
= nlm_read_sys_reg(clockbase
,
217 SYS_9XX_CPU_PLL_CTRL0(core
));
218 ctrl_val1
= nlm_read_sys_reg(clockbase
,
219 SYS_9XX_CPU_PLL_CTRL1(core
));
221 sysbase
= nlm_get_node(node
)->sysbase
;
222 ctrl_val0
= nlm_read_sys_reg(sysbase
,
223 SYS_CPU_PLL_CTRL0(core
));
224 ctrl_val1
= nlm_read_sys_reg(sysbase
,
225 SYS_CPU_PLL_CTRL1(core
));
228 /* Find PLL post divider value */
229 switch ((ctrl_val0
>> 24) & 0x7) {
248 num
= 1000000ULL * (400 * 3 + 100 * (ctrl_val1
& 0x3f));
249 denom
= 3 * pll_post_div
;
252 return (unsigned int)num
;
255 static unsigned int nlm_xlp_get_core_frequency(int node
, int core
)
257 unsigned int pll_divf
, pll_divr
, dfs_div
, ext_div
;
258 unsigned int rstval
, dfsval
, denom
;
259 uint64_t num
, sysbase
;
261 sysbase
= nlm_get_node(node
)->sysbase
;
262 rstval
= nlm_read_sys_reg(sysbase
, SYS_POWER_ON_RESET_CFG
);
263 dfsval
= nlm_read_sys_reg(sysbase
, SYS_CORE_DFS_DIV_VALUE
);
264 pll_divf
= ((rstval
>> 10) & 0x7f) + 1;
265 pll_divr
= ((rstval
>> 8) & 0x3) + 1;
266 ext_div
= ((rstval
>> 30) & 0x3) + 1;
267 dfs_div
= ((dfsval
>> (core
* 4)) & 0xf) + 1;
269 num
= 800000000ULL * pll_divf
;
270 denom
= 3 * pll_divr
* ext_div
* dfs_div
;
273 return (unsigned int)num
;
276 unsigned int nlm_get_core_frequency(int node
, int core
)
279 return nlm_xlp2_get_core_frequency(node
, core
);
281 return nlm_xlp_get_core_frequency(node
, core
);
285 * Calculate PIC frequency from PLL registers.
286 * freq_out = (ref_freq/2 * (6 + ctrl2[7:0]) + ctrl2[20:8]/2^13) /
287 * ((2^ctrl0[7:5]) * Table(ctrl0[26:24]))
289 static unsigned int nlm_xlp2_get_pic_frequency(int node
)
291 u32 ctrl_val0
, ctrl_val2
, vco_post_div
, pll_post_div
, cpu_xlp9xx
;
292 u32 mdiv
, fdiv
, pll_out_freq_den
, reg_select
, ref_div
, pic_div
;
293 u64 sysbase
, pll_out_freq_num
, ref_clk_select
, clockbase
, ref_clk
;
295 sysbase
= nlm_get_node(node
)->sysbase
;
296 clockbase
= nlm_get_clock_regbase(node
);
297 cpu_xlp9xx
= cpu_is_xlp9xx();
299 /* Find ref_clk_base */
301 ref_clk_select
= (nlm_read_sys_reg(sysbase
,
302 SYS_9XX_POWER_ON_RESET_CFG
) >> 18) & 0x3;
304 ref_clk_select
= (nlm_read_sys_reg(sysbase
,
305 SYS_POWER_ON_RESET_CFG
) >> 18) & 0x3;
306 switch (ref_clk_select
) {
308 ref_clk
= 200000000ULL;
312 ref_clk
= 100000000ULL;
316 ref_clk
= 125000000ULL;
320 ref_clk
= 400000000ULL;
325 /* Find the clock source PLL device for PIC */
327 reg_select
= nlm_read_sys_reg(clockbase
,
328 SYS_9XX_CLK_DEV_SEL
) & 0x3;
329 switch (reg_select
) {
331 ctrl_val0
= nlm_read_sys_reg(clockbase
,
333 ctrl_val2
= nlm_read_sys_reg(clockbase
,
337 ctrl_val0
= nlm_read_sys_reg(clockbase
,
338 SYS_9XX_PLL_CTRL0_DEVX(0));
339 ctrl_val2
= nlm_read_sys_reg(clockbase
,
340 SYS_9XX_PLL_CTRL2_DEVX(0));
343 ctrl_val0
= nlm_read_sys_reg(clockbase
,
344 SYS_9XX_PLL_CTRL0_DEVX(1));
345 ctrl_val2
= nlm_read_sys_reg(clockbase
,
346 SYS_9XX_PLL_CTRL2_DEVX(1));
349 ctrl_val0
= nlm_read_sys_reg(clockbase
,
350 SYS_9XX_PLL_CTRL0_DEVX(2));
351 ctrl_val2
= nlm_read_sys_reg(clockbase
,
352 SYS_9XX_PLL_CTRL2_DEVX(2));
356 reg_select
= (nlm_read_sys_reg(sysbase
,
357 SYS_CLK_DEV_SEL
) >> 22) & 0x3;
358 switch (reg_select
) {
360 ctrl_val0
= nlm_read_sys_reg(sysbase
,
362 ctrl_val2
= nlm_read_sys_reg(sysbase
,
366 ctrl_val0
= nlm_read_sys_reg(sysbase
,
367 SYS_PLL_CTRL0_DEVX(0));
368 ctrl_val2
= nlm_read_sys_reg(sysbase
,
369 SYS_PLL_CTRL2_DEVX(0));
372 ctrl_val0
= nlm_read_sys_reg(sysbase
,
373 SYS_PLL_CTRL0_DEVX(1));
374 ctrl_val2
= nlm_read_sys_reg(sysbase
,
375 SYS_PLL_CTRL2_DEVX(1));
378 ctrl_val0
= nlm_read_sys_reg(sysbase
,
379 SYS_PLL_CTRL0_DEVX(2));
380 ctrl_val2
= nlm_read_sys_reg(sysbase
,
381 SYS_PLL_CTRL2_DEVX(2));
386 vco_post_div
= (ctrl_val0
>> 5) & 0x7;
387 pll_post_div
= (ctrl_val0
>> 24) & 0x7;
388 mdiv
= ctrl_val2
& 0xff;
389 fdiv
= (ctrl_val2
>> 8) & 0x1fff;
391 /* Find PLL post divider value */
392 switch (pll_post_div
) {
411 fdiv
= fdiv
/(1 << 13);
412 pll_out_freq_num
= ((ref_clk
>> 1) * (6 + mdiv
)) + fdiv
;
413 pll_out_freq_den
= (1 << vco_post_div
) * pll_post_div
* ref_div
;
415 if (pll_out_freq_den
> 0)
416 do_div(pll_out_freq_num
, pll_out_freq_den
);
418 /* PIC post divider, which happens after PLL */
420 pic_div
= nlm_read_sys_reg(clockbase
,
421 SYS_9XX_CLK_DEV_DIV
) & 0x3;
423 pic_div
= (nlm_read_sys_reg(sysbase
,
424 SYS_CLK_DEV_DIV
) >> 22) & 0x3;
425 do_div(pll_out_freq_num
, 1 << pic_div
);
427 return pll_out_freq_num
;
430 unsigned int nlm_get_pic_frequency(int node
)
433 return nlm_xlp2_get_pic_frequency(node
);
438 unsigned int nlm_get_cpu_frequency(void)
440 return nlm_get_core_frequency(0, 0);
444 * Fills upto 8 pairs of entries containing the DRAM map of a node
445 * if n < 0, get dram map for all nodes
447 int xlp_get_dram_map(int n
, uint64_t *dram_map
)
449 uint64_t bridgebase
, base
, lim
;
451 unsigned int barreg
, limreg
, xlatreg
;
454 /* Look only at mapping on Node 0, we don't handle crazy configs */
455 bridgebase
= nlm_get_bridge_regbase(0);
457 for (i
= 0; i
< 8; i
++) {
458 if (cpu_is_xlp9xx()) {
459 barreg
= BRIDGE_9XX_DRAM_BAR(i
);
460 limreg
= BRIDGE_9XX_DRAM_LIMIT(i
);
461 xlatreg
= BRIDGE_9XX_DRAM_NODE_TRANSLN(i
);
463 barreg
= BRIDGE_DRAM_BAR(i
);
464 limreg
= BRIDGE_DRAM_LIMIT(i
);
465 xlatreg
= BRIDGE_DRAM_NODE_TRANSLN(i
);
468 /* node specified, get node mapping of BAR */
469 val
= nlm_read_bridge_reg(bridgebase
, xlatreg
);
470 node
= (val
>> 1) & 0x3;
474 val
= nlm_read_bridge_reg(bridgebase
, barreg
);
475 val
= (val
>> 12) & 0xfffff;
476 base
= (uint64_t) val
<< 20;
477 val
= nlm_read_bridge_reg(bridgebase
, limreg
);
478 val
= (val
>> 12) & 0xfffff;
479 if (val
== 0) /* BAR not used */
481 lim
= ((uint64_t)val
+ 1) << 20;
483 dram_map
[rv
+ 1] = lim
;