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 NOCACHEBSS_ATTR
;
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
/2];
95 #ifdef MAX_PHYS_SECTOR_SIZE
97 /** This is temporary **/
98 /* Define the mutex functions to use the special hack object */
99 #define mutex_init ata_spin_init
100 #define mutex_lock ata_spin_lock
101 #define mutex_unlock ata_spin_unlock
103 void ata_spin_init(struct mutex
*m
)
108 #if CONFIG_CORELOCK == SW_CORELOCK
109 corelock_init(&m
->cl
);
113 void ata_spin_lock(struct mutex
*m
)
115 struct thread_entry
*current
= thread_get_current();
117 if (current
== m
->thread
)
123 while (test_and_set(&m
->locked
, 1, &m
->cl
))
129 void ata_spin_unlock(struct mutex
*m
)
138 test_and_set(&m
->locked
, 0, &m
->cl
);
143 struct sector_cache_entry
{
145 unsigned long sectornum
; /* logical sector */
146 unsigned char data
[MAX_PHYS_SECTOR_SIZE
];
148 /* buffer for reading and writing large physical sectors */
150 static struct sector_cache_entry sector_cache
;
151 static int phys_sector_mult
= 1;
154 static int ata_power_on(void);
155 static int perform_soft_reset(void);
156 static int set_multiple_mode(int sectors
);
157 static int set_features(void);
159 STATICIRAM
int wait_for_bsy(void) ICODE_ATTR
;
160 STATICIRAM
int wait_for_bsy(void)
162 long timeout
= current_tick
+ HZ
*30;
163 while (TIME_BEFORE(current_tick
, timeout
) && (ATA_STATUS
& STATUS_BSY
)) {
164 last_disk_activity
= current_tick
;
168 if (TIME_BEFORE(current_tick
, timeout
))
171 return 0; /* timeout */
174 STATICIRAM
int wait_for_rdy(void) ICODE_ATTR
;
175 STATICIRAM
int wait_for_rdy(void)
182 timeout
= current_tick
+ HZ
*10;
184 while (TIME_BEFORE(current_tick
, timeout
) &&
185 !(ATA_ALT_STATUS
& STATUS_RDY
)) {
186 last_disk_activity
= current_tick
;
190 if (TIME_BEFORE(current_tick
, timeout
))
193 return 0; /* timeout */
196 STATICIRAM
int wait_for_start_of_transfer(void) ICODE_ATTR
;
197 STATICIRAM
int wait_for_start_of_transfer(void)
202 return (ATA_ALT_STATUS
& (STATUS_BSY
|STATUS_DRQ
)) == STATUS_DRQ
;
205 STATICIRAM
int wait_for_end_of_transfer(void) ICODE_ATTR
;
206 STATICIRAM
int wait_for_end_of_transfer(void)
210 return (ATA_ALT_STATUS
& (STATUS_RDY
|STATUS_DRQ
)) == STATUS_RDY
;
213 #if (CONFIG_LED == LED_REAL)
214 /* Conditionally block LED access for the ATA driver, so the LED can be
215 * (mis)used for other purposes */
216 static void ata_led(bool on
)
223 #define ata_led(on) led(on)
226 #ifndef ATA_OPTIMIZED_READING
227 STATICIRAM
void copy_read_sectors(unsigned char* buf
, int wordcount
) ICODE_ATTR
;
228 STATICIRAM
void copy_read_sectors(unsigned char* buf
, int wordcount
)
230 unsigned short tmp
= 0;
232 if ( (unsigned long)buf
& 1)
233 { /* not 16-bit aligned, copy byte by byte */
234 unsigned char* bufend
= buf
+ wordcount
*2;
238 #if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN)
239 *buf
++ = tmp
& 0xff; /* I assume big endian */
240 *buf
++ = tmp
>> 8; /* and don't use the SWAB16 macro */
245 } while (buf
< bufend
); /* tail loop is faster */
248 { /* 16-bit aligned, can do faster copy */
249 unsigned short* wbuf
= (unsigned short*)buf
;
250 unsigned short* wbufend
= wbuf
+ wordcount
;
254 *wbuf
= swap16(ATA_DATA
);
258 } while (++wbuf
< wbufend
); /* tail loop is faster */
261 #endif /* !ATA_OPTIMIZED_READING */
263 #ifdef MAX_PHYS_SECTOR_SIZE
264 static int _read_sectors(unsigned long start
,
268 int ata_read_sectors(IF_MV2(int drive
,)
280 #ifndef MAX_PHYS_SECTOR_SIZE
281 #ifdef HAVE_MULTIVOLUME
282 (void)drive
; /* unused for now */
284 mutex_lock(&ata_mtx
);
287 last_disk_activity
= current_tick
;
288 spinup_start
= current_tick
;
295 if (ata_power_on()) {
296 mutex_unlock(&ata_mtx
);
302 if (perform_soft_reset()) {
303 mutex_unlock(&ata_mtx
);
310 timeout
= current_tick
+ READ_TIMEOUT
;
312 SET_REG(ATA_SELECT
, ata_device
);
315 mutex_unlock(&ata_mtx
);
323 while (TIME_BEFORE(current_tick
, timeout
)) {
325 last_disk_activity
= current_tick
;
330 SET_REG(ATA_NSECTOR
, count
>> 8);
331 SET_REG(ATA_NSECTOR
, count
& 0xff);
332 SET_REG(ATA_SECTOR
, (start
>> 24) & 0xff); /* 31:24 */
333 SET_REG(ATA_SECTOR
, start
& 0xff); /* 7:0 */
334 SET_REG(ATA_LCYL
, 0); /* 39:32 */
335 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff); /* 15:8 */
336 SET_REG(ATA_HCYL
, 0); /* 47:40 */
337 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff); /* 23:16 */
338 SET_REG(ATA_SELECT
, SELECT_LBA
| ata_device
);
339 SET_REG(ATA_COMMAND
, CMD_READ_MULTIPLE_EXT
);
344 SET_REG(ATA_NSECTOR
, count
& 0xff); /* 0 means 256 sectors */
345 SET_REG(ATA_SECTOR
, start
& 0xff);
346 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff);
347 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff);
348 SET_REG(ATA_SELECT
, ((start
>> 24) & 0xf) | SELECT_LBA
| ata_device
);
349 SET_REG(ATA_COMMAND
, CMD_READ_MULTIPLE
);
352 /* wait at least 400ns between writing command and reading status */
353 __asm__
volatile ("nop");
354 __asm__
volatile ("nop");
355 __asm__
volatile ("nop");
356 __asm__
volatile ("nop");
357 __asm__
volatile ("nop");
364 if (!wait_for_start_of_transfer()) {
365 /* We have timed out waiting for RDY and/or DRQ, possibly
366 because the hard drive is shaking and has problems reading
367 the data. We have two options:
369 2) Perform a soft reset and try again.
371 We choose alternative 2.
373 perform_soft_reset();
379 ata_spinup_time
= current_tick
- spinup_start
;
385 /* read the status register exactly once per loop */
388 if (count
>= multisectors
)
389 sectors
= multisectors
;
393 wordcount
= sectors
* SECTOR_SIZE
/ 2;
395 copy_read_sectors(buf
, wordcount
);
398 "Device errors encountered during READ MULTIPLE commands are
399 posted at the beginning of the block or partial block transfer,
400 but the DRQ bit is still set to one and the data transfer shall
401 take place, including transfer of corrupted data, if any."
404 if ( status
& (STATUS_BSY
| STATUS_ERR
| STATUS_DF
) ) {
405 perform_soft_reset();
410 buf
+= sectors
* SECTOR_SIZE
; /* Advance one chunk of sectors */
413 last_disk_activity
= current_tick
;
416 if(!ret
&& !wait_for_end_of_transfer()) {
417 perform_soft_reset();
425 #ifndef MAX_PHYS_SECTOR_SIZE
426 mutex_unlock(&ata_mtx
);
432 #ifndef ATA_OPTIMIZED_WRITING
433 STATICIRAM
void copy_write_sectors(const unsigned char* buf
, int wordcount
)
435 STATICIRAM
void copy_write_sectors(const unsigned char* buf
, int wordcount
)
437 if ( (unsigned long)buf
& 1)
438 { /* not 16-bit aligned, copy byte by byte */
439 unsigned short tmp
= 0;
440 const unsigned char* bufend
= buf
+ wordcount
*2;
443 #if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN)
444 tmp
= (unsigned short) *buf
++;
445 tmp
|= (unsigned short) *buf
++ << 8;
446 SET_16BITREG(ATA_DATA
, tmp
);
448 tmp
= (unsigned short) *buf
++ << 8;
449 tmp
|= (unsigned short) *buf
++;
450 SET_16BITREG(ATA_DATA
, tmp
);
452 } while (buf
< bufend
); /* tail loop is faster */
455 { /* 16-bit aligned, can do faster copy */
456 unsigned short* wbuf
= (unsigned short*)buf
;
457 unsigned short* wbufend
= wbuf
+ wordcount
;
461 SET_16BITREG(ATA_DATA
, swap16(*wbuf
));
463 SET_16BITREG(ATA_DATA
, *wbuf
);
465 } while (++wbuf
< wbufend
); /* tail loop is faster */
468 #endif /* !ATA_OPTIMIZED_WRITING */
470 #ifdef MAX_PHYS_SECTOR_SIZE
471 static int _write_sectors(unsigned long start
,
475 int ata_write_sectors(IF_MV2(int drive
,)
486 panicf("Writing on sector 0\n");
488 #ifndef MAX_PHYS_SECTOR_SIZE
489 #ifdef HAVE_MULTIVOLUME
490 (void)drive
; /* unused for now */
492 mutex_lock(&ata_mtx
);
495 last_disk_activity
= current_tick
;
496 spinup_start
= current_tick
;
503 if (ata_power_on()) {
504 mutex_unlock(&ata_mtx
);
510 if (perform_soft_reset()) {
511 mutex_unlock(&ata_mtx
);
518 SET_REG(ATA_SELECT
, ata_device
);
521 mutex_unlock(&ata_mtx
);
529 SET_REG(ATA_NSECTOR
, count
>> 8);
530 SET_REG(ATA_NSECTOR
, count
& 0xff);
531 SET_REG(ATA_SECTOR
, (start
>> 24) & 0xff); /* 31:24 */
532 SET_REG(ATA_SECTOR
, start
& 0xff); /* 7:0 */
533 SET_REG(ATA_LCYL
, 0); /* 39:32 */
534 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff); /* 15:8 */
535 SET_REG(ATA_HCYL
, 0); /* 47:40 */
536 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff); /* 23:16 */
537 SET_REG(ATA_SELECT
, SELECT_LBA
| ata_device
);
538 SET_REG(ATA_COMMAND
, CMD_WRITE_SECTORS_EXT
);
543 SET_REG(ATA_NSECTOR
, count
& 0xff); /* 0 means 256 sectors */
544 SET_REG(ATA_SECTOR
, start
& 0xff);
545 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff);
546 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff);
547 SET_REG(ATA_SELECT
, ((start
>> 24) & 0xf) | SELECT_LBA
| ata_device
);
548 SET_REG(ATA_COMMAND
, CMD_WRITE_SECTORS
);
551 for (i
=0; i
<count
; i
++) {
553 if (!wait_for_start_of_transfer()) {
559 ata_spinup_time
= current_tick
- spinup_start
;
565 copy_write_sectors(buf
, SECTOR_SIZE
/2);
568 /* reading the status register clears the interrupt */
573 last_disk_activity
= current_tick
;
576 if(!ret
&& !wait_for_end_of_transfer()) {
577 DEBUGF("End on transfer failed. -- jyp");
583 #ifndef MAX_PHYS_SECTOR_SIZE
584 mutex_unlock(&ata_mtx
);
590 #ifdef MAX_PHYS_SECTOR_SIZE
591 static int cache_sector(unsigned long sector
)
595 sector
&= ~(phys_sector_mult
- 1);
596 /* round down to physical sector boundary */
598 /* check whether the sector is already cached */
599 if (sector_cache
.inuse
&& (sector_cache
.sectornum
== sector
))
602 /* not found: read the sector */
603 sector_cache
.inuse
= false;
604 rc
= _read_sectors(sector
, phys_sector_mult
, sector_cache
.data
);
607 sector_cache
.sectornum
= sector
;
608 sector_cache
.inuse
= true;
613 static inline int flush_current_sector(void)
615 return _write_sectors(sector_cache
.sectornum
, phys_sector_mult
,
619 int ata_read_sectors(IF_MV2(int drive
,)
627 #ifdef HAVE_MULTIVOLUME
628 (void)drive
; /* unused for now */
630 mutex_lock(&ata_mtx
);
632 offset
= start
& (phys_sector_mult
- 1);
634 if (offset
) /* first partial sector */
636 int partcount
= MIN(incount
, phys_sector_mult
- offset
);
638 rc
= cache_sector(start
);
644 memcpy(inbuf
, sector_cache
.data
+ offset
* SECTOR_SIZE
,
645 partcount
* SECTOR_SIZE
);
648 inbuf
+= partcount
* SECTOR_SIZE
;
649 incount
-= partcount
;
653 offset
= incount
& (phys_sector_mult
- 1);
658 rc
= _read_sectors(start
, incount
, inbuf
);
665 inbuf
+= incount
* SECTOR_SIZE
;
669 rc
= cache_sector(start
);
675 memcpy(inbuf
, sector_cache
.data
, offset
* SECTOR_SIZE
);
680 mutex_unlock(&ata_mtx
);
685 int ata_write_sectors(IF_MV2(int drive
,)
693 #ifdef HAVE_MULTIVOLUME
694 (void)drive
; /* unused for now */
696 mutex_lock(&ata_mtx
);
698 offset
= start
& (phys_sector_mult
- 1);
700 if (offset
) /* first partial sector */
702 int partcount
= MIN(count
, phys_sector_mult
- offset
);
704 rc
= cache_sector(start
);
710 memcpy(sector_cache
.data
+ offset
* SECTOR_SIZE
, buf
,
711 partcount
* SECTOR_SIZE
);
712 rc
= flush_current_sector();
719 buf
+= partcount
* SECTOR_SIZE
;
724 offset
= count
& (phys_sector_mult
- 1);
729 rc
= _write_sectors(start
, count
, buf
);
736 buf
+= count
* SECTOR_SIZE
;
740 rc
= cache_sector(start
);
746 memcpy(sector_cache
.data
, buf
, offset
* SECTOR_SIZE
);
747 rc
= flush_current_sector();
757 mutex_unlock(&ata_mtx
);
761 #endif /* MAX_PHYS_SECTOR_SIZE */
763 static int check_registers(void)
766 if ( ATA_STATUS
& STATUS_BSY
)
769 for (i
= 0; i
<64; i
++) {
770 SET_REG(ATA_NSECTOR
, WRITE_PATTERN1
);
771 SET_REG(ATA_SECTOR
, WRITE_PATTERN2
);
772 SET_REG(ATA_LCYL
, WRITE_PATTERN3
);
773 SET_REG(ATA_HCYL
, WRITE_PATTERN4
);
775 if (((ATA_NSECTOR
& READ_PATTERN1_MASK
) == READ_PATTERN1
) &&
776 ((ATA_SECTOR
& READ_PATTERN2_MASK
) == READ_PATTERN2
) &&
777 ((ATA_LCYL
& READ_PATTERN3_MASK
) == READ_PATTERN3
) &&
778 ((ATA_HCYL
& READ_PATTERN4_MASK
) == READ_PATTERN4
))
784 static int freeze_lock(void)
786 /* does the disk support Security Mode feature set? */
787 if (identify_info
[82] & 2)
789 SET_REG(ATA_SELECT
, ata_device
);
794 SET_REG(ATA_COMMAND
, CMD_SECURITY_FREEZE_LOCK
);
803 void ata_spindown(int seconds
)
805 sleep_timeout
= seconds
* HZ
;
808 bool ata_disk_is_active(void)
813 static int ata_perform_sleep(void)
817 mutex_lock(&ata_mtx
);
819 SET_REG(ATA_SELECT
, ata_device
);
821 if(!wait_for_rdy()) {
822 DEBUGF("ata_perform_sleep() - not RDY\n");
823 mutex_unlock(&ata_mtx
);
827 SET_REG(ATA_COMMAND
, CMD_SLEEP
);
831 DEBUGF("ata_perform_sleep() - CMD failed\n");
836 mutex_unlock(&ata_mtx
);
842 queue_post(&ata_queue
, Q_SLEEP
, 0);
845 void ata_sleepnow(void)
847 if (!spinup
&& !sleeping
&& !ata_mtx
.locked
&& initialized
)
849 call_ata_idle_notifys(false);
856 last_user_activity
= current_tick
;
859 static void ata_thread(void)
861 static long last_sleep
= 0;
862 struct queue_event ev
;
863 static long last_seen_mtx_unlock
= 0;
866 queue_wait_w_tmo(&ata_queue
, &ev
, HZ
/2);
870 if (!spinup
&& !sleeping
)
874 if (!last_seen_mtx_unlock
)
875 last_seen_mtx_unlock
= current_tick
;
876 if (TIME_AFTER(current_tick
, last_seen_mtx_unlock
+(HZ
*2)))
878 call_ata_idle_notifys(false);
879 last_seen_mtx_unlock
= 0;
882 if ( sleep_timeout
&&
883 TIME_AFTER( current_tick
,
884 last_user_activity
+ sleep_timeout
) &&
885 TIME_AFTER( current_tick
,
886 last_disk_activity
+ sleep_timeout
) )
888 call_ata_idle_notifys(true);
890 last_sleep
= current_tick
;
894 #ifdef HAVE_ATA_POWER_OFF
895 if ( !spinup
&& sleeping
&& !poweroff
&&
896 TIME_AFTER( current_tick
, last_sleep
+ ATA_POWER_OFF_TIMEOUT
))
898 mutex_lock(&ata_mtx
);
899 ide_power_enable(false);
900 mutex_unlock(&ata_mtx
);
907 case SYS_USB_CONNECTED
:
909 mutex_lock(&ata_mtx
);
913 mutex_unlock(&ata_mtx
);
916 /* Tell the USB thread that we are safe */
917 DEBUGF("ata_thread got SYS_USB_CONNECTED\n");
918 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
920 /* Wait until the USB cable is extracted again */
921 usb_wait_for_disconnect(&ata_queue
);
925 call_ata_idle_notifys(false);
926 last_disk_activity
= current_tick
- sleep_timeout
+ (HZ
/2);
932 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
933 int ata_hard_reset(void)
940 SET_REG(ATA_SELECT
, ata_device
); /* select the right device */
941 ret
= wait_for_bsy();
943 /* Massage the return code so it is 0 on success and -1 on failure */
949 static int perform_soft_reset(void)
951 /* If this code is allowed to run on a Nano, the next reads from the flash will
952 * time out, so we disable it. It shouldn't be necessary anyway, since the
953 * ATA -> Flash interface automatically sleeps almost immediately after the
960 SET_REG(ATA_SELECT
, SELECT_LBA
| ata_device
);
961 SET_REG(ATA_CONTROL
, CONTROL_nIEN
|CONTROL_SRST
);
962 sleep(1); /* >= 5us */
964 SET_REG(ATA_CONTROL
, CONTROL_nIEN
);
967 /* This little sucker can take up to 30 seconds */
971 ret
= wait_for_rdy();
972 } while(!ret
&& retry_count
--);
974 /* Massage the return code so it is 0 on success and -1 on failure */
979 return 0; /* Always report success */
983 int ata_soft_reset(void)
987 mutex_lock(&ata_mtx
);
989 ret
= perform_soft_reset();
991 mutex_unlock(&ata_mtx
);
995 static int ata_power_on(void)
999 ide_power_enable(true);
1000 sleep(HZ
/50); /* allow voltage to build up */
1001 if( ata_hard_reset() )
1004 rc
= set_features();
1008 if (set_multiple_mode(multisectors
))
1017 static int master_slave_detect(void)
1020 SET_REG(ATA_SELECT
, 0);
1021 if ( ATA_STATUS
& (STATUS_RDY
|STATUS_BSY
) ) {
1023 DEBUGF("Found master harddisk\n");
1027 SET_REG(ATA_SELECT
, SELECT_DEVICE1
);
1028 if ( ATA_STATUS
& (STATUS_RDY
|STATUS_BSY
) ) {
1029 ata_device
= SELECT_DEVICE1
;
1030 DEBUGF("Found slave harddisk\n");
1038 static int identify(void)
1042 SET_REG(ATA_SELECT
, ata_device
);
1044 if(!wait_for_rdy()) {
1045 DEBUGF("identify() - not RDY\n");
1048 SET_REG(ATA_COMMAND
, CMD_IDENTIFY
);
1050 if (!wait_for_start_of_transfer())
1052 DEBUGF("identify() - CMD failed\n");
1056 for (i
=0; i
<SECTOR_SIZE
/2; i
++) {
1057 /* the IDENTIFY words are already swapped, so we need to treat
1058 this info differently that normal sector data */
1059 #if defined(ROCKBOX_BIG_ENDIAN) && !defined(SWAP_WORDS)
1060 identify_info
[i
] = swap16(ATA_DATA
);
1062 identify_info
[i
] = ATA_DATA
;
1069 static int set_multiple_mode(int sectors
)
1071 SET_REG(ATA_SELECT
, ata_device
);
1073 if(!wait_for_rdy()) {
1074 DEBUGF("set_multiple_mode() - not RDY\n");
1078 SET_REG(ATA_NSECTOR
, sectors
);
1079 SET_REG(ATA_COMMAND
, CMD_SET_MULTIPLE_MODE
);
1081 if (!wait_for_rdy())
1083 DEBUGF("set_multiple_mode() - CMD failed\n");
1090 static int set_features(void)
1093 unsigned char id_word
;
1094 unsigned char id_bit
;
1095 unsigned char subcommand
;
1096 unsigned char parameter
;
1098 { 83, 3, 0x05, 0x80 }, /* power management: lowest power without standby */
1099 { 83, 9, 0x42, 0x80 }, /* acoustic management: lowest noise */
1100 { 82, 6, 0xaa, 0 }, /* enable read look-ahead */
1101 { 83, 14, 0x03, 0 }, /* force PIO mode */
1106 /* Find out the highest supported PIO mode */
1107 if(identify_info
[64] & 2)
1110 if(identify_info
[64] & 1)
1113 /* Update the table */
1114 features
[3].parameter
= 8 + pio_mode
;
1116 SET_REG(ATA_SELECT
, ata_device
);
1118 if (!wait_for_rdy()) {
1119 DEBUGF("set_features() - not RDY\n");
1123 for (i
=0; i
< (int)(sizeof(features
)/sizeof(features
[0])); i
++) {
1124 if (identify_info
[features
[i
].id_word
] & (1 << features
[i
].id_bit
)) {
1125 SET_REG(ATA_FEATURE
, features
[i
].subcommand
);
1126 SET_REG(ATA_NSECTOR
, features
[i
].parameter
);
1127 SET_REG(ATA_COMMAND
, CMD_SET_FEATURES
);
1129 if (!wait_for_rdy()) {
1130 DEBUGF("set_features() - CMD failed\n");
1134 if(ATA_ALT_STATUS
& STATUS_ERR
) {
1135 if(ATA_ERROR
& ERROR_ABRT
) {
1145 unsigned short* ata_get_identify(void)
1147 return identify_info
;
1150 static int init_and_check(bool hard_reset
)
1156 /* This should reset both master and slave, we don't yet know what's in */
1158 if (ata_hard_reset())
1162 rc
= master_slave_detect();
1166 /* symptom fix: else check_registers() below may fail */
1167 if (hard_reset
&& !wait_for_bsy())
1170 rc
= check_registers();
1182 if ( !initialized
) {
1183 mutex_init(&ata_mtx
);
1184 queue_init(&ata_queue
, true);
1187 mutex_lock(&ata_mtx
);
1189 /* must be called before ata_device_init() */
1190 coldstart
= ata_is_coldstart();
1195 #ifdef MAX_PHYS_SECTOR_SIZE
1196 memset(§or_cache
, 0, sizeof(sector_cache
));
1199 if ( !initialized
) {
1200 /* First call won't have multiple thread contention - this
1201 * may return at any point without having to unlock */
1202 mutex_unlock(&ata_mtx
);
1204 if (!ide_powered()) /* somebody has switched it off */
1206 ide_power_enable(true);
1207 sleep(HZ
/50); /* allow voltage to build up */
1210 /* first try, hard reset at cold start only */
1211 rc
= init_and_check(coldstart
);
1214 { /* failed? -> second try, always with hard reset */
1215 DEBUGF("ata: init failed, retrying...\n");
1216 rc
= init_and_check(true);
1226 multisectors
= identify_info
[47] & 0xff;
1227 if (multisectors
== 0) /* Invalid multisector info, try with 16 */
1230 DEBUGF("ata: %d sectors per ata request\n",multisectors
);
1232 #ifdef MAX_PHYS_SECTOR_SIZE
1233 /* Find out the physical sector size */
1234 if((identify_info
[106] & 0xe000) == 0x6000)
1235 phys_sector_mult
= 1 << (identify_info
[106] & 0x000f);
1237 phys_sector_mult
= 1;
1239 DEBUGF("ata: %d logical sectors per phys sector", phys_sector_mult
);
1241 if (phys_sector_mult
> (MAX_PHYS_SECTOR_SIZE
/SECTOR_SIZE
))
1242 panicf("Unsupported physical sector size: %d",
1243 phys_sector_mult
* SECTOR_SIZE
);
1247 if (identify_info
[83] & 0x0400 /* 48 bit address support */
1248 && identify_info
[60] == 0xFFFF /* and disk size >= 128 GiB */
1249 && identify_info
[61] == 0x0FFF) /* (needs BigLBA addressing) */
1251 lba48
= true; /* use BigLBA */
1259 rc
= set_features();
1263 mutex_lock(&ata_mtx
); /* Balance unlock below */
1265 last_disk_activity
= current_tick
;
1266 create_thread(ata_thread
, ata_stack
,
1267 sizeof(ata_stack
), 0, ata_thread_name
1268 IF_PRIO(, PRIORITY_USER_INTERFACE
)
1273 rc
= set_multiple_mode(multisectors
);
1277 mutex_unlock(&ata_mtx
);
1281 #if (CONFIG_LED == LED_REAL)
1282 void ata_set_led_enabled(bool enabled
)
1284 ata_led_enabled
= enabled
;
1285 if (ata_led_enabled
)