1 /* $NetBSD: genet_acpi.c,v 1.6 2024/09/15 08:30:01 skrll Exp $ */
2 
3 /*-
4  * Copyright (c) 2020 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: genet_acpi.c,v 1.6 2024/09/15 08:30:01 skrll Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/cpu.h>
35 #include <sys/device.h>
36 
37 #include <sys/rndsource.h>
38 
39 #include <net/if.h>
40 #include <net/if_dl.h>
41 #include <net/if_ether.h>
42 #include <net/if_media.h>
43 
44 #include <dev/mii/miivar.h>
45 
46 #include <dev/ic/bcmgenetvar.h>
47 
48 #include <dev/acpi/acpireg.h>
49 #include <dev/acpi/acpivar.h>
50 #include <dev/acpi/acpi_intr.h>
51 
52 static const struct device_compatible_entry compat_data[] = {
53           { .compat = "BCM6E4E" },      /* Broadcom GENET v5 */
54           DEVICE_COMPAT_EOL
55 };
56 
57 static int          genet_acpi_match(device_t, cfdata_t, void *);
58 static void         genet_acpi_attach(device_t, device_t, void *);
59 
60 CFATTACH_DECL_NEW(genet_acpi, sizeof(struct genet_softc),
61     genet_acpi_match, genet_acpi_attach, NULL, NULL);
62 
63 static int
genet_acpi_match(device_t parent,cfdata_t cf,void * aux)64 genet_acpi_match(device_t parent, cfdata_t cf, void *aux)
65 {
66           struct acpi_attach_args *aa = aux;
67 
68           return acpi_compatible_match(aa, compat_data);
69 }
70 
71 static void
genet_acpi_attach(device_t parent,device_t self,void * aux)72 genet_acpi_attach(device_t parent, device_t self, void *aux)
73 {
74           struct genet_softc * const sc = device_private(self);
75           struct acpi_attach_args *aa = aux;
76           ACPI_HANDLE handle = aa->aa_node->ad_handle;
77           struct acpi_resources res;
78           struct acpi_mem *mem;
79           struct acpi_irq *irq;
80           char *phy_mode;
81           ACPI_STATUS rv;
82           int error;
83           void *ih;
84 
85           sc->sc_dev = self;
86 
87           rv = acpi_resource_parse(sc->sc_dev, handle, "_CRS",
88               &res, &acpi_resource_parse_ops_default);
89           if (ACPI_FAILURE(rv))
90                     return;
91 
92           mem = acpi_res_mem(&res, 0);
93           if (mem == NULL) {
94                     aprint_error_dev(self, "couldn't find mem resource\n");
95                     goto done;
96           }
97 
98           irq = acpi_res_irq(&res, 0);
99           if (irq == NULL) {
100                     aprint_error_dev(self, "couldn't find irq resource\n");
101                     goto done;
102           }
103 
104           sc->sc_bst = aa->aa_memt;
105           error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length,
106               0, &sc->sc_bsh);
107           if (error != 0) {
108                     aprint_error_dev(self, "couldn't map registers\n");
109                     goto done;
110           }
111           sc->sc_dmat = BUS_DMA_TAG_VALID(aa->aa_dmat64) ?
112               aa->aa_dmat64 : aa->aa_dmat;
113           sc->sc_phy_id = MII_PHY_ANY;
114 
115           rv = acpi_dsd_string(handle, "phy-mode", &phy_mode);
116           if (ACPI_FAILURE(rv)) {
117                     aprint_error_dev(self, "missing 'phy-mode' property\n");
118                     goto done;
119           }
120 
121           if (strcmp(phy_mode, "rgmii-rxid") == 0)
122                     sc->sc_phy_mode = GENET_PHY_MODE_RGMII_RXID;
123           else if (strcmp(phy_mode, "rgmii-txid") == 0)
124                     sc->sc_phy_mode = GENET_PHY_MODE_RGMII_TXID;
125           else if (strcmp(phy_mode, "rgmii-id") == 0)
126                     sc->sc_phy_mode = GENET_PHY_MODE_RGMII_ID;
127           else if (strcmp(phy_mode, "rgmii") == 0)
128                     sc->sc_phy_mode = GENET_PHY_MODE_RGMII;
129           else {
130                     aprint_error(": unsupported phy-mode '%s'\n", phy_mode);
131                     goto done;
132           }
133 
134           aprint_normal("%s", device_xname(self));
135           if (genet_attach(sc) != 0)
136                     goto done;
137 
138         ih = acpi_intr_establish(self, (uint64_t)(uintptr_t)handle, IPL_NET,
139               true, genet_intr, sc, device_xname(self));
140           if (ih == NULL) {
141                     aprint_error_dev(self, "couldn't establish interrupt\n");
142                     goto done;
143           }
144 
145 done:
146           acpi_resource_cleanup(&res);
147 }
148