Subversion Repositories Kolibri OS

Rev

Rev 9219 | Rev 9270 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 9219 Rev 9231
Line 312... Line 312...
312
                  rb (0x100 - 0xA0) ; 0xA0, Reserved
312
                  rb (0x100 - 0xA0) ; 0xA0, Reserved
313
ends
313
ends
Line 314... Line 314...
314
 
314
 
315
; --------------------------------------------------
315
; --------------------------------------------------
-
 
316
uglobal
316
uglobal
317
 
-
 
318
align 4
-
 
319
 
317
align 4
320
struct SD_DATA
318
        ahci_controller AHCI_DATA
321
        ahci_controller  AHCI_DATA
319
        port_data_arr   rb (sizeof.PORT_DATA*AHCI_MAX_PORTS)
322
        port_data_arr    rb (sizeof.PORT_DATA*AHCI_MAX_PORTS)
-
 
323
        ahci_mutex       MUTEX
-
 
324
ends
-
 
325
 
-
 
326
sd1_data SD_DATA
-
 
327
sd2_data SD_DATA
-
 
328
sd3_data SD_DATA
-
 
329
sd4_data SD_DATA
-
 
330
sd5_data SD_DATA
-
 
331
sd6_data SD_DATA
-
 
332
sd7_data SD_DATA
-
 
333
sd8_data SD_DATA
-
 
334
 
320
        ahci_mutex      MUTEX
335
sd_data dd ?
Line 321... Line 336...
321
endg
336
endg
322
 
337
 
323
iglobal
338
iglobal
Line 331... Line 346...
331
    dd  ahci_write
346
    dd  ahci_write
332
    dd  0   ; no flush function
347
    dd  0   ; no flush function
333
    dd  0   ; use default cache size
348
    dd  0   ; use default cache size
334
.end:
349
.end:
335
hd_name db 'sd', 0, 0, 0
350
hd_name db 'sd', 0, 0, 0
336
hd_counter dd 0
351
sd_counter dd 0
-
 
352
controllers_counter dd 0
337
endg
353
endg
Line 338... Line 354...
338
 
354
 
339
; -----------------------------------------------------------------------
355
; -----------------------------------------------------------------------
340
; detect ahci controller and initialize
356
; detect ahci controller and initialize
341
align 4
357
align 4
342
ahci_init:
-
 
343
        mov     ecx, ahci_mutex
-
 
Line 344... Line 358...
344
        call    mutex_init
358
ahci_init:
345
 
359
 
-
 
360
        mov     ecx, sd1_data.ahci_controller
-
 
361
        mov     esi, pcidev_list
346
        mov     ecx, ahci_controller
362
        mov     [sd_data], sd1_data
347
        mov     esi, pcidev_list
363
        mov     [sd_counter], 0
348
.find_ahci_ctr:
364
.find_ahci_ctr:
349
        mov     esi, [esi + PCIDEV.fd]
365
        mov     esi, [esi + PCIDEV.fd]
350
        cmp     esi, pcidev_list
366
        cmp     esi, pcidev_list
Line 359... Line 375...
359
.ahci_ctr_not_found:
375
.ahci_ctr_not_found:
360
        DEBUGF  1, "K: AHCI controller not found\n"
376
        DEBUGF  1, "K: AHCI controller not found\n"
361
        ret
377
        ret
Line 362... Line 378...
362
 
378
 
-
 
379
.ahci_ctr_found:
-
 
380
        push    esi
-
 
381
 
-
 
382
        mov     ecx, [sd_data]
-
 
383
        add     ecx, SD_DATA.ahci_mutex
-
 
384
        call    mutex_init
-
 
385
 
-
 
386
        mov     ecx, [sd_data]
363
.ahci_ctr_found:
387
        add     ecx, SD_DATA.ahci_controller
Line 364... Line 388...
364
        mov     [ahci_controller + AHCI_DATA.pcidev], esi
388
        mov     [ecx + AHCI_DATA.pcidev], esi
365
 
389
 
366
        mov     eax, [esi+PCIDEV.class]
390
        mov     eax, [esi+PCIDEV.class]
367
        movzx   ebx, byte [esi+PCIDEV.bus]
391
        movzx   ebx, byte [esi+PCIDEV.bus]
Line 385... Line 409...
385
        inc     eax
409
        inc     eax
386
        DEBUGF  1, "K: AHCI: MMIO region size = 0x%x bytes\n", eax
410
        DEBUGF  1, "K: AHCI: MMIO region size = 0x%x bytes\n", eax
Line 387... Line 411...
387
 
411
 
388
        ; Map MMIO region to virtual memory
412
        ; Map MMIO region to virtual memory
-
 
413
        stdcall map_io_mem, edi, eax, PG_SWR + PG_NOCACHE
-
 
414
        push    ecx
-
 
415
        mov     ecx, [sd_data]
389
        stdcall map_io_mem, edi, eax, PG_SWR + PG_NOCACHE
416
        add     ecx, SD_DATA.ahci_controller 
-
 
417
        mov     [ecx + AHCI_DATA.abar], eax
