pci-xlr.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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/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 <linux/pci_regs.h>
  44. #include <asm/io.h>
  45. #include <asm/netlogic/interrupt.h>
  46. #include <asm/netlogic/haldefs.h>
  47. #include <asm/netlogic/common.h>
  48. #include <asm/netlogic/xlr/msidef.h>
  49. #include <asm/netlogic/xlr/iomap.h>
  50. #include <asm/netlogic/xlr/pic.h>
  51. #include <asm/netlogic/xlr/xlr.h>
  52. static void *pci_config_base;
  53. #define pci_cfg_addr(bus, devfn, off) (((bus) << 16) | ((devfn) << 8) | (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. cfgaddr = (u32 *)(pci_config_base +
  61. pci_cfg_addr(bus->number, devfn, where & ~3));
  62. data = *cfgaddr;
  63. return cpu_to_le32(data);
  64. }
  65. static inline void pci_cfg_write_32bit(struct pci_bus *bus, unsigned int devfn,
  66. int where, u32 data)
  67. {
  68. u32 *cfgaddr;
  69. cfgaddr = (u32 *)(pci_config_base +
  70. pci_cfg_addr(bus->number, devfn, where & ~3));
  71. *cfgaddr = cpu_to_le32(data);
  72. }
  73. static int nlm_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  74. int where, int size, u32 *val)
  75. {
  76. u32 data;
  77. if ((size == 2) && (where & 1))
  78. return PCIBIOS_BAD_REGISTER_NUMBER;
  79. else if ((size == 4) && (where & 3))
  80. return PCIBIOS_BAD_REGISTER_NUMBER;
  81. data = pci_cfg_read_32bit(bus, devfn, where);
  82. if (size == 1)
  83. *val = (data >> ((where & 3) << 3)) & 0xff;
  84. else if (size == 2)
  85. *val = (data >> ((where & 3) << 3)) & 0xffff;
  86. else
  87. *val = data;
  88. return PCIBIOS_SUCCESSFUL;
  89. }
  90. static int nlm_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  91. int where, int size, u32 val)
  92. {
  93. u32 data;
  94. if ((size == 2) && (where & 1))
  95. return PCIBIOS_BAD_REGISTER_NUMBER;
  96. else if ((size == 4) && (where & 3))
  97. return PCIBIOS_BAD_REGISTER_NUMBER;
  98. data = pci_cfg_read_32bit(bus, devfn, where);
  99. if (size == 1)
  100. data = (data & ~(0xff << ((where & 3) << 3))) |
  101. (val << ((where & 3) << 3));
  102. else if (size == 2)
  103. data = (data & ~(0xffff << ((where & 3) << 3))) |
  104. (val << ((where & 3) << 3));
  105. else
  106. data = val;
  107. pci_cfg_write_32bit(bus, devfn, where, data);
  108. return PCIBIOS_SUCCESSFUL;
  109. }
  110. struct pci_ops nlm_pci_ops = {
  111. .read = nlm_pcibios_read,
  112. .write = nlm_pcibios_write
  113. };
  114. static struct resource nlm_pci_mem_resource = {
  115. .name = "XLR PCI MEM",
  116. .start = 0xd0000000UL, /* 256MB PCI mem @ 0xd000_0000 */
  117. .end = 0xdfffffffUL,
  118. .flags = IORESOURCE_MEM,
  119. };
  120. static struct resource nlm_pci_io_resource = {
  121. .name = "XLR IO MEM",
  122. .start = 0x10000000UL, /* 16MB PCI IO @ 0x1000_0000 */
  123. .end = 0x100fffffUL,
  124. .flags = IORESOURCE_IO,
  125. };
  126. struct pci_controller nlm_pci_controller = {
  127. .index = 0,
  128. .pci_ops = &nlm_pci_ops,
  129. .mem_resource = &nlm_pci_mem_resource,
  130. .mem_offset = 0x00000000UL,
  131. .io_resource = &nlm_pci_io_resource,
  132. .io_offset = 0x00000000UL,
  133. };
  134. /*
  135. * The top level PCIe links on the XLS PCIe controller appear as
  136. * bridges. Given a device, this function finds which link it is
  137. * on.
  138. */
  139. static struct pci_dev *xls_get_pcie_link(const struct pci_dev *dev)
  140. {
  141. struct pci_bus *bus, *p;
  142. /* Find the bridge on bus 0 */
  143. bus = dev->bus;
  144. for (p = bus->parent; p && p->number != 0; p = p->parent)
  145. bus = p;
  146. return p ? bus->self : NULL;
  147. }
  148. static int nlm_pci_link_to_irq(int link)
  149. {
  150. switch (link) {
  151. case 0:
  152. return PIC_PCIE_LINK0_IRQ;
  153. case 1:
  154. return PIC_PCIE_LINK1_IRQ;
  155. case 2:
  156. if (nlm_chip_is_xls_b())
  157. return PIC_PCIE_XLSB0_LINK2_IRQ;
  158. else
  159. return PIC_PCIE_LINK2_IRQ;
  160. case 3:
  161. if (nlm_chip_is_xls_b())
  162. return PIC_PCIE_XLSB0_LINK3_IRQ;
  163. else
  164. return PIC_PCIE_LINK3_IRQ;
  165. }
  166. WARN(1, "Unexpected link %d\n", link);
  167. return 0;
  168. }
  169. static int get_irq_vector(const struct pci_dev *dev)
  170. {
  171. struct pci_dev *lnk;
  172. int link;
  173. if (!nlm_chip_is_xls())
  174. return PIC_PCIX_IRQ; /* for XLR just one IRQ */
  175. lnk = xls_get_pcie_link(dev);
  176. if (lnk == NULL)
  177. return 0;
  178. link = PCI_SLOT(lnk->devfn);
  179. return nlm_pci_link_to_irq(link);
  180. }
  181. #ifdef CONFIG_PCI_MSI
  182. void arch_teardown_msi_irq(unsigned int irq)
  183. {
  184. }
  185. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  186. {
  187. struct msi_msg msg;
  188. struct pci_dev *lnk;
  189. int irq, ret;
  190. u16 val;
  191. /* MSI not supported on XLR */
  192. if (!nlm_chip_is_xls())
  193. return 1;
  194. /*
  195. * Enable MSI on the XLS PCIe controller bridge which was disabled
  196. * at enumeration, the bridge MSI capability is at 0x50
  197. */
  198. lnk = xls_get_pcie_link(dev);
  199. if (lnk == NULL)
  200. return 1;
  201. pci_read_config_word(lnk, 0x50 + PCI_MSI_FLAGS, &val);
  202. if ((val & PCI_MSI_FLAGS_ENABLE) == 0) {
  203. val |= PCI_MSI_FLAGS_ENABLE;
  204. pci_write_config_word(lnk, 0x50 + PCI_MSI_FLAGS, val);
  205. }
  206. irq = get_irq_vector(dev);
  207. if (irq <= 0)
  208. return 1;
  209. msg.address_hi = MSI_ADDR_BASE_HI;
  210. msg.address_lo = MSI_ADDR_BASE_LO |
  211. MSI_ADDR_DEST_MODE_PHYSICAL |
  212. MSI_ADDR_REDIRECTION_CPU;
  213. msg.data = MSI_DATA_TRIGGER_EDGE |
  214. MSI_DATA_LEVEL_ASSERT |
  215. MSI_DATA_DELIVERY_FIXED;
  216. ret = irq_set_msi_desc(irq, desc);
  217. if (ret < 0)
  218. return ret;
  219. pci_write_msi_msg(irq, &msg);
  220. return 0;
  221. }
  222. #endif
  223. /* Extra ACK needed for XLR on chip PCI controller */
  224. static void xlr_pci_ack(struct irq_data *d)
  225. {
  226. uint64_t pcibase = nlm_mmio_base(NETLOGIC_IO_PCIX_OFFSET);
  227. nlm_read_reg(pcibase, (0x140 >> 2));
  228. }
  229. /* Extra ACK needed for XLS on chip PCIe controller */
  230. static void xls_pcie_ack(struct irq_data *d)
  231. {
  232. uint64_t pciebase_le = nlm_mmio_base(NETLOGIC_IO_PCIE_1_OFFSET);
  233. switch (d->irq) {
  234. case PIC_PCIE_LINK0_IRQ:
  235. nlm_write_reg(pciebase_le, (0x90 >> 2), 0xffffffff);
  236. break;
  237. case PIC_PCIE_LINK1_IRQ:
  238. nlm_write_reg(pciebase_le, (0x94 >> 2), 0xffffffff);
  239. break;
  240. case PIC_PCIE_LINK2_IRQ:
  241. nlm_write_reg(pciebase_le, (0x190 >> 2), 0xffffffff);
  242. break;
  243. case PIC_PCIE_LINK3_IRQ:
  244. nlm_write_reg(pciebase_le, (0x194 >> 2), 0xffffffff);
  245. break;
  246. }
  247. }
  248. /* For XLS B silicon, the 3,4 PCI interrupts are different */
  249. static void xls_pcie_ack_b(struct irq_data *d)
  250. {
  251. uint64_t pciebase_le = nlm_mmio_base(NETLOGIC_IO_PCIE_1_OFFSET);
  252. switch (d->irq) {
  253. case PIC_PCIE_LINK0_IRQ:
  254. nlm_write_reg(pciebase_le, (0x90 >> 2), 0xffffffff);
  255. break;
  256. case PIC_PCIE_LINK1_IRQ:
  257. nlm_write_reg(pciebase_le, (0x94 >> 2), 0xffffffff);
  258. break;
  259. case PIC_PCIE_XLSB0_LINK2_IRQ:
  260. nlm_write_reg(pciebase_le, (0x190 >> 2), 0xffffffff);
  261. break;
  262. case PIC_PCIE_XLSB0_LINK3_IRQ:
  263. nlm_write_reg(pciebase_le, (0x194 >> 2), 0xffffffff);
  264. break;
  265. }
  266. }
  267. int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  268. {
  269. return get_irq_vector(dev);
  270. }
  271. /* Do platform specific device initialization at pci_enable_device() time */
  272. int pcibios_plat_dev_init(struct pci_dev *dev)
  273. {
  274. return 0;
  275. }
  276. static int __init pcibios_init(void)
  277. {
  278. void (*extra_ack)(struct irq_data *);
  279. int link, irq;
  280. /* PSB assigns PCI resources */
  281. pci_set_flags(PCI_PROBE_ONLY);
  282. pci_config_base = ioremap(DEFAULT_PCI_CONFIG_BASE, 16 << 20);
  283. /* Extend IO port for memory mapped io */
  284. ioport_resource.start = 0;
  285. ioport_resource.end = ~0;
  286. set_io_port_base(CKSEG1);
  287. nlm_pci_controller.io_map_base = CKSEG1;
  288. pr_info("Registering XLR/XLS PCIX/PCIE Controller.\n");
  289. register_pci_controller(&nlm_pci_controller);
  290. /*
  291. * For PCI interrupts, we need to ack the PCI controller too, overload
  292. * irq handler data to do this
  293. */
  294. if (!nlm_chip_is_xls()) {
  295. /* XLR PCI controller ACK */
  296. nlm_set_pic_extra_ack(0, PIC_PCIX_IRQ, xlr_pci_ack);
  297. } else {
  298. if (nlm_chip_is_xls_b())
  299. extra_ack = xls_pcie_ack_b;
  300. else
  301. extra_ack = xls_pcie_ack;
  302. for (link = 0; link < 4; link++) {
  303. irq = nlm_pci_link_to_irq(link);
  304. nlm_set_pic_extra_ack(0, irq, extra_ack);
  305. }
  306. }
  307. return 0;
  308. }
  309. arch_initcall(pcibios_init);