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 unsigned long total_sectors
;
93 static int multisectors
; /* number of supported multisectors */
94 static unsigned short identify_info
[SECTOR_SIZE
/2];
96 #ifdef MAX_PHYS_SECTOR_SIZE
98 /** This is temporary **/
99 /* Define the mutex functions to use the special hack object */
100 #define mutex_init ata_spin_init
101 #define mutex_lock ata_spin_lock
102 #define mutex_unlock ata_spin_unlock
104 void ata_spin_init(struct mutex
*m
)
109 #if CONFIG_CORELOCK == SW_CORELOCK
110 corelock_init(&m
->cl
);
114 void ata_spin_lock(struct mutex
*m
)
116 struct thread_entry
*current
= thread_get_current();
118 if (current
== m
->thread
)
124 while (test_and_set(&m
->locked
, 1, &m
->cl
))
130 void ata_spin_unlock(struct mutex
*m
)
139 test_and_set(&m
->locked
, 0, &m
->cl
);
144 struct sector_cache_entry
{
146 unsigned long sectornum
; /* logical sector */
147 unsigned char data
[MAX_PHYS_SECTOR_SIZE
];
149 /* buffer for reading and writing large physical sectors */
151 static struct sector_cache_entry sector_cache
;
152 static int phys_sector_mult
= 1;
155 static int ata_power_on(void);
156 static int perform_soft_reset(void);
157 static int set_multiple_mode(int sectors
);
158 static int set_features(void);
160 STATICIRAM
int wait_for_bsy(void) ICODE_ATTR
;
161 STATICIRAM
int wait_for_bsy(void)
163 long timeout
= current_tick
+ HZ
*30;
164 while (TIME_BEFORE(current_tick
, timeout
) && (ATA_STATUS
& STATUS_BSY
)) {
165 last_disk_activity
= current_tick
;
169 if (TIME_BEFORE(current_tick
, timeout
))
172 return 0; /* timeout */
175 STATICIRAM
int wait_for_rdy(void) ICODE_ATTR
;
176 STATICIRAM
int wait_for_rdy(void)
183 timeout
= current_tick
+ HZ
*10;
185 while (TIME_BEFORE(current_tick
, timeout
) &&
186 !(ATA_ALT_STATUS
& STATUS_RDY
)) {
187 last_disk_activity
= current_tick
;
191 if (TIME_BEFORE(current_tick
, timeout
))
194 return 0; /* timeout */
197 STATICIRAM
int wait_for_start_of_transfer(void) ICODE_ATTR
;
198 STATICIRAM
int wait_for_start_of_transfer(void)
203 return (ATA_ALT_STATUS
& (STATUS_BSY
|STATUS_DRQ
)) == STATUS_DRQ
;
206 STATICIRAM
int wait_for_end_of_transfer(void) ICODE_ATTR
;
207 STATICIRAM
int wait_for_end_of_transfer(void)
211 return (ATA_ALT_STATUS
& (STATUS_RDY
|STATUS_DRQ
)) == STATUS_RDY
;
214 #if (CONFIG_LED == LED_REAL)
215 /* Conditionally block LED access for the ATA driver, so the LED can be
216 * (mis)used for other purposes */
217 static void ata_led(bool on
)
224 #define ata_led(on) led(on)
227 #ifndef ATA_OPTIMIZED_READING
228 STATICIRAM
void copy_read_sectors(unsigned char* buf
, int wordcount
) ICODE_ATTR
;
229 STATICIRAM
void copy_read_sectors(unsigned char* buf
, int wordcount
)
231 unsigned short tmp
= 0;
233 if ( (unsigned long)buf
& 1)
234 { /* not 16-bit aligned, copy byte by byte */
235 unsigned char* bufend
= buf
+ wordcount
*2;
239 #if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN)
240 *buf
++ = tmp
& 0xff; /* I assume big endian */
241 *buf
++ = tmp
>> 8; /* and don't use the SWAB16 macro */
246 } while (buf
< bufend
); /* tail loop is faster */
249 { /* 16-bit aligned, can do faster copy */
250 unsigned short* wbuf
= (unsigned short*)buf
;
251 unsigned short* wbufend
= wbuf
+ wordcount
;
255 *wbuf
= swap16(ATA_DATA
);
259 } while (++wbuf
< wbufend
); /* tail loop is faster */
262 #endif /* !ATA_OPTIMIZED_READING */
264 #ifdef MAX_PHYS_SECTOR_SIZE
265 static int _read_sectors(unsigned long start
,
269 int ata_read_sectors(IF_MV2(int drive
,)
281 #ifndef MAX_PHYS_SECTOR_SIZE
282 #ifdef HAVE_MULTIVOLUME
283 (void)drive
; /* unused for now */
285 mutex_lock(&ata_mtx
);
288 if (start
+ incount
> total_sectors
) {
293 last_disk_activity
= current_tick
;
294 spinup_start
= current_tick
;
301 if (ata_power_on()) {
307 if (perform_soft_reset()) {
314 timeout
= current_tick
+ READ_TIMEOUT
;
316 SET_REG(ATA_SELECT
, ata_device
);
326 while (TIME_BEFORE(current_tick
, timeout
)) {
328 last_disk_activity
= current_tick
;
333 SET_REG(ATA_NSECTOR
, count
>> 8);
334 SET_REG(ATA_NSECTOR
, count
& 0xff);
335 SET_REG(ATA_SECTOR
, (start
>> 24) & 0xff); /* 31:24 */
336 SET_REG(ATA_SECTOR
, start
& 0xff); /* 7:0 */
337 SET_REG(ATA_LCYL
, 0); /* 39:32 */
338 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff); /* 15:8 */
339 SET_REG(ATA_HCYL
, 0); /* 47:40 */
340 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff); /* 23:16 */
341 SET_REG(ATA_SELECT
, SELECT_LBA
| ata_device
);
342 SET_REG(ATA_COMMAND
, CMD_READ_MULTIPLE_EXT
);
347 SET_REG(ATA_NSECTOR
, count
& 0xff); /* 0 means 256 sectors */
348 SET_REG(ATA_SECTOR
, start
& 0xff);
349 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff);
350 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff);
351 SET_REG(ATA_SELECT
, ((start
>> 24) & 0xf) | SELECT_LBA
| ata_device
);
352 SET_REG(ATA_COMMAND
, CMD_READ_MULTIPLE
);
355 /* wait at least 400ns between writing command and reading status */
356 __asm__
volatile ("nop");
357 __asm__
volatile ("nop");
358 __asm__
volatile ("nop");
359 __asm__
volatile ("nop");
360 __asm__
volatile ("nop");
367 if (!wait_for_start_of_transfer()) {
368 /* We have timed out waiting for RDY and/or DRQ, possibly
369 because the hard drive is shaking and has problems reading
370 the data. We have two options:
372 2) Perform a soft reset and try again.
374 We choose alternative 2.
376 perform_soft_reset();
382 ata_spinup_time
= current_tick
- spinup_start
;
388 /* read the status register exactly once per loop */
391 if (count
>= multisectors
)
392 sectors
= multisectors
;
396 wordcount
= sectors
* SECTOR_SIZE
/ 2;
398 copy_read_sectors(buf
, wordcount
);
401 "Device errors encountered during READ MULTIPLE commands are
402 posted at the beginning of the block or partial block transfer,
403 but the DRQ bit is still set to one and the data transfer shall
404 take place, including transfer of corrupted data, if any."
407 if ( status
& (STATUS_BSY
| STATUS_ERR
| STATUS_DF
) ) {
408 perform_soft_reset();
413 buf
+= sectors
* SECTOR_SIZE
; /* Advance one chunk of sectors */
416 last_disk_activity
= current_tick
;
419 if(!ret
&& !wait_for_end_of_transfer()) {
420 perform_soft_reset();
429 #ifndef MAX_PHYS_SECTOR_SIZE
430 mutex_unlock(&ata_mtx
);
436 #ifndef ATA_OPTIMIZED_WRITING
437 STATICIRAM
void copy_write_sectors(const unsigned char* buf
, int wordcount
)
439 STATICIRAM
void copy_write_sectors(const unsigned char* buf
, int wordcount
)
441 if ( (unsigned long)buf
& 1)
442 { /* not 16-bit aligned, copy byte by byte */
443 unsigned short tmp
= 0;
444 const unsigned char* bufend
= buf
+ wordcount
*2;
447 #if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN)
448 tmp
= (unsigned short) *buf
++;
449 tmp
|= (unsigned short) *buf
++ << 8;
450 SET_16BITREG(ATA_DATA
, tmp
);
452 tmp
= (unsigned short) *buf
++ << 8;
453 tmp
|= (unsigned short) *buf
++;
454 SET_16BITREG(ATA_DATA
, tmp
);
456 } while (buf
< bufend
); /* tail loop is faster */
459 { /* 16-bit aligned, can do faster copy */
460 unsigned short* wbuf
= (unsigned short*)buf
;
461 unsigned short* wbufend
= wbuf
+ wordcount
;
465 SET_16BITREG(ATA_DATA
, swap16(*wbuf
));
467 SET_16BITREG(ATA_DATA
, *wbuf
);
469 } while (++wbuf
< wbufend
); /* tail loop is faster */
472 #endif /* !ATA_OPTIMIZED_WRITING */
474 #ifdef MAX_PHYS_SECTOR_SIZE
475 static int _write_sectors(unsigned long start
,
479 int ata_write_sectors(IF_MV2(int drive
,)
489 #ifndef MAX_PHYS_SECTOR_SIZE
490 #ifdef HAVE_MULTIVOLUME
491 (void)drive
; /* unused for now */
493 mutex_lock(&ata_mtx
);
496 if (start
+ count
> total_sectors
)
497 panicf("Writing past end of disk");
499 last_disk_activity
= current_tick
;
500 spinup_start
= current_tick
;
507 if (ata_power_on()) {
513 if (perform_soft_reset()) {
520 SET_REG(ATA_SELECT
, ata_device
);
530 SET_REG(ATA_NSECTOR
, count
>> 8);
531 SET_REG(ATA_NSECTOR
, count
& 0xff);
532 SET_REG(ATA_SECTOR
, (start
>> 24) & 0xff); /* 31:24 */
533 SET_REG(ATA_SECTOR
, start
& 0xff); /* 7:0 */
534 SET_REG(ATA_LCYL
, 0); /* 39:32 */
535 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff); /* 15:8 */
536 SET_REG(ATA_HCYL
, 0); /* 47:40 */
537 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff); /* 23:16 */
538 SET_REG(ATA_SELECT
, SELECT_LBA
| ata_device
);
539 SET_REG(ATA_COMMAND
, CMD_WRITE_SECTORS_EXT
);
544 SET_REG(ATA_NSECTOR
, count
& 0xff); /* 0 means 256 sectors */
545 SET_REG(ATA_SECTOR
, start
& 0xff);
546 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff);
547 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff);
548 SET_REG(ATA_SELECT
, ((start
>> 24) & 0xf) | SELECT_LBA
| ata_device
);
549 SET_REG(ATA_COMMAND
, CMD_WRITE_SECTORS
);
552 for (i
=0; i
<count
; i
++) {
554 if (!wait_for_start_of_transfer()) {
560 ata_spinup_time
= current_tick
- spinup_start
;
566 copy_write_sectors(buf
, SECTOR_SIZE
/2);
569 /* reading the status register clears the interrupt */
574 last_disk_activity
= current_tick
;
577 if(!ret
&& !wait_for_end_of_transfer()) {
578 DEBUGF("End on transfer failed. -- jyp");
584 #ifndef MAX_PHYS_SECTOR_SIZE
585 mutex_unlock(&ata_mtx
);
591 #ifdef MAX_PHYS_SECTOR_SIZE
592 static int cache_sector(unsigned long sector
)
596 sector
&= ~(phys_sector_mult
- 1);
597 /* round down to physical sector boundary */
599 /* check whether the sector is already cached */
600 if (sector_cache
.inuse
&& (sector_cache
.sectornum
== sector
))
603 /* not found: read the sector */
604 sector_cache
.inuse
= false;
605 rc
= _read_sectors(sector
, phys_sector_mult
, sector_cache
.data
);
608 sector_cache
.sectornum
= sector
;
609 sector_cache
.inuse
= true;
614 static inline int flush_current_sector(void)
616 return _write_sectors(sector_cache
.sectornum
, phys_sector_mult
,
620 int ata_read_sectors(IF_MV2(int drive
,)
628 #ifdef HAVE_MULTIVOLUME
629 (void)drive
; /* unused for now */
631 mutex_lock(&ata_mtx
);
633 offset
= start
& (phys_sector_mult
- 1);
635 if (offset
) /* first partial sector */
637 int partcount
= MIN(incount
, phys_sector_mult
- offset
);
639 rc
= cache_sector(start
);
645 memcpy(inbuf
, sector_cache
.data
+ offset
* SECTOR_SIZE
,
646 partcount
* SECTOR_SIZE
);
649 inbuf
+= partcount
* SECTOR_SIZE
;
650 incount
-= partcount
;
654 offset
= incount
& (phys_sector_mult
- 1);
659 rc
= _read_sectors(start
, incount
, inbuf
);
666 inbuf
+= incount
* SECTOR_SIZE
;
670 rc
= cache_sector(start
);
676 memcpy(inbuf
, sector_cache
.data
, offset
* SECTOR_SIZE
);
681 mutex_unlock(&ata_mtx
);
686 int ata_write_sectors(IF_MV2(int drive
,)
694 #ifdef HAVE_MULTIVOLUME
695 (void)drive
; /* unused for now */
697 mutex_lock(&ata_mtx
);
699 offset
= start
& (phys_sector_mult
- 1);
701 if (offset
) /* first partial sector */
703 int partcount
= MIN(count
, phys_sector_mult
- offset
);
705 rc
= cache_sector(start
);
711 memcpy(sector_cache
.data
+ offset
* SECTOR_SIZE
, buf
,
712 partcount
* SECTOR_SIZE
);
713 rc
= flush_current_sector();
720 buf
+= partcount
* SECTOR_SIZE
;
725 offset
= count
& (phys_sector_mult
- 1);
730 rc
= _write_sectors(start
, count
, buf
);
737 buf
+= count
* SECTOR_SIZE
;
741 rc
= cache_sector(start
);
747 memcpy(sector_cache
.data
, buf
, offset
* SECTOR_SIZE
);
748 rc
= flush_current_sector();
758 mutex_unlock(&ata_mtx
);
762 #endif /* MAX_PHYS_SECTOR_SIZE */
764 static int check_registers(void)
767 if ( ATA_STATUS
& STATUS_BSY
)
770 for (i
= 0; i
<64; i
++) {
771 SET_REG(ATA_NSECTOR
, WRITE_PATTERN1
);
772 SET_REG(ATA_SECTOR
, WRITE_PATTERN2
);
773 SET_REG(ATA_LCYL
, WRITE_PATTERN3
);
774 SET_REG(ATA_HCYL
, WRITE_PATTERN4
);
776 if (((ATA_NSECTOR
& READ_PATTERN1_MASK
) == READ_PATTERN1
) &&
777 ((ATA_SECTOR
& READ_PATTERN2_MASK
) == READ_PATTERN2
) &&
778 ((ATA_LCYL
& READ_PATTERN3_MASK
) == READ_PATTERN3
) &&
779 ((ATA_HCYL
& READ_PATTERN4_MASK
) == READ_PATTERN4
))
785 static int freeze_lock(void)
787 /* does the disk support Security Mode feature set? */
788 if (identify_info
[82] & 2)
790 SET_REG(ATA_SELECT
, ata_device
);
795 SET_REG(ATA_COMMAND
, CMD_SECURITY_FREEZE_LOCK
);
804 void ata_spindown(int seconds
)
806 sleep_timeout
= seconds
* HZ
;
809 bool ata_disk_is_active(void)
814 static int ata_perform_sleep(void)
816 mutex_lock(&ata_mtx
);
818 SET_REG(ATA_SELECT
, ata_device
);
820 if(!wait_for_rdy()) {
821 DEBUGF("ata_perform_sleep() - not RDY\n");
822 mutex_unlock(&ata_mtx
);
826 SET_REG(ATA_COMMAND
, CMD_SLEEP
);
830 DEBUGF("ata_perform_sleep() - CMD failed\n");
831 mutex_unlock(&ata_mtx
);
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
959 SET_REG(ATA_SELECT
, SELECT_LBA
| ata_device
);
960 SET_REG(ATA_CONTROL
, CONTROL_nIEN
|CONTROL_SRST
);
961 sleep(1); /* >= 5us */
963 SET_REG(ATA_CONTROL
, CONTROL_nIEN
);
966 /* This little sucker can take up to 30 seconds */
970 ret
= wait_for_rdy();
971 } while(!ret
&& retry_count
--);
979 if (set_multiple_mode(multisectors
))
988 int ata_soft_reset(void)
992 mutex_lock(&ata_mtx
);
994 ret
= perform_soft_reset();
996 mutex_unlock(&ata_mtx
);
1000 static int ata_power_on(void)
1004 ide_power_enable(true);
1005 sleep(HZ
/50); /* allow voltage to build up */
1006 if( ata_hard_reset() )
1009 rc
= set_features();
1013 if (set_multiple_mode(multisectors
))
1022 static int master_slave_detect(void)
1025 SET_REG(ATA_SELECT
, 0);
1026 if ( ATA_STATUS
& (STATUS_RDY
|STATUS_BSY
) ) {
1028 DEBUGF("Found master harddisk\n");
1032 SET_REG(ATA_SELECT
, SELECT_DEVICE1
);
1033 if ( ATA_STATUS
& (STATUS_RDY
|STATUS_BSY
) ) {
1034 ata_device
= SELECT_DEVICE1
;
1035 DEBUGF("Found slave harddisk\n");
1043 static int identify(void)
1047 SET_REG(ATA_SELECT
, ata_device
);
1049 if(!wait_for_rdy()) {
1050 DEBUGF("identify() - not RDY\n");
1053 SET_REG(ATA_COMMAND
, CMD_IDENTIFY
);
1055 if (!wait_for_start_of_transfer())
1057 DEBUGF("identify() - CMD failed\n");
1061 for (i
=0; i
<SECTOR_SIZE
/2; i
++) {
1062 /* the IDENTIFY words are already swapped, so we need to treat
1063 this info differently that normal sector data */
1064 #if defined(ROCKBOX_BIG_ENDIAN) && !defined(SWAP_WORDS)
1065 identify_info
[i
] = swap16(ATA_DATA
);
1067 identify_info
[i
] = ATA_DATA
;
1074 static int set_multiple_mode(int sectors
)
1076 SET_REG(ATA_SELECT
, ata_device
);
1078 if(!wait_for_rdy()) {
1079 DEBUGF("set_multiple_mode() - not RDY\n");
1083 SET_REG(ATA_NSECTOR
, sectors
);
1084 SET_REG(ATA_COMMAND
, CMD_SET_MULTIPLE_MODE
);
1086 if (!wait_for_rdy())
1088 DEBUGF("set_multiple_mode() - CMD failed\n");
1095 static int set_features(void)
1098 unsigned char id_word
;
1099 unsigned char id_bit
;
1100 unsigned char subcommand
;
1101 unsigned char parameter
;
1103 { 83, 14, 0x03, 0 }, /* force PIO mode */
1104 { 83, 3, 0x05, 0x80 }, /* adv. power management: lowest w/o standby */
1105 { 83, 9, 0x42, 0x80 }, /* acoustic management: lowest noise */
1106 { 82, 6, 0xaa, 0 }, /* enable read look-ahead */
1111 /* Find out the highest supported PIO mode */
1112 if(identify_info
[64] & 2)
1115 if(identify_info
[64] & 1)
1118 /* Update the table: set highest supported pio mode that we also support */
1119 features
[0].parameter
= 8 + pio_mode
;
1121 SET_REG(ATA_SELECT
, ata_device
);
1123 if (!wait_for_rdy()) {
1124 DEBUGF("set_features() - not RDY\n");
1128 for (i
=0; i
< (int)(sizeof(features
)/sizeof(features
[0])); i
++) {
1129 if (identify_info
[features
[i
].id_word
] & (1 << features
[i
].id_bit
)) {
1130 SET_REG(ATA_FEATURE
, features
[i
].subcommand
);
1131 SET_REG(ATA_NSECTOR
, features
[i
].parameter
);
1132 SET_REG(ATA_COMMAND
, CMD_SET_FEATURES
);
1134 if (!wait_for_rdy()) {
1135 DEBUGF("set_features() - CMD failed\n");
1139 if((ATA_ALT_STATUS
& STATUS_ERR
) && (i
!= 1)) {
1140 /* some CF cards don't like advanced powermanagement
1141 even if they mark it as supported - go figure... */
1142 if(ATA_ERROR
& ERROR_ABRT
) {
1152 unsigned short* ata_get_identify(void)
1154 return identify_info
;
1157 static int init_and_check(bool hard_reset
)
1163 /* This should reset both master and slave, we don't yet know what's in */
1165 if (ata_hard_reset())
1169 rc
= master_slave_detect();
1173 /* symptom fix: else check_registers() below may fail */
1174 if (hard_reset
&& !wait_for_bsy())
1177 rc
= check_registers();
1189 if ( !initialized
) {
1190 mutex_init(&ata_mtx
);
1191 queue_init(&ata_queue
, true);
1194 mutex_lock(&ata_mtx
);
1196 /* must be called before ata_device_init() */
1197 coldstart
= ata_is_coldstart();
1202 #ifdef MAX_PHYS_SECTOR_SIZE
1203 memset(§or_cache
, 0, sizeof(sector_cache
));
1206 if ( !initialized
) {
1207 /* First call won't have multiple thread contention - this
1208 * may return at any point without having to unlock */
1209 mutex_unlock(&ata_mtx
);
1211 if (!ide_powered()) /* somebody has switched it off */
1213 ide_power_enable(true);
1214 sleep(HZ
/50); /* allow voltage to build up */
1217 /* first try, hard reset at cold start only */
1218 rc
= init_and_check(coldstart
);
1221 { /* failed? -> second try, always with hard reset */
1222 DEBUGF("ata: init failed, retrying...\n");
1223 rc
= init_and_check(true);
1233 multisectors
= identify_info
[47] & 0xff;
1234 if (multisectors
== 0) /* Invalid multisector info, try with 16 */
1237 DEBUGF("ata: %d sectors per ata request\n",multisectors
);
1239 #ifdef MAX_PHYS_SECTOR_SIZE
1240 /* Find out the physical sector size */
1241 if((identify_info
[106] & 0xe000) == 0x6000)
1242 phys_sector_mult
= 1 << (identify_info
[106] & 0x000f);
1244 phys_sector_mult
= 1;
1246 DEBUGF("ata: %d logical sectors per phys sector", phys_sector_mult
);
1248 if (phys_sector_mult
> (MAX_PHYS_SECTOR_SIZE
/SECTOR_SIZE
))
1249 panicf("Unsupported physical sector size: %d",
1250 phys_sector_mult
* SECTOR_SIZE
);
1253 total_sectors
= identify_info
[60] | (identify_info
[61] << 16);
1256 if (identify_info
[83] & 0x0400 /* 48 bit address support */
1257 && total_sectors
== 0x0FFFFFFF) /* and disk size >= 128 GiB */
1258 { /* (needs BigLBA addressing) */
1259 if (identify_info
[102] || identify_info
[103])
1260 panicf("Unsupported disk size: >= 2^32 sectors");
1262 total_sectors
= identify_info
[100] | (identify_info
[101] << 16);
1263 lba48
= true; /* use BigLBA */
1271 rc
= set_features();
1275 mutex_lock(&ata_mtx
); /* Balance unlock below */
1277 last_disk_activity
= current_tick
;
1278 create_thread(ata_thread
, ata_stack
,
1279 sizeof(ata_stack
), 0, ata_thread_name
1280 IF_PRIO(, PRIORITY_USER_INTERFACE
)
1285 rc
= set_multiple_mode(multisectors
);
1289 mutex_unlock(&ata_mtx
);
1293 #if (CONFIG_LED == LED_REAL)
1294 void ata_set_led_enabled(bool enabled
)
1296 ata_led_enabled
= enabled
;
1297 if (ata_led_enabled
)