footbridge_pci.c revision 1.34 1 1.34 skrll /* $NetBSD: footbridge_pci.c,v 1.34 2022/09/27 06:36:41 skrll Exp $ */
2 1.1 chris
3 1.1 chris /*
4 1.1 chris * Copyright (c) 1997,1998 Mark Brinicombe.
5 1.1 chris * Copyright (c) 1997,1998 Causality Limited
6 1.1 chris * All rights reserved.
7 1.1 chris *
8 1.1 chris * Redistribution and use in source and binary forms, with or without
9 1.1 chris * modification, are permitted provided that the following conditions
10 1.1 chris * are met:
11 1.1 chris * 1. Redistributions of source code must retain the above copyright
12 1.1 chris * notice, this list of conditions and the following disclaimer.
13 1.1 chris * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 chris * notice, this list of conditions and the following disclaimer in the
15 1.1 chris * documentation and/or other materials provided with the distribution.
16 1.1 chris * 3. All advertising materials mentioning features or use of this software
17 1.1 chris * must display the following acknowledgement:
18 1.1 chris * This product includes software developed by Mark Brinicombe
19 1.1 chris * for the NetBSD Project.
20 1.1 chris * 4. The name of the company nor the name of the author may be used to
21 1.1 chris * endorse or promote products derived from this software without specific
22 1.1 chris * prior written permission.
23 1.1 chris *
24 1.1 chris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 1.1 chris * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 1.1 chris * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 1.1 chris * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 1.1 chris * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 1.1 chris * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 1.1 chris * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 chris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 chris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 chris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 chris * SUCH DAMAGE.
35 1.1 chris */
36 1.9 chris
37 1.9 chris #include <sys/cdefs.h>
38 1.34 skrll __KERNEL_RCSID(0, "$NetBSD: footbridge_pci.c,v 1.34 2022/09/27 06:36:41 skrll Exp $");
39 1.1 chris
40 1.1 chris #include <sys/param.h>
41 1.1 chris #include <sys/systm.h>
42 1.1 chris #include <sys/conf.h>
43 1.1 chris #include <sys/device.h>
44 1.1 chris
45 1.1 chris #define _ARM32_BUS_DMA_PRIVATE
46 1.21 dyoung #include <sys/bus.h>
47 1.4 matt #include <machine/intr.h>
48 1.1 chris
49 1.1 chris #include <dev/pci/pcireg.h>
50 1.1 chris #include <dev/pci/pcivar.h>
51 1.1 chris
52 1.1 chris #include <arm/footbridge/dc21285reg.h>
53 1.1 chris #include <arm/footbridge/dc21285mem.h>
54 1.1 chris
55 1.1 chris #include "isa.h"
56 1.1 chris #if NISA > 0
57 1.1 chris #include <dev/isa/isavar.h>
58 1.1 chris #endif
59 1.1 chris
60 1.24 chs void footbridge_pci_attach_hook(device_t, device_t,
61 1.24 chs struct pcibus_attach_args *);
62 1.16 dsl int footbridge_pci_bus_maxdevs(void *, int);
63 1.16 dsl pcitag_t footbridge_pci_make_tag(void *, int, int, int);
64 1.16 dsl void footbridge_pci_decompose_tag(void *, pcitag_t, int *,
65 1.16 dsl int *, int *);
66 1.16 dsl pcireg_t footbridge_pci_conf_read(void *, pcitag_t, int);
67 1.16 dsl void footbridge_pci_conf_write(void *, pcitag_t, int,
68 1.16 dsl pcireg_t);
69 1.19 dyoung int footbridge_pci_intr_map(const struct pci_attach_args *,
70 1.16 dsl pci_intr_handle_t *);
71 1.27 christos const char *footbridge_pci_intr_string(void *, pci_intr_handle_t,
72 1.27 christos char *, size_t);
73 1.16 dsl void *footbridge_pci_intr_establish(void *, pci_intr_handle_t,
74 1.31 rjs int, int (*)(void *), void *, const char *);
75 1.16 dsl void footbridge_pci_intr_disestablish(void *, void *);
76 1.16 dsl const struct evcnt *footbridge_pci_intr_evcnt(void *, pci_intr_handle_t);
77 1.1 chris
78 1.1 chris struct arm32_pci_chipset footbridge_pci_chipset = {
79 1.2 matt #ifdef netwinder
80 1.29 skrll .pc_attach_hook = netwinder_pci_attach_hook,
81 1.1 chris #else
82 1.29 skrll .pc_attach_hook = footbridge_pci_attach_hook,
83 1.1 chris #endif
84 1.29 skrll .pc_bus_maxdevs = footbridge_pci_bus_maxdevs,
85 1.29 skrll .pc_make_tag = footbridge_pci_make_tag,
86 1.29 skrll .pc_decompose_tag = footbridge_pci_decompose_tag,
87 1.29 skrll .pc_conf_read = footbridge_pci_conf_read,
88 1.29 skrll .pc_conf_write = footbridge_pci_conf_write,
89 1.29 skrll .pc_intr_map = footbridge_pci_intr_map,
90 1.29 skrll .pc_intr_string = footbridge_pci_intr_string,
91 1.29 skrll .pc_intr_evcnt = footbridge_pci_intr_evcnt,
92 1.29 skrll .pc_intr_establish = footbridge_pci_intr_establish,
93 1.29 skrll .pc_intr_disestablish = footbridge_pci_intr_disestablish
94 1.1 chris };
95 1.1 chris
96 1.14 chris struct arm32_dma_range footbridge_dma_ranges[1];
97 1.14 chris
98 1.1 chris /*
99 1.1 chris * PCI doesn't have any special needs; just use the generic versions
100 1.1 chris * of these functions.
101 1.1 chris */
102 1.1 chris struct arm32_bus_dma_tag footbridge_pci_bus_dma_tag = {
103 1.23 matt ._ranges = footbridge_dma_ranges,
104 1.23 matt ._nranges = 1,
105 1.23 matt _BUS_DMAMAP_FUNCS,
106 1.23 matt _BUS_DMAMEM_FUNCS,
107 1.23 matt _BUS_DMATAG_FUNCS,
108 1.1 chris };
109 1.1 chris
110 1.1 chris /*
111 1.1 chris * Currently we only support 12 devices as we select directly in the
112 1.1 chris * type 0 config cycle
113 1.1 chris * (See conf_{read,write} for more detail
114 1.1 chris */
115 1.1 chris #define MAX_PCI_DEVICES 21
116 1.1 chris
117 1.1 chris /*static int
118 1.1 chris pci_intr(void *arg)
119 1.1 chris {
120 1.1 chris printf("pci int %x\n", (int)arg);
121 1.1 chris return(0);
122 1.1 chris }*/
123 1.1 chris
124 1.1 chris
125 1.1 chris void
126 1.24 chs footbridge_pci_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba)
127 1.1 chris {
128 1.1 chris #ifdef PCI_DEBUG
129 1.1 chris printf("footbridge_pci_attach_hook()\n");
130 1.1 chris #endif
131 1.1 chris
132 1.1 chris /* intr_claim(18, IPL_NONE, "pci int 0", pci_intr, (void *)0x10000);
133 1.1 chris intr_claim(8, IPL_NONE, "pci int 1", pci_intr, (void *)0x10001);
134 1.1 chris intr_claim(9, IPL_NONE, "pci int 2", pci_intr, (void *)0x10002);
135 1.1 chris intr_claim(11, IPL_NONE, "pci int 3", pci_intr, (void *)0x10003);*/
136 1.1 chris }
137 1.1 chris
138 1.1 chris int
139 1.17 dsl footbridge_pci_bus_maxdevs(void *pcv, int busno)
140 1.1 chris {
141 1.1 chris #ifdef PCI_DEBUG
142 1.1 chris printf("footbridge_pci_bus_maxdevs(pcv=%p, busno=%d)\n", pcv, busno);
143 1.1 chris #endif
144 1.1 chris return(MAX_PCI_DEVICES);
145 1.1 chris }
146 1.1 chris
147 1.1 chris pcitag_t
148 1.18 dsl footbridge_pci_make_tag(void *pcv, int bus, int device, int function)
149 1.1 chris {
150 1.1 chris #ifdef PCI_DEBUG
151 1.1 chris printf("footbridge_pci_make_tag(pcv=%p, bus=%d, device=%d, function=%d)\n",
152 1.1 chris pcv, bus, device, function);
153 1.1 chris #endif
154 1.1 chris return ((bus << 16) | (device << 11) | (function << 8));
155 1.1 chris }
156 1.1 chris
157 1.1 chris void
158 1.18 dsl footbridge_pci_decompose_tag(void *pcv, pcitag_t tag, int *busp, int *devicep, int *functionp)
159 1.1 chris {
160 1.1 chris #ifdef PCI_DEBUG
161 1.13 chris printf("footbridge_pci_decompose_tag(pcv=%p, tag=0x%08x, bp=%p, dp=%p, fp=%p)\n",
162 1.13 chris pcv, (uint32_t)tag, busp, devicep, functionp);
163 1.1 chris #endif
164 1.1 chris
165 1.1 chris if (busp != NULL)
166 1.1 chris *busp = (tag >> 16) & 0xff;
167 1.1 chris if (devicep != NULL)
168 1.1 chris *devicep = (tag >> 11) & 0x1f;
169 1.1 chris if (functionp != NULL)
170 1.1 chris *functionp = (tag >> 8) & 0x7;
171 1.1 chris }
172 1.1 chris
173 1.1 chris pcireg_t
174 1.17 dsl footbridge_pci_conf_read(void *pcv, pcitag_t tag, int reg)
175 1.1 chris {
176 1.1 chris int bus, device, function;
177 1.1 chris u_int address;
178 1.1 chris pcireg_t data;
179 1.1 chris
180 1.28 msaitoh if ((unsigned int)reg >= PCI_CONF_SIZE)
181 1.28 msaitoh return ((pcireg_t) -1);
182 1.28 msaitoh
183 1.1 chris footbridge_pci_decompose_tag(pcv, tag, &bus, &device, &function);
184 1.1 chris if (bus == 0)
185 1.1 chris /* Limited to 12 devices or we exceed type 0 config space */
186 1.1 chris address = DC21285_PCI_TYPE_0_CONFIG_VBASE | (3 << 22) | (device << 11);
187 1.1 chris else
188 1.1 chris address = DC21285_PCI_TYPE_1_CONFIG_VBASE | (device << 11) |
189 1.1 chris (bus << 16);
190 1.1 chris
191 1.1 chris address |= (function << 8) | reg;
192 1.1 chris
193 1.1 chris data = *((unsigned int *)address);
194 1.1 chris #ifdef PCI_DEBUG
195 1.1 chris printf("footbridge_pci_conf_read(pcv=%p tag=0x%08x reg=0x%02x)=0x%08x\n",
196 1.13 chris pcv, (uint32_t)tag, reg, data);
197 1.1 chris #endif
198 1.1 chris return(data);
199 1.1 chris }
200 1.1 chris
201 1.1 chris void
202 1.17 dsl footbridge_pci_conf_write(void *pcv, pcitag_t tag, int reg, pcireg_t data)
203 1.1 chris {
204 1.1 chris int bus, device, function;
205 1.1 chris u_int address;
206 1.1 chris
207 1.28 msaitoh if ((unsigned int)reg >= PCI_CONF_SIZE)
208 1.28 msaitoh return;
209 1.28 msaitoh
210 1.1 chris footbridge_pci_decompose_tag(pcv, tag, &bus, &device, &function);
211 1.1 chris if (bus == 0)
212 1.1 chris address = DC21285_PCI_TYPE_0_CONFIG_VBASE | (3 << 22) | (device << 11);
213 1.1 chris else
214 1.1 chris address = DC21285_PCI_TYPE_1_CONFIG_VBASE | (device << 11) |
215 1.1 chris (bus << 16);
216 1.1 chris
217 1.1 chris address |= (function << 8) | reg;
218 1.1 chris
219 1.1 chris #ifdef PCI_DEBUG
220 1.1 chris printf("footbridge_pci_conf_write(pcv=%p tag=0x%08x reg=0x%02x, 0x%08x)\n",
221 1.13 chris pcv, (uint32_t)tag, reg, data);
222 1.1 chris #endif
223 1.1 chris
224 1.1 chris *((unsigned int *)address) = data;
225 1.1 chris }
226 1.1 chris
227 1.1 chris int
228 1.19 dyoung footbridge_pci_intr_map(const struct pci_attach_args *pa,
229 1.19 dyoung pci_intr_handle_t *ihp)
230 1.1 chris {
231 1.1 chris int pin = pa->pa_intrpin, line = pa->pa_intrline;
232 1.1 chris int intr = -1;
233 1.1 chris
234 1.1 chris #ifdef PCI_DEBUG
235 1.1 chris void *pcv = pa->pa_pc;
236 1.1 chris pcitag_t intrtag = pa->pa_intrtag;
237 1.1 chris int bus, device, function;
238 1.1 chris
239 1.1 chris footbridge_pci_decompose_tag(pcv, intrtag, &bus, &device, &function);
240 1.13 chris printf("footbridge_pci_intr_map: pcv=%p, tag=%08x pin=%d line=%d dev=%d\n",
241 1.13 chris pcv, (uint32_t)intrtag, pin, line, device);
242 1.1 chris #endif
243 1.1 chris
244 1.1 chris /*
245 1.1 chris * Only the line is used to map the interrupt.
246 1.1 chris * The firmware is expected to setup up the interrupt
247 1.1 chris * line as seen from the CPU
248 1.1 chris * This means the firmware deals with the interrupt rotation
249 1.1 chris * between slots etc.
250 1.1 chris *
251 1.1 chris * Perhaps the firmware should also to the final mapping
252 1.1 chris * to a 21285 interrupt bit so the code below would be
253 1.1 chris * completely MI.
254 1.1 chris */
255 1.1 chris
256 1.1 chris switch (line) {
257 1.1 chris case PCI_INTERRUPT_PIN_NONE:
258 1.1 chris case 0xff:
259 1.1 chris /* No IRQ */
260 1.1 chris printf("pci_intr_map: no mapping for pin %c\n", '@' + pin);
261 1.1 chris *ihp = -1;
262 1.1 chris return(1);
263 1.1 chris break;
264 1.3 chris #ifdef cats
265 1.20 wiz /* This is machine dependent and needs to be moved */
266 1.1 chris case PCI_INTERRUPT_PIN_A:
267 1.1 chris intr = IRQ_PCI;
268 1.1 chris break;
269 1.1 chris case PCI_INTERRUPT_PIN_B:
270 1.1 chris intr = IRQ_IN_L0;
271 1.1 chris break;
272 1.1 chris case PCI_INTERRUPT_PIN_C:
273 1.1 chris intr = IRQ_IN_L1;
274 1.1 chris break;
275 1.1 chris case PCI_INTERRUPT_PIN_D:
276 1.1 chris intr = IRQ_IN_L3;
277 1.1 chris break;
278 1.1 chris #endif
279 1.1 chris default:
280 1.1 chris /*
281 1.1 chris * Experimental firmware feature ...
282 1.1 chris *
283 1.1 chris * If the interrupt line is in the range 0x80 to 0x8F
284 1.1 chris * then the lower 4 bits indicate the ISA interrupt
285 1.1 chris * bit that should be used.
286 1.1 chris * If the interrupt line is in the range 0x40 to 0x5F
287 1.1 chris * then the lower 5 bits indicate the actual DC21285
288 1.1 chris * interrupt bit that should be used.
289 1.1 chris */
290 1.1 chris
291 1.1 chris if (line >= 0x40 && line <= 0x5f)
292 1.1 chris intr = line & 0x1f;
293 1.1 chris else if (line >= 0x80 && line <= 0x8f)
294 1.1 chris intr = line;
295 1.1 chris else {
296 1.1 chris printf("footbridge_pci_intr_map: out of range interrupt"
297 1.1 chris "pin %d line %d (%#x)\n", pin, line, line);
298 1.1 chris *ihp = -1;
299 1.1 chris return(1);
300 1.1 chris }
301 1.1 chris break;
302 1.1 chris }
303 1.1 chris
304 1.1 chris #ifdef PCI_DEBUG
305 1.1 chris printf("pin %d, line %d mapped to int %d\n", pin, line, intr);
306 1.1 chris #endif
307 1.1 chris
308 1.1 chris *ihp = intr;
309 1.1 chris return(0);
310 1.1 chris }
311 1.1 chris
312 1.1 chris const char *
313 1.26 christos footbridge_pci_intr_string(void *pcv, pci_intr_handle_t ih, char *buf, size_t len)
314 1.1 chris {
315 1.1 chris #ifdef PCI_DEBUG
316 1.30 jmcneill printf("footbridge_pci_intr_string(pcv=%p, ih=0x%" PRIx64 ")\n", pcv, ih);
317 1.1 chris #endif
318 1.1 chris if (ih == 0)
319 1.30 jmcneill panic("footbridge_pci_intr_string: bogus handle 0x%" PRIx64, ih);
320 1.1 chris
321 1.1 chris #if NISA > 0
322 1.1 chris if (ih >= 0x80 && ih <= 0x8f) {
323 1.30 jmcneill snprintf(buf, len, "isairq %" PRIu64, (ih & 0x0f));
324 1.26 christos return buf;
325 1.1 chris }
326 1.1 chris #endif
327 1.30 jmcneill snprintf(buf, len, "irq %" PRIu64, ih);
328 1.33 skrll return buf;
329 1.1 chris }
330 1.1 chris
331 1.1 chris void *
332 1.22 matt footbridge_pci_intr_establish(
333 1.22 matt void *pcv,
334 1.22 matt pci_intr_handle_t ih,
335 1.22 matt int level,
336 1.22 matt int (*func)(void *),
337 1.31 rjs void *arg, const char *xname)
338 1.1 chris {
339 1.1 chris void *intr;
340 1.26 christos char buf[PCI_INTRSTR_LEN];
341 1.26 christos const char *intrstr;
342 1.1 chris
343 1.1 chris #ifdef PCI_DEBUG
344 1.31 rjs printf("footbridge_pci_intr_establish(pcv=%p, ih=0x%" PRIx64 ", level=%d, func=%p, arg=%p, xname=%s)\n",
345 1.31 rjs pcv, ih, level, func, arg, xname);
346 1.1 chris #endif
347 1.1 chris
348 1.1 chris /* Copy the interrupt string to a private buffer */
349 1.26 christos intrstr = footbridge_pci_intr_string(pcv, ih, buf, sizeof(buf));
350 1.1 chris #if NISA > 0
351 1.1 chris /*
352 1.1 chris * XXX the IDE driver will attach the interrupts in compat mode and
353 1.1 chris * thus we need to fail this here.
354 1.1 chris * This assumes that the interrupts are 14 and 15 which they are for
355 1.1 chris * IDE compat mode.
356 1.1 chris * Really the firmware should make this clear in the interrupt reg.
357 1.1 chris */
358 1.1 chris if (ih >= 0x80 && ih <= 0x8d) {
359 1.1 chris intr = isa_intr_establish(NULL, (ih & 0x0f), IST_EDGE,
360 1.1 chris level, func, arg);
361 1.1 chris } else
362 1.1 chris #endif
363 1.26 christos intr = footbridge_intr_claim(ih, level, intrstr, func, arg);
364 1.1 chris
365 1.1 chris return(intr);
366 1.1 chris }
367 1.1 chris
368 1.1 chris void
369 1.17 dsl footbridge_pci_intr_disestablish(void *pcv, void *cookie)
370 1.1 chris {
371 1.1 chris #ifdef PCI_DEBUG
372 1.32 rin printf("footbridge_pci_intr_disestablish(pcv=%p, cookie=%p)\n",
373 1.1 chris pcv, cookie);
374 1.1 chris #endif
375 1.1 chris /* XXXX Need to free the string */
376 1.8 chris footbridge_intr_disestablish(cookie);
377 1.1 chris }
378