Top 9.47 KB
Newer Older
Jeffrey Lee's avatar
Jeffrey Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
; Copyright 2009 Castle Technology Ltd
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
;     http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
;
15 16 17 18

        GET     Hdr:ListOpts
        GET     Hdr:Macros
        GET     Hdr:System
Jeffrey Lee's avatar
Jeffrey Lee committed
19 20 21 22 23
        GET     Hdr:Machine.<Machine>
        GET     Hdr:ImageSize.<ImageSize>

        GET     Hdr:MEMM.ARM600

24 25 26 27
        GET     Hdr:Proc
        GET     Hdr:OSEntries
        GET     Hdr:HALEntries

Jeffrey Lee's avatar
Jeffrey Lee committed
28
        GET     hdr.omap3530
29
        GET     hdr.StaticWS
Jeffrey Lee's avatar
Jeffrey Lee committed
30 31 32
        GET     hdr.UART
        GET     hdr.Post
        GET     hdr.SDRC
33
        GET     hdr.Copro15ops
34
        GET     hdr.GPIO
Jeffrey Lee's avatar
Jeffrey Lee committed
35

36 37
        AREA    |!!!ROMStart|, CODE, READONLY, PIC

Jeffrey Lee's avatar
Jeffrey Lee committed
38 39
        ; Note: there must be only one fixed link from "Top" to "Boot" at +&2000
        IMPORT  rom_checkedout_ok
40
        EXPORT  HAL_Base
Jeffrey Lee's avatar
Jeffrey Lee committed
41
        IMPORT  HAL_DebugTX
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

HAL_Base

        B       start
        B       .
        B       .
        B       .
        B       .
        B       .
        B       .
        B       .
        DCD     1024*1024       ; image size (temp hack)

        ASSERT  . - HAL_Base < 0x60
        %       0x60 - (. - HAL_Base)
ROMsize
        DCD     0                               ; patched in by build system

        ENTRY
