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