Date: Thu, 2 Apr 1998 09:05:48 GMT
From: Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
To: linux-m68k@lists.linux-m68k.org
Cc: Randy Thelen <rthelen@ix.netcom.com>
Subject: L68K: Misc 2.1.90 fixes
Sender: owner-linux-m68k@phil.uni-sb.de


Ok, here's my promised patch for 2.1.90. First the easy parts:

 - Remove inclusion of <asm/atari_mouse.h> from atakeyb.c and
   joystick.c, since Andreas' last patch removed that header.

 - arch/m68k/atari/config.c: Definition of atari_mch_type is now in head.S.

 - arch/m68k/atari/stram.c: Remove references to swap_lockmap, this
   field doesn't exist anymore (don't know how swap pages are locked
   now, but don't care... the lockmap never was actively used by
   ST-RAM swapping); rewrite call to read a swap page (macro
   read_swap_page disappeared)

And now to head.S. Here my changes in more detail and with some rationale:
(numbers in parens are line numbers)

 - (255) Change definitions: #define CONSOLE inside #ifdef CONFIG_MAC;
   MMU_PRINT off by default

 - (272, 643, 815, 3485) Replace old is_{medusa,hades} stuff by new
   BI_ATARI_MCH_TYPE stuff; variable atari_mch_type now defined in
   head.S to make PC-relative addressing possible.

 - (472) Setting Mac display parameters should be inside #ifdef
   CONFIG_MAC, since even if the code isn't executed on non-Macs, it
   still references several Mac-only symbols.

 - (595) Fix typo in BVME6000 part: 'a' was appended to the correct
   label name

 - (231, 2565) Support both Atari SCC channels as serial debug
   devices. The #defines for the user now are USE_SCC_B and USE_SCC_A.
   USE_SCC is defined if either of these is defined. The scc_initable
   can be shared between both SCC channels. Use LSCC_CTRL/DATA as
   symbols for register, which are defined appropriately for the
   selected channel. USE_MFP is defined by default, is it was
   previously.

 - (2665) serial_init for Atari must load Liobase into a register and
   address relative to that. PC-relative addressing of I/O registers
   isn't possible anyway, and further would ignore the Liobase
   setting. Hope that trashing %a1 here is possible.

 - (828) Use only one MMU_MAP call for Atari I/O region, making the
   whole thing non-cachable. That the old code only made two 256k
   regions uncachable was laziness, not necessity :-) The rest isn't
   really used, only for sometimes peeking into ROM, so don't care
   much about its cache mode, and save the three additional calls.

 - (779, 787) Remove _PAGE_PRESENT from MMU_MAP flags, added
   automatically in low-level part

 - (1014) Give more rationale for the mmu fixup in the comment

 - (1259, 1291, 1337, 1378) Define symbolic constants for location
   where to do the temporary mapping

 - (1272) Temp. mapping does 16k, not 8k as the comment claimed

 - (1314, 1338, 1408) Surround '040 cinv and pflusha insns by nops,
   since some broken '040s require this

 - (1481) Clear frame pointer (%a6) before calling start_kernel, so
   that kgdb knows what the top-level function is

 - (2132, 2234) mmu_map_040 is also needed for '030 system (if no
   early termination possible), not only for Mac '030 systems :-)
   Remove the #if alltogether.

 - (2187, 2306, 2320) Replace divl and mull by appropriate shifts,
   because those insns must be emulated on a '060, and the emulator
   isn't activated yet during head.S.

 - (2269, ) and #x+cmp sequences can be done faster by and #~x

 - (Throughout mmu_map_*): Replace %d0 by MAP_LENGTH; if there's a
   definition, we should use it consistently :-)

 - (many places) Replace _PAGE_SHORT by _PAGE_TABLE, which is more
   descriptive

 - (many places) Replace hardcoded numbers by symbolic constants where
   possible

 - (many places) Place downcase uppercase letters in hex numbers, to
   make it look more uniform

 - (many places) Replace hardcoded opcodes (.long) by the real ones
   and use .chip

 - (even more places) Add _PAGE_ACCESSED to table entries, to avoid that
   the CPU has to update the entries unnecessarily later.

Further notes:

 - I haven't deleted the penguin code yet, but IMHO this should be
   done. Maybe Randy does himself...

 - I think the mmu fixup stuff could be done easier. The current
   function is pretty nice and general, but we know that all page
   tables and the like are allocated between the initial value of %a6
   and its final value. These two could be remembered somewhere, and
   then just change the cache mode of pages in that range. We can even
   assume the the pte's for them are one after another :-)

 - I had some problems yesterday with gcc 2.8.0 :-( It couldn't
   compile atarilance.c anymore, "internal compiler error" with signal
   6 or 11 (non-deterministic). A quick test showed that the big
   function lance_probe_1 is the guilty one, but I haven't look in
   more detail at this yet.

 - BTW, the new head.S (with my patches) boots fine on my TT :-) I
   just would be interested if it does the same on Medusa/Hades and a
   Falcon with TT-RAM in the low 16 MB (e.g. Magnum/FX board).

Roman

------------------------------------------------------------------------------
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.90.orig/arch/m68k/atari/atakeyb.c linux-2.1.90/arch/m68k/atari/atakeyb.c
--- linux-2.1.90.orig/arch/m68k/atari/atakeyb.c	Thu Mar 19 21:25:39 1998
+++ linux-2.1.90/arch/m68k/atari/atakeyb.c	Wed Apr  1 22:04:53 1998
@@ -28,7 +28,6 @@
 #include <asm/atariints.h>
 #include <asm/atarihw.h>
 #include <asm/atarikb.h>
