Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2002 by Alan Korr |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
Linus Nielsen Feltzing | e14e13d | 2002-05-13 19:22:38 +0000 | [diff] [blame] | 21 | #include <stdbool.h> |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 22 | #include "ata.h" |
| 23 | #include "kernel.h" |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 24 | #include "thread.h" |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 25 | #include "led.h" |
Linus Nielsen Feltzing | d681087 | 2004-10-07 07:09:49 +0000 | [diff] [blame] | 26 | #include "cpu.h" |
Björn Stenberg | 2cf3b5d | 2002-04-27 20:14:01 +0000 | [diff] [blame] | 27 | #include "system.h" |
Linus Nielsen Feltzing | dc4977d | 2002-05-07 22:59:03 +0000 | [diff] [blame] | 28 | #include "debug.h" |
Linus Nielsen Feltzing | a422b83 | 2002-06-18 12:53:02 +0000 | [diff] [blame] | 29 | #include "panic.h" |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 30 | #include "usb.h" |
Linus Nielsen Feltzing | 9d58940 | 2002-08-01 12:00:03 +0000 | [diff] [blame] | 31 | #include "power.h" |
Björn Stenberg | 6ee90d9 | 2002-08-15 12:54:52 +0000 | [diff] [blame] | 32 | #include "string.h" |
Jonathan Gordon | 0b22795 | 2006-11-06 14:24:18 +0000 | [diff] [blame] | 33 | #include "ata_idle_notify.h" |
Linus Nielsen Feltzing | 838a702 | 2006-03-07 13:25:19 +0000 | [diff] [blame] | 34 | #include "ata-target.h" |
Linus Nielsen Feltzing | 838a702 | 2006-03-07 13:25:19 +0000 | [diff] [blame] | 35 | |
Jean-Philippe Bernardy | 376057d | 2005-02-22 09:55:40 +0000 | [diff] [blame] | 36 | #define SECTOR_SIZE (512) |
| 37 | |
Björn Stenberg | 65bf851 | 2004-09-17 11:28:07 +0000 | [diff] [blame] | 38 | #define ATA_FEATURE ATA_ERROR |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 39 | |
Björn Stenberg | 65bf851 | 2004-09-17 11:28:07 +0000 | [diff] [blame] | 40 | #define ATA_STATUS ATA_COMMAND |
Björn Stenberg | 6c9e578 | 2002-04-27 14:19:00 +0000 | [diff] [blame] | 41 | #define ATA_ALT_STATUS ATA_CONTROL |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 42 | |
Björn Stenberg | 5c7847c | 2002-05-22 14:37:36 +0000 | [diff] [blame] | 43 | #define SELECT_DEVICE1 0x10 |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 44 | #define SELECT_LBA 0x40 |
| 45 | |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 46 | #define CONTROL_nIEN 0x02 |
| 47 | #define CONTROL_SRST 0x04 |
| 48 | |
| 49 | #define CMD_READ_SECTORS 0x20 |
| 50 | #define CMD_WRITE_SECTORS 0x30 |
Jörg Hohensohn | f5bb766 | 2006-12-19 22:40:23 +0000 | [diff] [blame] | 51 | #define CMD_WRITE_SECTORS_EXT 0x34 |
Björn Stenberg | 8ccbc76 | 2002-09-05 15:25:08 +0000 | [diff] [blame] | 52 | #define CMD_READ_MULTIPLE 0xC4 |
Jörg Hohensohn | f5bb766 | 2006-12-19 22:40:23 +0000 | [diff] [blame] | 53 | #define CMD_READ_MULTIPLE_EXT 0x29 |
Björn Stenberg | 8ccbc76 | 2002-09-05 15:25:08 +0000 | [diff] [blame] | 54 | #define CMD_WRITE_MULTIPLE 0xC5 |
| 55 | #define CMD_SET_MULTIPLE_MODE 0xC6 |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 56 | #define CMD_STANDBY_IMMEDIATE 0xE0 |
| 57 | #define CMD_STANDBY 0xE2 |
Björn Stenberg | 8ccbc76 | 2002-09-05 15:25:08 +0000 | [diff] [blame] | 58 | #define CMD_IDENTIFY 0xEC |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 59 | #define CMD_SLEEP 0xE6 |
Björn Stenberg | a53afc0 | 2004-01-14 13:15:19 +0000 | [diff] [blame] | 60 | #define CMD_SET_FEATURES 0xEF |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 61 | #define CMD_SECURITY_FREEZE_LOCK 0xF5 |
| 62 | |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 63 | #define Q_SLEEP 0 |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 64 | #define Q_CLOSE 1 |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 65 | |
Björn Stenberg | 70747f9 | 2002-09-24 14:23:18 +0000 | [diff] [blame] | 66 | #define READ_TIMEOUT 5*HZ |
| 67 | |
Nils Wallménius | 1b85f60 | 2007-01-23 15:43:37 +0000 | [diff] [blame] | 68 | #ifdef HAVE_ATA_POWER_OFF |
| 69 | #define ATA_POWER_OFF_TIMEOUT 2*HZ |
| 70 | #endif |
| 71 | |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 72 | #ifdef ATA_DRIVER_CLOSE |
| 73 | static struct thread_entry *ata_thread_p = NULL; |
| 74 | #endif |
| 75 | |
Michael Sevakis | 606d9d0 | 2008-06-03 04:23:09 +0000 | [diff] [blame] | 76 | #if defined(MAX_PHYS_SECTOR_SIZE) && MEM == 64 |
| 77 | /* Hack - what's the deal with 5g? */ |
| 78 | struct ata_lock |
| 79 | { |
| 80 | struct thread_entry *thread; |
| 81 | int count; |
| 82 | volatile unsigned char locked; |
| 83 | IF_COP( struct corelock cl; ) |
| 84 | }; |
| 85 | |
| 86 | static void ata_lock_init(struct ata_lock *l) |
| 87 | { |
| 88 | corelock_init(&l->cl); |
| 89 | l->locked = 0; |
| 90 | l->count = 0; |
| 91 | l->thread = NULL; |
| 92 | } |
| 93 | |
| 94 | static void ata_lock_lock(struct ata_lock *l) |
| 95 | { |
| 96 | struct thread_entry * const current = thread_get_current(); |
| 97 | |
| 98 | if (current == l->thread) |
| 99 | { |
| 100 | l->count++; |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | corelock_lock(&l->cl); |
| 105 | |
| 106 | IF_PRIO( current->skip_count = -1; ) |
| 107 | |
| 108 | while (l->locked != 0) |
| 109 | { |
| 110 | corelock_unlock(&l->cl); |
| 111 | switch_thread(); |
| 112 | corelock_lock(&l->cl); |
| 113 | } |
| 114 | |
| 115 | l->locked = 1; |
| 116 | l->thread = current; |
| 117 | corelock_unlock(&l->cl); |
| 118 | } |
| 119 | |
| 120 | static void ata_lock_unlock(struct ata_lock *l) |
| 121 | { |
| 122 | if (l->count > 0) |
| 123 | { |
| 124 | l->count--; |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | corelock_lock(&l->cl); |
| 129 | |
| 130 | IF_PRIO( l->thread->skip_count = 0; ) |
| 131 | |
| 132 | l->thread = NULL; |
| 133 | l->locked = 0; |
| 134 | |
| 135 | corelock_unlock(&l->cl); |
| 136 | } |
| 137 | |
| 138 | #define mutex ata_lock |
| 139 | #define mutex_init ata_lock_init |
| 140 | #define mutex_lock ata_lock_lock |
| 141 | #define mutex_unlock ata_lock_unlock |
| 142 | #endif /* MAX_PHYS_SECTOR_SIZE */ |
| 143 | |
Michael Sevakis | 0509914 | 2008-04-06 04:34:57 +0000 | [diff] [blame] | 144 | static struct mutex ata_mtx SHAREDBSS_ATTR; |
Jens Arnold | 31ffd7b | 2006-12-03 22:13:44 +0000 | [diff] [blame] | 145 | int ata_device; /* device 0 (master) or 1 (slave) */ |
Linus Nielsen Feltzing | 8169c8f | 2002-06-12 13:51:31 +0000 | [diff] [blame] | 146 | |
Björn Stenberg | b070dd5 | 2002-12-04 14:58:48 +0000 | [diff] [blame] | 147 | int ata_spinup_time = 0; |
Jonathan Gordon | bd47d48 | 2007-02-18 05:07:19 +0000 | [diff] [blame] | 148 | #if (CONFIG_LED == LED_REAL) |
Linus Nielsen Feltzing | 68482bb | 2005-04-04 09:12:12 +0000 | [diff] [blame] | 149 | static bool ata_led_enabled = true; |
| 150 | static bool ata_led_on = false; |
Jens Arnold | 5690f78 | 2005-06-04 23:15:52 +0000 | [diff] [blame] | 151 | #endif |
Björn Stenberg | efd9077 | 2002-12-06 15:17:30 +0000 | [diff] [blame] | 152 | static bool spinup = false; |
Jörg Hohensohn | f4b677e | 2003-07-18 21:55:06 +0000 | [diff] [blame] | 153 | static bool sleeping = true; |
Björn Stenberg | 494d261 | 2002-11-27 15:55:47 +0000 | [diff] [blame] | 154 | static bool poweroff = false; |
Jens Arnold | 31ffd7b | 2006-12-03 22:13:44 +0000 | [diff] [blame] | 155 | static long sleep_timeout = 5*HZ; |
Jörg Hohensohn | bb03586 | 2006-12-20 22:08:29 +0000 | [diff] [blame] | 156 | #ifdef HAVE_LBA48 |
| 157 | static bool lba48 = false; /* set for 48 bit addressing */ |
Jörg Hohensohn | f5bb766 | 2006-12-19 22:40:23 +0000 | [diff] [blame] | 158 | #endif |
Jonathan Gordon | b2e5090 | 2007-01-23 13:40:44 +0000 | [diff] [blame] | 159 | static long ata_stack[(DEFAULT_STACK_SIZE*3)/sizeof(long)]; |
Jens Arnold | 2b0694c | 2004-08-03 05:58:46 +0000 | [diff] [blame] | 160 | static const char ata_thread_name[] = "ata"; |
Michael Sevakis | 84f5c5c | 2007-10-16 22:00:51 +0000 | [diff] [blame] | 161 | static struct event_queue ata_queue; |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 162 | static bool initialized = false; |
Linus Nielsen Feltzing | 1a5032e | 2002-06-26 12:36:29 +0000 | [diff] [blame] | 163 | |
Björn Stenberg | 457b8a0 | 2002-08-26 13:21:14 +0000 | [diff] [blame] | 164 | static long last_user_activity = -1; |
Linus Nielsen Feltzing | 040e80c | 2002-09-23 11:39:21 +0000 | [diff] [blame] | 165 | long last_disk_activity = -1; |
Björn Stenberg | 457b8a0 | 2002-08-26 13:21:14 +0000 | [diff] [blame] | 166 | |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 167 | static unsigned long total_sectors; |
Björn Stenberg | 8ccbc76 | 2002-09-05 15:25:08 +0000 | [diff] [blame] | 168 | static int multisectors; /* number of supported multisectors */ |
Jens Arnold | 3fd2e52 | 2008-02-14 06:52:59 +0000 | [diff] [blame] | 169 | static unsigned short identify_info[SECTOR_SIZE/2]; |
Björn Stenberg | 8ccbc76 | 2002-09-05 15:25:08 +0000 | [diff] [blame] | 170 | |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 171 | #ifdef MAX_PHYS_SECTOR_SIZE |
Michael Sevakis | 766587e | 2008-02-22 17:29:37 +0000 | [diff] [blame] | 172 | |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 173 | struct sector_cache_entry { |
| 174 | bool inuse; |
| 175 | unsigned long sectornum; /* logical sector */ |
| 176 | unsigned char data[MAX_PHYS_SECTOR_SIZE]; |
| 177 | }; |
| 178 | /* buffer for reading and writing large physical sectors */ |
| 179 | #define NUMCACHES 2 |
| 180 | static struct sector_cache_entry sector_cache; |
| 181 | static int phys_sector_mult = 1; |
| 182 | #endif |
| 183 | |
Linus Nielsen Feltzing | 513e851 | 2002-08-02 06:01:22 +0000 | [diff] [blame] | 184 | static int ata_power_on(void); |
Linus Nielsen Feltzing | 22e09a3 | 2002-08-27 21:06:48 +0000 | [diff] [blame] | 185 | static int perform_soft_reset(void); |
Björn Stenberg | 7249c88 | 2002-12-02 10:30:40 +0000 | [diff] [blame] | 186 | static int set_multiple_mode(int sectors); |
Linus Nielsen Feltzing | 97955a7 | 2004-02-17 01:31:50 +0000 | [diff] [blame] | 187 | static int set_features(void); |
Linus Nielsen Feltzing | 22e09a3 | 2002-08-27 21:06:48 +0000 | [diff] [blame] | 188 | |
Jens Arnold | f18e4db | 2008-05-08 05:39:29 +0000 | [diff] [blame] | 189 | STATICIRAM ICODE_ATTR int wait_for_bsy(void) |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 190 | { |
Jean-Philippe Bernardy | fc19445 | 2005-02-25 18:50:16 +0000 | [diff] [blame] | 191 | long timeout = current_tick + HZ*30; |
Jens Arnold | f18e4db | 2008-05-08 05:39:29 +0000 | [diff] [blame] | 192 | |
| 193 | do |
| 194 | { |
| 195 | if (!(ATA_STATUS & STATUS_BSY)) |
| 196 | return 1; |
Björn Stenberg | c695f26 | 2003-07-10 13:32:15 +0000 | [diff] [blame] | 197 | last_disk_activity = current_tick; |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 198 | yield(); |
Jens Arnold | f18e4db | 2008-05-08 05:39:29 +0000 | [diff] [blame] | 199 | } while (TIME_BEFORE(current_tick, timeout)); |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 200 | |
Jens Arnold | f18e4db | 2008-05-08 05:39:29 +0000 | [diff] [blame] | 201 | return 0; /* timeout */ |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Jens Arnold | f18e4db | 2008-05-08 05:39:29 +0000 | [diff] [blame] | 204 | STATICIRAM ICODE_ATTR int wait_for_rdy(void) |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 205 | { |
Jean-Philippe Bernardy | fc19445 | 2005-02-25 18:50:16 +0000 | [diff] [blame] | 206 | long timeout; |
Jean-Philippe Bernardy | 99e72c8 | 2005-01-20 23:00:11 +0000 | [diff] [blame] | 207 | |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 208 | if (!wait_for_bsy()) |
| 209 | return 0; |
Linus Nielsen Feltzing | 5d154f9 | 2002-08-29 11:50:57 +0000 | [diff] [blame] | 210 | |
Björn Stenberg | e65b65e | 2002-09-04 20:15:45 +0000 | [diff] [blame] | 211 | timeout = current_tick + HZ*10; |
Jens Arnold | f18e4db | 2008-05-08 05:39:29 +0000 | [diff] [blame] | 212 | |
| 213 | do |
| 214 | { |
| 215 | if (ATA_ALT_STATUS & STATUS_RDY) |
| 216 | return 1; |
Björn Stenberg | c695f26 | 2003-07-10 13:32:15 +0000 | [diff] [blame] | 217 | last_disk_activity = current_tick; |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 218 | yield(); |
Jens Arnold | f18e4db | 2008-05-08 05:39:29 +0000 | [diff] [blame] | 219 | } while (TIME_BEFORE(current_tick, timeout)); |
Linus Nielsen Feltzing | 5d154f9 | 2002-08-29 11:50:57 +0000 | [diff] [blame] | 220 | |
Jens Arnold | f18e4db | 2008-05-08 05:39:29 +0000 | [diff] [blame] | 221 | return 0; /* timeout */ |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Jens Arnold | f18e4db | 2008-05-08 05:39:29 +0000 | [diff] [blame] | 224 | STATICIRAM ICODE_ATTR int wait_for_start_of_transfer(void) |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 225 | { |
| 226 | if (!wait_for_bsy()) |
| 227 | return 0; |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 228 | |
Björn Stenberg | 6c9e578 | 2002-04-27 14:19:00 +0000 | [diff] [blame] | 229 | return (ATA_ALT_STATUS & (STATUS_BSY|STATUS_DRQ)) == STATUS_DRQ; |
| 230 | } |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 231 | |
Jens Arnold | f18e4db | 2008-05-08 05:39:29 +0000 | [diff] [blame] | 232 | STATICIRAM ICODE_ATTR int wait_for_end_of_transfer(void) |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 233 | { |
| 234 | if (!wait_for_bsy()) |
| 235 | return 0; |
Björn Stenberg | 6c9e578 | 2002-04-27 14:19:00 +0000 | [diff] [blame] | 236 | return (ATA_ALT_STATUS & (STATUS_RDY|STATUS_DRQ)) == STATUS_RDY; |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Jonathan Gordon | bd47d48 | 2007-02-18 05:07:19 +0000 | [diff] [blame] | 239 | #if (CONFIG_LED == LED_REAL) |
Jens Arnold | 31ffd7b | 2006-12-03 22:13:44 +0000 | [diff] [blame] | 240 | /* Conditionally block LED access for the ATA driver, so the LED can be |
| 241 | * (mis)used for other purposes */ |
| 242 | static void ata_led(bool on) |
| 243 | { |
| 244 | ata_led_on = on; |
| 245 | if (ata_led_enabled) |
| 246 | led(ata_led_on); |
| 247 | } |
| 248 | #else |
| 249 | #define ata_led(on) led(on) |
| 250 | #endif |
| 251 | |
| 252 | #ifndef ATA_OPTIMIZED_READING |
Jens Arnold | f18e4db | 2008-05-08 05:39:29 +0000 | [diff] [blame] | 253 | STATICIRAM ICODE_ATTR void copy_read_sectors(unsigned char* buf, int wordcount) |
Jörg Hohensohn | 88faf38 | 2004-01-16 09:02:21 +0000 | [diff] [blame] | 254 | { |
Jörg Hohensohn | 5fb1e10 | 2004-03-10 14:15:14 +0000 | [diff] [blame] | 255 | unsigned short tmp = 0; |
Jörg Hohensohn | 88faf38 | 2004-01-16 09:02:21 +0000 | [diff] [blame] | 256 | |
Jean-Philippe Bernardy | 34d2a71 | 2005-01-24 14:26:24 +0000 | [diff] [blame] | 257 | if ( (unsigned long)buf & 1) |
Jörg Hohensohn | cb570b9 | 2004-01-27 09:12:51 +0000 | [diff] [blame] | 258 | { /* not 16-bit aligned, copy byte by byte */ |
Jörg Hohensohn | 88faf38 | 2004-01-16 09:02:21 +0000 | [diff] [blame] | 259 | unsigned char* bufend = buf + wordcount*2; |
| 260 | do |
Jens Arnold | 31ffd7b | 2006-12-03 22:13:44 +0000 | [diff] [blame] | 261 | { |
Jörg Hohensohn | cb570b9 | 2004-01-27 09:12:51 +0000 | [diff] [blame] | 262 | tmp = ATA_DATA; |
Dave Chapman | cf0e3d3 | 2006-01-18 14:04:30 +0000 | [diff] [blame] | 263 | #if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN) |
Jörg Hohensohn | 88faf38 | 2004-01-16 09:02:21 +0000 | [diff] [blame] | 264 | *buf++ = tmp & 0xff; /* I assume big endian */ |
| 265 | *buf++ = tmp >> 8; /* and don't use the SWAB16 macro */ |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 266 | #else |
| 267 | *buf++ = tmp >> 8; |
| 268 | *buf++ = tmp & 0xff; |
| 269 | #endif |
Jörg Hohensohn | 88faf38 | 2004-01-16 09:02:21 +0000 | [diff] [blame] | 270 | } while (buf < bufend); /* tail loop is faster */ |
| 271 | } |
Jörg Hohensohn | 5fb1e10 | 2004-03-10 14:15:14 +0000 | [diff] [blame] | 272 | else |
Jörg Hohensohn | cb570b9 | 2004-01-27 09:12:51 +0000 | [diff] [blame] | 273 | { /* 16-bit aligned, can do faster copy */ |
Jörg Hohensohn | 88faf38 | 2004-01-16 09:02:21 +0000 | [diff] [blame] | 274 | unsigned short* wbuf = (unsigned short*)buf; |
| 275 | unsigned short* wbufend = wbuf + wordcount; |
| 276 | do |
Jens Arnold | 31ffd7b | 2006-12-03 22:13:44 +0000 | [diff] [blame] | 277 | { |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 278 | #ifdef SWAP_WORDS |
Dave Chapman | 77372d1 | 2005-11-07 23:07:19 +0000 | [diff] [blame] | 279 | *wbuf = swap16(ATA_DATA); |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 280 | #else |
| 281 | *wbuf = ATA_DATA; |
| 282 | #endif |
Jörg Hohensohn | 88faf38 | 2004-01-16 09:02:21 +0000 | [diff] [blame] | 283 | } while (++wbuf < wbufend); /* tail loop is faster */ |
| 284 | } |
| 285 | } |
Jens Arnold | 31ffd7b | 2006-12-03 22:13:44 +0000 | [diff] [blame] | 286 | #endif /* !ATA_OPTIMIZED_READING */ |
Linus Nielsen Feltzing | 68482bb | 2005-04-04 09:12:12 +0000 | [diff] [blame] | 287 | |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 288 | #ifdef MAX_PHYS_SECTOR_SIZE |
| 289 | static int _read_sectors(unsigned long start, |
| 290 | int incount, |
| 291 | void* inbuf) |
| 292 | #else |
Jörg Hohensohn | 575c904 | 2004-12-29 22:37:31 +0000 | [diff] [blame] | 293 | int ata_read_sectors(IF_MV2(int drive,) |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 294 | unsigned long start, |
Björn Stenberg | 7526cf7 | 2002-09-25 14:10:50 +0000 | [diff] [blame] | 295 | int incount, |
| 296 | void* inbuf) |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 297 | #endif |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 298 | { |
Linus Nielsen Feltzing | 3922466 | 2002-05-16 21:28:21 +0000 | [diff] [blame] | 299 | int ret = 0; |
Jean-Philippe Bernardy | fc19445 | 2005-02-25 18:50:16 +0000 | [diff] [blame] | 300 | long timeout; |
Björn Stenberg | 7526cf7 | 2002-09-25 14:10:50 +0000 | [diff] [blame] | 301 | int count; |
| 302 | void* buf; |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 303 | long spinup_start; |
Linus Nielsen Feltzing | 22e09a3 | 2002-08-27 21:06:48 +0000 | [diff] [blame] | 304 | |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 305 | #ifndef MAX_PHYS_SECTOR_SIZE |
Jörg Hohensohn | 575c904 | 2004-12-29 22:37:31 +0000 | [diff] [blame] | 306 | #ifdef HAVE_MULTIVOLUME |
| 307 | (void)drive; /* unused for now */ |
| 308 | #endif |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 309 | mutex_lock(&ata_mtx); |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 310 | #endif |
Linus Nielsen Feltzing | 22e09a3 | 2002-08-27 21:06:48 +0000 | [diff] [blame] | 311 | |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 312 | if (start + incount > total_sectors) { |
| 313 | ret = -1; |
| 314 | goto error; |
| 315 | } |
| 316 | |
Björn Stenberg | ba0438e | 2002-12-05 09:35:01 +0000 | [diff] [blame] | 317 | last_disk_activity = current_tick; |
Linus Nielsen Feltzing | 4502e98 | 2003-04-05 22:25:21 +0000 | [diff] [blame] | 318 | spinup_start = current_tick; |
Björn Stenberg | ba0438e | 2002-12-05 09:35:01 +0000 | [diff] [blame] | 319 | |
Linus Nielsen Feltzing | 68482bb | 2005-04-04 09:12:12 +0000 | [diff] [blame] | 320 | ata_led(true); |
Björn Stenberg | 494d261 | 2002-11-27 15:55:47 +0000 | [diff] [blame] | 321 | |
Björn Stenberg | c4b2850 | 2002-07-16 12:18:17 +0000 | [diff] [blame] | 322 | if ( sleeping ) { |
Björn Stenberg | efd9077 | 2002-12-06 15:17:30 +0000 | [diff] [blame] | 323 | spinup = true; |
Björn Stenberg | 494d261 | 2002-11-27 15:55:47 +0000 | [diff] [blame] | 324 | if (poweroff) { |
| 325 | if (ata_power_on()) { |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 326 | ret = -2; |
| 327 | goto error; |
Björn Stenberg | 494d261 | 2002-11-27 15:55:47 +0000 | [diff] [blame] | 328 | } |
Linus Nielsen Feltzing | 513e851 | 2002-08-02 06:01:22 +0000 | [diff] [blame] | 329 | } |
Björn Stenberg | 494d261 | 2002-11-27 15:55:47 +0000 | [diff] [blame] | 330 | else { |
| 331 | if (perform_soft_reset()) { |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 332 | ret = -2; |
| 333 | goto error; |
Björn Stenberg | 494d261 | 2002-11-27 15:55:47 +0000 | [diff] [blame] | 334 | } |
Björn Stenberg | c4b2850 | 2002-07-16 12:18:17 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 337 | |
Björn Stenberg | 6a58106 | 2003-02-27 13:26:09 +0000 | [diff] [blame] | 338 | timeout = current_tick + READ_TIMEOUT; |
| 339 | |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 340 | SET_REG(ATA_SELECT, ata_device); |
Linus Nielsen Feltzing | b900a83 | 2002-05-08 08:27:44 +0000 | [diff] [blame] | 341 | if (!wait_for_rdy()) |
Linus Nielsen Feltzing | 3922466 | 2002-05-16 21:28:21 +0000 | [diff] [blame] | 342 | { |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 343 | ret = -3; |
| 344 | goto error; |
Linus Nielsen Feltzing | 3922466 | 2002-05-16 21:28:21 +0000 | [diff] [blame] | 345 | } |
Linus Nielsen Feltzing | b900a83 | 2002-05-08 08:27:44 +0000 | [diff] [blame] | 346 | |
Björn Stenberg | 674b632 | 2003-04-28 12:02:14 +0000 | [diff] [blame] | 347 | retry: |
Björn Stenberg | 7526cf7 | 2002-09-25 14:10:50 +0000 | [diff] [blame] | 348 | buf = inbuf; |
| 349 | count = incount; |
Björn Stenberg | 70747f9 | 2002-09-24 14:23:18 +0000 | [diff] [blame] | 350 | while (TIME_BEFORE(current_tick, timeout)) { |
Björn Stenberg | b12401b | 2003-03-13 15:45:38 +0000 | [diff] [blame] | 351 | ret = 0; |
Björn Stenberg | 7682d46 | 2003-03-17 13:42:30 +0000 | [diff] [blame] | 352 | last_disk_activity = current_tick; |
Björn Stenberg | f0599be | 2002-08-26 22:05:47 +0000 | [diff] [blame] | 353 | |
Jörg Hohensohn | bb03586 | 2006-12-20 22:08:29 +0000 | [diff] [blame] | 354 | #ifdef HAVE_LBA48 |
| 355 | if (lba48) |
Jörg Hohensohn | f5bb766 | 2006-12-19 22:40:23 +0000 | [diff] [blame] | 356 | { |
| 357 | SET_REG(ATA_NSECTOR, count >> 8); |
| 358 | SET_REG(ATA_NSECTOR, count & 0xff); |
| 359 | SET_REG(ATA_SECTOR, (start >> 24) & 0xff); /* 31:24 */ |
| 360 | SET_REG(ATA_SECTOR, start & 0xff); /* 7:0 */ |
| 361 | SET_REG(ATA_LCYL, 0); /* 39:32 */ |
| 362 | SET_REG(ATA_LCYL, (start >> 8) & 0xff); /* 15:8 */ |
| 363 | SET_REG(ATA_HCYL, 0); /* 47:40 */ |
| 364 | SET_REG(ATA_HCYL, (start >> 16) & 0xff); /* 23:16 */ |
| 365 | SET_REG(ATA_SELECT, SELECT_LBA | ata_device); |
| 366 | SET_REG(ATA_COMMAND, CMD_READ_MULTIPLE_EXT); |
| 367 | } |
Björn Stenberg | 8ccbc76 | 2002-09-05 15:25:08 +0000 | [diff] [blame] | 368 | else |
Jörg Hohensohn | f5bb766 | 2006-12-19 22:40:23 +0000 | [diff] [blame] | 369 | #endif |
| 370 | { |
Jörg Hohensohn | bb03586 | 2006-12-20 22:08:29 +0000 | [diff] [blame] | 371 | SET_REG(ATA_NSECTOR, count & 0xff); /* 0 means 256 sectors */ |
Jörg Hohensohn | f5bb766 | 2006-12-19 22:40:23 +0000 | [diff] [blame] | 372 | SET_REG(ATA_SECTOR, start & 0xff); |
| 373 | SET_REG(ATA_LCYL, (start >> 8) & 0xff); |
| 374 | SET_REG(ATA_HCYL, (start >> 16) & 0xff); |
| 375 | SET_REG(ATA_SELECT, ((start >> 24) & 0xf) | SELECT_LBA | ata_device); |
| 376 | SET_REG(ATA_COMMAND, CMD_READ_MULTIPLE); |
| 377 | } |
Linus Nielsen Feltzing | e82f701 | 2002-09-06 16:02:19 +0000 | [diff] [blame] | 378 | |
Björn Stenberg | fd9ce90 | 2003-03-31 14:14:07 +0000 | [diff] [blame] | 379 | /* wait at least 400ns between writing command and reading status */ |
Jean-Philippe Bernardy | 99e72c8 | 2005-01-20 23:00:11 +0000 | [diff] [blame] | 380 | __asm__ volatile ("nop"); |
| 381 | __asm__ volatile ("nop"); |
| 382 | __asm__ volatile ("nop"); |
| 383 | __asm__ volatile ("nop"); |
| 384 | __asm__ volatile ("nop"); |
Björn Stenberg | fd9ce90 | 2003-03-31 14:14:07 +0000 | [diff] [blame] | 385 | |
Björn Stenberg | 70747f9 | 2002-09-24 14:23:18 +0000 | [diff] [blame] | 386 | while (count) { |
Björn Stenberg | 70747f9 | 2002-09-24 14:23:18 +0000 | [diff] [blame] | 387 | int sectors; |
| 388 | int wordcount; |
Björn Stenberg | bb6e51a | 2003-03-14 16:06:09 +0000 | [diff] [blame] | 389 | int status; |
Björn Stenberg | 70747f9 | 2002-09-24 14:23:18 +0000 | [diff] [blame] | 390 | |
| 391 | if (!wait_for_start_of_transfer()) { |
Linus Nielsen Feltzing | 8bda0e1 | 2004-03-19 13:26:43 +0000 | [diff] [blame] | 392 | /* We have timed out waiting for RDY and/or DRQ, possibly |
| 393 | because the hard drive is shaking and has problems reading |
| 394 | the data. We have two options: |
| 395 | 1) Wait some more |
| 396 | 2) Perform a soft reset and try again. |
| 397 | |
| 398 | We choose alternative 2. |
| 399 | */ |
Jens Arnold | 97599d3 | 2005-05-10 22:39:53 +0000 | [diff] [blame] | 400 | perform_soft_reset(); |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 401 | ret = -5; |
Björn Stenberg | 7526cf7 | 2002-09-25 14:10:50 +0000 | [diff] [blame] | 402 | goto retry; |
Björn Stenberg | ed027a9 | 2002-08-14 14:34:54 +0000 | [diff] [blame] | 403 | } |
Björn Stenberg | 70747f9 | 2002-09-24 14:23:18 +0000 | [diff] [blame] | 404 | |
Björn Stenberg | bd0da0e | 2002-12-06 13:08:42 +0000 | [diff] [blame] | 405 | if (spinup) { |
Linus Nielsen Feltzing | 4502e98 | 2003-04-05 22:25:21 +0000 | [diff] [blame] | 406 | ata_spinup_time = current_tick - spinup_start; |
Björn Stenberg | bd0da0e | 2002-12-06 13:08:42 +0000 | [diff] [blame] | 407 | spinup = false; |
| 408 | sleeping = false; |
| 409 | poweroff = false; |
| 410 | } |
| 411 | |
Björn Stenberg | bb6e51a | 2003-03-14 16:06:09 +0000 | [diff] [blame] | 412 | /* read the status register exactly once per loop */ |
| 413 | status = ATA_STATUS; |
| 414 | |
Björn Stenberg | 70747f9 | 2002-09-24 14:23:18 +0000 | [diff] [blame] | 415 | if (count >= multisectors ) |
| 416 | sectors = multisectors; |
| 417 | else |
| 418 | sectors = count; |
| 419 | |
| 420 | wordcount = sectors * SECTOR_SIZE / 2; |
| 421 | |
Jörg Hohensohn | 88faf38 | 2004-01-16 09:02:21 +0000 | [diff] [blame] | 422 | copy_read_sectors(buf, wordcount); |
Björn Stenberg | 6c9e578 | 2002-04-27 14:19:00 +0000 | [diff] [blame] | 423 | |
Björn Stenberg | bb6e51a | 2003-03-14 16:06:09 +0000 | [diff] [blame] | 424 | /* |
| 425 | "Device errors encountered during READ MULTIPLE commands are |
| 426 | posted at the beginning of the block or partial block transfer, |
| 427 | but the DRQ bit is still set to one and the data transfer shall |
| 428 | take place, including transfer of corrupted data, if any." |
| 429 | -- ATA specification |
| 430 | */ |
| 431 | if ( status & (STATUS_BSY | STATUS_ERR | STATUS_DF) ) { |
Jens Arnold | 97599d3 | 2005-05-10 22:39:53 +0000 | [diff] [blame] | 432 | perform_soft_reset(); |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 433 | ret = -6; |
Björn Stenberg | bb6e51a | 2003-03-14 16:06:09 +0000 | [diff] [blame] | 434 | goto retry; |
| 435 | } |
Jens Arnold | 97599d3 | 2005-05-10 22:39:53 +0000 | [diff] [blame] | 436 | |
Björn Stenberg | 70747f9 | 2002-09-24 14:23:18 +0000 | [diff] [blame] | 437 | buf += sectors * SECTOR_SIZE; /* Advance one chunk of sectors */ |
| 438 | count -= sectors; |
Björn Stenberg | ba0438e | 2002-12-05 09:35:01 +0000 | [diff] [blame] | 439 | |
| 440 | last_disk_activity = current_tick; |
Björn Stenberg | 70747f9 | 2002-09-24 14:23:18 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Björn Stenberg | aa78324 | 2003-03-24 16:19:54 +0000 | [diff] [blame] | 443 | if(!ret && !wait_for_end_of_transfer()) { |
Jens Arnold | 97599d3 | 2005-05-10 22:39:53 +0000 | [diff] [blame] | 444 | perform_soft_reset(); |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 445 | ret = -4; |
Björn Stenberg | 7526cf7 | 2002-09-25 14:10:50 +0000 | [diff] [blame] | 446 | goto retry; |
Björn Stenberg | 70747f9 | 2002-09-24 14:23:18 +0000 | [diff] [blame] | 447 | } |
Björn Stenberg | 70747f9 | 2002-09-24 14:23:18 +0000 | [diff] [blame] | 448 | break; |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 449 | } |
Jens Arnold | 8c6920e | 2008-03-08 23:50:55 +0000 | [diff] [blame] | 450 | |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 451 | error: |
| 452 | ata_led(false); |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 453 | #ifndef MAX_PHYS_SECTOR_SIZE |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 454 | mutex_unlock(&ata_mtx); |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 455 | #endif |
Björn Stenberg | c9d98ca | 2002-08-15 12:42:37 +0000 | [diff] [blame] | 456 | |
Linus Nielsen Feltzing | 3922466 | 2002-05-16 21:28:21 +0000 | [diff] [blame] | 457 | return ret; |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Jens Arnold | 31ffd7b | 2006-12-03 22:13:44 +0000 | [diff] [blame] | 460 | #ifndef ATA_OPTIMIZED_WRITING |
Jens Arnold | f18e4db | 2008-05-08 05:39:29 +0000 | [diff] [blame] | 461 | STATICIRAM ICODE_ATTR void copy_write_sectors(const unsigned char* buf, |
| 462 | int wordcount) |
Jörg Hohensohn | 9c52b24 | 2004-04-01 05:46:31 +0000 | [diff] [blame] | 463 | { |
Jean-Philippe Bernardy | 34d2a71 | 2005-01-24 14:26:24 +0000 | [diff] [blame] | 464 | if ( (unsigned long)buf & 1) |
Jörg Hohensohn | 9c52b24 | 2004-04-01 05:46:31 +0000 | [diff] [blame] | 465 | { /* not 16-bit aligned, copy byte by byte */ |
| 466 | unsigned short tmp = 0; |
Jens Arnold | 8eedc94 | 2004-10-08 19:20:20 +0000 | [diff] [blame] | 467 | const unsigned char* bufend = buf + wordcount*2; |
Jörg Hohensohn | 9c52b24 | 2004-04-01 05:46:31 +0000 | [diff] [blame] | 468 | do |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 469 | { |
Dave Chapman | cf0e3d3 | 2006-01-18 14:04:30 +0000 | [diff] [blame] | 470 | #if defined(SWAP_WORDS) || defined(ROCKBOX_LITTLE_ENDIAN) |
Jens Arnold | 44e76cf | 2004-06-11 06:56:51 +0000 | [diff] [blame] | 471 | tmp = (unsigned short) *buf++; |
Jens Arnold | 31ffd7b | 2006-12-03 22:13:44 +0000 | [diff] [blame] | 472 | tmp |= (unsigned short) *buf++ << 8; |
| 473 | SET_16BITREG(ATA_DATA, tmp); |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 474 | #else |
| 475 | tmp = (unsigned short) *buf++ << 8; |
| 476 | tmp |= (unsigned short) *buf++; |
| 477 | SET_16BITREG(ATA_DATA, tmp); |
| 478 | #endif |
Jörg Hohensohn | 9c52b24 | 2004-04-01 05:46:31 +0000 | [diff] [blame] | 479 | } while (buf < bufend); /* tail loop is faster */ |
| 480 | } |
| 481 | else |
| 482 | { /* 16-bit aligned, can do faster copy */ |
| 483 | unsigned short* wbuf = (unsigned short*)buf; |
| 484 | unsigned short* wbufend = wbuf + wordcount; |
| 485 | do |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 486 | { |
| 487 | #ifdef SWAP_WORDS |
Dave Chapman | 77372d1 | 2005-11-07 23:07:19 +0000 | [diff] [blame] | 488 | SET_16BITREG(ATA_DATA, swap16(*wbuf)); |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 489 | #else |
| 490 | SET_16BITREG(ATA_DATA, *wbuf); |
| 491 | #endif |
Jörg Hohensohn | 9c52b24 | 2004-04-01 05:46:31 +0000 | [diff] [blame] | 492 | } while (++wbuf < wbufend); /* tail loop is faster */ |
| 493 | } |
Jörg Hohensohn | 9c52b24 | 2004-04-01 05:46:31 +0000 | [diff] [blame] | 494 | } |
Jens Arnold | 31ffd7b | 2006-12-03 22:13:44 +0000 | [diff] [blame] | 495 | #endif /* !ATA_OPTIMIZED_WRITING */ |
Jörg Hohensohn | 9c52b24 | 2004-04-01 05:46:31 +0000 | [diff] [blame] | 496 | |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 497 | #ifdef MAX_PHYS_SECTOR_SIZE |
| 498 | static int _write_sectors(unsigned long start, |
| 499 | int count, |
| 500 | const void* buf) |
| 501 | #else |
Jörg Hohensohn | 575c904 | 2004-12-29 22:37:31 +0000 | [diff] [blame] | 502 | int ata_write_sectors(IF_MV2(int drive,) |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 503 | unsigned long start, |
Björn Stenberg | 63457c5 | 2002-08-14 16:37:28 +0000 | [diff] [blame] | 504 | int count, |
Jens Arnold | 0ceaa5e | 2004-08-17 01:45:48 +0000 | [diff] [blame] | 505 | const void* buf) |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 506 | #endif |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 507 | { |
| 508 | int i; |
Björn Stenberg | f9fed81 | 2002-11-07 22:40:24 +0000 | [diff] [blame] | 509 | int ret = 0; |
Jean-Philippe Bernardy | fc19445 | 2005-02-25 18:50:16 +0000 | [diff] [blame] | 510 | long spinup_start; |
Björn Stenberg | 457b8a0 | 2002-08-26 13:21:14 +0000 | [diff] [blame] | 511 | |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 512 | #ifndef MAX_PHYS_SECTOR_SIZE |
| 513 | #ifdef HAVE_MULTIVOLUME |
| 514 | (void)drive; /* unused for now */ |
| 515 | #endif |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 516 | mutex_lock(&ata_mtx); |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 517 | #endif |
Linus Nielsen Feltzing | 22e09a3 | 2002-08-27 21:06:48 +0000 | [diff] [blame] | 518 | |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 519 | if (start + count > total_sectors) |
| 520 | panicf("Writing past end of disk"); |
| 521 | |
Björn Stenberg | ba0438e | 2002-12-05 09:35:01 +0000 | [diff] [blame] | 522 | last_disk_activity = current_tick; |
Linus Nielsen Feltzing | 4502e98 | 2003-04-05 22:25:21 +0000 | [diff] [blame] | 523 | spinup_start = current_tick; |
Björn Stenberg | ba0438e | 2002-12-05 09:35:01 +0000 | [diff] [blame] | 524 | |
Linus Nielsen Feltzing | 68482bb | 2005-04-04 09:12:12 +0000 | [diff] [blame] | 525 | ata_led(true); |
Björn Stenberg | fd9ce90 | 2003-03-31 14:14:07 +0000 | [diff] [blame] | 526 | |
Björn Stenberg | f0599be | 2002-08-26 22:05:47 +0000 | [diff] [blame] | 527 | if ( sleeping ) { |
Björn Stenberg | efd9077 | 2002-12-06 15:17:30 +0000 | [diff] [blame] | 528 | spinup = true; |
Björn Stenberg | 494d261 | 2002-11-27 15:55:47 +0000 | [diff] [blame] | 529 | if (poweroff) { |
| 530 | if (ata_power_on()) { |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 531 | ret = -1; |
| 532 | goto error; |
Björn Stenberg | 494d261 | 2002-11-27 15:55:47 +0000 | [diff] [blame] | 533 | } |
Linus Nielsen Feltzing | 513e851 | 2002-08-02 06:01:22 +0000 | [diff] [blame] | 534 | } |
Björn Stenberg | 494d261 | 2002-11-27 15:55:47 +0000 | [diff] [blame] | 535 | else { |
| 536 | if (perform_soft_reset()) { |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 537 | ret = -1; |
| 538 | goto error; |
Björn Stenberg | 494d261 | 2002-11-27 15:55:47 +0000 | [diff] [blame] | 539 | } |
Björn Stenberg | c4b2850 | 2002-07-16 12:18:17 +0000 | [diff] [blame] | 540 | } |
Björn Stenberg | f0599be | 2002-08-26 22:05:47 +0000 | [diff] [blame] | 541 | } |
Linus Nielsen Feltzing | 3922466 | 2002-05-16 21:28:21 +0000 | [diff] [blame] | 542 | |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 543 | SET_REG(ATA_SELECT, ata_device); |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 544 | if (!wait_for_rdy()) |
Linus Nielsen Feltzing | 3922466 | 2002-05-16 21:28:21 +0000 | [diff] [blame] | 545 | { |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 546 | ret = -2; |
| 547 | goto error; |
Linus Nielsen Feltzing | 3922466 | 2002-05-16 21:28:21 +0000 | [diff] [blame] | 548 | } |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 549 | |
Jörg Hohensohn | bb03586 | 2006-12-20 22:08:29 +0000 | [diff] [blame] | 550 | #ifdef HAVE_LBA48 |
| 551 | if (lba48) |
Jörg Hohensohn | f5bb766 | 2006-12-19 22:40:23 +0000 | [diff] [blame] | 552 | { |
| 553 | SET_REG(ATA_NSECTOR, count >> 8); |
| 554 | SET_REG(ATA_NSECTOR, count & 0xff); |
| 555 | SET_REG(ATA_SECTOR, (start >> 24) & 0xff); /* 31:24 */ |
| 556 | SET_REG(ATA_SECTOR, start & 0xff); /* 7:0 */ |
| 557 | SET_REG(ATA_LCYL, 0); /* 39:32 */ |
| 558 | SET_REG(ATA_LCYL, (start >> 8) & 0xff); /* 15:8 */ |
| 559 | SET_REG(ATA_HCYL, 0); /* 47:40 */ |
| 560 | SET_REG(ATA_HCYL, (start >> 16) & 0xff); /* 23:16 */ |
| 561 | SET_REG(ATA_SELECT, SELECT_LBA | ata_device); |
| 562 | SET_REG(ATA_COMMAND, CMD_WRITE_SECTORS_EXT); |
| 563 | } |
Björn Stenberg | 63457c5 | 2002-08-14 16:37:28 +0000 | [diff] [blame] | 564 | else |
Jörg Hohensohn | f5bb766 | 2006-12-19 22:40:23 +0000 | [diff] [blame] | 565 | #endif |
| 566 | { |
Jörg Hohensohn | bb03586 | 2006-12-20 22:08:29 +0000 | [diff] [blame] | 567 | SET_REG(ATA_NSECTOR, count & 0xff); /* 0 means 256 sectors */ |
Jörg Hohensohn | f5bb766 | 2006-12-19 22:40:23 +0000 | [diff] [blame] | 568 | SET_REG(ATA_SECTOR, start & 0xff); |
| 569 | SET_REG(ATA_LCYL, (start >> 8) & 0xff); |
| 570 | SET_REG(ATA_HCYL, (start >> 16) & 0xff); |
| 571 | SET_REG(ATA_SELECT, ((start >> 24) & 0xf) | SELECT_LBA | ata_device); |
| 572 | SET_REG(ATA_COMMAND, CMD_WRITE_SECTORS); |
| 573 | } |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 574 | |
| 575 | for (i=0; i<count; i++) { |
Jörg Hohensohn | 9c52b24 | 2004-04-01 05:46:31 +0000 | [diff] [blame] | 576 | |
Björn Stenberg | 674b632 | 2003-04-28 12:02:14 +0000 | [diff] [blame] | 577 | if (!wait_for_start_of_transfer()) { |
| 578 | ret = -3; |
| 579 | break; |
Linus Nielsen Feltzing | 3922466 | 2002-05-16 21:28:21 +0000 | [diff] [blame] | 580 | } |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 581 | |
Björn Stenberg | ba0438e | 2002-12-05 09:35:01 +0000 | [diff] [blame] | 582 | if (spinup) { |
Linus Nielsen Feltzing | 4502e98 | 2003-04-05 22:25:21 +0000 | [diff] [blame] | 583 | ata_spinup_time = current_tick - spinup_start; |
Björn Stenberg | ba0438e | 2002-12-05 09:35:01 +0000 | [diff] [blame] | 584 | spinup = false; |
Björn Stenberg | bd0da0e | 2002-12-06 13:08:42 +0000 | [diff] [blame] | 585 | sleeping = false; |
| 586 | poweroff = false; |
Björn Stenberg | ba0438e | 2002-12-05 09:35:01 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Jörg Hohensohn | 9c52b24 | 2004-04-01 05:46:31 +0000 | [diff] [blame] | 589 | copy_write_sectors(buf, SECTOR_SIZE/2); |
Björn Stenberg | 6c9e578 | 2002-04-27 14:19:00 +0000 | [diff] [blame] | 590 | |
| 591 | #ifdef USE_INTERRUPT |
| 592 | /* reading the status register clears the interrupt */ |
| 593 | j = ATA_STATUS; |
| 594 | #endif |
Linus Nielsen Feltzing | a4058ec | 2002-06-12 09:28:22 +0000 | [diff] [blame] | 595 | buf += SECTOR_SIZE; |
Björn Stenberg | ba0438e | 2002-12-05 09:35:01 +0000 | [diff] [blame] | 596 | |
| 597 | last_disk_activity = current_tick; |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Jean-Philippe Bernardy | 99e72c8 | 2005-01-20 23:00:11 +0000 | [diff] [blame] | 600 | if(!ret && !wait_for_end_of_transfer()) { |
| 601 | DEBUGF("End on transfer failed. -- jyp"); |
Björn Stenberg | 674b632 | 2003-04-28 12:02:14 +0000 | [diff] [blame] | 602 | ret = -4; |
Jean-Philippe Bernardy | 99e72c8 | 2005-01-20 23:00:11 +0000 | [diff] [blame] | 603 | } |
Linus Nielsen Feltzing | 3922466 | 2002-05-16 21:28:21 +0000 | [diff] [blame] | 604 | |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 605 | error: |
Linus Nielsen Feltzing | 68482bb | 2005-04-04 09:12:12 +0000 | [diff] [blame] | 606 | ata_led(false); |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 607 | #ifndef MAX_PHYS_SECTOR_SIZE |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 608 | mutex_unlock(&ata_mtx); |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 609 | #endif |
Björn Stenberg | c9d98ca | 2002-08-15 12:42:37 +0000 | [diff] [blame] | 610 | |
Björn Stenberg | f9fed81 | 2002-11-07 22:40:24 +0000 | [diff] [blame] | 611 | return ret; |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 614 | #ifdef MAX_PHYS_SECTOR_SIZE |
| 615 | static int cache_sector(unsigned long sector) |
| 616 | { |
| 617 | int rc; |
| 618 | |
| 619 | sector &= ~(phys_sector_mult - 1); |
| 620 | /* round down to physical sector boundary */ |
| 621 | |
| 622 | /* check whether the sector is already cached */ |
| 623 | if (sector_cache.inuse && (sector_cache.sectornum == sector)) |
| 624 | return 0; |
| 625 | |
| 626 | /* not found: read the sector */ |
| 627 | sector_cache.inuse = false; |
| 628 | rc = _read_sectors(sector, phys_sector_mult, sector_cache.data); |
| 629 | if (!rc) |
| 630 | { |
| 631 | sector_cache.sectornum = sector; |
| 632 | sector_cache.inuse = true; |
| 633 | } |
| 634 | return rc; |
| 635 | } |
| 636 | |
| 637 | static inline int flush_current_sector(void) |
| 638 | { |
| 639 | return _write_sectors(sector_cache.sectornum, phys_sector_mult, |
| 640 | sector_cache.data); |
| 641 | } |
| 642 | |
| 643 | int ata_read_sectors(IF_MV2(int drive,) |
| 644 | unsigned long start, |
| 645 | int incount, |
| 646 | void* inbuf) |
| 647 | { |
| 648 | int rc = 0; |
| 649 | int offset; |
| 650 | |
| 651 | #ifdef HAVE_MULTIVOLUME |
| 652 | (void)drive; /* unused for now */ |
| 653 | #endif |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 654 | mutex_lock(&ata_mtx); |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 655 | |
| 656 | offset = start & (phys_sector_mult - 1); |
| 657 | |
| 658 | if (offset) /* first partial sector */ |
| 659 | { |
| 660 | int partcount = MIN(incount, phys_sector_mult - offset); |
| 661 | |
| 662 | rc = cache_sector(start); |
| 663 | if (rc) |
| 664 | { |
| 665 | rc = rc * 10 - 1; |
| 666 | goto error; |
| 667 | } |
| 668 | memcpy(inbuf, sector_cache.data + offset * SECTOR_SIZE, |
| 669 | partcount * SECTOR_SIZE); |
| 670 | |
| 671 | start += partcount; |
| 672 | inbuf += partcount * SECTOR_SIZE; |
| 673 | incount -= partcount; |
| 674 | } |
| 675 | if (incount) |
| 676 | { |
| 677 | offset = incount & (phys_sector_mult - 1); |
| 678 | incount -= offset; |
| 679 | |
| 680 | if (incount) |
| 681 | { |
| 682 | rc = _read_sectors(start, incount, inbuf); |
| 683 | if (rc) |
| 684 | { |
| 685 | rc = rc * 10 - 2; |
| 686 | goto error; |
| 687 | } |
| 688 | start += incount; |
| 689 | inbuf += incount * SECTOR_SIZE; |
| 690 | } |
| 691 | if (offset) |
| 692 | { |
| 693 | rc = cache_sector(start); |
| 694 | if (rc) |
| 695 | { |
| 696 | rc = rc * 10 - 3; |
| 697 | goto error; |
| 698 | } |
| 699 | memcpy(inbuf, sector_cache.data, offset * SECTOR_SIZE); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | error: |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 704 | mutex_unlock(&ata_mtx); |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 705 | |
| 706 | return rc; |
| 707 | } |
| 708 | |
| 709 | int ata_write_sectors(IF_MV2(int drive,) |
| 710 | unsigned long start, |
| 711 | int count, |
| 712 | const void* buf) |
| 713 | { |
| 714 | int rc = 0; |
| 715 | int offset; |
| 716 | |
| 717 | #ifdef HAVE_MULTIVOLUME |
| 718 | (void)drive; /* unused for now */ |
| 719 | #endif |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 720 | mutex_lock(&ata_mtx); |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 721 | |
| 722 | offset = start & (phys_sector_mult - 1); |
| 723 | |
| 724 | if (offset) /* first partial sector */ |
| 725 | { |
| 726 | int partcount = MIN(count, phys_sector_mult - offset); |
| 727 | |
| 728 | rc = cache_sector(start); |
| 729 | if (rc) |
| 730 | { |
| 731 | rc = rc * 10 - 1; |
| 732 | goto error; |
| 733 | } |
| 734 | memcpy(sector_cache.data + offset * SECTOR_SIZE, buf, |
| 735 | partcount * SECTOR_SIZE); |
| 736 | rc = flush_current_sector(); |
| 737 | if (rc) |
| 738 | { |
| 739 | rc = rc * 10 - 2; |
| 740 | goto error; |
| 741 | } |
| 742 | start += partcount; |
| 743 | buf += partcount * SECTOR_SIZE; |
| 744 | count -= partcount; |
| 745 | } |
| 746 | if (count) |
| 747 | { |
| 748 | offset = count & (phys_sector_mult - 1); |
| 749 | count -= offset; |
| 750 | |
| 751 | if (count) |
| 752 | { |
| 753 | rc = _write_sectors(start, count, buf); |
| 754 | if (rc) |
| 755 | { |
| 756 | rc = rc * 10 - 3; |
| 757 | goto error; |
| 758 | } |
| 759 | start += count; |
| 760 | buf += count * SECTOR_SIZE; |
| 761 | } |
| 762 | if (offset) |
| 763 | { |
| 764 | rc = cache_sector(start); |
| 765 | if (rc) |
| 766 | { |
| 767 | rc = rc * 10 - 4; |
| 768 | goto error; |
| 769 | } |
| 770 | memcpy(sector_cache.data, buf, offset * SECTOR_SIZE); |
| 771 | rc = flush_current_sector(); |
| 772 | if (rc) |
| 773 | { |
| 774 | rc = rc * 10 - 5; |
| 775 | goto error; |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | error: |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 781 | mutex_unlock(&ata_mtx); |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 782 | |
| 783 | return rc; |
| 784 | } |
| 785 | #endif /* MAX_PHYS_SECTOR_SIZE */ |
| 786 | |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 787 | static int check_registers(void) |
| 788 | { |
Jean-Philippe Bernardy | 99e72c8 | 2005-01-20 23:00:11 +0000 | [diff] [blame] | 789 | int i; |
Jörg Hohensohn | adef8fb | 2003-07-08 06:33:30 +0000 | [diff] [blame] | 790 | if ( ATA_STATUS & STATUS_BSY ) |
Jörg Hohensohn | d1a3a3e | 2003-07-03 00:02:15 +0000 | [diff] [blame] | 791 | return -1; |
| 792 | |
Jean-Philippe Bernardy | 99e72c8 | 2005-01-20 23:00:11 +0000 | [diff] [blame] | 793 | for (i = 0; i<64; i++) { |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 794 | SET_REG(ATA_NSECTOR, WRITE_PATTERN1); |
| 795 | SET_REG(ATA_SECTOR, WRITE_PATTERN2); |
| 796 | SET_REG(ATA_LCYL, WRITE_PATTERN3); |
| 797 | SET_REG(ATA_HCYL, WRITE_PATTERN4); |
| 798 | |
| 799 | if (((ATA_NSECTOR & READ_PATTERN1_MASK) == READ_PATTERN1) && |
| 800 | ((ATA_SECTOR & READ_PATTERN2_MASK) == READ_PATTERN2) && |
| 801 | ((ATA_LCYL & READ_PATTERN3_MASK) == READ_PATTERN3) && |
| 802 | ((ATA_HCYL & READ_PATTERN4_MASK) == READ_PATTERN4)) |
| 803 | return 0; |
Jean-Philippe Bernardy | 99e72c8 | 2005-01-20 23:00:11 +0000 | [diff] [blame] | 804 | } |
Björn Stenberg | a97c441 | 2002-05-24 11:25:24 +0000 | [diff] [blame] | 805 | return -2; |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 808 | static int freeze_lock(void) |
| 809 | { |
Björn Stenberg | 65bf851 | 2004-09-17 11:28:07 +0000 | [diff] [blame] | 810 | /* does the disk support Security Mode feature set? */ |
| 811 | if (identify_info[82] & 2) |
| 812 | { |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 813 | SET_REG(ATA_SELECT, ata_device); |
Björn Stenberg | c032e65 | 2002-12-03 13:29:35 +0000 | [diff] [blame] | 814 | |
Björn Stenberg | 65bf851 | 2004-09-17 11:28:07 +0000 | [diff] [blame] | 815 | if (!wait_for_rdy()) |
| 816 | return -1; |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 817 | |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 818 | SET_REG(ATA_COMMAND, CMD_SECURITY_FREEZE_LOCK); |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 819 | |
Björn Stenberg | 65bf851 | 2004-09-17 11:28:07 +0000 | [diff] [blame] | 820 | if (!wait_for_rdy()) |
| 821 | return -2; |
| 822 | } |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 823 | |
| 824 | return 0; |
| 825 | } |
| 826 | |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 827 | void ata_spindown(int seconds) |
Björn Stenberg | 6c9e578 | 2002-04-27 14:19:00 +0000 | [diff] [blame] | 828 | { |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 829 | sleep_timeout = seconds * HZ; |
Björn Stenberg | 6c9e578 | 2002-04-27 14:19:00 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Linus Nielsen Feltzing | ad8ac5c | 2002-07-28 15:16:36 +0000 | [diff] [blame] | 832 | bool ata_disk_is_active(void) |
| 833 | { |
| 834 | return !sleeping; |
| 835 | } |
| 836 | |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 837 | static int ata_perform_sleep(void) |
Björn Stenberg | c4b2850 | 2002-07-16 12:18:17 +0000 | [diff] [blame] | 838 | { |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 839 | mutex_lock(&ata_mtx); |
Linus Nielsen Feltzing | 22e09a3 | 2002-08-27 21:06:48 +0000 | [diff] [blame] | 840 | |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 841 | SET_REG(ATA_SELECT, ata_device); |
Björn Stenberg | c032e65 | 2002-12-03 13:29:35 +0000 | [diff] [blame] | 842 | |
Björn Stenberg | c4b2850 | 2002-07-16 12:18:17 +0000 | [diff] [blame] | 843 | if(!wait_for_rdy()) { |
Linus Nielsen Feltzing | c0a53ea | 2002-09-02 06:26:00 +0000 | [diff] [blame] | 844 | DEBUGF("ata_perform_sleep() - not RDY\n"); |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 845 | mutex_unlock(&ata_mtx); |
Björn Stenberg | c4b2850 | 2002-07-16 12:18:17 +0000 | [diff] [blame] | 846 | return -1; |
| 847 | } |
| 848 | |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 849 | SET_REG(ATA_COMMAND, CMD_SLEEP); |
Björn Stenberg | c4b2850 | 2002-07-16 12:18:17 +0000 | [diff] [blame] | 850 | |
| 851 | if (!wait_for_rdy()) |
Linus Nielsen Feltzing | c0a53ea | 2002-09-02 06:26:00 +0000 | [diff] [blame] | 852 | { |
| 853 | DEBUGF("ata_perform_sleep() - CMD failed\n"); |
Peter D'Hoye | bc092ad | 2008-03-09 13:24:42 +0000 | [diff] [blame] | 854 | mutex_unlock(&ata_mtx); |
| 855 | return -2; |
Linus Nielsen Feltzing | c0a53ea | 2002-09-02 06:26:00 +0000 | [diff] [blame] | 856 | } |
Björn Stenberg | 494d261 | 2002-11-27 15:55:47 +0000 | [diff] [blame] | 857 | |
Björn Stenberg | c4b2850 | 2002-07-16 12:18:17 +0000 | [diff] [blame] | 858 | sleeping = true; |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 859 | mutex_unlock(&ata_mtx); |
Peter D'Hoye | bc092ad | 2008-03-09 13:24:42 +0000 | [diff] [blame] | 860 | return 0; |
Björn Stenberg | c4b2850 | 2002-07-16 12:18:17 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Jens Arnold | a8c1c20 | 2005-11-07 23:19:06 +0000 | [diff] [blame] | 863 | void ata_sleep(void) |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 864 | { |
Michael Sevakis | 4b90267 | 2006-12-19 16:50:07 +0000 | [diff] [blame] | 865 | queue_post(&ata_queue, Q_SLEEP, 0); |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Miika Pekkarinen | 077ed49 | 2006-11-29 12:17:26 +0000 | [diff] [blame] | 868 | void ata_sleepnow(void) |
| 869 | { |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 870 | if (!spinup && !sleeping && !ata_mtx.locked && initialized) |
Miika Pekkarinen | 077ed49 | 2006-11-29 12:17:26 +0000 | [diff] [blame] | 871 | { |
| 872 | call_ata_idle_notifys(false); |
| 873 | ata_perform_sleep(); |
| 874 | } |
| 875 | } |
| 876 | |
Björn Stenberg | 457b8a0 | 2002-08-26 13:21:14 +0000 | [diff] [blame] | 877 | void ata_spin(void) |
| 878 | { |
| 879 | last_user_activity = current_tick; |
| 880 | } |
| 881 | |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 882 | static void ata_thread(void) |
| 883 | { |
Björn Stenberg | 494d261 | 2002-11-27 15:55:47 +0000 | [diff] [blame] | 884 | static long last_sleep = 0; |
Michael Sevakis | a9b2fb5 | 2007-10-16 01:25:17 +0000 | [diff] [blame] | 885 | struct queue_event ev; |
Jonathan Gordon | f184152 | 2006-11-08 01:55:26 +0000 | [diff] [blame] | 886 | static long last_seen_mtx_unlock = 0; |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 887 | |
| 888 | while (1) { |
Michael Sevakis | 9811fc9 | 2008-02-11 07:42:11 +0000 | [diff] [blame] | 889 | queue_wait_w_tmo(&ata_queue, &ev, HZ/2); |
| 890 | |
| 891 | switch ( ev.id ) { |
| 892 | case SYS_TIMEOUT: |
| 893 | if (!spinup && !sleeping) |
Jonathan Gordon | f184152 | 2006-11-08 01:55:26 +0000 | [diff] [blame] | 894 | { |
Michael Sevakis | 9811fc9 | 2008-02-11 07:42:11 +0000 | [diff] [blame] | 895 | if (!ata_mtx.locked) |
Jonathan Gordon | f184152 | 2006-11-08 01:55:26 +0000 | [diff] [blame] | 896 | { |
Michael Sevakis | 9811fc9 | 2008-02-11 07:42:11 +0000 | [diff] [blame] | 897 | if (!last_seen_mtx_unlock) |
| 898 | last_seen_mtx_unlock = current_tick; |
| 899 | if (TIME_AFTER(current_tick, last_seen_mtx_unlock+(HZ*2))) |
| 900 | { |
| 901 | call_ata_idle_notifys(false); |
| 902 | last_seen_mtx_unlock = 0; |
| 903 | } |
| 904 | } |
| 905 | if ( sleep_timeout && |
| 906 | TIME_AFTER( current_tick, |
| 907 | last_user_activity + sleep_timeout ) && |
| 908 | TIME_AFTER( current_tick, |
| 909 | last_disk_activity + sleep_timeout ) ) |
| 910 | { |
| 911 | call_ata_idle_notifys(true); |
| 912 | ata_perform_sleep(); |
| 913 | last_sleep = current_tick; |
Jonathan Gordon | f184152 | 2006-11-08 01:55:26 +0000 | [diff] [blame] | 914 | } |
| 915 | } |
Björn Stenberg | 494d261 | 2002-11-27 15:55:47 +0000 | [diff] [blame] | 916 | |
Michael Sevakis | 9811fc9 | 2008-02-11 07:42:11 +0000 | [diff] [blame] | 917 | #ifdef HAVE_ATA_POWER_OFF |
| 918 | if ( !spinup && sleeping && !poweroff && |
| 919 | TIME_AFTER( current_tick, last_sleep + ATA_POWER_OFF_TIMEOUT )) |
| 920 | { |
| 921 | mutex_lock(&ata_mtx); |
| 922 | ide_power_enable(false); |
| 923 | mutex_unlock(&ata_mtx); |
| 924 | poweroff = true; |
| 925 | } |
| 926 | #endif |
| 927 | break; |
| 928 | |
Jonathan Gordon | f184152 | 2006-11-08 01:55:26 +0000 | [diff] [blame] | 929 | #ifndef USB_NONE |
Jonathan Gordon | fd0e640 | 2006-11-09 07:02:18 +0000 | [diff] [blame] | 930 | case SYS_USB_CONNECTED: |
Björn Stenberg | 2b77b4f | 2002-11-28 22:46:19 +0000 | [diff] [blame] | 931 | if (poweroff) { |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 932 | mutex_lock(&ata_mtx); |
Linus Nielsen Feltzing | 68482bb | 2005-04-04 09:12:12 +0000 | [diff] [blame] | 933 | ata_led(true); |
Björn Stenberg | 494d261 | 2002-11-27 15:55:47 +0000 | [diff] [blame] | 934 | ata_power_on(); |
Linus Nielsen Feltzing | 68482bb | 2005-04-04 09:12:12 +0000 | [diff] [blame] | 935 | ata_led(false); |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 936 | mutex_unlock(&ata_mtx); |
Björn Stenberg | 2b77b4f | 2002-11-28 22:46:19 +0000 | [diff] [blame] | 937 | } |
Björn Stenberg | 494d261 | 2002-11-27 15:55:47 +0000 | [diff] [blame] | 938 | |
Linus Nielsen Feltzing | 2f4b88e | 2002-07-23 15:02:25 +0000 | [diff] [blame] | 939 | /* Tell the USB thread that we are safe */ |
Linus Nielsen Feltzing | 01fd2a0 | 2002-07-27 22:44:52 +0000 | [diff] [blame] | 940 | DEBUGF("ata_thread got SYS_USB_CONNECTED\n"); |
Linus Nielsen Feltzing | 2f4b88e | 2002-07-23 15:02:25 +0000 | [diff] [blame] | 941 | usb_acknowledge(SYS_USB_CONNECTED_ACK); |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 942 | |
Linus Nielsen Feltzing | 2f4b88e | 2002-07-23 15:02:25 +0000 | [diff] [blame] | 943 | /* Wait until the USB cable is extracted again */ |
| 944 | usb_wait_for_disconnect(&ata_queue); |
Jonathan Gordon | f184152 | 2006-11-08 01:55:26 +0000 | [diff] [blame] | 945 | break; |
Jonathan Gordon | fd0e640 | 2006-11-09 07:02:18 +0000 | [diff] [blame] | 946 | #endif |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 947 | case Q_SLEEP: |
Jonathan Gordon | 4049d44 | 2006-11-26 09:53:42 +0000 | [diff] [blame] | 948 | call_ata_idle_notifys(false); |
Björn Stenberg | 8e96c47 | 2002-09-06 23:55:52 +0000 | [diff] [blame] | 949 | last_disk_activity = current_tick - sleep_timeout + (HZ/2); |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 950 | break; |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 951 | |
| 952 | #ifdef ATA_DRIVER_CLOSE |
| 953 | case Q_CLOSE: |
| 954 | return; |
| 955 | #endif |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | } |
| 959 | |
Björn Stenberg | 1ea00d1 | 2002-12-03 11:26:39 +0000 | [diff] [blame] | 960 | /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */ |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 961 | int ata_hard_reset(void) |
| 962 | { |
Linus Nielsen Feltzing | 3922466 | 2002-05-16 21:28:21 +0000 | [diff] [blame] | 963 | int ret; |
Björn Stenberg | 65bf851 | 2004-09-17 11:28:07 +0000 | [diff] [blame] | 964 | |
Michael Sevakis | 35e7b25 | 2008-05-06 10:12:05 +0000 | [diff] [blame] | 965 | mutex_lock(&ata_mtx); |
| 966 | |
Linus Nielsen Feltzing | 838a702 | 2006-03-07 13:25:19 +0000 | [diff] [blame] | 967 | ata_reset(); |
Linus Nielsen Feltzing | dc4977d | 2002-05-07 22:59:03 +0000 | [diff] [blame] | 968 | |
Björn Stenberg | 1ea00d1 | 2002-12-03 11:26:39 +0000 | [diff] [blame] | 969 | /* state HRR2 */ |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 970 | SET_REG(ATA_SELECT, ata_device); /* select the right device */ |
Björn Stenberg | 7249c88 | 2002-12-02 10:30:40 +0000 | [diff] [blame] | 971 | ret = wait_for_bsy(); |
Linus Nielsen Feltzing | 3922466 | 2002-05-16 21:28:21 +0000 | [diff] [blame] | 972 | |
Linus Nielsen Feltzing | 2012e1e | 2002-06-30 13:10:42 +0000 | [diff] [blame] | 973 | /* Massage the return code so it is 0 on success and -1 on failure */ |
| 974 | ret = ret?0:-1; |
| 975 | |
Michael Sevakis | 35e7b25 | 2008-05-06 10:12:05 +0000 | [diff] [blame] | 976 | mutex_unlock(&ata_mtx); |
| 977 | |
Linus Nielsen Feltzing | 3922466 | 2002-05-16 21:28:21 +0000 | [diff] [blame] | 978 | return ret; |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Linus Nielsen Feltzing | 22e09a3 | 2002-08-27 21:06:48 +0000 | [diff] [blame] | 981 | static int perform_soft_reset(void) |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 982 | { |
Thom Johansen | 017b0d7 | 2006-10-25 17:31:29 +0000 | [diff] [blame] | 983 | /* If this code is allowed to run on a Nano, the next reads from the flash will |
| 984 | * time out, so we disable it. It shouldn't be necessary anyway, since the |
| 985 | * ATA -> Flash interface automatically sleeps almost immediately after the |
| 986 | * last command. |
| 987 | */ |
Linus Nielsen Feltzing | 3922466 | 2002-05-16 21:28:21 +0000 | [diff] [blame] | 988 | int ret; |
Linus Nielsen Feltzing | 2012e1e | 2002-06-30 13:10:42 +0000 | [diff] [blame] | 989 | int retry_count; |
Peter D'Hoye | bc092ad | 2008-03-09 13:24:42 +0000 | [diff] [blame] | 990 | |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 991 | SET_REG(ATA_SELECT, SELECT_LBA | ata_device ); |
| 992 | SET_REG(ATA_CONTROL, CONTROL_nIEN|CONTROL_SRST ); |
Björn Stenberg | 1fe97ec | 2003-12-17 20:15:12 +0000 | [diff] [blame] | 993 | sleep(1); /* >= 5us */ |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 994 | |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 995 | SET_REG(ATA_CONTROL, CONTROL_nIEN); |
Björn Stenberg | 1fe97ec | 2003-12-17 20:15:12 +0000 | [diff] [blame] | 996 | sleep(1); /* >2ms */ |
Linus Nielsen Feltzing | 513e851 | 2002-08-02 06:01:22 +0000 | [diff] [blame] | 997 | |
Linus Nielsen Feltzing | 2012e1e | 2002-06-30 13:10:42 +0000 | [diff] [blame] | 998 | /* This little sucker can take up to 30 seconds */ |
| 999 | retry_count = 8; |
| 1000 | do |
| 1001 | { |
Linus Nielsen Feltzing | 2f4b88e | 2002-07-23 15:02:25 +0000 | [diff] [blame] | 1002 | ret = wait_for_rdy(); |
Linus Nielsen Feltzing | 2012e1e | 2002-06-30 13:10:42 +0000 | [diff] [blame] | 1003 | } while(!ret && retry_count--); |
Linus Nielsen Feltzing | 3922466 | 2002-05-16 21:28:21 +0000 | [diff] [blame] | 1004 | |
Peter D'Hoye | bc092ad | 2008-03-09 13:24:42 +0000 | [diff] [blame] | 1005 | if (!ret) |
| 1006 | return -1; |
Björn Stenberg | c4b2850 | 2002-07-16 12:18:17 +0000 | [diff] [blame] | 1007 | |
Peter D'Hoye | bc092ad | 2008-03-09 13:24:42 +0000 | [diff] [blame] | 1008 | if (set_features()) |
| 1009 | return -2; |
| 1010 | |
| 1011 | if (set_multiple_mode(multisectors)) |
| 1012 | return -3; |
| 1013 | |
| 1014 | if (freeze_lock()) |
| 1015 | return -4; |
| 1016 | |
| 1017 | return 0; |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Linus Nielsen Feltzing | 22e09a3 | 2002-08-27 21:06:48 +0000 | [diff] [blame] | 1020 | int ata_soft_reset(void) |
| 1021 | { |
| 1022 | int ret; |
| 1023 | |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 1024 | mutex_lock(&ata_mtx); |
Linus Nielsen Feltzing | 22e09a3 | 2002-08-27 21:06:48 +0000 | [diff] [blame] | 1025 | |
| 1026 | ret = perform_soft_reset(); |
| 1027 | |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 1028 | mutex_unlock(&ata_mtx); |
Linus Nielsen Feltzing | 22e09a3 | 2002-08-27 21:06:48 +0000 | [diff] [blame] | 1029 | return ret; |
| 1030 | } |
| 1031 | |
Linus Nielsen Feltzing | 513e851 | 2002-08-02 06:01:22 +0000 | [diff] [blame] | 1032 | static int ata_power_on(void) |
| 1033 | { |
Linus Nielsen Feltzing | 97955a7 | 2004-02-17 01:31:50 +0000 | [diff] [blame] | 1034 | int rc; |
| 1035 | |
Linus Nielsen Feltzing | 513e851 | 2002-08-02 06:01:22 +0000 | [diff] [blame] | 1036 | ide_power_enable(true); |
Jens Arnold | 8291cd7 | 2008-05-08 21:36:50 +0000 | [diff] [blame] | 1037 | sleep(HZ/4); /* allow voltage to build up */ |
| 1038 | |
| 1039 | /* Accessing the PP IDE controller too early after powering up the disk |
| 1040 | * makes the core hang for a short time, causing an audio dropout. This |
| 1041 | * also depends on the disk; iPod Mini G2 needs at least HZ/5 to get rid |
| 1042 | * of the dropout. Since this time isn't additive (the wait_for_bsy() in |
| 1043 | * ata_hard_reset() will shortened by the same amount), it's a good idea |
| 1044 | * to do this on all HDD based targets. */ |
| 1045 | |
Björn Stenberg | 7249c88 | 2002-12-02 10:30:40 +0000 | [diff] [blame] | 1046 | if( ata_hard_reset() ) |
| 1047 | return -1; |
Linus Nielsen Feltzing | 513e851 | 2002-08-02 06:01:22 +0000 | [diff] [blame] | 1048 | |
Linus Nielsen Feltzing | 867415b | 2004-02-17 01:30:25 +0000 | [diff] [blame] | 1049 | rc = set_features(); |
| 1050 | if (rc) |
| 1051 | return rc * 10 - 2; |
| 1052 | |
Björn Stenberg | 7249c88 | 2002-12-02 10:30:40 +0000 | [diff] [blame] | 1053 | if (set_multiple_mode(multisectors)) |
Linus Nielsen Feltzing | 867415b | 2004-02-17 01:30:25 +0000 | [diff] [blame] | 1054 | return -3; |
Linus Nielsen Feltzing | 513e851 | 2002-08-02 06:01:22 +0000 | [diff] [blame] | 1055 | |
Björn Stenberg | c032e65 | 2002-12-03 13:29:35 +0000 | [diff] [blame] | 1056 | if (freeze_lock()) |
Linus Nielsen Feltzing | 867415b | 2004-02-17 01:30:25 +0000 | [diff] [blame] | 1057 | return -4; |
Björn Stenberg | c032e65 | 2002-12-03 13:29:35 +0000 | [diff] [blame] | 1058 | |
Björn Stenberg | 7249c88 | 2002-12-02 10:30:40 +0000 | [diff] [blame] | 1059 | return 0; |
Linus Nielsen Feltzing | 513e851 | 2002-08-02 06:01:22 +0000 | [diff] [blame] | 1060 | } |
Linus Nielsen Feltzing | 513e851 | 2002-08-02 06:01:22 +0000 | [diff] [blame] | 1061 | |
Linus Nielsen Feltzing | 8169c8f | 2002-06-12 13:51:31 +0000 | [diff] [blame] | 1062 | static int master_slave_detect(void) |
Björn Stenberg | 5c7847c | 2002-05-22 14:37:36 +0000 | [diff] [blame] | 1063 | { |
| 1064 | /* master? */ |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 1065 | SET_REG(ATA_SELECT, 0); |
Björn Stenberg | 9f372f1 | 2003-07-09 22:04:31 +0000 | [diff] [blame] | 1066 | if ( ATA_STATUS & (STATUS_RDY|STATUS_BSY) ) { |
Linus Nielsen Feltzing | 1639e98 | 2002-07-02 14:23:30 +0000 | [diff] [blame] | 1067 | ata_device = 0; |
Björn Stenberg | 5c7847c | 2002-05-22 14:37:36 +0000 | [diff] [blame] | 1068 | DEBUGF("Found master harddisk\n"); |
| 1069 | } |
| 1070 | else { |
| 1071 | /* slave? */ |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 1072 | SET_REG(ATA_SELECT, SELECT_DEVICE1); |
Björn Stenberg | 9f372f1 | 2003-07-09 22:04:31 +0000 | [diff] [blame] | 1073 | if ( ATA_STATUS & (STATUS_RDY|STATUS_BSY) ) { |
Linus Nielsen Feltzing | 1639e98 | 2002-07-02 14:23:30 +0000 | [diff] [blame] | 1074 | ata_device = SELECT_DEVICE1; |
Björn Stenberg | 5c7847c | 2002-05-22 14:37:36 +0000 | [diff] [blame] | 1075 | DEBUGF("Found slave harddisk\n"); |
| 1076 | } |
| 1077 | else |
| 1078 | return -1; |
| 1079 | } |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
Björn Stenberg | 8ccbc76 | 2002-09-05 15:25:08 +0000 | [diff] [blame] | 1083 | static int identify(void) |
| 1084 | { |
| 1085 | int i; |
| 1086 | |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 1087 | SET_REG(ATA_SELECT, ata_device); |
Björn Stenberg | c032e65 | 2002-12-03 13:29:35 +0000 | [diff] [blame] | 1088 | |
Björn Stenberg | 8ccbc76 | 2002-09-05 15:25:08 +0000 | [diff] [blame] | 1089 | if(!wait_for_rdy()) { |
| 1090 | DEBUGF("identify() - not RDY\n"); |
| 1091 | return -1; |
| 1092 | } |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 1093 | SET_REG(ATA_COMMAND, CMD_IDENTIFY); |
Björn Stenberg | 8ccbc76 | 2002-09-05 15:25:08 +0000 | [diff] [blame] | 1094 | |
| 1095 | if (!wait_for_start_of_transfer()) |
| 1096 | { |
| 1097 | DEBUGF("identify() - CMD failed\n"); |
| 1098 | return -2; |
| 1099 | } |
| 1100 | |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 1101 | for (i=0; i<SECTOR_SIZE/2; i++) { |
| 1102 | /* the IDENTIFY words are already swapped, so we need to treat |
| 1103 | this info differently that normal sector data */ |
Miika Pekkarinen | f6c8058 | 2005-11-08 11:41:56 +0000 | [diff] [blame] | 1104 | #if defined(ROCKBOX_BIG_ENDIAN) && !defined(SWAP_WORDS) |
Dave Chapman | 77372d1 | 2005-11-07 23:07:19 +0000 | [diff] [blame] | 1105 | identify_info[i] = swap16(ATA_DATA); |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 1106 | #else |
Dave Chapman | 77372d1 | 2005-11-07 23:07:19 +0000 | [diff] [blame] | 1107 | identify_info[i] = ATA_DATA; |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 1108 | #endif |
| 1109 | } |
Michael Sevakis | be0c7d0 | 2008-05-05 10:53:06 +0000 | [diff] [blame] | 1110 | |
Björn Stenberg | 8ccbc76 | 2002-09-05 15:25:08 +0000 | [diff] [blame] | 1111 | return 0; |
| 1112 | } |
| 1113 | |
Björn Stenberg | 34fa70e | 2002-09-06 17:20:44 +0000 | [diff] [blame] | 1114 | static int set_multiple_mode(int sectors) |
| 1115 | { |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 1116 | SET_REG(ATA_SELECT, ata_device); |
Björn Stenberg | c032e65 | 2002-12-03 13:29:35 +0000 | [diff] [blame] | 1117 | |
Björn Stenberg | 34fa70e | 2002-09-06 17:20:44 +0000 | [diff] [blame] | 1118 | if(!wait_for_rdy()) { |
| 1119 | DEBUGF("set_multiple_mode() - not RDY\n"); |
| 1120 | return -1; |
| 1121 | } |
| 1122 | |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 1123 | SET_REG(ATA_NSECTOR, sectors); |
| 1124 | SET_REG(ATA_COMMAND, CMD_SET_MULTIPLE_MODE); |
Björn Stenberg | 34fa70e | 2002-09-06 17:20:44 +0000 | [diff] [blame] | 1125 | |
| 1126 | if (!wait_for_rdy()) |
| 1127 | { |
| 1128 | DEBUGF("set_multiple_mode() - CMD failed\n"); |
| 1129 | return -2; |
| 1130 | } |
| 1131 | |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
Björn Stenberg | a53afc0 | 2004-01-14 13:15:19 +0000 | [diff] [blame] | 1135 | static int set_features(void) |
| 1136 | { |
Jens Arnold | b85e1b9 | 2007-02-18 19:28:29 +0000 | [diff] [blame] | 1137 | static struct { |
Björn Stenberg | a53afc0 | 2004-01-14 13:15:19 +0000 | [diff] [blame] | 1138 | unsigned char id_word; |
| 1139 | unsigned char id_bit; |
| 1140 | unsigned char subcommand; |
| 1141 | unsigned char parameter; |
| 1142 | } features[] = { |
Peter D'Hoye | bc092ad | 2008-03-09 13:24:42 +0000 | [diff] [blame] | 1143 | { 83, 14, 0x03, 0 }, /* force PIO mode */ |
| 1144 | { 83, 3, 0x05, 0x80 }, /* adv. power management: lowest w/o standby */ |
Björn Stenberg | a53afc0 | 2004-01-14 13:15:19 +0000 | [diff] [blame] | 1145 | { 83, 9, 0x42, 0x80 }, /* acoustic management: lowest noise */ |
| 1146 | { 82, 6, 0xaa, 0 }, /* enable read look-ahead */ |
Björn Stenberg | a53afc0 | 2004-01-14 13:15:19 +0000 | [diff] [blame] | 1147 | }; |
| 1148 | int i; |
Linus Nielsen Feltzing | 68331ff | 2004-03-02 09:55:23 +0000 | [diff] [blame] | 1149 | int pio_mode = 2; |
Björn Stenberg | a53afc0 | 2004-01-14 13:15:19 +0000 | [diff] [blame] | 1150 | |
Linus Nielsen Feltzing | 68331ff | 2004-03-02 09:55:23 +0000 | [diff] [blame] | 1151 | /* Find out the highest supported PIO mode */ |
| 1152 | if(identify_info[64] & 2) |
| 1153 | pio_mode = 4; |
| 1154 | else |
| 1155 | if(identify_info[64] & 1) |
| 1156 | pio_mode = 3; |
| 1157 | |
Peter D'Hoye | bc092ad | 2008-03-09 13:24:42 +0000 | [diff] [blame] | 1158 | /* Update the table: set highest supported pio mode that we also support */ |
| 1159 | features[0].parameter = 8 + pio_mode; |
| 1160 | |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 1161 | SET_REG(ATA_SELECT, ata_device); |
Björn Stenberg | a53afc0 | 2004-01-14 13:15:19 +0000 | [diff] [blame] | 1162 | |
| 1163 | if (!wait_for_rdy()) { |
| 1164 | DEBUGF("set_features() - not RDY\n"); |
| 1165 | return -1; |
| 1166 | } |
| 1167 | |
Jens Arnold | b85e1b9 | 2007-02-18 19:28:29 +0000 | [diff] [blame] | 1168 | for (i=0; i < (int)(sizeof(features)/sizeof(features[0])); i++) { |
Björn Stenberg | a53afc0 | 2004-01-14 13:15:19 +0000 | [diff] [blame] | 1169 | if (identify_info[features[i].id_word] & (1 << features[i].id_bit)) { |
Linus Nielsen Feltzing | 4058b79 | 2005-01-26 12:53:48 +0000 | [diff] [blame] | 1170 | SET_REG(ATA_FEATURE, features[i].subcommand); |
| 1171 | SET_REG(ATA_NSECTOR, features[i].parameter); |
| 1172 | SET_REG(ATA_COMMAND, CMD_SET_FEATURES); |
Björn Stenberg | a53afc0 | 2004-01-14 13:15:19 +0000 | [diff] [blame] | 1173 | |
| 1174 | if (!wait_for_rdy()) { |
| 1175 | DEBUGF("set_features() - CMD failed\n"); |
Linus Nielsen Feltzing | 68331ff | 2004-03-02 09:55:23 +0000 | [diff] [blame] | 1176 | return -10 - i; |
| 1177 | } |
| 1178 | |
Peter D'Hoye | bc092ad | 2008-03-09 13:24:42 +0000 | [diff] [blame] | 1179 | if((ATA_ALT_STATUS & STATUS_ERR) && (i != 1)) { |
| 1180 | /* some CF cards don't like advanced powermanagement |
| 1181 | even if they mark it as supported - go figure... */ |
Linus Nielsen Feltzing | 68331ff | 2004-03-02 09:55:23 +0000 | [diff] [blame] | 1182 | if(ATA_ERROR & ERROR_ABRT) { |
| 1183 | return -20 - i; |
| 1184 | } |
Björn Stenberg | a53afc0 | 2004-01-14 13:15:19 +0000 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | } |
| 1188 | |
Michael Sevakis | 57cbd77 | 2008-05-05 13:00:08 +0000 | [diff] [blame] | 1189 | #ifdef ATA_SET_DEVICE_FEATURES |
| 1190 | ata_set_pio_timings(pio_mode); |
| 1191 | #endif |
| 1192 | |
Björn Stenberg | a53afc0 | 2004-01-14 13:15:19 +0000 | [diff] [blame] | 1193 | return 0; |
| 1194 | } |
| 1195 | |
Björn Stenberg | 45d32ce | 2002-12-03 13:12:55 +0000 | [diff] [blame] | 1196 | unsigned short* ata_get_identify(void) |
| 1197 | { |
| 1198 | return identify_info; |
| 1199 | } |
| 1200 | |
Jens Arnold | f05dec5 | 2004-11-23 22:00:41 +0000 | [diff] [blame] | 1201 | static int init_and_check(bool hard_reset) |
| 1202 | { |
| 1203 | int rc; |
| 1204 | |
| 1205 | if (hard_reset) |
| 1206 | { |
| 1207 | /* This should reset both master and slave, we don't yet know what's in */ |
| 1208 | ata_device = 0; |
| 1209 | if (ata_hard_reset()) |
| 1210 | return -1; |
| 1211 | } |
| 1212 | |
| 1213 | rc = master_slave_detect(); |
| 1214 | if (rc) |
| 1215 | return -10 + rc; |
| 1216 | |
| 1217 | /* symptom fix: else check_registers() below may fail */ |
| 1218 | if (hard_reset && !wait_for_bsy()) |
| 1219 | return -20; |
| 1220 | |
| 1221 | rc = check_registers(); |
| 1222 | if (rc) |
| 1223 | return -30 + rc; |
| 1224 | |
| 1225 | return 0; |
| 1226 | } |
| 1227 | |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 1228 | int ata_init(void) |
| 1229 | { |
Michael Sevakis | 3b36b98 | 2008-01-18 12:32:03 +0000 | [diff] [blame] | 1230 | int rc = 0; |
| 1231 | bool coldstart; |
Jörg Hohensohn | bbfaf26 | 2003-07-09 07:18:47 +0000 | [diff] [blame] | 1232 | |
Michael Sevakis | 3b36b98 | 2008-01-18 12:32:03 +0000 | [diff] [blame] | 1233 | if ( !initialized ) { |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 1234 | mutex_init(&ata_mtx); |
Michael Sevakis | 3b36b98 | 2008-01-18 12:32:03 +0000 | [diff] [blame] | 1235 | queue_init(&ata_queue, true); |
| 1236 | } |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 1237 | |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 1238 | mutex_lock(&ata_mtx); |
Michael Sevakis | 3b36b98 | 2008-01-18 12:32:03 +0000 | [diff] [blame] | 1239 | |
| 1240 | /* must be called before ata_device_init() */ |
| 1241 | coldstart = ata_is_coldstart(); |
Linus Nielsen Feltzing | 68482bb | 2005-04-04 09:12:12 +0000 | [diff] [blame] | 1242 | ata_led(false); |
Linus Nielsen Feltzing | 838a702 | 2006-03-07 13:25:19 +0000 | [diff] [blame] | 1243 | ata_device_init(); |
Jörg Hohensohn | f4b677e | 2003-07-18 21:55:06 +0000 | [diff] [blame] | 1244 | sleeping = false; |
Linus Nielsen Feltzing | 2012e1e | 2002-06-30 13:10:42 +0000 | [diff] [blame] | 1245 | ata_enable(true); |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 1246 | #ifdef MAX_PHYS_SECTOR_SIZE |
| 1247 | memset(§or_cache, 0, sizeof(sector_cache)); |
| 1248 | #endif |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 1249 | |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 1250 | if ( !initialized ) { |
Michael Sevakis | 9811fc9 | 2008-02-11 07:42:11 +0000 | [diff] [blame] | 1251 | /* First call won't have multiple thread contention - this |
| 1252 | * may return at any point without having to unlock */ |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 1253 | mutex_unlock(&ata_mtx); |
Michael Sevakis | 3b36b98 | 2008-01-18 12:32:03 +0000 | [diff] [blame] | 1254 | |
Jörg Hohensohn | 4429816 | 2003-12-03 01:03:54 +0000 | [diff] [blame] | 1255 | if (!ide_powered()) /* somebody has switched it off */ |
| 1256 | { |
| 1257 | ide_power_enable(true); |
Jens Arnold | 8291cd7 | 2008-05-08 21:36:50 +0000 | [diff] [blame] | 1258 | sleep(HZ/4); /* allow voltage to build up */ |
Jörg Hohensohn | 4429816 | 2003-12-03 01:03:54 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Jens Arnold | f05dec5 | 2004-11-23 22:00:41 +0000 | [diff] [blame] | 1261 | /* first try, hard reset at cold start only */ |
Jens Arnold | 28fd4b7 | 2006-12-19 09:27:41 +0000 | [diff] [blame] | 1262 | rc = init_and_check(coldstart); |
Björn Stenberg | 8ccbc76 | 2002-09-05 15:25:08 +0000 | [diff] [blame] | 1263 | |
Jens Arnold | 28fd4b7 | 2006-12-19 09:27:41 +0000 | [diff] [blame] | 1264 | if (rc) |
Jens Arnold | f05dec5 | 2004-11-23 22:00:41 +0000 | [diff] [blame] | 1265 | { /* failed? -> second try, always with hard reset */ |
| 1266 | DEBUGF("ata: init failed, retrying...\n"); |
| 1267 | rc = init_and_check(true); |
| 1268 | if (rc) |
| 1269 | return rc; |
Jörg Hohensohn | bbfaf26 | 2003-07-09 07:18:47 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Björn Stenberg | efd2f35 | 2003-07-09 16:46:46 +0000 | [diff] [blame] | 1272 | rc = identify(); |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 1273 | |
Björn Stenberg | efd2f35 | 2003-07-09 16:46:46 +0000 | [diff] [blame] | 1274 | if (rc) |
Björn Stenberg | 65bf851 | 2004-09-17 11:28:07 +0000 | [diff] [blame] | 1275 | return -40 + rc; |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 1276 | |
Björn Stenberg | 8ccbc76 | 2002-09-05 15:25:08 +0000 | [diff] [blame] | 1277 | multisectors = identify_info[47] & 0xff; |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 1278 | if (multisectors == 0) /* Invalid multisector info, try with 16 */ |
| 1279 | multisectors = 16; |
| 1280 | |
Björn Stenberg | 8ccbc76 | 2002-09-05 15:25:08 +0000 | [diff] [blame] | 1281 | DEBUGF("ata: %d sectors per ata request\n",multisectors); |
Jean-Philippe Bernardy | 99e72c8 | 2005-01-20 23:00:11 +0000 | [diff] [blame] | 1282 | |
Jens Arnold | c5d71aa | 2007-05-23 17:51:18 +0000 | [diff] [blame] | 1283 | #ifdef MAX_PHYS_SECTOR_SIZE |
| 1284 | /* Find out the physical sector size */ |
| 1285 | if((identify_info[106] & 0xe000) == 0x6000) |
| 1286 | phys_sector_mult = 1 << (identify_info[106] & 0x000f); |
| 1287 | else |
| 1288 | phys_sector_mult = 1; |
| 1289 | |
| 1290 | DEBUGF("ata: %d logical sectors per phys sector", phys_sector_mult); |
| 1291 | |
| 1292 | if (phys_sector_mult > (MAX_PHYS_SECTOR_SIZE/SECTOR_SIZE)) |
| 1293 | panicf("Unsupported physical sector size: %d", |
| 1294 | phys_sector_mult * SECTOR_SIZE); |
| 1295 | #endif |
| 1296 | |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 1297 | total_sectors = identify_info[60] | (identify_info[61] << 16); |
| 1298 | |
Jörg Hohensohn | bb03586 | 2006-12-20 22:08:29 +0000 | [diff] [blame] | 1299 | #ifdef HAVE_LBA48 |
Jens Arnold | 88e2053 | 2008-03-09 00:59:48 +0000 | [diff] [blame] | 1300 | if (identify_info[83] & 0x0400 /* 48 bit address support */ |
| 1301 | && total_sectors == 0x0FFFFFFF) /* and disk size >= 128 GiB */ |
| 1302 | { /* (needs BigLBA addressing) */ |
| 1303 | if (identify_info[102] || identify_info[103]) |
| 1304 | panicf("Unsupported disk size: >= 2^32 sectors"); |
| 1305 | |
| 1306 | total_sectors = identify_info[100] | (identify_info[101] << 16); |
Jörg Hohensohn | bb03586 | 2006-12-20 22:08:29 +0000 | [diff] [blame] | 1307 | lba48 = true; /* use BigLBA */ |
Jörg Hohensohn | f5bb766 | 2006-12-19 22:40:23 +0000 | [diff] [blame] | 1308 | } |
| 1309 | #endif |
Björn Stenberg | 65bf851 | 2004-09-17 11:28:07 +0000 | [diff] [blame] | 1310 | rc = freeze_lock(); |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 1311 | |
Björn Stenberg | 65bf851 | 2004-09-17 11:28:07 +0000 | [diff] [blame] | 1312 | if (rc) |
| 1313 | return -50 + rc; |
| 1314 | |
Björn Stenberg | a53afc0 | 2004-01-14 13:15:19 +0000 | [diff] [blame] | 1315 | rc = set_features(); |
| 1316 | if (rc) |
| 1317 | return -60 + rc; |
| 1318 | |
Michael Sevakis | 9811fc9 | 2008-02-11 07:42:11 +0000 | [diff] [blame] | 1319 | mutex_lock(&ata_mtx); /* Balance unlock below */ |
| 1320 | |
Björn Stenberg | 9cb5e0e | 2003-07-11 19:11:06 +0000 | [diff] [blame] | 1321 | last_disk_activity = current_tick; |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 1322 | #ifdef ATA_DRIVER_CLOSE |
| 1323 | ata_thread_p = |
| 1324 | #endif |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 1325 | create_thread(ata_thread, ata_stack, |
Michael Sevakis | a9b2fb5 | 2007-10-16 01:25:17 +0000 | [diff] [blame] | 1326 | sizeof(ata_stack), 0, ata_thread_name |
Michael Sevakis | 9811fc9 | 2008-02-11 07:42:11 +0000 | [diff] [blame] | 1327 | IF_PRIO(, PRIORITY_USER_INTERFACE) |
Michael Sevakis | a9b2fb5 | 2007-10-16 01:25:17 +0000 | [diff] [blame] | 1328 | IF_COP(, CPU)); |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 1329 | initialized = true; |
Jean-Philippe Bernardy | 99e72c8 | 2005-01-20 23:00:11 +0000 | [diff] [blame] | 1330 | |
Björn Stenberg | 11968a4 | 2002-07-16 14:07:47 +0000 | [diff] [blame] | 1331 | } |
Björn Stenberg | efd2f35 | 2003-07-09 16:46:46 +0000 | [diff] [blame] | 1332 | rc = set_multiple_mode(multisectors); |
| 1333 | if (rc) |
Michael Sevakis | 3b36b98 | 2008-01-18 12:32:03 +0000 | [diff] [blame] | 1334 | rc = -70 + rc; |
Björn Stenberg | 5c7847c | 2002-05-22 14:37:36 +0000 | [diff] [blame] | 1335 | |
Michael Sevakis | 6a83796 | 2008-01-18 13:12:33 +0000 | [diff] [blame] | 1336 | mutex_unlock(&ata_mtx); |
Michael Sevakis | 3b36b98 | 2008-01-18 12:32:03 +0000 | [diff] [blame] | 1337 | return rc; |
Björn Stenberg | 1acfd6b | 2002-04-21 22:06:12 +0000 | [diff] [blame] | 1338 | } |
Linus Nielsen Feltzing | 68482bb | 2005-04-04 09:12:12 +0000 | [diff] [blame] | 1339 | |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 1340 | #ifdef ATA_DRIVER_CLOSE |
| 1341 | void ata_close(void) |
| 1342 | { |
| 1343 | struct thread_entry *thread = ata_thread_p; |
| 1344 | |
| 1345 | if (thread == NULL) |
| 1346 | return; |
| 1347 | |
| 1348 | ata_thread_p = NULL; |
| 1349 | |
| 1350 | queue_post(&ata_queue, Q_CLOSE, 0); |
| 1351 | thread_wait(thread); |
| 1352 | } |
| 1353 | #endif /* ATA_DRIVER_CLOSE */ |
| 1354 | |
Jonathan Gordon | bd47d48 | 2007-02-18 05:07:19 +0000 | [diff] [blame] | 1355 | #if (CONFIG_LED == LED_REAL) |
Jens Arnold | 31ffd7b | 2006-12-03 22:13:44 +0000 | [diff] [blame] | 1356 | void ata_set_led_enabled(bool enabled) |
| 1357 | { |
Linus Nielsen Feltzing | 68482bb | 2005-04-04 09:12:12 +0000 | [diff] [blame] | 1358 | ata_led_enabled = enabled; |
Jens Arnold | 31ffd7b | 2006-12-03 22:13:44 +0000 | [diff] [blame] | 1359 | if (ata_led_enabled) |
Linus Nielsen Feltzing | 68482bb | 2005-04-04 09:12:12 +0000 | [diff] [blame] | 1360 | led(ata_led_on); |
Jens Arnold | 31ffd7b | 2006-12-03 22:13:44 +0000 | [diff] [blame] | 1361 | else |
Linus Nielsen Feltzing | 68482bb | 2005-04-04 09:12:12 +0000 | [diff] [blame] | 1362 | led(false); |
Linus Nielsen Feltzing | 68482bb | 2005-04-04 09:12:12 +0000 | [diff] [blame] | 1363 | } |
Jens Arnold | 5690f78 | 2005-06-04 23:15:52 +0000 | [diff] [blame] | 1364 | #endif |