1 /* $NetBSD: smc90cx6.c,v 1.38 2001/07/07 15:57:53 thorpej Exp $ */
7 * Copyright (c) 1994, 1995, 1998 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Ignatios Souvatzis.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
43 * Chip core driver for the SMC90c26 / SMC90c56 (and SMC90c66 in '56
44 * compatibility mode) boards
47 /* #define CMSOFTCOPY */
48 #define CMRETRANSMIT /**/
49 /* #define CM_DEBUG */
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/sockio.h>
55 #include <sys/module.h>
56 #include <sys/kernel.h>
57 #include <sys/socket.h>
58 #include <sys/syslog.h>
61 #include <machine/bus.h>
63 #include <machine/resource.h>
65 #if __FreeBSD_version < 500000
66 #include <machine/clock.h>
70 #include <net/if_dl.h>
71 #include <net/if_types.h>
72 #include <net/if_arc.h>
74 #include <dev/cm/smc90cx6reg.h>
75 #include <dev/cm/smc90cx6var.h>
77 MODULE_DEPEND(if_cm, arcnet, 1, 1, 1);
79 /* these should be elsewhere */
82 #define ARC_MIN_FORBID_LEN 254
83 #define ARC_MAX_FORBID_LEN 256
84 #define ARC_MAX_LEN 508
85 #define ARC_ADDR_LEN 1
87 /* for watchdog timer. This should be more than enough. */
88 #define ARCTIMEOUT (5*IFNET_SLOWHZ)
93 bus_space_read_1(rman_get_bustag((sc)->port_res), \
94 rman_get_bushandle((sc)->port_res), \
96 #define PUTREG(off, value) \
97 bus_space_write_1(rman_get_bustag((sc)->port_res), \
98 rman_get_bushandle((sc)->port_res), \
100 #define GETMEM(off) \
101 bus_space_read_1(rman_get_bustag((sc)->mem_res), \
102 rman_get_bushandle((sc)->mem_res), \
104 #define PUTMEM(off, value) \
105 bus_space_write_1(rman_get_bustag((sc)->mem_res), \
106 rman_get_bushandle((sc)->mem_res), \
109 devclass_t cm_devclass;
112 * This currently uses 2 bufs for tx, 2 for rx
116 * rx has a fillcount variable. If fillcount > (NRXBUF-1),
117 * rx can be switched off from rx hard int.
118 * Else rx is restarted on the other receiver.
119 * rx soft int counts down. if it is == (NRXBUF-1), it restarts
121 * To ensure packet ordering (we need that for 1201 later), we have a counter
122 * which is incremented modulo 256 on each receive and a per buffer
123 * variable, which is set to the counter on filling. The soft int can
124 * compare both values to determine the older packet.
126 * Transmit direction:
128 * cm_start checks tx_fillcount
131 * else fill tx_act ^ 1 && inc tx_fillcount
133 * check tx_fillcount again.
134 * case 2: set IFF_OACTIVE to stop arc_output from filling us.
137 * tint clears IFF_OCATIVE, decrements and checks tx_fillcount
138 * case 1: start tx on tx_act ^ 1, softcall cm_start
139 * case 0: softcall cm_start
141 * #define fill(i) get mbuf && copy mbuf to chip(i)
144 void cm_init(void *);
145 void cm_reset(struct cm_softc *);
146 void cm_start(struct ifnet *);
147 int cm_ioctl(struct ifnet *, unsigned long, caddr_t);
148 void cm_watchdog(struct ifnet *);
149 void cm_srint(void *vsc);
150 static void cm_tint(struct cm_softc *, int);
151 void cm_reconwatch(void *);
158 struct cm_softc *sc = device_get_softc(dev);
160 error = cm_alloc_port(dev, 0, CM_IO_PORTS);
164 if (GETREG(CMSTAT) == 0xff)
167 error = cm_alloc_memory(dev, 0, 0x800);
175 * Allocate a port resource with the given resource id.
178 cm_alloc_port(dev, rid, size)
183 struct cm_softc *sc = device_get_softc(dev);
184 struct resource *res;
186 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
187 0ul, ~0ul, size, RF_ACTIVE);
191 sc->port_used = size;
199 * Allocate a memory resource with the given resource id.
202 cm_alloc_memory(dev, rid, size)
207 struct cm_softc *sc = device_get_softc(dev);
208 struct resource *res;
210 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
211 0ul, ~0ul, size, RF_ACTIVE);
223 * Allocate an irq resource with the given resource id.
226 cm_alloc_irq(dev, rid)
230 struct cm_softc *sc = device_get_softc(dev);
231 struct resource *res;
233 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
244 * Release all resources
247 cm_release_resources(dev)
250 struct cm_softc *sc = device_get_softc(dev);
253 bus_deactivate_resource(dev, SYS_RES_IOPORT,
254 sc->port_rid, sc->port_res);
255 bus_release_resource(dev, SYS_RES_IOPORT,
256 sc->port_rid, sc->port_res);
260 bus_deactivate_resource(dev, SYS_RES_MEMORY,
261 sc->mem_rid, sc->mem_res);
262 bus_release_resource(dev, SYS_RES_MEMORY,
263 sc->mem_rid, sc->mem_res);
267 bus_deactivate_resource(dev, SYS_RES_IRQ,
268 sc->irq_rid, sc->irq_res);
269 bus_release_resource(dev, SYS_RES_IRQ,
270 sc->irq_rid, sc->irq_res);
279 struct cm_softc *sc = device_get_softc(dev);
280 struct ifnet *ifp = &sc->sc_arccom.ac_if;
282 u_int8_t linkaddress;
287 * read the arcnet address from the board
293 } while (!(GETREG(CMSTAT) & CM_POR));
295 linkaddress = GETMEM(CMMACOFF);
297 /* clear the int mask... */
302 PUTREG(CMCMD, CM_CONF(CONF_LONG));
303 PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
304 sc->sc_recontime = sc->sc_reconcount = 0;
306 /* and reenable kernel int level */
310 * set interface to stopped condition (reset)
315 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
316 ifp->if_output = arc_output;
317 ifp->if_start = cm_start;
318 ifp->if_ioctl = cm_ioctl;
319 ifp->if_watchdog = cm_watchdog;
320 ifp->if_init = cm_init;
321 /* XXX IFQ_SET_READY(&ifp->if_snd); */
322 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
324 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NEEDSGIANT;
326 arc_ifattach(ifp, linkaddress);
329 sc->sc_rxcookie = softintr_establish(IPL_SOFTNET, cm_srint, sc);
330 sc->sc_txcookie = softintr_establish(IPL_SOFTNET,
331 (void (*)(void *))cm_start, ifp);
334 #if __FreeBSD_version < 500000
335 callout_init(&sc->sc_recon_ch);
337 callout_init(&sc->sc_recon_ch, 0);
340 if_printf(ifp, "link addr 0x%02x (%d)\n", linkaddress, linkaddress);
352 struct cm_softc *sc = (struct cm_softc *)xsc;
356 ifp = &sc->sc_arccom.ac_if;
358 if ((ifp->if_flags & IFF_RUNNING) == 0) {
360 ifp->if_flags |= IFF_RUNNING;
368 * Reset the interface...
370 * this assumes that it is called inside a critical section...
380 ifp = &sc->sc_arccom.ac_if;
383 if_printf(ifp, "reset\n");
385 /* stop and restart hardware */
390 } while (!(GETREG(CMSTAT) & CM_POR));
392 linkaddress = GETMEM(CMMACOFF);
394 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
395 if_printf(ifp, "reset: card reset, link addr = 0x%02x (%d)\n",
396 linkaddress, linkaddress);
399 /* tell the routing level about the (possibly changed) link address */
400 arc_storelladdr(ifp, linkaddress);
403 /* POR is NMI, but we need it below: */
404 sc->sc_intmask = CM_RECON|CM_POR;
405 PUTREG(CMSTAT, sc->sc_intmask);
406 PUTREG(CMCMD, CM_CONF(CONF_LONG));
409 if_printf(ifp, "reset: chip configured, status=0x%02x\n",
412 PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
415 if_printf(ifp, "reset: bits cleared, status=0x%02x\n",
419 sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
423 sc->sc_intmask |= CM_RI;
424 sc->sc_rx_fillcount = 0;
427 PUTREG(CMCMD, CM_RXBC(2));
428 PUTREG(CMSTAT, sc->sc_intmask);
431 if_printf(ifp, "reset: started receiver, status=0x%02x\n",
435 /* and init transmitter status */
437 sc->sc_tx_fillcount = 0;
439 ifp->if_flags |= IFF_RUNNING;
440 ifp->if_flags &= ~IFF_OACTIVE;
446 * Take interface offline
452 /* Stop the interrupts */
455 /* Stop the interface */
458 /* Stop watchdog timer */
459 sc->sc_arccom.ac_if.if_timer = 0;
463 * Start output on interface. Get another datagram to send
464 * off the interface queue, and copy it to the
465 * interface becore starting the output
467 * this assumes that it is called inside a critical section...
468 * XXX hm... does it still?
475 struct cm_softc *sc = ifp->if_softc;
479 int len, tlen, offset, s, buffer;
481 u_long copystart, lencopy, perbyte;
484 #if defined(CM_DEBUG) && (CM_DEBUG > 3)
485 if_printf(ifp, "start(%p)\n", ifp);
488 if ((ifp->if_flags & IFF_RUNNING) == 0)
493 if (sc->sc_tx_fillcount >= 2) {
498 m = arc_frag_next(ifp);
499 buffer = sc->sc_tx_act ^ 1;
507 if (m->m_len < ARC_HDRLEN)
508 m = m_pullup(m, ARC_HDRLEN);/* gcc does structure padding */
509 if_printf(ifp, "start: filling %d from %d to %d type %d\n",
510 buffer, mtod(m, u_char *)[0],
511 mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
516 cm_ram_ptr = buffer * 512;
521 /* write the addresses to RAM and throw them away */
524 * Hardware does this: Yet Another Microsecond Saved.
525 * (btw, timing code says usually 2 microseconds)
526 * PUTMEM(cm_ram_ptr + 0, mtod(m, u_char *)[0]);
529 PUTMEM(cm_ram_ptr + 1, mtod(m, u_char *)[1]);
532 /* get total length left at this point */
533 tlen = m->m_pkthdr.len;
534 if (tlen < ARC_MIN_FORBID_LEN) {
536 PUTMEM(cm_ram_ptr + 2, offset);
538 PUTMEM(cm_ram_ptr + 2, 0);
539 if (tlen <= ARC_MAX_FORBID_LEN)
540 offset = 255; /* !!! */
542 if (tlen > ARC_MAX_LEN)
546 PUTMEM(cm_ram_ptr + 3, offset);
549 cm_ram_ptr += offset;
551 /* lets loop through the mbuf chain */
553 for (mp = m; mp; mp = mp->m_next) {
554 if ((len = mp->m_len)) { /* YAMS */
555 bus_space_write_region_1(
556 rman_get_bustag(sc->mem_res),
557 rman_get_bushandle(sc->mem_res),
558 cm_ram_ptr, mtod(mp, caddr_t), len);
564 sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
565 sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
567 /* actually transmit the packet */
570 if (++sc->sc_tx_fillcount > 1) {
572 * We are filled up to the rim. No more bufs for the moment,
575 ifp->if_flags |= IFF_OACTIVE;
578 if_printf(ifp, "start: starting transmitter on buffer %d\n",
581 /* Transmitter was off, start it */
582 sc->sc_tx_act = buffer;
585 * We still can accept another buf, so don't:
586 * ifp->if_flags |= IFF_OACTIVE;
588 sc->sc_intmask |= CM_TA;
589 PUTREG(CMCMD, CM_TX(buffer));
590 PUTREG(CMSTAT, sc->sc_intmask);
592 sc->sc_arccom.ac_if.if_timer = ARCTIMEOUT;
598 * After 10 times reading the docs, I realized
599 * that in the case the receiver NAKs the buffer request,
600 * the hardware retries till shutdown.
601 * This is integrated now in the code above.
608 * Arcnet interface receiver soft interrupt:
609 * get the stuff out of any filled buffer we find.
615 struct cm_softc *sc = (struct cm_softc *)vsc;
616 int buffer, len, offset, s, type;
619 struct arc_header *ah;
622 ifp = &sc->sc_arccom.ac_if;
625 buffer = sc->sc_rx_act ^ 1;
628 /* Allocate header mbuf */
629 MGETHDR(m, M_DONTWAIT, MT_DATA);
633 * in case s.th. goes wrong with mem, drop it
634 * to make sure the receiver can be started again
635 * count it as input error (we dont have any other
642 m->m_pkthdr.rcvif = ifp;
645 * Align so that IP packet will be longword aligned. Here we
646 * assume that m_data of new packet is longword aligned.
647 * When implementing PHDS, we might have to change it to 2,
648 * (2*sizeof(ulong) - CM_HDRNEWLEN)), packet type dependent.
651 cm_ram_ptr = buffer * 512;
652 offset = GETMEM(cm_ram_ptr + 2);
656 offset = GETMEM(cm_ram_ptr + 3);
661 * first +2 bytes for align fixup below
662 * second +2 bytes are for src/dst addresses
664 if ((len + 2 + 2) > MHLEN) {
665 /* attach an mbuf cluster */
666 MCLGET(m, M_DONTWAIT);
668 /* Insist on getting a cluster */
669 if ((m->m_flags & M_EXT) == 0) {
680 type = GETMEM(cm_ram_ptr + offset);
681 m->m_data += 1 + arc_isphds(type);
682 /* mbuf filled with ARCnet addresses */
683 m->m_pkthdr.len = m->m_len = len + 2;
685 ah = mtod(m, struct arc_header *);
686 ah->arc_shost = GETMEM(cm_ram_ptr + 0);
687 ah->arc_dhost = GETMEM(cm_ram_ptr + 1);
689 bus_space_read_region_1(
690 rman_get_bustag(sc->mem_res), rman_get_bushandle(sc->mem_res),
691 cm_ram_ptr + offset, mtod(m, u_char *) + 2, len);
703 /* mark buffer as invalid by source id 0 */
704 PUTMEM(buffer << 9, 0);
707 if (--sc->sc_rx_fillcount == 2 - 1) {
709 /* was off, restart it on buffer just emptied */
710 sc->sc_rx_act = buffer;
711 sc->sc_intmask |= CM_RI;
713 /* this also clears the RI flag interupt: */
714 PUTREG(CMCMD, CM_RXBC(buffer));
715 PUTREG(CMSTAT, sc->sc_intmask);
718 if_printf(ifp, "srint: restarted rx on buf %d\n", buffer);
736 ifp = &(sc->sc_arccom.ac_if);
737 buffer = sc->sc_tx_act;
741 * Normal situtations first for fast path:
742 * If acknowledgement received ok or broadcast, we're ok.
746 if (isr & CM_TMA || sc->sc_broadcast[buffer])
747 sc->sc_arccom.ac_if.if_opackets++;
749 else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
750 && --sc->sc_retransmits[buffer] > 0) {
751 /* retransmit same buffer */
752 PUTREG(CMCMD, CM_TX(buffer));
760 /* We know we can accept another buffer at this point. */
761 ifp->if_flags &= ~IFF_OACTIVE;
763 if (--sc->sc_tx_fillcount > 0) {
766 * start tx on other buffer.
767 * This also clears the int flag
770 sc->sc_tx_act = buffer;
774 * sc->sc_intmask |= CM_TA;
775 * PUTREG(CMSTAT, sc->sc_intmask);
777 PUTREG(CMCMD, CM_TX(buffer));
778 /* init watchdog timer */
779 ifp->if_timer = ARCTIMEOUT;
781 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
783 "tint: starting tx on buffer %d, status 0x%02x\n",
784 buffer, GETREG(CMSTAT));
787 /* have to disable TX interrupt */
788 sc->sc_intmask &= ~CM_TA;
789 PUTREG(CMSTAT, sc->sc_intmask);
790 /* ... and watchdog timer */
794 if_printf(ifp, "tint: no more buffers to send, status 0x%02x\n",
801 /* schedule soft int to fill a new buffer for us */
802 softintr_schedule(sc->sc_txcookie);
804 /* call it directly */
810 * Our interrupt routine
816 struct cm_softc *sc = arg;
817 struct ifnet *ifp = &sc->sc_arccom.ac_if;
819 u_char isr, maskedisr;
823 isr = GETREG(CMSTAT);
824 maskedisr = isr & sc->sc_intmask;
829 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
830 if_printf(ifp, "intr: status 0x%02x, intmask 0x%02x\n",
831 isr, sc->sc_intmask);
834 if (maskedisr & CM_POR) {
836 * XXX We should never see this. Don't bother to store
838 * sc->sc_arccom.ac_anaddr = GETMEM(CMMACOFF);
840 PUTREG(CMCMD, CM_CLR(CLR_POR));
842 "%s: intr: got spurious power on reset int\n",
846 if (maskedisr & CM_RECON) {
849 * PUTREG(CMCMD, CM_CONF(CONF_LONG));
851 PUTREG(CMCMD, CM_CLR(CLR_RECONFIG));
852 sc->sc_arccom.ac_if.if_collisions++;
855 * If less than 2 seconds per reconfig:
856 * If ARC_EXCESSIVE_RECONFIGS
857 * since last burst, complain and set treshold for
858 * warnings to ARC_EXCESSIVE_RECONS_REWARN.
860 * This allows for, e.g., new stations on the cable, or
861 * cable switching as long as it is over after
862 * (normally) 16 seconds.
864 * XXX TODO: check timeout bits in status word and
865 * double time if necessary.
868 callout_stop(&sc->sc_recon_ch);
869 newsec = time_second;
870 if ((newsec - sc->sc_recontime <= 2) &&
871 (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
873 "%s: excessive token losses, "
877 sc->sc_recontime = newsec;
878 callout_reset(&sc->sc_recon_ch, 15 * hz,
879 cm_reconwatch, (void *)sc);
882 if (maskedisr & CM_RI) {
883 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
884 if_printf(ifp, "intr: hard rint, act %d\n",
888 buffer = sc->sc_rx_act;
889 /* look if buffer is marked invalid: */
890 if (GETMEM(buffer * 512) == 0) {
892 * invalid marked buffer (or illegally
896 "%s: spurious RX interupt or sender 0 "
897 " (ignored)\n", ifp->if_xname);
899 * restart receiver on same buffer.
900 * XXX maybe better reset interface?
902 PUTREG(CMCMD, CM_RXBC(buffer));
904 if (++sc->sc_rx_fillcount > 1) {
905 sc->sc_intmask &= ~CM_RI;
906 PUTREG(CMSTAT, sc->sc_intmask);
909 sc->sc_rx_act = buffer;
912 * Start receiver on other receive
913 * buffer. This also clears the RI
916 PUTREG(CMCMD, CM_RXBC(buffer));
917 /* in RX intr, so mask is ok for RX */
920 if_printf(ifp, "strt rx for buf %d, "
922 sc->sc_rx_act, GETREG(CMSTAT));
928 * this one starts a soft int to copy out
931 softintr_schedule(sc->sc_rxcookie);
933 /* this one does the copy here */
938 if (maskedisr & CM_TA) {
941 isr = GETREG(CMSTAT);
942 maskedisr = isr & sc->sc_intmask;
944 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
945 if_printf(ifp, "intr (exit): status 0x%02x, intmask 0x%02x\n",
946 isr, sc->sc_intmask);
954 struct cm_softc *sc = arg;
955 struct ifnet *ifp = &sc->sc_arccom.ac_if;
957 if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
958 sc->sc_reconcount = 0;
959 log(LOG_WARNING, "%s: token valid again.\n",
962 sc->sc_reconcount = 0;
967 * Process an ioctl request.
968 * This code needs some work - it looks pretty ugly.
971 cm_ioctl(ifp, command, data)
983 ifa = (struct ifaddr *)data;
984 ifr = (struct ifreq *)data;
987 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
988 if_printf(ifp, "ioctl() called, cmd = 0x%lx\n", command);
997 error = arc_ioctl(ifp, command, data);
1001 if ((ifp->if_flags & IFF_UP) == 0 &&
1002 (ifp->if_flags & IFF_RUNNING) != 0) {
1004 * If interface is marked down and it is running,
1008 ifp->if_flags &= ~IFF_RUNNING;
1009 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1010 (ifp->if_flags & IFF_RUNNING) == 0) {
1012 * If interface is marked up and it is stopped, then
1029 * watchdog routine for transmitter.
1031 * We need this, because else a receiver whose hardware is alive, but whose
1032 * software has not enabled the Receiver, would make our hardware wait forever
1033 * Discovered this after 20 times reading the docs.
1035 * Only thing we do is disable transmitter. We'll get a transmit timeout,
1036 * and the int handler will have to decide not to retransmit (in case
1037 * retransmission is implemented).
1039 * This one assumes being called inside splimp()
1046 struct cm_softc *sc = ifp->if_softc;
1048 PUTREG(CMCMD, CM_TXDIS);