xref: /openbsd-src/sys/dev/pci/rtsx_pci.c (revision f615cd67b44e84976c75886093ca5794b1689fea)
1 /*	$OpenBSD: rtsx_pci.c,v 1.1 2012/11/29 23:36:34 stsp Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
5  * Copyright (c) 2012 Stefan Sperling <stsp@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/param.h>
21 #include <sys/device.h>
22 #include <sys/systm.h>
23 #include <sys/malloc.h>
24 
25 #include <dev/pci/pcivar.h>
26 #include <dev/pci/pcidevs.h>
27 #include <dev/ic/rtsxreg.h>
28 #include <dev/ic/rtsxvar.h>
29 #include <dev/sdmmc/sdmmcvar.h>
30 
31 #define RTSX_PCI_BAR 	0x10
32 
33 struct rtsx_pci_softc {
34 	struct rtsx_softc sc;
35 	void *sc_ih;
36 };
37 
38 int	rtsx_pci_match(struct device *, void *, void *);
39 void	rtsx_pci_attach(struct device *, struct device *, void *);
40 
41 struct cfattach rtsx_pci_ca = {
42 	sizeof(struct rtsx_pci_softc), rtsx_pci_match, rtsx_pci_attach,
43 	NULL, rtsx_activate
44 };
45 
46 int
47 rtsx_pci_match(struct device *parent, void *match, void *aux)
48 {
49 	struct pci_attach_args *pa = aux;
50 
51 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_REALTEK &&
52 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS5209)
53 		return 1;
54 
55 	return 0;
56 }
57 
58 void
59 rtsx_pci_attach(struct device *parent, struct device *self, void *aux)
60 {
61 	struct rtsx_pci_softc *sc = (struct rtsx_pci_softc *)self;
62 	struct pci_attach_args *pa = aux;
63 	pci_intr_handle_t ih;
64 	char const *intrstr;
65 	bus_space_tag_t iot;
66 	bus_space_handle_t ioh;
67 	bus_size_t size;
68 
69 	if ((pci_conf_read(pa->pa_pc, pa->pa_tag, RTSX_CFG_PCI)
70 	    & RTSX_CFG_ASIC) != 0) {
71 		printf("%s: no asic\n", sc->sc.sc_dev.dv_xname);
72 		return;
73 	}
74 
75 	if (pci_intr_map(pa, &ih)) {
76 		printf(": can't map interrupt\n");
77 		return;
78 	}
79 
80 	intrstr = pci_intr_string(pa->pa_pc, ih);
81 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_SDMMC,
82 	    rtsx_intr, sc, sc->sc.sc_dev.dv_xname);
83 	if (sc->sc_ih == NULL) {
84 		printf(": can't establish interrupt\n");
85 		return;
86 	}
87 	printf(": %s\n", intrstr);
88 
89 	if (pci_mem_find(pa->pa_pc, pa->pa_tag, RTSX_PCI_BAR,
90 	    NULL, NULL, NULL) != 0) {
91 		printf("%s: can't find registers\n", sc->sc.sc_dev.dv_xname);
92 	    	return;
93 	}
94 
95 	if (pci_mapreg_map(pa, RTSX_PCI_BAR, PCI_MAPREG_TYPE_MEM, 0,
96 	    &iot, &ioh, NULL, &size, 0)) {
97 		printf("%s: can't map registers\n", sc->sc.sc_dev.dv_xname);
98 		return;
99 	}
100 
101 	pci_set_powerstate(pa->pa_pc, pa->pa_tag, PCI_PMCSR_STATE_D0);
102 
103 	if (rtsx_attach(&sc->sc, iot, ioh, size, pa->pa_dmat) != 0)
104 		printf("%s: can't initialize chip\n", sc->sc.sc_dev.dv_xname);
105 }
106