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