Commit FS#12096 by Stephan Grossklass. Increases the maximum ID3v2 field length...
[kugel-rb.git] / firmware / drivers / ata.c
blobbaef6691d42f69d9812095f4099cc8938da26eee
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 = 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 static bool poweroff = false;
165 static long sleep_timeout = 5*HZ;
166 #ifdef HAVE_LBA48
167 static bool lba48 = false; /* set for 48 bit addressing */
168 #endif
169 static long ata_stack[(DEFAULT_STACK_SIZE*3)/sizeof(long)];
170 static const char ata_thread_name[] = "ata";
171 static struct event_queue ata_queue SHAREDBSS_ATTR;
172 static bool initialized = false;
174 static long last_user_activity = -1;
175 static long last_disk_activity = -1;
177 static unsigned long total_sectors;
178 static int multisectors; /* number of supported multisectors */
179 static unsigned short identify_info[SECTOR_SIZE/2];
181 #ifdef MAX_PHYS_SECTOR_SIZE
183 struct sector_cache_entry {
184 bool inuse;
185 unsigned long sectornum; /* logical sector */
186 unsigned char data[MAX_PHYS_SECTOR_SIZE];
188 /* buffer for reading and writing large physical sectors */
189 #define NUMCACHES 2
190 static struct sector_cache_entry sector_cache;
191 static int phys_sector_mult = 1;
192 #endif
194 #ifdef HAVE_ATA_DMA
195 static int dma_mode = 0;
196 #endif
198 static int ata_power_on(void);
199 static int perform_soft_reset(void);
200 static int set_multiple_mode(int sectors);
201 static int set_features(void);
203 #ifndef ATA_TARGET_POLLING
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 */
238 #else
239 extern int ata_wait_for_bsy(void);
240 extern int ata_wait_for_rdy(void);
241 #define wait_for_bsy ata_wait_for_bsy
242 #define wait_for_rdy ata_wait_for_rdy
243 #endif
245 STATICIRAM ICODE_ATTR int wait_for_start_of_transfer(void)
247 if (!wait_for_bsy())
248 return 0;
250 return (ATA_IN8(ATA_ALT_STATUS) & (STATUS_BSY|STATUS_DRQ)) == STATUS_DRQ;
253 STATICIRAM ICODE_ATTR int wait_for_end_of_transfer(void)
255 if (!wait_for_bsy())
256 return 0;
257 return (ATA_IN8(ATA_ALT_STATUS) &
258 (STATUS_BSY|STATUS_RDY|STATUS_DF|STATUS_DRQ|STATUS_ERR))
259 == STATUS_RDY;
262 #if (CONFIG_LED == LED_REAL)
263 /* Conditionally block LED access for the ATA driver, so the LED can be
264 * (mis)used for other purposes */
265 static void ata_led(bool on)
267 ata_led_on = on;
268 if (ata_led_enabled)
269 led(ata_led_on);
271 #else
272 #define ata_led(on) led(on)
273 #endif
275 #ifndef ATA_OPTIMIZED_READING
276 STATICIRAM ICODE_ATTR void copy_read_sectors(unsigned char* buf, int wordcount)
278 unsigned short tmp = 0;
280 if ( (unsigned long)buf & 1)
281 { /* not 16-bit aligned, copy byte by byte */
282 unsigned char* bufend = buf + wordcount*2;
285 tmp = ATA_IN16(ATA_DATA);
286 #if defined(ROCKBOX_LITTLE_ENDIAN)
287 *buf++ = tmp & 0xff; /* I assume big endian */
288 *buf++ = tmp >> 8; /* and don't use the SWAB16 macro */
289 #else
290 *buf++ = tmp >> 8;
291 *buf++ = tmp & 0xff;
292 #endif
293 } while (buf < bufend); /* tail loop is faster */
295 else
296 { /* 16-bit aligned, can do faster copy */
297 unsigned short* wbuf = (unsigned short*)buf;
298 unsigned short* wbufend = wbuf + wordcount;
301 *wbuf = ATA_IN16(ATA_DATA);
302 } while (++wbuf < wbufend); /* tail loop is faster */
305 #endif /* !ATA_OPTIMIZED_READING */
307 #ifndef ATA_OPTIMIZED_WRITING
308 STATICIRAM ICODE_ATTR void copy_write_sectors(const unsigned char* buf,
309 int wordcount)
311 if ( (unsigned long)buf & 1)
312 { /* not 16-bit aligned, copy byte by byte */
313 unsigned short tmp = 0;
314 const unsigned char* bufend = buf + wordcount*2;
317 #if defined(ROCKBOX_LITTLE_ENDIAN)
318 tmp = (unsigned short) *buf++;
319 tmp |= (unsigned short) *buf++ << 8;
320 #else
321 tmp = (unsigned short) *buf++ << 8;
322 tmp |= (unsigned short) *buf++;
323 #endif
324 ATA_OUT16(ATA_DATA, tmp);
325 } while (buf < bufend); /* tail loop is faster */
327 else
328 { /* 16-bit aligned, can do faster copy */
329 unsigned short* wbuf = (unsigned short*)buf;
330 unsigned short* wbufend = wbuf + wordcount;
333 ATA_OUT16(ATA_DATA, *wbuf);
334 } while (++wbuf < wbufend); /* tail loop is faster */
337 #endif /* !ATA_OPTIMIZED_WRITING */
339 static int ata_transfer_sectors(unsigned long start,
340 int incount,
341 void* inbuf,
342 int write)
344 int ret = 0;
345 long timeout;
346 int count;
347 void* buf;
348 long spinup_start;
349 #ifdef HAVE_ATA_DMA
350 bool usedma = false;
351 #endif
353 #ifndef MAX_PHYS_SECTOR_SIZE
354 mutex_lock(&ata_mtx);
355 #endif
357 if (start + incount > total_sectors) {
358 ret = -1;
359 goto error;
362 last_disk_activity = current_tick;
363 spinup_start = current_tick;
365 ata_led(true);
367 if ( sleeping ) {
368 sleeping = false; /* set this now since it'll be on */
369 spinup = true;
370 if (poweroff) {
371 if (ata_power_on()) {
372 ret = -2;
373 goto error;
376 else {
377 if (perform_soft_reset()) {
378 ret = -2;
379 goto error;
384 timeout = current_tick + READWRITE_TIMEOUT;
386 ATA_OUT8(ATA_SELECT, ata_device);
387 if (!wait_for_rdy())
389 ret = -3;
390 goto error;
393 retry:
394 buf = inbuf;
395 count = incount;
396 while (TIME_BEFORE(current_tick, timeout)) {
397 ret = 0;
398 last_disk_activity = current_tick;
400 #ifdef HAVE_ATA_DMA
401 /* If DMA is supported and parameters are ok for DMA, use it */
402 if (dma_mode && ata_dma_setup(inbuf, incount * SECTOR_SIZE, write))
403 usedma = true;
404 #endif
406 #ifdef HAVE_LBA48
407 if (lba48)
409 ATA_OUT8(ATA_NSECTOR, count >> 8);
410 ATA_OUT8(ATA_NSECTOR, count & 0xff);
411 ATA_OUT8(ATA_SECTOR, (start >> 24) & 0xff); /* 31:24 */
412 ATA_OUT8(ATA_SECTOR, start & 0xff); /* 7:0 */
413 ATA_OUT8(ATA_LCYL, 0); /* 39:32 */
414 ATA_OUT8(ATA_LCYL, (start >> 8) & 0xff); /* 15:8 */
415 ATA_OUT8(ATA_HCYL, 0); /* 47:40 */
416 ATA_OUT8(ATA_HCYL, (start >> 16) & 0xff); /* 23:16 */
417 ATA_OUT8(ATA_SELECT, SELECT_LBA | ata_device);
418 #ifdef HAVE_ATA_DMA
419 if (write)
420 ATA_OUT8(ATA_COMMAND, usedma ? CMD_WRITE_DMA_EXT : CMD_WRITE_MULTIPLE_EXT);
421 else
422 ATA_OUT8(ATA_COMMAND, usedma ? CMD_READ_DMA_EXT : CMD_READ_MULTIPLE_EXT);
423 #else
424 ATA_OUT8(ATA_COMMAND, write ? CMD_WRITE_MULTIPLE_EXT : CMD_READ_MULTIPLE_EXT);
425 #endif
427 else
428 #endif
430 ATA_OUT8(ATA_NSECTOR, count & 0xff); /* 0 means 256 sectors */
431 ATA_OUT8(ATA_SECTOR, start & 0xff);
432 ATA_OUT8(ATA_LCYL, (start >> 8) & 0xff);
433 ATA_OUT8(ATA_HCYL, (start >> 16) & 0xff);
434 ATA_OUT8(ATA_SELECT, ((start >> 24) & 0xf) | SELECT_LBA | ata_device);
435 #ifdef HAVE_ATA_DMA
436 if (write)
437 ATA_OUT8(ATA_COMMAND, usedma ? CMD_WRITE_DMA : CMD_WRITE_MULTIPLE);
438 else
439 ATA_OUT8(ATA_COMMAND, usedma ? CMD_READ_DMA : CMD_READ_MULTIPLE);
440 #else
441 ATA_OUT8(ATA_COMMAND, write ? CMD_WRITE_MULTIPLE : CMD_READ_MULTIPLE);
442 #endif
445 /* wait at least 400ns between writing command and reading status */
446 __asm__ volatile ("nop");
447 __asm__ volatile ("nop");
448 __asm__ volatile ("nop");
449 __asm__ volatile ("nop");
450 __asm__ volatile ("nop");
452 #ifdef HAVE_ATA_DMA
453 if (usedma) {
454 if (!ata_dma_finish())
455 ret = -7;
457 if (ret != 0) {
458 perform_soft_reset();
459 goto retry;
462 if (spinup) {
463 spinup_time = current_tick - spinup_start;
464 spinup = false;
465 poweroff = false;
468 else
469 #endif /* HAVE_ATA_DMA */
471 while (count) {
472 int sectors;
473 int wordcount;
474 int status;
475 int error;
477 if (!wait_for_start_of_transfer()) {
478 /* We have timed out waiting for RDY and/or DRQ, possibly
479 because the hard drive is shaking and has problems
480 reading the data. We have two options:
481 1) Wait some more
482 2) Perform a soft reset and try again.
484 We choose alternative 2.
486 perform_soft_reset();
487 ret = -5;
488 goto retry;
491 if (spinup) {
492 spinup_time = current_tick - spinup_start;
493 spinup = false;
494 poweroff = false;
497 /* read the status register exactly once per loop */
498 status = ATA_IN8(ATA_STATUS);
499 error = ATA_IN8(ATA_ERROR);
501 if (count >= multisectors)
502 sectors = multisectors;
503 else
504 sectors = count;
506 wordcount = sectors * SECTOR_SIZE / 2;
508 if (write)
509 copy_write_sectors(buf, wordcount);
510 else
511 copy_read_sectors(buf, wordcount);
514 "Device errors encountered during READ MULTIPLE commands
515 are posted at the beginning of the block or partial block
516 transfer, but the DRQ bit is still set to one and the data
517 transfer shall take place, including transfer of corrupted
518 data, if any."
519 -- ATA specification
521 if ( status & (STATUS_BSY | STATUS_ERR | STATUS_DF) ) {
522 perform_soft_reset();
523 ret = -6;
524 /* no point retrying IDNF, sector no. was invalid */
525 if (error & ERROR_IDNF)
526 break;
527 goto retry;
530 buf += sectors * SECTOR_SIZE; /* Advance one chunk of sectors */
531 count -= sectors;
533 last_disk_activity = current_tick;
537 if(!ret && !wait_for_end_of_transfer()) {
538 int error;
540 error = ATA_IN8(ATA_ERROR);
541 perform_soft_reset();
542 ret = -4;
543 /* no point retrying IDNF, sector no. was invalid */
544 if (error & ERROR_IDNF)
545 break;
546 goto retry;
548 break;
551 error:
552 ata_led(false);
553 #ifndef MAX_PHYS_SECTOR_SIZE
554 mutex_unlock(&ata_mtx);
555 #endif
557 return ret;
560 #ifndef MAX_PHYS_SECTOR_SIZE
561 int ata_read_sectors(IF_MD2(int drive,)
562 unsigned long start,
563 int incount,
564 void* inbuf)
566 #ifdef HAVE_MULTIDRIVE
567 (void)drive; /* unused for now */
568 #endif
570 return ata_transfer_sectors(start, incount, inbuf, false);
572 #endif
574 #ifndef MAX_PHYS_SECTOR_SIZE
575 int ata_write_sectors(IF_MD2(int drive,)
576 unsigned long start,
577 int count,
578 const void* buf)
580 #ifdef HAVE_MULTIDRIVE
581 (void)drive; /* unused for now */
582 #endif
584 return ata_transfer_sectors(start, count, (void*)buf, true);
586 #endif
588 #ifdef MAX_PHYS_SECTOR_SIZE
589 static int cache_sector(unsigned long sector)
591 int rc;
593 sector &= ~(phys_sector_mult - 1);
594 /* round down to physical sector boundary */
596 /* check whether the sector is already cached */
597 if (sector_cache.inuse && (sector_cache.sectornum == sector))
598 return 0;
600 /* not found: read the sector */
601 sector_cache.inuse = false;
602 rc = ata_transfer_sectors(sector, phys_sector_mult, sector_cache.data, false);
603 if (!rc)
605 sector_cache.sectornum = sector;
606 sector_cache.inuse = true;
608 return rc;
611 static inline int flush_current_sector(void)
613 return ata_transfer_sectors(sector_cache.sectornum, phys_sector_mult,
614 sector_cache.data, true);
617 int ata_read_sectors(IF_MD2(int drive,)
618 unsigned long start,
619 int incount,
620 void* inbuf)
622 int rc = 0;
623 int offset;
625 #ifdef HAVE_MULTIDRIVE
626 (void)drive; /* unused for now */
627 #endif
628 mutex_lock(&ata_mtx);
630 offset = start & (phys_sector_mult - 1);
632 if (offset) /* first partial sector */
634 int partcount = MIN(incount, phys_sector_mult - offset);
636 rc = cache_sector(start);
637 if (rc)
639 rc = rc * 10 - 1;
640 goto error;
642 memcpy(inbuf, sector_cache.data + offset * SECTOR_SIZE,
643 partcount * SECTOR_SIZE);
645 start += partcount;
646 inbuf += partcount * SECTOR_SIZE;
647 incount -= partcount;
649 if (incount)
651 offset = incount & (phys_sector_mult - 1);
652 incount -= offset;
654 if (incount)
656 rc = ata_transfer_sectors(start, incount, inbuf, false);
657 if (rc)
659 rc = rc * 10 - 2;
660 goto error;
662 start += incount;
663 inbuf += incount * SECTOR_SIZE;
665 if (offset)
667 rc = cache_sector(start);
668 if (rc)
670 rc = rc * 10 - 3;
671 goto error;
673 memcpy(inbuf, sector_cache.data, offset * SECTOR_SIZE);
677 error:
678 mutex_unlock(&ata_mtx);
680 return rc;
683 int ata_write_sectors(IF_MD2(int drive,)
684 unsigned long start,
685 int count,
686 const void* buf)
688 int rc = 0;
689 int offset;
691 #ifdef HAVE_MULTIDRIVE
692 (void)drive; /* unused for now */
693 #endif
694 mutex_lock(&ata_mtx);
696 offset = start & (phys_sector_mult - 1);
698 if (offset) /* first partial sector */
700 int partcount = MIN(count, phys_sector_mult - offset);
702 rc = cache_sector(start);
703 if (rc)
705 rc = rc * 10 - 1;
706 goto error;
708 memcpy(sector_cache.data + offset * SECTOR_SIZE, buf,
709 partcount * SECTOR_SIZE);
710 rc = flush_current_sector();
711 if (rc)
713 rc = rc * 10 - 2;
714 goto error;
716 start += partcount;
717 buf += partcount * SECTOR_SIZE;
718 count -= partcount;
720 if (count)
722 offset = count & (phys_sector_mult - 1);
723 count -= offset;
725 if (count)
727 rc = ata_transfer_sectors(start, count, (void*)buf, true);
728 if (rc)
730 rc = rc * 10 - 3;
731 goto error;
733 start += count;
734 buf += count * SECTOR_SIZE;
736 if (offset)
738 rc = cache_sector(start);
739 if (rc)
741 rc = rc * 10 - 4;
742 goto error;
744 memcpy(sector_cache.data, buf, offset * SECTOR_SIZE);
745 rc = flush_current_sector();
746 if (rc)
748 rc = rc * 10 - 5;
749 goto error;
754 error:
755 mutex_unlock(&ata_mtx);
757 return rc;
759 #endif /* MAX_PHYS_SECTOR_SIZE */
761 static int check_registers(void)
763 int i;
764 wait_for_bsy();
765 if (ATA_IN8(ATA_STATUS) & STATUS_BSY)
766 return -1;
768 for (i = 0; i<64; i++) {
769 ATA_OUT8(ATA_NSECTOR, TEST_PATTERN1);
770 ATA_OUT8(ATA_SECTOR, TEST_PATTERN2);
771 ATA_OUT8(ATA_LCYL, TEST_PATTERN3);
772 ATA_OUT8(ATA_HCYL, TEST_PATTERN4);
774 if (((ATA_IN8(ATA_NSECTOR) & 0xff) == TEST_PATTERN1) &&
775 ((ATA_IN8(ATA_SECTOR) & 0xff) == TEST_PATTERN2) &&
776 ((ATA_IN8(ATA_LCYL) & 0xff) == TEST_PATTERN3) &&
777 ((ATA_IN8(ATA_HCYL) & 0xff) == TEST_PATTERN4))
778 return 0;
780 sleep(1);
782 return -2;
785 static int freeze_lock(void)
787 /* does the disk support Security Mode feature set? */
788 if (identify_info[82] & 2)
790 ATA_OUT8(ATA_SELECT, ata_device);
792 if (!wait_for_rdy())
793 return -1;
795 ATA_OUT8(ATA_COMMAND, CMD_SECURITY_FREEZE_LOCK);
797 if (!wait_for_rdy())
798 return -2;
801 return 0;
804 void ata_spindown(int seconds)
806 sleep_timeout = seconds * HZ;
809 bool ata_disk_is_active(void)
811 return !sleeping;
814 static int ata_perform_sleep(void)
816 /* guard against calls made with checks of these variables outside
817 the mutex that may not be on the ata thread; status may have changed. */
818 if (spinup || sleeping) {
819 return 0;
822 ATA_OUT8(ATA_SELECT, ata_device);
824 if(!wait_for_rdy()) {
825 DEBUGF("ata_perform_sleep() - not RDY\n");
826 return -1;
829 ATA_OUT8(ATA_COMMAND, CMD_SLEEP);
831 if (!wait_for_rdy())
833 DEBUGF("ata_perform_sleep() - CMD failed\n");
834 return -2;
837 sleeping = true;
838 return 0;
841 void ata_sleep(void)
843 queue_post(&ata_queue, Q_SLEEP, 0);
846 void ata_sleepnow(void)
848 if (!spinup && !sleeping && initialized)
850 call_storage_idle_notifys(false);
851 mutex_lock(&ata_mtx);
852 ata_perform_sleep();
853 mutex_unlock(&ata_mtx);
857 void ata_spin(void)
859 last_user_activity = current_tick;
862 static void ata_thread(void)
864 static long last_sleep = 0;
865 struct queue_event ev;
866 #ifdef ALLOW_USB_SPINDOWN
867 static bool usb_mode = false;
868 #endif
870 while (1) {
871 queue_wait_w_tmo(&ata_queue, &ev, HZ/2);
873 switch ( ev.id ) {
874 case SYS_TIMEOUT:
875 if (!spinup && !sleeping)
877 if (TIME_AFTER( current_tick,
878 last_disk_activity + (HZ*2) ) )
880 #ifdef ALLOW_USB_SPINDOWN
881 if(!usb_mode)
882 #endif
884 call_storage_idle_notifys(false);
888 if ( sleep_timeout &&
889 TIME_AFTER( current_tick,
890 last_user_activity + sleep_timeout ) &&
891 TIME_AFTER( current_tick,
892 last_disk_activity + sleep_timeout ) )
894 #ifdef ALLOW_USB_SPINDOWN
895 if(!usb_mode)
896 #endif
898 call_storage_idle_notifys(true);
900 mutex_lock(&ata_mtx);
901 ata_perform_sleep();
902 last_sleep = current_tick;
903 mutex_unlock(&ata_mtx);
907 #ifdef HAVE_ATA_POWER_OFF
908 if ( !spinup && sleeping && !poweroff &&
909 TIME_AFTER( current_tick, last_sleep + ATA_POWER_OFF_TIMEOUT ))
911 mutex_lock(&ata_mtx);
912 ide_power_enable(false);
913 poweroff = true;
914 mutex_unlock(&ata_mtx);
916 #endif
917 break;
919 #ifndef USB_NONE
920 case SYS_USB_CONNECTED:
921 /* Tell the USB thread that we are safe */
922 DEBUGF("ata_thread got SYS_USB_CONNECTED\n");
923 #ifdef ALLOW_USB_SPINDOWN
924 usb_mode = true;
925 usb_acknowledge(SYS_USB_CONNECTED_ACK);
926 /* There is no need to force ATA power on */
927 #else
928 mutex_lock(&ata_mtx);
929 if (sleeping) {
930 ata_led(true);
931 sleeping = false; /* set this now since it'll be on */
933 if (poweroff) {
934 ata_power_on();
935 poweroff = false;
937 else {
938 perform_soft_reset();
941 ata_led(false);
943 mutex_unlock(&ata_mtx);
945 /* Wait until the USB cable is extracted again */
946 usb_acknowledge(SYS_USB_CONNECTED_ACK);
947 usb_wait_for_disconnect(&ata_queue);
948 #endif
949 break;
951 #ifdef ALLOW_USB_SPINDOWN
952 case SYS_USB_DISCONNECTED:
953 /* Tell the USB thread that we are ready again */
954 DEBUGF("ata_thread got SYS_USB_DISCONNECTED\n");
955 usb_mode = false;
956 break;
957 #endif
958 #endif /* USB_NONE */
960 case Q_SLEEP:
961 #ifdef ALLOW_USB_SPINDOWN
962 if(!usb_mode)
963 #endif
965 call_storage_idle_notifys(false);
967 last_disk_activity = current_tick - sleep_timeout + (HZ/2);
968 break;
970 #ifdef ATA_DRIVER_CLOSE
971 case Q_CLOSE:
972 return;
973 #endif
978 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
979 static int ata_hard_reset(void)
981 int ret;
983 mutex_lock(&ata_mtx);
985 ata_reset();
987 /* state HRR2 */
988 ATA_OUT8(ATA_SELECT, ata_device); /* select the right device */
989 ret = wait_for_bsy();
991 /* Massage the return code so it is 0 on success and -1 on failure */
992 ret = ret?0:-1;
994 mutex_unlock(&ata_mtx);
996 return ret;
999 static int perform_soft_reset(void)
1001 /* If this code is allowed to run on a Nano, the next reads from the flash will
1002 * time out, so we disable it. It shouldn't be necessary anyway, since the
1003 * ATA -> Flash interface automatically sleeps almost immediately after the
1004 * last command.
1006 int ret;
1007 int retry_count;
1009 ATA_OUT8(ATA_SELECT, SELECT_LBA | ata_device );
1010 ATA_OUT8(ATA_CONTROL, CONTROL_nIEN|CONTROL_SRST );
1011 sleep(1); /* >= 5us */
1013 #ifdef HAVE_ATA_DMA
1014 /* DMA requires INTRQ be enabled */
1015 ATA_OUT8(ATA_CONTROL, 0);
1016 #else
1017 ATA_OUT8(ATA_CONTROL, CONTROL_nIEN);
1018 #endif
1019 sleep(1); /* >2ms */
1021 /* This little sucker can take up to 30 seconds */
1022 retry_count = 8;
1025 ret = wait_for_rdy();
1026 } while(!ret && retry_count--);
1028 if (!ret)
1029 return -1;
1031 if (set_features())
1032 return -2;
1034 if (set_multiple_mode(multisectors))
1035 return -3;
1037 if (freeze_lock())
1038 return -4;
1040 return 0;
1043 int ata_soft_reset(void)
1045 int ret;
1047 mutex_lock(&ata_mtx);
1049 ret = perform_soft_reset();
1051 mutex_unlock(&ata_mtx);
1052 return ret;
1055 static int ata_power_on(void)
1057 int rc;
1059 ide_power_enable(true);
1060 sleep(HZ/4); /* allow voltage to build up */
1062 /* Accessing the PP IDE controller too early after powering up the disk
1063 * makes the core hang for a short time, causing an audio dropout. This
1064 * also depends on the disk; iPod Mini G2 needs at least HZ/5 to get rid
1065 * of the dropout. Since this time isn't additive (the wait_for_bsy() in
1066 * ata_hard_reset() will shortened by the same amount), it's a good idea
1067 * to do this on all HDD based targets. */
1069 if( ata_hard_reset() )
1070 return -1;
1072 rc = set_features();
1073 if (rc)
1074 return rc * 10 - 2;
1076 if (set_multiple_mode(multisectors))
1077 return -3;
1079 if (freeze_lock())
1080 return -4;
1082 return 0;
1085 static int master_slave_detect(void)
1087 /* master? */
1088 ATA_OUT8(ATA_SELECT, 0);
1089 if (ATA_IN8(ATA_STATUS) & (STATUS_RDY|STATUS_BSY)) {
1090 ata_device = 0;
1091 DEBUGF("Found master harddisk\n");
1093 else {
1094 /* slave? */
1095 ATA_OUT8(ATA_SELECT, SELECT_DEVICE1);
1096 if (ATA_IN8(ATA_STATUS) & (STATUS_RDY|STATUS_BSY)) {
1097 ata_device = SELECT_DEVICE1;
1098 DEBUGF("Found slave harddisk\n");
1100 else
1101 return -1;
1103 return 0;
1106 static int identify(void)
1108 int i;
1110 ATA_OUT8(ATA_SELECT, ata_device);
1112 if(!wait_for_rdy()) {
1113 DEBUGF("identify() - not RDY\n");
1114 return -1;
1116 ATA_OUT8(ATA_COMMAND, CMD_IDENTIFY);
1118 if (!wait_for_start_of_transfer())
1120 DEBUGF("identify() - CMD failed\n");
1121 return -2;
1124 for (i=0; i<SECTOR_SIZE/2; i++) {
1125 /* the IDENTIFY words are already swapped, so we need to treat
1126 this info differently that normal sector data */
1127 identify_info[i] = ATA_SWAP_IDENTIFY(ATA_IN16(ATA_DATA));
1130 return 0;
1133 static int set_multiple_mode(int sectors)
1135 ATA_OUT8(ATA_SELECT, ata_device);
1137 if(!wait_for_rdy()) {
1138 DEBUGF("set_multiple_mode() - not RDY\n");
1139 return -1;
1142 ATA_OUT8(ATA_NSECTOR, sectors);
1143 ATA_OUT8(ATA_COMMAND, CMD_SET_MULTIPLE_MODE);
1145 if (!wait_for_rdy())
1147 DEBUGF("set_multiple_mode() - CMD failed\n");
1148 return -2;
1151 return 0;
1154 #ifdef HAVE_ATA_DMA
1155 static int get_best_mode(unsigned short identword, int max, int modetype)
1157 unsigned short testbit = BIT_N(max);
1159 while (1) {
1160 if (identword & testbit)
1161 return max | modetype;
1162 testbit >>= 1;
1163 if (!testbit)
1164 return 0;
1165 max--;
1168 #endif
1170 static int set_features(void)
1172 static struct {
1173 unsigned char id_word;
1174 unsigned char id_bit;
1175 unsigned char subcommand;
1176 unsigned char parameter;
1177 } features[] = {
1178 { 83, 14, 0x03, 0 }, /* force PIO mode */
1179 { 83, 3, 0x05, 0x80 }, /* adv. power management: lowest w/o standby */
1180 { 83, 9, 0x42, 0x80 }, /* acoustic management: lowest noise */
1181 { 82, 6, 0xaa, 0 }, /* enable read look-ahead */
1182 #ifdef HAVE_ATA_DMA
1183 { 0, 0, 0x03, 0 }, /* DMA mode */
1184 #endif
1186 int i;
1187 int pio_mode = 2;
1189 /* Find out the highest supported PIO mode */
1190 if(identify_info[64] & 2)
1191 pio_mode = 4;
1192 else
1193 if(identify_info[64] & 1)
1194 pio_mode = 3;
1196 /* Update the table: set highest supported pio mode that we also support */
1197 features[0].parameter = 8 + pio_mode;
1199 #ifdef HAVE_ATA_DMA
1200 if (identify_info[53] & (1<<2))
1201 /* Ultra DMA mode info present, find a mode */
1202 dma_mode = get_best_mode(identify_info[88], ATA_MAX_UDMA, 0x40);
1204 if (!dma_mode) {
1205 /* No UDMA mode found, try to find a multi-word DMA mode */
1206 dma_mode = get_best_mode(identify_info[63], ATA_MAX_MWDMA, 0x20);
1207 features[4].id_word = 63;
1209 else
1210 features[4].id_word = 88;
1212 features[4].id_bit = dma_mode & 7;
1213 features[4].parameter = dma_mode;
1214 #endif /* HAVE_ATA_DMA */
1216 ATA_OUT8(ATA_SELECT, ata_device);
1218 if (!wait_for_rdy()) {
1219 DEBUGF("set_features() - not RDY\n");
1220 return -1;
1223 for (i=0; i < (int)(sizeof(features)/sizeof(features[0])); i++) {
1224 if (identify_info[features[i].id_word] & BIT_N(features[i].id_bit)) {
1225 ATA_OUT8(ATA_FEATURE, features[i].subcommand);
1226 ATA_OUT8(ATA_NSECTOR, features[i].parameter);
1227 ATA_OUT8(ATA_COMMAND, CMD_SET_FEATURES);
1229 if (!wait_for_rdy()) {
1230 DEBUGF("set_features() - CMD failed\n");
1231 return -10 - i;
1234 if((ATA_IN8(ATA_ALT_STATUS) & STATUS_ERR) && (i != 1)) {
1235 /* some CF cards don't like advanced powermanagement
1236 even if they mark it as supported - go figure... */
1237 if(ATA_IN8(ATA_ERROR) & ERROR_ABRT) {
1238 return -20 - i;
1244 #ifdef ATA_SET_DEVICE_FEATURES
1245 ata_set_pio_timings(pio_mode);
1246 #endif
1248 #ifdef HAVE_ATA_DMA
1249 ata_dma_set_mode(dma_mode);
1250 #endif
1252 return 0;
1255 unsigned short* ata_get_identify(void)
1257 return identify_info;
1260 static int init_and_check(bool hard_reset)
1262 int rc;
1264 if (hard_reset)
1266 /* This should reset both master and slave, we don't yet know what's in */
1267 ata_device = 0;
1268 if (ata_hard_reset())
1269 return -1;
1272 rc = master_slave_detect();
1273 if (rc)
1274 return -10 + rc;
1276 /* symptom fix: else check_registers() below may fail */
1277 if (hard_reset && !wait_for_bsy())
1278 return -20;
1280 rc = check_registers();
1281 if (rc)
1282 return -30 + rc;
1284 return 0;
1287 int ata_init(void)
1289 int rc = 0;
1290 bool coldstart;
1292 if ( !initialized ) {
1293 mutex_init(&ata_mtx);
1294 queue_init(&ata_queue, true);
1297 mutex_lock(&ata_mtx);
1299 /* must be called before ata_device_init() */
1300 coldstart = ata_is_coldstart();
1301 ata_led(false);
1302 ata_device_init();
1303 sleeping = false;
1304 ata_enable(true);
1305 #ifdef MAX_PHYS_SECTOR_SIZE
1306 memset(&sector_cache, 0, sizeof(sector_cache));
1307 #endif
1309 if ( !initialized ) {
1310 /* First call won't have multiple thread contention - this
1311 * may return at any point without having to unlock */
1312 mutex_unlock(&ata_mtx);
1314 if (!ide_powered()) /* somebody has switched it off */
1316 ide_power_enable(true);
1317 sleep(HZ/4); /* allow voltage to build up */
1320 #ifdef HAVE_ATA_DMA
1321 /* DMA requires INTRQ be enabled */
1322 ATA_OUT8(ATA_CONTROL, 0);
1323 #endif
1325 /* first try, hard reset at cold start only */
1326 rc = init_and_check(coldstart);
1328 if (rc)
1329 { /* failed? -> second try, always with hard reset */
1330 DEBUGF("ata: init failed, retrying...\n");
1331 rc = init_and_check(true);
1332 if (rc)
1333 return rc;
1336 rc = identify();
1338 if (rc)
1339 return -40 + rc;
1341 multisectors = identify_info[47] & 0xff;
1342 if (multisectors == 0) /* Invalid multisector info, try with 16 */
1343 multisectors = 16;
1345 DEBUGF("ata: %d sectors per ata request\n",multisectors);
1347 total_sectors = identify_info[60] | (identify_info[61] << 16);
1349 #ifdef HAVE_LBA48
1350 if (identify_info[83] & 0x0400 /* 48 bit address support */
1351 && total_sectors == 0x0FFFFFFF) /* and disk size >= 128 GiB */
1352 { /* (needs BigLBA addressing) */
1353 if (identify_info[102] || identify_info[103])
1354 panicf("Unsupported disk size: >= 2^32 sectors");
1356 total_sectors = identify_info[100] | (identify_info[101] << 16);
1357 lba48 = true; /* use BigLBA */
1359 #endif
1360 rc = freeze_lock();
1362 if (rc)
1363 return -50 + rc;
1365 rc = set_features();
1366 if (rc)
1367 return -60 + rc;
1369 #ifdef MAX_PHYS_SECTOR_SIZE
1370 /* Find out the physical sector size */
1371 if((identify_info[106] & 0xe000) == 0x6000)
1372 phys_sector_mult = BIT_N(identify_info[106] & 0x000f);
1373 else
1374 phys_sector_mult = 1;
1376 DEBUGF("ata: %d logical sectors per phys sector", phys_sector_mult);
1378 if (phys_sector_mult > 1)
1380 /* Check if drive really needs emulation - if we can access
1381 * sector 1 then assume the drive will handle it better than
1382 * us, and ignore the large physical sectors.
1384 char throwaway[SECTOR_SIZE];
1385 rc = ata_transfer_sectors(1, 1, &throwaway, false);
1386 if (rc == 0)
1387 phys_sector_mult = 1;
1390 if (phys_sector_mult > (MAX_PHYS_SECTOR_SIZE/SECTOR_SIZE))
1391 panicf("Unsupported physical sector size: %d",
1392 phys_sector_mult * SECTOR_SIZE);
1393 #endif
1395 mutex_lock(&ata_mtx); /* Balance unlock below */
1397 last_disk_activity = current_tick;
1398 #ifdef ATA_DRIVER_CLOSE
1399 ata_thread_id =
1400 #endif
1401 create_thread(ata_thread, ata_stack,
1402 sizeof(ata_stack), 0, ata_thread_name
1403 IF_PRIO(, PRIORITY_USER_INTERFACE)
1404 IF_COP(, CPU));
1405 initialized = true;
1408 rc = set_multiple_mode(multisectors);
1409 if (rc)
1410 rc = -70 + rc;
1412 mutex_unlock(&ata_mtx);
1413 return rc;
1416 #ifdef ATA_DRIVER_CLOSE
1417 void ata_close(void)
1419 unsigned int thread_id = ata_thread_id;
1421 if (thread_id == 0)
1422 return;
1424 ata_thread_id = 0;
1426 queue_post(&ata_queue, Q_CLOSE, 0);
1427 thread_wait(thread_id);
1429 #endif /* ATA_DRIVER_CLOSE */
1431 #if (CONFIG_LED == LED_REAL)
1432 void ata_set_led_enabled(bool enabled)
1434 ata_led_enabled = enabled;
1435 if (ata_led_enabled)
1436 led(ata_led_on);
1437 else
1438 led(false);
1440 #endif
1442 long ata_last_disk_activity(void)
1444 return last_disk_activity;
1447 int ata_spinup_time(void)
1449 return spinup_time;
1452 #ifdef STORAGE_GET_INFO
1453 void ata_get_info(IF_MD2(int drive,)struct storage_info *info)
1455 unsigned short *src,*dest;
1456 static char vendor[8];
1457 static char product[16];
1458 static char revision[4];
1459 #ifdef HAVE_MULTIDRIVE
1460 (void)drive; /* unused for now */
1461 #endif
1462 int i;
1463 info->sector_size = SECTOR_SIZE;
1464 info->num_sectors = total_sectors;
1466 src = (unsigned short*)&identify_info[27];
1467 dest = (unsigned short*)vendor;
1468 for (i=0;i<4;i++)
1469 dest[i] = htobe16(src[i]);
1470 info->vendor=vendor;
1472 src = (unsigned short*)&identify_info[31];
1473 dest = (unsigned short*)product;
1474 for (i=0;i<8;i++)
1475 dest[i] = htobe16(src[i]);
1476 info->product=product;
1478 src = (unsigned short*)&identify_info[23];
1479 dest = (unsigned short*)revision;
1480 for (i=0;i<2;i++)
1481 dest[i] = htobe16(src[i]);
1482 info->revision=revision;
1484 #endif
1486 #ifdef HAVE_ATA_DMA
1487 /* Returns last DMA mode as set by set_features() */
1488 int ata_get_dma_mode(void)
1490 return dma_mode;
1493 /* Needed to allow updating while waiting for DMA to complete */
1494 void ata_keep_active(void)
1496 last_disk_activity = current_tick;
1498 #endif
1500 #ifdef CONFIG_STORAGE_MULTI
1501 int ata_num_drives(int first_drive)
1503 /* We don't care which logical drive number(s) we have been assigned */
1504 (void)first_drive;
1506 return 1;
1508 #endif