xref: /openbsd-src/sys/dev/pci/ufshci_pci.c (revision 8579f9a0e7871ce8d71ce801814e06ae1d20e04f)
1 /*	$OpenBSD: ufshci_pci.c,v 1.5 2024/10/08 00:46:29 jsg 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 
45 const struct cfattach ufshci_pci_ca = {
46 	sizeof(struct ufshci_pci_softc),
47 	ufshci_pci_match,
48 	ufshci_pci_attach,
49 	ufshci_pci_detach,
50 	ufshci_activate
51 };
52 
53 int
54 ufshci_pci_match(struct device *parent, void *match, void *aux)
55 {
56 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
57 
58 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
59 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_UFS &&
60 	    PCI_INTERFACE(pa->pa_class) == UFSHCI_PCI_INTERFACE)
61 		return 1;
62 
63 	return 0;
64 }
65 
66 void
67 ufshci_pci_attach(struct device *parent, struct device *self, void *aux)
68 {
69 	struct ufshci_pci_softc *psc = (struct ufshci_pci_softc *)self;
70 	struct ufshci_softc *sc = &psc->psc_ufshci;
71 	struct pci_attach_args *pa = aux;
72 	pcireg_t maptype;
73 	pci_intr_handle_t ih;
74 
75 	psc->psc_pc = pa->pa_pc;
76 	sc->sc_dmat = pa->pa_dmat;
77 
78 	/* Map registers */
79 	maptype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, UFSHCI_PCI_BAR);
80 	if (pci_mapreg_map(pa, UFSHCI_PCI_BAR, maptype, 0, &sc->sc_iot,
81 	    &sc->sc_ioh, NULL, &sc->sc_ios, 0) != 0) {
82 		printf(": can't map registers\n");
83 		return;
84 	}
85 
86 	/* Map interrupt */
87 	if (pci_intr_map(pa, &ih) != 0) {
88 		printf(": can't map interrupt\n");
89 		return;
90 	}
91 	printf(": %s", pci_intr_string(pa->pa_pc, ih));
92 
93 	/* Establish interrupt */
94 	psc->psc_ih = pci_intr_establish(psc->psc_pc, ih, IPL_BIO, ufshci_intr,
95 	    sc, sc->sc_dev.dv_xname);
96 	if (psc->psc_ih == NULL) {
97 		printf(": can't establish interrupt\n");
98 		return;
99 	}
100 
101 	/* Call the driver attach */
102 	ufshci_attach(sc);
103 }
104 
105 int
106 ufshci_pci_detach(struct device *self, int flags)
107 {
108 	return 0;
109 }
110