;
;       Cache handling routines
;
;   Support routines used by startup code and RLM
;
;   MUST be in supervisor mode when called.
;   a6 MUST contain address of System variables.
;
;   $Source$
;   $Revision$
;
;   AMENDMENT HISTORY
;   ~~~~~~~~~~~~~~~~~
;   $Log$
;-------------------------------------------------------------------------
    .text

SYS_PRCS  equ   $a1                 ; PRoCeSsor on which system is running
SY.68020  equ   $20
SY.68040  equ   $40

;=======================================================================
;  Cache disable    (68030 and 68040)
;
;  For processors with caching we will switch the caching off while
;  we do the program relocation.   Whether this is strictly speaking
;  necessary is depends on exactly what type of caching is in use,
;  (but it is better to be safe than sorry!).
;
;  The code for disabling caching in the QXL uses some system variables
;  to control this cacheing.   These are actually ignored here so that
;  there is no dependency on SMSQ - but the instructions are added as
;  comment for those who are interested.
;-----------------------------------------------------------------------

CACHEOFF:
    moveq   #0,d0               ; clear d0 (for 68000, 68008 and 68010)
    cmpi.b  #SY.68020,SYS_PRCS(a6) ; Do we have an 68030 or higher ?
    bls     CACHEOFF_END        ; ... NO, just go to relocate  code

;   Actually do cache disable

    dc.l    $4e7a0002
;   MOVEC   CACR,d0             ; save current CACR reg
    move.l  #$00000808,d1       ; 68030: ensure Enable bits cleared
                                ;       bit 0 (Enable Instruction Cache)
                                ;       bit 8 (Enable Data cache)
                                ; 68030: ensure clear bits set
                                ;       bit 3   (Clear Instruction Cache)
                                ;       bit 11  (Clear Data cache)
                                ; 68040: ensure cache enable bits cleared
                                ;       bit 15 (Enable Instruction cache)
                                ;       bit 31 (Enable Data cache)
    dc.l    $4e7b1002
;   MOVEC   D1,CACR             ; set cache register
CACHEOFF_END:
    rts


;====================================================================
;  Cache enable     (68030 and 68040)
;
;   We have now done the relocation, so need to reinstate any caching.
;   If we switched caching off earlier, then we need to put it back
;   on again - back to the state before we disabled it.
;
;  The code for enabling 68040 caching is based on that used by SMSQ
;  in the QXL card.
;
;   Assumptions at this point:
;       In Supervisor mode
;       d0  Contains old cache register value
;       A6  System Variables
;-------------------------------------------------------------------

CACHEON:
    tst.l   d0                      ; Was cache on anyway ?
    beq     CACHEON_END             ; ... NO, then continue
    cmpi.b  #SY.68040,SYS_PRCS(a6)  ; 68040 ?
    blt     CACHEON_030             ; ... NO, then simpler re-enable
CACHEON_040:
    dc.w    $f4d8
;   CINVA BC                        ; Invalidate caches
CACHEON_030:
    dc.l    $4e7b0002           
;   MOVEC d0,CACR                   ; Set Cache control reg

CACHEON_END:
    rts

