xref: /openbsd-src/sys/dev/mii/rlphy.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: rlphy.c,v 1.6 2000/08/26 20:04:18 nate Exp $	*/
2 
3 /*
4  * Copyright (c) 1998, 1999 Jason L. Wright (jason@thought.net)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Jason L. Wright
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Driver for the internal PHY found on RTL8139 based nics, based
36  * on drivers for the 'exphy' (Internal 3Com phys) and 'nsphy'
37  * (National Semiconductor DP83840).
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/errno.h>
47 
48 #include <net/if.h>
49 #include <net/if_media.h>
50 
51 #include <dev/mii/mii.h>
52 #include <dev/mii/miivar.h>
53 #include <dev/mii/miidevs.h>
54 
55 int	rlphymatch __P((struct device *, void *, void *));
56 void	rlphyattach __P((struct device *, struct device *, void *));
57 
58 struct cfattach rlphy_ca = {
59 	sizeof(struct mii_softc), rlphymatch, rlphyattach, mii_phy_detach,
60 	    mii_phy_activate
61 };
62 
63 struct cfdriver rlphy_cd = {
64 	NULL, "rlphy", DV_DULL
65 };
66 
67 int	rlphy_service __P((struct mii_softc *, struct mii_data *, int));
68 void	rlphy_reset __P((struct mii_softc *));
69 
70 int
71 rlphymatch(parent, match, aux)
72 	struct device *parent;
73 	void *match;
74 	void *aux;
75 {
76 	struct mii_attach_args *ma = aux;
77 
78 	if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 ||
79 	    MII_MODEL(ma->mii_id2) != 0)
80 		return (0);
81 
82 	if (strcmp(parent->dv_cfdata->cf_driver->cd_name, "rl") != 0)
83 		return (0);
84 
85 	/*
86 	 * A "real" phy should get preference, but on the 8139 there
87 	 * is no phyid register.
88 	 */
89 	return (5);
90 }
91 
92 void
93 rlphyattach(parent, self, aux)
94 	struct device *parent, *self;
95 	void *aux;
96 {
97 	struct mii_softc *sc = (struct mii_softc *)self;
98 	struct mii_attach_args *ma = aux;
99 	struct mii_data *mii = ma->mii_data;
100 
101 	printf(": RTL internal phy\n");
102 
103 	sc->mii_inst = mii->mii_instance;
104 	sc->mii_phy = ma->mii_phyno;
105 	sc->mii_service = rlphy_service;
106 	sc->mii_status = ukphy_status;
107 	sc->mii_pdata = mii;
108 	sc->mii_flags = mii->mii_flags;
109 
110 	rlphy_reset(sc);
111 
112 	sc->mii_capabilities =
113 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
114 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
115 		mii_phy_add_media(sc);
116 }
117 
118 int
119 rlphy_service(sc, mii, cmd)
120 	struct mii_softc *sc;
121 	struct mii_data *mii;
122 	int cmd;
123 {
124 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
125 
126 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
127 		return (ENXIO);
128 
129 	/*
130 	 * Can't isolate the RTL8139 phy, so it has to be the only one.
131 	 */
132 	if (IFM_INST(ife->ifm_media) != sc->mii_inst)
133 		panic("rlphy_service: attempt to isolate phy");
134 
135 	switch (cmd) {
136 	case MII_POLLSTAT:
137 		break;
138 
139 	case MII_MEDIACHG:
140 		/*
141 		 * If the interface is not up, don't do anything.
142 		 */
143 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
144 			break;
145 
146 		mii_phy_setmedia(sc);
147 		break;
148 
149 	case MII_TICK:
150 		if (mii_phy_tick(sc) == EJUSTRETURN)
151 			return (0);
152 		break;
153 
154 	case MII_DOWN:
155 		mii_phy_down(sc);
156 		return (0);
157 	}
158 
159 	/* Update the media status. */
160 	mii_phy_status(sc);
161 
162 	/* Callback if something changed. */
163 	mii_phy_update(sc, cmd);
164 	return (0);
165 }
166 
167 void
168 rlphy_reset(sc)
169 	struct mii_softc *sc;
170 {
171 	int bmcr;
172 
173 	/*
174 	 * XXX The RTL8139 doesn't set the BMCR properly
175 	 * XXX after reset, which breaks autoneg.
176 	 */
177 
178 	bmcr = PHY_READ(sc, MII_BMCR);
179 	mii_phy_reset(sc);
180 	PHY_WRITE(sc, MII_BMCR, bmcr);
181 }
182