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"
24 #include "hw/sysbus.h"
31 #define PL041_DEBUG_LEVEL 1
34 #if defined(PL041_DEBUG_LEVEL) && (PL041_DEBUG_LEVEL >= 1)
35 #define DBG_L1(fmt, ...) \
36 do { printf("pl041: " fmt , ## __VA_ARGS__); } while (0)
38 #define DBG_L1(fmt, ...) \
42 #if defined(PL041_DEBUG_LEVEL) && (PL041_DEBUG_LEVEL >= 2)
43 #define DBG_L2(fmt, ...) \
44 do { printf("pl041: " fmt , ## __VA_ARGS__); } while (0)
46 #define DBG_L2(fmt, ...) \
51 #define MAX_FIFO_DEPTH (1024)
52 #define DEFAULT_FIFO_DEPTH (8)
54 #define SLOT1_RW (1 << 19)
56 /* This FIFO only stores 20-bit samples on 32-bit words.
57 So its level is independent of the selected mode */
60 uint32_t data[MAX_FIFO_DEPTH];
66 uint8_t tx_compact_mode;
67 uint8_t tx_sample_size;
71 uint8_t rx_compact_mode;
72 uint8_t rx_sample_size;
75 #define TYPE_PL041 "pl041"
76 #define PL041(obj) OBJECT_CHECK(PL041State, (obj), TYPE_PL041)
78 typedef struct PL041State {
79 SysBusDevice parent_obj;
84 uint32_t fifo_depth; /* FIFO depth in non-compact mode */
92 static const unsigned char pl041_default_id[8] = {
93 0x41, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
96 #if defined(PL041_DEBUG_LEVEL)
97 #define REGISTER(name, offset) #name,
98 static const char *pl041_regs_name[] = {
105 #if defined(PL041_DEBUG_LEVEL)
106 static const char *get_reg_name(hwaddr offset)
108 if (offset <= PL041_dr1_7) {
109 return pl041_regs_name[offset >> 2];
116 static uint8_t pl041_compute_periphid3(PL041State *s)
118 uint8_t id3 = 1; /* One channel */
120 /* Add the fifo depth information */
121 switch (s->fifo_depth) {
151 static void pl041_reset(PL041State *s)
153 DBG_L1("pl041_reset\n");
155 memset(&s->regs, 0x00, sizeof(pl041_regfile));
157 s->regs.slfr = SL1TXEMPTY | SL2TXEMPTY | SL12TXEMPTY;
158 s->regs.sr1 = TXFE | RXFE | TXHE;
161 memset(&s->fifo1, 0x00, sizeof(s->fifo1));
165 static void pl041_fifo1_write(PL041State *s, uint32_t value)
167 pl041_channel *channel = &s->fifo1;
168 pl041_fifo *fifo = &s->fifo1.tx_fifo;
170 /* Push the value in the FIFO */
171 if (channel->tx_compact_mode == 0) {
172 /* Non-compact mode */
174 if (fifo->level < s->fifo_depth) {
175 /* Pad the value with 0 to obtain a 20-bit sample */
176 switch (channel->tx_sample_size) {
178 value = (value << 8) & 0xFFFFF;
181 value = (value << 4) & 0xFFFFF;
184 value = (value << 2) & 0xFFFFF;
191 /* Store the sample in the FIFO */
192 fifo->data[fifo->level++] = value;
194 #if defined(PL041_DEBUG_LEVEL)
196 DBG_L1("fifo1 write: overrun\n");
202 if ((fifo->level + 2) < s->fifo_depth) {
206 for (i = 0; i < 2; i++) {
207 sample = value & 0xFFFF;
210 /* Pad each sample with 0 to obtain a 20-bit sample */
211 switch (channel->tx_sample_size) {
213 sample = sample << 8;
217 sample = sample << 4;
221 /* Store the sample in the FIFO */
222 fifo->data[fifo->level++] = sample;
225 #if defined(PL041_DEBUG_LEVEL)
227 DBG_L1("fifo1 write: overrun\n");
232 /* Update the status register */
233 if (fifo->level > 0) {
234 s->regs.sr1 &= ~(TXUNDERRUN | TXFE);
237 if (fifo->level >= (s->fifo_depth / 2)) {
238 s->regs.sr1 &= ~TXHE;
241 if (fifo->level >= s->fifo_depth) {
245 DBG_L2("fifo1_push sr1 = 0x%08x\n", s->regs.sr1);
248 static void pl041_fifo1_transmit(PL041State *s)
250 pl041_channel *channel = &s->fifo1;
251 pl041_fifo *fifo = &s->fifo1.tx_fifo;
252 uint32_t slots = s->regs.txcr1 & TXSLOT_MASK;
253 uint32_t written_samples;
255 /* Check if FIFO1 transmit is enabled */
256 if ((channel->tx_enabled) && (slots & (TXSLOT3 | TXSLOT4))) {
257 if (fifo->level >= (s->fifo_depth / 2)) {
260 DBG_L1("Transfer FIFO level = %i\n", fifo->level);
262 /* Try to transfer the whole FIFO */
263 for (i = 0; i < (fifo->level / 2); i++) {
264 uint32_t left = fifo->data[i * 2];
265 uint32_t right = fifo->data[i * 2 + 1];
267 /* Transmit two 20-bit samples to the codec */
268 if (lm4549_write_samples(&s->codec, left, right) == 0) {
269 DBG_L1("Codec buffer full\n");
274 written_samples = i * 2;
275 if (written_samples > 0) {
276 /* Update the FIFO level */
277 fifo->level -= written_samples;
279 /* Move back the pending samples to the start of the FIFO */
280 for (i = 0; i < fifo->level; i++) {
281 fifo->data[i] = fifo->data[written_samples + i];
284 /* Update the status register */
285 s->regs.sr1 &= ~TXFF;
287 if (fifo->level <= (s->fifo_depth / 2)) {
291 if (fifo->level == 0) {
292 s->regs.sr1 |= TXFE | TXUNDERRUN;
293 DBG_L1("Empty FIFO\n");
300 static void pl041_isr1_update(PL041State *s)
303 if (s->regs.sr1 & TXUNDERRUN) {
304 s->regs.isr1 |= URINTR;
306 s->regs.isr1 &= ~URINTR;
309 if (s->regs.sr1 & TXHE) {
310 s->regs.isr1 |= TXINTR;
312 s->regs.isr1 &= ~TXINTR;
315 if (!(s->regs.sr1 & TXBUSY) && (s->regs.sr1 & TXFE)) {
316 s->regs.isr1 |= TXCINTR;
318 s->regs.isr1 &= ~TXCINTR;
321 /* Update the irq state */
322 qemu_set_irq(s->irq, ((s->regs.isr1 & s->regs.ie1) > 0) ? 1 : 0);
323 DBG_L2("Set interrupt sr1 = 0x%08x isr1 = 0x%08x masked = 0x%08x\n",
324 s->regs.sr1, s->regs.isr1, s->regs.isr1 & s->regs.ie1);
327 static void pl041_request_data(void *opaque)
329 PL041State *s = (PL041State *)opaque;
331 /* Trigger pending transfers */
332 pl041_fifo1_transmit(s);
333 pl041_isr1_update(s);
336 static uint64_t pl041_read(void *opaque, hwaddr offset,
339 PL041State *s = (PL041State *)opaque;
342 if ((offset >= PL041_periphid0) && (offset <= PL041_pcellid3)) {
343 if (offset == PL041_periphid3) {
344 value = pl041_compute_periphid3(s);
346 value = pl041_default_id[(offset - PL041_periphid0) >> 2];
349 DBG_L1("pl041_read [0x%08x] => 0x%08x\n", offset, value);
351 } else if (offset <= PL041_dr4_7) {
352 value = *((uint32_t *)&s->regs + (offset >> 2));
354 DBG_L1("pl041_read: Reserved offset %x\n", (int)offset);
360 value = s->regs.isr1 & 0x7F;
364 DBG_L1("pl041_read [0x%08x] %s => 0x%08x\n", offset,
365 get_reg_name(offset), value);
370 static void pl041_write(void *opaque, hwaddr offset,
371 uint64_t value, unsigned size)
373 PL041State *s = (PL041State *)opaque;
374 uint16_t control, data;
377 DBG_L1("pl041_write [0x%08x] %s <= 0x%08x\n", offset,
378 get_reg_name(offset), (unsigned int)value);
380 /* Write the register */
381 if (offset <= PL041_dr4_7) {
382 *((uint32_t *)&s->regs + (offset >> 2)) = value;
384 DBG_L1("pl041_write: Reserved offset %x\n", (int)offset);
388 /* Execute the actions */
392 pl041_channel *channel = &s->fifo1;
394 uint32_t txen = s->regs.txcr1 & TXEN;
395 uint32_t tsize = (s->regs.txcr1 & TSIZE_MASK) >> TSIZE_MASK_BIT;
396 uint32_t compact_mode = (s->regs.txcr1 & TXCOMPACT) ? 1 : 0;
397 #if defined(PL041_DEBUG_LEVEL)
398 uint32_t slots = (s->regs.txcr1 & TXSLOT_MASK) >> TXSLOT_MASK_BIT;
399 uint32_t txfen = (s->regs.txcr1 & TXFEN) > 0 ? 1 : 0;
402 DBG_L1("=> txen = %i slots = 0x%01x tsize = %i compact = %i "
403 "txfen = %i\n", txen, slots, tsize, compact_mode, txfen);
405 channel->tx_enabled = txen;
406 channel->tx_compact_mode = compact_mode;
410 channel->tx_sample_size = 16;
413 channel->tx_sample_size = 18;
416 channel->tx_sample_size = 20;
419 channel->tx_sample_size = 12;
423 DBG_L1("TX enabled = %i\n", channel->tx_enabled);
424 DBG_L1("TX compact mode = %i\n", channel->tx_compact_mode);
425 DBG_L1("TX sample width = %i\n", channel->tx_sample_size);
427 /* Check if compact mode is allowed with selected tsize */
428 if (channel->tx_compact_mode == 1) {
429 if ((channel->tx_sample_size == 18) ||
430 (channel->tx_sample_size == 20)) {
431 channel->tx_compact_mode = 0;
432 DBG_L1("Compact mode not allowed with 18/20-bit sample size\n");
439 s->regs.slfr &= ~SL1TXEMPTY;
441 control = (s->regs.sl1tx >> 12) & 0x7F;
442 data = (s->regs.sl2tx >> 4) & 0xFFFF;
444 if ((s->regs.sl1tx & SLOT1_RW) == 0) {
445 /* Write operation */
446 lm4549_write(&s->codec, control, data);
449 result = lm4549_read(&s->codec, control);
451 /* Store the returned value */
452 s->regs.sl1rx = s->regs.sl1tx & ~SLOT1_RW;
453 s->regs.sl2rx = result << 4;
455 s->regs.slfr &= ~(SL1RXBUSY | SL2RXBUSY);
456 s->regs.slfr |= SL1RXVALID | SL2RXVALID;
461 s->regs.sl2tx = value;
462 s->regs.slfr &= ~SL2TXEMPTY;
466 DBG_L1("=> Clear interrupt intclr = 0x%08x isr1 = 0x%08x\n",
467 s->regs.intclr, s->regs.isr1);
469 if (s->regs.intclr & TXUEC1) {
470 s->regs.sr1 &= ~TXUNDERRUN;
476 #if defined(PL041_DEBUG_LEVEL)
477 char debug[] = " AACIFE SL1RXEN SL1TXEN";
478 if (!(value & AACIFE)) {
481 if (!(value & SL1RXEN)) {
484 if (!(value & SL1TXEN)) {
487 DBG_L1("%s\n", debug);
490 if ((s->regs.maincr & AACIFE) == 0) {
500 pl041_fifo1_write(s, value);
504 /* Transmit the FIFO content */
505 pl041_fifo1_transmit(s);
507 /* Update the ISR1 register */
508 pl041_isr1_update(s);
511 static void pl041_device_reset(DeviceState *d)
513 PL041State *s = PL041(d);
518 static const MemoryRegionOps pl041_ops = {
520 .write = pl041_write,
521 .endianness = DEVICE_NATIVE_ENDIAN,
524 static void pl041_init(Object *obj)
526 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
527 PL041State *s = PL041(dev);
529 DBG_L1("pl041_init 0x%08x\n", (uint32_t)s);
531 /* Connect the device to the sysbus */
532 memory_region_init_io(&s->iomem, obj, &pl041_ops, s, "pl041", 0x1000);
533 sysbus_init_mmio(dev, &s->iomem);
534 sysbus_init_irq(dev, &s->irq);
537 static void pl041_realize(DeviceState *dev, Error **errp)
539 PL041State *s = PL041(dev);
541 /* Check the device properties */
542 switch (s->fifo_depth) {
554 /* NC FIFO depth of 16 is not allowed because its id bits in
555 AACIPERIPHID3 overlap with the id for the default NC FIFO depth */
556 qemu_log_mask(LOG_UNIMP,
557 "pl041: unsupported non-compact fifo depth [%i]\n",
562 lm4549_init(&s->codec, &pl041_request_data, (void *)s);
565 static const VMStateDescription vmstate_pl041_regfile = {
566 .name = "pl041_regfile",
568 .minimum_version_id = 1,
569 .fields = (VMStateField[]) {
570 #define REGISTER(name, offset) VMSTATE_UINT32(name, pl041_regfile),
573 VMSTATE_END_OF_LIST()
577 static const VMStateDescription vmstate_pl041_fifo = {
578 .name = "pl041_fifo",
580 .minimum_version_id = 1,
581 .fields = (VMStateField[]) {
582 VMSTATE_UINT32(level, pl041_fifo),
583 VMSTATE_UINT32_ARRAY(data, pl041_fifo, MAX_FIFO_DEPTH),
584 VMSTATE_END_OF_LIST()
588 static const VMStateDescription vmstate_pl041_channel = {
589 .name = "pl041_channel",
591 .minimum_version_id = 1,
592 .fields = (VMStateField[]) {
593 VMSTATE_STRUCT(tx_fifo, pl041_channel, 0,
594 vmstate_pl041_fifo, pl041_fifo),
595 VMSTATE_UINT8(tx_enabled, pl041_channel),
596 VMSTATE_UINT8(tx_compact_mode, pl041_channel),
597 VMSTATE_UINT8(tx_sample_size, pl041_channel),
598 VMSTATE_STRUCT(rx_fifo, pl041_channel, 0,
599 vmstate_pl041_fifo, pl041_fifo),
600 VMSTATE_UINT8(rx_enabled, pl041_channel),
601 VMSTATE_UINT8(rx_compact_mode, pl041_channel),
602 VMSTATE_UINT8(rx_sample_size, pl041_channel),
603 VMSTATE_END_OF_LIST()
607 static const VMStateDescription vmstate_pl041 = {
610 .minimum_version_id = 1,
611 .fields = (VMStateField[]) {
612 VMSTATE_UINT32(fifo_depth, PL041State),
613 VMSTATE_STRUCT(regs, PL041State, 0,
614 vmstate_pl041_regfile, pl041_regfile),
615 VMSTATE_STRUCT(fifo1, PL041State, 0,
616 vmstate_pl041_channel, pl041_channel),
617 VMSTATE_STRUCT(codec, PL041State, 0,
618 vmstate_lm4549_state, lm4549_state),
619 VMSTATE_END_OF_LIST()
623 static Property pl041_device_properties[] = {
624 /* Non-compact FIFO depth property */
625 DEFINE_PROP_UINT32("nc_fifo_depth", PL041State, fifo_depth,
627 DEFINE_PROP_END_OF_LIST(),
630 static void pl041_device_class_init(ObjectClass *klass, void *data)
632 DeviceClass *dc = DEVICE_CLASS(klass);
634 dc->realize = pl041_realize;
635 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
636 dc->reset = pl041_device_reset;
637 dc->vmsd = &vmstate_pl041;
638 dc->props = pl041_device_properties;
641 static const TypeInfo pl041_device_info = {
643 .parent = TYPE_SYS_BUS_DEVICE,
644 .instance_size = sizeof(PL041State),
645 .instance_init = pl041_init,
646 .class_init = pl041_device_class_init,
649 static void pl041_register_types(void)
651 type_register_static(&pl041_device_info);
654 type_init(pl041_register_types)