1 /*        $NetBSD: if_ne_intio.c,v 1.20 2024/01/06 05:16:57 isaki Exp $         */
2 
3 /*
4  * Copyright (c) 2001 Tetsuya Isaki. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * Ethernet part of Nereid Ethernet/USB/Memory board
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_ne_intio.c,v 1.20 2024/01/06 05:16:57 isaki Exp $");
34 
35 #include "opt_inet.h"
36 #include "opt_ns.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mbuf.h>
41 #include <sys/socket.h>
42 #include <sys/select.h>
43 #include <sys/device.h>
44 
45 #include <net/if.h>
46 #include <net/if_dl.h>
47 #include <net/if_ether.h>
48 #include <net/if_media.h>
49 
50 #ifdef INET
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/in_var.h>
54 #include <netinet/ip.h>
55 #include <netinet/if_inarp.h>
56 #endif
57 
58 #if BPFILTER > 0
59 #include <net/bpf.h>
60 #endif
61 
62 #include <machine/bus.h>
63 #include <machine/cpu.h>
64 
65 #include <dev/ic/dp8390reg.h>
66 #include <dev/ic/dp8390var.h>
67 #include <dev/ic/ne2000reg.h>
68 #include <dev/ic/ne2000var.h>
69 
70 #include <arch/x68k/dev/intiovar.h>
71 
72 #define NE_INTIO_ADDR  (0xece300)
73 #define NE_INTIO_ADDR2 (0xeceb00)
74 #define NE_INTIO_INTR  (0xf9)
75 #define NE_INTIO_INTR2 (0xf8)
76 
77 static int  ne_intio_match(device_t, cfdata_t, void *);
78 static void ne_intio_attach(device_t, device_t, void *);
79 
80 #define ne_intio_softc ne2000_softc
81 
82 CFATTACH_DECL_NEW(ne_intio, sizeof(struct ne_intio_softc),
83     ne_intio_match, ne_intio_attach, NULL, NULL);
84 
85 static int
ne_intio_match(device_t parent,cfdata_t cf,void * aux)86 ne_intio_match(device_t parent, cfdata_t cf, void *aux)
87 {
88           struct intio_attach_args *ia = aux;
89           bus_space_tag_t iot = ia->ia_bst;
90           bus_space_handle_t ioh;
91           bus_space_tag_t asict;
92           bus_space_handle_t asich;
93           int rv = 0;
94 
95           if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
96                     ia->ia_addr = NE_INTIO_ADDR;
97           if (ia->ia_intr == INTIOCF_INTR_DEFAULT)
98                     ia->ia_intr = NE_INTIO_INTR;
99 
100           /* fixed parameters */
101           if (!(ia->ia_addr == NE_INTIO_ADDR  && ia->ia_intr == NE_INTIO_INTR ) &&
102               !(ia->ia_addr == NE_INTIO_ADDR2 && ia->ia_intr == NE_INTIO_INTR2)  )
103                     return 0;
104 
105           /* Make sure this is a valid NE2000 I/O address */
106           if ((ia->ia_addr & 0x1f) != 0)
107                     return 0;
108 
109           /* Check whether the board is inserted or not */
110           if (badaddr((void *)IIOV(ia->ia_addr)))
111                     return 0;
112 
113           /* Map I/O space */
114           if (bus_space_map(iot, ia->ia_addr, NE2000_NPORTS * 2,
115                               BUS_SPACE_MAP_SHIFTED_EVEN, &ioh))
116                     return 0;
117 
118           asict = iot;
119           if (bus_space_subregion(iot, ioh, NE2000_ASIC_OFFSET * 2,
120                               NE2000_ASIC_NPORTS * 2, &asich))
121                     goto out;
122 
123           /* Look for an NE2000 compatible card */
124           rv = ne2000_detect(iot, ioh, asict, asich);
125 
126  out:
127           bus_space_unmap(iot, ioh, NE2000_NPORTS);
128           return (rv != 0) ? 1 : 0;
129 }
130 
131 static void
ne_intio_attach(device_t parent,device_t self,void * aux)132 ne_intio_attach(device_t parent, device_t self, void *aux)
133 {
134           struct ne_intio_softc *sc = device_private(self);
135           struct dp8390_softc *dsc = &sc->sc_dp8390;
136           struct intio_attach_args *ia = aux;
137           bus_space_tag_t iot = ia->ia_bst;
138           bus_space_handle_t ioh;
139           bus_space_tag_t asict;
140           bus_space_handle_t asich;
141           const char *typestr;
142           int netype;
143 
144           dsc->sc_dev = self;
145           aprint_normal(": Nereid Ethernet\n");
146 
147           /* Map I/O space */
148           if (bus_space_map(iot, ia->ia_addr, NE2000_NPORTS * 2,
149                               BUS_SPACE_MAP_SHIFTED_EVEN, &ioh)) {
150                     aprint_error_dev(self, "can't map I/O space\n");
151                     return;
152           }
153 
154           asict = iot;
155           if (bus_space_subregion(iot, ioh, NE2000_ASIC_OFFSET * 2,
156                               NE2000_ASIC_NPORTS * 2, &asich)) {
157                     aprint_error_dev(self, "can't subregion I/O space\n");
158                     return;
159           }
160 
161           dsc->sc_regt = iot;
162           dsc->sc_regh = ioh;
163 
164           sc->sc_asict = asict;
165           sc->sc_asich = asich;
166 
167           /*
168            * detect it again, so we can print some information about
169            * the interface.
170            * XXX: Should I check NE1000 or NE2000 for Nereid?
171            */
172           netype = ne2000_detect(iot, ioh, asict, asich);
173           switch (netype) {
174           case NE2000_TYPE_NE1000:
175                     typestr = "NE1000";
176                     break;
177 
178           case NE2000_TYPE_NE2000:
179                     typestr = "NE2000";
180                     break;
181 
182           case NE2000_TYPE_RTL8019:
183                     typestr = "NE2000 (RTL8019)";
184                     break;
185 
186           default:
187                     aprint_error_dev(self, "where did the card go?!\n");
188                     return;
189           }
190 
191           aprint_normal_dev(self, "%s Ethernet\n", typestr);
192 
193           /* This interface is always enabled */
194           dsc->sc_enabled = 1;
195 
196           /*
197            * Do generic NE2000 attach.
198            * This will read the mac address from the EEPROM.
199            */
200           ne2000_attach(sc, NULL);
201 
202           /* Establish the interrupt handler */
203           if (intio_intr_establish(ia->ia_intr, "ne", dp8390_intr, dsc))
204                     aprint_error_dev(self,
205                         "couldn't establish interrupt handler\n");
206 }
207