if_sn_obio.c revision 1.22 1 /* $NetBSD: if_sn_obio.c,v 1.22 2003/04/02 00:44:28 thorpej 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 "opt_inet.h"
34
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/errno.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <sys/syslog.h>
41 #include <sys/systm.h>
42
43 #include <uvm/uvm_extern.h>
44
45 #include <net/if.h>
46 #include <net/if_ether.h>
47
48 #if 0 /* XXX this shouldn't be necessary... else reinsert */
49 #ifdef INET
50 #include <netinet/in.h>
51 #include <netinet/if_ether.h>
52 #endif
53 #endif
54
55 #include <machine/bus.h>
56 #include <machine/cpu.h>
57 #include <machine/viareg.h>
58
59 #include <mac68k/obio/obiovar.h>
60 #include <mac68k/dev/if_snreg.h>
61 #include <mac68k/dev/if_snvar.h>
62
63 #define SONIC_REG_BASE 0x50F0A000
64 #define SONIC_PROM_BASE 0x50F08000
65
66 static int sn_obio_match __P((struct device *, struct cfdata *, void *));
67 static void sn_obio_attach __P((struct device *, struct device *, void *));
68 static int sn_obio_getaddr __P((struct sn_softc *, u_int8_t *));
69 static int sn_obio_getaddr_kludge __P((struct sn_softc *, u_int8_t *));
70
71 CFATTACH_DECL(sn_obio, sizeof(struct sn_softc),
72 sn_obio_match, sn_obio_attach, NULL, NULL);
73
74 static int
75 sn_obio_match(parent, cf, aux)
76 struct device *parent;
77 struct cfdata *cf;
78 void *aux;
79 {
80 struct obio_attach_args *oa = (struct obio_attach_args *)aux;
81 bus_space_handle_t bsh;
82 int found = 0;
83
84 if (!mac68k_machine.sonic)
85 return 0;
86
87 if (bus_space_map(oa->oa_tag,
88 SONIC_REG_BASE, SN_REGSIZE, 0, &bsh))
89 return 0;
90
91 if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0, 4))
92 found = 1;
93
94 bus_space_unmap(oa->oa_tag, bsh, SN_REGSIZE);
95
96 return found;
97 }
98
99 /*
100 * Install interface into kernel networking data structures
101 */
102 static void
103 sn_obio_attach(parent, self, aux)
104 struct device *parent, *self;
105 void *aux;
106 {
107 struct obio_attach_args *oa = (struct obio_attach_args *)aux;
108 struct sn_softc *sc = (void *)self;
109 u_int8_t myaddr[ETHER_ADDR_LEN];
110 int i;
111
112 sc->snr_dcr = DCR_WAIT0 | DCR_DMABLOCK | DCR_RFT16 | DCR_TFT16;
113 sc->snr_dcr2 = 0;
114
115 sc->slotno = 9;
116
117 switch (current_mac_model->machineid) {
118 case MACH_MACC610:
119 case MACH_MACC650:
120 case MACH_MACQ610:
121 case MACH_MACQ650:
122 case MACH_MACQ700:
123 case MACH_MACQ800:
124 case MACH_MACQ900:
125 case MACH_MACQ950:
126 sc->snr_dcr |= DCR_EXBUS;
127 sc->bitmode = 1;
128 break;
129
130 case MACH_MACLC575:
131 case MACH_MACP580:
132 case MACH_MACQ630:
133 break;
134
135 case MACH_MACPB500:
136 sc->snr_dcr |= DCR_SYNC | DCR_LBR;
137 sc->bitmode = 0; /* 16 bit interface */
138 break;
139
140 default:
141 printf(": unsupported machine type\n");
142 return;
143 }
144
145 sc->sc_regt = oa->oa_tag;
146
147 if (bus_space_map(sc->sc_regt,
148 SONIC_REG_BASE, SN_REGSIZE, 0, &sc->sc_regh)) {
149 printf(": failed to map space for SONIC regs.\n");
150 return;
151 }
152
153 /* regs are addressed as words, big-endian. */
154 for (i = 0; i < SN_NREGS; i++) {
155 sc->sc_reg_map[i] = (bus_size_t)((i * 4) + 2);
156 }
157
158 /*
159 * Kind of kludge this. Comm-slot cards do not really
160 * have a visible type, as far as I can tell at this time,
161 * so assume that MacOS had it properly configured and use
162 * that configuration.
163 */
164 switch (current_mac_model->machineid) {
165 case MACH_MACLC575:
166 case MACH_MACP580:
167 case MACH_MACQ630:
168 NIC_PUT(sc, SNR_CR, CR_RST); wbflush();
169 i = NIC_GET(sc, SNR_DCR);
170 sc->snr_dcr |= (i & 0xfff0);
171 sc->bitmode = (i & DCR_DW) ? 1 : 0;
172 break;
173 default:
174 break;
175 }
176
177 if (sn_obio_getaddr(sc, myaddr) &&
178 sn_obio_getaddr_kludge(sc, myaddr)) { /* XXX kludge for PB */
179 printf(": failed to get MAC address.\n");
180 bus_space_unmap(sc->sc_regt, sc->sc_regh, SN_REGSIZE);
181 return;
182 }
183
184 printf(": integrated Ethernet adapter\n");
185
186 /* snsetup returns 1 if something fails */
187 if (snsetup(sc, myaddr)) {
188 bus_space_unmap(sc->sc_regt, sc->sc_regh, SN_REGSIZE);
189 return;
190 }
191
192 if (mac68k_machine.aux_interrupts) {
193 intr_establish((int (*)(void *))snintr, (void *)sc, 3);
194 } else {
195 add_nubus_intr(sc->slotno, snintr, (void *)sc);
196 }
197 }
198
199 static int
200 sn_obio_getaddr(sc, lladdr)
201 struct sn_softc *sc;
202 u_int8_t *lladdr;
203 {
204 bus_space_handle_t bsh;
205
206 if (bus_space_map(sc->sc_regt, SONIC_PROM_BASE, PAGE_SIZE, 0, &bsh)) {
207 printf(": failed to map space to read SONIC address.\n%s",
208 sc->sc_dev.dv_xname);
209 return (-1);
210 }
211
212 if (!mac68k_bus_space_probe(sc->sc_regt, bsh, 0, 1)) {
213 bus_space_unmap(sc->sc_regt, bsh, PAGE_SIZE);
214 return (-1);
215 }
216
217 sn_get_enaddr(sc->sc_regt, bsh, 0, lladdr);
218
219 bus_space_unmap(sc->sc_regt, bsh, PAGE_SIZE);
220
221 return 0;
222 }
223
224 /*
225 * Assume that the SONIC was initialized in MacOS. This should go away
226 * when we can properly get the MAC address on the PBs.
227 */
228 static int
229 sn_obio_getaddr_kludge(sc, lladdr)
230 struct sn_softc *sc;
231 u_int8_t *lladdr;
232 {
233 int i, ors = 0;
234
235 /* Shut down NIC */
236 NIC_PUT(sc, SNR_CR, CR_RST);
237 wbflush();
238
239 NIC_PUT(sc, SNR_CEP, 15); /* For some reason, Apple fills top first. */
240 wbflush();
241 i = NIC_GET(sc, SNR_CAP2);
242 wbflush();
243
244 ors |= i;
245 lladdr[5] = i >> 8;
246 lladdr[4] = i;
247
248 i = NIC_GET(sc, SNR_CAP1);
249 wbflush();
250
251 ors |= i;
252 lladdr[3] = i >> 8;
253 lladdr[2] = i;
254
255 i = NIC_GET(sc, SNR_CAP0);
256 wbflush();
257
258 ors |= i;
259 lladdr[1] = i >> 8;
260 lladdr[0] = i;
261
262 NIC_PUT(sc, SNR_CR, 0);
263 wbflush();
264
265 if (ors == 0)
266 return -1;
267
268 return 0;
269 }
270