33 #ifdef ATA_USE_AIFDEV9
47 #define MODNAME "atad"
51 IRX_ID(MODNAME, 2, 7);
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"
57 #define VERSION "v1.2"
59 #define ATA_XFER_MODE_PIO 0x08
60 #ifdef ATA_MWDMA_MODES
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
68 static int ata_devinfo_init = 0;
69 static int ata_evflg = -1;
72 static u8 ata_dvrp_workaround = 0;
74 #ifdef ATA_GAMESTAR_WORKAROUND
75 static u8 ata_gamestar_workaround = 0;
84 static 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))
191 #ifdef ATA_ENABLE_BDM
192 #define NUM_DEVICES 2
193 #define ATA_BD_SECTOR_SIZE 512
198 static int ata_intr_cb(
int flag);
200 #ifdef ATA_USE_AIFDEV9
201 static int ata_intr_cb(
void);
203 static unsigned int ata_alarm_cb(
void *unused);
205 #ifdef ATA_USE_AIFDEV9
206 static void aif_tune_drive(
int unit,
int mode);
210 static void ata_set_dir(
int dir);
212 static void ata_pio_mode(
int mode);
213 #ifdef ATA_MWDMA_MODES
214 static void ata_multiword_dma_mode(
int mode);
216 static void ata_ultra_dma_mode(
int mode);
218 static void ata_shutdown_cb(
void);
220 int ata_device_sector_io64(
int device,
void *buf, u64 lba, u32 nsectors,
int dir);
222 #ifdef ATA_ENABLE_BDM
223 static int ata_bd_read(
struct block_device *bd, u64 sector,
void *buffer, u16
count);
224 static int ata_bd_write(
struct block_device *bd, u64 sector,
const void *buffer, u16
count);
233 static void ata_pre_dma_cb(
int bcr,
int dir)
240 SPD_REG16(SPD_R_XFR_CTRL) |= 0x80;
243 static void ata_post_dma_cb(
int bcr,
int dir)
250 SPD_REG16(SPD_R_XFR_CTRL) &= ~0x80;
254 static int ata_create_event_flag(
void)
260 return CreateEventFlag(&
event);
263 int _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 ata_gamestar_workaround = (SPD_REG16(0x20) != 1) ? 1 : 0;
315 if (ata_gamestar_workaround) {
316 M_PRINTF(
"Compatible adaptor detected.\n");
319 ata_dvrp_workaround = 0;
324 if ((ata_evflg = ata_create_event_flag()) < 0) {
325 M_PRINTF(
"Couldn't create event flag, exiting.\n");
331 SpdRegisterIntrHandler(1, &ata_intr_cb);
332 SpdRegisterIntrHandler(0, &ata_intr_cb);
333 #ifdef ATA_GAMESTAR_WORKAROUND
334 if (!ata_gamestar_workaround) {
336 dev9RegisterPreDmaCb(0, &ata_pre_dma_cb);
337 dev9RegisterPostDmaCb(0, &ata_post_dma_cb);
338 #ifdef ATA_GAMESTAR_WORKAROUND
342 Dev9RegisterPowerOffHandler(15, &ata_shutdown_cb);
345 #ifdef ATA_USE_AIFDEV9
346 aifRegisterIntrCb(AIF_INUM_ATA0, &ata_intr_cb);
347 aifRegisterShutdownCb(AIF_INUM_ATA0, &ata_shutdown_cb);
349 M_PRINTF(
"AIF HDD initialized.\n");
352 #ifdef ATA_ENABLE_BDM
356 for (i = 0; i < NUM_DEVICES; ++i) {
357 g_ata_bd[i].priv = (
void *)&atad_devinfo[i];
358 g_ata_bd[i].name =
"ata";
359 g_ata_bd[i].path =
"ata";
360 g_ata_bd[i].devNr = i;
361 g_ata_bd[i].parNr = 0;
362 g_ata_bd[i].parId = 0x00;
363 g_ata_bd[i].sectorSize = 512;
364 g_ata_bd[i].sectorOffset = 0;
365 g_ata_bd[i].sectorCount = 0;
367 g_ata_bd[i].read = ata_bd_read;
368 g_ata_bd[i].write = ata_bd_write;
369 g_ata_bd[i].flush = ata_bd_flush;
370 g_ata_bd[i].stop = ata_bd_stop;
378 if (RegisterLibraryEntries(&_exp_atad) != 0) {
379 M_PRINTF(
"Library is already registered, exiting.\n");
383 res = MODULE_RESIDENT_END;
384 M_PRINTF(
"Driver loaded.\n");
389 int _exit(
void) {
return MODULE_RESIDENT_END; }
392 static int ata_intr_cb(
int flag)
395 memset(atad_devinfo, 0,
sizeof atad_devinfo);
397 SpdIntrDisable(SPD_INTR_ATA);
398 iSetEventFlag(ata_evflg, ATA_EV_COMPLETE);
405 #ifdef ATA_USE_AIFDEV9
406 static int ata_intr_cb(
void)
408 aifIntrDisable(AIF_INTR_ATA0);
409 iSetEventFlag(ata_evflg, ATA_EV_COMPLETE);
415 static unsigned int ata_alarm_cb(
void *unused)
419 iSetEventFlag(ata_evflg, ATA_EV_TIMEOUT);
424 int sceAtaGetError(
void)
429 #ifdef ATA_USE_AIFDEV9
432 return ata_hwport->r_error & 0xff;
435 #define ATA_WAIT_BUSY 0x80
436 #define ATA_WAIT_BUSBUSY 0x88
438 #define ata_wait_busy() gen_ata_wait_busy(ATA_WAIT_BUSY)
439 #define ata_wait_bus_busy() gen_ata_wait_busy(ATA_WAIT_BUSBUSY)
443 static int gen_ata_wait_busy(
int bits)
448 #ifdef ATA_USE_AIFDEV9
453 for (i = 0; i < 80; i++) {
456 if (!(ata_hwport->r_control & bits))
482 M_PRINTF(
"Timeout while waiting on busy (0x%02x).\n", bits);
483 return ATA_RES_ERR_TIMEOUT;
486 static int ata_device_select(
int device)
491 #ifdef ATA_USE_AIFDEV9
496 if ((res = ata_wait_bus_busy()) < 0)
500 if (((ata_hwport->r_select >> 4) & 1) == device)
504 ata_hwport->r_select = (device & 1) << 4;
505 (void)(ata_hwport->r_control);
506 (void)(ata_hwport->r_control);
508 return ata_wait_bus_busy();
511 static int g_atad_search_command = 0;
512 static int g_atad_search_type = 0;
515 int ata_set_command_type(
int command,
int type)
517 g_atad_search_command = command;
518 g_atad_search_type = type;
533 int sceAtaExecCmd(
void *buf, u32 blkcount, u16 feature, u16 nsector, u16 sector, u16 lcyl, u16 hcyl, u16 select, u16 command)
538 #ifdef ATA_USE_AIFDEV9
543 int i, res, type, cmd_table_size;
544 int using_timeout, device = (select >> 4) & 1;
547 ClearEventFlag(ata_evflg, 0);
549 if (!atad_devinfo[device].exists)
550 return ATA_RES_ERR_NODEV;
552 if ((res = ata_device_select(device)) != 0)
557 if (command == ATA_C_SCE_SECURITY_CONTROL) {
558 cmd_table = sec_ctrl_cmd_table;
559 cmd_table_size = SEC_CTRL_CMD_TABLE_SIZE;
560 searchcmd = (u8)feature;
561 }
else if (command == ATA_C_SMART) {
562 cmd_table = smart_cmd_table;
563 cmd_table_size = SMART_CMD_TABLE_SIZE;
564 searchcmd = (u8)feature;
566 cmd_table = ata_cmd_table;
567 cmd_table_size = ATA_CMD_TABLE_SIZE;
568 searchcmd = (u8)command;
572 for (i = 0; i < cmd_table_size; i++) {
573 if (searchcmd == cmd_table[i].command) {
574 type = cmd_table[i].type;
580 if (!type && searchcmd == g_atad_search_command)
581 type = g_atad_search_type;
583 if (!(atad_cmd_state.type = type & 0x7F))
584 return ATA_RES_ERR_CMD;
586 atad_cmd_state.buf = buf;
587 atad_cmd_state.blkcount = blkcount;
590 if (!(ata_hwport->r_control & 0x40)) {
592 case ATA_C_DEVICE_RESET:
593 case ATA_C_EXECUTE_DEVICE_DIAGNOSTIC:
594 case ATA_C_INITIALIZE_DEVICE_PARAMETERS:
596 case ATA_C_IDENTIFY_PACKET_DEVICE:
599 M_PRINTF(
"Error: Device %d is not ready.\n", device);
600 return ATA_RES_ERR_NOTREADY;
606 switch (type & 0x7F) {
612 atad_cmd_state.dir = (command != ATA_C_READ_DMA && command != ATA_C_READ_DMA_EXT);
618 cmd_timeout.lo = 0x41eb0000;
622 if (command == ATA_C_SCE_SECURITY_CONTROL && feature == ATA_SCE_SECURITY_ERASE_UNIT)
623 USec2SysClock(180000000, &cmd_timeout);
625 if ((res = SetAlarm(&cmd_timeout, &ata_alarm_cb, NULL)) < 0)
630 if ((type & 0x7F) == 1) {
632 SpdIntrEnable(SPD_INTR_ATA0);
634 #ifdef ATA_USE_AIFDEV9
635 aifIntrEnable(AIF_INTR_ATA0);
640 ata_hwport->r_control = (using_timeout == 0) << 1;
647 ata_hwport->r_feature = (feature >> 8) & 0xff;
648 ata_hwport->r_nsector = (nsector >> 8) & 0xff;
649 ata_hwport->r_sector = (sector >> 8) & 0xff;
650 ata_hwport->r_lcyl = (lcyl >> 8) & 0xff;
651 ata_hwport->r_hcyl = (hcyl >> 8) & 0xff;
654 ata_hwport->r_feature = feature & 0xff;
655 ata_hwport->r_nsector = nsector & 0xff;
656 ata_hwport->r_sector = sector & 0xff;
657 ata_hwport->r_lcyl = lcyl & 0xff;
658 ata_hwport->r_hcyl = hcyl & 0xff;
659 ata_hwport->r_select = (select | ATA_SEL_LBA) & 0xff;
660 ata_hwport->r_command = command & 0xff;
676 #ifdef ATA_USE_AIFDEV9
681 u16 status = ata_hwport->r_status & 0xff;
683 if (status & ATA_STAT_ERR) {
684 M_PRINTF(
"Error: PIO cmd error: status 0x%02x, error 0x%02x.\n", status, sceAtaGetError());
685 return ATA_RES_ERR_IO;
689 if (!(status & ATA_STAT_DRQ))
690 return ATA_RES_ERR_NODATA;
692 type = cmd_state->type;
694 if (type == 3 || type == 8) {
696 buf16 = cmd_state->buf16;
697 for (i = 0; i < 256; i++) {
698 ata_hwport->r_data = *buf16;
699 cmd_state->buf16 = ++buf16;
701 if (cmd_state->type == 8) {
704 buf8 = cmd_state->buf8;
705 for (i = 0; i < 4; i++) {
706 ata_hwport->r_data = *buf8;
707 cmd_state->buf8 = ++buf8;
710 }
else if (type == 2) {
712 buf16 = cmd_state->buf16;
713 for (i = 0; i < 256; i++) {
714 *buf16 = ata_hwport->r_data;
715 cmd_state->buf16 = ++buf16;
724 static int ata_dma_complete(
void *buf, u32 blkcount,
int dir)
728 u32 bits,
count, nbytes;
733 for (i = 0; i < 20; i++)
734 if ((dma_stat = SPD_REG16(0x38) & 0x1f))
740 SpdIntrEnable(SPD_INTR_ATA);
742 WaitEventFlag(ata_evflg, ATA_EV_TIMEOUT | ATA_EV_COMPLETE, WEF_CLEAR | WEF_OR, &bits);
744 if (bits & ATA_EV_TIMEOUT) {
745 M_PRINTF(
"Error: DMA timeout.\n");
746 return ATA_RES_ERR_TIMEOUT;
749 if (!(SPD_REG16(SPD_R_INTR_STAT) & 0x02)) {
750 if (ata_hwport->r_control & 0x01) {
751 M_PRINTF(
"Error: Command error while doing DMA.\n");
752 M_PRINTF(
"Error: Command error status 0x%02x, error 0x%02x.\n", ata_hwport->r_status, sceAtaGetError());
754 return ((sceAtaGetError() & ATA_ERR_ICRC) ? ATA_RES_ERR_ICRC : ATA_RES_ERR_IO);
756 M_PRINTF(
"Warning: Got command interrupt, but not an error.\n");
761 dma_stat = SPD_REG16(0x38) & 0x1f;
764 count = (blkcount < dma_stat) ? blkcount : dma_stat;
765 nbytes =
count * 512;
766 if ((res = SpdDmaTransfer(0, buf, (nbytes << 9) | 32, dir)) < 0)
769 buf = (
void *)((u8 *)buf + nbytes);
778 int sceAtaWaitResult(
void)
784 #ifdef ATA_USE_AIFDEV9
789 int res = 0, type = cmd_state->type;
792 if (type == 1 || type == 6) {
793 WaitEventFlag(ata_evflg, ATA_EV_TIMEOUT | ATA_EV_COMPLETE, WEF_CLEAR | WEF_OR, &bits);
794 if (bits & ATA_EV_TIMEOUT) {
795 M_PRINTF(
"Error: ATA timeout on a non-data command.\n");
796 return ATA_RES_ERR_TIMEOUT;
798 }
else if (type == 4) {
802 if ((res = ata_dma_complete(cmd_state->buf, cmd_state->blkcount,
803 cmd_state->dir)) < 0)
806 for (i = 0; i < 100; i++)
807 if ((stat = SPD_REG16(SPD_R_INTR_STAT) & 0x01))
810 SpdIntrEnable(SPD_INTR_ATA0);
811 WaitEventFlag(ata_evflg, ATA_EV_TIMEOUT | ATA_EV_COMPLETE, WEF_CLEAR | WEF_OR, &bits);
812 if (bits & ATA_EV_TIMEOUT) {
813 M_PRINTF(
"Error: ATA timeout on DMA completion, buffer stat %04x\n", SPD_REG16(0x38));
814 M_PRINTF(
"Error: istat %x, ienable %x\n", SPD_REG16(SPD_R_INTR_STAT), SPD_REG16(SPD_R_INTR_MASK));
815 res = ATA_RES_ERR_TIMEOUT;
819 #ifdef ATA_USE_AIFDEV9
823 stat = ata_hwport->r_control;
824 if ((res = ata_wait_busy()) < 0)
828 while ((
int)(--cmd_state->blkcount) != -1) {
829 if ((res = ata_pio_transfer(cmd_state)) < 0)
831 if ((res = ata_wait_busy()) < 0)
840 if (ata_hwport->r_status & ATA_STAT_BUSY)
841 res = ata_wait_busy();
842 if ((stat = ata_hwport->r_status) & ATA_STAT_ERR) {
843 M_PRINTF(
"Error: Command error: status 0x%02x, error 0x%02x.\n", stat, sceAtaGetError());
845 res = (sceAtaGetError() & ATA_ERR_ICRC) ? ATA_RES_ERR_ICRC : ATA_RES_ERR_IO;
850 CancelAlarm(&ata_alarm_cb, NULL);
857 M_PRINTF(
"error: ATA failed, %d\n", res);
863 static int ata_bus_reset(
void)
867 SPD_REG16(SPD_R_IF_CTRL) = SPD_IF_ATA_RESET;
869 SPD_REG16(SPD_R_IF_CTRL) = 0;
870 SPD_REG16(SPD_R_IF_CTRL) = 0x48;
873 return ata_wait_busy();
877 int sceAtaSoftReset(
void)
882 #ifdef ATA_USE_AIFDEV9
886 if (ata_hwport->r_control & 0x80)
887 return ATA_RES_ERR_NOTREADY;
890 ata_hwport->r_control = 6;
894 ata_hwport->r_control = 2;
897 return ata_wait_busy();
901 int sceAtaFlushCache(
int device)
905 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)))
906 res = sceAtaWaitResult();
912 int sceAtaIdle(
int device,
int period)
916 if (!(res = sceAtaExecCmd(NULL, 1, 0, period & 0xff, 0, 0, 0, (device << 4) & 0xffff, ATA_C_IDLE)))
917 res = sceAtaWaitResult();
922 static int ata_device_identify(
int device,
void *
info)
926 if (!(res = sceAtaExecCmd(
info, 1, 0, 0, 0, 0, 0, (device << 4) & 0xffff, ATA_C_IDENTIFY_DEVICE)))
927 res = sceAtaWaitResult();
932 static int ata_device_pkt_identify(
int device,
void *
info)
936 res = sceAtaExecCmd(
info, 1, 0, 0, 0, 0, 0, (device << 4) & 0xffff, ATA_C_IDENTIFY_PACKET_DEVICE);
938 return sceAtaWaitResult();
943 int sceAtaGetSceId(
int device,
void *data)
947 if (!(res = sceAtaExecCmd(data, 1, ATA_SCE_IDENTIFY_DRIVE, 0, 0, 0, 0, (device << 4) & 0xffff, ATA_C_SCE_SECURITY_CONTROL)))
948 res = sceAtaWaitResult();
953 static int ata_device_smart_enable(
int device)
957 if (!(res = sceAtaExecCmd(NULL, 1, ATA_S_SMART_ENABLE_OPERATIONS, 0, 0, 0x4f, 0xc2, (device << 4) & 0xffff, ATA_C_SMART)))
958 res = sceAtaWaitResult();
964 int sceAtaSmartSaveAttr(
int device)
968 if (!(res = sceAtaExecCmd(NULL, 1, ATA_S_SMART_SAVE_ATTRIBUTE_VALUES, 0, 0, 0x4f, 0xc2, (device << 4) & 0xffff, ATA_C_SMART)))
969 res = sceAtaWaitResult();
975 int sceAtaSmartReturnStatus(
int device)
980 res = sceAtaExecCmd(NULL, 1, ATA_S_SMART_RETURN_STATUS, 0, 0, 0x4f, 0xc2, (device << 4) & 0xffff, ATA_C_SMART);
984 res = sceAtaWaitResult();
989 if (((ata_hwport->r_lcyl & 0xFF) != 0x4f) || ((ata_hwport->r_hcyl & 0xFF) != 0xc2)) {
990 M_PRINTF(
"Error: SMART report exceeded threshold.\n");
998 static int ata_device_set_transfer_mode(
int device,
int type,
int mode)
1002 res = sceAtaExecCmd(NULL, 1, 3, (type | mode) & 0xff, 0, 0, 0, (device << 4) & 0xffff, ATA_C_SET_FEATURES);
1006 res = sceAtaWaitResult();
1012 #ifdef ATA_MWDMA_MODES
1013 case ATA_XFER_MODE_MDMA:
1014 ata_multiword_dma_mode(mode);
1017 case ATA_XFER_MODE_UDMA:
1018 ata_ultra_dma_mode(mode);
1020 case ATA_XFER_MODE_PIO:
1024 #ifdef ATA_USE_AIFDEV9
1025 case ATA_XFER_MODE_PIO:
1026 aif_tune_drive(0, mode);
1036 int sceAtaDmaTransfer(
int device,
void *buf, u32 lba, u32 nsectors,
int dir)
1038 return ata_device_sector_io64(device, buf, (u64)lba, nsectors, dir);
1041 int ata_device_sector_io64(
int device,
void *buf, u64 lba, u32 nsectors,
int dir)
1044 int res = 0, retries;
1045 u16 sector, lcyl, hcyl, select, command, len;
1048 while (res == 0 && nsectors > 0) {
1050 if (atad_devinfo[device].lba48 && (ata_dvrp_workaround ? (lba >= atad_devinfo[device].total_sectors) : 1)) {
1052 chunk = (nsectors > 65536) ? 65536 : nsectors;
1056 sector = ((lba >> 16) & 0xff00) | (lba & 0xff);
1057 lcyl = ((lba >> 24) & 0xff00) | ((lba >> 8) & 0xff);
1058 hcyl = ((lba >> 32) & 0xff00) | ((lba >> 16) & 0xff);
1061 select = (device << 4) & 0xffff;
1062 command = (dir == 1) ? ATA_C_WRITE_DMA_EXT : ATA_C_READ_DMA_EXT;
1065 chunk = (nsectors > 256) ? 256 : nsectors;
1067 sector = lba & 0xff;
1068 lcyl = (lba >> 8) & 0xff;
1069 hcyl = (lba >> 16) & 0xff;
1072 select = ((device << 4) | ((lba >> 24) & 0xf)) & 0xffff;
1073 command = (dir == 1) ? ATA_C_WRITE_DMA : ATA_C_READ_DMA;
1076 for (retries = 3; retries > 0; retries--) {
1078 #ifdef ATA_GAMESTAR_WORKAROUND
1080 if (ata_gamestar_workaround)
1085 if ((res = sceAtaExecCmd(buf, chunk, 0, len, sector, lcyl, hcyl, select, command)) != 0)
1089 #ifdef ATA_GAMESTAR_WORKAROUND
1090 if (!ata_gamestar_workaround)
1098 res = sceAtaWaitResult();
1101 SPD_REG16(SPD_R_IF_CTRL) &= ~SPD_IF_DMA_ENABLE;
1103 if (res != ATA_RES_ERR_ICRC)
1107 buf = (
void *)((u8 *)buf + chunk * 512);
1115 static void ata_get_security_status(
int device,
ata_devinfo_t *devinfo, u16 *param)
1117 if (ata_device_identify(device, param) == 0)
1122 int sceAtaSecuritySetPassword(
int device,
void *password)
1125 u16 *param = ata_param;
1128 if (devinfo[device].security_status & ATA_F_SEC_ENABLED)
1131 memset(param, 0, 512);
1132 memcpy(param + 1, password, 32);
1134 res = sceAtaExecCmd(param, 1, ATA_SCE_SECURITY_SET_PASSWORD, 0, 0, 0, 0, (device << 4) & 0xffff, ATA_C_SCE_SECURITY_CONTROL);
1136 res = sceAtaWaitResult();
1138 ata_get_security_status(device, devinfo, param);
1143 int sceAtaSecurityUnLock(
int device,
void *password)
1146 u16 *param = ata_param;
1149 if (!(devinfo[device].security_status & ATA_F_SEC_LOCKED))
1152 memset(param, 0, 512);
1153 memcpy(param + 1, password, 32);
1155 if ((res = sceAtaExecCmd(param, 1, ATA_SCE_SECURITY_UNLOCK, 0, 0, 0, 0, (device << 4) & 0xffff, ATA_C_SCE_SECURITY_CONTROL)) != 0)
1157 if ((res = sceAtaWaitResult()) != 0)
1161 ata_get_security_status(device, devinfo, param);
1162 if (devinfo[device].security_status & ATA_F_SEC_LOCKED)
1163 return ATA_RES_ERR_LOCKED;
1169 int sceAtaSecurityEraseUnit(
int device)
1174 if (!(devinfo[device].security_status & ATA_F_SEC_ENABLED) || !(devinfo[device].security_status & ATA_F_SEC_LOCKED))
1178 if ((res = sceAtaExecCmd(NULL, 1, ATA_SCE_SECURITY_ERASE_PREPARE, 0, 0, 0, 0, (device << 4) & 0xffff, ATA_C_SCE_SECURITY_CONTROL)) != 0)
1180 if ((res = sceAtaWaitResult()) != 0)
1183 if ((res = sceAtaExecCmd(NULL, 1, ATA_SCE_SECURITY_ERASE_UNIT, 0, 0, 0, 0, (device << 4) & 0xffff, ATA_C_SCE_SECURITY_CONTROL)) == 0)
1184 res = sceAtaWaitResult();
1187 ata_get_security_status(device, devinfo, NULL);
1196 #ifdef ATA_USE_AIFDEV9
1199 u16 nsector, sector, lcyl, hcyl;
1204 if (ata_hwport->r_control & 0x88)
1207 nsector = ata_hwport->r_nsector & 0xff;
1208 sector = ata_hwport->r_sector & 0xff;
1209 lcyl = ata_hwport->r_lcyl & 0xff;
1210 hcyl = ata_hwport->r_hcyl & 0xff;
1211 (void)ata_hwport->r_select;
1213 if ((nsector != 1) || (sector != 1))
1217 if ((lcyl == 0x00) && (hcyl == 0x00))
1219 else if ((lcyl == 0x14) && (hcyl == 0xeb))
1224 ata_hwport->r_lcyl = 0x55;
1225 ata_hwport->r_hcyl = 0xaa;
1226 lcyl = ata_hwport->r_lcyl & 0xff;
1227 hcyl = ata_hwport->r_hcyl & 0xff;
1229 if ((lcyl != 0x55) || (hcyl != 0xaa))
1238 #ifdef ATA_USE_AIFDEV9
1242 u32 total_sectors_nonlba48, total_sectors_lba48;
1245 if (!ata_dvrp_workaround) {
1246 for (i = 0; i < 2; i++) {
1247 if ((res = sceAtaSoftReset()) != 0)
1250 ata_device_select(i);
1251 if (ata_hwport->r_control & 0xff)
1252 ata_device_probe(&devinfo[i]);
1257 if ((res = sceAtaSoftReset()) != 0)
1260 ata_device_probe(&devinfo[0]);
1263 if (!devinfo[0].exists) {
1264 M_PRINTF(
"Error: Unable to detect HDD 0.\n");
1266 return ATA_RES_ERR_NODEV;
1269 if (ata_dvrp_workaround) {
1271 if ((res = ata_device_select(1)) != 0)
1273 if (ata_hwport->r_control & 0xff)
1274 ata_device_probe(&devinfo[1]);
1283 for (i = 0; i < 2; i++) {
1284 if (!devinfo[i].exists)
1289 if (!devinfo[i].has_packet) {
1290 res = ata_device_identify(i, ata_param);
1291 devinfo[i].
exists = (res == 0);
1292 }
else if (devinfo[i].has_packet == 1) {
1295 res = ata_device_pkt_identify(i, ata_param);
1296 devinfo[i].
exists = (res == 0);
1302 if (!devinfo[i].exists || devinfo[i].has_packet)
1308 devinfo[i].
lba48 = (ata_param[ATA_ID_COMMAND_SETS_SUPPORTED] & 0x0400) != 0;
1311 total_sectors_nonlba48 = (ata_param[ATA_ID_SECTOTAL_HI] << 16) | ata_param[ATA_ID_SECTOTAL_LO];
1312 if (ata_param[ATA_ID_48BIT_SECTOTAL_HI]) {
1313 total_sectors_lba48 = 0xffffffff;
1315 total_sectors_lba48 = (ata_param[ATA_ID_48BIT_SECTOTAL_MI] << 16) | ata_param[ATA_ID_48BIT_SECTOTAL_LO];
1322 #ifdef ATA_ENABLE_MAXUDMA
1329 for (
int j = maxUDMA; j >= 0; j--) {
1331 if (((ata_param[ATA_ID_UDMA_CONTROL] & 0xFF) & (1 << j)) != 0) {
1336 ata_device_set_transfer_mode(i, ATA_XFER_MODE_UDMA, udmaMode);
1338 #ifdef ATA_USE_AIFDEV9
1340 ata_device_set_transfer_mode(i, ATA_XFER_MODE_PIO, 4);
1342 ata_device_smart_enable(i);
1344 sceAtaIdle(i, 0xff);
1346 if (devinfo[i].lba48) {
1348 if (ata_dvrp_workaround) {
1352 if (ata_param[121] == 0x4456 &&
1353 ata_param[122] == 0x524c &&
1354 ata_param[123] == 0x4241 &&
1355 ata_param[124] == 0x3438) {
1356 M_PRINTF(
"LBA48-capable DVRP firmware detected, disabling the workaround\n");
1357 ata_dvrp_workaround = 0;
1372 #ifdef ATA_SCE_AUTH_HDD
1373 if (sceAtaGetSceId(i, ata_param) != 0) {
1374 M_PRINTF(
"error: This is not SCE genuine HDD.\n");
1375 memset(&devinfo[i], 0,
sizeof(devinfo[i]));
1379 #ifdef ATA_ENABLE_BDM
1381 bdm_connect_bd(&g_ata_bd[i]);
1390 if (!ata_devinfo_init) {
1391 ata_devinfo_init = 1;
1392 if (ata_bus_reset() || ata_init_devices(atad_devinfo))
1396 return &atad_devinfo[device];
1400 static void ata_set_dir(
int dir)
1405 SPD_REG16(0x38) = 3;
1406 val = SPD_REG16(SPD_R_IF_CTRL) & 1;
1407 val |= (dir == ATA_DIR_WRITE) ? 0x4c : 0x4e;
1408 SPD_REG16(SPD_R_IF_CTRL) = val;
1409 #ifdef ATA_GAMESTAR_WORKAROUND
1410 SPD_REG16(SPD_R_XFR_CTRL) = dir | (ata_gamestar_workaround ? 0x86 : 0x6);
1412 SPD_REG16(SPD_R_XFR_CTRL) = dir | 0x6;
1416 static void ata_pio_mode(
int mode)
1420 #ifdef ATA_ALL_PIO_MODES
1440 SPD_REG16(SPD_R_PIO_MODE) = val;
1443 SPD_REG16(SPD_R_PIO_MODE) = 0x92;
1447 #ifdef ATA_MWDMA_MODES
1448 static void ata_multiword_dma_mode(
int mode)
1464 SPD_REG16(SPD_R_MWDMA_MODE) = val;
1465 SPD_REG16(SPD_R_IF_CTRL) = (SPD_REG16(SPD_R_IF_CTRL) & 0xfffe) | 0x48;
1469 static void ata_ultra_dma_mode(
int mode)
1491 SPD_REG16(SPD_R_UDMA_MODE) = val;
1492 SPD_REG16(SPD_R_IF_CTRL) |= 0x49;
1496 #ifdef ATA_USE_AIFDEV9
1497 static void aif_tune_drive(
int unit,
int mode)
1500 unsigned int use_iordy, value;
1503 value = (use_iordy << 3) | mode;
1504 M_PRINTF(
"AIF HDD: tune unit%d, mode=%u\n", unit, value);
1508 aif_regs[AIF_ATA_TCFG] = (aif_regs[AIF_ATA_TCFG] & 0xf0) + value;
1511 aif_regs[AIF_ATA_TCFG] = (aif_regs[AIF_ATA_TCFG] & 0x0f) + (value << 4);
1518 int sceAtaIdleImmediate(
int device)
1522 if (!(res = sceAtaExecCmd(NULL, 1, 0, 0, 0, 0, 0, (device << 4) & 0xFFFF, ATA_C_IDLE_IMMEDIATE)))
1523 res = sceAtaWaitResult();
1528 static int ata_device_standby_immediate(
int device)
1532 if (!(res = sceAtaExecCmd(NULL, 1, 0, 0, 0, 0, 0, (device << 4) & 0xFFFF, ATA_C_STANDBY_IMMEDIATE)))
1533 res = sceAtaWaitResult();
1538 static void ata_shutdown_cb(
void)
1542 for (i = 0; i < 2; i++) {
1543 if (atad_devinfo[i].exists)
1544 ata_device_standby_immediate(i);
1548 #ifdef ATA_ENABLE_BDM
1552 static int ata_bd_read(
struct block_device *bd, u64 sector,
void *buffer, u16
count)
1554 if (ata_device_sector_io64(bd->devNr, buffer, sector,
count, ATA_DIR_READ) != 0) {
1561 static int ata_bd_write(
struct block_device *bd, u64 sector,
const void *buffer, u16
count)
1563 if (ata_device_sector_io64(bd->devNr, (
void *)buffer, sector,
count, ATA_DIR_WRITE) != 0) {
1572 sceAtaFlushCache(bd->devNr);
1577 ata_device_standby_immediate(bd->devNr);