xref: /openbsd-src/sys/dev/pci/rtsx_pci.c (revision 7b44a1936692c43037237f4ab19894e4923dda49)
1*7b44a193Smiod /*	$OpenBSD: rtsx_pci.c,v 1.16 2023/04/13 15:07:43 miod Exp $	*/
2f615cd67Sstsp 
3f615cd67Sstsp /*
4f615cd67Sstsp  * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
5f615cd67Sstsp  * Copyright (c) 2012 Stefan Sperling <stsp@openbsd.org>
6f615cd67Sstsp  *
7f615cd67Sstsp  * Permission to use, copy, modify, and distribute this software for any
8f615cd67Sstsp  * purpose with or without fee is hereby granted, provided that the above
9f615cd67Sstsp  * copyright notice and this permission notice appear in all copies.
10f615cd67Sstsp  *
11f615cd67Sstsp  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12f615cd67Sstsp  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13f615cd67Sstsp  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14f615cd67Sstsp  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15f615cd67Sstsp  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16f615cd67Sstsp  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17f615cd67Sstsp  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18f615cd67Sstsp  */
19f615cd67Sstsp 
20f615cd67Sstsp #include <sys/param.h>
21f615cd67Sstsp #include <sys/device.h>
22f615cd67Sstsp #include <sys/systm.h>
23f615cd67Sstsp 
24f615cd67Sstsp #include <dev/pci/pcivar.h>
25f615cd67Sstsp #include <dev/pci/pcidevs.h>
26f615cd67Sstsp #include <dev/ic/rtsxreg.h>
27f615cd67Sstsp #include <dev/ic/rtsxvar.h>
28f615cd67Sstsp #include <dev/sdmmc/sdmmcvar.h>
29f615cd67Sstsp 
30f615cd67Sstsp #define RTSX_PCI_BAR 		0x10
312c295eddSjcs #define RTSX_PCI_BAR_525A 	0x14
32f615cd67Sstsp 
33f615cd67Sstsp struct rtsx_pci_softc {
34f615cd67Sstsp 	struct rtsx_softc sc;
35f615cd67Sstsp 	void *sc_ih;
36f615cd67Sstsp };
37f615cd67Sstsp 
38f615cd67Sstsp int	rtsx_pci_match(struct device *, void *, void *);
39f615cd67Sstsp void	rtsx_pci_attach(struct device *, struct device *, void *);
40f615cd67Sstsp 
418d2c75e4Smpi const struct cfattach rtsx_pci_ca = {
42f615cd67Sstsp 	sizeof(struct rtsx_pci_softc), rtsx_pci_match, rtsx_pci_attach,
43f615cd67Sstsp 	NULL, rtsx_activate
44f615cd67Sstsp };
45f615cd67Sstsp 
46f615cd67Sstsp int
rtsx_pci_match(struct device * parent,void * match,void * aux)47f615cd67Sstsp rtsx_pci_match(struct device *parent, void *match, void *aux)
48f615cd67Sstsp {
49f615cd67Sstsp 	struct pci_attach_args *pa = aux;
50f615cd67Sstsp 
51fb9b2be5Sstsp 	/*
52ae1f177aSstsp 	 * Explicitly match the UNDEFINED device class only. Some RTS5209
53fb9b2be5Sstsp 	 * devices advertise a SYSTEM/SDHC class in addition to the UNDEFINED
54fb9b2be5Sstsp 	 * device class. Let sdhc(4) handle the SYSTEM/SDHC ones.
55fb9b2be5Sstsp 	 */
560cd9a838Sstsp 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_REALTEK ||
570cd9a838Sstsp 	    PCI_CLASS(pa->pa_class) != PCI_CLASS_UNDEFINED)
580cd9a838Sstsp 		return 0;
590cd9a838Sstsp 
600cd9a838Sstsp 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS5209 ||
61a8013ee7Sclaudio 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS5227 ||
621244917dSstsp 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS5229 ||
6332973e34Sjsg 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS522A ||
64d66a4d02Sjcs 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS5249 ||
652c295eddSjcs 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTS525A ||
66ae1f177aSstsp 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTL8402 ||
67f85a24beSderaadt 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTL8411 ||
68ae1f177aSstsp 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RTL8411B)
69f615cd67Sstsp 		return 1;
70f615cd67Sstsp 
71f615cd67Sstsp 	return 0;
72f615cd67Sstsp }
73f615cd67Sstsp 
74f615cd67Sstsp void
rtsx_pci_attach(struct device * parent,struct device * self,void * aux)75f615cd67Sstsp rtsx_pci_attach(struct device *parent, struct device *self, void *aux)
76f615cd67Sstsp {
77f615cd67Sstsp 	struct rtsx_pci_softc *sc = (struct rtsx_pci_softc *)self;
78f615cd67Sstsp 	struct pci_attach_args *pa = aux;
79f615cd67Sstsp 	pci_intr_handle_t ih;
80f615cd67Sstsp 	char const *intrstr;
81f615cd67Sstsp 	bus_space_tag_t iot;
82f615cd67Sstsp 	bus_space_handle_t ioh;
83f615cd67Sstsp 	bus_size_t size;
840cd9a838Sstsp 	int flags;
852c295eddSjcs 	int bar = RTSX_PCI_BAR;
86*7b44a193Smiod 	pcireg_t type;
87f615cd67Sstsp 
88f615cd67Sstsp 	if ((pci_conf_read(pa->pa_pc, pa->pa_tag, RTSX_CFG_PCI)
89f615cd67Sstsp 	    & RTSX_CFG_ASIC) != 0) {
90f615cd67Sstsp 		printf("%s: no asic\n", sc->sc.sc_dev.dv_xname);
91f615cd67Sstsp 		return;
92f615cd67Sstsp 	}
93f615cd67Sstsp 
949be16409Sstsp 	if (pci_intr_map_msi(pa, &ih) && pci_intr_map(pa, &ih)) {
95f615cd67Sstsp 		printf(": can't map interrupt\n");
96f615cd67Sstsp 		return;
97f615cd67Sstsp 	}
98f615cd67Sstsp 
99f615cd67Sstsp 	intrstr = pci_intr_string(pa->pa_pc, ih);
100f615cd67Sstsp 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_SDMMC,
101f615cd67Sstsp 	    rtsx_intr, sc, sc->sc.sc_dev.dv_xname);
102f615cd67Sstsp 	if (sc->sc_ih == NULL) {
103f615cd67Sstsp 		printf(": can't establish interrupt\n");
104f615cd67Sstsp 		return;
105f615cd67Sstsp 	}
106f615cd67Sstsp 	printf(": %s\n", intrstr);
107f615cd67Sstsp 
108e518625aSphessler 	switch (PCI_PRODUCT(pa->pa_id)) {
109e518625aSphessler 	case PCI_PRODUCT_REALTEK_RTS5209:
1100cd9a838Sstsp 		flags = RTSX_F_5209;
111e518625aSphessler 		break;
112e518625aSphessler 	case PCI_PRODUCT_REALTEK_RTS5229:
113d66a4d02Sjcs 	case PCI_PRODUCT_REALTEK_RTS5249:
1140cd9a838Sstsp 		flags = RTSX_F_5229;
115e518625aSphessler 		break;
1162c295eddSjcs 	case PCI_PRODUCT_REALTEK_RTS525A:
1172c295eddSjcs 		flags = RTSX_F_525A;
1182c295eddSjcs 		bar = RTSX_PCI_BAR_525A;
1192c295eddSjcs 		break;
120e518625aSphessler 	default:
121e518625aSphessler 		flags = 0;
122e518625aSphessler 		break;
123e518625aSphessler 	}
1240cd9a838Sstsp 
125*7b44a193Smiod 	type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, bar);
126*7b44a193Smiod 	if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, bar, type, NULL, NULL,
127*7b44a193Smiod 	    NULL) != 0) {
1282c295eddSjcs 		printf("%s: can't find registers\n", sc->sc.sc_dev.dv_xname);
1292c295eddSjcs 	    	return;
1302c295eddSjcs 	}
131*7b44a193Smiod 	if (pci_mapreg_map(pa, bar, type, 0, &iot, &ioh, NULL, &size, 0)) {
1322c295eddSjcs 		printf("%s: can't map registers\n", sc->sc.sc_dev.dv_xname);
1332c295eddSjcs 		return;
1342c295eddSjcs 	}
1352c295eddSjcs 
1362c295eddSjcs 	pci_set_powerstate(pa->pa_pc, pa->pa_tag, PCI_PMCSR_STATE_D0);
1372c295eddSjcs 
1380cd9a838Sstsp 	if (rtsx_attach(&sc->sc, iot, ioh, size, pa->pa_dmat, flags) != 0)
139f615cd67Sstsp 		printf("%s: can't initialize chip\n", sc->sc.sc_dev.dv_xname);
140f615cd67Sstsp }
141