xref: /freebsd/sys/dev/mii/qsphy.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1 /*	OpenBSD: qsphy.c,v 1.6 2000/08/26 20:04:18 nate Exp */
2 /*	NetBSD: qsphy.c,v 1.19 2000/02/02 23:34:57 thorpej Exp 	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-2-Clause-NetBSD AND BSD-2-Clause
6  *
7  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
12  * NASA Ames Research Center.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*-
37  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
52  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58  */
59 
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
62 
63 /*
64  * driver for Quality Semiconductor's QS6612 ethernet 10/100 PHY
65  * datasheet from www.qualitysemi.com
66  */
67 
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/kernel.h>
71 #include <sys/socket.h>
72 #include <sys/errno.h>
73 #include <sys/module.h>
74 #include <sys/bus.h>
75 
76 #include <net/if.h>
77 #include <net/if_media.h>
78 
79 #include <dev/mii/mii.h>
80 #include <dev/mii/miivar.h>
81 #include "miidevs.h"
82 
83 #include <dev/mii/qsphyreg.h>
84 
85 #include "miibus_if.h"
86 
87 static int qsphy_probe(device_t);
88 static int qsphy_attach(device_t);
89 
90 static device_method_t qsphy_methods[] = {
91 	/* device interface */
92 	DEVMETHOD(device_probe,		qsphy_probe),
93 	DEVMETHOD(device_attach,	qsphy_attach),
94 	DEVMETHOD(device_detach,	mii_phy_detach),
95 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
96 	DEVMETHOD_END
97 };
98 
99 static devclass_t qsphy_devclass;
100 
101 static driver_t qsphy_driver = {
102 	"qsphy",
103 	qsphy_methods,
104 	sizeof(struct mii_softc)
105 };
106 
107 DRIVER_MODULE(qsphy, miibus, qsphy_driver, qsphy_devclass, 0, 0);
108 
109 static int	qsphy_service(struct mii_softc *, struct mii_data *, int);
110 static void	qsphy_reset(struct mii_softc *);
111 static void	qsphy_status(struct mii_softc *);
112 
113 static const struct mii_phydesc qsphys[] = {
114 	MII_PHY_DESC(xxQUALSEMI, QS6612),
115 	MII_PHY_END
116 };
117 
118 static const struct mii_phy_funcs qsphy_funcs = {
119 	qsphy_service,
120 	qsphy_status,
121 	qsphy_reset
122 };
123 
124 static int
125 qsphy_probe(device_t dev)
126 {
127 
128 	return (mii_phy_dev_probe(dev, qsphys, BUS_PROBE_DEFAULT));
129 }
130 
131 static int
132 qsphy_attach(device_t dev)
133 {
134 
135 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &qsphy_funcs, 1);
136 	return (0);
137 }
138 
139 static int
140 qsphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
141 {
142 
143 	switch (cmd) {
144 	case MII_POLLSTAT:
145 		break;
146 
147 	case MII_MEDIACHG:
148 		mii_phy_setmedia(sc);
149 		break;
150 
151 	case MII_TICK:
152 		/*
153 		 * This PHY's autonegotiation doesn't need to be kicked.
154 		 */
155 		break;
156 	}
157 
158 	/* Update the media status. */
159 	PHY_STATUS(sc);
160 
161 	/* Callback if something changed. */
162 	mii_phy_update(sc, cmd);
163 	return (0);
164 }
165 
166 static void
167 qsphy_status(struct mii_softc *sc)
168 {
169 	struct mii_data *mii = sc->mii_pdata;
170 	int bmsr, bmcr, pctl;
171 
172 	mii->mii_media_status = IFM_AVALID;
173 	mii->mii_media_active = IFM_ETHER;
174 
175 	bmsr = PHY_READ(sc, MII_BMSR) |
176 	    PHY_READ(sc, MII_BMSR);
177 	if (bmsr & BMSR_LINK)
178 		mii->mii_media_status |= IFM_ACTIVE;
179 
180 	bmcr = PHY_READ(sc, MII_BMCR);
181 	if (bmcr & BMCR_ISO) {
182 		mii->mii_media_active |= IFM_NONE;
183 		mii->mii_media_status = 0;
184 		return;
185 	}
186 
187 	if (bmcr & BMCR_LOOP)
188 		mii->mii_media_active |= IFM_LOOP;
189 
190 	pctl = PHY_READ(sc, MII_QSPHY_PCTL);
191 	switch (pctl & PCTL_OPMASK) {
192 	case PCTL_10_T:
193 		mii->mii_media_active |= IFM_10_T|IFM_HDX;
194 		break;
195 	case PCTL_10_T_FDX:
196 		mii->mii_media_active |= IFM_10_T|IFM_FDX;
197 		break;
198 	case PCTL_100_TX:
199 		mii->mii_media_active |= IFM_100_TX|IFM_HDX;
200 		break;
201 	case PCTL_100_TX_FDX:
202 		mii->mii_media_active |= IFM_100_TX|IFM_FDX;
203 		break;
204 	case PCTL_100_T4:
205 		mii->mii_media_active |= IFM_100_T4|IFM_HDX;
206 		break;
207 	case PCTL_AN:
208 		mii->mii_media_active |= IFM_NONE;
209 		break;
210 	default:
211 		/* Erg... this shouldn't happen. */
212 		mii->mii_media_active |= IFM_NONE;
213 		break;
214 	}
215 	if ((mii->mii_media_active & IFM_FDX) != 0)
216 		mii->mii_media_active |= mii_phy_flowstatus(sc);
217 }
218 
219 static void
220 qsphy_reset(struct mii_softc *sc)
221 {
222 
223 	mii_phy_reset(sc);
224 	PHY_WRITE(sc, MII_QSPHY_IMASK, 0);
225 }
226