1 /*        $NetBSD: if_le.c,v 1.63 2024/01/16 05:48:28 thorpej Exp $   */
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum and 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  * Copyright (c) 1992, 1993
34  *        The Regents of the University of California.  All rights reserved.
35  *
36  * This code is derived from software contributed to Berkeley by
37  * Ralph Campbell and Rick Macklem.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. Neither the name of the University nor the names of its contributors
48  *    may be used to endorse or promote products derived from this software
49  *    without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61  * SUCH DAMAGE.
62  *
63  *        @(#)if_le.c         8.2 (Berkeley) 11/16/93
64  */
65 
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.63 2024/01/16 05:48:28 thorpej Exp $");
68 
69 #include "opt_inet.h"
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/device.h>
74 
75 #include <net/if.h>
76 #include <net/if_ether.h>
77 #include <net/if_media.h>
78 
79 #include <dev/ic/lancereg.h>
80 #include <dev/ic/lancevar.h>
81 #include <dev/ic/am7990var.h>
82 
83 #include <hp300/dev/diovar.h>
84 #include <hp300/dev/diodevs.h>
85 #include <hp300/dev/if_lereg.h>
86 
87 #include "opt_useleds.h"
88 
89 #ifdef USELEDS
90 #include <hp300/hp300/leds.h>
91 #endif
92 
93 struct  le_softc {
94           struct  am7990_softc sc_am7990; /* glue to MI code */
95 
96           bus_space_tag_t sc_bst;
97 
98           bus_space_handle_t sc_bsh0;   /* DIO registers */
99           bus_space_handle_t sc_bsh1;   /* LANCE registers */
100           bus_space_handle_t sc_bsh2;   /* buffer area */
101 };
102 
103 static int          lematch(device_t, cfdata_t, void *);
104 static void         leattach(device_t, device_t, void *);
105 
106 CFATTACH_DECL_NEW(le, sizeof(struct le_softc),
107     lematch, leattach, NULL, NULL);
108 
109 static int          leintr(void *);
110 
111 #if defined(_KERNEL_OPT)
112 #include "opt_ddb.h"
113 #endif
114 
115 static void         le_copytobuf(struct lance_softc *, void *, int, int);
116 static void         le_copyfrombuf(struct lance_softc *, void *, int, int);
117 static void         le_zerobuf(struct lance_softc *, int, int);
118 
119 /* offsets for:        ID,   REGS,    MEM,  NVRAM */
120 static const int lestd[] = { 0, 0x4000, 0x8000, 0xC008 };
121 
122 static void
lewrcsr(struct lance_softc * sc,uint16_t port,uint16_t val)123 lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
124 {
125           struct le_softc *lesc = (struct le_softc *)sc;
126           bus_space_tag_t bst = lesc->sc_bst;
127           bus_space_handle_t bsh0 = lesc->sc_bsh0;
128           bus_space_handle_t bsh1 = lesc->sc_bsh1;
129 
130           do {
131                     bus_space_write_2(bst, bsh1, LER1_RAP, port);
132           } while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
133           do {
134                     bus_space_write_2(bst, bsh1, LER1_RDP, val);
135           } while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
136 }
137 
138 static uint16_t
lerdcsr(struct lance_softc * sc,uint16_t port)139 lerdcsr(struct lance_softc *sc, uint16_t port)
140 {
141           struct le_softc *lesc = (struct le_softc *)sc;
142           bus_space_tag_t bst = lesc->sc_bst;
143           bus_space_handle_t bsh0 = lesc->sc_bsh0;
144           bus_space_handle_t bsh1 = lesc->sc_bsh1;
145           uint16_t val;
146 
147           do {
148                     bus_space_write_2(bst, bsh1, LER1_RAP, port);
149           } while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
150           do {
151                     val = bus_space_read_2(bst, bsh1, LER1_RDP);
152           } while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
153 
154           return val;
155 }
156 
157 static int
lematch(device_t parent,cfdata_t cf,void * aux)158 lematch(device_t parent, cfdata_t cf, void *aux)
159 {
160           struct dio_attach_args *da = aux;
161 
162           if (da->da_id == DIO_DEVICE_ID_LAN)
163                     return 1;
164           return 0;
165 }
166 
167 /*
168  * Interface exists: make available by filling in network interface
169  * record.  System will initialize the interface when it is ready
170  * to accept packets.
171  */
172 static void
leattach(device_t parent,device_t self,void * aux)173 leattach(device_t parent, device_t self, void *aux)
174 {
175           struct le_softc *lesc = device_private(self);
176           struct lance_softc *sc = &lesc->sc_am7990.lsc;
177           struct dio_attach_args *da = aux;
178           bus_space_tag_t bst = da->da_bst;
179           bus_space_handle_t bsh0, bsh1, bsh2;
180           bus_size_t offset;
181           int i;
182 
183           sc->sc_dev = self;
184 
185           if (bus_space_map(bst, (bus_addr_t)dio_scodetopa(da->da_scode),
186               da->da_size, 0, &bsh0)) {
187                     aprint_error(": can't map LANCE registers\n");
188                     return;
189           }
190 
191           if (bus_space_subregion(bst, bsh0, lestd[1], LER1_SIZE, &bsh1)) {
192                     aprint_error(": can't subregion LANCE space\n");
193                     return;
194           }
195 
196           if (bus_space_subregion(bst, bsh0, lestd[2], LE_BUFSIZE, &bsh2)) {
197                     aprint_error(": can't subregion buffer space\n");
198                     return;
199           }
200 
201           lesc->sc_bst = bst;
202           lesc->sc_bsh0 = bsh0;
203           lesc->sc_bsh1 = bsh1;
204           lesc->sc_bsh2 = bsh2;
205 
206           bus_space_write_1(bst, bsh0, LER0_ID, 0xff);
207           DELAY(100);
208 
209           sc->sc_conf3 = LE_C3_BSWP;
210           sc->sc_addr = 0;
211           sc->sc_memsize = LE_BUFSIZE;
212 
213           /*
214            * Read the ethernet address off the board, one nibble at a time.
215            */
216           offset = lestd[3];
217           for (i = 0; i < sizeof(sc->sc_enaddr); i++) {
218                     sc->sc_enaddr[i] =
219                         (bus_space_read_1(bst, bsh0, ++offset) & 0xf) << 4;
220                     offset++;
221                     sc->sc_enaddr[i] |=
222                         (bus_space_read_1(bst, bsh0, ++offset) & 0xf);
223                     offset++;
224           }
225 
226           sc->sc_copytodesc = le_copytobuf;
227           sc->sc_copyfromdesc = le_copyfrombuf;
228           sc->sc_copytobuf = le_copytobuf;
229           sc->sc_copyfrombuf = le_copyfrombuf;
230           sc->sc_zerobuf = le_zerobuf;
231 
232           sc->sc_rdcsr = lerdcsr;
233           sc->sc_wrcsr = lewrcsr;
234           sc->sc_hwinit = NULL;
235 
236           am7990_config(&lesc->sc_am7990);
237 
238           /* Establish the interrupt handler. */
239           (void) dio_intr_establish(leintr, sc, da->da_ipl, ISRPRI_NET);
240           bus_space_write_1(bst, bsh0, LER0_STATUS, LE_IE);
241 }
242 
243 static int
leintr(void * arg)244 leintr(void *arg)
245 {
246           struct lance_softc *sc = arg;
247 #ifdef USELEDS
248           uint16_t isr;
249 
250           isr = lerdcsr(sc, LE_CSR0);
251 
252           if ((isr & LE_C0_INTR) == 0)
253                     return 0;
254 
255           if (isr & LE_C0_RINT)
256                     ledcontrol(0, 0, LED_LANRCV);
257 
258           if (isr & LE_C0_TINT)
259                     ledcontrol(0, 0, LED_LANXMT);
260 #endif /* USELEDS */
261 
262           return am7990_intr(sc);
263 }
264 
265 static void
le_copytobuf(struct lance_softc * sc,void * from,int boff,int len)266 le_copytobuf(struct lance_softc *sc, void *from, int boff, int len)
267 {
268           struct le_softc *lesc = (struct le_softc *)sc;
269 
270           bus_space_write_region_1(lesc->sc_bst, lesc->sc_bsh2, boff, from, len);
271 }
272 
273 static void
le_copyfrombuf(struct lance_softc * sc,void * to,int boff,int len)274 le_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len)
275 {
276           struct le_softc *lesc = (struct le_softc *)sc;
277 
278           bus_space_read_region_1(lesc->sc_bst, lesc->sc_bsh2, boff, to, len);
279 }
280 
281 static void
le_zerobuf(struct lance_softc * sc,int boff,int len)282 le_zerobuf(struct lance_softc *sc, int boff, int len)
283 {
284           struct le_softc *lesc = (struct le_softc *)sc;
285 
286           bus_space_set_region_1(lesc->sc_bst, lesc->sc_bsh2, boff, 0, len);
287 }
288