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"
22 #define MP_MISC_BASE 0x80002000
23 #define MP_MISC_SIZE 0x00001000
25 #define MP_ETH_BASE 0x80008000
26 #define MP_ETH_SIZE 0x00001000
28 #define MP_WLAN_BASE 0x8000C000
29 #define MP_WLAN_SIZE 0x00000800
31 #define MP_UART1_BASE 0x8000C840
32 #define MP_UART2_BASE 0x8000C940
34 #define MP_GPIO_BASE 0x8000D000
35 #define MP_GPIO_SIZE 0x00001000
37 #define MP_FLASHCFG_BASE 0x90006000
38 #define MP_FLASHCFG_SIZE 0x00001000
40 #define MP_AUDIO_BASE 0x90007000
42 #define MP_PIC_BASE 0x90008000
43 #define MP_PIC_SIZE 0x00001000
45 #define MP_PIT_BASE 0x90009000
46 #define MP_PIT_SIZE 0x00001000
48 #define MP_LCD_BASE 0x9000c000
49 #define MP_LCD_SIZE 0x00001000
51 #define MP_SRAM_BASE 0xC0000000
52 #define MP_SRAM_SIZE 0x00020000
54 #define MP_RAM_DEFAULT_SIZE 32*1024*1024
55 #define MP_FLASH_SIZE_MAX 32*1024*1024
57 #define MP_TIMER1_IRQ 4
58 #define MP_TIMER2_IRQ 5
59 #define MP_TIMER3_IRQ 6
60 #define MP_TIMER4_IRQ 7
63 #define MP_UART1_IRQ 11
64 #define MP_UART2_IRQ 11
65 #define MP_GPIO_IRQ 12
67 #define MP_AUDIO_IRQ 30
69 /* Wolfson 8750 I2C address */
70 #define MP_WM_ADDR 0x34
72 /* Ethernet register offsets */
73 #define MP_ETH_SMIR 0x010
74 #define MP_ETH_PCXR 0x408
75 #define MP_ETH_SDCMR 0x448
76 #define MP_ETH_ICR 0x450
77 #define MP_ETH_IMR 0x458
78 #define MP_ETH_FRDP0 0x480
79 #define MP_ETH_FRDP1 0x484
80 #define MP_ETH_FRDP2 0x488
81 #define MP_ETH_FRDP3 0x48C
82 #define MP_ETH_CRDP0 0x4A0
83 #define MP_ETH_CRDP1 0x4A4
84 #define MP_ETH_CRDP2 0x4A8
85 #define MP_ETH_CRDP3 0x4AC
86 #define MP_ETH_CTDP0 0x4E0
87 #define MP_ETH_CTDP1 0x4E4
88 #define MP_ETH_CTDP2 0x4E8
89 #define MP_ETH_CTDP3 0x4EC
92 #define MP_ETH_SMIR_DATA 0x0000FFFF
93 #define MP_ETH_SMIR_ADDR 0x03FF0000
94 #define MP_ETH_SMIR_OPCODE (1 << 26) /* Read value */
95 #define MP_ETH_SMIR_RDVALID (1 << 27)
98 #define MP_ETH_PHY1_BMSR 0x00210000
99 #define MP_ETH_PHY1_PHYSID1 0x00410000
100 #define MP_ETH_PHY1_PHYSID2 0x00610000
102 #define MP_PHY_BMSR_LINK 0x0004
103 #define MP_PHY_BMSR_AUTONEG 0x0008
105 #define MP_PHY_88E3015 0x01410E20
107 /* TX descriptor status */
108 #define MP_ETH_TX_OWN (1 << 31)
110 /* RX descriptor status */
111 #define MP_ETH_RX_OWN (1 << 31)
113 /* Interrupt cause/mask bits */
114 #define MP_ETH_IRQ_RX_BIT 0
115 #define MP_ETH_IRQ_RX (1 << MP_ETH_IRQ_RX_BIT)
116 #define MP_ETH_IRQ_TXHI_BIT 2
117 #define MP_ETH_IRQ_TXLO_BIT 3
119 /* Port config bits */
120 #define MP_ETH_PCXR_2BSM_BIT 28 /* 2-byte incoming suffix */
122 /* SDMA command bits */
123 #define MP_ETH_CMD_TXHI (1 << 23)
124 #define MP_ETH_CMD_TXLO (1 << 22)
126 typedef struct mv88w8618_tx_desc
{
134 typedef struct mv88w8618_rx_desc
{
137 uint16_t buffer_size
;
142 typedef struct mv88w8618_eth_state
{
150 uint32_t tx_queue
[2];
151 uint32_t rx_queue
[4];
152 uint32_t frx_queue
[4];
155 } mv88w8618_eth_state
;
157 static void eth_rx_desc_put(uint32_t addr
, mv88w8618_rx_desc
*desc
)
159 cpu_to_le32s(&desc
->cmdstat
);
160 cpu_to_le16s(&desc
->bytes
);
161 cpu_to_le16s(&desc
->buffer_size
);
162 cpu_to_le32s(&desc
->buffer
);
163 cpu_to_le32s(&desc
->next
);
164 cpu_physical_memory_write(addr
, (void *)desc
, sizeof(*desc
));
167 static void eth_rx_desc_get(uint32_t addr
, mv88w8618_rx_desc
*desc
)
169 cpu_physical_memory_read(addr
, (void *)desc
, sizeof(*desc
));
170 le32_to_cpus(&desc
->cmdstat
);
171 le16_to_cpus(&desc
->bytes
);
172 le16_to_cpus(&desc
->buffer_size
);
173 le32_to_cpus(&desc
->buffer
);
174 le32_to_cpus(&desc
->next
);
177 static int eth_can_receive(VLANClientState
*vc
)
182 static ssize_t
eth_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
184 mv88w8618_eth_state
*s
= vc
->opaque
;
186 mv88w8618_rx_desc desc
;
189 for (i
= 0; i
< 4; i
++) {
190 desc_addr
= s
->cur_rx
[i
];
194 eth_rx_desc_get(desc_addr
, &desc
);
195 if ((desc
.cmdstat
& MP_ETH_RX_OWN
) && desc
.buffer_size
>= size
) {
196 cpu_physical_memory_write(desc
.buffer
+ s
->vlan_header
,
198 desc
.bytes
= size
+ s
->vlan_header
;
199 desc
.cmdstat
&= ~MP_ETH_RX_OWN
;
200 s
->cur_rx
[i
] = desc
.next
;
202 s
->icr
|= MP_ETH_IRQ_RX
;
204 qemu_irq_raise(s
->irq
);
205 eth_rx_desc_put(desc_addr
, &desc
);
208 desc_addr
= desc
.next
;
209 } while (desc_addr
!= s
->rx_queue
[i
]);
214 static void eth_tx_desc_put(uint32_t addr
, mv88w8618_tx_desc
*desc
)
216 cpu_to_le32s(&desc
->cmdstat
);
217 cpu_to_le16s(&desc
->res
);
218 cpu_to_le16s(&desc
->bytes
);
219 cpu_to_le32s(&desc
->buffer
);
220 cpu_to_le32s(&desc
->next
);
221 cpu_physical_memory_write(addr
, (void *)desc
, sizeof(*desc
));
224 static void eth_tx_desc_get(uint32_t addr
, mv88w8618_tx_desc
*desc
)
226 cpu_physical_memory_read(addr
, (void *)desc
, sizeof(*desc
));
227 le32_to_cpus(&desc
->cmdstat
);
228 le16_to_cpus(&desc
->res
);
229 le16_to_cpus(&desc
->bytes
);
230 le32_to_cpus(&desc
->buffer
);
231 le32_to_cpus(&desc
->next
);
234 static void eth_send(mv88w8618_eth_state
*s
, int queue_index
)
236 uint32_t desc_addr
= s
->tx_queue
[queue_index
];
237 mv88w8618_tx_desc desc
;
243 eth_tx_desc_get(desc_addr
, &desc
);
244 if (desc
.cmdstat
& MP_ETH_TX_OWN
) {
247 cpu_physical_memory_read(desc
.buffer
, buf
, len
);
248 qemu_send_packet(s
->vc
, buf
, len
);
250 desc
.cmdstat
&= ~MP_ETH_TX_OWN
;
251 s
->icr
|= 1 << (MP_ETH_IRQ_TXLO_BIT
- queue_index
);
252 eth_tx_desc_put(desc_addr
, &desc
);
254 desc_addr
= desc
.next
;
255 } while (desc_addr
!= s
->tx_queue
[queue_index
]);
258 static uint32_t mv88w8618_eth_read(void *opaque
, target_phys_addr_t offset
)
260 mv88w8618_eth_state
*s
= opaque
;
264 if (s
->smir
& MP_ETH_SMIR_OPCODE
) {
265 switch (s
->smir
& MP_ETH_SMIR_ADDR
) {
266 case MP_ETH_PHY1_BMSR
:
267 return MP_PHY_BMSR_LINK
| MP_PHY_BMSR_AUTONEG
|
269 case MP_ETH_PHY1_PHYSID1
:
270 return (MP_PHY_88E3015
>> 16) | MP_ETH_SMIR_RDVALID
;
271 case MP_ETH_PHY1_PHYSID2
:
272 return (MP_PHY_88E3015
& 0xFFFF) | MP_ETH_SMIR_RDVALID
;
274 return MP_ETH_SMIR_RDVALID
;
285 case MP_ETH_FRDP0
... MP_ETH_FRDP3
:
286 return s
->frx_queue
[(offset
- MP_ETH_FRDP0
)/4];
288 case MP_ETH_CRDP0
... MP_ETH_CRDP3
:
289 return s
->rx_queue
[(offset
- MP_ETH_CRDP0
)/4];
291 case MP_ETH_CTDP0
... MP_ETH_CTDP3
:
292 return s
->tx_queue
[(offset
- MP_ETH_CTDP0
)/4];
299 static void mv88w8618_eth_write(void *opaque
, target_phys_addr_t offset
,
302 mv88w8618_eth_state
*s
= opaque
;
310 s
->vlan_header
= ((value
>> MP_ETH_PCXR_2BSM_BIT
) & 1) * 2;
314 if (value
& MP_ETH_CMD_TXHI
)
316 if (value
& MP_ETH_CMD_TXLO
)
318 if (value
& (MP_ETH_CMD_TXHI
| MP_ETH_CMD_TXLO
) && s
->icr
& s
->imr
)
319 qemu_irq_raise(s
->irq
);
329 qemu_irq_raise(s
->irq
);
332 case MP_ETH_FRDP0
... MP_ETH_FRDP3
:
333 s
->frx_queue
[(offset
- MP_ETH_FRDP0
)/4] = value
;
336 case MP_ETH_CRDP0
... MP_ETH_CRDP3
:
337 s
->rx_queue
[(offset
- MP_ETH_CRDP0
)/4] =
338 s
->cur_rx
[(offset
- MP_ETH_CRDP0
)/4] = value
;
341 case MP_ETH_CTDP0
... MP_ETH_CTDP3
:
342 s
->tx_queue
[(offset
- MP_ETH_CTDP0
)/4] = value
;
347 static CPUReadMemoryFunc
* const mv88w8618_eth_readfn
[] = {
353 static CPUWriteMemoryFunc
* const mv88w8618_eth_writefn
[] = {
359 static void eth_cleanup(VLANClientState
*vc
)
361 mv88w8618_eth_state
*s
= vc
->opaque
;
363 cpu_unregister_io_memory(s
->mmio_index
);
368 static int mv88w8618_eth_init(SysBusDevice
*dev
)
370 mv88w8618_eth_state
*s
= FROM_SYSBUS(mv88w8618_eth_state
, dev
);
372 sysbus_init_irq(dev
, &s
->irq
);
373 s
->vc
= qdev_get_vlan_client(&dev
->qdev
,
374 eth_can_receive
, eth_receive
, NULL
,
376 s
->mmio_index
= cpu_register_io_memory(mv88w8618_eth_readfn
,
377 mv88w8618_eth_writefn
, s
);
378 sysbus_init_mmio(dev
, MP_ETH_SIZE
, s
->mmio_index
);
382 /* LCD register offsets */
383 #define MP_LCD_IRQCTRL 0x180
384 #define MP_LCD_IRQSTAT 0x184
385 #define MP_LCD_SPICTRL 0x1ac
386 #define MP_LCD_INST 0x1bc
387 #define MP_LCD_DATA 0x1c0
390 #define MP_LCD_SPI_DATA 0x00100011
391 #define MP_LCD_SPI_CMD 0x00104011
392 #define MP_LCD_SPI_INVALID 0x00000000
395 #define MP_LCD_INST_SETPAGE0 0xB0
397 #define MP_LCD_INST_SETPAGE7 0xB7
399 #define MP_LCD_TEXTCOLOR 0xe0e0ff /* RRGGBB */
401 typedef struct musicpal_lcd_state
{
409 uint8_t video_ram
[128*64/8];
410 } musicpal_lcd_state
;
412 static uint8_t scale_lcd_color(musicpal_lcd_state
*s
, uint8_t col
)
414 switch (s
->brightness
) {
420 return (col
* s
->brightness
) / 7;
424 #define SET_LCD_PIXEL(depth, type) \
425 static inline void glue(set_lcd_pixel, depth) \
426 (musicpal_lcd_state *s, int x, int y, type col) \
429 type *pixel = &((type *) ds_get_data(s->ds))[(y * 128 * 3 + x) * 3]; \
431 for (dy = 0; dy < 3; dy++, pixel += 127 * 3) \
432 for (dx = 0; dx < 3; dx++, pixel++) \
435 SET_LCD_PIXEL(8, uint8_t)
436 SET_LCD_PIXEL(16, uint16_t)
437 SET_LCD_PIXEL(32, uint32_t)
439 #include "pixel_ops.h"
441 static void lcd_refresh(void *opaque
)
443 musicpal_lcd_state
*s
= opaque
;
446 switch (ds_get_bits_per_pixel(s
->ds
)) {
449 #define LCD_REFRESH(depth, func) \
451 col = func(scale_lcd_color(s, (MP_LCD_TEXTCOLOR >> 16) & 0xff), \
452 scale_lcd_color(s, (MP_LCD_TEXTCOLOR >> 8) & 0xff), \
453 scale_lcd_color(s, MP_LCD_TEXTCOLOR & 0xff)); \
454 for (x = 0; x < 128; x++) \
455 for (y = 0; y < 64; y++) \
456 if (s->video_ram[x + (y/8)*128] & (1 << (y % 8))) \
457 glue(set_lcd_pixel, depth)(s, x, y, col); \
459 glue(set_lcd_pixel, depth)(s, x, y, 0); \
461 LCD_REFRESH(8, rgb_to_pixel8
)
462 LCD_REFRESH(16, rgb_to_pixel16
)
463 LCD_REFRESH(32, (is_surface_bgr(s
->ds
->surface
) ?
464 rgb_to_pixel32bgr
: rgb_to_pixel32
))
466 hw_error("unsupported colour depth %i\n",
467 ds_get_bits_per_pixel(s
->ds
));
470 dpy_update(s
->ds
, 0, 0, 128*3, 64*3);
473 static void lcd_invalidate(void *opaque
)
477 static void musicpal_lcd_gpio_brigthness_in(void *opaque
, int irq
, int level
)
479 musicpal_lcd_state
*s
= (musicpal_lcd_state
*) opaque
;
480 s
->brightness
&= ~(1 << irq
);
481 s
->brightness
|= level
<< irq
;
484 static uint32_t musicpal_lcd_read(void *opaque
, target_phys_addr_t offset
)
486 musicpal_lcd_state
*s
= opaque
;
497 static void musicpal_lcd_write(void *opaque
, target_phys_addr_t offset
,
500 musicpal_lcd_state
*s
= opaque
;
508 if (value
== MP_LCD_SPI_DATA
|| value
== MP_LCD_SPI_CMD
)
511 s
->mode
= MP_LCD_SPI_INVALID
;
515 if (value
>= MP_LCD_INST_SETPAGE0
&& value
<= MP_LCD_INST_SETPAGE7
) {
516 s
->page
= value
- MP_LCD_INST_SETPAGE0
;
522 if (s
->mode
== MP_LCD_SPI_CMD
) {
523 if (value
>= MP_LCD_INST_SETPAGE0
&&
524 value
<= MP_LCD_INST_SETPAGE7
) {
525 s
->page
= value
- MP_LCD_INST_SETPAGE0
;
528 } else if (s
->mode
== MP_LCD_SPI_DATA
) {
529 s
->video_ram
[s
->page
*128 + s
->page_off
] = value
;
530 s
->page_off
= (s
->page_off
+ 1) & 127;
536 static CPUReadMemoryFunc
* const musicpal_lcd_readfn
[] = {
542 static CPUWriteMemoryFunc
* const musicpal_lcd_writefn
[] = {
548 static int musicpal_lcd_init(SysBusDevice
*dev
)
550 musicpal_lcd_state
*s
= FROM_SYSBUS(musicpal_lcd_state
, dev
);
555 iomemtype
= cpu_register_io_memory(musicpal_lcd_readfn
,
556 musicpal_lcd_writefn
, s
);
557 sysbus_init_mmio(dev
, MP_LCD_SIZE
, iomemtype
);
559 s
->ds
= graphic_console_init(lcd_refresh
, lcd_invalidate
,
561 qemu_console_resize(s
->ds
, 128*3, 64*3);
563 qdev_init_gpio_in(&dev
->qdev
, musicpal_lcd_gpio_brigthness_in
, 3);
568 /* PIC register offsets */
569 #define MP_PIC_STATUS 0x00
570 #define MP_PIC_ENABLE_SET 0x08
571 #define MP_PIC_ENABLE_CLR 0x0C
573 typedef struct mv88w8618_pic_state
579 } mv88w8618_pic_state
;
581 static void mv88w8618_pic_update(mv88w8618_pic_state
*s
)
583 qemu_set_irq(s
->parent_irq
, (s
->level
& s
->enabled
));
586 static void mv88w8618_pic_set_irq(void *opaque
, int irq
, int level
)
588 mv88w8618_pic_state
*s
= opaque
;
591 s
->level
|= 1 << irq
;
593 s
->level
&= ~(1 << irq
);
594 mv88w8618_pic_update(s
);
597 static uint32_t mv88w8618_pic_read(void *opaque
, target_phys_addr_t offset
)
599 mv88w8618_pic_state
*s
= opaque
;
603 return s
->level
& s
->enabled
;
610 static void mv88w8618_pic_write(void *opaque
, target_phys_addr_t offset
,
613 mv88w8618_pic_state
*s
= opaque
;
616 case MP_PIC_ENABLE_SET
:
620 case MP_PIC_ENABLE_CLR
:
621 s
->enabled
&= ~value
;
625 mv88w8618_pic_update(s
);
628 static void mv88w8618_pic_reset(void *opaque
)
630 mv88w8618_pic_state
*s
= opaque
;
636 static CPUReadMemoryFunc
* const mv88w8618_pic_readfn
[] = {
642 static CPUWriteMemoryFunc
* const mv88w8618_pic_writefn
[] = {
648 static int mv88w8618_pic_init(SysBusDevice
*dev
)
650 mv88w8618_pic_state
*s
= FROM_SYSBUS(mv88w8618_pic_state
, dev
);
653 qdev_init_gpio_in(&dev
->qdev
, mv88w8618_pic_set_irq
, 32);
654 sysbus_init_irq(dev
, &s
->parent_irq
);
655 iomemtype
= cpu_register_io_memory(mv88w8618_pic_readfn
,
656 mv88w8618_pic_writefn
, s
);
657 sysbus_init_mmio(dev
, MP_PIC_SIZE
, iomemtype
);
659 qemu_register_reset(mv88w8618_pic_reset
, s
);
663 /* PIT register offsets */
664 #define MP_PIT_TIMER1_LENGTH 0x00
666 #define MP_PIT_TIMER4_LENGTH 0x0C
667 #define MP_PIT_CONTROL 0x10
668 #define MP_PIT_TIMER1_VALUE 0x14
670 #define MP_PIT_TIMER4_VALUE 0x20
671 #define MP_BOARD_RESET 0x34
673 /* Magic board reset value (probably some watchdog behind it) */
674 #define MP_BOARD_RESET_MAGIC 0x10000
676 typedef struct mv88w8618_timer_state
{
677 ptimer_state
*ptimer
;
681 } mv88w8618_timer_state
;
683 typedef struct mv88w8618_pit_state
{
685 mv88w8618_timer_state timer
[4];
687 } mv88w8618_pit_state
;
689 static void mv88w8618_timer_tick(void *opaque
)
691 mv88w8618_timer_state
*s
= opaque
;
693 qemu_irq_raise(s
->irq
);
696 static void mv88w8618_timer_init(SysBusDevice
*dev
, mv88w8618_timer_state
*s
,
701 sysbus_init_irq(dev
, &s
->irq
);
704 bh
= qemu_bh_new(mv88w8618_timer_tick
, s
);
705 s
->ptimer
= ptimer_init(bh
);
708 static uint32_t mv88w8618_pit_read(void *opaque
, target_phys_addr_t offset
)
710 mv88w8618_pit_state
*s
= opaque
;
711 mv88w8618_timer_state
*t
;
714 case MP_PIT_TIMER1_VALUE
... MP_PIT_TIMER4_VALUE
:
715 t
= &s
->timer
[(offset
-MP_PIT_TIMER1_VALUE
) >> 2];
716 return ptimer_get_count(t
->ptimer
);
723 static void mv88w8618_pit_write(void *opaque
, target_phys_addr_t offset
,
726 mv88w8618_pit_state
*s
= opaque
;
727 mv88w8618_timer_state
*t
;
731 case MP_PIT_TIMER1_LENGTH
... MP_PIT_TIMER4_LENGTH
:
732 t
= &s
->timer
[offset
>> 2];
734 ptimer_set_limit(t
->ptimer
, t
->limit
, 1);
738 for (i
= 0; i
< 4; i
++) {
741 ptimer_set_limit(t
->ptimer
, t
->limit
, 0);
742 ptimer_set_freq(t
->ptimer
, t
->freq
);
743 ptimer_run(t
->ptimer
, 0);
750 if (value
== MP_BOARD_RESET_MAGIC
)
751 qemu_system_reset_request();
756 static CPUReadMemoryFunc
* const mv88w8618_pit_readfn
[] = {
762 static CPUWriteMemoryFunc
* const mv88w8618_pit_writefn
[] = {
768 static int mv88w8618_pit_init(SysBusDevice
*dev
)
771 mv88w8618_pit_state
*s
= FROM_SYSBUS(mv88w8618_pit_state
, dev
);
774 /* Letting them all run at 1 MHz is likely just a pragmatic
776 for (i
= 0; i
< 4; i
++) {
777 mv88w8618_timer_init(dev
, &s
->timer
[i
], 1000000);
780 iomemtype
= cpu_register_io_memory(mv88w8618_pit_readfn
,
781 mv88w8618_pit_writefn
, s
);
782 sysbus_init_mmio(dev
, MP_PIT_SIZE
, iomemtype
);
786 /* Flash config register offsets */
787 #define MP_FLASHCFG_CFGR0 0x04
789 typedef struct mv88w8618_flashcfg_state
{
792 } mv88w8618_flashcfg_state
;
794 static uint32_t mv88w8618_flashcfg_read(void *opaque
,
795 target_phys_addr_t offset
)
797 mv88w8618_flashcfg_state
*s
= opaque
;
800 case MP_FLASHCFG_CFGR0
:
808 static void mv88w8618_flashcfg_write(void *opaque
, target_phys_addr_t offset
,
811 mv88w8618_flashcfg_state
*s
= opaque
;
814 case MP_FLASHCFG_CFGR0
:
820 static CPUReadMemoryFunc
* const mv88w8618_flashcfg_readfn
[] = {
821 mv88w8618_flashcfg_read
,
822 mv88w8618_flashcfg_read
,
823 mv88w8618_flashcfg_read
826 static CPUWriteMemoryFunc
* const mv88w8618_flashcfg_writefn
[] = {
827 mv88w8618_flashcfg_write
,
828 mv88w8618_flashcfg_write
,
829 mv88w8618_flashcfg_write
832 static int mv88w8618_flashcfg_init(SysBusDevice
*dev
)
835 mv88w8618_flashcfg_state
*s
= FROM_SYSBUS(mv88w8618_flashcfg_state
, dev
);
837 s
->cfgr0
= 0xfffe4285; /* Default as set by U-Boot for 8 MB flash */
838 iomemtype
= cpu_register_io_memory(mv88w8618_flashcfg_readfn
,
839 mv88w8618_flashcfg_writefn
, s
);
840 sysbus_init_mmio(dev
, MP_FLASHCFG_SIZE
, iomemtype
);
844 /* Misc register offsets */
845 #define MP_MISC_BOARD_REVISION 0x18
847 #define MP_BOARD_REVISION 0x31
849 static uint32_t musicpal_misc_read(void *opaque
, target_phys_addr_t offset
)
852 case MP_MISC_BOARD_REVISION
:
853 return MP_BOARD_REVISION
;
860 static void musicpal_misc_write(void *opaque
, target_phys_addr_t offset
,
865 static CPUReadMemoryFunc
* const musicpal_misc_readfn
[] = {
871 static CPUWriteMemoryFunc
* const musicpal_misc_writefn
[] = {
877 static void musicpal_misc_init(void)
881 iomemtype
= cpu_register_io_memory(musicpal_misc_readfn
,
882 musicpal_misc_writefn
, NULL
);
883 cpu_register_physical_memory(MP_MISC_BASE
, MP_MISC_SIZE
, iomemtype
);
886 /* WLAN register offsets */
887 #define MP_WLAN_MAGIC1 0x11c
888 #define MP_WLAN_MAGIC2 0x124
890 static uint32_t mv88w8618_wlan_read(void *opaque
, target_phys_addr_t offset
)
893 /* Workaround to allow loading the binary-only wlandrv.ko crap
894 * from the original Freecom firmware. */
905 static void mv88w8618_wlan_write(void *opaque
, target_phys_addr_t offset
,
910 static CPUReadMemoryFunc
* const mv88w8618_wlan_readfn
[] = {
916 static CPUWriteMemoryFunc
* const mv88w8618_wlan_writefn
[] = {
917 mv88w8618_wlan_write
,
918 mv88w8618_wlan_write
,
919 mv88w8618_wlan_write
,
922 static int mv88w8618_wlan_init(SysBusDevice
*dev
)
926 iomemtype
= cpu_register_io_memory(mv88w8618_wlan_readfn
,
927 mv88w8618_wlan_writefn
, NULL
);
928 sysbus_init_mmio(dev
, MP_WLAN_SIZE
, iomemtype
);
932 /* GPIO register offsets */
933 #define MP_GPIO_OE_LO 0x008
934 #define MP_GPIO_OUT_LO 0x00c
935 #define MP_GPIO_IN_LO 0x010
936 #define MP_GPIO_ISR_LO 0x020
937 #define MP_GPIO_OE_HI 0x508
938 #define MP_GPIO_OUT_HI 0x50c
939 #define MP_GPIO_IN_HI 0x510
940 #define MP_GPIO_ISR_HI 0x520
942 /* GPIO bits & masks */
943 #define MP_GPIO_LCD_BRIGHTNESS 0x00070000
944 #define MP_GPIO_I2C_DATA_BIT 29
945 #define MP_GPIO_I2C_DATA (1 << MP_GPIO_I2C_DATA_BIT)
946 #define MP_GPIO_I2C_CLOCK_BIT 30
948 /* LCD brightness bits in GPIO_OE_HI */
949 #define MP_OE_LCD_BRIGHTNESS 0x0007
951 typedef struct musicpal_gpio_state
{
953 uint32_t lcd_brightness
;
957 uint32_t i2c_read_data
;
958 uint32_t key_released
;
959 uint32_t keys_event
; /* store the received key event */
962 } musicpal_gpio_state
;
964 static void musicpal_gpio_brightness_update(musicpal_gpio_state
*s
) {
968 /* compute brightness ratio */
969 switch (s
->lcd_brightness
) {
1003 /* set lcd brightness GPIOs */
1004 for (i
= 0; i
<= 2; i
++)
1005 qemu_set_irq(s
->out
[i
], (brightness
>> i
) & 1);
1008 static void musicpal_gpio_keys_update(musicpal_gpio_state
*s
)
1012 /* transform the key state for GPIO usage */
1013 gpio_mask
|= (s
->keys_event
& 15) << 8;
1014 gpio_mask
|= ((s
->keys_event
>> 4) & 15) << 19;
1016 /* update GPIO state */
1017 if (s
->key_released
) {
1018 s
->in_state
|= gpio_mask
;
1020 s
->in_state
&= ~gpio_mask
;
1022 qemu_irq_raise(s
->irq
);
1026 static void musicpal_gpio_irq(void *opaque
, int irq
, int level
)
1028 musicpal_gpio_state
*s
= (musicpal_gpio_state
*) opaque
;
1031 s
->i2c_read_data
= level
;
1034 /* receives keys bits */
1036 s
->keys_event
&= ~(1 << irq
);
1037 s
->keys_event
|= level
<< irq
;
1041 /* receives key press/release */
1043 s
->key_released
= level
;
1047 /* a key has been transmited */
1048 if (irq
== 9 && level
== 1)
1049 musicpal_gpio_keys_update(s
);
1052 static uint32_t musicpal_gpio_read(void *opaque
, target_phys_addr_t offset
)
1054 musicpal_gpio_state
*s
= (musicpal_gpio_state
*) opaque
;
1057 case MP_GPIO_OE_HI
: /* used for LCD brightness control */
1058 return s
->lcd_brightness
& MP_OE_LCD_BRIGHTNESS
;
1060 case MP_GPIO_OUT_LO
:
1061 return s
->out_state
& 0xFFFF;
1062 case MP_GPIO_OUT_HI
:
1063 return s
->out_state
>> 16;
1066 return s
->in_state
& 0xFFFF;
1068 /* Update received I2C data */
1069 s
->in_state
= (s
->in_state
& ~MP_GPIO_I2C_DATA
) |
1070 (s
->i2c_read_data
<< MP_GPIO_I2C_DATA_BIT
);
1071 return s
->in_state
>> 16;
1073 case MP_GPIO_ISR_LO
:
1074 return s
->isr
& 0xFFFF;
1075 case MP_GPIO_ISR_HI
:
1076 return s
->isr
>> 16;
1083 static void musicpal_gpio_write(void *opaque
, target_phys_addr_t offset
,
1086 musicpal_gpio_state
*s
= (musicpal_gpio_state
*) opaque
;
1088 case MP_GPIO_OE_HI
: /* used for LCD brightness control */
1089 s
->lcd_brightness
= (s
->lcd_brightness
& MP_GPIO_LCD_BRIGHTNESS
) |
1090 (value
& MP_OE_LCD_BRIGHTNESS
);
1091 musicpal_gpio_brightness_update(s
);
1094 case MP_GPIO_OUT_LO
:
1095 s
->out_state
= (s
->out_state
& 0xFFFF0000) | (value
& 0xFFFF);
1097 case MP_GPIO_OUT_HI
:
1098 s
->out_state
= (s
->out_state
& 0xFFFF) | (value
<< 16);
1099 s
->lcd_brightness
= (s
->lcd_brightness
& 0xFFFF) |
1100 (s
->out_state
& MP_GPIO_LCD_BRIGHTNESS
);
1101 musicpal_gpio_brightness_update(s
);
1102 qemu_set_irq(s
->out
[3], (s
->out_state
>> MP_GPIO_I2C_DATA_BIT
) & 1);
1103 qemu_set_irq(s
->out
[4], (s
->out_state
>> MP_GPIO_I2C_CLOCK_BIT
) & 1);
1109 static CPUReadMemoryFunc
* const musicpal_gpio_readfn
[] = {
1115 static CPUWriteMemoryFunc
* const musicpal_gpio_writefn
[] = {
1116 musicpal_gpio_write
,
1117 musicpal_gpio_write
,
1118 musicpal_gpio_write
,
1121 static void musicpal_gpio_reset(musicpal_gpio_state
*s
)
1123 s
->in_state
= 0xffffffff;
1124 s
->i2c_read_data
= 1;
1125 s
->key_released
= 0;
1130 static int musicpal_gpio_init(SysBusDevice
*dev
)
1132 musicpal_gpio_state
*s
= FROM_SYSBUS(musicpal_gpio_state
, dev
);
1135 sysbus_init_irq(dev
, &s
->irq
);
1137 iomemtype
= cpu_register_io_memory(musicpal_gpio_readfn
,
1138 musicpal_gpio_writefn
, s
);
1139 sysbus_init_mmio(dev
, MP_GPIO_SIZE
, iomemtype
);
1141 musicpal_gpio_reset(s
);
1143 /* 3 brightness out + 2 lcd (data and clock ) */
1144 qdev_init_gpio_out(&dev
->qdev
, s
->out
, 5);
1145 /* 10 gpio button input + 1 I2C data input */
1146 qdev_init_gpio_in(&dev
->qdev
, musicpal_gpio_irq
, 11);
1151 /* Keyboard codes & masks */
1152 #define KEY_RELEASED 0x80
1153 #define KEY_CODE 0x7f
1155 #define KEYCODE_TAB 0x0f
1156 #define KEYCODE_ENTER 0x1c
1157 #define KEYCODE_F 0x21
1158 #define KEYCODE_M 0x32
1160 #define KEYCODE_EXTENDED 0xe0
1161 #define KEYCODE_UP 0x48
1162 #define KEYCODE_DOWN 0x50
1163 #define KEYCODE_LEFT 0x4b
1164 #define KEYCODE_RIGHT 0x4d
1166 #define MP_KEY_WHEEL_VOL (1)
1167 #define MP_KEY_WHEEL_VOL_INV (1 << 1)
1168 #define MP_KEY_WHEEL_NAV (1 << 2)
1169 #define MP_KEY_WHEEL_NAV_INV (1 << 3)
1170 #define MP_KEY_BTN_FAVORITS (1 << 4)
1171 #define MP_KEY_BTN_MENU (1 << 5)
1172 #define MP_KEY_BTN_VOLUME (1 << 6)
1173 #define MP_KEY_BTN_NAVIGATION (1 << 7)
1175 typedef struct musicpal_key_state
{
1176 SysBusDevice busdev
;
1177 uint32_t kbd_extended
;
1178 uint32_t keys_state
;
1180 } musicpal_key_state
;
1182 static void musicpal_key_event(void *opaque
, int keycode
)
1184 musicpal_key_state
*s
= (musicpal_key_state
*) opaque
;
1188 if (keycode
== KEYCODE_EXTENDED
) {
1189 s
->kbd_extended
= 1;
1193 if (s
->kbd_extended
)
1194 switch (keycode
& KEY_CODE
) {
1196 event
= MP_KEY_WHEEL_NAV
| MP_KEY_WHEEL_NAV_INV
;
1200 event
= MP_KEY_WHEEL_NAV
;
1204 event
= MP_KEY_WHEEL_VOL
| MP_KEY_WHEEL_VOL_INV
;
1208 event
= MP_KEY_WHEEL_VOL
;
1212 switch (keycode
& KEY_CODE
) {
1214 event
= MP_KEY_BTN_FAVORITS
;
1218 event
= MP_KEY_BTN_VOLUME
;
1222 event
= MP_KEY_BTN_NAVIGATION
;
1226 event
= MP_KEY_BTN_MENU
;
1229 /* Do not repeat already pressed buttons */
1230 if (!(keycode
& KEY_RELEASED
) && !(s
->keys_state
& event
))
1236 /* transmit key event on GPIOS */
1237 for (i
= 0; i
<= 7; i
++)
1238 qemu_set_irq(s
->out
[i
], (event
>> i
) & 1);
1240 /* handle key press/release */
1241 if (keycode
& KEY_RELEASED
) {
1242 s
->keys_state
|= event
;
1243 qemu_irq_raise(s
->out
[8]);
1245 s
->keys_state
&= ~event
;
1246 qemu_irq_lower(s
->out
[8]);
1249 /* signal that a key event occured */
1250 qemu_irq_pulse(s
->out
[9]);
1253 s
->kbd_extended
= 0;
1256 static int musicpal_key_init(SysBusDevice
*dev
)
1258 musicpal_key_state
*s
= FROM_SYSBUS(musicpal_key_state
, dev
);
1260 sysbus_init_mmio(dev
, 0x0, 0);
1262 s
->kbd_extended
= 0;
1265 /* 8 key event GPIO + 1 key press/release + 1 strobe */
1266 qdev_init_gpio_out(&dev
->qdev
, s
->out
, 10);
1268 qemu_add_kbd_event_handler(musicpal_key_event
, s
);
1273 static struct arm_boot_info musicpal_binfo
= {
1274 .loader_start
= 0x0,
1278 static void musicpal_init(ram_addr_t ram_size
,
1279 const char *boot_device
,
1280 const char *kernel_filename
, const char *kernel_cmdline
,
1281 const char *initrd_filename
, const char *cpu_model
)
1287 DeviceState
*i2c_dev
;
1288 DeviceState
*lcd_dev
;
1289 DeviceState
*key_dev
;
1291 DeviceState
*wm8750_dev
;
1296 unsigned long flash_size
;
1298 ram_addr_t sram_off
;
1301 cpu_model
= "arm926";
1303 env
= cpu_init(cpu_model
);
1305 fprintf(stderr
, "Unable to find CPU definition\n");
1308 cpu_pic
= arm_pic_init_cpu(env
);
1310 /* For now we use a fixed - the original - RAM size */
1311 cpu_register_physical_memory(0, MP_RAM_DEFAULT_SIZE
,
1312 qemu_ram_alloc(MP_RAM_DEFAULT_SIZE
));
1314 sram_off
= qemu_ram_alloc(MP_SRAM_SIZE
);
1315 cpu_register_physical_memory(MP_SRAM_BASE
, MP_SRAM_SIZE
, sram_off
);
1317 dev
= sysbus_create_simple("mv88w8618_pic", MP_PIC_BASE
,
1318 cpu_pic
[ARM_PIC_CPU_IRQ
]);
1319 for (i
= 0; i
< 32; i
++) {
1320 pic
[i
] = qdev_get_gpio_in(dev
, i
);
1322 sysbus_create_varargs("mv88w8618_pit", MP_PIT_BASE
, pic
[MP_TIMER1_IRQ
],
1323 pic
[MP_TIMER2_IRQ
], pic
[MP_TIMER3_IRQ
],
1324 pic
[MP_TIMER4_IRQ
], NULL
);
1327 serial_mm_init(MP_UART1_BASE
, 2, pic
[MP_UART1_IRQ
], 1825000,
1330 serial_mm_init(MP_UART2_BASE
, 2, pic
[MP_UART2_IRQ
], 1825000,
1333 /* Register flash */
1334 dinfo
= drive_get(IF_PFLASH
, 0, 0);
1336 flash_size
= bdrv_getlength(dinfo
->bdrv
);
1337 if (flash_size
!= 8*1024*1024 && flash_size
!= 16*1024*1024 &&
1338 flash_size
!= 32*1024*1024) {
1339 fprintf(stderr
, "Invalid flash image size\n");
1344 * The original U-Boot accesses the flash at 0xFE000000 instead of
1345 * 0xFF800000 (if there is 8 MB flash). So remap flash access if the
1346 * image is smaller than 32 MB.
1348 pflash_cfi02_register(0-MP_FLASH_SIZE_MAX
, qemu_ram_alloc(flash_size
),
1349 dinfo
->bdrv
, 0x10000,
1350 (flash_size
+ 0xffff) >> 16,
1351 MP_FLASH_SIZE_MAX
/ flash_size
,
1352 2, 0x00BF, 0x236D, 0x0000, 0x0000,
1355 sysbus_create_simple("mv88w8618_flashcfg", MP_FLASHCFG_BASE
, NULL
);
1357 qemu_check_nic_model(&nd_table
[0], "mv88w8618");
1358 dev
= qdev_create(NULL
, "mv88w8618_eth");
1359 dev
->nd
= &nd_table
[0];
1361 sysbus_mmio_map(sysbus_from_qdev(dev
), 0, MP_ETH_BASE
);
1362 sysbus_connect_irq(sysbus_from_qdev(dev
), 0, pic
[MP_ETH_IRQ
]);
1364 sysbus_create_simple("mv88w8618_wlan", MP_WLAN_BASE
, NULL
);
1366 musicpal_misc_init();
1368 dev
= sysbus_create_simple("musicpal_gpio", MP_GPIO_BASE
, pic
[MP_GPIO_IRQ
]);
1369 i2c_dev
= sysbus_create_simple("bitbang_i2c", 0, NULL
);
1370 i2c
= (i2c_bus
*)qdev_get_child_bus(i2c_dev
, "i2c");
1372 lcd_dev
= sysbus_create_simple("musicpal_lcd", MP_LCD_BASE
, NULL
);
1373 key_dev
= sysbus_create_simple("musicpal_key", 0, NULL
);
1376 qdev_connect_gpio_out(i2c_dev
, 0, qdev_get_gpio_in(dev
, 10));
1378 qdev_connect_gpio_out(dev
, 3, qdev_get_gpio_in(i2c_dev
, 0));
1380 qdev_connect_gpio_out(dev
, 4, qdev_get_gpio_in(i2c_dev
, 1));
1382 for (i
= 0; i
< 3; i
++)
1383 qdev_connect_gpio_out(dev
, i
, qdev_get_gpio_in(lcd_dev
, i
));
1385 for (i
= 0; i
< 10; i
++)
1386 qdev_connect_gpio_out(key_dev
, i
, qdev_get_gpio_in(dev
, i
));
1389 wm8750_dev
= i2c_create_slave(i2c
, "wm8750", MP_WM_ADDR
);
1390 dev
= qdev_create(NULL
, "mv88w8618_audio");
1391 s
= sysbus_from_qdev(dev
);
1392 qdev_prop_set_ptr(dev
, "wm8750", wm8750_dev
);
1394 sysbus_mmio_map(s
, 0, MP_AUDIO_BASE
);
1395 sysbus_connect_irq(s
, 0, pic
[MP_AUDIO_IRQ
]);
1398 musicpal_binfo
.ram_size
= MP_RAM_DEFAULT_SIZE
;
1399 musicpal_binfo
.kernel_filename
= kernel_filename
;
1400 musicpal_binfo
.kernel_cmdline
= kernel_cmdline
;
1401 musicpal_binfo
.initrd_filename
= initrd_filename
;
1402 arm_load_kernel(env
, &musicpal_binfo
);
1405 static QEMUMachine musicpal_machine
= {
1407 .desc
= "Marvell 88w8618 / MusicPal (ARM926EJ-S)",
1408 .init
= musicpal_init
,
1411 static void musicpal_machine_init(void)
1413 qemu_register_machine(&musicpal_machine
);
1416 machine_init(musicpal_machine_init
);
1418 static void musicpal_register_devices(void)
1420 sysbus_register_dev("mv88w8618_pic", sizeof(mv88w8618_pic_state
),
1421 mv88w8618_pic_init
);
1422 sysbus_register_dev("mv88w8618_pit", sizeof(mv88w8618_pit_state
),
1423 mv88w8618_pit_init
);
1424 sysbus_register_dev("mv88w8618_flashcfg", sizeof(mv88w8618_flashcfg_state
),
1425 mv88w8618_flashcfg_init
);
1426 sysbus_register_dev("mv88w8618_eth", sizeof(mv88w8618_eth_state
),
1427 mv88w8618_eth_init
);
1428 sysbus_register_dev("mv88w8618_wlan", sizeof(SysBusDevice
),
1429 mv88w8618_wlan_init
);
1430 sysbus_register_dev("musicpal_lcd", sizeof(musicpal_lcd_state
),
1432 sysbus_register_dev("musicpal_gpio", sizeof(musicpal_gpio_state
),
1433 musicpal_gpio_init
);
1434 sysbus_register_dev("musicpal_key", sizeof(musicpal_key_state
),
1438 device_init(musicpal_register_devices
)