Home | History | Annotate | Line # | Download | only in dev
emac3.c revision 1.1.22.1
      1  1.1.22.1  skrll /*	$NetBSD: emac3.c,v 1.1.22.1 2004/08/03 10:39:06 skrll Exp $	*/
      2       1.1    uch 
      3       1.1    uch /*-
      4       1.1    uch  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5       1.1    uch  * All rights reserved.
      6       1.1    uch  *
      7       1.1    uch  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1    uch  * by UCHIYAMA Yasushi.
      9       1.1    uch  *
     10       1.1    uch  * Redistribution and use in source and binary forms, with or without
     11       1.1    uch  * modification, are permitted provided that the following conditions
     12       1.1    uch  * are met:
     13       1.1    uch  * 1. Redistributions of source code must retain the above copyright
     14       1.1    uch  *    notice, this list of conditions and the following disclaimer.
     15       1.1    uch  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1    uch  *    notice, this list of conditions and the following disclaimer in the
     17       1.1    uch  *    documentation and/or other materials provided with the distribution.
     18       1.1    uch  * 3. All advertising materials mentioning features or use of this software
     19       1.1    uch  *    must display the following acknowledgement:
     20       1.1    uch  *        This product includes software developed by the NetBSD
     21       1.1    uch  *        Foundation, Inc. and its contributors.
     22       1.1    uch  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1    uch  *    contributors may be used to endorse or promote products derived
     24       1.1    uch  *    from this software without specific prior written permission.
     25       1.1    uch  *
     26       1.1    uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1    uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1    uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1    uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1    uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1    uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1    uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1    uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1    uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1    uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1    uch  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1    uch  */
     38       1.1    uch 
     39       1.1    uch /*
     40       1.1    uch  * EMAC3 (Ethernet Media Access Controller)
     41       1.1    uch  */
     42  1.1.22.1  skrll 
     43  1.1.22.1  skrll #include <sys/cdefs.h>
     44  1.1.22.1  skrll __KERNEL_RCSID(0, "$NetBSD: emac3.c,v 1.1.22.1 2004/08/03 10:39:06 skrll Exp $");
     45  1.1.22.1  skrll 
     46       1.1    uch #include "debug_playstation2.h"
     47       1.1    uch 
     48       1.1    uch #include <sys/param.h>
     49       1.1    uch #include <sys/systm.h>
     50       1.1    uch 
     51       1.1    uch #include <sys/socket.h>
     52       1.1    uch 
     53       1.1    uch #include <net/if.h>
     54       1.1    uch #include <net/if_ether.h>
     55       1.1    uch #include <net/if_media.h>
     56       1.1    uch 
     57       1.1    uch #include <dev/mii/mii.h>
     58       1.1    uch #include <dev/mii/miivar.h>
     59       1.1    uch 
     60       1.1    uch #include <playstation2/ee/eevar.h>
     61       1.1    uch #include <playstation2/dev/emac3reg.h>
     62       1.1    uch #include <playstation2/dev/emac3var.h>
     63       1.1    uch 
     64       1.1    uch #ifdef EMAC3_DEBUG
     65       1.1    uch #define STATIC
     66       1.1    uch int	emac3_debug = 0;
     67       1.1    uch #define	DPRINTF(fmt, args...)						\
     68       1.1    uch 	if (emac3_debug)						\
     69       1.1    uch 		printf("%s: " fmt, __FUNCTION__ , ##args)
     70       1.1    uch #define	DPRINTFN(n, arg)						\
     71       1.1    uch 	if (emac3_debug > (n))						\
     72       1.1    uch n		printf("%s: " fmt, __FUNCTION__ , ##args)
     73       1.1    uch #else
     74       1.1    uch #define STATIC			static
     75       1.1    uch #define	DPRINTF(arg...)		((void)0)
     76       1.1    uch #define DPRINTFN(n, arg...)	((void)0)
     77       1.1    uch #endif
     78       1.1    uch 
     79       1.1    uch /* SMAP specific EMAC3 define */
     80       1.1    uch #define EMAC3_BASE			MIPS_PHYS_TO_KSEG1(0x14002000)
     81       1.1    uch static __inline__ u_int32_t
     82       1.1    uch _emac3_reg_read_4(int ofs)
     83       1.1    uch {
     84       1.1    uch 	bus_addr_t a_ = EMAC3_BASE + ofs;
     85       1.1    uch 
     86       1.1    uch 	return (_reg_read_2(a_) << 16) | _reg_read_2(a_ + 2);
     87       1.1    uch }
     88       1.1    uch 
     89       1.1    uch static __inline__ void
     90       1.1    uch _emac3_reg_write_4(int ofs, u_int32_t v)
     91       1.1    uch {
     92       1.1    uch 	bus_addr_t a_ = EMAC3_BASE + ofs;
     93       1.1    uch 
     94       1.1    uch 	_reg_write_2(a_, (v >> 16) & 0xffff);
     95       1.1    uch 	_reg_write_2(a_ + 2, v & 0xffff);
     96       1.1    uch }
     97       1.1    uch 
     98       1.1    uch STATIC int emac3_phy_ready(void);
     99       1.1    uch STATIC int emac3_soft_reset(void);
    100       1.1    uch STATIC void emac3_config(const u_int8_t *);
    101       1.1    uch 
    102       1.1    uch int
    103       1.1    uch emac3_init(struct emac3_softc *sc)
    104       1.1    uch {
    105       1.1    uch 	u_int32_t r;
    106       1.1    uch 
    107       1.1    uch 	/* save current mode before reset */
    108       1.1    uch 	r = _emac3_reg_read_4(EMAC3_MR1);
    109       1.1    uch 
    110       1.1    uch 	if (emac3_soft_reset() != 0) {
    111       1.1    uch 		printf("%s: reset failed.\n", sc->dev.dv_xname);
    112       1.1    uch 		return (1);
    113       1.1    uch 	}
    114       1.1    uch 
    115       1.1    uch 	/* set operation mode */
    116       1.1    uch 	r |= MR1_RFS_2KB | MR1_TFS_1KB | MR1_TR0_SINGLE | MR1_TR1_SINGLE;
    117       1.1    uch 	_emac3_reg_write_4(EMAC3_MR1, r);
    118       1.1    uch 
    119       1.1    uch 	sc->mode1_reg = _emac3_reg_read_4(EMAC3_MR1);
    120       1.1    uch 
    121       1.1    uch 	emac3_intr_clear();
    122       1.1    uch 	emac3_intr_disable();
    123       1.1    uch 
    124       1.1    uch 	emac3_config(sc->eaddr);
    125       1.1    uch 
    126       1.1    uch 	return (0);
    127       1.1    uch }
    128       1.1    uch 
    129       1.1    uch void
    130       1.1    uch emac3_exit(struct emac3_softc *sc)
    131       1.1    uch {
    132       1.1    uch 	int retry = 10000;
    133       1.1    uch 
    134       1.1    uch 	/* wait for kicked transmission */
    135       1.1    uch 	while (((_emac3_reg_read_4(EMAC3_TMR0) & TMR0_GNP0) != 0) &&
    136       1.1    uch 	    --retry > 0)
    137       1.1    uch 		;
    138       1.1    uch 
    139       1.1    uch 	if (retry == 0)
    140       1.1    uch 		printf("%s: still running.\n", sc->dev.dv_xname);
    141       1.1    uch }
    142       1.1    uch 
    143       1.1    uch int
    144       1.1    uch emac3_reset(struct emac3_softc *sc)
    145       1.1    uch {
    146       1.1    uch 
    147       1.1    uch 	if (emac3_soft_reset() != 0) {
    148       1.1    uch 		printf("%s: reset failed.\n", sc->dev.dv_xname);
    149       1.1    uch 		return (1);
    150       1.1    uch 	}
    151       1.1    uch 
    152       1.1    uch 	/* restore previous mode */
    153       1.1    uch 	_emac3_reg_write_4(EMAC3_MR1, sc->mode1_reg);
    154       1.1    uch 
    155       1.1    uch 	emac3_config(sc->eaddr);
    156       1.1    uch 
    157       1.1    uch 	return (0);
    158       1.1    uch }
    159       1.1    uch 
    160       1.1    uch void
    161       1.1    uch emac3_enable()
    162       1.1    uch {
    163       1.1    uch 
    164       1.1    uch 	_emac3_reg_write_4(EMAC3_MR0, MR0_TXE | MR0_RXE);
    165       1.1    uch }
    166       1.1    uch 
    167       1.1    uch void
    168       1.1    uch emac3_disable()
    169       1.1    uch {
    170       1.1    uch 	int retry = 10000;
    171       1.1    uch 
    172       1.1    uch 	_emac3_reg_write_4(EMAC3_MR0,
    173       1.1    uch 	    _emac3_reg_read_4(EMAC3_MR0) & ~(MR0_TXE | MR0_RXE));
    174       1.1    uch 
    175       1.1    uch 	/* wait for idling state */
    176       1.1    uch 	while (((_emac3_reg_read_4(EMAC3_MR0) & (MR0_RXI | MR0_TXI)) !=
    177       1.1    uch 	    (MR0_RXI | MR0_TXI)) && --retry > 0)
    178       1.1    uch 		;
    179       1.1    uch 
    180       1.1    uch 	if (retry == 0)
    181       1.1    uch 		printf("emac3 running.\n");
    182       1.1    uch }
    183       1.1    uch 
    184       1.1    uch void
    185       1.1    uch emac3_intr_enable()
    186       1.1    uch {
    187       1.1    uch 
    188       1.1    uch 	_emac3_reg_write_4(EMAC3_ISER, ~0);
    189       1.1    uch }
    190       1.1    uch 
    191       1.1    uch void
    192       1.1    uch emac3_intr_disable()
    193       1.1    uch {
    194       1.1    uch 
    195       1.1    uch 	_emac3_reg_write_4(EMAC3_ISER, 0);
    196       1.1    uch }
    197       1.1    uch 
    198       1.1    uch void
    199       1.1    uch emac3_intr_clear()
    200       1.1    uch {
    201       1.1    uch 
    202       1.1    uch 	_emac3_reg_write_4(EMAC3_ISR, _emac3_reg_read_4(EMAC3_ISR));
    203       1.1    uch }
    204       1.1    uch 
    205       1.1    uch int
    206       1.1    uch emac3_intr(void *arg)
    207       1.1    uch {
    208       1.1    uch 	u_int32_t r = _emac3_reg_read_4(EMAC3_ISR);
    209       1.1    uch 
    210       1.1    uch 	DPRINTF("%08x\n", r);
    211       1.1    uch 	_emac3_reg_write_4(EMAC3_ISR, r);
    212       1.1    uch 
    213       1.1    uch 	return (1);
    214       1.1    uch }
    215       1.1    uch 
    216       1.1    uch void
    217       1.1    uch emac3_tx_kick()
    218       1.1    uch {
    219       1.1    uch 
    220       1.1    uch 	_emac3_reg_write_4(EMAC3_TMR0, TMR0_GNP0);
    221       1.1    uch }
    222       1.1    uch 
    223       1.1    uch int
    224       1.1    uch emac3_tx_done()
    225       1.1    uch {
    226       1.1    uch 
    227       1.1    uch 	return (_emac3_reg_read_4(EMAC3_TMR0) & TMR0_GNP0);
    228       1.1    uch }
    229       1.1    uch 
    230       1.1    uch void
    231       1.1    uch emac3_setmulti(struct emac3_softc *sc, struct ethercom *ec)
    232       1.1    uch {
    233       1.1    uch 	struct ether_multi *enm;
    234       1.1    uch 	struct ether_multistep step;
    235       1.1    uch 	struct ifnet *ifp = &ec->ec_if;
    236       1.1    uch 	u_int32_t r;
    237       1.1    uch 
    238       1.1    uch 	r = _emac3_reg_read_4(EMAC3_RMR);
    239       1.1    uch 	r &= ~(RMR_PME | RMR_PMME | RMR_MIAE);
    240       1.1    uch 
    241       1.1    uch 	if (ifp->if_flags & IFF_PROMISC) {
    242       1.1    uch allmulti:
    243       1.1    uch 		ifp->if_flags |= IFF_ALLMULTI;
    244       1.1    uch 		r |= RMR_PME;
    245       1.1    uch 		_emac3_reg_write_4(EMAC3_RMR, r);
    246       1.1    uch 
    247       1.1    uch 		return;
    248       1.1    uch 	}
    249       1.1    uch 
    250       1.1    uch 	ETHER_FIRST_MULTI(step, ec, enm);
    251       1.1    uch 	while (enm != NULL) {
    252       1.1    uch 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    253       1.1    uch 		    ETHER_ADDR_LEN) != 0)
    254       1.1    uch 			goto allmulti;
    255       1.1    uch 
    256       1.1    uch 		ETHER_NEXT_MULTI(step, enm)
    257       1.1    uch 	}
    258       1.1    uch 
    259       1.1    uch 	/* XXX always multicast promiscuous mode. XXX use hash table.. */
    260       1.1    uch 	ifp->if_flags |= IFF_ALLMULTI;
    261       1.1    uch 	r |= RMR_PMME;
    262       1.1    uch 	_emac3_reg_write_4(EMAC3_RMR, r);
    263       1.1    uch }
    264       1.1    uch 
    265       1.1    uch int
    266       1.1    uch emac3_soft_reset()
    267       1.1    uch {
    268       1.1    uch 	int retry = 10000;
    269       1.1    uch 
    270       1.1    uch 	_emac3_reg_write_4(EMAC3_MR0, MR0_SRST);
    271       1.1    uch 
    272       1.1    uch 	while ((_emac3_reg_read_4(EMAC3_MR0) & MR0_SRST) == MR0_SRST &&
    273       1.1    uch 	    --retry > 0)
    274       1.1    uch 		;
    275       1.1    uch 
    276       1.1    uch 	return (retry == 0);
    277       1.1    uch }
    278       1.1    uch 
    279       1.1    uch void
    280       1.1    uch emac3_config(const u_int8_t *eaddr)
    281       1.1    uch {
    282       1.1    uch 
    283       1.1    uch 	/* set ethernet address */
    284       1.1    uch 	_emac3_reg_write_4(EMAC3_IAHR, (eaddr[0] << 8) | eaddr[1]);
    285       1.1    uch 	_emac3_reg_write_4(EMAC3_IALR, (eaddr[2] << 24) | (eaddr[3] << 16) |
    286       1.1    uch 	    (eaddr[4] << 8) | eaddr[5]);
    287       1.1    uch 
    288       1.1    uch 	/* inter-frame GAP */
    289       1.1    uch 	_emac3_reg_write_4(EMAC3_IPGVR, 4);
    290       1.1    uch 
    291       1.1    uch 	/* RX mode */
    292       1.1    uch 	_emac3_reg_write_4(EMAC3_RMR,
    293       1.1    uch 	    RMR_SP |	/* strip padding */
    294       1.1    uch 	    RMR_SFCS |	/* strip FCS */
    295       1.1    uch 	    RMR_IAE |	/* individual address enable */
    296       1.1    uch 	    RMR_BAE);	/* boradcast address enable */
    297       1.1    uch 
    298       1.1    uch 	/* TX mode */
    299       1.1    uch 	_emac3_reg_write_4(EMAC3_TMR1,
    300       1.1    uch 	    ((7 << TMR1_TLR_SHIFT) & TMR1_TLR_MASK) | /* 16 word burst */
    301       1.1    uch 	    ((15 << TMR1_TUR_SHIFT) & TMR1_TUR_MASK));
    302       1.1    uch 
    303       1.1    uch 	/* TX threshold */
    304       1.1    uch 	_emac3_reg_write_4(EMAC3_TRTR,
    305       1.1    uch 	    (12 << TRTR_SHIFT) & TRTR_MASK); /* 832 bytes */
    306       1.1    uch 
    307       1.1    uch 	/* RX watermark */
    308       1.1    uch 	_emac3_reg_write_4(EMAC3_RWMR,
    309       1.1    uch 	    ((16 << RWMR_RLWM_SHIFT) & RWMR_RLWM_MASK) |
    310       1.1    uch 	    ((128 << RWMR_RHWM_SHIFT) & RWMR_RHWM_MASK));
    311       1.1    uch }
    312       1.1    uch 
    313       1.1    uch /*
    314       1.1    uch  * PHY/MII
    315       1.1    uch  */
    316       1.1    uch int
    317       1.1    uch emac3_ifmedia_upd(struct ifnet *ifp)
    318       1.1    uch {
    319       1.1    uch 	struct emac3_softc *sc;
    320       1.1    uch 
    321       1.1    uch 	sc = ifp->if_softc;
    322       1.1    uch 
    323       1.1    uch 	return (mii_mediachg(&sc->mii));
    324       1.1    uch }
    325       1.1    uch 
    326       1.1    uch void
    327       1.1    uch emac3_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
    328       1.1    uch {
    329       1.1    uch 	struct emac3_softc *sc;
    330       1.1    uch 
    331       1.1    uch 	sc = ifp->if_softc;
    332       1.1    uch 
    333       1.1    uch 	mii_pollstat(&sc->mii);
    334       1.1    uch 	ifmr->ifm_status = sc->mii.mii_media_status;
    335       1.1    uch 	ifmr->ifm_active = sc->mii.mii_media_active;
    336       1.1    uch }
    337       1.1    uch 
    338       1.1    uch void
    339       1.1    uch emac3_phy_writereg(struct device *self, int phy, int reg, int data)
    340       1.1    uch {
    341       1.1    uch 
    342       1.1    uch 	if (emac3_phy_ready() != 0)
    343       1.1    uch 		return;
    344       1.1    uch 
    345       1.1    uch 	_emac3_reg_write_4(EMAC3_STACR, STACR_WRITE |
    346       1.1    uch 	    ((phy << STACR_PCDASHIFT) & STACR_PCDA)  | /* command dest addr*/
    347       1.1    uch 	    ((reg << STACR_PRASHIFT) & STACR_PRA) |   /* register addr */
    348       1.1    uch 	    ((data << STACR_PHYDSHIFT) & STACR_PHYD)); /* data */
    349       1.1    uch 
    350       1.1    uch 	if (emac3_phy_ready() != 0)
    351       1.1    uch 		return;
    352       1.1    uch }
    353       1.1    uch 
    354       1.1    uch int
    355       1.1    uch emac3_phy_readreg(struct device *self, int phy, int reg)
    356       1.1    uch {
    357       1.1    uch 
    358       1.1    uch 	if (emac3_phy_ready() != 0)
    359       1.1    uch 		return (0);
    360       1.1    uch 
    361       1.1    uch 	_emac3_reg_write_4(EMAC3_STACR, STACR_READ |
    362       1.1    uch 	    ((phy << STACR_PCDASHIFT) & STACR_PCDA)  | /* command dest addr*/
    363       1.1    uch 	    ((reg << STACR_PRASHIFT) & STACR_PRA));   /* register addr */
    364       1.1    uch 
    365       1.1    uch 	if (emac3_phy_ready() != 0)
    366       1.1    uch 		return (0);
    367       1.1    uch 
    368       1.1    uch 	return ((_emac3_reg_read_4(EMAC3_STACR) >> STACR_PHYDSHIFT) & 0xffff);
    369       1.1    uch }
    370       1.1    uch 
    371       1.1    uch void
    372       1.1    uch emac3_phy_statchg(struct device *dev)
    373       1.1    uch {
    374       1.1    uch #define EMAC3_FDX	(MR1_FDE | MR1_EIFC | MR1_APP)
    375       1.1    uch 	struct emac3_softc *sc = (void *)dev;
    376       1.1    uch 	int media;
    377       1.1    uch 	u_int32_t r;
    378       1.1    uch 
    379       1.1    uch 	media = sc->mii.mii_media_active;
    380       1.1    uch 
    381       1.1    uch 	r = _emac3_reg_read_4(EMAC3_MR1);
    382       1.1    uch 
    383       1.1    uch 	r &= ~(MR1_MF_MASK | MR1_IST | EMAC3_FDX);
    384       1.1    uch 
    385       1.1    uch 	switch (media & 0x1f) {
    386       1.1    uch 	default:
    387       1.1    uch 		printf("unknown media type. %08x", media);
    388       1.1    uch 		/* FALLTHROUGH */
    389       1.1    uch 	case IFM_100_TX:
    390       1.1    uch 		r |= (MR1_MF_100MBS | MR1_IST);
    391       1.1    uch 		if (media & IFM_FDX)
    392       1.1    uch 			r |= EMAC3_FDX;
    393       1.1    uch 
    394       1.1    uch 		break;
    395       1.1    uch 	case IFM_10_T:
    396       1.1    uch 		r |= MR1_MF_10MBS;
    397       1.1    uch 		if (media & IFM_FDX)
    398       1.1    uch 			r |= (EMAC3_FDX | MR1_IST);
    399       1.1    uch 		break;
    400       1.1    uch 	}
    401       1.1    uch 
    402       1.1    uch 	_emac3_reg_write_4(EMAC3_MR1, r);
    403       1.1    uch 
    404       1.1    uch 	/* store current state for re-initialize */
    405       1.1    uch 	sc->mode1_reg = _emac3_reg_read_4(EMAC3_MR1);
    406       1.1    uch #undef EMAC3_FDX
    407       1.1    uch }
    408       1.1    uch 
    409       1.1    uch int
    410       1.1    uch emac3_phy_ready()
    411       1.1    uch {
    412       1.1    uch 	int retry = 10000;
    413       1.1    uch 
    414       1.1    uch 	while ((_emac3_reg_read_4(EMAC3_STACR) & STACR_OC) == 0 &&
    415       1.1    uch 	    --retry > 0)
    416       1.1    uch 		;
    417       1.1    uch 	if (retry == 0) {
    418       1.1    uch 		printf("emac3: phy busy.\n");
    419       1.1    uch 		return (1);
    420       1.1    uch 	}
    421       1.1    uch 
    422       1.1    uch 	return (0);
    423       1.1    uch }
    424       1.1    uch 
    425