ifpga_pci.c revision 1.6 1 /* $NetBSD: ifpga_pci.c,v 1.6 2003/07/15 00:24:59 lukem 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 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: ifpga_pci.c,v 1.6 2003/07/15 00:24:59 lukem Exp $");
66
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/conf.h>
70 #include <sys/malloc.h>
71 #include <sys/device.h>
72
73 #define _ARM32_BUS_DMA_PRIVATE
74 #include <evbarm/integrator/int_bus_dma.h>
75
76 #include <machine/intr.h>
77 #include <evbarm/ifpga/irqhandler.h> /* XXX XXX XXX */
78
79 #include <dev/pci/pcireg.h>
80 #include <dev/pci/pcivar.h>
81
82 #include <evbarm/ifpga/ifpgareg.h>
83 #include <evbarm/ifpga/ifpgamem.h>
84 #include <evbarm/ifpga/ifpga_pcivar.h>
85 #include <evbarm/dev/v360reg.h>
86
87
88 void ifpga_pci_attach_hook (struct device *, struct device *,
89 struct pcibus_attach_args *);
90 int ifpga_pci_bus_maxdevs (void *, int);
91 pcitag_t ifpga_pci_make_tag (void *, int, int, int);
92 void ifpga_pci_decompose_tag (void *, pcitag_t, int *, int *,
93 int *);
94 pcireg_t ifpga_pci_conf_read (void *, pcitag_t, int);
95 void ifpga_pci_conf_write (void *, pcitag_t, int, pcireg_t);
96 int ifpga_pci_intr_map (struct pci_attach_args *,
97 pci_intr_handle_t *);
98 const char *ifpga_pci_intr_string (void *, pci_intr_handle_t);
99 const struct evcnt *ifpga_pci_intr_evcnt (void *, pci_intr_handle_t);
100 void *ifpga_pci_intr_establish (void *, pci_intr_handle_t, int,
101 int (*)(void *), void *);
102 void ifpga_pci_intr_disestablish (void *, void *);
103
104 struct arm32_pci_chipset ifpga_pci_chipset = {
105 NULL, /* conf_v */
106 ifpga_pci_attach_hook,
107 ifpga_pci_bus_maxdevs,
108 ifpga_pci_make_tag,
109 ifpga_pci_decompose_tag,
110 ifpga_pci_conf_read,
111 ifpga_pci_conf_write,
112 NULL, /* intr_v */
113 ifpga_pci_intr_map,
114 ifpga_pci_intr_string,
115 ifpga_pci_intr_evcnt,
116 ifpga_pci_intr_establish,
117 ifpga_pci_intr_disestablish
118 };
119
120 /*
121 * Use the integrator-specific bus_dma routines.
122 */
123 struct arm32_bus_dma_tag ifpga_pci_bus_dma_tag = {
124 0,
125 0,
126 _bus_dmamap_create,
127 _bus_dmamap_destroy,
128 _bus_dmamap_load,
129 _bus_dmamap_load_mbuf,
130 _bus_dmamap_load_uio,
131 _bus_dmamap_load_raw,
132 _bus_dmamap_unload,
133 _bus_dmamap_sync, /* pre */
134 NULL, /* post */
135 _bus_dmamem_alloc,
136 _bus_dmamem_free,
137 _bus_dmamem_map,
138 _bus_dmamem_unmap,
139 _bus_dmamem_mmap,
140 };
141
142 /*
143 * Currently we only support 12 devices as we select directly in the
144 * type 0 config cycle
145 * (See conf_{read,write} for more detail
146 */
147 #define MAX_PCI_DEVICES 21
148
149 /*static int
150 pci_intr(void *arg)
151 {
152 printf("pci int %x\n", (int)arg);
153 return 0;
154 }*/
155
156
157 void
158 ifpga_pci_attach_hook(struct device *parent, struct device *self,
159 struct pcibus_attach_args *pba)
160 {
161 #ifdef PCI_DEBUG
162 printf("ifpga_pci_attach_hook()\n");
163 #endif
164 }
165
166 int
167 ifpga_pci_bus_maxdevs(void *pcv, int busno)
168 {
169 #ifdef PCI_DEBUG
170 printf("ifpga_pci_bus_maxdevs(pcv=%p, busno=%d)\n", pcv, busno);
171 #endif
172 return MAX_PCI_DEVICES;
173 }
174
175 pcitag_t
176 ifpga_pci_make_tag(void *pcv, int bus, int device, int function)
177 {
178 #ifdef PCI_DEBUG
179 printf("ifpga_pci_make_tag(pcv=%p, bus=%d, device=%d, function=%d)\n",
180 pcv, bus, device, function);
181 #endif
182 return (bus << 16) | (device << 11) | (function << 8);
183 }
184
185 void
186 ifpga_pci_decompose_tag(void *pcv, pcitag_t tag, int *busp, int *devicep,
187 int *functionp)
188 {
189 #ifdef PCI_DEBUG
190 printf("ifpga_pci_decompose_tag(pcv=%p, tag=0x%08lx, bp=%p, dp=%p, "
191 "fp=%p)\n", pcv, tag, busp, devicep, functionp);
192 #endif
193
194 if (busp != NULL)
195 *busp = (tag >> 16) & 0xff;
196 if (devicep != NULL)
197 *devicep = (tag >> 11) & 0x1f;
198 if (functionp != NULL)
199 *functionp = (tag >> 8) & 0x7;
200 }
201
202 pcireg_t
203 ifpga_pci_conf_read(void *pcv, pcitag_t tag, int reg)
204 {
205 pcireg_t data;
206 struct ifpga_pci_softc *sc = (struct ifpga_pci_softc *)pcv;
207 int bus, device, function;
208 u_int address;
209
210 ifpga_pci_decompose_tag(pcv, tag, &bus, &device, &function);
211
212 /* Reset the appertures so that we can talk to the register space. */
213 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE0,
214 IFPGA_PCI_APP0_512MB_BASE);
215 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE1,
216 IFPGA_PCI_APP1_CONF_BASE);
217
218 if (bus == 0) {
219 address = (1 << (device + 11)) | reg;
220 bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
221 IFPGA_PCI_APP1_CONF_T0_MAP | ((address >> 16) & 0xff00));
222
223 /* Read the value from the bus... */
224 data = bus_space_read_4(sc->sc_iot, sc->sc_conf_ioh,
225 address & 0x00ffffff);
226
227 } else {
228 bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
229 IFPGA_PCI_APP1_CONF_T1_MAP);
230
231 /* Read the value from the bus... */
232 data = bus_space_read_4(sc->sc_iot, sc->sc_conf_ioh,
233 tag | reg);
234 }
235 /* ... and put the memory spaces back again. */
236
237 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE1,
238 IFPGA_PCI_APP1_256MB_BASE);
239 bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
240 IFPGA_PCI_APP1_256MB_MAP);
241 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE0,
242 IFPGA_PCI_APP0_256MB_BASE);
243 #ifdef PCI_DEBUG
244 printf("ifpga_pci_conf_read(pcv=%p tag=0x%08lx reg=0x%02x)=0x%08x\n",
245 pcv, tag, reg, data);
246 #endif
247 return data;
248 }
249
250 void
251 ifpga_pci_conf_write(void *pcv, pcitag_t tag, int reg, pcireg_t data)
252 {
253 struct ifpga_pci_softc *sc = (struct ifpga_pci_softc *)pcv;
254 int bus, device, function;
255 u_int address;
256
257 #ifdef PCI_DEBUG
258 printf("ifpga_pci_conf_write(pcv=%p tag=0x%08lx reg=0x%02x, 0x%08x)\n",
259 pcv, tag, reg, data);
260 #endif
261
262 ifpga_pci_decompose_tag(pcv, tag, &bus, &device, &function);
263
264 /* Reset the appertures so that we can talk to the register space. */
265 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE0,
266 IFPGA_PCI_APP0_512MB_BASE);
267 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE1,
268 IFPGA_PCI_APP1_CONF_BASE);
269
270 if (bus == 0) {
271 address = (1 << (device + 11)) | reg;
272 bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
273 IFPGA_PCI_APP1_CONF_T0_MAP | ((address >> 16) & 0xff00));
274
275 /* Read the value from the bus... */
276 bus_space_write_4(sc->sc_iot, sc->sc_conf_ioh,
277 address & 0x00ffffff, data);
278
279 } else {
280 bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
281 IFPGA_PCI_APP1_CONF_T1_MAP);
282
283 /* Read the value from the bus... */
284 bus_space_write_4(sc->sc_iot, sc->sc_conf_ioh, tag | reg,
285 data);
286 }
287 /* ... and put the memory spaces back again. */
288
289 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE1,
290 IFPGA_PCI_APP1_256MB_BASE);
291 bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
292 IFPGA_PCI_APP1_256MB_MAP);
293 bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE0,
294 IFPGA_PCI_APP0_256MB_BASE);
295 }
296
297 int
298 ifpga_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
299 {
300 int line = pa->pa_intrline;
301
302 #ifdef PCI_DEBUG
303 int pin = pa->pa_intrpin;
304 void *pcv = pa->pa_pc;
305 pcitag_t intrtag = pa->pa_intrtag;
306 int bus, device, function;
307
308 ifpga_pci_decompose_tag(pcv, intrtag, &bus, &device, &function);
309 printf("ifpga_pci_intr_map: pcv=%p, tag=%08lx pin=%d line=%d "
310 "dev=%d\n", pcv, intrtag, pin, line, device);
311 #endif
312
313
314 #ifdef PCI_DEBUG
315 printf("pin %d, line %d mapped to int %d\n", pin, line, line);
316 #endif
317
318 *ihp = line;
319 return 0;
320 }
321
322 const char *
323 ifpga_pci_intr_string(void *pcv, pci_intr_handle_t ih)
324 {
325 static char irqstr[12]; /* 6 + 1 + NULL + sanity */
326
327 #ifdef PCI_DEBUG
328 printf("ifpga_pci_intr_string(pcv=0x%p, ih=0x%lx)\n", pcv, ih);
329 #endif
330 if (ih == 0)
331 panic("ifpga_pci_intr_string: bogus handle 0x%lx", ih);
332
333 sprintf(irqstr, "pciint%ld", ih - IFPGA_INTRNUM_PCIINT0);
334 return irqstr;
335 }
336
337 const struct evcnt *
338 ifpga_pci_intr_evcnt(void *pcv, pci_intr_handle_t ih)
339 {
340
341 /* XXX for now, no evcnt parent reported */
342 return NULL;
343 }
344
345 void *
346 ifpga_pci_intr_establish(void *pcv, pci_intr_handle_t ih, int level,
347 int (*func) (void *), void *arg)
348 {
349 void *intr;
350 int length;
351 char *string;
352
353 #ifdef PCI_DEBUG
354 printf("ifpga_pci_intr_establish(pcv=%p, ih=0x%lx, level=%d, "
355 "func=%p, arg=%p)\n", pcv, ih, level, func, arg);
356 #endif
357
358 /* Copy the interrupt string to a private buffer */
359 length = strlen(ifpga_pci_intr_string(pcv, ih));
360 string = malloc(length + 1, M_DEVBUF, M_WAITOK);
361 strcpy(string, ifpga_pci_intr_string(pcv, ih));
362 intr = intr_claim(ih, level, string, func, arg);
363
364 return intr;
365 }
366
367 void
368 ifpga_pci_intr_disestablish(void *pcv, void *cookie)
369 {
370 #ifdef PCI_DEBUG
371 printf("ifpga_pci_intr_disestablish(pcv=%p, cookie=%p)\n",
372 pcv, cookie);
373 #endif
374 /* XXXX Need to free the string */
375
376 intr_release(cookie);
377 }
378