2002-04-21 22:06:12 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2002-05-13 19:22:38 +00:00
|
|
|
#include <stdbool.h>
|
2002-04-21 22:06:12 +00:00
|
|
|
#include "ata.h"
|
|
|
|
#include "kernel.h"
|
2002-07-16 14:07:47 +00:00
|
|
|
#include "thread.h"
|
2002-04-21 22:06:12 +00:00
|
|
|
#include "led.h"
|
2004-10-07 07:09:49 +00:00
|
|
|
#include "cpu.h"
|
2002-04-27 20:14:01 +00:00
|
|
|
#include "system.h"
|
2002-05-07 22:59:03 +00:00
|
|
|
#include "debug.h"
|
2002-06-18 12:53:02 +00:00
|
|
|
#include "panic.h"
|
2002-07-16 14:07:47 +00:00
|
|
|
#include "usb.h"
|
2002-08-01 12:00:03 +00:00
|
|
|
#include "power.h"
|
2002-08-15 12:54:52 +00:00
|
|
|
#include "string.h"
|
2006-11-06 14:24:18 +00:00
|
|
|
#include "ata_idle_notify.h"
|
2006-03-07 13:25:19 +00:00
|
|
|
#include "ata-target.h"
|
|
|
|
|
2005-02-22 09:55:40 +00:00
|
|
|
#define SECTOR_SIZE (512)
|
|
|
|
|
2004-09-17 11:28:07 +00:00
|
|
|
#define ATA_FEATURE ATA_ERROR
|
2006-08-12 08:01:54 +00:00
|
|
|
|
2004-09-17 11:28:07 +00:00
|
|
|
#define ATA_STATUS ATA_COMMAND
|
2002-04-27 14:19:00 +00:00
|
|
|
#define ATA_ALT_STATUS ATA_CONTROL
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-05-22 14:37:36 +00:00
|
|
|
#define SELECT_DEVICE1 0x10
|
2002-04-21 22:06:12 +00:00
|
|
|
#define SELECT_LBA 0x40
|
|
|
|
|
|
|
|
#define CONTROL_nIEN 0x02
|
|
|
|
#define CONTROL_SRST 0x04
|
|
|
|
|
|
|
|
#define CMD_READ_SECTORS 0x20
|
|
|
|
#define CMD_WRITE_SECTORS 0x30
|
2006-12-19 22:40:23 +00:00
|
|
|
#define CMD_WRITE_SECTORS_EXT 0x34
|
2002-09-05 15:25:08 +00:00
|
|
|
#define CMD_READ_MULTIPLE 0xC4
|
2006-12-19 22:40:23 +00:00
|
|
|
#define CMD_READ_MULTIPLE_EXT 0x29
|
2002-09-05 15:25:08 +00:00
|
|
|
#define CMD_WRITE_MULTIPLE 0xC5
|
|
|
|
#define CMD_SET_MULTIPLE_MODE 0xC6
|
2002-04-21 22:06:12 +00:00
|
|
|
#define CMD_STANDBY_IMMEDIATE 0xE0
|
|
|
|
#define CMD_STANDBY 0xE2
|
2002-09-05 15:25:08 +00:00
|
|
|
#define CMD_IDENTIFY 0xEC
|
2002-04-21 22:06:12 +00:00
|
|
|
#define CMD_SLEEP 0xE6
|
2004-01-14 13:15:19 +00:00
|
|
|
#define CMD_SET_FEATURES 0xEF
|
2002-04-21 22:06:12 +00:00
|
|
|
#define CMD_SECURITY_FREEZE_LOCK 0xF5
|
|
|
|
|
2002-07-16 14:07:47 +00:00
|
|
|
#define Q_SLEEP 0
|
|
|
|
|
2002-09-24 14:23:18 +00:00
|
|
|
#define READ_TIMEOUT 5*HZ
|
|
|
|
|
2007-01-23 15:43:37 +00:00
|
|
|
#ifdef HAVE_ATA_POWER_OFF
|
|
|
|
#define ATA_POWER_OFF_TIMEOUT 2*HZ
|
|
|
|
#endif
|
|
|
|
|
2008-01-18 13:12:33 +00:00
|
|
|
static struct mutex ata_mtx NOCACHEBSS_ATTR;
|
2006-12-03 22:13:44 +00:00
|
|
|
int ata_device; /* device 0 (master) or 1 (slave) */
|
2002-06-12 13:51:31 +00:00
|
|
|
|
2002-12-04 14:58:48 +00:00
|
|
|
int ata_spinup_time = 0;
|
2007-02-18 05:07:19 +00:00
|
|
|
#if (CONFIG_LED == LED_REAL)
|
2005-04-04 09:12:12 +00:00
|
|
|
static bool ata_led_enabled = true;
|
|
|
|
static bool ata_led_on = false;
|
2005-06-04 23:15:52 +00:00
|
|
|
#endif
|
2002-12-06 15:17:30 +00:00
|
|
|
static bool spinup = false;
|
2003-07-18 21:55:06 +00:00
|
|
|
static bool sleeping = true;
|
2002-11-27 15:55:47 +00:00
|
|
|
static bool poweroff = false;
|
2006-12-03 22:13:44 +00:00
|
|
|
static long sleep_timeout = 5*HZ;
|
2006-12-20 22:08:29 +00:00
|
|
|
#ifdef HAVE_LBA48
|
|
|
|
static bool lba48 = false; /* set for 48 bit addressing */
|
2006-12-19 22:40:23 +00:00
|
|
|
#endif
|
2007-01-23 13:40:44 +00:00
|
|
|
static long ata_stack[(DEFAULT_STACK_SIZE*3)/sizeof(long)];
|
2004-08-03 05:58:46 +00:00
|
|
|
static const char ata_thread_name[] = "ata";
|
2007-10-16 22:00:51 +00:00
|
|
|
static struct event_queue ata_queue;
|
2002-07-16 14:07:47 +00:00
|
|
|
static bool initialized = false;
|
2002-06-26 12:36:29 +00:00
|
|
|
|
2002-08-26 13:21:14 +00:00
|
|
|
static long last_user_activity = -1;
|
2002-09-23 11:39:21 +00:00
|
|
|
long last_disk_activity = -1;
|
2002-08-26 13:21:14 +00:00
|
|
|
|
2008-03-09 00:59:48 +00:00
|
|
|
static unsigned long total_sectors;
|
2002-09-05 15:25:08 +00:00
|
|
|
static int multisectors; /* number of supported multisectors */
|
2008-02-14 06:52:59 +00:00
|
|
|
static unsigned short identify_info[SECTOR_SIZE/2];
|
2002-09-05 15:25:08 +00:00
|
|
|
|
2007-05-23 17:51:18 +00:00
|
|
|
#ifdef MAX_PHYS_SECTOR_SIZE
|
2008-02-22 17:29:37 +00:00
|
|
|
|
|
|
|
/** This is temporary **/
|
|
|
|
/* Define the mutex functions to use the special hack object */
|
|
|
|
#define mutex_init ata_spin_init
|
|
|
|
#define mutex_lock ata_spin_lock
|
|
|
|
#define mutex_unlock ata_spin_unlock
|
|
|
|
|
|
|
|
void ata_spin_init(struct mutex *m)
|
|
|
|
{
|
|
|
|
m->thread = NULL;
|
|
|
|
m->locked = 0;
|
|
|
|
m->count = 0;
|
|
|
|
#if CONFIG_CORELOCK == SW_CORELOCK
|
|
|
|
corelock_init(&m->cl);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ata_spin_lock(struct mutex *m)
|
|
|
|
{
|
|
|
|
struct thread_entry *current = thread_get_current();
|
|
|
|
|
|
|
|
if (current == m->thread)
|
|
|
|
{
|
|
|
|
m->count++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (test_and_set(&m->locked, 1, &m->cl))
|
|
|
|
yield();
|
|
|
|
|
|
|
|
m->thread = current;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ata_spin_unlock(struct mutex *m)
|
|
|
|
{
|
|
|
|
if (m->count > 0)
|
|
|
|
{
|
|
|
|
m->count--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m->thread = NULL;
|
|
|
|
test_and_set(&m->locked, 0, &m->cl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****/
|
|
|
|
|
2007-05-23 17:51:18 +00:00
|
|
|
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
|
|
|
|
|
2002-08-02 06:01:22 +00:00
|
|
|
static int ata_power_on(void);
|
2002-08-27 21:06:48 +00:00
|
|
|
static int perform_soft_reset(void);
|
2002-12-02 10:30:40 +00:00
|
|
|
static int set_multiple_mode(int sectors);
|
2004-02-17 01:31:50 +00:00
|
|
|
static int set_features(void);
|
2002-08-27 21:06:48 +00:00
|
|
|
|
2007-02-17 11:19:14 +00:00
|
|
|
STATICIRAM int wait_for_bsy(void) ICODE_ATTR;
|
|
|
|
STATICIRAM int wait_for_bsy(void)
|
2002-04-21 22:06:12 +00:00
|
|
|
{
|
2005-02-25 18:50:16 +00:00
|
|
|
long timeout = current_tick + HZ*30;
|
2003-07-10 13:32:15 +00:00
|
|
|
while (TIME_BEFORE(current_tick, timeout) && (ATA_STATUS & STATUS_BSY)) {
|
|
|
|
last_disk_activity = current_tick;
|
2007-03-09 08:03:18 +00:00
|
|
|
priority_yield();
|
2003-07-10 13:32:15 +00:00
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
|
|
|
|
if (TIME_BEFORE(current_tick, timeout))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0; /* timeout */
|
|
|
|
}
|
|
|
|
|
2007-02-17 11:19:14 +00:00
|
|
|
STATICIRAM int wait_for_rdy(void) ICODE_ATTR;
|
|
|
|
STATICIRAM int wait_for_rdy(void)
|
2002-04-21 22:06:12 +00:00
|
|
|
{
|
2005-02-25 18:50:16 +00:00
|
|
|
long timeout;
|
2005-01-20 23:00:11 +00:00
|
|
|
|
2002-04-21 22:06:12 +00:00
|
|
|
if (!wait_for_bsy())
|
|
|
|
return 0;
|
2002-08-29 11:50:57 +00:00
|
|
|
|
2002-09-04 20:15:45 +00:00
|
|
|
timeout = current_tick + HZ*10;
|
2003-03-31 14:14:07 +00:00
|
|
|
|
2003-07-10 13:32:15 +00:00
|
|
|
while (TIME_BEFORE(current_tick, timeout) &&
|
|
|
|
!(ATA_ALT_STATUS & STATUS_RDY)) {
|
|
|
|
last_disk_activity = current_tick;
|
2007-03-09 08:03:18 +00:00
|
|
|
priority_yield();
|
2003-07-10 13:32:15 +00:00
|
|
|
}
|
2002-08-29 11:50:57 +00:00
|
|
|
|
|
|
|
if (TIME_BEFORE(current_tick, timeout))
|
|
|
|
return STATUS_RDY;
|
|
|
|
else
|
|
|
|
return 0; /* timeout */
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2007-02-17 11:19:14 +00:00
|
|
|
STATICIRAM int wait_for_start_of_transfer(void) ICODE_ATTR;
|
|
|
|
STATICIRAM int wait_for_start_of_transfer(void)
|
2002-04-21 22:06:12 +00:00
|
|
|
{
|
|
|
|
if (!wait_for_bsy())
|
|
|
|
return 0;
|
2006-08-12 08:01:54 +00:00
|
|
|
|
2002-04-27 14:19:00 +00:00
|
|
|
return (ATA_ALT_STATUS & (STATUS_BSY|STATUS_DRQ)) == STATUS_DRQ;
|
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2007-02-17 11:19:14 +00:00
|
|
|
STATICIRAM int wait_for_end_of_transfer(void) ICODE_ATTR;
|
|
|
|
STATICIRAM int wait_for_end_of_transfer(void)
|
2002-04-21 22:06:12 +00:00
|
|
|
{
|
|
|
|
if (!wait_for_bsy())
|
|
|
|
return 0;
|
2002-04-27 14:19:00 +00:00
|
|
|
return (ATA_ALT_STATUS & (STATUS_RDY|STATUS_DRQ)) == STATUS_RDY;
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2007-02-18 05:07:19 +00:00
|
|
|
#if (CONFIG_LED == LED_REAL)
|
2006-12-03 22:13:44 +00:00
|
|
|
/* 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
|
2007-02-17 11:19:14 +00:00
|
|
|
STATICIRAM void copy_read_sectors(unsigned char* buf, int wordcount) ICODE_ATTR;
|
|
|
|
STATICIRAM void copy_read_sectors(unsigned char* buf, int wordcount)
|
2004-01-16 09:02:21 +00:00
|
|
|
{
|
2004-03-10 14:15:14 +00:00
|
|
|
unsigned short tmp = 0;
|
2004-01-16 09:02:21 +00:00
|
|
|
|
2005-01-24 14:26:24 +00:00
|
|
|
if ( (unsigned long)buf & 1)
|
2004-01-27 09:12:51 +00:00
|
|
|
{ /* not 16-bit aligned, copy byte by byte */
|
2004-01-16 09:02:21 +00:00
|
|
|
unsigned char* bufend = buf + wordcount*2;
|
|
|
|
do
|
2006-12-03 22:13:44 +00:00
|
|
|
{
|
2004-01-27 09:12:51 +00:00
|
|
|
tmp = ATA_DATA;
|
2006-01-18 14:04:30 +00:00
|
|
|
#if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN)
|
2004-01-16 09:02:21 +00:00
|
|
|
*buf++ = tmp & 0xff; /* I assume big endian */
|
|
|
|
*buf++ = tmp >> 8; /* and don't use the SWAB16 macro */
|
2005-01-26 12:53:48 +00:00
|
|
|
#else
|
|
|
|
*buf++ = tmp >> 8;
|
|
|
|
*buf++ = tmp & 0xff;
|
|
|
|
#endif
|
2004-01-16 09:02:21 +00:00
|
|
|
} while (buf < bufend); /* tail loop is faster */
|
|
|
|
}
|
2004-03-10 14:15:14 +00:00
|
|
|
else
|
2004-01-27 09:12:51 +00:00
|
|
|
{ /* 16-bit aligned, can do faster copy */
|
2004-01-16 09:02:21 +00:00
|
|
|
unsigned short* wbuf = (unsigned short*)buf;
|
|
|
|
unsigned short* wbufend = wbuf + wordcount;
|
|
|
|
do
|
2006-12-03 22:13:44 +00:00
|
|
|
{
|
2005-01-26 12:53:48 +00:00
|
|
|
#ifdef SWAP_WORDS
|
2005-11-07 23:07:19 +00:00
|
|
|
*wbuf = swap16(ATA_DATA);
|
2005-01-26 12:53:48 +00:00
|
|
|
#else
|
|
|
|
*wbuf = ATA_DATA;
|
|
|
|
#endif
|
2004-01-16 09:02:21 +00:00
|
|
|
} while (++wbuf < wbufend); /* tail loop is faster */
|
2004-03-10 14:15:14 +00:00
|
|
|
}
|
2005-04-04 09:12:12 +00:00
|
|
|
}
|
2006-12-03 22:13:44 +00:00
|
|
|
#endif /* !ATA_OPTIMIZED_READING */
|
2005-04-04 09:12:12 +00:00
|
|
|
|
2007-05-23 17:51:18 +00:00
|
|
|
#ifdef MAX_PHYS_SECTOR_SIZE
|
|
|
|
static int _read_sectors(unsigned long start,
|
|
|
|
int incount,
|
|
|
|
void* inbuf)
|
|
|
|
#else
|
2004-12-29 22:37:31 +00:00
|
|
|
int ata_read_sectors(IF_MV2(int drive,)
|
2004-12-28 22:16:07 +00:00
|
|
|
unsigned long start,
|
2002-09-25 14:10:50 +00:00
|
|
|
int incount,
|
|
|
|
void* inbuf)
|
2007-05-23 17:51:18 +00:00
|
|
|
#endif
|
2002-04-21 22:06:12 +00:00
|
|
|
{
|
2002-05-16 21:28:21 +00:00
|
|
|
int ret = 0;
|
2005-02-25 18:50:16 +00:00
|
|
|
long timeout;
|
2002-09-25 14:10:50 +00:00
|
|
|
int count;
|
|
|
|
void* buf;
|
2005-01-26 12:53:48 +00:00
|
|
|
long spinup_start;
|
2002-08-27 21:06:48 +00:00
|
|
|
|
2007-05-23 17:51:18 +00:00
|
|
|
#ifndef MAX_PHYS_SECTOR_SIZE
|
2004-12-29 22:37:31 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
(void)drive; /* unused for now */
|
|
|
|
#endif
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
2007-05-23 17:51:18 +00:00
|
|
|
#endif
|
2002-08-27 21:06:48 +00:00
|
|
|
|
2008-03-09 00:59:48 +00:00
|
|
|
if (start + incount > total_sectors) {
|
|
|
|
ret = -1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2002-12-05 09:35:01 +00:00
|
|
|
last_disk_activity = current_tick;
|
2003-04-05 22:25:21 +00:00
|
|
|
spinup_start = current_tick;
|
2002-12-05 09:35:01 +00:00
|
|
|
|
2005-04-04 09:12:12 +00:00
|
|
|
ata_led(true);
|
2002-11-27 15:55:47 +00:00
|
|
|
|
2002-07-16 12:18:17 +00:00
|
|
|
if ( sleeping ) {
|
2002-12-06 15:17:30 +00:00
|
|
|
spinup = true;
|
2002-11-27 15:55:47 +00:00
|
|
|
if (poweroff) {
|
|
|
|
if (ata_power_on()) {
|
2008-03-09 00:59:48 +00:00
|
|
|
ret = -2;
|
|
|
|
goto error;
|
2002-11-27 15:55:47 +00:00
|
|
|
}
|
2002-08-02 06:01:22 +00:00
|
|
|
}
|
2002-11-27 15:55:47 +00:00
|
|
|
else {
|
|
|
|
if (perform_soft_reset()) {
|
2008-03-09 00:59:48 +00:00
|
|
|
ret = -2;
|
|
|
|
goto error;
|
2002-11-27 15:55:47 +00:00
|
|
|
}
|
2002-07-16 12:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
2002-07-16 14:07:47 +00:00
|
|
|
|
2003-02-27 13:26:09 +00:00
|
|
|
timeout = current_tick + READ_TIMEOUT;
|
|
|
|
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_SELECT, ata_device);
|
2002-04-21 22:06:12 +00:00
|
|
|
if (!wait_for_rdy())
|
2002-05-16 21:28:21 +00:00
|
|
|
{
|
2008-03-09 00:59:48 +00:00
|
|
|
ret = -3;
|
|
|
|
goto error;
|
2002-05-16 21:28:21 +00:00
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2003-04-28 12:02:14 +00:00
|
|
|
retry:
|
2002-09-25 14:10:50 +00:00
|
|
|
buf = inbuf;
|
|
|
|
count = incount;
|
2002-09-24 14:23:18 +00:00
|
|
|
while (TIME_BEFORE(current_tick, timeout)) {
|
2003-03-13 15:45:38 +00:00
|
|
|
ret = 0;
|
2003-03-17 13:42:30 +00:00
|
|
|
last_disk_activity = current_tick;
|
2002-09-05 15:25:08 +00:00
|
|
|
|
2006-12-20 22:08:29 +00:00
|
|
|
#ifdef HAVE_LBA48
|
|
|
|
if (lba48)
|
2006-12-19 22:40:23 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2002-09-05 15:25:08 +00:00
|
|
|
else
|
2006-12-19 22:40:23 +00:00
|
|
|
#endif
|
|
|
|
{
|
2006-12-20 22:08:29 +00:00
|
|
|
SET_REG(ATA_NSECTOR, count & 0xff); /* 0 means 256 sectors */
|
2006-12-19 22:40:23 +00:00
|
|
|
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);
|
|
|
|
}
|
2002-09-24 14:23:18 +00:00
|
|
|
|
2003-03-31 14:14:07 +00:00
|
|
|
/* wait at least 400ns between writing command and reading status */
|
2005-01-20 23:00:11 +00:00
|
|
|
__asm__ volatile ("nop");
|
|
|
|
__asm__ volatile ("nop");
|
|
|
|
__asm__ volatile ("nop");
|
|
|
|
__asm__ volatile ("nop");
|
|
|
|
__asm__ volatile ("nop");
|
2003-03-31 14:14:07 +00:00
|
|
|
|
2002-09-24 14:23:18 +00:00
|
|
|
while (count) {
|
|
|
|
int sectors;
|
|
|
|
int wordcount;
|
2003-03-14 16:06:09 +00:00
|
|
|
int status;
|
2002-09-24 14:23:18 +00:00
|
|
|
|
|
|
|
if (!wait_for_start_of_transfer()) {
|
2004-03-19 13:26:43 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
2005-05-10 22:39:53 +00:00
|
|
|
perform_soft_reset();
|
2008-03-09 00:59:48 +00:00
|
|
|
ret = -5;
|
2002-09-25 14:10:50 +00:00
|
|
|
goto retry;
|
2002-09-24 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
2002-12-06 13:08:42 +00:00
|
|
|
if (spinup) {
|
2003-04-05 22:25:21 +00:00
|
|
|
ata_spinup_time = current_tick - spinup_start;
|
2002-12-06 13:08:42 +00:00
|
|
|
spinup = false;
|
|
|
|
sleeping = false;
|
|
|
|
poweroff = false;
|
|
|
|
}
|
|
|
|
|
2003-03-14 16:06:09 +00:00
|
|
|
/* read the status register exactly once per loop */
|
|
|
|
status = ATA_STATUS;
|
|
|
|
|
2002-09-24 14:23:18 +00:00
|
|
|
if (count >= multisectors )
|
|
|
|
sectors = multisectors;
|
|
|
|
else
|
|
|
|
sectors = count;
|
2002-09-06 16:02:19 +00:00
|
|
|
|
2002-09-24 14:23:18 +00:00
|
|
|
wordcount = sectors * SECTOR_SIZE / 2;
|
|
|
|
|
2004-01-16 09:02:21 +00:00
|
|
|
copy_read_sectors(buf, wordcount);
|
2002-04-27 14:19:00 +00:00
|
|
|
|
2003-03-14 16:06:09 +00:00
|
|
|
/*
|
|
|
|
"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) ) {
|
2005-05-10 22:39:53 +00:00
|
|
|
perform_soft_reset();
|
2008-03-09 00:59:48 +00:00
|
|
|
ret = -6;
|
2003-03-14 16:06:09 +00:00
|
|
|
goto retry;
|
|
|
|
}
|
2005-05-10 22:39:53 +00:00
|
|
|
|
2002-09-24 14:23:18 +00:00
|
|
|
buf += sectors * SECTOR_SIZE; /* Advance one chunk of sectors */
|
|
|
|
count -= sectors;
|
2002-12-05 09:35:01 +00:00
|
|
|
|
|
|
|
last_disk_activity = current_tick;
|
2002-09-24 14:23:18 +00:00
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2003-03-24 16:19:54 +00:00
|
|
|
if(!ret && !wait_for_end_of_transfer()) {
|
2005-05-10 22:39:53 +00:00
|
|
|
perform_soft_reset();
|
2008-03-09 00:59:48 +00:00
|
|
|
ret = -4;
|
2002-09-25 14:10:50 +00:00
|
|
|
goto retry;
|
2002-09-24 14:23:18 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-03-08 23:50:55 +00:00
|
|
|
|
2008-03-09 00:59:48 +00:00
|
|
|
error:
|
|
|
|
ata_led(false);
|
2007-05-23 17:51:18 +00:00
|
|
|
#ifndef MAX_PHYS_SECTOR_SIZE
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
2007-05-23 17:51:18 +00:00
|
|
|
#endif
|
2002-08-15 12:42:37 +00:00
|
|
|
|
2002-05-16 21:28:21 +00:00
|
|
|
return ret;
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2006-12-03 22:13:44 +00:00
|
|
|
#ifndef ATA_OPTIMIZED_WRITING
|
2007-02-17 11:19:14 +00:00
|
|
|
STATICIRAM void copy_write_sectors(const unsigned char* buf, int wordcount)
|
|
|
|
ICODE_ATTR;
|
|
|
|
STATICIRAM void copy_write_sectors(const unsigned char* buf, int wordcount)
|
2004-04-01 05:46:31 +00:00
|
|
|
{
|
2005-01-24 14:26:24 +00:00
|
|
|
if ( (unsigned long)buf & 1)
|
2004-04-01 05:46:31 +00:00
|
|
|
{ /* not 16-bit aligned, copy byte by byte */
|
|
|
|
unsigned short tmp = 0;
|
2004-10-08 19:20:20 +00:00
|
|
|
const unsigned char* bufend = buf + wordcount*2;
|
2004-04-01 05:46:31 +00:00
|
|
|
do
|
2005-01-26 12:53:48 +00:00
|
|
|
{
|
2006-01-18 14:04:30 +00:00
|
|
|
#if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN)
|
2004-06-11 06:56:51 +00:00
|
|
|
tmp = (unsigned short) *buf++;
|
2006-12-03 22:13:44 +00:00
|
|
|
tmp |= (unsigned short) *buf++ << 8;
|
|
|
|
SET_16BITREG(ATA_DATA, tmp);
|
2005-01-26 12:53:48 +00:00
|
|
|
#else
|
|
|
|
tmp = (unsigned short) *buf++ << 8;
|
|
|
|
tmp |= (unsigned short) *buf++;
|
|
|
|
SET_16BITREG(ATA_DATA, tmp);
|
|
|
|
#endif
|
2004-04-01 05:46:31 +00:00
|
|
|
} 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
|
2005-01-26 12:53:48 +00:00
|
|
|
{
|
|
|
|
#ifdef SWAP_WORDS
|
2005-11-07 23:07:19 +00:00
|
|
|
SET_16BITREG(ATA_DATA, swap16(*wbuf));
|
2005-01-26 12:53:48 +00:00
|
|
|
#else
|
|
|
|
SET_16BITREG(ATA_DATA, *wbuf);
|
|
|
|
#endif
|
2004-04-01 05:46:31 +00:00
|
|
|
} while (++wbuf < wbufend); /* tail loop is faster */
|
|
|
|
}
|
|
|
|
}
|
2006-12-03 22:13:44 +00:00
|
|
|
#endif /* !ATA_OPTIMIZED_WRITING */
|
2004-04-01 05:46:31 +00:00
|
|
|
|
2007-05-23 17:51:18 +00:00
|
|
|
#ifdef MAX_PHYS_SECTOR_SIZE
|
|
|
|
static int _write_sectors(unsigned long start,
|
|
|
|
int count,
|
|
|
|
const void* buf)
|
|
|
|
#else
|
2004-12-29 22:37:31 +00:00
|
|
|
int ata_write_sectors(IF_MV2(int drive,)
|
2004-12-28 22:16:07 +00:00
|
|
|
unsigned long start,
|
2002-08-14 16:37:28 +00:00
|
|
|
int count,
|
2004-08-17 01:45:48 +00:00
|
|
|
const void* buf)
|
2007-05-23 17:51:18 +00:00
|
|
|
#endif
|
2002-04-21 22:06:12 +00:00
|
|
|
{
|
|
|
|
int i;
|
2002-11-07 22:40:24 +00:00
|
|
|
int ret = 0;
|
2005-02-25 18:50:16 +00:00
|
|
|
long spinup_start;
|
2002-08-26 13:21:14 +00:00
|
|
|
|
2007-05-23 17:51:18 +00:00
|
|
|
#ifndef MAX_PHYS_SECTOR_SIZE
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
(void)drive; /* unused for now */
|
|
|
|
#endif
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
2007-05-23 17:51:18 +00:00
|
|
|
#endif
|
2002-08-27 21:06:48 +00:00
|
|
|
|
2008-03-09 00:59:48 +00:00
|
|
|
if (start + count > total_sectors)
|
|
|
|
panicf("Writing past end of disk");
|
|
|
|
|
2002-12-05 09:35:01 +00:00
|
|
|
last_disk_activity = current_tick;
|
2003-04-05 22:25:21 +00:00
|
|
|
spinup_start = current_tick;
|
2002-12-05 09:35:01 +00:00
|
|
|
|
2005-04-04 09:12:12 +00:00
|
|
|
ata_led(true);
|
2003-03-31 14:14:07 +00:00
|
|
|
|
2002-08-26 22:05:47 +00:00
|
|
|
if ( sleeping ) {
|
2002-12-06 15:17:30 +00:00
|
|
|
spinup = true;
|
2002-11-27 15:55:47 +00:00
|
|
|
if (poweroff) {
|
|
|
|
if (ata_power_on()) {
|
2008-03-09 00:59:48 +00:00
|
|
|
ret = -1;
|
|
|
|
goto error;
|
2002-11-27 15:55:47 +00:00
|
|
|
}
|
2002-08-02 06:01:22 +00:00
|
|
|
}
|
2002-11-27 15:55:47 +00:00
|
|
|
else {
|
|
|
|
if (perform_soft_reset()) {
|
2008-03-09 00:59:48 +00:00
|
|
|
ret = -1;
|
|
|
|
goto error;
|
2002-11-27 15:55:47 +00:00
|
|
|
}
|
2002-07-16 12:18:17 +00:00
|
|
|
}
|
2002-08-26 22:05:47 +00:00
|
|
|
}
|
2002-05-16 21:28:21 +00:00
|
|
|
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_SELECT, ata_device);
|
2002-04-21 22:06:12 +00:00
|
|
|
if (!wait_for_rdy())
|
2002-05-16 21:28:21 +00:00
|
|
|
{
|
2008-03-09 00:59:48 +00:00
|
|
|
ret = -2;
|
|
|
|
goto error;
|
2002-05-16 21:28:21 +00:00
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2006-12-20 22:08:29 +00:00
|
|
|
#ifdef HAVE_LBA48
|
|
|
|
if (lba48)
|
2006-12-19 22:40:23 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2002-08-14 16:37:28 +00:00
|
|
|
else
|
2006-12-19 22:40:23 +00:00
|
|
|
#endif
|
|
|
|
{
|
2006-12-20 22:08:29 +00:00
|
|
|
SET_REG(ATA_NSECTOR, count & 0xff); /* 0 means 256 sectors */
|
2006-12-19 22:40:23 +00:00
|
|
|
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);
|
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
|
|
|
|
for (i=0; i<count; i++) {
|
2004-04-01 05:46:31 +00:00
|
|
|
|
2003-04-28 12:02:14 +00:00
|
|
|
if (!wait_for_start_of_transfer()) {
|
|
|
|
ret = -3;
|
|
|
|
break;
|
2002-05-16 21:28:21 +00:00
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-12-05 09:35:01 +00:00
|
|
|
if (spinup) {
|
2003-04-05 22:25:21 +00:00
|
|
|
ata_spinup_time = current_tick - spinup_start;
|
2002-12-05 09:35:01 +00:00
|
|
|
spinup = false;
|
2002-12-06 13:08:42 +00:00
|
|
|
sleeping = false;
|
|
|
|
poweroff = false;
|
2002-12-05 09:35:01 +00:00
|
|
|
}
|
|
|
|
|
2004-04-01 05:46:31 +00:00
|
|
|
copy_write_sectors(buf, SECTOR_SIZE/2);
|
2002-04-27 14:19:00 +00:00
|
|
|
|
|
|
|
#ifdef USE_INTERRUPT
|
|
|
|
/* reading the status register clears the interrupt */
|
|
|
|
j = ATA_STATUS;
|
|
|
|
#endif
|
2002-06-12 09:28:22 +00:00
|
|
|
buf += SECTOR_SIZE;
|
2002-12-05 09:35:01 +00:00
|
|
|
|
|
|
|
last_disk_activity = current_tick;
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2005-01-20 23:00:11 +00:00
|
|
|
if(!ret && !wait_for_end_of_transfer()) {
|
|
|
|
DEBUGF("End on transfer failed. -- jyp");
|
2003-04-28 12:02:14 +00:00
|
|
|
ret = -4;
|
2005-01-20 23:00:11 +00:00
|
|
|
}
|
2002-05-16 21:28:21 +00:00
|
|
|
|
2008-03-09 00:59:48 +00:00
|
|
|
error:
|
2005-04-04 09:12:12 +00:00
|
|
|
ata_led(false);
|
2007-05-23 17:51:18 +00:00
|
|
|
#ifndef MAX_PHYS_SECTOR_SIZE
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
2007-05-23 17:51:18 +00:00
|
|
|
#endif
|
2002-08-15 12:42:37 +00:00
|
|
|
|
2002-11-07 22:40:24 +00:00
|
|
|
return ret;
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2007-05-23 17:51:18 +00:00
|
|
|
#ifdef MAX_PHYS_SECTOR_SIZE
|
|
|
|
static int cache_sector(unsigned long sector)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
sector &= ~(phys_sector_mult - 1);
|
|
|
|
/* round down to physical sector boundary */
|
|
|
|
|
|
|
|
/* check whether the sector is already cached */
|
|
|
|
if (sector_cache.inuse && (sector_cache.sectornum == sector))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* not found: read the sector */
|
|
|
|
sector_cache.inuse = false;
|
|
|
|
rc = _read_sectors(sector, phys_sector_mult, sector_cache.data);
|
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
sector_cache.sectornum = sector;
|
|
|
|
sector_cache.inuse = true;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int flush_current_sector(void)
|
|
|
|
{
|
|
|
|
return _write_sectors(sector_cache.sectornum, phys_sector_mult,
|
|
|
|
sector_cache.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ata_read_sectors(IF_MV2(int drive,)
|
|
|
|
unsigned long start,
|
|
|
|
int incount,
|
|
|
|
void* inbuf)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
(void)drive; /* unused for now */
|
|
|
|
#endif
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
2007-05-23 17:51:18 +00:00
|
|
|
|
|
|
|
offset = start & (phys_sector_mult - 1);
|
|
|
|
|
|
|
|
if (offset) /* first partial sector */
|
|
|
|
{
|
|
|
|
int partcount = MIN(incount, phys_sector_mult - offset);
|
|
|
|
|
|
|
|
rc = cache_sector(start);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
rc = rc * 10 - 1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
memcpy(inbuf, sector_cache.data + offset * SECTOR_SIZE,
|
|
|
|
partcount * SECTOR_SIZE);
|
|
|
|
|
|
|
|
start += partcount;
|
|
|
|
inbuf += partcount * SECTOR_SIZE;
|
|
|
|
incount -= partcount;
|
|
|
|
}
|
|
|
|
if (incount)
|
|
|
|
{
|
|
|
|
offset = incount & (phys_sector_mult - 1);
|
|
|
|
incount -= offset;
|
|
|
|
|
|
|
|
if (incount)
|
|
|
|
{
|
|
|
|
rc = _read_sectors(start, incount, inbuf);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
rc = rc * 10 - 2;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
start += incount;
|
|
|
|
inbuf += incount * SECTOR_SIZE;
|
|
|
|
}
|
|
|
|
if (offset)
|
|
|
|
{
|
|
|
|
rc = cache_sector(start);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
rc = rc * 10 - 3;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
memcpy(inbuf, sector_cache.data, offset * SECTOR_SIZE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
2007-05-23 17:51:18 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ata_write_sectors(IF_MV2(int drive,)
|
|
|
|
unsigned long start,
|
|
|
|
int count,
|
|
|
|
const void* buf)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
(void)drive; /* unused for now */
|
|
|
|
#endif
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
2007-05-23 17:51:18 +00:00
|
|
|
|
|
|
|
offset = start & (phys_sector_mult - 1);
|
|
|
|
|
|
|
|
if (offset) /* first partial sector */
|
|
|
|
{
|
|
|
|
int partcount = MIN(count, phys_sector_mult - offset);
|
|
|
|
|
|
|
|
rc = cache_sector(start);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
rc = rc * 10 - 1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
memcpy(sector_cache.data + offset * SECTOR_SIZE, buf,
|
|
|
|
partcount * SECTOR_SIZE);
|
|
|
|
rc = flush_current_sector();
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
rc = rc * 10 - 2;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
start += partcount;
|
|
|
|
buf += partcount * SECTOR_SIZE;
|
|
|
|
count -= partcount;
|
|
|
|
}
|
|
|
|
if (count)
|
|
|
|
{
|
|
|
|
offset = count & (phys_sector_mult - 1);
|
|
|
|
count -= offset;
|
|
|
|
|
|
|
|
if (count)
|
|
|
|
{
|
|
|
|
rc = _write_sectors(start, count, buf);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
rc = rc * 10 - 3;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
start += count;
|
|
|
|
buf += count * SECTOR_SIZE;
|
|
|
|
}
|
|
|
|
if (offset)
|
|
|
|
{
|
|
|
|
rc = cache_sector(start);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
rc = rc * 10 - 4;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
memcpy(sector_cache.data, buf, offset * SECTOR_SIZE);
|
|
|
|
rc = flush_current_sector();
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
rc = rc * 10 - 5;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
2007-05-23 17:51:18 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif /* MAX_PHYS_SECTOR_SIZE */
|
|
|
|
|
2002-04-21 22:06:12 +00:00
|
|
|
static int check_registers(void)
|
|
|
|
{
|
2005-01-20 23:00:11 +00:00
|
|
|
int i;
|
2003-07-08 06:33:30 +00:00
|
|
|
if ( ATA_STATUS & STATUS_BSY )
|
2003-07-03 00:02:15 +00:00
|
|
|
return -1;
|
|
|
|
|
2005-01-20 23:00:11 +00:00
|
|
|
for (i = 0; i<64; i++) {
|
2006-08-12 08:01:54 +00:00
|
|
|
SET_REG(ATA_NSECTOR, WRITE_PATTERN1);
|
|
|
|
SET_REG(ATA_SECTOR, WRITE_PATTERN2);
|
|
|
|
SET_REG(ATA_LCYL, WRITE_PATTERN3);
|
|
|
|
SET_REG(ATA_HCYL, WRITE_PATTERN4);
|
|
|
|
|
|
|
|
if (((ATA_NSECTOR & READ_PATTERN1_MASK) == READ_PATTERN1) &&
|
|
|
|
((ATA_SECTOR & READ_PATTERN2_MASK) == READ_PATTERN2) &&
|
|
|
|
((ATA_LCYL & READ_PATTERN3_MASK) == READ_PATTERN3) &&
|
|
|
|
((ATA_HCYL & READ_PATTERN4_MASK) == READ_PATTERN4))
|
|
|
|
return 0;
|
2005-01-20 23:00:11 +00:00
|
|
|
}
|
2002-05-24 11:25:24 +00:00
|
|
|
return -2;
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int freeze_lock(void)
|
|
|
|
{
|
2004-09-17 11:28:07 +00:00
|
|
|
/* does the disk support Security Mode feature set? */
|
|
|
|
if (identify_info[82] & 2)
|
|
|
|
{
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_SELECT, ata_device);
|
2002-12-03 13:29:35 +00:00
|
|
|
|
2004-09-17 11:28:07 +00:00
|
|
|
if (!wait_for_rdy())
|
|
|
|
return -1;
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_COMMAND, CMD_SECURITY_FREEZE_LOCK);
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2004-09-17 11:28:07 +00:00
|
|
|
if (!wait_for_rdy())
|
|
|
|
return -2;
|
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-16 14:07:47 +00:00
|
|
|
void ata_spindown(int seconds)
|
|
|
|
{
|
|
|
|
sleep_timeout = seconds * HZ;
|
|
|
|
}
|
|
|
|
|
2002-07-28 15:16:36 +00:00
|
|
|
bool ata_disk_is_active(void)
|
|
|
|
{
|
|
|
|
return !sleeping;
|
|
|
|
}
|
|
|
|
|
2002-07-16 14:07:47 +00:00
|
|
|
static int ata_perform_sleep(void)
|
2002-04-27 14:19:00 +00:00
|
|
|
{
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
2002-08-27 21:06:48 +00:00
|
|
|
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_SELECT, ata_device);
|
2002-12-03 13:29:35 +00:00
|
|
|
|
2002-07-16 14:07:47 +00:00
|
|
|
if(!wait_for_rdy()) {
|
2002-09-02 06:26:00 +00:00
|
|
|
DEBUGF("ata_perform_sleep() - not RDY\n");
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
2002-04-27 14:19:00 +00:00
|
|
|
return -1;
|
2002-05-16 21:28:21 +00:00
|
|
|
}
|
2002-04-27 14:19:00 +00:00
|
|
|
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_COMMAND, CMD_SLEEP);
|
2002-04-27 14:19:00 +00:00
|
|
|
|
|
|
|
if (!wait_for_rdy())
|
2002-09-02 06:26:00 +00:00
|
|
|
{
|
|
|
|
DEBUGF("ata_perform_sleep() - CMD failed\n");
|
2008-03-09 13:24:42 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
|
|
|
return -2;
|
2002-09-02 06:26:00 +00:00
|
|
|
}
|
2002-11-27 15:55:47 +00:00
|
|
|
|
2002-07-16 14:07:47 +00:00
|
|
|
sleeping = true;
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
2008-03-09 13:24:42 +00:00
|
|
|
return 0;
|
2002-04-27 14:19:00 +00:00
|
|
|
}
|
|
|
|
|
2005-11-07 23:19:06 +00:00
|
|
|
void ata_sleep(void)
|
2002-07-16 12:18:17 +00:00
|
|
|
{
|
2006-12-19 16:50:07 +00:00
|
|
|
queue_post(&ata_queue, Q_SLEEP, 0);
|
2002-07-16 14:07:47 +00:00
|
|
|
}
|
2002-07-16 12:18:17 +00:00
|
|
|
|
2006-11-29 12:17:26 +00:00
|
|
|
void ata_sleepnow(void)
|
|
|
|
{
|
2008-01-18 13:12:33 +00:00
|
|
|
if (!spinup && !sleeping && !ata_mtx.locked && initialized)
|
2006-11-29 12:17:26 +00:00
|
|
|
{
|
|
|
|
call_ata_idle_notifys(false);
|
|
|
|
ata_perform_sleep();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-26 13:21:14 +00:00
|
|
|
void ata_spin(void)
|
|
|
|
{
|
|
|
|
last_user_activity = current_tick;
|
|
|
|
}
|
|
|
|
|
2002-07-16 14:07:47 +00:00
|
|
|
static void ata_thread(void)
|
|
|
|
{
|
2002-11-27 15:55:47 +00:00
|
|
|
static long last_sleep = 0;
|
2007-10-16 01:25:17 +00:00
|
|
|
struct queue_event ev;
|
2006-11-08 01:55:26 +00:00
|
|
|
static long last_seen_mtx_unlock = 0;
|
2002-07-16 12:18:17 +00:00
|
|
|
|
2002-07-16 14:07:47 +00:00
|
|
|
while (1) {
|
2008-02-11 07:42:11 +00:00
|
|
|
queue_wait_w_tmo(&ata_queue, &ev, HZ/2);
|
|
|
|
|
|
|
|
switch ( ev.id ) {
|
|
|
|
case SYS_TIMEOUT:
|
|
|
|
if (!spinup && !sleeping)
|
2006-11-08 01:55:26 +00:00
|
|
|
{
|
2008-02-11 07:42:11 +00:00
|
|
|
if (!ata_mtx.locked)
|
|
|
|
{
|
|
|
|
if (!last_seen_mtx_unlock)
|
|
|
|
last_seen_mtx_unlock = current_tick;
|
|
|
|
if (TIME_AFTER(current_tick, last_seen_mtx_unlock+(HZ*2)))
|
|
|
|
{
|
|
|
|
call_ata_idle_notifys(false);
|
|
|
|
last_seen_mtx_unlock = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( sleep_timeout &&
|
|
|
|
TIME_AFTER( current_tick,
|
|
|
|
last_user_activity + sleep_timeout ) &&
|
|
|
|
TIME_AFTER( current_tick,
|
|
|
|
last_disk_activity + sleep_timeout ) )
|
2006-11-08 01:55:26 +00:00
|
|
|
{
|
2008-02-11 07:42:11 +00:00
|
|
|
call_ata_idle_notifys(true);
|
|
|
|
ata_perform_sleep();
|
|
|
|
last_sleep = current_tick;
|
2006-11-08 01:55:26 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-11 07:42:11 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_ATA_POWER_OFF
|
|
|
|
if ( !spinup && sleeping && !poweroff &&
|
|
|
|
TIME_AFTER( current_tick, last_sleep + ATA_POWER_OFF_TIMEOUT ))
|
2006-11-06 14:24:18 +00:00
|
|
|
{
|
2008-02-11 07:42:11 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
|
|
|
ide_power_enable(false);
|
|
|
|
mutex_unlock(&ata_mtx);
|
|
|
|
poweroff = true;
|
2006-11-06 14:24:18 +00:00
|
|
|
}
|
2002-11-27 15:55:47 +00:00
|
|
|
#endif
|
2008-02-11 07:42:11 +00:00
|
|
|
break;
|
2002-11-27 15:55:47 +00:00
|
|
|
|
2006-11-08 01:55:26 +00:00
|
|
|
#ifndef USB_NONE
|
2006-11-09 07:02:18 +00:00
|
|
|
case SYS_USB_CONNECTED:
|
2002-11-28 22:46:19 +00:00
|
|
|
if (poweroff) {
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
2005-04-04 09:12:12 +00:00
|
|
|
ata_led(true);
|
2002-11-27 15:55:47 +00:00
|
|
|
ata_power_on();
|
2005-04-04 09:12:12 +00:00
|
|
|
ata_led(false);
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
2002-11-28 22:46:19 +00:00
|
|
|
}
|
2002-11-27 15:55:47 +00:00
|
|
|
|
2002-07-23 15:02:25 +00:00
|
|
|
/* Tell the USB thread that we are safe */
|
2002-07-27 22:44:52 +00:00
|
|
|
DEBUGF("ata_thread got SYS_USB_CONNECTED\n");
|
2002-07-23 15:02:25 +00:00
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
2002-07-16 14:07:47 +00:00
|
|
|
|
2002-07-23 15:02:25 +00:00
|
|
|
/* Wait until the USB cable is extracted again */
|
|
|
|
usb_wait_for_disconnect(&ata_queue);
|
2006-11-08 01:55:26 +00:00
|
|
|
break;
|
2006-11-09 07:02:18 +00:00
|
|
|
#endif
|
2002-07-16 14:07:47 +00:00
|
|
|
case Q_SLEEP:
|
2006-11-26 09:53:42 +00:00
|
|
|
call_ata_idle_notifys(false);
|
2002-09-06 23:55:52 +00:00
|
|
|
last_disk_activity = current_tick - sleep_timeout + (HZ/2);
|
2002-07-16 14:07:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-07-16 12:18:17 +00:00
|
|
|
}
|
2002-07-16 14:07:47 +00:00
|
|
|
}
|
2002-07-16 12:18:17 +00:00
|
|
|
|
2002-12-03 11:26:39 +00:00
|
|
|
/* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
|
2002-04-21 22:06:12 +00:00
|
|
|
int ata_hard_reset(void)
|
|
|
|
{
|
2002-05-16 21:28:21 +00:00
|
|
|
int ret;
|
2004-09-17 11:28:07 +00:00
|
|
|
|
2006-03-07 13:25:19 +00:00
|
|
|
ata_reset();
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-12-03 11:26:39 +00:00
|
|
|
/* state HRR2 */
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_SELECT, ata_device); /* select the right device */
|
2002-12-02 10:30:40 +00:00
|
|
|
ret = wait_for_bsy();
|
2002-05-16 21:28:21 +00:00
|
|
|
|
2002-06-30 13:10:42 +00:00
|
|
|
/* Massage the return code so it is 0 on success and -1 on failure */
|
|
|
|
ret = ret?0:-1;
|
|
|
|
|
2002-05-16 21:28:21 +00:00
|
|
|
return ret;
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2002-08-27 21:06:48 +00:00
|
|
|
static int perform_soft_reset(void)
|
2002-04-21 22:06:12 +00:00
|
|
|
{
|
2006-10-25 17:31:29 +00:00
|
|
|
/* If this code is allowed to run on a Nano, the next reads from the flash will
|
|
|
|
* time out, so we disable it. It shouldn't be necessary anyway, since the
|
|
|
|
* ATA -> Flash interface automatically sleeps almost immediately after the
|
|
|
|
* last command.
|
|
|
|
*/
|
2002-05-16 21:28:21 +00:00
|
|
|
int ret;
|
2002-06-30 13:10:42 +00:00
|
|
|
int retry_count;
|
2008-03-09 13:24:42 +00:00
|
|
|
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_SELECT, SELECT_LBA | ata_device );
|
|
|
|
SET_REG(ATA_CONTROL, CONTROL_nIEN|CONTROL_SRST );
|
2003-12-17 20:15:12 +00:00
|
|
|
sleep(1); /* >= 5us */
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_CONTROL, CONTROL_nIEN);
|
2003-12-17 20:15:12 +00:00
|
|
|
sleep(1); /* >2ms */
|
2002-08-02 06:01:22 +00:00
|
|
|
|
2002-06-30 13:10:42 +00:00
|
|
|
/* This little sucker can take up to 30 seconds */
|
|
|
|
retry_count = 8;
|
|
|
|
do
|
|
|
|
{
|
2002-07-23 15:02:25 +00:00
|
|
|
ret = wait_for_rdy();
|
2002-06-30 13:10:42 +00:00
|
|
|
} while(!ret && retry_count--);
|
2002-05-16 21:28:21 +00:00
|
|
|
|
2008-03-09 13:24:42 +00:00
|
|
|
if (!ret)
|
|
|
|
return -1;
|
2002-07-16 12:18:17 +00:00
|
|
|
|
2008-03-09 13:24:42 +00:00
|
|
|
if (set_features())
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
if (set_multiple_mode(multisectors))
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
if (freeze_lock())
|
|
|
|
return -4;
|
|
|
|
|
|
|
|
return 0;
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2002-08-27 21:06:48 +00:00
|
|
|
int ata_soft_reset(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
2002-08-27 21:06:48 +00:00
|
|
|
|
|
|
|
ret = perform_soft_reset();
|
|
|
|
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
2002-08-27 21:06:48 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-08-02 06:01:22 +00:00
|
|
|
static int ata_power_on(void)
|
|
|
|
{
|
2004-02-17 01:31:50 +00:00
|
|
|
int rc;
|
|
|
|
|
2002-08-02 06:01:22 +00:00
|
|
|
ide_power_enable(true);
|
2007-11-03 23:57:24 +00:00
|
|
|
sleep(HZ/50); /* allow voltage to build up */
|
2002-12-02 10:30:40 +00:00
|
|
|
if( ata_hard_reset() )
|
|
|
|
return -1;
|
2002-08-02 06:01:22 +00:00
|
|
|
|
2004-02-17 01:30:25 +00:00
|
|
|
rc = set_features();
|
|
|
|
if (rc)
|
|
|
|
return rc * 10 - 2;
|
|
|
|
|
2002-12-02 10:30:40 +00:00
|
|
|
if (set_multiple_mode(multisectors))
|
2004-02-17 01:30:25 +00:00
|
|
|
return -3;
|
2002-08-02 06:01:22 +00:00
|
|
|
|
2002-12-03 13:29:35 +00:00
|
|
|
if (freeze_lock())
|
2004-02-17 01:30:25 +00:00
|
|
|
return -4;
|
2002-12-03 13:29:35 +00:00
|
|
|
|
2002-12-02 10:30:40 +00:00
|
|
|
return 0;
|
2002-08-02 06:01:22 +00:00
|
|
|
}
|
|
|
|
|
2002-06-12 13:51:31 +00:00
|
|
|
static int master_slave_detect(void)
|
2002-05-22 14:37:36 +00:00
|
|
|
{
|
|
|
|
/* master? */
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_SELECT, 0);
|
2003-07-09 22:04:31 +00:00
|
|
|
if ( ATA_STATUS & (STATUS_RDY|STATUS_BSY) ) {
|
2002-07-02 14:23:30 +00:00
|
|
|
ata_device = 0;
|
2002-05-22 14:37:36 +00:00
|
|
|
DEBUGF("Found master harddisk\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* slave? */
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_SELECT, SELECT_DEVICE1);
|
2003-07-09 22:04:31 +00:00
|
|
|
if ( ATA_STATUS & (STATUS_RDY|STATUS_BSY) ) {
|
2002-07-02 14:23:30 +00:00
|
|
|
ata_device = SELECT_DEVICE1;
|
2002-05-22 14:37:36 +00:00
|
|
|
DEBUGF("Found slave harddisk\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-09-05 15:25:08 +00:00
|
|
|
static int identify(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_SELECT, ata_device);
|
2002-12-03 13:29:35 +00:00
|
|
|
|
2002-09-05 15:25:08 +00:00
|
|
|
if(!wait_for_rdy()) {
|
|
|
|
DEBUGF("identify() - not RDY\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_COMMAND, CMD_IDENTIFY);
|
2002-09-05 15:25:08 +00:00
|
|
|
|
|
|
|
if (!wait_for_start_of_transfer())
|
|
|
|
{
|
|
|
|
DEBUGF("identify() - CMD failed\n");
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2005-01-26 12:53:48 +00:00
|
|
|
for (i=0; i<SECTOR_SIZE/2; i++) {
|
|
|
|
/* the IDENTIFY words are already swapped, so we need to treat
|
|
|
|
this info differently that normal sector data */
|
2005-11-08 11:41:56 +00:00
|
|
|
#if defined(ROCKBOX_BIG_ENDIAN) && !defined(SWAP_WORDS)
|
2005-11-07 23:07:19 +00:00
|
|
|
identify_info[i] = swap16(ATA_DATA);
|
2005-01-26 12:53:48 +00:00
|
|
|
#else
|
2005-11-07 23:07:19 +00:00
|
|
|
identify_info[i] = ATA_DATA;
|
2005-01-26 12:53:48 +00:00
|
|
|
#endif
|
|
|
|
}
|
2002-09-05 15:25:08 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-09-06 17:20:44 +00:00
|
|
|
static int set_multiple_mode(int sectors)
|
|
|
|
{
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_SELECT, ata_device);
|
2002-12-03 13:29:35 +00:00
|
|
|
|
2002-09-06 17:20:44 +00:00
|
|
|
if(!wait_for_rdy()) {
|
|
|
|
DEBUGF("set_multiple_mode() - not RDY\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_NSECTOR, sectors);
|
|
|
|
SET_REG(ATA_COMMAND, CMD_SET_MULTIPLE_MODE);
|
2002-09-06 17:20:44 +00:00
|
|
|
|
|
|
|
if (!wait_for_rdy())
|
|
|
|
{
|
|
|
|
DEBUGF("set_multiple_mode() - CMD failed\n");
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-01-14 13:15:19 +00:00
|
|
|
static int set_features(void)
|
|
|
|
{
|
2007-02-18 19:28:29 +00:00
|
|
|
static struct {
|
2004-01-14 13:15:19 +00:00
|
|
|
unsigned char id_word;
|
|
|
|
unsigned char id_bit;
|
|
|
|
unsigned char subcommand;
|
|
|
|
unsigned char parameter;
|
|
|
|
} features[] = {
|
2008-03-09 13:24:42 +00:00
|
|
|
{ 83, 14, 0x03, 0 }, /* force PIO mode */
|
|
|
|
{ 83, 3, 0x05, 0x80 }, /* adv. power management: lowest w/o standby */
|
2004-01-14 13:15:19 +00:00
|
|
|
{ 83, 9, 0x42, 0x80 }, /* acoustic management: lowest noise */
|
|
|
|
{ 82, 6, 0xaa, 0 }, /* enable read look-ahead */
|
|
|
|
};
|
|
|
|
int i;
|
2004-03-02 09:55:23 +00:00
|
|
|
int pio_mode = 2;
|
|
|
|
|
|
|
|
/* Find out the highest supported PIO mode */
|
|
|
|
if(identify_info[64] & 2)
|
|
|
|
pio_mode = 4;
|
|
|
|
else
|
|
|
|
if(identify_info[64] & 1)
|
|
|
|
pio_mode = 3;
|
2004-01-14 13:15:19 +00:00
|
|
|
|
2008-03-09 13:24:42 +00:00
|
|
|
/* Update the table: set highest supported pio mode that we also support */
|
|
|
|
features[0].parameter = 8 + pio_mode;
|
|
|
|
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_SELECT, ata_device);
|
2004-01-14 13:15:19 +00:00
|
|
|
|
|
|
|
if (!wait_for_rdy()) {
|
|
|
|
DEBUGF("set_features() - not RDY\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-18 19:28:29 +00:00
|
|
|
for (i=0; i < (int)(sizeof(features)/sizeof(features[0])); i++) {
|
2004-01-14 13:15:19 +00:00
|
|
|
if (identify_info[features[i].id_word] & (1 << features[i].id_bit)) {
|
2005-01-26 12:53:48 +00:00
|
|
|
SET_REG(ATA_FEATURE, features[i].subcommand);
|
|
|
|
SET_REG(ATA_NSECTOR, features[i].parameter);
|
|
|
|
SET_REG(ATA_COMMAND, CMD_SET_FEATURES);
|
2004-01-14 13:15:19 +00:00
|
|
|
|
|
|
|
if (!wait_for_rdy()) {
|
|
|
|
DEBUGF("set_features() - CMD failed\n");
|
2004-03-02 09:55:23 +00:00
|
|
|
return -10 - i;
|
|
|
|
}
|
|
|
|
|
2008-03-09 13:24:42 +00:00
|
|
|
if((ATA_ALT_STATUS & STATUS_ERR) && (i != 1)) {
|
|
|
|
/* some CF cards don't like advanced powermanagement
|
|
|
|
even if they mark it as supported - go figure... */
|
2004-03-02 09:55:23 +00:00
|
|
|
if(ATA_ERROR & ERROR_ABRT) {
|
|
|
|
return -20 - i;
|
|
|
|
}
|
2004-01-14 13:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-03 13:12:55 +00:00
|
|
|
unsigned short* ata_get_identify(void)
|
|
|
|
{
|
|
|
|
return identify_info;
|
|
|
|
}
|
|
|
|
|
2004-11-23 22:00:41 +00:00
|
|
|
static int init_and_check(bool hard_reset)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (hard_reset)
|
|
|
|
{
|
|
|
|
/* This should reset both master and slave, we don't yet know what's in */
|
|
|
|
ata_device = 0;
|
|
|
|
if (ata_hard_reset())
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = master_slave_detect();
|
|
|
|
if (rc)
|
|
|
|
return -10 + rc;
|
|
|
|
|
|
|
|
/* symptom fix: else check_registers() below may fail */
|
|
|
|
if (hard_reset && !wait_for_bsy())
|
|
|
|
return -20;
|
|
|
|
|
|
|
|
rc = check_registers();
|
|
|
|
if (rc)
|
|
|
|
return -30 + rc;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-04-21 22:06:12 +00:00
|
|
|
int ata_init(void)
|
|
|
|
{
|
2008-01-18 12:32:03 +00:00
|
|
|
int rc = 0;
|
|
|
|
bool coldstart;
|
2003-07-09 07:18:47 +00:00
|
|
|
|
2008-01-18 12:32:03 +00:00
|
|
|
if ( !initialized ) {
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_init(&ata_mtx);
|
2008-01-18 12:32:03 +00:00
|
|
|
queue_init(&ata_queue, true);
|
|
|
|
}
|
|
|
|
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
2002-07-16 14:07:47 +00:00
|
|
|
|
2008-01-18 12:32:03 +00:00
|
|
|
/* must be called before ata_device_init() */
|
|
|
|
coldstart = ata_is_coldstart();
|
2005-04-04 09:12:12 +00:00
|
|
|
ata_led(false);
|
2006-03-07 13:25:19 +00:00
|
|
|
ata_device_init();
|
2003-07-18 21:55:06 +00:00
|
|
|
sleeping = false;
|
2002-06-30 13:10:42 +00:00
|
|
|
ata_enable(true);
|
2007-05-23 17:51:18 +00:00
|
|
|
#ifdef MAX_PHYS_SECTOR_SIZE
|
|
|
|
memset(§or_cache, 0, sizeof(sector_cache));
|
|
|
|
#endif
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-07-16 14:07:47 +00:00
|
|
|
if ( !initialized ) {
|
2008-02-11 07:42:11 +00:00
|
|
|
/* First call won't have multiple thread contention - this
|
|
|
|
* may return at any point without having to unlock */
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
2008-01-18 12:32:03 +00:00
|
|
|
|
2003-12-03 01:03:54 +00:00
|
|
|
if (!ide_powered()) /* somebody has switched it off */
|
|
|
|
{
|
|
|
|
ide_power_enable(true);
|
2007-11-03 23:57:24 +00:00
|
|
|
sleep(HZ/50); /* allow voltage to build up */
|
2003-12-03 01:03:54 +00:00
|
|
|
}
|
|
|
|
|
2004-11-23 22:00:41 +00:00
|
|
|
/* first try, hard reset at cold start only */
|
2006-12-19 09:27:41 +00:00
|
|
|
rc = init_and_check(coldstart);
|
2004-11-23 22:00:41 +00:00
|
|
|
|
2006-12-19 09:27:41 +00:00
|
|
|
if (rc)
|
2004-11-23 22:00:41 +00:00
|
|
|
{ /* failed? -> second try, always with hard reset */
|
|
|
|
DEBUGF("ata: init failed, retrying...\n");
|
|
|
|
rc = init_and_check(true);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
2003-07-09 07:18:47 +00:00
|
|
|
}
|
2002-09-05 15:25:08 +00:00
|
|
|
|
2003-07-09 16:46:46 +00:00
|
|
|
rc = identify();
|
2006-08-12 08:01:54 +00:00
|
|
|
|
2003-07-09 16:46:46 +00:00
|
|
|
if (rc)
|
2004-09-17 11:28:07 +00:00
|
|
|
return -40 + rc;
|
2006-08-12 08:01:54 +00:00
|
|
|
|
2002-09-05 15:25:08 +00:00
|
|
|
multisectors = identify_info[47] & 0xff;
|
2007-05-23 17:51:18 +00:00
|
|
|
if (multisectors == 0) /* Invalid multisector info, try with 16 */
|
|
|
|
multisectors = 16;
|
|
|
|
|
2002-09-05 15:25:08 +00:00
|
|
|
DEBUGF("ata: %d sectors per ata request\n",multisectors);
|
2005-01-20 23:00:11 +00:00
|
|
|
|
2007-05-23 17:51:18 +00:00
|
|
|
#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
|
|
|
|
|
2008-03-09 00:59:48 +00:00
|
|
|
total_sectors = identify_info[60] | (identify_info[61] << 16);
|
|
|
|
|
2006-12-20 22:08:29 +00:00
|
|
|
#ifdef HAVE_LBA48
|
2008-03-09 00:59:48 +00:00
|
|
|
if (identify_info[83] & 0x0400 /* 48 bit address support */
|
|
|
|
&& total_sectors == 0x0FFFFFFF) /* and disk size >= 128 GiB */
|
|
|
|
{ /* (needs BigLBA addressing) */
|
|
|
|
if (identify_info[102] || identify_info[103])
|
|
|
|
panicf("Unsupported disk size: >= 2^32 sectors");
|
|
|
|
|
|
|
|
total_sectors = identify_info[100] | (identify_info[101] << 16);
|
2006-12-20 22:08:29 +00:00
|
|
|
lba48 = true; /* use BigLBA */
|
2006-12-19 22:40:23 +00:00
|
|
|
}
|
|
|
|
#endif
|
2004-09-17 11:28:07 +00:00
|
|
|
rc = freeze_lock();
|
2006-08-12 08:01:54 +00:00
|
|
|
|
2004-09-17 11:28:07 +00:00
|
|
|
if (rc)
|
|
|
|
return -50 + rc;
|
|
|
|
|
2004-01-14 13:15:19 +00:00
|
|
|
rc = set_features();
|
|
|
|
if (rc)
|
|
|
|
return -60 + rc;
|
|
|
|
|
2008-02-11 07:42:11 +00:00
|
|
|
mutex_lock(&ata_mtx); /* Balance unlock below */
|
|
|
|
|
2003-07-11 19:11:06 +00:00
|
|
|
last_disk_activity = current_tick;
|
2002-07-16 14:07:47 +00:00
|
|
|
create_thread(ata_thread, ata_stack,
|
2007-10-16 01:25:17 +00:00
|
|
|
sizeof(ata_stack), 0, ata_thread_name
|
2008-02-11 07:42:11 +00:00
|
|
|
IF_PRIO(, PRIORITY_USER_INTERFACE)
|
2007-10-16 01:25:17 +00:00
|
|
|
IF_COP(, CPU));
|
2002-07-16 14:07:47 +00:00
|
|
|
initialized = true;
|
2005-01-20 23:00:11 +00:00
|
|
|
|
2002-07-16 14:07:47 +00:00
|
|
|
}
|
2003-07-09 16:46:46 +00:00
|
|
|
rc = set_multiple_mode(multisectors);
|
|
|
|
if (rc)
|
2008-01-18 12:32:03 +00:00
|
|
|
rc = -70 + rc;
|
2002-05-22 14:37:36 +00:00
|
|
|
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
2008-01-18 12:32:03 +00:00
|
|
|
return rc;
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
2005-04-04 09:12:12 +00:00
|
|
|
|
2007-02-18 05:07:19 +00:00
|
|
|
#if (CONFIG_LED == LED_REAL)
|
2006-12-03 22:13:44 +00:00
|
|
|
void ata_set_led_enabled(bool enabled)
|
|
|
|
{
|
2005-04-04 09:12:12 +00:00
|
|
|
ata_led_enabled = enabled;
|
2006-12-03 22:13:44 +00:00
|
|
|
if (ata_led_enabled)
|
2005-04-04 09:12:12 +00:00
|
|
|
led(ata_led_on);
|
2006-12-03 22:13:44 +00:00
|
|
|
else
|
2005-04-04 09:12:12 +00:00
|
|
|
led(false);
|
|
|
|
}
|
2005-06-04 23:15:52 +00:00
|
|
|
#endif
|