53#define M_PRINTF(format, args...) \
54 printf(MODNAME ": " format, ##args)
56#define BANNER "ATA device driver %s - Copyright (c) 2003 Marcus R. Brown\n"
59#define ATA_XFER_MODE_PIO 0x08
61#define ATA_XFER_MODE_MDMA 0x20
63#define ATA_XFER_MODE_UDMA 0x40
65#define ATA_EV_TIMEOUT 1
66#define ATA_EV_COMPLETE 2
68static int ata_devinfo_init = 0;
69static int ata_evflg = -1;
72static u8 ata_dvrp_workaround = 0;
74#ifdef ATA_GAMESTAR_WORKAROUND
75static u8 ata_gamestar_workaround = 0;
84static u16 ata_param[256];
95 {ATA_C_CFA_REQUEST_EXTENDED_ERROR_CODE, 1},
96 {ATA_C_DEVICE_RESET, 5},
97 {ATA_C_READ_SECTOR, 2},
98 {ATA_C_READ_SECTOR_EXT, 0x82},
99 {ATA_C_READ_DMA_EXT, 0x84},
100 {ATA_C_READ_NATIVE_MAX_ADDRESS_EXT, 0x81},
101 {ATA_C_READ_MULTIPLE_EXT, 0x82},
102 {ATA_C_WRITE_SECTOR, 3},
103 {ATA_C_WRITE_LONG, 8},
104 {ATA_C_WRITE_SECTOR_EXT, 0x83},
105 {ATA_C_WRITE_DMA_EXT, 0x84},
106 {ATA_C_SET_MAX_ADDRESS_EXT, 0x81},
107 {ATA_C_CFA_WRITE_SECTORS_WITHOUT_ERASE, 3},
108 {ATA_C_WRITE_MULTIPLE_EXT, 0x83},
109 {ATA_C_READ_VERIFY_SECTOR, 1},
110 {ATA_C_READ_VERIFY_SECTOR_EXT, 0x81},
112 {ATA_C_CFA_TRANSLATE_SECTOR, 2},
113 {ATA_C_SCE_SECURITY_CONTROL, 7},
114 {ATA_C_EXECUTE_DEVICE_DIAGNOSTIC, 6},
115 {ATA_C_INITIALIZE_DEVICE_PARAMETERS, 1},
116 {ATA_C_DOWNLOAD_MICROCODE, 3},
117 {ATA_C_IDENTIFY_PACKET_DEVICE, 2},
119 {ATA_C_CFA_ERASE_SECTORS, 1},
120 {ATA_C_READ_MULTIPLE, 2},
121 {ATA_C_WRITE_MULTIPLE, 3},
122 {ATA_C_SET_MULTIPLE_MODE, 1},
124 {ATA_C_WRITE_DMA, 4},
125 {ATA_C_CFA_WRITE_MULTIPLE_WITHOUT_ERASE, 3},
126 {ATA_C_GET_MEDIA_STATUS, 1},
127 {ATA_C_MEDIA_LOCK, 1},
128 {ATA_C_MEDIA_UNLOCK, 1},
129 {ATA_C_STANDBY_IMMEDIATE, 1},
130 {ATA_C_IDLE_IMMEDIATE, 1},
133 {ATA_C_READ_BUFFER, 2},
134 {ATA_C_CHECK_POWER_MODE, 1},
136 {ATA_C_FLUSH_CACHE, 1},
137 {ATA_C_WRITE_BUFFER, 3},
138 {ATA_C_FLUSH_CACHE_EXT, 1},
139 {ATA_C_IDENTIFY_DEVICE, 2},
140 {ATA_C_MEDIA_EJECT, 1},
141 {ATA_C_SET_FEATURES, 1},
142 {ATA_C_SECURITY_SET_PASSWORD, 3},
143 {ATA_C_SECURITY_UNLOCK, 3},
144 {ATA_C_SECURITY_ERASE_PREPARE, 1},
145 {ATA_C_SECURITY_ERASE_UNIT, 3},
146 {ATA_C_SECURITY_FREEZE_LOCK, 1},
147 {ATA_C_SECURITY_DISABLE_PASSWORD, 3},
148 {ATA_C_READ_NATIVE_MAX_ADDRESS, 1},
149 {ATA_C_SET_MAX_ADDRESS, 1}};
150#define ATA_CMD_TABLE_SIZE (sizeof ata_cmd_table / sizeof(ata_cmd_info_t))
153 {ATA_SCE_IDENTIFY_DRIVE, 2},
154 {ATA_SCE_SECURITY_ERASE_PREPARE, 1},
155 {ATA_SCE_SECURITY_ERASE_UNIT, 1},
156 {ATA_SCE_SECURITY_FREEZE_LOCK, 1},
157 {ATA_SCE_SECURITY_SET_PASSWORD, 3},
158 {ATA_SCE_SECURITY_UNLOCK, 3},
159 {ATA_SCE_SECURITY_WRITE_ID, 3},
160 {ATA_SCE_SECURITY_READ_ID, 2}};
161#define SEC_CTRL_CMD_TABLE_SIZE (sizeof sec_ctrl_cmd_table / sizeof(ata_cmd_info_t))
164 {ATA_S_SMART_READ_DATA, 2},
165 {ATA_S_SMART_ENABLE_DISABLE_AUTOSAVE, 1},
166 {ATA_S_SMART_SAVE_ATTRIBUTE_VALUES, 1},
167 {ATA_S_SMART_EXECUTE_OFF_LINE, 1},
168 {ATA_S_SMART_READ_LOG, 2},
169 {ATA_S_SMART_WRITE_LOG, 3},
170 {ATA_S_SMART_ENABLE_OPERATIONS, 1},
171 {ATA_S_SMART_DISABLE_OPERATIONS, 1},
172 {ATA_S_SMART_RETURN_STATUS, 1}};
173#define SMART_CMD_TABLE_SIZE (sizeof smart_cmd_table / sizeof(ata_cmd_info_t))
193#define ATA_BD_SECTOR_SIZE 512
198static int ata_intr_cb(
int flag);
200#ifdef ATA_USE_AIFDEV9
201static int ata_intr_cb(
void);
203static unsigned int ata_alarm_cb(
void *unused);
205#ifdef ATA_USE_AIFDEV9
206static void aif_tune_drive(
int unit,
int mode);
210static void ata_set_dir(
int dir);
212static void ata_pio_mode(
int mode);
213#ifdef ATA_MWDMA_MODES
214static void ata_multiword_dma_mode(
int mode);
216static void ata_ultra_dma_mode(
int mode);
218static void ata_shutdown_cb(
void);
220int ata_device_sector_io64(
int device,
void *buf, u64 lba, u32 nsectors,
int dir);
223static int ata_bd_read(
struct block_device *bd, u64 sector,
void *buffer, u16
count);
224static int ata_bd_write(
struct block_device *bd, u64 sector,
const void *buffer, u16
count);
233static void ata_pre_dma_cb(
int bcr,
int dir)
240 SPD_REG16(SPD_R_XFR_CTRL) |= 0x80;
243static void ata_post_dma_cb(
int bcr,
int dir)
250 SPD_REG16(SPD_R_XFR_CTRL) &= ~0x80;
254static int ata_create_event_flag(
void)
260 return CreateEventFlag(&
event);
263int _start(
int argc,
char *argv[])
268#ifdef ATA_USE_AIFDEV9
276 printf(BANNER, VERSION);
278 res = MODULE_NO_RESIDENT_END;
281 if (!(SPD_REG16(SPD_R_REV_3) & SPD_CAPS_ATA) || !(SPD_REG16(SPD_R_REV_8) & 0x02)) {
282 M_PRINTF(
"HDD is not connected, exiting.\n");
297 ata_dvrp_workaround = (SPD_REG16(SPD_R_REV_3) & SPD_CAPS_DVR) ? 1 : 0;
300#ifdef ATA_USE_AIFDEV9
301 if (!aifIsDetected()) {
302 M_PRINTF(
"AIF HDD: controller not found.\n");
305 aif_regs[AIF_ATA_TCFG] = 0;
309#ifdef ATA_GAMESTAR_WORKAROUND
314 if (SPD_REG16(0x20) != 1) {
315 ata_gamestar_workaround = 1;
316 M_PRINTF(
"Compatible adaptor detected.\n");
318 ata_gamestar_workaround = 0;
323 if ((ata_evflg = ata_create_event_flag()) < 0) {
324 M_PRINTF(
"Couldn't create event flag, exiting.\n");
330 SpdRegisterIntrHandler(1, &ata_intr_cb);
331 SpdRegisterIntrHandler(0, &ata_intr_cb);
332#ifdef ATA_GAMESTAR_WORKAROUND
333 if (!ata_gamestar_workaround) {
335 dev9RegisterPreDmaCb(0, &ata_pre_dma_cb);
336 dev9RegisterPostDmaCb(0, &ata_post_dma_cb);
337#ifdef ATA_GAMESTAR_WORKAROUND
341 Dev9RegisterPowerOffHandler(15, &ata_shutdown_cb);
344#ifdef ATA_USE_AIFDEV9
345 aifRegisterIntrCb(AIF_INUM_ATA0, &ata_intr_cb);
346 aifRegisterShutdownCb(AIF_INUM_ATA0, &ata_shutdown_cb);
348 M_PRINTF(
"AIF HDD initialized.\n");
355 for (i = 0; i < NUM_DEVICES; ++i) {
356 g_ata_bd[i].priv = (
void *)&atad_devinfo[i];
357 g_ata_bd[i].name =
"ata";
358 g_ata_bd[i].path =
"ata";
359 g_ata_bd[i].devNr = i;
360 g_ata_bd[i].parNr = 0;
361 g_ata_bd[i].parId = 0x00;
362 g_ata_bd[i].sectorSize = 512;
363 g_ata_bd[i].sectorOffset = 0;
364 g_ata_bd[i].sectorCount = 0;
366 g_ata_bd[i].read = ata_bd_read;
367 g_ata_bd[i].write = ata_bd_write;
368 g_ata_bd[i].flush = ata_bd_flush;
369 g_ata_bd[i].stop = ata_bd_stop;
377 if (RegisterLibraryEntries(&_exp_atad) != 0) {
378 M_PRINTF(
"Library is already registered, exiting.\n");
382 res = MODULE_RESIDENT_END;
383 M_PRINTF(
"Driver loaded.\n");
388int _exit(
void) {
return MODULE_RESIDENT_END; }
391static int ata_intr_cb(
int flag)
394 memset(atad_devinfo, 0,
sizeof atad_devinfo);
396 SpdIntrDisable(SPD_INTR_ATA);
397 iSetEventFlag(ata_evflg, ATA_EV_COMPLETE);
404#ifdef ATA_USE_AIFDEV9
405static int ata_intr_cb(
void)
407 aifIntrDisable(AIF_INTR_ATA0);
408 iSetEventFlag(ata_evflg, ATA_EV_COMPLETE);
414static unsigned int ata_alarm_cb(
void *unused)
418 iSetEventFlag(ata_evflg, ATA_EV_TIMEOUT);
423int sceAtaGetError(
void)
428#ifdef ATA_USE_AIFDEV9
431 return ata_hwport->r_error & 0xff;
434#define ATA_WAIT_BUSY 0x80
435#define ATA_WAIT_BUSBUSY 0x88
437#define ata_wait_busy() gen_ata_wait_busy(ATA_WAIT_BUSY)
438#define ata_wait_bus_busy() gen_ata_wait_busy(ATA_WAIT_BUSBUSY)
442static int gen_ata_wait_busy(
int bits)
447#ifdef ATA_USE_AIFDEV9
452 for (i = 0; i < 80; i++) {
455 if (!(ata_hwport->r_control & bits))
481 M_PRINTF(
"Timeout while waiting on busy (0x%02x).\n", bits);
482 return ATA_RES_ERR_TIMEOUT;
485static int ata_device_select(
int device)
490#ifdef ATA_USE_AIFDEV9
495 if ((res = ata_wait_bus_busy()) < 0)
499 if (((ata_hwport->r_select >> 4) & 1) == device)
503 ata_hwport->r_select = (device & 1) << 4;
504 (void)(ata_hwport->r_control);
505 (void)(ata_hwport->r_control);
507 return ata_wait_bus_busy();
520int sceAtaExecCmd(
void *buf, u32 blkcount, u16 feature, u16 nsector, u16 sector, u16 lcyl, u16 hcyl, u16 select, u16 command)
525#ifdef ATA_USE_AIFDEV9
530 int i, res, type, cmd_table_size;
531 int using_timeout, device = (select >> 4) & 1;
534 ClearEventFlag(ata_evflg, 0);
536 if (!atad_devinfo[device].exists)
537 return ATA_RES_ERR_NODEV;
539 if ((res = ata_device_select(device)) != 0)
544 if (command == ATA_C_SCE_SECURITY_CONTROL) {
545 cmd_table = sec_ctrl_cmd_table;
546 cmd_table_size = SEC_CTRL_CMD_TABLE_SIZE;
547 searchcmd = (u8)feature;
548 }
else if (command == ATA_C_SMART) {
549 cmd_table = smart_cmd_table;
550 cmd_table_size = SMART_CMD_TABLE_SIZE;
551 searchcmd = (u8)feature;
553 cmd_table = ata_cmd_table;
554 cmd_table_size = ATA_CMD_TABLE_SIZE;
555 searchcmd = (u8)command;
559 for (i = 0; i < cmd_table_size; i++) {
560 if (searchcmd == cmd_table[i].command) {
561 type = cmd_table[i].type;
566 if (!(atad_cmd_state.type = type & 0x7F))
567 return ATA_RES_ERR_CMD;
569 atad_cmd_state.buf = buf;
570 atad_cmd_state.blkcount = blkcount;
573 if (!(ata_hwport->r_control & 0x40)) {
575 case ATA_C_DEVICE_RESET:
576 case ATA_C_EXECUTE_DEVICE_DIAGNOSTIC:
577 case ATA_C_INITIALIZE_DEVICE_PARAMETERS:
579 case ATA_C_IDENTIFY_PACKET_DEVICE:
582 M_PRINTF(
"Error: Device %d is not ready.\n", device);
583 return ATA_RES_ERR_NOTREADY;
589 switch (type & 0x7F) {
595 atad_cmd_state.dir = (command != ATA_C_READ_DMA && command != ATA_C_READ_DMA_EXT);
601 cmd_timeout.lo = 0x41eb0000;
605 if (command == ATA_C_SCE_SECURITY_CONTROL && feature == ATA_SCE_SECURITY_ERASE_UNIT)
606 USec2SysClock(180000000, &cmd_timeout);
608 if ((res = SetAlarm(&cmd_timeout, &ata_alarm_cb, NULL)) < 0)
613 if ((type & 0x7F) == 1) {
615 SpdIntrEnable(SPD_INTR_ATA0);
617#ifdef ATA_USE_AIFDEV9
618 aifIntrEnable(AIF_INTR_ATA0);
623 ata_hwport->r_control = (using_timeout == 0) << 1;
630 ata_hwport->r_feature = (feature >> 8) & 0xff;
631 ata_hwport->r_nsector = (nsector >> 8) & 0xff;
632 ata_hwport->r_sector = (sector >> 8) & 0xff;
633 ata_hwport->r_lcyl = (lcyl >> 8) & 0xff;
634 ata_hwport->r_hcyl = (hcyl >> 8) & 0xff;
637 ata_hwport->r_feature = feature & 0xff;
638 ata_hwport->r_nsector = nsector & 0xff;
639 ata_hwport->r_sector = sector & 0xff;
640 ata_hwport->r_lcyl = lcyl & 0xff;
641 ata_hwport->r_hcyl = hcyl & 0xff;
642 ata_hwport->r_select = (select | ATA_SEL_LBA) & 0xff;
643 ata_hwport->r_command = command & 0xff;
659#ifdef ATA_USE_AIFDEV9
664 u16 status = ata_hwport->r_status & 0xff;
666 if (status & ATA_STAT_ERR) {
667 M_PRINTF(
"Error: PIO cmd error: status 0x%02x, error 0x%02x.\n", status, sceAtaGetError());
668 return ATA_RES_ERR_IO;
672 if (!(status & ATA_STAT_DRQ))
673 return ATA_RES_ERR_NODATA;
675 type = cmd_state->type;
677 if (type == 3 || type == 8) {
679 buf16 = cmd_state->buf16;
680 for (i = 0; i < 256; i++) {
681 ata_hwport->r_data = *buf16;
682 cmd_state->buf16 = ++buf16;
684 if (cmd_state->type == 8) {
687 buf8 = cmd_state->buf8;
688 for (i = 0; i < 4; i++) {
689 ata_hwport->r_data = *buf8;
690 cmd_state->buf8 = ++buf8;
693 }
else if (type == 2) {
695 buf16 = cmd_state->buf16;
696 for (i = 0; i < 256; i++) {
697 *buf16 = ata_hwport->r_data;
698 cmd_state->buf16 = ++buf16;
707static int ata_dma_complete(
void *buf, u32 blkcount,
int dir)
711 u32 bits,
count, nbytes;
716 for (i = 0; i < 20; i++)
717 if ((dma_stat = SPD_REG16(0x38) & 0x1f))
723 SpdIntrEnable(SPD_INTR_ATA);
725 WaitEventFlag(ata_evflg, ATA_EV_TIMEOUT | ATA_EV_COMPLETE, WEF_CLEAR | WEF_OR, &bits);
727 if (bits & ATA_EV_TIMEOUT) {
728 M_PRINTF(
"Error: DMA timeout.\n");
729 return ATA_RES_ERR_TIMEOUT;
732 if (!(SPD_REG16(SPD_R_INTR_STAT) & 0x02)) {
733 if (ata_hwport->r_control & 0x01) {
734 M_PRINTF(
"Error: Command error while doing DMA.\n");
735 M_PRINTF(
"Error: Command error status 0x%02x, error 0x%02x.\n", ata_hwport->r_status, sceAtaGetError());
737 return ((sceAtaGetError() & ATA_ERR_ICRC) ? ATA_RES_ERR_ICRC : ATA_RES_ERR_IO);
739 M_PRINTF(
"Warning: Got command interrupt, but not an error.\n");
744 dma_stat = SPD_REG16(0x38) & 0x1f;
747 count = (blkcount < dma_stat) ? blkcount : dma_stat;
748 nbytes =
count * 512;
749 if ((res = SpdDmaTransfer(0, buf, (nbytes << 9) | 32, dir)) < 0)
752 buf = (
void *)((u8 *)buf + nbytes);
761int sceAtaWaitResult(
void)
767#ifdef ATA_USE_AIFDEV9
772 int res = 0, type = cmd_state->type;
775 if (type == 1 || type == 6) {
776 WaitEventFlag(ata_evflg, ATA_EV_TIMEOUT | ATA_EV_COMPLETE, WEF_CLEAR | WEF_OR, &bits);
777 if (bits & ATA_EV_TIMEOUT) {
778 M_PRINTF(
"Error: ATA timeout on a non-data command.\n");
779 return ATA_RES_ERR_TIMEOUT;
781 }
else if (type == 4) {
785 if ((res = ata_dma_complete(cmd_state->buf, cmd_state->blkcount,
786 cmd_state->dir)) < 0)
789 for (i = 0; i < 100; i++)
790 if ((stat = SPD_REG16(SPD_R_INTR_STAT) & 0x01))
793 SpdIntrEnable(SPD_INTR_ATA0);
794 WaitEventFlag(ata_evflg, ATA_EV_TIMEOUT | ATA_EV_COMPLETE, WEF_CLEAR | WEF_OR, &bits);
795 if (bits & ATA_EV_TIMEOUT) {
796 M_PRINTF(
"Error: ATA timeout on DMA completion, buffer stat %04x\n", SPD_REG16(0x38));
797 M_PRINTF(
"Error: istat %x, ienable %x\n", SPD_REG16(SPD_R_INTR_STAT), SPD_REG16(SPD_R_INTR_MASK));
798 res = ATA_RES_ERR_TIMEOUT;
802#ifdef ATA_USE_AIFDEV9
806 stat = ata_hwport->r_control;
807 if ((res = ata_wait_busy()) < 0)
811 while ((
int)(--cmd_state->blkcount) != -1) {
812 if ((res = ata_pio_transfer(cmd_state)) < 0)
814 if ((res = ata_wait_busy()) < 0)
823 if (ata_hwport->r_status & ATA_STAT_BUSY)
824 res = ata_wait_busy();
825 if ((stat = ata_hwport->r_status) & ATA_STAT_ERR) {
826 M_PRINTF(
"Error: Command error: status 0x%02x, error 0x%02x.\n", stat, sceAtaGetError());
828 res = (sceAtaGetError() & ATA_ERR_ICRC) ? ATA_RES_ERR_ICRC : ATA_RES_ERR_IO;
833 CancelAlarm(&ata_alarm_cb, NULL);
840 M_PRINTF(
"error: ATA failed, %d\n", res);
846static int ata_bus_reset(
void)
850 SPD_REG16(SPD_R_IF_CTRL) = SPD_IF_ATA_RESET;
852 SPD_REG16(SPD_R_IF_CTRL) = 0;
853 SPD_REG16(SPD_R_IF_CTRL) = 0x48;
856 return ata_wait_busy();
860int sceAtaSoftReset(
void)
865#ifdef ATA_USE_AIFDEV9
869 if (ata_hwport->r_control & 0x80)
870 return ATA_RES_ERR_NOTREADY;
873 ata_hwport->r_control = 6;
877 ata_hwport->r_control = 2;
880 return ata_wait_busy();
884int sceAtaFlushCache(
int device)
888 if (!(res = sceAtaExecCmd(NULL, 1, 0, 0, 0, 0, 0, (device << 4) & 0xffff, (atad_devinfo[device].lba48 && !ata_dvrp_workaround) ? ATA_C_FLUSH_CACHE_EXT : ATA_C_FLUSH_CACHE)))
889 res = sceAtaWaitResult();
895int sceAtaIdle(
int device,
int period)
899 if (!(res = sceAtaExecCmd(NULL, 1, 0, period & 0xff, 0, 0, 0, (device << 4) & 0xffff, ATA_C_IDLE)))
900 res = sceAtaWaitResult();
905static int ata_device_identify(
int device,
void *
info)
909 if (!(res = sceAtaExecCmd(
info, 1, 0, 0, 0, 0, 0, (device << 4) & 0xffff, ATA_C_IDENTIFY_DEVICE)))
910 res = sceAtaWaitResult();
915static int ata_device_pkt_identify(
int device,
void *
info)
919 res = sceAtaExecCmd(
info, 1, 0, 0, 0, 0, 0, (device << 4) & 0xffff, ATA_C_IDENTIFY_PACKET_DEVICE);
921 return sceAtaWaitResult();
926int sceAtaGetSceId(
int device,
void *data)
930 if (!(res = sceAtaExecCmd(data, 1, ATA_SCE_IDENTIFY_DRIVE, 0, 0, 0, 0, (device << 4) & 0xffff, ATA_C_SCE_SECURITY_CONTROL)))
931 res = sceAtaWaitResult();
936static int ata_device_smart_enable(
int device)
940 if (!(res = sceAtaExecCmd(NULL, 1, ATA_S_SMART_ENABLE_OPERATIONS, 0, 0, 0x4f, 0xc2, (device << 4) & 0xffff, ATA_C_SMART)))
941 res = sceAtaWaitResult();
947int sceAtaSmartSaveAttr(
int device)
951 if (!(res = sceAtaExecCmd(NULL, 1, ATA_S_SMART_SAVE_ATTRIBUTE_VALUES, 0, 0, 0x4f, 0xc2, (device << 4) & 0xffff, ATA_C_SMART)))
952 res = sceAtaWaitResult();
958int sceAtaSmartReturnStatus(
int device)
963 res = sceAtaExecCmd(NULL, 1, ATA_S_SMART_RETURN_STATUS, 0, 0, 0x4f, 0xc2, (device << 4) & 0xffff, ATA_C_SMART);
967 res = sceAtaWaitResult();
972 if (((ata_hwport->r_lcyl & 0xFF) != 0x4f) || ((ata_hwport->r_hcyl & 0xFF) != 0xc2)) {
973 M_PRINTF(
"Error: SMART report exceeded threshold.\n");
981static int ata_device_set_transfer_mode(
int device,
int type,
int mode)
985 res = sceAtaExecCmd(NULL, 1, 3, (type | mode) & 0xff, 0, 0, 0, (device << 4) & 0xffff, ATA_C_SET_FEATURES);
989 res = sceAtaWaitResult();
995#ifdef ATA_MWDMA_MODES
996 case ATA_XFER_MODE_MDMA:
997 ata_multiword_dma_mode(mode);
1000 case ATA_XFER_MODE_UDMA:
1001 ata_ultra_dma_mode(mode);
1003 case ATA_XFER_MODE_PIO:
1007#ifdef ATA_USE_AIFDEV9
1008 case ATA_XFER_MODE_PIO:
1009 aif_tune_drive(0, mode);
1019int sceAtaDmaTransfer(
int device,
void *buf, u32 lba, u32 nsectors,
int dir)
1021 return ata_device_sector_io64(device, buf, (u64)lba, nsectors, dir);
1024int ata_device_sector_io64(
int device,
void *buf, u64 lba, u32 nsectors,
int dir)
1027 int res = 0, retries;
1028 u16 sector, lcyl, hcyl, select, command, len;
1030 while (res == 0 && nsectors > 0) {
1032 if (atad_devinfo[device].lba48 && (ata_dvrp_workaround ? (lba >= atad_devinfo[device].total_sectors) : 1)) {
1034 len = (u16)((nsectors > 65536) ? 65536 : nsectors);
1037 sector = ((lba >> 16) & 0xff00) | (lba & 0xff);
1038 lcyl = ((lba >> 24) & 0xff00) | ((lba >> 8) & 0xff);
1039 hcyl = ((lba >> 32) & 0xff00) | ((lba >> 16) & 0xff);
1042 select = (device << 4) & 0xffff;
1043 command = (dir == 1) ? ATA_C_WRITE_DMA_EXT : ATA_C_READ_DMA_EXT;
1046 len = (nsectors > 256) ? 256 : nsectors;
1047 sector = lba & 0xff;
1048 lcyl = (lba >> 8) & 0xff;
1049 hcyl = (lba >> 16) & 0xff;
1052 select = ((device << 4) | ((lba >> 24) & 0xf)) & 0xffff;
1053 command = (dir == 1) ? ATA_C_WRITE_DMA : ATA_C_READ_DMA;
1056 for (retries = 3; retries > 0; retries--) {
1058#ifdef ATA_GAMESTAR_WORKAROUND
1060 if (ata_gamestar_workaround)
1065 if ((res = sceAtaExecCmd(buf, len, 0, len, sector, lcyl, hcyl, select, command)) != 0)
1069#ifdef ATA_GAMESTAR_WORKAROUND
1070 if (!ata_gamestar_workaround)
1078 res = sceAtaWaitResult();
1081 SPD_REG16(SPD_R_IF_CTRL) &= ~SPD_IF_DMA_ENABLE;
1083 if (res != ATA_RES_ERR_ICRC)
1087 buf = (
void *)((u8 *)buf + len * 512);
1095static void ata_get_security_status(
int device,
ata_devinfo_t *devinfo, u16 *param)
1097 if (ata_device_identify(device, param) == 0)
1102int sceAtaSecuritySetPassword(
int device,
void *password)
1105 u16 *param = ata_param;
1108 if (devinfo[device].security_status & ATA_F_SEC_ENABLED)
1111 memset(param, 0, 512);
1112 memcpy(param + 1, password, 32);
1114 res = sceAtaExecCmd(param, 1, ATA_SCE_SECURITY_SET_PASSWORD, 0, 0, 0, 0, (device << 4) & 0xffff, ATA_C_SCE_SECURITY_CONTROL);
1116 res = sceAtaWaitResult();
1118 ata_get_security_status(device, devinfo, param);
1123int sceAtaSecurityUnLock(
int device,
void *password)
1126 u16 *param = ata_param;
1129 if (!(devinfo[device].security_status & ATA_F_SEC_LOCKED))
1132 memset(param, 0, 512);
1133 memcpy(param + 1, password, 32);
1135 if ((res = sceAtaExecCmd(param, 1, ATA_SCE_SECURITY_UNLOCK, 0, 0, 0, 0, (device << 4) & 0xffff, ATA_C_SCE_SECURITY_CONTROL)) != 0)
1137 if ((res = sceAtaWaitResult()) != 0)
1141 ata_get_security_status(device, devinfo, param);
1142 if (devinfo[device].security_status & ATA_F_SEC_LOCKED)
1143 return ATA_RES_ERR_LOCKED;
1149int sceAtaSecurityEraseUnit(
int device)
1154 if (!(devinfo[device].security_status & ATA_F_SEC_ENABLED) || !(devinfo[device].security_status & ATA_F_SEC_LOCKED))
1158 if ((res = sceAtaExecCmd(NULL, 1, ATA_SCE_SECURITY_ERASE_PREPARE, 0, 0, 0, 0, (device << 4) & 0xffff, ATA_C_SCE_SECURITY_CONTROL)) != 0)
1160 if ((res = sceAtaWaitResult()) != 0)
1163 if ((res = sceAtaExecCmd(NULL, 1, ATA_SCE_SECURITY_ERASE_UNIT, 0, 0, 0, 0, (device << 4) & 0xffff, ATA_C_SCE_SECURITY_CONTROL)) == 0)
1164 res = sceAtaWaitResult();
1167 ata_get_security_status(device, devinfo, NULL);
1176#ifdef ATA_USE_AIFDEV9
1179 u16 nsector, sector, lcyl, hcyl;
1184 if (ata_hwport->r_control & 0x88)
1187 nsector = ata_hwport->r_nsector & 0xff;
1188 sector = ata_hwport->r_sector & 0xff;
1189 lcyl = ata_hwport->r_lcyl & 0xff;
1190 hcyl = ata_hwport->r_hcyl & 0xff;
1191 (void)ata_hwport->r_select;
1193 if ((nsector != 1) || (sector != 1))
1197 if ((lcyl == 0x00) && (hcyl == 0x00))
1199 else if ((lcyl == 0x14) && (hcyl == 0xeb))
1204 ata_hwport->r_lcyl = 0x55;
1205 ata_hwport->r_hcyl = 0xaa;
1206 lcyl = ata_hwport->r_lcyl & 0xff;
1207 hcyl = ata_hwport->r_hcyl & 0xff;
1209 if ((lcyl != 0x55) || (hcyl != 0xaa))
1218#ifdef ATA_USE_AIFDEV9
1222 u32 total_sectors_nonlba48, total_sectors_lba48;
1224 if ((res = sceAtaSoftReset()) != 0)
1227 ata_device_probe(&devinfo[0]);
1228 if (!devinfo[0].exists) {
1229 M_PRINTF(
"Error: Unable to detect HDD 0.\n");
1231 return ATA_RES_ERR_NODEV;
1235 if ((res = ata_device_select(1)) != 0)
1237 if (ata_hwport->r_control & 0xff)
1238 ata_device_probe(&devinfo[1]);
1246 for (i = 0; i < 2; i++) {
1247 if (!devinfo[i].exists)
1252 if (!devinfo[i].has_packet) {
1253 res = ata_device_identify(i, ata_param);
1254 devinfo[i].
exists = (res == 0);
1255 }
else if (devinfo[i].has_packet == 1) {
1258 res = ata_device_pkt_identify(i, ata_param);
1259 devinfo[i].
exists = (res == 0);
1265 if (!devinfo[i].exists || devinfo[i].has_packet)
1271 devinfo[i].
lba48 = (ata_param[ATA_ID_COMMAND_SETS_SUPPORTED] & 0x0400) != 0;
1274 total_sectors_nonlba48 = (ata_param[ATA_ID_SECTOTAL_HI] << 16) | ata_param[ATA_ID_SECTOTAL_LO];
1275 if (ata_param[ATA_ID_48BIT_SECTOTAL_HI]) {
1276 total_sectors_lba48 = 0xffffffff;
1278 total_sectors_lba48 = (ata_param[ATA_ID_48BIT_SECTOTAL_MI] << 16) | ata_param[ATA_ID_48BIT_SECTOTAL_LO];
1285#ifdef ATA_ENABLE_MAXUDMA
1292 for (
int j = maxUDMA; j >= 0; j--) {
1294 if (((ata_param[ATA_ID_UDMA_CONTROL] & 0xFF) & (1 << j)) != 0) {
1299 ata_device_set_transfer_mode(i, ATA_XFER_MODE_UDMA, udmaMode);
1301#ifdef ATA_USE_AIFDEV9
1303 ata_device_set_transfer_mode(i, ATA_XFER_MODE_PIO, 4);
1305 ata_device_smart_enable(i);
1307 sceAtaIdle(i, 0xff);
1309 if (devinfo[i].lba48) {
1311 if (ata_dvrp_workaround) {
1323#ifdef ATA_SCE_AUTH_HDD
1324 if (sceAtaGetSceId(i, ata_param) != 0) {
1325 M_PRINTF(
"error: This is not SCE genuine HDD.\n");
1326 memset(&devinfo[i], 0,
sizeof(devinfo[i]));
1330#ifdef ATA_ENABLE_BDM
1332 bdm_connect_bd(&g_ata_bd[i]);
1341 if (!ata_devinfo_init) {
1342 ata_devinfo_init = 1;
1343 if (ata_bus_reset() || ata_init_devices(atad_devinfo))
1347 return &atad_devinfo[device];
1351static void ata_set_dir(
int dir)
1356 SPD_REG16(0x38) = 3;
1357 val = SPD_REG16(SPD_R_IF_CTRL) & 1;
1358 val |= (dir == ATA_DIR_WRITE) ? 0x4c : 0x4e;
1359 SPD_REG16(SPD_R_IF_CTRL) = val;
1360#ifdef ATA_GAMESTAR_WORKAROUND
1361 SPD_REG16(SPD_R_XFR_CTRL) = dir | (ata_gamestar_workaround ? 0x86 : 0x6);
1363 SPD_REG16(SPD_R_XFR_CTRL) = dir | 0x6;
1367static void ata_pio_mode(
int mode)
1371#ifdef ATA_ALL_PIO_MODES
1391 SPD_REG16(SPD_R_PIO_MODE) = val;
1394 SPD_REG16(SPD_R_PIO_MODE) = 0x92;
1398#ifdef ATA_MWDMA_MODES
1399static void ata_multiword_dma_mode(
int mode)
1415 SPD_REG16(SPD_R_MWDMA_MODE) = val;
1416 SPD_REG16(SPD_R_IF_CTRL) = (SPD_REG16(SPD_R_IF_CTRL) & 0xfffe) | 0x48;
1420static void ata_ultra_dma_mode(
int mode)
1442 SPD_REG16(SPD_R_UDMA_MODE) = val;
1443 SPD_REG16(SPD_R_IF_CTRL) |= 0x49;
1447#ifdef ATA_USE_AIFDEV9
1448static void aif_tune_drive(
int unit,
int mode)
1451 unsigned int use_iordy, value;
1454 value = (use_iordy << 3) | mode;
1455 M_PRINTF(
"AIF HDD: tune unit%d, mode=%u\n", unit, value);
1459 aif_regs[AIF_ATA_TCFG] = (aif_regs[AIF_ATA_TCFG] & 0xf0) + value;
1462 aif_regs[AIF_ATA_TCFG] = (aif_regs[AIF_ATA_TCFG] & 0x0f) + (value << 4);
1469int sceAtaIdleImmediate(
int device)
1473 if (!(res = sceAtaExecCmd(NULL, 1, 0, 0, 0, 0, 0, (device << 4) & 0xFFFF, ATA_C_IDLE_IMMEDIATE)))
1474 res = sceAtaWaitResult();
1479static int ata_device_standby_immediate(
int device)
1483 if (!(res = sceAtaExecCmd(NULL, 1, 0, 0, 0, 0, 0, (device << 4) & 0xFFFF, ATA_C_STANDBY_IMMEDIATE)))
1484 res = sceAtaWaitResult();
1489static void ata_shutdown_cb(
void)
1493 for (i = 0; i < 2; i++) {
1494 if (atad_devinfo[i].exists)
1495 ata_device_standby_immediate(i);
1499#ifdef ATA_ENABLE_BDM
1503static int ata_bd_read(
struct block_device *bd, u64 sector,
void *buffer, u16
count)
1505 if (ata_device_sector_io64(bd->devNr, buffer, sector,
count, ATA_DIR_READ) != 0) {
1512static int ata_bd_write(
struct block_device *bd, u64 sector,
const void *buffer, u16
count)
1514 if (ata_device_sector_io64(bd->devNr, (
void *)buffer, sector,
count, ATA_DIR_WRITE) != 0) {
1523 sceAtaFlushCache(bd->devNr);
1528 ata_device_standby_immediate(bd->devNr);
u32 count
start sector of fragmented bd/file