2
* TI OMAP processors UART emulation.
4
* Copyright (C) 2006-2008 Andrzej Zaborowski <balrog@zabor.org>
5
* Copyright (C) 2007-2009 Nokia Corporation
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License as
9
* published by the Free Software Foundation; either version 2 or
10
* (at your option) version 3 of the License.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
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/>.
20
#include "sysemu/char.h"
22
#include "hw/arm/omap.h"
23
#include "hw/char/serial.h"
24
#include "exec/address-spaces.h"
25
#include "hw/sysbus.h"
27
/* The OMAP UART functionality is similar to the TI16C752; it is
28
* an enhanced version of the 16550A and we piggy-back on the 16550
31
* Currently unmodelled functionality:
32
* + We should have a 64 byte FIFO but QEMU's SerialState emulation
33
* always uses a 16 byte FIFO
35
* + interrupts based on TCR/TLR values
36
* + XON/XOFF flow control
37
* + UASR auto-baudrate-detection
44
SerialState *serial; /* TODO */
45
const MemoryRegionOps *serial_ops;
51
/* Register access mode, which affects what registers you see */
72
uint8_t xon[2], xoff[2];
75
static int tcr_tlr_mode(struct omap_uart_s *s)
77
/* Return true if registers 0x18 and 0x1c are TCR/TLR
78
* (as opposed to SPR/MSR/XOFF)
80
return (s->efr & 0x10) && (s->mcr_cache & 0x40);
83
static void omap_uart_reset(DeviceState *qdev)
85
struct omap_uart_s *s = FROM_SYSBUS(struct omap_uart_s,
86
SYS_BUS_DEVICE(qdev));
94
s->access_mode = regs_operational;
100
s->xon[0] = s->xon[1] = 0;
101
s->xoff[0] = s->xoff[1] = 0;
104
static uint64_t omap_uart_read(void *opaque, hwaddr addr,
107
struct omap_uart_s *s = (struct omap_uart_s *) opaque;
113
return s->serial_ops->read(s->serial, addr, size);
115
if (s->access_mode == regs_config_b) {
118
return s->serial_ops->read(s->serial, addr, size);
121
if (s->access_mode == regs_config_b) {
122
return s->xon[(addr & 7) >> 2];
123
} else if (addr == 0x10) {
124
return s->serial_ops->read(s->serial, addr, size)
125
| (s->mcr_cache & 0xe0);
127
return s->serial_ops->read(s->serial, addr, size);
130
if (tcr_tlr_mode(s)) {
131
return (addr == 0x18) ? s->tcr : s->tlr;
133
if (s->access_mode == regs_config_b) {
134
return s->xoff[(addr & 7) >> 2];
136
return s->serial_ops->read(s->serial, addr, size);
137
case 0x20: /* MDR1 */
139
case 0x24: /* MDR2 */
141
case 0x28: /* SFLSR */
143
case 0x2c: /* RESUME */
145
case 0x30: /* SFREGL */
147
case 0x34: /* SFREGH */
149
case 0x38: /* UASR/BLR */
150
if (s->access_mode != regs_operational) {
151
return 0; /* FIXME: return correct autodetect value */
154
case 0x3c: /* ACREG */
155
return (s->access_mode != regs_operational) ? 0 : s->acreg;
160
case 0x48: /* EBLR (OMAP2) */
162
case 0x4C: /* OSC_12M_SEL (OMAP1) */
166
case 0x54: /* SYSC (OMAP2) */
167
return s->syscontrol;
168
case 0x58: /* SYSS (OMAP2) */
170
case 0x5c: /* WER (OMAP2) */
172
case 0x60: /* CFPS (OMAP2) */
180
static void omap_uart_write(void *opaque, hwaddr addr,
181
uint64_t value, unsigned size)
183
struct omap_uart_s *s = (struct omap_uart_s *) opaque;
188
s->serial_ops->write(s->serial, addr, value, size);
191
if (s->access_mode == regs_config_b) {
194
s->serial_ops->write(s->serial, addr, value, size);
198
if ((value & 0xff) == 0xbf) {
199
s->access_mode = regs_config_b;
200
} else if (value & 0x80) {
201
s->access_mode = regs_config_a;
203
s->access_mode = regs_operational;
205
s->serial_ops->write(s->serial, addr, value, size);
209
if (s->access_mode == regs_config_b) {
210
s->xon[(addr & 7) >> 2] = value;
213
s->mcr_cache = value & 0x7f;
215
s->serial_ops->write(s->serial, addr, value, size);
220
if (tcr_tlr_mode(s)) {
222
s->tcr = value & 0xff;
224
s->tlr = value & 0xff;
226
} else if (s->access_mode == regs_config_b) {
227
s->xoff[(addr & 7) >> 2] = value;
229
s->serial_ops->write(s->serial, addr, value, size);
232
case 0x20: /* MDR1 */
233
s->mdr[0] = value & 0x7f;
235
case 0x24: /* MDR2 */
236
s->mdr[1] = value & 0xff;
238
case 0x28: /* TXFLL */
239
case 0x2c: /* TXFLH */
240
case 0x30: /* RXFLL */
241
case 0x34: /* RXFLH */
245
if (s->access_mode == regs_operational) {
246
s->blr = value & 0xc0;
249
case 0x3c: /* ACREG */
250
if (s->access_mode == regs_operational) {
251
s->acreg = value & 0xff;
255
s->scr = value & 0xff;
260
case 0x48: /* EBLR (OMAP2) */
261
s->eblr = value & 0xff;
263
case 0x4C: /* OSC_12M_SEL (OMAP1) */
264
s->clksel = value & 1;
269
case 0x54: /* SYSC (OMAP2) */
270
s->syscontrol = value & 0x1d;
272
/* TODO: reset s->serial also. */
273
omap_uart_reset(&s->busdev.qdev);
276
case 0x58: /* SYSS (OMAP2) */
279
case 0x5c: /* WER (OMAP2) */
280
s->wkup = value & 0x7f;
282
case 0x60: /* CFPS (OMAP2) */
283
s->cfps = value & 0xff;
290
static const MemoryRegionOps omap_uart_ops = {
291
.read = omap_uart_read,
292
.write = omap_uart_write,
293
.endianness = DEVICE_NATIVE_ENDIAN,
296
static int omap_uart_init(SysBusDevice *busdev)
298
struct omap_uart_s *s = FROM_SYSBUS(struct omap_uart_s, busdev);
300
s->chr = qemu_chr_new(busdev->qdev.id, "null", NULL);
302
/* TODO: DMA support. Current 16550A emulation does not emulate DMA mode
303
* transfers via TXRDY/RXRDY pins. We create DMA irq lines here for
304
* future use nevertheless. */
305
/* Nasty hackery because trying to extend an existing device is
306
* not really supported, and the serial driver isn't even qdev.
308
s->serial = serial_mm_init(NULL, 0, 2, NULL, s->baudrate, s->chr,
309
DEVICE_NATIVE_ENDIAN);
310
s->serial_ops = serial_get_memops(DEVICE_NATIVE_ENDIAN);
311
sysbus_init_irq(busdev, serial_get_irq(s->serial));
312
sysbus_init_irq(busdev, &s->tx_drq);
313
sysbus_init_irq(busdev, &s->rx_drq);
314
memory_region_init_io(&s->iomem, &omap_uart_ops, s, "omap_uart",
316
sysbus_init_mmio(busdev, &s->iomem);
320
static Property omap_uart_properties[] = {
321
DEFINE_PROP_UINT32("mmio_size", struct omap_uart_s, mmio_size, 0x400),
322
DEFINE_PROP_UINT32("baudrate", struct omap_uart_s, baudrate, 0),
323
DEFINE_PROP_CHR("chardev", struct omap_uart_s, chr),
324
DEFINE_PROP_END_OF_LIST()
327
static void omap_uart_class_init(ObjectClass *klass, void *data)
329
DeviceClass *dc = DEVICE_CLASS(klass);
330
SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
331
k->init = omap_uart_init;
332
dc->props = omap_uart_properties;
333
dc->reset = omap_uart_reset;
336
static TypeInfo omap_uart_info = {
338
.parent = TYPE_SYS_BUS_DEVICE,
339
.instance_size = sizeof(struct omap_uart_s),
340
.class_init = omap_uart_class_init,
343
static void omap_uart_register_types(void)
345
type_register_static(&omap_uart_info);
348
void omap_uart_attach(DeviceState *qdev, CharDriverState *chr,
351
struct omap_uart_s *s = FROM_SYSBUS(struct omap_uart_s,
352
SYS_BUS_DEVICE(qdev));
353
s->chr = chr ?: qemu_chr_new(label, "null", NULL);
354
serial_change_char_driver(s->serial, s->chr);
357
type_init(omap_uart_register_types)