2 * Arm PrimeCell PL041 Advanced Audio Codec Interface
5 * Written by Mathieu Sonet - www.elasticsheep.com
7 * This code is licensed under the GPL.
9 * *****************************************************************
11 * This driver emulates the ARM AACI interface
12 * connected to a LM4549 codec.
15 * - Supports only a playback on one channel (Versatile/Vexpress)
16 * - Supports only one TX FIFO in compact-mode or non-compact mode.
17 * - Supports playback of 12, 16, 18 and 20 bits samples.
18 * - Record is not supported.
19 * - The PL041 is hardwired to a LM4549 codec.
23 #include "qemu/osdep.h"
25 #include "hw/qdev-properties.h"
26 #include "hw/sysbus.h"
28 #include "qemu/module.h"
32 #include "migration/vmstate.h"
35 #define PL041_DEBUG_LEVEL 1
38 #if defined(PL041_DEBUG_LEVEL) && (PL041_DEBUG_LEVEL >= 1)
39 #define DBG_L1(fmt, ...) \
40 do { printf("pl041: " fmt , ## __VA_ARGS__); } while (0)
42 #define DBG_L1(fmt, ...) \
46 #if defined(PL041_DEBUG_LEVEL) && (PL041_DEBUG_LEVEL >= 2)
47 #define DBG_L2(fmt, ...) \
48 do { printf("pl041: " fmt , ## __VA_ARGS__); } while (0)
50 #define DBG_L2(fmt, ...) \
55 #define MAX_FIFO_DEPTH (1024)
56 #define DEFAULT_FIFO_DEPTH (8)
58 #define SLOT1_RW (1 << 19)
60 /* This FIFO only stores 20-bit samples on 32-bit words.
61 So its level is independent of the selected mode */
64 uint32_t data[MAX_FIFO_DEPTH];
70 uint8_t tx_compact_mode;
71 uint8_t tx_sample_size;
75 uint8_t rx_compact_mode;
76 uint8_t rx_sample_size;
79 #define TYPE_PL041 "pl041"
80 #define PL041(obj) OBJECT_CHECK(PL041State, (obj), TYPE_PL041)
82 typedef struct PL041State {
83 SysBusDevice parent_obj;
88 uint32_t fifo_depth; /* FIFO depth in non-compact mode */
96 static const unsigned char pl041_default_id[8] = {
97 0x41, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
100 #if defined(PL041_DEBUG_LEVEL)
101 #define REGISTER(name, offset) #name,
102 static const char *pl041_regs_name[] = {
109 #if defined(PL041_DEBUG_LEVEL)
110 static const char *get_reg_name(hwaddr offset)
112 if (offset <= PL041_dr1_7) {
113 return pl041_regs_name[offset >> 2];
120 static uint8_t pl041_compute_periphid3(PL041State *s)
122 uint8_t id3 = 1; /* One channel */
124 /* Add the fifo depth information */
125 switch (s->fifo_depth) {
155 static void pl041_reset(PL041State *s)
157 DBG_L1("pl041_reset\n");
159 memset(&s->regs, 0x00, sizeof(pl041_regfile));
161 s->regs.slfr = SL1TXEMPTY | SL2TXEMPTY | SL12TXEMPTY;
162 s->regs.sr1 = TXFE | RXFE | TXHE;
165 memset(&s->fifo1, 0x00, sizeof(s->fifo1));
169 static void pl041_fifo1_write(PL041State *s, uint32_t value)
171 pl041_channel *channel = &s->fifo1;
172 pl041_fifo *fifo = &s->fifo1.tx_fifo;
174 /* Push the value in the FIFO */
175 if (channel->tx_compact_mode == 0) {
176 /* Non-compact mode */
178 if (fifo->level < s->fifo_depth) {
179 /* Pad the value with 0 to obtain a 20-bit sample */
180 switch (channel->tx_sample_size) {
182 value = (value << 8) & 0xFFFFF;
185 value = (value << 4) & 0xFFFFF;
188 value = (value << 2) & 0xFFFFF;
195 /* Store the sample in the FIFO */
196 fifo->data[fifo->level++] = value;
198 #if defined(PL041_DEBUG_LEVEL)
200 DBG_L1("fifo1 write: overrun\n");
206 if ((fifo->level + 2) < s->fifo_depth) {
210 for (i = 0; i < 2; i++) {
211 sample = value & 0xFFFF;
214 /* Pad each sample with 0 to obtain a 20-bit sample */
215 switch (channel->tx_sample_size) {
217 sample = sample << 8;
221 sample = sample << 4;
225 /* Store the sample in the FIFO */
226 fifo->data[fifo->level++] = sample;
229 #if defined(PL041_DEBUG_LEVEL)
231 DBG_L1("fifo1 write: overrun\n");
236 /* Update the status register */
237 if (fifo->level > 0) {
238 s->regs.sr1 &= ~(TXUNDERRUN | TXFE);
241 if (fifo->level >= (s->fifo_depth / 2)) {
242 s->regs.sr1 &= ~TXHE;
245 if (fifo->level >= s->fifo_depth) {
249 DBG_L2("fifo1_push sr1 = 0x%08x\n", s->regs.sr1);
252 static void pl041_fifo1_transmit(PL041State *s)
254 pl041_channel *channel = &s->fifo1;
255 pl041_fifo *fifo = &s->fifo1.tx_fifo;
256 uint32_t slots = s->regs.txcr1 & TXSLOT_MASK;
257 uint32_t written_samples;
259 /* Check if FIFO1 transmit is enabled */
260 if ((channel->tx_enabled) && (slots & (TXSLOT3 | TXSLOT4))) {
261 if (fifo->level >= (s->fifo_depth / 2)) {
264 DBG_L1("Transfer FIFO level = %i\n", fifo->level);
266 /* Try to transfer the whole FIFO */
267 for (i = 0; i < (fifo->level / 2); i++) {
268 uint32_t left = fifo->data[i * 2];
269 uint32_t right = fifo->data[i * 2 + 1];
271 /* Transmit two 20-bit samples to the codec */
272 if (lm4549_write_samples(&s->codec, left, right) == 0) {
273 DBG_L1("Codec buffer full\n");
278 written_samples = i * 2;
279 if (written_samples > 0) {
280 /* Update the FIFO level */
281 fifo->level -= written_samples;
283 /* Move back the pending samples to the start of the FIFO */
284 for (i = 0; i < fifo->level; i++) {
285 fifo->data[i] = fifo->data[written_samples + i];
288 /* Update the status register */
289 s->regs.sr1 &= ~TXFF;
291 if (fifo->level <= (s->fifo_depth / 2)) {
295 if (fifo->level == 0) {
296 s->regs.sr1 |= TXFE | TXUNDERRUN;
297 DBG_L1("Empty FIFO\n");
304 static void pl041_isr1_update(PL041State *s)
307 if (s->regs.sr1 & TXUNDERRUN) {
308 s->regs.isr1 |= URINTR;
310 s->regs.isr1 &= ~URINTR;
313 if (s->regs.sr1 & TXHE) {
314 s->regs.isr1 |= TXINTR;
316 s->regs.isr1 &= ~TXINTR;
319 if (!(s->regs.sr1 & TXBUSY) && (s->regs.sr1 & TXFE)) {
320 s->regs.isr1 |= TXCINTR;
322 s->regs.isr1 &= ~TXCINTR;
325 /* Update the irq state */
326 qemu_set_irq(s->irq, ((s->regs.isr1 & s->regs.ie1) > 0) ? 1 : 0);
327 DBG_L2("Set interrupt sr1 = 0x%08x isr1 = 0x%08x masked = 0x%08x\n",
328 s->regs.sr1, s->regs.isr1, s->regs.isr1 & s->regs.ie1);
331 static void pl041_request_data(void *opaque)
333 PL041State *s = (PL041State *)opaque;
335 /* Trigger pending transfers */
336 pl041_fifo1_transmit(s);
337 pl041_isr1_update(s);
340 static uint64_t pl041_read(void *opaque, hwaddr offset,
343 PL041State *s = (PL041State *)opaque;
346 if ((offset >= PL041_periphid0) && (offset <= PL041_pcellid3)) {
347 if (offset == PL041_periphid3) {
348 value = pl041_compute_periphid3(s);
350 value = pl041_default_id[(offset - PL041_periphid0) >> 2];
353 DBG_L1("pl041_read [0x%08x] => 0x%08x\n", offset, value);
355 } else if (offset <= PL041_dr4_7) {
356 value = *((uint32_t *)&s->regs + (offset >> 2));
358 DBG_L1("pl041_read: Reserved offset %x\n", (int)offset);
364 value = s->regs.isr1 & 0x7F;
368 DBG_L1("pl041_read [0x%08x] %s => 0x%08x\n", offset,
369 get_reg_name(offset), value);
374 static void pl041_write(void *opaque, hwaddr offset,
375 uint64_t value, unsigned size)
377 PL041State *s = (PL041State *)opaque;
378 uint16_t control, data;
381 DBG_L1("pl041_write [0x%08x] %s <= 0x%08x\n", offset,
382 get_reg_name(offset), (unsigned int)value);
384 /* Write the register */
385 if (offset <= PL041_dr4_7) {
386 *((uint32_t *)&s->regs + (offset >> 2)) = value;
388 DBG_L1("pl041_write: Reserved offset %x\n", (int)offset);
392 /* Execute the actions */
396 pl041_channel *channel = &s->fifo1;
398 uint32_t txen = s->regs.txcr1 & TXEN;
399 uint32_t tsize = (s->regs.txcr1 & TSIZE_MASK) >> TSIZE_MASK_BIT;
400 uint32_t compact_mode = (s->regs.txcr1 & TXCOMPACT) ? 1 : 0;
401 #if defined(PL041_DEBUG_LEVEL)
402 uint32_t slots = (s->regs.txcr1 & TXSLOT_MASK) >> TXSLOT_MASK_BIT;
403 uint32_t txfen = (s->regs.txcr1 & TXFEN) > 0 ? 1 : 0;
406 DBG_L1("=> txen = %i slots = 0x%01x tsize = %i compact = %i "
407 "txfen = %i\n", txen, slots, tsize, compact_mode, txfen);
409 channel->tx_enabled = txen;
410 channel->tx_compact_mode = compact_mode;
414 channel->tx_sample_size = 16;
417 channel->tx_sample_size = 18;
420 channel->tx_sample_size = 20;
423 channel->tx_sample_size = 12;
427 DBG_L1("TX enabled = %i\n", channel->tx_enabled);
428 DBG_L1("TX compact mode = %i\n", channel->tx_compact_mode);
429 DBG_L1("TX sample width = %i\n", channel->tx_sample_size);
431 /* Check if compact mode is allowed with selected tsize */
432 if (channel->tx_compact_mode == 1) {
433 if ((channel->tx_sample_size == 18) ||
434 (channel->tx_sample_size == 20)) {
435 channel->tx_compact_mode = 0;
436 DBG_L1("Compact mode not allowed with 18/20-bit sample size\n");
443 s->regs.slfr &= ~SL1TXEMPTY;
445 control = (s->regs.sl1tx >> 12) & 0x7F;
446 data = (s->regs.sl2tx >> 4) & 0xFFFF;
448 if ((s->regs.sl1tx & SLOT1_RW) == 0) {
449 /* Write operation */
450 lm4549_write(&s->codec, control, data);
453 result = lm4549_read(&s->codec, control);
455 /* Store the returned value */
456 s->regs.sl1rx = s->regs.sl1tx & ~SLOT1_RW;
457 s->regs.sl2rx = result << 4;
459 s->regs.slfr &= ~(SL1RXBUSY | SL2RXBUSY);
460 s->regs.slfr |= SL1RXVALID | SL2RXVALID;
465 s->regs.sl2tx = value;
466 s->regs.slfr &= ~SL2TXEMPTY;
470 DBG_L1("=> Clear interrupt intclr = 0x%08x isr1 = 0x%08x\n",
471 s->regs.intclr, s->regs.isr1);
473 if (s->regs.intclr & TXUEC1) {
474 s->regs.sr1 &= ~TXUNDERRUN;
480 #if defined(PL041_DEBUG_LEVEL)
481 char debug[] = " AACIFE SL1RXEN SL1TXEN";
482 if (!(value & AACIFE)) {
485 if (!(value & SL1RXEN)) {
488 if (!(value & SL1TXEN)) {
491 DBG_L1("%s\n", debug);
494 if ((s->regs.maincr & AACIFE) == 0) {
504 pl041_fifo1_write(s, value);
508 /* Transmit the FIFO content */
509 pl041_fifo1_transmit(s);
511 /* Update the ISR1 register */
512 pl041_isr1_update(s);
515 static void pl041_device_reset(DeviceState *d)
517 PL041State *s = PL041(d);
522 static const MemoryRegionOps pl041_ops = {
524 .write = pl041_write,
525 .endianness = DEVICE_NATIVE_ENDIAN,
528 static void pl041_init(Object *obj)
530 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
531 PL041State *s = PL041(dev);
533 DBG_L1("pl041_init 0x%08x\n", (uint32_t)s);
535 /* Connect the device to the sysbus */
536 memory_region_init_io(&s->iomem, obj, &pl041_ops, s, "pl041", 0x1000);
537 sysbus_init_mmio(dev, &s->iomem);
538 sysbus_init_irq(dev, &s->irq);
541 static void pl041_realize(DeviceState *dev, Error **errp)
543 PL041State *s = PL041(dev);
545 /* Check the device properties */
546 switch (s->fifo_depth) {
558 /* NC FIFO depth of 16 is not allowed because its id bits in
559 AACIPERIPHID3 overlap with the id for the default NC FIFO depth */
560 qemu_log_mask(LOG_UNIMP,
561 "pl041: unsupported non-compact fifo depth [%i]\n",
566 lm4549_init(&s->codec, &pl041_request_data, (void *)s);
569 static const VMStateDescription vmstate_pl041_regfile = {
570 .name = "pl041_regfile",
572 .minimum_version_id = 1,
573 .fields = (VMStateField[]) {
574 #define REGISTER(name, offset) VMSTATE_UINT32(name, pl041_regfile),
577 VMSTATE_END_OF_LIST()
581 static const VMStateDescription vmstate_pl041_fifo = {
582 .name = "pl041_fifo",
584 .minimum_version_id = 1,
585 .fields = (VMStateField[]) {
586 VMSTATE_UINT32(level, pl041_fifo),
587 VMSTATE_UINT32_ARRAY(data, pl041_fifo, MAX_FIFO_DEPTH),
588 VMSTATE_END_OF_LIST()
592 static const VMStateDescription vmstate_pl041_channel = {
593 .name = "pl041_channel",
595 .minimum_version_id = 1,
596 .fields = (VMStateField[]) {
597 VMSTATE_STRUCT(tx_fifo, pl041_channel, 0,
598 vmstate_pl041_fifo, pl041_fifo),
599 VMSTATE_UINT8(tx_enabled, pl041_channel),
600 VMSTATE_UINT8(tx_compact_mode, pl041_channel),
601 VMSTATE_UINT8(tx_sample_size, pl041_channel),
602 VMSTATE_STRUCT(rx_fifo, pl041_channel, 0,
603 vmstate_pl041_fifo, pl041_fifo),
604 VMSTATE_UINT8(rx_enabled, pl041_channel),
605 VMSTATE_UINT8(rx_compact_mode, pl041_channel),
606 VMSTATE_UINT8(rx_sample_size, pl041_channel),
607 VMSTATE_END_OF_LIST()
611 static const VMStateDescription vmstate_pl041 = {
614 .minimum_version_id = 1,
615 .fields = (VMStateField[]) {
616 VMSTATE_UINT32(fifo_depth, PL041State),
617 VMSTATE_STRUCT(regs, PL041State, 0,
618 vmstate_pl041_regfile, pl041_regfile),
619 VMSTATE_STRUCT(fifo1, PL041State, 0,
620 vmstate_pl041_channel, pl041_channel),
621 VMSTATE_STRUCT(codec, PL041State, 0,
622 vmstate_lm4549_state, lm4549_state),
623 VMSTATE_END_OF_LIST()
627 static Property pl041_device_properties[] = {
628 DEFINE_AUDIO_PROPERTIES(PL041State, codec.card),
629 /* Non-compact FIFO depth property */
630 DEFINE_PROP_UINT32("nc_fifo_depth", PL041State, fifo_depth,
632 DEFINE_PROP_END_OF_LIST(),
635 static void pl041_device_class_init(ObjectClass *klass, void *data)
637 DeviceClass *dc = DEVICE_CLASS(klass);
639 dc->realize = pl041_realize;
640 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
641 dc->reset = pl041_device_reset;
642 dc->vmsd = &vmstate_pl041;
643 dc->props = pl041_device_properties;
646 static const TypeInfo pl041_device_info = {
648 .parent = TYPE_SYS_BUS_DEVICE,
649 .instance_size = sizeof(PL041State),
650 .instance_init = pl041_init,
651 .class_init = pl041_device_class_init,
654 static void pl041_register_types(void)
656 type_register_static(&pl041_device_info);
659 type_init(pl041_register_types)