Make local variable static
[kugel-rb.git] / firmware / drivers / ata.c
blob55e4892b243451b5f82b8ea445e389757bd809de
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include <stdbool.h>
22 #include "ata.h"
23 #include "kernel.h"
24 #include "thread.h"
25 #include "led.h"
26 #include "cpu.h"
27 #include "system.h"
28 #include "debug.h"
29 #include "panic.h"
30 #include "usb.h"
31 #include "power.h"
32 #include "string.h"
33 #include "ata_idle_notify.h"
34 #include "ata-target.h"
36 #define SECTOR_SIZE (512)
38 #define ATA_FEATURE ATA_ERROR
40 #define ATA_STATUS ATA_COMMAND
41 #define ATA_ALT_STATUS ATA_CONTROL
43 #define SELECT_DEVICE1 0x10
44 #define SELECT_LBA 0x40
46 #define CONTROL_nIEN 0x02
47 #define CONTROL_SRST 0x04
49 #define CMD_READ_SECTORS 0x20
50 #define CMD_WRITE_SECTORS 0x30
51 #define CMD_WRITE_SECTORS_EXT 0x34
52 #define CMD_READ_MULTIPLE 0xC4
53 #define CMD_READ_MULTIPLE_EXT 0x29
54 #define CMD_WRITE_MULTIPLE 0xC5
55 #define CMD_SET_MULTIPLE_MODE 0xC6
56 #define CMD_STANDBY_IMMEDIATE 0xE0
57 #define CMD_STANDBY 0xE2
58 #define CMD_IDENTIFY 0xEC
59 #define CMD_SLEEP 0xE6
60 #define CMD_SET_FEATURES 0xEF
61 #define CMD_SECURITY_FREEZE_LOCK 0xF5
63 #define Q_SLEEP 0
64 #define Q_CLOSE 1
66 #define READ_TIMEOUT 5*HZ
68 #ifdef HAVE_ATA_POWER_OFF
69 #define ATA_POWER_OFF_TIMEOUT 2*HZ
70 #endif
72 #ifdef ATA_DRIVER_CLOSE
73 static struct thread_entry *ata_thread_p = NULL;
74 #endif
76 #if defined(MAX_PHYS_SECTOR_SIZE) && MEM == 64
77 /* Hack - what's the deal with 5g? */
78 struct ata_lock
80 struct thread_entry *thread;
81 int count;
82 volatile unsigned char locked;
83 IF_COP( struct corelock cl; )
86 static void ata_lock_init(struct ata_lock *l)
88 corelock_init(&l->cl);
89 l->locked = 0;
90 l->count = 0;
91 l->thread = NULL;
94 static void ata_lock_lock(struct ata_lock *l)
96 struct thread_entry * const current = thread_get_current();
98 if (current == l->thread)
100 l->count++;
101 return;
104 corelock_lock(&l->cl);
106 IF_PRIO( current->skip_count = -1; )
108 while (l->locked != 0)
110 corelock_unlock(&l->cl);
111 switch_thread();
112 corelock_lock(&l->cl);
115 l->locked = 1;
116 l->thread = current;
117 corelock_unlock(&l->cl);
120 static void ata_lock_unlock(struct ata_lock *l)
122 if (l->count > 0)
124 l->count--;
125 return;
128 corelock_lock(&l->cl);
130 IF_PRIO( l->thread->skip_count = 0; )
132 l->thread = NULL;
133 l->locked = 0;
135 corelock_unlock(&l->cl);
138 #define mutex ata_lock
139 #define mutex_init ata_lock_init
140 #define mutex_lock ata_lock_lock
141 #define mutex_unlock ata_lock_unlock
142 #endif /* MAX_PHYS_SECTOR_SIZE */
144 static struct mutex ata_mtx SHAREDBSS_ATTR;
145 static int ata_device; /* device 0 (master) or 1 (slave) */
147 int ata_spinup_time = 0;
148 #if (CONFIG_LED == LED_REAL)
149 static bool ata_led_enabled = true;
150 static bool ata_led_on = false;
151 #endif
152 static bool spinup = false;
153 static bool sleeping = true;
154 static bool poweroff = false;
155 static long sleep_timeout = 5*HZ;
156 #ifdef HAVE_LBA48
157 static bool lba48 = false; /* set for 48 bit addressing */
158 #endif
159 static long ata_stack[(DEFAULT_STACK_SIZE*3)/sizeof(long)];
160 static const char ata_thread_name[] = "ata";
161 static struct event_queue ata_queue;
162 static bool initialized = false;
164 static long last_user_activity = -1;
165 long last_disk_activity = -1;
167 static unsigned long total_sectors;
168 static int multisectors; /* number of supported multisectors */
169 static unsigned short identify_info[SECTOR_SIZE/2];
171 #ifdef MAX_PHYS_SECTOR_SIZE
173 struct sector_cache_entry {
174 bool inuse;
175 unsigned long sectornum; /* logical sector */
176 unsigned char data[MAX_PHYS_SECTOR_SIZE];
178 /* buffer for reading and writing large physical sectors */
179 #define NUMCACHES 2
180 static struct sector_cache_entry sector_cache;
181 static int phys_sector_mult = 1;
182 #endif
184 static int ata_power_on(void);
185 static int perform_soft_reset(void);
186 static int set_multiple_mode(int sectors);
187 static int set_features(void);
189 STATICIRAM ICODE_ATTR int wait_for_bsy(void)
191 long timeout = current_tick + HZ*30;
195 if (!(ATA_STATUS & STATUS_BSY))
196 return 1;
197 last_disk_activity = current_tick;
198 yield();
199 } while (TIME_BEFORE(current_tick, timeout));
201 return 0; /* timeout */
204 STATICIRAM ICODE_ATTR int wait_for_rdy(void)
206 long timeout;
208 if (!wait_for_bsy())
209 return 0;
211 timeout = current_tick + HZ*10;
215 if (ATA_ALT_STATUS & STATUS_RDY)
216 return 1;
217 last_disk_activity = current_tick;
218 yield();
219 } while (TIME_BEFORE(current_tick, timeout));
221 return 0; /* timeout */
224 STATICIRAM ICODE_ATTR int wait_for_start_of_transfer(void)
226 if (!wait_for_bsy())
227 return 0;
229 return (ATA_ALT_STATUS & (STATUS_BSY|STATUS_DRQ)) == STATUS_DRQ;
232 STATICIRAM ICODE_ATTR int wait_for_end_of_transfer(void)
234 if (!wait_for_bsy())
235 return 0;
236 return (ATA_ALT_STATUS & (STATUS_RDY|STATUS_DRQ)) == STATUS_RDY;
239 #if (CONFIG_LED == LED_REAL)
240 /* Conditionally block LED access for the ATA driver, so the LED can be
241 * (mis)used for other purposes */
242 static void ata_led(bool on)
244 ata_led_on = on;
245 if (ata_led_enabled)
246 led(ata_led_on);
248 #else
249 #define ata_led(on) led(on)
250 #endif
252 #ifndef ATA_OPTIMIZED_READING
253 STATICIRAM ICODE_ATTR void copy_read_sectors(unsigned char* buf, int wordcount)
255 unsigned short tmp = 0;
257 if ( (unsigned long)buf & 1)
258 { /* not 16-bit aligned, copy byte by byte */
259 unsigned char* bufend = buf + wordcount*2;
262 tmp = ATA_DATA;
263 #if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN)
264 *buf++ = tmp & 0xff; /* I assume big endian */
265 *buf++ = tmp >> 8; /* and don't use the SWAB16 macro */
266 #else
267 *buf++ = tmp >> 8;
268 *buf++ = tmp & 0xff;
269 #endif
270 } while (buf < bufend); /* tail loop is faster */
272 else
273 { /* 16-bit aligned, can do faster copy */
274 unsigned short* wbuf = (unsigned short*)buf;
275 unsigned short* wbufend = wbuf + wordcount;
278 #ifdef SWAP_WORDS
279 *wbuf = swap16(ATA_DATA);
280 #else
281 *wbuf = ATA_DATA;
282 #endif
283 } while (++wbuf < wbufend); /* tail loop is faster */
286 #endif /* !ATA_OPTIMIZED_READING */
288 #ifdef MAX_PHYS_SECTOR_SIZE
289 static int _read_sectors(unsigned long start,
290 int incount,
291 void* inbuf)
292 #else
293 int ata_read_sectors(IF_MV2(int drive,)
294 unsigned long start,
295 int incount,
296 void* inbuf)
297 #endif
299 int ret = 0;
300 long timeout;
301 int count;
302 void* buf;
303 long spinup_start;
305 #ifndef MAX_PHYS_SECTOR_SIZE
306 #ifdef HAVE_MULTIVOLUME
307 (void)drive; /* unused for now */
308 #endif
309 mutex_lock(&ata_mtx);
310 #endif
312 if (start + incount > total_sectors) {
313 ret = -1;
314 goto error;
317 last_disk_activity = current_tick;
318 spinup_start = current_tick;
320 ata_led(true);
322 if ( sleeping ) {
323 spinup = true;
324 if (poweroff) {
325 if (ata_power_on()) {
326 ret = -2;
327 goto error;
330 else {
331 if (perform_soft_reset()) {
332 ret = -2;
333 goto error;
338 timeout = current_tick + READ_TIMEOUT;
340 SET_REG(ATA_SELECT, ata_device);
341 if (!wait_for_rdy())
343 ret = -3;
344 goto error;
347 retry:
348 buf = inbuf;
349 count = incount;
350 while (TIME_BEFORE(current_tick, timeout)) {
351 ret = 0;
352 last_disk_activity = current_tick;
354 #ifdef HAVE_LBA48
355 if (lba48)
357 SET_REG(ATA_NSECTOR, count >> 8);
358 SET_REG(ATA_NSECTOR, count & 0xff);
359 SET_REG(ATA_SECTOR, (start >> 24) & 0xff); /* 31:24 */
360 SET_REG(ATA_SECTOR, start & 0xff); /* 7:0 */
361 SET_REG(ATA_LCYL, 0); /* 39:32 */
362 SET_REG(ATA_LCYL, (start >> 8) & 0xff); /* 15:8 */
363 SET_REG(ATA_HCYL, 0); /* 47:40 */
364 SET_REG(ATA_HCYL, (start >> 16) & 0xff); /* 23:16 */
365 SET_REG(ATA_SELECT, SELECT_LBA | ata_device);
366 SET_REG(ATA_COMMAND, CMD_READ_MULTIPLE_EXT);
368 else
369 #endif
371 SET_REG(ATA_NSECTOR, count & 0xff); /* 0 means 256 sectors */
372 SET_REG(ATA_SECTOR, start & 0xff);
373 SET_REG(ATA_LCYL, (start >> 8) & 0xff);
374 SET_REG(ATA_HCYL, (start >> 16) & 0xff);
375 SET_REG(ATA_SELECT, ((start >> 24) & 0xf) | SELECT_LBA | ata_device);
376 SET_REG(ATA_COMMAND, CMD_READ_MULTIPLE);
379 /* wait at least 400ns between writing command and reading status */
380 __asm__ volatile ("nop");
381 __asm__ volatile ("nop");
382 __asm__ volatile ("nop");
383 __asm__ volatile ("nop");
384 __asm__ volatile ("nop");
386 while (count) {
387 int sectors;
388 int wordcount;
389 int status;
391 if (!wait_for_start_of_transfer()) {
392 /* We have timed out waiting for RDY and/or DRQ, possibly
393 because the hard drive is shaking and has problems reading
394 the data. We have two options:
395 1) Wait some more
396 2) Perform a soft reset and try again.
398 We choose alternative 2.
400 perform_soft_reset();
401 ret = -5;
402 goto retry;
405 if (spinup) {
406 ata_spinup_time = current_tick - spinup_start;
407 spinup = false;
408 sleeping = false;
409 poweroff = false;
412 /* read the status register exactly once per loop */
413 status = ATA_STATUS;
415 if (count >= multisectors )
416 sectors = multisectors;
417 else
418 sectors = count;
420 wordcount = sectors * SECTOR_SIZE / 2;
422 copy_read_sectors(buf, wordcount);
425 "Device errors encountered during READ MULTIPLE commands are
426 posted at the beginning of the block or partial block transfer,
427 but the DRQ bit is still set to one and the data transfer shall
428 take place, including transfer of corrupted data, if any."
429 -- ATA specification
431 if ( status & (STATUS_BSY | STATUS_ERR | STATUS_DF) ) {
432 perform_soft_reset();
433 ret = -6;
434 goto retry;
437 buf += sectors * SECTOR_SIZE; /* Advance one chunk of sectors */
438 count -= sectors;
440 last_disk_activity = current_tick;
443 if(!ret && !wait_for_end_of_transfer()) {
444 perform_soft_reset();
445 ret = -4;
446 goto retry;
448 break;
451 error:
452 ata_led(false);
453 #ifndef MAX_PHYS_SECTOR_SIZE
454 mutex_unlock(&ata_mtx);
455 #endif
457 return ret;
460 #ifndef ATA_OPTIMIZED_WRITING
461 STATICIRAM ICODE_ATTR void copy_write_sectors(const unsigned char* buf,
462 int wordcount)
464 if ( (unsigned long)buf & 1)
465 { /* not 16-bit aligned, copy byte by byte */
466 unsigned short tmp = 0;
467 const unsigned char* bufend = buf + wordcount*2;
470 #if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN)
471 tmp = (unsigned short) *buf++;
472 tmp |= (unsigned short) *buf++ << 8;
473 SET_16BITREG(ATA_DATA, tmp);
474 #else
475 tmp = (unsigned short) *buf++ << 8;
476 tmp |= (unsigned short) *buf++;
477 SET_16BITREG(ATA_DATA, tmp);
478 #endif
479 } while (buf < bufend); /* tail loop is faster */
481 else
482 { /* 16-bit aligned, can do faster copy */
483 unsigned short* wbuf = (unsigned short*)buf;
484 unsigned short* wbufend = wbuf + wordcount;
487 #ifdef SWAP_WORDS
488 SET_16BITREG(ATA_DATA, swap16(*wbuf));
489 #else
490 SET_16BITREG(ATA_DATA, *wbuf);
491 #endif
492 } while (++wbuf < wbufend); /* tail loop is faster */
495 #endif /* !ATA_OPTIMIZED_WRITING */
497 #ifdef MAX_PHYS_SECTOR_SIZE
498 static int _write_sectors(unsigned long start,
499 int count,
500 const void* buf)
501 #else
502 int ata_write_sectors(IF_MV2(int drive,)
503 unsigned long start,
504 int count,
505 const void* buf)
506 #endif
508 int i;
509 int ret = 0;
510 long spinup_start;
512 #ifndef MAX_PHYS_SECTOR_SIZE
513 #ifdef HAVE_MULTIVOLUME
514 (void)drive; /* unused for now */
515 #endif
516 mutex_lock(&ata_mtx);
517 #endif
519 if (start + count > total_sectors)
520 panicf("Writing past end of disk");
522 last_disk_activity = current_tick;
523 spinup_start = current_tick;
525 ata_led(true);
527 if ( sleeping ) {
528 spinup = true;
529 if (poweroff) {
530 if (ata_power_on()) {
531 ret = -1;
532 goto error;
535 else {
536 if (perform_soft_reset()) {
537 ret = -1;
538 goto error;
543 SET_REG(ATA_SELECT, ata_device);
544 if (!wait_for_rdy())
546 ret = -2;
547 goto error;
550 #ifdef HAVE_LBA48
551 if (lba48)
553 SET_REG(ATA_NSECTOR, count >> 8);
554 SET_REG(ATA_NSECTOR, count & 0xff);
555 SET_REG(ATA_SECTOR, (start >> 24) & 0xff); /* 31:24 */
556 SET_REG(ATA_SECTOR, start & 0xff); /* 7:0 */
557 SET_REG(ATA_LCYL, 0); /* 39:32 */
558 SET_REG(ATA_LCYL, (start >> 8) & 0xff); /* 15:8 */
559 SET_REG(ATA_HCYL, 0); /* 47:40 */
560 SET_REG(ATA_HCYL, (start >> 16) & 0xff); /* 23:16 */
561 SET_REG(ATA_SELECT, SELECT_LBA | ata_device);
562 SET_REG(ATA_COMMAND, CMD_WRITE_SECTORS_EXT);
564 else
565 #endif
567 SET_REG(ATA_NSECTOR, count & 0xff); /* 0 means 256 sectors */
568 SET_REG(ATA_SECTOR, start & 0xff);
569 SET_REG(ATA_LCYL, (start >> 8) & 0xff);
570 SET_REG(ATA_HCYL, (start >> 16) & 0xff);
571 SET_REG(ATA_SELECT, ((start >> 24) & 0xf) | SELECT_LBA | ata_device);
572 SET_REG(ATA_COMMAND, CMD_WRITE_SECTORS);
575 for (i=0; i<count; i++) {
577 if (!wait_for_start_of_transfer()) {
578 ret = -3;
579 break;
582 if (spinup) {
583 ata_spinup_time = current_tick - spinup_start;
584 spinup = false;
585 sleeping = false;
586 poweroff = false;
589 copy_write_sectors(buf, SECTOR_SIZE/2);
591 #ifdef USE_INTERRUPT
592 /* reading the status register clears the interrupt */
593 j = ATA_STATUS;
594 #endif
595 buf += SECTOR_SIZE;
597 last_disk_activity = current_tick;
600 if(!ret && !wait_for_end_of_transfer()) {
601 DEBUGF("End on transfer failed. -- jyp");
602 ret = -4;
605 error:
606 ata_led(false);
607 #ifndef MAX_PHYS_SECTOR_SIZE
608 mutex_unlock(&ata_mtx);
609 #endif
611 return ret;
614 #ifdef MAX_PHYS_SECTOR_SIZE
615 static int cache_sector(unsigned long sector)
617 int rc;
619 sector &= ~(phys_sector_mult - 1);
620 /* round down to physical sector boundary */
622 /* check whether the sector is already cached */
623 if (sector_cache.inuse && (sector_cache.sectornum == sector))
624 return 0;
626 /* not found: read the sector */
627 sector_cache.inuse = false;
628 rc = _read_sectors(sector, phys_sector_mult, sector_cache.data);
629 if (!rc)
631 sector_cache.sectornum = sector;
632 sector_cache.inuse = true;
634 return rc;
637 static inline int flush_current_sector(void)
639 return _write_sectors(sector_cache.sectornum, phys_sector_mult,
640 sector_cache.data);
643 int ata_read_sectors(IF_MV2(int drive,)
644 unsigned long start,
645 int incount,
646 void* inbuf)
648 int rc = 0;
649 int offset;
651 #ifdef HAVE_MULTIVOLUME
652 (void)drive; /* unused for now */
653 #endif
654 mutex_lock(&ata_mtx);
656 offset = start & (phys_sector_mult - 1);
658 if (offset) /* first partial sector */
660 int partcount = MIN(incount, phys_sector_mult - offset);
662 rc = cache_sector(start);
663 if (rc)
665 rc = rc * 10 - 1;
666 goto error;
668 memcpy(inbuf, sector_cache.data + offset * SECTOR_SIZE,
669 partcount * SECTOR_SIZE);
671 start += partcount;
672 inbuf += partcount * SECTOR_SIZE;
673 incount -= partcount;
675 if (incount)
677 offset = incount & (phys_sector_mult - 1);
678 incount -= offset;
680 if (incount)
682 rc = _read_sectors(start, incount, inbuf);
683 if (rc)
685 rc = rc * 10 - 2;
686 goto error;
688 start += incount;
689 inbuf += incount * SECTOR_SIZE;
691 if (offset)
693 rc = cache_sector(start);
694 if (rc)
696 rc = rc * 10 - 3;
697 goto error;
699 memcpy(inbuf, sector_cache.data, offset * SECTOR_SIZE);
703 error:
704 mutex_unlock(&ata_mtx);
706 return rc;
709 int ata_write_sectors(IF_MV2(int drive,)
710 unsigned long start,
711 int count,
712 const void* buf)
714 int rc = 0;
715 int offset;
717 #ifdef HAVE_MULTIVOLUME
718 (void)drive; /* unused for now */
719 #endif
720 mutex_lock(&ata_mtx);
722 offset = start & (phys_sector_mult - 1);
724 if (offset) /* first partial sector */
726 int partcount = MIN(count, phys_sector_mult - offset);
728 rc = cache_sector(start);
729 if (rc)
731 rc = rc * 10 - 1;
732 goto error;
734 memcpy(sector_cache.data + offset * SECTOR_SIZE, buf,
735 partcount * SECTOR_SIZE);
736 rc = flush_current_sector();
737 if (rc)
739 rc = rc * 10 - 2;
740 goto error;
742 start += partcount;
743 buf += partcount * SECTOR_SIZE;
744 count -= partcount;
746 if (count)
748 offset = count & (phys_sector_mult - 1);
749 count -= offset;
751 if (count)
753 rc = _write_sectors(start, count, buf);
754 if (rc)
756 rc = rc * 10 - 3;
757 goto error;
759 start += count;
760 buf += count * SECTOR_SIZE;
762 if (offset)
764 rc = cache_sector(start);
765 if (rc)
767 rc = rc * 10 - 4;
768 goto error;
770 memcpy(sector_cache.data, buf, offset * SECTOR_SIZE);
771 rc = flush_current_sector();
772 if (rc)
774 rc = rc * 10 - 5;
775 goto error;
780 error:
781 mutex_unlock(&ata_mtx);
783 return rc;
785 #endif /* MAX_PHYS_SECTOR_SIZE */
787 static int check_registers(void)
789 int i;
790 if ( ATA_STATUS & STATUS_BSY )
791 return -1;
793 for (i = 0; i<64; i++) {
794 SET_REG(ATA_NSECTOR, WRITE_PATTERN1);
795 SET_REG(ATA_SECTOR, WRITE_PATTERN2);
796 SET_REG(ATA_LCYL, WRITE_PATTERN3);
797 SET_REG(ATA_HCYL, WRITE_PATTERN4);
799 if (((ATA_NSECTOR & READ_PATTERN1_MASK) == READ_PATTERN1) &&
800 ((ATA_SECTOR & READ_PATTERN2_MASK) == READ_PATTERN2) &&
801 ((ATA_LCYL & READ_PATTERN3_MASK) == READ_PATTERN3) &&
802 ((ATA_HCYL & READ_PATTERN4_MASK) == READ_PATTERN4))
803 return 0;
805 return -2;
808 static int freeze_lock(void)
810 /* does the disk support Security Mode feature set? */
811 if (identify_info[82] & 2)
813 SET_REG(ATA_SELECT, ata_device);
815 if (!wait_for_rdy())
816 return -1;
818 SET_REG(ATA_COMMAND, CMD_SECURITY_FREEZE_LOCK);
820 if (!wait_for_rdy())
821 return -2;
824 return 0;
827 void ata_spindown(int seconds)
829 sleep_timeout = seconds * HZ;
832 bool ata_disk_is_active(void)
834 return !sleeping;
837 static int ata_perform_sleep(void)
839 mutex_lock(&ata_mtx);
841 SET_REG(ATA_SELECT, ata_device);
843 if(!wait_for_rdy()) {
844 DEBUGF("ata_perform_sleep() - not RDY\n");
845 mutex_unlock(&ata_mtx);
846 return -1;
849 SET_REG(ATA_COMMAND, CMD_SLEEP);
851 if (!wait_for_rdy())
853 DEBUGF("ata_perform_sleep() - CMD failed\n");
854 mutex_unlock(&ata_mtx);
855 return -2;
858 sleeping = true;
859 mutex_unlock(&ata_mtx);
860 return 0;
863 void ata_sleep(void)
865 queue_post(&ata_queue, Q_SLEEP, 0);
868 void ata_sleepnow(void)
870 if (!spinup && !sleeping && !ata_mtx.locked && initialized)
872 call_ata_idle_notifys(false);
873 ata_perform_sleep();
877 void ata_spin(void)
879 last_user_activity = current_tick;
882 static void ata_thread(void)
884 static long last_sleep = 0;
885 struct queue_event ev;
886 static long last_seen_mtx_unlock = 0;
888 while (1) {
889 queue_wait_w_tmo(&ata_queue, &ev, HZ/2);
891 switch ( ev.id ) {
892 case SYS_TIMEOUT:
893 if (!spinup && !sleeping)
895 if (!ata_mtx.locked)
897 if (!last_seen_mtx_unlock)
898 last_seen_mtx_unlock = current_tick;
899 if (TIME_AFTER(current_tick, last_seen_mtx_unlock+(HZ*2)))
901 call_ata_idle_notifys(false);
902 last_seen_mtx_unlock = 0;
905 if ( sleep_timeout &&
906 TIME_AFTER( current_tick,
907 last_user_activity + sleep_timeout ) &&
908 TIME_AFTER( current_tick,
909 last_disk_activity + sleep_timeout ) )
911 call_ata_idle_notifys(true);
912 ata_perform_sleep();
913 last_sleep = current_tick;
917 #ifdef HAVE_ATA_POWER_OFF
918 if ( !spinup && sleeping && !poweroff &&
919 TIME_AFTER( current_tick, last_sleep + ATA_POWER_OFF_TIMEOUT ))
921 mutex_lock(&ata_mtx);
922 ide_power_enable(false);
923 mutex_unlock(&ata_mtx);
924 poweroff = true;
926 #endif
927 break;
929 #ifndef USB_NONE
930 case SYS_USB_CONNECTED:
931 if (poweroff) {
932 mutex_lock(&ata_mtx);
933 ata_led(true);
934 ata_power_on();
935 ata_led(false);
936 mutex_unlock(&ata_mtx);
939 /* Tell the USB thread that we are safe */
940 DEBUGF("ata_thread got SYS_USB_CONNECTED\n");
941 usb_acknowledge(SYS_USB_CONNECTED_ACK);
943 /* Wait until the USB cable is extracted again */
944 usb_wait_for_disconnect(&ata_queue);
945 break;
946 #endif
947 case Q_SLEEP:
948 call_ata_idle_notifys(false);
949 last_disk_activity = current_tick - sleep_timeout + (HZ/2);
950 break;
952 #ifdef ATA_DRIVER_CLOSE
953 case Q_CLOSE:
954 return;
955 #endif
960 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
961 int ata_hard_reset(void)
963 int ret;
965 mutex_lock(&ata_mtx);
967 ata_reset();
969 /* state HRR2 */
970 SET_REG(ATA_SELECT, ata_device); /* select the right device */
971 ret = wait_for_bsy();
973 /* Massage the return code so it is 0 on success and -1 on failure */
974 ret = ret?0:-1;
976 mutex_unlock(&ata_mtx);
978 return ret;
981 static int perform_soft_reset(void)
983 /* If this code is allowed to run on a Nano, the next reads from the flash will
984 * time out, so we disable it. It shouldn't be necessary anyway, since the
985 * ATA -> Flash interface automatically sleeps almost immediately after the
986 * last command.
988 int ret;
989 int retry_count;
991 SET_REG(ATA_SELECT, SELECT_LBA | ata_device );
992 SET_REG(ATA_CONTROL, CONTROL_nIEN|CONTROL_SRST );
993 sleep(1); /* >= 5us */
995 SET_REG(ATA_CONTROL, CONTROL_nIEN);
996 sleep(1); /* >2ms */
998 /* This little sucker can take up to 30 seconds */
999 retry_count = 8;
1002 ret = wait_for_rdy();
1003 } while(!ret && retry_count--);
1005 if (!ret)
1006 return -1;
1008 if (set_features())
1009 return -2;
1011 if (set_multiple_mode(multisectors))
1012 return -3;
1014 if (freeze_lock())
1015 return -4;
1017 return 0;
1020 int ata_soft_reset(void)
1022 int ret;
1024 mutex_lock(&ata_mtx);
1026 ret = perform_soft_reset();
1028 mutex_unlock(&ata_mtx);
1029 return ret;
1032 static int ata_power_on(void)
1034 int rc;
1036 ide_power_enable(true);
1037 sleep(HZ/4); /* allow voltage to build up */
1039 /* Accessing the PP IDE controller too early after powering up the disk
1040 * makes the core hang for a short time, causing an audio dropout. This
1041 * also depends on the disk; iPod Mini G2 needs at least HZ/5 to get rid
1042 * of the dropout. Since this time isn't additive (the wait_for_bsy() in
1043 * ata_hard_reset() will shortened by the same amount), it's a good idea
1044 * to do this on all HDD based targets. */
1046 if( ata_hard_reset() )
1047 return -1;
1049 rc = set_features();
1050 if (rc)
1051 return rc * 10 - 2;
1053 if (set_multiple_mode(multisectors))
1054 return -3;
1056 if (freeze_lock())
1057 return -4;
1059 return 0;
1062 static int master_slave_detect(void)
1064 /* master? */
1065 SET_REG(ATA_SELECT, 0);
1066 if ( ATA_STATUS & (STATUS_RDY|STATUS_BSY) ) {
1067 ata_device = 0;
1068 DEBUGF("Found master harddisk\n");
1070 else {
1071 /* slave? */
1072 SET_REG(ATA_SELECT, SELECT_DEVICE1);
1073 if ( ATA_STATUS & (STATUS_RDY|STATUS_BSY) ) {
1074 ata_device = SELECT_DEVICE1;
1075 DEBUGF("Found slave harddisk\n");
1077 else
1078 return -1;
1080 return 0;
1083 static int identify(void)
1085 int i;
1087 SET_REG(ATA_SELECT, ata_device);
1089 if(!wait_for_rdy()) {
1090 DEBUGF("identify() - not RDY\n");
1091 return -1;
1093 SET_REG(ATA_COMMAND, CMD_IDENTIFY);
1095 if (!wait_for_start_of_transfer())
1097 DEBUGF("identify() - CMD failed\n");
1098 return -2;
1101 for (i=0; i<SECTOR_SIZE/2; i++) {
1102 /* the IDENTIFY words are already swapped, so we need to treat
1103 this info differently that normal sector data */
1104 #if defined(ROCKBOX_BIG_ENDIAN) && !defined(SWAP_WORDS)
1105 identify_info[i] = swap16(ATA_DATA);
1106 #else
1107 identify_info[i] = ATA_DATA;
1108 #endif
1111 return 0;
1114 static int set_multiple_mode(int sectors)
1116 SET_REG(ATA_SELECT, ata_device);
1118 if(!wait_for_rdy()) {
1119 DEBUGF("set_multiple_mode() - not RDY\n");
1120 return -1;
1123 SET_REG(ATA_NSECTOR, sectors);
1124 SET_REG(ATA_COMMAND, CMD_SET_MULTIPLE_MODE);
1126 if (!wait_for_rdy())
1128 DEBUGF("set_multiple_mode() - CMD failed\n");
1129 return -2;
1132 return 0;
1135 static int set_features(void)
1137 static struct {
1138 unsigned char id_word;
1139 unsigned char id_bit;
1140 unsigned char subcommand;
1141 unsigned char parameter;
1142 } features[] = {
1143 { 83, 14, 0x03, 0 }, /* force PIO mode */
1144 { 83, 3, 0x05, 0x80 }, /* adv. power management: lowest w/o standby */
1145 { 83, 9, 0x42, 0x80 }, /* acoustic management: lowest noise */
1146 { 82, 6, 0xaa, 0 }, /* enable read look-ahead */
1148 int i;
1149 int pio_mode = 2;
1151 /* Find out the highest supported PIO mode */
1152 if(identify_info[64] & 2)
1153 pio_mode = 4;
1154 else
1155 if(identify_info[64] & 1)
1156 pio_mode = 3;
1158 /* Update the table: set highest supported pio mode that we also support */
1159 features[0].parameter = 8 + pio_mode;
1161 SET_REG(ATA_SELECT, ata_device);
1163 if (!wait_for_rdy()) {
1164 DEBUGF("set_features() - not RDY\n");
1165 return -1;
1168 for (i=0; i < (int)(sizeof(features)/sizeof(features[0])); i++) {
1169 if (identify_info[features[i].id_word] & (1 << features[i].id_bit)) {
1170 SET_REG(ATA_FEATURE, features[i].subcommand);
1171 SET_REG(ATA_NSECTOR, features[i].parameter);
1172 SET_REG(ATA_COMMAND, CMD_SET_FEATURES);
1174 if (!wait_for_rdy()) {
1175 DEBUGF("set_features() - CMD failed\n");
1176 return -10 - i;
1179 if((ATA_ALT_STATUS & STATUS_ERR) && (i != 1)) {
1180 /* some CF cards don't like advanced powermanagement
1181 even if they mark it as supported - go figure... */
1182 if(ATA_ERROR & ERROR_ABRT) {
1183 return -20 - i;
1189 #ifdef ATA_SET_DEVICE_FEATURES
1190 ata_set_pio_timings(pio_mode);
1191 #endif
1193 return 0;
1196 unsigned short* ata_get_identify(void)
1198 return identify_info;
1201 static int init_and_check(bool hard_reset)
1203 int rc;
1205 if (hard_reset)
1207 /* This should reset both master and slave, we don't yet know what's in */
1208 ata_device = 0;
1209 if (ata_hard_reset())
1210 return -1;
1213 rc = master_slave_detect();
1214 if (rc)
1215 return -10 + rc;
1217 /* symptom fix: else check_registers() below may fail */
1218 if (hard_reset && !wait_for_bsy())
1219 return -20;
1221 rc = check_registers();
1222 if (rc)
1223 return -30 + rc;
1225 return 0;
1228 int ata_init(void)
1230 int rc = 0;
1231 bool coldstart;
1233 if ( !initialized ) {
1234 mutex_init(&ata_mtx);
1235 queue_init(&ata_queue, true);
1238 mutex_lock(&ata_mtx);
1240 /* must be called before ata_device_init() */
1241 coldstart = ata_is_coldstart();
1242 ata_led(false);
1243 ata_device_init();
1244 sleeping = false;
1245 ata_enable(true);
1246 #ifdef MAX_PHYS_SECTOR_SIZE
1247 memset(&sector_cache, 0, sizeof(sector_cache));
1248 #endif
1250 if ( !initialized ) {
1251 /* First call won't have multiple thread contention - this
1252 * may return at any point without having to unlock */
1253 mutex_unlock(&ata_mtx);
1255 if (!ide_powered()) /* somebody has switched it off */
1257 ide_power_enable(true);
1258 sleep(HZ/4); /* allow voltage to build up */
1261 /* first try, hard reset at cold start only */
1262 rc = init_and_check(coldstart);
1264 if (rc)
1265 { /* failed? -> second try, always with hard reset */
1266 DEBUGF("ata: init failed, retrying...\n");
1267 rc = init_and_check(true);
1268 if (rc)
1269 return rc;
1272 rc = identify();
1274 if (rc)
1275 return -40 + rc;
1277 multisectors = identify_info[47] & 0xff;
1278 if (multisectors == 0) /* Invalid multisector info, try with 16 */
1279 multisectors = 16;
1281 DEBUGF("ata: %d sectors per ata request\n",multisectors);
1283 #ifdef MAX_PHYS_SECTOR_SIZE
1284 /* Find out the physical sector size */
1285 if((identify_info[106] & 0xe000) == 0x6000)
1286 phys_sector_mult = 1 << (identify_info[106] & 0x000f);
1287 else
1288 phys_sector_mult = 1;
1290 DEBUGF("ata: %d logical sectors per phys sector", phys_sector_mult);
1292 if (phys_sector_mult > (MAX_PHYS_SECTOR_SIZE/SECTOR_SIZE))
1293 panicf("Unsupported physical sector size: %d",
1294 phys_sector_mult * SECTOR_SIZE);
1295 #endif
1297 total_sectors = identify_info[60] | (identify_info[61] << 16);
1299 #ifdef HAVE_LBA48
1300 if (identify_info[83] & 0x0400 /* 48 bit address support */
1301 && total_sectors == 0x0FFFFFFF) /* and disk size >= 128 GiB */
1302 { /* (needs BigLBA addressing) */
1303 if (identify_info[102] || identify_info[103])
1304 panicf("Unsupported disk size: >= 2^32 sectors");
1306 total_sectors = identify_info[100] | (identify_info[101] << 16);
1307 lba48 = true; /* use BigLBA */
1309 #endif
1310 rc = freeze_lock();
1312 if (rc)
1313 return -50 + rc;
1315 rc = set_features();
1316 if (rc)
1317 return -60 + rc;
1319 mutex_lock(&ata_mtx); /* Balance unlock below */
1321 last_disk_activity = current_tick;
1322 #ifdef ATA_DRIVER_CLOSE
1323 ata_thread_p =
1324 #endif
1325 create_thread(ata_thread, ata_stack,
1326 sizeof(ata_stack), 0, ata_thread_name
1327 IF_PRIO(, PRIORITY_USER_INTERFACE)
1328 IF_COP(, CPU));
1329 initialized = true;
1332 rc = set_multiple_mode(multisectors);
1333 if (rc)
1334 rc = -70 + rc;
1336 mutex_unlock(&ata_mtx);
1337 return rc;
1340 #ifdef ATA_DRIVER_CLOSE
1341 void ata_close(void)
1343 struct thread_entry *thread = ata_thread_p;
1345 if (thread == NULL)
1346 return;
1348 ata_thread_p = NULL;
1350 queue_post(&ata_queue, Q_CLOSE, 0);
1351 thread_wait(thread);
1353 #endif /* ATA_DRIVER_CLOSE */
1355 #if (CONFIG_LED == LED_REAL)
1356 void ata_set_led_enabled(bool enabled)
1358 ata_led_enabled = enabled;
1359 if (ata_led_enabled)
1360 led(ata_led_on);
1361 else
1362 led(false);
1364 #endif