xref: /openbsd-src/sys/dev/mii/txphy.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: txphy.c,v 1.11 2013/12/28 03:30:41 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1999 Jason L. Wright (jason@thought.net)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Driver for the Texas Instruments TNETE2101 phy.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/device.h>
37 #include <sys/socket.h>
38 #include <sys/errno.h>
39 
40 #include <net/if.h>
41 #include <net/if_media.h>
42 
43 #include <dev/mii/mii.h>
44 #include <dev/mii/miivar.h>
45 #include <dev/mii/miidevs.h>
46 
47 int	txphymatch(struct device *, void *, void *);
48 void	txphyattach(struct device *, struct device *, void *);
49 
50 struct cfattach txphy_ca = {
51 	sizeof(struct mii_softc), txphymatch, txphyattach, mii_phy_detach
52 };
53 
54 struct cfdriver txphy_cd = {
55 	NULL, "txphy", DV_DULL
56 };
57 
58 int	txphy_service(struct mii_softc *, struct mii_data *, int);
59 
60 const struct mii_phy_funcs txphy_funcs = {
61 	txphy_service, ukphy_status, mii_phy_reset,
62 };
63 
64 static const struct mii_phydesc txphys[] = {
65 	{ MII_OUI_xxTI,	MII_MODEL_xxTI_TNETE2101,
66 	  MII_STR_xxTI_TNETE2101 },
67 
68 	{ 0,			0,
69 	  NULL },
70 };
71 
72 int
73 txphymatch(struct device *parent, void *match, void *aux)
74 {
75 	struct mii_attach_args *ma = aux;
76 
77 	if (mii_phy_match(ma, txphys) != NULL)
78 		return (10);
79 
80 	return (0);
81 }
82 
83 void
84 txphyattach(struct device *parent, struct device *self, void *aux)
85 {
86 	struct mii_softc *sc = (struct mii_softc *)self;
87 	struct mii_attach_args *ma = aux;
88 	struct mii_data *mii = ma->mii_data;
89 	const struct mii_phydesc *mpd;
90 
91 	mpd = mii_phy_match(ma, txphys);
92 	printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
93 
94 	sc->mii_inst = mii->mii_instance;
95 	sc->mii_phy = ma->mii_phyno;
96 	sc->mii_funcs = &txphy_funcs;
97 	sc->mii_pdata = mii;
98 	sc->mii_flags = ma->mii_flags;
99 
100 	PHY_RESET(sc);
101 
102 	sc->mii_capabilities =
103 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
104 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
105 		mii_phy_add_media(sc);
106 }
107 
108 int
109 txphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
110 {
111 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
112 
113 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
114 		return (ENXIO);
115 
116 	/*
117 	 * Can't isolate the RTL8139 phy, so it has to be the only one.
118 	 */
119 	if (IFM_INST(ife->ifm_media) != sc->mii_inst)
120 		panic("txphy_service: attempt to isolate phy");
121 
122 	switch (cmd) {
123 	case MII_POLLSTAT:
124 		break;
125 
126 	case MII_MEDIACHG:
127 		/*
128 		 * If the interface is not up, don't do anything.
129 		 */
130 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
131 			break;
132 
133 		mii_phy_setmedia(sc);
134 		break;
135 
136 	case MII_TICK:
137 		if (mii_phy_tick(sc) == EJUSTRETURN)
138 			return (0);
139 		break;
140 
141 	case MII_DOWN:
142 		mii_phy_down(sc);
143 		return (0);
144 	}
145 
146 	/* Update the media status. */
147 	mii_phy_status(sc);
148 
149 	/* Callback if something changed. */
150 	mii_phy_update(sc, cmd);
151 	return (0);
152 }
153