1 // Low level ATA disk access
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU GPLv3 license.
8 #include "ata.h" // ATA_*
9 #include "types.h" // u8
10 #include "ioport.h" // inb
11 #include "util.h" // BX_INFO
12 #include "cmos.h" // inb_cmos
18 #define NOT_BSY_NOT_DRQ 4
21 #define IDE_SECTOR_SIZE 512
22 #define CDROM_SECTOR_SIZE 2048
24 #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
26 #define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
27 #define DEBUGF(fmt, args...)
30 /****************************************************************
32 ****************************************************************/
34 // Wait for the specified ide state
36 await_ide(u8 when_done, u16 base, u16 timeout)
40 u8 status = inb(base+ATA_CB_STAT);
44 result = status & ATA_CB_STAT_BSY;
45 else if (when_done == NOT_BSY)
46 result = !(status & ATA_CB_STAT_BSY);
47 else if (when_done == NOT_BSY_DRQ)
48 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
49 else if (when_done == NOT_BSY_NOT_DRQ)
50 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
51 else if (when_done == NOT_BSY_RDY)
52 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
53 else if (when_done == TIMEOUT)
58 // mod 2048 each 16 ms
59 if (time>>16 != last) {
61 DEBUGF("await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY)"
62 " %d time= %d timeout= %d\n"
63 , when_done, time>>11, timeout);
65 if (status & ATA_CB_STAT_ERR) {
66 DEBUGF("await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
67 ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n"
68 , when_done, time>>11, timeout);
71 if (timeout == 0 || (time>>11) > timeout)
74 BX_INFO("IDE time out\n");
78 // Wait for ide state - pauses for one ata cycle first.
80 pause_await_ide(u8 when_done, u16 iobase1, u16 iobase2, u16 timeout)
82 // Wait one PIO transfer cycle.
83 inb(iobase2 + ATA_CB_ASTAT);
85 return await_ide(when_done, iobase1, timeout);
88 // Delay for x nanoseconds
92 // XXX - how to implement ndelay?
99 ata_reset(int driveid)
101 u16 iobase1, iobase2;
102 u8 channel, slave, sn, sc;
105 channel = driveid / 2;
108 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
109 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
113 // 8.2.1 (a) -- set SRST in DC
114 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
116 // 8.2.1 (b) -- wait for BSY
117 await_ide(BSY, iobase1, 20);
119 // 8.2.1 (f) -- clear SRST
120 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
122 type=GET_EBDA(ata.devices[driveid].type);
123 if (type != ATA_TYPE_NONE) {
125 // 8.2.1 (g) -- check for sc==sn==0x01
127 outb(slave?ATA_CB_DH_DEV1:ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
128 sc = inb(iobase1+ATA_CB_SC);
129 sn = inb(iobase1+ATA_CB_SN);
131 if ( (sc==0x01) && (sn==0x01) ) {
132 if (type == ATA_TYPE_ATA) //ATA
133 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
135 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
138 // 8.2.1 (h) -- wait for not BSY
139 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
143 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
146 struct ata_pio_command {
161 // Send an ata command to the drive.
163 send_cmd(int driveid, struct ata_pio_command *cmd)
165 u8 channel = driveid / 2;
166 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
167 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
169 int status = inb(iobase1 + ATA_CB_STAT);
170 if (status & ATA_CB_STAT_BSY)
173 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
174 if (cmd->command & 0x04) {
175 outb(0x00, iobase1 + ATA_CB_FR);
176 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
177 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
178 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
179 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
181 outb(cmd->feature, iobase1 + ATA_CB_FR);
182 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
183 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
184 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
185 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
186 outb(cmd->device, iobase1 + ATA_CB_DH);
187 outb(cmd->command, iobase1 + ATA_CB_CMD);
191 status = await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
195 if (status & ATA_CB_STAT_ERR) {
196 DEBUGF("send_cmd : read error\n");
199 if (!(status & ATA_CB_STAT_DRQ)) {
200 DEBUGF("send_cmd : DRQ not set (status %02x)\n"
201 , (unsigned) status);
208 // Read and discard x number of bytes from an io channel.
210 insx_discard(int mode, int iobase1, int bytes)
213 if (mode == ATA_MODE_PIO32) {
215 for (i=0; i<count; i++)
219 for (i=0; i<count; i++)
224 // Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
225 // 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
226 // and/or last block may be partially transferred. This function is
227 // inlined because all the callers use different forms and because the
228 // large number of parameters would consume a lot of stack space.
230 ata_transfer(int driveid, int iswrite, int count, int blocksize
231 , int skipfirst, int skiplast, void *far_buffer)
233 DEBUGF("ata_transfer id=%d write=%d count=%d bs=%d"
234 " skipf=%d skipl=%d buf=%p\n"
235 , driveid, iswrite, count, blocksize
236 , skipfirst, skiplast, far_buffer);
238 // Reset count of transferred data
239 SET_EBDA(ata.trsfsectors, 0);
241 u8 channel = driveid / 2;
242 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
243 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
244 u8 mode = GET_EBDA(ata.devices[driveid].mode);
248 int bsize = blocksize;
249 if (skipfirst && current == 0) {
250 insx_discard(mode, iobase1, skipfirst);
253 if (skiplast && current == count-1)
257 // Write data to controller
258 DEBUGF("Write sector id=%d dest=%p\n", driveid, far_buffer);
259 if (mode == ATA_MODE_PIO32)
260 outsl_far(iobase1, far_buffer, bsize / 4);
262 outsw_far(iobase1, far_buffer, bsize / 2);
264 // Read data from controller
265 DEBUGF("Read sector id=%d dest=%p\n", driveid, far_buffer);
266 if (mode == ATA_MODE_PIO32)
267 insl_far(iobase1, far_buffer, bsize / 4);
269 insw_far(iobase1, far_buffer, bsize / 2);
273 if (skiplast && current == count-1)
274 insx_discard(mode, iobase1, skiplast);
276 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
282 SET_EBDA(ata.trsfsectors, current);
283 if (current == count)
285 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
287 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
288 DEBUGF("ata_transfer : more sectors left (status %02x)\n"
289 , (unsigned) status);
294 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
295 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
297 status &= ~ATA_CB_STAT_DF;
298 if (status != ATA_CB_STAT_RDY ) {
299 DEBUGF("ata_transfer : no sectors left (status %02x)\n"
300 , (unsigned) status);
305 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
310 /****************************************************************
311 * ATA hard drive functions
312 ****************************************************************/
314 // Read/write count blocks from a harddrive.
316 ata_cmd_data(int driveid, u16 command, u32 lba, u16 count, void *far_buffer)
318 u8 slave = driveid % 2;
320 struct ata_pio_command cmd;
321 memset(&cmd, 0, sizeof(cmd));
323 if (count >= (1<<8) || lba + count >= (1<<28)) {
324 cmd.sector_count2 = count >> 8;
325 cmd.lba_low2 = lba >> 24;
334 cmd.sector_count = count;
336 cmd.lba_mid = lba >> 8;
337 cmd.lba_high = lba >> 16;
338 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
339 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
340 cmd.command = command;
342 int ret = send_cmd(driveid, &cmd);
346 int iswrite = command == ATA_CMD_WRITE_SECTORS;
347 return ata_transfer(driveid, iswrite, count, IDE_SECTOR_SIZE
352 /****************************************************************
354 ****************************************************************/
356 // Low-level atapi command transmit function.
358 send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
360 u8 channel = driveid / 2;
361 u8 slave = driveid % 2;
362 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
363 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
365 struct ata_pio_command cmd;
366 cmd.sector_count = 0;
369 cmd.lba_mid = blocksize;
370 cmd.lba_high = blocksize >> 8;
371 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
372 cmd.command = ATA_CMD_PACKET;
374 int ret = send_cmd(driveid, &cmd);
378 // Send command to device
379 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
381 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
388 // Low-level cdrom read atapi command transmit function.
390 send_cdrom_cmd(int driveid, u32 lba, u16 count)
393 memset(atacmd, 0, sizeof(atacmd));
395 atacmd[0]=0x28; // READ command
396 atacmd[7]=(count & 0xff00) >> 8; // Sectors
397 atacmd[8]=(count & 0x00ff); // Sectors
398 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
399 atacmd[3]=(lba & 0x00ff0000) >> 16;
400 atacmd[4]=(lba & 0x0000ff00) >> 8;
401 atacmd[5]=(lba & 0x000000ff);
403 return send_atapi_cmd(driveid, atacmd, sizeof(atacmd), CDROM_SECTOR_SIZE);
406 // Read sectors from the cdrom.
408 cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
410 int ret = send_cdrom_cmd(driveid, lba, count);
414 return ata_transfer(driveid, 0, count, CDROM_SECTOR_SIZE, 0, 0, far_buffer);
417 // Pretend the cdrom has 512 byte sectors (instead of 2048) and read
420 cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
422 u32 velba = vlba + vcount - 1;
424 u32 elba = velba / 4;
425 int count = elba - lba + 1;
426 int before = vlba % 4;
427 int after = 3 - (velba % 4);
429 DEBUGF("cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
430 " count=%d before=%d after=%d\n"
431 , driveid, vlba, vcount, far_buffer, lba, elba
432 , count, before, after);
434 int ret = send_cdrom_cmd(driveid, lba, count);
438 ret = ata_transfer(driveid, 0, count, CDROM_SECTOR_SIZE
439 , before*512, after*512, far_buffer);
441 SET_EBDA(ata.trsfsectors, 0);
444 SET_EBDA(ata.trsfsectors, vcount);
448 // Send a simple atapi command to a drive.
450 ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
451 , u32 length, void *far_buffer)
453 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
457 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
461 /****************************************************************
462 * ATA detect and init
463 ****************************************************************/
466 report_model(int driveid, u8 *buffer)
472 for (i=0; i<40; i+=2) {
473 model[i] = buffer[i+54+1];
474 model[i+1] = buffer[i+54];
479 for (i=39; i>0; i--) {
480 if (model[i] != 0x20)
485 u8 channel = driveid / 2;
486 u8 slave = driveid % 2;
487 // XXX - model on stack not %cs
488 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
492 get_ata_version(u8 *buffer)
494 u16 ataversion = *(u16*)&buffer[160];
496 for (version=15; version>0; version--)
497 if (ataversion & (1<<version))
503 init_drive_atapi(int driveid)
505 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI);
507 // Temporary values to do the transfer
508 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
509 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
511 // Now we send a IDENTIFY command to ATAPI device
513 memset(buffer, 0, sizeof(buffer));
514 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
516 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
518 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
520 u8 type = buffer[1] & 0x1f;
521 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
522 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
523 u16 blksize = CDROM_SECTOR_SIZE;
525 SET_EBDA(ata.devices[driveid].device, type);
526 SET_EBDA(ata.devices[driveid].removable, removable);
527 SET_EBDA(ata.devices[driveid].mode, mode);
528 SET_EBDA(ata.devices[driveid].blksize, blksize);
531 u8 cdcount = GET_EBDA(ata.cdcount);
532 SET_EBDA(ata.idmap[1][cdcount], driveid);
533 SET_EBDA(ata.cdcount, ++cdcount);
535 report_model(driveid, buffer);
536 u8 version = get_ata_version(buffer);
537 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
538 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
540 printf(" ATAPI-%d Device\n", version);
544 init_drive_ata(int driveid)
546 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATA);
548 // Temporary values to do the transfer
549 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
550 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
552 // Now we send a IDENTIFY command to ATA device
554 memset(buffer, 0, sizeof(buffer));
555 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
557 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
559 BX_PANIC("ata-detect: Failed to detect ATA device\n");
561 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
562 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
563 u16 blksize = *(u16*)&buffer[10];
565 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
566 u16 heads = *(u16*)&buffer[3*2]; // word 3
567 u16 spt = *(u16*)&buffer[6*2]; // word 6
569 u32 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
571 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_HD);
572 SET_EBDA(ata.devices[driveid].removable, removable);
573 SET_EBDA(ata.devices[driveid].mode, mode);
574 SET_EBDA(ata.devices[driveid].blksize, blksize);
575 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
576 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
577 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
578 SET_EBDA(ata.devices[driveid].sectors, sectors);
580 u8 channel = driveid / 2;
581 u8 slave = driveid % 2;
582 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
584 for (shift=driveid%4; shift>0; shift--)
588 SET_EBDA(ata.devices[driveid].translation, translation);
590 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation="
591 , channel, slave, cylinders, heads, spt);
592 switch (translation) {
593 case ATA_TRANSLATION_NONE:
596 case ATA_TRANSLATION_LBA:
600 heads = sectors / 1024;
611 cylinders = sectors / heads;
613 case ATA_TRANSLATION_RECHS:
615 // Take care not to overflow
620 cylinders = (u16)((u32)(cylinders)*16/15);
622 // then go through the large bitshift process
623 case ATA_TRANSLATION_LARGE:
624 if (translation == ATA_TRANSLATION_LARGE)
626 while(cylinders > 1024) {
630 // If we max out the head count
636 // clip to 1024 cylinders in lchs
637 if (cylinders > 1024)
639 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
641 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
642 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
643 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
646 u8 hdcount = GET_EBDA(ata.hdcount);
647 SET_EBDA(ata.idmap[0][hdcount], driveid);
648 SET_EBDA(ata.hdcount, ++hdcount);
650 u32 sizeinmb = GET_EBDA(ata.devices[driveid].sectors);
653 report_model(driveid, buffer);
654 u8 version = get_ata_version(buffer);
655 if (sizeinmb < (1 << 16))
656 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, sizeinmb);
658 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, sizeinmb >> 10);
662 init_drive_unknown(int driveid)
664 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN);
666 u8 channel = driveid / 2;
667 u8 slave = driveid % 2;
668 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
674 #if CONFIG_MAX_ATA_INTERFACES > 0
675 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
676 SET_EBDA(ata.channels[0].iobase1,0x1f0);
677 SET_EBDA(ata.channels[0].iobase2,0x3f0);
678 SET_EBDA(ata.channels[0].irq,14);
680 #if CONFIG_MAX_ATA_INTERFACES > 1
681 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
682 SET_EBDA(ata.channels[1].iobase1,0x170);
683 SET_EBDA(ata.channels[1].iobase2,0x370);
684 SET_EBDA(ata.channels[1].irq,15);
686 #if CONFIG_MAX_ATA_INTERFACES > 2
687 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
688 SET_EBDA(ata.channels[2].iobase1,0x1e8);
689 SET_EBDA(ata.channels[2].iobase2,0x3e0);
690 SET_EBDA(ata.channels[2].irq,12);
692 #if CONFIG_MAX_ATA_INTERFACES > 3
693 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
694 SET_EBDA(ata.channels[3].iobase1,0x168);
695 SET_EBDA(ata.channels[3].iobase2,0x360);
696 SET_EBDA(ata.channels[3].irq,11);
698 #if CONFIG_MAX_ATA_INTERFACES > 4
699 #error Please fill the ATA interface informations
704 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
705 u8 channel = driveid / 2;
706 u8 slave = driveid % 2;
708 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
709 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
711 // Disable interrupts
712 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
715 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
716 outb(0x55, iobase1+ATA_CB_SC);
717 outb(0xaa, iobase1+ATA_CB_SN);
718 outb(0xaa, iobase1+ATA_CB_SC);
719 outb(0x55, iobase1+ATA_CB_SN);
720 outb(0x55, iobase1+ATA_CB_SC);
721 outb(0xaa, iobase1+ATA_CB_SN);
723 // If we found something
724 u8 sc = inb(iobase1+ATA_CB_SC);
725 u8 sn = inb(iobase1+ATA_CB_SN);
727 if (sc != 0x55 || sn != 0xaa)
733 // check for ATA or ATAPI
734 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
735 sc = inb(iobase1+ATA_CB_SC);
736 sn = inb(iobase1+ATA_CB_SN);
737 if (sc!=0x01 || sn!=0x01) {
738 init_drive_unknown(driveid);
741 u8 cl = inb(iobase1+ATA_CB_CL);
742 u8 ch = inb(iobase1+ATA_CB_CH);
743 u8 st = inb(iobase1+ATA_CB_STAT);
745 if (cl==0x14 && ch==0xeb)
746 init_drive_atapi(driveid);
747 else if (cl==0x00 && ch==0x00 && st!=0x00)
748 init_drive_ata(driveid);
749 else if (cl==0xff && ch==0xff)
753 init_drive_unknown(driveid);
756 // Store the device count
757 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
761 // FIXME : should use bios=cmos|auto|disable bits
762 // FIXME : should know about translation bits
763 // FIXME : move hard_drive_post here