xref: /openbsd-src/sys/dev/pci/rtsx_pci.c (revision f85a24beab58a8cf0f32278b672c6827c7cdef11)
1 /*	$OpenBSD: rtsx_pci.c,v 1.10 2015/03/14 16:42:30 deraadt 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 
24 #include <dev/pci/pcivar.h>
25 #include <dev/pci/pcidevs.h>
26 #include <dev/ic/rtsxreg.h>
27 #include <dev/ic/rtsxvar.h>
28 #include <dev/sdmmc/sdmmcvar.h>
29 
30 #define RTSX_PCI_BAR 	0x10
31 
32 struct rtsx_pci_softc {
33 	struct rtsx_softc sc;
34 	void *sc_ih;
35 };
36 
37 int	rtsx_pci_match(struct device *, void *, void *);
38 void	rtsx_pci_attach(struct device *, struct device *, void *);
39 
40 struct cfattach rtsx_pci_ca = {
41 	sizeof(struct rtsx_pci_softc), rtsx_pci_match, rtsx_pci_attach,
42 	NULL, rtsx_activate
43 };
44 
45 int
46 rtsx_pci_match(struct device *parent, void *match, void *aux)
47 {
48 	struct pci_attach_args *pa = aux;
49 
50 	/*
51 	 * Explicitly match the UNDEFINED device class only. Some RTS5902
52 	 * devices advertise a SYSTEM/SDHC class in addition to the UNDEFINED
53 	 * device class. Let sdhc(4) handle the SYSTEM/SDHC ones.
54 	 */
55 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_REALTEK ||
56 	    PCI_CLASS(pa->pa_class) != PCI_CLASS_UNDEFINED)
57 		return 0;
58 
59 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS5209 ||
60 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS5227 ||
61 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS5229 ||
62 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTL8411 ||
63 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTL8411B ||
64 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTL8402)
65 		return 1;
66 
67 	return 0;
68 }
69 
70 void
71 rtsx_pci_attach(struct device *parent, struct device *self, void *aux)
72 {
73 	struct rtsx_pci_softc *sc = (struct rtsx_pci_softc *)self;
74 	struct pci_attach_args *pa = aux;
75 	pci_intr_handle_t ih;
76 	char const *intrstr;
77 	bus_space_tag_t iot;
78 	bus_space_handle_t ioh;
79 	bus_size_t size;
80 	int flags;
81 
82 	if ((pci_conf_read(pa->pa_pc, pa->pa_tag, RTSX_CFG_PCI)
83 	    & RTSX_CFG_ASIC) != 0) {
84 		printf("%s: no asic\n", sc->sc.sc_dev.dv_xname);
85 		return;
86 	}
87 
88 	if (pci_intr_map_msi(pa, &ih) && pci_intr_map(pa, &ih)) {
89 		printf(": can't map interrupt\n");
90 		return;
91 	}
92 
93 	intrstr = pci_intr_string(pa->pa_pc, ih);
94 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_SDMMC,
95 	    rtsx_intr, sc, sc->sc.sc_dev.dv_xname);
96 	if (sc->sc_ih == NULL) {
97 		printf(": can't establish interrupt\n");
98 		return;
99 	}
100 	printf(": %s\n", intrstr);
101 
102 	if (pci_mem_find(pa->pa_pc, pa->pa_tag, RTSX_PCI_BAR,
103 	    NULL, NULL, NULL) != 0) {
104 		printf("%s: can't find registers\n", sc->sc.sc_dev.dv_xname);
105 	    	return;
106 	}
107 
108 	if (pci_mapreg_map(pa, RTSX_PCI_BAR, PCI_MAPREG_TYPE_MEM, 0,
109 	    &iot, &ioh, NULL, &size, 0)) {
110 		printf("%s: can't map registers\n", sc->sc.sc_dev.dv_xname);
111 		return;
112 	}
113 
114 	pci_set_powerstate(pa->pa_pc, pa->pa_tag, PCI_PMCSR_STATE_D0);
115 
116 	switch (PCI_PRODUCT(pa->pa_id)) {
117 		case PCI_PRODUCT_REALTEK_RTS5209:
118 			flags = RTSX_F_5209;
119 			break;
120 		case PCI_PRODUCT_REALTEK_RTS5227:
121 			flags = RTSX_F_5227;
122 			break;
123 		case PCI_PRODUCT_REALTEK_RTS5229:
124 			flags = RTSX_F_5229;
125 			break;
126 		default:
127 			flags = 0;
128 			break;
129 	}
130 
131 	if (rtsx_attach(&sc->sc, iot, ioh, size, pa->pa_dmat, flags) != 0)
132 		printf("%s: can't initialize chip\n", sc->sc.sc_dev.dv_xname);
133 }
134