nlm_hal.c 12 KB

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