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