1169695Skan/*	$NetBSD: if_ie_sebuf.c,v 1.15 2008/04/28 20:23:37 martin Exp $	*/
2169695Skan
3169695Skan/*-
4169695Skan * Copyright (c) 1996 The NetBSD Foundation, Inc.
5169695Skan * All rights reserved.
6169695Skan *
7169695Skan * This code is derived from software contributed to The NetBSD Foundation
8169695Skan * by Gordon W. Ross.
9169695Skan *
10169695Skan * Redistribution and use in source and binary forms, with or without
11169695Skan * modification, are permitted provided that the following conditions
12169695Skan * are met:
13169695Skan * 1. Redistributions of source code must retain the above copyright
14169695Skan *    notice, this list of conditions and the following disclaimer.
15169695Skan * 2. Redistributions in binary form must reproduce the above copyright
16169695Skan *    notice, this list of conditions and the following disclaimer in the
17169695Skan *    documentation and/or other materials provided with the distribution.
18169695Skan *
19169695Skan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20169695Skan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21169695Skan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22169695Skan * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23169695Skan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24169695Skan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25169695Skan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26169695Skan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27169695Skan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28169695Skan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29169695Skan * POSSIBILITY OF SUCH DAMAGE.
30169695Skan */
31169695Skan
32169695Skan/*
33169695Skan * Machine-dependent glue for the Intel Ethernet (ie) driver,
34169695Skan * as found on the Sun3/E SCSI/Ethernet board.
35169695Skan */
36169695Skan
37169695Skan#include <sys/cdefs.h>
38259890Spfg__KERNEL_RCSID(0, "$NetBSD: if_ie_sebuf.c,v 1.15 2008/04/28 20:23:37 martin Exp $");
39169695Skan
40169695Skan#include "opt_inet.h"
41169695Skan
42169695Skan#include <sys/param.h>
43169695Skan#include <sys/systm.h>
44169695Skan#include <sys/device.h>
45259890Spfg#include <sys/protosw.h>
46169695Skan#include <sys/socket.h>
47169695Skan#include <net/if.h>
48169695Skan#include <net/if_ether.h>
49169695Skan
50169695Skan#ifdef INET
51169695Skan#include <netinet/in.h>
52169695Skan#include <netinet/in_systm.h>
53169695Skan#include <netinet/in_var.h>
54169695Skan#include <netinet/ip.h>
55169695Skan#include <netinet/if_inarp.h>
56169695Skan#endif
57259890Spfg
58169695Skan#include <machine/autoconf.h>
59169695Skan#include <machine/cpu.h>
60169695Skan#include <machine/dvma.h>
61169695Skan#include <machine/idprom.h>
62169695Skan#include <machine/vmparam.h>
63169695Skan
64169695Skan#include "i82586.h"
65169695Skan#include "if_iereg.h"
66169695Skan#include "if_ievar.h"
67169695Skan#include "sereg.h"
68169695Skan#include "sevar.h"
69169695Skan
70169695Skanstatic void ie_sebuf_reset(struct ie_softc *);
71169695Skanstatic void ie_sebuf_attend(struct ie_softc *);
72169695Skanstatic void ie_sebuf_run(struct ie_softc *);
73169695Skan
74169695Skan/*
75169695Skan * zero/copy functions: OBIO can use the normal functions, but VME
76169695Skan *    must do only byte or half-word (16 bit) accesses...
77169695Skan */
78169695Skanstatic void *wmemcpy(void *, const void *, size_t);
79259890Spfgstatic void *wmemset(void *, int, size_t);
80169695Skan
81169695Skan
82169695Skan/*
83169695Skan * New-style autoconfig attachment
84169695Skan */
85169695Skan
86169695Skanstatic int  ie_sebuf_match(device_t, cfdata_t, void *);
87169695Skanstatic void ie_sebuf_attach(device_t, device_t, void *);
88169695Skan
89259890SpfgCFATTACH_DECL_NEW(ie_sebuf, sizeof(struct ie_softc),
90169695Skan    ie_sebuf_match, ie_sebuf_attach, NULL, NULL);
91169695Skan
92169695Skanstatic int
93169695Skanie_sebuf_match(device_t parent, cfdata_t cf, void *args)
94169695Skan{
95169695Skan	struct sebuf_attach_args *aa = args;
96169695Skan
97169695Skan	/* Match by name. */
98169695Skan	if (strcmp(aa->name, "ie"))
99169695Skan		return 0;
100169695Skan
101169695Skan	/* Anyting else to check? */
102169695Skan
103169695Skan	return 1;
104169695Skan}
105169695Skan
106169695Skanstatic void
107169695Skanie_sebuf_attach(device_t parent, device_t self, void *args)
108169695Skan{
109169695Skan	struct ie_softc *sc = device_private(self);
110169695Skan	struct sebuf_attach_args *aa = args;
111169695Skan	volatile struct ie_regs *regs;
112169695Skan	int     off;
113169695Skan
114169695Skan	sc->sc_dev = self;
115169695Skan	sc->hard_type = IE_VME3E;
116169695Skan	sc->reset_586 = ie_sebuf_reset;
117169695Skan	sc->chan_attn = ie_sebuf_attend;
118169695Skan	sc->run_586   = ie_sebuf_run;
119169695Skan	sc->sc_memcpy = wmemcpy;
120169695Skan	sc->sc_memset = wmemset;
121169695Skan
122169695Skan	/* Control regs mapped by parent. */
123169695Skan	sc->sc_reg = aa->regs;
124169695Skan	regs = (struct ie_regs *)sc->sc_reg;
125169695Skan
126259890Spfg	/*
127169695Skan	 * On this hardware, the i82586 address zero
128169695Skan	 * maps to the start of the board, which we
129169695Skan	 * happen to know is 128K below (XXX).
130169695Skan	 */
131169695Skan	sc->sc_iobase = aa->buf - 0x20000;
132169695Skan
133169695Skan	/*
134169695Skan	 * There is 128K of memory for the i82586 on
135169695Skan	 * the Sun3/E SCSI/Ethernet board, and the
136169695Skan	 * "sebuf" driver has mapped it in for us.
137169695Skan	 * (Fixed in hardware; NOT configurable!)
138169695Skan	 */
139259890Spfg	if (aa->blen < SE_IEBUFSIZE)
140169695Skan		panic("ie_sebuf: bad size");
141169695Skan	sc->sc_msize = SE_IEBUFSIZE;
142169695Skan	sc->sc_maddr = aa->buf;
143169695Skan
144169695Skan	/* Clear the memory. */
145169695Skan	(sc->sc_memset)(sc->sc_maddr, 0, sc->sc_msize);
146169695Skan
147169695Skan	/*
148169695Skan	 * XXX:  Unfortunately, the common driver code
149169695Skan	 * is not ready to deal with more than 64K of
150169695Skan	 * memory, so skip the first 64K.  Too bad.
151169695Skan	 * Note: device needs the SCP at the end.
152169695Skan	 */
153169695Skan	sc->sc_msize -= 0x10000;
154169695Skan	sc->sc_maddr = (char *)sc->sc_maddr + 0x10000;
155169695Skan
156169695Skan	/*
157169695Skan	 * Set the System Configuration Pointer (SCP).
158169695Skan	 * Its location is system-dependent because the
159169695Skan	 * i82586 reads it from a fixed physical address.
160259890Spfg	 * On this hardware, the i82586 address is just
161169695Skan	 * masked down to 17 bits, so the SCP is found
162169695Skan	 * at the end of the RAM on the VME board.
163169695Skan	 */
164169695Skan	off = IE_SCP_ADDR & 0xFFFF;
165169695Skan	sc->scp = (volatile void *)((char *)sc->sc_maddr + off);
166169695Skan
167169695Skan	/*
168169695Skan	 * The rest of ram is used for buffers, etc.
169169695Skan	 */
170169695Skan	sc->buf_area = sc->sc_maddr;
171169695Skan	sc->buf_area_sz = off;
172169695Skan
173169695Skan	/* Install interrupt handler. */
174169695Skan	regs->ie_ivec = aa->ca.ca_intvec;
175169695Skan	isr_add_vectored(ie_intr, sc, aa->ca.ca_intpri, aa->ca.ca_intvec);
176169695Skan
177169695Skan	/* Set the ethernet address. */
178169695Skan	idprom_etheraddr(sc->sc_addr);
179169695Skan
180169695Skan	/* Do machine-independent parts of attach. */
181169695Skan	ie_attach(sc);
182169695Skan
183169695Skan}
184169695Skan
185169695Skan/* Whack the "channel attetion" line. */
186169695Skanvoid
187169695Skanie_sebuf_attend(struct ie_softc *sc)
188169695Skan{
189169695Skan	volatile struct ie_regs *regs = (struct ie_regs *)sc->sc_reg;
190169695Skan
191169695Skan	regs->ie_csr |= IE_CSR_ATTEN;	/* flag! */
192169695Skan	regs->ie_csr &= ~IE_CSR_ATTEN;	/* down. */
193169695Skan}
194169695Skan
195169695Skan/*
196169695Skan * This is called during driver attach.
197259890Spfg * Reset and initialize.
198169695Skan */
199169695Skanvoid
200259890Spfgie_sebuf_reset(struct ie_softc *sc)
201169695Skan{
202169695Skan	volatile struct ie_regs *regs = (struct ie_regs *)sc->sc_reg;
203169695Skan	regs->ie_csr = IE_CSR_RESET;
204169695Skan	delay(20);
205169695Skan	regs->ie_csr = (IE_CSR_NOLOOP | IE_CSR_IENAB);
206169695Skan}
207169695Skan
208169695Skan/*
209169695Skan * This is called at the end of ieinit().
210169695Skan * optional.
211169695Skan */
212169695Skanvoid
213259890Spfgie_sebuf_run(struct ie_softc *sc)
214169695Skan{
215169695Skan
216169695Skan	/* do it all in reset */
217169695Skan}
218169695Skan
219259890Spfg/*
220169695Skan * wmemcpy/wmemset - like memcpy/memset but largest access is 16-bits,
221169695Skan * and also does byte swaps...
222169695Skan * XXX - Would be nice to have asm versions in some library...
223169695Skan */
224169695Skan
225169695Skanstatic void *
226259890Spfgwmemset(void *vb, int val, size_t l)
227169695Skan{
228169695Skan	uint8_t *b = vb;
229169695Skan	uint8_t *be = b + l;
230169695Skan	uint16_t *sp;
231169695Skan
232169695Skan	if (l == 0)
233169695Skan		return vb;
234169695Skan
235169695Skan	/* front, */
236169695Skan	if ((uint32_t)b & 1)
237169695Skan		*b++ = val;
238169695Skan
239169695Skan	/* back, */
240169695Skan	if (b != be && ((uint32_t)be & 1) != 0) {
241169695Skan		be--;
242169695Skan		*be = val;
243169695Skan	}
244169695Skan
245169695Skan	/* and middle. */
246169695Skan	sp = (uint16_t *)b;
247169695Skan	while (sp != (uint16_t *)be)
248169695Skan		*sp++ = val;
249169695Skan
250169695Skan	return vb;
251169695Skan}
252259890Spfg
253169695Skanstatic void *
254169695Skanwmemcpy(void *dst, const void *src, size_t l)
255169695Skan{
256169695Skan	const uint8_t *b1e, *b1 = src;
257169695Skan	uint8_t *b2 = dst;
258259890Spfg	const uint16_t *sp;
259169695Skan	int bstore = 0;
260169695Skan
261169695Skan	if (l == 0)
262169695Skan		return dst;
263169695Skan
264169695Skan	/* front, */
265259890Spfg	if ((uint32_t)b1 & 1) {
266169695Skan		*b2++ = *b1++;
267169695Skan		l--;
268169695Skan	}
269169695Skan
270169695Skan	/* middle, */
271169695Skan	sp = (const uint16_t *)b1;
272169695Skan	b1e = b1 + l;
273169695Skan	if (l & 1)
274169695Skan		b1e--;
275169695Skan	bstore = (uint32_t)b2 & 1;
276169695Skan
277169695Skan	while (sp < (const uint16_t *)b1e) {
278169695Skan		if (bstore) {
279169695Skan			b2[1] = *sp & 0xff;
280169695Skan			b2[0] = *sp >> 8;
281169695Skan		} else
282169695Skan			*((uint16_t *)b2) = *sp;
283169695Skan		sp++;
284169695Skan		b2 += 2;
285169695Skan	}
286169695Skan
287169695Skan	/* and back. */
288169695Skan	if (l & 1)
289169695Skan		*b2 = *b1e;
290169695Skan
291169695Skan	return dst;
292169695Skan}
293169695Skan