pci_machdep.c revision 1.3 1 /* $NetBSD: pci_machdep.c,v 1.3 2003/03/18 16:40:21 matt Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Charles M. Hannum.
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 /*
34 * Machine-specific functions for PCI autoconfiguration.
35 *
36 * On PCs, there are two methods of generating PCI configuration cycles.
37 * We try to detect the appropriate mechanism for this machine and set
38 * up a few function pointers to access the correct method directly.
39 */
40
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/time.h>
44 #include <sys/systm.h>
45 #include <sys/errno.h>
46 #include <sys/device.h>
47
48 #include <uvm/uvm_extern.h>
49
50 #define _POWERPC_BUS_DMA_PRIVATE
51 #include <machine/bus.h>
52 #include <machine/intr.h>
53 #include <machine/platform.h>
54
55 #include <powerpc/pio.h>
56
57 #include <dev/isa/isavar.h>
58 #include <dev/pci/pcivar.h>
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcidevs.h>
61
62 #define PCI_MODE1_ENABLE 0x80000000UL
63 #define PCI_MODE1_ADDRESS_REG (MVMEPPC_KVA_BASE_IO + 0xcf8)
64 #define PCI_MODE1_DATA_REG (MVMEPPC_KVA_BASE_IO + 0xcfc)
65
66 #define o2i(off) ((off)/sizeof(pcireg_t))
67
68 void pci_intr_fixup(int, int, int *);
69
70 #ifdef DEBUG
71 #define DPRF(x) printf x
72 #else
73 #define DPRF(x)
74 #endif
75
76 /*
77 * PCI doesn't have any special needs; just use the generic versions
78 * of these functions.
79 */
80 struct powerpc_bus_dma_tag pci_bus_dma_tag = {
81 0, /* _bounce_thresh */
82 _bus_dmamap_create,
83 _bus_dmamap_destroy,
84 _bus_dmamap_load,
85 _bus_dmamap_load_mbuf,
86 _bus_dmamap_load_uio,
87 _bus_dmamap_load_raw,
88 _bus_dmamap_unload,
89 NULL, /* _dmamap_sync */
90 _bus_dmamem_alloc,
91 _bus_dmamem_free,
92 _bus_dmamem_map,
93 _bus_dmamem_unmap,
94 _bus_dmamem_mmap,
95 };
96
97 void
98 pci_attach_hook(struct device *parent, struct device *self,
99 struct pcibus_attach_args *pba)
100 {
101
102 /* Nothing to do. */
103 }
104
105 int
106 pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
107 {
108
109 /*
110 * Bus number is irrelevant. Configuration Mechanism 1 is in
111 * use, can have devices 0-32 (i.e. the `normal' range).
112 */
113 return (32);
114 }
115
116 pcitag_t
117 pci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
118 {
119 pcitag_t tag;
120
121 if (bus >= 256 || device >= 32 || function >= 8)
122 panic("pci_make_tag: bad request");
123
124 tag = PCI_MODE1_ENABLE |
125 (bus << 16) | (device << 11) | (function << 8);
126 return tag;
127 }
128
129 void
130 pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag, int *bp, int *dp, int *fp)
131 {
132
133 if (bp != NULL)
134 *bp = (tag >> 16) & 0xff;
135 if (dp != NULL)
136 *dp = (tag >> 11) & 0x1f;
137 if (fp != NULL)
138 *fp = (tag >> 8) & 0x7;
139 return;
140 }
141
142 pcireg_t
143 pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
144 {
145 pcireg_t data;
146
147 out32rb(PCI_MODE1_ADDRESS_REG, tag | reg);
148 data = in32rb(PCI_MODE1_DATA_REG);
149 out32rb(PCI_MODE1_ADDRESS_REG, 0);
150 return data;
151 }
152
153 void
154 pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
155 {
156
157 out32rb(PCI_MODE1_ADDRESS_REG, tag | reg);
158 out32rb(PCI_MODE1_DATA_REG, data);
159 out32rb(PCI_MODE1_ADDRESS_REG, 0);
160 }
161
162 int
163 pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
164 {
165 int pin = pa->pa_intrpin;
166 int line = pa->pa_intrline;
167
168 if (pin == 0) {
169 /* No IRQ used. */
170 goto bad;
171 }
172
173 if (pin > 4) {
174 printf("pci_intr_map: bad interrupt pin %d\n", pin);
175 goto bad;
176 }
177
178 /*
179 * Section 6.2.4, `Miscellaneous Functions', says that 255 means
180 * `unknown' or `no connection' on a PC. We assume that a device with
181 * `no connection' either doesn't have an interrupt (in which case the
182 * pin number should be 0, and would have been noticed above), or
183 * wasn't configured by the BIOS (in which case we punt, since there's
184 * no real way we can know how the interrupt lines are mapped in the
185 * hardware).
186 *
187 * XXX
188 * Since IRQ 0 is only used by the clock, and we can't actually be sure
189 * that the BIOS did its job, we also recognize that as meaning that
190 * the BIOS has not configured the device.
191 */
192 if (line == 0 || line == 255) {
193 printf("pci_intr_map: no mapping for pin %c\n", '@' + pin);
194 goto bad;
195 } else {
196 if (line >= ICU_LEN) {
197 printf("pci_intr_map: bad interrupt line %d\n", line);
198 goto bad;
199 }
200 if (line == IRQ_SLAVE) {
201 printf("pci_intr_map: changed line 2 to line 9\n");
202 line = 9;
203 }
204 }
205
206 *ihp = line;
207 return 0;
208
209 bad:
210 *ihp = -1;
211 return 1;
212 }
213
214 const char *
215 pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih)
216 {
217 static char irqstr[8]; /* 4 + 2 + NULL + sanity */
218
219 if (ih == 0 || ih >= ICU_LEN || ih == IRQ_SLAVE)
220 panic("pci_intr_string: bogus handle 0x%x", ih);
221
222 sprintf(irqstr, "irq %d", ih);
223 return (irqstr);
224
225 }
226
227 const struct evcnt *
228 pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
229 {
230
231 /* XXX for now, no evcnt parent reported */
232 return NULL;
233 }
234
235 void *
236 pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
237 int (*func)(void *), void *arg)
238 {
239
240 if (ih == 0 || ih >= ICU_LEN || ih == IRQ_SLAVE)
241 panic("pci_intr_establish: bogus handle 0x%x", ih);
242
243 return isa_intr_establish(NULL, ih, IST_LEVEL, level, func, arg);
244 }
245
246 void
247 pci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
248 {
249
250 isa_intr_disestablish(NULL, cookie);
251 }
252
253 void
254 pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin,
255 int swiz, int *iline)
256 {
257
258 (*platform->pci_intr_fixup)(bus, dev, iline);
259 }
260