MIPS: Netlogic: Add MSI support for XLP
[linux-2.6-microblaze.git] / arch / mips / netlogic / xlp / nlm_hal.c
1 /*
2  * Copyright 2003-2011 NetLogic Microsystems, Inc. (NetLogic). All rights
3  * reserved.
4  *
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
9  * license below:
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
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
20  *    distribution.
21  *
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.
33  */
34
35 #include <linux/types.h>
36 #include <linux/kernel.h>
37 #include <linux/mm.h>
38 #include <linux/delay.h>
39
40 #include <asm/mipsregs.h>
41 #include <asm/time.h>
42
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>
50
51 /* Main initialization */
52 void nlm_node_init(int node)
53 {
54         struct nlm_soc_info *nodep;
55
56         nodep = nlm_get_node(node);
57         nodep->sysbase = nlm_get_sys_regbase(node);
58         nodep->picbase = nlm_get_pic_regbase(node);
59         nodep->ebase = read_c0_ebase() & (~((1 << 12) - 1));
60         spin_lock_init(&nodep->piclock);
61 }
62
63 int nlm_irq_to_irt(int irq)
64 {
65         uint64_t pcibase;
66         int devoff, irt;
67
68         devoff = 0;
69         switch (irq) {
70         case PIC_UART_0_IRQ:
71                 devoff = XLP_IO_UART0_OFFSET(0);
72                 break;
73         case PIC_UART_1_IRQ:
74                 devoff = XLP_IO_UART1_OFFSET(0);
75                 break;
76         case PIC_MMC_IRQ:
77                 devoff = XLP_IO_SD_OFFSET(0);
78                 break;
79         case PIC_I2C_0_IRQ:     /* I2C will be fixed up */
80         case PIC_I2C_1_IRQ:
81         case PIC_I2C_2_IRQ:
82         case PIC_I2C_3_IRQ:
83                 if (cpu_is_xlpii())
84                         devoff = XLP2XX_IO_I2C_OFFSET(0);
85                 else
86                         devoff = XLP_IO_I2C0_OFFSET(0);
87                 break;
88         default:
89                 if (cpu_is_xlpii()) {
90                         switch (irq) {
91                                 /* XLP2XX has three XHCI USB controller */
92                         case PIC_2XX_XHCI_0_IRQ:
93                                 devoff = XLP2XX_IO_USB_XHCI0_OFFSET(0);
94                                 break;
95                         case PIC_2XX_XHCI_1_IRQ:
96                                 devoff = XLP2XX_IO_USB_XHCI1_OFFSET(0);
97                                 break;
98                         case PIC_2XX_XHCI_2_IRQ:
99                                 devoff = XLP2XX_IO_USB_XHCI2_OFFSET(0);
100                                 break;
101                         }
102                 } else {
103                         switch (irq) {
104                         case PIC_EHCI_0_IRQ:
105                                 devoff = XLP_IO_USB_EHCI0_OFFSET(0);
106                                 break;
107                         case PIC_EHCI_1_IRQ:
108                                 devoff = XLP_IO_USB_EHCI1_OFFSET(0);
109                                 break;
110                         case PIC_OHCI_0_IRQ:
111                                 devoff = XLP_IO_USB_OHCI0_OFFSET(0);
112                                 break;
113                         case PIC_OHCI_1_IRQ:
114                                 devoff = XLP_IO_USB_OHCI1_OFFSET(0);
115                                 break;
116                         case PIC_OHCI_2_IRQ:
117                                 devoff = XLP_IO_USB_OHCI2_OFFSET(0);
118                                 break;
119                         case PIC_OHCI_3_IRQ:
120                                 devoff = XLP_IO_USB_OHCI3_OFFSET(0);
121                                 break;
122                         }
123                 }
124         }
125
126         if (devoff != 0) {
127                 pcibase = nlm_pcicfg_base(devoff);
128                 irt = nlm_read_reg(pcibase, XLP_PCI_IRTINFO_REG) & 0xffff;
129                 /* HW weirdness, I2C IRT entry has to be fixed up */
130                 switch (irq) {
131                 case PIC_I2C_1_IRQ:
132                         irt = irt + 1; break;
133                 case PIC_I2C_2_IRQ:
134                         irt = irt + 2; break;
135                 case PIC_I2C_3_IRQ:
136                         irt = irt + 3; break;
137                 }
138         } else if (irq >= PIC_PCIE_LINK_LEGACY_IRQ(0) &&
139                         irq <= PIC_PCIE_LINK_LEGACY_IRQ(3)) {
140                 /* HW bug, PCI IRT entries are bad on early silicon, fix */
141                 irt = PIC_IRT_PCIE_LINK_INDEX(irq -
142                                         PIC_PCIE_LINK_LEGACY_IRQ_BASE);
143         } else if (irq >= PIC_PCIE_LINK_MSI_IRQ(0) &&
144                         irq <= PIC_PCIE_LINK_MSI_IRQ(3)) {
145                 irt = -2;
146         } else if (irq >= PIC_PCIE_MSIX_IRQ(0) &&
147                         irq <= PIC_PCIE_MSIX_IRQ(3)) {
148                 irt = -2;
149         } else {
150                 irt = -1;
151         }
152         return irt;
153 }
154
155 unsigned int nlm_get_core_frequency(int node, int core)
156 {
157         unsigned int pll_divf, pll_divr, dfs_div, ext_div;
158         unsigned int rstval, dfsval, denom;
159         uint64_t num, sysbase;
160
161         sysbase = nlm_get_node(node)->sysbase;
162         rstval = nlm_read_sys_reg(sysbase, SYS_POWER_ON_RESET_CFG);
163         if (cpu_is_xlpii()) {
164                 num = 1000000ULL * (400 * 3 + 100 * (rstval >> 26));
165                 denom = 3;
166         } else {
167                 dfsval = nlm_read_sys_reg(sysbase, SYS_CORE_DFS_DIV_VALUE);
168                 pll_divf = ((rstval >> 10) & 0x7f) + 1;
169                 pll_divr = ((rstval >> 8)  & 0x3) + 1;
170                 ext_div  = ((rstval >> 30) & 0x3) + 1;
171                 dfs_div  = ((dfsval >> (core * 4)) & 0xf) + 1;
172
173                 num = 800000000ULL * pll_divf;
174                 denom = 3 * pll_divr * ext_div * dfs_div;
175         }
176         do_div(num, denom);
177         return (unsigned int)num;
178 }
179
180 /* Calculate Frequency to the PIC from PLL.
181  * freq_out = ( ref_freq/2 * (6 + ctrl2[7:0]) + ctrl2[20:8]/2^13 ) /
182  * ((2^ctrl0[7:5]) * Table(ctrl0[26:24]))
183  */
184 static unsigned int nlm_2xx_get_pic_frequency(int node)
185 {
186         u32 ctrl_val0, ctrl_val2, vco_post_div, pll_post_div;
187         u32 mdiv, fdiv, pll_out_freq_den, reg_select, ref_div, pic_div;
188         u64 ref_clk, sysbase, pll_out_freq_num, ref_clk_select;
189
190         sysbase = nlm_get_node(node)->sysbase;
191
192         /* Find ref_clk_base */
193         ref_clk_select =
194                 (nlm_read_sys_reg(sysbase, SYS_POWER_ON_RESET_CFG) >> 18) & 0x3;
195         switch (ref_clk_select) {
196         case 0:
197                 ref_clk = 200000000ULL;
198                 ref_div = 3;
199                 break;
200         case 1:
201                 ref_clk = 100000000ULL;
202                 ref_div = 1;
203                 break;
204         case 2:
205                 ref_clk = 125000000ULL;
206                 ref_div = 1;
207                 break;
208         case 3:
209                 ref_clk = 400000000ULL;
210                 ref_div = 3;
211                 break;
212         }
213
214         /* Find the clock source PLL device for PIC */
215         reg_select = (nlm_read_sys_reg(sysbase, SYS_CLK_DEV_SEL) >> 22) & 0x3;
216         switch (reg_select) {
217         case 0:
218                 ctrl_val0 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL0);
219                 ctrl_val2 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL2);
220                 break;
221         case 1:
222                 ctrl_val0 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL0_DEVX(0));
223                 ctrl_val2 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL2_DEVX(0));
224                 break;
225         case 2:
226                 ctrl_val0 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL0_DEVX(1));
227                 ctrl_val2 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL2_DEVX(1));
228                 break;
229         case 3:
230                 ctrl_val0 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL0_DEVX(2));
231                 ctrl_val2 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL2_DEVX(2));
232                 break;
233         }
234
235         vco_post_div = (ctrl_val0 >> 5) & 0x7;
236         pll_post_div = (ctrl_val0 >> 24) & 0x7;
237         mdiv = ctrl_val2 & 0xff;
238         fdiv = (ctrl_val2 >> 8) & 0xfff;
239
240         /* Find PLL post divider value */
241         switch (pll_post_div) {
242         case 1:
243                 pll_post_div = 2;
244                 break;
245         case 3:
246                 pll_post_div = 4;
247                 break;
248         case 7:
249                 pll_post_div = 8;
250                 break;
251         case 6:
252                 pll_post_div = 16;
253                 break;
254         case 0:
255         default:
256                 pll_post_div = 1;
257                 break;
258         }
259
260         fdiv = fdiv/(1 << 13);
261         pll_out_freq_num = ((ref_clk >> 1) * (6 + mdiv)) + fdiv;
262         pll_out_freq_den = (1 << vco_post_div) * pll_post_div * 3;
263
264         if (pll_out_freq_den > 0)
265                 do_div(pll_out_freq_num, pll_out_freq_den);
266
267         /* PIC post divider, which happens after PLL */
268         pic_div = (nlm_read_sys_reg(sysbase, SYS_CLK_DEV_DIV) >> 22) & 0x3;
269         do_div(pll_out_freq_num, 1 << pic_div);
270
271         return pll_out_freq_num;
272 }
273
274 unsigned int nlm_get_pic_frequency(int node)
275 {
276         if (cpu_is_xlpii())
277                 return nlm_2xx_get_pic_frequency(node);
278         else
279                 return 133333333;
280 }
281
282 unsigned int nlm_get_cpu_frequency(void)
283 {
284         return nlm_get_core_frequency(0, 0);
285 }
286
287 /*
288  * Fills upto 8 pairs of entries containing the DRAM map of a node
289  * if n < 0, get dram map for all nodes
290  */
291 int xlp_get_dram_map(int n, uint64_t *dram_map)
292 {
293         uint64_t bridgebase, base, lim;
294         uint32_t val;
295         int i, node, rv;
296
297         /* Look only at mapping on Node 0, we don't handle crazy configs */
298         bridgebase = nlm_get_bridge_regbase(0);
299         rv = 0;
300         for (i = 0; i < 8; i++) {
301                 val = nlm_read_bridge_reg(bridgebase,
302                                         BRIDGE_DRAM_NODE_TRANSLN(i));
303                 node = (val >> 1) & 0x3;
304                 if (n >= 0 && n != node)
305                         continue;
306                 val = nlm_read_bridge_reg(bridgebase, BRIDGE_DRAM_BAR(i));
307                 val = (val >>  12) & 0xfffff;
308                 base = (uint64_t) val << 20;
309                 val = nlm_read_bridge_reg(bridgebase, BRIDGE_DRAM_LIMIT(i));
310                 val = (val >>  12) & 0xfffff;
311                 if (val == 0)   /* BAR not used */
312                         continue;
313                 lim = ((uint64_t)val + 1) << 20;
314                 dram_map[rv] = base;
315                 dram_map[rv + 1] = lim;
316                 rv += 2;
317         }
318         return rv;
319 }