OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 1:01 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: ATA driver hangs on initialization while polling BSY
PostPosted: Wed Feb 06, 2019 11:30 am 
Offline
User avatar

Joined: Sat May 26, 2018 5:32 pm
Posts: 22
My code for a bootloader hangs while polling BSY before pretty much everywhere I poll BSY (i.e. BSY never seems to clear).

Code:
hdd_init:
        mov al, 0x06
        mov dx, 0x3f6
        out dx, al                      ; do a software reset
        mov dx, 0x1f7
        in al, dx
        in al, dx
        in al, dx
        in al, dx                       ; 400ns delay
        in al, dx
        cmp al, 0xff
        je mfail                        ; drive missing error
poll0:  in al, dx
        test al, 0x40                   ; test DRDY to make sure the drive is ready to switch
        pause
;       jz poll0                        ; this hangs infinitely
        dec dx
        mov al, 0xa0
        out dx, al                      ; select drive 0
        mov dx, 0x1f1
        xor al, al
        out dx, al                      ; send 0 to features register
        add dx, 6
        in al, dx
        in al, dx
        in al, dx
        in al, dx                       ; 400ns delay
        in al, dx
        cmp al, 0xff
        je mfail                        ; drive missing error
        test al, 0x01
        jnz mfail                       ; drive select error
        xor al, al
       mov dx, 0x1f2
        out dx, al
        inc dx                          ; 0x1f3
        out dx, al
        inc dx                          ; 0x1f4
        out dx, al
        inc dx                          ; 0x1f5
        out dx, al
        mov al, 0xec
        add dx, 2                       ; 0x1f7
        out dx, al                      ; send identify command
        in al, dx
        in al, dx
        in al, dx
        in al, dx                       ; 400ns delay
        in al, dx
        cmp al, 0
        je mfail
        sub dx, 2                       ; 0x1f5
        in al, dx
        test al, al
        jnz pfail
        dec dx                          ; 0x1f4
        in al, dx
        test al, al
        jnz pfail
poll5:  pause
        in al, dx
        test al, 0x80
        jnz poll5                       ; wait for BSY to clear -- this may also hang
        ; now to find a gpt table
        mov dx, 0x1f2
        mov al, 1
        out dx, al                      ; sector count
        inc dx
        mov al, 1
        out dx, al                      ; low byte of LBA
        inc dx
        xor al, al
        out dx, al
        inc dx
        xor al, al
        out dx, al                      ; higher bytes of LBA
        mov dx, 0x1f7
        in al, dx
        in al, dx
        in al, dx
        in al, dx                       ; 400ns delay
poll10: pause
        in al, dx
        and al, 0xc0
        cmp al, 0x40
        jne poll10                      ; test RDY and BSY -- or this one hangs
        mov al, 0x20
        out dx, al                      ; read using LBA 28
        in al, dx
        in al, dx
        in al, dx
        in al, dx                       ; 400ns delay
        in al, dx
        test al, 0x01
        je dfail
poll20: in al, dx
        test al, 0x40                   ; test RDY to make sure the drive is spun up
        pause
        jz poll20
poll30: in al, dx
        test al, 0x80                   ; test BSY to make sure the drive is preparing to read
        pause
        jz poll30
poll40: in al, dx
        test al, 0x80                   ; test BSY to make sure the drive is ready to read
        pause
        jnz poll40
poll50: in al, dx
        test al, 0x08
        pause
        jnz poll50                      ; test RDX to make sure the drive is really ready to read


I've tried to search for similar problems, but haven't found anything that fixed it. Any help would be much appreciated.

_________________
The maker of Foxtrot microkernels. Currently just Greyhound, which is currently smaller than 3 KiB.
Also working on osmkVII.


Top
 Profile  
 
 Post subject: Re: ATA driver hangs on initialization while polling BSY
PostPosted: Wed Feb 06, 2019 3:18 pm 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 934
Location: USA
K3achas wrote:
My code for a bootloader hangs while polling BSY before pretty much everywhere I poll BSY (i.e. BSY never seems to clear).

Code:
hdd_init:
        mov al, 0x06
        mov dx, 0x3f6
        out dx, al                      ; do a software reset

First, if this is a boot loader, why are you not calling the firmware to read from the drive? i.e.: why not call the BIOS interrupt 13h services?

Second, you set the reset bit, but never clear it. If it is always in reset, of course the busy bit will be set. Set the reset bit, wait for at least 5 uS then clear it waiting at least 2 mS before doing anything else.

May I suggest a few things as well? You hard code the port addresses (0x3F6, 0x1F7, etc). It would be better to call this as a function, passing the base address and alternate base address. Also, #defines are your friend. For example:
Code:
hdd_init:
       mov  al,ATA_DEV_CNTRL_RESET | ATA_DEV_CNTRL_nINT
       mov  dx,word ptr [alt_base]
       add  dx,ATA_DEV_CONTROL
       out  dx, al           ; *****start****** a software reset

Ben
- http://www.fysnet.net/osdesign_book_series.htm


Top
 Profile  
 
 Post subject: Re: ATA driver hangs on initialization while polling BSY
PostPosted: Wed Feb 06, 2019 5:59 pm 
Offline
User avatar

Joined: Sat May 26, 2018 5:32 pm
Posts: 22
Thanks, I didn't catch that by reading the wiki. I'll make the suggested changes to clean the code. Also, I should have mentioned that it's running in long mode and only has access to the hardware itself (and the rather limited EFI runtime services).

_________________
The maker of Foxtrot microkernels. Currently just Greyhound, which is currently smaller than 3 KiB.
Also working on osmkVII.


Top
 Profile  
 
 Post subject: Re: ATA driver hangs on initialization while polling BSY
PostPosted: Thu Feb 07, 2019 7:22 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
Is there any particular reason you don't want to use the boot services in your bootloader?


Top
 Profile  
 
 Post subject: Re: ATA driver hangs on initialization while polling BSY
PostPosted: Thu Feb 07, 2019 1:20 pm 
Offline
User avatar

Joined: Sat May 26, 2018 5:32 pm
Posts: 22
The main reason is that that way I can reclaim used memory a lot easier (I don't have to, say, add a loaded program just to reclaim pages used by the bootloader), but it also helps to do the proper hardware setup in kernel mode (i.e. a bootloader, as I use a tiny microkernel architecturally limited to 8 KiB) as opposed to making it way longer in usermode. Additionally, it's harder to find resources on EFI's boot services than it is on the hardware.

_________________
The maker of Foxtrot microkernels. Currently just Greyhound, which is currently smaller than 3 KiB.
Also working on osmkVII.


Top
 Profile  
 
 Post subject: Re: ATA driver hangs on initialization while polling BSY
PostPosted: Fri Feb 08, 2019 4:58 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
I don't see how using boot services to load the kernel will change how you reclaim the bootloader's pages.

Plus, if you use boot services, you won't have to keep updating your bootloader as hardware changes. For example, you're writing an IDE driver, but most PCs use AHCI nowadays. In the future, that may be replaced by NVMe.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 62 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group