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