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