1 /*        $NetBSD: if_ie_sebuf.c,v 1.17 2024/12/20 23:52:00 tsutsui Exp $       */
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Gordon W. Ross.
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  * Machine-dependent glue for the Intel Ethernet (ie) driver,
34  * as found on the Sun3/E SCSI/Ethernet board.
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: if_ie_sebuf.c,v 1.17 2024/12/20 23:52:00 tsutsui Exp $");
39 
40 #include "opt_inet.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <net/if.h>
48 #include <net/if_ether.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 #include <machine/autoconf.h>
59 #include <machine/cpu.h>
60 #include <machine/dvma.h>
61 #include <machine/idprom.h>
62 #include <machine/vmparam.h>
63 
64 #include "i82586.h"
65 #include "if_iereg.h"
66 #include "if_ievar.h"
67 #include "sereg.h"
68 #include "sevar.h"
69 
70 static void ie_sebuf_reset(struct ie_softc *);
71 static void ie_sebuf_attend(struct ie_softc *);
72 static void ie_sebuf_run(struct ie_softc *);
73 
74 /*
75  * zero/copy functions: OBIO can use the normal functions, but VME
76  *    must do only byte or half-word (16 bit) accesses...
77  */
78 static void *wmemcpy(void *, const void *, size_t);
79 static void *wmemset(void *, int, size_t);
80 
81 
82 /*
83  * New-style autoconfig attachment
84  */
85 
86 static int  ie_sebuf_match(device_t, cfdata_t, void *);
87 static void ie_sebuf_attach(device_t, device_t, void *);
88 
89 CFATTACH_DECL_NEW(ie_sebuf, sizeof(struct ie_softc),
90     ie_sebuf_match, ie_sebuf_attach, NULL, NULL);
91 
92 static int
ie_sebuf_match(device_t parent,cfdata_t cf,void * args)93 ie_sebuf_match(device_t parent, cfdata_t cf, void *args)
94 {
95           struct sebuf_attach_args *aa = args;
96 
97           /* Match by name. */
98           if (strcmp(aa->name, "ie"))
99                     return 0;
100 
101           /* Anyting else to check? */
102 
103           return 1;
104 }
105 
106 static void
ie_sebuf_attach(device_t parent,device_t self,void * args)107 ie_sebuf_attach(device_t parent, device_t self, void *args)
108 {
109           struct ie_softc *sc = device_private(self);
110           struct sebuf_attach_args *aa = args;
111           volatile struct ie_regs *regs;
112           int     off;
113 
114           sc->sc_dev = self;
115           sc->hard_type = IE_VME3E;
116           sc->reset_586 = ie_sebuf_reset;
117           sc->chan_attn = ie_sebuf_attend;
118           sc->run_586   = ie_sebuf_run;
119           sc->sc_memcpy = wmemcpy;
120           sc->sc_memset = wmemset;
121 
122           /* Control regs mapped by parent. */
123           sc->sc_reg = aa->regs;
124           regs = (struct ie_regs *)sc->sc_reg;
125 
126           /*
127            * On this hardware, the i82586 address zero
128            * maps to the start of the board, which we
129            * happen to know is 128K below (XXX).
130            */
131           sc->sc_iobase = aa->buf - 0x20000;
132 
133           /*
134            * There is 128K of memory for the i82586 on
135            * the Sun3/E SCSI/Ethernet board, and the
136            * "sebuf" driver has mapped it in for us.
137            * (Fixed in hardware; NOT configurable!)
138            */
139           if (aa->blen < SE_IEBUFSIZE)
140                     panic("ie_sebuf: bad size");
141           sc->sc_msize = SE_IEBUFSIZE;
142           sc->sc_maddr = aa->buf;
143 
144           /* Clear the memory. */
145           (sc->sc_memset)(sc->sc_maddr, 0, sc->sc_msize);
146 
147           /*
148            * XXX:  Unfortunately, the common driver code
149            * is not ready to deal with more than 64K of
150            * memory, so skip the first 64K.  Too bad.
151            * Note: device needs the SCP at the end.
152            */
153           sc->sc_msize -= 0x10000;
154           sc->sc_maddr = (char *)sc->sc_maddr + 0x10000;
155 
156           /*
157            * Set the System Configuration Pointer (SCP).
158            * Its location is system-dependent because the
159            * i82586 reads it from a fixed physical address.
160            * On this hardware, the i82586 address is just
161            * masked down to 17 bits, so the SCP is found
162            * at the end of the RAM on the VME board.
163            */
164           off = IE_SCP_ADDR & 0xFFFF;
165           sc->scp = (volatile void *)((char *)sc->sc_maddr + off);
166 
167           /*
168            * The rest of ram is used for buffers, etc.
169            */
170           sc->buf_area = sc->sc_maddr;
171           sc->buf_area_sz = off;
172 
173           /* Install interrupt handler. */
174           regs->ie_ivec = aa->ca.ca_intvec;
175           isr_add_vectored(ie_intr, sc, aa->ca.ca_intpri, aa->ca.ca_intvec);
176 
177           /* Set the ethernet address. */
178           idprom_etheraddr(sc->sc_addr);
179 
180           /* Do machine-independent parts of attach. */
181           ie_attach(sc);
182 
183 }
184 
185 /* Whack the "channel attetion" line. */
186 void
ie_sebuf_attend(struct ie_softc * sc)187 ie_sebuf_attend(struct ie_softc *sc)
188 {
189           volatile struct ie_regs *regs = (struct ie_regs *)sc->sc_reg;
190 
191           regs->ie_csr |= IE_CSR_ATTEN; /* flag! */
192           regs->ie_csr &= ~IE_CSR_ATTEN;          /* down. */
193 }
194 
195 /*
196  * This is called during driver attach.
197  * Reset and initialize.
198  */
199 void
ie_sebuf_reset(struct ie_softc * sc)200 ie_sebuf_reset(struct ie_softc *sc)
201 {
202           volatile struct ie_regs *regs = (struct ie_regs *)sc->sc_reg;
203           regs->ie_csr = IE_CSR_RESET;
204           delay(20);
205           regs->ie_csr = (IE_CSR_NOLOOP | IE_CSR_IENAB);
206 }
207 
208 /*
209  * This is called at the end of ieinit().
210  * optional.
211  */
212 void
ie_sebuf_run(struct ie_softc * sc)213 ie_sebuf_run(struct ie_softc *sc)
214 {
215 
216           /* do it all in reset */
217 }
218 
219 /*
220  * wmemcpy/wmemset - like memcpy/memset but largest access is 16-bits,
221  * and also does byte swaps...
222  * XXX - Would be nice to have asm versions in some library...
223  */
224 
225 static void *
wmemset(void * vb,int val,size_t l)226 wmemset(void *vb, int val, size_t l)
227 {
228           uint8_t *b = vb;
229           uint8_t *be = b + l;
230           uint16_t *sp;
231 
232           if (l == 0)
233                     return vb;
234 
235           /* front, */
236           if ((uint32_t)b & 1)
237                     *b++ = val;
238 
239           /* back, */
240           if (b != be && ((uint32_t)be & 1) != 0) {
241                     be--;
242                     *be = val;
243           }
244 
245           /* and middle. */
246           sp = (uint16_t *)b;
247           while (sp != (uint16_t *)be)
248                     *sp++ = val;
249 
250           return vb;
251 }
252 
253 static void *
wmemcpy(void * dst,const void * src,size_t l)254 wmemcpy(void *dst, const void *src, size_t l)
255 {
256           const uint8_t *b1e, *b1 = src;
257           uint8_t *b2 = dst;
258           const uint16_t *sp;
259           int bstore = 0;
260 
261           if (l == 0)
262                     return dst;
263 
264           /* front, */
265           if ((uint32_t)b1 & 1) {
266                     *b2++ = *b1++;
267                     l--;
268           }
269 
270           /* middle, */
271           sp = (const uint16_t *)b1;
272           b1e = b1 + l;
273           if (l & 1)
274                     b1e--;
275           bstore = (uint32_t)b2 & 1;
276 
277           while (sp < (const uint16_t *)b1e) {
278                     if (bstore) {
279                               b2[1] = *sp & 0xff;
280                               b2[0] = *sp >> 8;
281                     } else
282                               *((uint16_t *)b2) = *sp;
283                     sp++;
284                     b2 += 2;
285           }
286 
287           /* and back. */
288           if (l & 1)
289                     *b2 = *b1e;
290 
291           return dst;
292 }
293