\ ========== Copyright Header Begin ==========================================
\ Hypervisor Software File: ide.fth
\ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
\ - Do no alter or remove copyright notices
\ - Redistribution and use of this software in source and binary forms, with
\ or without modification, are permitted provided that the following
\ - Redistribution of source code must retain the above copyright notice,
\ this list of conditions and the following disclaimer.
\ - Redistribution 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 Sun Microsystems, Inc. or the names of contributors
\ may be used to endorse or promote products derived from this software
\ without specific prior written permission.
\ This software is provided "AS IS," without a warranty of any kind.
\ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
\ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
\ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
\ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
\ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
\ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
\ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
\ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
\ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
\ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
\ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
\ You acknowledge that this software is not designed, licensed or
\ intended for use in the design, construction, operation or maintenance of
\ ========== Copyright Header End ============================================
id: @(#)ide.fth 1.21 06/07/14
copyright: Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
copyright: Use is subject to license terms.
2 encode-int " #address-cells" property
0 instance value cmd-regs
0 instance value ctrl-regs
0 instance value interface \ which interface
0 instance value disk-id \ which drive
0 value secondary? \ secondary interface present?
: ata! ( data reg -- ) cmd-regs + rb! ;
: ata@ ( reg -- data ) cmd-regs + rb@ ;
: ctrl! ( data -- ) ctrl-regs + rb! ;
: ctrl@ ( -- data ) ctrl-regs + rb@ ;
: data! ( data -- ) wbflip cmd-regs rw! ;
: data@ ( -- data ) cmd-regs rw@ wbflip ;
: err@ ( -- data ) h# 1 ata@ ;
: cmd! ( data -- ) h# 7 ata! ;
: stat@ ( -- data ) h# 7 ata@ ;
: astat@ ( -- data ) h# 2 ctrl@ ; \ Alternate status reg
: .rd-ide-block ( regs addr bytes -- ) bounds do data@ i w! 2 +loop drop ;
: .wr-ide-block ( regs addr bytes -- ) bounds do i w@ data! 2 +loop drop ;
: (read) ( -- ) ['] .rd-ide-block is xfer-fn ;
: (write) ( -- ) ['] .wr-ide-block is xfer-fn ;
\ Checking IDE interrupts is device/vendor specific..
defer ide-irq? ( -- flag? ) ' false is ide-irq?
\ These bits must be set for commands to work.
h# E0 constant cmd-reg-bits
\ h# A0 constant cmd-reg-bits
d# 5000 constant reset-bsy-timeout
0 instance value blocksize
: .inform ( str,len -- ) type space ;
: .drive " drive" .inform ;
: .master " Master" .inform ;
: .slave " Slave" .inform ;
: .interface " interface" .inform ;
: .primary " Primary" .inform ;
: .secondary " Secondary" .inform ;
: .number " number" .inform ;
: .invalid-ide " Invalid IDE" .inform ;
: .diag-msg ." Failed Diagnostic" cr ;
0 instance value timeout?
instance defer check-bits
\ Define a simple usec counter
[ifndef] have-usec-fcode?
: us ( n -- ) looptime * 0 ?do loop ;
d# 3.000.000 dup is looptime ( n )
get-msecs looptime 0 ?do loop get-msecs swap - ( n ms )
d# 1000 * / 1 max is looptime
: xfer-wait-status ( timeout check-acf -- ok? )
false is timeout? ( timeout check-acf )
stat@ check-bits if unloop true exit then
true is timeout? ( failed )
: wait-status ( timeout check-acf -- ok? )
false is timeout? ( timeout check-acf )
stat@ check-bits if unloop true exit then
true is timeout? ( failed )
: alt-wait-status ( timeout check-acf -- ok? )
false is timeout? ( timeout check-acf )
astat@ check-bits if unloop true exit then
true is timeout? ( failed )
: bitset? ( data n -- set? ) tuck and = ;
: .check-busy ( status -- set? ) h# 80 bitset? ;
: .check-!busy ( status -- set? ) .check-busy 0= ;
: .check-ready ( status -- set? ) h# 40 bitset? ;
: .check-data ( status -- set? ) h# 8 bitset? ;
: .check-data&!busy ( status -- set? ) dup h# 8 bitset? swap .check-!busy and ;
: wait-!busy? ( t -- ok? ) ['] .check-!busy wait-status ;
: wait-busy? ( t -- ok? ) ['] .check-busy wait-status ;
: wait-ready? ( t -- ok? ) ['] .check-ready wait-status ;
: wait-data? ( t -- ok? ) ['] .check-data wait-status ;
: wait-data&!busy? ( t -- ok? ) ['] .check-data&!busy wait-status ;
: xfer-wait-!busy? ( t -- ok? ) ['] .check-!busy xfer-wait-status ;
: alt-wait-!busy? ( t -- ok? ) ['] .check-!busy alt-wait-status ;
: alt-wait-ready? ( t -- ok? ) ['] .check-ready alt-wait-status ;
: alt-wait-data? ( t -- ok? ) ['] .check-data alt-wait-status ;
: decode-unit,lun ( target lun -- m/s p/s )
swap 1 rshift ( m/s p/s )
\ Our Addressing scheme works like this:
\ where D = Device 0-3, bit 1 = Interface, 0 = Device
\ anything else is invalid.
\ By default set-port does nothing.
: (set-port) ( port# -- okay? ) drop false ;
\ On M1575-derived soutbridges you need to use config space to access some
\ important SATA registers.
: config-b@ ( offset -- data ) my-space + " config-b@" $call-parent ;
: config-b! ( data offset -- ) my-space + " config-b!" $call-parent ;
\ This is only guaranteed to work with M1575-derived soutbridges
: (set-m1575-port) ( port# -- okay? )
\ Save the port in case we ever need to use it again.
\ Switch to regs for this PHY
interface 1 << disk-id or h# 80 config-b!
defer set-port ' (set-port) to set-port
: set-address ( target lun -- OK? )
decode-unit,lun ( m/s p/s )
dup 0 1 between ( m/s p/s valid )
dup 0<> secondary? 0= and if ( m/s p/s )
" No" .inform ( m/s p/s )
.secondary .interface ( m/s p/s )
2drop false exit ( false )
if 2 else 0 then ( m/s offset )
reg-array swap ( m/s array offset )
2dup na+ @ is cmd-regs ( m/s array offset )
1+ na+ @ is ctrl-regs ( m/s )
dup 0 1 between ( m/s valid? )
.Invalid-IDE .drive .number ( -- )
.Invalid-IDE .interface .number ( m/s )
\ For the ATA interface the data in the command block is
\ a copy of the contents of the registers and I just copy then into the
\ correct places to run the command.
fload ${BP}/dev/ide/pktdata.fth
fload ${BP}/dev/ide/ata/support.fth \ Load ATA interface
fload ${BP}/dev/ide/atapi/support.fth \ Load ATAPI interface
: #emit ( n -- ) ascii 0 + emit ;
." Model: " id-buf d# 54 + dup c@ if d# 40 type else drop ." <Unknown>" then
: .not-present ( -- false ) 4 spaces ." Not Present" false ;
: (reset) ( check? -- ok? )
if 1 wait-busy? else true then