Home | History | Annotate | Line # | Download | only in obio
if_sn_obio.c revision 1.14
      1 /*	$NetBSD: if_sn_obio.c,v 1.14 1998/01/13 19:24:55 scottr Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1997 Allen Briggs
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Allen Briggs
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/param.h>
     34 #include <sys/device.h>
     35 #include <sys/errno.h>
     36 #include <sys/ioctl.h>
     37 #include <sys/socket.h>
     38 #include <sys/syslog.h>
     39 #include <sys/systm.h>
     40 
     41 #include <net/if.h>
     42 #include <net/if_ether.h>
     43 
     44 #if 0 /* XXX this shouldn't be necessary... else reinsert */
     45 #ifdef INET
     46 #include <netinet/in.h>
     47 #include <netinet/if_ether.h>
     48 #endif
     49 #endif
     50 
     51 #include <machine/bus.h>
     52 #include <machine/cpu.h>
     53 #include <machine/viareg.h>
     54 
     55 #include <mac68k/dev/obiovar.h>
     56 #include <mac68k/dev/if_snreg.h>
     57 #include <mac68k/dev/if_snvar.h>
     58 
     59 #define SONIC_REG_BASE	0x50F0A000
     60 #define SONIC_PROM_BASE	0x50F08000
     61 
     62 static int	sn_obio_match __P((struct device *, struct cfdata *, void *));
     63 static void	sn_obio_attach __P((struct device *, struct device *, void *));
     64 static int	sn_obio_getaddr __P((struct sn_softc *, u_int8_t *));
     65 static int	sn_obio_getaddr_kludge __P((struct sn_softc *, u_int8_t *));
     66 
     67 struct cfattach sn_obio_ca = {
     68 	sizeof(struct sn_softc), sn_obio_match, sn_obio_attach
     69 };
     70 
     71 static int
     72 sn_obio_match(parent, cf, aux)
     73 	struct device *parent;
     74 	struct cfdata *cf;
     75 	void *aux;
     76 {
     77 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
     78 	bus_space_handle_t	bsh;
     79 	int			found = 0;
     80 
     81 	if (!mac68k_machine.sonic)
     82 		return 0;
     83 
     84 	if (bus_space_map(oa->oa_tag,
     85 	    		  SONIC_REG_BASE, SN_REGSIZE, 0, &bsh))
     86 		return 0;
     87 
     88 	if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0, 4))
     89 		found = 1;
     90 
     91 	bus_space_unmap(oa->oa_tag, bsh, SN_REGSIZE);
     92 
     93 	return found;
     94 }
     95 
     96 /*
     97  * Install interface into kernel networking data structures
     98  */
     99 static void
    100 sn_obio_attach(parent, self, aux)
    101 	struct device *parent, *self;
    102 	void   *aux;
    103 {
    104 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    105 	struct sn_softc	*sc = (void *)self;
    106 	u_int8_t myaddr[ETHER_ADDR_LEN];
    107 	int i;
    108 
    109 	sc->snr_dcr = DCR_WAIT0 | DCR_DMABLOCK | DCR_RFT16 | DCR_TFT16;
    110 	sc->snr_dcr2 = 0;
    111 
    112 	sc->slotno = 9;
    113 
    114 	switch (current_mac_model->machineid) {
    115 	case MACH_MACC610:
    116 	case MACH_MACC650:
    117 	case MACH_MACQ610:
    118 	case MACH_MACQ650:
    119 	case MACH_MACQ700:
    120 	case MACH_MACQ800:
    121 	case MACH_MACQ900:
    122 	case MACH_MACQ950:
    123 		sc->snr_dcr |= DCR_EXBUS;
    124 		sc->bitmode = 1;
    125 		break;
    126 
    127 	case MACH_MACLC575:
    128 	case MACH_MACQ630:
    129 		break;
    130 
    131 	case MACH_MACPB500:
    132 		sc->snr_dcr |= DCR_SYNC | DCR_LBR;
    133 		sc->bitmode = 0;	/* 16 bit interface */
    134 		break;
    135 
    136 	default:
    137 		printf(": unsupported machine type\n");
    138 		return;
    139 	}
    140 
    141 	sc->sc_regt = oa->oa_tag;
    142 
    143 	if (bus_space_map(sc->sc_regt,
    144 	    SONIC_REG_BASE, SN_REGSIZE, 0, &sc->sc_regh)) {
    145 		printf(": failed to map space for SONIC regs.\n");
    146 		return;
    147 	}
    148 
    149 	/* regs are addressed as words, big-endian. */
    150 	for (i = 0; i < SN_NREGS; i++) {
    151 		sc->sc_reg_map[i] = (bus_size_t)((i * 4) + 2);
    152 	}
    153 
    154 	/*
    155 	 * Kind of kludge this.  Comm-slot cards do not really
    156 	 * have a visible type, as far as I can tell at this time,
    157 	 * so assume that MacOS had it properly configured and use
    158 	 * that configuration.
    159 	 */
    160 	switch (current_mac_model->machineid) {
    161 	case MACH_MACLC575:
    162 	case MACH_MACQ630:
    163 		NIC_PUT(sc, SNR_CR, CR_RST);	wbflush();
    164 		i = NIC_GET(sc, SNR_DCR);
    165 		sc->snr_dcr |= (i & 0xfff0);
    166 		sc->bitmode = (i & DCR_DW) ? 1 : 0;
    167 		break;
    168 	default:
    169 		break;
    170 	}
    171 
    172 	if (sn_obio_getaddr(sc, myaddr) &&
    173 	    sn_obio_getaddr_kludge(sc, myaddr)) { /* XXX kludge for PB */
    174 		printf(": failed to get MAC address.\n");
    175 		bus_space_unmap(sc->sc_regt, sc->sc_regh, SN_REGSIZE);
    176 		return;
    177 	}
    178 
    179 	/* snsetup returns 1 if something fails */
    180 	if (snsetup(sc, myaddr)) {
    181 		bus_space_unmap(sc->sc_regt, sc->sc_regh, SN_REGSIZE);
    182 		return;
    183 	}
    184 
    185 	add_nubus_intr(sc->slotno, snintr, (void *)sc);
    186 }
    187 
    188 static int
    189 sn_obio_getaddr(sc, lladdr)
    190 	struct sn_softc	*sc;
    191 	u_int8_t *lladdr;
    192 {
    193 	bus_space_handle_t bsh;
    194 
    195 	if (bus_space_map(sc->sc_regt, SONIC_PROM_BASE, NBPG, 0, &bsh)) {
    196 		printf(": failed to map space to read SONIC address.\n%s",
    197 		    sc->sc_dev.dv_xname);
    198 		return (-1);
    199 	}
    200 
    201 	if (!mac68k_bus_space_probe(sc->sc_regt, bsh, 0, 1)) {
    202 		bus_space_unmap(sc->sc_regt, bsh, NBPG);
    203 		return (-1);
    204 	}
    205 
    206 	sn_get_enaddr(sc->sc_regt, bsh, 0, lladdr);
    207 
    208 	bus_space_unmap(sc->sc_regt, bsh, NBPG);
    209 
    210 	return 0;
    211 }
    212 
    213 /*
    214  * Assume that the SONIC was initialized in MacOS.  This should go away
    215  * when we can properly get the MAC address on the PBs.
    216  */
    217 static int
    218 sn_obio_getaddr_kludge(sc, lladdr)
    219 	struct sn_softc	*sc;
    220 	u_int8_t *lladdr;
    221 {
    222 	int i, ors = 0;
    223 
    224 	/* Shut down NIC */
    225 	NIC_PUT(sc, SNR_CR, CR_RST);
    226 	wbflush();
    227 
    228 	NIC_PUT(sc, SNR_CEP, 15); /* For some reason, Apple fills top first. */
    229 	wbflush();
    230 	i = NIC_GET(sc, SNR_CAP2);
    231 	wbflush();
    232 
    233 	ors |= i;
    234 	lladdr[5] = i >> 8;
    235 	lladdr[4] = i;
    236 
    237 	i = NIC_GET(sc, SNR_CAP1);
    238 	wbflush();
    239 
    240 	ors |= i;
    241 	lladdr[3] = i >> 8;
    242 	lladdr[2] = i;
    243 
    244 	i = NIC_GET(sc, SNR_CAP0);
    245 	wbflush();
    246 
    247 	ors |= i;
    248 	lladdr[1] = i >> 8;
    249 	lladdr[0] = i;
    250 
    251 	NIC_PUT(sc, SNR_CR, 0);
    252 	wbflush();
    253 
    254 	if (ors == 0)
    255 		return -1;
    256 
    257 	return 0;
    258 }
    259