]> Zhao Yanbai Git Server - kernel.git/commitdiff
fix the inl bug; fix pci_read_config bug
authorAceVest <zhaoyanbai@126.com>
Sat, 10 May 2014 14:43:39 +0000 (22:43 +0800)
committerAceVest <zhaoyanbai@126.com>
Sat, 10 May 2014 14:43:39 +0000 (22:43 +0800)
.bochsrc
drivers/hd.c
drivers/hd.h
drivers/keyboard.c
include/io.h
include/pci.h
pci/setuppci.c
setup/logo.c [deleted file]
setup/setup.c
setup/system.c
setup/version.c [deleted file]

index 61c5227c417781f060e5165cada8e16a0e71cf1a..e751457d448d15d8969e13d732e76c40e1be61de 100644 (file)
--- a/.bochsrc
+++ b/.bochsrc
@@ -25,6 +25,9 @@ com4: enabled=0
 cpuid: vendor_string="GenuineIntel"
 cpuid: brand_string="              Intel(R) Pentium(R) 4 CPU        "
 
+pci: enabled=1, chipset=i440fx, slot1=pcivga
+#pcidev: vendor=0x1234, device=0x5678
+
 #usb_uhci: enabled=0
 #usb_ohci: enabled=0
 print_timestamps: enabled=0
index 04c47dd7c9225bb3d5be3fbc36b7cbd6274b71a1..092726e3dc2a8635ecaec5b735ff4d453421c49e 100644 (file)
@@ -14,9 +14,9 @@
 #include <irq.h>
 #include <system.h>
 //void    hd_handler(pt_regs_t * regs, unsigned int irq)
-void    hd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id)
+void hd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id)
 {
-    printk("hd_handler:%d ", irq);
+    printk("\nhd_handler:%d\n", irq);
 }
 
 int    hd_controller_ready(Dev dev)
@@ -34,21 +34,15 @@ int    hd_controller_ready(Dev dev)
 
 void    hd_controller_reset(unsigned int dev)
 {
-    outb(HD_CTL_RESET,    REG_CTL(dev));
-/*
-    //outb(HD_CTL_RESET,    REG_CTL(dev));
-    while(1)
-    {
-        printk("%02x ", inb(REG_STATUS(dev)));
-    }
+    outb_p(HD_CTL_RESET,    REG_CTL(dev));
+    outb_p(0,    REG_CTL(dev));
     while(hd_bsy(dev))
     {
         printk("bsy");
     }
-*/
 }
 
-void    hd_out(Dev dev, u32 nsect, u64 sect_nr, u32 cmd)
+void hd_out(Dev dev, u32 nsect, u64 sect_nr, u32 cmd)
 {
     assert(nsect > 0);
     assert(HD_GET_DEV(dev) < 2);    // 0: ata-master 1:ata-slave
@@ -66,9 +60,10 @@ void    hd_out(Dev dev, u32 nsect, u64 sect_nr, u32 cmd)
         hd_controller_reset(dev);
     }
 
+    printk("hdout\n");
     outb(0x00,                REG_CTL(dev));
     outb(0x00,                REG_FEATURES(dev));
-    outb((u8)nsect,                REG_NSECTOR(dev));
+    outb((u8)nsect,           REG_NSECTOR(dev));
 #ifdef USE_LBA_48
     /* 
      * LBA-48 bit
@@ -86,8 +81,8 @@ void    hd_out(Dev dev, u32 nsect, u64 sect_nr, u32 cmd)
     outb((u8)((sect_nr>>0x08)&0xFF),    REG_LBAM(dev));
     outb((u8)((sect_nr>>0x10)&0xFF),    REG_LBAH(dev));
 #endif    
-    outb((u8)device,            REG_DEVICE(dev));
-    outb((u8)cmd,                REG_CMD(dev));
+    outb((u8)device,    REG_DEVICE(dev));
+    outb((u8)cmd,       REG_CMD(dev));
 }
 
 void    _hd_read(Dev dev, u64 sect_nr, void *buf, u32 count, u32 cmd)
@@ -137,8 +132,8 @@ void    hd_print_identify(const char *buf)
 
     char    hd_sn[32];    /* 20 bytes */
     char    hd_model[64];    /* 40 bytes */
