xref: /openbsd-src/sys/dev/tc/if_le_ioasic.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: if_le_ioasic.c,v 1.6 2001/06/27 04:45:59 art Exp $	*/
2 /*	$NetBSD: if_le_ioasic.c,v 1.2 1996/05/07 02:24:56 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1996 Carnegie-Mellon University.
6  * All rights reserved.
7  *
8  * Author: Chris G. Demetriou
9  *
10  * Permission to use, copy, modify and distribute this software and
11  * its documentation is hereby granted, provided that both the copyright
12  * notice and this permission notice appear in all copies of the
13  * software, derivative works or modified versions, and any portions
14  * thereof, and that both notices appear in supporting documentation.
15  *
16  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
18  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19  *
20  * Carnegie Mellon requests users of this software to return to
21  *
22  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
23  *  School of Computer Science
24  *  Carnegie Mellon University
25  *  Pittsburgh PA 15213-3890
26  *
27  * any improvements or extensions that they make and grant Carnegie the
28  * rights to redistribute these changes.
29  */
30 
31 /*
32  * LANCE on DEC IOCTL ASIC.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/mbuf.h>
38 #include <sys/syslog.h>
39 #include <sys/socket.h>
40 #include <sys/device.h>
41 
42 #include <net/if.h>
43 #include <net/if_media.h>
44 
45 #include <vm/vm.h>
46 #include <uvm/uvm_extern.h>
47 
48 #ifdef INET
49 #include <netinet/in.h>
50 #include <netinet/if_ether.h>
51 #endif
52 
53 #include <dev/ic/am7990reg.h>
54 #include <dev/ic/am7990var.h>
55 
56 #include <dev/tc/if_levar.h>
57 #include <dev/tc/tcvar.h>
58 #include <dev/tc/ioasicvar.h>
59 
60 int	le_ioasic_match __P((struct device *, void *, void *));
61 void	le_ioasic_attach __P((struct device *, struct device *, void *));
62 
63 hide void le_ioasic_copytobuf_gap2 __P((struct am7990_softc *, void *,
64 	    int, int));
65 hide void le_ioasic_copyfrombuf_gap2 __P((struct am7990_softc *, void *,
66 	    int, int));
67 
68 hide void le_ioasic_copytobuf_gap16 __P((struct am7990_softc *, void *,
69 	    int, int));
70 hide void le_ioasic_copyfrombuf_gap16 __P((struct am7990_softc *, void *,
71 	    int, int));
72 hide void le_ioasic_zerobuf_gap16 __P((struct am7990_softc *, int, int));
73 
74 struct cfattach le_ioasic_ca = {
75 	sizeof(struct le_softc), le_ioasic_match, le_ioasic_attach
76 };
77 
78 int
79 le_ioasic_match(parent, match, aux)
80 	struct device *parent;
81 	void *match, *aux;
82 {
83 	struct ioasicdev_attach_args *d = aux;
84 
85 	if (!ioasic_submatch(match, aux))
86 		return (0);
87 	if (strncmp("lance", d->iada_modname, TC_ROM_LLEN))
88 		return (0);
89 
90 	return (1);
91 }
92 
93 #define LE_IOASIC_MEMSIZE	(128*1024)
94 #define LE_IOASIC_MEMALIGN	(128*1024)
95 void
96 le_ioasic_attach(parent, self, aux)
97 	struct device *parent, *self;
98 	void *aux;
99 {
100 	struct ioasicdev_attach_args *d = aux;
101 	register struct le_softc *lesc = (void *)self;
102 	register struct am7990_softc *sc = &lesc->sc_am7990;
103 	caddr_t le_iomem;
104 
105 	le_iomem = (caddr_t)uvm_pagealloc_contig(LE_IOASIC_MEMSIZE, 0, 0, LE_IOASIC_MEMALIGN);
106 
107 	lesc->sc_r1 = (struct lereg1 *)
108 		TC_DENSE_TO_SPARSE(TC_PHYS_TO_UNCACHED(d->iada_addr));
109 	sc->sc_mem = (void *)TC_PHYS_TO_UNCACHED(le_iomem);
110 
111 	sc->sc_copytodesc = le_ioasic_copytobuf_gap2;
112 	sc->sc_copyfromdesc = le_ioasic_copyfrombuf_gap2;
113 	sc->sc_copytobuf = le_ioasic_copytobuf_gap16;
114 	sc->sc_copyfrombuf = le_ioasic_copyfrombuf_gap16;
115 	sc->sc_zerobuf = le_ioasic_zerobuf_gap16;
116 
117 	ioasic_lance_dma_setup(le_iomem);	/* XXX more thought */
118 
119 	dec_le_common_attach(sc, ioasic_lance_ether_address());
120 
121 	ioasic_intr_establish(parent, d->iada_cookie, TC_IPL_NET,
122 	    am7990_intr, sc);
123 }
124 
125 /*
126  * Special memory access functions needed by ioasic-attached LANCE
127  * chips.
128  */
129 
130 /*
131  * gap2: two bytes of data followed by two bytes of pad.
132  *
133  * Buffers must be 4-byte aligned.  The code doesn't worry about
134  * doing an extra byte.
135  */
136 
137 void
138 le_ioasic_copytobuf_gap2(sc, fromv, boff, len)
139 	struct am7990_softc *sc;
140 	void *fromv;
141 	int boff;
142 	register int len;
143 {
144 	volatile caddr_t buf = sc->sc_mem;
145 	register caddr_t from = fromv;
146 	register volatile u_int16_t *bptr;
147 
148 	if (boff & 0x1) {
149 		/* handle unaligned first byte */
150 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
151 		*bptr = (*from++ << 8) | (*bptr & 0xff);
152 		bptr += 2;
153 		len--;
154 	} else
155 		bptr = ((volatile u_int16_t *)buf) + boff;
156 	while (len > 1) {
157 		*bptr = (from[1] << 8) | (from[0] & 0xff);
158 		bptr += 2;
159 		from += 2;
160 		len -= 2;
161 	}
162 	if (len == 1)
163 		*bptr = (u_int16_t)*from;
164 }
165 
166 void
167 le_ioasic_copyfrombuf_gap2(sc, tov, boff, len)
168 	struct am7990_softc *sc;
169 	void *tov;
170 	int boff, len;
171 {
172 	volatile caddr_t buf = sc->sc_mem;
173 	register caddr_t to = tov;
174 	register volatile u_int16_t *bptr;
175 	register u_int16_t tmp;
176 
177 	if (boff & 0x1) {
178 		/* handle unaligned first byte */
179 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
180 		*to++ = (*bptr >> 8) & 0xff;
181 		bptr += 2;
182 		len--;
183 	} else
184 		bptr = ((volatile u_int16_t *)buf) + boff;
185 	while (len > 1) {
186 		tmp = *bptr;
187 		*to++ = tmp & 0xff;
188 		*to++ = (tmp >> 8) & 0xff;
189 		bptr += 2;
190 		len -= 2;
191 	}
192 	if (len == 1)
193 		*to = *bptr & 0xff;
194 }
195 
196 /*
197  * gap16: 16 bytes of data followed by 16 bytes of pad.
198  *
199  * Buffers must be 32-byte aligned.
200  */
201 
202 void
203 le_ioasic_copytobuf_gap16(sc, fromv, boff, len)
204 	struct am7990_softc *sc;
205 	void *fromv;
206 	int boff;
207 	register int len;
208 {
209 	volatile caddr_t buf = sc->sc_mem;
210 	register caddr_t from = fromv;
211 	register caddr_t bptr;
212 	register int xfer;
213 
214 	bptr = buf + ((boff << 1) & ~0x1f);
215 	boff &= 0xf;
216 	xfer = min(len, 16 - boff);
217 	while (len > 0) {
218 		bcopy(from, bptr + boff, xfer);
219 		from += xfer;
220 		bptr += 32;
221 		boff = 0;
222 		len -= xfer;
223 		xfer = min(len, 16);
224 	}
225 }
226 
227 void
228 le_ioasic_copyfrombuf_gap16(sc, tov, boff, len)
229 	struct am7990_softc *sc;
230 	void *tov;
231 	int boff, len;
232 {
233 	volatile caddr_t buf = sc->sc_mem;
234 	register caddr_t to = tov;
235 	register caddr_t bptr;
236 	register int xfer;
237 
238 	bptr = buf + ((boff << 1) & ~0x1f);
239 	boff &= 0xf;
240 	xfer = min(len, 16 - boff);
241 	while (len > 0) {
242 		bcopy(bptr + boff, to, xfer);
243 		to += xfer;
244 		bptr += 32;
245 		boff = 0;
246 		len -= xfer;
247 		xfer = min(len, 16);
248 	}
249 }
250 
251 void
252 le_ioasic_zerobuf_gap16(sc, boff, len)
253 	struct am7990_softc *sc;
254 	int boff, len;
255 {
256 	volatile caddr_t buf = sc->sc_mem;
257 	register caddr_t bptr;
258 	register int xfer;
259 
260 	bptr = buf + ((boff << 1) & ~0x1f);
261 	boff &= 0xf;
262 	xfer = min(len, 16 - boff);
263 	while (len > 0) {
264 		bzero(bptr + boff, xfer);
265 		bptr += 32;
266 		boff = 0;
267 		len -= xfer;
268 		xfer = min(len, 16);
269 	}
270 }
271