Home | History | Annotate | Line # | Download | only in apbus
      1  1.16  thorpej /*	$NetBSD: if_tlp_ap.c,v 1.16 2023/12/20 04:30:27 thorpej Exp $	*/
      2   1.1     onoe 
      3   1.1     onoe /*
      4   1.1     onoe  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5   1.1     onoe  * All rights reserved.
      6   1.1     onoe  *
      7   1.1     onoe  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1     onoe  * by Atsushi Onoe.
      9   1.1     onoe  *
     10   1.1     onoe  * Redistribution and use in source and binary forms, with or without
     11   1.1     onoe  * modification, are permitted provided that the following conditions
     12   1.1     onoe  * are met:
     13   1.1     onoe  * 1. Redistributions of source code must retain the above copyright
     14   1.1     onoe  *    notice, this list of conditions and the following disclaimer.
     15   1.1     onoe  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     onoe  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     onoe  *    documentation and/or other materials provided with the distribution.
     18   1.1     onoe  *
     19   1.1     onoe  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1     onoe  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1     onoe  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1     onoe  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1     onoe  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1     onoe  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1     onoe  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1     onoe  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1     onoe  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1     onoe  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1     onoe  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1     onoe  */
     31   1.6    lukem 
     32   1.6    lukem #include <sys/cdefs.h>
     33  1.16  thorpej __KERNEL_RCSID(0, "$NetBSD: if_tlp_ap.c,v 1.16 2023/12/20 04:30:27 thorpej Exp $");
     34   1.1     onoe 
     35   1.1     onoe #include <sys/param.h>
     36   1.1     onoe #include <sys/device.h>
     37   1.1     onoe #include <sys/errno.h>
     38   1.1     onoe #include <sys/ioctl.h>
     39   1.1     onoe #include <sys/socket.h>
     40   1.1     onoe #include <sys/syslog.h>
     41   1.1     onoe #include <sys/systm.h>
     42   1.1     onoe 
     43   1.1     onoe #include <net/if.h>
     44   1.1     onoe #include <net/if_dl.h>
     45   1.1     onoe #include <net/if_media.h>
     46   1.1     onoe #include <net/if_ether.h>
     47   1.1     onoe 
     48   1.1     onoe #include <machine/bus.h>
     49   1.1     onoe #include <machine/intr.h>
     50   1.2  thorpej 
     51   1.2  thorpej #include <mips/cache.h>
     52   1.1     onoe 
     53   1.1     onoe #include <dev/mii/miivar.h>
     54   1.1     onoe #include <dev/mii/mii_bitbang.h>
     55   1.1     onoe 
     56   1.1     onoe #include <dev/ic/tulipreg.h>
     57   1.1     onoe #include <dev/ic/tulipvar.h>
     58   1.1     onoe 
     59   1.1     onoe #include <newsmips/apbus/apbusvar.h>
     60   1.1     onoe 
     61   1.1     onoe #define	TLP_AP_ROM	0x00000000	/* AProm entry address */
     62   1.1     onoe #define	TLP_AP_CFG	0x00040000	/* PCI configuration registers */
     63   1.1     onoe #define		TLP_AP_CFG_CFID	0x00		/* Identification */
     64   1.1     onoe #define		TLP_AP_CFG_CFCS	0x04		/* Command and status */
     65   1.1     onoe #define		TLP_AP_CFG_CFRV	0x08		/* Revision */
     66   1.1     onoe #define		TLP_AP_CFG_CFLT	0x0c		/* Latency timer */
     67   1.1     onoe #define		TLP_AP_CFG_CBIO	0x10		/* Base I/O address */
     68   1.1     onoe #define		TLP_AP_CFG_CBMA	0x14		/* Base memory address */
     69   1.1     onoe #define		TLP_AP_CFG_CFIT	0x3c		/* Interrupt */
     70   1.1     onoe #define	TLP_AP_CSR	0x00080000	/* CSR base address */
     71   1.1     onoe #define	TLP_AP_RST	0x00100000	/* Board Reset */
     72   1.1     onoe 
     73   1.1     onoe 
     74   1.1     onoe struct tulip_ap_softc {
     75   1.1     onoe 	struct tulip_softc sc_tulip;	/* real Tulip softc */
     76   1.1     onoe 	bus_space_tag_t sc_cfst;
     77   1.1     onoe 	bus_space_handle_t sc_cfsh;
     78   1.1     onoe };
     79   1.1     onoe 
     80  1.11  tsutsui static int	tlp_ap_match(device_t, cfdata_t, void *);
     81  1.11  tsutsui static void	tlp_ap_attach(device_t, device_t, void *);
     82   1.1     onoe 
     83  1.11  tsutsui CFATTACH_DECL_NEW(tlp_ap, sizeof(struct tulip_ap_softc),
     84   1.4  thorpej     tlp_ap_match, tlp_ap_attach, NULL, NULL);
     85   1.1     onoe 
     86   1.7  tsutsui static void tlp_ap_preinit(struct tulip_softc *);
     87   1.7  tsutsui static void tlp_ap_tmsw_init(struct tulip_softc *);
     88   1.7  tsutsui static void tlp_ap_getmedia(struct tulip_softc *, struct ifmediareq *);
     89   1.7  tsutsui static int tlp_ap_setmedia(struct tulip_softc *);
     90   1.1     onoe 
     91   1.1     onoe const struct tulip_mediasw tlp_ap_mediasw = {
     92   1.1     onoe 	tlp_ap_tmsw_init, tlp_ap_getmedia, tlp_ap_setmedia
     93   1.1     onoe };
     94   1.1     onoe 
     95   1.1     onoe static int
     96  1.11  tsutsui tlp_ap_match(device_t parent, cfdata_t cf, void *aux)
     97   1.1     onoe {
     98   1.1     onoe 	struct apbus_attach_args *apa = aux;
     99   1.1     onoe 
    100   1.1     onoe 	if (strcmp(apa->apa_name, "cbasetx") != 0)
    101   1.1     onoe 		return 0;
    102   1.1     onoe 
    103   1.1     onoe 	return 1;
    104   1.1     onoe }
    105   1.1     onoe 
    106   1.1     onoe /*
    107   1.1     onoe  * Install interface into kernel networking data structures
    108   1.1     onoe  */
    109   1.1     onoe static void
    110  1.11  tsutsui tlp_ap_attach(device_t parent, device_t self, void *aux)
    111   1.1     onoe {
    112  1.11  tsutsui 	struct tulip_ap_softc *psc = device_private(self);
    113   1.1     onoe 	struct tulip_softc *sc = &psc->sc_tulip;
    114   1.1     onoe 	struct apbus_attach_args *apa = aux;
    115   1.7  tsutsui 	uint8_t enaddr[ETHER_ADDR_LEN];
    116   1.1     onoe 	u_int intrmask;
    117   1.1     onoe 	int i;
    118   1.1     onoe 
    119  1.11  tsutsui 	sc->sc_dev = self;
    120  1.11  tsutsui 
    121  1.11  tsutsui 	aprint_normal(" slot%d addr 0x%lx", apa->apa_slotno, apa->apa_hwbase);
    122   1.1     onoe 
    123   1.1     onoe 	/* PCI configuration register */
    124   1.1     onoe 	psc->sc_cfst = 0;
    125   1.1     onoe 	psc->sc_cfsh = apa->apa_hwbase + TLP_AP_CFG;
    126   1.1     onoe 	sc->sc_devno = apa->apa_slotno;
    127   1.1     onoe 	sc->sc_rev = bus_space_read_4(psc->sc_cfst, psc->sc_cfsh,
    128   1.1     onoe 	    TLP_AP_CFG_CFRV);
    129   1.1     onoe 	switch (bus_space_read_4(psc->sc_cfst, psc->sc_cfsh, TLP_AP_CFG_CFID)) {
    130   1.1     onoe 	case 0x00091011:
    131   1.1     onoe 		if (sc->sc_rev >= 0x20)
    132   1.1     onoe 			sc->sc_chip = TULIP_CHIP_21140A;
    133   1.1     onoe 		else
    134   1.1     onoe 			sc->sc_chip = TULIP_CHIP_21140;
    135   1.1     onoe 		break;
    136   1.1     onoe 	default:
    137  1.11  tsutsui 		aprint_error(": unable to handle your board\n");
    138   1.1     onoe 		return;
    139   1.1     onoe 	}
    140   1.1     onoe 
    141  1.11  tsutsui 	aprint_normal(": %s Ethernet, pass %d.%d\n",
    142  1.13       he 	    tlp_chip_name(sc->sc_chip),
    143   1.1     onoe 	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
    144   1.1     onoe 
    145   1.1     onoe 	/* CSR */
    146   1.1     onoe 	sc->sc_st = 0;
    147   1.1     onoe 	sc->sc_sh = apa->apa_hwbase + TLP_AP_CSR;
    148   1.1     onoe 	sc->sc_dmat = apbus_dmatag_init(apa);
    149   1.1     onoe 	if (sc->sc_dmat == NULL) {
    150  1.11  tsutsui 		aprint_error_dev(self, "cannot allocate memory\n");
    151   1.1     onoe 		return;
    152   1.1     onoe 	}
    153   1.1     onoe 
    154   1.1     onoe 	/*
    155   1.1     onoe 	 * Initialize bus specific parameters.
    156   1.1     onoe 	 */
    157  1.12     matt 	if (mips_cache_info.mci_sdcache_line_size > 0)
    158  1.12     matt 		sc->sc_cacheline = mips_cache_info.mci_sdcache_line_size / 4;
    159  1.12     matt 	else if (mips_cache_info.mci_pdcache_line_size > 0)
    160  1.12     matt 		sc->sc_cacheline = mips_cache_info.mci_pdcache_line_size / 4;
    161   1.1     onoe 	else
    162   1.1     onoe 		sc->sc_cacheline = 4;
    163   1.1     onoe 	sc->sc_maxburst = sc->sc_cacheline;		/* XXX */
    164   1.1     onoe 	sc->sc_regshift = 3;
    165   1.1     onoe 	sc->sc_flags |= TULIPF_DBO | TULIPF_BLE;	/* Big Endian BUS */
    166   1.1     onoe 	sc->sc_flags |= TULIPF_ENABLED;			/* No Power Mgmt */
    167   1.1     onoe 
    168   1.1     onoe 	/*
    169   1.1     onoe 	 * Reset hardware.
    170   1.1     onoe 	 */
    171   1.1     onoe 	bus_space_write_4(0, apa->apa_hwbase + TLP_AP_RST, 0, 1);
    172   1.1     onoe 	delay(100);
    173   1.1     onoe 
    174   1.1     onoe 	/*
    175   1.1     onoe 	 * Initialize PCI configuration register
    176   1.1     onoe 	 */
    177   1.1     onoe 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
    178   1.1     onoe 	    TLP_AP_CFG_CFCS, 0x00000005);	/* Master, IO */
    179   1.1     onoe 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
    180   1.1     onoe 	    TLP_AP_CFG_CFLT, 0x00000100);
    181   1.1     onoe 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
    182   1.1     onoe 	    TLP_AP_CFG_CBIO, MIPS_KSEG1_TO_PHYS(sc->sc_sh));
    183   1.1     onoe 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
    184   1.1     onoe 	    TLP_AP_CFG_CFIT, 0x00000101);
    185   1.1     onoe 
    186   1.1     onoe 	/*
    187   1.1     onoe 	 * Initialize general purpose port register.
    188   1.1     onoe 	 */
    189   1.1     onoe 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | 0xc0);	/* read */
    190   1.1     onoe 	TULIP_WRITE(sc, CSR_GPP, 0x40);			/* dipsw port on */
    191   1.1     onoe 	i = TULIP_READ(sc, CSR_GPP) & GPP_MD;		/* dipsw contents */
    192   1.1     onoe 	TULIP_WRITE(sc, CSR_GPP, 0xc0);			/* dipsw port off */
    193   1.1     onoe 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | 0xcf);	/* read write */
    194   1.1     onoe 	if (sc->sc_maxburst == 16) {
    195   1.1     onoe 		TULIP_WRITE(sc, CSR_GPP, 0x8f);		/* 16word burst */
    196   1.1     onoe 		TULIP_WRITE(sc, CSR_GPP, 0xcf);
    197   1.1     onoe 	} else {
    198   1.1     onoe 		TULIP_WRITE(sc, CSR_GPP, 0x8e);		/* 8word burst */
    199   1.1     onoe 		TULIP_WRITE(sc, CSR_GPP, 0xce);
    200   1.1     onoe 	}
    201   1.1     onoe 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | 0xcf);	/* read write */
    202   1.5      wiz 	TULIP_WRITE(sc, CSR_GPP, 0xc3);			/* mask abort/DMA err */
    203   1.5      wiz 	TULIP_WRITE(sc, CSR_GPP, 0xcf);			/* mask abort/DMA err */
    204   1.1     onoe 
    205   1.1     onoe 	if (tlp_read_srom(sc) == 0) {
    206  1.11  tsutsui 		aprint_error_dev(self, "srom read failed\n");
    207   1.1     onoe 		free(sc->sc_dmat, M_DEVBUF);
    208   1.1     onoe 		return;
    209   1.1     onoe 	}
    210   1.1     onoe 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    211   1.1     onoe 		enaddr[i] = sc->sc_srom[0x11 + i * 2];
    212   1.1     onoe 
    213   1.1     onoe 	sc->sc_mediasw = &tlp_ap_mediasw;
    214   1.1     onoe 
    215   1.1     onoe 	/*
    216   1.1     onoe 	 * Finish off the attach.
    217   1.1     onoe 	 */
    218   1.1     onoe 	tlp_attach(sc, enaddr);
    219   1.1     onoe 
    220   1.1     onoe 	intrmask = SLOTTOMASK(apa->apa_slotno);
    221   1.1     onoe 	apbus_intr_establish(0, /* interrupt level (0 or 1) */
    222   1.7  tsutsui 	    intrmask,
    223   1.7  tsutsui 	    0, /* priority */
    224  1.14  tsutsui 	    tlp_intr, sc, device_xname(self), apa->apa_ctlnum);
    225   1.1     onoe }
    226   1.1     onoe 
    227   1.1     onoe static void
    228   1.7  tsutsui tlp_ap_preinit(struct tulip_softc *sc)
    229   1.1     onoe {
    230   1.1     onoe 
    231   1.1     onoe 	sc->sc_opmode |= OPMODE_MBO | OPMODE_SCR | OPMODE_PCS | OPMODE_HBD |
    232   1.1     onoe 	    OPMODE_PS;
    233   1.1     onoe 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
    234   1.1     onoe }
    235   1.1     onoe 
    236   1.1     onoe static void
    237   1.7  tsutsui tlp_ap_tmsw_init(struct tulip_softc *sc)
    238   1.1     onoe {
    239   1.1     onoe 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    240  1.15  msaitoh 	struct mii_data *mii = &sc->sc_mii;
    241   1.1     onoe 
    242   1.1     onoe 	sc->sc_preinit = tlp_ap_preinit;
    243   1.1     onoe 
    244  1.15  msaitoh 	mii->mii_ifp = ifp;
    245  1.15  msaitoh 	mii->mii_readreg = NULL;
    246  1.15  msaitoh 	mii->mii_writereg = NULL;
    247  1.15  msaitoh 	mii->mii_statchg = sc->sc_statchg;
    248  1.15  msaitoh 	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
    249  1.15  msaitoh 	ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_100_TX, 0, NULL);
    250  1.15  msaitoh 	ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_100_TX | IFM_FDX, 0,
    251   1.1     onoe 	    NULL);
    252  1.15  msaitoh 	ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_100_TX);
    253   1.1     onoe }
    254   1.1     onoe 
    255   1.1     onoe static void
    256   1.7  tsutsui tlp_ap_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr)
    257   1.1     onoe {
    258   1.1     onoe 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    259   1.1     onoe 
    260   1.1     onoe 	ifmr->ifm_status = IFM_AVALID;
    261   1.1     onoe 	if (ifp->if_flags & IFF_RUNNING)
    262   1.1     onoe 		ifmr->ifm_status |= IFM_ACTIVE;
    263   1.1     onoe 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
    264   1.1     onoe }
    265   1.1     onoe 
    266   1.1     onoe static int
    267   1.7  tsutsui tlp_ap_setmedia(struct tulip_softc *sc)
    268   1.1     onoe {
    269   1.1     onoe 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    270   1.1     onoe 
    271   1.1     onoe 	sc->sc_mii.mii_media_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
    272   1.1     onoe 
    273   1.1     onoe 	if (ifp->if_flags & IFF_UP)
    274  1.15  msaitoh 		tlp_idle(sc, OPMODE_ST | OPMODE_SR);
    275   1.1     onoe 	if (sc->sc_mii.mii_media_active & IFM_FDX)
    276   1.1     onoe 		sc->sc_opmode |= OPMODE_FD;
    277   1.1     onoe 	else
    278   1.1     onoe 		sc->sc_opmode &= ~OPMODE_FD;
    279   1.1     onoe 	if (ifp->if_flags & IFF_UP)
    280   1.1     onoe 		TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
    281   1.7  tsutsui 	return 0;
    282   1.1     onoe }
    283