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/sysbus.h"
27 #include "qemu/module.h"
33 #define PL041_DEBUG_LEVEL 1
36 #if defined(PL041_DEBUG_LEVEL) && (PL041_DEBUG_LEVEL >= 1)
37 #define DBG_L1(fmt, ...) \
38 do { printf("pl041: " fmt , ## __VA_ARGS__); } while (0)
40 #define DBG_L1(fmt, ...) \
44 #if defined(PL041_DEBUG_LEVEL) && (PL041_DEBUG_LEVEL >= 2)
45 #define DBG_L2(fmt, ...) \
46 do { printf("pl041: " fmt , ## __VA_ARGS__); } while (0)
48 #define DBG_L2(fmt, ...) \
53 #define MAX_FIFO_DEPTH (1024)
54 #define DEFAULT_FIFO_DEPTH (8)
56 #define SLOT1_RW (1 << 19)
58 /* This FIFO only stores 20-bit samples on 32-bit words.
59 So its level is independent of the selected mode */
62 uint32_t data[MAX_FIFO_DEPTH];
68 uint8_t tx_compact_mode;
69 uint8_t tx_sample_size;
73 uint8_t rx_compact_mode;
74 uint8_t rx_sample_size;
77 #define TYPE_PL041 "pl041"
78 #define PL041(obj) OBJECT_CHECK(PL041State, (obj), TYPE_PL041)
80 typedef struct PL041State {
81 SysBusDevice parent_obj;
86 uint32_t fifo_depth; /* FIFO depth in non-compact mode */
94 static const unsigned char pl041_default_id[8] = {
95 0x41, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
98 #if defined(PL041_DEBUG_LEVEL)
99 #define REGISTER(name, offset) #name,
100 static const char *pl041_regs_name[] = {
107 #if defined(PL041_DEBUG_LEVEL)
108 static const char *get_reg_name(hwaddr offset)
110 if (offset <= PL041_dr1_7) {
111 return pl041_regs_name[offset >> 2];
118 static uint8_t pl041_compute_periphid3(PL041State *s)
120 uint8_t id3 = 1; /* One channel */
122 /* Add the fifo depth information */
123 switch (s->fifo_depth) {
153 static void pl041_reset(PL041State *s)
155 DBG_L1("pl041_reset\n");
157 memset(&s->regs, 0x00, sizeof(pl041_regfile));
159 s->regs.slfr = SL1TXEMPTY | SL2TXEMPTY | SL12TXEMPTY;
160 s->regs.sr1 = TXFE | RXFE | TXHE;
163 memset(&s->fifo1, 0x00, sizeof(s->fifo1));
167 static void pl041_fifo1_write(PL041State *s, uint32_t value)
169 pl041_channel *channel = &s->fifo1;
170 pl041_fifo *fifo = &s->fifo1.tx_fifo;
172 /* Push the value in the FIFO */
173 if (channel->tx_compact_mode == 0) {
174 /* Non-compact mode */
176 if (fifo->level < s->fifo_depth) {
177 /* Pad the value with 0 to obtain a 20-bit sample */
178 switch (channel->tx_sample_size) {
180 value = (value << 8) & 0xFFFFF;
183 value = (value << 4) & 0xFFFFF;
186 value = (value << 2) & 0xFFFFF;
193 /* Store the sample in the FIFO */
194 fifo->data[fifo->level++] = value;
196 #if defined(PL041_DEBUG_LEVEL)
198 DBG_L1("fifo1 write: overrun\n");
204 if ((fifo->level + 2) < s->fifo_depth) {
208 for (i = 0; i < 2; i++) {
209 sample = value & 0xFFFF;
212 /* Pad each sample with 0 to obtain a 20-bit sample */
213 switch (channel->tx_sample_size) {
215 sample = sample << 8;
219 sample = sample << 4;
223 /* Store the sample in the FIFO */
224 fifo->data[fifo->level++] = sample;
227 #if defined(PL041_DEBUG_LEVEL)
229 DBG_L1("fifo1 write: overrun\n");
234 /* Update the status register */
235 if (fifo->level > 0) {
236 s->regs.sr1 &= ~(TXUNDERRUN | TXFE);
239 if (fifo->level >= (s->fifo_depth / 2)) {
240 s->regs.sr1 &= ~TXHE;
243 if (fifo->level >= s->fifo_depth) {
247 DBG_L2("fifo1_push sr1 = 0x%08x\n", s->regs.sr1);
250 static void pl041_fifo1_transmit(PL041State *s)
252 pl041_channel *channel = &s->fifo1;
253 pl041_fifo *fifo = &s->fifo1.tx_fifo;
254 uint32_t slots = s->regs.txcr1 & TXSLOT_MASK;
255 uint32_t written_samples;
257 /* Check if FIFO1 transmit is enabled */
258 if ((channel->tx_enabled) && (slots & (TXSLOT3 | TXSLOT4))) {
259 if (fifo->level >= (s->fifo_depth / 2)) {
262 DBG_L1("Transfer FIFO level = %i\n", fifo->level);
264 /* Try to transfer the whole FIFO */
265 for (i = 0; i < (fifo->level / 2); i++) {
266 uint32_t left = fifo->data[i * 2];
267 uint32_t right = fifo->data[i * 2 + 1];
269 /* Transmit two 20-bit samples to the codec */
270 if (lm4549_write_samples(&s->codec, left, right) == 0) {
271 DBG_L1("Codec buffer full\n");
276 written_samples = i * 2;
277 if (written_samples > 0) {
278 /* Update the FIFO level */
279 fifo->level -= written_samples;
281 /* Move back the pending samples to the start of the FIFO */
282 for (i = 0; i < fifo->level; i++) {
283 fifo->data[i] = fifo->data[written_samples + i];
286 /* Update the status register */
287 s->regs.sr1 &= ~TXFF;
289 if (fifo->level <= (s->fifo_depth / 2)) {
293 if (fifo->level == 0) {
294 s->regs.sr1 |= TXFE | TXUNDERRUN;
295 DBG_L1("Empty FIFO\n");
302 static void pl041_isr1_update(PL041State *s)
305 if (s->regs.sr1 & TXUNDERRUN) {
306 s->regs.isr1 |= URINTR;
308 s->regs.isr1 &= ~URINTR;
311 if (s->regs.sr1 & TXHE) {
312 s->regs.isr1 |= TXINTR;
314 s->regs.isr1 &= ~TXINTR;
317 if (!(s->regs.sr1 & TXBUSY) && (s->regs.sr1 & TXFE)) {
318 s->regs.isr1 |= TXCINTR;
320 s->regs.isr1 &= ~TXCINTR;
323 /* Update the irq state */
324 qemu_set_irq(s->irq, ((s->regs.isr1 & s->regs.ie1) > 0) ? 1 : 0);
325 DBG_L2("Set interrupt sr1 = 0x%08x isr1 = 0x%08x masked = 0x%08x\n",
326 s->regs.sr1, s->regs.isr1, s->regs.isr1 & s->regs.ie1);
329 static void pl041_request_data(void *opaque)
331 PL041State *s = (PL041State *)opaque;
333 /* Trigger pending transfers */
334 pl041_fifo1_transmit(s);
335 pl041_isr1_update(s);
338 static uint64_t pl041_read(void *opaque, hwaddr offset,
341 PL041State *s = (PL041State *)opaque;
344 if ((offset >= PL041_periphid0) && (offset <= PL041_pcellid3)) {
345 if (offset == PL041_periphid3) {
346 value = pl041_compute_periphid3(s);
348 value = pl041_default_id[(offset - PL041_periphid0) >> 2];
351 DBG_L1("pl041_read [0x%08x] => 0x%08x\n", offset, value);
353 } else if (offset <= PL041_dr4_7) {
354 value = *((uint32_t *)&s->regs + (offset >> 2));
356 DBG_L1("pl041_read: Reserved offset %x\n", (int)offset);
362 value = s->regs.isr1 & 0x7F;
366 DBG_L1("pl041_read [0x%08x] %s => 0x%08x\n", offset,
367 get_reg_name(offset), value);
372 static void pl041_write(void *opaque, hwaddr offset,
373 uint64_t value, unsigned size)
375 PL041State *s = (PL041State *)opaque;
376 uint16_t control, data;
379 DBG_L1("pl041_write [0x%08x] %s <= 0x%08x\n", offset,
380 get_reg_name(offset), (unsigned int)value);
382 /* Write the register */
383 if (offset <= PL041_dr4_7) {
384 *((uint32_t *)&s->regs + (offset >> 2)) = value;
386 DBG_L1("pl041_write: Reserved offset %x\n", (int)offset);
390 /* Execute the actions */
394 pl041_channel *channel = &s->fifo1;
396 uint32_t txen = s->regs.txcr1 & TXEN;
397 uint32_t tsize = (s->regs.txcr1 & TSIZE_MASK) >> TSIZE_MASK_BIT;
398 uint32_t compact_mode = (s->regs.txcr1 & TXCOMPACT) ? 1 : 0;
399 #if defined(PL041_DEBUG_LEVEL)
400 uint32_t slots = (s->regs.txcr1 & TXSLOT_MASK) >> TXSLOT_MASK_BIT;
401 uint32_t txfen = (s->regs.txcr1 & TXFEN) > 0 ? 1 : 0;
404 DBG_L1("=> txen = %i slots = 0x%01x tsize = %i compact = %i "
405 "txfen = %i\n", txen, slots, tsize, compact_mode, txfen);
407 channel->tx_enabled = txen;
408 channel->tx_compact_mode = compact_mode;
412 channel->tx_sample_size = 16;
415 channel->tx_sample_size = 18;
418 channel->tx_sample_size = 20;
421 channel->tx_sample_size = 12;
425 DBG_L1("TX enabled = %i\n", channel->tx_enabled);
426 DBG_L1("TX compact mode = %i\n", channel->tx_compact_mode);
427 DBG_L1("TX sample width = %i\n", channel->tx_sample_size);
429 /* Check if compact mode is allowed with selected tsize */
430 if (channel->tx_compact_mode == 1) {
431 if ((channel->tx_sample_size == 18) ||
432 (channel->tx_sample_size == 20)) {
433 channel->tx_compact_mode = 0;
434 DBG_L1("Compact mode not allowed with 18/20-bit sample size\n");
441 s->regs.slfr &= ~SL1TXEMPTY;
443 control = (s->regs.sl1tx >> 12) & 0x7F;
444 data = (s->regs.sl2tx >> 4) & 0xFFFF;
446 if ((s->regs.sl1tx & SLOT1_RW) == 0) {
447 /* Write operation */
448 lm4549_write(&s->codec, control, data);
451 result = lm4549_read(&s->codec, control);
453 /* Store the returned value */
454 s->regs.sl1rx = s->regs.sl1tx & ~SLOT1_RW;
455 s->regs.sl2rx = result << 4;
457 s->regs.slfr &= ~(SL1RXBUSY | SL2RXBUSY);
458 s->regs.slfr |= SL1RXVALID | SL2RXVALID;
463 s->regs.sl2tx = value;
464 s->regs.slfr &= ~SL2TXEMPTY;
468 DBG_L1("=> Clear interrupt intclr = 0x%08x isr1 = 0x%08x\n",
469 s->regs.intclr, s->regs.isr1);
471 if (s->regs.intclr & TXUEC1) {
472 s->regs.sr1 &= ~TXUNDERRUN;
478 #if defined(PL041_DEBUG_LEVEL)
479 char debug[] = " AACIFE SL1RXEN SL1TXEN";
480 if (!(value & AACIFE)) {
483 if (!(value & SL1RXEN)) {
486 if (!(value & SL1TXEN)) {
489 DBG_L1("%s\n", debug);
492 if ((s->regs.maincr & AACIFE) == 0) {
502 pl041_fifo1_write(s, value);
506 /* Transmit the FIFO content */
507 pl041_fifo1_transmit(s);
509 /* Update the ISR1 register */
510 pl041_isr1_update(s);
513 static void pl041_device_reset(DeviceState *d)
515 PL041State *s = PL041(d);
520 static const MemoryRegionOps pl041_ops = {
522 .write = pl041_write,
523 .endianness = DEVICE_NATIVE_ENDIAN,
526 static void pl041_init(Object *obj)
528 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
529 PL041State *s = PL041(dev);
531 DBG_L1("pl041_init 0x%08x\n", (uint32_t)s);
533 /* Connect the device to the sysbus */
534 memory_region_init_io(&s->iomem, obj, &pl041_ops, s, "pl041", 0x1000);
535 sysbus_init_mmio(dev, &s->iomem);
536 sysbus_init_irq(dev, &s->irq);
539 static void pl041_realize(DeviceState *dev, Error **errp)
541 PL041State *s = PL041(dev);
543 /* Check the device properties */
544 switch (s->fifo_depth) {
556 /* NC FIFO depth of 16 is not allowed because its id bits in
557 AACIPERIPHID3 overlap with the id for the default NC FIFO depth */
558 qemu_log_mask(LOG_UNIMP,
559 "pl041: unsupported non-compact fifo depth [%i]\n",
564 lm4549_init(&s->codec, &pl041_request_data, (void *)s);
567 static const VMStateDescription vmstate_pl041_regfile = {
568 .name = "pl041_regfile",
570 .minimum_version_id = 1,
571 .fields = (VMStateField[]) {
572 #define REGISTER(name, offset) VMSTATE_UINT32(name, pl041_regfile),
575 VMSTATE_END_OF_LIST()
579 static const VMStateDescription vmstate_pl041_fifo = {
580 .name = "pl041_fifo",
582 .minimum_version_id = 1,
583 .fields = (VMStateField[]) {
584 VMSTATE_UINT32(level, pl041_fifo),
585 VMSTATE_UINT32_ARRAY(data, pl041_fifo, MAX_FIFO_DEPTH),
586 VMSTATE_END_OF_LIST()
590 static const VMStateDescription vmstate_pl041_channel = {
591 .name = "pl041_channel",
593 .minimum_version_id = 1,
594 .fields = (VMStateField[]) {
595 VMSTATE_STRUCT(tx_fifo, pl041_channel, 0,
596 vmstate_pl041_fifo, pl041_fifo),
597 VMSTATE_UINT8(tx_enabled, pl041_channel),
598 VMSTATE_UINT8(tx_compact_mode, pl041_channel),
599 VMSTATE_UINT8(tx_sample_size, pl041_channel),
600 VMSTATE_STRUCT(rx_fifo, pl041_channel, 0,
601 vmstate_pl041_fifo, pl041_fifo),
602 VMSTATE_UINT8(rx_enabled, pl041_channel),
603 VMSTATE_UINT8(rx_compact_mode, pl041_channel),
604 VMSTATE_UINT8(rx_sample_size, pl041_channel),
605 VMSTATE_END_OF_LIST()
609 static const VMStateDescription vmstate_pl041 = {
612 .minimum_version_id = 1,
613 .fields = (VMStateField[]) {
614 VMSTATE_UINT32(fifo_depth, PL041State),
615 VMSTATE_STRUCT(regs, PL041State, 0,
616 vmstate_pl041_regfile, pl041_regfile),
617 VMSTATE_STRUCT(fifo1, PL041State, 0,
618 vmstate_pl041_channel, pl041_channel),
619 VMSTATE_STRUCT(codec, PL041State, 0,
620 vmstate_lm4549_state, lm4549_state),
621 VMSTATE_END_OF_LIST()
625 static Property pl041_device_properties[] = {
626 /* Non-compact FIFO depth property */
627 DEFINE_PROP_UINT32("nc_fifo_depth", PL041State, fifo_depth,
629 DEFINE_PROP_END_OF_LIST(),
632 static void pl041_device_class_init(ObjectClass *klass, void *data)
634 DeviceClass *dc = DEVICE_CLASS(klass);
636 dc->realize = pl041_realize;
637 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
638 dc->reset = pl041_device_reset;
639 dc->vmsd = &vmstate_pl041;
640 dc->props = pl041_device_properties;
643 static const TypeInfo pl041_device_info = {
645 .parent = TYPE_SYS_BUS_DEVICE,
646 .instance_size = sizeof(PL041State),
647 .instance_init = pl041_init,
648 .class_init = pl041_device_class_init,
651 static void pl041_register_types(void)
653 type_register_static(&pl041_device_info);
656 type_init(pl041_register_types)