xref: /openbsd-src/sys/dev/pci/rtsx_pci.c (revision 32973e346c45f62e04cd62aabeaef16dbbf04515)
1 /*	$OpenBSD: rtsx_pci.c,v 1.13 2016/04/26 00:21:27 jsg 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 RTS5209
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_RTS522A ||
63 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS5249 ||
64 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTL8402 ||
65 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTL8411 ||
66 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTL8411B)
67 		return 1;
68 
69 	return 0;
70 }
71 
72 void
73 rtsx_pci_attach(struct device *parent, struct device *self, void *aux)
74 {
75 	struct rtsx_pci_softc *sc = (struct rtsx_pci_softc *)self;
76 	struct pci_attach_args *pa = aux;
77 	pci_intr_handle_t ih;
78 	char const *intrstr;
79 	bus_space_tag_t iot;
80 	bus_space_handle_t ioh;
81 	bus_size_t size;
82 	int flags;
83 
84 	if ((pci_conf_read(pa->pa_pc, pa->pa_tag, RTSX_CFG_PCI)
85 	    & RTSX_CFG_ASIC) != 0) {
86 		printf("%s: no asic\n", sc->sc.sc_dev.dv_xname);
87 		return;
88 	}
89 
90 	if (pci_intr_map_msi(pa, &ih) && pci_intr_map(pa, &ih)) {
91 		printf(": can't map interrupt\n");
92 		return;
93 	}
94 
95 	intrstr = pci_intr_string(pa->pa_pc, ih);
96 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_SDMMC,
97 	    rtsx_intr, sc, sc->sc.sc_dev.dv_xname);
98 	if (sc->sc_ih == NULL) {
99 		printf(": can't establish interrupt\n");
100 		return;
101 	}
102 	printf(": %s\n", intrstr);
103 
104 	if (pci_mem_find(pa->pa_pc, pa->pa_tag, RTSX_PCI_BAR,
105 	    NULL, NULL, NULL) != 0) {
106 		printf("%s: can't find registers\n", sc->sc.sc_dev.dv_xname);
107 	    	return;
108 	}
109 
110 	if (pci_mapreg_map(pa, RTSX_PCI_BAR, PCI_MAPREG_TYPE_MEM, 0,
111 	    &iot, &ioh, NULL, &size, 0)) {
112 		printf("%s: can't map registers\n", sc->sc.sc_dev.dv_xname);
113 		return;
114 	}
115 
116 	pci_set_powerstate(pa->pa_pc, pa->pa_tag, PCI_PMCSR_STATE_D0);
117 
118 	switch (PCI_PRODUCT(pa->pa_id)) {
119 		case PCI_PRODUCT_REALTEK_RTS5209:
120 			flags = RTSX_F_5209;
121 			break;
122 		case PCI_PRODUCT_REALTEK_RTS5229:
123 		case PCI_PRODUCT_REALTEK_RTS5249:
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