xref: /netbsd-src/sys/dev/sbus/if_le_lebuffer.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: if_le_lebuffer.c,v 1.28 2010/01/19 22:07:43 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 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; Jason R. Thorpe of the Numerical Aerospace
9  * Simulation Facility, NASA Ames Research Center; Paul Kranenburg.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: if_le_lebuffer.c,v 1.28 2010/01/19 22:07:43 pooka Exp $");
35 
36 #include "opt_inet.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mbuf.h>
41 #include <sys/syslog.h>
42 #include <sys/socket.h>
43 #include <sys/device.h>
44 #include <sys/malloc.h>
45 
46 #include <net/if.h>
47 #include <net/if_ether.h>
48 #include <net/if_media.h>
49 
50 #ifdef INET
51 #include <netinet/in.h>
52 #include <netinet/if_inarp.h>
53 #endif
54 
55 #include <sys/bus.h>
56 #include <sys/intr.h>
57 #include <machine/autoconf.h>
58 
59 #include <dev/sbus/sbusvar.h>
60 #include <dev/sbus/lebuffervar.h>
61 
62 #include <dev/ic/lancereg.h>
63 #include <dev/ic/lancevar.h>
64 #include <dev/ic/am7990reg.h>
65 #include <dev/ic/am7990var.h>
66 
67 #include "ioconf.h"
68 
69 /*
70  * LANCE registers.
71  */
72 #define LEREG1_RDP	0	/* Register Data port */
73 #define LEREG1_RAP	2	/* Register Address port */
74 
75 struct	le_softc {
76 	struct	am7990_softc	sc_am7990;	/* glue to MI code */
77 	bus_space_tag_t		sc_bustag;
78 	bus_dma_tag_t		sc_dmatag;
79 	bus_space_handle_t	sc_reg;		/* LANCE registers */
80 };
81 
82 
83 int	lematch_lebuffer(device_t, cfdata_t, void *);
84 void	leattach_lebuffer(device_t, device_t, void *);
85 
86 /*
87  * Media types supported.
88  */
89 static int lemedia[] = {
90 	IFM_ETHER|IFM_10_T,
91 };
92 #define NLEMEDIA	__arraycount(lemedia)
93 
94 CFATTACH_DECL_NEW(le_lebuffer, sizeof(struct le_softc),
95     lematch_lebuffer, leattach_lebuffer, NULL, NULL);
96 
97 #if defined(_KERNEL_OPT)
98 #include "opt_ddb.h"
99 #endif
100 
101 static void lewrcsr(struct lance_softc *, uint16_t, uint16_t);
102 static uint16_t lerdcsr(struct lance_softc *, uint16_t);
103 
104 static void
105 lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
106 {
107 	struct le_softc *lesc = (struct le_softc *)sc;
108 	bus_space_tag_t t = lesc->sc_bustag;
109 	bus_space_handle_t h = lesc->sc_reg;
110 
111 	bus_space_write_2(t, h, LEREG1_RAP, port);
112 	bus_space_write_2(t, h, LEREG1_RDP, val);
113 
114 #if defined(SUN4M)
115 	/*
116 	 * We need to flush the Sbus->Mbus write buffers. This can most
117 	 * easily be accomplished by reading back the register that we
118 	 * just wrote (thanks to Chris Torek for this solution).
119 	 */
120 	(void)bus_space_read_2(t, h, LEREG1_RDP);
121 #endif
122 }
123 
124 static uint16_t
125 lerdcsr(struct lance_softc *sc, uint16_t port)
126 {
127 	struct le_softc *lesc = (struct le_softc *)sc;
128 	bus_space_tag_t t = lesc->sc_bustag;
129 	bus_space_handle_t h = lesc->sc_reg;
130 
131 	bus_space_write_2(t, h, LEREG1_RAP, port);
132 	return (bus_space_read_2(t, h, LEREG1_RDP));
133 }
134 
135 int
136 lematch_lebuffer(device_t parent, cfdata_t cf, void *aux)
137 {
138 	struct sbus_attach_args *sa = aux;
139 
140 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
141 }
142 
143 
144 void
145 leattach_lebuffer(device_t parent, device_t self, void *aux)
146 {
147 	struct le_softc *lesc = device_private(self);
148 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
149 	struct lebuf_softc *lebuf = device_private(parent);
150 	struct sbus_attach_args *sa = aux;
151 
152 	sc->sc_dev = self;
153 	lesc->sc_bustag = sa->sa_bustag;
154 	lesc->sc_dmatag = sa->sa_dmatag;
155 
156 	if (sbus_bus_map(sa->sa_bustag,
157 			 sa->sa_slot,
158 			 sa->sa_offset,
159 			 sa->sa_size,
160 			 0, &lesc->sc_reg)) {
161 		aprint_error(": cannot map registers\n");
162 		return;
163 	}
164 
165 	sc->sc_mem = lebuf->sc_buffer;
166 	sc->sc_memsize = lebuf->sc_bufsiz;
167 	sc->sc_addr = 0; /* Lance view is offset by buffer location */
168 	lebuf->attached = 1;
169 
170 	/* That old black magic... */
171 	sc->sc_conf3 = prom_getpropint(sa->sa_node, "busmaster-regval",
172 				  LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
173 
174 	sc->sc_supmedia = lemedia;
175 	sc->sc_nsupmedia = NLEMEDIA;
176 	sc->sc_defaultmedia = lemedia[0];
177 
178 	prom_getether(sa->sa_node, sc->sc_enaddr);
179 
180 	sc->sc_copytodesc = lance_copytobuf_contig;
181 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
182 	sc->sc_copytobuf = lance_copytobuf_contig;
183 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
184 	sc->sc_zerobuf = lance_zerobuf_contig;
185 
186 	sc->sc_rdcsr = lerdcsr;
187 	sc->sc_wrcsr = lewrcsr;
188 
189 	am7990_config(&lesc->sc_am7990);
190 
191 	/* Establish interrupt handler */
192 	if (sa->sa_nintr != 0)
193 		(void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri,
194 					 IPL_NET, am7990_intr, sc);
195 }
196