-    short    hd_capabilites = ident[49];
-    short     hd_supt_inst_set = ident[83];
+    short   hd_capabilites = ident[49];
+    short   hd_supt_inst_set = ident[83];
 
     p = (char *) (ident+10);
     for(i=0; i<20; i++)
@@ -166,242 +161,17 @@ void    hd_print_identify(const char *buf)
         panic("Your hard disk ");
 }
 
-void    setup_hd()
+void setup_hd()
 {
-    if(!system.debug)
-        hd_controller_reset(ROOT_DEV);
-    //hd_controller_reset(1);
-    //return ;
+    hd_controller_reset(ROOT_DEV);
 
     char *buf;
-    buf = (unsigned char *) get_virt_pages(1);
+    buf = (unsigned char *) alloc_one_page(0);
     assert(buf != NULL);
 
-#if 1
     hd_read_identify(ROOT_DEV, buf);
-#else
-    outb(0x00, 0x1F1);
-    outb(0x01, 0x1F2);
-    outb(0x00, 0x1F3);
-    outb(0x00, 0x1F4);
-    outb(0x00, 0x1F5);
-    outb(0xE0, 0x1F6);
-    outb(0xEC, 0x1F7);
-    while(!(inb(0x1F7) & 0x08)){}
-    asm("cld;rep;insw;"::"c"(256), "d"(0x1F0), "D"(buf));
-#endif
-    hd_print_identify(buf);
-
-    free_virt_pages(buf);
-}
-
-
-
-
-#if 0
-int    hd_controller_ready(unsigned int channel)
-{
-    int retries = 0x1000;
-
-    do
-    {
-        if(hd_rdy(channel))
-            return 1;
-    }while(--retries);
-
-    return 0;
-}
-
-void    hd_controller_reset(unsigned int channel)
-{
-    outb(HD_CTL_RESET,    REG_CTL(channel));
-}
-
-void    hd_out(u32 channel, u32 device, u32 nsect, u64 sect_nr, u32 cmd)
-{
-    assert(device < 2);    // 0: ata-master 1: ata-slave
-    assert(channel < 2);    // PC only support 2 IDE channels
-    assert(nsect > 0);
-
-    unsigned char dev = (device)?0x10 : 0x00;
-    dev |= 0xE0;
-
-    if(!hd_controller_ready(channel))
-        return ;
-
-    outb(0x00,                REG_CTL(channel));
-    outb(0x00,                REG_FEATURES(channel));
-    outb((u8)nsect,                REG_NSECTOR(channel));
-#ifdef USE_LBA_48
-    /* 
-     * LBA-48 bit
-     * 先写高位.再写低位.
-     */
-    outb((u8)((sect_nr>>0x18)&0xFF),    REG_LBAL(channel));
-    outb((u8)((sect_nr>>0x20)&0xFF),    REG_LBAM(channel));
-    outb((u8)((sect_nr>>0x28)&0xFF),    REG_LBAH(channel));
-
-    outb((u8)((sect_nr>>0x00)&0xFF),    REG_LBAL(channel));
-    outb((u8)((sect_nr>>0x08)&0xFF),    REG_LBAM(channel));
-    outb((u8)((sect_nr>>0x10)&0xFF),    REG_LBAH(channel));
-#else
-    outb((u8)((sect_nr>>0x00)&0xFF),    REG_LBAL(channel));
-    outb((u8)((sect_nr>>0x08)&0xFF),    REG_LBAM(channel));
-    outb((u8)((sect_nr>>0x10)&0xFF),    REG_LBAH(channel));
-#endif    
-    outb((u8)dev,                REG_DEVICE(channel));
-    outb((u8)cmd,                REG_CMD(channel));
-}
-
-void    _hd_read(u32 dev, u64 sect_nr, void *buf, u32 count, u32 cmd)
-{
-    u32    channel, device;
-    u32    nsect;
-    u32    retires = 100;
-
-    channel = device = 0;    //
-
-    nsect    = (count + SECT_SIZE -1)/SECT_SIZE;
-
-    do
-    {
-        hd_out(channel, device,    nsect,  sect_nr, cmd);
-
-        int drq_retires = 100000;
-        while(!hd_drq(channel) && --drq_retires)
-            /* do nothing */;
-
-        if(drq_retires != 0)
-            break;
-    }while(--retires);
-
-    if(retires == 0)
-        panic("hard disk is not ready");
-
-    hd_rd_data(channel, buf, count);
-}
-
 
