xref: /netbsd-src/sys/arch/evbmips/loongson/dev/pcib.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: pcib.c,v 1.1 2011/08/27 13:42:46 bouyer Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: pcib.c,v 1.1 2011/08/27 13:42:46 bouyer Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 
39 
40 #include <evbmips/loongson/autoconf.h>
41 
42 #include <dev/pci/pcireg.h>
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcidevs.h>
45 
46 #include <dev/isa/isavar.h>
47 
48 #include <evbmips/loongson/loongson_bus_defs.h>
49 #include <evbmips/loongson/dev/pcibvar.h>
50 
51 #include "isa.h"
52 
53 int	pcibmatch(device_t, cfdata_t, void *);
54 void	pcib_callback(device_t);
55 
56 CFATTACH_DECL3_NEW(pcib, sizeof(struct pcib_softc),
57     pcibmatch, pcibattach, pcibdetach, NULL, pcibrescan, pcibchilddet,
58 	DVF_DETACH_SHUTDOWN);
59 
60 
61 int
62 pcibmatch(device_t parent, cfdata_t match, void *aux)
63 {
64 	struct pci_attach_args *pa = aux;
65 
66 	switch (PCI_VENDOR(pa->pa_id)) {
67 	case PCI_VENDOR_INTEL:
68 		switch (PCI_PRODUCT(pa->pa_id)) {
69 		case PCI_PRODUCT_INTEL_SIO:
70 		case PCI_PRODUCT_INTEL_82371MX:
71 		case PCI_PRODUCT_INTEL_82371AB_ISA:
72 		case PCI_PRODUCT_INTEL_82440MX_ISA:
73 			/* The above bridges mis-identify themselves */
74 			return (1);
75 		}
76 		break;
77 	case PCI_VENDOR_SIS:
78 		switch (PCI_PRODUCT(pa->pa_id)) {
79 		case PCI_PRODUCT_SIS_85C503:
80 			/* mis-identifies itself as a miscellaneous prehistoric */
81 			return (1);
82 		}
83 		break;
84 	case PCI_VENDOR_VIATECH:
85 		switch (PCI_PRODUCT(pa->pa_id)) {
86 		case PCI_PRODUCT_VIATECH_VT82C686A_SMB:
87 			/* mis-identifies itself as a ISA bridge */
88 			return (0);
89 		}
90 		break;
91 	}
92 
93 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
94 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_ISA)
95 		return (1);
96 
97 	return (0);
98 }
99 
100 void
101 pcibattach(device_t parent, device_t self, void *aux)
102 {
103 	struct pcib_softc *sc = device_private(self);
104 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
105 	char devinfo[256];
106 
107 	aprint_naive("\n");
108 
109 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
110 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
111 	    PCI_REVISION(pa->pa_class));
112 
113 	/*
114 	 * Wait until all PCI devices are attached before attaching isa;
115 	 * otherwise this might mess the interrupt setup on some systems.
116 	 */
117 	sc->sc_pc = pa->pa_pc;
118 	sc->sc_tag = pa->pa_tag;
119 
120 	if (!pmf_device_register(self, NULL, NULL))
121 		aprint_error_dev(self, "couldn't establish power handler\n");
122 	config_defer(self, pcib_callback);
123 }
124 
125 int
126 pcibdetach(device_t self, int flags)
127 {
128 	int rc;
129 
130 	if ((rc = config_detach_children(self, flags)) != 0)
131 		return rc;
132 	pmf_device_deregister(self);
133 	return 0;
134 }
135 
136 void
137 pcibchilddet(device_t self, device_t child)
138 {
139 	struct pcib_softc *sc = device_private(self);
140 
141 	if (sc->sc_isabus == child)
142 		sc->sc_isabus = NULL;
143 }
144 
145 int
146 pcibrescan(device_t self, const char *ifattr, const int *loc)
147 {
148 	struct pcib_softc *sc = device_private(self);
149 	struct isabus_attach_args iba;
150 
151 	if (ifattr_match(ifattr, "isabus") && sc->sc_isabus == NULL) {
152 		/*
153 		 * Attach the ISA bus behind this bridge.
154 		 */
155 		memset(&iba, 0, sizeof(iba));
156 		iba.iba_iot = &bonito_iot;
157 		iba.iba_memt = &bonito_memt;
158 #if NISA > 0
159 		iba.iba_dmat = &bonito_dmat;
160 #endif
161 		iba.iba_ic = sys_platform->isa_chipset;
162 
163 		if (iba.iba_ic != NULL)
164 			sc->sc_isabus =
165 			    config_found_ia(self, "isabus", &iba, isabusprint);
166 	}
167 	return 0;
168 }
169 
170 
171 void
172 pcib_callback(device_t self)
173 {
174 	pcibrescan(self, "isabus", NULL);
175 }
176