390
        mov     [ahci_controller + AHCI_DATA.abar], eax
418
        pop     ecx
Line 391... Line 419...
391
        DEBUGF  1, "K: AHCI controller BAR5 mapped to virtual addr %x\n", eax
419
        DEBUGF  1, "K: AHCI controller BAR5 mapped to virtual addr %x\n", eax
392
 
420
 
Line 403... Line 431...
403
        btr     eax, 10 ; clear the "disable interrupts" bit
431
        btr     eax, 10 ; clear the "disable interrupts" bit
404
        DEBUGF  1, "K: AHCI: pci_status_command = %x\n", eax
432
        DEBUGF  1, "K: AHCI: pci_status_command = %x\n", eax
405
        stdcall pci_write32, ebx, ebp, PCI_REG_STATUS_COMMAND, eax
433
        stdcall pci_write32, ebx, ebp, PCI_REG_STATUS_COMMAND, eax
Line 406... Line 434...
406
 
434
 
407
        ; ; Print some register values to debug board
435
        ; ; Print some register values to debug board
408
        ; mov     esi, [ahci_controller + AHCI_DATA.abar]
436
        ; mov     esi, [[sd_data].ahci_controller + AHCI_DATA.abar]
Line 409... Line 437...
409
        ; DEBUGF  1, "K: AHCI: HBA.cap = %x, HBA.ghc = %x, HBA_MEM.version = %x\n", [esi + HBA_MEM.cap], [esi + HBA_MEM.ghc], [esi + HBA_MEM.version]
437
        ; DEBUGF  1, "K: AHCI: HBA.cap = %x, HBA.ghc = %x, HBA_MEM.version = %x\n", [esi + HBA_MEM.cap], [esi + HBA_MEM.ghc], [esi + HBA_MEM.version]
410
 
438
 
-
 
439
        ;-------------------------------------------------------
-
 
440
        ; Request BIOS/OS ownership handoff, if supported. (TODO check correctness)
-
 
441
        push    ecx
411
        ;-------------------------------------------------------
442
        mov     ecx, [sd_data]
-
 
443
        add     ecx, SD_DATA.ahci_controller
412
        ; Request BIOS/OS ownership handoff, if supported. (TODO check correctness)
444
        mov     esi, [ecx + AHCI_DATA.abar]
413
        mov     esi, [ahci_controller + AHCI_DATA.abar]
445
        pop     ecx
414
        ;mov     ebx, [esi + HBA_MEM.cap2]
446
        ;mov     ebx, [esi + HBA_MEM.cap2]
415
        ;DEBUGF  1, "K: AHCI: HBA_MEM.cap2 = %x\n", ebx
447
        ;DEBUGF  1, "K: AHCI: HBA_MEM.cap2 = %x\n", ebx
416
        bt      [esi + HBA_MEM.cap2], bit_AHCI_HBA_CAP2_BOH
448
        bt      [esi + HBA_MEM.cap2], bit_AHCI_HBA_CAP2_BOH
Line 458... Line 490...
458
        ; calculate irq line
490
        ; calculate irq line
459
        ; ahciHBA->ghc |= AHCI_GHC_IE;
491
        ; ahciHBA->ghc |= AHCI_GHC_IE;
460
        ; IDT::RegisterInterruptHandler(irq, InterruptHandler);
492
        ; IDT::RegisterInterruptHandler(irq, InterruptHandler);
461
        ; ahciHBA->is = 0xffffffff;
493
        ; ahciHBA->is = 0xffffffff;
Line 462... Line -...
462
 
-
 
463
        mov     [hd_counter], 0
494
 
464
        xor     ebx, ebx
495
        xor     ebx, ebx
465
.detect_drives:
496
.detect_drives:
466
        cmp     ebx, AHCI_MAX_PORTS
497
        cmp     ebx, AHCI_MAX_PORTS
Line 532... Line 563...
532
 
563
 
Line 533... Line 564...
533
        ; DEBUGF  1, "K: AHCI: found drive at port %d, cmd = 0x%x, ssts = 0x%x, signature = 0x%x\n", ebx, [edi + HBA_PORT.command], [edi + HBA_PORT.sata_status], [edi + HBA_PORT.signature]  
564
        ; DEBUGF  1, "K: AHCI: found drive at port %d, cmd = 0x%x, ssts = 0x%x, signature = 0x%x\n", ebx, [edi + HBA_PORT.command], [edi + HBA_PORT.sata_status], [edi + HBA_PORT.signature]  
534
 
565
 
-
 
566
        mov     ecx, ebx
535
        mov     ecx, ebx
567
        imul    ecx, sizeof.PORT_DATA
536
        imul    ecx, sizeof.PORT_DATA
568
        mov     ecx, [sd_data]
Line 537... Line 569...
537
        add     ecx, port_data_arr
569
        add     ecx, SD_DATA.port_data_arr
Line 538... Line 570...
538
        stdcall ahci_port_rebase, edi, ebx, ecx
570
        stdcall ahci_port_rebase, edi, ebx, ecx
Line 571... Line 603...
571
        ; register disk in system:
603
        ; register disk in system:
