pci-xlp.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/xlp-hal/iomap.h>
  48. #include <asm/netlogic/xlp-hal/pic.h>
  49. #include <asm/netlogic/xlp-hal/xlp.h>
  50. #include <asm/netlogic/xlp-hal/pcibus.h>
  51. #include <asm/netlogic/xlp-hal/bridge.h>
  52. static void *pci_config_base;
  53. #define pci_cfg_addr(bus, devfn, off) (((bus) << 20) | ((devfn) << 12) | (off))
  54. /* PCI ops */
  55. static inline u32 pci_cfg_read_32bit(struct pci_bus *bus, unsigned int devfn,
  56. int where)
  57. {
  58. u32 data;
  59. u32 *cfgaddr;
  60. where &= ~3;
  61. if (bus->number == 0 && PCI_SLOT(devfn) == 1 && where == 0x954)
  62. return 0xffffffff;
  63. cfgaddr = (u32 *)(pci_config_base +
  64. pci_cfg_addr(bus->number, devfn, where));
  65. data = *cfgaddr;
  66. return data;
  67. }
  68. static inline void pci_cfg_write_32bit(struct pci_bus *bus, unsigned int devfn,
  69. int where, u32 data)
  70. {
  71. u32 *cfgaddr;
  72. cfgaddr = (u32 *)(pci_config_base +
  73. pci_cfg_addr(bus->number, devfn, where & ~3));
  74. *cfgaddr = data;
  75. }
  76. static int nlm_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  77. int where, int size, u32 *val)
  78. {
  79. u32 data;
  80. if ((size == 2) && (where & 1))
  81. return PCIBIOS_BAD_REGISTER_NUMBER;
  82. else if ((size == 4) && (where & 3))
  83. return PCIBIOS_BAD_REGISTER_NUMBER;
  84. data = pci_cfg_read_32bit(bus, devfn, where);
  85. if (size == 1)
  86. *val = (data >> ((where & 3) << 3)) & 0xff;
  87. else if (size == 2)
  88. *val = (data >> ((where & 3) << 3)) & 0xffff;
  89. else
  90. *val = data;
  91. return PCIBIOS_SUCCESSFUL;
  92. }
  93. static int nlm_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  94. int where, int size, u32 val)
  95. {
  96. u32 data;
  97. if ((size == 2) && (where & 1))
  98. return PCIBIOS_BAD_REGISTER_NUMBER;
  99. else if ((size == 4) && (where & 3))
  100. return PCIBIOS_BAD_REGISTER_NUMBER;
  101. data = pci_cfg_read_32bit(bus, devfn, where);
  102. if (size == 1)
  103. data = (data & ~(0xff << ((where & 3) << 3))) |
  104. (val << ((where & 3) << 3));
  105. else if (size == 2)
  106. data = (data & ~(0xffff << ((where & 3) << 3))) |
  107. (val << ((where & 3) << 3));
  108. else
  109. data = val;
  110. pci_cfg_write_32bit(bus, devfn, where, data);
  111. return PCIBIOS_SUCCESSFUL;
  112. }
  113. struct pci_ops nlm_pci_ops = {
  114. .read = nlm_pcibios_read,
  115. .write = nlm_pcibios_write
  116. };
  117. static struct resource nlm_pci_mem_resource = {
  118. .name = "XLP PCI MEM",
  119. .start = 0xd0000000UL, /* 256MB PCI mem @ 0xd000_0000 */
  120. .end = 0xdfffffffUL,
  121. .flags = IORESOURCE_MEM,
  122. };
  123. static struct resource nlm_pci_io_resource = {
  124. .name = "XLP IO MEM",
  125. .start = 0x14000000UL, /* 64MB PCI IO @ 0x1000_0000 */
  126. .end = 0x17ffffffUL,
  127. .flags = IORESOURCE_IO,
  128. };
  129. struct pci_controller nlm_pci_controller = {
  130. .index = 0,
  131. .pci_ops = &nlm_pci_ops,
  132. .mem_resource = &nlm_pci_mem_resource,
  133. .mem_offset = 0x00000000UL,
  134. .io_resource = &nlm_pci_io_resource,
  135. .io_offset = 0x00000000UL,
  136. };
  137. static struct pci_dev *xlp_get_pcie_link(const struct pci_dev *dev)
  138. {
  139. struct pci_bus *bus, *p;
  140. /* Find the bridge on bus 0 */
  141. bus = dev->bus;
  142. for (p = bus->parent; p && p->number != 0; p = p->parent)
  143. bus = p;
  144. return p ? bus->self : NULL;
  145. }
  146. static inline int nlm_pci_link_to_irq(int link)
  147. {
  148. return PIC_PCIE_LINK_0_IRQ + link;
  149. }
  150. int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  151. {
  152. struct pci_dev *lnkdev;
  153. int lnkslot, lnkfunc;
  154. /*
  155. * For XLP PCIe, there is an IRQ per Link, find out which
  156. * link the device is on to assign interrupts
  157. */
  158. lnkdev = xlp_get_pcie_link(dev);
  159. if (lnkdev == NULL)
  160. return 0;
  161. lnkfunc = PCI_FUNC(lnkdev->devfn);
  162. lnkslot = PCI_SLOT(lnkdev->devfn);
  163. return nlm_irq_to_xirq(lnkslot / 8, nlm_pci_link_to_irq(lnkfunc));
  164. }
  165. /* Do platform specific device initialization at pci_enable_device() time */
  166. int pcibios_plat_dev_init(struct pci_dev *dev)
  167. {
  168. return 0;
  169. }
  170. /*
  171. * If big-endian, enable hardware byteswap on the PCIe bridges.
  172. * This will make both the SoC and PCIe devices behave consistently with
  173. * readl/writel.
  174. */
  175. #ifdef __BIG_ENDIAN
  176. static void xlp_config_pci_bswap(int node, int link)
  177. {
  178. uint64_t nbubase, lnkbase;
  179. u32 reg;
  180. nbubase = nlm_get_bridge_regbase(node);
  181. lnkbase = nlm_get_pcie_base(node, link);
  182. /*
  183. * Enable byte swap in hardware. Program each link's PCIe SWAP regions
  184. * from the link's address ranges.
  185. */
  186. reg = nlm_read_bridge_reg(nbubase, BRIDGE_PCIEMEM_BASE0 + link);
  187. nlm_write_pci_reg(lnkbase, PCIE_BYTE_SWAP_MEM_BASE, reg);
  188. reg = nlm_read_bridge_reg(nbubase, BRIDGE_PCIEMEM_LIMIT0 + link);
  189. nlm_write_pci_reg(lnkbase, PCIE_BYTE_SWAP_MEM_LIM, reg | 0xfff);
  190. reg = nlm_read_bridge_reg(nbubase, BRIDGE_PCIEIO_BASE0 + link);
  191. nlm_write_pci_reg(lnkbase, PCIE_BYTE_SWAP_IO_BASE, reg);
  192. reg = nlm_read_bridge_reg(nbubase, BRIDGE_PCIEIO_LIMIT0 + link);
  193. nlm_write_pci_reg(lnkbase, PCIE_BYTE_SWAP_IO_LIM, reg | 0xfff);
  194. }
  195. #else
  196. /* Swap configuration not needed in little-endian mode */
  197. static inline void xlp_config_pci_bswap(int node, int link) {}
  198. #endif /* __BIG_ENDIAN */
  199. static int __init pcibios_init(void)
  200. {
  201. struct nlm_soc_info *nodep;
  202. uint64_t pciebase;
  203. int link, n;
  204. u32 reg;
  205. /* Firmware assigns PCI resources */
  206. pci_set_flags(PCI_PROBE_ONLY);
  207. pci_config_base = ioremap(XLP_DEFAULT_PCI_ECFG_BASE, 64 << 20);
  208. /* Extend IO port for memory mapped io */
  209. ioport_resource.start = 0;
  210. ioport_resource.end = ~0;
  211. for (n = 0; n < NLM_NR_NODES; n++) {
  212. nodep = nlm_get_node(n);
  213. if (!nodep->coremask)
  214. continue; /* node does not exist */
  215. for (link = 0; link < 4; link++) {
  216. pciebase = nlm_get_pcie_base(n, link);
  217. if (nlm_read_pci_reg(pciebase, 0) == 0xffffffff)
  218. continue;
  219. xlp_config_pci_bswap(n, link);
  220. /* put in intpin and irq - u-boot does not */
  221. reg = nlm_read_pci_reg(pciebase, 0xf);
  222. reg &= ~0x1fu;
  223. reg |= (1 << 8) | nlm_pci_link_to_irq(link);
  224. nlm_write_pci_reg(pciebase, 0xf, reg);
  225. pr_info("XLP PCIe: Link %d-%d initialized.\n", n, link);
  226. }
  227. }
  228. set_io_port_base(CKSEG1);
  229. nlm_pci_controller.io_map_base = CKSEG1;
  230. register_pci_controller(&nlm_pci_controller);
  231. pr_info("XLP PCIe Controller %pR%pR.\n", &nlm_pci_io_resource,
  232. &nlm_pci_mem_resource);
  233. return 0;
  234. }
  235. arch_initcall(pcibios_init);