-void    hd_read(u32 dev, u64 sect_nr, void *buf, u32 count)
-{
-    _hd_read(dev, sect_nr, buf, count, HD_CMD_READ_EXT);
-}
-
-void    hd_read_identify(u32 dev, void *buf)
-{
-    _hd_read(dev, 0, buf, SECT_SIZE, HD_CMD_IDENTIFY);
-}
-
-void    hd_print_identify(const char *buf)
-{
-    char    *p;
-    short    *ident;
-    int    i;
-
-    ident = (short *) buf;
-
-    char    hd_sn[32];    /* 20 bytes */
-    char    hd_model[64];    /* 40 bytes */
-    short    hd_capabilites = ident[49];
-    short     hd_supt_inst_set = ident[83];
-
-    p = (char *) (ident+10);
-    for(i=0; i<20; i++)
-    {
-        hd_sn[i] = p[i];
-    }
-    hd_sn[i] = 0;
-
-    p = (char *) (ident+27);
-    for(i=0; i<40; i++)
-    {
-        hd_model[i] = p[i];
-    }
-    hd_model[i] = 0;
-
-
-    printk("Hard Disk SN: %s\n", hd_sn);
-    printk("Hard Disk Model: %s\n", hd_model);
-    printk("Hard Disk Support LBA: %s\n",
-        (hd_capabilites & 0x0200) ? "Yes" : "No");
-    printk("Hard Disk Support LBA-48bit: %s\n",
-        (hd_supt_inst_set & 0x0400) ? "Yes" : "No");
-
-    if(!(hd_supt_inst_set & 0x0400))
-        panic("Your hard disk ");
-}
-
-void    setup_hd()
-{
-    //hd_controller_reset(0);
-    //hd_controller_reset(1);
-    //return ;
-
-    char *buf;
-    buf = (unsigned char *) get_virt_pages(1);
-    assert(buf != NULL);
-
-#if 1
-    hd_read_identify(ROOT_DEV, buf);
-#else
-    outb(0x00, 0x1F1);
-    outb(0x01, 0x1F2);
-    outb(0x00, 0x1F3);
-    outb(0x00, 0x1F4);
-    outb(0x00, 0x1F5);
-    outb(0xE0, 0x1F6);
-    outb(0xEC, 0x1F7);
-    while(!(inb(0x1F7) & 0x08)){}
-    asm("cld;rep;insw;"::"c"(256), "d"(0x1F0), "D"(buf));
-#endif
     hd_print_identify(buf);
 
-    free_virt_pages(buf);
-}
-#endif
-
-
-#if 0
-void    hd_rd_sect(u64 sn, char *buf)
-{
-    int chl = 0;
-    unsigned char dev = 0xE0;
-
-    outb(HD_CTL_RESET,    REG_CTL(chl));
-    outb(HD_CTL_DISABLE_INT,REG_CTL(chl));
-    outb(0x00,         REG_FEATURES(chl));
-    outb(0x01,        REG_NSECTOR(chl));
-#if 0
-    outb((sn>>0x00)&0xFF,    REG_LBAL(chl));
-    outb((sn>>0x08)&0xFF,    REG_LBAM(chl));
-    outb((sn>>0x10)&0xFF,    REG_LBAH(chl));
-#endif
-#if 0
-    outb(0x00,    REG_LBAL(chl));
-    outb(0x00,    REG_LBAL(chl));
-    outb(0x00,    REG_LBAM(chl));
-    outb(0x00,    REG_LBAM(chl));
-    outb(0x00,    REG_LBAH(chl));
-    outb(0x00,    REG_LBAH(chl));
-#endif
-#if 1
-    /* 
-     * LBA-48 bit
-     * 先写高位.再写低位.
-     */
-    outb(0x00,            REG_LBAL(chl));
-    outb(0x00,            REG_LBAM(chl));
-    outb(0x00,            REG_LBAH(chl));
-
-    outb(0x00,            REG_LBAL(chl)); outb(0x00,            REG_LBAM(chl));
-    outb(0x00,            REG_LBAH(chl));
-#endif
-
-    outb(dev,            REG_DEVICE(chl));
-    outb(HD_CMD_READ_EXT,        REG_CMD(chl));
-
-    while(!hd_drq(chl));
-
-    hd_rd_data(chl, buf, SECT_SIZE);
+    free_pages((unsigned long)buf);
 }
