/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id$ * * Copyright (C) 2002 by Alan Korr * * All files in this archive are subject to the GNU General Public License. * See the file COPYING in the source tree root for full license agreement. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ****************************************************************************/ #include #include "ata.h" #include "kernel.h" #include "thread.h" #include "led.h" #include "cpu.h" #include "system.h" #include "debug.h" #include "panic.h" #include "usb.h" #include "power.h" #include "string.h" #include "ata_idle_notify.h" #include "ata-target.h" #define SECTOR_SIZE (512) #define ATA_FEATURE ATA_ERROR #define ATA_STATUS ATA_COMMAND #define ATA_ALT_STATUS ATA_CONTROL #define SELECT_DEVICE1 0x10 #define SELECT_LBA 0x40 #define CONTROL_nIEN 0x02 #define CONTROL_SRST 0x04 #define CMD_READ_SECTORS 0x20 #define CMD_WRITE_SECTORS 0x30 #define CMD_WRITE_SECTORS_EXT 0x34 #define CMD_READ_MULTIPLE 0xC4 #define CMD_READ_MULTIPLE_EXT 0x29 #define CMD_WRITE_MULTIPLE 0xC5 #define CMD_SET_MULTIPLE_MODE 0xC6 #define CMD_STANDBY_IMMEDIATE 0xE0 #define CMD_STANDBY 0xE2 #define CMD_IDENTIFY 0xEC #define CMD_SLEEP 0xE6 #define CMD_SET_FEATURES 0xEF #define CMD_SECURITY_FREEZE_LOCK 0xF5 #define Q_SLEEP 0 #define READ_TIMEOUT 5*HZ #ifdef HAVE_ATA_POWER_OFF #define ATA_POWER_OFF_TIMEOUT 2*HZ #endif static struct mutex ata_mtx; int ata_device; /* device 0 (master) or 1 (slave) */ int ata_spinup_time = 0; #if (CONFIG_LED == LED_REAL) static bool ata_led_enabled = true; static bool ata_led_on = false; #endif static bool spinup = false; static bool sleeping = true; static bool poweroff = false; static long sleep_timeout = 5*HZ; #ifdef HAVE_LBA48 static bool lba48 = false; /* set for 48 bit addressing */ #endif static long ata_stack[(DEFAULT_STACK_SIZE*3)/sizeof(long)]; static const char ata_thread_name[] = "ata"; static struct event_queue ata_queue; static bool initialized = false; static long last_user_activity = -1; long last_disk_activity = -1; static int multisectors; /* number of supported multisectors */ static unsigned short identify_info[SECTOR_SIZE]; #ifdef MAX_PHYS_SECTOR_SIZE struct sector_cache_entry { bool inuse; unsigned long sectornum; /* logical sector */ unsigned char data[MAX_PHYS_SECTOR_SIZE]; }; /* buffer for reading and writing large physical sectors */ #define NUMCACHES 2 static struct sector_cache_entry sector_cache; static int phys_sector_mult = 1; #endif static int ata_power_on(void); static int perform_soft_reset(void); static int set_multiple_mode(int sectors); static int set_features(void); STATICIRAM int wait_for_bsy(void) ICODE_ATTR; STATICIRAM int wait_for_bsy(void) { long timeout = current_tick + HZ*30; while (TIME_BEFORE(current_tick, timeout) && (ATA_STATUS & STATUS_BSY)) { last_disk_activity = current_tick; priority_yield(); } if (TIME_BEFORE(current_tick, timeout)) return 1; else return 0; /* timeout */ } STATICIRAM int wait_for_rdy(void) ICODE_ATTR; STATICIRAM int wait_for_rdy(void) { long timeout; if (!wait_for_bsy()) return 0; timeout = current_tick + HZ*10; while (TIME_BEFORE(current_tick, timeout) && !(ATA_ALT_STATUS & STATUS_RDY)) { last_disk_activity = current_tick; priority_yield(); } if (TIME_BEFORE(current_tick, timeout)) return STATUS_RDY; else return 0; /* timeout */ } STATICIRAM int wait_for_start_of_transfer(void) ICODE_ATTR; STATICIRAM int wait_for_start_of_transfer(void) { if (!wait_for_bsy()) return 0; return (ATA_ALT_STATUS & (STATUS_BSY|STATUS_DRQ)) == STATUS_DRQ; } STATICIRAM int wait_for_end_of_transfer(void) ICODE_ATTR; STATICIRAM int wait_for_end_of_transfer(void) { if (!wait_for_bsy()) return 0; return (ATA_ALT_STATUS & (STATUS_RDY|STATUS_DRQ)) == STATUS_RDY; } #if (CONFIG_LED == LED_REAL) /* Conditionally block LED access for the ATA driver, so the LED can be * (mis)used for other purposes */ static void ata_led(bool on) { ata_led_on = on; if (ata_led_enabled) led(ata_led_on); } #else #define ata_led(on) led(on) #endif #ifndef ATA_OPTIMIZED_READING STATICIRAM void copy_read_sectors(unsigned char* buf, int wordcount) ICODE_ATTR; STATICIRAM void copy_read_sectors(unsigned char* buf, int wordcount) { unsigned short tmp = 0; if ( (unsigned long)buf & 1) { /* not 16-bit aligned, copy byte by byte */ unsigned char* bufend = buf + wordcount*2; do { tmp = ATA_DATA; #if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN) *buf++ = tmp & 0xff; /* I assume big endian */ *buf++ = tmp >> 8; /* and don't use the SWAB16 macro */ #else *buf++ = tmp >> 8; *buf++ = tmp & 0xff; #endif } while (buf < bufend); /* tail loop is faster */ } else { /* 16-bit aligned, can do faster copy */ unsigned short* wbuf = (unsigned short*)buf; unsigned short* wbufend = wbuf + wordcount; do { #ifdef SWAP_WORDS *wbuf = swap16(ATA_DATA); #else *wbuf = ATA_DATA; #endif } while (++wbuf < wbufend); /* tail loop is faster */ } } #endif /* !ATA_OPTIMIZED_READING */ #ifdef MAX_PHYS_SECTOR_SIZE static int _read_sectors(unsigned long start, int incount, void* inbuf) #else int ata_read_sectors(IF_MV2(int drive,) unsigned long start, int incount, void* inbuf) #endif { int ret = 0; long timeout; int count; void* buf; long spinup_start; #ifndef MAX_PHYS_SECTOR_SIZE #ifdef HAVE_MULTIVOLUME (void)drive; /* unused for now */ #endif spinlock_lock(&ata_mtx); #endif last_disk_activity = current_tick; spinup_start = current_tick; ata_led(true); if ( sleeping ) { spinup = true; if (poweroff) { if (ata_power_on()) { spinlock_unlock(&ata_mtx); ata_led(false); return -1; } } else { if (perform_soft_reset()) { spinlock_unlock(&ata_mtx); ata_led(false); return -1; } } } timeout = current_tick + READ_TIMEOUT; SET_REG(ATA_SELECT, ata_device); if (!wait_for_rdy()) { spinlock_unlock(&ata_mtx); ata_led(false); return -2; } retry: buf = inbuf; count = incount; while (TIME_BEFORE(current_tick, timeout)) { ret = 0; last_disk_activity = current_tick; #ifdef HAVE_LBA48 if (lba48) { SET_REG(ATA_NSECTOR, count >> 8); SET_REG(ATA_NSECTOR, count & 0xff); SET_REG(ATA_SECTOR, (start >> 24) & 0xff); /* 31:24 */ SET_REG(ATA_SECTOR, start & 0xff); /* 7:0 */ SET_REG(ATA_LCYL, 0); /* 39:32 */ SET_REG(ATA_LCYL, (start >> 8) & 0xff); /* 15:8 */ SET_REG(ATA_HCYL, 0); /* 47:40 */ SET_REG(ATA_HCYL, (start >> 16) & 0xff); /* 23:16 */ SET_REG(ATA_SELECT, SELECT_LBA | ata_device); SET_REG(ATA_COMMAND, CMD_READ_MULTIPLE_EXT); } else #endif { SET_REG(ATA_NSECTOR, count & 0xff); /* 0 means 256 sectors */ SET_REG(ATA_SECTOR, start & 0xff); SET_REG(ATA_LCYL, (start >> 8) & 0xff); SET_REG(ATA_HCYL, (start >> 16) & 0xff); SET_REG(ATA_SELECT, ((start >> 24) & 0xf) | SELECT_LBA | ata_device); SET_REG(ATA_COMMAND, CMD_READ_MULTIPLE); } /* wait at least 400ns between writing command and reading status */ __asm__ volatile ("nop"); __asm__ volatile ("nop"); __asm__ volatile ("nop"); __asm__ volatile ("nop"); __asm__ volatile ("nop"); while (count) { int sectors; int wordcount; int status; if (!wait_for_start_of_transfer()) { /* We have timed out waiting for RDY and/or DRQ, possibly because the hard drive is shaking and has problems reading the data. We have two options: 1) Wait some more 2) Perform a soft reset and try again. We choose alternative 2. */ perform_soft_reset(); ret = -4; goto retry; } if (spinup) { ata_spinup_time = current_tick - spinup_start; spinup = false; sleeping = false; poweroff = false; } /* read the status register exactly once per loop */ status = ATA_STATUS; if (count >= multisectors ) sectors = multisectors; else sectors = count; wordcount = sectors * SECTOR_SIZE / 2; copy_read_sectors(buf, wordcount); /* "Device errors encountered during READ MULTIPLE commands are posted at the beginning of the block or partial block transfer, but the DRQ bit is still set to one and the data transfer shall take place, including transfer of corrupted data, if any." -- ATA specification */ if ( status & (STATUS_BSY | STATUS_ERR | STATUS_DF) ) { perform_soft_reset(); ret = -5; goto retry; } buf += sectors * SECTOR_SIZE; /* Advance one chunk of sectors */ count -= sectors; last_disk_activity = current_tick; } if(!ret && !wait_for_end_of_transfer()) { perform_soft_reset(); ret = -3; goto retry; } break; } ata_led(false); #ifndef MAX_PHYS_SECTOR_SIZE spinlock_unlock(&ata_mtx); #endif return ret; } #ifndef ATA_OPTIMIZED_WRITING STATICIRAM void copy_write_sectors(const unsigned char* buf, int wordcount) ICODE_ATTR; STATICIRAM void copy_write_sectors(const unsigned char* buf, int wordcount) { if ( (unsigned long)buf & 1) { /* not 16-bit aligned, copy byte by byte */ unsigned short tmp = 0; const unsigned char* bufend = buf + wordcount*2; do { #if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN) tmp = (unsigned short) *buf++; tmp |= (unsigned short) *buf++ << 8; SET_16BITREG(ATA_DATA, tmp); #else tmp = (unsigned short) *buf++ << 8; tmp |= (unsigned short) *buf++; SET_16BITREG(ATA_DATA, tmp); #endif } while (buf < bufend); /* tail loop is faster */ } else { /* 16-bit aligned, can do faster copy */ unsigned short* wbuf = (unsigned short*)buf; unsigned short* wbufend = wbuf + wordcount; do { #ifdef SWAP_WORDS SET_16BITREG(ATA_DATA, swap16(*wbuf)); #else SET_16BITREG(ATA_DATA, *wbuf); #endif } while (++wbuf < wbufend); /* tail loop is faster */ } } #endif /* !ATA_OPTIMIZED_WRITING */ #ifdef MAX_PHYS_SECTOR_SIZE static int _write_sectors(unsigned long start, int count, const void* buf) #else int ata_write_sectors(IF_MV2(int drive,) unsigned long start, int count, const void* buf) #endif { int i; int ret = 0; long spinup_start; if (start == 0) panicf("Writing on sector 0\n"); #ifndef MAX_PHYS_SECTOR_SIZE #ifdef HAVE_MULTIVOLUME (void)drive; /* unused for now */ #endif spinlock_lock(&ata_mtx); #endif last_disk_activity = current_tick; spinup_start = current_tick; ata_led(true); if ( sleeping ) { spinup = true; if (poweroff) { if (ata_power_on()) { spinlock_unlock(&ata_mtx); ata_led(false); return -1; } } else { if (perform_soft_reset()) { spinlock_unlock(&ata_mtx); ata_led(false); return -1; } } } SET_REG(ATA_SELECT, ata_device); if (!wait_for_rdy()) { spinlock_unlock(&ata_mtx); ata_led(false); return -2; } #ifdef HAVE_LBA48 if (lba48) { SET_REG(ATA_NSECTOR, count >> 8); SET_REG(ATA_NSECTOR, count & 0xff); SET_REG(ATA_SECTOR, (start >> 24) & 0xff); /* 31:24 */ SET_REG(ATA_SECTOR, start & 0xff); /* 7:0 */ SET_REG(ATA_LCYL, 0); /* 39:32 */ SET_REG(ATA_LCYL, (start >> 8) & 0xff); /* 15:8 */ SET_REG(ATA_HCYL, 0); /* 47:40 */ SET_REG(ATA_HCYL, (start >> 16) & 0xff); /* 23:16 */ SET_REG(ATA_SELECT, SELECT_LBA | ata_device); SET_REG(ATA_COMMAND, CMD_WRITE_SECTORS_EXT); } else #endif { SET_REG(ATA_NSECTOR, count & 0xff); /* 0 means 256 sectors */ SET_REG(ATA_SECTOR, start & 0xff); SET_REG(ATA_LCYL, (start >> 8) & 0xff); SET_REG(ATA_HCYL, (start >> 16) & 0xff); SET_REG(ATA_SELECT, ((start >> 24) & 0xf) | SELECT_LBA | ata_device); SET_REG(ATA_COMMAND, CMD_WRITE_SECTORS); } for (i=0; i Flash interface automatically sleeps almost immediately after the * last command. */ #ifndef IPOD_NANO int ret; int retry_count; SET_REG(ATA_SELECT, SELECT_LBA | ata_device ); SET_REG(ATA_CONTROL, CONTROL_nIEN|CONTROL_SRST ); sleep(1); /* >= 5us */ SET_REG(ATA_CONTROL, CONTROL_nIEN); sleep(1); /* >2ms */ /* This little sucker can take up to 30 seconds */ retry_count = 8; do { ret = wait_for_rdy(); } while(!ret && retry_count--); /* Massage the return code so it is 0 on success and -1 on failure */ ret = ret?0:-1; return ret; #else return 0; /* Always report success */ #endif } int ata_soft_reset(void) { int ret; spinlock_lock(&ata_mtx); ret = perform_soft_reset(); spinlock_unlock(&ata_mtx); return ret; } static int ata_power_on(void) { int rc; ide_power_enable(true); if( ata_hard_reset() ) return -1; rc = set_features(); if (rc) return rc * 10 - 2; if (set_multiple_mode(multisectors)) return -3; if (freeze_lock()) return -4; return 0; } static int master_slave_detect(void) { /* master? */ SET_REG(ATA_SELECT, 0); if ( ATA_STATUS & (STATUS_RDY|STATUS_BSY) ) { ata_device = 0; DEBUGF("Found master harddisk\n"); } else { /* slave? */ SET_REG(ATA_SELECT, SELECT_DEVICE1); if ( ATA_STATUS & (STATUS_RDY|STATUS_BSY) ) { ata_device = SELECT_DEVICE1; DEBUGF("Found slave harddisk\n"); } else return -1; } return 0; } static int identify(void) { int i; SET_REG(ATA_SELECT, ata_device); if(!wait_for_rdy()) { DEBUGF("identify() - not RDY\n"); return -1; } SET_REG(ATA_COMMAND, CMD_IDENTIFY); if (!wait_for_start_of_transfer()) { DEBUGF("identify() - CMD failed\n"); return -2; } for (i=0; i second try, always with hard reset */ DEBUGF("ata: init failed, retrying...\n"); rc = init_and_check(true); if (rc) return rc; } rc = identify(); if (rc) return -40 + rc; multisectors = identify_info[47] & 0xff; if (multisectors == 0) /* Invalid multisector info, try with 16 */ multisectors = 16; DEBUGF("ata: %d sectors per ata request\n",multisectors); #ifdef MAX_PHYS_SECTOR_SIZE /* Find out the physical sector size */ if((identify_info[106] & 0xe000) == 0x6000) phys_sector_mult = 1 << (identify_info[106] & 0x000f); else phys_sector_mult = 1; DEBUGF("ata: %d logical sectors per phys sector", phys_sector_mult); if (phys_sector_mult > (MAX_PHYS_SECTOR_SIZE/SECTOR_SIZE)) panicf("Unsupported physical sector size: %d", phys_sector_mult * SECTOR_SIZE); #endif #ifdef HAVE_LBA48 if (identify_info[83] & 0x0400 /* 48 bit address support */ && identify_info[60] == 0xFFFF /* and disk size >= 128 GiB */ && identify_info[61] == 0x0FFF) /* (needs BigLBA addressing) */ { lba48 = true; /* use BigLBA */ } #endif rc = freeze_lock(); if (rc) return -50 + rc; rc = set_features(); if (rc) return -60 + rc; queue_init(&ata_queue, true); last_disk_activity = current_tick; create_thread(ata_thread, ata_stack, sizeof(ata_stack), ata_thread_name IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU, false)); initialized = true; } rc = set_multiple_mode(multisectors); if (rc) return -70 + rc; return 0; } #if (CONFIG_LED == LED_REAL) void ata_set_led_enabled(bool enabled) { ata_led_enabled = enabled; if (ata_led_enabled) led(ata_led_on); else led(false); } #endif