pci_machdep.c revision 1.13 1 1.13 mycroft /* $NetBSD: pci_machdep.c,v 1.13 1998/08/15 03:02:35 mycroft Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1996 Leo Weppelman. All rights reserved.
5 1.7 cgd * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
6 1.13 mycroft * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
7 1.1 leo *
8 1.1 leo * Redistribution and use in source and binary forms, with or without
9 1.1 leo * modification, are permitted provided that the following conditions
10 1.1 leo * are met:
11 1.1 leo * 1. Redistributions of source code must retain the above copyright
12 1.1 leo * notice, this list of conditions and the following disclaimer.
13 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 leo * notice, this list of conditions and the following disclaimer in the
15 1.1 leo * documentation and/or other materials provided with the distribution.
16 1.1 leo * 3. All advertising materials mentioning features or use of this software
17 1.1 leo * must display the following acknowledgement:
18 1.13 mycroft * This product includes software developed by Charles M. Hannum.
19 1.1 leo * 4. The name of the author may not be used to endorse or promote products
20 1.1 leo * derived from this software without specific prior written permission.
21 1.1 leo *
22 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 leo */
33 1.1 leo
34 1.1 leo #include <sys/types.h>
35 1.1 leo #include <sys/param.h>
36 1.1 leo #include <sys/time.h>
37 1.1 leo #include <sys/systm.h>
38 1.1 leo #include <sys/errno.h>
39 1.1 leo #include <sys/device.h>
40 1.1 leo
41 1.1 leo #include <vm/vm.h>
42 1.1 leo #include <vm/vm_kern.h>
43 1.1 leo
44 1.1 leo #include <dev/pci/pcivar.h>
45 1.1 leo #include <dev/pci/pcireg.h>
46 1.1 leo
47 1.1 leo #include <machine/cpu.h>
48 1.1 leo #include <machine/iomap.h>
49 1.9 leo #include <machine/mfp.h>
50 1.10 leo #include <machine/bus.h>
51 1.10 leo
52 1.1 leo #include <atari/atari/device.h>
53 1.1 leo
54 1.9 leo /*
55 1.9 leo * I/O and memory we assume 'reserved' when an vga card is detected on
56 1.9 leo * the PCI-bus.
57 1.9 leo */
58 1.9 leo #define MAX_VGA_MEM 0x1000000 /* 16 MB mem */
59 1.9 leo #define MAX_VGA_IO 0x0010000 /* 64 KB io */
60 1.9 leo
61 1.1 leo int pcibusprint __P((void *auxp, const char *));
62 1.5 leo int pcibusmatch __P((struct device *, struct cfdata *, void *));
63 1.1 leo void pcibusattach __P((struct device *, struct device *, void *));
64 1.1 leo
65 1.1 leo static int pci_config_offset __P((pcitag_t));
66 1.1 leo
67 1.1 leo struct cfattach pcibus_ca = {
68 1.1 leo sizeof(struct device), pcibusmatch, pcibusattach
69 1.1 leo };
70 1.1 leo
71 1.1 leo int
72 1.5 leo pcibusmatch(pdp, cfp, auxp)
73 1.1 leo struct device *pdp;
74 1.5 leo struct cfdata *cfp;
75 1.5 leo void *auxp;
76 1.1 leo {
77 1.1 leo if(atari_realconfig == 0)
78 1.1 leo return (0);
79 1.1 leo if (strcmp((char *)auxp, "pcibus") || cfp->cf_unit != 0)
80 1.1 leo return(0);
81 1.1 leo return(machineid & ATARI_HADES ? 1 : 0);
82 1.1 leo }
83 1.1 leo
84 1.1 leo void
85 1.1 leo pcibusattach(pdp, dp, auxp)
86 1.1 leo struct device *pdp, *dp;
87 1.1 leo void *auxp;
88 1.1 leo {
89 1.1 leo struct pcibus_attach_args pba;
90 1.11 leo bus_space_tag_t leb_alloc_bus_space_tag __P((void));
91 1.11 leo
92 1.1 leo
93 1.1 leo pba.pba_busname = "pci";
94 1.4 leo pba.pba_pc = NULL;
95 1.1 leo pba.pba_bus = 0;
96 1.7 cgd pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
97 1.10 leo pba.pba_dmat = BUS_PCI_DMA_TAG;
98 1.11 leo pba.pba_iot = leb_alloc_bus_space_tag();
99 1.11 leo pba.pba_memt = leb_alloc_bus_space_tag();
100 1.11 leo if ((pba.pba_iot == NULL) || (pba.pba_memt == NULL)) {
101 1.11 leo printf("leb_alloc_bus_space_tag failed!\n");
102 1.11 leo return;
103 1.11 leo }
104 1.11 leo pba.pba_iot->base = PCI_IO_PHYS;
105 1.11 leo pba.pba_memt->base = PCI_MEM_PHYS;
106 1.6 leo
107 1.9 leo MFP2->mf_aer &= ~(0x27); /* PCI interrupts: HIGH -> LOW */
108 1.9 leo
109 1.6 leo printf("\n");
110 1.1 leo
111 1.1 leo config_found(dp, &pba, pcibusprint);
112 1.1 leo }
113 1.1 leo
114 1.1 leo int
115 1.1 leo pcibusprint(auxp, name)
116 1.1 leo void *auxp;
117 1.1 leo const char *name;
118 1.1 leo {
119 1.1 leo if(name == NULL)
120 1.1 leo return(UNCONF);
121 1.1 leo return(QUIET);
122 1.1 leo }
123 1.1 leo
124 1.1 leo void
125 1.1 leo pci_attach_hook(parent, self, pba)
126 1.1 leo struct device *parent, *self;
127 1.1 leo struct pcibus_attach_args *pba;
128 1.1 leo {
129 1.1 leo }
130 1.1 leo
131 1.1 leo /*
132 1.9 leo * Initialize the PCI-bus. The Atari-BIOS does not do this, so....
133 1.9 leo */
134 1.9 leo void
135 1.9 leo init_pci_bus()
136 1.9 leo {
137 1.9 leo pci_chipset_tag_t pc = NULL; /* XXX */
138 1.9 leo pcitag_t tag;
139 1.9 leo pcireg_t csr, address, mask;
140 1.9 leo int device, id, class, maxndevs;
141 1.9 leo int reg;
142 1.9 leo u_int32_t membase, iobase;
143 1.9 leo
144 1.9 leo tag = 0;
145 1.9 leo id = class = 0;
146 1.9 leo
147 1.9 leo membase = iobase = 0;
148 1.9 leo
149 1.9 leo maxndevs = pci_bus_maxdevs(pc, 0);
150 1.9 leo
151 1.9 leo /*
152 1.9 leo * Scan the bus for prehistory (usually VGA) devices.
153 1.9 leo */
154 1.9 leo for (device = 0; device < maxndevs; device++) {
155 1.9 leo
156 1.9 leo tag = pci_make_tag(pc, 0, device, 0);
157 1.9 leo id = pci_conf_read(pc, tag, PCI_ID_REG);
158 1.9 leo if (id == 0 || id == 0xffffffff)
159 1.9 leo continue;
160 1.9 leo class = pci_conf_read(pc, tag, PCI_CLASS_REG);
161 1.9 leo switch (PCI_CLASS(class)) {
162 1.9 leo case PCI_CLASS_PREHISTORIC:
163 1.9 leo case PCI_CLASS_DISPLAY:
164 1.9 leo
165 1.9 leo membase = MAX_VGA_MEM;
166 1.9 leo iobase = MAX_VGA_IO;
167 1.9 leo }
168 1.9 leo }
169 1.9 leo
170 1.9 leo for (device = 0; device < maxndevs; device++) {
171 1.9 leo
172 1.9 leo tag = pci_make_tag(pc, 0, device, 0);
173 1.9 leo id = pci_conf_read(pc, tag, PCI_ID_REG);
174 1.9 leo if (id == 0 || id == 0xffffffff)
175 1.9 leo continue;
176 1.9 leo
177 1.9 leo class = pci_conf_read(pc, tag, PCI_CLASS_REG);
178 1.9 leo switch (PCI_CLASS(class)) {
179 1.9 leo case PCI_CLASS_PREHISTORIC:
180 1.9 leo case PCI_CLASS_DISPLAY:
181 1.9 leo /*
182 1.9 leo * XXX: We rely on the BIOS to do the
183 1.9 leo * right thing here. Eventually, we should
184 1.9 leo * take the initiative...
185 1.9 leo */
186 1.9 leo continue;
187 1.9 leo }
188 1.9 leo
189 1.9 leo csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
190 1.9 leo csr &= ~(PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_IO_ENABLE);
191 1.9 leo csr &= ~PCI_COMMAND_MASTER_ENABLE;
192 1.9 leo pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
193 1.9 leo
194 1.9 leo for (reg = PCI_MAPREG_START; reg < PCI_MAPREG_END; reg += 4) {
195 1.9 leo int size, type;
196 1.9 leo
197 1.9 leo address = pci_conf_read(pc, tag, reg);
198 1.9 leo pci_conf_write(pc, tag, reg, 0xffffffff);
199 1.9 leo mask = pci_conf_read(pc, tag, reg);
200 1.9 leo pci_conf_write(pc, tag, reg, address);
201 1.9 leo if (mask == 0)
202 1.9 leo continue; /* Register unused */
203 1.9 leo
204 1.9 leo if (mask & PCI_MAPREG_TYPE_IO) {
205 1.9 leo csr |= PCI_COMMAND_IO_ENABLE;
206 1.9 leo address = PCI_MAPREG_IO_ADDR(mask);
207 1.9 leo mask = (~address << 1) | 1;
208 1.9 leo size = (mask & address) & 0xffffffff;
209 1.9 leo address = iobase | PCI_MAPREG_TYPE_IO;
210 1.9 leo iobase += roundup(size, 4096); /* XXX */
211 1.9 leo }
212 1.9 leo else {
213 1.9 leo type = PCI_MAPREG_MEM_TYPE(address);
214 1.9 leo switch (type) {
215 1.9 leo case PCI_MAPREG_MEM_TYPE_32BIT:
216 1.9 leo break;
217 1.9 leo case PCI_MAPREG_MEM_TYPE_64BIT:
218 1.9 leo reg++;
219 1.9 leo case PCI_MAPREG_MEM_TYPE_32BIT_1M:
220 1.9 leo /*
221 1.9 leo * XXX: We can do better here!
222 1.9 leo */
223 1.9 leo if (membase >= 0x100000)
224 1.9 leo continue;
225 1.9 leo }
226 1.9 leo csr |= PCI_COMMAND_MEM_ENABLE;
227 1.9 leo size = PCI_MAPREG_MEM_SIZE(mask);
228 1.9 leo address = membase | PCI_MAPREG_TYPE_MEM;
229 1.9 leo membase += roundup(size, 4096); /* XXX */
230 1.9 leo }
231 1.9 leo pci_conf_write(pc, tag, reg, address);
232 1.9 leo }
233 1.9 leo csr |= PCI_COMMAND_MASTER_ENABLE;
234 1.9 leo pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
235 1.9 leo
236 1.9 leo /*
237 1.9 leo * Both interrupt pin & line are set to the device (== slot)
238 1.9 leo * number. This makes sense on the atari because the
239 1.9 leo * individual slots are hard-wired to a specific MFP-pin.
240 1.9 leo */
241 1.9 leo csr = (device << PCI_INTERRUPT_PIN_SHIFT);
242 1.9 leo csr |= (device << PCI_INTERRUPT_LINE_SHIFT);
243 1.9 leo pci_conf_write(pc, tag, PCI_INTERRUPT_REG, csr);
244 1.9 leo }
245 1.9 leo }
246 1.9 leo
247 1.9 leo /*
248 1.1 leo * Atari_init.c maps the config areas NBPG bytes apart....
249 1.1 leo */
250 1.1 leo static int pci_config_offset(tag)
251 1.1 leo pcitag_t tag;
252 1.1 leo {
253 1.1 leo int device;
254 1.1 leo
255 1.1 leo device = (tag >> 11) & 0x1f;
256 1.1 leo return(device * NBPG);
257 1.1 leo }
258 1.1 leo
259 1.1 leo int
260 1.1 leo pci_bus_maxdevs(pc, busno)
261 1.1 leo pci_chipset_tag_t pc;
262 1.1 leo int busno;
263 1.1 leo {
264 1.1 leo return (4);
265 1.1 leo }
266 1.1 leo
267 1.1 leo pcitag_t
268 1.1 leo pci_make_tag(pc, bus, device, function)
269 1.1 leo pci_chipset_tag_t pc;
270 1.1 leo int bus, device, function;
271 1.1 leo {
272 1.1 leo return ((bus << 16) | (device << 11) | (function << 8));
273 1.1 leo }
274 1.1 leo
275 1.1 leo pcireg_t
276 1.1 leo pci_conf_read(pc, tag, reg)
277 1.1 leo pci_chipset_tag_t pc;
278 1.1 leo pcitag_t tag;
279 1.1 leo int reg;
280 1.1 leo {
281 1.1 leo u_long data;
282 1.1 leo
283 1.1 leo data = *(u_long *)(pci_conf_addr + pci_config_offset(tag) + reg);
284 1.9 leo return (bswap32(data));
285 1.1 leo }
286 1.1 leo
287 1.1 leo void
288 1.1 leo pci_conf_write(pc, tag, reg, data)
289 1.1 leo pci_chipset_tag_t pc;
290 1.1 leo pcitag_t tag;
291 1.1 leo int reg;
292 1.1 leo pcireg_t data;
293 1.1 leo {
294 1.9 leo *((u_long *)(pci_conf_addr + pci_config_offset(tag) + reg))
295 1.9 leo = bswap32(data);
296 1.1 leo }
297 1.1 leo
298 1.1 leo int
299 1.1 leo pci_intr_map(pc, intrtag, pin, line, ihp)
300 1.1 leo pci_chipset_tag_t pc;
301 1.1 leo pcitag_t intrtag;
302 1.1 leo int pin, line;
303 1.1 leo pci_intr_handle_t *ihp;
304 1.1 leo {
305 1.9 leo /*
306 1.9 leo * According to the PCI-spec, 255 means `unknown' or `no connection'.
307 1.9 leo * Interpret this as 'no interrupt assigned'.
308 1.9 leo */
309 1.9 leo if (line == 255) {
310 1.9 leo *ihp = -1;
311 1.9 leo return 1;
312 1.9 leo }
313 1.9 leo
314 1.9 leo /*
315 1.9 leo * Values are pretty useless because the on the Hades all interrupt
316 1.9 leo * lines for a card are tied together and hardwired to the TT-MFP
317 1.9 leo * I/O port.
318 1.9 leo */
319 1.9 leo *ihp = line;
320 1.9 leo return 0;
321 1.1 leo }
322 1.1 leo
323 1.1 leo const char *
324 1.1 leo pci_intr_string(pc, ih)
325 1.1 leo pci_chipset_tag_t pc;
326 1.1 leo pci_intr_handle_t ih;
327 1.1 leo {
328 1.1 leo static char irqstr[8]; /* 4 + 2 + NULL + sanity */
329 1.1 leo
330 1.9 leo if (ih == -1)
331 1.1 leo panic("pci_intr_string: bogus handle 0x%x\n", ih);
332 1.1 leo
333 1.3 christos sprintf(irqstr, "irq %d", ih);
334 1.1 leo return (irqstr);
335 1.1 leo
336 1.1 leo }
337 1.1 leo
338 1.9 leo /*
339 1.9 leo * The interrupt stuff is rather ugly. On the Hades, all interrupt lines
340 1.9 leo * for a slot are wired together and connected to IO 0,1,2 or 5 (slots:
341 1.9 leo * (0-3) on the TT-MFP. The Pci-config code initializes the irq. number
342 1.9 leo * to the slot position.
343 1.9 leo */
344 1.9 leo static pci_intr_info_t iinfo[4] = { { -1 }, { -1 }, { -1 }, { -1 } };
345 1.9 leo
346 1.9 leo static int iifun __P((int, int));
347 1.9 leo
348 1.9 leo static int
349 1.9 leo iifun(slot, sr)
350 1.9 leo int slot;
351 1.9 leo int sr;
352 1.9 leo {
353 1.9 leo pci_intr_info_t *iinfo_p;
354 1.9 leo int s;
355 1.9 leo
356 1.9 leo iinfo_p = &iinfo[slot];
357 1.9 leo
358 1.9 leo /*
359 1.9 leo * Disable the interrupts
360 1.9 leo */
361 1.9 leo MFP2->mf_imrb &= ~iinfo_p->imask;
362 1.9 leo
363 1.12 leo if ((sr & PSL_IPL) >= (iinfo_p->ipl & PSL_IPL)) {
364 1.9 leo /*
365 1.9 leo * We're running at a too high priority now.
366 1.9 leo */
367 1.9 leo add_sicallback((si_farg)iifun, (void*)slot, 0);
368 1.9 leo }
369 1.9 leo else {
370 1.9 leo s = splx(iinfo_p->ipl);
371 1.9 leo (void) (iinfo_p->ifunc)(iinfo_p->iarg);
372 1.9 leo splx(s);
373 1.9 leo
374 1.9 leo /*
375 1.9 leo * Re-enable interrupts after handling
376 1.9 leo */
377 1.9 leo MFP2->mf_imrb |= iinfo_p->imask;
378 1.9 leo }
379 1.9 leo return 1;
380 1.9 leo }
381 1.9 leo
382 1.1 leo void *
383 1.9 leo pci_intr_establish(pc, ih, level, ih_fun, ih_arg)
384 1.9 leo pci_chipset_tag_t pc;
385 1.9 leo pci_intr_handle_t ih;
386 1.9 leo int level;
387 1.9 leo int (*ih_fun) __P((void *));
388 1.9 leo void *ih_arg;
389 1.9 leo {
390 1.9 leo pci_intr_info_t *iinfo_p;
391 1.9 leo struct intrhand *ihand;
392 1.9 leo int slot;
393 1.9 leo
394 1.9 leo slot = ih;
395 1.9 leo iinfo_p = &iinfo[slot];
396 1.9 leo
397 1.9 leo if (iinfo_p->ipl > 0)
398 1.9 leo panic("pci_intr_establish: interrupt was already established\n");
399 1.9 leo
400 1.9 leo ihand = intr_establish((slot == 3) ? 23 : 16 + slot, USER_VEC, 0,
401 1.9 leo (hw_ifun_t)iifun, (void *)slot);
402 1.9 leo if (ihand != NULL) {
403 1.9 leo iinfo_p->ipl = level;
404 1.9 leo iinfo_p->imask = (slot == 3) ? 0x80 : (0x01 << slot);
405 1.9 leo iinfo_p->ifunc = ih_fun;
406 1.9 leo iinfo_p->iarg = ih_arg;
407 1.9 leo iinfo_p->ihand = ihand;
408 1.9 leo
409 1.9 leo /*
410 1.9 leo * Enable (unmask) the interrupt
411 1.9 leo */
412 1.9 leo MFP2->mf_imrb |= iinfo_p->imask;
413 1.9 leo MFP2->mf_ierb |= iinfo_p->imask;
414 1.9 leo return(iinfo_p);
415 1.9 leo }
416 1.1 leo return NULL;
417 1.1 leo }
418 1.1 leo
419 1.1 leo void
420 1.1 leo pci_intr_disestablish(pc, cookie)
421 1.1 leo pci_chipset_tag_t pc;
422 1.1 leo void *cookie;
423 1.1 leo {
424 1.9 leo pci_intr_info_t *iinfo_p = (pci_intr_info_t *)cookie;
425 1.9 leo
426 1.9 leo if (iinfo->ipl < 0)
427 1.9 leo panic("pci_intr_disestablish: interrupt was not established\n");
428 1.9 leo
429 1.9 leo MFP2->mf_imrb &= ~iinfo->imask;
430 1.9 leo MFP2->mf_ierb &= ~iinfo->imask;
431 1.9 leo (void) intr_disestablish(iinfo_p->ihand);
432 1.9 leo iinfo_p->ipl = -1;
433 1.1 leo }
434