summaryrefslogtreecommitdiff
path: root/Source/Kernel/Devices/ATA/ATAController.class.cpp
blob: d8073d08e5aebe365d8712a49e30f8726076c676 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "ATADrive.class.h"

#include <DeviceManager/Dev.ns.h>
#include <VFS/Part.ns.h>

String ATAController::getClass() {
	return "controller.ata";
}

String ATAController::getName() {
	return String("ATA controller #") + String::number(m_number);
}

void ATAController::detect() {
	ATAController *c[2];
	c[0] = new ATAController(ATA_BUS1_BASE, 0);
	c[1] = new ATAController(ATA_BUS2_BASE, 1);
	Dev::registerDevice(c[0]); Dev::registerDevice(c[1]);
	for (u32int d = 0; d < 2; d++) {
		for (u32int r = 0; r < 2; r++) {
			ATADrive* drv = c[d]->m_drives[r];
			if (drv != 0) {
				Dev::registerDevice(drv);
				Part::registerDevice(drv);
			}
		}
	}
}

ATAController::ATAController(u32int base, u8int number) : Mutex(MUTEX_TRUE) {
	m_base = base; m_number = number;
	m_drives[0] = NULL; m_drives[1] = NULL;
	identify(false);	//Identify master device
	identify(true);		//Identify slave device
	unlock();
}

void ATAController::identify(bool slave) {
	if (!locked()) return;
	if (m_drives[(slave ? 1 : 0)] != NULL) return;
	writeByte(ATA_PORT_DRIVE_SELECT, (slave ? 0xB0 : 0xA0)); 
	writeByte(ATA_PORT_COMMAND, ATA_CMD_IDENTIFY);
	u8int ret = readByte(ATA_PORT_COMMAND);
	if (ret == 0) return;	//Drive does not exist
	while ((ret & 0x88) != 0x08 and (ret & 1) != 1) {
		ret = readByte(ATA_PORT_COMMAND);
	}
	if ((ret & 1) == 1) return;	//Error while IDENTIFY
	u16int data[256];
	for (u32int i = 0; i < 256; i++) data[i] = readWord(ATA_PORT_DATA);
	u32int blocks = (data[61] << 16) | data[60];
	if (blocks != 0) {	//Drive supports LBA28
		m_drives[(slave ? 1 : 0)] = new ATADrive(this, slave, blocks, data);
	}
}