2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright (c) 2003 Marcus R. Brown <mrbrown@0xd6.org>
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
10 # $Id: ps2atad.c 1455 2007-11-04 23:46:27Z roman_ps2dev $
12 # This module provides the low-level ATA support for hard disk drives. It is
13 # 100% compatible with its proprietary counterpart called atad.irx.
15 # This module also include support for 48-bit feature set (done by Clement).
29 #include "speedregs.h"
32 #define MODNAME "atad_driver"
33 IRX_ID(MODNAME
, 1, 1);
35 #define M_PRINTF(format, args...) \
36 printf(MODNAME ": " format, ## args)
38 #define BANNER "ATA device driver %s - Copyright (c) 2003 Marcus R. Brown\n"
39 #define VERSION "v1.1"
41 static int ata_devinfo_init
= 0;
42 static int ata_evflg
= -1;
44 /* Used for indicating 48-bit LBA support. */
45 static int lba_48bit
[2] = {0, 0};
47 /* This is true if this is a SCEI-modified HDD. */
48 static int is_sony_hdd
[2] = {0, 0};
50 /* Local device info kept for drives 0 and 1. */
51 static ata_devinfo_t atad_devinfo
[2];
53 /* Data returned from DEVICE IDENTIFY is kept here. Also, this is used by the
54 security commands to set and unlock the password. */
55 static u16 ata_param
[256];
57 /* ATA command info. */
58 typedef struct _ata_cmd_info
{
63 static ata_cmd_info_t ata_cmd_table
[] = {
64 {0,1},{3,1},{8,5},{0x20,2},{0x30,3},{0x32,8},{0x38,3},{0x40,1},{0x70,1},
65 {0x87,2},{0x8e,7},{0x90,6},{0x91,1},{0x92,3},{0xa1,2},{0xb0,7},{0xc0,1},
66 {0xc4,2},{0xc5,3},{0xc6,1},{0xc8,4},{0xca,4},{0xcd,3},{0xda,1},{0xde,1},
67 {0xdf,1},{0xe0,1},{0xe1,1},{0xe2,1},{0xe3,1},{0xe4,2},{0xe5,1},{0xe6,1},
68 {0xe7,1},{0xe8,3},{0xec,2},{0xed,1},{0xef,1},{0xf1,3},{0xf2,3},{0xf3,1},
69 {0xf4,3},{0xf5,1},{0xf6,3},{0xf8,1},{0xf9,1},{0x25,4},{0x35,4},{0xea,1}
71 #define ATA_CMD_TABLE_SIZE (sizeof ata_cmd_table/sizeof(ata_cmd_info_t))
73 static ata_cmd_info_t sec_ctrl_cmd_table
[] = {
74 {0xec,2},{0xf3,1},{0xf4,1},{0xf5,1},{0xf1,3},{0xf2,3},{0x30,3},{0x20,2}
76 #define SEC_CTRL_CMD_TABLE_SIZE (sizeof sec_ctrl_cmd_table/sizeof(ata_cmd_info_t))
78 static ata_cmd_info_t smart_cmd_table
[] = {
79 {0xd0,2},{0xd2,1},{0xd3,1},{0xd4,1},{0xd5,2},{0xd6,3},{0xd8,1},{0xd9,1},
82 #define SMART_CMD_TABLE_SIZE (sizeof smart_cmd_table/sizeof(ata_cmd_info_t))
84 /* This is the state info tracked between ata_io_start() and ata_io_finish(). */
85 typedef struct _ata_cmd_state
{
86 int type
; /* The ata_cmd_info_t type field. */
88 u32 blkcount
; /* The number of 512-byte blocks (sectors) to transfer. */
89 int dir
; /* DMA direction: 0 - to RAM, 1 - from RAM. */
92 static ata_cmd_state_t atad_cmd_state
;
94 static int ata_intr_cb(int flag
);
95 static u32
ata_alarm_cb(void *unused
);
97 static void ata_dma_set_dir(int dir
);
99 static void ata_pio_mode(int mode
);
100 static void ata_multiword_dma_mode(int mode
);
101 static void ata_ultra_dma_mode(int mode
);
103 struct irx_export_table _exp_atad
;
105 int _start(int argc
, char *argv
[])
111 printf(BANNER
, VERSION
);
113 if (!(SPD_REG16(SPD_R_REV_3
) & SPD_CAPS_ATA
) || !(SPD_REG16(SPD_R_REV_8
) & 0x02)) {
114 M_PRINTF("HDD is not connected, exiting.\n");
118 if ((res
= RegisterLibraryEntries(&_exp_atad
)) != 0) {
119 M_PRINTF("Library is already registered, exiting.\n");
125 if ((ata_evflg
= CreateEventFlag(&event
)) < 0) {
126 M_PRINTF("Couldn't create event flag, exiting.\n");
133 dev9RegisterIntrCb(1, ata_intr_cb
);
134 dev9RegisterIntrCb(0, ata_intr_cb
);
137 M_PRINTF("Driver loaded.\n");
142 int __attribute__((unused
)) shutdown() { return 0; }
144 static int ata_intr_cb(int flag
)
146 if (flag
== 1) { /* New card, invalidate device info. */
147 memset(atad_devinfo
, 0, sizeof atad_devinfo
);
148 is_sony_hdd
[0] = is_sony_hdd
[1] = 0;
150 dev9IntrDisable(SPD_INTR_ATA
);
151 iSetEventFlag(ata_evflg
, 0x02);
157 static u32
ata_alarm_cb(void *unused
)
159 iSetEventFlag(ata_evflg
, 0x01);
167 return ata_hwport
->r_error
& 0xff;
170 /* 0x80 for busy, 0x88 for bus busy. */
171 static int ata_wait_busy(int bits
)
176 for (i
= 0; i
< 80; i
++) {
177 if (!(ata_hwport
->r_control
& bits
))
203 M_PRINTF("Timeout while waiting on busy (0x%02x).\n", bits
);
207 static int ata_device_select(int device
)
212 if ((res
= ata_wait_busy(0x88)) < 0)
215 /* If the device was already selected, nothing to do. */
216 if (((ata_hwport
->r_select
>> 4) & 1) == device
)
219 /* Select the device. */
220 ata_hwport
->r_select
= (device
& 1) << 4;
221 res
= ata_hwport
->r_control
;
223 return ata_wait_busy(0x88);
227 int ata_io_start(void *buf
, u32 blkcount
, u16 feature
, u16 nsector
, u16 sector
,
228 u16 lcyl
, u16 hcyl
, u16 select
, u16 command
)
232 iop_sys_clock_t cmd_timeout
;
233 ata_cmd_info_t
*cmd_table
;
234 int i
, res
, type
, cmd_table_size
;
235 int using_timeout
, device
= (select
>> 4) & 1;
238 ClearEventFlag(ata_evflg
, 0);
240 if (!atad_devinfo
[device
].exists
)
243 if ((res
= ata_device_select(device
)) != 0)
246 /* For the SCE and SMART commands, we need to search on the subcommand
247 specified in the feature register. */
248 if (command
== ATA_C_SCE_SEC_CONTROL
) {
249 cmd_table
= sec_ctrl_cmd_table
;
250 cmd_table_size
= SEC_CTRL_CMD_TABLE_SIZE
;
252 } else if (command
== ATA_C_SMART
) {
253 cmd_table
= smart_cmd_table
;
254 cmd_table_size
= SMART_CMD_TABLE_SIZE
;
257 cmd_table
= ata_cmd_table
;
258 cmd_table_size
= ATA_CMD_TABLE_SIZE
;
259 searchcmd
= command
& 0xff;
263 for (i
= 0; i
< cmd_table_size
; i
++) {
264 if (searchcmd
== cmd_table
[i
].command
) {
265 type
= cmd_table
[i
].type
;
270 if (!(atad_cmd_state
.type
= type
))
273 atad_cmd_state
.buf
= buf
;
274 atad_cmd_state
.blkcount
= blkcount
;
276 /* Check that the device is ready if this the appropiate command. */
277 if (!(ata_hwport
->r_control
& 0x40)) {
286 M_PRINTF("Error: Device %d is not ready.\n", device
);
291 /* Does this command need a timeout? */
299 /* Modified to include ATA_C_READ_DMA_EXT. */
300 atad_cmd_state
.dir
= ((command
!= 0xc8) && (command
!= 0x25));
305 cmd_timeout
.lo
= 0x41eb0000;
308 /* SECURITY ERASE UNIT needs a bit more time. */
309 if ((command
== 0xf4) || (command
== 0x8e && feature
== 0xf4))
310 USec2SysClock(180000000, &cmd_timeout
);
312 if ((res
= SetAlarm(&cmd_timeout
, (void *)ata_alarm_cb
, NULL
)) < 0)
316 /* Enable the command completion interrupt. */
318 dev9IntrEnable(SPD_INTR_ATA0
);
320 /* Finally! We send off the ATA command with arguments. */
321 ata_hwport
->r_control
= (using_timeout
== 0) << 1;
323 /* 48-bit LBA requires writing to the address registers twice,
324 24 bits of the LBA address is written each time.
325 Writing to registers twice does not affect 28-bit LBA since
326 only the latest data stored in address registers is used. */
327 ata_hwport
->r_feature
= (feature
>> 8) & 0xff;
328 ata_hwport
->r_nsector
= (nsector
>> 8) & 0xff;
329 ata_hwport
->r_sector
= (sector
>> 8) & 0xff;
330 ata_hwport
->r_lcyl
= (lcyl
>> 8) & 0xff;
331 ata_hwport
->r_hcyl
= (hcyl
>> 8) & 0xff;
333 ata_hwport
->r_feature
= feature
& 0xff;
334 ata_hwport
->r_nsector
= nsector
& 0xff;
335 ata_hwport
->r_sector
= sector
& 0xff;
336 ata_hwport
->r_lcyl
= lcyl
& 0xff;
337 ata_hwport
->r_hcyl
= hcyl
& 0xff;
338 ata_hwport
->r_select
= select
& 0xff;
339 ata_hwport
->r_command
= command
& 0xff;
341 /* Turn on the LED. */
342 SPD_REG8(SPD_R_PIO_DIR
) = 1;
343 SPD_REG8(SPD_R_PIO_DATA
) = 0;
348 /* Do a PIO transfer, to or from the device. */
349 static int ata_pio_transfer(ata_cmd_state_t
*cmd_state
)
354 u16 status
= ata_hwport
->r_status
& 0xff;
356 if (status
& ATA_STAT_ERR
) {
357 M_PRINTF("Error: Command error: status 0x%02x, error 0x%02x.\n",
358 status
, ata_get_error());
362 /* DRQ must be set (data request). */
363 if (!(status
& ATA_STAT_DRQ
))
366 type
= cmd_state
->type
;
368 if (type
== 3 || type
== 8) {
370 buf
= cmd_state
->buf
;
371 for (i
= 0; i
< 256; i
++) {
372 ata_hwport
->r_data
= *(u16
*)buf
;
373 cmd_state
->buf
= ++((u16
*)buf
);
375 if (cmd_state
->type
== 8) {
376 for (i
= 0; i
< 4; i
++) {
377 ata_hwport
->r_data
= *(u8
*)buf
;
378 cmd_state
->buf
= ++((u8
*)buf
);
381 } else if (type
== 2) {
383 buf
= cmd_state
->buf
;
384 for (i
= 0; i
< 256; i
++) {
385 *(u16
*)buf
= ata_hwport
->r_data
;
386 cmd_state
->buf
= ++((u16
*)buf
);
393 /* Complete a DMA transfer, to or from the device. */
394 static int ata_dma_complete(void *buf
, int blkcount
, int dir
)
398 u32 bits
, count
, nbytes
;
403 for (i
= 0; i
< 20; i
++)
404 if ((dma_stat
= SPD_REG16(0x38) & 0x1f))
410 dev9IntrEnable(SPD_INTR_ATA
);
411 /* Wait for the previous transfer to complete or a timeout. */
412 WaitEventFlag(ata_evflg
, 0x03, 0x11, &bits
);
414 if (bits
& 0x01) { /* Timeout. */
415 M_PRINTF("Error: DMA timeout.\n");
418 /* No DMA completion bit? Spurious interrupt. */
419 if (!(SPD_REG16(SPD_R_INTR_STAT
) & 0x02)) {
420 if (ata_hwport
->r_control
& 0x01) {
421 M_PRINTF("Error: Command error while doing DMA.\n");
422 M_PRINTF("Error: Command error status 0x%02x, error 0x%02x.\n",
423 ata_hwport
->r_status
, ata_get_error());
426 M_PRINTF("Warning: Got command interrupt, but not an error.\n");
431 dma_stat
= SPD_REG16(0x38) & 0x1f;
434 count
= (blkcount
< dma_stat
) ? blkcount
: dma_stat
;
435 nbytes
= count
* 512;
436 if ((res
= dev9DmaTransfer(0, buf
, (nbytes
<< 9)|32, dir
)) < 0)
451 ata_cmd_state_t
*cmd_state
= &atad_cmd_state
;
453 int i
, res
= 0, type
= cmd_state
->type
;
456 if (type
== 1 || type
== 6) { /* Non-data commands. */
457 WaitEventFlag(ata_evflg
, 0x03, 0x11, &bits
);
458 if (bits
& 0x01) { /* Timeout. */
459 M_PRINTF("Error: ATA timeout on a non-data command.\n");
462 } else if (type
== 4) { /* DMA. */
463 if ((res
= ata_dma_complete(cmd_state
->buf
, cmd_state
->blkcount
,
464 cmd_state
->dir
)) < 0)
467 for (i
= 0; i
< 100; i
++)
468 if ((stat
= SPD_REG16(SPD_R_INTR_STAT
) & 0x01))
471 dev9IntrEnable(SPD_INTR_ATA0
);
472 WaitEventFlag(ata_evflg
, 0x03, 0x11, &bits
);
474 M_PRINTF("Error: ATA timeout on DMA completion.\n");
478 } else { /* PIO transfers. */
479 stat
= ata_hwport
->r_control
;
480 if ((res
= ata_wait_busy(0x80)) < 0)
483 /* Transfer each PIO data block. */
484 while (--cmd_state
->blkcount
!= -1) {
485 if ((res
= ata_pio_transfer(cmd_state
)) < 0)
487 if ((res
= ata_wait_busy(0x80)) < 0)
495 /* Wait until the device isn't busy. */
496 if (ata_hwport
->r_status
& ATA_STAT_BUSY
)
497 res
= ata_wait_busy(0x80);
498 if ((stat
= ata_hwport
->r_status
) & ATA_STAT_ERR
) {
499 M_PRINTF("Error: Command error: status 0x%02x, error 0x%02x.\n",
500 stat
, ata_get_error());
505 /* The command has completed (with an error or not), so clean things up. */
506 CancelAlarm((void *)ata_alarm_cb
, NULL
);
507 /* Turn off the LED. */
508 SPD_REG8(SPD_R_PIO_DIR
) = 1;
509 SPD_REG8(SPD_R_PIO_DATA
) = 1;
514 /* Reset the ATA controller/bus. */
515 static int ata_bus_reset()
518 SPD_REG16(SPD_R_IF_CTRL
) = SPD_IF_ATA_RESET
;
520 SPD_REG16(SPD_R_IF_CTRL
) = 0x48;
522 return ata_wait_busy(0x80);
526 /* Not sure if it's reset, but it disables ATA interrupts. */
527 int ata_reset_devices()
531 if (ata_hwport
->r_control
& 0x80)
534 /* Dunno what this does. */
535 ata_hwport
->r_control
= 6;
538 /* Disable ATA interrupts. */
539 ata_hwport
->r_control
= 2;
542 return ata_wait_busy(0x80);
546 int ata_device_flush_cache(int device
)
550 if (lba_48bit
[device
]) {
551 res
= ata_io_start(NULL
, 1, 0, 0, 0, 0, 0, (device
<< 4) & 0xffff,
552 ATA_C_FLUSH_CACHE_EXT
);
554 res
= ata_io_start(NULL
, 1, 0, 0, 0, 0, 0, (device
<< 4) & 0xffff,
558 return ata_io_finish();
563 int ata_device_idle(int device
, int period
)
567 res
= ata_io_start(NULL
, 1, 0, period
& 0xff, 0, 0, 0,
568 (device
<< 4) & 0xffff, ATA_C_IDLE
);
570 return ata_io_finish();
574 static int ata_device_identify(int device
, void *info
)
578 res
= ata_io_start(info
, 1, 0, 0, 0, 0, 0, (device
<< 4) & 0xffff,
579 ATA_C_IDENTIFY_DEVICE
);
581 return ata_io_finish();
585 static int ata_device_pkt_identify(int device
, void *info
)
589 res
= ata_io_start(info
, 1, 0, 0, 0, 0, 0, (device
<< 4) & 0xffff,
590 ATA_C_IDENTIFY_PKT_DEVICE
);
592 return ata_io_finish();
597 /* I assume 0xec is either init/identify. */
598 int ata_device_sce_security_init(int device
, void *data
)
602 res
= ata_io_start(data
, 1, 0xec, 0, 0, 0, 0,
603 (device
<< 4) & 0xffff, ATA_C_SCE_SEC_CONTROL
);
605 return ata_io_finish();
609 static int ata_device_smart_enable(int device
)
613 res
= ata_io_start(NULL
, 1, ATA_C_SMART_ENABLE
, 0, 0, 0x4f, 0xc2,
614 (device
<< 4) & 0xffff, ATA_C_SMART
);
616 return ata_io_finish();
621 int ata_device_smart_save_attr(int device
)
625 res
= ata_io_start(NULL
, 1, ATA_C_SMART_SAVE_ATTR
, 0, 0, 0x4f, 0xc2,
626 (device
<< 4) & 0xffff, ATA_C_SMART
);
628 return ata_io_finish();
633 int ata_device_smart_get_status(int device
)
638 res
= ata_io_start(NULL
, 1, ATA_C_SMART_GET_STATUS
, 0, 0, 0x4f, 0xc2,
639 (device
<< 4) & 0xffff, ATA_C_SMART
);
643 res
= ata_io_finish();
647 /* Check to see if the report exceeded the threshold. */
648 if ((ata_hwport
->r_lcyl
!= 0x4f) || (ata_hwport
->r_hcyl
!= 0xc2)) {
649 M_PRINTF("Error: SMART report exceeded threshold.\n");
656 /* Set features - set transfer mode. */
657 int ata_device_set_transfer_mode(int device
, int type
, int mode
)
661 res
= ata_io_start(NULL
, 1, 3, (type
|mode
) & 0xff, 0, 0, 0,
662 (device
<< 4) & 0xffff, ATA_C_SET_FEATURES
);
666 res
= ata_io_finish();
670 if (type
== 0x20) /* Multiword DMA. */
671 ata_multiword_dma_mode(mode
);
672 else if (type
== 0x40) /* Ultra DMA. */
673 ata_ultra_dma_mode(mode
);
674 else if (type
== 0x08) /* PIO (flow control). */
681 int ata_device_dma_transfer(int device
, void *buf
, u32 lba
, u32 nsectors
, int dir
)
685 u16 sector
, lcyl
, hcyl
, select
, command
, len
;
688 len
= (nsectors
> 256) ? 256 : nsectors
;
690 ata_dma_set_dir(dir
);
692 /* Variable lba is only 32 bits so no change for lcyl and hcyl. */
693 lcyl
= (lba
>> 8) & 0xff;
694 hcyl
= (lba
>> 16) & 0xff;
696 if (lba_48bit
[device
]) {
697 /* Setup for 48-bit LBA. */
698 /* Combine bits 24-31 and bits 0-7 of lba into sector. */
699 sector
= ((lba
>> 16) & 0xff00) | (lba
& 0xff);
700 /* 0x40 enables LBA. */
701 select
= ((device
<< 4) | 0x40) & 0xffff;
702 command
= (dir
== 1) ? ATA_C_WRITE_DMA_EXT
: ATA_C_READ_DMA_EXT
;
704 /* Setup for 28-bit LBA. */
706 /* 0x40 enables LBA. */
707 select
= ((device
<< 4) | ((lba
>> 24) & 0xf) | 0x40) & 0xffff;
708 command
= (dir
== 1) ? ATA_C_WRITE_DMA
: ATA_C_READ_DMA
;
711 if ((res
= ata_io_start(buf
, len
, 0, len
, sector
, lcyl
,
712 hcyl
, select
, command
)) != 0)
714 if ((res
= ata_io_finish()) != 0)
726 static inline void ata_get_security_status(int device
, ata_devinfo_t
*devinfo
, u16
*param
)
728 if (ata_device_identify(device
, param
) == 0)
729 devinfo
[device
].security_status
= param
[ATA_ID_SECURITY_STATUS
];
733 int ata_device_sec_set_password(int device
, void *password
)
735 ata_devinfo_t
*devinfo
= atad_devinfo
;
736 u16
*param
= ata_param
;
738 u16 feature
= 0, command
= ATA_C_SEC_SET_PASSWORD
;
740 if (devinfo
[device
].security_status
& ATA_F_SEC_ENABLED
)
743 memset(param
, 0, 512);
744 memcpy(param
+ 1, password
, 32);
746 if (is_sony_hdd
[device
]) {
747 command
= ATA_C_SCE_SEC_CONTROL
;
748 feature
= ATA_C_SEC_SET_PASSWORD
;
751 res
= ata_io_start(param
, 1, feature
, 0, 0, 0, 0,
752 (device
<< 4) & 0xffff, command
);
754 res
= ata_io_finish();
756 ata_get_security_status(device
, devinfo
, param
);
761 int ata_device_sec_unlock(int device
, void *password
)
763 ata_devinfo_t
*devinfo
= atad_devinfo
;
764 u16
*param
= ata_param
;
766 u16 feature
= 0, command
= ATA_C_SEC_UNLOCK
;
768 if (!(devinfo
[device
].security_status
& ATA_F_SEC_LOCKED
))
771 memset(param
, 0, 512);
772 memcpy(param
+ 1, password
, 32);
774 if (is_sony_hdd
[device
]) {
775 command
= ATA_C_SCE_SEC_CONTROL
;
776 feature
= ATA_C_SEC_UNLOCK
;
779 if ((res
= ata_io_start(param
, 1, feature
, 0, 0, 0, 0,
780 (device
<< 4) & 0xffff, command
)) != 0)
782 if ((res
= ata_io_finish()) != 0)
785 /* Check to see if the drive was actually unlocked. */
786 ata_get_security_status(device
, devinfo
, param
);
787 if (devinfo
[device
].security_status
& ATA_F_SEC_LOCKED
)
794 /* I hope this is handled correctly - Sony's atad doesn't send a password, but
795 I allow for one to conform to the ATA spec.
796 TODO: Make password a vararg. */
797 int ata_device_sec_erase(int device
, void *password
)
799 ata_devinfo_t
*devinfo
= atad_devinfo
;
802 u16 feature
= 0, command
= ATA_C_SEC_ERASE_PREPARE
;
804 if (!(devinfo
[device
].security_status
& ATA_F_SEC_ENABLED
) ||
805 !(devinfo
[device
].security_status
& ATA_F_SEC_LOCKED
))
808 /* First send the mandatory ERASE PREPARE command. */
809 if (is_sony_hdd
[device
]) {
810 command
= ATA_C_SCE_SEC_CONTROL
;
811 feature
= ATA_C_SEC_ERASE_PREPARE
;
814 if ((res
= ata_io_start(NULL
, 1, feature
, 0, 0, 0, 0,
815 (device
<< 4) & 0xffff, command
)) != 0)
817 if ((res
= ata_io_finish()) != 0)
820 /* If this isn't a Sony HDD, then we will send the password. */
821 if (!is_sony_hdd
[device
]) {
822 if (password
!= NULL
) {
824 memset(param
, 0, 512);
825 memcpy(param
+ 1, password
, 32);
827 command
= ATA_C_SEC_ERASE_UNIT
;
830 command
= ATA_C_SCE_SEC_CONTROL
;
831 feature
= ATA_C_SEC_ERASE_UNIT
;
834 if ((res
= ata_io_start(param
, 1, feature
, 0, 0, 0, 0,
835 (device
<< 4) & 0xffff, command
)) == 0)
836 res
= ata_io_finish();
839 ata_get_security_status(device
, devinfo
, param
);
843 static void ata_device_probe(ata_devinfo_t
*devinfo
)
846 u16 nsector
, sector
, lcyl
, hcyl
, select
;
849 devinfo
->has_packet
= 0;
851 if (ata_hwport
->r_control
& 0x88)
854 nsector
= ata_hwport
->r_nsector
& 0xff;
855 sector
= ata_hwport
->r_sector
& 0xff;
856 lcyl
= ata_hwport
->r_lcyl
& 0xff;
857 hcyl
= ata_hwport
->r_hcyl
& 0xff;
858 select
= ata_hwport
->r_select
;
860 if ((nsector
!= 1) || (sector
!= 1))
864 if ((lcyl
== 0x14) && (hcyl
== 0xeb))
865 devinfo
->has_packet
= 1;
868 static int ata_init_devices(ata_devinfo_t
*devinfo
)
875 ata_device_probe(&devinfo
[0]);
876 if (!devinfo
[0].exists
) {
877 M_PRINTF("Error: Unable to detect HDD 0.\n");
878 devinfo
[1].exists
= 0;
882 /* If there is a device 1, grab it's info too. */
883 if ((res
= ata_device_select(1)) != 0)
885 if (ata_hwport
->r_control
& 0xff)
886 ata_device_probe(&devinfo
[1]);
888 devinfo
[1].exists
= 0;
890 for (i
= 0; i
< 2; i
++) {
891 if (!devinfo
[i
].exists
)
894 /* Send the IDENTIFY DEVICE command. if it doesn't succeed
895 devinfo is disabled. */
896 if (!devinfo
[i
].has_packet
) {
897 res
= ata_device_identify(i
, ata_param
);
898 devinfo
[i
].exists
= (res
== 0);
900 /* If it's a packet device, send the IDENTIFY PACKET
902 res
= ata_device_pkt_identify(i
, ata_param
);
903 devinfo
[i
].exists
= (res
== 0);
906 /* This next section is HDD-specific: if no device or it's a
907 packet (ATAPI) device, we're done. */
908 if (!devinfo
[i
].exists
|| devinfo
[i
].has_packet
)
911 /* This section is to detect whether the HDD supports 48-bit LBA
912 (IDENITFY DEVICE bit 10 word 83) and get the total sectors from
913 either words(61:60) for 28-bit or words(103:100) for 48-bit. */
914 if (ata_param
[ATA_ID_COMMAND_SETS_SUPPORTED
] & 0x0400) {
916 /* I don't think anyone would use a >2TB HDD but just in case. */
917 if (ata_param
[ATA_ID_48BIT_SECTOTAL_HI
]) {
918 devinfo
[i
].total_sectors
= 0xffffffff;
920 devinfo
[i
].total_sectors
=
921 (ata_param
[ATA_ID_48BIT_SECTOTAL_MI
] << 16)|
922 ata_param
[ATA_ID_48BIT_SECTOTAL_LO
];
926 devinfo
[i
].total_sectors
= (ata_param
[ATA_ID_SECTOTAL_HI
] << 16)|
927 ata_param
[ATA_ID_SECTOTAL_LO
];
929 devinfo
[i
].security_status
= ata_param
[ATA_ID_SECURITY_STATUS
];
931 /* Ultra DMA mode 4. */
932 ata_device_set_transfer_mode(i
, 0x40, 4);
933 ata_device_smart_enable(i
);
934 /* Set idle timeout period to 21min 15s. */
935 ata_device_idle(i
, 0xff);
942 ata_devinfo_t
* ata_get_devinfo(int device
)
944 if (ata_devinfo_init
)
945 return &atad_devinfo
[device
];
947 if (ata_bus_reset() != 0)
950 if (ata_init_devices(atad_devinfo
) != 0)
953 ata_devinfo_init
= 1;
954 return &atad_devinfo
[device
];
957 static void ata_dma_set_dir(int dir
)
963 val
= SPD_REG16(SPD_R_IF_CTRL
) & 1;
964 val
|= (dir
== 1) ? 0x4c : 0x4e;
965 SPD_REG16(SPD_R_IF_CTRL
) = val
;
966 SPD_REG16(SPD_R_XFR_CTRL
) = dir
| 0x86;
969 static void ata_pio_mode(int mode
)
989 SPD_REG16(SPD_R_PIO_MODE
) = val
;
992 static void ata_multiword_dma_mode(int mode
)
1002 SPD_REG16(SPD_R_MWDMA_MODE
) = val
;
1003 SPD_REG16(SPD_R_IF_CTRL
) = (SPD_REG16(SPD_R_IF_CTRL
) & 0xfffe)|0x48;
1006 static void ata_ultra_dma_mode(int mode
)
1027 SPD_REG16(SPD_R_UDMA_MODE
) = val
;
1028 SPD_REG16(SPD_R_IF_CTRL
) |= 0x49;
1032 int ata_device_is_sce(int device
)
1034 return is_sony_hdd
[device
];
1038 int ata_device_is_48bit(int device
)
1040 return lba_48bit
[device
];