Top 27.6 KB
Newer Older
1
;
Ben Avison's avatar
Ben Avison committed
2
; Copyright (c) 2012, RISC OS Open Ltd
3
; Copyright (c) 2012, Adrian Lees
Ben Avison's avatar
Ben Avison committed
4
; All rights reserved.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;     * Redistributions of source code must retain the above copyright
;       notice, this list of conditions and the following disclaimer.
;     * Redistributions in binary form must reproduce the above copyright
;       notice, this list of conditions and the following disclaimer in the
;       documentation and/or other materials provided with the distribution.
;     * Neither the name of RISC OS Open Ltd nor the names of its contributors
;       may be used to endorse or promote products derived from this software
;       without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
; POSSIBILITY OF SUCH DAMAGE.
;
; With many thanks to Broadcom Europe Ltd for releasing the source code to
; its Linux drivers, thus making this port possible.
;

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

        GET     Hdr:ListOpts
        GET     Hdr:Macros
        GET     Hdr:System
        GET     Hdr:Machine.<Machine>
        GET     Hdr:HALSize.<HALSize>

        GET     Hdr:HALEntries

Ben Avison's avatar
Ben Avison committed
43
        GET     hdr.BCM2835
44 45 46
        GET     hdr.StaticWS
        GET     hdr.CastleMacros

47
        IMPORT  Interrupt_Init
Ben Avison's avatar
Ben Avison committed
48 49 50 51 52 53 54 55 56 57 58
        IMPORT  HAL_IRQEnable
        IMPORT  HAL_IRQDisable
        IMPORT  HAL_IRQClear
        IMPORT  HAL_IRQSource
        IMPORT  HAL_IRQStatus
        IMPORT  HAL_FIQEnable
        IMPORT  HAL_FIQDisable
        IMPORT  HAL_FIQDisableAll
        IMPORT  HAL_FIQClear
        IMPORT  HAL_FIQSource
        IMPORT  HAL_FIQStatus
59

60
        IMPORT  Timer_Init
Ben Avison's avatar
Ben Avison committed
61 62 63 64 65 66 67
        IMPORT  HAL_Timers
        IMPORT  HAL_TimerDevice
        IMPORT  HAL_TimerGranularity
        IMPORT  HAL_TimerMaxPeriod
        IMPORT  HAL_TimerSetPeriod
        IMPORT  HAL_TimerPeriod
        IMPORT  HAL_TimerReadCountdown
John Ballance's avatar
John Ballance committed
68
        IMPORT  HAL_TimerIRQClear
69

Ben Avison's avatar
Ben Avison committed
70 71 72 73 74 75 76 77 78 79 80 81 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        IMPORT  HAL_CounterRate
        IMPORT  HAL_CounterPeriod
        IMPORT  HAL_CounterRead
        IMPORT  HAL_CounterDelay

        IMPORT  HAL_IICBuses
        IMPORT  HAL_IICType
        IMPORT  HAL_IICDevice
        IMPORT  HAL_IICTransfer
        IMPORT  HAL_IICMonitorTransfer

        IMPORT  HAL_NVMemoryType
        IMPORT  HAL_NVMemorySize
        IMPORT  HAL_NVMemoryPageSize
        IMPORT  HAL_NVMemoryProtectedSize
        IMPORT  HAL_NVMemoryProtection
        IMPORT  HAL_NVMemoryRead
        IMPORT  HAL_NVMemoryWrite

        IMPORT  HAL_VideoFlybackDevice
        IMPORT  HAL_Video_SetMode
        IMPORT  HAL_Video_WritePaletteEntry
        IMPORT  HAL_Video_WritePaletteEntries
        IMPORT  HAL_Video_ReadPaletteEntry
        IMPORT  HAL_Video_SetInterlace
        IMPORT  HAL_Video_SetBlank
        IMPORT  HAL_Video_SetPowerSave
        IMPORT  HAL_Video_UpdatePointer
        IMPORT  HAL_Video_SetDAG
        IMPORT  HAL_Video_VetMode
        IMPORT  HAL_Video_PixelFormats
        IMPORT  HAL_Video_Features
        IMPORT  HAL_Video_BufferAlignment
        IMPORT  HAL_Video_OutputFormat
        IMPORT  HAL_Video_Render
        IMPORT  HAL_Video_IICOp

        IMPORT  HAL_UARTPorts
        IMPORT  HAL_UARTStartUp
        IMPORT  HAL_UARTShutdown
        IMPORT  HAL_UARTFeatures
        IMPORT  HAL_UARTReceiveByte
        IMPORT  HAL_UARTTransmitByte
        IMPORT  HAL_UARTLineStatus
        IMPORT  HAL_UARTInterruptEnable
        IMPORT  HAL_UARTRate
        IMPORT  HAL_UARTFormat
        IMPORT  HAL_UARTFIFOSize
        IMPORT  HAL_UARTFIFOClear
        IMPORT  HAL_UARTFIFOEnable
        IMPORT  HAL_UARTFIFOThreshold
        IMPORT  HAL_UARTInterruptID
        IMPORT  HAL_UARTBreak
        IMPORT  HAL_UARTModemControl
        IMPORT  HAL_UARTModemStatus
        IMPORT  HAL_UARTDevice

        IMPORT  HAL_DebugRX
        IMPORT  HAL_DebugTX

        IMPORT  HAL_ATAControllerInfo

        IMPORT  HAL_KbdScanSetup
        IMPORT  HAL_KbdScan
        IMPORT  HAL_KbdScanFinish
        IMPORT  HAL_KbdScanInterrupt

        IMPORT  HAL_USBControllerInfo
        IMPORT  HAL_USBPortPower
        IMPORT  HAL_USBPortStatus
        IMPORT  HAL_USBPortIRQ
