Subversion Repositories Kolibri OS

Rev

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