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 "migration/vmstate.h"
27 #include "qemu/module.h"
29 #ifndef DEBUG_IMX_UART
30 #define DEBUG_IMX_UART 0
33 #define DPRINTF(fmt, args...) \
35 if (DEBUG_IMX_UART) { \
36 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_SERIAL, \
41 static const VMStateDescription vmstate_imx_serial
= {
42 .name
= TYPE_IMX_SERIAL
,
44 .minimum_version_id
= 2,
45 .fields
= (VMStateField
[]) {
46 VMSTATE_INT32(readbuff
, IMXSerialState
),
47 VMSTATE_UINT32(usr1
, IMXSerialState
),
48 VMSTATE_UINT32(usr2
, IMXSerialState
),
49 VMSTATE_UINT32(ucr1
, IMXSerialState
),
50 VMSTATE_UINT32(uts1
, IMXSerialState
),
51 VMSTATE_UINT32(onems
, IMXSerialState
),
52 VMSTATE_UINT32(ufcr
, IMXSerialState
),
53 VMSTATE_UINT32(ubmr
, IMXSerialState
),
54 VMSTATE_UINT32(ubrc
, IMXSerialState
),
55 VMSTATE_UINT32(ucr3
, IMXSerialState
),
56 VMSTATE_UINT32(ucr4
, IMXSerialState
),
61 static void imx_update(IMXSerialState
*s
)
68 * Lucky for us TRDY and RRDY has the same offset in both USR1 and
69 * UCR1, so we can get away with something as simple as the
72 usr1
= s
->usr1
& s
->ucr1
& (USR1_TRDY
| USR1_RRDY
);
74 * Bits that we want in USR2 are not as conveniently laid out,
77 mask
= (s
->ucr1
& UCR1_TXMPTYEN
) ? USR2_TXFE
: 0;
79 * TCEN and TXDC are both bit 3
80 * RDR and DREN are both bit 0
82 mask
|= s
->ucr4
& (UCR4_TCEN
| UCR4_DREN
);
84 usr2
= s
->usr2
& mask
;
86 qemu_set_irq(s
->irq
, usr1
|| usr2
);
89 static void imx_serial_reset(IMXSerialState
*s
)
92 s
->usr1
= USR1_TRDY
| USR1_RXDS
;
94 * Fake attachment of a terminal: assert RTS.
97 s
->usr2
= USR2_TXFE
| USR2_TXDC
| USR2_DCDIN
;
98 s
->uts1
= UTS1_RXEMPTY
| UTS1_TXEMPTY
;
104 s
->readbuff
= URXD_ERR
;
107 static void imx_serial_reset_at_boot(DeviceState
*dev
)
109 IMXSerialState
*s
= IMX_SERIAL(dev
);
114 * enable the uart on boot, so messages from the linux decompresser
115 * are visible. On real hardware this is done by the boot rom
116 * before anything else is loaded.
118 s
->ucr1
= UCR1_UARTEN
;
123 static uint64_t imx_serial_read(void *opaque
, hwaddr offset
,
126 IMXSerialState
*s
= (IMXSerialState
*)opaque
;
129 DPRINTF("read(offset=0x%" HWADDR_PRIx
")\n", offset
);
131 switch (offset
>> 2) {
134 if (!(s
->uts1
& UTS1_RXEMPTY
)) {
135 /* Character is valid */
137 s
->usr1
&= ~USR1_RRDY
;
138 s
->usr2
&= ~USR2_RDR
;
139 s
->uts1
|= UTS1_RXEMPTY
;
141 qemu_chr_fe_accept_input(&s
->chr
);
145 case 0x20: /* UCR1 */
148 case 0x21: /* UCR2 */
151 case 0x25: /* USR1 */
154 case 0x26: /* USR2 */
157 case 0x2A: /* BRM Modulator */
160 case 0x2B: /* Baud Rate Count */
163 case 0x2d: /* Test register */
166 case 0x24: /* UFCR */
172 case 0x22: /* UCR3 */
175 case 0x23: /* UCR4 */
178 case 0x29: /* BRM Incremental */
179 return 0x0; /* TODO */
182 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
183 HWADDR_PRIx
"\n", TYPE_IMX_SERIAL
, __func__
, offset
);
188 static void imx_serial_write(void *opaque
, hwaddr offset
,
189 uint64_t value
, unsigned size
)
191 IMXSerialState
*s
= (IMXSerialState
*)opaque
;
192 Chardev
*chr
= qemu_chr_fe_get_driver(&s
->chr
);
195 DPRINTF("write(offset=0x%" HWADDR_PRIx
", value = 0x%x) to %s\n",
196 offset
, (unsigned int)value
, chr
? chr
->label
: "NODEV");
198 switch (offset
>> 2) {
199 case 0x10: /* UTXD */
201 if (s
->ucr2
& UCR2_TXEN
) {
202 /* XXX this blocks entire thread. Rewrite to use
203 * qemu_chr_fe_write and background I/O callbacks */
204 qemu_chr_fe_write_all(&s
->chr
, &ch
, 1);
205 s
->usr1
&= ~USR1_TRDY
;
206 s
->usr2
&= ~USR2_TXDC
;
208 s
->usr1
|= USR1_TRDY
;
209 s
->usr2
|= USR2_TXDC
;
214 case 0x20: /* UCR1 */
215 s
->ucr1
= value
& 0xffff;
217 DPRINTF("write(ucr1=%x)\n", (unsigned int)value
);
222 case 0x21: /* UCR2 */
224 * Only a few bits in control register 2 are implemented as yet.
225 * If it's intended to use a real serial device as a back-end, this
226 * register will have to be implemented more fully.
228 if (!(value
& UCR2_SRST
)) {
233 if (value
& UCR2_RXEN
) {
234 if (!(s
->ucr2
& UCR2_RXEN
)) {
235 qemu_chr_fe_accept_input(&s
->chr
);
238 s
->ucr2
= value
& 0xffff;
241 case 0x25: /* USR1 */
242 value
&= USR1_AWAKE
| USR1_AIRINT
| USR1_DTRD
| USR1_AGTIM
|
243 USR1_FRAMERR
| USR1_ESCF
| USR1_RTSD
| USR1_PARTYER
;
247 case 0x26: /* USR2 */
249 * Writing 1 to some bits clears them; all other
252 value
&= USR2_ADET
| USR2_DTRF
| USR2_IDLE
| USR2_ACST
|
253 USR2_RIDELT
| USR2_IRINT
| USR2_WAKE
|
254 USR2_DCDDELT
| USR2_RTSF
| USR2_BRCD
| USR2_ORE
;
259 * Linux expects to see what it writes to these registers
260 * We don't currently alter the baud rate
262 case 0x29: /* UBIR */
263 s
->ubrc
= value
& 0xffff;
266 case 0x2a: /* UBMR */
267 s
->ubmr
= value
& 0xffff;
270 case 0x2c: /* One ms reg */
271 s
->onems
= value
& 0xffff;
274 case 0x24: /* FIFO control register */
275 s
->ufcr
= value
& 0xffff;
278 case 0x22: /* UCR3 */
279 s
->ucr3
= value
& 0xffff;
282 case 0x23: /* UCR4 */
283 s
->ucr4
= value
& 0xffff;
287 case 0x2d: /* UTS1 */
288 qemu_log_mask(LOG_UNIMP
, "[%s]%s: Unimplemented reg 0x%"
289 HWADDR_PRIx
"\n", TYPE_IMX_SERIAL
, __func__
, offset
);
294 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
295 HWADDR_PRIx
"\n", TYPE_IMX_SERIAL
, __func__
, offset
);
299 static int imx_can_receive(void *opaque
)
301 IMXSerialState
*s
= (IMXSerialState
*)opaque
;
302 return !(s
->usr1
& USR1_RRDY
);
305 static void imx_put_data(void *opaque
, uint32_t value
)
307 IMXSerialState
*s
= (IMXSerialState
*)opaque
;
309 DPRINTF("received char\n");
311 s
->usr1
|= USR1_RRDY
;
313 s
->uts1
&= ~UTS1_RXEMPTY
;
315 if (value
& URXD_BRK
) {
316 s
->usr2
|= USR2_BRCD
;
321 static void imx_receive(void *opaque
, const uint8_t *buf
, int size
)
323 imx_put_data(opaque
, *buf
);
326 static void imx_event(void *opaque
, QEMUChrEvent event
)
328 if (event
== CHR_EVENT_BREAK
) {
329 imx_put_data(opaque
, URXD_BRK
| URXD_FRMERR
| URXD_ERR
);
334 static const struct MemoryRegionOps imx_serial_ops
= {
335 .read
= imx_serial_read
,
336 .write
= imx_serial_write
,
337 .endianness
= DEVICE_NATIVE_ENDIAN
,
340 static void imx_serial_realize(DeviceState
*dev
, Error
**errp
)
342 IMXSerialState
*s
= IMX_SERIAL(dev
);
344 DPRINTF("char dev for uart: %p\n", qemu_chr_fe_get_driver(&s
->chr
));
346 qemu_chr_fe_set_handlers(&s
->chr
, imx_can_receive
, imx_receive
,
347 imx_event
, NULL
, s
, NULL
, true);
350 static void imx_serial_init(Object
*obj
)
352 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
353 IMXSerialState
*s
= IMX_SERIAL(obj
);
355 memory_region_init_io(&s
->iomem
, obj
, &imx_serial_ops
, s
,
356 TYPE_IMX_SERIAL
, 0x1000);
357 sysbus_init_mmio(sbd
, &s
->iomem
);
358 sysbus_init_irq(sbd
, &s
->irq
);
361 static Property imx_serial_properties
[] = {
362 DEFINE_PROP_CHR("chardev", IMXSerialState
, chr
),
363 DEFINE_PROP_END_OF_LIST(),
366 static void imx_serial_class_init(ObjectClass
*klass
, void *data
)
368 DeviceClass
*dc
= DEVICE_CLASS(klass
);
370 dc
->realize
= imx_serial_realize
;
371 dc
->vmsd
= &vmstate_imx_serial
;
372 dc
->reset
= imx_serial_reset_at_boot
;
373 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
374 dc
->desc
= "i.MX series UART";
375 device_class_set_props(dc
, imx_serial_properties
);
378 static const TypeInfo imx_serial_info
= {
379 .name
= TYPE_IMX_SERIAL
,
380 .parent
= TYPE_SYS_BUS_DEVICE
,
381 .instance_size
= sizeof(IMXSerialState
),
382 .instance_init
= imx_serial_init
,
383 .class_init
= imx_serial_class_init
,
386 static void imx_serial_register_types(void)
388 type_register_static(&imx_serial_info
);
391 type_init(imx_serial_register_types
)