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