musicpal: Fix regression caused by 6839 (Jan Kiszka)
[qemu-kvm/fedora.git] / hw / musicpal.c
blob8eb13160aa9a41205949452ee46213771fbeae85
1 /*
2 * Marvell MV88W8618 / Freecom MusicPal emulation.
4 * Copyright (c) 2008 Jan Kiszka
6 * This code is licenced under the GNU GPL v2.
7 */
9 #include "hw.h"
10 #include "arm-misc.h"
11 #include "devices.h"
12 #include "net.h"
13 #include "sysemu.h"
14 #include "boards.h"
15 #include "pc.h"
16 #include "qemu-timer.h"
17 #include "block.h"
18 #include "flash.h"
19 #include "console.h"
20 #include "audio/audio.h"
21 #include "i2c.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
60 /* ... */
61 #define MP_TIMER4_IRQ 7
62 #define MP_EHCI_IRQ 8
63 #define MP_ETH_IRQ 9
64 #define MP_UART1_IRQ 11
65 #define MP_UART2_IRQ 11
66 #define MP_GPIO_IRQ 12
67 #define MP_RTC_IRQ 28
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 /* Address conversion helpers */
76 static void *target2host_addr(uint32_t addr)
78 if (addr < MP_SRAM_BASE) {
79 if (addr >= MP_RAM_DEFAULT_SIZE)
80 return NULL;
81 return (void *)(phys_ram_base + addr);
82 } else {
83 if (addr >= MP_SRAM_BASE + MP_SRAM_SIZE)
84 return NULL;
85 return (void *)(phys_ram_base + sram_off + addr - MP_SRAM_BASE);
89 static uint32_t host2target_addr(void *addr)
91 if (addr < ((void *)phys_ram_base) + sram_off)
92 return (unsigned long)addr - (unsigned long)phys_ram_base;
93 else
94 return (unsigned long)addr - (unsigned long)phys_ram_base -
95 sram_off + MP_SRAM_BASE;
99 typedef enum i2c_state {
100 STOPPED = 0,
101 INITIALIZING,
102 SENDING_BIT7,
103 SENDING_BIT6,
104 SENDING_BIT5,
105 SENDING_BIT4,
106 SENDING_BIT3,
107 SENDING_BIT2,
108 SENDING_BIT1,
109 SENDING_BIT0,
110 WAITING_FOR_ACK,
111 RECEIVING_BIT7,
112 RECEIVING_BIT6,
113 RECEIVING_BIT5,
114 RECEIVING_BIT4,
115 RECEIVING_BIT3,
116 RECEIVING_BIT2,
117 RECEIVING_BIT1,
118 RECEIVING_BIT0,
119 SENDING_ACK
120 } i2c_state;
122 typedef struct i2c_interface {
123 i2c_bus *bus;
124 i2c_state state;
125 int last_data;
126 int last_clock;
127 uint8_t buffer;
128 int current_addr;
129 } i2c_interface;
131 static void i2c_enter_stop(i2c_interface *i2c)
133 if (i2c->current_addr >= 0)
134 i2c_end_transfer(i2c->bus);
135 i2c->current_addr = -1;
136 i2c->state = STOPPED;
139 static void i2c_state_update(i2c_interface *i2c, int data, int clock)
141 if (!i2c)
142 return;
144 switch (i2c->state) {
145 case STOPPED:
146 if (data == 0 && i2c->last_data == 1 && clock == 1)
147 i2c->state = INITIALIZING;
148 break;
150 case INITIALIZING:
151 if (clock == 0 && i2c->last_clock == 1 && data == 0)
152 i2c->state = SENDING_BIT7;
153 else
154 i2c_enter_stop(i2c);
155 break;
157 case SENDING_BIT7 ... SENDING_BIT0:
158 if (clock == 0 && i2c->last_clock == 1) {
159 i2c->buffer = (i2c->buffer << 1) | data;
160 i2c->state++; /* will end up in WAITING_FOR_ACK */
161 } else if (data == 1 && i2c->last_data == 0 && clock == 1)
162 i2c_enter_stop(i2c);
163 break;
165 case WAITING_FOR_ACK:
166 if (clock == 0 && i2c->last_clock == 1) {
167 if (i2c->current_addr < 0) {
168 i2c->current_addr = i2c->buffer;
169 i2c_start_transfer(i2c->bus, i2c->current_addr & 0xfe,
170 i2c->buffer & 1);
171 } else
172 i2c_send(i2c->bus, i2c->buffer);
173 if (i2c->current_addr & 1) {
174 i2c->state = RECEIVING_BIT7;
175 i2c->buffer = i2c_recv(i2c->bus);
176 } else
177 i2c->state = SENDING_BIT7;
178 } else if (data == 1 && i2c->last_data == 0 && clock == 1)
179 i2c_enter_stop(i2c);
180 break;
182 case RECEIVING_BIT7 ... RECEIVING_BIT0:
183 if (clock == 0 && i2c->last_clock == 1) {
184 i2c->state++; /* will end up in SENDING_ACK */
185 i2c->buffer <<= 1;
186 } else if (data == 1 && i2c->last_data == 0 && clock == 1)
187 i2c_enter_stop(i2c);
188 break;
190 case SENDING_ACK:
191 if (clock == 0 && i2c->last_clock == 1) {
192 i2c->state = RECEIVING_BIT7;
193 if (data == 0)
194 i2c->buffer = i2c_recv(i2c->bus);
195 else
196 i2c_nack(i2c->bus);
197 } else if (data == 1 && i2c->last_data == 0 && clock == 1)
198 i2c_enter_stop(i2c);
199 break;
202 i2c->last_data = data;
203 i2c->last_clock = clock;
206 static int i2c_get_data(i2c_interface *i2c)
208 if (!i2c)
209 return 0;
211 switch (i2c->state) {
212 case RECEIVING_BIT7 ... RECEIVING_BIT0:
213 return (i2c->buffer >> 7);
215 case WAITING_FOR_ACK:
216 default:
217 return 0;
221 static i2c_interface *mixer_i2c;
223 #ifdef HAS_AUDIO
225 /* Audio register offsets */
226 #define MP_AUDIO_PLAYBACK_MODE 0x00
227 #define MP_AUDIO_CLOCK_DIV 0x18
228 #define MP_AUDIO_IRQ_STATUS 0x20
229 #define MP_AUDIO_IRQ_ENABLE 0x24
230 #define MP_AUDIO_TX_START_LO 0x28
231 #define MP_AUDIO_TX_THRESHOLD 0x2C
232 #define MP_AUDIO_TX_STATUS 0x38
233 #define MP_AUDIO_TX_START_HI 0x40
235 /* Status register and IRQ enable bits */
236 #define MP_AUDIO_TX_HALF (1 << 6)
237 #define MP_AUDIO_TX_FULL (1 << 7)
239 /* Playback mode bits */
240 #define MP_AUDIO_16BIT_SAMPLE (1 << 0)
241 #define MP_AUDIO_PLAYBACK_EN (1 << 7)
242 #define MP_AUDIO_CLOCK_24MHZ (1 << 9)
243 #define MP_AUDIO_MONO (1 << 14)
245 /* Wolfson 8750 I2C address */
246 #define MP_WM_ADDR 0x34
248 static const char audio_name[] = "mv88w8618";
250 typedef struct musicpal_audio_state {
251 qemu_irq irq;
252 uint32_t playback_mode;
253 uint32_t status;
254 uint32_t irq_enable;
255 unsigned long phys_buf;
256 int8_t *target_buffer;
257 unsigned int threshold;
258 unsigned int play_pos;
259 unsigned int last_free;
260 uint32_t clock_div;
261 i2c_slave *wm;
262 } musicpal_audio_state;
264 static void audio_callback(void *opaque, int free_out, int free_in)
266 musicpal_audio_state *s = opaque;
267 int16_t *codec_buffer;
268 int8_t *mem_buffer;
269 int pos, block_size;
271 if (!(s->playback_mode & MP_AUDIO_PLAYBACK_EN))
272 return;
274 if (s->playback_mode & MP_AUDIO_16BIT_SAMPLE)
275 free_out <<= 1;
277 if (!(s->playback_mode & MP_AUDIO_MONO))
278 free_out <<= 1;
280 block_size = s->threshold/2;
281 if (free_out - s->last_free < block_size)
282 return;
284 mem_buffer = s->target_buffer + s->play_pos;
285 if (s->playback_mode & MP_AUDIO_16BIT_SAMPLE) {
286 if (s->playback_mode & MP_AUDIO_MONO) {
287 codec_buffer = wm8750_dac_buffer(s->wm, block_size >> 1);
288 for (pos = 0; pos < block_size; pos += 2) {
289 *codec_buffer++ = *(int16_t *)mem_buffer;
290 *codec_buffer++ = *(int16_t *)mem_buffer;
291 mem_buffer += 2;
293 } else
294 memcpy(wm8750_dac_buffer(s->wm, block_size >> 2),
295 (uint32_t *)mem_buffer, block_size);
296 } else {
297 if (s->playback_mode & MP_AUDIO_MONO) {
298 codec_buffer = wm8750_dac_buffer(s->wm, block_size);
299 for (pos = 0; pos < block_size; pos++) {
300 *codec_buffer++ = cpu_to_le16(256 * *mem_buffer);
301 *codec_buffer++ = cpu_to_le16(256 * *mem_buffer++);
303 } else {
304 codec_buffer = wm8750_dac_buffer(s->wm, block_size >> 1);
305 for (pos = 0; pos < block_size; pos += 2) {
306 *codec_buffer++ = cpu_to_le16(256 * *mem_buffer++);
307 *codec_buffer++ = cpu_to_le16(256 * *mem_buffer++);
311 wm8750_dac_commit(s->wm);
313 s->last_free = free_out - block_size;
315 if (s->play_pos == 0) {
316 s->status |= MP_AUDIO_TX_HALF;
317 s->play_pos = block_size;
318 } else {
319 s->status |= MP_AUDIO_TX_FULL;
320 s->play_pos = 0;
323 if (s->status & s->irq_enable)
324 qemu_irq_raise(s->irq);
327 static void musicpal_audio_clock_update(musicpal_audio_state *s)
329 int rate;
331 if (s->playback_mode & MP_AUDIO_CLOCK_24MHZ)
332 rate = 24576000 / 64; /* 24.576MHz */
333 else
334 rate = 11289600 / 64; /* 11.2896MHz */
336 rate /= ((s->clock_div >> 8) & 0xff) + 1;
338 wm8750_set_bclk_in(s->wm, rate);
341 static uint32_t musicpal_audio_read(void *opaque, target_phys_addr_t offset)
343 musicpal_audio_state *s = opaque;
345 switch (offset) {
346 case MP_AUDIO_PLAYBACK_MODE:
347 return s->playback_mode;
349 case MP_AUDIO_CLOCK_DIV:
350 return s->clock_div;
352 case MP_AUDIO_IRQ_STATUS:
353 return s->status;
355 case MP_AUDIO_IRQ_ENABLE:
356 return s->irq_enable;
358 case MP_AUDIO_TX_STATUS:
359 return s->play_pos >> 2;
361 default:
362 return 0;
366 static void musicpal_audio_write(void *opaque, target_phys_addr_t offset,
367 uint32_t value)
369 musicpal_audio_state *s = opaque;
371 switch (offset) {
372 case MP_AUDIO_PLAYBACK_MODE:
373 if (value & MP_AUDIO_PLAYBACK_EN &&
374 !(s->playback_mode & MP_AUDIO_PLAYBACK_EN)) {
375 s->status = 0;
376 s->last_free = 0;
377 s->play_pos = 0;
379 s->playback_mode = value;
380 musicpal_audio_clock_update(s);
381 break;
383 case MP_AUDIO_CLOCK_DIV:
384 s->clock_div = value;
385 s->last_free = 0;
386 s->play_pos = 0;
387 musicpal_audio_clock_update(s);
388 break;
390 case MP_AUDIO_IRQ_STATUS:
391 s->status &= ~value;
392 break;
394 case MP_AUDIO_IRQ_ENABLE:
395 s->irq_enable = value;
396 if (s->status & s->irq_enable)
397 qemu_irq_raise(s->irq);
398 break;
400 case MP_AUDIO_TX_START_LO:
401 s->phys_buf = (s->phys_buf & 0xFFFF0000) | (value & 0xFFFF);
402 s->target_buffer = target2host_addr(s->phys_buf);
403 s->play_pos = 0;
404 s->last_free = 0;
405 break;
407 case MP_AUDIO_TX_THRESHOLD:
408 s->threshold = (value + 1) * 4;
409 break;
411 case MP_AUDIO_TX_START_HI:
412 s->phys_buf = (s->phys_buf & 0xFFFF) | (value << 16);
413 s->target_buffer = target2host_addr(s->phys_buf);
414 s->play_pos = 0;
415 s->last_free = 0;
416 break;
420 static void musicpal_audio_reset(void *opaque)
422 musicpal_audio_state *s = opaque;
424 s->playback_mode = 0;
425 s->status = 0;
426 s->irq_enable = 0;
429 static CPUReadMemoryFunc *musicpal_audio_readfn[] = {
430 musicpal_audio_read,
431 musicpal_audio_read,
432 musicpal_audio_read
435 static CPUWriteMemoryFunc *musicpal_audio_writefn[] = {
436 musicpal_audio_write,
437 musicpal_audio_write,
438 musicpal_audio_write
441 static i2c_interface *musicpal_audio_init(qemu_irq irq)
443 AudioState *audio;
444 musicpal_audio_state *s;
445 i2c_interface *i2c;
446 int iomemtype;
448 audio = AUD_init();
449 if (!audio) {
450 AUD_log(audio_name, "No audio state\n");
451 return NULL;
454 s = qemu_mallocz(sizeof(musicpal_audio_state));
455 s->irq = irq;
457 i2c = qemu_mallocz(sizeof(i2c_interface));
458 i2c->bus = i2c_init_bus();
459 i2c->current_addr = -1;
461 s->wm = wm8750_init(i2c->bus, audio);
462 if (!s->wm)
463 return NULL;
464 i2c_set_slave_address(s->wm, MP_WM_ADDR);
465 wm8750_data_req_set(s->wm, audio_callback, s);
467 iomemtype = cpu_register_io_memory(0, musicpal_audio_readfn,
468 musicpal_audio_writefn, s);
469 cpu_register_physical_memory(MP_AUDIO_BASE, MP_AUDIO_SIZE, iomemtype);
471 qemu_register_reset(musicpal_audio_reset, s);
473 return i2c;
475 #else /* !HAS_AUDIO */
476 static i2c_interface *musicpal_audio_init(qemu_irq irq)
478 return NULL;
480 #endif /* !HAS_AUDIO */
482 /* Ethernet register offsets */
483 #define MP_ETH_SMIR 0x010
484 #define MP_ETH_PCXR 0x408
485 #define MP_ETH_SDCMR 0x448
486 #define MP_ETH_ICR 0x450
487 #define MP_ETH_IMR 0x458
488 #define MP_ETH_FRDP0 0x480
489 #define MP_ETH_FRDP1 0x484
490 #define MP_ETH_FRDP2 0x488
491 #define MP_ETH_FRDP3 0x48C
492 #define MP_ETH_CRDP0 0x4A0
493 #define MP_ETH_CRDP1 0x4A4
494 #define MP_ETH_CRDP2 0x4A8
495 #define MP_ETH_CRDP3 0x4AC
496 #define MP_ETH_CTDP0 0x4E0
497 #define MP_ETH_CTDP1 0x4E4
498 #define MP_ETH_CTDP2 0x4E8
499 #define MP_ETH_CTDP3 0x4EC
501 /* MII PHY access */
502 #define MP_ETH_SMIR_DATA 0x0000FFFF
503 #define MP_ETH_SMIR_ADDR 0x03FF0000
504 #define MP_ETH_SMIR_OPCODE (1 << 26) /* Read value */
505 #define MP_ETH_SMIR_RDVALID (1 << 27)
507 /* PHY registers */
508 #define MP_ETH_PHY1_BMSR 0x00210000
509 #define MP_ETH_PHY1_PHYSID1 0x00410000
510 #define MP_ETH_PHY1_PHYSID2 0x00610000
512 #define MP_PHY_BMSR_LINK 0x0004
513 #define MP_PHY_BMSR_AUTONEG 0x0008
515 #define MP_PHY_88E3015 0x01410E20
517 /* TX descriptor status */
518 #define MP_ETH_TX_OWN (1 << 31)
520 /* RX descriptor status */
521 #define MP_ETH_RX_OWN (1 << 31)
523 /* Interrupt cause/mask bits */
524 #define MP_ETH_IRQ_RX_BIT 0
525 #define MP_ETH_IRQ_RX (1 << MP_ETH_IRQ_RX_BIT)
526 #define MP_ETH_IRQ_TXHI_BIT 2
527 #define MP_ETH_IRQ_TXLO_BIT 3
529 /* Port config bits */
530 #define MP_ETH_PCXR_2BSM_BIT 28 /* 2-byte incoming suffix */
532 /* SDMA command bits */
533 #define MP_ETH_CMD_TXHI (1 << 23)
534 #define MP_ETH_CMD_TXLO (1 << 22)
536 typedef struct mv88w8618_tx_desc {
537 uint32_t cmdstat;
538 uint16_t res;
539 uint16_t bytes;
540 uint32_t buffer;
541 uint32_t next;
542 } mv88w8618_tx_desc;
544 typedef struct mv88w8618_rx_desc {
545 uint32_t cmdstat;
546 uint16_t bytes;
547 uint16_t buffer_size;
548 uint32_t buffer;
549 uint32_t next;
550 } mv88w8618_rx_desc;
552 typedef struct mv88w8618_eth_state {
553 qemu_irq irq;
554 uint32_t smir;
555 uint32_t icr;
556 uint32_t imr;
557 int vlan_header;
558 mv88w8618_tx_desc *tx_queue[2];
559 mv88w8618_rx_desc *rx_queue[4];
560 mv88w8618_rx_desc *frx_queue[4];
561 mv88w8618_rx_desc *cur_rx[4];
562 VLANClientState *vc;
563 } mv88w8618_eth_state;
565 static int eth_can_receive(void *opaque)
567 return 1;
570 static void eth_receive(void *opaque, const uint8_t *buf, int size)
572 mv88w8618_eth_state *s = opaque;
573 mv88w8618_rx_desc *desc;
574 int i;
576 for (i = 0; i < 4; i++) {
577 desc = s->cur_rx[i];
578 if (!desc)
579 continue;
580 do {
581 if (le32_to_cpu(desc->cmdstat) & MP_ETH_RX_OWN &&
582 le16_to_cpu(desc->buffer_size) >= size) {
583 memcpy(target2host_addr(le32_to_cpu(desc->buffer) +
584 s->vlan_header),
585 buf, size);
586 desc->bytes = cpu_to_le16(size + s->vlan_header);
587 desc->cmdstat &= cpu_to_le32(~MP_ETH_RX_OWN);
588 s->cur_rx[i] = target2host_addr(le32_to_cpu(desc->next));
590 s->icr |= MP_ETH_IRQ_RX;
591 if (s->icr & s->imr)
592 qemu_irq_raise(s->irq);
593 return;
595 desc = target2host_addr(le32_to_cpu(desc->next));
596 } while (desc != s->rx_queue[i]);
600 static void eth_send(mv88w8618_eth_state *s, int queue_index)
602 mv88w8618_tx_desc *desc = s->tx_queue[queue_index];
604 do {
605 if (le32_to_cpu(desc->cmdstat) & MP_ETH_TX_OWN) {
606 qemu_send_packet(s->vc,
607 target2host_addr(le32_to_cpu(desc->buffer)),
608 le16_to_cpu(desc->bytes));
609 desc->cmdstat &= cpu_to_le32(~MP_ETH_TX_OWN);
610 s->icr |= 1 << (MP_ETH_IRQ_TXLO_BIT - queue_index);
612 desc = target2host_addr(le32_to_cpu(desc->next));
613 } while (desc != s->tx_queue[queue_index]);
616 static uint32_t mv88w8618_eth_read(void *opaque, target_phys_addr_t offset)
618 mv88w8618_eth_state *s = opaque;
620 switch (offset) {
621 case MP_ETH_SMIR:
622 if (s->smir & MP_ETH_SMIR_OPCODE) {
623 switch (s->smir & MP_ETH_SMIR_ADDR) {
624 case MP_ETH_PHY1_BMSR:
625 return MP_PHY_BMSR_LINK | MP_PHY_BMSR_AUTONEG |
626 MP_ETH_SMIR_RDVALID;
627 case MP_ETH_PHY1_PHYSID1:
628 return (MP_PHY_88E3015 >> 16) | MP_ETH_SMIR_RDVALID;
629 case MP_ETH_PHY1_PHYSID2:
630 return (MP_PHY_88E3015 & 0xFFFF) | MP_ETH_SMIR_RDVALID;
631 default:
632 return MP_ETH_SMIR_RDVALID;
635 return 0;
637 case MP_ETH_ICR:
638 return s->icr;
640 case MP_ETH_IMR:
641 return s->imr;
643 case MP_ETH_FRDP0 ... MP_ETH_FRDP3:
644 return host2target_addr(s->frx_queue[(offset - MP_ETH_FRDP0)/4]);
646 case MP_ETH_CRDP0 ... MP_ETH_CRDP3:
647 return host2target_addr(s->rx_queue[(offset - MP_ETH_CRDP0)/4]);
649 case MP_ETH_CTDP0 ... MP_ETH_CTDP3:
650 return host2target_addr(s->tx_queue[(offset - MP_ETH_CTDP0)/4]);
652 default:
653 return 0;
657 static void mv88w8618_eth_write(void *opaque, target_phys_addr_t offset,
658 uint32_t value)
660 mv88w8618_eth_state *s = opaque;
662 switch (offset) {
663 case MP_ETH_SMIR:
664 s->smir = value;
665 break;
667 case MP_ETH_PCXR:
668 s->vlan_header = ((value >> MP_ETH_PCXR_2BSM_BIT) & 1) * 2;
669 break;
671 case MP_ETH_SDCMR:
672 if (value & MP_ETH_CMD_TXHI)
673 eth_send(s, 1);
674 if (value & MP_ETH_CMD_TXLO)
675 eth_send(s, 0);
676 if (value & (MP_ETH_CMD_TXHI | MP_ETH_CMD_TXLO) && s->icr & s->imr)
677 qemu_irq_raise(s->irq);
678 break;
680 case MP_ETH_ICR:
681 s->icr &= value;
682 break;
684 case MP_ETH_IMR:
685 s->imr = value;
686 if (s->icr & s->imr)
687 qemu_irq_raise(s->irq);
688 break;
690 case MP_ETH_FRDP0 ... MP_ETH_FRDP3:
691 s->frx_queue[(offset - MP_ETH_FRDP0)/4] = target2host_addr(value);
692 break;
694 case MP_ETH_CRDP0 ... MP_ETH_CRDP3:
695 s->rx_queue[(offset - MP_ETH_CRDP0)/4] =
696 s->cur_rx[(offset - MP_ETH_CRDP0)/4] = target2host_addr(value);
697 break;
699 case MP_ETH_CTDP0 ... MP_ETH_CTDP3:
700 s->tx_queue[(offset - MP_ETH_CTDP0)/4] = target2host_addr(value);
701 break;
705 static CPUReadMemoryFunc *mv88w8618_eth_readfn[] = {
706 mv88w8618_eth_read,
707 mv88w8618_eth_read,
708 mv88w8618_eth_read
711 static CPUWriteMemoryFunc *mv88w8618_eth_writefn[] = {
712 mv88w8618_eth_write,
713 mv88w8618_eth_write,
714 mv88w8618_eth_write
717 static void mv88w8618_eth_init(NICInfo *nd, uint32_t base, qemu_irq irq)
719 mv88w8618_eth_state *s;
720 int iomemtype;
722 qemu_check_nic_model(nd, "mv88w8618");
724 s = qemu_mallocz(sizeof(mv88w8618_eth_state));
725 s->irq = irq;
726 s->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
727 eth_receive, eth_can_receive, s);
728 iomemtype = cpu_register_io_memory(0, mv88w8618_eth_readfn,
729 mv88w8618_eth_writefn, s);
730 cpu_register_physical_memory(base, MP_ETH_SIZE, iomemtype);
733 /* LCD register offsets */
734 #define MP_LCD_IRQCTRL 0x180
735 #define MP_LCD_IRQSTAT 0x184
736 #define MP_LCD_SPICTRL 0x1ac
737 #define MP_LCD_INST 0x1bc
738 #define MP_LCD_DATA 0x1c0
740 /* Mode magics */
741 #define MP_LCD_SPI_DATA 0x00100011
742 #define MP_LCD_SPI_CMD 0x00104011
743 #define MP_LCD_SPI_INVALID 0x00000000
745 /* Commmands */
746 #define MP_LCD_INST_SETPAGE0 0xB0
747 /* ... */
748 #define MP_LCD_INST_SETPAGE7 0xB7
750 #define MP_LCD_TEXTCOLOR 0xe0e0ff /* RRGGBB */
752 typedef struct musicpal_lcd_state {
753 uint32_t mode;
754 uint32_t irqctrl;
755 int page;
756 int page_off;
757 DisplayState *ds;
758 uint8_t video_ram[128*64/8];
759 } musicpal_lcd_state;
761 static uint32_t lcd_brightness;
763 static uint8_t scale_lcd_color(uint8_t col)
765 int tmp = col;
767 switch (lcd_brightness) {
768 case 0x00000007: /* 0 */
769 return 0;
771 case 0x00020000: /* 1 */
772 return (tmp * 1) / 7;
774 case 0x00020001: /* 2 */
775 return (tmp * 2) / 7;
777 case 0x00040000: /* 3 */
778 return (tmp * 3) / 7;
780 case 0x00010006: /* 4 */
781 return (tmp * 4) / 7;
783 case 0x00020005: /* 5 */
784 return (tmp * 5) / 7;
786 case 0x00040003: /* 6 */
787 return (tmp * 6) / 7;
789 case 0x00030004: /* 7 */
790 default:
791 return col;
795 #define SET_LCD_PIXEL(depth, type) \
796 static inline void glue(set_lcd_pixel, depth) \
797 (musicpal_lcd_state *s, int x, int y, type col) \
799 int dx, dy; \
800 type *pixel = &((type *) ds_get_data(s->ds))[(y * 128 * 3 + x) * 3]; \
802 for (dy = 0; dy < 3; dy++, pixel += 127 * 3) \
803 for (dx = 0; dx < 3; dx++, pixel++) \
804 *pixel = col; \
806 SET_LCD_PIXEL(8, uint8_t)
807 SET_LCD_PIXEL(16, uint16_t)
808 SET_LCD_PIXEL(32, uint32_t)
810 #include "pixel_ops.h"
812 static void lcd_refresh(void *opaque)
814 musicpal_lcd_state *s = opaque;
815 int x, y, col;
817 switch (ds_get_bits_per_pixel(s->ds)) {
818 case 0:
819 return;
820 #define LCD_REFRESH(depth, func) \
821 case depth: \
822 col = func(scale_lcd_color((MP_LCD_TEXTCOLOR >> 16) & 0xff), \
823 scale_lcd_color((MP_LCD_TEXTCOLOR >> 8) & 0xff), \
824 scale_lcd_color(MP_LCD_TEXTCOLOR & 0xff)); \
825 for (x = 0; x < 128; x++) \
826 for (y = 0; y < 64; y++) \
827 if (s->video_ram[x + (y/8)*128] & (1 << (y % 8))) \
828 glue(set_lcd_pixel, depth)(s, x, y, col); \
829 else \
830 glue(set_lcd_pixel, depth)(s, x, y, 0); \
831 break;
832 LCD_REFRESH(8, rgb_to_pixel8)
833 LCD_REFRESH(16, rgb_to_pixel16)
834 LCD_REFRESH(32, (is_surface_bgr(s->ds->surface) ?
835 rgb_to_pixel32bgr : rgb_to_pixel32))
836 default:
837 cpu_abort(cpu_single_env, "unsupported colour depth %i\n",
838 ds_get_bits_per_pixel(s->ds));
841 dpy_update(s->ds, 0, 0, 128*3, 64*3);
844 static void lcd_invalidate(void *opaque)
848 static uint32_t musicpal_lcd_read(void *opaque, target_phys_addr_t offset)
850 musicpal_lcd_state *s = opaque;
852 switch (offset) {
853 case MP_LCD_IRQCTRL:
854 return s->irqctrl;
856 default:
857 return 0;
861 static void musicpal_lcd_write(void *opaque, target_phys_addr_t offset,
862 uint32_t value)
864 musicpal_lcd_state *s = opaque;
866 switch (offset) {
867 case MP_LCD_IRQCTRL:
868 s->irqctrl = value;
869 break;
871 case MP_LCD_SPICTRL:
872 if (value == MP_LCD_SPI_DATA || value == MP_LCD_SPI_CMD)
873 s->mode = value;
874 else
875 s->mode = MP_LCD_SPI_INVALID;
876 break;
878 case MP_LCD_INST:
879 if (value >= MP_LCD_INST_SETPAGE0 && value <= MP_LCD_INST_SETPAGE7) {
880 s->page = value - MP_LCD_INST_SETPAGE0;
881 s->page_off = 0;
883 break;
885 case MP_LCD_DATA:
886 if (s->mode == MP_LCD_SPI_CMD) {
887 if (value >= MP_LCD_INST_SETPAGE0 &&
888 value <= MP_LCD_INST_SETPAGE7) {
889 s->page = value - MP_LCD_INST_SETPAGE0;
890 s->page_off = 0;
892 } else if (s->mode == MP_LCD_SPI_DATA) {
893 s->video_ram[s->page*128 + s->page_off] = value;
894 s->page_off = (s->page_off + 1) & 127;
896 break;
900 static CPUReadMemoryFunc *musicpal_lcd_readfn[] = {
901 musicpal_lcd_read,
902 musicpal_lcd_read,
903 musicpal_lcd_read
906 static CPUWriteMemoryFunc *musicpal_lcd_writefn[] = {
907 musicpal_lcd_write,
908 musicpal_lcd_write,
909 musicpal_lcd_write
912 static void musicpal_lcd_init(void)
914 musicpal_lcd_state *s;
915 int iomemtype;
917 s = qemu_mallocz(sizeof(musicpal_lcd_state));
918 iomemtype = cpu_register_io_memory(0, musicpal_lcd_readfn,
919 musicpal_lcd_writefn, s);
920 cpu_register_physical_memory(MP_LCD_BASE, MP_LCD_SIZE, iomemtype);
922 s->ds = graphic_console_init(lcd_refresh, lcd_invalidate,
923 NULL, NULL, s);
924 qemu_console_resize(s->ds, 128*3, 64*3);
927 /* PIC register offsets */
928 #define MP_PIC_STATUS 0x00
929 #define MP_PIC_ENABLE_SET 0x08
930 #define MP_PIC_ENABLE_CLR 0x0C
932 typedef struct mv88w8618_pic_state
934 uint32_t level;
935 uint32_t enabled;
936 qemu_irq parent_irq;
937 } mv88w8618_pic_state;
939 static void mv88w8618_pic_update(mv88w8618_pic_state *s)
941 qemu_set_irq(s->parent_irq, (s->level & s->enabled));
944 static void mv88w8618_pic_set_irq(void *opaque, int irq, int level)
946 mv88w8618_pic_state *s = opaque;
948 if (level)
949 s->level |= 1 << irq;
950 else
951 s->level &= ~(1 << irq);
952 mv88w8618_pic_update(s);
955 static uint32_t mv88w8618_pic_read(void *opaque, target_phys_addr_t offset)
957 mv88w8618_pic_state *s = opaque;
959 switch (offset) {
960 case MP_PIC_STATUS:
961 return s->level & s->enabled;
963 default:
964 return 0;
968 static void mv88w8618_pic_write(void *opaque, target_phys_addr_t offset,
969 uint32_t value)
971 mv88w8618_pic_state *s = opaque;
973 switch (offset) {
974 case MP_PIC_ENABLE_SET:
975 s->enabled |= value;
976 break;
978 case MP_PIC_ENABLE_CLR:
979 s->enabled &= ~value;
980 s->level &= ~value;
981 break;
983 mv88w8618_pic_update(s);
986 static void mv88w8618_pic_reset(void *opaque)
988 mv88w8618_pic_state *s = opaque;
990 s->level = 0;
991 s->enabled = 0;
994 static CPUReadMemoryFunc *mv88w8618_pic_readfn[] = {
995 mv88w8618_pic_read,
996 mv88w8618_pic_read,
997 mv88w8618_pic_read
1000 static CPUWriteMemoryFunc *mv88w8618_pic_writefn[] = {
1001 mv88w8618_pic_write,
1002 mv88w8618_pic_write,
1003 mv88w8618_pic_write
1006 static qemu_irq *mv88w8618_pic_init(uint32_t base, qemu_irq parent_irq)
1008 mv88w8618_pic_state *s;
1009 int iomemtype;
1010 qemu_irq *qi;
1012 s = qemu_mallocz(sizeof(mv88w8618_pic_state));
1013 qi = qemu_allocate_irqs(mv88w8618_pic_set_irq, s, 32);
1014 s->parent_irq = parent_irq;
1015 iomemtype = cpu_register_io_memory(0, mv88w8618_pic_readfn,
1016 mv88w8618_pic_writefn, s);
1017 cpu_register_physical_memory(base, MP_PIC_SIZE, iomemtype);
1019 qemu_register_reset(mv88w8618_pic_reset, s);
1021 return qi;
1024 /* PIT register offsets */
1025 #define MP_PIT_TIMER1_LENGTH 0x00
1026 /* ... */
1027 #define MP_PIT_TIMER4_LENGTH 0x0C
1028 #define MP_PIT_CONTROL 0x10
1029 #define MP_PIT_TIMER1_VALUE 0x14
1030 /* ... */
1031 #define MP_PIT_TIMER4_VALUE 0x20
1032 #define MP_BOARD_RESET 0x34
1034 /* Magic board reset value (probably some watchdog behind it) */
1035 #define MP_BOARD_RESET_MAGIC 0x10000
1037 typedef struct mv88w8618_timer_state {
1038 ptimer_state *timer;
1039 uint32_t limit;
1040 int freq;
1041 qemu_irq irq;
1042 } mv88w8618_timer_state;
1044 typedef struct mv88w8618_pit_state {
1045 void *timer[4];
1046 uint32_t control;
1047 } mv88w8618_pit_state;
1049 static void mv88w8618_timer_tick(void *opaque)
1051 mv88w8618_timer_state *s = opaque;
1053 qemu_irq_raise(s->irq);
1056 static void *mv88w8618_timer_init(uint32_t freq, qemu_irq irq)
1058 mv88w8618_timer_state *s;
1059 QEMUBH *bh;
1061 s = qemu_mallocz(sizeof(mv88w8618_timer_state));
1062 s->irq = irq;
1063 s->freq = freq;
1065 bh = qemu_bh_new(mv88w8618_timer_tick, s);
1066 s->timer = ptimer_init(bh);
1068 return s;
1071 static uint32_t mv88w8618_pit_read(void *opaque, target_phys_addr_t offset)
1073 mv88w8618_pit_state *s = opaque;
1074 mv88w8618_timer_state *t;
1076 switch (offset) {
1077 case MP_PIT_TIMER1_VALUE ... MP_PIT_TIMER4_VALUE:
1078 t = s->timer[(offset-MP_PIT_TIMER1_VALUE) >> 2];
1079 return ptimer_get_count(t->timer);
1081 default:
1082 return 0;
1086 static void mv88w8618_pit_write(void *opaque, target_phys_addr_t offset,
1087 uint32_t value)
1089 mv88w8618_pit_state *s = opaque;
1090 mv88w8618_timer_state *t;
1091 int i;
1093 switch (offset) {
1094 case MP_PIT_TIMER1_LENGTH ... MP_PIT_TIMER4_LENGTH:
1095 t = s->timer[offset >> 2];
1096 t->limit = value;
1097 ptimer_set_limit(t->timer, t->limit, 1);
1098 break;
1100 case MP_PIT_CONTROL:
1101 for (i = 0; i < 4; i++) {
1102 if (value & 0xf) {
1103 t = s->timer[i];
1104 ptimer_set_limit(t->timer, t->limit, 0);
1105 ptimer_set_freq(t->timer, t->freq);
1106 ptimer_run(t->timer, 0);
1108 value >>= 4;
1110 break;
1112 case MP_BOARD_RESET:
1113 if (value == MP_BOARD_RESET_MAGIC)
1114 qemu_system_reset_request();
1115 break;
1119 static CPUReadMemoryFunc *mv88w8618_pit_readfn[] = {
1120 mv88w8618_pit_read,
1121 mv88w8618_pit_read,
1122 mv88w8618_pit_read
1125 static CPUWriteMemoryFunc *mv88w8618_pit_writefn[] = {
1126 mv88w8618_pit_write,
1127 mv88w8618_pit_write,
1128 mv88w8618_pit_write
1131 static void mv88w8618_pit_init(uint32_t base, qemu_irq *pic, int irq)
1133 int iomemtype;
1134 mv88w8618_pit_state *s;
1136 s = qemu_mallocz(sizeof(mv88w8618_pit_state));
1138 /* Letting them all run at 1 MHz is likely just a pragmatic
1139 * simplification. */
1140 s->timer[0] = mv88w8618_timer_init(1000000, pic[irq]);
1141 s->timer[1] = mv88w8618_timer_init(1000000, pic[irq + 1]);
1142 s->timer[2] = mv88w8618_timer_init(1000000, pic[irq + 2]);
1143 s->timer[3] = mv88w8618_timer_init(1000000, pic[irq + 3]);
1145 iomemtype = cpu_register_io_memory(0, mv88w8618_pit_readfn,
1146 mv88w8618_pit_writefn, s);
1147 cpu_register_physical_memory(base, MP_PIT_SIZE, iomemtype);
1150 /* Flash config register offsets */
1151 #define MP_FLASHCFG_CFGR0 0x04
1153 typedef struct mv88w8618_flashcfg_state {
1154 uint32_t cfgr0;
1155 } mv88w8618_flashcfg_state;
1157 static uint32_t mv88w8618_flashcfg_read(void *opaque,
1158 target_phys_addr_t offset)
1160 mv88w8618_flashcfg_state *s = opaque;
1162 switch (offset) {
1163 case MP_FLASHCFG_CFGR0:
1164 return s->cfgr0;
1166 default:
1167 return 0;
1171 static void mv88w8618_flashcfg_write(void *opaque, target_phys_addr_t offset,
1172 uint32_t value)
1174 mv88w8618_flashcfg_state *s = opaque;
1176 switch (offset) {
1177 case MP_FLASHCFG_CFGR0:
1178 s->cfgr0 = value;
1179 break;
1183 static CPUReadMemoryFunc *mv88w8618_flashcfg_readfn[] = {
1184 mv88w8618_flashcfg_read,
1185 mv88w8618_flashcfg_read,
1186 mv88w8618_flashcfg_read
1189 static CPUWriteMemoryFunc *mv88w8618_flashcfg_writefn[] = {
1190 mv88w8618_flashcfg_write,
1191 mv88w8618_flashcfg_write,
1192 mv88w8618_flashcfg_write
1195 static void mv88w8618_flashcfg_init(uint32_t base)
1197 int iomemtype;
1198 mv88w8618_flashcfg_state *s;
1200 s = qemu_mallocz(sizeof(mv88w8618_flashcfg_state));
1202 s->cfgr0 = 0xfffe4285; /* Default as set by U-Boot for 8 MB flash */
1203 iomemtype = cpu_register_io_memory(0, mv88w8618_flashcfg_readfn,
1204 mv88w8618_flashcfg_writefn, s);
1205 cpu_register_physical_memory(base, MP_FLASHCFG_SIZE, iomemtype);
1208 /* Misc register offsets */
1209 #define MP_MISC_BOARD_REVISION 0x18
1211 #define MP_BOARD_REVISION 0x31
1213 static uint32_t musicpal_misc_read(void *opaque, target_phys_addr_t offset)
1215 switch (offset) {
1216 case MP_MISC_BOARD_REVISION:
1217 return MP_BOARD_REVISION;
1219 default:
1220 return 0;
1224 static void musicpal_misc_write(void *opaque, target_phys_addr_t offset,
1225 uint32_t value)
1229 static CPUReadMemoryFunc *musicpal_misc_readfn[] = {
1230 musicpal_misc_read,
1231 musicpal_misc_read,
1232 musicpal_misc_read,
1235 static CPUWriteMemoryFunc *musicpal_misc_writefn[] = {
1236 musicpal_misc_write,
1237 musicpal_misc_write,
1238 musicpal_misc_write,
1241 static void musicpal_misc_init(void)
1243 int iomemtype;
1245 iomemtype = cpu_register_io_memory(0, musicpal_misc_readfn,
1246 musicpal_misc_writefn, NULL);
1247 cpu_register_physical_memory(MP_MISC_BASE, MP_MISC_SIZE, iomemtype);
1250 /* WLAN register offsets */
1251 #define MP_WLAN_MAGIC1 0x11c
1252 #define MP_WLAN_MAGIC2 0x124
1254 static uint32_t mv88w8618_wlan_read(void *opaque, target_phys_addr_t offset)
1256 switch (offset) {
1257 /* Workaround to allow loading the binary-only wlandrv.ko crap
1258 * from the original Freecom firmware. */
1259 case MP_WLAN_MAGIC1:
1260 return ~3;
1261 case MP_WLAN_MAGIC2:
1262 return -1;
1264 default:
1265 return 0;
1269 static void mv88w8618_wlan_write(void *opaque, target_phys_addr_t offset,
1270 uint32_t value)
1274 static CPUReadMemoryFunc *mv88w8618_wlan_readfn[] = {
1275 mv88w8618_wlan_read,
1276 mv88w8618_wlan_read,
1277 mv88w8618_wlan_read,
1280 static CPUWriteMemoryFunc *mv88w8618_wlan_writefn[] = {
1281 mv88w8618_wlan_write,
1282 mv88w8618_wlan_write,
1283 mv88w8618_wlan_write,
1286 static void mv88w8618_wlan_init(uint32_t base)
1288 int iomemtype;
1290 iomemtype = cpu_register_io_memory(0, mv88w8618_wlan_readfn,
1291 mv88w8618_wlan_writefn, NULL);
1292 cpu_register_physical_memory(base, MP_WLAN_SIZE, iomemtype);
1295 /* GPIO register offsets */
1296 #define MP_GPIO_OE_LO 0x008
1297 #define MP_GPIO_OUT_LO 0x00c
1298 #define MP_GPIO_IN_LO 0x010
1299 #define MP_GPIO_ISR_LO 0x020
1300 #define MP_GPIO_OE_HI 0x508
1301 #define MP_GPIO_OUT_HI 0x50c
1302 #define MP_GPIO_IN_HI 0x510
1303 #define MP_GPIO_ISR_HI 0x520
1305 /* GPIO bits & masks */
1306 #define MP_GPIO_WHEEL_VOL (1 << 8)
1307 #define MP_GPIO_WHEEL_VOL_INV (1 << 9)
1308 #define MP_GPIO_WHEEL_NAV (1 << 10)
1309 #define MP_GPIO_WHEEL_NAV_INV (1 << 11)
1310 #define MP_GPIO_LCD_BRIGHTNESS 0x00070000
1311 #define MP_GPIO_BTN_FAVORITS (1 << 19)
1312 #define MP_GPIO_BTN_MENU (1 << 20)
1313 #define MP_GPIO_BTN_VOLUME (1 << 21)
1314 #define MP_GPIO_BTN_NAVIGATION (1 << 22)
1315 #define MP_GPIO_I2C_DATA_BIT 29
1316 #define MP_GPIO_I2C_DATA (1 << MP_GPIO_I2C_DATA_BIT)
1317 #define MP_GPIO_I2C_CLOCK_BIT 30
1319 /* LCD brightness bits in GPIO_OE_HI */
1320 #define MP_OE_LCD_BRIGHTNESS 0x0007
1322 static uint32_t musicpal_gpio_read(void *opaque, target_phys_addr_t offset)
1324 switch (offset) {
1325 case MP_GPIO_OE_HI: /* used for LCD brightness control */
1326 return lcd_brightness & MP_OE_LCD_BRIGHTNESS;
1328 case MP_GPIO_OUT_LO:
1329 return gpio_out_state & 0xFFFF;
1330 case MP_GPIO_OUT_HI:
1331 return gpio_out_state >> 16;
1333 case MP_GPIO_IN_LO:
1334 return gpio_in_state & 0xFFFF;
1335 case MP_GPIO_IN_HI:
1336 /* Update received I2C data */
1337 gpio_in_state = (gpio_in_state & ~MP_GPIO_I2C_DATA) |
1338 (i2c_get_data(mixer_i2c) << MP_GPIO_I2C_DATA_BIT);
1339 return gpio_in_state >> 16;
1341 case MP_GPIO_ISR_LO:
1342 return gpio_isr & 0xFFFF;
1343 case MP_GPIO_ISR_HI:
1344 return gpio_isr >> 16;
1346 default:
1347 return 0;
1351 static void musicpal_gpio_write(void *opaque, target_phys_addr_t offset,
1352 uint32_t value)
1354 switch (offset) {
1355 case MP_GPIO_OE_HI: /* used for LCD brightness control */
1356 lcd_brightness = (lcd_brightness & MP_GPIO_LCD_BRIGHTNESS) |
1357 (value & MP_OE_LCD_BRIGHTNESS);
1358 break;
1360 case MP_GPIO_OUT_LO:
1361 gpio_out_state = (gpio_out_state & 0xFFFF0000) | (value & 0xFFFF);
1362 break;
1363 case MP_GPIO_OUT_HI:
1364 gpio_out_state = (gpio_out_state & 0xFFFF) | (value << 16);
1365 lcd_brightness = (lcd_brightness & 0xFFFF) |
1366 (gpio_out_state & MP_GPIO_LCD_BRIGHTNESS);
1367 i2c_state_update(mixer_i2c,
1368 (gpio_out_state >> MP_GPIO_I2C_DATA_BIT) & 1,
1369 (gpio_out_state >> MP_GPIO_I2C_CLOCK_BIT) & 1);
1370 break;
1375 static CPUReadMemoryFunc *musicpal_gpio_readfn[] = {
1376 musicpal_gpio_read,
1377 musicpal_gpio_read,
1378 musicpal_gpio_read,
1381 static CPUWriteMemoryFunc *musicpal_gpio_writefn[] = {
1382 musicpal_gpio_write,
1383 musicpal_gpio_write,
1384 musicpal_gpio_write,
1387 static void musicpal_gpio_init(void)
1389 int iomemtype;
1391 iomemtype = cpu_register_io_memory(0, musicpal_gpio_readfn,
1392 musicpal_gpio_writefn, NULL);
1393 cpu_register_physical_memory(MP_GPIO_BASE, MP_GPIO_SIZE, iomemtype);
1396 /* Keyboard codes & masks */
1397 #define KEY_RELEASED 0x80
1398 #define KEY_CODE 0x7f
1400 #define KEYCODE_TAB 0x0f
1401 #define KEYCODE_ENTER 0x1c
1402 #define KEYCODE_F 0x21
1403 #define KEYCODE_M 0x32
1405 #define KEYCODE_EXTENDED 0xe0
1406 #define KEYCODE_UP 0x48
1407 #define KEYCODE_DOWN 0x50
1408 #define KEYCODE_LEFT 0x4b
1409 #define KEYCODE_RIGHT 0x4d
1411 static void musicpal_key_event(void *opaque, int keycode)
1413 qemu_irq irq = opaque;
1414 uint32_t event = 0;
1415 static int kbd_extended;
1417 if (keycode == KEYCODE_EXTENDED) {
1418 kbd_extended = 1;
1419 return;
1422 if (kbd_extended)
1423 switch (keycode & KEY_CODE) {
1424 case KEYCODE_UP:
1425 event = MP_GPIO_WHEEL_NAV | MP_GPIO_WHEEL_NAV_INV;
1426 break;
1428 case KEYCODE_DOWN:
1429 event = MP_GPIO_WHEEL_NAV;
1430 break;
1432 case KEYCODE_LEFT:
1433 event = MP_GPIO_WHEEL_VOL | MP_GPIO_WHEEL_VOL_INV;
1434 break;
1436 case KEYCODE_RIGHT:
1437 event = MP_GPIO_WHEEL_VOL;
1438 break;
1440 else {
1441 switch (keycode & KEY_CODE) {
1442 case KEYCODE_F:
1443 event = MP_GPIO_BTN_FAVORITS;
1444 break;
1446 case KEYCODE_TAB:
1447 event = MP_GPIO_BTN_VOLUME;
1448 break;
1450 case KEYCODE_ENTER:
1451 event = MP_GPIO_BTN_NAVIGATION;
1452 break;
1454 case KEYCODE_M:
1455 event = MP_GPIO_BTN_MENU;
1456 break;
1458 /* Do not repeat already pressed buttons */
1459 if (!(keycode & KEY_RELEASED) && !(gpio_in_state & event))
1460 event = 0;
1463 if (event) {
1464 if (keycode & KEY_RELEASED) {
1465 gpio_in_state |= event;
1466 } else {
1467 gpio_in_state &= ~event;
1468 gpio_isr = event;
1469 qemu_irq_raise(irq);
1473 kbd_extended = 0;
1476 static struct arm_boot_info musicpal_binfo = {
1477 .loader_start = 0x0,
1478 .board_id = 0x20e,
1481 static void musicpal_init(ram_addr_t ram_size, int vga_ram_size,
1482 const char *boot_device,
1483 const char *kernel_filename, const char *kernel_cmdline,
1484 const char *initrd_filename, const char *cpu_model)
1486 CPUState *env;
1487 qemu_irq *pic;
1488 int index;
1489 unsigned long flash_size;
1491 if (!cpu_model)
1492 cpu_model = "arm926";
1494 env = cpu_init(cpu_model);
1495 if (!env) {
1496 fprintf(stderr, "Unable to find CPU definition\n");
1497 exit(1);
1499 pic = arm_pic_init_cpu(env);
1501 /* For now we use a fixed - the original - RAM size */
1502 cpu_register_physical_memory(0, MP_RAM_DEFAULT_SIZE,
1503 qemu_ram_alloc(MP_RAM_DEFAULT_SIZE));
1505 sram_off = qemu_ram_alloc(MP_SRAM_SIZE);
1506 cpu_register_physical_memory(MP_SRAM_BASE, MP_SRAM_SIZE, sram_off);
1508 pic = mv88w8618_pic_init(MP_PIC_BASE, pic[ARM_PIC_CPU_IRQ]);
1509 mv88w8618_pit_init(MP_PIT_BASE, pic, MP_TIMER1_IRQ);
1511 if (serial_hds[0])
1512 serial_mm_init(MP_UART1_BASE, 2, pic[MP_UART1_IRQ], 1825000,
1513 serial_hds[0], 1);
1514 if (serial_hds[1])
1515 serial_mm_init(MP_UART2_BASE, 2, pic[MP_UART2_IRQ], 1825000,
1516 serial_hds[1], 1);
1518 /* Register flash */
1519 index = drive_get_index(IF_PFLASH, 0, 0);
1520 if (index != -1) {
1521 flash_size = bdrv_getlength(drives_table[index].bdrv);
1522 if (flash_size != 8*1024*1024 && flash_size != 16*1024*1024 &&
1523 flash_size != 32*1024*1024) {
1524 fprintf(stderr, "Invalid flash image size\n");
1525 exit(1);
1529 * The original U-Boot accesses the flash at 0xFE000000 instead of
1530 * 0xFF800000 (if there is 8 MB flash). So remap flash access if the
1531 * image is smaller than 32 MB.
1533 pflash_cfi02_register(0-MP_FLASH_SIZE_MAX, qemu_ram_alloc(flash_size),
1534 drives_table[index].bdrv, 0x10000,
1535 (flash_size + 0xffff) >> 16,
1536 MP_FLASH_SIZE_MAX / flash_size,
1537 2, 0x00BF, 0x236D, 0x0000, 0x0000,
1538 0x5555, 0x2AAA);
1540 mv88w8618_flashcfg_init(MP_FLASHCFG_BASE);
1542 musicpal_lcd_init();
1544 qemu_add_kbd_event_handler(musicpal_key_event, pic[MP_GPIO_IRQ]);
1546 mv88w8618_eth_init(&nd_table[0], MP_ETH_BASE, pic[MP_ETH_IRQ]);
1548 mixer_i2c = musicpal_audio_init(pic[MP_AUDIO_IRQ]);
1550 mv88w8618_wlan_init(MP_WLAN_BASE);
1552 musicpal_misc_init();
1553 musicpal_gpio_init();
1555 musicpal_binfo.ram_size = MP_RAM_DEFAULT_SIZE;
1556 musicpal_binfo.kernel_filename = kernel_filename;
1557 musicpal_binfo.kernel_cmdline = kernel_cmdline;
1558 musicpal_binfo.initrd_filename = initrd_filename;
1559 arm_load_kernel(env, &musicpal_binfo);
1562 QEMUMachine musicpal_machine = {
1563 .name = "musicpal",
1564 .desc = "Marvell 88w8618 / MusicPal (ARM926EJ-S)",
1565 .init = musicpal_init,
1566 .ram_require = MP_RAM_DEFAULT_SIZE + MP_SRAM_SIZE +
1567 MP_FLASH_SIZE_MAX + RAMSIZE_FIXED,