]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sparc64/sparc64/ofw_machdep.c
This commit was generated by cvs2svn to compensate for changes in r127208,
[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 "opt_ofw_pci.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36
37 #include <net/ethernet.h>
38
39 #include <dev/ofw/openfirm.h>
40
41 #include <machine/bus.h>
42 #include <machine/idprom.h>
43 #include <machine/ofw_machdep.h>
44 #include <machine/ofw_upa.h>
45 #include <machine/resource.h>
46
47 #include <sparc64/pci/ofw_pci.h>
48 #include <sparc64/isa/ofw_isa.h>
49 #include <sparc64/sbus/ofw_sbus.h>
50
51 void
52 OF_getetheraddr(device_t dev, u_char *addr)
53 {
54         phandle_t node;
55         struct idprom idp;
56
57         node = OF_peer(0);
58         if (node <= 0 || OF_getprop(node, "idprom", &idp, sizeof(idp)) == -1)
59                 panic("Could not determine the machine ethernet address");
60         bcopy(&idp.id_ether, addr, ETHER_ADDR_LEN);
61 }
62
63 int
64 OF_getetheraddr2(device_t dev, u_char *addr)
65 {
66         phandle_t node;
67
68 #ifdef OFW_NEWPCI
69         node = ofw_pci_get_node(dev);
70 #else
71         node = ofw_pci_node(dev);
72 #endif
73         if (node <= 0)
74                return (-1);
75         return (OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN));
76 }
77
78 int
79 OF_decode_addr(phandle_t node, int *space, bus_addr_t *addr)
80 {
81         char name[32];
82         union {
83                 struct isa_ranges isa[4];
84                 struct sbus_ranges sbus[8];
85                 struct upa_ranges upa[4];
86         } range;
87         union {
88                 struct isa_regs isa;
89                 struct sbus_regs sbus;
90         } reg;
91         phandle_t bus, pbus;
92         u_long child, dummy, phys;
93         int cs, i, rsz, type;
94
95         bus = OF_parent(node);
96         if (bus == 0)
97                 return (ENXIO);
98         if (OF_getprop(bus, "name", name, sizeof(name)) == -1)
99                 return (ENXIO);
100         name[sizeof(name) - 1] = '\0';
101         if (strcmp(name, "ebus") == 0 || strcmp(name, "isa") == 0) {
102                 if (OF_getprop(node, "reg", &reg.isa, sizeof(reg.isa)) == -1)
103                         return (ENXIO);
104                 rsz = OF_getprop(bus, "ranges", range.isa, sizeof(range.isa));
105                 if (rsz == -1)
106                         return (ENXIO);
107                 phys = ISA_REG_PHYS(&reg.isa);
108                 dummy = phys + 1;
109                 type = ofw_isa_map_iorange(range.isa, rsz / sizeof(*range.isa),
110                     &phys, &dummy);
111                 if (type == SYS_RES_MEMORY) {
112                         cs = PCI_CS_MEM32;
113                         *space = PCI_MEMORY_BUS_SPACE;
114                 } else {
115                         cs = PCI_CS_IO;
116                         *space = PCI_IO_BUS_SPACE;
117                 }
118
119                 /* Find the topmost PCI node (the host bridge) */
120                 while (1) {
121                         pbus = OF_parent(bus);
122                         if (pbus == 0)
123                                 return (ENXIO);
124                         if (OF_getprop(pbus, "name", name, sizeof(name)) == -1)
125                                 return (ENXIO);
126                         name[sizeof(name) - 1] = '\0';
127                         if (strcmp(name, "pci") != 0)
128                                 break;
129                         bus = pbus;
130                 }
131
132                 /* There wasn't a PCI bridge. */
133                 if (bus == OF_parent(node))
134                         return (ENXIO);
135
136                 /* Make sure we reached the UPA/PCI node. */
137                 if (OF_getprop(pbus, "device_type", name, sizeof(name)) == -1)
138                         return (ENXIO);
139                 name[sizeof(name) - 1] = '\0';
140                 if (strcmp(name, "upa") != 0)
141                         return (ENXIO);
142
143                 rsz = OF_getprop(bus, "ranges", range.upa, sizeof(range.upa));
144                 if (rsz == -1)
145                         return (ENXIO);
146                 for (i = 0; i < (rsz / sizeof(range.upa[0])); i++) {
147                         child = UPA_RANGE_CHILD(&range.upa[i]);
148                         if (UPA_RANGE_CS(&range.upa[i]) == cs &&
149                             phys >= child &&
150                             phys - child < UPA_RANGE_SIZE(&range.upa[i])) {
151                                 *addr = UPA_RANGE_PHYS(&range.upa[i]) + phys;
152                                 return (0);
153                         }
154                 }
155         } else if (strcmp(name, "sbus") == 0) {
156                 if (OF_getprop(node, "reg", &reg.sbus, sizeof(reg.sbus)) == -1)
157                         return (ENXIO);
158                 rsz = OF_getprop(bus, "ranges", range.sbus,
159                     sizeof(range.sbus));
160                 if (rsz == -1)
161                         return (ENXIO);
162                 for (i = 0; i < (rsz / sizeof(range.sbus[0])); i++) {
163                         if (reg.sbus.sbr_slot != range.sbus[i].cspace)
164                                 continue;
165                         if (reg.sbus.sbr_offset < range.sbus[i].coffset ||
166                             reg.sbus.sbr_offset >= range.sbus[i].coffset +
167                             range.sbus[i].size)
168                                 continue;
169                         /* Found it... */
170                         phys = range.sbus[i].poffset |
171                             ((bus_addr_t)range.sbus[i].pspace << 32);
172                         phys += reg.sbus.sbr_offset - range.sbus[i].coffset;
173                         *addr = phys;
174                         *space = SBUS_BUS_SPACE;
175                         return (0);
176                 }
177         }
178         return (ENXIO);
179 }