-#include <asm/atari_mouse.h>
 #include <asm/atari_joystick.h>
 #include <asm/irq.h>
 
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.90.orig/arch/m68k/atari/config.c linux-2.1.90/arch/m68k/atari/config.c
--- linux-2.1.90.orig/arch/m68k/atari/config.c	Mon Jan 12 12:44:24 1998
+++ linux-2.1.90/arch/m68k/atari/config.c	Thu Apr  2 07:50:04 1998
@@ -40,7 +40,6 @@
 #include <asm/machdep.h>
 
 u_long atari_mch_cookie;
-u_long atari_mch_type = 0;
 struct atari_hw_present atari_hw_present;
 u_long atari_switches = 0;
 int atari_dont_touch_floppy_select = 0;
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.90.orig/arch/m68k/atari/joystick.c linux-2.1.90/arch/m68k/atari/joystick.c
--- linux-2.1.90.orig/arch/m68k/atari/joystick.c	Wed Mar 18 13:23:02 1998
+++ linux-2.1.90/arch/m68k/atari/joystick.c	Wed Apr  1 22:06:24 1998
@@ -15,7 +15,6 @@
 
 #include <asm/atarikb.h>
 #include <asm/atari_joystick.h>
-#include <asm/atari_mouse.h>
 #include <asm/uaccess.h>
 
 #define MAJOR_NR    JOYSTICK_MAJOR
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.90.orig/arch/m68k/atari/stram.c linux-2.1.90/arch/m68k/atari/stram.c
--- linux-2.1.90.orig/arch/m68k/atari/stram.c	Thu Feb  5 15:48:55 1998
+++ linux-2.1.90/arch/m68k/atari/stram.c	Wed Apr  1 22:29:07 1998
@@ -357,10 +357,10 @@
 				 "swap=%08lx-%08lx\n", swap_start, swap_end );
 		
 		/* reserve some amount of memory for maintainance of swapping itself:
-		 * 1 page for the lockmap, and one page for each 4096 (PAGE_SIZE) swap
-		 * pages. (1 byte for each page) */
+		 * one page for each 4096 (PAGE_SIZE) swap pages. (1 byte for each
+		 * page) */
 		swap_data = start_mem;
-		start_mem += PAGE_ALIGN(SWAP_NR(swap_end)) + PAGE_SIZE;
+		start_mem += PAGE_ALIGN(SWAP_NR(swap_end));
 		/* correct swap_start if necessary */
 		if (swap_start == swap_data)
 			swap_start = start_mem;
@@ -620,8 +620,7 @@
 	p->flags        = SWP_USED;
 	p->swap_file    = &fake_dentry[0];
 	p->swap_device  = 0;
-	p->swap_lockmap = (unsigned char *)(swap_data);
-	p->swap_map	    = (unsigned char *)(swap_data + PAGE_SIZE);
+	p->swap_map	    = (unsigned char *)swap_data;
 	p->cluster_nr   = 0;
 	p->next         = -1;
 	p->prio         = 0x7ff0;	/* a rather high priority, but not the higest
@@ -634,9 +633,6 @@
 	stram_open( &swap_inode, MAGIC_FILE_P );
 	p->max = SWAP_NR(swap_end);
 
-	/* initialize lockmap */
-	memset( p->swap_lockmap, 0, PAGE_SIZE );
-
 	/* initialize swap_map: set regions that are already allocated or belong
 	 * to kernel data space to SWP_RSVD, otherwise to free */
 	j = 0; /* # of free pages */
@@ -954,7 +950,7 @@
 				return( -ENOMEM );
 			}
 			DPRINTK( "unswap: reading swap page %lu to %08lx\n", i, page );
