if_enet_imx.c revision 1.1.2.3 1 1.1.2.3 thorpej /* $NetBSD: if_enet_imx.c,v 1.1.2.3 2021/04/03 22:28:17 thorpej Exp $ */
2 1.1.2.2 thorpej
3 1.1.2.2 thorpej /*-
4 1.1.2.2 thorpej * Copyright (c) 2019 Genetec Corporation. All rights reserved.
5 1.1.2.2 thorpej * Written by Hashimoto Kenichi for Genetec Corporation.
6 1.1.2.2 thorpej *
7 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
9 1.1.2.2 thorpej * are met:
10 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
15 1.1.2.2 thorpej *
16 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.2.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.2.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.2.2 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.2.2 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1.2.2 thorpej * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1.2.2 thorpej * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1.2.2 thorpej * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1.2.2 thorpej * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.2.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.2.2 thorpej * SUCH DAMAGE.
27 1.1.2.2 thorpej */
28 1.1.2.2 thorpej
29 1.1.2.2 thorpej #include <sys/cdefs.h>
30 1.1.2.3 thorpej __KERNEL_RCSID(0, "$NetBSD: if_enet_imx.c,v 1.1.2.3 2021/04/03 22:28:17 thorpej Exp $");
31 1.1.2.2 thorpej
32 1.1.2.2 thorpej #include "opt_fdt.h"
33 1.1.2.2 thorpej
34 1.1.2.2 thorpej #include <sys/param.h>
35 1.1.2.2 thorpej #include <sys/bus.h>
36 1.1.2.2 thorpej #include <sys/device.h>
37 1.1.2.2 thorpej
38 1.1.2.2 thorpej #include <arm/imx/if_enetreg.h>
39 1.1.2.2 thorpej #include <arm/imx/if_enetvar.h>
40 1.1.2.2 thorpej
41 1.1.2.2 thorpej #include <dev/fdt/fdtvar.h>
42 1.1.2.2 thorpej
43 1.1.2.2 thorpej struct enet_fdt_softc {
44 1.1.2.2 thorpej struct enet_softc sc_enet;
45 1.1.2.2 thorpej
46 1.1.2.2 thorpej struct fdtbus_gpio_pin *sc_pin_reset;
47 1.1.2.2 thorpej };
48 1.1.2.2 thorpej
49 1.1.2.2 thorpej CFATTACH_DECL_NEW(enet_fdt, sizeof(struct enet_fdt_softc),
50 1.1.2.2 thorpej enet_match, enet_attach, NULL, NULL);
51 1.1.2.2 thorpej
52 1.1.2.3 thorpej static const struct device_compatible_entry compat_data[] = {
53 1.1.2.2 thorpej /* compatible imxtype */
54 1.1.2.3 thorpej { .compat = "fsl,imx6q-fec", .value = 6 },
55 1.1.2.3 thorpej { .compat = "fsl,imx6sx-fec", .value = 7 },
56 1.1.2.3 thorpej DEVICE_COMPAT_EOL
57 1.1.2.2 thorpej };
58 1.1.2.2 thorpej
59 1.1.2.2 thorpej static int enet_init_clocks(struct enet_softc *);
60 1.1.2.2 thorpej static void enet_phy_reset(struct enet_fdt_softc *, const int);
61 1.1.2.2 thorpej static int enet_phy_id(struct enet_softc *, const int);
62 1.1.2.2 thorpej static void *enet_intr_establish(struct enet_softc *, int, u_int);
63 1.1.2.2 thorpej
64 1.1.2.2 thorpej int
65 1.1.2.2 thorpej enet_match(device_t parent, cfdata_t cf, void *aux)
66 1.1.2.2 thorpej {
67 1.1.2.2 thorpej struct fdt_attach_args * const faa = aux;
68 1.1.2.2 thorpej
69 1.1.2.3 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
70 1.1.2.2 thorpej }
71 1.1.2.2 thorpej
72 1.1.2.2 thorpej void
73 1.1.2.2 thorpej enet_attach(device_t parent, device_t self, void *aux)
74 1.1.2.2 thorpej {
75 1.1.2.2 thorpej struct enet_fdt_softc * const efsc = device_private(self);
76 1.1.2.2 thorpej struct enet_softc *sc = &efsc->sc_enet;
77 1.1.2.2 thorpej struct fdt_attach_args * const faa = aux;
78 1.1.2.2 thorpej prop_dictionary_t prop = device_properties(self);
79 1.1.2.2 thorpej const int phandle = faa->faa_phandle;
80 1.1.2.2 thorpej bus_space_tag_t bst = faa->faa_bst;
81 1.1.2.2 thorpej bus_space_handle_t bsh;
82 1.1.2.2 thorpej bus_addr_t addr;
83 1.1.2.2 thorpej bus_size_t size;
84 1.1.2.2 thorpej int error;
85 1.1.2.2 thorpej
86 1.1.2.2 thorpej if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
87 1.1.2.2 thorpej aprint_error(": couldn't get enet registers\n");
88 1.1.2.2 thorpej return;
89 1.1.2.2 thorpej }
90 1.1.2.2 thorpej
91 1.1.2.2 thorpej error = bus_space_map(bst, addr, size, 0, &bsh);
92 1.1.2.2 thorpej if (error) {
93 1.1.2.2 thorpej aprint_error(": couldn't map enet registers: %d\n", error);
94 1.1.2.2 thorpej return;
95 1.1.2.2 thorpej }
96 1.1.2.2 thorpej
97 1.1.2.2 thorpej sc->sc_clk_ipg = fdtbus_clock_get(phandle, "ipg");
98 1.1.2.2 thorpej if (sc->sc_clk_ipg == NULL) {
99 1.1.2.2 thorpej aprint_error(": couldn't get clock ipg\n");
100 1.1.2.2 thorpej goto failure;
101 1.1.2.2 thorpej }
102 1.1.2.2 thorpej sc->sc_clk_enet = fdtbus_clock_get(phandle, "ahb");
103 1.1.2.2 thorpej if (sc->sc_clk_enet == NULL) {
104 1.1.2.2 thorpej aprint_error(": couldn't get clock ahb\n");
105 1.1.2.2 thorpej goto failure;
106 1.1.2.2 thorpej }
107 1.1.2.2 thorpej sc->sc_clk_enet_ref = fdtbus_clock_get(phandle, "ptp");
108 1.1.2.2 thorpej if (sc->sc_clk_enet_ref == NULL) {
109 1.1.2.2 thorpej aprint_error(": couldn't get clock ptp\n");
110 1.1.2.2 thorpej goto failure;
111 1.1.2.2 thorpej }
112 1.1.2.2 thorpej
113 1.1.2.2 thorpej if (fdtbus_clock_enable(phandle, "enet_clk_ref", false) != 0) {
114 1.1.2.2 thorpej aprint_error(": couldn't enable clock enet_clk_ref\n");
115 1.1.2.2 thorpej goto failure;
116 1.1.2.2 thorpej }
117 1.1.2.2 thorpej if (fdtbus_clock_enable(phandle, "enet_out", false) != 0) {
118 1.1.2.2 thorpej aprint_error(": couldn't enable clock enet_out\n");
119 1.1.2.2 thorpej goto failure;
120 1.1.2.2 thorpej }
121 1.1.2.2 thorpej
122 1.1.2.2 thorpej aprint_naive("\n");
123 1.1.2.2 thorpej aprint_normal(": Gigabit Ethernet Controller\n");
124 1.1.2.2 thorpej
125 1.1.2.2 thorpej sc->sc_dev = self;
126 1.1.2.2 thorpej sc->sc_iot = bst;
127 1.1.2.2 thorpej sc->sc_ioh = bsh;
128 1.1.2.2 thorpej sc->sc_dmat = faa->faa_dmat;
129 1.1.2.2 thorpej
130 1.1.2.3 thorpej sc->sc_imxtype = of_compatible_lookup(phandle, compat_data)->value;
131 1.1.2.2 thorpej sc->sc_unit = 0;
132 1.1.2.2 thorpej sc->sc_phyid = enet_phy_id(sc, phandle);
133 1.1.2.2 thorpej
134 1.1.2.2 thorpej const char *phy_mode = fdtbus_get_string(phandle, "phy-mode");
135 1.1.2.2 thorpej if (phy_mode == NULL) {
136 1.1.2.2 thorpej aprint_error(": missing 'phy-mode' property\n");
137 1.1.2.2 thorpej goto failure;
138 1.1.2.2 thorpej }
139 1.1.2.2 thorpej
140 1.1.2.2 thorpej if (strcmp(phy_mode, "rgmii-txid") == 0) {
141 1.1.2.2 thorpej prop_dictionary_set_bool(prop, "tx_internal_delay", true);
142 1.1.2.2 thorpej sc->sc_rgmii = 1;
143 1.1.2.2 thorpej } else if (strcmp(phy_mode, "rgmii-rxid") == 0) {
144 1.1.2.2 thorpej prop_dictionary_set_bool(prop, "rx_internal_delay", true);
145 1.1.2.2 thorpej sc->sc_rgmii = 1;
146 1.1.2.2 thorpej } else if (strcmp(phy_mode, "rgmii-id") == 0) {
147 1.1.2.2 thorpej prop_dictionary_set_bool(prop, "tx_internal_delay", true);
148 1.1.2.2 thorpej prop_dictionary_set_bool(prop, "rx_internal_delay", true);
149 1.1.2.2 thorpej sc->sc_rgmii = 1;
150 1.1.2.2 thorpej } else if (strcmp(phy_mode, "rgmii") == 0) {
151 1.1.2.2 thorpej sc->sc_rgmii = 1;
152 1.1.2.2 thorpej } else {
153 1.1.2.2 thorpej sc->sc_rgmii = 0;
154 1.1.2.2 thorpej }
155 1.1.2.2 thorpej
156 1.1.2.2 thorpej sc->sc_ih = enet_intr_establish(sc, phandle, 0);
157 1.1.2.2 thorpej if (sc->sc_ih == NULL)
158 1.1.2.2 thorpej goto failure;
159 1.1.2.2 thorpej
160 1.1.2.2 thorpej if (sc->sc_imxtype == 7) {
161 1.1.2.2 thorpej sc->sc_ih2 = enet_intr_establish(sc, phandle, 1);
162 1.1.2.2 thorpej sc->sc_ih3 = enet_intr_establish(sc, phandle, 2);
163 1.1.2.2 thorpej if (sc->sc_ih2 == NULL || sc->sc_ih3 == NULL)
164 1.1.2.2 thorpej goto failure;
165 1.1.2.2 thorpej }
166 1.1.2.2 thorpej
167 1.1.2.2 thorpej enet_init_clocks(sc);
168 1.1.2.2 thorpej sc->sc_clock = clk_get_rate(sc->sc_clk_ipg);
169 1.1.2.2 thorpej
170 1.1.2.2 thorpej enet_phy_reset(efsc, phandle);
171 1.1.2.2 thorpej
172 1.1.2.2 thorpej if (enet_attach_common(self) != 0)
173 1.1.2.2 thorpej goto failure;
174 1.1.2.2 thorpej
175 1.1.2.2 thorpej return;
176 1.1.2.2 thorpej
177 1.1.2.2 thorpej failure:
178 1.1.2.2 thorpej bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
179 1.1.2.2 thorpej return;
180 1.1.2.2 thorpej }
181 1.1.2.2 thorpej
182 1.1.2.2 thorpej static void *
183 1.1.2.2 thorpej enet_intr_establish(struct enet_softc *sc, int phandle, u_int index)
184 1.1.2.2 thorpej {
185 1.1.2.2 thorpej char intrstr[128];
186 1.1.2.3 thorpej char xname[16];
187 1.1.2.2 thorpej void *ih;
188 1.1.2.2 thorpej
189 1.1.2.2 thorpej if (!fdtbus_intr_str(phandle, index, intrstr, sizeof(intrstr))) {
190 1.1.2.2 thorpej aprint_error_dev(sc->sc_dev, "failed to decode interrupt %d\n",
191 1.1.2.2 thorpej index);
192 1.1.2.2 thorpej return NULL;
193 1.1.2.2 thorpej }
194 1.1.2.2 thorpej
195 1.1.2.3 thorpej snprintf(xname, sizeof(xname), "%s #%u", device_xname(sc->sc_dev),
196 1.1.2.3 thorpej index);
197 1.1.2.3 thorpej ih = fdtbus_intr_establish_xname(phandle, index, IPL_NET, 0,
198 1.1.2.3 thorpej enet_intr, sc, xname);
199 1.1.2.2 thorpej if (ih == NULL) {
200 1.1.2.2 thorpej aprint_error_dev(sc->sc_dev, "failed to establish interrupt on %s\n",
201 1.1.2.2 thorpej intrstr);
202 1.1.2.2 thorpej return NULL;
203 1.1.2.2 thorpej }
204 1.1.2.2 thorpej aprint_normal_dev(sc->sc_dev, "interrupting on %s\n", intrstr);
205 1.1.2.2 thorpej
206 1.1.2.2 thorpej return ih;
207 1.1.2.2 thorpej }
208 1.1.2.2 thorpej
209 1.1.2.2 thorpej static int
210 1.1.2.2 thorpej enet_init_clocks(struct enet_softc *sc)
211 1.1.2.2 thorpej {
212 1.1.2.2 thorpej int error;
213 1.1.2.2 thorpej
214 1.1.2.2 thorpej error = clk_enable(sc->sc_clk_ipg);
215 1.1.2.2 thorpej if (error) {
216 1.1.2.2 thorpej aprint_error_dev(sc->sc_dev, "couldn't enable ipg: %d\n", error);
217 1.1.2.2 thorpej return error;
218 1.1.2.2 thorpej }
219 1.1.2.2 thorpej error = clk_enable(sc->sc_clk_enet);
220 1.1.2.2 thorpej if (error) {
221 1.1.2.2 thorpej aprint_error_dev(sc->sc_dev, "couldn't enable enet: %d\n", error);
222 1.1.2.2 thorpej return error;
223 1.1.2.2 thorpej }
224 1.1.2.2 thorpej error = clk_enable(sc->sc_clk_enet_ref);
225 1.1.2.2 thorpej if (error) {
226 1.1.2.2 thorpej aprint_error_dev(sc->sc_dev, "couldn't enable enet_ref: %d\n", error);
227 1.1.2.2 thorpej return error;
228 1.1.2.2 thorpej }
229 1.1.2.2 thorpej
230 1.1.2.2 thorpej return 0;
231 1.1.2.2 thorpej }
232 1.1.2.2 thorpej
233 1.1.2.2 thorpej static void
234 1.1.2.2 thorpej enet_phy_reset(struct enet_fdt_softc *sc, const int phandle)
235 1.1.2.2 thorpej {
236 1.1.2.2 thorpej u_int msec;
237 1.1.2.2 thorpej
238 1.1.2.2 thorpej sc->sc_pin_reset = fdtbus_gpio_acquire(phandle, "phy-reset-gpios", GPIO_PIN_OUTPUT);
239 1.1.2.2 thorpej if (sc->sc_pin_reset == NULL) {
240 1.1.2.2 thorpej aprint_error_dev(sc->sc_enet.sc_dev, "couldn't find phy reset gpios\n");
241 1.1.2.2 thorpej return;
242 1.1.2.2 thorpej }
243 1.1.2.2 thorpej
244 1.1.2.2 thorpej if (of_getprop_uint32(phandle, "phy-reset-duration", &msec))
245 1.1.2.2 thorpej msec = 1;
246 1.1.2.2 thorpej
247 1.1.2.2 thorpej /* Reset */
248 1.1.2.2 thorpej fdtbus_gpio_write(sc->sc_pin_reset, 1);
249 1.1.2.2 thorpej delay(msec * 1000);
250 1.1.2.2 thorpej fdtbus_gpio_write(sc->sc_pin_reset, 0);
251 1.1.2.2 thorpej
252 1.1.2.2 thorpej /* Post delay */
253 1.1.2.2 thorpej if (of_getprop_uint32(phandle, "phy-reset-post-delay", &msec))
254 1.1.2.2 thorpej msec = 0;
255 1.1.2.2 thorpej
256 1.1.2.2 thorpej delay(msec * 1000);
257 1.1.2.2 thorpej }
258 1.1.2.2 thorpej
259 1.1.2.2 thorpej static int
260 1.1.2.2 thorpej enet_phy_id(struct enet_softc *sc, const int phandle)
261 1.1.2.2 thorpej {
262 1.1.2.2 thorpej int phy_phandle;
263 1.1.2.2 thorpej bus_addr_t addr;
264 1.1.2.2 thorpej
265 1.1.2.2 thorpej phy_phandle = fdtbus_get_phandle(phandle, "phy-handle");
266 1.1.2.2 thorpej if (phy_phandle == -1)
267 1.1.2.2 thorpej return MII_PHY_ANY;
268 1.1.2.2 thorpej
269 1.1.2.2 thorpej if (fdtbus_get_reg(phy_phandle, 0, &addr, NULL) != 0)
270 1.1.2.2 thorpej return MII_PHY_ANY;
271 1.1.2.2 thorpej
272 1.1.2.2 thorpej return (int)addr;
273 1.1.2.2 thorpej }
274