xref: /netbsd-src/sys/arch/arm/altera/cycv_gmac.c (revision c9ba1e932e746beba3edd75eea8cc6b8111ca83c)
1 /* $NetBSD: cycv_gmac.c,v 1.7 2024/08/10 12:16:46 skrll Exp $ */
2 
3 /*-
4  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /* Taken from sunxi_gmac.c */
30 
31 #include <sys/cdefs.h>
32 
33 __KERNEL_RCSID(0, "$NetBSD: cycv_gmac.c,v 1.7 2024/08/10 12:16:46 skrll Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/device.h>
38 #include <sys/intr.h>
39 #include <sys/systm.h>
40 #include <sys/gpio.h>
41 #include <sys/rndsource.h>
42 
43 #include <net/if.h>
44 #include <net/if_ether.h>
45 #include <net/if_media.h>
46 
47 #include <dev/mii/miivar.h>
48 
49 #include <dev/ic/dwc_gmac_var.h>
50 #include <dev/ic/dwc_gmac_reg.h>
51 
52 #include <dev/fdt/fdtvar.h>
53 
54 static const struct device_compatible_entry compat_data[] = {
55 	{ .compat = "altr,socfpga-stmmac" },
56 	DEVICE_COMPAT_EOL
57 };
58 
59 static int
60 cycv_gmac_intr(void *arg)
61 {
62 	return dwc_gmac_intr(arg);
63 }
64 
65 static int
66 cycv_gmac_match(device_t parent, cfdata_t cf, void *aux)
67 {
68 	struct fdt_attach_args * const faa = aux;
69 
70 	return of_compatible_match(faa->faa_phandle, compat_data);
71 }
72 
73 static void
74 cycv_gmac_attach(device_t parent, device_t self, void *aux)
75 {
76 	struct dwc_gmac_softc * const sc = device_private(self);
77 	struct fdt_attach_args * const faa = aux;
78 	const int phandle = faa->faa_phandle;
79 	struct clk *clk_gmac;
80 	struct fdtbus_reset *rst_gmac;
81 	const char *phy_mode;
82 	char intrstr[128];
83 	bus_addr_t addr;
84 	bus_size_t size;
85 
86 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
87 		aprint_error(": couldn't get registers\n");
88 		return;
89 	}
90 
91 	sc->sc_dev = self;
92 	sc->sc_bst = faa->faa_bst;
93 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
94 		aprint_error(": couldn't map registers\n");
95 		return;
96 	}
97 	sc->sc_dmat = faa->faa_dmat;
98 
99 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof intrstr)) {
100 		aprint_error(": failed to decode interrupt\n");
101 		return;
102 	}
103 
104 	clk_gmac = fdtbus_clock_get(phandle, "stmmaceth");
105 	if (clk_gmac == NULL) {
106 		aprint_error(": couldn't get clock\n");
107 		return;
108 	}
109 
110 	rst_gmac = fdtbus_reset_get(phandle, "stmmaceth");
111 	if (rst_gmac == NULL) {
112 		aprint_error(": couldn't get reset\n");
113 		return;
114 	}
115 
116 	phy_mode = fdtbus_get_string(phandle, "phy-mode");
117 	if (phy_mode == NULL) {
118 		aprint_error(": missing 'phy-mode' property\n");
119 		return;
120 	}
121 
122 	if (clk_enable(clk_gmac) != 0) {
123 		aprint_error(": couldn't enable clocks\n");
124 		return;
125 	}
126 
127 	if (rst_gmac != NULL && fdtbus_reset_deassert(rst_gmac) != 0) {
128 		aprint_error(": couldn't de-assert reset\n");
129 		return;
130 	}
131 
132 	aprint_naive("\n");
133 	aprint_normal(": GMAC\n");
134 
135 	if (fdtbus_intr_establish_xname(phandle, 0, IPL_NET, FDT_INTR_MPSAFE,
136 	     cycv_gmac_intr, sc, device_xname(sc->sc_dev)) == NULL) {
137 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
138 				 intrstr);
139 		return;
140 	}
141 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
142 
143 	dwc_gmac_attach(sc, MII_PHY_ANY, GMAC_MII_CLK_150_250M_DIV102);
144 }
145 
146 CFATTACH_DECL_NEW(cycv_gmac, sizeof(struct dwc_gmac_softc),
147 	cycv_gmac_match, cycv_gmac_attach, NULL, NULL);
148