2002-04-21 22:06:12 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Alan Korr
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-04-21 22:06:12 +00:00
|
|
|
*
|
|
|
|
* 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>
|
2011-01-02 22:51:47 +00:00
|
|
|
#include <inttypes.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-08-01 12:00:03 +00:00
|
|
|
#include "power.h"
|
2002-08-15 12:54:52 +00:00
|
|
|
#include "string.h"
|
2011-12-08 21:23:53 +00:00
|
|
|
#include "ata-driver.h"
|
2011-01-02 23:09:01 +00:00
|
|
|
#include "ata-defines.h"
|
2017-03-13 02:05:44 +00:00
|
|
|
#include "fs_defines.h"
|
2008-11-01 16:14:28 +00:00
|
|
|
#include "storage.h"
|
2006-03-07 13:25:19 +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
|
2010-02-01 22:59:36 +00:00
|
|
|
#define CMD_WRITE_MULTIPLE_EXT 0x39
|
2002-09-05 15:25:08 +00:00
|
|
|
#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
|
2009-03-12 02:01:25 +00:00
|
|
|
#ifdef HAVE_ATA_DMA
|
|
|
|
#define CMD_READ_DMA 0xC8
|
|
|
|
#define CMD_READ_DMA_EXT 0x25
|
|
|
|
#define CMD_WRITE_DMA 0xCA
|
|
|
|
#define CMD_WRITE_DMA_EXT 0x35
|
|
|
|
#endif
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2009-11-24 18:03:57 +00:00
|
|
|
#define READWRITE_TIMEOUT 5*HZ
|
2002-09-24 14:23:18 +00:00
|
|
|
|
2007-01-23 15:43:37 +00:00
|
|
|
#ifdef HAVE_ATA_POWER_OFF
|
|
|
|
#define ATA_POWER_OFF_TIMEOUT 2*HZ
|
|
|
|
#endif
|
|
|
|
|
2014-12-30 16:36:59 +00:00
|
|
|
#if defined(HAVE_USBSTACK)
|
2017-03-15 05:51:54 +00:00
|
|
|
#define ATA_ACTIVE_IN_USB 1
|
|
|
|
#else
|
|
|
|
#define ATA_ACTIVE_IN_USB 0
|
2008-10-15 19:05:00 +00:00
|
|
|
#endif
|
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
enum {
|
|
|
|
ATA_BOOT = -1,
|
|
|
|
ATA_OFF,
|
|
|
|
ATA_SLEEPING,
|
|
|
|
ATA_SPINUP,
|
|
|
|
ATA_ON,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int ata_state = ATA_BOOT;
|
|
|
|
|
2008-04-06 04:34:57 +00:00
|
|
|
static struct mutex ata_mtx SHAREDBSS_ATTR;
|
2008-07-18 22:32:44 +00:00
|
|
|
static int ata_device; /* device 0 (master) or 1 (slave) */
|
2002-06-12 13:51:31 +00:00
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
static int 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
|
2017-03-15 05:51:54 +00:00
|
|
|
|
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
|
2002-06-26 12:36:29 +00:00
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
static long last_disk_activity = -1;
|
2017-03-15 05:51:54 +00:00
|
|
|
#ifdef HAVE_ATA_POWER_OFF
|
|
|
|
static long power_off_tick;
|
|
|
|
#endif
|
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
|
|
|
|
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
|
|
|
|
|
2009-03-12 02:01:25 +00:00
|
|
|
#ifdef HAVE_ATA_DMA
|
|
|
|
static int dma_mode = 0;
|
|
|
|
#endif
|
|
|
|
|
2011-11-27 17:40:57 +00:00
|
|
|
#ifdef HAVE_ATA_POWER_OFF
|
2002-08-02 06:01:22 +00:00
|
|
|
static int ata_power_on(void);
|
2011-11-27 17:40:57 +00:00
|
|
|
#endif
|
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
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
static inline void keep_ata_active(void)
|
|
|
|
{
|
|
|
|
last_disk_activity = current_tick;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void schedule_ata_sleep(long from_now)
|
|
|
|
{
|
|
|
|
last_disk_activity = current_tick - sleep_timeout + from_now;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool ata_sleep_timed_out(void)
|
|
|
|
{
|
|
|
|
return sleep_timeout &&
|
|
|
|
TIME_AFTER(current_tick, last_disk_activity + sleep_timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void schedule_ata_power_off(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_ATA_POWER_OFF
|
|
|
|
power_off_tick = current_tick + ATA_POWER_OFF_TIMEOUT;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool ata_power_off_timed_out(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_ATA_POWER_OFF
|
|
|
|
return TIME_AFTER(current_tick, power_off_tick);
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-01-22 09:23:31 +00:00
|
|
|
#ifndef ATA_TARGET_POLLING
|
2012-05-08 13:58:09 +00:00
|
|
|
static ICODE_ATTR 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;
|
2008-05-08 05:39:29 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2011-01-02 22:51:47 +00:00
|
|
|
if (!(ATA_IN8(ATA_STATUS) & STATUS_BSY))
|
2008-05-08 05:39:29 +00:00
|
|
|
return 1;
|
2017-03-15 05:51:54 +00:00
|
|
|
keep_ata_active();
|
2008-03-25 02:34:12 +00:00
|
|
|
yield();
|
2008-05-08 05:39:29 +00:00
|
|
|
} while (TIME_BEFORE(current_tick, timeout));
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2008-05-08 05:39:29 +00:00
|
|
|
return 0; /* timeout */
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2012-05-08 13:58:09 +00:00
|
|
|
static ICODE_ATTR 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;
|
2008-05-08 05:39:29 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2011-01-02 22:51:47 +00:00
|
|
|
if (ATA_IN8(ATA_ALT_STATUS) & STATUS_RDY)
|
2008-05-08 05:39:29 +00:00
|
|
|
return 1;
|
2017-03-15 05:51:54 +00:00
|
|
|
keep_ata_active();
|
2008-03-25 02:34:12 +00:00
|
|
|
yield();
|
2008-05-08 05:39:29 +00:00
|
|
|
} while (TIME_BEFORE(current_tick, timeout));
|
2002-08-29 11:50:57 +00:00
|
|
|
|
2008-05-08 05:39:29 +00:00
|
|
|
return 0; /* timeout */
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
2011-01-22 09:23:31 +00:00
|
|
|
#else
|
|
|
|
#define wait_for_bsy ata_wait_for_bsy
|
|
|
|
#define wait_for_rdy ata_wait_for_rdy
|
|
|
|
#endif
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
static int ata_perform_wakeup(int state)
|
|
|
|
{
|
|
|
|
if (state > ATA_OFF) {
|
|
|
|
if (perform_soft_reset()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef HAVE_ATA_POWER_OFF
|
|
|
|
else {
|
|
|
|
if (ata_power_on()) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ata_perform_sleep(void)
|
|
|
|
{
|
|
|
|
ATA_OUT8(ATA_SELECT, ata_device);
|
|
|
|
|
|
|
|
if(!wait_for_rdy()) {
|
|
|
|
DEBUGF("ata_perform_sleep() - not RDY\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ATA_OUT8(ATA_COMMAND, CMD_SLEEP);
|
|
|
|
|
|
|
|
if (!wait_for_rdy())
|
|
|
|
{
|
|
|
|
DEBUGF("ata_perform_sleep() - CMD failed\n");
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-08 13:58:09 +00:00
|
|
|
static ICODE_ATTR 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
|
|
|
|
2011-01-02 22:51:47 +00:00
|
|
|
return (ATA_IN8(ATA_ALT_STATUS) & (STATUS_BSY|STATUS_DRQ)) == STATUS_DRQ;
|
2002-04-27 14:19:00 +00:00
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
|
2012-05-08 13:58:09 +00:00
|
|
|
static ICODE_ATTR int wait_for_end_of_transfer(void)
|
2002-04-21 22:06:12 +00:00
|
|
|
{
|
|
|
|
if (!wait_for_bsy())
|
|
|
|
return 0;
|
2011-01-02 22:51:47 +00:00
|
|
|
return (ATA_IN8(ATA_ALT_STATUS) &
|
2009-11-24 18:09:21 +00:00
|
|
|
(STATUS_BSY|STATUS_RDY|STATUS_DF|STATUS_DRQ|STATUS_ERR))
|
|
|
|
== 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
|
2012-05-08 13:58:09 +00:00
|
|
|
static ICODE_ATTR 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
|
|
|
{
|
2011-01-02 22:51:47 +00:00
|
|
|
tmp = ATA_IN16(ATA_DATA);
|
|
|
|
#if 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
|
|
|
{
|
2011-01-02 22:51:47 +00:00
|
|
|
*wbuf = ATA_IN16(ATA_DATA);
|
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
|
|
|
|
2009-11-24 18:03:57 +00:00
|
|
|
#ifndef ATA_OPTIMIZED_WRITING
|
2012-05-08 13:58:09 +00:00
|
|
|
static ICODE_ATTR void copy_write_sectors(const unsigned char* buf,
|
2009-11-24 18:03:57 +00:00
|
|
|
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
|
|
|
|
{
|
2011-01-02 22:51:47 +00:00
|
|
|
#if defined(ROCKBOX_LITTLE_ENDIAN)
|
2009-11-24 18:03:57 +00:00
|
|
|
tmp = (unsigned short) *buf++;
|
|
|
|
tmp |= (unsigned short) *buf++ << 8;
|
2007-05-23 17:51:18 +00:00
|
|
|
#else
|
2009-11-24 18:03:57 +00:00
|
|
|
tmp = (unsigned short) *buf++ << 8;
|
|
|
|
tmp |= (unsigned short) *buf++;
|
2007-05-23 17:51:18 +00:00
|
|
|
#endif
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT16(ATA_DATA, tmp);
|
2009-11-24 18:03:57 +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
|
|
|
|
{
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT16(ATA_DATA, *wbuf);
|
2009-11-24 18:03:57 +00:00
|
|
|
} while (++wbuf < wbufend); /* tail loop is faster */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* !ATA_OPTIMIZED_WRITING */
|
|
|
|
|
2013-05-12 21:38:52 +00:00
|
|
|
int ata_disk_isssd(void)
|
2020-07-08 16:26:04 +00:00
|
|
|
{
|
|
|
|
/* offset 217 is "Nominal Rotation rate"
|
|
|
|
0x0000 == Not reported
|
|
|
|
0x0001 == Solid State
|
|
|
|
0x0401 -> 0xffe == RPM
|
|
|
|
All others reserved
|
|
|
|
|
|
|
|
Some CF cards return 0x0100 (ie byteswapped 0x0001) so accept either
|
|
|
|
*/
|
|
|
|
return (identify_info[217] == 0x0001 || identify_info[217] == 0x0100);
|
|
|
|
}
|
|
|
|
|
2009-11-24 18:03:57 +00:00
|
|
|
static int ata_transfer_sectors(unsigned long start,
|
|
|
|
int incount,
|
|
|
|
void* inbuf,
|
|
|
|
int write)
|
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;
|
2017-03-15 05:51:54 +00:00
|
|
|
long spinup_start = spinup_start;
|
2009-03-12 02:01:25 +00:00
|
|
|
#ifdef HAVE_ATA_DMA
|
|
|
|
bool usedma = false;
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
keep_ata_active();
|
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
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
if (ata_state < ATA_ON) {
|
|
|
|
spinup_start = current_tick;
|
|
|
|
int state = ata_state;
|
|
|
|
ata_state = ATA_SPINUP;
|
|
|
|
if (ata_perform_wakeup(state)) {
|
|
|
|
ret = -2;
|
|
|
|
goto error;
|
2002-07-16 12:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
2002-07-16 14:07:47 +00:00
|
|
|
|
2009-11-24 18:03:57 +00:00
|
|
|
timeout = current_tick + READWRITE_TIMEOUT;
|
2003-02-27 13:26:09 +00:00
|
|
|
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(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;
|
2017-03-15 05:51:54 +00:00
|
|
|
keep_ata_active();
|
2002-09-05 15:25:08 +00:00
|
|
|
|
2009-03-12 02:01:25 +00:00
|
|
|
#ifdef HAVE_ATA_DMA
|
|
|
|
/* If DMA is supported and parameters are ok for DMA, use it */
|
2009-11-24 18:03:57 +00:00
|
|
|
if (dma_mode && ata_dma_setup(inbuf, incount * SECTOR_SIZE, write))
|
2009-03-12 02:01:25 +00:00
|
|
|
usedma = true;
|
|
|
|
#endif
|
|
|
|
|
2006-12-20 22:08:29 +00:00
|
|
|
#ifdef HAVE_LBA48
|
|
|
|
if (lba48)
|
2006-12-19 22:40:23 +00:00
|
|
|
{
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_NSECTOR, count >> 8);
|
|
|
|
ATA_OUT8(ATA_NSECTOR, count & 0xff);
|
|
|
|
ATA_OUT8(ATA_SECTOR, (start >> 24) & 0xff); /* 31:24 */
|
|
|
|
ATA_OUT8(ATA_SECTOR, start & 0xff); /* 7:0 */
|
|
|
|
ATA_OUT8(ATA_LCYL, 0); /* 39:32 */
|
|
|
|
ATA_OUT8(ATA_LCYL, (start >> 8) & 0xff); /* 15:8 */
|
|
|
|
ATA_OUT8(ATA_HCYL, 0); /* 47:40 */
|
|
|
|
ATA_OUT8(ATA_HCYL, (start >> 16) & 0xff); /* 23:16 */
|
|
|
|
ATA_OUT8(ATA_SELECT, SELECT_LBA | ata_device);
|
2009-03-12 02:01:25 +00:00
|
|
|
#ifdef HAVE_ATA_DMA
|
2009-11-24 18:03:57 +00:00
|
|
|
if (write)
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_COMMAND, usedma ? CMD_WRITE_DMA_EXT : CMD_WRITE_MULTIPLE_EXT);
|
2009-11-24 18:03:57 +00:00
|
|
|
else
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_COMMAND, usedma ? CMD_READ_DMA_EXT : CMD_READ_MULTIPLE_EXT);
|
2009-03-12 02:01:25 +00:00
|
|
|
#else
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_COMMAND, write ? CMD_WRITE_MULTIPLE_EXT : CMD_READ_MULTIPLE_EXT);
|
2009-03-12 02:01:25 +00:00
|
|
|
#endif
|
2006-12-19 22:40:23 +00:00
|
|
|
}
|
2002-09-05 15:25:08 +00:00
|
|
|
else
|
2006-12-19 22:40:23 +00:00
|
|
|
#endif
|
|
|
|
{
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_NSECTOR, count & 0xff); /* 0 means 256 sectors */
|
|
|
|
ATA_OUT8(ATA_SECTOR, start & 0xff);
|
|
|
|
ATA_OUT8(ATA_LCYL, (start >> 8) & 0xff);
|
|
|
|
ATA_OUT8(ATA_HCYL, (start >> 16) & 0xff);
|
|
|
|
ATA_OUT8(ATA_SELECT, ((start >> 24) & 0xf) | SELECT_LBA | ata_device);
|
2009-03-12 02:01:25 +00:00
|
|
|
#ifdef HAVE_ATA_DMA
|
2009-11-24 18:03:57 +00:00
|
|
|
if (write)
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_COMMAND, usedma ? CMD_WRITE_DMA : CMD_WRITE_MULTIPLE);
|
2009-11-24 18:03:57 +00:00
|
|
|
else
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_COMMAND, usedma ? CMD_READ_DMA : CMD_READ_MULTIPLE);
|
2009-03-12 02:01:25 +00:00
|
|
|
#else
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_COMMAND, write ? CMD_WRITE_MULTIPLE : CMD_READ_MULTIPLE);
|
2009-03-12 02:01:25 +00:00
|
|
|
#endif
|
2006-12-19 22:40:23 +00:00
|
|
|
}
|
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
|
|
|
|
2009-03-12 02:01:25 +00:00
|
|
|
#ifdef HAVE_ATA_DMA
|
|
|
|
if (usedma) {
|
|
|
|
if (!ata_dma_finish())
|
|
|
|
ret = -7;
|
2002-09-24 14:23:18 +00:00
|
|
|
|
2009-03-12 02:01:25 +00:00
|
|
|
if (ret != 0) {
|
2005-05-10 22:39:53 +00:00
|
|
|
perform_soft_reset();
|
2002-09-25 14:10:50 +00:00
|
|
|
goto retry;
|
2002-09-24 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
if (ata_state == ATA_SPINUP) {
|
|
|
|
ata_state = ATA_ON;
|
2008-11-01 16:14:28 +00:00
|
|
|
spinup_time = current_tick - spinup_start;
|
2002-12-06 13:08:42 +00:00
|
|
|
}
|
2009-03-12 02:01:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* HAVE_ATA_DMA */
|
|
|
|
{
|
|
|
|
while (count) {
|
|
|
|
int sectors;
|
|
|
|
int wordcount;
|
|
|
|
int status;
|
2010-04-07 20:01:21 +00:00
|
|
|
int error;
|
2009-03-12 02:01:25 +00:00
|
|
|
|
|
|
|
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 = -5;
|
|
|
|
goto retry;
|
|
|
|
}
|
2002-12-06 13:08:42 +00:00
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
if (ata_state == ATA_SPINUP) {
|
|
|
|
ata_state = ATA_ON;
|
2009-03-12 02:01:25 +00:00
|
|
|
spinup_time = current_tick - spinup_start;
|
|
|
|
}
|
2003-03-14 16:06:09 +00:00
|
|
|
|
2009-03-12 02:01:25 +00:00
|
|
|
/* read the status register exactly once per loop */
|
2011-01-02 22:51:47 +00:00
|
|
|
status = ATA_IN8(ATA_STATUS);
|
|
|
|
error = ATA_IN8(ATA_ERROR);
|
2002-09-06 16:02:19 +00:00
|
|
|
|
2011-01-02 22:51:47 +00:00
|
|
|
if (count >= multisectors)
|
2009-03-12 02:01:25 +00:00
|
|
|
sectors = multisectors;
|
|
|
|
else
|
|
|
|
sectors = count;
|
2002-09-24 14:23:18 +00:00
|
|
|
|
2009-03-12 02:01:25 +00:00
|
|
|
wordcount = sectors * SECTOR_SIZE / 2;
|
2002-04-27 14:19:00 +00:00
|
|
|
|
2009-11-24 18:03:57 +00:00
|
|
|
if (write)
|
|
|
|
copy_write_sectors(buf, wordcount);
|
|
|
|
else
|
|
|
|
copy_read_sectors(buf, wordcount);
|
2005-05-10 22:39:53 +00:00
|
|
|
|
2009-03-12 02:01:25 +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) ) {
|
|
|
|
perform_soft_reset();
|
|
|
|
ret = -6;
|
2010-04-07 20:01:21 +00:00
|
|
|
/* no point retrying IDNF, sector no. was invalid */
|
|
|
|
if (error & ERROR_IDNF)
|
|
|
|
break;
|
2009-03-12 02:01:25 +00:00
|
|
|
goto retry;
|
|
|
|
}
|
2002-12-05 09:35:01 +00:00
|
|
|
|
2009-03-12 02:01:25 +00:00
|
|
|
buf += sectors * SECTOR_SIZE; /* Advance one chunk of sectors */
|
|
|
|
count -= sectors;
|
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
keep_ata_active();
|
2009-03-12 02:01:25 +00:00
|
|
|
}
|
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()) {
|
2010-04-07 20:01:21 +00:00
|
|
|
int error;
|
|
|
|
|
2011-01-02 22:51:47 +00:00
|
|
|
error = ATA_IN8(ATA_ERROR);
|
2005-05-10 22:39:53 +00:00
|
|
|
perform_soft_reset();
|
2008-03-09 00:59:48 +00:00
|
|
|
ret = -4;
|
2010-04-07 20:01:21 +00:00
|
|
|
/* no point retrying IDNF, sector no. was invalid */
|
|
|
|
if (error & ERROR_IDNF)
|
|
|
|
break;
|
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);
|
2017-03-15 05:51:54 +00:00
|
|
|
|
|
|
|
if (ret < 0 && ata_state == ATA_SPINUP) {
|
|
|
|
/* bailed out before updating */
|
|
|
|
ata_state = ATA_ON;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2009-11-24 18:03:57 +00:00
|
|
|
#ifndef MAX_PHYS_SECTOR_SIZE
|
2013-08-17 16:18:22 +00:00
|
|
|
int ata_read_sectors(IF_MD(int drive,)
|
2009-11-24 18:03:57 +00:00
|
|
|
unsigned long start,
|
|
|
|
int incount,
|
|
|
|
void* inbuf)
|
2004-04-01 05:46:31 +00:00
|
|
|
{
|
2009-11-24 18:03:57 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
|
|
|
(void)drive; /* unused for now */
|
2005-01-26 12:53:48 +00:00
|
|
|
#endif
|
2009-11-24 18:03:57 +00:00
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
|
|
|
int rc = ata_transfer_sectors(start, incount, inbuf, false);
|
|
|
|
mutex_unlock(&ata_mtx);
|
|
|
|
return rc;
|
2004-04-01 05:46:31 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 16:18:22 +00:00
|
|
|
int ata_write_sectors(IF_MD(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)
|
2002-04-21 22:06:12 +00:00
|
|
|
{
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
2007-05-23 17:51:18 +00:00
|
|
|
(void)drive; /* unused for now */
|
|
|
|
#endif
|
2002-08-15 12:42:37 +00:00
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
|
|
|
int rc = ata_transfer_sectors(start, count, (void*)buf, true);
|
|
|
|
mutex_unlock(&ata_mtx);
|
|
|
|
return rc;
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
2017-03-15 05:51:54 +00:00
|
|
|
#endif /* ndef MAX_PHYS_SECTOR_SIZE */
|
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;
|
2009-11-24 18:03:57 +00:00
|
|
|
rc = ata_transfer_sectors(sector, phys_sector_mult, sector_cache.data, false);
|
2007-05-23 17:51:18 +00:00
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
sector_cache.sectornum = sector;
|
|
|
|
sector_cache.inuse = true;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int flush_current_sector(void)
|
|
|
|
{
|
2009-11-24 18:03:57 +00:00
|
|
|
return ata_transfer_sectors(sector_cache.sectornum, phys_sector_mult,
|
|
|
|
sector_cache.data, true);
|
2007-05-23 17:51:18 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 16:18:22 +00:00
|
|
|
int ata_read_sectors(IF_MD(int drive,)
|
2007-05-23 17:51:18 +00:00
|
|
|
unsigned long start,
|
|
|
|
int incount,
|
|
|
|
void* inbuf)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
int offset;
|
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
2007-05-23 17:51:18 +00:00
|
|
|
(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)
|
|
|
|
{
|
2009-11-24 18:03:57 +00:00
|
|
|
rc = ata_transfer_sectors(start, incount, inbuf, false);
|
2007-05-23 17:51:18 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-08-17 16:18:22 +00:00
|
|
|
int ata_write_sectors(IF_MD(int drive,)
|
2007-05-23 17:51:18 +00:00
|
|
|
unsigned long start,
|
|
|
|
int count,
|
|
|
|
const void* buf)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
int offset;
|
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
2007-05-23 17:51:18 +00:00
|
|
|
(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)
|
|
|
|
{
|
2009-11-24 18:03:57 +00:00
|
|
|
rc = ata_transfer_sectors(start, count, (void*)buf, true);
|
2007-05-23 17:51:18 +00:00
|
|
|
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 */
|
|
|
|
|
2011-11-27 04:56:47 +00:00
|
|
|
static int STORAGE_INIT_ATTR check_registers(void)
|
2002-04-21 22:06:12 +00:00
|
|
|
{
|
2005-01-20 23:00:11 +00:00
|
|
|
int i;
|
2011-01-02 22:51:47 +00:00
|
|
|
wait_for_bsy();
|
|
|
|
if (ATA_IN8(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++) {
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_NSECTOR, TEST_PATTERN1);
|
|
|
|
ATA_OUT8(ATA_SECTOR, TEST_PATTERN2);
|
|
|
|
ATA_OUT8(ATA_LCYL, TEST_PATTERN3);
|
|
|
|
ATA_OUT8(ATA_HCYL, TEST_PATTERN4);
|
|
|
|
|
2011-01-03 13:09:42 +00:00
|
|
|
if (((ATA_IN8(ATA_NSECTOR) & 0xff) == TEST_PATTERN1) &&
|
|
|
|
((ATA_IN8(ATA_SECTOR) & 0xff) == TEST_PATTERN2) &&
|
|
|
|
((ATA_IN8(ATA_LCYL) & 0xff) == TEST_PATTERN3) &&
|
|
|
|
((ATA_IN8(ATA_HCYL) & 0xff) == TEST_PATTERN4))
|
2006-08-12 08:01:54 +00:00
|
|
|
return 0;
|
2011-01-02 22:51:47 +00:00
|
|
|
|
|
|
|
sleep(1);
|
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)
|
|
|
|
{
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(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
|
|
|
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(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)
|
|
|
|
{
|
2017-03-15 05:51:54 +00:00
|
|
|
return ata_state >= ATA_SPINUP;
|
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)
|
|
|
|
{
|
2020-07-08 15:05:27 +00:00
|
|
|
/* Don't enter sleep if the device doesn't support
|
|
|
|
power management. */
|
|
|
|
if (!(identify_info[82] & (1 << 3)))
|
|
|
|
return;
|
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
if (ata_state >= ATA_SPINUP) {
|
2010-05-21 15:33:31 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
2017-03-15 05:51:54 +00:00
|
|
|
if (ata_state == ATA_ON) {
|
|
|
|
if (!ata_perform_sleep()) {
|
|
|
|
ata_state = ATA_SLEEPING;
|
|
|
|
schedule_ata_power_off();
|
|
|
|
}
|
|
|
|
}
|
2010-05-21 15:33:31 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
2006-11-29 12:17:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-26 13:21:14 +00:00
|
|
|
void ata_spin(void)
|
|
|
|
{
|
2017-03-15 05:51:54 +00:00
|
|
|
keep_ata_active();
|
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 */
|
2011-11-27 17:40:57 +00:00
|
|
|
#ifdef HAVE_ATA_POWER_OFF
|
2008-11-04 19:37:28 +00:00
|
|
|
static int ata_hard_reset(void)
|
2011-11-27 17:40:57 +00:00
|
|
|
#else
|
|
|
|
static int STORAGE_INIT_ATTR ata_hard_reset(void)
|
|
|
|
#endif
|
2002-04-21 22:06:12 +00:00
|
|
|
{
|
2002-05-16 21:28:21 +00:00
|
|
|
int ret;
|
2004-09-17 11:28:07 +00:00
|
|
|
|
2008-05-06 10:12:05 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
|
|
|
|
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 */
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(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;
|
|
|
|
|
2008-05-06 10:12:05 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
|
|
|
|
2002-05-16 21:28:21 +00:00
|
|
|
return ret;
|
2002-04-21 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2012-04-17 16:35:50 +00:00
|
|
|
// not putting this into STORAGE_INIT_ATTR, as ATA spec recommends to
|
|
|
|
// re-read identify_info after soft reset. So we'll do that.
|
|
|
|
static int identify(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ATA_OUT8(ATA_SELECT, ata_device);
|
|
|
|
|
|
|
|
if(!wait_for_rdy()) {
|
|
|
|
DEBUGF("identify() - not RDY\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ATA_OUT8(ATA_COMMAND, CMD_IDENTIFY);
|
|
|
|
|
|
|
|
if (!wait_for_start_of_transfer())
|
|
|
|
{
|
|
|
|
DEBUGF("identify() - CMD failed\n");
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
identify_info[i] = ATA_SWAP_IDENTIFY(ATA_IN16(ATA_DATA));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_SELECT, SELECT_LBA | ata_device );
|
|
|
|
ATA_OUT8(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
|
|
|
|
2009-03-12 02:01:25 +00:00
|
|
|
#ifdef HAVE_ATA_DMA
|
|
|
|
/* DMA requires INTRQ be enabled */
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_CONTROL, 0);
|
2009-03-12 02:01:25 +00:00
|
|
|
#else
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_CONTROL, CONTROL_nIEN);
|
2009-03-12 02:01:25 +00:00
|
|
|
#endif
|
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
|
|
|
|
2012-04-17 16:35:50 +00:00
|
|
|
if (identify())
|
|
|
|
return -5;
|
|
|
|
|
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)
|
|
|
|
{
|
2017-03-15 05:51:54 +00:00
|
|
|
int ret = -6;
|
2002-08-27 21:06:48 +00:00
|
|
|
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_lock(&ata_mtx);
|
2002-08-27 21:06:48 +00:00
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
if (ata_state > ATA_OFF) {
|
|
|
|
ret = perform_soft_reset();
|
|
|
|
}
|
2002-08-27 21:06:48 +00:00
|
|
|
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_unlock(&ata_mtx);
|
2002-08-27 21:06:48 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-11-27 17:40:57 +00:00
|
|
|
#ifdef HAVE_ATA_POWER_OFF
|
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);
|
2008-05-08 21:36:50 +00:00
|
|
|
sleep(HZ/4); /* allow voltage to build up */
|
|
|
|
|
|
|
|
/* Accessing the PP IDE controller too early after powering up the disk
|
|
|
|
* makes the core hang for a short time, causing an audio dropout. This
|
|
|
|
* also depends on the disk; iPod Mini G2 needs at least HZ/5 to get rid
|
|
|
|
* of the dropout. Since this time isn't additive (the wait_for_bsy() in
|
|
|
|
* ata_hard_reset() will shortened by the same amount), it's a good idea
|
|
|
|
* to do this on all HDD based targets. */
|
|
|
|
|
2002-12-02 10:30:40 +00:00
|
|
|
if( ata_hard_reset() )
|
|
|
|
return -1;
|
2002-08-02 06:01:22 +00:00
|
|
|
|
2012-04-17 16:35:50 +00:00
|
|
|
if (identify())
|
|
|
|
return -5;
|
|
|
|
|
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
|
|
|
}
|
2017-03-15 05:51:54 +00:00
|
|
|
#endif /* HAVE_ATA_POWER_OFF */
|
2002-08-02 06:01:22 +00:00
|
|
|
|
2011-11-27 04:56:47 +00:00
|
|
|
static int STORAGE_INIT_ATTR master_slave_detect(void)
|
2002-05-22 14:37:36 +00:00
|
|
|
{
|
|
|
|
/* master? */
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_SELECT, 0);
|
|
|
|
if (ATA_IN8(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? */
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_SELECT, SELECT_DEVICE1);
|
|
|
|
if (ATA_IN8(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-06 17:20:44 +00:00
|
|
|
static int set_multiple_mode(int sectors)
|
|
|
|
{
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(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;
|
|
|
|
}
|
|
|
|
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_NSECTOR, sectors);
|
|
|
|
ATA_OUT8(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;
|
|
|
|
}
|
|
|
|
|
2009-03-12 02:01:25 +00:00
|
|
|
#ifdef HAVE_ATA_DMA
|
|
|
|
static int get_best_mode(unsigned short identword, int max, int modetype)
|
|
|
|
{
|
2009-06-07 21:27:05 +00:00
|
|
|
unsigned short testbit = BIT_N(max);
|
2009-03-12 02:01:25 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (identword & testbit)
|
|
|
|
return max | modetype;
|
|
|
|
testbit >>= 1;
|
|
|
|
if (!testbit)
|
|
|
|
return 0;
|
|
|
|
max--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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 */
|
2009-03-12 02:01:25 +00:00
|
|
|
#ifdef HAVE_ATA_DMA
|
|
|
|
{ 0, 0, 0x03, 0 }, /* DMA mode */
|
|
|
|
#endif
|
2004-01-14 13:15:19 +00:00
|
|
|
};
|
|
|
|
int i;
|
2004-03-02 09:55:23 +00:00
|
|
|
int pio_mode = 2;
|
|
|
|
|
|
|
|
/* Find out the highest supported PIO mode */
|
2020-07-08 16:03:40 +00:00
|
|
|
if (identify_info[53] & (1<<1)) { /* Is word 64 valid? */
|
|
|
|
if (identify_info[64] & 2)
|
2004-03-02 09:55:23 +00:00
|
|
|
pio_mode = 4;
|
2020-07-08 16:03:40 +00:00
|
|
|
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;
|
2020-07-08 16:03:40 +00:00
|
|
|
|
2009-03-12 02:01:25 +00:00
|
|
|
#ifdef HAVE_ATA_DMA
|
|
|
|
if (identify_info[53] & (1<<2))
|
|
|
|
/* Ultra DMA mode info present, find a mode */
|
|
|
|
dma_mode = get_best_mode(identify_info[88], ATA_MAX_UDMA, 0x40);
|
|
|
|
|
|
|
|
if (!dma_mode) {
|
|
|
|
/* No UDMA mode found, try to find a multi-word DMA mode */
|
|
|
|
dma_mode = get_best_mode(identify_info[63], ATA_MAX_MWDMA, 0x20);
|
|
|
|
features[4].id_word = 63;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
features[4].id_word = 88;
|
|
|
|
|
|
|
|
features[4].id_bit = dma_mode & 7;
|
|
|
|
features[4].parameter = dma_mode;
|
|
|
|
#endif /* HAVE_ATA_DMA */
|
2008-03-09 13:24:42 +00:00
|
|
|
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(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++) {
|
2009-06-07 21:27:05 +00:00
|
|
|
if (identify_info[features[i].id_word] & BIT_N(features[i].id_bit)) {
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_FEATURE, features[i].subcommand);
|
|
|
|
ATA_OUT8(ATA_NSECTOR, features[i].parameter);
|
|
|
|
ATA_OUT8(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;
|
|
|
|
}
|
|
|
|
|
2011-01-02 22:51:47 +00:00
|
|
|
if((ATA_IN8(ATA_ALT_STATUS) & STATUS_ERR) && (i != 1)) {
|
2008-03-09 13:24:42 +00:00
|
|
|
/* some CF cards don't like advanced powermanagement
|
|
|
|
even if they mark it as supported - go figure... */
|
2011-01-02 22:51:47 +00:00
|
|
|
if(ATA_IN8(ATA_ERROR) & ERROR_ABRT) {
|
2004-03-02 09:55:23 +00:00
|
|
|
return -20 - i;
|
|
|
|
}
|
2004-01-14 13:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-08 21:23:53 +00:00
|
|
|
#ifdef ATA_SET_PIO_TIMING
|
2008-05-05 13:00:08 +00:00
|
|
|
ata_set_pio_timings(pio_mode);
|
|
|
|
#endif
|
|
|
|
|
2009-03-12 02:01:25 +00:00
|
|
|
#ifdef HAVE_ATA_DMA
|
|
|
|
ata_dma_set_mode(dma_mode);
|
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-27 04:56:47 +00:00
|
|
|
static int STORAGE_INIT_ATTR init_and_check(bool hard_reset)
|
2004-11-23 22:00:41 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-27 04:56:47 +00:00
|
|
|
int STORAGE_INIT_ATTR ata_init(void)
|
2002-04-21 22:06:12 +00:00
|
|
|
{
|
2008-01-18 12:32:03 +00:00
|
|
|
int rc = 0;
|
|
|
|
bool coldstart;
|
2003-07-09 07:18:47 +00:00
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
if (ata_state == ATA_BOOT) {
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_init(&ata_mtx);
|
2008-01-18 12:32:03 +00:00
|
|
|
}
|
|
|
|
|
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();
|
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
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
if (ata_state == ATA_BOOT) {
|
|
|
|
ata_state = ATA_OFF;
|
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);
|
2008-05-08 21:36:50 +00:00
|
|
|
sleep(HZ/4); /* allow voltage to build up */
|
2003-12-03 01:03:54 +00:00
|
|
|
}
|
|
|
|
|
2009-03-12 02:01:25 +00:00
|
|
|
#ifdef HAVE_ATA_DMA
|
|
|
|
/* DMA requires INTRQ be enabled */
|
2011-01-02 22:51:47 +00:00
|
|
|
ATA_OUT8(ATA_CONTROL, 0);
|
2009-03-12 02:01:25 +00:00
|
|
|
#endif
|
|
|
|
|
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 */
|
2011-01-02 23:02:55 +00:00
|
|
|
DEBUGF("ata: init failed, retrying...\n");
|
|
|
|
rc = init_and_check(true);
|
2017-03-15 05:51:54 +00:00
|
|
|
if (rc) {
|
|
|
|
goto error;
|
|
|
|
}
|
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
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
if (rc) {
|
|
|
|
rc = -40 + rc;
|
|
|
|
goto error;
|
|
|
|
}
|
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
|
|
|
|
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
|
|
|
}
|
2017-03-15 05:51:54 +00:00
|
|
|
#endif /* HAVE_LBA48 */
|
|
|
|
|
2004-09-17 11:28:07 +00:00
|
|
|
rc = freeze_lock();
|
2006-08-12 08:01:54 +00:00
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
if (rc) {
|
|
|
|
rc = -50 + rc;
|
|
|
|
goto error;
|
|
|
|
}
|
2004-09-17 11:28:07 +00:00
|
|
|
|
2004-01-14 13:15:19 +00:00
|
|
|
rc = set_features();
|
2017-03-15 05:51:54 +00:00
|
|
|
if (rc) {
|
|
|
|
rc = -60 + rc;
|
|
|
|
goto error;
|
|
|
|
}
|
2004-01-14 13:15:19 +00:00
|
|
|
|
2010-04-14 14:40:03 +00:00
|
|
|
#ifdef MAX_PHYS_SECTOR_SIZE
|
|
|
|
/* Find out the physical sector size */
|
|
|
|
if((identify_info[106] & 0xe000) == 0x6000)
|
|
|
|
phys_sector_mult = BIT_N(identify_info[106] & 0x000f);
|
|
|
|
else
|
|
|
|
phys_sector_mult = 1;
|
|
|
|
|
|
|
|
DEBUGF("ata: %d logical sectors per phys sector", phys_sector_mult);
|
|
|
|
|
|
|
|
if (phys_sector_mult > 1)
|
|
|
|
{
|
|
|
|
/* Check if drive really needs emulation - if we can access
|
|
|
|
* sector 1 then assume the drive will handle it better than
|
|
|
|
* us, and ignore the large physical sectors.
|
|
|
|
*/
|
|
|
|
char throwaway[SECTOR_SIZE];
|
|
|
|
rc = ata_transfer_sectors(1, 1, &throwaway, false);
|
|
|
|
if (rc == 0)
|
|
|
|
phys_sector_mult = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (phys_sector_mult > (MAX_PHYS_SECTOR_SIZE/SECTOR_SIZE))
|
|
|
|
panicf("Unsupported physical sector size: %d",
|
|
|
|
phys_sector_mult * SECTOR_SIZE);
|
2017-03-15 05:51:54 +00:00
|
|
|
#endif /* MAX_PHYS_SECTOR_SIZE */
|
2005-01-20 23:00:11 +00:00
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
ata_state = ATA_ON;
|
|
|
|
keep_ata_active();
|
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
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
error:
|
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
|
2008-11-01 16:14:28 +00:00
|
|
|
|
|
|
|
long ata_last_disk_activity(void)
|
|
|
|
{
|
|
|
|
return last_disk_activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ata_spinup_time(void)
|
|
|
|
{
|
|
|
|
return spinup_time;
|
|
|
|
}
|
|
|
|
|
2008-11-01 17:33:21 +00:00
|
|
|
#ifdef STORAGE_GET_INFO
|
2013-08-17 16:18:22 +00:00
|
|
|
void ata_get_info(IF_MD(int drive,)struct storage_info *info)
|
2008-11-01 16:14:28 +00:00
|
|
|
{
|
|
|
|
unsigned short *src,*dest;
|
|
|
|
static char vendor[8];
|
|
|
|
static char product[16];
|
|
|
|
static char revision[4];
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
|
|
|
(void)drive; /* unused for now */
|
|
|
|
#endif
|
2008-11-01 16:14:28 +00:00
|
|
|
int i;
|
|
|
|
info->sector_size = SECTOR_SIZE;
|
2011-01-04 17:17:38 +00:00
|
|
|
info->num_sectors = total_sectors;
|
2008-11-01 16:14:28 +00:00
|
|
|
|
|
|
|
src = (unsigned short*)&identify_info[27];
|
|
|
|
dest = (unsigned short*)vendor;
|
|
|
|
for (i=0;i<4;i++)
|
|
|
|
dest[i] = htobe16(src[i]);
|
|
|
|
info->vendor=vendor;
|
|
|
|
|
|
|
|
src = (unsigned short*)&identify_info[31];
|
|
|
|
dest = (unsigned short*)product;
|
|
|
|
for (i=0;i<8;i++)
|
|
|
|
dest[i] = htobe16(src[i]);
|
|
|
|
info->product=product;
|
|
|
|
|
|
|
|
src = (unsigned short*)&identify_info[23];
|
|
|
|
dest = (unsigned short*)revision;
|
|
|
|
for (i=0;i<2;i++)
|
|
|
|
dest[i] = htobe16(src[i]);
|
|
|
|
info->revision=revision;
|
|
|
|
}
|
2008-11-01 17:33:21 +00:00
|
|
|
#endif
|
2009-03-12 02:01:25 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_ATA_DMA
|
|
|
|
/* Returns last DMA mode as set by set_features() */
|
|
|
|
int ata_get_dma_mode(void)
|
|
|
|
{
|
|
|
|
return dma_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Needed to allow updating while waiting for DMA to complete */
|
|
|
|
void ata_keep_active(void)
|
2017-03-15 05:51:54 +00:00
|
|
|
__attribute__((alias("ata_spin")));
|
2009-03-12 02:01:25 +00:00
|
|
|
#endif
|
2009-07-17 22:28:49 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_STORAGE_MULTI
|
|
|
|
int ata_num_drives(int first_drive)
|
|
|
|
{
|
|
|
|
/* We don't care which logical drive number(s) we have been assigned */
|
|
|
|
(void)first_drive;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
2017-03-15 05:51:54 +00:00
|
|
|
|
|
|
|
int ata_event(long id, intptr_t data)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
/* GCC does a lousy job culling unreachable cases in the default handler
|
|
|
|
if statements are in a switch statement, so we'll do it this way. Only
|
|
|
|
the first case is frequently hit anyway. */
|
|
|
|
if (LIKELY(id == Q_STORAGE_TICK)) {
|
|
|
|
/* won't see ATA_BOOT in here */
|
|
|
|
int state = ata_state;
|
|
|
|
if (state != ATA_ON || !ata_sleep_timed_out()) {
|
|
|
|
if (state == ATA_SLEEPING && ata_power_off_timed_out()) {
|
|
|
|
mutex_lock(&ata_mtx);
|
|
|
|
if (ata_state == ATA_SLEEPING) {
|
|
|
|
ide_power_enable(false);
|
|
|
|
ata_state = ATA_OFF;
|
|
|
|
}
|
|
|
|
mutex_unlock(&ata_mtx);
|
|
|
|
}
|
|
|
|
STG_EVENT_ASSERT_ACTIVE(STORAGE_ATA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (id == Q_STORAGE_SLEEPNOW) {
|
|
|
|
ata_sleepnow();
|
|
|
|
}
|
|
|
|
else if (id == Q_STORAGE_SLEEP) {
|
|
|
|
schedule_ata_sleep(HZ/5);
|
|
|
|
}
|
|
|
|
#ifndef USB_NONE
|
|
|
|
else if (id == SYS_USB_CONNECTED) {
|
|
|
|
if (ATA_ACTIVE_IN_USB) {
|
|
|
|
/* There is no need to force ATA power on */
|
|
|
|
STG_EVENT_ASSERT_ACTIVE(STORAGE_ATA);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mutex_lock(&ata_mtx);
|
|
|
|
if (ata_state < ATA_ON) {
|
|
|
|
ata_led(true);
|
|
|
|
if (!(rc = ata_perform_wakeup(ata_state))) {
|
|
|
|
ata_state = ATA_ON;
|
|
|
|
}
|
|
|
|
ata_led(false);
|
|
|
|
}
|
|
|
|
mutex_unlock(&ata_mtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* ndef USB_NONE */
|
|
|
|
else {
|
|
|
|
rc = storage_event_default_handler(id, data, last_disk_activity,
|
|
|
|
STORAGE_ATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|