1 /* Derived from Etherboot 5.1 */
5 #include <console/console.h>
8 #include <device/device.h>
9 #include <device/pci.h>
11 #include <arch/byteorder.h>
13 #define BSY_SET_DURING_SPINUP 1
15 static unsigned short ide_base[] = {
23 static struct controller controllers[IDE_MAX_CONTROLLERS];
24 static struct harddisk_info harddisk_info[IDE_MAX_DRIVES];
26 static unsigned char ide_buffer[IDE_SECTOR_SIZE];
28 static int await_ide(int (*done)(struct controller *ctrl),
29 struct controller *ctrl, unsigned long timeout)
37 //poll_interruptions();
41 udelay(1000); /* Added to avoid spinning GRW */
43 printk_info("IDE time out\n");
47 /* The maximum time any IDE command can last 31 seconds,
48 * So if any IDE commands takes this long we know we have problems.
50 #define IDE_TIMEOUT (32*1000)
52 static int not_bsy(struct controller *ctrl)
54 return !(inb(IDE_REG_STATUS(ctrl)) & IDE_STATUS_BSY);
57 /* IDE drives assert BSY bit within 400 nsec when SRST is set.
58 * Use 2 msec since our tick is 1 msec */
59 #define IDE_RESET_PULSE 2
61 static int bsy(struct controller *ctrl)
63 return inb(IDE_REG_STATUS(ctrl)) & IDE_STATUS_BSY;
66 #if !BSY_SET_DURING_SPINUP
67 static int timeout(struct controller *ctrl)
73 static void print_status(struct controller *ctrl)
75 printk_debug("IDE: status=%#x, err=%#x\n",
76 inb(IDE_REG_STATUS(ctrl)), inb(IDE_REG_ERROR(ctrl)));
79 static int ide_software_reset(struct controller *ctrl)
81 /* Wait a little bit in case this is immediately after
85 /* A software reset should not be delivered while the bsy bit
86 * is set. If the bsy bit does not clear in a reasonable
87 * amount of time give up.
89 printk_debug("Waiting for ide%d to become ready for reset... ",
91 if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
92 printk_debug("failed\n");
97 /* Disable Interrupts and reset the ide bus */
98 outb(IDE_CTRL_HD15 | IDE_CTRL_SRST | IDE_CTRL_NIEN,
99 IDE_REG_DEVICE_CONTROL(ctrl));
100 /* If BSY bit is not asserted within 400ns, no device there */
101 if (await_ide(bsy, ctrl, IDE_RESET_PULSE) < 0) {
104 outb(IDE_CTRL_HD15 | IDE_CTRL_NIEN, IDE_REG_DEVICE_CONTROL(ctrl));
106 if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
112 static void pio_set_registers(
113 struct controller *ctrl, const struct ide_pio_command *cmd)
116 /* Disable Interrupts */
117 outb(IDE_CTRL_HD15 | IDE_CTRL_NIEN, IDE_REG_DEVICE_CONTROL(ctrl));
119 /* Possibly switch selected device */
120 device = inb(IDE_REG_DEVICE(ctrl));
121 outb(cmd->device, IDE_REG_DEVICE(ctrl));
122 if ((device & (1UL << 4)) != (cmd->device & (1UL << 4))) {
123 /* Allow time for the selected drive to switch,
124 * The linux ide code suggests 50ms is the right
125 * amount of time to use here.
129 outb(cmd->feature, IDE_REG_FEATURE(ctrl));
130 if (cmd->command == IDE_CMD_READ_SECTORS_EXT) {
131 outb(cmd->sector_count2, IDE_REG_SECTOR_COUNT(ctrl));
132 outb(cmd->lba_low2, IDE_REG_LBA_LOW(ctrl));
133 outb(cmd->lba_mid2, IDE_REG_LBA_MID(ctrl));
134 outb(cmd->lba_high2, IDE_REG_LBA_HIGH(ctrl));
136 outb(cmd->sector_count, IDE_REG_SECTOR_COUNT(ctrl));
137 outb(cmd->lba_low, IDE_REG_LBA_LOW(ctrl));
138 outb(cmd->lba_mid, IDE_REG_LBA_MID(ctrl));
139 outb(cmd->lba_high, IDE_REG_LBA_HIGH(ctrl));
140 outb(cmd->command, IDE_REG_COMMAND(ctrl));
144 static int pio_non_data(struct controller *ctrl, const struct ide_pio_command *cmd)
146 /* Wait until the busy bit is clear */
147 if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
151 pio_set_registers(ctrl, cmd);
153 if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
156 /* FIXME is there more error checking I could do here? */
160 static int pio_data_in(struct controller *ctrl,
161 const struct ide_pio_command *cmd,
162 void *buffer, size_t bytes)
166 /* FIXME handle commands with multiple blocks */
167 /* Wait until the busy bit is clear */
168 if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
172 /* How do I tell if INTRQ is asserted? */
173 pio_set_registers(ctrl, cmd);
175 if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
178 status = inb(IDE_REG_STATUS(ctrl));
179 if (!(status & IDE_STATUS_DRQ)) {
183 insw(IDE_REG_DATA(ctrl), buffer, bytes/2);
184 status = inb(IDE_REG_STATUS(ctrl));
185 if (status & IDE_STATUS_DRQ) {
193 static int pio_data_in_sw(struct controller *ctrl,
194 const struct ide_pio_command *cmd,
195 void *buffer, size_t bytes)
199 /* FIXME handle commands with multiple blocks */
200 /* Wait until the busy bit is clear */
201 if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
205 /* How do I tell if INTRQ is asserted? */
206 pio_set_registers(ctrl, cmd);
208 if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
211 status = inb(IDE_REG_STATUS(ctrl));
212 if (!(status & IDE_STATUS_DRQ)) {
216 insw_ns(IDE_REG_DATA(ctrl), buffer, bytes/2);
217 status = inb(IDE_REG_STATUS(ctrl));
218 if (status & IDE_STATUS_DRQ) {
224 #endif /* __BIG_ENDIAN */
226 static int pio_packet(struct harddisk_info *info, int in,
227 const void *packet, int packet_len,
228 void *buffer, int buffer_len)
231 struct ide_pio_command cmd;
233 memset(&cmd, 0, sizeof(cmd));
235 /* Wait until the busy bit is clear */
236 if (await_ide(not_bsy, info->ctrl, IDE_TIMEOUT) < 0) {
240 /* Issue a PACKET command */
241 cmd.lba_mid = (uint8_t) buffer_len;
242 cmd.lba_high = (uint8_t) (buffer_len >> 8);
243 cmd.device = IDE_DH_DEFAULT | info->slave;
244 cmd.command = IDE_CMD_PACKET;
245 pio_set_registers(info->ctrl, &cmd);
247 if (await_ide(not_bsy, info->ctrl, IDE_TIMEOUT) < 0) {
250 status = inb(IDE_REG_STATUS(info->ctrl));
251 if (!(status & IDE_STATUS_DRQ)) {
252 printk_debug("no drq after PACKET\n");
253 print_status(info->ctrl);
257 /* Send the packet */
259 outsw_ns(IDE_REG_DATA(info->ctrl), packet, packet_len/2);
260 #else /* __BIG_ENDIAN */
261 outsw(IDE_REG_DATA(info->ctrl), packet, packet_len/2);
262 #endif /* __BIG_ENDIAN */
264 if (await_ide(not_bsy, info->ctrl, IDE_TIMEOUT) < 0) {
267 status = inb(IDE_REG_STATUS(info->ctrl));
268 if (buffer_len == 0) {
269 if (status & IDE_STATUS_DRQ) {
270 printk_debug("drq after non-data command\n");
271 print_status(info->ctrl);
277 if (!(status & IDE_STATUS_DRQ)) {
278 printk_debug("no drq after sending packet\n");
279 print_status(info->ctrl);
284 insw_ns(IDE_REG_DATA(info->ctrl), buffer, buffer_len/2);
285 #else /* __BIG_ENDIAN */
286 insw(IDE_REG_DATA(info->ctrl), buffer, buffer_len/2);
287 #endif /* __BIG_ENDIAN */
289 status = inb(IDE_REG_STATUS(info->ctrl));
290 if (status & IDE_STATUS_DRQ) {
291 printk_debug("drq after insw\n");
292 print_status(info->ctrl);
298 static inline int ide_read_sector_chs(
299 struct harddisk_info *info, void *buffer, unsigned long sector)
301 struct ide_pio_command cmd;
304 unsigned int cylinder;
306 memset(&cmd, 0, sizeof(cmd));
307 cmd.sector_count = 1;
309 //printk_debug("ide_read_sector_chs: sector= %ld.\n",sector);
311 track = sector / info->sectors_per_track;
313 offset = 1 + (sector % info->sectors_per_track);
314 cylinder = track / info->heads;
315 cmd.lba_low = offset;
316 cmd.lba_mid = cylinder & 0xff;
317 cmd.lba_high = (cylinder >> 8) & 0xff;
318 cmd.device = IDE_DH_DEFAULT |
319 IDE_DH_HEAD(track % info->heads) |
322 cmd.command = IDE_CMD_READ_SECTORS;
324 return pio_data_in_sw(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
325 #else /* __BIG_ENDIAN */
326 return pio_data_in(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
327 #endif /* __BIG_ENDIAN */
330 static inline int ide_read_sector_lba(
331 struct harddisk_info *info, void *buffer, unsigned long sector)
333 struct ide_pio_command cmd;
334 memset(&cmd, 0, sizeof(cmd));
336 cmd.sector_count = 1;
337 cmd.lba_low = sector & 0xff;
338 cmd.lba_mid = (sector >> 8) & 0xff;
339 cmd.lba_high = (sector >> 16) & 0xff;
340 cmd.device = IDE_DH_DEFAULT |
341 ((sector >> 24) & 0x0f) |
344 cmd.command = IDE_CMD_READ_SECTORS;
345 //printk_debug("%s: sector= %ld, device command= 0x%x.\n",__FUNCTION__,(unsigned long) sector, cmd.device);
347 return pio_data_in_sw(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
348 #else /* __BIG_ENDIAN */
349 return pio_data_in(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
350 #endif /* __BIG_ENDIAN */
353 static inline int ide_read_sector_lba48(
354 struct harddisk_info *info, void *buffer, sector_t sector)
356 struct ide_pio_command cmd;
357 memset(&cmd, 0, sizeof(cmd));
358 //printk_debug("ide_read_sector_lba48: sector= %ld.\n",(unsigned long) sector);
360 cmd.sector_count = 1;
361 cmd.lba_low = sector & 0xff;
362 cmd.lba_mid = (sector >> 8) & 0xff;
363 cmd.lba_high = (sector >> 16) & 0xff;
364 cmd.lba_low2 = (sector >> 24) & 0xff;
365 cmd.lba_mid2 = (sector >> 32) & 0xff;
366 cmd.lba_high2 = (sector >> 40) & 0xff;
367 cmd.device = info->slave | IDE_DH_LBA;
368 cmd.command = IDE_CMD_READ_SECTORS_EXT;
370 return pio_data_in_sw(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
371 #else /* __BIG_ENDIAN */
372 return pio_data_in(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
373 #endif /* __BIG_ENDIAN */
376 static inline int ide_read_sector_packet(
377 struct harddisk_info *info, void *buffer, sector_t sector)
380 static uint8_t cdbuffer[CDROM_SECTOR_SIZE];
381 static struct harddisk_info *last_disk = 0;
382 static sector_t last_sector = (sector_t) -1;
386 //printk_debug("sector=%Ld\n", sector);
388 if (info->hw_sector_size == CDROM_SECTOR_SIZE) {
390 hw_sector = sector >> 2;
396 if (buf==buffer || info != last_disk || hw_sector != last_sector) {
397 //printk_debug("hw_sector=%u\n", hw_sector);
398 memset(packet, 0, sizeof packet);
399 packet[0] = 0x28; /* READ */
400 packet[2] = hw_sector >> 24;
401 packet[3] = hw_sector >> 16;
402 packet[4] = hw_sector >> 8;
403 packet[5] = hw_sector >> 0;
405 packet[8] = 1; /* length */
407 if (pio_packet(info, 1, packet, sizeof packet,
408 buf, info->hw_sector_size) != 0) {
409 printk_debug("read error\n");
413 last_sector = hw_sector;
417 memcpy(buffer, &cdbuffer[(sector & 3) << 9], IDE_SECTOR_SIZE);
421 int ide_read(int drive, sector_t sector, void *buffer)
423 struct harddisk_info *info = &harddisk_info[drive];
426 //printk_debug("drive=%d, sector=%ld\n",drive,(unsigned long) sector);
427 /* Report the buffer is empty */
428 if (sector > info->sectors) {
431 if (info->address_mode == ADDRESS_MODE_CHS) {
432 result = ide_read_sector_chs(info, buffer, sector);
434 else if (info->address_mode == ADDRESS_MODE_LBA) {
435 result = ide_read_sector_lba(info, buffer, sector);
437 else if (info->address_mode == ADDRESS_MODE_LBA48) {
438 result = ide_read_sector_lba48(info, buffer, sector);
440 else if (info->address_mode == ADDRESS_MODE_PACKET) {
441 result = ide_read_sector_packet(info, buffer, sector);
449 static int init_drive(struct harddisk_info *info, struct controller *ctrl,
450 int slave, int drive, unsigned char *buffer, int ident_command)
452 uint16_t* drive_info;
453 struct ide_pio_command cmd;
458 info->cylinders = 0u;
459 info->sectors_per_track = 0u;
460 info->address_mode = IDE_DH_CHS;
462 info->drive_exists = 0;
463 info->slave_absent = 0;
465 info->hw_sector_size = IDE_SECTOR_SIZE;
466 info->slave = slave?IDE_DH_SLAVE: IDE_DH_MASTER;
468 printk_debug("Testing for hd%c\n", 'a'+drive);
470 /* Select the drive that we are testing */
471 outb(IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | info->slave,
472 IDE_REG_DEVICE(ctrl));
475 /* Test to see if the drive registers exist,
476 * In many cases this quickly rules out a missing drive.
478 for(i = 0; i < 4; i++) {
479 outb(0xaa + i, (ctrl->cmd_base) + 2 + i);
481 for(i = 0; i < 4; i++) {
482 if (inb((ctrl->cmd_base) + 2 + i) != 0xaa + i) {
486 for(i = 0; i < 4; i++) {
487 outb(0x55 + i, (ctrl->cmd_base) + 2 + i);
489 for(i = 0; i < 4; i++) {
490 if (inb((ctrl->cmd_base) + 2 + i) != 0x55 + i) {
494 printk_debug("Probing for hd%c\n", 'a'+drive);
496 memset(&cmd, 0, sizeof(cmd));
497 cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | info->slave;
498 cmd.command = ident_command;
500 if (pio_data_in(ctrl, &cmd, buffer, IDE_SECTOR_SIZE) < 0) {
501 /* Well, if that command didn't work, we probably don't have drive. */
505 /* Now suck the data out */
506 drive_info = (uint16_t *)buffer;
507 if (drive_info[2] == 0x37C8) {
508 /* If the response is incomplete spin up the drive... */
509 memset(&cmd, 0, sizeof(cmd));
510 cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS |
512 cmd.feature = IDE_FEATURE_STANDBY_SPINUP_DRIVE;
513 if (pio_non_data(ctrl, &cmd) < 0) {
514 /* If the command doesn't work give up on the drive */
519 if ((drive_info[2] == 0x37C8) || (drive_info[2] == 0x8C73)) {
520 /* The response is incomplete retry the drive info command */
521 memset(&cmd, 0, sizeof(cmd));
522 cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS |
524 cmd.command = ident_command;
525 if (pio_data_in(ctrl, &cmd, buffer, IDE_SECTOR_SIZE) < 0) {
526 /* If the command didn't work give up on the drive. */
530 if ((drive_info[2] != 0x37C8) &&
531 (drive_info[2] != 0x738C) &&
532 (drive_info[2] != 0x8C73) &&
533 (drive_info[2] != 0xC837) &&
534 (drive_info[2] != 0x0000)) {
535 printk_info("Invalid IDE Configuration: %hx\n", drive_info[2]);
538 for(i = 27; i < 47; i++) {
539 info->model_number[((i-27)<< 1)] = (drive_info[i] >> 8) & 0xff;
540 info->model_number[((i-27)<< 1)+1] = drive_info[i] & 0xff;
542 info->model_number[40] = '\0';
543 info->drive_exists = 1;
545 /* See if LBA is supported */
546 if (ident_command == IDE_CMD_IDENTIFY_PACKET_DEVICE) {
547 info->address_mode = ADDRESS_MODE_PACKET;
548 info->removable = 1; /* XXX */
549 } else if (drive_info[49] & (1 << 9)) {
550 info->address_mode = ADDRESS_MODE_LBA;
551 info->sectors = (drive_info[61] << 16) | (drive_info[60]);
552 printk_debug("LBA mode, sectors=%Ld\n", info->sectors);
553 /* Enable LBA48 mode if it is present */
554 if (drive_info[83] & (1 <<10)) {
555 /* Should LBA48 depend on LBA? */
556 info->address_mode = ADDRESS_MODE_LBA48;
558 (((sector_t)drive_info[103]) << 48) |
559 (((sector_t)drive_info[102]) << 32) |
560 (((sector_t)drive_info[101]) << 16) |
561 (((sector_t)drive_info[100]) << 0);
562 printk_debug("LBA48 mode, sectors=%Ld\n", info->sectors);
565 info->address_mode = ADDRESS_MODE_CHS;
566 info->heads = drive_info[3];
567 info->cylinders = drive_info[1];
568 info->sectors_per_track = drive_info[6];
570 info->sectors_per_track *
573 printk_debug("CHS mode, sectors_per_track=[%d], heads=[%d], cylinders=[%d]\n",
574 info->sectors_per_track,
577 printk_debug("sectors=%Ld\n", info->sectors);
579 /* See if we have a slave */
580 if (!info->slave && (((drive_info[93] >> 14) & 3) == 1)) {
581 info->slave_absent = !(drive_info[93] & (1 << 5));
584 /* See if we need to put the device in CFA power mode 1 */
585 if ((drive_info[160] & ((1 << 15) | (1 << 13)| (1 << 12))) ==
586 ((1 << 15) | (1 << 13)| (1 << 12))) {
587 memset(&cmd, 0, sizeof(cmd));
588 cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | info->slave;
589 cmd.feature = IDE_FEATURE_CFA_ENABLE_POWER_MODE1;
590 if (pio_non_data(ctrl, &cmd) < 0) {
591 /* If I need to power up the drive, and I can't
594 printk_info("Cannot power up CFA device\n");
599 /* Some extra steps for older drives.. */
600 if (info->address_mode != ADDRESS_MODE_PACKET) {
601 /* Initialize drive parameters
602 * This is an obsolete command (disappeared as of ATA-6)
603 * but old drives need it before accessing media. */
604 memset(&cmd, 0, sizeof(cmd));
605 cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(drive_info[3] - 1)
607 cmd.sector_count = drive_info[6];
608 cmd.command = IDE_CMD_INITIALIZE_DRIVE_PARAMETERS;
609 printk_debug("Init device params... ");
610 if (pio_non_data(ctrl, &cmd) < 0)
611 printk_debug("failed (ok for newer drives)\n");
613 printk_debug("ok\n");
616 printk_info("hd%c: %s",
618 (info->address_mode==ADDRESS_MODE_CHS) ? "CHS" :
619 (info->address_mode==ADDRESS_MODE_LBA) ? "LBA" :
620 (info->address_mode==ADDRESS_MODE_LBA48) ? "LBA48" :
621 (info->address_mode==ADDRESS_MODE_PACKET) ? "ATAPI" : "???");
623 if (info->sectors > (10LL*1000*1000*1000/512))
624 printk_info(" %uGB", (unsigned) (info->sectors / (1000*1000*1000/512)));
625 else if (info->sectors > (10*1000*1000/512))
626 printk_info(" %uMB", (unsigned) (info->sectors / (1000*1000/512)));
627 else if (info->sectors > 0)
628 printk_info(" %uKB", (unsigned) (info->sectors / 2));
629 printk_info(": %s\n", info->model_number);
634 /* Experimental floating bus detection
635 * As Eric mentions, we get stuck when the bus has no drive
636 * and floating high. To avoid this, try some heuristics.
637 * This is based on a paper on Phoenix website. --ts1 */
638 static int ide_bus_floating(struct controller *ctrl)
640 unsigned long timeout;
641 unsigned char status;
643 /* Test 1: if status reads 0xff, probably no device is present
644 * on the bus. Repeat this for 20msec. */
648 /* Take logical OR to avoid chattering */
649 status |= inb(IDE_REG_STATUS(ctrl));
650 /* If it makes 0xff, it's possible to be floating,
651 * do test2 to ensure. */
654 /* If BSY bit is not set, it's harmless to continue probing. */
655 if ((status & IDE_STATUS_BSY) == 0)
658 } while (timeout > 0);
659 /* Timed out. Logical ORed status didn't make 0xFF.
660 * We have something there. */
664 /* Test 2: write something to registers, then read back and
665 * compare. Note that ATA spec inhibits this while BSY is set,
666 * but for many drives this works. This is a confirmation step anyway.
668 outb(0xaa, ctrl->cmd_base + 2);
669 outb(0x55, ctrl->cmd_base + 3);
670 outb(0xff, ctrl->cmd_base + 4);
671 if (inb(ctrl->cmd_base+2) == 0xaa
672 && inb(ctrl->cmd_base+3) == 0x55
673 && inb(ctrl->cmd_base+4) == 0xff) {
674 /* We have some registers there.
675 * Though this does not mean it is not a NIC or something... */
679 /* Status port is 0xFF, and other registers are not there.
680 * Most certainly this bus is floating. */
681 printk_info("Detected floating bus\n");
685 static int init_controller(struct controller *ctrl, int drive, unsigned char *buffer)
687 struct harddisk_info *info;
689 /* Put the drives ide channel in a know state and wait
690 * for the drives to spinup.
692 * In practice IDE disks tend not to respond to commands until
693 * they have spun up. This makes IDE hard to deal with
694 * immediately after power up, as the delays can be quite
695 * long, so we must be very careful here.
697 * There are two pathological cases that must be dealt with:
699 * - The BSY bit not being set while the IDE drives spin up.
700 * In this cases only a hard coded delay will work. As
701 * I have not reproduced it, and this is out of spec for
702 * IDE drives the work around can be enabled by setting
703 * BSY_SET_DURING_SPINUP to 0.
705 * - The BSY bit floats high when no drives are plugged in.
706 * This case will not be detected except by timing out but
707 * we avoid the problems by only probing devices we are
708 * supposed to boot from. If we don't do the probe we
709 * will not experience the problem.
711 * So speed wise I am only slow if the BSY bit is not set
712 * or not reported by the IDE controller during spinup, which
716 #if !BSY_SET_DURING_SPINUP
717 if (await_ide(timeout, ctrl, IDE_TIMEOUT) < 0) {
721 /* ts1: Try some heuristics to avoid waiting for floating bus */
722 if (ide_bus_floating(ctrl))
725 if (ide_software_reset(ctrl) < 0) {
729 /* Note: I have just done a software reset. It may be
730 * reasonable to just read the boot time signatures
731 * off of the drives to see if they are present.
733 * For now I will go with just sending commands to the drives
734 * and assuming filtering out missing drives by detecting registers
735 * that won't set and commands that fail to execute properly.
738 /* Now initialize the individual drives */
739 info = &harddisk_info[drive];
740 init_drive(info, ctrl, 0, drive, buffer, IDE_CMD_IDENTIFY_DEVICE);
741 if (!info->drive_exists)
742 init_drive(info, ctrl, 0, drive, buffer,
743 IDE_CMD_IDENTIFY_PACKET_DEVICE);
744 #ifdef CHECK_FOR_SLAVES
745 if (info->drive_exists && !info->slave_absent) {
748 init_drive(info, ctrl, 1, drive, buffer,
749 IDE_CMD_IDENTIFY_DEVICE);
750 if (!info->drive_exists)
751 init_drive(info, ctrl, 1, drive, buffer,
752 IDE_CMD_IDENTIFY_PACKET_DEVICE);
760 atapi_request_sense(struct harddisk_info *info, uint8_t *asc, uint8_t *ascq)
765 memset(packet, 0, sizeof packet);
766 packet[0] = 0x03; /* REQUEST SENSE */
767 packet[4] = sizeof buf;
768 if (pio_packet(info, 1, packet, sizeof packet, buf, sizeof buf) != 0)
778 static int atapi_detect_medium(struct harddisk_info *info)
782 uint32_t block_len, sectors;
783 unsigned long timeout;
787 memset(packet, 0, sizeof packet);
788 packet[0] = 0x25; /* READ CAPACITY */
790 /* Retry READ CAPACITY for 5 seconds unless MEDIUM NOT PRESENT
791 * is reported by the drive. If the drive reports "IN PROGRESS",
792 * 30 seconds is added. */
795 while (timeout > 0) {
796 if (pio_packet(info, 1, packet, sizeof packet, buf, sizeof buf)
800 if (atapi_request_sense(info, &asc, &ascq) == 0) {
801 if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
802 printk_debug("Device reports MEDIUM NOT PRESENT\n");
806 if (asc == 0x04 && ascq == 0x01 && !in_progress) {
807 /* IN PROGRESS OF BECOMING READY */
808 printk_info("Waiting for drive to detect "
810 /* Allow 30 seconds more */
818 printk_debug("read capacity failed\n");
822 block_len = (uint32_t) buf[4] << 24
823 | (uint32_t) buf[5] << 16
824 | (uint32_t) buf[6] << 8
825 | (uint32_t) buf[7] << 0;
826 if (block_len != IDE_SECTOR_SIZE && block_len != CDROM_SECTOR_SIZE) {
827 printk_info("Unsupported sector size %u\n", block_len);
830 info->hw_sector_size = block_len;
832 sectors = (uint32_t) buf[0] << 24
833 | (uint32_t) buf[1] << 16
834 | (uint32_t) buf[2] << 8
835 | (uint32_t) buf[3] << 0;
837 if (info->hw_sector_size == CDROM_SECTOR_SIZE)
838 sectors <<= 2; /* # of sectors in 512-byte "soft" sector */
839 if (sectors != info->sectors)
840 printk_info("%uMB medium detected\n", sectors>>(20-9));
841 info->sectors = sectors;
845 static int detect_medium(struct harddisk_info *info)
847 if (info->address_mode == ADDRESS_MODE_PACKET) {
848 if (atapi_detect_medium(info) != 0)
851 printk_debug("not implemented for non-ATAPI device\n");
857 static int find_ide_controller_compat(struct controller *ctrl, int index)
859 if (index >= IDE_MAX_CONTROLLERS)
861 ctrl->cmd_base = ide_base[index];
862 ctrl->ctrl_base = ide_base[index] + IDE_REG_EXTENDED_OFFSET;
866 static int find_ide_controller(struct controller *ctrl, int ctrl_index)
869 struct device *dev = 0;
871 unsigned int prog_if;
873 /* A PCI IDE controller has two channels (pri, sec) */
874 pci_index = ctrl_index >> 1;
877 /* Find a IDE storage class device */
878 dev = dev_find_class(0x010100, dev);
880 printk_debug("PCI IDE #%d not found\n", pci_index);
884 if (pci_index-- == 0)
888 prog_if = dev->class & 0xff;
889 printk_debug("found PCI IDE controller %04x:%04x prog_if=%#x\n",
890 dev->vendor, dev->device, prog_if);
892 /* See how this controller is configured */
893 mask = (ctrl_index & 1) ? 4 : 1;
894 printk_debug("%s channel: ", (ctrl_index & 1) ? "secondary" : "primary");
895 if (prog_if & mask) {
896 printk_debug("native PCI mode\n");
897 if ((ctrl_index & 1) == 0) {
898 /* Primary channel */
899 ctrl->cmd_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
900 ctrl->ctrl_base = pci_read_config32(dev, PCI_BASE_ADDRESS_1);
902 /* Secondary channel */
903 ctrl->cmd_base = pci_read_config32(dev, PCI_BASE_ADDRESS_2);
904 ctrl->ctrl_base = pci_read_config32(dev, PCI_BASE_ADDRESS_3);
906 ctrl->cmd_base &= ~3;
907 ctrl->ctrl_base &= ~3;
909 printk_debug("compatibility mode\n");
910 if (find_ide_controller_compat(ctrl, ctrl_index) != 0)
916 int ide_probe(int drive)
918 struct controller *ctrl;
920 struct harddisk_info *info;
922 if (drive >= IDE_MAX_DRIVES) {
923 printk_info("Unsupported drive number\n");
927 /* A controller has two drives (master, slave) */
928 ctrl_index = drive >> 1;
930 ctrl = &controllers[ctrl_index];
931 if (ctrl->cmd_base == 0) {
932 if (find_ide_controller(ctrl, ctrl_index) != 0) {
933 printk_info("IDE channel %d not found\n", ctrl_index);
936 if (init_controller(ctrl, drive & ~1, ide_buffer) != 0) {
937 printk_info("No drive detected on IDE channel %d\n",
942 info = &harddisk_info[drive];
943 if (!info->drive_exists) {
944 printk_info("Drive %d does not exist\n", drive);
948 if (info->removable) {
949 if (detect_medium(info) != 0) {
950 printk_info("Media detection failed\n");
958 /* vim:set sts=8 sw=8: */