xref: /netbsd/sys/arch/powerpc/ibm4xx/pci/pchb.c (revision 6550d01e)
1 /*	$NetBSD: pchb.c,v 1.6 2008/05/04 00:08:45 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996 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 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: pchb.c,v 1.6 2008/05/04 00:08:45 martin Exp $");
33 
34 #include "pci.h"
35 #include "opt_pci.h"
36 
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/extent.h>
42 #include <sys/malloc.h>
43 
44 #define _IBM4XX_BUS_DMA_PRIVATE
45 
46 #include <powerpc/ibm4xx/ibm405gp.h>
47 #include <powerpc/ibm4xx/dev/plbvar.h>
48 
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcidevs.h>
52 #include <dev/pci/pciconf.h>
53 
54 static int	pchbmatch(struct device *, struct cfdata *, void *);
55 static void	pchbattach(struct device *, struct device *, void *);
56 static int	pchbprint(void *, const char *);
57 static pci_chipset_tag_t	alloc_chipset_tag(int);
58 
59 CFATTACH_DECL(pchb, sizeof(struct device),
60     pchbmatch, pchbattach, NULL, NULL);
61 
62 static int pcifound = 0;
63 
64 /* IO window located @ e8000000 and maps to 0-0xffff */
65 static struct powerpc_bus_space pchb_io_tag = {
66 	_BUS_SPACE_LITTLE_ENDIAN | _BUS_SPACE_IO_TYPE,
67 	IBM405GP_PLB_PCI_IO_START,		/* offset */
68 	IBM405GP_PCI_PCI_IO_START,		/* extent base */
69 	IBM405GP_PCI_PCI_IO_START + 0xffff,	/* extent limit */
70 };
71 
72 /* PCI memory window is directly mapped */
73 static struct powerpc_bus_space pchb_mem_tag = {
74 	_BUS_SPACE_LITTLE_ENDIAN | _BUS_SPACE_MEM_TYPE,
75 	0x00000000,					/* offset */
76 	IBM405GP_PCI_MEM_START,			/* extent base */
77 	IBM405GP_PCI_MEM_START + 0x1fffffff,	/* extent limit */
78 };
79 
80 
81 static int
82 pchbmatch(struct device *parent, struct cfdata *cf, void *aux)
83 {
84 	struct plb_attach_args *paa = aux;
85 	/* XXX chipset tag unused by walnut, so just pass 0 */
86 	pci_chipset_tag_t pc = alloc_chipset_tag(0);
87 	pcitag_t tag;
88 	int class, id;
89 
90 	/* match only pchb devices */
91 	if (strcmp(paa->plb_name, cf->cf_name) != 0)
92 		return 0;
93 
94 	pci_machdep_init();
95 	tag = pci_make_tag(pc, 0, 0, 0);
96 
97 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
98 	id = pci_conf_read(pc, tag, PCI_ID_REG);
99 
100 	/*
101 	 * Match all known PCI host chipsets.
102 	 */
103 	if (PCI_CLASS(class) == PCI_CLASS_BRIDGE &&
104 	    PCI_SUBCLASS(class) == PCI_SUBCLASS_BRIDGE_HOST) {
105 		switch (PCI_VENDOR(id)) {
106 		case PCI_VENDOR_IBM:
107 			switch (PCI_PRODUCT(id)) {
108 			case PCI_PRODUCT_IBM_405GP:
109 				return (!pcifound);
110 			}
111 			break;
112 		}
113 	}
114 	return (0);
115 }
116 
117 static void
118 pchbattach(struct device *parent, struct device *self, void *aux)
119 {
120 	struct plb_attach_args *paa = aux;
121 	struct pcibus_attach_args pba;
122 	char devinfo[256];
123 #ifdef PCI_CONFIGURE_VERBOSE
124 	extern int pci_conf_debug;
125 
126 	pci_conf_debug = 1;
127 #endif
128 	pci_chipset_tag_t pc = alloc_chipset_tag(0);
129 	pcitag_t tag;
130 	int class, id;
131 
132 	pci_machdep_init();
133 	tag = pci_make_tag(pc, 0, 0, 0);
134 
135 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
136 	id = pci_conf_read(pc, tag, PCI_ID_REG);
137 
138 	printf("\n");
139 	pcifound++;
140 	/*
141 	 * All we do is print out a description.  Eventually, we
142 	 * might want to add code that does something that's
143 	 * possibly chipset-specific.
144 	 */
145 
146 	pci_devinfo(id, class, 0, devinfo, sizeof(devinfo));
147 	printf("%s: %s (rev. 0x%02x)\n", self->dv_xname, devinfo,
148 	    PCI_REVISION(class));
149 
150 	pci_machdep_init(); /* Redundant... */
151 	ibm4xx_setup_pci();
152 #ifdef PCI_CONFIGURE_VERBOSE
153 	ibm4xx_show_pci_map();
154 #endif
155 
156 	if (bus_space_init(&pchb_io_tag, "pchbio", NULL, 0))
157 		panic("pchbattach: can't init IO tag");
158 	if (bus_space_init(&pchb_mem_tag, "pchbmem", NULL, 0))
159 		panic("pchbattach: can't init MEM tag");
160 
161 #ifdef PCI_NETBSD_CONFIGURE
162 	pc->memext = extent_create("pcimem", IBM405GP_PCI_MEM_START,
163 	    IBM405GP_PCI_MEM_START + 0x1fffffff, M_DEVBUF, NULL, 0,
164 	    EX_NOWAIT);
165 	pc->ioext = extent_create("pciio", IBM405GP_PCI_PCI_IO_START,
166 	    IBM405GP_PCI_PCI_IO_START + 0xffff, M_DEVBUF, NULL, 0, EX_NOWAIT);
167 	pci_configure_bus(pc, pc->ioext, pc->memext, NULL, 0, 32);
168 #endif /* PCI_NETBSD_CONFIGURE */
169 
170 #ifdef PCI_CONFIGURE_VERBOSE
171 	printf("running config_found PCI\n");
172 #endif
173 	/* IO window located @ e8000000 and maps to 0-0xffff */
174 	pba.pba_iot = &pchb_io_tag;
175 	/* PCI memory window is directly mapped */
176 	pba.pba_memt = &pchb_mem_tag;
177 	pba.pba_dmat = paa->plb_dmat;
178 	pba.pba_dmat64 = NULL;
179 	pba.pba_pc = pc;
180 	pba.pba_bus = 0;
181 	pba.pba_bridgetag = NULL;
182 	pba.pba_flags = PCI_FLAGS_MEM_ENABLED | PCI_FLAGS_IO_ENABLED;
183 	config_found_ia(self, "pcibus", &pba, pchbprint);
184 }
185 
186 
187 static int
188 pchbprint(void *aux, const char *p)
189 {
190 
191 	if (p == NULL)
192 		return (UNCONF);
193 	return (QUIET);
194 }
195 
196 #if 0
197 static void
198 scan_pci_bus(void)
199 {
200 	pcitag_t tag;
201 	int i, x;
202 
203 	for (i=0;i<32;i++){
204 		tag = pci_make_tag(0, 0, i, 0);
205 		x = pci_conf_read(0, tag, 0);
206 		printf("%d tag=%08x : %08x\n", i, tag, x);
207 #if 0
208 		if (PCI_VENDOR(x) == PCI_VENDOR_INTEL
209 		    && PCI_PRODUCT(x) == PCI_PRODUCT_INTEL_80960_RP) {
210 			/* Do not configure PCI bus analyzer */
211 			continue;
212 		}
213 		x = pci_conf_read(0, tag, PCI_COMMAND_STATUS_REG);
214 		x |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
215 		pci_conf_write(0, tag, PCI_COMMAND_STATUS_REG, x);
216 #endif
217 	}
218 }
219 #endif
220 
221 static pci_chipset_tag_t
222 alloc_chipset_tag(int node)
223 {
224 	pci_chipset_tag_t npc;
225 
226 	npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
227 	if (npc == NULL)
228 		panic("could not allocate pci_chipset_tag_t");
229 	npc->rootnode = node;
230 
231 	return (npc);
232 }
233