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 "hw/sysbus.h"
29 #define PL041_DEBUG_LEVEL 1
32 #if defined(PL041_DEBUG_LEVEL) && (PL041_DEBUG_LEVEL >= 1)
33 #define DBG_L1(fmt, ...) \
34 do { printf("pl041: " fmt , ## __VA_ARGS__); } while (0)
36 #define DBG_L1(fmt, ...) \
40 #if defined(PL041_DEBUG_LEVEL) && (PL041_DEBUG_LEVEL >= 2)
41 #define DBG_L2(fmt, ...) \
42 do { printf("pl041: " fmt , ## __VA_ARGS__); } while (0)
44 #define DBG_L2(fmt, ...) \
49 #define MAX_FIFO_DEPTH (1024)
50 #define DEFAULT_FIFO_DEPTH (8)
52 #define SLOT1_RW (1 << 19)
54 /* This FIFO only stores 20-bit samples on 32-bit words.
55 So its level is independent of the selected mode */
58 uint32_t data
[MAX_FIFO_DEPTH
];
64 uint8_t tx_compact_mode
;
65 uint8_t tx_sample_size
;
69 uint8_t rx_compact_mode
;
70 uint8_t rx_sample_size
;
73 #define TYPE_PL041 "pl041"
74 #define PL041(obj) OBJECT_CHECK(PL041State, (obj), TYPE_PL041)
76 typedef struct PL041State
{
77 SysBusDevice parent_obj
;
82 uint32_t fifo_depth
; /* FIFO depth in non-compact mode */
90 static const unsigned char pl041_default_id
[8] = {
91 0x41, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
94 #if defined(PL041_DEBUG_LEVEL)
95 #define REGISTER(name, offset) #name,
96 static const char *pl041_regs_name
[] = {
103 #if defined(PL041_DEBUG_LEVEL)
104 static const char *get_reg_name(hwaddr offset
)
106 if (offset
<= PL041_dr1_7
) {
107 return pl041_regs_name
[offset
>> 2];
114 static uint8_t pl041_compute_periphid3(PL041State
*s
)
116 uint8_t id3
= 1; /* One channel */
118 /* Add the fifo depth information */
119 switch (s
->fifo_depth
) {
149 static void pl041_reset(PL041State
*s
)
151 DBG_L1("pl041_reset\n");
153 memset(&s
->regs
, 0x00, sizeof(pl041_regfile
));
155 s
->regs
.slfr
= SL1TXEMPTY
| SL2TXEMPTY
| SL12TXEMPTY
;
156 s
->regs
.sr1
= TXFE
| RXFE
| TXHE
;
159 memset(&s
->fifo1
, 0x00, sizeof(s
->fifo1
));
163 static void pl041_fifo1_write(PL041State
*s
, uint32_t value
)
165 pl041_channel
*channel
= &s
->fifo1
;
166 pl041_fifo
*fifo
= &s
->fifo1
.tx_fifo
;
168 /* Push the value in the FIFO */
169 if (channel
->tx_compact_mode
== 0) {
170 /* Non-compact mode */
172 if (fifo
->level
< s
->fifo_depth
) {
173 /* Pad the value with 0 to obtain a 20-bit sample */
174 switch (channel
->tx_sample_size
) {
176 value
= (value
<< 8) & 0xFFFFF;
179 value
= (value
<< 4) & 0xFFFFF;
182 value
= (value
<< 2) & 0xFFFFF;
189 /* Store the sample in the FIFO */
190 fifo
->data
[fifo
->level
++] = value
;
192 #if defined(PL041_DEBUG_LEVEL)
194 DBG_L1("fifo1 write: overrun\n");
200 if ((fifo
->level
+ 2) < s
->fifo_depth
) {
204 for (i
= 0; i
< 2; i
++) {
205 sample
= value
& 0xFFFF;
208 /* Pad each sample with 0 to obtain a 20-bit sample */
209 switch (channel
->tx_sample_size
) {
211 sample
= sample
<< 8;
215 sample
= sample
<< 4;
219 /* Store the sample in the FIFO */
220 fifo
->data
[fifo
->level
++] = sample
;
223 #if defined(PL041_DEBUG_LEVEL)
225 DBG_L1("fifo1 write: overrun\n");
230 /* Update the status register */
231 if (fifo
->level
> 0) {
232 s
->regs
.sr1
&= ~(TXUNDERRUN
| TXFE
);
235 if (fifo
->level
>= (s
->fifo_depth
/ 2)) {
236 s
->regs
.sr1
&= ~TXHE
;
239 if (fifo
->level
>= s
->fifo_depth
) {
243 DBG_L2("fifo1_push sr1 = 0x%08x\n", s
->regs
.sr1
);
246 static void pl041_fifo1_transmit(PL041State
*s
)
248 pl041_channel
*channel
= &s
->fifo1
;
249 pl041_fifo
*fifo
= &s
->fifo1
.tx_fifo
;
250 uint32_t slots
= s
->regs
.txcr1
& TXSLOT_MASK
;
251 uint32_t written_samples
;
253 /* Check if FIFO1 transmit is enabled */
254 if ((channel
->tx_enabled
) && (slots
& (TXSLOT3
| TXSLOT4
))) {
255 if (fifo
->level
>= (s
->fifo_depth
/ 2)) {
258 DBG_L1("Transfer FIFO level = %i\n", fifo
->level
);
260 /* Try to transfer the whole FIFO */
261 for (i
= 0; i
< (fifo
->level
/ 2); i
++) {
262 uint32_t left
= fifo
->data
[i
* 2];
263 uint32_t right
= fifo
->data
[i
* 2 + 1];
265 /* Transmit two 20-bit samples to the codec */
266 if (lm4549_write_samples(&s
->codec
, left
, right
) == 0) {
267 DBG_L1("Codec buffer full\n");
272 written_samples
= i
* 2;
273 if (written_samples
> 0) {
274 /* Update the FIFO level */
275 fifo
->level
-= written_samples
;
277 /* Move back the pending samples to the start of the FIFO */
278 for (i
= 0; i
< fifo
->level
; i
++) {
279 fifo
->data
[i
] = fifo
->data
[written_samples
+ i
];
282 /* Update the status register */
283 s
->regs
.sr1
&= ~TXFF
;
285 if (fifo
->level
<= (s
->fifo_depth
/ 2)) {
289 if (fifo
->level
== 0) {
290 s
->regs
.sr1
|= TXFE
| TXUNDERRUN
;
291 DBG_L1("Empty FIFO\n");
298 static void pl041_isr1_update(PL041State
*s
)
301 if (s
->regs
.sr1
& TXUNDERRUN
) {
302 s
->regs
.isr1
|= URINTR
;
304 s
->regs
.isr1
&= ~URINTR
;
307 if (s
->regs
.sr1
& TXHE
) {
308 s
->regs
.isr1
|= TXINTR
;
310 s
->regs
.isr1
&= ~TXINTR
;
313 if (!(s
->regs
.sr1
& TXBUSY
) && (s
->regs
.sr1
& TXFE
)) {
314 s
->regs
.isr1
|= TXCINTR
;
316 s
->regs
.isr1
&= ~TXCINTR
;
319 /* Update the irq state */
320 qemu_set_irq(s
->irq
, ((s
->regs
.isr1
& s
->regs
.ie1
) > 0) ? 1 : 0);
321 DBG_L2("Set interrupt sr1 = 0x%08x isr1 = 0x%08x masked = 0x%08x\n",
322 s
->regs
.sr1
, s
->regs
.isr1
, s
->regs
.isr1
& s
->regs
.ie1
);
325 static void pl041_request_data(void *opaque
)
327 PL041State
*s
= (PL041State
*)opaque
;
329 /* Trigger pending transfers */
330 pl041_fifo1_transmit(s
);
331 pl041_isr1_update(s
);
334 static uint64_t pl041_read(void *opaque
, hwaddr offset
,
337 PL041State
*s
= (PL041State
*)opaque
;
340 if ((offset
>= PL041_periphid0
) && (offset
<= PL041_pcellid3
)) {
341 if (offset
== PL041_periphid3
) {
342 value
= pl041_compute_periphid3(s
);
344 value
= pl041_default_id
[(offset
- PL041_periphid0
) >> 2];
347 DBG_L1("pl041_read [0x%08x] => 0x%08x\n", offset
, value
);
349 } else if (offset
<= PL041_dr4_7
) {
350 value
= *((uint32_t *)&s
->regs
+ (offset
>> 2));
352 DBG_L1("pl041_read: Reserved offset %x\n", (int)offset
);
358 value
= s
->regs
.isr1
& 0x7F;
362 DBG_L1("pl041_read [0x%08x] %s => 0x%08x\n", offset
,
363 get_reg_name(offset
), value
);
368 static void pl041_write(void *opaque
, hwaddr offset
,
369 uint64_t value
, unsigned size
)
371 PL041State
*s
= (PL041State
*)opaque
;
372 uint16_t control
, data
;
375 DBG_L1("pl041_write [0x%08x] %s <= 0x%08x\n", offset
,
376 get_reg_name(offset
), (unsigned int)value
);
378 /* Write the register */
379 if (offset
<= PL041_dr4_7
) {
380 *((uint32_t *)&s
->regs
+ (offset
>> 2)) = value
;
382 DBG_L1("pl041_write: Reserved offset %x\n", (int)offset
);
386 /* Execute the actions */
390 pl041_channel
*channel
= &s
->fifo1
;
392 uint32_t txen
= s
->regs
.txcr1
& TXEN
;
393 uint32_t tsize
= (s
->regs
.txcr1
& TSIZE_MASK
) >> TSIZE_MASK_BIT
;
394 uint32_t compact_mode
= (s
->regs
.txcr1
& TXCOMPACT
) ? 1 : 0;
395 #if defined(PL041_DEBUG_LEVEL)
396 uint32_t slots
= (s
->regs
.txcr1
& TXSLOT_MASK
) >> TXSLOT_MASK_BIT
;
397 uint32_t txfen
= (s
->regs
.txcr1
& TXFEN
) > 0 ? 1 : 0;
400 DBG_L1("=> txen = %i slots = 0x%01x tsize = %i compact = %i "
401 "txfen = %i\n", txen
, slots
, tsize
, compact_mode
, txfen
);
403 channel
->tx_enabled
= txen
;
404 channel
->tx_compact_mode
= compact_mode
;
408 channel
->tx_sample_size
= 16;
411 channel
->tx_sample_size
= 18;
414 channel
->tx_sample_size
= 20;
417 channel
->tx_sample_size
= 12;
421 DBG_L1("TX enabled = %i\n", channel
->tx_enabled
);
422 DBG_L1("TX compact mode = %i\n", channel
->tx_compact_mode
);
423 DBG_L1("TX sample width = %i\n", channel
->tx_sample_size
);
425 /* Check if compact mode is allowed with selected tsize */
426 if (channel
->tx_compact_mode
== 1) {
427 if ((channel
->tx_sample_size
== 18) ||
428 (channel
->tx_sample_size
== 20)) {
429 channel
->tx_compact_mode
= 0;
430 DBG_L1("Compact mode not allowed with 18/20-bit sample size\n");
437 s
->regs
.slfr
&= ~SL1TXEMPTY
;
439 control
= (s
->regs
.sl1tx
>> 12) & 0x7F;
440 data
= (s
->regs
.sl2tx
>> 4) & 0xFFFF;
442 if ((s
->regs
.sl1tx
& SLOT1_RW
) == 0) {
443 /* Write operation */
444 lm4549_write(&s
->codec
, control
, data
);
447 result
= lm4549_read(&s
->codec
, control
);
449 /* Store the returned value */
450 s
->regs
.sl1rx
= s
->regs
.sl1tx
& ~SLOT1_RW
;
451 s
->regs
.sl2rx
= result
<< 4;
453 s
->regs
.slfr
&= ~(SL1RXBUSY
| SL2RXBUSY
);
454 s
->regs
.slfr
|= SL1RXVALID
| SL2RXVALID
;
459 s
->regs
.sl2tx
= value
;
460 s
->regs
.slfr
&= ~SL2TXEMPTY
;
464 DBG_L1("=> Clear interrupt intclr = 0x%08x isr1 = 0x%08x\n",
465 s
->regs
.intclr
, s
->regs
.isr1
);
467 if (s
->regs
.intclr
& TXUEC1
) {
468 s
->regs
.sr1
&= ~TXUNDERRUN
;
474 #if defined(PL041_DEBUG_LEVEL)
475 char debug
[] = " AACIFE SL1RXEN SL1TXEN";
476 if (!(value
& AACIFE
)) {
479 if (!(value
& SL1RXEN
)) {
482 if (!(value
& SL1TXEN
)) {
485 DBG_L1("%s\n", debug
);
488 if ((s
->regs
.maincr
& AACIFE
) == 0) {
498 pl041_fifo1_write(s
, value
);
502 /* Transmit the FIFO content */
503 pl041_fifo1_transmit(s
);
505 /* Update the ISR1 register */
506 pl041_isr1_update(s
);
509 static void pl041_device_reset(DeviceState
*d
)
511 PL041State
*s
= PL041(d
);
516 static const MemoryRegionOps pl041_ops
= {
518 .write
= pl041_write
,
519 .endianness
= DEVICE_NATIVE_ENDIAN
,
522 static int pl041_init(SysBusDevice
*dev
)
524 PL041State
*s
= PL041(dev
);
526 DBG_L1("pl041_init 0x%08x\n", (uint32_t)s
);
528 /* Check the device properties */
529 switch (s
->fifo_depth
) {
541 /* NC FIFO depth of 16 is not allowed because its id bits in
542 AACIPERIPHID3 overlap with the id for the default NC FIFO depth */
543 qemu_log_mask(LOG_UNIMP
,
544 "pl041: unsupported non-compact fifo depth [%i]\n",
549 /* Connect the device to the sysbus */
550 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl041_ops
, s
, "pl041", 0x1000);
551 sysbus_init_mmio(dev
, &s
->iomem
);
552 sysbus_init_irq(dev
, &s
->irq
);
555 lm4549_init(&s
->codec
, &pl041_request_data
, (void *)s
);
560 static const VMStateDescription vmstate_pl041_regfile
= {
561 .name
= "pl041_regfile",
563 .minimum_version_id
= 1,
564 .minimum_version_id_old
= 1,
565 .fields
= (VMStateField
[]) {
566 #define REGISTER(name, offset) VMSTATE_UINT32(name, pl041_regfile),
569 VMSTATE_END_OF_LIST()
573 static const VMStateDescription vmstate_pl041_fifo
= {
574 .name
= "pl041_fifo",
576 .minimum_version_id
= 1,
577 .minimum_version_id_old
= 1,
578 .fields
= (VMStateField
[]) {
579 VMSTATE_UINT32(level
, pl041_fifo
),
580 VMSTATE_UINT32_ARRAY(data
, pl041_fifo
, MAX_FIFO_DEPTH
),
581 VMSTATE_END_OF_LIST()
585 static const VMStateDescription vmstate_pl041_channel
= {
586 .name
= "pl041_channel",
588 .minimum_version_id
= 1,
589 .minimum_version_id_old
= 1,
590 .fields
= (VMStateField
[]) {
591 VMSTATE_STRUCT(tx_fifo
, pl041_channel
, 0,
592 vmstate_pl041_fifo
, pl041_fifo
),
593 VMSTATE_UINT8(tx_enabled
, pl041_channel
),
594 VMSTATE_UINT8(tx_compact_mode
, pl041_channel
),
595 VMSTATE_UINT8(tx_sample_size
, pl041_channel
),
596 VMSTATE_STRUCT(rx_fifo
, pl041_channel
, 0,
597 vmstate_pl041_fifo
, pl041_fifo
),
598 VMSTATE_UINT8(rx_enabled
, pl041_channel
),
599 VMSTATE_UINT8(rx_compact_mode
, pl041_channel
),
600 VMSTATE_UINT8(rx_sample_size
, pl041_channel
),
601 VMSTATE_END_OF_LIST()
605 static const VMStateDescription vmstate_pl041
= {
608 .minimum_version_id
= 1,
609 .fields
= (VMStateField
[]) {
610 VMSTATE_UINT32(fifo_depth
, PL041State
),
611 VMSTATE_STRUCT(regs
, PL041State
, 0,
612 vmstate_pl041_regfile
, pl041_regfile
),
613 VMSTATE_STRUCT(fifo1
, PL041State
, 0,
614 vmstate_pl041_channel
, pl041_channel
),
615 VMSTATE_STRUCT(codec
, PL041State
, 0,
616 vmstate_lm4549_state
, lm4549_state
),
617 VMSTATE_END_OF_LIST()
621 static Property pl041_device_properties
[] = {
622 /* Non-compact FIFO depth property */
623 DEFINE_PROP_UINT32("nc_fifo_depth", PL041State
, fifo_depth
,
625 DEFINE_PROP_END_OF_LIST(),
628 static void pl041_device_class_init(ObjectClass
*klass
, void *data
)
630 DeviceClass
*dc
= DEVICE_CLASS(klass
);
631 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
633 k
->init
= pl041_init
;
634 set_bit(DEVICE_CATEGORY_SOUND
, dc
->categories
);
635 dc
->reset
= pl041_device_reset
;
636 dc
->vmsd
= &vmstate_pl041
;
637 dc
->props
= pl041_device_properties
;
640 static const TypeInfo pl041_device_info
= {
642 .parent
= TYPE_SYS_BUS_DEVICE
,
643 .instance_size
= sizeof(PL041State
),
644 .class_init
= pl041_device_class_init
,
647 static void pl041_register_types(void)
649 type_register_static(&pl041_device_info
);
652 type_init(pl041_register_types
)