blob: 5f191f54f9c0051d438716bbb0ba22096458df98 [file] [log] [blame]
Jayachandran C65040e22011-11-16 00:21:28 +00001/*
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
Jayachandran C77ae7982012-10-31 12:01:39 +000043#include <asm/netlogic/common.h>
Jayachandran C65040e22011-11-16 00:21:28 +000044#include <asm/netlogic/haldefs.h>
45#include <asm/netlogic/xlp-hal/iomap.h>
46#include <asm/netlogic/xlp-hal/xlp.h>
Jayachandran Ca2ba6cd2013-08-21 19:31:29 +053047#include <asm/netlogic/xlp-hal/bridge.h>
Jayachandran C65040e22011-11-16 00:21:28 +000048#include <asm/netlogic/xlp-hal/pic.h>
49#include <asm/netlogic/xlp-hal/sys.h>
50
Jayachandran C65040e22011-11-16 00:21:28 +000051/* Main initialization */
Jayachandran C77ae7982012-10-31 12:01:39 +000052void nlm_node_init(int node)
Jayachandran C65040e22011-11-16 00:21:28 +000053{
Jayachandran C77ae7982012-10-31 12:01:39 +000054 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));
Jayachandran C5513c762013-12-21 16:52:21 +053060 if (cpu_is_xlp9xx())
61 nodep->socbus = xlp9xx_get_socbus(node);
62 else
63 nodep->socbus = 0;
Jayachandran C77ae7982012-10-31 12:01:39 +000064 spin_lock_init(&nodep->piclock);
Jayachandran C65040e22011-11-16 00:21:28 +000065}
66
67int nlm_irq_to_irt(int irq)
68{
Jayachandran C3c0553e2013-03-23 17:27:56 +000069 uint64_t pcibase;
70 int devoff, irt;
Jayachandran C65040e22011-11-16 00:21:28 +000071
Ganesan Ramalingam9eac3592013-08-21 19:32:41 +053072 devoff = 0;
Jayachandran C65040e22011-11-16 00:21:28 +000073 switch (irq) {
74 case PIC_UART_0_IRQ:
Jayachandran C3c0553e2013-03-23 17:27:56 +000075 devoff = XLP_IO_UART0_OFFSET(0);
76 break;
Jayachandran C65040e22011-11-16 00:21:28 +000077 case PIC_UART_1_IRQ:
Jayachandran C3c0553e2013-03-23 17:27:56 +000078 devoff = XLP_IO_UART1_OFFSET(0);
79 break;
Jayachandran C57d7cdb2012-07-24 17:28:54 +020080 case PIC_MMC_IRQ:
Jayachandran C3c0553e2013-03-23 17:27:56 +000081 devoff = XLP_IO_SD_OFFSET(0);
82 break;
Ganesan Ramalingame5be1fd2013-08-11 14:43:58 +053083 case PIC_I2C_0_IRQ: /* I2C will be fixed up */
Jayachandran C57d7cdb2012-07-24 17:28:54 +020084 case PIC_I2C_1_IRQ:
Ganesan Ramalingame5be1fd2013-08-11 14:43:58 +053085 case PIC_I2C_2_IRQ:
86 case PIC_I2C_3_IRQ:
87 if (cpu_is_xlpii())
88 devoff = XLP2XX_IO_I2C_OFFSET(0);
89 else
90 devoff = XLP_IO_I2C0_OFFSET(0);
Jayachandran C3c0553e2013-03-23 17:27:56 +000091 break;
Jayachandran C65040e22011-11-16 00:21:28 +000092 default:
Ganesan Ramalingam9eac3592013-08-21 19:32:41 +053093 if (cpu_is_xlpii()) {
94 switch (irq) {
95 /* XLP2XX has three XHCI USB controller */
96 case PIC_2XX_XHCI_0_IRQ:
97 devoff = XLP2XX_IO_USB_XHCI0_OFFSET(0);
98 break;
99 case PIC_2XX_XHCI_1_IRQ:
100 devoff = XLP2XX_IO_USB_XHCI1_OFFSET(0);
101 break;
102 case PIC_2XX_XHCI_2_IRQ:
103 devoff = XLP2XX_IO_USB_XHCI2_OFFSET(0);
104 break;
105 }
106 } else {
107 switch (irq) {
108 case PIC_EHCI_0_IRQ:
109 devoff = XLP_IO_USB_EHCI0_OFFSET(0);
110 break;
111 case PIC_EHCI_1_IRQ:
112 devoff = XLP_IO_USB_EHCI1_OFFSET(0);
113 break;
114 case PIC_OHCI_0_IRQ:
115 devoff = XLP_IO_USB_OHCI0_OFFSET(0);
116 break;
117 case PIC_OHCI_1_IRQ:
118 devoff = XLP_IO_USB_OHCI1_OFFSET(0);
119 break;
120 case PIC_OHCI_2_IRQ:
121 devoff = XLP_IO_USB_OHCI2_OFFSET(0);
122 break;
123 case PIC_OHCI_3_IRQ:
124 devoff = XLP_IO_USB_OHCI3_OFFSET(0);
125 break;
126 }
127 }
Jayachandran C65040e22011-11-16 00:21:28 +0000128 }
Jayachandran C3c0553e2013-03-23 17:27:56 +0000129
130 if (devoff != 0) {
131 pcibase = nlm_pcicfg_base(devoff);
132 irt = nlm_read_reg(pcibase, XLP_PCI_IRTINFO_REG) & 0xffff;
Ganesan Ramalingame5be1fd2013-08-11 14:43:58 +0530133 /* HW weirdness, I2C IRT entry has to be fixed up */
134 switch (irq) {
135 case PIC_I2C_1_IRQ:
136 irt = irt + 1; break;
137 case PIC_I2C_2_IRQ:
138 irt = irt + 2; break;
139 case PIC_I2C_3_IRQ:
140 irt = irt + 3; break;
141 }
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530142 } else if (irq >= PIC_PCIE_LINK_LEGACY_IRQ(0) &&
143 irq <= PIC_PCIE_LINK_LEGACY_IRQ(3)) {
Jayachandran C3c0553e2013-03-23 17:27:56 +0000144 /* HW bug, PCI IRT entries are bad on early silicon, fix */
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530145 irt = PIC_IRT_PCIE_LINK_INDEX(irq -
146 PIC_PCIE_LINK_LEGACY_IRQ_BASE);
147 } else if (irq >= PIC_PCIE_LINK_MSI_IRQ(0) &&
148 irq <= PIC_PCIE_LINK_MSI_IRQ(3)) {
149 irt = -2;
150 } else if (irq >= PIC_PCIE_MSIX_IRQ(0) &&
151 irq <= PIC_PCIE_MSIX_IRQ(3)) {
152 irt = -2;
Jayachandran C3c0553e2013-03-23 17:27:56 +0000153 } else {
154 irt = -1;
155 }
156 return irt;
Jayachandran C65040e22011-11-16 00:21:28 +0000157}
158
Jayachandran C77ae7982012-10-31 12:01:39 +0000159unsigned int nlm_get_core_frequency(int node, int core)
Jayachandran C65040e22011-11-16 00:21:28 +0000160{
Jayachandran C2aa54b22011-11-16 00:21:29 +0000161 unsigned int pll_divf, pll_divr, dfs_div, ext_div;
162 unsigned int rstval, dfsval, denom;
Jayachandran C77ae7982012-10-31 12:01:39 +0000163 uint64_t num, sysbase;
Jayachandran C65040e22011-11-16 00:21:28 +0000164
Jayachandran C77ae7982012-10-31 12:01:39 +0000165 sysbase = nlm_get_node(node)->sysbase;
166 rstval = nlm_read_sys_reg(sysbase, SYS_POWER_ON_RESET_CFG);
Ganesan Ramalingam57ceb4b2013-08-11 14:43:56 +0530167 if (cpu_is_xlpii()) {
168 num = 1000000ULL * (400 * 3 + 100 * (rstval >> 26));
169 denom = 3;
170 } else {
171 dfsval = nlm_read_sys_reg(sysbase, SYS_CORE_DFS_DIV_VALUE);
172 pll_divf = ((rstval >> 10) & 0x7f) + 1;
173 pll_divr = ((rstval >> 8) & 0x3) + 1;
174 ext_div = ((rstval >> 30) & 0x3) + 1;
175 dfs_div = ((dfsval >> (core * 4)) & 0xf) + 1;
Jayachandran C65040e22011-11-16 00:21:28 +0000176
Ganesan Ramalingam57ceb4b2013-08-11 14:43:56 +0530177 num = 800000000ULL * pll_divf;
178 denom = 3 * pll_divr * ext_div * dfs_div;
179 }
Jayachandran C65040e22011-11-16 00:21:28 +0000180 do_div(num, denom);
181 return (unsigned int)num;
182}
Jayachandran C2aa54b22011-11-16 00:21:29 +0000183
Ganesan Ramalingam57ceb4b2013-08-11 14:43:56 +0530184/* Calculate Frequency to the PIC from PLL.
185 * freq_out = ( ref_freq/2 * (6 + ctrl2[7:0]) + ctrl2[20:8]/2^13 ) /
186 * ((2^ctrl0[7:5]) * Table(ctrl0[26:24]))
187 */
188static unsigned int nlm_2xx_get_pic_frequency(int node)
189{
190 u32 ctrl_val0, ctrl_val2, vco_post_div, pll_post_div;
191 u32 mdiv, fdiv, pll_out_freq_den, reg_select, ref_div, pic_div;
192 u64 ref_clk, sysbase, pll_out_freq_num, ref_clk_select;
193
194 sysbase = nlm_get_node(node)->sysbase;
195
196 /* Find ref_clk_base */
197 ref_clk_select =
198 (nlm_read_sys_reg(sysbase, SYS_POWER_ON_RESET_CFG) >> 18) & 0x3;
199 switch (ref_clk_select) {
200 case 0:
201 ref_clk = 200000000ULL;
202 ref_div = 3;
203 break;
204 case 1:
205 ref_clk = 100000000ULL;
206 ref_div = 1;
207 break;
208 case 2:
209 ref_clk = 125000000ULL;
210 ref_div = 1;
211 break;
212 case 3:
213 ref_clk = 400000000ULL;
214 ref_div = 3;
215 break;
216 }
217
218 /* Find the clock source PLL device for PIC */
219 reg_select = (nlm_read_sys_reg(sysbase, SYS_CLK_DEV_SEL) >> 22) & 0x3;
220 switch (reg_select) {
221 case 0:
222 ctrl_val0 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL0);
223 ctrl_val2 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL2);
224 break;
225 case 1:
226 ctrl_val0 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL0_DEVX(0));
227 ctrl_val2 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL2_DEVX(0));
228 break;
229 case 2:
230 ctrl_val0 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL0_DEVX(1));
231 ctrl_val2 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL2_DEVX(1));
232 break;
233 case 3:
234 ctrl_val0 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL0_DEVX(2));
235 ctrl_val2 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL2_DEVX(2));
236 break;
237 }
238
239 vco_post_div = (ctrl_val0 >> 5) & 0x7;
240 pll_post_div = (ctrl_val0 >> 24) & 0x7;
241 mdiv = ctrl_val2 & 0xff;
242 fdiv = (ctrl_val2 >> 8) & 0xfff;
243
244 /* Find PLL post divider value */
245 switch (pll_post_div) {
246 case 1:
247 pll_post_div = 2;
248 break;
249 case 3:
250 pll_post_div = 4;
251 break;
252 case 7:
253 pll_post_div = 8;
254 break;
255 case 6:
256 pll_post_div = 16;
257 break;
258 case 0:
259 default:
260 pll_post_div = 1;
261 break;
262 }
263
264 fdiv = fdiv/(1 << 13);
265 pll_out_freq_num = ((ref_clk >> 1) * (6 + mdiv)) + fdiv;
266 pll_out_freq_den = (1 << vco_post_div) * pll_post_div * 3;
267
268 if (pll_out_freq_den > 0)
269 do_div(pll_out_freq_num, pll_out_freq_den);
270
271 /* PIC post divider, which happens after PLL */
272 pic_div = (nlm_read_sys_reg(sysbase, SYS_CLK_DEV_DIV) >> 22) & 0x3;
273 do_div(pll_out_freq_num, 1 << pic_div);
274
275 return pll_out_freq_num;
276}
277
278unsigned int nlm_get_pic_frequency(int node)
279{
280 if (cpu_is_xlpii())
281 return nlm_2xx_get_pic_frequency(node);
282 else
283 return 133333333;
284}
285
Jayachandran C2aa54b22011-11-16 00:21:29 +0000286unsigned int nlm_get_cpu_frequency(void)
287{
Jayachandran C77ae7982012-10-31 12:01:39 +0000288 return nlm_get_core_frequency(0, 0);
Jayachandran C2aa54b22011-11-16 00:21:29 +0000289}
Jayachandran Ca2ba6cd2013-08-21 19:31:29 +0530290
291/*
292 * Fills upto 8 pairs of entries containing the DRAM map of a node
293 * if n < 0, get dram map for all nodes
294 */
295int xlp_get_dram_map(int n, uint64_t *dram_map)
296{
297 uint64_t bridgebase, base, lim;
298 uint32_t val;
299 int i, node, rv;
300
301 /* Look only at mapping on Node 0, we don't handle crazy configs */
302 bridgebase = nlm_get_bridge_regbase(0);
303 rv = 0;
304 for (i = 0; i < 8; i++) {
305 val = nlm_read_bridge_reg(bridgebase,
306 BRIDGE_DRAM_NODE_TRANSLN(i));
307 node = (val >> 1) & 0x3;
308 if (n >= 0 && n != node)
309 continue;
310 val = nlm_read_bridge_reg(bridgebase, BRIDGE_DRAM_BAR(i));
311 val = (val >> 12) & 0xfffff;
312 base = (uint64_t) val << 20;
313 val = nlm_read_bridge_reg(bridgebase, BRIDGE_DRAM_LIMIT(i));
314 val = (val >> 12) & 0xfffff;
315 if (val == 0) /* BAR not used */
316 continue;
317 lim = ((uint64_t)val + 1) << 20;
318 dram_map[rv] = base;
319 dram_map[rv + 1] = lim;
320 rv += 2;
321 }
322 return rv;
323}