Subversion Repositories Kolibri OS

Rev

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