Subversion Repositories Kolibri OS

Rev

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 0 ; bit0: io space control
  19.         PCI_BIT_MMIO            equ 1 ; bit1: memory space control
  20.         PCI_BIT_MASTER          equ 2 ; bit2: device acts as a PCI master
  21.  
  22.  
  23.         PAGESIZE                equ     4096
  24.  
  25.  
  26.  
  27.         LAST_IO = 0
  28.  
  29. macro set_io addr {
  30.  
  31.         if      addr = 0
  32.  
  33.         mov     edx, [device.io_addr]
  34.  
  35.         else if addr = LAST_IO
  36.  
  37.         else
  38.  
  39.         add     edx, addr - LAST_IO
  40.  
  41.         end if
  42.  
  43.         LAST_IO = addr
  44. }
  45.  
  46.  
  47. macro diff16 title,l1,l2
  48. {
  49.   local s,d
  50.   s = l2-l1
  51.   display title,': 0x'
  52.   repeat 16
  53.     d = 48 + s shr ((16-%) shl 2) and $0F
  54.     if d > 57
  55.       d = d + 65-57-1
  56.     end if
  57.     display d
  58.   end repeat
  59.   display 13,10
  60. }
  61.  
  62. macro allocate_and_clear dest, size, err {
  63.  
  64. ; We need to allocate at least 8 pages, if we want a continuous memory in ram
  65.     if (size < 8*4096) & (size > 4096)
  66.         stdcall KernelAlloc, 8*4096
  67.     else
  68.         stdcall KernelAlloc, size
  69.     end if
  70.         test    eax, eax
  71.         jz      err
  72.         mov     dest, eax               ; Save the address to it into the device struct
  73.         mov     edi, eax                ; look at last part of code!
  74.  
  75. ; Release the unused pages (if any)
  76.     if (size < 8*4096) & (size > 4096)
  77.         add     eax, (size/4096+1)*4096
  78.         mov     ecx, 8-(size/4096+1)
  79.         call    ReleasePages
  80.     end if
  81.  
  82. ; Clear the allocated buffer
  83.         ;mov     edi, eax
  84.         mov     ecx, size/4             ; divide by 4 because of DWORD
  85.         xor     eax, eax
  86.         rep     stosd
  87.  
  88. }
  89.  
  90.  
  91.  
  92. macro find_io bus, dev, io {
  93.  
  94.  
  95.         local   .check, .inc, .got
  96.  
  97.  
  98.         xor     eax, eax
  99.         mov     esi, PCI_BASE_ADDRESS_0
  100.         movzx   ecx, bus
  101.         movzx   edx, dev
  102.   .check:
  103.         stdcall PciRead16, ecx ,edx ,esi
  104.  
  105.         mov     io , eax
  106.         and     eax, PCI_BASE_ADDRESS_IO_MASK
  107.         test    eax, eax
  108.         jz      .inc
  109.  
  110.         mov     eax, io
  111.         and     eax, PCI_BASE_ADDRESS_SPACE_IO
  112.         test    eax, eax
  113.         jz      .inc
  114.  
  115.         mov     eax, io
  116.         and     eax, PCI_BASE_ADDRESS_IO_MASK
  117.         mov     io , eax
  118.         jmp     .got
  119.  
  120.   .inc:
  121.         add     esi, 4
  122.         cmp     esi, PCI_BASE_ADDRESS_5
  123.         jbe     .check
  124.  
  125.   .got:
  126.  
  127. }
  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, (1 shl PCI_BIT_MASTER) or (1 shl PCI_BIT_PIO)
  138.         and     al, not (1 shl PCI_BIT_MMIO)
  139.         stdcall PciWrite32, ecx, edx, PCI_REG_COMMAND, eax
  140.  
  141. }
  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.  
  160.  
  161. if used null_op
  162.  
  163. align 4
  164. null_op:
  165.         or      eax, -1
  166.         ret
  167.  
  168. end if
  169.  
  170.  
  171. macro virt_to_dma { ; input is eax
  172.  
  173.         push    ax
  174.         and     word[esp], PAGESIZE - 1
  175.         call    GetPgAddr
  176.         or      ax, word[esp]
  177.         inc     esp
  178.         inc     esp
  179.  
  180. }