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