nlm_hal.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. nodep->sysbase = nlm_get_sys_regbase(node);
  53. nodep->picbase = nlm_get_pic_regbase(node);
  54. nodep->ebase = read_c0_ebase() & (~((1 << 12) - 1));
  55. if (cpu_is_xlp9xx())
  56. nodep->socbus = xlp9xx_get_socbus(node);
  57. else
  58. nodep->socbus = 0;
  59. spin_lock_init(&nodep->piclock);
  60. }
  61. int nlm_irq_to_irt(int irq)
  62. {
  63. uint64_t pcibase;
  64. int devoff, irt;
  65. /* bypass for 9xx */
  66. if (cpu_is_xlp9xx()) {
  67. switch (irq) {
  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_PCIE_LINK_LEGACY_IRQ(0):
  77. case PIC_PCIE_LINK_LEGACY_IRQ(1):
  78. case PIC_PCIE_LINK_LEGACY_IRQ(2):
  79. case PIC_PCIE_LINK_LEGACY_IRQ(3):
  80. return 191 + irq - PIC_PCIE_LINK_LEGACY_IRQ_BASE;
  81. }
  82. return -1;
  83. }
  84. devoff = 0;
  85. switch (irq) {
  86. case PIC_UART_0_IRQ:
  87. devoff = XLP_IO_UART0_OFFSET(0);
  88. break;
  89. case PIC_UART_1_IRQ:
  90. devoff = XLP_IO_UART1_OFFSET(0);
  91. break;
  92. case PIC_MMC_IRQ:
  93. devoff = XLP_IO_SD_OFFSET(0);
  94. break;
  95. case PIC_I2C_0_IRQ: /* I2C will be fixed up */
  96. case PIC_I2C_1_IRQ:
  97. case PIC_I2C_2_IRQ:
  98. case PIC_I2C_3_IRQ:
  99. if (cpu_is_xlpii())
  100. devoff = XLP2XX_IO_I2C_OFFSET(0);
  101. else
  102. devoff = XLP_IO_I2C0_OFFSET(0);
  103. break;
  104. default:
  105. if (cpu_is_xlpii()) {
  106. switch (irq) {
  107. /* XLP2XX has three XHCI USB controller */
  108. case PIC_2XX_XHCI_0_IRQ:
  109. devoff = XLP2XX_IO_USB_XHCI0_OFFSET(0);
  110. break;
  111. case PIC_2XX_XHCI_1_IRQ:
  112. devoff = XLP2XX_IO_USB_XHCI1_OFFSET(0);
  113. break;
  114. case PIC_2XX_XHCI_2_IRQ:
  115. devoff = XLP2XX_IO_USB_XHCI2_OFFSET(0);
  116. break;
  117. }
  118. } else {
  119. switch (irq) {
  120. case PIC_EHCI_0_IRQ:
  121. devoff = XLP_IO_USB_EHCI0_OFFSET(0);
  122. break;
  123. case PIC_EHCI_1_IRQ:
  124. devoff = XLP_IO_USB_EHCI1_OFFSET(0);
  125. break;
  126. case PIC_OHCI_0_IRQ:
  127. devoff = XLP_IO_USB_OHCI0_OFFSET(0);
  128. break;
  129. case PIC_OHCI_1_IRQ:
  130. devoff = XLP_IO_USB_OHCI1_OFFSET(0);
  131. break;
  132. case PIC_OHCI_2_IRQ:
  133. devoff = XLP_IO_USB_OHCI2_OFFSET(0);
  134. break;
  135. case PIC_OHCI_3_IRQ:
  136. devoff = XLP_IO_USB_OHCI3_OFFSET(0);
  137. break;
  138. }
  139. }
  140. }
  141. if (devoff != 0) {
  142. pcibase = nlm_pcicfg_base(devoff);
  143. irt = nlm_read_reg(pcibase, XLP_PCI_IRTINFO_REG) & 0xffff;
  144. /* HW weirdness, I2C IRT entry has to be fixed up */
  145. switch (irq) {
  146. case PIC_I2C_1_IRQ:
  147. irt = irt + 1; break;
  148. case PIC_I2C_2_IRQ:
  149. irt = irt + 2; break;
  150. case PIC_I2C_3_IRQ:
  151. irt = irt + 3; break;
  152. }
  153. } else if (irq >= PIC_PCIE_LINK_LEGACY_IRQ(0) &&
  154. irq <= PIC_PCIE_LINK_LEGACY_IRQ(3)) {
  155. /* HW bug, PCI IRT entries are bad on early silicon, fix */
  156. irt = PIC_IRT_PCIE_LINK_INDEX(irq -
  157. PIC_PCIE_LINK_LEGACY_IRQ_BASE);
  158. } else if (irq >= PIC_PCIE_LINK_MSI_IRQ(0) &&
  159. irq <= PIC_PCIE_LINK_MSI_IRQ(3)) {
  160. irt = -2;
  161. } else if (irq >= PIC_PCIE_MSIX_IRQ(0) &&
  162. irq <= PIC_PCIE_MSIX_IRQ(3)) {
  163. irt = -2;
  164. } else {
  165. irt = -1;
  166. }
  167. return irt;
  168. }
  169. unsigned int nlm_get_core_frequency(int node, int core)
  170. {
  171. unsigned int pll_divf, pll_divr, dfs_div, ext_div;
  172. unsigned int rstval, dfsval, denom;
  173. uint64_t num, sysbase;
  174. sysbase = nlm_get_node(node)->sysbase;
  175. if (cpu_is_xlp9xx())
  176. rstval = nlm_read_sys_reg(sysbase, SYS_9XX_POWER_ON_RESET_CFG);
  177. else
  178. rstval = nlm_read_sys_reg(sysbase, SYS_POWER_ON_RESET_CFG);
  179. if (cpu_is_xlpii()) {
  180. num = 1000000ULL * (400 * 3 + 100 * (rstval >> 26));
  181. denom = 3;
  182. } else {
  183. dfsval = nlm_read_sys_reg(sysbase, SYS_CORE_DFS_DIV_VALUE);
  184. pll_divf = ((rstval >> 10) & 0x7f) + 1;
  185. pll_divr = ((rstval >> 8) & 0x3) + 1;
  186. ext_div = ((rstval >> 30) & 0x3) + 1;
  187. dfs_div = ((dfsval >> (core * 4)) & 0xf) + 1;
  188. num = 800000000ULL * pll_divf;
  189. denom = 3 * pll_divr * ext_div * dfs_div;
  190. }
  191. do_div(num, denom);
  192. return (unsigned int)num;
  193. }
  194. /* Calculate Frequency to the PIC from PLL.
  195. * freq_out = ( ref_freq/2 * (6 + ctrl2[7:0]) + ctrl2[20:8]/2^13 ) /
  196. * ((2^ctrl0[7:5]) * Table(ctrl0[26:24]))
  197. */
  198. static unsigned int nlm_2xx_get_pic_frequency(int node)
  199. {
  200. u32 ctrl_val0, ctrl_val2, vco_post_div, pll_post_div;
  201. u32 mdiv, fdiv, pll_out_freq_den, reg_select, ref_div, pic_div;
  202. u64 ref_clk, sysbase, pll_out_freq_num, ref_clk_select;
  203. sysbase = nlm_get_node(node)->sysbase;
  204. /* Find ref_clk_base */
  205. ref_clk_select =
  206. (nlm_read_sys_reg(sysbase, SYS_POWER_ON_RESET_CFG) >> 18) & 0x3;
  207. switch (ref_clk_select) {
  208. case 0:
  209. ref_clk = 200000000ULL;
  210. ref_div = 3;
  211. break;
  212. case 1:
  213. ref_clk = 100000000ULL;
  214. ref_div = 1;
  215. break;
  216. case 2:
  217. ref_clk = 125000000ULL;
  218. ref_div = 1;
  219. break;
  220. case 3:
  221. ref_clk = 400000000ULL;
  222. ref_div = 3;
  223. break;
  224. }
  225. /* Find the clock source PLL device for PIC */
  226. reg_select = (nlm_read_sys_reg(sysbase, SYS_CLK_DEV_SEL) >> 22) & 0x3;
  227. switch (reg_select) {
  228. case 0:
  229. ctrl_val0 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL0);
  230. ctrl_val2 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL2);
  231. break;
  232. case 1:
  233. ctrl_val0 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL0_DEVX(0));
  234. ctrl_val2 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL2_DEVX(0));
  235. break;
  236. case 2:
  237. ctrl_val0 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL0_DEVX(1));
  238. ctrl_val2 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL2_DEVX(1));
  239. break;
  240. case 3:
  241. ctrl_val0 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL0_DEVX(2));
  242. ctrl_val2 = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL2_DEVX(2));
  243. break;
  244. }
  245. vco_post_div = (ctrl_val0 >> 5) & 0x7;
  246. pll_post_div = (ctrl_val0 >> 24) & 0x7;
  247. mdiv = ctrl_val2 & 0xff;
  248. fdiv = (ctrl_val2 >> 8) & 0xfff;
  249. /* Find PLL post divider value */
  250. switch (pll_post_div) {
  251. case 1:
  252. pll_post_div = 2;
  253. break;
  254. case 3:
  255. pll_post_div = 4;
  256. break;
  257. case 7:
  258. pll_post_div = 8;
  259. break;
  260. case 6:
  261. pll_post_div = 16;
  262. break;
  263. case 0:
  264. default:
  265. pll_post_div = 1;
  266. break;
  267. }
  268. fdiv = fdiv/(1 << 13);
  269. pll_out_freq_num = ((ref_clk >> 1) * (6 + mdiv)) + fdiv;
  270. pll_out_freq_den = (1 << vco_post_div) * pll_post_div * 3;
  271. if (pll_out_freq_den > 0)
  272. do_div(pll_out_freq_num, pll_out_freq_den);
  273. /* PIC post divider, which happens after PLL */
  274. pic_div = (nlm_read_sys_reg(sysbase, SYS_CLK_DEV_DIV) >> 22) & 0x3;
  275. do_div(pll_out_freq_num, 1 << pic_div);
  276. return pll_out_freq_num;
  277. }
  278. unsigned int nlm_get_pic_frequency(int node)
  279. {
  280. /* TODO Has to calculate freq as like 2xx */
  281. if (cpu_is_xlp9xx())
  282. return 250000000;
  283. if (cpu_is_xlpii())
  284. return nlm_2xx_get_pic_frequency(node);
  285. else
  286. return 133333333;
  287. }
  288. unsigned int nlm_get_cpu_frequency(void)
  289. {
  290. return nlm_get_core_frequency(0, 0);
  291. }
  292. /*
  293. * Fills upto 8 pairs of entries containing the DRAM map of a node
  294. * if n < 0, get dram map for all nodes
  295. */
  296. int xlp_get_dram_map(int n, uint64_t *dram_map)
  297. {
  298. uint64_t bridgebase, base, lim;
  299. uint32_t val;
  300. unsigned int barreg, limreg, xlatreg;
  301. int i, node, rv;
  302. /* Look only at mapping on Node 0, we don't handle crazy configs */
  303. bridgebase = nlm_get_bridge_regbase(0);
  304. rv = 0;
  305. for (i = 0; i < 8; i++) {
  306. if (cpu_is_xlp9xx()) {
  307. barreg = BRIDGE_9XX_DRAM_BAR(i);
  308. limreg = BRIDGE_9XX_DRAM_LIMIT(i);
  309. xlatreg = BRIDGE_9XX_DRAM_NODE_TRANSLN(i);
  310. } else {
  311. barreg = BRIDGE_DRAM_BAR(i);
  312. limreg = BRIDGE_DRAM_LIMIT(i);
  313. xlatreg = BRIDGE_DRAM_NODE_TRANSLN(i);
  314. }
  315. if (n >= 0) {
  316. /* node specified, get node mapping of BAR */
  317. val = nlm_read_bridge_reg(bridgebase, xlatreg);
  318. node = (val >> 1) & 0x3;
  319. if (n != node)
  320. continue;
  321. }
  322. val = nlm_read_bridge_reg(bridgebase, barreg);
  323. val = (val >> 12) & 0xfffff;
  324. base = (uint64_t) val << 20;
  325. val = nlm_read_bridge_reg(bridgebase, limreg);
  326. val = (val >> 12) & 0xfffff;
  327. if (val == 0) /* BAR not used */
  328. continue;
  329. lim = ((uint64_t)val + 1) << 20;
  330. dram_map[rv] = base;
  331. dram_map[rv + 1] = lim;
  332. rv += 2;
  333. }
  334. return rv;
  335. }