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@
27 #include "AppleUltra33ATA.h"
29 #include <IOKit/IODeviceTreeSupport.h>
32 #define super IOATAStandardDriver
34 extern pmap_t kernel_pmap
;
36 OSDefineMetaClassAndStructors( AppleUltra33ATA
, IOATAStandardDriver
)
45 { 165, 600 }, /* Mode 0 */
57 bool AppleUltra33ATA::configure( IOService
*forProvider
, ATAControllerInfo
*controllerInfo
)
60 provider
= (IOPCIDevice
*)forProvider
;
64 ioMapATA
[0] = provider
->mapDeviceMemoryWithRegister( 0x10 + busNum
* 8 + 0 );
65 if ( ioMapATA
[0] == NULL
) return false;
66 ioBaseATA
[0] = (volatile UInt32
*)ioMapATA
[0]->getVirtualAddress();
68 ioMapATA
[1] = provider
->mapDeviceMemoryWithRegister( 0x10 + busNum
* 8 + 4 );
69 if ( ioMapATA
[1] == NULL
) return false;
70 ioBaseATA
[1] = (volatile UInt32
*)ioMapATA
[1]->getVirtualAddress();
72 pciWriteLong( 0x04, 0x05 );
74 dmaDescriptors
= (Ultra646Descriptor
*)kalloc(page_size
);
75 if ( dmaDescriptors
== 0 )
80 dmaDescriptorsPhys
= (UInt32
) pmap_extract(kernel_pmap
, (vm_offset_t
) dmaDescriptors
);
82 if ( (UInt32
)dmaDescriptors
& (page_size
- 1) )
84 IOLog("AppleUltra33ATA::%s() - DMA Descriptor memory not page aligned!!", __FUNCTION__
);
88 bzero( dmaDescriptors
, page_size
);
90 numDescriptors
= page_size
/sizeof(Ultra646Descriptor
);
92 dmaMemoryCursor
= IOBigMemoryCursor::withSpecification( 64*1024-2, 0xffffffff );
93 if ( dmaMemoryCursor
== NULL
)
98 bitBucketAddr
= IOMalloc(32);
99 if ( bitBucketAddr
== 0 )
103 bitBucketAddrPhys
= (UInt32
) pmap_extract(kernel_pmap
, (vm_offset_t
) (((UInt32
)bitBucketAddr
+ 0xf) & ~0x0f));
105 interruptEventSource
= IOInterruptEventSource::interruptEventSource( (OSObject
*) this,
106 (IOInterruptEventAction
) &AppleUltra33ATA::interruptOccurred
,
107 (IOService
*) provider
,
110 if ( interruptEventSource
== NULL
)
115 disableControllerInterrupts();
117 getWorkLoop()->addEventSource( interruptEventSource
);
119 controllerInfo
->maxDevicesSupported
= 2;
120 controllerInfo
->devicePrivateDataSize
= 0;
121 controllerInfo
->commandPrivateDataSize
= 0;
122 controllerInfo
->disableCancelCommands
= false;
131 bool AppleUltra33ATA::calculateTiming( UInt32 unit
, ATATiming
*pTiming
)
135 ideTimingRegs
[unit
].arttimReg
= 0x40;
136 ideTimingRegs
[unit
].cmdtimReg
= 0xA9;
138 switch ( pTiming
->timingProtocol
)
141 rc
= calculatePIOTiming( unit
, pTiming
);
145 rc
= calculateDMATiming( unit
, pTiming
);
148 case kATATimingUltraDMA33
:
149 rc
= calculateUltraDMATiming( unit
, pTiming
);
164 bool AppleUltra33ATA::calculatePIOTiming( UInt32 unit
, ATATiming
*pTiming
)
167 UInt32 drwActClks
, drwRecClks
;
168 UInt32 drwActTime
, drwRecTime
;
170 accessTime
= pioModes
[pTiming
->mode
].minDataAccess
;
172 drwActClks
= accessTime
/ IDE_SYSCLK_NS
;
173 drwActClks
+= (accessTime
% IDE_SYSCLK_NS
) ? 1 : 0;
174 drwActTime
= drwActClks
* IDE_SYSCLK_NS
;
176 drwRecTime
= pioModes
[pTiming
->mode
].minDataCycle
- drwActTime
;
177 drwRecClks
= drwRecTime
/ IDE_SYSCLK_NS
;
178 drwRecClks
+= (drwRecTime
% IDE_SYSCLK_NS
) ? 1 : 0;
180 if ( drwRecClks
>= 16 )
182 else if ( drwRecClks
<= 1 )
185 ideTimingRegs
[unit
].drwtimRegPIO
= ((drwActClks
& 0x0f) << 4) | ((drwRecClks
-1) & 0x0f);
195 bool AppleUltra33ATA::calculateDMATiming( UInt32 unit
, ATATiming
*pTiming
)
198 UInt32 drwActClks
, drwRecClks
;
199 UInt32 drwActTime
, drwRecTime
;
201 ideTimingRegs
[unit
].udidetcrReg
= 0;
203 accessTime
= pTiming
->minDataAccess
;
205 drwActClks
= accessTime
/ IDE_SYSCLK_NS
;
206 drwActClks
+= (accessTime
% IDE_SYSCLK_NS
) ? 1 : 0;
207 drwActTime
= drwActClks
* IDE_SYSCLK_NS
;
209 drwRecTime
= pTiming
->minDataCycle
- drwActTime
;
210 drwRecClks
= drwRecTime
/ IDE_SYSCLK_NS
;
211 drwRecClks
+= (drwRecTime
% IDE_SYSCLK_NS
) ? 1 : 0;
213 if ( drwRecClks
>= 16 )
215 else if ( drwRecClks
<= 1 )
218 ideTimingRegs
[unit
].drwtimRegDMA
= ((drwActClks
& 0x0f) << 4) | ((drwRecClks
-1) & 0x0f);
227 bool AppleUltra33ATA::calculateUltraDMATiming( UInt32 unit
, ATATiming
*pTiming
)
232 cycleTime
= pTiming
->minDataCycle
;
234 cycleClks
= cycleTime
/ IDE_SYSCLK_NS
;
235 cycleClks
+= (cycleTime
% IDE_SYSCLK_NS
) ? 1 : 0;
237 ideTimingRegs
[unit
].udidetcrReg
= (0x01 << unit
) | ((cycleClks
-1) << ((!unit
) ? 4 : 6)) ;
246 void AppleUltra33ATA::newDeviceSelected( IOATADevice
*newDevice
)
255 bool AppleUltra33ATA::selectTiming( UInt32 unit
, ATATimingProtocol timingProtocol
)
257 Ultra646Regs
*cfgRegs
;
260 cfgRegs
= &ideTimingRegs
[unit
];
264 pciWriteByte( kUltra646CMDTIM
, cfgRegs
->cmdtimReg
);
268 pciWriteByte( kUltra646ARTTIM0
, cfgRegs
->arttimReg
);
270 if ( timingProtocol
== kATATimingPIO
)
272 cfgByte
= pciReadByte( kUltra646CNTRL
);
273 cfgByte
&= ~kUltra646CNTRL_Drive0ReadAhead
;
274 cfgByte
|= cfgRegs
->cntrlReg
;
275 pciWriteByte( kUltra646CNTRL
, cfgByte
);
277 pciWriteByte( kUltra646DRWTIM0
, cfgRegs
->drwtimRegPIO
);
279 else if ( timingProtocol
== kATATimingDMA
)
281 pciWriteByte( kUltra646DRWTIM0
, cfgRegs
->drwtimRegDMA
);
283 else if ( timingProtocol
== kATATimingUltraDMA33
)
285 cfgByte
= pciReadByte( kUltra646UDIDETCR0
);
286 cfgByte
&= ~(kUltra646UDIDETCR0_Drive0UDMACycleTime
| kUltra646UDIDETCR0_Drive0UDMAEnable
);
287 cfgByte
|= cfgRegs
->udidetcrReg
;
288 pciWriteByte( kUltra646UDIDETCR0
, cfgByte
);
293 pciWriteByte( kUltra646ARTTIM1
, cfgRegs
->arttimReg
);
295 if ( timingProtocol
== kATATimingPIO
)
297 cfgByte
= pciReadByte( kUltra646CNTRL
);
298 cfgByte
&= ~kUltra646CNTRL_Drive1ReadAhead
;
299 cfgByte
|= cfgRegs
->cntrlReg
;
300 pciWriteByte( kUltra646CNTRL
, cfgByte
);
302 pciWriteByte( kUltra646DRWTIM1
, cfgRegs
->drwtimRegPIO
);
304 else if ( timingProtocol
== kATATimingDMA
)
306 pciWriteByte( kUltra646DRWTIM1
, cfgRegs
->drwtimRegDMA
);
308 else if ( timingProtocol
== kATATimingUltraDMA33
)
310 cfgByte
= pciReadByte( kUltra646UDIDETCR0
);
311 cfgByte
&= ~(kUltra646UDIDETCR0_Drive1UDMACycleTime
| kUltra646UDIDETCR0_Drive1UDMAEnable
);
312 cfgByte
|= cfgRegs
->udidetcrReg
;
313 pciWriteByte( kUltra646UDIDETCR0
, cfgByte
);
319 pciWriteByte( kUltra646CMDTIM
, cfgRegs
->cmdtimReg
);
323 cfgByte
= pciReadByte( kUltra646ARTTIM23
);
324 cfgByte
&= ~(kUltra646ARTTIM23_Drive2ReadAhead
| kUltra646ARTTIM23_AddrSetup
);
325 cfgByte
|= (cfgRegs
->cntrlReg
>> 4) | cfgRegs
->arttimReg
;
326 pciWriteByte( kUltra646ARTTIM23
, cfgByte
);
328 if ( timingProtocol
== kATATimingPIO
)
330 pciWriteByte( kUltra646DRWTIM2
, cfgRegs
->drwtimRegPIO
);
332 else if ( timingProtocol
== kATATimingDMA
)
334 pciWriteByte( kUltra646DRWTIM1
, cfgRegs
->drwtimRegDMA
);
336 else if ( timingProtocol
== kATATimingUltraDMA33
)
338 cfgByte
= pciReadByte( kUltra646UDIDETCR1
);
339 cfgByte
&= ~(kUltra646UDIDETCR1_Drive2UDMACycleTime
| kUltra646UDIDETCR1_Drive2UDMAEnable
);
340 cfgByte
|= cfgRegs
->udidetcrReg
;
341 pciWriteByte( kUltra646UDIDETCR1
, cfgByte
);
346 cfgByte
= pciReadByte( kUltra646ARTTIM23
);
347 cfgByte
&= ~(kUltra646ARTTIM23_Drive3ReadAhead
| kUltra646ARTTIM23_AddrSetup
);
348 cfgByte
|= (cfgRegs
->cntrlReg
>> 4) | cfgRegs
->arttimReg
;
349 pciWriteByte( kUltra646ARTTIM23
, cfgByte
);
351 if ( timingProtocol
== kATATimingPIO
)
353 pciWriteByte( kUltra646DRWTIM3
, cfgRegs
->drwtimRegPIO
);
355 else if ( timingProtocol
== kATATimingDMA
)
357 pciWriteByte( kUltra646DRWTIM3
, cfgRegs
->drwtimRegDMA
);
359 else if ( timingProtocol
== kATATimingUltraDMA33
)
361 cfgByte
= pciReadByte( kUltra646UDIDETCR1
);
362 cfgByte
&= ~(kUltra646UDIDETCR1_Drive3UDMACycleTime
| kUltra646UDIDETCR1_Drive3UDMAEnable
);
363 cfgByte
|= cfgRegs
->udidetcrReg
;
364 pciWriteByte( kUltra646UDIDETCR1
, cfgByte
);
377 void AppleUltra33ATA::interruptOccurred()
382 intReg
= (busNum
== 0) ? kUltra646CFR
: kUltra646ARTTIM23
;
383 cfgReg
= pciReadByte( intReg
);
384 pciWriteByte( intReg
, cfgReg
);
386 intReg
= (busNum
== 0) ? kUltra646BMIDESR0
: kUltra646BMIDESR1
;
387 cfgReg
= pciReadByte( intReg
);
388 pciWriteByte( intReg
, cfgReg
);
390 super::interruptOccurred();
392 enableControllerInterrupts();
399 bool AppleUltra33ATA::programDma( IOATAStandardCommand
*cmd
)
401 IOMemoryDescriptor
*memoryDesc
;
402 IOPhysicalSegment physSeg
;
407 Ultra646Descriptor
*dmaDesc
;
408 UInt32 startSeg
, endSeg
;
410 cmd
->getPointers( &memoryDesc
, &dmaReqLength
, &dmaIsWrite
);
412 if ( dmaReqLength
== 0 )
419 dmaDesc
= dmaDescriptors
;
421 bytesLeft
= dmaReqLength
;
423 for (i
= 0; i
< numDescriptors
-1; i
++, dmaDesc
++ )
425 if ( dmaMemoryCursor
->getPhysicalSegments( memoryDesc
, offset
, &physSeg
, 1 ) != 1 )
430 startSeg
= (physSeg
.location
& ~0xffff);
431 endSeg
= (physSeg
.location
+ physSeg
.length
- 1) & ~0xffff;
433 OSWriteSwapInt32( &dmaDesc
->start
, 0, physSeg
.location
);
435 if ( startSeg
== endSeg
)
437 OSWriteSwapInt32( &dmaDesc
->length
, 0, physSeg
.length
);
441 len
= (-physSeg
.location
& 0xffff);
442 OSWriteSwapInt32( &dmaDesc
->length
, 0, len
);
445 OSWriteSwapInt32( &dmaDesc
->start
, 0, physSeg
.location
+ len
);
446 OSWriteSwapInt32( &dmaDesc
->length
, 0, physSeg
.length
- len
);
449 bytesLeft
-= physSeg
.length
;
450 offset
+= physSeg
.length
;
453 if ( bytesLeft
!= 0 )
459 * Note: ATAPI always transfers even byte-counts. Send the extra byte to/from the bit-bucket
460 * if the requested transfer length is odd.
462 if ( dmaReqLength
& 1 )
464 if ( i
== numDescriptors
) return false;
467 OSWriteSwapInt32( &dmaDesc
->start
, 0, bitBucketAddrPhys
);
468 OSWriteSwapInt32( &dmaDesc
->length
, 0, 1 );
473 dmaDesc
->length
|= 0x80;
475 pciWriteLong( ((busNum
== 0) ? kUltra646DTPR0
: kUltra646DTPR1
), dmaDescriptorsPhys
);
484 bool AppleUltra33ATA::startDma( IOATAStandardCommand
* )
491 if ( dmaReqLength
!= 0 )
493 reg
= (busNum
== 0) ? kUltra646BMIDECR0
: kUltra646BMIDECR1
;
494 startMask
= (busNum
== 0) ? kUltra646BMIDECR0_StartDMAPRI
: kUltra646BMIDECR1_StartDMASDY
;
495 writeMask
= (busNum
== 0) ? kUltra646BMIDECR0_PCIWritePRI
: kUltra646BMIDECR1_PCIWriteSDY
;
496 cfgReg
= pciReadByte( reg
);
497 cfgReg
&= ~writeMask
;
498 cfgReg
|= startMask
| ((dmaIsWrite
== false) ? writeMask
: 0);
499 pciWriteByte( reg
, cfgReg
);
508 bool AppleUltra33ATA::stopDma( IOATAStandardCommand
*, UInt32
*transferCount
)
516 if ( dmaReqLength
== 0 )
521 reg
= (busNum
== 0) ? kUltra646BMIDECR0
: kUltra646BMIDECR1
;
522 startMask
= (busNum
== 0) ? kUltra646BMIDECR0_StartDMAPRI
: kUltra646BMIDECR1_StartDMASDY
;
523 cfgReg
= pciReadByte( reg
);
524 cfgReg
&= ~startMask
;
525 pciWriteByte( reg
, cfgReg
);
527 *transferCount
= dmaReqLength
;
536 bool AppleUltra33ATA::checkDmaActive()
542 reg
= (busNum
== 0) ? kUltra646BMIDESR0
: kUltra646BMIDESR1
;
543 activeMask
= (busNum
== 0) ? kUltra646BMIDESR0_DMAActivePRI
: kUltra646BMIDESR1_DMAActiveSDY
;
545 cfgReg
= pciReadByte( reg
);
547 return ((cfgReg
& activeMask
) != 0);
554 bool AppleUltra33ATA::resetDma()
560 reg
= (busNum
== 0) ? kUltra646BMIDECR0
: kUltra646BMIDECR1
;
561 startMask
= (busNum
== 0) ? kUltra646BMIDECR0_StartDMAPRI
: kUltra646BMIDECR1_StartDMASDY
;
563 cfgReg
= pciReadByte( reg
);
564 cfgReg
&= ~startMask
;
565 pciWriteByte( reg
, cfgReg
);
574 void AppleUltra33ATA::disableControllerInterrupts()
576 interruptEventSource
->disable();
583 void AppleUltra33ATA::enableControllerInterrupts()
585 interruptEventSource
->enable();
592 void AppleUltra33ATA::free()
596 if ( interruptEventSource
!= 0 )
598 interruptEventSource
->disable();
599 interruptEventSource
->release();
602 for (i
= 0; i
< 2; i
++ )
604 if ( ioMapATA
[i
] != 0 ) ioMapATA
[i
]->release();
607 if ( dmaDescriptors
!= 0 )
609 kfree( (vm_offset_t
)dmaDescriptors
, page_size
);
617 void AppleUltra33ATA::writeATAReg( UInt32 regIndex
, UInt32 regValue
)
621 *(volatile UInt16
*)ioBaseATA
[0] = regValue
;
623 else if ( regIndex
< kATARegDeviceControl
)
625 *((volatile UInt8
*)ioBaseATA
[0] + regIndex
) = regValue
;
629 *((volatile UInt8
*)ioBaseATA
[1] + regIndex
- kATARegDeviceControl
+ 2) = regValue
;
634 UInt32
AppleUltra33ATA::readATAReg( UInt32 regIndex
)
638 return *(volatile UInt16
*)ioBaseATA
[0];
640 else if ( regIndex
< kATARegDeviceControl
)
642 return *((volatile UInt8
*)ioBaseATA
[0] + regIndex
);
645 return *((volatile UInt8
*)ioBaseATA
[1] + regIndex
- kATARegDeviceControl
+ 2);
652 UInt32
AppleUltra33ATA::pciReadByte( UInt32 reg
)
657 unsigned char byte
[4];
660 data
.word
= provider
->configRead32( reg
);
661 return data
.byte
[3 - (reg
& 0x03)];
664 void AppleUltra33ATA::pciWriteByte( UInt32 reg
, UInt32 value
)
669 unsigned char byte
[4];
674 regWord
= reg
& ~0x03;
676 data
.word
= provider
->configRead32( regWord
);
677 data
.word
= OSReadSwapInt32( &data
.word
, 0 );
682 data
.byte
[kUltra646CFR
& 0x03] &= ~kUltra646CFR_IDEIntPRI
;
684 case kUltra646DRWTIM0
:
685 data
.byte
[kUltra646ARTTIM23
& 0x03] &= ~kUltra646ARTTIM23_IDEIntSDY
;
687 case kUltra646BMIDECR0
:
688 data
.byte
[kUltra646MRDMODE
& 0x03 ] &= ~(kUltra646MRDMODE_IDEIntPRI
| kUltra646MRDMODE_IDEIntSDY
);
689 data
.byte
[kUltra646BMIDESR0
& 0x03] &= ~(kUltra646BMIDESR0_DMAIntPRI
| kUltra646BMIDESR0_DMAErrorPRI
);
691 case kUltra646BMIDECR1
:
692 data
.byte
[kUltra646BMIDESR1
& 0x03] &= ~(kUltra646BMIDESR1_DMAIntSDY
| kUltra646BMIDESR1_DMAErrorSDY
);
695 data
.byte
[reg
& 0x03] = value
;
697 data
.word
= OSReadSwapInt32(&data
.word
, 0);
699 provider
->configWrite32( regWord
, data
.word
);
702 UInt32
AppleUltra33ATA::pciReadLong( UInt32 reg
)
704 return provider
->configRead32( reg
);
707 void AppleUltra33ATA::pciWriteLong( UInt32 reg
, UInt32 value
)
709 provider
->configWrite32( reg
, value
);
712 /* These overrides take care of OpenFirmware referring to the controller
713 * as a child of the PCI device, "ata-4" */
715 bool AppleUltra33ATA::attach( IOService
* provider
)
717 if ( super::attach(provider
) )
719 // assumes the first child determines the path OF uses to reference the controller
720 pathProvider
= OSDynamicCast(IOService
, provider
->getChildEntry(gIODTPlane
));
724 setLocation(pathProvider
->getLocation(gIODTPlane
), gIODTPlane
);
725 setName(pathProvider
->getName(gIODTPlane
), gIODTPlane
);
726 attachToParent(provider
, gIODTPlane
);
727 pathProvider
->retain();
728 pathProvider
->detachFromParent(provider
, gIODTPlane
);
737 void AppleUltra33ATA::detach( IOService
* provider
)
741 detachFromParent(provider
, gIODTPlane
);
742 pathProvider
->attachToParent(provider
, gIODTPlane
);
743 pathProvider
->release();
746 super::detach(provider
);