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