Fix USB storage driver. ata_get_info was no longer filling-in num_sectors after recen...
[maemo-rb.git] / firmware / drivers / ata.c
blob77cd89f13f7580842026842cf7595843ba24f03f
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) && MEM == 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 STATICIRAM ICODE_ATTR int wait_for_bsy(void)
206 long timeout = current_tick + HZ*30;
210 if (!(ATA_IN8(ATA_STATUS) & STATUS_BSY))
211 return 1;
212 last_disk_activity = current_tick;
213 yield();
214 } while (TIME_BEFORE(current_tick, timeout));
216 return 0; /* timeout */
219 STATICIRAM ICODE_ATTR int wait_for_rdy(void)
221 long timeout;
223 if (!wait_for_bsy())
224 return 0;
226 timeout = current_tick + HZ*10;
230 if (ATA_IN8(ATA_ALT_STATUS) & STATUS_RDY)
231 return 1;
232 last_disk_activity = current_tick;
233 yield();
234 } while (TIME_BEFORE(current_tick, timeout));
236 return 0; /* timeout */
239 STATICIRAM ICODE_ATTR int wait_for_start_of_transfer(void)
241 if (!wait_for_bsy())
242 return 0;
244 return (ATA_IN8(ATA_ALT_STATUS) & (STATUS_BSY|STATUS_DRQ)) == STATUS_DRQ;
247 STATICIRAM ICODE_ATTR int wait_for_end_of_transfer(void)
249 if (!wait_for_bsy())
250 return 0;
251 return (ATA_IN8(ATA_ALT_STATUS) &
252 (STATUS_BSY|STATUS_RDY|STATUS_DF|STATUS_DRQ|STATUS_ERR))
253 == STATUS_RDY;
256 #if (CONFIG_LED == LED_REAL)
257 /* Conditionally block LED access for the ATA driver, so the LED can be
258 * (mis)used for other purposes */
259 static void ata_led(bool on)
261 ata_led_on = on;
262 if (ata_led_enabled)
263 led(ata_led_on);
265 #else
266 #define ata_led(on) led(on)
267 #endif
269 #ifndef ATA_OPTIMIZED_READING
270 STATICIRAM ICODE_ATTR void copy_read_sectors(unsigned char* buf, int wordcount)
272 unsigned short tmp = 0;
274 if ( (unsigned long)buf & 1)
275 { /* not 16-bit aligned, copy byte by byte */
276 unsigned char* bufend = buf + wordcount*2;
279 tmp = ATA_IN16(ATA_DATA);
280 #if defined(ROCKBOX_LITTLE_ENDIAN)
281 *buf++ = tmp & 0xff; /* I assume big endian */
282 *buf++ = tmp >> 8; /* and don't use the SWAB16 macro */
283 #else
284 *buf++ = tmp >> 8;
285 *buf++ = tmp & 0xff;
286 #endif
287 } while (buf < bufend); /* tail loop is faster */
289 else
290 { /* 16-bit aligned, can do faster copy */
291 unsigned short* wbuf = (unsigned short*)buf;
292 unsigned short* wbufend = wbuf + wordcount;
295 *wbuf = ATA_IN16(ATA_DATA);
296 } while (++wbuf < wbufend); /* tail loop is faster */
299 #endif /* !ATA_OPTIMIZED_READING */
301 #ifndef ATA_OPTIMIZED_WRITING
302 STATICIRAM ICODE_ATTR void copy_write_sectors(const unsigned char* buf,
303 int wordcount)
305 if ( (unsigned long)buf & 1)
306 { /* not 16-bit aligned, copy byte by byte */
307 unsigned short tmp = 0;
308 const unsigned char* bufend = buf + wordcount*2;
311 #if defined(ROCKBOX_LITTLE_ENDIAN)
312 tmp = (unsigned short) *buf++;
313 tmp |= (unsigned short) *buf++ << 8;
314 #else
315 tmp = (unsigned short) *buf++ << 8;
316 tmp |= (unsigned short) *buf++;
317 #endif
318 ATA_OUT16(ATA_DATA, tmp);
319 } while (buf < bufend); /* tail loop is faster */
321 else
322 { /* 16-bit aligned, can do faster copy */
323 unsigned short* wbuf = (unsigned short*)buf;
324 unsigned short* wbufend = wbuf + wordcount;
327 ATA_OUT16(ATA_DATA, *wbuf);
328 } while (++wbuf < wbufend); /* tail loop is faster */
331 #endif /* !ATA_OPTIMIZED_WRITING */
333 static int ata_transfer_sectors(unsigned long start,
334 int incount,
335 void* inbuf,
336 int write)
338 int ret = 0;
339 long timeout;
340 int count;
341 void* buf;
342 long spinup_start;
343 #ifdef HAVE_ATA_DMA
344 bool usedma = false;
345 #endif
347 #ifndef MAX_PHYS_SECTOR_SIZE
348 mutex_lock(&ata_mtx);
349 #endif
351 if (start + incount > total_sectors) {
352 ret = -1;
353 goto error;
356 last_disk_activity = current_tick;
357 spinup_start = current_tick;
359 ata_led(true);
361 if ( sleeping ) {
362 sleeping = false; /* set this now since it'll be on */
363 spinup = true;
364 if (poweroff) {
365 if (ata_power_on()) {
366 ret = -2;
367 goto error;
370 else {
371 if (perform_soft_reset()) {
372 ret = -2;
373 goto error;
378 timeout = current_tick + READWRITE_TIMEOUT;
380 ATA_OUT8(ATA_SELECT, ata_device);
381 if (!wait_for_rdy())
383 ret = -3;
384 goto error;
387 retry:
388 buf = inbuf;
389 count = incount;
390 while (TIME_BEFORE(current_tick, timeout)) {
391 ret = 0;
392 last_disk_activity = current_tick;
394 #ifdef HAVE_ATA_DMA
395 /* If DMA is supported and parameters are ok for DMA, use it */
396 if (dma_mode && ata_dma_setup(inbuf, incount * SECTOR_SIZE, write))
397 usedma = true;
398 #endif
400 #ifdef HAVE_LBA48
401 if (lba48)
403 ATA_OUT8(ATA_NSECTOR, count >> 8);
404 ATA_OUT8(ATA_NSECTOR, count & 0xff);
405 ATA_OUT8(ATA_SECTOR, (start >> 24) & 0xff); /* 31:24 */
406 ATA_OUT8(ATA_SECTOR, start & 0xff); /* 7:0 */
407 ATA_OUT8(ATA_LCYL, 0); /* 39:32 */
408 ATA_OUT8(ATA_LCYL, (start >> 8) & 0xff); /* 15:8 */
409 ATA_OUT8(ATA_HCYL, 0); /* 47:40 */
410 ATA_OUT8(ATA_HCYL, (start >> 16) & 0xff); /* 23:16 */
411 ATA_OUT8(ATA_SELECT, SELECT_LBA | ata_device);
412 #ifdef HAVE_ATA_DMA
413 if (write)
414 ATA_OUT8(ATA_COMMAND, usedma ? CMD_WRITE_DMA_EXT : CMD_WRITE_MULTIPLE_EXT);
415 else
416 ATA_OUT8(ATA_COMMAND, usedma ? CMD_READ_DMA_EXT : CMD_READ_MULTIPLE_EXT);
417 #else
418 ATA_OUT8(ATA_COMMAND, write ? CMD_WRITE_MULTIPLE_EXT : CMD_READ_MULTIPLE_EXT);
419 #endif
421 else
422 #endif
424 ATA_OUT8(ATA_NSECTOR, count & 0xff); /* 0 means 256 sectors */
425 ATA_OUT8(ATA_SECTOR, start & 0xff);
426 ATA_OUT8(ATA_LCYL, (start >> 8) & 0xff);
427 ATA_OUT8(ATA_HCYL, (start >> 16) & 0xff);
428 ATA_OUT8(ATA_SELECT, ((start >> 24) & 0xf) | SELECT_LBA | ata_device);
429 #ifdef HAVE_ATA_DMA
430 if (write)
431 ATA_OUT8(ATA_COMMAND, usedma ? CMD_WRITE_DMA : CMD_WRITE_MULTIPLE);
432 else
433 ATA_OUT8(ATA_COMMAND, usedma ? CMD_READ_DMA : CMD_READ_MULTIPLE);
434 #else
435 ATA_OUT8(ATA_COMMAND, write ? CMD_WRITE_MULTIPLE : CMD_READ_MULTIPLE);
436 #endif
439 /* wait at least 400ns between writing command and reading status */
440 __asm__ volatile ("nop");
441 __asm__ volatile ("nop");
442 __asm__ volatile ("nop");
443 __asm__ volatile ("nop");
444 __asm__ volatile ("nop");
446 #ifdef HAVE_ATA_DMA
447 if (usedma) {
448 if (!ata_dma_finish())
449 ret = -7;
451 if (ret != 0) {
452 perform_soft_reset();
453 goto retry;
456 if (spinup) {
457 spinup_time = current_tick - spinup_start;
458 spinup = false;
459 poweroff = false;
462 else
463 #endif /* HAVE_ATA_DMA */
465 while (count) {
466 int sectors;
467 int wordcount;
468 int status;
469 int error;
471 if (!wait_for_start_of_transfer()) {
472 /* We have timed out waiting for RDY and/or DRQ, possibly
473 because the hard drive is shaking and has problems
474 reading the data. We have two options:
475 1) Wait some more
476 2) Perform a soft reset and try again.
478 We choose alternative 2.
480 perform_soft_reset();
481 ret = -5;
482 goto retry;
485 if (spinup) {
486 spinup_time = current_tick - spinup_start;
487 spinup = false;
488 poweroff = false;
491 /* read the status register exactly once per loop */
492 status = ATA_IN8(ATA_STATUS);
493 error = ATA_IN8(ATA_ERROR);
495 if (count >= multisectors)
496 sectors = multisectors;
497 else
498 sectors = count;
500 wordcount = sectors * SECTOR_SIZE / 2;
502 if (write)
503 copy_write_sectors(buf, wordcount);
504 else
505 copy_read_sectors(buf, wordcount);
508 "Device errors encountered during READ MULTIPLE commands
509 are posted at the beginning of the block or partial block
510 transfer, but the DRQ bit is still set to one and the data
511 transfer shall take place, including transfer of corrupted
512 data, if any."
513 -- ATA specification
515 if ( status & (STATUS_BSY | STATUS_ERR | STATUS_DF) ) {
516 perform_soft_reset();
517 ret = -6;
518 /* no point retrying IDNF, sector no. was invalid */
519 if (error & ERROR_IDNF)
520 break;
521 goto retry;
524 buf += sectors * SECTOR_SIZE; /* Advance one chunk of sectors */
525 count -= sectors;
527 last_disk_activity = current_tick;
531 if(!ret && !wait_for_end_of_transfer()) {
532 int error;
534 error = ATA_IN8(ATA_ERROR);
535 perform_soft_reset();
536 ret = -4;
537 /* no point retrying IDNF, sector no. was invalid */
538 if (error & ERROR_IDNF)
539 break;
540 goto retry;
542 break;
545 error:
546 ata_led(false);
547 #ifndef MAX_PHYS_SECTOR_SIZE
548 mutex_unlock(&ata_mtx);
549 #endif
551 return ret;
554 #ifndef MAX_PHYS_SECTOR_SIZE
555 int ata_read_sectors(IF_MD2(int drive,)
556 unsigned long start,
557 int incount,
558 void* inbuf)
560 #ifdef HAVE_MULTIDRIVE
561 (void)drive; /* unused for now */
562 #endif
564 return ata_transfer_sectors(start, incount, inbuf, false);
566 #endif
568 #ifndef MAX_PHYS_SECTOR_SIZE
569 int ata_write_sectors(IF_MD2(int drive,)
570 unsigned long start,
571 int count,
572 const void* buf)
574 #ifdef HAVE_MULTIDRIVE
575 (void)drive; /* unused for now */
576 #endif
578 return ata_transfer_sectors(start, count, (void*)buf, true);
580 #endif
582 #ifdef MAX_PHYS_SECTOR_SIZE
583 static int cache_sector(unsigned long sector)
585 int rc;
587 sector &= ~(phys_sector_mult - 1);
588 /* round down to physical sector boundary */
590 /* check whether the sector is already cached */
591 if (sector_cache.inuse && (sector_cache.sectornum == sector))
592 return 0;
594 /* not found: read the sector */
595 sector_cache.inuse = false;
596 rc = ata_transfer_sectors(sector, phys_sector_mult, sector_cache.data, false);
597 if (!rc)
599 sector_cache.sectornum = sector;
600 sector_cache.inuse = true;
602 return rc;
605 static inline int flush_current_sector(void)
607 return ata_transfer_sectors(sector_cache.sectornum, phys_sector_mult,
608 sector_cache.data, true);
611 int ata_read_sectors(IF_MD2(int drive,)
612 unsigned long start,
613 int incount,
614 void* inbuf)
616 int rc = 0;
617 int offset;
619 #ifdef HAVE_MULTIDRIVE
620 (void)drive; /* unused for now */
621 #endif
622 mutex_lock(&ata_mtx);
624 offset = start & (phys_sector_mult - 1);
626 if (offset) /* first partial sector */
628 int partcount = MIN(incount, phys_sector_mult - offset);
630 rc = cache_sector(start);
631 if (rc)
633 rc = rc * 10 - 1;
634 goto error;
636 memcpy(inbuf, sector_cache.data + offset * SECTOR_SIZE,
637 partcount * SECTOR_SIZE);
639 start += partcount;
640 inbuf += partcount * SECTOR_SIZE;
641 incount -= partcount;
643 if (incount)
645 offset = incount & (phys_sector_mult - 1);
646 incount -= offset;
648 if (incount)
650 rc = ata_transfer_sectors(start, incount, inbuf, false);
651 if (rc)
653 rc = rc * 10 - 2;
654 goto error;
656 start += incount;
657 inbuf += incount * SECTOR_SIZE;
659 if (offset)
661 rc = cache_sector(start);
662 if (rc)
664 rc = rc * 10 - 3;
665 goto error;
667 memcpy(inbuf, sector_cache.data, offset * SECTOR_SIZE);
671 error:
672 mutex_unlock(&ata_mtx);
674 return rc;
677 int ata_write_sectors(IF_MD2(int drive,)
678 unsigned long start,
679 int count,
680 const void* buf)
682 int rc = 0;
683 int offset;
685 #ifdef HAVE_MULTIDRIVE
686 (void)drive; /* unused for now */
687 #endif
688 mutex_lock(&ata_mtx);
690 offset = start & (phys_sector_mult - 1);
692 if (offset) /* first partial sector */
694 int partcount = MIN(count, phys_sector_mult - offset);
696 rc = cache_sector(start);
697 if (rc)
699 rc = rc * 10 - 1;
700 goto error;
702 memcpy(sector_cache.data + offset * SECTOR_SIZE, buf,
703 partcount * SECTOR_SIZE);
704 rc = flush_current_sector();
705 if (rc)
707 rc = rc * 10 - 2;
708 goto error;
710 start += partcount;
711 buf += partcount * SECTOR_SIZE;
712 count -= partcount;
714 if (count)
716 offset = count & (phys_sector_mult - 1);
717 count -= offset;
719 if (count)
721 rc = ata_transfer_sectors(start, count, (void*)buf, true);
722 if (rc)
724 rc = rc * 10 - 3;
725 goto error;
727 start += count;
728 buf += count * SECTOR_SIZE;
730 if (offset)
732 rc = cache_sector(start);
733 if (rc)
735 rc = rc * 10 - 4;
736 goto error;
738 memcpy(sector_cache.data, buf, offset * SECTOR_SIZE);
739 rc = flush_current_sector();
740 if (rc)
742 rc = rc * 10 - 5;
743 goto error;
748 error:
749 mutex_unlock(&ata_mtx);
751 return rc;
753 #endif /* MAX_PHYS_SECTOR_SIZE */
755 static int check_registers(void)
757 int i;
758 wait_for_bsy();
759 if (ATA_IN8(ATA_STATUS) & STATUS_BSY)
760 return -1;
762 for (i = 0; i<64; i++) {
763 ATA_OUT8(ATA_NSECTOR, TEST_PATTERN1);
764 ATA_OUT8(ATA_SECTOR, TEST_PATTERN2);
765 ATA_OUT8(ATA_LCYL, TEST_PATTERN3);
766 ATA_OUT8(ATA_HCYL, TEST_PATTERN4);
768 if (((ATA_IN8(ATA_NSECTOR) & 0xff) == TEST_PATTERN1) &&
769 ((ATA_IN8(ATA_SECTOR) & 0xff) == TEST_PATTERN2) &&
770 ((ATA_IN8(ATA_LCYL) & 0xff) == TEST_PATTERN3) &&
771 ((ATA_IN8(ATA_HCYL) & 0xff) == TEST_PATTERN4))
772 return 0;
774 sleep(1);
776 return -2;
779 static int freeze_lock(void)
781 /* does the disk support Security Mode feature set? */
782 if (identify_info[82] & 2)
784 ATA_OUT8(ATA_SELECT, ata_device);
786 if (!wait_for_rdy())
787 return -1;
789 ATA_OUT8(ATA_COMMAND, CMD_SECURITY_FREEZE_LOCK);
791 if (!wait_for_rdy())
792 return -2;
795 return 0;
798 void ata_spindown(int seconds)
800 sleep_timeout = seconds * HZ;
803 bool ata_disk_is_active(void)
805 return !sleeping;
808 static int ata_perform_sleep(void)
810 /* guard against calls made with checks of these variables outside
811 the mutex that may not be on the ata thread; status may have changed. */
812 if (spinup || sleeping) {
813 return 0;
816 ATA_OUT8(ATA_SELECT, ata_device);
818 if(!wait_for_rdy()) {
819 DEBUGF("ata_perform_sleep() - not RDY\n");
820 return -1;
823 ATA_OUT8(ATA_COMMAND, CMD_SLEEP);
825 if (!wait_for_rdy())
827 DEBUGF("ata_perform_sleep() - CMD failed\n");
828 return -2;
831 sleeping = true;
832 return 0;
835 void ata_sleep(void)
837 queue_post(&ata_queue, Q_SLEEP, 0);
840 void ata_sleepnow(void)
842 if (!spinup && !sleeping && initialized)
844 call_storage_idle_notifys(false);
845 mutex_lock(&ata_mtx);
846 ata_perform_sleep();
847 mutex_unlock(&ata_mtx);
851 void ata_spin(void)
853 last_user_activity = current_tick;
856 static void ata_thread(void)
858 static long last_sleep = 0;
859 struct queue_event ev;
860 #ifdef ALLOW_USB_SPINDOWN
861 static bool usb_mode = false;
862 #endif
864 while (1) {
865 queue_wait_w_tmo(&ata_queue, &ev, HZ/2);
867 switch ( ev.id ) {
868 case SYS_TIMEOUT:
869 if (!spinup && !sleeping)
871 if (TIME_AFTER( current_tick,
872 last_disk_activity + (HZ*2) ) )
874 #ifdef ALLOW_USB_SPINDOWN
875 if(!usb_mode)
876 #endif
878 call_storage_idle_notifys(false);
882 if ( sleep_timeout &&
883 TIME_AFTER( current_tick,
884 last_user_activity + sleep_timeout ) &&
885 TIME_AFTER( current_tick,
886 last_disk_activity + sleep_timeout ) )
888 #ifdef ALLOW_USB_SPINDOWN
889 if(!usb_mode)
890 #endif
892 call_storage_idle_notifys(true);
894 mutex_lock(&ata_mtx);
895 ata_perform_sleep();
896 last_sleep = current_tick;
897 mutex_unlock(&ata_mtx);
901 #ifdef HAVE_ATA_POWER_OFF
902 if ( !spinup && sleeping && !poweroff &&
903 TIME_AFTER( current_tick, last_sleep + ATA_POWER_OFF_TIMEOUT ))
905 mutex_lock(&ata_mtx);
906 ide_power_enable(false);
907 poweroff = true;
908 mutex_unlock(&ata_mtx);
910 #endif
911 break;
913 #ifndef USB_NONE
914 case SYS_USB_CONNECTED:
915 /* Tell the USB thread that we are safe */
916 DEBUGF("ata_thread got SYS_USB_CONNECTED\n");
917 #ifdef ALLOW_USB_SPINDOWN
918 usb_mode = true;
919 usb_acknowledge(SYS_USB_CONNECTED_ACK);
920 /* There is no need to force ATA power on */
921 #else
922 mutex_lock(&ata_mtx);
923 if (sleeping) {
924 ata_led(true);
925 sleeping = false; /* set this now since it'll be on */
927 if (poweroff) {
928 ata_power_on();
929 poweroff = false;
931 else {
932 perform_soft_reset();
935 ata_led(false);
937 mutex_unlock(&ata_mtx);
939 /* Wait until the USB cable is extracted again */
940 usb_acknowledge(SYS_USB_CONNECTED_ACK);
941 usb_wait_for_disconnect(&ata_queue);
942 #endif
943 break;
945 #ifdef ALLOW_USB_SPINDOWN
946 case SYS_USB_DISCONNECTED:
947 /* Tell the USB thread that we are ready again */
948 DEBUGF("ata_thread got SYS_USB_DISCONNECTED\n");
949 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
950 usb_mode = false;
951 break;
952 #endif
953 #endif /* USB_NONE */
955 case Q_SLEEP:
956 #ifdef ALLOW_USB_SPINDOWN
957 if(!usb_mode)
958 #endif
960 call_storage_idle_notifys(false);
962 last_disk_activity = current_tick - sleep_timeout + (HZ/2);
963 break;
965 #ifdef ATA_DRIVER_CLOSE
966 case Q_CLOSE:
967 return;
968 #endif
973 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
974 static int ata_hard_reset(void)
976 int ret;
978 mutex_lock(&ata_mtx);
980 ata_reset();
982 /* state HRR2 */
983 ATA_OUT8(ATA_SELECT, ata_device); /* select the right device */
984 ret = wait_for_bsy();
986 /* Massage the return code so it is 0 on success and -1 on failure */
987 ret = ret?0:-1;
989 mutex_unlock(&ata_mtx);
991 return ret;
994 static int perform_soft_reset(void)
996 /* If this code is allowed to run on a Nano, the next reads from the flash will
997 * time out, so we disable it. It shouldn't be necessary anyway, since the
998 * ATA -> Flash interface automatically sleeps almost immediately after the
999 * last command.
1001 int ret;
1002 int retry_count;
1004 ATA_OUT8(ATA_SELECT, SELECT_LBA | ata_device );
1005 ATA_OUT8(ATA_CONTROL, CONTROL_nIEN|CONTROL_SRST );
1006 sleep(1); /* >= 5us */
1008 #ifdef HAVE_ATA_DMA
1009 /* DMA requires INTRQ be enabled */
1010 ATA_OUT8(ATA_CONTROL, 0);
1011 #else
1012 ATA_OUT8(ATA_CONTROL, CONTROL_nIEN);
1013 #endif
1014 sleep(1); /* >2ms */
1016 /* This little sucker can take up to 30 seconds */
1017 retry_count = 8;
1020 ret = wait_for_rdy();
1021 } while(!ret && retry_count--);
1023 if (!ret)
1024 return -1;
1026 if (set_features())
1027 return -2;
1029 if (set_multiple_mode(multisectors))
1030 return -3;
1032 if (freeze_lock())
1033 return -4;
1035 return 0;
1038 int ata_soft_reset(void)
1040 int ret;
1042 mutex_lock(&ata_mtx);
1044 ret = perform_soft_reset();
1046 mutex_unlock(&ata_mtx);
1047 return ret;
1050 static int ata_power_on(void)
1052 int rc;
1054 ide_power_enable(true);
1055 sleep(HZ/4); /* allow voltage to build up */
1057 /* Accessing the PP IDE controller too early after powering up the disk
1058 * makes the core hang for a short time, causing an audio dropout. This
1059 * also depends on the disk; iPod Mini G2 needs at least HZ/5 to get rid
1060 * of the dropout. Since this time isn't additive (the wait_for_bsy() in
1061 * ata_hard_reset() will shortened by the same amount), it's a good idea
1062 * to do this on all HDD based targets. */
1064 if( ata_hard_reset() )
1065 return -1;
1067 rc = set_features();
1068 if (rc)
1069 return rc * 10 - 2;
1071 if (set_multiple_mode(multisectors))
1072 return -3;
1074 if (freeze_lock())
1075 return -4;
1077 return 0;
1080 static int master_slave_detect(void)
1082 /* master? */
1083 ATA_OUT8(ATA_SELECT, 0);
1084 if (ATA_IN8(ATA_STATUS) & (STATUS_RDY|STATUS_BSY)) {
1085 ata_device = 0;
1086 DEBUGF("Found master harddisk\n");
1088 else {
1089 /* slave? */
1090 ATA_OUT8(ATA_SELECT, SELECT_DEVICE1);
1091 if (ATA_IN8(ATA_STATUS) & (STATUS_RDY|STATUS_BSY)) {
1092 ata_device = SELECT_DEVICE1;
1093 DEBUGF("Found slave harddisk\n");
1095 else
1096 return -1;
1098 return 0;
1101 static int identify(void)
1103 int i;
1105 ATA_OUT8(ATA_SELECT, ata_device);
1107 if(!wait_for_rdy()) {
1108 DEBUGF("identify() - not RDY\n");
1109 return -1;
1111 ATA_OUT8(ATA_COMMAND, CMD_IDENTIFY);
1113 if (!wait_for_start_of_transfer())
1115 DEBUGF("identify() - CMD failed\n");
1116 return -2;
1119 for (i=0; i<SECTOR_SIZE/2; i++) {
1120 /* the IDENTIFY words are already swapped, so we need to treat
1121 this info differently that normal sector data */
1122 identify_info[i] = ATA_SWAP_IDENTIFY(ATA_IN16(ATA_DATA));
1125 return 0;
1128 static int set_multiple_mode(int sectors)
1130 ATA_OUT8(ATA_SELECT, ata_device);
1132 if(!wait_for_rdy()) {
1133 DEBUGF("set_multiple_mode() - not RDY\n");
1134 return -1;
1137 ATA_OUT8(ATA_NSECTOR, sectors);
1138 ATA_OUT8(ATA_COMMAND, CMD_SET_MULTIPLE_MODE);
1140 if (!wait_for_rdy())
1142 DEBUGF("set_multiple_mode() - CMD failed\n");
1143 return -2;
1146 return 0;
1149 #ifdef HAVE_ATA_DMA
1150 static int get_best_mode(unsigned short identword, int max, int modetype)
1152 unsigned short testbit = BIT_N(max);
1154 while (1) {
1155 if (identword & testbit)
1156 return max | modetype;
1157 testbit >>= 1;
1158 if (!testbit)
1159 return 0;
1160 max--;
1163 #endif
1165 static int set_features(void)
1167 static struct {
1168 unsigned char id_word;
1169 unsigned char id_bit;
1170 unsigned char subcommand;
1171 unsigned char parameter;
1172 } features[] = {
1173 { 83, 14, 0x03, 0 }, /* force PIO mode */
1174 { 83, 3, 0x05, 0x80 }, /* adv. power management: lowest w/o standby */
1175 { 83, 9, 0x42, 0x80 }, /* acoustic management: lowest noise */
1176 { 82, 6, 0xaa, 0 }, /* enable read look-ahead */
1177 #ifdef HAVE_ATA_DMA
1178 { 0, 0, 0x03, 0 }, /* DMA mode */
1179 #endif
1181 int i;
1182 int pio_mode = 2;
1184 /* Find out the highest supported PIO mode */
1185 if(identify_info[64] & 2)
1186 pio_mode = 4;
1187 else
1188 if(identify_info[64] & 1)
1189 pio_mode = 3;
1191 /* Update the table: set highest supported pio mode that we also support */
1192 features[0].parameter = 8 + pio_mode;
1194 #ifdef HAVE_ATA_DMA
1195 if (identify_info[53] & (1<<2))
1196 /* Ultra DMA mode info present, find a mode */
1197 dma_mode = get_best_mode(identify_info[88], ATA_MAX_UDMA, 0x40);
1199 if (!dma_mode) {
1200 /* No UDMA mode found, try to find a multi-word DMA mode */
1201 dma_mode = get_best_mode(identify_info[63], ATA_MAX_MWDMA, 0x20);
1202 features[4].id_word = 63;
1204 else
1205 features[4].id_word = 88;
1207 features[4].id_bit = dma_mode & 7;
1208 features[4].parameter = dma_mode;
1209 #endif /* HAVE_ATA_DMA */
1211 ATA_OUT8(ATA_SELECT, ata_device);
1213 if (!wait_for_rdy()) {
1214 DEBUGF("set_features() - not RDY\n");
1215 return -1;
1218 for (i=0; i < (int)(sizeof(features)/sizeof(features[0])); i++) {
1219 if (identify_info[features[i].id_word] & BIT_N(features[i].id_bit)) {
1220 ATA_OUT8(ATA_FEATURE, features[i].subcommand);
1221 ATA_OUT8(ATA_NSECTOR, features[i].parameter);
1222 ATA_OUT8(ATA_COMMAND, CMD_SET_FEATURES);
1224 if (!wait_for_rdy()) {
1225 DEBUGF("set_features() - CMD failed\n");
1226 return -10 - i;
1229 if((ATA_IN8(ATA_ALT_STATUS) & STATUS_ERR) && (i != 1)) {
1230 /* some CF cards don't like advanced powermanagement
1231 even if they mark it as supported - go figure... */
1232 if(ATA_IN8(ATA_ERROR) & ERROR_ABRT) {
1233 return -20 - i;
1239 #ifdef ATA_SET_DEVICE_FEATURES
1240 ata_set_pio_timings(pio_mode);
1241 #endif
1243 #ifdef HAVE_ATA_DMA
1244 ata_dma_set_mode(dma_mode);
1245 #endif
1247 return 0;
1250 unsigned short* ata_get_identify(void)
1252 return identify_info;
1255 static int init_and_check(bool hard_reset)
1257 int rc;
1259 if (hard_reset)
1261 /* This should reset both master and slave, we don't yet know what's in */
1262 ata_device = 0;
1263 if (ata_hard_reset())
1264 return -1;
1267 rc = master_slave_detect();
1268 if (rc)
1269 return -10 + rc;
1271 /* symptom fix: else check_registers() below may fail */
1272 if (hard_reset && !wait_for_bsy())
1273 return -20;
1275 rc = check_registers();
1276 if (rc)
1277 return -30 + rc;
1279 return 0;
1282 int ata_init(void)
1284 int rc = 0;
1285 bool coldstart;
1287 if ( !initialized ) {
1288 mutex_init(&ata_mtx);
1289 queue_init(&ata_queue, true);
1292 mutex_lock(&ata_mtx);
1294 /* must be called before ata_device_init() */
1295 coldstart = ata_is_coldstart();
1296 ata_led(false);
1297 ata_device_init();
1298 sleeping = false;
1299 ata_enable(true);
1300 #ifdef MAX_PHYS_SECTOR_SIZE
1301 memset(&sector_cache, 0, sizeof(sector_cache));
1302 #endif
1304 if ( !initialized ) {
1305 /* First call won't have multiple thread contention - this
1306 * may return at any point without having to unlock */
1307 mutex_unlock(&ata_mtx);
1309 if (!ide_powered()) /* somebody has switched it off */
1311 ide_power_enable(true);
1312 sleep(HZ/4); /* allow voltage to build up */
1315 #ifdef HAVE_ATA_DMA
1316 /* DMA requires INTRQ be enabled */
1317 ATA_OUT8(ATA_CONTROL, 0);
1318 #endif
1320 /* first try, hard reset at cold start only */
1321 rc = init_and_check(coldstart);
1323 if (rc)
1324 { /* failed? -> second try, always with hard reset */
1325 DEBUGF("ata: init failed, retrying...\n");
1326 rc = init_and_check(true);
1327 if (rc)
1328 return rc;
1331 rc = identify();
1333 if (rc)
1334 return -40 + rc;
1336 multisectors = identify_info[47] & 0xff;
1337 if (multisectors == 0) /* Invalid multisector info, try with 16 */
1338 multisectors = 16;
1340 DEBUGF("ata: %d sectors per ata request\n",multisectors);
1342 total_sectors = identify_info[60] | (identify_info[61] << 16);
1344 #ifdef HAVE_LBA48
1345 if (identify_info[83] & 0x0400 /* 48 bit address support */
1346 && total_sectors == 0x0FFFFFFF) /* and disk size >= 128 GiB */
1347 { /* (needs BigLBA addressing) */
1348 if (identify_info[102] || identify_info[103])
1349 panicf("Unsupported disk size: >= 2^32 sectors");
1351 total_sectors = identify_info[100] | (identify_info[101] << 16);
1352 lba48 = true; /* use BigLBA */
1354 #endif
1355 rc = freeze_lock();
1357 if (rc)
1358 return -50 + rc;
1360 rc = set_features();
1361 if (rc)
1362 return -60 + rc;
1364 #ifdef MAX_PHYS_SECTOR_SIZE
1365 /* Find out the physical sector size */
1366 if((identify_info[106] & 0xe000) == 0x6000)
1367 phys_sector_mult = BIT_N(identify_info[106] & 0x000f);
1368 else
1369 phys_sector_mult = 1;
1371 DEBUGF("ata: %d logical sectors per phys sector", phys_sector_mult);
1373 if (phys_sector_mult > 1)
1375 /* Check if drive really needs emulation - if we can access
1376 * sector 1 then assume the drive will handle it better than
1377 * us, and ignore the large physical sectors.
1379 char throwaway[SECTOR_SIZE];
1380 rc = ata_transfer_sectors(1, 1, &throwaway, false);
1381 if (rc == 0)
1382 phys_sector_mult = 1;
1385 if (phys_sector_mult > (MAX_PHYS_SECTOR_SIZE/SECTOR_SIZE))
1386 panicf("Unsupported physical sector size: %d",
1387 phys_sector_mult * SECTOR_SIZE);
1388 #endif
1390 mutex_lock(&ata_mtx); /* Balance unlock below */
1392 last_disk_activity = current_tick;
1393 #ifdef ATA_DRIVER_CLOSE
1394 ata_thread_id =
1395 #endif
1396 create_thread(ata_thread, ata_stack,
1397 sizeof(ata_stack), 0, ata_thread_name
1398 IF_PRIO(, PRIORITY_USER_INTERFACE)
1399 IF_COP(, CPU));
1400 initialized = true;
1403 rc = set_multiple_mode(multisectors);
1404 if (rc)
1405 rc = -70 + rc;
1407 mutex_unlock(&ata_mtx);
1408 return rc;
1411 #ifdef ATA_DRIVER_CLOSE
1412 void ata_close(void)
1414 unsigned int thread_id = ata_thread_id;
1416 if (thread_id == 0)
1417 return;
1419 ata_thread_id = 0;
1421 queue_post(&ata_queue, Q_CLOSE, 0);
1422 thread_wait(thread_id);
1424 #endif /* ATA_DRIVER_CLOSE */
1426 #if (CONFIG_LED == LED_REAL)
1427 void ata_set_led_enabled(bool enabled)
1429 ata_led_enabled = enabled;
1430 if (ata_led_enabled)
1431 led(ata_led_on);
1432 else
1433 led(false);
1435 #endif
1437 long ata_last_disk_activity(void)
1439 return last_disk_activity;
1442 int ata_spinup_time(void)
1444 return spinup_time;
1447 #ifdef STORAGE_GET_INFO
1448 void ata_get_info(IF_MD2(int drive,)struct storage_info *info)
1450 unsigned short *src,*dest;
1451 static char vendor[8];
1452 static char product[16];
1453 static char revision[4];
1454 #ifdef HAVE_MULTIDRIVE
1455 (void)drive; /* unused for now */
1456 #endif
1457 int i;
1458 info->sector_size = SECTOR_SIZE;
1459 info->num_sectors = total_sectors;
1461 src = (unsigned short*)&identify_info[27];
1462 dest = (unsigned short*)vendor;
1463 for (i=0;i<4;i++)
1464 dest[i] = htobe16(src[i]);
1465 info->vendor=vendor;
1467 src = (unsigned short*)&identify_info[31];
1468 dest = (unsigned short*)product;
1469 for (i=0;i<8;i++)
1470 dest[i] = htobe16(src[i]);
1471 info->product=product;
1473 src = (unsigned short*)&identify_info[23];
1474 dest = (unsigned short*)revision;
1475 for (i=0;i<2;i++)
1476 dest[i] = htobe16(src[i]);
1477 info->revision=revision;
1479 #endif
1481 #ifdef HAVE_ATA_DMA
1482 /* Returns last DMA mode as set by set_features() */
1483 int ata_get_dma_mode(void)
1485 return dma_mode;
1488 /* Needed to allow updating while waiting for DMA to complete */
1489 void ata_keep_active(void)
1491 last_disk_activity = current_tick;
1493 #endif
1495 #ifdef CONFIG_STORAGE_MULTI
1496 int ata_num_drives(int first_drive)
1498 /* We don't care which logical drive number(s) we have been assigned */
1499 (void)first_drive;
1501 return 1;
1503 #endif