2 * Marvell MV88W8618 / Freecom MusicPal emulation.
4 * Copyright (c) 2008 Jan Kiszka
6 * This code is licenced under the GNU GPL v2.
16 #include "qemu-timer.h"
20 #include "audio/audio.h"
23 #define MP_MISC_BASE 0x80002000
24 #define MP_MISC_SIZE 0x00001000
26 #define MP_ETH_BASE 0x80008000
27 #define MP_ETH_SIZE 0x00001000
29 #define MP_WLAN_BASE 0x8000C000
30 #define MP_WLAN_SIZE 0x00000800
32 #define MP_UART1_BASE 0x8000C840
33 #define MP_UART2_BASE 0x8000C940
35 #define MP_GPIO_BASE 0x8000D000
36 #define MP_GPIO_SIZE 0x00001000
38 #define MP_FLASHCFG_BASE 0x90006000
39 #define MP_FLASHCFG_SIZE 0x00001000
41 #define MP_AUDIO_BASE 0x90007000
42 #define MP_AUDIO_SIZE 0x00001000
44 #define MP_PIC_BASE 0x90008000
45 #define MP_PIC_SIZE 0x00001000
47 #define MP_PIT_BASE 0x90009000
48 #define MP_PIT_SIZE 0x00001000
50 #define MP_LCD_BASE 0x9000c000
51 #define MP_LCD_SIZE 0x00001000
53 #define MP_SRAM_BASE 0xC0000000
54 #define MP_SRAM_SIZE 0x00020000
56 #define MP_RAM_DEFAULT_SIZE 32*1024*1024
57 #define MP_FLASH_SIZE_MAX 32*1024*1024
59 #define MP_TIMER1_IRQ 4
61 #define MP_TIMER4_IRQ 7
64 #define MP_UART1_IRQ 11
65 #define MP_UART2_IRQ 11
66 #define MP_GPIO_IRQ 12
68 #define MP_AUDIO_IRQ 30
70 static uint32_t gpio_in_state
= 0xffffffff;
71 static uint32_t gpio_isr
;
72 static uint32_t gpio_out_state
;
73 static ram_addr_t sram_off
;
75 typedef enum i2c_state
{
98 typedef struct i2c_interface
{
107 static void i2c_enter_stop(i2c_interface
*i2c
)
109 if (i2c
->current_addr
>= 0)
110 i2c_end_transfer(i2c
->bus
);
111 i2c
->current_addr
= -1;
112 i2c
->state
= STOPPED
;
115 static void i2c_state_update(i2c_interface
*i2c
, int data
, int clock
)
120 switch (i2c
->state
) {
122 if (data
== 0 && i2c
->last_data
== 1 && clock
== 1)
123 i2c
->state
= INITIALIZING
;
127 if (clock
== 0 && i2c
->last_clock
== 1 && data
== 0)
128 i2c
->state
= SENDING_BIT7
;
133 case SENDING_BIT7
... SENDING_BIT0
:
134 if (clock
== 0 && i2c
->last_clock
== 1) {
135 i2c
->buffer
= (i2c
->buffer
<< 1) | data
;
136 i2c
->state
++; /* will end up in WAITING_FOR_ACK */
137 } else if (data
== 1 && i2c
->last_data
== 0 && clock
== 1)
141 case WAITING_FOR_ACK
:
142 if (clock
== 0 && i2c
->last_clock
== 1) {
143 if (i2c
->current_addr
< 0) {
144 i2c
->current_addr
= i2c
->buffer
;
145 i2c_start_transfer(i2c
->bus
, i2c
->current_addr
& 0xfe,
148 i2c_send(i2c
->bus
, i2c
->buffer
);
149 if (i2c
->current_addr
& 1) {
150 i2c
->state
= RECEIVING_BIT7
;
151 i2c
->buffer
= i2c_recv(i2c
->bus
);
153 i2c
->state
= SENDING_BIT7
;
154 } else if (data
== 1 && i2c
->last_data
== 0 && clock
== 1)
158 case RECEIVING_BIT7
... RECEIVING_BIT0
:
159 if (clock
== 0 && i2c
->last_clock
== 1) {
160 i2c
->state
++; /* will end up in SENDING_ACK */
162 } else if (data
== 1 && i2c
->last_data
== 0 && clock
== 1)
167 if (clock
== 0 && i2c
->last_clock
== 1) {
168 i2c
->state
= RECEIVING_BIT7
;
170 i2c
->buffer
= i2c_recv(i2c
->bus
);
173 } else if (data
== 1 && i2c
->last_data
== 0 && clock
== 1)
178 i2c
->last_data
= data
;
179 i2c
->last_clock
= clock
;
182 static int i2c_get_data(i2c_interface
*i2c
)
187 switch (i2c
->state
) {
188 case RECEIVING_BIT7
... RECEIVING_BIT0
:
189 return (i2c
->buffer
>> 7);
191 case WAITING_FOR_ACK
:
197 static i2c_interface
*mixer_i2c
;
201 /* Audio register offsets */
202 #define MP_AUDIO_PLAYBACK_MODE 0x00
203 #define MP_AUDIO_CLOCK_DIV 0x18
204 #define MP_AUDIO_IRQ_STATUS 0x20
205 #define MP_AUDIO_IRQ_ENABLE 0x24
206 #define MP_AUDIO_TX_START_LO 0x28
207 #define MP_AUDIO_TX_THRESHOLD 0x2C
208 #define MP_AUDIO_TX_STATUS 0x38
209 #define MP_AUDIO_TX_START_HI 0x40
211 /* Status register and IRQ enable bits */
212 #define MP_AUDIO_TX_HALF (1 << 6)
213 #define MP_AUDIO_TX_FULL (1 << 7)
215 /* Playback mode bits */
216 #define MP_AUDIO_16BIT_SAMPLE (1 << 0)
217 #define MP_AUDIO_PLAYBACK_EN (1 << 7)
218 #define MP_AUDIO_CLOCK_24MHZ (1 << 9)
219 #define MP_AUDIO_MONO (1 << 14)
221 /* Wolfson 8750 I2C address */
222 #define MP_WM_ADDR 0x34
224 static const char audio_name
[] = "mv88w8618";
226 typedef struct musicpal_audio_state
{
228 uint32_t playback_mode
;
231 unsigned long phys_buf
;
232 uint32_t target_buffer
;
233 unsigned int threshold
;
234 unsigned int play_pos
;
235 unsigned int last_free
;
238 } musicpal_audio_state
;
240 static void audio_callback(void *opaque
, int free_out
, int free_in
)
242 musicpal_audio_state
*s
= opaque
;
243 int16_t *codec_buffer
;
248 if (!(s
->playback_mode
& MP_AUDIO_PLAYBACK_EN
))
251 if (s
->playback_mode
& MP_AUDIO_16BIT_SAMPLE
)
254 if (!(s
->playback_mode
& MP_AUDIO_MONO
))
257 block_size
= s
->threshold
/2;
258 if (free_out
- s
->last_free
< block_size
)
261 if (block_size
> 4096)
264 cpu_physical_memory_read(s
->target_buffer
+ s
->play_pos
, (void *)buf
,
267 if (s
->playback_mode
& MP_AUDIO_16BIT_SAMPLE
) {
268 if (s
->playback_mode
& MP_AUDIO_MONO
) {
269 codec_buffer
= wm8750_dac_buffer(s
->wm
, block_size
>> 1);
270 for (pos
= 0; pos
< block_size
; pos
+= 2) {
271 *codec_buffer
++ = *(int16_t *)mem_buffer
;
272 *codec_buffer
++ = *(int16_t *)mem_buffer
;
276 memcpy(wm8750_dac_buffer(s
->wm
, block_size
>> 2),
277 (uint32_t *)mem_buffer
, block_size
);
279 if (s
->playback_mode
& MP_AUDIO_MONO
) {
280 codec_buffer
= wm8750_dac_buffer(s
->wm
, block_size
);
281 for (pos
= 0; pos
< block_size
; pos
++) {
282 *codec_buffer
++ = cpu_to_le16(256 * *mem_buffer
);
283 *codec_buffer
++ = cpu_to_le16(256 * *mem_buffer
++);
286 codec_buffer
= wm8750_dac_buffer(s
->wm
, block_size
>> 1);
287 for (pos
= 0; pos
< block_size
; pos
+= 2) {
288 *codec_buffer
++ = cpu_to_le16(256 * *mem_buffer
++);
289 *codec_buffer
++ = cpu_to_le16(256 * *mem_buffer
++);
293 wm8750_dac_commit(s
->wm
);
295 s
->last_free
= free_out
- block_size
;
297 if (s
->play_pos
== 0) {
298 s
->status
|= MP_AUDIO_TX_HALF
;
299 s
->play_pos
= block_size
;
301 s
->status
|= MP_AUDIO_TX_FULL
;
305 if (s
->status
& s
->irq_enable
)
306 qemu_irq_raise(s
->irq
);
309 static void musicpal_audio_clock_update(musicpal_audio_state
*s
)
313 if (s
->playback_mode
& MP_AUDIO_CLOCK_24MHZ
)
314 rate
= 24576000 / 64; /* 24.576MHz */
316 rate
= 11289600 / 64; /* 11.2896MHz */
318 rate
/= ((s
->clock_div
>> 8) & 0xff) + 1;
320 wm8750_set_bclk_in(s
->wm
, rate
);
323 static uint32_t musicpal_audio_read(void *opaque
, target_phys_addr_t offset
)
325 musicpal_audio_state
*s
= opaque
;
328 case MP_AUDIO_PLAYBACK_MODE
:
329 return s
->playback_mode
;
331 case MP_AUDIO_CLOCK_DIV
:
334 case MP_AUDIO_IRQ_STATUS
:
337 case MP_AUDIO_IRQ_ENABLE
:
338 return s
->irq_enable
;
340 case MP_AUDIO_TX_STATUS
:
341 return s
->play_pos
>> 2;
348 static void musicpal_audio_write(void *opaque
, target_phys_addr_t offset
,
351 musicpal_audio_state
*s
= opaque
;
354 case MP_AUDIO_PLAYBACK_MODE
:
355 if (value
& MP_AUDIO_PLAYBACK_EN
&&
356 !(s
->playback_mode
& MP_AUDIO_PLAYBACK_EN
)) {
361 s
->playback_mode
= value
;
362 musicpal_audio_clock_update(s
);
365 case MP_AUDIO_CLOCK_DIV
:
366 s
->clock_div
= value
;
369 musicpal_audio_clock_update(s
);
372 case MP_AUDIO_IRQ_STATUS
:
376 case MP_AUDIO_IRQ_ENABLE
:
377 s
->irq_enable
= value
;
378 if (s
->status
& s
->irq_enable
)
379 qemu_irq_raise(s
->irq
);
382 case MP_AUDIO_TX_START_LO
:
383 s
->phys_buf
= (s
->phys_buf
& 0xFFFF0000) | (value
& 0xFFFF);
384 s
->target_buffer
= s
->phys_buf
;
389 case MP_AUDIO_TX_THRESHOLD
:
390 s
->threshold
= (value
+ 1) * 4;
393 case MP_AUDIO_TX_START_HI
:
394 s
->phys_buf
= (s
->phys_buf
& 0xFFFF) | (value
<< 16);
395 s
->target_buffer
= s
->phys_buf
;
402 static void musicpal_audio_reset(void *opaque
)
404 musicpal_audio_state
*s
= opaque
;
406 s
->playback_mode
= 0;
411 static CPUReadMemoryFunc
*musicpal_audio_readfn
[] = {
417 static CPUWriteMemoryFunc
*musicpal_audio_writefn
[] = {
418 musicpal_audio_write
,
419 musicpal_audio_write
,
423 static i2c_interface
*musicpal_audio_init(qemu_irq irq
)
425 musicpal_audio_state
*s
;
429 s
= qemu_mallocz(sizeof(musicpal_audio_state
));
432 i2c
= qemu_mallocz(sizeof(i2c_interface
));
433 i2c
->bus
= i2c_init_bus();
434 i2c
->current_addr
= -1;
436 s
->wm
= wm8750_init(i2c
->bus
);
439 i2c_set_slave_address(s
->wm
, MP_WM_ADDR
);
440 wm8750_data_req_set(s
->wm
, audio_callback
, s
);
442 iomemtype
= cpu_register_io_memory(0, musicpal_audio_readfn
,
443 musicpal_audio_writefn
, s
);
444 cpu_register_physical_memory(MP_AUDIO_BASE
, MP_AUDIO_SIZE
, iomemtype
);
446 qemu_register_reset(musicpal_audio_reset
, s
);
450 #else /* !HAS_AUDIO */
451 static i2c_interface
*musicpal_audio_init(qemu_irq irq
)
455 #endif /* !HAS_AUDIO */
457 /* Ethernet register offsets */
458 #define MP_ETH_SMIR 0x010
459 #define MP_ETH_PCXR 0x408
460 #define MP_ETH_SDCMR 0x448
461 #define MP_ETH_ICR 0x450
462 #define MP_ETH_IMR 0x458
463 #define MP_ETH_FRDP0 0x480
464 #define MP_ETH_FRDP1 0x484
465 #define MP_ETH_FRDP2 0x488
466 #define MP_ETH_FRDP3 0x48C
467 #define MP_ETH_CRDP0 0x4A0
468 #define MP_ETH_CRDP1 0x4A4
469 #define MP_ETH_CRDP2 0x4A8
470 #define MP_ETH_CRDP3 0x4AC
471 #define MP_ETH_CTDP0 0x4E0
472 #define MP_ETH_CTDP1 0x4E4
473 #define MP_ETH_CTDP2 0x4E8
474 #define MP_ETH_CTDP3 0x4EC
477 #define MP_ETH_SMIR_DATA 0x0000FFFF
478 #define MP_ETH_SMIR_ADDR 0x03FF0000
479 #define MP_ETH_SMIR_OPCODE (1 << 26) /* Read value */
480 #define MP_ETH_SMIR_RDVALID (1 << 27)
483 #define MP_ETH_PHY1_BMSR 0x00210000
484 #define MP_ETH_PHY1_PHYSID1 0x00410000
485 #define MP_ETH_PHY1_PHYSID2 0x00610000
487 #define MP_PHY_BMSR_LINK 0x0004
488 #define MP_PHY_BMSR_AUTONEG 0x0008
490 #define MP_PHY_88E3015 0x01410E20
492 /* TX descriptor status */
493 #define MP_ETH_TX_OWN (1 << 31)
495 /* RX descriptor status */
496 #define MP_ETH_RX_OWN (1 << 31)
498 /* Interrupt cause/mask bits */
499 #define MP_ETH_IRQ_RX_BIT 0
500 #define MP_ETH_IRQ_RX (1 << MP_ETH_IRQ_RX_BIT)
501 #define MP_ETH_IRQ_TXHI_BIT 2
502 #define MP_ETH_IRQ_TXLO_BIT 3
504 /* Port config bits */
505 #define MP_ETH_PCXR_2BSM_BIT 28 /* 2-byte incoming suffix */
507 /* SDMA command bits */
508 #define MP_ETH_CMD_TXHI (1 << 23)
509 #define MP_ETH_CMD_TXLO (1 << 22)
511 typedef struct mv88w8618_tx_desc
{
519 typedef struct mv88w8618_rx_desc
{
522 uint16_t buffer_size
;
527 typedef struct mv88w8618_eth_state
{
534 uint32_t tx_queue
[2];
535 uint32_t rx_queue
[4];
536 uint32_t frx_queue
[4];
539 } mv88w8618_eth_state
;
541 static void eth_rx_desc_put(uint32_t addr
, mv88w8618_rx_desc
*desc
)
543 cpu_to_le32s(&desc
->cmdstat
);
544 cpu_to_le16s(&desc
->bytes
);
545 cpu_to_le16s(&desc
->buffer_size
);
546 cpu_to_le32s(&desc
->buffer
);
547 cpu_to_le32s(&desc
->next
);
548 cpu_physical_memory_write(addr
, (void *)desc
, sizeof(*desc
));
551 static void eth_rx_desc_get(uint32_t addr
, mv88w8618_rx_desc
*desc
)
553 cpu_physical_memory_read(addr
, (void *)desc
, sizeof(*desc
));
554 le32_to_cpus(&desc
->cmdstat
);
555 le16_to_cpus(&desc
->bytes
);
556 le16_to_cpus(&desc
->buffer_size
);
557 le32_to_cpus(&desc
->buffer
);
558 le32_to_cpus(&desc
->next
);
561 static int eth_can_receive(void *opaque
)
566 static void eth_receive(void *opaque
, const uint8_t *buf
, int size
)
568 mv88w8618_eth_state
*s
= opaque
;
570 mv88w8618_rx_desc desc
;
573 for (i
= 0; i
< 4; i
++) {
574 desc_addr
= s
->cur_rx
[i
];
578 eth_rx_desc_get(desc_addr
, &desc
);
579 if ((desc
.cmdstat
& MP_ETH_RX_OWN
) && desc
.buffer_size
>= size
) {
580 cpu_physical_memory_write(desc
.buffer
+ s
->vlan_header
,
582 desc
.bytes
= size
+ s
->vlan_header
;
583 desc
.cmdstat
&= ~MP_ETH_RX_OWN
;
584 s
->cur_rx
[i
] = desc
.next
;
586 s
->icr
|= MP_ETH_IRQ_RX
;
588 qemu_irq_raise(s
->irq
);
589 eth_rx_desc_put(desc_addr
, &desc
);
592 desc_addr
= desc
.next
;
593 } while (desc_addr
!= s
->rx_queue
[i
]);
597 static void eth_tx_desc_put(uint32_t addr
, mv88w8618_tx_desc
*desc
)
599 cpu_to_le32s(&desc
->cmdstat
);
600 cpu_to_le16s(&desc
->res
);
601 cpu_to_le16s(&desc
->bytes
);
602 cpu_to_le32s(&desc
->buffer
);
603 cpu_to_le32s(&desc
->next
);
604 cpu_physical_memory_write(addr
, (void *)desc
, sizeof(*desc
));
607 static void eth_tx_desc_get(uint32_t addr
, mv88w8618_tx_desc
*desc
)
609 cpu_physical_memory_read(addr
, (void *)desc
, sizeof(*desc
));
610 le32_to_cpus(&desc
->cmdstat
);
611 le16_to_cpus(&desc
->res
);
612 le16_to_cpus(&desc
->bytes
);
613 le32_to_cpus(&desc
->buffer
);
614 le32_to_cpus(&desc
->next
);
617 static void eth_send(mv88w8618_eth_state
*s
, int queue_index
)
619 uint32_t desc_addr
= s
->tx_queue
[queue_index
];
620 mv88w8618_tx_desc desc
;
626 eth_tx_desc_get(desc_addr
, &desc
);
627 if (desc
.cmdstat
& MP_ETH_TX_OWN
) {
630 cpu_physical_memory_read(desc
.buffer
, buf
, len
);
631 qemu_send_packet(s
->vc
, buf
, len
);
633 desc
.cmdstat
&= ~MP_ETH_TX_OWN
;
634 s
->icr
|= 1 << (MP_ETH_IRQ_TXLO_BIT
- queue_index
);
635 eth_tx_desc_put(desc_addr
, &desc
);
637 desc_addr
= desc
.next
;
638 } while (desc_addr
!= s
->tx_queue
[queue_index
]);
641 static uint32_t mv88w8618_eth_read(void *opaque
, target_phys_addr_t offset
)
643 mv88w8618_eth_state
*s
= opaque
;
647 if (s
->smir
& MP_ETH_SMIR_OPCODE
) {
648 switch (s
->smir
& MP_ETH_SMIR_ADDR
) {
649 case MP_ETH_PHY1_BMSR
:
650 return MP_PHY_BMSR_LINK
| MP_PHY_BMSR_AUTONEG
|
652 case MP_ETH_PHY1_PHYSID1
:
653 return (MP_PHY_88E3015
>> 16) | MP_ETH_SMIR_RDVALID
;
654 case MP_ETH_PHY1_PHYSID2
:
655 return (MP_PHY_88E3015
& 0xFFFF) | MP_ETH_SMIR_RDVALID
;
657 return MP_ETH_SMIR_RDVALID
;
668 case MP_ETH_FRDP0
... MP_ETH_FRDP3
:
669 return s
->frx_queue
[(offset
- MP_ETH_FRDP0
)/4];
671 case MP_ETH_CRDP0
... MP_ETH_CRDP3
:
672 return s
->rx_queue
[(offset
- MP_ETH_CRDP0
)/4];
674 case MP_ETH_CTDP0
... MP_ETH_CTDP3
:
675 return s
->tx_queue
[(offset
- MP_ETH_CTDP0
)/4];
682 static void mv88w8618_eth_write(void *opaque
, target_phys_addr_t offset
,
685 mv88w8618_eth_state
*s
= opaque
;
693 s
->vlan_header
= ((value
>> MP_ETH_PCXR_2BSM_BIT
) & 1) * 2;
697 if (value
& MP_ETH_CMD_TXHI
)
699 if (value
& MP_ETH_CMD_TXLO
)
701 if (value
& (MP_ETH_CMD_TXHI
| MP_ETH_CMD_TXLO
) && s
->icr
& s
->imr
)
702 qemu_irq_raise(s
->irq
);
712 qemu_irq_raise(s
->irq
);
715 case MP_ETH_FRDP0
... MP_ETH_FRDP3
:
716 s
->frx_queue
[(offset
- MP_ETH_FRDP0
)/4] = value
;
719 case MP_ETH_CRDP0
... MP_ETH_CRDP3
:
720 s
->rx_queue
[(offset
- MP_ETH_CRDP0
)/4] =
721 s
->cur_rx
[(offset
- MP_ETH_CRDP0
)/4] = value
;
724 case MP_ETH_CTDP0
... MP_ETH_CTDP3
:
725 s
->tx_queue
[(offset
- MP_ETH_CTDP0
)/4] = value
;
730 static CPUReadMemoryFunc
*mv88w8618_eth_readfn
[] = {
736 static CPUWriteMemoryFunc
*mv88w8618_eth_writefn
[] = {
742 static void eth_cleanup(VLANClientState
*vc
)
744 mv88w8618_eth_state
*s
= vc
->opaque
;
746 cpu_unregister_io_memory(s
->mmio_index
);
751 static void mv88w8618_eth_init(NICInfo
*nd
, uint32_t base
, qemu_irq irq
)
753 mv88w8618_eth_state
*s
;
755 qemu_check_nic_model(nd
, "mv88w8618");
757 s
= qemu_mallocz(sizeof(mv88w8618_eth_state
));
759 s
->vc
= qemu_new_vlan_client(nd
->vlan
, nd
->model
, nd
->name
,
760 eth_receive
, eth_can_receive
,
762 s
->mmio_index
= cpu_register_io_memory(0, mv88w8618_eth_readfn
,
763 mv88w8618_eth_writefn
, s
);
764 cpu_register_physical_memory(base
, MP_ETH_SIZE
, s
->mmio_index
);
767 /* LCD register offsets */
768 #define MP_LCD_IRQCTRL 0x180
769 #define MP_LCD_IRQSTAT 0x184
770 #define MP_LCD_SPICTRL 0x1ac
771 #define MP_LCD_INST 0x1bc
772 #define MP_LCD_DATA 0x1c0
775 #define MP_LCD_SPI_DATA 0x00100011
776 #define MP_LCD_SPI_CMD 0x00104011
777 #define MP_LCD_SPI_INVALID 0x00000000
780 #define MP_LCD_INST_SETPAGE0 0xB0
782 #define MP_LCD_INST_SETPAGE7 0xB7
784 #define MP_LCD_TEXTCOLOR 0xe0e0ff /* RRGGBB */
786 typedef struct musicpal_lcd_state
{
792 uint8_t video_ram
[128*64/8];
793 } musicpal_lcd_state
;
795 static uint32_t lcd_brightness
;
797 static uint8_t scale_lcd_color(uint8_t col
)
801 switch (lcd_brightness
) {
802 case 0x00000007: /* 0 */
805 case 0x00020000: /* 1 */
806 return (tmp
* 1) / 7;
808 case 0x00020001: /* 2 */
809 return (tmp
* 2) / 7;
811 case 0x00040000: /* 3 */
812 return (tmp
* 3) / 7;
814 case 0x00010006: /* 4 */
815 return (tmp
* 4) / 7;
817 case 0x00020005: /* 5 */
818 return (tmp
* 5) / 7;
820 case 0x00040003: /* 6 */
821 return (tmp
* 6) / 7;
823 case 0x00030004: /* 7 */
829 #define SET_LCD_PIXEL(depth, type) \
830 static inline void glue(set_lcd_pixel, depth) \
831 (musicpal_lcd_state *s, int x, int y, type col) \
834 type *pixel = &((type *) ds_get_data(s->ds))[(y * 128 * 3 + x) * 3]; \
836 for (dy = 0; dy < 3; dy++, pixel += 127 * 3) \
837 for (dx = 0; dx < 3; dx++, pixel++) \
840 SET_LCD_PIXEL(8, uint8_t)
841 SET_LCD_PIXEL(16, uint16_t)
842 SET_LCD_PIXEL(32, uint32_t)
844 #include "pixel_ops.h"
846 static void lcd_refresh(void *opaque
)
848 musicpal_lcd_state
*s
= opaque
;
851 switch (ds_get_bits_per_pixel(s
->ds
)) {
854 #define LCD_REFRESH(depth, func) \
856 col = func(scale_lcd_color((MP_LCD_TEXTCOLOR >> 16) & 0xff), \
857 scale_lcd_color((MP_LCD_TEXTCOLOR >> 8) & 0xff), \
858 scale_lcd_color(MP_LCD_TEXTCOLOR & 0xff)); \
859 for (x = 0; x < 128; x++) \
860 for (y = 0; y < 64; y++) \
861 if (s->video_ram[x + (y/8)*128] & (1 << (y % 8))) \
862 glue(set_lcd_pixel, depth)(s, x, y, col); \
864 glue(set_lcd_pixel, depth)(s, x, y, 0); \
866 LCD_REFRESH(8, rgb_to_pixel8
)
867 LCD_REFRESH(16, rgb_to_pixel16
)
868 LCD_REFRESH(32, (is_surface_bgr(s
->ds
->surface
) ?
869 rgb_to_pixel32bgr
: rgb_to_pixel32
))
871 hw_error("unsupported colour depth %i\n",
872 ds_get_bits_per_pixel(s
->ds
));
875 dpy_update(s
->ds
, 0, 0, 128*3, 64*3);
878 static void lcd_invalidate(void *opaque
)
882 static uint32_t musicpal_lcd_read(void *opaque
, target_phys_addr_t offset
)
884 musicpal_lcd_state
*s
= opaque
;
895 static void musicpal_lcd_write(void *opaque
, target_phys_addr_t offset
,
898 musicpal_lcd_state
*s
= opaque
;
906 if (value
== MP_LCD_SPI_DATA
|| value
== MP_LCD_SPI_CMD
)
909 s
->mode
= MP_LCD_SPI_INVALID
;
913 if (value
>= MP_LCD_INST_SETPAGE0
&& value
<= MP_LCD_INST_SETPAGE7
) {
914 s
->page
= value
- MP_LCD_INST_SETPAGE0
;
920 if (s
->mode
== MP_LCD_SPI_CMD
) {
921 if (value
>= MP_LCD_INST_SETPAGE0
&&
922 value
<= MP_LCD_INST_SETPAGE7
) {
923 s
->page
= value
- MP_LCD_INST_SETPAGE0
;
926 } else if (s
->mode
== MP_LCD_SPI_DATA
) {
927 s
->video_ram
[s
->page
*128 + s
->page_off
] = value
;
928 s
->page_off
= (s
->page_off
+ 1) & 127;
934 static CPUReadMemoryFunc
*musicpal_lcd_readfn
[] = {
940 static CPUWriteMemoryFunc
*musicpal_lcd_writefn
[] = {
946 static void musicpal_lcd_init(void)
948 musicpal_lcd_state
*s
;
951 s
= qemu_mallocz(sizeof(musicpal_lcd_state
));
952 iomemtype
= cpu_register_io_memory(0, musicpal_lcd_readfn
,
953 musicpal_lcd_writefn
, s
);
954 cpu_register_physical_memory(MP_LCD_BASE
, MP_LCD_SIZE
, iomemtype
);
956 s
->ds
= graphic_console_init(lcd_refresh
, lcd_invalidate
,
958 qemu_console_resize(s
->ds
, 128*3, 64*3);
961 /* PIC register offsets */
962 #define MP_PIC_STATUS 0x00
963 #define MP_PIC_ENABLE_SET 0x08
964 #define MP_PIC_ENABLE_CLR 0x0C
966 typedef struct mv88w8618_pic_state
971 } mv88w8618_pic_state
;
973 static void mv88w8618_pic_update(mv88w8618_pic_state
*s
)
975 qemu_set_irq(s
->parent_irq
, (s
->level
& s
->enabled
));
978 static void mv88w8618_pic_set_irq(void *opaque
, int irq
, int level
)
980 mv88w8618_pic_state
*s
= opaque
;
983 s
->level
|= 1 << irq
;
985 s
->level
&= ~(1 << irq
);
986 mv88w8618_pic_update(s
);
989 static uint32_t mv88w8618_pic_read(void *opaque
, target_phys_addr_t offset
)
991 mv88w8618_pic_state
*s
= opaque
;
995 return s
->level
& s
->enabled
;
1002 static void mv88w8618_pic_write(void *opaque
, target_phys_addr_t offset
,
1005 mv88w8618_pic_state
*s
= opaque
;
1008 case MP_PIC_ENABLE_SET
:
1009 s
->enabled
|= value
;
1012 case MP_PIC_ENABLE_CLR
:
1013 s
->enabled
&= ~value
;
1017 mv88w8618_pic_update(s
);
1020 static void mv88w8618_pic_reset(void *opaque
)
1022 mv88w8618_pic_state
*s
= opaque
;
1028 static CPUReadMemoryFunc
*mv88w8618_pic_readfn
[] = {
1034 static CPUWriteMemoryFunc
*mv88w8618_pic_writefn
[] = {
1035 mv88w8618_pic_write
,
1036 mv88w8618_pic_write
,
1040 static qemu_irq
*mv88w8618_pic_init(uint32_t base
, qemu_irq parent_irq
)
1042 mv88w8618_pic_state
*s
;
1046 s
= qemu_mallocz(sizeof(mv88w8618_pic_state
));
1047 qi
= qemu_allocate_irqs(mv88w8618_pic_set_irq
, s
, 32);
1048 s
->parent_irq
= parent_irq
;
1049 iomemtype
= cpu_register_io_memory(0, mv88w8618_pic_readfn
,
1050 mv88w8618_pic_writefn
, s
);
1051 cpu_register_physical_memory(base
, MP_PIC_SIZE
, iomemtype
);
1053 qemu_register_reset(mv88w8618_pic_reset
, s
);
1058 /* PIT register offsets */
1059 #define MP_PIT_TIMER1_LENGTH 0x00
1061 #define MP_PIT_TIMER4_LENGTH 0x0C
1062 #define MP_PIT_CONTROL 0x10
1063 #define MP_PIT_TIMER1_VALUE 0x14
1065 #define MP_PIT_TIMER4_VALUE 0x20
1066 #define MP_BOARD_RESET 0x34
1068 /* Magic board reset value (probably some watchdog behind it) */
1069 #define MP_BOARD_RESET_MAGIC 0x10000
1071 typedef struct mv88w8618_timer_state
{
1072 ptimer_state
*timer
;
1076 } mv88w8618_timer_state
;
1078 typedef struct mv88w8618_pit_state
{
1081 } mv88w8618_pit_state
;
1083 static void mv88w8618_timer_tick(void *opaque
)
1085 mv88w8618_timer_state
*s
= opaque
;
1087 qemu_irq_raise(s
->irq
);
1090 static void *mv88w8618_timer_init(uint32_t freq
, qemu_irq irq
)
1092 mv88w8618_timer_state
*s
;
1095 s
= qemu_mallocz(sizeof(mv88w8618_timer_state
));
1099 bh
= qemu_bh_new(mv88w8618_timer_tick
, s
);
1100 s
->timer
= ptimer_init(bh
);
1105 static uint32_t mv88w8618_pit_read(void *opaque
, target_phys_addr_t offset
)
1107 mv88w8618_pit_state
*s
= opaque
;
1108 mv88w8618_timer_state
*t
;
1111 case MP_PIT_TIMER1_VALUE
... MP_PIT_TIMER4_VALUE
:
1112 t
= s
->timer
[(offset
-MP_PIT_TIMER1_VALUE
) >> 2];
1113 return ptimer_get_count(t
->timer
);
1120 static void mv88w8618_pit_write(void *opaque
, target_phys_addr_t offset
,
1123 mv88w8618_pit_state
*s
= opaque
;
1124 mv88w8618_timer_state
*t
;
1128 case MP_PIT_TIMER1_LENGTH
... MP_PIT_TIMER4_LENGTH
:
1129 t
= s
->timer
[offset
>> 2];
1131 ptimer_set_limit(t
->timer
, t
->limit
, 1);
1134 case MP_PIT_CONTROL
:
1135 for (i
= 0; i
< 4; i
++) {
1138 ptimer_set_limit(t
->timer
, t
->limit
, 0);
1139 ptimer_set_freq(t
->timer
, t
->freq
);
1140 ptimer_run(t
->timer
, 0);
1146 case MP_BOARD_RESET
:
1147 if (value
== MP_BOARD_RESET_MAGIC
)
1148 qemu_system_reset_request();
1153 static CPUReadMemoryFunc
*mv88w8618_pit_readfn
[] = {
1159 static CPUWriteMemoryFunc
*mv88w8618_pit_writefn
[] = {
1160 mv88w8618_pit_write
,
1161 mv88w8618_pit_write
,
1165 static void mv88w8618_pit_init(uint32_t base
, qemu_irq
*pic
, int irq
)
1168 mv88w8618_pit_state
*s
;
1170 s
= qemu_mallocz(sizeof(mv88w8618_pit_state
));
1172 /* Letting them all run at 1 MHz is likely just a pragmatic
1173 * simplification. */
1174 s
->timer
[0] = mv88w8618_timer_init(1000000, pic
[irq
]);
1175 s
->timer
[1] = mv88w8618_timer_init(1000000, pic
[irq
+ 1]);
1176 s
->timer
[2] = mv88w8618_timer_init(1000000, pic
[irq
+ 2]);
1177 s
->timer
[3] = mv88w8618_timer_init(1000000, pic
[irq
+ 3]);
1179 iomemtype
= cpu_register_io_memory(0, mv88w8618_pit_readfn
,
1180 mv88w8618_pit_writefn
, s
);
1181 cpu_register_physical_memory(base
, MP_PIT_SIZE
, iomemtype
);
1184 /* Flash config register offsets */
1185 #define MP_FLASHCFG_CFGR0 0x04
1187 typedef struct mv88w8618_flashcfg_state
{
1189 } mv88w8618_flashcfg_state
;
1191 static uint32_t mv88w8618_flashcfg_read(void *opaque
,
1192 target_phys_addr_t offset
)
1194 mv88w8618_flashcfg_state
*s
= opaque
;
1197 case MP_FLASHCFG_CFGR0
:
1205 static void mv88w8618_flashcfg_write(void *opaque
, target_phys_addr_t offset
,
1208 mv88w8618_flashcfg_state
*s
= opaque
;
1211 case MP_FLASHCFG_CFGR0
:
1217 static CPUReadMemoryFunc
*mv88w8618_flashcfg_readfn
[] = {
1218 mv88w8618_flashcfg_read
,
1219 mv88w8618_flashcfg_read
,
1220 mv88w8618_flashcfg_read
1223 static CPUWriteMemoryFunc
*mv88w8618_flashcfg_writefn
[] = {
1224 mv88w8618_flashcfg_write
,
1225 mv88w8618_flashcfg_write
,
1226 mv88w8618_flashcfg_write
1229 static void mv88w8618_flashcfg_init(uint32_t base
)
1232 mv88w8618_flashcfg_state
*s
;
1234 s
= qemu_mallocz(sizeof(mv88w8618_flashcfg_state
));
1236 s
->cfgr0
= 0xfffe4285; /* Default as set by U-Boot for 8 MB flash */
1237 iomemtype
= cpu_register_io_memory(0, mv88w8618_flashcfg_readfn
,
1238 mv88w8618_flashcfg_writefn
, s
);
1239 cpu_register_physical_memory(base
, MP_FLASHCFG_SIZE
, iomemtype
);
1242 /* Misc register offsets */
1243 #define MP_MISC_BOARD_REVISION 0x18
1245 #define MP_BOARD_REVISION 0x31
1247 static uint32_t musicpal_misc_read(void *opaque
, target_phys_addr_t offset
)
1250 case MP_MISC_BOARD_REVISION
:
1251 return MP_BOARD_REVISION
;
1258 static void musicpal_misc_write(void *opaque
, target_phys_addr_t offset
,
1263 static CPUReadMemoryFunc
*musicpal_misc_readfn
[] = {
1269 static CPUWriteMemoryFunc
*musicpal_misc_writefn
[] = {
1270 musicpal_misc_write
,
1271 musicpal_misc_write
,
1272 musicpal_misc_write
,
1275 static void musicpal_misc_init(void)
1279 iomemtype
= cpu_register_io_memory(0, musicpal_misc_readfn
,
1280 musicpal_misc_writefn
, NULL
);
1281 cpu_register_physical_memory(MP_MISC_BASE
, MP_MISC_SIZE
, iomemtype
);
1284 /* WLAN register offsets */
1285 #define MP_WLAN_MAGIC1 0x11c
1286 #define MP_WLAN_MAGIC2 0x124
1288 static uint32_t mv88w8618_wlan_read(void *opaque
, target_phys_addr_t offset
)
1291 /* Workaround to allow loading the binary-only wlandrv.ko crap
1292 * from the original Freecom firmware. */
1293 case MP_WLAN_MAGIC1
:
1295 case MP_WLAN_MAGIC2
:
1303 static void mv88w8618_wlan_write(void *opaque
, target_phys_addr_t offset
,
1308 static CPUReadMemoryFunc
*mv88w8618_wlan_readfn
[] = {
1309 mv88w8618_wlan_read
,
1310 mv88w8618_wlan_read
,
1311 mv88w8618_wlan_read
,
1314 static CPUWriteMemoryFunc
*mv88w8618_wlan_writefn
[] = {
1315 mv88w8618_wlan_write
,
1316 mv88w8618_wlan_write
,
1317 mv88w8618_wlan_write
,
1320 static void mv88w8618_wlan_init(uint32_t base
)
1324 iomemtype
= cpu_register_io_memory(0, mv88w8618_wlan_readfn
,
1325 mv88w8618_wlan_writefn
, NULL
);
1326 cpu_register_physical_memory(base
, MP_WLAN_SIZE
, iomemtype
);
1329 /* GPIO register offsets */
1330 #define MP_GPIO_OE_LO 0x008
1331 #define MP_GPIO_OUT_LO 0x00c
1332 #define MP_GPIO_IN_LO 0x010
1333 #define MP_GPIO_ISR_LO 0x020
1334 #define MP_GPIO_OE_HI 0x508
1335 #define MP_GPIO_OUT_HI 0x50c
1336 #define MP_GPIO_IN_HI 0x510
1337 #define MP_GPIO_ISR_HI 0x520
1339 /* GPIO bits & masks */
1340 #define MP_GPIO_WHEEL_VOL (1 << 8)
1341 #define MP_GPIO_WHEEL_VOL_INV (1 << 9)
1342 #define MP_GPIO_WHEEL_NAV (1 << 10)
1343 #define MP_GPIO_WHEEL_NAV_INV (1 << 11)
1344 #define MP_GPIO_LCD_BRIGHTNESS 0x00070000
1345 #define MP_GPIO_BTN_FAVORITS (1 << 19)
1346 #define MP_GPIO_BTN_MENU (1 << 20)
1347 #define MP_GPIO_BTN_VOLUME (1 << 21)
1348 #define MP_GPIO_BTN_NAVIGATION (1 << 22)
1349 #define MP_GPIO_I2C_DATA_BIT 29
1350 #define MP_GPIO_I2C_DATA (1 << MP_GPIO_I2C_DATA_BIT)
1351 #define MP_GPIO_I2C_CLOCK_BIT 30
1353 /* LCD brightness bits in GPIO_OE_HI */
1354 #define MP_OE_LCD_BRIGHTNESS 0x0007
1356 static uint32_t musicpal_gpio_read(void *opaque
, target_phys_addr_t offset
)
1359 case MP_GPIO_OE_HI
: /* used for LCD brightness control */
1360 return lcd_brightness
& MP_OE_LCD_BRIGHTNESS
;
1362 case MP_GPIO_OUT_LO
:
1363 return gpio_out_state
& 0xFFFF;
1364 case MP_GPIO_OUT_HI
:
1365 return gpio_out_state
>> 16;
1368 return gpio_in_state
& 0xFFFF;
1370 /* Update received I2C data */
1371 gpio_in_state
= (gpio_in_state
& ~MP_GPIO_I2C_DATA
) |
1372 (i2c_get_data(mixer_i2c
) << MP_GPIO_I2C_DATA_BIT
);
1373 return gpio_in_state
>> 16;
1375 case MP_GPIO_ISR_LO
:
1376 return gpio_isr
& 0xFFFF;
1377 case MP_GPIO_ISR_HI
:
1378 return gpio_isr
>> 16;
1385 static void musicpal_gpio_write(void *opaque
, target_phys_addr_t offset
,
1389 case MP_GPIO_OE_HI
: /* used for LCD brightness control */
1390 lcd_brightness
= (lcd_brightness
& MP_GPIO_LCD_BRIGHTNESS
) |
1391 (value
& MP_OE_LCD_BRIGHTNESS
);
1394 case MP_GPIO_OUT_LO
:
1395 gpio_out_state
= (gpio_out_state
& 0xFFFF0000) | (value
& 0xFFFF);
1397 case MP_GPIO_OUT_HI
:
1398 gpio_out_state
= (gpio_out_state
& 0xFFFF) | (value
<< 16);
1399 lcd_brightness
= (lcd_brightness
& 0xFFFF) |
1400 (gpio_out_state
& MP_GPIO_LCD_BRIGHTNESS
);
1401 i2c_state_update(mixer_i2c
,
1402 (gpio_out_state
>> MP_GPIO_I2C_DATA_BIT
) & 1,
1403 (gpio_out_state
>> MP_GPIO_I2C_CLOCK_BIT
) & 1);
1409 static CPUReadMemoryFunc
*musicpal_gpio_readfn
[] = {
1415 static CPUWriteMemoryFunc
*musicpal_gpio_writefn
[] = {
1416 musicpal_gpio_write
,
1417 musicpal_gpio_write
,
1418 musicpal_gpio_write
,
1421 static void musicpal_gpio_init(void)
1425 iomemtype
= cpu_register_io_memory(0, musicpal_gpio_readfn
,
1426 musicpal_gpio_writefn
, NULL
);
1427 cpu_register_physical_memory(MP_GPIO_BASE
, MP_GPIO_SIZE
, iomemtype
);
1430 /* Keyboard codes & masks */
1431 #define KEY_RELEASED 0x80
1432 #define KEY_CODE 0x7f
1434 #define KEYCODE_TAB 0x0f
1435 #define KEYCODE_ENTER 0x1c
1436 #define KEYCODE_F 0x21
1437 #define KEYCODE_M 0x32
1439 #define KEYCODE_EXTENDED 0xe0
1440 #define KEYCODE_UP 0x48
1441 #define KEYCODE_DOWN 0x50
1442 #define KEYCODE_LEFT 0x4b
1443 #define KEYCODE_RIGHT 0x4d
1445 static void musicpal_key_event(void *opaque
, int keycode
)
1447 qemu_irq irq
= opaque
;
1449 static int kbd_extended
;
1451 if (keycode
== KEYCODE_EXTENDED
) {
1457 switch (keycode
& KEY_CODE
) {
1459 event
= MP_GPIO_WHEEL_NAV
| MP_GPIO_WHEEL_NAV_INV
;
1463 event
= MP_GPIO_WHEEL_NAV
;
1467 event
= MP_GPIO_WHEEL_VOL
| MP_GPIO_WHEEL_VOL_INV
;
1471 event
= MP_GPIO_WHEEL_VOL
;
1475 switch (keycode
& KEY_CODE
) {
1477 event
= MP_GPIO_BTN_FAVORITS
;
1481 event
= MP_GPIO_BTN_VOLUME
;
1485 event
= MP_GPIO_BTN_NAVIGATION
;
1489 event
= MP_GPIO_BTN_MENU
;
1492 /* Do not repeat already pressed buttons */
1493 if (!(keycode
& KEY_RELEASED
) && !(gpio_in_state
& event
))
1498 if (keycode
& KEY_RELEASED
) {
1499 gpio_in_state
|= event
;
1501 gpio_in_state
&= ~event
;
1503 qemu_irq_raise(irq
);
1510 static struct arm_boot_info musicpal_binfo
= {
1511 .loader_start
= 0x0,
1515 static void musicpal_init(ram_addr_t ram_size
,
1516 const char *boot_device
,
1517 const char *kernel_filename
, const char *kernel_cmdline
,
1518 const char *initrd_filename
, const char *cpu_model
)
1523 unsigned long flash_size
;
1526 cpu_model
= "arm926";
1528 env
= cpu_init(cpu_model
);
1530 fprintf(stderr
, "Unable to find CPU definition\n");
1533 pic
= arm_pic_init_cpu(env
);
1535 /* For now we use a fixed - the original - RAM size */
1536 cpu_register_physical_memory(0, MP_RAM_DEFAULT_SIZE
,
1537 qemu_ram_alloc(MP_RAM_DEFAULT_SIZE
));
1539 sram_off
= qemu_ram_alloc(MP_SRAM_SIZE
);
1540 cpu_register_physical_memory(MP_SRAM_BASE
, MP_SRAM_SIZE
, sram_off
);
1542 pic
= mv88w8618_pic_init(MP_PIC_BASE
, pic
[ARM_PIC_CPU_IRQ
]);
1543 mv88w8618_pit_init(MP_PIT_BASE
, pic
, MP_TIMER1_IRQ
);
1546 serial_mm_init(MP_UART1_BASE
, 2, pic
[MP_UART1_IRQ
], 1825000,
1549 serial_mm_init(MP_UART2_BASE
, 2, pic
[MP_UART2_IRQ
], 1825000,
1552 /* Register flash */
1553 index
= drive_get_index(IF_PFLASH
, 0, 0);
1555 flash_size
= bdrv_getlength(drives_table
[index
].bdrv
);
1556 if (flash_size
!= 8*1024*1024 && flash_size
!= 16*1024*1024 &&
1557 flash_size
!= 32*1024*1024) {
1558 fprintf(stderr
, "Invalid flash image size\n");
1563 * The original U-Boot accesses the flash at 0xFE000000 instead of
1564 * 0xFF800000 (if there is 8 MB flash). So remap flash access if the
1565 * image is smaller than 32 MB.
1567 pflash_cfi02_register(0-MP_FLASH_SIZE_MAX
, qemu_ram_alloc(flash_size
),
1568 drives_table
[index
].bdrv
, 0x10000,
1569 (flash_size
+ 0xffff) >> 16,
1570 MP_FLASH_SIZE_MAX
/ flash_size
,
1571 2, 0x00BF, 0x236D, 0x0000, 0x0000,
1574 mv88w8618_flashcfg_init(MP_FLASHCFG_BASE
);
1576 musicpal_lcd_init();
1578 qemu_add_kbd_event_handler(musicpal_key_event
, pic
[MP_GPIO_IRQ
]);
1580 mv88w8618_eth_init(&nd_table
[0], MP_ETH_BASE
, pic
[MP_ETH_IRQ
]);
1582 mixer_i2c
= musicpal_audio_init(pic
[MP_AUDIO_IRQ
]);
1584 mv88w8618_wlan_init(MP_WLAN_BASE
);
1586 musicpal_misc_init();
1587 musicpal_gpio_init();
1589 musicpal_binfo
.ram_size
= MP_RAM_DEFAULT_SIZE
;
1590 musicpal_binfo
.kernel_filename
= kernel_filename
;
1591 musicpal_binfo
.kernel_cmdline
= kernel_cmdline
;
1592 musicpal_binfo
.initrd_filename
= initrd_filename
;
1593 arm_load_kernel(env
, &musicpal_binfo
);
1596 QEMUMachine musicpal_machine
= {
1598 .desc
= "Marvell 88w8618 / MusicPal (ARM926EJ-S)",
1599 .init
= musicpal_init
,