tlphy.c revision 1.3 1 /* $NetBSD: tlphy.c,v 1.3 1997/11/16 22:38:34 christos Exp $ */
2
3 /*
4 * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Manuel Bouyer.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /* Driver for Texas Instruments's ThunderLAN internal PHY */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38 #include <sys/malloc.h>
39 #include <sys/proc.h>
40 #include <sys/socket.h>
41 #include <net/if.h>
42 #include <net/if_media.h>
43
44
45 #include <dev/mii/mii_adapter.h>
46 #include <dev/mii/mii_adapters_id.h>
47 #include <dev/mii/mii_phy.h>
48 #include <dev/mii/generic_phy.h>
49 #include <dev/mii/tlphy.h>
50
51 static int tlphy_up __P((struct phy_softc *sc));
52 void tlphy_pdown __P((void *v));
53 int tlphy_media_set __P((int, void *));
54 int tlphy_media_set_aui __P((struct phy_softc *));
55 int tlphy_status __P((int, void*));
56
57 #ifdef __BROKEN_INDIRECT_CONFIG
58 int tlphymatch __P((struct device *, void *, void *));
59 #else
60 int tlphymatch __P((struct device *, struct cfdata *, void *));
61 #endif
62 void tlphyattach __P((struct device *, struct device *, void *));
63
64 struct cfattach tlphy_ca = {
65 sizeof(struct phy_softc), tlphymatch, tlphyattach
66 };
67
68 struct cfdriver tlphy_cd = {
69 NULL, "tlphy", DV_IFNET
70 };
71
72 int
73 tlphymatch(parent, match, aux)
74 struct device *parent;
75 #ifdef __BROKEN_INDIRECT_CONFIG
76 void *match;
77 #else
78 struct cfdata *match;
79 #endif
80 void *aux;
81 {
82 mii_phy_t *phy = aux;
83
84 if (phy->phy_id == 0x40005013 || phy->phy_id == 0x40005014 ||
85 phy->phy_id == 0x40005015)
86 return 1;
87 return 0;
88 }
89
90 void
91 tlphyattach(parent, self, aux)
92 struct device *parent, *self;
93 void *aux;
94 {
95 struct phy_softc *sc = (struct phy_softc *)self;
96
97 sc->phy_link = aux;
98 sc->phy_link->phy_softc = sc;
99 sc->phy_link->phy_media_set = tlphy_media_set;
100 sc->phy_link->phy_status = tlphy_status;
101 sc->phy_link->phy_pdown = tlphy_pdown;
102
103 phy_reset(sc);
104
105 switch (sc->phy_link->adapter_id) {
106 case COMPAQ_INT_NETLIGENT_10_100:
107 sc->phy_link->phy_media = PHY_BNC;
108 break;
109 case COMPAQ_NETLIGENT_10_100:
110 sc->phy_link->phy_media = 0;
111 break;
112 case COMPAQ_INT_NETFLEX:
113 case COMPAQ_NETFLEX_BNC:
114 sc->phy_link->phy_media = PHY_BNC | PHY_10baseT;
115 break;
116 case TI_TLAN:
117 sc->phy_link->phy_media = PHY_10baseT;
118 break;
119 default:
120 sc->phy_link->phy_media = PHY_AUI;
121 if (phy_media_probe(sc) != 0) {
122 printf(" (autoconfig failed)");
123 break;
124 }
125 }
126
127 if(sc->phy_link->phy_media) {
128 printf(": ");
129 phy_media_print(sc->phy_link->phy_media);
130 }
131 printf("\n");
132 }
133
134 void tlphy_pdown(v)
135 void *v;
136 {
137 struct phy_softc *sc = v;
138 mii_phy_t *phy_link = sc->phy_link;
139 mii_writereg(phy_link->mii_softc, phy_link->dev, PHY_CONTROL,
140 CTRL_ISO | CTRL_PDOWN);
141 }
142
143 static int tlphy_up(sc)
144 struct phy_softc *sc;
145 {
146 int s;
147 mii_phy_t *phy_link = sc->phy_link;
148 int control, i;
149 s = splnet();
150
151 /* set control registers to a reasonable value, wait for power_up */
152 mii_writereg(phy_link->mii_softc, phy_link->dev, PHY_CONTROL,
153 CTRL_LOOPBK);
154 mii_writereg(phy_link->mii_softc, phy_link->dev, PHY_TL_CTRL,
155 0);
156 i = 0;
157 do {
158 DELAY(100000);
159 control = mii_readreg(phy_link->mii_softc, phy_link->dev,
160 PHY_TL_ST);
161 } while ((control < 0 || (control & TL_ST_PHOK) == 0) && ++i < 10);
162 if (control < 0 || (control & TL_ST_PHOK) == 0) {
163 splx(s);
164 printf("%s: power-up failed\n", sc->sc_dev.dv_xname);
165 return 0;
166 }
167 if ( phy_reset(sc) == 0) {
168 splx(s);
169 return 0;
170 }
171 splx(s);
172 return 1;
173 }
174
175 int tlphy_media_set(media, v)
176 int media;
177 void *v;
178 {
179 struct phy_softc *sc = v;
180 int subtype;
181
182 if (IFM_TYPE(media) != IFM_ETHER)
183 return EINVAL;
184
185 if (tlphy_up(sc) == 0)
186 return EIO;
187
188 subtype = IFM_SUBTYPE(media);
189 switch (subtype) {
190 case IFM_10_2:
191 case IFM_10_5:
192 if ((subtype == IFM_10_2 && (sc->phy_link->phy_media & PHY_BNC)) ||
193 (subtype == IFM_10_5 && (sc->phy_link->phy_media & PHY_AUI)))
194 return tlphy_media_set_aui(sc);
195 else
196 return EINVAL;
197 case IFM_10_T:
198 return phy_media_set_10_100(sc, media);
199 case IFM_AUTO:
200 return ENODEV;
201 default:
202 return EINVAL;
203 }
204 }
205
206 int
207 tlphy_media_set_aui(sc)
208 struct phy_softc *sc;
209 {
210 mii_phy_t *phy_link = sc->phy_link;
211
212 mii_writereg(phy_link->mii_softc, phy_link->dev,
213 PHY_CONTROL, 0);
214 mii_writereg(phy_link->mii_softc, phy_link->dev,
215 PHY_TL_CTRL, TL_CTRL_AUISEL);
216 DELAY(100000);
217 return 0;
218 }
219
220 int tlphy_status(media, v)
221 int media;
222 void *v;
223 {
224 struct phy_softc *sc = v;
225 int reg;
226
227 if (IFM_SUBTYPE(media) == IFM_10_T)
228 return phy_status(media, sc);
229 reg = mii_readreg(sc->phy_link->mii_softc, sc->phy_link->dev,
230 PHY_TL_ST);
231 if ((reg & PHY_TL_ST) == 0)
232 return ENETDOWN;
233 return 0;
234 }
235