if_netdock_nubus.c revision 1.22 1 1.22 chs /* $NetBSD: if_netdock_nubus.c,v 1.22 2012/10/27 17:17:59 chs 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.22 chs __KERNEL_RCSID(0, "$NetBSD: if_netdock_nubus.c,v 1.22 2012/10/27 17:17:59 chs 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 <net/bpf.h>
66 1.1 itojun
67 1.1 itojun #include <machine/bus.h>
68 1.1 itojun #include <machine/viareg.h>
69 1.1 itojun #include <mac68k/nubus/nubus.h>
70 1.1 itojun
71 1.1 itojun /***********************/
72 1.1 itojun
73 1.1 itojun #define NETDOCK_DEBUG
74 1.1 itojun
75 1.1 itojun #define NETDOCK_NUBUS_CATEGORY 0x0020
76 1.1 itojun #define NETDOCK_NUBUS_TYPE 0x0003
77 1.1 itojun #define NETDOCK_NUBUS_DRSW 0x0103
78 1.1 itojun #define NETDOCK_NUBUS_DRHW 0x0100
79 1.1 itojun
80 1.1 itojun #define ETHERMICRODOCK_NUBUS_CATEGORY 0x0020
81 1.1 itojun #define ETHERMICRODOCK_NUBUS_TYPE 0x0003
82 1.1 itojun #define ETHERMICRODOCK_NUBUS_DRSW 0x0102
83 1.1 itojun #define ETHERMICRODOCK_NUBUS_DRHW 0x0100
84 1.1 itojun
85 1.1 itojun #define REG_ISR 0x000c
86 1.1 itojun #define REG_000E 0x000e
87 1.1 itojun #define REG_0000 0x0000
88 1.1 itojun #define REG_0002 0x0002
89 1.1 itojun #define REG_0004 0x0004
90 1.1 itojun #define REG_0006 0x0006
91 1.1 itojun #define REG_DATA 0x0008
92 1.1 itojun #define REG_EFD00 0xefd00
93 1.1 itojun
94 1.1 itojun #define ISR_ALL 0x3300
95 1.1 itojun #define ISR_TX 0x0200
96 1.1 itojun #define ISR_RX 0x0100
97 1.1 itojun #define ISR_READY 0x0800
98 1.1 itojun #define ISR_BIT_0C 0x1000
99 1.1 itojun #define ISR_BIT_0D 0x2000
100 1.1 itojun #define ISR_MASK 0x0033
101 1.1 itojun #define ISR_BIT_03 0x0008
102 1.1 itojun
103 1.1 itojun #define REG_0002_BIT_04 0x0010
104 1.1 itojun #define REG_0000_BIT_08 0x0100
105 1.1 itojun #define REG_0004_BIT_0F 0x8000
106 1.1 itojun #define REG_0004_BIT_07 0x0080
107 1.1 itojun #define REG_DATA_BIT_02 0x0004
108 1.1 itojun #define REG_DATA_BIT_03 0x0008
109 1.1 itojun #define REG_DATA_BIT_04 0x0010
110 1.1 itojun #define REG_DATA_BIT_05 0x0020
111 1.1 itojun #define REG_DATA_BIT_08 0x0100
112 1.1 itojun #define REG_DATA_BIT_07 0x0080
113 1.1 itojun #define REG_DATA_BIT_0F 0x8000
114 1.1 itojun
115 1.1 itojun /***********************/
116 1.1 itojun
117 1.1 itojun typedef struct netdock_softc {
118 1.18 hauke device_t sc_dev;
119 1.1 itojun struct ethercom sc_ethercom;
120 1.1 itojun #define sc_if sc_ethercom.ec_if
121 1.1 itojun
122 1.1 itojun bus_space_tag_t sc_regt;
123 1.1 itojun bus_space_handle_t sc_regh;
124 1.1 itojun
125 1.1 itojun u_int8_t sc_enaddr[ETHER_ADDR_LEN];
126 1.1 itojun
127 1.1 itojun } netdock_softc_t;
128 1.1 itojun
129 1.1 itojun /***********************/
130 1.1 itojun
131 1.22 chs static int netdock_nubus_match(device_t, cfdata_t, void *);
132 1.22 chs static void netdock_nubus_attach(device_t, device_t, void *);
133 1.6 chs static int netdock_nb_get_enaddr(bus_space_tag_t, bus_space_handle_t,
134 1.6 chs struct nubus_attach_args *, u_int8_t *);
135 1.1 itojun #ifdef NETDOCK_DEBUG_DRIVER
136 1.6 chs static void netdock_print_driver(bus_space_tag_t, bus_space_handle_t,
137 1.6 chs struct nubus_attach_args *);
138 1.1 itojun #endif
139 1.1 itojun
140 1.6 chs int netdock_setup(struct netdock_softc *, u_int8_t *);
141 1.6 chs void netdock_intr(void *);
142 1.1 itojun
143 1.6 chs static void netdock_watchdog(struct ifnet *);
144 1.6 chs static int netdock_init(struct netdock_softc *);
145 1.6 chs static int netdock_stop(struct netdock_softc *);
146 1.12 christos static int netdock_ioctl(struct ifnet *, u_long, void *);
147 1.6 chs static void netdock_start(struct ifnet *);
148 1.6 chs static void netdock_reset(struct netdock_softc *);
149 1.6 chs static void netdock_txint(struct netdock_softc *);
150 1.6 chs static void netdock_rxint(struct netdock_softc *);
151 1.6 chs
152 1.6 chs static u_int netdock_put(struct netdock_softc *, struct mbuf *);
153 1.6 chs static int netdock_read(struct netdock_softc *, int);
154 1.6 chs static struct mbuf *netdock_get(struct netdock_softc *, int);
155 1.1 itojun
156 1.1 itojun /***********************/
157 1.1 itojun
158 1.1 itojun #define NIC_GET_1(sc, o) (bus_space_read_1((sc)->sc_regt, \
159 1.1 itojun (sc)->sc_regh, (o)))
160 1.1 itojun #define NIC_PUT_1(sc, o, val) (bus_space_write_1((sc)->sc_regt, \
161 1.1 itojun (sc)->sc_regh, (o), (val)))
162 1.1 itojun #define NIC_GET_2(sc, o) (bus_space_read_2((sc)->sc_regt, \
163 1.1 itojun (sc)->sc_regh, (o)))
164 1.1 itojun #define NIC_PUT_2(sc, o, val) (bus_space_write_2((sc)->sc_regt, \
165 1.1 itojun (sc)->sc_regh, (o), (val)))
166 1.1 itojun #define NIC_GET_4(sc, o) (bus_space_read_4((sc)->sc_regt, \
167 1.1 itojun (sc)->sc_regh, (o)))
168 1.1 itojun #define NIC_PUT_4(sc, o, val) (bus_space_write_4((sc)->sc_regt, \
169 1.1 itojun (sc)->sc_regh, (o), (val)))
170 1.1 itojun
171 1.1 itojun #define NIC_BSET(sc, o, b) \
172 1.11 perry __asm volatile("bset %0,%1" : : "di" ((u_short)(b)), \
173 1.1 itojun "g" (*(u_int8_t *)((sc)->sc_regh.base + (o))))
174 1.1 itojun #define NIC_BCLR(sc, o, b) \
175 1.11 perry __asm volatile("bclr %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_ANDW(sc, o, b) \
178 1.11 perry __asm volatile("andw %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_ORW(sc, o, b) \
181 1.11 perry __asm volatile("orw %0,%1" : : "di" ((u_short)(b)), \
182 1.1 itojun "g" (*(u_int8_t *)((sc)->sc_regh.base + (o))))
183 1.1 itojun
184 1.1 itojun
185 1.1 itojun /***********************/
186 1.1 itojun
187 1.22 chs CFATTACH_DECL_NEW(netdock_nubus, sizeof(struct netdock_softc),
188 1.3 thorpej netdock_nubus_match, netdock_nubus_attach, NULL, NULL);
189 1.1 itojun
190 1.1 itojun /***********************/
191 1.1 itojun
192 1.1 itojun static int
193 1.22 chs netdock_nubus_match(device_t parent, cfdata_t cf, void *aux)
194 1.1 itojun {
195 1.1 itojun struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
196 1.1 itojun bus_space_handle_t bsh;
197 1.1 itojun int rv;
198 1.1 itojun
199 1.1 itojun if (bus_space_map(na->na_tag, NUBUS_SLOT2PA(na->slot),
200 1.1 itojun NBMEMSIZE, 0, &bsh))
201 1.1 itojun return (0);
202 1.1 itojun
203 1.1 itojun rv = 0;
204 1.1 itojun
205 1.1 itojun if (na->category == NETDOCK_NUBUS_CATEGORY &&
206 1.1 itojun na->type == NETDOCK_NUBUS_TYPE &&
207 1.1 itojun na->drsw == NETDOCK_NUBUS_DRSW) {
208 1.1 itojun /* assuming this IS Asante NetDock */
209 1.1 itojun rv = 1;
210 1.1 itojun }
211 1.1 itojun
212 1.1 itojun if (na->category == ETHERMICRODOCK_NUBUS_CATEGORY &&
213 1.1 itojun na->type == ETHERMICRODOCK_NUBUS_TYPE &&
214 1.1 itojun na->drsw == ETHERMICRODOCK_NUBUS_DRSW) {
215 1.1 itojun /* assuming this IS Newer EtherMicroDock */
216 1.1 itojun rv = 1;
217 1.1 itojun }
218 1.1 itojun
219 1.1 itojun bus_space_unmap(na->na_tag, bsh, NBMEMSIZE);
220 1.1 itojun
221 1.1 itojun return rv;
222 1.1 itojun }
223 1.1 itojun
224 1.1 itojun static void
225 1.22 chs netdock_nubus_attach(device_t parent, device_t self, void *aux)
226 1.1 itojun {
227 1.22 chs struct netdock_softc *sc = device_private(self);
228 1.1 itojun struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
229 1.1 itojun bus_space_tag_t bst;
230 1.1 itojun bus_space_handle_t bsh;
231 1.1 itojun u_int8_t enaddr[ETHER_ADDR_LEN];
232 1.8 rjs const char *cardtype;
233 1.1 itojun
234 1.1 itojun bst = na->na_tag;
235 1.1 itojun if (bus_space_map(bst, NUBUS_SLOT2PA(na->slot), NBMEMSIZE, 0, &bsh)) {
236 1.1 itojun printf(": failed to map memory space.\n");
237 1.1 itojun return;
238 1.1 itojun }
239 1.1 itojun
240 1.1 itojun sc->sc_regt = bst;
241 1.22 chs sc->sc_dev = self;
242 1.1 itojun cardtype = nubus_get_card_name(bst, bsh, na->fmt);
243 1.1 itojun
244 1.1 itojun #ifdef NETDOCK_DEBUG_DRIVER
245 1.1 itojun netdock_print_driver(bst, bsh, na);
246 1.1 itojun #endif
247 1.1 itojun
248 1.1 itojun if (netdock_nb_get_enaddr(bst, bsh, na, enaddr)) {
249 1.1 itojun printf(": can't find MAC address.\n");
250 1.1 itojun bus_space_unmap(bst, bsh, NBMEMSIZE);
251 1.1 itojun return;
252 1.1 itojun }
253 1.1 itojun
254 1.1 itojun if (bus_space_subregion(bst, bsh, 0xe00300, 0xf0000, &sc->sc_regh)) {
255 1.1 itojun printf(": failed to map register space.\n");
256 1.1 itojun bus_space_unmap(bst, bsh, NBMEMSIZE);
257 1.1 itojun return;
258 1.1 itojun }
259 1.1 itojun
260 1.1 itojun printf(": %s\n", cardtype);
261 1.1 itojun
262 1.1 itojun if (netdock_setup(sc, enaddr)) {
263 1.1 itojun bus_space_unmap(bst, bsh, NBMEMSIZE);
264 1.1 itojun return;
265 1.1 itojun }
266 1.1 itojun
267 1.1 itojun add_nubus_intr(na->slot, netdock_intr, (void *)sc);
268 1.1 itojun
269 1.1 itojun return;
270 1.1 itojun }
271 1.1 itojun
272 1.1 itojun static int
273 1.6 chs netdock_nb_get_enaddr(bus_space_tag_t bst, bus_space_handle_t bsh,
274 1.6 chs struct nubus_attach_args *na, u_int8_t *ep)
275 1.1 itojun {
276 1.1 itojun nubus_dir dir;
277 1.1 itojun nubus_dirent dirent;
278 1.1 itojun
279 1.1 itojun /*
280 1.1 itojun * these hardwired resource IDs are only for NetDock
281 1.1 itojun */
282 1.1 itojun nubus_get_main_dir(na->fmt, &dir);
283 1.1 itojun if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x81, &dirent) <= 0)
284 1.1 itojun return 1;
285 1.1 itojun nubus_get_dir_from_rsrc(na->fmt, &dirent, &dir);
286 1.1 itojun if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x80, &dirent) <= 0)
287 1.1 itojun return 1;
288 1.1 itojun if (nubus_get_ind_data(bst, bsh, na->fmt, &dirent,
289 1.1 itojun ep, ETHER_ADDR_LEN) <= 0)
290 1.1 itojun return 1;
291 1.1 itojun
292 1.1 itojun return 0;
293 1.1 itojun }
294 1.1 itojun
295 1.1 itojun #ifdef NETDOCK_DEBUG_DRIVER
296 1.1 itojun static void
297 1.6 chs netdock_print_driver(bus_space_tag_t bst, bus_space_handle_t bsh,
298 1.6 chs struct nubus_attach_args *na)
299 1.1 itojun {
300 1.1 itojun #define HEADSIZE (8+4)
301 1.1 itojun #define CODESIZE (6759-4)
302 1.1 itojun unsigned char mydata[HEADSIZE + CODESIZE];
303 1.1 itojun nubus_dir dir;
304 1.1 itojun nubus_dirent dirent;
305 1.1 itojun int i, rv;
306 1.1 itojun
307 1.1 itojun nubus_get_main_dir(na->fmt, &dir);
308 1.1 itojun rv = nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x81, &dirent);
309 1.1 itojun if (rv <= 0) {
310 1.1 itojun printf(": can't find sResource.\n");
311 1.1 itojun return;
312 1.1 itojun }
313 1.1 itojun nubus_get_dir_from_rsrc(na->fmt, &dirent, &dir);
314 1.1 itojun if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, NUBUS_RSRC_DRVRDIR,
315 1.1 itojun &dirent) <= 0) {
316 1.1 itojun printf(": can't find sResource.\n");
317 1.1 itojun return;
318 1.1 itojun }
319 1.1 itojun if (nubus_get_ind_data(bst, bsh, na->fmt,
320 1.1 itojun &dirent, mydata, HEADSIZE+CODESIZE) <= 0) {
321 1.1 itojun printf(": can't find indirect data.\n");
322 1.1 itojun return;
323 1.1 itojun }
324 1.1 itojun printf("\n########## begin driver dir");
325 1.1 itojun for (i = 0; i < HEADSIZE; i++) {
326 1.1 itojun if (i % 16 == 0)
327 1.1 itojun printf("\n%02x:",i);
328 1.1 itojun printf(" %02x", mydata[i]);
329 1.1 itojun }
330 1.1 itojun printf("\n########## begin driver code");
331 1.1 itojun for (i = 0; i < CODESIZE; i++) {
332 1.1 itojun if (i % 16 == 0)
333 1.1 itojun printf("\n%02x:",i);
334 1.1 itojun printf(" %02x", mydata[i + HEADSIZE]);
335 1.1 itojun }
336 1.1 itojun #if 0
337 1.1 itojun printf("\n########## begin driver code (partial)\n");
338 1.1 itojun #define PARTSIZE 256
339 1.1 itojun #define OFFSET 0x1568
340 1.1 itojun for (i = OFFSET; i < OFFSET + PARTSIZE; i++) {
341 1.1 itojun if ((i - OFFSET) % 16 == 0)
342 1.1 itojun printf("\n%02x:",i);
343 1.1 itojun printf(" %02x", mydata[i + HEADSIZE]);
344 1.1 itojun }
345 1.1 itojun #endif
346 1.1 itojun printf("\n########## end\n");
347 1.1 itojun }
348 1.1 itojun #endif
349 1.1 itojun
350 1.1 itojun
351 1.1 itojun int
352 1.6 chs netdock_setup(struct netdock_softc *sc, u_int8_t *lladdr)
353 1.1 itojun {
354 1.1 itojun struct ifnet *ifp = &sc->sc_if;
355 1.1 itojun
356 1.6 chs memcpy(sc->sc_enaddr, lladdr, ETHER_ADDR_LEN);
357 1.1 itojun printf("%s: Ethernet address %s\n",
358 1.22 chs device_xname(sc->sc_dev), ether_sprintf(lladdr));
359 1.1 itojun
360 1.18 hauke memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
361 1.1 itojun ifp->if_softc = sc;
362 1.1 itojun ifp->if_ioctl = netdock_ioctl;
363 1.1 itojun ifp->if_start = netdock_start;
364 1.1 itojun ifp->if_flags =
365 1.1 itojun IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
366 1.1 itojun ifp->if_watchdog = netdock_watchdog;
367 1.1 itojun
368 1.1 itojun if_attach(ifp);
369 1.1 itojun ether_ifattach(ifp, lladdr);
370 1.1 itojun
371 1.1 itojun return (0);
372 1.1 itojun }
373 1.1 itojun
374 1.1 itojun static int
375 1.12 christos netdock_ioctl(struct ifnet *ifp, u_long cmd, void *data)
376 1.1 itojun {
377 1.1 itojun struct ifaddr *ifa;
378 1.1 itojun struct netdock_softc *sc = ifp->if_softc;
379 1.1 itojun int s = splnet();
380 1.1 itojun int err = 0;
381 1.1 itojun int temp;
382 1.1 itojun
383 1.1 itojun switch (cmd) {
384 1.19 dyoung case SIOCINITIFADDR:
385 1.1 itojun ifa = (struct ifaddr *)data;
386 1.1 itojun ifp->if_flags |= IFF_UP;
387 1.19 dyoung (void)netdock_init(sc);
388 1.1 itojun switch (ifa->ifa_addr->sa_family) {
389 1.1 itojun #ifdef INET
390 1.1 itojun case AF_INET:
391 1.1 itojun arp_ifinit(ifp, ifa);
392 1.1 itojun break;
393 1.1 itojun #endif
394 1.1 itojun default:
395 1.1 itojun break;
396 1.1 itojun }
397 1.1 itojun break;
398 1.1 itojun
399 1.1 itojun case SIOCSIFFLAGS:
400 1.19 dyoung if ((err = ifioctl_common(ifp, cmd, data)) != 0)
401 1.19 dyoung break;
402 1.19 dyoung /* XXX see the comment in ed_ioctl() about code re-use */
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.19 dyoung err = ether_ioctl(ifp, cmd, data);
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.18 hauke device_xname(sc->sc_dev));
454 1.1 itojun
455 1.21 joerg bpf_mtap(ifp, m);
456 1.1 itojun
457 1.1 itojun if ((netdock_put(sc, m)) == 0) {
458 1.1 itojun IF_PREPEND(&ifp->if_snd, m);
459 1.1 itojun return;
460 1.1 itojun }
461 1.1 itojun
462 1.1 itojun ifp->if_opackets++;
463 1.1 itojun }
464 1.1 itojun
465 1.1 itojun }
466 1.1 itojun
467 1.1 itojun static void
468 1.6 chs netdock_reset(struct netdock_softc *sc)
469 1.1 itojun {
470 1.1 itojun
471 1.1 itojun netdock_stop(sc);
472 1.1 itojun netdock_init(sc);
473 1.1 itojun }
474 1.1 itojun
475 1.1 itojun static int
476 1.6 chs netdock_init(struct netdock_softc *sc)
477 1.1 itojun {
478 1.1 itojun int s;
479 1.1 itojun int saveisr;
480 1.1 itojun int savetmp;
481 1.1 itojun
482 1.1 itojun if (sc->sc_if.if_flags & IFF_RUNNING)
483 1.1 itojun return (0);
484 1.1 itojun
485 1.1 itojun s = splnet();
486 1.1 itojun
487 1.1 itojun /* 0606 */
488 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
489 1.1 itojun NIC_PUT_2(sc, REG_ISR , 0);
490 1.1 itojun NIC_PUT_2(sc, REG_000E, 0);
491 1.1 itojun
492 1.1 itojun NIC_PUT_2(sc, REG_0000, 0x8104);
493 1.1 itojun NIC_PUT_2(sc, REG_0004, 0x0043);
494 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0100);
495 1.1 itojun NIC_PUT_2(sc, REG_0002, 0x6618);
496 1.1 itojun NIC_PUT_2(sc, REG_0000, 0x8010);
497 1.1 itojun
498 1.1 itojun NIC_PUT_1(sc, REG_0004 + 0, sc->sc_enaddr[0]);
499 1.1 itojun NIC_PUT_1(sc, REG_0004 + 1, sc->sc_enaddr[1]);
500 1.1 itojun NIC_PUT_1(sc, REG_0004 + 2, sc->sc_enaddr[2]);
501 1.1 itojun NIC_PUT_1(sc, REG_0004 + 3, sc->sc_enaddr[3]);
502 1.1 itojun NIC_PUT_1(sc, REG_0004 + 4, sc->sc_enaddr[4]);
503 1.1 itojun NIC_PUT_1(sc, REG_0004 + 5, sc->sc_enaddr[5]);
504 1.1 itojun
505 1.1 itojun NIC_PUT_2(sc, REG_ISR , 0x2008);
506 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
507 1.1 itojun NIC_PUT_2(sc, REG_0000, 0x4000);
508 1.1 itojun NIC_PUT_2(sc, REG_ISR, ISR_MASK);
509 1.1 itojun
510 1.1 itojun
511 1.1 itojun /* 1320 */
512 1.1 itojun saveisr = NIC_GET_2(sc, REG_ISR);
513 1.1 itojun NIC_PUT_2(sc, REG_ISR , 0);
514 1.1 itojun savetmp = NIC_GET_2(sc, REG_000E);
515 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0100);
516 1.1 itojun NIC_ANDW(sc, REG_ISR, ~ISR_BIT_03);
517 1.1 itojun NIC_PUT_2(sc, REG_000E, savetmp);
518 1.1 itojun
519 1.1 itojun /* 1382 */
520 1.1 itojun savetmp = NIC_GET_2(sc, REG_000E);
521 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0100);
522 1.1 itojun NIC_ORW(sc, REG_ISR, ISR_BIT_03);
523 1.1 itojun NIC_PUT_2(sc, REG_000E, savetmp);
524 1.1 itojun NIC_PUT_2(sc, REG_ISR , saveisr);
525 1.1 itojun
526 1.1 itojun
527 1.1 itojun sc->sc_if.if_flags |= IFF_RUNNING;
528 1.1 itojun sc->sc_if.if_flags &= ~IFF_OACTIVE;
529 1.1 itojun
530 1.1 itojun splx(s);
531 1.1 itojun return (0);
532 1.1 itojun }
533 1.1 itojun
534 1.1 itojun static int
535 1.6 chs netdock_stop(struct netdock_softc *sc)
536 1.1 itojun {
537 1.1 itojun int s = splnet();
538 1.1 itojun
539 1.1 itojun sc->sc_if.if_timer = 0;
540 1.1 itojun sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
541 1.1 itojun
542 1.1 itojun splx(s);
543 1.1 itojun return (0);
544 1.1 itojun }
545 1.1 itojun
546 1.1 itojun static void
547 1.6 chs netdock_watchdog(struct ifnet *ifp)
548 1.1 itojun {
549 1.1 itojun struct netdock_softc *sc = ifp->if_softc;
550 1.1 itojun int tmp;
551 1.1 itojun
552 1.1 itojun printf("netdock_watchdog: resetting chip\n");
553 1.1 itojun tmp = ifp->if_flags & IFF_UP;
554 1.1 itojun netdock_reset(sc);
555 1.1 itojun ifp->if_flags |= tmp;
556 1.1 itojun }
557 1.1 itojun
558 1.1 itojun static u_int
559 1.6 chs netdock_put(struct netdock_softc *sc, struct mbuf *m0)
560 1.1 itojun {
561 1.1 itojun struct mbuf *m;
562 1.1 itojun u_int totlen = 0;
563 1.1 itojun u_int tmplen;
564 1.1 itojun int isr;
565 1.1 itojun int timeout;
566 1.1 itojun int tmp;
567 1.1 itojun u_int i;
568 1.1 itojun
569 1.1 itojun for (m = m0; m; m = m->m_next)
570 1.1 itojun totlen += m->m_len;
571 1.1 itojun
572 1.1 itojun if (totlen >= ETHER_MAX_LEN)
573 1.17 hauke panic("%s: netdock_put: packet overflow",
574 1.18 hauke device_xname(sc->sc_dev));
575 1.1 itojun
576 1.1 itojun totlen += 6;
577 1.1 itojun tmplen = totlen;
578 1.1 itojun tmplen &= 0xff00;
579 1.1 itojun tmplen |= 0x2000;
580 1.1 itojun NIC_PUT_2(sc, REG_0000, tmplen);
581 1.1 itojun
582 1.1 itojun timeout = 0x3000;
583 1.1 itojun while ((((isr = NIC_GET_2(sc, REG_ISR)) & ISR_READY) == 0) &&
584 1.1 itojun timeout--) {
585 1.1 itojun if (isr & ISR_TX)
586 1.1 itojun netdock_txint(sc);
587 1.1 itojun }
588 1.1 itojun if (timeout == 0)
589 1.1 itojun return (0);
590 1.1 itojun
591 1.1 itojun tmp = NIC_GET_2(sc, REG_0002);
592 1.1 itojun tmp <<= 8;
593 1.1 itojun NIC_PUT_2(sc, REG_0002, tmp);
594 1.1 itojun NIC_PUT_2(sc, REG_0006, 0x240);
595 1.1 itojun NIC_GET_2(sc, REG_ISR);
596 1.1 itojun tmplen = ((totlen << 8) & 0xfe00) | ((totlen >> 8) & 0x00ff);
597 1.1 itojun NIC_PUT_2(sc, REG_DATA, tmplen);
598 1.1 itojun
599 1.1 itojun for (m = m0; m; m = m->m_next) {
600 1.1 itojun u_char *data = mtod(m, u_char *);
601 1.1 itojun int len = m->m_len;
602 1.1 itojun int len4 = len >> 2;
603 1.1 itojun u_int32_t *data4 = (u_int32_t *)data;
604 1.1 itojun for (i = 0; i < len4; i++)
605 1.1 itojun NIC_PUT_4(sc, REG_DATA, data4[i]);
606 1.1 itojun for (i = len4 << 2; i < len; i++)
607 1.1 itojun NIC_PUT_1(sc, REG_DATA, data[i]);
608 1.1 itojun }
609 1.1 itojun
610 1.1 itojun if (totlen & 0x01)
611 1.1 itojun NIC_PUT_2(sc, REG_DATA, 0x2020);
612 1.1 itojun else
613 1.1 itojun NIC_PUT_2(sc, REG_DATA, 0);
614 1.1 itojun
615 1.1 itojun NIC_PUT_2(sc, REG_0000, 0xc000);
616 1.1 itojun
617 1.1 itojun m_freem(m0);
618 1.1 itojun /* sc->sc_if.if_timer = 5; */
619 1.1 itojun return (totlen);
620 1.1 itojun }
621 1.1 itojun
622 1.1 itojun void
623 1.6 chs netdock_intr(void *arg)
624 1.1 itojun {
625 1.1 itojun struct netdock_softc *sc = (struct netdock_softc *)arg;
626 1.1 itojun int isr;
627 1.1 itojun int tmp;
628 1.1 itojun
629 1.1 itojun NIC_PUT_2(sc, REG_ISR, 0);
630 1.1 itojun while ((isr = (NIC_GET_2(sc, REG_ISR) & ISR_ALL)) != 0) {
631 1.1 itojun if (isr & ISR_TX)
632 1.1 itojun netdock_txint(sc);
633 1.1 itojun
634 1.1 itojun if (isr & ISR_RX)
635 1.1 itojun netdock_rxint(sc);
636 1.1 itojun
637 1.1 itojun if (isr & (ISR_BIT_0C | ISR_BIT_0D)) {
638 1.1 itojun if (isr & ISR_BIT_0C) {
639 1.1 itojun NIC_PUT_2(sc, REG_000E, 0);
640 1.1 itojun NIC_BSET(sc, REG_0004, 0x08);
641 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
642 1.1 itojun }
643 1.1 itojun if (isr & ISR_BIT_0D) {
644 1.1 itojun NIC_PUT_2(sc, REG_000E, 0);
645 1.1 itojun tmp = NIC_GET_2(sc, REG_0002);
646 1.1 itojun if (tmp & REG_0002_BIT_04)
647 1.1 itojun NIC_GET_2(sc, REG_0006);
648 1.1 itojun tmp = NIC_GET_2(sc, REG_0000);
649 1.1 itojun if (tmp & REG_0000_BIT_08)
650 1.1 itojun NIC_BSET(sc, REG_0000, 0x08);
651 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
652 1.1 itojun }
653 1.1 itojun NIC_PUT_2(sc, REG_ISR, isr);
654 1.1 itojun }
655 1.1 itojun }
656 1.1 itojun NIC_PUT_2(sc, REG_ISR, ISR_MASK);
657 1.1 itojun }
658 1.1 itojun
659 1.1 itojun static void
660 1.6 chs netdock_txint(struct netdock_softc *sc)
661 1.1 itojun {
662 1.1 itojun struct ifnet *ifp = &sc->sc_if;
663 1.1 itojun int savereg0002;
664 1.1 itojun int reg0004;
665 1.1 itojun int regdata;
666 1.1 itojun
667 1.1 itojun ifp->if_flags &= ~IFF_OACTIVE;
668 1.1 itojun ifp->if_timer = 0;
669 1.1 itojun
670 1.1 itojun savereg0002 = NIC_GET_2(sc, REG_0002);
671 1.1 itojun
672 1.1 itojun while (((reg0004 = NIC_GET_2(sc, REG_0004)) & REG_0004_BIT_0F) == 0) {
673 1.1 itojun NIC_PUT_2(sc, REG_0002, reg0004);
674 1.1 itojun NIC_PUT_2(sc, REG_0006, 0x0060);
675 1.1 itojun NIC_GET_2(sc, REG_ISR);
676 1.1 itojun regdata = NIC_GET_2(sc, REG_DATA);
677 1.1 itojun if ((regdata & REG_DATA_BIT_08) == 0) {
678 1.1 itojun /* ifp->if_collisions++; */
679 1.1 itojun if (regdata & REG_DATA_BIT_07)
680 1.1 itojun /* ifp->if_oerrors++; */
681 1.1 itojun NIC_PUT_2(sc, REG_000E, 0);
682 1.1 itojun NIC_ORW(sc, REG_0000, 0x0100);
683 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
684 1.1 itojun }
685 1.1 itojun NIC_GET_2(sc ,REG_DATA);
686 1.1 itojun
687 1.1 itojun if (regdata & REG_DATA_BIT_0F)
688 1.1 itojun NIC_GET_4(sc, REG_EFD00);
689 1.1 itojun NIC_PUT_2(sc, REG_0000, 0xa000);
690 1.1 itojun NIC_PUT_2(sc, REG_ISR, ISR_TX);
691 1.1 itojun }
692 1.1 itojun
693 1.1 itojun NIC_PUT_2(sc, REG_0002, savereg0002);
694 1.1 itojun NIC_PUT_2(sc, REG_000E, 0);
695 1.1 itojun NIC_GET_2(sc, REG_0006);
696 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
697 1.1 itojun NIC_PUT_2(sc, REG_0006, 0);
698 1.1 itojun }
699 1.1 itojun
700 1.1 itojun static void
701 1.6 chs netdock_rxint(struct netdock_softc *sc)
702 1.1 itojun {
703 1.1 itojun struct ifnet *ifp = &sc->sc_if;
704 1.1 itojun int regdata1;
705 1.1 itojun int regdata2;
706 1.1 itojun u_int len;
707 1.1 itojun int timeout;
708 1.1 itojun
709 1.1 itojun while ((NIC_GET_2(sc, REG_0004) & REG_0004_BIT_07) == 0) {
710 1.1 itojun NIC_GET_2(sc, REG_ISR);
711 1.1 itojun NIC_PUT_2(sc, REG_0006, 0x00e0);
712 1.1 itojun NIC_GET_2(sc, REG_ISR);
713 1.1 itojun regdata1 = NIC_GET_2(sc, REG_DATA);
714 1.1 itojun regdata2 = NIC_GET_2(sc, REG_DATA);
715 1.1 itojun len = ((regdata2 << 8) & 0x0700) | ((regdata2 >> 8) & 0x00ff);
716 1.1 itojun
717 1.1 itojun #if 0
718 1.1 itojun printf("netdock_rxint: r1=0x%04x, r2=0x%04x, len=%d\n",
719 1.1 itojun regdata1, regdata2, len);
720 1.1 itojun #endif
721 1.1 itojun
722 1.1 itojun if ((regdata1 & REG_DATA_BIT_04) == 0)
723 1.1 itojun len -= 2;
724 1.1 itojun
725 1.7 thorpej /* CRC is included with the packet; trim it off. */
726 1.7 thorpej len -= ETHER_CRC_LEN;
727 1.7 thorpej
728 1.1 itojun if ((regdata1 & 0x00ac) == 0) {
729 1.1 itojun if (netdock_read(sc, len))
730 1.1 itojun ifp->if_ipackets++;
731 1.1 itojun else
732 1.1 itojun ifp->if_ierrors++;
733 1.1 itojun } else {
734 1.1 itojun ifp->if_ierrors++;
735 1.1 itojun
736 1.1 itojun if (regdata1 & REG_DATA_BIT_02)
737 1.1 itojun NIC_GET_4(sc, REG_EFD00);
738 1.1 itojun if (regdata1 & REG_DATA_BIT_03)
739 1.1 itojun ;
740 1.1 itojun if (regdata1 & REG_DATA_BIT_05)
741 1.1 itojun NIC_GET_4(sc, REG_EFD00);
742 1.1 itojun if (regdata1 & REG_DATA_BIT_07)
743 1.1 itojun ;
744 1.1 itojun }
745 1.1 itojun
746 1.1 itojun timeout = 0x14;
747 1.1 itojun while ((NIC_GET_2(sc, REG_0000) & REG_0000_BIT_08) && timeout--)
748 1.1 itojun ;
749 1.1 itojun if (timeout == 0)
750 1.1 itojun ;
751 1.1 itojun
752 1.1 itojun NIC_PUT_2(sc, REG_0000, 0x8000);
753 1.1 itojun }
754 1.1 itojun NIC_PUT_2(sc, REG_0006, 0);
755 1.1 itojun }
756 1.1 itojun
757 1.1 itojun static int
758 1.6 chs netdock_read(struct netdock_softc *sc, int len)
759 1.1 itojun {
760 1.1 itojun struct ifnet *ifp = &sc->sc_if;
761 1.1 itojun struct mbuf *m;
762 1.1 itojun
763 1.1 itojun m = netdock_get(sc, len);
764 1.1 itojun if (m == 0)
765 1.1 itojun return (0);
766 1.1 itojun
767 1.21 joerg bpf_mtap(ifp, m);
768 1.1 itojun
769 1.1 itojun (*ifp->if_input)(ifp, m);
770 1.1 itojun
771 1.1 itojun return (1);
772 1.1 itojun }
773 1.1 itojun
774 1.1 itojun static struct mbuf *
775 1.6 chs netdock_get(struct netdock_softc *sc, int datalen)
776 1.1 itojun {
777 1.1 itojun struct mbuf *m, *top, **mp;
778 1.1 itojun u_char *data;
779 1.1 itojun int i;
780 1.1 itojun int len;
781 1.1 itojun int len4;
782 1.1 itojun u_int32_t *data4;
783 1.1 itojun
784 1.1 itojun MGETHDR(m, M_DONTWAIT, MT_DATA);
785 1.1 itojun if (m == NULL)
786 1.1 itojun return (NULL);
787 1.1 itojun m->m_pkthdr.rcvif = &sc->sc_if;
788 1.1 itojun m->m_pkthdr.len = datalen;
789 1.1 itojun len = MHLEN;
790 1.1 itojun top = NULL;
791 1.1 itojun mp = ⊤
792 1.1 itojun
793 1.1 itojun while (datalen > 0) {
794 1.1 itojun if (top) {
795 1.1 itojun MGET(m, M_DONTWAIT, MT_DATA);
796 1.1 itojun if (m == 0) {
797 1.1 itojun m_freem(top);
798 1.1 itojun return (NULL);
799 1.1 itojun }
800 1.1 itojun len = MLEN;
801 1.1 itojun }
802 1.1 itojun if (datalen >= MINCLSIZE) {
803 1.1 itojun MCLGET(m, M_DONTWAIT);
804 1.1 itojun if ((m->m_flags & M_EXT) == 0) {
805 1.1 itojun if (top)
806 1.1 itojun m_freem(top);
807 1.1 itojun return (NULL);
808 1.1 itojun }
809 1.1 itojun len = MCLBYTES;
810 1.1 itojun }
811 1.1 itojun
812 1.1 itojun if (mp == &top) {
813 1.13 he char *newdata = (char *)
814 1.1 itojun ALIGN(m->m_data + sizeof(struct ether_header)) -
815 1.1 itojun sizeof(struct ether_header);
816 1.1 itojun len -= newdata - m->m_data;
817 1.1 itojun m->m_data = newdata;
818 1.1 itojun }
819 1.1 itojun
820 1.1 itojun m->m_len = len = min(datalen, len);
821 1.1 itojun
822 1.1 itojun data = mtod(m, u_char *);
823 1.1 itojun len4 = len >> 2;
824 1.1 itojun data4 = (u_int32_t *)data;
825 1.1 itojun for (i = 0; i < len4; i++)
826 1.1 itojun data4[i] = NIC_GET_4(sc, REG_DATA);
827 1.1 itojun for (i = len4 << 2; i < len; i++)
828 1.1 itojun data[i] = NIC_GET_1(sc, REG_DATA);
829 1.1 itojun
830 1.1 itojun datalen -= len;
831 1.1 itojun *mp = m;
832 1.1 itojun mp = &m->m_next;
833 1.1 itojun }
834 1.1 itojun
835 1.1 itojun return (top);
836 1.1 itojun }
837