xref: /openbsd-src/sys/dev/pci/ufshci_pci.c (revision 7c0ec4b8992567abb1e1536622dc789a9a39d9f1)
1 /*	$OpenBSD: ufshci_pci.c,v 1.4 2024/05/24 09:51:13 mglocker Exp $ */
2 
3 /*
4  * Copyright (c) 2024 Marcus Glocker <mglocker@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/device.h>
22 
23 #include <dev/pci/pcireg.h>
24 #include <dev/pci/pcivar.h>
25 
26 #include <scsi/scsi_all.h>
27 #include <scsi/scsiconf.h>
28 
29 #include <dev/ic/ufshcivar.h>
30 
31 #define UFSHCI_PCI_BAR		0x10
32 #define UFSHCI_PCI_INTERFACE	0x01
33 
34 struct ufshci_pci_softc {
35 	struct ufshci_softc	 psc_ufshci;
36 
37 	pci_chipset_tag_t	 psc_pc;
38 	void			*psc_ih;
39 };
40 
41 int	ufshci_pci_match(struct device *, void *, void *);
42 void	ufshci_pci_attach(struct device *, struct device *, void *);
43 int	ufshci_pci_detach(struct device *, int);
44 int	ufshci_pci_activate(struct device *, int);
45 
46 const struct cfattach ufshci_pci_ca = {
47 	sizeof(struct ufshci_pci_softc),
48 	ufshci_pci_match,
49 	ufshci_pci_attach,
50 	ufshci_pci_detach,
51 	ufshci_pci_activate
52 };
53 
54 int
55 ufshci_pci_match(struct device *parent, void *match, void *aux)
56 {
57 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
58 
59 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
60 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_UFS &&
61 	    PCI_INTERFACE(pa->pa_class) == UFSHCI_PCI_INTERFACE)
62 		return 1;
63 
64 	return 0;
65 }
66 
67 void
68 ufshci_pci_attach(struct device *parent, struct device *self, void *aux)
69 {
70 	struct ufshci_pci_softc *psc = (struct ufshci_pci_softc *)self;
71 	struct ufshci_softc *sc = &psc->psc_ufshci;
72 	struct pci_attach_args *pa = aux;
73 	pcireg_t maptype;
74 	pci_intr_handle_t ih;
75 
76 	psc->psc_pc = pa->pa_pc;
77 	sc->sc_dmat = pa->pa_dmat;
78 
79 	/* Map registers */
80 	maptype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, UFSHCI_PCI_BAR);
81 	if (pci_mapreg_map(pa, UFSHCI_PCI_BAR, maptype, 0, &sc->sc_iot,
82 	    &sc->sc_ioh, NULL, &sc->sc_ios, 0) != 0) {
83 		printf(": can't map registers\n");
84 		return;
85 	}
86 
87 	/* Map interrupt */
88 	if (pci_intr_map(pa, &ih) != 0) {
89 		printf(": can't map interrupt\n");
90 		return;
91 	}
92 	printf(": %s", pci_intr_string(pa->pa_pc, ih));
93 
94 	/* Establish interrupt */
95 	psc->psc_ih = pci_intr_establish(psc->psc_pc, ih, IPL_BIO, ufshci_intr,
96 	    sc, sc->sc_dev.dv_xname);
97 	if (psc->psc_ih == NULL) {
98 		printf(": can't establish interrupt\n");
99 		return;
100 	}
101 
102 	/* Call the driver attach */
103 	ufshci_attach(sc);
104 }
105 
106 int
107 ufshci_pci_detach(struct device *self, int flags)
108 {
109 	return 0;
110 }
111 
112 int
113 ufshci_pci_activate(struct device *self, int act)
114 {
115 	struct ufshci_pci_softc *psc = (struct ufshci_pci_softc *)self;
116 
117 	return ufshci_activate(&psc->psc_ufshci, act);
118 }
119