wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 2 | * (C) Copyright 2000-2011 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * IDE support |
| 27 | */ |
Albert Aribaud | 113bfe4 | 2010-08-08 05:17:06 +0530 | [diff] [blame] | 28 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 29 | #include <common.h> |
| 30 | #include <config.h> |
| 31 | #include <watchdog.h> |
| 32 | #include <command.h> |
| 33 | #include <image.h> |
| 34 | #include <asm/byteorder.h> |
Heiko Schocher | f98984c | 2007-08-28 17:39:14 +0200 | [diff] [blame] | 35 | #include <asm/io.h> |
Grant Likely | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 36 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 37 | #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA) |
| 38 | # include <pcmcia.h> |
| 39 | #endif |
Grant Likely | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 40 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 41 | #include <ide.h> |
| 42 | #include <ata.h> |
Grant Likely | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 43 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 44 | #ifdef CONFIG_STATUS_LED |
| 45 | # include <status_led.h> |
| 46 | #endif |
Grant Likely | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 47 | |
wdenk | 5cf91d6 | 2004-04-23 20:32:05 +0000 | [diff] [blame] | 48 | #ifdef __PPC__ |
| 49 | # define EIEIO __asm__ volatile ("eieio") |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 50 | # define SYNC __asm__ volatile ("sync") |
wdenk | 5cf91d6 | 2004-04-23 20:32:05 +0000 | [diff] [blame] | 51 | #else |
| 52 | # define EIEIO /* nothing */ |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 53 | # define SYNC /* nothing */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 54 | #endif |
| 55 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 56 | /* ------------------------------------------------------------------------- */ |
| 57 | |
| 58 | /* Current I/O Device */ |
| 59 | static int curr_device = -1; |
| 60 | |
| 61 | /* Current offset for IDE0 / IDE1 bus access */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 62 | ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = { |
| 63 | #if defined(CONFIG_SYS_ATA_IDE0_OFFSET) |
| 64 | CONFIG_SYS_ATA_IDE0_OFFSET, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 65 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 66 | #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1) |
| 67 | CONFIG_SYS_ATA_IDE1_OFFSET, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 68 | #endif |
| 69 | }; |
| 70 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 71 | static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 72 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 73 | block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 74 | /* ------------------------------------------------------------------------- */ |
| 75 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 76 | #ifdef CONFIG_IDE_RESET |
| 77 | static void ide_reset (void); |
| 78 | #else |
| 79 | #define ide_reset() /* dummy */ |
| 80 | #endif |
| 81 | |
| 82 | static void ide_ident (block_dev_desc_t *dev_desc); |
| 83 | static uchar ide_wait (int dev, ulong t); |
| 84 | |
| 85 | #define IDE_TIME_OUT 2000 /* 2 sec timeout */ |
| 86 | |
| 87 | #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */ |
| 88 | |
| 89 | #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */ |
| 90 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 91 | static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len); |
| 92 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 93 | #ifndef CONFIG_SYS_ATA_PORT_ADDR |
| 94 | #define CONFIG_SYS_ATA_PORT_ADDR(port) (port) |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 95 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 96 | |
| 97 | #ifdef CONFIG_ATAPI |
| 98 | static void atapi_inquiry(block_dev_desc_t *dev_desc); |
Grant Likely | eb867a7 | 2007-02-20 09:05:45 +0100 | [diff] [blame] | 99 | ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 100 | #endif |
| 101 | |
| 102 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 103 | /* ------------------------------------------------------------------------- */ |
| 104 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 105 | int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 106 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 107 | int rcode = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 108 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 109 | switch (argc) { |
| 110 | case 0: |
| 111 | case 1: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 112 | return CMD_RET_USAGE; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 113 | case 2: |
| 114 | if (strncmp(argv[1], "res", 3) == 0) { |
| 115 | puts("\nReset IDE" |
| 116 | #ifdef CONFIG_IDE_8xx_DIRECT |
| 117 | " on PCMCIA " PCMCIA_SLOT_MSG |
| 118 | #endif |
| 119 | ": "); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 120 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 121 | ide_init(); |
| 122 | return 0; |
| 123 | } else if (strncmp(argv[1], "inf", 3) == 0) { |
| 124 | int i; |
| 125 | |
| 126 | putc('\n'); |
| 127 | |
| 128 | for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) { |
| 129 | if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN) |
| 130 | continue; /* list only known devices */ |
| 131 | printf("IDE device %d: ", i); |
| 132 | dev_print(&ide_dev_desc[i]); |
| 133 | } |
| 134 | return 0; |
| 135 | |
| 136 | } else if (strncmp(argv[1], "dev", 3) == 0) { |
| 137 | if ((curr_device < 0) |
| 138 | || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) { |
| 139 | puts("\nno IDE devices available\n"); |
| 140 | return 1; |
| 141 | } |
| 142 | printf("\nIDE device %d: ", curr_device); |
| 143 | dev_print(&ide_dev_desc[curr_device]); |
| 144 | return 0; |
| 145 | } else if (strncmp(argv[1], "part", 4) == 0) { |
| 146 | int dev, ok; |
| 147 | |
| 148 | for (ok = 0, dev = 0; |
| 149 | dev < CONFIG_SYS_IDE_MAXDEVICE; |
| 150 | ++dev) { |
| 151 | if (ide_dev_desc[dev].part_type != |
| 152 | PART_TYPE_UNKNOWN) { |
| 153 | ++ok; |
| 154 | if (dev) |
| 155 | putc('\n'); |
| 156 | print_part(&ide_dev_desc[dev]); |
| 157 | } |
| 158 | } |
| 159 | if (!ok) { |
| 160 | puts("\nno IDE devices available\n"); |
| 161 | rcode++; |
| 162 | } |
| 163 | return rcode; |
| 164 | } |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 165 | return CMD_RET_USAGE; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 166 | case 3: |
| 167 | if (strncmp(argv[1], "dev", 3) == 0) { |
| 168 | int dev = (int) simple_strtoul(argv[2], NULL, 10); |
| 169 | |
| 170 | printf("\nIDE device %d: ", dev); |
| 171 | if (dev >= CONFIG_SYS_IDE_MAXDEVICE) { |
| 172 | puts("unknown device\n"); |
| 173 | return 1; |
| 174 | } |
| 175 | dev_print(&ide_dev_desc[dev]); |
| 176 | /*ide_print (dev); */ |
| 177 | |
| 178 | if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) |
| 179 | return 1; |
| 180 | |
| 181 | curr_device = dev; |
| 182 | |
| 183 | puts("... is now current device\n"); |
| 184 | |
| 185 | return 0; |
| 186 | } else if (strncmp(argv[1], "part", 4) == 0) { |
| 187 | int dev = (int) simple_strtoul(argv[2], NULL, 10); |
| 188 | |
| 189 | if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) { |
| 190 | print_part(&ide_dev_desc[dev]); |
| 191 | } else { |
| 192 | printf("\nIDE device %d not available\n", |
| 193 | dev); |
| 194 | rcode = 1; |
| 195 | } |
| 196 | return rcode; |
| 197 | } |
| 198 | |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 199 | return CMD_RET_USAGE; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 200 | default: |
| 201 | /* at least 4 args */ |
| 202 | |
| 203 | if (strcmp(argv[1], "read") == 0) { |
| 204 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 205 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 206 | ulong n; |
| 207 | |
| 208 | #ifdef CONFIG_SYS_64BIT_LBA |
| 209 | lbaint_t blk = simple_strtoull(argv[3], NULL, 16); |
| 210 | |
| 211 | printf("\nIDE read: device %d block # %lld, count %ld ... ", |
| 212 | curr_device, blk, cnt); |
| 213 | #else |
| 214 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); |
| 215 | |
| 216 | printf("\nIDE read: device %d block # %ld, count %ld ... ", |
| 217 | curr_device, blk, cnt); |
| 218 | #endif |
| 219 | |
| 220 | n = ide_dev_desc[curr_device].block_read(curr_device, |
| 221 | blk, cnt, |
| 222 | (ulong *)addr); |
| 223 | /* flush cache after read */ |
| 224 | flush_cache(addr, |
| 225 | cnt * ide_dev_desc[curr_device].blksz); |
| 226 | |
| 227 | printf("%ld blocks read: %s\n", |
| 228 | n, (n == cnt) ? "OK" : "ERROR"); |
| 229 | if (n == cnt) |
| 230 | return 0; |
| 231 | else |
| 232 | return 1; |
| 233 | } else if (strcmp(argv[1], "write") == 0) { |
| 234 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 235 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 236 | ulong n; |
| 237 | |
| 238 | #ifdef CONFIG_SYS_64BIT_LBA |
| 239 | lbaint_t blk = simple_strtoull(argv[3], NULL, 16); |
| 240 | |
| 241 | printf("\nIDE write: device %d block # %lld, count %ld ... ", |
| 242 | curr_device, blk, cnt); |
| 243 | #else |
| 244 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); |
| 245 | |
| 246 | printf("\nIDE write: device %d block # %ld, count %ld ... ", |
| 247 | curr_device, blk, cnt); |
| 248 | #endif |
| 249 | n = ide_write(curr_device, blk, cnt, (ulong *) addr); |
| 250 | |
| 251 | printf("%ld blocks written: %s\n", |
| 252 | n, (n == cnt) ? "OK" : "ERROR"); |
| 253 | if (n == cnt) |
| 254 | return 0; |
| 255 | else |
| 256 | return 1; |
| 257 | } else { |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 258 | return CMD_RET_USAGE; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return rcode; |
| 262 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 265 | int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 266 | { |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 267 | return common_diskboot(cmdtp, "ide", argc, argv); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* ------------------------------------------------------------------------- */ |
| 271 | |
Pavel Herrmann | 19be2ea | 2012-10-07 05:56:10 +0000 | [diff] [blame] | 272 | void __ide_led(uchar led, uchar status) |
| 273 | { |
| 274 | #if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */ |
| 275 | static uchar led_buffer; /* Buffer for current LED status */ |
| 276 | |
| 277 | uchar *led_port = LED_PORT; |
| 278 | |
| 279 | if (status) /* switch LED on */ |
| 280 | led_buffer |= led; |
| 281 | else /* switch LED off */ |
| 282 | led_buffer &= ~led; |
| 283 | |
| 284 | *led_port = led_buffer; |
| 285 | #endif |
| 286 | } |
| 287 | |
| 288 | void ide_led(uchar led, uchar status) |
| 289 | __attribute__ ((weak, alias("__ide_led"))); |
| 290 | |
| 291 | #ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */ |
| 292 | # define DEVICE_LED(x) 0 |
| 293 | # define LED_IDE1 1 |
| 294 | # define LED_IDE2 2 |
| 295 | #endif |
| 296 | |
| 297 | /* ------------------------------------------------------------------------- */ |
| 298 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 299 | inline void __ide_outb(int dev, int port, unsigned char val) |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 300 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 301 | debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n", |
| 302 | dev, port, val, |
| 303 | (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port))); |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 304 | |
| 305 | #if defined(CONFIG_IDE_AHB) |
| 306 | if (port) { |
| 307 | /* write command */ |
| 308 | ide_write_register(dev, port, val); |
| 309 | } else { |
| 310 | /* write data */ |
| 311 | outb(val, (ATA_CURR_BASE(dev))); |
| 312 | } |
| 313 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 314 | outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port))); |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 315 | #endif |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 316 | } |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 317 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 318 | void ide_outb(int dev, int port, unsigned char val) |
| 319 | __attribute__ ((weak, alias("__ide_outb"))); |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 320 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 321 | inline unsigned char __ide_inb(int dev, int port) |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 322 | { |
| 323 | uchar val; |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 324 | |
| 325 | #if defined(CONFIG_IDE_AHB) |
| 326 | val = ide_read_register(dev, port); |
| 327 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 328 | val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port))); |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 329 | #endif |
| 330 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 331 | debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n", |
| 332 | dev, port, |
| 333 | (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val); |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 334 | return val; |
| 335 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 336 | |
Kim Phillips | 2df72b8 | 2009-05-19 12:53:36 -0500 | [diff] [blame] | 337 | unsigned char ide_inb(int dev, int port) |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 338 | __attribute__ ((weak, alias("__ide_inb"))); |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 339 | |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 340 | #ifdef CONFIG_TUNE_PIO |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 341 | inline int __ide_set_piomode(int pio_mode) |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 342 | { |
| 343 | return 0; |
| 344 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 345 | |
| 346 | inline int ide_set_piomode(int pio_mode) |
| 347 | __attribute__ ((weak, alias("__ide_set_piomode"))); |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 348 | #endif |
| 349 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 350 | void ide_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 351 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 352 | unsigned char c; |
| 353 | int i, bus; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 354 | |
wdenk | 9fd5e31 | 2003-12-07 23:55:12 +0000 | [diff] [blame] | 355 | #ifdef CONFIG_IDE_8xx_PCCARD |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 356 | extern int ide_devices_found; /* Initialized in check_ide_device() */ |
| 357 | #endif /* CONFIG_IDE_8xx_PCCARD */ |
wdenk | 9fd5e31 | 2003-12-07 23:55:12 +0000 | [diff] [blame] | 358 | |
| 359 | #ifdef CONFIG_IDE_PREINIT |
| 360 | WATCHDOG_RESET(); |
| 361 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 362 | if (ide_preinit()) { |
| 363 | puts("ide_preinit failed\n"); |
wdenk | 9fd5e31 | 2003-12-07 23:55:12 +0000 | [diff] [blame] | 364 | return; |
| 365 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 366 | #endif /* CONFIG_IDE_PREINIT */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 367 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 368 | WATCHDOG_RESET(); |
| 369 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 370 | /* |
| 371 | * Reset the IDE just to be sure. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 372 | * Light LED's to show |
| 373 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 374 | ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */ |
| 375 | |
| 376 | /* ATAPI Drives seems to need a proper IDE Reset */ |
| 377 | ide_reset(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 378 | |
Pavel Herrmann | 8d1165e11a | 2012-10-09 07:01:56 +0000 | [diff] [blame] | 379 | #ifdef CONFIG_IDE_INIT_POSTRESET |
| 380 | WATCHDOG_RESET(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 381 | |
Pavel Herrmann | 8d1165e11a | 2012-10-09 07:01:56 +0000 | [diff] [blame] | 382 | if (ide_init_postreset()) { |
| 383 | puts("ide_preinit_postreset failed\n"); |
| 384 | return; |
| 385 | } |
| 386 | #endif /* CONFIG_IDE_INIT_POSTRESET */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 387 | |
| 388 | /* |
| 389 | * Wait for IDE to get ready. |
| 390 | * According to spec, this can take up to 31 seconds! |
| 391 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 392 | for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) { |
| 393 | int dev = |
| 394 | bus * (CONFIG_SYS_IDE_MAXDEVICE / |
| 395 | CONFIG_SYS_IDE_MAXBUS); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 396 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 397 | #ifdef CONFIG_IDE_8xx_PCCARD |
| 398 | /* Skip non-ide devices from probing */ |
| 399 | if ((ide_devices_found & (1 << bus)) == 0) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 400 | ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */ |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 401 | continue; |
| 402 | } |
| 403 | #endif |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 404 | printf("Bus %d: ", bus); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 405 | |
| 406 | ide_bus_ok[bus] = 0; |
| 407 | |
| 408 | /* Select device |
| 409 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 410 | udelay(100000); /* 100 ms */ |
| 411 | ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev)); |
| 412 | udelay(100000); /* 100 ms */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 413 | i = 0; |
| 414 | do { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 415 | udelay(10000); /* 10 ms */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 416 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 417 | c = ide_inb(dev, ATA_STATUS); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 418 | i++; |
| 419 | if (i > (ATA_RESET_TIME * 100)) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 420 | puts("** Timeout **\n"); |
| 421 | /* LED's off */ |
| 422 | ide_led((LED_IDE1 | LED_IDE2), 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 423 | return; |
| 424 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 425 | if ((i >= 100) && ((i % 100) == 0)) |
| 426 | putc('.'); |
| 427 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 428 | } while (c & ATA_STAT_BUSY); |
| 429 | |
| 430 | if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 431 | puts("not available "); |
| 432 | debug("Status = 0x%02X ", c); |
| 433 | #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */ |
| 434 | } else if ((c & ATA_STAT_READY) == 0) { |
| 435 | puts("not available "); |
| 436 | debug("Status = 0x%02X ", c); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 437 | #endif |
| 438 | } else { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 439 | puts("OK "); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 440 | ide_bus_ok[bus] = 1; |
| 441 | } |
| 442 | WATCHDOG_RESET(); |
| 443 | } |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 444 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 445 | putc('\n'); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 446 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 447 | ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 448 | |
| 449 | curr_device = -1; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 450 | for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 451 | int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 452 | ide_dev_desc[i].type = DEV_TYPE_UNKNOWN; |
| 453 | ide_dev_desc[i].if_type = IF_TYPE_IDE; |
| 454 | ide_dev_desc[i].dev = i; |
| 455 | ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN; |
| 456 | ide_dev_desc[i].blksz = 0; |
| 457 | ide_dev_desc[i].lba = 0; |
| 458 | ide_dev_desc[i].block_read = ide_read; |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 459 | ide_dev_desc[i].block_write = ide_write; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 460 | if (!ide_bus_ok[IDE_BUS(i)]) |
| 461 | continue; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 462 | ide_led(led, 1); /* LED on */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 463 | ide_ident(&ide_dev_desc[i]); |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 464 | ide_led(led, 0); /* LED off */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 465 | dev_print(&ide_dev_desc[i]); |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 466 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 467 | if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 468 | /* initialize partition type */ |
| 469 | init_part(&ide_dev_desc[i]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 470 | if (curr_device < 0) |
| 471 | curr_device = i; |
| 472 | } |
| 473 | } |
| 474 | WATCHDOG_RESET(); |
| 475 | } |
| 476 | |
| 477 | /* ------------------------------------------------------------------------- */ |
| 478 | |
Matthew McClintock | df3fc52 | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 479 | #ifdef CONFIG_PARTITIONS |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 480 | block_dev_desc_t *ide_get_dev(int dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 481 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 482 | return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 483 | } |
Matthew McClintock | df3fc52 | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 484 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 485 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 486 | /* ------------------------------------------------------------------------- */ |
| 487 | |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 488 | void ide_input_swap_data(int dev, ulong *sect_buf, int words) |
| 489 | __attribute__ ((weak, alias("__ide_input_swap_data"))); |
| 490 | |
| 491 | void ide_input_data(int dev, ulong *sect_buf, int words) |
| 492 | __attribute__ ((weak, alias("__ide_input_data"))); |
| 493 | |
| 494 | void ide_output_data(int dev, const ulong *sect_buf, int words) |
| 495 | __attribute__ ((weak, alias("__ide_output_data"))); |
| 496 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 497 | /* We only need to swap data if we are running on a big endian cpu. */ |
Pavel Herrmann | 4d1361d | 2012-10-09 07:10:08 +0000 | [diff] [blame] | 498 | #if defined(__LITTLE_ENDIAN) |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 499 | void __ide_input_swap_data(int dev, ulong *sect_buf, int words) |
| 500 | { |
| 501 | ide_input_data(dev, sect_buf, words); |
| 502 | } |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 503 | #else |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 504 | void __ide_input_swap_data(int dev, ulong *sect_buf, int words) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 505 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 506 | volatile ushort *pbuf = |
| 507 | (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG); |
| 508 | ushort *dbuf = (ushort *) sect_buf; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 509 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 510 | debug("in input swap data base for read is %lx\n", |
| 511 | (unsigned long) pbuf); |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 512 | |
| 513 | while (words--) { |
Wolfgang Denk | 0c32d96 | 2006-06-16 17:32:31 +0200 | [diff] [blame] | 514 | #ifdef __MIPS__ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 515 | *dbuf++ = swab16p((u16 *) pbuf); |
| 516 | *dbuf++ = swab16p((u16 *) pbuf); |
Wolfgang Denk | 0c32d96 | 2006-06-16 17:32:31 +0200 | [diff] [blame] | 517 | #else |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 518 | *dbuf++ = ld_le16(pbuf); |
| 519 | *dbuf++ = ld_le16(pbuf); |
Wolfgang Denk | 0c32d96 | 2006-06-16 17:32:31 +0200 | [diff] [blame] | 520 | #endif /* !MIPS */ |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 521 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 522 | } |
Pavel Herrmann | 4d1361d | 2012-10-09 07:10:08 +0000 | [diff] [blame] | 523 | #endif /* __LITTLE_ENDIAN */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 524 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 525 | |
Albert Aribaud | f2a37fc | 2010-08-08 05:17:05 +0530 | [diff] [blame] | 526 | #if defined(CONFIG_IDE_SWAP_IO) |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 527 | void __ide_output_data(int dev, const ulong *sect_buf, int words) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 528 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 529 | ushort *dbuf; |
| 530 | volatile ushort *pbuf; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 531 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 532 | pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG); |
| 533 | dbuf = (ushort *) sect_buf; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 534 | while (words--) { |
| 535 | EIEIO; |
| 536 | *pbuf = *dbuf++; |
| 537 | EIEIO; |
| 538 | *pbuf = *dbuf++; |
| 539 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 540 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 541 | #else /* ! CONFIG_IDE_SWAP_IO */ |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 542 | void __ide_output_data(int dev, const ulong *sect_buf, int words) |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 543 | { |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 544 | #if defined(CONFIG_IDE_AHB) |
| 545 | ide_write_data(dev, sect_buf, words); |
| 546 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 547 | outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1); |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 548 | #endif |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 549 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 550 | #endif /* CONFIG_IDE_SWAP_IO */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 551 | |
Albert Aribaud | f2a37fc | 2010-08-08 05:17:05 +0530 | [diff] [blame] | 552 | #if defined(CONFIG_IDE_SWAP_IO) |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 553 | void __ide_input_data(int dev, ulong *sect_buf, int words) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 554 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 555 | ushort *dbuf; |
| 556 | volatile ushort *pbuf; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 557 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 558 | pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG); |
| 559 | dbuf = (ushort *) sect_buf; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 560 | |
| 561 | debug("in input data base for read is %lx\n", (unsigned long) pbuf); |
| 562 | |
| 563 | while (words--) { |
| 564 | EIEIO; |
| 565 | *dbuf++ = *pbuf; |
| 566 | EIEIO; |
| 567 | *dbuf++ = *pbuf; |
| 568 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 569 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 570 | #else /* ! CONFIG_IDE_SWAP_IO */ |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 571 | void __ide_input_data(int dev, ulong *sect_buf, int words) |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 572 | { |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 573 | #if defined(CONFIG_IDE_AHB) |
| 574 | ide_read_data(dev, sect_buf, words); |
| 575 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 576 | insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1); |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 577 | #endif |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 580 | #endif /* CONFIG_IDE_SWAP_IO */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 581 | |
| 582 | /* ------------------------------------------------------------------------- |
| 583 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 584 | static void ide_ident(block_dev_desc_t *dev_desc) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 585 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 586 | unsigned char c; |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 587 | hd_driveid_t iop; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 588 | |
wdenk | 64f70be | 2004-09-28 20:34:50 +0000 | [diff] [blame] | 589 | #ifdef CONFIG_ATAPI |
| 590 | int retries = 0; |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 591 | #endif |
| 592 | |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 593 | #ifdef CONFIG_TUNE_PIO |
| 594 | int pio_mode; |
| 595 | #endif |
| 596 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 597 | #if 0 |
| 598 | int mode, cycle_time; |
| 599 | #endif |
| 600 | int device; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 601 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 602 | device = dev_desc->dev; |
| 603 | printf(" Device %d: ", device); |
| 604 | |
| 605 | ide_led(DEVICE_LED(device), 1); /* LED on */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 606 | /* Select device |
| 607 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 608 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 609 | dev_desc->if_type = IF_TYPE_IDE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 610 | #ifdef CONFIG_ATAPI |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 611 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 612 | retries = 0; |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 613 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 614 | /* Warning: This will be tricky to read */ |
| 615 | while (retries <= 1) { |
| 616 | /* check signature */ |
| 617 | if ((ide_inb(device, ATA_SECT_CNT) == 0x01) && |
| 618 | (ide_inb(device, ATA_SECT_NUM) == 0x01) && |
| 619 | (ide_inb(device, ATA_CYL_LOW) == 0x14) && |
| 620 | (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) { |
| 621 | /* ATAPI Signature found */ |
| 622 | dev_desc->if_type = IF_TYPE_ATAPI; |
| 623 | /* |
| 624 | * Start Ident Command |
| 625 | */ |
| 626 | ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT); |
| 627 | /* |
| 628 | * Wait for completion - ATAPI devices need more time |
| 629 | * to become ready |
| 630 | */ |
| 631 | c = ide_wait(device, ATAPI_TIME_OUT); |
| 632 | } else |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 633 | #endif |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 634 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 635 | /* |
| 636 | * Start Ident Command |
| 637 | */ |
| 638 | ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT); |
| 639 | |
| 640 | /* |
| 641 | * Wait for completion |
| 642 | */ |
| 643 | c = ide_wait(device, IDE_TIME_OUT); |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 644 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 645 | ide_led(DEVICE_LED(device), 0); /* LED off */ |
| 646 | |
| 647 | if (((c & ATA_STAT_DRQ) == 0) || |
| 648 | ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) { |
wdenk | 64f70be | 2004-09-28 20:34:50 +0000 | [diff] [blame] | 649 | #ifdef CONFIG_ATAPI |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 650 | { |
| 651 | /* |
| 652 | * Need to soft reset the device |
| 653 | * in case it's an ATAPI... |
| 654 | */ |
| 655 | debug("Retrying...\n"); |
| 656 | ide_outb(device, ATA_DEV_HD, |
| 657 | ATA_LBA | ATA_DEVICE(device)); |
| 658 | udelay(100000); |
| 659 | ide_outb(device, ATA_COMMAND, 0x08); |
| 660 | udelay(500000); /* 500 ms */ |
| 661 | } |
| 662 | /* |
| 663 | * Select device |
| 664 | */ |
| 665 | ide_outb(device, ATA_DEV_HD, |
| 666 | ATA_LBA | ATA_DEVICE(device)); |
| 667 | retries++; |
| 668 | #else |
| 669 | return; |
| 670 | #endif |
| 671 | } |
| 672 | #ifdef CONFIG_ATAPI |
| 673 | else |
| 674 | break; |
| 675 | } /* see above - ugly to read */ |
wdenk | 64f70be | 2004-09-28 20:34:50 +0000 | [diff] [blame] | 676 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 677 | if (retries == 2) /* Not found */ |
wdenk | 64f70be | 2004-09-28 20:34:50 +0000 | [diff] [blame] | 678 | return; |
| 679 | #endif |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 680 | |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 681 | ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 682 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 683 | ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev, |
| 684 | sizeof(dev_desc->revision)); |
| 685 | ident_cpy((unsigned char *) dev_desc->vendor, iop.model, |
| 686 | sizeof(dev_desc->vendor)); |
| 687 | ident_cpy((unsigned char *) dev_desc->product, iop.serial_no, |
| 688 | sizeof(dev_desc->product)); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 689 | #ifdef __LITTLE_ENDIAN |
| 690 | /* |
Richard Retanubun | bcdf1d2 | 2008-11-06 14:01:51 -0500 | [diff] [blame] | 691 | * firmware revision, model, and serial number have Big Endian Byte |
| 692 | * order in Word. Convert all three to little endian. |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 693 | * |
| 694 | * See CF+ and CompactFlash Specification Revision 2.0: |
Richard Retanubun | bcdf1d2 | 2008-11-06 14:01:51 -0500 | [diff] [blame] | 695 | * 6.2.1.6: Identify Drive, Table 39 for more details |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 696 | */ |
| 697 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 698 | strswab(dev_desc->revision); |
| 699 | strswab(dev_desc->vendor); |
| 700 | strswab(dev_desc->product); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 701 | #endif /* __LITTLE_ENDIAN */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 702 | |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 703 | if ((iop.config & 0x0080) == 0x0080) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 704 | dev_desc->removable = 1; |
| 705 | else |
| 706 | dev_desc->removable = 0; |
| 707 | |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 708 | #ifdef CONFIG_TUNE_PIO |
| 709 | /* Mode 0 - 2 only, are directly determined by word 51. */ |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 710 | pio_mode = iop.tPIO; |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 711 | if (pio_mode > 2) { |
| 712 | printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode); |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 713 | /* Force it to dead slow, and hope for the best... */ |
| 714 | pio_mode = 0; |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | /* Any CompactFlash Storage Card that supports PIO mode 3 or above |
| 718 | * shall set bit 1 of word 53 to one and support the fields contained |
| 719 | * in words 64 through 70. |
| 720 | */ |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 721 | if (iop.field_valid & 0x02) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 722 | /* |
| 723 | * Mode 3 and above are possible. Check in order from slow |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 724 | * to fast, so we wind up with the highest mode allowed. |
| 725 | */ |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 726 | if (iop.eide_pio_modes & 0x01) |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 727 | pio_mode = 3; |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 728 | if (iop.eide_pio_modes & 0x02) |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 729 | pio_mode = 4; |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 730 | if (ata_id_is_cfa((u16 *)&iop)) { |
| 731 | if ((iop.cf_advanced_caps & 0x07) == 0x01) |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 732 | pio_mode = 5; |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 733 | if ((iop.cf_advanced_caps & 0x07) == 0x02) |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 734 | pio_mode = 6; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /* System-specific, depends on bus speeds, etc. */ |
| 739 | ide_set_piomode(pio_mode); |
| 740 | #endif /* CONFIG_TUNE_PIO */ |
| 741 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 742 | #if 0 |
| 743 | /* |
| 744 | * Drive PIO mode autoselection |
| 745 | */ |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 746 | mode = iop.tPIO; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 747 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 748 | printf("tPIO = 0x%02x = %d\n", mode, mode); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 749 | if (mode > 2) { /* 2 is maximum allowed tPIO value */ |
| 750 | mode = 2; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 751 | debug("Override tPIO -> 2\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 752 | } |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 753 | if (iop.field_valid & 2) { /* drive implements ATA2? */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 754 | debug("Drive implements ATA2\n"); |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 755 | if (iop.capability & 8) { /* drive supports use_iordy? */ |
| 756 | cycle_time = iop.eide_pio_iordy; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 757 | } else { |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 758 | cycle_time = iop.eide_pio; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 759 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 760 | debug("cycle time = %d\n", cycle_time); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 761 | mode = 4; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 762 | if (cycle_time > 120) |
| 763 | mode = 3; /* 120 ns for PIO mode 4 */ |
| 764 | if (cycle_time > 180) |
| 765 | mode = 2; /* 180 ns for PIO mode 3 */ |
| 766 | if (cycle_time > 240) |
| 767 | mode = 1; /* 240 ns for PIO mode 4 */ |
| 768 | if (cycle_time > 383) |
| 769 | mode = 0; /* 383 ns for PIO mode 4 */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 770 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 771 | printf("PIO mode to use: PIO %d\n", mode); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 772 | #endif /* 0 */ |
| 773 | |
| 774 | #ifdef CONFIG_ATAPI |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 775 | if (dev_desc->if_type == IF_TYPE_ATAPI) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 776 | atapi_inquiry(dev_desc); |
| 777 | return; |
| 778 | } |
| 779 | #endif /* CONFIG_ATAPI */ |
| 780 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 781 | #ifdef __BIG_ENDIAN |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 782 | /* swap shorts */ |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 783 | dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16); |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 784 | #else /* ! __BIG_ENDIAN */ |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 785 | /* |
| 786 | * do not swap shorts on little endian |
| 787 | * |
| 788 | * See CF+ and CompactFlash Specification Revision 2.0: |
| 789 | * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details. |
| 790 | */ |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 791 | dev_desc->lba = iop.lba_capacity; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 792 | #endif /* __BIG_ENDIAN */ |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 793 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 794 | #ifdef CONFIG_LBA48 |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 795 | if (iop.command_set_2 & 0x0400) { /* LBA 48 support */ |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 796 | dev_desc->lba48 = 1; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 797 | dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] | |
| 798 | ((unsigned long long) iop.lba48_capacity[1] << 16) | |
| 799 | ((unsigned long long) iop.lba48_capacity[2] << 32) | |
| 800 | ((unsigned long long) iop.lba48_capacity[3] << 48); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 801 | } else { |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 802 | dev_desc->lba48 = 0; |
| 803 | } |
| 804 | #endif /* CONFIG_LBA48 */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 805 | /* assuming HD */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 806 | dev_desc->type = DEV_TYPE_HARDDISK; |
| 807 | dev_desc->blksz = ATA_BLOCKSIZE; |
| 808 | dev_desc->lun = 0; /* just to fill something in... */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 809 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 810 | #if 0 /* only used to test the powersaving mode, |
| 811 | * if enabled, the drive goes after 5 sec |
| 812 | * in standby mode */ |
| 813 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 814 | c = ide_wait(device, IDE_TIME_OUT); |
| 815 | ide_outb(device, ATA_SECT_CNT, 1); |
| 816 | ide_outb(device, ATA_LBA_LOW, 0); |
| 817 | ide_outb(device, ATA_LBA_MID, 0); |
| 818 | ide_outb(device, ATA_LBA_HIGH, 0); |
| 819 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 820 | ide_outb(device, ATA_COMMAND, 0xe3); |
| 821 | udelay(50); |
| 822 | c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 823 | #endif |
| 824 | } |
| 825 | |
| 826 | |
| 827 | /* ------------------------------------------------------------------------- */ |
| 828 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 829 | ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 830 | { |
| 831 | ulong n = 0; |
| 832 | unsigned char c; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 833 | unsigned char pwrsave = 0; /* power save */ |
| 834 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 835 | #ifdef CONFIG_LBA48 |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 836 | unsigned char lba48 = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 837 | |
Guennadi Liakhovetski | 413bf58 | 2008-04-28 14:36:06 +0200 | [diff] [blame] | 838 | if (blknr & 0x0000fffff0000000ULL) { |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 839 | /* more than 28 bits used, use 48bit mode */ |
| 840 | lba48 = 1; |
| 841 | } |
| 842 | #endif |
Marek Vasut | 5bbe10d | 2011-10-25 11:39:15 +0200 | [diff] [blame] | 843 | debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n", |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 844 | device, blknr, blkcnt, (ulong) buffer); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 845 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 846 | ide_led(DEVICE_LED(device), 1); /* LED on */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 847 | |
| 848 | /* Select device |
| 849 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 850 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 851 | c = ide_wait(device, IDE_TIME_OUT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 852 | |
| 853 | if (c & ATA_STAT_BUSY) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 854 | printf("IDE read: device %d not ready\n", device); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 855 | goto IDE_READ_E; |
| 856 | } |
| 857 | |
| 858 | /* first check if the drive is in Powersaving mode, if yes, |
| 859 | * increase the timeout value */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 860 | ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR); |
| 861 | udelay(50); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 862 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 863 | c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 864 | |
| 865 | if (c & ATA_STAT_BUSY) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 866 | printf("IDE read: device %d not ready\n", device); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 867 | goto IDE_READ_E; |
| 868 | } |
| 869 | if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 870 | printf("No Powersaving mode %X\n", c); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 871 | } else { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 872 | c = ide_inb(device, ATA_SECT_CNT); |
| 873 | debug("Powersaving %02X\n", c); |
| 874 | if (c == 0) |
| 875 | pwrsave = 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | |
| 879 | while (blkcnt-- > 0) { |
| 880 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 881 | c = ide_wait(device, IDE_TIME_OUT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 882 | |
| 883 | if (c & ATA_STAT_BUSY) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 884 | printf("IDE read: device %d not ready\n", device); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 885 | break; |
| 886 | } |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 887 | #ifdef CONFIG_LBA48 |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 888 | if (lba48) { |
| 889 | /* write high bits */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 890 | ide_outb(device, ATA_SECT_CNT, 0); |
| 891 | ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 892 | #ifdef CONFIG_SYS_64BIT_LBA |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 893 | ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF); |
| 894 | ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF); |
Guennadi Liakhovetski | 413bf58 | 2008-04-28 14:36:06 +0200 | [diff] [blame] | 895 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 896 | ide_outb(device, ATA_LBA_MID, 0); |
| 897 | ide_outb(device, ATA_LBA_HIGH, 0); |
Guennadi Liakhovetski | 413bf58 | 2008-04-28 14:36:06 +0200 | [diff] [blame] | 898 | #endif |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 899 | } |
| 900 | #endif |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 901 | ide_outb(device, ATA_SECT_CNT, 1); |
| 902 | ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF); |
| 903 | ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF); |
| 904 | ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 905 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 906 | #ifdef CONFIG_LBA48 |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 907 | if (lba48) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 908 | ide_outb(device, ATA_DEV_HD, |
| 909 | ATA_LBA | ATA_DEVICE(device)); |
| 910 | ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 911 | |
| 912 | } else |
| 913 | #endif |
| 914 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 915 | ide_outb(device, ATA_DEV_HD, ATA_LBA | |
| 916 | ATA_DEVICE(device) | ((blknr >> 24) & 0xF)); |
| 917 | ide_outb(device, ATA_COMMAND, ATA_CMD_READ); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 918 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 919 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 920 | udelay(50); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 921 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 922 | if (pwrsave) { |
| 923 | /* may take up to 4 sec */ |
| 924 | c = ide_wait(device, IDE_SPIN_UP_TIME_OUT); |
| 925 | pwrsave = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 926 | } else { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 927 | /* can't take over 500 ms */ |
| 928 | c = ide_wait(device, IDE_TIME_OUT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 931 | if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) != |
| 932 | ATA_STAT_DRQ) { |
Heiko Schocher | 4b142fe | 2009-12-03 11:21:21 +0100 | [diff] [blame] | 933 | #if defined(CONFIG_SYS_64BIT_LBA) |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 934 | printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 935 | device, blknr, c); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 936 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 937 | printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n", |
| 938 | device, (ulong) blknr, c); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 939 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 940 | break; |
| 941 | } |
| 942 | |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 943 | ide_input_data(device, buffer, ATA_SECTORWORDS); |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 944 | (void) ide_inb(device, ATA_STATUS); /* clear IRQ */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 945 | |
| 946 | ++n; |
| 947 | ++blknr; |
Greg Lopp | 0b94504 | 2007-04-13 08:02:24 +0200 | [diff] [blame] | 948 | buffer += ATA_BLOCKSIZE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 949 | } |
| 950 | IDE_READ_E: |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 951 | ide_led(DEVICE_LED(device), 0); /* LED off */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 952 | return (n); |
| 953 | } |
| 954 | |
| 955 | /* ------------------------------------------------------------------------- */ |
| 956 | |
| 957 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 958 | ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 959 | { |
| 960 | ulong n = 0; |
| 961 | unsigned char c; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 962 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 963 | #ifdef CONFIG_LBA48 |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 964 | unsigned char lba48 = 0; |
| 965 | |
Guennadi Liakhovetski | 413bf58 | 2008-04-28 14:36:06 +0200 | [diff] [blame] | 966 | if (blknr & 0x0000fffff0000000ULL) { |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 967 | /* more than 28 bits used, use 48bit mode */ |
| 968 | lba48 = 1; |
| 969 | } |
| 970 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 971 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 972 | ide_led(DEVICE_LED(device), 1); /* LED on */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 973 | |
| 974 | /* Select device |
| 975 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 976 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 977 | |
| 978 | while (blkcnt-- > 0) { |
| 979 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 980 | c = ide_wait(device, IDE_TIME_OUT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 981 | |
| 982 | if (c & ATA_STAT_BUSY) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 983 | printf("IDE read: device %d not ready\n", device); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 984 | goto WR_OUT; |
| 985 | } |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 986 | #ifdef CONFIG_LBA48 |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 987 | if (lba48) { |
| 988 | /* write high bits */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 989 | ide_outb(device, ATA_SECT_CNT, 0); |
| 990 | ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 991 | #ifdef CONFIG_SYS_64BIT_LBA |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 992 | ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF); |
| 993 | ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF); |
Guennadi Liakhovetski | 413bf58 | 2008-04-28 14:36:06 +0200 | [diff] [blame] | 994 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 995 | ide_outb(device, ATA_LBA_MID, 0); |
| 996 | ide_outb(device, ATA_LBA_HIGH, 0); |
Guennadi Liakhovetski | 413bf58 | 2008-04-28 14:36:06 +0200 | [diff] [blame] | 997 | #endif |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 998 | } |
| 999 | #endif |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1000 | ide_outb(device, ATA_SECT_CNT, 1); |
| 1001 | ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF); |
| 1002 | ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF); |
| 1003 | ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 1004 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 1005 | #ifdef CONFIG_LBA48 |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 1006 | if (lba48) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1007 | ide_outb(device, ATA_DEV_HD, |
| 1008 | ATA_LBA | ATA_DEVICE(device)); |
| 1009 | ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 1010 | |
| 1011 | } else |
| 1012 | #endif |
| 1013 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1014 | ide_outb(device, ATA_DEV_HD, ATA_LBA | |
| 1015 | ATA_DEVICE(device) | ((blknr >> 24) & 0xF)); |
| 1016 | ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 1017 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1018 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1019 | udelay(50); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1020 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1021 | /* can't take over 500 ms */ |
| 1022 | c = ide_wait(device, IDE_TIME_OUT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1023 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1024 | if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) != |
| 1025 | ATA_STAT_DRQ) { |
Heiko Schocher | 4b142fe | 2009-12-03 11:21:21 +0100 | [diff] [blame] | 1026 | #if defined(CONFIG_SYS_64BIT_LBA) |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1027 | printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1028 | device, blknr, c); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 1029 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1030 | printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n", |
| 1031 | device, (ulong) blknr, c); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 1032 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1033 | goto WR_OUT; |
| 1034 | } |
| 1035 | |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1036 | ide_output_data(device, buffer, ATA_SECTORWORDS); |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1037 | c = ide_inb(device, ATA_STATUS); /* clear IRQ */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1038 | ++n; |
| 1039 | ++blknr; |
Greg Lopp | 0b94504 | 2007-04-13 08:02:24 +0200 | [diff] [blame] | 1040 | buffer += ATA_BLOCKSIZE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1041 | } |
| 1042 | WR_OUT: |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1043 | ide_led(DEVICE_LED(device), 0); /* LED off */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1044 | return (n); |
| 1045 | } |
| 1046 | |
| 1047 | /* ------------------------------------------------------------------------- */ |
| 1048 | |
| 1049 | /* |
| 1050 | * copy src to dest, skipping leading and trailing blanks and null |
| 1051 | * terminate the string |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1052 | * "len" is the size of available memory including the terminating '\0' |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1053 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1054 | static void ident_cpy(unsigned char *dst, unsigned char *src, |
| 1055 | unsigned int len) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1056 | { |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1057 | unsigned char *end, *last; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1058 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1059 | last = dst; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1060 | end = src + len - 1; |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1061 | |
| 1062 | /* reserve space for '\0' */ |
| 1063 | if (len < 2) |
| 1064 | goto OUT; |
wdenk | efa329c | 2004-03-23 20:18:25 +0000 | [diff] [blame] | 1065 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1066 | /* skip leading white space */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1067 | while ((*src) && (src < end) && (*src == ' ')) |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1068 | ++src; |
| 1069 | |
| 1070 | /* copy string, omitting trailing white space */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1071 | while ((*src) && (src < end)) { |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1072 | *dst++ = *src; |
| 1073 | if (*src++ != ' ') |
| 1074 | last = dst; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1075 | } |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1076 | OUT: |
| 1077 | *last = '\0'; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | /* ------------------------------------------------------------------------- */ |
| 1081 | |
| 1082 | /* |
| 1083 | * Wait until Busy bit is off, or timeout (in ms) |
| 1084 | * Return last status |
| 1085 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1086 | static uchar ide_wait(int dev, ulong t) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1087 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1088 | ulong delay = 10 * t; /* poll every 100 us */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1089 | uchar c; |
| 1090 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1091 | while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1092 | udelay(100); |
| 1093 | if (delay-- == 0) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1094 | break; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1095 | } |
| 1096 | return (c); |
| 1097 | } |
| 1098 | |
| 1099 | /* ------------------------------------------------------------------------- */ |
| 1100 | |
| 1101 | #ifdef CONFIG_IDE_RESET |
| 1102 | extern void ide_set_reset(int idereset); |
| 1103 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1104 | static void ide_reset(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1105 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1106 | int i; |
| 1107 | |
| 1108 | curr_device = -1; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1109 | for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1110 | ide_bus_ok[i] = 0; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1111 | for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1112 | ide_dev_desc[i].type = DEV_TYPE_UNKNOWN; |
| 1113 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1114 | ide_set_reset(1); /* assert reset */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1115 | |
Martin Krause | e175eac | 2008-04-03 13:37:56 +0200 | [diff] [blame] | 1116 | /* the reset signal shall be asserted for et least 25 us */ |
| 1117 | udelay(25); |
| 1118 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1119 | WATCHDOG_RESET(); |
| 1120 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1121 | /* de-assert RESET signal */ |
| 1122 | ide_set_reset(0); |
| 1123 | |
| 1124 | /* wait 250 ms */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1125 | for (i = 0; i < 250; ++i) |
| 1126 | udelay(1000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1129 | #endif /* CONFIG_IDE_RESET */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1130 | |
| 1131 | /* ------------------------------------------------------------------------- */ |
| 1132 | |
Heiko Schocher | 3887c3f | 2009-09-23 07:56:08 +0200 | [diff] [blame] | 1133 | #if defined(CONFIG_OF_IDE_FIXUP) |
| 1134 | int ide_device_present(int dev) |
| 1135 | { |
| 1136 | if (dev >= CONFIG_SYS_IDE_MAXBUS) |
| 1137 | return 0; |
| 1138 | return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1); |
| 1139 | } |
| 1140 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1141 | /* ------------------------------------------------------------------------- */ |
| 1142 | |
| 1143 | #ifdef CONFIG_ATAPI |
| 1144 | /**************************************************************************** |
| 1145 | * ATAPI Support |
| 1146 | */ |
| 1147 | |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1148 | void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts) |
| 1149 | __attribute__ ((weak, alias("__ide_input_data_shorts"))); |
| 1150 | |
| 1151 | void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts) |
| 1152 | __attribute__ ((weak, alias("__ide_output_data_shorts"))); |
| 1153 | |
| 1154 | |
Albert Aribaud | f2a37fc | 2010-08-08 05:17:05 +0530 | [diff] [blame] | 1155 | #if defined(CONFIG_IDE_SWAP_IO) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1156 | /* since ATAPI may use commands with not 4 bytes alligned length |
| 1157 | * we have our own transfer functions, 2 bytes alligned */ |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1158 | void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1159 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1160 | ushort *dbuf; |
| 1161 | volatile ushort *pbuf; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1162 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1163 | pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG); |
| 1164 | dbuf = (ushort *) sect_buf; |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 1165 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1166 | debug("in output data shorts base for read is %lx\n", |
| 1167 | (unsigned long) pbuf); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 1168 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1169 | while (shorts--) { |
wdenk | 5cf91d6 | 2004-04-23 20:32:05 +0000 | [diff] [blame] | 1170 | EIEIO; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 1171 | *pbuf = *dbuf++; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1172 | } |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1175 | void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts) |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 1176 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1177 | ushort *dbuf; |
| 1178 | volatile ushort *pbuf; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 1179 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1180 | pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG); |
| 1181 | dbuf = (ushort *) sect_buf; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 1182 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1183 | debug("in input data shorts base for read is %lx\n", |
| 1184 | (unsigned long) pbuf); |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 1185 | |
| 1186 | while (shorts--) { |
| 1187 | EIEIO; |
| 1188 | *dbuf++ = *pbuf; |
| 1189 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1192 | #else /* ! CONFIG_IDE_SWAP_IO */ |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1193 | void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts) |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1194 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1195 | outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts); |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1198 | void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts) |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1199 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1200 | insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts); |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1203 | #endif /* CONFIG_IDE_SWAP_IO */ |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1204 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1205 | /* |
| 1206 | * Wait until (Status & mask) == res, or timeout (in ms) |
| 1207 | * Return last status |
| 1208 | * This is used since some ATAPI CD ROMs clears their Busy Bit first |
| 1209 | * and then they set their DRQ Bit |
| 1210 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1211 | static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1212 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1213 | ulong delay = 10 * t; /* poll every 100 us */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1214 | uchar c; |
| 1215 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1216 | /* prevents to read the status before valid */ |
| 1217 | c = ide_inb(dev, ATA_DEV_CTL); |
| 1218 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1219 | while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1220 | /* break if error occurs (doesn't make sense to wait more) */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1221 | if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1222 | break; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1223 | udelay(100); |
| 1224 | if (delay-- == 0) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1225 | break; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1226 | } |
| 1227 | return (c); |
| 1228 | } |
| 1229 | |
| 1230 | /* |
| 1231 | * issue an atapi command |
| 1232 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1233 | unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen, |
| 1234 | unsigned char *buffer, int buflen) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1235 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1236 | unsigned char c, err, mask, res; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1237 | int n; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1238 | |
| 1239 | ide_led(DEVICE_LED(device), 1); /* LED on */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1240 | |
| 1241 | /* Select device |
| 1242 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1243 | mask = ATA_STAT_BUSY | ATA_STAT_DRQ; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1244 | res = 0; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1245 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 1246 | c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1247 | if ((c & mask) != res) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1248 | printf("ATAPI_ISSUE: device %d not ready status %X\n", device, |
| 1249 | c); |
| 1250 | err = 0xFF; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1251 | goto AI_OUT; |
| 1252 | } |
| 1253 | /* write taskfile */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1254 | ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */ |
| 1255 | ide_outb(device, ATA_SECT_CNT, 0); |
| 1256 | ide_outb(device, ATA_SECT_NUM, 0); |
| 1257 | ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF)); |
| 1258 | ide_outb(device, ATA_CYL_HIGH, |
| 1259 | (unsigned char) ((buflen >> 8) & 0xFF)); |
| 1260 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1261 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1262 | ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET); |
| 1263 | udelay(50); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1264 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1265 | mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1266 | res = ATA_STAT_DRQ; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1267 | c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1268 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1269 | if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */ |
| 1270 | printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n", |
| 1271 | device, c); |
| 1272 | err = 0xFF; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1273 | goto AI_OUT; |
| 1274 | } |
| 1275 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1276 | /* write command block */ |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1277 | ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2); |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1278 | |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1279 | /* ATAPI Command written wait for completition */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1280 | udelay(5000); /* device must set bsy */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1281 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1282 | mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR; |
| 1283 | /* |
| 1284 | * if no data wait for DRQ = 0 BSY = 0 |
| 1285 | * if data wait for DRQ = 1 BSY = 0 |
| 1286 | */ |
| 1287 | res = 0; |
| 1288 | if (buflen) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1289 | res = ATA_STAT_DRQ; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1290 | c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); |
| 1291 | if ((c & mask) != res) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1292 | if (c & ATA_STAT_ERR) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1293 | err = (ide_inb(device, ATA_ERROR_REG)) >> 4; |
| 1294 | debug("atapi_issue 1 returned sense key %X status %02X\n", |
| 1295 | err, c); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1296 | } else { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1297 | printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", |
| 1298 | ccb[0], c); |
| 1299 | err = 0xFF; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1300 | } |
| 1301 | goto AI_OUT; |
| 1302 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1303 | n = ide_inb(device, ATA_CYL_HIGH); |
| 1304 | n <<= 8; |
| 1305 | n += ide_inb(device, ATA_CYL_LOW); |
| 1306 | if (n > buflen) { |
| 1307 | printf("ERROR, transfer bytes %d requested only %d\n", n, |
| 1308 | buflen); |
| 1309 | err = 0xff; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1310 | goto AI_OUT; |
| 1311 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1312 | if ((n == 0) && (buflen < 0)) { |
| 1313 | printf("ERROR, transfer bytes %d requested %d\n", n, buflen); |
| 1314 | err = 0xff; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1315 | goto AI_OUT; |
| 1316 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1317 | if (n != buflen) { |
| 1318 | debug("WARNING, transfer bytes %d not equal with requested %d\n", |
| 1319 | n, buflen); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1320 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1321 | if (n != 0) { /* data transfer */ |
| 1322 | debug("ATAPI_ISSUE: %d Bytes to transfer\n", n); |
| 1323 | /* we transfer shorts */ |
| 1324 | n >>= 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1325 | /* ok now decide if it is an in or output */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1326 | if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) { |
| 1327 | debug("Write to device\n"); |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1328 | ide_output_data_shorts(device, |
| 1329 | (unsigned short *) buffer, n); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1330 | } else { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1331 | debug("Read from device @ %p shorts %d\n", buffer, n); |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1332 | ide_input_data_shorts(device, |
| 1333 | (unsigned short *) buffer, n); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1334 | } |
| 1335 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1336 | udelay(5000); /* seems that some CD ROMs need this... */ |
| 1337 | mask = ATA_STAT_BUSY | ATA_STAT_ERR; |
| 1338 | res = 0; |
| 1339 | c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1340 | if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1341 | err = (ide_inb(device, ATA_ERROR_REG) >> 4); |
| 1342 | debug("atapi_issue 2 returned sense key %X status %X\n", err, |
| 1343 | c); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1344 | } else { |
| 1345 | err = 0; |
| 1346 | } |
| 1347 | AI_OUT: |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1348 | ide_led(DEVICE_LED(device), 0); /* LED off */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1349 | return (err); |
| 1350 | } |
| 1351 | |
| 1352 | /* |
| 1353 | * sending the command to atapi_issue. If an status other than good |
| 1354 | * returns, an request_sense will be issued |
| 1355 | */ |
| 1356 | |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1357 | #define ATAPI_DRIVE_NOT_READY 100 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1358 | #define ATAPI_UNIT_ATTN 10 |
| 1359 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1360 | unsigned char atapi_issue_autoreq(int device, |
| 1361 | unsigned char *ccb, |
| 1362 | int ccblen, |
| 1363 | unsigned char *buffer, int buflen) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1364 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1365 | unsigned char sense_data[18], sense_ccb[12]; |
| 1366 | unsigned char res, key, asc, ascq; |
| 1367 | int notready, unitattn; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1368 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1369 | unitattn = ATAPI_UNIT_ATTN; |
| 1370 | notready = ATAPI_DRIVE_NOT_READY; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1371 | |
| 1372 | retry: |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1373 | res = atapi_issue(device, ccb, ccblen, buffer, buflen); |
| 1374 | if (res == 0) |
| 1375 | return 0; /* Ok */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1376 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1377 | if (res == 0xFF) |
| 1378 | return 0xFF; /* error */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1379 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1380 | debug("(auto_req)atapi_issue returned sense key %X\n", res); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1381 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1382 | memset(sense_ccb, 0, sizeof(sense_ccb)); |
| 1383 | memset(sense_data, 0, sizeof(sense_data)); |
| 1384 | sense_ccb[0] = ATAPI_CMD_REQ_SENSE; |
| 1385 | sense_ccb[4] = 18; /* allocation Length */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1386 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1387 | res = atapi_issue(device, sense_ccb, 12, sense_data, 18); |
| 1388 | key = (sense_data[2] & 0xF); |
| 1389 | asc = (sense_data[12]); |
| 1390 | ascq = (sense_data[13]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1391 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1392 | debug("ATAPI_CMD_REQ_SENSE returned %x\n", res); |
| 1393 | debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n", |
| 1394 | sense_data[0], key, asc, ascq); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1395 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1396 | if ((key == 0)) |
| 1397 | return 0; /* ok device ready */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1398 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1399 | if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */ |
| 1400 | if (unitattn-- > 0) { |
| 1401 | udelay(200 * 1000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1402 | goto retry; |
| 1403 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1404 | printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1405 | goto error; |
| 1406 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1407 | if ((asc == 0x4) && (ascq == 0x1)) { |
| 1408 | /* not ready, but will be ready soon */ |
| 1409 | if (notready-- > 0) { |
| 1410 | udelay(200 * 1000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1411 | goto retry; |
| 1412 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1413 | printf("Drive not ready, tried %d times\n", |
| 1414 | ATAPI_DRIVE_NOT_READY); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1415 | goto error; |
| 1416 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1417 | if (asc == 0x3a) { |
| 1418 | debug("Media not present\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1419 | goto error; |
| 1420 | } |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 1421 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1422 | printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc, |
| 1423 | ascq); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1424 | error: |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1425 | debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1426 | return (0xFF); |
| 1427 | } |
| 1428 | |
| 1429 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1430 | static void atapi_inquiry(block_dev_desc_t *dev_desc) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1431 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1432 | unsigned char ccb[12]; /* Command descriptor block */ |
| 1433 | unsigned char iobuf[64]; /* temp buf */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1434 | unsigned char c; |
| 1435 | int device; |
| 1436 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1437 | device = dev_desc->dev; |
| 1438 | dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */ |
| 1439 | dev_desc->block_read = atapi_read; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1440 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1441 | memset(ccb, 0, sizeof(ccb)); |
| 1442 | memset(iobuf, 0, sizeof(iobuf)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1443 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1444 | ccb[0] = ATAPI_CMD_INQUIRY; |
| 1445 | ccb[4] = 40; /* allocation Legnth */ |
| 1446 | c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1447 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1448 | debug("ATAPI_CMD_INQUIRY returned %x\n", c); |
| 1449 | if (c != 0) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1450 | return; |
| 1451 | |
| 1452 | /* copy device ident strings */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1453 | ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8); |
| 1454 | ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16); |
| 1455 | ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1456 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1457 | dev_desc->lun = 0; |
| 1458 | dev_desc->lba = 0; |
| 1459 | dev_desc->blksz = 0; |
| 1460 | dev_desc->type = iobuf[0] & 0x1f; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1461 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1462 | if ((iobuf[1] & 0x80) == 0x80) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1463 | dev_desc->removable = 1; |
| 1464 | else |
| 1465 | dev_desc->removable = 0; |
| 1466 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1467 | memset(ccb, 0, sizeof(ccb)); |
| 1468 | memset(iobuf, 0, sizeof(iobuf)); |
| 1469 | ccb[0] = ATAPI_CMD_START_STOP; |
| 1470 | ccb[4] = 0x03; /* start */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1471 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1472 | c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1473 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1474 | debug("ATAPI_CMD_START_STOP returned %x\n", c); |
| 1475 | if (c != 0) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1476 | return; |
| 1477 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1478 | memset(ccb, 0, sizeof(ccb)); |
| 1479 | memset(iobuf, 0, sizeof(iobuf)); |
| 1480 | c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1481 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1482 | debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c); |
| 1483 | if (c != 0) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1484 | return; |
| 1485 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1486 | memset(ccb, 0, sizeof(ccb)); |
| 1487 | memset(iobuf, 0, sizeof(iobuf)); |
| 1488 | ccb[0] = ATAPI_CMD_READ_CAP; |
| 1489 | c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8); |
| 1490 | debug("ATAPI_CMD_READ_CAP returned %x\n", c); |
| 1491 | if (c != 0) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1492 | return; |
| 1493 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1494 | debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n", |
| 1495 | iobuf[0], iobuf[1], iobuf[2], iobuf[3], |
| 1496 | iobuf[4], iobuf[5], iobuf[6], iobuf[7]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1497 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1498 | dev_desc->lba = ((unsigned long) iobuf[0] << 24) + |
| 1499 | ((unsigned long) iobuf[1] << 16) + |
| 1500 | ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]); |
| 1501 | dev_desc->blksz = ((unsigned long) iobuf[4] << 24) + |
| 1502 | ((unsigned long) iobuf[5] << 16) + |
| 1503 | ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]); |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 1504 | #ifdef CONFIG_LBA48 |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1505 | /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */ |
| 1506 | dev_desc->lba48 = 0; |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 1507 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1508 | return; |
| 1509 | } |
| 1510 | |
| 1511 | |
| 1512 | /* |
| 1513 | * atapi_read: |
| 1514 | * we transfer only one block per command, since the multiple DRQ per |
| 1515 | * command is not yet implemented |
| 1516 | */ |
| 1517 | #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */ |
| 1518 | #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1519 | #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1520 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1521 | ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1522 | { |
| 1523 | ulong n = 0; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1524 | unsigned char ccb[12]; /* Command descriptor block */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1525 | ulong cnt; |
| 1526 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1527 | debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n", |
| 1528 | device, blknr, blkcnt, (ulong) buffer); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1529 | |
| 1530 | do { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1531 | if (blkcnt > ATAPI_READ_MAX_BLOCK) |
| 1532 | cnt = ATAPI_READ_MAX_BLOCK; |
| 1533 | else |
| 1534 | cnt = blkcnt; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1535 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1536 | ccb[0] = ATAPI_CMD_READ_12; |
| 1537 | ccb[1] = 0; /* reserved */ |
| 1538 | ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */ |
| 1539 | ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */ |
| 1540 | ccb[4] = (unsigned char) (blknr >> 8) & 0xFF; |
| 1541 | ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */ |
| 1542 | ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */ |
| 1543 | ccb[7] = (unsigned char) (cnt >> 16) & 0xFF; |
| 1544 | ccb[8] = (unsigned char) (cnt >> 8) & 0xFF; |
| 1545 | ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */ |
| 1546 | ccb[10] = 0; /* reserved */ |
| 1547 | ccb[11] = 0; /* reserved */ |
| 1548 | |
| 1549 | if (atapi_issue_autoreq(device, ccb, 12, |
| 1550 | (unsigned char *) buffer, |
| 1551 | cnt * ATAPI_READ_BLOCK_SIZE) |
| 1552 | == 0xFF) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1553 | return (n); |
| 1554 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1555 | n += cnt; |
| 1556 | blkcnt -= cnt; |
| 1557 | blknr += cnt; |
| 1558 | buffer += (cnt * ATAPI_READ_BLOCK_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1559 | } while (blkcnt > 0); |
| 1560 | return (n); |
| 1561 | } |
| 1562 | |
| 1563 | /* ------------------------------------------------------------------------- */ |
| 1564 | |
| 1565 | #endif /* CONFIG_ATAPI */ |
| 1566 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1567 | U_BOOT_CMD(ide, 5, 1, do_ide, |
| 1568 | "IDE sub-system", |
| 1569 | "reset - reset IDE controller\n" |
| 1570 | "ide info - show available IDE devices\n" |
| 1571 | "ide device [dev] - show or set current device\n" |
| 1572 | "ide part [dev] - print partition table of one or all IDE devices\n" |
| 1573 | "ide read addr blk# cnt\n" |
| 1574 | "ide write addr blk# cnt - read/write `cnt'" |
| 1575 | " blocks starting at block `blk#'\n" |
| 1576 | " to/from memory address `addr'"); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1577 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1578 | U_BOOT_CMD(diskboot, 3, 1, do_diskboot, |
| 1579 | "boot from IDE device", "loadAddr dev:part"); |