-#endif
index e41cb84e1fbe212f03d9a2051673996720e36596..5ea9f0d541f080d06708a60931010c548b6c76aa 100644 (file)
 #define HD_LBAH            5
 #define HD_DEVICE        6
 #define HD_STATUS        7        /* controller status */
-#define     HD_STATUS_BSY        0x80    /* controller busy */
-#define     HD_STATUS_RDY        0x40    /* drive ready */
+#define     HD_STATUS_BSY       0x80    /* controller busy */
+#define     HD_STATUS_RDY       0x40    /* drive ready */
 #define     HD_STATUS_WF        0x20    /* write fault */
-#define     HD_STATUS_SC        0x10    /* seek complete */
-#define     HD_STATUS_DRQ        0x08    /* data transfer request */
-#define     HD_STATUS_CRD        0x04    /* correct data */
-#define     HD_STATUS_IDX        0x02    /* index pulse */
-#define     HD_STATUS_ERR        0x01    /* error */
+#define     HD_STATUS_SEEK_CMPT 0x10    /* seek complete */
+#define     HD_STATUS_DRQ       0x08    /* data transfer request */
+#define     HD_STATUS_CRD       0x04    /* correct data */
+#define     HD_STATUS_IDX       0x02    /* index pulse */
+#define     HD_STATUS_ERR       0x01    /* error */
 #define HD_FEATURES        HD_ERR
 #define HD_CMD            HD_STATUS
 #define     HD_CMD_IDLE        0x00
 #define HD_CTL            0
 #define     HD_CTL_NORETRY        0x80    /* disable access retry */
 #define     HD_CTL_NOECC        0x40    /* disable ecc retry */
-#define     HD_CTL_EIGHTHEADS    0x08    /* more than 8 heads */
+#define     HD_CTL_EIGHTHEADS   0x08    /* more than 8 heads */
 #define     HD_CTL_RESET        0x04    /* reset controller */
 #define     HD_CTL_DISABLE_INT    0x02    /* disable interrupts */
 
 #define     HD_GET_CHL(dev)        (0)    /* 暂时还是只支持通道0 */
 #define     HD_GET_DEV(dev)        (0)    /* 暂时还是只支持一个硬盘 */
 
-#define REG_CMD_BASE(dev, offset)            \
-    (HD_GET_CHL(dev)?(HD_CHL1_CMD_BASE+offset)    \
-    :                        \
-    (HD_CHL0_CMD_BASE+offset))
+#define REG_CMD_BASE(dev, offset)  ( HD_GET_CHL(dev) ? (HD_CHL1_CMD_BASE+offset) : (HD_CHL0_CMD_BASE+offset) )
 
-#define REG_CTL_BASE(dev, offset)            \
-    (HD_GET_CHL(dev)?(HD_CHL1_CTL_BASE+offset)    \
-    :                        \
-    (HD_CHL0_CTL_BASE+offset))
+#define REG_CTL_BASE(dev, offset)  ( HD_GET_CHL(dev) ? (HD_CHL1_CTL_BASE+offset) : (HD_CHL0_CTL_BASE+offset) )
 
 #if 1
 #define REG_DATA(dev)        REG_CMD_BASE(dev, HD_DATA)
index 6dd88507e10891a78130853537d91082da651e42..6da523dcf051deed0f04df3592955501cdc94b02 100644 (file)
@@ -35,12 +35,16 @@ static struct
 
 extern void reboot();
 extern void poweroff();
