4 * Copyright (c) 2008 OKL
5 * Originally Written by Hans Jiang
6 * Copyright (c) 2011 NICTA Pty Ltd.
7 * Updated by Jean-Christophe Dubois <jcd@tribudubois.net>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
12 * This is a `bare-bones' implementation of the IMX series serial ports.
14 * -- implement FIFOs. The real hardware has 32 word transmit
15 * and receive FIFOs; we currently use a 1-char buffer
17 * -- implement BAUD-rate and modem lines, for when the backend
18 * is a real serial device.
21 #include "qemu/osdep.h"
22 #include "hw/char/imx_serial.h"
24 #include "hw/qdev-properties.h"
25 #include "hw/qdev-properties-system.h"
26 #include "migration/vmstate.h"
28 #include "qemu/module.h"
30 #ifndef DEBUG_IMX_UART
31 #define DEBUG_IMX_UART 0
34 #define DPRINTF(fmt, args...) \
36 if (DEBUG_IMX_UART) { \
37 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_SERIAL, \
42 static const VMStateDescription vmstate_imx_serial
= {
43 .name
= TYPE_IMX_SERIAL
,
45 .minimum_version_id
= 2,
46 .fields
= (VMStateField
[]) {
47 VMSTATE_INT32(readbuff
, IMXSerialState
),
48 VMSTATE_UINT32(usr1
, IMXSerialState
),
49 VMSTATE_UINT32(usr2
, IMXSerialState
),
50 VMSTATE_UINT32(ucr1
, IMXSerialState
),
51 VMSTATE_UINT32(uts1
, IMXSerialState
),
52 VMSTATE_UINT32(onems
, IMXSerialState
),
53 VMSTATE_UINT32(ufcr
, IMXSerialState
),
54 VMSTATE_UINT32(ubmr
, IMXSerialState
),
55 VMSTATE_UINT32(ubrc
, IMXSerialState
),
56 VMSTATE_UINT32(ucr3
, IMXSerialState
),
57 VMSTATE_UINT32(ucr4
, IMXSerialState
),
62 static void imx_update(IMXSerialState
*s
)
69 * Lucky for us TRDY and RRDY has the same offset in both USR1 and
70 * UCR1, so we can get away with something as simple as the
73 usr1
= s
->usr1
& s
->ucr1
& (USR1_TRDY
| USR1_RRDY
);
75 * Bits that we want in USR2 are not as conveniently laid out,
78 mask
= (s
->ucr1
& UCR1_TXMPTYEN
) ? USR2_TXFE
: 0;
80 * TCEN and TXDC are both bit 3
81 * RDR and DREN are both bit 0
83 mask
|= s
->ucr4
& (UCR4_TCEN
| UCR4_DREN
);
85 usr2
= s
->usr2
& mask
;
87 qemu_set_irq(s
->irq
, usr1
|| usr2
);
90 static void imx_serial_reset(IMXSerialState
*s
)
93 s
->usr1
= USR1_TRDY
| USR1_RXDS
;
95 * Fake attachment of a terminal: assert RTS.
98 s
->usr2
= USR2_TXFE
| USR2_TXDC
| USR2_DCDIN
;
99 s
->uts1
= UTS1_RXEMPTY
| UTS1_TXEMPTY
;
105 s
->readbuff
= URXD_ERR
;
108 static void imx_serial_reset_at_boot(DeviceState
*dev
)
110 IMXSerialState
*s
= IMX_SERIAL(dev
);
115 * enable the uart on boot, so messages from the linux decompresser
116 * are visible. On real hardware this is done by the boot rom
117 * before anything else is loaded.
119 s
->ucr1
= UCR1_UARTEN
;
124 static uint64_t imx_serial_read(void *opaque
, hwaddr offset
,
127 IMXSerialState
*s
= (IMXSerialState
*)opaque
;
130 DPRINTF("read(offset=0x%" HWADDR_PRIx
")\n", offset
);
132 switch (offset
>> 2) {
135 if (!(s
->uts1
& UTS1_RXEMPTY
)) {
136 /* Character is valid */
138 s
->usr1
&= ~USR1_RRDY
;
139 s
->usr2
&= ~USR2_RDR
;
140 s
->uts1
|= UTS1_RXEMPTY
;
142 qemu_chr_fe_accept_input(&s
->chr
);
146 case 0x20: /* UCR1 */
149 case 0x21: /* UCR2 */
152 case 0x25: /* USR1 */
155 case 0x26: /* USR2 */
158 case 0x2A: /* BRM Modulator */
161 case 0x2B: /* Baud Rate Count */
164 case 0x2d: /* Test register */
167 case 0x24: /* UFCR */
173 case 0x22: /* UCR3 */
176 case 0x23: /* UCR4 */
179 case 0x29: /* BRM Incremental */
180 return 0x0; /* TODO */
183 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
184 HWADDR_PRIx
"\n", TYPE_IMX_SERIAL
, __func__
, offset
);
189 static void imx_serial_write(void *opaque
, hwaddr offset
,
190 uint64_t value
, unsigned size
)
192 IMXSerialState
*s
= (IMXSerialState
*)opaque
;
193 Chardev
*chr
= qemu_chr_fe_get_driver(&s
->chr
);
196 DPRINTF("write(offset=0x%" HWADDR_PRIx
", value = 0x%x) to %s\n",
197 offset
, (unsigned int)value
, chr
? chr
->label
: "NODEV");
199 switch (offset
>> 2) {
200 case 0x10: /* UTXD */
202 if (s
->ucr2
& UCR2_TXEN
) {
203 /* XXX this blocks entire thread. Rewrite to use
204 * qemu_chr_fe_write and background I/O callbacks */
205 qemu_chr_fe_write_all(&s
->chr
, &ch
, 1);
206 s
->usr1
&= ~USR1_TRDY
;
207 s
->usr2
&= ~USR2_TXDC
;
209 s
->usr1
|= USR1_TRDY
;
210 s
->usr2
|= USR2_TXDC
;
215 case 0x20: /* UCR1 */
216 s
->ucr1
= value
& 0xffff;
218 DPRINTF("write(ucr1=%x)\n", (unsigned int)value
);
223 case 0x21: /* UCR2 */
225 * Only a few bits in control register 2 are implemented as yet.
226 * If it's intended to use a real serial device as a back-end, this
227 * register will have to be implemented more fully.
229 if (!(value
& UCR2_SRST
)) {
234 if (value
& UCR2_RXEN
) {
235 if (!(s
->ucr2
& UCR2_RXEN
)) {
236 qemu_chr_fe_accept_input(&s
->chr
);
239 s
->ucr2
= value
& 0xffff;
242 case 0x25: /* USR1 */
243 value
&= USR1_AWAKE
| USR1_AIRINT
| USR1_DTRD
| USR1_AGTIM
|
244 USR1_FRAMERR
| USR1_ESCF
| USR1_RTSD
| USR1_PARTYER
;
248 case 0x26: /* USR2 */
250 * Writing 1 to some bits clears them; all other
253 value
&= USR2_ADET
| USR2_DTRF
| USR2_IDLE
| USR2_ACST
|
254 USR2_RIDELT
| USR2_IRINT
| USR2_WAKE
|
255 USR2_DCDDELT
| USR2_RTSF
| USR2_BRCD
| USR2_ORE
;
260 * Linux expects to see what it writes to these registers
261 * We don't currently alter the baud rate
263 case 0x29: /* UBIR */
264 s
->ubrc
= value
& 0xffff;
267 case 0x2a: /* UBMR */
268 s
->ubmr
= value
& 0xffff;
271 case 0x2c: /* One ms reg */
272 s
->onems
= value
& 0xffff;
275 case 0x24: /* FIFO control register */
276 s
->ufcr
= value
& 0xffff;
279 case 0x22: /* UCR3 */
280 s
->ucr3
= value
& 0xffff;
283 case 0x23: /* UCR4 */
284 s
->ucr4
= value
& 0xffff;
288 case 0x2d: /* UTS1 */
289 qemu_log_mask(LOG_UNIMP
, "[%s]%s: Unimplemented reg 0x%"
290 HWADDR_PRIx
"\n", TYPE_IMX_SERIAL
, __func__
, offset
);
295 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
296 HWADDR_PRIx
"\n", TYPE_IMX_SERIAL
, __func__
, offset
);
300 static int imx_can_receive(void *opaque
)
302 IMXSerialState
*s
= (IMXSerialState
*)opaque
;
303 return !(s
->usr1
& USR1_RRDY
);
306 static void imx_put_data(void *opaque
, uint32_t value
)
308 IMXSerialState
*s
= (IMXSerialState
*)opaque
;
310 DPRINTF("received char\n");
312 s
->usr1
|= USR1_RRDY
;
314 s
->uts1
&= ~UTS1_RXEMPTY
;
316 if (value
& URXD_BRK
) {
317 s
->usr2
|= USR2_BRCD
;
322 static void imx_receive(void *opaque
, const uint8_t *buf
, int size
)
324 imx_put_data(opaque
, *buf
);
327 static void imx_event(void *opaque
, QEMUChrEvent event
)
329 if (event
== CHR_EVENT_BREAK
) {
330 imx_put_data(opaque
, URXD_BRK
| URXD_FRMERR
| URXD_ERR
);
335 static const struct MemoryRegionOps imx_serial_ops
= {
336 .read
= imx_serial_read
,
337 .write
= imx_serial_write
,
338 .endianness
= DEVICE_NATIVE_ENDIAN
,
341 static void imx_serial_realize(DeviceState
*dev
, Error
**errp
)
343 IMXSerialState
*s
= IMX_SERIAL(dev
);
345 DPRINTF("char dev for uart: %p\n", qemu_chr_fe_get_driver(&s
->chr
));
347 qemu_chr_fe_set_handlers(&s
->chr
, imx_can_receive
, imx_receive
,
348 imx_event
, NULL
, s
, NULL
, true);
351 static void imx_serial_init(Object
*obj
)
353 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
354 IMXSerialState
*s
= IMX_SERIAL(obj
);
356 memory_region_init_io(&s
->iomem
, obj
, &imx_serial_ops
, s
,
357 TYPE_IMX_SERIAL
, 0x1000);
358 sysbus_init_mmio(sbd
, &s
->iomem
);
359 sysbus_init_irq(sbd
, &s
->irq
);
362 static Property imx_serial_properties
[] = {
363 DEFINE_PROP_CHR("chardev", IMXSerialState
, chr
),
364 DEFINE_PROP_END_OF_LIST(),
367 static void imx_serial_class_init(ObjectClass
*klass
, void *data
)
369 DeviceClass
*dc
= DEVICE_CLASS(klass
);
371 dc
->realize
= imx_serial_realize
;
372 dc
->vmsd
= &vmstate_imx_serial
;
373 dc
->reset
= imx_serial_reset_at_boot
;
374 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
375 dc
->desc
= "i.MX series UART";
376 device_class_set_props(dc
, imx_serial_properties
);
379 static const TypeInfo imx_serial_info
= {
380 .name
= TYPE_IMX_SERIAL
,
381 .parent
= TYPE_SYS_BUS_DEVICE
,
382 .instance_size
= sizeof(IMXSerialState
),
383 .instance_init
= imx_serial_init
,
384 .class_init
= imx_serial_class_init
,
387 static void imx_serial_register_types(void)
389 type_register_static(&imx_serial_info
);
392 type_init(imx_serial_register_types
)