2 Copyright � 2004-2009, The AROS Development Team. All rights reserved
12 * ---------- ------------------ -------------------------------------------------------------------
13 * 2006-12-20 T. Wiszkowski Updated ATA Packet Interface to handle ATAPI/SCSI Commands
14 * 2008-01-06 T. Wiszkowski Corrected and completed ATA Packet Interface handling. PIO transfers fully operational.
15 * 2008-01-07 T. Wiszkowski Added initial DMA support for Direct SCSI commands. Corrected atapi
16 * READ and WRITE commands to pass proper transfer size to the atapi_SendPacket
17 * as discovered by mschulz
18 * 2008-01-25 T. Wiszkowski Rebuilt, rearranged and partially fixed 60% of the code here
19 * Enabled implementation to scan for other PCI IDE controllers
20 * Implemented ATAPI Packet Support for both read and write
21 * Corrected ATAPI DMA handling
22 * Fixed major IDE enumeration bugs severely handicapping transfers with more than one controller
23 * Compacted source and implemented major ATA support procedure
24 * Improved DMA and Interrupt management
25 * Removed obsolete code
26 * 2008-01-26 T. Wiszkowski Restored 32bit io
27 * Removed memory dump upon RequestSense
28 * 2008-02-08 T. Wiszkowski Fixed DMA accesses for direct scsi devices,
29 * Corrected IO Areas to allow ATA to talk to PCI controllers
30 * 2008-03-03 T. Wiszkowski Added drive reselection + setup delay on Init
31 * 2008-03-29 T. Wiszkowski Restored error on 64bit R/W access to non-64bit capable atapi devices
33 * 2008-03-30 T. Wiszkowski Added workaround for interrupt collision handling; fixed SATA in LEGACY mode.
34 * nForce and Intel SATA chipsets should now be operational (nForce confirmed)
35 * 2008-03-31 M. Schulz The ins/outs function definitions used only in case of x86 and x86_64 architectures.
36 * Otherwise, function declaratons are emitted.
37 * 2008-04-01 M. Schulz Use C functions ata_ins[wl] ata_outs[wl]
38 * 2008-04-03 T. Wiszkowski Fixed IRQ flood issue, eliminated and reduced obsolete / redundant code
39 * 2008-04-05 T. Wiszkowski Improved IRQ management
40 * 2008-04-07 T. Wiszkowski Changed bus timeout mechanism
41 * increased failure timeout values to cover rainy day scenarios
42 * 2008-04-20 T. Wiszkowski Corrected the flaw in drive identification routines leading to ocassional system hangups
43 * 2008-05-11 T. Wiszkowski Remade the ata trannsfers altogether, corrected the pio/irq handling
44 * medium removal, device detection, bus management and much more
45 * 2008-05-12 P. Fedin Explicitly enable multisector transfers on the drive
46 * 2008-05-18 T. Wiszkowski Added extra checks to prevent duplicating drive0 in drive0 only configs
47 * 2008-05-18 T. Wiszkowski Replaced static C/H/S with more accurate calcs, should make HDTB and other tools see right capacity
48 * 2008-05-19 T. Wiszkowski Updated ATA DMA handling and transfer wait operation to allow complete transfer before dma_StopDMA()
49 * 2008-05-30 T. Wiszkowski Corrected CHS calculation for larger disks
50 * 2008-06-03 K. Smiechowicz Added 400ns delay in ata_WaitBusyTO before read of device status.
51 * 2008-06-25 P. Fedin Added "nomulti" flag
52 * PIO works correctly again
53 * 2008-11-28 T. Wiszkowski updated test unit ready to suit individual taste of hw manufacturers
54 * 2009-01-20 J. Koivisto Modified bus reseting scheme
55 * 2009-02-04 T. Wiszkowski Disabled ATA debug on official builds
56 * 2009-03-05 T. Wiszkowski remade timeouts, added timer-based and benchmark-based delays.
60 * - put a critical section around DMA transfers (shared dma channels)
64 // use #define xxx(a) D(a) to enable particular sections.
66 #define DIRQ_MORE(a) D(a)
69 #define DATAPI(a) D(a)
72 #include <aros/debug.h>
73 #include <exec/types.h>
74 #include <exec/exec.h>
75 #include <exec/resident.h>
76 #include <utility/utility.h>
79 #include <proto/exec.h>
80 #include <devices/timer.h>
90 Prototypes of static functions from lowlevel.c. I do not want to make them
91 non-static as I'd like to remove as much symbols from global table as possible.
92 Besides some of this functions could conflict with old ide.device or any other
95 static ULONG
ata_ReadSector32(struct ata_Unit
*, ULONG
, ULONG
, APTR
, ULONG
*);
96 static ULONG
ata_ReadSector64(struct ata_Unit
*, UQUAD
, ULONG
, APTR
, ULONG
*);
97 static ULONG
ata_ReadMultiple32(struct ata_Unit
*, ULONG
, ULONG
, APTR
, ULONG
*);
98 static ULONG
ata_ReadMultiple64(struct ata_Unit
*, UQUAD
, ULONG
, APTR
, ULONG
*);
99 static ULONG
ata_ReadDMA32(struct ata_Unit
*, ULONG
, ULONG
, APTR
, ULONG
*);
100 static ULONG
ata_ReadDMA64(struct ata_Unit
*, UQUAD
, ULONG
, APTR
, ULONG
*);
101 static ULONG
ata_WriteSector32(struct ata_Unit
*, ULONG
, ULONG
, APTR
, ULONG
*);
102 static ULONG
ata_WriteSector64(struct ata_Unit
*, UQUAD
, ULONG
, APTR
, ULONG
*);
103 static ULONG
ata_WriteMultiple32(struct ata_Unit
*, ULONG
, ULONG
, APTR
, ULONG
*);
104 static ULONG
ata_WriteMultiple64(struct ata_Unit
*, UQUAD
, ULONG
, APTR
, ULONG
*);
105 static ULONG
ata_WriteDMA32(struct ata_Unit
*, ULONG
, ULONG
, APTR
, ULONG
*);
106 static ULONG
ata_WriteDMA64(struct ata_Unit
*, UQUAD
, ULONG
, APTR
, ULONG
*);
107 static ULONG
ata_Eject(struct ata_Unit
*);
108 static BOOL
ata_WaitBusyTO(struct ata_Unit
*unit
, UWORD tout
, BOOL irq
, UBYTE
*stout
);
110 static ULONG
atapi_EndCmd(struct ata_Unit
*unit
);
112 static ULONG
atapi_Read(struct ata_Unit
*, ULONG
, ULONG
, APTR
, ULONG
*);
113 static ULONG
atapi_Write(struct ata_Unit
*, ULONG
, ULONG
, APTR
, ULONG
*);
114 static ULONG
atapi_Eject(struct ata_Unit
*);
116 static void common_SetBestXferMode(struct ata_Unit
* unit
);
119 Again piece of code which shouldn't be here. Geee. After removing all this
120 asm constrictuins this ata.device will really deserve for location in
124 * having an x86 assembly here i dare to assume that this is meant to be
125 * an x86[_64] device only.
129 * This functions will stay here for some time *IF* and only if the code is compiled for
130 * x86 or x86_64 architecture. Otherwise, function declarations will be emitted and the
131 * one who ports the driver will be reponsible for adding the missing code.
135 * the outsl and insl commands improperly assumed that every transfer is sized to multiple of four
137 #if defined(__i386__) || defined(__x86_64__)
139 static VOID
ata_insw(APTR address
, UWORD port
, ULONG count
)
141 insw(port
, address
, count
>> 1);
144 static VOID
ata_insl(APTR address
, UWORD port
, ULONG count
)
147 insw(port
, address
, count
>> 1);
149 insl(port
, address
, count
>> 2);
152 static VOID
ata_outsw(APTR address
, UWORD port
, ULONG count
)
154 outsw(port
, address
, count
>> 1);
157 static VOID
ata_outsl(APTR address
, UWORD port
, ULONG count
)
160 outsw(port
, address
, count
>> 1);
162 outsl(port
, address
, count
>> 2);
166 extern VOID
ata_insw(APTR address
, UWORD port
, ULONG count
);
167 extern VOID
ata_insl(APTR address
, UWORD port
, ULONG count
);
168 extern VOID
ata_outsw(APTR address
, UWORD port
, ULONG count
);
169 extern VOID
ata_outsl(APTR address
, UWORD port
, ULONG count
);
172 static void dump(APTR mem
, ULONG len
)
175 for (j
=0; j
<(len
+15)>>4; ++j
)
177 bug("[ATA ] %06lx: ", j
<<4);
179 for (i
=0; i
<len
-(j
<<4); i
++)
181 bug("%02lx ", ((unsigned char*)mem
)[(j
<<4)|i
]);
186 for (i
=0; i
<len
-(j
<<4); i
++)
188 unsigned char c
= ((unsigned char*)mem
)[(j
<<4)|i
];
190 bug("%c", c
>= 0x20 ? c
<=0x7f ? c
: '.' : '.');
198 static void ata_strcpy(const UBYTE
*str1
, UBYTE
*str2
, ULONG size
)
200 register int i
= size
;
204 str2
[size
^1] = str1
[size
];
216 * a STUB function for commands not supported by this particular device
218 static ULONG
ata_STUB(struct ata_Unit
*au
)
220 bug("[ATA%02ld] CALLED STUB FUNCTION (GENERIC). THIS OPERATION IS NOT "
221 "SUPPORTED BY DEVICE\n", au
->au_UnitNum
);
225 static ULONG
ata_STUB_IO32(struct ata_Unit
*au
, ULONG blk
, ULONG len
, APTR buf
, ULONG
* act
)
227 bug("[ATA%02ld] CALLED STUB FUNCTION (IO32). THIS OPERATION IS NOT "
228 "SUPPORTED BY DEVICE\n", au
->au_UnitNum
);
232 static ULONG
ata_STUB_IO64(struct ata_Unit
*au
, UQUAD blk
, ULONG len
, APTR buf
, ULONG
* act
)
234 bug("[ATA%02ld] CALLED STUB FUNCTION -- IO ACCESS TO BLOCK %08lx:%08lx, LENGTH %08lx. THIS OPERATION IS NOT SUPPORTED BY DEVICE\n", au
->au_UnitNum
, (blk
>> 32), (blk
& 0xffffffff), len
);
238 static ULONG
ata_STUB_SCSI(struct ata_Unit
*au
, struct SCSICmd
* cmd
)
240 bug("[ATA%02ld] CALLED STUB FUNCTION. THIS OPERATION IS NOT SUPPORTED BY DEVICE\n", au
->au_UnitNum
);
244 inline BOOL
ata_SelectUnit(struct ata_Unit
* unit
)
246 ata_out(unit
->au_DevMask
, ata_DevHead
, unit
->au_Bus
->ab_Port
);
251 //ata_WaitTO(unit->au_Bus->ab_Timer, 0, 1, 0);
253 while (0 != (ATAF_BUSY
& ata_ReadStatus(unit
->au_Bus
)));
258 inline struct ata_Unit
* ata_GetSelectedUnit(struct ata_Bus
* bus
)
260 register int id
= (ata_in(ata_DevHead
, bus
->ab_Port
) & 0x10) >> 4;
261 return bus
->ab_Units
[id
];
264 UBYTE
ata_ReadStatus(struct ata_Bus
*bus
)
266 return ata_in(ata_Status
, bus
->ab_Port
);
269 UBYTE
ata_ReadAltStatus(struct ata_Bus
*bus
)
271 return ata_in(ata_AltStatus
, bus
->ab_Alt
);
275 * handle IRQ; still fast and efficient, supposed to verify if this irq is for us and take adequate steps
276 * part of code moved here from ata.c to reduce containment
278 void ata_IRQSignalTask(struct ata_Bus
*bus
)
281 Signal(bus
->ab_Task
, 1UL << bus
->ab_SleepySignal
);
284 void ata_HandleIRQ(struct ata_Bus
*bus
)
286 struct ata_Unit
*unit
= ata_GetSelectedUnit(bus
);
287 UBYTE status
= ata_ReadStatus(bus
);
290 * don't waste your time on checking other devices.
291 * pass irq ONLY if task is expecting one;
293 if ((unit
!= NULL
) && (0 != bus
->ab_HandleIRQ
)
294 && ((status
& ATAF_BUSY
) == 0))
297 * ok, we have a routine to handle any form of transmission etc.
299 DIRQ(bug("[ATA ] IRQ: Calling dedicated handler... \n"));
300 bus
->ab_HandleIRQ(unit
, status
);
303 * Acknowledge DMA interrupt bit (some controllers require this for
304 * all interrupt types)
306 if (unit
->au_DMAPort
!= 0)
307 ata_out(ata_in(dma_Status
, unit
->au_DMAPort
) |
308 DMAF_Error
| DMAF_Interrupt
, dma_Status
, unit
->au_DMAPort
);
315 * if we got *here* then device is most likely not expected to have an irq.
317 bug("[ATA%02ld] IRQ: Checking busy flag: ", unit
->au_UnitNum
);
319 if (0 == (ATAF_BUSY
& status
))
321 bug("device ready. Dumping details:\n");
323 bug("[ATA ] STATUS: %02lx\n", status
);
324 bug("[ATA ] ALT STATUS: %02lx\n", ata_in(ata_AltStatus
, bus
->ab_Alt
));
325 bug("[ATA ] ERROR: %02lx\n", ata_in(ata_Error
, bus
->ab_Port
));
326 bug("[ATA ] IRQ: REASON: %02lx\n", ata_in(atapi_Reason
, bus
->ab_Port
));
330 bug("device still busy. ignoring irq.\n");
335 void ata_IRQSetHandler(struct ata_Unit
*unit
, void (*handler
)(struct ata_Unit
*, UBYTE
), APTR piomem
, ULONG blklen
, ULONG piolen
)
338 unit
->au_cmd_error
= 0;
340 unit
->au_cmd_data
= piomem
;
341 unit
->au_cmd_length
= (piolen
< blklen
) ? piolen
: blklen
;
342 unit
->au_cmd_total
= piolen
;
343 unit
->au_Bus
->ab_HandleIRQ
= handler
;
346 void ata_IRQNoData(struct ata_Unit
*unit
, UBYTE status
)
348 if (status
& ATAF_BUSY
)
351 if ((unit
->au_cmd_error
== 0) && (status
& ATAF_ERROR
))
352 unit
->au_cmd_error
= HFERR_BadStatus
;
354 DIRQ(bug("[ATA%02ld] IRQ: NoData - done; status %02lx.\n", unit
->au_UnitNum
, status
));
355 ata_IRQSetHandler(unit
, NULL
, NULL
, 0, 0);
356 ata_IRQSignalTask(unit
->au_Bus
);
359 void ata_IRQPIORead(struct ata_Unit
*unit
, UBYTE status
)
361 if (status
& ATAF_DATAREQ
) {
362 DIRQ(bug("[ATA ] IRQ: PIOReadData - DRQ.\n"));
363 unit
->au_ins(unit
->au_cmd_data
, unit
->au_Bus
->ab_Port
, unit
->au_cmd_length
);
366 * indicate it's all done here
368 unit
->au_cmd_data
+= unit
->au_cmd_length
;
369 unit
->au_cmd_total
-= unit
->au_cmd_length
;
370 if (unit
->au_cmd_total
) {
371 if (unit
->au_cmd_length
> unit
->au_cmd_total
)
372 unit
->au_cmd_length
= unit
->au_cmd_total
;
375 DIRQ(bug("[ATA ] IRQ: PIOReadData - transfer completed.\n"));
377 ata_IRQNoData(unit
, status
);
380 void ata_PIOWriteBlk(struct ata_Unit
*unit
)
382 unit
->au_outs(unit
->au_cmd_data
, unit
->au_Bus
->ab_Port
, unit
->au_cmd_length
);
385 * indicate it's all done here
387 unit
->au_cmd_data
+= unit
->au_cmd_length
;
388 unit
->au_cmd_total
-= unit
->au_cmd_length
;
389 if (unit
->au_cmd_length
> unit
->au_cmd_total
)
390 unit
->au_cmd_length
= unit
->au_cmd_total
;
393 void ata_IRQPIOWrite(struct ata_Unit
*unit
, UBYTE status
)
395 if (status
& ATAF_DATAREQ
) {
396 DIRQ(bug("[ATA ] IRQ: PIOWriteData - DRQ.\n"));
397 ata_PIOWriteBlk(unit
);
400 DIRQ(bug("[ATA ] IRQ: PIOWriteData - done.\n"));
401 ata_IRQNoData(unit
, status
);
404 void ata_IRQDMAReadWrite(struct ata_Unit
*au
, UBYTE status
)
406 UBYTE stat
= ata_in(dma_Status
, au
->au_DMAPort
);
407 DIRQ(bug("[ATA%02ld] IRQ: IO status %02lx, DMA status %02lx\n", au
->au_UnitNum
, status
, stat
));
409 if (0 == (stat
& DMAF_Interrupt
))
411 bug("[ATA ] IRQ: Fake IRQ.\n");
413 else if ((status
& ATAF_ERROR
) || (stat
& DMAF_Error
))
415 /* This is turned on in order to help Phantom - Pavel Fedin <sonic_amiga@rambler.ru> */
416 bug("[ATA%02ld] IRQ: IO status %02lx, DMA status %02lx\n", au
->au_UnitNum
, status
, stat
);
417 bug("[ATA%02ld] IRQ: ERROR %02lx\n", au
->au_UnitNum
, ata_in(atapi_Error
, au
->au_Bus
->ab_Port
));
418 bug("[ATA ] IRQ: DMA Failed.\n");
419 au
->au_cmd_error
= HFERR_DMA
;
420 ata_IRQNoData(au
, status
);
422 else if (0 == (status
& (ATAF_BUSY
| ATAF_DATAREQ
)))
424 DIRQ(bug("[ATA ] IRQ: DMA Done.\n"));
425 ata_IRQNoData(au
, status
);
429 void ata_IRQPIOReadAtapi(struct ata_Unit
*unit
, UBYTE status
)
431 ULONG port
= unit
->au_Bus
->ab_Port
;
434 UBYTE reason
= ata_in(atapi_Reason
, port
);
435 DIRQ(bug("[DSCSI] Current status: %ld during READ\n", reason
));
437 /* have we failed yet? */
438 if (0 == (status
& (ATAF_BUSY
| ATAF_DATAREQ
)))
439 ata_IRQNoData(unit
, status
);
440 if (status
& ATAF_ERROR
)
442 ata_IRQNoData(unit
, status
);
446 /* anything for us please? */
447 if (ATAPIF_READ
!= (reason
& ATAPIF_MASK
))
450 size
= ata_in(atapi_ByteCntH
, port
) << 8 | ata_in(atapi_ByteCntL
, port
);
451 DIRQ(bug("[ATAPI] IRQ: data available for read (%ld bytes, max: %ld bytes)\n", size
, unit
->au_cmd_total
));
453 if (size
> unit
->au_cmd_total
)
455 bug("[ATAPI] IRQ: CRITICAL! MORE DATA OFFERED THAN STORAGE CAN TAKE: %ld bytes vs %ld bytes left!\n", size
, unit
->au_cmd_total
);
456 remainder
= size
- unit
->au_cmd_total
;
457 size
= unit
->au_cmd_total
;
460 unit
->au_ins(unit
->au_cmd_data
, port
, size
);
461 unit
->au_cmd_data
= &((UBYTE
*)unit
->au_cmd_data
)[size
];
462 unit
->au_cmd_total
-= size
;
464 DIRQ(bug("[ATAPI] IRQ: %lu bytes read.\n", size
));
466 for (; remainder
> 0; remainder
-= 2)
467 unit
->au_ins(&size
, port
, 2);
469 if (unit
->au_cmd_total
== 0)
470 ata_IRQSetHandler(unit
, &ata_IRQNoData
, NULL
, 0, 0);
473 void ata_IRQPIOWriteAtapi(struct ata_Unit
*unit
, UBYTE status
)
475 ULONG port
= unit
->au_Bus
->ab_Port
;
477 UBYTE reason
= ata_in(atapi_Reason
, port
);
478 DIRQ(bug("[ATAPI] IRQ: Current status: %ld during WRITE\n", reason
));
480 /* have we failed yet? */
481 if (0 == (status
& (ATAF_BUSY
| ATAF_DATAREQ
)))
482 ata_IRQNoData(unit
, status
);
483 if (status
& ATAF_ERROR
)
485 ata_IRQNoData(unit
, status
);
489 /* anything for us please? */
490 if (ATAPIF_WRITE
!= (reason
& ATAPIF_MASK
))
493 size
= ata_in(atapi_ByteCntH
, port
) << 8 | ata_in(atapi_ByteCntL
, port
);
494 DIRQ(bug("[ATAPI] IRQ: data requested for write (%ld bytes, max: %ld bytes)\n", size
, unit
->au_cmd_total
));
496 if (size
> unit
->au_cmd_total
)
498 bug("[ATAPI] IRQ: CRITICAL! MORE DATA REQUESTED THAN STORAGE CAN GIVE: %ld bytes vs %ld bytes left!\n", size
, unit
->au_cmd_total
);
499 size
= unit
->au_cmd_total
;
502 unit
->au_outs(unit
->au_cmd_data
, port
, size
);
503 unit
->au_cmd_data
= &((UBYTE
*)unit
->au_cmd_data
)[size
];
504 unit
->au_cmd_total
-= size
;
506 DIRQ(bug("[ATAPI] IRQ: %lu bytes written.\n", size
));
508 if (unit
->au_cmd_total
== 0)
509 ata_IRQSetHandler(unit
, &ata_IRQNoData
, NULL
, 0, 0);
513 * wait for timeout or drive ready
514 * polling-in-a-loop, but it should be safe to remove this already
516 BOOL
ata_WaitBusyTO(struct ata_Unit
*unit
, UWORD tout
, BOOL irq
, UBYTE
*stout
)
519 ULONG sigs
= SIGBREAKF_CTRL_C
;
523 sigs
|= (irq
? (1 << unit
->au_Bus
->ab_SleepySignal
) : 0);
526 * clear up all old signals
534 unit
->au_Bus
->ab_Timeout
= tout
;
538 * this loop may experience one of two scenarios
539 * 1) we get a valid irq and the drive wanted to let us know that it's ready
540 * 2) we get an invalid irq due to some collisions. We may still want to go ahead and get some extra irq breaks
541 * this would reduce system load a little
547 * delay the check - this was found needed for some hardware
551 //ata_WaitTO(unit->au_Bus->ab_Timer, 0, 1, 0);
554 * let's check if the drive is already good
556 status
= ata_in(ata_AltStatus
, unit
->au_Bus
->ab_Alt
); //ata_in(ata_Status, unit->au_Bus->ab_Port);
557 if (0 == (status
& (ATAF_DATAREQ
| ATAF_BUSY
)))
561 * so we're stuck in a loop?
563 DIRQ(bug("[ATA%02ld] Waiting (Current status: %02lx)...\n", unit
->au_UnitNum
, status
));
566 * if IRQ wait is requested then allow either timeout or irq;
567 * then clear irq flag so we don't keep receiving more of these (especially when system suffers collisions)
572 * wait for either IRQ or TIMEOUT
577 * now if we did reach timeout, then there's no point in going ahead.
579 if (SIGBREAKF_CTRL_C
& step
)
581 bug("[ATA%02ld] Timeout while waiting for device to complete operation\n", unit
->au_UnitNum
);
587 * if we get as far as this, there's no more signals to expect
588 * but we still want the status
590 status
= ata_in(ata_Status
, unit
->au_Bus
->ab_Port
);
598 * every 16n rounds do some extra stuff
600 if ((step
& 16) == 0)
603 * huhm. so it's been 16n rounds already. any timeout yet?
605 if (SetSignal(0, SIGBREAKF_CTRL_C
) & SIGBREAKF_CTRL_C
)
607 DIRQ(bug("[ATA%02ld] Device still busy after timeout. Aborting\n", unit
->au_UnitNum
));
613 * no timeout just yet, but it's not a good idea to keep spinning like that.
614 * let's give the system some time.
617 //ata_WaitTO(unit->au_Bus->ab_Timer, 0, 1, 0);
618 // TODO: Put some delay here!
621 } while(status
& ATAF_BUSY
);
624 * be nice to frustrated developer
626 DIRQ(bug("[ATA%02ld] WaitBusy status: %lx / %ld\n", unit
->au_UnitNum
, status
, res
));
629 * clear up all our expectations
632 unit
->au_Bus
->ab_Timeout
= -1;
636 * clear any interrupt (may be neccessary if we were polling, for example)
638 ata_in(ata_Status
, unit
->au_Bus
->ab_Port
);
646 * and say it went fine (i mean it)
654 * Procedure for sending ATA command blocks
655 * it appears LARGE but there's a lot of COMMENTS here :)
656 * handles *all* ata commands (no data, pio and dma)
657 * naturally could be split at some point in the future
658 * depends if anyone believes that the change for 50 lines
659 * would make slow ATA transfers any faster
661 static ULONG
ata_exec_cmd(struct ata_Unit
* au
, ata_CommandBlock
*block
)
663 ULONG port
= au
->au_Bus
->ab_Port
;
665 APTR mem
= block
->buffer
;
668 if (FALSE
== ata_SelectUnit(au
))
669 return IOERR_UNITBUSY
;
674 if (block
->sectors
> 256)
676 bug("[ATA%02ld] ata_exec_cmd: ERROR: Transfer length (%ld) exceeds 256 sectors. Aborting.\n", au
->au_UnitNum
, block
->sectors
);
677 return IOERR_BADLENGTH
;
681 * we want the above to fall in here!
682 * we really do (checking for secmul)
686 if (block
->sectors
> 65536)
688 bug("[ATA%02ld] ata_exec_cmd: ERROR: Transfer length (%ld) exceeds 65536 sectors. Aborting.\n", au
->au_UnitNum
, block
->sectors
);
689 return IOERR_BADLENGTH
;
691 if (block
->secmul
== 0)
693 bug("[ATA%02ld] ata_exec_cmd: ERROR: Invalid transfer multiplier. Should be at least set to 1 (correcting)\n", au
->au_UnitNum
);
702 bug("[ATA%02ld] ata_exec_cmd: ERROR: Invalid command type %lx. Aborting.\n", au
->au_UnitNum
, block
->type
);
707 D(bug("[ATA%02ld] ata_exec_cmd: Executing command %02lx\n", au
->au_UnitNum
, block
->command
));
709 if (block
->feature
!= 0)
710 ata_out(block
->feature
, ata_Feature
, port
);
713 * - set LBA and sector count
718 DATA(bug("[ATA%02ld] ata_exec_cmd: Command uses 28bit LBA addressing (OLD)\n", au
->au_UnitNum
));
719 ata_out(((block
->blk
>> 24) & 0x0f) | 0x40 | au
->au_DevMask
, ata_DevHead
, port
);
720 ata_out(block
->blk
>> 16, ata_LBAHigh
, port
);
721 ata_out(block
->blk
>> 8, ata_LBAMid
, port
);
722 ata_out(block
->blk
, ata_LBALow
, port
);
723 ata_out(block
->sectors
, ata_Count
, port
);
727 DATA(bug("[ATA%02ld] ata_exec_cmd: Command uses 48bit LBA addressing (NEW)\n", au
->au_UnitNum
));
728 ata_out(block
->blk
>> 40, ata_LBAHigh
, port
);
729 ata_out(block
->blk
>> 32, ata_LBAMid
, port
);
730 ata_out(block
->blk
>> 24, ata_LBALow
, port
);
732 ata_out(block
->blk
>> 16, ata_LBAHigh
, port
);
733 ata_out(block
->blk
>> 8, ata_LBAMid
, port
);
734 ata_out(block
->blk
, ata_LBALow
, port
);
736 ata_out(block
->sectors
>> 8, ata_Count
, port
);
737 ata_out(block
->sectors
, ata_Count
, port
);
741 DATA(bug("[ATA%02ld] ata_exec_cmd: Command does not address any block\n", au
->au_UnitNum
));
745 switch (block
->method
)
748 ata_IRQSetHandler(au
, &ata_IRQPIOWrite
, mem
, block
->secmul
<< au
->au_SectorShift
, block
->length
);
752 ata_IRQSetHandler(au
, &ata_IRQPIORead
, mem
, block
->secmul
<< au
->au_SectorShift
, block
->length
);
756 if (FALSE
== dma_SetupPRDSize(au
, mem
, block
->length
, TRUE
))
757 return IOERR_ABORTED
;
758 ata_IRQSetHandler(au
, &ata_IRQDMAReadWrite
, NULL
, 0, 0);
763 if (FALSE
== dma_SetupPRDSize(au
, mem
, block
->length
, FALSE
))
764 return IOERR_ABORTED
;
765 ata_IRQSetHandler(au
, &ata_IRQDMAReadWrite
, NULL
, 0, 0);
770 ata_IRQSetHandler(au
, &ata_IRQNoData
, NULL
, 0, 0);
780 * let drive propagate its signals
782 DATA(bug("[ATA%02ld] ata_exec_cmd: Sending command\n", au
->au_UnitNum
));
783 ata_out(block
->command
, ata_Command
, port
);
785 //ata_WaitTO(au->au_Bus->ab_Timer, 0, 1, 0);
788 * In case of PIO write the drive won't issue an IRQ before first
789 * data transfer, so we should poll the status and send the first
790 * block upon request.
792 if (block
->method
== CM_PIOWrite
) {
793 if (FALSE
== ata_WaitBusyTO(au
, 30, FALSE
, &status
)) {
794 D(bug("[ATA%02ld] ata_exec_cmd: PIOWrite - no response from device\n", au
->au_UnitNum
));
795 return IOERR_UNITBUSY
;
797 if (status
& ATAF_DATAREQ
) {
798 DATA(bug("[ATA%02ld] ata_exec_cmd: PIOWrite - DRQ.\n", au
->au_UnitNum
));
803 D(bug("[ATA%02ld] ata_exec_cmd: PIOWrite - bad status: %02X\n", status
));
804 return HFERR_BadStatus
;
809 * wait for drive to complete what it has to do
811 if (FALSE
== ata_WaitBusyTO(au
, 30, TRUE
, NULL
))
813 bug("[ATA%02ld] ata_exec_cmd: Device is late - no response\n", au
->au_UnitNum
);
814 err
= IOERR_UNITBUSY
;
817 err
= au
->au_cmd_error
;
819 DATA(bug("[ATA%02ld] ata_exec_cmd: Command done\n", au
->au_UnitNum
));
822 * don't use 'mem' pointer here as it's already invalid.
824 if (block
->method
== CM_DMARead
)
827 dma_Cleanup(block
->buffer
, block
->length
, TRUE
);
829 else if (block
->method
== CM_DMAWrite
)
832 dma_Cleanup(block
->buffer
, block
->length
, FALSE
);
835 D(bug("[ATA%02ld] ata_exec_cmd: return code %ld\n", au
->au_UnitNum
, err
));
842 int atapi_SendPacket(struct ata_Unit
*unit
, APTR packet
, APTR data
, LONG datalen
, BOOL
*dma
, BOOL write
)
844 *dma
= *dma
&& (unit
->au_XferModes
& AF_XFER_DMA
) ? TRUE
: FALSE
;
850 register int t
=5,l
=0;
851 ULONG port
= unit
->au_Bus
->ab_Port
;
853 if (((UBYTE
*)packet
)[0] > 0x1f)
855 if (((UBYTE
*)packet
)[0] > 0x5f)
858 switch (((UBYTE
*)packet
)[0])
863 case 0xb9: // readcdmsf
866 case 0xaa: // write12
867 case 0x2e: // writeverify
868 case 0xad: // readdvdstructure
869 case 0xa4: // reportkey
870 case 0xa3: // sendkey
878 cmd
[l
] = ((UBYTE
*)packet
)[l
];
883 bug("[ATA%02lx] Sending %s ATA packet: ", unit
->au_UnitNum
, (*dma
) ? "DMA" : "PIO");
887 bug("%02lx ", ((UBYTE
*)cmd
)[l
]);
893 bug("[ATAPI] ERROR: DATA LENGTH NOT EVEN! Rounding Up! (%ld bytes requested)\n", datalen
);
896 datalen
= (datalen
+1)&~1;
898 if (FALSE
== ata_SelectUnit(unit
))
900 DATAPI(bug("[ATAPI] WaitBusy failed at first check\n"));
901 return IOERR_UNITBUSY
;
905 * tell device whether we want to read or write and if we want a dma transfer
907 ata_out(((*dma
) ? 1 : 0) |
908 (((unit
->au_Drive
->id_DMADir
& 0x8000) && !write
) ? 4 : 0),
909 atapi_Features
, port
);
910 ata_out((datalen
& 0xff), atapi_ByteCntL
, port
);
911 ata_out((datalen
>> 8) & 0xff, atapi_ByteCntH
, port
);
914 * once we're done with that, we can go ahead and inform device that we're about to send atapi packet
915 * after command is dispatched, we are obliged to give 400ns for the unit to parse command and set status
917 DATAPI(bug("[ATAPI] Issuing ATA_PACKET command.\n"));
918 ata_out(ATA_PACKET
, atapi_Command
, port
);
920 //ata_WaitTO(unit->au_Bus->ab_Timer, 0, 1, 0);
922 ata_WaitBusyTO(unit
, 30, (unit
->au_Drive
->id_General
& 0x60) == 0x20, NULL
);
923 if (0 == (ata_ReadStatus(unit
->au_Bus
) & ATAF_DATAREQ
))
924 return HFERR_BadStatus
;
927 * setup appropriate hooks
930 ata_IRQSetHandler(unit
, &ata_IRQNoData
, 0, 0, 0);
932 ata_IRQSetHandler(unit
, &ata_IRQDMAReadWrite
, NULL
, 0, 0);
934 ata_IRQSetHandler(unit
, &ata_IRQPIOWriteAtapi
, data
, 0, datalen
);
936 ata_IRQSetHandler(unit
, &ata_IRQPIOReadAtapi
, data
, 0, datalen
);
940 DATAPI(bug("[ATAPI] Starting DMA\n"));
944 DATAPI(bug("[ATAPI] Sending packet\n"));
945 unit
->au_outs(cmd
, unit
->au_Bus
->ab_Port
, 12);
947 DATAPI(bug("[ATAPI] Status after packet: %lx\n",
948 ata_ReadAltStatus(unit
->au_Bus
)));
951 * Wait for command to complete. Note that two interrupts will occur
952 * before we wake up if this is a PIO data transfer
954 if (ata_WaitTO(unit
->au_Bus
->ab_Timer
, 30, 0,
955 1 << unit
->au_Bus
->ab_SleepySignal
) == 0)
957 DATAPI(bug("[DSCSI] Command timed out.\n"));
958 err
= IOERR_UNITBUSY
;
961 err
= atapi_EndCmd(unit
);
966 dma_Cleanup(data
, datalen
, !write
);
969 DATAPI(bug("[ATAPI] IO error code %ld\n", err
));
973 ULONG
atapi_DirectSCSI(struct ata_Unit
*unit
, struct SCSICmd
*cmd
)
975 APTR buffer
= cmd
->scsi_Data
;
976 ULONG length
= cmd
->scsi_Length
;
981 cmd
->scsi_Actual
= 0;
983 DATAPI(bug("[DSCSI] Sending packet!\n"));
986 * setup DMA & push command
987 * it does not really mean we will use dma here btw
989 if ((unit
->au_XferModes
& AF_XFER_DMA
) && (length
!=0) && (buffer
!= 0))
992 if ((cmd
->scsi_Flags
& SCSIF_READ
) != 0)
995 if (FALSE
== dma_SetupPRDSize(unit
, buffer
, length
, TRUE
))
1000 if (FALSE
== dma_SetupPRDSize(unit
, buffer
, length
, FALSE
))
1005 err
= atapi_SendPacket(unit
, cmd
->scsi_Command
, cmd
->scsi_Data
, cmd
->scsi_Length
, &dma
, (cmd
->scsi_Flags
& SCSIF_READ
) == 0);
1007 DUMP({ if (cmd
->scsi_Data
!= 0) dump(cmd
->scsi_Data
, cmd
->scsi_Length
); });
1010 * on check condition - grab sense data
1012 DATAPI(bug("[ATA%02lx] atapi_DirectSCSI: SCSI Flags: %02lx / Error: %ld\n", unit
->au_UnitNum
, cmd
->scsi_Flags
, err
));
1013 if ((err
!= 0) && (cmd
->scsi_Flags
& SCSIF_AUTOSENSE
))
1015 DATAPI(bug("[DSCSI] atapi_DirectSCSI: Packet Failed. Calling atapi_RequestSense\n"));
1016 atapi_RequestSense(unit
, cmd
->scsi_SenseData
, cmd
->scsi_SenseLength
);
1017 DUMP(dump(cmd
->scsi_SenseData
, cmd
->scsi_SenseLength
));
1024 * chops the large transfers into set of smaller transfers
1025 * specifically useful when requested transfer size is >256 sectors for 28bit commands
1027 static ULONG
ata_exec_blk(struct ata_Unit
*unit
, ata_CommandBlock
*blk
)
1032 ULONG count
=blk
->sectors
;
1034 if (blk
->type
== CT_LBA48
)
1037 DATA(bug("[ATA%02ld] ata_exec_blk: Accessing %ld sectors starting from %x%08x\n", unit
->au_UnitNum
, count
, (ULONG
)(blk
->blk
>> 32), (ULONG
)blk
->blk
));
1038 while ((count
> 0) && (err
== 0))
1040 part
= (count
> max
) ? max
: count
;
1041 blk
->sectors
= part
;
1042 blk
->length
= part
<< unit
->au_SectorShift
;
1044 DATA(bug("[ATA%02ld] Transfer of %ld sectors from %x%08x\n", unit
->au_UnitNum
, part
, (ULONG
)(blk
->blk
>> 32), (ULONG
)blk
->blk
));
1045 err
= ata_exec_cmd(unit
, blk
);
1046 DATA(bug("[ATA%02ld] ata_exec_blk: ata_exec_cmd returned %lx\n", unit
->au_UnitNum
, err
));
1049 blk
->buffer
= &((char*)blk
->buffer
)[part
<< unit
->au_SectorShift
];
1056 * Initial device configuration that suits *all* cases
1058 BOOL
ata_init_unit(struct ata_Bus
*bus
, UBYTE u
)
1060 struct ata_Unit
*unit
=NULL
;
1062 DINIT(bug("[ATA ] ata_init_unit(%ld)\n", u
));
1064 unit
= bus
->ab_Units
[u
];
1069 unit
->au_Drive
= AllocPooled(bus
->ab_Base
->ata_MemPool
, sizeof(struct DriveIdent
));
1070 unit
->au_UnitNum
= bus
->ab_BusNum
<< 1 | u
; // b << 8 | u
1071 unit
->au_DevMask
= 0xa0 | (u
<< 4);
1072 if (bus
->ab_Base
->ata_32bit
)
1074 unit
->au_ins
= ata_insl
;
1075 unit
->au_outs
= ata_outsl
;
1079 unit
->au_ins
= ata_insw
;
1080 unit
->au_outs
= ata_outsw
;
1082 unit
->au_SectorShift
= 9; /* this really has to be set here. */
1084 NEWLIST(&unit
->au_SoftList
);
1087 * since the stack is always handled by caller
1088 * it's safe to stub all calls with one function
1090 unit
->au_Read32
= ata_STUB_IO32
;
1091 unit
->au_Read64
= ata_STUB_IO64
;
1092 unit
->au_Write32
= ata_STUB_IO32
;
1093 unit
->au_Write64
= ata_STUB_IO64
;
1094 unit
->au_Eject
= ata_STUB
;
1095 unit
->au_DirectSCSI
= ata_STUB_SCSI
;
1096 unit
->au_Identify
= ata_STUB
;
1100 BOOL
ata_setup_unit(struct ata_Bus
*bus
, UBYTE u
)
1102 struct ata_Unit
*unit
=NULL
;
1105 * this stuff always goes along the same way
1106 * WARNING: NO INTERRUPTS AT THIS POINT!
1108 DINIT(bug("[ATA ] ata_setup_unit(%ld,%ld)\n", bus
->ab_BusNum
, u
));
1110 unit
= bus
->ab_Units
[u
];
1114 ata_SelectUnit(unit
);
1116 if (unit
->au_DMAPort
!= 0
1117 && (ata_in(dma_Status
, unit
->au_DMAPort
) & 0x80) != 0)
1118 bug("[ATA%02ld] ata_setup_unit: WARNING: Controller only supports "
1119 "DMA on one bus at a time. DMAStatus=%lx\n", unit
->au_UnitNum
,
1120 ata_in(dma_Status
, unit
->au_DMAPort
));
1122 if (FALSE
== ata_WaitBusyTO(unit
, 1, FALSE
, NULL
))
1124 DINIT(bug("[ATA%02ld] ata_setup_unit: ERROR: Drive not ready for use. Keeping functions stubbed\n", unit
->au_UnitNum
));
1125 FreePooled(bus
->ab_Base
->ata_MemPool
, unit
->au_Drive
, sizeof(struct DriveIdent
));
1130 switch (bus
->ab_Dev
[u
])
1133 * safe fallback settings
1137 unit
->au_Identify
= atapi_Identify
;
1142 unit
->au_Identify
= ata_Identify
;
1146 DINIT(bug("[ATA%02ld] ata_setup_unit: Unsupported device %lx. All functions will remain stubbed.\n", unit
->au_UnitNum
, bus
->ab_Dev
[u
]));
1147 FreePooled(bus
->ab_Base
->ata_MemPool
, unit
->au_Drive
, sizeof(struct DriveIdent
));
1152 D(bug("[ATA ] ata_setup_unit: Enabling IRQs\n"));
1153 ata_out(0x0, ata_AltControl
, bus
->ab_Alt
);
1156 * now make unit self diagnose
1158 if (unit
->au_Identify(unit
) != 0)
1160 FreePooled(bus
->ab_Base
->ata_MemPool
, unit
->au_Drive
, sizeof(struct DriveIdent
));
1161 unit
->au_Drive
= NULL
;
1171 static void common_SetXferMode(struct ata_Unit
* unit
, ata_XferMode mode
)
1175 ata_CommandBlock acb
=
1189 DINIT(bug("[ATA%02ld] common_SetXferMode: Trying to set mode %d\n", unit
->au_UnitNum
, mode
));
1191 if ((unit
->au_DMAPort
== 0) && (mode
>= AB_XFER_MDMA0
))
1193 DINIT(bug("[ATA%02ld] common_SetXferMode: This controller does not own DMA port! Will set best PIO\n", unit
->au_UnitNum
));
1194 common_SetBestXferMode(unit
);
1199 * first, ONLY for ATA devices, set new commands
1201 if (0 == (unit
->au_XferModes
& AF_XFER_PACKET
))
1203 if ((mode
>= AB_XFER_PIO0
) && (mode
<= AB_XFER_PIO4
))
1205 if ((!unit
->au_Bus
->ab_Base
->ata_NoMulti
) && (unit
->au_XferModes
& AF_XFER_RWMULTI
))
1207 ata_IRQSetHandler(unit
, ata_IRQNoData
, NULL
, 0, 0);
1208 ata_out(unit
->au_Drive
->id_RWMultipleSize
& 0xFF, ata_Count
, unit
->au_Bus
->ab_Port
);
1209 ata_out(ATA_SET_MULTIPLE
, ata_Command
, unit
->au_Bus
->ab_Port
);
1210 ata_WaitBusyTO(unit
, -1, TRUE
, NULL
);
1212 unit
->au_Read32
= ata_ReadMultiple32
;
1213 unit
->au_Write32
= ata_WriteMultiple32
;
1214 if (unit
->au_XferModes
& AF_XFER_48BIT
)
1216 unit
->au_Read64
= ata_ReadMultiple64
;
1217 unit
->au_Write64
= ata_WriteMultiple64
;
1222 unit
->au_Read32
= ata_ReadSector32
;
1223 unit
->au_Write32
= ata_WriteSector32
;
1224 if (unit
->au_XferModes
& AF_XFER_48BIT
)
1226 unit
->au_Read64
= ata_ReadSector64
;
1227 unit
->au_Write64
= ata_WriteSector64
;
1231 else if ((mode
>= AB_XFER_MDMA0
) && (mode
<= AB_XFER_MDMA2
))
1233 unit
->au_Read32
= ata_ReadDMA32
;
1234 unit
->au_Write32
= ata_WriteDMA32
;
1235 if (unit
->au_XferModes
& AF_XFER_48BIT
)
1237 unit
->au_Read64
= ata_ReadDMA64
;
1238 unit
->au_Write64
= ata_WriteDMA64
;
1241 else if ((mode
>= AB_XFER_UDMA0
) && (mode
<= AB_XFER_UDMA6
))
1243 unit
->au_Read32
= ata_ReadDMA32
;
1244 unit
->au_Write32
= ata_WriteDMA32
;
1245 if (unit
->au_XferModes
& AF_XFER_48BIT
)
1247 unit
->au_Read64
= ata_ReadDMA64
;
1248 unit
->au_Write64
= ata_WriteDMA64
;
1253 unit
->au_Read32
= ata_ReadSector32
;
1254 unit
->au_Write32
= ata_WriteSector32
;
1255 if (unit
->au_XferModes
& AF_XFER_48BIT
)
1257 unit
->au_Read64
= ata_ReadSector64
;
1258 unit
->au_Write64
= ata_WriteSector64
;
1263 if ((mode
>= AB_XFER_PIO0
) && (mode
<= AB_XFER_PIO4
))
1265 type
= 8 + (mode
- AB_XFER_PIO0
);
1267 else if ((mode
>= AB_XFER_MDMA0
) && (mode
<= AB_XFER_MDMA2
))
1269 type
= 32 + (mode
- AB_XFER_MDMA0
);
1272 else if ((mode
>= AB_XFER_UDMA0
) && (mode
<= AB_XFER_UDMA6
))
1274 type
= 64 + (mode
- AB_XFER_UDMA0
);
1282 #if 0 // We can't set drive modes unless we also set the controller's timing registers
1284 if (0 != ata_exec_cmd(unit
, &acb
))
1286 DINIT(bug("[ATA%02ld] common_SetXferMode: ERROR: Failed to apply new xfer mode.\n", unit
->au_UnitNum
));
1290 if (unit
->au_DMAPort
)
1292 type
= ata_in(dma_Status
, unit
->au_DMAPort
);
1296 type
|= 1 << (5 + (unit
->au_UnitNum
& 1));
1300 type
&= ~(1 << (5 + (unit
->au_UnitNum
& 1)));
1303 DINIT(bug("[DSCSI] common_SetXferMode: Trying to apply new DMA (%lx) status: %02lx (unit %ld)\n", unit
->au_DMAPort
, type
, unit
->au_UnitNum
& 1));
1305 ata_SelectUnit(unit
);
1306 ata_out(type
, dma_Status
, unit
->au_DMAPort
);
1307 if (type
== (ata_in(dma_Status
, unit
->au_DMAPort
) & 0x60))
1309 DINIT(bug("[DSCSI] common_SetXferMode: New DMA Status: %02lx\n", type
));
1313 DINIT(bug("[DSCSI] common_SetXferMode: Failed to modify DMA state for this device\n"));
1319 unit
->au_XferModes
|= AF_XFER_DMA
;
1321 unit
->au_XferModes
&= ~AF_XFER_DMA
;
1324 static void common_SetBestXferMode(struct ata_Unit
* unit
)
1327 int max
= AB_XFER_UDMA6
;
1329 if (unit
->au_DMAPort
== 0
1330 || !(unit
->au_Drive
->id_MWDMASupport
& 0x0700)
1331 && !(unit
->au_Drive
->id_UDMASupport
& 0x7f00))
1334 * make sure you reduce scan search to pio here!
1335 * otherwise this and above function will fall into infinite loop
1337 DINIT(bug("[ATA%02ld] common_SetBestXferMode: This controller does not own DMA port\n", unit
->au_UnitNum
));
1340 else if (!(unit
->au_Flags
& AF_80Wire
))
1342 DINIT(bug("[ATA%02ld] common_SetBestXferMode: "
1343 "An 80-wire cable has not been detected for this drive. "
1344 "Disabling modes above UDMA2.\n", unit
->au_UnitNum
));
1345 max
= AB_XFER_UDMA2
;
1348 for (iter
=max
; iter
>=AB_XFER_PIO0
; --iter
)
1350 if (unit
->au_XferModes
& (1<<iter
))
1352 common_SetXferMode(unit
, iter
);
1356 bug("[ATA%02ld] common_SetBestXferMode: ERROR: device never reported any valid xfer modes. will continue at default\n", unit
->au_UnitNum
);
1357 common_SetXferMode(unit
, AB_XFER_PIO0
);
1360 void common_DetectXferModes(struct ata_Unit
* unit
)
1364 DINIT(bug("[ATA%02ld] common_DetectXferModes: Supports\n", unit
->au_UnitNum
));
1366 if (unit
->au_Drive
->id_Commands4
& (1 << 4))
1368 DINIT(bug("[ATA%02ld] common_DetectXferModes: - Packet interface\n", unit
->au_UnitNum
));
1369 unit
->au_XferModes
|= AF_XFER_PACKET
;
1370 unit
->au_DirectSCSI
= atapi_DirectSCSI
;
1372 else if (unit
->au_Drive
->id_Commands5
& (1 << 10))
1374 /* ATAPI devices do not use this bit. */
1375 DINIT(bug("[ATA%02ld] common_DetectXferModes: - 48bit I/O\n", unit
->au_UnitNum
));
1376 unit
->au_XferModes
|= AF_XFER_48BIT
;
1379 if ((unit
->au_XferModes
& AF_XFER_PACKET
) || (unit
->au_Drive
->id_Capabilities
& (1<< 9)))
1381 DINIT(bug("[ATA%02ld] common_DetectXferModes: - LBA Addressing\n", unit
->au_UnitNum
));
1382 unit
->au_XferModes
|= AF_XFER_LBA
;
1386 DINIT(bug("[ATA%02ld] common_DetectXferModes: - DEVICE DOES NOT SUPPORT LBA ADDRESSING >> THIS IS A POTENTIAL PROBLEM <<\n", unit
->au_UnitNum
));
1389 if (unit
->au_Drive
->id_RWMultipleSize
& 0xff)
1391 DINIT(bug("[ATA%02ld] common_DetectXferModes: - R/W Multiple (%ld sectors per xfer)\n", unit
->au_UnitNum
, unit
->au_Drive
->id_RWMultipleSize
& 0xff));
1392 unit
->au_XferModes
|= AF_XFER_RWMULTI
;
1395 DINIT(bug("[ATA%02ld] common_DetectXferModes: - PIO0 PIO1 PIO2 ",
1397 unit
->au_XferModes
|= AF_XFER_PIO(0) | AF_XFER_PIO(1) | AF_XFER_PIO(2);
1398 if (unit
->au_Drive
->id_ConfigAvailable
& (1 << 1))
1400 for (iter
= 0; iter
< 2; iter
++)
1402 if (unit
->au_Drive
->id_PIOSupport
& (1 << iter
))
1404 DINIT(bug("PIO%ld ", 3 + iter
));
1405 unit
->au_XferModes
|= AF_XFER_PIO(3 + iter
);
1411 if ((unit
->au_Drive
->id_ConfigAvailable
& (1 << 1)) &&
1412 (unit
->au_Drive
->id_Capabilities
& (1<<8)))
1414 DINIT(bug("[ATA%02ld] common_DetectXferModes: DMA:\n", unit
->au_UnitNum
));
1415 if (unit
->au_Drive
->id_MWDMASupport
& 0xff)
1417 DINIT(bug("[ATA%02ld] common_DetectXferModes: - ", unit
->au_UnitNum
));
1418 for (iter
= 0; iter
< 3; iter
++)
1420 if (unit
->au_Drive
->id_MWDMASupport
& (1 << iter
))
1422 unit
->au_XferModes
|= AF_XFER_MDMA(iter
);
1423 if (unit
->au_Drive
->id_MWDMASupport
& (256 << iter
))
1425 DINIT(bug("[MDMA%ld] ", iter
));
1429 DINIT(bug("MDMA%ld ", iter
));
1436 if (unit
->au_Drive
->id_UDMASupport
& 0xff)
1438 DINIT(bug("[ATA%02ld] common_DetectXferModes: - ", unit
->au_UnitNum
));
1439 for (iter
= 0; iter
< 7; iter
++)
1441 if (unit
->au_Drive
->id_UDMASupport
& (1 << iter
))
1443 unit
->au_XferModes
|= AF_XFER_UDMA(iter
);
1444 if (unit
->au_Drive
->id_UDMASupport
& (256 << iter
))
1446 DINIT(bug("[UDMA%ld] ", iter
));
1450 DINIT(bug("UDMA%ld ", iter
));
1459 #define SWAP_LE_WORD(x) (x) = AROS_LE2WORD((x))
1460 #define SWAP_LE_LONG(x) (x) = AROS_LE2LONG((x))
1461 #define SWAP_LE_QUAD(x) (x) = AROS_LE2LONG((x)>>32) | AROS_LE2LONG((x) & 0xffffffff) << 32
1463 BYTE
atapi_Identify(struct ata_Unit
* unit
)
1465 ata_CommandBlock acb
=
1474 sizeof(struct DriveIdent
),
1480 ata_SelectUnit(unit
);
1482 if (ata_exec_cmd(unit
, &acb
))
1484 return IOERR_OPENFAIL
;
1487 #if (AROS_BIG_ENDIAN != 0)
1488 SWAP_LE_WORD(unit
->au_Drive
->id_General
);
1489 SWAP_LE_WORD(unit
->au_Drive
->id_OldCylinders
);
1490 SWAP_LE_WORD(unit
->au_Drive
->id_SpecificConfig
);
1491 SWAP_LE_WORD(unit
->au_Drive
->id_OldHeads
);
1492 SWAP_LE_WORD(unit
->au_Drive
->id_OldSectors
);
1493 SWAP_LE_WORD(unit
->au_Drive
->id_RWMultipleSize
);
1494 SWAP_LE_WORD(unit
->au_Drive
->id_Capabilities
);
1495 SWAP_LE_WORD(unit
->au_Drive
->id_OldCaps
);
1496 SWAP_LE_WORD(unit
->au_Drive
->id_OldPIO
);
1497 SWAP_LE_WORD(unit
->au_Drive
->id_ConfigAvailable
);
1498 SWAP_LE_WORD(unit
->au_Drive
->id_OldLCylinders
);
1499 SWAP_LE_WORD(unit
->au_Drive
->id_OldLHeads
);
1500 SWAP_LE_WORD(unit
->au_Drive
->id_OldLSectors
);
1501 SWAP_LE_WORD(unit
->au_Drive
->id_RWMultipleTrans
);
1502 SWAP_LE_WORD(unit
->au_Drive
->id_MWDMASupport
);
1503 SWAP_LE_WORD(unit
->au_Drive
->id_PIOSupport
);
1504 SWAP_LE_WORD(unit
->au_Drive
->id_MWDMA_MinCycleTime
);
1505 SWAP_LE_WORD(unit
->au_Drive
->id_MWDMA_DefCycleTime
);
1506 SWAP_LE_WORD(unit
->au_Drive
->id_PIO_MinCycleTime
);
1507 SWAP_LE_WORD(unit
->au_Drive
->id_PIO_MinCycleTImeIORDY
);
1508 SWAP_LE_WORD(unit
->au_Drive
->id_QueueDepth
);
1509 SWAP_LE_WORD(unit
->au_Drive
->id_ATAVersion
);
1510 SWAP_LE_WORD(unit
->au_Drive
->id_ATARevision
);
1511 SWAP_LE_WORD(unit
->au_Drive
->id_Commands1
);
1512 SWAP_LE_WORD(unit
->au_Drive
->id_Commands2
);
1513 SWAP_LE_WORD(unit
->au_Drive
->id_Commands3
);
1514 SWAP_LE_WORD(unit
->au_Drive
->id_Commands4
);
1515 SWAP_LE_WORD(unit
->au_Drive
->id_Commands5
);
1516 SWAP_LE_WORD(unit
->au_Drive
->id_Commands6
);
1517 SWAP_LE_WORD(unit
->au_Drive
->id_UDMASupport
);
1518 SWAP_LE_WORD(unit
->au_Drive
->id_SecurityEraseTime
);
1519 SWAP_LE_WORD(unit
->au_Drive
->id_ESecurityEraseTime
);
1520 SWAP_LE_WORD(unit
->au_Drive
->id_CurrentAdvPowerMode
);
1521 SWAP_LE_WORD(unit
->au_Drive
->id_MasterPwdRevision
);
1522 SWAP_LE_WORD(unit
->au_Drive
->id_HWResetResult
);
1523 SWAP_LE_WORD(unit
->au_Drive
->id_AcousticManagement
);
1524 SWAP_LE_WORD(unit
->au_Drive
->id_StreamMinimunReqSize
);
1525 SWAP_LE_WORD(unit
->au_Drive
->id_StreamingTimeDMA
);
1526 SWAP_LE_WORD(unit
->au_Drive
->id_StreamingLatency
);
1527 SWAP_LE_WORD(unit
->au_Drive
->id_StreamingTimePIO
);
1528 SWAP_LE_WORD(unit
->au_Drive
->id_PhysSectorSize
);
1529 SWAP_LE_WORD(unit
->au_Drive
->id_RemMediaStatusNotificationFeatures
);
1530 SWAP_LE_WORD(unit
->au_Drive
->id_SecurityStatus
);
1532 SWAP_LE_LONG(unit
->au_Drive
->id_WordsPerLogicalSector
);
1533 SWAP_LE_LONG(unit
->au_Drive
->id_LBASectors
);
1534 SWAP_LE_LONG(unit
->au_Drive
->id_StreamingGranularity
);
1536 SWAP_LE_QUAD(unit
->au_Drive
->id_LBA48Sectors
);
1540 * Some SATA controllers in legacy mode emulate a non-existent ATAPI
1541 * device so well that we have to wait until we get an empty identify
1542 * structure to find out that the drive isn't real
1544 if ((unit
->au_Drive
->id_General
& 0x8000) == 0)
1546 bug("[ATA%02ld] atapi_Identify: Identify data is invalid."
1547 " Disabling drive.\n", unit
->au_UnitNum
);
1548 return IOERR_OPENFAIL
;
1551 DUMP(dump(unit
->au_Drive
, sizeof(struct DriveIdent
)));
1553 unit
->au_SectorShift
= 11;
1554 unit
->au_Read32
= atapi_Read
;
1555 unit
->au_Write32
= atapi_Write
;
1556 unit
->au_DirectSCSI
= atapi_DirectSCSI
;
1557 unit
->au_Eject
= atapi_Eject
;
1558 unit
->au_Flags
|= AF_DiscChanged
;
1559 unit
->au_DevType
= (unit
->au_Drive
->id_General
>>8) & 0x1f;
1560 unit
->au_XferModes
= AF_XFER_PACKET
;
1562 ata_strcpy(unit
->au_Drive
->id_Model
, unit
->au_Model
, 40);
1563 ata_strcpy(unit
->au_Drive
->id_SerialNumber
, unit
->au_SerialNumber
, 20);
1564 ata_strcpy(unit
->au_Drive
->id_FirmwareRev
, unit
->au_FirmwareRev
, 8);
1566 bug("[ATA%02ld] atapi_Identify: Unit info: %s / %s / %s\n", unit
->au_UnitNum
, unit
->au_Model
, unit
->au_SerialNumber
, unit
->au_FirmwareRev
);
1567 common_DetectXferModes(unit
);
1568 common_SetBestXferMode(unit
);
1570 if (unit
->au_Drive
->id_General
& 0x80)
1572 DINIT(bug("[ATA%02ld] Device is removable.\n", unit
->au_UnitNum
));
1573 unit
->au_Flags
|= AF_Removable
;
1576 unit
->au_Capacity
= unit
->au_Drive
->id_LBASectors
;
1577 unit
->au_Capacity48
= unit
->au_Drive
->id_LBA48Sectors
;
1578 bug("[ATA%02ld] atapi_Identify: Unit info: %07lx 28bit / %04lx:%08lx 48bit addressable blocks\n", unit
->au_UnitNum
, unit
->au_Capacity
, (ULONG
)(unit
->au_Capacity48
>> 32), (ULONG
)(unit
->au_Capacity48
& 0xfffffffful
));
1581 * ok, this is not very original, but quite compatible :P
1583 switch (unit
->au_DevType
)
1587 case DG_OPTICAL_DISK
:
1588 unit
->au_SectorShift
= 11;
1590 unit
->au_Sectors
= 75;
1591 unit
->au_Cylinders
= 4440;
1594 case DG_DIRECT_ACCESS
:
1595 unit
->au_SectorShift
= 9;
1596 if (!strcmp("LS-120", &unit
->au_Model
[0]))
1599 unit
->au_Sectors
= 18;
1600 unit
->au_Cylinders
= 6848;
1602 else if (!strcmp("ZIP 100 ", &unit
->au_Model
[8]))
1605 unit
->au_Sectors
= 64;
1606 unit
->au_Cylinders
= 3072;
1611 atapi_TestUnitOK(unit
);
1616 BYTE
ata_Identify(struct ata_Unit
* unit
)
1618 ata_CommandBlock acb
=
1620 ATA_IDENTIFY_DEVICE
,
1627 sizeof(struct DriveIdent
),
1633 if (ata_exec_cmd(unit
, &acb
))
1635 return IOERR_OPENFAIL
;
1638 #if (AROS_BIG_ENDIAN != 0)
1639 SWAP_LE_WORD(unit
->au_Drive
->id_General
);
1640 SWAP_LE_WORD(unit
->au_Drive
->id_OldCylinders
);
1641 SWAP_LE_WORD(unit
->au_Drive
->id_SpecificConfig
);
1642 SWAP_LE_WORD(unit
->au_Drive
->id_OldHeads
);
1643 SWAP_LE_WORD(unit
->au_Drive
->id_OldSectors
);
1644 SWAP_LE_WORD(unit
->au_Drive
->id_RWMultipleSize
);
1645 SWAP_LE_WORD(unit
->au_Drive
->id_Capabilities
);
1646 SWAP_LE_WORD(unit
->au_Drive
->id_OldCaps
);
1647 SWAP_LE_WORD(unit
->au_Drive
->id_OldPIO
);
1648 SWAP_LE_WORD(unit
->au_Drive
->id_ConfigAvailable
);
1649 SWAP_LE_WORD(unit
->au_Drive
->id_OldLCylinders
);
1650 SWAP_LE_WORD(unit
->au_Drive
->id_OldLHeads
);
1651 SWAP_LE_WORD(unit
->au_Drive
->id_OldLSectors
);
1652 SWAP_LE_WORD(unit
->au_Drive
->id_RWMultipleTrans
);
1653 SWAP_LE_WORD(unit
->au_Drive
->id_MWDMASupport
);
1654 SWAP_LE_WORD(unit
->au_Drive
->id_PIOSupport
);
1655 SWAP_LE_WORD(unit
->au_Drive
->id_MWDMA_MinCycleTime
);
1656 SWAP_LE_WORD(unit
->au_Drive
->id_MWDMA_DefCycleTime
);
1657 SWAP_LE_WORD(unit
->au_Drive
->id_PIO_MinCycleTime
);
1658 SWAP_LE_WORD(unit
->au_Drive
->id_PIO_MinCycleTImeIORDY
);
1659 SWAP_LE_WORD(unit
->au_Drive
->id_QueueDepth
);
1660 SWAP_LE_WORD(unit
->au_Drive
->id_ATAVersion
);
1661 SWAP_LE_WORD(unit
->au_Drive
->id_ATARevision
);
1662 SWAP_LE_WORD(unit
->au_Drive
->id_Commands1
);
1663 SWAP_LE_WORD(unit
->au_Drive
->id_Commands2
);
1664 SWAP_LE_WORD(unit
->au_Drive
->id_Commands3
);
1665 SWAP_LE_WORD(unit
->au_Drive
->id_Commands4
);
1666 SWAP_LE_WORD(unit
->au_Drive
->id_Commands5
);
1667 SWAP_LE_WORD(unit
->au_Drive
->id_Commands6
);
1668 SWAP_LE_WORD(unit
->au_Drive
->id_UDMASupport
);
1669 SWAP_LE_WORD(unit
->au_Drive
->id_SecurityEraseTime
);
1670 SWAP_LE_WORD(unit
->au_Drive
->id_ESecurityEraseTime
);
1671 SWAP_LE_WORD(unit
->au_Drive
->id_CurrentAdvPowerMode
);
1672 SWAP_LE_WORD(unit
->au_Drive
->id_MasterPwdRevision
);
1673 SWAP_LE_WORD(unit
->au_Drive
->id_HWResetResult
);
1674 SWAP_LE_WORD(unit
->au_Drive
->id_AcousticManagement
);
1675 SWAP_LE_WORD(unit
->au_Drive
->id_StreamMinimunReqSize
);
1676 SWAP_LE_WORD(unit
->au_Drive
->id_StreamingTimeDMA
);
1677 SWAP_LE_WORD(unit
->au_Drive
->id_StreamingLatency
);
1678 SWAP_LE_WORD(unit
->au_Drive
->id_StreamingTimePIO
);
1679 SWAP_LE_WORD(unit
->au_Drive
->id_PhysSectorSize
);
1680 SWAP_LE_WORD(unit
->au_Drive
->id_RemMediaStatusNotificationFeatures
);
1681 SWAP_LE_WORD(unit
->au_Drive
->id_SecurityStatus
);
1683 SWAP_LE_LONG(unit
->au_Drive
->id_WordsPerLogicalSector
);
1684 SWAP_LE_LONG(unit
->au_Drive
->id_LBASectors
);
1685 SWAP_LE_LONG(unit
->au_Drive
->id_StreamingGranularity
);
1687 SWAP_LE_QUAD(unit
->au_Drive
->id_LBA48Sectors
);
1690 DUMP(dump(unit
->au_Drive
, sizeof(struct DriveIdent
)));
1692 unit
->au_SectorShift
= 9;
1693 unit
->au_DevType
= DG_DIRECT_ACCESS
;
1694 unit
->au_Read32
= ata_ReadSector32
;
1695 unit
->au_Write32
= ata_WriteSector32
;
1696 unit
->au_DirectSCSI
= atapi_DirectSCSI
;
1697 unit
->au_Eject
= ata_Eject
;
1698 unit
->au_XferModes
= 0;
1699 unit
->au_Flags
|= AF_DiscPresent
| AF_DiscChanged
;
1700 unit
->au_DevType
= DG_DIRECT_ACCESS
;
1702 ata_strcpy(unit
->au_Drive
->id_Model
, unit
->au_Model
, 40);
1703 ata_strcpy(unit
->au_Drive
->id_SerialNumber
, unit
->au_SerialNumber
, 20);
1704 ata_strcpy(unit
->au_Drive
->id_FirmwareRev
, unit
->au_FirmwareRev
, 8);
1706 bug("[ATA%02ld] ata_Identify: Unit info: %s / %s / %s\n", unit
->au_UnitNum
, unit
->au_Model
, unit
->au_SerialNumber
, unit
->au_FirmwareRev
);
1707 common_DetectXferModes(unit
);
1708 common_SetBestXferMode(unit
);
1710 if (unit
->au_Drive
->id_General
& 0x80)
1712 DINIT(bug("[ATA%02ld] ata_Identify: Device is removable.\n", unit
->au_UnitNum
));
1713 unit
->au_Flags
|= AF_Removable
;
1716 unit
->au_Capacity
= unit
->au_Drive
->id_LBASectors
;
1717 unit
->au_Capacity48
= unit
->au_Drive
->id_LBA48Sectors
;
1718 bug("[ATA%02ld] ata_Identify: Unit info: %07lx 28bit / %04lx:%08lx 48bit addressable blocks\n", unit
->au_UnitNum
, unit
->au_Capacity
, (ULONG
)(unit
->au_Capacity48
>> 32), (ULONG
)(unit
->au_Capacity48
& 0xfffffffful
));
1721 For drive capacities > 8.3GB assume maximal possible layout.
1722 It really doesn't matter here, as BIOS will not handle them in
1724 i guess this just solves that weirdo div-by-zero crash, if nothing else...
1726 if ((unit
->au_Drive
->id_LBA48Sectors
> (63 * 255 * 1024)) ||
1727 (unit
->au_Drive
->id_LBASectors
> (63 * 255 * 1024)))
1731 * TODO: this shouldn't be casted down here.
1733 ULONG sec
= unit
->au_Capacity48
;
1735 if (sec
< unit
->au_Capacity48
)
1738 if (sec
< unit
->au_Capacity
)
1739 sec
= unit
->au_Capacity
;
1741 unit
->au_Sectors
= 63;
1744 * keep dividing by 2
1748 if (((sec
>> 1) << 1) != sec
)
1750 if ((div
<< 1) > 255)
1758 if (((sec
/ 3) * 3) != sec
)
1760 if ((div
* 3) > 255)
1766 unit
->au_Cylinders
= sec
;
1767 unit
->au_Heads
= div
;
1771 unit
->au_Cylinders
= unit
->au_Drive
->id_OldLCylinders
;
1772 unit
->au_Heads
= unit
->au_Drive
->id_OldLHeads
;
1773 unit
->au_Sectors
= unit
->au_Drive
->id_OldLSectors
;
1780 * ata read32 commands
1782 static ULONG
ata_ReadSector32(struct ata_Unit
*unit
, ULONG block
, ULONG count
, APTR buffer
, ULONG
*act
)
1784 ata_CommandBlock acb
=
1793 count
<< unit
->au_SectorShift
,
1800 D(bug("[ATA%02ld] ata_ReadSector32()\n", unit
->au_UnitNum
));
1803 if (0 != (err
= ata_exec_blk(unit
, &acb
)))
1806 *act
= count
<< unit
->au_SectorShift
;
1810 static ULONG
ata_ReadMultiple32(struct ata_Unit
*unit
, ULONG block
, ULONG count
, APTR buffer
, ULONG
*act
)
1812 ata_CommandBlock acb
=
1816 unit
->au_Drive
->id_RWMultipleSize
& 0xff,
1821 count
<< unit
->au_SectorShift
,
1828 D(bug("[ATA%02ld] ata_ReadMultiple32()\n", unit
->au_UnitNum
));
1831 if (0 != (err
= ata_exec_blk(unit
, &acb
)))
1834 *act
= count
<< unit
->au_SectorShift
;
1838 static ULONG
ata_ReadDMA32(struct ata_Unit
*unit
, ULONG block
, ULONG count
, APTR buffer
, ULONG
*act
)
1841 ata_CommandBlock acb
=
1850 count
<< unit
->au_SectorShift
,
1856 D(bug("[ATA%02ld] ata_ReadDMA32()\n", unit
->au_UnitNum
));
1859 if (0 != (err
= ata_exec_blk(unit
, &acb
)))
1862 *act
= count
<< unit
->au_SectorShift
;
1868 * ata read64 commands
1870 static ULONG
ata_ReadSector64(struct ata_Unit
*unit
, UQUAD block
, ULONG count
, APTR buffer
, ULONG
*act
)
1872 ata_CommandBlock acb
=
1881 count
<< unit
->au_SectorShift
,
1886 register ULONG err
= 0;
1888 D(bug("[ATA%02ld] ata_ReadSector64()\n", unit
->au_UnitNum
));
1891 if (0 != (err
= ata_exec_blk(unit
, &acb
)))
1894 *act
= count
<< unit
->au_SectorShift
;
1898 static ULONG
ata_ReadMultiple64(struct ata_Unit
*unit
, UQUAD block
, ULONG count
, APTR buffer
, ULONG
*act
)
1900 ata_CommandBlock acb
=
1902 ATA_READ_MULTIPLE64
,
1904 unit
->au_Drive
->id_RWMultipleSize
& 0xff,
1909 count
<< unit
->au_SectorShift
,
1916 D(bug("[ATA%02ld] ata_ReadMultiple64()\n", unit
->au_UnitNum
));
1919 if (0 != (err
= ata_exec_blk(unit
, &acb
)))
1922 *act
= count
<< unit
->au_SectorShift
;
1926 static ULONG
ata_ReadDMA64(struct ata_Unit
*unit
, UQUAD block
, ULONG count
, APTR buffer
, ULONG
*act
)
1928 ata_CommandBlock acb
=
1937 count
<< unit
->au_SectorShift
,
1944 D(bug("[ATA%02ld] ata_ReadDMA64()\n", unit
->au_UnitNum
));
1947 if (0 != (err
= ata_exec_blk(unit
, &acb
)))
1950 *act
= count
<< unit
->au_SectorShift
;
1956 * ata write32 commands
1958 static ULONG
ata_WriteSector32(struct ata_Unit
*unit
, ULONG block
, ULONG count
, APTR buffer
, ULONG
*act
)
1960 ata_CommandBlock acb
=
1969 count
<< unit
->au_SectorShift
,
1976 D(bug("[ATA%02ld] ata_WriteSector32()\n", unit
->au_UnitNum
));
1979 if (0 != (err
= ata_exec_blk(unit
, &acb
)))
1982 *act
= count
<< unit
->au_SectorShift
;
1986 static ULONG
ata_WriteMultiple32(struct ata_Unit
*unit
, ULONG block
, ULONG count
, APTR buffer
, ULONG
*act
)
1988 ata_CommandBlock acb
=
1992 unit
->au_Drive
->id_RWMultipleSize
& 0xff,
1997 count
<< unit
->au_SectorShift
,
2004 D(bug("[ATA%02ld] ata_WriteMultiple32()\n", unit
->au_UnitNum
));
2007 if (0 != (err
= ata_exec_blk(unit
, &acb
)))
2010 *act
= count
<< unit
->au_SectorShift
;
2014 static ULONG
ata_WriteDMA32(struct ata_Unit
*unit
, ULONG block
, ULONG count
, APTR buffer
, ULONG
*act
)
2016 ata_CommandBlock acb
=
2025 count
<< unit
->au_SectorShift
,
2032 D(bug("[ATA%02ld] ata_WriteDMA32()\n", unit
->au_UnitNum
));
2035 if (0 != (err
= ata_exec_blk(unit
, &acb
)))
2038 *act
= count
<< unit
->au_SectorShift
;
2044 * ata write64 commands
2046 static ULONG
ata_WriteSector64(struct ata_Unit
*unit
, UQUAD block
, ULONG count
, APTR buffer
, ULONG
*act
)
2048 ata_CommandBlock acb
=
2057 count
<< unit
->au_SectorShift
,
2064 D(bug("[ATA%02ld] ata_WriteSector64()\n", unit
->au_UnitNum
));
2067 if (0 != (err
= ata_exec_blk(unit
, &acb
)))
2070 *act
= count
<< unit
->au_SectorShift
;
2074 static ULONG
ata_WriteMultiple64(struct ata_Unit
*unit
, UQUAD block
, ULONG count
, APTR buffer
, ULONG
*act
)
2076 ata_CommandBlock acb
=
2078 ATA_WRITE_MULTIPLE64
,
2080 unit
->au_Drive
->id_RWMultipleSize
& 0xff,
2085 count
<< unit
->au_SectorShift
,
2092 D(bug("[ATA%02ld] ata_WriteMultiple64()\n", unit
->au_UnitNum
));
2095 if (0 != (err
= ata_exec_blk(unit
, &acb
)))
2098 *act
= count
<< unit
->au_SectorShift
;
2102 static ULONG
ata_WriteDMA64(struct ata_Unit
*unit
, UQUAD block
, ULONG count
, APTR buffer
, ULONG
*act
)
2104 ata_CommandBlock acb
=
2113 count
<< unit
->au_SectorShift
,
2120 D(bug("[ATA%02ld] ata_WriteDMA64()\n", unit
->au_UnitNum
));
2123 if (0 != (err
= ata_exec_blk(unit
, &acb
)))
2126 *act
= count
<< unit
->au_SectorShift
;
2131 * ata miscellaneous commands
2133 static ULONG
ata_Eject(struct ata_Unit
*unit
)
2135 ata_CommandBlock acb
=
2150 D(bug("[ATA%02ld] ata_Eject()\n", unit
->au_UnitNum
));
2152 return ata_exec_cmd(unit
, &acb
);
2158 int atapi_TestUnitOK(struct ata_Unit
*unit
)
2166 struct SCSICmd sc
= {
2170 D(bug("[ATA%02ld] atapi_TestUnitOK()\n", unit
->au_UnitNum
));
2172 sc
.scsi_Command
= (void*) &cmd
;
2173 sc
.scsi_CmdLength
= sizeof(cmd
);
2174 sc
.scsi_SenseData
= (void*)&sense
;
2175 sc
.scsi_SenseLength
= sizeof(sense
);
2176 sc
.scsi_Flags
= SCSIF_AUTOSENSE
;
2178 DATAPI(bug("[ATA%02ld] atapi_TestUnitOK: Testing Unit Ready sense...\n", unit
->au_UnitNum
));
2179 unit
->au_DirectSCSI(unit
, &sc
);
2180 unit
->au_SenseKey
= sense
[2];
2183 * we may have just lost the disc...?
2186 * per MMC, drives are expected to return 02-3a-0# status, when disc is not present
2187 * that would translate into following code:
2188 * int p1 = ((sense[2] == 2) && (sense[12] == 0x3a)) ? 1 : 0;
2189 * unfortunately, it's what MMC says, not what vendors code.
2191 int p1
= (sense
[2] == 2) ? 1 : 0;
2192 int p2
= (0 != (AF_DiscPresent
& unit
->au_Flags
)) ? 1 : 0;
2196 //unit->au_Flags ^= AF_DiscPresent;
2198 unit
->au_Flags
|= AF_DiscPresent
;
2200 unit
->au_Flags
&= ~AF_DiscPresent
;
2202 unit
->au_Flags
|= AF_DiscChanged
;
2205 DATAPI(bug("[ATA%02ld] atapi_TestUnitOK: Test Unit Ready sense: %02lx, Media %s\n", unit
->au_UnitNum
, sense
[2], unit
->au_Flags
& AF_DiscPresent
? "PRESENT" : "ABSENT"));
2209 static ULONG
atapi_Read(struct ata_Unit
*unit
, ULONG block
, ULONG count
, APTR buffer
, ULONG
*act
)
2212 SCSI_READ10
, 0, block
>>24, block
>>16, block
>>8, block
, 0, count
>>8, count
, 0
2214 struct SCSICmd sc
= {
2218 D(bug("[ATA%02ld] atapi_Read()\n", unit
->au_UnitNum
));
2220 sc
.scsi_Command
= (void*) &cmd
;
2221 sc
.scsi_CmdLength
= sizeof(cmd
);
2222 sc
.scsi_Data
= buffer
;
2223 sc
.scsi_Length
= count
<< unit
->au_SectorShift
;
2224 sc
.scsi_Flags
= SCSIF_READ
;
2226 return unit
->au_DirectSCSI(unit
, &sc
);
2229 static ULONG
atapi_Write(struct ata_Unit
*unit
, ULONG block
, ULONG count
, APTR buffer
, ULONG
*act
)
2232 SCSI_WRITE10
, 0, block
>>24, block
>>16, block
>>8, block
, 0, count
>>8, count
, 0
2234 struct SCSICmd sc
= {
2238 D(bug("[ATA%02ld] atapi_Write()\n", unit
->au_UnitNum
));
2240 sc
.scsi_Command
= (void*) &cmd
;
2241 sc
.scsi_CmdLength
= sizeof(cmd
);
2242 sc
.scsi_Data
= buffer
;
2243 sc
.scsi_Length
= count
<< unit
->au_SectorShift
;
2244 sc
.scsi_Flags
= SCSIF_WRITE
;
2246 return unit
->au_DirectSCSI(unit
, &sc
);
2249 static ULONG
atapi_Eject(struct ata_Unit
*unit
)
2251 struct atapi_StartStop cmd
= {
2252 command
: SCSI_STARTSTOP
,
2254 flags
: ATAPI_SS_EJECT
,
2257 struct SCSICmd sc
= {
2261 D(bug("[ATA%02ld] atapi_Eject()\n", unit
->au_UnitNum
));
2263 sc
.scsi_Command
= (void*) &cmd
;
2264 sc
.scsi_CmdLength
= sizeof(cmd
);
2265 sc
.scsi_Flags
= SCSIF_READ
;
2267 return unit
->au_DirectSCSI(unit
, &sc
);
2270 ULONG
atapi_RequestSense(struct ata_Unit
* unit
, UBYTE
* sense
, ULONG senselen
)
2273 3, 0, 0, 0, senselen
& 0xfe, 0
2275 struct SCSICmd sc
= {
2279 D(bug("[ATA%02ld] atapi_RequestSense()\n", unit
->au_UnitNum
));
2281 if ((senselen
== 0) || (sense
== 0))
2285 sc
.scsi_Data
= (void*)sense
;
2286 sc
.scsi_Length
= senselen
& 0xfe;
2287 sc
.scsi_Command
= (void*)&cmd
;
2288 sc
.scsi_CmdLength
= 6;
2289 sc
.scsi_Flags
= SCSIF_READ
;
2291 unit
->au_DirectSCSI(unit
, &sc
);
2293 DATAPI(dump(sense
, senselen
));
2294 DATAPI(bug("[SENSE] atapi_RequestSense: sensed data: %lx %lx %lx\n", sense
[2]&0xf, sense
[12], sense
[13]));
2295 return ((sense
[2]&0xf)<<16) | (sense
[12]<<8) | (sense
[13]);
2298 ULONG
ata_ReadSignature(struct ata_Bus
*bus
, int unit
)
2300 ULONG port
= bus
->ab_Port
;
2303 D(bug("[ATA ] ata_ReadSignature(%02ld)\n", unit
));
2305 ata_out(0xa0 | (unit
<< 4), ata_DevHead
, port
);
2307 //ata_WaitTO(bus->ab_Timer, 0, 1, 0);
2309 /* Check basic signature. All live devices should provide it */
2310 tmp1
= ata_in(ata_Count
, port
);
2311 tmp2
= ata_in(ata_LBALow
, port
);
2312 DINIT(bug("[ATA ] ata_ReadSignature: Checking Count / LBA against expected values (%d:%d)\n", tmp1
, tmp2
));
2314 DINIT(bug("[ATA ] ata_ReadSignature: Status %08lx Device %08lx\n", ata_in(ata_Status
, port
), ata_in(ata_DevHead
, port
)));
2316 if ((tmp1
== 0x01) && (tmp2
== 0x01))
2318 /* Ok, ATA/ATAPI device. Get detailed signature */
2319 DINIT(bug("[ATA ] ata_ReadSignature: Found an ATA[PI] Device. Attempting to detect specific subtype\n"));
2321 tmp1
= ata_in(ata_LBAMid
, port
);
2322 tmp2
= ata_in(ata_LBAHigh
, port
);
2324 DINIT(bug("[ATA ] ata_ReadSignature: Subtype check returned %02lx:%02lx (%04lx)\n", tmp1
, tmp2
, (tmp1
<< 8) | tmp2
));
2326 switch ((tmp1
<< 8) | tmp2
)
2329 if (0 == (ata_ReadStatus(bus
) & 0xfe))
2331 ata_out(ATA_EXECUTE_DIAG
, ata_Command
, port
);
2333 ata_WaitTO(bus
->ab_Timer
, 0, 2000, 0);
2334 while (ata_ReadStatus(bus
) & ATAF_BUSY
)
2336 //ata_WaitTO(bus->ab_Timer, 0, 1, 0);
2338 ata_out(0xa0 | (unit
<< 4), ata_DevHead
, port
);
2342 //ata_WaitTO(unit->au_Bus->ab_Timer, 0, 1, 0);
2344 while (0 != (ATAF_BUSY
& ata_ReadStatus(bus
)));
2345 DINIT(bug("[ATA ] ata_ReadSignature: Further validating ATA signature: %lx & 0x7f = 1, %lx & 0x10 = unit\n", ata_in(ata_Error
, port
), ata_in(ata_DevHead
, port
)));
2347 if ((ata_in(ata_Error
, port
) & 0x7f) == 1)
2349 DINIT(bug("[ATA ] ata_ReadSignature: Found *valid* signature for ATA device\n"));
2352 bug("[ATA ] ata_ReadSignature: Found signature for ATA "
2353 "device, but further validation failed\n");
2357 DINIT(bug("[ATA ] ata_ReadSignature: Found signature for ATAPI device\n"));
2361 DINIT(bug("[ATA ] ata_ReadSignature: Found signature for SATA device\n"));
2365 DINIT(bug("[ATA ] ata_ReadSignature: Found signature for SATAPI device\n"));
2369 if (((tmp1
| tmp2
) == 0xff) &&
2370 ((tmp1
& tmp2
) == 0x00))
2372 bug("[ATA ] ata_ReadSignature: Found valid subtype, but don't know how to handle this device: %02lx %02lx\n", tmp1
, tmp2
);
2376 bug("[ATA ] ata_ReadSignature: Invalid signature: %02lx %02lx\n", tmp1
, tmp2
);
2385 void ata_ResetBus(struct ata_Bus
*bus
)
2388 ULONG alt
= bus
->ab_Alt
;
2389 ULONG port
= bus
->ab_Port
;
2392 /* Set and then reset the soft reset bit in the Device Control
2393 * register. This causes device 0 be selected */
2394 D(bug("[ATA ] ata_ResetBus(%d)\n", bus
->ab_BusNum
));
2395 ata_out(0xa0 | (0 << 4), ata_DevHead
, port
); /* Select it never the less */
2397 //ata_WaitTO(bus->ab_Timer, 0, 1, 0);
2399 ata_out(0x04, ata_AltControl
, alt
);
2400 ata_WaitTO(bus
->ab_Timer
, 0, 10, 0); /* sleep 10us; min: 5us */
2401 ata_out(0x02, ata_AltControl
, alt
);
2402 ata_WaitTO(bus
->ab_Timer
, 0, 20000, 0); /* sleep 20ms; min: 2ms */
2404 /* If there is a device 0, wait for device 0 to clear BSY */
2405 if (DEV_NONE
!= bus
->ab_Dev
[0]) {
2406 D(bug("[ATA%02ld] ata_ResetBus: Wait for Device to clear BSY\n", ((bus
->ab_BusNum
<< 1 ) + 0)));
2407 TimeOut
= 1000; /* Timeout 1s (1ms x 1000) */
2409 if( (ata_ReadStatus(bus
) & ATAF_BUSY
) == 0 )
2411 ata_WaitTO(bus
->ab_Timer
, 0, 1000, 0);
2413 D(bug("[ATA%02ld] ata_ResetBus: Device Timed Out!\n", ((bus
->ab_BusNum
<< 1 ) + 0)));
2414 bus
->ab_Dev
[0] = DEV_NONE
;
2418 D(bug("[ATA%02ld] ata_ResetBus: Wait left after %d ms\n", ((bus
->ab_BusNum
<< 1 ) + 0), (1000 - TimeOut
)));
2421 /* If there is a device 1, wait until device 1 allows
2422 * register access */
2423 if (DEV_NONE
!= bus
->ab_Dev
[1]) {
2424 D(bug("[ATA ] ata_ResetBus: Wait DEV1 to allow access\n"));
2425 ata_out(0xa0 | (1 << 4), ata_DevHead
, port
);
2427 //ata_WaitTO(bus->ab_Timer, 0, 1, 0);
2428 TimeOut
= 1000; /* Timeout 1s (1ms x 1000) */
2430 if ( (ata_in(2, port
) == 0x01) && (ata_in(3, port
) == 0x01) )
2432 ata_WaitTO(bus
->ab_Timer
, 0, 1000, 0);
2434 D(bug("[ATA ] ata_ResetBus: DEV1 1/2 TimeOut!\n"));
2435 bus
->ab_Dev
[1] = DEV_NONE
;
2439 D(bug("[ATA ] ata_ResetBus: DEV1 1/2 Wait left after %d ms\n", (1000 - TimeOut
)));
2441 if (DEV_NONE
!= bus
->ab_Dev
[1]) {
2442 D(bug("[ATA%02ld] ata_ResetBus: Wait for Device to clear BSY\n", ((bus
->ab_BusNum
<< 1 ) + 1)));
2443 TimeOut
= 1000; /* Timeout 1s (1ms x 1000) */
2445 if( (ata_ReadStatus(bus
) & ATAF_BUSY
) == 0 )
2447 ata_WaitTO(bus
->ab_Timer
, 0, 1000, 0);
2449 D(bug("[ATA%02ld] ata_ResetBus: Device Timed Out!\n", ((bus
->ab_BusNum
<< 1 ) + 1)));
2450 bus
->ab_Dev
[1] = DEV_NONE
;
2454 D(bug("[ATA%02ld] ata_ResetBus: Wait left after %d ms\n", ((bus
->ab_BusNum
<< 1 ) + 1), 1000 - TimeOut
));
2458 if (DEV_NONE
!= bus
->ab_Dev
[0])
2459 bus
->ab_Dev
[0] = ata_ReadSignature(bus
, 0);
2460 if (DEV_NONE
!= bus
->ab_Dev
[1])
2461 bus
->ab_Dev
[1] = ata_ReadSignature(bus
, 1);
2464 void ata_InitBus(struct ata_Bus
*bus
)
2466 ULONG port
= bus
->ab_Port
;
2470 * initialize timer for the sake of scanning
2472 bus
->ab_Timer
= ata_OpenTimer();
2474 D(bug("[ATA ] ata_InitBus(%d)\n", bus
->ab_BusNum
));
2476 bus
->ab_Dev
[0] = DEV_NONE
;
2477 bus
->ab_Dev
[1] = DEV_NONE
;
2479 /* Select device 0 and disable IRQs */
2480 ata_out(0xa0, ata_DevHead
, port
);
2481 ata_WaitTO(bus
->ab_Timer
, 0, 100, 0);
2482 ata_out(0x2, ata_AltControl
, bus
->ab_Alt
);
2484 /* Write some pattern to registers */
2485 ata_out(0x55, ata_Count
, port
);
2486 ata_out(0xaa, ata_LBALow
, port
);
2487 ata_out(0xaa, ata_Count
, port
);
2488 ata_out(0x55, ata_LBALow
, port
);
2489 ata_out(0x55, ata_Count
, port
);
2490 ata_out(0xaa, ata_LBALow
, port
);
2492 tmp1
= ata_in(ata_Count
, port
);
2493 tmp2
= ata_in(ata_LBALow
, port
);
2495 if ((tmp1
== 0x55) && (tmp2
== 0xaa))
2496 bus
->ab_Dev
[0] = DEV_UNKNOWN
;
2497 D(bug("[ATA%02ld] ata_InitBus: Device type = %x\n", (bus
->ab_BusNum
<< 1 ) + 0, bus
->ab_Dev
[0]));
2499 /* Select device 1 and disable IRQs */
2500 ata_out(0xb0, ata_DevHead
, port
);
2501 ata_WaitTO(bus
->ab_Timer
, 0, 100, 0);
2502 ata_out(0x2, ata_AltControl
, bus
->ab_Alt
);
2504 /* Write some pattern to registers */
2505 ata_out(0x55, ata_Count
, port
);
2506 ata_out(0xaa, ata_LBALow
, port
);
2507 ata_out(0xaa, ata_Count
, port
);
2508 ata_out(0x55, ata_LBALow
, port
);
2509 ata_out(0x55, ata_Count
, port
);
2510 ata_out(0xaa, ata_LBALow
, port
);
2512 tmp1
= ata_in(ata_Count
, port
);
2513 tmp2
= ata_in(ata_LBALow
, port
);
2515 if ((tmp1
== 0x55) && (tmp2
== 0xaa))
2516 bus
->ab_Dev
[1] = DEV_UNKNOWN
;
2517 D(bug("[ATA%02ld] ata_InitBus: Device type = %x\n", (bus
->ab_BusNum
<< 1 ) + 1, bus
->ab_Dev
[1]));
2520 ata_CloseTimer(bus
->ab_Timer
);
2521 D(bug("[ATA ] ata_InitBus: Finished\n"));
2525 * not really sure what this is meant to be - TO BE REPLACED
2527 static const ULONG ErrorMap
[] = {
2546 static ULONG
atapi_EndCmd(struct ata_Unit
*unit
)
2550 DATAPI(bug("[ATA%02ld] atapi_EndCmd()\n", unit
->au_UnitNum
));
2553 * read alternate status register (per specs)
2555 status
= ata_in(ata_AltStatus
, unit
->au_Bus
->ab_Alt
);
2556 DATAPI(bug("[ATA%02ld] atapi_EndCmd: Alternate status: %lx\n", unit
->au_UnitNum
, status
));
2558 status
= ata_in(atapi_Status
, unit
->au_Bus
->ab_Port
);
2560 DATAPI(bug("[ATAPI] atapi_EndCmd: Command complete. Status: %lx\n", unit
->au_UnitNum
, status
));
2562 if (!(status
& ATAPIF_CHECK
))
2568 status
= ata_in(atapi_Error
, unit
->au_Bus
->ab_Port
);
2569 DATAPI(bug("[ATA%02ld] atapi_EndCmd: Error code 0x%lx\n", unit
->au_UnitNum
, status
>> 4));
2570 return ErrorMap
[status
>> 4];
2575 * vim: ts=4 et sw=4 fdm=marker fmr={,}