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