1139749Simp/*-
254134Swpaul * Copyright (c) 1997, 1998, 1999
354134Swpaul *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
454134Swpaul *
554134Swpaul * Redistribution and use in source and binary forms, with or without
654134Swpaul * modification, are permitted provided that the following conditions
754134Swpaul * are met:
854134Swpaul * 1. Redistributions of source code must retain the above copyright
954134Swpaul *    notice, this list of conditions and the following disclaimer.
1054134Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1154134Swpaul *    notice, this list of conditions and the following disclaimer in the
1254134Swpaul *    documentation and/or other materials provided with the distribution.
1354134Swpaul * 3. All advertising materials mentioning features or use of this software
1454134Swpaul *    must display the following acknowledgement:
1554134Swpaul *	This product includes software developed by Bill Paul.
1654134Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1754134Swpaul *    may be used to endorse or promote products derived from this software
1854134Swpaul *    without specific prior written permission.
1954134Swpaul *
2054134Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2154134Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2254134Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2354134Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2454134Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2554134Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2654134Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2754134Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2854134Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2954134Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3054134Swpaul * THE POSSIBILITY OF SUCH DAMAGE.
3154134Swpaul */
3254134Swpaul
33119418Sobrien#include <sys/cdefs.h>
34119418Sobrien__FBSDID("$FreeBSD$");
35119418Sobrien
3654134Swpaul/*
3754134Swpaul * Pseudo-driver for internal NWAY support on DEC 21143 and workalike
38183505Smarius * controllers.  Technically we're abusing the miibus code to handle
3954134Swpaul * media selection and NWAY support here since there is no MII
40183505Smarius * interface.  However the logical operations are roughly the same,
4154134Swpaul * and the alternative is to create a fake MII interface in the driver,
4254134Swpaul * which is harder to do.
4354134Swpaul */
4454134Swpaul
4554134Swpaul#include <sys/param.h>
4654134Swpaul#include <sys/systm.h>
4754134Swpaul#include <sys/kernel.h>
4854134Swpaul#include <sys/socket.h>
4954134Swpaul#include <sys/errno.h>
5074914Sjhb#include <sys/lock.h>
5154134Swpaul#include <sys/module.h>
5267365Sjhb#include <sys/mutex.h>
5354134Swpaul#include <sys/bus.h>
5454134Swpaul
5554134Swpaul#include <net/if.h>
5654134Swpaul#include <net/if_arp.h>
5754134Swpaul#include <net/if_media.h>
5854134Swpaul
5954134Swpaul#include <dev/mii/mii.h>
6054134Swpaul#include <dev/mii/miivar.h>
61109514Sobrien#include "miidevs.h"
6254134Swpaul
6354134Swpaul#include <machine/bus.h>
6454134Swpaul#include <machine/resource.h>
6554134Swpaul
66119285Simp#include <dev/pci/pcivar.h>
6754134Swpaul
68151435Simp#include <dev/dc/if_dcreg.h>
6954134Swpaul
7054134Swpaul#include "miibus_if.h"
7154134Swpaul
7254134Swpaul#define DC_SETBIT(sc, reg, x)                           \
7354134Swpaul        CSR_WRITE_4(sc, reg,                            \
7454134Swpaul                CSR_READ_4(sc, reg) | x)
7554134Swpaul
7654134Swpaul#define DC_CLRBIT(sc, reg, x)                           \
7754134Swpaul        CSR_WRITE_4(sc, reg,                            \
7854134Swpaul                CSR_READ_4(sc, reg) & ~x)
7954134Swpaul
8054134Swpaul#define MIIF_AUTOTIMEOUT	0x0004
8154134Swpaul
8254577Swpaul/*
8354577Swpaul * This is the subsystem ID for the built-in 21143 ethernet
84183505Smarius * in several Compaq Presario systems.  Apparently these are
8554577Swpaul * 10Mbps only, so we need to treat them specially.
8654577Swpaul */
8754577Swpaul#define COMPAQ_PRESARIO_ID	0xb0bb0e11
8854577Swpaul
89105135Salfredstatic int dcphy_probe(device_t);
90105135Salfredstatic int dcphy_attach(device_t);
9154134Swpaul
9254134Swpaulstatic device_method_t dcphy_methods[] = {
9354134Swpaul	/* device interface */
9454134Swpaul	DEVMETHOD(device_probe,		dcphy_probe),
9554134Swpaul	DEVMETHOD(device_attach,	dcphy_attach),
9695722Sphk	DEVMETHOD(device_detach,	mii_phy_detach),
9754134Swpaul	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
98227908Smarius	DEVMETHOD_END
9954134Swpaul};
10054134Swpaul
10154134Swpaulstatic devclass_t dcphy_devclass;
10254134Swpaul
10354134Swpaulstatic driver_t dcphy_driver = {
10454134Swpaul	"dcphy",
10554134Swpaul	dcphy_methods,
10654134Swpaul	sizeof(struct mii_softc)
10754134Swpaul};
10854134Swpaul
10954134SwpaulDRIVER_MODULE(dcphy, miibus, dcphy_driver, dcphy_devclass, 0, 0);
11054134Swpaul
11192739Salfredstatic int	dcphy_service(struct mii_softc *, struct mii_data *, int);
11292739Salfredstatic void	dcphy_status(struct mii_softc *);
11392739Salfredstatic void	dcphy_reset(struct mii_softc *);
11496026Sphkstatic int	dcphy_auto(struct mii_softc *);
11554134Swpaul
116221407Smariusstatic const struct mii_phy_funcs dcphy_funcs = {
117221407Smarius	dcphy_service,
118221407Smarius	dcphy_status,
119221407Smarius	dcphy_reset
120221407Smarius};
121221407Smarius
122105135Salfredstatic int
123150763Simpdcphy_probe(device_t dev)
12454134Swpaul{
12554134Swpaul	struct mii_attach_args *ma;
12654134Swpaul
12754134Swpaul	ma = device_get_ivars(dev);
12854134Swpaul
12954134Swpaul	/*
13054134Swpaul	 * The dc driver will report the 21143 vendor and device
13154134Swpaul	 * ID to let us know that it wants us to attach.
13254134Swpaul	 */
13354134Swpaul	if (ma->mii_id1 != DC_VENDORID_DEC ||
13454134Swpaul	    ma->mii_id2 != DC_DEVICEID_21143)
135183505Smarius		return (ENXIO);
13654134Swpaul
13754134Swpaul	device_set_desc(dev, "Intel 21143 NWAY media interface");
13854134Swpaul
139160907Syongari	return (BUS_PROBE_DEFAULT);
14054134Swpaul}
14154134Swpaul
142105135Salfredstatic int
143150763Simpdcphy_attach(device_t dev)
14454134Swpaul{
14554134Swpaul	struct mii_softc *sc;
14654134Swpaul	struct dc_softc		*dc_sc;
147159201Sjhb	device_t brdev;
14854134Swpaul
14954134Swpaul	sc = device_get_softc(dev);
15054134Swpaul
151221407Smarius	mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE,
152221407Smarius	    &dcphy_funcs, 0);
15354134Swpaul
154221407Smarius	/*PHY_RESET(sc);*/
155221407Smarius	dc_sc = sc->mii_pdata->mii_ifp->if_softc;
15654134Swpaul	CSR_WRITE_4(dc_sc, DC_10BTSTAT, 0);
15754134Swpaul	CSR_WRITE_4(dc_sc, DC_10BTCTRL, 0);
15854134Swpaul
159159201Sjhb	brdev = device_get_parent(sc->mii_dev);
160159201Sjhb	switch (pci_get_subdevice(brdev) << 16 | pci_get_subvendor(brdev)) {
16154577Swpaul	case COMPAQ_PRESARIO_ID:
16254134Swpaul		/* Example of how to only allow 10Mbps modes. */
163183505Smarius		sc->mii_capabilities = BMSR_ANEG | BMSR_10TFDX | BMSR_10THDX;
16454134Swpaul		break;
16554134Swpaul	default:
166183505Smarius		if (dc_sc->dc_pmode == DC_PMODE_SIA)
16766681Swpaul			sc->mii_capabilities =
168183505Smarius			    BMSR_ANEG | BMSR_10TFDX | BMSR_10THDX;
169183505Smarius		else
17066681Swpaul			sc->mii_capabilities =
171183505Smarius			    BMSR_ANEG | BMSR_100TXFDX | BMSR_100TXHDX |
172183505Smarius			    BMSR_10TFDX | BMSR_10THDX;
17354134Swpaul		break;
17454134Swpaul	}
17554134Swpaul
176221407Smarius	sc->mii_capabilities &= sc->mii_capmask;
17754134Swpaul	device_printf(dev, " ");
178190117Smarius	mii_phy_add_media(sc);
17954134Swpaul	printf("\n");
18054134Swpaul
18154134Swpaul	MIIBUS_MEDIAINIT(sc->mii_dev);
182183505Smarius	return (0);
18354134Swpaul}
18454134Swpaul
18584145Sjlemonstatic int
186150763Simpdcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
18754134Swpaul{
18854134Swpaul	struct dc_softc		*dc_sc;
18954134Swpaul	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
19054134Swpaul	int reg;
19154134Swpaul	u_int32_t		mode;
19254134Swpaul
19354134Swpaul	dc_sc = mii->mii_ifp->if_softc;
19454134Swpaul
19554134Swpaul	switch (cmd) {
19654134Swpaul	case MII_POLLSTAT:
19754134Swpaul		break;
19854134Swpaul
19954134Swpaul	case MII_MEDIACHG:
20054134Swpaul		/*
20154134Swpaul		 * If the interface is not up, don't do anything.
20254134Swpaul		 */
20354134Swpaul		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
20454134Swpaul			break;
20554134Swpaul
20654134Swpaul		mii->mii_media_active = IFM_NONE;
20754134Swpaul		mode = CSR_READ_4(dc_sc, DC_NETCFG);
208183505Smarius		mode &= ~(DC_NETCFG_FULLDUPLEX | DC_NETCFG_PORTSEL |
209183505Smarius		    DC_NETCFG_PCS | DC_NETCFG_SCRAMBLER | DC_NETCFG_SPEEDSEL);
21054134Swpaul
21154134Swpaul		switch (IFM_SUBTYPE(ife->ifm_media)) {
21254134Swpaul		case IFM_AUTO:
213221407Smarius			/*PHY_RESET(sc);*/
214221407Smarius			(void)dcphy_auto(sc);
21554134Swpaul			break;
21654134Swpaul		case IFM_100_TX:
217221407Smarius			PHY_RESET(sc);
21854134Swpaul			DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
219183505Smarius			mode |= DC_NETCFG_PORTSEL | DC_NETCFG_PCS |
22054134Swpaul			    DC_NETCFG_SCRAMBLER;
221217417Smarius			if ((ife->ifm_media & IFM_FDX) != 0)
22254134Swpaul				mode |= DC_NETCFG_FULLDUPLEX;
22354134Swpaul			else
22454134Swpaul				mode &= ~DC_NETCFG_FULLDUPLEX;
22554134Swpaul			CSR_WRITE_4(dc_sc, DC_NETCFG, mode);
22654134Swpaul			break;
22754134Swpaul		case IFM_10_T:
22854134Swpaul			DC_CLRBIT(dc_sc, DC_SIARESET, DC_SIA_RESET);
22954134Swpaul			DC_CLRBIT(dc_sc, DC_10BTCTRL, 0xFFFF);
230217417Smarius			if ((ife->ifm_media & IFM_FDX) != 0)
23154134Swpaul				DC_SETBIT(dc_sc, DC_10BTCTRL, 0x7F3D);
23254134Swpaul			else
23354134Swpaul				DC_SETBIT(dc_sc, DC_10BTCTRL, 0x7F3F);
23454134Swpaul			DC_SETBIT(dc_sc, DC_SIARESET, DC_SIA_RESET);
23554134Swpaul			DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
23654134Swpaul			mode &= ~DC_NETCFG_PORTSEL;
23754134Swpaul			mode |= DC_NETCFG_SPEEDSEL;
238217417Smarius			if ((ife->ifm_media & IFM_FDX) != 0)
23954134Swpaul				mode |= DC_NETCFG_FULLDUPLEX;
24054134Swpaul			else
24154134Swpaul				mode &= ~DC_NETCFG_FULLDUPLEX;
24254134Swpaul			CSR_WRITE_4(dc_sc, DC_NETCFG, mode);
24354134Swpaul			break;
24454134Swpaul		default:
245183505Smarius			return (EINVAL);
24654134Swpaul		}
24754134Swpaul		break;
24854134Swpaul
24954134Swpaul	case MII_TICK:
25054134Swpaul		/*
25184145Sjlemon		 * Is the interface even up?
25254134Swpaul		 */
25384145Sjlemon		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
25454134Swpaul			return (0);
25554134Swpaul
25654134Swpaul		/*
25784145Sjlemon		 * Only used for autonegotiation.
25854134Swpaul		 */
25984145Sjlemon		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
26084145Sjlemon			break;
26154134Swpaul
26294994Smckay		reg = CSR_READ_4(dc_sc, DC_10BTSTAT);
26361110Swpaul		if (!(reg & DC_TSTAT_LS10) || !(reg & DC_TSTAT_LS100))
26484145Sjlemon			break;
26554134Swpaul
26661110Swpaul                /*
26761110Swpaul                 * Only retry autonegotiation every 5 seconds.
26894994Smckay		 *
26994994Smckay		 * Otherwise, fall through to calling dcphy_status()
27094994Smckay		 * since real Intel 21143 chips don't show valid link
27194994Smckay		 * status until autonegotiation is switched off, and
27294994Smckay		 * that only happens in dcphy_status().  Without this,
273129845Smarius		 * successful autonegotiation is never recognised on
27494994Smckay		 * these chips.
27561110Swpaul                 */
276128870Sandre                if (++sc->mii_ticks <= 50)
27794994Smckay			break;
27861110Swpaul
27954134Swpaul		sc->mii_ticks = 0;
28096026Sphk		dcphy_auto(sc);
28154134Swpaul
28254134Swpaul		break;
28354134Swpaul	}
28454134Swpaul
28554134Swpaul	/* Update the media status. */
286221407Smarius	PHY_STATUS(sc);
28754134Swpaul
28854134Swpaul	/* Callback if something changed. */
28984145Sjlemon	mii_phy_update(sc, cmd);
29054134Swpaul	return (0);
29154134Swpaul}
29254134Swpaul
29384145Sjlemonstatic void
294150763Simpdcphy_status(struct mii_softc *sc)
29554134Swpaul{
29654134Swpaul	struct mii_data *mii = sc->mii_pdata;
297227686Smarius	int anlpar, tstat;
29854134Swpaul	struct dc_softc		*dc_sc;
29954134Swpaul
30054134Swpaul	dc_sc = mii->mii_ifp->if_softc;
30154134Swpaul
30254134Swpaul	mii->mii_media_status = IFM_AVALID;
30354134Swpaul	mii->mii_media_active = IFM_ETHER;
30454134Swpaul
30561290Swpaul	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
30661290Swpaul		return;
30761290Swpaul
308227686Smarius	tstat = CSR_READ_4(dc_sc, DC_10BTSTAT);
309227686Smarius	if (!(tstat & DC_TSTAT_LS10) || !(tstat & DC_TSTAT_LS100))
31054134Swpaul		mii->mii_media_status |= IFM_ACTIVE;
31154134Swpaul
31261110Swpaul	if (CSR_READ_4(dc_sc, DC_10BTCTRL) & DC_TCTL_AUTONEGENBL) {
31354134Swpaul		/* Erg, still trying, I guess... */
31461110Swpaul		if ((tstat & DC_TSTAT_ANEGSTAT) != DC_ASTAT_AUTONEGCMP) {
31561110Swpaul			if ((DC_IS_MACRONIX(dc_sc) || DC_IS_PNICII(dc_sc)) &&
31661110Swpaul			    (tstat & DC_TSTAT_ANEGSTAT) == DC_ASTAT_DISABLE)
31761110Swpaul				goto skip;
31854134Swpaul			mii->mii_media_active |= IFM_NONE;
31954134Swpaul			return;
32054134Swpaul		}
32154134Swpaul
32261110Swpaul		if (tstat & DC_TSTAT_LP_CAN_NWAY) {
32361110Swpaul			anlpar = tstat >> 16;
324173665Syongari			if (anlpar & ANLPAR_TX_FD &&
32561110Swpaul			    sc->mii_capabilities & BMSR_100TXFDX)
326183505Smarius				mii->mii_media_active |= IFM_100_TX | IFM_FDX;
327173665Syongari			else if (anlpar & ANLPAR_T4 &&
328173665Syongari			    sc->mii_capabilities & BMSR_100T4)
329213384Smarius				mii->mii_media_active |= IFM_100_T4 | IFM_HDX;
33054577Swpaul			else if (anlpar & ANLPAR_TX &&
33154577Swpaul			    sc->mii_capabilities & BMSR_100TXHDX)
332213384Smarius				mii->mii_media_active |= IFM_100_TX | IFM_HDX;
33354134Swpaul			else if (anlpar & ANLPAR_10_FD)
334183505Smarius				mii->mii_media_active |= IFM_10_T | IFM_FDX;
33554134Swpaul			else if (anlpar & ANLPAR_10)
336213384Smarius				mii->mii_media_active |= IFM_10_T | IFM_HDX;
33754134Swpaul			else
33854134Swpaul				mii->mii_media_active |= IFM_NONE;
33954134Swpaul			if (DC_IS_INTEL(dc_sc))
34054134Swpaul				DC_CLRBIT(dc_sc, DC_10BTCTRL,
34154134Swpaul				    DC_TCTL_AUTONEGENBL);
34254134Swpaul			return;
34354134Swpaul		}
344183505Smarius
34554134Swpaul		/*
34654134Swpaul		 * If the other side doesn't support NWAY, then the
34754134Swpaul		 * best we can do is determine if we have a 10Mbps or
348183505Smarius		 * 100Mbps link.  There's no way to know if the link
34954134Swpaul		 * is full or half duplex, so we default to half duplex
35054134Swpaul		 * and hope that the user is clever enough to manually
35154134Swpaul		 * change the media settings if we're wrong.
35254134Swpaul		 */
353227686Smarius		if (!(tstat & DC_TSTAT_LS100))
354213384Smarius			mii->mii_media_active |= IFM_100_TX | IFM_HDX;
355227686Smarius		else if (!(tstat & DC_TSTAT_LS10))
356213384Smarius			mii->mii_media_active |= IFM_10_T | IFM_HDX;
35754134Swpaul		else
35854134Swpaul			mii->mii_media_active |= IFM_NONE;
35954134Swpaul		if (DC_IS_INTEL(dc_sc))
36054134Swpaul			DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
36154134Swpaul		return;
36254134Swpaul	}
36354134Swpaul
36461110Swpaulskip:
36566681Swpaul	if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_SPEEDSEL)
36666681Swpaul		mii->mii_media_active |= IFM_10_T;
36766681Swpaul	else
36854134Swpaul		mii->mii_media_active |= IFM_100_TX;
36954134Swpaul	if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_FULLDUPLEX)
37054134Swpaul		mii->mii_media_active |= IFM_FDX;
371213384Smarius	else
372213384Smarius		mii->mii_media_active |= IFM_HDX;
37354134Swpaul}
37454134Swpaul
37554134Swpaulstatic int
376150763Simpdcphy_auto(struct mii_softc *mii)
37754134Swpaul{
37854134Swpaul	struct dc_softc		*sc;
37954134Swpaul
38054134Swpaul	sc = mii->mii_pdata->mii_ifp->if_softc;
38154134Swpaul
38296026Sphk	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
38396026Sphk	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
38496026Sphk	DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
38596026Sphk	if (mii->mii_capabilities & BMSR_100TXHDX)
38696026Sphk		CSR_WRITE_4(sc, DC_10BTCTRL, 0x3FFFF);
38796026Sphk	else
38896026Sphk		CSR_WRITE_4(sc, DC_10BTCTRL, 0xFFFF);
38996026Sphk	DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
39096026Sphk	DC_SETBIT(sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
39196026Sphk	DC_SETBIT(sc, DC_10BTSTAT, DC_ASTAT_TXDISABLE);
39254134Swpaul
393183505Smarius	return (EJUSTRETURN);
39454134Swpaul}
39554134Swpaul
39654134Swpaulstatic void
397150763Simpdcphy_reset(struct mii_softc *mii)
39854134Swpaul{
39954134Swpaul	struct dc_softc		*sc;
40054134Swpaul
40154134Swpaul	sc = mii->mii_pdata->mii_ifp->if_softc;
40254134Swpaul
40354134Swpaul	DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
40454134Swpaul	DELAY(1000);
40554134Swpaul	DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
40654134Swpaul}
407