1 /* $NetBSD: mainbus.c,v 1.40 2021/08/07 16:19:03 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 1.40 2021/08/07 16:19:03 thorpej Exp $");
35
36 #include "opt_pci.h"
37 #include "opt_residual.h"
38
39 #include "pnpbus.h"
40 #include "pci.h"
41 #include "isa.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 #include <sys/kmem.h>
47
48 #include <machine/autoconf.h>
49 #include <sys/bus.h>
50 #include <machine/isa_machdep.h>
51
52 #include <dev/pci/pcivar.h>
53 #include <dev/pci/pciconf.h>
54
55 #include <prep/pnpbus/pnpbusvar.h>
56
57 #include <machine/platform.h>
58 #include <machine/residual.h>
59
60 int mainbus_match(device_t, cfdata_t, void *);
61 void mainbus_attach(device_t, device_t, void *);
62
63 CFATTACH_DECL_NEW(mainbus, 0,
64 mainbus_match, mainbus_attach, NULL, NULL);
65
66 int mainbus_print(void *, const char *);
67
68 union mainbus_attach_args {
69 const char *mba_busname; /* first elem of all */
70 struct pcibus_attach_args mba_pba;
71 struct pnpbus_attach_args mba_paa;
72 };
73
74 /* There can be only one. */
75 int mainbus_found = 0;
76 struct powerpc_isa_chipset genppc_ict;
77 struct genppc_pci_chipset *genppc_pct;
78
79 #define PCI_IO_START 0x00008000
80 #define PCI_IO_END 0x0000ffff
81 #define PCI_IO_SIZE ((PCI_IO_END - PCI_IO_START) + 1)
82
83 #define PCI_MEM_START 0x00000000
84 #define PCI_MEM_END 0x0fffffff
85 #define PCI_MEM_SIZE ((PCI_MEM_END - PCI_MEM_START) + 1)
86
87 /*
88 * Probe for the mainbus; always succeeds.
89 */
90 int
mainbus_match(device_t parent,cfdata_t match,void * aux)91 mainbus_match(device_t parent, cfdata_t match, void *aux)
92 {
93
94 if (mainbus_found)
95 return 0;
96 return 1;
97 }
98
99 /*
100 * Attach the mainbus.
101 */
102 void
mainbus_attach(device_t parent,device_t self,void * aux)103 mainbus_attach(device_t parent, device_t self, void *aux)
104 {
105 union mainbus_attach_args mba;
106 struct confargs ca;
107 int i;
108 #if NPCI > 0
109 struct genppc_pci_chipset_businfo *pbi;
110 #endif
111
112 mainbus_found = 1;
113
114 aprint_normal("\n");
115
116 #if defined(RESIDUAL_DATA_DUMP)
117 print_residual_device_info();
118 #endif
119
120 for (i = 0; i < CPU_MAXNUM; i++) {
121 ca.ca_name = "cpu";
122 ca.ca_node = i;
123 config_found(self, &ca, NULL,
124 CFARGS(.iattr = "mainbus"));
125 }
126
127 /*
128 * XXX Note also that the presence of a PCI bus should
129 * XXX _always_ be checked, and if present the bus should be
130 * XXX 'found'. However, because of the structure of the code,
131 * XXX that's not currently possible.
132 */
133 #if NPCI > 0
134 genppc_pct = kmem_alloc(sizeof(struct genppc_pci_chipset), KM_SLEEP);
135 prep_pci_get_chipset_tag(genppc_pct);
136
137 pbi = kmem_alloc(sizeof(struct genppc_pci_chipset_businfo), KM_SLEEP);
138 pbi->pbi_properties = prop_dictionary_create();
139 KASSERT(pbi->pbi_properties != NULL);
140
141 SIMPLEQ_INIT(&genppc_pct->pc_pbi);
142 SIMPLEQ_INSERT_TAIL(&genppc_pct->pc_pbi, pbi, next);
143
144 /* fix pci interrupt routings on some models */
145 setup_pciroutinginfo();
146
147 /* find the primary host bridge */
148 setup_pciintr_map(pbi, 0, 0, 0);
149
150 #ifdef PCI_NETBSD_CONFIGURE
151 struct pciconf_resources *pcires = pciconf_resource_init();
152
153 pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
154 PCI_IO_START, PCI_IO_SIZE);
155 pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
156 PCI_MEM_START, PCI_MEM_SIZE);
157
158 pci_configure_bus(genppc_pct, pcires, 0, CACHELINESIZE);
159
160 pciconf_resource_fini(pcires);
161 #endif /* PCI_NETBSD_CONFIGURE */
162 #endif /* NPCI */
163
164 /* scan pnpbus first */
165 #if NPNPBUS > 0
166 mba.mba_paa.paa_name = "pnpbus";
167 mba.mba_paa.paa_iot = &genppc_isa_io_space_tag;
168 mba.mba_paa.paa_memt = &genppc_isa_mem_space_tag;
169 mba.mba_paa.paa_ic = &genppc_ict;
170 mba.mba_paa.paa_dmat = &isa_bus_dma_tag;
171 config_found(self, &mba.mba_pba, mainbus_print,
172 CFARGS(.iattr = "mainbus"));
173 #endif /* NPNPBUS */
174
175 #if NPCI > 0
176 memset(&mba, 0, sizeof(mba));
177 mba.mba_pba._pba_busname = NULL;
178 mba.mba_pba.pba_iot = &prep_io_space_tag;
179 mba.mba_pba.pba_memt = &prep_mem_space_tag;
180 mba.mba_pba.pba_dmat = &pci_bus_dma_tag;
181 mba.mba_pba.pba_dmat64 = NULL;
182 mba.mba_pba.pba_pc = genppc_pct;
183 mba.mba_pba.pba_bus = 0;
184 mba.mba_pba.pba_bridgetag = NULL;
185 mba.mba_pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY;
186 config_found(self, &mba.mba_pba, pcibusprint,
187 CFARGS(.iattr = "pcibus"));
188 #endif /* NPCI */
189
190 #ifdef RESIDUAL_DATA_DUMP
191 SIMPLEQ_FOREACH(pbi, &genppc_pct->pc_pbi, next)
192 printf("%s\n", prop_dictionary_externalize(pbi->pbi_properties));
193 #endif
194 }
195
196 int
mainbus_print(void * aux,const char * pnp)197 mainbus_print(void *aux, const char *pnp)
198 {
199 union mainbus_attach_args *mba = aux;
200
201 if (pnp)
202 aprint_normal("%s at %s", mba->mba_busname, pnp);
203
204 return (UNCONF);
205 }
206