1 /* 2 * (C) Copyright 2000-2011 3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <common.h> 9 #include <ata.h> 10 #include <dm.h> 11 #include <ide.h> 12 #include <watchdog.h> 13 #include <asm/io.h> 14 15 #ifdef __PPC__ 16 # define EIEIO __asm__ volatile ("eieio") 17 # define SYNC __asm__ volatile ("sync") 18 #else 19 # define EIEIO /* nothing */ 20 # define SYNC /* nothing */ 21 #endif 22 23 /* Current offset for IDE0 / IDE1 bus access */ 24 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = { 25 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET) 26 CONFIG_SYS_ATA_IDE0_OFFSET, 27 #endif 28 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1) 29 CONFIG_SYS_ATA_IDE1_OFFSET, 30 #endif 31 }; 32 33 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS]; 34 35 struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE]; 36 37 #define IDE_TIME_OUT 2000 /* 2 sec timeout */ 38 39 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */ 40 41 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */ 42 43 #ifndef CONFIG_SYS_ATA_PORT_ADDR 44 #define CONFIG_SYS_ATA_PORT_ADDR(port) (port) 45 #endif 46 47 #ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */ 48 # define DEVICE_LED(x) 0 49 # define LED_IDE1 1 50 # define LED_IDE2 2 51 #endif 52 53 #ifdef CONFIG_IDE_RESET 54 extern void ide_set_reset(int idereset); 55 56 static void ide_reset(void) 57 { 58 int i; 59 60 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i) 61 ide_bus_ok[i] = 0; 62 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) 63 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN; 64 65 ide_set_reset(1); /* assert reset */ 66 67 /* the reset signal shall be asserted for et least 25 us */ 68 udelay(25); 69 70 WATCHDOG_RESET(); 71 72 /* de-assert RESET signal */ 73 ide_set_reset(0); 74 75 /* wait 250 ms */ 76 for (i = 0; i < 250; ++i) 77 udelay(1000); 78 } 79 #else 80 #define ide_reset() /* dummy */ 81 #endif /* CONFIG_IDE_RESET */ 82 83 /* 84 * Wait until Busy bit is off, or timeout (in ms) 85 * Return last status 86 */ 87 static uchar ide_wait(int dev, ulong t) 88 { 89 ulong delay = 10 * t; /* poll every 100 us */ 90 uchar c; 91 92 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) { 93 udelay(100); 94 if (delay-- == 0) 95 break; 96 } 97 return c; 98 } 99 100 /* 101 * copy src to dest, skipping leading and trailing blanks and null 102 * terminate the string 103 * "len" is the size of available memory including the terminating '\0' 104 */ 105 static void ident_cpy(unsigned char *dst, unsigned char *src, 106 unsigned int len) 107 { 108 unsigned char *end, *last; 109 110 last = dst; 111 end = src + len - 1; 112 113 /* reserve space for '\0' */ 114 if (len < 2) 115 goto OUT; 116 117 /* skip leading white space */ 118 while ((*src) && (src < end) && (*src == ' ')) 119 ++src; 120 121 /* copy string, omitting trailing white space */ 122 while ((*src) && (src < end)) { 123 *dst++ = *src; 124 if (*src++ != ' ') 125 last = dst; 126 } 127 OUT: 128 *last = '\0'; 129 } 130 131 #ifdef CONFIG_ATAPI 132 /**************************************************************************** 133 * ATAPI Support 134 */ 135 136 #if defined(CONFIG_IDE_SWAP_IO) 137 /* since ATAPI may use commands with not 4 bytes alligned length 138 * we have our own transfer functions, 2 bytes alligned */ 139 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts) 140 { 141 ushort *dbuf; 142 volatile ushort *pbuf; 143 144 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG); 145 dbuf = (ushort *)sect_buf; 146 147 debug("in output data shorts base for read is %lx\n", 148 (unsigned long) pbuf); 149 150 while (shorts--) { 151 EIEIO; 152 *pbuf = *dbuf++; 153 } 154 } 155 156 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts) 157 { 158 ushort *dbuf; 159 volatile ushort *pbuf; 160 161 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG); 162 dbuf = (ushort *)sect_buf; 163 164 debug("in input data shorts base for read is %lx\n", 165 (unsigned long) pbuf); 166 167 while (shorts--) { 168 EIEIO; 169 *dbuf++ = *pbuf; 170 } 171 } 172 173 #else /* ! CONFIG_IDE_SWAP_IO */ 174 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts) 175 { 176 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts); 177 } 178 179 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts) 180 { 181 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts); 182 } 183 184 #endif /* CONFIG_IDE_SWAP_IO */ 185 186 /* 187 * Wait until (Status & mask) == res, or timeout (in ms) 188 * Return last status 189 * This is used since some ATAPI CD ROMs clears their Busy Bit first 190 * and then they set their DRQ Bit 191 */ 192 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res) 193 { 194 ulong delay = 10 * t; /* poll every 100 us */ 195 uchar c; 196 197 /* prevents to read the status before valid */ 198 c = ide_inb(dev, ATA_DEV_CTL); 199 200 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) { 201 /* break if error occurs (doesn't make sense to wait more) */ 202 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) 203 break; 204 udelay(100); 205 if (delay-- == 0) 206 break; 207 } 208 return c; 209 } 210 211 /* 212 * issue an atapi command 213 */ 214 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen, 215 unsigned char *buffer, int buflen) 216 { 217 unsigned char c, err, mask, res; 218 int n; 219 220 ide_led(DEVICE_LED(device), 1); /* LED on */ 221 222 /* Select device 223 */ 224 mask = ATA_STAT_BUSY | ATA_STAT_DRQ; 225 res = 0; 226 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); 227 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); 228 if ((c & mask) != res) { 229 printf("ATAPI_ISSUE: device %d not ready status %X\n", device, 230 c); 231 err = 0xFF; 232 goto AI_OUT; 233 } 234 /* write taskfile */ 235 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */ 236 ide_outb(device, ATA_SECT_CNT, 0); 237 ide_outb(device, ATA_SECT_NUM, 0); 238 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF)); 239 ide_outb(device, ATA_CYL_HIGH, 240 (unsigned char) ((buflen >> 8) & 0xFF)); 241 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); 242 243 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET); 244 udelay(50); 245 246 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR; 247 res = ATA_STAT_DRQ; 248 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); 249 250 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */ 251 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n", 252 device, c); 253 err = 0xFF; 254 goto AI_OUT; 255 } 256 257 /* write command block */ 258 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2); 259 260 /* ATAPI Command written wait for completition */ 261 udelay(5000); /* device must set bsy */ 262 263 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR; 264 /* 265 * if no data wait for DRQ = 0 BSY = 0 266 * if data wait for DRQ = 1 BSY = 0 267 */ 268 res = 0; 269 if (buflen) 270 res = ATA_STAT_DRQ; 271 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); 272 if ((c & mask) != res) { 273 if (c & ATA_STAT_ERR) { 274 err = (ide_inb(device, ATA_ERROR_REG)) >> 4; 275 debug("atapi_issue 1 returned sense key %X status %02X\n", 276 err, c); 277 } else { 278 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", 279 ccb[0], c); 280 err = 0xFF; 281 } 282 goto AI_OUT; 283 } 284 n = ide_inb(device, ATA_CYL_HIGH); 285 n <<= 8; 286 n += ide_inb(device, ATA_CYL_LOW); 287 if (n > buflen) { 288 printf("ERROR, transfer bytes %d requested only %d\n", n, 289 buflen); 290 err = 0xff; 291 goto AI_OUT; 292 } 293 if ((n == 0) && (buflen < 0)) { 294 printf("ERROR, transfer bytes %d requested %d\n", n, buflen); 295 err = 0xff; 296 goto AI_OUT; 297 } 298 if (n != buflen) { 299 debug("WARNING, transfer bytes %d not equal with requested %d\n", 300 n, buflen); 301 } 302 if (n != 0) { /* data transfer */ 303 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n); 304 /* we transfer shorts */ 305 n >>= 1; 306 /* ok now decide if it is an in or output */ 307 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) { 308 debug("Write to device\n"); 309 ide_output_data_shorts(device, (unsigned short *)buffer, 310 n); 311 } else { 312 debug("Read from device @ %p shorts %d\n", buffer, n); 313 ide_input_data_shorts(device, (unsigned short *)buffer, 314 n); 315 } 316 } 317 udelay(5000); /* seems that some CD ROMs need this... */ 318 mask = ATA_STAT_BUSY | ATA_STAT_ERR; 319 res = 0; 320 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); 321 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) { 322 err = (ide_inb(device, ATA_ERROR_REG) >> 4); 323 debug("atapi_issue 2 returned sense key %X status %X\n", err, 324 c); 325 } else { 326 err = 0; 327 } 328 AI_OUT: 329 ide_led(DEVICE_LED(device), 0); /* LED off */ 330 return err; 331 } 332 333 /* 334 * sending the command to atapi_issue. If an status other than good 335 * returns, an request_sense will be issued 336 */ 337 338 #define ATAPI_DRIVE_NOT_READY 100 339 #define ATAPI_UNIT_ATTN 10 340 341 unsigned char atapi_issue_autoreq(int device, 342 unsigned char *ccb, 343 int ccblen, 344 unsigned char *buffer, int buflen) 345 { 346 unsigned char sense_data[18], sense_ccb[12]; 347 unsigned char res, key, asc, ascq; 348 int notready, unitattn; 349 350 unitattn = ATAPI_UNIT_ATTN; 351 notready = ATAPI_DRIVE_NOT_READY; 352 353 retry: 354 res = atapi_issue(device, ccb, ccblen, buffer, buflen); 355 if (res == 0) 356 return 0; /* Ok */ 357 358 if (res == 0xFF) 359 return 0xFF; /* error */ 360 361 debug("(auto_req)atapi_issue returned sense key %X\n", res); 362 363 memset(sense_ccb, 0, sizeof(sense_ccb)); 364 memset(sense_data, 0, sizeof(sense_data)); 365 sense_ccb[0] = ATAPI_CMD_REQ_SENSE; 366 sense_ccb[4] = 18; /* allocation Length */ 367 368 res = atapi_issue(device, sense_ccb, 12, sense_data, 18); 369 key = (sense_data[2] & 0xF); 370 asc = (sense_data[12]); 371 ascq = (sense_data[13]); 372 373 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res); 374 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n", 375 sense_data[0], key, asc, ascq); 376 377 if ((key == 0)) 378 return 0; /* ok device ready */ 379 380 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */ 381 if (unitattn-- > 0) { 382 udelay(200 * 1000); 383 goto retry; 384 } 385 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN); 386 goto error; 387 } 388 if ((asc == 0x4) && (ascq == 0x1)) { 389 /* not ready, but will be ready soon */ 390 if (notready-- > 0) { 391 udelay(200 * 1000); 392 goto retry; 393 } 394 printf("Drive not ready, tried %d times\n", 395 ATAPI_DRIVE_NOT_READY); 396 goto error; 397 } 398 if (asc == 0x3a) { 399 debug("Media not present\n"); 400 goto error; 401 } 402 403 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc, 404 ascq); 405 error: 406 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq); 407 return 0xFF; 408 } 409 410 /* 411 * atapi_read: 412 * we transfer only one block per command, since the multiple DRQ per 413 * command is not yet implemented 414 */ 415 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */ 416 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */ 417 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE) 418 419 ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, 420 void *buffer) 421 { 422 int device = block_dev->devnum; 423 ulong n = 0; 424 unsigned char ccb[12]; /* Command descriptor block */ 425 ulong cnt; 426 427 debug("atapi_read dev %d start " LBAF " blocks " LBAF 428 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer); 429 430 do { 431 if (blkcnt > ATAPI_READ_MAX_BLOCK) 432 cnt = ATAPI_READ_MAX_BLOCK; 433 else 434 cnt = blkcnt; 435 436 ccb[0] = ATAPI_CMD_READ_12; 437 ccb[1] = 0; /* reserved */ 438 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */ 439 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */ 440 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF; 441 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */ 442 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */ 443 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF; 444 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF; 445 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */ 446 ccb[10] = 0; /* reserved */ 447 ccb[11] = 0; /* reserved */ 448 449 if (atapi_issue_autoreq(device, ccb, 12, 450 (unsigned char *)buffer, 451 cnt * ATAPI_READ_BLOCK_SIZE) 452 == 0xFF) { 453 return n; 454 } 455 n += cnt; 456 blkcnt -= cnt; 457 blknr += cnt; 458 buffer += (cnt * ATAPI_READ_BLOCK_SIZE); 459 } while (blkcnt > 0); 460 return n; 461 } 462 463 static void atapi_inquiry(struct blk_desc *dev_desc) 464 { 465 unsigned char ccb[12]; /* Command descriptor block */ 466 unsigned char iobuf[64]; /* temp buf */ 467 unsigned char c; 468 int device; 469 470 device = dev_desc->devnum; 471 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */ 472 #ifndef CONFIG_BLK 473 dev_desc->block_read = atapi_read; 474 #endif 475 476 memset(ccb, 0, sizeof(ccb)); 477 memset(iobuf, 0, sizeof(iobuf)); 478 479 ccb[0] = ATAPI_CMD_INQUIRY; 480 ccb[4] = 40; /* allocation Legnth */ 481 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40); 482 483 debug("ATAPI_CMD_INQUIRY returned %x\n", c); 484 if (c != 0) 485 return; 486 487 /* copy device ident strings */ 488 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8); 489 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16); 490 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5); 491 492 dev_desc->lun = 0; 493 dev_desc->lba = 0; 494 dev_desc->blksz = 0; 495 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz)); 496 dev_desc->type = iobuf[0] & 0x1f; 497 498 if ((iobuf[1] & 0x80) == 0x80) 499 dev_desc->removable = 1; 500 else 501 dev_desc->removable = 0; 502 503 memset(ccb, 0, sizeof(ccb)); 504 memset(iobuf, 0, sizeof(iobuf)); 505 ccb[0] = ATAPI_CMD_START_STOP; 506 ccb[4] = 0x03; /* start */ 507 508 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0); 509 510 debug("ATAPI_CMD_START_STOP returned %x\n", c); 511 if (c != 0) 512 return; 513 514 memset(ccb, 0, sizeof(ccb)); 515 memset(iobuf, 0, sizeof(iobuf)); 516 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0); 517 518 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c); 519 if (c != 0) 520 return; 521 522 memset(ccb, 0, sizeof(ccb)); 523 memset(iobuf, 0, sizeof(iobuf)); 524 ccb[0] = ATAPI_CMD_READ_CAP; 525 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8); 526 debug("ATAPI_CMD_READ_CAP returned %x\n", c); 527 if (c != 0) 528 return; 529 530 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n", 531 iobuf[0], iobuf[1], iobuf[2], iobuf[3], 532 iobuf[4], iobuf[5], iobuf[6], iobuf[7]); 533 534 dev_desc->lba = ((unsigned long) iobuf[0] << 24) + 535 ((unsigned long) iobuf[1] << 16) + 536 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]); 537 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) + 538 ((unsigned long) iobuf[5] << 16) + 539 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]); 540 dev_desc->log2blksz = LOG2(dev_desc->blksz); 541 #ifdef CONFIG_LBA48 542 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */ 543 dev_desc->lba48 = 0; 544 #endif 545 return; 546 } 547 548 #endif /* CONFIG_ATAPI */ 549 550 static void ide_ident(struct blk_desc *dev_desc) 551 { 552 unsigned char c; 553 hd_driveid_t iop; 554 555 #ifdef CONFIG_ATAPI 556 int retries = 0; 557 #endif 558 int device; 559 560 device = dev_desc->devnum; 561 printf(" Device %d: ", device); 562 563 ide_led(DEVICE_LED(device), 1); /* LED on */ 564 /* Select device 565 */ 566 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); 567 dev_desc->if_type = IF_TYPE_IDE; 568 #ifdef CONFIG_ATAPI 569 570 retries = 0; 571 572 /* Warning: This will be tricky to read */ 573 while (retries <= 1) { 574 /* check signature */ 575 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) && 576 (ide_inb(device, ATA_SECT_NUM) == 0x01) && 577 (ide_inb(device, ATA_CYL_LOW) == 0x14) && 578 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) { 579 /* ATAPI Signature found */ 580 dev_desc->if_type = IF_TYPE_ATAPI; 581 /* 582 * Start Ident Command 583 */ 584 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT); 585 /* 586 * Wait for completion - ATAPI devices need more time 587 * to become ready 588 */ 589 c = ide_wait(device, ATAPI_TIME_OUT); 590 } else 591 #endif 592 { 593 /* 594 * Start Ident Command 595 */ 596 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT); 597 598 /* 599 * Wait for completion 600 */ 601 c = ide_wait(device, IDE_TIME_OUT); 602 } 603 ide_led(DEVICE_LED(device), 0); /* LED off */ 604 605 if (((c & ATA_STAT_DRQ) == 0) || 606 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) { 607 #ifdef CONFIG_ATAPI 608 { 609 /* 610 * Need to soft reset the device 611 * in case it's an ATAPI... 612 */ 613 debug("Retrying...\n"); 614 ide_outb(device, ATA_DEV_HD, 615 ATA_LBA | ATA_DEVICE(device)); 616 udelay(100000); 617 ide_outb(device, ATA_COMMAND, 0x08); 618 udelay(500000); /* 500 ms */ 619 } 620 /* 621 * Select device 622 */ 623 ide_outb(device, ATA_DEV_HD, 624 ATA_LBA | ATA_DEVICE(device)); 625 retries++; 626 #else 627 return; 628 #endif 629 } 630 #ifdef CONFIG_ATAPI 631 else 632 break; 633 } /* see above - ugly to read */ 634 635 if (retries == 2) /* Not found */ 636 return; 637 #endif 638 639 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS); 640 641 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev, 642 sizeof(dev_desc->revision)); 643 ident_cpy((unsigned char *)dev_desc->vendor, iop.model, 644 sizeof(dev_desc->vendor)); 645 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no, 646 sizeof(dev_desc->product)); 647 #ifdef __LITTLE_ENDIAN 648 /* 649 * firmware revision, model, and serial number have Big Endian Byte 650 * order in Word. Convert all three to little endian. 651 * 652 * See CF+ and CompactFlash Specification Revision 2.0: 653 * 6.2.1.6: Identify Drive, Table 39 for more details 654 */ 655 656 strswab(dev_desc->revision); 657 strswab(dev_desc->vendor); 658 strswab(dev_desc->product); 659 #endif /* __LITTLE_ENDIAN */ 660 661 if ((iop.config & 0x0080) == 0x0080) 662 dev_desc->removable = 1; 663 else 664 dev_desc->removable = 0; 665 666 #ifdef CONFIG_ATAPI 667 if (dev_desc->if_type == IF_TYPE_ATAPI) { 668 atapi_inquiry(dev_desc); 669 return; 670 } 671 #endif /* CONFIG_ATAPI */ 672 673 #ifdef __BIG_ENDIAN 674 /* swap shorts */ 675 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16); 676 #else /* ! __BIG_ENDIAN */ 677 /* 678 * do not swap shorts on little endian 679 * 680 * See CF+ and CompactFlash Specification Revision 2.0: 681 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details. 682 */ 683 dev_desc->lba = iop.lba_capacity; 684 #endif /* __BIG_ENDIAN */ 685 686 #ifdef CONFIG_LBA48 687 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */ 688 dev_desc->lba48 = 1; 689 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] | 690 ((unsigned long long) iop.lba48_capacity[1] << 16) | 691 ((unsigned long long) iop.lba48_capacity[2] << 32) | 692 ((unsigned long long) iop.lba48_capacity[3] << 48); 693 } else { 694 dev_desc->lba48 = 0; 695 } 696 #endif /* CONFIG_LBA48 */ 697 /* assuming HD */ 698 dev_desc->type = DEV_TYPE_HARDDISK; 699 dev_desc->blksz = ATA_BLOCKSIZE; 700 dev_desc->log2blksz = LOG2(dev_desc->blksz); 701 dev_desc->lun = 0; /* just to fill something in... */ 702 703 #if 0 /* only used to test the powersaving mode, 704 * if enabled, the drive goes after 5 sec 705 * in standby mode */ 706 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); 707 c = ide_wait(device, IDE_TIME_OUT); 708 ide_outb(device, ATA_SECT_CNT, 1); 709 ide_outb(device, ATA_LBA_LOW, 0); 710 ide_outb(device, ATA_LBA_MID, 0); 711 ide_outb(device, ATA_LBA_HIGH, 0); 712 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); 713 ide_outb(device, ATA_COMMAND, 0xe3); 714 udelay(50); 715 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */ 716 #endif 717 } 718 719 __weak void ide_led(uchar led, uchar status) 720 { 721 #if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */ 722 static uchar led_buffer; /* Buffer for current LED status */ 723 724 uchar *led_port = LED_PORT; 725 726 if (status) /* switch LED on */ 727 led_buffer |= led; 728 else /* switch LED off */ 729 led_buffer &= ~led; 730 731 *led_port = led_buffer; 732 #endif 733 } 734 735 __weak void ide_outb(int dev, int port, unsigned char val) 736 { 737 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n", 738 dev, port, val, 739 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port))); 740 741 #if defined(CONFIG_IDE_AHB) 742 if (port) { 743 /* write command */ 744 ide_write_register(dev, port, val); 745 } else { 746 /* write data */ 747 outb(val, (ATA_CURR_BASE(dev))); 748 } 749 #else 750 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port))); 751 #endif 752 } 753 754 __weak unsigned char ide_inb(int dev, int port) 755 { 756 uchar val; 757 758 #if defined(CONFIG_IDE_AHB) 759 val = ide_read_register(dev, port); 760 #else 761 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port))); 762 #endif 763 764 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n", 765 dev, port, 766 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val); 767 return val; 768 } 769 770 void ide_init(void) 771 { 772 unsigned char c; 773 int i, bus; 774 775 #ifdef CONFIG_IDE_PREINIT 776 WATCHDOG_RESET(); 777 778 if (ide_preinit()) { 779 puts("ide_preinit failed\n"); 780 return; 781 } 782 #endif /* CONFIG_IDE_PREINIT */ 783 784 WATCHDOG_RESET(); 785 786 /* 787 * Reset the IDE just to be sure. 788 * Light LED's to show 789 */ 790 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */ 791 792 /* ATAPI Drives seems to need a proper IDE Reset */ 793 ide_reset(); 794 795 /* 796 * Wait for IDE to get ready. 797 * According to spec, this can take up to 31 seconds! 798 */ 799 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) { 800 int dev = 801 bus * (CONFIG_SYS_IDE_MAXDEVICE / 802 CONFIG_SYS_IDE_MAXBUS); 803 804 printf("Bus %d: ", bus); 805 806 ide_bus_ok[bus] = 0; 807 808 /* Select device 809 */ 810 udelay(100000); /* 100 ms */ 811 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev)); 812 udelay(100000); /* 100 ms */ 813 i = 0; 814 do { 815 udelay(10000); /* 10 ms */ 816 817 c = ide_inb(dev, ATA_STATUS); 818 i++; 819 if (i > (ATA_RESET_TIME * 100)) { 820 puts("** Timeout **\n"); 821 /* LED's off */ 822 ide_led((LED_IDE1 | LED_IDE2), 0); 823 return; 824 } 825 if ((i >= 100) && ((i % 100) == 0)) 826 putc('.'); 827 828 } while (c & ATA_STAT_BUSY); 829 830 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) { 831 puts("not available "); 832 debug("Status = 0x%02X ", c); 833 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */ 834 } else if ((c & ATA_STAT_READY) == 0) { 835 puts("not available "); 836 debug("Status = 0x%02X ", c); 837 #endif 838 } else { 839 puts("OK "); 840 ide_bus_ok[bus] = 1; 841 } 842 WATCHDOG_RESET(); 843 } 844 845 putc('\n'); 846 847 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */ 848 849 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) { 850 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2; 851 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN; 852 ide_dev_desc[i].if_type = IF_TYPE_IDE; 853 ide_dev_desc[i].devnum = i; 854 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN; 855 ide_dev_desc[i].blksz = 0; 856 ide_dev_desc[i].log2blksz = 857 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz)); 858 ide_dev_desc[i].lba = 0; 859 #ifndef CONFIG_BLK 860 ide_dev_desc[i].block_read = ide_read; 861 ide_dev_desc[i].block_write = ide_write; 862 #endif 863 if (!ide_bus_ok[IDE_BUS(i)]) 864 continue; 865 ide_led(led, 1); /* LED on */ 866 ide_ident(&ide_dev_desc[i]); 867 ide_led(led, 0); /* LED off */ 868 dev_print(&ide_dev_desc[i]); 869 870 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) { 871 /* initialize partition type */ 872 part_init(&ide_dev_desc[i]); 873 } 874 } 875 WATCHDOG_RESET(); 876 } 877 878 /* We only need to swap data if we are running on a big endian cpu. */ 879 #if defined(__LITTLE_ENDIAN) 880 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words) 881 { 882 ide_input_data(dev, sect_buf, words); 883 } 884 #else 885 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words) 886 { 887 volatile ushort *pbuf = 888 (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG); 889 ushort *dbuf = (ushort *)sect_buf; 890 891 debug("in input swap data base for read is %lx\n", 892 (unsigned long) pbuf); 893 894 while (words--) { 895 #ifdef __MIPS__ 896 *dbuf++ = swab16p((u16 *)pbuf); 897 *dbuf++ = swab16p((u16 *)pbuf); 898 #else 899 *dbuf++ = ld_le16(pbuf); 900 *dbuf++ = ld_le16(pbuf); 901 #endif /* !MIPS */ 902 } 903 } 904 #endif /* __LITTLE_ENDIAN */ 905 906 907 #if defined(CONFIG_IDE_SWAP_IO) 908 __weak void ide_output_data(int dev, const ulong *sect_buf, int words) 909 { 910 ushort *dbuf; 911 volatile ushort *pbuf; 912 913 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG); 914 dbuf = (ushort *)sect_buf; 915 while (words--) { 916 EIEIO; 917 *pbuf = *dbuf++; 918 EIEIO; 919 *pbuf = *dbuf++; 920 } 921 } 922 #else /* ! CONFIG_IDE_SWAP_IO */ 923 __weak void ide_output_data(int dev, const ulong *sect_buf, int words) 924 { 925 #if defined(CONFIG_IDE_AHB) 926 ide_write_data(dev, sect_buf, words); 927 #else 928 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1); 929 #endif 930 } 931 #endif /* CONFIG_IDE_SWAP_IO */ 932 933 #if defined(CONFIG_IDE_SWAP_IO) 934 __weak void ide_input_data(int dev, ulong *sect_buf, int words) 935 { 936 ushort *dbuf; 937 volatile ushort *pbuf; 938 939 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG); 940 dbuf = (ushort *)sect_buf; 941 942 debug("in input data base for read is %lx\n", (unsigned long) pbuf); 943 944 while (words--) { 945 EIEIO; 946 *dbuf++ = *pbuf; 947 EIEIO; 948 *dbuf++ = *pbuf; 949 } 950 } 951 #else /* ! CONFIG_IDE_SWAP_IO */ 952 __weak void ide_input_data(int dev, ulong *sect_buf, int words) 953 { 954 #if defined(CONFIG_IDE_AHB) 955 ide_read_data(dev, sect_buf, words); 956 #else 957 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1); 958 #endif 959 } 960 961 #endif /* CONFIG_IDE_SWAP_IO */ 962 963 #ifdef CONFIG_BLK 964 ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, 965 void *buffer) 966 #else 967 ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, 968 void *buffer) 969 #endif 970 { 971 #ifdef CONFIG_BLK 972 struct blk_desc *block_dev = dev_get_uclass_platdata(dev); 973 #endif 974 int device = block_dev->devnum; 975 ulong n = 0; 976 unsigned char c; 977 unsigned char pwrsave = 0; /* power save */ 978 979 #ifdef CONFIG_LBA48 980 unsigned char lba48 = 0; 981 982 if (blknr & 0x0000fffff0000000ULL) { 983 /* more than 28 bits used, use 48bit mode */ 984 lba48 = 1; 985 } 986 #endif 987 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n", 988 device, blknr, blkcnt, (ulong) buffer); 989 990 ide_led(DEVICE_LED(device), 1); /* LED on */ 991 992 /* Select device 993 */ 994 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); 995 c = ide_wait(device, IDE_TIME_OUT); 996 997 if (c & ATA_STAT_BUSY) { 998 printf("IDE read: device %d not ready\n", device); 999 goto IDE_READ_E; 1000 } 1001 1002 /* first check if the drive is in Powersaving mode, if yes, 1003 * increase the timeout value */ 1004 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR); 1005 udelay(50); 1006 1007 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */ 1008 1009 if (c & ATA_STAT_BUSY) { 1010 printf("IDE read: device %d not ready\n", device); 1011 goto IDE_READ_E; 1012 } 1013 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) { 1014 printf("No Powersaving mode %X\n", c); 1015 } else { 1016 c = ide_inb(device, ATA_SECT_CNT); 1017 debug("Powersaving %02X\n", c); 1018 if (c == 0) 1019 pwrsave = 1; 1020 } 1021 1022 1023 while (blkcnt-- > 0) { 1024 c = ide_wait(device, IDE_TIME_OUT); 1025 1026 if (c & ATA_STAT_BUSY) { 1027 printf("IDE read: device %d not ready\n", device); 1028 break; 1029 } 1030 #ifdef CONFIG_LBA48 1031 if (lba48) { 1032 /* write high bits */ 1033 ide_outb(device, ATA_SECT_CNT, 0); 1034 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF); 1035 #ifdef CONFIG_SYS_64BIT_LBA 1036 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF); 1037 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF); 1038 #else 1039 ide_outb(device, ATA_LBA_MID, 0); 1040 ide_outb(device, ATA_LBA_HIGH, 0); 1041 #endif 1042 } 1043 #endif 1044 ide_outb(device, ATA_SECT_CNT, 1); 1045 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF); 1046 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF); 1047 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF); 1048 1049 #ifdef CONFIG_LBA48 1050 if (lba48) { 1051 ide_outb(device, ATA_DEV_HD, 1052 ATA_LBA | ATA_DEVICE(device)); 1053 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT); 1054 1055 } else 1056 #endif 1057 { 1058 ide_outb(device, ATA_DEV_HD, ATA_LBA | 1059 ATA_DEVICE(device) | ((blknr >> 24) & 0xF)); 1060 ide_outb(device, ATA_COMMAND, ATA_CMD_READ); 1061 } 1062 1063 udelay(50); 1064 1065 if (pwrsave) { 1066 /* may take up to 4 sec */ 1067 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT); 1068 pwrsave = 0; 1069 } else { 1070 /* can't take over 500 ms */ 1071 c = ide_wait(device, IDE_TIME_OUT); 1072 } 1073 1074 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) != 1075 ATA_STAT_DRQ) { 1076 printf("Error (no IRQ) dev %d blk " LBAF 1077 ": status %#02x\n", device, blknr, c); 1078 break; 1079 } 1080 1081 ide_input_data(device, buffer, ATA_SECTORWORDS); 1082 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */ 1083 1084 ++n; 1085 ++blknr; 1086 buffer += ATA_BLOCKSIZE; 1087 } 1088 IDE_READ_E: 1089 ide_led(DEVICE_LED(device), 0); /* LED off */ 1090 return n; 1091 } 1092 1093 #ifdef CONFIG_BLK 1094 ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, 1095 const void *buffer) 1096 #else 1097 ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, 1098 const void *buffer) 1099 #endif 1100 { 1101 #ifdef CONFIG_BLK 1102 struct blk_desc *block_dev = dev_get_uclass_platdata(dev); 1103 #endif 1104 int device = block_dev->devnum; 1105 ulong n = 0; 1106 unsigned char c; 1107 1108 #ifdef CONFIG_LBA48 1109 unsigned char lba48 = 0; 1110 1111 if (blknr & 0x0000fffff0000000ULL) { 1112 /* more than 28 bits used, use 48bit mode */ 1113 lba48 = 1; 1114 } 1115 #endif 1116 1117 ide_led(DEVICE_LED(device), 1); /* LED on */ 1118 1119 /* Select device 1120 */ 1121 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); 1122 1123 while (blkcnt-- > 0) { 1124 c = ide_wait(device, IDE_TIME_OUT); 1125 1126 if (c & ATA_STAT_BUSY) { 1127 printf("IDE read: device %d not ready\n", device); 1128 goto WR_OUT; 1129 } 1130 #ifdef CONFIG_LBA48 1131 if (lba48) { 1132 /* write high bits */ 1133 ide_outb(device, ATA_SECT_CNT, 0); 1134 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF); 1135 #ifdef CONFIG_SYS_64BIT_LBA 1136 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF); 1137 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF); 1138 #else 1139 ide_outb(device, ATA_LBA_MID, 0); 1140 ide_outb(device, ATA_LBA_HIGH, 0); 1141 #endif 1142 } 1143 #endif 1144 ide_outb(device, ATA_SECT_CNT, 1); 1145 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF); 1146 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF); 1147 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF); 1148 1149 #ifdef CONFIG_LBA48 1150 if (lba48) { 1151 ide_outb(device, ATA_DEV_HD, 1152 ATA_LBA | ATA_DEVICE(device)); 1153 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT); 1154 1155 } else 1156 #endif 1157 { 1158 ide_outb(device, ATA_DEV_HD, ATA_LBA | 1159 ATA_DEVICE(device) | ((blknr >> 24) & 0xF)); 1160 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE); 1161 } 1162 1163 udelay(50); 1164 1165 /* can't take over 500 ms */ 1166 c = ide_wait(device, IDE_TIME_OUT); 1167 1168 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) != 1169 ATA_STAT_DRQ) { 1170 printf("Error (no IRQ) dev %d blk " LBAF 1171 ": status %#02x\n", device, blknr, c); 1172 goto WR_OUT; 1173 } 1174 1175 ide_output_data(device, buffer, ATA_SECTORWORDS); 1176 c = ide_inb(device, ATA_STATUS); /* clear IRQ */ 1177 ++n; 1178 ++blknr; 1179 buffer += ATA_BLOCKSIZE; 1180 } 1181 WR_OUT: 1182 ide_led(DEVICE_LED(device), 0); /* LED off */ 1183 return n; 1184 } 1185 1186 #if defined(CONFIG_OF_IDE_FIXUP) 1187 int ide_device_present(int dev) 1188 { 1189 if (dev >= CONFIG_SYS_IDE_MAXBUS) 1190 return 0; 1191 return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1; 1192 } 1193 #endif 1194 1195 #ifdef CONFIG_BLK 1196 static const struct blk_ops ide_blk_ops = { 1197 .read = ide_read, 1198 .write = ide_write, 1199 }; 1200 1201 U_BOOT_DRIVER(ide_blk) = { 1202 .name = "ide_blk", 1203 .id = UCLASS_BLK, 1204 .ops = &ide_blk_ops, 1205 }; 1206 #else 1207 U_BOOT_LEGACY_BLK(ide) = { 1208 .if_typename = "ide", 1209 .if_type = IF_TYPE_IDE, 1210 .max_devs = CONFIG_SYS_IDE_MAXDEVICE, 1211 .desc = ide_dev_desc, 1212 }; 1213 #endif 1214