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