pci-xlp.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/xlp-hal/iomap.h>
  47. #include <asm/netlogic/xlp-hal/pic.h>
  48. #include <asm/netlogic/xlp-hal/xlp.h>
  49. #include <asm/netlogic/xlp-hal/pcibus.h>
  50. #include <asm/netlogic/xlp-hal/bridge.h>
  51. static void *pci_config_base;
  52. #define pci_cfg_addr(bus, devfn, off) (((bus) << 20) | ((devfn) << 12) | (off))
  53. /* PCI ops */
  54. static inline u32 pci_cfg_read_32bit(struct pci_bus *bus, unsigned int devfn,
  55. int where)
  56. {
  57. u32 data;
  58. u32 *cfgaddr;
  59. cfgaddr = (u32 *)(pci_config_base +
  60. pci_cfg_addr(bus->number, devfn, where & ~3));
  61. data = *cfgaddr;
  62. return data;
  63. }
  64. static inline void pci_cfg_write_32bit(struct pci_bus *bus, unsigned int devfn,
  65. int where, u32 data)
  66. {
  67. u32 *cfgaddr;
  68. cfgaddr = (u32 *)(pci_config_base +
  69. pci_cfg_addr(bus->number, devfn, where & ~3));
  70. *cfgaddr = data;
  71. }
  72. static int nlm_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  73. int where, int size, u32 *val)
  74. {
  75. u32 data;
  76. if ((size == 2) && (where & 1))
  77. return PCIBIOS_BAD_REGISTER_NUMBER;
  78. else if ((size == 4) && (where & 3))
  79. return PCIBIOS_BAD_REGISTER_NUMBER;
  80. data = pci_cfg_read_32bit(bus, devfn, where);
  81. if (size == 1)
  82. *val = (data >> ((where & 3) << 3)) & 0xff;
  83. else if (size == 2)
  84. *val = (data >> ((where & 3) << 3)) & 0xffff;
  85. else
  86. *val = data;
  87. return PCIBIOS_SUCCESSFUL;
  88. }
  89. static int nlm_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  90. int where, int size, u32 val)
  91. {
  92. u32 data;
  93. if ((size == 2) && (where & 1))
  94. return PCIBIOS_BAD_REGISTER_NUMBER;
  95. else if ((size == 4) && (where & 3))
  96. return PCIBIOS_BAD_REGISTER_NUMBER;
  97. data = pci_cfg_read_32bit(bus, devfn, where);
  98. if (size == 1)
  99. data = (data & ~(0xff << ((where & 3) << 3))) |
  100. (val << ((where & 3) << 3));
  101. else if (size == 2)
  102. data = (data & ~(0xffff << ((where & 3) << 3))) |
  103. (val << ((where & 3) << 3));
  104. else
  105. data = val;
  106. pci_cfg_write_32bit(bus, devfn, where, data);
  107. return PCIBIOS_SUCCESSFUL;
  108. }
  109. struct pci_ops nlm_pci_ops = {
  110. .read = nlm_pcibios_read,
  111. .write = nlm_pcibios_write
  112. };
  113. static struct resource nlm_pci_mem_resource = {
  114. .name = "XLP PCI MEM",
  115. .start = 0xd0000000UL, /* 256MB PCI mem @ 0xd000_0000 */
  116. .end = 0xdfffffffUL,
  117. .flags = IORESOURCE_MEM,
  118. };
  119. static struct resource nlm_pci_io_resource = {
  120. .name = "XLP IO MEM",
  121. .start = 0x14000000UL, /* 64MB PCI IO @ 0x1000_0000 */
  122. .end = 0x17ffffffUL,
  123. .flags = IORESOURCE_IO,
  124. };
  125. struct pci_controller nlm_pci_controller = {
  126. .index = 0,
  127. .pci_ops = &nlm_pci_ops,
  128. .mem_resource = &nlm_pci_mem_resource,
  129. .mem_offset = 0x00000000UL,
  130. .io_resource = &nlm_pci_io_resource,
  131. .io_offset = 0x00000000UL,
  132. };
  133. static int get_irq_vector(const struct pci_dev *dev)
  134. {
  135. /*
  136. * For XLP PCIe, there is an IRQ per Link, find out which
  137. * link the device is on to assign interrupts
  138. */
  139. if (dev->bus->self == NULL)
  140. return 0;
  141. switch (dev->bus->self->devfn) {
  142. case 0x8:
  143. return PIC_PCIE_LINK_0_IRQ;
  144. case 0x9:
  145. return PIC_PCIE_LINK_1_IRQ;
  146. case 0xa:
  147. return PIC_PCIE_LINK_2_IRQ;
  148. case 0xb:
  149. return PIC_PCIE_LINK_3_IRQ;
  150. }
  151. WARN(1, "Unexpected devfn %d\n", dev->bus->self->devfn);
  152. return 0;
  153. }
  154. int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  155. {
  156. return get_irq_vector(dev);
  157. }
  158. /* Do platform specific device initialization at pci_enable_device() time */
  159. int pcibios_plat_dev_init(struct pci_dev *dev)
  160. {
  161. return 0;
  162. }
  163. static int xlp_enable_pci_bswap(void)
  164. {
  165. uint64_t pciebase, sysbase;
  166. int node, i;
  167. u32 reg;
  168. /* Chip-0 so node set to 0 */
  169. node = 0;
  170. sysbase = nlm_get_bridge_regbase(node);
  171. /*
  172. * Enable byte swap in hardware. Program each link's PCIe SWAP regions
  173. * from the link's address ranges.
  174. */
  175. for (i = 0; i < 4; i++) {
  176. pciebase = nlm_pcicfg_base(XLP_IO_PCIE_OFFSET(node, i));
  177. if (nlm_read_pci_reg(pciebase, 0) == 0xffffffff)
  178. continue;
  179. reg = nlm_read_bridge_reg(sysbase, BRIDGE_PCIEMEM_BASE0 + i);
  180. nlm_write_pci_reg(pciebase, PCIE_BYTE_SWAP_MEM_BASE, reg);
  181. reg = nlm_read_bridge_reg(sysbase, BRIDGE_PCIEMEM_LIMIT0 + i);
  182. nlm_write_pci_reg(pciebase, PCIE_BYTE_SWAP_MEM_LIM,
  183. reg | 0xfff);
  184. reg = nlm_read_bridge_reg(sysbase, BRIDGE_PCIEIO_BASE0 + i);
  185. nlm_write_pci_reg(pciebase, PCIE_BYTE_SWAP_IO_BASE, reg);
  186. reg = nlm_read_bridge_reg(sysbase, BRIDGE_PCIEIO_LIMIT0 + i);
  187. nlm_write_pci_reg(pciebase, PCIE_BYTE_SWAP_IO_LIM, reg | 0xfff);
  188. }
  189. return 0;
  190. }
  191. static int __init pcibios_init(void)
  192. {
  193. /* Firmware assigns PCI resources */
  194. pci_set_flags(PCI_PROBE_ONLY);
  195. pci_config_base = ioremap(XLP_DEFAULT_PCI_ECFG_BASE, 64 << 20);
  196. /* Extend IO port for memory mapped io */
  197. ioport_resource.start = 0;
  198. ioport_resource.end = ~0;
  199. xlp_enable_pci_bswap();
  200. set_io_port_base(CKSEG1);
  201. nlm_pci_controller.io_map_base = CKSEG1;
  202. register_pci_controller(&nlm_pci_controller);
  203. pr_info("XLP PCIe Controller %pR%pR.\n", &nlm_pci_io_resource,
  204. &nlm_pci_mem_resource);
  205. return 0;
  206. }
  207. arch_initcall(pcibios_init);