1/*	$NetBSD: dmphy.c,v 1.47 2020/03/15 23:04:50 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56
57/*
58 * driver for Davicom's DM9101 ethernet 10/100 PHY
59 * Data Sheet available from www.davicom8.com
60 */
61
62#include <sys/cdefs.h>
63__KERNEL_RCSID(0, "$NetBSD: dmphy.c,v 1.47 2020/03/15 23:04:50 thorpej Exp $");
64
65#include <sys/param.h>
66#include <sys/systm.h>
67#include <sys/kernel.h>
68#include <sys/device.h>
69#include <sys/socket.h>
70#include <sys/errno.h>
71
72#include <net/if.h>
73#include <net/if_media.h>
74
75#include <dev/mii/mii.h>
76#include <dev/mii/miivar.h>
77#include <dev/mii/miidevs.h>
78
79#include <dev/mii/dmphyreg.h>
80
81static int	dmphymatch(device_t, cfdata_t, void *);
82static void	dmphyattach(device_t, device_t, void *);
83
84CFATTACH_DECL_NEW(dmphy, sizeof(struct mii_softc),
85    dmphymatch, dmphyattach, mii_phy_detach, mii_phy_activate);
86
87static int	dmphy_service(struct mii_softc *, struct mii_data *, int);
88static void	dmphy_status(struct mii_softc *);
89
90static const struct mii_phy_funcs dmphy_funcs = {
91	dmphy_service, dmphy_status, mii_phy_reset,
92};
93
94static const struct mii_phydesc dmphys[] = {
95	MII_PHY_DESC(xxDAVICOM, DM9101),
96	MII_PHY_DESC(xxDAVICOM, DM9102),
97	MII_PHY_DESC(xxDAVICOM, DM9161),
98	MII_PHY_DESC(xxDAVICOM, DM9161A),
99	MII_PHY_DESC(xxDAVICOM, DM9161B),
100	MII_PHY_DESC(xxDAVICOM, DM9601),
101	MII_PHY_DESC(DAVICOM, DM9101),
102	MII_PHY_END,
103};
104
105static int
106dmphymatch(device_t parent, cfdata_t match, void *aux)
107{
108	struct mii_attach_args *ma = aux;
109
110	if (mii_phy_match(ma, dmphys) != NULL)
111		return 10;
112
113	return 0;
114}
115
116static void
117dmphyattach(device_t parent, device_t self, void *aux)
118{
119	struct mii_softc *sc = device_private(self);
120	struct mii_attach_args *ma = aux;
121	struct mii_data *mii = ma->mii_data;
122	const struct mii_phydesc *mpd;
123
124	mpd = mii_phy_match(ma, dmphys);
125	aprint_naive(": Media interface\n");
126	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
127
128	sc->mii_dev = self;
129	sc->mii_inst = mii->mii_instance;
130	sc->mii_phy = ma->mii_phyno;
131	sc->mii_funcs = &dmphy_funcs;
132	sc->mii_pdata = mii;
133	sc->mii_flags = ma->mii_flags;
134
135	mii_lock(mii);
136
137	PHY_RESET(sc);
138
139	PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
140	sc->mii_capabilities &= ma->mii_capmask;
141
142	mii_unlock(mii);
143
144	mii_phy_add_media(sc);
145}
146
147static int
148dmphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
149{
150	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
151	uint16_t reg;
152
153	KASSERT(mii_locked(mii));
154
155	switch (cmd) {
156	case MII_POLLSTAT:
157		/* If we're not polling our PHY instance, just return. */
158		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
159			return 0;
160		break;
161
162	case MII_MEDIACHG:
163		/*
164		 * If the media indicates a different PHY instance,
165		 * isolate ourselves.
166		 */
167		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
168			PHY_READ(sc, MII_BMCR, &reg);
169			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
170			return 0;
171		}
172
173		/* If the interface is not up, don't do anything. */
174		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
175			break;
176
177		mii_phy_setmedia(sc);
178		break;
179
180	case MII_TICK:
181		/* If we're not currently selected, just return. */
182		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
183			return 0;
184
185		if (mii_phy_tick(sc) == EJUSTRETURN)
186			return 0;
187		break;
188
189	case MII_DOWN:
190		mii_phy_down(sc);
191		return 0;
192	}
193
194	/* Update the media status. */
195	mii_phy_status(sc);
196
197	/* Callback if something changed. */
198	mii_phy_update(sc, cmd);
199	return 0;
200}
201
202static void
203dmphy_status(struct mii_softc *sc)
204{
205	struct mii_data *mii = sc->mii_pdata;
206	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
207	uint16_t bmsr, bmcr, dscsr;
208
209	KASSERT(mii_locked(mii));
210
211	mii->mii_media_status = IFM_AVALID;
212	mii->mii_media_active = IFM_ETHER;
213
214	PHY_READ(sc, MII_BMSR, &bmsr);
215	PHY_READ(sc, MII_BMSR, &bmsr);
216	if (bmsr & BMSR_LINK)
217		mii->mii_media_status |= IFM_ACTIVE;
218
219	PHY_READ(sc, MII_BMCR, &bmcr);
220	if (bmcr & BMCR_ISO) {
221		mii->mii_media_active |= IFM_NONE;
222		mii->mii_media_status = 0;
223		return;
224	}
225
226	if (bmcr & BMCR_LOOP)
227		mii->mii_media_active |= IFM_LOOP;
228
229	if (bmcr & BMCR_AUTOEN) {
230		/*
231		 * The PAR status bits are only valid if autonegotiation
232		 * has completed (or it's disabled).
233		 */
234		if ((bmsr & BMSR_ACOMP) == 0) {
235			/* Erg, still trying, I guess... */
236			mii->mii_media_active |= IFM_NONE;
237			return;
238		}
239
240		PHY_READ(sc, MII_DMPHY_DSCSR, &dscsr);
241		if (dscsr & DSCSR_100FDX)
242			mii->mii_media_active |= IFM_100_TX | IFM_FDX;
243		else if (dscsr & DSCSR_100HDX)
244			mii->mii_media_active |= IFM_100_TX | IFM_HDX;
245		else if (dscsr & DSCSR_10FDX)
246			mii->mii_media_active |= IFM_10_T | IFM_FDX;
247		else if (dscsr & DSCSR_10HDX)
248			mii->mii_media_active |= IFM_10_T | IFM_HDX;
249		else
250			mii->mii_media_active |= IFM_NONE;
251		if (mii->mii_media_active & IFM_FDX)
252			mii->mii_media_active |= mii_phy_flowstatus(sc);
253	} else
254		mii->mii_media_active = ife->ifm_media;
255}
256