xref: /freebsd/sys/dev/dwc/if_dwc_aw.c (revision 432ae724e6ebac4696a7baf5020e2445db06dc2f)
1*432ae724SEmmanuel Vadot /*-
2*432ae724SEmmanuel Vadot  * Copyright (c) 2015 Luiz Otavio O Souza <loos@FreeBSD.org>
3*432ae724SEmmanuel Vadot  * All rights reserved.
4*432ae724SEmmanuel Vadot  *
5*432ae724SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
6*432ae724SEmmanuel Vadot  * modification, are permitted provided that the following conditions
7*432ae724SEmmanuel Vadot  * are met:
8*432ae724SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
9*432ae724SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
10*432ae724SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
11*432ae724SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
12*432ae724SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
13*432ae724SEmmanuel Vadot  *
14*432ae724SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*432ae724SEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*432ae724SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*432ae724SEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*432ae724SEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*432ae724SEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*432ae724SEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*432ae724SEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*432ae724SEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*432ae724SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*432ae724SEmmanuel Vadot  * SUCH DAMAGE.
25*432ae724SEmmanuel Vadot  */
26*432ae724SEmmanuel Vadot 
27*432ae724SEmmanuel Vadot #include <sys/cdefs.h>
28*432ae724SEmmanuel Vadot #include <sys/param.h>
29*432ae724SEmmanuel Vadot #include <sys/systm.h>
30*432ae724SEmmanuel Vadot #include <sys/bus.h>
31*432ae724SEmmanuel Vadot #include <sys/kernel.h>
32*432ae724SEmmanuel Vadot #include <sys/socket.h>
33*432ae724SEmmanuel Vadot #include <sys/module.h>
34*432ae724SEmmanuel Vadot 
35*432ae724SEmmanuel Vadot #include <net/if.h>
36*432ae724SEmmanuel Vadot 
37*432ae724SEmmanuel Vadot #include <machine/bus.h>
38*432ae724SEmmanuel Vadot 
39*432ae724SEmmanuel Vadot #include <dev/dwc/if_dwc.h>
40*432ae724SEmmanuel Vadot #include <dev/dwc/if_dwcvar.h>
41*432ae724SEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
42*432ae724SEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
43*432ae724SEmmanuel Vadot 
44*432ae724SEmmanuel Vadot #include <arm/allwinner/aw_machdep.h>
45*432ae724SEmmanuel Vadot #include <dev/extres/clk/clk.h>
46*432ae724SEmmanuel Vadot #include <dev/extres/regulator/regulator.h>
47*432ae724SEmmanuel Vadot 
48*432ae724SEmmanuel Vadot #include "if_dwc_if.h"
49*432ae724SEmmanuel Vadot 
50*432ae724SEmmanuel Vadot static int
51*432ae724SEmmanuel Vadot a20_if_dwc_probe(device_t dev)
52*432ae724SEmmanuel Vadot {
53*432ae724SEmmanuel Vadot 
54*432ae724SEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
55*432ae724SEmmanuel Vadot 		return (ENXIO);
56*432ae724SEmmanuel Vadot 	if (!ofw_bus_is_compatible(dev, "allwinner,sun7i-a20-gmac"))
57*432ae724SEmmanuel Vadot 		return (ENXIO);
58*432ae724SEmmanuel Vadot 	device_set_desc(dev, "A20 Gigabit Ethernet Controller");
59*432ae724SEmmanuel Vadot 
60*432ae724SEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
61*432ae724SEmmanuel Vadot }
62*432ae724SEmmanuel Vadot 
63*432ae724SEmmanuel Vadot static int
64*432ae724SEmmanuel Vadot a20_if_dwc_init(device_t dev)
65*432ae724SEmmanuel Vadot {
66*432ae724SEmmanuel Vadot 	struct dwc_softc *sc;
67*432ae724SEmmanuel Vadot 	const char *tx_parent_name;
68*432ae724SEmmanuel Vadot 	clk_t clk_tx, clk_tx_parent;
69*432ae724SEmmanuel Vadot 	regulator_t reg;
70*432ae724SEmmanuel Vadot 	int error;
71*432ae724SEmmanuel Vadot 
72*432ae724SEmmanuel Vadot 	sc = device_get_softc(dev);
73*432ae724SEmmanuel Vadot 
74*432ae724SEmmanuel Vadot 	/* Configure PHY for MII or RGMII mode */
75*432ae724SEmmanuel Vadot 	switch(sc->phy_mode) {
76*432ae724SEmmanuel Vadot 	case PHY_MODE_RGMII:
77*432ae724SEmmanuel Vadot 		tx_parent_name = "gmac_int_tx";
78*432ae724SEmmanuel Vadot 		break;
79*432ae724SEmmanuel Vadot 	case PHY_MODE_MII:
80*432ae724SEmmanuel Vadot 		tx_parent_name = "mii_phy_tx";
81*432ae724SEmmanuel Vadot 		break;
82*432ae724SEmmanuel Vadot 	default:
83*432ae724SEmmanuel Vadot 		device_printf(dev, "unsupported PHY connection type: %d",
84*432ae724SEmmanuel Vadot 		    sc->phy_mode);
85*432ae724SEmmanuel Vadot 		return (ENXIO);
86*432ae724SEmmanuel Vadot 	}
87*432ae724SEmmanuel Vadot 
88*432ae724SEmmanuel Vadot 	error = clk_get_by_ofw_name(dev, 0, "allwinner_gmac_tx", &clk_tx);
89*432ae724SEmmanuel Vadot 	if (error != 0) {
90*432ae724SEmmanuel Vadot 		device_printf(dev, "could not get tx clk\n");
91*432ae724SEmmanuel Vadot 		return (error);
92*432ae724SEmmanuel Vadot 	}
93*432ae724SEmmanuel Vadot 	error = clk_get_by_name(dev, tx_parent_name, &clk_tx_parent);
94*432ae724SEmmanuel Vadot 	if (error != 0) {
95*432ae724SEmmanuel Vadot 		device_printf(dev, "could not get clock '%s'\n",
96*432ae724SEmmanuel Vadot 		    tx_parent_name);
97*432ae724SEmmanuel Vadot 		return (error);
98*432ae724SEmmanuel Vadot 	}
99*432ae724SEmmanuel Vadot 	error = clk_set_parent_by_clk(clk_tx, clk_tx_parent);
100*432ae724SEmmanuel Vadot 	if (error != 0) {
101*432ae724SEmmanuel Vadot 		device_printf(dev, "could not set tx clk parent\n");
102*432ae724SEmmanuel Vadot 		return (error);
103*432ae724SEmmanuel Vadot 	}
104*432ae724SEmmanuel Vadot 
105*432ae724SEmmanuel Vadot 	/* Enable PHY regulator if applicable */
106*432ae724SEmmanuel Vadot 	if (regulator_get_by_ofw_property(dev, 0, "phy-supply", &reg) == 0) {
107*432ae724SEmmanuel Vadot 		error = regulator_enable(reg);
108*432ae724SEmmanuel Vadot 		if (error != 0) {
109*432ae724SEmmanuel Vadot 			device_printf(dev, "could not enable PHY regulator\n");
110*432ae724SEmmanuel Vadot 			return (error);
111*432ae724SEmmanuel Vadot 		}
112*432ae724SEmmanuel Vadot 	}
113*432ae724SEmmanuel Vadot 
114*432ae724SEmmanuel Vadot 	return (0);
115*432ae724SEmmanuel Vadot }
116*432ae724SEmmanuel Vadot 
117*432ae724SEmmanuel Vadot static int
118*432ae724SEmmanuel Vadot a20_if_dwc_mac_type(device_t dev)
119*432ae724SEmmanuel Vadot {
120*432ae724SEmmanuel Vadot 
121*432ae724SEmmanuel Vadot 	return (DWC_GMAC_NORMAL_DESC);
122*432ae724SEmmanuel Vadot }
123*432ae724SEmmanuel Vadot 
124*432ae724SEmmanuel Vadot static int
125*432ae724SEmmanuel Vadot a20_if_dwc_mii_clk(device_t dev)
126*432ae724SEmmanuel Vadot {
127*432ae724SEmmanuel Vadot 
128*432ae724SEmmanuel Vadot 	return (GMAC_MII_CLK_150_250M_DIV102);
129*432ae724SEmmanuel Vadot }
130*432ae724SEmmanuel Vadot 
131*432ae724SEmmanuel Vadot static device_method_t a20_dwc_methods[] = {
132*432ae724SEmmanuel Vadot 	DEVMETHOD(device_probe,		a20_if_dwc_probe),
133*432ae724SEmmanuel Vadot 
134*432ae724SEmmanuel Vadot 	DEVMETHOD(if_dwc_init,		a20_if_dwc_init),
135*432ae724SEmmanuel Vadot 	DEVMETHOD(if_dwc_mac_type,	a20_if_dwc_mac_type),
136*432ae724SEmmanuel Vadot 	DEVMETHOD(if_dwc_mii_clk,	a20_if_dwc_mii_clk),
137*432ae724SEmmanuel Vadot 
138*432ae724SEmmanuel Vadot 	DEVMETHOD_END
139*432ae724SEmmanuel Vadot };
140*432ae724SEmmanuel Vadot 
141*432ae724SEmmanuel Vadot extern driver_t dwc_driver;
142*432ae724SEmmanuel Vadot 
143*432ae724SEmmanuel Vadot DEFINE_CLASS_1(dwc, a20_dwc_driver, a20_dwc_methods, sizeof(struct dwc_softc),
144*432ae724SEmmanuel Vadot     dwc_driver);
145*432ae724SEmmanuel Vadot DRIVER_MODULE(a20_dwc, simplebus, a20_dwc_driver, 0, 0);
146*432ae724SEmmanuel Vadot 
147*432ae724SEmmanuel Vadot MODULE_DEPEND(a20_dwc, dwc, 1, 1, 1);
148