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"
33 #include "qom/object.h"
36 #define PL041_DEBUG_LEVEL 1
39 #if defined(PL041_DEBUG_LEVEL) && (PL041_DEBUG_LEVEL >= 1)
40 #define DBG_L1(fmt, ...) \
41 do { printf("pl041: " fmt , ## __VA_ARGS__); } while (0)
43 #define DBG_L1(fmt, ...) \
47 #if defined(PL041_DEBUG_LEVEL) && (PL041_DEBUG_LEVEL >= 2)
48 #define DBG_L2(fmt, ...) \
49 do { printf("pl041: " fmt , ## __VA_ARGS__); } while (0)
51 #define DBG_L2(fmt, ...) \
56 #define MAX_FIFO_DEPTH (1024)
57 #define DEFAULT_FIFO_DEPTH (8)
59 #define SLOT1_RW (1 << 19)
61 /* This FIFO only stores 20-bit samples on 32-bit words.
62 So its level is independent of the selected mode */
65 uint32_t data[MAX_FIFO_DEPTH];
71 uint8_t tx_compact_mode;
72 uint8_t tx_sample_size;
76 uint8_t rx_compact_mode;
77 uint8_t rx_sample_size;
80 #define TYPE_PL041 "pl041"
81 OBJECT_DECLARE_SIMPLE_TYPE(PL041State, PL041)
84 SysBusDevice parent_obj;
89 uint32_t fifo_depth; /* FIFO depth in non-compact mode */
97 static const unsigned char pl041_default_id[8] = {
98 0x41, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
101 #if defined(PL041_DEBUG_LEVEL)
102 #define REGISTER(name, offset) #name,
103 static const char *pl041_regs_name[] = {
110 #if defined(PL041_DEBUG_LEVEL)
111 static const char *get_reg_name(hwaddr offset)
113 if (offset <= PL041_dr1_7) {
114 return pl041_regs_name[offset >> 2];
121 static uint8_t pl041_compute_periphid3(PL041State *s)
123 uint8_t id3 = 1; /* One channel */
125 /* Add the fifo depth information */
126 switch (s->fifo_depth) {
156 static void pl041_reset(PL041State *s)
158 DBG_L1("pl041_reset\n");
160 memset(&s->regs, 0x00, sizeof(pl041_regfile));
162 s->regs.slfr = SL1TXEMPTY | SL2TXEMPTY | SL12TXEMPTY;
163 s->regs.sr1 = TXFE | RXFE | TXHE;
166 memset(&s->fifo1, 0x00, sizeof(s->fifo1));
170 static void pl041_fifo1_write(PL041State *s, uint32_t value)
172 pl041_channel *channel = &s->fifo1;
173 pl041_fifo *fifo = &s->fifo1.tx_fifo;
175 /* Push the value in the FIFO */
176 if (channel->tx_compact_mode == 0) {
177 /* Non-compact mode */
179 if (fifo->level < s->fifo_depth) {
180 /* Pad the value with 0 to obtain a 20-bit sample */
181 switch (channel->tx_sample_size) {
183 value = (value << 8) & 0xFFFFF;
186 value = (value << 4) & 0xFFFFF;
189 value = (value << 2) & 0xFFFFF;
196 /* Store the sample in the FIFO */
197 fifo->data[fifo->level++] = value;
199 #if defined(PL041_DEBUG_LEVEL)
201 DBG_L1("fifo1 write: overrun\n");
207 if ((fifo->level + 2) < s->fifo_depth) {
211 for (i = 0; i < 2; i++) {
212 sample = value & 0xFFFF;
215 /* Pad each sample with 0 to obtain a 20-bit sample */
216 switch (channel->tx_sample_size) {
218 sample = sample << 8;
222 sample = sample << 4;
226 /* Store the sample in the FIFO */
227 fifo->data[fifo->level++] = sample;
230 #if defined(PL041_DEBUG_LEVEL)
232 DBG_L1("fifo1 write: overrun\n");
237 /* Update the status register */
238 if (fifo->level > 0) {
239 s->regs.sr1 &= ~(TXUNDERRUN | TXFE);
242 if (fifo->level >= (s->fifo_depth / 2)) {
243 s->regs.sr1 &= ~TXHE;
246 if (fifo->level >= s->fifo_depth) {
250 DBG_L2("fifo1_push sr1 = 0x%08x\n", s->regs.sr1);
253 static void pl041_fifo1_transmit(PL041State *s)
255 pl041_channel *channel = &s->fifo1;
256 pl041_fifo *fifo = &s->fifo1.tx_fifo;
257 uint32_t slots = s->regs.txcr1 & TXSLOT_MASK;
258 uint32_t written_samples;
260 /* Check if FIFO1 transmit is enabled */
261 if ((channel->tx_enabled) && (slots & (TXSLOT3 | TXSLOT4))) {
262 if (fifo->level >= (s->fifo_depth / 2)) {
265 DBG_L1("Transfer FIFO level = %i\n", fifo->level);
267 /* Try to transfer the whole FIFO */
268 for (i = 0; i < (fifo->level / 2); i++) {
269 uint32_t left = fifo->data[i * 2];
270 uint32_t right = fifo->data[i * 2 + 1];
272 /* Transmit two 20-bit samples to the codec */
273 if (lm4549_write_samples(&s->codec, left, right) == 0) {
274 DBG_L1("Codec buffer full\n");
279 written_samples = i * 2;
280 if (written_samples > 0) {
281 /* Update the FIFO level */
282 fifo->level -= written_samples;
284 /* Move back the pending samples to the start of the FIFO */
285 for (i = 0; i < fifo->level; i++) {
286 fifo->data[i] = fifo->data[written_samples + i];
289 /* Update the status register */
290 s->regs.sr1 &= ~TXFF;
292 if (fifo->level <= (s->fifo_depth / 2)) {
296 if (fifo->level == 0) {
297 s->regs.sr1 |= TXFE | TXUNDERRUN;
298 DBG_L1("Empty FIFO\n");
305 static void pl041_isr1_update(PL041State *s)
308 if (s->regs.sr1 & TXUNDERRUN) {
309 s->regs.isr1 |= URINTR;
311 s->regs.isr1 &= ~URINTR;
314 if (s->regs.sr1 & TXHE) {
315 s->regs.isr1 |= TXINTR;
317 s->regs.isr1 &= ~TXINTR;
320 if (!(s->regs.sr1 & TXBUSY) && (s->regs.sr1 & TXFE)) {
321 s->regs.isr1 |= TXCINTR;
323 s->regs.isr1 &= ~TXCINTR;
326 /* Update the irq state */
327 qemu_set_irq(s->irq, ((s->regs.isr1 & s->regs.ie1) > 0) ? 1 : 0);
328 DBG_L2("Set interrupt sr1 = 0x%08x isr1 = 0x%08x masked = 0x%08x\n",
329 s->regs.sr1, s->regs.isr1, s->regs.isr1 & s->regs.ie1);
332 static void pl041_request_data(void *opaque)
334 PL041State *s = (PL041State *)opaque;
336 /* Trigger pending transfers */
337 pl041_fifo1_transmit(s);
338 pl041_isr1_update(s);
341 static uint64_t pl041_read(void *opaque, hwaddr offset,
344 PL041State *s = (PL041State *)opaque;
347 if ((offset >= PL041_periphid0) && (offset <= PL041_pcellid3)) {
348 if (offset == PL041_periphid3) {
349 value = pl041_compute_periphid3(s);
351 value = pl041_default_id[(offset - PL041_periphid0) >> 2];
354 DBG_L1("pl041_read [0x%08x] => 0x%08x\n", offset, value);
356 } else if (offset <= PL041_dr4_7) {
357 value = *((uint32_t *)&s->regs + (offset >> 2));
359 DBG_L1("pl041_read: Reserved offset %x\n", (int)offset);
365 value = s->regs.isr1 & 0x7F;
369 DBG_L1("pl041_read [0x%08x] %s => 0x%08x\n", offset,
370 get_reg_name(offset), value);
375 static void pl041_write(void *opaque, hwaddr offset,
376 uint64_t value, unsigned size)
378 PL041State *s = (PL041State *)opaque;
379 uint16_t control, data;
382 DBG_L1("pl041_write [0x%08x] %s <= 0x%08x\n", offset,
383 get_reg_name(offset), (unsigned int)value);
385 /* Write the register */
386 if (offset <= PL041_dr4_7) {
387 *((uint32_t *)&s->regs + (offset >> 2)) = value;
389 DBG_L1("pl041_write: Reserved offset %x\n", (int)offset);
393 /* Execute the actions */
397 pl041_channel *channel = &s->fifo1;
399 uint32_t txen = s->regs.txcr1 & TXEN;
400 uint32_t tsize = (s->regs.txcr1 & TSIZE_MASK) >> TSIZE_MASK_BIT;
401 uint32_t compact_mode = (s->regs.txcr1 & TXCOMPACT) ? 1 : 0;
402 #if defined(PL041_DEBUG_LEVEL)
403 uint32_t slots = (s->regs.txcr1 & TXSLOT_MASK) >> TXSLOT_MASK_BIT;
404 uint32_t txfen = (s->regs.txcr1 & TXFEN) > 0 ? 1 : 0;
407 DBG_L1("=> txen = %i slots = 0x%01x tsize = %i compact = %i "
408 "txfen = %i\n", txen, slots, tsize, compact_mode, txfen);
410 channel->tx_enabled = txen;
411 channel->tx_compact_mode = compact_mode;
415 channel->tx_sample_size = 16;
418 channel->tx_sample_size = 18;
421 channel->tx_sample_size = 20;
424 channel->tx_sample_size = 12;
428 DBG_L1("TX enabled = %i\n", channel->tx_enabled);
429 DBG_L1("TX compact mode = %i\n", channel->tx_compact_mode);
430 DBG_L1("TX sample width = %i\n", channel->tx_sample_size);
432 /* Check if compact mode is allowed with selected tsize */
433 if (channel->tx_compact_mode == 1) {
434 if ((channel->tx_sample_size == 18) ||
435 (channel->tx_sample_size == 20)) {
436 channel->tx_compact_mode = 0;
437 DBG_L1("Compact mode not allowed with 18/20-bit sample size\n");
444 s->regs.slfr &= ~SL1TXEMPTY;
446 control = (s->regs.sl1tx >> 12) & 0x7F;
447 data = (s->regs.sl2tx >> 4) & 0xFFFF;
449 if ((s->regs.sl1tx & SLOT1_RW) == 0) {
450 /* Write operation */
451 lm4549_write(&s->codec, control, data);
454 result = lm4549_read(&s->codec, control);
456 /* Store the returned value */
457 s->regs.sl1rx = s->regs.sl1tx & ~SLOT1_RW;
458 s->regs.sl2rx = result << 4;
460 s->regs.slfr &= ~(SL1RXBUSY | SL2RXBUSY);
461 s->regs.slfr |= SL1RXVALID | SL2RXVALID;
466 s->regs.sl2tx = value;
467 s->regs.slfr &= ~SL2TXEMPTY;
471 DBG_L1("=> Clear interrupt intclr = 0x%08x isr1 = 0x%08x\n",
472 s->regs.intclr, s->regs.isr1);
474 if (s->regs.intclr & TXUEC1) {
475 s->regs.sr1 &= ~TXUNDERRUN;
481 #if defined(PL041_DEBUG_LEVEL)
482 char debug[] = " AACIFE SL1RXEN SL1TXEN";
483 if (!(value & AACIFE)) {
486 if (!(value & SL1RXEN)) {
489 if (!(value & SL1TXEN)) {
492 DBG_L1("%s\n", debug);
495 if ((s->regs.maincr & AACIFE) == 0) {
505 pl041_fifo1_write(s, value);
509 /* Transmit the FIFO content */
510 pl041_fifo1_transmit(s);
512 /* Update the ISR1 register */
513 pl041_isr1_update(s);
516 static void pl041_device_reset(DeviceState *d)
518 PL041State *s = PL041(d);
523 static const MemoryRegionOps pl041_ops = {
525 .write = pl041_write,
526 .endianness = DEVICE_NATIVE_ENDIAN,
529 static void pl041_init(Object *obj)
531 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
532 PL041State *s = PL041(dev);
534 DBG_L1("pl041_init 0x%08x\n", (uint32_t)s);
536 /* Connect the device to the sysbus */
537 memory_region_init_io(&s->iomem, obj, &pl041_ops, s, "pl041", 0x1000);
538 sysbus_init_mmio(dev, &s->iomem);
539 sysbus_init_irq(dev, &s->irq);
542 static void pl041_realize(DeviceState *dev, Error **errp)
544 PL041State *s = PL041(dev);
546 /* Check the device properties */
547 switch (s->fifo_depth) {
559 /* NC FIFO depth of 16 is not allowed because its id bits in
560 AACIPERIPHID3 overlap with the id for the default NC FIFO depth */
561 qemu_log_mask(LOG_UNIMP,
562 "pl041: unsupported non-compact fifo depth [%i]\n",
567 lm4549_init(&s->codec, &pl041_request_data, (void *)s);
570 static const VMStateDescription vmstate_pl041_regfile = {
571 .name = "pl041_regfile",
573 .minimum_version_id = 1,
574 .fields = (VMStateField[]) {
575 #define REGISTER(name, offset) VMSTATE_UINT32(name, pl041_regfile),
578 VMSTATE_END_OF_LIST()
582 static const VMStateDescription vmstate_pl041_fifo = {
583 .name = "pl041_fifo",
585 .minimum_version_id = 1,
586 .fields = (VMStateField[]) {
587 VMSTATE_UINT32(level, pl041_fifo),
588 VMSTATE_UINT32_ARRAY(data, pl041_fifo, MAX_FIFO_DEPTH),
589 VMSTATE_END_OF_LIST()
593 static const VMStateDescription vmstate_pl041_channel = {
594 .name = "pl041_channel",
596 .minimum_version_id = 1,
597 .fields = (VMStateField[]) {
598 VMSTATE_STRUCT(tx_fifo, pl041_channel, 0,
599 vmstate_pl041_fifo, pl041_fifo),
600 VMSTATE_UINT8(tx_enabled, pl041_channel),
601 VMSTATE_UINT8(tx_compact_mode, pl041_channel),
602 VMSTATE_UINT8(tx_sample_size, pl041_channel),
603 VMSTATE_STRUCT(rx_fifo, pl041_channel, 0,
604 vmstate_pl041_fifo, pl041_fifo),
605 VMSTATE_UINT8(rx_enabled, pl041_channel),
606 VMSTATE_UINT8(rx_compact_mode, pl041_channel),
607 VMSTATE_UINT8(rx_sample_size, pl041_channel),
608 VMSTATE_END_OF_LIST()
612 static const VMStateDescription vmstate_pl041 = {
615 .minimum_version_id = 1,
616 .fields = (VMStateField[]) {
617 VMSTATE_UINT32(fifo_depth, PL041State),
618 VMSTATE_STRUCT(regs, PL041State, 0,
619 vmstate_pl041_regfile, pl041_regfile),
620 VMSTATE_STRUCT(fifo1, PL041State, 0,
621 vmstate_pl041_channel, pl041_channel),
622 VMSTATE_STRUCT(codec, PL041State, 0,
623 vmstate_lm4549_state, lm4549_state),
624 VMSTATE_END_OF_LIST()
628 static Property pl041_device_properties[] = {
629 DEFINE_AUDIO_PROPERTIES(PL041State, codec.card),
630 /* Non-compact FIFO depth property */
631 DEFINE_PROP_UINT32("nc_fifo_depth", PL041State, fifo_depth,
633 DEFINE_PROP_END_OF_LIST(),
636 static void pl041_device_class_init(ObjectClass *klass, void *data)
638 DeviceClass *dc = DEVICE_CLASS(klass);
640 dc->realize = pl041_realize;
641 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
642 dc->reset = pl041_device_reset;
643 dc->vmsd = &vmstate_pl041;
644 device_class_set_props(dc, pl041_device_properties);
647 static const TypeInfo pl041_device_info = {
649 .parent = TYPE_SYS_BUS_DEVICE,
650 .instance_size = sizeof(PL041State),
651 .instance_init = pl041_init,
652 .class_init = pl041_device_class_init,
655 static void pl041_register_types(void)
657 type_register_static(&pl041_device_info);
660 type_init(pl041_register_types)