xref: /freebsd/sys/dev/mii/vscphy.c (revision 1965dd85c3b33ed99cb8ef164dd7c5b20425a85e)
1b6803171SIan Lepore /*-
2b6803171SIan Lepore  * Copyright (c) 2017 Ian Lepore <ian@freebsd.org>
3b6803171SIan Lepore  * All rights reserved.
4b6803171SIan Lepore  *
5b6803171SIan Lepore  * Development sponsored by Microsemi, Inc.
6b6803171SIan Lepore  *
7b6803171SIan Lepore  * Redistribution and use in source and binary forms, with or without
8b6803171SIan Lepore  * modification, are permitted provided that the following conditions
9b6803171SIan Lepore  * are met:
10b6803171SIan Lepore  * 1. Redistributions of source code must retain the above copyright
11b6803171SIan Lepore  *    notice, this list of conditions and the following disclaimer.
12b6803171SIan Lepore  * 2. Redistributions in binary form must reproduce the above copyright
13b6803171SIan Lepore  *    notice, this list of conditions and the following disclaimer in the
14b6803171SIan Lepore  *    documentation and/or other materials provided with the distribution.
15b6803171SIan Lepore  *
16b6803171SIan Lepore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17b6803171SIan Lepore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18b6803171SIan Lepore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19b6803171SIan Lepore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20b6803171SIan Lepore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b6803171SIan Lepore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22b6803171SIan Lepore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23b6803171SIan Lepore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24b6803171SIan Lepore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25b6803171SIan Lepore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26b6803171SIan Lepore  * SUCH DAMAGE.
27b6803171SIan Lepore  */
28b6803171SIan Lepore 
29b6803171SIan Lepore #include <sys/cdefs.h>
30b6803171SIan Lepore /*
31b6803171SIan Lepore  * Microsemi / Vitesse VSC8501 (and similar).
32b6803171SIan Lepore  */
33b6803171SIan Lepore 
34b6803171SIan Lepore #include "opt_platform.h"
35b6803171SIan Lepore 
36b6803171SIan Lepore #include <sys/param.h>
37b6803171SIan Lepore #include <sys/systm.h>
38b6803171SIan Lepore #include <sys/kernel.h>
39b6803171SIan Lepore #include <sys/socket.h>
40b6803171SIan Lepore #include <sys/errno.h>
41b6803171SIan Lepore #include <sys/module.h>
42b6803171SIan Lepore #include <sys/bus.h>
43b6803171SIan Lepore #include <sys/malloc.h>
4489595c17SKornel Duleba #include <sys/resource.h>
4589595c17SKornel Duleba #include <sys/rman.h>
46b6803171SIan Lepore 
47b6803171SIan Lepore #include <net/if.h>
48b6803171SIan Lepore #include <net/if_media.h>
49b6803171SIan Lepore 
50b6803171SIan Lepore #include <dev/mii/mii.h>
51b6803171SIan Lepore #include <dev/mii/miivar.h>
52b6803171SIan Lepore #include "miidevs.h"
53b6803171SIan Lepore #include "miibus_if.h"
54b6803171SIan Lepore 
55b6803171SIan Lepore #ifdef FDT
56b6803171SIan Lepore #include <dev/ofw/openfirm.h>
57b6803171SIan Lepore #include <dev/ofw/ofw_bus.h>
58b6803171SIan Lepore #include <dev/ofw/ofw_bus_subr.h>
59b6803171SIan Lepore #include <dev/mii/mii_fdt.h>
60b6803171SIan Lepore #endif
61b6803171SIan Lepore 
6289595c17SKornel Duleba #define	BIT(x)	(1 << (x))
6389595c17SKornel Duleba 
64b6803171SIan Lepore /* Vitesse VSC8501 */
65b6803171SIan Lepore #define	VSC8501_EXTPAGE_REG		0x001f
66b6803171SIan Lepore 
67b6803171SIan Lepore #define	VSC8501_EXTCTL1_REG		0x0017
68b6803171SIan Lepore #define	  VSC8501_EXTCTL1_RGMII_MODE	  (1u << 12)
69b6803171SIan Lepore 
7089595c17SKornel Duleba #define	VSC8501_INT_MASK		0x19
7189595c17SKornel Duleba #define	VSC8501_INT_MDINT		BIT(15)
7289595c17SKornel Duleba #define	VSC8501_INT_SPD_CHG		BIT(14)
7389595c17SKornel Duleba #define	VSC8501_INT_LINK_CHG		BIT(13)
7489595c17SKornel Duleba #define	VSC8501_INT_FD_CHG		BIT(12)
7589595c17SKornel Duleba #define	VSC8501_INT_AN_CMPL		BIT(10)
7689595c17SKornel Duleba 
7789595c17SKornel Duleba #define	VSC8501_INT_STS			0x1a
7889595c17SKornel Duleba 
79b6803171SIan Lepore #define	VSC8501_RGMII_CTRL_PAGE		0x02
80b6803171SIan Lepore #define	VSC8501_RGMII_CTRL_REG		0x14
81b6803171SIan Lepore #define	  VSC8501_RGMII_DELAY_MASK	  0x07
82b6803171SIan Lepore #define	  VSC8501_RGMII_DELAY_TXSHIFT	  0
83b6803171SIan Lepore #define	  VSC8501_RGMII_DELAY_RXSHIFT	  4
84b6803171SIan Lepore #define	  VSC8501_RGMII_RXCLOCK_DISABLE	  (1u << 11)
85b6803171SIan Lepore #define	  VSC8501_RGMII_RXSWAP		  (1u <<  7)
86b6803171SIan Lepore #define	  VSC8501_RGMII_TXSWAP		  (1u <<  3)
87b6803171SIan Lepore #define	  VSC8501_RGMII_LANESWAP	  (VSC8501_RGMII_RXSWAP | \
88b6803171SIan Lepore 					   VSC8501_RGMII_TXSWAP)
89b6803171SIan Lepore 
90b6803171SIan Lepore struct vscphy_softc {
91b6803171SIan Lepore 	mii_softc_t	mii_sc;
92b6803171SIan Lepore 	device_t	dev;
93b6803171SIan Lepore 	mii_contype_t	contype;
94b6803171SIan Lepore 	int		rxdelay;
95b6803171SIan Lepore 	int		txdelay;
96b6803171SIan Lepore 	bool		laneswap;
9789595c17SKornel Duleba 	struct resource *irq_res;
9889595c17SKornel Duleba 	void 		*irq_cookie;
99b6803171SIan Lepore };
100b6803171SIan Lepore 
101b6803171SIan Lepore static void vscphy_reset(struct mii_softc *);
102b6803171SIan Lepore static int  vscphy_service(struct mii_softc *, struct mii_data *, int);
103b6803171SIan Lepore 
104b6803171SIan Lepore static const struct mii_phydesc vscphys[] = {
105b6803171SIan Lepore 	MII_PHY_DESC(xxVITESSE, VSC8501),
10689595c17SKornel Duleba 	MII_PHY_DESC(xxVITESSE, VSC8504),
107*1965dd85SBjoern A. Zeeb 	MII_PHY_DESC(xxVITESSE, VSC8514),
108b6803171SIan Lepore 	MII_PHY_END
109b6803171SIan Lepore };
110b6803171SIan Lepore 
111b6803171SIan Lepore static const struct mii_phy_funcs vscphy_funcs = {
112b6803171SIan Lepore 	vscphy_service,
113b6803171SIan Lepore 	ukphy_status,
114b6803171SIan Lepore 	vscphy_reset
115b6803171SIan Lepore };
116b6803171SIan Lepore 
117b6803171SIan Lepore #ifdef FDT
118b6803171SIan Lepore static void
vscphy_fdt_get_config(struct vscphy_softc * vsc)119b6803171SIan Lepore vscphy_fdt_get_config(struct vscphy_softc *vsc)
120b6803171SIan Lepore {
121b6803171SIan Lepore 	mii_fdt_phy_config_t *cfg;
122b6803171SIan Lepore 	pcell_t val;
123b6803171SIan Lepore 
124b6803171SIan Lepore 	cfg = mii_fdt_get_config(vsc->dev);
125b6803171SIan Lepore 	vsc->contype = cfg->con_type;
126b6803171SIan Lepore 	vsc->laneswap = (cfg->flags & MIIF_FDT_LANE_SWAP) &&
127b6803171SIan Lepore 	    !(cfg->flags & MIIF_FDT_NO_LANE_SWAP);
128b6803171SIan Lepore 	if (OF_getencprop(cfg->phynode, "rx-delay", &val, sizeof(val)) > 0)
129b6803171SIan Lepore 		vsc->rxdelay = val;
130b6803171SIan Lepore 	if (OF_getencprop(cfg->phynode, "tx-delay", &val, sizeof(val)) > 0)
131b6803171SIan Lepore 		vsc->txdelay = val;
13289fb4802SKornel Duleba 	vsc->mii_sc.mii_maxspeed = cfg->max_speed;
133b6803171SIan Lepore 	mii_fdt_free_config(cfg);
134b6803171SIan Lepore }
135b6803171SIan Lepore #endif
136b6803171SIan Lepore 
137b6803171SIan Lepore static inline int
vscphy_read(struct vscphy_softc * sc,u_int reg)138b6803171SIan Lepore vscphy_read(struct vscphy_softc *sc, u_int reg)
139b6803171SIan Lepore {
140b6803171SIan Lepore 	u_int val;
141b6803171SIan Lepore 
142b6803171SIan Lepore 	val = PHY_READ(&sc->mii_sc, reg);
143b6803171SIan Lepore 	return (val);
144b6803171SIan Lepore }
145b6803171SIan Lepore 
146b6803171SIan Lepore static inline void
vscphy_write(struct vscphy_softc * sc,u_int reg,u_int val)147b6803171SIan Lepore vscphy_write(struct vscphy_softc *sc, u_int reg, u_int val)
148b6803171SIan Lepore {
149b6803171SIan Lepore 
150b6803171SIan Lepore 	PHY_WRITE(&sc->mii_sc, reg, val);
151b6803171SIan Lepore }
152b6803171SIan Lepore 
153b6803171SIan Lepore static void
vsc8501_setup_rgmii(struct vscphy_softc * vsc)154b6803171SIan Lepore vsc8501_setup_rgmii(struct vscphy_softc *vsc)
155b6803171SIan Lepore {
156b6803171SIan Lepore 	int reg;
157b6803171SIan Lepore 
158b6803171SIan Lepore 	vscphy_write(vsc, VSC8501_EXTPAGE_REG, VSC8501_RGMII_CTRL_PAGE);
159b6803171SIan Lepore 
160b6803171SIan Lepore 	reg = vscphy_read(vsc, VSC8501_RGMII_CTRL_REG);
161b6803171SIan Lepore 	reg &= ~VSC8501_RGMII_RXCLOCK_DISABLE;
162b6803171SIan Lepore 	reg &= ~VSC8501_RGMII_LANESWAP;
163b6803171SIan Lepore 	reg &= ~(VSC8501_RGMII_DELAY_MASK << VSC8501_RGMII_DELAY_TXSHIFT);
164b6803171SIan Lepore 	reg &= ~(VSC8501_RGMII_DELAY_MASK << VSC8501_RGMII_DELAY_RXSHIFT);
165b6803171SIan Lepore 	if (vsc->laneswap)
166b6803171SIan Lepore 		reg |= VSC8501_RGMII_LANESWAP;
167b6803171SIan Lepore 	if (vsc->contype == MII_CONTYPE_RGMII_ID ||
168b6803171SIan Lepore 	    vsc->contype == MII_CONTYPE_RGMII_TXID) {
169b6803171SIan Lepore 		reg |= vsc->txdelay << VSC8501_RGMII_DELAY_TXSHIFT;
170b6803171SIan Lepore 	}
171b6803171SIan Lepore 	if (vsc->contype == MII_CONTYPE_RGMII_ID ||
172b6803171SIan Lepore 	    vsc->contype == MII_CONTYPE_RGMII_RXID) {
173b6803171SIan Lepore 		reg |= vsc->rxdelay << VSC8501_RGMII_DELAY_RXSHIFT;
174b6803171SIan Lepore 	}
175b6803171SIan Lepore 	vscphy_write(vsc, VSC8501_RGMII_CTRL_REG, reg);
176b6803171SIan Lepore 
177b6803171SIan Lepore 	vscphy_write(vsc, VSC8501_EXTPAGE_REG, 0);
178b6803171SIan Lepore }
179b6803171SIan Lepore 
180b6803171SIan Lepore static void
vsc8501_reset(struct vscphy_softc * vsc)181b6803171SIan Lepore vsc8501_reset(struct vscphy_softc *vsc)
182b6803171SIan Lepore {
183b6803171SIan Lepore 	int reg;
184b6803171SIan Lepore 
185b6803171SIan Lepore 	/*
186b6803171SIan Lepore 	 * Must set whether the mac<->phy connection is RGMII first; changes to
187b6803171SIan Lepore 	 * that bit take effect only after a softreset.
188b6803171SIan Lepore 	 */
189b6803171SIan Lepore 	reg = vscphy_read(vsc, VSC8501_EXTCTL1_REG);
190b6803171SIan Lepore 	if (mii_contype_is_rgmii(vsc->contype))
191b6803171SIan Lepore 		reg |= VSC8501_EXTCTL1_RGMII_MODE;
192b6803171SIan Lepore 	else
193b6803171SIan Lepore 		reg &= ~VSC8501_EXTCTL1_RGMII_MODE;
194b6803171SIan Lepore 	vscphy_write(vsc, VSC8501_EXTCTL1_REG, reg);
195b6803171SIan Lepore 
196b6803171SIan Lepore 	mii_phy_reset(&vsc->mii_sc);
197b6803171SIan Lepore 
198b6803171SIan Lepore 	/*
199b6803171SIan Lepore 	 * Setup rgmii control register if necessary, after softreset.
200b6803171SIan Lepore 	 */
201b6803171SIan Lepore 	if (mii_contype_is_rgmii(vsc->contype))
202b6803171SIan Lepore 	    vsc8501_setup_rgmii(vsc);
203b6803171SIan Lepore }
204b6803171SIan Lepore 
205b6803171SIan Lepore static void
vscphy_reset(struct mii_softc * sc)206b6803171SIan Lepore vscphy_reset(struct mii_softc *sc)
207b6803171SIan Lepore {
208b6803171SIan Lepore 	struct vscphy_softc *vsc = (struct vscphy_softc *)sc;
209b6803171SIan Lepore 
210b6803171SIan Lepore 	switch (sc->mii_mpd_model) {
211b6803171SIan Lepore 	case MII_MODEL_xxVITESSE_VSC8501:
212b6803171SIan Lepore 		vsc8501_reset(vsc);
213b6803171SIan Lepore 		break;
214b6803171SIan Lepore 	default:
215b6803171SIan Lepore 		mii_phy_reset(sc);
216b6803171SIan Lepore 		break;
217b6803171SIan Lepore 	}
218b6803171SIan Lepore }
219b6803171SIan Lepore 
220b6803171SIan Lepore static int
vscphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)221b6803171SIan Lepore vscphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
222b6803171SIan Lepore {
223b6803171SIan Lepore 
224b6803171SIan Lepore 	switch (cmd) {
225b6803171SIan Lepore 	case MII_POLLSTAT:
226b6803171SIan Lepore 		break;
227b6803171SIan Lepore 
228b6803171SIan Lepore 	case MII_MEDIACHG:
229b6803171SIan Lepore 		mii_phy_setmedia(sc);
230b6803171SIan Lepore 		break;
231b6803171SIan Lepore 
232b6803171SIan Lepore 	case MII_TICK:
233b6803171SIan Lepore 		if (mii_phy_tick(sc) == EJUSTRETURN)
234b6803171SIan Lepore 			return (0);
235b6803171SIan Lepore 		break;
236b6803171SIan Lepore 	}
237b6803171SIan Lepore 
238b6803171SIan Lepore 	/* Update the media status. */
239b6803171SIan Lepore 	PHY_STATUS(sc);
240b6803171SIan Lepore 
241b6803171SIan Lepore 	/* Callback if something changed. */
242b6803171SIan Lepore 	mii_phy_update(sc, cmd);
243b6803171SIan Lepore 	return (0);
244b6803171SIan Lepore }
245b6803171SIan Lepore 
246b6803171SIan Lepore static int
vscphy_probe(device_t dev)247b6803171SIan Lepore vscphy_probe(device_t dev)
248b6803171SIan Lepore {
249b6803171SIan Lepore 
250b6803171SIan Lepore 	return (mii_phy_dev_probe(dev, vscphys, BUS_PROBE_DEFAULT));
251b6803171SIan Lepore }
252b6803171SIan Lepore 
25389595c17SKornel Duleba static void
vscphy_intr(void * arg)25489595c17SKornel Duleba vscphy_intr(void *arg)
25589595c17SKornel Duleba {
25689595c17SKornel Duleba 	struct vscphy_softc *vsc;
25789595c17SKornel Duleba 	uint32_t status;
25889595c17SKornel Duleba 
25989595c17SKornel Duleba 	vsc = (struct vscphy_softc *)arg;
26089595c17SKornel Duleba 
26189595c17SKornel Duleba 	status = vscphy_read(vsc, VSC8501_INT_STS);
26289595c17SKornel Duleba 	status &= vscphy_read(vsc, VSC8501_INT_MASK);
26389595c17SKornel Duleba 
26489595c17SKornel Duleba 	if (!status)
26589595c17SKornel Duleba 		return;
26689595c17SKornel Duleba 
26789595c17SKornel Duleba 	PHY_STATUS(&vsc->mii_sc);
26889595c17SKornel Duleba 	mii_phy_update(&vsc->mii_sc, MII_MEDIACHG);
26989595c17SKornel Duleba }
27089595c17SKornel Duleba 
271b6803171SIan Lepore static int
vscphy_attach(device_t dev)272b6803171SIan Lepore vscphy_attach(device_t dev)
273b6803171SIan Lepore {
274b6803171SIan Lepore 	struct vscphy_softc *vsc;
27589595c17SKornel Duleba 	uint32_t value;
27689595c17SKornel Duleba 	int rid, error;
277b6803171SIan Lepore 
278b6803171SIan Lepore 	vsc = device_get_softc(dev);
279b6803171SIan Lepore 	vsc->dev = dev;
280b6803171SIan Lepore 
281b6803171SIan Lepore #ifdef FDT
282b6803171SIan Lepore 	vscphy_fdt_get_config(vsc);
283b6803171SIan Lepore #endif
284b6803171SIan Lepore 
285b6803171SIan Lepore 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &vscphy_funcs, 1);
286b6803171SIan Lepore 	mii_phy_setmedia(&vsc->mii_sc);
287b6803171SIan Lepore 
28889595c17SKornel Duleba 	rid = 0;
28989595c17SKornel Duleba 	vsc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
29089595c17SKornel Duleba 	    RF_ACTIVE | RF_SHAREABLE);
29189595c17SKornel Duleba 	if (vsc->irq_res == NULL)
29289595c17SKornel Duleba 		goto no_irq;
29389595c17SKornel Duleba 
29489595c17SKornel Duleba 	error = bus_setup_intr(dev, vsc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
29589595c17SKornel Duleba 	    NULL, vscphy_intr, vsc, &vsc->irq_cookie);
29689595c17SKornel Duleba 	if (error != 0) {
29789595c17SKornel Duleba 		bus_release_resource(dev, SYS_RES_IRQ, 0, vsc->irq_res);
29889595c17SKornel Duleba 		vsc->irq_res = NULL;
29989595c17SKornel Duleba 		goto no_irq;
30089595c17SKornel Duleba 	}
30189595c17SKornel Duleba 
30289595c17SKornel Duleba 	/* Ack and unmask all relevant interrupts. */
30389595c17SKornel Duleba 	(void)vscphy_read(vsc, VSC8501_INT_STS);
30489595c17SKornel Duleba 	value = VSC8501_INT_MDINT    |
30589595c17SKornel Duleba 		VSC8501_INT_SPD_CHG  |
30689595c17SKornel Duleba 		VSC8501_INT_LINK_CHG |
30789595c17SKornel Duleba 		VSC8501_INT_FD_CHG   |
30889595c17SKornel Duleba 		VSC8501_INT_AN_CMPL;
30989595c17SKornel Duleba 	vscphy_write(vsc, VSC8501_INT_MASK, value);
31089595c17SKornel Duleba 
31189595c17SKornel Duleba no_irq:
312b6803171SIan Lepore 	return (0);
313b6803171SIan Lepore }
314b6803171SIan Lepore 
31589595c17SKornel Duleba static int
vscphy_detach(device_t dev)31689595c17SKornel Duleba vscphy_detach(device_t dev)
31789595c17SKornel Duleba {
31889595c17SKornel Duleba 	struct vscphy_softc *vsc;
31989595c17SKornel Duleba 
32089595c17SKornel Duleba 	vsc = device_get_softc(dev);
32189595c17SKornel Duleba 
32289595c17SKornel Duleba 	bus_teardown_intr(dev, vsc->irq_res, vsc->irq_cookie);
32389595c17SKornel Duleba 	bus_release_resource(dev, SYS_RES_IRQ, 0, vsc->irq_res);
32489595c17SKornel Duleba 
32589595c17SKornel Duleba 	return (mii_phy_detach(dev));
32689595c17SKornel Duleba }
32789595c17SKornel Duleba 
328b6803171SIan Lepore static device_method_t vscphy_methods[] = {
329b6803171SIan Lepore 	/* device interface */
330b6803171SIan Lepore 	DEVMETHOD(device_probe,		vscphy_probe),
331b6803171SIan Lepore 	DEVMETHOD(device_attach,	vscphy_attach),
33289595c17SKornel Duleba 	DEVMETHOD(device_detach,	vscphy_detach),
333b6803171SIan Lepore 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
334b6803171SIan Lepore 	DEVMETHOD_END
335b6803171SIan Lepore };
336b6803171SIan Lepore 
337b6803171SIan Lepore static driver_t vscphy_driver = {
338b6803171SIan Lepore 	"vscphy",
339b6803171SIan Lepore 	vscphy_methods,
340b6803171SIan Lepore 	sizeof(struct vscphy_softc)
341b6803171SIan Lepore };
342b6803171SIan Lepore 
343f438c2ffSJohn Baldwin DRIVER_MODULE(vscphy, miibus, vscphy_driver, 0, 0);
344