3 Copyright (c) 2002-2016 Alexander A. Telyatnikov (Alter)
9 This is the miniport driver for ATAPI IDE controllers
10 With Busmaster DMA support
13 Alexander A. Telyatnikov (Alter)
20 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 This module is a port from FreeBSD 4.3-6.1 ATA driver (ata-dma.c, ata-chipset.c) by
34 Søren Schmidt, Copyright (c) 1998-2008
36 Changed defaulting-to-generic-PIO/DMA policy
37 Added PIO settings for VIA
38 Optimized VIA/AMD/nVidia init part
39 Optimized Promise TX2 init part
40 Optimized Intel init part
41 by Alex A. Telyatnikov (Alter) (c) 2002-2007
51 static const ULONG valid_udma
[7] = {0,0,2,0,4,5,6};
53 static const CHAR retry_Wdma
[MAX_RETRIES
+1] = {2, 2, 2,-1,-1,-1};
54 static const CHAR retry_Udma
[MAX_RETRIES
+1] = {6, 2,-1,-1,-1,-1};
56 PHYSICAL_ADDRESS ph4gb
= {{0xFFFFFFFF, 0}};
61 IN PHW_DEVICE_EXTENSION deviceExtension
,
62 IN ULONG dev
, // physical device number (0-3)
69 IN PHW_DEVICE_EXTENSION deviceExtension
,
70 IN ULONG dev
, // physical device number (0-3)
77 IN PHW_DEVICE_EXTENSION deviceExtension
,
78 IN ULONG dev
, // physical device number (0-3)
85 IN PHW_DEVICE_EXTENSION deviceExtension
,
86 IN ULONG dev
, // physical device number (0-3)
93 IN PHW_DEVICE_EXTENSION deviceExtension
,
94 IN ULONG channel
// physical channel number (0-1)
100 IN PVOID HwDeviceExtension
,
101 IN PSCSI_REQUEST_BLOCK Srb
,
103 OUT PULONG count
, /* bytes */
107 PHYSICAL_ADDRESS ph_addr
;
110 *(volatile char*)data
; // Touch memory, this will prevent condition of not-ready page table for valid addresses
112 ph_addr
= MmGetPhysicalAddress(data
);
113 KdPrint3((PRINT_PREFIX
"AtapiVirtToPhysAddr_: %x -> %8.8x:%8.8x\n", data
, ph_addr
.HighPart
, ph_addr
.LowPart
));
114 if(!ph_addru
&& ph_addr
.HighPart
) {
115 // do so until we add 64bit address support
116 // or some workaround
121 (*ph_addru
) = ph_addr
.HighPart
;
122 //addr = ScsiPortConvertPhysicalAddressToUlong(ph_addr);
123 addr
= ph_addr
.LowPart
;
124 if(!addr
&& !ph_addr
.HighPart
) {
129 *count
= sizeof(BM_DMA_ENTRY
)*ATA_DMA_ENTRIES
;
131 *count
= PAGE_SIZE
- (addr
& (PAGE_SIZE
-1));
134 } // end AtapiVirtToPhysAddr_()
139 IN PVOID HwDeviceExtension
,
140 IN PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
141 IN ULONG lChannel
// logical channel,
145 PHW_DEVICE_EXTENSION deviceExtension
= (PHW_DEVICE_EXTENSION
)HwDeviceExtension
;
146 PHW_CHANNEL chan
= &(deviceExtension
->chan
[lChannel
]);
151 deviceExtension
->chan
[c
].CopyDmaBuffer
= FALSE
;
153 if(!deviceExtension
->Host64
&& (WinVer_Id() > WinVer_NT
)) {
154 KdPrint2((PRINT_PREFIX
"AtapiDmaAlloc: allocate tmp buffers below 4Gb\n"));
156 KdPrint2((PRINT_PREFIX
" already initialized %x\n", chan
->DB_PRD
));
159 chan
->DB_PRD
= MmAllocateContiguousMemory(sizeof(((PATA_REQ
)NULL
)->dma_tab
), ph4gb
);
161 chan
->DB_PRD_PhAddr
= AtapiVirtToPhysAddr(HwDeviceExtension
, NULL
, (PUCHAR
)(chan
->DB_PRD
), &i
, &ph_addru
);
162 if(!chan
->DB_PRD_PhAddr
|| !i
|| ((LONG
)(chan
->DB_PRD_PhAddr
) == -1)) {
163 KdPrint2((PRINT_PREFIX
"AtapiDmaAlloc: No DB PRD BASE\n" ));
165 chan
->DB_PRD_PhAddr
= 0;
169 KdPrint2((PRINT_PREFIX
"AtapiDmaAlloc: No DB PRD below 4Gb\n" ));
173 chan
->DB_IO
= MmAllocateContiguousMemory(deviceExtension
->MaximumDmaTransferLength
, ph4gb
);
175 chan
->DB_IO_PhAddr
= AtapiVirtToPhysAddr(HwDeviceExtension
, NULL
, (PUCHAR
)(chan
->DB_IO
), &i
, &ph_addru
);
176 if(!chan
->DB_IO_PhAddr
|| !i
|| ((LONG
)(chan
->DB_IO_PhAddr
) == -1)) {
177 KdPrint2((PRINT_PREFIX
"AtapiDmaAlloc: No DB IO BASE\n" ));
179 MmFreeContiguousMemory(chan
->DB_PRD
);
181 chan
->DB_PRD_PhAddr
= 0;
183 chan
->DB_IO_PhAddr
= 0;
187 KdPrint2((PRINT_PREFIX
"AtapiDmaAlloc: No DB IO below 4Gb\n" ));
188 MmFreeContiguousMemory(chan
->DB_IO
);
195 if(deviceExtension
->HwFlags
& UNIATA_AHCI
) {
196 KdPrint2((PRINT_PREFIX
"AtapiDmaAlloc: AHCI\n" ));
197 if(chan
->AhciCtlBlock
) {
198 KdPrint2((PRINT_PREFIX
" already initialized %x\n", chan
->AhciCtlBlock
));
201 // Need 1K-byte alignment
202 chan
->AhciCtlBlock0
= (PIDE_AHCI_CHANNEL_CTL_BLOCK
)MmAllocateContiguousMemory(
203 sizeof(IDE_AHCI_CHANNEL_CTL_BLOCK
)+AHCI_CLB_ALIGNEMENT_MASK
,
205 if(chan
->AhciCtlBlock0
) {
208 ULONGLONG AhciCtlBlock64
;
211 AhciCtlBlock
= (PUCHAR
)chan
->AhciCtlBlock0
;
213 KdPrint2((PRINT_PREFIX
"AtapiDmaAlloc: CLP BASE %I64x\n", AhciCtlBlock64
));
215 AhciCtlBlock64
+= AHCI_CLB_ALIGNEMENT_MASK
;
216 AhciCtlBlock64
&= ~AHCI_CLB_ALIGNEMENT_MASK
;
218 KdPrint2((PRINT_PREFIX
"AtapiDmaAlloc: CLP BASE 1k-aligned %I64x\n", AhciCtlBlock64
));
220 chan
->AhciCtlBlock
= (PIDE_AHCI_CHANNEL_CTL_BLOCK
)AhciCtlBlock
;
222 chan
->AHCI_CTL_PhAddr
= AtapiVirtToPhysAddr(HwDeviceExtension
, NULL
, (PUCHAR
)(chan
->AhciCtlBlock
), &i
, &ph_addru
);
223 KdPrint2((PRINT_PREFIX
"AtapiDmaAlloc: CLP Phys BASE %I64x\n", chan
->AHCI_CTL_PhAddr
));
224 if(!chan
->AHCI_CTL_PhAddr
|| !i
|| ((LONG
)(chan
->AHCI_CTL_PhAddr
) == -1)) {
225 KdPrint2((PRINT_PREFIX
"AtapiDmaAlloc: No AHCI CLP BASE\n" ));
226 chan
->AhciCtlBlock
= NULL
;
227 chan
->AHCI_CTL_PhAddr
= 0;
231 KdPrint2((PRINT_PREFIX
"AtapiDmaAlloc: No AHCI CLP below 4Gb\n" ));
232 MmFreeContiguousMemory(chan
->AhciCtlBlock0
);
233 chan
->AhciCtlBlock
= NULL
;
234 chan
->AHCI_CTL_PhAddr
= 0;
238 KdPrint2((PRINT_PREFIX
"AtapiDmaAlloc: Can't alloc AHCI CLP\n"));
243 } // end AtapiDmaAlloc()
248 IN PVOID HwDeviceExtension
,
249 IN ULONG DeviceNumber
,
250 IN ULONG lChannel
, // logical channel,
251 IN PSCSI_REQUEST_BLOCK Srb
,
253 IN ULONG count
/* bytes */
256 PHW_DEVICE_EXTENSION deviceExtension
= (PHW_DEVICE_EXTENSION
)HwDeviceExtension
;
257 ULONG dma_count
, dma_base
, dma_baseu
;
258 ULONG dma_count0
, dma_base0
;
260 PHW_CHANNEL chan
= &(deviceExtension
->chan
[lChannel
]);
261 PATA_REQ AtaReq
= (PATA_REQ
)(Srb
->SrbExtension
);
262 BOOLEAN use_DB_IO
= FALSE
;
263 BOOLEAN use_AHCI
= (deviceExtension
->HwFlags
& UNIATA_AHCI
) ? TRUE
: FALSE
;
264 ULONG orig_count
= count
;
265 ULONG max_entries
= use_AHCI
? ATA_AHCI_DMA_ENTRIES
: ATA_DMA_ENTRIES
;
266 //ULONG max_frag = use_AHCI ? (0x3fffff+1) : (4096); // DEBUG, replace 4096 for proper chipset-specific value
267 ULONG max_frag
= deviceExtension
->DmaSegmentLength
;
268 ULONG seg_align
= deviceExtension
->DmaSegmentAlignmentMask
;
270 if(AtaReq
->dma_entries
) {
271 AtaReq
->Flags
|= REQ_FLAG_DMA_OPERATION
;
272 KdPrint2((PRINT_PREFIX
"AtapiDmaSetup: already setup, %d entries\n", AtaReq
->dma_entries
));
275 AtaReq
->ata
.dma_base
= 0;
276 AtaReq
->Flags
&= ~REQ_FLAG_DMA_OPERATION
;
278 KdPrint2((PRINT_PREFIX
"AtapiDmaSetup: mode %#x, data %x, count %x, lCh %x, dev %x\n",
279 chan
->lun
[DeviceNumber
]->TransferMode
,
280 data
, count
, lChannel
, DeviceNumber
));
281 if(chan
->lun
[DeviceNumber
]->TransferMode
< ATA_DMA
) {
282 KdPrint2((PRINT_PREFIX
"AtapiDmaSetup: Not DMA mode, assume this is just preparation\n" ));
285 //KdPrint2((PRINT_PREFIX " checkpoint 1\n" ));
287 KdPrint2((PRINT_PREFIX
"AtapiDmaSetup: count=0\n" ));
290 //KdPrint2((PRINT_PREFIX " checkpoint 2\n" ));
291 if(count
> deviceExtension
->MaximumDmaTransferLength
) {
292 KdPrint2((PRINT_PREFIX
"AtapiDmaSetup: deviceExtension->MaximumDmaTransferLength > count\n" ));
295 //KdPrint2((PRINT_PREFIX " checkpoint 3\n" ));
297 if((ULONG_PTR
)data
& deviceExtension
->AlignmentMask
) {
299 if((ULONG
)data
& deviceExtension
->AlignmentMask
) {
301 KdPrint2((PRINT_PREFIX
"AtapiDmaSetup: unaligned data: %#x (%#x)\n", data
, deviceExtension
->AlignmentMask
));
305 //KdPrint2((PRINT_PREFIX " checkpoint 4\n" ));
307 KdPrint2((PRINT_PREFIX
" get Phys(AHCI_CMD=%x)\n", AtaReq
->ahci
.ahci_cmd_ptr
));
308 dma_base
= AtapiVirtToPhysAddr(HwDeviceExtension
, NULL
, (PUCHAR
)(AtaReq
->ahci
.ahci_cmd_ptr
), &i
, &dma_baseu
);
309 AtaReq
->ahci
.ahci_base64
= 0; // clear before setup
311 KdPrint2((PRINT_PREFIX
" get Phys(PRD=%x)\n", &(AtaReq
->dma_tab
) ));
312 dma_base
= AtapiVirtToPhysAddr(HwDeviceExtension
, NULL
, (PUCHAR
)&(AtaReq
->dma_tab
) /*chan->dma_tab*/, &i
, &dma_baseu
);
315 KdPrint2((PRINT_PREFIX
"AtapiDmaSetup: SRB built-in PRD above 4Gb: %8.8x%8.8x\n", dma_baseu
, dma_base
));
316 if(!deviceExtension
->Host64
) {
317 dma_base
= chan
->DB_PRD_PhAddr
;
318 AtaReq
->Flags
|= REQ_FLAG_DMA_DBUF_PRD
;
322 if(!dma_base
|| !i
|| ((LONG
)(dma_base
) == -1)) {
323 KdPrint2((PRINT_PREFIX
"AtapiDmaSetup: No BASE\n" ));
326 AtaReq
->ata
.dma_base
= dma_base
; // aliased to AtaReq->ahci.ahci_base64
328 KdPrint2((PRINT_PREFIX
" get Phys(data[0]=%x)\n", data
));
329 dma_base
= AtapiVirtToPhysAddr(HwDeviceExtension
, Srb
, data
, &dma_count
, &dma_baseu
);
330 if(dma_baseu
&& dma_count
) {
331 KdPrint2((PRINT_PREFIX
"AtapiDmaSetup: 1st block of buffer above 4Gb: %8.8x%8.8x cnt=%x\n", dma_baseu
, dma_base
, dma_count
));
332 if(!deviceExtension
->Host64
) {
335 dma_base
= chan
->DB_IO_PhAddr
;
336 data
= (PUCHAR
)(chan
->DB_IO
);
338 AtaReq
->ahci
.ahci_base64
= (ULONGLONG
)dma_base
| ((ULONGLONG
)dma_baseu
<< 32);
341 if(!dma_count
|| ((LONG
)(dma_base
) == -1)) {
342 KdPrint2((PRINT_PREFIX
"AtapiDmaSetup: No 1st block\n" ));
343 //AtaReq->dma_base = NULL;
344 AtaReq
->ahci
.ahci_base64
= NULL
;
349 dma_count
= min(count
, (PAGE_SIZE
- ((ULONG_PTR
)data
& PAGE_MASK
)));
351 dma_count
= min(count
, (PAGE_SIZE
- ((ULONG
)data
& PAGE_MASK
)));
357 dma_count0
= dma_count
;
358 dma_base0
= dma_base
;
361 /* KdPrint2((PRINT_PREFIX " segments %#x+%#x == %#x && %#x+%#x <= %#x\n",
362 dma_base0, dma_count0, dma_base,
363 dma_count0, dma_count, max_frag));*/
364 if(dma_base0
+dma_count0
== dma_base
&&
365 dma_count0
+dma_count
<= max_frag
) {
366 // 'i' should be always > 0 here
367 // for BM we cannot cross 64k boundary
368 if(dma_base
& seg_align
) {
369 //KdPrint2((PRINT_PREFIX " merge segments\n" ));
373 dma_base
= dma_base0
;
374 dma_count
+= dma_count0
;
378 AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
[i
].base
= dma_base
;
379 AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
[i
].baseu
= dma_baseu
;
380 AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
[i
].Reserved1
= 0;
381 *((PULONG
)&(AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
[i
].DBC_ULONG
)) = ((dma_count
-1) & 0x3fffff);
382 /* AtaReq->ahci.ahci_cmd_ptr->prd_tab[i].Reserved2 = 0;
383 AtaReq->ahci.ahci_cmd_ptr->prd_tab[i].I = 0;*/
384 KdPrint2((PRINT_PREFIX
" ph data[%d]=%x:%x (%x)\n", i
, dma_baseu
, dma_base
, AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
[i
].DBC
));
386 AtaReq
->dma_tab
[i
].base
= dma_base
;
387 AtaReq
->dma_tab
[i
].count
= (dma_count
& 0xffff);
389 dma_count0
= dma_count
;
390 dma_base0
= dma_base
;
392 if (i
>= max_entries
) {
393 KdPrint2((PRINT_PREFIX
"too many segments in DMA table\n" ));
394 //AtaReq->dma_base = NULL;
395 AtaReq
->ahci
.ahci_base64
= NULL
;
398 KdPrint2((PRINT_PREFIX
" get Phys(data[n=%d+%x]=%x)\n", i
, dma_count0
, data
));
399 dma_base
= AtapiVirtToPhysAddr(HwDeviceExtension
, Srb
, data
, &dma_count
, &dma_baseu
);
400 if(dma_baseu
&& dma_count
) {
401 KdPrint2((PRINT_PREFIX
"AtapiDmaSetup: block of buffer above 4Gb: %8.8x%8.8x, cnt=%x\n", dma_baseu
, dma_base
, dma_count
));
402 if(!deviceExtension
->Host64
) {
404 KdPrint2((PRINT_PREFIX
"AtapiDmaSetup: *ERROR* special buffer above 4Gb: %8.8x%8.8x\n", dma_baseu
, dma_base
));
411 if(!dma_count
|| !dma_base
|| ((LONG
)(dma_base
) == -1)) {
412 //AtaReq->dma_base = NULL;
413 AtaReq
->ahci
.ahci_base64
= 0;
414 KdPrint2((PRINT_PREFIX
"AtapiDmaSetup: No NEXT block\n" ));
418 dma_count
= min(count
, PAGE_SIZE
);
419 data
+= min(count
, PAGE_SIZE
);
420 count
-= min(count
, PAGE_SIZE
);
422 KdPrint2((PRINT_PREFIX
" set TERM\n" ));
423 /* KdPrint2((PRINT_PREFIX " segments %#x+%#x == %#x && #x+%#x <= %#x\n",
424 dma_base0, dma_count0, dma_base,
425 dma_count0, dma_count, max_frag));*/
426 if(dma_base0
+dma_count0
== dma_base
&&
427 dma_count0
+dma_count
<= max_frag
) {
428 // 'i' should be always > 0 here
429 if(dma_base
& seg_align
) {
430 //KdPrint2((PRINT_PREFIX " merge segments\n" ));
434 dma_base
= dma_base0
;
435 dma_count
+= dma_count0
;
439 AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
[i
].base
= dma_base
;
440 AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
[i
].baseu
= dma_baseu
;
441 AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
[i
].Reserved1
= 0;
442 //AtaReq->ahci.ahci_cmd_ptr->prd_tab[i].DBC = ((dma_count-1) & 0x3fffff);
443 //AtaReq->ahci.ahci_cmd_ptr->prd_tab[i].Reserved2 = 0;
444 *((PULONG
)&(AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
[i
].DBC_ULONG
)) = ((dma_count
-1) & 0x3fffff);
445 //AtaReq->ahci.ahci_cmd_ptr->prd_tab[i].I = 1; // interrupt when ready
446 KdPrint2((PRINT_PREFIX
" ph data[%d]=%x:%x (%x)\n", i
, dma_baseu
, dma_base
, AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
[i
].DBC
));
448 if(((ULONG_PTR
)&(AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
) & ~PAGE_MASK
) != ((ULONG_PTR
)&(AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
[i
]) & ~PAGE_MASK
)) {
450 if(((ULONG
)&(AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
) & ~PAGE_MASK
) != ((ULONG
)&(AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
[i
]) & ~PAGE_MASK
)) {
452 KdPrint2((PRINT_PREFIX
"PRD table crosses page boundary! %x vs %x\n",
453 &AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
, &(AtaReq
->ahci
.ahci_cmd_ptr
->prd_tab
[i
]) ));
454 //AtaReq->Flags |= REQ_FLAG_DMA_DBUF_PRD;
457 AtaReq
->dma_tab
[i
].base
= dma_base
;
458 AtaReq
->dma_tab
[i
].count
= (dma_count
& 0xffff) | ATA_DMA_EOT
;
460 if(((ULONG_PTR
)&(AtaReq
->dma_tab
) & ~PAGE_MASK
) != ((ULONG_PTR
)&(AtaReq
->dma_tab
[i
]) & ~PAGE_MASK
)) {
462 if(((ULONG
)&(AtaReq
->dma_tab
) & ~PAGE_MASK
) != ((ULONG
)&(AtaReq
->dma_tab
[i
]) & ~PAGE_MASK
)) {
464 KdPrint2((PRINT_PREFIX
"DMA table crosses page boundary! %x vs %x\n",
465 &AtaReq
->dma_tab
, &(AtaReq
->dma_tab
[i
]) ));
466 //AtaReq->Flags |= REQ_FLAG_DMA_DBUF_PRD;
469 AtaReq
->dma_entries
= i
+1;
472 AtaReq
->Flags
|= REQ_FLAG_DMA_DBUF
;
474 AtaReq
->Flags
|= REQ_FLAG_DMA_OPERATION
;
476 KdPrint2((PRINT_PREFIX
"AtapiDmaSetup: OK\n" ));
479 } // end AtapiDmaSetup()
484 PVOID HwDeviceExtension
,
485 PSCSI_REQUEST_BLOCK Srb
,
491 PHW_DEVICE_EXTENSION deviceExtension
= (PHW_DEVICE_EXTENSION
)HwDeviceExtension
;
498 // This must never be called after DMA operation !!!
499 KdPrint2((PRINT_PREFIX
"AtapiDmaPioSync: data %#x, len %#x\n", data
, count
));
502 KdPrint2((PRINT_PREFIX
"AtapiDmaPioSync: !Srb\n" ));
506 AtaReq
= (PATA_REQ
)(Srb
->SrbExtension
);
508 // do nothing on PCI (This can be changed. We cannot guarantee,
509 // that CommonBuffer will always point to User's buffer,
510 // however, this usually happens on PCI-32)
511 if(deviceExtension
->OrigAdapterInterfaceType
== PCIBus
) {
514 // do nothing for DMA
515 if(AtaReq
->Flags
& REQ_FLAG_DMA_OPERATION
) {
520 KdPrint2((PRINT_PREFIX
"AtapiDmaPioSync: !data\n" ));
525 dma_base
= AtapiVirtToPhysAddr(HwDeviceExtension
, Srb
, data
, &dma_count
);
527 KdPrint2((PRINT_PREFIX
"AtapiDmaPioSync: !dma_base for data %#x\n", data
));
530 DmaBuffer
= (PUCHAR
)ScsiPortGetVirtualAddress(HwDeviceExtension
,
531 ScsiPortConvertUlongToPhysicalAddress(dma_base
));
533 KdPrint2((PRINT_PREFIX
"AtapiDmaPioSync: !DmaBuffer for dma_base %#x\n", dma_base
));
536 len
= min(dma_count
, count
);
537 memcpy(DmaBuffer
, data
, len
);
544 } // end AtapiDmaPioSync()
550 PSCSI_REQUEST_BLOCK Srb
555 AtaReq
= (PATA_REQ
)(Srb
->SrbExtension
);
556 if((Srb
->SrbFlags
& SRB_FLAGS_DATA_IN
) &&
557 (AtaReq
->Flags
& REQ_FLAG_DMA_DBUF
)) {
558 KdPrint2((PRINT_PREFIX
" AtapiDmaDBSync is issued.\n"));
560 KdPrint2((PRINT_PREFIX
" DBUF (Read)\n"));
561 RtlCopyMemory(AtaReq
->DataBuffer
, chan
->DB_IO
,
562 Srb
->DataTransferLength
);
565 } // end AtapiDmaDBSync()
570 IN PVOID HwDeviceExtension
,
572 PSCSI_REQUEST_BLOCK Srb
575 PHW_DEVICE_EXTENSION deviceExtension
= (PHW_DEVICE_EXTENSION
)HwDeviceExtension
;
576 PATA_REQ AtaReq
= (PATA_REQ
)(Srb
->SrbExtension
);
578 if(!AtaReq
->ata
.dma_base
) {
579 KdPrint2((PRINT_PREFIX
"AtapiDmaDBPreSync: *** !AtaReq->ata.dma_base\n"));
582 // GetStatus(chan, statusByte2);
583 if(AtaReq
->Flags
& REQ_FLAG_DMA_DBUF_PRD
) {
584 KdPrint2((PRINT_PREFIX
" DBUF_PRD\n"));
586 if(deviceExtension
->HwFlags
& UNIATA_AHCI
) {
587 RtlCopyMemory(chan
->DB_PRD
, AtaReq
->ahci
.ahci_cmd_ptr
, sizeof(AtaReq
->ahci_cmd0
));
589 RtlCopyMemory(chan
->DB_PRD
, &(AtaReq
->dma_tab
), sizeof(AtaReq
->dma_tab
));
592 if(!(Srb
->SrbFlags
& SRB_FLAGS_DATA_IN
) &&
593 (AtaReq
->Flags
& REQ_FLAG_DMA_DBUF
)) {
594 KdPrint2((PRINT_PREFIX
" DBUF (Write)\n"));
596 RtlCopyMemory(chan
->DB_IO
, AtaReq
->DataBuffer
,
597 Srb
->DataTransferLength
);
600 } // end AtapiDmaDBPreSync()
605 IN PVOID HwDeviceExtension
,
606 IN ULONG DeviceNumber
,
607 IN ULONG lChannel
, // logical channel,
608 IN PSCSI_REQUEST_BLOCK Srb
611 PHW_DEVICE_EXTENSION deviceExtension
= (PHW_DEVICE_EXTENSION
)HwDeviceExtension
;
612 //PIDE_BUSMASTER_REGISTERS BaseIoAddressBM = deviceExtension->BaseIoAddressBM[lChannel];
613 PATA_REQ AtaReq
= (PATA_REQ
)(Srb
->SrbExtension
);
614 PHW_CHANNEL chan
= &deviceExtension
->chan
[lChannel
];
616 ULONG VendorID
= deviceExtension
->DevID
& 0xffff;
617 ULONG ChipType
= deviceExtension
->HwFlags
& CHIPTYPE_MASK
;
618 // UCHAR statusByte2;
620 GetStatus(chan, statusByte2);
621 KdPrint2((PRINT_PREFIX "AtapiDmaStart: %s on %#x:%#x\n",
622 (Srb->SrbFlags & SRB_FLAGS_DATA_IN) ? "read" : "write",
623 lChannel, DeviceNumber ));
625 if(!AtaReq
->ata
.dma_base
) {
626 KdPrint2((PRINT_PREFIX
"AtapiDmaStart: *** !AtaReq->ata.dma_base\n"));
629 KdPrint2((PRINT_PREFIX
"AtapiDmaStart: lchan=%d\n", lChannel
));
632 // GetStatus(chan, statusByte2);
633 if(AtaReq->Flags & REQ_FLAG_DMA_DBUF_PRD) {
634 KdPrint2((PRINT_PREFIX " DBUF_PRD\n"));
636 if(deviceExtension->HwFlags & UNIATA_AHCI) {
637 RtlCopyMemory(chan->DB_PRD, AtaReq->ahci.ahci_cmd_ptr, sizeof(AtaReq->ahci_cmd0));
639 RtlCopyMemory(chan->DB_PRD, &(AtaReq->dma_tab), sizeof(AtaReq->dma_tab));
642 if(!(Srb->SrbFlags & SRB_FLAGS_DATA_IN) &&
643 (AtaReq->Flags & REQ_FLAG_DMA_DBUF)) {
644 KdPrint2((PRINT_PREFIX " DBUF (Write)\n"));
646 RtlCopyMemory(chan->DB_IO, AtaReq->DataBuffer,
647 Srb->DataTransferLength);
651 chan
->ChannelCtrlFlags
|= CTRFLAGS_DMA_ACTIVE
;
655 if(ChipType
== PRNEW
) {
656 ULONG Channel
= deviceExtension
->Channel
+ lChannel
;
658 if(chan
->ChannelCtrlFlags
& CTRFLAGS_LBA48
) {
659 AtapiWritePortEx4(chan
, (ULONGIO_PTR
)(&deviceExtension
->BaseIoAddressBM_0
),(Channel
? 0x24 : 0x20),
660 ((Srb
->SrbFlags
& SRB_FLAGS_DATA_IN
) ? 0x05000000 : 0x06000000) | (Srb
->DataTransferLength
>> 1)
665 if(deviceExtension->MemIo) {
667 AtapiWritePort4(chan,
669 (AtapiReadPort4(chan,
670 IDX_BM_Command) & ~0x000000c0) |
671 ((Srb->SrbFlags & SRB_FLAGS_DATA_IN) ? 0x00000080 : 0x000000c0) );
678 // set pointer to Pointer Table
679 AtapiWritePort4(chan
, IDX_BM_PRD_Table
,
682 // set transfer direction
683 AtapiWritePort1(chan
, IDX_BM_Command
,
684 (Srb
->SrbFlags
& SRB_FLAGS_DATA_IN
) ? BM_COMMAND_READ
: BM_COMMAND_WRITE
);
685 // clear Error & Intr bits (writeing 1 clears bits)
686 // set DMA capability bit
687 AtapiWritePort1(chan
, IDX_BM_Status
,
688 AtapiReadPort1(chan
, IDX_BM_Status
) |
689 (BM_STATUS_INTR
| BM_STATUS_ERR
) /*|
690 (DeviceNumber ? BM_STATUS_DRIVE_1_DMA : BM_STATUS_DRIVE_0_DMA)*/);
692 AtapiWritePort1(chan
, IDX_BM_Command
,
693 AtapiReadPort1(chan
, IDX_BM_Command
) |
694 BM_COMMAND_START_STOP
);
697 } // end AtapiDmaStart()
702 IN PVOID HwDeviceExtension
,
703 IN ULONG DeviceNumber
,
704 IN ULONG lChannel
, // logical channel,
705 IN PSCSI_REQUEST_BLOCK Srb
708 PHW_DEVICE_EXTENSION deviceExtension
= (PHW_DEVICE_EXTENSION
)HwDeviceExtension
;
709 //PIDE_BUSMASTER_REGISTERS BaseIoAddressBM = deviceExtension->BaseIoAddressBM[lChannel];
710 PHW_CHANNEL chan
= &deviceExtension
->chan
[lChannel
];
713 ULONG VendorID
= deviceExtension
->DevID
& 0xffff;
714 ULONG ChipType
= deviceExtension
->HwFlags
& CHIPTYPE_MASK
;
716 KdPrint2((PRINT_PREFIX
"AtapiDmaDone: dev %d\n", DeviceNumber
));
718 if(deviceExtension
->HwFlags
& UNIATA_AHCI
) {
719 KdPrint2((PRINT_PREFIX
" ACHTUNG! should not be called for AHCI!\n"));
720 return IDE_STATUS_WRONG
;
725 if(ChipType
== PRNEW
) {
726 ULONG Channel
= deviceExtension
->Channel
+ lChannel
;
728 AtapiWritePortEx1(chan, (ULONGIO_PTR)(&deviceExtension->BaseIoAddressBM_0),0x11,
729 AtapiReadPortEx1(chan, (ULONGIO_PTR)(&deviceExtension->BaseIoAddressBM_0),0x11) &
730 ~(Channel ? 0x08 : 0x02));
732 if(chan
->ChannelCtrlFlags
& CTRFLAGS_LBA48
) {
733 AtapiWritePortEx4(chan
, (ULONGIO_PTR
)(&deviceExtension
->BaseIoAddressBM_0
),(Channel
? 0x24 : 0x20),
739 if(deviceExtension->MemIo) {
741 AtapiWritePort4(chan,
743 (AtapiReadPort4(chan,
744 IDX_BM_Command) & ~0x00000080) );
746 chan->ChannelCtrlFlags &= ~CTRFLAGS_DMA_ACTIVE;
754 dma_status
= AtapiReadPort1(chan
, IDX_BM_Status
) & BM_STATUS_MASK
;
756 AtapiWritePort1(chan
, IDX_BM_Command
,
757 AtapiReadPort1(chan
, IDX_BM_Command
) &
758 ~BM_COMMAND_START_STOP
);
759 // clear interrupt and error status
760 AtapiWritePort1(chan
, IDX_BM_Status
, BM_STATUS_ERR
| BM_STATUS_INTR
);
762 chan
->ChannelCtrlFlags
&= ~CTRFLAGS_DMA_ACTIVE
;
766 } // end AtapiDmaDone()
771 IN PHW_DEVICE_EXTENSION deviceExtension
,
772 IN PHW_LU_EXTENSION LunExt
,
778 if((deviceExtension
->HwFlags
& UNIATA_AHCI
) &&
779 !(LunExt
->DeviceFlags
& DFLAGS_ATAPI_DEVICE
)) {
780 // skip unnecessary checks
781 KdPrint2((PRINT_PREFIX
"AtapiDmaReinit: ahci, nothing to do for HDD\n"));
785 apiomode
= (CHAR
)AtaPioMode(&(LunExt
->IdentifyData
));
787 if(!(AtaReq
->Flags
& REQ_FLAG_DMA_OPERATION
)) {
788 KdPrint2((PRINT_PREFIX
789 "AtapiDmaReinit: !(AtaReq->Flags & REQ_FLAG_DMA_OPERATION), fall to PIO on Device %d\n", LunExt
->Lun
));
792 if(deviceExtension
->HwFlags
& UNIATA_AHCI
) {
793 if(!AtaReq
->ahci
.ahci_base64
) {
794 KdPrint2((PRINT_PREFIX
795 "AtapiDmaReinit: no AHCI PRD, fatal on Device %d\n", LunExt
->Lun
));
799 if(!AtaReq
->ata
.dma_base
) {
800 KdPrint2((PRINT_PREFIX
801 "AtapiDmaReinit: no PRD, fall to PIO on Device %d\n", LunExt
->Lun
));
805 if((deviceExtension
->HbaCtrlFlags
& HBAFLAGS_DMA_DISABLED_LBA48
) &&
806 (AtaReq
->lba
>= (LONGLONG
)ATA_MAX_LBA28
) &&
807 (LunExt
->TransferMode
> ATA_PIO5
) ) {
808 KdPrint2((PRINT_PREFIX
809 "AtapiDmaReinit: FORCE_DOWNRATE on Device %d for LBA48\n", LunExt
->Lun
));
814 if(AtaReq
->Flags
& REQ_FLAG_FORCE_DOWNRATE
) {
815 KdPrint2((PRINT_PREFIX
816 "AtapiDmaReinit: FORCE_DOWNRATE on Device %d\n", LunExt
->Lun
));
817 if(AtaReq
->lba
>= (LONGLONG
)ATA_MAX_LBA28
) {
819 LunExt
->DeviceFlags
|= REQ_FLAG_FORCE_DOWNRATE_LBA48
;
821 // do not make extra work if we already use PIO
822 if(/*LunExt->TransferMode >= ATA_DMA*/
823 (LunExt
->TransferMode
> ATA_PIO5
) && (LunExt
->TransferMode
!= ATA_PIO0
+apiomode
)
825 KdPrint2((PRINT_PREFIX
826 "AtapiDmaReinit: set PIO mode on Device %d (%x -> %x)\n", LunExt
->Lun
, LunExt
->TransferMode
, ATA_PIO0
+apiomode
));
827 AtapiDmaInit(deviceExtension
, LunExt
->Lun
, LunExt
->chan
->lChannel
,
832 if(LunExt
->LimitedTransferMode
< LunExt
->TransferMode
) {
833 KdPrint2((PRINT_PREFIX
834 "AtapiDmaReinit: set PIO mode on Device %d (%x -> %x) (2)\n", LunExt
->Lun
, LunExt
->TransferMode
, LunExt
->LimitedTransferMode
));
835 AtapiDmaInit(deviceExtension
, LunExt
->Lun
, LunExt
->chan
->lChannel
,
836 LunExt
->LimitedTransferMode
-ATA_PIO0
,
842 KdPrint2((PRINT_PREFIX
843 "AtapiDmaReinit: set MAX mode on Device %d\n", LunExt
->Lun
));
844 AtapiDmaInit(deviceExtension
, LunExt
->Lun
, LunExt
->chan
->lChannel
,
846 min( retry_Wdma
[AtaReq
->retry
],
847 (CHAR
)AtaWmode(&(LunExt
->IdentifyData
)) ),
848 min( retry_Udma
[AtaReq
->retry
],
849 (CHAR
)AtaUmode(&(LunExt
->IdentifyData
)) ) );
851 // LunExt->DeviceFlags &= ~DFLAGS_FORCE_DOWNRATE;
853 if(/*!(LunExt->DeviceFlags & DFLAGS_FORCE_DOWNRATE) &&*/
854 (LunExt
->LimitedTransferMode
>
855 LunExt
->TransferMode
) ||
856 (LunExt
->DeviceFlags
& DFLAGS_REINIT_DMA
) ||
857 ((deviceExtension
->HwFlags
& UNIATA_CHAN_TIMINGS
) && ((ULONG
)LunExt
->Lun
!= LunExt
->chan
->last_cdev
))) {
859 KdPrint2((PRINT_PREFIX
860 "AtapiDmaReinit: restore IO mode on Device %d, last dev %d\n", LunExt
->Lun
, LunExt
->chan
->last_cdev
));
861 AtapiDmaInit__(deviceExtension
, LunExt
);
863 KdPrint2((PRINT_PREFIX
864 "AtapiDmaReinit: LimitedTransferMode == TransferMode = %x (%x), Device %d, last dev %d\n", LunExt
->TransferMode
, LunExt
->DeviceFlags
, LunExt
->Lun
, LunExt
->chan
->last_cdev
));
869 } // end AtapiDmaReinit()
874 IN PHW_DEVICE_EXTENSION deviceExtension
,
875 IN PHW_LU_EXTENSION LunExt
878 if(LunExt
->IdentifyData
.SupportDma
||
879 (LunExt
->IdentifyData
.AtapiDMA
.DMASupport
&& (LunExt
->DeviceFlags
& DFLAGS_ATAPI_DEVICE
))) {
880 KdPrint2((PRINT_PREFIX
881 "AtapiDmaInit__: Set (U)DMA on Device %d\n", LunExt
->Lun
));
882 /* for(i=AtaUmode(&(LunExt->IdentifyData)); i>=0; i--) {
883 AtapiDmaInit(deviceExtension, ldev & 1, ldev >> 1,
884 (CHAR)AtaPioMode(&(LunExt->IdentifyData)),
885 (CHAR)AtaWmode(&(LunExt->IdentifyData)),
886 UDMA_MODE0+(CHAR)i );
888 for(i=AtaWmode(&(LunExt->IdentifyData)); i>=0; i--) {
889 AtapiDmaInit(deviceExtension, ldev & 1, ldev >> 1,
890 (CHAR)AtaPioMode(&(LunExt->IdentifyData)),
891 (CHAR)AtaWmode(&(LunExt->IdentifyData)),
892 UDMA_MODE0+(CHAR)i );
894 AtapiDmaInit(deviceExtension
, LunExt
->Lun
, LunExt
->chan
->lChannel
,
895 (CHAR
)AtaPioMode(&(LunExt
->IdentifyData
)),
896 (CHAR
)AtaWmode(&(LunExt
->IdentifyData
)),
897 (CHAR
)AtaUmode(&(LunExt
->IdentifyData
)) );
899 KdPrint2((PRINT_PREFIX
900 "AtapiDmaInit__: Set PIO on Device %d\n", LunExt
->Lun
));
901 AtapiDmaInit(deviceExtension
, LunExt
->Lun
, LunExt
->chan
->lChannel
,
902 (CHAR
)AtaPioMode(&(LunExt
->IdentifyData
)), -1, -1);
904 } // end AtapiDmaInit__()
909 IN PHW_DEVICE_EXTENSION deviceExtension
,
910 IN ULONG DeviceNumber
,
911 IN ULONG lChannel
, // logical channel,
912 IN PHW_LU_EXTENSION LunExt
,
916 KdPrint3((PRINT_PREFIX
917 "AtaSetTransferMode: Set %#x on Device %d/%d\n", mode
, lChannel
, DeviceNumber
));
920 BOOLEAN disable_intr
= (deviceExtension
->HwFlags
& UNIATA_AHCI
) || (CPU_num
>1);
922 if(deviceExtension->HwFlags & UNIATA_SATA) {
923 // experimental do nothing, see ROS BUG-9119, v0.46a1
924 statusByte = IDE_STATUS_IDLE;
926 if(LunExt
->DeviceFlags
& DFLAGS_MANUAL_CHS
) {
927 statusByte
= mode
<= ATA_PIO2
? IDE_STATUS_IDLE
: IDE_STATUS_ERROR
;
931 AtapiDisableInterrupts(deviceExtension
, lChannel
);
933 if(mode
> ATA_UDMA6
) {
934 // for SATA, it doesn't have option to set transfer rate
935 // We need this just to switch between PIO and DMA modes
936 // Devices may support only some lower transfer rates (e.g. UDMA0-4)
937 umode
= min((UCHAR
)AtaUmode(&(LunExt
->IdentifyData
)), 6);
942 statusByte
= AtaCommand(deviceExtension
, DeviceNumber
, lChannel
,
943 IDE_COMMAND_SET_FEATURES
, 0, 0, 0,
944 (UCHAR
)umode
, ATA_C_F_SETXFER
, ATA_WAIT_BASE_READY
);
946 AtapiEnableInterrupts(deviceExtension
, lChannel
);
949 if(statusByte
& IDE_STATUS_ERROR
) {
950 KdPrint3((PRINT_PREFIX
" wait ready after error\n"));
951 if(LunExt
->DeviceFlags
& DFLAGS_ATAPI_DEVICE
) {
952 AtapiStallExecution(10);
954 AtapiStallExecution(100);
956 apiomode
= (CHAR
)AtaPioMode(&(LunExt
->IdentifyData
));
957 if( (apiomode
> 0) &&
958 ((CHAR
)AtaWmode(&(LunExt
->IdentifyData
)) > 0) &&
959 ((CHAR
)AtaUmode(&(LunExt
->IdentifyData
)) > 0)
963 if(mode
> ATA_PIO2
) {
966 KdPrint3((PRINT_PREFIX
" assume that drive doesn't support mode swithing using PIO%d\n", apiomode
));
967 mode
= ATA_PIO0
+ apiomode
;
969 // SATA sets actual transfer rate in LunExt on init.
970 // There is no run-time SATA rate adjustment yet.
971 // On the other hand, we may turn SATA device in PIO mode
972 LunExt
->TransferMode
= (UCHAR
)mode
;
973 if(deviceExtension
->HwFlags
& UNIATA_SATA
) {
974 if(mode
< ATA_SA150
) {
975 LunExt
->PhyTransferMode
= max(LunExt
->PhyTransferMode
, LunExt
->TransferMode
);
977 LunExt
->PhyTransferMode
= LunExt
->TransferMode
;
980 if(LunExt
->DeviceFlags
& DFLAGS_ATAPI_DEVICE
) {
981 LunExt
->PhyTransferMode
= max(LunExt
->LimitedTransferMode
, LunExt
->TransferMode
);
983 LunExt
->PhyTransferMode
= LunExt
->TransferMode
;
987 } // end AtaSetTransferMode()
992 IN PVOID HwDeviceExtension
,
993 IN ULONG DeviceNumber
,
994 IN ULONG lChannel
, // logical channel,
995 // is always 0 except simplex-only controllers
1001 PHW_DEVICE_EXTENSION deviceExtension
= (PHW_DEVICE_EXTENSION
)HwDeviceExtension
;
1002 ULONG Channel
= deviceExtension
->Channel
+ lChannel
;
1003 PHW_CHANNEL chan
= &deviceExtension
->chan
[lChannel
];
1004 //LONG statusByte = 0;
1005 ULONG dev
= Channel
*2 + DeviceNumber
; // for non-SATA/AHCI only!
1006 //ULONG ldev = lChannel*2 + DeviceNumber; // for non-SATA/AHCI only!
1007 BOOLEAN isAtapi
= ATAPI_DEVICE(chan
, DeviceNumber
);
1008 ULONG slotNumber
= deviceExtension
->slotNumber
;
1009 ULONG SystemIoBusNumber
= deviceExtension
->SystemIoBusNumber
;
1011 PHW_LU_EXTENSION LunExt
= chan
->lun
[DeviceNumber
];
1014 ULONG VendorID
= deviceExtension
->DevID
& 0xffff;
1015 //ULONG DeviceID = (deviceExtension->DevID >> 16) & 0xffff;
1016 //ULONG RevID = deviceExtension->RevID;
1017 ULONG ChipType
= deviceExtension
->HwFlags
& CHIPTYPE_MASK
;
1018 ULONG ChipFlags
= deviceExtension
->HwFlags
& CHIPFLAG_MASK
;
1020 LONG statusByte
= 0;
1022 //UCHAR *reg_val = NULL;
1024 LunExt
->DeviceFlags
&= ~DFLAGS_REINIT_DMA
;
1025 /* set our most pessimistic default mode */
1026 LunExt
->TransferMode
= ATA_PIO
;
1027 // if(!deviceExtension->BaseIoAddressBM[lChannel]) {
1028 if(!deviceExtension
->BusMaster
) {
1029 KdPrint2((PRINT_PREFIX
" !deviceExtension->BusMaster: NO DMA\n"));
1030 wdmamode
= udmamode
= -1;
1033 // Limit transfer mode (controller limitation)
1034 if((LONG
)chan
->MaxTransferMode
>= ATA_UDMA
) {
1035 KdPrint2((PRINT_PREFIX
"AtapiDmaInit: chan->MaxTransferMode >= ATA_UDMA\n"));
1036 udmamode
= min( udmamode
, (CHAR
)(chan
->MaxTransferMode
- ATA_UDMA
));
1038 if((LONG
)chan
->MaxTransferMode
>= ATA_WDMA
) {
1039 KdPrint2((PRINT_PREFIX
"AtapiDmaInit: chan->MaxTransferMode >= ATA_WDMA\n"));
1041 wdmamode
= min( wdmamode
, (CHAR
)(chan
->MaxTransferMode
- ATA_WDMA
));
1043 if((LONG
)chan
->MaxTransferMode
>= ATA_PIO0
) {
1044 KdPrint2((PRINT_PREFIX
"AtapiDmaInit: NO DMA\n"));
1045 wdmamode
= udmamode
= -1;
1046 apiomode
= min( apiomode
, (CHAR
)(chan
->MaxTransferMode
- ATA_PIO0
));
1048 KdPrint2((PRINT_PREFIX
"AtapiDmaInit: PIO0\n"));
1049 wdmamode
= udmamode
= -1;
1052 // Limit transfer mode (device limitation)
1053 KdPrint2((PRINT_PREFIX
"AtapiDmaInit: LunExt->LimitedTransferMode %#x\n", LunExt
->LimitedTransferMode
));
1054 if((LONG
)LunExt
->LimitedTransferMode
>= ATA_UDMA
) {
1055 KdPrint2((PRINT_PREFIX
"AtapiDmaInit: LunExt->MaxTransferMode >= ATA_UDMA => %#x\n",
1056 min( udmamode
, (CHAR
)(LunExt
->LimitedTransferMode
- ATA_UDMA
))
1058 udmamode
= min( udmamode
, (CHAR
)(LunExt
->LimitedTransferMode
- ATA_UDMA
));
1060 if((LONG
)LunExt
->LimitedTransferMode
>= ATA_WDMA
) {
1061 KdPrint2((PRINT_PREFIX
"AtapiDmaInit: LunExt->MaxTransferMode >= ATA_WDMA => %#x\n",
1062 min( wdmamode
, (CHAR
)(LunExt
->LimitedTransferMode
- ATA_WDMA
))
1065 wdmamode
= min( wdmamode
, (CHAR
)(LunExt
->LimitedTransferMode
- ATA_WDMA
));
1067 if((LONG
)LunExt
->LimitedTransferMode
>= ATA_PIO0
) {
1068 KdPrint2((PRINT_PREFIX
"AtapiDmaInit: lun NO DMA\n"));
1069 wdmamode
= udmamode
= -1;
1070 apiomode
= min( apiomode
, (CHAR
)(LunExt
->LimitedTransferMode
- ATA_PIO0
));
1072 KdPrint2((PRINT_PREFIX
"AtapiDmaInit: lun PIO0\n"));
1073 wdmamode
= udmamode
= -1;
1077 //if(!(ChipFlags & UNIATA_AHCI)) {
1079 // this is necessary for future PM support
1080 SelectDrive(chan
, DeviceNumber
);
1081 GetStatus(chan
, statusByte
);
1082 // we can see here IDE_STATUS_ERROR status after previous operation
1083 if(statusByte
& IDE_STATUS_ERROR
) {
1084 KdPrint2((PRINT_PREFIX
"IDE_STATUS_ERROR detected on entry, statusByte = %#x\n", statusByte
));
1085 //GetBaseStatus(chan, statusByte);
1087 if(statusByte
&& UniataIsIdle(deviceExtension
, statusByte
& ~IDE_STATUS_ERROR
) != IDE_STATUS_IDLE
) {
1088 KdPrint2((PRINT_PREFIX
"Can't setup transfer mode: statusByte = %#x\n", statusByte
));
1093 chan
->last_cdev
= DeviceNumber
;
1094 if(UniataIsSATARangeAvailable(deviceExtension
, lChannel
) ||
1095 (ChipFlags
& UNIATA_AHCI
) || (chan
->MaxTransferMode
>= ATA_SA150
)
1097 //if(ChipFlags & (UNIATA_SATA | UNIATA_AHCI)) {
1102 KdPrint2((PRINT_PREFIX
"SATA Generic\n"));
1104 if((udmamode
>= 5) || (ChipFlags
& UNIATA_AHCI
) || ((udmamode
>= 0) && (chan
->MaxTransferMode
>= ATA_SA150
))) {
1105 /* some drives report UDMA6, some UDMA5 */
1106 /* ATAPI may not have SataCapabilities set in IDENTIFY DATA */
1107 if(ata_is_sata(&(LunExt
->IdentifyData
))) {
1108 //udmamode = min(udmamode, 6);
1109 KdPrint2((PRINT_PREFIX
"LunExt->LimitedTransferMode %x, LunExt->OrigTransferMode %x\n",
1110 LunExt
->LimitedTransferMode
, LunExt
->OrigTransferMode
));
1111 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, min(LunExt
->LimitedTransferMode
, LunExt
->OrigTransferMode
))) {
1114 udmamode
= min(udmamode
, 6);
1117 KdPrint2((PRINT_PREFIX
"SATA -> PATA adapter ?\n"));
1118 if (udmamode
> 2 && (!LunExt
->IdentifyData
.HwResCableId
&& (LunExt
->IdentifyData
.HwResValid
== IDENTIFY_CABLE_ID_VALID
) )) {
1119 KdPrint2((PRINT_PREFIX
"AtapiDmaInit: DMA limited to UDMA33, non-ATA66 compliant cable\n"));
1121 apiomode
= min( apiomode
, (CHAR
)(LunExt
->LimitedTransferMode
- ATA_PIO0
));
1123 udmamode
= min(udmamode
, 6);
1128 ModeByte
= ATA_UDMA0
+ udmamode
;
1131 ModeByte
= ATA_WDMA0
+ wdmamode
;
1134 ModeByte
= ATA_PIO0
+ apiomode
;
1139 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ModeByte
);
1143 if(deviceExtension
->UnknownDev
) {
1144 KdPrint2((PRINT_PREFIX
"Unknown chip, omit Vendor/Dev checks\n"));
1145 goto try_generic_dma
;
1148 if(udmamode
> 2 && (!LunExt
->IdentifyData
.HwResCableId
&& (LunExt
->IdentifyData
.HwResValid
== IDENTIFY_CABLE_ID_VALID
)) ) {
1149 if(ata_is_sata(&(LunExt
->IdentifyData
))) {
1150 KdPrint2((PRINT_PREFIX
"AtapiDmaInit: SATA beyond adapter or Controller compat mode\n"));
1152 KdPrint2((PRINT_PREFIX
"AtapiDmaInit: DMA limited to UDMA33, non-ATA66 compliant cable\n"));
1154 apiomode
= min( apiomode
, (CHAR
)(LunExt
->LimitedTransferMode
- ATA_PIO
));
1158 KdPrint2((PRINT_PREFIX
"Setup chip a:w:u=%d:%d:%d\n",
1164 case ATA_ACARD_ID
: {
1168 static const USHORT reg4a
= 0xa6;
1169 UCHAR reg
= 0x40 + (UCHAR
)dev
;
1171 if(ChipType
== ATPOLD
) {
1173 static const USHORT reg4x2
= 0x0301;
1175 for(i
=udmamode
; i
>=0; i
--) {
1176 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA
+ i
)) {
1178 ChangePciConfig1(0x54, a
| (0x01 << dev
) | ((i
+1) << (dev
*2)));
1179 SetPciConfig1(0x4a, reg4a
);
1180 SetPciConfig2(reg
, reg4x2
);
1185 if (wdmamode
>= 2 && apiomode
>= 4) {
1186 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA2
)) {
1192 static const UCHAR reg4x
= 0x31;
1194 for(i
=udmamode
; i
>=0; i
--) {
1195 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA
+ i
)) {
1197 ChangePciConfig2(0x44, (a
& ~(0x000f << (dev
* 4))) | ((i
+1) << (dev
*4)));
1198 SetPciConfig1(0x4a, reg4a
);
1199 SetPciConfig1(reg
, reg4x
);
1204 if (wdmamode
>= 2 && apiomode
>= 4) {
1205 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA2
)) {
1210 /* Use GENERIC PIO */
1212 case ATA_ACER_LABS_ID
: {
1213 /************************/
1214 /* Acer Labs Inc. (ALI) */
1215 /************************/
1216 static const UCHAR ali_udma
[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f};
1217 static const ULONG ali_pio
[] =
1218 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
1219 0x00310001, 0x00440001};
1220 /* the older Aladdin doesn't support ATAPI DMA on both master & slave */
1221 if ((ChipFlags
& ALIOLD
) &&
1222 (udmamode
>= 0 || wdmamode
>= 0)) {
1223 if(ATAPI_DEVICE(chan
, 0) &&
1224 ATAPI_DEVICE(chan
, 1)) {
1225 // 2 devices on this channel - NO DMA
1226 chan
->MaxTransferMode
=
1227 min(chan
->MaxTransferMode
, ATA_PIO4
);
1228 udmamode
= wdmamode
= -1;
1232 for(i
=udmamode
; i
>=0; i
--) {
1233 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
1236 GetPciConfig4(0x54, word54
);
1237 word54
&= ~(0x000f000f << (dev
* 4));
1238 word54
|= (((ali_udma
[i
]<<16) | 5) << (dev
* 4));
1239 SetPciConfig4(0x54, word54
);
1240 ChangePciConfig1(0x53, a
| 0x03);
1241 SetPciConfig4(0x58 + (Channel
<<2), 0x00310001);
1245 /* make sure eventual UDMA mode from the BIOS is disabled */
1246 ChangePciConfig2(0x56, a
& ~(0x0008 << (dev
* 4)) );
1247 if (wdmamode
>= 2 && apiomode
>= 4) {
1248 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA2
)) {
1249 ChangePciConfig1(0x53, a
| 0x03);
1250 chan
->ChannelCtrlFlags
|= CTRFLAGS_DMA_RO
;
1254 ChangePciConfig1(0x53, (a
& ~0x01) | 0x02);
1256 for(i
=apiomode
; i
>=0; i
--) {
1257 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ i
)) {
1258 ChangePciConfig4(0x54, a
& ~(0x0008000f << (dev
* 4)));
1259 SetPciConfig4(0x58 + (Channel
<<2), ali_pio
[i
]);
1268 /********************/
1269 /* AMD, nVidia, VIA */
1270 /********************/
1271 if(VendorID
== ATA_VIA_ID
) {
1272 if((ChipFlags
& VIASATA
) &&
1274 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_SA150
);
1277 if((ChipFlags
& VIABAR
) &&
1279 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_SA150
);
1284 static const UCHAR via_modes
[6][7] = {
1285 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* ATA33 and New Chips */
1286 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* ATA66 */
1287 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* ATA100 */
1288 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
1289 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }, /* AMD/nVIDIA */
1290 { 0xee, 0xe8, 0xe6, 0xe4, 0xe2, 0xe1, 0xe0 }}; /* VIA new */
1291 static const UCHAR via_pio
[] =
1292 { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
1293 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
1294 const UCHAR
*reg_val
= NULL
;
1295 UCHAR reg
= 0x53-(UCHAR
)dev
;
1296 UCHAR reg2
= reg
-0x08;
1298 if(ChipFlags
& VIABAR
) {
1303 reg_val
= &via_modes
[ChipType
][0];
1305 if(VendorID
== ATA_NVIDIA_ID
) {
1310 for(i
= udmamode
; i
>=0; i
--) {
1311 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
1312 SetPciConfig1(reg2
, via_pio
[8+i
]);
1313 SetPciConfig1(reg
, (UCHAR
)reg_val
[i
]);
1317 if(!(ChipFlags
& VIABAR
)) {
1318 /* This chip can't do WDMA. */
1319 for(i
= wdmamode
; i
>=0; i
--) {
1320 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
1321 SetPciConfig1(reg2
, via_pio
[5+i
]);
1322 SetPciConfig1(reg
, 0x8b);
1327 /* set PIO mode timings */
1328 if((apiomode
>= 0) && (ChipType
!= VIA133
)) {
1329 SetPciConfig1(reg2
, via_pio
[apiomode
]);
1331 if(VendorID
== ATA_VIA_ID
/*&& (ChipType == VIA33 || ChipType == VIA66)*/) {
1332 via82c_timing(deviceExtension
, dev
, ATA_PIO0
+ apiomode
);
1334 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
1338 case ATA_CYRIX_ID
: {
1346 if(ChipType
== CYRIX_3x
) {
1347 ULONG cyr_piotiming
[] =
1348 { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
1349 ULONG cyr_wdmatiming
[] = { 0x00077771, 0x00012121, 0x00002020 };
1350 ULONG cyr_udmatiming
[] = { 0x00921250, 0x00911140, 0x00911030 };
1351 ULONG mode_reg
= 0x24+(dev
<< 3);
1353 for(i
=udmamode
; i
>=0; i
--) {
1354 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
1355 AtapiWritePortEx4(chan
, (ULONGIO_PTR
)(&deviceExtension
->BaseIoAddressBM_0
), mode_reg
, cyr_udmatiming
[udmamode
]);
1359 for(i
=wdmamode
; i
>=0; i
--) {
1360 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
1361 AtapiWritePortEx4(chan
, (ULONGIO_PTR
)(&deviceExtension
->BaseIoAddressBM_0
), mode_reg
, cyr_wdmatiming
[wdmamode
]);
1365 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
)) {
1366 AtapiWritePortEx4(chan
, (ULONGIO_PTR
)(&deviceExtension
->BaseIoAddressBM_0
), mode_reg
, cyr_piotiming
[apiomode
]);
1370 if(ChipType
== CYRIX_OLD
) {
1371 UCHAR cyr_piotiming_old
[] =
1375 for(i
=wdmamode
; i
>=0; i
--) {
1376 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
1380 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
)) {
1381 timing
= (6-apiomode
) | (cyr_piotiming_old
[i
]);
1382 /* Channel command timing */
1383 SetPciConfig1(0x62+Channel
, timing
);
1384 /* Read command timing */
1385 SetPciConfig1(0x64+Channel
*4+dev
, timing
);
1386 /* Write command timing */
1387 SetPciConfig1(0x66+Channel
*4+dev
, timing
);
1391 if(ChipType
== CYRIX_35
) {
1393 USHORT c35_pio_timings[5] = {
1394 0xF7F4, 0xF173, 0x8141, 0x5131, 0x1131
1396 USHORT c35_pio_cmd_timings[5] = {
1397 0xF7F4, 0x53F3, 0x13F1, 0x5131, 0x1131
1399 ULONG c35_udma_timings[5] = {
1400 0x7F7436A1, 0x7F733481, 0x7F723261, 0x7F713161, 0x7F703061
1402 ULONG c35_mwdma_timings[3] = {
1403 0x7F0FFFF3, 0x7F035352, 0x7F024241
1405 ULONG mode_reg = 0x24+(dev << 3);
1407 /* No MSR support yet, do not touch any regs */
1408 for(i
=udmamode
; i
>=0; i
--) {
1409 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
1413 for(i
=wdmamode
; i
>=0; i
--) {
1414 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
1418 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
)) {
1425 case ATA_NATIONAL_ID
: {
1430 ULONG nat_piotiming
[] =
1431 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
1432 0x00803020, 0x20102010, 0x00100010,
1433 0x00100010, 0x00100010, 0x00100010 };
1434 ULONG nat_dmatiming
[] = { 0x80077771, 0x80012121, 0x80002020 };
1435 ULONG nat_udmatiming
[] = { 0x80921250, 0x80911140, 0x80911030 };
1439 for(i
=udmamode
; i
>=0; i
--) {
1440 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
1441 SetPciConfig4(0x44 + (dev
* 8), nat_udmatiming
[i
]);
1442 SetPciConfig4(0x40 + (dev
* 8), nat_piotiming
[i
+8]);
1446 for(i
=wdmamode
; i
>=0; i
--) {
1447 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
1448 SetPciConfig4(0x44 + (dev
* 8), nat_dmatiming
[i
]);
1449 SetPciConfig4(0x40 + (dev
* 8), nat_piotiming
[i
+5]);
1453 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
)) {
1454 ChangePciConfig4(0x44 + (dev
* 8), a
| 0x80000000);
1455 SetPciConfig4(0x40 + (dev
* 8), nat_piotiming
[apiomode
]);
1461 /* Use GENERIC PIO */
1463 case ATA_CYPRESS_ID
:
1467 if (wdmamode
>= 2 && apiomode
>= 4) {
1468 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA2
)) {
1469 SetPciConfig2(Channel
? 0x4e:0x4c, 0x2020);
1473 /* Use GENERIC PIO */
1475 case ATA_MARVELL_ID
:
1479 for(i
=udmamode
; i
>=0; i
--) {
1480 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
1484 for(i
=wdmamode
; i
>=0; i
--) {
1485 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
1489 /* try generic DMA, use hpt_timing() */
1490 if (wdmamode
>= 0 && apiomode
>= 4) {
1491 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_DMA
)) {
1495 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
1498 case ATA_NETCELL_ID
:
1502 if (wdmamode
>= 2 && apiomode
>= 4) {
1503 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA2
)) {
1507 /* Use GENERIC PIO */
1509 case ATA_HIGHPOINT_ID
: {
1510 /********************/
1511 /* High Point (HPT) */
1512 /********************/
1513 for(i
=udmamode
; i
>=0; i
--) {
1514 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
1515 hpt_timing(deviceExtension
, dev
, (UCHAR
)(ATA_UDMA
+ i
)); // ???
1520 for(i
=wdmamode
; i
>=0; i
--) {
1521 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
1522 hpt_timing(deviceExtension
, dev
, (UCHAR
)(ATA_WDMA0
+i
));
1526 /* try generic DMA, use hpt_timing() */
1527 if (wdmamode
>= 0 && apiomode
>= 4) {
1528 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_DMA
)) {
1532 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
1533 hpt_timing(deviceExtension
, dev
, ATA_PIO0
+ apiomode
);
1536 case ATA_INTEL_ID
: {
1541 KdPrint2((PRINT_PREFIX
"Intel %d\n", Channel
));
1542 BOOLEAN udma_ok
= FALSE
;
1553 UCHAR intel_timings
[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1554 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1555 UCHAR intel_utimings
[] = { 0x00, 0x01, 0x02, 0x01, 0x02, 0x01, 0x02 };
1556 const UCHAR needed_pio
[3] = {
1557 ATA_PIO0
, ATA_PIO3
, ATA_PIO4
1560 if(deviceExtension
->DevID
== ATA_I82371FB
) {
1561 KdPrint2((PRINT_PREFIX
" I82371FB\n"));
1564 for(i
=wdmamode
; i
>=0; i
--) {
1566 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
1572 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
1575 /* If the drive MWDMA is faster than it can do PIO then
1576 we must force PIO into PIO0 */
1577 if (apiomode
< needed_pio
[wdmamode
]) {
1578 /* Enable DMA timing only */
1579 control
|= 8; /* PIO cycles in PIO0 */
1582 GetPciConfig2(0x40+Channel
*2, reg4x
);
1583 if(apiomode
> ATA_PIO0
) {
1584 control
|= 0x03; /* IORDY|TIME0 */
1586 control
|= 0x02; /* IORDY */
1588 // if (ata_pio_need_iordy(adev))
1589 //control |= 2; /* IE */
1591 control
|= 4; /* PPE enable */
1593 /* Mask out the relevant control and timing bits we will load. Also
1594 clear the other drive TIME register as a precaution */
1595 reg4x
&= 0xCC00 | (0x0E << (DeviceNumber
*4));
1596 reg4x
|= control
<< (DeviceNumber
*4);
1597 reg4x
|= intel_timings
[idx
] << 8;
1598 SetPciConfig2(0x40+Channel
*2, reg4x
);
1602 if(deviceExtension
->DevID
== ATA_ISCH
) {
1604 KdPrint2((PRINT_PREFIX
" ISCH\n"));
1605 GetPciConfig4(0x80 + dev
*4, tim
);
1607 for(i
=udmamode
; i
>=0; i
--) {
1608 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
1610 tim
&= ~(0x7 << 16);
1615 apiomode
= ATA_PIO4
;
1620 for(i
=wdmamode
; i
>=0; i
--) {
1621 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
1622 tim
&= ~(0x1 << 31);
1628 apiomode
= (i
== 0) ? ATA_PIO0
:
1629 (i
== 1) ? ATA_PIO3
: ATA_PIO4
;
1635 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
1639 tim
|= (apiomode
& 0x7);
1640 SetPciConfig4(0x80 + dev
*4, tim
);
1645 GetPciConfig2(0x48, reg48
);
1646 // if(!(ChipFlags & ICH4_FIX)) {
1647 GetPciConfig2(0x4a, reg4a
);
1649 GetPciConfig2(0x54, reg54
);
1650 // if(udmamode >= 0) {
1651 // enable the write buffer to be used in a split (ping/pong) manner.
1657 // reg40 &= ~0x00ff00ff;
1658 // reg40 |= 0x40774077;
1660 for(i
=udmamode
; i
>=0; i
--) {
1661 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
1663 /* Set UDMA reference clock (33 MHz or more). */
1664 SetPciConfig1(0x48, reg48
| (0x0001 << dev
));
1665 // if(!(ChipFlags & ICH4_FIX)) {
1666 if(deviceExtension
->MaxTransferMode
== ATA_UDMA3
) {
1667 // Special case (undocumented overclock !) for PIIX4e
1668 SetPciConfig2(0x4a, (reg4a
| (0x03 << (dev
<<2)) ) );
1670 SetPciConfig2(0x4a, (reg4a
& ~(0x03 << (dev
<<2))) |
1671 (((USHORT
)(intel_utimings
[i
])) << (dev
<<2) ) );
1674 /* Set UDMA reference clock (66 MHz or more). */
1675 reg54
&= ~(0x1001 << dev
);
1677 reg54
|= (0x1 << dev
);
1679 /* Set UDMA reference clock (133 MHz). */
1681 reg54
|= (0x1000 << dev
);
1683 SetPciConfig2(0x54, reg54
);
1687 if(ChipFlags
& ICH4_FIX
) {
1688 KdPrint2((PRINT_PREFIX
" ICH4_FIX udma\n"));
1696 SetPciConfig1(0x48, reg48
& ~(0x0001 << dev
));
1697 if(!(ChipFlags
& ICH4_FIX
)) {
1698 SetPciConfig2(0x4a, (reg4a
& ~(0x3 << (dev
<< 2))) );
1700 SetPciConfig2(0x54, reg54
& ~(0x1001 << dev
));
1701 for(i
=wdmamode
; i
>=0; i
--) {
1702 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
1705 if(ChipFlags
& ICH4_FIX
) {
1706 KdPrint2((PRINT_PREFIX
" ICH4_FIX wdma\n"));
1715 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
1719 GetPciConfig4(0x40, reg40
);
1720 GetPciConfig1(0x44, reg44
);
1722 /* Allow PIO/WDMA timing controls. */
1723 reg40
&= ~0x00ff00ff;
1724 reg40
|= 0x40774077;
1726 mask40
= 0x000000ff;
1727 /* Set PIO/WDMA timings. */
1728 if(!(DeviceNumber
& 1)) {
1729 mask40
= 0x00003300;
1730 new40
= ((USHORT
)(intel_timings
[idx
]) << 8);
1733 new44
= ((intel_timings
[idx
] & 0x30) >> 2) |
1734 (intel_timings
[idx
] & 0x03);
1744 KdPrint2((PRINT_PREFIX
" 0x40 %x/%x, 0x44 %x/%x\n", mask40
, new40
, mask44
, new44
));
1745 SetPciConfig4(0x40, (reg40
& ~mask40
) | new40
);
1746 SetPciConfig1(0x44, (reg44
& ~mask44
) | new44
);
1750 case ATA_PROMISE_ID
: {
1755 UCHAR sel66
= Channel
? 0x08: 0x02;
1757 if(ChipType
< PRTX
) {
1763 for(i
=udmamode
; i
>=0; i
--) {
1765 if(ChipType
== PRNEW
) {
1767 AtapiWritePortEx1(chan
, (ULONGIO_PTR
)(&deviceExtension
->BaseIoAddressBM_0
),0x11,
1768 AtapiReadPortEx1(chan
, (ULONGIO_PTR
)(&deviceExtension
->BaseIoAddressBM_0
),0x11) |
1771 AtapiWritePortEx1(chan
, (ULONGIO_PTR
)(&deviceExtension
->BaseIoAddressBM_0
),0x11,
1772 AtapiReadPortEx1(chan
, (ULONGIO_PTR
)(&deviceExtension
->BaseIoAddressBM_0
),0x11) &
1777 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
1778 promise_timing(deviceExtension
, dev
, (UCHAR
)(ATA_UDMA
+ i
)); // ???
1782 if(ChipType
== PRNEW
) {
1783 AtapiWritePortEx1(chan
, (ULONGIO_PTR
)(&deviceExtension
->BaseIoAddressBM_0
),0x11,
1784 AtapiReadPortEx1(chan
, (ULONGIO_PTR
)(&deviceExtension
->BaseIoAddressBM_0
),0x11) &
1787 for(i
=wdmamode
; i
>=0; i
--) {
1788 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
1789 promise_timing(deviceExtension
, dev
, (UCHAR
)(ATA_WDMA0
+i
));
1793 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
1794 promise_timing(deviceExtension
, dev
, ATA_PIO0
+ apiomode
);
1799 KdPrint2((PRINT_PREFIX
"ATI\n"));
1800 if(ChipType
== SIIMIO
) {
1801 goto l_ATA_SILICON_IMAGE_ID
;
1803 //goto ATA_SERVERWORKS_ID;
1808 case ATA_SERVERWORKS_ID
: {
1812 // static const ULONG udma_modes[] = { 0x70, 0x21, 0x20 };
1813 static const ULONG sw_dma_modes
[] = { 0x70, 0x21, 0x20 };
1814 static const ULONG sw_pio_modes
[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
1815 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
1819 ULONG offset
= dev
^ 0x01;
1820 ULONG bit_offset
= offset
* 8;
1822 for(i
=udmamode
; i
>=0; i
--) {
1823 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
1824 GetPciConfig2(0x56, reg56
);
1825 reg56
&= ~(0xf << (dev
* 4));
1826 reg56
|= ((USHORT
)i
<< (dev
* 4));
1827 SetPciConfig2(0x56, reg56
);
1828 ChangePciConfig1(0x54, a
| (0x01 << dev
));
1830 GetPciConfig4(0x44, reg44
);
1831 reg44
= (reg44
& ~(0xff << bit_offset
)) |
1832 (sw_dma_modes
[2] << bit_offset
);
1833 SetPciConfig4(0x44, reg44
);
1835 GetPciConfig4(0x40, reg40
);
1836 reg40
= (reg40
& ~(0xff << bit_offset
)) |
1837 (sw_pio_modes
[8+i
] << bit_offset
);
1838 SetPciConfig4(0x40, reg40
);
1843 for(i
=wdmamode
; i
>=0; i
--) {
1844 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
1846 ChangePciConfig1(0x54, a
& ~(0x01 << dev
));
1848 GetPciConfig4(0x44, reg44
);
1849 reg44
= (reg44
& ~(0xff << bit_offset
)) |
1850 (sw_dma_modes
[wdmamode
] << bit_offset
);
1851 SetPciConfig4(0x44, reg44
);
1853 GetPciConfig4(0x40, reg40
);
1854 reg40
= (reg40
& ~(0xff << bit_offset
)) |
1855 (sw_pio_modes
[5+i
] << bit_offset
);
1856 SetPciConfig4(0x40, reg40
);
1860 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
1861 // SetPciConfig4(0x44, sw_pio_modes[apiomode]);
1862 if(VendorID
== ATA_ATI_ID
) {
1863 // special case for ATI
1864 // Seems, that PATA ATI are just re-brended ServerWorks
1867 GetPciConfig2(0x4a, reg4a
);
1868 reg4a
= (reg4a
& ~(0xf << (dev
*4))) |
1869 (apiomode
<< (dev
*4));
1870 SetPciConfig2(0x4a, reg4a
);
1874 GetPciConfig4(0x40, reg40
);
1875 reg40
= (reg40
& ~(0xff << bit_offset
)) |
1876 (sw_pio_modes
[apiomode
] << bit_offset
);
1877 SetPciConfig4(0x40, reg40
);
1880 case ATA_SILICON_IMAGE_ID
: {
1881 l_ATA_SILICON_IMAGE_ID
:
1882 /********************/
1883 /* SiliconImage/CMD */
1884 /********************/
1885 if(ChipType
== SIIMIO
) {
1887 static const UCHAR sil_modes
[7] =
1888 { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
1889 static const USHORT sil_wdma_modes
[3] =
1890 { 0x2208, 0x10c2, 0x10c1 };
1891 static const USHORT sil_pio_modes
[6] =
1892 { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1, 0x10c1 };
1894 UCHAR ureg
= 0xac + ((UCHAR
)DeviceNumber
* 0x02) + ((UCHAR
)Channel
* 0x10);
1896 UCHAR mreg
= Channel
? 0x84 : 0x80;
1897 UCHAR mask
= DeviceNumber
? 0x30 : 0x03;
1900 GetPciConfig1(ureg
, uval
);
1901 GetPciConfig1(mreg
, mode
);
1903 /* enable UDMA mode */
1904 for(i
= udmamode
; i
>=0; i
--) {
1906 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
1907 SetPciConfig1(mreg
, mode
| mask
);
1908 SetPciConfig1(ureg
, (uval
& 0x3f) | sil_modes
[i
]);
1912 /* enable WDMA mode */
1913 for(i
= wdmamode
; i
>=0; i
--) {
1915 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
1916 SetPciConfig1(mreg
, mode
| (mask
& 0x22));
1917 SetPciConfig2(ureg
- 0x4, sil_wdma_modes
[i
]);
1921 /* restore PIO mode */
1922 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
1924 SetPciConfig1(mreg
, mode
| (mask
& 0x11));
1925 SetPciConfig2(ureg
- 0x8, sil_pio_modes
[apiomode
]);
1930 static const UCHAR cmd_modes
[2][6] = {
1931 { 0x31, 0x21, 0x011, 0x25, 0x15, 0x05 },
1932 { 0xc2, 0x82, 0x042, 0x8a, 0x4a, 0x0a } };
1933 static const UCHAR cmd_wdma_modes
[] = { 0x87, 0x32, 0x3f };
1934 static const UCHAR cmd_pio_modes
[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
1935 ULONG treg
= 0x54 + ((dev
< 3) ? (dev
<< 1) : 7);
1937 udmamode
= min(udmamode
, 5);
1938 /* enable UDMA mode */
1939 for(i
= udmamode
; i
>=0; i
--) {
1942 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
1943 GetPciConfig1(Channel
? 0x7b : 0x73, umode
);
1944 umode
&= ~(!(DeviceNumber
& 1) ? 0x35 : 0xca);
1945 umode
|= ( cmd_modes
[DeviceNumber
& 1][i
]);
1946 SetPciConfig1(Channel
? 0x7b : 0x73, umode
);
1950 /* make sure eventual UDMA mode from the BIOS is disabled */
1951 ChangePciConfig1(Channel
? 0x7b : 0x73, a
& ~(!(DeviceNumber
& 1) ? 0x35 : 0xca));
1953 for(i
= wdmamode
; i
>=0; i
--) {
1955 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
1956 SetPciConfig1(treg
, cmd_wdma_modes
[i
]);
1957 ChangePciConfig1(Channel
? 0x7b : 0x73, a
& ~(!(DeviceNumber
& 1) ? 0x35 : 0xca));
1961 /* set PIO mode timings */
1962 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
1964 SetPciConfig1(treg
, cmd_pio_modes
[apiomode
]);
1965 ChangePciConfig1(Channel
? 0x7b : 0x73, a
& ~(!(DeviceNumber
& 1) ? 0x35 : 0xca));
1975 PULONG sis_modes
= NULL
;
1976 static const ULONG sis_modes_new133
[] =
1977 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
1978 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
1979 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
1980 static const ULONG sis_modes_old133
[] =
1981 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
1982 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
1983 static const ULONG sis_modes_old
[] =
1984 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303, 0x0301,
1985 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
1986 static const ULONG sis_modes_new100
[] =
1987 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
1988 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
1997 sis_modes
= (PULONG
)(&sis_modes_new133
[0]);
1999 GetPciConfig1(0x57, reg57
);
2000 reg
= (reg57
& 0x40 ? 0x70 : 0x40) + (dev
* 4);
2003 sis_modes
= (PULONG
)(&sis_modes_old133
[0]);
2005 reg
= 0x40 + (dev
* 2);
2008 sis_modes
= (PULONG
)(&sis_modes_new100
[0]);
2010 reg
= 0x40 + (dev
* 2);
2015 sis_modes
= (PULONG
)(&sis_modes_old
[0]);
2017 reg
= 0x40 + (dev
* 2);
2022 for(i
=udmamode
; i
>=0; i
--) {
2023 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
2025 SetPciConfig4(reg
, sis_modes
[offs
+i
]);
2027 SetPciConfig2(reg
, (USHORT
)sis_modes
[offs
+i
]);
2034 for(i
=wdmamode
; i
>=0; i
--) {
2035 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
2037 SetPciConfig4(reg
, sis_modes
[offs
+i
]);
2039 SetPciConfig2(reg
, (USHORT
)sis_modes
[offs
+i
]);
2044 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
2046 SetPciConfig4(reg
, sis_modes
[apiomode
]);
2048 SetPciConfig2(reg
, (USHORT
)sis_modes
[apiomode
]);
2053 /* Cenatek Rocket Drive controller */
2054 if (wdmamode
>= 0 &&
2055 (AtapiReadPort1(chan
, IDX_BM_Status
) &
2056 (DeviceNumber
? BM_STATUS_DRIVE_1_DMA
: BM_STATUS_DRIVE_0_DMA
))) {
2057 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ wdmamode
);
2059 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
2062 case ATA_ITE_ID
: { /* ITE IDE controller */
2064 if(ChipType
== ITE_33
) {
2065 int a_speed
= 3 << (dev
* 4);
2066 int u_flag
= 1 << dev
;
2070 USHORT drive_enables
;
2073 GetPciConfig1(0x48, reg48
);
2074 GetPciConfig1(0x4a, reg4a
);
2077 * Setting the DMA cycle time to 2 or 3 PCI clocks (60 and 91 nsec
2078 * at 33 MHz PCI clock) seems to cause BadCRC errors during DMA
2079 * transfers on some drives, even though both numbers meet the minimum
2080 * ATAPI-4 spec of 73 and 54 nsec for UDMA 1 and 2 respectively.
2081 * So the faster times are just commented out here. The good news is
2082 * that the slower cycle time has very little affect on transfer
2086 for(i
=udmamode
; i
>=0; i
--) {
2087 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
2088 SetPciConfig1(0x48, reg48
| u_flag
);
2090 SetPciConfig1(0x4a, reg4a
| u_speed
);
2092 goto setup_drive_ite
;
2096 for(i
=wdmamode
; i
>=0; i
--) {
2097 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
2098 SetPciConfig1(0x48, reg48
& ~u_flag
);
2099 SetPciConfig1(0x4a, reg4a
& ~a_speed
);
2104 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
2105 SetPciConfig1(0x48, reg48
& ~u_flag
);
2106 SetPciConfig1(0x4a, reg4a
& ~a_speed
);
2112 GetPciConfig2(0x40, drive_enables
);
2113 GetPciConfig4(0x44, drive_timing
);
2116 * FIX! The DIOR/DIOW pulse width and recovery times in port 0x44
2117 * are being left at the default values of 8 PCI clocks (242 nsec
2118 * for a 33 MHz clock). These can be safely shortened at higher
2119 * PIO modes. The DIOR/DIOW pulse width and recovery times only
2120 * apply to PIO modes, not to the DMA modes.
2124 * Enable port 0x44. The IT8172G spec is confused; it calls
2125 * this register the "Slave IDE Timing Register", but in fact,
2126 * it controls timing for both master and slave drives.
2128 drive_enables
|= 0x4000;
2130 drive_enables
&= (0xc000 | (0x06 << (DeviceNumber
*4)));
2132 /* enable prefetch and IORDY sample-point */
2133 drive_enables
|= (0x06 << (DeviceNumber
*4));
2136 SetPciConfig2(0x40, drive_enables
);
2138 if(ChipType
== ITE_133
) {
2139 static const UCHAR udmatiming
[] =
2140 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
2141 static const UCHAR chtiming
[] =
2142 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
2143 ULONG offset
= (Channel
<<2) + DeviceNumber
;
2146 for(i
=udmamode
; i
>=0; i
--) {
2147 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
2148 ChangePciConfig1(0x50, a
& ~(1 << (dev
+ 3)) );
2149 SetPciConfig1(0x56 + offset
, udmatiming
[i
]);
2154 for(i
=wdmamode
; i
>=0; i
--) {
2155 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
2157 ChangePciConfig1(0x50, a
| (1 << (dev
+ 3)) );
2158 GetPciConfig1(0x54 + offset
, reg54
);
2159 if(reg54
< chtiming
[i
+5]) {
2160 SetPciConfig1(0x54 + offset
, chtiming
[i
+5]);
2165 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
2166 ChangePciConfig1(0x50, a
| (1 << (dev
+ 3)) );
2167 GetPciConfig1(0x54 + offset
, reg54
);
2168 if(reg54
< chtiming
[apiomode
]) {
2169 SetPciConfig1(0x54 + offset
, chtiming
[apiomode
]);
2173 if(ChipType
== ITE_133_NEW
) {
2174 //static const USHORT reg54_timings[] = { 0x0000, 0x0000, 0x0001, 0x0001, 0x0001, 0x1001, 0x1001 };
2175 static const UCHAR udmatiming
[] =
2176 { 0x00, 0x01, 0x10, 0x01, 0x10, 0x01, 0x10 };
2177 static const UCHAR timings
[] =
2178 { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
2179 0x23, 0x23, 0x23, 0x23, 0x23, 0x02, 0x02 };
2180 BOOLEAN udma_ok
= FALSE
;
2188 USHORT mask40
=0, new40
=0;
2189 UCHAR mask44
=0, new44
=0;
2191 GetPciConfig2(0x40, reg40
);
2192 GetPciConfig1(0x44, reg44
);
2193 GetPciConfig2(0x4a, reg4a
);
2194 GetPciConfig2(0x54, reg54
);
2196 if(!(reg54
& (0x10 << dev
))) {
2198 udmamode
= min(udmamode
, 2);
2201 for(i
=udmamode
; i
>=0; i
--) {
2202 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
2203 ChangePciConfig1(0x48, a
| (1 << dev
) );
2204 ChangePciConfig2(0x4a,
2205 (a
& ~(0x3 << (dev
*4))) |
2206 (udmatiming
[i
] << (dev
*4)) );
2209 timing
= timings
[i
+8];
2214 for(i
=wdmamode
; !ok
&& i
>=0; i
--) {
2215 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
2218 timing
= timings
[i
+5];
2224 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
2225 timing
= timings
[apiomode
];
2229 ChangePciConfig1(0x48, a
& ~(1 << dev
) );
2230 ChangePciConfig2(0x4a, a
& ~(0x3 << (dev
<< 2)) );
2232 if (udma_ok
&& udmamode
>= ATA_UDMA2
) {
2233 reg54
|= (0x1 << dev
);
2235 reg54
&= ~(0x1 << dev
);
2237 if (udma_ok
&& udmamode
>= ATA_UDMA5
) {
2238 reg54
|= (0x1000 << dev
);
2240 reg54
&= ~(0x1000 << dev
);
2242 SetPciConfig2(0x54, reg54
);
2247 if(!(DeviceNumber
& 1)) {
2248 reg40
|= (isAtapi
? 0x04 : 0x00);
2250 new40
= timing
<< 8;
2252 reg40
|= (isAtapi
? 0x40 : 0x00);
2254 new44
= ((timing
& 0x30) >> 2) |
2257 SetPciConfig2(0x40, (reg40
& ~mask40
) | new40
);
2258 SetPciConfig1(0x44, (reg44
& ~mask44
) | new44
);
2265 /* HiNT Corp. VXPro II EIDE */
2266 if (wdmamode
>= 0 &&
2267 (AtapiReadPort1(chan
, IDX_BM_Status
) &
2268 (DeviceNumber
? BM_STATUS_DRIVE_1_DMA
: BM_STATUS_DRIVE_0_DMA
))) {
2269 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_DMA
);
2271 AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
);
2274 case ATA_JMICRON_ID
: {
2277 GetPciConfig1(0x40, reg40
);
2281 udmamode
= min(udmamode
, 2);
2283 /* Nothing to do to setup mode, the controller snoop SET_FEATURE cmd. */
2286 for(i
=udmamode
; i
>=0; i
--) {
2287 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_UDMA0
+ i
)) {
2291 for(i
=wdmamode
; i
>=0; i
--) {
2292 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_WDMA0
+ i
)) {
2296 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
)) {
2305 /* unknown controller chip */
2307 /* better not try generic DMA on ATAPI devices it almost never works */
2309 KdPrint2((PRINT_PREFIX
"ATAPI on unknown controller -> PIO\n"));
2314 /* if controller says its setup for DMA take the easy way out */
2315 /* the downside is we dont know what DMA mode we are in */
2316 if ((udmamode
>= 0 || /*wdmamode > 1*/ wdmamode
>= 0) &&
2317 /*deviceExtension->BaseIoAddressBM[lChannel]*/ (deviceExtension
->BusMaster
==DMA_MODE_BM
) &&
2318 (GetDmaStatus(deviceExtension
, lChannel
) &
2319 (!(DeviceNumber
& 1) ?
2320 BM_STATUS_DRIVE_0_DMA
: BM_STATUS_DRIVE_1_DMA
))) {
2321 // LunExt->TransferMode = ATA_DMA;
2323 KdPrint2((PRINT_PREFIX
"try DMA on unknown controller\n"));
2324 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_DMA
)) {
2330 /* well, we have no support for this, but try anyways */
2331 if ((wdmamode
>= 0 && apiomode
>= 4) && deviceExtension
->BaseIoAddressBM
[lChannel
]) {
2332 if(AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_DMA
/* + wdmamode*/)) {
2338 KdPrint2((PRINT_PREFIX
"try PIO%d on unknown controller\n", apiomode
));
2339 if(!AtaSetTransferMode(deviceExtension
, DeviceNumber
, lChannel
, LunExt
, ATA_PIO0
+ apiomode
)) {
2340 KdPrint2((PRINT_PREFIX
"fall to PIO on unknown controller\n"));
2341 LunExt
->TransferMode
= ATA_PIO
;
2344 } // end AtapiDmaInit()
2350 IN PHW_DEVICE_EXTENSION deviceExtension
,
2351 IN ULONG dev
, // physical device number (0-3)
2355 // ASSERT(dev/2 >= deviceExtension->Channel);
2356 // PHW_CHANNEL chan = &(deviceExtension->chan[dev/2-deviceExtension->Channel]);
2357 ULONG reg20
= 0x0000e132;
2358 ULONG reg24
= 0x00017771;
2360 if(mode
== ATA_PIO5
)
2364 case ATA_PIO0
: reg20
= 0x0000e132; break;
2365 case ATA_PIO1
: reg20
= 0x00018121; break;
2366 case ATA_PIO2
: reg20
= 0x00024020; break;
2367 case ATA_PIO3
: reg20
= 0x00032010; break;
2369 case ATA_PIO5
: reg20
= 0x00040010; break;
2370 case ATA_WDMA2
: reg24
= 0x00002020; break;
2371 case ATA_UDMA2
: reg24
= 0x00911030; break;
2373 AtapiWritePortEx4(NULL
, (ULONGIO_PTR
)(&deviceExtension
->BaseIoAddressBM_0
),(dev
*8) + 0x20, reg20
);
2374 AtapiWritePortEx4(NULL
, (ULONGIO_PTR
)(&deviceExtension
->BaseIoAddressBM_0
),(dev
*8) + 0x24, reg24
);
2380 IN PHW_DEVICE_EXTENSION deviceExtension
,
2381 IN ULONG dev
, // physical device number (0-3)
2385 PVOID HwDeviceExtension
= (PVOID
)deviceExtension
;
2386 ULONG slotNumber
= deviceExtension
->slotNumber
;
2387 ULONG SystemIoBusNumber
= deviceExtension
->SystemIoBusNumber
;
2389 //ULONG ChipFlags = deviceExtension->HwFlags & CHIPFLAG_MASK;
2391 ULONG port
= 0x60 + (dev
<<2);
2393 if(mode
== ATA_PIO5
)
2400 // **** FreeBSD code ****
2402 ULONG ChipType
= deviceExtension
->HwFlags
& CHIPTYPE_MASK
;
2409 case ATA_PIO0
: timing
= 0x004ff329; break;
2410 case ATA_PIO1
: timing
= 0x004fec25; break;
2411 case ATA_PIO2
: timing
= 0x004fe823; break;
2412 case ATA_PIO3
: timing
= 0x004fe622; break;
2413 case ATA_PIO4
: timing
= 0x004fe421; break;
2414 case ATA_WDMA0
: timing
= 0x004567f3; break;
2415 case ATA_WDMA1
: timing
= 0x004467f3; break;
2416 case ATA_WDMA2
: timing
= 0x004367f3; break;
2417 case ATA_UDMA0
: timing
= 0x004367f3; break;
2418 case ATA_UDMA1
: timing
= 0x004247f3; break;
2419 case ATA_UDMA2
: timing
= 0x004127f3; break;
2426 case ATA_PIO0
: timing
= 0x004fff2f; break;
2427 case ATA_PIO1
: timing
= 0x004ff82a; break;
2428 case ATA_PIO2
: timing
= 0x004ff026; break;
2429 case ATA_PIO3
: timing
= 0x004fec24; break;
2430 case ATA_PIO4
: timing
= 0x004fe822; break;
2431 case ATA_WDMA0
: timing
= 0x004acef6; break;
2432 case ATA_WDMA1
: timing
= 0x0048cef6; break;
2433 case ATA_WDMA2
: timing
= 0x0046cef6; break;
2434 case ATA_UDMA0
: timing
= 0x0046cef6; break;
2435 case ATA_UDMA1
: timing
= 0x00448ef6; break;
2436 case ATA_UDMA2
: timing
= 0x00436ef6; break;
2437 case ATA_UDMA3
: timing
= 0x00424ef6; break;
2438 case ATA_UDMA4
: timing
= 0x004127f3; break;
2439 case ATA_UDMA5
: timing
= 0x004127f3; break;
2448 SetPciConfig4(port
, timing
);
2451 // **** Linux code ****
2453 UCHAR r_bp
, r_cp
, r_ap
;
2456 static UCHAR udma_timing
[6][2] = {
2457 { 0x60, 0x03 }, /* 33 Mhz Clock */
2460 { 0x40, 0x02 }, /* 66 Mhz Clock */
2464 static UCHAR mdma_timing
[3][2] = {
2469 static USHORT pio_timing
[5] = {
2470 0x0913, 0x050C , 0x0308, 0x0206, 0x0104
2473 if(mode
> ATA_UDMA5
) {
2477 if(mode
> ATA_WDMA0
) {
2479 GetPciConfig1(port
+1, r_bp
);
2480 GetPciConfig1(port
+2, r_cp
);
2485 if(mode
>= ATA_UDMA0
) {
2486 i
= mode
- ATA_UDMA0
;
2487 r_bp
|= udma_timing
[i
][0];
2488 r_cp
|= udma_timing
[i
][1];
2491 i
= mode
- ATA_WDMA0
;
2492 r_bp
|= mdma_timing
[i
][0];
2493 r_cp
|= mdma_timing
[i
][1];
2495 SetPciConfig1(port
+1, r_bp
);
2496 SetPciConfig1(port
+2, r_cp
);
2498 if(mode
<= ATA_PIO5
) {
2499 GetPciConfig1(port
+0, r_ap
);
2500 GetPciConfig1(port
+1, r_bp
);
2502 i
= mode
- ATA_PIO0
;
2503 r_ap
&= ~0x3F; /* Preserve ERRDY_EN, SYNC_IN */
2505 r_ap
|= (UCHAR
)(pio_timing
[i
] >> 8);
2506 r_bp
|= (UCHAR
)(pio_timing
[i
] & 0xFF);
2508 // if (ata_pio_need_iordy(adev))
2509 r_ap
|= 0x20; /* IORDY enable */
2510 // if (adev->class == ATA_DEV_ATA)
2511 // r_ap |= 0x10; /* FIFO enable */
2513 SetPciConfig1(port
+0, r_ap
);
2514 SetPciConfig1(port
+1, r_bp
);
2519 } // end promise_timing()
2525 IN PHW_DEVICE_EXTENSION deviceExtension
,
2526 IN ULONG dev
, // physical device number (0-3)
2530 PVOID HwDeviceExtension
= (PVOID
)deviceExtension
;
2531 ULONG slotNumber
= deviceExtension
->slotNumber
;
2532 ULONG SystemIoBusNumber
= deviceExtension
->SystemIoBusNumber
;
2534 ULONG ChipType
= deviceExtension
->HwFlags
& CHIPTYPE_MASK
;
2535 //ULONG ChipFlags = deviceExtension->HwFlags & CHIPFLAG_MASK;
2539 if(mode
== ATA_PIO5
)
2545 switch (mode
) { /* HPT374 */
2546 case ATA_PIO0
: timing
= 0x0ac1f48a; break;
2547 case ATA_PIO1
: timing
= 0x0ac1f465; break;
2548 case ATA_PIO2
: timing
= 0x0a81f454; break;
2549 case ATA_PIO3
: timing
= 0x0a81f443; break;
2550 case ATA_PIO4
: timing
= 0x0a81f442; break;
2551 case ATA_WDMA0
: timing
= 0x228082ea; break;
2552 case ATA_WDMA1
: timing
= 0x22808254; break;
2553 case ATA_WDMA2
: timing
= 0x22808242; break;
2554 case ATA_UDMA0
: timing
= 0x121882ea; break;
2555 case ATA_UDMA1
: timing
= 0x12148254; break;
2556 case ATA_UDMA2
: timing
= 0x120c8242; break;
2557 case ATA_UDMA3
: timing
= 0x128c8242; break;
2558 case ATA_UDMA4
: timing
= 0x12ac8242; break;
2559 case ATA_UDMA5
: timing
= 0x12848242; break;
2560 case ATA_UDMA6
: timing
= 0x12808242; break;
2561 default: timing
= 0x0d029d5e;
2567 switch (mode
) { /* HPT372 */
2568 case ATA_PIO0
: timing
= 0x0d029d5e; break;
2569 case ATA_PIO1
: timing
= 0x0d029d26; break;
2570 case ATA_PIO2
: timing
= 0x0c829ca6; break;
2571 case ATA_PIO3
: timing
= 0x0c829c84; break;
2572 case ATA_PIO4
: timing
= 0x0c829c62; break;
2573 case ATA_WDMA0
: timing
= 0x2c82922e; break;
2574 case ATA_WDMA1
: timing
= 0x2c829266; break;
2575 case ATA_WDMA2
: timing
= 0x2c829262; break;
2576 case ATA_UDMA0
: timing
= 0x1c829c62; break;
2577 case ATA_UDMA1
: timing
= 0x1c9a9c62; break;
2578 case ATA_UDMA2
: timing
= 0x1c929c62; break;
2579 case ATA_UDMA3
: timing
= 0x1c8e9c62; break;
2580 case ATA_UDMA4
: timing
= 0x1c8a9c62; break;
2581 case ATA_UDMA5
: timing
= 0x1c8a9c62; break;
2582 case ATA_UDMA6
: timing
= 0x1c869c62; break;
2583 default: timing
= 0x0d029d5e;
2589 switch (mode
) { /* HPT370 */
2590 case ATA_PIO0
: timing
= 0x06914e57; break;
2591 case ATA_PIO1
: timing
= 0x06914e43; break;
2592 case ATA_PIO2
: timing
= 0x06514e33; break;
2593 case ATA_PIO3
: timing
= 0x06514e22; break;
2594 case ATA_PIO4
: timing
= 0x06514e21; break;
2595 case ATA_WDMA0
: timing
= 0x26514e97; break;
2596 case ATA_WDMA1
: timing
= 0x26514e33; break;
2597 case ATA_WDMA2
: timing
= 0x26514e21; break;
2598 case ATA_UDMA0
: timing
= 0x16514e31; break;
2599 case ATA_UDMA1
: timing
= 0x164d4e31; break;
2600 case ATA_UDMA2
: timing
= 0x16494e31; break;
2601 case ATA_UDMA3
: timing
= 0x166d4e31; break;
2602 case ATA_UDMA4
: timing
= 0x16454e31; break;
2603 case ATA_UDMA5
: timing
= 0x16454e31; break;
2604 default: timing
= 0x06514e57;
2610 GetPciConfig1(0x41 + (dev
<< 2), reg41
);
2613 case 0x85: /* 25Mhz */
2615 case ATA_PIO0
: timing
= 0x40d08585; break;
2616 case ATA_PIO1
: timing
= 0x40d08572; break;
2617 case ATA_PIO2
: timing
= 0x40ca8542; break;
2618 case ATA_PIO3
: timing
= 0x40ca8532; break;
2619 case ATA_PIO4
: timing
= 0x40ca8521; break;
2620 case ATA_WDMA2
: timing
= 0x20ca8521; break;
2621 case ATA_UDMA2
: timing
= 0x10cf8521; break;
2622 case ATA_UDMA4
: timing
= 0x10c98521; break;
2623 default: timing
= 0x01208585;
2627 case 0xa7: /* 33MHz */
2629 case ATA_PIO0
: timing
= 0x40d0a7aa; break;
2630 case ATA_PIO1
: timing
= 0x40d0a7a3; break;
2631 case ATA_PIO2
: timing
= 0x40d0a753; break;
2632 case ATA_PIO3
: timing
= 0x40c8a742; break;
2633 case ATA_PIO4
: timing
= 0x40c8a731; break;
2634 case ATA_WDMA0
: timing
= 0x20c8a797; break;
2635 case ATA_WDMA1
: timing
= 0x20c8a732; break;
2636 case ATA_WDMA2
: timing
= 0x20c8a731; break;
2637 case ATA_UDMA0
: timing
= 0x10c8a731; break;
2638 case ATA_UDMA1
: timing
= 0x10cba731; break;
2639 case ATA_UDMA2
: timing
= 0x10caa731; break;
2640 case ATA_UDMA3
: timing
= 0x10cfa731; break;
2641 case ATA_UDMA4
: timing
= 0x10c9a731; break;
2642 default: timing
= 0x0120a7a7;
2645 case 0xd9: /* 40Mhz */
2647 case ATA_PIO0
: timing
= 0x4018d9d9; break;
2648 case ATA_PIO1
: timing
= 0x4010d9c7; break;
2649 case ATA_PIO2
: timing
= 0x4010d997; break;
2650 case ATA_PIO3
: timing
= 0x4010d974; break;
2651 case ATA_PIO4
: timing
= 0x4008d963; break;
2652 case ATA_WDMA2
: timing
= 0x2008d943; break;
2653 case ATA_UDMA2
: timing
= 0x100bd943; break;
2654 case ATA_UDMA4
: timing
= 0x100fd943; break;
2655 default: timing
= 0x0120d9d9;
2661 SetPciConfig4(0x40 + (dev
<<2), timing
);
2662 } // end hpt_timing()
2665 #define FIT(v,min,max) (((v)>(max)?(max):(v))<(min)?(min):(v))
2670 IN PHW_DEVICE_EXTENSION deviceExtension
,
2671 IN ULONG dev
, // physical device number (0-3)
2675 PVOID HwDeviceExtension
= (PVOID
)deviceExtension
;
2676 ULONG slotNumber
= deviceExtension
->slotNumber
;
2677 ULONG SystemIoBusNumber
= deviceExtension
->SystemIoBusNumber
;
2679 // Newer chips dislike this:
2680 if(/*!(deviceExtension->HwFlags & VIAAST)*/
2681 deviceExtension
->MaxTransferMode
< ATA_UDMA6
) {
2685 USHORT T
= 1000 / /* PciBusClockMHz()*/ 33;
2696 case ATA_PIO0
: setup
= 70; active
= 165; recover
= 150; cycle
= 600; break;
2697 case ATA_PIO1
: setup
= 50; active
= 125; recover
= 100; cycle
= 383; break;
2698 case ATA_PIO2
: setup
= 30; active
= 100; recover
= 90; cycle
= 240; break;
2699 case ATA_PIO3
: setup
= 30; active
= 80; recover
= 70; cycle
= 180; break;
2700 case ATA_PIO4
: setup
= 25; active
= 70; recover
= 25; cycle
= 120; break;
2701 case ATA_PIO5
: setup
= 20; active
= 50; recover
= 30; cycle
= 100; break;
2704 setup
= (setup
-1)/(T
+1);
2705 active
= (active
-1)/(T
+1);
2706 recover
= (recover
-1)/(T
+1);
2707 cycle
= (cycle
-1)/(T
+1);
2709 if (active
+ recover
< cycle
) {
2710 active
+= (cycle
- (active
+ recover
)) / 2;
2711 recover
= cycle
- active
;
2714 /* PIO address setup */
2715 GetPciConfig1(0x4c, t
);
2716 t
= (t
& ~(3 << ((3 - dev
) << 1))) | (FIT(setup
- 1, 0, 3) << ((3 - dev
) << 1));
2717 SetPciConfig1(0x4c, t
);
2719 /* PIO active & recover */
2720 SetPciConfig1(0x4b-dev
, (FIT(active
- 1, 0, 0xf) << 4) | FIT(recover
- 1, 0, 0xf) );
2721 } // end via82c_timing()