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