2 * Copyright (C) 1998, 1999 Jesper Skov
4 * Basically what is needed to replace functionality found in
5 * arch/m68k allowing Amiga drivers to work under APUS.
6 * Bits of code and/or ideas from arch/m68k and arch/ppc files.
9 * This file needs a *really* good cleanup. Restructure and optimize.
10 * Make sure it can be compiled for non-APUS configs. Begin to move
11 * Amiga specific stuff into mach/amiga.
14 #include <linux/config.h>
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/init.h>
18 #include <linux/initrd.h>
19 #include <linux/seq_file.h>
21 /* Needs INITSERIAL call in head.S! */
24 #include <asm/bootinfo.h>
25 #include <asm/setup.h>
26 #include <asm/amigahw.h>
27 #include <asm/amigaints.h>
28 #include <asm/amigappc.h>
29 #include <asm/pgtable.h>
31 #include <asm/machdep.h>
34 unsigned long m68k_machtype;
35 char debug_device[6] = "";
37 extern void amiga_init_IRQ(void);
39 extern void apus_setup_pci_ptrs(void);
41 void (*mach_sched_init) (void (*handler)(int, void *, struct pt_regs *)) __initdata = NULL;
42 /* machine dependent irq functions */
43 void (*mach_init_IRQ) (void) __initdata = NULL;
44 void (*(*mach_default_handler)[]) (int, void *, struct pt_regs *) = NULL;
45 void (*mach_get_model) (char *model) = NULL;
46 int (*mach_get_hardware_list) (char *buffer) = NULL;
47 int (*mach_get_irq_list) (struct seq_file *, void *) = NULL;
48 void (*mach_process_int) (int, struct pt_regs *) = NULL;
49 /* machine dependent timer functions */
50 unsigned long (*mach_gettimeoffset) (void);
51 void (*mach_gettod) (int*, int*, int*, int*, int*, int*);
52 int (*mach_hwclk) (int, struct hwclk_time*) = NULL;
53 int (*mach_set_clock_mmss) (unsigned long) = NULL;
54 void (*mach_reset)( void );
55 long mach_max_dma_address = 0x00ffffff; /* default set to the lower 16MB */
56 #ifdef CONFIG_HEARTBEAT
57 void (*mach_heartbeat) (int) = NULL;
58 extern void apus_heartbeat (void);
61 extern unsigned long amiga_model;
62 extern unsigned decrementer_count;/* count value for 1e6/HZ microseconds */
63 extern unsigned count_period_num; /* 1 decrementer count equals */
64 extern unsigned count_period_den; /* count_period_num / count_period_den us */
67 struct mem_info memory[NUM_MEMINFO];/* memory description */
68 /* FIXME: Duplicate memory data to avoid conflicts with m68k shared code. */
69 int m68k_realnum_memory = 0;
70 struct mem_info m68k_memory[NUM_MEMINFO];/* memory description */
72 struct mem_info ramdisk;
74 extern void config_amiga(void);
76 static int __60nsram = 0;
79 static int __bus_speed = 0;
80 static int __speed_test_failed = 0;
82 /********************************************** COMPILE PROTECTION */
83 /* Provide some stubs that links to Amiga specific functions.
84 * This allows CONFIG_APUS to be removed from generic PPC files while
85 * preventing link errors for other PPC targets.
87 unsigned long apus_get_rtc_time(void)
90 extern unsigned long m68k_get_rtc_time(void);
92 return m68k_get_rtc_time ();
98 int apus_set_rtc_time(unsigned long nowtime)
101 extern int m68k_set_rtc_time(unsigned long nowtime);
103 return m68k_set_rtc_time (nowtime);
109 /*********************************************************** SETUP */
110 /* From arch/m68k/kernel/setup.c. */
111 void __init apus_setup_arch(void)
114 extern char cmd_line[];
118 /* Let m68k-shared code know it should do the Amiga thing. */
119 m68k_machtype = MACH_AMIGA;
121 /* Parse the command line for arch-specific options.
122 * For the m68k, this is currently only "debug=xxx" to enable printing
123 * certain kernel messages to some machine-specific device. */
124 for( p = cmd_line; p && *p; ) {
126 if (!strncmp( p, "debug=", 6 )) {
127 strlcpy( debug_device, p+6, sizeof(debug_device) );
128 if ((q = strchr( debug_device, ' ' ))) *q = 0;
130 } else if (!strncmp( p, "60nsram", 7 )) {
131 APUS_WRITE (APUS_REG_WAITSTATE,
132 REGWAITSTATE_SETRESET
140 /* option processed, delete it */
141 if ((q = strchr( p, ' ' )))
146 if ((p = strchr( p, ' ' ))) ++p;
152 #if 0 /* Enable for logging - also include logging.o in Makefile rule */
154 #define LOG_SIZE 4096
157 /* Throw away some memory - the P5 firmare stomps on top
158 * of CHIP memory during bootup.
160 amiga_chip_alloc(0x1000);
162 base = amiga_chip_alloc(LOG_SIZE+sizeof(klog_data_t));
163 LOG_INIT(base, base+sizeof(klog_data_t), LOG_SIZE);
170 apus_show_cpuinfo(struct seq_file *m)
172 extern int __map_without_bats;
173 extern unsigned long powerup_PCI_present;
175 seq_printf(m, "machine\t\t: Amiga\n");
176 seq_printf(m, "bus speed\t: %d%s", __bus_speed,
177 (__speed_test_failed) ? " [failed]\n" : "\n");
178 seq_printf(m, "using BATs\t: %s\n",
179 (__map_without_bats) ? "No" : "Yes");
180 seq_printf(m, "ram speed\t: %dns\n", (__60nsram) ? 60 : 70);
181 seq_printf(m, "PCI bridge\t: %s\n",
182 (powerup_PCI_present) ? "Yes" : "No");
186 static void get_current_tb(unsigned long long *time)
188 __asm __volatile ("1:mftbu 4 \n\t"
201 void apus_calibrate_decr(void)
206 /* This algorithm for determining the bus speed was
207 contributed by Ralph Schmidt. */
208 unsigned long long start, stop;
210 int speed_test_failed = 0;
213 unsigned long loop = amiga_eclock / 10;
215 get_current_tb (&start);
221 get_current_tb (&stop);
224 bus_speed = (((unsigned long)(stop-start))*10*4) / 1000000;
225 if (AMI_1200 == amiga_model)
228 if ((bus_speed >= 47) && (bus_speed < 53)) {
231 } else if ((bus_speed >= 57) && (bus_speed < 63)) {
234 } else if ((bus_speed >= 63) && (bus_speed < 69)) {
238 printk ("APUS: Unable to determine bus speed (%d). "
239 "Defaulting to 50MHz", bus_speed);
242 speed_test_failed = 1;
245 /* Ease diagnostics... */
247 extern int __map_without_bats;
248 extern unsigned long powerup_PCI_present;
250 printk ("APUS: BATs=%d, BUS=%dMHz",
251 (__map_without_bats) ? 0 : 1,
253 if (speed_test_failed)
254 printk ("[FAILED - please report]");
256 printk (", RAM=%dns, PCI bridge=%d\n",
257 (__60nsram) ? 60 : 70,
258 (powerup_PCI_present) ? 1 : 0);
260 /* print a bit more if asked politely... */
261 if (!(ciaa.pra & 0x40)){
262 extern unsigned int bat_addrs[4][3];
264 for (b = 0; b < 4; ++b) {
265 printk ("APUS: BAT%d ", b);
266 printk ("%08x-%08x -> %08x\n",
275 printk("time_init: decrementer frequency = %lu.%.6lu MHz\n",
276 freq/1000000, freq%1000000);
277 tb_ticks_per_jiffy = freq / HZ;
278 tb_to_us = mulhwu_scale_factor(freq, 1000000);
280 __bus_speed = bus_speed;
281 __speed_test_failed = speed_test_failed;
285 void arch_gettod(int *year, int *mon, int *day, int *hour,
290 mach_gettod(year, mon, day, hour, min, sec);
292 *year = *mon = *day = *hour = *min = *sec = 0;
296 /* for "kbd-reset" cmdline param */
298 void kbd_reset_setup(char *str, int *ints)
302 /*********************************************************** MEMORY */
304 unsigned long kmap_chunks[KMAP_MAX*3];
305 int kmap_chunk_count = 0;
308 static __inline__ pte_t *my_find_pte(struct mm_struct *mm,unsigned long va)
316 dir = pgd_offset( mm, va );
319 pmd = pmd_offset(dir, va & PAGE_MASK);
320 if (pmd && pmd_present(*pmd))
322 pte = pte_offset(pmd, va);
329 /* Again simulating an m68k/mm/kmap.c function. */
330 void kernel_set_cachemode( unsigned long address, unsigned long size,
333 unsigned long mask, flags;
337 case IOMAP_FULL_CACHING:
338 mask = ~(_PAGE_NO_CACHE | _PAGE_GUARDED);
341 case IOMAP_NOCACHE_SER:
343 flags = (_PAGE_NO_CACHE | _PAGE_GUARDED);
346 panic ("kernel_set_cachemode() doesn't support mode %d\n",
352 address &= PAGE_MASK;
357 pte = my_find_pte(&init_mm, address);
360 printk("pte NULL in kernel_set_cachemode()\n");
364 pte_val (*pte) &= mask;
365 pte_val (*pte) |= flags;
366 flush_tlb_page(find_vma(&init_mm,address),address);
368 address += PAGE_SIZE;
372 unsigned long mm_ptov (unsigned long paddr)
375 if (paddr < 16*1024*1024)
376 ret = ZTWO_VADDR(paddr);
380 for (i = 0; i < kmap_chunk_count;){
381 unsigned long phys = kmap_chunks[i++];
382 unsigned long size = kmap_chunks[i++];
383 unsigned long virt = kmap_chunks[i++];
385 && paddr < (phys + size)){
386 ret = virt + paddr - phys;
391 ret = (unsigned long) __va(paddr);
395 printk ("PTOV(%lx)=%lx\n", paddr, ret);
400 int mm_end_of_chunk (unsigned long addr, int len)
402 if (memory[0].addr + memory[0].size == addr + len)
407 /*********************************************************** CACHE */
409 #define L1_CACHE_BYTES 32
410 #define MAX_CACHE_SIZE 8192
411 void cache_push(__u32 addr, int length)
413 addr = mm_ptov(addr);
415 if (MAX_CACHE_SIZE < length)
416 length = MAX_CACHE_SIZE;
419 __asm ("dcbf 0,%0\n\t"
421 addr += L1_CACHE_BYTES;
422 length -= L1_CACHE_BYTES;
424 /* Also flush trailing block */
425 __asm ("dcbf 0,%0\n\t"
430 void cache_clear(__u32 addr, int length)
432 if (MAX_CACHE_SIZE < length)
433 length = MAX_CACHE_SIZE;
435 addr = mm_ptov(addr);
437 __asm ("dcbf 0,%0\n\t"
443 addr += L1_CACHE_BYTES;
444 length -= L1_CACHE_BYTES;
447 __asm ("dcbf 0,%0\n\t"
452 addr += L1_CACHE_BYTES;
453 length -= L1_CACHE_BYTES;
456 __asm ("dcbf 0,%0\n\t"
463 /****************************************************** from setup.c */
465 apus_restart(char *cmd)
469 APUS_WRITE(APUS_REG_LOCK,
470 REGLOCK_BLACKMAGICK1|REGLOCK_BLACKMAGICK2);
471 APUS_WRITE(APUS_REG_LOCK,
472 REGLOCK_BLACKMAGICK1|REGLOCK_BLACKMAGICK3);
473 APUS_WRITE(APUS_REG_LOCK,
474 REGLOCK_BLACKMAGICK2|REGLOCK_BLACKMAGICK3);
475 APUS_WRITE(APUS_REG_SHADOW, REGSHADOW_SELFRESET);
476 APUS_WRITE(APUS_REG_RESET, REGRESET_AMIGARESET);
492 /****************************************************** IRQ stuff */
494 static unsigned char last_ipl[8];
496 int apus_get_irq(struct pt_regs* regs)
498 unsigned char ipl_emu, mask;
501 APUS_READ(APUS_IPL_EMU, ipl_emu);
502 level = (ipl_emu >> 3) & IPLEMU_IPLMASK;
503 mask = IPLEMU_SETRESET|IPLEMU_DISABLEINT|level;
506 /* Save previous IPL value */
509 last_ipl[level] = ipl_emu;
511 /* Set to current IPL value */
512 APUS_WRITE(APUS_IPL_EMU, mask);
513 APUS_WRITE(APUS_IPL_EMU, IPLEMU_DISABLEINT|level);
516 #ifdef __INTERRUPT_DEBUG
517 printk("<%d:%d>", level, ~ipl_emu & IPLEMU_IPLMASK);
519 return level + IRQ_AMIGA_AUTO;
522 void apus_end_irq(unsigned int irq)
524 unsigned char ipl_emu;
525 unsigned int level = irq - IRQ_AMIGA_AUTO;
526 #ifdef __INTERRUPT_DEBUG
527 printk("{%d}", ~last_ipl[level] & IPLEMU_IPLMASK);
529 /* Restore IPL to the previous value */
530 ipl_emu = last_ipl[level] & IPLEMU_IPLMASK;
531 APUS_WRITE(APUS_IPL_EMU, IPLEMU_SETRESET|IPLEMU_DISABLEINT|ipl_emu);
534 APUS_WRITE(APUS_IPL_EMU, IPLEMU_DISABLEINT|ipl_emu);
537 /****************************************************** debugging */
539 /* some serial hardware definitions */
540 #define SDR_OVRUN (1<<15)
541 #define SDR_RBF (1<<14)
542 #define SDR_TBE (1<<13)
543 #define SDR_TSRE (1<<12)
545 #define AC_SETCLR (1<<15)
546 #define AC_UARTBRK (1<<11)
548 #define SER_DTR (1<<7)
549 #define SER_RTS (1<<6)
550 #define SER_DCD (1<<5)
551 #define SER_CTS (1<<4)
552 #define SER_DSR (1<<3)
554 static __inline__ void ser_RTSon(void)
556 ciab.pra &= ~SER_RTS; /* active low */
559 int __debug_ser_out( unsigned char c )
561 amiga_custom.serdat = c | 0x100;
563 while (!(amiga_custom.serdatr & 0x2000))
568 unsigned char __debug_ser_in( void )
572 /* XXX: is that ok?? derived from amiga_ser.c... */
573 while( !(amiga_custom.intreqr & IF_RBF) )
575 c = amiga_custom.serdatr;
576 /* clear the interrupt, so that another character can be read */
577 amiga_custom.intreq = IF_RBF;
581 int __debug_serinit( void )
585 local_irq_save(flags);
587 /* turn off Rx and Tx interrupts */
588 amiga_custom.intena = IF_RBF | IF_TBE;
590 /* clear any pending interrupt */
591 amiga_custom.intreq = IF_RBF | IF_TBE;
593 local_irq_restore(flags);
596 * set the appropriate directions for the modem control flags,
597 * and clear RTS and DTR
599 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
600 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
603 /* turn Rx interrupts on for GDB */
604 amiga_custom.intena = IF_SETCLR | IF_RBF;
611 void __debug_print_hex(unsigned long x)
614 char hexchars[] = "0123456789ABCDEF";
616 for (i = 0; i < 8; i++) {
617 __debug_ser_out(hexchars[(x >> 28) & 15]);
620 __debug_ser_out('\n');
621 __debug_ser_out('\r');
624 void __debug_print_string(char* s)
629 __debug_ser_out('\n');
630 __debug_ser_out('\r');
633 static void apus_progress(char *s, unsigned short value)
635 __debug_print_string(s);
638 /****************************************************** init */
640 /* The number of spurious interrupts */
641 volatile unsigned int num_spurious;
643 extern struct irqaction amiga_sys_irqaction[AUTO_IRQS];
646 extern void amiga_enable_irq(unsigned int irq);
647 extern void amiga_disable_irq(unsigned int irq);
649 struct hw_interrupt_type amiga_sys_irqctrl = {
650 .typename = "Amiga IPL",
654 struct hw_interrupt_type amiga_irqctrl = {
655 .typename = "Amiga ",
656 .enable = amiga_enable_irq,
657 .disable = amiga_disable_irq,
660 #define HARDWARE_MAPPED_SIZE (512*1024)
661 unsigned long __init apus_find_end_of_memory(void)
666 /* The memory size reported by ADOS excludes the 512KB
667 reserved for PPC exception registers and possibly 512KB
668 containing a shadow of the ADOS ROM. */
670 unsigned long size = memory[0].size;
672 /* If 2MB aligned, size was probably user
673 specified. We can't tell anything about shadowing
674 in this case so skip shadow assignment. */
675 if (0 != (size & 0x1fffff)){
676 /* Align to 512KB to ensure correct handling
677 of both memfile and system specified
679 size = ((size+0x0007ffff) & 0xfff80000);
680 /* If memory is 1MB aligned, assume
682 shadow = !(size & 0x80000);
685 /* Add the chunk that ADOS does not see. by aligning
686 the size to the nearest 2MB limit upwards. */
687 memory[0].size = ((size+0x001fffff) & 0xffe00000);
690 ppc_memstart = memory[0].addr;
691 ppc_memoffset = PAGE_OFFSET - PPC_MEMSTART;
692 total = memory[0].size;
694 /* Remove the memory chunks that are controlled by special
697 /* Remove the upper 512KB if it contains a shadow of
698 the ADOS ROM. FIXME: It might be possible to
699 disable this shadow HW. Check the booter
702 total -= HARDWARE_MAPPED_SIZE;
704 /* Remove the upper 512KB where the PPC exception
705 vectors are mapped. */
706 total -= HARDWARE_MAPPED_SIZE;
708 /* Linux/APUS only handles one block of memory -- the one on
709 the PowerUP board. Other system memory is horrible slow in
710 comparison. The user can use other memory for swapping
711 using the z2ram device. */
718 /* Map PPC exception vectors. */
719 io_block_mapping(0xfff00000, 0xfff00000, 0x00020000, _PAGE_KERNEL);
720 /* Map chip and ZorroII memory */
721 io_block_mapping(zTwoBase, 0x00000000, 0x01000000, _PAGE_IO);
725 void apus_init_IRQ(void)
727 struct irqaction *action;
731 apus_setup_pci_ptrs();
734 for ( i = 0 ; i < AMI_IRQS; i++ ) {
735 irq_desc[i].status = IRQ_LEVEL;
736 if (i < IRQ_AMIGA_AUTO) {
737 irq_desc[i].handler = &amiga_irqctrl;
739 irq_desc[i].handler = &amiga_sys_irqctrl;
740 action = &amiga_sys_irqaction[i-IRQ_AMIGA_AUTO];
742 setup_irq(i, action);
751 void platform_init(unsigned long r3, unsigned long r4, unsigned long r5,
752 unsigned long r6, unsigned long r7)
754 extern int parse_bootinfo(const struct bi_record *);
757 /* Parse bootinfo. The bootinfo is located right after
759 parse_bootinfo((const struct bi_record *)&_end);
760 #ifdef CONFIG_BLK_DEV_INITRD
761 /* Take care of initrd if we have one. Use data from
762 bootinfo to avoid the need to initialize PPC
763 registers when kernel is booted via a PPC reset. */
764 if ( ramdisk.addr ) {
765 initrd_start = (unsigned long) __va(ramdisk.addr);
766 initrd_end = (unsigned long)
767 __va(ramdisk.size + ramdisk.addr);
769 #endif /* CONFIG_BLK_DEV_INITRD */
771 ISA_DMA_THRESHOLD = 0x00ffffff;
773 ppc_md.setup_arch = apus_setup_arch;
774 ppc_md.show_cpuinfo = apus_show_cpuinfo;
775 ppc_md.init_IRQ = apus_init_IRQ;
776 ppc_md.get_irq = apus_get_irq;
778 #ifdef CONFIG_HEARTBEAT
779 ppc_md.heartbeat = apus_heartbeat;
780 ppc_md.heartbeat_count = 1;
784 ppc_md.progress = apus_progress;
788 ppc_md.restart = apus_restart;
789 ppc_md.power_off = apus_power_off;
790 ppc_md.halt = apus_halt;
792 ppc_md.time_init = NULL;
793 ppc_md.set_rtc_time = apus_set_rtc_time;
794 ppc_md.get_rtc_time = apus_get_rtc_time;
795 ppc_md.calibrate_decr = apus_calibrate_decr;
797 ppc_md.find_end_of_memory = apus_find_end_of_memory;
798 ppc_md.setup_io_mappings = apus_map_io;