141

142
        IMPORT  HAL_Video_FramestoreAddress
Ben Avison's avatar
Ben Avison committed
143
        EXPORT  HAL_Base
144 145 146

HAL_Base
     [ HALDebug
Ben Avison's avatar
Ben Avison committed
147 148 149 150 151 152 153 154
        IMPORT  clear_block
        IMPORT  set_text_colours
        IMPORT  output_char
        IMPORT  output_hex8
        IMPORT  output_newline
        IMPORT  output_regs
        IMPORT  output_text
        IMPORT  output_text_at
155 156
     ]

Ben Avison's avatar
Ben Avison committed
157 158
        EXPORT  reset
        EXPORT  workspace
159

Ben Avison's avatar
Ben Avison committed
160
        ENTRY
161

Ben Avison's avatar
Ben Avison committed
162 163 164 165 166 167 168
reset   B       start
undef   B       undefined_instr
swi     B       swi_instr
pabort  B       prefetch_abort
dabort  B       data_abort
irq     B       interrupt
fiq     B       fast_interrupt
169

Ben Avison's avatar
Ben Avison committed
170
        ALIGN   256
171

Ben Avison's avatar
Ben Avison committed
172 173 174 175
atags   ; list of 'atags' structures constructed here by the loader code
        ; running on VideoCore, describing
        ; - available memory
        ; - command line parameters, including framebuffer parameters
176

Ben Avison's avatar
Ben Avison committed
177
        ALIGN   4096
178 179 180

end_stack
workspace
Ben Avison's avatar
Ben Avison committed
181
        %       sizeof_workspace
182

Ben Avison's avatar
Ben Avison committed
183
        LTORG
184

Ben Avison's avatar
Ben Avison committed
185 186
        ; exception handlers just for use during HAL init,
        ;   in case something goes wrong
187 188

interrupt
Ben Avison's avatar
Ben Avison committed
189 190
        ADRL    sb,workspace
        ADRL    R13,end_stack
191
     [ HALDebug
Ben Avison's avatar
Ben Avison committed
192 193 194 195 196
        ADR     R0,irq_text
        MOV     R1,#0
        MOV     R2,#0
        ADR     R14,interrupt
        B       output_text_at
197 198 199 200 201
     |
        B       interrupt
     ]

fast_interrupt
Ben Avison's avatar
Ben Avison committed
202 203
        ADRL    sb,workspace
        ADRL    R13,end_stack
204
     [ HALDebug
Ben Avison's avatar
Ben Avison committed
205 206 207 208 209
        ADR     R0,fiq_text
        MOV     R1,#0
        MOV     R2,#0
        ADR     R14,fast_interrupt
        B       output_text_at
210 211 212 213 214
     |
        B       fast_interrupt
     ]

swi_instr
Ben Avison's avatar
Ben Avison committed
215 216
        ADRL    sb,workspace
        ADRL    R13,end_stack
217
     [ HALDebug
Ben Avison's avatar
Ben Avison committed
218 219 220 221 222
        ADR     R0,swi_text
        MOV     R1,#0
        MOV     R2,#0
        ADR     R14,swi_instr
        B       output_text_at
223 224 225 226 227
     |
        B       swi_instr
     ]

prefetch_abort
Ben Avison's avatar
Ben Avison committed
228 229
        ADRL    sb,workspace
        ADRL    R13,end_stack
230
     [ HALDebug
Ben Avison's avatar
Ben Avison committed
231 232 233 234 235
        ADR     R0,pabt_text
        MOV     R1,#0
        MOV     R2,#0
        ADR     R14,prefetch_abort
        B       output_text_at
236 237 238 239 240
     |
        B       prefetch_abort
     ]

data_abort
Ben Avison's avatar
Ben Avison committed
241 242
        ADRL    sb,workspace
        ADRL    R13,end_stack
243
     [ HALDebug
Ben Avison's avatar
Ben Avison committed
244 245 246 247 248
        ADR     R0,dabt_text
        MOV     R1,#0
        MOV     R2,#0
        ADR     R14,data_abort
        B       output_text_at
249 250 251 252 253
     |
        B       data_abort
     ]

undefined_instr
Ben Avison's avatar
Ben Avison committed
254 255
        ADRL    sb,workspace
        ADRL    R13,end_stack
256
     [ HALDebug
Ben Avison's avatar
Ben Avison committed
257 258 259 260 261
        ADR     R0,und_text
        MOV     R1,#0
        MOV     R2,#5
        ADR     R14,undefined_instr
        B       output_text_at
262 263 264 265 266
     |
        B       undefined_instr
     ]

     [ HALDebug
Ben Avison's avatar
Ben Avison committed
267 268 269 270 271 272
fiq_text        =       "FIQ",0
irq_text        =       "IRQ",0
swi_text        =       "SWI",0
pabt_text       =       "Prefetch Abort",0
dabt_text       =       "Data Abort",0
und_text        =       "Undefined Instruction",0
273 274 275
        ALIGN
     ]