Line 572... Line 604...
572
        
604
        
Line 573... Line 605...
573
        ;stdcall ahci_read_first_sector, ecx
605
        ;stdcall ahci_read_first_sector, ecx
574
 
606
 
575
        push    ecx
607
        push    ecx
576
        mov     eax, [hd_counter]
608
        mov     eax, [sd_counter]
577
        inc     [hd_counter]
609
        inc     [sd_counter]
578
        xor     edx, edx
610
        xor     edx, edx
579
        mov     ecx, 10
611
        mov     ecx, 10
580
        div     ecx ; eax = hd_counter / 10, edx = hd_counter % 10
612
        div     ecx ; eax = sd_counter / 10, edx = sd_counter % 10
581
        test    eax, eax
613
        test    eax, eax
582
        jz      .concat_one
614
        jz      .concat_one
583
        add     al, '0'
615
        add     al, '0'
Line 613... Line 645...
613
        jmp     .detect_drives
645
        jmp     .detect_drives
Line 614... Line 646...
614
 
646
 
-
 
647
        
-
 
648
 
-
 
649
.end_detect_drives:
-
 
650
        pop     esi
-
 
651
        add     [sd_data], sizeof.SD_DATA 
-
 
652
        inc     [controllers_counter]       
615
        
-
 
616
 
-
 
617
.end_detect_drives:
653
        cmp     [controllers_counter], 8
618
        
654
        jnz     .find_ahci_ctr
Line 619... Line 655...
619
 
655
        DEBUGF  1, "AHCI: reached controllers number limit\n"
620
        ret
656
        ret
Line 1021... Line 1057...
1021
                numsectors dd ?
1057
                numsectors dd ?
1022
        endl
1058
        endl
Line 1023... Line 1059...
1023
 
1059
 
Line 1024... Line 1060...
1024
        pushad
1060
        pushad
1025
 
1061
 
Line 1026... Line 1062...
1026
        mov     ecx, ahci_mutex
1062
        mov     ecx, sd1_data.ahci_mutex
1027
        call    mutex_lock
1063
        call    mutex_lock
1028
 
1064
 
Line 1059... Line 1095...
1059
        adc     dword [startsector + 4], 0
1095
        adc     dword [startsector + 4], 0
Line 1060... Line 1096...
1060
 
1096
 
1061
        jmp     .read_loop
1097
        jmp     .read_loop
Line 1062... Line 1098...
1062
.read_loop_end:
1098
.read_loop_end:
1063
 
1099
 
Line 1064... Line 1100...
1064
        mov     ecx, ahci_mutex
1100
        mov     ecx, sd1_data.ahci_mutex
1065
        call    mutex_unlock
1101
        call    mutex_unlock
1066
 
1102
 
Line 1076... Line 1112...
1076
                numsectors dd ?
1112
                numsectors dd ?
1077
        endl
1113
        endl
Line 1078... Line 1114...
1078
 
1114
 
Line 1079... Line 1115...
1079
        pushad
1115
        pushad
1080
 
1116
 
Line 1081... Line 1117...
1081
        mov     ecx, ahci_mutex
1117
        mov     ecx, sd1_data.ahci_mutex
1082
        call    mutex_lock
1118
        call    mutex_lock
1083
 
1119
 
Line 1105... Line 1141...
1105
        adc     dword [startsector + 4], 0
1141
        adc     dword [startsector + 4], 0
Line 1106... Line 1142...
1106
 
1142
 
1107
        jmp     .write_loop
1143
        jmp     .write_loop
Line 1108... Line 1144...
1108
.write_loop_end:
1144
.write_loop_end:
1109
 
1145
 
Line 1110... Line 1146...
1110
        mov     ecx, ahci_mutex
1146
        mov     ecx, sd1_data.ahci_mutex
1111
        call    mutex_unlock
1147
        call    mutex_unlock
1112
 
1148
 
Line 1339... Line 1375...
1339
        push    ebx ecx edx esi
1375
        push    ebx ecx edx esi
1340
        ; If not set in SACT and CI, the slot is free
1376
        ; If not set in SACT and CI, the slot is free
1341
        mov     ebx, [eax + HBA_PORT.sata_active]
1377
        mov     ebx, [eax + HBA_PORT.sata_active]
1342
        or      ebx, [eax + HBA_PORT.command_issue] ; ebx = slots
1378
        or      ebx, [eax + HBA_PORT.command_issue] ; ebx = slots
Line 1343... Line 1379...
1343
 
1379
 
1344
        mov     esi, [ahci_controller + AHCI_DATA.abar]
1380
        mov     esi, [sd1_data.ahci_controller + AHCI_DATA.abar]
1345
        mov     edx, [esi + HBA_MEM.cap]
1381
        mov     edx, [esi + HBA_MEM.cap]
1346
        shr     edx, 8
1382
        shr     edx, 8
1347
        and     edx, 0xf
1383
        and     edx, 0xf
1348
        ; DEBUGF  1, "Number of Command Slots on each port = %u\n", edx
1384
        ; DEBUGF  1, "Number of Command Slots on each port = %u\n", edx