start
62 63
        DebugChar  a1,a2,65
        DebugChar  a1,a2,48
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
;        MVN     a1, #0
;        BL      hexnumber
;        MVN     a1, #0
;        BL      hexnumber
;        MRS     a1, CPSR ; CPSR 200001d3 - SVC32 FIQ IRQ ImpAbrt
;        BL      hexnumber
;        MRC     p15, 0, a1, c1, c0, 0 ; control register 00c5187a - IZA
;        BL      hexnumber
;        MRC     p15, 0, a1, c1, c0, 1 ; aux control register 00000042 - IBE L2EN
;        BL      hexnumber
;        MRC     p15, 1, a1, c0, c0, 1 ; cache level id register 0a000023 - OK
;        BL      hexnumber
;        MRC     p15, 0, a1, c1, c1, 2 ; nonsecure access control register 00070c00 - PLE, TLB, CL, CP10, CP11 available
;        BL      hexnumber
;        MRC     p15, 0, a1, c1, c0, 2 ; coprocessor access control register 00000000 - all disabled
;        BL      hexnumber
Jeffrey Lee's avatar
Jeffrey Lee committed
80 81
        B       restart

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
;hexnumber ; Output a1 in hex to the serial port
;        LDR     a2, =L4_UART3
;        ADR     a3, hextab
;        MOV     a4, #8
;10
;        LDRB    v1, [a2, #UART_LSR]
;        TST     v1, #THRE
;        BEQ     %BT10
;        LDRB    v1, [a3, a1, LSR #28]
;        STRB    v1, [a2, #UART_THR]
;        MOV     a1, a1, LSL #4
;        SUBS    a4, a4, #1
;        BNE     %BT10
;20
;        LDRB    v1, [a2, #UART_LSR]
;        TST     v1, #THRE
;        BEQ     %BT20
;        MOV     v1, #13
;        STRB    v1, [a2, #UART_THR]
;30
;        LDRB    v1, [a2, #UART_LSR]
;        TST     v1, #THRE
;        BEQ     %BT30
;        MOV     v1, #10
;        STRB    v1, [a2, #UART_THR]
;
;        MOV     pc, lr
;
;hextab  DCB "0123456789abcdef"

Jeffrey Lee's avatar
Jeffrey Lee committed
112 113 114 115 116 117 118 119 120 121
        LTORG

        MACRO
        CallOSM $entry, $reg
        LDR     ip, [v8, #$entry*4]
        MOV     lr, pc
        ADD     pc, v8, ip
        MEND

; ------------------------------------------------------------------------------
122 123
; Perform some Cortex-A8 specific CPU setup
; Then start looking for RAM
Jeffrey Lee's avatar
Jeffrey Lee committed
124
restart
125
        DebugChar a3,a4,48
Jeffrey Lee's avatar
Jeffrey Lee committed
126
        MSR     CPSR_c, #F32_bit+I32_bit+SVC32_mode
127
        DebugChar a3,a4,49
128
        MOV     a1, #&78 ; Matches MMUC_L+MMUC_D+MMUC_P+MMUC_W, which are now reserved bits in the Cortex spec, but should be written as 1
129 130 131 132 133 134 135
        MCR     p15, 0, a1, c1, c0, 0              ; MMU, caches etc off
        DebugChar a3,a4,50
        MRC     p15, 0, a1, c1, c0, 1
        BIC     a1, a1, #2
        MCR     p15, 0, a1, c1, c0, 1 ; Ensure L2 cache disabled
        DebugChar a3,a4,51

Jeffrey Lee's avatar
Jeffrey Lee committed
136
        MOV     a1, #0
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        MCR     p15, 0, a1, c7, c5, 0           ; invalidate instruction cache
        DebugChar a3,a4,52
        MCR     p15, 0, a1, c8, c7, 0           ; invalidate TLBs
        DebugChar a3,a4,53

        ; Invalidate data caches. To do this we must query their sizes
        ; The register describes up to 8 cache levels, so to provide some future-proofing I might as well implement a full cache clean routine here
        MRC     p15, 1, a1, c0, c0, 1 ; Cache level ID register
        DebugChar a3,a4,54
        BIC     a1, a1, #&FF000000 ; Discard unification/coherency bits
        MOV     a2, #0 ; Current cache level
20
        DebugChar a3,a4,49
        ANDS    a3, a1, #7 ; Get flags
        BEQ     %FT10 ; Cache clean complete
        MCR     p15, 2, a2, c0, c0, 0 ; Program cache size selection register
        MRC     p15, 1, a3, c0, c0, 0 ; Get size info
        AND     v1, a3, #&7 ; log2(Line size)-2
        BIC     a3, a3, #&F0000007 ; Clear flags & line size
        MOV     v2, a3, LSL #19 ; Number of ways-1 in upper 10 bits
Jeffrey Lee's avatar
Jeffrey Lee committed
157
        MOV     v3, a3, LSR #13 ; Number of sets-1 in lower 15 bits
158 159 160
        ; Way number needs to be packed right up at the high end of the data word; shift it up
        CLZ     a4, v2
        MOV     v2, v2, LSL a4
Jeffrey Lee's avatar
Jeffrey Lee committed
161 162 163
        ; Set number needs to start at log2(Line size)+2
        MOV     v3, v3, LSL #4 ; Start at bit 4
        MOV     v3, v3, LSL v1 ; Start at log2(Line size)+2
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        ; Now calculate the offset numbers we will use to increment sets & ways
        BIC     v4, v2, v2, LSL #1 ; Way increment
        BIC     v5, v3, v3, LSL #1 ; Set increment
        ; Now we can finally clean this cache!
        ORR     a3, a2, v3 ; Current way (0), set (max), and level
30
        MCR     p15, 0, a3, c7, c6, 2 ; Invalidate
        ADDS    a3, a3, v4 ; Increment way
        BCC     %BT30 ; Overflow will occur once ways are enumerated
        TST     a3, v3 ; Are set bits all zero?
        SUBNE   a3, a3, v5 ; No, so decrement set and loop around again
        BNE     %BT30
        ; This cache is now clean. Move on to the next level.
        DebugChar a3,a4,50
        ADD     a2, a2, #2
        MOVS    a1, a1, LSR #3
        BNE     %BT20
10
        DebugChar a1,a2,51
Jeffrey Lee's avatar
Jeffrey Lee committed
183 184 185 186 187 188

        ADRL    v1, HAL_Base + 64*1024          ; v1 -> RISC OS image

        LDR     v8, [v1, #OSHdr_Entries]
        ADD     v8, v8, v1                      ; v8 -> RISC OS entry table

189
        ARM_read_control a1
Jeffrey Lee's avatar
Jeffrey Lee committed
190 191
        ; We assume that ARMs with an I cache can have it enabled while the MMU is off.
        [ :LNOT:CacheOff
192
        ORR   a1, a1, #MMUC_I
Jeffrey Lee's avatar
Jeffrey Lee committed
193
        ]
194
        ARM_write_control a1                                ; whoosh
Jeffrey Lee's avatar
Jeffrey Lee committed
195 196 197 198 199 200 201 202 203

        ; Keep a soft copy of the CR in a banked register (R13_und)
        MSR     CPSR_c, #F32_bit+I32_bit+UND32_mode
        MOV     sp, a1
        ; Switch into SVC32 mode (we may have been in SVC26 before).
        MSR     CPSR_c, #F32_bit+I32_bit+SVC32_mode

        MOV     sb, #0

204 205 206 207
        ; Initialise RAM
        BL      init_ram

        ; The first 4K of the first registered block of RAM is used by RISC OS's init code, and also contains the stack
Jeffrey Lee's avatar
Jeffrey Lee committed
208
        ; To keep things simple and safe, we'll relocate the HAL and OS image to the top end of RAM
209
        ; Although with the beagleboard we know we'll be booted from RAM, this code has been written so that it should work if running from ROM
Jeffrey Lee's avatar
Jeffrey Lee committed
210 211 212 213 214 215 216

        ; First, identify the top end of RAM
        ; Then check if we intersect it
        ; If we do, first copy ourselves down
        ; Then copy ourselves up

relocate_code
217
        DebugChar a1,a2,66
Jeffrey Lee's avatar
Jeffrey Lee committed
218
        BL      get_end_of_ram
219
        DebugChar v1,v2,67
Jeffrey Lee's avatar
Jeffrey Lee committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

        ; How big are we?
        ADRL    v1, HAL_Base + 64*1024
        LDR     v2, [v1, #OSHdr_ImageSize]
        SUB     v1, v1, #64*1024 ; Start of HAL
        ADD     v2, v2, #64*1024 ; Size of HAL+OS
        ADD     v3, v1, v2 ; End of OS
        MOV     v4, a1 ; End of RAM
        SUB     v5, v4, v2 ; New start address of HAL
        CMP     v1, v5
        BEQ     %FT10 ; No copy needed
        BGT     %FT20 ; We're in some ROM above RAM. OK to continue with copy.
        CMP     v3, v5
        BLE     %FT20 ; We're in some ROM/RAM below our copy destination. OK to continue with copy.
        ; Else we currently overlap the area we want to copy ourselves into.
        SUB     v5, v1, v2 ; Copy the HAL+OS to just before itself
20
        MOV     a1, v5
        MOV     a2, v1 ; Copy source
        MOV     a3, v2
30
        LDR     a4, [a2], #4
        STR     a4, [a1], #4
        SUBS    a3, a3, #4
        BGT     %BT30
        ; Invalidate I-cache
        MOV     a1, #0
247 248
        MCR     p15, 0, a1, c7, c5, 0
        DebugChar a1,a2,68
Jeffrey Lee's avatar
Jeffrey Lee committed
249 250 251 252 253 254 255 256 257 258 259
        ; Jump to our new copy
        ADR     a1, relocate_code ; Keep things simple by just running through the same code again
        SUB     a2, v5, v1
        ADD     a1, a1, a2 ; relocate our branch target
        ADD     v8, v8, a2 ; Update OS entry table ptr
        MOV     pc, a1
10
        ; Copy completed OK.
        ; v2 = size of HAL+OS
        ; v4 = end of OS/RAM
        ; v5 = start of HAL
260
        ; v8 = OS entry table ptr
261
        DebugChar a1,a2,69
Jeffrey Lee's avatar
Jeffrey Lee committed
262 263 264 265
        ; Clear RAM up to v5
        MOV     a1, v5
        BL      clear_ram

266
        DebugChar a1,a2,70
Jeffrey Lee's avatar
Jeffrey Lee committed
267 268

        B       rom_checkedout_ok
269

Jeffrey Lee's avatar
Jeffrey Lee committed
270 271 272
; ------------------------------------------------------------------------------
; The RAM setup is locked in the first 8k of ROM
; as is the flash recovery code
Ben Avison's avatar
Ben Avison committed
273
        GET     RAM.s
274
        LTORG
Jeffrey Lee's avatar
Jeffrey Lee committed
275
        ALIGN   sizeofRecoveryArea
276 277

        END