Add a note about Rockbox not running on Sansas v2 (FS#8477 by Marc Guay).
[Rockbox.git] / firmware / drivers / ata.c
blobe067235d95a6c877880c72f0dd393ae84256c6c0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
19 #include <stdbool.h>
20 #include "ata.h"
21 #include "kernel.h"
22 #include "thread.h"
23 #include "led.h"
24 #include "cpu.h"
25 #include "system.h"
26 #include "debug.h"
27 #include "panic.h"
28 #include "usb.h"
29 #include "power.h"
30 #include "string.h"
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
61 #define Q_SLEEP 0
63 #define READ_TIMEOUT 5*HZ
65 #ifdef HAVE_ATA_POWER_OFF
66 #define ATA_POWER_OFF_TIMEOUT 2*HZ
67 #endif
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;
76 #endif
77 static bool spinup = false;
78 static bool sleeping = true;
79 static bool poweroff = false;
80 static long sleep_timeout = 5*HZ;
81 #ifdef HAVE_LBA48
82 static bool lba48 = false; /* set for 48 bit addressing */
83 #endif
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)
106 m->thread = NULL;
107 m->locked = 0;
108 m->count = 0;
109 #if CONFIG_CORELOCK == SW_CORELOCK
110 corelock_init(&m->cl);
111 #endif
114 void ata_spin_lock(struct mutex *m)
116 struct thread_entry *current = thread_get_current();
118 if (current == m->thread)
120 m->count++;
121 return;
124 while (test_and_set(&m->locked, 1, &m->cl))
125 yield();
127 m->thread = current;
130 void ata_spin_unlock(struct mutex *m)
132 if (m->count > 0)
134 m->count--;
135 return;
138 m->thread = NULL;
139 test_and_set(&m->locked, 0, &m->cl);
142 /****/
144 struct sector_cache_entry {
145 bool inuse;
146 unsigned long sectornum; /* logical sector */
147 unsigned char data[MAX_PHYS_SECTOR_SIZE];
149 /* buffer for reading and writing large physical sectors */
150 #define NUMCACHES 2
151 static struct sector_cache_entry sector_cache;
152 static int phys_sector_mult = 1;
153 #endif
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;
166 priority_yield();
169 if (TIME_BEFORE(current_tick, timeout))
170 return 1;
171 else
172 return 0; /* timeout */
175 STATICIRAM int wait_for_rdy(void) ICODE_ATTR;
176 STATICIRAM int wait_for_rdy(void)
178 long timeout;
180 if (!wait_for_bsy())
181 return 0;
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;
188 priority_yield();
191 if (TIME_BEFORE(current_tick, timeout))
192 return STATUS_RDY;
193 else
194 return 0; /* timeout */
197 STATICIRAM int wait_for_start_of_transfer(void) ICODE_ATTR;
198 STATICIRAM int wait_for_start_of_transfer(void)
200 if (!wait_for_bsy())
201 return 0;
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)
209 if (!wait_for_bsy())
210 return 0;
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)
219 ata_led_on = on;
220 if (ata_led_enabled)
221 led(ata_led_on);
223 #else
224 #define ata_led(on) led(on)
225 #endif
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;
238 tmp = ATA_DATA;
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 */
242 #else
243 *buf++ = tmp >> 8;
244 *buf++ = tmp & 0xff;
245 #endif
246 } while (buf < bufend); /* tail loop is faster */
248 else
249 { /* 16-bit aligned, can do faster copy */
250 unsigned short* wbuf = (unsigned short*)buf;
251 unsigned short* wbufend = wbuf + wordcount;
254 #ifdef SWAP_WORDS
255 *wbuf = swap16(ATA_DATA);
256 #else
257 *wbuf = ATA_DATA;
258 #endif
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,
266 int incount,
267 void* inbuf)
268 #else
269 int ata_read_sectors(IF_MV2(int drive,)
270 unsigned long start,
271 int incount,
272 void* inbuf)
273 #endif
275 int ret = 0;
276 long timeout;
277 int count;
278 void* buf;
279 long spinup_start;
281 #ifndef MAX_PHYS_SECTOR_SIZE
282 #ifdef HAVE_MULTIVOLUME
283 (void)drive; /* unused for now */
284 #endif
285 mutex_lock(&ata_mtx);
286 #endif
288 if (start + incount > total_sectors) {
289 ret = -1;
290 goto error;
293 last_disk_activity = current_tick;
294 spinup_start = current_tick;
296 ata_led(true);
298 if ( sleeping ) {
299 spinup = true;
300 if (poweroff) {
301 if (ata_power_on()) {
302 ret = -2;
303 goto error;
306 else {
307 if (perform_soft_reset()) {
308 ret = -2;
309 goto error;
314 timeout = current_tick + READ_TIMEOUT;
316 SET_REG(ATA_SELECT, ata_device);
317 if (!wait_for_rdy())
319 ret = -3;
320 goto error;
323 retry:
324 buf = inbuf;
325 count = incount;
326 while (TIME_BEFORE(current_tick, timeout)) {
327 ret = 0;
328 last_disk_activity = current_tick;
330 #ifdef HAVE_LBA48
331 if (lba48)
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);
344 else
345 #endif
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");
362 while (count) {
363 int sectors;
364 int wordcount;
365 int status;
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:
371 1) Wait some more
372 2) Perform a soft reset and try again.
374 We choose alternative 2.
376 perform_soft_reset();
377 ret = -5;
378 goto retry;
381 if (spinup) {
382 ata_spinup_time = current_tick - spinup_start;
383 spinup = false;
384 sleeping = false;
385 poweroff = false;
388 /* read the status register exactly once per loop */
389 status = ATA_STATUS;
391 if (count >= multisectors )
392 sectors = multisectors;
393 else
394 sectors = count;
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."
405 -- ATA specification
407 if ( status & (STATUS_BSY | STATUS_ERR | STATUS_DF) ) {
408 perform_soft_reset();
409 ret = -6;
410 goto retry;
413 buf += sectors * SECTOR_SIZE; /* Advance one chunk of sectors */
414 count -= sectors;
416 last_disk_activity = current_tick;
419 if(!ret && !wait_for_end_of_transfer()) {
420 perform_soft_reset();
421 ret = -4;
422 goto retry;
424 break;
427 error:
428 ata_led(false);
429 #ifndef MAX_PHYS_SECTOR_SIZE
430 mutex_unlock(&ata_mtx);
431 #endif
433 return ret;
436 #ifndef ATA_OPTIMIZED_WRITING
437 STATICIRAM void copy_write_sectors(const unsigned char* buf, int wordcount)
438 ICODE_ATTR;
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);
451 #else
452 tmp = (unsigned short) *buf++ << 8;
453 tmp |= (unsigned short) *buf++;
454 SET_16BITREG(ATA_DATA, tmp);
455 #endif
456 } while (buf < bufend); /* tail loop is faster */
458 else
459 { /* 16-bit aligned, can do faster copy */
460 unsigned short* wbuf = (unsigned short*)buf;
461 unsigned short* wbufend = wbuf + wordcount;
464 #ifdef SWAP_WORDS
465 SET_16BITREG(ATA_DATA, swap16(*wbuf));
466 #else
467 SET_16BITREG(ATA_DATA, *wbuf);
468 #endif
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,
476 int count,
477 const void* buf)
478 #else
479 int ata_write_sectors(IF_MV2(int drive,)
480 unsigned long start,
481 int count,
482 const void* buf)
483 #endif
485 int i;
486 int ret = 0;
487 long spinup_start;
489 #ifndef MAX_PHYS_SECTOR_SIZE
490 #ifdef HAVE_MULTIVOLUME
491 (void)drive; /* unused for now */
492 #endif
493 mutex_lock(&ata_mtx);
494 #endif
496 if (start + count > total_sectors)
497 panicf("Writing past end of disk");
499 last_disk_activity = current_tick;
500 spinup_start = current_tick;
502 ata_led(true);
504 if ( sleeping ) {
505 spinup = true;
506 if (poweroff) {
507 if (ata_power_on()) {
508 ret = -1;
509 goto error;
512 else {
513 if (perform_soft_reset()) {
514 ret = -1;
515 goto error;
520 SET_REG(ATA_SELECT, ata_device);
521 if (!wait_for_rdy())
523 ret = -2;
524 goto error;
527 #ifdef HAVE_LBA48
528 if (lba48)
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);
541 else
542 #endif
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()) {
555 ret = -3;
556 break;
559 if (spinup) {
560 ata_spinup_time = current_tick - spinup_start;
561 spinup = false;
562 sleeping = false;
563 poweroff = false;
566 copy_write_sectors(buf, SECTOR_SIZE/2);
568 #ifdef USE_INTERRUPT
569 /* reading the status register clears the interrupt */
570 j = ATA_STATUS;
571 #endif
572 buf += SECTOR_SIZE;
574 last_disk_activity = current_tick;
577 if(!ret && !wait_for_end_of_transfer()) {
578 DEBUGF("End on transfer failed. -- jyp");
579 ret = -4;
582 error:
583 ata_led(false);
584 #ifndef MAX_PHYS_SECTOR_SIZE
585 mutex_unlock(&ata_mtx);
586 #endif
588 return ret;
591 #ifdef MAX_PHYS_SECTOR_SIZE
592 static int cache_sector(unsigned long sector)
594 int rc;
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))
601 return 0;
603 /* not found: read the sector */
604 sector_cache.inuse = false;
605 rc = _read_sectors(sector, phys_sector_mult, sector_cache.data);
606 if (!rc)
608 sector_cache.sectornum = sector;
609 sector_cache.inuse = true;
611 return rc;
614 static inline int flush_current_sector(void)
616 return _write_sectors(sector_cache.sectornum, phys_sector_mult,
617 sector_cache.data);
620 int ata_read_sectors(IF_MV2(int drive,)
621 unsigned long start,
622 int incount,
623 void* inbuf)
625 int rc = 0;
626 int offset;
628 #ifdef HAVE_MULTIVOLUME
629 (void)drive; /* unused for now */
630 #endif
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);
640 if (rc)
642 rc = rc * 10 - 1;
643 goto error;
645 memcpy(inbuf, sector_cache.data + offset * SECTOR_SIZE,
646 partcount * SECTOR_SIZE);
648 start += partcount;
649 inbuf += partcount * SECTOR_SIZE;
650 incount -= partcount;
652 if (incount)
654 offset = incount & (phys_sector_mult - 1);
655 incount -= offset;
657 if (incount)
659 rc = _read_sectors(start, incount, inbuf);
660 if (rc)
662 rc = rc * 10 - 2;
663 goto error;
665 start += incount;
666 inbuf += incount * SECTOR_SIZE;
668 if (offset)
670 rc = cache_sector(start);
671 if (rc)
673 rc = rc * 10 - 3;
674 goto error;
676 memcpy(inbuf, sector_cache.data, offset * SECTOR_SIZE);
680 error:
681 mutex_unlock(&ata_mtx);
683 return rc;
686 int ata_write_sectors(IF_MV2(int drive,)
687 unsigned long start,
688 int count,
689 const void* buf)
691 int rc = 0;
692 int offset;
694 #ifdef HAVE_MULTIVOLUME
695 (void)drive; /* unused for now */
696 #endif
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);
706 if (rc)
708 rc = rc * 10 - 1;
709 goto error;
711 memcpy(sector_cache.data + offset * SECTOR_SIZE, buf,
712 partcount * SECTOR_SIZE);
713 rc = flush_current_sector();
714 if (rc)
716 rc = rc * 10 - 2;
717 goto error;
719 start += partcount;
720 buf += partcount * SECTOR_SIZE;
721 count -= partcount;
723 if (count)
725 offset = count & (phys_sector_mult - 1);
726 count -= offset;
728 if (count)
730 rc = _write_sectors(start, count, buf);
731 if (rc)
733 rc = rc * 10 - 3;
734 goto error;
736 start += count;
737 buf += count * SECTOR_SIZE;
739 if (offset)
741 rc = cache_sector(start);
742 if (rc)
744 rc = rc * 10 - 4;
745 goto error;
747 memcpy(sector_cache.data, buf, offset * SECTOR_SIZE);
748 rc = flush_current_sector();
749 if (rc)
751 rc = rc * 10 - 5;
752 goto error;
757 error:
758 mutex_unlock(&ata_mtx);
760 return rc;
762 #endif /* MAX_PHYS_SECTOR_SIZE */
764 static int check_registers(void)
766 int i;
767 if ( ATA_STATUS & STATUS_BSY )
768 return -1;
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))
780 return 0;
782 return -2;
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);
792 if (!wait_for_rdy())
793 return -1;
795 SET_REG(ATA_COMMAND, CMD_SECURITY_FREEZE_LOCK);
797 if (!wait_for_rdy())
798 return -2;
801 return 0;
804 void ata_spindown(int seconds)
806 sleep_timeout = seconds * HZ;
809 bool ata_disk_is_active(void)
811 return !sleeping;
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);
823 return -1;
826 SET_REG(ATA_COMMAND, CMD_SLEEP);
828 if (!wait_for_rdy())
830 DEBUGF("ata_perform_sleep() - CMD failed\n");
831 mutex_unlock(&ata_mtx);
832 return -2;
835 sleeping = true;
836 mutex_unlock(&ata_mtx);
837 return 0;
840 void ata_sleep(void)
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);
850 ata_perform_sleep();
854 void ata_spin(void)
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;
865 while (1) {
866 queue_wait_w_tmo(&ata_queue, &ev, HZ/2);
868 switch ( ev.id ) {
869 case SYS_TIMEOUT:
870 if (!spinup && !sleeping)
872 if (!ata_mtx.locked)
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);
889 ata_perform_sleep();
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);
901 poweroff = true;
903 #endif
904 break;
906 #ifndef USB_NONE
907 case SYS_USB_CONNECTED:
908 if (poweroff) {
909 mutex_lock(&ata_mtx);
910 ata_led(true);
911 ata_power_on();
912 ata_led(false);
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);
922 break;
923 #endif
924 case Q_SLEEP:
925 call_ata_idle_notifys(false);
926 last_disk_activity = current_tick - sleep_timeout + (HZ/2);
927 break;
932 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
933 int ata_hard_reset(void)
935 int ret;
937 ata_reset();
939 /* state HRR2 */
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 */
944 ret = ret?0:-1;
946 return ret;
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
954 * last command.
956 int ret;
957 int retry_count;
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);
964 sleep(1); /* >2ms */
966 /* This little sucker can take up to 30 seconds */
967 retry_count = 8;
970 ret = wait_for_rdy();
971 } while(!ret && retry_count--);
973 if (!ret)
974 return -1;
976 if (set_features())
977 return -2;
979 if (set_multiple_mode(multisectors))
980 return -3;
982 if (freeze_lock())
983 return -4;
985 return 0;
988 int ata_soft_reset(void)
990 int ret;
992 mutex_lock(&ata_mtx);
994 ret = perform_soft_reset();
996 mutex_unlock(&ata_mtx);
997 return ret;
1000 static int ata_power_on(void)
1002 int rc;
1004 ide_power_enable(true);
1005 sleep(HZ/50); /* allow voltage to build up */
1006 if( ata_hard_reset() )
1007 return -1;
1009 rc = set_features();
1010 if (rc)
1011 return rc * 10 - 2;
1013 if (set_multiple_mode(multisectors))
1014 return -3;
1016 if (freeze_lock())
1017 return -4;
1019 return 0;
1022 static int master_slave_detect(void)
1024 /* master? */
1025 SET_REG(ATA_SELECT, 0);
1026 if ( ATA_STATUS & (STATUS_RDY|STATUS_BSY) ) {
1027 ata_device = 0;
1028 DEBUGF("Found master harddisk\n");
1030 else {
1031 /* slave? */
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");
1037 else
1038 return -1;
1040 return 0;
1043 static int identify(void)
1045 int i;
1047 SET_REG(ATA_SELECT, ata_device);
1049 if(!wait_for_rdy()) {
1050 DEBUGF("identify() - not RDY\n");
1051 return -1;
1053 SET_REG(ATA_COMMAND, CMD_IDENTIFY);
1055 if (!wait_for_start_of_transfer())
1057 DEBUGF("identify() - CMD failed\n");
1058 return -2;
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);
1066 #else
1067 identify_info[i] = ATA_DATA;
1068 #endif
1071 return 0;
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");
1080 return -1;
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");
1089 return -2;
1092 return 0;
1095 static int set_features(void)
1097 static struct {
1098 unsigned char id_word;
1099 unsigned char id_bit;
1100 unsigned char subcommand;
1101 unsigned char parameter;
1102 } features[] = {
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 */
1108 int i;
1109 int pio_mode = 2;
1111 /* Find out the highest supported PIO mode */
1112 if(identify_info[64] & 2)
1113 pio_mode = 4;
1114 else
1115 if(identify_info[64] & 1)
1116 pio_mode = 3;
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");
1125 return -1;
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");
1136 return -10 - i;
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) {
1143 return -20 - i;
1149 return 0;
1152 unsigned short* ata_get_identify(void)
1154 return identify_info;
1157 static int init_and_check(bool hard_reset)
1159 int rc;
1161 if (hard_reset)
1163 /* This should reset both master and slave, we don't yet know what's in */
1164 ata_device = 0;
1165 if (ata_hard_reset())
1166 return -1;
1169 rc = master_slave_detect();
1170 if (rc)
1171 return -10 + rc;
1173 /* symptom fix: else check_registers() below may fail */
1174 if (hard_reset && !wait_for_bsy())
1175 return -20;
1177 rc = check_registers();
1178 if (rc)
1179 return -30 + rc;
1181 return 0;
1184 int ata_init(void)
1186 int rc = 0;
1187 bool coldstart;
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();
1198 ata_led(false);
1199 ata_device_init();
1200 sleeping = false;
1201 ata_enable(true);
1202 #ifdef MAX_PHYS_SECTOR_SIZE
1203 memset(&sector_cache, 0, sizeof(sector_cache));
1204 #endif
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);
1220 if (rc)
1221 { /* failed? -> second try, always with hard reset */
1222 DEBUGF("ata: init failed, retrying...\n");
1223 rc = init_and_check(true);
1224 if (rc)
1225 return rc;
1228 rc = identify();
1230 if (rc)
1231 return -40 + rc;
1233 multisectors = identify_info[47] & 0xff;
1234 if (multisectors == 0) /* Invalid multisector info, try with 16 */
1235 multisectors = 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);
1243 else
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);
1251 #endif
1253 total_sectors = identify_info[60] | (identify_info[61] << 16);
1255 #ifdef HAVE_LBA48
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 */
1265 #endif
1266 rc = freeze_lock();
1268 if (rc)
1269 return -50 + rc;
1271 rc = set_features();
1272 if (rc)
1273 return -60 + rc;
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)
1281 IF_COP(, CPU));
1282 initialized = true;
1285 rc = set_multiple_mode(multisectors);
1286 if (rc)
1287 rc = -70 + rc;
1289 mutex_unlock(&ata_mtx);
1290 return rc;
1293 #if (CONFIG_LED == LED_REAL)
1294 void ata_set_led_enabled(bool enabled)
1296 ata_led_enabled = enabled;
1297 if (ata_led_enabled)
1298 led(ata_led_on);
1299 else
1300 led(false);
1302 #endif