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