xref: /netbsd-src/sys/arch/acorn32/podulebus/podulebus.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /* $NetBSD: podulebus.c,v 1.17 2003/05/21 17:17:51 thorpej Exp $ */
2 
3 /*
4  * Copyright (c) 1994-1996 Mark Brinicombe.
5  * Copyright (c) 1994 Brini.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Brini.
19  * 4. The name of the company nor the name of the author may be used to
20  *    endorse or promote products derived from this software without specific
21  *    prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * RiscBSD kernel project
36  *
37  * podulebus.c
38  *
39  * Podule probe and configuration routines
40  *
41  * Created      : 07/11/94
42  */
43 
44 #include <sys/param.h>
45 
46 __KERNEL_RCSID(0, "$NetBSD: podulebus.c,v 1.17 2003/05/21 17:17:51 thorpej Exp $");
47 
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/conf.h>
51 #include <sys/malloc.h>
52 #include <sys/device.h>
53 #include <uvm/uvm_extern.h>
54 #include <machine/io.h>
55 #include <arm/arm32/katelib.h>
56 #include <machine/intr.h>
57 #include <machine/bootconfig.h>
58 #include <machine/pmap.h>
59 #include <arm/iomd/iomdreg.h>
60 #include <arm/iomd/iomdvar.h>
61 #include <acorn32/podulebus/podulebus.h>
62 #include <dev/podulebus/podules.h>
63 #include <dev/podulebus/podule_data.h>
64 
65 #include "locators.h"
66 
67 /* Array of podule structures, one per possible podule */
68 
69 podule_t podules[MAX_PODULES + MAX_NETSLOTS];
70 
71 extern struct bus_space podulebus_bs_tag;
72 
73 /* Declare prototypes */
74 
75 u_int poduleread __P((u_int, int));
76 int podulebusmatch(struct device *, struct cfdata *, void *);
77 void podulebusattach(struct device *, struct device *, void *);
78 int podulebusprint(void *, const char *);
79 int podulebussubmatch(struct device *, struct cfdata *, void *);
80 void podulechunkdirectory(podule_t *);
81 void podulescan(struct device *);
82 
83 /*
84  * int podulebusmatch(struct device *parent, void *match, void *aux)
85  *
86  * Probe for the podule bus. Currently all this does is return 1 to
87  * indicate that the podule bus was found.
88  */
89 
90 int
91 podulebusmatch(parent, cf, aux)
92 	struct device *parent;
93 	struct cfdata *cf;
94 	void *aux;
95 {
96 	switch (IOMD_ID) {
97 	case RPC600_IOMD_ID:
98 	case ARM7500_IOC_ID:
99 	case ARM7500FE_IOC_ID:
100 		return(1);
101 	}
102 	return (0);
103 }
104 
105 
106 int
107 podulebusprint(aux, name)
108 	void *aux;
109 	const char *name;
110 {
111 	struct podule_attach_args *pa = aux;
112 
113 	if (name)
114 		aprint_normal("podule at %s", name);
115 	if (pa->pa_podule->slottype == SLOT_POD)
116 		aprint_normal(" slot %d", pa->pa_podule_number);
117 	else if (pa->pa_podule->slottype == SLOT_NET)
118 		aprint_normal(" [ netslot %d ]",
119 		    pa->pa_podule_number - MAX_PODULES);
120 #ifdef DIAGNOSTIC
121 	else
122 		panic("Invalid slot type");
123 #endif
124 
125 	return (UNCONF);
126 }
127 
128 
129 int
130 podulebussubmatch(parent, cf, aux)
131 	struct device *parent;
132 	struct cfdata *cf;
133 	void *aux;
134 {
135 	struct podule_attach_args *pa = aux;
136 
137 	/* Return priority 0 or 1 for wildcarded podule */
138 
139 	if (cf->cf_loc[PODULEBUSCF_SLOT] == PODULEBUSCF_SLOT_DEFAULT)
140 		return(config_match(parent, cf, aux));
141 
142 	/* Return higher priority if we match the specific podule */
143 
144 	else if (cf->cf_loc[PODULEBUSCF_SLOT] == pa->pa_podule_number)
145 		return(config_match(parent, cf, aux) * 8);
146 
147 	/* Fail */
148 	return(0);
149 }
150 
151 
152 #if 0
153 void
154 dump_podule(podule)
155 	podule_t *podule;
156 {
157 	printf("podule%d: ", podule->podulenum);
158 	printf("flags0=%02x ", podule->flags0);
159 	printf("flags1=%02x ", podule->flags1);
160 	printf("reserved=%02x ", podule->reserved);
161 	printf("product=%02x ", podule->product);
162 	printf("manufacturer=%02x ", podule->manufacturer);
163 	printf("country=%02x ", podule->country);
164 	printf("irq_addr=%08x ", podule->irq_addr);
165 	printf("irq_mask=%02x ", podule->irq_mask);
166 	printf("fiq_addr=%08x ", podule->fiq_addr);
167 	printf("fiq_mask=%02x ", podule->fiq_mask);
168 	printf("fast_base=%08x ", podule->fast_base);
169 	printf("medium_base=%08x ", podule->medium_base);
170 	printf("slow_base=%08x ", podule->slow_base);
171 	printf("sync_base=%08x ", podule->sync_base);
172 	printf("mod_base=%08x ", podule->mod_base);
173 	printf("easi_base=%08x ", podule->easi_base);
174 	printf("attached=%d ", podule->attached);
175 	printf("slottype=%d ", podule->slottype);
176 	printf("podulenum=%d ", podule->podulenum);
177 	printf("description=%s ", podule->description);
178 	printf("\n");
179 }
180 #endif
181 
182 void
183 podulechunkdirectory(podule)
184 	podule_t *podule;
185 {
186 	u_int address;
187 	u_int id;
188 	u_int size;
189 	u_int addr;
190 	int loop;
191 	int done_f5;
192 
193 	done_f5 = 0;
194 	address = 0x40;
195 
196 	do {
197 		id = podule->read_rom(podule->sync_base, address);
198 		size = podule->read_rom(podule->sync_base, address + 4);
199 		size |= (podule->read_rom(podule->sync_base, address + 8) << 8);
200 		size |= (podule->read_rom(podule->sync_base, address + 12) << 16);
201 		if (id == 0xf5) {
202 			addr = podule->read_rom(podule->sync_base, address + 16);
203 			addr |= (podule->read_rom(podule->sync_base, address + 20) << 8);
204 			addr |= (podule->read_rom(podule->sync_base, address + 24) << 16);
205 			addr |= (podule->read_rom(podule->sync_base, address + 28) << 24);
206 			if (addr < 0x800 && done_f5 == 0) {
207 				done_f5 = 1;
208 				for (loop = 0; loop < size; ++loop) {
209 					if (loop < PODULE_DESCRIPTION_LENGTH) {
210 						podule->description[loop] =
211 						    podule->read_rom(podule->sync_base, (addr + loop)*4);
212 						podule->description[loop + 1] = 0;
213 					}
214 				}
215 			}
216 		}
217 #ifdef DEBUG_CHUNK_DIR
218 		if (id == 0xf5 || id == 0xf1 || id == 0xf2 || id == 0xf3 || id == 0xf4 || id == 0xf6) {
219 			addr = podule->read_rom(podule->sync_base, address + 16);
220 			addr |= (podule->read_rom(podule->sync_base, address + 20) << 8);
221 			addr |= (podule->read_rom(podule->sync_base, address + 24) << 16);
222 			addr |= (podule->read_rom(podule->sync_base, address + 28) << 24);
223 			printf("<%04x.%04x.%04x.%04x>", id, address, addr, size);
224 			if (addr < 0x800) {
225 				for (loop = 0; loop < size; ++loop) {
226 					printf("%c", podule->read_rom(podule->sync_base, (addr + loop)*4));
227 				}
228 				printf("\\n\n");
229 			}
230 		}
231 #endif
232 		address += 32;
233 	} while (id != 0 && address < 0x800);
234 }
235 
236 
237 void
238 poduleexamine(podule, dev, slottype)
239 	podule_t *podule;
240 	struct device *dev;
241 	int slottype;
242 {
243 	struct manufacturer_description *man_desc;
244 	struct podule_description *pod_desc;
245 
246 	/* Test to see if the podule is present */
247 
248 	if ((podule->flags0 & 0x02) == 0x00) {
249 		podule->slottype = slottype;
250 		if (slottype == SLOT_NET)
251 			printf("netslot%d at %s : ", podule->podulenum - MAX_PODULES,
252 			    dev->dv_xname);
253 		else
254 			printf("podule%d  at %s : ", podule->podulenum,
255 			    dev->dv_xname);
256 
257 		/* Is it Acorn conformant ? */
258 
259 		if (podule->flags0 & 0x80)
260 			printf("Non-Acorn conformant expansion card\n");
261 		else {
262 			int id;
263 
264 			/* Is it a simple podule ? */
265 
266 			id = (podule->flags0 >> 3) & 0x0f;
267 			if (id != 0)
268 				printf("Simple expansion card <%x>\n", id);
269 			else {
270 				/* Scan the chunk directory if present for tags we use */
271 				if (podule->flags1 & PODULE_FLAGS_CD)
272 					podulechunkdirectory(podule);
273 
274 				/* Do we know this manufacturer ? */
275 				man_desc = known_manufacturers;
276 				while (man_desc->description) {
277 					if (man_desc->manufacturer_id ==
278 					    podule->manufacturer)
279 						break;
280 					++man_desc;
281 				}
282 				if (!man_desc->description)
283 					printf("man=%04x   : ", podule->manufacturer);
284 				else
285 					printf("%s : ", man_desc->description);
286 
287 				/* Do we know this product ? */
288 
289 				pod_desc = known_podules;
290 				while (pod_desc->description) {
291 					if (pod_desc->product_id == podule->product)
292 						break;
293 					++pod_desc;
294 				}
295 				if (!pod_desc->description)
296 					printf("prod=%04x : ",
297 					    podule->product);
298 				else
299 					printf("%s : ", pod_desc->description);
300 				printf("%s\n", podule->description);
301 			}
302 		}
303 	}
304 }
305 
306 
307 u_int
308 poduleread(address, offset)
309 	u_int address;
310 	int offset;
311 {
312 
313 	return(ReadByte(address + offset));
314 }
315 
316 void
317 podulescan(dev)
318 	struct device *dev;
319 {
320 	int loop;
321 	podule_t *podule;
322 	u_char *address;
323 	u_int offset = 0;
324 
325 	/* Loop round all the podules */
326 
327 	for (loop = 0; loop < MAX_PODULES; ++loop, offset += SIMPLE_PODULE_SIZE) {
328 		podule = &podules[loop];
329 		podule->podulenum = loop;
330 		podule->attached = 0;
331 		podule->slottype = SLOT_NONE;
332 		podule->interrupt = IRQ_PODULE;
333 		podule->read_rom = poduleread;
334 		podule->dma_channel = -1;
335 		podule->dma_interrupt = -1;
336 		podule->description[0] = 0;
337 
338 		if (loop == 4) offset += PODULE_GAP;
339 		address = ((u_char *)SYNC_PODULE_BASE) + offset;
340 
341 		if ((address[0] & 0x02) == 0x00) {
342 			podule->fast_base = FAST_PODULE_BASE + offset;
343 			podule->medium_base = MEDIUM_PODULE_BASE + offset;
344 			podule->slow_base = SLOW_PODULE_BASE + offset;
345 			podule->sync_base = SYNC_PODULE_BASE + offset;
346 			podule->mod_base = MOD_PODULE_BASE + offset;
347 			podule->easi_base = EASI_BASE + loop * EASI_SIZE;
348 		} else {
349 			address = ((u_char *)EASI_BASE) + loop * EASI_SIZE;
350 			if ((address[0] & 0x02) != 0x00)
351 				continue;
352 
353 			podule->fast_base = 0;
354 			podule->medium_base = 0;
355 			podule->slow_base = 0;
356 			podule->sync_base = 0;
357 			podule->mod_base = 0;
358 			podule->easi_base = EASI_BASE + loop * EASI_SIZE;
359 		}
360 
361 		/* XXX - Really needs to be linked to a DMA manager */
362 		if (IOMD_ID == RPC600_IOMD_ID) {
363 			switch (loop) {
364 			case 0:
365 				podule->dma_channel = 2;
366 				podule->dma_interrupt = IRQ_DMACH2;
367 				break;
368 			case 1:
369 				podule->dma_channel = 3;
370 				podule->dma_interrupt = IRQ_DMACH3;
371 				break;
372 			}
373 		}
374 
375 		/* Get information from the podule header */
376 
377 		podule->flags0 = address[0];
378 		if ((podule->flags0 & 0x78) == 0) {
379 			podule->flags1 = address[4];
380 			podule->reserved = address[8];
381 			podule->product = address[12] + (address[16] << 8);
382 			podule->manufacturer = address[20] + (address[24] << 8);
383 			podule->country = address[28];
384 			if (podule->flags1 & PODULE_FLAGS_IS) {
385 				podule->irq_addr = address[52] + (address[56] << 8) + (address[60] << 16);
386 				podule->irq_addr += podule->slow_base;
387 				podule->irq_mask = address[48];
388 				if (podule->irq_mask == 0)
389 					podule->irq_mask = 0x01;
390 				podule->fiq_addr = address[36] + (address[40] << 8) + (address[44] << 16);
391 				podule->fiq_addr += podule->slow_base;
392 				podule->fiq_mask = address[32];
393 				if (podule->fiq_mask == 0)
394 					podule->fiq_mask = 0x04;
395 			} else {
396 				podule->irq_addr = podule->slow_base;
397 				podule->irq_mask = 0x01;
398 				podule->fiq_addr = podule->slow_base;
399 				podule->fiq_mask = 0x04;
400 			}
401 		}
402 
403 		poduleexamine(podule, dev, SLOT_POD);
404 	}
405 }
406 
407 
408 /*
409  * void podulebusattach(struct device *parent, struct device *dev, void *aux)
410  *
411  * Attach podulebus.
412  * This probes all the podules and sets up the podules array with
413  * information found in the podule headers.
414  * After identifing all the podules, all the children of the podulebus
415  * are probed and attached.
416  */
417 
418 void
419 podulebusattach(parent, self, aux)
420 	struct device *parent;
421 	struct device *self;
422 	void *aux;
423 {
424 	int loop;
425 	struct podule_attach_args pa;
426 #if 0
427 	int easi_time;
428 	int bit;
429 #endif
430 	unsigned int value;
431 	char argstring[20];
432 
433 #if 0
434 	easi_time = IOMD_READ_BYTE(IOMD_ECTCR);
435 	printf(": easi timings=");
436 	for (bit = 0x01; bit < 0x100; bit = bit << 1)
437 		if (easi_time & bit)
438 			printf("C");
439 		else
440 			printf("A");
441 #endif
442 	printf("\n");
443 
444 
445 #if 0 /* XXXJRT */
446 	/* Ok we need to map in the podulebus */
447 	/* with the new pmap mappings have to be done when the L1 tables
448 	 * are built during initarm
449 	 */
450 	/* Map the FAST and SYNC simple podules */
451 	pmap_map_section((vm_offset_t)pmap_kernel()->pm_pdir,
452 	    SYNC_PODULE_BASE & 0xfff00000, SYNC_PODULE_HW_BASE & 0xfff00000,
453 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
454 	cpu_tlb_flushD();
455 	/* Now map the EASI space */
456 
457 	for (loop = 0; loop < MAX_PODULES; ++loop) {
458 		int loop1;
459 
460 		for (loop1 = loop * EASI_SIZE; loop1 < ((loop + 1) * EASI_SIZE);
461 		    loop1 += L1_S_SIZE)
462 		pmap_map_section((vm_offset_t)pmap_kernel()->pm_pdir,
463 		    EASI_BASE + loop1, EASI_HW_BASE + loop1,
464 		    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
465 	}
466 	cpu_tlb_flushD();
467 #endif
468 	/*
469 	 * The MEDIUM and SLOW simple podules and the module space will have been
470 	 * mapped when the IOMD and COMBO we mapped in for the RPC
471 	 */
472 
473 	/* Find out what hardware is bolted on */
474 
475 	podulescan(self);
476 	netslotscan(self);
477 
478 	/* Look for drivers to attach */
479 
480 	for (loop = 0; loop < MAX_PODULES+MAX_NETSLOTS; ++loop) {
481 #if 1
482 		/* Provide backwards compat for a while */
483 		sprintf(argstring, "podule%d.disable", loop);
484 		if (get_bootconf_option(boot_args, argstring,
485 		    BOOTOPT_TYPE_BOOLEAN, &value)) {
486 			if (value) {
487 				if (podules[loop].slottype != SLOT_NONE)
488 					printf("podule%d: Disabled\n", loop);
489 				continue;
490 			}
491  		}
492 #endif
493  		sprintf(argstring, "podule%d=", loop);
494  		if (get_bootconf_option(boot_args, argstring,
495  		    BOOTOPT_TYPE_HEXINT, &value)) {
496 			/* Override the ID */
497 			podules[loop].manufacturer = value >> 16;
498  			podules[loop].product = value & 0xffff;
499 			/* Any old description is now wrong */
500 			podules[loop].description[0] = 0;
501 			if (value != 0xffff) {
502 				printf("podule%d: ID overriden man=%04x prod=%04x\n",
503 				    loop, podules[loop].manufacturer,
504 				    podules[loop].product);
505 				podules[loop].slottype = SLOT_POD;
506 				pa.pa_podule_number = loop;
507 				pa.pa_ih = pa.pa_podule_number;
508 				pa.pa_podule = &podules[loop];
509 				pa.pa_iot = &podulebus_bs_tag;
510 				config_found_sm(self, &pa, podulebusprint,
511 				    podulebussubmatch);
512 				continue;
513 			}
514 			if (value == 0xffff) {
515 				printf("podule%d: Disabled\n", loop);
516 				continue;
517 			}
518 		}
519 
520 		if (podules[loop].slottype != SLOT_NONE) {
521 			pa.pa_podule_number = loop;
522 			pa.pa_ih = pa.pa_podule_number;
523 			pa.pa_podule = &podules[loop];
524 			pa.pa_iot = &podulebus_bs_tag;
525 			config_found_sm(self, &pa, podulebusprint, podulebussubmatch);
526 		}
527 	}
528 }
529 
530 
531 CFATTACH_DECL(podulebus, sizeof(struct device),
532 	podulebusmatch, podulebusattach, NULL, NULL);
533 
534 /* Useful functions that drivers may share */
535 
536 /*
537  * Match a podule structure with the specified parameters
538  * Returns 0 if the match failed
539  * The required_slot is not used at the moment.
540  */
541 
542 int
543 matchpodule(pa, manufacturer, product, required_slot)
544 	struct podule_attach_args *pa;
545 	int manufacturer;
546 	int product;
547 	int required_slot;
548 {
549 	if (pa->pa_podule->attached)
550 		panic("podulebus: Podule already attached");
551 
552 	if (IS_PODULE(pa, manufacturer, product))
553 		return(1);
554 
555 	return(0);
556 }
557 
558 void *
559 podulebus_irq_establish(ih, ipl, func, arg, ev)
560 	podulebus_intr_handle_t ih;
561 	int ipl;
562 	int (*func) __P((void *));
563 	void *arg;
564 	struct evcnt *ev;
565 {
566 
567 	/* XXX We don't actually use the evcnt supplied, just its name. */
568 	return intr_claim(podules[ih].interrupt, ipl, ev->ev_group, func,
569 	    arg);
570 }
571 
572 /*
573  * Generate a bus_space_tag_t with the specified address-bus shift.
574  */
575 void
576 podulebus_shift_tag(tag, shift, tagp)
577 	bus_space_tag_t tag, *tagp;
578 	u_int shift;
579 {
580 
581 	/*
582 	 * For the podulebus, the bus tag cookie is the shift to apply
583 	 * to registers, so duplicate the bus space tag and change the
584 	 * cookie.
585 	 */
586 
587 	/* XXX never freed, but podules are never detached anyway. */
588         *tagp = malloc(sizeof(struct bus_space), M_DEVBUF, M_WAITOK);
589 	**tagp = *tag;
590 	(*tagp)->bs_cookie = (void *)shift;
591 }
592 
593 int
594 podulebus_initloader(struct podulebus_attach_args *pa)
595 {
596 
597 	/* No loader support at present on arm32, so always fail. */
598 	return -1;
599 }
600 
601 int
602 podloader_readbyte(struct podulebus_attach_args *pa, u_int addr)
603 {
604 
605 	panic("podloader_readbyte");
606 }
607 
608 void
609 podloader_writebyte(struct podulebus_attach_args *pa, u_int addr, int val)
610 {
611 
612 	panic("podloader_writebyte");
613 }
614 
615 void
616 podloader_reset(struct podulebus_attach_args *pa)
617 {
618 
619 	panic("podloader_reset");
620 }
621 
622 int
623 podloader_callloader(struct podulebus_attach_args *pa, u_int r0, u_int r1)
624 {
625 
626 	panic("podloader_callloader");
627 }
628 
629 /* End of podulebus.c */
630