xref: /netbsd-src/sys/arch/powerpc/pci/pci_machdep_common.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /* $NetBSD: pci_machdep_common.c,v 1.8 2008/04/28 20:23:32 martin Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Tim Rightnour
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 /*
33  * Generic PowerPC functions for dealing with a PCI bridge.  For most cases,
34  * these functions will work just fine, however, some machines may need
35  * specialized code, so those ports are free to write thier own functions
36  * and call those instead where appropriate.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: pci_machdep_common.c,v 1.8 2008/04/28 20:23:32 martin Exp $");
41 
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/time.h>
45 #include <sys/systm.h>
46 #include <sys/errno.h>
47 #include <sys/extent.h>
48 #include <sys/device.h>
49 #include <sys/malloc.h>
50 
51 #include <uvm/uvm_extern.h>
52 
53 #define _POWERPC_BUS_DMA_PRIVATE
54 #include <machine/bus.h>
55 #include <machine/intr.h>
56 
57 #include <dev/pci/pcivar.h>
58 #include <dev/pci/pcireg.h>
59 #include <dev/pci/pcidevs.h>
60 #include <dev/pci/pciconf.h>
61 #include <dev/pci/pciidereg.h>
62 
63 /*
64  * PCI doesn't have any special needs; just use the generic versions
65  * of these functions.
66  */
67 struct powerpc_bus_dma_tag pci_bus_dma_tag = {
68 	0,			/* _bounce_thresh */
69 	_bus_dmamap_create,
70 	_bus_dmamap_destroy,
71 	_bus_dmamap_load,
72 	_bus_dmamap_load_mbuf,
73 	_bus_dmamap_load_uio,
74 	_bus_dmamap_load_raw,
75 	_bus_dmamap_unload,
76 	NULL,			/* _dmamap_sync */
77 	_bus_dmamem_alloc,
78 	_bus_dmamem_free,
79 	_bus_dmamem_map,
80 	_bus_dmamem_unmap,
81 	_bus_dmamem_mmap,
82 };
83 
84 int
85 genppc_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
86 {
87 	return 32;
88 }
89 
90 const char *
91 genppc_pci_intr_string(void *v, pci_intr_handle_t ih)
92 {
93 	static char irqstr[8];		/* 4 + 2 + NULL + sanity */
94 
95 	if (ih == 0 || ih >= ICU_LEN
96 /* XXX on macppc it's completely legal to have PCI interrupts on a slave PIC */
97 #ifdef IRQ_SLAVE
98 	    || ih == IRQ_SLAVE
99 #endif
100 	    )
101 		panic("pci_intr_string: bogus handle 0x%x", ih);
102 
103 	sprintf(irqstr, "irq %d", ih);
104 	return (irqstr);
105 
106 }
107 
108 const struct evcnt *
109 genppc_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
110 {
111 
112 	/* XXX for now, no evcnt parent reported */
113 	return NULL;
114 }
115 
116 void *
117 genppc_pci_intr_establish(void *v, pci_intr_handle_t ih, int level,
118     int (*func)(void *), void *arg)
119 {
120 
121 	if (ih == 0 || ih >= ICU_LEN
122 #ifdef IRQ_SLAVE
123 	    || ih == IRQ_SLAVE
124 #endif
125 	    )
126 		panic("pci_intr_establish: bogus handle 0x%x", ih);
127 
128 	return intr_establish(ih, IST_LEVEL, level, func, arg);
129 }
130 
131 void
132 genppc_pci_intr_disestablish(void *v, void *cookie)
133 {
134 
135 	intr_disestablish(cookie);
136 }
137 
138 void
139 genppc_pci_conf_interrupt(pci_chipset_tag_t pct, int bus, int dev, int pin,
140     int swiz, int *iline)
141 {
142 	/* do nothing */
143 }
144 
145 int
146 genppc_pci_conf_hook(pci_chipset_tag_t pct, int bus, int dev, int func,
147 	pcireg_t id)
148 {
149 	return (PCI_CONF_DEFAULT);
150 }
151 
152 int
153 genppc_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
154 {
155 	int pin = pa->pa_intrpin;
156 	int line = pa->pa_intrline;
157 
158 #ifdef DEBUG
159 	printf("%s: pin: %d, line: %d\n", __func__, pin, line);
160 #endif
161 
162 	if (pin == 0) {
163 		/* No IRQ used. */
164 		aprint_error("pci_intr_map: interrupt pin %d\n", pin);
165 		goto bad;
166 	}
167 
168 	if (pin > 4) {
169 		aprint_error("pci_intr_map: bad interrupt pin %d\n", pin);
170 		goto bad;
171 	}
172 
173 	/*
174 	 * Section 6.2.4, `Miscellaneous Functions', says that 255 means
175 	 * `unknown' or `no connection' on a PC.  We assume that a device with
176 	 * `no connection' either doesn't have an interrupt (in which case the
177 	 * pin number should be 0, and would have been noticed above), or
178 	 * wasn't configured by the BIOS (in which case we punt, since there's
179 	 * no real way we can know how the interrupt lines are mapped in the
180 	 * hardware).
181 	 *
182 	 * XXX
183 	 * Since IRQ 0 is only used by the clock, and we can't actually be sure
184 	 * that the BIOS did its job, we also recognize that as meaning that
185 	 * the BIOS has not configured the device.
186 	 */
187 	if (line == 0 || line == 255) {
188 		aprint_error("pci_intr_map: no mapping for pin %c\n", '@' + pin);
189 		goto bad;
190 	} else {
191 		if (line >= ICU_LEN) {
192 			aprint_error("pci_intr_map: bad interrupt line %d\n", line);
193 			goto bad;
194 		}
195 	}
196 
197 	*ihp = line;
198 	return 0;
199 
200 bad:
201 	*ihp = -1;
202 	return 1;
203 }
204 
205 #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
206 #include <machine/isa_machdep.h>
207 #include "isa.h"
208 
209 void *genppc_pciide_machdep_compat_intr_establish(struct device *,
210     struct pci_attach_args *, int, int (*)(void *), void *);
211 
212 void *
213 genppc_pciide_machdep_compat_intr_establish(struct device *dev,
214     struct pci_attach_args *pa, int chan, int (*func)(void *), void *arg)
215 {
216 #if NISA > 0
217 	int irq;
218 	void *cookie;
219 
220 	irq = PCIIDE_COMPAT_IRQ(chan);
221 	cookie = isa_intr_establish(NULL, irq, IST_LEVEL, IPL_BIO, func, arg);
222 	if (cookie == NULL)
223 		return (NULL);
224 	printf("%s: %s channel interrupting at irq %d\n", dev->dv_xname,
225 	    PCIIDE_CHANNEL_NAME(chan), irq);
226 	return (cookie);
227 #else
228 	panic("pciide_machdep_compat_intr_establish() called");
229 #endif
230 }
231 #endif /* __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH */
232