ypr0: Correct .scrobbler.log path for YPR0
[maemo-rb.git] / firmware / drivers / ata.c
blobec04ac1426cb7b7800013f8cb34d610b04e6f9cc
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-driver.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 = thread_self_entry();
104 if (current == l->thread)
106 l->count++;
107 return;
110 corelock_lock(&l->cl);
112 IF_PRIO( current->skip_count = -1; )
114 while (l->locked != 0)
116 corelock_unlock(&l->cl);
117 switch_thread();
118 corelock_lock(&l->cl);
121 l->locked = 1;
122 l->thread = current;
123 corelock_unlock(&l->cl);
126 static void ata_lock_unlock(struct ata_lock *l)
128 if (l->count > 0)
130 l->count--;
131 return;
134 corelock_lock(&l->cl);
136 IF_PRIO( l->thread->skip_count = 0; )
138 l->thread = NULL;
139 l->locked = 0;
141 corelock_unlock(&l->cl);
144 #define mutex ata_lock
145 #define mutex_init ata_lock_init
146 #define mutex_lock ata_lock_lock
147 #define mutex_unlock ata_lock_unlock
148 #endif /* MAX_PHYS_SECTOR_SIZE */
150 #if defined(HAVE_USBSTACK) && defined(USE_ROCKBOX_USB)
151 #define ALLOW_USB_SPINDOWN
152 #endif
154 static struct mutex ata_mtx SHAREDBSS_ATTR;
155 static int ata_device; /* device 0 (master) or 1 (slave) */
157 static int spinup_time = 0;
158 #if (CONFIG_LED == LED_REAL)
159 static bool ata_led_enabled = true;
160 static bool ata_led_on = false;
161 #endif
162 static bool spinup = false;
163 static bool sleeping = true;
164 #ifdef HAVE_ATA_POWER_OFF
165 static bool poweroff = false;
166 #endif
167 static long sleep_timeout = 5*HZ;
168 #ifdef HAVE_LBA48
169 static bool lba48 = false; /* set for 48 bit addressing */
170 #endif
171 static long ata_stack[(DEFAULT_STACK_SIZE*3)/sizeof(long)];
172 static const char ata_thread_name[] = "ata";
173 static struct event_queue ata_queue SHAREDBSS_ATTR;
174 static bool initialized = false;
176 static long last_user_activity = -1;
177 static long last_disk_activity = -1;
179 static unsigned long total_sectors;
180 static int multisectors; /* number of supported multisectors */
181 static unsigned short identify_info[SECTOR_SIZE/2];
183 #ifdef MAX_PHYS_SECTOR_SIZE
185 struct sector_cache_entry {
186 bool inuse;
187 unsigned long sectornum; /* logical sector */
188 unsigned char data[MAX_PHYS_SECTOR_SIZE];
190 /* buffer for reading and writing large physical sectors */
191 #define NUMCACHES 2
192 static struct sector_cache_entry sector_cache;
193 static int phys_sector_mult = 1;
194 #endif
196 #ifdef HAVE_ATA_DMA
197 static int dma_mode = 0;
198 #endif
200 #ifdef HAVE_ATA_POWER_OFF
201 static int ata_power_on(void);
202 #endif
203 static int perform_soft_reset(void);
204 static int set_multiple_mode(int sectors);
205 static int set_features(void);
207 #ifndef ATA_TARGET_POLLING
208 static ICODE_ATTR int wait_for_bsy(void)
210 long timeout = current_tick + HZ*30;
214 if (!(ATA_IN8(ATA_STATUS) & STATUS_BSY))
215 return 1;
216 last_disk_activity = current_tick;
217 yield();
218 } while (TIME_BEFORE(current_tick, timeout));
220 return 0; /* timeout */
223 static ICODE_ATTR int wait_for_rdy(void)
225 long timeout;
227 if (!wait_for_bsy())
228 return 0;
230 timeout = current_tick + HZ*10;
234 if (ATA_IN8(ATA_ALT_STATUS) & STATUS_RDY)
235 return 1;
236 last_disk_activity = current_tick;
237 yield();
238 } while (TIME_BEFORE(current_tick, timeout));
240 return 0; /* timeout */
242 #else
243 #define wait_for_bsy ata_wait_for_bsy
244 #define wait_for_rdy ata_wait_for_rdy
245 #endif
247 static ICODE_ATTR int wait_for_start_of_transfer(void)
249 if (!wait_for_bsy())
250 return 0;
252 return (ATA_IN8(ATA_ALT_STATUS) & (STATUS_BSY|STATUS_DRQ)) == STATUS_DRQ;
255 static ICODE_ATTR int wait_for_end_of_transfer(void)
257 if (!wait_for_bsy())
258 return 0;
259 return (ATA_IN8(ATA_ALT_STATUS) &
260 (STATUS_BSY|STATUS_RDY|STATUS_DF|STATUS_DRQ|STATUS_ERR))
261 == STATUS_RDY;
264 #if (CONFIG_LED == LED_REAL)
265 /* Conditionally block LED access for the ATA driver, so the LED can be
266 * (mis)used for other purposes */
267 static void ata_led(bool on)
269 ata_led_on = on;
270 if (ata_led_enabled)
271 led(ata_led_on);
273 #else
274 #define ata_led(on) led(on)
275 #endif
277 #ifndef ATA_OPTIMIZED_READING
278 static ICODE_ATTR void copy_read_sectors(unsigned char* buf, int wordcount)
280 unsigned short tmp = 0;
282 if ( (unsigned long)buf & 1)
283 { /* not 16-bit aligned, copy byte by byte */
284 unsigned char* bufend = buf + wordcount*2;
287 tmp = ATA_IN16(ATA_DATA);
288 #if defined(ROCKBOX_LITTLE_ENDIAN)
289 *buf++ = tmp & 0xff; /* I assume big endian */
290 *buf++ = tmp >> 8; /* and don't use the SWAB16 macro */
291 #else
292 *buf++ = tmp >> 8;
293 *buf++ = tmp & 0xff;
294 #endif
295 } while (buf < bufend); /* tail loop is faster */
297 else
298 { /* 16-bit aligned, can do faster copy */
299 unsigned short* wbuf = (unsigned short*)buf;
300 unsigned short* wbufend = wbuf + wordcount;
303 *wbuf = ATA_IN16(ATA_DATA);
304 } while (++wbuf < wbufend); /* tail loop is faster */
307 #endif /* !ATA_OPTIMIZED_READING */
309 #ifndef ATA_OPTIMIZED_WRITING
310 static ICODE_ATTR void copy_write_sectors(const unsigned char* buf,
311 int wordcount)
313 if ( (unsigned long)buf & 1)
314 { /* not 16-bit aligned, copy byte by byte */
315 unsigned short tmp = 0;
316 const unsigned char* bufend = buf + wordcount*2;
319 #if defined(ROCKBOX_LITTLE_ENDIAN)
320 tmp = (unsigned short) *buf++;
321 tmp |= (unsigned short) *buf++ << 8;
322 #else
323 tmp = (unsigned short) *buf++ << 8;
324 tmp |= (unsigned short) *buf++;
325 #endif
326 ATA_OUT16(ATA_DATA, tmp);
327 } while (buf < bufend); /* tail loop is faster */
329 else
330 { /* 16-bit aligned, can do faster copy */
331 unsigned short* wbuf = (unsigned short*)buf;
332 unsigned short* wbufend = wbuf + wordcount;
335 ATA_OUT16(ATA_DATA, *wbuf);
336 } while (++wbuf < wbufend); /* tail loop is faster */
339 #endif /* !ATA_OPTIMIZED_WRITING */
341 static int ata_transfer_sectors(unsigned long start,
342 int incount,
343 void* inbuf,
344 int write)
346 int ret = 0;
347 long timeout;
348 int count;
349 void* buf;
350 long spinup_start;
351 #ifdef HAVE_ATA_DMA
352 bool usedma = false;
353 #endif
355 #ifndef MAX_PHYS_SECTOR_SIZE
356 mutex_lock(&ata_mtx);
357 #endif
359 if (start + incount > total_sectors) {
360 ret = -1;
361 goto error;
364 last_disk_activity = current_tick;
365 spinup_start = current_tick;
367 ata_led(true);
369 if ( sleeping ) {
370 sleeping = false; /* set this now since it'll be on */
371 spinup = true;
372 #ifdef HAVE_ATA_POWER_OFF
373 if (poweroff) {
374 if (ata_power_on()) {
375 ret = -2;
376 goto error;
379 else
380 #endif
382 if (perform_soft_reset()) {
383 ret = -2;
384 goto error;
389 timeout = current_tick + READWRITE_TIMEOUT;
391 ATA_OUT8(ATA_SELECT, ata_device);
392 if (!wait_for_rdy())
394 ret = -3;
395 goto error;
398 retry:
399 buf = inbuf;
400 count = incount;
401 while (TIME_BEFORE(current_tick, timeout)) {
402 ret = 0;
403 last_disk_activity = current_tick;
405 #ifdef HAVE_ATA_DMA
406 /* If DMA is supported and parameters are ok for DMA, use it */
407 if (dma_mode && ata_dma_setup(inbuf, incount * SECTOR_SIZE, write))
408 usedma = true;
409 #endif
411 #ifdef HAVE_LBA48
412 if (lba48)
414 ATA_OUT8(ATA_NSECTOR, count >> 8);
415 ATA_OUT8(ATA_NSECTOR, count & 0xff);
416 ATA_OUT8(ATA_SECTOR, (start >> 24) & 0xff); /* 31:24 */
417 ATA_OUT8(ATA_SECTOR, start & 0xff); /* 7:0 */
418 ATA_OUT8(ATA_LCYL, 0); /* 39:32 */
419 ATA_OUT8(ATA_LCYL, (start >> 8) & 0xff); /* 15:8 */
420 ATA_OUT8(ATA_HCYL, 0); /* 47:40 */
421 ATA_OUT8(ATA_HCYL, (start >> 16) & 0xff); /* 23:16 */
422 ATA_OUT8(ATA_SELECT, SELECT_LBA | ata_device);
423 #ifdef HAVE_ATA_DMA
424 if (write)
425 ATA_OUT8(ATA_COMMAND, usedma ? CMD_WRITE_DMA_EXT : CMD_WRITE_MULTIPLE_EXT);
426 else
427 ATA_OUT8(ATA_COMMAND, usedma ? CMD_READ_DMA_EXT : CMD_READ_MULTIPLE_EXT);
428 #else
429 ATA_OUT8(ATA_COMMAND, write ? CMD_WRITE_MULTIPLE_EXT : CMD_READ_MULTIPLE_EXT);
430 #endif
432 else
433 #endif
435 ATA_OUT8(ATA_NSECTOR, count & 0xff); /* 0 means 256 sectors */
436 ATA_OUT8(ATA_SECTOR, start & 0xff);
437 ATA_OUT8(ATA_LCYL, (start >> 8) & 0xff);
438 ATA_OUT8(ATA_HCYL, (start >> 16) & 0xff);
439 ATA_OUT8(ATA_SELECT, ((start >> 24) & 0xf) | SELECT_LBA | ata_device);
440 #ifdef HAVE_ATA_DMA
441 if (write)
442 ATA_OUT8(ATA_COMMAND, usedma ? CMD_WRITE_DMA : CMD_WRITE_MULTIPLE);
443 else
444 ATA_OUT8(ATA_COMMAND, usedma ? CMD_READ_DMA : CMD_READ_MULTIPLE);
445 #else
446 ATA_OUT8(ATA_COMMAND, write ? CMD_WRITE_MULTIPLE : CMD_READ_MULTIPLE);
447 #endif
450 /* wait at least 400ns between writing command and reading status */
451 __asm__ volatile ("nop");
452 __asm__ volatile ("nop");
453 __asm__ volatile ("nop");
454 __asm__ volatile ("nop");
455 __asm__ volatile ("nop");
457 #ifdef HAVE_ATA_DMA
458 if (usedma) {
459 if (!ata_dma_finish())
460 ret = -7;
462 if (ret != 0) {
463 perform_soft_reset();
464 goto retry;
467 if (spinup) {
468 spinup_time = current_tick - spinup_start;
469 spinup = false;
470 #ifdef HAVE_ATA_POWER_OFF
471 poweroff = false;
472 #endif
475 else
476 #endif /* HAVE_ATA_DMA */
478 while (count) {
479 int sectors;
480 int wordcount;
481 int status;
482 int error;
484 if (!wait_for_start_of_transfer()) {
485 /* We have timed out waiting for RDY and/or DRQ, possibly
486 because the hard drive is shaking and has problems
487 reading the data. We have two options:
488 1) Wait some more
489 2) Perform a soft reset and try again.
491 We choose alternative 2.
493 perform_soft_reset();
494 ret = -5;
495 goto retry;
498 if (spinup) {
499 spinup_time = current_tick - spinup_start;
500 spinup = false;
501 #ifdef HAVE_ATA_POWER_OFF
502 poweroff = false;
503 #endif
506 /* read the status register exactly once per loop */
507 status = ATA_IN8(ATA_STATUS);
508 error = ATA_IN8(ATA_ERROR);
510 if (count >= multisectors)
511 sectors = multisectors;
512 else
513 sectors = count;
515 wordcount = sectors * SECTOR_SIZE / 2;
517 if (write)
518 copy_write_sectors(buf, wordcount);
519 else
520 copy_read_sectors(buf, wordcount);
523 "Device errors encountered during READ MULTIPLE commands
524 are posted at the beginning of the block or partial block
525 transfer, but the DRQ bit is still set to one and the data
526 transfer shall take place, including transfer of corrupted
527 data, if any."
528 -- ATA specification
530 if ( status & (STATUS_BSY | STATUS_ERR | STATUS_DF) ) {
531 perform_soft_reset();
532 ret = -6;
533 /* no point retrying IDNF, sector no. was invalid */
534 if (error & ERROR_IDNF)
535 break;
536 goto retry;
539 buf += sectors * SECTOR_SIZE; /* Advance one chunk of sectors */
540 count -= sectors;
542 last_disk_activity = current_tick;
546 if(!ret && !wait_for_end_of_transfer()) {
547 int error;
549 error = ATA_IN8(ATA_ERROR);
550 perform_soft_reset();
551 ret = -4;
552 /* no point retrying IDNF, sector no. was invalid */
553 if (error & ERROR_IDNF)
554 break;
555 goto retry;
557 break;
560 error:
561 ata_led(false);
562 #ifndef MAX_PHYS_SECTOR_SIZE
563 mutex_unlock(&ata_mtx);
564 #endif
566 return ret;
569 #ifndef MAX_PHYS_SECTOR_SIZE
570 int ata_read_sectors(IF_MD2(int drive,)
571 unsigned long start,
572 int incount,
573 void* inbuf)
575 #ifdef HAVE_MULTIDRIVE
576 (void)drive; /* unused for now */
577 #endif
579 return ata_transfer_sectors(start, incount, inbuf, false);
581 #endif
583 #ifndef MAX_PHYS_SECTOR_SIZE
584 int ata_write_sectors(IF_MD2(int drive,)
585 unsigned long start,
586 int count,
587 const void* buf)
589 #ifdef HAVE_MULTIDRIVE
590 (void)drive; /* unused for now */
591 #endif
593 return ata_transfer_sectors(start, count, (void*)buf, true);
595 #endif
597 #ifdef MAX_PHYS_SECTOR_SIZE
598 static int cache_sector(unsigned long sector)
600 int rc;
602 sector &= ~(phys_sector_mult - 1);
603 /* round down to physical sector boundary */
605 /* check whether the sector is already cached */
606 if (sector_cache.inuse && (sector_cache.sectornum == sector))
607 return 0;
609 /* not found: read the sector */
610 sector_cache.inuse = false;
611 rc = ata_transfer_sectors(sector, phys_sector_mult, sector_cache.data, false);
612 if (!rc)
614 sector_cache.sectornum = sector;
615 sector_cache.inuse = true;
617 return rc;
620 static inline int flush_current_sector(void)
622 return ata_transfer_sectors(sector_cache.sectornum, phys_sector_mult,
623 sector_cache.data, true);
626 int ata_read_sectors(IF_MD2(int drive,)
627 unsigned long start,
628 int incount,
629 void* inbuf)
631 int rc = 0;
632 int offset;
634 #ifdef HAVE_MULTIDRIVE
635 (void)drive; /* unused for now */
636 #endif
637 mutex_lock(&ata_mtx);
639 offset = start & (phys_sector_mult - 1);
641 if (offset) /* first partial sector */
643 int partcount = MIN(incount, phys_sector_mult - offset);
645 rc = cache_sector(start);
646 if (rc)
648 rc = rc * 10 - 1;
649 goto error;
651 memcpy(inbuf, sector_cache.data + offset * SECTOR_SIZE,
652 partcount * SECTOR_SIZE);
654 start += partcount;
655 inbuf += partcount * SECTOR_SIZE;
656 incount -= partcount;
658 if (incount)
660 offset = incount & (phys_sector_mult - 1);
661 incount -= offset;
663 if (incount)
665 rc = ata_transfer_sectors(start, incount, inbuf, false);
666 if (rc)
668 rc = rc * 10 - 2;
669 goto error;
671 start += incount;
672 inbuf += incount * SECTOR_SIZE;
674 if (offset)
676 rc = cache_sector(start);
677 if (rc)
679 rc = rc * 10 - 3;
680 goto error;
682 memcpy(inbuf, sector_cache.data, offset * SECTOR_SIZE);
686 error:
687 mutex_unlock(&ata_mtx);
689 return rc;
692 int ata_write_sectors(IF_MD2(int drive,)
693 unsigned long start,
694 int count,
695 const void* buf)
697 int rc = 0;
698 int offset;
700 #ifdef HAVE_MULTIDRIVE
701 (void)drive; /* unused for now */
702 #endif
703 mutex_lock(&ata_mtx);
705 offset = start & (phys_sector_mult - 1);
707 if (offset) /* first partial sector */
709 int partcount = MIN(count, phys_sector_mult - offset);
711 rc = cache_sector(start);
712 if (rc)
714 rc = rc * 10 - 1;
715 goto error;
717 memcpy(sector_cache.data + offset * SECTOR_SIZE, buf,
718 partcount * SECTOR_SIZE);
719 rc = flush_current_sector();
720 if (rc)
722 rc = rc * 10 - 2;
723 goto error;
725 start += partcount;
726 buf += partcount * SECTOR_SIZE;
727 count -= partcount;
729 if (count)
731 offset = count & (phys_sector_mult - 1);
732 count -= offset;
734 if (count)
736 rc = ata_transfer_sectors(start, count, (void*)buf, true);
737 if (rc)
739 rc = rc * 10 - 3;
740 goto error;
742 start += count;
743 buf += count * SECTOR_SIZE;
745 if (offset)
747 rc = cache_sector(start);
748 if (rc)
750 rc = rc * 10 - 4;
751 goto error;
753 memcpy(sector_cache.data, buf, offset * SECTOR_SIZE);
754 rc = flush_current_sector();
755 if (rc)
757 rc = rc * 10 - 5;
758 goto error;
763 error:
764 mutex_unlock(&ata_mtx);
766 return rc;
768 #endif /* MAX_PHYS_SECTOR_SIZE */
770 static int STORAGE_INIT_ATTR check_registers(void)
772 int i;
773 wait_for_bsy();
774 if (ATA_IN8(ATA_STATUS) & STATUS_BSY)
775 return -1;
777 for (i = 0; i<64; i++) {
778 ATA_OUT8(ATA_NSECTOR, TEST_PATTERN1);
779 ATA_OUT8(ATA_SECTOR, TEST_PATTERN2);
780 ATA_OUT8(ATA_LCYL, TEST_PATTERN3);
781 ATA_OUT8(ATA_HCYL, TEST_PATTERN4);
783 if (((ATA_IN8(ATA_NSECTOR) & 0xff) == TEST_PATTERN1) &&
784 ((ATA_IN8(ATA_SECTOR) & 0xff) == TEST_PATTERN2) &&
785 ((ATA_IN8(ATA_LCYL) & 0xff) == TEST_PATTERN3) &&
786 ((ATA_IN8(ATA_HCYL) & 0xff) == TEST_PATTERN4))
787 return 0;
789 sleep(1);
791 return -2;
794 static int freeze_lock(void)
796 /* does the disk support Security Mode feature set? */
797 if (identify_info[82] & 2)
799 ATA_OUT8(ATA_SELECT, ata_device);
801 if (!wait_for_rdy())
802 return -1;
804 ATA_OUT8(ATA_COMMAND, CMD_SECURITY_FREEZE_LOCK);
806 if (!wait_for_rdy())
807 return -2;
810 return 0;
813 void ata_spindown(int seconds)
815 sleep_timeout = seconds * HZ;
818 bool ata_disk_is_active(void)
820 return !sleeping;
823 static int ata_perform_sleep(void)
825 /* guard against calls made with checks of these variables outside
826 the mutex that may not be on the ata thread; status may have changed. */
827 if (spinup || sleeping) {
828 return 0;
831 ATA_OUT8(ATA_SELECT, ata_device);
833 if(!wait_for_rdy()) {
834 DEBUGF("ata_perform_sleep() - not RDY\n");
835 return -1;
838 ATA_OUT8(ATA_COMMAND, CMD_SLEEP);
840 if (!wait_for_rdy())
842 DEBUGF("ata_perform_sleep() - CMD failed\n");
843 return -2;
846 sleeping = true;
847 return 0;
850 void ata_sleep(void)
852 queue_post(&ata_queue, Q_SLEEP, 0);
855 void ata_sleepnow(void)
857 if (!spinup && !sleeping && initialized)
859 call_storage_idle_notifys(false);
860 mutex_lock(&ata_mtx);
861 ata_perform_sleep();
862 mutex_unlock(&ata_mtx);
866 void ata_spin(void)
868 last_user_activity = current_tick;
871 static void ata_thread(void)
873 #ifdef HAVE_ATA_POWER_OFF
874 static long last_sleep = 0;
875 #endif
876 struct queue_event ev;
877 #ifdef ALLOW_USB_SPINDOWN
878 static bool usb_mode = false;
879 #endif
881 while (1) {
882 queue_wait_w_tmo(&ata_queue, &ev, HZ/2);
884 switch ( ev.id ) {
885 case SYS_TIMEOUT:
886 if (!spinup && !sleeping)
888 if (TIME_AFTER( current_tick,
889 last_disk_activity + (HZ*2) ) )
891 #ifdef ALLOW_USB_SPINDOWN
892 if(!usb_mode)
893 #endif
895 call_storage_idle_notifys(false);
899 if ( sleep_timeout &&
900 TIME_AFTER( current_tick,
901 last_user_activity + sleep_timeout ) &&
902 TIME_AFTER( current_tick,
903 last_disk_activity + sleep_timeout ) )
905 #ifdef ALLOW_USB_SPINDOWN
906 if(!usb_mode)
907 #endif
909 call_storage_idle_notifys(true);
911 mutex_lock(&ata_mtx);
912 ata_perform_sleep();
913 #ifdef HAVE_ATA_POWER_OFF
914 last_sleep = current_tick;
915 #endif
916 mutex_unlock(&ata_mtx);
920 #ifdef HAVE_ATA_POWER_OFF
921 if ( !spinup && sleeping && !poweroff &&
922 TIME_AFTER( current_tick, last_sleep + ATA_POWER_OFF_TIMEOUT ))
924 mutex_lock(&ata_mtx);
925 ide_power_enable(false);
926 poweroff = true;
927 mutex_unlock(&ata_mtx);
929 #endif
930 break;
932 #ifndef USB_NONE
933 case SYS_USB_CONNECTED:
934 /* Tell the USB thread that we are safe */
935 DEBUGF("ata_thread got SYS_USB_CONNECTED\n");
936 #ifdef ALLOW_USB_SPINDOWN
937 usb_mode = true;
938 usb_acknowledge(SYS_USB_CONNECTED_ACK);
939 /* There is no need to force ATA power on */
940 #else
941 mutex_lock(&ata_mtx);
942 if (sleeping) {
943 ata_led(true);
944 sleeping = false; /* set this now since it'll be on */
946 #ifdef HAVE_ATA_POWER_OFF
947 if (poweroff) {
948 ata_power_on();
949 poweroff = false;
951 else
952 #endif
954 perform_soft_reset();
957 ata_led(false);
959 mutex_unlock(&ata_mtx);
961 /* Wait until the USB cable is extracted again */
962 usb_acknowledge(SYS_USB_CONNECTED_ACK);
963 usb_wait_for_disconnect(&ata_queue);
964 #endif
965 break;
967 #ifdef ALLOW_USB_SPINDOWN
968 case SYS_USB_DISCONNECTED:
969 /* Tell the USB thread that we are ready again */
970 DEBUGF("ata_thread got SYS_USB_DISCONNECTED\n");
971 usb_mode = false;
972 break;
973 #endif
974 #endif /* USB_NONE */
976 case Q_SLEEP:
977 #ifdef ALLOW_USB_SPINDOWN
978 if(!usb_mode)
979 #endif
981 call_storage_idle_notifys(false);
983 last_disk_activity = current_tick - sleep_timeout + (HZ/2);
984 break;
986 #ifdef ATA_DRIVER_CLOSE
987 case Q_CLOSE:
988 return;
989 #endif
994 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
995 #ifdef HAVE_ATA_POWER_OFF
996 static int ata_hard_reset(void)
997 #else
998 static int STORAGE_INIT_ATTR ata_hard_reset(void)
999 #endif
1001 int ret;
1003 mutex_lock(&ata_mtx);
1005 ata_reset();
1007 /* state HRR2 */
1008 ATA_OUT8(ATA_SELECT, ata_device); /* select the right device */
1009 ret = wait_for_bsy();
1011 /* Massage the return code so it is 0 on success and -1 on failure */
1012 ret = ret?0:-1;
1014 mutex_unlock(&ata_mtx);
1016 return ret;
1019 // not putting this into STORAGE_INIT_ATTR, as ATA spec recommends to
1020 // re-read identify_info after soft reset. So we'll do that.
1021 static int identify(void)
1023 int i;
1025 ATA_OUT8(ATA_SELECT, ata_device);
1027 if(!wait_for_rdy()) {
1028 DEBUGF("identify() - not RDY\n");
1029 return -1;
1031 ATA_OUT8(ATA_COMMAND, CMD_IDENTIFY);
1033 if (!wait_for_start_of_transfer())
1035 DEBUGF("identify() - CMD failed\n");
1036 return -2;
1039 for (i=0; i<SECTOR_SIZE/2; i++) {
1040 /* the IDENTIFY words are already swapped, so we need to treat
1041 this info differently that normal sector data */
1042 identify_info[i] = ATA_SWAP_IDENTIFY(ATA_IN16(ATA_DATA));
1045 return 0;
1048 static int perform_soft_reset(void)
1050 /* If this code is allowed to run on a Nano, the next reads from the flash will
1051 * time out, so we disable it. It shouldn't be necessary anyway, since the
1052 * ATA -> Flash interface automatically sleeps almost immediately after the
1053 * last command.
1055 int ret;
1056 int retry_count;
1058 ATA_OUT8(ATA_SELECT, SELECT_LBA | ata_device );
1059 ATA_OUT8(ATA_CONTROL, CONTROL_nIEN|CONTROL_SRST );
1060 sleep(1); /* >= 5us */
1062 #ifdef HAVE_ATA_DMA
1063 /* DMA requires INTRQ be enabled */
1064 ATA_OUT8(ATA_CONTROL, 0);
1065 #else
1066 ATA_OUT8(ATA_CONTROL, CONTROL_nIEN);
1067 #endif
1068 sleep(1); /* >2ms */
1070 /* This little sucker can take up to 30 seconds */
1071 retry_count = 8;
1074 ret = wait_for_rdy();
1075 } while(!ret && retry_count--);
1077 if (!ret)
1078 return -1;
1080 if (identify())
1081 return -5;
1083 if (set_features())
1084 return -2;
1086 if (set_multiple_mode(multisectors))
1087 return -3;
1089 if (freeze_lock())
1090 return -4;
1092 return 0;
1095 int ata_soft_reset(void)
1097 int ret;
1099 mutex_lock(&ata_mtx);
1101 ret = perform_soft_reset();
1103 mutex_unlock(&ata_mtx);
1104 return ret;
1107 #ifdef HAVE_ATA_POWER_OFF
1108 static int ata_power_on(void)
1110 int rc;
1112 ide_power_enable(true);
1113 sleep(HZ/4); /* allow voltage to build up */
1115 /* Accessing the PP IDE controller too early after powering up the disk
1116 * makes the core hang for a short time, causing an audio dropout. This
1117 * also depends on the disk; iPod Mini G2 needs at least HZ/5 to get rid
1118 * of the dropout. Since this time isn't additive (the wait_for_bsy() in
1119 * ata_hard_reset() will shortened by the same amount), it's a good idea
1120 * to do this on all HDD based targets. */
1122 if( ata_hard_reset() )
1123 return -1;
1125 if (identify())
1126 return -5;
1128 rc = set_features();
1129 if (rc)
1130 return rc * 10 - 2;
1132 if (set_multiple_mode(multisectors))
1133 return -3;
1135 if (freeze_lock())
1136 return -4;
1138 return 0;
1140 #endif
1142 static int STORAGE_INIT_ATTR master_slave_detect(void)
1144 /* master? */
1145 ATA_OUT8(ATA_SELECT, 0);
1146 if (ATA_IN8(ATA_STATUS) & (STATUS_RDY|STATUS_BSY)) {
1147 ata_device = 0;
1148 DEBUGF("Found master harddisk\n");
1150 else {
1151 /* slave? */
1152 ATA_OUT8(ATA_SELECT, SELECT_DEVICE1);
1153 if (ATA_IN8(ATA_STATUS) & (STATUS_RDY|STATUS_BSY)) {
1154 ata_device = SELECT_DEVICE1;
1155 DEBUGF("Found slave harddisk\n");
1157 else
1158 return -1;
1160 return 0;
1163 static int set_multiple_mode(int sectors)
1165 ATA_OUT8(ATA_SELECT, ata_device);
1167 if(!wait_for_rdy()) {
1168 DEBUGF("set_multiple_mode() - not RDY\n");
1169 return -1;
1172 ATA_OUT8(ATA_NSECTOR, sectors);
1173 ATA_OUT8(ATA_COMMAND, CMD_SET_MULTIPLE_MODE);
1175 if (!wait_for_rdy())
1177 DEBUGF("set_multiple_mode() - CMD failed\n");
1178 return -2;
1181 return 0;
1184 #ifdef HAVE_ATA_DMA
1185 static int get_best_mode(unsigned short identword, int max, int modetype)
1187 unsigned short testbit = BIT_N(max);
1189 while (1) {
1190 if (identword & testbit)
1191 return max | modetype;
1192 testbit >>= 1;
1193 if (!testbit)
1194 return 0;
1195 max--;
1198 #endif
1200 static int set_features(void)
1202 static struct {
1203 unsigned char id_word;
1204 unsigned char id_bit;
1205 unsigned char subcommand;
1206 unsigned char parameter;
1207 } features[] = {
1208 { 83, 14, 0x03, 0 }, /* force PIO mode */
1209 { 83, 3, 0x05, 0x80 }, /* adv. power management: lowest w/o standby */
1210 { 83, 9, 0x42, 0x80 }, /* acoustic management: lowest noise */
1211 { 82, 6, 0xaa, 0 }, /* enable read look-ahead */
1212 #ifdef HAVE_ATA_DMA
1213 { 0, 0, 0x03, 0 }, /* DMA mode */
1214 #endif
1216 int i;
1217 int pio_mode = 2;
1219 /* Find out the highest supported PIO mode */
1220 if(identify_info[64] & 2)
1221 pio_mode = 4;
1222 else
1223 if(identify_info[64] & 1)
1224 pio_mode = 3;
1226 /* Update the table: set highest supported pio mode that we also support */
1227 features[0].parameter = 8 + pio_mode;
1229 #ifdef HAVE_ATA_DMA
1230 if (identify_info[53] & (1<<2))
1231 /* Ultra DMA mode info present, find a mode */
1232 dma_mode = get_best_mode(identify_info[88], ATA_MAX_UDMA, 0x40);
1234 if (!dma_mode) {
1235 /* No UDMA mode found, try to find a multi-word DMA mode */
1236 dma_mode = get_best_mode(identify_info[63], ATA_MAX_MWDMA, 0x20);
1237 features[4].id_word = 63;
1239 else
1240 features[4].id_word = 88;
1242 features[4].id_bit = dma_mode & 7;
1243 features[4].parameter = dma_mode;
1244 #endif /* HAVE_ATA_DMA */
1246 ATA_OUT8(ATA_SELECT, ata_device);
1248 if (!wait_for_rdy()) {
1249 DEBUGF("set_features() - not RDY\n");
1250 return -1;
1253 for (i=0; i < (int)(sizeof(features)/sizeof(features[0])); i++) {
1254 if (identify_info[features[i].id_word] & BIT_N(features[i].id_bit)) {
1255 ATA_OUT8(ATA_FEATURE, features[i].subcommand);
1256 ATA_OUT8(ATA_NSECTOR, features[i].parameter);
1257 ATA_OUT8(ATA_COMMAND, CMD_SET_FEATURES);
1259 if (!wait_for_rdy()) {
1260 DEBUGF("set_features() - CMD failed\n");
1261 return -10 - i;
1264 if((ATA_IN8(ATA_ALT_STATUS) & STATUS_ERR) && (i != 1)) {
1265 /* some CF cards don't like advanced powermanagement
1266 even if they mark it as supported - go figure... */
1267 if(ATA_IN8(ATA_ERROR) & ERROR_ABRT) {
1268 return -20 - i;
1274 #ifdef ATA_SET_PIO_TIMING
1275 ata_set_pio_timings(pio_mode);
1276 #endif
1278 #ifdef HAVE_ATA_DMA
1279 ata_dma_set_mode(dma_mode);
1280 #endif
1282 return 0;
1285 unsigned short* ata_get_identify(void)
1287 return identify_info;
1290 static int STORAGE_INIT_ATTR init_and_check(bool hard_reset)
1292 int rc;
1294 if (hard_reset)
1296 /* This should reset both master and slave, we don't yet know what's in */
1297 ata_device = 0;
1298 if (ata_hard_reset())
1299 return -1;
1302 rc = master_slave_detect();
1303 if (rc)
1304 return -10 + rc;
1306 /* symptom fix: else check_registers() below may fail */
1307 if (hard_reset && !wait_for_bsy())
1308 return -20;
1310 rc = check_registers();
1311 if (rc)
1312 return -30 + rc;
1314 return 0;
1317 int STORAGE_INIT_ATTR ata_init(void)
1319 int rc = 0;
1320 bool coldstart;
1322 if ( !initialized ) {
1323 mutex_init(&ata_mtx);
1324 queue_init(&ata_queue, true);
1327 mutex_lock(&ata_mtx);
1329 /* must be called before ata_device_init() */
1330 coldstart = ata_is_coldstart();
1331 ata_led(false);
1332 ata_device_init();
1333 sleeping = false;
1334 ata_enable(true);
1335 #ifdef MAX_PHYS_SECTOR_SIZE
1336 memset(&sector_cache, 0, sizeof(sector_cache));
1337 #endif
1339 if ( !initialized ) {
1340 /* First call won't have multiple thread contention - this
1341 * may return at any point without having to unlock */
1342 mutex_unlock(&ata_mtx);
1344 if (!ide_powered()) /* somebody has switched it off */
1346 ide_power_enable(true);
1347 sleep(HZ/4); /* allow voltage to build up */
1350 #ifdef HAVE_ATA_DMA
1351 /* DMA requires INTRQ be enabled */
1352 ATA_OUT8(ATA_CONTROL, 0);
1353 #endif
1355 /* first try, hard reset at cold start only */
1356 rc = init_and_check(coldstart);
1358 if (rc)
1359 { /* failed? -> second try, always with hard reset */
1360 DEBUGF("ata: init failed, retrying...\n");
1361 rc = init_and_check(true);
1362 if (rc)
1363 return rc;
1366 rc = identify();
1368 if (rc)
1369 return -40 + rc;
1371 multisectors = identify_info[47] & 0xff;
1372 if (multisectors == 0) /* Invalid multisector info, try with 16 */
1373 multisectors = 16;
1375 DEBUGF("ata: %d sectors per ata request\n",multisectors);
1377 total_sectors = identify_info[60] | (identify_info[61] << 16);
1379 #ifdef HAVE_LBA48
1380 if (identify_info[83] & 0x0400 /* 48 bit address support */
1381 && total_sectors == 0x0FFFFFFF) /* and disk size >= 128 GiB */
1382 { /* (needs BigLBA addressing) */
1383 if (identify_info[102] || identify_info[103])
1384 panicf("Unsupported disk size: >= 2^32 sectors");
1386 total_sectors = identify_info[100] | (identify_info[101] << 16);
1387 lba48 = true; /* use BigLBA */
1389 #endif
1390 rc = freeze_lock();
1392 if (rc)
1393 return -50 + rc;
1395 rc = set_features();
1396 if (rc)
1397 return -60 + rc;
1399 #ifdef MAX_PHYS_SECTOR_SIZE
1400 /* Find out the physical sector size */
1401 if((identify_info[106] & 0xe000) == 0x6000)
1402 phys_sector_mult = BIT_N(identify_info[106] & 0x000f);
1403 else
1404 phys_sector_mult = 1;
1406 DEBUGF("ata: %d logical sectors per phys sector", phys_sector_mult);
1408 if (phys_sector_mult > 1)
1410 /* Check if drive really needs emulation - if we can access
1411 * sector 1 then assume the drive will handle it better than
1412 * us, and ignore the large physical sectors.
1414 char throwaway[SECTOR_SIZE];
1415 rc = ata_transfer_sectors(1, 1, &throwaway, false);
1416 if (rc == 0)
1417 phys_sector_mult = 1;
1420 if (phys_sector_mult > (MAX_PHYS_SECTOR_SIZE/SECTOR_SIZE))
1421 panicf("Unsupported physical sector size: %d",
1422 phys_sector_mult * SECTOR_SIZE);
1423 #endif
1425 mutex_lock(&ata_mtx); /* Balance unlock below */
1427 last_disk_activity = current_tick;
1428 #ifdef ATA_DRIVER_CLOSE
1429 ata_thread_id =
1430 #endif
1431 create_thread(ata_thread, ata_stack,
1432 sizeof(ata_stack), 0, ata_thread_name
1433 IF_PRIO(, PRIORITY_USER_INTERFACE)
1434 IF_COP(, CPU));
1435 initialized = true;
1438 rc = set_multiple_mode(multisectors);
1439 if (rc)
1440 rc = -70 + rc;
1442 mutex_unlock(&ata_mtx);
1443 return rc;
1446 #ifdef ATA_DRIVER_CLOSE
1447 void ata_close(void)
1449 unsigned int thread_id = ata_thread_id;
1451 if (thread_id == 0)
1452 return;
1454 ata_thread_id = 0;
1456 queue_post(&ata_queue, Q_CLOSE, 0);
1457 thread_wait(thread_id);
1459 #endif /* ATA_DRIVER_CLOSE */
1461 #if (CONFIG_LED == LED_REAL)
1462 void ata_set_led_enabled(bool enabled)
1464 ata_led_enabled = enabled;
1465 if (ata_led_enabled)
1466 led(ata_led_on);
1467 else
1468 led(false);
1470 #endif
1472 long ata_last_disk_activity(void)
1474 return last_disk_activity;
1477 int ata_spinup_time(void)
1479 return spinup_time;
1482 #ifdef STORAGE_GET_INFO
1483 void ata_get_info(IF_MD2(int drive,)struct storage_info *info)
1485 unsigned short *src,*dest;
1486 static char vendor[8];
1487 static char product[16];
1488 static char revision[4];
1489 #ifdef HAVE_MULTIDRIVE
1490 (void)drive; /* unused for now */
1491 #endif
1492 int i;
1493 info->sector_size = SECTOR_SIZE;
1494 info->num_sectors = total_sectors;
1496 src = (unsigned short*)&identify_info[27];
1497 dest = (unsigned short*)vendor;
1498 for (i=0;i<4;i++)
1499 dest[i] = htobe16(src[i]);
1500 info->vendor=vendor;
1502 src = (unsigned short*)&identify_info[31];
1503 dest = (unsigned short*)product;
1504 for (i=0;i<8;i++)
1505 dest[i] = htobe16(src[i]);
1506 info->product=product;
1508 src = (unsigned short*)&identify_info[23];
1509 dest = (unsigned short*)revision;
1510 for (i=0;i<2;i++)
1511 dest[i] = htobe16(src[i]);
1512 info->revision=revision;
1514 #endif
1516 #ifdef HAVE_ATA_DMA
1517 /* Returns last DMA mode as set by set_features() */
1518 int ata_get_dma_mode(void)
1520 return dma_mode;
1523 /* Needed to allow updating while waiting for DMA to complete */
1524 void ata_keep_active(void)
1526 last_disk_activity = current_tick;
1528 #endif
1530 #ifdef CONFIG_STORAGE_MULTI
1531 int ata_num_drives(int first_drive)
1533 /* We don't care which logical drive number(s) we have been assigned */
1534 (void)first_drive;
1536 return 1;
1538 #endif