Ben Avison's avatar
Ben Avison committed
276
start   MSR     CPSR_c,#F32_bit+I32_bit+SVC32_mode
277

Ben Avison's avatar
Ben Avison committed
278 279 280
        ADRL    v1, HAL_Base + OSROM_HALSize    ; v1 -> RISC OS image
        LDR     v8, [v1, #OSHdr_Entries]
        ADD     v8, v8, v1                      ; v8 -> RISC OS entry table
281

Ben Avison's avatar
Ben Avison committed
282 283 284
        ; Ensure CPU is 'set up' (typically enables ICache)
        MOV     a1, #0
        CallOSM OS_InitARM
285

Ben Avison's avatar
Ben Avison committed
286 287
        ADRL    sb,workspace
        ADRL    R13,end_stack
288

289 290 291 292 293 294 295 296 297
 [ HALDebug
        LDR     r4,=IO_Base
        STR     r4,PeriBase
        mov     a1, #1
        bl      HAL_UARTStartUp          ; start early for debug use
        bl      HAL_DebugTXStrInline
        DCB     "HalStartup",10,0
        ALIGN
 ]
Ben Avison's avatar
Ben Avison committed
298
        LDR     R4,mbox_addr
299
 [ UseALBlob
John Ballance's avatar
John Ballance committed
300
        MOV     R5,#&80 + MB_Chan_Pwr  ; (looks like turn on USB)
301
        STR     R5,[R4,#MB_ChWr]
302 303 304

        ; For now, just assume that we have 40MB of memory...
        ; set frame buffer at the top...1920x1080x32bpp requires 8MB
305 306 307 308 309 310
        LDR     R1,=FB_Length
        STR     R1, FB_Size
        LDR     R1,=FB_Address
        STR     R1, FB_Base
        ORR     R3,R1,#DMA_RAM_Base + MB_Chan_FB
        STR     R3,[R4,#MB_ChWr]
311 312
        and     r0, r3, #GPU_CacheMask
        str     r0, FB_CacheMode
313 314 315 316 317 318 319 320 321 322 323 324 325 326
 |
        ldr     r1, =1920
        str     r1, mbxres
        str     r1, mbxvres
        ldr     r1, =1080
        str     r1, mbyres
        str     r1, mbyvres
 [ SCR32
        mov     r1, #32
 |
        MOV     r1, #16
 ]
        str     r1, mbbpp
        ADR     r1, mbram
John Ballance's avatar
John Ballance committed
327 328
        ORR     R3, R1, #GPU_L2CnonAl + MB_Chan_FB; try with L2 cache on
10      str     r3, [r4, #MB_ChWr]
329 330 331 332 333 334 335 336 337 338 339
11      ldr     r0, [r4, #MB_Sta]            ; await response
        tst     r0, #MB_Sta_Empty
        bne     %bt11
        ldr     r0, [r4, #MB_ChRd]
        teq     r0, #MB_Chan_FB
        bne     %bt11                       ; not the response to our request
        ldr     r1, mbscrsz
        str     r1, FB_Size
     mov r0,r1
     bl HAL_DebugHexTX4
        ldr     r1, mbbase
John Ballance's avatar
John Ballance committed
340 341 342 343 344 345 346
        and     r0, r1, #GPU_CacheMask
        str     r0, FB_CacheMode
     bl HAL_DebugHexTX4
        teq     r1, #0                      ;did we get an answer
        ORREQ   R3, R3, #GPU_UnCached + MB_Chan_FB; try with L2 cache off
        BEQ     %BT10                       ; 0 size.. try with L2 off
        bic     r1, r1, #GPU_CacheMask
347 348 349 350 351
        str     r1, FB_Base
     mov r0,r1
     bl HAL_DebugHexTX4


352 353 354
     ]

     [ HALDebug
Ben Avison's avatar
Ben Avison committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
        [ SCR32
        LDR     R0,=1920*8*4
        STR     R1,ScreenBase               ; for HAL use, remember address we were given
        STR     R0,BytesPerRow
        MOV     R0,#8*4
        STR     R0,BytesPerChar
        |
        LDR     R0,=1920*8*2
        STR     R1,ScreenBase
        STR     R0,BytesPerRow
        MOV     R0,#8*2
        STR     R0,BytesPerChar
        ]
        MOV     R0,#1920/8
        STR     R0,Columns
        MOV     R0,#1080/8
        STR     R0,Rows
        MOV     R0,#0
        STR     R0,InvertFont
        STR     R0,OutputX
        STR     R0,OutputY

        [ SCR32
        MOV     R0,#&FF000000
        MOV     R1,#&FF000000
        MOV     R8,#32
        |
        MVN     R0,#0
        MOV     R1,#0
        MOV     R0,R0,LSR #17
        MOV     R8,#16
        ]
        STR     R8,BitsPerPixel
        BL      set_text_colours

        MOV     R0,#0
        MOV     R1,#0
        LDR     R2,Columns
        LDR     R3,Rows
        BL      clear_block

        [ SCR32
        MVN     R0,#0
        MOV     R1,#&FF000000
        MOV     R8,#32
        |
        MOV     R0,#&1F<<10
        MOV     R1,#0
        MOV     R8,#16
        ]
        BL      set_text_colours

        MOV     R0,#0
        MOV     R1,#0
        ADR     R2,sign_on
        BL      output_text_at
411 412 413 414 415
 [ HALDebug
        bl      HAL_DebugTXStrInline
        DCB     "HalStartup2",10,0
        ALIGN
 ]
416
     ]
417
 [ UseALBlob        ; nolonger used
Ben Avison's avatar
Ben Avison committed
418 419 420
        ; Traverse list of 'atags' structures
        ; word 0 = size of tag, including header, in words
        ; word 1 = type tag
421

Ben Avison's avatar
Ben Avison committed
422 423
ATAG_MEM        *       &54410002
ATAG_NONE       *       0
424

Ben Avison's avatar
Ben Avison committed
425 426 427
        ADRL    v1, HAL_Base + OSROM_HALSize
        LDR     v2, [v1, #OSHdr_ImageSize]
        ADD     v2, v2, v1      ; End of OS
428

Ben Avison's avatar
Ben Avison committed
429
        ADRL    v3, atags
430
atags_loop
Ben Avison's avatar
Ben Avison committed
431 432
        LDR     a1, [v3, #4]    ;tag type
        LDR     a2, [v3]        ;size of tag inc header, in words
433

Ben Avison's avatar
Ben Avison committed
434 435
        SUB     a4, a1, #ATAG_MEM :AND: &FF000000
        SUB     a4, a4, #ATAG_MEM :AND: &00FF0000
436

Ben Avison's avatar
Ben Avison committed
437 438
        TEQ     a4, #ATAG_MEM :AND: &FFFF
        BNE     atag_next
439

Ben Avison's avatar
Ben Avison committed
440
        ; clear RAM
441

Ben Avison's avatar
Ben Avison committed
442 443
        LDR     a1, [v3, #8]    ;RAM size
        LDR     lr, [v3, #12]   ;RAM start
444

445
  [ UseALBlob
Ben Avison's avatar
Ben Avison committed
446
        ;!!! bodge to exclude frame buffer
447 448 449
        LDR     a3, =FB_MemBase
        CMP     a1, a3
        MOVHI   a1, a3
Ben Avison's avatar
Ben Avison committed
450
        STRHI   a1, [v3, #8]
451
  ]
Ben Avison's avatar
Ben Avison committed
452 453 454 455 456 457 458 459 460
        MOV     a2, #0
        MOV     a3, #0
        MOV     a4, #0
        MOV     v4, #0
        MOV     v5, #0
        MOV     v7, #0
        MOV     ip, #0
        ADD     a1, a1, lr      ;end of RAM
        MOV     lr, #0
461 462

clear_lp
Ben Avison's avatar
Ben Avison committed
463 464 465 466 467 468
        STMDB   a1!,{a2-a4,v4,v5,v7,ip,lr}
        STMDB   a1!,{a2-a4,v4,v5,v7,ip,lr}
        STMDB   a1!,{a2-a4,v4,v5,v7,ip,lr}
        STMDB   a1!,{a2-a4,v4,v5,v7,ip,lr}
        CMP     a1, v2
        BHI     clear_lp
469 470 471

;!!! this assumes single call :)

Ben Avison's avatar
Ben Avison committed
472 473 474 475 476 477
        LDR     lr, [v3]        ;tag size, words
        LDR     v1, [v3, #8]    ;RAM size
        LDR     a4, [v3, #12]   ;RAM start
        MOV     a2, v2          ;start of available RAM, after HAL + OS image
        ADD     a3, v1, a4      ;end of RAM
        ADD     v3, v3, lr, LSL #2 ;next tag
478 479 480 481
     mov a1,a2
     bl HAL_DebugHexTX4
     mov a1,a3
     bl HAL_DebugHexTX4
Ben Avison's avatar
Ben Avison committed
482
        MVN     a4, #0
483 484
        MOV     a1, #0
        STR     a1, [sp, #-4]!  ;reference handle (NULL for first call)
Ben Avison's avatar
Ben Avison committed
485
        CallOSM OS_AddRAM
486

Ben Avison's avatar
Ben Avison committed
487
        STR     a1,[sp] ;ref for next call
488

Ben Avison's avatar
Ben Avison committed
489
        B       atags_loop
490 491

atag_next
Ben Avison's avatar
Ben Avison committed
492 493 494
        TEQ     a1,#ATAG_NONE
        ADDNE   v3, v3, a2, LSL #2
        BNE     atags_loop
John Ballance's avatar
John Ballance committed
495 496 497 498
 |
ROMTOP   *  6    <<20
RAMTOP   *  128  <<20
     ; debug hack to force 128meg ram and 6meg rom
Ben Avison's avatar
Ben Avison committed
499 500 501 502 503 504 505 506 507 508
        MOV     v2, #ROMTOP     ;start of available RAM, after HAL + OS image
        MOV     a1, #RAMTOP     ; end of RAM
        MOV     a2, #0
        MOV     a3, #0
        MOV     a4, #0
        MOV     v4, #0
        MOV     v5, #0
        MOV     v7, #0
        MOV     ip, #0
        MOV     lr, #0
John Ballance's avatar
John Ballance committed
509 510

clear_lp1
Ben Avison's avatar
Ben Avison committed
511 512 513 514 515 516
        STMDB   a1!,{a2-a4,v4,v5,v7,ip,lr}
        STMDB   a1!,{a2-a4,v4,v5,v7,ip,lr}
        STMDB   a1!,{a2-a4,v4,v5,v7,ip,lr}
        STMDB   a1!,{a2-a4,v4,v5,v7,ip,lr}
        CMP     a1, v2
        BHI     clear_lp1
John Ballance's avatar
John Ballance committed
517
        mov     a2, v2
Ben Avison's avatar
Ben Avison committed
518 519
        ADD     a3, a2, #RAMTOP ; end of RAM
        SUB     a3, a3, #ROMTOP ; less what is used
John Ballance's avatar
John Ballance committed
520 521 522 523
     mov a1,a2
     bl HAL_DebugHexTX4
     mov a1,a3
     bl HAL_DebugHexTX4
Ben Avison's avatar
Ben Avison committed
524
        MVN     a4, #0
John Ballance's avatar
John Ballance committed
525 526
        MOV     a1, #0
        STR     a1, [sp, #-4]!  ;reference handle (NULL for first call)
Ben Avison's avatar
Ben Avison committed
527
        CallOSM OS_AddRAM
John Ballance's avatar
John Ballance committed
528

Ben Avison's avatar
Ben Avison committed
529
        STR     a1,[sp] ;ref for next call
John Ballance's avatar
John Ballance committed
530 531 532
 ]


533 534

     [ HALDebug
Ben Avison's avatar
Ben Avison committed
535 536
        ADR     R0,start_os
        BL      output_text
537
     ]
John Ballance's avatar
John Ballance committed
538

539
  [ UseALBlob
John Ballance's avatar
John Ballance committed
540 541 542 543 544
 ; fill the frame buffer
   ldr v2, FB_Base

   ldr a1, FB_Size
     ; debug hack to force 128meg ram and 6meg rom
Ben Avison's avatar
Ben Avison committed
545
        LDR     a2, =&00ffffff
John Ballance's avatar
John Ballance committed
546
        ldr     a3, =&ffffffff
Ben Avison's avatar
Ben Avison committed
547 548 549 550 551 552 553
        ldr     a4, =&00ffffff
        ldr     v4, =&ffffffff
        ldr     v5, =&00ffffff
        ldr     v7, =&ffffffff
        ldr     ip, =&00ffffff
        ADD     a1, v2, a1      ;end of RAM
        MOV     lr, #0
John Ballance's avatar
John Ballance committed
554 555

clear_lp12
Ben Avison's avatar
Ben Avison committed
556 557 558 559 560 561
        STMDB   a1!,{a2-a4,v4,v5,v7,ip,lr}
        STMDB   a1!,{a2-a4,v4,v5,v7,ip,lr}
        STMDB   a1!,{a2-a4,v4,v5,v7,ip,lr}
        STMDB   a1!,{a2-a4,v4,v5,v7,ip,lr}
        CMP     a1, v2
        BHI     clear_lp12
John Ballance's avatar
John Ballance committed
562
  ]
Ben Avison's avatar
Ben Avison committed
563
        ; OS kernel informed of RAM areas
564

Ben Avison's avatar
Ben Avison committed
565
        LDR     a4,[sp],#4      ;!!! ref from last AddRAM
566 567 568 569 570 571
        MOV     a1, #OSStartFlag_RAMCleared
        ADRL    a2, HAL_Base + OSROM_HALSize       ; a2 -> RISC OS image
        ADR     a3, HALdescriptor

        CallOSM OS_Start

Ben Avison's avatar
Ben Avison committed
572
        ; OS_Start doesn't return....invokes HAL_Init after MMU activation
573 574

mbox_addr
575
        DCD     IO_Base + MB_Base
576 577

     [ HALDebug
Ben Avison's avatar
Ben Avison committed
578 579 580
sign_on  =      "BCM2835 Raspberry Pi",13,10,0
start_os =      "Starting OS",13,10,0
        ALIGN
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
     ]

HALdescriptor   DATA
        DCD     HALFlag_NCNBWorkspace
        DCD     HAL_Base - HALdescriptor
        DCD     OSROM_HALSize
        DCD     HAL_EntryTable - HALdescriptor
        DCD     HAL_Entries
        DCD     sizeof_workspace

HAL_EntryTable  DATA
        HALEntry HAL_Init

        HALEntry HAL_IRQEnable
        HALEntry HAL_IRQDisable
        HALEntry HAL_IRQClear
        HALEntry HAL_IRQSource
        HALEntry HAL_IRQStatus
        HALEntry HAL_FIQEnable
        HALEntry HAL_FIQDisable
        HALEntry HAL_FIQDisableAll
        HALEntry HAL_FIQClear
        HALEntry HAL_FIQSource
        HALEntry HAL_FIQStatus

        HALEntry HAL_Timers
        HALEntry HAL_TimerDevice
        HALEntry HAL_TimerGranularity
        HALEntry HAL_TimerMaxPeriod
        HALEntry HAL_TimerSetPeriod
        HALEntry HAL_TimerPeriod
        HALEntry HAL_TimerReadCountdown

        HALEntry HAL_CounterRate
        HALEntry HAL_CounterPeriod
        HALEntry HAL_CounterRead
        HALEntry HAL_CounterDelay

        HALEntry HAL_NVMemoryType
        HALEntry HAL_NVMemorySize
        HALEntry HAL_NVMemoryPageSize
        HALEntry HAL_NVMemoryProtectedSize
        HALEntry HAL_NVMemoryProtection
        NullEntry ; HAL_NVMemoryIICAddress
        HALEntry HAL_NVMemoryRead
        HALEntry HAL_NVMemoryWrite

        HALEntry HAL_IICBuses
        HALEntry HAL_IICType
        NullEntry ; HAL_IICSetLines
        NullEntry ; HAL_IICReadLines
        HALEntry HAL_IICDevice
        HALEntry HAL_IICTransfer
        HALEntry HAL_IICMonitorTransfer

        HALEntry HAL_VideoFlybackDevice
        HALEntry HAL_Video_SetMode
        HALEntry HAL_Video_WritePaletteEntry
        HALEntry HAL_Video_WritePaletteEntries
        HALEntry HAL_Video_ReadPaletteEntry
        HALEntry HAL_Video_SetInterlace
        HALEntry HAL_Video_SetBlank
        HALEntry HAL_Video_SetPowerSave
        HALEntry HAL_Video_UpdatePointer
        HALEntry HAL_Video_SetDAG
        HALEntry HAL_Video_VetMode
        HALEntry HAL_Video_PixelFormats
        HALEntry HAL_Video_Features
        HALEntry HAL_Video_BufferAlignment
        HALEntry HAL_Video_OutputFormat

        NullEntry ; HALEntry HAL_MatrixColumns
        NullEntry ; HALEntry HAL_MatrixScan

        NullEntry ; HALEntry HAL_TouchscreenType
        NullEntry ; HALEntry HAL_TouchscreenRead
        NullEntry ; HALEntry HAL_TouchscreenMode
        NullEntry ; HALEntry HAL_TouchscreenMeasure

        HALEntry HAL_MachineID

        HALEntry HAL_ControllerAddress
        HALEntry HAL_HardwareInfo
        HALEntry HAL_SuperIOInfo
        HALEntry HAL_PlatformInfo
        NullEntry ; HALEntry HAL_CleanerSpace

        HALEntry HAL_UARTPorts
        HALEntry HAL_UARTStartUp
        HALEntry HAL_UARTShutdown
        HALEntry HAL_UARTFeatures
        HALEntry HAL_UARTReceiveByte
        HALEntry HAL_UARTTransmitByte
        HALEntry HAL_UARTLineStatus
        HALEntry HAL_UARTInterruptEnable
        HALEntry HAL_UARTRate
        HALEntry HAL_UARTFormat
        HALEntry HAL_UARTFIFOSize
        HALEntry HAL_UARTFIFOClear
        HALEntry HAL_UARTFIFOEnable
        HALEntry HAL_UARTFIFOThreshold
        HALEntry HAL_UARTInterruptID
        HALEntry HAL_UARTBreak
        HALEntry HAL_UARTModemControl
        HALEntry HAL_UARTModemStatus
        HALEntry HAL_UARTDevice

        HALEntry HAL_Reset

        HALEntry HAL_DebugRX
        HALEntry HAL_DebugTX

        NullEntry ; HAL_PCIFeatures
        NullEntry ; HAL_PCIReadConfigByte
        NullEntry ; HAL_PCIReadConfigHalfword
        NullEntry ; HAL_PCIReadConfigWord
        NullEntry ; HAL_PCIWriteConfigByte
        NullEntry ; HAL_PCIWriteConfigHalfword
        NullEntry ; HAL_PCIWriteConfigWord
        NullEntry ; HAL_PCISpecialCycle
        NullEntry ; HAL_PCISlotTable
        NullEntry ; HAL_PCIAddresses

        HALEntry HAL_ATAControllerInfo
        NullEntry ; HAL_ATASetModes
        NullEntry ; HAL_ATACableID

        HALEntry HAL_InitDevices

        HALEntry HAL_KbdScanSetup
        HALEntry HAL_KbdScan
        HALEntry HAL_KbdScanFinish
        HALEntry HAL_KbdScanInterrupt

        HALEntry HAL_PhysInfo

        HALEntry HAL_USBControllerInfo

        NullEntry ;HALEntry HAL_MonitorLeadID

        HALEntry HAL_Video_Render

        HALEntry HAL_USBPortPower
        HALEntry HAL_USBPortStatus
        HALEntry HAL_USBPortIRQ

        HALEntry HAL_Video_IICOp

John Ballance's avatar
John Ballance committed
729
        HALEntry  HAL_TimerIRQClear
730 731 732 733
        NullEntry ; HAL_TimerIRQStatus

        HALEntry HAL_ExtMachineID

734 735
        HALEntry HAL_Video_FramestoreAddress

736 737 738 739 740 741 742
HAL_Entries     * (.-HAL_EntryTable)/4

;--------------------------------------------------------------------------------------
; HAL Initialisation callback from OS kernel
;--------------------------------------------------------------------------------------

HAL_Init
Ben Avison's avatar
Ben Avison committed
743
        STMFD   R13!,{R8,R14}
744

Ben Avison's avatar
Ben Avison committed
745 746
        MOV     R8,a2
        BL      SetUpOSEntries
747

John Ballance's avatar
John Ballance committed
748 749 750 751 752 753 754 755 756 757
        MOV     a4, sb                       ; confirm the caching mode in use in GPU
        ADRL    sb, workspace                ; where we remembered it is
        LDR     a3, FB_Size
        LDR     a2, FB_Base                  ; effectively part of the ROM image
        LDR     a1, FB_CacheMode             ; GPU cache mode
        mov     sb, a4
        STR     a3, FB_Size                  ; put in our workspace
        STR     a2, FB_Base                  ; for HAL_FramestoreAddress use
        STR     a1, FB_CacheMode

Ben Avison's avatar
Ben Avison committed
758 759
        ; Get the physical address of the start of our workspace
        ; R8 -> start of the page containing our workspace
760

Ben Avison's avatar
Ben Avison committed
761 762 763
        MOV     a1,R8
        CallOS  OS_LogToPhys
        MOV     a2, sb, LSL #20
John Ballance's avatar
John Ballance committed
764 765
        LDR     a3, FB_CacheMode
        ORR     a1, a1, a3;#DMA_RAM_Base
Ben Avison's avatar
Ben Avison committed
766 767
        ORR     a1, a1, a2, LSR #20     ; factor in start offset
        STR     a1, WSPhysAddr
John Ballance's avatar
John Ballance committed
768 769 770 771

        MOV     a1, #0                   ; map in the IO space
        LDR     a2, =IO_Base
        LDR     a3, =IO_Size
772
        CallOS  OS_MapInIO
John Ballance's avatar
John Ballance committed
773
        STR     a1, PeriBase
774 775 776 777 778 779 780

        BL      Interrupt_Init          ; initialise our interrupts
        BL      Timer_Init

        MOV     a1,#0                   ; start the uart ..we use it for debug
        BL      HAL_UARTStartUp         ; restart to capture logical io address

Ben Avison's avatar
Ben Avison committed
781 782
        ; Map in the frame buffer
        ;    and initialise the display driver
783 784
        LDR     a3, FB_Size
        LDR     a2, FB_Base                  ; effectively part of the ROM image
John Ballance's avatar
John Ballance committed
785 786
;        LDR     a1, FB_CacheMode
;        ORR     a2, a2, a1                   ; combine in the address we need 'the other side'
787 788 789 790
     mov r0,a2
     bl HAL_DebugHexTX4
     mov r0,a3
     bl HAL_DebugHexTX4
Ben Avison's avatar
Ben Avison committed
791 792 793
        MOV     a1, #0
        STR     a1, LastInt
        STR     a1, CurAddr
794

Ben Avison's avatar
Ben Avison committed
795 796
        CallOS  OS_MapInIO
        STR     a1,ScreenBase
797
     bl HAL_DebugHexTX4
798

Ben Avison's avatar
Ben Avison committed
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
        [ SCR32
        LDR     a1,=1920*8*4
        STR     a1,BytesPerRow
        MOV     a1,#8*4
        STR     a1,BytesPerChar
        |
        LDR     a1,=1920*8*2
        STR     a1,BytesPerRow
        MOV     a1,#8*2
        STR     a1,BytesPerChar
        ]
        MOV     a1,#1920/8
        STR     a1,Columns
        MOV     a1,#1080/8
        STR     a1,Rows
        MOV     a1,#0
        STR     a1,InvertFont
        MOV     a1,#4
        STR     a1,OutputX
        STR     a1,OutputY

        [ SCR32
        MOV     a1,#-1
        MOV     a2,#&FF000000
        MOV     R8,#32
        |
        MVN     a1,#0
        MOV     a2,#0
        MOV     a1,a1,LSR #17
        MOV     R8,#16
        ]
        STR     R8,BitsPerPixel
831
     [ HALDebug
Ben Avison's avatar
Ben Avison committed
832
        BL      set_text_colours
833

Ben Avison's avatar
Ben Avison committed
834 835
        ADR     a1,hal_init
        BL      output_text
836 837 838 839 840 841
     ]

     [ HALDebug
        ADR     a1,uart_started
        BL      output_text

Ben Avison's avatar
Ben Avison committed
842 843
        STR     v1,[sp,#-4]!
        ADRL    v1,hal_init
844 845
txloop
txbusylp
Ben Avison's avatar
Ben Avison committed
846 847 848 849 850 851 852 853 854 855 856 857 858
        MOV a1,#0
        BL HAL_UARTLineStatus
        TST a1,#&20
        BEQ txbusylp
        MOV a1,#0
        LDRB a2,[v1],#1
        BL HAL_UARTTransmitByte
        LDRB a1,[v1]
        TEQ a1,#0
        BNE txloop
        LDR v1,[sp],#4

        LDMFD   R13!,{R8,PC}
859

860 861
uart_started = " UART started",13,10,0
hal_init     = " HAL Init completed",13,10,0
Ben Avison's avatar
Ben Avison committed
862
        ALIGN
863 864

     |
Ben Avison's avatar
Ben Avison committed
865
        LDMFD   R13!,{R8,PC}
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
     ]


; Initialise and relocate the entry table.
SetUpOSEntries  ROUT
        STR     a1, OSheader
        LDR     a2, [a1, #OSHdr_NumEntries]
        CMP     a2, #HighestOSEntry+1
        MOVHI   a2, #HighestOSEntry+1

        ADRL    a3, OSentries
        LDR     a4, [a1, #OSHdr_Entries]
        ADD     a4, a4, a1

05      SUBS    a2, a2, #1
        LDR     ip, [a4, a2, LSL #2]
        ADD     ip, ip, a4
        STR     ip, [a3, a2, LSL #2]
        BNE     %BT05

        MOV     pc, lr

HAL_ControllerAddress
        MOV     a1, #0
        MOV     pc, lr

HAL_HardwareInfo
        LDR     ip, =&FFFFFF00
        STR     ip, [a1]
        MOV     ip, #0
        STR     ip, [a2]
        LDR     ip, =&00FFFF00
        STR     ip, [a3]
        MOV     pc, lr

HAL_PlatformInfo
Ben Avison's avatar
Ben Avison committed
902
        MOV     ip, #2_10000    ; no podules,no PCI cards,no multi CPU,no soft off,and soft ROM
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
        STR     ip, [a2]
        MOV     ip, #2_11111    ; mask of valid bits
        STR     ip, [a3]
        MOV     pc, lr

HAL_SuperIOInfo
        MOV     ip, #0
        STR     ip, [a1]
        STR     ip, [a2]
        MOV     pc, lr

HAL_MachineID
        MOV     a1, #0
        MOV     a2, #0
        MOV     pc, lr

HAL_ExtMachineID
        MOVS    ip, a1
        MOV     a1, #16
        MOV     pc, lr

HAL_PhysInfo
HAL_Reset
HAL_Null
        MOV     pc, lr

929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
HAL_InitDevices
        MOV     pc, lr



 [ HALDebug
; a2-> null terminated string
HAL_DebugTXS    ROUT
        STMFD   sp!, {a1,lr}
        SUB     a2,a2,#1
1       LDRB    a1, [a2,#1]!
        TEQ     a1, #&0
        LDMEQFD sp!, {a2,pc}
        BL      HAL_DebugTX
        B       %BT1

HAL_DebugHexTX
       stmfd    r13!, {r0-r3,lr}
       b        jbdt1
HAL_DebugHexTX2
       stmfd    r13!, {r0-r3,lr}
       b        jbdt2
HAL_DebugHexTX4
       stmfd    r13!, {r0-r3,lr}
       mov      r0,r0,ror #24          ; hi byte
       bl       jbdtxh
       mov      r0,r0,ror #24
       bl       jbdtxh
jbdt2
       mov      r0,r0,ror #24
       bl       jbdtxh
       mov      r0,r0,ror #24
jbdt1
       bl       jbdtxh
       mov      r0,#' '
       bl       HAL_DebugTX
       ldmfd    r13!, {r0-r3,pc}

HAL_DebugTXStrInline
       stmfd    r13!, {r0-r3}          ; lr points to prinstring, immediately
                                       ; following call, null terminated
       sub      r3,lr,#1
1      ldrb     r0,[r3,#1]!            ; pop next char, auto incr
       teq      r0,#0                  ; terminating null
       biceq    lr,r3,#3               ; round down address
       ldmeqfd  r13!,{r0-r3}
       addeq    pc,lr,#4               ; return to next word
       bl       HAL_DebugTX            ; else send, then
       b        %bt1                   ; loop

jbdtxh stmfd    r13!,{r0-r3,lr}        ; print byte as hex
       and      a4,a1,#&f              ; get low nibble
       and      a1,a1,#&f0             ; get hi nibble
       mov      a1,a1,lsr #4           ; shift to low nibble
       cmp      a1,#&9                 ; 9?
       addle    a1,a1,#&30
       addgt    a1,a1,#&37             ; convert letter if needed
       bl       HAL_DebugTX
       cmp      a4,#9
       addle    a1,a4,#&30
       addgt    a1,a4,#&37
       bl       HAL_DebugTX
       ldmfd    r13!,{r0-r3,pc}

 |

HAL_DebugTX
HAL_DebugS
HAL_DebugHexTX
HAL_DebugHexTX2
HAL_DebugHexTX4
       MOV     pc, lr
HAL_DebugTXStrInline
       stmfd    r13!, {r0-r3}          ; lr points to prinstring, immediately
                                       ; following call, null terminated
       sub      r3,lr,#1
1      ldrb     r0,[r3,#1]!            ; pop next char, auto incr
       teq      r0,#0                  ; terminating null
       biceq    lr,r3,#3               ; round down address
       ldmeqfd  r13!,{r0-r3}
       addeq    pc,lr,#4               ; return to next word
       b        %bt1                   ; loop

 ]

1014
        END