2 Copyright 2011, jimmikaelkael
3 Licenced under Academic Free License version 3.0
4 Review Open PS2 Loader README & LICENSE files for further details.
6 ATA Driver for the HD Pro Kit, based on original ATAD form ps2sdk:
8 Copyright (c) 2003 Marcus R. Brown <mrbrown@0xd6.org>
9 Licenced under Academic Free License version 2.0
10 Review ps2sdk README & LICENSE files for further details.
12 $Id: ps2atad.c 1455 2007-11-04 23:46:27Z roman_ps2dev $
27 #ifdef __IOPCORE_DEBUG
28 #define M_PRINTF(format, args...) \
29 printf(format, ## args)
31 #define M_PRINTF(format, args...) \
35 #define BANNER "ATA device driver for HD Pro Kit %s\n"
36 #define VERSION "v1.0"
39 // HD Pro Kit is mapping the 1st word in ROM0 seg as a main ATA controller,
40 // The pseudo ATA controller registers are accessed (input/ouput) by writing
41 // an id to the main ATA controller (id specific to HDpro, see registers id below).
42 #define HDPROreg_IO8 (*(volatile unsigned char *)0xBFC00000)
43 #define HDPROreg_IO32 (*(volatile unsigned int *)0xBFC00000)
45 #define CDVDreg_STATUS (*(volatile unsigned char *)0xBF40200A)
47 // Pseudo ATA controller registers id - Output
48 #define ATAreg_CONTROL_RD 0x68
49 #define ATAreg_SELECT_RD 0x70
50 #define ATAreg_STATUS_RD 0xf0
51 #define ATAreg_ERROR_RD 0x90
52 #define ATAreg_NSECTOR_RD 0x50
53 #define ATAreg_SECTOR_RD 0xd0
54 #define ATAreg_LCYL_RD 0x30
55 #define ATAreg_HCYL_RD 0xb0
56 #define ATAreg_DATA_RD 0x41
58 // Pseudo ATA controller registers id - Input
59 #define ATAreg_CONTROL_WR 0x6a
60 #define ATAreg_SELECT_WR 0x72
61 #define ATAreg_COMMAND_WR 0xf2
62 #define ATAreg_FEATURE_WR 0x92
63 #define ATAreg_NSECTOR_WR 0x52
64 #define ATAreg_SECTOR_WR 0xd2
65 #define ATAreg_LCYL_WR 0x32
66 #define ATAreg_HCYL_WR 0xb2
67 #define ATAreg_DATA_WR 0x12
69 // HD Pro uses PIO commands for reading/writing from HDD
70 #define ATA_C_READ_PIO 0x20
71 #define ATA_C_READ_PIO_EXT 0x24
72 #define ATA_C_WRITE_PIO 0x30
73 #define ATA_C_WRITE_PIO_EXT 0x34
76 static int ata_evflg
= -1;
78 /* Used for indicating 48-bit LBA support. */
84 static int io_sema
= -1;
86 #define WAITIOSEMA(x) WaitSema(x)
87 #define SIGNALIOSEMA(x) SignalSema(x)
91 #define SIGNALIOSEMA(x)
95 /* Local device info. */
96 static ata_devinfo_t atad_devinfo
;
98 /* ATA command info. */
99 typedef struct _ata_cmd_info
{
104 static ata_cmd_info_t ata_cmd_table
[] = {
105 {0xc8, 0x04}, {0xec, 0x02}, {0xa1, 0x02}, {0xb0, 0x07}, {0xef, 0x01}, {0x25, 0x04}, {0xca, 0x04}, {0xe3, 0x01}, {0x35, 0x04},
106 {0x20, 0x02}, {0x30, 0x03}, {0x24, 0x02}, {0x34, 0x03}
108 #define ATA_CMD_TABLE_SIZE (sizeof ata_cmd_table/sizeof(ata_cmd_info_t))
110 static ata_cmd_info_t smart_cmd_table
[] = {
113 #define SMART_CMD_TABLE_SIZE (sizeof smart_cmd_table/sizeof(ata_cmd_info_t))
115 /* This is the state info tracked between ata_io_start() and ata_io_finish(). */
116 typedef struct _ata_cmd_state
{
117 int type
; /* The ata_cmd_info_t type field. */
119 u32 blkcount
; /* The number of 512-byte blocks (sectors) to transfer. */
120 int dir
; /* DMA direction: 0 - to RAM, 1 - from RAM. */
123 static ata_cmd_state_t atad_cmd_state
;
125 static int hdpro_io_active
= 0;
126 static int intr_suspended
= 0;
127 static int intr_state
;
129 static int ata_wait_busy(int bits
);
131 static u32
ata_alarm_cb(void *unused
)
133 iSetEventFlag(ata_evflg
, 0x01);
137 static void suspend_intr(void)
139 if (!intr_suspended
) {
140 CpuSuspendIntr(&intr_state
);
146 static void resume_intr(void)
148 if (intr_suspended
) {
149 CpuResumeIntr(intr_state
);
155 static int hdpro_io_start(void)
164 // HD Pro IO start commands sequence
171 u32 res
= HDPROreg_IO8
;
177 if ((res
& 0xff) == 0xe7)
180 return hdpro_io_active
;
183 static int hdpro_io_finish(void)
185 if (!hdpro_io_active
)
190 // HD Pro IO finish commands sequence
198 if (HDPROreg_IO32
== 0x401a7800) // check the 1st in ROM0 seg get
199 hdpro_io_active
= 0; // back to it's original state
201 return hdpro_io_active
^ 1;
204 static void hdpro_io_write(u8 cmd
, u16 val
)
208 // IO write to HD Pro
213 HDPROreg_IO8
= (val
& 0xffff) >> 8;
219 static int hdpro_io_read(u8 cmd
)
223 // IO read from HD Pro
226 u32 res0
= HDPROreg_IO8
;
228 u32 res1
= HDPROreg_IO8
;
230 res0
= (res0
& 0xff) | (res1
<< 8);
234 return res0
& 0xffff;
237 static int hdpro_io_init(void)
241 // HD Pro IO initialization commands sequence
262 return ata_wait_busy(0x80);
270 M_PRINTF(BANNER
, VERSION
);
274 if ((ata_evflg
= CreateEventFlag(&event
)) < 0) {
275 M_PRINTF("Couldn't create event flag, exiting.\n");
284 if (hdpro_io_init() != 0)
293 io_sema
= CreateSema(&smp
);
297 M_PRINTF("Driver loaded.\n");
307 return hdpro_io_read(ATAreg_ERROR_RD
) & 0xff;
310 /* 0x80 for busy, 0x88 for bus busy. */
311 static int ata_wait_busy(int bits
)
316 for (i
= 0; i
< 80; i
++) {
320 u16 r_control
= hdpro_io_read(ATAreg_CONTROL_RD
);
324 if (!((r_control
& 0xffff) & bits
))
352 M_PRINTF("Timeout while waiting on busy (0x%02x).\n", bits
);
360 static int ata_device_select(int device
)
364 if ((res
= ata_wait_busy(0x88)) < 0)
367 /* If the device was already selected, nothing to do. */
368 if (((hdpro_io_read(ATAreg_SELECT_RD
) >> 4) & 1) == device
)
371 /* Select the device. */
372 hdpro_io_write(ATAreg_SELECT_WR
, (device
& 1) << 4);
373 res
= hdpro_io_read(ATAreg_CONTROL_RD
);
375 return ata_wait_busy(0x88);
379 int ata_io_start(void *buf
, u32 blkcount
, u16 feature
, u16 nsector
, u16 sector
,
380 u16 lcyl
, u16 hcyl
, u16 select
, u16 command
)
382 iop_sys_clock_t cmd_timeout
;
383 ata_cmd_info_t
*cmd_table
;
384 int i
, res
, type
, cmd_table_size
;
385 int using_timeout
, device
= (select
>> 4) & 1;
388 ClearEventFlag(ata_evflg
, 0);
390 if ((res
= ata_device_select(device
)) != 0)
393 /* For the SCE and SMART commands, we need to search on the subcommand
394 specified in the feature register. */
395 if (command
== ATA_C_SMART
) {
396 cmd_table
= smart_cmd_table
;
397 cmd_table_size
= SMART_CMD_TABLE_SIZE
;
400 cmd_table
= ata_cmd_table
;
401 cmd_table_size
= ATA_CMD_TABLE_SIZE
;
406 for (i
= 0; i
< cmd_table_size
; i
++) {
407 if ((searchcmd
& 0xff) == cmd_table
[i
].command
) {
408 type
= cmd_table
[i
].type
;
413 if (!(atad_cmd_state
.type
= type
))
416 atad_cmd_state
.buf
= buf
;
417 atad_cmd_state
.blkcount
= blkcount
;
419 /* Check that the device is ready if this the appropiate command. */
420 if (!(hdpro_io_read(ATAreg_CONTROL_RD
) & 0x40)) {
429 M_PRINTF("Error: Device %d is not ready.\n", device
);
434 /* Does this command need a timeout? */
443 atad_cmd_state
.dir
= ((command
!= 0xc8) && (command
!= 0x25));
445 atad_cmd_state
.dir
= ATA_DIR_READ
;
451 cmd_timeout
.lo
= 0x41eb0000;
454 if ((res
= SetAlarm(&cmd_timeout
, (void *)ata_alarm_cb
, NULL
)) < 0)
458 /* Enable the command completion interrupt. */
462 u32 dummy
= HDPROreg_IO8
;
467 /* Finally! We send off the ATA command with arguments. */
468 hdpro_io_write(ATAreg_CONTROL_WR
, (using_timeout
== 0) << 1);
470 /* 48-bit LBA requires writing to the address registers twice,
471 24 bits of the LBA address is written each time.
472 Writing to registers twice does not affect 28-bit LBA since
473 only the latest data stored in address registers is used. */
474 hdpro_io_write(ATAreg_FEATURE_WR
, (feature
& 0xffff) >> 8);
475 hdpro_io_write(ATAreg_NSECTOR_WR
, (nsector
& 0xffff) >> 8);
476 hdpro_io_write(ATAreg_SECTOR_WR
, (sector
& 0xffff) >> 8);
477 hdpro_io_write(ATAreg_LCYL_WR
, (lcyl
& 0xffff) >> 8);
478 hdpro_io_write(ATAreg_HCYL_WR
, (hcyl
& 0xffff) >> 8);
480 hdpro_io_write(ATAreg_FEATURE_WR
, feature
& 0xff);
481 hdpro_io_write(ATAreg_NSECTOR_WR
, nsector
& 0xff);
482 hdpro_io_write(ATAreg_SECTOR_WR
, sector
& 0xff);
483 hdpro_io_write(ATAreg_LCYL_WR
, lcyl
& 0xff);
484 hdpro_io_write(ATAreg_HCYL_WR
, hcyl
& 0xff);
486 hdpro_io_write(ATAreg_SELECT_WR
, select
& 0xff);
487 hdpro_io_write(ATAreg_COMMAND_WR
, command
& 0xff);
492 /* Do a PIO transfer, to or from the device. */
493 static int ata_pio_transfer(ata_cmd_state_t
*cmd_state
)
497 int res
= 0, chk
= 0;
498 u16 status
= hdpro_io_read(ATAreg_STATUS_RD
);
500 if (status
& ATA_STAT_ERR
) {
501 M_PRINTF("Error: Command error: status 0x%02x, error 0x%02x.\n",
502 status
, ata_get_error());
506 /* DRQ must be set (data request). */
507 if (!(status
& ATA_STAT_DRQ
))
510 type
= cmd_state
->type
;
512 if (type
== 3 || type
== 8) {
514 buf
= cmd_state
->buf
;
519 for (i
= 0; i
< 256; i
++) {
520 u16 r_data
= *(u16
*)buf
;
521 hdpro_io_write(ATAreg_DATA_WR
, r_data
);
523 cmd_state
->buf
= ++((u16
*)buf
);
526 u16 out
= hdpro_io_read(ATAreg_DATA_RD
) & 0xffff;
527 if (out
!= (chk
& 0xffff))
530 if (cmd_state
->type
== 8) {
531 for (i
= 0; i
< 4; i
++) {
532 hdpro_io_write(ATAreg_DATA_WR
, *(u8
*)buf
);
533 cmd_state
->buf
= ++((u8
*)buf
);
537 } else if (type
== 2) {
539 buf
= cmd_state
->buf
;
547 for (i
= 0; i
< 256; i
++) {
549 u32 res0
= HDPROreg_IO8
;
551 u32 res1
= HDPROreg_IO8
;
554 res0
= (res0
& 0xff) | (res1
<< 8);
557 *(u16
*)buf
= res0
& 0xffff;
558 cmd_state
->buf
= ++((u16
*)buf
);
567 u16 r_data
= hdpro_io_read(ATAreg_DATA_RD
) & 0xffff;
568 if (r_data
!= (chk
& 0xffff))
576 int ata_io_finish(void)
578 ata_cmd_state_t
*cmd_state
= &atad_cmd_state
;
580 int res
= 0, type
= cmd_state
->type
;
583 if (type
== 1 || type
== 6) { /* Non-data commands. */
590 u32 ret
= HDPROreg_IO8
;
595 if (((ret
& 0xff) & 1) == 0) {
597 WaitEventFlag(ata_evflg
, 0x03, 0x11, &bits
);
598 if (bits
& 0x01) { /* Timeout. */
599 M_PRINTF("Error: ATA timeout on a non-data command.\n");
607 } else if (type
== 4) { /* DMA. */
608 M_PRINTF("Error: DMA mode not implemented.\n");
610 } else { /* PIO transfers. */
611 stat
= hdpro_io_read(ATAreg_CONTROL_RD
);
612 if ((res
= ata_wait_busy(0x80)) < 0)
615 /* Transfer each PIO data block. */
616 while (--cmd_state
->blkcount
!= -1) {
617 if ((res
= ata_pio_transfer(cmd_state
)) < 0)
619 if ((res
= ata_wait_busy(0x80)) < 0)
627 /* Wait until the device isn't busy. */
628 if (hdpro_io_read(ATAreg_STATUS_RD
) & ATA_STAT_BUSY
)
629 res
= ata_wait_busy(0x80);
630 if ((stat
= hdpro_io_read(ATAreg_STATUS_RD
)) & ATA_STAT_ERR
) {
631 M_PRINTF("Error: Command error: status 0x%02x, error 0x%02x.\n",
632 stat
, ata_get_error());
637 /* The command has completed (with an error or not), so clean things up. */
638 CancelAlarm((void *)ata_alarm_cb
, NULL
);
644 int ata_device_dma_transfer(int device
, void *buf
, u32 lba
, u32 nsectors
, int dir
)
648 u16 sector
, lcyl
, hcyl
, select
, command
, len
;
652 if (!hdpro_io_start())
656 len
= (nsectors
> 256) ? 256 : nsectors
;
658 /* Variable lba is only 32 bits so no change for lcyl and hcyl. */
659 lcyl
= (lba
>> 8) & 0xff;
660 hcyl
= (lba
>> 16) & 0xff;
663 /* Setup for 48-bit LBA. */
664 /* Combine bits 24-31 and bits 0-7 of lba into sector. */
665 sector
= ((lba
>> 16) & 0xff00) | (lba
& 0xff);
666 /* 0x40 enables LBA. */
667 select
= ((device
<< 4) | 0x40) & 0xffff;
668 command
= ((dir
== 1)&&(ATAWRITE
)) ? ATA_C_WRITE_PIO_EXT
: ATA_C_READ_PIO_EXT
;
670 /* Setup for 28-bit LBA. */
672 /* 0x40 enables LBA. */
673 select
= ((device
<< 4) | ((lba
>> 24) & 0xf) | 0x40) & 0xffff;
674 command
= ((dir
== 1)&&(ATAWRITE
)) ? ATA_C_WRITE_PIO
: ATA_C_READ_PIO
;
677 if ((res
= ata_io_start(buf
, len
, 0, len
, sector
, lcyl
,
678 hcyl
, select
, command
)) != 0)
680 if ((res
= ata_io_finish()) != 0)
689 if (!hdpro_io_finish())
692 SIGNALIOSEMA(io_sema
);
698 ata_devinfo_t
* ata_get_devinfo(int device
)
700 return &atad_devinfo
;