Unify cd emulation access and main disk access code.
[seabios.git] / src / ata.c
1 // Low level ATA disk access
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "types.h" // u8
9 #include "ioport.h" // inb
10 #include "util.h" // dprintf
11 #include "cmos.h" // inb_cmos
12 #include "pic.h" // enable_hwirq
13 #include "biosvar.h" // GET_EBDA
14 #include "pci.h" // foreachpci
15 #include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
16 #include "pci_regs.h" // PCI_INTERRUPT_LINE
17 #include "boot.h" // add_bcv_hd
18 #include "disk.h" // struct ata_s
19 #include "ata.h" // ATA_CB_STAT
20
21 #define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
22
23 struct ata_channel_s ATA_channels[CONFIG_MAX_ATA_INTERFACES] VAR16VISIBLE;
24
25
26 /****************************************************************
27  * Helper functions
28  ****************************************************************/
29
30 // Wait for the specified ide state
31 static inline int
32 await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
33 {
34     u64 end = calc_future_tsc(timeout);
35     for (;;) {
36         u8 status = inb(base+ATA_CB_STAT);
37         if ((status & mask) == flags)
38             return status;
39         if (rdtscll() > end) {
40             dprintf(1, "IDE time out\n");
41             return -1;
42         }
43     }
44 }
45
46 // Wait for the device to be not-busy.
47 static int
48 await_not_bsy(u16 base)
49 {
50     return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
51 }
52
53 // Wait for the device to be ready.
54 static int
55 await_rdy(u16 base)
56 {
57     return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
58 }
59
60 // Wait for ide state - pauses for one ata cycle first.
61 static inline int
62 pause_await_not_bsy(u16 iobase1, u16 iobase2)
63 {
64     // Wait one PIO transfer cycle.
65     inb(iobase2 + ATA_CB_ASTAT);
66
67     return await_not_bsy(iobase1);
68 }
69
70 // Wait for ide state - pause for 400ns first.
71 static inline int
72 ndelay_await_not_bsy(u16 iobase1)
73 {
74     ndelay(400);
75     return await_not_bsy(iobase1);
76 }
77
78 // Reset a drive
79 static void
80 ata_reset(int driveid)
81 {
82     u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
83     u8 channel = ataid / 2;
84     u8 slave = ataid % 2;
85     u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
86     u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
87
88     dprintf(6, "ata_reset driveid=%d\n", driveid);
89     // Pulse SRST
90     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
91     udelay(5);
92     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
93     mdelay(2);
94
95     // wait for device to become not busy.
96     int status = await_not_bsy(iobase1);
97     if (status < 0)
98         goto done;
99     if (slave) {
100         // Change device.
101         u64 end = calc_future_tsc(IDE_TIMEOUT);
102         for (;;) {
103             outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
104             status = ndelay_await_not_bsy(iobase1);
105             if (status < 0)
106                 goto done;
107             if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
108                 break;
109             // Change drive request failed to take effect - retry.
110             if (rdtscll() > end) {
111                 dprintf(1, "ata_reset slave time out\n");
112                 goto done;
113             }
114         }
115     } else {
116         // QEMU doesn't reset dh on reset, so set it explicitly.
117         outb(ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
118     }
119
120     // On a user-reset request, wait for RDY if it is an ATA device.
121     u8 type=GET_GLOBAL(Drives.drives[driveid].type);
122     if (type == DTYPE_ATA)
123         status = await_rdy(iobase1);
124
125 done:
126     // Enable interrupts
127     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
128
129     dprintf(6, "ata_reset exit status=%x\n", status);
130 }
131
132 static int
133 isready(int driveid)
134 {
135     // Read the status from controller
136     u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
137     u8 channel = ataid / 2;
138     u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
139     u8 status = inb(iobase1 + ATA_CB_STAT);
140     if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY)
141         return DISK_RET_SUCCESS;
142     return DISK_RET_ENOTREADY;
143 }
144
145 static int
146 process_ata_misc_op(struct disk_op_s *op)
147 {
148     if (!CONFIG_ATA)
149         return 0;
150
151     switch (op->command) {
152     case CMD_RESET:
153         ata_reset(op->driveid);
154         return DISK_RET_SUCCESS;
155     case CMD_ISREADY:
156         return isready(op->driveid);
157     case CMD_FORMAT:
158     case CMD_VERIFY:
159     case CMD_SEEK:
160         return DISK_RET_SUCCESS;
161     default:
162         op->count = 0;
163         return DISK_RET_EPARAM;
164     }
165 }
166
167
168 /****************************************************************
169  * ATA send command
170  ****************************************************************/
171
172 struct ata_pio_command {
173     u8 feature;
174     u8 sector_count;
175     u8 lba_low;
176     u8 lba_mid;
177     u8 lba_high;
178     u8 device;
179     u8 command;
180
181     u8 sector_count2;
182     u8 lba_low2;
183     u8 lba_mid2;
184     u8 lba_high2;
185 };
186
187 // Send an ata command to the drive.
188 static int
189 send_cmd(int driveid, struct ata_pio_command *cmd)
190 {
191     u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
192     u8 channel = ataid / 2;
193     u8 slave = ataid % 2;
194     u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
195     u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
196
197     // Disable interrupts
198     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
199
200     // Select device
201     int status = await_not_bsy(iobase1);
202     if (status < 0)
203         return status;
204     u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
205                 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
206     u8 olddh = inb(iobase1 + ATA_CB_DH);
207     outb(newdh, iobase1 + ATA_CB_DH);
208     if ((olddh ^ newdh) & (1<<4)) {
209         // Was a device change - wait for device to become not busy.
210         status = ndelay_await_not_bsy(iobase1);
211         if (status < 0)
212             return status;
213     }
214
215     if (cmd->command & 0x04) {
216         outb(0x00, iobase1 + ATA_CB_FR);
217         outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
218         outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
219         outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
220         outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
221     }
222     outb(cmd->feature, iobase1 + ATA_CB_FR);
223     outb(cmd->sector_count, iobase1 + ATA_CB_SC);
224     outb(cmd->lba_low, iobase1 + ATA_CB_SN);
225     outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
226     outb(cmd->lba_high, iobase1 + ATA_CB_CH);
227     outb(cmd->command, iobase1 + ATA_CB_CMD);
228
229     status = ndelay_await_not_bsy(iobase1);
230     if (status < 0)
231         return status;
232
233     if (status & ATA_CB_STAT_ERR) {
234         dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
235                 , status, inb(iobase1 + ATA_CB_ERR));
236         return -4;
237     }
238     if (!(status & ATA_CB_STAT_DRQ)) {
239         dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
240         return -5;
241     }
242
243     return 0;
244 }
245
246
247 /****************************************************************
248  * ATA transfers
249  ****************************************************************/
250
251 // Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
252 // 'op->driveid'.
253 static int
254 ata_transfer(struct disk_op_s *op, int iswrite, int blocksize)
255 {
256     dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d buf=%p\n"
257             , op->driveid, iswrite, op->count, blocksize, op->buf_fl);
258
259     u8 ataid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
260     u8 channel = ataid / 2;
261     u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
262     u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
263     int count = op->count;
264     void *buf_fl = op->buf_fl;
265     int status;
266     for (;;) {
267         if (iswrite) {
268             // Write data to controller
269             dprintf(16, "Write sector id=%d dest=%p\n", op->driveid, buf_fl);
270             if (CONFIG_ATA_PIO32)
271                 outsl_fl(iobase1, buf_fl, blocksize / 4);
272             else
273                 outsw_fl(iobase1, buf_fl, blocksize / 2);
274         } else {
275             // Read data from controller
276             dprintf(16, "Read sector id=%d dest=%p\n", op->driveid, buf_fl);
277             if (CONFIG_ATA_PIO32)
278                 insl_fl(iobase1, buf_fl, blocksize / 4);
279             else
280                 insw_fl(iobase1, buf_fl, blocksize / 2);
281         }
282         buf_fl += blocksize;
283
284         status = pause_await_not_bsy(iobase1, iobase2);
285         if (status < 0) {
286             // Error
287             op->count -= count;
288             return status;
289         }
290
291         count--;
292         if (!count)
293             break;
294         status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
295         if (status != ATA_CB_STAT_DRQ) {
296             dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
297                     , status);
298             op->count -= count;
299             return -6;
300         }
301     }
302
303     status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
304                | ATA_CB_STAT_ERR);
305     if (!iswrite)
306         status &= ~ATA_CB_STAT_DF;
307     if (status != 0) {
308         dprintf(6, "ata_transfer : no sectors left (status %02x)\n", status);
309         return -7;
310     }
311
312     // Enable interrupts
313     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
314     return 0;
315 }
316
317
318 /****************************************************************
319  * ATA hard drive functions
320  ****************************************************************/
321
322 // Read/write count blocks from a harddrive.
323 static int
324 ata_cmd_data(struct disk_op_s *op, int iswrite, int command)
325 {
326     u64 lba = op->lba;
327
328     struct ata_pio_command cmd;
329     memset(&cmd, 0, sizeof(cmd));
330
331     cmd.command = command;
332     if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
333         cmd.sector_count2 = op->count >> 8;
334         cmd.lba_low2 = lba >> 24;
335         cmd.lba_mid2 = lba >> 32;
336         cmd.lba_high2 = lba >> 40;
337
338         cmd.command |= 0x04;
339         lba &= 0xffffff;
340     }
341
342     cmd.feature = 0;
343     cmd.sector_count = op->count;
344     cmd.lba_low = lba;
345     cmd.lba_mid = lba >> 8;
346     cmd.lba_high = lba >> 16;
347     cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
348
349     int ret = send_cmd(op->driveid, &cmd);
350     if (ret)
351         return ret;
352     return ata_transfer(op, iswrite, DISK_SECTOR_SIZE);
353 }
354
355 int
356 process_ata_op(struct disk_op_s *op)
357 {
358     if (!CONFIG_ATA)
359         return 0;
360
361     int ret;
362     switch (op->command) {
363     case CMD_READ:
364         ret = ata_cmd_data(op, 0, ATA_CMD_READ_SECTORS);
365         break;
366     case CMD_WRITE:
367         ret = ata_cmd_data(op, 1, ATA_CMD_WRITE_SECTORS);
368         break;
369     default:
370         return process_ata_misc_op(op);
371     }
372     if (ret)
373         return DISK_RET_EBADTRACK;
374     return DISK_RET_SUCCESS;
375 }
376
377
378 /****************************************************************
379  * ATAPI functions
380  ****************************************************************/
381
382 // Low-level atapi command transmit function.
383 static int
384 send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
385 {
386     u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
387     u8 channel = ataid / 2;
388     u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
389     u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
390
391     struct ata_pio_command cmd;
392     cmd.sector_count = 0;
393     cmd.feature = 0;
394     cmd.lba_low = 0;
395     cmd.lba_mid = blocksize;
396     cmd.lba_high = blocksize >> 8;
397     cmd.device = 0;
398     cmd.command = ATA_CMD_PACKET;
399
400     int ret = send_cmd(driveid, &cmd);
401     if (ret)
402         return ret;
403
404     // Send command to device
405     outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
406
407     int status = pause_await_not_bsy(iobase1, iobase2);
408     if (status < 0)
409         return status;
410
411     if (status & ATA_CB_STAT_ERR) {
412         u8 err = inb(iobase1 + ATA_CB_ERR);
413         // skip "Not Ready"
414         if (err != 0x20)
415             dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
416                     , status, err);
417         return -2;
418     }
419     if (!(status & ATA_CB_STAT_DRQ)) {
420         dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
421         return -3;
422     }
423
424     return 0;
425 }
426
427 // Read sectors from the cdrom.
428 int
429 cdrom_read(struct disk_op_s *op)
430 {
431     u8 atacmd[12];
432     memset(atacmd, 0, sizeof(atacmd));
433     atacmd[0]=0x28;                         // READ command
434     atacmd[7]=(op->count & 0xff00) >> 8;    // Sectors
435     atacmd[8]=(op->count & 0x00ff);
436     atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
437     atacmd[3]=(op->lba & 0x00ff0000) >> 16;
438     atacmd[4]=(op->lba & 0x0000ff00) >> 8;
439     atacmd[5]=(op->lba & 0x000000ff);
440
441     int ret = send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
442                              , CDROM_SECTOR_SIZE);
443     if (ret)
444         return ret;
445
446     return ata_transfer(op, 0, CDROM_SECTOR_SIZE);
447 }
448
449 int
450 process_atapi_op(struct disk_op_s *op)
451 {
452     int ret;
453     switch (op->command) {
454     case CMD_READ:
455         ret = cdrom_read(op);
456         break;
457     case CMD_FORMAT:
458     case CMD_WRITE:
459         return DISK_RET_EWRITEPROTECT;
460     default:
461         return process_ata_misc_op(op);
462     }
463     if (ret)
464         return DISK_RET_EBADTRACK;
465     return DISK_RET_SUCCESS;
466 }
467
468 // Send a simple atapi command to a drive.
469 int
470 ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
471                , u32 length, void *buf_fl)
472 {
473     int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
474     if (ret)
475         return ret;
476
477     struct disk_op_s dop;
478     memset(&dop, 0, sizeof(dop));
479     dop.driveid = driveid;
480     dop.count = 1;
481     dop.buf_fl = buf_fl;
482
483     return ata_transfer(&dop, 0, length);
484 }
485
486
487 /****************************************************************
488  * ATA detect and init
489  ****************************************************************/
490
491 // Extract the ATA/ATAPI version info.
492 static int
493 extract_version(u16 *buffer)
494 {
495     // Extract ATA/ATAPI version.
496     u16 ataversion = buffer[80];
497     u8 version;
498     for (version=15; version>0; version--)
499         if (ataversion & (1<<version))
500             break;
501     return version;
502 }
503
504 // Extract common information from IDENTIFY commands.
505 static void
506 extract_identify(int driveid, u16 *buffer)
507 {
508     dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
509
510     // Read model name
511     char *model = Drives.drives[driveid].model;
512     int maxsize = ARRAY_SIZE(Drives.drives[driveid].model);
513     int i;
514     for (i=0; i<maxsize/2; i++) {
515         u16 v = buffer[27+i];
516         model[i*2] = v >> 8;
517         model[i*2+1] = v & 0xff;
518     }
519     model[maxsize-1] = 0x00;
520
521     // Trim trailing spaces from model name.
522     for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
523         model[i] = 0x00;
524
525     // Common flags.
526     SET_GLOBAL(Drives.drives[driveid].removable, (buffer[0] & 0x80) ? 1 : 0);
527     SET_GLOBAL(Drives.drives[driveid].cntl_info, extract_version(buffer));
528 }
529
530 void
531 describe_atapi(int driveid)
532 {
533     u8 ataid = Drives.drives[driveid].cntl_id;
534     u8 channel = ataid / 2;
535     u8 slave = ataid % 2;
536     u8 version = Drives.drives[driveid].cntl_info;
537     int iscd = Drives.drives[driveid].floppy_type;
538     printf("ata%d-%d: %s ATAPI-%d %s", channel, slave
539            , Drives.drives[driveid].model, version
540            , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
541 }
542
543 static int
544 init_drive_atapi(int driveid, u16 *buffer)
545 {
546     // Send an IDENTIFY_DEVICE_PACKET command to device
547     memset(buffer, 0, DISK_SECTOR_SIZE);
548     struct disk_op_s dop;
549     memset(&dop, 0, sizeof(dop));
550     dop.driveid = driveid;
551     dop.count = 1;
552     dop.lba = 1;
553     dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
554     int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE_PACKET);
555     if (ret)
556         return ret;
557
558     // Success - setup as ATAPI.
559     extract_identify(driveid, buffer);
560     SET_GLOBAL(Drives.drives[driveid].type, DTYPE_ATAPI);
561     SET_GLOBAL(Drives.drives[driveid].blksize, CDROM_SECTOR_SIZE);
562     SET_GLOBAL(Drives.drives[driveid].sectors, (u64)-1);
563     u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
564     SET_GLOBAL(Drives.drives[driveid].floppy_type, iscd);
565
566     // fill cdidmap
567     if (iscd)
568         map_cd_drive(driveid);
569
570     return 0;
571 }
572
573 void
574 describe_ata(int driveid)
575 {
576     u8 ataid = Drives.drives[driveid].cntl_id;
577     u8 channel = ataid / 2;
578     u8 slave = ataid % 2;
579     u64 sectors = Drives.drives[driveid].sectors;
580     u8 version = Drives.drives[driveid].cntl_info;
581     char *model = Drives.drives[driveid].model;
582     printf("ata%d-%d: %s ATA-%d Hard-Disk", channel, slave, model, version);
583     u64 sizeinmb = sectors >> 11;
584     if (sizeinmb < (1 << 16))
585         printf(" (%u MiBytes)", (u32)sizeinmb);
586     else
587         printf(" (%u GiBytes)", (u32)(sizeinmb >> 10));
588 }
589
590 static int
591 init_drive_ata(int driveid, u16 *buffer)
592 {
593     // Send an IDENTIFY_DEVICE command to device
594     memset(buffer, 0, DISK_SECTOR_SIZE);
595     struct disk_op_s dop;
596     memset(&dop, 0, sizeof(dop));
597     dop.driveid = driveid;
598     dop.count = 1;
599     dop.lba = 1;
600     dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
601     int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE);
602     if (ret)
603         return ret;
604
605     // Success - setup as ATA.
606     extract_identify(driveid, buffer);
607     SET_GLOBAL(Drives.drives[driveid].type, DTYPE_ATA);
608     SET_GLOBAL(Drives.drives[driveid].blksize, DISK_SECTOR_SIZE);
609
610     SET_GLOBAL(Drives.drives[driveid].pchs.cylinders, buffer[1]);
611     SET_GLOBAL(Drives.drives[driveid].pchs.heads, buffer[3]);
612     SET_GLOBAL(Drives.drives[driveid].pchs.spt, buffer[6]);
613
614     u64 sectors;
615     if (buffer[83] & (1 << 10)) // word 83 - lba48 support
616         sectors = *(u64*)&buffer[100]; // word 100-103
617     else
618         sectors = *(u32*)&buffer[60]; // word 60 and word 61
619     SET_GLOBAL(Drives.drives[driveid].sectors, sectors);
620
621     // Setup disk geometry translation.
622     setup_translation(driveid);
623
624     // Register with bcv system.
625     add_bcv_internal(driveid);
626
627     return 0;
628 }
629
630 static int
631 powerup_await_non_bsy(u16 base, u64 end)
632 {
633     u8 orstatus = 0;
634     u8 status;
635     for (;;) {
636         status = inb(base+ATA_CB_STAT);
637         if (!(status & ATA_CB_STAT_BSY))
638             break;
639         orstatus |= status;
640         if (orstatus == 0xff) {
641             dprintf(1, "powerup IDE floating\n");
642             return orstatus;
643         }
644         if (rdtscll() > end) {
645             dprintf(1, "powerup IDE time out\n");
646             return -1;
647         }
648     }
649     dprintf(6, "powerup iobase=%x st=%x\n", base, status);
650     return status;
651 }
652
653 static void
654 ata_detect()
655 {
656     // Device detection
657     u64 end = calc_future_tsc(IDE_TIMEOUT);
658     int ataid, last_reset_ataid=-1;
659     for (ataid=0; ataid<CONFIG_MAX_ATA_INTERFACES*2; ataid++) {
660         u8 channel = ataid / 2;
661         u8 slave = ataid % 2;
662
663         u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
664         if (!iobase1)
665             break;
666
667         // Wait for not-bsy.
668         int status = powerup_await_non_bsy(iobase1, end);
669         if (status < 0)
670             continue;
671         u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
672         outb(newdh, iobase1+ATA_CB_DH);
673         ndelay(400);
674         status = powerup_await_non_bsy(iobase1, end);
675         if (status < 0)
676             continue;
677
678         // Check if ioport registers look valid.
679         outb(newdh, iobase1+ATA_CB_DH);
680         u8 dh = inb(iobase1+ATA_CB_DH);
681         outb(0x55, iobase1+ATA_CB_SC);
682         outb(0xaa, iobase1+ATA_CB_SN);
683         u8 sc = inb(iobase1+ATA_CB_SC);
684         u8 sn = inb(iobase1+ATA_CB_SN);
685         dprintf(6, "ata_detect ataid=%d sc=%x sn=%x dh=%x\n"
686                 , ataid, sc, sn, dh);
687         if (sc != 0x55 || sn != 0xaa || dh != newdh)
688             continue;
689
690         // Prepare new driveid.
691         u8 driveid = GET_GLOBAL(Drives.drivecount);
692         if (driveid >= ARRAY_SIZE(Drives.drives))
693             break;
694         memset(&Drives.drives[driveid], 0, sizeof(Drives.drives[0]));
695         Drives.drives[driveid].cntl_id = ataid;
696
697         // reset the channel
698         if (slave && ataid == last_reset_ataid + 1) {
699             // The drive was just reset - no need to reset it again.
700         } else {
701             ata_reset(driveid);
702             last_reset_ataid = ataid;
703         }
704
705         // check for ATAPI
706         u16 buffer[256];
707         int ret = init_drive_atapi(driveid, buffer);
708         if (ret) {
709             // Didn't find an ATAPI drive - look for ATA drive.
710             u8 st = inb(iobase1+ATA_CB_STAT);
711             if (!st)
712                 // Status not set - can't be a valid drive.
713                 continue;
714
715             // Wait for RDY.
716             ret = await_rdy(iobase1);
717             if (ret < 0)
718                 continue;
719
720             // check for ATA.
721             ret = init_drive_ata(driveid, buffer);
722             if (ret)
723                 // No ATA drive found
724                 continue;
725         }
726         SET_GLOBAL(Drives.drivecount, driveid+1);
727
728         // Report drive info to user.
729         describe_drive(driveid);
730         printf("\n");
731
732         u16 resetresult = buffer[93];
733         dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
734         if (!slave && (resetresult & 0xdf61) == 0x4041)
735             // resetresult looks valid and device 0 is responding to
736             // device 1 requests - device 1 must not be present - skip
737             // detection.
738             ataid++;
739     }
740
741     printf("\n");
742 }
743
744 static void
745 ata_init()
746 {
747     // Scan PCI bus for ATA adapters
748     int count=0;
749     int bdf, max;
750     foreachpci(bdf, max) {
751         if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
752             continue;
753         if (count >= ARRAY_SIZE(ATA_channels))
754             break;
755
756         u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
757         SET_GLOBAL(ATA_channels[count].irq, irq);
758         SET_GLOBAL(ATA_channels[count].pci_bdf, bdf);
759
760         u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
761         u32 port1, port2;
762
763         if (prog_if & 1) {
764             port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
765             port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
766         } else {
767             port1 = 0x1f0;
768             port2 = 0x3f0;
769         }
770         SET_GLOBAL(ATA_channels[count].iobase1, port1);
771         SET_GLOBAL(ATA_channels[count].iobase2, port2);
772         dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
773                 , count, port1, port2, bdf, prog_if);
774         count++;
775
776         if (prog_if & 4) {
777             port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
778             port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
779         } else {
780             port1 = 0x170;
781             port2 = 0x370;
782         }
783         dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
784                 , count, port1, port2, bdf, prog_if);
785         SET_GLOBAL(ATA_channels[count].iobase1, port1);
786         SET_GLOBAL(ATA_channels[count].iobase2, port2);
787         count++;
788     }
789 }
790
791 void
792 ata_setup()
793 {
794     if (!CONFIG_ATA)
795         return;
796
797     dprintf(3, "init hard drives\n");
798     ata_init();
799     ata_detect();
800
801     SET_BDA(disk_control_byte, 0xc0);
802
803     enable_hwirq(14, entry_76);
804 }