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.
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
;
78 uint32_t fifo_depth
; /* FIFO depth in non-compact mode */
86 static const unsigned char pl041_default_id
[8] = {
87 0x41, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
90 #if defined(PL041_DEBUG_LEVEL)
91 #define REGISTER(name, offset) #name,
92 static const char *pl041_regs_name
[] = {
99 #if defined(PL041_DEBUG_LEVEL)
100 static const char *get_reg_name(hwaddr offset
)
102 if (offset
<= PL041_dr1_7
) {
103 return pl041_regs_name
[offset
>> 2];
110 static uint8_t pl041_compute_periphid3(pl041_state
*s
)
112 uint8_t id3
= 1; /* One channel */
114 /* Add the fifo depth information */
115 switch (s
->fifo_depth
) {
145 static void pl041_reset(pl041_state
*s
)
147 DBG_L1("pl041_reset\n");
149 memset(&s
->regs
, 0x00, sizeof(pl041_regfile
));
151 s
->regs
.slfr
= SL1TXEMPTY
| SL2TXEMPTY
| SL12TXEMPTY
;
152 s
->regs
.sr1
= TXFE
| RXFE
| TXHE
;
155 memset(&s
->fifo1
, 0x00, sizeof(s
->fifo1
));
159 static void pl041_fifo1_write(pl041_state
*s
, uint32_t value
)
161 pl041_channel
*channel
= &s
->fifo1
;
162 pl041_fifo
*fifo
= &s
->fifo1
.tx_fifo
;
164 /* Push the value in the FIFO */
165 if (channel
->tx_compact_mode
== 0) {
166 /* Non-compact mode */
168 if (fifo
->level
< s
->fifo_depth
) {
169 /* Pad the value with 0 to obtain a 20-bit sample */
170 switch (channel
->tx_sample_size
) {
172 value
= (value
<< 8) & 0xFFFFF;
175 value
= (value
<< 4) & 0xFFFFF;
178 value
= (value
<< 2) & 0xFFFFF;
185 /* Store the sample in the FIFO */
186 fifo
->data
[fifo
->level
++] = value
;
188 #if defined(PL041_DEBUG_LEVEL)
190 DBG_L1("fifo1 write: overrun\n");
196 if ((fifo
->level
+ 2) < s
->fifo_depth
) {
200 for (i
= 0; i
< 2; i
++) {
201 sample
= value
& 0xFFFF;
204 /* Pad each sample with 0 to obtain a 20-bit sample */
205 switch (channel
->tx_sample_size
) {
207 sample
= sample
<< 8;
211 sample
= sample
<< 4;
215 /* Store the sample in the FIFO */
216 fifo
->data
[fifo
->level
++] = sample
;
219 #if defined(PL041_DEBUG_LEVEL)
221 DBG_L1("fifo1 write: overrun\n");
226 /* Update the status register */
227 if (fifo
->level
> 0) {
228 s
->regs
.sr1
&= ~(TXUNDERRUN
| TXFE
);
231 if (fifo
->level
>= (s
->fifo_depth
/ 2)) {
232 s
->regs
.sr1
&= ~TXHE
;
235 if (fifo
->level
>= s
->fifo_depth
) {
239 DBG_L2("fifo1_push sr1 = 0x%08x\n", s
->regs
.sr1
);
242 static void pl041_fifo1_transmit(pl041_state
*s
)
244 pl041_channel
*channel
= &s
->fifo1
;
245 pl041_fifo
*fifo
= &s
->fifo1
.tx_fifo
;
246 uint32_t slots
= s
->regs
.txcr1
& TXSLOT_MASK
;
247 uint32_t written_samples
;
249 /* Check if FIFO1 transmit is enabled */
250 if ((channel
->tx_enabled
) && (slots
& (TXSLOT3
| TXSLOT4
))) {
251 if (fifo
->level
>= (s
->fifo_depth
/ 2)) {
254 DBG_L1("Transfer FIFO level = %i\n", fifo
->level
);
256 /* Try to transfer the whole FIFO */
257 for (i
= 0; i
< (fifo
->level
/ 2); i
++) {
258 uint32_t left
= fifo
->data
[i
* 2];
259 uint32_t right
= fifo
->data
[i
* 2 + 1];
261 /* Transmit two 20-bit samples to the codec */
262 if (lm4549_write_samples(&s
->codec
, left
, right
) == 0) {
263 DBG_L1("Codec buffer full\n");
268 written_samples
= i
* 2;
269 if (written_samples
> 0) {
270 /* Update the FIFO level */
271 fifo
->level
-= written_samples
;
273 /* Move back the pending samples to the start of the FIFO */
274 for (i
= 0; i
< fifo
->level
; i
++) {
275 fifo
->data
[i
] = fifo
->data
[written_samples
+ i
];
278 /* Update the status register */
279 s
->regs
.sr1
&= ~TXFF
;
281 if (fifo
->level
<= (s
->fifo_depth
/ 2)) {
285 if (fifo
->level
== 0) {
286 s
->regs
.sr1
|= TXFE
| TXUNDERRUN
;
287 DBG_L1("Empty FIFO\n");
294 static void pl041_isr1_update(pl041_state
*s
)
297 if (s
->regs
.sr1
& TXUNDERRUN
) {
298 s
->regs
.isr1
|= URINTR
;
300 s
->regs
.isr1
&= ~URINTR
;
303 if (s
->regs
.sr1
& TXHE
) {
304 s
->regs
.isr1
|= TXINTR
;
306 s
->regs
.isr1
&= ~TXINTR
;
309 if (!(s
->regs
.sr1
& TXBUSY
) && (s
->regs
.sr1
& TXFE
)) {
310 s
->regs
.isr1
|= TXCINTR
;
312 s
->regs
.isr1
&= ~TXCINTR
;
315 /* Update the irq state */
316 qemu_set_irq(s
->irq
, ((s
->regs
.isr1
& s
->regs
.ie1
) > 0) ? 1 : 0);
317 DBG_L2("Set interrupt sr1 = 0x%08x isr1 = 0x%08x masked = 0x%08x\n",
318 s
->regs
.sr1
, s
->regs
.isr1
, s
->regs
.isr1
& s
->regs
.ie1
);
321 static void pl041_request_data(void *opaque
)
323 pl041_state
*s
= (pl041_state
*)opaque
;
325 /* Trigger pending transfers */
326 pl041_fifo1_transmit(s
);
327 pl041_isr1_update(s
);
330 static uint64_t pl041_read(void *opaque
, hwaddr offset
,
333 pl041_state
*s
= (pl041_state
*)opaque
;
336 if ((offset
>= PL041_periphid0
) && (offset
<= PL041_pcellid3
)) {
337 if (offset
== PL041_periphid3
) {
338 value
= pl041_compute_periphid3(s
);
340 value
= pl041_default_id
[(offset
- PL041_periphid0
) >> 2];
343 DBG_L1("pl041_read [0x%08x] => 0x%08x\n", offset
, value
);
345 } else if (offset
<= PL041_dr4_7
) {
346 value
= *((uint32_t *)&s
->regs
+ (offset
>> 2));
348 DBG_L1("pl041_read: Reserved offset %x\n", (int)offset
);
354 value
= s
->regs
.isr1
& 0x7F;
358 DBG_L1("pl041_read [0x%08x] %s => 0x%08x\n", offset
,
359 get_reg_name(offset
), value
);
364 static void pl041_write(void *opaque
, hwaddr offset
,
365 uint64_t value
, unsigned size
)
367 pl041_state
*s
= (pl041_state
*)opaque
;
368 uint16_t control
, data
;
371 DBG_L1("pl041_write [0x%08x] %s <= 0x%08x\n", offset
,
372 get_reg_name(offset
), (unsigned int)value
);
374 /* Write the register */
375 if (offset
<= PL041_dr4_7
) {
376 *((uint32_t *)&s
->regs
+ (offset
>> 2)) = value
;
378 DBG_L1("pl041_write: Reserved offset %x\n", (int)offset
);
382 /* Execute the actions */
386 pl041_channel
*channel
= &s
->fifo1
;
388 uint32_t txen
= s
->regs
.txcr1
& TXEN
;
389 uint32_t tsize
= (s
->regs
.txcr1
& TSIZE_MASK
) >> TSIZE_MASK_BIT
;
390 uint32_t compact_mode
= (s
->regs
.txcr1
& TXCOMPACT
) ? 1 : 0;
391 #if defined(PL041_DEBUG_LEVEL)
392 uint32_t slots
= (s
->regs
.txcr1
& TXSLOT_MASK
) >> TXSLOT_MASK_BIT
;
393 uint32_t txfen
= (s
->regs
.txcr1
& TXFEN
) > 0 ? 1 : 0;
396 DBG_L1("=> txen = %i slots = 0x%01x tsize = %i compact = %i "
397 "txfen = %i\n", txen
, slots
, tsize
, compact_mode
, txfen
);
399 channel
->tx_enabled
= txen
;
400 channel
->tx_compact_mode
= compact_mode
;
404 channel
->tx_sample_size
= 16;
407 channel
->tx_sample_size
= 18;
410 channel
->tx_sample_size
= 20;
413 channel
->tx_sample_size
= 12;
417 DBG_L1("TX enabled = %i\n", channel
->tx_enabled
);
418 DBG_L1("TX compact mode = %i\n", channel
->tx_compact_mode
);
419 DBG_L1("TX sample width = %i\n", channel
->tx_sample_size
);
421 /* Check if compact mode is allowed with selected tsize */
422 if (channel
->tx_compact_mode
== 1) {
423 if ((channel
->tx_sample_size
== 18) ||
424 (channel
->tx_sample_size
== 20)) {
425 channel
->tx_compact_mode
= 0;
426 DBG_L1("Compact mode not allowed with 18/20-bit sample size\n");
433 s
->regs
.slfr
&= ~SL1TXEMPTY
;
435 control
= (s
->regs
.sl1tx
>> 12) & 0x7F;
436 data
= (s
->regs
.sl2tx
>> 4) & 0xFFFF;
438 if ((s
->regs
.sl1tx
& SLOT1_RW
) == 0) {
439 /* Write operation */
440 lm4549_write(&s
->codec
, control
, data
);
443 result
= lm4549_read(&s
->codec
, control
);
445 /* Store the returned value */
446 s
->regs
.sl1rx
= s
->regs
.sl1tx
& ~SLOT1_RW
;
447 s
->regs
.sl2rx
= result
<< 4;
449 s
->regs
.slfr
&= ~(SL1RXBUSY
| SL2RXBUSY
);
450 s
->regs
.slfr
|= SL1RXVALID
| SL2RXVALID
;
455 s
->regs
.sl2tx
= value
;
456 s
->regs
.slfr
&= ~SL2TXEMPTY
;
460 DBG_L1("=> Clear interrupt intclr = 0x%08x isr1 = 0x%08x\n",
461 s
->regs
.intclr
, s
->regs
.isr1
);
463 if (s
->regs
.intclr
& TXUEC1
) {
464 s
->regs
.sr1
&= ~TXUNDERRUN
;
470 #if defined(PL041_DEBUG_LEVEL)
471 char debug
[] = " AACIFE SL1RXEN SL1TXEN";
472 if (!(value
& AACIFE
)) {
475 if (!(value
& SL1RXEN
)) {
478 if (!(value
& SL1TXEN
)) {
481 DBG_L1("%s\n", debug
);
484 if ((s
->regs
.maincr
& AACIFE
) == 0) {
494 pl041_fifo1_write(s
, value
);
498 /* Transmit the FIFO content */
499 pl041_fifo1_transmit(s
);
501 /* Update the ISR1 register */
502 pl041_isr1_update(s
);
505 static void pl041_device_reset(DeviceState
*d
)
507 pl041_state
*s
= DO_UPCAST(pl041_state
, busdev
.qdev
, d
);
512 static const MemoryRegionOps pl041_ops
= {
514 .write
= pl041_write
,
515 .endianness
= DEVICE_NATIVE_ENDIAN
,
518 static int pl041_init(SysBusDevice
*dev
)
520 pl041_state
*s
= FROM_SYSBUS(pl041_state
, dev
);
522 DBG_L1("pl041_init 0x%08x\n", (uint32_t)s
);
524 /* Check the device properties */
525 switch (s
->fifo_depth
) {
537 /* NC FIFO depth of 16 is not allowed because its id bits in
538 AACIPERIPHID3 overlap with the id for the default NC FIFO depth */
539 qemu_log_mask(LOG_UNIMP
,
540 "pl041: unsupported non-compact fifo depth [%i]\n",
545 /* Connect the device to the sysbus */
546 memory_region_init_io(&s
->iomem
, &pl041_ops
, s
, "pl041", 0x1000);
547 sysbus_init_mmio(dev
, &s
->iomem
);
548 sysbus_init_irq(dev
, &s
->irq
);
551 lm4549_init(&s
->codec
, &pl041_request_data
, (void *)s
);
556 static const VMStateDescription vmstate_pl041_regfile
= {
557 .name
= "pl041_regfile",
559 .minimum_version_id
= 1,
560 .minimum_version_id_old
= 1,
561 .fields
= (VMStateField
[]) {
562 #define REGISTER(name, offset) VMSTATE_UINT32(name, pl041_regfile),
565 VMSTATE_END_OF_LIST()
569 static const VMStateDescription vmstate_pl041_fifo
= {
570 .name
= "pl041_fifo",
572 .minimum_version_id
= 1,
573 .minimum_version_id_old
= 1,
574 .fields
= (VMStateField
[]) {
575 VMSTATE_UINT32(level
, pl041_fifo
),
576 VMSTATE_UINT32_ARRAY(data
, pl041_fifo
, MAX_FIFO_DEPTH
),
577 VMSTATE_END_OF_LIST()
581 static const VMStateDescription vmstate_pl041_channel
= {
582 .name
= "pl041_channel",
584 .minimum_version_id
= 1,
585 .minimum_version_id_old
= 1,
586 .fields
= (VMStateField
[]) {
587 VMSTATE_STRUCT(tx_fifo
, pl041_channel
, 0,
588 vmstate_pl041_fifo
, pl041_fifo
),
589 VMSTATE_UINT8(tx_enabled
, pl041_channel
),
590 VMSTATE_UINT8(tx_compact_mode
, pl041_channel
),
591 VMSTATE_UINT8(tx_sample_size
, pl041_channel
),
592 VMSTATE_STRUCT(rx_fifo
, pl041_channel
, 0,
593 vmstate_pl041_fifo
, pl041_fifo
),
594 VMSTATE_UINT8(rx_enabled
, pl041_channel
),
595 VMSTATE_UINT8(rx_compact_mode
, pl041_channel
),
596 VMSTATE_UINT8(rx_sample_size
, pl041_channel
),
597 VMSTATE_END_OF_LIST()
601 static const VMStateDescription vmstate_pl041
= {
604 .minimum_version_id
= 1,
605 .fields
= (VMStateField
[]) {
606 VMSTATE_UINT32(fifo_depth
, pl041_state
),
607 VMSTATE_STRUCT(regs
, pl041_state
, 0,
608 vmstate_pl041_regfile
, pl041_regfile
),
609 VMSTATE_STRUCT(fifo1
, pl041_state
, 0,
610 vmstate_pl041_channel
, pl041_channel
),
611 VMSTATE_STRUCT(codec
, pl041_state
, 0,
612 vmstate_lm4549_state
, lm4549_state
),
613 VMSTATE_END_OF_LIST()
617 static Property pl041_device_properties
[] = {
618 /* Non-compact FIFO depth property */
619 DEFINE_PROP_UINT32("nc_fifo_depth", pl041_state
, fifo_depth
, DEFAULT_FIFO_DEPTH
),
620 DEFINE_PROP_END_OF_LIST(),
623 static void pl041_device_class_init(ObjectClass
*klass
, void *data
)
625 DeviceClass
*dc
= DEVICE_CLASS(klass
);
626 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
628 k
->init
= pl041_init
;
630 dc
->reset
= pl041_device_reset
;
631 dc
->vmsd
= &vmstate_pl041
;
632 dc
->props
= pl041_device_properties
;
635 static const TypeInfo pl041_device_info
= {
637 .parent
= TYPE_SYS_BUS_DEVICE
,
638 .instance_size
= sizeof(pl041_state
),
639 .class_init
= pl041_device_class_init
,
642 static void pl041_register_types(void)
644 type_register_static(&pl041_device_info
);
647 type_init(pl041_register_types
)