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