StupidOS/kernel/dev/at/com.inc

199 lines
2.3 KiB
PHP
Raw Normal View History

2024-07-14 16:48:36 +00:00
;; File: com.inc
2024-07-22 21:43:35 +00:00
;; UART 8250, 16750 driver
2024-07-18 10:36:43 +00:00
;;
;; Usefull links:
;; - <PC16550D datasheet at https://www.scs.stanford.edu/10wi-cs140/pintos/specs/pc16550d.pdf>
2024-07-14 16:48:36 +00:00
2024-07-18 10:36:43 +00:00
COM1 = 0x3F8
COM2 = 0x2F8
COM3 = 0x3E8
COM4 = 0x2E8
2024-07-21 16:14:38 +00:00
NCOM = 4
2024-07-18 10:36:43 +00:00
UART8250_RBR = 0x0
UART8250_THR = 0x0
UART8250_IER = 0x1
2024-07-22 21:43:35 +00:00
UART8250_IER_RDA = 0x1
UART8250_IER_THRE = 0x2
UART8250_IER_RLSRC = 0x4
UART8250_IER_MSRC = 0x8
UART16750_IER_SLEEP = 0x10
UART16750_IER_LPM = 0x20
2024-07-18 10:36:43 +00:00
UART8250_IIR = 0x2
2024-07-22 21:43:35 +00:00
2024-07-18 10:36:43 +00:00
UART8250_FCR = 0x2
2024-07-22 21:43:35 +00:00
UART8250_FCR_EN = 0x1
UART8250_FCR_CLSR = 0x2
UART8250_FCR_CLST = 0x4
UART8250_FCR_DMA = 0x8
UART16750_FCR_64EN = 0x20
2024-07-18 10:36:43 +00:00
UART8250_LCR = 0x3
UART8250_MCR = 0x4
2024-07-22 08:29:10 +00:00
UART8250_MCR_OUT2 = 0x08
2024-07-18 10:36:43 +00:00
UART8250_LSR = 0x5
2024-07-22 08:29:10 +00:00
UART8250_LSR_THRE = 0x20
2024-07-18 10:36:43 +00:00
UART8250_MSR = 0x6
UART8250_SCR = 0x7
; DLAB = 1
UART8250_DLL = 0x0
UART8250_DLH = 0x1
2024-07-17 07:41:36 +00:00
2024-07-18 09:48:35 +00:00
com_init:
2024-07-22 08:29:10 +00:00
push ebx
xor cx, cx
.loop:
inc ch
; get io address
movzx eax, cl
shl eax, 1
add eax, aComPorts
mov bx, [eax]
mov ax, bx
push ecx
2024-07-21 16:14:38 +00:00
call com_probe
2024-07-22 08:29:10 +00:00
pop ecx
2024-07-21 16:14:38 +00:00
or eax, eax
jnz @f
2024-07-22 08:29:10 +00:00
; comX found yeah
movzx eax, ch
push ecx
push eax
mov esi, szMsgComFound
2024-07-21 16:14:38 +00:00
call klog
2024-07-22 08:29:10 +00:00
pop ecx
; mark comX as active
mov al, 1
shl al, cl
or byte [uComActive], al
2024-07-21 16:14:38 +00:00
@@:
2024-07-22 08:29:10 +00:00
inc cl
cmp cl, NCOM
jb .loop
pop ebx
2024-07-18 09:48:35 +00:00
ret
2024-07-21 16:14:38 +00:00
;; Function: com_probe
2024-07-22 08:29:10 +00:00
;;
;; In:
;; AX - IO port
2024-07-14 16:48:36 +00:00
com_probe:
2024-07-21 16:14:38 +00:00
mov dx, ax
add dx, UART8250_LCR
xor al, al
out dx, al
dec dx
out dx, al
xor ecx, ecx
2024-07-22 08:29:10 +00:00
;; wait a little
2024-07-21 16:14:38 +00:00
@@:
inc ecx
cmp ecx, 100
jbe @b
in al, dx
and al, 0x38
or al, al
jz @f
mov eax, 1
ret
@@:
xor eax, eax
2024-07-14 16:48:36 +00:00
ret
2024-07-14 20:39:48 +00:00
2024-07-22 08:29:10 +00:00
;; Function: com_putc
;;
;; In:
;; AL - Char to print
;; DX - IO port
;;
com_putc:
add dx, UART8250_LSR
push eax
@@:
in al, dx
and al, UART8250_LSR_THRE
jnz @b
pop eax
sub dx, UART8250_LSR
out dx, al
ret
ret
2024-07-18 10:36:43 +00:00
com_irq1:
2024-07-18 09:48:35 +00:00
pusha
2024-07-22 08:29:10 +00:00
mov esi, szMsgComIRQ
call klog
2024-07-18 09:48:35 +00:00
popa
2024-07-14 20:39:48 +00:00
iret
2024-07-17 07:41:36 +00:00
2024-07-18 10:36:43 +00:00
com_irq2:
pusha
popa
iret
2024-07-19 07:53:03 +00:00
2024-07-21 16:14:38 +00:00
com_open:
ret
com_close:
ret
com_read:
ret
com_write:
ret
com_ioctl:
ret
com_select:
ret
com_stop:
ret
com_mmap:
ret
com_reset:
ret
uCom1Lock dd 0
uCom2Lock dd 0
uComActive db 0
2024-07-22 08:29:10 +00:00
szMsgComFound db "COM: com%u found", 0
szMsgComIRQ db "com irq", 0
aComPorts:
dw COM1
dw COM2
dw COM3
dw COM4
.end:
2024-07-21 16:14:38 +00:00
2024-07-19 07:53:03 +00:00
com_device:
db 'com', 0, 0, 0, 0, 0
dd com_init
2024-07-21 16:14:38 +00:00
com_cdevsw:
dd com_open
dd com_close
dd com_read
dd com_write
dd com_ioctl
dd com_select
dd com_stop
dd com_mmap
dd com_reset