if_sn_obio.c revision 1.24 1 /* $NetBSD: if_sn_obio.c,v 1.24 2005/01/15 16:01:00 chs 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.24 2005/01/15 16:01:00 chs 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(struct device *, struct cfdata *, void *);
70 static void sn_obio_attach(struct device *, struct device *, void *);
71 static int sn_obio_getaddr(struct sn_softc *, u_int8_t *);
72 static int sn_obio_getaddr_kludge(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(struct device *parent, struct cfdata *cf, 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(struct device *parent, struct device *self, void *aux)
104 {
105 struct obio_attach_args *oa = (struct obio_attach_args *)aux;
106 struct sn_softc *sc = (void *)self;
107 u_int8_t myaddr[ETHER_ADDR_LEN];
108 int i;
109
110 sc->snr_dcr = DCR_WAIT0 | DCR_DMABLOCK | DCR_RFT16 | DCR_TFT16;
111 sc->snr_dcr2 = 0;
112
113 sc->slotno = 9;
114
115 switch (current_mac_model->machineid) {
116 case MACH_MACC610:
117 case MACH_MACC650:
118 case MACH_MACQ610:
119 case MACH_MACQ650:
120 case MACH_MACQ700:
121 case MACH_MACQ800:
122 case MACH_MACQ900:
123 case MACH_MACQ950:
124 sc->snr_dcr |= DCR_EXBUS;
125 sc->bitmode = 1;
126 break;
127
128 case MACH_MACLC575:
129 case MACH_MACP580:
130 case MACH_MACQ630:
131 break;
132
133 case MACH_MACPB500:
134 sc->snr_dcr |= DCR_SYNC | DCR_LBR;
135 sc->bitmode = 0; /* 16 bit interface */
136 break;
137
138 default:
139 printf(": unsupported machine type\n");
140 return;
141 }
142
143 sc->sc_regt = oa->oa_tag;
144
145 if (bus_space_map(sc->sc_regt,
146 SONIC_REG_BASE, SN_REGSIZE, 0, &sc->sc_regh)) {
147 printf(": failed to map space for SONIC regs.\n");
148 return;
149 }
150
151 /* regs are addressed as words, big-endian. */
152 for (i = 0; i < SN_NREGS; i++) {
153 sc->sc_reg_map[i] = (bus_size_t)((i * 4) + 2);
154 }
155
156 /*
157 * Kind of kludge this. Comm-slot cards do not really
158 * have a visible type, as far as I can tell at this time,
159 * so assume that MacOS had it properly configured and use
160 * that configuration.
161 */
162 switch (current_mac_model->machineid) {
163 case MACH_MACLC575:
164 case MACH_MACP580:
165 case MACH_MACQ630:
166 NIC_PUT(sc, SNR_CR, CR_RST); wbflush();
167 i = NIC_GET(sc, SNR_DCR);
168 sc->snr_dcr |= (i & 0xfff0);
169 sc->bitmode = (i & DCR_DW) ? 1 : 0;
170 break;
171 default:
172 break;
173 }
174
175 if (sn_obio_getaddr(sc, myaddr) &&
176 sn_obio_getaddr_kludge(sc, myaddr)) { /* XXX kludge for PB */
177 printf(": failed to get MAC address.\n");
178 bus_space_unmap(sc->sc_regt, sc->sc_regh, SN_REGSIZE);
179 return;
180 }
181
182 printf(": integrated Ethernet adapter\n");
183
184 /* snsetup returns 1 if something fails */
185 if (snsetup(sc, myaddr)) {
186 bus_space_unmap(sc->sc_regt, sc->sc_regh, SN_REGSIZE);
187 return;
188 }
189
190 if (mac68k_machine.aux_interrupts) {
191 intr_establish((int (*)(void *))snintr, (void *)sc, 3);
192 } else {
193 add_nubus_intr(sc->slotno, snintr, (void *)sc);
194 }
195 }
196
197 static int
198 sn_obio_getaddr(struct sn_softc *sc, u_int8_t *lladdr)
199 {
200 bus_space_handle_t bsh;
201
202 if (bus_space_map(sc->sc_regt, SONIC_PROM_BASE, PAGE_SIZE, 0, &bsh)) {
203 printf(": failed to map space to read SONIC address.\n%s",
204 sc->sc_dev.dv_xname);
205 return (-1);
206 }
207
208 if (!mac68k_bus_space_probe(sc->sc_regt, bsh, 0, 1)) {
209 bus_space_unmap(sc->sc_regt, bsh, PAGE_SIZE);
210 return (-1);
211 }
212
213 sn_get_enaddr(sc->sc_regt, bsh, 0, lladdr);
214
215 bus_space_unmap(sc->sc_regt, bsh, PAGE_SIZE);
216
217 return 0;
218 }
219
220 /*
221 * Assume that the SONIC was initialized in MacOS. This should go away
222 * when we can properly get the MAC address on the PBs.
223 */
224 static int
225 sn_obio_getaddr_kludge(struct sn_softc *sc, u_int8_t *lladdr)
226 {
227 int i, ors = 0;
228
229 /* Shut down NIC */
230 NIC_PUT(sc, SNR_CR, CR_RST);
231 wbflush();
232
233 NIC_PUT(sc, SNR_CEP, 15); /* For some reason, Apple fills top first. */
234 wbflush();
235 i = NIC_GET(sc, SNR_CAP2);
236 wbflush();
237
238 ors |= i;
239 lladdr[5] = i >> 8;
240 lladdr[4] = i;
241
242 i = NIC_GET(sc, SNR_CAP1);
243 wbflush();
244
245 ors |= i;
246 lladdr[3] = i >> 8;
247 lladdr[2] = i;
248
249 i = NIC_GET(sc, SNR_CAP0);
250 wbflush();
251
252 ors |= i;
253 lladdr[1] = i >> 8;
254 lladdr[0] = i;
255
256 NIC_PUT(sc, SNR_CR, 0);
257 wbflush();
258
259 if (ors == 0)
260 return -1;
261
262 return 0;
263 }
264