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
)
426 musicpal_audio_state
*s
;
432 AUD_log(audio_name
, "No audio state\n");
436 s
= qemu_mallocz(sizeof(musicpal_audio_state
));
439 i2c
= qemu_mallocz(sizeof(i2c_interface
));
440 i2c
->bus
= i2c_init_bus();
441 i2c
->current_addr
= -1;
443 s
->wm
= wm8750_init(i2c
->bus
, audio
);
446 i2c_set_slave_address(s
->wm
, MP_WM_ADDR
);
447 wm8750_data_req_set(s
->wm
, audio_callback
, s
);
449 iomemtype
= cpu_register_io_memory(0, musicpal_audio_readfn
,
450 musicpal_audio_writefn
, s
);
451 cpu_register_physical_memory(MP_AUDIO_BASE
, MP_AUDIO_SIZE
, iomemtype
);
453 qemu_register_reset(musicpal_audio_reset
, s
);
457 #else /* !HAS_AUDIO */
458 static i2c_interface
*musicpal_audio_init(qemu_irq irq
)
462 #endif /* !HAS_AUDIO */
464 /* Ethernet register offsets */
465 #define MP_ETH_SMIR 0x010
466 #define MP_ETH_PCXR 0x408
467 #define MP_ETH_SDCMR 0x448
468 #define MP_ETH_ICR 0x450
469 #define MP_ETH_IMR 0x458
470 #define MP_ETH_FRDP0 0x480
471 #define MP_ETH_FRDP1 0x484
472 #define MP_ETH_FRDP2 0x488
473 #define MP_ETH_FRDP3 0x48C
474 #define MP_ETH_CRDP0 0x4A0
475 #define MP_ETH_CRDP1 0x4A4
476 #define MP_ETH_CRDP2 0x4A8
477 #define MP_ETH_CRDP3 0x4AC
478 #define MP_ETH_CTDP0 0x4E0
479 #define MP_ETH_CTDP1 0x4E4
480 #define MP_ETH_CTDP2 0x4E8
481 #define MP_ETH_CTDP3 0x4EC
484 #define MP_ETH_SMIR_DATA 0x0000FFFF
485 #define MP_ETH_SMIR_ADDR 0x03FF0000
486 #define MP_ETH_SMIR_OPCODE (1 << 26) /* Read value */
487 #define MP_ETH_SMIR_RDVALID (1 << 27)
490 #define MP_ETH_PHY1_BMSR 0x00210000
491 #define MP_ETH_PHY1_PHYSID1 0x00410000
492 #define MP_ETH_PHY1_PHYSID2 0x00610000
494 #define MP_PHY_BMSR_LINK 0x0004
495 #define MP_PHY_BMSR_AUTONEG 0x0008
497 #define MP_PHY_88E3015 0x01410E20
499 /* TX descriptor status */
500 #define MP_ETH_TX_OWN (1 << 31)
502 /* RX descriptor status */
503 #define MP_ETH_RX_OWN (1 << 31)
505 /* Interrupt cause/mask bits */
506 #define MP_ETH_IRQ_RX_BIT 0
507 #define MP_ETH_IRQ_RX (1 << MP_ETH_IRQ_RX_BIT)
508 #define MP_ETH_IRQ_TXHI_BIT 2
509 #define MP_ETH_IRQ_TXLO_BIT 3
511 /* Port config bits */
512 #define MP_ETH_PCXR_2BSM_BIT 28 /* 2-byte incoming suffix */
514 /* SDMA command bits */
515 #define MP_ETH_CMD_TXHI (1 << 23)
516 #define MP_ETH_CMD_TXLO (1 << 22)
518 typedef struct mv88w8618_tx_desc
{
526 typedef struct mv88w8618_rx_desc
{
529 uint16_t buffer_size
;
534 typedef struct mv88w8618_eth_state
{
541 uint32_t tx_queue
[2];
542 uint32_t rx_queue
[4];
543 uint32_t frx_queue
[4];
546 } mv88w8618_eth_state
;
548 static void eth_rx_desc_put(uint32_t addr
, mv88w8618_rx_desc
*desc
)
550 cpu_to_le32s(&desc
->cmdstat
);
551 cpu_to_le16s(&desc
->bytes
);
552 cpu_to_le16s(&desc
->buffer_size
);
553 cpu_to_le32s(&desc
->buffer
);
554 cpu_to_le32s(&desc
->next
);
555 cpu_physical_memory_write(addr
, (void *)desc
, sizeof(*desc
));
558 static void eth_rx_desc_get(uint32_t addr
, mv88w8618_rx_desc
*desc
)
560 cpu_physical_memory_read(addr
, (void *)desc
, sizeof(*desc
));
561 le32_to_cpus(&desc
->cmdstat
);
562 le16_to_cpus(&desc
->bytes
);
563 le16_to_cpus(&desc
->buffer_size
);
564 le32_to_cpus(&desc
->buffer
);
565 le32_to_cpus(&desc
->next
);
568 static int eth_can_receive(void *opaque
)
573 static void eth_receive(void *opaque
, const uint8_t *buf
, int size
)
575 mv88w8618_eth_state
*s
= opaque
;
577 mv88w8618_rx_desc desc
;
580 for (i
= 0; i
< 4; i
++) {
581 desc_addr
= s
->cur_rx
[i
];
585 eth_rx_desc_get(desc_addr
, &desc
);
586 if ((desc
.cmdstat
& MP_ETH_RX_OWN
) && desc
.buffer_size
>= size
) {
587 cpu_physical_memory_write(desc
.buffer
+ s
->vlan_header
,
589 desc
.bytes
= size
+ s
->vlan_header
;
590 desc
.cmdstat
&= ~MP_ETH_RX_OWN
;
591 s
->cur_rx
[i
] = desc
.next
;
593 s
->icr
|= MP_ETH_IRQ_RX
;
595 qemu_irq_raise(s
->irq
);
596 eth_rx_desc_put(desc_addr
, &desc
);
599 desc_addr
= desc
.next
;
600 } while (desc_addr
!= s
->rx_queue
[i
]);
604 static void eth_tx_desc_put(uint32_t addr
, mv88w8618_tx_desc
*desc
)
606 cpu_to_le32s(&desc
->cmdstat
);
607 cpu_to_le16s(&desc
->res
);
608 cpu_to_le16s(&desc
->bytes
);
609 cpu_to_le32s(&desc
->buffer
);
610 cpu_to_le32s(&desc
->next
);
611 cpu_physical_memory_write(addr
, (void *)desc
, sizeof(*desc
));
614 static void eth_tx_desc_get(uint32_t addr
, mv88w8618_tx_desc
*desc
)
616 cpu_physical_memory_read(addr
, (void *)desc
, sizeof(*desc
));
617 le32_to_cpus(&desc
->cmdstat
);
618 le16_to_cpus(&desc
->res
);
619 le16_to_cpus(&desc
->bytes
);
620 le32_to_cpus(&desc
->buffer
);
621 le32_to_cpus(&desc
->next
);
624 static void eth_send(mv88w8618_eth_state
*s
, int queue_index
)
626 uint32_t desc_addr
= s
->tx_queue
[queue_index
];
627 mv88w8618_tx_desc desc
;
633 eth_tx_desc_get(desc_addr
, &desc
);
634 if (desc
.cmdstat
& MP_ETH_TX_OWN
) {
637 cpu_physical_memory_read(desc
.buffer
, buf
, len
);
638 qemu_send_packet(s
->vc
, buf
, len
);
640 desc
.cmdstat
&= ~MP_ETH_TX_OWN
;
641 s
->icr
|= 1 << (MP_ETH_IRQ_TXLO_BIT
- queue_index
);
642 eth_tx_desc_put(desc_addr
, &desc
);
644 desc_addr
= desc
.next
;
645 } while (desc_addr
!= s
->tx_queue
[queue_index
]);
648 static uint32_t mv88w8618_eth_read(void *opaque
, target_phys_addr_t offset
)
650 mv88w8618_eth_state
*s
= opaque
;
654 if (s
->smir
& MP_ETH_SMIR_OPCODE
) {
655 switch (s
->smir
& MP_ETH_SMIR_ADDR
) {
656 case MP_ETH_PHY1_BMSR
:
657 return MP_PHY_BMSR_LINK
| MP_PHY_BMSR_AUTONEG
|
659 case MP_ETH_PHY1_PHYSID1
:
660 return (MP_PHY_88E3015
>> 16) | MP_ETH_SMIR_RDVALID
;
661 case MP_ETH_PHY1_PHYSID2
:
662 return (MP_PHY_88E3015
& 0xFFFF) | MP_ETH_SMIR_RDVALID
;
664 return MP_ETH_SMIR_RDVALID
;
675 case MP_ETH_FRDP0
... MP_ETH_FRDP3
:
676 return s
->frx_queue
[(offset
- MP_ETH_FRDP0
)/4];
678 case MP_ETH_CRDP0
... MP_ETH_CRDP3
:
679 return s
->rx_queue
[(offset
- MP_ETH_CRDP0
)/4];
681 case MP_ETH_CTDP0
... MP_ETH_CTDP3
:
682 return s
->tx_queue
[(offset
- MP_ETH_CTDP0
)/4];
689 static void mv88w8618_eth_write(void *opaque
, target_phys_addr_t offset
,
692 mv88w8618_eth_state
*s
= opaque
;
700 s
->vlan_header
= ((value
>> MP_ETH_PCXR_2BSM_BIT
) & 1) * 2;
704 if (value
& MP_ETH_CMD_TXHI
)
706 if (value
& MP_ETH_CMD_TXLO
)
708 if (value
& (MP_ETH_CMD_TXHI
| MP_ETH_CMD_TXLO
) && s
->icr
& s
->imr
)
709 qemu_irq_raise(s
->irq
);
719 qemu_irq_raise(s
->irq
);
722 case MP_ETH_FRDP0
... MP_ETH_FRDP3
:
723 s
->frx_queue
[(offset
- MP_ETH_FRDP0
)/4] = value
;
726 case MP_ETH_CRDP0
... MP_ETH_CRDP3
:
727 s
->rx_queue
[(offset
- MP_ETH_CRDP0
)/4] =
728 s
->cur_rx
[(offset
- MP_ETH_CRDP0
)/4] = value
;
731 case MP_ETH_CTDP0
... MP_ETH_CTDP3
:
732 s
->tx_queue
[(offset
- MP_ETH_CTDP0
)/4] = value
;
737 static CPUReadMemoryFunc
*mv88w8618_eth_readfn
[] = {
743 static CPUWriteMemoryFunc
*mv88w8618_eth_writefn
[] = {
749 static void eth_cleanup(VLANClientState
*vc
)
751 mv88w8618_eth_state
*s
= vc
->opaque
;
753 cpu_unregister_io_memory(s
->mmio_index
);
758 static void mv88w8618_eth_init(NICInfo
*nd
, uint32_t base
, qemu_irq irq
)
760 mv88w8618_eth_state
*s
;
762 qemu_check_nic_model(nd
, "mv88w8618");
764 s
= qemu_mallocz(sizeof(mv88w8618_eth_state
));
766 s
->vc
= qemu_new_vlan_client(nd
->vlan
, nd
->model
, nd
->name
,
767 eth_receive
, eth_can_receive
,
769 s
->mmio_index
= cpu_register_io_memory(0, mv88w8618_eth_readfn
,
770 mv88w8618_eth_writefn
, s
);
771 cpu_register_physical_memory(base
, MP_ETH_SIZE
, s
->mmio_index
);
774 /* LCD register offsets */
775 #define MP_LCD_IRQCTRL 0x180
776 #define MP_LCD_IRQSTAT 0x184
777 #define MP_LCD_SPICTRL 0x1ac
778 #define MP_LCD_INST 0x1bc
779 #define MP_LCD_DATA 0x1c0
782 #define MP_LCD_SPI_DATA 0x00100011
783 #define MP_LCD_SPI_CMD 0x00104011
784 #define MP_LCD_SPI_INVALID 0x00000000
787 #define MP_LCD_INST_SETPAGE0 0xB0
789 #define MP_LCD_INST_SETPAGE7 0xB7
791 #define MP_LCD_TEXTCOLOR 0xe0e0ff /* RRGGBB */
793 typedef struct musicpal_lcd_state
{
799 uint8_t video_ram
[128*64/8];
800 } musicpal_lcd_state
;
802 static uint32_t lcd_brightness
;
804 static uint8_t scale_lcd_color(uint8_t col
)
808 switch (lcd_brightness
) {
809 case 0x00000007: /* 0 */
812 case 0x00020000: /* 1 */
813 return (tmp
* 1) / 7;
815 case 0x00020001: /* 2 */
816 return (tmp
* 2) / 7;
818 case 0x00040000: /* 3 */
819 return (tmp
* 3) / 7;
821 case 0x00010006: /* 4 */
822 return (tmp
* 4) / 7;
824 case 0x00020005: /* 5 */
825 return (tmp
* 5) / 7;
827 case 0x00040003: /* 6 */
828 return (tmp
* 6) / 7;
830 case 0x00030004: /* 7 */
836 #define SET_LCD_PIXEL(depth, type) \
837 static inline void glue(set_lcd_pixel, depth) \
838 (musicpal_lcd_state *s, int x, int y, type col) \
841 type *pixel = &((type *) ds_get_data(s->ds))[(y * 128 * 3 + x) * 3]; \
843 for (dy = 0; dy < 3; dy++, pixel += 127 * 3) \
844 for (dx = 0; dx < 3; dx++, pixel++) \
847 SET_LCD_PIXEL(8, uint8_t)
848 SET_LCD_PIXEL(16, uint16_t)
849 SET_LCD_PIXEL(32, uint32_t)
851 #include "pixel_ops.h"
853 static void lcd_refresh(void *opaque
)
855 musicpal_lcd_state
*s
= opaque
;
858 switch (ds_get_bits_per_pixel(s
->ds
)) {
861 #define LCD_REFRESH(depth, func) \
863 col = func(scale_lcd_color((MP_LCD_TEXTCOLOR >> 16) & 0xff), \
864 scale_lcd_color((MP_LCD_TEXTCOLOR >> 8) & 0xff), \
865 scale_lcd_color(MP_LCD_TEXTCOLOR & 0xff)); \
866 for (x = 0; x < 128; x++) \
867 for (y = 0; y < 64; y++) \
868 if (s->video_ram[x + (y/8)*128] & (1 << (y % 8))) \
869 glue(set_lcd_pixel, depth)(s, x, y, col); \
871 glue(set_lcd_pixel, depth)(s, x, y, 0); \
873 LCD_REFRESH(8, rgb_to_pixel8
)
874 LCD_REFRESH(16, rgb_to_pixel16
)
875 LCD_REFRESH(32, (is_surface_bgr(s
->ds
->surface
) ?
876 rgb_to_pixel32bgr
: rgb_to_pixel32
))
878 cpu_abort(cpu_single_env
, "unsupported colour depth %i\n",
879 ds_get_bits_per_pixel(s
->ds
));
882 dpy_update(s
->ds
, 0, 0, 128*3, 64*3);
885 static void lcd_invalidate(void *opaque
)
889 static uint32_t musicpal_lcd_read(void *opaque
, target_phys_addr_t offset
)
891 musicpal_lcd_state
*s
= opaque
;
902 static void musicpal_lcd_write(void *opaque
, target_phys_addr_t offset
,
905 musicpal_lcd_state
*s
= opaque
;
913 if (value
== MP_LCD_SPI_DATA
|| value
== MP_LCD_SPI_CMD
)
916 s
->mode
= MP_LCD_SPI_INVALID
;
920 if (value
>= MP_LCD_INST_SETPAGE0
&& value
<= MP_LCD_INST_SETPAGE7
) {
921 s
->page
= value
- MP_LCD_INST_SETPAGE0
;
927 if (s
->mode
== MP_LCD_SPI_CMD
) {
928 if (value
>= MP_LCD_INST_SETPAGE0
&&
929 value
<= MP_LCD_INST_SETPAGE7
) {
930 s
->page
= value
- MP_LCD_INST_SETPAGE0
;
933 } else if (s
->mode
== MP_LCD_SPI_DATA
) {
934 s
->video_ram
[s
->page
*128 + s
->page_off
] = value
;
935 s
->page_off
= (s
->page_off
+ 1) & 127;
941 static CPUReadMemoryFunc
*musicpal_lcd_readfn
[] = {
947 static CPUWriteMemoryFunc
*musicpal_lcd_writefn
[] = {
953 static void musicpal_lcd_init(void)
955 musicpal_lcd_state
*s
;
958 s
= qemu_mallocz(sizeof(musicpal_lcd_state
));
959 iomemtype
= cpu_register_io_memory(0, musicpal_lcd_readfn
,
960 musicpal_lcd_writefn
, s
);
961 cpu_register_physical_memory(MP_LCD_BASE
, MP_LCD_SIZE
, iomemtype
);
963 s
->ds
= graphic_console_init(lcd_refresh
, lcd_invalidate
,
965 qemu_console_resize(s
->ds
, 128*3, 64*3);
968 /* PIC register offsets */
969 #define MP_PIC_STATUS 0x00
970 #define MP_PIC_ENABLE_SET 0x08
971 #define MP_PIC_ENABLE_CLR 0x0C
973 typedef struct mv88w8618_pic_state
978 } mv88w8618_pic_state
;
980 static void mv88w8618_pic_update(mv88w8618_pic_state
*s
)
982 qemu_set_irq(s
->parent_irq
, (s
->level
& s
->enabled
));
985 static void mv88w8618_pic_set_irq(void *opaque
, int irq
, int level
)
987 mv88w8618_pic_state
*s
= opaque
;
990 s
->level
|= 1 << irq
;
992 s
->level
&= ~(1 << irq
);
993 mv88w8618_pic_update(s
);
996 static uint32_t mv88w8618_pic_read(void *opaque
, target_phys_addr_t offset
)
998 mv88w8618_pic_state
*s
= opaque
;
1002 return s
->level
& s
->enabled
;
1009 static void mv88w8618_pic_write(void *opaque
, target_phys_addr_t offset
,
1012 mv88w8618_pic_state
*s
= opaque
;
1015 case MP_PIC_ENABLE_SET
:
1016 s
->enabled
|= value
;
1019 case MP_PIC_ENABLE_CLR
:
1020 s
->enabled
&= ~value
;
1024 mv88w8618_pic_update(s
);
1027 static void mv88w8618_pic_reset(void *opaque
)
1029 mv88w8618_pic_state
*s
= opaque
;
1035 static CPUReadMemoryFunc
*mv88w8618_pic_readfn
[] = {
1041 static CPUWriteMemoryFunc
*mv88w8618_pic_writefn
[] = {
1042 mv88w8618_pic_write
,
1043 mv88w8618_pic_write
,
1047 static qemu_irq
*mv88w8618_pic_init(uint32_t base
, qemu_irq parent_irq
)
1049 mv88w8618_pic_state
*s
;
1053 s
= qemu_mallocz(sizeof(mv88w8618_pic_state
));
1054 qi
= qemu_allocate_irqs(mv88w8618_pic_set_irq
, s
, 32);
1055 s
->parent_irq
= parent_irq
;
1056 iomemtype
= cpu_register_io_memory(0, mv88w8618_pic_readfn
,
1057 mv88w8618_pic_writefn
, s
);
1058 cpu_register_physical_memory(base
, MP_PIC_SIZE
, iomemtype
);
1060 qemu_register_reset(mv88w8618_pic_reset
, s
);
1065 /* PIT register offsets */
1066 #define MP_PIT_TIMER1_LENGTH 0x00
1068 #define MP_PIT_TIMER4_LENGTH 0x0C
1069 #define MP_PIT_CONTROL 0x10
1070 #define MP_PIT_TIMER1_VALUE 0x14
1072 #define MP_PIT_TIMER4_VALUE 0x20
1073 #define MP_BOARD_RESET 0x34
1075 /* Magic board reset value (probably some watchdog behind it) */
1076 #define MP_BOARD_RESET_MAGIC 0x10000
1078 typedef struct mv88w8618_timer_state
{
1079 ptimer_state
*timer
;
1083 } mv88w8618_timer_state
;
1085 typedef struct mv88w8618_pit_state
{
1088 } mv88w8618_pit_state
;
1090 static void mv88w8618_timer_tick(void *opaque
)
1092 mv88w8618_timer_state
*s
= opaque
;
1094 qemu_irq_raise(s
->irq
);
1097 static void *mv88w8618_timer_init(uint32_t freq
, qemu_irq irq
)
1099 mv88w8618_timer_state
*s
;
1102 s
= qemu_mallocz(sizeof(mv88w8618_timer_state
));
1106 bh
= qemu_bh_new(mv88w8618_timer_tick
, s
);
1107 s
->timer
= ptimer_init(bh
);
1112 static uint32_t mv88w8618_pit_read(void *opaque
, target_phys_addr_t offset
)
1114 mv88w8618_pit_state
*s
= opaque
;
1115 mv88w8618_timer_state
*t
;
1118 case MP_PIT_TIMER1_VALUE
... MP_PIT_TIMER4_VALUE
:
1119 t
= s
->timer
[(offset
-MP_PIT_TIMER1_VALUE
) >> 2];
1120 return ptimer_get_count(t
->timer
);
1127 static void mv88w8618_pit_write(void *opaque
, target_phys_addr_t offset
,
1130 mv88w8618_pit_state
*s
= opaque
;
1131 mv88w8618_timer_state
*t
;
1135 case MP_PIT_TIMER1_LENGTH
... MP_PIT_TIMER4_LENGTH
:
1136 t
= s
->timer
[offset
>> 2];
1138 ptimer_set_limit(t
->timer
, t
->limit
, 1);
1141 case MP_PIT_CONTROL
:
1142 for (i
= 0; i
< 4; i
++) {
1145 ptimer_set_limit(t
->timer
, t
->limit
, 0);
1146 ptimer_set_freq(t
->timer
, t
->freq
);
1147 ptimer_run(t
->timer
, 0);
1153 case MP_BOARD_RESET
:
1154 if (value
== MP_BOARD_RESET_MAGIC
)
1155 qemu_system_reset_request();
1160 static CPUReadMemoryFunc
*mv88w8618_pit_readfn
[] = {
1166 static CPUWriteMemoryFunc
*mv88w8618_pit_writefn
[] = {
1167 mv88w8618_pit_write
,
1168 mv88w8618_pit_write
,
1172 static void mv88w8618_pit_init(uint32_t base
, qemu_irq
*pic
, int irq
)
1175 mv88w8618_pit_state
*s
;
1177 s
= qemu_mallocz(sizeof(mv88w8618_pit_state
));
1179 /* Letting them all run at 1 MHz is likely just a pragmatic
1180 * simplification. */
1181 s
->timer
[0] = mv88w8618_timer_init(1000000, pic
[irq
]);
1182 s
->timer
[1] = mv88w8618_timer_init(1000000, pic
[irq
+ 1]);
1183 s
->timer
[2] = mv88w8618_timer_init(1000000, pic
[irq
+ 2]);
1184 s
->timer
[3] = mv88w8618_timer_init(1000000, pic
[irq
+ 3]);
1186 iomemtype
= cpu_register_io_memory(0, mv88w8618_pit_readfn
,
1187 mv88w8618_pit_writefn
, s
);
1188 cpu_register_physical_memory(base
, MP_PIT_SIZE
, iomemtype
);
1191 /* Flash config register offsets */
1192 #define MP_FLASHCFG_CFGR0 0x04
1194 typedef struct mv88w8618_flashcfg_state
{
1196 } mv88w8618_flashcfg_state
;
1198 static uint32_t mv88w8618_flashcfg_read(void *opaque
,
1199 target_phys_addr_t offset
)
1201 mv88w8618_flashcfg_state
*s
= opaque
;
1204 case MP_FLASHCFG_CFGR0
:
1212 static void mv88w8618_flashcfg_write(void *opaque
, target_phys_addr_t offset
,
1215 mv88w8618_flashcfg_state
*s
= opaque
;
1218 case MP_FLASHCFG_CFGR0
:
1224 static CPUReadMemoryFunc
*mv88w8618_flashcfg_readfn
[] = {
1225 mv88w8618_flashcfg_read
,
1226 mv88w8618_flashcfg_read
,
1227 mv88w8618_flashcfg_read
1230 static CPUWriteMemoryFunc
*mv88w8618_flashcfg_writefn
[] = {
1231 mv88w8618_flashcfg_write
,
1232 mv88w8618_flashcfg_write
,
1233 mv88w8618_flashcfg_write
1236 static void mv88w8618_flashcfg_init(uint32_t base
)
1239 mv88w8618_flashcfg_state
*s
;
1241 s
= qemu_mallocz(sizeof(mv88w8618_flashcfg_state
));
1243 s
->cfgr0
= 0xfffe4285; /* Default as set by U-Boot for 8 MB flash */
1244 iomemtype
= cpu_register_io_memory(0, mv88w8618_flashcfg_readfn
,
1245 mv88w8618_flashcfg_writefn
, s
);
1246 cpu_register_physical_memory(base
, MP_FLASHCFG_SIZE
, iomemtype
);
1249 /* Misc register offsets */
1250 #define MP_MISC_BOARD_REVISION 0x18
1252 #define MP_BOARD_REVISION 0x31
1254 static uint32_t musicpal_misc_read(void *opaque
, target_phys_addr_t offset
)
1257 case MP_MISC_BOARD_REVISION
:
1258 return MP_BOARD_REVISION
;
1265 static void musicpal_misc_write(void *opaque
, target_phys_addr_t offset
,
1270 static CPUReadMemoryFunc
*musicpal_misc_readfn
[] = {
1276 static CPUWriteMemoryFunc
*musicpal_misc_writefn
[] = {
1277 musicpal_misc_write
,
1278 musicpal_misc_write
,
1279 musicpal_misc_write
,
1282 static void musicpal_misc_init(void)
1286 iomemtype
= cpu_register_io_memory(0, musicpal_misc_readfn
,
1287 musicpal_misc_writefn
, NULL
);
1288 cpu_register_physical_memory(MP_MISC_BASE
, MP_MISC_SIZE
, iomemtype
);
1291 /* WLAN register offsets */
1292 #define MP_WLAN_MAGIC1 0x11c
1293 #define MP_WLAN_MAGIC2 0x124
1295 static uint32_t mv88w8618_wlan_read(void *opaque
, target_phys_addr_t offset
)
1298 /* Workaround to allow loading the binary-only wlandrv.ko crap
1299 * from the original Freecom firmware. */
1300 case MP_WLAN_MAGIC1
:
1302 case MP_WLAN_MAGIC2
:
1310 static void mv88w8618_wlan_write(void *opaque
, target_phys_addr_t offset
,
1315 static CPUReadMemoryFunc
*mv88w8618_wlan_readfn
[] = {
1316 mv88w8618_wlan_read
,
1317 mv88w8618_wlan_read
,
1318 mv88w8618_wlan_read
,
1321 static CPUWriteMemoryFunc
*mv88w8618_wlan_writefn
[] = {
1322 mv88w8618_wlan_write
,
1323 mv88w8618_wlan_write
,
1324 mv88w8618_wlan_write
,
1327 static void mv88w8618_wlan_init(uint32_t base
)
1331 iomemtype
= cpu_register_io_memory(0, mv88w8618_wlan_readfn
,
1332 mv88w8618_wlan_writefn
, NULL
);
1333 cpu_register_physical_memory(base
, MP_WLAN_SIZE
, iomemtype
);
1336 /* GPIO register offsets */
1337 #define MP_GPIO_OE_LO 0x008
1338 #define MP_GPIO_OUT_LO 0x00c
1339 #define MP_GPIO_IN_LO 0x010
1340 #define MP_GPIO_ISR_LO 0x020
1341 #define MP_GPIO_OE_HI 0x508
1342 #define MP_GPIO_OUT_HI 0x50c
1343 #define MP_GPIO_IN_HI 0x510
1344 #define MP_GPIO_ISR_HI 0x520
1346 /* GPIO bits & masks */
1347 #define MP_GPIO_WHEEL_VOL (1 << 8)
1348 #define MP_GPIO_WHEEL_VOL_INV (1 << 9)
1349 #define MP_GPIO_WHEEL_NAV (1 << 10)
1350 #define MP_GPIO_WHEEL_NAV_INV (1 << 11)
1351 #define MP_GPIO_LCD_BRIGHTNESS 0x00070000
1352 #define MP_GPIO_BTN_FAVORITS (1 << 19)
1353 #define MP_GPIO_BTN_MENU (1 << 20)
1354 #define MP_GPIO_BTN_VOLUME (1 << 21)
1355 #define MP_GPIO_BTN_NAVIGATION (1 << 22)
1356 #define MP_GPIO_I2C_DATA_BIT 29
1357 #define MP_GPIO_I2C_DATA (1 << MP_GPIO_I2C_DATA_BIT)
1358 #define MP_GPIO_I2C_CLOCK_BIT 30
1360 /* LCD brightness bits in GPIO_OE_HI */
1361 #define MP_OE_LCD_BRIGHTNESS 0x0007
1363 static uint32_t musicpal_gpio_read(void *opaque
, target_phys_addr_t offset
)
1366 case MP_GPIO_OE_HI
: /* used for LCD brightness control */
1367 return lcd_brightness
& MP_OE_LCD_BRIGHTNESS
;
1369 case MP_GPIO_OUT_LO
:
1370 return gpio_out_state
& 0xFFFF;
1371 case MP_GPIO_OUT_HI
:
1372 return gpio_out_state
>> 16;
1375 return gpio_in_state
& 0xFFFF;
1377 /* Update received I2C data */
1378 gpio_in_state
= (gpio_in_state
& ~MP_GPIO_I2C_DATA
) |
1379 (i2c_get_data(mixer_i2c
) << MP_GPIO_I2C_DATA_BIT
);
1380 return gpio_in_state
>> 16;
1382 case MP_GPIO_ISR_LO
:
1383 return gpio_isr
& 0xFFFF;
1384 case MP_GPIO_ISR_HI
:
1385 return gpio_isr
>> 16;
1392 static void musicpal_gpio_write(void *opaque
, target_phys_addr_t offset
,
1396 case MP_GPIO_OE_HI
: /* used for LCD brightness control */
1397 lcd_brightness
= (lcd_brightness
& MP_GPIO_LCD_BRIGHTNESS
) |
1398 (value
& MP_OE_LCD_BRIGHTNESS
);
1401 case MP_GPIO_OUT_LO
:
1402 gpio_out_state
= (gpio_out_state
& 0xFFFF0000) | (value
& 0xFFFF);
1404 case MP_GPIO_OUT_HI
:
1405 gpio_out_state
= (gpio_out_state
& 0xFFFF) | (value
<< 16);
1406 lcd_brightness
= (lcd_brightness
& 0xFFFF) |
1407 (gpio_out_state
& MP_GPIO_LCD_BRIGHTNESS
);
1408 i2c_state_update(mixer_i2c
,
1409 (gpio_out_state
>> MP_GPIO_I2C_DATA_BIT
) & 1,
1410 (gpio_out_state
>> MP_GPIO_I2C_CLOCK_BIT
) & 1);
1416 static CPUReadMemoryFunc
*musicpal_gpio_readfn
[] = {
1422 static CPUWriteMemoryFunc
*musicpal_gpio_writefn
[] = {
1423 musicpal_gpio_write
,
1424 musicpal_gpio_write
,
1425 musicpal_gpio_write
,
1428 static void musicpal_gpio_init(void)
1432 iomemtype
= cpu_register_io_memory(0, musicpal_gpio_readfn
,
1433 musicpal_gpio_writefn
, NULL
);
1434 cpu_register_physical_memory(MP_GPIO_BASE
, MP_GPIO_SIZE
, iomemtype
);
1437 /* Keyboard codes & masks */
1438 #define KEY_RELEASED 0x80
1439 #define KEY_CODE 0x7f
1441 #define KEYCODE_TAB 0x0f
1442 #define KEYCODE_ENTER 0x1c
1443 #define KEYCODE_F 0x21
1444 #define KEYCODE_M 0x32
1446 #define KEYCODE_EXTENDED 0xe0
1447 #define KEYCODE_UP 0x48
1448 #define KEYCODE_DOWN 0x50
1449 #define KEYCODE_LEFT 0x4b
1450 #define KEYCODE_RIGHT 0x4d
1452 static void musicpal_key_event(void *opaque
, int keycode
)
1454 qemu_irq irq
= opaque
;
1456 static int kbd_extended
;
1458 if (keycode
== KEYCODE_EXTENDED
) {
1464 switch (keycode
& KEY_CODE
) {
1466 event
= MP_GPIO_WHEEL_NAV
| MP_GPIO_WHEEL_NAV_INV
;
1470 event
= MP_GPIO_WHEEL_NAV
;
1474 event
= MP_GPIO_WHEEL_VOL
| MP_GPIO_WHEEL_VOL_INV
;
1478 event
= MP_GPIO_WHEEL_VOL
;
1482 switch (keycode
& KEY_CODE
) {
1484 event
= MP_GPIO_BTN_FAVORITS
;
1488 event
= MP_GPIO_BTN_VOLUME
;
1492 event
= MP_GPIO_BTN_NAVIGATION
;
1496 event
= MP_GPIO_BTN_MENU
;
1499 /* Do not repeat already pressed buttons */
1500 if (!(keycode
& KEY_RELEASED
) && !(gpio_in_state
& event
))
1505 if (keycode
& KEY_RELEASED
) {
1506 gpio_in_state
|= event
;
1508 gpio_in_state
&= ~event
;
1510 qemu_irq_raise(irq
);
1517 static struct arm_boot_info musicpal_binfo
= {
1518 .loader_start
= 0x0,
1522 static void musicpal_init(ram_addr_t ram_size
, int vga_ram_size
,
1523 const char *boot_device
,
1524 const char *kernel_filename
, const char *kernel_cmdline
,
1525 const char *initrd_filename
, const char *cpu_model
)
1530 unsigned long flash_size
;
1533 cpu_model
= "arm926";
1535 env
= cpu_init(cpu_model
);
1537 fprintf(stderr
, "Unable to find CPU definition\n");
1540 pic
= arm_pic_init_cpu(env
);
1542 /* For now we use a fixed - the original - RAM size */
1543 cpu_register_physical_memory(0, MP_RAM_DEFAULT_SIZE
,
1544 qemu_ram_alloc(MP_RAM_DEFAULT_SIZE
));
1546 sram_off
= qemu_ram_alloc(MP_SRAM_SIZE
);
1547 cpu_register_physical_memory(MP_SRAM_BASE
, MP_SRAM_SIZE
, sram_off
);
1549 pic
= mv88w8618_pic_init(MP_PIC_BASE
, pic
[ARM_PIC_CPU_IRQ
]);
1550 mv88w8618_pit_init(MP_PIT_BASE
, pic
, MP_TIMER1_IRQ
);
1553 serial_mm_init(MP_UART1_BASE
, 2, pic
[MP_UART1_IRQ
], 1825000,
1556 serial_mm_init(MP_UART2_BASE
, 2, pic
[MP_UART2_IRQ
], 1825000,
1559 /* Register flash */
1560 index
= drive_get_index(IF_PFLASH
, 0, 0);
1562 flash_size
= bdrv_getlength(drives_table
[index
].bdrv
);
1563 if (flash_size
!= 8*1024*1024 && flash_size
!= 16*1024*1024 &&
1564 flash_size
!= 32*1024*1024) {
1565 fprintf(stderr
, "Invalid flash image size\n");
1570 * The original U-Boot accesses the flash at 0xFE000000 instead of
1571 * 0xFF800000 (if there is 8 MB flash). So remap flash access if the
1572 * image is smaller than 32 MB.
1574 pflash_cfi02_register(0-MP_FLASH_SIZE_MAX
, qemu_ram_alloc(flash_size
),
1575 drives_table
[index
].bdrv
, 0x10000,
1576 (flash_size
+ 0xffff) >> 16,
1577 MP_FLASH_SIZE_MAX
/ flash_size
,
1578 2, 0x00BF, 0x236D, 0x0000, 0x0000,
1581 mv88w8618_flashcfg_init(MP_FLASHCFG_BASE
);
1583 musicpal_lcd_init();
1585 qemu_add_kbd_event_handler(musicpal_key_event
, pic
[MP_GPIO_IRQ
]);
1587 mv88w8618_eth_init(&nd_table
[0], MP_ETH_BASE
, pic
[MP_ETH_IRQ
]);
1589 mixer_i2c
= musicpal_audio_init(pic
[MP_AUDIO_IRQ
]);
1591 mv88w8618_wlan_init(MP_WLAN_BASE
);
1593 musicpal_misc_init();
1594 musicpal_gpio_init();
1596 musicpal_binfo
.ram_size
= MP_RAM_DEFAULT_SIZE
;
1597 musicpal_binfo
.kernel_filename
= kernel_filename
;
1598 musicpal_binfo
.kernel_cmdline
= kernel_cmdline
;
1599 musicpal_binfo
.initrd_filename
= initrd_filename
;
1600 arm_load_kernel(env
, &musicpal_binfo
);
1603 QEMUMachine musicpal_machine
= {
1605 .desc
= "Marvell 88w8618 / MusicPal (ARM926EJ-S)",
1606 .init
= musicpal_init
,