From: AceVest Date: Tue, 27 May 2014 14:41:27 +0000 (+0800) Subject: ide.c can drive my real disk with irq now. X-Git-Tag: 0.3.0~59 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/pkcs11-keygen.html?a=commitdiff_plain;h=73adce58bbcad7810ef06faa62ecf53f41fa1157;p=kernel.git ide.c can drive my real disk with irq now. --- diff --git a/drivers/ahci.c b/drivers/ahci.c new file mode 100644 index 0000000..fd112c9 --- /dev/null +++ b/drivers/ahci.c @@ -0,0 +1,24 @@ +/* + * ------------------------------------------------------------------------ + * File Name: ahci.c + * Author: Zhao Yanbai + * Sat May 24 20:05:49 2014 + * Description: none + * ------------------------------------------------------------------------ + */ + +#include +#include +#include +#include +#include +#include +#include + + +ahci_init() +{ + + + +} diff --git a/drivers/ide.c b/drivers/ide.c index 07610a4..7b1fc44 100644 --- a/drivers/ide.c +++ b/drivers/ide.c @@ -11,14 +11,16 @@ #include #include #include -#include #include #include #include -/* ata command base register */ -#define HD_CHL0_CMD_BASE 0x1F0 -#define HD_CHL1_CMD_BASE 0x170 +unsigned int HD_CHL0_CMD_BASE = 0x1F0; +unsigned int HD_CHL1_CMD_BASE = 0x170; + + +unsigned int HD_CHL0_CTL_BASE = 0x3F6; +unsigned int HD_CHL1_CTL_BASE = 0x376; #define HD_DATA 0 #define HD_FEATURES 1 @@ -58,11 +60,6 @@ #define HD_CMD_SPECIFY 0x91 #define HD_CMD_IDENTIFY 0xEC -/* hard disk control register */ -#define HD_CHL0_CTL_BASE 0x3F6 -#define HD_CHL1_CTL_BASE 0x376 - - #define HD_CTL 0 #define HD_CTL_NORETRY 0x80 /* disable access retry */ #define HD_CTL_NOECC 0x40 /* disable ecc retry */ @@ -212,17 +209,22 @@ void ide_pci_init(pci_device_t *pci) printk(" ide pci Base IO Address Register %08x\n", v); iobase = v & 0xFFF0; +#if 0 pci_write_config_word(2, pci_cmd(pci, PCI_COMMAND)); v = inw(iobase+0); printk(" ide bus master ide command register primary %04x\n", v); v = inw(iobase+2); printk(" ide bus master ide status register primary %04x\n", v); +#endif int i; printk(" BARS: "); - for(i=0; i<5; ++i) + for(i=0; i<6; ++i) + { printk("%08x ", pci->bars[i]); + pci->bars[i] &= (~1UL); + } printk("\n"); #if 0 prd_t *p = (prd_t *) va2pa(hd_prd_tbl); @@ -239,6 +241,17 @@ void ide_pci_init(pci_device_t *pci) printk(" ide bus master ide status register primary %04x\n", v); #endif + + HD_CHL0_CMD_BASE = pci->bars[0] ? pci->bars[0] : HD_CHL0_CMD_BASE; + HD_CHL0_CTL_BASE = pci->bars[1] ? pci->bars[1] : HD_CHL0_CTL_BASE; + + HD_CHL1_CMD_BASE = pci->bars[2] ? pci->bars[2] : HD_CHL1_CMD_BASE; + HD_CHL1_CTL_BASE = pci->bars[3] ? pci->bars[3] : HD_CHL1_CTL_BASE; + + printk("channel0: cmd %04x ctl %04x channel1: cmd %04x ctl %04x\n", HD_CHL0_CMD_BASE, HD_CHL0_CTL_BASE, HD_CHL1_CMD_BASE, HD_CHL1_CTL_BASE); + + +#if 0 pci_write_config_byte(0xFE, pci_cmd(pci, PCI_INTRLINE)); v = pci_read_config_byte(pci_cmd(pci, PCI_INTRLINE)); printk("---- %x\n", v); @@ -252,20 +265,133 @@ void ide_pci_init(pci_device_t *pci) printk("This is a Parallel IDE Controller which use IRQ 14 and IRQ 15.\n"); } } +#endif +} + +void ide_hd_out(Dev dev, u32 nsect, u64 sect_nr, u32 cmd) +{ + + { + unsigned long long sect_nr = 0; + unsigned int nsect = 1; + outb_p(0x00, REG_CTL(dev)); + + outb(0, REG_NSECTOR(dev)); // High + outb((u8)nsect, REG_NSECTOR(dev)); // Low + + outb((u8)((sect_nr>>24)&0xFF), REG_LBAL(dev)); + outb((u8)((sect_nr>> 0)&0xFF), REG_LBAL(dev)); + + outb((u8)((sect_nr>>32)&0xFF), REG_LBAM(dev)); + outb((u8)((sect_nr>> 8)&0xFF), REG_LBAM(dev)); + + outb((u8)((sect_nr>>40)&0xFF), REG_LBAH(dev)); + outb((u8)((sect_nr>>16)&0xFF), REG_LBAH(dev)); + + outb(0xE0, REG_DEVSEL(dev)); + outb(0x24, REG_CMD(dev)); + } +} + + +void ide_debug() +{ + u32 device; + u32 nsect = 1; + u32 retires = 100; + u32 sect_nr = 1; + int count=SECT_SIZE; + + nsect = (count + SECT_SIZE -1)/SECT_SIZE; + + do + { + ide_hd_out(0, nsect, sect_nr, HD_CMD_READ_EXT); + + int drq_retires = 100000; + while(!hd_drq(dev) && --drq_retires) + /* do nothing */; + + if(drq_retires != 0) + break; + }while(--retires); + + if(retires == 0) + panic("hard disk is not ready"); + + char buf[1024]; + memset(buf, 0xDD, 512); + insw(REG_DATA(0), buf, count>>1); + unsigned short *p = (unsigned short *) (buf+510); + printk("ide_debug %04x\n", *p); + //hd_rd_data(dev, buf, count); +} + +void dbg() +{ + pci_device_t *pci; + pci = pci_find_device(PCI_VENDORID_INTEL, 0x2829); + if(pci != 0) + { + printk("0x2829 command %08x\n", pci->command); + } + + pci = pci_find_device(PCI_VENDORID_INTEL, 0x2850); + if(pci != 0) + { + printk("0x2850 command %08x\n", pci->command); + } + +} + +void dump_pci_controller(unsigned int vendor, unsigned int device) +{ + pci_device_t *pci = pci_find_device(vendor, device); + if(pci != 0) + { + printk("Found PCI Vendor %04x Device %04x Class %04x\n", vendor, device, pci->classcode); + ide_pci_init(pci); + } } void ide_init() { - pci_device_t *pci = pci_find_device(PCI_VENDORID_INTEL, 0x2850); + dump_pci_controller(PCI_VENDORID_INTEL, 0x7010); + dump_pci_controller(PCI_VENDORID_INTEL, 0x2922); + dump_pci_controller(PCI_VENDORID_INTEL, 0x2829); + dump_pci_controller(PCI_VENDORID_INTEL, 0x2850); + pci_device_t *pci = 0; +#if 0 + pci = pci_find_device(PCI_VENDORID_INTEL, 0x7010); // qemu if(pci == 0) - pci = pci_find_device(PCI_VENDORID_INTEL, 0x7010); // qemu - + { + printk("qemu ide controller\n"); + } +#endif +#if 0 + pci_device_t *pci = pci_find_device(PCI_VENDORID_INTEL, 0x2922); + if(pci != 0) + printk("laptop ide....\n"); +#endif +#if 0 + pci_device_t *pci = pci_find_device(PCI_VENDORID_INTEL, 0x2829); + if(pci != 0) + printk("laptop achi....\n"); +#endif +#if 0 + pci_device_t *pci = pci_find_device(PCI_VENDORID_INTEL, 0x2850); + if(pci != 0) + printk("laptop ide....\n"); +#endif + +#if 0 if(pci == 0) panic("can not find ide device"); printk("found ide pci device\n"); - ide_pci_init(pci); +#endif + return; outb_p(0x02, REG_CTL(0)); ide_read_identify(); diff --git a/drivers/keyboard.c b/drivers/keyboard.c index f98e1f3..37ef2e0 100644 --- a/drivers/keyboard.c +++ b/drivers/keyboard.c @@ -18,7 +18,6 @@ #include #include #include -#include #define KBD_BUF_SIZE 256 static struct @@ -36,44 +35,44 @@ static struct extern void reboot(); extern void poweroff(); -extern void hd_out(Dev dev, u32 nsect, u64 sect_nr, u32 cmd); +extern void ide_debug(); void kbd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id) { - unsigned char ScanCode; - //printk("%s\n", dev_id); - ScanCode = inb(0x60); + unsigned char scan_code; + scan_code = inb(0x60); - if(ScanCode == 0x01) // Esc + if(scan_code == 0x01) // Esc reboot(); - printk("%02x", ScanCode); + printk("%02x", scan_code); - hd_out(0, 1, 1, HD_CMD_READ_EXT); + if(scan_code == 0x13) + ide_debug(); if(count < KBD_BUF_SIZE) { count++; - buf[tail++] = ScanCode; + buf[tail++] = scan_code; tail %= KBD_BUF_SIZE; } } -inline int getScanCode() +inline int getscan_code() { - unsigned int ScanCode; + unsigned int scan_code; //while(count <= 0); if(count <= 0) return -1; - ScanCode = buf[head++]; + scan_code = buf[head++]; head %= KBD_BUF_SIZE; count--; //很明显这是临界资源但现在只能这样了 - return (0xFF & ScanCode); + return (0xFF & scan_code); } int sysc_read_kbd() { - return getScanCode(); + return getscan_code(); } diff --git a/fs/ext2.c b/fs/ext2.c index 862b7c7..bd2c7dc 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -18,7 +18,7 @@ unsigned int ext2_start_sect; Inode ext2_root_inode; -extern void hd_read(Dev dev, u64 sect_nr, void *buf, u32 count); +void hd_read(Dev dev, u64 sect_nr, void *buf, u32 count);// { /*TODO*/ } void ext2_read_block(int block_id, char *buf); static void ext2_print_group_descriptor(pGroupDesc p); static void ext2_print_inode(pInode p); diff --git a/include/pci.h b/include/pci.h index 4405aef..0247f3e 100644 --- a/include/pci.h +++ b/include/pci.h @@ -116,6 +116,17 @@ typedef union pci_device #define PCI_VENDORID 0x00 #define PCI_DEVICEID 0x02 #define PCI_COMMAND 0x04 + #define PCI_COMMAND_IO 0x01 + #define PCI_COMMAND_MEMORY 0x02 + #define PCI_COMMAND_MASTER 0x04 + #define PCI_COMMAND_SPECIAL 0x08 + #define PCI_COMMAND_INVALIDATE 0x10 + #define PCI_COMMAND_VGA_PALETTE 0x20 + #define PCI_COMMAND_PARITY 0x40 + #define PCI_COMMAND_WAIT 0x80 + #define PCI_COMMAND_SERR 0x100 + #define PCI_COMMAND_FAST_BACK 0x200 + #define PCI_COMMAND_INTR_DISABLE 0x400 #define PCI_STATUS 0x06 #define PCI_REVISION 0x08 #define PCI_PROGIF 0x09 diff --git a/include/processor.h b/include/processor.h index 3626e40..3661e67 100644 --- a/include/processor.h +++ b/include/processor.h @@ -161,6 +161,8 @@ static inline void set_idt_gate(u32 vec, u32 handler,u8 type, u8 DPL) idt[vec] = _create_gate(handler, type, DPL); } +#define set_sys_int(vect, type, DPL, handler) do { void handler(); set_idt_gate(vect, (u32)handler, type, DPL); } while(0) + typedef struct { u16 backlink, _backlink; diff --git a/setup/setup.c b/setup/setup.c index 0ca82d4..4fff3bd 100644 --- a/setup/setup.c +++ b/setup/setup.c @@ -17,7 +17,6 @@ #include #include #include -#include extern void setup_gdt(); extern void setup_idt(); @@ -56,36 +55,6 @@ const char *version = BUIDER; -extern void hd_out(Dev dev, u32 nsect, u64 sect_nr, u32 cmd); - -#include "hd.h" -void hd() -{ - unsigned long long sect_nr = 0; - unsigned int nsect = 1; - - cli(); - outb(0x00, REG_CTL(dev)); - - outb(0, REG_NSECTOR(dev)); // High - outb((u8)nsect, REG_NSECTOR(dev)); // Low - - outb((u8)((sect_nr>>24)&0xFF), REG_LBAL(dev)); - outb((u8)((sect_nr>> 0)&0xFF), REG_LBAL(dev)); - - outb((u8)((sect_nr>>32)&0xFF), REG_LBAM(dev)); - outb((u8)((sect_nr>> 8)&0xFF), REG_LBAM(dev)); - - outb((u8)((sect_nr>>40)&0xFF), REG_LBAH(dev)); - outb((u8)((sect_nr>>16)&0xFF), REG_LBAH(dev)); - - outb(0xE0, REG_DEVSEL(dev)); - outb(0x24, REG_CMD(dev)); - sti(); - -} - - void setup_kernel() { extern char kernel_begin, kernel_end; @@ -110,16 +79,14 @@ void setup_kernel() setup_tasks(); setup_irqs(); - - //asm("sti;"); - //hd(); - + + void ide_init(); ide_init(); + ahci_init(); printk("%s\n", version); - //asm("cli;"); - //while(1); + + return; - hd_out(0, 1, 1, HD_CMD_READ_EXT); while(1); // TODO MODIFY CODE BELOW diff --git a/setup/system.c b/setup/system.c index fae0265..dd868de 100644 --- a/setup/system.c +++ b/setup/system.c @@ -44,75 +44,56 @@ void setup_gdt() void setup_idt() { - //init idt. *((unsigned short *)idtr) = NIDT*sizeof(Gate); *((unsigned long *)(idtr+2)) = (unsigned long)idt; lidt(); } + + void setup_gate() { int i; -#define set_sys_int(vect, type, DPL, handler) \ -do{ \ - extern void handler (); \ - set_idt_gate(vect, (u32)handler, type, DPL); \ -}while(0) - -#define PL_KRNL PRIVILEGE_KRNL -#define PL_USER PRIVILEGE_USER -#if 1 - set_sys_int(0x00, TRAP_GATE, PL_KRNL, DivideError); - set_sys_int(0x01, TRAP_GATE, PL_KRNL, Debug); - set_sys_int(0x02, INTR_GATE, PL_KRNL, NMI); - set_sys_int(0x03, TRAP_GATE, PL_USER, BreakPoint); - set_sys_int(0x04, TRAP_GATE, PL_USER, OverFlow); - set_sys_int(0x05, TRAP_GATE, PL_USER, BoundsCheck); - set_sys_int(0x06, TRAP_GATE, PL_KRNL, InvalidOpcode); - set_sys_int(0x07, TRAP_GATE, PL_KRNL, DeviceNotAvailable); - set_sys_int(0x08, TRAP_GATE, PL_KRNL, DoubleFault); - set_sys_int(0x09, TRAP_GATE, PL_KRNL, CoprocSegOverRun); - set_sys_int(0x0A, TRAP_GATE, PL_KRNL, InvalidTss); - set_sys_int(0x0B, TRAP_GATE, PL_KRNL, SegNotPresent); - set_sys_int(0x0C, TRAP_GATE, PL_KRNL, StackFault); - set_sys_int(0x0D, TRAP_GATE, PL_KRNL, GeneralProtection); - set_sys_int(0x0E, TRAP_GATE, PL_KRNL, PageFault); - set_sys_int(0x10, TRAP_GATE, PL_KRNL, CoprocError); + set_sys_int(0x00, TRAP_GATE, PRIVILEGE_KRNL, DivideError); + set_sys_int(0x01, TRAP_GATE, PRIVILEGE_KRNL, Debug); + set_sys_int(0x02, INTR_GATE, PRIVILEGE_KRNL, NMI); + set_sys_int(0x03, TRAP_GATE, PRIVILEGE_USER, BreakPoint); + set_sys_int(0x04, TRAP_GATE, PRIVILEGE_USER, OverFlow); + set_sys_int(0x05, TRAP_GATE, PRIVILEGE_USER, BoundsCheck); + set_sys_int(0x06, TRAP_GATE, PRIVILEGE_KRNL, InvalidOpcode); + set_sys_int(0x07, TRAP_GATE, PRIVILEGE_KRNL, DeviceNotAvailable); + set_sys_int(0x08, TRAP_GATE, PRIVILEGE_KRNL, DoubleFault); + set_sys_int(0x09, TRAP_GATE, PRIVILEGE_KRNL, CoprocSegOverRun); + set_sys_int(0x0A, TRAP_GATE, PRIVILEGE_KRNL, InvalidTss); + set_sys_int(0x0B, TRAP_GATE, PRIVILEGE_KRNL, SegNotPresent); + set_sys_int(0x0C, TRAP_GATE, PRIVILEGE_KRNL, StackFault); + set_sys_int(0x0D, TRAP_GATE, PRIVILEGE_KRNL, GeneralProtection); + set_sys_int(0x0E, TRAP_GATE, PRIVILEGE_KRNL, PageFault); + set_sys_int(0x10, TRAP_GATE, PRIVILEGE_KRNL, CoprocError); for(i=0x20; i<256; i++) - set_sys_int(i, INTR_GATE, PL_KRNL, no_irq_handler); - -#if 1 - set_sys_int(0x20, INTR_GATE, PL_KRNL, irq_0x00_handler); - set_sys_int(0x21, INTR_GATE, PL_KRNL, irq_0x01_handler); - set_sys_int(0x22, INTR_GATE, PL_KRNL, irq_0x02_handler); - set_sys_int(0x23, INTR_GATE, PL_KRNL, irq_0x03_handler); - set_sys_int(0x24, INTR_GATE, PL_KRNL, irq_0x04_handler); - set_sys_int(0x25, INTR_GATE, PL_KRNL, irq_0x05_handler); - set_sys_int(0x26, INTR_GATE, PL_KRNL, irq_0x06_handler); - set_sys_int(0x27, INTR_GATE, PL_KRNL, irq_0x07_handler); - set_sys_int(0x28, INTR_GATE, PL_KRNL, irq_0x08_handler); - set_sys_int(0x29, INTR_GATE, PL_KRNL, irq_0x09_handler); - set_sys_int(0x2A, INTR_GATE, PL_KRNL, irq_0x0A_handler); - set_sys_int(0x2B, INTR_GATE, PL_KRNL, irq_0x0B_handler); - set_sys_int(0x2C, INTR_GATE, PL_KRNL, irq_0x0C_handler); - set_sys_int(0x2D, INTR_GATE, PL_KRNL, irq_0x0D_handler); - set_sys_int(0x2E, INTR_GATE, PL_KRNL, irq_0x0E_handler); - set_sys_int(0x2F, INTR_GATE, PL_KRNL, irq_0x0F_handler); -#endif -#endif + set_sys_int(i, INTR_GATE, PRIVILEGE_KRNL, no_irq_handler); + + set_sys_int(0x20, INTR_GATE, PRIVILEGE_KRNL, irq_0x00_handler); + set_sys_int(0x21, INTR_GATE, PRIVILEGE_KRNL, irq_0x01_handler); + set_sys_int(0x22, INTR_GATE, PRIVILEGE_KRNL, irq_0x02_handler); + set_sys_int(0x23, INTR_GATE, PRIVILEGE_KRNL, irq_0x03_handler); + set_sys_int(0x24, INTR_GATE, PRIVILEGE_KRNL, irq_0x04_handler); + set_sys_int(0x25, INTR_GATE, PRIVILEGE_KRNL, irq_0x05_handler); + set_sys_int(0x26, INTR_GATE, PRIVILEGE_KRNL, irq_0x06_handler); + set_sys_int(0x27, INTR_GATE, PRIVILEGE_KRNL, irq_0x07_handler); + set_sys_int(0x28, INTR_GATE, PRIVILEGE_KRNL, irq_0x08_handler); + set_sys_int(0x29, INTR_GATE, PRIVILEGE_KRNL, irq_0x09_handler); + set_sys_int(0x2A, INTR_GATE, PRIVILEGE_KRNL, irq_0x0A_handler); + set_sys_int(0x2B, INTR_GATE, PRIVILEGE_KRNL, irq_0x0B_handler); + set_sys_int(0x2C, INTR_GATE, PRIVILEGE_KRNL, irq_0x0C_handler); + set_sys_int(0x2D, INTR_GATE, PRIVILEGE_KRNL, irq_0x0D_handler); + set_sys_int(0x2E, INTR_GATE, PRIVILEGE_KRNL, irq_0x0E_handler); + set_sys_int(0x2F, INTR_GATE, PRIVILEGE_KRNL, irq_0x0F_handler); } -#include -void dump(); -void sata_handler(unsigned int irq, pt_regs_t * regs, void *dev_id) +void default_irq_handler(unsigned int irq, pt_regs_t * regs, void *dev_id) { - printk("sata irq handler %d \n", irq); - inb(REG_STATUS(0)); - char buf[1024]; - memset(buf, 0, 1024); - hd_rd_data(0, buf, 512); - printk("SECTOR %04x\n", *(unsigned short *)(buf+510)); - dump(); + printk("default irq handler %d \n", irq); } void setup_irqs() @@ -129,58 +110,19 @@ void setup_irqs() irq_desc[i].chip = &i8259_chip; } - //extern void kbd_handler(pt_regs_t *, unsigned int); //extern void clk_handler(pt_regs_t *, unsigned int); - //extern void hd_handler(pt_regs_t *, unsigned int); - extern void hd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id); void kbd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id); void clk_handler(unsigned int irq, pt_regs_t * regs, void *dev_id); - void hd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id); + request_irq(0x00, clk_handler, "Intel 8254", "Clock Chip"); request_irq(0x01, kbd_handler, "Intel 8042", "PS/2 Keyboard"); - //request_irq(0x0E, hd_handler, "IDE", "IDE"); - for(i=3; i<256; i++) + for(i=3; i<16; i++) { - //request_irq(i, hd_handler, "IDE", "IDE"); - request_irq(i, sata_handler, "Intel 8086", "SATA"); + request_irq(i, default_irq_handler, "default", "default"); } - //request_irq(11, sata_handler, "Intel 8086", "SATA"); - //request_irq(0x11, sata_handler, "Intel 8086", "SATA"); - - //enable_irq(0x00); - enable_irq(0x01); - for(i=2; i<16; i++) + for(i=1; i<16; i++) enable_irq(i); asm("sti"); - //asm("cli"); - -/* - pIRQAction pKbdAction, pClkAction; - - - pKbdAction = (pIRQAction) kmalloc_old(sizeof(IRQAction)); - pClkAction = (pIRQAction) kmalloc_old(sizeof(IRQAction)); - - - assert(pKbdAction != NULL); - assert(pClkAction != NULL); - - pClkAction->handler = clk_handler; - pClkAction->next = NULL; - pKbdAction->handler = kbd_handler; - pKbdAction->next = NULL; - - printk("**********%08x %08x", pKbdAction, pClkAction); - - irq_desc[0x0].chip = &i8259_chip; - irq_desc[0x0].chip->enable(0x0); - irq_desc[0x0].action = pClkAction; - - irq_desc[0x1].chip = &i8259_chip; - irq_desc[0x1].chip->enable(0x1); - irq_desc[0x1].action = pKbdAction; -*/ - } void set_tss() @@ -198,7 +140,6 @@ void set_tss() p->eflags = 0x1200; p->iomap_base = sizeof(TSS); set_tss_gate(INDEX_TSS, (u32)p); - //printk("TSS:%08x\n", p); asm("ltr %%ax"::"a"((INDEX_TSS<<3)+3)); } @@ -206,20 +147,5 @@ void setup_root_dev() { unsigned char dev; dev = (unsigned char)(system.boot_device >> 24); - //if(dev != 0x80) - // panic("OS must boot from the first hard disk"); - printk("root device: %08x\n", system.root_dev); -#if 0 - - /* - * 硬盘的次设备号 - * 0: 整个硬盘 - * 1-4: 主分区 - * 5~N: 逻辑分区(N<2^16-5) - * 目录只支持从第一个硬盘引导. - */ - int minor = ((system.boot_device>>16) & 0xFF) + 1; - system.root_dev = MAKE_DEV(DEV_MAJOR_HD, minor); -#endif }