summaryrefslogtreecommitdiffstats
path: root/sys/sparc64/pci/ofw_pci.c
blob: 636c9f98cbc367cb97ee94f9cd29672cee9aa186 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (c) 1999, 2000 Matthew R. Green
 * All rights reserved.
 * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	from: NetBSD: psycho.c,v 1.35 2001/09/10 16:17:06 eeh Exp
 *
 * $FreeBSD$
 */

#include "opt_ofw_pci.h"

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/bus.h>

#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>

#include <dev/ofw/ofw_pci.h>
#include <dev/ofw/openfirm.h>

#include <sparc64/pci/ofw_pci.h>

#include <machine/ofw_bus.h>

#include "pcib_if.h"

u_int32_t
ofw_pci_route_intr(phandle_t node)
{
	u_int32_t rv;

	rv = ofw_bus_route_intr(node, ORIP_NOINT);
	if (rv == ORIR_NOTFOUND)
		return (255);
	return (rv);
}

#define	OFW_PCI_PCIBUS	"pci"
/*
 * Walk the PCI bus hierarchy, starting with the root PCI bus and descending
 * through bridges, and initialize the interrupt line configuration registers
 * of attached devices using firmware information.
 */
void
ofw_pci_init_intr(device_t dev, phandle_t bus)
{
	struct ofw_pci_register pcir;
	phandle_t node;
	char type[32];
	int intr;
	int freemap;

	if ((node = OF_child(bus)) == 0)
		return;
	freemap = 0;
	do {
		if (node == -1)
			panic("ofw_pci_init_intr: OF_child failed");
		if (OF_getprop(node, "device_type", type, sizeof(type)) == -1)
			type[0] = '\0';
		else
			type[sizeof(type) - 1] = '\0';
		if (strcmp(type, OFW_PCI_PCIBUS) == 0) {
			/*
			 * This is a pci-pci bridge, recurse to initialize the
			 * child bus. The hierarchy is usually at most 2 levels
			 * deep, so recursion is feasible.
			 */
#ifdef OFW_PCI_DEBUG
			device_printf(dev, "%s: descending to "
			    "subordinate PCI bus\n", __func__);
#endif
			ofw_pci_init_intr(dev, node);
		} else {
			if (OF_getprop(node, "reg", &pcir, sizeof(pcir)) == -1)
				panic("ofw_pci_route_intr: OF_getprop failed");

			if ((intr = ofw_pci_route_intr(node)) != 255) {
#ifdef OFW_PCI_DEBUG
				device_printf(dev, "%s: mapping intr for "
				    "%d/%d/%d to %d (preset was %d)\n",
				    __func__,
				    OFW_PCI_PHYS_HI_BUS(pcir.phys_hi),
				    OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi),
				    OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi),
				    intr,
				    (int)PCIB_READ_CONFIG(dev,
					OFW_PCI_PHYS_HI_BUS(pcir.phys_hi),
					OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi),
					OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi),
					PCIR_INTLINE, 1));
#endif /* OFW_PCI_DEBUG */
				PCIB_WRITE_CONFIG(dev,
				    OFW_PCI_PHYS_HI_BUS(pcir.phys_hi),
				    OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi),
				    OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi),
				    PCIR_INTLINE, intr, 1);
			} else {
#ifdef OFW_PCI_DEBUG
				device_printf(dev, "%s: no interrupt "
				    "mapping found for %d/%d/%d (preset %d)\n",
				    __func__,
				    OFW_PCI_PHYS_HI_BUS(pcir.phys_hi),
				    OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi),
				    OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi),
				    (int)PCIB_READ_CONFIG(dev,
					OFW_PCI_PHYS_HI_BUS(pcir.phys_hi),
					OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi),
					OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi),
					PCIR_INTLINE, 1));
#endif /* OFW_PCI_DEBUG */
				/* The firmware initializes to 0 instead 255 */
				PCIB_WRITE_CONFIG(dev,
				    OFW_PCI_PHYS_HI_BUS(pcir.phys_hi),
				    OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi),
				    OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi),
				    PCIR_INTLINE, 255, 1);
			}
		}
	} while ((node = OF_peer(node)) != 0);
}

phandle_t
ofw_pci_find_node(int bus, int slot, int func)
{
	phandle_t node, bnode, parent;
	struct ofw_pci_register pcir;
	int br[2];
	char name[16];

	/* 1. Try to find the bus in question. */
	bnode = 0;
	name[sizeof(name) - 1] = '\0';
	parent = OF_peer(0);
	node = OF_child(parent);
	while (node != 0 && node != -1) {
		if (OF_getprop(node, "name", name, sizeof(name) - 1) != -1 &&
		    strcmp(name, "pci") == 0 &&
		    OF_getprop(node, "bus-range", br, sizeof(br)) != -1) {
			/* Found the bus? */
			if (bus == br[0]) {
				bnode = node;
				break;
			}
			/* Need to descend? */
			if (bus > br[0] && bus <= br[1]) {
				parent = node;
				node = OF_child(node);
				continue;
			}
		}
		node = OF_peer(node);
	}
	if (bnode == 0)
		return (0);
	for (node = OF_child(bnode); node != 0 && node != -1;
	     node = OF_peer(node)) {
		if (OF_getprop(node, "reg", &pcir, sizeof(pcir)) == -1)
			continue;
		if (OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi) == slot &&
		    OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi) == func) {
			if (OFW_PCI_PHYS_HI_BUS(pcir.phys_hi) != bus)
				panic("ofw_pci_find_node: bus number mismatch");
			return (node);
		}
	}
	return (0);
}

phandle_t
ofw_pci_node(device_t dev)
{

	return (ofw_pci_find_node(pci_get_bus(dev), pci_get_slot(dev),
	    pci_get_function(dev)));
}
OpenPOWER on IntegriCloud