xref: /openbsd/sys/dev/acpi/ufshci_acpi.c (revision 899d618d)
1 /*	$OpenBSD: ufshci_acpi.c,v 1.2 2024/01/06 17:47:43 mglocker Exp $ */
2 /*
3  * Copyright (c) 2022 Marcus Glocker <mglocker@openbsd.org>
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 <dev/acpi/acpireg.h>
26 #include <dev/acpi/acpivar.h>
27 #include <dev/acpi/acpidev.h>
28 #include <dev/acpi/amltypes.h>
29 #include <dev/acpi/dsdt.h>
30 
31 #include <scsi/scsi_all.h>
32 #include <scsi/scsi_disk.h>
33 #include <scsi/scsiconf.h>
34 
35 #include <dev/ic/ufshcivar.h>
36 
37 struct ufshci_acpi_softc {
38 	struct ufshci_softc	 sc;
39 	struct acpi_softc	*sc_acpi;
40 	struct aml_node		*sc_node;
41 	void			*sc_ih;
42 };
43 
44 int	ufshci_acpi_match(struct device *, void *, void *);
45 void	ufshci_acpi_attach(struct device *, struct device *, void *);
46 
47 const struct cfattach ufshci_acpi_ca = {
48 	sizeof(struct ufshci_acpi_softc), ufshci_acpi_match, ufshci_acpi_attach
49 };
50 
51 const char *ufshci_hids[] = {
52 	"QCOM24A5",
53 	NULL
54 };
55 
56 int
57 ufshci_acpi_match(struct device *parent, void *match, void *aux)
58 {
59 	struct acpi_attach_args *aaa = aux;
60 	struct cfdata *cf = match;
61 
62 	if (aaa->aaa_naddr < 1)
63 		return 0;
64 
65 	return acpi_matchhids(aaa, ufshci_hids, cf->cf_driver->cd_name);
66 }
67 
68 void
69 ufshci_acpi_attach(struct device *parent, struct device *self, void *aux)
70 {
71 	struct ufshci_acpi_softc *sc = (struct ufshci_acpi_softc *)self;
72 	struct acpi_attach_args *aaa = aux;
73 	int error;
74 
75 	sc->sc_acpi = (struct acpi_softc *)parent;
76 	sc->sc_node = aaa->aaa_node;
77 	printf(" %s", sc->sc_node->name);
78 
79 	if (aaa->aaa_nirq < 1) {
80 		printf(": no interrupt\n");
81 		return;
82 	}
83 
84 	printf(" addr 0x%llx/0x%llx", aaa->aaa_addr[0], aaa->aaa_size[0]);
85 	printf(" irq %d", aaa->aaa_irq[0]);
86 
87 	sc->sc.sc_iot = aaa->aaa_bst[0];
88 	sc->sc.sc_ios = aaa->aaa_size[0];
89 	sc->sc.sc_dmat = aaa->aaa_dmat;
90 
91 	if (bus_space_map(sc->sc.sc_iot, aaa->aaa_addr[0], aaa->aaa_size[0], 0,
92 	    &sc->sc.sc_ioh)) {
93 		printf(": can't map registers\n");
94 		return;
95 	}
96 
97 	sc->sc_ih = acpi_intr_establish(aaa->aaa_irq[0], aaa->aaa_irq_flags[0],
98 	    IPL_BIO, ufshci_intr, sc, sc->sc.sc_dev.dv_xname);
99 	if (sc->sc_ih == NULL) {
100 		printf(": can't establish interrupt\n");
101 		return;
102 	}
103 
104 	error = ufshci_attach(&sc->sc);
105 	if (error) {
106 		printf("%s: attach failed, error=%d\n",
107 		    sc->sc.sc_dev.dv_xname, error);
108 		return;
109 	}
110 }
111