xref: /openbsd/sys/dev/acpi/if_bse_acpi.c (revision 771fbea0)
1 /*	$OpenBSD: if_bse_acpi.c,v 1.3 2020/05/08 11:18:01 kettenis 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 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
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 	return acpi_matchhids(aaa, bse_hids, cf->cf_driver->cd_name);
64 }
65 
66 void
67 bse_acpi_attach(struct device *parent, struct device *self, void *aux)
68 {
69 	struct bse_acpi_softc *sc = (struct bse_acpi_softc *)self;
70 	struct acpi_attach_args *aaa = aux;
71 	char phy_mode[16] = { 0 };
72 	int error;
73 
74 	sc->sc_acpi = (struct acpi_softc *)parent;
75 	sc->sc_node = aaa->aaa_node;
76 	printf(" %s", sc->sc_node->name);
77 
78 	if (aaa->aaa_naddr < 1) {
79 		printf(": no registers\n");
80 		return;
81 	}
82 
83 	if (aaa->aaa_nirq < 1) {
84 		printf(": no interrupt\n");
85 		return;
86 	}
87 
88 	printf(" addr 0x%llx/0x%llx", aaa->aaa_addr[0], aaa->aaa_size[0]);
89 	printf(" irq %d", aaa->aaa_irq[0]);
90 
91 	sc->sc.sc_bst = aaa->aaa_bst[0];
92 	sc->sc.sc_dmat = aaa->aaa_dmat;
93 
94 	if (bus_space_map(sc->sc.sc_bst, aaa->aaa_addr[0], aaa->aaa_size[0],
95 	    0, &sc->sc.sc_bsh)) {
96 		printf(": can't map registers\n");
97 		return;
98 	}
99 
100 	sc->sc.sc_ih = acpi_intr_establish(aaa->aaa_irq[0],
101 	    aaa->aaa_irq_flags[0], IPL_NET, genet_intr,
102 	    sc, sc->sc.sc_dev.dv_xname);
103 	if (sc->sc.sc_ih == NULL) {
104 		printf(": can't establish interrupt\n");
105 		goto unmap;
106 	}
107 
108 	/*
109 	 * UEFI firmware initializes the hardware MAC address
110 	 * registers.  Read them here before we reset the hardware.
111 	 */
112 	genet_lladdr_read(&sc->sc, sc->sc.sc_lladdr);
113 
114 	acpi_getprop(sc->sc_node, "phy-mode", phy_mode, sizeof(phy_mode));
115 	if (strcmp(phy_mode, "rgmii-id") == 0)
116 		sc->sc.sc_phy_mode = GENET_PHY_MODE_RGMII_ID;
117 	else if (strcmp(phy_mode, "rgmii-rxid") == 0)
118 		sc->sc.sc_phy_mode = GENET_PHY_MODE_RGMII_RXID;
119 	else if (strcmp(phy_mode, "rgmii-txid") == 0)
120 		sc->sc.sc_phy_mode = GENET_PHY_MODE_RGMII_TXID;
121 	else
122 		sc->sc.sc_phy_mode = GENET_PHY_MODE_RGMII;
123 
124 	sc->sc.sc_phy_id = MII_PHY_ANY;
125 	error = genet_attach(&sc->sc);
126 	if (error)
127 		goto disestablish;
128 
129 	return;
130 
131 disestablish:
132 #ifdef notyet
133 	acpi_intr_disestablish(sc->sc.sc_ih);
134 #endif
135 unmap:
136 	bus_space_unmap(sc->sc.sc_bst, sc->sc.sc_bsh, aaa->aaa_size[0]);
137 }
138