2 * Exynos4210 UART Emulation
4 * Copyright (C) 2011 Samsung Electronics Co Ltd.
5 * Maksim Kozlov, <m.kozlov@samsung.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "qemu/osdep.h"
23 #include "hw/sysbus.h"
24 #include "qemu/error-report.h"
25 #include "sysemu/sysemu.h"
26 #include "chardev/char-fe.h"
27 #include "chardev/char-serial.h"
29 #include "hw/arm/exynos4210.h"
32 #undef DEBUG_UART_EXTEND
38 #define DEBUG_UART_EXTEND 0
40 #define DEBUG_Rx_DATA 0
41 #define DEBUG_Tx_DATA 0
44 #define PRINT_DEBUG(fmt, args...) \
46 fprintf(stderr, " [%s:%d] "fmt, __func__, __LINE__, ##args); \
50 #define PRINT_DEBUG_EXTEND(fmt, args...) \
52 fprintf(stderr, " [%s:%d] "fmt, __func__, __LINE__, ##args); \
55 #define PRINT_DEBUG_EXTEND(fmt, args...) \
60 #define PRINT_DEBUG(fmt, args...) \
62 #define PRINT_DEBUG_EXTEND(fmt, args...) \
66 #define PRINT_ERROR(fmt, args...) \
68 fprintf(stderr, " [%s:%d] "fmt, __func__, __LINE__, ##args); \
72 * Offsets for UART registers relative to SFR base address
76 #define ULCON 0x0000 /* Line Control */
77 #define UCON 0x0004 /* Control */
78 #define UFCON 0x0008 /* FIFO Control */
79 #define UMCON 0x000C /* Modem Control */
80 #define UTRSTAT 0x0010 /* Tx/Rx Status */
81 #define UERSTAT 0x0014 /* UART Error Status */
82 #define UFSTAT 0x0018 /* FIFO Status */
83 #define UMSTAT 0x001C /* Modem Status */
84 #define UTXH 0x0020 /* Transmit Buffer */
85 #define URXH 0x0024 /* Receive Buffer */
86 #define UBRDIV 0x0028 /* Baud Rate Divisor */
87 #define UFRACVAL 0x002C /* Divisor Fractional Value */
88 #define UINTP 0x0030 /* Interrupt Pending */
89 #define UINTSP 0x0034 /* Interrupt Source Pending */
90 #define UINTM 0x0038 /* Interrupt Mask */
93 * for indexing register in the uint32_t array
95 * 'reg' - register offset (see offsets definitions above)
98 #define I_(reg) (reg / sizeof(uint32_t))
100 typedef struct Exynos4210UartReg
{
101 const char *name
; /* the only reason is the debug output */
103 uint32_t reset_value
;
106 static const Exynos4210UartReg exynos4210_uart_regs
[] = {
107 {"ULCON", ULCON
, 0x00000000},
108 {"UCON", UCON
, 0x00003000},
109 {"UFCON", UFCON
, 0x00000000},
110 {"UMCON", UMCON
, 0x00000000},
111 {"UTRSTAT", UTRSTAT
, 0x00000006}, /* RO */
112 {"UERSTAT", UERSTAT
, 0x00000000}, /* RO */
113 {"UFSTAT", UFSTAT
, 0x00000000}, /* RO */
114 {"UMSTAT", UMSTAT
, 0x00000000}, /* RO */
115 {"UTXH", UTXH
, 0x5c5c5c5c}, /* WO, undefined reset value*/
116 {"URXH", URXH
, 0x00000000}, /* RO */
117 {"UBRDIV", UBRDIV
, 0x00000000},
118 {"UFRACVAL", UFRACVAL
, 0x00000000},
119 {"UINTP", UINTP
, 0x00000000},
120 {"UINTSP", UINTSP
, 0x00000000},
121 {"UINTM", UINTM
, 0x00000000},
124 #define EXYNOS4210_UART_REGS_MEM_SIZE 0x3C
126 /* UART FIFO Control */
127 #define UFCON_FIFO_ENABLE 0x1
128 #define UFCON_Rx_FIFO_RESET 0x2
129 #define UFCON_Tx_FIFO_RESET 0x4
130 #define UFCON_Tx_FIFO_TRIGGER_LEVEL_SHIFT 8
131 #define UFCON_Tx_FIFO_TRIGGER_LEVEL (7 << UFCON_Tx_FIFO_TRIGGER_LEVEL_SHIFT)
132 #define UFCON_Rx_FIFO_TRIGGER_LEVEL_SHIFT 4
133 #define UFCON_Rx_FIFO_TRIGGER_LEVEL (7 << UFCON_Rx_FIFO_TRIGGER_LEVEL_SHIFT)
135 /* Uart FIFO Status */
136 #define UFSTAT_Rx_FIFO_COUNT 0xff
137 #define UFSTAT_Rx_FIFO_FULL 0x100
138 #define UFSTAT_Rx_FIFO_ERROR 0x200
139 #define UFSTAT_Tx_FIFO_COUNT_SHIFT 16
140 #define UFSTAT_Tx_FIFO_COUNT (0xff << UFSTAT_Tx_FIFO_COUNT_SHIFT)
141 #define UFSTAT_Tx_FIFO_FULL_SHIFT 24
142 #define UFSTAT_Tx_FIFO_FULL (1 << UFSTAT_Tx_FIFO_FULL_SHIFT)
144 /* UART Interrupt Source Pending */
145 #define UINTSP_RXD 0x1 /* Receive interrupt */
146 #define UINTSP_ERROR 0x2 /* Error interrupt */
147 #define UINTSP_TXD 0x4 /* Transmit interrupt */
148 #define UINTSP_MODEM 0x8 /* Modem interrupt */
150 /* UART Line Control */
151 #define ULCON_IR_MODE_SHIFT 6
152 #define ULCON_PARITY_SHIFT 3
153 #define ULCON_STOP_BIT_SHIFT 1
155 /* UART Tx/Rx Status */
156 #define UTRSTAT_TRANSMITTER_EMPTY 0x4
157 #define UTRSTAT_Tx_BUFFER_EMPTY 0x2
158 #define UTRSTAT_Rx_BUFFER_DATA_READY 0x1
160 /* UART Error Status */
161 #define UERSTAT_OVERRUN 0x1
162 #define UERSTAT_PARITY 0x2
163 #define UERSTAT_FRAME 0x4
164 #define UERSTAT_BREAK 0x8
168 uint32_t sp
, rp
; /* store and retrieve pointers */
170 } Exynos4210UartFIFO
;
172 #define TYPE_EXYNOS4210_UART "exynos4210.uart"
173 #define EXYNOS4210_UART(obj) \
174 OBJECT_CHECK(Exynos4210UartState, (obj), TYPE_EXYNOS4210_UART)
176 typedef struct Exynos4210UartState
{
177 SysBusDevice parent_obj
;
181 uint32_t reg
[EXYNOS4210_UART_REGS_MEM_SIZE
/ sizeof(uint32_t)];
182 Exynos4210UartFIFO rx
;
183 Exynos4210UartFIFO tx
;
190 } Exynos4210UartState
;
194 /* Used only for debugging inside PRINT_DEBUG_... macros */
195 static const char *exynos4210_uart_regname(hwaddr offset
)
200 for (i
= 0; i
< ARRAY_SIZE(exynos4210_uart_regs
); i
++) {
201 if (offset
== exynos4210_uart_regs
[i
].offset
) {
202 return exynos4210_uart_regs
[i
].name
;
211 static void fifo_store(Exynos4210UartFIFO
*q
, uint8_t ch
)
214 q
->sp
= (q
->sp
+ 1) % q
->size
;
217 static uint8_t fifo_retrieve(Exynos4210UartFIFO
*q
)
219 uint8_t ret
= q
->data
[q
->rp
];
220 q
->rp
= (q
->rp
+ 1) % q
->size
;
224 static int fifo_elements_number(const Exynos4210UartFIFO
*q
)
227 return q
->size
- q
->rp
+ q
->sp
;
230 return q
->sp
- q
->rp
;
233 static int fifo_empty_elements_number(const Exynos4210UartFIFO
*q
)
235 return q
->size
- fifo_elements_number(q
);
238 static void fifo_reset(Exynos4210UartFIFO
*q
)
243 q
->data
= (uint8_t *)g_malloc0(q
->size
);
249 static uint32_t exynos4210_uart_Tx_FIFO_trigger_level(const Exynos4210UartState
*s
)
254 reg
= (s
->reg
[I_(UFCON
)] & UFCON_Tx_FIFO_TRIGGER_LEVEL
) >>
255 UFCON_Tx_FIFO_TRIGGER_LEVEL_SHIFT
;
257 switch (s
->channel
) {
271 PRINT_ERROR("Wrong UART channel number: %d\n", s
->channel
);
277 static void exynos4210_uart_update_irq(Exynos4210UartState
*s
)
280 * The Tx interrupt is always requested if the number of data in the
281 * transmit FIFO is smaller than the trigger level.
283 if (s
->reg
[I_(UFCON
)] & UFCON_FIFO_ENABLE
) {
285 uint32_t count
= (s
->reg
[I_(UFSTAT
)] & UFSTAT_Tx_FIFO_COUNT
) >>
286 UFSTAT_Tx_FIFO_COUNT_SHIFT
;
288 if (count
<= exynos4210_uart_Tx_FIFO_trigger_level(s
)) {
289 s
->reg
[I_(UINTSP
)] |= UINTSP_TXD
;
293 s
->reg
[I_(UINTP
)] = s
->reg
[I_(UINTSP
)] & ~s
->reg
[I_(UINTM
)];
295 if (s
->reg
[I_(UINTP
)]) {
296 qemu_irq_raise(s
->irq
);
299 fprintf(stderr
, "UART%d: IRQ has been raised: %08x\n",
300 s
->channel
, s
->reg
[I_(UINTP
)]);
304 qemu_irq_lower(s
->irq
);
308 static void exynos4210_uart_update_parameters(Exynos4210UartState
*s
)
310 int speed
, parity
, data_bits
, stop_bits
;
311 QEMUSerialSetParams ssp
;
314 if (s
->reg
[I_(UBRDIV
)] == 0) {
318 if (s
->reg
[I_(ULCON
)] & 0x20) {
319 if (s
->reg
[I_(ULCON
)] & 0x28) {
328 if (s
->reg
[I_(ULCON
)] & 0x4) {
334 data_bits
= (s
->reg
[I_(ULCON
)] & 0x3) + 5;
336 uclk_rate
= 24000000;
338 speed
= uclk_rate
/ ((16 * (s
->reg
[I_(UBRDIV
)]) & 0xffff) +
339 (s
->reg
[I_(UFRACVAL
)] & 0x7) + 16);
343 ssp
.data_bits
= data_bits
;
344 ssp
.stop_bits
= stop_bits
;
346 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
348 PRINT_DEBUG("UART%d: speed: %d, parity: %c, data: %d, stop: %d\n",
349 s
->channel
, speed
, parity
, data_bits
, stop_bits
);
352 static void exynos4210_uart_write(void *opaque
, hwaddr offset
,
353 uint64_t val
, unsigned size
)
355 Exynos4210UartState
*s
= (Exynos4210UartState
*)opaque
;
358 PRINT_DEBUG_EXTEND("UART%d: <0x%04x> %s <- 0x%08llx\n", s
->channel
,
359 offset
, exynos4210_uart_regname(offset
), (long long unsigned int)val
);
365 s
->reg
[I_(offset
)] = val
;
366 exynos4210_uart_update_parameters(s
);
369 s
->reg
[I_(UFCON
)] = val
;
370 if (val
& UFCON_Rx_FIFO_RESET
) {
372 s
->reg
[I_(UFCON
)] &= ~UFCON_Rx_FIFO_RESET
;
373 PRINT_DEBUG("UART%d: Rx FIFO Reset\n", s
->channel
);
375 if (val
& UFCON_Tx_FIFO_RESET
) {
377 s
->reg
[I_(UFCON
)] &= ~UFCON_Tx_FIFO_RESET
;
378 PRINT_DEBUG("UART%d: Tx FIFO Reset\n", s
->channel
);
383 if (qemu_chr_fe_backend_connected(&s
->chr
)) {
384 s
->reg
[I_(UTRSTAT
)] &= ~(UTRSTAT_TRANSMITTER_EMPTY
|
385 UTRSTAT_Tx_BUFFER_EMPTY
);
387 /* XXX this blocks entire thread. Rewrite to use
388 * qemu_chr_fe_write and background I/O callbacks */
389 qemu_chr_fe_write_all(&s
->chr
, &ch
, 1);
391 fprintf(stderr
, "%c", ch
);
393 s
->reg
[I_(UTRSTAT
)] |= UTRSTAT_TRANSMITTER_EMPTY
|
394 UTRSTAT_Tx_BUFFER_EMPTY
;
395 s
->reg
[I_(UINTSP
)] |= UINTSP_TXD
;
396 exynos4210_uart_update_irq(s
);
401 s
->reg
[I_(UINTP
)] &= ~val
;
402 s
->reg
[I_(UINTSP
)] &= ~val
;
403 PRINT_DEBUG("UART%d: UINTP [%04x] have been cleared: %08x\n",
404 s
->channel
, offset
, s
->reg
[I_(UINTP
)]);
405 exynos4210_uart_update_irq(s
);
412 PRINT_DEBUG("UART%d: Trying to write into RO register: %s [%04x]\n",
413 s
->channel
, exynos4210_uart_regname(offset
), offset
);
416 s
->reg
[I_(UINTSP
)] &= ~val
;
419 s
->reg
[I_(UINTM
)] = val
;
420 exynos4210_uart_update_irq(s
);
425 s
->reg
[I_(offset
)] = val
;
429 static uint64_t exynos4210_uart_read(void *opaque
, hwaddr offset
,
432 Exynos4210UartState
*s
= (Exynos4210UartState
*)opaque
;
436 case UERSTAT
: /* Read Only */
437 res
= s
->reg
[I_(UERSTAT
)];
438 s
->reg
[I_(UERSTAT
)] = 0;
440 case UFSTAT
: /* Read Only */
441 s
->reg
[I_(UFSTAT
)] = fifo_elements_number(&s
->rx
) & 0xff;
442 if (fifo_empty_elements_number(&s
->rx
) == 0) {
443 s
->reg
[I_(UFSTAT
)] |= UFSTAT_Rx_FIFO_FULL
;
444 s
->reg
[I_(UFSTAT
)] &= ~0xff;
446 return s
->reg
[I_(UFSTAT
)];
448 if (s
->reg
[I_(UFCON
)] & UFCON_FIFO_ENABLE
) {
449 if (fifo_elements_number(&s
->rx
)) {
450 res
= fifo_retrieve(&s
->rx
);
452 fprintf(stderr
, "%c", res
);
454 if (!fifo_elements_number(&s
->rx
)) {
455 s
->reg
[I_(UTRSTAT
)] &= ~UTRSTAT_Rx_BUFFER_DATA_READY
;
457 s
->reg
[I_(UTRSTAT
)] |= UTRSTAT_Rx_BUFFER_DATA_READY
;
460 s
->reg
[I_(UINTSP
)] |= UINTSP_ERROR
;
461 exynos4210_uart_update_irq(s
);
465 s
->reg
[I_(UTRSTAT
)] &= ~UTRSTAT_Rx_BUFFER_DATA_READY
;
466 res
= s
->reg
[I_(URXH
)];
470 PRINT_DEBUG("UART%d: Trying to read from WO register: %s [%04x]\n",
471 s
->channel
, exynos4210_uart_regname(offset
), offset
);
474 return s
->reg
[I_(offset
)];
480 static const MemoryRegionOps exynos4210_uart_ops
= {
481 .read
= exynos4210_uart_read
,
482 .write
= exynos4210_uart_write
,
483 .endianness
= DEVICE_NATIVE_ENDIAN
,
485 .max_access_size
= 4,
490 static int exynos4210_uart_can_receive(void *opaque
)
492 Exynos4210UartState
*s
= (Exynos4210UartState
*)opaque
;
494 return fifo_empty_elements_number(&s
->rx
);
498 static void exynos4210_uart_receive(void *opaque
, const uint8_t *buf
, int size
)
500 Exynos4210UartState
*s
= (Exynos4210UartState
*)opaque
;
503 if (s
->reg
[I_(UFCON
)] & UFCON_FIFO_ENABLE
) {
504 if (fifo_empty_elements_number(&s
->rx
) < size
) {
505 for (i
= 0; i
< fifo_empty_elements_number(&s
->rx
); i
++) {
506 fifo_store(&s
->rx
, buf
[i
]);
508 s
->reg
[I_(UINTSP
)] |= UINTSP_ERROR
;
509 s
->reg
[I_(UTRSTAT
)] |= UTRSTAT_Rx_BUFFER_DATA_READY
;
511 for (i
= 0; i
< size
; i
++) {
512 fifo_store(&s
->rx
, buf
[i
]);
514 s
->reg
[I_(UTRSTAT
)] |= UTRSTAT_Rx_BUFFER_DATA_READY
;
516 /* XXX: Around here we maybe should check Rx trigger level */
517 s
->reg
[I_(UINTSP
)] |= UINTSP_RXD
;
519 s
->reg
[I_(URXH
)] = buf
[0];
520 s
->reg
[I_(UINTSP
)] |= UINTSP_RXD
;
521 s
->reg
[I_(UTRSTAT
)] |= UTRSTAT_Rx_BUFFER_DATA_READY
;
524 exynos4210_uart_update_irq(s
);
528 static void exynos4210_uart_event(void *opaque
, int event
)
530 Exynos4210UartState
*s
= (Exynos4210UartState
*)opaque
;
532 if (event
== CHR_EVENT_BREAK
) {
533 /* When the RxDn is held in logic 0, then a null byte is pushed into the
535 fifo_store(&s
->rx
, '\0');
536 s
->reg
[I_(UERSTAT
)] |= UERSTAT_BREAK
;
537 exynos4210_uart_update_irq(s
);
542 static void exynos4210_uart_reset(DeviceState
*dev
)
544 Exynos4210UartState
*s
= EXYNOS4210_UART(dev
);
547 for (i
= 0; i
< ARRAY_SIZE(exynos4210_uart_regs
); i
++) {
548 s
->reg
[I_(exynos4210_uart_regs
[i
].offset
)] =
549 exynos4210_uart_regs
[i
].reset_value
;
555 PRINT_DEBUG("UART%d: Rx FIFO size: %d\n", s
->channel
, s
->rx
.size
);
558 static const VMStateDescription vmstate_exynos4210_uart_fifo
= {
559 .name
= "exynos4210.uart.fifo",
561 .minimum_version_id
= 1,
562 .fields
= (VMStateField
[]) {
563 VMSTATE_UINT32(sp
, Exynos4210UartFIFO
),
564 VMSTATE_UINT32(rp
, Exynos4210UartFIFO
),
565 VMSTATE_VBUFFER_UINT32(data
, Exynos4210UartFIFO
, 1, NULL
, size
),
566 VMSTATE_END_OF_LIST()
570 static const VMStateDescription vmstate_exynos4210_uart
= {
571 .name
= "exynos4210.uart",
573 .minimum_version_id
= 1,
574 .fields
= (VMStateField
[]) {
575 VMSTATE_STRUCT(rx
, Exynos4210UartState
, 1,
576 vmstate_exynos4210_uart_fifo
, Exynos4210UartFIFO
),
577 VMSTATE_UINT32_ARRAY(reg
, Exynos4210UartState
,
578 EXYNOS4210_UART_REGS_MEM_SIZE
/ sizeof(uint32_t)),
579 VMSTATE_END_OF_LIST()
583 DeviceState
*exynos4210_uart_create(hwaddr addr
,
592 dev
= qdev_create(NULL
, TYPE_EXYNOS4210_UART
);
594 qdev_prop_set_chr(dev
, "chardev", chr
);
595 qdev_prop_set_uint32(dev
, "channel", channel
);
596 qdev_prop_set_uint32(dev
, "rx-size", fifo_size
);
597 qdev_prop_set_uint32(dev
, "tx-size", fifo_size
);
599 bus
= SYS_BUS_DEVICE(dev
);
600 qdev_init_nofail(dev
);
601 if (addr
!= (hwaddr
)-1) {
602 sysbus_mmio_map(bus
, 0, addr
);
604 sysbus_connect_irq(bus
, 0, irq
);
609 static void exynos4210_uart_init(Object
*obj
)
611 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
612 Exynos4210UartState
*s
= EXYNOS4210_UART(dev
);
615 memory_region_init_io(&s
->iomem
, obj
, &exynos4210_uart_ops
, s
,
616 "exynos4210.uart", EXYNOS4210_UART_REGS_MEM_SIZE
);
617 sysbus_init_mmio(dev
, &s
->iomem
);
619 sysbus_init_irq(dev
, &s
->irq
);
622 static void exynos4210_uart_realize(DeviceState
*dev
, Error
**errp
)
624 Exynos4210UartState
*s
= EXYNOS4210_UART(dev
);
626 qemu_chr_fe_set_handlers(&s
->chr
, exynos4210_uart_can_receive
,
627 exynos4210_uart_receive
, exynos4210_uart_event
,
628 NULL
, s
, NULL
, true);
631 static Property exynos4210_uart_properties
[] = {
632 DEFINE_PROP_CHR("chardev", Exynos4210UartState
, chr
),
633 DEFINE_PROP_UINT32("channel", Exynos4210UartState
, channel
, 0),
634 DEFINE_PROP_UINT32("rx-size", Exynos4210UartState
, rx
.size
, 16),
635 DEFINE_PROP_UINT32("tx-size", Exynos4210UartState
, tx
.size
, 16),
636 DEFINE_PROP_END_OF_LIST(),
639 static void exynos4210_uart_class_init(ObjectClass
*klass
, void *data
)
641 DeviceClass
*dc
= DEVICE_CLASS(klass
);
643 dc
->realize
= exynos4210_uart_realize
;
644 dc
->reset
= exynos4210_uart_reset
;
645 dc
->props
= exynos4210_uart_properties
;
646 dc
->vmsd
= &vmstate_exynos4210_uart
;
649 static const TypeInfo exynos4210_uart_info
= {
650 .name
= TYPE_EXYNOS4210_UART
,
651 .parent
= TYPE_SYS_BUS_DEVICE
,
652 .instance_size
= sizeof(Exynos4210UartState
),
653 .instance_init
= exynos4210_uart_init
,
654 .class_init
= exynos4210_uart_class_init
,
657 static void exynos4210_uart_register(void)
659 type_register_static(&exynos4210_uart_info
);
662 type_init(exynos4210_uart_register
)