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
,)
485 #ifndef MAX_PHYS_SECTOR_SIZE
486 #ifdef HAVE_MULTIVOLUME
487 (void)drive
; /* unused for now */
489 mutex_lock(&ata_mtx
);
492 last_disk_activity
= current_tick
;
493 spinup_start
= current_tick
;
500 if (ata_power_on()) {
501 mutex_unlock(&ata_mtx
);
507 if (perform_soft_reset()) {
508 mutex_unlock(&ata_mtx
);
515 SET_REG(ATA_SELECT
, ata_device
);
518 mutex_unlock(&ata_mtx
);
526 SET_REG(ATA_NSECTOR
, count
>> 8);
527 SET_REG(ATA_NSECTOR
, count
& 0xff);
528 SET_REG(ATA_SECTOR
, (start
>> 24) & 0xff); /* 31:24 */
529 SET_REG(ATA_SECTOR
, start
& 0xff); /* 7:0 */
530 SET_REG(ATA_LCYL
, 0); /* 39:32 */
531 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff); /* 15:8 */
532 SET_REG(ATA_HCYL
, 0); /* 47:40 */
533 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff); /* 23:16 */
534 SET_REG(ATA_SELECT
, SELECT_LBA
| ata_device
);
535 SET_REG(ATA_COMMAND
, CMD_WRITE_SECTORS_EXT
);
540 SET_REG(ATA_NSECTOR
, count
& 0xff); /* 0 means 256 sectors */
541 SET_REG(ATA_SECTOR
, start
& 0xff);
542 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff);
543 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff);
544 SET_REG(ATA_SELECT
, ((start
>> 24) & 0xf) | SELECT_LBA
| ata_device
);
545 SET_REG(ATA_COMMAND
, CMD_WRITE_SECTORS
);
548 for (i
=0; i
<count
; i
++) {
550 if (!wait_for_start_of_transfer()) {
556 ata_spinup_time
= current_tick
- spinup_start
;
562 copy_write_sectors(buf
, SECTOR_SIZE
/2);
565 /* reading the status register clears the interrupt */
570 last_disk_activity
= current_tick
;
573 if(!ret
&& !wait_for_end_of_transfer()) {
574 DEBUGF("End on transfer failed. -- jyp");
580 #ifndef MAX_PHYS_SECTOR_SIZE
581 mutex_unlock(&ata_mtx
);
587 #ifdef MAX_PHYS_SECTOR_SIZE
588 static int cache_sector(unsigned long sector
)
592 sector
&= ~(phys_sector_mult
- 1);
593 /* round down to physical sector boundary */
595 /* check whether the sector is already cached */
596 if (sector_cache
.inuse
&& (sector_cache
.sectornum
== sector
))
599 /* not found: read the sector */
600 sector_cache
.inuse
= false;
601 rc
= _read_sectors(sector
, phys_sector_mult
, sector_cache
.data
);
604 sector_cache
.sectornum
= sector
;
605 sector_cache
.inuse
= true;
610 static inline int flush_current_sector(void)
612 return _write_sectors(sector_cache
.sectornum
, phys_sector_mult
,
616 int ata_read_sectors(IF_MV2(int drive
,)
624 #ifdef HAVE_MULTIVOLUME
625 (void)drive
; /* unused for now */
627 mutex_lock(&ata_mtx
);
629 offset
= start
& (phys_sector_mult
- 1);
631 if (offset
) /* first partial sector */
633 int partcount
= MIN(incount
, phys_sector_mult
- offset
);
635 rc
= cache_sector(start
);
641 memcpy(inbuf
, sector_cache
.data
+ offset
* SECTOR_SIZE
,
642 partcount
* SECTOR_SIZE
);
645 inbuf
+= partcount
* SECTOR_SIZE
;
646 incount
-= partcount
;
650 offset
= incount
& (phys_sector_mult
- 1);
655 rc
= _read_sectors(start
, incount
, inbuf
);
662 inbuf
+= incount
* SECTOR_SIZE
;
666 rc
= cache_sector(start
);
672 memcpy(inbuf
, sector_cache
.data
, offset
* SECTOR_SIZE
);
677 mutex_unlock(&ata_mtx
);
682 int ata_write_sectors(IF_MV2(int drive
,)
690 #ifdef HAVE_MULTIVOLUME
691 (void)drive
; /* unused for now */
693 mutex_lock(&ata_mtx
);
695 offset
= start
& (phys_sector_mult
- 1);
697 if (offset
) /* first partial sector */
699 int partcount
= MIN(count
, phys_sector_mult
- offset
);
701 rc
= cache_sector(start
);
707 memcpy(sector_cache
.data
+ offset
* SECTOR_SIZE
, buf
,
708 partcount
* SECTOR_SIZE
);
709 rc
= flush_current_sector();
716 buf
+= partcount
* SECTOR_SIZE
;
721 offset
= count
& (phys_sector_mult
- 1);
726 rc
= _write_sectors(start
, count
, buf
);
733 buf
+= count
* SECTOR_SIZE
;
737 rc
= cache_sector(start
);
743 memcpy(sector_cache
.data
, buf
, offset
* SECTOR_SIZE
);
744 rc
= flush_current_sector();
754 mutex_unlock(&ata_mtx
);
758 #endif /* MAX_PHYS_SECTOR_SIZE */
760 static int check_registers(void)
763 if ( ATA_STATUS
& STATUS_BSY
)
766 for (i
= 0; i
<64; i
++) {
767 SET_REG(ATA_NSECTOR
, WRITE_PATTERN1
);
768 SET_REG(ATA_SECTOR
, WRITE_PATTERN2
);
769 SET_REG(ATA_LCYL
, WRITE_PATTERN3
);
770 SET_REG(ATA_HCYL
, WRITE_PATTERN4
);
772 if (((ATA_NSECTOR
& READ_PATTERN1_MASK
) == READ_PATTERN1
) &&
773 ((ATA_SECTOR
& READ_PATTERN2_MASK
) == READ_PATTERN2
) &&
774 ((ATA_LCYL
& READ_PATTERN3_MASK
) == READ_PATTERN3
) &&
775 ((ATA_HCYL
& READ_PATTERN4_MASK
) == READ_PATTERN4
))
781 static int freeze_lock(void)
783 /* does the disk support Security Mode feature set? */
784 if (identify_info
[82] & 2)
786 SET_REG(ATA_SELECT
, ata_device
);
791 SET_REG(ATA_COMMAND
, CMD_SECURITY_FREEZE_LOCK
);
800 void ata_spindown(int seconds
)
802 sleep_timeout
= seconds
* HZ
;
805 bool ata_disk_is_active(void)
810 static int ata_perform_sleep(void)
814 mutex_lock(&ata_mtx
);
816 SET_REG(ATA_SELECT
, ata_device
);
818 if(!wait_for_rdy()) {
819 DEBUGF("ata_perform_sleep() - not RDY\n");
820 mutex_unlock(&ata_mtx
);
824 SET_REG(ATA_COMMAND
, CMD_SLEEP
);
828 DEBUGF("ata_perform_sleep() - CMD failed\n");
833 mutex_unlock(&ata_mtx
);
839 queue_post(&ata_queue
, Q_SLEEP
, 0);
842 void ata_sleepnow(void)
844 if (!spinup
&& !sleeping
&& !ata_mtx
.locked
&& initialized
)
846 call_ata_idle_notifys(false);
853 last_user_activity
= current_tick
;
856 static void ata_thread(void)
858 static long last_sleep
= 0;
859 struct queue_event ev
;
860 static long last_seen_mtx_unlock
= 0;
863 queue_wait_w_tmo(&ata_queue
, &ev
, HZ
/2);
867 if (!spinup
&& !sleeping
)
871 if (!last_seen_mtx_unlock
)
872 last_seen_mtx_unlock
= current_tick
;
873 if (TIME_AFTER(current_tick
, last_seen_mtx_unlock
+(HZ
*2)))
875 call_ata_idle_notifys(false);
876 last_seen_mtx_unlock
= 0;
879 if ( sleep_timeout
&&
880 TIME_AFTER( current_tick
,
881 last_user_activity
+ sleep_timeout
) &&
882 TIME_AFTER( current_tick
,
883 last_disk_activity
+ sleep_timeout
) )
885 call_ata_idle_notifys(true);
887 last_sleep
= current_tick
;
891 #ifdef HAVE_ATA_POWER_OFF
892 if ( !spinup
&& sleeping
&& !poweroff
&&
893 TIME_AFTER( current_tick
, last_sleep
+ ATA_POWER_OFF_TIMEOUT
))
895 mutex_lock(&ata_mtx
);
896 ide_power_enable(false);
897 mutex_unlock(&ata_mtx
);
904 case SYS_USB_CONNECTED
:
906 mutex_lock(&ata_mtx
);
910 mutex_unlock(&ata_mtx
);
913 /* Tell the USB thread that we are safe */
914 DEBUGF("ata_thread got SYS_USB_CONNECTED\n");
915 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
917 /* Wait until the USB cable is extracted again */
918 usb_wait_for_disconnect(&ata_queue
);
922 call_ata_idle_notifys(false);
923 last_disk_activity
= current_tick
- sleep_timeout
+ (HZ
/2);
929 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
930 int ata_hard_reset(void)
937 SET_REG(ATA_SELECT
, ata_device
); /* select the right device */
938 ret
= wait_for_bsy();
940 /* Massage the return code so it is 0 on success and -1 on failure */
946 static int perform_soft_reset(void)
948 /* If this code is allowed to run on a Nano, the next reads from the flash will
949 * time out, so we disable it. It shouldn't be necessary anyway, since the
950 * ATA -> Flash interface automatically sleeps almost immediately after the
957 SET_REG(ATA_SELECT
, SELECT_LBA
| ata_device
);
958 SET_REG(ATA_CONTROL
, CONTROL_nIEN
|CONTROL_SRST
);
959 sleep(1); /* >= 5us */
961 SET_REG(ATA_CONTROL
, CONTROL_nIEN
);
964 /* This little sucker can take up to 30 seconds */
968 ret
= wait_for_rdy();
969 } while(!ret
&& retry_count
--);
971 /* Massage the return code so it is 0 on success and -1 on failure */
976 return 0; /* Always report success */
980 int ata_soft_reset(void)
984 mutex_lock(&ata_mtx
);
986 ret
= perform_soft_reset();
988 mutex_unlock(&ata_mtx
);
992 static int ata_power_on(void)
996 ide_power_enable(true);
997 sleep(HZ
/50); /* allow voltage to build up */
998 if( ata_hard_reset() )
1001 rc
= set_features();
1005 if (set_multiple_mode(multisectors
))
1014 static int master_slave_detect(void)
1017 SET_REG(ATA_SELECT
, 0);
1018 if ( ATA_STATUS
& (STATUS_RDY
|STATUS_BSY
) ) {
1020 DEBUGF("Found master harddisk\n");
1024 SET_REG(ATA_SELECT
, SELECT_DEVICE1
);
1025 if ( ATA_STATUS
& (STATUS_RDY
|STATUS_BSY
) ) {
1026 ata_device
= SELECT_DEVICE1
;
1027 DEBUGF("Found slave harddisk\n");
1035 static int identify(void)
1039 SET_REG(ATA_SELECT
, ata_device
);
1041 if(!wait_for_rdy()) {
1042 DEBUGF("identify() - not RDY\n");
1045 SET_REG(ATA_COMMAND
, CMD_IDENTIFY
);
1047 if (!wait_for_start_of_transfer())
1049 DEBUGF("identify() - CMD failed\n");
1053 for (i
=0; i
<SECTOR_SIZE
/2; i
++) {
1054 /* the IDENTIFY words are already swapped, so we need to treat
1055 this info differently that normal sector data */
1056 #if defined(ROCKBOX_BIG_ENDIAN) && !defined(SWAP_WORDS)
1057 identify_info
[i
] = swap16(ATA_DATA
);
1059 identify_info
[i
] = ATA_DATA
;
1066 static int set_multiple_mode(int sectors
)
1068 SET_REG(ATA_SELECT
, ata_device
);
1070 if(!wait_for_rdy()) {
1071 DEBUGF("set_multiple_mode() - not RDY\n");
1075 SET_REG(ATA_NSECTOR
, sectors
);
1076 SET_REG(ATA_COMMAND
, CMD_SET_MULTIPLE_MODE
);
1078 if (!wait_for_rdy())
1080 DEBUGF("set_multiple_mode() - CMD failed\n");
1087 static int set_features(void)
1090 unsigned char id_word
;
1091 unsigned char id_bit
;
1092 unsigned char subcommand
;
1093 unsigned char parameter
;
1095 { 83, 3, 0x05, 0x80 }, /* power management: lowest power without standby */
1096 { 83, 9, 0x42, 0x80 }, /* acoustic management: lowest noise */
1097 { 82, 6, 0xaa, 0 }, /* enable read look-ahead */
1098 { 83, 14, 0x03, 0 }, /* force PIO mode */
1103 /* Find out the highest supported PIO mode */
1104 if(identify_info
[64] & 2)
1107 if(identify_info
[64] & 1)
1110 /* Update the table */
1111 features
[3].parameter
= 8 + pio_mode
;
1113 SET_REG(ATA_SELECT
, ata_device
);
1115 if (!wait_for_rdy()) {
1116 DEBUGF("set_features() - not RDY\n");
1120 for (i
=0; i
< (int)(sizeof(features
)/sizeof(features
[0])); i
++) {
1121 if (identify_info
[features
[i
].id_word
] & (1 << features
[i
].id_bit
)) {
1122 SET_REG(ATA_FEATURE
, features
[i
].subcommand
);
1123 SET_REG(ATA_NSECTOR
, features
[i
].parameter
);
1124 SET_REG(ATA_COMMAND
, CMD_SET_FEATURES
);
1126 if (!wait_for_rdy()) {
1127 DEBUGF("set_features() - CMD failed\n");
1131 if(ATA_ALT_STATUS
& STATUS_ERR
) {
1132 if(ATA_ERROR
& ERROR_ABRT
) {
1142 unsigned short* ata_get_identify(void)
1144 return identify_info
;
1147 static int init_and_check(bool hard_reset
)
1153 /* This should reset both master and slave, we don't yet know what's in */
1155 if (ata_hard_reset())
1159 rc
= master_slave_detect();
1163 /* symptom fix: else check_registers() below may fail */
1164 if (hard_reset
&& !wait_for_bsy())
1167 rc
= check_registers();
1179 if ( !initialized
) {
1180 mutex_init(&ata_mtx
);
1181 queue_init(&ata_queue
, true);
1184 mutex_lock(&ata_mtx
);
1186 /* must be called before ata_device_init() */
1187 coldstart
= ata_is_coldstart();
1192 #ifdef MAX_PHYS_SECTOR_SIZE
1193 memset(§or_cache
, 0, sizeof(sector_cache
));
1196 if ( !initialized
) {
1197 /* First call won't have multiple thread contention - this
1198 * may return at any point without having to unlock */
1199 mutex_unlock(&ata_mtx
);
1201 if (!ide_powered()) /* somebody has switched it off */
1203 ide_power_enable(true);
1204 sleep(HZ
/50); /* allow voltage to build up */
1207 /* first try, hard reset at cold start only */
1208 rc
= init_and_check(coldstart
);
1211 { /* failed? -> second try, always with hard reset */
1212 DEBUGF("ata: init failed, retrying...\n");
1213 rc
= init_and_check(true);
1223 multisectors
= identify_info
[47] & 0xff;
1224 if (multisectors
== 0) /* Invalid multisector info, try with 16 */
1227 DEBUGF("ata: %d sectors per ata request\n",multisectors
);
1229 #ifdef MAX_PHYS_SECTOR_SIZE
1230 /* Find out the physical sector size */
1231 if((identify_info
[106] & 0xe000) == 0x6000)
1232 phys_sector_mult
= 1 << (identify_info
[106] & 0x000f);
1234 phys_sector_mult
= 1;
1236 DEBUGF("ata: %d logical sectors per phys sector", phys_sector_mult
);
1238 if (phys_sector_mult
> (MAX_PHYS_SECTOR_SIZE
/SECTOR_SIZE
))
1239 panicf("Unsupported physical sector size: %d",
1240 phys_sector_mult
* SECTOR_SIZE
);
1244 if (identify_info
[83] & 0x0400 /* 48 bit address support */
1245 && identify_info
[60] == 0xFFFF /* and disk size >= 128 GiB */
1246 && identify_info
[61] == 0x0FFF) /* (needs BigLBA addressing) */
1248 lba48
= true; /* use BigLBA */
1256 rc
= set_features();
1260 mutex_lock(&ata_mtx
); /* Balance unlock below */
1262 last_disk_activity
= current_tick
;
1263 create_thread(ata_thread
, ata_stack
,
1264 sizeof(ata_stack
), 0, ata_thread_name
1265 IF_PRIO(, PRIORITY_USER_INTERFACE
)
1270 rc
= set_multiple_mode(multisectors
);
1274 mutex_unlock(&ata_mtx
);
1278 #if (CONFIG_LED == LED_REAL)
1279 void ata_set_led_enabled(bool enabled
)
1281 ata_led_enabled
= enabled
;
1282 if (ata_led_enabled
)