GitLab has been upgraded to 13.7.4 If you encounter any issues mail code@riscosopen.org

SDIO 34.5 KB
Newer Older
1 2 3 4 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
;
; Copyright (c) 2012, RISC OS Open Ltd
; All rights reserved.
;
; 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.
;

        EXPORT  SDIO_InitDevices
        IMPORT  memcpy
        IMPORT  HAL_CounterDelay
        IMPORT  HAL_CounterRead
Ben Avison's avatar
Ben Avison committed
33
        IMPORT  HAL_SendHostMessage
34 35 36 37

        ; KEEP ; for debugging

        GET     Hdr:ListOpts
Jeffrey Lee's avatar
Jeffrey Lee committed
38
        GET     Hdr:CPU.Arch
39
        GET     Hdr:Macros
Ben Avison's avatar
Ben Avison committed
40 41 42 43
        GET     Hdr:System
        GET     Hdr:FSNumbers
        GET     Hdr:NewErrors
        GET     Hdr:BCMSupport
44
        GET     hdr.CastleMacros
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        GET     hdr.BCM2835
        GET     hdr.StaticWS

sb      RN      9

; Timings

; How long to wait for SDCLK to stabilise, in ms
CLOCKSET_TIMEOUT *      20 ; in ms

; Number of SDCLK cycles that must elapse between writes to registers (chip bug)
REGISTER_WRITE_PACE *   2

; Minimum value SDCLK will be set to, in kHz. Is used to set a safe gap between
; writes while reprogramming SDCLK
MIN_SDCLK       *       400

; Base address of controller as an offset from PeriBase
EMMC_Base       *       &00300000

Ben Avison's avatar
Ben Avison committed
65 66 67
; Base address of 2nd controller as an offset from PeriBase (BCM2838 only)
EMMC2_Base      *       &00340000

68
; GPIO pin assignments
Ben Avison's avatar
Ben Avison committed
69 70 71 72 73 74 75 76 77
GPIO_DAT3               *       53
GPIO_DAT2               *       52
GPIO_DAT1               *       51
GPIO_DAT0               *       50
GPIO_CMD                *       49  ; needs pull-up iff in ID mode
GPIO_CLK                *       48
GPIO_CD_AB              *       47  ; needs pull-up; active low
GPIO_STATUS_LED_AB      *       16  ; active low
GPIO_STATUS_LED_BPlus   *       47  ; active high
Ben Avison's avatar
Ben Avison committed
78
GPIO_STATUS_LED_B4      *       42  ; active high
Ben Avison's avatar
Ben Avison committed
79
EXPGPIO_VREG            *       4   ; 0 => 3.3V, 1 => 1.8V
80 81 82 83

; GPIO function select bitfields
GPIO_FSEL_INPUT  *      0
GPIO_FSEL_OUTPUT *      1
Ben Avison's avatar
Ben Avison committed
84 85 86 87 88 89
GPIO_FSEL_ALT0   *      4
GPIO_FSEL_ALT1   *      5
GPIO_FSEL_ALT2   *      6
GPIO_FSEL_ALT3   *      7
GPIO_FSEL_ALT4   *      3
GPIO_FSEL_ALT5   *      2
90 91 92 93 94 95 96 97 98
GPIO_FSEL_SHIFT  *      3
GPIO_FSEL_MASK   *      7

; GPIO pull-up/down states
GPIO_PUD_OFF     *      0
GPIO_PUD_DOWN    *      1
GPIO_PUD_UP      *      2

; Selected SD controller registers and bits
Ben Avison's avatar
Ben Avison committed
99 100 101 102 103
ARG1                    *       &08
CMDTM                   *       &0C
CMDTM_CMD_INDEX_SHIFT       *   24
STATUS                  *       &24
STATUS_CMD_INHIBIT          *   1 :SHL: 0
104 105 106 107 108 109 110 111 112
CONTROL1                *       &2C
CONTROL1_CLK_FREQ8_SHIFT    *   8
CONTROL1_CLK_FREQ8_MASK     *   &FF :SHL: CONTROL1_CLK_FREQ8_SHIFT
CONTROL1_CLK_FREQ_MS2_SHIFT *   6
CONTROL1_CLK_FREQ_MS2_MASK  *   3 :SHL: CONTROL1_CLK_FREQ_MS2_SHIFT
CONTROL1_CLK_GENSEL         *   1 :SHL: 5
CONTROL1_CLK_EN             *   1 :SHL: 2
CONTROL1_CLK_STABLE         *   1 :SHL: 1
CONTROL1_CLK_INTLEN         *   1 :SHL: 0
Ben Avison's avatar
Ben Avison committed
113 114 115
INTERRUPT               *       &30
IRPT_MASK               *       &34
IRQ_CC                      *   1 :SHL: 0
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

; Capabilities bits
CAP_DDR50S      *       1 :SHL: 34
CAP_SDR104S     *       1 :SHL: 33
CAP_SDR50S      *       1 :SHL: 32
CAP_VS18        *       1 :SHL: 26  ; Voltage support 1.8V
CAP_VS30        *       1 :SHL: 25  ; Voltage support 3.0V
CAP_VS33        *       1 :SHL: 24  ; Voltage support 3.3V
CAP_HSS         *       1 :SHL: 21  ; High speed (52MHz) support
CAP_MBL_SHIFT   *       16
CAP_MBL_512     *       0 :SHL: CAP_MBL_SHIFT  ; 512-byte blocks
CAP_MBL_1024    *       1 :SHL: CAP_MBL_SHIFT  ; 1024-byte blocks
CAP_MBL_2048    *       2 :SHL: CAP_MBL_SHIFT  ; 2048-byte blocks
CAP_MBL_MASK    *       3 :SHL: CAP_MBL_SHIFT


        AREA    |Asm$$Code|, CODE, READONLY, PIC

