ifpga_pci.c revision 1.8 1 /* $NetBSD: ifpga_pci.c,v 1.8 2003/09/06 11:31:22 rearnsha Exp $ */
2
3 /*
4 * Copyright (c) 2001 ARM Ltd
5 * 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. The name of the company may not be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * Copyright (c) 1997,1998 Mark Brinicombe.
32 * Copyright (c) 1997,1998 Causality Limited
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by Mark Brinicombe
46 * for the NetBSD Project.
47 * 4. The name of the company nor the name of the author may be used to
48 * endorse or promote products derived from this software without specific
49 * prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
52 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
55 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63
64 #define _ARM32_BUS_DMA_PRIVATE
65
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: ifpga_pci.c,v 1.8 2003/09/06 11:31:22 rearnsha Exp $");
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/conf.h>
72 #include <sys/malloc.h>
73 #include <sys/device.h>
74
75 #include <evbarm/integrator/int_bus_dma.h>
76
77 #include <machine/intr.h>
78 #include <evbarm/ifpga/irqhandler.h> /* XXX XXX XXX */
79
80 #include <dev/pci/pcireg.h>
81 #include <dev/pci/pcivar.h>
82
83 #include <evbarm/ifpga/ifpgareg.h>
84 #include <evbarm/ifpga/ifpgamem.h>
85 #include <evbarm/ifpga/ifpga_pcivar.h>
86 #include <evbarm/dev/v360reg.h>
87
88
89 void ifpga_pci_attach_hook (struct device *, struct device *,
90 struct pcibus_attach_args *);
91 int ifpga_pci_bus_maxdevs (void *, int);
92 pcitag_t ifpga_pci_make_tag (void *, int, int, int);
93 void ifpga_pci_decompose_tag (void *, pcitag_t, int *, int *,
94 int *);
95 pcireg_t ifpga_pci_conf_read (void *, pcitag_t, int);
96 void ifpga_pci_conf_write (void *, pcitag_t, int, pcireg_t);
97 int ifpga_pci_intr_map (struct pci_attach_args *,
98 pci_intr_handle_t *);
99 const char *ifpga_pci_intr_string (void *, pci_intr_handle_t);
100 const struct evcnt *ifpga_pci_intr_evcnt (void *, pci_intr_handle_t);
101 void *ifpga_pci_intr_establish (void *, pci_intr_handle_t, int,
102 int (*)(void *), void *);
103 void ifpga_pci_intr_disestablish (void *, void *);
104
105 struct arm32_pci_chipset ifpga_pci_chipset = {
106 NULL, /* conf_v */
107 ifpga_pci_attach_hook,
108 ifpga_pci_bus_maxdevs,
109 ifpga_pci_make_tag,
110 ifpga_pci_decompose_tag,
111 ifpga_pci_conf_read,
112 ifpga_pci_conf_write,
113 NULL, /* intr_v */
114 ifpga_pci_intr_map,
115 ifpga_pci_intr_string,
116 ifpga_pci_intr_evcnt,
117 ifpga_pci_intr_establish,
118 ifpga_pci_intr_disestablish
119 };
120
121 /*
122 * Use the integrator-specific bus_dma routines.
123 */
124 struct arm32_bus_dma_tag ifpga_pci_bus_dma_tag = {
125 0,
126 0,
127 NULL,
128 _bus_dmamap_create,
129 _bus_dmamap_destroy,
130 _bus_dmamap_load,
131 _bus_dmamap_load_mbuf,
132 _bus_dmamap_load_uio,
133 _bus_dmamap_load_raw,
134 _bus_dmamap_unload,
135 _bus_dmamap_sync, /* pre */
136 NULL, /* post */
137 _bus_dmamem_alloc,
138 _bus_dmamem_free,
139 _bus_dmamem_map,
140 _bus_dmamem_unmap,
141 _bus_dmamem_mmap,
142 };
143
144 /*
145 * Currently we only support 12 devices as we select directly in the
146 * type 0 config cycle
147 * (See conf_{read,write} for more detail
148 */
149 #define MAX_PCI_DEVICES 21
150
151 /*static int
152 pci_intr(void *arg)
153 {
154 printf("pci int %x\n", (int)arg);
155 return 0;
156 }*/
157
158
159 void
160 ifpga_pci_attach_hook(struct device *parent, struct device *self,
161 struct pcibus_attach_args *pba)
162 {
163 #ifdef PCI_DEBUG
164 printf("ifpga_pci_attach_hook()\n");
165 #endif
166 }
167
168 int
169 ifpga_pci_bus_maxdevs(void *pcv, int busno)
170 {
171 #ifdef PCI_DEBUG
172 printf("ifpga_pci_bus_maxdevs(pcv=%p, busno=%d)\n", pcv, busno);
173 #endif
174 return MAX_PCI_DEVICES;
175 }
176
177 pcitag_t
178 ifpga_pci_make_tag(void *pcv, int bus, int device, int function)
179 {
180 #ifdef PCI_DEBUG
181 printf("ifpga_pci_make_tag(pcv=%p, bus=%d, device=%d, function=%d)\n",
182 pcv, bus, device, function);
183 #endif
184 return (bus << 16) | (device << 11) | (function << 8);
185 }
186
187 void
188 ifpga_pci_decompose_tag(void *pcv, pcitag_t tag, int *busp, int *devicep,
189 int *functionp)
190 {
191 #ifdef PCI_DEBUG
192 printf("ifpga_pci_decompose_tag(pcv=%p, tag=0x%08lx, bp=%p, dp=%p, "
193 "fp=%p)\n", pcv, tag, busp, devicep, functionp);
194 #endif
195
196 if (busp != NULL)
197 *busp = (tag >> 16) & 0xff;
198 if (devicep != NULL)
199 *devicep = (tag >> 11) & 0x1f;
200 if (functionp != NULL)
201 *functionp = (tag >> 8) & 0x7;
202 }
203
204 pcireg_t
205 ifpga_pci_conf_read(void *pcv, pcitag_t tag, int reg)
206 {
207 pcireg_t data;
208 struct ifpga_pci_softc *sc = (struct ifpga_pci_softc *)pcv;
209 int bus, device, function;
210 u_int address;
211
212 ifpga_pci_decompose_tag(pcv, tag, &bus, &device, &function);
213
214 /* Reset the appertures so that we can talk to the register space. */
215 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE0,
216 IFPGA_PCI_APP0_512MB_BASE);
217 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE1,
218 IFPGA_PCI_APP1_CONF_BASE);
219
220 if (bus == 0) {
221 address = (1 << (device + 11)) | reg;
222 bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
223 IFPGA_PCI_APP1_CONF_T0_MAP | ((address >> 16) & 0xff00));
224
225 /* Read the value from the bus... */
226 data = bus_space_read_4(sc->sc_iot, sc->sc_conf_ioh,
227 address & 0x00ffffff);
228
229 } else {
230 bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
231 IFPGA_PCI_APP1_CONF_T1_MAP);
232
233 /* Read the value from the bus... */
234 data = bus_space_read_4(sc->sc_iot, sc->sc_conf_ioh,
235 tag | reg);
236 }
237 /* ... and put the memory spaces back again. */
238
239 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE1,
240 IFPGA_PCI_APP1_256MB_BASE);
241 bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
242 IFPGA_PCI_APP1_256MB_MAP);
243 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE0,
244 IFPGA_PCI_APP0_256MB_BASE);
245 #ifdef PCI_DEBUG
246 printf("ifpga_pci_conf_read(pcv=%p tag=0x%08lx reg=0x%02x)=0x%08x\n",
247 pcv, tag, reg, data);
248 #endif
249 return data;
250 }
251
252 void
253 ifpga_pci_conf_write(void *pcv, pcitag_t tag, int reg, pcireg_t data)
254 {
255 struct ifpga_pci_softc *sc = (struct ifpga_pci_softc *)pcv;
256 int bus, device, function;
257 u_int address;
258
259 #ifdef PCI_DEBUG
260 printf("ifpga_pci_conf_write(pcv=%p tag=0x%08lx reg=0x%02x, 0x%08x)\n",
261 pcv, tag, reg, data);
262 #endif
263
264 ifpga_pci_decompose_tag(pcv, tag, &bus, &device, &function);
265
266 /* Reset the appertures so that we can talk to the register space. */
267 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE0,
268 IFPGA_PCI_APP0_512MB_BASE);
269 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE1,
270 IFPGA_PCI_APP1_CONF_BASE);
271
272 if (bus == 0) {
273 address = (1 << (device + 11)) | reg;
274 bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
275 IFPGA_PCI_APP1_CONF_T0_MAP | ((address >> 16) & 0xff00));
276
277 /* Read the value from the bus... */
278 bus_space_write_4(sc->sc_iot, sc->sc_conf_ioh,
279 address & 0x00ffffff, data);
280
281 } else {
282 bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
283 IFPGA_PCI_APP1_CONF_T1_MAP);
284
285 /* Read the value from the bus... */
286 bus_space_write_4(sc->sc_iot, sc->sc_conf_ioh, tag | reg,
287 data);
288 }
289 /* ... and put the memory spaces back again. */
290
291 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE1,
292 IFPGA_PCI_APP1_256MB_BASE);
293 bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
294 IFPGA_PCI_APP1_256MB_MAP);
295 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE0,
296 IFPGA_PCI_APP0_256MB_BASE);
297 }
298
299 int
300 ifpga_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
301 {
302 int line = pa->pa_intrline;
303
304 #ifdef PCI_DEBUG
305 int pin = pa->pa_intrpin;
306 void *pcv = pa->pa_pc;
307 pcitag_t intrtag = pa->pa_intrtag;
308 int bus, device, function;
309
310 ifpga_pci_decompose_tag(pcv, intrtag, &bus, &device, &function);
311 printf("ifpga_pci_intr_map: pcv=%p, tag=%08lx pin=%d line=%d "
312 "dev=%d\n", pcv, intrtag, pin, line, device);
313 #endif
314
315
316 #ifdef PCI_DEBUG
317 printf("pin %d, line %d mapped to int %d\n", pin, line, line);
318 #endif
319
320 *ihp = line;
321 return 0;
322 }
323
324 const char *
325 ifpga_pci_intr_string(void *pcv, pci_intr_handle_t ih)
326 {
327 static char irqstr[12]; /* 6 + 1 + NULL + sanity */
328
329 #ifdef PCI_DEBUG
330 printf("ifpga_pci_intr_string(pcv=0x%p, ih=0x%lx)\n", pcv, ih);
331 #endif
332 if (ih == 0)
333 panic("ifpga_pci_intr_string: bogus handle 0x%lx", ih);
334
335 sprintf(irqstr, "pciint%ld", ih - IFPGA_INTRNUM_PCIINT0);
336 return irqstr;
337 }
338
339 const struct evcnt *
340 ifpga_pci_intr_evcnt(void *pcv, pci_intr_handle_t ih)
341 {
342
343 /* XXX for now, no evcnt parent reported */
344 return NULL;
345 }
346
347 void *
348 ifpga_pci_intr_establish(void *pcv, pci_intr_handle_t ih, int level,
349 int (*func) (void *), void *arg)
350 {
351 void *intr;
352 int length;
353
354 #ifdef PCI_DEBUG
355 printf("ifpga_pci_intr_establish(pcv=%p, ih=0x%lx, level=%d, "
356 "func=%p, arg=%p)\n", pcv, ih, level, func, arg);
357 #endif
358
359 /* Copy the interrupt string to a private buffer */
360 length = strlen(ifpga_pci_intr_string(pcv, ih));
361 intr = ifpga_intr_establish(ih, level, func, arg);
362
363 return intr;
364 }
365
366 void
367 ifpga_pci_intr_disestablish(void *pcv, void *cookie)
368 {
369 #ifdef PCI_DEBUG
370 printf("ifpga_pci_intr_disestablish(pcv=%p, cookie=%p)\n",
371 pcv, cookie);
372 #endif
373 /* XXXX Need to free the string */
374
375 ifpga_intr_disestablish(cookie);
376 }
377