tlphy.c revision 1.5 1 1.5 thorpej /* $NetBSD: tlphy.c,v 1.5 1998/01/12 09:28:54 thorpej 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.4 thorpej * 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.4 thorpej void tlphy_pdown __P((void *v));
53 1.4 thorpej int tlphy_media_set __P((int, void *));
54 1.4 thorpej int tlphy_media_set_aui __P((struct phy_softc *));
55 1.4 thorpej int tlphy_status __P((int, void*));
56 1.1 bouyer
57 1.1 bouyer #ifdef __BROKEN_INDIRECT_CONFIG
58 1.4 thorpej int tlphymatch __P((struct device *, void *, void *));
59 1.1 bouyer #else
60 1.4 thorpej int tlphymatch __P((struct device *, struct cfdata *, void *));
61 1.1 bouyer #endif
62 1.4 thorpej 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 int
69 1.1 bouyer tlphymatch(parent, match, aux)
70 1.4 thorpej struct device *parent;
71 1.1 bouyer #ifdef __BROKEN_INDIRECT_CONFIG
72 1.1 bouyer void *match;
73 1.1 bouyer #else
74 1.1 bouyer struct cfdata *match;
75 1.1 bouyer #endif
76 1.1 bouyer void *aux;
77 1.1 bouyer {
78 1.1 bouyer mii_phy_t *phy = aux;
79 1.1 bouyer
80 1.3 christos if (phy->phy_id == 0x40005013 || phy->phy_id == 0x40005014 ||
81 1.3 christos phy->phy_id == 0x40005015)
82 1.4 thorpej return (1);
83 1.4 thorpej return (0);
84 1.1 bouyer }
85 1.1 bouyer
86 1.1 bouyer void
87 1.1 bouyer tlphyattach(parent, self, aux)
88 1.4 thorpej struct device *parent, *self;
89 1.1 bouyer void *aux;
90 1.1 bouyer {
91 1.1 bouyer struct phy_softc *sc = (struct phy_softc *)self;
92 1.1 bouyer
93 1.1 bouyer sc->phy_link = aux;
94 1.1 bouyer sc->phy_link->phy_softc = sc;
95 1.1 bouyer sc->phy_link->phy_media_set = tlphy_media_set;
96 1.1 bouyer sc->phy_link->phy_status = tlphy_status;
97 1.1 bouyer sc->phy_link->phy_pdown = tlphy_pdown;
98 1.1 bouyer
99 1.1 bouyer phy_reset(sc);
100 1.1 bouyer
101 1.1 bouyer switch (sc->phy_link->adapter_id) {
102 1.1 bouyer case COMPAQ_INT_NETLIGENT_10_100:
103 1.1 bouyer sc->phy_link->phy_media = PHY_BNC;
104 1.1 bouyer break;
105 1.1 bouyer case COMPAQ_NETLIGENT_10_100:
106 1.1 bouyer sc->phy_link->phy_media = 0;
107 1.1 bouyer break;
108 1.1 bouyer case COMPAQ_INT_NETFLEX:
109 1.1 bouyer case COMPAQ_NETFLEX_BNC:
110 1.1 bouyer sc->phy_link->phy_media = PHY_BNC | PHY_10baseT;
111 1.3 christos break;
112 1.3 christos case TI_TLAN:
113 1.3 christos sc->phy_link->phy_media = PHY_10baseT;
114 1.1 bouyer break;
115 1.1 bouyer default:
116 1.1 bouyer sc->phy_link->phy_media = PHY_AUI;
117 1.1 bouyer if (phy_media_probe(sc) != 0) {
118 1.1 bouyer printf(" (autoconfig failed)");
119 1.1 bouyer break;
120 1.1 bouyer }
121 1.1 bouyer }
122 1.1 bouyer
123 1.4 thorpej if (sc->phy_link->phy_media) {
124 1.1 bouyer printf(": ");
125 1.1 bouyer phy_media_print(sc->phy_link->phy_media);
126 1.1 bouyer }
127 1.1 bouyer printf("\n");
128 1.1 bouyer }
129 1.1 bouyer
130 1.4 thorpej void
131 1.4 thorpej tlphy_pdown(v)
132 1.1 bouyer void *v;
133 1.1 bouyer {
134 1.1 bouyer struct phy_softc *sc = v;
135 1.1 bouyer mii_phy_t *phy_link = sc->phy_link;
136 1.4 thorpej
137 1.1 bouyer mii_writereg(phy_link->mii_softc, phy_link->dev, PHY_CONTROL,
138 1.4 thorpej CTRL_ISO | CTRL_PDOWN);
139 1.1 bouyer }
140 1.1 bouyer
141 1.4 thorpej static int
142 1.4 thorpej tlphy_up(sc)
143 1.1 bouyer struct phy_softc *sc;
144 1.1 bouyer {
145 1.1 bouyer int s;
146 1.1 bouyer mii_phy_t *phy_link = sc->phy_link;
147 1.1 bouyer int control, i;
148 1.4 thorpej
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.4 thorpej CTRL_LOOPBK);
154 1.4 thorpej mii_writereg(phy_link->mii_softc, phy_link->dev, PHY_TL_CTRL, 0);
155 1.1 bouyer i = 0;
156 1.1 bouyer do {
157 1.1 bouyer DELAY(100000);
158 1.1 bouyer control = mii_readreg(phy_link->mii_softc, phy_link->dev,
159 1.4 thorpej PHY_TL_ST);
160 1.1 bouyer } while ((control < 0 || (control & TL_ST_PHOK) == 0) && ++i < 10);
161 1.1 bouyer if (control < 0 || (control & TL_ST_PHOK) == 0) {
162 1.1 bouyer splx(s);
163 1.1 bouyer printf("%s: power-up failed\n", sc->sc_dev.dv_xname);
164 1.4 thorpej return (0);
165 1.1 bouyer }
166 1.4 thorpej if (phy_reset(sc) == 0) {
167 1.1 bouyer splx(s);
168 1.4 thorpej return (0);
169 1.1 bouyer }
170 1.1 bouyer splx(s);
171 1.4 thorpej return (1);
172 1.1 bouyer }
173 1.1 bouyer
174 1.4 thorpej int
175 1.4 thorpej 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.4 thorpej return (EINVAL);
184 1.1 bouyer
185 1.1 bouyer if (tlphy_up(sc) == 0)
186 1.4 thorpej 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.4 thorpej if ((subtype == IFM_10_2 &&
193 1.4 thorpej (sc->phy_link->phy_media & PHY_BNC)) ||
194 1.4 thorpej (subtype == IFM_10_5 &&
195 1.4 thorpej (sc->phy_link->phy_media & PHY_AUI)))
196 1.4 thorpej return (tlphy_media_set_aui(sc));
197 1.1 bouyer else
198 1.4 thorpej return (EINVAL);
199 1.1 bouyer case IFM_10_T:
200 1.4 thorpej return (phy_media_set_10_100(sc, media));
201 1.1 bouyer case IFM_AUTO:
202 1.4 thorpej return (ENODEV);
203 1.1 bouyer default:
204 1.4 thorpej return (EINVAL);
205 1.1 bouyer }
206 1.1 bouyer }
207 1.1 bouyer
208 1.1 bouyer int
209 1.1 bouyer tlphy_media_set_aui(sc)
210 1.1 bouyer struct phy_softc *sc;
211 1.1 bouyer {
212 1.1 bouyer mii_phy_t *phy_link = sc->phy_link;
213 1.1 bouyer
214 1.1 bouyer mii_writereg(phy_link->mii_softc, phy_link->dev,
215 1.4 thorpej PHY_CONTROL, 0);
216 1.1 bouyer mii_writereg(phy_link->mii_softc, phy_link->dev,
217 1.4 thorpej PHY_TL_CTRL, TL_CTRL_AUISEL);
218 1.1 bouyer DELAY(100000);
219 1.4 thorpej return (0);
220 1.1 bouyer }
221 1.1 bouyer
222 1.4 thorpej int
223 1.4 thorpej tlphy_status(media, v)
224 1.1 bouyer int media;
225 1.1 bouyer void *v;
226 1.1 bouyer {
227 1.1 bouyer struct phy_softc *sc = v;
228 1.1 bouyer int reg;
229 1.1 bouyer
230 1.1 bouyer if (IFM_SUBTYPE(media) == IFM_10_T)
231 1.4 thorpej return (phy_status(media, sc));
232 1.1 bouyer reg = mii_readreg(sc->phy_link->mii_softc, sc->phy_link->dev,
233 1.4 thorpej PHY_TL_ST);
234 1.1 bouyer if ((reg & PHY_TL_ST) == 0)
235 1.4 thorpej return (ENETDOWN);
236 1.4 thorpej return (0);
237 1.1 bouyer }
238