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
|
/*-
* Copyright (c) 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.
*
* 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 OR CONTRIBUTORS 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.
*
* $FreeBSD$
*/
/*
* Some Open Firmware helper functions that are likely machine dependent.
*/
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/systm.h>
#include <net/ethernet.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/openfirm.h>
#include <machine/bus.h>
#include <machine/idprom.h>
#include <machine/ofw_machdep.h>
#include <machine/ofw_upa.h>
#include <machine/resource.h>
#include <sparc64/pci/ofw_pci.h>
#include <sparc64/isa/ofw_isa.h>
#include <sparc64/sbus/ofw_sbus.h>
void
OF_getetheraddr(device_t dev, u_char *addr)
{
char buf[sizeof("true")];
phandle_t node;
struct idprom idp;
if ((node = OF_finddevice("/options")) > 0 &&
OF_getprop(node, "local-mac-address?", buf, sizeof(buf)) > 0) {
buf[sizeof(buf) - 1] = '\0';
if (strcmp(buf, "true") == 0 &&
(node = ofw_bus_get_node(dev)) > 0 &&
OF_getprop(node, "local-mac-address", addr,
ETHER_ADDR_LEN) == ETHER_ADDR_LEN)
return;
}
node = OF_peer(0);
if (node <= 0 || OF_getprop(node, "idprom", &idp, sizeof(idp)) == -1)
panic("Could not determine the machine ethernet address");
bcopy(&idp.id_ether, addr, ETHER_ADDR_LEN);
}
int
OF_decode_addr(phandle_t node, int *space, bus_addr_t *addr)
{
char name[32];
union {
struct isa_ranges isa[4];
struct sbus_ranges sbus[8];
struct upa_ranges upa[4];
} range;
union {
struct isa_regs isa;
struct sbus_regs sbus;
} reg;
phandle_t bus, pbus;
u_long child, dummy, phys;
int cs, i, rsz, type;
bus = OF_parent(node);
if (bus == 0)
return (ENXIO);
if (OF_getprop(bus, "name", name, sizeof(name)) == -1)
return (ENXIO);
name[sizeof(name) - 1] = '\0';
if (strcmp(name, "ebus") == 0 || strcmp(name, "isa") == 0) {
if (OF_getprop(node, "reg", ®.isa, sizeof(reg.isa)) == -1)
return (ENXIO);
rsz = OF_getprop(bus, "ranges", range.isa, sizeof(range.isa));
if (rsz == -1)
return (ENXIO);
phys = ISA_REG_PHYS(®.isa);
dummy = phys + 1;
type = ofw_isa_range_map(range.isa, rsz / sizeof(*range.isa),
&phys, &dummy, NULL);
if (type == SYS_RES_MEMORY) {
cs = PCI_CS_MEM32;
*space = PCI_MEMORY_BUS_SPACE;
} else {
cs = PCI_CS_IO;
*space = PCI_IO_BUS_SPACE;
}
/* Find the topmost PCI node (the host bridge) */
while (1) {
pbus = OF_parent(bus);
if (pbus == 0)
return (ENXIO);
if (OF_getprop(pbus, "name", name, sizeof(name)) == -1)
return (ENXIO);
name[sizeof(name) - 1] = '\0';
if (strcmp(name, "pci") != 0)
break;
bus = pbus;
}
/* There wasn't a PCI bridge. */
if (bus == OF_parent(node))
return (ENXIO);
/* Make sure we reached the UPA/PCI node. */
if (OF_getprop(pbus, "device_type", name, sizeof(name)) == -1)
return (ENXIO);
name[sizeof(name) - 1] = '\0';
if (strcmp(name, "upa") != 0)
return (ENXIO);
rsz = OF_getprop(bus, "ranges", range.upa, sizeof(range.upa));
if (rsz == -1)
return (ENXIO);
for (i = 0; i < (rsz / sizeof(range.upa[0])); i++) {
child = UPA_RANGE_CHILD(&range.upa[i]);
if (UPA_RANGE_CS(&range.upa[i]) == cs &&
phys >= child &&
phys - child < UPA_RANGE_SIZE(&range.upa[i])) {
*addr = UPA_RANGE_PHYS(&range.upa[i]) + phys;
return (0);
}
}
} else if (strcmp(name, "sbus") == 0) {
if (OF_getprop(node, "reg", ®.sbus, sizeof(reg.sbus)) == -1)
return (ENXIO);
rsz = OF_getprop(bus, "ranges", range.sbus,
sizeof(range.sbus));
if (rsz == -1)
return (ENXIO);
for (i = 0; i < (rsz / sizeof(range.sbus[0])); i++) {
if (reg.sbus.sbr_slot != range.sbus[i].cspace)
continue;
if (reg.sbus.sbr_offset < range.sbus[i].coffset ||
reg.sbus.sbr_offset >= range.sbus[i].coffset +
range.sbus[i].size)
continue;
/* Found it... */
phys = range.sbus[i].poffset |
((bus_addr_t)range.sbus[i].pspace << 32);
phys += reg.sbus.sbr_offset - range.sbus[i].coffset;
*addr = phys;
*space = SBUS_BUS_SPACE;
return (0);
}
}
return (ENXIO);
}
|