Remove solved issue
[maemo-rb.git] / firmware / drivers / ata.c
bloba16a6cc15e922f74c1c9cef5c2910014c663c362
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 <inttypes.h>
23 #include "ata.h"
24 #include "kernel.h"
25 #include "thread.h"
26 #include "led.h"
27 #include "cpu.h"
28 #include "system.h"
29 #include "debug.h"
30 #include "panic.h"
31 #include "usb.h"
32 #include "power.h"
33 #include "string.h"
34 #include "ata_idle_notify.h"
35 #include "ata-target.h"
36 #include "ata-defines.h"
37 #include "storage.h"
39 #define SECTOR_SIZE 512
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_WRITE_MULTIPLE_EXT 0x39
54 #define CMD_SET_MULTIPLE_MODE 0xC6
55 #define CMD_STANDBY_IMMEDIATE 0xE0
56 #define CMD_STANDBY 0xE2
57 #define CMD_IDENTIFY 0xEC
58 #define CMD_SLEEP 0xE6
59 #define CMD_SET_FEATURES 0xEF
60 #define CMD_SECURITY_FREEZE_LOCK 0xF5
61 #ifdef HAVE_ATA_DMA
62 #define CMD_READ_DMA 0xC8
63 #define CMD_READ_DMA_EXT 0x25
64 #define CMD_WRITE_DMA 0xCA
65 #define CMD_WRITE_DMA_EXT 0x35
66 #endif
68 /* Should all be < 0x100 (which are reserved for control messages) */
69 #define Q_SLEEP 0
70 #define Q_CLOSE 1
72 #define READWRITE_TIMEOUT 5*HZ
74 #ifdef HAVE_ATA_POWER_OFF
75 #define ATA_POWER_OFF_TIMEOUT 2*HZ
76 #endif
78 #ifdef ATA_DRIVER_CLOSE
79 static unsigned int ata_thread_id = 0;
80 #endif
82 #if defined(MAX_PHYS_SECTOR_SIZE) && MEMORYSIZE == 64
83 /* Hack - what's the deal with 5g? */
84 struct ata_lock
86 struct thread_entry *thread;
87 int count;
88 volatile unsigned char locked;
89 IF_COP( struct corelock cl; )
92 static void ata_lock_init(struct ata_lock *l)
94 corelock_init(&l->cl);
95 l->locked = 0;
96 l->count = 0;
97 l->thread = NULL;
100 static void ata_lock_lock(struct ata_lock *l)
102 struct thread_entry * const current =
103 thread_id_entry(THREAD_ID_CURRENT);
105 if (current == l->thread)
107 l->count++;
108 return;
111 corelock_lock(&l->cl);
113 IF_PRIO( current->skip_count = -1; )
115 while (l->locked != 0)
117 corelock_unlock(&l->cl);
118 switch_thread();
119 corelock_lock(&l->cl);
122 l->locked = 1;
123 l->thread = current;
124 corelock_unlock(&l->cl);
127 static void ata_lock_unlock(struct ata_lock *l)
129 if (l->count > 0)
131 l->count--;
132 return;
135 corelock_lock(&l->cl);
137 IF_PRIO( l->thread->skip_count = 0; )
139 l->thread = NULL;
140 l->locked = 0;
142 corelock_unlock(&l->cl);
145 #define mutex ata_lock
146 #define mutex_init ata_lock_init
147 #define mutex_lock ata_lock_lock
148 #define mutex_unlock ata_lock_unlock
149 #endif /* MAX_PHYS_SECTOR_SIZE */
151 #if defined(HAVE_USBSTACK) && defined(USE_ROCKBOX_USB)
152 #define ALLOW_USB_SPINDOWN
153 #endif
155 static struct mutex ata_mtx SHAREDBSS_ATTR;
156 static int ata_device; /* device 0 (master) or 1 (slave) */
158 static int spinup_time = 0;
159 #if (CONFIG_LED == LED_REAL)
160 static bool ata_led_enabled = true;
161 static bool ata_led_on = false;
162 #endif
163 static bool spinup = false;
164 static bool sleeping = true;
165 static bool poweroff = false;
166 static long sleep_timeout = 5*HZ;
167 #ifdef HAVE_LBA48
168 static bool lba48 = false; /* set for 48 bit addressing */
169 #endif
170 static long ata_stack[(DEFAULT_STACK_SIZE*3)/sizeof(long)];
171 static const char ata_thread_name[] = "ata";
172 static struct event_queue ata_queue SHAREDBSS_ATTR;
173 static bool initialized = false;
175 static long last_user_activity = -1;
176 static long last_disk_activity = -1;
178 static unsigned long total_sectors;
179 static int multisectors; /* number of supported multisectors */
180 static unsigned short identify_info[SECTOR_SIZE/2];
182 #ifdef MAX_PHYS_SECTOR_SIZE
184 struct sector_cache_entry {
185 bool inuse;
186 unsigned long sectornum; /* logical sector */
187 unsigned char data[MAX_PHYS_SECTOR_SIZE];
189 /* buffer for reading and writing large physical sectors */
190 #define NUMCACHES 2
191 static struct sector_cache_entry sector_cache;
192 static int phys_sector_mult = 1;
193 #endif
195 #ifdef HAVE_ATA_DMA
196 static int dma_mode = 0;
197 #endif
199 static int ata_power_on(void);
200 static int perform_soft_reset(void);
201 static int set_multiple_mode(int sectors);
202 static int set_features(void);
204 #ifndef ATA_TARGET_POLLING
205 STATICIRAM ICODE_ATTR int wait_for_bsy(void)
207 long timeout = current_tick + HZ*30;
211 if (!(ATA_IN8(ATA_STATUS) & STATUS_BSY))
212 return 1;
213 last_disk_activity = current_tick;
214 yield();
215 } while (TIME_BEFORE(current_tick, timeout));
217 return 0; /* timeout */
220 STATICIRAM ICODE_ATTR int wait_for_rdy(void)
222 long timeout;
224 if (!wait_for_bsy())
225 return 0;
227 timeout = current_tick + HZ*10;
231 if (ATA_IN8(ATA_ALT_STATUS) & STATUS_RDY)
232 return 1;
233 last_disk_activity = current_tick;
234 yield();
235 } while (TIME_BEFORE(current_tick, timeout));
237 return 0; /* timeout */
239 #else
240 extern int ata_wait_for_bsy(void);
241 extern int ata_wait_for_rdy(void);
242 #define wait_for_bsy ata_wait_for_bsy
243 #define wait_for_rdy ata_wait_for_rdy
244 #endif
246 STATICIRAM ICODE_ATTR int wait_for_start_of_transfer(void)
248 if (!wait_for_bsy())
249 return 0;
251 return (ATA_IN8(ATA_ALT_STATUS) & (STATUS_BSY|STATUS_DRQ)) == STATUS_DRQ;
254 STATICIRAM ICODE_ATTR int wait_for_end_of_transfer(void)
256 if (!wait_for_bsy())
257 return 0;
258 return (ATA_IN8(ATA_ALT_STATUS) &
259 (STATUS_BSY|STATUS_RDY|STATUS_DF|STATUS_DRQ|STATUS_ERR))
260 == STATUS_RDY;
263 #if (CONFIG_LED == LED_REAL)
264 /* Conditionally block LED access for the ATA driver, so the LED can be
265 * (mis)used for other purposes */
266 static void ata_led(bool on)
268 ata_led_on = on;
269 if (ata_led_enabled)
270 led(ata_led_on);
272 #else
273 #define ata_led(on) led(on)
274 #endif
276 #ifndef ATA_OPTIMIZED_READING
277 STATICIRAM ICODE_ATTR void copy_read_sectors(unsigned char* buf, int wordcount)
279 unsigned short tmp = 0;
281 if ( (unsigned long)buf & 1)
282 { /* not 16-bit aligned, copy byte by byte */
283 unsigned char* bufend = buf + wordcount*2;
286 tmp = ATA_IN16(ATA_DATA);
287 #if defined(ROCKBOX_LITTLE_ENDIAN)
288 *buf++ = tmp & 0xff; /* I assume big endian */
289 *buf++ = tmp >> 8; /* and don't use the SWAB16 macro */
290 #else
291 *buf++ = tmp >> 8;
292 *buf++ = tmp & 0xff;
293 #endif
294 } while (buf < bufend); /* tail loop is faster */
296 else
297 { /* 16-bit aligned, can do faster copy */
298 unsigned short* wbuf = (unsigned short*)buf;
299 unsigned short* wbufend = wbuf + wordcount;
302 *wbuf = ATA_IN16(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,
310 int wordcount)
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(ROCKBOX_LITTLE_ENDIAN)
319 tmp = (unsigned short) *buf++;
320 tmp |= (unsigned short) *buf++ << 8;
321 #else
322 tmp = (unsigned short) *buf++ << 8;
323 tmp |= (unsigned short) *buf++;
324 #endif
325 ATA_OUT16(ATA_DATA, tmp);
326 } while (buf < bufend); /* tail loop is faster */
328 else
329 { /* 16-bit aligned, can do faster copy */
330 unsigned short* wbuf = (unsigned short*)buf;
331 unsigned short* wbufend = wbuf + wordcount;
334 ATA_OUT16(ATA_DATA, *wbuf);
335 } while (++wbuf < wbufend); /* tail loop is faster */
338 #endif /* !ATA_OPTIMIZED_WRITING */
340 static int ata_transfer_sectors(unsigned long start,
341 int incount,
342 void* inbuf,
343 int write)
345 int ret = 0;
346 long timeout;
347 int count;
348 void* buf;
349 long spinup_start;
350 #ifdef HAVE_ATA_DMA
351 bool usedma = false;
352 #endif
354 #ifndef MAX_PHYS_SECTOR_SIZE
355 mutex_lock(&ata_mtx);
356 #endif
358 if (start + incount > total_sectors) {
359 ret = -1;
360 goto error;
363 last_disk_activity = current_tick;
364 spinup_start = current_tick;
366 ata_led(true);
368 if ( sleeping ) {
369 sleeping = false; /* set this now since it'll be on */
370 spinup = true;
371 if (poweroff) {
372 if (ata_power_on()) {
373 ret = -2;
374 goto error;
377 else {
378 if (perform_soft_reset()) {
379 ret = -2;
380 goto error;
385 timeout = current_tick + READWRITE_TIMEOUT;
387 ATA_OUT8(ATA_SELECT, ata_device);
388 if (!wait_for_rdy())
390 ret = -3;
391 goto error;
394 retry:
395 buf = inbuf;
396 count = incount;
397 while (TIME_BEFORE(current_tick, timeout)) {
398 ret = 0;
399 last_disk_activity = current_tick;
401 #ifdef HAVE_ATA_DMA
402 /* If DMA is supported and parameters are ok for DMA, use it */
403 if (dma_mode && ata_dma_setup(inbuf, incount * SECTOR_SIZE, write))
404 usedma = true;
405 #endif
407 #ifdef HAVE_LBA48
408 if (lba48)
410 ATA_OUT8(ATA_NSECTOR, count >> 8);
411 ATA_OUT8(ATA_NSECTOR, count & 0xff);
412 ATA_OUT8(ATA_SECTOR, (start >> 24) & 0xff); /* 31:24 */
413 ATA_OUT8(ATA_SECTOR, start & 0xff); /* 7:0 */
414 ATA_OUT8(ATA_LCYL, 0); /* 39:32 */
415 ATA_OUT8(ATA_LCYL, (start >> 8) & 0xff); /* 15:8 */
416 ATA_OUT8(ATA_HCYL, 0); /* 47:40 */
417 ATA_OUT8(ATA_HCYL, (start >> 16) & 0xff); /* 23:16 */
418 ATA_OUT8(ATA_SELECT, SELECT_LBA | ata_device);
419 #ifdef HAVE_ATA_DMA
420 if (write)
421 ATA_OUT8(ATA_COMMAND, usedma ? CMD_WRITE_DMA_EXT : CMD_WRITE_MULTIPLE_EXT);
422 else
423 ATA_OUT8(ATA_COMMAND, usedma ? CMD_READ_DMA_EXT : CMD_READ_MULTIPLE_EXT);
424 #else
425 ATA_OUT8(ATA_COMMAND, write ? CMD_WRITE_MULTIPLE_EXT : CMD_READ_MULTIPLE_EXT);
426 #endif
428 else
429 #endif
431 ATA_OUT8(ATA_NSECTOR, count & 0xff); /* 0 means 256 sectors */
432 ATA_OUT8(ATA_SECTOR, start & 0xff);
433 ATA_OUT8(ATA_LCYL, (start >> 8) & 0xff);
434 ATA_OUT8(ATA_HCYL, (start >> 16) & 0xff);
435 ATA_OUT8(ATA_SELECT, ((start >> 24) & 0xf) | SELECT_LBA | ata_device);
436 #ifdef HAVE_ATA_DMA
437 if (write)
438 ATA_OUT8(ATA_COMMAND, usedma ? CMD_WRITE_DMA : CMD_WRITE_MULTIPLE);
439 else
440 ATA_OUT8(ATA_COMMAND, usedma ? CMD_READ_DMA : CMD_READ_MULTIPLE);
441 #else
442 ATA_OUT8(ATA_COMMAND, write ? CMD_WRITE_MULTIPLE : CMD_READ_MULTIPLE);
443 #endif
446 /* wait at least 400ns between writing command and reading status */
447 __asm__ volatile ("nop");
448 __asm__ volatile ("nop");
449 __asm__ volatile ("nop");
450 __asm__ volatile ("nop");
451 __asm__ volatile ("nop");
453 #ifdef HAVE_ATA_DMA
454 if (usedma) {
455 if (!ata_dma_finish())
456 ret = -7;
458 if (ret != 0) {
459 perform_soft_reset();
460 goto retry;
463 if (spinup) {
464 spinup_time = current_tick - spinup_start;
465 spinup = false;
466 poweroff = false;
469 else
470 #endif /* HAVE_ATA_DMA */
472 while (count) {
473 int sectors;
474 int wordcount;
475 int status;
476 int error;
478 if (!wait_for_start_of_transfer()) {
479 /* We have timed out waiting for RDY and/or DRQ, possibly
480 because the hard drive is shaking and has problems
481 reading the data. We have two options:
482 1) Wait some more
483 2) Perform a soft reset and try again.
485 We choose alternative 2.
487 perform_soft_reset();
488 ret = -5;
489 goto retry;
492 if (spinup) {
493 spinup_time = current_tick - spinup_start;
494 spinup = false;
495 poweroff = false;
498 /* read the status register exactly once per loop */
499 status = ATA_IN8(ATA_STATUS);
500 error = ATA_IN8(ATA_ERROR);
502 if (count >= multisectors)
503 sectors = multisectors;
504 else
505 sectors = count;
507 wordcount = sectors * SECTOR_SIZE / 2;
509 if (write)
510 copy_write_sectors(buf, wordcount);
511 else
512 copy_read_sectors(buf, wordcount);
515 "Device errors encountered during READ MULTIPLE commands
516 are posted at the beginning of the block or partial block
517 transfer, but the DRQ bit is still set to one and the data
518 transfer shall take place, including transfer of corrupted
519 data, if any."
520 -- ATA specification
522 if ( status & (STATUS_BSY | STATUS_ERR | STATUS_DF) ) {
523 perform_soft_reset();
524 ret = -6;
525 /* no point retrying IDNF, sector no. was invalid */
526 if (error & ERROR_IDNF)
527 break;
528 goto retry;
531 buf += sectors * SECTOR_SIZE; /* Advance one chunk of sectors */
532 count -= sectors;
534 last_disk_activity = current_tick;
538 if(!ret && !wait_for_end_of_transfer()) {
539 int error;
541 error = ATA_IN8(ATA_ERROR);
542 perform_soft_reset();
543 ret = -4;
544 /* no point retrying IDNF, sector no. was invalid */
545 if (error & ERROR_IDNF)
546 break;
547 goto retry;
549 break;
552 error:
553 ata_led(false);
554 #ifndef MAX_PHYS_SECTOR_SIZE
555 mutex_unlock(&ata_mtx);
556 #endif
558 return ret;
561 #ifndef MAX_PHYS_SECTOR_SIZE
562 int ata_read_sectors(IF_MD2(int drive,)
563 unsigned long start,
564 int incount,
565 void* inbuf)
567 #ifdef HAVE_MULTIDRIVE
568 (void)drive; /* unused for now */
569 #endif
571 return ata_transfer_sectors(start, incount, inbuf, false);
573 #endif
575 #ifndef MAX_PHYS_SECTOR_SIZE
576 int ata_write_sectors(IF_MD2(int drive,)
577 unsigned long start,
578 int count,
579 const void* buf)
581 #ifdef HAVE_MULTIDRIVE
582 (void)drive; /* unused for now */
583 #endif
585 return ata_transfer_sectors(start, count, (void*)buf, true);
587 #endif
589 #ifdef MAX_PHYS_SECTOR_SIZE
590 static int cache_sector(unsigned long sector)
592 int rc;
594 sector &= ~(phys_sector_mult - 1);
595 /* round down to physical sector boundary */
597 /* check whether the sector is already cached */
598 if (sector_cache.inuse && (sector_cache.sectornum == sector))
599 return 0;
601 /* not found: read the sector */
602 sector_cache.inuse = false;
603 rc = ata_transfer_sectors(sector, phys_sector_mult, sector_cache.data, false);
604 if (!rc)
606 sector_cache.sectornum = sector;
607 sector_cache.inuse = true;
609 return rc;
612 static inline int flush_current_sector(void)
614 return ata_transfer_sectors(sector_cache.sectornum, phys_sector_mult,
615 sector_cache.data, true);
618 int ata_read_sectors(IF_MD2(int drive,)
619 unsigned long start,
620 int incount,
621 void* inbuf)
623 int rc = 0;
624 int offset;
626 #ifdef HAVE_MULTIDRIVE
627 (void)drive; /* unused for now */
628 #endif
629 mutex_lock(&ata_mtx);
631 offset = start & (phys_sector_mult - 1);
633 if (offset) /* first partial sector */
635 int partcount = MIN(incount, phys_sector_mult - offset);
637 rc = cache_sector(start);
638 if (rc)
640 rc = rc * 10 - 1;
641 goto error;
643 memcpy(inbuf, sector_cache.data + offset * SECTOR_SIZE,
644 partcount * SECTOR_SIZE);
646 start += partcount;
647 inbuf += partcount * SECTOR_SIZE;
648 incount -= partcount;
650 if (incount)
652 offset = incount & (phys_sector_mult - 1);
653 incount -= offset;
655 if (incount)
657 rc = ata_transfer_sectors(start, incount, inbuf, false);
658 if (rc)
660 rc = rc * 10 - 2;
661 goto error;
663 start += incount;
664 inbuf += incount * SECTOR_SIZE;
666 if (offset)
668 rc = cache_sector(start);
669 if (rc)
671 rc = rc * 10 - 3;
672 goto error;
674 memcpy(inbuf, sector_cache.data, offset * SECTOR_SIZE);
678 error:
679 mutex_unlock(&ata_mtx);
681 return rc;
684 int ata_write_sectors(IF_MD2(int drive,)
685 unsigned long start,
686 int count,
687 const void* buf)
689 int rc = 0;
690 int offset;
692 #ifdef HAVE_MULTIDRIVE
693 (void)drive; /* unused for now */
694 #endif
695 mutex_lock(&ata_mtx);
697 offset = start & (phys_sector_mult - 1);
699 if (offset) /* first partial sector */
701 int partcount = MIN(count, phys_sector_mult - offset);
703 rc = cache_sector(start);
704 if (rc)
706 rc = rc * 10 - 1;
707 goto error;
709 memcpy(sector_cache.data + offset * SECTOR_SIZE, buf,
710 partcount * SECTOR_SIZE);
711 rc = flush_current_sector();
712 if (rc)
714 rc = rc * 10 - 2;
715 goto error;
717 start += partcount;
718 buf += partcount * SECTOR_SIZE;
719 count -= partcount;
721 if (count)
723 offset = count & (phys_sector_mult - 1);
724 count -= offset;
726 if (count)
728 rc = ata_transfer_sectors(start, count, (void*)buf, true);
729 if (rc)
731 rc = rc * 10 - 3;
732 goto error;
734 start += count;
735 buf += count * SECTOR_SIZE;
737 if (offset)
739 rc = cache_sector(start);
740 if (rc)
742 rc = rc * 10 - 4;
743 goto error;
745 memcpy(sector_cache.data, buf, offset * SECTOR_SIZE);
746 rc = flush_current_sector();
747 if (rc)
749 rc = rc * 10 - 5;
750 goto error;
755 error:
756 mutex_unlock(&ata_mtx);
758 return rc;
760 #endif /* MAX_PHYS_SECTOR_SIZE */
762 static int check_registers(void)
764 int i;
765 wait_for_bsy();
766 if (ATA_IN8(ATA_STATUS) & STATUS_BSY)
767 return -1;
769 for (i = 0; i<64; i++) {
770 ATA_OUT8(ATA_NSECTOR, TEST_PATTERN1);
771 ATA_OUT8(ATA_SECTOR, TEST_PATTERN2);
772 ATA_OUT8(ATA_LCYL, TEST_PATTERN3);
773 ATA_OUT8(ATA_HCYL, TEST_PATTERN4);
775 if (((ATA_IN8(ATA_NSECTOR) & 0xff) == TEST_PATTERN1) &&
776 ((ATA_IN8(ATA_SECTOR) & 0xff) == TEST_PATTERN2) &&
777 ((ATA_IN8(ATA_LCYL) & 0xff) == TEST_PATTERN3) &&
778 ((ATA_IN8(ATA_HCYL) & 0xff) == TEST_PATTERN4))
779 return 0;
781 sleep(1);
783 return -2;
786 static int freeze_lock(void)
788 /* does the disk support Security Mode feature set? */
789 if (identify_info[82] & 2)
791 ATA_OUT8(ATA_SELECT, ata_device);
793 if (!wait_for_rdy())
794 return -1;
796 ATA_OUT8(ATA_COMMAND, CMD_SECURITY_FREEZE_LOCK);
798 if (!wait_for_rdy())
799 return -2;
802 return 0;
805 void ata_spindown(int seconds)
807 sleep_timeout = seconds * HZ;
810 bool ata_disk_is_active(void)
812 return !sleeping;
815 static int ata_perform_sleep(void)
817 /* guard against calls made with checks of these variables outside
818 the mutex that may not be on the ata thread; status may have changed. */
819 if (spinup || sleeping) {
820 return 0;
823 ATA_OUT8(ATA_SELECT, ata_device);
825 if(!wait_for_rdy()) {
826 DEBUGF("ata_perform_sleep() - not RDY\n");
827 return -1;
830 ATA_OUT8(ATA_COMMAND, CMD_SLEEP);
832 if (!wait_for_rdy())
834 DEBUGF("ata_perform_sleep() - CMD failed\n");
835 return -2;
838 sleeping = true;
839 return 0;
842 void ata_sleep(void)
844 queue_post(&ata_queue, Q_SLEEP, 0);
847 void ata_sleepnow(void)
849 if (!spinup && !sleeping && initialized)
851 call_storage_idle_notifys(false);
852 mutex_lock(&ata_mtx);
853 ata_perform_sleep();
854 mutex_unlock(&ata_mtx);
858 void ata_spin(void)
860 last_user_activity = current_tick;
863 static void ata_thread(void)
865 static long last_sleep = 0;
866 struct queue_event ev;
867 #ifdef ALLOW_USB_SPINDOWN
868 static bool usb_mode = false;
869 #endif
871 while (1) {
872 queue_wait_w_tmo(&ata_queue, &ev, HZ/2);
874 switch ( ev.id ) {
875 case SYS_TIMEOUT:
876 if (!spinup && !sleeping)
878 if (TIME_AFTER( current_tick,
879 last_disk_activity + (HZ*2) ) )
881 #ifdef ALLOW_USB_SPINDOWN
882 if(!usb_mode)
883 #endif
885 call_storage_idle_notifys(false);
889 if ( sleep_timeout &&
890 TIME_AFTER( current_tick,
891 last_user_activity + sleep_timeout ) &&
892 TIME_AFTER( current_tick,
893 last_disk_activity + sleep_timeout ) )
895 #ifdef ALLOW_USB_SPINDOWN
896 if(!usb_mode)
897 #endif
899 call_storage_idle_notifys(true);
901 mutex_lock(&ata_mtx);
902 ata_perform_sleep();
903 last_sleep = current_tick;
904 mutex_unlock(&ata_mtx);
908 #ifdef HAVE_ATA_POWER_OFF
909 if ( !spinup && sleeping && !poweroff &&
910 TIME_AFTER( current_tick, last_sleep + ATA_POWER_OFF_TIMEOUT ))
912 mutex_lock(&ata_mtx);
913 ide_power_enable(false);
914 poweroff = true;
915 mutex_unlock(&ata_mtx);
917 #endif
918 break;
920 #ifndef USB_NONE
921 case SYS_USB_CONNECTED:
922 /* Tell the USB thread that we are safe */
923 DEBUGF("ata_thread got SYS_USB_CONNECTED\n");
924 #ifdef ALLOW_USB_SPINDOWN
925 usb_mode = true;
926 usb_acknowledge(SYS_USB_CONNECTED_ACK);
927 /* There is no need to force ATA power on */
928 #else
929 mutex_lock(&ata_mtx);
930 if (sleeping) {
931 ata_led(true);
932 sleeping = false; /* set this now since it'll be on */
934 if (poweroff) {
935 ata_power_on();
936 poweroff = false;
938 else {
939 perform_soft_reset();
942 ata_led(false);
944 mutex_unlock(&ata_mtx);
946 /* Wait until the USB cable is extracted again */
947 usb_acknowledge(SYS_USB_CONNECTED_ACK);
948 usb_wait_for_disconnect(&ata_queue);
949 #endif
950 break;
952 #ifdef ALLOW_USB_SPINDOWN
953 case SYS_USB_DISCONNECTED:
954 /* Tell the USB thread that we are ready again */
955 DEBUGF("ata_thread got SYS_USB_DISCONNECTED\n");
956 usb_mode = false;
957 break;
958 #endif
959 #endif /* USB_NONE */
961 case Q_SLEEP:
962 #ifdef ALLOW_USB_SPINDOWN
963 if(!usb_mode)
964 #endif
966 call_storage_idle_notifys(false);
968 last_disk_activity = current_tick - sleep_timeout + (HZ/2);
969 break;
971 #ifdef ATA_DRIVER_CLOSE
972 case Q_CLOSE:
973 return;
974 #endif
979 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
980 static int ata_hard_reset(void)
982 int ret;
984 mutex_lock(&ata_mtx);
986 ata_reset();
988 /* state HRR2 */
989 ATA_OUT8(ATA_SELECT, ata_device); /* select the right device */
990 ret = wait_for_bsy();
992 /* Massage the return code so it is 0 on success and -1 on failure */
993 ret = ret?0:-1;
995 mutex_unlock(&ata_mtx);
997 return ret;
1000 static int perform_soft_reset(void)
1002 /* If this code is allowed to run on a Nano, the next reads from the flash will
1003 * time out, so we disable it. It shouldn't be necessary anyway, since the
1004 * ATA -> Flash interface automatically sleeps almost immediately after the
1005 * last command.
1007 int ret;
1008 int retry_count;
1010 ATA_OUT8(ATA_SELECT, SELECT_LBA | ata_device );
1011 ATA_OUT8(ATA_CONTROL, CONTROL_nIEN|CONTROL_SRST );
1012 sleep(1); /* >= 5us */
1014 #ifdef HAVE_ATA_DMA
1015 /* DMA requires INTRQ be enabled */
1016 ATA_OUT8(ATA_CONTROL, 0);
1017 #else
1018 ATA_OUT8(ATA_CONTROL, CONTROL_nIEN);
1019 #endif
1020 sleep(1); /* >2ms */
1022 /* This little sucker can take up to 30 seconds */
1023 retry_count = 8;
1026 ret = wait_for_rdy();
1027 } while(!ret && retry_count--);
1029 if (!ret)
1030 return -1;
1032 if (set_features())
1033 return -2;
1035 if (set_multiple_mode(multisectors))
1036 return -3;
1038 if (freeze_lock())
1039 return -4;
1041 return 0;
1044 int ata_soft_reset(void)
1046 int ret;
1048 mutex_lock(&ata_mtx);
1050 ret = perform_soft_reset();
1052 mutex_unlock(&ata_mtx);
1053 return ret;
1056 static int ata_power_on(void)
1058 int rc;
1060 ide_power_enable(true);
1061 sleep(HZ/4); /* allow voltage to build up */
1063 /* Accessing the PP IDE controller too early after powering up the disk
1064 * makes the core hang for a short time, causing an audio dropout. This
1065 * also depends on the disk; iPod Mini G2 needs at least HZ/5 to get rid
1066 * of the dropout. Since this time isn't additive (the wait_for_bsy() in
1067 * ata_hard_reset() will shortened by the same amount), it's a good idea
1068 * to do this on all HDD based targets. */
1070 if( ata_hard_reset() )
1071 return -1;
1073 rc = set_features();
1074 if (rc)
1075 return rc * 10 - 2;
1077 if (set_multiple_mode(multisectors))
1078 return -3;
1080 if (freeze_lock())
1081 return -4;
1083 return 0;
1086 static int master_slave_detect(void)
1088 /* master? */
1089 ATA_OUT8(ATA_SELECT, 0);
1090 if (ATA_IN8(ATA_STATUS) & (STATUS_RDY|STATUS_BSY)) {
1091 ata_device = 0;
1092 DEBUGF("Found master harddisk\n");
1094 else {
1095 /* slave? */
1096 ATA_OUT8(ATA_SELECT, SELECT_DEVICE1);
1097 if (ATA_IN8(ATA_STATUS) & (STATUS_RDY|STATUS_BSY)) {
1098 ata_device = SELECT_DEVICE1;
1099 DEBUGF("Found slave harddisk\n");
1101 else
1102 return -1;
1104 return 0;
1107 static int identify(void)
1109 int i;
1111 ATA_OUT8(ATA_SELECT, ata_device);
1113 if(!wait_for_rdy()) {
1114 DEBUGF("identify() - not RDY\n");
1115 return -1;
1117 ATA_OUT8(ATA_COMMAND, CMD_IDENTIFY);
1119 if (!wait_for_start_of_transfer())
1121 DEBUGF("identify() - CMD failed\n");
1122 return -2;
1125 for (i=0; i<SECTOR_SIZE/2; i++) {
1126 /* the IDENTIFY words are already swapped, so we need to treat
1127 this info differently that normal sector data */
1128 identify_info[i] = ATA_SWAP_IDENTIFY(ATA_IN16(ATA_DATA));
1131 return 0;
1134 static int set_multiple_mode(int sectors)
1136 ATA_OUT8(ATA_SELECT, ata_device);
1138 if(!wait_for_rdy()) {
1139 DEBUGF("set_multiple_mode() - not RDY\n");
1140 return -1;
1143 ATA_OUT8(ATA_NSECTOR, sectors);
1144 ATA_OUT8(ATA_COMMAND, CMD_SET_MULTIPLE_MODE);
1146 if (!wait_for_rdy())
1148 DEBUGF("set_multiple_mode() - CMD failed\n");
1149 return -2;
1152 return 0;
1155 #ifdef HAVE_ATA_DMA
1156 static int get_best_mode(unsigned short identword, int max, int modetype)
1158 unsigned short testbit = BIT_N(max);
1160 while (1) {
1161 if (identword & testbit)
1162 return max | modetype;
1163 testbit >>= 1;
1164 if (!testbit)
1165 return 0;
1166 max--;
1169 #endif
1171 static int set_features(void)
1173 static struct {
1174 unsigned char id_word;
1175 unsigned char id_bit;
1176 unsigned char subcommand;
1177 unsigned char parameter;
1178 } features[] = {
1179 { 83, 14, 0x03, 0 }, /* force PIO mode */
1180 { 83, 3, 0x05, 0x80 }, /* adv. power management: lowest w/o standby */
1181 { 83, 9, 0x42, 0x80 }, /* acoustic management: lowest noise */
1182 { 82, 6, 0xaa, 0 }, /* enable read look-ahead */
1183 #ifdef HAVE_ATA_DMA
1184 { 0, 0, 0x03, 0 }, /* DMA mode */
1185 #endif
1187 int i;
1188 int pio_mode = 2;
1190 /* Find out the highest supported PIO mode */
1191 if(identify_info[64] & 2)
1192 pio_mode = 4;
1193 else
1194 if(identify_info[64] & 1)
1195 pio_mode = 3;
1197 /* Update the table: set highest supported pio mode that we also support */
1198 features[0].parameter = 8 + pio_mode;
1200 #ifdef HAVE_ATA_DMA
1201 if (identify_info[53] & (1<<2))
1202 /* Ultra DMA mode info present, find a mode */
1203 dma_mode = get_best_mode(identify_info[88], ATA_MAX_UDMA, 0x40);
1205 if (!dma_mode) {
1206 /* No UDMA mode found, try to find a multi-word DMA mode */
1207 dma_mode = get_best_mode(identify_info[63], ATA_MAX_MWDMA, 0x20);
1208 features[4].id_word = 63;
1210 else
1211 features[4].id_word = 88;
1213 features[4].id_bit = dma_mode & 7;
1214 features[4].parameter = dma_mode;
1215 #endif /* HAVE_ATA_DMA */
1217 ATA_OUT8(ATA_SELECT, ata_device);
1219 if (!wait_for_rdy()) {
1220 DEBUGF("set_features() - not RDY\n");
1221 return -1;
1224 for (i=0; i < (int)(sizeof(features)/sizeof(features[0])); i++) {
1225 if (identify_info[features[i].id_word] & BIT_N(features[i].id_bit)) {
1226 ATA_OUT8(ATA_FEATURE, features[i].subcommand);
1227 ATA_OUT8(ATA_NSECTOR, features[i].parameter);
1228 ATA_OUT8(ATA_COMMAND, CMD_SET_FEATURES);
1230 if (!wait_for_rdy()) {
1231 DEBUGF("set_features() - CMD failed\n");
1232 return -10 - i;
1235 if((ATA_IN8(ATA_ALT_STATUS) & STATUS_ERR) && (i != 1)) {
1236 /* some CF cards don't like advanced powermanagement
1237 even if they mark it as supported - go figure... */
1238 if(ATA_IN8(ATA_ERROR) & ERROR_ABRT) {
1239 return -20 - i;
1245 #ifdef ATA_SET_DEVICE_FEATURES
1246 ata_set_pio_timings(pio_mode);
1247 #endif
1249 #ifdef HAVE_ATA_DMA
1250 ata_dma_set_mode(dma_mode);
1251 #endif
1253 return 0;
1256 unsigned short* ata_get_identify(void)
1258 return identify_info;
1261 static int init_and_check(bool hard_reset)
1263 int rc;
1265 if (hard_reset)
1267 /* This should reset both master and slave, we don't yet know what's in */
1268 ata_device = 0;
1269 if (ata_hard_reset())
1270 return -1;
1273 rc = master_slave_detect();
1274 if (rc)
1275 return -10 + rc;
1277 /* symptom fix: else check_registers() below may fail */
1278 if (hard_reset && !wait_for_bsy())
1279 return -20;
1281 rc = check_registers();
1282 if (rc)
1283 return -30 + rc;
1285 return 0;
1288 int ata_init(void)
1290 int rc = 0;
1291 bool coldstart;
1293 if ( !initialized ) {
1294 mutex_init(&ata_mtx);
1295 queue_init(&ata_queue, true);
1298 mutex_lock(&ata_mtx);
1300 /* must be called before ata_device_init() */
1301 coldstart = ata_is_coldstart();
1302 ata_led(false);
1303 ata_device_init();
1304 sleeping = false;
1305 ata_enable(true);
1306 #ifdef MAX_PHYS_SECTOR_SIZE
1307 memset(&sector_cache, 0, sizeof(sector_cache));
1308 #endif
1310 if ( !initialized ) {
1311 /* First call won't have multiple thread contention - this
1312 * may return at any point without having to unlock */
1313 mutex_unlock(&ata_mtx);
1315 if (!ide_powered()) /* somebody has switched it off */
1317 ide_power_enable(true);
1318 sleep(HZ/4); /* allow voltage to build up */
1321 #ifdef HAVE_ATA_DMA
1322 /* DMA requires INTRQ be enabled */
1323 ATA_OUT8(ATA_CONTROL, 0);
1324 #endif
1326 /* first try, hard reset at cold start only */
1327 rc = init_and_check(coldstart);
1329 if (rc)
1330 { /* failed? -> second try, always with hard reset */
1331 DEBUGF("ata: init failed, retrying...\n");
1332 rc = init_and_check(true);
1333 if (rc)
1334 return rc;
1337 rc = identify();
1339 if (rc)
1340 return -40 + rc;
1342 multisectors = identify_info[47] & 0xff;
1343 if (multisectors == 0) /* Invalid multisector info, try with 16 */
1344 multisectors = 16;
1346 DEBUGF("ata: %d sectors per ata request\n",multisectors);
1348 total_sectors = identify_info[60] | (identify_info[61] << 16);
1350 #ifdef HAVE_LBA48
1351 if (identify_info[83] & 0x0400 /* 48 bit address support */
1352 && total_sectors == 0x0FFFFFFF) /* and disk size >= 128 GiB */
1353 { /* (needs BigLBA addressing) */
1354 if (identify_info[102] || identify_info[103])
1355 panicf("Unsupported disk size: >= 2^32 sectors");
1357 total_sectors = identify_info[100] | (identify_info[101] << 16);
1358 lba48 = true; /* use BigLBA */
1360 #endif
1361 rc = freeze_lock();
1363 if (rc)
1364 return -50 + rc;
1366 rc = set_features();
1367 if (rc)
1368 return -60 + rc;
1370 #ifdef MAX_PHYS_SECTOR_SIZE
1371 /* Find out the physical sector size */
1372 if((identify_info[106] & 0xe000) == 0x6000)
1373 phys_sector_mult = BIT_N(identify_info[106] & 0x000f);
1374 else
1375 phys_sector_mult = 1;
1377 DEBUGF("ata: %d logical sectors per phys sector", phys_sector_mult);
1379 if (phys_sector_mult > 1)
1381 /* Check if drive really needs emulation - if we can access
1382 * sector 1 then assume the drive will handle it better than
1383 * us, and ignore the large physical sectors.
1385 char throwaway[SECTOR_SIZE];
1386 rc = ata_transfer_sectors(1, 1, &throwaway, false);
1387 if (rc == 0)
1388 phys_sector_mult = 1;
1391 if (phys_sector_mult > (MAX_PHYS_SECTOR_SIZE/SECTOR_SIZE))
1392 panicf("Unsupported physical sector size: %d",
1393 phys_sector_mult * SECTOR_SIZE);
1394 #endif
1396 mutex_lock(&ata_mtx); /* Balance unlock below */
1398 last_disk_activity = current_tick;
1399 #ifdef ATA_DRIVER_CLOSE
1400 ata_thread_id =
1401 #endif
1402 create_thread(ata_thread, ata_stack,
1403 sizeof(ata_stack), 0, ata_thread_name
1404 IF_PRIO(, PRIORITY_USER_INTERFACE)
1405 IF_COP(, CPU));
1406 initialized = true;
1409 rc = set_multiple_mode(multisectors);
1410 if (rc)
1411 rc = -70 + rc;
1413 mutex_unlock(&ata_mtx);
1414 return rc;
1417 #ifdef ATA_DRIVER_CLOSE
1418 void ata_close(void)
1420 unsigned int thread_id = ata_thread_id;
1422 if (thread_id == 0)
1423 return;
1425 ata_thread_id = 0;
1427 queue_post(&ata_queue, Q_CLOSE, 0);
1428 thread_wait(thread_id);
1430 #endif /* ATA_DRIVER_CLOSE */
1432 #if (CONFIG_LED == LED_REAL)
1433 void ata_set_led_enabled(bool enabled)
1435 ata_led_enabled = enabled;
1436 if (ata_led_enabled)
1437 led(ata_led_on);
1438 else
1439 led(false);
1441 #endif
1443 long ata_last_disk_activity(void)
1445 return last_disk_activity;
1448 int ata_spinup_time(void)
1450 return spinup_time;
1453 #ifdef STORAGE_GET_INFO
1454 void ata_get_info(IF_MD2(int drive,)struct storage_info *info)
1456 unsigned short *src,*dest;
1457 static char vendor[8];
1458 static char product[16];
1459 static char revision[4];
1460 #ifdef HAVE_MULTIDRIVE
1461 (void)drive; /* unused for now */
1462 #endif
1463 int i;
1464 info->sector_size = SECTOR_SIZE;
1465 info->num_sectors = total_sectors;
1467 src = (unsigned short*)&identify_info[27];
1468 dest = (unsigned short*)vendor;
1469 for (i=0;i<4;i++)
1470 dest[i] = htobe16(src[i]);
1471 info->vendor=vendor;
1473 src = (unsigned short*)&identify_info[31];
1474 dest = (unsigned short*)product;
1475 for (i=0;i<8;i++)
1476 dest[i] = htobe16(src[i]);
1477 info->product=product;
1479 src = (unsigned short*)&identify_info[23];
1480 dest = (unsigned short*)revision;
1481 for (i=0;i<2;i++)
1482 dest[i] = htobe16(src[i]);
1483 info->revision=revision;
1485 #endif
1487 #ifdef HAVE_ATA_DMA
1488 /* Returns last DMA mode as set by set_features() */
1489 int ata_get_dma_mode(void)
1491 return dma_mode;
1494 /* Needed to allow updating while waiting for DMA to complete */
1495 void ata_keep_active(void)
1497 last_disk_activity = current_tick;
1499 #endif
1501 #ifdef CONFIG_STORAGE_MULTI
1502 int ata_num_drives(int first_drive)
1504 /* We don't care which logical drive number(s) we have been assigned */
1505 (void)first_drive;
1507 return 1;
1509 #endif