1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Alan Korr
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
33 #include "ata_idle_notify.h"
34 #include "ata-target.h"
37 #define SECTOR_SIZE (512)
39 #define ATA_FEATURE ATA_ERROR
41 #define ATA_STATUS ATA_COMMAND
42 #define ATA_ALT_STATUS ATA_CONTROL
44 #define SELECT_DEVICE1 0x10
45 #define SELECT_LBA 0x40
47 #define CONTROL_nIEN 0x02
48 #define CONTROL_SRST 0x04
50 #define CMD_READ_SECTORS 0x20
51 #define CMD_WRITE_SECTORS 0x30
52 #define CMD_WRITE_SECTORS_EXT 0x34
53 #define CMD_READ_MULTIPLE 0xC4
54 #define CMD_READ_MULTIPLE_EXT 0x29
55 #define CMD_WRITE_MULTIPLE 0xC5
56 #define CMD_WRITE_MULTIPLE_EXT 0x39
57 #define CMD_SET_MULTIPLE_MODE 0xC6
58 #define CMD_STANDBY_IMMEDIATE 0xE0
59 #define CMD_STANDBY 0xE2
60 #define CMD_IDENTIFY 0xEC
61 #define CMD_SLEEP 0xE6
62 #define CMD_SET_FEATURES 0xEF
63 #define CMD_SECURITY_FREEZE_LOCK 0xF5
65 #define CMD_READ_DMA 0xC8
66 #define CMD_READ_DMA_EXT 0x25
67 #define CMD_WRITE_DMA 0xCA
68 #define CMD_WRITE_DMA_EXT 0x35
71 /* Should all be < 0x100 (which are reserved for control messages) */
75 #define READWRITE_TIMEOUT 5*HZ
77 #ifdef HAVE_ATA_POWER_OFF
78 #define ATA_POWER_OFF_TIMEOUT 2*HZ
81 #ifdef ATA_DRIVER_CLOSE
82 static unsigned int ata_thread_id
= 0;
85 #if defined(MAX_PHYS_SECTOR_SIZE) && MEM == 64
86 /* Hack - what's the deal with 5g? */
89 struct thread_entry
*thread
;
91 volatile unsigned char locked
;
92 IF_COP( struct corelock cl
; )
95 static void ata_lock_init(struct ata_lock
*l
)
97 corelock_init(&l
->cl
);
103 static void ata_lock_lock(struct ata_lock
*l
)
105 struct thread_entry
* const current
=
106 thread_id_entry(THREAD_ID_CURRENT
);
108 if (current
== l
->thread
)
114 corelock_lock(&l
->cl
);
116 IF_PRIO( current
->skip_count
= -1; )
118 while (l
->locked
!= 0)
120 corelock_unlock(&l
->cl
);
122 corelock_lock(&l
->cl
);
127 corelock_unlock(&l
->cl
);
130 static void ata_lock_unlock(struct ata_lock
*l
)
138 corelock_lock(&l
->cl
);
140 IF_PRIO( l
->thread
->skip_count
= 0; )
145 corelock_unlock(&l
->cl
);
148 #define mutex ata_lock
149 #define mutex_init ata_lock_init
150 #define mutex_lock ata_lock_lock
151 #define mutex_unlock ata_lock_unlock
152 #endif /* MAX_PHYS_SECTOR_SIZE */
154 #if defined(HAVE_USBSTACK) && defined(USE_ROCKBOX_USB)
155 #define ALLOW_USB_SPINDOWN
158 static struct mutex ata_mtx SHAREDBSS_ATTR
;
159 static int ata_device
; /* device 0 (master) or 1 (slave) */
161 static int spinup_time
= 0;
162 #if (CONFIG_LED == LED_REAL)
163 static bool ata_led_enabled
= true;
164 static bool ata_led_on
= false;
166 static bool spinup
= false;
167 static bool sleeping
= true;
168 static bool poweroff
= false;
169 static long sleep_timeout
= 5*HZ
;
171 static bool lba48
= false; /* set for 48 bit addressing */
173 static long ata_stack
[(DEFAULT_STACK_SIZE
*3)/sizeof(long)];
174 static const char ata_thread_name
[] = "ata";
175 static struct event_queue ata_queue SHAREDBSS_ATTR
;
176 static bool initialized
= false;
178 static long last_user_activity
= -1;
179 static long last_disk_activity
= -1;
181 static unsigned long total_sectors
;
182 static int multisectors
; /* number of supported multisectors */
183 static unsigned short identify_info
[SECTOR_SIZE
/2];
185 #ifdef MAX_PHYS_SECTOR_SIZE
187 struct sector_cache_entry
{
189 unsigned long sectornum
; /* logical sector */
190 unsigned char data
[MAX_PHYS_SECTOR_SIZE
];
192 /* buffer for reading and writing large physical sectors */
194 static struct sector_cache_entry sector_cache
;
195 static int phys_sector_mult
= 1;
199 static int dma_mode
= 0;
202 static int ata_power_on(void);
203 static int perform_soft_reset(void);
204 static int set_multiple_mode(int sectors
);
205 static int set_features(void);
207 STATICIRAM ICODE_ATTR
int wait_for_bsy(void)
209 long timeout
= current_tick
+ HZ
*30;
213 if (!(ATA_STATUS
& STATUS_BSY
))
215 last_disk_activity
= current_tick
;
217 } while (TIME_BEFORE(current_tick
, timeout
));
219 return 0; /* timeout */
222 STATICIRAM ICODE_ATTR
int wait_for_rdy(void)
229 timeout
= current_tick
+ HZ
*10;
233 if (ATA_ALT_STATUS
& STATUS_RDY
)
235 last_disk_activity
= current_tick
;
237 } while (TIME_BEFORE(current_tick
, timeout
));
239 return 0; /* timeout */
242 STATICIRAM ICODE_ATTR
int wait_for_start_of_transfer(void)
247 return (ATA_ALT_STATUS
& (STATUS_BSY
|STATUS_DRQ
)) == STATUS_DRQ
;
250 STATICIRAM ICODE_ATTR
int wait_for_end_of_transfer(void)
254 return (ATA_ALT_STATUS
&
255 (STATUS_BSY
|STATUS_RDY
|STATUS_DF
|STATUS_DRQ
|STATUS_ERR
))
259 #if (CONFIG_LED == LED_REAL)
260 /* Conditionally block LED access for the ATA driver, so the LED can be
261 * (mis)used for other purposes */
262 static void ata_led(bool on
)
269 #define ata_led(on) led(on)
272 #ifndef ATA_OPTIMIZED_READING
273 STATICIRAM ICODE_ATTR
void copy_read_sectors(unsigned char* buf
, int wordcount
)
275 unsigned short tmp
= 0;
277 if ( (unsigned long)buf
& 1)
278 { /* not 16-bit aligned, copy byte by byte */
279 unsigned char* bufend
= buf
+ wordcount
*2;
283 #if defined(ATA_SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN)
284 *buf
++ = tmp
& 0xff; /* I assume big endian */
285 *buf
++ = tmp
>> 8; /* and don't use the SWAB16 macro */
290 } while (buf
< bufend
); /* tail loop is faster */
293 { /* 16-bit aligned, can do faster copy */
294 unsigned short* wbuf
= (unsigned short*)buf
;
295 unsigned short* wbufend
= wbuf
+ wordcount
;
298 #ifdef ATA_SWAP_WORDS
299 *wbuf
= swap16(ATA_DATA
);
303 } while (++wbuf
< wbufend
); /* tail loop is faster */
306 #endif /* !ATA_OPTIMIZED_READING */
308 #ifndef ATA_OPTIMIZED_WRITING
309 STATICIRAM ICODE_ATTR
void copy_write_sectors(const unsigned char* buf
,
312 if ( (unsigned long)buf
& 1)
313 { /* not 16-bit aligned, copy byte by byte */
314 unsigned short tmp
= 0;
315 const unsigned char* bufend
= buf
+ wordcount
*2;
318 #if defined(ATA_SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN)
319 tmp
= (unsigned short) *buf
++;
320 tmp
|= (unsigned short) *buf
++ << 8;
321 SET_16BITREG(ATA_DATA
, tmp
);
323 tmp
= (unsigned short) *buf
++ << 8;
324 tmp
|= (unsigned short) *buf
++;
325 SET_16BITREG(ATA_DATA
, tmp
);
327 } while (buf
< bufend
); /* tail loop is faster */
330 { /* 16-bit aligned, can do faster copy */
331 unsigned short* wbuf
= (unsigned short*)buf
;
332 unsigned short* wbufend
= wbuf
+ wordcount
;
335 #ifdef ATA_SWAP_WORDS
336 SET_16BITREG(ATA_DATA
, swap16(*wbuf
));
338 SET_16BITREG(ATA_DATA
, *wbuf
);
340 } while (++wbuf
< wbufend
); /* tail loop is faster */
343 #endif /* !ATA_OPTIMIZED_WRITING */
345 static int ata_transfer_sectors(unsigned long start
,
359 #ifndef MAX_PHYS_SECTOR_SIZE
360 mutex_lock(&ata_mtx
);
363 if (start
+ incount
> total_sectors
) {
368 last_disk_activity
= current_tick
;
369 spinup_start
= current_tick
;
374 sleeping
= false; /* set this now since it'll be on */
377 if (ata_power_on()) {
383 if (perform_soft_reset()) {
390 timeout
= current_tick
+ READWRITE_TIMEOUT
;
392 SET_REG(ATA_SELECT
, ata_device
);
402 while (TIME_BEFORE(current_tick
, timeout
)) {
404 last_disk_activity
= current_tick
;
407 /* If DMA is supported and parameters are ok for DMA, use it */
408 if (dma_mode
&& ata_dma_setup(inbuf
, incount
* SECTOR_SIZE
, write
))
415 SET_REG(ATA_NSECTOR
, count
>> 8);
416 SET_REG(ATA_NSECTOR
, count
& 0xff);
417 SET_REG(ATA_SECTOR
, (start
>> 24) & 0xff); /* 31:24 */
418 SET_REG(ATA_SECTOR
, start
& 0xff); /* 7:0 */
419 SET_REG(ATA_LCYL
, 0); /* 39:32 */
420 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff); /* 15:8 */
421 SET_REG(ATA_HCYL
, 0); /* 47:40 */
422 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff); /* 23:16 */
423 SET_REG(ATA_SELECT
, SELECT_LBA
| ata_device
);
426 SET_REG(ATA_COMMAND
, usedma
? CMD_WRITE_DMA_EXT
: CMD_WRITE_MULTIPLE_EXT
);
428 SET_REG(ATA_COMMAND
, usedma
? CMD_READ_DMA_EXT
: CMD_READ_MULTIPLE_EXT
);
430 SET_REG(ATA_COMMAND
, write
? CMD_WRITE_MULTIPLE_EXT
: CMD_READ_MULTIPLE_EXT
);
436 SET_REG(ATA_NSECTOR
, count
& 0xff); /* 0 means 256 sectors */
437 SET_REG(ATA_SECTOR
, start
& 0xff);
438 SET_REG(ATA_LCYL
, (start
>> 8) & 0xff);
439 SET_REG(ATA_HCYL
, (start
>> 16) & 0xff);
440 SET_REG(ATA_SELECT
, ((start
>> 24) & 0xf) | SELECT_LBA
| ata_device
);
443 SET_REG(ATA_COMMAND
, usedma
? CMD_WRITE_DMA
: CMD_WRITE_MULTIPLE
);
445 SET_REG(ATA_COMMAND
, usedma
? CMD_READ_DMA
: CMD_READ_MULTIPLE
);
447 SET_REG(ATA_COMMAND
, write
? CMD_WRITE_MULTIPLE
: CMD_READ_MULTIPLE
);
451 /* wait at least 400ns between writing command and reading status */
452 __asm__
volatile ("nop");
453 __asm__
volatile ("nop");
454 __asm__
volatile ("nop");
455 __asm__
volatile ("nop");
456 __asm__
volatile ("nop");
460 if (!ata_dma_finish())
464 perform_soft_reset();
469 spinup_time
= current_tick
- spinup_start
;
475 #endif /* HAVE_ATA_DMA */
483 if (!wait_for_start_of_transfer()) {
484 /* We have timed out waiting for RDY and/or DRQ, possibly
485 because the hard drive is shaking and has problems
486 reading the data. We have two options:
488 2) Perform a soft reset and try again.
490 We choose alternative 2.
492 perform_soft_reset();
498 spinup_time
= current_tick
- spinup_start
;
503 /* read the status register exactly once per loop */
507 if (count
>= multisectors
)
508 sectors
= multisectors
;
512 wordcount
= sectors
* SECTOR_SIZE
/ 2;
515 copy_write_sectors(buf
, wordcount
);
517 copy_read_sectors(buf
, wordcount
);
520 "Device errors encountered during READ MULTIPLE commands
521 are posted at the beginning of the block or partial block
522 transfer, but the DRQ bit is still set to one and the data
523 transfer shall take place, including transfer of corrupted
527 if ( status
& (STATUS_BSY
| STATUS_ERR
| STATUS_DF
) ) {
528 perform_soft_reset();
530 /* no point retrying IDNF, sector no. was invalid */
531 if (error
& ERROR_IDNF
)
536 buf
+= sectors
* SECTOR_SIZE
; /* Advance one chunk of sectors */
539 last_disk_activity
= current_tick
;
543 if(!ret
&& !wait_for_end_of_transfer()) {
547 perform_soft_reset();
549 /* no point retrying IDNF, sector no. was invalid */
550 if (error
& ERROR_IDNF
)
559 #ifndef MAX_PHYS_SECTOR_SIZE
560 mutex_unlock(&ata_mtx
);
566 #ifndef MAX_PHYS_SECTOR_SIZE
567 int ata_read_sectors(IF_MD2(int drive
,)
572 #ifdef HAVE_MULTIDRIVE
573 (void)drive
; /* unused for now */
576 return ata_transfer_sectors(start
, incount
, inbuf
, false);
580 #ifndef MAX_PHYS_SECTOR_SIZE
581 int ata_write_sectors(IF_MD2(int drive
,)
586 #ifdef HAVE_MULTIDRIVE
587 (void)drive
; /* unused for now */
590 return ata_transfer_sectors(start
, count
, (void*)buf
, true);
594 #ifdef MAX_PHYS_SECTOR_SIZE
595 static int cache_sector(unsigned long sector
)
599 sector
&= ~(phys_sector_mult
- 1);
600 /* round down to physical sector boundary */
602 /* check whether the sector is already cached */
603 if (sector_cache
.inuse
&& (sector_cache
.sectornum
== sector
))
606 /* not found: read the sector */
607 sector_cache
.inuse
= false;
608 rc
= ata_transfer_sectors(sector
, phys_sector_mult
, sector_cache
.data
, false);
611 sector_cache
.sectornum
= sector
;
612 sector_cache
.inuse
= true;
617 static inline int flush_current_sector(void)
619 return ata_transfer_sectors(sector_cache
.sectornum
, phys_sector_mult
,
620 sector_cache
.data
, true);
623 int ata_read_sectors(IF_MD2(int drive
,)
631 #ifdef HAVE_MULTIDRIVE
632 (void)drive
; /* unused for now */
634 mutex_lock(&ata_mtx
);
636 offset
= start
& (phys_sector_mult
- 1);
638 if (offset
) /* first partial sector */
640 int partcount
= MIN(incount
, phys_sector_mult
- offset
);
642 rc
= cache_sector(start
);
648 memcpy(inbuf
, sector_cache
.data
+ offset
* SECTOR_SIZE
,
649 partcount
* SECTOR_SIZE
);
652 inbuf
+= partcount
* SECTOR_SIZE
;
653 incount
-= partcount
;
657 offset
= incount
& (phys_sector_mult
- 1);
662 rc
= ata_transfer_sectors(start
, incount
, inbuf
, false);
669 inbuf
+= incount
* SECTOR_SIZE
;
673 rc
= cache_sector(start
);
679 memcpy(inbuf
, sector_cache
.data
, offset
* SECTOR_SIZE
);
684 mutex_unlock(&ata_mtx
);
689 int ata_write_sectors(IF_MD2(int drive
,)
697 #ifdef HAVE_MULTIDRIVE
698 (void)drive
; /* unused for now */
700 mutex_lock(&ata_mtx
);
702 offset
= start
& (phys_sector_mult
- 1);
704 if (offset
) /* first partial sector */
706 int partcount
= MIN(count
, phys_sector_mult
- offset
);
708 rc
= cache_sector(start
);
714 memcpy(sector_cache
.data
+ offset
* SECTOR_SIZE
, buf
,
715 partcount
* SECTOR_SIZE
);
716 rc
= flush_current_sector();
723 buf
+= partcount
* SECTOR_SIZE
;
728 offset
= count
& (phys_sector_mult
- 1);
733 rc
= ata_transfer_sectors(start
, count
, (void*)buf
, true);
740 buf
+= count
* SECTOR_SIZE
;
744 rc
= cache_sector(start
);
750 memcpy(sector_cache
.data
, buf
, offset
* SECTOR_SIZE
);
751 rc
= flush_current_sector();
761 mutex_unlock(&ata_mtx
);
765 #endif /* MAX_PHYS_SECTOR_SIZE */
767 static int check_registers(void)
770 if ( ATA_STATUS
& STATUS_BSY
)
773 for (i
= 0; i
<64; i
++) {
774 SET_REG(ATA_NSECTOR
, WRITE_PATTERN1
);
775 SET_REG(ATA_SECTOR
, WRITE_PATTERN2
);
776 SET_REG(ATA_LCYL
, WRITE_PATTERN3
);
777 SET_REG(ATA_HCYL
, WRITE_PATTERN4
);
779 if (((ATA_NSECTOR
& READ_PATTERN1_MASK
) == READ_PATTERN1
) &&
780 ((ATA_SECTOR
& READ_PATTERN2_MASK
) == READ_PATTERN2
) &&
781 ((ATA_LCYL
& READ_PATTERN3_MASK
) == READ_PATTERN3
) &&
782 ((ATA_HCYL
& READ_PATTERN4_MASK
) == READ_PATTERN4
))
788 static int freeze_lock(void)
790 /* does the disk support Security Mode feature set? */
791 if (identify_info
[82] & 2)
793 SET_REG(ATA_SELECT
, ata_device
);
798 SET_REG(ATA_COMMAND
, CMD_SECURITY_FREEZE_LOCK
);
807 void ata_spindown(int seconds
)
809 sleep_timeout
= seconds
* HZ
;
812 bool ata_disk_is_active(void)
817 static int ata_perform_sleep(void)
819 /* guard against calls made with checks of these variables outside
820 the mutex that may not be on the ata thread; status may have changed. */
821 if (spinup
|| sleeping
) {
825 SET_REG(ATA_SELECT
, ata_device
);
827 if(!wait_for_rdy()) {
828 DEBUGF("ata_perform_sleep() - not RDY\n");
832 SET_REG(ATA_COMMAND
, CMD_SLEEP
);
836 DEBUGF("ata_perform_sleep() - CMD failed\n");
846 queue_post(&ata_queue
, Q_SLEEP
, 0);
849 void ata_sleepnow(void)
851 if (!spinup
&& !sleeping
&& initialized
)
853 call_storage_idle_notifys(false);
854 mutex_lock(&ata_mtx
);
856 mutex_unlock(&ata_mtx
);
862 last_user_activity
= current_tick
;
865 static void ata_thread(void)
867 static long last_sleep
= 0;
868 struct queue_event ev
;
869 #ifdef ALLOW_USB_SPINDOWN
870 static bool usb_mode
= false;
874 queue_wait_w_tmo(&ata_queue
, &ev
, HZ
/2);
878 if (!spinup
&& !sleeping
)
880 if (TIME_AFTER( current_tick
,
881 last_disk_activity
+ (HZ
*2) ) )
883 #ifdef ALLOW_USB_SPINDOWN
887 call_storage_idle_notifys(false);
891 if ( sleep_timeout
&&
892 TIME_AFTER( current_tick
,
893 last_user_activity
+ sleep_timeout
) &&
894 TIME_AFTER( current_tick
,
895 last_disk_activity
+ sleep_timeout
) )
897 #ifdef ALLOW_USB_SPINDOWN
901 call_storage_idle_notifys(true);
903 mutex_lock(&ata_mtx
);
905 last_sleep
= current_tick
;
906 mutex_unlock(&ata_mtx
);
910 #ifdef HAVE_ATA_POWER_OFF
911 if ( !spinup
&& sleeping
&& !poweroff
&&
912 TIME_AFTER( current_tick
, last_sleep
+ ATA_POWER_OFF_TIMEOUT
))
914 mutex_lock(&ata_mtx
);
915 ide_power_enable(false);
917 mutex_unlock(&ata_mtx
);
923 case SYS_USB_CONNECTED
:
924 /* Tell the USB thread that we are safe */
925 DEBUGF("ata_thread got SYS_USB_CONNECTED\n");
926 #ifdef ALLOW_USB_SPINDOWN
928 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
929 /* There is no need to force ATA power on */
931 mutex_lock(&ata_mtx
);
934 sleeping
= false; /* set this now since it'll be on */
941 perform_soft_reset();
946 mutex_unlock(&ata_mtx
);
948 /* Wait until the USB cable is extracted again */
949 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
950 usb_wait_for_disconnect(&ata_queue
);
954 #ifdef ALLOW_USB_SPINDOWN
955 case SYS_USB_DISCONNECTED
:
956 /* Tell the USB thread that we are ready again */
957 DEBUGF("ata_thread got SYS_USB_DISCONNECTED\n");
958 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
962 #endif /* USB_NONE */
965 #ifdef ALLOW_USB_SPINDOWN
969 call_storage_idle_notifys(false);
971 last_disk_activity
= current_tick
- sleep_timeout
+ (HZ
/2);
974 #ifdef ATA_DRIVER_CLOSE
982 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
983 static int ata_hard_reset(void)
987 mutex_lock(&ata_mtx
);
992 SET_REG(ATA_SELECT
, ata_device
); /* select the right device */
993 ret
= wait_for_bsy();
995 /* Massage the return code so it is 0 on success and -1 on failure */
998 mutex_unlock(&ata_mtx
);
1003 static int perform_soft_reset(void)
1005 /* If this code is allowed to run on a Nano, the next reads from the flash will
1006 * time out, so we disable it. It shouldn't be necessary anyway, since the
1007 * ATA -> Flash interface automatically sleeps almost immediately after the
1013 SET_REG(ATA_SELECT
, SELECT_LBA
| ata_device
);
1014 SET_REG(ATA_CONTROL
, CONTROL_nIEN
|CONTROL_SRST
);
1015 sleep(1); /* >= 5us */
1018 /* DMA requires INTRQ be enabled */
1019 SET_REG(ATA_CONTROL
, 0);
1021 SET_REG(ATA_CONTROL
, CONTROL_nIEN
);
1023 sleep(1); /* >2ms */
1025 /* This little sucker can take up to 30 seconds */
1029 ret
= wait_for_rdy();
1030 } while(!ret
&& retry_count
--);
1038 if (set_multiple_mode(multisectors
))
1047 int ata_soft_reset(void)
1051 mutex_lock(&ata_mtx
);
1053 ret
= perform_soft_reset();
1055 mutex_unlock(&ata_mtx
);
1059 static int ata_power_on(void)
1063 ide_power_enable(true);
1064 sleep(HZ
/4); /* allow voltage to build up */
1066 /* Accessing the PP IDE controller too early after powering up the disk
1067 * makes the core hang for a short time, causing an audio dropout. This
1068 * also depends on the disk; iPod Mini G2 needs at least HZ/5 to get rid
1069 * of the dropout. Since this time isn't additive (the wait_for_bsy() in
1070 * ata_hard_reset() will shortened by the same amount), it's a good idea
1071 * to do this on all HDD based targets. */
1073 if( ata_hard_reset() )
1076 rc
= set_features();
1080 if (set_multiple_mode(multisectors
))
1089 static int master_slave_detect(void)
1092 SET_REG(ATA_SELECT
, 0);
1093 if ( ATA_STATUS
& (STATUS_RDY
|STATUS_BSY
) ) {
1095 DEBUGF("Found master harddisk\n");
1099 SET_REG(ATA_SELECT
, SELECT_DEVICE1
);
1100 if ( ATA_STATUS
& (STATUS_RDY
|STATUS_BSY
) ) {
1101 ata_device
= SELECT_DEVICE1
;
1102 DEBUGF("Found slave harddisk\n");
1110 static int identify(void)
1114 SET_REG(ATA_SELECT
, ata_device
);
1116 if(!wait_for_rdy()) {
1117 DEBUGF("identify() - not RDY\n");
1120 SET_REG(ATA_COMMAND
, CMD_IDENTIFY
);
1122 if (!wait_for_start_of_transfer())
1124 DEBUGF("identify() - CMD failed\n");
1128 for (i
=0; i
<SECTOR_SIZE
/2; i
++) {
1129 /* the IDENTIFY words are already swapped, so we need to treat
1130 this info differently that normal sector data */
1131 #if defined(ROCKBOX_BIG_ENDIAN) && !defined(ATA_SWAP_WORDS)
1132 identify_info
[i
] = swap16(ATA_DATA
);
1134 identify_info
[i
] = ATA_DATA
;
1141 static int set_multiple_mode(int sectors
)
1143 SET_REG(ATA_SELECT
, ata_device
);
1145 if(!wait_for_rdy()) {
1146 DEBUGF("set_multiple_mode() - not RDY\n");
1150 SET_REG(ATA_NSECTOR
, sectors
);
1151 SET_REG(ATA_COMMAND
, CMD_SET_MULTIPLE_MODE
);
1153 if (!wait_for_rdy())
1155 DEBUGF("set_multiple_mode() - CMD failed\n");
1163 static int get_best_mode(unsigned short identword
, int max
, int modetype
)
1165 unsigned short testbit
= BIT_N(max
);
1168 if (identword
& testbit
)
1169 return max
| modetype
;
1178 static int set_features(void)
1181 unsigned char id_word
;
1182 unsigned char id_bit
;
1183 unsigned char subcommand
;
1184 unsigned char parameter
;
1186 { 83, 14, 0x03, 0 }, /* force PIO mode */
1187 { 83, 3, 0x05, 0x80 }, /* adv. power management: lowest w/o standby */
1188 { 83, 9, 0x42, 0x80 }, /* acoustic management: lowest noise */
1189 { 82, 6, 0xaa, 0 }, /* enable read look-ahead */
1191 { 0, 0, 0x03, 0 }, /* DMA mode */
1197 /* Find out the highest supported PIO mode */
1198 if(identify_info
[64] & 2)
1201 if(identify_info
[64] & 1)
1204 /* Update the table: set highest supported pio mode that we also support */
1205 features
[0].parameter
= 8 + pio_mode
;
1208 if (identify_info
[53] & (1<<2))
1209 /* Ultra DMA mode info present, find a mode */
1210 dma_mode
= get_best_mode(identify_info
[88], ATA_MAX_UDMA
, 0x40);
1213 /* No UDMA mode found, try to find a multi-word DMA mode */
1214 dma_mode
= get_best_mode(identify_info
[63], ATA_MAX_MWDMA
, 0x20);
1215 features
[4].id_word
= 63;
1218 features
[4].id_word
= 88;
1220 features
[4].id_bit
= dma_mode
& 7;
1221 features
[4].parameter
= dma_mode
;
1222 #endif /* HAVE_ATA_DMA */
1224 SET_REG(ATA_SELECT
, ata_device
);
1226 if (!wait_for_rdy()) {
1227 DEBUGF("set_features() - not RDY\n");
1231 for (i
=0; i
< (int)(sizeof(features
)/sizeof(features
[0])); i
++) {
1232 if (identify_info
[features
[i
].id_word
] & BIT_N(features
[i
].id_bit
)) {
1233 SET_REG(ATA_FEATURE
, features
[i
].subcommand
);
1234 SET_REG(ATA_NSECTOR
, features
[i
].parameter
);
1235 SET_REG(ATA_COMMAND
, CMD_SET_FEATURES
);
1237 if (!wait_for_rdy()) {
1238 DEBUGF("set_features() - CMD failed\n");
1242 if((ATA_ALT_STATUS
& STATUS_ERR
) && (i
!= 1)) {
1243 /* some CF cards don't like advanced powermanagement
1244 even if they mark it as supported - go figure... */
1245 if(ATA_ERROR
& ERROR_ABRT
) {
1252 #ifdef ATA_SET_DEVICE_FEATURES
1253 ata_set_pio_timings(pio_mode
);
1257 ata_dma_set_mode(dma_mode
);
1263 unsigned short* ata_get_identify(void)
1265 return identify_info
;
1268 static int init_and_check(bool hard_reset
)
1274 /* This should reset both master and slave, we don't yet know what's in */
1276 if (ata_hard_reset())
1280 rc
= master_slave_detect();
1284 /* symptom fix: else check_registers() below may fail */
1285 if (hard_reset
&& !wait_for_bsy())
1288 rc
= check_registers();
1300 if ( !initialized
) {
1301 mutex_init(&ata_mtx
);
1302 queue_init(&ata_queue
, true);
1305 mutex_lock(&ata_mtx
);
1307 /* must be called before ata_device_init() */
1308 coldstart
= ata_is_coldstart();
1313 #ifdef MAX_PHYS_SECTOR_SIZE
1314 memset(§or_cache
, 0, sizeof(sector_cache
));
1317 if ( !initialized
) {
1318 /* First call won't have multiple thread contention - this
1319 * may return at any point without having to unlock */
1320 mutex_unlock(&ata_mtx
);
1322 if (!ide_powered()) /* somebody has switched it off */
1324 ide_power_enable(true);
1325 sleep(HZ
/4); /* allow voltage to build up */
1329 /* DMA requires INTRQ be enabled */
1330 SET_REG(ATA_CONTROL
, 0);
1333 /* first try, hard reset at cold start only */
1334 rc
= init_and_check(coldstart
);
1337 { /* failed? -> second try, always with hard reset */
1338 DEBUGF("ata: init failed, retrying...\n");
1339 rc
= init_and_check(true);
1349 multisectors
= identify_info
[47] & 0xff;
1350 if (multisectors
== 0) /* Invalid multisector info, try with 16 */
1353 DEBUGF("ata: %d sectors per ata request\n",multisectors
);
1355 total_sectors
= identify_info
[60] | (identify_info
[61] << 16);
1358 if (identify_info
[83] & 0x0400 /* 48 bit address support */
1359 && total_sectors
== 0x0FFFFFFF) /* and disk size >= 128 GiB */
1360 { /* (needs BigLBA addressing) */
1361 if (identify_info
[102] || identify_info
[103])
1362 panicf("Unsupported disk size: >= 2^32 sectors");
1364 total_sectors
= identify_info
[100] | (identify_info
[101] << 16);
1365 lba48
= true; /* use BigLBA */
1373 rc
= set_features();
1377 #ifdef MAX_PHYS_SECTOR_SIZE
1378 /* Find out the physical sector size */
1379 if((identify_info
[106] & 0xe000) == 0x6000)
1380 phys_sector_mult
= BIT_N(identify_info
[106] & 0x000f);
1382 phys_sector_mult
= 1;
1384 DEBUGF("ata: %d logical sectors per phys sector", phys_sector_mult
);
1386 if (phys_sector_mult
> 1)
1388 /* Check if drive really needs emulation - if we can access
1389 * sector 1 then assume the drive will handle it better than
1390 * us, and ignore the large physical sectors.
1392 char throwaway
[SECTOR_SIZE
];
1393 rc
= ata_transfer_sectors(1, 1, &throwaway
, false);
1395 phys_sector_mult
= 1;
1398 if (phys_sector_mult
> (MAX_PHYS_SECTOR_SIZE
/SECTOR_SIZE
))
1399 panicf("Unsupported physical sector size: %d",
1400 phys_sector_mult
* SECTOR_SIZE
);
1403 mutex_lock(&ata_mtx
); /* Balance unlock below */
1405 last_disk_activity
= current_tick
;
1406 #ifdef ATA_DRIVER_CLOSE
1409 create_thread(ata_thread
, ata_stack
,
1410 sizeof(ata_stack
), 0, ata_thread_name
1411 IF_PRIO(, PRIORITY_USER_INTERFACE
)
1416 rc
= set_multiple_mode(multisectors
);
1420 mutex_unlock(&ata_mtx
);
1424 #ifdef ATA_DRIVER_CLOSE
1425 void ata_close(void)
1427 unsigned int thread_id
= ata_thread_id
;
1434 queue_post(&ata_queue
, Q_CLOSE
, 0);
1435 thread_wait(thread_id
);
1437 #endif /* ATA_DRIVER_CLOSE */
1439 #if (CONFIG_LED == LED_REAL)
1440 void ata_set_led_enabled(bool enabled
)
1442 ata_led_enabled
= enabled
;
1443 if (ata_led_enabled
)
1450 long ata_last_disk_activity(void)
1452 return last_disk_activity
;
1455 int ata_spinup_time(void)
1460 #ifdef STORAGE_GET_INFO
1461 void ata_get_info(IF_MD2(int drive
,)struct storage_info
*info
)
1463 unsigned short *src
,*dest
;
1464 static char vendor
[8];
1465 static char product
[16];
1466 static char revision
[4];
1467 #ifdef HAVE_MULTIDRIVE
1468 (void)drive
; /* unused for now */
1471 info
->sector_size
= SECTOR_SIZE
;
1472 info
->num_sectors
= total_sectors
;
1474 src
= (unsigned short*)&identify_info
[27];
1475 dest
= (unsigned short*)vendor
;
1477 dest
[i
] = htobe16(src
[i
]);
1478 info
->vendor
=vendor
;
1480 src
= (unsigned short*)&identify_info
[31];
1481 dest
= (unsigned short*)product
;
1483 dest
[i
] = htobe16(src
[i
]);
1484 info
->product
=product
;
1486 src
= (unsigned short*)&identify_info
[23];
1487 dest
= (unsigned short*)revision
;
1489 dest
[i
] = htobe16(src
[i
]);
1490 info
->revision
=revision
;
1495 /* Returns last DMA mode as set by set_features() */
1496 int ata_get_dma_mode(void)
1501 /* Needed to allow updating while waiting for DMA to complete */
1502 void ata_keep_active(void)
1504 last_disk_activity
= current_tick
;
1508 #ifdef CONFIG_STORAGE_MULTI
1509 int ata_num_drives(int first_drive
)
1511 /* We don't care which logical drive number(s) we have been assigned */