Home | History | Annotate | Line # | Download | only in mii
mvphy.c revision 1.1
      1  1.1  gdamore /*	$NetBSD: mvphy.c,v 1.1 2006/07/21 23:55:27 gdamore Exp $	*/
      2  1.1  gdamore 
      3  1.1  gdamore /*-
      4  1.1  gdamore  * Copyright (c) 2006 Sam Leffler, Errno Consulting
      5  1.1  gdamore  * All rights reserved.
      6  1.1  gdamore  *
      7  1.1  gdamore  * Redistribution and use in source and binary forms, with or without
      8  1.1  gdamore  * modification, are permitted provided that the following conditions
      9  1.1  gdamore  * are met:
     10  1.1  gdamore  * 1. Redistributions of source code must retain the above copyright
     11  1.1  gdamore  *    notice, this list of conditions and the following disclaimer.
     12  1.1  gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  gdamore  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  gdamore  *    documentation and/or other materials provided with the distribution.
     15  1.1  gdamore  *
     16  1.1  gdamore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  gdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  gdamore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  gdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  gdamore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  gdamore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  gdamore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  gdamore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  gdamore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  gdamore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  gdamore  * SUCH DAMAGE.
     27  1.1  gdamore  */
     28  1.1  gdamore 
     29  1.1  gdamore /*
     30  1.1  gdamore  * Driver for Marvell 88E6060 10/100 5-port PHY switch.
     31  1.1  gdamore  */
     32  1.1  gdamore 
     33  1.1  gdamore #include <sys/cdefs.h>
     34  1.1  gdamore __KERNEL_RCSID(0, "$NetBSD: mvphy.c,v 1.1 2006/07/21 23:55:27 gdamore Exp $");
     35  1.1  gdamore 
     36  1.1  gdamore #include <sys/param.h>
     37  1.1  gdamore #include <sys/systm.h>
     38  1.1  gdamore #include <sys/kernel.h>
     39  1.1  gdamore #include <sys/device.h>
     40  1.1  gdamore #include <sys/socket.h>
     41  1.1  gdamore #include <sys/errno.h>
     42  1.1  gdamore 
     43  1.1  gdamore #include <net/if.h>
     44  1.1  gdamore #include <net/if_media.h>
     45  1.1  gdamore 
     46  1.1  gdamore #include <dev/mii/mii.h>
     47  1.1  gdamore #include <dev/mii/miivar.h>
     48  1.1  gdamore #include <dev/mii/miidevs.h>
     49  1.1  gdamore 
     50  1.1  gdamore #include <dev/mii/mvphyreg.h>
     51  1.1  gdamore 
     52  1.1  gdamore #define	MV_PORT(sc)	((sc)->mii_phy - 16)	/* PHY # to switch port */
     53  1.1  gdamore #define	MV_CPU_PORT	5			/* port # of CPU port */
     54  1.1  gdamore 
     55  1.1  gdamore #define	MV_READ(p, phy, r) \
     56  1.1  gdamore 	(*(p)->mii_pdata->mii_readreg)(device_parent(&(p)->mii_dev), \
     57  1.1  gdamore 	    phy, (r))
     58  1.1  gdamore #define	MV_WRITE(p, phy, r, v) \
     59  1.1  gdamore 	(*(p)->mii_pdata->mii_writereg)(device_parent(&(p)->mii_dev), \
     60  1.1  gdamore 	    phy, (r), (v))
     61  1.1  gdamore 
     62  1.1  gdamore /* XXX sysctl'able */
     63  1.1  gdamore #define MV_ATUCTRL_ATU_SIZE_DEFAULT	2	/* 1024 entry database */
     64  1.1  gdamore #define MV_ATUCTRL_AGE_TIME_DEFAULT	19	/* 19 * 16 = 304 seconds */
     65  1.1  gdamore 
     66  1.1  gdamore /*
     67  1.1  gdamore  * Register manipulation macros that expect bit field defines
     68  1.1  gdamore  * to follow the convention that an _S suffix is appended for
     69  1.1  gdamore  * a shift count, while the field mask has no suffix.
     70  1.1  gdamore  */
     71  1.1  gdamore #define	SM(_v, _f)	(((_v) << _f##_S) & _f)
     72  1.1  gdamore #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
     73  1.1  gdamore 
     74  1.1  gdamore static int	mvphymatch(struct device *, struct cfdata *, void *);
     75  1.1  gdamore static void	mvphyattach(struct device *, struct device *, void *);
     76  1.1  gdamore 
     77  1.1  gdamore CFATTACH_DECL(mvphy, sizeof(struct mii_softc),
     78  1.1  gdamore     mvphymatch, mvphyattach, mii_phy_detach, mii_phy_activate);
     79  1.1  gdamore 
     80  1.1  gdamore static int	mvphy_service(struct mii_softc *, struct mii_data *, int);
     81  1.1  gdamore static void	mvphy_status(struct mii_softc *);
     82  1.1  gdamore static void	mvphy_reset(struct mii_softc *sc);
     83  1.1  gdamore 
     84  1.1  gdamore static const struct mii_phy_funcs mvphy_funcs = {
     85  1.1  gdamore 	mvphy_service, mvphy_status, mvphy_reset,
     86  1.1  gdamore };
     87  1.1  gdamore 
     88  1.1  gdamore static const struct mii_phydesc mvphys[] = {
     89  1.1  gdamore 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E6060,
     90  1.1  gdamore 	  MII_STR_xxMARVELL_E6060 },
     91  1.1  gdamore 
     92  1.1  gdamore 	{ 0,				0,
     93  1.1  gdamore 	  NULL },
     94  1.1  gdamore };
     95  1.1  gdamore 
     96  1.1  gdamore /*
     97  1.1  gdamore  * On AP30/AR5312 the switch is configured in one of two ways:
     98  1.1  gdamore  * as a ROUTER or as a BRIDGE.  The ROUTER config sets up ports
     99  1.1  gdamore  * 0-3 as LAN ports, port 4 as the WAN port, and port 5 connects
    100  1.1  gdamore  * to the MAC in the 5312.  The BRIDGE config sets up ports
    101  1.1  gdamore  * 0-4 as LAN ports with port 5 connected to the MAC in the 5312.
    102  1.1  gdamore  */
    103  1.1  gdamore struct mvPhyConfig {
    104  1.1  gdamore 	uint16_t switchPortAddr;/* switch port associated with PHY */
    105  1.1  gdamore 	uint16_t vlanSetting;	/* VLAN table setting  for PHY */
    106  1.1  gdamore 	uint32_t portControl;	/* switch port control setting for PHY */
    107  1.1  gdamore };
    108  1.1  gdamore static const struct mvPhyConfig routerConfig[] = {
    109  1.1  gdamore 	{ 0x18, 0x2e,		/* PHY port 0 = LAN port 0 */
    110  1.1  gdamore 	  MV_PORT_CONTROL_PORT_STATE_FORWARDING },
    111  1.1  gdamore 	{ 0x19, 0x2d,		/* PHY port 1 = LAN port 1 */
    112  1.1  gdamore 	  MV_PORT_CONTROL_PORT_STATE_FORWARDING },
    113  1.1  gdamore 	{ 0x1a, 0x2b,		/* PHY port 2 = LAN port 2 */
    114  1.1  gdamore 	  MV_PORT_CONTROL_PORT_STATE_FORWARDING },
    115  1.1  gdamore 	{ 0x1b, 0x27,		/* PHY port 3 = LAN port 3 */
    116  1.1  gdamore 	  MV_PORT_CONTROL_PORT_STATE_FORWARDING },
    117  1.1  gdamore 	{ 0x1c, 0x1020,		/* PHY port 4 = WAN port */
    118  1.1  gdamore 	  MV_PORT_CONTROL_PORT_STATE_FORWARDING },
    119  1.1  gdamore 	/* NB: 0x0f =>'s send only to LAN ports */
    120  1.1  gdamore 	{ 0x1d, 0x0f,		/* PHY port 5 = CPU port */
    121  1.1  gdamore 	  MV_PORT_CONTROL_PORT_STATE_FORWARDING
    122  1.1  gdamore #if 0
    123  1.1  gdamore 	  | MV_PORT_CONTROL_INGRESS_TRAILER
    124  1.1  gdamore 	  | MV_PORT_CONTROL_EGRESS_MODE
    125  1.1  gdamore #endif
    126  1.1  gdamore 	  }
    127  1.1  gdamore };
    128  1.1  gdamore static const struct mvPhyConfig bridgeConfig[] = {
    129  1.1  gdamore 	{ 0x18, 0x3e,		/* PHY port 0 = LAN port 0 */
    130  1.1  gdamore 	  MV_PORT_CONTROL_PORT_STATE_FORWARDING },
    131  1.1  gdamore 	{ 0x19, 0x3d,		/* PHY port 1 = LAN port 1 */
    132  1.1  gdamore 	  MV_PORT_CONTROL_PORT_STATE_FORWARDING },
    133  1.1  gdamore 	{ 0x1a, 0x3b,		/* PHY port 2 = LAN port 2 */
    134  1.1  gdamore 	  MV_PORT_CONTROL_PORT_STATE_FORWARDING },
    135  1.1  gdamore 	{ 0x1b, 0x37,		/* PHY port 3 = LAN port 3 */
    136  1.1  gdamore 	  MV_PORT_CONTROL_PORT_STATE_FORWARDING },
    137  1.1  gdamore 	{ 0x1c, 0x37,		/* PHY port 4 = LAN port 4 */
    138  1.1  gdamore 	  MV_PORT_CONTROL_PORT_STATE_FORWARDING },
    139  1.1  gdamore 	/* NB: 0x1f =>'s send to all ports */
    140  1.1  gdamore 	{ 0x1d, 0x1f,		/* PHY port 5 = CPU port */
    141  1.1  gdamore 	  MV_PORT_CONTROL_PORT_STATE_FORWARDING
    142  1.1  gdamore #if 0
    143  1.1  gdamore 	  | MV_PORT_CONTROL_INGRESS_TRAILER
    144  1.1  gdamore 	  | MV_PORT_CONTROL_EGRESS_MODE
    145  1.1  gdamore #endif
    146  1.1  gdamore 	}
    147  1.1  gdamore };
    148  1.1  gdamore 
    149  1.1  gdamore static void mvphy_switchconfig(struct mii_softc *, int);
    150  1.1  gdamore static void mvphy_flushatu(struct mii_softc *);
    151  1.1  gdamore 
    152  1.1  gdamore static int
    153  1.1  gdamore mvphymatch(struct device *parent, struct cfdata *match, void *aux)
    154  1.1  gdamore {
    155  1.1  gdamore 	struct mii_attach_args *ma = aux;
    156  1.1  gdamore 
    157  1.1  gdamore 	if (mii_phy_match(ma, mvphys) != NULL)
    158  1.1  gdamore 		return (10);
    159  1.1  gdamore 
    160  1.1  gdamore 	return (0);
    161  1.1  gdamore }
    162  1.1  gdamore 
    163  1.1  gdamore static void
    164  1.1  gdamore mvphyattach(struct device *parent, struct device *self, void *aux)
    165  1.1  gdamore {
    166  1.1  gdamore 	struct mii_softc *sc = device_private(self);
    167  1.1  gdamore 	struct mii_attach_args *ma = aux;
    168  1.1  gdamore 	struct mii_data *mii = ma->mii_data;
    169  1.1  gdamore 	const struct mii_phydesc *mpd;
    170  1.1  gdamore 
    171  1.1  gdamore 	mpd = mii_phy_match(ma, mvphys);
    172  1.1  gdamore 	aprint_naive(": Media interface\n");
    173  1.1  gdamore 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    174  1.1  gdamore 
    175  1.1  gdamore 	sc->mii_inst = mii->mii_instance;
    176  1.1  gdamore 	sc->mii_phy = ma->mii_phyno;
    177  1.1  gdamore 	sc->mii_funcs = &mvphy_funcs;
    178  1.1  gdamore 	sc->mii_pdata = mii;
    179  1.1  gdamore 	sc->mii_flags = ma->mii_flags;
    180  1.1  gdamore 	sc->mii_anegticks = 5;
    181  1.1  gdamore 
    182  1.1  gdamore 	if (MV_PORT(sc) == 0) {		/* NB: only when attaching first PHY */
    183  1.1  gdamore 		/*
    184  1.1  gdamore 		 * Set the global switch settings and configure the
    185  1.1  gdamore 		 * CPU port since it does not probe as a visible PHY.
    186  1.1  gdamore 		 */
    187  1.1  gdamore 		MV_WRITE(sc, MII_MV_SWITCH_GLOBAL_ADDR, MV_ATU_CONTROL,
    188  1.1  gdamore 		      SM(MV_ATUCTRL_AGE_TIME_DEFAULT, MV_ATUCTRL_AGE_TIME)
    189  1.1  gdamore 		    | SM(MV_ATUCTRL_ATU_SIZE_DEFAULT, MV_ATUCTRL_ATU_SIZE));
    190  1.1  gdamore 		mvphy_switchconfig(sc, MV_CPU_PORT);
    191  1.1  gdamore 	}
    192  1.1  gdamore 	PHY_RESET(sc);
    193  1.1  gdamore 
    194  1.1  gdamore 	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    195  1.1  gdamore 	aprint_normal("%s: ", sc->mii_dev.dv_xname);
    196  1.1  gdamore 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
    197  1.1  gdamore 		aprint_error("no media present");
    198  1.1  gdamore 	else
    199  1.1  gdamore 		mii_phy_add_media(sc);
    200  1.1  gdamore 	aprint_normal("\n");
    201  1.1  gdamore }
    202  1.1  gdamore 
    203  1.1  gdamore static int
    204  1.1  gdamore mvphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    205  1.1  gdamore {
    206  1.1  gdamore 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    207  1.1  gdamore 
    208  1.1  gdamore 	if (!device_is_active(&sc->mii_dev))
    209  1.1  gdamore 		return (ENXIO);
    210  1.1  gdamore 
    211  1.1  gdamore 	switch (cmd) {
    212  1.1  gdamore 	case MII_POLLSTAT:
    213  1.1  gdamore 		/*
    214  1.1  gdamore 		 * If we're not polling our PHY instance, just return.
    215  1.1  gdamore 		 */
    216  1.1  gdamore 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    217  1.1  gdamore 			return (0);
    218  1.1  gdamore 		break;
    219  1.1  gdamore 
    220  1.1  gdamore 	case MII_MEDIACHG:
    221  1.1  gdamore 		/*
    222  1.1  gdamore 		 * If the media indicates a different PHY instance,
    223  1.1  gdamore 		 * isolate ourselves.
    224  1.1  gdamore 		 */
    225  1.1  gdamore 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    226  1.1  gdamore 			/* XXX? */
    227  1.1  gdamore 			return (0);
    228  1.1  gdamore 		}
    229  1.1  gdamore 
    230  1.1  gdamore 		/*
    231  1.1  gdamore 		 * If the interface is not up, don't do anything.
    232  1.1  gdamore 		 */
    233  1.1  gdamore 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    234  1.1  gdamore 			break;
    235  1.1  gdamore 
    236  1.1  gdamore 		mii_phy_setmedia(sc);
    237  1.1  gdamore 		break;
    238  1.1  gdamore 
    239  1.1  gdamore 	case MII_TICK:
    240  1.1  gdamore 		/*
    241  1.1  gdamore 		 * If we're not currently selected, just return.
    242  1.1  gdamore 		 */
    243  1.1  gdamore 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    244  1.1  gdamore 			return (0);
    245  1.1  gdamore 
    246  1.1  gdamore 		if (mii_phy_tick(sc) == EJUSTRETURN)
    247  1.1  gdamore 			return (0);
    248  1.1  gdamore 		break;
    249  1.1  gdamore 
    250  1.1  gdamore 	case MII_DOWN:
    251  1.1  gdamore 		mii_phy_down(sc);
    252  1.1  gdamore 		return (0);
    253  1.1  gdamore 	}
    254  1.1  gdamore 
    255  1.1  gdamore 	/* Update the media status. */
    256  1.1  gdamore 	mii_phy_status(sc);
    257  1.1  gdamore 
    258  1.1  gdamore 	/* Callback if something changed. */
    259  1.1  gdamore 	mii_phy_update(sc, cmd);
    260  1.1  gdamore 	return (0);
    261  1.1  gdamore }
    262  1.1  gdamore 
    263  1.1  gdamore static void
    264  1.1  gdamore mvphy_status(struct mii_softc *sc)
    265  1.1  gdamore {
    266  1.1  gdamore 	struct mii_data *mii = sc->mii_pdata;
    267  1.1  gdamore 	int hwstatus;
    268  1.1  gdamore 
    269  1.1  gdamore 	mii->mii_media_status = IFM_AVALID;
    270  1.1  gdamore 	mii->mii_media_active = IFM_ETHER;
    271  1.1  gdamore 
    272  1.1  gdamore 	hwstatus = PHY_READ(sc, MII_MV_PHY_SPECIFIC_STATUS);
    273  1.1  gdamore 	if (hwstatus & MV_STATUS_REAL_TIME_LINK_UP) {
    274  1.1  gdamore 		mii->mii_media_status |= IFM_ACTIVE;
    275  1.1  gdamore 		if (hwstatus & MV_STATUS_RESOLVED_SPEED_100)
    276  1.1  gdamore 			mii->mii_media_active |= IFM_100_TX;
    277  1.1  gdamore 		else
    278  1.1  gdamore 			mii->mii_media_active |= IFM_10_T;
    279  1.1  gdamore 		if (hwstatus & MV_STATUS_RESOLVED_DUPLEX_FULL)
    280  1.1  gdamore 			mii->mii_media_active |= IFM_FDX;
    281  1.1  gdamore 	} else {
    282  1.1  gdamore 		mii->mii_media_active |= IFM_NONE;
    283  1.1  gdamore 		/* XXX flush ATU only on link down transition */
    284  1.1  gdamore 		mvphy_flushatu(sc);
    285  1.1  gdamore 	}
    286  1.1  gdamore }
    287  1.1  gdamore 
    288  1.1  gdamore static void
    289  1.1  gdamore mvphy_reset(struct mii_softc *sc)
    290  1.1  gdamore {
    291  1.1  gdamore 
    292  1.1  gdamore 	/* XXX handle fixed media config */
    293  1.1  gdamore 	PHY_WRITE(sc, MII_BMCR, BMCR_RESET | BMCR_AUTOEN);
    294  1.1  gdamore 	mvphy_switchconfig(sc, MV_PORT(sc));
    295  1.1  gdamore }
    296  1.1  gdamore 
    297  1.1  gdamore /*
    298  1.1  gdamore  * Configure switch for the specified port.
    299  1.1  gdamore  */
    300  1.1  gdamore static void
    301  1.1  gdamore mvphy_switchconfig(struct mii_softc *sc, int port)
    302  1.1  gdamore {
    303  1.1  gdamore 	/* XXX router vs bridge */
    304  1.1  gdamore 	const struct mvPhyConfig *conf = &routerConfig[port];
    305  1.1  gdamore 
    306  1.1  gdamore 	MV_WRITE(sc, conf->switchPortAddr, MV_PORT_BASED_VLAN_MAP,
    307  1.1  gdamore 	    conf->vlanSetting);
    308  1.1  gdamore 	/* XXX administrative control of port enable? */
    309  1.1  gdamore 	MV_WRITE(sc, conf->switchPortAddr, MV_PORT_CONTROL, conf->portControl);
    310  1.1  gdamore 	MV_WRITE(sc, conf->switchPortAddr, MV_PORT_ASSOCIATION_VECTOR, 1<<port);
    311  1.1  gdamore }
    312  1.1  gdamore 
    313  1.1  gdamore /*
    314  1.1  gdamore  * Flush the Address Translation Unit (ATU).
    315  1.1  gdamore  */
    316  1.1  gdamore static void
    317  1.1  gdamore mvphy_flushatu(struct mii_softc *sc)
    318  1.1  gdamore {
    319  1.1  gdamore 	uint16_t status;
    320  1.1  gdamore 	int i;
    321  1.1  gdamore 
    322  1.1  gdamore printf("%s: %s\n", sc->mii_dev.dv_xname, __func__);/*XXX*/
    323  1.1  gdamore 	/* wait for any previous request to complete */
    324  1.1  gdamore 	/* XXX if busy defer to tick */
    325  1.1  gdamore 	/* XXX timeout */
    326  1.1  gdamore 	for (i = 0; i < 1000; i++) {
    327  1.1  gdamore 		status = MV_READ(sc, MII_MV_SWITCH_GLOBAL_ADDR,
    328  1.1  gdamore 				MV_ATU_OPERATION);
    329  1.1  gdamore 		if (MV_ATU_IS_BUSY(status))
    330  1.1  gdamore 			break;
    331  1.1  gdamore 	}
    332  1.1  gdamore 	if (i != 1000) {
    333  1.1  gdamore 		MV_WRITE(sc, MII_MV_SWITCH_GLOBAL_ADDR, MV_ATU_OPERATION,
    334  1.1  gdamore 		    MV_ATU_OP_FLUSH_ALL | MV_ATU_BUSY);
    335  1.1  gdamore 	} else
    336  1.1  gdamore 		printf("%s: timeout waiting for ATU flush\n",
    337  1.1  gdamore 		    sc->mii_dev.dv_xname);
    338  1.1  gdamore }
    339