jh7100_gmac.c revision 1.1 1 /* $NetBSD: jh7100_gmac.c,v 1.1 2024/10/26 15:49:43 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2024 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nick Hudson
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: jh7100_gmac.c,v 1.1 2024/10/26 15:49:43 skrll Exp $");
34
35 #include <sys/param.h>
36
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/device.h>
40 #include <sys/rndsource.h>
41
42 #include <net/if_ether.h>
43 #include <net/if_media.h>
44
45 #include <dev/fdt/fdtvar.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 <prop/proplib.h>
53
54 #include <riscv/starfive/jh71x0_eth.h>
55
56 struct jh7100_gmac_softc {
57 struct dwc_gmac_softc sc_gmac;
58 struct jh71x0_eth_softc sc_jh71x0eth;
59 };
60
61 static void
62 jh7100_gmac_set_speed(struct dwc_gmac_softc *gmac_sc, int speed)
63 {
64 struct jh7100_gmac_softc * const sc =
65 container_of(gmac_sc, struct jh7100_gmac_softc, sc_gmac);
66 struct jh71x0_eth_softc * const jheth_sc = &sc->sc_jh71x0eth;
67
68 u_int rate = clk_get_rate(jheth_sc->sc_clk_tx);
69 switch (speed) {
70 case IFM_10_T:
71 rate = 2500000;
72 break;
73 case IFM_100_TX:
74 rate = 25000000;
75 break;
76 case IFM_1000_T:
77 rate = 125000000;
78 default:
79 // error unsupported
80 break;
81 }
82
83 int error = clk_set_rate(jheth_sc->sc_clk_tx, rate);
84 if (error)
85 aprint_error_dev(jheth_sc->sc_dev, "failed to set TX rate %u",
86 rate);
87 }
88
89 static int
90 jh7100_gmac_intr(void *arg)
91 {
92 return dwc_gmac_intr(arg);
93 }
94
95 /* Compat string(s) */
96 static const struct device_compatible_entry compat_data[] = {
97 { .compat = "starfive,jh7100-dwmac" },
98 DEVICE_COMPAT_EOL
99 };
100
101 static int
102 jh7100_gmac_match(device_t parent, cfdata_t cf, void *aux)
103 {
104 struct fdt_attach_args * const faa = aux;
105
106 return of_compatible_match(faa->faa_phandle, compat_data);
107 }
108
109 static void
110 jh7100_gmac_attach(device_t parent, device_t self, void *aux)
111 {
112 struct jh7100_gmac_softc * const sc = device_private(self);
113 struct jh71x0_eth_softc * const jheth_sc = &sc->sc_jh71x0eth;
114 struct dwc_gmac_softc * const gmac_sc = &sc->sc_gmac;
115 struct fdt_attach_args * const faa = aux;
116 const int phandle = faa->faa_phandle;
117 char intrstr[128];
118 int error;
119
120 sc->sc_gmac.sc_dev = self;
121 sc->sc_gmac.sc_bst = faa->faa_bst;
122 sc->sc_gmac.sc_dmat = faa->faa_dmat;
123 sc->sc_jh71x0eth.sc_dev = self;
124 sc->sc_jh71x0eth.sc_phy = MII_PHY_ANY;
125 sc->sc_jh71x0eth.sc_type = JH71X0ETH_GMAC;
126
127 error = jh71x0_eth_attach(jheth_sc, faa, &sc->sc_gmac.sc_bsh);
128 if (error)
129 return;
130
131 if (sc->sc_jh71x0eth.sc_clk_tx) {
132 sc->sc_gmac.sc_set_speed = jh7100_gmac_set_speed;
133 }
134
135 aprint_naive("\n");
136 aprint_normal(": Gigabit Ethernet Controller\n");
137
138 error = dwc_gmac_attach(gmac_sc, sc->sc_jh71x0eth.sc_phy,
139 GMAC_MII_CLK_150_250M_DIV102);
140 if (error)
141 return;
142
143 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
144 aprint_error(": failed to decode interrupt\n");
145 return;
146 }
147 void *ih = fdtbus_intr_establish_xname(phandle, 0, IPL_NET,
148 FDT_INTR_MPSAFE, jh7100_gmac_intr, sc, device_xname(self));
149 if (ih == NULL) {
150 aprint_error_dev(self, "failed to establish interrupt on %s\n",
151 intrstr);
152 // XXXNH unwind
153 return;
154 }
155 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
156 }
157
158
159 CFATTACH_DECL_NEW(jh7100_gmac, sizeof(struct jh7100_gmac_softc),
160 jh7100_gmac_match, jh7100_gmac_attach, NULL, NULL);
161