Ben Avison's avatar
Ben Avison committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
; Delay in microseconds between writes to SDHCI registers during speed
; measurement. At this point we don't know what the delay actually needs to be
; because we haven't figured out the clock speed yet, but assuming the GPU boot
; code never sets it slower than 25 MHz, this should be long enough for all cases.
MEASURE_DELAY * 5

; Delay to use when waiting for SDCLK to settle. 20 ms is the longest it's
; supposed to take, and because this is a simplified implementation, we always
; wait this long.
MEASURE_LONG_DELAY * 20000

; If the input clock is 25 MHz, this will set SDCLK to approximately 400 kHz,
; and proportionately higher for other inputs.
MEASURE_DIVIDER * 63

; Command to time is CMD7_SELECT_DESELECT_CARD
MEASURE_COMMAND * 7

; Empirically, I have determined that it takes the controller this many cycles
; of SDCLK to complete a deselect card command
MEASURE_SDCLKS * 56

; Round the estimate of the input clock to nearest multiple of this many MHz
MEASURE_ACCURACY * 5

; Routine to estimate the input clock speed to the SDHCI block based upon how
; long it takes to issue a deselect-all-cards command. This command has no
; response, so it doesn't matter that lots of things aren't set up properly yet.
MeasureSpeed ROUT
        Push    "v1-v3,lr"
Ben Avison's avatar
Ben Avison committed
164
        LDR     v1, SDIO_Address
