if_sn_obio.c revision 1.7 1 /* $NetBSD: if_sn_obio.c,v 1.7 1997/04/10 03:22:48 briggs Exp $ */
2
3 /*
4 * Copyright (C) 1997 Allen Briggs
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Allen Briggs
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/param.h>
34 #include <sys/device.h>
35 #include <sys/errno.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/syslog.h>
39 #include <sys/systm.h>
40
41 #include <net/if.h>
42 #include <net/if_ether.h>
43
44 #if 0 /* XXX this shouldn't be necessary... else reinsert */
45 #ifdef INET
46 #include <netinet/in.h>
47 #include <netinet/if_ether.h>
48 #endif
49 #endif
50
51 #include <machine/bus.h>
52 #include <machine/cpu.h>
53 #include <machine/viareg.h>
54
55 #include "obiovar.h"
56 #include "if_snreg.h"
57 #include "if_snvar.h"
58
59 #define SONIC_REG_BASE 0x50F0A000
60 #define SONIC_PROM_BASE 0x50F08000
61
62 static int sn_obio_match __P((struct device *, struct cfdata *, void *));
63 static void sn_obio_attach __P((struct device *, struct device *, void *));
64 static int sn_obio_getaddr __P((struct sn_softc *, u_int8_t *));
65 static int sn_obio_getaddr_kludge __P((struct sn_softc *, u_int8_t *));
66
67 struct cfattach sn_obio_ca = {
68 sizeof(struct sn_softc), sn_obio_match, sn_obio_attach
69 };
70
71 static int
72 sn_obio_match(parent, cf, aux)
73 struct device *parent;
74 struct cfdata *cf;
75 void *aux;
76 {
77 if (mac68k_machine.sonic)
78 return 1;
79
80 return 0;
81 }
82
83 /*
84 * Install interface into kernel networking data structures
85 */
86 static void
87 sn_obio_attach(parent, self, aux)
88 struct device *parent, *self;
89 void *aux;
90 {
91 struct obio_attach_args *oa = (struct obio_attach_args *) aux;
92 struct sn_softc *sc = (void *)self;
93 u_int8_t myaddr[ETHER_ADDR_LEN];
94 int i;
95
96 sc->snr_dcr = DCR_WAIT0 | DCR_DMABLOCK | DCR_RFT16 | DCR_TFT16;
97 sc->snr_dcr2 = 0;
98
99 switch (current_mac_model->machineid) {
100 case MACH_MACQ700: case MACH_MACQ900: case MACH_MACQ950:
101 case MACH_MACQ800: case MACH_MACQ650: case MACH_MACC650:
102 case MACH_MACC610: case MACH_MACQ610:
103 sc->snr_dcr |= DCR_EXBUS | DCR_DW32;
104 sc->bitmode = 1;
105 break;
106
107 case MACH_MACPB500:
108 sc->snr_dcr |= DCR_SYNC | DCR_LBR | DCR_DW16;
109 sc->bitmode = 0;
110 break;
111
112 default:
113 printf(": unsupported machine type\n");
114 return;
115 }
116
117 sc->sc_regt = oa->oa_tag;
118
119 if (bus_space_map(sc->sc_regt, SONIC_REG_BASE, SN_REGSIZE,
120 0, &sc->sc_regh)) {
121 panic(": failed to map space for SONIC regs.\n");
122 }
123
124 sc->slotno = 9;
125
126 /* regs are addressed as words, big-endian. */
127 for (i = 0; i < SN_NREGS; i++) {
128 sc->sc_reg_map[i] = (bus_size_t)((i * 4) + 2);
129 }
130
131 if (sn_obio_getaddr(sc, myaddr)) {
132 printf(": failed to get MAC address. Trying kludge.\n");
133 if (sn_obio_getaddr_kludge(sc, myaddr)) {
134 printf("Kludge failed, too. Attachment failing.\n");
135 bus_space_unmap(sc->sc_regt, sc->sc_regh, SN_REGSIZE);
136 return;
137 }
138 }
139
140 /* snsetup returns 1 if something fails */
141 if (snsetup(sc, myaddr)) {
142 bus_space_unmap(sc->sc_regt, sc->sc_regh, SN_REGSIZE);
143 return;
144 }
145
146 add_nubus_intr(sc->slotno, snintr, (void *)sc);
147 }
148
149 static u_char bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
150 #define bbr(v) ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
151
152 static int
153 sn_obio_getaddr(sc, lladdr)
154 struct sn_softc *sc;
155 u_int8_t *lladdr;
156 {
157 bus_space_handle_t bsh;
158 int i, do_bbr;
159 u_char b;
160
161 if (bus_space_map(sc->sc_regt, SONIC_PROM_BASE, NBPG, 0, &bsh)) {
162 panic("failed to map space to read SONIC address.\n");
163 }
164
165 if (!bus_probe(sc->sc_regt, bsh, 0, 1)) {
166 bus_space_unmap(sc->sc_regt, bsh, NBPG);
167 return -1;
168 }
169
170 /*
171 * For reasons known only to Apple, MAC addresses in the ethernet
172 * PROM are stored in Token Ring (IEEE 802.5) format, that is
173 * with all of the bits in each byte reversed (canonical bit format).
174 * When the address is read out it must be reversed to ethernet format
175 * before use.
176 *
177 * Apple has been assigned OUI's 08:00:07 and 00:a0:40. All onboard
178 * ethernet addresses on 68K machines should be in one of these
179 * two ranges.
180 *
181 * Here is where it gets complicated.
182 *
183 * The PMac 7200, 7500, 8500, and 9500 accidentally had the PROM
184 * written in standard ethernet format. The MacOS accounted for this
185 * in these systems, and did not reverse the bytes. Some other
186 * networking utilities were not so forgiving, and got confused.
187 * "Some" of Apple's Nubus ethernet cards also had their bits
188 * burned in ethernet format.
189 *
190 * Apple petitioned the IEEE and was granted the 00:05:02 (bit reversal
191 * of 00:a0:40) as well. As of OpenTransport 1.1.1, Apple removed
192 * their workaround and now reverses the bits regardless of
193 * what kind of machine it is. So PMac systems and the affected
194 * Nubus cards now use 00:05:02, instead of the 00:a0:40 for which they
195 * were intended.
196 *
197 * See Apple Techinfo article TECHINFO-0020552, "OpenTransport 1.1.1
198 * and MacOS System 7.5.3 FAQ (10/96)" for more details.
199 */
200 do_bbr = 0;
201 b = bus_space_read_1(sc->sc_regt, bsh, 0);
202 if (b == 0x10)
203 do_bbr = 1;
204 lladdr[0] = (do_bbr) ? bbr(b) : b;
205
206 for (i = 1 ; i < ETHER_ADDR_LEN ; i++) {
207 b = bus_space_read_1(sc->sc_regt, bsh, i);
208 lladdr[i] = (do_bbr) ? bbr(b) : b;
209 }
210
211 bus_space_unmap(sc->sc_regt, bsh, NBPG);
212
213 return 0;
214 }
215
216 /*
217 * Assume that the SONIC was initialized in MacOS. This should go away
218 * when we can properly get the MAC address on the PBs.
219 */
220 static int
221 sn_obio_getaddr_kludge(sc, lladdr)
222 struct sn_softc *sc;
223 u_int8_t *lladdr;
224 {
225 int i, ors=0;
226
227 /* Shut down NIC */
228 NIC_PUT(sc, SNR_CR, CR_RST);
229 wbflush();
230
231 NIC_PUT(sc, SNR_CEP, 15); /* For some reason, Apple fills top first. */
232 wbflush();
233 i = NIC_GET(sc, SNR_CAP2);
234 wbflush();
235
236 ors |= i;
237 lladdr[5] = i >> 8;
238 lladdr[4] = i;
239
240 i = NIC_GET(sc, SNR_CAP1);
241 wbflush();
242
243 ors |= i;
244 lladdr[3] = i >> 8;
245 lladdr[2] = i;
246
247 i = NIC_GET(sc, SNR_CAP0);
248 wbflush();
249
250 ors |= i;
251 lladdr[1] = i >> 8;
252 lladdr[0] = i;
253
254 NIC_PUT(sc, SNR_CR, 0);
255 wbflush();
256
257 if (ors == 0) return -1;
258 return 0;
259 }
260