xref: /openbsd/sys/dev/acpi/ufshci_acpi.c (revision 8579f9a0)
1 /*	$OpenBSD: ufshci_acpi.c,v 1.3 2024/10/08 00:46:29 jsg 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 	NULL, ufshci_activate
50 };
51 
52 const char *ufshci_hids[] = {
53 	"QCOM24A5",
54 	NULL
55 };
56 
57 int
ufshci_acpi_match(struct device * parent,void * match,void * aux)58 ufshci_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)
64 		return 0;
65 
66 	return acpi_matchhids(aaa, ufshci_hids, cf->cf_driver->cd_name);
67 }
68 
69 void
ufshci_acpi_attach(struct device * parent,struct device * self,void * aux)70 ufshci_acpi_attach(struct device *parent, struct device *self, void *aux)
71 {
72 	struct ufshci_acpi_softc *sc = (struct ufshci_acpi_softc *)self;
73 	struct acpi_attach_args *aaa = aux;
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 	if (aaa->aaa_nirq < 1) {
81 		printf(": no interrupt\n");
82 		return;
83 	}
84 
85 	printf(" addr 0x%llx/0x%llx", aaa->aaa_addr[0], aaa->aaa_size[0]);
86 	printf(" irq %d", aaa->aaa_irq[0]);
87 
88 	sc->sc.sc_iot = aaa->aaa_bst[0];
89 	sc->sc.sc_ios = aaa->aaa_size[0];
90 	sc->sc.sc_dmat = aaa->aaa_dmat;
91 
92 	if (bus_space_map(sc->sc.sc_iot, aaa->aaa_addr[0], aaa->aaa_size[0], 0,
93 	    &sc->sc.sc_ioh)) {
94 		printf(": can't map registers\n");
95 		return;
96 	}
97 
98 	sc->sc_ih = acpi_intr_establish(aaa->aaa_irq[0], aaa->aaa_irq_flags[0],
99 	    IPL_BIO, ufshci_intr, sc, sc->sc.sc_dev.dv_xname);
100 	if (sc->sc_ih == NULL) {
101 		printf(": can't establish interrupt\n");
102 		return;
103 	}
104 
105 	error = ufshci_attach(&sc->sc);
106 	if (error) {
107 		printf("%s: attach failed, error=%d\n",
108 		    sc->sc.sc_dev.dv_xname, error);
109 		return;
110 	}
111 }
112