1288050Sloos/*-
2288050Sloos * Copyright (c) 2015 Luiz Otavio O Souza <loos@FreeBSD.org>
3288050Sloos * All rights reserved.
4288050Sloos *
5288050Sloos * Redistribution and use in source and binary forms, with or without
6288050Sloos * modification, are permitted provided that the following conditions
7288050Sloos * are met:
8288050Sloos * 1. Redistributions of source code must retain the above copyright
9288050Sloos *    notice, this list of conditions and the following disclaimer.
10288050Sloos * 2. Redistributions in binary form must reproduce the above copyright
11288050Sloos *    notice, this list of conditions and the following disclaimer in the
12288050Sloos *    documentation and/or other materials provided with the distribution.
13288050Sloos *
14288050Sloos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15288050Sloos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16288050Sloos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17288050Sloos * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18288050Sloos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19288050Sloos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20288050Sloos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21288050Sloos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22288050Sloos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23288050Sloos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24288050Sloos * SUCH DAMAGE.
25288050Sloos */
26288050Sloos
27288050Sloos#include <sys/cdefs.h>
28288050Sloos__FBSDID("$FreeBSD: releng/11.0/sys/arm/allwinner/aw_if_dwc.c 298422 2016-04-21 16:49:04Z jmcneill $");
29288050Sloos
30288050Sloos#include <sys/param.h>
31288050Sloos#include <sys/systm.h>
32288050Sloos#include <sys/bus.h>
33288050Sloos#include <sys/kernel.h>
34288050Sloos#include <sys/module.h>
35288050Sloos
36288050Sloos#include <machine/bus.h>
37288050Sloos
38288050Sloos#include <dev/dwc/if_dwc.h>
39288050Sloos#include <dev/dwc/if_dwcvar.h>
40288050Sloos#include <dev/ofw/ofw_bus.h>
41288050Sloos#include <dev/ofw/ofw_bus_subr.h>
42288050Sloos
43296093Sandrew#include <arm/allwinner/allwinner_machdep.h>
44297627Sjmcneill#include <dev/extres/clk/clk.h>
45297739Sjmcneill#include <dev/extres/regulator/regulator.h>
46288050Sloos
47288050Sloos#include "if_dwc_if.h"
48288050Sloos
49288050Sloosstatic int
50288050Sloosa20_if_dwc_probe(device_t dev)
51288050Sloos{
52288050Sloos
53288050Sloos	if (!ofw_bus_status_okay(dev))
54288050Sloos		return (ENXIO);
55288050Sloos	if (!ofw_bus_is_compatible(dev, "allwinner,sun7i-a20-gmac"))
56288050Sloos		return (ENXIO);
57288050Sloos	device_set_desc(dev, "A20 Gigabit Ethernet Controller");
58288050Sloos
59288050Sloos	return (BUS_PROBE_DEFAULT);
60288050Sloos}
61288050Sloos
62288050Sloosstatic int
63288050Sloosa20_if_dwc_init(device_t dev)
64288050Sloos{
65297627Sjmcneill	const char *tx_parent_name;
66297627Sjmcneill	char *phy_type;
67297627Sjmcneill	clk_t clk_tx, clk_tx_parent;
68297739Sjmcneill	regulator_t reg;
69297627Sjmcneill	phandle_t node;
70297627Sjmcneill	int error;
71288050Sloos
72297627Sjmcneill	node = ofw_bus_get_node(dev);
73297627Sjmcneill
74297627Sjmcneill	/* Configure PHY for MII or RGMII mode */
75297627Sjmcneill	if (OF_getprop_alloc(node, "phy-mode", 1, (void **)&phy_type)) {
76297627Sjmcneill		error = clk_get_by_ofw_name(dev, "allwinner_gmac_tx", &clk_tx);
77297627Sjmcneill		if (error != 0) {
78297627Sjmcneill			device_printf(dev, "could not get tx clk\n");
79297627Sjmcneill			return (error);
80297627Sjmcneill		}
81297627Sjmcneill
82297627Sjmcneill		if (strcmp(phy_type, "rgmii") == 0)
83297627Sjmcneill			tx_parent_name = "gmac_int_tx";
84297627Sjmcneill		else
85297627Sjmcneill			tx_parent_name = "mii_phy_tx";
86297627Sjmcneill
87297627Sjmcneill		error = clk_get_by_name(dev, tx_parent_name, &clk_tx_parent);
88297627Sjmcneill		if (error != 0) {
89297627Sjmcneill			device_printf(dev, "could not get clock '%s'\n",
90297627Sjmcneill			    tx_parent_name);
91297627Sjmcneill			return (error);
92297627Sjmcneill		}
93297627Sjmcneill
94297627Sjmcneill		error = clk_set_parent_by_clk(clk_tx, clk_tx_parent);
95297627Sjmcneill		if (error != 0) {
96297627Sjmcneill			device_printf(dev, "could not set tx clk parent\n");
97297627Sjmcneill			return (error);
98297627Sjmcneill		}
99296093Sandrew	}
100288050Sloos
101297739Sjmcneill	/* Enable PHY regulator if applicable */
102297739Sjmcneill	if (regulator_get_by_ofw_property(dev, "phy-supply", &reg) == 0) {
103297739Sjmcneill		error = regulator_enable(reg);
104297739Sjmcneill		if (error != 0) {
105297739Sjmcneill			device_printf(dev, "could not enable PHY regulator\n");
106297739Sjmcneill			return (error);
107297739Sjmcneill		}
108297739Sjmcneill	}
109297739Sjmcneill
110288050Sloos	return (0);
111288050Sloos}
112288050Sloos
113288050Sloosstatic int
114288050Sloosa20_if_dwc_mac_type(device_t dev)
115288050Sloos{
116288050Sloos
117288050Sloos	return (DWC_GMAC_ALT_DESC);
118288050Sloos}
119288050Sloos
120288050Sloosstatic int
121288050Sloosa20_if_dwc_mii_clk(device_t dev)
122288050Sloos{
123288050Sloos
124288050Sloos	return (GMAC_MII_CLK_150_250M_DIV102);
125288050Sloos}
126288050Sloos
127288050Sloosstatic device_method_t a20_dwc_methods[] = {
128288050Sloos	DEVMETHOD(device_probe,		a20_if_dwc_probe),
129288050Sloos
130288050Sloos	DEVMETHOD(if_dwc_init,		a20_if_dwc_init),
131288050Sloos	DEVMETHOD(if_dwc_mac_type,	a20_if_dwc_mac_type),
132288050Sloos	DEVMETHOD(if_dwc_mii_clk,	a20_if_dwc_mii_clk),
133288050Sloos
134288050Sloos	DEVMETHOD_END
135288050Sloos};
136288050Sloos
137288050Sloosstatic devclass_t a20_dwc_devclass;
138288050Sloos
139288050Sloosextern driver_t dwc_driver;
140288050Sloos
141288050SloosDEFINE_CLASS_1(dwc, a20_dwc_driver, a20_dwc_methods, sizeof(struct dwc_softc),
142288050Sloos    dwc_driver);
143288050SloosDRIVER_MODULE(a20_dwc, simplebus, a20_dwc_driver, a20_dwc_devclass, 0, 0);
144288050Sloos
145288050SloosMODULE_DEPEND(a20_dwc, dwc, 1, 1, 1);
146