1 /*        $NetBSD: pcib.c,v 1.4 2021/08/07 16:18:51 thorpej 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.4 2021/08/07 16:18:51 thorpej 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
pcibmatch(device_t parent,cfdata_t match,void * aux)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_PWR:
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
pcibattach(device_t parent,device_t self,void * aux)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
pcibdetach(device_t self,int flags)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
pcibchilddet(device_t self,device_t child)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
pcibrescan(device_t self,const char * ifattr,const int * loc)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           /*
152            * pcib only carries "isabus", but this can also be used
153            * by other drivers that carry additional interface attributes.
154            */
155           if (ifattr_match(ifattr, "isabus") && sc->sc_isabus == NULL) {
156                     /*
157                      * Attach the ISA bus behind this bridge.
158                      */
159                     memset(&iba, 0, sizeof(iba));
160                     iba.iba_iot = &bonito_iot;
161                     iba.iba_memt = &bonito_memt;
162 #if NISA > 0
163                     iba.iba_dmat = &bonito_dmat;
164 #endif
165                     iba.iba_ic = sys_platform->isa_chipset;
166 
167                     if (iba.iba_ic != NULL)
168                               sc->sc_isabus =
169                                   config_found(self, &iba, isabusprint,
170                                                    CFARGS(.iattr = "isabus"));
171           }
172           return 0;
173 }
174 
175 
176 void
pcib_callback(device_t self)177 pcib_callback(device_t self)
178 {
179           pcibrescan(self, "isabus", NULL);
180 }
181