mcommphy.c revision 1.2 1 /* $NetBSD: mcommphy.c,v 1.2 2024/10/12 18:16:05 skrll Exp $ */
2
3 /*
4 * Copyright (c) 2022 Jared McNeill <jmcneill (at) invisible.ca>
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Motorcomm YT8511C / YT8511H Integrated 10/100/1000 Gigabit Ethernet
31 * Transceiver.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: mcommphy.c,v 1.2 2024/10/12 18:16:05 skrll Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/socket.h>
42 #include <sys/errno.h>
43
44 #include <net/if.h>
45 #include <net/if_media.h>
46
47 #include <dev/mii/mii.h>
48 #include <dev/mii/miivar.h>
49 #include <dev/mii/miidevs.h>
50
51 #define YT85X1_MCOMMPHY_OUI 0x000000
52 #define YT8511_MCOMMPHY_MODEL 0x10
53 #define YT85X1_MCOMMPHY_REV 0x0a
54
55 #define YTPHY_EXT_REG_ADDR 0x1e
56 #define YTPHY_EXT_REG_DATA 0x1f
57
58 /* Extended registers */
59 #define YT8511_CLOCK_GATING_REG 0x0c
60 #define YT8511_TX_CLK_DELAY_SEL __BITS(7,4)
61 #define YT8511_CLK_25M_SEL __BITS(2,1)
62 #define YT8511_CLK_25M_SEL_125M 3
63 #define YT8511_RX_CLK_DELAY_EN __BIT(0)
64
65 #define YT8511_SLEEP_CONTROL1_REG 0x27
66 #define YT8511_PLLON_IN_SLP __BIT(14)
67
68 static int mcommphymatch(device_t, cfdata_t, void *);
69 static void mcommphyattach(device_t, device_t, void *);
70
71 CFATTACH_DECL_NEW(mcommphy, sizeof(struct mii_softc),
72 mcommphymatch, mcommphyattach, mii_phy_detach, mii_phy_activate);
73
74 static int mcommphy_service(struct mii_softc *, struct mii_data *, int);
75
76 static const struct mii_phy_funcs mcommphy_funcs = {
77 mcommphy_service, ukphy_status, mii_phy_reset,
78 };
79
80 static int
81 mcommphymatch(device_t parent, cfdata_t match, void *aux)
82 {
83 struct mii_attach_args *ma = aux;
84
85 /*
86 * The YT8511C reports an OUI of 0. Best we can do here is to match
87 * exactly the contents of the PHY identification registers.
88 */
89 if (MII_OUI(ma->mii_id1, ma->mii_id2) == YT85X1_MCOMMPHY_OUI &&
90 MII_MODEL(ma->mii_id2) == YT8511_MCOMMPHY_MODEL &&
91 MII_REV(ma->mii_id2) == YT85X1_MCOMMPHY_REV) {
92 return 10;
93 }
94
95 return 0;
96 }
97
98 static void
99 mcommphyattach(device_t parent, device_t self, void *aux)
100 {
101 struct mii_softc *sc = device_private(self);
102 struct mii_attach_args *ma = aux;
103 struct mii_data *mii = ma->mii_data;
104 uint16_t oldaddr, data;
105
106 aprint_normal(": Motorcomm YT8511 GbE PHY\n");
107 aprint_naive(": Media interface\n");
108
109 sc->mii_dev = self;
110 sc->mii_inst = mii->mii_instance;
111 sc->mii_phy = ma->mii_phyno;
112 sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
113 sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
114 sc->mii_mpd_rev = MII_REV(ma->mii_id2);
115 sc->mii_funcs = &mcommphy_funcs;
116 sc->mii_pdata = mii;
117 sc->mii_flags = ma->mii_flags;
118
119 mii_lock(mii);
120
121 PHY_RESET(sc);
122
123 PHY_READ(sc, YTPHY_EXT_REG_ADDR, &oldaddr);
124
125 PHY_WRITE(sc, YTPHY_EXT_REG_ADDR, YT8511_CLOCK_GATING_REG);
126 PHY_READ(sc, YTPHY_EXT_REG_DATA, &data);
127 data &= ~YT8511_CLK_25M_SEL;
128 data |= __SHIFTIN(YT8511_CLK_25M_SEL_125M, YT8511_CLK_25M_SEL);
129 if (ISSET(sc->mii_flags, MIIF_RXID)) {
130 data |= YT8511_RX_CLK_DELAY_EN;
131 } else {
132 data &= ~YT8511_RX_CLK_DELAY_EN;
133 }
134 data &= ~YT8511_TX_CLK_DELAY_SEL;
135 if (ISSET(sc->mii_flags, MIIF_TXID)) {
136 data |= __SHIFTIN(0xf, YT8511_TX_CLK_DELAY_SEL);
137 } else {
138 data |= __SHIFTIN(0x2, YT8511_TX_CLK_DELAY_SEL);
139 }
140 PHY_WRITE(sc, YTPHY_EXT_REG_DATA, data);
141
142 PHY_WRITE(sc, YTPHY_EXT_REG_ADDR, YT8511_SLEEP_CONTROL1_REG);
143 PHY_READ(sc, YTPHY_EXT_REG_DATA, &data);
144 data |= YT8511_PLLON_IN_SLP;
145 PHY_WRITE(sc, YTPHY_EXT_REG_DATA, data);
146
147 PHY_WRITE(sc, YTPHY_EXT_REG_ADDR, oldaddr);
148
149 PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
150 sc->mii_capabilities &= ma->mii_capmask;
151 if (sc->mii_capabilities & BMSR_EXTSTAT)
152 PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
153
154 mii_unlock(mii);
155
156 mii_phy_add_media(sc);
157 }
158
159 static int
160 mcommphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
161 {
162 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
163
164 KASSERT(mii_locked(mii));
165
166 switch (cmd) {
167 case MII_POLLSTAT:
168 /* If we're not polling our PHY instance, just return. */
169 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
170 return 0;
171 break;
172
173 case MII_MEDIACHG:
174 /* If the interface is not up, don't do anything. */
175 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
176 break;
177
178 mii_phy_setmedia(sc);
179 break;
180
181 case MII_TICK:
182 /* If we're not currently selected, just return. */
183 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
184 return 0;
185
186 if (mii_phy_tick(sc) == EJUSTRETURN)
187 return 0;
188 break;
189
190 case MII_DOWN:
191 mii_phy_down(sc);
192 return 0;
193 }
194
195 /* Update the media status. */
196 mii_phy_status(sc);
197
198 /* Callback if something changed. */
199 mii_phy_update(sc, cmd);
200 return 0;
201 }
202