]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sparc64/sparc64/ofw_machdep.c
- Remove the old sparc64 OFW PCI code (as opposed to the former
[FreeBSD/FreeBSD.git] / sys / sparc64 / sparc64 / ofw_machdep.c
1 /*-
2  * Copyright (c) 2001 by Thomas Moestl <tmm@FreeBSD.org>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27
28 /*
29  * Some OpenFirmware helper functions that are likely machine dependent.
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34
35 #include <net/ethernet.h>
36
37 #include <dev/ofw/openfirm.h>
38
39 #include <machine/bus.h>
40 #include <machine/idprom.h>
41 #include <machine/ofw_machdep.h>
42 #include <machine/ofw_upa.h>
43 #include <machine/resource.h>
44
45 #include <sparc64/pci/ofw_pci.h>
46 #include <sparc64/isa/ofw_isa.h>
47 #include <sparc64/sbus/ofw_sbus.h>
48
49 void
50 OF_getetheraddr(device_t dev, u_char *addr)
51 {
52         phandle_t node;
53         struct idprom idp;
54
55         node = OF_peer(0);
56         if (node <= 0 || OF_getprop(node, "idprom", &idp, sizeof(idp)) == -1)
57                 panic("Could not determine the machine ethernet address");
58         bcopy(&idp.id_ether, addr, ETHER_ADDR_LEN);
59 }
60
61 int
62 OF_getetheraddr2(device_t dev, u_char *addr)
63 {
64         phandle_t node;
65
66         node = ofw_pci_get_node(dev);
67         if (node <= 0)
68                return (-1);
69         return (OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN));
70 }
71
72 int
73 OF_decode_addr(phandle_t node, int *space, bus_addr_t *addr)
74 {
75         char name[32];
76         union {
77                 struct isa_ranges isa[4];
78                 struct sbus_ranges sbus[8];
79                 struct upa_ranges upa[4];
80         } range;
81         union {
82                 struct isa_regs isa;
83                 struct sbus_regs sbus;
84         } reg;
85         phandle_t bus, pbus;
86         u_long child, dummy, phys;
87         int cs, i, rsz, type;
88
89         bus = OF_parent(node);
90         if (bus == 0)
91                 return (ENXIO);
92         if (OF_getprop(bus, "name", name, sizeof(name)) == -1)
93                 return (ENXIO);
94         name[sizeof(name) - 1] = '\0';
95         if (strcmp(name, "ebus") == 0 || strcmp(name, "isa") == 0) {
96                 if (OF_getprop(node, "reg", &reg.isa, sizeof(reg.isa)) == -1)
97                         return (ENXIO);
98                 rsz = OF_getprop(bus, "ranges", range.isa, sizeof(range.isa));
99                 if (rsz == -1)
100                         return (ENXIO);
101                 phys = ISA_REG_PHYS(&reg.isa);
102                 dummy = phys + 1;
103                 type = ofw_isa_range_map(range.isa, rsz / sizeof(*range.isa),
104                     &phys, &dummy, NULL);
105                 if (type == SYS_RES_MEMORY) {
106                         cs = PCI_CS_MEM32;
107                         *space = PCI_MEMORY_BUS_SPACE;
108                 } else {
109                         cs = PCI_CS_IO;
110                         *space = PCI_IO_BUS_SPACE;
111                 }
112
113                 /* Find the topmost PCI node (the host bridge) */
114                 while (1) {
115                         pbus = OF_parent(bus);
116                         if (pbus == 0)
117                                 return (ENXIO);
118                         if (OF_getprop(pbus, "name", name, sizeof(name)) == -1)
119                                 return (ENXIO);
120                         name[sizeof(name) - 1] = '\0';
121                         if (strcmp(name, "pci") != 0)
122                                 break;
123                         bus = pbus;
124                 }
125
126                 /* There wasn't a PCI bridge. */
127                 if (bus == OF_parent(node))
128                         return (ENXIO);
129
130                 /* Make sure we reached the UPA/PCI node. */
131                 if (OF_getprop(pbus, "device_type", name, sizeof(name)) == -1)
132                         return (ENXIO);
133                 name[sizeof(name) - 1] = '\0';
134                 if (strcmp(name, "upa") != 0)
135                         return (ENXIO);
136
137                 rsz = OF_getprop(bus, "ranges", range.upa, sizeof(range.upa));
138                 if (rsz == -1)
139                         return (ENXIO);
140                 for (i = 0; i < (rsz / sizeof(range.upa[0])); i++) {
141                         child = UPA_RANGE_CHILD(&range.upa[i]);
142                         if (UPA_RANGE_CS(&range.upa[i]) == cs &&
143                             phys >= child &&
144                             phys - child < UPA_RANGE_SIZE(&range.upa[i])) {
145                                 *addr = UPA_RANGE_PHYS(&range.upa[i]) + phys;
146                                 return (0);
147                         }
148                 }
149         } else if (strcmp(name, "sbus") == 0) {
150                 if (OF_getprop(node, "reg", &reg.sbus, sizeof(reg.sbus)) == -1)
151                         return (ENXIO);
152                 rsz = OF_getprop(bus, "ranges", range.sbus,
153                     sizeof(range.sbus));
154                 if (rsz == -1)
155                         return (ENXIO);
156                 for (i = 0; i < (rsz / sizeof(range.sbus[0])); i++) {
157                         if (reg.sbus.sbr_slot != range.sbus[i].cspace)
158                                 continue;
159                         if (reg.sbus.sbr_offset < range.sbus[i].coffset ||
160                             reg.sbus.sbr_offset >= range.sbus[i].coffset +
161                             range.sbus[i].size)
162                                 continue;
163                         /* Found it... */
164                         phys = range.sbus[i].poffset |
165                             ((bus_addr_t)range.sbus[i].pspace << 32);
166                         phys += reg.sbus.sbr_offset - range.sbus[i].coffset;
167                         *addr = phys;
168                         *space = SBUS_BUS_SPACE;
169                         return (0);
170                 }
171         }
172         return (ENXIO);
173 }