1 /*        $NetBSD: depca_eisa.c,v 1.17 2021/07/24 19:14:35 thorpej Exp $        */
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * EISA bus front-end for the Digital DEPCA Ethernet controller.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: depca_eisa.c,v 1.17 2021/07/24 19:14:35 thorpej Exp $");
38 
39 #include "opt_inet.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/mbuf.h>
44 #include <sys/syslog.h>
45 #include <sys/socket.h>
46 #include <sys/device.h>
47 
48 #include <net/if.h>
49 #include <net/if_media.h>
50 #include <net/if_ether.h>
51 
52 #ifdef INET
53 #include <netinet/in.h>
54 #include <netinet/if_inarp.h>
55 #endif
56 
57 #include <sys/bus.h>
58 #include <sys/intr.h>
59 
60 #include <dev/eisa/eisareg.h>
61 #include <dev/eisa/eisavar.h>
62 #include <dev/eisa/eisadevs.h>
63 
64 #include <dev/ic/lancereg.h>
65 #include <dev/ic/lancevar.h>
66 #include <dev/ic/am7990reg.h>
67 #include <dev/ic/am7990var.h>
68 #include <dev/ic/depcareg.h>
69 #include <dev/ic/depcavar.h>
70 
71 static int          depca_eisa_match(device_t, cfdata_t, void *);
72 static void         depca_eisa_attach(device_t, device_t, void *);
73 
74 struct depca_eisa_softc {
75           struct depca_softc sc_depca;
76 
77           eisa_chipset_tag_t sc_ec;
78           int sc_irq;
79           int sc_ist;
80 };
81 
82 CFATTACH_DECL_NEW(depca_eisa, sizeof(struct depca_eisa_softc),
83     depca_eisa_match, depca_eisa_attach, NULL, NULL);
84 
85 static void         *depca_eisa_intr_establish(struct depca_softc *,
86                                                      struct lance_softc *);
87 
88 static int
depca_eisa_match(device_t parent,cfdata_t cf,void * aux)89 depca_eisa_match(device_t parent, cfdata_t cf, void *aux)
90 {
91           struct eisa_attach_args *ea = aux;
92 
93           return (strcmp(ea->ea_idstring, "DEC4220") == 0);
94 }
95 
96 #define   DEPCA_ECU_FUNC_NETINTR        0
97 #define   DEPCA_ECU_FUNC_NETBUF         1
98 
99 static void
depca_eisa_attach(device_t parent,device_t self,void * aux)100 depca_eisa_attach(device_t parent, device_t self, void *aux)
101 {
102           struct depca_eisa_softc *esc = device_private(self);
103           struct depca_softc *sc = &esc->sc_depca;
104           struct eisa_attach_args *ea = aux;
105           struct eisa_cfg_mem ecm;
106           struct eisa_cfg_irq eci;
107 
108           sc->sc_dev = self;
109           aprint_naive("\n");
110           aprint_normal(": DEC DE422 Ethernet\n");
111 
112           sc->sc_iot = ea->ea_iot;
113           sc->sc_memt = ea->ea_memt;
114 
115           esc->sc_ec = ea->ea_ec;
116 
117           sc->sc_intr_establish = depca_eisa_intr_establish;
118 
119           if (eisa_conf_read_mem(ea->ea_ec, ea->ea_slot,
120               DEPCA_ECU_FUNC_NETBUF, 0, &ecm) != 0) {
121                     aprint_error_dev(self, "unable to find network buffer\n");
122                     return;
123           }
124 
125           aprint_normal_dev(self, "shared memory at 0x%lx-0x%lx\n",
126               ecm.ecm_addr, ecm.ecm_addr + ecm.ecm_size - 1);
127 
128           sc->sc_memsize = ecm.ecm_size;
129 
130           if (bus_space_map(sc->sc_iot, EISA_SLOT_ADDR(ea->ea_slot) + 0xc00, 16,
131               0, &sc->sc_ioh) != 0) {
132                     aprint_error_dev(self, "unable to map i/o space\n");
133                     return;
134           }
135           if (bus_space_map(sc->sc_memt, ecm.ecm_addr, sc->sc_memsize,
136               0, &sc->sc_memh) != 0) {
137                     aprint_error_dev(self, "unable to map memory space\n");
138                     return;
139           }
140 
141           if (eisa_conf_read_irq(ea->ea_ec, ea->ea_slot,
142               DEPCA_ECU_FUNC_NETINTR, 0, &eci) != 0) {
143                     aprint_error_dev(self, "unable to determine IRQ\n");
144                     return;
145           }
146 
147           esc->sc_irq = eci.eci_irq;
148           esc->sc_ist = eci.eci_ist;
149 
150           depca_attach(sc);
151 }
152 
153 static void *
depca_eisa_intr_establish(struct depca_softc * sc,struct lance_softc * child)154 depca_eisa_intr_establish(struct depca_softc *sc, struct lance_softc *child)
155 {
156           struct depca_eisa_softc *esc = (struct depca_eisa_softc *)sc;
157           eisa_intr_handle_t ih;
158           const char *intrstr;
159           void *rv;
160           char intrbuf[EISA_INTRSTR_LEN];
161 
162           if (eisa_intr_map(esc->sc_ec, esc->sc_irq, &ih)) {
163                     aprint_error_dev(sc->sc_dev,
164                         "unable to map interrupt (%d)\n", esc->sc_irq);
165                     return (NULL);
166           }
167           intrstr = eisa_intr_string(esc->sc_ec, ih, intrbuf, sizeof(intrbuf));
168           rv = eisa_intr_establish(esc->sc_ec, ih, esc->sc_ist, IPL_NET,
169               (esc->sc_ist == IST_LEVEL) ? am7990_intr : depca_intredge, child);
170           if (rv == NULL) {
171                     aprint_error_dev(sc->sc_dev,
172                         "unable to establish interrupt");
173                     if (intrstr != NULL)
174                               aprint_error(" at %s", intrstr);
175                     aprint_error("\n");
176                     return (NULL);
177           }
178           if (intrstr != NULL) {
179                     aprint_normal_dev(sc->sc_dev,
180                         "interrupting at %s (%s trigger)\n",
181                         (esc->sc_ist == IST_LEVEL) ? "level" : "edge", intrstr);
182           }
183 
184           return (rv);
185 }
186