-			read_swap_page( entry, (char *)page );
+			rw_swap_page( READ, entry, (char *)page, 1 );
 
 			for_each_task(p) {
 				if (unswap_process( p->mm, entry, page, 0 )) {
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.90.orig/arch/m68k/kernel/head.S linux-2.1.90/arch/m68k/kernel/head.S
--- linux-2.1.90.orig/arch/m68k/kernel/head.S	Wed Apr  1 20:28:26 1998
+++ linux-2.1.90/arch/m68k/kernel/head.S	Thu Apr  2 08:00:28 1998
@@ -228,8 +228,9 @@
  * 
  * Atari constants:
  * USE_PRINTER:	Use the printer port for serial debug.
- * USE_SCC:	Use the SCC port for serial debug.
- * USE_MFP:	Use the MFP port for serial debug. 
+ * USE_SCC_B:	Use the SCC port A (Serial2) for serial debug.
+ * USE_SCC_A:	Use the SCC port B (Modem2) for serial debug.
+ * USE_MFP:	Use the ST-MFP port (Modem1) for serial debug. 
  * 
  * DEBUG:	This is the standard DEBUG flag that can be set for building
  *		the kernel.  It has the effect adding additional tests into
@@ -252,8 +253,10 @@
 #include <asm/setup.h>
 #include <asm/pgtable.h>
 
+#if defined(CONFIG_MAC)
 #define CONSOLE
-#define MMU_PRINT
+#endif
+#undef MMU_PRINT
 #undef MMU_NOCACHE_KERNEL
 #define SERIAL_DEBUG
 #undef DEBUG
@@ -269,8 +272,7 @@
 .globl SYMBOL_NAME(swapper_pg_dir)
 
 #if defined(CONFIG_ATARI)
-.globl SYMBOL_NAME(is_hades)
-.globl SYMBOL_NAME(is_medusa)
+.globl SYMBOL_NAME(atari_mch_type)
 #endif
 
 #if defined(CONFIG_MAC)
@@ -336,6 +338,7 @@
 
 /* Miscellaneous definitions */
 PAGESIZE	= 4096
+PAGESHIFT	= 12
 
 ROOT_TABLE_SIZE	= 128
 PTR_TABLE_SIZE	= 128
@@ -469,6 +472,7 @@
 	lea	%pc@(SYMBOL_NAME(m68k_cputype)),%a0
 	movel	%d0,%a0@
 
+#if defined(CONFIG_MAC)
 /*
  * For Macintosh, we need to determine the display parameters early (at least 
  * while debugging it).
@@ -524,6 +528,7 @@
 	movel	%a0@,%d0
 
 Ltest_notmac:
+#endif /* CONFIG_MAC */
 
 
 /*
@@ -592,7 +597,7 @@
 	is_not_bvme6000(Lset_cache040_norm)
 
 	movel	#_PAGE_CACHE040W,%d6
-	jbra	Lsave_cachetypea
+	jbra	Lsave_cachetype
 	
 Lset_cache040_norm:
 #endif /* CONFIG_BVME6000 */
@@ -614,7 +619,7 @@
 	movel	%d6,%a0@
 
 /*
- * raise interrupt level with MASTER bit set, copy isp to msp (if not 68060)
+ * raise interrupt level
  */
 	movew	#0x2700,%sr
 
@@ -640,33 +645,25 @@
 #ifdef CONFIG_ATARI
 	is_not_atari(Lnotypetest)
 
-	moveq	#0,%d3			/* base addr for others: 0x00000000 */
-	moveq	#0,%d2			/* no Hades */
-	movec	%d3,%vbr
-	lea	%pc@(Ltest_berr),%a0
-	movel	%a0,0x8
-	movel	%sp,%a0
-	moveb	0x0,%d1
-	clrb	0x0
-	nop
-	moveb	%d1,0x0
-	nop
-	tstb	0x00ff82fe
-	nop
-	movel	#0xff000000,%d3		/* Medusa base addr: 0xff000000 */
-	tstb	0xb0000000
-	nop
-	movel	#0xff000000,%d2		/* Computer is a Hades */
-	moveq	#0,%d3
-Ltest_berr:
-	movel	%a0,%sp
-	lea	%pc@(SYMBOL_NAME(is_hades)),%a0
-	movel	%d2,%a0@
-	lea	%pc@(SYMBOL_NAME(is_medusa)),%a0
+	/* get special machine type (Medusa/Hades/AB40) */
+	moveq	#0,%d3 /* default if tag doesn't exist */
+	movew	#BI_ATARI_MCH_TYPE,%d0
+	jbsr	Lget_bi_record
+	tstl	%d0
+	jbmi	1f
+	movel	%a0@,%d3
+	lea	%pc@(SYMBOL_NAME(atari_mch_type)),%a0
 	movel	%d3,%a0@
-	lea     %pc@(Liobase),%a0
-	movel   %d2,%a0@		/* On a Hades the iobase must be set
-					   before opening the serial port. */
+1:	
+	/* On the Hades, the iobase must be set up before opening the
+	 * serial port. There are no I/O regs at 0x00ffxxxx at all. */
+	moveq	#0,%d0
+	cmpl	#ATARI_MACH_HADES,%d3
+	jbne	1f
+	movel	#0xff000000,%d0		/* Hades I/O base addr: 0xff000000 */
+1:	lea     %pc@(Liobase),%a0
+	movel   %d0,%a0@
+	
 Lnotypetest:
 #endif
 
@@ -776,7 +773,7 @@
 	/*
 	 * 040: Map the 32Meg range physical 0x0 upto logical 0x8000.0000
 	 */
-	MMU_MAP(#0x80000000,#0,#0x01000000,#_PAGE_GLOBAL040+_PAGE_NOCACHE_S+_PAGE_PRESENT+_PAGE_ACCESSED)
+	MMU_MAP(#0x80000000,#0,#0x01000000,#_PAGE_GLOBAL040+_PAGE_NOCACHE_S+_PAGE_ACCESSED)
 	
 	jbra	mmu_init_done
 
@@ -784,7 +781,7 @@
 	/*
 	 * 030:	Map the 32Meg range physical 0x0 upto logical 0x8000.0000
 	 */
-	MMU_MAP(#0x80000000,#0,#0x02000000,#_PAGE_NOCACHE030+_PAGE_PRESENT+_PAGE_ACCESSED)
+	MMU_MAP(#0x80000000,#0,#0x02000000,#_PAGE_NOCACHE030+_PAGE_ACCESSED)
 	
 	jbra	mmu_init_done
 	
@@ -812,27 +809,31 @@
    area.
 */
 
-	lea	%pc@(SYMBOL_NAME(is_medusa)),%a0
-	movel	%a0@,%d3
-	bne     1f
-	lea	%pc@(SYMBOL_NAME(is_hades)),%a0
-	movel   %a0@,%d3
-1:
+	/* I/O base addr for non-Medusa, non-Hades: 0x00000000 */
+	moveq	#0,%d0
+	movel	%pc@(SYMBOL_NAME(atari_mch_type)),%d3
+	cmpl	#ATARI_MACH_MEDUSA,%d3
+	jbeq	2f
+	cmpl	#ATARI_MACH_HADES,%d3
+	jbne	1f
+2:	movel	#0xff000000,%d0 /* Medusa/Hades base addr: 0xff000000 */
+1:	movel	%d0,%d3
+
 	is_040_or_060(Lspata68040)
 
-	MMU_MAP(#0xff000000,%d3,#0x00f00000,#0)
-	addl	#0x00f00000,%d3
-	MMU_MAP(#0xfff00000,%d3,#0x00040000,#_PAGE_NOCACHE030)
-	addl	#0x00040000,%d3
-	MMU_MAP(#0xfff40000,%d3,#0x00080000,#0)
-	addl	#0x00080000,%d3
-	MMU_MAP(#0xfffc0000,%d3,#0x00040000,#_PAGE_NOCACHE030)
+	/* Map everything non-cacheable, though not all parts really
+	 * need to disable caches (crucial only for 0xffc000..0xffffff
+	 * (standard I/O) and 0xf00000..0xf3ffff (IDE)). The remainder
+	 * isn't really used, except for sometimes peeking into the
+	 * ROMs (mirror at phys. 0x0), so caching isn't necessary for
+	 * this. */
+	MMU_MAP(#0xff000000,%d3,#0x01000000,#_PAGE_NOCACHE030+_PAGE_ACCESSED)
 
 	jbra	mmu_init_done
 	
 Lspata68040:
 	
-	MMU_MAP(#0xFF000000,%d3,#0x01000000,#_PAGE_GLOBAL040+_PAGE_NOCACHE_S)
+	MMU_MAP(#0xff000000,%d3,#0x01000000,#_PAGE_GLOBAL040+_PAGE_NOCACHE_S+_PAGE_ACCESSED)
 	
 	jbra	mmu_init_done
 
@@ -874,7 +875,7 @@
 	/* Some something or other.  Richard, what the hell is this range? */
 	/* And how large is it? */
 	
-	MMU_MAP_TT(#0xe0000000,#0x20000000,#_PAGE_NOCACHE_S)
+	MMU_MAP_TT(#0xe0000000,#0x20000000,#_PAGE_NOCACHE_S+_PAGE_ACCESSED)
 	
 #if 0
 	/* This is essentially the same as the above ... */
@@ -901,7 +902,7 @@
 	 * clash with User code virtual address space.
 	 */
 
-	MMU_MAP_TT(#0xe0000000,#0x20000000,#_PAGE_NOCACHE_S)
+	MMU_MAP_TT(#0xe0000000,#0x20000000,#_PAGE_NOCACHE_S+_PAGE_ACCESSED)
 	
 #if 0
 	/*
@@ -961,10 +962,10 @@
 
 	is_not_040_or_060(1f)
 	
-	movel	#_PAGE_GLOBAL040+_PAGE_NOCACHE_S,MAP_CACHE
+	movel	#_PAGE_GLOBAL040+_PAGE_NOCACHE_S+_PAGE_ACCESSED,MAP_CACHE
 	jbra	2f
 1:		
-	movel	#_PAGE_NOCACHE030,MAP_CACHE
+	movel	#_PAGE_NOCACHE030+_PAGE_ACCESSED,MAP_CACHE
 2:	
 	/*
 	 * Mac Note: screen address of logical 0xF000.0000 -> <screen physical>
@@ -1011,11 +1012,15 @@
  * mmu_fixup
  * 
  * On the 040 class machines, all pages that are used for the
- * mmu have to be fixed up.  So, we'll walk through the MMU
- * table to determine which pages were allocated.  An alternative
- * would be to "know" what pages were allocated above.  But that's
- * fraught with maintenance problems.  It's easier to walk the
- * table.
+ * mmu have to be fixed up. According to Motorola, pages holding mmu
+ * tables should be non-cacheable on a '040 and write-through on a
+ * '060. But analysis of the reasons for this, and practical
+ * experience, showed that write-through also works on a '040.
+ *
+ * So, we'll walk through the MMU table to determine which pages were
+ * allocated.  An alternative would be to "know" what pages were
+ * allocated above.  But that's fraught with maintenance problems.
+ * It's easier to walk the table.
  * 
  */
 #if defined(CONFIG_M68040) || defined(CONFIG_M68060)
@@ -1037,7 +1042,7 @@
 1:	
 	movel	%a5@(%d5*4),%d2
 	movel	%d2,%d0
-	andb	#_PAGE_SHORT,%d0
+	andb	#_PAGE_TABLE,%d0
 	jbeq	4f
 
 	movel	%d2,%a0
@@ -1050,7 +1055,7 @@
 2:
 	movel	%a4@(%d4*4),%d2
 	movel	%d2,%d0
-	andb	#_PAGE_SHORT,%d0
+	andb	#_PAGE_TABLE,%d0
 	jbeq	3f
 
 	movel	%d2,%a0
@@ -1072,20 +1077,20 @@
 	/* Calculate the offset in the root table 
 	 */
 	movel	%a0,%d5
-	andil	#0xFE000000,%d5
+	andil	#0xfe000000,%d5
 	roll	#7,%d5
 
 	/* Calculate the offset in the pointer table 
 	 */
 	movel	%a0,%d4
-	andil	#0x01fC0000,%d4
+	andil	#0x01fc0000,%d4
 	lsrl	#2,%d4
 	swap	%d4
 	
 	/* Calculate the offset in the page table
 	 */
 	movel	%a0,%d3
-	andil	#0x0002F000,%d3
+	andil	#0x0002f000,%d3
 	lsll	#4,%d3
 	swap	%d3
 
@@ -1224,7 +1229,9 @@
 	lea	mmu_engage_040_disable_itt0,%a0
 	
 	orw	#TTR_ENABLE+TTR_KERNELMODE+_PAGE_NOCACHE_S,%d0
-	.long	0x4e7b0004	/* movec %d0,%itt0 */
+	.chip	68040
+	movec	%d0,%itt0
+	.chip	68k
 	jbra	2f
 1:
 #endif
@@ -1234,7 +1241,9 @@
 	orw	#TTR_ENABLE+TTR_CI+TTR_RWM+TTR_FCB2+TTR_FCM1+TTR_FCM0,%d0
 	lea	%pc@(Lmmu),%a3
 	movel	%d0,%a3@
-	.long	0xf0130800	/* pmove %a3@,%tt0 */
+	.chip	68030
+	pmove	%a3@,%tt0
+	.chip	68k
 #endif
 
 	/*
@@ -1256,6 +1265,10 @@
 	 * at 0x8000.0000 and do all the right stuff magic.
 	 * This bites.  -- rst
 	 */
+
+#define TMP_MAP		0x80000000
+#define TMP_MAP_OFFS	(TMP_MAP>>(ROOT_INDEX_SHIFT-2))
+
 	/*
 	 * Build a really small Ptr table at %d5
 	 */
@@ -1263,20 +1276,21 @@
 	jbsr	mmu_clear_pointer_table
 	
 	movel	%d5,%d0
-	orw	#0x0102,%d0
+	addl	#0x100,%d0
+	orw	#_PAGE_TABLE+_PAGE_ACCESSED,%d0
 	movel	%d0,%a4@
 	
 	/*
 	 * Build a really small Page table at %d5 + 0x100
-	 * (Maps the first 8K of the kernel @ 0x8000.0000)
+	 * (Maps the first 16K of the kernel @ 0x8000.0000)
 	 */
 	movel	%d5,%a3
 	addaw	#0x100,%a3
 	jbsr	mmu_clear_page_table
 	
-	movel	#0x00001000,%d1
+	movel	#PAGESIZE,%d1
 	movel	%d5,%d0
-	addql	#1,%d0		/* essentially 'OR' */
+	orw	#_PAGE_PRESENT+_PAGE_ACCESSED,%d0
 	movel	%d0,%a3@+
 	addl	%d1,%d0
 	movel	%d0,%a3@+
@@ -1288,21 +1302,22 @@
 	/*
 	 * Alter the Root table to use our really small entries
 	 */
-	movel	%a5@(0x100),%d2		/* save entry */
+	lea	%a5@(TMP_MAP_OFFS),%a0
+	movel	%a0@,%d2		/* save entry */
 	movel	%d5,%d0
-	addql	#2,%d0
-	movel	%d0,%a5@(0x100)		/* insert temp. entry */
+	orw	#_PAGE_TABLE+_PAGE_ACCESSED,%d0
+	movel	%d0,%a0@		/* insert temp. entry */
 
 #if defined(CONFIG_M68040) || defined(CONFIG_M68060)
 	is_not_040_or_060(1f)
 	lea	mmu_engage_040_disable_8000,%a0
-	addal	#0x80000000,%a0
+	addal	#TMP_MAP,%a0
 	jbra	mmu_engage_core
 1:
 #endif
 #if defined(CONFIG_M68020) || defined(CONFIG_M68030)
 	lea	mmu_engage_030_disable_8000,%a0
-	addal	#0x80000000,%a0
+	addal	#TMP_MAP,%a0
 #endif
 	
 mmu_engage_core:
@@ -1310,19 +1325,23 @@
 #if defined(CONFIG_M68040) || defined(CONFIG_M68060)
 	is_not_040_or_060(2f)
 
-mmu_engage_040:		
-	.word	0xf4d8		/* CINVA I/D    */
-	.word	0xf518		/* pflusha	*/
-	.long	0x4e7bd807	/* movec a5,srp */
-	.long	0x4e7bd806	/* movec a5,urp */
+mmu_engage_040:
+	.chip	68040
+	nop
+	cinva	%bc
+	nop
+	pflusha
+	nop
+	movec	a5,srp
+	movec	a5,urp
 	movel	#TC_ENABLE+TC_PAGE4K,%d0
-	.long	0x4e7b0003	/* movec d0,tc  (enable the MMU) */
+	movec	d0,tc		/* enable the MMU */
 	lea	SYMBOL_NAME(kernel_pg_dir),%a5
 	jmp	%a0@		/* Go to clean up code */
 
 mmu_engage_040_disable_itt0:
 	moveq	#0,%d0
-	.long	0x4e7b0004	/* movec %d0,%itt0 */
+	movec	%d0,%itt0
 	jmp	mmu_engage_done:w
 
 mmu_engage_040_disable_8000:
@@ -1330,47 +1349,55 @@
 	 * This code is running at 0x8000.0000+ right now
 	 */
 	moveq	#0,%d0
-	.long	0x4e7b0004	/* movec %d0,%itt0 */
-	jmp	1f:w		/* Jump down into logical space into the kernel! */
+	movec	%d0,%itt0
+	jmp	1f:w	/* Jump down into logical space into the kernel! */
 1:
 	/* Now we're back on the ground! */
-	movel	%d2,%a5@(0x100)	/* Restore the old 0x8000.0000 mapping */
-	.word	0xf518		/* pflusha */
+	movel	%d2,%a5@(TMP_MAP_OFFS) /* Restore the old 0x8000.0000 mapping */
+	nop
+	pflusha
+	nop
 	jmp	mmu_engage_done:w
+	.chip	68k
 
 2:
 #endif
 
 #if defined(CONFIG_M68020) || defined(CONFIG_M68030)
 mmu_engage_030:
-
+	.chip	68030
 	lea	%pc@(Lmmu),%a3
 	movel	#0x80000002,%a3@
 	movel	%a5,%a3@(4)
 	movel	#0x0808,%d1
-	.long	0x4e7b1002	/* movec %d1,%cacr */
-	.long	0xf0134800	/* pmove %a3@,%srp */
-	.long	0xf0134c00	/* pmove %a3@,%crp */
-	.long	0xf0002400	/* pflusha */
+	movec	%d1,%cacr
+	pmove	%a3@,%srp
+	pmove	%a3@,%crp
+	pflusha
+	/*
+	 * enable,super root enable,4096 byte pages,7 bit root index,
+	 * 7 bit pointer index, 6 bit page table index.
+	 */
 	movel	#0x82c07760,%a3@
-	.long	0xf0134000	/* pmove %a3@,%tc (enable the MMU) */
+	pmove	%a3@,%tc	/* enable the MMU */
 	lea	SYMBOL_NAME(kernel_pg_dir),%a5
 	jmp	%a0@		/* Go to the appropriate clean up code */
 
 mmu_engage_030_disable_tt0:
 	clrl	%a3@
-	.long	0xf0130800	/* pmove %a3@,%tt0 */
+	pmove	%a3@,%tt0
 	jmp	mmu_engage_done:w
 	
 mmu_engage_030_disable_8000:
 	clrl	%a3@
-	.long	0xf0130800	/* pmove %a3@,%tt0 */
-	jmp	1f:w		/* Jump down into logical space into the kernel! */
+	pmove	%a3@,%tt0
+	jmp	1f:w	/* Jump down into logical space into the kernel! */
 1:
 	/* Now we're back on the ground! */
-	movel	%d2,%a5@(0x100)	/* Restore the old 0x8000.0000 mapping */
-	.long	0xf0002400	/* pflusha */
+	movel	%d2,%a5@(TMP_MAP_OFFS)	/* Restore the old 0x8000.0000 mapping */
+	pflusha
 	jmp	mmu_engage_done:w
+	.chip	68k
 #endif
 	
 mmu_engage_done:
@@ -1404,8 +1431,11 @@
 	is_not_040_or_060(Lcache_not_680460)
 
 Lcache680460:
-	.word	0xf4f8		/* cpusha %bc */
-
+	.chip	68040
+	nop
+	cpusha	%bc
+	nop
+	
 	is_060(Lcache68060)
 
 	movel	#CC6_ENABLE_D+CC6_ENABLE_I,%d0
@@ -1419,18 +1449,21 @@
 	movec	%d0,%cacr
 	/* enable superscalar dispatch in PCR */
 	moveq	#1,%d0
-	.long	0x4e7b0808	/* movec d0,pcr */
+	.chip	68060
+	movec	d0,pcr
 
 	jbra	Lcache_done
 Lcache_not_680460:
 #endif
 #if defined(CONFIG_M68020) || defined(CONFIG_M68030)
 Lcache68030:
+	.chip	68030
 	movel	#CC3_ENABLE_DB+CC3_CLR_D+CC3_ENABLE_D+CC3_ENABLE_IB+CC3_CLR_I+CC3_ENABLE_I,%d0
 	movec	%d0,%cacr
 
 	jra	Lcache_done
 #endif
+	.chip	68k
 Lcache_done:
 
 	putc_trace('P')
@@ -1478,6 +1511,7 @@
 /* jump to the kernel start */
 	putr()
 
+	subl	%a6,%a6		/* clear a6 for gdb */
 	jbsr	SYMBOL_NAME(start_kernel)
 
 /*
@@ -2063,20 +2097,20 @@
 	/* Calculate the offset in the root table 
 	 */
 	movel	MAP_LOG,%d5
-	andil	#0xFE000000,%d5
+	andil	#0xfe000000,%d5
 	roll	#7,%d5
 
 	/* Calculate the offset in the pointer table 
 	 */
 	movel	MAP_LOG,%d4
-	andil	#0x01fC0000,%d4
+	andil	#0x01fc0000,%d4
 	lsrl	#2,%d4
 	swap	%d4
 	
 	/* Calculate the offset in the page table (used on 040's + 060's)
 	 */
 	movel	MAP_LOG,%d3
-	andil	#0x0002F000,%d3
+	andil	#0x0002f000,%d3
 	lsll	#4,%d3
 	swap	%d3
 
@@ -2100,7 +2134,7 @@
 	/* If there is an entry already, we must use it
 	 * to preserve existing MMU mapping data!
 	 */
-	andil	#0xFFFFFE00,%d2
+	andil	#_TABLE_MASK,%d2
 	movel	%d2,%a4
 	jbra	3f
 	
@@ -2108,7 +2142,7 @@
 1:
 	/* Should we get a pointer table from memory on behalf of the caller?
 	 */
-	cmpl	#0x00000000,%a4
+	tstl	%a4
 	jbne	2f
 
 	jbsr	mmu_get_pointer_table
@@ -2117,7 +2151,7 @@
 	/* Put the pointer table into the root table
 	 */	
 	movel	%a4,%d2
-	oril	#_PAGE_TABLE,%d2
+	orw	#_PAGE_TABLE+_PAGE_ACCESSED,%d2
 	movel	%d2,%a5@(%d5*4)
 
 3:
@@ -2129,7 +2163,6 @@
 	
 #endif /* CONFIG_M68020 || CONFIG_M68030 */
 
-#if defined(CONFIG_M68040) || defined(CONFIG_M68060) || defined(CONFIG_MAC)
 /*
  *	MMU 040 & 060 Support
  *
@@ -2184,13 +2217,14 @@
 
 	/* Convert address range length into # of pages
 	 */
-	divul	#PAGESIZE,%d0
+	movel	#PAGESHIFT,%d2
+	lsrl	%d2,MAP_LENGTH
 
 mmu_040_loop:
 	/* See if there is an existing page table pointer to use
 	 */
 	movel	%a4@(%d4*4),%d2
-	andil	#0xFFFFFF00,%d2
+	andil	#_TABLE_MASK,%d2
 	movel	%d2,%a3
 	tstl	%a3
 	jbne	mmu_fill_040_pagetable
@@ -2204,7 +2238,7 @@
 
 	/* Decrement page count
 	 */
-	subq	#1,%d0
+	subq	#1,MAP_LENGTH
 	jbeq	mmu_map_done
 
 	/* Increase mapping addresses
@@ -2231,7 +2265,6 @@
 	clrl	%d4	/* %d4 is ptr table index */
 	
 	jbra	mmu_040_loop
-#endif /* CONFIG_M68040 || CONFIG_M68060 || CONFIG_MAC */
 
 /*
  * mmu_map_revert
@@ -2266,8 +2299,7 @@
 	jbne	mmu_map_revert
 
 	movel	MAP_PHYS,%d2
-	andil	#0xFF000000,%d2
-	cmpl	%d2,MAP_PHYS
+	andil	#0x00ffffff,%d2
 	jbne	mmu_map_revert
 
 	/* Length must be a power of two
@@ -2290,8 +2322,10 @@
 	oriw	#0xa000,%d2	/* Enable | Supervisor Only */
 	orb	MAP_CACHE,%d2
 
-	.long	0x4e7b2005	/* movec %d2,%itt1 */
-	.long	0x4e7b2007	/* movec %d2,%dtt1 */
+	.chip	68040
+	movec	%d2,%itt1
+	movec	%d2,%dtt1
+	.chip	68k
 
 	moveml	%sp@+,%d0-%d7/%a0-%a4
 	rts
@@ -2303,9 +2337,7 @@
 	 * If not a multiple of 256K, use page descriptors.
 	 */
 	movel	MAP_LENGTH,%d2
-	divul	#PAGE_TABLE_SIZE*PAGESIZE,%d2
-	mulul	#PAGE_TABLE_SIZE*PAGESIZE,%d2
-	cmpl	%d2,MAP_LENGTH
+	andl	#(PAGE_TABLE_SIZE*PAGESIZE)-1,%d2
 	jbne	mmu_map_040
 
 	/* Enhance the MMU mode to make an early terminating descriptor
@@ -2317,9 +2349,10 @@
 
 	/* Convert # of pages into # of 256K entries
 	 */
-	divul	#PAGE_TABLE_SIZE*PAGESIZE,MAP_LENGTH
+	movel	#PTR_INDEX_SHIFT,%d2
+	lsrl	%d2,MAP_LENGTH
 
-	cmpil	#PTR_TABLE_SIZE,%d0
+	cmpil	#PTR_TABLE_SIZE,MAP_LENGTH
 	jbcc	mmu_030_root_loop
 		
 	/* Since %a5 and %a4 point to a valid root table and
@@ -2360,9 +2393,9 @@
 
 	/* Decrement number of 32M chunks to map
 	 */
-	subw	#PTR_TABLE_SIZE,MAP_LENGTH
+	subl	#PTR_TABLE_SIZE,MAP_LENGTH
 	jbeq	mmu_map_done
-	cmp	#PTR_TABLE_SIZE,MAP_LENGTH
+	cmpl	#PTR_TABLE_SIZE,MAP_LENGTH
 	jbcs	mmu_030_ptr_loop
 
 	/* Increment mapping addresses
@@ -2454,8 +2487,12 @@
 	movel	%d2,%a3
 	addil	#PAGE_TABLE_SIZE*4,%d2
 	movel	%d2,%a2@
-	
-	andil	#0x00000F00,%d2
+
+	/* Basically this is (PAGESIZE-1)-(PAGE_TABLE_SIZE*4-1), but
+	 * the two -1 can be eliminated.
+	 * The condition is true if the current page table is at the
+	 * start of the next page. */
+	andil	#PAGESIZE-PAGE_TABLE_SIZE*4,%d2
 	jbne	2f
 
 	/* Get the page table from above the kernel memory
@@ -2474,7 +2511,7 @@
 	/* Log this page table (%a3) in the pointer table (%a4)
 	 */
 	movel	%a3,%d2
-	orib	#2,%d2	/* enable this pointer table entry */
+	orw	#_PAGE_TABLE+_PAGE_ACCESSED,%d2
 	movel	%d2,%a4@(%d4*4)
 	
 	moveml	%sp@+,%a0-%a2/%d0-%d2
@@ -2516,7 +2553,7 @@
 
 	/* Did we just hand out the last ptr table in the cache page?
 	 */
-	andil	#0x00000E00,%d2
+	andil	#PAGESIZE-PTR_TABLE_SIZE*4,%d2
 	jbne	2f
 
 	/* Get a new cache-of-ptr-tables page from above the kernel memory
@@ -2529,7 +2566,7 @@
 	/* Log this pointer table (%a4) in the root table (%a5)
 	 */
 	movel	%a4,%d2
-	orib	#2,%d2	/* enable this pointer table entry */
+	orw	#_PAGE_TABLE+_PAGE_ACCESSED,%d2
 	movel	%d2,%a5@(%d5*4)
 	
 	moveml	%sp@+,%a0-%a2/%d0-%d2
@@ -2562,27 +2599,12 @@
 
 #if defined(CONFIG_ATARI)
 /* #define USE_PRINTER */
-/* #define USE_SCC */
-#define USE_SCC_A
-/* #define USE_MFP */
-
-#if defined(USE_PRINTER)
-
-LPSG_SELECT	= 0xff8800
-LPSG_READ	= 0xff8800
-LPSG_WRITE	= 0xff8802
-LPSG_IO_A	= 14
-LPSG_IO_B	= 15
-LPSG_CONTROL	= 7
-LSTMFP_GPIP	= 0xfffa01
-LSTMFP_DDR	= 0xfffa05
-LSTMFP_IERB	= 0xfffa09
-
-#elif defined(USE_SCC)
- 
-LSCC_CTRL_B	= 0xff8c85
-LSCC_DATA_B	= 0xff8c87
+/* #define USE_SCC_B */
+/* #define USE_SCC_A */
+#define USE_MFP
 
+#if defined(USE_SCC_A) || defined(USE_SCC_B)
+#define USE_SCC
 /* Initialisation table for SCC */
 scc_initable:
 	.byte	9,12		/* Reset */
@@ -2598,11 +2620,29 @@
 	.byte	5,0xea		/* enable transmitter */
 	.byte	-1
 	.even
+#endif
+
+#if defined(USE_PRINTER)
+
+LPSG_SELECT	= 0xff8800
+LPSG_READ	= 0xff8800
+LPSG_WRITE	= 0xff8802
+LPSG_IO_A	= 14
+LPSG_IO_B	= 15
+LPSG_CONTROL	= 7
+LSTMFP_GPIP	= 0xfffa01
+LSTMFP_DDR	= 0xfffa05
+LSTMFP_IERB	= 0xfffa09
+
+#elif defined(USE_SCC_B)
+ 
+LSCC_CTRL	= 0xff8c85
+LSCC_DATA	= 0xff8c87
 
 #elif defined(USE_SCC_A)
 
-LSCC_CTRL_A	= 0xff8c81
-LSCC_DATA_A	= 0xff8c83
+LSCC_CTRL	= 0xff8c81
+LSCC_DATA	= 0xff8c83
 
 /* Initialisation table for SCC */
 scc_initable:
@@ -2662,28 +2702,20 @@
 #endif
 #if defined(CONFIG_ATARI)
 	is_not_atari(4f)
+	movel	%pc@(Liobase),%a1
 #if defined(USE_PRINTER)
-	bclr	#0,%pc@(LSTMFP_IERB)
-	bclr	#0,%pc@(LSTMFP_DDR)
-	moveb	#LPSG_CONTROL,%pc@(LPSG_SELECT)
-	moveb	#0xff,%pc@(LPSG_WRITE)
-	moveb	#LPSG_IO_B,%pc@(LPSG_SELECT)
-	clrb	%pc@(LPSG_WRITE)
-	moveb	#LPSG_IO_A,%pc@(LPSG_SELECT)
-	moveb	%pc@(LPSG_READ),%d0
+	bclr	#0,%a1@(LSTMFP_IERB)
+	bclr	#0,%a1@(LSTMFP_DDR)
+	moveb	#LPSG_CONTROL,%a1@(LPSG_SELECT)
+	moveb	#0xff,%a1@(LPSG_WRITE)
+	moveb	#LPSG_IO_B,%a1@(LPSG_SELECT)
+	clrb	%a1@(LPSG_WRITE)
+	moveb	#LPSG_IO_A,%a1@(LPSG_SELECT)
+	moveb	%a1@(LPSG_READ),%d0
 	bset	#5,%d0
-	moveb	%d0,%pc@(LPSG_WRITE)
+	moveb	%d0,%a1@(LPSG_WRITE)
 #elif defined(USE_SCC)
-	lea	LSCC_CTRL_B,%a0
-	lea	%pc@(scc_initable:w),%a1
-2:	moveb	%a1@+,%d0
-	jmi	3f
-	moveb	%d0,%a0@
-	moveb	%a1@+,%a0@
-	jra	2b
-3:	clrb	%a0@
-#elif defined(USE_SCC_A)
-	lea	LSCC_CTRL_A,%a0
+	lea	%a1@(LSCC_CTRL),%a0
 	lea	%pc@(scc_initable:w),%a1
 2:	moveb	%a1@+,%d0
 	jmi	3f
@@ -2692,12 +2724,12 @@
 	jra	2b
 3:	clrb	%a0@
 #elif defined(USE_MFP)
-	bclr	#1,%pc@(LMFP_TSR)
-	moveb   #0x88,%pc@(LMFP_UCR)
-	andb	#0x70,%pc@(LMFP_TDCDR)
-	moveb   #2,%pc@(LMFP_TDDR)
-	orb	#1,%pc@(LMFP_TDCDR)
-	bset	#1,%pc@(LMFP_TSR)
+	bclr	#1,%a1@(LMFP_TSR)
+	moveb   #0x88,%a1@(LMFP_UCR)
+	andb	#0x70,%a1@(LMFP_TDCDR)
+	moveb   #2,%a1@(LMFP_TDDR)
+	orb	#1,%a1@(LMFP_TDCDR)
+	bset	#1,%a1@(LMFP_TSR)
 #endif
 4:	
 #endif
@@ -2788,13 +2820,9 @@
 	bset	#5,%d7
 	moveb	%d7,%a1@(LPSG_WRITE)
 #elif defined(USE_SCC)
-3:	btst	#2,%a1@(LSCC_CTRL_B)
-	jeq	3b
-	moveb	%d7,%a1@(LSCC_DATA_B)
-#elif defined(USE_SCC_A)
-3:	btst	#2,%a1@(LSCC_CTRL_A)
+3:	btst	#2,%a1@(LSCC_CTRL)
 	jeq	3b
-	moveb	%d7,%a1@(LSCC_DATA_A)
+	moveb	%d7,%a1@(LSCC_DATA)
 #elif defined(USE_MFP)
 3:	btst	#7,%a1@(LMFP_TSR)
 	jeq	3b
@@ -3482,10 +3510,8 @@
 #endif
 
 #if defined(CONFIG_ATARI)
-SYMBOL_NAME_LABEL(is_medusa)
-	.long 0
-SYMBOL_NAME_LABEL(is_hades)
-	.long 0
+SYMBOL_NAME_LABEL(atari_mch_type)
+	 .long 0
 #endif
 
 #if defined(CONFIG_MAC)
