xref: /freebsd/sys/dev/mii/icsphy.c (revision 4d846d26)
1 /*	$NetBSD: icsphy.c,v 1.41 2006/11/16 21:24:07 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
11  * NASA Ames Research Center.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57  */
58 
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
61 
62 /*
63  * driver for Integrated Circuit Systems' ICS1889-1893 ethernet 10/100 PHY
64  * datasheet from www.icst.com
65  */
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/module.h>
71 #include <sys/socket.h>
72 #include <sys/bus.h>
73 
74 #include <net/if.h>
75 #include <net/if_media.h>
76 
77 #include <dev/mii/mii.h>
78 #include <dev/mii/miivar.h>
79 #include "miidevs.h"
80 
81 #include <dev/mii/icsphyreg.h>
82 
83 #include "miibus_if.h"
84 
85 static int	icsphy_probe(device_t dev);
86 static int	icsphy_attach(device_t dev);
87 
88 static device_method_t icsphy_methods[] = {
89 	/* device interface */
90 	DEVMETHOD(device_probe,		icsphy_probe),
91 	DEVMETHOD(device_attach,	icsphy_attach),
92 	DEVMETHOD(device_detach,	mii_phy_detach),
93 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
94 	DEVMETHOD_END
95 };
96 
97 static driver_t icsphy_driver = {
98 	"icsphy",
99 	icsphy_methods,
100 	sizeof(struct mii_softc)
101 };
102 
103 DRIVER_MODULE(icsphy, miibus, icsphy_driver, 0, 0);
104 
105 static int	icsphy_service(struct mii_softc *, struct mii_data *, int);
106 static void	icsphy_status(struct mii_softc *);
107 static void	icsphy_reset(struct mii_softc *);
108 
109 static const struct mii_phydesc icsphys[] = {
110 	MII_PHY_DESC(ICS, 1889),
111 	MII_PHY_DESC(ICS, 1890),
112 	MII_PHY_DESC(ICS, 1892),
113 	MII_PHY_DESC(ICS, 1893),
114 	MII_PHY_DESC(ICS, 1893C),
115 	MII_PHY_END
116 };
117 
118 static const struct mii_phy_funcs icsphy_funcs = {
119 	icsphy_service,
120 	icsphy_status,
121 	icsphy_reset
122 };
123 
124 static int
125 icsphy_probe(device_t dev)
126 {
127 
128 	return (mii_phy_dev_probe(dev, icsphys, BUS_PROBE_DEFAULT));
129 }
130 
131 static int
132 icsphy_attach(device_t dev)
133 {
134 
135 	mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE,
136 	    &icsphy_funcs, 1);
137 	return (0);
138 }
139 
140 static int
141 icsphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
142 {
143 
144 	switch (cmd) {
145 	case MII_POLLSTAT:
146 		break;
147 
148 	case MII_MEDIACHG:
149 		mii_phy_setmedia(sc);
150 		break;
151 
152 	case MII_TICK:
153 		if (mii_phy_tick(sc) == EJUSTRETURN)
154 			return (0);
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 icsphy_status(struct mii_softc *sc)
168 {
169 	struct mii_data *mii = sc->mii_pdata;
170 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
171 	int bmcr, qpr;
172 
173 	mii->mii_media_status = IFM_AVALID;
174 	mii->mii_media_active = IFM_ETHER;
175 
176 	/*
177 	 * Don't get link from the BMSR.  It's available in the QPR,
178 	 * and we have to read it twice to unlatch it anyhow.  This
179 	 * gives us fewer register reads.
180 	 */
181 	qpr = PHY_READ(sc, MII_ICSPHY_QPR);		/* unlatch */
182 	qpr = PHY_READ(sc, MII_ICSPHY_QPR);		/* real value */
183 
184 	if (qpr & QPR_LINK)
185 		mii->mii_media_status |= IFM_ACTIVE;
186 
187 	bmcr = PHY_READ(sc, MII_BMCR);
188 	if (bmcr & BMCR_ISO) {
189 		mii->mii_media_active |= IFM_NONE;
190 		mii->mii_media_status = 0;
191 		return;
192 	}
193 
194 	if (bmcr & BMCR_LOOP)
195 		mii->mii_media_active |= IFM_LOOP;
196 
197 	if (bmcr & BMCR_AUTOEN) {
198 		if ((qpr & QPR_ACOMP) == 0) {
199 			/* Erg, still trying, I guess... */
200 			mii->mii_media_active |= IFM_NONE;
201 			return;
202 		}
203 		if (qpr & QPR_SPEED)
204 			mii->mii_media_active |= IFM_100_TX;
205 		else
206 			mii->mii_media_active |= IFM_10_T;
207 		if (qpr & QPR_FDX)
208 			mii->mii_media_active |=
209 			    IFM_FDX | mii_phy_flowstatus(sc);
210 		else
211 			mii->mii_media_active |= IFM_HDX;
212 	} else
213 		mii->mii_media_active = ife->ifm_media;
214 }
215 
216 static void
217 icsphy_reset(struct mii_softc *sc)
218 {
219 
220 	mii_phy_reset(sc);
221 	/* set powerdown feature */
222 	switch (sc->mii_mpd_model) {
223 		case MII_MODEL_ICS_1890:
224 		case MII_MODEL_ICS_1893:
225 			PHY_WRITE(sc, MII_ICSPHY_ECR2, ECR2_100AUTOPWRDN);
226 			break;
227 		case MII_MODEL_ICS_1892:
228 			PHY_WRITE(sc, MII_ICSPHY_ECR2,
229 			    ECR2_10AUTOPWRDN|ECR2_100AUTOPWRDN);
230 			break;
231 		default:
232 			/* 1889 have no ECR2 */
233 			break;
234 	}
235 	/*
236 	 * There is no description that the reset do auto-negotiation in the
237 	 * data sheet.
238 	 */
239 	PHY_WRITE(sc, MII_BMCR, BMCR_S100|BMCR_STARTNEG|BMCR_FDX);
240 }
241