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"
|
2002-04-27 20:14:01 +00:00
|
|
|
#include "sh7034.h"
|
|
|
|
#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"
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-08-02 06:01:22 +00:00
|
|
|
/* Define one of USE_STANDBY, USE_SLEEP or USE_POWEROFF */
|
|
|
|
#define USE_STANDBY
|
|
|
|
|
|
|
|
/* We can only use power off on the recorder */
|
|
|
|
#if !defined(ARCHOS_RECORDER) && defined(USE_POWEROFF)
|
|
|
|
#undef USE_POWEROFF
|
|
|
|
#define USE_STANDBY
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-06-12 09:28:22 +00:00
|
|
|
#define SECTOR_SIZE 512
|
2002-04-21 22:06:12 +00:00
|
|
|
#define ATA_DATA (*((volatile unsigned short*)0x06104100))
|
|
|
|
#define ATA_ERROR (*((volatile unsigned char*)0x06100101))
|
|
|
|
#define ATA_FEATURE ATA_ERROR
|
|
|
|
#define ATA_NSECTOR (*((volatile unsigned char*)0x06100102))
|
|
|
|
#define ATA_SECTOR (*((volatile unsigned char*)0x06100103))
|
|
|
|
#define ATA_LCYL (*((volatile unsigned char*)0x06100104))
|
|
|
|
#define ATA_HCYL (*((volatile unsigned char*)0x06100105))
|
|
|
|
#define ATA_SELECT (*((volatile unsigned char*)0x06100106))
|
|
|
|
#define ATA_COMMAND (*((volatile unsigned char*)0x06100107))
|
2002-05-08 08:27:44 +00:00
|
|
|
#define ATA_STATUS (*((volatile unsigned char*)0x06100107))
|
2002-06-12 13:51:31 +00:00
|
|
|
|
|
|
|
#define ATA_CONTROL1 ((volatile unsigned char*)0x06200206)
|
|
|
|
#define ATA_CONTROL2 ((volatile unsigned char*)0x06200306)
|
|
|
|
|
|
|
|
#define ATA_CONTROL (*ata_control)
|
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 STATUS_BSY 0x80
|
|
|
|
#define STATUS_RDY 0x40
|
|
|
|
#define STATUS_DRQ 0x08
|
|
|
|
#define STATUS_ERR 0x01
|
|
|
|
|
|
|
|
#define CONTROL_nIEN 0x02
|
|
|
|
#define CONTROL_SRST 0x04
|
|
|
|
|
|
|
|
#define CMD_READ_SECTORS 0x20
|
|
|
|
#define CMD_WRITE_SECTORS 0x30
|
|
|
|
#define CMD_STANDBY_IMMEDIATE 0xE0
|
|
|
|
#define CMD_STANDBY 0xE2
|
|
|
|
#define CMD_SLEEP 0xE6
|
|
|
|
#define CMD_SECURITY_FREEZE_LOCK 0xF5
|
|
|
|
|
2002-07-16 14:07:47 +00:00
|
|
|
#define Q_SLEEP 0
|
|
|
|
|
2002-05-22 14:37:36 +00:00
|
|
|
static struct mutex ata_mtx;
|
2002-07-02 14:23:30 +00:00
|
|
|
char ata_device; /* device 0 (master) or 1 (slave) */
|
|
|
|
int ata_io_address; /* 0x300 or 0x200, only valid on recorder */
|
2002-06-12 13:51:31 +00:00
|
|
|
static volatile unsigned char* ata_control;
|
|
|
|
|
2002-06-26 12:36:29 +00:00
|
|
|
bool old_recorder = false;
|
2002-07-16 12:18:17 +00:00
|
|
|
static bool sleeping = false;
|
2002-07-16 14:07:47 +00:00
|
|
|
static int sleep_timer = 0;
|
|
|
|
static int sleep_timeout = 5*HZ;
|
|
|
|
static char ata_stack[DEFAULT_STACK_SIZE];
|
|
|
|
static char ata_thread_name[] = "ata";
|
|
|
|
static struct event_queue ata_queue;
|
|
|
|
static bool initialized = false;
|
2002-08-15 12:42:37 +00:00
|
|
|
static bool delayed_write = false;
|
|
|
|
static unsigned char delayed_sector[SECTOR_SIZE];
|
|
|
|
static int delayed_sector_num;
|
2002-06-26 12:36:29 +00:00
|
|
|
|
2002-08-02 06:01:22 +00:00
|
|
|
#ifdef USE_POWEROFF
|
|
|
|
static int ata_power_on(void);
|
|
|
|
#endif
|
|
|
|
|
2002-08-01 08:15:58 +00:00
|
|
|
static int wait_for_bsy(void) __attribute__ ((section (".icode")));
|
2002-04-21 22:06:12 +00:00
|
|
|
static int wait_for_bsy(void)
|
|
|
|
{
|
2002-05-08 08:27:44 +00:00
|
|
|
int timeout = current_tick + HZ*4;
|
2002-04-27 14:19:00 +00:00
|
|
|
while (TIME_BEFORE(current_tick, timeout) && (ATA_ALT_STATUS & STATUS_BSY))
|
2002-04-21 22:06:12 +00:00
|
|
|
yield();
|
|
|
|
|
|
|
|
if (TIME_BEFORE(current_tick, timeout))
|
2002-05-07 22:59:03 +00:00
|
|
|
{
|
2002-04-21 22:06:12 +00:00
|
|
|
return 1;
|
2002-05-07 22:59:03 +00:00
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
else
|
2002-05-07 22:59:03 +00:00
|
|
|
{
|
2002-04-21 22:06:12 +00:00
|
|
|
return 0; /* timeout */
|
2002-05-07 22:59:03 +00:00
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2002-08-01 08:15:58 +00:00
|
|
|
static int wait_for_rdy(void) __attribute__ ((section (".icode")));
|
2002-04-21 22:06:12 +00:00
|
|
|
static int wait_for_rdy(void)
|
|
|
|
{
|
|
|
|
if (!wait_for_bsy())
|
|
|
|
return 0;
|
2002-04-27 14:19:00 +00:00
|
|
|
return ATA_ALT_STATUS & STATUS_RDY;
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2002-08-01 08:15:58 +00:00
|
|
|
static int wait_for_start_of_transfer(void) __attribute__ ((section (".icode")));
|
2002-04-21 22:06:12 +00:00
|
|
|
static int wait_for_start_of_transfer(void)
|
|
|
|
{
|
|
|
|
if (!wait_for_bsy())
|
|
|
|
return 0;
|
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
|
|
|
|
2002-08-01 08:15:58 +00:00
|
|
|
static int wait_for_end_of_transfer(void) __attribute__ ((section (".icode")));
|
2002-04-21 22:06:12 +00:00
|
|
|
static int wait_for_end_of_transfer(void)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-08-01 08:15:58 +00:00
|
|
|
int ata_read_sectors(unsigned long start,
|
2002-08-14 16:37:28 +00:00
|
|
|
int count,
|
2002-08-01 08:15:58 +00:00
|
|
|
void* buf) __attribute__ ((section (".icode")));
|
2002-04-21 22:06:12 +00:00
|
|
|
int ata_read_sectors(unsigned long start,
|
2002-08-14 16:37:28 +00:00
|
|
|
int count,
|
2002-04-22 12:34:25 +00:00
|
|
|
void* buf)
|
2002-04-21 22:06:12 +00:00
|
|
|
{
|
|
|
|
int i;
|
2002-05-16 21:28:21 +00:00
|
|
|
int ret = 0;
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-08-02 06:01:22 +00:00
|
|
|
#ifndef USE_STANDBY
|
2002-07-16 12:18:17 +00:00
|
|
|
if ( sleeping ) {
|
2002-08-02 06:01:22 +00:00
|
|
|
#ifdef USE_POWEROFF
|
|
|
|
if (ata_power_on()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#else
|
2002-07-16 12:18:17 +00:00
|
|
|
if (ata_soft_reset()) {
|
|
|
|
return -1;
|
|
|
|
}
|
2002-08-02 06:01:22 +00:00
|
|
|
#endif
|
2002-07-16 12:18:17 +00:00
|
|
|
}
|
2002-08-02 06:01:22 +00:00
|
|
|
#endif
|
2002-05-16 21:28:21 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
2002-07-16 14:07:47 +00:00
|
|
|
sleep_timer = sleep_timeout;
|
|
|
|
|
2002-04-21 22:06:12 +00:00
|
|
|
if (!wait_for_rdy())
|
2002-05-16 21:28:21 +00:00
|
|
|
{
|
|
|
|
mutex_unlock(&ata_mtx);
|
2002-05-08 08:27:44 +00:00
|
|
|
return -1;
|
2002-05-16 21:28:21 +00:00
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-05-13 19:22:38 +00:00
|
|
|
led(true);
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-08-14 16:37:28 +00:00
|
|
|
if ( count == 256 )
|
|
|
|
ATA_NSECTOR = 0; /* 0 means 256 sectors */
|
|
|
|
else
|
|
|
|
ATA_NSECTOR = (unsigned char)count;
|
|
|
|
|
2002-04-21 22:06:12 +00:00
|
|
|
ATA_SECTOR = start & 0xff;
|
|
|
|
ATA_LCYL = (start >> 8) & 0xff;
|
|
|
|
ATA_HCYL = (start >> 16) & 0xff;
|
2002-07-02 14:23:30 +00:00
|
|
|
ATA_SELECT = ((start >> 24) & 0xf) | SELECT_LBA | ata_device;
|
2002-04-21 22:06:12 +00:00
|
|
|
ATA_COMMAND = CMD_READ_SECTORS;
|
|
|
|
|
|
|
|
for (i=0; i<count; i++) {
|
|
|
|
int j;
|
|
|
|
if (!wait_for_start_of_transfer())
|
2002-05-16 21:28:21 +00:00
|
|
|
{
|
|
|
|
mutex_unlock(&ata_mtx);
|
2002-05-08 08:27:44 +00:00
|
|
|
return -1;
|
2002-05-16 21:28:21 +00:00
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-08-14 14:34:54 +00:00
|
|
|
/* if destination address is odd, use byte copying,
|
|
|
|
otherwise use word copying */
|
|
|
|
if ( (unsigned int)buf & 1 ) {
|
|
|
|
for (j=0; j<SECTOR_SIZE/2; j++) {
|
|
|
|
unsigned short tmp = SWAB16(ATA_DATA);
|
|
|
|
((unsigned char*)buf)[j*2] = tmp >> 8;
|
|
|
|
((unsigned char*)buf)[j*2+1] = tmp & 0xff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (j=0; j<SECTOR_SIZE/2; j++)
|
|
|
|
((unsigned short*)buf)[j] = SWAB16(ATA_DATA);
|
|
|
|
}
|
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; /* Advance one sector */
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2002-05-13 19:22:38 +00:00
|
|
|
led(false);
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-05-08 08:27:44 +00:00
|
|
|
if(!wait_for_end_of_transfer())
|
2002-05-16 21:28:21 +00:00
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
mutex_unlock(&ata_mtx);
|
2002-08-15 12:42:37 +00:00
|
|
|
|
|
|
|
if ( delayed_write )
|
|
|
|
ata_flush();
|
|
|
|
|
2002-05-16 21:28:21 +00:00
|
|
|
return ret;
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ata_write_sectors(unsigned long start,
|
2002-08-14 16:37:28 +00:00
|
|
|
int count,
|
2002-04-22 12:34:25 +00:00
|
|
|
void* buf)
|
2002-04-21 22:06:12 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2002-08-02 06:01:22 +00:00
|
|
|
#ifndef USE_STANDBY
|
|
|
|
#ifdef USE_POWEROFF
|
|
|
|
if (ata_power_on()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#else
|
2002-07-16 12:18:17 +00:00
|
|
|
if (ata_soft_reset()) {
|
|
|
|
return -1;
|
|
|
|
}
|
2002-08-02 06:01:22 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
2002-05-16 21:28:21 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
2002-07-16 14:07:47 +00:00
|
|
|
sleep_timer = sleep_timeout;
|
2002-05-16 21:28:21 +00:00
|
|
|
|
2002-04-21 22:06:12 +00:00
|
|
|
if (!wait_for_rdy())
|
2002-05-16 21:28:21 +00:00
|
|
|
{
|
|
|
|
mutex_unlock(&ata_mtx);
|
2002-04-21 22:06:12 +00:00
|
|
|
return 0;
|
2002-05-16 21:28:21 +00:00
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-05-13 19:22:38 +00:00
|
|
|
led(true);
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-08-14 16:37:28 +00:00
|
|
|
if ( count == 256 )
|
|
|
|
ATA_NSECTOR = 0; /* 0 means 256 sectors */
|
|
|
|
else
|
|
|
|
ATA_NSECTOR = (unsigned char)count;
|
2002-04-21 22:06:12 +00:00
|
|
|
ATA_SECTOR = start & 0xff;
|
|
|
|
ATA_LCYL = (start >> 8) & 0xff;
|
|
|
|
ATA_HCYL = (start >> 16) & 0xff;
|
2002-07-02 14:23:30 +00:00
|
|
|
ATA_SELECT = ((start >> 24) & 0xf) | SELECT_LBA | ata_device;
|
2002-04-21 22:06:12 +00:00
|
|
|
ATA_COMMAND = CMD_WRITE_SECTORS;
|
|
|
|
|
|
|
|
for (i=0; i<count; i++) {
|
|
|
|
int j;
|
|
|
|
if (!wait_for_start_of_transfer())
|
2002-05-16 21:28:21 +00:00
|
|
|
{
|
|
|
|
mutex_unlock(&ata_mtx);
|
2002-04-21 22:06:12 +00:00
|
|
|
return 0;
|
2002-05-16 21:28:21 +00:00
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-06-12 09:28:22 +00:00
|
|
|
for (j=0; j<SECTOR_SIZE/2; j++)
|
2002-04-22 12:34:25 +00:00
|
|
|
ATA_DATA = SWAB16(((unsigned short*)buf)[j]);
|
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-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2002-05-13 19:22:38 +00:00
|
|
|
led(false);
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-05-16 21:28:21 +00:00
|
|
|
i = wait_for_end_of_transfer();
|
|
|
|
|
|
|
|
mutex_unlock(&ata_mtx);
|
2002-08-15 12:42:37 +00:00
|
|
|
|
|
|
|
if ( delayed_write )
|
|
|
|
ata_flush();
|
|
|
|
|
2002-05-16 21:28:21 +00:00
|
|
|
return i;
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2002-08-15 12:42:37 +00:00
|
|
|
extern void ata_delayed_write(unsigned long sector, void* buf)
|
|
|
|
{
|
|
|
|
memcpy(delayed_sector, buf, SECTOR_SIZE);
|
|
|
|
delayed_sector_num = sector;
|
|
|
|
delayed_write = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void ata_flush(void)
|
|
|
|
{
|
|
|
|
if ( delayed_write ) {
|
2002-08-16 14:41:47 +00:00
|
|
|
DEBUGF("ata_flush()\n");
|
2002-08-15 12:42:37 +00:00
|
|
|
delayed_write = false;
|
|
|
|
ata_write_sectors(delayed_sector_num, 1, delayed_sector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-04-21 22:06:12 +00:00
|
|
|
static int check_registers(void)
|
|
|
|
{
|
|
|
|
if ( ATA_STATUS & STATUS_BSY )
|
2002-05-24 11:25:24 +00:00
|
|
|
return -1;
|
2002-04-21 22:06:12 +00:00
|
|
|
|
|
|
|
ATA_NSECTOR = 0xa5;
|
|
|
|
ATA_SECTOR = 0x5a;
|
|
|
|
ATA_LCYL = 0xaa;
|
|
|
|
ATA_HCYL = 0x55;
|
|
|
|
|
|
|
|
if ((ATA_NSECTOR == 0xa5) &&
|
|
|
|
(ATA_SECTOR == 0x5a) &&
|
|
|
|
(ATA_LCYL == 0xaa) &&
|
|
|
|
(ATA_HCYL == 0x55))
|
2002-05-24 11:25:24 +00:00
|
|
|
return 0;
|
2002-05-16 21:28:21 +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)
|
|
|
|
{
|
|
|
|
if (!wait_for_rdy())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ATA_COMMAND = CMD_SECURITY_FREEZE_LOCK;
|
|
|
|
|
|
|
|
if (!wait_for_rdy())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2002-05-16 21:28:21 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
mutex_lock(&ata_mtx);
|
|
|
|
|
2002-07-16 14:07:47 +00:00
|
|
|
if(!wait_for_rdy()) {
|
2002-05-16 21:28:21 +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
|
|
|
|
2002-08-02 06:01:22 +00:00
|
|
|
#ifdef USE_POWEROFF
|
2002-08-01 12:00:03 +00:00
|
|
|
ide_power_enable(false);
|
|
|
|
#else
|
2002-07-16 14:07:47 +00:00
|
|
|
ATA_SELECT = ata_device;
|
2002-08-02 06:01:22 +00:00
|
|
|
#ifdef USE_SLEEP
|
|
|
|
ATA_COMMAND = CMD_SLEEP;
|
|
|
|
#else
|
2002-07-27 19:24:27 +00:00
|
|
|
ATA_COMMAND = CMD_STANDBY;
|
2002-08-02 06:01:22 +00:00
|
|
|
#endif
|
2002-04-27 14:19:00 +00:00
|
|
|
|
|
|
|
if (!wait_for_rdy())
|
2002-05-16 21:28:21 +00:00
|
|
|
ret = -1;
|
2002-08-01 12:00:03 +00:00
|
|
|
#endif
|
2002-07-16 14:07:47 +00:00
|
|
|
sleeping = true;
|
|
|
|
sleep_timer = 0;
|
2002-05-16 21:28:21 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
|
|
|
return ret;
|
2002-04-27 14:19:00 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 12:18:17 +00:00
|
|
|
int ata_sleep(void)
|
|
|
|
{
|
2002-07-16 14:07:47 +00:00
|
|
|
queue_post(&ata_queue, Q_SLEEP, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-07-16 12:18:17 +00:00
|
|
|
|
2002-07-16 14:07:47 +00:00
|
|
|
static void ata_thread(void)
|
|
|
|
{
|
|
|
|
struct event ev;
|
2002-07-16 12:18:17 +00:00
|
|
|
|
2002-07-16 14:07:47 +00:00
|
|
|
while (1) {
|
|
|
|
queue_wait(&ata_queue, &ev);
|
|
|
|
switch ( ev.id ) {
|
2002-07-23 15:02:25 +00:00
|
|
|
case SYS_USB_CONNECTED:
|
|
|
|
/* 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);
|
|
|
|
break;
|
2002-07-16 14:07:47 +00:00
|
|
|
|
|
|
|
case Q_SLEEP:
|
|
|
|
ata_perform_sleep();
|
|
|
|
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-07-16 14:07:47 +00:00
|
|
|
static void ata_tick(void)
|
|
|
|
{
|
|
|
|
if (sleep_timer) {
|
|
|
|
sleep_timer--;
|
|
|
|
if (!sleep_timer)
|
|
|
|
queue_post(&ata_queue, 0, NULL);
|
|
|
|
}
|
2002-07-16 12:18:17 +00:00
|
|
|
}
|
|
|
|
|
2002-04-21 22:06:12 +00:00
|
|
|
int ata_hard_reset(void)
|
|
|
|
{
|
2002-05-16 21:28:21 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
mutex_lock(&ata_mtx);
|
|
|
|
|
2002-05-24 11:25:24 +00:00
|
|
|
PADR &= ~0x0200;
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-05-07 22:59:03 +00:00
|
|
|
sleep(2);
|
|
|
|
|
2002-05-24 11:25:24 +00:00
|
|
|
PADR |= 0x0200;
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-05-16 21:28:21 +00:00
|
|
|
ret = wait_for_rdy();
|
|
|
|
|
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-07-16 12:18:17 +00:00
|
|
|
sleeping = false;
|
2002-05-16 21:28:21 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
|
|
|
return ret;
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ata_soft_reset(void)
|
|
|
|
{
|
2002-05-16 21:28:21 +00:00
|
|
|
int ret;
|
2002-06-30 13:10:42 +00:00
|
|
|
int retry_count;
|
2002-05-16 21:28:21 +00:00
|
|
|
|
|
|
|
mutex_lock(&ata_mtx);
|
2002-08-01 12:00:03 +00:00
|
|
|
|
2002-07-02 14:23:30 +00:00
|
|
|
ATA_SELECT = SELECT_LBA | ata_device;
|
2002-04-21 22:06:12 +00:00
|
|
|
ATA_CONTROL = CONTROL_nIEN|CONTROL_SRST;
|
|
|
|
sleep(HZ/20000); /* >= 5us */
|
|
|
|
|
|
|
|
ATA_CONTROL = CONTROL_nIEN;
|
|
|
|
sleep(HZ/400); /* >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
|
|
|
|
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-07-16 12:18:17 +00:00
|
|
|
|
|
|
|
sleeping = false;
|
2002-05-16 21:28:21 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
|
|
|
return ret;
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2002-08-02 06:01:22 +00:00
|
|
|
#ifdef USE_POWEROFF
|
|
|
|
static int ata_power_on(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int retry_count;
|
|
|
|
|
|
|
|
mutex_lock(&ata_mtx);
|
|
|
|
|
|
|
|
ide_power_enable(true);
|
|
|
|
sleep(HZ/2);
|
|
|
|
|
|
|
|
ATA_CONTROL = CONTROL_nIEN;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
sleeping = false;
|
|
|
|
mutex_unlock(&ata_mtx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-06-12 13:51:31 +00:00
|
|
|
static int master_slave_detect(void)
|
2002-05-22 14:37:36 +00:00
|
|
|
{
|
|
|
|
/* master? */
|
|
|
|
ATA_SELECT = 0;
|
|
|
|
if ( ATA_STATUS & STATUS_RDY ) {
|
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? */
|
|
|
|
ATA_SELECT = SELECT_DEVICE1;
|
|
|
|
if ( ATA_STATUS & STATUS_RDY ) {
|
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-06-12 13:51:31 +00:00
|
|
|
static int io_address_detect(void)
|
|
|
|
{
|
2002-06-18 12:53:02 +00:00
|
|
|
unsigned char tmp = ATA_STATUS & 0xf9; /* Mask the IDX and CORR bits */
|
2002-06-12 13:51:31 +00:00
|
|
|
unsigned char dummy;
|
|
|
|
|
|
|
|
/* We compare the STATUS register with the ALT_STATUS register, which
|
|
|
|
is located at the same address as CONTROL. If they are the same, we
|
|
|
|
assume that we have the correct address.
|
|
|
|
|
|
|
|
We can't read the ATA_STATUS directly, since the read data will stay
|
|
|
|
on the data bus if the following read does not assert the Chip Select
|
|
|
|
to the ATA controller. We read a register that we know exists to make
|
|
|
|
sure that the data on the bus isn't identical to the STATUS register
|
|
|
|
contents. */
|
2002-06-18 12:53:02 +00:00
|
|
|
ATA_SECTOR = 0;
|
2002-06-12 13:51:31 +00:00
|
|
|
dummy = ATA_SECTOR;
|
2002-06-18 13:19:25 +00:00
|
|
|
if(tmp == ((*ATA_CONTROL2) & 0xf9))
|
2002-06-12 13:51:31 +00:00
|
|
|
{
|
|
|
|
DEBUGF("CONTROL is at 0x306\n");
|
2002-07-02 14:23:30 +00:00
|
|
|
ata_io_address = 0x300; /* For debug purposes only */
|
2002-06-26 12:36:29 +00:00
|
|
|
old_recorder = true;
|
2002-06-12 13:51:31 +00:00
|
|
|
ata_control = ATA_CONTROL2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGF("CONTROL is at 0x206\n");
|
2002-07-02 14:23:30 +00:00
|
|
|
ata_io_address = 0x200; /* For debug purposes only */
|
2002-06-26 12:36:29 +00:00
|
|
|
old_recorder = false;
|
2002-06-12 13:51:31 +00:00
|
|
|
ata_control = ATA_CONTROL1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Let's check again, to be sure */
|
|
|
|
if(tmp != ATA_CONTROL)
|
|
|
|
{
|
|
|
|
DEBUGF("ATA I/O address detection failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-30 13:10:42 +00:00
|
|
|
void ata_enable(bool on)
|
|
|
|
{
|
|
|
|
if(on)
|
2002-07-23 15:02:25 +00:00
|
|
|
PADR &= ~0x80; /* enable ATA */
|
2002-06-30 13:10:42 +00:00
|
|
|
else
|
2002-07-23 15:02:25 +00:00
|
|
|
PADR |= 0x80; /* disable ATA */
|
2002-06-30 13:10:42 +00:00
|
|
|
|
|
|
|
PAIOR |= 0x80;
|
|
|
|
}
|
|
|
|
|
2002-04-21 22:06:12 +00:00
|
|
|
int ata_init(void)
|
|
|
|
{
|
2002-05-16 21:28:21 +00:00
|
|
|
mutex_init(&ata_mtx);
|
2002-07-16 14:07:47 +00:00
|
|
|
|
2002-05-13 19:22:38 +00:00
|
|
|
led(false);
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-06-30 13:10:42 +00:00
|
|
|
ata_enable(true);
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2002-07-16 14:07:47 +00:00
|
|
|
if ( !initialized ) {
|
2002-07-23 15:02:25 +00:00
|
|
|
if (master_slave_detect())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (io_address_detect())
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
if (check_registers())
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
if (freeze_lock())
|
|
|
|
return -4;
|
|
|
|
|
2002-07-16 14:07:47 +00:00
|
|
|
queue_init(&ata_queue);
|
|
|
|
create_thread(ata_thread, ata_stack,
|
|
|
|
sizeof(ata_stack), ata_thread_name);
|
|
|
|
tick_add_task(ata_tick);
|
|
|
|
initialized = true;
|
|
|
|
}
|
2002-05-22 14:37:36 +00:00
|
|
|
|
2002-04-21 22:06:12 +00:00
|
|
|
ATA_SELECT = SELECT_LBA;
|
|
|
|
ATA_CONTROL = CONTROL_nIEN;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|