1 /* $OpenBSD: if_bse_acpi.c,v 1.6 2022/04/06 18:59:27 naddy Exp $ */
2 /*
3 * Copyright (c) 2020 Mark Kettenis
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/device.h>
21
22 #include <machine/bus.h>
23 #include <machine/intr.h>
24
25 #include <net/if.h>
26 #include <net/if_media.h>
27 #include <netinet/in.h>
28 #include <netinet/if_ether.h>
29
30 #include <dev/acpi/acpireg.h>
31 #include <dev/acpi/acpivar.h>
32 #include <dev/acpi/acpidev.h>
33 #include <dev/acpi/amltypes.h>
34 #include <dev/acpi/dsdt.h>
35
36 #include <dev/mii/miivar.h>
37 #include <dev/ic/bcmgenetvar.h>
38
39 struct bse_acpi_softc {
40 struct genet_softc sc;
41 struct acpi_softc *sc_acpi;
42 struct aml_node *sc_node;
43 };
44
45 int bse_acpi_match(struct device *, void *, void *);
46 void bse_acpi_attach(struct device *, struct device *, void *);
47
48 const struct cfattach bse_acpi_ca = {
49 sizeof(struct bse_acpi_softc), bse_acpi_match, bse_acpi_attach
50 };
51
52 const char *bse_hids[] = {
53 "BCM6E4E",
54 NULL
55 };
56
57 int
bse_acpi_match(struct device * parent,void * match,void * aux)58 bse_acpi_match(struct device *parent, void *match, void *aux)
59 {
60 struct acpi_attach_args *aaa = aux;
61 struct cfdata *cf = match;
62
63 if (aaa->aaa_naddr < 1 || aaa->aaa_nirq < 1)
64 return 0;
65 return acpi_matchhids(aaa, bse_hids, cf->cf_driver->cd_name);
66 }
67
68 void
bse_acpi_attach(struct device * parent,struct device * self,void * aux)69 bse_acpi_attach(struct device *parent, struct device *self, void *aux)
70 {
71 struct bse_acpi_softc *sc = (struct bse_acpi_softc *)self;
72 struct acpi_attach_args *aaa = aux;
73 char phy_mode[16] = { 0 };
74 int error;
75
76 sc->sc_acpi = (struct acpi_softc *)parent;
77 sc->sc_node = aaa->aaa_node;
78 printf(" %s", sc->sc_node->name);
79
80 printf(" addr 0x%llx/0x%llx", aaa->aaa_addr[0], aaa->aaa_size[0]);
81 printf(" irq %d", aaa->aaa_irq[0]);
82
83 sc->sc.sc_bst = aaa->aaa_bst[0];
84 sc->sc.sc_dmat = aaa->aaa_dmat;
85
86 if (bus_space_map(sc->sc.sc_bst, aaa->aaa_addr[0], aaa->aaa_size[0],
87 0, &sc->sc.sc_bsh)) {
88 printf(": can't map registers\n");
89 return;
90 }
91
92 sc->sc.sc_ih = acpi_intr_establish(aaa->aaa_irq[0],
93 aaa->aaa_irq_flags[0], IPL_NET, genet_intr,
94 sc, sc->sc.sc_dev.dv_xname);
95 if (sc->sc.sc_ih == NULL) {
96 printf(": can't establish interrupt\n");
97 goto unmap;
98 }
99
100 /*
101 * UEFI firmware initializes the hardware MAC address
102 * registers. Read them here before we reset the hardware.
103 */
104 genet_lladdr_read(&sc->sc, sc->sc.sc_lladdr);
105
106 acpi_getprop(sc->sc_node, "phy-mode", phy_mode, sizeof(phy_mode));
107 if (strcmp(phy_mode, "rgmii-id") == 0)
108 sc->sc.sc_phy_mode = GENET_PHY_MODE_RGMII_ID;
109 else if (strcmp(phy_mode, "rgmii-rxid") == 0)
110 sc->sc.sc_phy_mode = GENET_PHY_MODE_RGMII_RXID;
111 else if (strcmp(phy_mode, "rgmii-txid") == 0)
112 sc->sc.sc_phy_mode = GENET_PHY_MODE_RGMII_TXID;
113 else
114 sc->sc.sc_phy_mode = GENET_PHY_MODE_RGMII;
115
116 sc->sc.sc_phy_id = MII_PHY_ANY;
117 error = genet_attach(&sc->sc);
118 if (error)
119 goto disestablish;
120
121 return;
122
123 disestablish:
124 acpi_intr_disestablish(sc->sc.sc_ih);
125 unmap:
126 bus_space_unmap(sc->sc.sc_bst, sc->sc.sc_bsh, aaa->aaa_size[0]);
127 }
128