Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2014. All rights reserved.      ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. $Revision: 4850 $
  9.  
  10.  
  11. ;-----------------------------------------------------------------------------
  12. ; find the IDE controller in the device list
  13. ;-----------------------------------------------------------------------------
  14.         mov     ecx, IDE_controller_1
  15.         mov     esi, pcidev_list
  16. ;--------------------------------------
  17. align 4
  18. .loop:
  19.         mov     esi, [esi+PCIDEV.fd]
  20.         cmp     esi, pcidev_list
  21.         jz      find_IDE_controller_done
  22.  
  23.         mov     eax, [esi+PCIDEV.class]
  24.         shr     eax, 4
  25.         cmp     eax, 0x01018
  26.         jnz     .loop
  27. ;--------------------------------------
  28. .found:
  29.         mov     eax, [esi+PCIDEV.class]
  30.         DEBUGF  1, 'K : IDE controller programming interface %x\n', eax
  31.         mov     [ecx+IDE_DATA.ProgrammingInterface], eax
  32.  
  33.         mov     ah, [esi+PCIDEV.bus]
  34.         mov     al, 2
  35.         mov     bh, [esi+PCIDEV.devfn]
  36. ;--------------------------------------
  37.         mov     dx, 0x1F0
  38.         test    byte [esi+PCIDEV.class], 1
  39.         jz      @f
  40.         mov     bl, 0x10
  41.         push    eax
  42.         call    pci_read_reg
  43.         and     eax, 0xFFFC
  44.         mov     edx, eax
  45.         pop     eax
  46. @@:
  47.         DEBUGF  1, 'K : BAR0 IDE base addr %x\n', dx
  48.         mov     [StandardATABases], dx
  49.         mov     [ecx+IDE_DATA.BAR0_val], dx
  50. ;--------------------------------------
  51.         mov     dx, 0x3F4
  52.         test    byte [esi+PCIDEV.class], 1
  53.         jz      @f
  54.         mov     bl, 0x14
  55.         push    eax
  56.         call    pci_read_reg
  57.         and     eax, 0xFFFC
  58.         mov     edx, eax
  59.         pop     eax
  60. @@:
  61.         DEBUGF  1, 'K : BAR1 IDE base addr %x\n', dx
  62.         mov     [ecx+IDE_DATA.BAR1_val], dx
  63. ;--------------------------------------
  64.         mov     dx, 0x170
  65.         test    byte [esi+PCIDEV.class], 4
  66.         jz      @f
  67.         mov     bl, 0x18
  68.         push    eax
  69.         call    pci_read_reg
  70.         and     eax, 0xFFFC
  71.         mov     edx, eax
  72.         pop     eax
  73. @@:
  74.         DEBUGF  1, 'K : BAR2 IDE base addr %x\n', dx
  75.         mov     [StandardATABases+2], dx
  76.         mov     [ecx+IDE_DATA.BAR2_val], dx
  77. ;--------------------------------------
  78.         mov     dx, 0x374
  79.         test    byte [esi+PCIDEV.class], 4
  80.         jz      @f
  81.         mov     bl, 0x1C
  82.         push    eax
  83.         call    pci_read_reg
  84.         and     eax, 0xFFFC
  85.         mov     edx, eax
  86.         pop     eax
  87. @@:
  88.         DEBUGF  1, 'K : BAR3 IDE base addr %x\n', dx
  89.         mov     [ecx+IDE_DATA.BAR3_val], dx
  90. ;--------------------------------------
  91.         mov     bl, 0x20
  92.         push    eax
  93.         call    pci_read_reg
  94.         and     eax, 0xFFFC
  95.         DEBUGF  1, 'K : BAR4 IDE controller register base addr %x\n', ax
  96.         mov     [ecx+IDE_DATA.RegsBaseAddres], ax
  97.         pop     eax
  98. ;--------------------------------------
  99.         mov     bl, 0x3C
  100.         push    eax
  101.         call    pci_read_reg
  102.         and     eax, 0xFF
  103.         DEBUGF  1, 'K : IDE Interrupt %x\n', al
  104.         mov     [ecx+IDE_DATA.Interrupt], ax
  105.         pop     eax
  106.  
  107.         add     ecx, sizeof.IDE_DATA
  108. ;--------------------------------------
  109.         jmp     .loop
  110. ;-----------------------------------------------------------------------------
  111. uglobal
  112. align 4
  113. ;--------------------------------------
  114. IDE_controller_pointer dd ?
  115. ;--------------------------------------
  116. IDE_controller_1 IDE_DATA
  117. IDE_controller_2 IDE_DATA
  118. IDE_controller_3 IDE_DATA
  119. ;--------------------------------------
  120. cache_ide0  IDE_CACHE
  121. cache_ide1  IDE_CACHE
  122. cache_ide2  IDE_CACHE
  123. cache_ide3  IDE_CACHE
  124. cache_ide4  IDE_CACHE
  125. cache_ide5  IDE_CACHE
  126. cache_ide6  IDE_CACHE
  127. cache_ide7  IDE_CACHE
  128. cache_ide8  IDE_CACHE
  129. cache_ide9  IDE_CACHE
  130. cache_ide10 IDE_CACHE
  131. cache_ide11 IDE_CACHE
  132. ;--------------------------------------
  133. IDE_device_1 rd 2
  134. IDE_device_2 rd 2
  135. IDE_device_3 rd 2
  136. ;--------------------------------------
  137. endg
  138. ;-----------------------------------------------------------------------------
  139. ; START of initialisation IDE ATA code
  140. ;-----------------------------------------------------------------------------
  141. Init_IDE_ATA_controller:
  142.         cmp     [ecx+IDE_DATA.ProgrammingInterface], 0
  143.         jne     @f
  144.  
  145.         ret
  146. ;--------------------------------------
  147. @@:
  148.         mov     esi, boot_disabling_ide
  149.         call    boot_log
  150. ;--------------------------------------
  151. ; Disable IDE interrupts, because the search
  152. ; for IDE partitions is in the PIO mode.
  153. ;--------------------------------------
  154. .disable_IDE_interrupt:
  155. ; Disable interrupts in IDE controller for PIO
  156.         mov     al, 2
  157.         mov     dx, [ecx+IDE_DATA.BAR1_val] ;0x3F4
  158.         add     dx, 2 ;0x3F6
  159.         out     dx, al
  160.         mov     dx, [ecx+IDE_DATA.BAR3_val] ;0x374
  161.         add     dx, 2 ;0x376
  162.         out     dx, al
  163. ;-----------------------------------------------------------------------------
  164. ; set current ata bases
  165. @@:
  166.         mov     ax, [ecx+IDE_DATA.BAR0_val]
  167.         mov     [StandardATABases], ax
  168.         mov     ax, [ecx+IDE_DATA.BAR2_val]
  169.         mov     [StandardATABases+2], ax
  170.  
  171.         mov     esi, boot_detecthdcd
  172.         call    boot_log
  173. ;--------------------------------------
  174. include 'dev_hdcd.inc'
  175. ;--------------------------------------
  176.         ret
  177. ;-----------------------------------------------------------------------------
  178. Init_IDE_ATA_controller_2:
  179.         cmp     [ecx+IDE_DATA.ProgrammingInterface], 0
  180.         jne     @f
  181.  
  182.         ret
  183. ;--------------------------------------
  184. @@:
  185.         mov     dx, [ecx+IDE_DATA.RegsBaseAddres]
  186. ; test whether it is our interrupt?
  187.         add     dx, 2
  188.         in      al, dx
  189.         test    al, 100b
  190.         jz      @f
  191. ; clear Bus Master IDE Status register
  192. ; clear Interrupt bit
  193.         out     dx, al
  194. ;--------------------------------------
  195. @@:
  196.         add     dx, 8
  197. ; test whether it is our interrupt?
  198.         in      al, dx
  199.         test    al, 100b
  200.         jz      @f
  201. ; clear Bus Master IDE Status register
  202. ; clear Interrupt bit
  203.         out     dx, al
  204. ;--------------------------------------
  205. @@:
  206. ; read status register and remove the interrupt request
  207.         mov     dx, [ecx+IDE_DATA.BAR0_val] ;0x1F0
  208.         add     dx, 0x7 ;0x1F7
  209.         in      al, dx
  210.         mov     dx, [ecx+IDE_DATA.BAR2_val] ;0x170
  211.         add     dx, 0x7 ;0x177
  212.         in      al, dx
  213. ;-----------------------------------------------------------------------------
  214. ;        push    eax edx
  215. ;        mov     dx, [ecx+IDE_DATA.RegsBaseAddres]
  216. ;        xor     eax, eax
  217. ;        add     dx, 2
  218. ;        in      al, dx
  219. ;        DEBUGF  1, "K : Primary Bus Master IDE Status Register %x\n", eax
  220.  
  221. ;        add     dx, 8
  222. ;        in      al, dx
  223. ;        DEBUGF  1, "K : Secondary Bus Master IDE Status Register %x\n", eax
  224. ;        pop     edx eax
  225.  
  226. ;        cmp     [ecx+IDE_DATA.RegsBaseAddres], 0
  227. ;        setnz   [ecx+IDE_DATA.dma_hdd]
  228. ;-----------------------------------------------------------------------------
  229. ; set interrupts for IDE Controller
  230. ;-----------------------------------------------------------------------------
  231.         pushfd
  232.         cli
  233. .enable_IDE_interrupt:
  234.         mov     esi, boot_enabling_ide
  235.         call    boot_log
  236. ; Enable interrupts in IDE controller for DMA
  237.         xor     ebx, ebx
  238.         cmp     ecx, IDE_controller_2
  239.         jne     @f
  240.  
  241.         add     ebx, 5
  242.         jmp     .check_DRIVE_DATA
  243. ;--------------------------------------
  244. @@:
  245.         cmp     ecx, IDE_controller_3
  246.         jne     .check_DRIVE_DATA
  247.  
  248.         add     ebx, 10
  249. ;--------------------------------------
  250. .check_DRIVE_DATA:
  251.         mov     al, 0
  252.         mov     ah, [ebx+DRIVE_DATA+1]
  253.         test    ah, 10100000b ; check for ATAPI devices
  254.         jz      @f
  255. ;--------------------------------------
  256. .ch1_pio_set_ATAPI:
  257.         DEBUGF  1, "K : IDE CH1 PIO, because ATAPI drive present\n"
  258.         jmp     .ch1_pio_set_for_all
  259. ;--------------------------------------
  260. .ch1_pio_set_no_devices:
  261.         DEBUGF  1, "K : IDE CH1 PIO because no devices\n"
  262.         jmp     .ch1_pio_set_for_all
  263. ;-------------------------------------
  264. .ch1_pio_set:
  265.         DEBUGF  1, "K : IDE CH1 PIO because device not support UDMA\n"
  266. ;-------------------------------------
  267. .ch1_pio_set_for_all:
  268.         mov     [ecx+IDE_DATA.dma_hdd_channel_1], al
  269.         jmp     .ch2_check
  270. ;--------------------------------------
  271. @@:
  272.         xor     ebx, ebx
  273.         call    calculate_IDE_device_values_storage
  274.  
  275.         test    ah, 1010000b
  276.         jz      .ch1_pio_set_no_devices
  277.  
  278.         test    ah, 1000000b
  279.         jz      @f
  280.  
  281.         cmp     [ebx+IDE_DEVICE.UDMA_possible_modes], al
  282.         je      .ch1_pio_set
  283.  
  284.         cmp     [ebx+IDE_DEVICE.UDMA_set_mode], al
  285.         je      .ch1_pio_set
  286. ;--------------------------------------
  287. @@:
  288.         test    ah, 10000b
  289.         jz      @f
  290.  
  291.         add     ebx, 2
  292.  
  293.         cmp     [ebx+IDE_DEVICE.UDMA_possible_modes], al
  294.         je      .ch1_pio_set
  295.  
  296.         cmp     [ebx+IDE_DEVICE.UDMA_set_mode], al
  297.         je      .ch1_pio_set
  298. ;--------------------------------------
  299. @@:
  300.         mov     dx, [ecx+IDE_DATA.BAR1_val] ;0x3F4
  301.         add     dx, 2 ;0x3F6
  302.         out     dx, al
  303.         DEBUGF  1, "K : IDE CH1 DMA enabled\n"
  304.         mov     [ecx+IDE_DATA.dma_hdd_channel_1], byte 1
  305. ;--------------------------------------
  306. .ch2_check:
  307.         test    ah, 1010b ; check for ATAPI devices
  308.         jz      @f
  309. ;--------------------------------------
  310. .ch2_pio_set_ATAPI:
  311.         DEBUGF  1, "K : IDE CH2 PIO, because ATAPI drive present\n"
  312.         jmp     .ch2_pio_set_for_all
  313. ;--------------------------------------
  314. .ch2_pio_set_no_devices:
  315.         DEBUGF  1, "K : IDE CH2 PIO because no devices\n"
  316.         jmp     .ch2_pio_set_for_all
  317. ;--------------------------------------
  318. .ch2_pio_set:
  319.         DEBUGF  1, "K : IDE CH2 PIO because device not support UDMA\n"
  320. ;--------------------------------------
  321. .ch2_pio_set_for_all:
  322.         mov     [ecx+IDE_DATA.dma_hdd_channel_2], al
  323.         jmp     .set_interrupts_for_IDE_controllers
  324. ;--------------------------------------
  325. @@:
  326.         mov     ebx, 4
  327.         call    calculate_IDE_device_values_storage
  328.  
  329.         test    ah, 101b
  330.         jz      .ch2_pio_set_no_devices
  331.  
  332.         test    ah, 100b
  333.         jz      @f
  334.  
  335.         cmp     [ebx+IDE_DEVICE.UDMA_possible_modes], al
  336.         je      .ch2_pio_set
  337.  
  338.         cmp     [ebx+IDE_DEVICE.UDMA_set_mode], al
  339.         je      .ch2_pio_set
  340. ;--------------------------------------
  341. @@:
  342.         test    ah, 1b
  343.         jz      @f
  344.  
  345.         add     ebx, 2
  346.  
  347.         cmp     [ebx+IDE_DEVICE.UDMA_possible_modes], al
  348.         je      .ch2_pio_set
  349.  
  350.         cmp     [ebx+IDE_DEVICE.UDMA_set_mode], al
  351.         je      .ch2_pio_set
  352. ;--------------------------------------
  353. @@:
  354.         mov     dx, [ecx+IDE_DATA.BAR3_val] ;0x374
  355.         add     dx, 2 ;0x376
  356.         out     dx, al
  357.         DEBUGF  1, "K : IDE CH2 DMA enabled\n"
  358.         mov     [ecx+IDE_DATA.dma_hdd_channel_2], byte 1
  359. ;--------------------------------------
  360. .set_interrupts_for_IDE_controllers:
  361.         mov     esi, boot_set_int_IDE
  362.         call    boot_log
  363. ;--------------------------------------
  364.         mov     eax, [ecx+IDE_DATA.ProgrammingInterface]
  365.         cmp     ax, 0x0180
  366.         je      .pata_ide
  367.  
  368.         cmp     ax, 0x018a
  369.         jne     .sata_ide
  370. ;--------------------------------------
  371. .pata_ide:
  372.         cmp     [ecx+IDE_DATA.RegsBaseAddres], 0
  373.         je      .end_set_interrupts
  374.  
  375.         push    ecx
  376.         stdcall attach_int_handler, 14, IDE_irq_14_handler, 0
  377.         DEBUGF  1, "K : Set IDE IRQ14 return code %x\n", eax
  378.         stdcall attach_int_handler, 15, IDE_irq_15_handler, 0
  379.         DEBUGF  1, "K : Set IDE IRQ15 return code %x\n", eax
  380.         pop     ecx
  381.  
  382.         jmp     .end_set_interrupts
  383. ;--------------------------------------
  384. .sata_ide:
  385.         cmp     ax, 0x0185
  386.         je      .sata_ide_1
  387.  
  388.         cmp     ax, 0x018f
  389.         jne     .end_set_interrupts
  390. ;--------------------------------------
  391. .sata_ide_1:
  392. ; Some weird controllers generate an interrupt even if IDE interrupts
  393. ; are disabled and no IDE devices. For example, notebook ASUS K72F -
  394. ; IDE controller 010185 generates false interrupt when we work with
  395. ; the IDE controller 01018f. For this reason, the interrupt handler
  396. ; does not need to be installed if both channel IDE controller
  397. ; running in PIO mode.
  398.         cmp     [ecx+IDE_DATA.RegsBaseAddres], 0
  399.         je      .end_set_interrupts
  400.  
  401.         cmp     [ecx+IDE_DATA.dma_hdd_channel_1], 0
  402.         jne     @f
  403.  
  404.         cmp     [ecx+IDE_DATA.dma_hdd_channel_2], 0
  405.         je      .end_set_interrupts
  406. ;--------------------------------------
  407. @@:
  408.         mov     ax, [ecx+IDE_DATA.Interrupt]
  409.         movzx   eax, al
  410.         push    ecx
  411.         stdcall attach_int_handler, eax, IDE_common_irq_handler, 0
  412.         pop     ecx
  413.         DEBUGF  1, "K : Set IDE IRQ%d return code %x\n", [ecx+IDE_DATA.Interrupt]:1, eax
  414. ;--------------------------------------
  415. .end_set_interrupts:
  416.         popfd
  417.         ret
  418. ;-----------------------------------------------------------------------------
  419. ; END of initialisation IDE ATA code
  420. ;-----------------------------------------------------------------------------
  421. find_IDE_controller_done:
  422.         mov     ecx, IDE_controller_1
  423.         mov     [IDE_controller_pointer], ecx
  424.         call    Init_IDE_ATA_controller
  425.         mov     ecx, IDE_controller_2
  426.         mov     [IDE_controller_pointer], ecx
  427.         call    Init_IDE_ATA_controller
  428.         mov     ecx, IDE_controller_3
  429.         mov     [IDE_controller_pointer], ecx
  430.         call    Init_IDE_ATA_controller
  431. ;-----------------------------------------------------------------------------
  432.         mov     esi, boot_getcache
  433.         call    boot_log
  434. include 'getcache.inc'
  435. ;-----------------------------------------------------------------------------
  436.         mov     esi, boot_detectpart
  437.         call    boot_log
  438. include 'sear_par.inc'
  439. ;-----------------------------------------------------------------------------
  440.         mov     esi, boot_init_sys
  441.         call    boot_log
  442.         call    Parser_params
  443.  
  444. if ~ defined extended_primary_loader
  445. ; ramdisk image should be loaded by extended primary loader if it exists
  446. ; READ RAMDISK IMAGE FROM HD
  447. include '../boot/rdload.inc'
  448. end if
  449. ;-----------------------------------------------------------------------------
  450.         mov     ecx, IDE_controller_1
  451.         mov     [IDE_controller_pointer], ecx
  452.         call    Init_IDE_ATA_controller_2
  453.         mov     ecx, IDE_controller_2
  454.         mov     [IDE_controller_pointer], ecx
  455.         call    Init_IDE_ATA_controller_2
  456.         mov     ecx, IDE_controller_3
  457.         mov     [IDE_controller_pointer], ecx
  458.         call    Init_IDE_ATA_controller_2
  459. ;-----------------------------------------------------------------------------
  460.