pci-xlp.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright (c) 2003-2012 Broadcom Corporation
  3. * All Rights 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 Broadcom
  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 BROADCOM ``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 BROADCOM 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/pci.h>
  36. #include <linux/kernel.h>
  37. #include <linux/init.h>
  38. #include <linux/msi.h>
  39. #include <linux/mm.h>
  40. #include <linux/irq.h>
  41. #include <linux/irqdesc.h>
  42. #include <linux/console.h>
  43. #include <asm/io.h>
  44. #include <asm/netlogic/interrupt.h>
  45. #include <asm/netlogic/haldefs.h>
  46. #include <asm/netlogic/common.h>
  47. #include <asm/netlogic/mips-extns.h>
  48. #include <asm/netlogic/xlp-hal/iomap.h>
  49. #include <asm/netlogic/xlp-hal/xlp.h>
  50. #include <asm/netlogic/xlp-hal/pic.h>
  51. #include <asm/netlogic/xlp-hal/pcibus.h>
  52. #include <asm/netlogic/xlp-hal/bridge.h>
  53. static void *pci_config_base;
  54. #define pci_cfg_addr(bus, devfn, off) (((bus) << 20) | ((devfn) << 12) | (off))
  55. /* PCI ops */
  56. static inline u32 pci_cfg_read_32bit(struct pci_bus *bus, unsigned int devfn,
  57. int where)
  58. {
  59. u32 data;
  60. u32 *cfgaddr;
  61. where &= ~3;
  62. if (cpu_is_xlp9xx()) {
  63. /* be very careful on SoC buses */
  64. if (bus->number == 0) {
  65. /* Scan only existing nodes - uboot bug? */
  66. if (PCI_SLOT(devfn) != 0 ||
  67. !nlm_node_present(PCI_FUNC(devfn)))
  68. return 0xffffffff;
  69. } else if (bus->parent->number == 0) { /* SoC bus */
  70. if (PCI_SLOT(devfn) == 0) /* b.0.0 hangs */
  71. return 0xffffffff;
  72. if (devfn == 44) /* b.5.4 hangs */
  73. return 0xffffffff;
  74. }
  75. } else if (bus->number == 0 && PCI_SLOT(devfn) == 1 && where == 0x954) {
  76. return 0xffffffff;
  77. }
  78. cfgaddr = (u32 *)(pci_config_base +
  79. pci_cfg_addr(bus->number, devfn, where));
  80. data = *cfgaddr;
  81. return data;
  82. }
  83. static inline void pci_cfg_write_32bit(struct pci_bus *bus, unsigned int devfn,
  84. int where, u32 data)
  85. {
  86. u32 *cfgaddr;
  87. cfgaddr = (u32 *)(pci_config_base +
  88. pci_cfg_addr(bus->number, devfn, where & ~3));
  89. *cfgaddr = data;
  90. }
  91. static int nlm_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  92. int where, int size, u32 *val)
  93. {
  94. u32 data;
  95. if ((size == 2) && (where & 1))
  96. return PCIBIOS_BAD_REGISTER_NUMBER;
  97. else if ((size == 4) && (where & 3))
  98. return PCIBIOS_BAD_REGISTER_NUMBER;
  99. data = pci_cfg_read_32bit(bus, devfn, where);
  100. if (size == 1)
  101. *val = (data >> ((where & 3) << 3)) & 0xff;
  102. else if (size == 2)
  103. *val = (data >> ((where & 3) << 3)) & 0xffff;
  104. else
  105. *val = data;
  106. return PCIBIOS_SUCCESSFUL;
  107. }
  108. static int nlm_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  109. int where, int size, u32 val)
  110. {
  111. u32 data;
  112. if ((size == 2) && (where & 1))
  113. return PCIBIOS_BAD_REGISTER_NUMBER;
  114. else if ((size == 4) && (where & 3))
  115. return PCIBIOS_BAD_REGISTER_NUMBER;
  116. data = pci_cfg_read_32bit(bus, devfn, where);
  117. if (size == 1)
  118. data = (data & ~(0xff << ((where & 3) << 3))) |
  119. (val << ((where & 3) << 3));
  120. else if (size == 2)
  121. data = (data & ~(0xffff << ((where & 3) << 3))) |
  122. (val << ((where & 3) << 3));
  123. else
  124. data = val;
  125. pci_cfg_write_32bit(bus, devfn, where, data);
  126. return PCIBIOS_SUCCESSFUL;
  127. }
  128. struct pci_ops nlm_pci_ops = {
  129. .read = nlm_pcibios_read,
  130. .write = nlm_pcibios_write
  131. };
  132. static struct resource nlm_pci_mem_resource = {
  133. .name = "XLP PCI MEM",
  134. .start = 0xd0000000UL, /* 256MB PCI mem @ 0xd000_0000 */
  135. .end = 0xdfffffffUL,
  136. .flags = IORESOURCE_MEM,
  137. };
  138. static struct resource nlm_pci_io_resource = {
  139. .name = "XLP IO MEM",
  140. .start = 0x14000000UL, /* 64MB PCI IO @ 0x1000_0000 */
  141. .end = 0x17ffffffUL,
  142. .flags = IORESOURCE_IO,
  143. };
  144. struct pci_controller nlm_pci_controller = {
  145. .index = 0,
  146. .pci_ops = &nlm_pci_ops,
  147. .mem_resource = &nlm_pci_mem_resource,
  148. .mem_offset = 0x00000000UL,
  149. .io_resource = &nlm_pci_io_resource,
  150. .io_offset = 0x00000000UL,
  151. };
  152. struct pci_dev *xlp_get_pcie_link(const struct pci_dev *dev)
  153. {
  154. struct pci_bus *bus, *p;
  155. bus = dev->bus;
  156. if (cpu_is_xlp9xx()) {
  157. /* find bus with grand parent number == 0 */
  158. for (p = bus->parent; p && p->parent && p->parent->number != 0;
  159. p = p->parent)
  160. bus = p;
  161. return (p && p->parent) ? bus->self : NULL;
  162. } else {
  163. /* Find the bridge on bus 0 */
  164. for (p = bus->parent; p && p->number != 0; p = p->parent)
  165. bus = p;
  166. return p ? bus->self : NULL;
  167. }
  168. }
  169. int xlp_socdev_to_node(const struct pci_dev *lnkdev)
  170. {
  171. if (cpu_is_xlp9xx())
  172. return PCI_FUNC(lnkdev->bus->self->devfn);
  173. else
  174. return PCI_SLOT(lnkdev->devfn) / 8;
  175. }
  176. int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  177. {
  178. struct pci_dev *lnkdev;
  179. int lnkfunc, node;
  180. /*
  181. * For XLP PCIe, there is an IRQ per Link, find out which
  182. * link the device is on to assign interrupts
  183. */
  184. lnkdev = xlp_get_pcie_link(dev);
  185. if (lnkdev == NULL)
  186. return 0;
  187. lnkfunc = PCI_FUNC(lnkdev->devfn);
  188. node = xlp_socdev_to_node(lnkdev);
  189. return nlm_irq_to_xirq(node, PIC_PCIE_LINK_LEGACY_IRQ(lnkfunc));
  190. }
  191. /* Do platform specific device initialization at pci_enable_device() time */
  192. int pcibios_plat_dev_init(struct pci_dev *dev)
  193. {
  194. return 0;
  195. }
  196. /*
  197. * If big-endian, enable hardware byteswap on the PCIe bridges.
  198. * This will make both the SoC and PCIe devices behave consistently with
  199. * readl/writel.
  200. */
  201. #ifdef __BIG_ENDIAN
  202. static void xlp_config_pci_bswap(int node, int link)
  203. {
  204. uint64_t nbubase, lnkbase;
  205. u32 reg;
  206. nbubase = nlm_get_bridge_regbase(node);
  207. lnkbase = nlm_get_pcie_base(node, link);
  208. /*
  209. * Enable byte swap in hardware. Program each link's PCIe SWAP regions
  210. * from the link's address ranges.
  211. */
  212. if (cpu_is_xlp9xx()) {
  213. reg = nlm_read_bridge_reg(nbubase,
  214. BRIDGE_9XX_PCIEMEM_BASE0 + link);
  215. nlm_write_pci_reg(lnkbase, PCIE_9XX_BYTE_SWAP_MEM_BASE, reg);
  216. reg = nlm_read_bridge_reg(nbubase,
  217. BRIDGE_9XX_PCIEMEM_LIMIT0 + link);
  218. nlm_write_pci_reg(lnkbase,
  219. PCIE_9XX_BYTE_SWAP_MEM_LIM, reg | 0xfff);
  220. reg = nlm_read_bridge_reg(nbubase,
  221. BRIDGE_9XX_PCIEIO_BASE0 + link);
  222. nlm_write_pci_reg(lnkbase, PCIE_9XX_BYTE_SWAP_IO_BASE, reg);
  223. reg = nlm_read_bridge_reg(nbubase,
  224. BRIDGE_9XX_PCIEIO_LIMIT0 + link);
  225. nlm_write_pci_reg(lnkbase,
  226. PCIE_9XX_BYTE_SWAP_IO_LIM, reg | 0xfff);
  227. } else {
  228. reg = nlm_read_bridge_reg(nbubase, BRIDGE_PCIEMEM_BASE0 + link);
  229. nlm_write_pci_reg(lnkbase, PCIE_BYTE_SWAP_MEM_BASE, reg);
  230. reg = nlm_read_bridge_reg(nbubase,
  231. BRIDGE_PCIEMEM_LIMIT0 + link);
  232. nlm_write_pci_reg(lnkbase, PCIE_BYTE_SWAP_MEM_LIM, reg | 0xfff);
  233. reg = nlm_read_bridge_reg(nbubase, BRIDGE_PCIEIO_BASE0 + link);
  234. nlm_write_pci_reg(lnkbase, PCIE_BYTE_SWAP_IO_BASE, reg);
  235. reg = nlm_read_bridge_reg(nbubase, BRIDGE_PCIEIO_LIMIT0 + link);
  236. nlm_write_pci_reg(lnkbase, PCIE_BYTE_SWAP_IO_LIM, reg | 0xfff);
  237. }
  238. }
  239. #else
  240. /* Swap configuration not needed in little-endian mode */
  241. static inline void xlp_config_pci_bswap(int node, int link) {}
  242. #endif /* __BIG_ENDIAN */
  243. static int __init pcibios_init(void)
  244. {
  245. uint64_t pciebase;
  246. int link, n;
  247. u32 reg;
  248. /* Firmware assigns PCI resources */
  249. pci_set_flags(PCI_PROBE_ONLY);
  250. pci_config_base = ioremap(XLP_DEFAULT_PCI_ECFG_BASE, 64 << 20);
  251. /* Extend IO port for memory mapped io */
  252. ioport_resource.start = 0;
  253. ioport_resource.end = ~0;
  254. for (n = 0; n < NLM_NR_NODES; n++) {
  255. if (!nlm_node_present(n))
  256. continue;
  257. for (link = 0; link < PCIE_NLINKS; link++) {
  258. pciebase = nlm_get_pcie_base(n, link);
  259. if (nlm_read_pci_reg(pciebase, 0) == 0xffffffff)
  260. continue;
  261. xlp_config_pci_bswap(n, link);
  262. xlp_init_node_msi_irqs(n, link);
  263. /* put in intpin and irq - u-boot does not */
  264. reg = nlm_read_pci_reg(pciebase, 0xf);
  265. reg &= ~0x1ffu;
  266. reg |= (1 << 8) | PIC_PCIE_LINK_LEGACY_IRQ(link);
  267. nlm_write_pci_reg(pciebase, 0xf, reg);
  268. pr_info("XLP PCIe: Link %d-%d initialized.\n", n, link);
  269. }
  270. }
  271. set_io_port_base(CKSEG1);
  272. nlm_pci_controller.io_map_base = CKSEG1;
  273. register_pci_controller(&nlm_pci_controller);
  274. pr_info("XLP PCIe Controller %pR%pR.\n", &nlm_pci_io_resource,
  275. &nlm_pci_mem_resource);
  276. return 0;
  277. }
  278. arch_initcall(pcibios_init);