2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 * IOATAStandardDriverPio.cpp
27 #include <IOKit/ata/IOATAStandardInterface.h>
29 /*----------------------------------- ATA SetRegs Protocol ------------------------------*/
35 void IOATAStandardDriver::doProtocolSetRegs( IOATAStandardCommand
*ataCmd
)
41 setCommandLimit( currentDevice
, 1 );
43 ataCmd
->getTaskfile( &taskfile
);
45 regmask
= taskfile
.regmask
;
47 if ( regmask
& ATARegtoMask(kATARegDriveHead
) )
49 regmask
&= ~ATARegtoMask(kATARegDriveHead
);
50 if ( selectDrive( taskfile
.ataRegs
[kATARegDriveHead
] ) == false )
52 completeCmd( ataCmd
, kATAReturnBusyError
);
57 for ( i
= 0; regmask
; i
++ )
61 writeATAReg( i
, taskfile
.ataRegs
[i
] );
68 completeCmd( ataCmd
, kATAReturnSuccess
);
71 /*----------------------------------- ATA PIO Protocol ------------------------------*/
77 void IOATAStandardDriver::doATAProtocolPio( IOATAStandardCommand
*ataCmd
)
84 setCommandLimit( currentDevice
, 1 );
86 ataCmd
->getTaskfile( &taskfile
);
88 regmask
= taskfile
.regmask
;
90 if ( regmask
& ATARegtoMask(kATARegDriveHead
) )
92 regmask
&= ~ATARegtoMask(kATARegDriveHead
);
93 if ( selectDrive( taskfile
.ataRegs
[kATARegDriveHead
] ) == false )
95 completeCmd( ataCmd
, kATAReturnBusyError
);
101 ataCmd
->getPointers( &xferDesc
, &xferRemaining
, &xferIsWrite
);
103 for ( i
= 0; regmask
; i
++ )
107 writeATAReg( i
, taskfile
.ataRegs
[i
] );
114 rc
= waitForDRQ( kATADRQTimeoutmS
);
115 if ( rc
!= kATAReturnSuccess
)
117 completeCmd( ataCmd
, rc
);
129 void IOATAStandardDriver::processATAPioInt()
131 IOATAStandardCommand
*ataCmd
;
132 UInt16 tmpBuffer
[256];
135 ATAReturnCode rc
= kATAReturnSuccess
;
137 ataCmd
= findCommandWithNexus( currentDevice
, (UInt32
) -1 );
140 IOLog( "IOATAStandardDriver::processATAPioInt() - ATA Command not found\n\r" );
144 if ( waitForStatus( 0, kATAStatusBSY
, kATABusyTimeoutmS
) == false )
146 completeCmd( ataCmd
, kATAReturnBusyError
, xferCount
);
150 status
= readATAReg( kATARegStatus
);
152 if ( (status
& kATAStatusDRQ
) && (xferRemaining
!= 0) )
154 if ( xferIsWrite
== true )
156 xferDesc
->readBytes( xferCount
, tmpBuffer
, 512 );
158 for ( i
=0; i
< 256; i
++ )
160 writeATAReg( kATARegData
, tmpBuffer
[i
] );
165 for ( i
=0; i
< 256; i
++ )
167 tmpBuffer
[i
] = readATAReg( kATARegData
);
169 xferDesc
->writeBytes( xferCount
, tmpBuffer
, 512 );
173 xferRemaining
-= 512;
176 if ( status
& kATAStatusERR
)
178 completeCmd( ataCmd
, kATAReturnStatusError
, xferCount
);
180 else if ( !xferRemaining
)
182 completeCmd( ataCmd
, rc
, xferCount
);
185 /*----------------------------------- ATA Reset Protocol ------------------------------*/
192 void IOATAStandardDriver::doATAReset( IOATAStandardCommand
*ataCmd
)
197 completeCmd( ataCmd
, kATAReturnNoResource
);
201 if ( resetPollEvent
== 0 )
203 resetPollEvent
= IOTimerEventSource::timerEventSource( this,
204 (IOTimerEventSource::Action
) &IOATAStandardDriver::checkATAResetComplete
);
206 if ( (resetPollEvent
== 0) || (getWorkLoop()->addEventSource( resetPollEvent
) != kIOReturnSuccess
) )
208 completeCmd( ataCmd
, kATAReturnNoResource
);
215 clock_interval_to_deadline( resetCmd
->getTimeout(), 1000000, &resetTimeout
);
217 writeATAReg( kATARegDeviceControl
, kATADevControlnIEN
| kATADevControlSRST
);
219 writeATAReg( kATARegDeviceControl
, 0 );
223 checkATAResetComplete();
233 void IOATAStandardDriver::checkATAResetComplete()
236 IOATAStandardCommand
*ataCmd
;
237 AbsoluteTime currentTime
;
238 ATAReturnCode rc
= kATAReturnSuccess
;
242 status
= readATAReg( kATARegStatus
);
244 if ( (status
& kATAStatusBSY
) == 0 )
249 clock_get_uptime( ¤tTime
);
250 if ( CMP_ABSOLUTETIME( ¤tTime
, &resetTimeout
) > 0 )
252 rc
= kATAReturnBusyError
;
256 resetPollEvent
->setTimeoutMS(kATAResetPollIntervalmS
);
264 if ( ataCmd
->getCmdType() != kATACommandBusReset
)
269 completeCmd( ataCmd
, rc
);
273 /*----------------------------------- ATAPI PIO Protocols ------------------------------*/
280 void IOATAStandardDriver::doATAPIProtocolPio( IOATAStandardCommand
*ataCmd
)
282 ATATaskfile taskfile
;
288 setCommandLimit( currentDevice
, 1 );
292 ataCmd
->getTaskfile( &taskfile
);
293 ataCmd
->getCDB( &atapiCmd
);
295 regmask
= taskfile
.regmask
;
297 if ( regmask
& ATARegtoMask(kATARegDriveHead
) )
299 regmask
&= ~ATARegtoMask(kATARegDriveHead
);
300 if ( selectDrive( taskfile
.ataRegs
[kATARegDriveHead
] ) == false )
302 completeCmd( ataCmd
, kATAReturnBusyError
);
307 for ( i
= 0; regmask
; i
++ )
311 writeATAReg( i
, taskfile
.ataRegs
[i
] );
317 ataCmd
->getPointers( &xferDesc
, &xferRemaining
, &xferIsWrite
);
319 if ( ataCmd
->getDevice(kIOATAStandardDevice
)->getATAPIPktInt() == false )
321 rc
= sendATAPIPacket( ataCmd
);
323 if ( rc
!= kATAReturnSuccess
)
325 completeCmd( ataCmd
, rc
);
335 void IOATAStandardDriver::processATAPIPioInt()
337 IOATAStandardCommand
*ataCmd
;
338 ATAReturnCode rc
= kATAReturnProtocolError
;
343 ataCmd
= findCommandWithNexus( currentDevice
, (UInt32
) -1 );
346 IOLog( "IOATAStandardDriver::processATAPIPioInt() - ATA Command not found\n\r" );
350 if ( waitForStatus( 0, kATAStatusBSY
, kATABusyTimeoutmS
) == false )
352 completeCmd( ataCmd
, kATAReturnBusyError
, xferCount
);
356 status
= readATAReg( kATARegATAPIStatus
);
357 intReason
= readATAReg( kATARegATAPIIntReason
);
359 if ( status
& kATAPIStatusDRQ
)
361 if ( intReason
& kATAPIIntReasonCD
)
363 if ( !(intReason
& kATAPIIntReasonIO
) )
365 rc
= sendATAPIPacket( ataCmd
);
370 n
= readATAReg( kATARegATAPIByteCountLow
) | (readATAReg( kATARegATAPIByteCountHigh
) << 8);
373 if ( !(intReason
& kATAPIIntReasonIO
) && (xferIsWrite
== true) )
375 rc
= writeATAPIDevice( n
);
377 else if ( (intReason
& kATAPIIntReasonIO
) && (xferIsWrite
== false) )
379 rc
= readATAPIDevice( n
);
383 else if ( (intReason
& kATAPIIntReasonCD
) && (intReason
& kATAPIIntReasonIO
) )
385 rc
= (status
& kATAPIStatusCHK
) ? kATAReturnStatusError
: kATAReturnSuccess
;
386 completeCmd( ataCmd
, rc
, xferCount
);
394 ATAReturnCode
IOATAStandardDriver::sendATAPIPacket( IOATAStandardCommand
*ataCmd
)
401 ataCmd
->getCDB( &atapiCmd
);
403 rc
= waitForDRQ( kATADRQTimeoutmS
);
404 if ( rc
!= kATAReturnSuccess
) return rc
;
406 pCDB
= (UInt16
*)atapiCmd
.cdb
;
407 for ( i
=0; i
< atapiCmd
.cdbLength
>> 1; i
++ )
409 writeATAReg( kATARegData
, *pCDB
++ );
420 ATAReturnCode
IOATAStandardDriver::readATAPIDevice( UInt32 n
)
422 UInt16 tmpBuffer
[256];
427 j
= (n
< 512) ? n
: 512;
430 for ( i
=0; i
< j
; i
++ )
432 tmpBuffer
[i
] = readATAReg( kATARegData
);
437 k
= (j
> xferRemaining
) ? xferRemaining
: j
;
439 xferDesc
->writeBytes( xferCount
, tmpBuffer
, k
);
445 return kATAReturnSuccess
;
452 ATAReturnCode
IOATAStandardDriver::writeATAPIDevice( UInt32 n
)
454 UInt16 tmpBuffer
[256];
460 j
= (n
< 512) ? n
: 512;
462 k
= (j
> xferRemaining
) ? xferRemaining
: j
;
464 xferDesc
->readBytes( xferCount
, tmpBuffer
, k
);
467 for ( i
=0; i
< j
; i
++ )
469 writeATAReg( kATARegData
, tmpBuffer
[i
] );
478 return kATAReturnSuccess
;
486 bool IOATAStandardDriver::selectDrive( UInt32 driveHeadReg
)
488 if ( waitForAltStatus( 0, kATAStatusBSY
, kATABusyTimeoutmS
) == false )
493 writeATAReg( kATARegDriveHead
, driveHeadReg
);
495 if ( waitForAltStatus( 0, kATAStatusBSY
, kATABusyTimeoutmS
) == false )