Subversion Repositories Kolibri OS

Rev

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