xref: /freebsd/sys/dev/mii/icsphy.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*	$NetBSD: icsphy.c,v 1.41 2006/11/16 21:24:07 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
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 /*
61  * driver for Integrated Circuit Systems' ICS1889-1893 ethernet 10/100 PHY
62  * datasheet from www.icst.com
63  */
64 
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/module.h>
69 #include <sys/socket.h>
70 #include <sys/bus.h>
71 
72 #include <net/if.h>
73 #include <net/if_media.h>
74 
75 #include <dev/mii/mii.h>
76 #include <dev/mii/miivar.h>
77 #include "miidevs.h"
78 
79 #include <dev/mii/icsphyreg.h>
80 
81 #include "miibus_if.h"
82 
83 static int	icsphy_probe(device_t dev);
84 static int	icsphy_attach(device_t dev);
85 
86 static device_method_t icsphy_methods[] = {
87 	/* device interface */
88 	DEVMETHOD(device_probe,		icsphy_probe),
89 	DEVMETHOD(device_attach,	icsphy_attach),
90 	DEVMETHOD(device_detach,	mii_phy_detach),
91 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
92 	DEVMETHOD_END
93 };
94 
95 static driver_t icsphy_driver = {
96 	"icsphy",
97 	icsphy_methods,
98 	sizeof(struct mii_softc)
99 };
100 
101 DRIVER_MODULE(icsphy, miibus, icsphy_driver, 0, 0);
102 
103 static int	icsphy_service(struct mii_softc *, struct mii_data *, int);
104 static void	icsphy_status(struct mii_softc *);
105 static void	icsphy_reset(struct mii_softc *);
106 
107 static const struct mii_phydesc icsphys[] = {
108 	MII_PHY_DESC(ICS, 1889),
109 	MII_PHY_DESC(ICS, 1890),
110 	MII_PHY_DESC(ICS, 1892),
111 	MII_PHY_DESC(ICS, 1893),
112 	MII_PHY_DESC(ICS, 1893C),
113 	MII_PHY_END
114 };
115 
116 static const struct mii_phy_funcs icsphy_funcs = {
117 	icsphy_service,
118 	icsphy_status,
119 	icsphy_reset
120 };
121 
122 static int
123 icsphy_probe(device_t dev)
124 {
125 
126 	return (mii_phy_dev_probe(dev, icsphys, BUS_PROBE_DEFAULT));
127 }
128 
129 static int
130 icsphy_attach(device_t dev)
131 {
132 
133 	mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE,
134 	    &icsphy_funcs, 1);
135 	return (0);
136 }
137 
138 static int
139 icsphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
140 {
141 
142 	switch (cmd) {
143 	case MII_POLLSTAT:
144 		break;
145 
146 	case MII_MEDIACHG:
147 		mii_phy_setmedia(sc);
148 		break;
149 
150 	case MII_TICK:
151 		if (mii_phy_tick(sc) == EJUSTRETURN)
152 			return (0);
153 		break;
154 	}
155 
156 	/* Update the media status. */
157 	PHY_STATUS(sc);
158 
159 	/* Callback if something changed. */
160 	mii_phy_update(sc, cmd);
161 	return (0);
162 }
163 
164 static void
165 icsphy_status(struct mii_softc *sc)
166 {
167 	struct mii_data *mii = sc->mii_pdata;
168 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
169 	int bmcr, qpr;
170 
171 	mii->mii_media_status = IFM_AVALID;
172 	mii->mii_media_active = IFM_ETHER;
173 
174 	/*
175 	 * Don't get link from the BMSR.  It's available in the QPR,
176 	 * and we have to read it twice to unlatch it anyhow.  This
177 	 * gives us fewer register reads.
178 	 */
179 	qpr = PHY_READ(sc, MII_ICSPHY_QPR);		/* unlatch */
180 	qpr = PHY_READ(sc, MII_ICSPHY_QPR);		/* real value */
181 
182 	if (qpr & QPR_LINK)
183 		mii->mii_media_status |= IFM_ACTIVE;
184 
185 	bmcr = PHY_READ(sc, MII_BMCR);
186 	if (bmcr & BMCR_ISO) {
187 		mii->mii_media_active |= IFM_NONE;
188 		mii->mii_media_status = 0;
189 		return;
190 	}
191 
192 	if (bmcr & BMCR_LOOP)
193 		mii->mii_media_active |= IFM_LOOP;
194 
195 	if (bmcr & BMCR_AUTOEN) {
196 		if ((qpr & QPR_ACOMP) == 0) {
197 			/* Erg, still trying, I guess... */
198 			mii->mii_media_active |= IFM_NONE;
199 			return;
200 		}
201 		if (qpr & QPR_SPEED)
202 			mii->mii_media_active |= IFM_100_TX;
203 		else
204 			mii->mii_media_active |= IFM_10_T;
205 		if (qpr & QPR_FDX)
206 			mii->mii_media_active |=
207 			    IFM_FDX | mii_phy_flowstatus(sc);
208 		else
209 			mii->mii_media_active |= IFM_HDX;
210 	} else
211 		mii->mii_media_active = ife->ifm_media;
212 }
213 
214 static void
215 icsphy_reset(struct mii_softc *sc)
216 {
217 
218 	mii_phy_reset(sc);
219 	/* set powerdown feature */
220 	switch (sc->mii_mpd_model) {
221 		case MII_MODEL_ICS_1890:
222 		case MII_MODEL_ICS_1893:
223 			PHY_WRITE(sc, MII_ICSPHY_ECR2, ECR2_100AUTOPWRDN);
224 			break;
225 		case MII_MODEL_ICS_1892:
226 			PHY_WRITE(sc, MII_ICSPHY_ECR2,
227 			    ECR2_10AUTOPWRDN|ECR2_100AUTOPWRDN);
228 			break;
229 		default:
230 			/* 1889 have no ECR2 */
231 			break;
232 	}
233 	/*
234 	 * There is no description that the reset do auto-negotiation in the
235 	 * data sheet.
236 	 */
237 	PHY_WRITE(sc, MII_BMCR, BMCR_S100|BMCR_STARTNEG|BMCR_FDX);
238 }
239