xref: /OK3568_Linux_fs/kernel/arch/microblaze/pci/xilinx_pci.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * PCI support for Xilinx plbv46_pci soft-core which can be used on
3*4882a593Smuzhiyun  * Xilinx Virtex ML410 / ML510 boards.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2009 Roderick Colenbrander
6*4882a593Smuzhiyun  * Copyright 2009 Secret Lab Technologies Ltd.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * The pci bridge fixup code was copied from ppc4xx_pci.c and was written
9*4882a593Smuzhiyun  * by Benjamin Herrenschmidt.
10*4882a593Smuzhiyun  * Copyright 2007 Ben. Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * This file is licensed under the terms of the GNU General Public License
13*4882a593Smuzhiyun  * version 2. This program is licensed "as is" without any warranty of any
14*4882a593Smuzhiyun  * kind, whether express or implied.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <linux/ioport.h>
18*4882a593Smuzhiyun #include <linux/of.h>
19*4882a593Smuzhiyun #include <linux/of_address.h>
20*4882a593Smuzhiyun #include <linux/pci.h>
21*4882a593Smuzhiyun #include <linux/io.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #define XPLB_PCI_ADDR 0x10c
24*4882a593Smuzhiyun #define XPLB_PCI_DATA 0x110
25*4882a593Smuzhiyun #define XPLB_PCI_BUS  0x114
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #define PCI_HOST_ENABLE_CMD (PCI_COMMAND_SERR | PCI_COMMAND_PARITY | \
28*4882a593Smuzhiyun 				PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY)
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun static struct of_device_id xilinx_pci_match[] = {
31*4882a593Smuzhiyun 	{ .compatible = "xlnx,plbv46-pci-1.03.a", },
32*4882a593Smuzhiyun 	{}
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /**
36*4882a593Smuzhiyun  * xilinx_pci_fixup_bridge - Block Xilinx PHB configuration.
37*4882a593Smuzhiyun  */
xilinx_pci_fixup_bridge(struct pci_dev * dev)38*4882a593Smuzhiyun static void xilinx_pci_fixup_bridge(struct pci_dev *dev)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	struct pci_controller *hose;
41*4882a593Smuzhiyun 	int i;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	if (dev->devfn || dev->bus->self)
44*4882a593Smuzhiyun 		return;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	hose = pci_bus_to_host(dev->bus);
47*4882a593Smuzhiyun 	if (!hose)
48*4882a593Smuzhiyun 		return;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	if (!of_match_node(xilinx_pci_match, hose->dn))
51*4882a593Smuzhiyun 		return;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	/* Hide the PCI host BARs from the kernel as their content doesn't
54*4882a593Smuzhiyun 	 * fit well in the resource management
55*4882a593Smuzhiyun 	 */
56*4882a593Smuzhiyun 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
57*4882a593Smuzhiyun 		dev->resource[i].start = 0;
58*4882a593Smuzhiyun 		dev->resource[i].end = 0;
59*4882a593Smuzhiyun 		dev->resource[i].flags = 0;
60*4882a593Smuzhiyun 	}
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	dev_info(&dev->dev, "Hiding Xilinx plb-pci host bridge resources %s\n",
63*4882a593Smuzhiyun 		 pci_name(dev));
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, xilinx_pci_fixup_bridge);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun #ifdef DEBUG
68*4882a593Smuzhiyun /**
69*4882a593Smuzhiyun  * xilinx_pci_exclude_device - Don't do config access for non-root bus
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * This is a hack.  Config access to any bus other than bus 0 does not
72*4882a593Smuzhiyun  * currently work on the ML510 so we prevent it here.
73*4882a593Smuzhiyun  */
74*4882a593Smuzhiyun static int
xilinx_pci_exclude_device(struct pci_controller * hose,u_char bus,u8 devfn)75*4882a593Smuzhiyun xilinx_pci_exclude_device(struct pci_controller *hose, u_char bus, u8 devfn)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	return (bus != 0);
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun /**
81*4882a593Smuzhiyun  * xilinx_early_pci_scan - List pci config space for available devices
82*4882a593Smuzhiyun  *
83*4882a593Smuzhiyun  * List pci devices in very early phase.
84*4882a593Smuzhiyun  */
xilinx_early_pci_scan(struct pci_controller * hose)85*4882a593Smuzhiyun static void __init xilinx_early_pci_scan(struct pci_controller *hose)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	u32 bus = 0;
88*4882a593Smuzhiyun 	u32 val, dev, func, offset;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	/* Currently we have only 2 device connected - up-to 32 devices */
91*4882a593Smuzhiyun 	for (dev = 0; dev < 2; dev++) {
92*4882a593Smuzhiyun 		/* List only first function number - up-to 8 functions */
93*4882a593Smuzhiyun 		for (func = 0; func < 1; func++) {
94*4882a593Smuzhiyun 			pr_info("%02x:%02x:%02x", bus, dev, func);
95*4882a593Smuzhiyun 			/* read the first 64 standardized bytes */
96*4882a593Smuzhiyun 			/* Up-to 192 bytes can be list of capabilities */
97*4882a593Smuzhiyun 			for (offset = 0; offset < 64; offset += 4) {
98*4882a593Smuzhiyun 				early_read_config_dword(hose, bus,
99*4882a593Smuzhiyun 					PCI_DEVFN(dev, func), offset, &val);
100*4882a593Smuzhiyun 				if (offset == 0 && val == 0xFFFFFFFF) {
101*4882a593Smuzhiyun 					pr_cont("\nABSENT");
102*4882a593Smuzhiyun 					break;
103*4882a593Smuzhiyun 				}
104*4882a593Smuzhiyun 				if (!(offset % 0x10))
105*4882a593Smuzhiyun 					pr_cont("\n%04x:    ", offset);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 				pr_cont("%08x  ", val);
108*4882a593Smuzhiyun 			}
109*4882a593Smuzhiyun 			pr_info("\n");
110*4882a593Smuzhiyun 		}
111*4882a593Smuzhiyun 	}
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun #else
xilinx_early_pci_scan(struct pci_controller * hose)114*4882a593Smuzhiyun static void __init xilinx_early_pci_scan(struct pci_controller *hose)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun #endif
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /**
120*4882a593Smuzhiyun  * xilinx_pci_init - Find and register a Xilinx PCI host bridge
121*4882a593Smuzhiyun  */
xilinx_pci_init(void)122*4882a593Smuzhiyun void __init xilinx_pci_init(void)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun 	struct pci_controller *hose;
125*4882a593Smuzhiyun 	struct resource r;
126*4882a593Smuzhiyun 	void __iomem *pci_reg;
127*4882a593Smuzhiyun 	struct device_node *pci_node;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	pci_node = of_find_matching_node(NULL, xilinx_pci_match);
130*4882a593Smuzhiyun 	if (!pci_node)
131*4882a593Smuzhiyun 		return;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	if (of_address_to_resource(pci_node, 0, &r)) {
134*4882a593Smuzhiyun 		pr_err("xilinx-pci: cannot resolve base address\n");
135*4882a593Smuzhiyun 		return;
136*4882a593Smuzhiyun 	}
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	hose = pcibios_alloc_controller(pci_node);
139*4882a593Smuzhiyun 	if (!hose) {
140*4882a593Smuzhiyun 		pr_err("xilinx-pci: pcibios_alloc_controller() failed\n");
141*4882a593Smuzhiyun 		return;
142*4882a593Smuzhiyun 	}
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	/* Setup config space */
145*4882a593Smuzhiyun 	setup_indirect_pci(hose, r.start + XPLB_PCI_ADDR,
146*4882a593Smuzhiyun 			   r.start + XPLB_PCI_DATA,
147*4882a593Smuzhiyun 			   INDIRECT_TYPE_SET_CFG_TYPE);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/* According to the xilinx plbv46_pci documentation the soft-core starts
150*4882a593Smuzhiyun 	 * a self-init when the bus master enable bit is set. Without this bit
151*4882a593Smuzhiyun 	 * set the pci bus can't be scanned.
152*4882a593Smuzhiyun 	 */
153*4882a593Smuzhiyun 	early_write_config_word(hose, 0, 0, PCI_COMMAND, PCI_HOST_ENABLE_CMD);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	/* Set the max latency timer to 255 */
156*4882a593Smuzhiyun 	early_write_config_byte(hose, 0, 0, PCI_LATENCY_TIMER, 0xff);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	/* Set the max bus number to 255, and bus/subbus no's to 0 */
159*4882a593Smuzhiyun 	pci_reg = of_iomap(pci_node, 0);
160*4882a593Smuzhiyun 	WARN_ON(!pci_reg);
161*4882a593Smuzhiyun 	out_be32(pci_reg + XPLB_PCI_BUS, 0x000000ff);
162*4882a593Smuzhiyun 	iounmap(pci_reg);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	/* Register the host bridge with the linux kernel! */
165*4882a593Smuzhiyun 	pci_process_bridge_OF_ranges(hose, pci_node,
166*4882a593Smuzhiyun 					INDIRECT_TYPE_SET_CFG_TYPE);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	pr_info("xilinx-pci: Registered PCI host bridge\n");
169*4882a593Smuzhiyun 	xilinx_early_pci_scan(hose);
170*4882a593Smuzhiyun }
171