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