2 * OpenBIOS polled ide driver
4 * Copyright (C) 2004 Jens Axboe <axboe@suse.de>
5 * Copyright (C) 2005 Stefan Reinauer <stepan@openbios.org>
7 * Credit goes to Hale Landis for his excellent ata demo software
8 * OF node handling and some fixes by Stefan Reinauer
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
17 #include "libopenbios/bindings.h"
18 #include "kernel/kernel.h"
19 #include "libc/byteorder.h"
20 #include "libc/vsprintf.h"
22 #include "drivers/drivers.h"
27 #ifdef CONFIG_DEBUG_IDE
28 #define IDE_DPRINTF(fmt, args...) \
29 do { printk("IDE - %s: " fmt, __func__ , ##args); } while (0)
31 #define IDE_DPRINTF(fmt, args...) do { } while (0)
34 /* DECLARE data structures for the nodes. */
35 DECLARE_UNNAMED_NODE( ob_ide, INSTALL_OPEN, sizeof(struct ide_drive*) );
36 DECLARE_UNNAMED_NODE( ob_ide_ctrl, INSTALL_OPEN, sizeof(int));
39 * define to 2 for the standard 2 channels only
41 #ifndef CONFIG_IDE_NUM_CHANNELS
42 #define IDE_NUM_CHANNELS 4
44 #define IDE_NUM_CHANNELS CONFIG_IDE_NUM_CHANNELS
46 #define IDE_MAX_CHANNELS 4
48 #ifndef CONFIG_IDE_FIRST_UNIT
51 #define FIRST_UNIT CONFIG_IDE_FIRST_UNIT
54 #ifndef CONFIG_IDE_DEV_TYPE
55 #define DEV_TYPE "ide"
57 #define DEV_TYPE CONFIG_IDE_DEV_TYPE
60 #ifndef CONFIG_IDE_DEV_NAME
61 #define DEV_NAME "ide%d"
63 #define DEV_NAME CONFIG_IDE_DEV_NAME
66 static int current_channel = FIRST_UNIT;
68 static struct ide_channel *channels = NULL;
70 static inline void ide_add_channel(struct ide_channel *chan)
72 chan->next = channels;
76 static struct ide_channel *ide_seek_channel(const char *name)
78 struct ide_channel *current;
82 if (!strcmp(current->name, name))
84 current = current->next;
94 static void dump_drive(struct ide_drive *drive)
96 #ifdef CONFIG_DEBUG_IDE
97 printk("IDE DRIVE @%lx:\n", (unsigned long)drive);
98 printk("unit: %d\n",drive->unit);
99 printk("present: %d\n",drive->present);
100 printk("type: %d\n",drive->type);
101 printk("media: %d\n",drive->media);
102 printk("model: %s\n",drive->model);
103 printk("nr: %d\n",drive->nr);
104 printk("cyl: %d\n",drive->cyl);
105 printk("head: %d\n",drive->head);
106 printk("sect: %d\n",drive->sect);
107 printk("bs: %d\n",drive->bs);
112 * old style io port operations
115 ob_ide_inb(struct ide_channel *chan, unsigned int port)
117 return inb(chan->io_regs[port]);
121 ob_ide_outb(struct ide_channel *chan, unsigned char data, unsigned int port)
123 outb(data, chan->io_regs[port]);
127 ob_ide_insw(struct ide_channel *chan,
128 unsigned int port, unsigned char *addr, unsigned int count)
130 insw(chan->io_regs[port], addr, count);
134 ob_ide_outsw(struct ide_channel *chan,
135 unsigned int port, unsigned char *addr, unsigned int count)
137 outsw(chan->io_regs[port], addr, count);
140 static inline unsigned char
141 ob_ide_pio_readb(struct ide_drive *drive, unsigned int offset)
143 struct ide_channel *chan = drive->channel;
145 return chan->obide_inb(chan, offset);
149 ob_ide_pio_writeb(struct ide_drive *drive, unsigned int offset,
152 struct ide_channel *chan = drive->channel;
154 chan->obide_outb(chan, data, offset);
158 ob_ide_pio_insw(struct ide_drive *drive, unsigned int offset,
159 unsigned char *addr, unsigned int len)
161 struct ide_channel *chan = drive->channel;
164 IDE_DPRINTF("%d: command not word aligned\n", drive->nr);
168 chan->obide_insw(chan, offset, addr, len / 2);
172 ob_ide_pio_outsw(struct ide_drive *drive, unsigned int offset,
173 unsigned char *addr, unsigned int len)
175 struct ide_channel *chan = drive->channel;
178 IDE_DPRINTF("%d: command not word aligned\n", drive->nr);
182 chan->obide_outsw(chan, offset, addr, len / 2);
186 ob_ide_400ns_delay(struct ide_drive *drive)
188 (void) ob_ide_pio_readb(drive, IDEREG_ASTATUS);
189 (void) ob_ide_pio_readb(drive, IDEREG_ASTATUS);
190 (void) ob_ide_pio_readb(drive, IDEREG_ASTATUS);
191 (void) ob_ide_pio_readb(drive, IDEREG_ASTATUS);
197 ob_ide_error(struct ide_drive *drive, unsigned char stat, const char *msg)
199 #ifdef CONFIG_DEBUG_IDE
200 struct ide_channel *chan = drive->channel;
205 stat = ob_ide_pio_readb(drive, IDEREG_STATUS);
207 IDE_DPRINTF("ob_ide_error drive<%d>: %s:\n", drive->nr, msg);
208 IDE_DPRINTF(" cmd=%x, stat=%x", chan->ata_cmd.command, stat);
210 if ((stat & (BUSY_STAT | ERR_STAT)) == ERR_STAT) {
211 #ifdef CONFIG_DEBUG_IDE
214 ob_ide_pio_readb(drive, IDEREG_ERROR);
215 IDE_DPRINTF(", err=%x", err);
219 #ifdef CONFIG_DEBUG_IDE
221 * see if sense is valid and dump that
223 if (chan->ata_cmd.command == WIN_PACKET) {
224 struct atapi_command *cmd = &chan->atapi_cmd;
225 unsigned char old_cdb = cmd->cdb[0];
227 if (cmd->cdb[0] == ATAPI_REQ_SENSE) {
228 old_cdb = cmd->old_cdb;
230 IDE_DPRINTF(" atapi opcode=%02x", old_cdb);
234 IDE_DPRINTF(" cdb: ");
235 for (i = 0; i < sizeof(cmd->cdb); i++)
236 IDE_DPRINTF("%02x ", cmd->cdb[i]);
238 if (cmd->sense_valid)
239 IDE_DPRINTF(", sense: %02x/%02x/%02x",
240 cmd->sense.sense_key, cmd->sense.asc,
243 IDE_DPRINTF(", no sense");
250 * wait for 'stat' to be set. returns 1 if failed, 0 if succesful
253 ob_ide_wait_stat(struct ide_drive *drive, unsigned char ok_stat,
254 unsigned char bad_stat, unsigned char *ret_stat)
259 ob_ide_400ns_delay(drive);
261 for (i = 0; i < 5000; i++) {
262 stat = ob_ide_pio_readb(drive, IDEREG_STATUS);
263 if (!(stat & BUSY_STAT))
275 if ((stat & ok_stat) || !ok_stat)
282 ob_ide_select_drive(struct ide_drive *drive)
284 struct ide_channel *chan = drive->channel;
285 unsigned char control = IDEHEAD_DEV0;
287 if (ob_ide_wait_stat(drive, 0, BUSY_STAT, NULL)) {
288 IDE_DPRINTF("select_drive: timed out\n");
293 * don't select drive if already active. Note: we always
294 * wait for BUSY clear
296 if (drive->unit == chan->selected)
300 control = IDEHEAD_DEV1;
302 ob_ide_pio_writeb(drive, IDEREG_CURRENT, control);
303 ob_ide_400ns_delay(drive);
305 if (ob_ide_wait_stat(drive, 0, BUSY_STAT, NULL)) {
306 IDE_DPRINTF("select_drive: timed out\n");
310 chan->selected = drive->unit;
315 ob_ide_write_tasklet(struct ide_drive *drive, struct ata_command *cmd)
317 ob_ide_pio_writeb(drive, IDEREG_FEATURE, cmd->task[1]);
318 ob_ide_pio_writeb(drive, IDEREG_NSECTOR, cmd->task[3]);
319 ob_ide_pio_writeb(drive, IDEREG_SECTOR, cmd->task[7]);
320 ob_ide_pio_writeb(drive, IDEREG_LCYL, cmd->task[8]);
321 ob_ide_pio_writeb(drive, IDEREG_HCYL, cmd->task[9]);
323 ob_ide_pio_writeb(drive, IDEREG_FEATURE, cmd->task[0]);
324 ob_ide_pio_writeb(drive, IDEREG_NSECTOR, cmd->task[2]);
325 ob_ide_pio_writeb(drive, IDEREG_SECTOR, cmd->task[4]);
326 ob_ide_pio_writeb(drive, IDEREG_LCYL, cmd->task[5]);
327 ob_ide_pio_writeb(drive, IDEREG_HCYL, cmd->task[6]);
330 cmd->device_head |= IDEHEAD_DEV1;
332 ob_ide_pio_writeb(drive, IDEREG_CURRENT, cmd->device_head);
334 ob_ide_pio_writeb(drive, IDEREG_COMMAND, cmd->command);
335 ob_ide_400ns_delay(drive);
339 ob_ide_write_registers(struct ide_drive *drive, struct ata_command *cmd)
342 * we are _always_ polled
344 ob_ide_pio_writeb(drive, IDEREG_CONTROL, cmd->control | IDECON_NIEN);
346 ob_ide_pio_writeb(drive, IDEREG_FEATURE, cmd->feature);
347 ob_ide_pio_writeb(drive, IDEREG_NSECTOR, cmd->nsector);
348 ob_ide_pio_writeb(drive, IDEREG_SECTOR, cmd->sector);
349 ob_ide_pio_writeb(drive, IDEREG_LCYL, cmd->lcyl);
350 ob_ide_pio_writeb(drive, IDEREG_HCYL, cmd->hcyl);
353 cmd->device_head |= IDEHEAD_DEV1;
355 ob_ide_pio_writeb(drive, IDEREG_CURRENT, cmd->device_head);
357 ob_ide_pio_writeb(drive, IDEREG_COMMAND, cmd->command);
358 ob_ide_400ns_delay(drive);
362 * execute command with "pio non data" protocol
366 ob_ide_pio_non_data(struct ide_drive *drive, struct ata_command *cmd)
368 if (ob_ide_select_drive(drive))
371 ob_ide_write_registers(drive, cmd);
373 if (ob_ide_wait_stat(drive, 0, BUSY_STAT, NULL))
381 * execute given command with a pio data-in phase.
384 ob_ide_pio_data_in(struct ide_drive *drive, struct ata_command *cmd)
387 unsigned int bytes, timeout;
389 if (ob_ide_select_drive(drive))
393 * ATA must set ready and seek stat, ATAPI need only clear busy
397 stat = ob_ide_pio_readb(drive, IDEREG_STATUS);
399 if (drive->type == ide_type_ata) {
401 * this is BIOS code, don't be too pedantic
404 if ((stat & (BUSY_STAT | READY_STAT | SEEK_STAT)) ==
405 (READY_STAT | SEEK_STAT))
408 if ((stat & (BUSY_STAT | READY_STAT)) == READY_STAT)
412 if (!(stat & BUSY_STAT))
415 ob_ide_400ns_delay(drive);
416 } while (timeout++ < 1000);
418 if (timeout >= 1000) {
419 ob_ide_error(drive, stat, "drive timed out");
424 ob_ide_write_registers(drive, cmd);
431 unsigned count = cmd->buflen;
433 if (count > drive->bs)
436 /* delay 100ms for ATAPI? */
439 * wait for BUSY clear
441 if (ob_ide_wait_stat(drive, 0, BUSY_STAT | ERR_STAT, &stat)) {
442 ob_ide_error(drive, stat, "timed out waiting for BUSY clear");
450 if ((stat & (BUSY_STAT | DRQ_STAT)) == DRQ_STAT) {
451 ob_ide_pio_insw(drive, IDEREG_DATA, cmd->buffer, count);
453 cmd->buffer += count;
456 ob_ide_400ns_delay(drive);
459 if (stat & (BUSY_STAT | WRERR_STAT | ERR_STAT)) {
464 if (!(stat & DRQ_STAT)) {
471 IDE_DPRINTF("bytes=%d, stat=%x\n", bytes, stat);
473 return bytes ? 1 : 0;
477 * execute ata command with pio packet protocol
480 ob_ide_pio_packet(struct ide_drive *drive, struct atapi_command *cmd)
482 unsigned char stat, reason, lcyl, hcyl;
483 struct ata_command *acmd = &drive->channel->ata_cmd;
484 unsigned char *buffer;
487 if (ob_ide_select_drive(drive))
490 if (cmd->buflen && cmd->data_direction == atapi_ddir_none)
491 IDE_DPRINTF("non-zero buflen but no data direction\n");
493 memset(acmd, 0, sizeof(*acmd));
494 acmd->lcyl = cmd->buflen & 0xff;
495 acmd->hcyl = (cmd->buflen >> 8) & 0xff;
496 acmd->command = WIN_PACKET;
497 ob_ide_write_registers(drive, acmd);
500 * BUSY must be set, _or_ DRQ | ERR
502 stat = ob_ide_pio_readb(drive, IDEREG_ASTATUS);
503 if ((stat & BUSY_STAT) == 0) {
504 if (!(stat & (DRQ_STAT | ERR_STAT))) {
505 ob_ide_error(drive, stat, "bad stat in atapi cmd");
511 if (ob_ide_wait_stat(drive, 0, BUSY_STAT | ERR_STAT, &stat)) {
512 ob_ide_error(drive, stat, "timeout, ATAPI BUSY clear");
517 if ((stat & (BUSY_STAT | DRQ_STAT | ERR_STAT)) != DRQ_STAT) {
519 * if command isn't request sense, then we have a problem. if
520 * we are doing a sense, ERR_STAT == CHECK_CONDITION
522 if (cmd->cdb[0] != ATAPI_REQ_SENSE) {
523 IDE_DPRINTF("odd, drive didn't want to transfer %x\n",
532 ob_ide_pio_outsw(drive, IDEREG_DATA, cmd->cdb,sizeof(cmd->cdb));
533 ob_ide_400ns_delay(drive);
536 * ok, cdb was sent to drive, now do data transfer (if any)
539 buffer = cmd->buffer;
543 if (ob_ide_wait_stat(drive, 0, BUSY_STAT | ERR_STAT, &stat)) {
544 ob_ide_error(drive, stat, "busy not clear after cdb");
552 if ((stat & (BUSY_STAT | DRQ_STAT)) == 0)
555 if ((stat & (BUSY_STAT | DRQ_STAT)) != DRQ_STAT)
558 reason = ob_ide_pio_readb(drive, IDEREG_NSECTOR);
559 lcyl = ob_ide_pio_readb(drive, IDEREG_LCYL);
560 hcyl = ob_ide_pio_readb(drive, IDEREG_HCYL);
563 * check if the drive wants to transfer data in the same
564 * direction as we do...
566 if ((reason & IREASON_CD) && cmd->data_direction != atapi_ddir_read) {
567 ob_ide_error(drive, stat, "atapi, bad transfer ddir");
571 bc = (hcyl << 8) | lcyl;
578 if (cmd->data_direction == atapi_ddir_read)
579 ob_ide_pio_insw(drive, IDEREG_DATA, buffer, bc);
581 ob_ide_pio_outsw(drive, IDEREG_DATA, buffer, bc);
586 ob_ide_400ns_delay(drive);
589 if (cmd->data_direction != atapi_ddir_none)
590 (void) ob_ide_wait_stat(drive, 0, BUSY_STAT, &stat);
593 IDE_DPRINTF("cdb failed, bytes=%d, stat=%x\n", bytes, stat);
595 return (stat & ERR_STAT) || bytes;
599 * execute a packet command, with retries if appropriate
602 ob_ide_atapi_packet(struct ide_drive *drive, struct atapi_command *cmd)
604 int retries = 5, ret;
606 if (drive->type != ide_type_atapi)
608 if (cmd->buflen > 0xffff)
615 ret = ob_ide_pio_packet(drive, cmd);
620 * request sense failed, bummer
622 if (cmd->cdb[0] == ATAPI_REQ_SENSE)
625 if (ob_ide_atapi_request_sense(drive))
629 * we know sense is valid. retry if the drive isn't ready,
630 * otherwise don't bother.
632 if (cmd->sense.sense_key != ATAPI_SENSE_NOT_READY)
635 * ... except 'medium not present'
637 if (cmd->sense.asc == 0x3a)
644 ob_ide_error(drive, 0, "atapi command");
650 ob_ide_atapi_request_sense(struct ide_drive *drive)
652 struct atapi_command *cmd = &drive->channel->atapi_cmd;
653 unsigned char old_cdb;
656 * save old cdb for debug error
658 old_cdb = cmd->cdb[0];
660 memset(cmd, 0, sizeof(*cmd));
661 cmd->cdb[0] = ATAPI_REQ_SENSE;
663 cmd->buffer = (unsigned char *) &cmd->sense;
665 cmd->data_direction = atapi_ddir_read;
666 cmd->old_cdb = old_cdb;
668 if (ob_ide_atapi_packet(drive, cmd))
671 cmd->sense_valid = 1;
676 * make sure drive is ready and media loaded
679 ob_ide_atapi_drive_ready(struct ide_drive *drive)
681 struct atapi_command *cmd = &drive->channel->atapi_cmd;
682 struct atapi_capacity cap;
684 IDE_DPRINTF("ob_ide_atapi_drive_ready\n");
687 * Test Unit Ready is like a ping
689 memset(cmd, 0, sizeof(*cmd));
690 cmd->cdb[0] = ATAPI_TUR;
692 if (ob_ide_atapi_packet(drive, cmd)) {
693 IDE_DPRINTF("%d: TUR failed\n", drive->nr);
698 * don't force load of tray (bit 2 in byte 4 of cdb), it's
699 * annoying and we don't want to deal with errors from drives
702 memset(cmd, 0, sizeof(*cmd));
703 cmd->cdb[0] = ATAPI_START_STOP_UNIT;
706 if (ob_ide_atapi_packet(drive, cmd)) {
707 IDE_DPRINTF("%d: START_STOP unit failed\n", drive->nr);
712 * finally, get capacity and block size
714 memset(cmd, 0, sizeof(*cmd));
715 memset(&cap, 0, sizeof(cap));
717 cmd->cdb[0] = ATAPI_READ_CAPACITY;
718 cmd->buffer = (unsigned char *) ∩
719 cmd->buflen = sizeof(cap);
720 cmd->data_direction = atapi_ddir_read;
722 if (ob_ide_atapi_packet(drive, cmd)) {
723 drive->sectors = 0x1fffff;
728 drive->sectors = __be32_to_cpu(cap.lba) + 1;
729 drive->bs = __be32_to_cpu(cap.block_size);
734 * read from an atapi device, using READ_10
737 ob_ide_read_atapi(struct ide_drive *drive, unsigned long long block,
738 unsigned char *buf, unsigned int sectors)
740 struct atapi_command *cmd = &drive->channel->atapi_cmd;
742 if (ob_ide_atapi_drive_ready(drive))
745 memset(cmd, 0, sizeof(*cmd));
748 * READ_10 should work on generally any atapi device
750 cmd->cdb[0] = ATAPI_READ_10;
751 cmd->cdb[2] = (block >> 24) & 0xff;
752 cmd->cdb[3] = (block >> 16) & 0xff;
753 cmd->cdb[4] = (block >> 8) & 0xff;
754 cmd->cdb[5] = block & 0xff;
755 cmd->cdb[7] = (sectors >> 8) & 0xff;
756 cmd->cdb[8] = sectors & 0xff;
759 cmd->buflen = sectors * 2048;
760 cmd->data_direction = atapi_ddir_read;
762 return ob_ide_atapi_packet(drive, cmd);
766 ob_ide_read_ata_chs(struct ide_drive *drive, unsigned long long block,
767 unsigned char *buf, unsigned int sectors)
769 struct ata_command *cmd = &drive->channel->ata_cmd;
770 unsigned int track = (block / drive->sect);
771 unsigned int sect = (block % drive->sect) + 1;
772 unsigned int head = (track % drive->head);
773 unsigned int cyl = (track / drive->head);
776 * fill in chs command to read from disk at given location
779 cmd->buflen = sectors * 512;
781 cmd->nsector = sectors & 0xff;
784 cmd->hcyl = cyl >> 8;
785 cmd->device_head = head;
787 cmd->command = WIN_READ;
789 return ob_ide_pio_data_in(drive, cmd);
793 ob_ide_read_ata_lba28(struct ide_drive *drive, unsigned long long block,
794 unsigned char *buf, unsigned int sectors)
796 struct ata_command *cmd = &drive->channel->ata_cmd;
798 memset(cmd, 0, sizeof(*cmd));
801 * fill in 28-bit lba command to read from disk at given location
804 cmd->buflen = sectors * 512;
806 cmd->nsector = sectors;
808 cmd->lcyl = block >>= 8;
809 cmd->hcyl = block >>= 8;
810 cmd->device_head = ((block >> 8) & 0x0f);
811 cmd->device_head |= (1 << 6);
813 cmd->command = WIN_READ;
815 return ob_ide_pio_data_in(drive, cmd);
819 ob_ide_read_ata_lba48(struct ide_drive *drive, unsigned long long block,
820 unsigned char *buf, unsigned int sectors)
822 struct ata_command *cmd = &drive->channel->ata_cmd;
824 memset(cmd, 0, sizeof(*cmd));
827 cmd->buflen = sectors * 512;
830 * we are using tasklet addressing here
832 cmd->task[2] = sectors;
833 cmd->task[3] = sectors >> 8;
834 cmd->task[4] = block;
835 cmd->task[5] = block >> 8;
836 cmd->task[6] = block >> 16;
837 cmd->task[7] = block >> 24;
838 cmd->task[8] = (u64) block >> 32;
839 cmd->task[9] = (u64) block >> 40;
841 cmd->command = WIN_READ_EXT;
843 ob_ide_write_tasklet(drive, cmd);
845 return ob_ide_pio_data_in(drive, cmd);
848 * read 'sectors' sectors from ata device
851 ob_ide_read_ata(struct ide_drive *drive, unsigned long long block,
852 unsigned char *buf, unsigned int sectors)
854 unsigned long long end_block = block + sectors;
855 const int need_lba48 = (end_block > (1ULL << 28)) || (sectors > 255);
857 if (end_block > drive->sectors)
859 if (need_lba48 && drive->addressing != ide_lba48)
863 * use lba48 if we have to, otherwise use the faster lba28
866 return ob_ide_read_ata_lba48(drive, block, buf, sectors);
867 else if (drive->addressing != ide_chs)
868 return ob_ide_read_ata_lba28(drive, block, buf, sectors);
870 return ob_ide_read_ata_chs(drive, block, buf, sectors);
874 ob_ide_read_sectors(struct ide_drive *drive, unsigned long long block,
875 unsigned char *buf, unsigned int sectors)
879 if (block + sectors > drive->sectors)
882 IDE_DPRINTF("ob_ide_read_sectors: block=%lu sectors=%u\n",
883 (unsigned long) block, sectors);
885 if (drive->type == ide_type_ata)
886 return ob_ide_read_ata(drive, block, buf, sectors);
888 return ob_ide_read_atapi(drive, block, buf, sectors);
892 * byte swap the string if necessay, and strip leading/trailing blanks
895 ob_ide_fixup_string(unsigned char *s, unsigned int len)
897 unsigned char *p = s, *end = &s[len & ~1];
900 * if big endian arch, byte swap the string
902 #ifdef CONFIG_BIG_ENDIAN
903 for (p = end ; p != s;) {
904 unsigned short *pp = (unsigned short *) (p -= 2);
905 *pp = __le16_to_cpu(*pp);
909 while (s != end && *s == ' ')
911 while (s != end && *s)
912 if (*s++ != ' ' || (s != end && *s && *s != ' '))
919 * it's big endian, we need to swap (if on little endian) the items we use
922 ob_ide_fixup_id(struct hd_driveid *id)
924 ob_ide_fixup_string(id->model, 40);
925 id->config = __le16_to_cpu(id->config);
926 id->lba_capacity = __le32_to_cpu(id->lba_capacity);
927 id->cyls = __le16_to_cpu(id->cyls);
928 id->heads = __le16_to_cpu(id->heads);
929 id->sectors = __le16_to_cpu(id->sectors);
930 id->command_set_2 = __le16_to_cpu(id->command_set_2);
931 id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2);
937 ob_ide_identify_drive(struct ide_drive *drive)
939 struct ata_command *cmd = &drive->channel->ata_cmd;
940 struct hd_driveid id;
942 memset(cmd, 0, sizeof(*cmd));
943 cmd->buffer = (unsigned char *) &id;
946 if (drive->type == ide_type_ata)
947 cmd->command = WIN_IDENTIFY;
948 else if (drive->type == ide_type_atapi)
949 cmd->command = WIN_IDENTIFY_PACKET;
951 IDE_DPRINTF("%s: called with bad device type %d\n",
952 __FUNCTION__, drive->type);
956 if (ob_ide_pio_data_in(drive, cmd))
959 ob_ide_fixup_id(&id);
961 if (drive->type == ide_type_atapi) {
962 drive->media = (id.config >> 8) & 0x1f;
963 drive->sectors = 0x7fffffff;
965 drive->max_sectors = 31;
967 drive->media = ide_media_disk;
968 drive->sectors = id.lba_capacity;
970 drive->max_sectors = 255;
972 #ifdef CONFIG_IDE_LBA48
973 if ((id.command_set_2 & 0x0400) && (id.cfs_enable_2 & 0x0400)) {
974 drive->addressing = ide_lba48;
975 drive->max_sectors = 65535;
978 if (id.capability & 2)
979 drive->addressing = ide_lba28;
981 drive->addressing = ide_chs;
984 /* only set these in chs mode? */
985 drive->cyl = id.cyls;
986 drive->head = id.heads;
987 drive->sect = id.sectors;
990 strncpy(drive->model, (char*)id.model, sizeof(id.model));
991 drive->model[40] = '\0';
996 * identify type of devices on channel. must have already been probed.
999 ob_ide_identify_drives(struct ide_channel *chan)
1001 struct ide_drive *drive;
1004 for (i = 0; i < 2; i++) {
1005 drive = &chan->drives[i];
1007 if (!drive->present)
1010 ob_ide_identify_drive(drive);
1015 * software reset (ATA-4, section 8.3)
1018 ob_ide_software_reset(struct ide_drive *drive)
1020 struct ide_channel *chan = drive->channel;
1022 ob_ide_pio_writeb(drive, IDEREG_CONTROL, IDECON_NIEN | IDECON_SRST);
1023 ob_ide_400ns_delay(drive);
1024 ob_ide_pio_writeb(drive, IDEREG_CONTROL, IDECON_NIEN);
1025 ob_ide_400ns_delay(drive);
1028 * if master is present, wait for BUSY clear
1030 if (chan->drives[0].present)
1031 ob_ide_wait_stat(drive, 0, BUSY_STAT, NULL);
1034 * if slave is present, wait until it allows register access
1036 if (chan->drives[1].present) {
1037 unsigned char sectorn, sectorc;
1044 ob_ide_pio_writeb(drive, IDEREG_CURRENT, IDEHEAD_DEV1);
1045 ob_ide_400ns_delay(drive);
1047 sectorn = ob_ide_pio_readb(drive, IDEREG_SECTOR);
1048 sectorc = ob_ide_pio_readb(drive, IDEREG_NSECTOR);
1050 if (sectorc == 0x01 && sectorn == 0x01)
1053 } while (--timeout);
1057 * reset done, reselect original device
1059 drive->channel->selected = -1;
1060 ob_ide_select_drive(drive);
1064 * this serves as both a device check, and also to verify that the drives
1065 * we initially "found" are really there
1068 ob_ide_device_type_check(struct ide_drive *drive)
1070 unsigned char sc, sn, cl, ch, st;
1072 if (ob_ide_select_drive(drive))
1075 sc = ob_ide_pio_readb(drive, IDEREG_NSECTOR);
1076 sn = ob_ide_pio_readb(drive, IDEREG_SECTOR);
1078 if (sc == 0x01 && sn == 0x01) {
1080 * read device signature
1082 cl = ob_ide_pio_readb(drive, IDEREG_LCYL);
1083 ch = ob_ide_pio_readb(drive, IDEREG_HCYL);
1084 st = ob_ide_pio_readb(drive, IDEREG_STATUS);
1085 if (cl == 0x14 && ch == 0xeb)
1086 drive->type = ide_type_atapi;
1087 else if (cl == 0x00 && ch == 0x00 && st != 0x00)
1088 drive->type = ide_type_ata;
1099 ob_ide_device_check(struct ide_drive *drive)
1101 unsigned char sc, sn;
1104 * non-existing io port should return 0xff, don't probe this
1105 * channel at all then
1107 if (ob_ide_pio_readb(drive, IDEREG_STATUS) == 0xff) {
1108 drive->channel->present = 0;
1112 if (ob_ide_select_drive(drive))
1115 ob_ide_pio_writeb(drive, IDEREG_NSECTOR, 0x55);
1116 ob_ide_pio_writeb(drive, IDEREG_SECTOR, 0xaa);
1117 ob_ide_pio_writeb(drive, IDEREG_NSECTOR, 0xaa);
1118 ob_ide_pio_writeb(drive, IDEREG_SECTOR, 0x55);
1119 ob_ide_pio_writeb(drive, IDEREG_NSECTOR, 0x55);
1120 ob_ide_pio_writeb(drive, IDEREG_SECTOR, 0xaa);
1122 sc = ob_ide_pio_readb(drive, IDEREG_NSECTOR);
1123 sn = ob_ide_pio_readb(drive, IDEREG_SECTOR);
1126 * we _think_ the device is there, we will make sure later
1128 if (sc == 0x55 && sn == 0xaa) {
1130 drive->type = ide_type_unknown;
1135 * probe the legacy ide ports and find attached devices.
1138 ob_ide_probe(struct ide_channel *chan)
1140 struct ide_drive *drive;
1143 for (i = 0; i < 2; i++) {
1144 drive = &chan->drives[i];
1146 ob_ide_device_check(drive);
1149 * no point in continuing
1154 if (!drive->present)
1158 * select and reset device
1160 if (ob_ide_select_drive(drive))
1163 ob_ide_software_reset(drive);
1165 ob_ide_device_type_check(drive);
1170 * The following functions are interfacing with OpenBIOS. They
1171 * are device node methods. Thus they have to do proper stack handling.
1176 * 255 sectors for ata lba28, 65535 for lba48, and 31 sectors for atapi
1179 ob_ide_max_transfer(int *idx)
1181 struct ide_drive *drive = *(struct ide_drive **)idx;
1183 IDE_DPRINTF("max_transfer %x\n", drive->max_sectors * drive->bs);
1185 PUSH(drive->max_sectors * drive->bs);
1189 ob_ide_read_blocks(int *idx)
1191 cell n = POP(), cnt=n;
1193 unsigned char *dest = (unsigned char *)cell2pointer(POP());
1194 struct ide_drive *drive = *(struct ide_drive **)idx;
1196 IDE_DPRINTF("ob_ide_read_blocks %lx block=%ld n=%ld\n",
1197 (unsigned long)dest, (unsigned long)blk, (long)n);
1201 if (len > drive->max_sectors)
1202 len = drive->max_sectors;
1204 if (ob_ide_read_sectors(drive, blk, dest, len)) {
1205 IDE_DPRINTF("ob_ide_read_blocks: error\n");
1209 dest += len * drive->bs;
1218 ob_ide_block_size(int *idx)
1220 struct ide_drive *drive = *(struct ide_drive **)idx;
1222 IDE_DPRINTF("ob_ide_block_size: block size %x\n", drive->bs);
1228 ob_ide_initialize(int *idx)
1231 phandle_t ph=get_cur_dev();
1234 fword("device-type");
1236 // Set dummy reg properties
1238 set_int_property(ph, "#address-cells", 1);
1239 set_int_property(ph, "#size-cells", 0);
1241 props[0] = __cpu_to_be32(0); props[1] = __cpu_to_be32(0); props[2] = __cpu_to_be32(0);
1242 set_property(ph, "reg", (char *)&props, 3*sizeof(int));
1244 fword("is-deblocker");
1248 ob_ide_open(int *idx)
1252 struct ide_drive *drive;
1253 struct ide_channel *chan;
1261 fword("ihandle>phandle");
1262 ph=(phandle_t)POP();
1263 idename=get_property(ph, "name", &len);
1265 chan = ide_seek_channel(idename);
1266 drive = &chan->drives[unit];
1267 *(struct ide_drive **)idx = drive;
1269 IDE_DPRINTF("opening channel %d unit %d\n", idx[1], idx[0]);
1272 if (drive->type != ide_type_ata)
1273 ret= !ob_ide_atapi_drive_ready(drive);
1275 selfword("open-deblocker");
1277 /* interpose disk-label */
1278 ph = find_dev("/packages/disk-label");
1287 ob_ide_close(struct ide_drive *drive)
1289 selfword("close-deblocker");
1292 NODE_METHODS(ob_ide) = {
1293 { NULL, ob_ide_initialize },
1294 { "open", ob_ide_open },
1295 { "close", ob_ide_close },
1296 { "read-blocks", ob_ide_read_blocks },
1297 { "block-size", ob_ide_block_size },
1298 { "max-transfer", ob_ide_max_transfer },
1302 ob_ide_ctrl_initialize(int *idx)
1304 phandle_t ph=get_cur_dev();
1306 /* set device type */
1308 fword("device-type");
1310 set_int_property(ph, "#address-cells", 1);
1311 set_int_property(ph, "#size-cells", 0);
1315 ob_ide_ctrl_decodeunit(int *idx)
1320 NODE_METHODS(ob_ide_ctrl) = {
1321 { NULL, ob_ide_ctrl_initialize },
1322 { "decode-unit", ob_ide_ctrl_decodeunit },
1325 static void set_cd_alias(const char *path)
1329 aliases = find_dev("/aliases");
1331 if (get_property(aliases, "cd", NULL))
1334 set_property(aliases, "cd", path, strlen(path) + 1);
1335 set_property(aliases, "cdrom", path, strlen(path) + 1);
1338 static void set_hd_alias(const char *path)
1342 aliases = find_dev("/aliases");
1344 if (get_property(aliases, "hd", NULL))
1347 set_property(aliases, "hd", path, strlen(path) + 1);
1348 set_property(aliases, "disk", path, strlen(path) + 1);
1351 static void set_ide_alias(const char *path)
1354 static int ide_counter = 0;
1357 aliases = find_dev("/aliases");
1359 snprintf(idestr, sizeof(idestr), "ide%d", ide_counter++);
1360 set_property(aliases, idestr, path, strlen(path) + 1);
1363 int ob_ide_init(const char *path, uint32_t io_port0, uint32_t ctl_port0,
1364 uint32_t io_port1, uint32_t ctl_port1)
1369 struct ide_channel *chan;
1370 int io_ports[IDE_MAX_CHANNELS];
1371 int ctl_ports[IDE_MAX_CHANNELS];
1374 io_ports[0] = io_port0;
1375 ctl_ports[0] = ctl_port0;
1376 io_ports[1] = io_port1;
1377 ctl_ports[1] = ctl_port1;
1379 for (i = 0; i < IDE_NUM_CHANNELS; i++, current_channel++) {
1381 chan = malloc(sizeof(struct ide_channel));
1383 snprintf(chan->name, sizeof(chan->name),
1384 DEV_NAME, current_channel);
1388 for (j = 0; j < 8; j++)
1389 chan->io_regs[j] = io_ports[i] + j;
1391 chan->io_regs[8] = ctl_ports[i];
1392 chan->io_regs[9] = ctl_ports[i] + 1;
1394 chan->obide_inb = ob_ide_inb;
1395 chan->obide_insw = ob_ide_insw;
1396 chan->obide_outb = ob_ide_outb;
1397 chan->obide_outsw = ob_ide_outsw;
1399 chan->selected = -1;
1402 * assume it's there, if not io port dead check will clear
1406 for (j = 0; j < 2; j++) {
1407 chan->drives[j].present = 0;
1408 chan->drives[j].unit = j;
1409 chan->drives[j].channel = chan;
1410 /* init with a decent value */
1411 chan->drives[j].bs = 512;
1413 chan->drives[j].nr = i * 2 + j;
1416 ide_add_channel(chan);
1423 ob_ide_identify_drives(chan);
1425 snprintf(nodebuff, sizeof(nodebuff), "%s/" DEV_NAME, path,
1427 REGISTER_NAMED_NODE(ob_ide_ctrl, nodebuff);
1429 dnode = find_dev(nodebuff);
1431 #if !defined(CONFIG_PPC) && !defined(CONFIG_SPARC64)
1432 props[0]=14; props[1]=0;
1433 set_property(dnode, "interrupts",
1434 (char *)&props, 2*sizeof(props[0]));
1437 props[0] = __cpu_to_be32(chan->io_regs[0]);
1438 props[1] = __cpu_to_be32(1); props[2] = __cpu_to_be32(8);
1439 props[3] = __cpu_to_be32(chan->io_regs[8]);
1440 props[4] = __cpu_to_be32(1); props[5] = __cpu_to_be32(2);
1441 set_property(dnode, "reg", (char *)&props, 6*sizeof(props[0]));
1443 IDE_DPRINTF(DEV_NAME": [io ports 0x%x-0x%x,0x%x]\n",
1444 current_channel, chan->io_regs[0],
1445 chan->io_regs[0] + 7, chan->io_regs[8]);
1447 for (j = 0; j < 2; j++) {
1448 struct ide_drive *drive = &chan->drives[j];
1449 const char *media = "UNKNOWN";
1451 if (!drive->present)
1454 IDE_DPRINTF(" drive%d [ATA%s ", j,
1455 drive->type == ide_type_atapi ? "PI" : "");
1456 switch (drive->media) {
1457 case ide_media_floppy:
1460 case ide_media_cdrom:
1463 case ide_media_optical:
1466 case ide_media_disk:
1470 IDE_DPRINTF("%s]: %s\n", media, drive->model);
1471 snprintf(nodebuff, sizeof(nodebuff),
1472 "%s/" DEV_NAME "/%s", path, current_channel,
1474 REGISTER_NAMED_NODE(ob_ide, nodebuff);
1475 dnode=find_dev(nodebuff);
1476 set_int_property(dnode, "reg", j);
1478 /* create aliases */
1480 set_ide_alias(nodebuff);
1481 if (drive->media == ide_media_cdrom)
1482 set_cd_alias(nodebuff);
1483 if (drive->media == ide_media_disk)
1484 set_hd_alias(nodebuff);
1491 #if defined(CONFIG_DRIVER_MACIO)
1492 static unsigned char
1493 macio_ide_inb(struct ide_channel *chan, unsigned int port)
1495 return in_8((unsigned char*)(chan->mmio + (port << 4)));
1499 macio_ide_outb(struct ide_channel *chan, unsigned char data, unsigned int port)
1501 out_8((unsigned char*)(chan->mmio + (port << 4)), data);
1505 macio_ide_insw(struct ide_channel *chan,
1506 unsigned int port, unsigned char *addr, unsigned int count)
1508 _insw((uint16_t*)(chan->mmio + (port << 4)), addr, count);
1512 macio_ide_outsw(struct ide_channel *chan,
1513 unsigned int port, unsigned char *addr, unsigned int count)
1515 _outsw((uint16_t*)(chan->mmio + (port << 4)), addr, count);
1518 #define MACIO_IDE_OFFSET 0x00020000
1519 #define MACIO_IDE_SIZE 0x00001000
1521 int macio_ide_init(const char *path, uint32_t addr, int nb_channels)
1527 struct ide_channel *chan;
1529 /* IDE ports on Macs are numbered from 3.
1530 * Also see comments in macio.c:openpic_init() */
1531 current_channel = 3;
1533 for (i = 0; i < nb_channels; i++, current_channel++) {
1535 chan = malloc(sizeof(struct ide_channel));
1537 snprintf(chan->name, sizeof(chan->name),
1538 DEV_NAME, current_channel);
1540 chan->mmio = addr + MACIO_IDE_OFFSET + i * MACIO_IDE_SIZE;
1542 chan->obide_inb = macio_ide_inb;
1543 chan->obide_insw = macio_ide_insw;
1544 chan->obide_outb = macio_ide_outb;
1545 chan->obide_outsw = macio_ide_outsw;
1547 chan->selected = -1;
1550 * assume it's there, if not io port dead check will clear
1554 for (j = 0; j < 2; j++) {
1555 chan->drives[j].present = 0;
1556 chan->drives[j].unit = j;
1557 chan->drives[j].channel = chan;
1558 /* init with a decent value */
1559 chan->drives[j].bs = 512;
1561 chan->drives[j].nr = i * 2 + j;
1566 if (!chan->present) {
1571 ide_add_channel(chan);
1573 ob_ide_identify_drives(chan);
1575 snprintf(nodebuff, sizeof(nodebuff), "%s/" DEV_NAME, path,
1577 REGISTER_NAMED_NODE(ob_ide_ctrl, nodebuff);
1579 dnode = find_dev(nodebuff);
1581 set_property(dnode, "compatible", (is_oldworld() ?
1582 "heathrow-ata" : "keylargo-ata"), 13);
1584 props[0] = 0x00000526;
1585 props[1] = 0x00000085;
1586 props[2] = 0x00000025;
1587 props[3] = 0x00000025;
1588 props[4] = 0x00000025;
1589 props[5] = 0x00000000;
1590 props[6] = 0x00000000;
1591 props[7] = 0x00000000;
1592 OLDWORLD(set_property(dnode, "AAPL,pio-timing",
1593 (char *)&props, 8*sizeof(props[0])));
1595 /* The first interrupt entry is the ide interrupt, the second
1596 the dbdma interrupt */
1599 props[0] = 0x0000000d;
1600 props[2] = 0x00000002;
1603 props[0] = 0x0000000e;
1604 props[2] = 0x00000003;
1607 props[0] = 0x0000000f;
1608 props[2] = 0x00000004;
1611 props[0] = 0x00000000;
1612 props[2] = 0x00000000;
1615 props[1] = 0x00000000; /* XXX level triggered on real hw */
1616 props[3] = 0x00000000;
1617 NEWWORLD(set_property(dnode, "interrupts",
1618 (char *)&props, 4*sizeof(props[0])));
1619 NEWWORLD(set_int_property(dnode, "#interrupt-cells", 2));
1621 props[1] = props[2];
1622 OLDWORLD(set_property(dnode, "AAPL,interrupts",
1623 (char *)&props, 2*sizeof(props[0])));
1625 props[0] = MACIO_IDE_OFFSET + i * MACIO_IDE_SIZE;
1626 props[1] = MACIO_IDE_SIZE;
1627 props[2] = 0x00008b00 + i * 0x0200;
1629 set_property(dnode, "reg", (char *)&props, 4*sizeof(props[0]));
1631 props[0] = addr + MACIO_IDE_OFFSET + i * MACIO_IDE_SIZE;
1632 props[1] = addr + 0x00008b00 + i * 0x0200;
1633 OLDWORLD(set_property(dnode, "AAPL,address",
1634 (char *)&props, 2*sizeof(props[0])));
1637 OLDWORLD(set_property(dnode, "AAPL,bus-id", (char*)props,
1638 1 * sizeof(props[0])));
1639 IDE_DPRINTF(DEV_NAME": [io ports 0x%lx]\n",
1640 current_channel, chan->mmio);
1642 for (j = 0; j < 2; j++) {
1643 struct ide_drive *drive = &chan->drives[j];
1644 const char *media = "UNKNOWN";
1646 if (!drive->present)
1649 IDE_DPRINTF(" drive%d [ATA%s ", j,
1650 drive->type == ide_type_atapi ? "PI" : "");
1651 switch (drive->media) {
1652 case ide_media_floppy:
1655 case ide_media_cdrom:
1658 case ide_media_optical:
1661 case ide_media_disk:
1665 IDE_DPRINTF("%s]: %s\n", media, drive->model);
1666 snprintf(nodebuff, sizeof(nodebuff),
1667 "%s/" DEV_NAME "/%s", path, current_channel,
1669 REGISTER_NAMED_NODE(ob_ide, nodebuff);
1670 dnode = find_dev(nodebuff);
1671 set_int_property(dnode, "reg", j);
1673 /* create aliases */
1675 set_ide_alias(nodebuff);
1676 if (drive->media == ide_media_cdrom)
1677 set_cd_alias(nodebuff);
1678 if (drive->media == ide_media_disk)
1679 set_hd_alias(nodebuff);
1685 #endif /* CONFIG_DRIVER_MACIO */