1/*	$NetBSD: if_le.c,v 1.17 2008/04/28 20:23:30 martin 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 Adam Glass and 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#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.17 2008/04/28 20:23:30 martin Exp $");
34
35#include "opt_inet.h"
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/mbuf.h>
40#include <sys/syslog.h>
41#include <sys/socket.h>
42#include <sys/device.h>
43
44#include <net/if.h>
45#include <net/if_ether.h>
46#include <net/if_media.h>
47
48#ifdef INET
49#include <netinet/in.h>
50#include <netinet/if_inarp.h>
51#endif
52
53#include <machine/cpu.h>
54#include <machine/adrsmap.h>
55
56#include <dev/ic/lancereg.h>
57#include <dev/ic/lancevar.h>
58#include <dev/ic/am7990reg.h>
59#include <dev/ic/am7990var.h>
60
61#include <newsmips/dev/hbvar.h>
62
63/*
64 * LANCE registers.
65 * The real stuff is in dev/ic/am7990reg.h
66 */
67struct lereg1 {
68	volatile uint16_t	ler1_rdp;	/* data port */
69	volatile uint16_t	ler1_rap;	/* register select port */
70};
71
72/*
73 * Ethernet software status per interface.
74 * The real stuff is in dev/ic/am7990var.h
75 */
76struct	le_softc {
77	struct	am7990_softc sc_am7990;	/* glue to MI code */
78
79	struct	lereg1 *sc_r1;		/* LANCE registers */
80};
81
82static int	le_match(device_t, cfdata_t, void *);
83static void	le_attach(device_t, device_t, void *);
84
85CFATTACH_DECL_NEW(le, sizeof(struct le_softc),
86    le_match, le_attach, NULL, NULL);
87
88#if defined(_KERNEL_OPT)
89#include "opt_ddb.h"
90#endif
91
92#ifdef DDB
93#define	integrate
94#define hide
95#else
96#define	integrate	static inline
97#define hide		static
98#endif
99
100hide void lewrcsr(struct lance_softc *, uint16_t, uint16_t);
101hide uint16_t lerdcsr(struct lance_softc *, uint16_t);
102
103hide void
104lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
105{
106	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
107
108	ler1->ler1_rap = port;
109	ler1->ler1_rdp = val;
110}
111
112hide uint16_t
113lerdcsr(struct lance_softc *sc, uint16_t port)
114{
115	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
116	uint16_t val;
117
118	ler1->ler1_rap = port;
119	val = ler1->ler1_rdp;
120	return val;
121}
122
123int
124le_match(device_t parent, cfdata_t cf, void *aux)
125{
126	struct hb_attach_args *ha = aux;
127
128	if (strcmp(ha->ha_name, "le"))
129		return 0;
130
131	if (hb_badaddr((void *)ha->ha_addr, 1)) /* XXX */
132		return 0;
133
134	return 1;
135}
136
137void
138le_attach(device_t parent, device_t self, void *aux)
139{
140	struct le_softc *lesc = device_private(self);
141	struct lance_softc *sc = &lesc->sc_am7990.lsc;
142	struct hb_attach_args *ha = aux;
143	int intlevel, intmask;
144	uint8_t *p;
145
146	sc->sc_dev = self;
147	intlevel = ha->ha_level;
148	if (intlevel == -1) {
149		aprint_error(": interrupt level not configured\n");
150		return;
151	}
152	aprint_normal(" level %d", intlevel);
153
154	switch (ha->ha_addr) {
155
156	case LANCE_PORT:
157		sc->sc_mem = (void *)LANCE_MEMORY;
158		p = (uint8_t *)(ETHER_ID + 16);
159		intmask = INTST1_LANCE;
160		break;
161	case LANCE_PORT1:
162		sc->sc_mem = (void *)LANCE_MEMORY1;
163		p = (uint8_t *)(ETHER_ID1 + 16);
164		intmask = INTST1_SLOT3; /* XXX not tested */
165		break;
166	case LANCE_PORT2:
167		sc->sc_mem = (void *)LANCE_MEMORY2;
168		p = (uint8_t *)(ETHER_ID2 + 16);
169		intmask = INTST1_SLOT3; /* XXX not tested */
170		break;
171
172	default:
173		aprint_error(": unknown LANCE addr\n");
174		return;
175	}
176
177	lesc->sc_r1 = (void *)ha->ha_addr;
178
179	sc->sc_memsize = 0x4000;	/* 16K */
180	sc->sc_addr = (int)sc->sc_mem & 0x00ffffff;
181	sc->sc_conf3 = LE_C3_BSWP|LE_C3_BCON;
182
183	sc->sc_enaddr[0] = (*p++ << 4);
184	sc->sc_enaddr[0] |= *p++ & 0x0f;
185	sc->sc_enaddr[1] = (*p++ << 4);
186	sc->sc_enaddr[1] |= *p++ & 0x0f;
187	sc->sc_enaddr[2] = (*p++ << 4);
188	sc->sc_enaddr[2] |= *p++ & 0x0f;
189	sc->sc_enaddr[3] = (*p++ << 4);
190	sc->sc_enaddr[3] |= *p++ & 0x0f;
191	sc->sc_enaddr[4] = (*p++ << 4);
192	sc->sc_enaddr[4] |= *p++ & 0x0f;
193	sc->sc_enaddr[5] = (*p++ << 4);
194	sc->sc_enaddr[5] |= *p++ & 0x0f;
195
196	sc->sc_copytodesc = lance_copytobuf_contig;
197	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
198	sc->sc_copytobuf = lance_copytobuf_contig;
199	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
200	sc->sc_zerobuf = lance_zerobuf_contig;
201
202	sc->sc_rdcsr = lerdcsr;
203	sc->sc_wrcsr = lewrcsr;
204	sc->sc_hwinit = NULL;
205
206	am7990_config(&lesc->sc_am7990);
207	hb_intr_establish(intlevel, intmask, IPL_NET, am7990_intr, sc);
208}
209