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