]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/at91/if_macb.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r302418, and update
[FreeBSD/FreeBSD.git] / sys / arm / at91 / if_macb.c
1 /*-
2  * Copyright (c) 2010 Yohanes Nugroho <yohanes@gmail.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include "opt_platform.h"
28 #include "opt_at91.h"
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/mbuf.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/rman.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/sysctl.h>
45 #include <sys/taskqueue.h>
46
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_arp.h>
50 #include <net/if_dl.h>
51 #include <net/if_media.h>
52 #include <net/if_types.h>
53 #include <net/if_vlan_var.h>
54
55 #ifdef INET
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #endif
61
62 #include <net/bpf.h>
63 #include <net/bpfdesc.h>
64
65 #include <dev/mii/mii.h>
66 #include <dev/mii/miivar.h>
67
68 #include <arm/at91/at91_pmcvar.h>
69 #include <arm/at91/if_macbreg.h>
70 #include <arm/at91/if_macbvar.h>
71 #include <arm/at91/at91_piovar.h>
72
73 #include <arm/at91/at91sam9g20reg.h>
74
75 #include <machine/bus.h>
76 #include <machine/intr.h>
77
78 #ifdef FDT
79 #include <dev/ofw/ofw_bus.h>
80 #include <dev/ofw/ofw_bus_subr.h>
81 #endif
82
83 /* "device miibus" required.  See GENERIC if you get errors here. */
84 #include "miibus_if.h"
85
86
87 #define MACB_LOCK(_sc)          mtx_lock(&(_sc)->sc_mtx)
88 #define MACB_UNLOCK(_sc)                mtx_unlock(&(_sc)->sc_mtx)
89 #define MACB_LOCK_INIT(_sc)                                     \
90         mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev),   \
91             MTX_NETWORK_LOCK, MTX_DEF)
92 #define MACB_LOCK_DESTROY(_sc)  mtx_destroy(&_sc->sc_mtx);
93 #define MACB_LOCK_ASSERT(_sc)   mtx_assert(&_sc->sc_mtx, MA_OWNED);
94 #define MACB_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
95
96
97 static inline uint32_t
98 read_4(struct macb_softc *sc, bus_size_t off)
99 {
100
101         return (bus_read_4(sc->mem_res, off));
102 }
103
104 static inline void
105 write_4(struct macb_softc *sc, bus_size_t off, uint32_t val)
106 {
107
108         bus_write_4(sc->mem_res, off, val);
109 }
110
111
112 static devclass_t macb_devclass;
113
114 /* ifnet entry points */
115
116 static void     macbinit_locked(void *);
117 static void     macbstart_locked(struct ifnet *);
118
119 static void     macbinit(void *);
120 static void     macbstart(struct ifnet *);
121 static void     macbstop(struct macb_softc *);
122 static int      macbioctl(struct ifnet * ifp, u_long, caddr_t);
123
124 /* bus entry points */
125
126 static int      macb_probe(device_t dev);
127 static int      macb_attach(device_t dev);
128 static int      macb_detach(device_t dev);
129
130 /* helper functions */
131 static int
132 macb_new_rxbuf(struct macb_softc *sc, int index);
133
134 static void
135 macb_free_desc_dma_tx(struct macb_softc *sc);
136
137 static void
138 macb_free_desc_dma_rx(struct macb_softc *sc);
139
140 static void
141 macb_init_desc_dma_tx(struct macb_softc *sc);
142
143 static void
144 macb_watchdog(struct macb_softc *sc);
145
146 static int macb_intr_rx_locked(struct macb_softc *sc, int count);
147 static void macb_intr_task(void *arg, int pending __unused);
148 static void macb_intr(void *xsc);
149
150 static void
151 macb_tx_cleanup(struct macb_softc *sc);
152
153 static inline int
154 phy_write(struct macb_softc *sc, int phy, int reg, int data);
155
156 static void     macb_reset(struct macb_softc *sc);
157
158 static void
159 macb_deactivate(device_t dev)
160 {
161         struct macb_softc *sc;
162
163         sc = device_get_softc(dev);
164
165         macb_free_desc_dma_tx(sc);
166         macb_free_desc_dma_rx(sc);
167
168 }
169
170 static void
171 macb_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
172 {
173         bus_addr_t *paddr;
174
175         KASSERT(nsegs == 1, ("wrong number of segments, should be 1"));
176         paddr = arg;
177         *paddr = segs->ds_addr;
178 }
179
180 static int
181 macb_alloc_desc_dma_tx(struct macb_softc *sc)
182 {
183         int error, i;
184
185         /* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
186         error = bus_dma_tag_create(sc->sc_parent_tag,   /* parent */
187             16, 0,                      /* alignment, boundary */
188             BUS_SPACE_MAXADDR,          /* lowaddr */
189             BUS_SPACE_MAXADDR,          /* highaddr */
190             NULL, NULL,                 /* filtfunc, filtfuncarg */
191             sizeof(struct eth_tx_desc) * MACB_MAX_TX_BUFFERS, /* max size */
192             1,                          /* nsegments */
193             sizeof(struct eth_tx_desc) * MACB_MAX_TX_BUFFERS,
194             0,                          /* flags */
195             NULL, NULL,                 /* lockfunc, lockfuncarg */
196             &sc->dmatag_data_tx);       /* dmat */
197         if (error != 0) {
198                 device_printf(sc->dev,
199                     "Couldn't create TX descriptor dma tag\n");
200                 return (error);
201         }
202         /* Allocate memory for TX ring. */
203         error = bus_dmamem_alloc(sc->dmatag_data_tx,
204             (void**)&(sc->desc_tx), BUS_DMA_NOWAIT | BUS_DMA_ZERO |
205             BUS_DMA_COHERENT, &sc->dmamap_ring_tx);
206         if (error != 0) {
207                 device_printf(sc->dev, "failed to allocate TX dma memory\n");
208                 return (error);
209         }
210         /* Load Ring DMA. */
211         error = bus_dmamap_load(sc->dmatag_data_tx, sc->dmamap_ring_tx,
212             sc->desc_tx, sizeof(struct eth_tx_desc) * MACB_MAX_TX_BUFFERS,
213             macb_getaddr, &sc->ring_paddr_tx, BUS_DMA_NOWAIT);
214         if (error != 0) {
215                 device_printf(sc->dev, "can't load TX descriptor dma map\n");
216                 return (error);
217         }
218         /* Allocate a busdma tag for mbufs. No alignment restriction applys. */
219         error = bus_dma_tag_create(sc->sc_parent_tag,   /* parent */
220             1, 0,                       /* alignment, boundary */
221             BUS_SPACE_MAXADDR,          /* lowaddr */
222             BUS_SPACE_MAXADDR,          /* highaddr */
223             NULL, NULL,                 /* filtfunc, filtfuncarg */
224             MCLBYTES * MAX_FRAGMENT,    /* maxsize */
225             MAX_FRAGMENT,               /* nsegments */
226             MCLBYTES, 0,                /* maxsegsz, flags */
227             NULL, NULL,                 /* lockfunc, lockfuncarg */
228             &sc->dmatag_ring_tx);       /* dmat */
229         if (error != 0) {
230                 device_printf(sc->dev, "failed to create TX mbuf dma tag\n");
231                 return (error);
232         }
233
234         for (i = 0; i < MACB_MAX_TX_BUFFERS; i++) {
235                 /* Create dma map for each descriptor. */
236                 error = bus_dmamap_create(sc->dmatag_ring_tx, 0,
237                     &sc->tx_desc[i].dmamap);
238                 if (error != 0) {
239                         device_printf(sc->dev,
240                             "failed to create TX mbuf dma map\n");
241                         return (error);
242                 }
243         }
244         return (0);
245 }
246
247 static void
248 macb_free_desc_dma_tx(struct macb_softc *sc)
249 {
250         struct tx_desc_info *td;
251         int i;
252
253         /* TX buffers. */
254         if (sc->dmatag_ring_tx != NULL) {
255                 for (i = 0; i < MACB_MAX_TX_BUFFERS; i++) {
256                         td = &sc->tx_desc[i];
257                         if (td->dmamap != NULL) {
258                                 bus_dmamap_destroy(sc->dmatag_ring_tx,
259                                     td->dmamap);
260                                 td->dmamap = NULL;
261                         }
262                 }
263                 bus_dma_tag_destroy(sc->dmatag_ring_tx);
264                 sc->dmatag_ring_tx = NULL;
265         }
266
267         /* TX descriptor ring. */
268         if (sc->dmatag_data_tx != NULL) {
269                 if (sc->ring_paddr_tx != 0)
270                         bus_dmamap_unload(sc->dmatag_data_tx,
271                             sc->dmamap_ring_tx);
272                 if (sc->desc_tx != NULL)
273                         bus_dmamem_free(sc->dmatag_data_tx, sc->desc_tx,
274                             sc->dmamap_ring_tx);
275                 sc->ring_paddr_tx = 0;
276                 sc->desc_tx = NULL;
277                 bus_dma_tag_destroy(sc->dmatag_data_tx);
278                 sc->dmatag_data_tx = NULL;
279         }
280 }
281
282 static void
283 macb_init_desc_dma_tx(struct macb_softc *sc)
284 {
285         struct eth_tx_desc *desc;
286         int i;
287
288         MACB_LOCK_ASSERT(sc);
289
290         sc->tx_prod = 0;
291         sc->tx_cons = 0;
292         sc->tx_cnt = 0;
293
294         desc = &sc->desc_tx[0];
295         bzero(desc, sizeof(struct eth_tx_desc) * MACB_MAX_TX_BUFFERS);
296
297         for (i = 0; i < MACB_MAX_TX_BUFFERS; i++) {
298                 desc = &sc->desc_tx[i];
299                 if (i == MACB_MAX_TX_BUFFERS - 1)
300                         desc->flags = TD_OWN | TD_WRAP_MASK;
301                 else
302                         desc->flags = TD_OWN;
303         }
304
305         bus_dmamap_sync(sc->dmatag_data_tx, sc->dmamap_ring_tx,
306             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
307 }
308
309 static int
310 macb_alloc_desc_dma_rx(struct macb_softc *sc)
311 {
312         int error, i;
313
314         /* Allocate a busdma tag and DMA safe memory for RX descriptors. */
315         error = bus_dma_tag_create(sc->sc_parent_tag,   /* parent */
316             16, 0,                      /* alignment, boundary */
317             BUS_SPACE_MAXADDR,          /* lowaddr */
318             BUS_SPACE_MAXADDR,          /* highaddr */
319             NULL, NULL,                 /* filtfunc, filtfuncarg */
320             /* maxsize, nsegments */
321             sizeof(struct eth_rx_desc) * MACB_MAX_RX_BUFFERS, 1,
322             /* maxsegsz, flags */
323             sizeof(struct eth_rx_desc) * MACB_MAX_RX_BUFFERS, 0,
324             NULL, NULL,                 /* lockfunc, lockfuncarg */
325             &sc->dmatag_data_rx);       /* dmat */
326         if (error != 0) {
327                 device_printf(sc->dev,
328                     "Couldn't create RX descriptor dma tag\n");
329                 return (error);
330         }
331         /* Allocate RX ring. */
332         error = bus_dmamem_alloc(sc->dmatag_data_rx, (void**)&(sc->desc_rx),
333             BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT,
334             &sc->dmamap_ring_rx);
335         if (error != 0) {
336                 device_printf(sc->dev,
337                     "failed to allocate RX descriptor dma memory\n");
338                 return (error);
339         }
340
341         /* Load dmamap. */
342         error = bus_dmamap_load(sc->dmatag_data_rx, sc->dmamap_ring_rx,
343             sc->desc_rx, sizeof(struct eth_rx_desc) * MACB_MAX_RX_BUFFERS,
344             macb_getaddr, &sc->ring_paddr_rx, BUS_DMA_NOWAIT);
345         if (error != 0) {
346                 device_printf(sc->dev, "can't load RX descriptor dma map\n");
347                 return (error);
348         }
349
350         /* Allocate a busdma tag for mbufs. */
351         error = bus_dma_tag_create(sc->sc_parent_tag,/* parent */
352             16, 0,                      /* alignment, boundary */
353             BUS_SPACE_MAXADDR,          /* lowaddr */
354             BUS_SPACE_MAXADDR,          /* highaddr */
355             NULL, NULL,                 /* filtfunc, filtfuncarg */
356             MCLBYTES, 1,                /* maxsize, nsegments */
357             MCLBYTES, 0,                /* maxsegsz, flags */
358             NULL, NULL,                 /* lockfunc, lockfuncarg */
359             &sc->dmatag_ring_rx);       /* dmat */
360
361         if (error != 0) {
362                 device_printf(sc->dev, "failed to create RX mbuf dma tag\n");
363                 return (error);
364         }
365
366         for (i = 0; i < MACB_MAX_RX_BUFFERS; i++) {
367                 error = bus_dmamap_create(sc->dmatag_ring_rx, 0,
368                     &sc->rx_desc[i].dmamap);
369                 if (error != 0) {
370                         device_printf(sc->dev,
371                             "failed to create RX mbuf dmamap\n");
372                         return (error);
373                 }
374         }
375
376         return (0);
377 }
378
379 static void
380 macb_free_desc_dma_rx(struct macb_softc *sc)
381 {
382         struct rx_desc_info *rd;
383         int i;
384
385         /* RX buffers. */
386         if (sc->dmatag_ring_rx != NULL) {
387                 for (i = 0; i < MACB_MAX_RX_BUFFERS; i++) {
388                         rd = &sc->rx_desc[i];
389                         if (rd->dmamap != NULL) {
390                                 bus_dmamap_destroy(sc->dmatag_ring_rx,
391                                     rd->dmamap);
392                                 rd->dmamap = NULL;
393                         }
394                 }
395                 bus_dma_tag_destroy(sc->dmatag_ring_rx);
396                 sc->dmatag_ring_rx = NULL;
397         }
398         /* RX descriptor ring. */
399         if (sc->dmatag_data_rx != NULL) {
400                 if (sc->ring_paddr_rx != 0)
401                         bus_dmamap_unload(sc->dmatag_data_rx,
402                             sc->dmamap_ring_rx);
403                 if (sc->desc_rx != NULL)
404                         bus_dmamem_free(sc->dmatag_data_rx, sc->desc_rx,
405                             sc->dmamap_ring_rx);
406                 sc->ring_paddr_rx = 0;
407                 sc->desc_rx = NULL;
408                 bus_dma_tag_destroy(sc->dmatag_data_rx);
409                 sc->dmatag_data_rx = NULL;
410         }
411 }
412
413 static int
414 macb_init_desc_dma_rx(struct macb_softc *sc)
415 {
416         struct eth_rx_desc *desc;
417         struct rx_desc_info *rd;
418         int i;
419
420         MACB_LOCK_ASSERT(sc);
421
422         sc->rx_cons = 0;
423         desc = &sc->desc_rx[0];
424         bzero(desc, sizeof(struct eth_rx_desc) * MACB_MAX_RX_BUFFERS);
425         for (i = 0; i < MACB_MAX_RX_BUFFERS; i++) {
426                 rd = &sc->rx_desc[i];
427                 rd->buff = NULL;
428                 if (macb_new_rxbuf(sc, i) != 0)
429                         return (ENOBUFS);
430         }
431         bus_dmamap_sync(sc->dmatag_ring_rx, sc->dmamap_ring_rx,
432             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
433         return (0);
434 }
435
436 static int
437 macb_new_rxbuf(struct macb_softc *sc, int index)
438 {
439         struct rx_desc_info *rd;
440         struct eth_rx_desc *desc;
441         struct mbuf *m;
442         bus_dma_segment_t seg[1];
443         int error, nsegs;
444
445         m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
446         if (m == NULL)
447                 return (ENOBUFS);
448         m->m_len = m->m_pkthdr.len = MCLBYTES - ETHER_ALIGN;
449         rd = &sc->rx_desc[index];
450         bus_dmamap_unload(sc->dmatag_ring_rx, rd->dmamap);
451         error = bus_dmamap_load_mbuf_sg(sc->dmatag_ring_rx, rd->dmamap, m,
452             seg, &nsegs, 0);
453         KASSERT(nsegs == 1, ("Too many segments returned!"));
454         if (error != 0) {
455                 m_free(m);
456                 return (error);
457         }
458
459         bus_dmamap_sync(sc->dmatag_ring_rx, rd->dmamap, BUS_DMASYNC_PREREAD);
460         rd->buff = m;
461
462         desc = &sc->desc_rx[index];
463         desc->addr = seg[0].ds_addr;
464
465         desc->flags = DATA_SIZE;
466
467         if (index == MACB_MAX_RX_BUFFERS - 1)
468                 desc->addr |= RD_WRAP_MASK;
469
470         return (0);
471 }
472
473 static int
474 macb_allocate_dma(struct macb_softc *sc)
475 {
476         int error;
477
478         /* Create parent tag for tx and rx */
479         error = bus_dma_tag_create(
480             bus_get_dma_tag(sc->dev),   /* parent */
481             1, 0,                       /* alignment, boundary */
482             BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
483             BUS_SPACE_MAXADDR,          /* highaddr */
484             NULL, NULL,                 /* filter, filterarg */
485             BUS_SPACE_MAXSIZE_32BIT, 0, /* maxsize, nsegments */
486             BUS_SPACE_MAXSIZE_32BIT,    /* maxsegsize */
487             0,                          /* flags */
488             NULL, NULL,         /* lockfunc, lockarg */
489             &sc->sc_parent_tag);
490         if (error != 0) {
491                 device_printf(sc->dev, "Couldn't create parent DMA tag\n");
492                 return (error);
493         }
494
495         if ((error = macb_alloc_desc_dma_tx(sc)) != 0)
496                 return (error);
497         if ((error = macb_alloc_desc_dma_rx(sc)) != 0)
498                 return (error);
499         return (0);
500 }
501
502
503 static void
504 macb_tick(void *xsc)
505 {
506         struct macb_softc *sc;
507         struct mii_data *mii;
508
509         sc = xsc;
510         mii = device_get_softc(sc->miibus);
511         mii_tick(mii);
512         macb_watchdog(sc);
513         /*
514          * Schedule another timeout one second from now.
515          */
516         callout_reset(&sc->tick_ch, hz, macb_tick, sc);
517 }
518
519
520 static void
521 macb_watchdog(struct macb_softc *sc)
522 {
523         struct ifnet *ifp;
524
525         MACB_LOCK_ASSERT(sc);
526
527         if (sc->macb_watchdog_timer == 0 || --sc->macb_watchdog_timer)
528                 return;
529
530         ifp = sc->ifp;
531         if ((sc->flags & MACB_FLAG_LINK) == 0) {
532                 if_printf(ifp, "watchdog timeout (missed link)\n");
533                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
534                 return;
535         }
536
537         if_printf(ifp, "watchdog timeout\n");
538         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
539         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
540         macbinit_locked(sc);
541         if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
542                 macbstart_locked(ifp);
543 }
544
545
546
547 static void
548 macbinit_locked(void *xsc)
549 {
550         struct macb_softc *sc;
551         struct ifnet *ifp;
552         int err;
553         uint32_t config;
554         struct mii_data *mii;
555
556         sc = xsc;
557         ifp = sc->ifp;
558
559         MACB_LOCK_ASSERT(sc);
560
561         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
562                 return;
563
564         if ((err = macb_init_desc_dma_rx(sc)) != 0) {
565                 device_printf(sc->dev, "no memory for RX buffers\n");
566                 //ecestop(sc);
567                 return;
568         }
569         macb_init_desc_dma_tx(sc);
570
571         config = read_4(sc, EMAC_NCFGR) | (sc->clock << 10); /*set clock*/
572         config |= CFG_PAE;              /* PAuse Enable */
573         config |= CFG_DRFCS;            /* Discard Rx FCS */
574         config |= CFG_SPD;              /* 100 mbps*/
575         //config |= CFG_CAF;
576         config |= CFG_FD;
577
578         config |= CFG_RBOF_2; /*offset +2*/
579
580         write_4(sc, EMAC_NCFGR, config);
581
582         /* Initialize TX and RX buffers */
583         write_4(sc, EMAC_RBQP, sc->ring_paddr_rx);
584         write_4(sc, EMAC_TBQP, sc->ring_paddr_tx);
585
586         /* Enable TX and RX */
587         write_4(sc, EMAC_NCR, RX_ENABLE | TX_ENABLE | MPE_ENABLE);
588
589
590         /* Enable interrupts */
591         write_4(sc, EMAC_IER, (RCOMP_INTERRUPT |
592                                RXUBR_INTERRUPT |
593                                TUND_INTERRUPT |
594                                RLE_INTERRUPT |
595                                TXERR_INTERRUPT |
596                                ROVR_INTERRUPT |
597                                HRESP_INTERRUPT|
598                                TCOMP_INTERRUPT
599                         ));
600
601         /*
602          * Set 'running' flag, and clear output active flag
603          * and attempt to start the output
604          */
605         ifp->if_drv_flags |= IFF_DRV_RUNNING;
606         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
607
608         mii = device_get_softc(sc->miibus);
609
610         sc->flags |= MACB_FLAG_LINK;
611
612         mii_mediachg(mii);
613
614         callout_reset(&sc->tick_ch, hz, macb_tick, sc);
615 }
616
617
618 static void
619 macb_tx_cleanup(struct macb_softc *sc)
620 {
621         struct ifnet *ifp;
622         struct eth_tx_desc *desc;
623         struct tx_desc_info *td;
624         int flags;
625         int status;
626         int i;
627
628         MACB_LOCK_ASSERT(sc);
629
630         status = read_4(sc, EMAC_TSR);
631
632         write_4(sc, EMAC_TSR, status);
633
634         /*buffer underrun*/
635         if ((status & TSR_UND) != 0) {
636                 /*reset buffers*/
637                 printf("underrun\n");
638                 bus_dmamap_sync(sc->dmatag_data_tx, sc->dmamap_ring_tx,
639                     BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
640                 sc->tx_cons = sc->tx_prod = 0;
641                 for (i = 0; i < MACB_MAX_TX_BUFFERS; i++) {
642                         desc = &sc->desc_tx[i];
643                         desc->flags = TD_OWN;
644                 }
645
646                 for (i = 0; i < MACB_MAX_TX_BUFFERS; i++) {
647                         td = &sc->tx_desc[i];
648                         if (td->buff != NULL) {
649                                 /* We are finished with this descriptor. */
650                                 bus_dmamap_sync(sc->dmatag_ring_tx, td->dmamap,
651                                                 BUS_DMASYNC_POSTWRITE);
652                                 /* ... and unload, so we can reuse. */
653                                 bus_dmamap_unload(sc->dmatag_data_tx,
654                                                   td->dmamap);
655                                 m_freem(td->buff);
656                                 td->buff = NULL;
657                         }
658                 }
659         }
660
661         if ((status & TSR_COMP) == 0)
662                 return;
663
664
665         if (sc->tx_cons == sc->tx_prod)
666                 return;
667
668         ifp = sc->ifp;
669
670         /* Prepare to read the ring (owner bit). */
671         bus_dmamap_sync(sc->dmatag_data_tx, sc->dmamap_ring_tx,
672             BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
673         while (sc->tx_cons != sc->tx_prod) {
674                 desc = &sc->desc_tx[sc->tx_cons];
675                 if ((desc->flags & TD_OWN) == 0)
676                         break;
677
678                 td = &sc->tx_desc[sc->tx_cons];
679                 if (td->buff != NULL) {
680                         /* We are finished with this descriptor. */
681                         bus_dmamap_sync(sc->dmatag_ring_tx, td->dmamap,
682                                         BUS_DMASYNC_POSTWRITE);
683                         /* ... and unload, so we can reuse. */
684                         bus_dmamap_unload(sc->dmatag_data_tx,
685                                           td->dmamap);
686                         m_freem(td->buff);
687                         td->buff = NULL;
688                         if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
689                 }
690
691                 do {
692                         sc->tx_cnt--;
693                         MACB_DESC_INC(sc->tx_cons, MACB_MAX_TX_BUFFERS);
694                         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
695                         flags = desc->flags;
696                         desc->flags = TD_OWN;
697                         desc = &sc->desc_tx[sc->tx_cons];
698                         if (flags & TD_LAST) {
699                                 break;
700                         }
701                 } while (sc->tx_cons != sc->tx_prod);
702         }
703
704         /* Unarm watchog timer when there is no pending descriptors in queue. */
705         if (sc->tx_cnt == 0)
706                 sc->macb_watchdog_timer = 0;
707 }
708
709 static void
710 macb_rx(struct macb_softc *sc)
711 {
712         struct eth_rx_desc      *rxdesc;
713         struct ifnet *ifp;
714         struct mbuf *m;
715         int rxbytes;
716         int flags;
717         int nsegs;
718         int first;
719
720         rxdesc = &(sc->desc_rx[sc->rx_cons]);
721
722         ifp = sc->ifp;
723
724         bus_dmamap_sync(sc->dmatag_ring_rx, sc->dmamap_ring_rx,
725             BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
726
727
728         nsegs = 0;
729         while (rxdesc->addr & RD_OWN) {
730
731                 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
732                         break;
733
734                 flags = rxdesc->flags;
735
736                 rxbytes = flags & RD_LEN_MASK;
737
738                 m = sc->rx_desc[sc->rx_cons].buff;
739
740                 bus_dmamap_sync(sc->dmatag_ring_rx,
741                     sc->rx_desc[sc->rx_cons].dmamap, BUS_DMASYNC_POSTREAD);
742                 if (macb_new_rxbuf(sc, sc->rx_cons) != 0) {
743                         if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
744                         first = sc->rx_cons;
745                         
746                         do  {
747                                 rxdesc->flags = DATA_SIZE;
748                                 MACB_DESC_INC(sc->rx_cons, MACB_MAX_RX_BUFFERS);
749                                 if ((rxdesc->flags & RD_EOF) != 0)
750                                         break;
751                                 rxdesc = &(sc->desc_rx[sc->rx_cons]);
752                         } while (sc->rx_cons != first);
753
754                         if (sc->macb_cdata.rxhead != NULL) {
755                                 m_freem(sc->macb_cdata.rxhead);
756                                 sc->macb_cdata.rxhead = NULL;
757                                 sc->macb_cdata.rxtail = NULL;                           
758                         }
759                         
760                         break;
761                 }
762
763                 nsegs++;
764
765                 /* Chain received mbufs. */
766                 if (sc->macb_cdata.rxhead == NULL) {
767                         m->m_data += 2;
768                         sc->macb_cdata.rxhead = m;
769                         sc->macb_cdata.rxtail = m;
770                         if (flags & RD_EOF)
771                                 m->m_len = rxbytes;
772                         else
773                                 m->m_len = DATA_SIZE - 2;
774                 } else {
775                         m->m_flags &= ~M_PKTHDR;
776                         m->m_len = DATA_SIZE;
777                         sc->macb_cdata.rxtail->m_next = m;
778                         sc->macb_cdata.rxtail = m;
779                 }
780
781                 if (flags & RD_EOF) {
782
783                         if (nsegs > 1) {
784                                 sc->macb_cdata.rxtail->m_len = (rxbytes -
785                                     ((nsegs - 1) * DATA_SIZE)) + 2;
786                         }
787
788                         m = sc->macb_cdata.rxhead;
789                         m->m_flags |= M_PKTHDR;
790                         m->m_pkthdr.len = rxbytes;
791                         m->m_pkthdr.rcvif = ifp;
792                         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
793
794                         nsegs = 0;
795                         MACB_UNLOCK(sc);
796                         (*ifp->if_input)(ifp, m);
797                         MACB_LOCK(sc);
798                         sc->macb_cdata.rxhead = NULL;
799                         sc->macb_cdata.rxtail = NULL;
800
801                 }
802                 
803                 rxdesc->addr &= ~RD_OWN;
804
805                 MACB_DESC_INC(sc->rx_cons, MACB_MAX_RX_BUFFERS);
806
807                 rxdesc = &(sc->desc_rx[sc->rx_cons]);
808         }
809
810         write_4(sc, EMAC_IER, (RCOMP_INTERRUPT|RXUBR_INTERRUPT));
811
812 }
813
814 static int
815 macb_intr_rx_locked(struct macb_softc *sc, int count)
816 {
817         macb_rx(sc);
818         return (0);
819 }
820
821 static void
822 macb_intr_task(void *arg, int pending __unused)
823 {
824         struct macb_softc *sc;
825
826         sc = arg;
827         MACB_LOCK(sc);
828         macb_intr_rx_locked(sc, -1);
829         MACB_UNLOCK(sc);
830 }
831
832 static void
833 macb_intr(void *xsc)
834 {
835         struct macb_softc *sc;
836         struct ifnet *ifp;
837         uint32_t status;
838
839         sc = xsc;
840         ifp = sc->ifp;
841         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
842                 printf("not running\n");
843                 return;
844         }
845
846         MACB_LOCK(sc);
847         status = read_4(sc, EMAC_ISR);
848
849         while (status) {
850                 if (status & RCOMP_INTERRUPT) {
851                         write_4(sc, EMAC_IDR, (RCOMP_INTERRUPT|RXUBR_INTERRUPT));
852                         taskqueue_enqueue(sc->sc_tq, &sc->sc_intr_task);
853                 }
854
855                 if (status & TCOMP_INTERRUPT) {
856                         macb_tx_cleanup(sc);
857                 }
858
859                 status = read_4(sc, EMAC_ISR);
860         }
861
862         if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
863                 macbstart_locked(ifp);
864         MACB_UNLOCK(sc);
865 }
866
867 static inline int
868 macb_encap(struct macb_softc *sc, struct mbuf **m_head)
869 {
870         struct eth_tx_desc *desc;
871         struct tx_desc_info *txd, *txd_last;
872         struct mbuf *m;
873         bus_dma_segment_t segs[MAX_FRAGMENT];
874         bus_dmamap_t map;
875         uint32_t csum_flags;
876         int error, i, nsegs, prod, si;
877
878         M_ASSERTPKTHDR((*m_head));
879
880         prod = sc->tx_prod;
881
882         m = *m_head;
883
884         txd = txd_last = &sc->tx_desc[prod];
885         error = bus_dmamap_load_mbuf_sg(sc->dmatag_ring_tx, txd->dmamap,
886             *m_head, segs, &nsegs, 0);
887         if (error == EFBIG) {
888                 m = m_collapse(*m_head, M_NOWAIT, MAX_FRAGMENT);
889                 if (m == NULL) {
890                         m_freem(*m_head);
891                         *m_head = NULL;
892                         return (ENOMEM);
893                 }
894                 *m_head = m;
895                 error = bus_dmamap_load_mbuf_sg(sc->dmatag_ring_tx, txd->dmamap,
896                     *m_head, segs, &nsegs, 0);
897                 if (error != 0) {
898                         m_freem(*m_head);
899                         *m_head = NULL;
900                         return (error);
901                 }
902         } else if (error != 0) {
903                 return (error);
904         }
905         /* Check for TX descriptor overruns. */
906         if (sc->tx_cnt + nsegs > MACB_MAX_TX_BUFFERS - 1) {
907                 bus_dmamap_unload(sc->dmatag_ring_tx, txd->dmamap);
908                 return (ENOBUFS);
909         }
910         bus_dmamap_sync(sc->dmatag_ring_tx, txd->dmamap, BUS_DMASYNC_PREWRITE);
911         m = *m_head;
912
913         /* TODO: VLAN hardware tag insertion. */
914
915         csum_flags = 0;
916         si = prod;
917         desc = NULL;
918
919         for (i = 0; i < nsegs; i++) {
920                 desc = &sc->desc_tx[prod];
921                 desc->addr = segs[i].ds_addr;
922
923                 if (i == 0 ) {
924                         desc->flags = segs[i].ds_len | TD_OWN;
925                 } else {
926                         desc->flags = segs[i].ds_len;
927                 }
928
929                 if (prod == MACB_MAX_TX_BUFFERS - 1)
930                         desc->flags |= TD_WRAP_MASK;
931
932                 sc->tx_cnt++;
933                 MACB_DESC_INC(prod, MACB_MAX_TX_BUFFERS);
934         }
935         /*
936          * Set EOP on the last fragment.
937          */
938
939         desc->flags |= TD_LAST;
940         desc = &sc->desc_tx[si];
941         desc->flags &= ~TD_OWN;
942
943         sc->tx_prod = prod;
944
945         /* Swap the first dma map and the last. */
946         map = txd_last->dmamap;
947         txd_last->dmamap = txd->dmamap;
948         txd->dmamap = map;
949         txd->buff = m;
950
951         return (0);
952 }
953
954
955 static void
956 macbstart_locked(struct ifnet *ifp)
957 {
958
959
960
961         struct macb_softc *sc;
962         struct mbuf *m0;
963 #if 0
964         struct mbuf *m_new;
965 #endif
966         int queued = 0;
967
968         sc = ifp->if_softc;
969
970         if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
971             IFF_DRV_RUNNING || (sc->flags & MACB_FLAG_LINK) == 0) {
972                 return;
973         }
974
975         while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
976                 /* Get packet from the queue */
977                 IF_DEQUEUE(&ifp->if_snd, m0);
978                 if (m0 == NULL)
979                         break;
980 #if 0
981                 if (m0->m_next != NULL) {
982                         /* Fragmented mbuf chain, collapse it. */
983                         m_new = m_defrag(m0, M_NOWAIT);
984                         if (m_new != NULL) {
985                                 /* Original frame freed. */
986                                 m0 = m_new;
987                         } else {
988                                 /* Defragmentation failed, just use the chain. */
989                         }
990                 }
991 #endif
992                 if (macb_encap(sc, &m0)) {
993                         if (m0 == NULL)
994                                 break;
995                         IF_PREPEND(&ifp->if_snd, m0);
996                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
997                         break;
998                 }
999                 queued++;
1000                 BPF_MTAP(ifp, m0);
1001         }
1002         if (IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1003                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1004         if (queued) {
1005                 bus_dmamap_sync(sc->dmatag_data_tx, sc->dmamap_ring_tx,
1006                     BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1007                 write_4(sc, EMAC_NCR, read_4(sc, EMAC_NCR) | TRANSMIT_START);
1008                 sc->macb_watchdog_timer = MACB_TIMEOUT;
1009         }
1010 }
1011
1012 static void
1013 macbinit(void *xsc)
1014 {
1015         struct macb_softc *sc = xsc;
1016
1017         MACB_LOCK(sc);
1018         macbinit_locked(sc);
1019         MACB_UNLOCK(sc);
1020 }
1021
1022 static void
1023 macbstart(struct ifnet *ifp)
1024 {
1025         struct macb_softc *sc = ifp->if_softc;
1026         MACB_ASSERT_UNLOCKED(sc);
1027         MACB_LOCK(sc);
1028         macbstart_locked(ifp);
1029         MACB_UNLOCK(sc);
1030
1031 }
1032
1033
1034 static void
1035 macbstop(struct macb_softc *sc)
1036 {
1037         struct ifnet *ifp = sc->ifp;
1038         struct rx_desc_info *rd;
1039         struct tx_desc_info *td;
1040         int i;
1041
1042         ifp = sc->ifp;
1043
1044         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1045
1046         macb_reset(sc);
1047
1048         sc->flags &= ~MACB_FLAG_LINK;
1049         callout_stop(&sc->tick_ch);
1050         sc->macb_watchdog_timer = 0;
1051
1052         /* Free TX/RX mbufs still in the queues. */
1053         for (i = 0; i < MACB_MAX_TX_BUFFERS; i++) {
1054                 td = &sc->tx_desc[i];
1055                 if (td->buff != NULL) {
1056                         bus_dmamap_sync(sc->dmatag_ring_tx, td->dmamap,
1057                             BUS_DMASYNC_POSTWRITE);
1058                         bus_dmamap_unload(sc->dmatag_data_tx, td->dmamap);
1059                         m_freem(td->buff);
1060                         td->buff = NULL;
1061                 }
1062         }
1063         for (i = 0; i < MACB_MAX_RX_BUFFERS; i++) {
1064                 rd = &sc->rx_desc[i];
1065                 if (rd->buff != NULL) {
1066                         bus_dmamap_sync(sc->dmatag_ring_rx, rd->dmamap,
1067                             BUS_DMASYNC_POSTREAD);
1068                         bus_dmamap_unload(sc->dmatag_data_rx, rd->dmamap);
1069                         m_freem(rd->buff);
1070                         rd->buff = NULL;
1071                 }
1072         }       
1073 }
1074
1075 static int
1076 get_hash_index(uint8_t *mac)
1077 {
1078         int i, j, k;
1079         int result;
1080         int bit;
1081
1082         result = 0;
1083         for (i = 0; i < 6; i++) {
1084                 bit = 0;
1085                 for (j = 0; j < 8;  j++) {
1086                         k = j * 6 + i;
1087                         bit ^= (mac[k/8] & (1 << (k % 8)) ) != 0;
1088                 }
1089                 result |= bit;
1090         }
1091         return result;
1092 }
1093
1094 static void
1095 set_mac_filter(uint32_t *filter, uint8_t *mac)
1096 {
1097         int bits;
1098
1099         bits = get_hash_index(mac);
1100         filter[bits >> 5] |= 1 << (bits & 31);
1101 }
1102
1103 static void
1104 set_filter(struct macb_softc *sc)
1105 {
1106         struct ifnet *ifp;
1107         struct ifmultiaddr *ifma;
1108         int config;
1109         int count;
1110         uint32_t multicast_filter[2];
1111
1112         ifp = sc->ifp;
1113
1114         config = read_4(sc, EMAC_NCFGR);
1115         
1116         config &= ~(CFG_CAF | CFG_MTI);
1117         write_4(sc, EMAC_HRB, 0);
1118         write_4(sc, EMAC_HRT, 0);
1119
1120         if ((ifp->if_flags & (IFF_ALLMULTI |IFF_PROMISC)) != 0){
1121                 if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
1122                         write_4(sc, EMAC_HRB, ~0);
1123                         write_4(sc, EMAC_HRT, ~0);
1124                         config |= CFG_MTI;
1125                 }
1126                 if ((ifp->if_flags & IFF_PROMISC) != 0) {
1127                         config |= CFG_CAF;
1128                 }
1129                 write_4(sc, EMAC_NCFGR, config);
1130                 return;
1131         }
1132
1133         if_maddr_rlock(ifp);
1134         count = 0;
1135         multicast_filter[0] = 0;
1136         multicast_filter[1] = 0;
1137
1138         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1139                 if (ifma->ifma_addr->sa_family != AF_LINK)
1140                         continue;
1141                 count++;
1142                 set_mac_filter(multicast_filter,
1143                            LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1144         }
1145         if (count) {
1146                 write_4(sc, EMAC_HRB, multicast_filter[0]);
1147                 write_4(sc, EMAC_HRT, multicast_filter[1]);
1148                 write_4(sc, EMAC_NCFGR, config|CFG_MTI);
1149         }
1150         if_maddr_runlock(ifp);
1151 }
1152
1153 static int
1154 macbioctl(struct ifnet * ifp, u_long cmd, caddr_t data)
1155 {
1156
1157         struct macb_softc *sc = ifp->if_softc;
1158         struct mii_data *mii;
1159         struct ifreq *ifr = (struct ifreq *)data;
1160
1161         int error = 0;
1162
1163         switch (cmd) {
1164         case SIOCSIFFLAGS:
1165                 MACB_LOCK(sc);
1166
1167                 if ((ifp->if_flags & IFF_UP) != 0) {
1168                         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1169                                 if (((ifp->if_flags ^ sc->if_flags)
1170                                     & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
1171                                         set_filter(sc);
1172                         } else {
1173                                 macbinit_locked(sc);
1174                         }
1175                 } else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1176                         macbstop(sc);
1177                 }
1178                 sc->if_flags = ifp->if_flags;
1179                 MACB_UNLOCK(sc);
1180                 break;
1181         case SIOCADDMULTI:
1182         case SIOCDELMULTI:
1183                 MACB_LOCK(sc);
1184                 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1185                         set_filter(sc);
1186
1187                 MACB_UNLOCK(sc);
1188                 break;
1189         case SIOCSIFMEDIA:
1190         case SIOCGIFMEDIA:
1191                 mii = device_get_softc(sc->miibus);
1192                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1193                 break;
1194         default:
1195                 error = ether_ioctl(ifp, cmd, data);
1196                 break;
1197         }
1198         return (error);
1199
1200 }
1201
1202 /* bus entry points */
1203
1204 static int
1205 macb_probe(device_t dev)
1206 {
1207 #ifdef FDT
1208         if (!ofw_bus_is_compatible(dev, "cdns,at32ap7000-macb"))
1209                 return (ENXIO);
1210 #endif
1211
1212         device_set_desc(dev, "macb");
1213         return (0);
1214 }
1215
1216 /*
1217  * Change media according to request.
1218  */
1219 static int
1220 macb_ifmedia_upd(struct ifnet *ifp)
1221 {
1222         struct macb_softc *sc = ifp->if_softc;
1223         struct mii_data *mii;
1224
1225         mii = device_get_softc(sc->miibus);
1226         MACB_LOCK(sc);
1227         mii_mediachg(mii);
1228         MACB_UNLOCK(sc);
1229         return (0);
1230 }
1231
1232 /*
1233  * Notify the world which media we're using.
1234  */
1235 static void
1236 macb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1237 {
1238         struct macb_softc *sc = ifp->if_softc;
1239         struct mii_data *mii;
1240
1241         mii = device_get_softc(sc->miibus);
1242
1243         MACB_LOCK(sc);
1244         /* Don't report link state if driver is not running. */
1245         if ((ifp->if_flags & IFF_UP) == 0) {
1246                 MACB_UNLOCK(sc);
1247                 return;
1248         }
1249         mii_pollstat(mii);
1250         ifmr->ifm_active = mii->mii_media_active;
1251         ifmr->ifm_status = mii->mii_media_status;
1252         MACB_UNLOCK(sc);
1253 }
1254
1255 static void
1256 macb_reset(struct macb_softc *sc)
1257 {
1258         /*
1259          * Disable RX and TX
1260          */
1261         write_4(sc, EMAC_NCR, 0);
1262
1263         write_4(sc, EMAC_NCR, CLEAR_STAT);
1264
1265         /* Clear all status flags */
1266         write_4(sc, EMAC_TSR, ~0UL);
1267         write_4(sc, EMAC_RSR, ~0UL);
1268
1269         /* Disable all interrupts */
1270         write_4(sc, EMAC_IDR, ~0UL);
1271         read_4(sc, EMAC_ISR);
1272
1273 }
1274
1275
1276 static int
1277 macb_get_mac(struct macb_softc *sc, u_char *eaddr)
1278 {
1279         uint32_t bottom;
1280         uint16_t top;
1281
1282         bottom = read_4(sc, EMAC_SA1B);
1283         top = read_4(sc, EMAC_SA1T);
1284
1285         eaddr[0] = bottom & 0xff;
1286         eaddr[1] = (bottom >> 8) & 0xff;
1287         eaddr[2] = (bottom >> 16) & 0xff;
1288         eaddr[3] = (bottom >> 24) & 0xff;
1289         eaddr[4] = top & 0xff;
1290         eaddr[5] = (top >> 8) & 0xff;
1291
1292         return (0);
1293 }
1294
1295
1296 #ifdef FDT
1297 /*
1298  * We have to know if we're using MII or RMII attachment
1299  * for the MACB to talk to the PHY correctly. With FDT,
1300  * we must use rmii if there's a proprety phy-mode
1301  * equal to "rmii". Otherwise we MII mode is used.
1302  */
1303 static void
1304 macb_set_rmii(struct macb_softc *sc)
1305 {
1306         phandle_t node;
1307         char prop[10];
1308         ssize_t len;
1309
1310         node = ofw_bus_get_node(sc->dev);
1311         memset(prop, 0 ,sizeof(prop));
1312         len = OF_getproplen(node, "phy-mode");
1313         if (len != 4)
1314                 return;
1315         if (OF_getprop(node, "phy-mode", prop, len) != len)
1316                 return;
1317         if (strncmp(prop, "rmii", 4) == 0)
1318                 sc->use_rmii = USRIO_RMII;
1319 }
1320 #else
1321 /*
1322  * We have to know if we're using MII or RMII attachment
1323  * for the MACB to talk to the PHY correctly. Without FDT,
1324  * there's no good way to do this. So, if the config file
1325  * has 'option AT91_MACB_USE_RMII', then we'll force RMII.
1326  * Otherwise, we'll use what the bootloader setup. Either
1327  * it setup RMII or MII, in which case we'll get it right,
1328  * or it did nothing, and we'll fall back to MII and the
1329  * option would override if present.
1330  */
1331 static void
1332 macb_set_rmii(struct macb_softc *sc)
1333 {
1334 #ifdef AT91_MACB_USE_RMII
1335         sc->use_rmii = USRIO_RMII;
1336 #else
1337         sc->use_rmii = read_4(sc, EMAC_USRIO) & USRIO_RMII;
1338 #endif
1339 }
1340 #endif
1341
1342 static int
1343 macb_attach(device_t dev)
1344 {
1345         struct macb_softc *sc;
1346         struct ifnet *ifp = NULL;
1347         struct sysctl_ctx_list *sctx;
1348         struct sysctl_oid *soid;
1349         int pclk_hz;
1350         u_char eaddr[ETHER_ADDR_LEN];
1351         int rid;
1352         int err;
1353         struct at91_pmc_clock *master;
1354
1355
1356         err = 0;
1357
1358         sc = device_get_softc(dev);
1359         sc->dev = dev;
1360
1361         MACB_LOCK_INIT(sc);
1362
1363         callout_init_mtx(&sc->tick_ch, &sc->sc_mtx, 0);
1364
1365         /*
1366          * Allocate resources.
1367          */
1368         rid = 0;
1369         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1370             RF_ACTIVE);
1371         if (sc->mem_res == NULL) {
1372                 device_printf(dev, "could not allocate memory resources.\n");
1373                 err = ENOMEM;
1374                 goto out;
1375         }
1376         rid = 0;
1377         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1378             RF_ACTIVE);
1379         if (sc->irq_res == NULL) {
1380                 device_printf(dev, "could not allocate interrupt resources.\n");
1381                 err = ENOMEM;
1382                 goto out;
1383         }
1384
1385         /*setup clock*/
1386         sc->clk = at91_pmc_clock_ref(device_get_nameunit(sc->dev));
1387         at91_pmc_clock_enable(sc->clk);
1388
1389         macb_reset(sc);
1390         macb_get_mac(sc, eaddr);
1391
1392         master = at91_pmc_clock_ref("mck");
1393
1394         pclk_hz = master->hz;
1395
1396         sc->clock = CFG_CLK_8;
1397         if (pclk_hz <= 20000000)
1398                 sc->clock = CFG_CLK_8;
1399         else if (pclk_hz <= 40000000)
1400                 sc->clock = CFG_CLK_16;
1401         else if (pclk_hz <= 80000000)
1402                 sc->clock = CFG_CLK_32;
1403         else
1404                 sc->clock = CFG_CLK_64;
1405
1406         sc->clock = sc->clock << 10;
1407
1408         macb_set_rmii(sc);
1409         write_4(sc, EMAC_NCFGR, sc->clock);
1410         write_4(sc, EMAC_USRIO, USRIO_CLOCK | sc->use_rmii);       //enable clock
1411
1412         write_4(sc, EMAC_NCR, MPE_ENABLE); //enable MPE
1413
1414         sc->ifp = ifp = if_alloc(IFT_ETHER);
1415         err = mii_attach(dev, &sc->miibus, ifp, macb_ifmedia_upd,
1416             macb_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
1417         if (err != 0) {
1418                 device_printf(dev, "attaching PHYs failed\n");
1419                 goto out;
1420         }
1421
1422         if (macb_allocate_dma(sc) != 0)
1423                 goto out;
1424
1425         /* Sysctls */
1426         sctx = device_get_sysctl_ctx(dev);
1427         soid = device_get_sysctl_tree(dev);
1428
1429         ifp->if_softc = sc;
1430         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1431         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1432         ifp->if_capabilities |= IFCAP_VLAN_MTU;
1433         ifp->if_capenable |= IFCAP_VLAN_MTU;    /* The hw bits already set. */
1434         ifp->if_start = macbstart;
1435         ifp->if_ioctl = macbioctl;
1436         ifp->if_init = macbinit;
1437         IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
1438         ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
1439         IFQ_SET_READY(&ifp->if_snd);
1440         sc->if_flags = ifp->if_flags;
1441
1442         TASK_INIT(&sc->sc_intr_task, 0, macb_intr_task, sc);
1443
1444         sc->sc_tq = taskqueue_create_fast("macb_taskq", M_WAITOK,
1445             taskqueue_thread_enqueue, &sc->sc_tq);
1446         if (sc->sc_tq == NULL) {
1447                 device_printf(sc->dev, "could not create taskqueue\n");
1448                 goto out;
1449         }
1450
1451         ether_ifattach(ifp, eaddr);
1452
1453         /*
1454          * Activate the interrupt.
1455          */
1456         err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
1457             NULL, macb_intr, sc, &sc->intrhand);
1458         if (err) {
1459                 device_printf(dev, "could not establish interrupt handler.\n");
1460                 ether_ifdetach(ifp);
1461                 goto out;
1462         }
1463
1464         taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq",
1465             device_get_nameunit(sc->dev));
1466
1467         sc->macb_cdata.rxhead = 0;
1468         sc->macb_cdata.rxtail = 0;
1469
1470         phy_write(sc, 0, 0, 0x3300); //force autoneg
1471
1472         return (0);
1473 out:
1474
1475         return (err);
1476 }
1477
1478 static int
1479 macb_detach(device_t dev)
1480 {
1481         struct macb_softc *sc;
1482
1483         sc = device_get_softc(dev);
1484         ether_ifdetach(sc->ifp);
1485         MACB_LOCK(sc);
1486         macbstop(sc);
1487         MACB_UNLOCK(sc);
1488         callout_drain(&sc->tick_ch);
1489         bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
1490         taskqueue_drain(sc->sc_tq, &sc->sc_intr_task);
1491         taskqueue_free(sc->sc_tq);
1492         macb_deactivate(dev);
1493         bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
1494         bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
1495         MACB_LOCK_DESTROY(sc);
1496
1497         return (0);
1498 }
1499
1500 /*PHY related functions*/
1501 static inline int
1502 phy_read(struct macb_softc *sc, int phy, int reg)
1503 {
1504         int val;
1505
1506         write_4(sc, EMAC_MAN, EMAC_MAN_REG_RD(phy, reg));
1507         while ((read_4(sc, EMAC_SR) & EMAC_SR_IDLE) == 0)
1508                 continue;
1509         val = read_4(sc, EMAC_MAN) & EMAC_MAN_VALUE_MASK;
1510
1511         return (val);
1512 }
1513
1514 static inline int
1515 phy_write(struct macb_softc *sc, int phy, int reg, int data)
1516 {
1517
1518         write_4(sc, EMAC_MAN, EMAC_MAN_REG_WR(phy, reg, data));
1519         while ((read_4(sc, EMAC_SR) & EMAC_SR_IDLE) == 0)
1520                 continue;
1521
1522         return (0);
1523 }
1524
1525 /*
1526  * MII bus support routines.
1527  */
1528 static int
1529 macb_miibus_readreg(device_t dev, int phy, int reg)
1530 {
1531         struct macb_softc *sc;
1532         sc = device_get_softc(dev);
1533         return (phy_read(sc, phy, reg));
1534 }
1535
1536 static int
1537 macb_miibus_writereg(device_t dev, int phy, int reg, int data)
1538 {
1539         struct macb_softc *sc;
1540         sc = device_get_softc(dev);
1541         return (phy_write(sc, phy, reg, data));
1542 }
1543
1544 static void
1545 macb_child_detached(device_t dev, device_t child)
1546 {
1547         struct macb_softc *sc;
1548         sc = device_get_softc(dev);
1549
1550 }
1551
1552 static void
1553 macb_miibus_statchg(device_t dev)
1554 {
1555         struct macb_softc *sc;
1556         struct mii_data *mii;
1557         int config;
1558
1559         sc = device_get_softc(dev);
1560
1561         mii = device_get_softc(sc->miibus);
1562
1563         sc->flags &= ~MACB_FLAG_LINK;
1564
1565         config = read_4(sc, EMAC_NCFGR);
1566
1567         if ((mii->mii_media_status & IFM_AVALID) != 0) {
1568                 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1569                 case IFM_10_T:
1570                         config &= ~(CFG_SPD);
1571                         sc->flags |= MACB_FLAG_LINK;
1572                         break;
1573                 case IFM_100_TX:
1574                         config |= CFG_SPD;
1575                         sc->flags |= MACB_FLAG_LINK;
1576                         break;
1577                 default:
1578                         break;
1579                 }
1580         }
1581
1582         config |= CFG_FD;
1583         write_4(sc, EMAC_NCFGR, config);
1584 }
1585
1586 static device_method_t macb_methods[] = {
1587         /* Device interface */
1588         DEVMETHOD(device_probe, macb_probe),
1589         DEVMETHOD(device_attach,        macb_attach),
1590         DEVMETHOD(device_detach,        macb_detach),
1591
1592         /* Bus interface */
1593         DEVMETHOD(bus_child_detached,   macb_child_detached),
1594
1595         /* MII interface */
1596         DEVMETHOD(miibus_readreg,       macb_miibus_readreg),
1597         DEVMETHOD(miibus_writereg,      macb_miibus_writereg),
1598         DEVMETHOD(miibus_statchg,       macb_miibus_statchg),
1599         { 0, 0 }
1600 };
1601
1602 static driver_t macb_driver = {
1603         "macb",
1604         macb_methods,
1605         sizeof(struct macb_softc),
1606 };
1607
1608
1609 #ifdef FDT
1610 DRIVER_MODULE(macb, simplebus, macb_driver, macb_devclass, NULL, NULL);
1611 #else
1612 DRIVER_MODULE(macb, atmelarm, macb_driver, macb_devclass, 0, 0);
1613 #endif
1614 DRIVER_MODULE(miibus, macb, miibus_driver, miibus_devclass, 0, 0);
1615 MODULE_DEPEND(macb, miibus, 1, 1, 1);
1616 MODULE_DEPEND(macb, ether, 1, 1, 1);