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