blob: 8d743d08fd7361e21b1b9d96193a5fa24f29d5d2 [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);
Jayachandran C3e468562014-04-29 20:07:42 +053057 if (node == 0)
58 nodep->coremask = 1; /* node 0, boot cpu */
Jayachandran C77ae7982012-10-31 12:01:39 +000059 nodep->sysbase = nlm_get_sys_regbase(node);
60 nodep->picbase = nlm_get_pic_regbase(node);
61 nodep->ebase = read_c0_ebase() & (~((1 << 12) - 1));
Jayachandran C5513c762013-12-21 16:52:21 +053062 if (cpu_is_xlp9xx())
63 nodep->socbus = xlp9xx_get_socbus(node);
64 else
65 nodep->socbus = 0;
Jayachandran C77ae7982012-10-31 12:01:39 +000066 spin_lock_init(&nodep->piclock);
Jayachandran C65040e22011-11-16 00:21:28 +000067}
68
Jayachandran C0d57eba2014-05-09 16:35:34 +053069static int xlp9xx_irq_to_irt(int irq)
70{
71 switch (irq) {
72 case PIC_GPIO_IRQ:
73 return 12;
74 case PIC_9XX_XHCI_0_IRQ:
75 return 114;
76 case PIC_9XX_XHCI_1_IRQ:
77 return 115;
Ganesan Ramalingamc9822322015-01-07 16:58:36 +053078 case PIC_9XX_XHCI_2_IRQ:
79 return 116;
Jayachandran C0d57eba2014-05-09 16:35:34 +053080 case PIC_UART_0_IRQ:
81 return 133;
82 case PIC_UART_1_IRQ:
83 return 134;
84 case PIC_SATA_IRQ:
85 return 143;
86 case PIC_SPI_IRQ:
87 return 152;
88 case PIC_MMC_IRQ:
89 return 153;
90 case PIC_PCIE_LINK_LEGACY_IRQ(0):
91 case PIC_PCIE_LINK_LEGACY_IRQ(1):
92 case PIC_PCIE_LINK_LEGACY_IRQ(2):
93 case PIC_PCIE_LINK_LEGACY_IRQ(3):
94 return 191 + irq - PIC_PCIE_LINK_LEGACY_IRQ_BASE;
95 }
96 return -1;
97}
98
99static int xlp_irq_to_irt(int irq)
Jayachandran C65040e22011-11-16 00:21:28 +0000100{
Jayachandran C3c0553e2013-03-23 17:27:56 +0000101 uint64_t pcibase;
102 int devoff, irt;
Jayachandran C65040e22011-11-16 00:21:28 +0000103
Ganesan Ramalingam9eac3592013-08-21 19:32:41 +0530104 devoff = 0;
Jayachandran C65040e22011-11-16 00:21:28 +0000105 switch (irq) {
106 case PIC_UART_0_IRQ:
Jayachandran C3c0553e2013-03-23 17:27:56 +0000107 devoff = XLP_IO_UART0_OFFSET(0);
108 break;
Jayachandran C65040e22011-11-16 00:21:28 +0000109 case PIC_UART_1_IRQ:
Jayachandran C3c0553e2013-03-23 17:27:56 +0000110 devoff = XLP_IO_UART1_OFFSET(0);
111 break;
Jayachandran C57d7cdb2012-07-24 17:28:54 +0200112 case PIC_MMC_IRQ:
Jayachandran C0d57eba2014-05-09 16:35:34 +0530113 devoff = XLP_IO_MMC_OFFSET(0);
Jayachandran C3c0553e2013-03-23 17:27:56 +0000114 break;
Ganesan Ramalingame5be1fd2013-08-11 14:43:58 +0530115 case PIC_I2C_0_IRQ: /* I2C will be fixed up */
Jayachandran C57d7cdb2012-07-24 17:28:54 +0200116 case PIC_I2C_1_IRQ:
Ganesan Ramalingame5be1fd2013-08-11 14:43:58 +0530117 case PIC_I2C_2_IRQ:
118 case PIC_I2C_3_IRQ:
119 if (cpu_is_xlpii())
120 devoff = XLP2XX_IO_I2C_OFFSET(0);
121 else
122 devoff = XLP_IO_I2C0_OFFSET(0);
Jayachandran C3c0553e2013-03-23 17:27:56 +0000123 break;
Jayachandran C0d57eba2014-05-09 16:35:34 +0530124 case PIC_SATA_IRQ:
125 devoff = XLP_IO_SATA_OFFSET(0);
126 break;
127 case PIC_GPIO_IRQ:
128 devoff = XLP_IO_GPIO_OFFSET(0);
129 break;
130 case PIC_NAND_IRQ:
131 devoff = XLP_IO_NAND_OFFSET(0);
132 break;
133 case PIC_SPI_IRQ:
134 devoff = XLP_IO_SPI_OFFSET(0);
135 break;
Jayachandran C65040e22011-11-16 00:21:28 +0000136 default:
Ganesan Ramalingam9eac3592013-08-21 19:32:41 +0530137 if (cpu_is_xlpii()) {
138 switch (irq) {
139 /* XLP2XX has three XHCI USB controller */
140 case PIC_2XX_XHCI_0_IRQ:
141 devoff = XLP2XX_IO_USB_XHCI0_OFFSET(0);
142 break;
143 case PIC_2XX_XHCI_1_IRQ:
144 devoff = XLP2XX_IO_USB_XHCI1_OFFSET(0);
145 break;
146 case PIC_2XX_XHCI_2_IRQ:
147 devoff = XLP2XX_IO_USB_XHCI2_OFFSET(0);
148 break;
149 }
150 } else {
151 switch (irq) {
152 case PIC_EHCI_0_IRQ:
153 devoff = XLP_IO_USB_EHCI0_OFFSET(0);
154 break;
155 case PIC_EHCI_1_IRQ:
156 devoff = XLP_IO_USB_EHCI1_OFFSET(0);
157 break;
158 case PIC_OHCI_0_IRQ:
159 devoff = XLP_IO_USB_OHCI0_OFFSET(0);
160 break;
161 case PIC_OHCI_1_IRQ:
162 devoff = XLP_IO_USB_OHCI1_OFFSET(0);
163 break;
164 case PIC_OHCI_2_IRQ:
165 devoff = XLP_IO_USB_OHCI2_OFFSET(0);
166 break;
167 case PIC_OHCI_3_IRQ:
168 devoff = XLP_IO_USB_OHCI3_OFFSET(0);
169 break;
170 }
171 }
Jayachandran C65040e22011-11-16 00:21:28 +0000172 }
Jayachandran C3c0553e2013-03-23 17:27:56 +0000173
174 if (devoff != 0) {
Jayachandran C72e06052015-01-07 16:58:25 +0530175 uint32_t val;
176
Jayachandran C3c0553e2013-03-23 17:27:56 +0000177 pcibase = nlm_pcicfg_base(devoff);
Jayachandran C72e06052015-01-07 16:58:25 +0530178 val = nlm_read_reg(pcibase, XLP_PCI_IRTINFO_REG);
179 if (val == 0xffffffff) {
180 irt = -1;
181 } else {
182 irt = val & 0xffff;
183 /* HW weirdness, I2C IRT entry has to be fixed up */
184 switch (irq) {
185 case PIC_I2C_1_IRQ:
186 irt = irt + 1; break;
187 case PIC_I2C_2_IRQ:
188 irt = irt + 2; break;
189 case PIC_I2C_3_IRQ:
190 irt = irt + 3; break;
191 }
Ganesan Ramalingame5be1fd2013-08-11 14:43:58 +0530192 }
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530193 } else if (irq >= PIC_PCIE_LINK_LEGACY_IRQ(0) &&
194 irq <= PIC_PCIE_LINK_LEGACY_IRQ(3)) {
Jayachandran C3c0553e2013-03-23 17:27:56 +0000195 /* HW bug, PCI IRT entries are bad on early silicon, fix */
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530196 irt = PIC_IRT_PCIE_LINK_INDEX(irq -
197 PIC_PCIE_LINK_LEGACY_IRQ_BASE);
Jayachandran C3c0553e2013-03-23 17:27:56 +0000198 } else {
199 irt = -1;
200 }
201 return irt;
Jayachandran C65040e22011-11-16 00:21:28 +0000202}
203
Jayachandran C0d57eba2014-05-09 16:35:34 +0530204int nlm_irq_to_irt(int irq)
205{
206 /* return -2 for irqs without 1-1 mapping */
207 if (irq >= PIC_PCIE_LINK_MSI_IRQ(0) && irq <= PIC_PCIE_LINK_MSI_IRQ(3))
208 return -2;
209 if (irq >= PIC_PCIE_MSIX_IRQ(0) && irq <= PIC_PCIE_MSIX_IRQ(3))
210 return -2;
211
212 if (cpu_is_xlp9xx())
213 return xlp9xx_irq_to_irt(irq);
214 else
215 return xlp_irq_to_irt(irq);
216}
217
Jayachandran Cedf3ed52014-04-29 20:07:52 +0530218static unsigned int nlm_xlp2_get_core_frequency(int node, int core)
219{
220 unsigned int pll_post_div, ctrl_val0, ctrl_val1, denom;
221 uint64_t num, sysbase, clockbase;
222
223 if (cpu_is_xlp9xx()) {
224 clockbase = nlm_get_clock_regbase(node);
225 ctrl_val0 = nlm_read_sys_reg(clockbase,
226 SYS_9XX_CPU_PLL_CTRL0(core));
227 ctrl_val1 = nlm_read_sys_reg(clockbase,
228 SYS_9XX_CPU_PLL_CTRL1(core));
229 } else {
230 sysbase = nlm_get_node(node)->sysbase;
231 ctrl_val0 = nlm_read_sys_reg(sysbase,
232 SYS_CPU_PLL_CTRL0(core));
233 ctrl_val1 = nlm_read_sys_reg(sysbase,
234 SYS_CPU_PLL_CTRL1(core));
235 }
236
237 /* Find PLL post divider value */
238 switch ((ctrl_val0 >> 24) & 0x7) {
239 case 1:
240 pll_post_div = 2;
241 break;
242 case 3:
243 pll_post_div = 4;
244 break;
245 case 7:
246 pll_post_div = 8;
247 break;
248 case 6:
249 pll_post_div = 16;
250 break;
251 case 0:
252 default:
253 pll_post_div = 1;
254 break;
255 }
256
257 num = 1000000ULL * (400 * 3 + 100 * (ctrl_val1 & 0x3f));
258 denom = 3 * pll_post_div;
259 do_div(num, denom);
260
261 return (unsigned int)num;
262}
263
264static unsigned int nlm_xlp_get_core_frequency(int node, int core)
Jayachandran C65040e22011-11-16 00:21:28 +0000265{
Jayachandran C2aa54b22011-11-16 00:21:29 +0000266 unsigned int pll_divf, pll_divr, dfs_div, ext_div;
267 unsigned int rstval, dfsval, denom;
Jayachandran C77ae7982012-10-31 12:01:39 +0000268 uint64_t num, sysbase;
Jayachandran C65040e22011-11-16 00:21:28 +0000269
Jayachandran C77ae7982012-10-31 12:01:39 +0000270 sysbase = nlm_get_node(node)->sysbase;
Jayachandran Cedf3ed52014-04-29 20:07:52 +0530271 rstval = nlm_read_sys_reg(sysbase, SYS_POWER_ON_RESET_CFG);
272 dfsval = nlm_read_sys_reg(sysbase, SYS_CORE_DFS_DIV_VALUE);
273 pll_divf = ((rstval >> 10) & 0x7f) + 1;
274 pll_divr = ((rstval >> 8) & 0x3) + 1;
275 ext_div = ((rstval >> 30) & 0x3) + 1;
276 dfs_div = ((dfsval >> (core * 4)) & 0xf) + 1;
Jayachandran C65040e22011-11-16 00:21:28 +0000277
Jayachandran Cedf3ed52014-04-29 20:07:52 +0530278 num = 800000000ULL * pll_divf;
279 denom = 3 * pll_divr * ext_div * dfs_div;
Jayachandran C65040e22011-11-16 00:21:28 +0000280 do_div(num, denom);
Jayachandran Cedf3ed52014-04-29 20:07:52 +0530281
Jayachandran C65040e22011-11-16 00:21:28 +0000282 return (unsigned int)num;
283}
Jayachandran C2aa54b22011-11-16 00:21:29 +0000284
Jayachandran Cedf3ed52014-04-29 20:07:52 +0530285unsigned int nlm_get_core_frequency(int node, int core)
286{
287 if (cpu_is_xlpii())
288 return nlm_xlp2_get_core_frequency(node, core);
289 else
290 return nlm_xlp_get_core_frequency(node, core);
291}
292
Ganesan Ramalingamc0659092014-04-29 20:07:51 +0530293/*
294 * Calculate PIC frequency from PLL registers.
295 * freq_out = (ref_freq/2 * (6 + ctrl2[7:0]) + ctrl2[20:8]/2^13) /
296 * ((2^ctrl0[7:5]) * Table(ctrl0[26:24]))
Ganesan Ramalingam57ceb4b2013-08-11 14:43:56 +0530297 */
Ganesan Ramalingamc0659092014-04-29 20:07:51 +0530298static unsigned int nlm_xlp2_get_pic_frequency(int node)
Ganesan Ramalingam57ceb4b2013-08-11 14:43:56 +0530299{
Ganesan Ramalingamc0659092014-04-29 20:07:51 +0530300 u32 ctrl_val0, ctrl_val2, vco_post_div, pll_post_div, cpu_xlp9xx;
Ganesan Ramalingam57ceb4b2013-08-11 14:43:56 +0530301 u32 mdiv, fdiv, pll_out_freq_den, reg_select, ref_div, pic_div;
Ganesan Ramalingamc0659092014-04-29 20:07:51 +0530302 u64 sysbase, pll_out_freq_num, ref_clk_select, clockbase, ref_clk;
Ganesan Ramalingam57ceb4b2013-08-11 14:43:56 +0530303
304 sysbase = nlm_get_node(node)->sysbase;
Ganesan Ramalingamc0659092014-04-29 20:07:51 +0530305 clockbase = nlm_get_clock_regbase(node);
306 cpu_xlp9xx = cpu_is_xlp9xx();
Ganesan Ramalingam57ceb4b2013-08-11 14:43:56 +0530307
308 /* Find ref_clk_base */
Ganesan Ramalingamc0659092014-04-29 20:07:51 +0530309 if (cpu_xlp9xx)
310 ref_clk_select = (nlm_read_sys_reg(sysbase,
311 SYS_9XX_POWER_ON_RESET_CFG) >> 18) & 0x3;
312 else
313 ref_clk_select = (nlm_read_sys_reg(sysbase,
314 SYS_POWER_ON_RESET_CFG) >> 18) & 0x3;
Ganesan Ramalingam57ceb4b2013-08-11 14:43:56 +0530315 switch (ref_clk_select) {
316 case 0:
317 ref_clk = 200000000ULL;
318 ref_div = 3;
319 break;
320 case 1:
321 ref_clk = 100000000ULL;
322 ref_div = 1;
323 break;
324 case 2:
325 ref_clk = 125000000ULL;
326 ref_div = 1;
327 break;
328 case 3:
329 ref_clk = 400000000ULL;
330 ref_div = 3;
331 break;
332 }
333
334 /* Find the clock source PLL device for PIC */
Ganesan Ramalingamc0659092014-04-29 20:07:51 +0530335 if (cpu_xlp9xx) {
336 reg_select = nlm_read_sys_reg(clockbase,
Ganesan Ramalingama3613be2015-01-07 16:58:27 +0530337 SYS_9XX_CLK_DEV_SEL_REG) & 0x3;
Ganesan Ramalingamc0659092014-04-29 20:07:51 +0530338 switch (reg_select) {
339 case 0:
340 ctrl_val0 = nlm_read_sys_reg(clockbase,
341 SYS_9XX_PLL_CTRL0);
342 ctrl_val2 = nlm_read_sys_reg(clockbase,
343 SYS_9XX_PLL_CTRL2);
344 break;
345 case 1:
346 ctrl_val0 = nlm_read_sys_reg(clockbase,
347 SYS_9XX_PLL_CTRL0_DEVX(0));
348 ctrl_val2 = nlm_read_sys_reg(clockbase,
349 SYS_9XX_PLL_CTRL2_DEVX(0));
350 break;
351 case 2:
352 ctrl_val0 = nlm_read_sys_reg(clockbase,
353 SYS_9XX_PLL_CTRL0_DEVX(1));
354 ctrl_val2 = nlm_read_sys_reg(clockbase,
355 SYS_9XX_PLL_CTRL2_DEVX(1));
356 break;
357 case 3:
358 ctrl_val0 = nlm_read_sys_reg(clockbase,
359 SYS_9XX_PLL_CTRL0_DEVX(2));
360 ctrl_val2 = nlm_read_sys_reg(clockbase,
361 SYS_9XX_PLL_CTRL2_DEVX(2));
362 break;
363 }
364 } else {
365 reg_select = (nlm_read_sys_reg(sysbase,
Ganesan Ramalingama3613be2015-01-07 16:58:27 +0530366 SYS_CLK_DEV_SEL_REG) >> 22) & 0x3;
Ganesan Ramalingamc0659092014-04-29 20:07:51 +0530367 switch (reg_select) {
368 case 0:
369 ctrl_val0 = nlm_read_sys_reg(sysbase,
370 SYS_PLL_CTRL0);
371 ctrl_val2 = nlm_read_sys_reg(sysbase,
372 SYS_PLL_CTRL2);
373 break;
374 case 1:
375 ctrl_val0 = nlm_read_sys_reg(sysbase,
376 SYS_PLL_CTRL0_DEVX(0));
377 ctrl_val2 = nlm_read_sys_reg(sysbase,
378 SYS_PLL_CTRL2_DEVX(0));
379 break;
380 case 2:
381 ctrl_val0 = nlm_read_sys_reg(sysbase,
382 SYS_PLL_CTRL0_DEVX(1));
383 ctrl_val2 = nlm_read_sys_reg(sysbase,
384 SYS_PLL_CTRL2_DEVX(1));
385 break;
386 case 3:
387 ctrl_val0 = nlm_read_sys_reg(sysbase,
388 SYS_PLL_CTRL0_DEVX(2));
389 ctrl_val2 = nlm_read_sys_reg(sysbase,
390 SYS_PLL_CTRL2_DEVX(2));
391 break;
392 }
Ganesan Ramalingam57ceb4b2013-08-11 14:43:56 +0530393 }
394
395 vco_post_div = (ctrl_val0 >> 5) & 0x7;
396 pll_post_div = (ctrl_val0 >> 24) & 0x7;
397 mdiv = ctrl_val2 & 0xff;
Ganesan Ramalingamc0659092014-04-29 20:07:51 +0530398 fdiv = (ctrl_val2 >> 8) & 0x1fff;
Ganesan Ramalingam57ceb4b2013-08-11 14:43:56 +0530399
400 /* Find PLL post divider value */
401 switch (pll_post_div) {
402 case 1:
403 pll_post_div = 2;
404 break;
405 case 3:
406 pll_post_div = 4;
407 break;
408 case 7:
409 pll_post_div = 8;
410 break;
411 case 6:
412 pll_post_div = 16;
413 break;
414 case 0:
415 default:
416 pll_post_div = 1;
417 break;
418 }
419
420 fdiv = fdiv/(1 << 13);
421 pll_out_freq_num = ((ref_clk >> 1) * (6 + mdiv)) + fdiv;
Qingmin Liu8db23f72015-01-07 16:58:23 +0530422 pll_out_freq_den = (1 << vco_post_div) * pll_post_div * ref_div;
Ganesan Ramalingam57ceb4b2013-08-11 14:43:56 +0530423
424 if (pll_out_freq_den > 0)
425 do_div(pll_out_freq_num, pll_out_freq_den);
426
427 /* PIC post divider, which happens after PLL */
Ganesan Ramalingamc0659092014-04-29 20:07:51 +0530428 if (cpu_xlp9xx)
429 pic_div = nlm_read_sys_reg(clockbase,
Ganesan Ramalingama3613be2015-01-07 16:58:27 +0530430 SYS_9XX_CLK_DEV_DIV_REG) & 0x3;
Ganesan Ramalingamc0659092014-04-29 20:07:51 +0530431 else
432 pic_div = (nlm_read_sys_reg(sysbase,
Ganesan Ramalingama3613be2015-01-07 16:58:27 +0530433 SYS_CLK_DEV_DIV_REG) >> 22) & 0x3;
Ganesan Ramalingam57ceb4b2013-08-11 14:43:56 +0530434 do_div(pll_out_freq_num, 1 << pic_div);
435
436 return pll_out_freq_num;
437}
438
439unsigned int nlm_get_pic_frequency(int node)
440{
441 if (cpu_is_xlpii())
Ganesan Ramalingamc0659092014-04-29 20:07:51 +0530442 return nlm_xlp2_get_pic_frequency(node);
Ganesan Ramalingam57ceb4b2013-08-11 14:43:56 +0530443 else
444 return 133333333;
445}
446
Jayachandran C2aa54b22011-11-16 00:21:29 +0000447unsigned int nlm_get_cpu_frequency(void)
448{
Jayachandran C77ae7982012-10-31 12:01:39 +0000449 return nlm_get_core_frequency(0, 0);
Jayachandran C2aa54b22011-11-16 00:21:29 +0000450}
Jayachandran Ca2ba6cd2013-08-21 19:31:29 +0530451
452/*
453 * Fills upto 8 pairs of entries containing the DRAM map of a node
Jayachandran Cb3b73ae62015-01-07 16:58:33 +0530454 * if node < 0, get dram map for all nodes
Jayachandran Ca2ba6cd2013-08-21 19:31:29 +0530455 */
Jayachandran Cb3b73ae62015-01-07 16:58:33 +0530456int nlm_get_dram_map(int node, uint64_t *dram_map, int nentries)
Jayachandran Ca2ba6cd2013-08-21 19:31:29 +0530457{
458 uint64_t bridgebase, base, lim;
459 uint32_t val;
Jayachandran Ce7aa6c62013-12-21 16:52:25 +0530460 unsigned int barreg, limreg, xlatreg;
Jayachandran Cb3b73ae62015-01-07 16:58:33 +0530461 int i, n, rv;
Jayachandran Ca2ba6cd2013-08-21 19:31:29 +0530462
463 /* Look only at mapping on Node 0, we don't handle crazy configs */
464 bridgebase = nlm_get_bridge_regbase(0);
465 rv = 0;
466 for (i = 0; i < 8; i++) {
Jayachandran Cb3b73ae62015-01-07 16:58:33 +0530467 if (rv + 1 >= nentries)
468 break;
Jayachandran Ce7aa6c62013-12-21 16:52:25 +0530469 if (cpu_is_xlp9xx()) {
470 barreg = BRIDGE_9XX_DRAM_BAR(i);
471 limreg = BRIDGE_9XX_DRAM_LIMIT(i);
472 xlatreg = BRIDGE_9XX_DRAM_NODE_TRANSLN(i);
473 } else {
474 barreg = BRIDGE_DRAM_BAR(i);
475 limreg = BRIDGE_DRAM_LIMIT(i);
476 xlatreg = BRIDGE_DRAM_NODE_TRANSLN(i);
477 }
Jayachandran Cb3b73ae62015-01-07 16:58:33 +0530478 if (node >= 0) {
Jayachandran Ce7aa6c62013-12-21 16:52:25 +0530479 /* node specified, get node mapping of BAR */
480 val = nlm_read_bridge_reg(bridgebase, xlatreg);
Jayachandran Cb3b73ae62015-01-07 16:58:33 +0530481 n = (val >> 1) & 0x3;
Jayachandran Ce7aa6c62013-12-21 16:52:25 +0530482 if (n != node)
483 continue;
484 }
485 val = nlm_read_bridge_reg(bridgebase, barreg);
Jayachandran Ca2ba6cd2013-08-21 19:31:29 +0530486 val = (val >> 12) & 0xfffff;
487 base = (uint64_t) val << 20;
Jayachandran Ce7aa6c62013-12-21 16:52:25 +0530488 val = nlm_read_bridge_reg(bridgebase, limreg);
Jayachandran Ca2ba6cd2013-08-21 19:31:29 +0530489 val = (val >> 12) & 0xfffff;
490 if (val == 0) /* BAR not used */
491 continue;
492 lim = ((uint64_t)val + 1) << 20;
493 dram_map[rv] = base;
494 dram_map[rv + 1] = lim;
495 rv += 2;
496 }
497 return rv;
498}