Subversion Repositories Kolibri OS

Rev

Rev 5558 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2015-2017. All rights reserved. ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. format PE DLL native 0.05
  9. entry START
  10.  
  11.         DEBUG                   = 1
  12.         __DEBUG__               = 1
  13.         __DEBUG_LEVEL__         = 1             ; 1 = verbose, 2 = errors only
  14.  
  15.  
  16.         API_VERSION             = 0  ;debug
  17.  
  18.         STRIDE                  = 4      ;size of row in devices table
  19.  
  20.         SRV_GETVERSION          = 0
  21.  
  22. section '.flat' code readable writable executable
  23.  
  24. include '../proc32.inc'
  25. include '../struct.inc'
  26. include '../macros.inc'
  27. include '../peimport.inc'
  28. include '../fdo.inc'
  29.  
  30. GPIO_PORT_CONFIG_ADDR = 0xF100
  31. GPIO_DATA_ADDR = 0xF200
  32. ADC_ADDR = 0xFE00
  33.  
  34. proc START c, state:dword, cmdline:dword
  35.  
  36.         cmp     [state], 1
  37.         jne     .exit
  38. .entry:
  39.  
  40.         push    esi
  41.         DEBUGF  1,"Loading vortex86EX GPIO driver\n"
  42.         call    detect
  43.         pop     esi
  44.         test    eax, eax
  45.         jz      .fail
  46.  
  47. ; Set crossbar base address register in southbridge
  48.         invoke  PciWrite16, [bus], [dev], 0x64, 0x0A00 or 1
  49.  
  50. ; Set GPIO base address register in southbridge
  51.         invoke  PciWrite16, [bus], [dev], 0x62, GPIO_PORT_CONFIG_ADDR or 1
  52.  
  53.         DEBUGF  1,"Setting up ADC\n"
  54.  
  55. ; Enable ADC
  56.         invoke  PciRead32, [bus], [dev], 0xBC
  57.         and     eax, not (1 shl 28)
  58.         invoke  PciWrite32, [bus], [dev], 0xBC, eax
  59.  
  60.         DEBUGF  1,"1\n"
  61.  
  62. ; Set ADC base address
  63.         mov     ebx, [dev]
  64.         inc     ebx
  65.         invoke  PciRead16, [bus], ebx, 0xDE
  66.         or      ax, 0x02
  67.         invoke  PciWrite16, [bus], ebx, 0xDE, eax
  68.  
  69.         invoke  PciWrite32, [bus], ebx, 0xE0, 0x00500000 or ADC_ADDR
  70.  
  71.         DEBUGF  1,"2\n"
  72.  
  73. ; set up ADC
  74.         mov     dx, ADC_ADDR + 1
  75.         xor     al, al
  76.         out     dx, al
  77.  
  78.         DEBUGF  1,"3\n"
  79.  
  80. ; Empty FIFO
  81.   @@:
  82.         mov     dx, ADC_ADDR + 2        ; Status register
  83.         in      al, dx
  84.         test    al, 0x01                ; FIFO ready
  85.         jz      @f
  86.         mov     dx, ADC_ADDR + 4
  87.         in      ax, dx
  88.         jmp     @r
  89.   @@:
  90.  
  91.         DEBUGF  1,"4\n"
  92.  
  93. ; Enable GPIO0-9
  94.         mov     dx, GPIO_PORT_CONFIG_ADDR + 0  ; General-Purpose I/O Data & Direction Decode Enable
  95.         mov     eax, 0x000001ff
  96.         out     dx, eax
  97.  
  98.         mov     ecx, 10                 ; 10 GPIO ports total
  99.         mov     dx, GPIO_PORT_CONFIG_ADDR + 4  ; General-Purpose I/O Port0 Data & Direction Decode Address
  100.         mov     ax, GPIO_DATA_ADDR
  101.   .gpio_init:
  102. ; Set GPIO data port base address
  103.         out     dx, ax
  104.         add     ax, 2
  105.         add     dx, 2
  106. ; Set GPIO direction base address
  107.         out     dx, ax
  108.         add     ax, 2
  109.         add     dx, 2
  110. ; loop
  111.         dec     ecx
  112.         jnz     .gpio_init
  113.  
  114. ; Set GPIO0 pin 0 as output
  115.         mov     al, 0x01
  116.         mov     dx, GPIO_DATA_ADDR + 0*4 + 2
  117.         out     dx, al
  118.  
  119.         invoke  RegService, my_service, service_proc
  120.         ret
  121.   .fail:
  122.   .exit:
  123.         xor     eax, eax
  124.         ret
  125. endp
  126.  
  127. proc service_proc stdcall, ioctl:dword
  128.  
  129.         mov     ebx, [ioctl]
  130.         mov     eax, [ebx+IOCTL.io_code]
  131.         cmp     eax, SRV_GETVERSION
  132.         jne     @F
  133.  
  134.         mov     eax, [ebx+IOCTL.output]
  135.         cmp     [ebx+IOCTL.out_size], 4
  136.         jne     .fail
  137.         mov     dword [eax], API_VERSION
  138.         xor     eax, eax
  139.         ret
  140.   @@:
  141.         cmp     eax, 1  ; read GPIO P0
  142.         jne     .no_gpioread
  143.         mov     dx, GPIO_DATA_ADDR + 0x00
  144.         in      al, dx
  145.         ret
  146.   .no_gpioread:
  147.         cmp     eax, 2  ; write GPIO P0
  148.         jne     .no_gpiowrite
  149.  
  150.         mov     eax, [ebx + IOCTL.input]
  151.         mov     dx, GPIO_DATA_ADDR + 0x00
  152.         out     dx, al
  153.         xor     eax, eax
  154.         ret
  155.   .no_gpiowrite:
  156.         cmp     eax, 3  ; read ADC channel 0
  157.         jne     .no_adcread
  158.  
  159.         mov     dx, ADC_ADDR + 1
  160.         mov     al, 1 shl 3             ; Power down ADC
  161.         out     dx, al
  162.  
  163.         mov     dx, ADC_ADDR + 0        ; AUX channel select register
  164.         mov     al, 1 shl 0             ; Enable AUX0 scan
  165.         out     dx, al
  166.  
  167.         mov     dx, ADC_ADDR + 1
  168.         mov     al, 1 shl 0             ; Single shot, no interrupts, start
  169.         out     dx, al
  170.  
  171.         mov     dx, ADC_ADDR + 2
  172.   @@:
  173.         in      al, dx
  174.         test    al, 1 shl 0             ; data ready?
  175.         jz      @r
  176.  
  177.         mov     dx, ADC_ADDR + 4
  178.         in      ax, dx                  ; read the data and return to user call
  179.         DEBUGF  1, "ADC read: 0x%x\n", eax:4
  180.         ret
  181.   .no_adcread:
  182.   .fail:
  183.         or      eax, -1
  184.         ret
  185. endp
  186.  
  187.  
  188. proc detect
  189.         push    ebx
  190.         invoke  GetPCIList
  191.         mov     ebx, eax
  192.   .next_dev:
  193.         mov     eax, [eax+PCIDEV.fd]
  194.         cmp     eax, ebx
  195.         jz      .err
  196.         mov     edx, [eax+PCIDEV.vendor_device_id]
  197.  
  198.         mov     esi, devices
  199.   @@:
  200.         cmp     dword [esi], 0
  201.         jz      .next_dev
  202.         cmp     edx, [esi]
  203.         jz      .found
  204.  
  205.         add     esi, STRIDE
  206.         jmp     @B
  207.  
  208.   .found:
  209.         movzx   ebx, [eax+PCIDEV.devfn]
  210.         mov     [dev], ebx
  211.         movzx   ebx, [eax+PCIDEV.bus]
  212.         mov     [bus], ebx
  213.         xor     eax, eax
  214.         inc     eax
  215.         pop     ebx
  216.         ret
  217.   .err:
  218.         DEBUGF  1,"Could not find vortex86EX south bridge!\n"
  219.         xor     eax, eax
  220.         pop     ebx
  221.         ret
  222. endp
  223.  
  224. DEVICE_ID    = 6011h
  225. VENDOR_ID    = 17F3h
  226.  
  227. ;all initialized data place here
  228.  
  229. align 4
  230. devices      dd (DEVICE_ID shl 16)+VENDOR_ID
  231.              dd 0    ;terminator
  232.  
  233. my_service   db '86DUINO-GPIO',0  ;max 16 chars include zero
  234.  
  235. include_debug_strings                           ; All data wich FDO uses will be included here
  236.  
  237. dev     dd ?
  238. bus     dd ?
  239.  
  240. align 4
  241. data fixups
  242. end data
  243.