if_netdock_nubus.c revision 1.35 1 1.35 thorpej /* $NetBSD: if_netdock_nubus.c,v 1.35 2022/09/18 02:45:38 thorpej 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.35 thorpej __KERNEL_RCSID(0, "$NetBSD: if_netdock_nubus.c,v 1.35 2022/09/18 02:45:38 thorpej 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.34 skrll #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.29 msaitoh ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
365 1.1 itojun ifp->if_watchdog = netdock_watchdog;
366 1.1 itojun
367 1.1 itojun if_attach(ifp);
368 1.1 itojun ether_ifattach(ifp, lladdr);
369 1.1 itojun
370 1.1 itojun return (0);
371 1.1 itojun }
372 1.1 itojun
373 1.1 itojun static int
374 1.12 christos netdock_ioctl(struct ifnet *ifp, u_long cmd, void *data)
375 1.1 itojun {
376 1.1 itojun struct ifaddr *ifa;
377 1.1 itojun struct netdock_softc *sc = ifp->if_softc;
378 1.1 itojun int s = splnet();
379 1.1 itojun int err = 0;
380 1.30 msaitoh u_short temp;
381 1.1 itojun
382 1.1 itojun switch (cmd) {
383 1.19 dyoung case SIOCINITIFADDR:
384 1.1 itojun ifa = (struct ifaddr *)data;
385 1.1 itojun ifp->if_flags |= IFF_UP;
386 1.19 dyoung (void)netdock_init(sc);
387 1.1 itojun switch (ifa->ifa_addr->sa_family) {
388 1.1 itojun #ifdef INET
389 1.1 itojun case AF_INET:
390 1.1 itojun arp_ifinit(ifp, ifa);
391 1.1 itojun break;
392 1.1 itojun #endif
393 1.1 itojun default:
394 1.1 itojun break;
395 1.1 itojun }
396 1.1 itojun break;
397 1.1 itojun
398 1.1 itojun case SIOCSIFFLAGS:
399 1.19 dyoung if ((err = ifioctl_common(ifp, cmd, data)) != 0)
400 1.19 dyoung break;
401 1.19 dyoung /* XXX see the comment in ed_ioctl() about code re-use */
402 1.1 itojun if ((ifp->if_flags & IFF_UP) == 0 &&
403 1.1 itojun (ifp->if_flags & IFF_RUNNING) != 0) {
404 1.1 itojun netdock_stop(sc);
405 1.1 itojun ifp->if_flags &= ~IFF_RUNNING;
406 1.1 itojun } else if ((ifp->if_flags & IFF_UP) != 0 &&
407 1.1 itojun (ifp->if_flags & IFF_RUNNING) == 0) {
408 1.1 itojun (void)netdock_init(sc);
409 1.1 itojun } else {
410 1.1 itojun temp = ifp->if_flags & IFF_UP;
411 1.1 itojun netdock_reset(sc);
412 1.1 itojun ifp->if_flags |= temp;
413 1.1 itojun netdock_start(ifp);
414 1.1 itojun }
415 1.1 itojun break;
416 1.1 itojun
417 1.1 itojun case SIOCADDMULTI:
418 1.1 itojun case SIOCDELMULTI:
419 1.15 jmmv if ((err = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
420 1.5 thorpej if (ifp->if_flags & IFF_RUNNING) {
421 1.5 thorpej temp = ifp->if_flags & IFF_UP;
422 1.5 thorpej netdock_reset(sc);
423 1.5 thorpej ifp->if_flags |= temp;
424 1.5 thorpej }
425 1.1 itojun err = 0;
426 1.1 itojun }
427 1.1 itojun break;
428 1.1 itojun default:
429 1.19 dyoung err = ether_ioctl(ifp, cmd, data);
430 1.1 itojun break;
431 1.1 itojun }
432 1.1 itojun splx(s);
433 1.1 itojun return (err);
434 1.1 itojun }
435 1.1 itojun
436 1.1 itojun static void
437 1.6 chs netdock_start(struct ifnet *ifp)
438 1.1 itojun {
439 1.1 itojun struct netdock_softc *sc = ifp->if_softc;
440 1.1 itojun struct mbuf *m;
441 1.1 itojun
442 1.35 thorpej if ((ifp->if_flags & IFF_RUNNING) == 0)
443 1.1 itojun return;
444 1.1 itojun
445 1.1 itojun while (1) {
446 1.1 itojun IF_DEQUEUE(&ifp->if_snd, m);
447 1.1 itojun if (m == 0)
448 1.1 itojun return;
449 1.1 itojun
450 1.35 thorpej KASSERT(m->m_flags & M_PKTHDR);
451 1.1 itojun
452 1.27 msaitoh bpf_mtap(ifp, m, BPF_D_OUT);
453 1.1 itojun
454 1.1 itojun if ((netdock_put(sc, m)) == 0) {
455 1.1 itojun IF_PREPEND(&ifp->if_snd, m);
456 1.1 itojun return;
457 1.1 itojun }
458 1.1 itojun
459 1.32 thorpej if_statinc(ifp, if_opackets);
460 1.1 itojun }
461 1.1 itojun }
462 1.1 itojun
463 1.1 itojun static void
464 1.6 chs netdock_reset(struct netdock_softc *sc)
465 1.1 itojun {
466 1.1 itojun
467 1.1 itojun netdock_stop(sc);
468 1.1 itojun netdock_init(sc);
469 1.1 itojun }
470 1.1 itojun
471 1.1 itojun static int
472 1.6 chs netdock_init(struct netdock_softc *sc)
473 1.1 itojun {
474 1.1 itojun int s;
475 1.1 itojun int saveisr;
476 1.1 itojun int savetmp;
477 1.1 itojun
478 1.1 itojun if (sc->sc_if.if_flags & IFF_RUNNING)
479 1.1 itojun return (0);
480 1.1 itojun
481 1.1 itojun s = splnet();
482 1.1 itojun
483 1.1 itojun /* 0606 */
484 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
485 1.1 itojun NIC_PUT_2(sc, REG_ISR , 0);
486 1.1 itojun NIC_PUT_2(sc, REG_000E, 0);
487 1.1 itojun
488 1.1 itojun NIC_PUT_2(sc, REG_0000, 0x8104);
489 1.1 itojun NIC_PUT_2(sc, REG_0004, 0x0043);
490 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0100);
491 1.1 itojun NIC_PUT_2(sc, REG_0002, 0x6618);
492 1.1 itojun NIC_PUT_2(sc, REG_0000, 0x8010);
493 1.1 itojun
494 1.1 itojun NIC_PUT_1(sc, REG_0004 + 0, sc->sc_enaddr[0]);
495 1.1 itojun NIC_PUT_1(sc, REG_0004 + 1, sc->sc_enaddr[1]);
496 1.1 itojun NIC_PUT_1(sc, REG_0004 + 2, sc->sc_enaddr[2]);
497 1.1 itojun NIC_PUT_1(sc, REG_0004 + 3, sc->sc_enaddr[3]);
498 1.1 itojun NIC_PUT_1(sc, REG_0004 + 4, sc->sc_enaddr[4]);
499 1.1 itojun NIC_PUT_1(sc, REG_0004 + 5, sc->sc_enaddr[5]);
500 1.1 itojun
501 1.1 itojun NIC_PUT_2(sc, REG_ISR , 0x2008);
502 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
503 1.1 itojun NIC_PUT_2(sc, REG_0000, 0x4000);
504 1.1 itojun NIC_PUT_2(sc, REG_ISR, ISR_MASK);
505 1.1 itojun
506 1.1 itojun
507 1.1 itojun /* 1320 */
508 1.1 itojun saveisr = NIC_GET_2(sc, REG_ISR);
509 1.1 itojun NIC_PUT_2(sc, REG_ISR , 0);
510 1.1 itojun savetmp = NIC_GET_2(sc, REG_000E);
511 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0100);
512 1.1 itojun NIC_ANDW(sc, REG_ISR, ~ISR_BIT_03);
513 1.1 itojun NIC_PUT_2(sc, REG_000E, savetmp);
514 1.1 itojun
515 1.1 itojun /* 1382 */
516 1.1 itojun savetmp = NIC_GET_2(sc, REG_000E);
517 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0100);
518 1.34 skrll NIC_ORW(sc, REG_ISR, ISR_BIT_03);
519 1.1 itojun NIC_PUT_2(sc, REG_000E, savetmp);
520 1.1 itojun NIC_PUT_2(sc, REG_ISR , saveisr);
521 1.1 itojun
522 1.1 itojun
523 1.1 itojun sc->sc_if.if_flags |= IFF_RUNNING;
524 1.1 itojun
525 1.1 itojun splx(s);
526 1.1 itojun return (0);
527 1.1 itojun }
528 1.1 itojun
529 1.1 itojun static int
530 1.6 chs netdock_stop(struct netdock_softc *sc)
531 1.1 itojun {
532 1.1 itojun int s = splnet();
533 1.1 itojun
534 1.1 itojun sc->sc_if.if_timer = 0;
535 1.1 itojun sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
536 1.1 itojun
537 1.1 itojun splx(s);
538 1.1 itojun return (0);
539 1.1 itojun }
540 1.1 itojun
541 1.1 itojun static void
542 1.6 chs netdock_watchdog(struct ifnet *ifp)
543 1.1 itojun {
544 1.1 itojun struct netdock_softc *sc = ifp->if_softc;
545 1.30 msaitoh u_short tmp;
546 1.1 itojun
547 1.1 itojun printf("netdock_watchdog: resetting chip\n");
548 1.1 itojun tmp = ifp->if_flags & IFF_UP;
549 1.1 itojun netdock_reset(sc);
550 1.1 itojun ifp->if_flags |= tmp;
551 1.1 itojun }
552 1.1 itojun
553 1.1 itojun static u_int
554 1.6 chs netdock_put(struct netdock_softc *sc, struct mbuf *m0)
555 1.1 itojun {
556 1.1 itojun struct mbuf *m;
557 1.1 itojun u_int totlen = 0;
558 1.1 itojun u_int tmplen;
559 1.1 itojun int isr;
560 1.1 itojun int timeout;
561 1.1 itojun int tmp;
562 1.1 itojun u_int i;
563 1.1 itojun
564 1.1 itojun for (m = m0; m; m = m->m_next)
565 1.1 itojun totlen += m->m_len;
566 1.1 itojun
567 1.1 itojun if (totlen >= ETHER_MAX_LEN)
568 1.17 hauke panic("%s: netdock_put: packet overflow",
569 1.18 hauke device_xname(sc->sc_dev));
570 1.1 itojun
571 1.1 itojun totlen += 6;
572 1.1 itojun tmplen = totlen;
573 1.1 itojun tmplen &= 0xff00;
574 1.1 itojun tmplen |= 0x2000;
575 1.1 itojun NIC_PUT_2(sc, REG_0000, tmplen);
576 1.1 itojun
577 1.1 itojun timeout = 0x3000;
578 1.1 itojun while ((((isr = NIC_GET_2(sc, REG_ISR)) & ISR_READY) == 0) &&
579 1.1 itojun timeout--) {
580 1.1 itojun if (isr & ISR_TX)
581 1.1 itojun netdock_txint(sc);
582 1.1 itojun }
583 1.1 itojun if (timeout == 0)
584 1.1 itojun return (0);
585 1.1 itojun
586 1.1 itojun tmp = NIC_GET_2(sc, REG_0002);
587 1.1 itojun tmp <<= 8;
588 1.1 itojun NIC_PUT_2(sc, REG_0002, tmp);
589 1.1 itojun NIC_PUT_2(sc, REG_0006, 0x240);
590 1.1 itojun NIC_GET_2(sc, REG_ISR);
591 1.1 itojun tmplen = ((totlen << 8) & 0xfe00) | ((totlen >> 8) & 0x00ff);
592 1.1 itojun NIC_PUT_2(sc, REG_DATA, tmplen);
593 1.1 itojun
594 1.1 itojun for (m = m0; m; m = m->m_next) {
595 1.1 itojun u_char *data = mtod(m, u_char *);
596 1.1 itojun int len = m->m_len;
597 1.1 itojun int len4 = len >> 2;
598 1.1 itojun u_int32_t *data4 = (u_int32_t *)data;
599 1.1 itojun for (i = 0; i < len4; i++)
600 1.1 itojun NIC_PUT_4(sc, REG_DATA, data4[i]);
601 1.1 itojun for (i = len4 << 2; i < len; i++)
602 1.1 itojun NIC_PUT_1(sc, REG_DATA, data[i]);
603 1.1 itojun }
604 1.1 itojun
605 1.1 itojun if (totlen & 0x01)
606 1.1 itojun NIC_PUT_2(sc, REG_DATA, 0x2020);
607 1.1 itojun else
608 1.1 itojun NIC_PUT_2(sc, REG_DATA, 0);
609 1.1 itojun
610 1.1 itojun NIC_PUT_2(sc, REG_0000, 0xc000);
611 1.1 itojun
612 1.1 itojun m_freem(m0);
613 1.1 itojun /* sc->sc_if.if_timer = 5; */
614 1.1 itojun return (totlen);
615 1.1 itojun }
616 1.1 itojun
617 1.1 itojun void
618 1.6 chs netdock_intr(void *arg)
619 1.1 itojun {
620 1.1 itojun struct netdock_softc *sc = (struct netdock_softc *)arg;
621 1.1 itojun int isr;
622 1.1 itojun int tmp;
623 1.1 itojun
624 1.1 itojun NIC_PUT_2(sc, REG_ISR, 0);
625 1.1 itojun while ((isr = (NIC_GET_2(sc, REG_ISR) & ISR_ALL)) != 0) {
626 1.1 itojun if (isr & ISR_TX)
627 1.1 itojun netdock_txint(sc);
628 1.1 itojun
629 1.1 itojun if (isr & ISR_RX)
630 1.1 itojun netdock_rxint(sc);
631 1.1 itojun
632 1.1 itojun if (isr & (ISR_BIT_0C | ISR_BIT_0D)) {
633 1.1 itojun if (isr & ISR_BIT_0C) {
634 1.1 itojun NIC_PUT_2(sc, REG_000E, 0);
635 1.1 itojun NIC_BSET(sc, REG_0004, 0x08);
636 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
637 1.1 itojun }
638 1.1 itojun if (isr & ISR_BIT_0D) {
639 1.1 itojun NIC_PUT_2(sc, REG_000E, 0);
640 1.1 itojun tmp = NIC_GET_2(sc, REG_0002);
641 1.1 itojun if (tmp & REG_0002_BIT_04)
642 1.1 itojun NIC_GET_2(sc, REG_0006);
643 1.1 itojun tmp = NIC_GET_2(sc, REG_0000);
644 1.1 itojun if (tmp & REG_0000_BIT_08)
645 1.1 itojun NIC_BSET(sc, REG_0000, 0x08);
646 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
647 1.1 itojun }
648 1.1 itojun NIC_PUT_2(sc, REG_ISR, isr);
649 1.1 itojun }
650 1.1 itojun }
651 1.1 itojun NIC_PUT_2(sc, REG_ISR, ISR_MASK);
652 1.1 itojun }
653 1.1 itojun
654 1.1 itojun static void
655 1.6 chs netdock_txint(struct netdock_softc *sc)
656 1.1 itojun {
657 1.1 itojun struct ifnet *ifp = &sc->sc_if;
658 1.1 itojun int savereg0002;
659 1.1 itojun int reg0004;
660 1.1 itojun int regdata;
661 1.1 itojun
662 1.1 itojun ifp->if_timer = 0;
663 1.1 itojun
664 1.1 itojun savereg0002 = NIC_GET_2(sc, REG_0002);
665 1.1 itojun
666 1.1 itojun while (((reg0004 = NIC_GET_2(sc, REG_0004)) & REG_0004_BIT_0F) == 0) {
667 1.1 itojun NIC_PUT_2(sc, REG_0002, reg0004);
668 1.1 itojun NIC_PUT_2(sc, REG_0006, 0x0060);
669 1.1 itojun NIC_GET_2(sc, REG_ISR);
670 1.1 itojun regdata = NIC_GET_2(sc, REG_DATA);
671 1.1 itojun if ((regdata & REG_DATA_BIT_08) == 0) {
672 1.33 skrll /* if_statinc(ifp, if_collisions); */
673 1.1 itojun if (regdata & REG_DATA_BIT_07)
674 1.33 skrll /* if_statinc(ifp, if_oerrors); */
675 1.1 itojun NIC_PUT_2(sc, REG_000E, 0);
676 1.1 itojun NIC_ORW(sc, REG_0000, 0x0100);
677 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
678 1.1 itojun }
679 1.1 itojun NIC_GET_2(sc ,REG_DATA);
680 1.1 itojun
681 1.1 itojun if (regdata & REG_DATA_BIT_0F)
682 1.1 itojun NIC_GET_4(sc, REG_EFD00);
683 1.1 itojun NIC_PUT_2(sc, REG_0000, 0xa000);
684 1.1 itojun NIC_PUT_2(sc, REG_ISR, ISR_TX);
685 1.1 itojun }
686 1.1 itojun
687 1.1 itojun NIC_PUT_2(sc, REG_0002, savereg0002);
688 1.1 itojun NIC_PUT_2(sc, REG_000E, 0);
689 1.1 itojun NIC_GET_2(sc, REG_0006);
690 1.1 itojun NIC_PUT_2(sc, REG_000E, 0x0200);
691 1.1 itojun NIC_PUT_2(sc, REG_0006, 0);
692 1.1 itojun }
693 1.1 itojun
694 1.1 itojun static void
695 1.6 chs netdock_rxint(struct netdock_softc *sc)
696 1.1 itojun {
697 1.1 itojun struct ifnet *ifp = &sc->sc_if;
698 1.1 itojun int regdata1;
699 1.1 itojun int regdata2;
700 1.1 itojun u_int len;
701 1.1 itojun int timeout;
702 1.1 itojun
703 1.1 itojun while ((NIC_GET_2(sc, REG_0004) & REG_0004_BIT_07) == 0) {
704 1.1 itojun NIC_GET_2(sc, REG_ISR);
705 1.1 itojun NIC_PUT_2(sc, REG_0006, 0x00e0);
706 1.1 itojun NIC_GET_2(sc, REG_ISR);
707 1.1 itojun regdata1 = NIC_GET_2(sc, REG_DATA);
708 1.1 itojun regdata2 = NIC_GET_2(sc, REG_DATA);
709 1.1 itojun len = ((regdata2 << 8) & 0x0700) | ((regdata2 >> 8) & 0x00ff);
710 1.1 itojun
711 1.1 itojun #if 0
712 1.1 itojun printf("netdock_rxint: r1=0x%04x, r2=0x%04x, len=%d\n",
713 1.1 itojun regdata1, regdata2, len);
714 1.1 itojun #endif
715 1.1 itojun
716 1.1 itojun if ((regdata1 & REG_DATA_BIT_04) == 0)
717 1.1 itojun len -= 2;
718 1.1 itojun
719 1.7 thorpej /* CRC is included with the packet; trim it off. */
720 1.7 thorpej len -= ETHER_CRC_LEN;
721 1.7 thorpej
722 1.1 itojun if ((regdata1 & 0x00ac) == 0) {
723 1.31 msaitoh if (netdock_read(sc, len) == 0)
724 1.32 thorpej if_statinc(ifp, if_ierrors);
725 1.1 itojun } else {
726 1.32 thorpej if_statinc(ifp, if_ierrors);
727 1.1 itojun
728 1.1 itojun if (regdata1 & REG_DATA_BIT_02)
729 1.1 itojun NIC_GET_4(sc, REG_EFD00);
730 1.1 itojun if (regdata1 & REG_DATA_BIT_03)
731 1.1 itojun ;
732 1.1 itojun if (regdata1 & REG_DATA_BIT_05)
733 1.1 itojun NIC_GET_4(sc, REG_EFD00);
734 1.1 itojun if (regdata1 & REG_DATA_BIT_07)
735 1.1 itojun ;
736 1.1 itojun }
737 1.1 itojun
738 1.1 itojun timeout = 0x14;
739 1.1 itojun while ((NIC_GET_2(sc, REG_0000) & REG_0000_BIT_08) && timeout--)
740 1.1 itojun ;
741 1.1 itojun if (timeout == 0)
742 1.1 itojun ;
743 1.1 itojun
744 1.1 itojun NIC_PUT_2(sc, REG_0000, 0x8000);
745 1.1 itojun }
746 1.1 itojun NIC_PUT_2(sc, REG_0006, 0);
747 1.1 itojun }
748 1.1 itojun
749 1.1 itojun static int
750 1.6 chs netdock_read(struct netdock_softc *sc, int len)
751 1.1 itojun {
752 1.1 itojun struct ifnet *ifp = &sc->sc_if;
753 1.1 itojun struct mbuf *m;
754 1.1 itojun
755 1.1 itojun m = netdock_get(sc, len);
756 1.1 itojun if (m == 0)
757 1.1 itojun return (0);
758 1.1 itojun
759 1.23 ozaki if_percpuq_enqueue(ifp->if_percpuq, m);
760 1.1 itojun
761 1.1 itojun return (1);
762 1.1 itojun }
763 1.1 itojun
764 1.1 itojun static struct mbuf *
765 1.6 chs netdock_get(struct netdock_softc *sc, int datalen)
766 1.1 itojun {
767 1.1 itojun struct mbuf *m, *top, **mp;
768 1.1 itojun u_char *data;
769 1.1 itojun int i;
770 1.1 itojun int len;
771 1.1 itojun int len4;
772 1.1 itojun u_int32_t *data4;
773 1.1 itojun
774 1.1 itojun MGETHDR(m, M_DONTWAIT, MT_DATA);
775 1.1 itojun if (m == NULL)
776 1.1 itojun return (NULL);
777 1.24 ozaki m_set_rcvif(m, &sc->sc_if);
778 1.1 itojun m->m_pkthdr.len = datalen;
779 1.1 itojun len = MHLEN;
780 1.1 itojun top = NULL;
781 1.1 itojun mp = ⊤
782 1.1 itojun
783 1.1 itojun while (datalen > 0) {
784 1.1 itojun if (top) {
785 1.1 itojun MGET(m, M_DONTWAIT, MT_DATA);
786 1.1 itojun if (m == 0) {
787 1.1 itojun m_freem(top);
788 1.1 itojun return (NULL);
789 1.1 itojun }
790 1.1 itojun len = MLEN;
791 1.1 itojun }
792 1.1 itojun if (datalen >= MINCLSIZE) {
793 1.1 itojun MCLGET(m, M_DONTWAIT);
794 1.1 itojun if ((m->m_flags & M_EXT) == 0) {
795 1.1 itojun if (top)
796 1.1 itojun m_freem(top);
797 1.26 riastrad else
798 1.26 riastrad m_freem(m);
799 1.1 itojun return (NULL);
800 1.1 itojun }
801 1.1 itojun len = MCLBYTES;
802 1.1 itojun }
803 1.1 itojun
804 1.1 itojun if (mp == &top) {
805 1.13 he char *newdata = (char *)
806 1.1 itojun ALIGN(m->m_data + sizeof(struct ether_header)) -
807 1.1 itojun sizeof(struct ether_header);
808 1.1 itojun len -= newdata - m->m_data;
809 1.1 itojun m->m_data = newdata;
810 1.1 itojun }
811 1.1 itojun
812 1.28 riastrad m->m_len = len = uimin(datalen, len);
813 1.1 itojun
814 1.1 itojun data = mtod(m, u_char *);
815 1.1 itojun len4 = len >> 2;
816 1.1 itojun data4 = (u_int32_t *)data;
817 1.1 itojun for (i = 0; i < len4; i++)
818 1.1 itojun data4[i] = NIC_GET_4(sc, REG_DATA);
819 1.1 itojun for (i = len4 << 2; i < len; i++)
820 1.1 itojun data[i] = NIC_GET_1(sc, REG_DATA);
821 1.1 itojun
822 1.1 itojun datalen -= len;
823 1.1 itojun *mp = m;
824 1.1 itojun mp = &m->m_next;
825 1.1 itojun }
826 1.1 itojun
827 1.1 itojun return (top);
828 1.1 itojun }
829