/* File - sam_mii.c * * Description : This file is used to access the MII interface on MPC85XX * Board. * */ #include "sam_mii.h" /* Attach the samMiiAttach functions with QNX stack. */ CFATTACH_DECL(samPHY, sizeof(struct mii_softc), samphy_match, samphy_attach, mii_phy_detach, mii_phy_activate); const struct mii_phy_funcs samphy_funcs = { NULL, NULL, samphy_reset, }; /* Global variable to store the parent structure. */ struct mii_softc *global_mii_softc; /****************************************************************************** * FUNCTION NAME : samphy_match * * * * INPUT PARAM(s) : struct device *parent * * struct cfdata *match * * void *aux * * * * OUTPUT PARAM(s): None. * * * * Return : 0 - SUCCESS * * 1 - FAILURE * * * * Description : This function is used to find the parent sam PHY. * * * * * ******************************************************************************/ int samphy_match(struct device *parent, struct cfdata *match, void *aux) { struct mii_attach_args *ma = aux; if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 && MII_MODEL(ma->mii_id2) != 0) return (0); #ifndef __QNXNTO__ /* * Make sure the parent is an `sam'. */ if (strcmp(parent->dv_cfdata->cf_name, "sam") != 0) return (0); #else if (strcmp(parent->dv_cfdata, "sam") != 0) return (0); #endif /* Return if PHY is not present. */ return (10); } /****************************************************************************** * FUNCTION NAME : samphy_attach * * * * INPUT PARAM(s) : struct device *parent * * struct device *self * * void *aux * * * * OUTPUT PARAM(s): None. * * * * Return : 0 - SUCCESS * * 1 - FAILURE * * * * Description : This function is used to attach MII interface layer to * * Parent device and create a name space in /dev/mii for * * acccess the MII interface from CPSS application * * * * * ******************************************************************************/ #ifndef __QNXNTO__ void #else int #endif samphy_attach(struct device *parent, struct device *self, void *aux) { struct mii_softc *sc = (struct mii_softc *)self; struct mii_attach_args *ma = aux; struct mii_data *mii = ma->mii_data; resmgr_attr_t resmgr_attr; dispatch_t *dispatch; dispatch_context_t *dispatchContext; int resmgrid; char buffer[MAX_MSG_SIZE]; aprint_naive(": Media interface\n"); /* Update the mii_soft structure in a global variable * to access the Parent PHY . */ global_mii_softc = sc; sc->mii_inst = mii->mii_instance; sc->mii_phy = ma->mii_phyno; sc->mii_funcs = &samphy_funcs; sc->mii_pdata = mii; sc->mii_flags = ma->mii_flags; sc->mii_anegticks = 5; /* * The MPC 85xx PHY can never be isolated, so never allow non-zero * instances! */ if (mii->mii_instance != 0) { aprint_error("%s: ignoring this PHY, non-zero instance\n", sc->mii_dev.dv_xname); #ifndef __QNXNTO__ return; #else return 1; #endif } sc->mii_flags |= MIIF_NOISOLATE; PHY_RESET(sc); sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; aprint_normal("%s: ", sc->mii_dev.dv_xname); if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) aprint_error("no media present"); else mii_phy_add_media(sc); /* Create a resource manager to access the MII interface from * the CPSS Application. */ /* Create the dispatch interface */ if( (dispatch = dispatch_create()) == NULL ) { LOG(LOG_ERROR,"Unable to alloacet dispatch handle:[%s]\n", __FUNCTION__); return 1; } /* Initialize the Resource manager attributes */ memset(&resmgr_attr,0,sizeof(resmgr_attr)); resmgr_attr.nparts_max = 1; resmgr_attr.msg_max_size = MAX_MSG_SIZE; /* initialize functions for handling messages */ iofunc_func_init(_RESMGR_CONNECT_NFUNCS,&connect_funcs, _RESMGR_IO_NFUNCS,&io_funcs); /* devctl function to perform MII read/write */ io_funcs.devctl = io_devctl; /* initialize the attribute structure used by the device */ iofunc_attr_init(&attr,S_IFNAM | 0666,0,0); attr.nbytes = strlen(buffer) + 1; /* Attach our device name */ if( ( resmgrid = resmgr_attach(dispatch,&resmgr_attr,"/dev/mii", _FTYPE_ANY,0,&connect_funcs, &io_funcs,&attr)) == -1) { LOG(LOG_ERROR,"Unable to attach the device name:[%s]\n", __FUNCTION__); return 1; } /* Allocate a context structure */ dispatchContext = dispatch_context_alloc(dispatch); /* start the resource manager message loop */ while(1) { if( (dispatchContext = dispatch_block(dispatchContext)) == NULL ) { LOG(LOG_ERROR,"Unable to Dispatch Block:[%s]\n", __FUNCTION__); return 1; } dispatch_handler(dispatchContext); } #ifdef __QNXNTO__ return 0; #endif } /****************************************************************************** * FUNCTION NAME : samphy_reset * * * * INPUT PARAM(s) : struct device *parent * * struct device *self * * void *aux * * * * OUTPUT PARAM(s): None. * * * * Return : SUCESS/FAILURE * * * * Description : This function is used to reset the sam PHY. * * * ******************************************************************************/ void samphy_reset(struct mii_softc *sc) { mii_phy_reset(sc); /* * XXX MPC85XX PHY doesn't set the BMCR properly after * XXX reset, which breaks autonegotiation. */ PHY_WRITE(sc, MII_BMCR, BMCR_S100|BMCR_AUTOEN|BMCR_FDX); } /****************************************************************************** * Function Name : io_devctl * * * * Description : This function is get the user commands to read/write the * * MII registers. * * * * INPUT PARAM(s) : resmgr_context_t ctp * * io_devctl_t *msg * * RESMGR_OCB_T *ocb * * * * OUTPUT PARAM(s): msg. * * * * Return : SUCESS/FAILURE * ******************************************************************************/ int io_devctl(resmgr_context_t *ctp,io_devctl_t *msg,RESMGR_OCB_T *ocb) { int status = 0; int nbytes = 0; struct SMI_REG *smiReg; status = nbytes = 0; /* Allocate a memory for storing SMI register values */ smiReg = (struct SMI_REG*)malloc(sizeof(struct SMI_REG)); if( smiReg == NULL ) { return -1; } /* Get the Data */ smiReg = _DEVCTL_DATA(msg->i); /* process the devctl commands */ switch(msg->i.dcmd) { case MII_READ: /* Read the value from PHY Registers */ smiReg->value = PHY_READ(global_mii_softc, smiReg->devSlvId, smiReg->regAddr); nbytes = sizeof(smiReg); break; case MII_WRITE: /* Write the value in to PHY Registers */ status = PHY_WRITE(global_mii_softc, smiReg->devSlvId, smiReg->regAddr, smiReg->value); break; default : return (ENOSYS); }