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