xref: /openbsd/sys/dev/pci/rtsx_pci.c (revision 09467b48)
1 /*	$OpenBSD: rtsx_pci.c,v 1.14 2017/09/06 13:07:38 jcs 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 #define RTSX_PCI_BAR_525A 	0x14
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 	/*
52 	 * Explicitly match the UNDEFINED device class only. Some RTS5209
53 	 * devices advertise a SYSTEM/SDHC class in addition to the UNDEFINED
54 	 * device class. Let sdhc(4) handle the SYSTEM/SDHC ones.
55 	 */
56 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_REALTEK ||
57 	    PCI_CLASS(pa->pa_class) != PCI_CLASS_UNDEFINED)
58 		return 0;
59 
60 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS5209 ||
61 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS5227 ||
62 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS5229 ||
63 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS522A ||
64 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS5249 ||
65 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS525A ||
66 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTL8402 ||
67 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTL8411 ||
68 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTL8411B)
69 		return 1;
70 
71 	return 0;
72 }
73 
74 void
75 rtsx_pci_attach(struct device *parent, struct device *self, void *aux)
76 {
77 	struct rtsx_pci_softc *sc = (struct rtsx_pci_softc *)self;
78 	struct pci_attach_args *pa = aux;
79 	pci_intr_handle_t ih;
80 	char const *intrstr;
81 	bus_space_tag_t iot;
82 	bus_space_handle_t ioh;
83 	bus_size_t size;
84 	int flags;
85 	int bar = RTSX_PCI_BAR;
86 
87 	if ((pci_conf_read(pa->pa_pc, pa->pa_tag, RTSX_CFG_PCI)
88 	    & RTSX_CFG_ASIC) != 0) {
89 		printf("%s: no asic\n", sc->sc.sc_dev.dv_xname);
90 		return;
91 	}
92 
93 	if (pci_intr_map_msi(pa, &ih) && pci_intr_map(pa, &ih)) {
94 		printf(": can't map interrupt\n");
95 		return;
96 	}
97 
98 	intrstr = pci_intr_string(pa->pa_pc, ih);
99 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_SDMMC,
100 	    rtsx_intr, sc, sc->sc.sc_dev.dv_xname);
101 	if (sc->sc_ih == NULL) {
102 		printf(": can't establish interrupt\n");
103 		return;
104 	}
105 	printf(": %s\n", intrstr);
106 
107 	switch (PCI_PRODUCT(pa->pa_id)) {
108 	case PCI_PRODUCT_REALTEK_RTS5209:
109 		flags = RTSX_F_5209;
110 		break;
111 	case PCI_PRODUCT_REALTEK_RTS5229:
112 	case PCI_PRODUCT_REALTEK_RTS5249:
113 		flags = RTSX_F_5229;
114 		break;
115 	case PCI_PRODUCT_REALTEK_RTS525A:
116 		flags = RTSX_F_525A;
117 		bar = RTSX_PCI_BAR_525A;
118 		break;
119 	default:
120 		flags = 0;
121 		break;
122 	}
123 
124 	if (pci_mem_find(pa->pa_pc, pa->pa_tag, bar, NULL, NULL, NULL) != 0) {
125 		printf("%s: can't find registers\n", sc->sc.sc_dev.dv_xname);
126 	    	return;
127 	}
128 
129 	if (pci_mapreg_map(pa, bar, PCI_MAPREG_TYPE_MEM, 0, &iot, &ioh, NULL,
130 	    &size, 0)) {
131 		printf("%s: can't map registers\n", sc->sc.sc_dev.dv_xname);
132 		return;
133 	}
134 
135 	pci_set_powerstate(pa->pa_pc, pa->pa_tag, PCI_PMCSR_STATE_D0);
136 
137 	if (rtsx_attach(&sc->sc, iot, ioh, size, pa->pa_dmat, flags) != 0)
138 		printf("%s: can't initialize chip\n", sc->sc.sc_dev.dv_xname);
139 }
140