nlm_hal.c 8.3 KB

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