]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/allwinner/aw_if_dwc.c
sqlite3: Vendor import of sqlite3 3.43.1
[FreeBSD/FreeBSD.git] / sys / arm / allwinner / aw_if_dwc.c
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 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/socket.h>
33 #include <sys/module.h>
34
35 #include <net/if.h>
36
37 #include <machine/bus.h>
38
39 #include <dev/dwc/if_dwc.h>
40 #include <dev/dwc/if_dwcvar.h>
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43
44 #include <arm/allwinner/aw_machdep.h>
45 #include <dev/extres/clk/clk.h>
46 #include <dev/extres/regulator/regulator.h>
47
48 #include "if_dwc_if.h"
49
50 static int
51 a20_if_dwc_probe(device_t dev)
52 {
53
54         if (!ofw_bus_status_okay(dev))
55                 return (ENXIO);
56         if (!ofw_bus_is_compatible(dev, "allwinner,sun7i-a20-gmac"))
57                 return (ENXIO);
58         device_set_desc(dev, "A20 Gigabit Ethernet Controller");
59
60         return (BUS_PROBE_DEFAULT);
61 }
62
63 static int
64 a20_if_dwc_init(device_t dev)
65 {
66         struct dwc_softc *sc;
67         const char *tx_parent_name;
68         clk_t clk_tx, clk_tx_parent;
69         regulator_t reg;
70         int error;
71
72         sc = device_get_softc(dev);
73
74         /* Configure PHY for MII or RGMII mode */
75         switch(sc->phy_mode) {
76         case PHY_MODE_RGMII:
77                 tx_parent_name = "gmac_int_tx";
78                 break;
79         case PHY_MODE_MII:
80                 tx_parent_name = "mii_phy_tx";
81                 break;
82         default:
83                 device_printf(dev, "unsupported PHY connection type: %d",
84                     sc->phy_mode);
85                 return (ENXIO);
86         }
87
88         error = clk_get_by_ofw_name(dev, 0, "allwinner_gmac_tx", &clk_tx);
89         if (error != 0) {
90                 device_printf(dev, "could not get tx clk\n");
91                 return (error);
92         }
93         error = clk_get_by_name(dev, tx_parent_name, &clk_tx_parent);
94         if (error != 0) {
95                 device_printf(dev, "could not get clock '%s'\n",
96                     tx_parent_name);
97                 return (error);
98         }
99         error = clk_set_parent_by_clk(clk_tx, clk_tx_parent);
100         if (error != 0) {
101                 device_printf(dev, "could not set tx clk parent\n");
102                 return (error);
103         }
104
105         /* Enable PHY regulator if applicable */
106         if (regulator_get_by_ofw_property(dev, 0, "phy-supply", &reg) == 0) {
107                 error = regulator_enable(reg);
108                 if (error != 0) {
109                         device_printf(dev, "could not enable PHY regulator\n");
110                         return (error);
111                 }
112         }
113
114         return (0);
115 }
116
117 static int
118 a20_if_dwc_mac_type(device_t dev)
119 {
120
121         return (DWC_GMAC_NORMAL_DESC);
122 }
123
124 static int
125 a20_if_dwc_mii_clk(device_t dev)
126 {
127
128         return (GMAC_MII_CLK_150_250M_DIV102);
129 }
130
131 static device_method_t a20_dwc_methods[] = {
132         DEVMETHOD(device_probe,         a20_if_dwc_probe),
133
134         DEVMETHOD(if_dwc_init,          a20_if_dwc_init),
135         DEVMETHOD(if_dwc_mac_type,      a20_if_dwc_mac_type),
136         DEVMETHOD(if_dwc_mii_clk,       a20_if_dwc_mii_clk),
137
138         DEVMETHOD_END
139 };
140
141 extern driver_t dwc_driver;
142
143 DEFINE_CLASS_1(dwc, a20_dwc_driver, a20_dwc_methods, sizeof(struct dwc_softc),
144     dwc_driver);
145 DRIVER_MODULE(a20_dwc, simplebus, a20_dwc_driver, 0, 0);
146
147 MODULE_DEPEND(a20_dwc, dwc, 1, 1, 1);