if_netdock_nubus.c revision 1.17 1 1.17 hauke /* $NetBSD: if_netdock_nubus.c,v 1.17 2008/04/02 18:40:02 hauke Exp $ */
2 1.1 itojun
3 1.1 itojun /*
4 1.1 itojun * Copyright (C) 2000,2002 Daishi Kato <daishi (at) axlight.com>
5 1.1 itojun * All rights reserved.
6 1.1 itojun *
7 1.1 itojun * Redistribution and use in source and binary forms, with or without
8 1.1 itojun * modification, are permitted provided that the following conditions
9 1.1 itojun * are met:
10 1.1 itojun * 1. Redistributions of source code must retain the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer.
12 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 itojun * notice, this list of conditions and the following disclaimer in the
14 1.1 itojun * documentation and/or other materials provided with the distribution.
15 1.1 itojun * 3. All advertising materials mentioning features or use of this software
16 1.1 itojun * must display the following acknowledgement:
17 1.1 itojun * This product includes software developed by Daishi Kato
18 1.1 itojun * 4. The name of the author may not be used to endorse or promote products
19 1.1 itojun * derived from this software without specific prior written permission.
20 1.1 itojun *
21 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 itojun * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 itojun * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 itojun * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 itojun * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 itojun * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 itojun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 itojun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 itojun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 itojun * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 itojun */
32 1.1 itojun
33 1.1 itojun /*
34 1.1 itojun * Asante NetDock (for Duo series) driver
35 1.1 itojun * the chip inside is not known
36 1.1 itojun */
37 1.1 itojun
38 1.1 itojun /*
39 1.1 itojun * The author would like to thank Takeo Kuwata <tkuwata (at) mac.com> for
40 1.1 itojun * his help in stabilizing this driver.
41 1.1 itojun */
42 1.1 itojun
43 1.1 itojun /***********************/
44 1.4 lukem
45 1.4 lukem #include <sys/cdefs.h>
46 1.17 hauke __KERNEL_RCSID(0, "$NetBSD: if_netdock_nubus.c,v 1.17 2008/04/02 18:40:02 hauke Exp $");
47 1.1 itojun
48 1.1 itojun #include <sys/param.h>
49 1.1 itojun #include <sys/device.h>
50 1.1 itojun #include <sys/socket.h>
51 1.1 itojun #include <sys/systm.h>
52 1.1 itojun #include <sys/mbuf.h>
53 1.1 itojun #include <sys/ioctl.h>
54 1.1 itojun
55 1.1 itojun #include <net/if.h>
56 1.1 itojun #include <net/if_dl.h>
57 1.1 itojun #include <net/if_ether.h>
58 1.1 itojun
59 1.1 itojun #include "opt_inet.h"
60 1.1 itojun #ifdef INET
61 1.1 itojun #include <netinet/in.h>
62 1.1 itojun #include <netinet/if_inarp.h>
63 1.1 itojun #endif
64 1.1 itojun
65 1.1 itojun #include "bpfilter.h"
66 1.1 itojun #if NBPFILTER > 0
67 1.1 itojun #include <net/bpf.h>
68 1.1 itojun #endif
69 1.1 itojun
70 1.1 itojun #include <machine/bus.h>
71 1.1 itojun #include <machine/viareg.h>
72 1.1 itojun #include <mac68k/nubus/nubus.h>
73 1.1 itojun
74 1.1 itojun /***********************/
75 1.1 itojun
76 1.1 itojun #define NETDOCK_DEBUG
77 1.1 itojun
78 1.1 itojun #define NETDOCK_NUBUS_CATEGORY 0x0020
79 1.1 itojun #define NETDOCK_NUBUS_TYPE 0x0003
80 1.1 itojun #define NETDOCK_NUBUS_DRSW 0x0103
81 1.1 itojun #define NETDOCK_NUBUS_DRHW 0x0100
82 1.1 itojun
83 1.1 itojun #define ETHERMICRODOCK_NUBUS_CATEGORY 0x0020
84 1.1 itojun #define ETHERMICRODOCK_NUBUS_TYPE 0x0003
85 1.1 itojun #define ETHERMICRODOCK_NUBUS_DRSW 0x0102
86 1.1 itojun #define ETHERMICRODOCK_NUBUS_DRHW 0x0100
87 1.1 itojun
88 1.1 itojun #define REG_ISR 0x000c
89 1.1 itojun #define REG_000E 0x000e
90 1.1 itojun #define REG_0000 0x0000
91 1.1 itojun #define REG_0002 0x0002
92 1.1 itojun #define REG_0004 0x0004
93 1.1 itojun #define REG_0006 0x0006
94 1.1 itojun #define REG_DATA 0x0008
95 1.1 itojun #define REG_EFD00 0xefd00
96 1.1 itojun
97 1.1 itojun #define ISR_ALL 0x3300
98 1.1 itojun #define ISR_TX 0x0200
99 1.1 itojun #define ISR_RX 0x0100
100 1.1 itojun #define ISR_READY 0x0800
101 1.1 itojun #define ISR_BIT_0C 0x1000
102 1.1 itojun #define ISR_BIT_0D 0x2000
103 1.1 itojun #define ISR_MASK 0x0033
104 1.1 itojun #define ISR_BIT_03 0x0008
105 1.1 itojun
106 1.1 itojun #define REG_0002_BIT_04 0x0010
107 1.1 itojun #define REG_0000_BIT_08 0x0100
108 1.1 itojun #define REG_0004_BIT_0F 0x8000
109 1.1 itojun #define REG_0004_BIT_07 0x0080
110 1.1 itojun #define REG_DATA_BIT_02 0x0004
111 1.1 itojun #define REG_DATA_BIT_03 0x0008
112 1.1 itojun #define REG_DATA_BIT_04 0x0010
113 1.1 itojun #define REG_DATA_BIT_05 0x0020
114 1.1 itojun #define REG_DATA_BIT_08 0x0100
115 1.1 itojun #define REG_DATA_BIT_07 0x0080
116 1.1 itojun #define REG_DATA_BIT_0F 0x8000
117 1.1 itojun
118 1.1 itojun /***********************/
119 1.1 itojun
120 1.1 itojun typedef struct netdock_softc {
121 1.1 itojun struct device sc_dev;
122 1.1 itojun struct ethercom sc_ethercom;
123 1.1 itojun #define sc_if sc_ethercom.ec_if
124 1.1 itojun
125 1.1 itojun bus_space_tag_t sc_regt;
126 1.1 itojun bus_space_handle_t sc_regh;
127 1.1 itojun
128 1.1 itojun u_int8_t sc_enaddr[ETHER_ADDR_LEN];
129 1.1 itojun
130 1.1 itojun } netdock_softc_t;
131 1.1 itojun
132 1.1 itojun /***********************/
133 1.1 itojun
134 1.6 chs static int netdock_nubus_match(struct device *, struct cfdata *, void *);
135 1.6 chs static void netdock_nubus_attach(struct device *, struct device *, void *);
136 1.6 chs static int netdock_nb_get_enaddr(bus_space_tag_t, bus_space_handle_t,
137 1.6 chs struct nubus_attach_args *, u_int8_t *);
138 1.1 itojun #ifdef NETDOCK_DEBUG_DRIVER
139 1.6 chs static void netdock_print_driver(bus_space_tag_t, bus_space_handle_t,
140 1.6 chs struct nubus_attach_args *);
141 1.1 itojun #endif
142 1.1 itojun
143 1.6 chs int netdock_setup(struct netdock_softc *, u_int8_t *);
144 1.6 chs void netdock_intr(void *);
145 1.1 itojun
146 1.6 chs static void netdock_watchdog(struct ifnet *);
147 1.6 chs static int netdock_init(struct netdock_softc *);
148 1.6 chs static int netdock_stop(struct netdock_softc *);
149 1.12 christos static int netdock_ioctl(struct ifnet *, u_long, void *);
150 1.6 chs static void netdock_start(struct ifnet *);
151 1.6 chs static void netdock_reset(struct netdock_softc *);
152 1.6 chs static void netdock_txint(struct netdock_softc *);
153 1.6 chs static void netdock_rxint(struct netdock_softc *);
154 1.6 chs
155 1.6 chs static u_int netdock_put(struct netdock_softc *, struct mbuf *);
156 1.6 chs static int netdock_read(struct netdock_softc *, int);
157 1.6 chs static struct mbuf *netdock_get(struct netdock_softc *, int);
158 1.1 itojun
159 1.1 itojun /***********************/
160 1.1 itojun
161 1.1 itojun #define NIC_GET_1(sc, o) (bus_space_read_1((sc)->sc_regt, \
162 1.1 itojun (sc)->sc_regh, (o)))
163 1.1 itojun #define NIC_PUT_1(sc, o, val) (bus_space_write_1((sc)->sc_regt, \
164 1.1 itojun (sc)->sc_regh, (o), (val)))
165 1.1 itojun #define NIC_GET_2(sc, o) (bus_space_read_2((sc)->sc_regt, \
166 1.1 itojun (sc)->sc_regh, (o)))
167 1.1 itojun #define NIC_PUT_2(sc, o, val) (bus_space_write_2((sc)->sc_regt, \
168 1.1 itojun (sc)->sc_regh, (o), (val)))
169 1.1 itojun #define NIC_GET_4(sc, o) (bus_space_read_4((sc)->sc_regt, \
170 1.1 itojun (sc)->sc_regh, (o)))
171 1.1 itojun #define NIC_PUT_4(sc, o, val) (bus_space_write_4((sc)->sc_regt, \
172 1.1 itojun (sc)->sc_regh, (o), (val)))
173 1.1 itojun
174 1.1 itojun #define NIC_BSET(sc, o, b) \
175 1.11 perry __asm volatile("bset %0,%1" : : "di" ((u_short)(b)), \
176 1.1 itojun "g" (*(u_int8_t *)((sc)->sc_regh.base + (o))))
177 1.1 itojun #define NIC_BCLR(sc, o, b) \
178 1.11 perry __asm volatile("bclr %0,%1" : : "di" ((u_short)(b)), \
179 1.1 itojun "g" (*(u_int8_t *)((sc)->sc_regh.base + (o))))
180 1.1 itojun #define NIC_ANDW(sc, o, b) \
181 1.11 perry __asm volatile("andw %0,%1" : : "di" ((u_short)(b)), \
182 1.1 itojun "g" (*(u_int8_t *)((sc)->sc_regh.base + (o))))
183 1.1 itojun #define NIC_ORW(sc, o, b) \
184 1.11 perry __asm volatile("orw %0,%1" : : "di" ((u_short)(b)), \
185 1.1 itojun "g" (*(u_int8_t *)((sc)->sc_regh.base + (o))))
186 1.1 itojun
187 1.1 itojun
188 1.1 itojun /***********************/
189 1.1 itojun
190 1.3 thorpej CFATTACH_DECL(netdock_nubus, sizeof(struct netdock_softc),
191 1.3 thorpej netdock_nubus_match, netdock_nubus_attach, NULL, NULL);
192 1.1 itojun
193 1.1 itojun /***********************/
194 1.1 itojun
195 1.1 itojun static int
196 1.6 chs netdock_nubus_match(struct device *parent, struct cfdata *cf, void *aux)
197 1.1 itojun {
198 1.1 itojun struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
199 1.1 itojun bus_space_handle_t bsh;
200 1.1 itojun int rv;
201 1.1 itojun
202 1.1 itojun if (bus_space_map(na->na_tag, NUBUS_SLOT2PA(na->slot),
203 1.1 itojun NBMEMSIZE, 0, &bsh))
204 1.1 itojun return (0);
205 1.1 itojun
206 1.1 itojun rv = 0;
207 1.1 itojun
208 1.1 itojun if (na->category == NETDOCK_NUBUS_CATEGORY &&
209 1.1 itojun na->type == NETDOCK_NUBUS_TYPE &&
210 1.1 itojun na->drsw == NETDOCK_NUBUS_DRSW) {
211 1.1 itojun /* assuming this IS Asante NetDock */
212 1.1 itojun rv = 1;
213 1.1 itojun }
214 1.1 itojun
215 1.1 itojun if (na->category == ETHERMICRODOCK_NUBUS_CATEGORY &&
216 1.1 itojun na->type == ETHERMICRODOCK_NUBUS_TYPE &&
217 1.1 itojun na->drsw == ETHERMICRODOCK_NUBUS_DRSW) {
218 1.1 itojun /* assuming this IS Newer EtherMicroDock */
219 1.1 itojun rv = 1;
220 1.1 itojun }
221 1.1 itojun
222 1.1 itojun bus_space_unmap(na->na_tag, bsh, NBMEMSIZE);
223 1.1 itojun
224 1.1 itojun return rv;
225 1.1 itojun }
226 1.1 itojun
227 1.1 itojun static void
228 1.6 chs netdock_nubus_attach(struct device *parent, struct device *self, void *aux)
229 1.1 itojun {
230 1.1 itojun struct netdock_softc *sc = (struct netdock_softc *)self;
231 1.1 itojun struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
232 1.1 itojun bus_space_tag_t bst;
233 1.1 itojun bus_space_handle_t bsh;
234 1.1 itojun u_int8_t enaddr[ETHER_ADDR_LEN];
235 1.8 rjs const char *cardtype;
236 1.1 itojun
237 1.1 itojun bst = na->na_tag;
238 1.1 itojun if (bus_space_map(bst, NUBUS_SLOT2PA(na->slot), NBMEMSIZE, 0, &bsh)) {
239 1.1 itojun printf(": failed to map memory space.\n");
240 1.1 itojun return;
241 1.1 itojun }
242 1.1 itojun
243 1.1 itojun sc->sc_regt = bst;
244 1.1 itojun cardtype = nubus_get_card_name(bst, bsh, na->fmt);
245 1.1 itojun
246 1.1 itojun #ifdef NETDOCK_DEBUG_DRIVER
247 1.1 itojun netdock_print_driver(bst, bsh, na);
248 1.1 itojun #endif
249 1.1 itojun
250 1.1 itojun if (netdock_nb_get_enaddr(bst, bsh, na, enaddr)) {
251 1.1 itojun printf(": can't find MAC address.\n");
252 1.1 itojun bus_space_unmap(bst, bsh, NBMEMSIZE);
253 1.1 itojun return;
254 1.1 itojun }
255 1.1 itojun
256 1.1 itojun if (bus_space_subregion(bst, bsh, 0xe00300, 0xf0000, &sc->sc_regh)) {
257 1.1 itojun printf(": failed to map register space.\n");
258 1.1 itojun bus_space_unmap(bst, bsh, NBMEMSIZE);
259 1.1 itojun return;
260 1.1 itojun }
261 1.1 itojun
262 1.1 itojun printf(": %s\n", cardtype);
263 1.1 itojun
264 1.1 itojun if (netdock_setup(sc, enaddr)) {
265 1.1 itojun bus_space_unmap(bst, bsh, NBMEMSIZE);
266 1.1 itojun return;
267 1.1 itojun }
268 1.1 itojun
269 1.1 itojun add_nubus_intr(na->slot, netdock_intr, (void *)sc);
270 1.1 itojun
271 1.1 itojun return;
272 1.1 itojun }
273 1.1 itojun
274 1.1 itojun static int
275 1.6 chs netdock_nb_get_enaddr(bus_space_tag_t bst, bus_space_handle_t bsh,
276 1.6 chs struct nubus_attach_args *na, u_int8_t *ep)
277 1.1 itojun {
278 1.1 itojun nubus_dir dir;
279 1.1 itojun nubus_dirent dirent;
280 1.1 itojun
281 1.1 itojun /*
282 1.1 itojun * these hardwired resource IDs are only for NetDock
283 1.1 itojun */
284 1.1 itojun nubus_get_main_dir(na->fmt, &dir);
285 1.1 itojun if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x81, &dirent) <= 0)
286 1.1 itojun return 1;
287 1.1 itojun nubus_get_dir_from_rsrc(na->fmt, &dirent, &dir);
288 1.1 itojun if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x80, &dirent) <= 0)
289 1.1 itojun return 1;
290 1.1 itojun if (nubus_get_ind_data(bst, bsh, na->fmt, &dirent,
291 1.1 itojun ep, ETHER_ADDR_LEN) <= 0)
292 1.1 itojun return 1;
293 1.1 itojun
294 1.1 itojun return 0;
295 1.1 itojun }
296 1.1 itojun
297 1.1 itojun #ifdef NETDOCK_DEBUG_DRIVER
298 1.1 itojun static void
299 1.6 chs netdock_print_driver(bus_space_tag_t bst, bus_space_handle_t bsh,
300 1.6 chs struct nubus_attach_args *na)
301 1.1 itojun {
302 1.1 itojun #define HEADSIZE (8+4)
303 1.1 itojun #define CODESIZE (6759-4)
304 1.1 itojun unsigned char mydata[HEADSIZE + CODESIZE];
305 1.1 itojun nubus_dir dir;
306 1.1 itojun nubus_dirent dirent;
307 1.1 itojun int i, rv;
308 1.1 itojun
309 1.1 itojun nubus_get_main_dir(na->fmt, &dir);
310 1.1 itojun rv = nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x81, &dirent);
311 1.1 itojun if (rv <= 0) {
312 1.1 itojun printf(": can't find sResource.\n");
313 1.1 itojun return;
314 1.1 itojun }
315 1.1 itojun nubus_get_dir_from_rsrc(na->fmt, &dirent, &dir);
316 1.1 itojun if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, NUBUS_RSRC_DRVRDIR,
317 1.1 itojun &dirent) <= 0) {
318 1.1 itojun printf(": can't find sResource.\n");
319 1.1 itojun return;
320 1.1 itojun }
321 1.1 itojun if (nubus_get_ind_data(bst, bsh, na->fmt,
322 1.1 itojun &dirent, mydata, HEADSIZE+CODESIZE) <= 0) {
323 1.1 itojun printf(": can't find indirect data.\n");
324 1.1 itojun return;
325 1.1 itojun }
326 1.1 itojun printf("\n########## begin driver dir");
327 1.1 itojun for (i = 0; i < HEADSIZE; i++) {
328 1.1 itojun if (i % 16 == 0)
329 1.1 itojun printf("\n%02x:",i);
330 1.1 itojun printf(" %02x", mydata[i]);
331 1.1 itojun }
332 1.1 itojun printf("\n########## begin driver code");
333 1.1 itojun for (i = 0; i < CODESIZE; i++) {
334 1.1 itojun if (i % 16 == 0)
335 1.1 itojun printf("\n%02x:",i);
336 1.1 itojun printf(" %02x", mydata[i + HEADSIZE]);
337 1.1 itojun }
338 1.1 itojun #if 0
339 1.1 itojun printf("\n########## begin driver code (partial)\n");
340 1.1 itojun #define PARTSIZE 256
341 1.1 itojun #define OFFSET 0x1568
342 1.1 itojun for (i = OFFSET; i < OFFSET + PARTSIZE; i++) {
343 1.1 itojun if ((i - OFFSET) % 16 == 0)
344 1.1 itojun printf("\n%02x:",i);
345 1.1 itojun printf(" %02x", mydata[i + HEADSIZE]);
346 1.1 itojun }
347 1.1 itojun #endif
348 1.1 itojun printf("\n########## end\n");
349 1.1 itojun }
350 1.1 itojun #endif
351 1.1 itojun
352 1.1 itojun
353 1.1 itojun int
354 1.6 chs netdock_setup(struct netdock_softc *sc, u_int8_t *lladdr)
355 1.1 itojun {
356 1.1 itojun struct ifnet *ifp = &sc->sc_if;
357 1.1 itojun
358 1.6 chs memcpy(sc->sc_enaddr, lladdr, ETHER_ADDR_LEN);
359 1.1 itojun printf("%s: Ethernet address %s\n",
360 1.17 hauke sc->sc_dev->dv_xname, ether_sprintf(lladdr));
361 1.1 itojun
362 1.17 hauke memcpy(ifp->if_xname, sc->sc_dev->dv_xname, IFNAMSIZ);
363 1.1 itojun ifp->if_softc = sc;
364 1.1 itojun ifp->if_ioctl = netdock_ioctl;
365 1.1 itojun ifp->if_start = netdock_start;
366 1.1 itojun ifp->if_flags =
367 1.1 itojun IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
368 1.1 itojun ifp->if_watchdog = netdock_watchdog;
369 1.1 itojun
370 1.1 itojun if_attach(ifp);
371 1.1 itojun ether_ifattach(ifp, lladdr);
372 1.1 itojun
373 1.1 itojun return (0);
374 1.1 itojun }
375 1.1 itojun
376 1.1 itojun static int
377 1.12 christos netdock_ioctl(struct ifnet *ifp, u_long cmd, void *data)
378 1.1 itojun {
379 1.1 itojun struct ifaddr *ifa;
380 1.1 itojun struct netdock_softc *sc = ifp->if_softc;
381 1.1 itojun int s = splnet();
382 1.1 itojun int err = 0;
383 1.1 itojun int temp;
384 1.1 itojun
385 1.1 itojun switch (cmd) {
386 1.1 itojun case SIOCSIFADDR:
387 1.1 itojun ifa = (struct ifaddr *)data;
388 1.1 itojun ifp->if_flags |= IFF_UP;
389 1.1 itojun switch (ifa->ifa_addr->sa_family) {
390 1.1 itojun #ifdef INET
391 1.1 itojun case AF_INET:
392 1.1 itojun (void)netdock_init(sc);
393 1.1 itojun arp_ifinit(ifp, ifa);
394 1.1 itojun break;
395 1.1 itojun #endif
396 1.1 itojun default:
397 1.1 itojun (void)netdock_init(sc);
398 1.1 itojun break;
399 1.1 itojun }
400 1.1 itojun break;
401 1.1 itojun
402 1.1 itojun case SIOCSIFFLAGS:
403 1.1 itojun if ((ifp->if_flags & IFF_UP) == 0 &&
404 1.1 itojun (ifp->if_flags & IFF_RUNNING) != 0) {
405 1.1 itojun netdock_stop(sc);
406 1.1 itojun ifp->if_flags &= ~IFF_RUNNING;
407 1.1 itojun } else if ((ifp->if_flags & IFF_UP) != 0 &&
408 1.1 itojun (ifp->if_flags & IFF_RUNNING) == 0) {
409 1.1 itojun (void)netdock_init(sc);
410 1.1 itojun } else {
411 1.1 itojun temp = ifp->if_flags & IFF_UP;
412 1.1 itojun netdock_reset(sc);
413 1.1 itojun ifp->if_flags |= temp;
414 1.1 itojun netdock_start(ifp);
415 1.1 itojun }
416 1.1 itojun break;
417 1.1 itojun
418 1.1 itojun case SIOCADDMULTI:
419 1.1 itojun case SIOCDELMULTI:
420 1.15 jmmv if ((err = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
421 1.5 thorpej if (ifp->if_flags & IFF_RUNNING) {
422 1.5 thorpej temp = ifp->if_flags & IFF_UP;
423 1.5 thorpej netdock_reset(sc);
424 1.5 thorpej ifp->if_flags |= temp;
425 1.5 thorpej }
426 1.1 itojun err = 0;
427 1.1 itojun }
428 1.1 itojun break;
429 1.1 itojun default:
430 1.1 itojun err = EINVAL;
431 1.1 itojun break;
432 1.1 itojun }
433 1.1 itojun splx(s);
434 1.1 itojun return (err);
435 1.1 itojun }
436 1.1 itojun
437 1.1 itojun static void
438 1.6 chs netdock_start(struct ifnet *ifp)
439 1.1 itojun {
440 1.1 itojun struct netdock_softc *sc = ifp->if_softc;
441 1.1 itojun struct mbuf *m;
442 1.1 itojun
443 1.1 itojun if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
444 1.1 itojun return;
445 1.1 itojun
446 1.1 itojun while (1) {
447 1.1 itojun IF_DEQUEUE(&ifp->if_snd, m);
448 1.1 itojun if (m == 0)
449 1.1 itojun return;
450 1.1 itojun
451 1.1 itojun if ((m->m_flags & M_PKTHDR) == 0)
452 1.1 itojun panic("%s: netdock_start: no header mbuf",
453 1.17 hauke sc->sc_dev->dv_xname);
454 1.1 itojun
455 1.1 itojun #if NBPFILTER > 0
456 1.1 itojun if (ifp->if_bpf)
457 1.1 itojun bpf_mtap(ifp->if_bpf, m);
458 1.1 itojun #endif
459 1.1 itojun
460 1.1 itojun if ((netdock_put(sc, m)) == 0) {
461 1.1 itojun IF_PREPEND(&ifp->if_snd, m);
462 1.1 itojun return;
463 1.1 itojun }
464 1.1 itojun
465 1.1 itojun ifp->if_opackets++;
466 1.1 itojun }
467 1.1 itojun
468 1.1 itojun }
469 1.1 itojun
470 1.1 itojun static void
471 1.6 chs netdock_reset(struct netdock_softc *sc)
472 1.1 itojun {
473 1.1 itojun
474 1.1 itojun netdock_stop(sc);
475 1.1 itojun netdock_init(sc);
476 1.1 itojun }
477 1.1 itojun
478 1.1 itojun static int
479 1.6 chs netdock_init(struct netdock_softc *sc)
480 1.1 itojun {
481 1.1 itojun int s;
482 1.1 itojun int saveisr;
483 1.1 itojun int savetmp;
484 1.1 itojun
485 1.1 itojun if (sc->sc_if.if_flags & IFF_RUNNING)
486 1.1 itojun return (0);
487 1.1 itojun
488 1.1 itojun s = splnet();
489 1.1 itojun
490 1.1 itojun /* 0606 */
491 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
492 1.1 itojun NIC_PUT_2(sc, REG_ISR , 0);
493 1.1 itojun NIC_PUT_2(sc, REG_000E, 0);
494 1.1 itojun
495 1.1 itojun NIC_PUT_2(sc, REG_0000, 0x8104);
496 1.1 itojun NIC_PUT_2(sc, REG_0004, 0x0043);
497 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0100);
498 1.1 itojun NIC_PUT_2(sc, REG_0002, 0x6618);
499 1.1 itojun NIC_PUT_2(sc, REG_0000, 0x8010);
500 1.1 itojun
501 1.1 itojun NIC_PUT_1(sc, REG_0004 + 0, sc->sc_enaddr[0]);
502 1.1 itojun NIC_PUT_1(sc, REG_0004 + 1, sc->sc_enaddr[1]);
503 1.1 itojun NIC_PUT_1(sc, REG_0004 + 2, sc->sc_enaddr[2]);
504 1.1 itojun NIC_PUT_1(sc, REG_0004 + 3, sc->sc_enaddr[3]);
505 1.1 itojun NIC_PUT_1(sc, REG_0004 + 4, sc->sc_enaddr[4]);
506 1.1 itojun NIC_PUT_1(sc, REG_0004 + 5, sc->sc_enaddr[5]);
507 1.1 itojun
508 1.1 itojun NIC_PUT_2(sc, REG_ISR , 0x2008);
509 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
510 1.1 itojun NIC_PUT_2(sc, REG_0000, 0x4000);
511 1.1 itojun NIC_PUT_2(sc, REG_ISR, ISR_MASK);
512 1.1 itojun
513 1.1 itojun
514 1.1 itojun /* 1320 */
515 1.1 itojun saveisr = NIC_GET_2(sc, REG_ISR);
516 1.1 itojun NIC_PUT_2(sc, REG_ISR , 0);
517 1.1 itojun savetmp = NIC_GET_2(sc, REG_000E);
518 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0100);
519 1.1 itojun NIC_ANDW(sc, REG_ISR, ~ISR_BIT_03);
520 1.1 itojun NIC_PUT_2(sc, REG_000E, savetmp);
521 1.1 itojun
522 1.1 itojun /* 1382 */
523 1.1 itojun savetmp = NIC_GET_2(sc, REG_000E);
524 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0100);
525 1.1 itojun NIC_ORW(sc, REG_ISR, ISR_BIT_03);
526 1.1 itojun NIC_PUT_2(sc, REG_000E, savetmp);
527 1.1 itojun NIC_PUT_2(sc, REG_ISR , saveisr);
528 1.1 itojun
529 1.1 itojun
530 1.1 itojun sc->sc_if.if_flags |= IFF_RUNNING;
531 1.1 itojun sc->sc_if.if_flags &= ~IFF_OACTIVE;
532 1.1 itojun
533 1.1 itojun splx(s);
534 1.1 itojun return (0);
535 1.1 itojun }
536 1.1 itojun
537 1.1 itojun static int
538 1.6 chs netdock_stop(struct netdock_softc *sc)
539 1.1 itojun {
540 1.1 itojun int s = splnet();
541 1.1 itojun
542 1.1 itojun sc->sc_if.if_timer = 0;
543 1.1 itojun sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
544 1.1 itojun
545 1.1 itojun splx(s);
546 1.1 itojun return (0);
547 1.1 itojun }
548 1.1 itojun
549 1.1 itojun static void
550 1.6 chs netdock_watchdog(struct ifnet *ifp)
551 1.1 itojun {
552 1.1 itojun struct netdock_softc *sc = ifp->if_softc;
553 1.1 itojun int tmp;
554 1.1 itojun
555 1.1 itojun printf("netdock_watchdog: resetting chip\n");
556 1.1 itojun tmp = ifp->if_flags & IFF_UP;
557 1.1 itojun netdock_reset(sc);
558 1.1 itojun ifp->if_flags |= tmp;
559 1.1 itojun }
560 1.1 itojun
561 1.1 itojun static u_int
562 1.6 chs netdock_put(struct netdock_softc *sc, struct mbuf *m0)
563 1.1 itojun {
564 1.1 itojun struct mbuf *m;
565 1.1 itojun u_int totlen = 0;
566 1.1 itojun u_int tmplen;
567 1.1 itojun int isr;
568 1.1 itojun int timeout;
569 1.1 itojun int tmp;
570 1.1 itojun u_int i;
571 1.1 itojun
572 1.1 itojun for (m = m0; m; m = m->m_next)
573 1.1 itojun totlen += m->m_len;
574 1.1 itojun
575 1.1 itojun if (totlen >= ETHER_MAX_LEN)
576 1.17 hauke panic("%s: netdock_put: packet overflow",
577 1.17 hauke sc->sc_dev->dv_xname);
578 1.1 itojun
579 1.1 itojun totlen += 6;
580 1.1 itojun tmplen = totlen;
581 1.1 itojun tmplen &= 0xff00;
582 1.1 itojun tmplen |= 0x2000;
583 1.1 itojun NIC_PUT_2(sc, REG_0000, tmplen);
584 1.1 itojun
585 1.1 itojun timeout = 0x3000;
586 1.1 itojun while ((((isr = NIC_GET_2(sc, REG_ISR)) & ISR_READY) == 0) &&
587 1.1 itojun timeout--) {
588 1.1 itojun if (isr & ISR_TX)
589 1.1 itojun netdock_txint(sc);
590 1.1 itojun }
591 1.1 itojun if (timeout == 0)
592 1.1 itojun return (0);
593 1.1 itojun
594 1.1 itojun tmp = NIC_GET_2(sc, REG_0002);
595 1.1 itojun tmp <<= 8;
596 1.1 itojun NIC_PUT_2(sc, REG_0002, tmp);
597 1.1 itojun NIC_PUT_2(sc, REG_0006, 0x240);
598 1.1 itojun NIC_GET_2(sc, REG_ISR);
599 1.1 itojun tmplen = ((totlen << 8) & 0xfe00) | ((totlen >> 8) & 0x00ff);
600 1.1 itojun NIC_PUT_2(sc, REG_DATA, tmplen);
601 1.1 itojun
602 1.1 itojun for (m = m0; m; m = m->m_next) {
603 1.1 itojun u_char *data = mtod(m, u_char *);
604 1.1 itojun int len = m->m_len;
605 1.1 itojun int len4 = len >> 2;
606 1.1 itojun u_int32_t *data4 = (u_int32_t *)data;
607 1.1 itojun for (i = 0; i < len4; i++)
608 1.1 itojun NIC_PUT_4(sc, REG_DATA, data4[i]);
609 1.1 itojun for (i = len4 << 2; i < len; i++)
610 1.1 itojun NIC_PUT_1(sc, REG_DATA, data[i]);
611 1.1 itojun }
612 1.1 itojun
613 1.1 itojun if (totlen & 0x01)
614 1.1 itojun NIC_PUT_2(sc, REG_DATA, 0x2020);
615 1.1 itojun else
616 1.1 itojun NIC_PUT_2(sc, REG_DATA, 0);
617 1.1 itojun
618 1.1 itojun NIC_PUT_2(sc, REG_0000, 0xc000);
619 1.1 itojun
620 1.1 itojun m_freem(m0);
621 1.1 itojun /* sc->sc_if.if_timer = 5; */
622 1.1 itojun return (totlen);
623 1.1 itojun }
624 1.1 itojun
625 1.1 itojun void
626 1.6 chs netdock_intr(void *arg)
627 1.1 itojun {
628 1.1 itojun struct netdock_softc *sc = (struct netdock_softc *)arg;
629 1.1 itojun int isr;
630 1.1 itojun int tmp;
631 1.1 itojun
632 1.1 itojun NIC_PUT_2(sc, REG_ISR, 0);
633 1.1 itojun while ((isr = (NIC_GET_2(sc, REG_ISR) & ISR_ALL)) != 0) {
634 1.1 itojun if (isr & ISR_TX)
635 1.1 itojun netdock_txint(sc);
636 1.1 itojun
637 1.1 itojun if (isr & ISR_RX)
638 1.1 itojun netdock_rxint(sc);
639 1.1 itojun
640 1.1 itojun if (isr & (ISR_BIT_0C | ISR_BIT_0D)) {
641 1.1 itojun if (isr & ISR_BIT_0C) {
642 1.1 itojun NIC_PUT_2(sc, REG_000E, 0);
643 1.1 itojun NIC_BSET(sc, REG_0004, 0x08);
644 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
645 1.1 itojun }
646 1.1 itojun if (isr & ISR_BIT_0D) {
647 1.1 itojun NIC_PUT_2(sc, REG_000E, 0);
648 1.1 itojun tmp = NIC_GET_2(sc, REG_0002);
649 1.1 itojun if (tmp & REG_0002_BIT_04)
650 1.1 itojun NIC_GET_2(sc, REG_0006);
651 1.1 itojun tmp = NIC_GET_2(sc, REG_0000);
652 1.1 itojun if (tmp & REG_0000_BIT_08)
653 1.1 itojun NIC_BSET(sc, REG_0000, 0x08);
654 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
655 1.1 itojun }
656 1.1 itojun NIC_PUT_2(sc, REG_ISR, isr);
657 1.1 itojun }
658 1.1 itojun }
659 1.1 itojun NIC_PUT_2(sc, REG_ISR, ISR_MASK);
660 1.1 itojun }
661 1.1 itojun
662 1.1 itojun static void
663 1.6 chs netdock_txint(struct netdock_softc *sc)
664 1.1 itojun {
665 1.1 itojun struct ifnet *ifp = &sc->sc_if;
666 1.1 itojun int savereg0002;
667 1.1 itojun int reg0004;
668 1.1 itojun int regdata;
669 1.1 itojun
670 1.1 itojun ifp->if_flags &= ~IFF_OACTIVE;
671 1.1 itojun ifp->if_timer = 0;
672 1.1 itojun
673 1.1 itojun savereg0002 = NIC_GET_2(sc, REG_0002);
674 1.1 itojun
675 1.1 itojun while (((reg0004 = NIC_GET_2(sc, REG_0004)) & REG_0004_BIT_0F) == 0) {
676 1.1 itojun NIC_PUT_2(sc, REG_0002, reg0004);
677 1.1 itojun NIC_PUT_2(sc, REG_0006, 0x0060);
678 1.1 itojun NIC_GET_2(sc, REG_ISR);
679 1.1 itojun regdata = NIC_GET_2(sc, REG_DATA);
680 1.1 itojun if ((regdata & REG_DATA_BIT_08) == 0) {
681 1.1 itojun /* ifp->if_collisions++; */
682 1.1 itojun if (regdata & REG_DATA_BIT_07)
683 1.1 itojun /* ifp->if_oerrors++; */
684 1.1 itojun NIC_PUT_2(sc, REG_000E, 0);
685 1.1 itojun NIC_ORW(sc, REG_0000, 0x0100);
686 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
687 1.1 itojun }
688 1.1 itojun NIC_GET_2(sc ,REG_DATA);
689 1.1 itojun
690 1.1 itojun if (regdata & REG_DATA_BIT_0F)
691 1.1 itojun NIC_GET_4(sc, REG_EFD00);
692 1.1 itojun NIC_PUT_2(sc, REG_0000, 0xa000);
693 1.1 itojun NIC_PUT_2(sc, REG_ISR, ISR_TX);
694 1.1 itojun }
695 1.1 itojun
696 1.1 itojun NIC_PUT_2(sc, REG_0002, savereg0002);
697 1.1 itojun NIC_PUT_2(sc, REG_000E, 0);
698 1.1 itojun NIC_GET_2(sc, REG_0006);
699 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
700 1.1 itojun NIC_PUT_2(sc, REG_0006, 0);
701 1.1 itojun }
702 1.1 itojun
703 1.1 itojun static void
704 1.6 chs netdock_rxint(struct netdock_softc *sc)
705 1.1 itojun {
706 1.1 itojun struct ifnet *ifp = &sc->sc_if;
707 1.1 itojun int regdata1;
708 1.1 itojun int regdata2;
709 1.1 itojun u_int len;
710 1.1 itojun int timeout;
711 1.1 itojun
712 1.1 itojun while ((NIC_GET_2(sc, REG_0004) & REG_0004_BIT_07) == 0) {
713 1.1 itojun NIC_GET_2(sc, REG_ISR);
714 1.1 itojun NIC_PUT_2(sc, REG_0006, 0x00e0);
715 1.1 itojun NIC_GET_2(sc, REG_ISR);
716 1.1 itojun regdata1 = NIC_GET_2(sc, REG_DATA);
717 1.1 itojun regdata2 = NIC_GET_2(sc, REG_DATA);
718 1.1 itojun len = ((regdata2 << 8) & 0x0700) | ((regdata2 >> 8) & 0x00ff);
719 1.1 itojun
720 1.1 itojun #if 0
721 1.1 itojun printf("netdock_rxint: r1=0x%04x, r2=0x%04x, len=%d\n",
722 1.1 itojun regdata1, regdata2, len);
723 1.1 itojun #endif
724 1.1 itojun
725 1.1 itojun if ((regdata1 & REG_DATA_BIT_04) == 0)
726 1.1 itojun len -= 2;
727 1.1 itojun
728 1.7 thorpej /* CRC is included with the packet; trim it off. */
729 1.7 thorpej len -= ETHER_CRC_LEN;
730 1.7 thorpej
731 1.1 itojun if ((regdata1 & 0x00ac) == 0) {
732 1.1 itojun if (netdock_read(sc, len))
733 1.1 itojun ifp->if_ipackets++;
734 1.1 itojun else
735 1.1 itojun ifp->if_ierrors++;
736 1.1 itojun } else {
737 1.1 itojun ifp->if_ierrors++;
738 1.1 itojun
739 1.1 itojun if (regdata1 & REG_DATA_BIT_02)
740 1.1 itojun NIC_GET_4(sc, REG_EFD00);
741 1.1 itojun if (regdata1 & REG_DATA_BIT_03)
742 1.1 itojun ;
743 1.1 itojun if (regdata1 & REG_DATA_BIT_05)
744 1.1 itojun NIC_GET_4(sc, REG_EFD00);
745 1.1 itojun if (regdata1 & REG_DATA_BIT_07)
746 1.1 itojun ;
747 1.1 itojun }
748 1.1 itojun
749 1.1 itojun timeout = 0x14;
750 1.1 itojun while ((NIC_GET_2(sc, REG_0000) & REG_0000_BIT_08) && timeout--)
751 1.1 itojun ;
752 1.1 itojun if (timeout == 0)
753 1.1 itojun ;
754 1.1 itojun
755 1.1 itojun NIC_PUT_2(sc, REG_0000, 0x8000);
756 1.1 itojun }
757 1.1 itojun NIC_PUT_2(sc, REG_0006, 0);
758 1.1 itojun }
759 1.1 itojun
760 1.1 itojun static int
761 1.6 chs netdock_read(struct netdock_softc *sc, int len)
762 1.1 itojun {
763 1.1 itojun struct ifnet *ifp = &sc->sc_if;
764 1.1 itojun struct mbuf *m;
765 1.1 itojun
766 1.1 itojun m = netdock_get(sc, len);
767 1.1 itojun if (m == 0)
768 1.1 itojun return (0);
769 1.1 itojun
770 1.1 itojun #if NBPFILTER > 0
771 1.1 itojun if (ifp->if_bpf)
772 1.1 itojun bpf_mtap(ifp->if_bpf, m);
773 1.1 itojun #endif
774 1.1 itojun
775 1.1 itojun (*ifp->if_input)(ifp, m);
776 1.1 itojun
777 1.1 itojun return (1);
778 1.1 itojun }
779 1.1 itojun
780 1.1 itojun static struct mbuf *
781 1.6 chs netdock_get(struct netdock_softc *sc, int datalen)
782 1.1 itojun {
783 1.1 itojun struct mbuf *m, *top, **mp;
784 1.1 itojun u_char *data;
785 1.1 itojun int i;
786 1.1 itojun int len;
787 1.1 itojun int len4;
788 1.1 itojun u_int32_t *data4;
789 1.1 itojun
790 1.1 itojun MGETHDR(m, M_DONTWAIT, MT_DATA);
791 1.1 itojun if (m == NULL)
792 1.1 itojun return (NULL);
793 1.1 itojun m->m_pkthdr.rcvif = &sc->sc_if;
794 1.1 itojun m->m_pkthdr.len = datalen;
795 1.1 itojun len = MHLEN;
796 1.1 itojun top = NULL;
797 1.1 itojun mp = ⊤
798 1.1 itojun
799 1.1 itojun while (datalen > 0) {
800 1.1 itojun if (top) {
801 1.1 itojun MGET(m, M_DONTWAIT, MT_DATA);
802 1.1 itojun if (m == 0) {
803 1.1 itojun m_freem(top);
804 1.1 itojun return (NULL);
805 1.1 itojun }
806 1.1 itojun len = MLEN;
807 1.1 itojun }
808 1.1 itojun if (datalen >= MINCLSIZE) {
809 1.1 itojun MCLGET(m, M_DONTWAIT);
810 1.1 itojun if ((m->m_flags & M_EXT) == 0) {
811 1.1 itojun if (top)
812 1.1 itojun m_freem(top);
813 1.1 itojun return (NULL);
814 1.1 itojun }
815 1.1 itojun len = MCLBYTES;
816 1.1 itojun }
817 1.1 itojun
818 1.1 itojun if (mp == &top) {
819 1.13 he char *newdata = (char *)
820 1.1 itojun ALIGN(m->m_data + sizeof(struct ether_header)) -
821 1.1 itojun sizeof(struct ether_header);
822 1.1 itojun len -= newdata - m->m_data;
823 1.1 itojun m->m_data = newdata;
824 1.1 itojun }
825 1.1 itojun
826 1.1 itojun m->m_len = len = min(datalen, len);
827 1.1 itojun
828 1.1 itojun data = mtod(m, u_char *);
829 1.1 itojun len4 = len >> 2;
830 1.1 itojun data4 = (u_int32_t *)data;
831 1.1 itojun for (i = 0; i < len4; i++)
832 1.1 itojun data4[i] = NIC_GET_4(sc, REG_DATA);
833 1.1 itojun for (i = len4 << 2; i < len; i++)
834 1.1 itojun data[i] = NIC_GET_1(sc, REG_DATA);
835 1.1 itojun
836 1.1 itojun datalen -= len;
837 1.1 itojun *mp = m;
838 1.1 itojun mp = &m->m_next;
839 1.1 itojun }
840 1.1 itojun
841 1.1 itojun return (top);
842 1.1 itojun }
843