1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Alan Korr
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
31 #include "ata_idle_notify.h"
32 #include "ata-target.h"
34 #define SECTOR_SIZE (512)
36 #define ATA_FEATURE ATA_ERROR
38 #define ATA_STATUS ATA_COMMAND
39 #define ATA_ALT_STATUS ATA_CONTROL
41 #define SELECT_DEVICE1 0x10
42 #define SELECT_LBA 0x40
44 #define CONTROL_nIEN 0x02
45 #define CONTROL_SRST 0x04
47 #define CMD_READ_SECTORS 0x20
48 #define CMD_WRITE_SECTORS 0x30
49 #define CMD_WRITE_SECTORS_EXT 0x34
50 #define CMD_READ_MULTIPLE 0xC4
51 #define CMD_READ_MULTIPLE_EXT 0x29
52 #define CMD_WRITE_MULTIPLE 0xC5
53 #define CMD_SET_MULTIPLE_MODE 0xC6
54 #define CMD_STANDBY_IMMEDIATE 0xE0
55 #define CMD_STANDBY 0xE2
56 #define CMD_IDENTIFY 0xEC
57 #define CMD_SLEEP 0xE6
58 #define CMD_SET_FEATURES 0xEF
59 #define CMD_SECURITY_FREEZE_LOCK 0xF5
63 #define READ_TIMEOUT 5*HZ
65 #ifdef HAVE_ATA_POWER_OFF
66 #define ATA_POWER_OFF_TIMEOUT 2*HZ
69 static struct mutex ata_mtx
;
70 int ata_device
; /* device 0 (master) or 1 (slave) */
72 int ata_spinup_time
= 0;
73 #if (CONFIG_LED == LED_REAL)
74 static bool ata_led_enabled
= true;
75 static bool ata_led_on
= false;
77 static bool spinup
= false;
78 static bool sleeping
= true;
79 static bool poweroff
= false;
80 static long sleep_timeout
= 5*HZ
;
82 static bool lba48
= false; /* set for 48 bit addressing */
84 static long ata_stack
[(DEFAULT_STACK_SIZE
*3)/sizeof(long)];
85 static const char ata_thread_name
[] = "ata";
86 static struct event_queue ata_queue
;
87 static bool initialized
= false;
89 static long last_user_activity
= -1;
90 long last_disk_activity
= -1;
92 static int multisectors
; /* number of supported multisectors */
93 static unsigned short identify_info
[SECTOR_SIZE
];
95 #ifdef MAX_PHYS_SECTOR_SIZE
96 struct sector_cache_entry
{
98 unsigned long sectornum
; /* logical sector */
99 unsigned char data
[MAX_PHYS_SECTOR_SIZE
];
101 /* buffer for reading and writing large physical sectors */
103 static struct sector_cache_entry sector_cache
;
104 static int phys_sector_mult
= 1;
107 static int ata_power_on(void);
108 static int perform_soft_reset(void);
109 static int set_multiple_mode(int sectors
);
110 static int set_features(void);
112 STATICIRAM
int wait_for_bsy(void) ICODE_ATTR
;
113 STATICIRAM
int wait_for_bsy(void)
115 long timeout
= current_tick
+ HZ
*30;
116 while (TIME_BEFORE(current_tick
, timeout
) && (ATA_STATUS
& STATUS_BSY
)) {
117 last_disk_activity
= current_tick
;
121 if (TIME_BEFORE(current_tick
, timeout
))
124 return 0; /* timeout */
127 STATICIRAM
int wait_for_rdy(void) ICODE_ATTR
;
128 STATICIRAM
int wait_for_rdy(void)
135 timeout
= current_tick
+ HZ
*10;
137 while (TIME_BEFORE(current_tick
, timeout
) &&
138 !(ATA_ALT_STATUS
& STATUS_RDY
)) {
139 last_disk_activity
= current_tick
;
143 if (TIME_BEFORE(current_tick
, timeout
))
146 return 0; /* timeout */
149 STATICIRAM
int wait_for_start_of_transfer(void) ICODE_ATTR
;
150 STATICIRAM
int wait_for_start_of_transfer(void)
155 return (ATA_ALT_STATUS
& (STATUS_BSY
|STATUS_DRQ
)) == STATUS_DRQ
;
158 STATICIRAM
int wait_for_end_of_transfer(void) ICODE_ATTR
;
159 STATICIRAM
int wait_for_end_of_transfer(void)
163 return (ATA_ALT_STATUS
& (STATUS_RDY
|STATUS_DRQ
)) == STATUS_RDY
;
166 #if (CONFIG_LED == LED_REAL)
167 /* Conditionally block LED access for the ATA driver, so the LED can be
168 * (mis)used for other purposes */
169 static void ata_led(bool on
)
176 #define ata_led(on) led(on)
179 #ifndef ATA_OPTIMIZED_READING
180 STATICIRAM
void copy_read_sectors(unsigned char* buf
, int wordcount
) ICODE_ATTR
;
181 STATICIRAM
void copy_read_sectors(unsigned char* buf
, int wordcount
)
183 unsigned short tmp
= 0;
185 if ( (unsigned long)buf
& 1)
186 { /* not 16-bit aligned, copy byte by byte */
187 unsigned char* bufend
= buf
+ wordcount
*2;
191 #if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN)
192 *buf
++ = tmp
& 0xff; /* I assume big endian */
193 *buf
++ = tmp
>> 8; /* and don't use the SWAB16 macro */
198 } while (buf
< bufend
); /* tail loop is faster */
201 { /* 16-bit aligned, can do faster copy */
202 unsigned short* wbuf
= (unsigned short*)buf
;
203 unsigned short* wbufend
= wbuf
+ wordcount
;
207 *wbuf
= swap16(ATA_DATA
);
211 } while (++wbuf
< wbufend
); /* tail loop is faster */
214 #endif /* !ATA_OPTIMIZED_READING */
216 #ifdef MAX_PHYS_SECTOR_SIZE
217 static int _read_sectors(unsigned long start
,
221 int ata_read_sectors(IF_MV2(int drive
,)
233 #ifndef MAX_PHYS_SECTOR_SIZE
234 #ifdef HAVE_MULTIVOLUME
235 (void)drive
; /* unused for now */
237 spinlock_lock(&ata_mtx
);
240 last_disk_activity
= current_tick
;
241 spinup_start
= current_tick
;
248 if (ata_power_on()) {
249 spinlock_unlock(&ata_mtx
);
255 if (perform_soft_reset()) {
256 spinlock_unlock(&ata_mtx
);
263 timeout
= current_tick
+ READ_TIMEOUT
;
265 SET_REG(ATA_SELECT
, ata_device
);
268 spinlock_unlock(&ata_mtx
);
276 while (TIME_BEFORE(current_tick
, timeout
)) {
278 last_disk_activity
= current_tick
;
283 SET_REG(ATA_NSECTOR
, count
>> 8);
284 SET_REG(ATA_NSECTOR
, count
& 0xff);
285 SET_REG(ATA_SECTOR
, (start
>> 24) & 0xff); /* 31:24 */
286 SET_REG(ATA_SECTOR
, start
& 0xff); /* 7:0 */
287 SET_REG(ATA_LCYL
, 0); /* 39:32 */
288 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff); /* 15:8 */
289 SET_REG(ATA_HCYL
, 0); /* 47:40 */
290 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff); /* 23:16 */
291 SET_REG(ATA_SELECT
, SELECT_LBA
| ata_device
);
292 SET_REG(ATA_COMMAND
, CMD_READ_MULTIPLE_EXT
);
297 SET_REG(ATA_NSECTOR
, count
& 0xff); /* 0 means 256 sectors */
298 SET_REG(ATA_SECTOR
, start
& 0xff);
299 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff);
300 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff);
301 SET_REG(ATA_SELECT
, ((start
>> 24) & 0xf) | SELECT_LBA
| ata_device
);
302 SET_REG(ATA_COMMAND
, CMD_READ_MULTIPLE
);
305 /* wait at least 400ns between writing command and reading status */
306 __asm__
volatile ("nop");
307 __asm__
volatile ("nop");
308 __asm__
volatile ("nop");
309 __asm__
volatile ("nop");
310 __asm__
volatile ("nop");
317 if (!wait_for_start_of_transfer()) {
318 /* We have timed out waiting for RDY and/or DRQ, possibly
319 because the hard drive is shaking and has problems reading
320 the data. We have two options:
322 2) Perform a soft reset and try again.
324 We choose alternative 2.
326 perform_soft_reset();
332 ata_spinup_time
= current_tick
- spinup_start
;
338 /* read the status register exactly once per loop */
341 if (count
>= multisectors
)
342 sectors
= multisectors
;
346 wordcount
= sectors
* SECTOR_SIZE
/ 2;
348 copy_read_sectors(buf
, wordcount
);
351 "Device errors encountered during READ MULTIPLE commands are
352 posted at the beginning of the block or partial block transfer,
353 but the DRQ bit is still set to one and the data transfer shall
354 take place, including transfer of corrupted data, if any."
357 if ( status
& (STATUS_BSY
| STATUS_ERR
| STATUS_DF
) ) {
358 perform_soft_reset();
363 buf
+= sectors
* SECTOR_SIZE
; /* Advance one chunk of sectors */
366 last_disk_activity
= current_tick
;
369 if(!ret
&& !wait_for_end_of_transfer()) {
370 perform_soft_reset();
378 #ifndef MAX_PHYS_SECTOR_SIZE
379 spinlock_unlock(&ata_mtx
);
385 #ifndef ATA_OPTIMIZED_WRITING
386 STATICIRAM
void copy_write_sectors(const unsigned char* buf
, int wordcount
)
388 STATICIRAM
void copy_write_sectors(const unsigned char* buf
, int wordcount
)
390 if ( (unsigned long)buf
& 1)
391 { /* not 16-bit aligned, copy byte by byte */
392 unsigned short tmp
= 0;
393 const unsigned char* bufend
= buf
+ wordcount
*2;
396 #if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN)
397 tmp
= (unsigned short) *buf
++;
398 tmp
|= (unsigned short) *buf
++ << 8;
399 SET_16BITREG(ATA_DATA
, tmp
);
401 tmp
= (unsigned short) *buf
++ << 8;
402 tmp
|= (unsigned short) *buf
++;
403 SET_16BITREG(ATA_DATA
, tmp
);
405 } while (buf
< bufend
); /* tail loop is faster */
408 { /* 16-bit aligned, can do faster copy */
409 unsigned short* wbuf
= (unsigned short*)buf
;
410 unsigned short* wbufend
= wbuf
+ wordcount
;
414 SET_16BITREG(ATA_DATA
, swap16(*wbuf
));
416 SET_16BITREG(ATA_DATA
, *wbuf
);
418 } while (++wbuf
< wbufend
); /* tail loop is faster */
421 #endif /* !ATA_OPTIMIZED_WRITING */
423 #ifdef MAX_PHYS_SECTOR_SIZE
424 static int _write_sectors(unsigned long start
,
428 int ata_write_sectors(IF_MV2(int drive
,)
439 panicf("Writing on sector 0\n");
441 #ifndef MAX_PHYS_SECTOR_SIZE
442 #ifdef HAVE_MULTIVOLUME
443 (void)drive
; /* unused for now */
445 spinlock_lock(&ata_mtx
);
448 last_disk_activity
= current_tick
;
449 spinup_start
= current_tick
;
456 if (ata_power_on()) {
457 spinlock_unlock(&ata_mtx
);
463 if (perform_soft_reset()) {
464 spinlock_unlock(&ata_mtx
);
471 SET_REG(ATA_SELECT
, ata_device
);
474 spinlock_unlock(&ata_mtx
);
482 SET_REG(ATA_NSECTOR
, count
>> 8);
483 SET_REG(ATA_NSECTOR
, count
& 0xff);
484 SET_REG(ATA_SECTOR
, (start
>> 24) & 0xff); /* 31:24 */
485 SET_REG(ATA_SECTOR
, start
& 0xff); /* 7:0 */
486 SET_REG(ATA_LCYL
, 0); /* 39:32 */
487 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff); /* 15:8 */
488 SET_REG(ATA_HCYL
, 0); /* 47:40 */
489 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff); /* 23:16 */
490 SET_REG(ATA_SELECT
, SELECT_LBA
| ata_device
);
491 SET_REG(ATA_COMMAND
, CMD_WRITE_SECTORS_EXT
);
496 SET_REG(ATA_NSECTOR
, count
& 0xff); /* 0 means 256 sectors */
497 SET_REG(ATA_SECTOR
, start
& 0xff);
498 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff);
499 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff);
500 SET_REG(ATA_SELECT
, ((start
>> 24) & 0xf) | SELECT_LBA
| ata_device
);
501 SET_REG(ATA_COMMAND
, CMD_WRITE_SECTORS
);
504 for (i
=0; i
<count
; i
++) {
506 if (!wait_for_start_of_transfer()) {
512 ata_spinup_time
= current_tick
- spinup_start
;
518 copy_write_sectors(buf
, SECTOR_SIZE
/2);
521 /* reading the status register clears the interrupt */
526 last_disk_activity
= current_tick
;
529 if(!ret
&& !wait_for_end_of_transfer()) {
530 DEBUGF("End on transfer failed. -- jyp");
536 #ifndef MAX_PHYS_SECTOR_SIZE
537 spinlock_unlock(&ata_mtx
);
543 #ifdef MAX_PHYS_SECTOR_SIZE
544 static int cache_sector(unsigned long sector
)
548 sector
&= ~(phys_sector_mult
- 1);
549 /* round down to physical sector boundary */
551 /* check whether the sector is already cached */
552 if (sector_cache
.inuse
&& (sector_cache
.sectornum
== sector
))
555 /* not found: read the sector */
556 sector_cache
.inuse
= false;
557 rc
= _read_sectors(sector
, phys_sector_mult
, sector_cache
.data
);
560 sector_cache
.sectornum
= sector
;
561 sector_cache
.inuse
= true;
566 static inline int flush_current_sector(void)
568 return _write_sectors(sector_cache
.sectornum
, phys_sector_mult
,
572 int ata_read_sectors(IF_MV2(int drive
,)
580 #ifdef HAVE_MULTIVOLUME
581 (void)drive
; /* unused for now */
583 spinlock_lock(&ata_mtx
);
585 offset
= start
& (phys_sector_mult
- 1);
587 if (offset
) /* first partial sector */
589 int partcount
= MIN(incount
, phys_sector_mult
- offset
);
591 rc
= cache_sector(start
);
597 memcpy(inbuf
, sector_cache
.data
+ offset
* SECTOR_SIZE
,
598 partcount
* SECTOR_SIZE
);
601 inbuf
+= partcount
* SECTOR_SIZE
;
602 incount
-= partcount
;
606 offset
= incount
& (phys_sector_mult
- 1);
611 rc
= _read_sectors(start
, incount
, inbuf
);
618 inbuf
+= incount
* SECTOR_SIZE
;
622 rc
= cache_sector(start
);
628 memcpy(inbuf
, sector_cache
.data
, offset
* SECTOR_SIZE
);
633 spinlock_unlock(&ata_mtx
);
638 int ata_write_sectors(IF_MV2(int drive
,)
646 #ifdef HAVE_MULTIVOLUME
647 (void)drive
; /* unused for now */
649 spinlock_lock(&ata_mtx
);
651 offset
= start
& (phys_sector_mult
- 1);
653 if (offset
) /* first partial sector */
655 int partcount
= MIN(count
, phys_sector_mult
- offset
);
657 rc
= cache_sector(start
);
663 memcpy(sector_cache
.data
+ offset
* SECTOR_SIZE
, buf
,
664 partcount
* SECTOR_SIZE
);
665 rc
= flush_current_sector();
672 buf
+= partcount
* SECTOR_SIZE
;
677 offset
= count
& (phys_sector_mult
- 1);
682 rc
= _write_sectors(start
, count
, buf
);
689 buf
+= count
* SECTOR_SIZE
;
693 rc
= cache_sector(start
);
699 memcpy(sector_cache
.data
, buf
, offset
* SECTOR_SIZE
);
700 rc
= flush_current_sector();
710 spinlock_unlock(&ata_mtx
);
714 #endif /* MAX_PHYS_SECTOR_SIZE */
716 static int check_registers(void)
719 if ( ATA_STATUS
& STATUS_BSY
)
722 for (i
= 0; i
<64; i
++) {
723 SET_REG(ATA_NSECTOR
, WRITE_PATTERN1
);
724 SET_REG(ATA_SECTOR
, WRITE_PATTERN2
);
725 SET_REG(ATA_LCYL
, WRITE_PATTERN3
);
726 SET_REG(ATA_HCYL
, WRITE_PATTERN4
);
728 if (((ATA_NSECTOR
& READ_PATTERN1_MASK
) == READ_PATTERN1
) &&
729 ((ATA_SECTOR
& READ_PATTERN2_MASK
) == READ_PATTERN2
) &&
730 ((ATA_LCYL
& READ_PATTERN3_MASK
) == READ_PATTERN3
) &&
731 ((ATA_HCYL
& READ_PATTERN4_MASK
) == READ_PATTERN4
))
737 static int freeze_lock(void)
739 /* does the disk support Security Mode feature set? */
740 if (identify_info
[82] & 2)
742 SET_REG(ATA_SELECT
, ata_device
);
747 SET_REG(ATA_COMMAND
, CMD_SECURITY_FREEZE_LOCK
);
756 void ata_spindown(int seconds
)
758 sleep_timeout
= seconds
* HZ
;
761 bool ata_disk_is_active(void)
766 static int ata_perform_sleep(void)
770 spinlock_lock(&ata_mtx
);
772 SET_REG(ATA_SELECT
, ata_device
);
774 if(!wait_for_rdy()) {
775 DEBUGF("ata_perform_sleep() - not RDY\n");
776 spinlock_unlock(&ata_mtx
);
780 SET_REG(ATA_COMMAND
, CMD_SLEEP
);
784 DEBUGF("ata_perform_sleep() - CMD failed\n");
789 spinlock_unlock(&ata_mtx
);
795 queue_post(&ata_queue
, Q_SLEEP
, 0);
798 void ata_sleepnow(void)
800 if (!spinup
&& !sleeping
&& !ata_mtx
.locked
&& initialized
)
802 call_ata_idle_notifys(false);
809 last_user_activity
= current_tick
;
812 static void ata_thread(void)
814 static long last_sleep
= 0;
816 static long last_seen_mtx_unlock
= 0;
819 while ( queue_empty( &ata_queue
) ) {
820 if (!spinup
&& !sleeping
)
824 if (!last_seen_mtx_unlock
)
825 last_seen_mtx_unlock
= current_tick
;
826 if (TIME_AFTER(current_tick
, last_seen_mtx_unlock
+(HZ
*2)))
828 call_ata_idle_notifys(false);
829 last_seen_mtx_unlock
= 0;
832 if ( sleep_timeout
&&
833 TIME_AFTER( current_tick
,
834 last_user_activity
+ sleep_timeout
) &&
835 TIME_AFTER( current_tick
,
836 last_disk_activity
+ sleep_timeout
) )
838 call_ata_idle_notifys(true);
840 last_sleep
= current_tick
;
843 #ifdef HAVE_ATA_POWER_OFF
844 if ( !spinup
&& sleeping
&& !poweroff
&&
845 TIME_AFTER( current_tick
, last_sleep
+ ATA_POWER_OFF_TIMEOUT
))
847 spinlock_lock(&ata_mtx
);
848 ide_power_enable(false);
849 spinlock_unlock(&ata_mtx
);
856 queue_wait(&ata_queue
, &ev
);
859 case SYS_USB_CONNECTED
:
861 spinlock_lock(&ata_mtx
);
865 spinlock_unlock(&ata_mtx
);
868 /* Tell the USB thread that we are safe */
869 DEBUGF("ata_thread got SYS_USB_CONNECTED\n");
870 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
872 /* Wait until the USB cable is extracted again */
873 usb_wait_for_disconnect(&ata_queue
);
877 call_ata_idle_notifys(false);
878 last_disk_activity
= current_tick
- sleep_timeout
+ (HZ
/2);
884 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
885 int ata_hard_reset(void)
892 SET_REG(ATA_SELECT
, ata_device
); /* select the right device */
893 ret
= wait_for_bsy();
895 /* Massage the return code so it is 0 on success and -1 on failure */
901 static int perform_soft_reset(void)
903 /* If this code is allowed to run on a Nano, the next reads from the flash will
904 * time out, so we disable it. It shouldn't be necessary anyway, since the
905 * ATA -> Flash interface automatically sleeps almost immediately after the
912 SET_REG(ATA_SELECT
, SELECT_LBA
| ata_device
);
913 SET_REG(ATA_CONTROL
, CONTROL_nIEN
|CONTROL_SRST
);
914 sleep(1); /* >= 5us */
916 SET_REG(ATA_CONTROL
, CONTROL_nIEN
);
919 /* This little sucker can take up to 30 seconds */
923 ret
= wait_for_rdy();
924 } while(!ret
&& retry_count
--);
926 /* Massage the return code so it is 0 on success and -1 on failure */
931 return 0; /* Always report success */
935 int ata_soft_reset(void)
939 spinlock_lock(&ata_mtx
);
941 ret
= perform_soft_reset();
943 spinlock_unlock(&ata_mtx
);
947 static int ata_power_on(void)
951 ide_power_enable(true);
952 if( ata_hard_reset() )
959 if (set_multiple_mode(multisectors
))
968 static int master_slave_detect(void)
971 SET_REG(ATA_SELECT
, 0);
972 if ( ATA_STATUS
& (STATUS_RDY
|STATUS_BSY
) ) {
974 DEBUGF("Found master harddisk\n");
978 SET_REG(ATA_SELECT
, SELECT_DEVICE1
);
979 if ( ATA_STATUS
& (STATUS_RDY
|STATUS_BSY
) ) {
980 ata_device
= SELECT_DEVICE1
;
981 DEBUGF("Found slave harddisk\n");
989 static int identify(void)
993 SET_REG(ATA_SELECT
, ata_device
);
995 if(!wait_for_rdy()) {
996 DEBUGF("identify() - not RDY\n");
999 SET_REG(ATA_COMMAND
, CMD_IDENTIFY
);
1001 if (!wait_for_start_of_transfer())
1003 DEBUGF("identify() - CMD failed\n");
1007 for (i
=0; i
<SECTOR_SIZE
/2; i
++) {
1008 /* the IDENTIFY words are already swapped, so we need to treat
1009 this info differently that normal sector data */
1010 #if defined(ROCKBOX_BIG_ENDIAN) && !defined(SWAP_WORDS)
1011 identify_info
[i
] = swap16(ATA_DATA
);
1013 identify_info
[i
] = ATA_DATA
;
1020 static int set_multiple_mode(int sectors
)
1022 SET_REG(ATA_SELECT
, ata_device
);
1024 if(!wait_for_rdy()) {
1025 DEBUGF("set_multiple_mode() - not RDY\n");
1029 SET_REG(ATA_NSECTOR
, sectors
);
1030 SET_REG(ATA_COMMAND
, CMD_SET_MULTIPLE_MODE
);
1032 if (!wait_for_rdy())
1034 DEBUGF("set_multiple_mode() - CMD failed\n");
1041 static int set_features(void)
1044 unsigned char id_word
;
1045 unsigned char id_bit
;
1046 unsigned char subcommand
;
1047 unsigned char parameter
;
1049 { 83, 3, 0x05, 0x80 }, /* power management: lowest power without standby */
1050 { 83, 9, 0x42, 0x80 }, /* acoustic management: lowest noise */
1051 { 82, 6, 0xaa, 0 }, /* enable read look-ahead */
1052 { 83, 14, 0x03, 0 }, /* force PIO mode */
1057 /* Find out the highest supported PIO mode */
1058 if(identify_info
[64] & 2)
1061 if(identify_info
[64] & 1)
1064 /* Update the table */
1065 features
[3].parameter
= 8 + pio_mode
;
1067 SET_REG(ATA_SELECT
, ata_device
);
1069 if (!wait_for_rdy()) {
1070 DEBUGF("set_features() - not RDY\n");
1074 for (i
=0; i
< (int)(sizeof(features
)/sizeof(features
[0])); i
++) {
1075 if (identify_info
[features
[i
].id_word
] & (1 << features
[i
].id_bit
)) {
1076 SET_REG(ATA_FEATURE
, features
[i
].subcommand
);
1077 SET_REG(ATA_NSECTOR
, features
[i
].parameter
);
1078 SET_REG(ATA_COMMAND
, CMD_SET_FEATURES
);
1080 if (!wait_for_rdy()) {
1081 DEBUGF("set_features() - CMD failed\n");
1085 if(ATA_ALT_STATUS
& STATUS_ERR
) {
1086 if(ATA_ERROR
& ERROR_ABRT
) {
1096 unsigned short* ata_get_identify(void)
1098 return identify_info
;
1101 static int init_and_check(bool hard_reset
)
1107 /* This should reset both master and slave, we don't yet know what's in */
1109 if (ata_hard_reset())
1113 rc
= master_slave_detect();
1117 /* symptom fix: else check_registers() below may fail */
1118 if (hard_reset
&& !wait_for_bsy())
1121 rc
= check_registers();
1131 bool coldstart
= ata_is_coldstart();
1132 /* must be called before ata_device_init() */
1134 spinlock_init(&ata_mtx
);
1140 #ifdef MAX_PHYS_SECTOR_SIZE
1141 memset(§or_cache
, 0, sizeof(sector_cache
));
1144 if ( !initialized
) {
1145 if (!ide_powered()) /* somebody has switched it off */
1147 ide_power_enable(true);
1148 sleep(HZ
); /* allow voltage to build up */
1151 /* first try, hard reset at cold start only */
1152 rc
= init_and_check(coldstart
);
1155 { /* failed? -> second try, always with hard reset */
1156 DEBUGF("ata: init failed, retrying...\n");
1157 rc
= init_and_check(true);
1167 multisectors
= identify_info
[47] & 0xff;
1168 if (multisectors
== 0) /* Invalid multisector info, try with 16 */
1171 DEBUGF("ata: %d sectors per ata request\n",multisectors
);
1173 #ifdef MAX_PHYS_SECTOR_SIZE
1174 /* Find out the physical sector size */
1175 if((identify_info
[106] & 0xe000) == 0x6000)
1176 phys_sector_mult
= 1 << (identify_info
[106] & 0x000f);
1178 phys_sector_mult
= 1;
1180 DEBUGF("ata: %d logical sectors per phys sector", phys_sector_mult
);
1182 if (phys_sector_mult
> (MAX_PHYS_SECTOR_SIZE
/SECTOR_SIZE
))
1183 panicf("Unsupported physical sector size: %d",
1184 phys_sector_mult
* SECTOR_SIZE
);
1188 if (identify_info
[83] & 0x0400 /* 48 bit address support */
1189 && identify_info
[60] == 0xFFFF /* and disk size >= 128 GiB */
1190 && identify_info
[61] == 0x0FFF) /* (needs BigLBA addressing) */
1192 lba48
= true; /* use BigLBA */
1200 rc
= set_features();
1204 queue_init(&ata_queue
, true);
1206 last_disk_activity
= current_tick
;
1207 create_thread(ata_thread
, ata_stack
,
1208 sizeof(ata_stack
), ata_thread_name
1209 IF_PRIO(, PRIORITY_SYSTEM
)
1210 IF_COP(, CPU
, false));
1214 rc
= set_multiple_mode(multisectors
);
1221 #if (CONFIG_LED == LED_REAL)
1222 void ata_set_led_enabled(bool enabled
)
1224 ata_led_enabled
= enabled
;
1225 if (ata_led_enabled
)