tlphy.c revision 1.1 1 /* $NetBSD: tlphy.c,v 1.1 1997/10/17 17:34:05 bouyer 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 thunderland 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 == 0x40005014 || phy->phy_id == 0x40005015)
85 return 1;
86 return 0;
87 }
88
89 void
90 tlphyattach(parent, self, aux)
91 struct device *parent, *self;
92 void *aux;
93 {
94 struct phy_softc *sc = (struct phy_softc *)self;
95
96 sc->phy_link = aux;
97 sc->phy_link->phy_softc = sc;
98 sc->phy_link->phy_media_set = tlphy_media_set;
99 sc->phy_link->phy_status = tlphy_status;
100 sc->phy_link->phy_pdown = tlphy_pdown;
101
102 phy_reset(sc);
103
104 switch (sc->phy_link->adapter_id) {
105 case COMPAQ_INT_NETLIGENT_10_100:
106 sc->phy_link->phy_media = PHY_BNC;
107 break;
108 case COMPAQ_NETLIGENT_10_100:
109 sc->phy_link->phy_media = 0;
110 break;
111 case COMPAQ_INT_NETFLEX:
112 case COMPAQ_NETFLEX_BNC:
113 sc->phy_link->phy_media = PHY_BNC | PHY_10baseT;
114 break;
115 default:
116 sc->phy_link->phy_media = PHY_AUI;
117 if (phy_media_probe(sc) != 0) {
118 printf(" (autoconfig failed)");
119 break;
120 }
121 }
122
123 if(sc->phy_link->phy_media) {
124 printf(": ");
125 phy_media_print(sc->phy_link->phy_media);
126 }
127 printf("\n");
128 }
129
130 void tlphy_pdown(v)
131 void *v;
132 {
133 struct phy_softc *sc = v;
134 mii_phy_t *phy_link = sc->phy_link;
135 mii_writereg(phy_link->mii_softc, phy_link->dev, PHY_CONTROL,
136 CTRL_ISO | CTRL_PDOWN);
137 }
138
139 static int tlphy_up(sc)
140 struct phy_softc *sc;
141 {
142 int s;
143 mii_phy_t *phy_link = sc->phy_link;
144 int control, i;
145 s = splnet();
146
147 /* set control registers to a reasonable value, wait for power_up */
148 mii_writereg(phy_link->mii_softc, phy_link->dev, PHY_CONTROL,
149 CTRL_LOOPBK);
150 mii_writereg(phy_link->mii_softc, phy_link->dev, PHY_TL_CTRL,
151 0);
152 i = 0;
153 do {
154 DELAY(100000);
155 control = mii_readreg(phy_link->mii_softc, phy_link->dev,
156 PHY_TL_ST);
157 } while ((control < 0 || (control & TL_ST_PHOK) == 0) && ++i < 10);
158 if (control < 0 || (control & TL_ST_PHOK) == 0) {
159 splx(s);
160 printf("%s: power-up failed\n", sc->sc_dev.dv_xname);
161 return 0;
162 }
163 if ( phy_reset(sc) == 0) {
164 splx(s);
165 return 0;
166 }
167 splx(s);
168 return 1;
169 }
170
171 int tlphy_media_set(media, v)
172 int media;
173 void *v;
174 {
175 struct phy_softc *sc = v;
176 int subtype;
177
178 if (IFM_TYPE(media) != IFM_ETHER)
179 return EINVAL;
180
181 if (tlphy_up(sc) == 0)
182 return EIO;
183
184 subtype = IFM_SUBTYPE(media);
185 switch (subtype) {
186 case IFM_10_2:
187 case IFM_10_5:
188 if ((subtype == IFM_10_2 && (sc->phy_link->phy_media & PHY_BNC)) ||
189 (subtype == IFM_10_5 && (sc->phy_link->phy_media & PHY_AUI)))
190 return tlphy_media_set_aui(sc);
191 else
192 return EINVAL;
193 case IFM_10_T:
194 return phy_media_set_10_100(sc, media);
195 case IFM_AUTO:
196 return ENODEV;
197 default:
198 return EINVAL;
199 }
200 }
201
202 int
203 tlphy_media_set_aui(sc)
204 struct phy_softc *sc;
205 {
206 mii_phy_t *phy_link = sc->phy_link;
207
208 mii_writereg(phy_link->mii_softc, phy_link->dev,
209 PHY_CONTROL, 0);
210 mii_writereg(phy_link->mii_softc, phy_link->dev,
211 PHY_TL_CTRL, TL_CTRL_AUISEL);
212 DELAY(100000);
213 return 0;
214 }
215
216 int tlphy_status(media, v)
217 int media;
218 void *v;
219 {
220 struct phy_softc *sc = v;
221 int reg;
222
223 if (IFM_SUBTYPE(media) == IFM_10_T)
224 return phy_status(media, sc);
225 reg = mii_readreg(sc->phy_link->mii_softc, sc->phy_link->dev,
226 PHY_TL_ST);
227 if ((reg & PHY_TL_ST) == 0)
228 return ENETDOWN;
229 return 0;
230 }
231