summaryrefslogtreecommitdiffstats
path: root/contrib/syslinux-4.02/core/serirq.inc
blob: 47ccd50fd3d8bbd6655a5fb4b8f844fe972660c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
;; -----------------------------------------------------------------------
;;
;;   Copyright 2009 Intel Corporation; author: H. Peter Anvin
;;
;;   This program is free software; you can redistribute it and/or modify
;;   it under the terms of the GNU General Public License as published by
;;   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
;;   Boston MA 02111-1307, USA; either version 2 of the License, or
;;   (at your option) any later version; incorporated herein by reference.
;;
;; -----------------------------------------------------------------------

;;
;; serirq.inc
;;
;; Serial port IRQ code
;;
;; We don't know what IRQ, if any, we have, so map all of them...
;;

		section .text16
		bits 16
		align 8

		section .bss16
		alignb 8

%assign n 0
%rep 16
		section .text16
serstub_irq %+ n :
		push dword [cs:oldirq %+ n]
		jmp short irq_common

		section .bss16
oldirq %+ n	resd 1
%assign n n+1
%endrep

		section .text16
irq_common:
		pushf
		push ax
		push dx
		mov dx,[cs:SerialPort]
		add dx,5			; DX -> LSR
		in al,dx
		test al,1			; Received data
		jnz .data
.done:
		pop dx
		pop ax
		popf
		retf				; Chain to next handler
.data:
		push es
		push di
		mov ax,aux_seg + (aux.serial >> 4)
		mov es,ax
		mov di,[cs:SerialHead]
.loop:
		mov dx,[cs:SerialPort]		; DX -> RDR
		in al,dx
		stosb
		mov ah,[cs:FlowIgnore]
		add dx,5			; DX -> LSR
		in al,dx
		push ax
		and al,ah
		cmp al,ah
		jne .drop
		and di,serial_buf_size-1	; Wrap around if necessary
		cmp di,[cs:SerialTail]		; Would this cause overflow?
		je .drop			; If so, just drop the data
		mov [cs:SerialHead],di
.drop:
		pop ax
		test al,1			; More data?
		jnz .loop
.full:
		pop di
		pop es
		jmp .done

		section .bss16
;
; SerialIRQPort will generally track SerialPort, but will be 0 when an
; IRQ service is not installed.
;
SerialIRQPort	resw 1			; Serial port w IRQ service
SerialHead	resw 1			; Head of serial port rx buffer
SerialTail	resw 1			; Tail of serial port rx buffer

		section .bss16
IRQMask		resw 1			; PIC IRQ mask status

		section .text16

sirq_install:
		pushad

		call sirq_cleanup

		; Save the old interrupt vectors
		mov si,4*08h
		mov di,oldirq0
		mov cx,8
		rep movsd
		mov si,4*70h
		mov cx,8
		rep movsd

		; Install new interrupt vectors
		mov di,4*08h
		mov cx,8
		mov eax,serstub_irq0
.pic0:
		stosd
		add ax,serstub_irq1 - serstub_irq0
		loop .pic0
		mov di,4*70h
		mov cx,8
.pic1:
		stosd
		add ax,serstub_irq1 - serstub_irq0
		loop .pic1

		mov bx,[SerialPort]
		mov [SerialIRQPort],bx

		lea dx,[bx+5]		; DX -> LCR
		mov al,03h		; Clear DLAB (should already be...)
		slow_out dx,al

		lea dx,[bx+1]		; DX -> IER
		mov al,1		; Enable receive interrupt
		slow_out dx,al

		;
		; Enable all ther interupt lines at the PIC.  Some BIOSes
		; only enable the timer interrupts and other interrupts
		; actively in use by the BIOS.
		;
		in al,0xA1		; Secondary PIC mask register
		mov ah,al
		in al,0x21		; Primary PIC mask register
		mov [IRQMask],ax

		io_delay

		xor ax,ax		; Remove all interrupt masks
		out 0x21,al
		out 0xA1,al

		popad
		ret

sirq_cleanup_nowipe:
		pushad
		push ds
		push es
		xor ax,ax
		mov ds,ax
		mov es,ax

		mov bx,[SerialIRQPort]
		and bx,bx
		jz .done

		lea dx,[bx+5]		; DX -> LCR
		mov al,03h		; Clear DLAB (should already be...)
		slow_out dx,al

		lea dx,[bx+1]		; DX -> IER
		xor ax,ax
		slow_out dx,al		; Clear IER

		; Restore PIC masks
		mov ax,[IRQMask]
		out 0x21,al
		mov al,ah
		out 0xA1,al

		; Restore the original interrupt vectors
		mov si,oldirq0
		mov di,4*08h
		mov cx,8
		rep movsd
		mov di,4*70h
		mov cx,8
		rep movsd

		xor ax,ax
		mov [SerialIRQPort],ax		; No active interrupt system

.done:
		pop es
		pop ds
		popad
		ret

sirq_cleanup:
		call sirq_cleanup_nowipe
		pushad
		push es
		; Just in case it might contain a password, erase the
		; serial port receive buffer...
		mov ax,aux_seg + (aux.serial >> 4)
		mov es,ax
		xor eax,eax
		mov [cs:SerialHead],eax
		mov cx,serial_buf_size >> 2
		xor di,di
		rep stosd
		pop es
		popad
		ret

		section .text16