libwmapro: tiny tweak fo coldfire fixmul24 for a very slight speedup.
[maemo-rb.git] / firmware / drivers / ata.c
blob1d738f5bc29c50d1298eb24787a3e404ada4dfda
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(ATA_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 ATA_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(ATA_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 ATA_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 sleeping = false; /* set this now since it'll be on */
375 spinup = true;
376 if (poweroff) {
377 if (ata_power_on()) {
378 ret = -2;
379 goto error;
382 else {
383 if (perform_soft_reset()) {
384 ret = -2;
385 goto error;
390 timeout = current_tick + READWRITE_TIMEOUT;
392 SET_REG(ATA_SELECT, ata_device);
393 if (!wait_for_rdy())
395 ret = -3;
396 goto error;
399 retry:
400 buf = inbuf;
401 count = incount;
402 while (TIME_BEFORE(current_tick, timeout)) {
403 ret = 0;
404 last_disk_activity = current_tick;
406 #ifdef HAVE_ATA_DMA
407 /* If DMA is supported and parameters are ok for DMA, use it */
408 if (dma_mode && ata_dma_setup(inbuf, incount * SECTOR_SIZE, write))
409 usedma = true;
410 #endif
412 #ifdef HAVE_LBA48
413 if (lba48)
415 SET_REG(ATA_NSECTOR, count >> 8);
416 SET_REG(ATA_NSECTOR, count & 0xff);
417 SET_REG(ATA_SECTOR, (start >> 24) & 0xff); /* 31:24 */
418 SET_REG(ATA_SECTOR, start & 0xff); /* 7:0 */
419 SET_REG(ATA_LCYL, 0); /* 39:32 */
420 SET_REG(ATA_LCYL, (start >> 8) & 0xff); /* 15:8 */
421 SET_REG(ATA_HCYL, 0); /* 47:40 */
422 SET_REG(ATA_HCYL, (start >> 16) & 0xff); /* 23:16 */
423 SET_REG(ATA_SELECT, SELECT_LBA | ata_device);
424 #ifdef HAVE_ATA_DMA
425 if (write)
426 SET_REG(ATA_COMMAND, usedma ? CMD_WRITE_DMA_EXT : CMD_WRITE_MULTIPLE_EXT);
427 else
428 SET_REG(ATA_COMMAND, usedma ? CMD_READ_DMA_EXT : CMD_READ_MULTIPLE_EXT);
429 #else
430 SET_REG(ATA_COMMAND, write ? CMD_WRITE_MULTIPLE_EXT : CMD_READ_MULTIPLE_EXT);
431 #endif
433 else
434 #endif
436 SET_REG(ATA_NSECTOR, count & 0xff); /* 0 means 256 sectors */
437 SET_REG(ATA_SECTOR, start & 0xff);
438 SET_REG(ATA_LCYL, (start >> 8) & 0xff);
439 SET_REG(ATA_HCYL, (start >> 16) & 0xff);
440 SET_REG(ATA_SELECT, ((start >> 24) & 0xf) | SELECT_LBA | ata_device);
441 #ifdef HAVE_ATA_DMA
442 if (write)
443 SET_REG(ATA_COMMAND, usedma ? CMD_WRITE_DMA : CMD_WRITE_MULTIPLE);
444 else
445 SET_REG(ATA_COMMAND, usedma ? CMD_READ_DMA : CMD_READ_MULTIPLE);
446 #else
447 SET_REG(ATA_COMMAND, write ? CMD_WRITE_MULTIPLE : CMD_READ_MULTIPLE);
448 #endif
451 /* wait at least 400ns between writing command and reading status */
452 __asm__ volatile ("nop");
453 __asm__ volatile ("nop");
454 __asm__ volatile ("nop");
455 __asm__ volatile ("nop");
456 __asm__ volatile ("nop");
458 #ifdef HAVE_ATA_DMA
459 if (usedma) {
460 if (!ata_dma_finish())
461 ret = -7;
463 if (ret != 0) {
464 perform_soft_reset();
465 goto retry;
468 if (spinup) {
469 spinup_time = current_tick - spinup_start;
470 spinup = false;
471 poweroff = false;
474 else
475 #endif /* HAVE_ATA_DMA */
477 while (count) {
478 int sectors;
479 int wordcount;
480 int status;
481 int error;
483 if (!wait_for_start_of_transfer()) {
484 /* We have timed out waiting for RDY and/or DRQ, possibly
485 because the hard drive is shaking and has problems
486 reading the data. We have two options:
487 1) Wait some more
488 2) Perform a soft reset and try again.
490 We choose alternative 2.
492 perform_soft_reset();
493 ret = -5;
494 goto retry;
497 if (spinup) {
498 spinup_time = current_tick - spinup_start;
499 spinup = false;
500 poweroff = false;
503 /* read the status register exactly once per loop */
504 status = ATA_STATUS;
505 error = ATA_ERROR;
507 if (count >= multisectors )
508 sectors = multisectors;
509 else
510 sectors = count;
512 wordcount = sectors * SECTOR_SIZE / 2;
514 if (write)
515 copy_write_sectors(buf, wordcount);
516 else
517 copy_read_sectors(buf, wordcount);
520 "Device errors encountered during READ MULTIPLE commands
521 are posted at the beginning of the block or partial block
522 transfer, but the DRQ bit is still set to one and the data
523 transfer shall take place, including transfer of corrupted
524 data, if any."
525 -- ATA specification
527 if ( status & (STATUS_BSY | STATUS_ERR | STATUS_DF) ) {
528 perform_soft_reset();
529 ret = -6;
530 /* no point retrying IDNF, sector no. was invalid */
531 if (error & ERROR_IDNF)
532 break;
533 goto retry;
536 buf += sectors * SECTOR_SIZE; /* Advance one chunk of sectors */
537 count -= sectors;
539 last_disk_activity = current_tick;
543 if(!ret && !wait_for_end_of_transfer()) {
544 int error;
546 error = ATA_ERROR;
547 perform_soft_reset();
548 ret = -4;
549 /* no point retrying IDNF, sector no. was invalid */
550 if (error & ERROR_IDNF)
551 break;
552 goto retry;
554 break;
557 error:
558 ata_led(false);
559 #ifndef MAX_PHYS_SECTOR_SIZE
560 mutex_unlock(&ata_mtx);
561 #endif
563 return ret;
566 #ifndef MAX_PHYS_SECTOR_SIZE
567 int ata_read_sectors(IF_MD2(int drive,)
568 unsigned long start,
569 int incount,
570 void* inbuf)
572 #ifdef HAVE_MULTIDRIVE
573 (void)drive; /* unused for now */
574 #endif
576 return ata_transfer_sectors(start, incount, inbuf, false);
578 #endif
580 #ifndef MAX_PHYS_SECTOR_SIZE
581 int ata_write_sectors(IF_MD2(int drive,)
582 unsigned long start,
583 int count,
584 const void* buf)
586 #ifdef HAVE_MULTIDRIVE
587 (void)drive; /* unused for now */
588 #endif
590 return ata_transfer_sectors(start, count, (void*)buf, true);
592 #endif
594 #ifdef MAX_PHYS_SECTOR_SIZE
595 static int cache_sector(unsigned long sector)
597 int rc;
599 sector &= ~(phys_sector_mult - 1);
600 /* round down to physical sector boundary */
602 /* check whether the sector is already cached */
603 if (sector_cache.inuse && (sector_cache.sectornum == sector))
604 return 0;
606 /* not found: read the sector */
607 sector_cache.inuse = false;
608 rc = ata_transfer_sectors(sector, phys_sector_mult, sector_cache.data, false);
609 if (!rc)
611 sector_cache.sectornum = sector;
612 sector_cache.inuse = true;
614 return rc;
617 static inline int flush_current_sector(void)
619 return ata_transfer_sectors(sector_cache.sectornum, phys_sector_mult,
620 sector_cache.data, true);
623 int ata_read_sectors(IF_MD2(int drive,)
624 unsigned long start,
625 int incount,
626 void* inbuf)
628 int rc = 0;
629 int offset;
631 #ifdef HAVE_MULTIDRIVE
632 (void)drive; /* unused for now */
633 #endif
634 mutex_lock(&ata_mtx);
636 offset = start & (phys_sector_mult - 1);
638 if (offset) /* first partial sector */
640 int partcount = MIN(incount, phys_sector_mult - offset);
642 rc = cache_sector(start);
643 if (rc)
645 rc = rc * 10 - 1;
646 goto error;
648 memcpy(inbuf, sector_cache.data + offset * SECTOR_SIZE,
649 partcount * SECTOR_SIZE);
651 start += partcount;
652 inbuf += partcount * SECTOR_SIZE;
653 incount -= partcount;
655 if (incount)
657 offset = incount & (phys_sector_mult - 1);
658 incount -= offset;
660 if (incount)
662 rc = ata_transfer_sectors(start, incount, inbuf, false);
663 if (rc)
665 rc = rc * 10 - 2;
666 goto error;
668 start += incount;
669 inbuf += incount * SECTOR_SIZE;
671 if (offset)
673 rc = cache_sector(start);
674 if (rc)
676 rc = rc * 10 - 3;
677 goto error;
679 memcpy(inbuf, sector_cache.data, offset * SECTOR_SIZE);
683 error:
684 mutex_unlock(&ata_mtx);
686 return rc;
689 int ata_write_sectors(IF_MD2(int drive,)
690 unsigned long start,
691 int count,
692 const void* buf)
694 int rc = 0;
695 int offset;
697 #ifdef HAVE_MULTIDRIVE
698 (void)drive; /* unused for now */
699 #endif
700 mutex_lock(&ata_mtx);
702 offset = start & (phys_sector_mult - 1);
704 if (offset) /* first partial sector */
706 int partcount = MIN(count, phys_sector_mult - offset);
708 rc = cache_sector(start);
709 if (rc)
711 rc = rc * 10 - 1;
712 goto error;
714 memcpy(sector_cache.data + offset * SECTOR_SIZE, buf,
715 partcount * SECTOR_SIZE);
716 rc = flush_current_sector();
717 if (rc)
719 rc = rc * 10 - 2;
720 goto error;
722 start += partcount;
723 buf += partcount * SECTOR_SIZE;
724 count -= partcount;
726 if (count)
728 offset = count & (phys_sector_mult - 1);
729 count -= offset;
731 if (count)
733 rc = ata_transfer_sectors(start, count, (void*)buf, true);
734 if (rc)
736 rc = rc * 10 - 3;
737 goto error;
739 start += count;
740 buf += count * SECTOR_SIZE;
742 if (offset)
744 rc = cache_sector(start);
745 if (rc)
747 rc = rc * 10 - 4;
748 goto error;
750 memcpy(sector_cache.data, buf, offset * SECTOR_SIZE);
751 rc = flush_current_sector();
752 if (rc)
754 rc = rc * 10 - 5;
755 goto error;
760 error:
761 mutex_unlock(&ata_mtx);
763 return rc;
765 #endif /* MAX_PHYS_SECTOR_SIZE */
767 static int check_registers(void)
769 int i;
770 if ( ATA_STATUS & STATUS_BSY )
771 return -1;
773 for (i = 0; i<64; i++) {
774 SET_REG(ATA_NSECTOR, WRITE_PATTERN1);
775 SET_REG(ATA_SECTOR, WRITE_PATTERN2);
776 SET_REG(ATA_LCYL, WRITE_PATTERN3);
777 SET_REG(ATA_HCYL, WRITE_PATTERN4);
779 if (((ATA_NSECTOR & READ_PATTERN1_MASK) == READ_PATTERN1) &&
780 ((ATA_SECTOR & READ_PATTERN2_MASK) == READ_PATTERN2) &&
781 ((ATA_LCYL & READ_PATTERN3_MASK) == READ_PATTERN3) &&
782 ((ATA_HCYL & READ_PATTERN4_MASK) == READ_PATTERN4))
783 return 0;
785 return -2;
788 static int freeze_lock(void)
790 /* does the disk support Security Mode feature set? */
791 if (identify_info[82] & 2)
793 SET_REG(ATA_SELECT, ata_device);
795 if (!wait_for_rdy())
796 return -1;
798 SET_REG(ATA_COMMAND, CMD_SECURITY_FREEZE_LOCK);
800 if (!wait_for_rdy())
801 return -2;
804 return 0;
807 void ata_spindown(int seconds)
809 sleep_timeout = seconds * HZ;
812 bool ata_disk_is_active(void)
814 return !sleeping;
817 static int ata_perform_sleep(void)
819 /* guard against calls made with checks of these variables outside
820 the mutex that may not be on the ata thread; status may have changed. */
821 if (spinup || sleeping) {
822 return 0;
825 SET_REG(ATA_SELECT, ata_device);
827 if(!wait_for_rdy()) {
828 DEBUGF("ata_perform_sleep() - not RDY\n");
829 return -1;
832 SET_REG(ATA_COMMAND, CMD_SLEEP);
834 if (!wait_for_rdy())
836 DEBUGF("ata_perform_sleep() - CMD failed\n");
837 return -2;
840 sleeping = true;
841 return 0;
844 void ata_sleep(void)
846 queue_post(&ata_queue, Q_SLEEP, 0);
849 void ata_sleepnow(void)
851 if (!spinup && !sleeping && initialized)
853 call_storage_idle_notifys(false);
854 mutex_lock(&ata_mtx);
855 ata_perform_sleep();
856 mutex_unlock(&ata_mtx);
860 void ata_spin(void)
862 last_user_activity = current_tick;
865 static void ata_thread(void)
867 static long last_sleep = 0;
868 struct queue_event ev;
869 #ifdef ALLOW_USB_SPINDOWN
870 static bool usb_mode = false;
871 #endif
873 while (1) {
874 queue_wait_w_tmo(&ata_queue, &ev, HZ/2);
876 switch ( ev.id ) {
877 case SYS_TIMEOUT:
878 if (!spinup && !sleeping)
880 if (TIME_AFTER( current_tick,
881 last_disk_activity + (HZ*2) ) )
883 #ifdef ALLOW_USB_SPINDOWN
884 if(!usb_mode)
885 #endif
887 call_storage_idle_notifys(false);
891 if ( sleep_timeout &&
892 TIME_AFTER( current_tick,
893 last_user_activity + sleep_timeout ) &&
894 TIME_AFTER( current_tick,
895 last_disk_activity + sleep_timeout ) )
897 #ifdef ALLOW_USB_SPINDOWN
898 if(!usb_mode)
899 #endif
901 call_storage_idle_notifys(true);
903 mutex_lock(&ata_mtx);
904 ata_perform_sleep();
905 last_sleep = current_tick;
906 mutex_unlock(&ata_mtx);
910 #ifdef HAVE_ATA_POWER_OFF
911 if ( !spinup && sleeping && !poweroff &&
912 TIME_AFTER( current_tick, last_sleep + ATA_POWER_OFF_TIMEOUT ))
914 mutex_lock(&ata_mtx);
915 ide_power_enable(false);
916 poweroff = true;
917 mutex_unlock(&ata_mtx);
919 #endif
920 break;
922 #ifndef USB_NONE
923 case SYS_USB_CONNECTED:
924 /* Tell the USB thread that we are safe */
925 DEBUGF("ata_thread got SYS_USB_CONNECTED\n");
926 #ifdef ALLOW_USB_SPINDOWN
927 usb_mode = true;
928 usb_acknowledge(SYS_USB_CONNECTED_ACK);
929 /* There is no need to force ATA power on */
930 #else
931 mutex_lock(&ata_mtx);
932 if (sleeping) {
933 ata_led(true);
934 sleeping = false; /* set this now since it'll be on */
936 if (poweroff) {
937 ata_power_on();
938 poweroff = false;
940 else {
941 perform_soft_reset();
944 ata_led(false);
946 mutex_unlock(&ata_mtx);
948 /* Wait until the USB cable is extracted again */
949 usb_acknowledge(SYS_USB_CONNECTED_ACK);
950 usb_wait_for_disconnect(&ata_queue);
951 #endif
952 break;
954 #ifdef ALLOW_USB_SPINDOWN
955 case SYS_USB_DISCONNECTED:
956 /* Tell the USB thread that we are ready again */
957 DEBUGF("ata_thread got SYS_USB_DISCONNECTED\n");
958 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
959 usb_mode = false;
960 break;
961 #endif
962 #endif /* USB_NONE */
964 case Q_SLEEP:
965 #ifdef ALLOW_USB_SPINDOWN
966 if(!usb_mode)
967 #endif
969 call_storage_idle_notifys(false);
971 last_disk_activity = current_tick - sleep_timeout + (HZ/2);
972 break;
974 #ifdef ATA_DRIVER_CLOSE
975 case Q_CLOSE:
976 return;
977 #endif
982 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
983 static int ata_hard_reset(void)
985 int ret;
987 mutex_lock(&ata_mtx);
989 ata_reset();
991 /* state HRR2 */
992 SET_REG(ATA_SELECT, ata_device); /* select the right device */
993 ret = wait_for_bsy();
995 /* Massage the return code so it is 0 on success and -1 on failure */
996 ret = ret?0:-1;
998 mutex_unlock(&ata_mtx);
1000 return ret;
1003 static int perform_soft_reset(void)
1005 /* If this code is allowed to run on a Nano, the next reads from the flash will
1006 * time out, so we disable it. It shouldn't be necessary anyway, since the
1007 * ATA -> Flash interface automatically sleeps almost immediately after the
1008 * last command.
1010 int ret;
1011 int retry_count;
1013 SET_REG(ATA_SELECT, SELECT_LBA | ata_device );
1014 SET_REG(ATA_CONTROL, CONTROL_nIEN|CONTROL_SRST );
1015 sleep(1); /* >= 5us */
1017 #ifdef HAVE_ATA_DMA
1018 /* DMA requires INTRQ be enabled */
1019 SET_REG(ATA_CONTROL, 0);
1020 #else
1021 SET_REG(ATA_CONTROL, CONTROL_nIEN);
1022 #endif
1023 sleep(1); /* >2ms */
1025 /* This little sucker can take up to 30 seconds */
1026 retry_count = 8;
1029 ret = wait_for_rdy();
1030 } while(!ret && retry_count--);
1032 if (!ret)
1033 return -1;
1035 if (set_features())
1036 return -2;
1038 if (set_multiple_mode(multisectors))
1039 return -3;
1041 if (freeze_lock())
1042 return -4;
1044 return 0;
1047 int ata_soft_reset(void)
1049 int ret;
1051 mutex_lock(&ata_mtx);
1053 ret = perform_soft_reset();
1055 mutex_unlock(&ata_mtx);
1056 return ret;
1059 static int ata_power_on(void)
1061 int rc;
1063 ide_power_enable(true);
1064 sleep(HZ/4); /* allow voltage to build up */
1066 /* Accessing the PP IDE controller too early after powering up the disk
1067 * makes the core hang for a short time, causing an audio dropout. This
1068 * also depends on the disk; iPod Mini G2 needs at least HZ/5 to get rid
1069 * of the dropout. Since this time isn't additive (the wait_for_bsy() in
1070 * ata_hard_reset() will shortened by the same amount), it's a good idea
1071 * to do this on all HDD based targets. */
1073 if( ata_hard_reset() )
1074 return -1;
1076 rc = set_features();
1077 if (rc)
1078 return rc * 10 - 2;
1080 if (set_multiple_mode(multisectors))
1081 return -3;
1083 if (freeze_lock())
1084 return -4;
1086 return 0;
1089 static int master_slave_detect(void)
1091 /* master? */
1092 SET_REG(ATA_SELECT, 0);
1093 if ( ATA_STATUS & (STATUS_RDY|STATUS_BSY) ) {
1094 ata_device = 0;
1095 DEBUGF("Found master harddisk\n");
1097 else {
1098 /* slave? */
1099 SET_REG(ATA_SELECT, SELECT_DEVICE1);
1100 if ( ATA_STATUS & (STATUS_RDY|STATUS_BSY) ) {
1101 ata_device = SELECT_DEVICE1;
1102 DEBUGF("Found slave harddisk\n");
1104 else
1105 return -1;
1107 return 0;
1110 static int identify(void)
1112 int i;
1114 SET_REG(ATA_SELECT, ata_device);
1116 if(!wait_for_rdy()) {
1117 DEBUGF("identify() - not RDY\n");
1118 return -1;
1120 SET_REG(ATA_COMMAND, CMD_IDENTIFY);
1122 if (!wait_for_start_of_transfer())
1124 DEBUGF("identify() - CMD failed\n");
1125 return -2;
1128 for (i=0; i<SECTOR_SIZE/2; i++) {
1129 /* the IDENTIFY words are already swapped, so we need to treat
1130 this info differently that normal sector data */
1131 #if defined(ROCKBOX_BIG_ENDIAN) && !defined(ATA_SWAP_WORDS)
1132 identify_info[i] = swap16(ATA_DATA);
1133 #else
1134 identify_info[i] = ATA_DATA;
1135 #endif
1138 return 0;
1141 static int set_multiple_mode(int sectors)
1143 SET_REG(ATA_SELECT, ata_device);
1145 if(!wait_for_rdy()) {
1146 DEBUGF("set_multiple_mode() - not RDY\n");
1147 return -1;
1150 SET_REG(ATA_NSECTOR, sectors);
1151 SET_REG(ATA_COMMAND, CMD_SET_MULTIPLE_MODE);
1153 if (!wait_for_rdy())
1155 DEBUGF("set_multiple_mode() - CMD failed\n");
1156 return -2;
1159 return 0;
1162 #ifdef HAVE_ATA_DMA
1163 static int get_best_mode(unsigned short identword, int max, int modetype)
1165 unsigned short testbit = BIT_N(max);
1167 while (1) {
1168 if (identword & testbit)
1169 return max | modetype;
1170 testbit >>= 1;
1171 if (!testbit)
1172 return 0;
1173 max--;
1176 #endif
1178 static int set_features(void)
1180 static struct {
1181 unsigned char id_word;
1182 unsigned char id_bit;
1183 unsigned char subcommand;
1184 unsigned char parameter;
1185 } features[] = {
1186 { 83, 14, 0x03, 0 }, /* force PIO mode */
1187 { 83, 3, 0x05, 0x80 }, /* adv. power management: lowest w/o standby */
1188 { 83, 9, 0x42, 0x80 }, /* acoustic management: lowest noise */
1189 { 82, 6, 0xaa, 0 }, /* enable read look-ahead */
1190 #ifdef HAVE_ATA_DMA
1191 { 0, 0, 0x03, 0 }, /* DMA mode */
1192 #endif
1194 int i;
1195 int pio_mode = 2;
1197 /* Find out the highest supported PIO mode */
1198 if(identify_info[64] & 2)
1199 pio_mode = 4;
1200 else
1201 if(identify_info[64] & 1)
1202 pio_mode = 3;
1204 /* Update the table: set highest supported pio mode that we also support */
1205 features[0].parameter = 8 + pio_mode;
1207 #ifdef HAVE_ATA_DMA
1208 if (identify_info[53] & (1<<2))
1209 /* Ultra DMA mode info present, find a mode */
1210 dma_mode = get_best_mode(identify_info[88], ATA_MAX_UDMA, 0x40);
1212 if (!dma_mode) {
1213 /* No UDMA mode found, try to find a multi-word DMA mode */
1214 dma_mode = get_best_mode(identify_info[63], ATA_MAX_MWDMA, 0x20);
1215 features[4].id_word = 63;
1217 else
1218 features[4].id_word = 88;
1220 features[4].id_bit = dma_mode & 7;
1221 features[4].parameter = dma_mode;
1222 #endif /* HAVE_ATA_DMA */
1224 SET_REG(ATA_SELECT, ata_device);
1226 if (!wait_for_rdy()) {
1227 DEBUGF("set_features() - not RDY\n");
1228 return -1;
1231 for (i=0; i < (int)(sizeof(features)/sizeof(features[0])); i++) {
1232 if (identify_info[features[i].id_word] & BIT_N(features[i].id_bit)) {
1233 SET_REG(ATA_FEATURE, features[i].subcommand);
1234 SET_REG(ATA_NSECTOR, features[i].parameter);
1235 SET_REG(ATA_COMMAND, CMD_SET_FEATURES);
1237 if (!wait_for_rdy()) {
1238 DEBUGF("set_features() - CMD failed\n");
1239 return -10 - i;
1242 if((ATA_ALT_STATUS & STATUS_ERR) && (i != 1)) {
1243 /* some CF cards don't like advanced powermanagement
1244 even if they mark it as supported - go figure... */
1245 if(ATA_ERROR & ERROR_ABRT) {
1246 return -20 - i;
1252 #ifdef ATA_SET_DEVICE_FEATURES
1253 ata_set_pio_timings(pio_mode);
1254 #endif
1256 #ifdef HAVE_ATA_DMA
1257 ata_dma_set_mode(dma_mode);
1258 #endif
1260 return 0;
1263 unsigned short* ata_get_identify(void)
1265 return identify_info;
1268 static int init_and_check(bool hard_reset)
1270 int rc;
1272 if (hard_reset)
1274 /* This should reset both master and slave, we don't yet know what's in */
1275 ata_device = 0;
1276 if (ata_hard_reset())
1277 return -1;
1280 rc = master_slave_detect();
1281 if (rc)
1282 return -10 + rc;
1284 /* symptom fix: else check_registers() below may fail */
1285 if (hard_reset && !wait_for_bsy())
1286 return -20;
1288 rc = check_registers();
1289 if (rc)
1290 return -30 + rc;
1292 return 0;
1295 int ata_init(void)
1297 int rc = 0;
1298 bool coldstart;
1300 if ( !initialized ) {
1301 mutex_init(&ata_mtx);
1302 queue_init(&ata_queue, true);
1305 mutex_lock(&ata_mtx);
1307 /* must be called before ata_device_init() */
1308 coldstart = ata_is_coldstart();
1309 ata_led(false);
1310 ata_device_init();
1311 sleeping = false;
1312 ata_enable(true);
1313 #ifdef MAX_PHYS_SECTOR_SIZE
1314 memset(&sector_cache, 0, sizeof(sector_cache));
1315 #endif
1317 if ( !initialized ) {
1318 /* First call won't have multiple thread contention - this
1319 * may return at any point without having to unlock */
1320 mutex_unlock(&ata_mtx);
1322 if (!ide_powered()) /* somebody has switched it off */
1324 ide_power_enable(true);
1325 sleep(HZ/4); /* allow voltage to build up */
1328 #ifdef HAVE_ATA_DMA
1329 /* DMA requires INTRQ be enabled */
1330 SET_REG(ATA_CONTROL, 0);
1331 #endif
1333 /* first try, hard reset at cold start only */
1334 rc = init_and_check(coldstart);
1336 if (rc)
1337 { /* failed? -> second try, always with hard reset */
1338 DEBUGF("ata: init failed, retrying...\n");
1339 rc = init_and_check(true);
1340 if (rc)
1341 return rc;
1344 rc = identify();
1346 if (rc)
1347 return -40 + rc;
1349 multisectors = identify_info[47] & 0xff;
1350 if (multisectors == 0) /* Invalid multisector info, try with 16 */
1351 multisectors = 16;
1353 DEBUGF("ata: %d sectors per ata request\n",multisectors);
1355 total_sectors = identify_info[60] | (identify_info[61] << 16);
1357 #ifdef HAVE_LBA48
1358 if (identify_info[83] & 0x0400 /* 48 bit address support */
1359 && total_sectors == 0x0FFFFFFF) /* and disk size >= 128 GiB */
1360 { /* (needs BigLBA addressing) */
1361 if (identify_info[102] || identify_info[103])
1362 panicf("Unsupported disk size: >= 2^32 sectors");
1364 total_sectors = identify_info[100] | (identify_info[101] << 16);
1365 lba48 = true; /* use BigLBA */
1367 #endif
1368 rc = freeze_lock();
1370 if (rc)
1371 return -50 + rc;
1373 rc = set_features();
1374 if (rc)
1375 return -60 + rc;
1377 #ifdef MAX_PHYS_SECTOR_SIZE
1378 /* Find out the physical sector size */
1379 if((identify_info[106] & 0xe000) == 0x6000)
1380 phys_sector_mult = BIT_N(identify_info[106] & 0x000f);
1381 else
1382 phys_sector_mult = 1;
1384 DEBUGF("ata: %d logical sectors per phys sector", phys_sector_mult);
1386 if (phys_sector_mult > 1)
1388 /* Check if drive really needs emulation - if we can access
1389 * sector 1 then assume the drive will handle it better than
1390 * us, and ignore the large physical sectors.
1392 char throwaway[SECTOR_SIZE];
1393 rc = ata_transfer_sectors(1, 1, &throwaway, false);
1394 if (rc == 0)
1395 phys_sector_mult = 1;
1398 if (phys_sector_mult > (MAX_PHYS_SECTOR_SIZE/SECTOR_SIZE))
1399 panicf("Unsupported physical sector size: %d",
1400 phys_sector_mult * SECTOR_SIZE);
1401 #endif
1403 mutex_lock(&ata_mtx); /* Balance unlock below */
1405 last_disk_activity = current_tick;
1406 #ifdef ATA_DRIVER_CLOSE
1407 ata_thread_id =
1408 #endif
1409 create_thread(ata_thread, ata_stack,
1410 sizeof(ata_stack), 0, ata_thread_name
1411 IF_PRIO(, PRIORITY_USER_INTERFACE)
1412 IF_COP(, CPU));
1413 initialized = true;
1416 rc = set_multiple_mode(multisectors);
1417 if (rc)
1418 rc = -70 + rc;
1420 mutex_unlock(&ata_mtx);
1421 return rc;
1424 #ifdef ATA_DRIVER_CLOSE
1425 void ata_close(void)
1427 unsigned int thread_id = ata_thread_id;
1429 if (thread_id == 0)
1430 return;
1432 ata_thread_id = 0;
1434 queue_post(&ata_queue, Q_CLOSE, 0);
1435 thread_wait(thread_id);
1437 #endif /* ATA_DRIVER_CLOSE */
1439 #if (CONFIG_LED == LED_REAL)
1440 void ata_set_led_enabled(bool enabled)
1442 ata_led_enabled = enabled;
1443 if (ata_led_enabled)
1444 led(ata_led_on);
1445 else
1446 led(false);
1448 #endif
1450 long ata_last_disk_activity(void)
1452 return last_disk_activity;
1455 int ata_spinup_time(void)
1457 return spinup_time;
1460 #ifdef STORAGE_GET_INFO
1461 void ata_get_info(IF_MD2(int drive,)struct storage_info *info)
1463 unsigned short *src,*dest;
1464 static char vendor[8];
1465 static char product[16];
1466 static char revision[4];
1467 #ifdef HAVE_MULTIDRIVE
1468 (void)drive; /* unused for now */
1469 #endif
1470 int i;
1471 info->sector_size = SECTOR_SIZE;
1472 info->num_sectors= total_sectors;
1474 src = (unsigned short*)&identify_info[27];
1475 dest = (unsigned short*)vendor;
1476 for (i=0;i<4;i++)
1477 dest[i] = htobe16(src[i]);
1478 info->vendor=vendor;
1480 src = (unsigned short*)&identify_info[31];
1481 dest = (unsigned short*)product;
1482 for (i=0;i<8;i++)
1483 dest[i] = htobe16(src[i]);
1484 info->product=product;
1486 src = (unsigned short*)&identify_info[23];
1487 dest = (unsigned short*)revision;
1488 for (i=0;i<2;i++)
1489 dest[i] = htobe16(src[i]);
1490 info->revision=revision;
1492 #endif
1494 #ifdef HAVE_ATA_DMA
1495 /* Returns last DMA mode as set by set_features() */
1496 int ata_get_dma_mode(void)
1498 return dma_mode;
1501 /* Needed to allow updating while waiting for DMA to complete */
1502 void ata_keep_active(void)
1504 last_disk_activity = current_tick;
1506 #endif
1508 #ifdef CONFIG_STORAGE_MULTI
1509 int ata_num_drives(int first_drive)
1511 /* We don't care which logical drive number(s) we have been assigned */
1512 (void)first_drive;
1514 return 1;
1516 #endif