meson_dwmac.c revision 1.1.2.2 1 1.1.2.2 pgoyette /* $NetBSD: meson_dwmac.c,v 1.1.2.2 2019/01/26 21:59:59 pgoyette Exp $ */
2 1.1.2.2 pgoyette
3 1.1.2.2 pgoyette /*-
4 1.1.2.2 pgoyette * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1.2.2 pgoyette * All rights reserved.
6 1.1.2.2 pgoyette *
7 1.1.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 pgoyette * modification, are permitted provided that the following conditions
9 1.1.2.2 pgoyette * are met:
10 1.1.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 pgoyette * documentation and/or other materials provided with the distribution.
15 1.1.2.2 pgoyette *
16 1.1.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.2.2 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.2.2 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.2.2 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.2.2 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.2.2 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.2.2 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.2.2 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.2.2 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.2.2 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.2.2 pgoyette * POSSIBILITY OF SUCH DAMAGE.
27 1.1.2.2 pgoyette */
28 1.1.2.2 pgoyette
29 1.1.2.2 pgoyette #include <sys/cdefs.h>
30 1.1.2.2 pgoyette
31 1.1.2.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: meson_dwmac.c,v 1.1.2.2 2019/01/26 21:59:59 pgoyette Exp $");
32 1.1.2.2 pgoyette
33 1.1.2.2 pgoyette #include <sys/param.h>
34 1.1.2.2 pgoyette #include <sys/bus.h>
35 1.1.2.2 pgoyette #include <sys/device.h>
36 1.1.2.2 pgoyette #include <sys/intr.h>
37 1.1.2.2 pgoyette #include <sys/systm.h>
38 1.1.2.2 pgoyette #include <sys/gpio.h>
39 1.1.2.2 pgoyette
40 1.1.2.2 pgoyette #include <net/if.h>
41 1.1.2.2 pgoyette #include <net/if_ether.h>
42 1.1.2.2 pgoyette #include <net/if_media.h>
43 1.1.2.2 pgoyette
44 1.1.2.2 pgoyette #include <dev/mii/miivar.h>
45 1.1.2.2 pgoyette
46 1.1.2.2 pgoyette #include <dev/ic/dwc_gmac_var.h>
47 1.1.2.2 pgoyette #include <dev/ic/dwc_gmac_reg.h>
48 1.1.2.2 pgoyette
49 1.1.2.2 pgoyette #include <dev/fdt/fdtvar.h>
50 1.1.2.2 pgoyette
51 1.1.2.2 pgoyette #define GMAC_TX_RATE_MII 25000000
52 1.1.2.2 pgoyette #define GMAC_TX_RATE_RGMII 125000000
53 1.1.2.2 pgoyette
54 1.1.2.2 pgoyette static const char * compatible[] = {
55 1.1.2.2 pgoyette "amlogic,meson8b-dwmac",
56 1.1.2.2 pgoyette NULL
57 1.1.2.2 pgoyette };
58 1.1.2.2 pgoyette
59 1.1.2.2 pgoyette static int
60 1.1.2.2 pgoyette meson_dwmac_reset(const int phandle)
61 1.1.2.2 pgoyette {
62 1.1.2.2 pgoyette #if notyet
63 1.1.2.2 pgoyette struct fdtbus_gpio_pin *pin_reset;
64 1.1.2.2 pgoyette const u_int *reset_delay_us;
65 1.1.2.2 pgoyette bool reset_active_low;
66 1.1.2.2 pgoyette int len, val;
67 1.1.2.2 pgoyette
68 1.1.2.2 pgoyette pin_reset = fdtbus_gpio_acquire(phandle, "snps,reset-gpio", GPIO_PIN_OUTPUT);
69 1.1.2.2 pgoyette if (pin_reset == NULL)
70 1.1.2.2 pgoyette return 0;
71 1.1.2.2 pgoyette
72 1.1.2.2 pgoyette reset_delay_us = fdtbus_get_prop(phandle, "snps,reset-delays-us", &len);
73 1.1.2.2 pgoyette if (reset_delay_us == NULL || len != 12)
74 1.1.2.2 pgoyette return ENXIO;
75 1.1.2.2 pgoyette
76 1.1.2.2 pgoyette reset_active_low = of_hasprop(phandle, "snps,reset-active-low");
77 1.1.2.2 pgoyette
78 1.1.2.2 pgoyette val = reset_active_low ? 1 : 0;
79 1.1.2.2 pgoyette
80 1.1.2.2 pgoyette fdtbus_gpio_write(pin_reset, val);
81 1.1.2.2 pgoyette delay(be32toh(reset_delay_us[0]));
82 1.1.2.2 pgoyette fdtbus_gpio_write(pin_reset, !val);
83 1.1.2.2 pgoyette delay(be32toh(reset_delay_us[1]));
84 1.1.2.2 pgoyette fdtbus_gpio_write(pin_reset, val);
85 1.1.2.2 pgoyette delay(be32toh(reset_delay_us[2]));
86 1.1.2.2 pgoyette #endif
87 1.1.2.2 pgoyette
88 1.1.2.2 pgoyette return 0;
89 1.1.2.2 pgoyette }
90 1.1.2.2 pgoyette
91 1.1.2.2 pgoyette static int
92 1.1.2.2 pgoyette meson_dwmac_intr(void *arg)
93 1.1.2.2 pgoyette {
94 1.1.2.2 pgoyette struct dwc_gmac_softc * const sc = arg;
95 1.1.2.2 pgoyette
96 1.1.2.2 pgoyette return dwc_gmac_intr(sc);
97 1.1.2.2 pgoyette }
98 1.1.2.2 pgoyette
99 1.1.2.2 pgoyette static int
100 1.1.2.2 pgoyette meson_dwmac_match(device_t parent, cfdata_t cf, void *aux)
101 1.1.2.2 pgoyette {
102 1.1.2.2 pgoyette struct fdt_attach_args * const faa = aux;
103 1.1.2.2 pgoyette
104 1.1.2.2 pgoyette return of_match_compatible(faa->faa_phandle, compatible);
105 1.1.2.2 pgoyette }
106 1.1.2.2 pgoyette
107 1.1.2.2 pgoyette static void
108 1.1.2.2 pgoyette meson_dwmac_attach(device_t parent, device_t self, void *aux)
109 1.1.2.2 pgoyette {
110 1.1.2.2 pgoyette struct dwc_gmac_softc * const sc = device_private(self);
111 1.1.2.2 pgoyette struct fdt_attach_args * const faa = aux;
112 1.1.2.2 pgoyette const int phandle = faa->faa_phandle;
113 1.1.2.2 pgoyette struct fdtbus_reset *rst_gmac;
114 1.1.2.2 pgoyette struct clk *clk_gmac;
115 1.1.2.2 pgoyette const char *phy_mode;
116 1.1.2.2 pgoyette char intrstr[128];
117 1.1.2.2 pgoyette bus_addr_t addr;
118 1.1.2.2 pgoyette bus_size_t size;
119 1.1.2.2 pgoyette
120 1.1.2.2 pgoyette if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
121 1.1.2.2 pgoyette aprint_error(": couldn't get registers\n");
122 1.1.2.2 pgoyette return;
123 1.1.2.2 pgoyette }
124 1.1.2.2 pgoyette
125 1.1.2.2 pgoyette sc->sc_dev = self;
126 1.1.2.2 pgoyette sc->sc_bst = faa->faa_bst;
127 1.1.2.2 pgoyette if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
128 1.1.2.2 pgoyette aprint_error(": couldn't map registers\n");
129 1.1.2.2 pgoyette return;
130 1.1.2.2 pgoyette }
131 1.1.2.2 pgoyette sc->sc_dmat = faa->faa_dmat;
132 1.1.2.2 pgoyette
133 1.1.2.2 pgoyette if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
134 1.1.2.2 pgoyette aprint_error(": failed to decode interrupt\n");
135 1.1.2.2 pgoyette return;
136 1.1.2.2 pgoyette }
137 1.1.2.2 pgoyette
138 1.1.2.2 pgoyette clk_gmac = fdtbus_clock_get(phandle, "stmmaceth");
139 1.1.2.2 pgoyette if (clk_gmac == NULL) {
140 1.1.2.2 pgoyette aprint_error(": couldn't get clocks\n");
141 1.1.2.2 pgoyette return;
142 1.1.2.2 pgoyette }
143 1.1.2.2 pgoyette
144 1.1.2.2 pgoyette rst_gmac = fdtbus_reset_get(phandle, "stmmaceth");
145 1.1.2.2 pgoyette
146 1.1.2.2 pgoyette phy_mode = fdtbus_get_string(phandle, "phy-mode");
147 1.1.2.2 pgoyette if (phy_mode == NULL) {
148 1.1.2.2 pgoyette aprint_error(": missing 'phy-mode' property\n");
149 1.1.2.2 pgoyette return;
150 1.1.2.2 pgoyette }
151 1.1.2.2 pgoyette #if notyet
152 1.1.2.2 pgoyette if (strcmp(phy_mode, "mii") == 0) {
153 1.1.2.2 pgoyette if (clk_set_rate(clk_gmac_tx, GMAC_TX_RATE_MII) != 0) {
154 1.1.2.2 pgoyette aprint_error(": failed to set TX clock rate (MII)\n");
155 1.1.2.2 pgoyette return;
156 1.1.2.2 pgoyette }
157 1.1.2.2 pgoyette } else if (strcmp(phy_mode, "rgmii") == 0) {
158 1.1.2.2 pgoyette if (clk_set_rate(clk_gmac_tx, GMAC_TX_RATE_RGMII) != 0) {
159 1.1.2.2 pgoyette aprint_error(": failed to set TX clock rate (RGMII)\n");
160 1.1.2.2 pgoyette return;
161 1.1.2.2 pgoyette }
162 1.1.2.2 pgoyette } else {
163 1.1.2.2 pgoyette aprint_error(": unsupported phy-mode '%s'\n", phy_mode);
164 1.1.2.2 pgoyette return;
165 1.1.2.2 pgoyette }
166 1.1.2.2 pgoyette #endif
167 1.1.2.2 pgoyette
168 1.1.2.2 pgoyette if (clk_enable(clk_gmac) != 0) {
169 1.1.2.2 pgoyette aprint_error(": couldn't enable clock\n");
170 1.1.2.2 pgoyette return;
171 1.1.2.2 pgoyette }
172 1.1.2.2 pgoyette
173 1.1.2.2 pgoyette if (rst_gmac != NULL && fdtbus_reset_deassert(rst_gmac) != 0) {
174 1.1.2.2 pgoyette aprint_error(": couldn't de-assert reset\n");
175 1.1.2.2 pgoyette return;
176 1.1.2.2 pgoyette }
177 1.1.2.2 pgoyette
178 1.1.2.2 pgoyette aprint_naive("\n");
179 1.1.2.2 pgoyette aprint_normal(": Gigabit Ethernet Controller\n");
180 1.1.2.2 pgoyette
181 1.1.2.2 pgoyette if (fdtbus_intr_establish(phandle, 0, IPL_NET, 0, meson_dwmac_intr, sc) == NULL) {
182 1.1.2.2 pgoyette aprint_error_dev(self, "failed to establish interrupt on %s\n", intrstr);
183 1.1.2.2 pgoyette return;
184 1.1.2.2 pgoyette }
185 1.1.2.2 pgoyette aprint_normal_dev(self, "interrupting on %s\n", intrstr);
186 1.1.2.2 pgoyette
187 1.1.2.2 pgoyette if (meson_dwmac_reset(phandle) != 0)
188 1.1.2.2 pgoyette aprint_error_dev(self, "PHY reset failed\n");
189 1.1.2.2 pgoyette
190 1.1.2.2 pgoyette dwc_gmac_attach(sc, GMAC_MII_CLK_100_150M_DIV62);
191 1.1.2.2 pgoyette }
192 1.1.2.2 pgoyette
193 1.1.2.2 pgoyette CFATTACH_DECL_NEW(meson_dwmac, sizeof(struct dwc_gmac_softc),
194 1.1.2.2 pgoyette meson_dwmac_match, meson_dwmac_attach, NULL, NULL);
195