Ben Avison's avatar
Ben Avison committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

        ; Switch the clock divider to a known value.
        ; This is a simplified version of the algorithm in SetSDCLK.
        LDR     a1, =MEASURE_DELAY
        BL      HAL_CounterDelay
        LDR     v2, [v1, #CONTROL1]
        BIC     v2, v2, #CONTROL1_CLK_EN ; stop clock going to card
        STR     v2, [v1, #CONTROL1]
        LDR     a1, =MEASURE_DELAY
        BL      HAL_CounterDelay
        BIC     v2, v2, #CONTROL1_CLK_INTLEN ; stop clock
        STR     v2, [v1, #CONTROL1]
        LDR     a1, =MEASURE_DELAY
        BL      HAL_CounterDelay
        BIC     v2, v2, #CONTROL1_CLK_FREQ8_MASK
        BIC     v2, v2, #CONTROL1_CLK_FREQ_MS2_MASK
        ORR     v2, v2, #MEASURE_DIVIDER :SHL: CONTROL1_CLK_FREQ8_SHIFT
        STR     v2, [v1, #CONTROL1]
        LDR     a1, =MEASURE_DELAY
        BL      HAL_CounterDelay
        ORR     v2, v2, #CONTROL1_CLK_INTLEN ; start clock
        STR     v2, [v1, #CONTROL1]
        LDR     a1, =MEASURE_LONG_DELAY
        BL      HAL_CounterDelay
        LDR     v2, [v1, #CONTROL1]
        ORR     v2, v2, #CONTROL1_CLK_EN ; let clock go to card
        STR     v2, [v1, #CONTROL1]
        LDR     a1, =MEASURE_DELAY
        BL      HAL_CounterDelay

        ; Wait for command inhibit to be unset
        ; It's really important that this works, we won't be able to boot if
        ; this never happens, so don't bother with a timeout.
01      LDR     v2, [v1, #STATUS]
        TST     v2, #STATUS_CMD_INHIBIT
        BNE     %B01

        ; Ensure no interrupts are pending
        MOV     lr, #-1
        STR     lr, [v1, #INTERRUPT]
        LDR     a1, =MEASURE_DELAY
        BL      HAL_CounterDelay

        ; Enable the command-complete "interrupt" status bit.
        LDR     v2, =IRQ_CC
        STR     v2, [v1, #IRPT_MASK]
        LDR     a1, =MEASURE_DELAY
        BL      HAL_CounterDelay

        ; Use argument 0 to deselect all cards
        MOV     v2, #0
        STR     v2, [v1, #ARG1]
        LDR     a1, =MEASURE_DELAY
        BL      HAL_CounterDelay

        ; Wait for the timer to wrap, so we don't have to deal with wrap later
        BL      HAL_CounterRead
02      MOV     v2, a1
        BL      HAL_CounterRead
        CMP     a1, v2
        BLS     %B02

        MRS     v3, CPSR
        ORR     lr, v3, #&80
        MSR     CPSR_c, lr ; IRQs off

        ; Read start time
        BL      HAL_CounterRead
        MOV     v2, a1

        ; Trigger the command (all the other bits in CMDTM are 0)
        MOV     lr, #MEASURE_COMMAND :SHL: CMDTM_CMD_INDEX_SHIFT
        STR     lr, [v1, #CMDTM]

        ; Wait for completion
03      LDR     lr, [v1, #INTERRUPT]
        TST     lr, #IRQ_CC
        BEQ     %B03

        ; Read finish time
        BL      HAL_CounterRead

        ; Acknowledge interrupt
        MOV     lr, #IRQ_CC
        STR     lr, [v1, #INTERRUPT]

        MSR     CPSR_c, v3 ; IRQs restored

        ; Calculate elapsed time
        SUB     v2, v2, a1
        LDR     a1, =MEASURE_DELAY
        BL      HAL_CounterDelay

        ; Calculate input clock frequency
        LDR     a1, =(MEASURE_SDCLKS * MEASURE_DIVIDER) / MEASURE_ACCURACY
        ADD     a1, a1, v2, LSR #1 ; round to nearest
        DivRem  a2, a1, v2, lr
        LDR     a3, =MEASURE_ACCURACY  * 1000
        MUL     a4, a2, a3
        STR     a4, SDHCIInputClock

        Pull    "v1-v3,pc"

Ben Avison's avatar
Ben Avison committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
        ; in: a1 = GPIO pin number
        ;     a2 = setting
ExpGPIOWrite ROUT
        Push    "a1-a2,lr"
        ADR     a1, tagbuffer + 12
        BIC     a1, a1, #15 ; align to 16-byte boundary
        ADR     a2, %FT01
        MOV     a3, #%FT04 - %FT01
        BL      memcpy
        Pull    "a3-a4"
        ADD     a3, a3, #128
        STR     a3, [a1, #%FT02 - %FT01]
        STR     a4, [a1, #%FT03 - %FT01]
        CallOS  OS_LogToPhys
        MOV     a2, a1
        MOV     a1, #MB_Chan_ARM2VC
        BL      HAL_SendHostMessage
        Pull    "pc"

01      &       %FT04 - .
        &       0 ; request
        &       ARM2VC_Tag_SetExtGPIOState
        &       8 ; tag buffer size
        &       8 ; number of bytes in buffer on entry
02      &       0 ; fill in with GPIO pin number + 128
03      &       0 ; fill in with GPIO pin setting
        &       ARM2VC_Tag_End
04

Init_BCM2838 ROUT
        Push    "v1,lr"

        ; Set voltage regulator to 3.3V
        MOV     a1, #EXPGPIO_VREG
        MOV     a2, #0
        BL      ExpGPIOWrite

        ; Settling time for the regulator is 5 ms.
        ; Wait for the counter to wrap twice (which will take somewhere
        ; between 10-20 ms).
        BL      HAL_CounterRead
01      MOV     v1, a1
        BL      HAL_CounterRead
        CMP     a1, v1
        BLS     %BT01
01      MOV     v1, a1
        BL      HAL_CounterRead
        CMP     a1, v1
        BLS     %BT01

        Pull    "v1,pc"

Ben Avison's avatar
Ben Avison committed
320

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
        MACRO
$class  HALDeviceField $field, $value
        LCLS    myvalue
      [ "$value" = ""
myvalue SETS    "$field"
      |
myvalue SETS    "$value"
      ]
        ASSERT  . - %A0 = HALDevice_$class$field
     [ ?HALDevice_$class$field = 2
        DCW     $myvalue
   ELIF ?HALDevice_$class$field = 4
        DCD     $myvalue
      |
        %       ?HALDevice_$class$field
      ]
        MEND

; Template for device block

Template
0
        HALDeviceField Type,               HALDeviceType_ExpCtl + HALDeviceExpCtl_SDIO
        HALDeviceField ID,                 HALDeviceID_SDIO_SDHCI
        HALDeviceField Location,           HALDeviceBus_Sys + HALDeviceSysBus_AHB ; judging from the name of the unobtainable Arasan spec!
        HALDeviceField Version,            0
        HALDeviceField Description
        HALDeviceField Address,            0 ; patched up at initialisation
        HALDeviceField Reserved1,          0
Ben Avison's avatar
Ben Avison committed
350
        HALDeviceField Activate,           0 ; patched up at initialisation
351 352 353
        HALDeviceField Deactivate
        HALDeviceField Reset
        HALDeviceField Sleep
Ben Avison's avatar
Ben Avison committed
354
        HALDeviceField Device,             iDev_GPU_SDIO ; patched up at initialisation
355 356 357 358 359 360
        HALDeviceField TestIRQ
        HALDeviceField ClearIRQ,           0
        HALDeviceField Reserved2,          0
SDHCI   HALDeviceField Flags,              HALDeviceSDHCI_Flag_32bit
SDHCI   HALDeviceField Slots,              1
SDHCI   HALDeviceField SlotInfo,           0 ; patched up at initialisation
Ben Avison's avatar
Ben Avison committed
361
SDHCI   HALDeviceField WriteRegister       ; patched up at initialisation
362
SDHCI   HALDeviceField GetCapabilities,    0 ; patched up at initialisation
363
SDHCI   HALDeviceField GetVddCapabilities, 0 ; our fake capabilities register suffices already
Ben Avison's avatar
Ben Avison committed
364
SDHCI   HALDeviceField SetVdd              ; patched up at initialisation
Ben Avison's avatar
Ben Avison committed
365
SDHCI   HALDeviceField SetBusMode,         0
366 367
SDHCI   HALDeviceField PostPowerOn,        0
SDHCI   HALDeviceField SetBusWidth,        0
Ben Avison's avatar
Ben Avison committed
368 369 370
SDHCI   HALDeviceField GetMaxCurrent       ; patched up at initialisation
SDHCI   HALDeviceField SetSDCLK            ; patched up at initialisation
SDHCI   HALDeviceField GetTMCLK            ; patched up at initialisation
Ben Avison's avatar
Ben Avison committed
371 372
SDHCI   HALDeviceField SetActivity,        0 ; patched up at initialisation
SDHCI   HALDeviceField GetCardDetect,      0 ; patched up at initialisation
373 374 375 376 377 378
SDHCI   HALDeviceField GetWriteProtect


        ; Init the SDHCI HAL device
SDIO_InitDevices ROUT
        Push    "lr"
Ben Avison's avatar
Ben Avison committed
379
        LDR     a1, PeriBase
Ben Avison's avatar
Ben Avison committed
380

Ben Avison's avatar
Ben Avison committed
381 382 383 384 385 386 387 388 389
        CPUDetect lr
        ADDLS   a1, a1, #EMMC_Base
        ADDHI   a1, a1, #EMMC2_Base
        STR     a1, SDIO_Address

        ADR     lr, %FT00
        BLS     MeasureSpeed
        BHI     Init_BCM2838
00
390 391 392 393 394
        ADR     a1, SDHCIDevice
        ADR     a2, Template
        MOV     a3, #HALDevice_SDHCISize
        BL      memcpy

Ben Avison's avatar
Ben Avison committed
395
        LDR     a3, Board_Revision
ROOL's avatar
ROOL committed
396
        BIC     a3, a3, #BoardRevision_User_Mask ; mask off overclocking / user bits
397 398
        TST     a3, #BoardRevision_NewScheme
        BNE     %FT01
Jeffrey Lee's avatar
Jeffrey Lee committed
399 400 401 402
        ASSERT  BoardRevision_AorB_Last < BoardRevision_BPlus_Sony
        CMP     a3, #BoardRevision_BPlus_Sony
        TEQ     a3, #BoardRevision_Compute_Sony
        TEQNE   a3, #BoardRevision_Compute_Embest
Ben Avison's avatar
Ben Avison committed
403 404 405
        MOVLO   a3, #BoardRevision_Model_B       :SHR: BoardRevision_Model_Shift
        MOVEQ   a3, #BoardRevision_Model_Compute :SHR: BoardRevision_Model_Shift
        MOVHI   a3, #BoardRevision_Model_BPlus   :SHR: BoardRevision_Model_Shift
406 407
        B       %FT02
01      AND     a3, a3, #BoardRevision_Model_Mask
Ben Avison's avatar
Ben Avison committed
408
        MOV     a3, a3, LSR #BoardRevision_Model_Shift
409 410
        ASSERT  BoardRevision_Model_A < BoardRevision_Model_APlus
        ASSERT  BoardRevision_Model_B < BoardRevision_Model_APlus
Ben Avison's avatar
Ben Avison committed
411 412 413 414 415
        CMP     a3, #BoardRevision_Model_APlus        :SHR: BoardRevision_Model_Shift
        TEQ     a3, #BoardRevision_Model_Compute      :SHR: BoardRevision_Model_Shift
        TEQNE   a3, #BoardRevision_Model_Compute3     :SHR: BoardRevision_Model_Shift
        TEQNE   a3, #BoardRevision_Model_Compute3Plus :SHR: BoardRevision_Model_Shift
        TEQNE   a3, #BoardRevision_Model_Compute4     :SHR: BoardRevision_Model_Shift
Ben Avison's avatar
Ben Avison committed
416
02      ; So now LO => model A or B, EQ => Compute or Compute3, HI => A+, B+ or Pi 2 or Pi 3 or Pi 0 or Pi 4
Ben Avison's avatar
Ben Avison committed
417 418

        ; Activate
419
        ADRLO   a1, Activate_AB
420 421
        ADRHI   a1, Activate_BPlus
        ADREQ   a1, Activate_Compute
Ben Avison's avatar
Ben Avison committed
422 423
        STR     a1, SDHCIDevice + HALDevice_Activate

424
        ; Address and SlotInfo_StdRegs
Ben Avison's avatar
Ben Avison committed
425
        LDR     a1, SDIO_Address
426 427 428 429
        STR     a1, SDHCIDevice + HALDevice_Address
        STR     a1, SDHCISlotInfo + HALDeviceSDHCI_SlotInfo_StdRegs

        ; SlotInfo
430
        ADRL    a1, SDHCISlotInfo
431 432 433 434
        STR     a1, SDHCIDevice + HALDevice_SDHCISlotInfo

        ; SlotInfo_Flags
        MOV     a1, #HALDeviceSDHCI_SlotFlag_Bus4Bit
435 436
        ORRHI   a1, a1, #HALDeviceSDHCI_SlotFlag_NoCardDetect
        ORREQ   a1, a1, #HALDeviceSDHCI_SlotFlag_IntegratedMem
437
        BNE     %FT20
Ben Avison's avatar
Ben Avison committed
438 439 440
        TEQ     a3, #BoardRevision_Model_Compute3     :SHR: BoardRevision_Model_Shift
        TEQNE   a3, #BoardRevision_Model_Compute3Plus :SHR: BoardRevision_Model_Shift
        TEQNE   a3, #BoardRevision_Model_Compute4     :SHR: BoardRevision_Model_Shift
441 442 443 444 445 446 447 448 449
        BNE     %FT10
        LDR     a4, SafetyCatch ; Here iff a Compute Module 3
                                ; Unhelpfully the revision number for both CM3 and CM3L is the same, but we need
                                ; to be sure whether the SDIO attached memory is fixed or removable.
        TEQ     a4, #0
        BICNE   a1, a1, #HALDeviceSDHCI_SlotFlag_IntegratedMem
10
        MOVS    a4, #0 ; put back EQ status
20
450 451
        STR     a1, SDHCISlotInfo + HALDeviceSDHCI_SlotInfo_Flags

452 453
        ; GetCapabilities
        ADR     a1, GetCapabilities_AB_BPlus
454
        ADDEQ   a1, a1, #GetCapabilities_Compute - GetCapabilities_AB_BPlus
455 456
        STR     a1, SDHCIDevice + HALDevice_SDHCIGetCapabilities

Ben Avison's avatar
Ben Avison committed
457 458
        ; SetActivity and GetCardDetect
        ADRL    a1, SetActivity_AB
459 460
        ADDHI   a1, a1, #SetActivity_BPlus - SetActivity_AB
        ADDEQ   a1, a1, #SetActivity_Compute - SetActivity_AB
Ben Avison's avatar
Ben Avison committed
461
        ADRL    a2, GetCardDetect_AB
462
        ADDHS   a2, a2, #GetCardDetect_BPlus_Compute - GetCardDetect_AB
Ben Avison's avatar
Ben Avison committed
463 464 465
        TEQ     a3, #BoardRevision_Model_B3     :SHR: BoardRevision_Model_Shift
        TEQNE   a3, #BoardRevision_Model_B3Plus :SHR: BoardRevision_Model_Shift
        TEQNE   a3, #BoardRevision_Model_A3Plus :SHR: BoardRevision_Model_Shift
466
        ADREQL  a1, SetActivity_B3
Ben Avison's avatar
Ben Avison committed
467
        TEQ     a3, #BoardRevision_Model_B4     :SHR: BoardRevision_Model_Shift
Ben Avison's avatar
Ben Avison committed
468
        ADREQL  a1, SetActivity_B4
Ben Avison's avatar
Ben Avison committed
469 470 471
        STR     a1, SDHCIDevice + HALDevice_SDHCISetActivity
        STR     a2, SDHCIDevice + HALDevice_SDHCIGetCardDetect

Ben Avison's avatar
Ben Avison committed
472
        ; Don't do pin muxing in Activate for any of these boards either
Ben Avison's avatar
Ben Avison committed
473 474 475
        TEQ     a3, #BoardRevision_Model_B3     :SHR: BoardRevision_Model_Shift
        TEQNE   a3, #BoardRevision_Model_B3Plus :SHR: BoardRevision_Model_Shift
        TEQNE   a3, #BoardRevision_Model_A3Plus :SHR: BoardRevision_Model_Shift
Ben Avison's avatar
Ben Avison committed
476 477
        ADREQ   a1, Activate_Compute
        STREQ   a1, SDHCIDevice + HALDevice_Activate
Ben Avison's avatar
Ben Avison committed
478 479 480
        CMP     a3, #BoardRevision_Model_B4     :SHR: BoardRevision_Model_Shift
        ADRHS   a1, Activate_B4
        STRHS   a1, SDHCIDevice + HALDevice_Activate
Ben Avison's avatar
Ben Avison committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

        ; Patch a few things up for Pi 4...

        ; Device
        CPUDetect a1
        MOVHI   a1, #iDev_GPU_VCSDIO
        STRHI   a1, SDHCIDevice + HALDevice_Device
        MOVHI   a1, #0

        ; WriteRegister - assume this hardware bug has been fixed now
        STRHI   a1, SDHCIDevice + HALDevice_SDHCIWriteRegister

        ; Most of the registers now strictly follow the SDHCI standard!!
        STRHI   a1, SDHCIDevice + HALDevice_SDHCIGetCapabilities
        STRHI   a1, SDHCIDevice + HALDevice_SDHCISetVdd
        STRHI   a1, SDHCIDevice + HALDevice_SDHCIGetMaxCurrent
        STRHI   a1, SDHCIDevice + HALDevice_SDHCISetSDCLK
        STRHI   a1, SDHCIDevice + HALDevice_SDHCIGetTMCLK
Ben Avison's avatar
Ben Avison committed
499

500 501 502
        MOV     a1, #0 ; flags
        ADR     a2, SDHCIDevice
        Pull    "lr"
503
        CallOS  OS_AddDevice, tailcall
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527

        ; a1 = GPIO pin
        ; a2 = function select number
PinMux_SetFunction ROUT
        Push    "lr"
        MOV     ip, #32 / GPIO_FSEL_SHIFT
        DivRem  a3, a1, ip, lr ; a3 = register, a1 = field in register
        ASSERT  GPIO_FSEL_SHIFT = 3
        ADD     a1, a1, a1, LSL #1

        LDR     a4, PeriBase
        ADD     a4, a4, #GPIO_Base
        ADD     a4, a4, #GPFSel0
        MOV     ip, #GPIO_FSEL_MASK
        MOV     ip, ip, LSL a1
        LDR     lr, [a4, a3, LSL #2]
        BIC     lr, lr, ip
        ORR     lr, lr, a2, LSL a1
        STR     lr, [a4, a3, LSL #2]
        Pull    "pc"

        ; a1 = GPIO pin
        ; a2 = pull type
GPIO_SetPull ROUT
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
        Push    "v1-v3,lr"
        MOV     v2, a2
        CMP     a1, #32      ; Select a port
        AND     v1, a1, #&1F ; Pin on the port
        ADRCCL  v3, GPIO0Device
        ADRCSL  v3, GPIO1Device

        TEQ     v2, #GPIO_PUD_OFF
        BEQ     %FT10

        ; Do the up/down
        MOV     ip, #1
        MOV     a2, ip, LSL v1
        TEQ     v2, #GPIO_PUD_UP
        MOVEQ   a3, ip, LSL v1
        MOVNE   a3, #0
        LDR     ip, [v3, #HALDevice_GPIOPullDirection]
        MOV     a1, v3
        BLX     ip
10
        ; Do the enable/disable
        MOV     ip, #1
        MOV     a2, ip, LSL v1
        TEQ     v2, #GPIO_PUD_OFF
        MOVNE   a3, ip, LSL v1
        MOVEQ   a3, #0
        LDR     ip, [v3, #HALDevice_GPIOPullControl]
        MOV     a1, v3
        BLX     ip
        Pull    "v1-v3,pc"
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593

        ; a1 = GPIO pin
        ; a2 = value to set (0 or 1)
GPIO_Output ROUT
        LDR     ip, PeriBase
        TEQ     a2, #0
        ADD     ip, ip, #GPIO_Base
        ADDEQ   ip, ip, #GPClr0
        ADDNE   ip, ip, #GPSet0
        MOV     a4, #1
        MOV     a2, a1, LSR #5
        AND     a1, a1, #&1F
        MOV     a4, a4, LSL a1
        STR     a4, [ip, a2, LSL #2]
        MOV     pc, lr

        ; a1 = GPIO pin
        ; on exit, a1 = 0 or 1
GPIO_Input ROUT
        LDR     ip, PeriBase
        MOV     a4, #1
        MOV     a2, a1, LSR #5
        AND     a1, a1, #&1F
        ADD     ip, ip, #GPIO_Base
        ADD     ip, ip, #GPLev0
        LDR     a3, [ip, a2, LSL #2]
        AND     a1, a4, a3, LSR a1
        MOV     pc, lr

Description DATA
        =       "Arasan SD host controller", 0
        ALIGN

NOPEntry ROUT
        MOV     pc, lr

Ben Avison's avatar
Ben Avison committed
594
Activate_AB ROUT
595 596 597 598 599 600 601 602 603 604 605 606 607 608
        Push    "sb,lr"
        SUB     sb, a1, #:INDEX:SDHCIDevice

        MOV     lr, #0
        STR     lr, SDHCILastWriteCount

        ; SDCLK has to be set up before any register writes are done - it's not
        ; reasonable to expect SDIODriver to know this requirement
        MOV     a3, #MIN_SDCLK
        BL      SetSDCLK

        DoMemBarrier a1 ; switch to GPIO peripheral

        ; Set up pin mux registers and GPIO directions
Ben Avison's avatar
Ben Avison committed
609
        MOV     a1, #GPIO_CD_AB
610 611
        MOV     a2, #GPIO_FSEL_INPUT
        BL      PinMux_SetFunction
Ben Avison's avatar
Ben Avison committed
612
        MOV     a1, #GPIO_CD_AB
613 614
        MOV     a2, #GPIO_PUD_UP
        BL      GPIO_SetPull
Ben Avison's avatar
Ben Avison committed
615
        MOV     a1, #GPIO_STATUS_LED_AB
616 617
        MOV     a2, #GPIO_FSEL_OUTPUT
        BL      PinMux_SetFunction
Ben Avison's avatar
Ben Avison committed
618
        MOV     a1, #GPIO_STATUS_LED_AB
619 620
        MOV     a2, #GPIO_PUD_OFF
        BL      GPIO_SetPull
Ben Avison's avatar
Ben Avison committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
        B       Activate_Common

Activate_BPlus ROUT
        Push    "sb,lr"
        SUB     sb, a1, #:INDEX:SDHCIDevice

        MOV     lr, #0
        STR     lr, SDHCILastWriteCount

        ; SDCLK has to be set up before any register writes are done - it's not
        ; reasonable to expect SDIODriver to know this requirement
        MOV     a3, #MIN_SDCLK
        BL      SetSDCLK

        DoMemBarrier a1 ; switch to GPIO peripheral
636

Ben Avison's avatar
Ben Avison committed
637 638 639 640 641 642 643
        ; Set up pin mux registers and GPIO directions
        MOV     a1, #GPIO_STATUS_LED_BPlus
        MOV     a2, #GPIO_FSEL_OUTPUT
        BL      PinMux_SetFunction
        MOV     a1, #GPIO_STATUS_LED_BPlus
        MOV     a2, #GPIO_PUD_OFF
        BL      GPIO_SetPull
644 645 646 647 648 649 650 651 652 653 654 655 656 657
        B       Activate_Common

Activate_Compute ROUT
        Push    "sb,lr"
        SUB     sb, a1, #:INDEX:SDHCIDevice

        MOV     lr, #0
        STR     lr, SDHCILastWriteCount

        ; SDCLK has to be set up before any register writes are done - it's not
        ; reasonable to expect SDIODriver to know this requirement
        MOV     a3, #MIN_SDCLK
        BL      SetSDCLK

Ben Avison's avatar
Ben Avison committed
658 659 660 661 662 663 664 665 666
        DoMemBarrier a1 ; switch to GPIO peripheral
        B       Activate_Common

Activate_B4 ROUT
        Push    "sb,lr"
        SUB     sb, a1, #:INDEX:SDHCIDevice

        ; Assume requirement to preinitialise SDCLK has gone away

667
        DoMemBarrier a1 ; switch to GPIO peripheral
Ben Avison's avatar
Ben Avison committed
668 669
        ; drop through...
Activate_Common
Ben Avison's avatar
Ben Avison committed
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
        MOV     a1, #GPIO_DAT3
        MOV     a2, #GPIO_PUD_UP
        BL      GPIO_SetPull
        MOV     a1, #GPIO_DAT2
        MOV     a2, #GPIO_PUD_UP
        BL      GPIO_SetPull
        MOV     a1, #GPIO_DAT1
        MOV     a2, #GPIO_PUD_UP
        BL      GPIO_SetPull
        MOV     a1, #GPIO_DAT0
        MOV     a2, #GPIO_PUD_UP
        BL      GPIO_SetPull
        MOV     a1, #GPIO_CMD
        MOV     a2, #GPIO_PUD_UP
        BL      GPIO_SetPull
        MOV     a1, #GPIO_CLK
        MOV     a2, #GPIO_PUD_UP
        BL      GPIO_SetPull

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 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
        DoMemBarrier a1 ; back to SDHCI peripheral

        MOV     a1, #1 ; always succeed
        Pull    "sb,pc"

Deactivate * NOPEntry

Reset * NOPEntry ; I don't think there's any way for us to reset the controller

Sleep ROUT
        MOV     a1, #0
        MOV     pc, lr

TestIRQ ROUT
        ; Not a shared interrupt, so it must be our fault
        MOV     a1, #1
        MOV     pc, lr

WriteRegister ROUT
        ; The BCM2835 has a bug which means that we have to leave 2 SDCLK cycles
        ; between each SDHCI register write, except for those to the data port.
        ; This entry point isn't used for data port writes, so there is no need
        ; to filter them out.
        Push    "v1-v4,sb,lr"
        SUB     sb, a1, #:INDEX:SDHCIDevice
        MOV     v3, a3
        MOV     v4, a4

        BL      HAL_CounterRead
        LDR     v1, SDHCILastWriteCount ; v1 = upper limit of pause period
        LDR     v2, SDHCIWriteInterval
        SUBS    v2, v1, v2              ; v2 = lower limit of pause period
        BPL     %F30

        ; The precise value of the counter frequency is liable to change over
        ; time to correct for clock drift, so if our delay includes the wrap
        ; point then we have to be careful to avoid proportionately large errors.
        ; The approach we take is to wait until a wrap happens, then do an
        ; additional wait for the remainder of the time, starting from the count
        ; we read back from the hardware.
        B       %F20
10      BL      HAL_CounterRead
20      CMP     a1, v1
        BLS     %B10
        MOV     v1, a1
        ADD     v2, v2, v1

        ; Loop while counter <= upper limit and counter > lower limit
30      ADD     v1, v1, #1
        B       %F50
40      BL      HAL_CounterRead
50      CMP     a1, v1
        CMPLO   v2, a1
        BLO     %B40

        ; Now we can do the register write
        STR     v4, [v3]

        ; Remember the time at which we did the write
        BL      HAL_CounterRead
        STR     a1, SDHCILastWriteCount
        Pull    "v1-v4,sb,pc"

752
GetCapabilities_AB_BPlus ROUT
753 754 755
        ; Vss is fixed in hardware - see schematics
        ; Eben says high bus speed doesn't work, but Linux seems to manage?
        ; Buffer size is mentioned in the prose in the BCM2835 ARM peripherals datasheet
Ben Avison's avatar
Ben Avison committed
756
        LDR     a1, =CAP_VS33 :OR: CAP_HSS :OR: CAP_MBL_1024
757 758
        MOV     pc, lr

759 760 761 762 763 764 765 766 767 768 769
GetCapabilities_Compute ROUT
        ; The eMMC chip doesn't seem to be compatible with the BCM2835's high bus speed timings.
        ; We get command line conflicts unless we set HostControl1's HSE bit (which we don't
        ; normally do due to compatibility issues with some 50 MHz MMCplus cards and 52 MHz
        ; SD cards, which otherwise function on the Pis with card slots - see SDIODriver.c.probe
        ; for more). But even if we do set HSE, data transfer is too unreliable for practical
        ; use with the eMMC. The easiest workaround is to alter the fake capabilities register
        ; to claim that high speed isn't supported by the controller.
        LDR     a1, =CAP_VS33 :OR: CAP_MBL_1024
        MOV     pc, lr

770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
SetVdd * NOPEntry ; There is no software control of Vdd for this board

GetMaxCurrent ROUT
        ; Can't find any information on this, so just stick with a low value
        MOV     a1, #100
        MOV     pc, lr

        ; In: a1 = requested frequency, kHz
        ; Out: v1 = how many counter ticks we have to see to guarantee 2 SDCLK cycles have passed
        ;      v2 = divider to use
        ;      v3 = actual frequency achieved, kHz
        ;      a2,a4,ip corrupted
CalcTimings ROUT
        ; By experimentation, I've determined that the 40 MHz input clock
        ; can be divided by any integer from 1 to 1023.
Ben Avison's avatar
Ben Avison committed
785
        LDR     ip, SDHCIInputClock
786
        ADD     ip, ip, a1 ; input clock, adjusted to ensure round up
Ben Avison's avatar
Ben Avison committed
787
        SUB     ip, ip, #1
788 789 790 791 792 793 794 795 796
        DivRem  v2, ip, a1, a2
        CMP     v2, #1
        MOVLO   v2, #1
        MOV     ip, #1024
        CMP     v2, #1024
        SUBHS   v2, ip, #1

        ; v2 now contains the final divisor. Work out what frequency that
        ; corresponds to.
Ben Avison's avatar
Ben Avison committed
797
        LDR     ip, SDHCIInputClock
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
        DivRem  v3, ip, v2, a2

        ; Work out how many counter ticks we have to see to guarantee 2 SDCLK cycles
        ; between each register write. This is 2 * (counter-freq / SDCLK-freq)
        ; with a round up, or (2 * counter-freq + SDCLK-freq - 1) / SDCLK-freq
        ; Also add 1 to allow for the fact that when the first count is read, we
        ; don't know how close to the end of that counter period it is.
        MOV     ip, #1000
        MUL     a4, v3, ip
        LDR     ip, =REGISTER_WRITE_PACE * TIMER_RATE - 1
        ADD     ip, ip, a4
        DivRem  v1, ip, a4, a2
        ADD     v1, v1, #1
        MOV     pc, lr

SetSDCLK ROUT
        Push    "v1-v5,sb,lr"
        SUB     sb, a1, #:INDEX:SDHCIDevice

        ; Set a long gap between writes while we're fiddling with the SDCLK
        MOV     a1, #MIN_SDCLK
        BL      CalcTimings
Ben Avison's avatar
Ben Avison committed
820
        STR     v1, SDHCIWriteInterval
821 822 823 824 825 826 827

        ; Calculate desired values
        MOV     a1, a3
        BL      CalcTimings
        ; v1 = interval, v2 = divider, v3 = freq

        ; Program clock control register
Ben Avison's avatar
Ben Avison committed
828
        LDR     v5, SDIO_Address
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 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
        LDR     v4, [v5, #CONTROL1]!

        BIC     v4, v4, #CONTROL1_CLK_EN ; stop clock going to card
        ADR     a1, SDHCIDevice
        MOV     a3, v5
        MOV     a4, v4
        BL      WriteRegister

        BIC     v4, v4, #CONTROL1_CLK_INTLEN ; stop clock
        ADR     a1, SDHCIDevice
        MOV     a3, v5
        MOV     a4, v4
        BL      WriteRegister

        BIC     v4, v4, #CONTROL1_CLK_FREQ8_MASK
        BIC     v4, v4, #CONTROL1_CLK_FREQ_MS2_MASK
        MOV     a1, v2, LSL #CONTROL1_CLK_FREQ8_SHIFT - 0
        MOV     a2, v2, LSR #8 - CONTROL1_CLK_FREQ_MS2_SHIFT
        AND     a1, a1, #CONTROL1_CLK_FREQ8_MASK
        AND     a2, a2, #CONTROL1_CLK_FREQ_MS2_MASK
        ORR     v4, v4, a1
        ORR     v4, v4, a2
        ADR     a1, SDHCIDevice
        MOV     a3, v5
        MOV     a4, v4
        BL      WriteRegister

        ORR     v4, v4, #CONTROL1_CLK_INTLEN ; start clock
        ADR     a1, SDHCIDevice
        MOV     a3, v5
        MOV     a4, v4
        BL      WriteRegister

        ; Wait for up to 20 ms for the clock to stabilise
        ; v2 = previous counter value
        ; v4 = time remaining
        BL     HAL_CounterRead
        MOV    v2, a1
        LDR    v4, =(TIMER_RATE * CLOCKSET_TIMEOUT) / 1000
70      LDR    a4, [v5]
        CMP    v4, #0
        BMI    %F80 ; timed out (hope this never happens!)
        TST    a4, #CONTROL1_CLK_STABLE
        BNE    %F80 ; stabilised
        BL     HAL_CounterRead
        SUBS   a2, a1, v2 ; elapsed since last time - if -ve then we have wrap so do nothing
        SUBPL  v4, v4, a2
        MOV    v2, a1
        B      %B70

80      ; Re-enable clock
        ADR     a1, SDHCIDevice
        MOV     a3, v5
        ORR     a4, a4, #CONTROL1_CLK_EN
        BL      WriteRegister

        ; Now we can save the final gap
        STR     v1, SDHCIWriteInterval
        ; Return the actual frequency
        MOV     a1, v3
        Pull    "v1-v5,sb,pc"

GetTMCLK ROUT
        ; SDCLK is reused for TMCLK
        Push    "sb,lr"
        SUB     sb, a1, #:INDEX:SDHCIDevice
Ben Avison's avatar
Ben Avison committed
895
        LDR     a1, SDIO_Address
896 897 898 899 900
        LDR     a2, [a1, #CONTROL1]
        AND     a3, a2, #CONTROL1_CLK_FREQ8_MASK
        AND     a4, a2, #CONTROL1_CLK_FREQ_MS2_MASK
        MOV     a3, a3, LSR #CONTROL1_CLK_FREQ8_SHIFT - 0
        ORR     a3, a3, a4, LSL #8 - CONTROL1_CLK_FREQ_MS2_SHIFT
Ben Avison's avatar
Ben Avison committed
901
        LDR     a2, SDHCIInputClock
902 903 904
        DivRem  a1, a2, a3, a4
        Pull    "sb,pc"

Ben Avison's avatar
Ben Avison committed
905
SetActivity_AB ROUT
906 907 908 909 910 911 912 913
        Push    "sb,lr"
        DoMemBarrier lr ; switch to GPIO peripheral
        SUB     sb, a1, #:INDEX:SDHCIDevice

        ; Line is active low
        TEQ     a3, #HALDeviceSDHCI_ActivityOff
        MOVEQ   a2, #1
        MOVNE   a2, #0
Ben Avison's avatar
Ben Avison committed
914
        MOV     a1, #GPIO_STATUS_LED_AB
915 916 917 918 919
        BL      GPIO_Output

        DoMemBarrier lr ; back to SDHCI peripheral
        Pull    "sb,pc"

Ben Avison's avatar
Ben Avison committed
920
SetActivity_BPlus ROUT
921 922 923 924
        Push    "sb,lr"
        DoMemBarrier lr ; switch to GPIO peripheral
        SUB     sb, a1, #:INDEX:SDHCIDevice

Ben Avison's avatar
Ben Avison committed
925 926 927 928 929 930 931 932 933 934
        ; Line is active high
        ASSERT  HALDeviceSDHCI_ActivityOff = 0
        MOVS    a2, a3
        MOVNE   a2, #1
        MOV     a1, #GPIO_STATUS_LED_BPlus
        BL      GPIO_Output

        DoMemBarrier lr ; back to SDHCI peripheral
        Pull    "sb,pc"

935 936 937 938
SetActivity_Compute ROUT
        ; No standard way of wiring up the activity LED on Compute
        MOV     pc, lr

939 940 941 942 943 944 945 946 947 948
SetActivity_B3 ROUT
        Push    "sb,lr"
        SUB     sb, a1, #:INDEX:SDHCIDevice
        LDR     a1, VirtGPIOBuf
        CMP     a1, #0
        Pull    "sb,pc",EQ
        ; The virtual GPIO buffer uses some funny counter system to track enable/disable state changes. If we don't follow its rules, it won't work.
        LDR     a2, [a1]
        SXTH    a4, a2, ROR #16 ; Enable count
        SXTH    a2, a2 ; Disable count
949 950
        SUB     lr, a4, a2
        SXTH    lr, lr ; Difference
951 952
        TEQ     a3, #HALDeviceSDHCI_ActivityOff
        BEQ     %FT50
953
        CMP     lr, #0
954 955 956 957
        Pull    "sb,pc", GT ; Don't update if already on
        ADD     a4, a4, #1
        B       %FT90
50
958
        CMP     lr, #0
959 960 961 962 963 964 965
        Pull    "sb,pc", LE ; Don't update if already off
        ADD     a2, a2, #1
90
        PKHBT   a2, a2, a4, LSL #16
        STR     a2, [a1]
        Pull    "sb,pc"

Ben Avison's avatar
Ben Avison committed
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
SetActivity_B4 ROUT
        Push    "sb,lr"
        DoMemBarrier lr ; switch to GPIO peripheral
        SUB     sb, a1, #:INDEX:SDHCIDevice

        ; Line is active high
        ASSERT  HALDeviceSDHCI_ActivityOff = 0
        MOVS    a2, a3
        MOVNE   a2, #1
        MOV     a1, #GPIO_STATUS_LED_B4
        BL      GPIO_Output

        DoMemBarrier lr ; back to SDHCI peripheral
        Pull    "sb,pc"

Ben Avison's avatar
Ben Avison committed
981 982 983 984 985 986
GetCardDetect_AB ROUT
        Push    "sb,lr"
        DoMemBarrier lr ; switch to GPIO peripheral
        SUB     sb, a1, #:INDEX:SDHCIDevice

        MOV     a1, #GPIO_CD_AB
987 988 989 990 991 992 993 994
        BL      GPIO_Input
        ; Line is active low, so invert
        SUB     a1, a1, #1
        AND     a1, a1, #1

        DoMemBarrier lr ; back to SDHCI peripheral
        Pull    "sb,pc"

995
GetCardDetect_BPlus_Compute ROUT
Ben Avison's avatar
Ben Avison committed
996 997 998 999 1000
        ; CD is not connected. This entry shouldn't be called, but just in case
        ; (perhaps someone softloaded old SDFSDriver/SDFS?) report card present
        MOV     a1, #1
        MOV     pc, lr

1001 1002 1003 1004 1005 1006
GetWriteProtect ROUT
        ; WP is not connected, so just have to always report unprotected :-(
        MOV     a1, #0
        MOV     pc, lr

        END