Subversion Repositories Kolibri OS

Rev

Rev 1502 | 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.       .type             dd ?
  191. }
  192.  
  193. ;struc ETH_DEVICE {
  194. macro ETH_DEVICE {
  195.       NET_DEVICE
  196. ; pointers to procedures
  197.       .unload           dd ?
  198.       .reset            dd ?
  199.       .transmit         dd ?
  200.       .set_MAC          dd ?
  201.       .get_MAC          dd ?
  202.       .set_mode         dd ?
  203.       .get_mode         dd ?
  204. ; status
  205.       .bytes_tx         dq ?
  206.       .bytes_rx         dq ?
  207.       .packets_tx       dd ?
  208.       .packets_rx       dd ?
  209.       .mode             dd ?
  210.       .name             dd ?
  211.       .mac              dp ?
  212. }
  213.  
  214.  
  215.  
  216. macro SLIP_DEVICE {
  217.         NET_DEVICE
  218. ; pointers to procedures
  219.         .unload         dd ?
  220.         .reset          dd ?
  221.         .transmit       dd ?
  222.         .set_mode       dd ?
  223.         .get_mode       dd ?
  224. ; status
  225.         .bytes_tx       dq ?
  226.         .bytes_rx       dq ?
  227.         .packets_tx     dd ?
  228.         .packets_rx     dd ?
  229.         .mode           dd ?
  230.         .name           dd ?
  231. }
  232.  
  233. macro GetRealAddr {
  234.  
  235.         push    eax
  236.         call    GetPgAddr
  237.         and     dword [esp], (PAGESIZE - 1)
  238.         add     eax, dword [esp]
  239.         add     esp, 4
  240.  
  241. }
  242.  
  243.  
  244.