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