+extern void hd_out(Dev dev, u32 nsect, u64 sect_nr, u32 cmd);
 void    kbd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id)
 {
     unsigned char ScanCode;
     //printk("%s\n", dev_id);
     ScanCode = inb(0x60);
-    //printk("%02x", ScanCode);
+    
+    printk("%02x", ScanCode);
+    hd_out(0, 1, 1, 0x24);
+    return; // debug
     if(count < KBD_BUF_SIZE)
     {
         count++;
index 351c46e7b6d7c65dac83144e321c50f6d78193ad..b2da013b91e56a2f75aac0340711d542bef487bb 100644 (file)
@@ -68,7 +68,7 @@ _bt;                            \
 })
 
 #define inl(port)({             \
-u16 _bt;                        \
+u32 _bt;                        \
 asm("inl    %%dx,%%eax"         \
 :"=a" (_bt)                     \
 :"d" (port));                   \
index 2a30dd1ad2fbfc6926897e7f33cf447484fa48fd..7910edce96437f42d8337f514ced4aacaefd98a4 100644 (file)
@@ -126,8 +126,7 @@ typedef union pci_device
 #define PCI_MAXLAT          0x3F
 
 // PCI Command Register
-#define PCI_CMD(bus, dev, devfn, reg) \
-(0x80000000 | (dev << 11) | (devfn << 8) | (reg & 0xFC))
+#define PCI_CMD(bus, dev, devfn, reg) (0x80000000 | (bus << 16) | (dev << 11) | (devfn << 8) | (reg/* & 0xFC */))
 
 #define PCI_CONFIG_CMD(cmd) (cmd & ~3)
 #define PCI_GET_CMD_REG(cmd) (cmd & 0xFF)
index 1248f00fa331092e3dbb1f07cbb6bf7e61f21e1a..fdeaa355441afb79cc4ec6d1a7945bfc348b561e 100644 (file)
@@ -53,9 +53,22 @@ void scan_pci_bus(int bus)
         {
             cmd = PCI_CMD(bus, dev, devfn, PCI_VENDORID);
             v = pci_read_config_word(cmd);
+            //v = pci_read_config_long(cmd);
             if(v == 0xFFFF)
                 continue;
 
+#if 0
+            printk("dev %d ", dev);
+            unsigned int i;
+            for(i=0; i<16; ++i)
+            {
+                cmd = PCI_CMD(bus, dev, devfn, i*4);
+                printk("%08x ", pci_read_config_long(cmd));
+            }
+
+            printk("\n");
+#endif
+
             pci_device_t *pci = kmalloc(sizeof(pci_device_t), 0);
             if(0 == pci)
             {
@@ -67,7 +80,7 @@ void scan_pci_bus(int bus)
 
             cmd = PCI_CMD(bus, dev, devfn, PCI_DEVICEID);
             pci->device = pci_read_config_word(cmd);
-        
+
             cmd = PCI_CMD(bus, dev, devfn, PCI_REVISION);
             pci->revision = pci_read_config_byte(cmd);
 
@@ -119,6 +132,7 @@ void dump_pci_dev()
             break;
         }
     }
+    while(1);
 }
 
 int probe_pci_bus()
@@ -154,6 +168,8 @@ int probe_pci_bus()
         }
     }
 
+
+    return 1;
 err:
     printk("Can not find PCI bus on your computer\n");
 
