nlm_hal.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. #include <linux/types.h>
  35. #include <linux/kernel.h>
  36. #include <linux/mm.h>
  37. #include <linux/delay.h>
  38. #include <asm/mipsregs.h>
  39. #include <asm/time.h>
  40. #include <asm/netlogic/common.h>
  41. #include <asm/netlogic/haldefs.h>
  42. #include <asm/netlogic/xlp-hal/iomap.h>
  43. #include <asm/netlogic/xlp-hal/xlp.h>
  44. #include <asm/netlogic/xlp-hal/bridge.h>
  45. #include <asm/netlogic/xlp-hal/pic.h>
  46. #include <asm/netlogic/xlp-hal/sys.h>
  47. /* Main initialization */
  48. void nlm_node_init(int node)
  49. {
  50. struct nlm_soc_info *nodep;
  51. nodep = nlm_get_node(node);
  52. if (node == 0)
  53. nodep->coremask = 1; /* node 0, boot cpu */
  54. nodep->sysbase = nlm_get_sys_regbase(node);
  55. nodep->picbase = nlm_get_pic_regbase(node);
  56. nodep->ebase = read_c0_ebase() & MIPS_EBASE_BASE;
  57. if (cpu_is_xlp9xx())
  58. nodep->socbus = xlp9xx_get_socbus(node);
  59. else
  60. nodep->socbus = 0;
  61. spin_lock_init(&nodep->piclock);
  62. }
  63. static int xlp9xx_irq_to_irt(int irq)
  64. {
  65. switch (irq) {
  66. case PIC_GPIO_IRQ:
  67. return 12;
  68. case PIC_I2C_0_IRQ:
  69. return 125;
  70. case PIC_I2C_1_IRQ:
  71. return 126;
  72. case PIC_I2C_2_IRQ:
  73. return 127;
  74. case PIC_I2C_3_IRQ:
  75. return 128;
  76. case PIC_9XX_XHCI_0_IRQ:
  77. return 114;
  78. case PIC_9XX_XHCI_1_IRQ:
  79. return 115;
  80. case PIC_9XX_XHCI_2_IRQ:
  81. return 116;
  82. case PIC_UART_0_IRQ:
  83. return 133;
  84. case PIC_UART_1_IRQ:
  85. return 134;
  86. case PIC_SATA_IRQ:
  87. return 143;
  88. case PIC_NAND_IRQ:
  89. return 151;
  90. case PIC_SPI_IRQ:
  91. return 152;
  92. case PIC_MMC_IRQ:
  93. return 153;
  94. case PIC_PCIE_LINK_LEGACY_IRQ(0):
  95. case PIC_PCIE_LINK_LEGACY_IRQ(1):
  96. case PIC_PCIE_LINK_LEGACY_IRQ(2):
  97. case PIC_PCIE_LINK_LEGACY_IRQ(3):
  98. return 191 + irq - PIC_PCIE_LINK_LEGACY_IRQ_BASE;
  99. }
  100. return -1;
  101. }
  102. static int xlp_irq_to_irt(int irq)
  103. {
  104. uint64_t pcibase;
  105. int devoff, irt;
  106. devoff = 0;
  107. switch (irq) {
  108. case PIC_UART_0_IRQ:
  109. devoff = XLP_IO_UART0_OFFSET(0);
  110. break;
  111. case PIC_UART_1_IRQ:
  112. devoff = XLP_IO_UART1_OFFSET(0);
  113. break;
  114. case PIC_MMC_IRQ:
  115. devoff = XLP_IO_MMC_OFFSET(0);
  116. break;
  117. case PIC_I2C_0_IRQ: /* I2C will be fixed up */
  118. case PIC_I2C_1_IRQ:
  119. case PIC_I2C_2_IRQ:
  120. case PIC_I2C_3_IRQ:
  121. if (cpu_is_xlpii())
  122. devoff = XLP2XX_IO_I2C_OFFSET(0);
  123. else
  124. devoff = XLP_IO_I2C0_OFFSET(0);
  125. break;
  126. case PIC_SATA_IRQ:
  127. devoff = XLP_IO_SATA_OFFSET(0);
  128. break;
  129. case PIC_GPIO_IRQ:
  130. devoff = XLP_IO_GPIO_OFFSET(0);
  131. break;
  132. case PIC_NAND_IRQ:
  133. devoff = XLP_IO_NAND_OFFSET(0);
  134. break;
  135. case PIC_SPI_IRQ:
  136. devoff = XLP_IO_SPI_OFFSET(0);
  137. break;
  138. default:
  139. if (cpu_is_xlpii()) {
  140. switch (irq) {
  141. /* XLP2XX has three XHCI USB controller */
  142. case PIC_2XX_XHCI_0_IRQ:
  143. devoff = XLP2XX_IO_USB_XHCI0_OFFSET(0);
  144. break;
  145. case PIC_2XX_XHCI_1_IRQ:
  146. devoff = XLP2XX_IO_USB_XHCI1_OFFSET(0);
  147. break;
  148. case PIC_2XX_XHCI_2_IRQ:
  149. devoff = XLP2XX_IO_USB_XHCI2_OFFSET(0);
  150. break;
  151. }
  152. } else {
  153. switch (irq) {
  154. case PIC_EHCI_0_IRQ:
  155. devoff = XLP_IO_USB_EHCI0_OFFSET(0);
  156. break;
  157. case PIC_EHCI_1_IRQ:
  158. devoff = XLP_IO_USB_EHCI1_OFFSET(0);
  159. break;
  160. case PIC_OHCI_0_IRQ:
  161. devoff = XLP_IO_USB_OHCI0_OFFSET(0);
  162. break;
  163. case PIC_OHCI_1_IRQ:
  164. devoff = XLP_IO_USB_OHCI1_OFFSET(0);
  165. break;
  166. case PIC_OHCI_2_IRQ:
  167. devoff = XLP_IO_USB_OHCI2_OFFSET(0);
  168. break;
  169. case PIC_OHCI_3_IRQ:
  170. devoff = XLP_IO_USB_OHCI3_OFFSET(0);
  171. break;
  172. }
  173. }
  174. }
  175. if (devoff != 0) {
  176. uint32_t val;
  177. pcibase = nlm_pcicfg_base(devoff);
  178. 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. }
  192. }
  193. } else if (irq >= PIC_PCIE_LINK_LEGACY_IRQ(0) &&
  194. irq <= PIC_PCIE_LINK_LEGACY_IRQ(3)) {
  195. /* HW bug, PCI IRT entries are bad on early silicon, fix */
  196. irt = PIC_IRT_PCIE_LINK_INDEX(irq -
  197. PIC_PCIE_LINK_LEGACY_IRQ_BASE);
  198. } else {
  199. irt = -1;
  200. }
  201. return irt;
  202. }
  203. int nlm_irq_to_irt(int irq)
  204. {
  205. /* return -2 for irqs without 1-1 mapping */
  206. if (irq >= PIC_PCIE_LINK_MSI_IRQ(0) && irq <= PIC_PCIE_LINK_MSI_IRQ(3))
  207. return -2;
  208. if (irq >= PIC_PCIE_MSIX_IRQ(0) && irq <= PIC_PCIE_MSIX_IRQ(3))
  209. return -2;
  210. if (cpu_is_xlp9xx())
  211. return xlp9xx_irq_to_irt(irq);
  212. else
  213. return xlp_irq_to_irt(irq);
  214. }
  215. static unsigned int nlm_xlp2_get_core_frequency(int node, int core)
  216. {
  217. unsigned int pll_post_div, ctrl_val0, ctrl_val1, denom;
  218. uint64_t num, sysbase, clockbase;
  219. if (cpu_is_xlp9xx()) {
  220. clockbase = nlm_get_clock_regbase(node);
  221. ctrl_val0 = nlm_read_sys_reg(clockbase,
  222. SYS_9XX_CPU_PLL_CTRL0(core));
  223. ctrl_val1 = nlm_read_sys_reg(clockbase,
  224. SYS_9XX_CPU_PLL_CTRL1(core));
  225. } else {
  226. sysbase = nlm_get_node(node)->sysbase;
  227. ctrl_val0 = nlm_read_sys_reg(sysbase,
  228. SYS_CPU_PLL_CTRL0(core));
  229. ctrl_val1 = nlm_read_sys_reg(sysbase,
  230. SYS_CPU_PLL_CTRL1(core));
  231. }
  232. /* Find PLL post divider value */
  233. switch ((ctrl_val0 >> 24) & 0x7) {
  234. case 1:
  235. pll_post_div = 2;
  236. break;
  237. case 3:
  238. pll_post_div = 4;
  239. break;
  240. case 7:
  241. pll_post_div = 8;
  242. break;
  243. case 6:
  244. pll_post_div = 16;
  245. break;
  246. case 0:
  247. default:
  248. pll_post_div = 1;
  249. break;
  250. }
  251. num = 1000000ULL * (400 * 3 + 100 * (ctrl_val1 & 0x3f));
  252. denom = 3 * pll_post_div;
  253. do_div(num, denom);
  254. return (unsigned int)num;
  255. }
  256. static unsigned int nlm_xlp_get_core_frequency(int node, int core)
  257. {
  258. unsigned int pll_divf, pll_divr, dfs_div, ext_div;
  259. unsigned int rstval, dfsval, denom;
  260. 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;
  268. num = 800000000ULL * pll_divf;
  269. denom = 3 * pll_divr * ext_div * dfs_div;
  270. do_div(num, denom);
  271. return (unsigned int)num;
  272. }
  273. unsigned int nlm_get_core_frequency(int node, int core)
  274. {
  275. if (cpu_is_xlpii())
  276. return nlm_xlp2_get_core_frequency(node, core);
  277. else
  278. return nlm_xlp_get_core_frequency(node, core);
  279. }
  280. /*
  281. * Calculate PIC frequency from PLL registers.
  282. * freq_out = (ref_freq/2 * (6 + ctrl2[7:0]) + ctrl2[20:8]/2^13) /
  283. * ((2^ctrl0[7:5]) * Table(ctrl0[26:24]))
  284. */
  285. static unsigned int nlm_xlp2_get_pic_frequency(int node)
  286. {
  287. u32 ctrl_val0, ctrl_val2, vco_post_div, pll_post_div, cpu_xlp9xx;
  288. u32 mdiv, fdiv, pll_out_freq_den, reg_select, ref_div, pic_div;
  289. u64 sysbase, pll_out_freq_num, ref_clk_select, clockbase, ref_clk;
  290. sysbase = nlm_get_node(node)->sysbase;
  291. clockbase = nlm_get_clock_regbase(node);
  292. cpu_xlp9xx = cpu_is_xlp9xx();
  293. /* Find ref_clk_base */
  294. if (cpu_xlp9xx)
  295. ref_clk_select = (nlm_read_sys_reg(sysbase,
  296. SYS_9XX_POWER_ON_RESET_CFG) >> 18) & 0x3;
  297. else
  298. ref_clk_select = (nlm_read_sys_reg(sysbase,
  299. SYS_POWER_ON_RESET_CFG) >> 18) & 0x3;
  300. switch (ref_clk_select) {
  301. case 0:
  302. ref_clk = 200000000ULL;
  303. ref_div = 3;
  304. break;
  305. case 1:
  306. ref_clk = 100000000ULL;
  307. ref_div = 1;
  308. break;
  309. case 2:
  310. ref_clk = 125000000ULL;
  311. ref_div = 1;
  312. break;
  313. case 3:
  314. ref_clk = 400000000ULL;
  315. ref_div = 3;
  316. break;
  317. }
  318. /* Find the clock source PLL device for PIC */
  319. if (cpu_xlp9xx) {
  320. reg_select = nlm_read_sys_reg(clockbase,
  321. SYS_9XX_CLK_DEV_SEL_REG) & 0x3;
  322. switch (reg_select) {
  323. case 0:
  324. ctrl_val0 = nlm_read_sys_reg(clockbase,
  325. SYS_9XX_PLL_CTRL0);
  326. ctrl_val2 = nlm_read_sys_reg(clockbase,
  327. SYS_9XX_PLL_CTRL2);
  328. break;
  329. case 1:
  330. ctrl_val0 = nlm_read_sys_reg(clockbase,
  331. SYS_9XX_PLL_CTRL0_DEVX(0));
  332. ctrl_val2 = nlm_read_sys_reg(clockbase,
  333. SYS_9XX_PLL_CTRL2_DEVX(0));
  334. break;
  335. case 2:
  336. ctrl_val0 = nlm_read_sys_reg(clockbase,
  337. SYS_9XX_PLL_CTRL0_DEVX(1));
  338. ctrl_val2 = nlm_read_sys_reg(clockbase,
  339. SYS_9XX_PLL_CTRL2_DEVX(1));
  340. break;
  341. case 3:
  342. ctrl_val0 = nlm_read_sys_reg(clockbase,
  343. SYS_9XX_PLL_CTRL0_DEVX(2));
  344. ctrl_val2 = nlm_read_sys_reg(clockbase,
  345. SYS_9XX_PLL_CTRL2_DEVX(2));
  346. break;
  347. }
  348. } else {
  349. reg_select = (nlm_read_sys_reg(sysbase,
  350. SYS_CLK_DEV_SEL_REG) >> 22) & 0x3;
  351. switch (reg_select) {
  352. case 0:
  353. ctrl_val0 = nlm_read_sys_reg(sysbase,
  354. SYS_PLL_CTRL0);
  355. ctrl_val2 = nlm_read_sys_reg(sysbase,
  356. SYS_PLL_CTRL2);
  357. break;
  358. case 1:
  359. ctrl_val0 = nlm_read_sys_reg(sysbase,
  360. SYS_PLL_CTRL0_DEVX(0));
  361. ctrl_val2 = nlm_read_sys_reg(sysbase,
  362. SYS_PLL_CTRL2_DEVX(0));
  363. break;
  364. case 2:
  365. ctrl_val0 = nlm_read_sys_reg(sysbase,
  366. SYS_PLL_CTRL0_DEVX(1));
  367. ctrl_val2 = nlm_read_sys_reg(sysbase,
  368. SYS_PLL_CTRL2_DEVX(1));
  369. break;
  370. case 3:
  371. ctrl_val0 = nlm_read_sys_reg(sysbase,
  372. SYS_PLL_CTRL0_DEVX(2));
  373. ctrl_val2 = nlm_read_sys_reg(sysbase,
  374. SYS_PLL_CTRL2_DEVX(2));
  375. break;
  376. }
  377. }
  378. vco_post_div = (ctrl_val0 >> 5) & 0x7;
  379. pll_post_div = (ctrl_val0 >> 24) & 0x7;
  380. mdiv = ctrl_val2 & 0xff;
  381. fdiv = (ctrl_val2 >> 8) & 0x1fff;
  382. /* Find PLL post divider value */
  383. switch (pll_post_div) {
  384. case 1:
  385. pll_post_div = 2;
  386. break;
  387. case 3:
  388. pll_post_div = 4;
  389. break;
  390. case 7:
  391. pll_post_div = 8;
  392. break;
  393. case 6:
  394. pll_post_div = 16;
  395. break;
  396. case 0:
  397. default:
  398. pll_post_div = 1;
  399. break;
  400. }
  401. fdiv = fdiv/(1 << 13);
  402. pll_out_freq_num = ((ref_clk >> 1) * (6 + mdiv)) + fdiv;
  403. pll_out_freq_den = (1 << vco_post_div) * pll_post_div * ref_div;
  404. if (pll_out_freq_den > 0)
  405. do_div(pll_out_freq_num, pll_out_freq_den);
  406. /* PIC post divider, which happens after PLL */
  407. if (cpu_xlp9xx)
  408. pic_div = nlm_read_sys_reg(clockbase,
  409. SYS_9XX_CLK_DEV_DIV_REG) & 0x3;
  410. else
  411. pic_div = (nlm_read_sys_reg(sysbase,
  412. SYS_CLK_DEV_DIV_REG) >> 22) & 0x3;
  413. do_div(pll_out_freq_num, 1 << pic_div);
  414. return pll_out_freq_num;
  415. }
  416. unsigned int nlm_get_pic_frequency(int node)
  417. {
  418. if (cpu_is_xlpii())
  419. return nlm_xlp2_get_pic_frequency(node);
  420. else
  421. return 133333333;
  422. }
  423. unsigned int nlm_get_cpu_frequency(void)
  424. {
  425. return nlm_get_core_frequency(0, 0);
  426. }
  427. /*
  428. * Fills upto 8 pairs of entries containing the DRAM map of a node
  429. * if node < 0, get dram map for all nodes
  430. */
  431. int nlm_get_dram_map(int node, uint64_t *dram_map, int nentries)
  432. {
  433. uint64_t bridgebase, base, lim;
  434. uint32_t val;
  435. unsigned int barreg, limreg, xlatreg;
  436. int i, n, rv;
  437. /* Look only at mapping on Node 0, we don't handle crazy configs */
  438. bridgebase = nlm_get_bridge_regbase(0);
  439. rv = 0;
  440. for (i = 0; i < 8; i++) {
  441. if (rv + 1 >= nentries)
  442. break;
  443. if (cpu_is_xlp9xx()) {
  444. barreg = BRIDGE_9XX_DRAM_BAR(i);
  445. limreg = BRIDGE_9XX_DRAM_LIMIT(i);
  446. xlatreg = BRIDGE_9XX_DRAM_NODE_TRANSLN(i);
  447. } else {
  448. barreg = BRIDGE_DRAM_BAR(i);
  449. limreg = BRIDGE_DRAM_LIMIT(i);
  450. xlatreg = BRIDGE_DRAM_NODE_TRANSLN(i);
  451. }
  452. if (node >= 0) {
  453. /* node specified, get node mapping of BAR */
  454. val = nlm_read_bridge_reg(bridgebase, xlatreg);
  455. n = (val >> 1) & 0x3;
  456. if (n != node)
  457. continue;
  458. }
  459. val = nlm_read_bridge_reg(bridgebase, barreg);
  460. val = (val >> 12) & 0xfffff;
  461. base = (uint64_t) val << 20;
  462. val = nlm_read_bridge_reg(bridgebase, limreg);
  463. val = (val >> 12) & 0xfffff;
  464. if (val == 0) /* BAR not used */
  465. continue;
  466. lim = ((uint64_t)val + 1) << 20;
  467. dram_map[rv] = base;
  468. dram_map[rv + 1] = lim;
  469. rv += 2;
  470. }
  471. return rv;
  472. }