at91emac.c revision 1.1.16.1 1 1.1.16.1 mjf /* $Id: at91emac.c,v 1.1.16.1 2008/09/28 10:39:48 mjf Exp $ */
2 1.1.16.1 mjf /* $NetBSD: at91emac.c,v 1.1.16.1 2008/09/28 10:39:48 mjf Exp $ */
3 1.1.16.1 mjf
4 1.1.16.1 mjf /*
5 1.1.16.1 mjf * Copyright (c) 2007 Embedtronics Oy
6 1.1.16.1 mjf * All rights reserved.
7 1.1.16.1 mjf *
8 1.1.16.1 mjf * Based on arch/arm/ep93xx/epe.c
9 1.1.16.1 mjf *
10 1.1.16.1 mjf * Copyright (c) 2004 Jesse Off
11 1.1.16.1 mjf * All rights reserved.
12 1.1.16.1 mjf *
13 1.1.16.1 mjf * Redistribution and use in source and binary forms, with or without
14 1.1.16.1 mjf * modification, are permitted provided that the following conditions
15 1.1.16.1 mjf * are met:
16 1.1.16.1 mjf * 1. Redistributions of source code must retain the above copyright
17 1.1.16.1 mjf * notice, this list of conditions and the following disclaimer.
18 1.1.16.1 mjf * 2. Redistributions in binary form must reproduce the above copyright
19 1.1.16.1 mjf * notice, this list of conditions and the following disclaimer in the
20 1.1.16.1 mjf * documentation and/or other materials provided with the distribution.
21 1.1.16.1 mjf * 3. All advertising materials mentioning features or use of this software
22 1.1.16.1 mjf * must display the following acknowledgement:
23 1.1.16.1 mjf * This product includes software developed by the NetBSD
24 1.1.16.1 mjf * Foundation, Inc. and its contributors.
25 1.1.16.1 mjf * 4. Neither the name of The NetBSD Foundation nor the names of its
26 1.1.16.1 mjf * contributors may be used to endorse or promote products derived
27 1.1.16.1 mjf * from this software without specific prior written permission.
28 1.1.16.1 mjf *
29 1.1.16.1 mjf * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 1.1.16.1 mjf * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 1.1.16.1 mjf * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 1.1.16.1 mjf * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 1.1.16.1 mjf * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 1.1.16.1 mjf * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 1.1.16.1 mjf * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 1.1.16.1 mjf * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 1.1.16.1 mjf * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 1.1.16.1 mjf * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 1.1.16.1 mjf * POSSIBILITY OF SUCH DAMAGE.
40 1.1.16.1 mjf */
41 1.1.16.1 mjf
42 1.1.16.1 mjf #include <sys/cdefs.h>
43 1.1.16.1 mjf __KERNEL_RCSID(0, "$NetBSD: at91emac.c,v 1.1.16.1 2008/09/28 10:39:48 mjf Exp $");
44 1.1.16.1 mjf
45 1.1.16.1 mjf #include <sys/types.h>
46 1.1.16.1 mjf #include <sys/param.h>
47 1.1.16.1 mjf #include <sys/systm.h>
48 1.1.16.1 mjf #include <sys/ioctl.h>
49 1.1.16.1 mjf #include <sys/kernel.h>
50 1.1.16.1 mjf #include <sys/proc.h>
51 1.1.16.1 mjf #include <sys/malloc.h>
52 1.1.16.1 mjf #include <sys/time.h>
53 1.1.16.1 mjf #include <sys/device.h>
54 1.1.16.1 mjf #include <uvm/uvm_extern.h>
55 1.1.16.1 mjf
56 1.1.16.1 mjf #include <machine/bus.h>
57 1.1.16.1 mjf #include <machine/intr.h>
58 1.1.16.1 mjf
59 1.1.16.1 mjf #include <arm/cpufunc.h>
60 1.1.16.1 mjf
61 1.1.16.1 mjf #include <net/if.h>
62 1.1.16.1 mjf #include <net/if_dl.h>
63 1.1.16.1 mjf #include <net/if_types.h>
64 1.1.16.1 mjf #include <net/if_media.h>
65 1.1.16.1 mjf #include <net/if_ether.h>
66 1.1.16.1 mjf
67 1.1.16.1 mjf #include <dev/mii/mii.h>
68 1.1.16.1 mjf #include <dev/mii/miivar.h>
69 1.1.16.1 mjf
70 1.1.16.1 mjf #ifdef INET
71 1.1.16.1 mjf #include <netinet/in.h>
72 1.1.16.1 mjf #include <netinet/in_systm.h>
73 1.1.16.1 mjf #include <netinet/in_var.h>
74 1.1.16.1 mjf #include <netinet/ip.h>
75 1.1.16.1 mjf #include <netinet/if_inarp.h>
76 1.1.16.1 mjf #endif
77 1.1.16.1 mjf
78 1.1.16.1 mjf #ifdef NS
79 1.1.16.1 mjf #include <netns/ns.h>
80 1.1.16.1 mjf #include <netns/ns_if.h>
81 1.1.16.1 mjf #endif
82 1.1.16.1 mjf
83 1.1.16.1 mjf #include "bpfilter.h"
84 1.1.16.1 mjf #if NBPFILTER > 0
85 1.1.16.1 mjf #include <net/bpf.h>
86 1.1.16.1 mjf #include <net/bpfdesc.h>
87 1.1.16.1 mjf #endif
88 1.1.16.1 mjf
89 1.1.16.1 mjf #ifdef IPKDB_AT91 // @@@
90 1.1.16.1 mjf #include <ipkdb/ipkdb.h>
91 1.1.16.1 mjf #endif
92 1.1.16.1 mjf
93 1.1.16.1 mjf #include <arm/at91/at91var.h>
94 1.1.16.1 mjf #include <arm/at91/at91emacreg.h>
95 1.1.16.1 mjf #include <arm/at91/at91emacvar.h>
96 1.1.16.1 mjf
97 1.1.16.1 mjf #define DEFAULT_MDCDIV 32
98 1.1.16.1 mjf
99 1.1.16.1 mjf #ifndef EMAC_FAST
100 1.1.16.1 mjf #define EMAC_FAST
101 1.1.16.1 mjf #endif
102 1.1.16.1 mjf
103 1.1.16.1 mjf #ifndef EMAC_FAST
104 1.1.16.1 mjf #define EMAC_READ(x) \
105 1.1.16.1 mjf bus_space_read_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x))
106 1.1.16.1 mjf #define EMAC_WRITE(x, y) \
107 1.1.16.1 mjf bus_space_write_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x), (y))
108 1.1.16.1 mjf #else
109 1.1.16.1 mjf #define EMAC_READ(x) ETHREG(x)
110 1.1.16.1 mjf #define EMAC_WRITE(x, y) ETHREG(x) = (y)
111 1.1.16.1 mjf #endif /* ! EMAC_FAST */
112 1.1.16.1 mjf
113 1.1.16.1 mjf static int emac_match(device_t, cfdata_t, void *);
114 1.1.16.1 mjf static void emac_attach(device_t, device_t, void *);
115 1.1.16.1 mjf static void emac_init(struct emac_softc *);
116 1.1.16.1 mjf static int emac_intr(void* arg);
117 1.1.16.1 mjf static int emac_gctx(struct emac_softc *);
118 1.1.16.1 mjf static int emac_mediachange(struct ifnet *);
119 1.1.16.1 mjf static void emac_mediastatus(struct ifnet *, struct ifmediareq *);
120 1.1.16.1 mjf int emac_mii_readreg (device_t, int, int);
121 1.1.16.1 mjf void emac_mii_writereg (device_t, int, int, int);
122 1.1.16.1 mjf void emac_statchg (device_t );
123 1.1.16.1 mjf void emac_tick (void *);
124 1.1.16.1 mjf static int emac_ifioctl (struct ifnet *, u_long, void *);
125 1.1.16.1 mjf static void emac_ifstart (struct ifnet *);
126 1.1.16.1 mjf static void emac_ifwatchdog (struct ifnet *);
127 1.1.16.1 mjf static int emac_ifinit (struct ifnet *);
128 1.1.16.1 mjf static void emac_ifstop (struct ifnet *, int);
129 1.1.16.1 mjf static void emac_setaddr (struct ifnet *);
130 1.1.16.1 mjf
131 1.1.16.1 mjf CFATTACH_DECL(at91emac, sizeof(struct emac_softc),
132 1.1.16.1 mjf emac_match, emac_attach, NULL, NULL);
133 1.1.16.1 mjf
134 1.1.16.1 mjf #ifdef EMAC_DEBUG
135 1.1.16.1 mjf int emac_debug = EMAC_DEBUG;
136 1.1.16.1 mjf #define DPRINTFN(n,fmt) if (emac_debug >= (n)) printf fmt
137 1.1.16.1 mjf #else
138 1.1.16.1 mjf #define DPRINTFN(n,fmt)
139 1.1.16.1 mjf #endif
140 1.1.16.1 mjf
141 1.1.16.1 mjf static int
142 1.1.16.1 mjf emac_match(device_t parent, cfdata_t match, void *aux)
143 1.1.16.1 mjf {
144 1.1.16.1 mjf if (strcmp(match->cf_name, "at91emac") == 0)
145 1.1.16.1 mjf return 2;
146 1.1.16.1 mjf return 0;
147 1.1.16.1 mjf }
148 1.1.16.1 mjf
149 1.1.16.1 mjf static void
150 1.1.16.1 mjf emac_attach(device_t parent, device_t self, void *aux)
151 1.1.16.1 mjf {
152 1.1.16.1 mjf struct emac_softc *sc = device_private(self);
153 1.1.16.1 mjf struct at91bus_attach_args *sa = aux;
154 1.1.16.1 mjf prop_data_t enaddr;
155 1.1.16.1 mjf uint32_t u;
156 1.1.16.1 mjf
157 1.1.16.1 mjf printf("\n");
158 1.1.16.1 mjf sc->sc_dev = self;
159 1.1.16.1 mjf sc->sc_iot = sa->sa_iot;
160 1.1.16.1 mjf sc->sc_pid = sa->sa_pid;
161 1.1.16.1 mjf sc->sc_dmat = sa->sa_dmat;
162 1.1.16.1 mjf
163 1.1.16.1 mjf if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh))
164 1.1.16.1 mjf panic("%s: Cannot map registers", device_xname(self));
165 1.1.16.1 mjf
166 1.1.16.1 mjf /* enable peripheral clock */
167 1.1.16.1 mjf at91_peripheral_clock(sc->sc_pid, 1);
168 1.1.16.1 mjf
169 1.1.16.1 mjf /* configure emac: */
170 1.1.16.1 mjf EMAC_WRITE(ETH_CTL, 0); // disable everything
171 1.1.16.1 mjf EMAC_WRITE(ETH_IDR, -1); // disable interrupts
172 1.1.16.1 mjf EMAC_WRITE(ETH_RBQP, 0); // clear receive
173 1.1.16.1 mjf EMAC_WRITE(ETH_CFG, ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
174 1.1.16.1 mjf EMAC_WRITE(ETH_TCR, 0); // send nothing
175 1.1.16.1 mjf //(void)EMAC_READ(ETH_ISR);
176 1.1.16.1 mjf u = EMAC_READ(ETH_TSR);
177 1.1.16.1 mjf EMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
178 1.1.16.1 mjf | ETH_TSR_IDLE | ETH_TSR_RLE
179 1.1.16.1 mjf | ETH_TSR_COL|ETH_TSR_OVR)));
180 1.1.16.1 mjf u = EMAC_READ(ETH_RSR);
181 1.1.16.1 mjf EMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR|ETH_RSR_REC|ETH_RSR_BNA)));
182 1.1.16.1 mjf
183 1.1.16.1 mjf /* Fetch the Ethernet address from property if set. */
184 1.1.16.1 mjf enaddr = prop_dictionary_get(device_properties(self), "mac-addr");
185 1.1.16.1 mjf
186 1.1.16.1 mjf if (enaddr != NULL) {
187 1.1.16.1 mjf KASSERT(prop_object_type(enaddr) == PROP_TYPE_DATA);
188 1.1.16.1 mjf KASSERT(prop_data_size(enaddr) == ETHER_ADDR_LEN);
189 1.1.16.1 mjf memcpy(sc->sc_enaddr, prop_data_data_nocopy(enaddr),
190 1.1.16.1 mjf ETHER_ADDR_LEN);
191 1.1.16.1 mjf } else {
192 1.1.16.1 mjf static const uint8_t hardcoded[ETHER_ADDR_LEN] = {
193 1.1.16.1 mjf 0x00, 0x0d, 0x10, 0x81, 0x0c, 0x94
194 1.1.16.1 mjf };
195 1.1.16.1 mjf memcpy(sc->sc_enaddr, hardcoded, ETHER_ADDR_LEN);
196 1.1.16.1 mjf }
197 1.1.16.1 mjf
198 1.1.16.1 mjf at91_intr_establish(sc->sc_pid, IPL_NET, INTR_HIGH_LEVEL, emac_intr, sc);
199 1.1.16.1 mjf emac_init(sc);
200 1.1.16.1 mjf }
201 1.1.16.1 mjf
202 1.1.16.1 mjf static int
203 1.1.16.1 mjf emac_gctx(struct emac_softc *sc)
204 1.1.16.1 mjf {
205 1.1.16.1 mjf struct ifnet * ifp = &sc->sc_ec.ec_if;
206 1.1.16.1 mjf u_int32_t tsr;
207 1.1.16.1 mjf
208 1.1.16.1 mjf tsr = EMAC_READ(ETH_TSR);
209 1.1.16.1 mjf if (!(tsr & ETH_TSR_BNQ)) {
210 1.1.16.1 mjf // no space left
211 1.1.16.1 mjf return 0;
212 1.1.16.1 mjf }
213 1.1.16.1 mjf
214 1.1.16.1 mjf // free sent frames
215 1.1.16.1 mjf while (sc->txqc > (tsr & ETH_TSR_IDLE ? 0 : 1)) {
216 1.1.16.1 mjf int i = sc->txqi % TX_QLEN;
217 1.1.16.1 mjf bus_dmamap_sync(sc->sc_dmat, sc->txq[i].m_dmamap, 0,
218 1.1.16.1 mjf sc->txq[i].m->m_pkthdr.len, BUS_DMASYNC_POSTWRITE);
219 1.1.16.1 mjf bus_dmamap_unload(sc->sc_dmat, sc->txq[i].m_dmamap);
220 1.1.16.1 mjf m_freem(sc->txq[i].m);
221 1.1.16.1 mjf DPRINTFN(2,("%s: freed idx #%i mbuf %p (txqc=%i)\n", __FUNCTION__, i, sc->txq[i].m, sc->txqc));
222 1.1.16.1 mjf sc->txq[i].m = NULL;
223 1.1.16.1 mjf sc->txqi = (i + 1) % TX_QLEN;
224 1.1.16.1 mjf sc->txqc--;
225 1.1.16.1 mjf }
226 1.1.16.1 mjf
227 1.1.16.1 mjf // mark we're free
228 1.1.16.1 mjf if (ifp->if_flags & IFF_OACTIVE) {
229 1.1.16.1 mjf ifp->if_flags &= ~IFF_OACTIVE;
230 1.1.16.1 mjf /* Disable transmit-buffer-free interrupt */
231 1.1.16.1 mjf /*EMAC_WRITE(ETH_IDR, ETH_ISR_TBRE);*/
232 1.1.16.1 mjf }
233 1.1.16.1 mjf
234 1.1.16.1 mjf return 1;
235 1.1.16.1 mjf }
236 1.1.16.1 mjf
237 1.1.16.1 mjf static int
238 1.1.16.1 mjf emac_intr(void *arg)
239 1.1.16.1 mjf {
240 1.1.16.1 mjf struct emac_softc *sc = (struct emac_softc *)arg;
241 1.1.16.1 mjf struct ifnet * ifp = &sc->sc_ec.ec_if;
242 1.1.16.1 mjf u_int32_t imr, isr, rsr, ctl;
243 1.1.16.1 mjf int bi;
244 1.1.16.1 mjf
245 1.1.16.1 mjf imr = ~EMAC_READ(ETH_IMR);
246 1.1.16.1 mjf if (!(imr & (ETH_ISR_RCOM|ETH_ISR_TBRE|ETH_ISR_TIDLE|ETH_ISR_RBNA|ETH_ISR_ROVR))) {
247 1.1.16.1 mjf // interrupt not enabled, can't be us
248 1.1.16.1 mjf return 0;
249 1.1.16.1 mjf }
250 1.1.16.1 mjf
251 1.1.16.1 mjf isr = EMAC_READ(ETH_ISR) & imr;
252 1.1.16.1 mjf rsr = EMAC_READ(ETH_RSR); // get receive status register
253 1.1.16.1 mjf
254 1.1.16.1 mjf DPRINTFN(2, ("%s: isr=0x%08X rsr=0x%08X imr=0x%08X\n", __FUNCTION__, isr, rsr, imr));
255 1.1.16.1 mjf
256 1.1.16.1 mjf if (isr & ETH_ISR_RBNA) { // out of receive buffers
257 1.1.16.1 mjf EMAC_WRITE(ETH_RSR, ETH_RSR_BNA); // clear interrupt
258 1.1.16.1 mjf ctl = EMAC_READ(ETH_CTL); // get current control register value
259 1.1.16.1 mjf EMAC_WRITE(ETH_CTL, ctl & ~ETH_CTL_RE); // disable receiver
260 1.1.16.1 mjf EMAC_WRITE(ETH_RSR, ETH_RSR_BNA); // clear BNA bit
261 1.1.16.1 mjf EMAC_WRITE(ETH_CTL, ctl | ETH_CTL_RE); // re-enable receiver
262 1.1.16.1 mjf ifp->if_ierrors++;
263 1.1.16.1 mjf ifp->if_ipackets++;
264 1.1.16.1 mjf DPRINTFN(1,("%s: out of receive buffers\n", __FUNCTION__));
265 1.1.16.1 mjf }
266 1.1.16.1 mjf if (isr & ETH_ISR_ROVR) {
267 1.1.16.1 mjf EMAC_WRITE(ETH_RSR, ETH_RSR_OVR); // clear interrupt
268 1.1.16.1 mjf ifp->if_ierrors++;
269 1.1.16.1 mjf ifp->if_ipackets++;
270 1.1.16.1 mjf DPRINTFN(1,("%s: receive overrun\n", __FUNCTION__));
271 1.1.16.1 mjf }
272 1.1.16.1 mjf
273 1.1.16.1 mjf if (isr & ETH_ISR_RCOM) { // packet has been received!
274 1.1.16.1 mjf uint32_t nfo;
275 1.1.16.1 mjf // @@@ if memory is NOT coherent, then we're in trouble @@@@
276 1.1.16.1 mjf // bus_dmamap_sync(sc->sc_dmat, sc->rbqpage_dmamap, 0, sc->rbqlen, BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
277 1.1.16.1 mjf // printf("## RDSC[%i].ADDR=0x%08X\n", sc->rxqi % RX_QLEN, sc->RDSC[sc->rxqi % RX_QLEN].Addr);
278 1.1.16.1 mjf DPRINTFN(2,("#2 RDSC[%i].INFO=0x%08X\n", sc->rxqi % RX_QLEN, sc->RDSC[sc->rxqi % RX_QLEN].Info));
279 1.1.16.1 mjf while (sc->RDSC[(bi = sc->rxqi % RX_QLEN)].Addr & ETH_RDSC_F_USED) {
280 1.1.16.1 mjf int fl;
281 1.1.16.1 mjf struct mbuf *m;
282 1.1.16.1 mjf
283 1.1.16.1 mjf nfo = sc->RDSC[bi].Info;
284 1.1.16.1 mjf fl = (nfo & ETH_RDSC_I_LEN) - 4;
285 1.1.16.1 mjf DPRINTFN(2,("## nfo=0x%08X\n", nfo));
286 1.1.16.1 mjf
287 1.1.16.1 mjf MGETHDR(m, M_DONTWAIT, MT_DATA);
288 1.1.16.1 mjf if (m != NULL) MCLGET(m, M_DONTWAIT);
289 1.1.16.1 mjf if (m != NULL && (m->m_flags & M_EXT)) {
290 1.1.16.1 mjf bus_dmamap_sync(sc->sc_dmat, sc->rxq[bi].m_dmamap, 0,
291 1.1.16.1 mjf MCLBYTES, BUS_DMASYNC_POSTREAD);
292 1.1.16.1 mjf bus_dmamap_unload(sc->sc_dmat,
293 1.1.16.1 mjf sc->rxq[bi].m_dmamap);
294 1.1.16.1 mjf sc->rxq[bi].m->m_pkthdr.rcvif = ifp;
295 1.1.16.1 mjf sc->rxq[bi].m->m_pkthdr.len =
296 1.1.16.1 mjf sc->rxq[bi].m->m_len = fl;
297 1.1.16.1 mjf #if NBPFILTER > 0
298 1.1.16.1 mjf if (ifp->if_bpf)
299 1.1.16.1 mjf bpf_mtap(ifp->if_bpf, sc->rxq[bi].m);
300 1.1.16.1 mjf #endif /* NBPFILTER > 0 */
301 1.1.16.1 mjf DPRINTFN(2,("received %u bytes packet\n", fl));
302 1.1.16.1 mjf (*ifp->if_input)(ifp, sc->rxq[bi].m);
303 1.1.16.1 mjf if (mtod(m, intptr_t) & 3) {
304 1.1.16.1 mjf m_adj(m, mtod(m, intptr_t) & 3);
305 1.1.16.1 mjf }
306 1.1.16.1 mjf sc->rxq[bi].m = m;
307 1.1.16.1 mjf bus_dmamap_load(sc->sc_dmat,
308 1.1.16.1 mjf sc->rxq[bi].m_dmamap,
309 1.1.16.1 mjf m->m_ext.ext_buf, MCLBYTES,
310 1.1.16.1 mjf NULL, BUS_DMA_NOWAIT);
311 1.1.16.1 mjf bus_dmamap_sync(sc->sc_dmat, sc->rxq[bi].m_dmamap, 0,
312 1.1.16.1 mjf MCLBYTES, BUS_DMASYNC_PREREAD);
313 1.1.16.1 mjf sc->RDSC[bi].Info = 0;
314 1.1.16.1 mjf sc->RDSC[bi].Addr =
315 1.1.16.1 mjf sc->rxq[bi].m_dmamap->dm_segs[0].ds_addr
316 1.1.16.1 mjf | (bi == (RX_QLEN-1) ? ETH_RDSC_F_WRAP : 0);
317 1.1.16.1 mjf } else {
318 1.1.16.1 mjf /* Drop packets until we can get replacement
319 1.1.16.1 mjf * empty mbufs for the RXDQ.
320 1.1.16.1 mjf */
321 1.1.16.1 mjf if (m != NULL) {
322 1.1.16.1 mjf m_freem(m);
323 1.1.16.1 mjf }
324 1.1.16.1 mjf ifp->if_ierrors++;
325 1.1.16.1 mjf }
326 1.1.16.1 mjf sc->rxqi++;
327 1.1.16.1 mjf }
328 1.1.16.1 mjf // bus_dmamap_sync(sc->sc_dmat, sc->rbqpage_dmamap, 0, sc->rbqlen, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
329 1.1.16.1 mjf }
330 1.1.16.1 mjf
331 1.1.16.1 mjf if (emac_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
332 1.1.16.1 mjf emac_ifstart(ifp);
333 1.1.16.1 mjf }
334 1.1.16.1 mjf #if 0 // reloop
335 1.1.16.1 mjf irq = EMAC_READ(IntStsC);
336 1.1.16.1 mjf if ((irq & (IntSts_RxSQ|IntSts_ECI)) != 0)
337 1.1.16.1 mjf goto begin;
338 1.1.16.1 mjf #endif
339 1.1.16.1 mjf
340 1.1.16.1 mjf return (1);
341 1.1.16.1 mjf }
342 1.1.16.1 mjf
343 1.1.16.1 mjf
344 1.1.16.1 mjf static void
345 1.1.16.1 mjf emac_init(struct emac_softc *sc)
346 1.1.16.1 mjf {
347 1.1.16.1 mjf bus_dma_segment_t segs;
348 1.1.16.1 mjf void *addr;
349 1.1.16.1 mjf int rsegs, err, i;
350 1.1.16.1 mjf struct ifnet * ifp = &sc->sc_ec.ec_if;
351 1.1.16.1 mjf uint32_t u;
352 1.1.16.1 mjf #if 0
353 1.1.16.1 mjf int mdcdiv = DEFAULT_MDCDIV;
354 1.1.16.1 mjf #endif
355 1.1.16.1 mjf
356 1.1.16.1 mjf callout_init(&sc->emac_tick_ch, 0);
357 1.1.16.1 mjf
358 1.1.16.1 mjf // ok...
359 1.1.16.1 mjf EMAC_WRITE(ETH_CTL, ETH_CTL_MPE); // disable everything
360 1.1.16.1 mjf EMAC_WRITE(ETH_IDR, -1); // disable interrupts
361 1.1.16.1 mjf EMAC_WRITE(ETH_RBQP, 0); // clear receive
362 1.1.16.1 mjf EMAC_WRITE(ETH_CFG, ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
363 1.1.16.1 mjf EMAC_WRITE(ETH_TCR, 0); // send nothing
364 1.1.16.1 mjf // (void)EMAC_READ(ETH_ISR);
365 1.1.16.1 mjf u = EMAC_READ(ETH_TSR);
366 1.1.16.1 mjf EMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
367 1.1.16.1 mjf | ETH_TSR_IDLE | ETH_TSR_RLE
368 1.1.16.1 mjf | ETH_TSR_COL|ETH_TSR_OVR)));
369 1.1.16.1 mjf u = EMAC_READ(ETH_RSR);
370 1.1.16.1 mjf EMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR|ETH_RSR_REC|ETH_RSR_BNA)));
371 1.1.16.1 mjf
372 1.1.16.1 mjf /* configure EMAC */
373 1.1.16.1 mjf EMAC_WRITE(ETH_CFG, ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
374 1.1.16.1 mjf EMAC_WRITE(ETH_CTL, ETH_CTL_MPE);
375 1.1.16.1 mjf #if 0
376 1.1.16.1 mjf if (device_cfdata(&sc->sc_dev)->cf_flags)
377 1.1.16.1 mjf mdcdiv = device_cfdata(&sc->sc_dev)->cf_flags;
378 1.1.16.1 mjf #endif
379 1.1.16.1 mjf /* set ethernet address */
380 1.1.16.1 mjf EMAC_WRITE(ETH_SA1L, (sc->sc_enaddr[3] << 24)
381 1.1.16.1 mjf | (sc->sc_enaddr[2] << 16) | (sc->sc_enaddr[1] << 8)
382 1.1.16.1 mjf | (sc->sc_enaddr[0]));
383 1.1.16.1 mjf EMAC_WRITE(ETH_SA1H, (sc->sc_enaddr[5] << 8)
384 1.1.16.1 mjf | (sc->sc_enaddr[4]));
385 1.1.16.1 mjf EMAC_WRITE(ETH_SA2L, 0);
386 1.1.16.1 mjf EMAC_WRITE(ETH_SA2H, 0);
387 1.1.16.1 mjf EMAC_WRITE(ETH_SA3L, 0);
388 1.1.16.1 mjf EMAC_WRITE(ETH_SA3H, 0);
389 1.1.16.1 mjf EMAC_WRITE(ETH_SA4L, 0);
390 1.1.16.1 mjf EMAC_WRITE(ETH_SA4H, 0);
391 1.1.16.1 mjf
392 1.1.16.1 mjf /* Allocate a page of memory for receive queue descriptors */
393 1.1.16.1 mjf sc->rbqlen = (ETH_RDSC_SIZE * (RX_QLEN + 1) * 2 + PAGE_SIZE - 1) / PAGE_SIZE;
394 1.1.16.1 mjf sc->rbqlen *= PAGE_SIZE;
395 1.1.16.1 mjf DPRINTFN(1,("%s: rbqlen=%i\n", __FUNCTION__, sc->rbqlen));
396 1.1.16.1 mjf
397 1.1.16.1 mjf err = bus_dmamem_alloc(sc->sc_dmat, sc->rbqlen, 0,
398 1.1.16.1 mjf MAX(16384, PAGE_SIZE), // see EMAC errata why forced to 16384 byte boundary
399 1.1.16.1 mjf &segs, 1, &rsegs, BUS_DMA_WAITOK);
400 1.1.16.1 mjf if (err == 0) {
401 1.1.16.1 mjf DPRINTFN(1,("%s: -> bus_dmamem_map\n", __FUNCTION__));
402 1.1.16.1 mjf err = bus_dmamem_map(sc->sc_dmat, &segs, 1, sc->rbqlen,
403 1.1.16.1 mjf &sc->rbqpage, (BUS_DMA_WAITOK|BUS_DMA_COHERENT));
404 1.1.16.1 mjf }
405 1.1.16.1 mjf if (err == 0) {
406 1.1.16.1 mjf DPRINTFN(1,("%s: -> bus_dmamap_create\n", __FUNCTION__));
407 1.1.16.1 mjf err = bus_dmamap_create(sc->sc_dmat, sc->rbqlen, 1,
408 1.1.16.1 mjf sc->rbqlen, MAX(16384, PAGE_SIZE), BUS_DMA_WAITOK,
409 1.1.16.1 mjf &sc->rbqpage_dmamap);
410 1.1.16.1 mjf }
411 1.1.16.1 mjf if (err == 0) {
412 1.1.16.1 mjf DPRINTFN(1,("%s: -> bus_dmamap_load\n", __FUNCTION__));
413 1.1.16.1 mjf err = bus_dmamap_load(sc->sc_dmat, sc->rbqpage_dmamap,
414 1.1.16.1 mjf sc->rbqpage, sc->rbqlen, NULL, BUS_DMA_WAITOK);
415 1.1.16.1 mjf }
416 1.1.16.1 mjf if (err != 0) {
417 1.1.16.1 mjf panic("%s: Cannot get DMA memory", device_xname(sc->sc_dev));
418 1.1.16.1 mjf }
419 1.1.16.1 mjf sc->rbqpage_dsaddr = sc->rbqpage_dmamap->dm_segs[0].ds_addr;
420 1.1.16.1 mjf
421 1.1.16.1 mjf bzero(sc->rbqpage, sc->rbqlen);
422 1.1.16.1 mjf
423 1.1.16.1 mjf /* Set up pointers to start of each queue in kernel addr space.
424 1.1.16.1 mjf * Each descriptor queue or status queue entry uses 2 words
425 1.1.16.1 mjf */
426 1.1.16.1 mjf sc->RDSC = (void*)sc->rbqpage;
427 1.1.16.1 mjf
428 1.1.16.1 mjf /* Populate the RXQ with mbufs */
429 1.1.16.1 mjf sc->rxqi = 0;
430 1.1.16.1 mjf for(i = 0; i < RX_QLEN; i++) {
431 1.1.16.1 mjf struct mbuf *m;
432 1.1.16.1 mjf
433 1.1.16.1 mjf err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, PAGE_SIZE,
434 1.1.16.1 mjf BUS_DMA_WAITOK, &sc->rxq[i].m_dmamap);
435 1.1.16.1 mjf if (err) {
436 1.1.16.1 mjf panic("%s: dmamap_create failed: %i\n", __FUNCTION__, err);
437 1.1.16.1 mjf }
438 1.1.16.1 mjf MGETHDR(m, M_WAIT, MT_DATA);
439 1.1.16.1 mjf MCLGET(m, M_WAIT);
440 1.1.16.1 mjf sc->rxq[i].m = m;
441 1.1.16.1 mjf if (mtod(m, intptr_t) & 3) {
442 1.1.16.1 mjf m_adj(m, mtod(m, intptr_t) & 3);
443 1.1.16.1 mjf }
444 1.1.16.1 mjf err = bus_dmamap_load(sc->sc_dmat, sc->rxq[i].m_dmamap,
445 1.1.16.1 mjf m->m_ext.ext_buf, MCLBYTES, NULL,
446 1.1.16.1 mjf BUS_DMA_WAITOK);
447 1.1.16.1 mjf if (err) {
448 1.1.16.1 mjf panic("%s: dmamap_load failed: %i\n", __FUNCTION__, err);
449 1.1.16.1 mjf }
450 1.1.16.1 mjf sc->RDSC[i].Addr = sc->rxq[i].m_dmamap->dm_segs[0].ds_addr
451 1.1.16.1 mjf | (i == (RX_QLEN-1) ? ETH_RDSC_F_WRAP : 0);
452 1.1.16.1 mjf sc->RDSC[i].Info = 0;
453 1.1.16.1 mjf bus_dmamap_sync(sc->sc_dmat, sc->rxq[i].m_dmamap, 0,
454 1.1.16.1 mjf MCLBYTES, BUS_DMASYNC_PREREAD);
455 1.1.16.1 mjf }
456 1.1.16.1 mjf
457 1.1.16.1 mjf /* prepare transmit queue */
458 1.1.16.1 mjf for (i = 0; i < TX_QLEN; i++) {
459 1.1.16.1 mjf err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
460 1.1.16.1 mjf (BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW),
461 1.1.16.1 mjf &sc->txq[i].m_dmamap);
462 1.1.16.1 mjf if (err)
463 1.1.16.1 mjf panic("ARGH #1");
464 1.1.16.1 mjf sc->txq[i].m = NULL;
465 1.1.16.1 mjf }
466 1.1.16.1 mjf
467 1.1.16.1 mjf /* Program each queue's start addr, cur addr, and len registers
468 1.1.16.1 mjf * with the physical addresses.
469 1.1.16.1 mjf */
470 1.1.16.1 mjf bus_dmamap_sync(sc->sc_dmat, sc->rbqpage_dmamap, 0, sc->rbqlen,
471 1.1.16.1 mjf BUS_DMASYNC_PREREAD);
472 1.1.16.1 mjf addr = (void *)sc->rbqpage_dmamap->dm_segs[0].ds_addr;
473 1.1.16.1 mjf EMAC_WRITE(ETH_RBQP, (u_int32_t)addr);
474 1.1.16.1 mjf
475 1.1.16.1 mjf /* Divide HCLK by 32 for MDC clock */
476 1.1.16.1 mjf sc->sc_mii.mii_ifp = ifp;
477 1.1.16.1 mjf sc->sc_mii.mii_readreg = emac_mii_readreg;
478 1.1.16.1 mjf sc->sc_mii.mii_writereg = emac_mii_writereg;
479 1.1.16.1 mjf sc->sc_mii.mii_statchg = emac_statchg;
480 1.1.16.1 mjf ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, emac_mediachange,
481 1.1.16.1 mjf emac_mediastatus);
482 1.1.16.1 mjf mii_attach((device_t )sc, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
483 1.1.16.1 mjf MII_OFFSET_ANY, 0);
484 1.1.16.1 mjf ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
485 1.1.16.1 mjf
486 1.1.16.1 mjf // enable / disable interrupts
487 1.1.16.1 mjf
488 1.1.16.1 mjf #if 0
489 1.1.16.1 mjf // enable / disable interrupts
490 1.1.16.1 mjf EMAC_WRITE(ETH_IDR, -1);
491 1.1.16.1 mjf EMAC_WRITE(ETH_IER, ETH_ISR_RCOM | ETH_ISR_TBRE | ETH_ISR_TIDLE
492 1.1.16.1 mjf | ETH_ISR_RBNA | ETH_ISR_ROVR);
493 1.1.16.1 mjf // (void)EMAC_READ(ETH_ISR); // why
494 1.1.16.1 mjf
495 1.1.16.1 mjf // enable transmitter / receiver
496 1.1.16.1 mjf EMAC_WRITE(ETH_CTL, ETH_CTL_TE | ETH_CTL_RE | ETH_CTL_ISR
497 1.1.16.1 mjf | ETH_CTL_CSR | ETH_CTL_MPE);
498 1.1.16.1 mjf #endif
499 1.1.16.1 mjf /*
500 1.1.16.1 mjf * We can support 802.1Q VLAN-sized frames.
501 1.1.16.1 mjf */
502 1.1.16.1 mjf sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
503 1.1.16.1 mjf
504 1.1.16.1 mjf strcpy(ifp->if_xname, device_xname(sc->sc_dev));
505 1.1.16.1 mjf ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
506 1.1.16.1 mjf ifp->if_ioctl = emac_ifioctl;
507 1.1.16.1 mjf ifp->if_start = emac_ifstart;
508 1.1.16.1 mjf ifp->if_watchdog = emac_ifwatchdog;
509 1.1.16.1 mjf ifp->if_init = emac_ifinit;
510 1.1.16.1 mjf ifp->if_stop = emac_ifstop;
511 1.1.16.1 mjf ifp->if_timer = 0;
512 1.1.16.1 mjf ifp->if_softc = sc;
513 1.1.16.1 mjf IFQ_SET_READY(&ifp->if_snd);
514 1.1.16.1 mjf if_attach(ifp);
515 1.1.16.1 mjf ether_ifattach(ifp, (sc)->sc_enaddr);
516 1.1.16.1 mjf }
517 1.1.16.1 mjf
518 1.1.16.1 mjf static int
519 1.1.16.1 mjf emac_mediachange(ifp)
520 1.1.16.1 mjf struct ifnet *ifp;
521 1.1.16.1 mjf {
522 1.1.16.1 mjf if (ifp->if_flags & IFF_UP)
523 1.1.16.1 mjf emac_ifinit(ifp);
524 1.1.16.1 mjf return (0);
525 1.1.16.1 mjf }
526 1.1.16.1 mjf
527 1.1.16.1 mjf static void
528 1.1.16.1 mjf emac_mediastatus(ifp, ifmr)
529 1.1.16.1 mjf struct ifnet *ifp;
530 1.1.16.1 mjf struct ifmediareq *ifmr;
531 1.1.16.1 mjf {
532 1.1.16.1 mjf struct emac_softc *sc = ifp->if_softc;
533 1.1.16.1 mjf
534 1.1.16.1 mjf mii_pollstat(&sc->sc_mii);
535 1.1.16.1 mjf ifmr->ifm_active = sc->sc_mii.mii_media_active;
536 1.1.16.1 mjf ifmr->ifm_status = sc->sc_mii.mii_media_status;
537 1.1.16.1 mjf }
538 1.1.16.1 mjf
539 1.1.16.1 mjf
540 1.1.16.1 mjf int
541 1.1.16.1 mjf emac_mii_readreg(self, phy, reg)
542 1.1.16.1 mjf device_t self;
543 1.1.16.1 mjf int phy, reg;
544 1.1.16.1 mjf {
545 1.1.16.1 mjf struct emac_softc *sc;
546 1.1.16.1 mjf
547 1.1.16.1 mjf sc = (struct emac_softc *)self;
548 1.1.16.1 mjf EMAC_WRITE(ETH_MAN, (ETH_MAN_HIGH | ETH_MAN_RW_RD
549 1.1.16.1 mjf | ((phy << ETH_MAN_PHYA_SHIFT) & ETH_MAN_PHYA)
550 1.1.16.1 mjf | ((reg << ETH_MAN_REGA_SHIFT) & ETH_MAN_REGA)
551 1.1.16.1 mjf | ETH_MAN_CODE_IEEE802_3));
552 1.1.16.1 mjf while (!(EMAC_READ(ETH_SR) & ETH_SR_IDLE)) ;
553 1.1.16.1 mjf return (EMAC_READ(ETH_MAN) & ETH_MAN_DATA);
554 1.1.16.1 mjf }
555 1.1.16.1 mjf
556 1.1.16.1 mjf void
557 1.1.16.1 mjf emac_mii_writereg(self, phy, reg, val)
558 1.1.16.1 mjf device_t self;
559 1.1.16.1 mjf int phy, reg, val;
560 1.1.16.1 mjf {
561 1.1.16.1 mjf struct emac_softc *sc;
562 1.1.16.1 mjf sc = (struct emac_softc *)self;
563 1.1.16.1 mjf EMAC_WRITE(ETH_MAN, (ETH_MAN_HIGH | ETH_MAN_RW_WR
564 1.1.16.1 mjf | ((phy << ETH_MAN_PHYA_SHIFT) & ETH_MAN_PHYA)
565 1.1.16.1 mjf | ((reg << ETH_MAN_REGA_SHIFT) & ETH_MAN_REGA)
566 1.1.16.1 mjf | ETH_MAN_CODE_IEEE802_3
567 1.1.16.1 mjf | (val & ETH_MAN_DATA)));
568 1.1.16.1 mjf while (!(EMAC_READ(ETH_SR) & ETH_SR_IDLE)) ;
569 1.1.16.1 mjf }
570 1.1.16.1 mjf
571 1.1.16.1 mjf
572 1.1.16.1 mjf void
573 1.1.16.1 mjf emac_statchg(self)
574 1.1.16.1 mjf device_t self;
575 1.1.16.1 mjf {
576 1.1.16.1 mjf struct emac_softc *sc = (struct emac_softc *)self;
577 1.1.16.1 mjf u_int32_t reg;
578 1.1.16.1 mjf
579 1.1.16.1 mjf /*
580 1.1.16.1 mjf * We must keep the MAC and the PHY in sync as
581 1.1.16.1 mjf * to the status of full-duplex!
582 1.1.16.1 mjf */
583 1.1.16.1 mjf reg = EMAC_READ(ETH_CFG);
584 1.1.16.1 mjf if (sc->sc_mii.mii_media_active & IFM_FDX)
585 1.1.16.1 mjf reg |= ETH_CFG_FD;
586 1.1.16.1 mjf else
587 1.1.16.1 mjf reg &= ~ETH_CFG_FD;
588 1.1.16.1 mjf EMAC_WRITE(ETH_CFG, reg);
589 1.1.16.1 mjf }
590 1.1.16.1 mjf
591 1.1.16.1 mjf void
592 1.1.16.1 mjf emac_tick(arg)
593 1.1.16.1 mjf void *arg;
594 1.1.16.1 mjf {
595 1.1.16.1 mjf struct emac_softc* sc = (struct emac_softc *)arg;
596 1.1.16.1 mjf struct ifnet * ifp = &sc->sc_ec.ec_if;
597 1.1.16.1 mjf int s;
598 1.1.16.1 mjf u_int32_t misses;
599 1.1.16.1 mjf
600 1.1.16.1 mjf ifp->if_collisions += EMAC_READ(ETH_SCOL) + EMAC_READ(ETH_MCOL);
601 1.1.16.1 mjf /* These misses are ok, they will happen if the RAM/CPU can't keep up */
602 1.1.16.1 mjf misses = EMAC_READ(ETH_DRFC);
603 1.1.16.1 mjf if (misses > 0)
604 1.1.16.1 mjf printf("%s: %d rx misses\n", device_xname(sc->sc_dev), misses);
605 1.1.16.1 mjf
606 1.1.16.1 mjf s = splnet();
607 1.1.16.1 mjf if (emac_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
608 1.1.16.1 mjf emac_ifstart(ifp);
609 1.1.16.1 mjf }
610 1.1.16.1 mjf splx(s);
611 1.1.16.1 mjf
612 1.1.16.1 mjf mii_tick(&sc->sc_mii);
613 1.1.16.1 mjf callout_reset(&sc->emac_tick_ch, hz, emac_tick, sc);
614 1.1.16.1 mjf }
615 1.1.16.1 mjf
616 1.1.16.1 mjf
617 1.1.16.1 mjf static int
618 1.1.16.1 mjf emac_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
619 1.1.16.1 mjf {
620 1.1.16.1 mjf struct emac_softc *sc = ifp->if_softc;
621 1.1.16.1 mjf struct ifreq *ifr = (struct ifreq *)data;
622 1.1.16.1 mjf int s, error;
623 1.1.16.1 mjf
624 1.1.16.1 mjf s = splnet();
625 1.1.16.1 mjf switch(cmd) {
626 1.1.16.1 mjf case SIOCSIFMEDIA:
627 1.1.16.1 mjf case SIOCGIFMEDIA:
628 1.1.16.1 mjf error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
629 1.1.16.1 mjf break;
630 1.1.16.1 mjf default:
631 1.1.16.1 mjf error = ether_ioctl(ifp, cmd, data);
632 1.1.16.1 mjf if (error == ENETRESET) {
633 1.1.16.1 mjf if (ifp->if_flags & IFF_RUNNING)
634 1.1.16.1 mjf emac_setaddr(ifp);
635 1.1.16.1 mjf error = 0;
636 1.1.16.1 mjf }
637 1.1.16.1 mjf }
638 1.1.16.1 mjf splx(s);
639 1.1.16.1 mjf return error;
640 1.1.16.1 mjf }
641 1.1.16.1 mjf
642 1.1.16.1 mjf static void
643 1.1.16.1 mjf emac_ifstart(ifp)
644 1.1.16.1 mjf struct ifnet *ifp;
645 1.1.16.1 mjf {
646 1.1.16.1 mjf struct emac_softc *sc = (struct emac_softc *)ifp->if_softc;
647 1.1.16.1 mjf struct mbuf *m;
648 1.1.16.1 mjf bus_dma_segment_t *segs;
649 1.1.16.1 mjf int s, bi, err, nsegs;
650 1.1.16.1 mjf
651 1.1.16.1 mjf s = splnet();
652 1.1.16.1 mjf start:
653 1.1.16.1 mjf if (emac_gctx(sc) == 0) {
654 1.1.16.1 mjf /* Enable transmit-buffer-free interrupt */
655 1.1.16.1 mjf EMAC_WRITE(ETH_IER, ETH_ISR_TBRE);
656 1.1.16.1 mjf ifp->if_flags |= IFF_OACTIVE;
657 1.1.16.1 mjf ifp->if_timer = 10;
658 1.1.16.1 mjf splx(s);
659 1.1.16.1 mjf return;
660 1.1.16.1 mjf }
661 1.1.16.1 mjf
662 1.1.16.1 mjf ifp->if_timer = 0;
663 1.1.16.1 mjf
664 1.1.16.1 mjf IFQ_POLL(&ifp->if_snd, m);
665 1.1.16.1 mjf if (m == NULL) {
666 1.1.16.1 mjf splx(s);
667 1.1.16.1 mjf return;
668 1.1.16.1 mjf }
669 1.1.16.1 mjf //more:
670 1.1.16.1 mjf bi = (sc->txqi + sc->txqc) % TX_QLEN;
671 1.1.16.1 mjf if ((err = bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
672 1.1.16.1 mjf BUS_DMA_NOWAIT)) ||
673 1.1.16.1 mjf sc->txq[bi].m_dmamap->dm_segs[0].ds_addr & 0x3 ||
674 1.1.16.1 mjf sc->txq[bi].m_dmamap->dm_nsegs > 1) {
675 1.1.16.1 mjf /* Copy entire mbuf chain to new single */
676 1.1.16.1 mjf struct mbuf *mn;
677 1.1.16.1 mjf
678 1.1.16.1 mjf if (err == 0)
679 1.1.16.1 mjf bus_dmamap_unload(sc->sc_dmat, sc->txq[bi].m_dmamap);
680 1.1.16.1 mjf
681 1.1.16.1 mjf MGETHDR(mn, M_DONTWAIT, MT_DATA);
682 1.1.16.1 mjf if (mn == NULL) goto stop;
683 1.1.16.1 mjf if (m->m_pkthdr.len > MHLEN) {
684 1.1.16.1 mjf MCLGET(mn, M_DONTWAIT);
685 1.1.16.1 mjf if ((mn->m_flags & M_EXT) == 0) {
686 1.1.16.1 mjf m_freem(mn);
687 1.1.16.1 mjf goto stop;
688 1.1.16.1 mjf }
689 1.1.16.1 mjf }
690 1.1.16.1 mjf m_copydata(m, 0, m->m_pkthdr.len, mtod(mn, void *));
691 1.1.16.1 mjf mn->m_pkthdr.len = mn->m_len = m->m_pkthdr.len;
692 1.1.16.1 mjf IFQ_DEQUEUE(&ifp->if_snd, m);
693 1.1.16.1 mjf m_freem(m);
694 1.1.16.1 mjf m = mn;
695 1.1.16.1 mjf bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
696 1.1.16.1 mjf BUS_DMA_NOWAIT);
697 1.1.16.1 mjf } else {
698 1.1.16.1 mjf IFQ_DEQUEUE(&ifp->if_snd, m);
699 1.1.16.1 mjf }
700 1.1.16.1 mjf
701 1.1.16.1 mjf #if NBPFILTER > 0
702 1.1.16.1 mjf if (ifp->if_bpf)
703 1.1.16.1 mjf bpf_mtap(ifp->if_bpf, m);
704 1.1.16.1 mjf #endif /* NBPFILTER > 0 */
705 1.1.16.1 mjf
706 1.1.16.1 mjf nsegs = sc->txq[bi].m_dmamap->dm_nsegs;
707 1.1.16.1 mjf segs = sc->txq[bi].m_dmamap->dm_segs;
708 1.1.16.1 mjf if (nsegs > 1) {
709 1.1.16.1 mjf panic("#### ARGH #2");
710 1.1.16.1 mjf }
711 1.1.16.1 mjf
712 1.1.16.1 mjf sc->txq[bi].m = m;
713 1.1.16.1 mjf sc->txqc++;
714 1.1.16.1 mjf
715 1.1.16.1 mjf DPRINTFN(2,("%s: start sending idx #%i mbuf %p (txqc=%i, phys %p), len=%u\n", __FUNCTION__, bi, sc->txq[bi].m, sc->txqc, (void*)segs->ds_addr,
716 1.1.16.1 mjf (unsigned)m->m_pkthdr.len));
717 1.1.16.1 mjf #ifdef DIAGNOSTIC
718 1.1.16.1 mjf if (sc->txqc > TX_QLEN) {
719 1.1.16.1 mjf panic("%s: txqc %i > %i", __FUNCTION__, sc->txqc, TX_QLEN);
720 1.1.16.1 mjf }
721 1.1.16.1 mjf #endif
722 1.1.16.1 mjf
723 1.1.16.1 mjf bus_dmamap_sync(sc->sc_dmat, sc->txq[bi].m_dmamap, 0,
724 1.1.16.1 mjf sc->txq[bi].m_dmamap->dm_mapsize,
725 1.1.16.1 mjf BUS_DMASYNC_PREWRITE);
726 1.1.16.1 mjf
727 1.1.16.1 mjf EMAC_WRITE(ETH_TAR, segs->ds_addr);
728 1.1.16.1 mjf EMAC_WRITE(ETH_TCR, m->m_pkthdr.len);
729 1.1.16.1 mjf if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
730 1.1.16.1 mjf goto start;
731 1.1.16.1 mjf stop:
732 1.1.16.1 mjf
733 1.1.16.1 mjf splx(s);
734 1.1.16.1 mjf return;
735 1.1.16.1 mjf }
736 1.1.16.1 mjf
737 1.1.16.1 mjf static void
738 1.1.16.1 mjf emac_ifwatchdog(ifp)
739 1.1.16.1 mjf struct ifnet *ifp;
740 1.1.16.1 mjf {
741 1.1.16.1 mjf struct emac_softc *sc = (struct emac_softc *)ifp->if_softc;
742 1.1.16.1 mjf
743 1.1.16.1 mjf if ((ifp->if_flags & IFF_RUNNING) == 0)
744 1.1.16.1 mjf return;
745 1.1.16.1 mjf printf("%s: device timeout, CTL = 0x%08x, CFG = 0x%08x\n",
746 1.1.16.1 mjf device_xname(sc->sc_dev), EMAC_READ(ETH_CTL), EMAC_READ(ETH_CFG));
747 1.1.16.1 mjf }
748 1.1.16.1 mjf
749 1.1.16.1 mjf static int
750 1.1.16.1 mjf emac_ifinit(ifp)
751 1.1.16.1 mjf struct ifnet *ifp;
752 1.1.16.1 mjf {
753 1.1.16.1 mjf struct emac_softc *sc = ifp->if_softc;
754 1.1.16.1 mjf int s = splnet();
755 1.1.16.1 mjf
756 1.1.16.1 mjf callout_stop(&sc->emac_tick_ch);
757 1.1.16.1 mjf
758 1.1.16.1 mjf // enable interrupts
759 1.1.16.1 mjf EMAC_WRITE(ETH_IDR, -1);
760 1.1.16.1 mjf EMAC_WRITE(ETH_IER, ETH_ISR_RCOM | ETH_ISR_TBRE | ETH_ISR_TIDLE
761 1.1.16.1 mjf | ETH_ISR_RBNA | ETH_ISR_ROVR);
762 1.1.16.1 mjf
763 1.1.16.1 mjf // enable transmitter / receiver
764 1.1.16.1 mjf EMAC_WRITE(ETH_CTL, ETH_CTL_TE | ETH_CTL_RE | ETH_CTL_ISR
765 1.1.16.1 mjf | ETH_CTL_CSR | ETH_CTL_MPE);
766 1.1.16.1 mjf
767 1.1.16.1 mjf mii_mediachg(&sc->sc_mii);
768 1.1.16.1 mjf callout_reset(&sc->emac_tick_ch, hz, emac_tick, sc);
769 1.1.16.1 mjf ifp->if_flags |= IFF_RUNNING;
770 1.1.16.1 mjf splx(s);
771 1.1.16.1 mjf return 0;
772 1.1.16.1 mjf }
773 1.1.16.1 mjf
774 1.1.16.1 mjf static void
775 1.1.16.1 mjf emac_ifstop(ifp, disable)
776 1.1.16.1 mjf struct ifnet *ifp;
777 1.1.16.1 mjf int disable;
778 1.1.16.1 mjf {
779 1.1.16.1 mjf // u_int32_t u;
780 1.1.16.1 mjf struct emac_softc *sc = ifp->if_softc;
781 1.1.16.1 mjf
782 1.1.16.1 mjf #if 0
783 1.1.16.1 mjf EMAC_WRITE(ETH_CTL, ETH_CTL_MPE); // disable everything
784 1.1.16.1 mjf EMAC_WRITE(ETH_IDR, -1); // disable interrupts
785 1.1.16.1 mjf // EMAC_WRITE(ETH_RBQP, 0); // clear receive
786 1.1.16.1 mjf EMAC_WRITE(ETH_CFG, ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
787 1.1.16.1 mjf EMAC_WRITE(ETH_TCR, 0); // send nothing
788 1.1.16.1 mjf // (void)EMAC_READ(ETH_ISR);
789 1.1.16.1 mjf u = EMAC_READ(ETH_TSR);
790 1.1.16.1 mjf EMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
791 1.1.16.1 mjf | ETH_TSR_IDLE | ETH_TSR_RLE
792 1.1.16.1 mjf | ETH_TSR_COL|ETH_TSR_OVR)));
793 1.1.16.1 mjf u = EMAC_READ(ETH_RSR);
794 1.1.16.1 mjf EMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR|ETH_RSR_REC|ETH_RSR_BNA)));
795 1.1.16.1 mjf #endif
796 1.1.16.1 mjf callout_stop(&sc->emac_tick_ch);
797 1.1.16.1 mjf
798 1.1.16.1 mjf /* Down the MII. */
799 1.1.16.1 mjf mii_down(&sc->sc_mii);
800 1.1.16.1 mjf
801 1.1.16.1 mjf ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
802 1.1.16.1 mjf ifp->if_timer = 0;
803 1.1.16.1 mjf sc->sc_mii.mii_media_status &= ~IFM_ACTIVE;
804 1.1.16.1 mjf }
805 1.1.16.1 mjf
806 1.1.16.1 mjf static void
807 1.1.16.1 mjf emac_setaddr(ifp)
808 1.1.16.1 mjf struct ifnet *ifp;
809 1.1.16.1 mjf {
810 1.1.16.1 mjf struct emac_softc *sc = ifp->if_softc;
811 1.1.16.1 mjf struct ethercom *ac = &sc->sc_ec;
812 1.1.16.1 mjf struct ether_multi *enm;
813 1.1.16.1 mjf struct ether_multistep step;
814 1.1.16.1 mjf u_int8_t ias[3][ETHER_ADDR_LEN];
815 1.1.16.1 mjf u_int32_t h, nma = 0, hashes[2] = { 0, 0 };
816 1.1.16.1 mjf u_int32_t ctl = EMAC_READ(ETH_CTL);
817 1.1.16.1 mjf u_int32_t cfg = EMAC_READ(ETH_CFG);
818 1.1.16.1 mjf
819 1.1.16.1 mjf /* disable receiver temporarily */
820 1.1.16.1 mjf EMAC_WRITE(ETH_CTL, ctl & ~ETH_CTL_RE);
821 1.1.16.1 mjf
822 1.1.16.1 mjf cfg &= ~(ETH_CFG_MTI | ETH_CFG_UNI | ETH_CFG_CAF | ETH_CFG_UNI);
823 1.1.16.1 mjf
824 1.1.16.1 mjf if (ifp->if_flags & IFF_PROMISC) {
825 1.1.16.1 mjf cfg |= ETH_CFG_CAF;
826 1.1.16.1 mjf } else {
827 1.1.16.1 mjf cfg &= ~ETH_CFG_CAF;
828 1.1.16.1 mjf }
829 1.1.16.1 mjf
830 1.1.16.1 mjf // ETH_CFG_BIG?
831 1.1.16.1 mjf
832 1.1.16.1 mjf ifp->if_flags &= ~IFF_ALLMULTI;
833 1.1.16.1 mjf
834 1.1.16.1 mjf ETHER_FIRST_MULTI(step, ac, enm);
835 1.1.16.1 mjf while (enm != NULL) {
836 1.1.16.1 mjf if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
837 1.1.16.1 mjf /*
838 1.1.16.1 mjf * We must listen to a range of multicast addresses.
839 1.1.16.1 mjf * For now, just accept all multicasts, rather than
840 1.1.16.1 mjf * trying to set only those filter bits needed to match
841 1.1.16.1 mjf * the range. (At this time, the only use of address
842 1.1.16.1 mjf * ranges is for IP multicast routing, for which the
843 1.1.16.1 mjf * range is big enough to require all bits set.)
844 1.1.16.1 mjf */
845 1.1.16.1 mjf cfg |= ETH_CFG_CAF;
846 1.1.16.1 mjf hashes[0] = 0xffffffffUL;
847 1.1.16.1 mjf hashes[1] = 0xffffffffUL;
848 1.1.16.1 mjf ifp->if_flags |= IFF_ALLMULTI;
849 1.1.16.1 mjf nma = 0;
850 1.1.16.1 mjf break;
851 1.1.16.1 mjf }
852 1.1.16.1 mjf
853 1.1.16.1 mjf if (nma < 3) {
854 1.1.16.1 mjf /* We can program 3 perfect address filters for mcast */
855 1.1.16.1 mjf memcpy(ias[nma], enm->enm_addrlo, ETHER_ADDR_LEN);
856 1.1.16.1 mjf } else {
857 1.1.16.1 mjf /*
858 1.1.16.1 mjf * XXX: Datasheet is not very clear here, I'm not sure
859 1.1.16.1 mjf * if I'm doing this right. --joff
860 1.1.16.1 mjf */
861 1.1.16.1 mjf h = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
862 1.1.16.1 mjf
863 1.1.16.1 mjf /* Just want the 6 most-significant bits. */
864 1.1.16.1 mjf h = h >> 26;
865 1.1.16.1 mjf
866 1.1.16.1 mjf hashes[ h / 32 ] |= (1 << (h % 32));
867 1.1.16.1 mjf cfg |= ETH_CFG_MTI;
868 1.1.16.1 mjf }
869 1.1.16.1 mjf ETHER_NEXT_MULTI(step, enm);
870 1.1.16.1 mjf nma++;
871 1.1.16.1 mjf }
872 1.1.16.1 mjf
873 1.1.16.1 mjf // program...
874 1.1.16.1 mjf DPRINTFN(1,("%s: en0 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
875 1.1.16.1 mjf sc->sc_enaddr[0], sc->sc_enaddr[1], sc->sc_enaddr[2],
876 1.1.16.1 mjf sc->sc_enaddr[3], sc->sc_enaddr[4], sc->sc_enaddr[5]));
877 1.1.16.1 mjf EMAC_WRITE(ETH_SA1L, (sc->sc_enaddr[3] << 24)
878 1.1.16.1 mjf | (sc->sc_enaddr[2] << 16) | (sc->sc_enaddr[1] << 8)
879 1.1.16.1 mjf | (sc->sc_enaddr[0]));
880 1.1.16.1 mjf EMAC_WRITE(ETH_SA1H, (sc->sc_enaddr[5] << 8)
881 1.1.16.1 mjf | (sc->sc_enaddr[4]));
882 1.1.16.1 mjf if (nma > 1) {
883 1.1.16.1 mjf DPRINTFN(1,("%s: en1 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
884 1.1.16.1 mjf ias[0][0], ias[0][1], ias[0][2],
885 1.1.16.1 mjf ias[0][3], ias[0][4], ias[0][5]));
886 1.1.16.1 mjf EMAC_WRITE(ETH_SA2L, (ias[0][3] << 24)
887 1.1.16.1 mjf | (ias[0][2] << 16) | (ias[0][1] << 8)
888 1.1.16.1 mjf | (ias[0][0]));
889 1.1.16.1 mjf EMAC_WRITE(ETH_SA2H, (ias[0][4] << 8)
890 1.1.16.1 mjf | (ias[0][5]));
891 1.1.16.1 mjf }
892 1.1.16.1 mjf if (nma > 2) {
893 1.1.16.1 mjf DPRINTFN(1,("%s: en2 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
894 1.1.16.1 mjf ias[1][0], ias[1][1], ias[1][2],
895 1.1.16.1 mjf ias[1][3], ias[1][4], ias[1][5]));
896 1.1.16.1 mjf EMAC_WRITE(ETH_SA3L, (ias[1][3] << 24)
897 1.1.16.1 mjf | (ias[1][2] << 16) | (ias[1][1] << 8)
898 1.1.16.1 mjf | (ias[1][0]));
899 1.1.16.1 mjf EMAC_WRITE(ETH_SA3H, (ias[1][4] << 8)
900 1.1.16.1 mjf | (ias[1][5]));
901 1.1.16.1 mjf }
902 1.1.16.1 mjf if (nma > 3) {
903 1.1.16.1 mjf DPRINTFN(1,("%s: en3 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
904 1.1.16.1 mjf ias[2][0], ias[2][1], ias[2][2],
905 1.1.16.1 mjf ias[2][3], ias[2][4], ias[2][5]));
906 1.1.16.1 mjf EMAC_WRITE(ETH_SA3L, (ias[2][3] << 24)
907 1.1.16.1 mjf | (ias[2][2] << 16) | (ias[2][1] << 8)
908 1.1.16.1 mjf | (ias[2][0]));
909 1.1.16.1 mjf EMAC_WRITE(ETH_SA3H, (ias[2][4] << 8)
910 1.1.16.1 mjf | (ias[2][5]));
911 1.1.16.1 mjf }
912 1.1.16.1 mjf EMAC_WRITE(ETH_HSH, hashes[0]);
913 1.1.16.1 mjf EMAC_WRITE(ETH_HSL, hashes[1]);
914 1.1.16.1 mjf EMAC_WRITE(ETH_CFG, cfg);
915 1.1.16.1 mjf EMAC_WRITE(ETH_CTL, ctl | ETH_CTL_RE);
916 1.1.16.1 mjf }
917