diff --git a/setup/logo.c b/setup/logo.c
deleted file mode 100644 (file)
index 5a70a32..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-#include <string.h>
-extern const char *version;
-extern void printString(const char *s, unsigned char color);
-void show_logo()
-{
-#if 1
-int i;
-#define VER_SIZE    81
-char ver[VER_SIZE];
-memset(ver, ' ', VER_SIZE);
-ver[VER_SIZE-1] = 0;
-//printString(ver, 0x70);
-#if 0
-printString(
-"                     ** **       *******   **    ** **                          "
-"                    *** **       ********  **    ** ***                         "
-"                   **** **       **     ** **    ** ****                        "
-"                  ** ** **       *******   ******** ** **                       "
-"                 **  ** **       ******    ******** **  **                      "
-"                ******* **       **        **    ** *******                     "
-"               **    ** ******** **        **    ** **    **                    "
-"              **     ** ******** **        **    ** **     **                   "
-, 0x9F);
-#endif
-
-memset(ver, ' ', VER_SIZE);
-strcpy(ver, version);
-for(i=0; i<VER_SIZE; i++)
-    if(ver[i] == '\0')
-    {
-        ver[i] = ' ';
-        break;
-    }
-ver[VER_SIZE-1] = 0;
-printString(ver, 0x70);
-#endif
-#if 0
-for(i=1; i<80*1*2; i+=2)
-    *((unsigned char *)(0xB8000+i)) = 0x00;
-for(i=1+80*1*2; i<80*9*2; i+=2)
-    *((unsigned char *)(0xB8000+i)) = 0x1F;
-for(i=1+80*9*2; i<80*10*2; i+=2)
-    *((unsigned char *)(0xB8000+i)) = 0x7F;
-#endif
-}
index ee845f57287c881a1b490925e42dfbf2cedb8754..b7d224f5c3a4c636c1911f5e33c225f08586fb6b 100644 (file)
@@ -45,6 +45,16 @@ void setup_i8253()
     outb(LATCH >> 8, 0x40);
 }
 
+#define VERSION    "0.2.5 Final"
+#define BUIDER    "Zhao Yanbai"
+const char *version = 
+    "Kernel "
+    VERSION
+    " Build on "__DATE__ " " __TIME__
+    " by "
+    BUIDER;
+
+
 void setup_kernel()
 {
     extern char kernel_begin, kernel_end;
@@ -57,7 +67,7 @@ void setup_kernel()
     setup_idt();
     setup_gate();
 
-    setup_i8253();
+    //setup_i8253();
 
     detect_cpu();
 
@@ -70,12 +80,15 @@ void setup_kernel()
 
     setup_irqs();
 
+    setup_hd();
+
+    printk("%s\n", version);
+
     return;
     while(1); // TODO MODIFY CODE BELOW
 
 
     setup_root_dev();
-    setup_hd();
     setup_fs();
     setup_ext2();
 
index 072bb6a6ee789e7aefaa43539471ecb006e814c7..69fa0d10fd9f6e466ba9f9f2d82e2dfbad385bde 100644 (file)
@@ -52,7 +52,7 @@ void    setup_idt()
 void no_irq_handler()
 {
     printk("no_irq_handler");
-    while(1);
+    //while(1);
 }
 
 void    setup_gate()
@@ -129,9 +129,22 @@ void    setup_irqs()
     request_irq(0x0E, hd_handler,     "IDE",           "IDE");
     enable_irq(0x00);
     enable_irq(0x01);
+    enable_irq(0x02);
+    enable_irq(0x03);
+    enable_irq(0x04);
+    enable_irq(0x05);
+    enable_irq(0x06);
+    enable_irq(0x07);
+    enable_irq(0x08);
+    enable_irq(0x09);
+    enable_irq(0x0A);
+    enable_irq(0x0B);
+    enable_irq(0x0C);
+    enable_irq(0x0D);
+    enable_irq(0x0F);
     enable_irq(0x0E);
-    //asm("sti");
-    asm("cli");
+    asm("sti");
+    //asm("cli");
 
 /*
     pIRQAction    pKbdAction, pClkAction;
diff --git a/setup/version.c b/setup/version.c
deleted file mode 100644 (file)
index 24173c1..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- *--------------------------------------------------------------------------
- *   File Name: version.c
- * 
- * Description: none
- * 
- * 
- *      Author: Zhao Yanbai [zhaoyanbai@126.com]
- * 
- *     Version:    1.0
- * Create Date: Sat Mar  7 10:47:41 2009
- * Last Update: Sat Mar  7 10:47:41 2009
- * 
- *--------------------------------------------------------------------------
- */
-#define VERSION    "0.2.5 Final"
-#define BUIDER    "Zhao Yanbai"
-const char *version = 
-    "Kernel "
-    VERSION
-    " Build on "__DATE__ " " __TIME__
-    " by "
-    BUIDER;