nlm_hal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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() & (~((1 << 12) - 1));
  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_SPI_IRQ:
  89. return 152;
  90. case PIC_MMC_IRQ:
  91. return 153;
  92. case PIC_PCIE_LINK_LEGACY_IRQ(0):
  93. case PIC_PCIE_LINK_LEGACY_IRQ(1):
  94. case PIC_PCIE_LINK_LEGACY_IRQ(2):
  95. case PIC_PCIE_LINK_LEGACY_IRQ(3):
  96. return 191 + irq - PIC_PCIE_LINK_LEGACY_IRQ_BASE;
  97. }
  98. return -1;
  99. }
  100. static int xlp_irq_to_irt(int irq)
  101. {
  102. uint64_t pcibase;
  103. int devoff, irt;
  104. devoff = 0;
  105. switch (irq) {
  106. case PIC_UART_0_IRQ:
  107. devoff = XLP_IO_UART0_OFFSET(0);
  108. break;
  109. case PIC_UART_1_IRQ:
  110. devoff = XLP_IO_UART1_OFFSET(0);
  111. break;
  112. case PIC_MMC_IRQ:
  113. devoff = XLP_IO_MMC_OFFSET(0);
  114. break;
  115. case PIC_I2C_0_IRQ: /* I2C will be fixed up */
  116. case PIC_I2C_1_IRQ:
  117. 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);
  123. break;
  124. 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;
  136. default:
  137. 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. }
  172. }
  173. if (devoff != 0) {
  174. uint32_t val;
  175. pcibase = nlm_pcicfg_base(devoff);
  176. val = nlm_read_reg(pcibase, XLP_PCI_IRTINFO_REG);
  177. if (val == 0xffffffff) {
  178. irt = -1;
  179. } else {
  180. irt = val & 0xffff;
  181. /* HW weirdness, I2C IRT entry has to be fixed up */
  182. switch (irq) {
  183. case PIC_I2C_1_IRQ:
  184. irt = irt + 1; break;
  185. case PIC_I2C_2_IRQ:
  186. irt = irt + 2; break;
  187. case PIC_I2C_3_IRQ:
  188. irt = irt + 3; break;
  189. }
  190. }
  191. } else if (irq >= PIC_PCIE_LINK_LEGACY_IRQ(0) &&
  192. irq <= PIC_PCIE_LINK_LEGACY_IRQ(3)) {
  193. /* HW bug, PCI IRT entries are bad on early silicon, fix */
  194. irt = PIC_IRT_PCIE_LINK_INDEX(irq -
  195. PIC_PCIE_LINK_LEGACY_IRQ_BASE);
  196. } else {
  197. irt = -1;
  198. }
  199. return irt;
  200. }
  201. int nlm_irq_to_irt(int irq)
  202. {
  203. /* return -2 for irqs without 1-1 mapping */
  204. if (irq >= PIC_PCIE_LINK_MSI_IRQ(0) && irq <= PIC_PCIE_LINK_MSI_IRQ(3))
  205. return -2;
  206. if (irq >= PIC_PCIE_MSIX_IRQ(0) && irq <= PIC_PCIE_MSIX_IRQ(3))
  207. return -2;
  208. if (cpu_is_xlp9xx())
  209. return xlp9xx_irq_to_irt(irq);
  210. else
  211. return xlp_irq_to_irt(irq);
  212. }
  213. static unsigned int nlm_xlp2_get_core_frequency(int node, int core)
  214. {
  215. unsigned int pll_post_div, ctrl_val0, ctrl_val1, denom;
  216. uint64_t num, sysbase, clockbase;
  217. if (cpu_is_xlp9xx()) {
  218. clockbase = nlm_get_clock_regbase(node);
  219. ctrl_val0 = nlm_read_sys_reg(clockbase,
  220. SYS_9XX_CPU_PLL_CTRL0(core));
  221. ctrl_val1 = nlm_read_sys_reg(clockbase,
  222. SYS_9XX_CPU_PLL_CTRL1(core));
  223. } else {
  224. sysbase = nlm_get_node(node)->sysbase;
  225. ctrl_val0 = nlm_read_sys_reg(sysbase,
  226. SYS_CPU_PLL_CTRL0(core));
  227. ctrl_val1 = nlm_read_sys_reg(sysbase,
  228. SYS_CPU_PLL_CTRL1(core));
  229. }
  230. /* Find PLL post divider value */
  231. switch ((ctrl_val0 >> 24) & 0x7) {
  232. case 1:
  233. pll_post_div = 2;
  234. break;
  235. case 3:
  236. pll_post_div = 4;
  237. break;
  238. case 7:
  239. pll_post_div = 8;
  240. break;
  241. case 6:
  242. pll_post_div = 16;
  243. break;
  244. case 0:
  245. default:
  246. pll_post_div = 1;
  247. break;
  248. }
  249. num = 1000000ULL * (400 * 3 + 100 * (ctrl_val1 & 0x3f));
  250. denom = 3 * pll_post_div;
  251. do_div(num, denom);
  252. return (unsigned int)num;
  253. }
  254. static unsigned int nlm_xlp_get_core_frequency(int node, int core)
  255. {
  256. unsigned int pll_divf, pll_divr, dfs_div, ext_div;
  257. unsigned int rstval, dfsval, denom;
  258. uint64_t num, sysbase;
  259. sysbase = nlm_get_node(node)->sysbase;
  260. rstval = nlm_read_sys_reg(sysbase, SYS_POWER_ON_RESET_CFG);
  261. dfsval = nlm_read_sys_reg(sysbase, SYS_CORE_DFS_DIV_VALUE);
  262. pll_divf = ((rstval >> 10) & 0x7f) + 1;
  263. pll_divr = ((rstval >> 8) & 0x3) + 1;
  264. ext_div = ((rstval >> 30) & 0x3) + 1;
  265. dfs_div = ((dfsval >> (core * 4)) & 0xf) + 1;
  266. num = 800000000ULL * pll_divf;
  267. denom = 3 * pll_divr * ext_div * dfs_div;
  268. do_div(num, denom);
  269. return (unsigned int)num;
  270. }
  271. unsigned int nlm_get_core_frequency(int node, int core)
  272. {
  273. if (cpu_is_xlpii())
  274. return nlm_xlp2_get_core_frequency(node, core);
  275. else
  276. return nlm_xlp_get_core_frequency(node, core);
  277. }
  278. /*
  279. * Calculate PIC frequency from PLL registers.
  280. * freq_out = (ref_freq/2 * (6 + ctrl2[7:0]) + ctrl2[20:8]/2^13) /
  281. * ((2^ctrl0[7:5]) * Table(ctrl0[26:24]))
  282. */
  283. static unsigned int nlm_xlp2_get_pic_frequency(int node)
  284. {
  285. u32 ctrl_val0, ctrl_val2, vco_post_div, pll_post_div, cpu_xlp9xx;
  286. u32 mdiv, fdiv, pll_out_freq_den, reg_select, ref_div, pic_div;
  287. u64 sysbase, pll_out_freq_num, ref_clk_select, clockbase, ref_clk;
  288. sysbase = nlm_get_node(node)->sysbase;
  289. clockbase = nlm_get_clock_regbase(node);
  290. cpu_xlp9xx = cpu_is_xlp9xx();
  291. /* Find ref_clk_base */
  292. if (cpu_xlp9xx)
  293. ref_clk_select = (nlm_read_sys_reg(sysbase,
  294. SYS_9XX_POWER_ON_RESET_CFG) >> 18) & 0x3;
  295. else
  296. ref_clk_select = (nlm_read_sys_reg(sysbase,
  297. SYS_POWER_ON_RESET_CFG) >> 18) & 0x3;
  298. switch (ref_clk_select) {
  299. case 0:
  300. ref_clk = 200000000ULL;
  301. ref_div = 3;
  302. break;
  303. case 1:
  304. ref_clk = 100000000ULL;
  305. ref_div = 1;
  306. break;
  307. case 2:
  308. ref_clk = 125000000ULL;
  309. ref_div = 1;
  310. break;
  311. case 3:
  312. ref_clk = 400000000ULL;
  313. ref_div = 3;
  314. break;
  315. }
  316. /* Find the clock source PLL device for PIC */
  317. if (cpu_xlp9xx) {
  318. reg_select = nlm_read_sys_reg(clockbase,
  319. SYS_9XX_CLK_DEV_SEL_REG) & 0x3;
  320. switch (reg_select) {
  321. case 0:
  322. ctrl_val0 = nlm_read_sys_reg(clockbase,
  323. SYS_9XX_PLL_CTRL0);
  324. ctrl_val2 = nlm_read_sys_reg(clockbase,
  325. SYS_9XX_PLL_CTRL2);
  326. break;
  327. case 1:
  328. ctrl_val0 = nlm_read_sys_reg(clockbase,
  329. SYS_9XX_PLL_CTRL0_DEVX(0));
  330. ctrl_val2 = nlm_read_sys_reg(clockbase,
  331. SYS_9XX_PLL_CTRL2_DEVX(0));
  332. break;
  333. case 2:
  334. ctrl_val0 = nlm_read_sys_reg(clockbase,
  335. SYS_9XX_PLL_CTRL0_DEVX(1));
  336. ctrl_val2 = nlm_read_sys_reg(clockbase,
  337. SYS_9XX_PLL_CTRL2_DEVX(1));
  338. break;
  339. case 3:
  340. ctrl_val0 = nlm_read_sys_reg(clockbase,
  341. SYS_9XX_PLL_CTRL0_DEVX(2));
  342. ctrl_val2 = nlm_read_sys_reg(clockbase,
  343. SYS_9XX_PLL_CTRL2_DEVX(2));
  344. break;
  345. }
  346. } else {
  347. reg_select = (nlm_read_sys_reg(sysbase,
  348. SYS_CLK_DEV_SEL_REG) >> 22) & 0x3;
  349. switch (reg_select) {
  350. case 0:
  351. ctrl_val0 = nlm_read_sys_reg(sysbase,
  352. SYS_PLL_CTRL0);
  353. ctrl_val2 = nlm_read_sys_reg(sysbase,
  354. SYS_PLL_CTRL2);
  355. break;
  356. case 1:
  357. ctrl_val0 = nlm_read_sys_reg(sysbase,
  358. SYS_PLL_CTRL0_DEVX(0));
  359. ctrl_val2 = nlm_read_sys_reg(sysbase,
  360. SYS_PLL_CTRL2_DEVX(0));
  361. break;
  362. case 2:
  363. ctrl_val0 = nlm_read_sys_reg(sysbase,
  364. SYS_PLL_CTRL0_DEVX(1));
  365. ctrl_val2 = nlm_read_sys_reg(sysbase,
  366. SYS_PLL_CTRL2_DEVX(1));
  367. break;
  368. case 3:
  369. ctrl_val0 = nlm_read_sys_reg(sysbase,
  370. SYS_PLL_CTRL0_DEVX(2));
  371. ctrl_val2 = nlm_read_sys_reg(sysbase,
  372. SYS_PLL_CTRL2_DEVX(2));
  373. break;
  374. }
  375. }
  376. vco_post_div = (ctrl_val0 >> 5) & 0x7;
  377. pll_post_div = (ctrl_val0 >> 24) & 0x7;
  378. mdiv = ctrl_val2 & 0xff;
  379. fdiv = (ctrl_val2 >> 8) & 0x1fff;
  380. /* Find PLL post divider value */
  381. switch (pll_post_div) {
  382. case 1:
  383. pll_post_div = 2;
  384. break;
  385. case 3:
  386. pll_post_div = 4;
  387. break;
  388. case 7:
  389. pll_post_div = 8;
  390. break;
  391. case 6:
  392. pll_post_div = 16;
  393. break;
  394. case 0:
  395. default:
  396. pll_post_div = 1;
  397. break;
  398. }
  399. fdiv = fdiv/(1 << 13);
  400. pll_out_freq_num = ((ref_clk >> 1) * (6 + mdiv)) + fdiv;
  401. pll_out_freq_den = (1 << vco_post_div) * pll_post_div * ref_div;
  402. if (pll_out_freq_den > 0)
  403. do_div(pll_out_freq_num, pll_out_freq_den);
  404. /* PIC post divider, which happens after PLL */
  405. if (cpu_xlp9xx)
  406. pic_div = nlm_read_sys_reg(clockbase,
  407. SYS_9XX_CLK_DEV_DIV_REG) & 0x3;
  408. else
  409. pic_div = (nlm_read_sys_reg(sysbase,
  410. SYS_CLK_DEV_DIV_REG) >> 22) & 0x3;
  411. do_div(pll_out_freq_num, 1 << pic_div);
  412. return pll_out_freq_num;
  413. }
  414. unsigned int nlm_get_pic_frequency(int node)
  415. {
  416. if (cpu_is_xlpii())
  417. return nlm_xlp2_get_pic_frequency(node);
  418. else
  419. return 133333333;
  420. }
  421. unsigned int nlm_get_cpu_frequency(void)
  422. {
  423. return nlm_get_core_frequency(0, 0);
  424. }
  425. /*
  426. * Fills upto 8 pairs of entries containing the DRAM map of a node
  427. * if node < 0, get dram map for all nodes
  428. */
  429. int nlm_get_dram_map(int node, uint64_t *dram_map, int nentries)
  430. {
  431. uint64_t bridgebase, base, lim;
  432. uint32_t val;
  433. unsigned int barreg, limreg, xlatreg;
  434. int i, n, rv;
  435. /* Look only at mapping on Node 0, we don't handle crazy configs */
  436. bridgebase = nlm_get_bridge_regbase(0);
  437. rv = 0;
  438. for (i = 0; i < 8; i++) {
  439. if (rv + 1 >= nentries)
  440. break;
  441. if (cpu_is_xlp9xx()) {
  442. barreg = BRIDGE_9XX_DRAM_BAR(i);
  443. limreg = BRIDGE_9XX_DRAM_LIMIT(i);
  444. xlatreg = BRIDGE_9XX_DRAM_NODE_TRANSLN(i);
  445. } else {
  446. barreg = BRIDGE_DRAM_BAR(i);
  447. limreg = BRIDGE_DRAM_LIMIT(i);
  448. xlatreg = BRIDGE_DRAM_NODE_TRANSLN(i);
  449. }
  450. if (node >= 0) {
  451. /* node specified, get node mapping of BAR */
  452. val = nlm_read_bridge_reg(bridgebase, xlatreg);
  453. n = (val >> 1) & 0x3;
  454. if (n != node)
  455. continue;
  456. }
  457. val = nlm_read_bridge_reg(bridgebase, barreg);
  458. val = (val >> 12) & 0xfffff;
  459. base = (uint64_t) val << 20;
  460. val = nlm_read_bridge_reg(bridgebase, limreg);
  461. val = (val >> 12) & 0xfffff;
  462. if (val == 0) /* BAR not used */
  463. continue;
  464. lim = ((uint64_t)val + 1) << 20;
  465. dram_map[rv] = base;
  466. dram_map[rv + 1] = lim;
  467. rv += 2;
  468. }
  469. return rv;
  470. }