root / hw / serial.c @ d796321b
History | View | Annotate | Download (12.5 kB)
1 | 80cabfad | bellard | /*
|
---|---|---|---|
2 | 80cabfad | bellard | * QEMU 16450 UART emulation
|
3 | 80cabfad | bellard | *
|
4 | 80cabfad | bellard | * Copyright (c) 2003-2004 Fabrice Bellard
|
5 | 80cabfad | bellard | *
|
6 | 80cabfad | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 80cabfad | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 80cabfad | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 80cabfad | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 80cabfad | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 80cabfad | bellard | * furnished to do so, subject to the following conditions:
|
12 | 80cabfad | bellard | *
|
13 | 80cabfad | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 80cabfad | bellard | * all copies or substantial portions of the Software.
|
15 | 80cabfad | bellard | *
|
16 | 80cabfad | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 80cabfad | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 80cabfad | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 80cabfad | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 80cabfad | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 80cabfad | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 80cabfad | bellard | * THE SOFTWARE.
|
23 | 80cabfad | bellard | */
|
24 | 80cabfad | bellard | #include "vl.h" |
25 | 80cabfad | bellard | |
26 | 80cabfad | bellard | //#define DEBUG_SERIAL
|
27 | 80cabfad | bellard | |
28 | 80cabfad | bellard | #define UART_LCR_DLAB 0x80 /* Divisor latch access bit */ |
29 | 80cabfad | bellard | |
30 | 80cabfad | bellard | #define UART_IER_MSI 0x08 /* Enable Modem status interrupt */ |
31 | 80cabfad | bellard | #define UART_IER_RLSI 0x04 /* Enable receiver line status interrupt */ |
32 | 80cabfad | bellard | #define UART_IER_THRI 0x02 /* Enable Transmitter holding register int. */ |
33 | 80cabfad | bellard | #define UART_IER_RDI 0x01 /* Enable receiver data interrupt */ |
34 | 80cabfad | bellard | |
35 | 80cabfad | bellard | #define UART_IIR_NO_INT 0x01 /* No interrupts pending */ |
36 | 80cabfad | bellard | #define UART_IIR_ID 0x06 /* Mask for the interrupt ID */ |
37 | 80cabfad | bellard | |
38 | 80cabfad | bellard | #define UART_IIR_MSI 0x00 /* Modem status interrupt */ |
39 | 80cabfad | bellard | #define UART_IIR_THRI 0x02 /* Transmitter holding register empty */ |
40 | 80cabfad | bellard | #define UART_IIR_RDI 0x04 /* Receiver data interrupt */ |
41 | 80cabfad | bellard | #define UART_IIR_RLSI 0x06 /* Receiver line status interrupt */ |
42 | 80cabfad | bellard | |
43 | 80cabfad | bellard | /*
|
44 | 80cabfad | bellard | * These are the definitions for the Modem Control Register
|
45 | 80cabfad | bellard | */
|
46 | 80cabfad | bellard | #define UART_MCR_LOOP 0x10 /* Enable loopback test mode */ |
47 | 80cabfad | bellard | #define UART_MCR_OUT2 0x08 /* Out2 complement */ |
48 | 80cabfad | bellard | #define UART_MCR_OUT1 0x04 /* Out1 complement */ |
49 | 80cabfad | bellard | #define UART_MCR_RTS 0x02 /* RTS complement */ |
50 | 80cabfad | bellard | #define UART_MCR_DTR 0x01 /* DTR complement */ |
51 | 80cabfad | bellard | |
52 | 80cabfad | bellard | /*
|
53 | 80cabfad | bellard | * These are the definitions for the Modem Status Register
|
54 | 80cabfad | bellard | */
|
55 | 80cabfad | bellard | #define UART_MSR_DCD 0x80 /* Data Carrier Detect */ |
56 | 80cabfad | bellard | #define UART_MSR_RI 0x40 /* Ring Indicator */ |
57 | 80cabfad | bellard | #define UART_MSR_DSR 0x20 /* Data Set Ready */ |
58 | 80cabfad | bellard | #define UART_MSR_CTS 0x10 /* Clear to Send */ |
59 | 80cabfad | bellard | #define UART_MSR_DDCD 0x08 /* Delta DCD */ |
60 | 80cabfad | bellard | #define UART_MSR_TERI 0x04 /* Trailing edge ring indicator */ |
61 | 80cabfad | bellard | #define UART_MSR_DDSR 0x02 /* Delta DSR */ |
62 | 80cabfad | bellard | #define UART_MSR_DCTS 0x01 /* Delta CTS */ |
63 | 80cabfad | bellard | #define UART_MSR_ANY_DELTA 0x0F /* Any of the delta bits! */ |
64 | 80cabfad | bellard | |
65 | 80cabfad | bellard | #define UART_LSR_TEMT 0x40 /* Transmitter empty */ |
66 | 80cabfad | bellard | #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */ |
67 | 80cabfad | bellard | #define UART_LSR_BI 0x10 /* Break interrupt indicator */ |
68 | 80cabfad | bellard | #define UART_LSR_FE 0x08 /* Frame error indicator */ |
69 | 80cabfad | bellard | #define UART_LSR_PE 0x04 /* Parity error indicator */ |
70 | 80cabfad | bellard | #define UART_LSR_OE 0x02 /* Overrun error indicator */ |
71 | 80cabfad | bellard | #define UART_LSR_DR 0x01 /* Receiver data ready */ |
72 | 80cabfad | bellard | |
73 | b41a2cd1 | bellard | struct SerialState {
|
74 | 80cabfad | bellard | uint8_t divider; |
75 | 80cabfad | bellard | uint8_t rbr; /* receive register */
|
76 | 80cabfad | bellard | uint8_t ier; |
77 | 80cabfad | bellard | uint8_t iir; /* read only */
|
78 | 80cabfad | bellard | uint8_t lcr; |
79 | 80cabfad | bellard | uint8_t mcr; |
80 | 80cabfad | bellard | uint8_t lsr; /* read only */
|
81 | 3e749fe1 | bellard | uint8_t msr; /* read only */
|
82 | 80cabfad | bellard | uint8_t scr; |
83 | 80cabfad | bellard | /* NOTE: this hidden state is necessary for tx irq generation as
|
84 | 80cabfad | bellard | it can be reset while reading iir */
|
85 | 80cabfad | bellard | int thr_ipending;
|
86 | e5d13e2f | bellard | SetIRQFunc *set_irq; |
87 | e5d13e2f | bellard | void *irq_opaque;
|
88 | 80cabfad | bellard | int irq;
|
89 | 82c643ff | bellard | CharDriverState *chr; |
90 | f8d179e3 | bellard | int last_break_enable;
|
91 | e5d13e2f | bellard | target_ulong base; |
92 | e5d13e2f | bellard | int it_shift;
|
93 | b41a2cd1 | bellard | }; |
94 | 80cabfad | bellard | |
95 | b41a2cd1 | bellard | static void serial_update_irq(SerialState *s) |
96 | 80cabfad | bellard | { |
97 | 80cabfad | bellard | if ((s->lsr & UART_LSR_DR) && (s->ier & UART_IER_RDI)) {
|
98 | 80cabfad | bellard | s->iir = UART_IIR_RDI; |
99 | 80cabfad | bellard | } else if (s->thr_ipending && (s->ier & UART_IER_THRI)) { |
100 | 80cabfad | bellard | s->iir = UART_IIR_THRI; |
101 | 80cabfad | bellard | } else {
|
102 | 80cabfad | bellard | s->iir = UART_IIR_NO_INT; |
103 | 80cabfad | bellard | } |
104 | 80cabfad | bellard | if (s->iir != UART_IIR_NO_INT) {
|
105 | e5d13e2f | bellard | s->set_irq(s->irq_opaque, s->irq, 1);
|
106 | 80cabfad | bellard | } else {
|
107 | e5d13e2f | bellard | s->set_irq(s->irq_opaque, s->irq, 0);
|
108 | 80cabfad | bellard | } |
109 | 80cabfad | bellard | } |
110 | 80cabfad | bellard | |
111 | f8d179e3 | bellard | static void serial_update_parameters(SerialState *s) |
112 | f8d179e3 | bellard | { |
113 | f8d179e3 | bellard | int speed, parity, data_bits, stop_bits;
|
114 | 2122c51a | bellard | QEMUSerialSetParams ssp; |
115 | f8d179e3 | bellard | |
116 | f8d179e3 | bellard | if (s->lcr & 0x08) { |
117 | f8d179e3 | bellard | if (s->lcr & 0x10) |
118 | f8d179e3 | bellard | parity = 'E';
|
119 | f8d179e3 | bellard | else
|
120 | f8d179e3 | bellard | parity = 'O';
|
121 | f8d179e3 | bellard | } else {
|
122 | f8d179e3 | bellard | parity = 'N';
|
123 | f8d179e3 | bellard | } |
124 | f8d179e3 | bellard | if (s->lcr & 0x04) |
125 | f8d179e3 | bellard | stop_bits = 2;
|
126 | f8d179e3 | bellard | else
|
127 | f8d179e3 | bellard | stop_bits = 1;
|
128 | f8d179e3 | bellard | data_bits = (s->lcr & 0x03) + 5; |
129 | f8d179e3 | bellard | if (s->divider == 0) |
130 | f8d179e3 | bellard | return;
|
131 | f8d179e3 | bellard | speed = 115200 / s->divider;
|
132 | 2122c51a | bellard | ssp.speed = speed; |
133 | 2122c51a | bellard | ssp.parity = parity; |
134 | 2122c51a | bellard | ssp.data_bits = data_bits; |
135 | 2122c51a | bellard | ssp.stop_bits = stop_bits; |
136 | 2122c51a | bellard | qemu_chr_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp); |
137 | 2122c51a | bellard | #if 0
|
138 | f8d179e3 | bellard | printf("speed=%d parity=%c data=%d stop=%d\n",
|
139 | f8d179e3 | bellard | speed, parity, data_bits, stop_bits);
|
140 | f8d179e3 | bellard | #endif
|
141 | f8d179e3 | bellard | } |
142 | f8d179e3 | bellard | |
143 | b41a2cd1 | bellard | static void serial_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
144 | 80cabfad | bellard | { |
145 | b41a2cd1 | bellard | SerialState *s = opaque; |
146 | 80cabfad | bellard | unsigned char ch; |
147 | 80cabfad | bellard | |
148 | 80cabfad | bellard | addr &= 7;
|
149 | 80cabfad | bellard | #ifdef DEBUG_SERIAL
|
150 | 80cabfad | bellard | printf("serial: write addr=0x%02x val=0x%02x\n", addr, val);
|
151 | 80cabfad | bellard | #endif
|
152 | 80cabfad | bellard | switch(addr) {
|
153 | 80cabfad | bellard | default:
|
154 | 80cabfad | bellard | case 0: |
155 | 80cabfad | bellard | if (s->lcr & UART_LCR_DLAB) {
|
156 | 80cabfad | bellard | s->divider = (s->divider & 0xff00) | val;
|
157 | f8d179e3 | bellard | serial_update_parameters(s); |
158 | 80cabfad | bellard | } else {
|
159 | 80cabfad | bellard | s->thr_ipending = 0;
|
160 | 80cabfad | bellard | s->lsr &= ~UART_LSR_THRE; |
161 | b41a2cd1 | bellard | serial_update_irq(s); |
162 | 82c643ff | bellard | ch = val; |
163 | 82c643ff | bellard | qemu_chr_write(s->chr, &ch, 1);
|
164 | 80cabfad | bellard | s->thr_ipending = 1;
|
165 | 80cabfad | bellard | s->lsr |= UART_LSR_THRE; |
166 | 80cabfad | bellard | s->lsr |= UART_LSR_TEMT; |
167 | b41a2cd1 | bellard | serial_update_irq(s); |
168 | 80cabfad | bellard | } |
169 | 80cabfad | bellard | break;
|
170 | 80cabfad | bellard | case 1: |
171 | 80cabfad | bellard | if (s->lcr & UART_LCR_DLAB) {
|
172 | 80cabfad | bellard | s->divider = (s->divider & 0x00ff) | (val << 8); |
173 | f8d179e3 | bellard | serial_update_parameters(s); |
174 | 80cabfad | bellard | } else {
|
175 | 60e336db | bellard | s->ier = val & 0x0f;
|
176 | 60e336db | bellard | if (s->lsr & UART_LSR_THRE) {
|
177 | 60e336db | bellard | s->thr_ipending = 1;
|
178 | 60e336db | bellard | } |
179 | b41a2cd1 | bellard | serial_update_irq(s); |
180 | 80cabfad | bellard | } |
181 | 80cabfad | bellard | break;
|
182 | 80cabfad | bellard | case 2: |
183 | 80cabfad | bellard | break;
|
184 | 80cabfad | bellard | case 3: |
185 | f8d179e3 | bellard | { |
186 | f8d179e3 | bellard | int break_enable;
|
187 | f8d179e3 | bellard | s->lcr = val; |
188 | f8d179e3 | bellard | serial_update_parameters(s); |
189 | f8d179e3 | bellard | break_enable = (val >> 6) & 1; |
190 | f8d179e3 | bellard | if (break_enable != s->last_break_enable) {
|
191 | f8d179e3 | bellard | s->last_break_enable = break_enable; |
192 | 2122c51a | bellard | qemu_chr_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_BREAK, |
193 | 2122c51a | bellard | &break_enable); |
194 | f8d179e3 | bellard | } |
195 | f8d179e3 | bellard | } |
196 | 80cabfad | bellard | break;
|
197 | 80cabfad | bellard | case 4: |
198 | 60e336db | bellard | s->mcr = val & 0x1f;
|
199 | 80cabfad | bellard | break;
|
200 | 80cabfad | bellard | case 5: |
201 | 80cabfad | bellard | break;
|
202 | 80cabfad | bellard | case 6: |
203 | 80cabfad | bellard | break;
|
204 | 80cabfad | bellard | case 7: |
205 | 80cabfad | bellard | s->scr = val; |
206 | 80cabfad | bellard | break;
|
207 | 80cabfad | bellard | } |
208 | 80cabfad | bellard | } |
209 | 80cabfad | bellard | |
210 | b41a2cd1 | bellard | static uint32_t serial_ioport_read(void *opaque, uint32_t addr) |
211 | 80cabfad | bellard | { |
212 | b41a2cd1 | bellard | SerialState *s = opaque; |
213 | 80cabfad | bellard | uint32_t ret; |
214 | 80cabfad | bellard | |
215 | 80cabfad | bellard | addr &= 7;
|
216 | 80cabfad | bellard | switch(addr) {
|
217 | 80cabfad | bellard | default:
|
218 | 80cabfad | bellard | case 0: |
219 | 80cabfad | bellard | if (s->lcr & UART_LCR_DLAB) {
|
220 | 80cabfad | bellard | ret = s->divider & 0xff;
|
221 | 80cabfad | bellard | } else {
|
222 | 80cabfad | bellard | ret = s->rbr; |
223 | 80cabfad | bellard | s->lsr &= ~(UART_LSR_DR | UART_LSR_BI); |
224 | b41a2cd1 | bellard | serial_update_irq(s); |
225 | 80cabfad | bellard | } |
226 | 80cabfad | bellard | break;
|
227 | 80cabfad | bellard | case 1: |
228 | 80cabfad | bellard | if (s->lcr & UART_LCR_DLAB) {
|
229 | 80cabfad | bellard | ret = (s->divider >> 8) & 0xff; |
230 | 80cabfad | bellard | } else {
|
231 | 80cabfad | bellard | ret = s->ier; |
232 | 80cabfad | bellard | } |
233 | 80cabfad | bellard | break;
|
234 | 80cabfad | bellard | case 2: |
235 | 80cabfad | bellard | ret = s->iir; |
236 | 80cabfad | bellard | /* reset THR pending bit */
|
237 | 80cabfad | bellard | if ((ret & 0x7) == UART_IIR_THRI) |
238 | 80cabfad | bellard | s->thr_ipending = 0;
|
239 | b41a2cd1 | bellard | serial_update_irq(s); |
240 | 80cabfad | bellard | break;
|
241 | 80cabfad | bellard | case 3: |
242 | 80cabfad | bellard | ret = s->lcr; |
243 | 80cabfad | bellard | break;
|
244 | 80cabfad | bellard | case 4: |
245 | 80cabfad | bellard | ret = s->mcr; |
246 | 80cabfad | bellard | break;
|
247 | 80cabfad | bellard | case 5: |
248 | 80cabfad | bellard | ret = s->lsr; |
249 | 80cabfad | bellard | break;
|
250 | 80cabfad | bellard | case 6: |
251 | 80cabfad | bellard | if (s->mcr & UART_MCR_LOOP) {
|
252 | 80cabfad | bellard | /* in loopback, the modem output pins are connected to the
|
253 | 80cabfad | bellard | inputs */
|
254 | 80cabfad | bellard | ret = (s->mcr & 0x0c) << 4; |
255 | 80cabfad | bellard | ret |= (s->mcr & 0x02) << 3; |
256 | 80cabfad | bellard | ret |= (s->mcr & 0x01) << 5; |
257 | 80cabfad | bellard | } else {
|
258 | 80cabfad | bellard | ret = s->msr; |
259 | 80cabfad | bellard | } |
260 | 80cabfad | bellard | break;
|
261 | 80cabfad | bellard | case 7: |
262 | 80cabfad | bellard | ret = s->scr; |
263 | 80cabfad | bellard | break;
|
264 | 80cabfad | bellard | } |
265 | 80cabfad | bellard | #ifdef DEBUG_SERIAL
|
266 | 80cabfad | bellard | printf("serial: read addr=0x%02x val=0x%02x\n", addr, ret);
|
267 | 80cabfad | bellard | #endif
|
268 | 80cabfad | bellard | return ret;
|
269 | 80cabfad | bellard | } |
270 | 80cabfad | bellard | |
271 | 82c643ff | bellard | static int serial_can_receive(SerialState *s) |
272 | 80cabfad | bellard | { |
273 | 80cabfad | bellard | return !(s->lsr & UART_LSR_DR);
|
274 | 80cabfad | bellard | } |
275 | 80cabfad | bellard | |
276 | 82c643ff | bellard | static void serial_receive_byte(SerialState *s, int ch) |
277 | 80cabfad | bellard | { |
278 | 80cabfad | bellard | s->rbr = ch; |
279 | 80cabfad | bellard | s->lsr |= UART_LSR_DR; |
280 | b41a2cd1 | bellard | serial_update_irq(s); |
281 | 80cabfad | bellard | } |
282 | 80cabfad | bellard | |
283 | 82c643ff | bellard | static void serial_receive_break(SerialState *s) |
284 | 80cabfad | bellard | { |
285 | 80cabfad | bellard | s->rbr = 0;
|
286 | 80cabfad | bellard | s->lsr |= UART_LSR_BI | UART_LSR_DR; |
287 | b41a2cd1 | bellard | serial_update_irq(s); |
288 | 80cabfad | bellard | } |
289 | 80cabfad | bellard | |
290 | b41a2cd1 | bellard | static int serial_can_receive1(void *opaque) |
291 | 80cabfad | bellard | { |
292 | b41a2cd1 | bellard | SerialState *s = opaque; |
293 | b41a2cd1 | bellard | return serial_can_receive(s);
|
294 | b41a2cd1 | bellard | } |
295 | b41a2cd1 | bellard | |
296 | b41a2cd1 | bellard | static void serial_receive1(void *opaque, const uint8_t *buf, int size) |
297 | b41a2cd1 | bellard | { |
298 | b41a2cd1 | bellard | SerialState *s = opaque; |
299 | b41a2cd1 | bellard | serial_receive_byte(s, buf[0]);
|
300 | b41a2cd1 | bellard | } |
301 | 80cabfad | bellard | |
302 | 82c643ff | bellard | static void serial_event(void *opaque, int event) |
303 | 82c643ff | bellard | { |
304 | 82c643ff | bellard | SerialState *s = opaque; |
305 | 82c643ff | bellard | if (event == CHR_EVENT_BREAK)
|
306 | 82c643ff | bellard | serial_receive_break(s); |
307 | 82c643ff | bellard | } |
308 | 82c643ff | bellard | |
309 | 8738a8d0 | bellard | static void serial_save(QEMUFile *f, void *opaque) |
310 | 8738a8d0 | bellard | { |
311 | 8738a8d0 | bellard | SerialState *s = opaque; |
312 | 8738a8d0 | bellard | |
313 | 8738a8d0 | bellard | qemu_put_8s(f,&s->divider); |
314 | 8738a8d0 | bellard | qemu_put_8s(f,&s->rbr); |
315 | 8738a8d0 | bellard | qemu_put_8s(f,&s->ier); |
316 | 8738a8d0 | bellard | qemu_put_8s(f,&s->iir); |
317 | 8738a8d0 | bellard | qemu_put_8s(f,&s->lcr); |
318 | 8738a8d0 | bellard | qemu_put_8s(f,&s->mcr); |
319 | 8738a8d0 | bellard | qemu_put_8s(f,&s->lsr); |
320 | 8738a8d0 | bellard | qemu_put_8s(f,&s->msr); |
321 | 8738a8d0 | bellard | qemu_put_8s(f,&s->scr); |
322 | 8738a8d0 | bellard | } |
323 | 8738a8d0 | bellard | |
324 | 8738a8d0 | bellard | static int serial_load(QEMUFile *f, void *opaque, int version_id) |
325 | 8738a8d0 | bellard | { |
326 | 8738a8d0 | bellard | SerialState *s = opaque; |
327 | 8738a8d0 | bellard | |
328 | 8738a8d0 | bellard | if(version_id != 1) |
329 | 8738a8d0 | bellard | return -EINVAL;
|
330 | 8738a8d0 | bellard | |
331 | 8738a8d0 | bellard | qemu_get_8s(f,&s->divider); |
332 | 8738a8d0 | bellard | qemu_get_8s(f,&s->rbr); |
333 | 8738a8d0 | bellard | qemu_get_8s(f,&s->ier); |
334 | 8738a8d0 | bellard | qemu_get_8s(f,&s->iir); |
335 | 8738a8d0 | bellard | qemu_get_8s(f,&s->lcr); |
336 | 8738a8d0 | bellard | qemu_get_8s(f,&s->mcr); |
337 | 8738a8d0 | bellard | qemu_get_8s(f,&s->lsr); |
338 | 8738a8d0 | bellard | qemu_get_8s(f,&s->msr); |
339 | 8738a8d0 | bellard | qemu_get_8s(f,&s->scr); |
340 | 8738a8d0 | bellard | |
341 | 8738a8d0 | bellard | return 0; |
342 | 8738a8d0 | bellard | } |
343 | 8738a8d0 | bellard | |
344 | b41a2cd1 | bellard | /* If fd is zero, it means that the serial device uses the console */
|
345 | e5d13e2f | bellard | SerialState *serial_init(SetIRQFunc *set_irq, void *opaque,
|
346 | e5d13e2f | bellard | int base, int irq, CharDriverState *chr) |
347 | b41a2cd1 | bellard | { |
348 | b41a2cd1 | bellard | SerialState *s; |
349 | b41a2cd1 | bellard | |
350 | b41a2cd1 | bellard | s = qemu_mallocz(sizeof(SerialState));
|
351 | b41a2cd1 | bellard | if (!s)
|
352 | b41a2cd1 | bellard | return NULL; |
353 | e5d13e2f | bellard | s->set_irq = set_irq; |
354 | e5d13e2f | bellard | s->irq_opaque = opaque; |
355 | 80cabfad | bellard | s->irq = irq; |
356 | 80cabfad | bellard | s->lsr = UART_LSR_TEMT | UART_LSR_THRE; |
357 | 80cabfad | bellard | s->iir = UART_IIR_NO_INT; |
358 | 3e749fe1 | bellard | s->msr = UART_MSR_DCD | UART_MSR_DSR | UART_MSR_CTS; |
359 | b41a2cd1 | bellard | |
360 | 8738a8d0 | bellard | register_savevm("serial", base, 1, serial_save, serial_load, s); |
361 | 8738a8d0 | bellard | |
362 | b41a2cd1 | bellard | register_ioport_write(base, 8, 1, serial_ioport_write, s); |
363 | b41a2cd1 | bellard | register_ioport_read(base, 8, 1, serial_ioport_read, s); |
364 | 82c643ff | bellard | s->chr = chr; |
365 | 82c643ff | bellard | qemu_chr_add_read_handler(chr, serial_can_receive1, serial_receive1, s); |
366 | 82c643ff | bellard | qemu_chr_add_event_handler(chr, serial_event); |
367 | b41a2cd1 | bellard | return s;
|
368 | 80cabfad | bellard | } |
369 | e5d13e2f | bellard | |
370 | e5d13e2f | bellard | /* Memory mapped interface */
|
371 | e5d13e2f | bellard | static uint32_t serial_mm_readb (void *opaque, target_phys_addr_t addr) |
372 | e5d13e2f | bellard | { |
373 | e5d13e2f | bellard | SerialState *s = opaque; |
374 | e5d13e2f | bellard | |
375 | e5d13e2f | bellard | return serial_ioport_read(s, (addr - s->base) >> s->it_shift) & 0xFF; |
376 | e5d13e2f | bellard | } |
377 | e5d13e2f | bellard | |
378 | e5d13e2f | bellard | static void serial_mm_writeb (void *opaque, |
379 | e5d13e2f | bellard | target_phys_addr_t addr, uint32_t value) |
380 | e5d13e2f | bellard | { |
381 | e5d13e2f | bellard | SerialState *s = opaque; |
382 | e5d13e2f | bellard | |
383 | e5d13e2f | bellard | serial_ioport_write(s, (addr - s->base) >> s->it_shift, value & 0xFF);
|
384 | e5d13e2f | bellard | } |
385 | e5d13e2f | bellard | |
386 | e5d13e2f | bellard | static uint32_t serial_mm_readw (void *opaque, target_phys_addr_t addr) |
387 | e5d13e2f | bellard | { |
388 | e5d13e2f | bellard | SerialState *s = opaque; |
389 | e5d13e2f | bellard | |
390 | e5d13e2f | bellard | return serial_ioport_read(s, (addr - s->base) >> s->it_shift) & 0xFFFF; |
391 | e5d13e2f | bellard | } |
392 | e5d13e2f | bellard | |
393 | e5d13e2f | bellard | static void serial_mm_writew (void *opaque, |
394 | e5d13e2f | bellard | target_phys_addr_t addr, uint32_t value) |
395 | e5d13e2f | bellard | { |
396 | e5d13e2f | bellard | SerialState *s = opaque; |
397 | e5d13e2f | bellard | |
398 | e5d13e2f | bellard | serial_ioport_write(s, (addr - s->base) >> s->it_shift, value & 0xFFFF);
|
399 | e5d13e2f | bellard | } |
400 | e5d13e2f | bellard | |
401 | e5d13e2f | bellard | static uint32_t serial_mm_readl (void *opaque, target_phys_addr_t addr) |
402 | e5d13e2f | bellard | { |
403 | e5d13e2f | bellard | SerialState *s = opaque; |
404 | e5d13e2f | bellard | |
405 | e5d13e2f | bellard | return serial_ioport_read(s, (addr - s->base) >> s->it_shift);
|
406 | e5d13e2f | bellard | } |
407 | e5d13e2f | bellard | |
408 | e5d13e2f | bellard | static void serial_mm_writel (void *opaque, |
409 | e5d13e2f | bellard | target_phys_addr_t addr, uint32_t value) |
410 | e5d13e2f | bellard | { |
411 | e5d13e2f | bellard | SerialState *s = opaque; |
412 | e5d13e2f | bellard | |
413 | e5d13e2f | bellard | serial_ioport_write(s, (addr - s->base) >> s->it_shift, value); |
414 | e5d13e2f | bellard | } |
415 | e5d13e2f | bellard | |
416 | e5d13e2f | bellard | static CPUReadMemoryFunc *serial_mm_read[] = {
|
417 | e5d13e2f | bellard | &serial_mm_readb, |
418 | e5d13e2f | bellard | &serial_mm_readw, |
419 | e5d13e2f | bellard | &serial_mm_readl, |
420 | e5d13e2f | bellard | }; |
421 | e5d13e2f | bellard | |
422 | e5d13e2f | bellard | static CPUWriteMemoryFunc *serial_mm_write[] = {
|
423 | e5d13e2f | bellard | &serial_mm_writeb, |
424 | e5d13e2f | bellard | &serial_mm_writew, |
425 | e5d13e2f | bellard | &serial_mm_writel, |
426 | e5d13e2f | bellard | }; |
427 | e5d13e2f | bellard | |
428 | e5d13e2f | bellard | SerialState *serial_mm_init (SetIRQFunc *set_irq, void *opaque,
|
429 | e5d13e2f | bellard | target_ulong base, int it_shift,
|
430 | e5d13e2f | bellard | int irq, CharDriverState *chr)
|
431 | e5d13e2f | bellard | { |
432 | e5d13e2f | bellard | SerialState *s; |
433 | e5d13e2f | bellard | int s_io_memory;
|
434 | e5d13e2f | bellard | |
435 | e5d13e2f | bellard | s = qemu_mallocz(sizeof(SerialState));
|
436 | e5d13e2f | bellard | if (!s)
|
437 | e5d13e2f | bellard | return NULL; |
438 | e5d13e2f | bellard | s->set_irq = set_irq; |
439 | e5d13e2f | bellard | s->irq_opaque = opaque; |
440 | e5d13e2f | bellard | s->irq = irq; |
441 | e5d13e2f | bellard | s->lsr = UART_LSR_TEMT | UART_LSR_THRE; |
442 | e5d13e2f | bellard | s->iir = UART_IIR_NO_INT; |
443 | 3e749fe1 | bellard | s->msr = UART_MSR_DCD | UART_MSR_DSR | UART_MSR_CTS; |
444 | e5d13e2f | bellard | s->base = base; |
445 | e5d13e2f | bellard | s->it_shift = it_shift; |
446 | e5d13e2f | bellard | |
447 | e5d13e2f | bellard | register_savevm("serial", base, 1, serial_save, serial_load, s); |
448 | e5d13e2f | bellard | |
449 | e5d13e2f | bellard | s_io_memory = cpu_register_io_memory(0, serial_mm_read,
|
450 | e5d13e2f | bellard | serial_mm_write, s); |
451 | e5d13e2f | bellard | cpu_register_physical_memory(base, 8 << it_shift, s_io_memory);
|
452 | e5d13e2f | bellard | s->chr = chr; |
453 | e5d13e2f | bellard | qemu_chr_add_read_handler(chr, serial_can_receive1, serial_receive1, s); |
454 | e5d13e2f | bellard | qemu_chr_add_event_handler(chr, serial_event); |
455 | e5d13e2f | bellard | return s;
|
456 | e5d13e2f | bellard | } |