Subversion Repositories Kolibri OS

Rev

Rev 1554 | Blame | Last modification | View Log | Download | RSS feed

  1. ; PCI Bus defines
  2.         PCI_HEADER_TYPE                 equ     0x0e  ;8 bit
  3.         PCI_BASE_ADDRESS_0              equ     0x10  ;32 bit
  4.         PCI_BASE_ADDRESS_5              equ     0x24  ;32 bits
  5.         PCI_BASE_ADDRESS_SPACE_IO       equ     0x01
  6.         PCI_VENDOR_ID                   equ     0x00  ;16 bit
  7.         PCI_BASE_ADDRESS_IO_MASK        equ     0xFFFFFFFC
  8.  
  9.  
  10. ; PCI programming
  11.         PCI_REG_COMMAND         equ 0x4 ; command register
  12.         PCI_REG_STATUS          equ 0x6 ; status register
  13.         PCI_REG_LATENCY         equ 0xd ; latency timer register
  14.         PCI_REG_CAP_PTR         equ 0x34 ; capabilities pointer
  15.         PCI_REG_CAPABILITY_ID   equ 0x0 ; capapility ID in pm register block
  16.         PCI_REG_PM_STATUS       equ 0x4 ; power management status register
  17.         PCI_REG_PM_CTRL         equ 0x4 ; power management control register
  18.         PCI_BIT_PIO             equ 1 ; bit0: io space control
  19.         PCI_BIT_MMIO            equ 2 ; bit1: memory space control
  20.         PCI_BIT_MASTER          equ 4 ; bit2: device acts as a PCI master
  21.  
  22.  
  23. ; Kernel variables
  24.  
  25.         PAGESIZE                equ 4096
  26.         PG_SW                   equ 0x003
  27.  
  28.  
  29. ; network driver types
  30.  
  31.         NET_TYPE_ETH            equ 1
  32.         NET_TYPE_SLIP           equ 2
  33.  
  34.  
  35.  
  36.         LAST_IO = 0
  37.  
  38. macro set_io addr {
  39.  
  40.         if      addr = 0
  41.         mov     edx, [device.io_addr]
  42.         else if addr = LAST_IO
  43.         else
  44.         add     edx, addr - LAST_IO
  45.         end if
  46.  
  47.         LAST_IO = addr
  48. }
  49.  
  50. macro allocate_and_clear dest, size, err {
  51.  
  52. ; We need to allocate at least 8 pages, if we want a continuous memory in ram
  53.         push    edx
  54.     if (size < 8*4096) & (size > 4096)
  55.         stdcall KernelAlloc, 8*4096
  56.     else
  57.         stdcall KernelAlloc, size
  58.     end if
  59.         pop     edx
  60.  
  61.         test    eax, eax
  62.         jz      err
  63.         mov     dest, eax               ; Save the address to it into the device struct
  64.         mov     edi, eax                ; look at last part of code!
  65.  
  66. ; Release the unused pages (if any)
  67.     if (size < 8*4096) & (size > 4096)
  68.         add     eax, (size/4096+1)*4096
  69.         mov     ecx, 8-(size/4096+1)
  70.         push    edx
  71.         call    ReleasePages
  72.         pop     edx
  73.     end if
  74.  
  75. ; Clear the allocated buffer
  76.         mov     ecx, size/4             ; divide by 4 because of DWORD
  77.         xor     eax, eax
  78.         rep     stosd
  79.  
  80. }
  81.  
  82. macro find_io bus, dev, io {
  83.  
  84.         local   .check, .inc, .got
  85.  
  86.         xor     eax, eax
  87.         mov     esi, PCI_BASE_ADDRESS_0
  88.         movzx   ecx, bus
  89.         movzx   edx, dev
  90.   .check:
  91.         stdcall PciRead32, ecx ,edx ,esi
  92.  
  93.         test    eax, PCI_BASE_ADDRESS_IO_MASK
  94.         jz      .inc
  95.  
  96.         test    eax, PCI_BASE_ADDRESS_SPACE_IO
  97.         jz      .inc
  98.  
  99.         and     eax, PCI_BASE_ADDRESS_IO_MASK
  100.         mov     io , eax
  101.         jmp     .got
  102.  
  103.   .inc:
  104.         add     esi, 4
  105.         cmp     esi, PCI_BASE_ADDRESS_5
  106.         jle     .check
  107.  
  108.   .got:
  109.  
  110. }
  111.  
  112. macro find_irq bus, dev, irq {
  113.  
  114.         push    eax edx ecx
  115.         movzx   ecx, bus
  116.         movzx   edx, dev
  117.         stdcall PciRead8, ecx ,edx ,0x3c                                ; 0x3c is the offset where irq can be found
  118.         mov     irq, al
  119.         pop     ecx edx eax
  120.  
  121. }
  122.  
  123. macro find_rev bus, dev, rev {
  124.  
  125.         push    eax edx ecx
  126.         movzx   ecx, bus
  127.         movzx   edx, dev
  128.         stdcall PciRead8, ecx ,edx ,0x8
  129.         mov     rev, al
  130.         pop     ecx edx eax
  131.  
  132. }
  133.  
  134. macro make_bus_master bus, dev {
  135.  
  136.         movzx   ecx, bus
  137.         movzx   edx, dev
  138.         stdcall PciRead32, ecx ,edx, PCI_REG_COMMAND
  139.  
  140.         or      al, PCI_BIT_MASTER ;or PCI_BIT_PIO
  141. ;        and     al, not PCI_BIT_MMIO
  142.         stdcall PciWrite32, ecx, edx, PCI_REG_COMMAND, eax
  143.  
  144. ;; TODO: try to switch to PIO, and check if PIO works or not..
  145.  
  146. }
  147.  
  148. struc IOCTL {
  149.       .handle           dd ?
  150.       .io_code          dd ?
  151.       .input            dd ?
  152.       .inp_size         dd ?
  153.       .output           dd ?
  154.       .out_size         dd ?
  155. }
  156.  
  157. virtual at edx
  158.   IOCTL IOCTL
  159. end virtual
  160.  
  161.  
  162. if used null_op
  163. align 4
  164. null_op:
  165.         or      eax, -1
  166.         ret
  167.  
  168. end if
  169.  
  170.  
  171. macro GetRealAddr { ; input and output is eax
  172.  
  173.         push    ax
  174.         call    GetPgAddr
  175.         and     word[esp], PAGESIZE - 1
  176.         or      ax, word[esp]
  177.         inc     esp
  178.         inc     esp
  179.  
  180. }
  181.  
  182. macro NET_DEVICE {
  183.  
  184.         .type           dd ?    ; Type field
  185.         .mtu            dd ?    ; Maximal Transmission Unit
  186.         .name           dd ?    ; Ptr to 0 terminated string
  187.  
  188.         .unload         dd ?    ; Ptrs to driver functions
  189.         .reset          dd ?    ;
  190.         .transmit       dd ?    ;
  191.  
  192.         .bytes_tx       dq ?    ; Statistics, updated by the driver
  193.         .bytes_rx       dq ?    ;
  194.         .packets_tx     dd ?    ;
  195.         .packets_rx     dd ?    ;
  196.  
  197.         .end:
  198. }
  199.  
  200.  
  201. macro ETH_DEVICE {
  202.       NET_DEVICE
  203.  
  204.         .set_mode       dd ?
  205.         .get_mode       dd ?
  206.  
  207.         .set_MAC        dd ?
  208.         .get_MAC        dd ?
  209.  
  210.         .mode           dd ?
  211.         .mac            dp ?
  212.                         dp ?    ; qword alignment
  213.  
  214. }
  215.  
  216.  
  217.  
  218. macro SLIP_DEVICE {
  219.         NET_DEVICE
  220.  
  221.         .set_mode       dd ?
  222.         .get_mode       dd ?
  223.  
  224.         .mode           dd ?
  225.  
  226. }