Quickscreen: add more check for case that qs item is not set.
[kugel-rb.git] / firmware / drivers / ata.c
blobddbd6a1edfc0db0f7c7d620c457d5a691b799381
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Alan Korr
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdbool.h>
22 #include "ata.h"
23 #include "kernel.h"
24 #include "thread.h"
25 #include "led.h"
26 #include "cpu.h"
27 #include "system.h"
28 #include "debug.h"
29 #include "panic.h"
30 #include "usb.h"
31 #include "power.h"
32 #include "string.h"
33 #include "ata_idle_notify.h"
34 #include "ata-target.h"
35 #include "storage.h"
37 #define SECTOR_SIZE (512)
39 #define ATA_FEATURE ATA_ERROR
41 #define ATA_STATUS ATA_COMMAND
42 #define ATA_ALT_STATUS ATA_CONTROL
44 #define SELECT_DEVICE1 0x10
45 #define SELECT_LBA 0x40
47 #define CONTROL_nIEN 0x02
48 #define CONTROL_SRST 0x04
50 #define CMD_READ_SECTORS 0x20
51 #define CMD_WRITE_SECTORS 0x30
52 #define CMD_WRITE_SECTORS_EXT 0x34
53 #define CMD_READ_MULTIPLE 0xC4
54 #define CMD_READ_MULTIPLE_EXT 0x29
55 #define CMD_WRITE_MULTIPLE 0xC5
56 #define CMD_WRITE_MULTIPLE_EXT 0x39
57 #define CMD_SET_MULTIPLE_MODE 0xC6
58 #define CMD_STANDBY_IMMEDIATE 0xE0
59 #define CMD_STANDBY 0xE2
60 #define CMD_IDENTIFY 0xEC
61 #define CMD_SLEEP 0xE6
62 #define CMD_SET_FEATURES 0xEF
63 #define CMD_SECURITY_FREEZE_LOCK 0xF5
64 #ifdef HAVE_ATA_DMA
65 #define CMD_READ_DMA 0xC8
66 #define CMD_READ_DMA_EXT 0x25
67 #define CMD_WRITE_DMA 0xCA
68 #define CMD_WRITE_DMA_EXT 0x35
69 #endif
71 /* Should all be < 0x100 (which are reserved for control messages) */
72 #define Q_SLEEP 0
73 #define Q_CLOSE 1
75 #define READWRITE_TIMEOUT 5*HZ
77 #ifdef HAVE_ATA_POWER_OFF
78 #define ATA_POWER_OFF_TIMEOUT 2*HZ
79 #endif
81 #ifdef ATA_DRIVER_CLOSE
82 static unsigned int ata_thread_id = 0;
83 #endif
85 #if defined(MAX_PHYS_SECTOR_SIZE) && MEM == 64
86 /* Hack - what's the deal with 5g? */
87 struct ata_lock
89 struct thread_entry *thread;
90 int count;
91 volatile unsigned char locked;
92 IF_COP( struct corelock cl; )
95 static void ata_lock_init(struct ata_lock *l)
97 corelock_init(&l->cl);
98 l->locked = 0;
99 l->count = 0;
100 l->thread = NULL;
103 static void ata_lock_lock(struct ata_lock *l)
105 struct thread_entry * const current =
106 thread_id_entry(THREAD_ID_CURRENT);
108 if (current == l->thread)
110 l->count++;
111 return;
114 corelock_lock(&l->cl);
116 IF_PRIO( current->skip_count = -1; )
118 while (l->locked != 0)
120 corelock_unlock(&l->cl);
121 switch_thread();
122 corelock_lock(&l->cl);
125 l->locked = 1;
126 l->thread = current;
127 corelock_unlock(&l->cl);
130 static void ata_lock_unlock(struct ata_lock *l)
132 if (l->count > 0)
134 l->count--;
135 return;
138 corelock_lock(&l->cl);
140 IF_PRIO( l->thread->skip_count = 0; )
142 l->thread = NULL;
143 l->locked = 0;
145 corelock_unlock(&l->cl);
148 #define mutex ata_lock
149 #define mutex_init ata_lock_init
150 #define mutex_lock ata_lock_lock
151 #define mutex_unlock ata_lock_unlock
152 #endif /* MAX_PHYS_SECTOR_SIZE */
154 #if defined(HAVE_USBSTACK) && defined(USE_ROCKBOX_USB)
155 #define ALLOW_USB_SPINDOWN
156 #endif
158 static struct mutex ata_mtx SHAREDBSS_ATTR;
159 static int ata_device; /* device 0 (master) or 1 (slave) */
161 static int spinup_time = 0;
162 #if (CONFIG_LED == LED_REAL)
163 static bool ata_led_enabled = true;
164 static bool ata_led_on = false;
165 #endif
166 static bool spinup = false;
167 static bool sleeping = true;
168 static bool poweroff = false;
169 static long sleep_timeout = 5*HZ;
170 #ifdef HAVE_LBA48
171 static bool lba48 = false; /* set for 48 bit addressing */
172 #endif
173 static long ata_stack[(DEFAULT_STACK_SIZE*3)/sizeof(long)];
174 static const char ata_thread_name[] = "ata";
175 static struct event_queue ata_queue SHAREDBSS_ATTR;
176 static bool initialized = false;
178 static long last_user_activity = -1;
179 static long last_disk_activity = -1;
181 static unsigned long total_sectors;
182 static int multisectors; /* number of supported multisectors */
183 static unsigned short identify_info[SECTOR_SIZE/2];
185 #ifdef MAX_PHYS_SECTOR_SIZE
187 struct sector_cache_entry {
188 bool inuse;
189 unsigned long sectornum; /* logical sector */
190 unsigned char data[MAX_PHYS_SECTOR_SIZE];
192 /* buffer for reading and writing large physical sectors */
193 #define NUMCACHES 2
194 static struct sector_cache_entry sector_cache;
195 static int phys_sector_mult = 1;
196 #endif
198 #ifdef HAVE_ATA_DMA
199 static int dma_mode = 0;
200 #endif
202 static int ata_power_on(void);
203 static int perform_soft_reset(void);
204 static int set_multiple_mode(int sectors);
205 static int set_features(void);
207 STATICIRAM ICODE_ATTR int wait_for_bsy(void)
209 long timeout = current_tick + HZ*30;
213 if (!(ATA_STATUS & STATUS_BSY))
214 return 1;
215 last_disk_activity = current_tick;
216 yield();
217 } while (TIME_BEFORE(current_tick, timeout));
219 return 0; /* timeout */
222 STATICIRAM ICODE_ATTR int wait_for_rdy(void)
224 long timeout;
226 if (!wait_for_bsy())
227 return 0;
229 timeout = current_tick + HZ*10;
233 if (ATA_ALT_STATUS & STATUS_RDY)
234 return 1;
235 last_disk_activity = current_tick;
236 yield();
237 } while (TIME_BEFORE(current_tick, timeout));
239 return 0; /* timeout */
242 STATICIRAM ICODE_ATTR int wait_for_start_of_transfer(void)
244 if (!wait_for_bsy())
245 return 0;
247 return (ATA_ALT_STATUS & (STATUS_BSY|STATUS_DRQ)) == STATUS_DRQ;
250 STATICIRAM ICODE_ATTR int wait_for_end_of_transfer(void)
252 if (!wait_for_bsy())
253 return 0;
254 return (ATA_ALT_STATUS &
255 (STATUS_BSY|STATUS_RDY|STATUS_DF|STATUS_DRQ|STATUS_ERR))
256 == STATUS_RDY;
259 #if (CONFIG_LED == LED_REAL)
260 /* Conditionally block LED access for the ATA driver, so the LED can be
261 * (mis)used for other purposes */
262 static void ata_led(bool on)
264 ata_led_on = on;
265 if (ata_led_enabled)
266 led(ata_led_on);
268 #else
269 #define ata_led(on) led(on)
270 #endif
272 #ifndef ATA_OPTIMIZED_READING
273 STATICIRAM ICODE_ATTR void copy_read_sectors(unsigned char* buf, int wordcount)
275 unsigned short tmp = 0;
277 if ( (unsigned long)buf & 1)
278 { /* not 16-bit aligned, copy byte by byte */
279 unsigned char* bufend = buf + wordcount*2;
282 tmp = ATA_DATA;
283 #if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN)
284 *buf++ = tmp & 0xff; /* I assume big endian */
285 *buf++ = tmp >> 8; /* and don't use the SWAB16 macro */
286 #else
287 *buf++ = tmp >> 8;
288 *buf++ = tmp & 0xff;
289 #endif
290 } while (buf < bufend); /* tail loop is faster */
292 else
293 { /* 16-bit aligned, can do faster copy */
294 unsigned short* wbuf = (unsigned short*)buf;
295 unsigned short* wbufend = wbuf + wordcount;
298 #ifdef SWAP_WORDS
299 *wbuf = swap16(ATA_DATA);
300 #else
301 *wbuf = ATA_DATA;
302 #endif
303 } while (++wbuf < wbufend); /* tail loop is faster */
306 #endif /* !ATA_OPTIMIZED_READING */
308 #ifndef ATA_OPTIMIZED_WRITING
309 STATICIRAM ICODE_ATTR void copy_write_sectors(const unsigned char* buf,
310 int wordcount)
312 if ( (unsigned long)buf & 1)
313 { /* not 16-bit aligned, copy byte by byte */
314 unsigned short tmp = 0;
315 const unsigned char* bufend = buf + wordcount*2;
318 #if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN)
319 tmp = (unsigned short) *buf++;
320 tmp |= (unsigned short) *buf++ << 8;
321 SET_16BITREG(ATA_DATA, tmp);
322 #else
323 tmp = (unsigned short) *buf++ << 8;
324 tmp |= (unsigned short) *buf++;
325 SET_16BITREG(ATA_DATA, tmp);
326 #endif
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 #ifdef SWAP_WORDS
336 SET_16BITREG(ATA_DATA, swap16(*wbuf));
337 #else
338 SET_16BITREG(ATA_DATA, *wbuf);
339 #endif
340 } while (++wbuf < wbufend); /* tail loop is faster */
343 #endif /* !ATA_OPTIMIZED_WRITING */
345 static int ata_transfer_sectors(unsigned long start,
346 int incount,
347 void* inbuf,
348 int write)
350 int ret = 0;
351 long timeout;
352 int count;
353 void* buf;
354 long spinup_start;
355 #ifdef HAVE_ATA_DMA
356 bool usedma = false;
357 #endif
359 #ifndef MAX_PHYS_SECTOR_SIZE
360 mutex_lock(&ata_mtx);
361 #endif
363 if (start + incount > total_sectors) {
364 ret = -1;
365 goto error;
368 last_disk_activity = current_tick;
369 spinup_start = current_tick;
371 ata_led(true);
373 if ( sleeping ) {
374 spinup = true;
375 if (poweroff) {
376 if (ata_power_on()) {
377 ret = -2;
378 goto error;
381 else {
382 if (perform_soft_reset()) {
383 ret = -2;
384 goto error;
389 timeout = current_tick + READWRITE_TIMEOUT;
391 SET_REG(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 SET_REG(ATA_NSECTOR, count >> 8);
415 SET_REG(ATA_NSECTOR, count & 0xff);
416 SET_REG(ATA_SECTOR, (start >> 24) & 0xff); /* 31:24 */
417 SET_REG(ATA_SECTOR, start & 0xff); /* 7:0 */
418 SET_REG(ATA_LCYL, 0); /* 39:32 */
419 SET_REG(ATA_LCYL, (start >> 8) & 0xff); /* 15:8 */
420 SET_REG(ATA_HCYL, 0); /* 47:40 */
421 SET_REG(ATA_HCYL, (start >> 16) & 0xff); /* 23:16 */
422 SET_REG(ATA_SELECT, SELECT_LBA | ata_device);
423 #ifdef HAVE_ATA_DMA
424 if (write)
425 SET_REG(ATA_COMMAND, usedma ? CMD_WRITE_DMA_EXT : CMD_WRITE_MULTIPLE_EXT);
426 else
427 SET_REG(ATA_COMMAND, usedma ? CMD_READ_DMA_EXT : CMD_READ_MULTIPLE_EXT);
428 #else
429 SET_REG(ATA_COMMAND, write ? CMD_WRITE_MULTIPLE_EXT : CMD_READ_MULTIPLE_EXT);
430 #endif
432 else
433 #endif
435 SET_REG(ATA_NSECTOR, count & 0xff); /* 0 means 256 sectors */
436 SET_REG(ATA_SECTOR, start & 0xff);
437 SET_REG(ATA_LCYL, (start >> 8) & 0xff);
438 SET_REG(ATA_HCYL, (start >> 16) & 0xff);
439 SET_REG(ATA_SELECT, ((start >> 24) & 0xf) | SELECT_LBA | ata_device);
440 #ifdef HAVE_ATA_DMA
441 if (write)
442 SET_REG(ATA_COMMAND, usedma ? CMD_WRITE_DMA : CMD_WRITE_MULTIPLE);
443 else
444 SET_REG(ATA_COMMAND, usedma ? CMD_READ_DMA : CMD_READ_MULTIPLE);
445 #else
446 SET_REG(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 sleeping = false;
471 poweroff = false;
474 else
475 #endif /* HAVE_ATA_DMA */
477 while (count) {
478 int sectors;
479 int wordcount;
480 int status;
482 if (!wait_for_start_of_transfer()) {
483 /* We have timed out waiting for RDY and/or DRQ, possibly
484 because the hard drive is shaking and has problems
485 reading the data. We have two options:
486 1) Wait some more
487 2) Perform a soft reset and try again.
489 We choose alternative 2.
491 perform_soft_reset();
492 ret = -5;
493 goto retry;
496 if (spinup) {
497 spinup_time = current_tick - spinup_start;
498 spinup = false;
499 sleeping = false;
500 poweroff = false;
503 /* read the status register exactly once per loop */
504 status = ATA_STATUS;
506 if (count >= multisectors )
507 sectors = multisectors;
508 else
509 sectors = count;
511 wordcount = sectors * SECTOR_SIZE / 2;
513 if (write)
514 copy_write_sectors(buf, wordcount);
515 else
516 copy_read_sectors(buf, wordcount);
519 "Device errors encountered during READ MULTIPLE commands
520 are posted at the beginning of the block or partial block
521 transfer, but the DRQ bit is still set to one and the data
522 transfer shall take place, including transfer of corrupted
523 data, if any."
524 -- ATA specification
526 if ( status & (STATUS_BSY | STATUS_ERR | STATUS_DF) ) {
527 perform_soft_reset();
528 ret = -6;
529 goto retry;
532 buf += sectors * SECTOR_SIZE; /* Advance one chunk of sectors */
533 count -= sectors;
535 last_disk_activity = current_tick;
539 if(!ret && !wait_for_end_of_transfer()) {
540 perform_soft_reset();
541 ret = -4;
542 goto retry;
544 break;
547 error:
548 ata_led(false);
549 #ifndef MAX_PHYS_SECTOR_SIZE
550 mutex_unlock(&ata_mtx);
551 #endif
553 return ret;
556 #ifndef MAX_PHYS_SECTOR_SIZE
557 int ata_read_sectors(IF_MD2(int drive,)
558 unsigned long start,
559 int incount,
560 void* inbuf)
562 #ifdef HAVE_MULTIDRIVE
563 (void)drive; /* unused for now */
564 #endif
566 return ata_transfer_sectors(start, incount, inbuf, false);
568 #endif
570 #ifndef MAX_PHYS_SECTOR_SIZE
571 int ata_write_sectors(IF_MD2(int drive,)
572 unsigned long start,
573 int count,
574 const void* buf)
576 #ifdef HAVE_MULTIDRIVE
577 (void)drive; /* unused for now */
578 #endif
580 return ata_transfer_sectors(start, count, (void*)buf, true);
582 #endif
584 #ifdef MAX_PHYS_SECTOR_SIZE
585 static int cache_sector(unsigned long sector)
587 int rc;
589 sector &= ~(phys_sector_mult - 1);
590 /* round down to physical sector boundary */
592 /* check whether the sector is already cached */
593 if (sector_cache.inuse && (sector_cache.sectornum == sector))
594 return 0;
596 /* not found: read the sector */
597 sector_cache.inuse = false;
598 rc = ata_transfer_sectors(sector, phys_sector_mult, sector_cache.data, false);
599 if (!rc)
601 sector_cache.sectornum = sector;
602 sector_cache.inuse = true;
604 return rc;
607 static inline int flush_current_sector(void)
609 return ata_transfer_sectors(sector_cache.sectornum, phys_sector_mult,
610 sector_cache.data, true);
613 int ata_read_sectors(IF_MD2(int drive,)
614 unsigned long start,
615 int incount,
616 void* inbuf)
618 int rc = 0;
619 int offset;
621 #ifdef HAVE_MULTIDRIVE
622 (void)drive; /* unused for now */
623 #endif
624 mutex_lock(&ata_mtx);
626 offset = start & (phys_sector_mult - 1);
628 if (offset) /* first partial sector */
630 int partcount = MIN(incount, phys_sector_mult - offset);
632 rc = cache_sector(start);
633 if (rc)
635 rc = rc * 10 - 1;
636 goto error;
638 memcpy(inbuf, sector_cache.data + offset * SECTOR_SIZE,
639 partcount * SECTOR_SIZE);
641 start += partcount;
642 inbuf += partcount * SECTOR_SIZE;
643 incount -= partcount;
645 if (incount)
647 offset = incount & (phys_sector_mult - 1);
648 incount -= offset;
650 if (incount)
652 rc = ata_transfer_sectors(start, incount, inbuf, false);
653 if (rc)
655 rc = rc * 10 - 2;
656 goto error;
658 start += incount;
659 inbuf += incount * SECTOR_SIZE;
661 if (offset)
663 rc = cache_sector(start);
664 if (rc)
666 rc = rc * 10 - 3;
667 goto error;
669 memcpy(inbuf, sector_cache.data, offset * SECTOR_SIZE);
673 error:
674 mutex_unlock(&ata_mtx);
676 return rc;
679 int ata_write_sectors(IF_MD2(int drive,)
680 unsigned long start,
681 int count,
682 const void* buf)
684 int rc = 0;
685 int offset;
687 #ifdef HAVE_MULTIDRIVE
688 (void)drive; /* unused for now */
689 #endif
690 mutex_lock(&ata_mtx);
692 offset = start & (phys_sector_mult - 1);
694 if (offset) /* first partial sector */
696 int partcount = MIN(count, phys_sector_mult - offset);
698 rc = cache_sector(start);
699 if (rc)
701 rc = rc * 10 - 1;
702 goto error;
704 memcpy(sector_cache.data + offset * SECTOR_SIZE, buf,
705 partcount * SECTOR_SIZE);
706 rc = flush_current_sector();
707 if (rc)
709 rc = rc * 10 - 2;
710 goto error;
712 start += partcount;
713 buf += partcount * SECTOR_SIZE;
714 count -= partcount;
716 if (count)
718 offset = count & (phys_sector_mult - 1);
719 count -= offset;
721 if (count)
723 rc = ata_transfer_sectors(start, count, (void*)buf, true);
724 if (rc)
726 rc = rc * 10 - 3;
727 goto error;
729 start += count;
730 buf += count * SECTOR_SIZE;
732 if (offset)
734 rc = cache_sector(start);
735 if (rc)
737 rc = rc * 10 - 4;
738 goto error;
740 memcpy(sector_cache.data, buf, offset * SECTOR_SIZE);
741 rc = flush_current_sector();
742 if (rc)
744 rc = rc * 10 - 5;
745 goto error;
750 error:
751 mutex_unlock(&ata_mtx);
753 return rc;
755 #endif /* MAX_PHYS_SECTOR_SIZE */
757 static int check_registers(void)
759 int i;
760 if ( ATA_STATUS & STATUS_BSY )
761 return -1;
763 for (i = 0; i<64; i++) {
764 SET_REG(ATA_NSECTOR, WRITE_PATTERN1);
765 SET_REG(ATA_SECTOR, WRITE_PATTERN2);
766 SET_REG(ATA_LCYL, WRITE_PATTERN3);
767 SET_REG(ATA_HCYL, WRITE_PATTERN4);
769 if (((ATA_NSECTOR & READ_PATTERN1_MASK) == READ_PATTERN1) &&
770 ((ATA_SECTOR & READ_PATTERN2_MASK) == READ_PATTERN2) &&
771 ((ATA_LCYL & READ_PATTERN3_MASK) == READ_PATTERN3) &&
772 ((ATA_HCYL & READ_PATTERN4_MASK) == READ_PATTERN4))
773 return 0;
775 return -2;
778 static int freeze_lock(void)
780 /* does the disk support Security Mode feature set? */
781 if (identify_info[82] & 2)
783 SET_REG(ATA_SELECT, ata_device);
785 if (!wait_for_rdy())
786 return -1;
788 SET_REG(ATA_COMMAND, CMD_SECURITY_FREEZE_LOCK);
790 if (!wait_for_rdy())
791 return -2;
794 return 0;
797 void ata_spindown(int seconds)
799 sleep_timeout = seconds * HZ;
802 bool ata_disk_is_active(void)
804 return !sleeping;
807 static int ata_perform_sleep(void)
809 mutex_lock(&ata_mtx);
811 SET_REG(ATA_SELECT, ata_device);
813 if(!wait_for_rdy()) {
814 DEBUGF("ata_perform_sleep() - not RDY\n");
815 mutex_unlock(&ata_mtx);
816 return -1;
819 SET_REG(ATA_COMMAND, CMD_SLEEP);
821 if (!wait_for_rdy())
823 DEBUGF("ata_perform_sleep() - CMD failed\n");
824 mutex_unlock(&ata_mtx);
825 return -2;
828 sleeping = true;
829 mutex_unlock(&ata_mtx);
830 return 0;
833 void ata_sleep(void)
835 queue_post(&ata_queue, Q_SLEEP, 0);
838 void ata_sleepnow(void)
840 if (!spinup && !sleeping && !ata_mtx.locked && initialized)
842 call_storage_idle_notifys(false);
843 ata_perform_sleep();
847 void ata_spin(void)
849 last_user_activity = current_tick;
852 static void ata_thread(void)
854 static long last_sleep = 0;
855 struct queue_event ev;
856 static long last_seen_mtx_unlock = 0;
857 #ifdef ALLOW_USB_SPINDOWN
858 static bool usb_mode = false;
859 #endif
861 while (1) {
862 queue_wait_w_tmo(&ata_queue, &ev, HZ/2);
864 switch ( ev.id ) {
865 case SYS_TIMEOUT:
866 if (!spinup && !sleeping)
868 if (!ata_mtx.locked)
870 if (!last_seen_mtx_unlock)
871 last_seen_mtx_unlock = current_tick;
872 if (TIME_AFTER(current_tick, last_seen_mtx_unlock+(HZ*2)))
874 #ifdef ALLOW_USB_SPINDOWN
875 if(!usb_mode)
876 #endif
878 call_storage_idle_notifys(false);
880 last_seen_mtx_unlock = 0;
883 if ( sleep_timeout &&
884 TIME_AFTER( current_tick,
885 last_user_activity + sleep_timeout ) &&
886 TIME_AFTER( current_tick,
887 last_disk_activity + sleep_timeout ) )
889 #ifdef ALLOW_USB_SPINDOWN
890 if(!usb_mode)
891 #endif
893 call_storage_idle_notifys(true);
895 ata_perform_sleep();
896 last_sleep = current_tick;
900 #ifdef HAVE_ATA_POWER_OFF
901 if ( !spinup && sleeping && !poweroff &&
902 TIME_AFTER( current_tick, last_sleep + ATA_POWER_OFF_TIMEOUT ))
904 mutex_lock(&ata_mtx);
905 ide_power_enable(false);
906 poweroff = true;
907 mutex_unlock(&ata_mtx);
909 #endif
910 break;
912 #ifndef USB_NONE
913 case SYS_USB_CONNECTED:
914 /* Tell the USB thread that we are safe */
915 DEBUGF("ata_thread got SYS_USB_CONNECTED\n");
916 #ifdef ALLOW_USB_SPINDOWN
917 usb_mode = true;
918 usb_acknowledge(SYS_USB_CONNECTED_ACK);
919 /* There is no need to force ATA power on */
920 #else
921 if (sleeping) {
922 mutex_lock(&ata_mtx);
923 ata_led(true);
924 if (poweroff) {
925 ata_power_on();
926 poweroff = false;
928 else {
929 perform_soft_reset();
931 sleeping = false;
932 ata_led(false);
933 mutex_unlock(&ata_mtx);
936 /* Wait until the USB cable is extracted again */
937 usb_acknowledge(SYS_USB_CONNECTED_ACK);
938 usb_wait_for_disconnect(&ata_queue);
939 #endif
940 break;
942 #ifdef ALLOW_USB_SPINDOWN
943 case SYS_USB_DISCONNECTED:
944 /* Tell the USB thread that we are ready again */
945 DEBUGF("ata_thread got SYS_USB_DISCONNECTED\n");
946 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
947 usb_mode = false;
948 break;
949 #endif
950 #endif /* USB_NONE */
952 case Q_SLEEP:
953 #ifdef ALLOW_USB_SPINDOWN
954 if(!usb_mode)
955 #endif
957 call_storage_idle_notifys(false);
959 last_disk_activity = current_tick - sleep_timeout + (HZ/2);
960 break;
962 #ifdef ATA_DRIVER_CLOSE
963 case Q_CLOSE:
964 return;
965 #endif
970 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
971 static int ata_hard_reset(void)
973 int ret;
975 mutex_lock(&ata_mtx);
977 ata_reset();
979 /* state HRR2 */
980 SET_REG(ATA_SELECT, ata_device); /* select the right device */
981 ret = wait_for_bsy();
983 /* Massage the return code so it is 0 on success and -1 on failure */
984 ret = ret?0:-1;
986 mutex_unlock(&ata_mtx);
988 return ret;
991 static int perform_soft_reset(void)
993 /* If this code is allowed to run on a Nano, the next reads from the flash will
994 * time out, so we disable it. It shouldn't be necessary anyway, since the
995 * ATA -> Flash interface automatically sleeps almost immediately after the
996 * last command.
998 int ret;
999 int retry_count;
1001 SET_REG(ATA_SELECT, SELECT_LBA | ata_device );
1002 SET_REG(ATA_CONTROL, CONTROL_nIEN|CONTROL_SRST );
1003 sleep(1); /* >= 5us */
1005 #ifdef HAVE_ATA_DMA
1006 /* DMA requires INTRQ be enabled */
1007 SET_REG(ATA_CONTROL, 0);
1008 #else
1009 SET_REG(ATA_CONTROL, CONTROL_nIEN);
1010 #endif
1011 sleep(1); /* >2ms */
1013 /* This little sucker can take up to 30 seconds */
1014 retry_count = 8;
1017 ret = wait_for_rdy();
1018 } while(!ret && retry_count--);
1020 if (!ret)
1021 return -1;
1023 if (set_features())
1024 return -2;
1026 if (set_multiple_mode(multisectors))
1027 return -3;
1029 if (freeze_lock())
1030 return -4;
1032 return 0;
1035 int ata_soft_reset(void)
1037 int ret;
1039 mutex_lock(&ata_mtx);
1041 ret = perform_soft_reset();
1043 mutex_unlock(&ata_mtx);
1044 return ret;
1047 static int ata_power_on(void)
1049 int rc;
1051 ide_power_enable(true);
1052 sleep(HZ/4); /* allow voltage to build up */
1054 /* Accessing the PP IDE controller too early after powering up the disk
1055 * makes the core hang for a short time, causing an audio dropout. This
1056 * also depends on the disk; iPod Mini G2 needs at least HZ/5 to get rid
1057 * of the dropout. Since this time isn't additive (the wait_for_bsy() in
1058 * ata_hard_reset() will shortened by the same amount), it's a good idea
1059 * to do this on all HDD based targets. */
1061 if( ata_hard_reset() )
1062 return -1;
1064 rc = set_features();
1065 if (rc)
1066 return rc * 10 - 2;
1068 if (set_multiple_mode(multisectors))
1069 return -3;
1071 if (freeze_lock())
1072 return -4;
1074 return 0;
1077 static int master_slave_detect(void)
1079 /* master? */
1080 SET_REG(ATA_SELECT, 0);
1081 if ( ATA_STATUS & (STATUS_RDY|STATUS_BSY) ) {
1082 ata_device = 0;
1083 DEBUGF("Found master harddisk\n");
1085 else {
1086 /* slave? */
1087 SET_REG(ATA_SELECT, SELECT_DEVICE1);
1088 if ( ATA_STATUS & (STATUS_RDY|STATUS_BSY) ) {
1089 ata_device = SELECT_DEVICE1;
1090 DEBUGF("Found slave harddisk\n");
1092 else
1093 return -1;
1095 return 0;
1098 static int identify(void)
1100 int i;
1102 SET_REG(ATA_SELECT, ata_device);
1104 if(!wait_for_rdy()) {
1105 DEBUGF("identify() - not RDY\n");
1106 return -1;
1108 SET_REG(ATA_COMMAND, CMD_IDENTIFY);
1110 if (!wait_for_start_of_transfer())
1112 DEBUGF("identify() - CMD failed\n");
1113 return -2;
1116 for (i=0; i<SECTOR_SIZE/2; i++) {
1117 /* the IDENTIFY words are already swapped, so we need to treat
1118 this info differently that normal sector data */
1119 #if defined(ROCKBOX_BIG_ENDIAN) && !defined(SWAP_WORDS)
1120 identify_info[i] = swap16(ATA_DATA);
1121 #else
1122 identify_info[i] = ATA_DATA;
1123 #endif
1126 return 0;
1129 static int set_multiple_mode(int sectors)
1131 SET_REG(ATA_SELECT, ata_device);
1133 if(!wait_for_rdy()) {
1134 DEBUGF("set_multiple_mode() - not RDY\n");
1135 return -1;
1138 SET_REG(ATA_NSECTOR, sectors);
1139 SET_REG(ATA_COMMAND, CMD_SET_MULTIPLE_MODE);
1141 if (!wait_for_rdy())
1143 DEBUGF("set_multiple_mode() - CMD failed\n");
1144 return -2;
1147 return 0;
1150 #ifdef HAVE_ATA_DMA
1151 static int get_best_mode(unsigned short identword, int max, int modetype)
1153 unsigned short testbit = BIT_N(max);
1155 while (1) {
1156 if (identword & testbit)
1157 return max | modetype;
1158 testbit >>= 1;
1159 if (!testbit)
1160 return 0;
1161 max--;
1164 #endif
1166 static int set_features(void)
1168 static struct {
1169 unsigned char id_word;
1170 unsigned char id_bit;
1171 unsigned char subcommand;
1172 unsigned char parameter;
1173 } features[] = {
1174 { 83, 14, 0x03, 0 }, /* force PIO mode */
1175 { 83, 3, 0x05, 0x80 }, /* adv. power management: lowest w/o standby */
1176 { 83, 9, 0x42, 0x80 }, /* acoustic management: lowest noise */
1177 { 82, 6, 0xaa, 0 }, /* enable read look-ahead */
1178 #ifdef HAVE_ATA_DMA
1179 { 0, 0, 0x03, 0 }, /* DMA mode */
1180 #endif
1182 int i;
1183 int pio_mode = 2;
1185 /* Find out the highest supported PIO mode */
1186 if(identify_info[64] & 2)
1187 pio_mode = 4;
1188 else
1189 if(identify_info[64] & 1)
1190 pio_mode = 3;
1192 /* Update the table: set highest supported pio mode that we also support */
1193 features[0].parameter = 8 + pio_mode;
1195 #ifdef HAVE_ATA_DMA
1196 if (identify_info[53] & (1<<2))
1197 /* Ultra DMA mode info present, find a mode */
1198 dma_mode = get_best_mode(identify_info[88], ATA_MAX_UDMA, 0x40);
1200 if (!dma_mode) {
1201 /* No UDMA mode found, try to find a multi-word DMA mode */
1202 dma_mode = get_best_mode(identify_info[63], ATA_MAX_MWDMA, 0x20);
1203 features[4].id_word = 63;
1205 else
1206 features[4].id_word = 88;
1208 features[4].id_bit = dma_mode & 7;
1209 features[4].parameter = dma_mode;
1210 #endif /* HAVE_ATA_DMA */
1212 SET_REG(ATA_SELECT, ata_device);
1214 if (!wait_for_rdy()) {
1215 DEBUGF("set_features() - not RDY\n");
1216 return -1;
1219 for (i=0; i < (int)(sizeof(features)/sizeof(features[0])); i++) {
1220 if (identify_info[features[i].id_word] & BIT_N(features[i].id_bit)) {
1221 SET_REG(ATA_FEATURE, features[i].subcommand);
1222 SET_REG(ATA_NSECTOR, features[i].parameter);
1223 SET_REG(ATA_COMMAND, CMD_SET_FEATURES);
1225 if (!wait_for_rdy()) {
1226 DEBUGF("set_features() - CMD failed\n");
1227 return -10 - i;
1230 if((ATA_ALT_STATUS & STATUS_ERR) && (i != 1)) {
1231 /* some CF cards don't like advanced powermanagement
1232 even if they mark it as supported - go figure... */
1233 if(ATA_ERROR & ERROR_ABRT) {
1234 return -20 - i;
1240 #ifdef ATA_SET_DEVICE_FEATURES
1241 ata_set_pio_timings(pio_mode);
1242 #endif
1244 #ifdef HAVE_ATA_DMA
1245 ata_dma_set_mode(dma_mode);
1246 #endif
1248 return 0;
1251 unsigned short* ata_get_identify(void)
1253 return identify_info;
1256 static int init_and_check(bool hard_reset)
1258 int rc;
1260 if (hard_reset)
1262 /* This should reset both master and slave, we don't yet know what's in */
1263 ata_device = 0;
1264 if (ata_hard_reset())
1265 return -1;
1268 rc = master_slave_detect();
1269 if (rc)
1270 return -10 + rc;
1272 /* symptom fix: else check_registers() below may fail */
1273 if (hard_reset && !wait_for_bsy())
1274 return -20;
1276 rc = check_registers();
1277 if (rc)
1278 return -30 + rc;
1280 return 0;
1283 int ata_init(void)
1285 int rc = 0;
1286 bool coldstart;
1288 if ( !initialized ) {
1289 mutex_init(&ata_mtx);
1290 queue_init(&ata_queue, true);
1293 mutex_lock(&ata_mtx);
1295 /* must be called before ata_device_init() */
1296 coldstart = ata_is_coldstart();
1297 ata_led(false);
1298 ata_device_init();
1299 sleeping = false;
1300 ata_enable(true);
1301 #ifdef MAX_PHYS_SECTOR_SIZE
1302 memset(&sector_cache, 0, sizeof(sector_cache));
1303 #endif
1305 if ( !initialized ) {
1306 /* First call won't have multiple thread contention - this
1307 * may return at any point without having to unlock */
1308 mutex_unlock(&ata_mtx);
1310 if (!ide_powered()) /* somebody has switched it off */
1312 ide_power_enable(true);
1313 sleep(HZ/4); /* allow voltage to build up */
1316 #ifdef HAVE_ATA_DMA
1317 /* DMA requires INTRQ be enabled */
1318 SET_REG(ATA_CONTROL, 0);
1319 #endif
1321 /* first try, hard reset at cold start only */
1322 rc = init_and_check(coldstart);
1324 if (rc)
1325 { /* failed? -> second try, always with hard reset */
1326 DEBUGF("ata: init failed, retrying...\n");
1327 rc = init_and_check(true);
1328 if (rc)
1329 return rc;
1332 rc = identify();
1334 if (rc)
1335 return -40 + rc;
1337 multisectors = identify_info[47] & 0xff;
1338 if (multisectors == 0) /* Invalid multisector info, try with 16 */
1339 multisectors = 16;
1341 DEBUGF("ata: %d sectors per ata request\n",multisectors);
1343 #ifdef MAX_PHYS_SECTOR_SIZE
1344 /* Find out the physical sector size */
1345 if((identify_info[106] & 0xe000) == 0x6000)
1346 phys_sector_mult = BIT_N(identify_info[106] & 0x000f);
1347 else
1348 phys_sector_mult = 1;
1350 DEBUGF("ata: %d logical sectors per phys sector", phys_sector_mult);
1352 if (phys_sector_mult > (MAX_PHYS_SECTOR_SIZE/SECTOR_SIZE))
1353 panicf("Unsupported physical sector size: %d",
1354 phys_sector_mult * SECTOR_SIZE);
1355 #endif
1357 total_sectors = identify_info[60] | (identify_info[61] << 16);
1359 #ifdef HAVE_LBA48
1360 if (identify_info[83] & 0x0400 /* 48 bit address support */
1361 && total_sectors == 0x0FFFFFFF) /* and disk size >= 128 GiB */
1362 { /* (needs BigLBA addressing) */
1363 if (identify_info[102] || identify_info[103])
1364 panicf("Unsupported disk size: >= 2^32 sectors");
1366 total_sectors = identify_info[100] | (identify_info[101] << 16);
1367 lba48 = true; /* use BigLBA */
1369 #endif
1370 rc = freeze_lock();
1372 if (rc)
1373 return -50 + rc;
1375 rc = set_features();
1376 if (rc)
1377 return -60 + rc;
1379 mutex_lock(&ata_mtx); /* Balance unlock below */
1381 last_disk_activity = current_tick;
1382 #ifdef ATA_DRIVER_CLOSE
1383 ata_thread_id =
1384 #endif
1385 create_thread(ata_thread, ata_stack,
1386 sizeof(ata_stack), 0, ata_thread_name
1387 IF_PRIO(, PRIORITY_USER_INTERFACE)
1388 IF_COP(, CPU));
1389 initialized = true;
1392 rc = set_multiple_mode(multisectors);
1393 if (rc)
1394 rc = -70 + rc;
1396 mutex_unlock(&ata_mtx);
1397 return rc;
1400 #ifdef ATA_DRIVER_CLOSE
1401 void ata_close(void)
1403 unsigned int thread_id = ata_thread_id;
1405 if (thread_id == 0)
1406 return;
1408 ata_thread_id = 0;
1410 queue_post(&ata_queue, Q_CLOSE, 0);
1411 thread_wait(thread_id);
1413 #endif /* ATA_DRIVER_CLOSE */
1415 #if (CONFIG_LED == LED_REAL)
1416 void ata_set_led_enabled(bool enabled)
1418 ata_led_enabled = enabled;
1419 if (ata_led_enabled)
1420 led(ata_led_on);
1421 else
1422 led(false);
1424 #endif
1426 long ata_last_disk_activity(void)
1428 return last_disk_activity;
1431 int ata_spinup_time(void)
1433 return spinup_time;
1436 #ifdef STORAGE_GET_INFO
1437 void ata_get_info(IF_MD2(int drive,)struct storage_info *info)
1439 unsigned short *src,*dest;
1440 static char vendor[8];
1441 static char product[16];
1442 static char revision[4];
1443 #ifdef HAVE_MULTIDRIVE
1444 (void)drive; /* unused for now */
1445 #endif
1446 int i;
1447 info->sector_size = SECTOR_SIZE;
1448 info->num_sectors= total_sectors;
1450 src = (unsigned short*)&identify_info[27];
1451 dest = (unsigned short*)vendor;
1452 for (i=0;i<4;i++)
1453 dest[i] = htobe16(src[i]);
1454 info->vendor=vendor;
1456 src = (unsigned short*)&identify_info[31];
1457 dest = (unsigned short*)product;
1458 for (i=0;i<8;i++)
1459 dest[i] = htobe16(src[i]);
1460 info->product=product;
1462 src = (unsigned short*)&identify_info[23];
1463 dest = (unsigned short*)revision;
1464 for (i=0;i<2;i++)
1465 dest[i] = htobe16(src[i]);
1466 info->revision=revision;
1468 #endif
1470 #ifdef HAVE_ATA_DMA
1471 /* Returns last DMA mode as set by set_features() */
1472 int ata_get_dma_mode(void)
1474 return dma_mode;
1477 /* Needed to allow updating while waiting for DMA to complete */
1478 void ata_keep_active(void)
1480 last_disk_activity = current_tick;
1482 #endif
1484 #ifdef CONFIG_STORAGE_MULTI
1485 int ata_num_drives(int first_drive)
1487 /* We don't care which logical drive number(s) we have been assigned */
1488 (void)first_drive;
1490 return 1;
1492 #endif