blob: 02d4c70ba52990d93737e73a46977408cc3c53f3 [file] [log] [blame]
balrog24859b62008-04-24 19:21:53 +00001/*
2 * Marvell MV88W8618 / Freecom MusicPal emulation.
3 *
4 * Copyright (c) 2008 Jan Kiszka
5 *
6 * This code is licenced under the GNU GPL v2.
7 */
8
Paul Brookb47b50f2009-05-14 22:35:08 +01009#include "sysbus.h"
balrog24859b62008-04-24 19:21:53 +000010#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"
balrog24859b62008-04-24 19:21:53 +000020#include "i2c.h"
21
malc718ec0b2009-03-10 19:25:13 +000022#define MP_MISC_BASE 0x80002000
23#define MP_MISC_SIZE 0x00001000
24
balrog24859b62008-04-24 19:21:53 +000025#define MP_ETH_BASE 0x80008000
26#define MP_ETH_SIZE 0x00001000
27
malc718ec0b2009-03-10 19:25:13 +000028#define MP_WLAN_BASE 0x8000C000
29#define MP_WLAN_SIZE 0x00000800
30
balrog24859b62008-04-24 19:21:53 +000031#define MP_UART1_BASE 0x8000C840
32#define MP_UART2_BASE 0x8000C940
33
malc718ec0b2009-03-10 19:25:13 +000034#define MP_GPIO_BASE 0x8000D000
35#define MP_GPIO_SIZE 0x00001000
36
balrog24859b62008-04-24 19:21:53 +000037#define MP_FLASHCFG_BASE 0x90006000
38#define MP_FLASHCFG_SIZE 0x00001000
39
40#define MP_AUDIO_BASE 0x90007000
balrog24859b62008-04-24 19:21:53 +000041
42#define MP_PIC_BASE 0x90008000
43#define MP_PIC_SIZE 0x00001000
44
45#define MP_PIT_BASE 0x90009000
46#define MP_PIT_SIZE 0x00001000
47
48#define MP_LCD_BASE 0x9000c000
49#define MP_LCD_SIZE 0x00001000
50
51#define MP_SRAM_BASE 0xC0000000
52#define MP_SRAM_SIZE 0x00020000
53
54#define MP_RAM_DEFAULT_SIZE 32*1024*1024
55#define MP_FLASH_SIZE_MAX 32*1024*1024
56
57#define MP_TIMER1_IRQ 4
Paul Brookb47b50f2009-05-14 22:35:08 +010058#define MP_TIMER2_IRQ 5
59#define MP_TIMER3_IRQ 6
balrog24859b62008-04-24 19:21:53 +000060#define MP_TIMER4_IRQ 7
61#define MP_EHCI_IRQ 8
62#define MP_ETH_IRQ 9
63#define MP_UART1_IRQ 11
64#define MP_UART2_IRQ 11
65#define MP_GPIO_IRQ 12
66#define MP_RTC_IRQ 28
67#define MP_AUDIO_IRQ 30
68
balrog24859b62008-04-24 19:21:53 +000069/* Wolfson 8750 I2C address */
70#define MP_WM_ADDR 0x34
71
balrog24859b62008-04-24 19:21:53 +000072/* 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
90
91/* MII PHY access */
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)
96
97/* PHY registers */
98#define MP_ETH_PHY1_BMSR 0x00210000
99#define MP_ETH_PHY1_PHYSID1 0x00410000
100#define MP_ETH_PHY1_PHYSID2 0x00610000
101
102#define MP_PHY_BMSR_LINK 0x0004
103#define MP_PHY_BMSR_AUTONEG 0x0008
104
105#define MP_PHY_88E3015 0x01410E20
106
107/* TX descriptor status */
108#define MP_ETH_TX_OWN (1 << 31)
109
110/* RX descriptor status */
111#define MP_ETH_RX_OWN (1 << 31)
112
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
118
119/* Port config bits */
120#define MP_ETH_PCXR_2BSM_BIT 28 /* 2-byte incoming suffix */
121
122/* SDMA command bits */
123#define MP_ETH_CMD_TXHI (1 << 23)
124#define MP_ETH_CMD_TXLO (1 << 22)
125
126typedef struct mv88w8618_tx_desc {
127 uint32_t cmdstat;
128 uint16_t res;
129 uint16_t bytes;
130 uint32_t buffer;
131 uint32_t next;
132} mv88w8618_tx_desc;
133
134typedef struct mv88w8618_rx_desc {
135 uint32_t cmdstat;
136 uint16_t bytes;
137 uint16_t buffer_size;
138 uint32_t buffer;
139 uint32_t next;
140} mv88w8618_rx_desc;
141
142typedef struct mv88w8618_eth_state {
Paul Brookb47b50f2009-05-14 22:35:08 +0100143 SysBusDevice busdev;
balrog24859b62008-04-24 19:21:53 +0000144 qemu_irq irq;
145 uint32_t smir;
146 uint32_t icr;
147 uint32_t imr;
aliguorib946a152009-04-17 17:11:08 +0000148 int mmio_index;
Jan Kiszkad5b61dd2009-09-18 20:51:23 +0200149 uint32_t vlan_header;
pbrook930c8682009-04-10 01:24:26 +0000150 uint32_t tx_queue[2];
151 uint32_t rx_queue[4];
152 uint32_t frx_queue[4];
153 uint32_t cur_rx[4];
balrog24859b62008-04-24 19:21:53 +0000154 VLANClientState *vc;
155} mv88w8618_eth_state;
156
pbrook930c8682009-04-10 01:24:26 +0000157static void eth_rx_desc_put(uint32_t addr, mv88w8618_rx_desc *desc)
158{
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));
165}
166
167static void eth_rx_desc_get(uint32_t addr, mv88w8618_rx_desc *desc)
168{
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);
175}
176
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100177static int eth_can_receive(VLANClientState *vc)
balrog24859b62008-04-24 19:21:53 +0000178{
179 return 1;
180}
181
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100182static ssize_t eth_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
balrog24859b62008-04-24 19:21:53 +0000183{
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100184 mv88w8618_eth_state *s = vc->opaque;
pbrook930c8682009-04-10 01:24:26 +0000185 uint32_t desc_addr;
186 mv88w8618_rx_desc desc;
balrog24859b62008-04-24 19:21:53 +0000187 int i;
188
189 for (i = 0; i < 4; i++) {
pbrook930c8682009-04-10 01:24:26 +0000190 desc_addr = s->cur_rx[i];
Jan Kiszka49fedd02009-09-18 20:51:23 +0200191 if (!desc_addr) {
balrog24859b62008-04-24 19:21:53 +0000192 continue;
Jan Kiszka49fedd02009-09-18 20:51:23 +0200193 }
balrog24859b62008-04-24 19:21:53 +0000194 do {
pbrook930c8682009-04-10 01:24:26 +0000195 eth_rx_desc_get(desc_addr, &desc);
196 if ((desc.cmdstat & MP_ETH_RX_OWN) && desc.buffer_size >= size) {
197 cpu_physical_memory_write(desc.buffer + s->vlan_header,
198 buf, size);
199 desc.bytes = size + s->vlan_header;
200 desc.cmdstat &= ~MP_ETH_RX_OWN;
201 s->cur_rx[i] = desc.next;
balrog24859b62008-04-24 19:21:53 +0000202
203 s->icr |= MP_ETH_IRQ_RX;
Jan Kiszka49fedd02009-09-18 20:51:23 +0200204 if (s->icr & s->imr) {
balrog24859b62008-04-24 19:21:53 +0000205 qemu_irq_raise(s->irq);
Jan Kiszka49fedd02009-09-18 20:51:23 +0200206 }
pbrook930c8682009-04-10 01:24:26 +0000207 eth_rx_desc_put(desc_addr, &desc);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100208 return size;
balrog24859b62008-04-24 19:21:53 +0000209 }
pbrook930c8682009-04-10 01:24:26 +0000210 desc_addr = desc.next;
211 } while (desc_addr != s->rx_queue[i]);
balrog24859b62008-04-24 19:21:53 +0000212 }
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100213 return size;
balrog24859b62008-04-24 19:21:53 +0000214}
215
pbrook930c8682009-04-10 01:24:26 +0000216static void eth_tx_desc_put(uint32_t addr, mv88w8618_tx_desc *desc)
217{
218 cpu_to_le32s(&desc->cmdstat);
219 cpu_to_le16s(&desc->res);
220 cpu_to_le16s(&desc->bytes);
221 cpu_to_le32s(&desc->buffer);
222 cpu_to_le32s(&desc->next);
223 cpu_physical_memory_write(addr, (void *)desc, sizeof(*desc));
224}
225
226static void eth_tx_desc_get(uint32_t addr, mv88w8618_tx_desc *desc)
227{
228 cpu_physical_memory_read(addr, (void *)desc, sizeof(*desc));
229 le32_to_cpus(&desc->cmdstat);
230 le16_to_cpus(&desc->res);
231 le16_to_cpus(&desc->bytes);
232 le32_to_cpus(&desc->buffer);
233 le32_to_cpus(&desc->next);
234}
235
balrog24859b62008-04-24 19:21:53 +0000236static void eth_send(mv88w8618_eth_state *s, int queue_index)
237{
pbrook930c8682009-04-10 01:24:26 +0000238 uint32_t desc_addr = s->tx_queue[queue_index];
239 mv88w8618_tx_desc desc;
240 uint8_t buf[2048];
241 int len;
242
Jan Kiszka2e87c5b2009-09-18 20:51:22 +0200243 if (!desc_addr) {
244 return;
245 }
balrog24859b62008-04-24 19:21:53 +0000246 do {
pbrook930c8682009-04-10 01:24:26 +0000247 eth_tx_desc_get(desc_addr, &desc);
248 if (desc.cmdstat & MP_ETH_TX_OWN) {
249 len = desc.bytes;
250 if (len < 2048) {
251 cpu_physical_memory_read(desc.buffer, buf, len);
252 qemu_send_packet(s->vc, buf, len);
253 }
254 desc.cmdstat &= ~MP_ETH_TX_OWN;
balrog24859b62008-04-24 19:21:53 +0000255 s->icr |= 1 << (MP_ETH_IRQ_TXLO_BIT - queue_index);
pbrook930c8682009-04-10 01:24:26 +0000256 eth_tx_desc_put(desc_addr, &desc);
balrog24859b62008-04-24 19:21:53 +0000257 }
pbrook930c8682009-04-10 01:24:26 +0000258 desc_addr = desc.next;
259 } while (desc_addr != s->tx_queue[queue_index]);
balrog24859b62008-04-24 19:21:53 +0000260}
261
Anthony Liguoric227f092009-10-01 16:12:16 -0500262static uint32_t mv88w8618_eth_read(void *opaque, target_phys_addr_t offset)
balrog24859b62008-04-24 19:21:53 +0000263{
264 mv88w8618_eth_state *s = opaque;
265
balrog24859b62008-04-24 19:21:53 +0000266 switch (offset) {
267 case MP_ETH_SMIR:
268 if (s->smir & MP_ETH_SMIR_OPCODE) {
269 switch (s->smir & MP_ETH_SMIR_ADDR) {
270 case MP_ETH_PHY1_BMSR:
271 return MP_PHY_BMSR_LINK | MP_PHY_BMSR_AUTONEG |
272 MP_ETH_SMIR_RDVALID;
273 case MP_ETH_PHY1_PHYSID1:
274 return (MP_PHY_88E3015 >> 16) | MP_ETH_SMIR_RDVALID;
275 case MP_ETH_PHY1_PHYSID2:
276 return (MP_PHY_88E3015 & 0xFFFF) | MP_ETH_SMIR_RDVALID;
277 default:
278 return MP_ETH_SMIR_RDVALID;
279 }
280 }
281 return 0;
282
283 case MP_ETH_ICR:
284 return s->icr;
285
286 case MP_ETH_IMR:
287 return s->imr;
288
289 case MP_ETH_FRDP0 ... MP_ETH_FRDP3:
pbrook930c8682009-04-10 01:24:26 +0000290 return s->frx_queue[(offset - MP_ETH_FRDP0)/4];
balrog24859b62008-04-24 19:21:53 +0000291
292 case MP_ETH_CRDP0 ... MP_ETH_CRDP3:
pbrook930c8682009-04-10 01:24:26 +0000293 return s->rx_queue[(offset - MP_ETH_CRDP0)/4];
balrog24859b62008-04-24 19:21:53 +0000294
295 case MP_ETH_CTDP0 ... MP_ETH_CTDP3:
pbrook930c8682009-04-10 01:24:26 +0000296 return s->tx_queue[(offset - MP_ETH_CTDP0)/4];
balrog24859b62008-04-24 19:21:53 +0000297
298 default:
299 return 0;
300 }
301}
302
Anthony Liguoric227f092009-10-01 16:12:16 -0500303static void mv88w8618_eth_write(void *opaque, target_phys_addr_t offset,
balrog24859b62008-04-24 19:21:53 +0000304 uint32_t value)
305{
306 mv88w8618_eth_state *s = opaque;
307
balrog24859b62008-04-24 19:21:53 +0000308 switch (offset) {
309 case MP_ETH_SMIR:
310 s->smir = value;
311 break;
312
313 case MP_ETH_PCXR:
314 s->vlan_header = ((value >> MP_ETH_PCXR_2BSM_BIT) & 1) * 2;
315 break;
316
317 case MP_ETH_SDCMR:
Jan Kiszka49fedd02009-09-18 20:51:23 +0200318 if (value & MP_ETH_CMD_TXHI) {
balrog24859b62008-04-24 19:21:53 +0000319 eth_send(s, 1);
Jan Kiszka49fedd02009-09-18 20:51:23 +0200320 }
321 if (value & MP_ETH_CMD_TXLO) {
balrog24859b62008-04-24 19:21:53 +0000322 eth_send(s, 0);
Jan Kiszka49fedd02009-09-18 20:51:23 +0200323 }
324 if (value & (MP_ETH_CMD_TXHI | MP_ETH_CMD_TXLO) && s->icr & s->imr) {
balrog24859b62008-04-24 19:21:53 +0000325 qemu_irq_raise(s->irq);
Jan Kiszka49fedd02009-09-18 20:51:23 +0200326 }
balrog24859b62008-04-24 19:21:53 +0000327 break;
328
329 case MP_ETH_ICR:
330 s->icr &= value;
331 break;
332
333 case MP_ETH_IMR:
334 s->imr = value;
Jan Kiszka49fedd02009-09-18 20:51:23 +0200335 if (s->icr & s->imr) {
balrog24859b62008-04-24 19:21:53 +0000336 qemu_irq_raise(s->irq);
Jan Kiszka49fedd02009-09-18 20:51:23 +0200337 }
balrog24859b62008-04-24 19:21:53 +0000338 break;
339
340 case MP_ETH_FRDP0 ... MP_ETH_FRDP3:
pbrook930c8682009-04-10 01:24:26 +0000341 s->frx_queue[(offset - MP_ETH_FRDP0)/4] = value;
balrog24859b62008-04-24 19:21:53 +0000342 break;
343
344 case MP_ETH_CRDP0 ... MP_ETH_CRDP3:
345 s->rx_queue[(offset - MP_ETH_CRDP0)/4] =
pbrook930c8682009-04-10 01:24:26 +0000346 s->cur_rx[(offset - MP_ETH_CRDP0)/4] = value;
balrog24859b62008-04-24 19:21:53 +0000347 break;
348
349 case MP_ETH_CTDP0 ... MP_ETH_CTDP3:
pbrook930c8682009-04-10 01:24:26 +0000350 s->tx_queue[(offset - MP_ETH_CTDP0)/4] = value;
balrog24859b62008-04-24 19:21:53 +0000351 break;
352 }
353}
354
Blue Swirld60efc62009-08-25 18:29:31 +0000355static CPUReadMemoryFunc * const mv88w8618_eth_readfn[] = {
balrog24859b62008-04-24 19:21:53 +0000356 mv88w8618_eth_read,
357 mv88w8618_eth_read,
358 mv88w8618_eth_read
359};
360
Blue Swirld60efc62009-08-25 18:29:31 +0000361static CPUWriteMemoryFunc * const mv88w8618_eth_writefn[] = {
balrog24859b62008-04-24 19:21:53 +0000362 mv88w8618_eth_write,
363 mv88w8618_eth_write,
364 mv88w8618_eth_write
365};
366
aliguorib946a152009-04-17 17:11:08 +0000367static void eth_cleanup(VLANClientState *vc)
368{
369 mv88w8618_eth_state *s = vc->opaque;
370
371 cpu_unregister_io_memory(s->mmio_index);
372
373 qemu_free(s);
374}
375
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200376static int mv88w8618_eth_init(SysBusDevice *dev)
balrog24859b62008-04-24 19:21:53 +0000377{
Paul Brookb47b50f2009-05-14 22:35:08 +0100378 mv88w8618_eth_state *s = FROM_SYSBUS(mv88w8618_eth_state, dev);
balrog24859b62008-04-24 19:21:53 +0000379
Paul Brookb47b50f2009-05-14 22:35:08 +0100380 sysbus_init_irq(dev, &s->irq);
381 s->vc = qdev_get_vlan_client(&dev->qdev,
Mark McLoughlin463af532009-05-18 12:55:27 +0100382 eth_can_receive, eth_receive, NULL,
aliguorib946a152009-04-17 17:11:08 +0000383 eth_cleanup, s);
Avi Kivity1eed09c2009-06-14 11:38:51 +0300384 s->mmio_index = cpu_register_io_memory(mv88w8618_eth_readfn,
aliguorib946a152009-04-17 17:11:08 +0000385 mv88w8618_eth_writefn, s);
Paul Brookb47b50f2009-05-14 22:35:08 +0100386 sysbus_init_mmio(dev, MP_ETH_SIZE, s->mmio_index);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200387 return 0;
balrog24859b62008-04-24 19:21:53 +0000388}
389
Jan Kiszkad5b61dd2009-09-18 20:51:23 +0200390static const VMStateDescription mv88w8618_eth_vmsd = {
391 .name = "mv88w8618_eth",
392 .version_id = 1,
393 .minimum_version_id = 1,
394 .minimum_version_id_old = 1,
395 .fields = (VMStateField[]) {
396 VMSTATE_UINT32(smir, mv88w8618_eth_state),
397 VMSTATE_UINT32(icr, mv88w8618_eth_state),
398 VMSTATE_UINT32(imr, mv88w8618_eth_state),
399 VMSTATE_UINT32(vlan_header, mv88w8618_eth_state),
400 VMSTATE_UINT32_ARRAY(tx_queue, mv88w8618_eth_state, 2),
401 VMSTATE_UINT32_ARRAY(rx_queue, mv88w8618_eth_state, 4),
402 VMSTATE_UINT32_ARRAY(frx_queue, mv88w8618_eth_state, 4),
403 VMSTATE_UINT32_ARRAY(cur_rx, mv88w8618_eth_state, 4),
404 VMSTATE_END_OF_LIST()
405 }
406};
407
408static SysBusDeviceInfo mv88w8618_eth_info = {
409 .init = mv88w8618_eth_init,
410 .qdev.name = "mv88w8618_eth",
411 .qdev.size = sizeof(mv88w8618_eth_state),
412 .qdev.vmsd = &mv88w8618_eth_vmsd,
413};
414
balrog24859b62008-04-24 19:21:53 +0000415/* LCD register offsets */
416#define MP_LCD_IRQCTRL 0x180
417#define MP_LCD_IRQSTAT 0x184
418#define MP_LCD_SPICTRL 0x1ac
419#define MP_LCD_INST 0x1bc
420#define MP_LCD_DATA 0x1c0
421
422/* Mode magics */
423#define MP_LCD_SPI_DATA 0x00100011
424#define MP_LCD_SPI_CMD 0x00104011
425#define MP_LCD_SPI_INVALID 0x00000000
426
427/* Commmands */
428#define MP_LCD_INST_SETPAGE0 0xB0
429/* ... */
430#define MP_LCD_INST_SETPAGE7 0xB7
431
432#define MP_LCD_TEXTCOLOR 0xe0e0ff /* RRGGBB */
433
434typedef struct musicpal_lcd_state {
Paul Brookb47b50f2009-05-14 22:35:08 +0100435 SysBusDevice busdev;
Benoit Canet343ec8e2009-08-23 14:38:07 +0200436 uint32_t brightness;
balrog24859b62008-04-24 19:21:53 +0000437 uint32_t mode;
438 uint32_t irqctrl;
Jan Kiszkad5b61dd2009-09-18 20:51:23 +0200439 uint32_t page;
440 uint32_t page_off;
balrog24859b62008-04-24 19:21:53 +0000441 DisplayState *ds;
442 uint8_t video_ram[128*64/8];
443} musicpal_lcd_state;
444
Benoit Canet343ec8e2009-08-23 14:38:07 +0200445static uint8_t scale_lcd_color(musicpal_lcd_state *s, uint8_t col)
balrog24859b62008-04-24 19:21:53 +0000446{
Benoit Canet343ec8e2009-08-23 14:38:07 +0200447 switch (s->brightness) {
448 case 7:
balrog24859b62008-04-24 19:21:53 +0000449 return col;
Benoit Canet343ec8e2009-08-23 14:38:07 +0200450 case 0:
451 return 0;
452 default:
453 return (col * s->brightness) / 7;
balrog24859b62008-04-24 19:21:53 +0000454 }
455}
456
balrog0266f2c2008-04-25 00:59:43 +0000457#define SET_LCD_PIXEL(depth, type) \
458static inline void glue(set_lcd_pixel, depth) \
459 (musicpal_lcd_state *s, int x, int y, type col) \
460{ \
461 int dx, dy; \
aliguori0e1f5a02008-11-24 19:29:13 +0000462 type *pixel = &((type *) ds_get_data(s->ds))[(y * 128 * 3 + x) * 3]; \
balrog0266f2c2008-04-25 00:59:43 +0000463\
464 for (dy = 0; dy < 3; dy++, pixel += 127 * 3) \
465 for (dx = 0; dx < 3; dx++, pixel++) \
466 *pixel = col; \
balrog24859b62008-04-24 19:21:53 +0000467}
balrog0266f2c2008-04-25 00:59:43 +0000468SET_LCD_PIXEL(8, uint8_t)
469SET_LCD_PIXEL(16, uint16_t)
470SET_LCD_PIXEL(32, uint32_t)
471
472#include "pixel_ops.h"
balrog24859b62008-04-24 19:21:53 +0000473
474static void lcd_refresh(void *opaque)
475{
476 musicpal_lcd_state *s = opaque;
balrog0266f2c2008-04-25 00:59:43 +0000477 int x, y, col;
balrog24859b62008-04-24 19:21:53 +0000478
aliguori0e1f5a02008-11-24 19:29:13 +0000479 switch (ds_get_bits_per_pixel(s->ds)) {
balrog0266f2c2008-04-25 00:59:43 +0000480 case 0:
481 return;
482#define LCD_REFRESH(depth, func) \
483 case depth: \
Benoit Canet343ec8e2009-08-23 14:38:07 +0200484 col = func(scale_lcd_color(s, (MP_LCD_TEXTCOLOR >> 16) & 0xff), \
485 scale_lcd_color(s, (MP_LCD_TEXTCOLOR >> 8) & 0xff), \
486 scale_lcd_color(s, MP_LCD_TEXTCOLOR & 0xff)); \
Jan Kiszka49fedd02009-09-18 20:51:23 +0200487 for (x = 0; x < 128; x++) { \
488 for (y = 0; y < 64; y++) { \
489 if (s->video_ram[x + (y/8)*128] & (1 << (y % 8))) { \
balrog0266f2c2008-04-25 00:59:43 +0000490 glue(set_lcd_pixel, depth)(s, x, y, col); \
Jan Kiszka49fedd02009-09-18 20:51:23 +0200491 } else { \
balrog0266f2c2008-04-25 00:59:43 +0000492 glue(set_lcd_pixel, depth)(s, x, y, 0); \
Jan Kiszka49fedd02009-09-18 20:51:23 +0200493 } \
494 } \
495 } \
balrog0266f2c2008-04-25 00:59:43 +0000496 break;
497 LCD_REFRESH(8, rgb_to_pixel8)
498 LCD_REFRESH(16, rgb_to_pixel16)
aliguoribf9b48a2009-03-28 17:28:53 +0000499 LCD_REFRESH(32, (is_surface_bgr(s->ds->surface) ?
500 rgb_to_pixel32bgr : rgb_to_pixel32))
balrog0266f2c2008-04-25 00:59:43 +0000501 default:
Paul Brook2ac71172009-05-08 02:35:15 +0100502 hw_error("unsupported colour depth %i\n",
aliguori0e1f5a02008-11-24 19:29:13 +0000503 ds_get_bits_per_pixel(s->ds));
balrog0266f2c2008-04-25 00:59:43 +0000504 }
balrog24859b62008-04-24 19:21:53 +0000505
506 dpy_update(s->ds, 0, 0, 128*3, 64*3);
507}
508
balrog167bc3d2008-06-02 02:04:56 +0000509static void lcd_invalidate(void *opaque)
510{
balrog167bc3d2008-06-02 02:04:56 +0000511}
512
Benoit Canet343ec8e2009-08-23 14:38:07 +0200513static void musicpal_lcd_gpio_brigthness_in(void *opaque, int irq, int level)
514{
Jan Kiszka243cd132009-09-18 20:51:23 +0200515 musicpal_lcd_state *s = opaque;
Benoit Canet343ec8e2009-08-23 14:38:07 +0200516 s->brightness &= ~(1 << irq);
517 s->brightness |= level << irq;
518}
519
Anthony Liguoric227f092009-10-01 16:12:16 -0500520static uint32_t musicpal_lcd_read(void *opaque, target_phys_addr_t offset)
balrog24859b62008-04-24 19:21:53 +0000521{
522 musicpal_lcd_state *s = opaque;
523
balrog24859b62008-04-24 19:21:53 +0000524 switch (offset) {
525 case MP_LCD_IRQCTRL:
526 return s->irqctrl;
527
528 default:
529 return 0;
530 }
531}
532
Anthony Liguoric227f092009-10-01 16:12:16 -0500533static void musicpal_lcd_write(void *opaque, target_phys_addr_t offset,
balrog24859b62008-04-24 19:21:53 +0000534 uint32_t value)
535{
536 musicpal_lcd_state *s = opaque;
537
balrog24859b62008-04-24 19:21:53 +0000538 switch (offset) {
539 case MP_LCD_IRQCTRL:
540 s->irqctrl = value;
541 break;
542
543 case MP_LCD_SPICTRL:
Jan Kiszka49fedd02009-09-18 20:51:23 +0200544 if (value == MP_LCD_SPI_DATA || value == MP_LCD_SPI_CMD) {
balrog24859b62008-04-24 19:21:53 +0000545 s->mode = value;
Jan Kiszka49fedd02009-09-18 20:51:23 +0200546 } else {
balrog24859b62008-04-24 19:21:53 +0000547 s->mode = MP_LCD_SPI_INVALID;
Jan Kiszka49fedd02009-09-18 20:51:23 +0200548 }
balrog24859b62008-04-24 19:21:53 +0000549 break;
550
551 case MP_LCD_INST:
552 if (value >= MP_LCD_INST_SETPAGE0 && value <= MP_LCD_INST_SETPAGE7) {
553 s->page = value - MP_LCD_INST_SETPAGE0;
554 s->page_off = 0;
555 }
556 break;
557
558 case MP_LCD_DATA:
559 if (s->mode == MP_LCD_SPI_CMD) {
560 if (value >= MP_LCD_INST_SETPAGE0 &&
561 value <= MP_LCD_INST_SETPAGE7) {
562 s->page = value - MP_LCD_INST_SETPAGE0;
563 s->page_off = 0;
564 }
565 } else if (s->mode == MP_LCD_SPI_DATA) {
566 s->video_ram[s->page*128 + s->page_off] = value;
567 s->page_off = (s->page_off + 1) & 127;
568 }
569 break;
570 }
571}
572
Blue Swirld60efc62009-08-25 18:29:31 +0000573static CPUReadMemoryFunc * const musicpal_lcd_readfn[] = {
balrog24859b62008-04-24 19:21:53 +0000574 musicpal_lcd_read,
575 musicpal_lcd_read,
576 musicpal_lcd_read
577};
578
Blue Swirld60efc62009-08-25 18:29:31 +0000579static CPUWriteMemoryFunc * const musicpal_lcd_writefn[] = {
balrog24859b62008-04-24 19:21:53 +0000580 musicpal_lcd_write,
581 musicpal_lcd_write,
582 musicpal_lcd_write
583};
584
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200585static int musicpal_lcd_init(SysBusDevice *dev)
balrog24859b62008-04-24 19:21:53 +0000586{
Paul Brookb47b50f2009-05-14 22:35:08 +0100587 musicpal_lcd_state *s = FROM_SYSBUS(musicpal_lcd_state, dev);
balrog24859b62008-04-24 19:21:53 +0000588 int iomemtype;
589
Benoit Canet343ec8e2009-08-23 14:38:07 +0200590 s->brightness = 7;
591
Avi Kivity1eed09c2009-06-14 11:38:51 +0300592 iomemtype = cpu_register_io_memory(musicpal_lcd_readfn,
balrog24859b62008-04-24 19:21:53 +0000593 musicpal_lcd_writefn, s);
Paul Brookb47b50f2009-05-14 22:35:08 +0100594 sysbus_init_mmio(dev, MP_LCD_SIZE, iomemtype);
balrog24859b62008-04-24 19:21:53 +0000595
aliguori3023f332009-01-16 19:04:14 +0000596 s->ds = graphic_console_init(lcd_refresh, lcd_invalidate,
597 NULL, NULL, s);
598 qemu_console_resize(s->ds, 128*3, 64*3);
Benoit Canet343ec8e2009-08-23 14:38:07 +0200599
600 qdev_init_gpio_in(&dev->qdev, musicpal_lcd_gpio_brigthness_in, 3);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200601
602 return 0;
balrog24859b62008-04-24 19:21:53 +0000603}
604
Jan Kiszkad5b61dd2009-09-18 20:51:23 +0200605static const VMStateDescription musicpal_lcd_vmsd = {
606 .name = "musicpal_lcd",
607 .version_id = 1,
608 .minimum_version_id = 1,
609 .minimum_version_id_old = 1,
610 .fields = (VMStateField[]) {
611 VMSTATE_UINT32(brightness, musicpal_lcd_state),
612 VMSTATE_UINT32(mode, musicpal_lcd_state),
613 VMSTATE_UINT32(irqctrl, musicpal_lcd_state),
614 VMSTATE_UINT32(page, musicpal_lcd_state),
615 VMSTATE_UINT32(page_off, musicpal_lcd_state),
616 VMSTATE_BUFFER(video_ram, musicpal_lcd_state),
617 VMSTATE_END_OF_LIST()
618 }
619};
620
621static SysBusDeviceInfo musicpal_lcd_info = {
622 .init = musicpal_lcd_init,
623 .qdev.name = "musicpal_lcd",
624 .qdev.size = sizeof(musicpal_lcd_state),
625 .qdev.vmsd = &musicpal_lcd_vmsd,
626};
627
balrog24859b62008-04-24 19:21:53 +0000628/* PIC register offsets */
629#define MP_PIC_STATUS 0x00
630#define MP_PIC_ENABLE_SET 0x08
631#define MP_PIC_ENABLE_CLR 0x0C
632
633typedef struct mv88w8618_pic_state
634{
Paul Brookb47b50f2009-05-14 22:35:08 +0100635 SysBusDevice busdev;
balrog24859b62008-04-24 19:21:53 +0000636 uint32_t level;
637 uint32_t enabled;
638 qemu_irq parent_irq;
639} mv88w8618_pic_state;
640
641static void mv88w8618_pic_update(mv88w8618_pic_state *s)
642{
643 qemu_set_irq(s->parent_irq, (s->level & s->enabled));
644}
645
646static void mv88w8618_pic_set_irq(void *opaque, int irq, int level)
647{
648 mv88w8618_pic_state *s = opaque;
649
Jan Kiszka49fedd02009-09-18 20:51:23 +0200650 if (level) {
balrog24859b62008-04-24 19:21:53 +0000651 s->level |= 1 << irq;
Jan Kiszka49fedd02009-09-18 20:51:23 +0200652 } else {
balrog24859b62008-04-24 19:21:53 +0000653 s->level &= ~(1 << irq);
Jan Kiszka49fedd02009-09-18 20:51:23 +0200654 }
balrog24859b62008-04-24 19:21:53 +0000655 mv88w8618_pic_update(s);
656}
657
Anthony Liguoric227f092009-10-01 16:12:16 -0500658static uint32_t mv88w8618_pic_read(void *opaque, target_phys_addr_t offset)
balrog24859b62008-04-24 19:21:53 +0000659{
660 mv88w8618_pic_state *s = opaque;
661
balrog24859b62008-04-24 19:21:53 +0000662 switch (offset) {
663 case MP_PIC_STATUS:
664 return s->level & s->enabled;
665
666 default:
667 return 0;
668 }
669}
670
Anthony Liguoric227f092009-10-01 16:12:16 -0500671static void mv88w8618_pic_write(void *opaque, target_phys_addr_t offset,
balrog24859b62008-04-24 19:21:53 +0000672 uint32_t value)
673{
674 mv88w8618_pic_state *s = opaque;
675
balrog24859b62008-04-24 19:21:53 +0000676 switch (offset) {
677 case MP_PIC_ENABLE_SET:
678 s->enabled |= value;
679 break;
680
681 case MP_PIC_ENABLE_CLR:
682 s->enabled &= ~value;
683 s->level &= ~value;
684 break;
685 }
686 mv88w8618_pic_update(s);
687}
688
Jan Kiszkad5b61dd2009-09-18 20:51:23 +0200689static void mv88w8618_pic_reset(DeviceState *d)
balrog24859b62008-04-24 19:21:53 +0000690{
Jan Kiszkad5b61dd2009-09-18 20:51:23 +0200691 mv88w8618_pic_state *s = FROM_SYSBUS(mv88w8618_pic_state,
692 sysbus_from_qdev(d));
balrog24859b62008-04-24 19:21:53 +0000693
694 s->level = 0;
695 s->enabled = 0;
696}
697
Blue Swirld60efc62009-08-25 18:29:31 +0000698static CPUReadMemoryFunc * const mv88w8618_pic_readfn[] = {
balrog24859b62008-04-24 19:21:53 +0000699 mv88w8618_pic_read,
700 mv88w8618_pic_read,
701 mv88w8618_pic_read
702};
703
Blue Swirld60efc62009-08-25 18:29:31 +0000704static CPUWriteMemoryFunc * const mv88w8618_pic_writefn[] = {
balrog24859b62008-04-24 19:21:53 +0000705 mv88w8618_pic_write,
706 mv88w8618_pic_write,
707 mv88w8618_pic_write
708};
709
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200710static int mv88w8618_pic_init(SysBusDevice *dev)
balrog24859b62008-04-24 19:21:53 +0000711{
Paul Brookb47b50f2009-05-14 22:35:08 +0100712 mv88w8618_pic_state *s = FROM_SYSBUS(mv88w8618_pic_state, dev);
balrog24859b62008-04-24 19:21:53 +0000713 int iomemtype;
balrog24859b62008-04-24 19:21:53 +0000714
Paul Brook067a3dd2009-05-26 14:56:11 +0100715 qdev_init_gpio_in(&dev->qdev, mv88w8618_pic_set_irq, 32);
Paul Brookb47b50f2009-05-14 22:35:08 +0100716 sysbus_init_irq(dev, &s->parent_irq);
Avi Kivity1eed09c2009-06-14 11:38:51 +0300717 iomemtype = cpu_register_io_memory(mv88w8618_pic_readfn,
balrog24859b62008-04-24 19:21:53 +0000718 mv88w8618_pic_writefn, s);
Paul Brookb47b50f2009-05-14 22:35:08 +0100719 sysbus_init_mmio(dev, MP_PIC_SIZE, iomemtype);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200720 return 0;
balrog24859b62008-04-24 19:21:53 +0000721}
722
Jan Kiszkad5b61dd2009-09-18 20:51:23 +0200723static const VMStateDescription mv88w8618_pic_vmsd = {
724 .name = "mv88w8618_pic",
725 .version_id = 1,
726 .minimum_version_id = 1,
727 .minimum_version_id_old = 1,
728 .fields = (VMStateField[]) {
729 VMSTATE_UINT32(level, mv88w8618_pic_state),
730 VMSTATE_UINT32(enabled, mv88w8618_pic_state),
731 VMSTATE_END_OF_LIST()
732 }
733};
734
735static SysBusDeviceInfo mv88w8618_pic_info = {
736 .init = mv88w8618_pic_init,
737 .qdev.name = "mv88w8618_pic",
738 .qdev.size = sizeof(mv88w8618_pic_state),
739 .qdev.reset = mv88w8618_pic_reset,
740 .qdev.vmsd = &mv88w8618_pic_vmsd,
741};
742
balrog24859b62008-04-24 19:21:53 +0000743/* PIT register offsets */
744#define MP_PIT_TIMER1_LENGTH 0x00
745/* ... */
746#define MP_PIT_TIMER4_LENGTH 0x0C
747#define MP_PIT_CONTROL 0x10
748#define MP_PIT_TIMER1_VALUE 0x14
749/* ... */
750#define MP_PIT_TIMER4_VALUE 0x20
751#define MP_BOARD_RESET 0x34
752
753/* Magic board reset value (probably some watchdog behind it) */
754#define MP_BOARD_RESET_MAGIC 0x10000
755
756typedef struct mv88w8618_timer_state {
Paul Brookb47b50f2009-05-14 22:35:08 +0100757 ptimer_state *ptimer;
balrog24859b62008-04-24 19:21:53 +0000758 uint32_t limit;
759 int freq;
760 qemu_irq irq;
761} mv88w8618_timer_state;
762
763typedef struct mv88w8618_pit_state {
Paul Brookb47b50f2009-05-14 22:35:08 +0100764 SysBusDevice busdev;
765 mv88w8618_timer_state timer[4];
balrog24859b62008-04-24 19:21:53 +0000766} mv88w8618_pit_state;
767
768static void mv88w8618_timer_tick(void *opaque)
769{
770 mv88w8618_timer_state *s = opaque;
771
772 qemu_irq_raise(s->irq);
773}
774
Paul Brookb47b50f2009-05-14 22:35:08 +0100775static void mv88w8618_timer_init(SysBusDevice *dev, mv88w8618_timer_state *s,
776 uint32_t freq)
balrog24859b62008-04-24 19:21:53 +0000777{
balrog24859b62008-04-24 19:21:53 +0000778 QEMUBH *bh;
779
Paul Brookb47b50f2009-05-14 22:35:08 +0100780 sysbus_init_irq(dev, &s->irq);
balrog24859b62008-04-24 19:21:53 +0000781 s->freq = freq;
782
783 bh = qemu_bh_new(mv88w8618_timer_tick, s);
Paul Brookb47b50f2009-05-14 22:35:08 +0100784 s->ptimer = ptimer_init(bh);
balrog24859b62008-04-24 19:21:53 +0000785}
786
Anthony Liguoric227f092009-10-01 16:12:16 -0500787static uint32_t mv88w8618_pit_read(void *opaque, target_phys_addr_t offset)
balrog24859b62008-04-24 19:21:53 +0000788{
789 mv88w8618_pit_state *s = opaque;
790 mv88w8618_timer_state *t;
791
balrog24859b62008-04-24 19:21:53 +0000792 switch (offset) {
793 case MP_PIT_TIMER1_VALUE ... MP_PIT_TIMER4_VALUE:
Paul Brookb47b50f2009-05-14 22:35:08 +0100794 t = &s->timer[(offset-MP_PIT_TIMER1_VALUE) >> 2];
795 return ptimer_get_count(t->ptimer);
balrog24859b62008-04-24 19:21:53 +0000796
797 default:
798 return 0;
799 }
800}
801
Anthony Liguoric227f092009-10-01 16:12:16 -0500802static void mv88w8618_pit_write(void *opaque, target_phys_addr_t offset,
balrog24859b62008-04-24 19:21:53 +0000803 uint32_t value)
804{
805 mv88w8618_pit_state *s = opaque;
806 mv88w8618_timer_state *t;
807 int i;
808
balrog24859b62008-04-24 19:21:53 +0000809 switch (offset) {
810 case MP_PIT_TIMER1_LENGTH ... MP_PIT_TIMER4_LENGTH:
Paul Brookb47b50f2009-05-14 22:35:08 +0100811 t = &s->timer[offset >> 2];
balrog24859b62008-04-24 19:21:53 +0000812 t->limit = value;
Jan Kiszkac88d6bd2009-09-18 20:51:23 +0200813 if (t->limit > 0) {
814 ptimer_set_limit(t->ptimer, t->limit, 1);
815 } else {
816 ptimer_stop(t->ptimer);
817 }
balrog24859b62008-04-24 19:21:53 +0000818 break;
819
820 case MP_PIT_CONTROL:
821 for (i = 0; i < 4; i++) {
Jan Kiszkac88d6bd2009-09-18 20:51:23 +0200822 t = &s->timer[i];
823 if (value & 0xf && t->limit > 0) {
Paul Brookb47b50f2009-05-14 22:35:08 +0100824 ptimer_set_limit(t->ptimer, t->limit, 0);
825 ptimer_set_freq(t->ptimer, t->freq);
826 ptimer_run(t->ptimer, 0);
Jan Kiszkac88d6bd2009-09-18 20:51:23 +0200827 } else {
828 ptimer_stop(t->ptimer);
balrog24859b62008-04-24 19:21:53 +0000829 }
830 value >>= 4;
831 }
832 break;
833
834 case MP_BOARD_RESET:
Jan Kiszka49fedd02009-09-18 20:51:23 +0200835 if (value == MP_BOARD_RESET_MAGIC) {
balrog24859b62008-04-24 19:21:53 +0000836 qemu_system_reset_request();
Jan Kiszka49fedd02009-09-18 20:51:23 +0200837 }
balrog24859b62008-04-24 19:21:53 +0000838 break;
839 }
840}
841
Jan Kiszkad5b61dd2009-09-18 20:51:23 +0200842static void mv88w8618_pit_reset(DeviceState *d)
Jan Kiszkac88d6bd2009-09-18 20:51:23 +0200843{
Jan Kiszkad5b61dd2009-09-18 20:51:23 +0200844 mv88w8618_pit_state *s = FROM_SYSBUS(mv88w8618_pit_state,
845 sysbus_from_qdev(d));
Jan Kiszkac88d6bd2009-09-18 20:51:23 +0200846 int i;
847
848 for (i = 0; i < 4; i++) {
849 ptimer_stop(s->timer[i].ptimer);
850 s->timer[i].limit = 0;
851 }
852}
853
Blue Swirld60efc62009-08-25 18:29:31 +0000854static CPUReadMemoryFunc * const mv88w8618_pit_readfn[] = {
balrog24859b62008-04-24 19:21:53 +0000855 mv88w8618_pit_read,
856 mv88w8618_pit_read,
857 mv88w8618_pit_read
858};
859
Blue Swirld60efc62009-08-25 18:29:31 +0000860static CPUWriteMemoryFunc * const mv88w8618_pit_writefn[] = {
balrog24859b62008-04-24 19:21:53 +0000861 mv88w8618_pit_write,
862 mv88w8618_pit_write,
863 mv88w8618_pit_write
864};
865
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200866static int mv88w8618_pit_init(SysBusDevice *dev)
balrog24859b62008-04-24 19:21:53 +0000867{
868 int iomemtype;
Paul Brookb47b50f2009-05-14 22:35:08 +0100869 mv88w8618_pit_state *s = FROM_SYSBUS(mv88w8618_pit_state, dev);
870 int i;
balrog24859b62008-04-24 19:21:53 +0000871
balrog24859b62008-04-24 19:21:53 +0000872 /* Letting them all run at 1 MHz is likely just a pragmatic
873 * simplification. */
Paul Brookb47b50f2009-05-14 22:35:08 +0100874 for (i = 0; i < 4; i++) {
875 mv88w8618_timer_init(dev, &s->timer[i], 1000000);
876 }
balrog24859b62008-04-24 19:21:53 +0000877
Avi Kivity1eed09c2009-06-14 11:38:51 +0300878 iomemtype = cpu_register_io_memory(mv88w8618_pit_readfn,
balrog24859b62008-04-24 19:21:53 +0000879 mv88w8618_pit_writefn, s);
Paul Brookb47b50f2009-05-14 22:35:08 +0100880 sysbus_init_mmio(dev, MP_PIT_SIZE, iomemtype);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200881 return 0;
balrog24859b62008-04-24 19:21:53 +0000882}
883
Jan Kiszkad5b61dd2009-09-18 20:51:23 +0200884static const VMStateDescription mv88w8618_timer_vmsd = {
885 .name = "timer",
886 .version_id = 1,
887 .minimum_version_id = 1,
888 .minimum_version_id_old = 1,
889 .fields = (VMStateField[]) {
890 VMSTATE_PTIMER(ptimer, mv88w8618_timer_state),
891 VMSTATE_UINT32(limit, mv88w8618_timer_state),
892 VMSTATE_END_OF_LIST()
893 }
894};
895
896static const VMStateDescription mv88w8618_pit_vmsd = {
897 .name = "mv88w8618_pit",
898 .version_id = 1,
899 .minimum_version_id = 1,
900 .minimum_version_id_old = 1,
901 .fields = (VMStateField[]) {
902 VMSTATE_STRUCT_ARRAY(timer, mv88w8618_pit_state, 4, 1,
903 mv88w8618_timer_vmsd, mv88w8618_timer_state),
904 VMSTATE_END_OF_LIST()
905 }
906};
907
Jan Kiszkac88d6bd2009-09-18 20:51:23 +0200908static SysBusDeviceInfo mv88w8618_pit_info = {
909 .init = mv88w8618_pit_init,
910 .qdev.name = "mv88w8618_pit",
911 .qdev.size = sizeof(mv88w8618_pit_state),
912 .qdev.reset = mv88w8618_pit_reset,
Jan Kiszkad5b61dd2009-09-18 20:51:23 +0200913 .qdev.vmsd = &mv88w8618_pit_vmsd,
Jan Kiszkac88d6bd2009-09-18 20:51:23 +0200914};
915
balrog24859b62008-04-24 19:21:53 +0000916/* Flash config register offsets */
917#define MP_FLASHCFG_CFGR0 0x04
918
919typedef struct mv88w8618_flashcfg_state {
Paul Brookb47b50f2009-05-14 22:35:08 +0100920 SysBusDevice busdev;
balrog24859b62008-04-24 19:21:53 +0000921 uint32_t cfgr0;
922} mv88w8618_flashcfg_state;
923
924static uint32_t mv88w8618_flashcfg_read(void *opaque,
Anthony Liguoric227f092009-10-01 16:12:16 -0500925 target_phys_addr_t offset)
balrog24859b62008-04-24 19:21:53 +0000926{
927 mv88w8618_flashcfg_state *s = opaque;
928
balrog24859b62008-04-24 19:21:53 +0000929 switch (offset) {
930 case MP_FLASHCFG_CFGR0:
931 return s->cfgr0;
932
933 default:
934 return 0;
935 }
936}
937
Anthony Liguoric227f092009-10-01 16:12:16 -0500938static void mv88w8618_flashcfg_write(void *opaque, target_phys_addr_t offset,
balrog24859b62008-04-24 19:21:53 +0000939 uint32_t value)
940{
941 mv88w8618_flashcfg_state *s = opaque;
942
balrog24859b62008-04-24 19:21:53 +0000943 switch (offset) {
944 case MP_FLASHCFG_CFGR0:
945 s->cfgr0 = value;
946 break;
947 }
948}
949
Blue Swirld60efc62009-08-25 18:29:31 +0000950static CPUReadMemoryFunc * const mv88w8618_flashcfg_readfn[] = {
balrog24859b62008-04-24 19:21:53 +0000951 mv88w8618_flashcfg_read,
952 mv88w8618_flashcfg_read,
953 mv88w8618_flashcfg_read
954};
955
Blue Swirld60efc62009-08-25 18:29:31 +0000956static CPUWriteMemoryFunc * const mv88w8618_flashcfg_writefn[] = {
balrog24859b62008-04-24 19:21:53 +0000957 mv88w8618_flashcfg_write,
958 mv88w8618_flashcfg_write,
959 mv88w8618_flashcfg_write
960};
961
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200962static int mv88w8618_flashcfg_init(SysBusDevice *dev)
balrog24859b62008-04-24 19:21:53 +0000963{
964 int iomemtype;
Paul Brookb47b50f2009-05-14 22:35:08 +0100965 mv88w8618_flashcfg_state *s = FROM_SYSBUS(mv88w8618_flashcfg_state, dev);
balrog24859b62008-04-24 19:21:53 +0000966
balrog24859b62008-04-24 19:21:53 +0000967 s->cfgr0 = 0xfffe4285; /* Default as set by U-Boot for 8 MB flash */
Avi Kivity1eed09c2009-06-14 11:38:51 +0300968 iomemtype = cpu_register_io_memory(mv88w8618_flashcfg_readfn,
Jan Kiszka49fedd02009-09-18 20:51:23 +0200969 mv88w8618_flashcfg_writefn, s);
Paul Brookb47b50f2009-05-14 22:35:08 +0100970 sysbus_init_mmio(dev, MP_FLASHCFG_SIZE, iomemtype);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200971 return 0;
balrog24859b62008-04-24 19:21:53 +0000972}
973
Jan Kiszkad5b61dd2009-09-18 20:51:23 +0200974static const VMStateDescription mv88w8618_flashcfg_vmsd = {
975 .name = "mv88w8618_flashcfg",
976 .version_id = 1,
977 .minimum_version_id = 1,
978 .minimum_version_id_old = 1,
979 .fields = (VMStateField[]) {
980 VMSTATE_UINT32(cfgr0, mv88w8618_flashcfg_state),
981 VMSTATE_END_OF_LIST()
982 }
983};
984
985static SysBusDeviceInfo mv88w8618_flashcfg_info = {
986 .init = mv88w8618_flashcfg_init,
987 .qdev.name = "mv88w8618_flashcfg",
988 .qdev.size = sizeof(mv88w8618_flashcfg_state),
989 .qdev.vmsd = &mv88w8618_flashcfg_vmsd,
990};
991
malc718ec0b2009-03-10 19:25:13 +0000992/* Misc register offsets */
993#define MP_MISC_BOARD_REVISION 0x18
balrog24859b62008-04-24 19:21:53 +0000994
malc718ec0b2009-03-10 19:25:13 +0000995#define MP_BOARD_REVISION 0x31
balrog24859b62008-04-24 19:21:53 +0000996
Anthony Liguoric227f092009-10-01 16:12:16 -0500997static uint32_t musicpal_misc_read(void *opaque, target_phys_addr_t offset)
malc718ec0b2009-03-10 19:25:13 +0000998{
999 switch (offset) {
1000 case MP_MISC_BOARD_REVISION:
1001 return MP_BOARD_REVISION;
1002
1003 default:
1004 return 0;
1005 }
1006}
1007
Anthony Liguoric227f092009-10-01 16:12:16 -05001008static void musicpal_misc_write(void *opaque, target_phys_addr_t offset,
malc718ec0b2009-03-10 19:25:13 +00001009 uint32_t value)
1010{
1011}
1012
Blue Swirld60efc62009-08-25 18:29:31 +00001013static CPUReadMemoryFunc * const musicpal_misc_readfn[] = {
malc718ec0b2009-03-10 19:25:13 +00001014 musicpal_misc_read,
1015 musicpal_misc_read,
1016 musicpal_misc_read,
1017};
1018
Blue Swirld60efc62009-08-25 18:29:31 +00001019static CPUWriteMemoryFunc * const musicpal_misc_writefn[] = {
malc718ec0b2009-03-10 19:25:13 +00001020 musicpal_misc_write,
1021 musicpal_misc_write,
1022 musicpal_misc_write,
1023};
1024
1025static void musicpal_misc_init(void)
1026{
1027 int iomemtype;
1028
Avi Kivity1eed09c2009-06-14 11:38:51 +03001029 iomemtype = cpu_register_io_memory(musicpal_misc_readfn,
malc718ec0b2009-03-10 19:25:13 +00001030 musicpal_misc_writefn, NULL);
1031 cpu_register_physical_memory(MP_MISC_BASE, MP_MISC_SIZE, iomemtype);
1032}
1033
1034/* WLAN register offsets */
1035#define MP_WLAN_MAGIC1 0x11c
1036#define MP_WLAN_MAGIC2 0x124
1037
Anthony Liguoric227f092009-10-01 16:12:16 -05001038static uint32_t mv88w8618_wlan_read(void *opaque, target_phys_addr_t offset)
malc718ec0b2009-03-10 19:25:13 +00001039{
1040 switch (offset) {
1041 /* Workaround to allow loading the binary-only wlandrv.ko crap
1042 * from the original Freecom firmware. */
1043 case MP_WLAN_MAGIC1:
1044 return ~3;
1045 case MP_WLAN_MAGIC2:
1046 return -1;
1047
1048 default:
1049 return 0;
1050 }
1051}
1052
Anthony Liguoric227f092009-10-01 16:12:16 -05001053static void mv88w8618_wlan_write(void *opaque, target_phys_addr_t offset,
malc718ec0b2009-03-10 19:25:13 +00001054 uint32_t value)
1055{
1056}
1057
Blue Swirld60efc62009-08-25 18:29:31 +00001058static CPUReadMemoryFunc * const mv88w8618_wlan_readfn[] = {
malc718ec0b2009-03-10 19:25:13 +00001059 mv88w8618_wlan_read,
1060 mv88w8618_wlan_read,
1061 mv88w8618_wlan_read,
1062};
1063
Blue Swirld60efc62009-08-25 18:29:31 +00001064static CPUWriteMemoryFunc * const mv88w8618_wlan_writefn[] = {
malc718ec0b2009-03-10 19:25:13 +00001065 mv88w8618_wlan_write,
1066 mv88w8618_wlan_write,
1067 mv88w8618_wlan_write,
1068};
1069
Gerd Hoffmann81a322d2009-08-14 10:36:05 +02001070static int mv88w8618_wlan_init(SysBusDevice *dev)
malc718ec0b2009-03-10 19:25:13 +00001071{
1072 int iomemtype;
1073
Avi Kivity1eed09c2009-06-14 11:38:51 +03001074 iomemtype = cpu_register_io_memory(mv88w8618_wlan_readfn,
malc718ec0b2009-03-10 19:25:13 +00001075 mv88w8618_wlan_writefn, NULL);
Paul Brookb47b50f2009-05-14 22:35:08 +01001076 sysbus_init_mmio(dev, MP_WLAN_SIZE, iomemtype);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +02001077 return 0;
malc718ec0b2009-03-10 19:25:13 +00001078}
1079
1080/* GPIO register offsets */
1081#define MP_GPIO_OE_LO 0x008
1082#define MP_GPIO_OUT_LO 0x00c
1083#define MP_GPIO_IN_LO 0x010
Jan Kiszka708afdf2009-09-18 20:51:23 +02001084#define MP_GPIO_IER_LO 0x014
1085#define MP_GPIO_IMR_LO 0x018
malc718ec0b2009-03-10 19:25:13 +00001086#define MP_GPIO_ISR_LO 0x020
1087#define MP_GPIO_OE_HI 0x508
1088#define MP_GPIO_OUT_HI 0x50c
1089#define MP_GPIO_IN_HI 0x510
Jan Kiszka708afdf2009-09-18 20:51:23 +02001090#define MP_GPIO_IER_HI 0x514
1091#define MP_GPIO_IMR_HI 0x518
malc718ec0b2009-03-10 19:25:13 +00001092#define MP_GPIO_ISR_HI 0x520
balrog24859b62008-04-24 19:21:53 +00001093
1094/* GPIO bits & masks */
balrog24859b62008-04-24 19:21:53 +00001095#define MP_GPIO_LCD_BRIGHTNESS 0x00070000
balrog24859b62008-04-24 19:21:53 +00001096#define MP_GPIO_I2C_DATA_BIT 29
balrog24859b62008-04-24 19:21:53 +00001097#define MP_GPIO_I2C_CLOCK_BIT 30
1098
1099/* LCD brightness bits in GPIO_OE_HI */
1100#define MP_OE_LCD_BRIGHTNESS 0x0007
1101
Benoit Canet343ec8e2009-08-23 14:38:07 +02001102typedef struct musicpal_gpio_state {
1103 SysBusDevice busdev;
1104 uint32_t lcd_brightness;
1105 uint32_t out_state;
1106 uint32_t in_state;
Jan Kiszka708afdf2009-09-18 20:51:23 +02001107 uint32_t ier;
1108 uint32_t imr;
Benoit Canet343ec8e2009-08-23 14:38:07 +02001109 uint32_t isr;
Benoit Canet343ec8e2009-08-23 14:38:07 +02001110 qemu_irq irq;
Jan Kiszka708afdf2009-09-18 20:51:23 +02001111 qemu_irq out[5]; /* 3 brightness out + 2 lcd (data and clock ) */
Benoit Canet343ec8e2009-08-23 14:38:07 +02001112} musicpal_gpio_state;
1113
1114static void musicpal_gpio_brightness_update(musicpal_gpio_state *s) {
1115 int i;
1116 uint32_t brightness;
1117
1118 /* compute brightness ratio */
1119 switch (s->lcd_brightness) {
1120 case 0x00000007:
1121 brightness = 0;
1122 break;
1123
1124 case 0x00020000:
1125 brightness = 1;
1126 break;
1127
1128 case 0x00020001:
1129 brightness = 2;
1130 break;
1131
1132 case 0x00040000:
1133 brightness = 3;
1134 break;
1135
1136 case 0x00010006:
1137 brightness = 4;
1138 break;
1139
1140 case 0x00020005:
1141 brightness = 5;
1142 break;
1143
1144 case 0x00040003:
1145 brightness = 6;
1146 break;
1147
1148 case 0x00030004:
1149 default:
1150 brightness = 7;
1151 }
1152
1153 /* set lcd brightness GPIOs */
Jan Kiszka49fedd02009-09-18 20:51:23 +02001154 for (i = 0; i <= 2; i++) {
Benoit Canet343ec8e2009-08-23 14:38:07 +02001155 qemu_set_irq(s->out[i], (brightness >> i) & 1);
Jan Kiszka49fedd02009-09-18 20:51:23 +02001156 }
Benoit Canet343ec8e2009-08-23 14:38:07 +02001157}
1158
Jan Kiszka708afdf2009-09-18 20:51:23 +02001159static void musicpal_gpio_pin_event(void *opaque, int pin, int level)
Benoit Canet343ec8e2009-08-23 14:38:07 +02001160{
Jan Kiszka243cd132009-09-18 20:51:23 +02001161 musicpal_gpio_state *s = opaque;
Jan Kiszka708afdf2009-09-18 20:51:23 +02001162 uint32_t mask = 1 << pin;
1163 uint32_t delta = level << pin;
1164 uint32_t old = s->in_state & mask;
Benoit Canet343ec8e2009-08-23 14:38:07 +02001165
Jan Kiszka708afdf2009-09-18 20:51:23 +02001166 s->in_state &= ~mask;
1167 s->in_state |= delta;
1168
1169 if ((old ^ delta) &&
1170 ((level && (s->imr & mask)) || (!level && (s->ier & mask)))) {
1171 s->isr = mask;
1172 qemu_irq_raise(s->irq);
Andrzej Zaborowskid0747692009-08-23 15:51:14 +02001173 }
Benoit Canet343ec8e2009-08-23 14:38:07 +02001174}
1175
Anthony Liguoric227f092009-10-01 16:12:16 -05001176static uint32_t musicpal_gpio_read(void *opaque, target_phys_addr_t offset)
balrog24859b62008-04-24 19:21:53 +00001177{
Jan Kiszka243cd132009-09-18 20:51:23 +02001178 musicpal_gpio_state *s = opaque;
Benoit Canet343ec8e2009-08-23 14:38:07 +02001179
balrog24859b62008-04-24 19:21:53 +00001180 switch (offset) {
balrog24859b62008-04-24 19:21:53 +00001181 case MP_GPIO_OE_HI: /* used for LCD brightness control */
Benoit Canet343ec8e2009-08-23 14:38:07 +02001182 return s->lcd_brightness & MP_OE_LCD_BRIGHTNESS;
balrog24859b62008-04-24 19:21:53 +00001183
1184 case MP_GPIO_OUT_LO:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001185 return s->out_state & 0xFFFF;
balrog24859b62008-04-24 19:21:53 +00001186 case MP_GPIO_OUT_HI:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001187 return s->out_state >> 16;
balrog24859b62008-04-24 19:21:53 +00001188
1189 case MP_GPIO_IN_LO:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001190 return s->in_state & 0xFFFF;
balrog24859b62008-04-24 19:21:53 +00001191 case MP_GPIO_IN_HI:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001192 return s->in_state >> 16;
balrog24859b62008-04-24 19:21:53 +00001193
Jan Kiszka708afdf2009-09-18 20:51:23 +02001194 case MP_GPIO_IER_LO:
1195 return s->ier & 0xFFFF;
1196 case MP_GPIO_IER_HI:
1197 return s->ier >> 16;
1198
1199 case MP_GPIO_IMR_LO:
1200 return s->imr & 0xFFFF;
1201 case MP_GPIO_IMR_HI:
1202 return s->imr >> 16;
1203
balrog24859b62008-04-24 19:21:53 +00001204 case MP_GPIO_ISR_LO:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001205 return s->isr & 0xFFFF;
balrog24859b62008-04-24 19:21:53 +00001206 case MP_GPIO_ISR_HI:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001207 return s->isr >> 16;
balrog24859b62008-04-24 19:21:53 +00001208
balrog24859b62008-04-24 19:21:53 +00001209 default:
1210 return 0;
1211 }
1212}
1213
Anthony Liguoric227f092009-10-01 16:12:16 -05001214static void musicpal_gpio_write(void *opaque, target_phys_addr_t offset,
malc718ec0b2009-03-10 19:25:13 +00001215 uint32_t value)
balrog24859b62008-04-24 19:21:53 +00001216{
Jan Kiszka243cd132009-09-18 20:51:23 +02001217 musicpal_gpio_state *s = opaque;
balrog24859b62008-04-24 19:21:53 +00001218 switch (offset) {
1219 case MP_GPIO_OE_HI: /* used for LCD brightness control */
Benoit Canet343ec8e2009-08-23 14:38:07 +02001220 s->lcd_brightness = (s->lcd_brightness & MP_GPIO_LCD_BRIGHTNESS) |
balrog24859b62008-04-24 19:21:53 +00001221 (value & MP_OE_LCD_BRIGHTNESS);
Benoit Canet343ec8e2009-08-23 14:38:07 +02001222 musicpal_gpio_brightness_update(s);
balrog24859b62008-04-24 19:21:53 +00001223 break;
1224
1225 case MP_GPIO_OUT_LO:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001226 s->out_state = (s->out_state & 0xFFFF0000) | (value & 0xFFFF);
balrog24859b62008-04-24 19:21:53 +00001227 break;
1228 case MP_GPIO_OUT_HI:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001229 s->out_state = (s->out_state & 0xFFFF) | (value << 16);
1230 s->lcd_brightness = (s->lcd_brightness & 0xFFFF) |
1231 (s->out_state & MP_GPIO_LCD_BRIGHTNESS);
1232 musicpal_gpio_brightness_update(s);
Andrzej Zaborowskid0747692009-08-23 15:51:14 +02001233 qemu_set_irq(s->out[3], (s->out_state >> MP_GPIO_I2C_DATA_BIT) & 1);
1234 qemu_set_irq(s->out[4], (s->out_state >> MP_GPIO_I2C_CLOCK_BIT) & 1);
balrog24859b62008-04-24 19:21:53 +00001235 break;
1236
Jan Kiszka708afdf2009-09-18 20:51:23 +02001237 case MP_GPIO_IER_LO:
1238 s->ier = (s->ier & 0xFFFF0000) | (value & 0xFFFF);
1239 break;
1240 case MP_GPIO_IER_HI:
1241 s->ier = (s->ier & 0xFFFF) | (value << 16);
1242 break;
1243
1244 case MP_GPIO_IMR_LO:
1245 s->imr = (s->imr & 0xFFFF0000) | (value & 0xFFFF);
1246 break;
1247 case MP_GPIO_IMR_HI:
1248 s->imr = (s->imr & 0xFFFF) | (value << 16);
1249 break;
balrog24859b62008-04-24 19:21:53 +00001250 }
1251}
1252
Blue Swirld60efc62009-08-25 18:29:31 +00001253static CPUReadMemoryFunc * const musicpal_gpio_readfn[] = {
malc718ec0b2009-03-10 19:25:13 +00001254 musicpal_gpio_read,
1255 musicpal_gpio_read,
1256 musicpal_gpio_read,
1257};
1258
Blue Swirld60efc62009-08-25 18:29:31 +00001259static CPUWriteMemoryFunc * const musicpal_gpio_writefn[] = {
malc718ec0b2009-03-10 19:25:13 +00001260 musicpal_gpio_write,
1261 musicpal_gpio_write,
1262 musicpal_gpio_write,
1263};
1264
Jan Kiszkad5b61dd2009-09-18 20:51:23 +02001265static void musicpal_gpio_reset(DeviceState *d)
malc718ec0b2009-03-10 19:25:13 +00001266{
Jan Kiszkad5b61dd2009-09-18 20:51:23 +02001267 musicpal_gpio_state *s = FROM_SYSBUS(musicpal_gpio_state,
1268 sysbus_from_qdev(d));
Jan Kiszka30624c92009-09-18 20:51:23 +02001269
1270 s->lcd_brightness = 0;
1271 s->out_state = 0;
Benoit Canet343ec8e2009-08-23 14:38:07 +02001272 s->in_state = 0xffffffff;
Jan Kiszka708afdf2009-09-18 20:51:23 +02001273 s->ier = 0;
1274 s->imr = 0;
Benoit Canet343ec8e2009-08-23 14:38:07 +02001275 s->isr = 0;
1276}
1277
Gerd Hoffmann81a322d2009-08-14 10:36:05 +02001278static int musicpal_gpio_init(SysBusDevice *dev)
Benoit Canet343ec8e2009-08-23 14:38:07 +02001279{
1280 musicpal_gpio_state *s = FROM_SYSBUS(musicpal_gpio_state, dev);
malc718ec0b2009-03-10 19:25:13 +00001281 int iomemtype;
1282
Benoit Canet343ec8e2009-08-23 14:38:07 +02001283 sysbus_init_irq(dev, &s->irq);
1284
Avi Kivity1eed09c2009-06-14 11:38:51 +03001285 iomemtype = cpu_register_io_memory(musicpal_gpio_readfn,
Benoit Canet343ec8e2009-08-23 14:38:07 +02001286 musicpal_gpio_writefn, s);
1287 sysbus_init_mmio(dev, MP_GPIO_SIZE, iomemtype);
1288
Jan Kiszkad5b61dd2009-09-18 20:51:23 +02001289 musicpal_gpio_reset(&dev->qdev);
Benoit Canet343ec8e2009-08-23 14:38:07 +02001290
Jan Kiszka708afdf2009-09-18 20:51:23 +02001291 qdev_init_gpio_out(&dev->qdev, s->out, ARRAY_SIZE(s->out));
1292
1293 qdev_init_gpio_in(&dev->qdev, musicpal_gpio_pin_event, 32);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +02001294
1295 return 0;
malc718ec0b2009-03-10 19:25:13 +00001296}
1297
Jan Kiszkad5b61dd2009-09-18 20:51:23 +02001298static const VMStateDescription musicpal_gpio_vmsd = {
1299 .name = "musicpal_gpio",
1300 .version_id = 1,
1301 .minimum_version_id = 1,
1302 .minimum_version_id_old = 1,
1303 .fields = (VMStateField[]) {
1304 VMSTATE_UINT32(lcd_brightness, musicpal_gpio_state),
1305 VMSTATE_UINT32(out_state, musicpal_gpio_state),
1306 VMSTATE_UINT32(in_state, musicpal_gpio_state),
1307 VMSTATE_UINT32(ier, musicpal_gpio_state),
1308 VMSTATE_UINT32(imr, musicpal_gpio_state),
1309 VMSTATE_UINT32(isr, musicpal_gpio_state),
1310 VMSTATE_END_OF_LIST()
1311 }
1312};
1313
Jan Kiszka30624c92009-09-18 20:51:23 +02001314static SysBusDeviceInfo musicpal_gpio_info = {
1315 .init = musicpal_gpio_init,
1316 .qdev.name = "musicpal_gpio",
1317 .qdev.size = sizeof(musicpal_gpio_state),
1318 .qdev.reset = musicpal_gpio_reset,
Jan Kiszkad5b61dd2009-09-18 20:51:23 +02001319 .qdev.vmsd = &musicpal_gpio_vmsd,
Jan Kiszka30624c92009-09-18 20:51:23 +02001320};
1321
balrog24859b62008-04-24 19:21:53 +00001322/* Keyboard codes & masks */
balrog7c6ce4b2008-05-17 18:18:04 +00001323#define KEY_RELEASED 0x80
balrog24859b62008-04-24 19:21:53 +00001324#define KEY_CODE 0x7f
1325
1326#define KEYCODE_TAB 0x0f
1327#define KEYCODE_ENTER 0x1c
1328#define KEYCODE_F 0x21
1329#define KEYCODE_M 0x32
1330
1331#define KEYCODE_EXTENDED 0xe0
1332#define KEYCODE_UP 0x48
1333#define KEYCODE_DOWN 0x50
1334#define KEYCODE_LEFT 0x4b
1335#define KEYCODE_RIGHT 0x4d
1336
Jan Kiszka708afdf2009-09-18 20:51:23 +02001337#define MP_KEY_WHEEL_VOL (1 << 0)
Benoit Canet343ec8e2009-08-23 14:38:07 +02001338#define MP_KEY_WHEEL_VOL_INV (1 << 1)
1339#define MP_KEY_WHEEL_NAV (1 << 2)
1340#define MP_KEY_WHEEL_NAV_INV (1 << 3)
1341#define MP_KEY_BTN_FAVORITS (1 << 4)
1342#define MP_KEY_BTN_MENU (1 << 5)
1343#define MP_KEY_BTN_VOLUME (1 << 6)
1344#define MP_KEY_BTN_NAVIGATION (1 << 7)
1345
1346typedef struct musicpal_key_state {
1347 SysBusDevice busdev;
1348 uint32_t kbd_extended;
Jan Kiszka708afdf2009-09-18 20:51:23 +02001349 uint32_t pressed_keys;
1350 qemu_irq out[8];
Benoit Canet343ec8e2009-08-23 14:38:07 +02001351} musicpal_key_state;
1352
balrog24859b62008-04-24 19:21:53 +00001353static void musicpal_key_event(void *opaque, int keycode)
1354{
Jan Kiszka243cd132009-09-18 20:51:23 +02001355 musicpal_key_state *s = opaque;
balrog24859b62008-04-24 19:21:53 +00001356 uint32_t event = 0;
Benoit Canet343ec8e2009-08-23 14:38:07 +02001357 int i;
balrog24859b62008-04-24 19:21:53 +00001358
1359 if (keycode == KEYCODE_EXTENDED) {
Benoit Canet343ec8e2009-08-23 14:38:07 +02001360 s->kbd_extended = 1;
balrog24859b62008-04-24 19:21:53 +00001361 return;
1362 }
1363
Jan Kiszka49fedd02009-09-18 20:51:23 +02001364 if (s->kbd_extended) {
balrog24859b62008-04-24 19:21:53 +00001365 switch (keycode & KEY_CODE) {
1366 case KEYCODE_UP:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001367 event = MP_KEY_WHEEL_NAV | MP_KEY_WHEEL_NAV_INV;
balrog24859b62008-04-24 19:21:53 +00001368 break;
1369
1370 case KEYCODE_DOWN:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001371 event = MP_KEY_WHEEL_NAV;
balrog24859b62008-04-24 19:21:53 +00001372 break;
1373
1374 case KEYCODE_LEFT:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001375 event = MP_KEY_WHEEL_VOL | MP_KEY_WHEEL_VOL_INV;
balrog24859b62008-04-24 19:21:53 +00001376 break;
1377
1378 case KEYCODE_RIGHT:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001379 event = MP_KEY_WHEEL_VOL;
balrog24859b62008-04-24 19:21:53 +00001380 break;
1381 }
Jan Kiszka49fedd02009-09-18 20:51:23 +02001382 } else {
balrog24859b62008-04-24 19:21:53 +00001383 switch (keycode & KEY_CODE) {
1384 case KEYCODE_F:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001385 event = MP_KEY_BTN_FAVORITS;
balrog24859b62008-04-24 19:21:53 +00001386 break;
1387
1388 case KEYCODE_TAB:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001389 event = MP_KEY_BTN_VOLUME;
balrog24859b62008-04-24 19:21:53 +00001390 break;
1391
1392 case KEYCODE_ENTER:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001393 event = MP_KEY_BTN_NAVIGATION;
balrog24859b62008-04-24 19:21:53 +00001394 break;
1395
1396 case KEYCODE_M:
Benoit Canet343ec8e2009-08-23 14:38:07 +02001397 event = MP_KEY_BTN_MENU;
balrog24859b62008-04-24 19:21:53 +00001398 break;
1399 }
balrog7c6ce4b2008-05-17 18:18:04 +00001400 /* Do not repeat already pressed buttons */
Jan Kiszka708afdf2009-09-18 20:51:23 +02001401 if (!(keycode & KEY_RELEASED) && (s->pressed_keys & event)) {
balrog7c6ce4b2008-05-17 18:18:04 +00001402 event = 0;
Jan Kiszka708afdf2009-09-18 20:51:23 +02001403 }
balrog7c6ce4b2008-05-17 18:18:04 +00001404 }
balrog24859b62008-04-24 19:21:53 +00001405
balrog7c6ce4b2008-05-17 18:18:04 +00001406 if (event) {
Jan Kiszka708afdf2009-09-18 20:51:23 +02001407 /* Raise GPIO pin first if repeating a key */
1408 if (!(keycode & KEY_RELEASED) && (s->pressed_keys & event)) {
1409 for (i = 0; i <= 7; i++) {
1410 if (event & (1 << i)) {
1411 qemu_set_irq(s->out[i], 1);
1412 }
1413 }
balrog7c6ce4b2008-05-17 18:18:04 +00001414 }
Jan Kiszka708afdf2009-09-18 20:51:23 +02001415 for (i = 0; i <= 7; i++) {
1416 if (event & (1 << i)) {
1417 qemu_set_irq(s->out[i], !!(keycode & KEY_RELEASED));
1418 }
1419 }
1420 if (keycode & KEY_RELEASED) {
1421 s->pressed_keys &= ~event;
1422 } else {
1423 s->pressed_keys |= event;
1424 }
balrog24859b62008-04-24 19:21:53 +00001425 }
1426
Benoit Canet343ec8e2009-08-23 14:38:07 +02001427 s->kbd_extended = 0;
1428}
1429
Gerd Hoffmann81a322d2009-08-14 10:36:05 +02001430static int musicpal_key_init(SysBusDevice *dev)
Benoit Canet343ec8e2009-08-23 14:38:07 +02001431{
1432 musicpal_key_state *s = FROM_SYSBUS(musicpal_key_state, dev);
1433
1434 sysbus_init_mmio(dev, 0x0, 0);
1435
1436 s->kbd_extended = 0;
Jan Kiszka708afdf2009-09-18 20:51:23 +02001437 s->pressed_keys = 0;
Benoit Canet343ec8e2009-08-23 14:38:07 +02001438
Jan Kiszka708afdf2009-09-18 20:51:23 +02001439 qdev_init_gpio_out(&dev->qdev, s->out, ARRAY_SIZE(s->out));
Benoit Canet343ec8e2009-08-23 14:38:07 +02001440
1441 qemu_add_kbd_event_handler(musicpal_key_event, s);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +02001442
1443 return 0;
balrog24859b62008-04-24 19:21:53 +00001444}
1445
Jan Kiszkad5b61dd2009-09-18 20:51:23 +02001446static const VMStateDescription musicpal_key_vmsd = {
1447 .name = "musicpal_key",
1448 .version_id = 1,
1449 .minimum_version_id = 1,
1450 .minimum_version_id_old = 1,
1451 .fields = (VMStateField[]) {
1452 VMSTATE_UINT32(kbd_extended, musicpal_key_state),
1453 VMSTATE_UINT32(pressed_keys, musicpal_key_state),
1454 VMSTATE_END_OF_LIST()
1455 }
1456};
1457
1458static SysBusDeviceInfo musicpal_key_info = {
1459 .init = musicpal_key_init,
1460 .qdev.name = "musicpal_key",
1461 .qdev.size = sizeof(musicpal_key_state),
1462 .qdev.vmsd = &musicpal_key_vmsd,
1463};
1464
balrog24859b62008-04-24 19:21:53 +00001465static struct arm_boot_info musicpal_binfo = {
1466 .loader_start = 0x0,
1467 .board_id = 0x20e,
1468};
1469
Anthony Liguoric227f092009-10-01 16:12:16 -05001470static void musicpal_init(ram_addr_t ram_size,
aliguori3023f332009-01-16 19:04:14 +00001471 const char *boot_device,
balrog24859b62008-04-24 19:21:53 +00001472 const char *kernel_filename, const char *kernel_cmdline,
1473 const char *initrd_filename, const char *cpu_model)
1474{
1475 CPUState *env;
Paul Brookb47b50f2009-05-14 22:35:08 +01001476 qemu_irq *cpu_pic;
1477 qemu_irq pic[32];
1478 DeviceState *dev;
Andrzej Zaborowskid0747692009-08-23 15:51:14 +02001479 DeviceState *i2c_dev;
Benoit Canet343ec8e2009-08-23 14:38:07 +02001480 DeviceState *lcd_dev;
1481 DeviceState *key_dev;
Andrzej Zaborowskid0747692009-08-23 15:51:14 +02001482#ifdef HAS_AUDIO
1483 DeviceState *wm8750_dev;
1484 SysBusDevice *s;
1485#endif
1486 i2c_bus *i2c;
Paul Brookb47b50f2009-05-14 22:35:08 +01001487 int i;
balrog24859b62008-04-24 19:21:53 +00001488 unsigned long flash_size;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001489 DriveInfo *dinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05001490 ram_addr_t sram_off;
balrog24859b62008-04-24 19:21:53 +00001491
Jan Kiszka49fedd02009-09-18 20:51:23 +02001492 if (!cpu_model) {
balrog24859b62008-04-24 19:21:53 +00001493 cpu_model = "arm926";
Jan Kiszka49fedd02009-09-18 20:51:23 +02001494 }
balrog24859b62008-04-24 19:21:53 +00001495 env = cpu_init(cpu_model);
1496 if (!env) {
1497 fprintf(stderr, "Unable to find CPU definition\n");
1498 exit(1);
1499 }
Paul Brookb47b50f2009-05-14 22:35:08 +01001500 cpu_pic = arm_pic_init_cpu(env);
balrog24859b62008-04-24 19:21:53 +00001501
1502 /* For now we use a fixed - the original - RAM size */
1503 cpu_register_physical_memory(0, MP_RAM_DEFAULT_SIZE,
1504 qemu_ram_alloc(MP_RAM_DEFAULT_SIZE));
1505
1506 sram_off = qemu_ram_alloc(MP_SRAM_SIZE);
1507 cpu_register_physical_memory(MP_SRAM_BASE, MP_SRAM_SIZE, sram_off);
1508
Paul Brookb47b50f2009-05-14 22:35:08 +01001509 dev = sysbus_create_simple("mv88w8618_pic", MP_PIC_BASE,
1510 cpu_pic[ARM_PIC_CPU_IRQ]);
1511 for (i = 0; i < 32; i++) {
Paul Brook067a3dd2009-05-26 14:56:11 +01001512 pic[i] = qdev_get_gpio_in(dev, i);
Paul Brookb47b50f2009-05-14 22:35:08 +01001513 }
1514 sysbus_create_varargs("mv88w8618_pit", MP_PIT_BASE, pic[MP_TIMER1_IRQ],
1515 pic[MP_TIMER2_IRQ], pic[MP_TIMER3_IRQ],
1516 pic[MP_TIMER4_IRQ], NULL);
balrog24859b62008-04-24 19:21:53 +00001517
Jan Kiszka49fedd02009-09-18 20:51:23 +02001518 if (serial_hds[0]) {
aurel32b6cd0ea2008-05-04 21:42:11 +00001519 serial_mm_init(MP_UART1_BASE, 2, pic[MP_UART1_IRQ], 1825000,
balrog24859b62008-04-24 19:21:53 +00001520 serial_hds[0], 1);
Jan Kiszka49fedd02009-09-18 20:51:23 +02001521 }
1522 if (serial_hds[1]) {
aurel32b6cd0ea2008-05-04 21:42:11 +00001523 serial_mm_init(MP_UART2_BASE, 2, pic[MP_UART2_IRQ], 1825000,
balrog24859b62008-04-24 19:21:53 +00001524 serial_hds[1], 1);
Jan Kiszka49fedd02009-09-18 20:51:23 +02001525 }
balrog24859b62008-04-24 19:21:53 +00001526
1527 /* Register flash */
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001528 dinfo = drive_get(IF_PFLASH, 0, 0);
1529 if (dinfo) {
1530 flash_size = bdrv_getlength(dinfo->bdrv);
balrog24859b62008-04-24 19:21:53 +00001531 if (flash_size != 8*1024*1024 && flash_size != 16*1024*1024 &&
1532 flash_size != 32*1024*1024) {
1533 fprintf(stderr, "Invalid flash image size\n");
1534 exit(1);
1535 }
1536
1537 /*
1538 * The original U-Boot accesses the flash at 0xFE000000 instead of
1539 * 0xFF800000 (if there is 8 MB flash). So remap flash access if the
1540 * image is smaller than 32 MB.
1541 */
1542 pflash_cfi02_register(0-MP_FLASH_SIZE_MAX, qemu_ram_alloc(flash_size),
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001543 dinfo->bdrv, 0x10000,
balrog24859b62008-04-24 19:21:53 +00001544 (flash_size + 0xffff) >> 16,
1545 MP_FLASH_SIZE_MAX / flash_size,
1546 2, 0x00BF, 0x236D, 0x0000, 0x0000,
1547 0x5555, 0x2AAA);
1548 }
Paul Brookb47b50f2009-05-14 22:35:08 +01001549 sysbus_create_simple("mv88w8618_flashcfg", MP_FLASHCFG_BASE, NULL);
balrog24859b62008-04-24 19:21:53 +00001550
Paul Brookb47b50f2009-05-14 22:35:08 +01001551 qemu_check_nic_model(&nd_table[0], "mv88w8618");
1552 dev = qdev_create(NULL, "mv88w8618_eth");
Gerd Hoffmannee6847d2009-07-15 13:43:31 +02001553 dev->nd = &nd_table[0];
Markus Armbrustere23a1b32009-10-07 01:15:58 +02001554 qdev_init_nofail(dev);
Paul Brookb47b50f2009-05-14 22:35:08 +01001555 sysbus_mmio_map(sysbus_from_qdev(dev), 0, MP_ETH_BASE);
1556 sysbus_connect_irq(sysbus_from_qdev(dev), 0, pic[MP_ETH_IRQ]);
balrog24859b62008-04-24 19:21:53 +00001557
Paul Brookb47b50f2009-05-14 22:35:08 +01001558 sysbus_create_simple("mv88w8618_wlan", MP_WLAN_BASE, NULL);
malc718ec0b2009-03-10 19:25:13 +00001559
1560 musicpal_misc_init();
Benoit Canet343ec8e2009-08-23 14:38:07 +02001561
1562 dev = sysbus_create_simple("musicpal_gpio", MP_GPIO_BASE, pic[MP_GPIO_IRQ]);
Andrzej Zaborowskid0747692009-08-23 15:51:14 +02001563 i2c_dev = sysbus_create_simple("bitbang_i2c", 0, NULL);
1564 i2c = (i2c_bus *)qdev_get_child_bus(i2c_dev, "i2c");
1565
Benoit Canet343ec8e2009-08-23 14:38:07 +02001566 lcd_dev = sysbus_create_simple("musicpal_lcd", MP_LCD_BASE, NULL);
1567 key_dev = sysbus_create_simple("musicpal_key", 0, NULL);
1568
Andrzej Zaborowskid0747692009-08-23 15:51:14 +02001569 /* I2C read data */
Jan Kiszka708afdf2009-09-18 20:51:23 +02001570 qdev_connect_gpio_out(i2c_dev, 0,
1571 qdev_get_gpio_in(dev, MP_GPIO_I2C_DATA_BIT));
Andrzej Zaborowskid0747692009-08-23 15:51:14 +02001572 /* I2C data */
1573 qdev_connect_gpio_out(dev, 3, qdev_get_gpio_in(i2c_dev, 0));
1574 /* I2C clock */
1575 qdev_connect_gpio_out(dev, 4, qdev_get_gpio_in(i2c_dev, 1));
1576
Jan Kiszka49fedd02009-09-18 20:51:23 +02001577 for (i = 0; i < 3; i++) {
Benoit Canet343ec8e2009-08-23 14:38:07 +02001578 qdev_connect_gpio_out(dev, i, qdev_get_gpio_in(lcd_dev, i));
Jan Kiszka49fedd02009-09-18 20:51:23 +02001579 }
Jan Kiszka708afdf2009-09-18 20:51:23 +02001580 for (i = 0; i < 4; i++) {
1581 qdev_connect_gpio_out(key_dev, i, qdev_get_gpio_in(dev, i + 8));
1582 }
1583 for (i = 4; i < 8; i++) {
1584 qdev_connect_gpio_out(key_dev, i, qdev_get_gpio_in(dev, i + 15));
1585 }
balrog24859b62008-04-24 19:21:53 +00001586
Andrzej Zaborowskid0747692009-08-23 15:51:14 +02001587#ifdef HAS_AUDIO
1588 wm8750_dev = i2c_create_slave(i2c, "wm8750", MP_WM_ADDR);
1589 dev = qdev_create(NULL, "mv88w8618_audio");
1590 s = sysbus_from_qdev(dev);
1591 qdev_prop_set_ptr(dev, "wm8750", wm8750_dev);
Markus Armbrustere23a1b32009-10-07 01:15:58 +02001592 qdev_init_nofail(dev);
Andrzej Zaborowskid0747692009-08-23 15:51:14 +02001593 sysbus_mmio_map(s, 0, MP_AUDIO_BASE);
1594 sysbus_connect_irq(s, 0, pic[MP_AUDIO_IRQ]);
1595#endif
1596
balrog24859b62008-04-24 19:21:53 +00001597 musicpal_binfo.ram_size = MP_RAM_DEFAULT_SIZE;
1598 musicpal_binfo.kernel_filename = kernel_filename;
1599 musicpal_binfo.kernel_cmdline = kernel_cmdline;
1600 musicpal_binfo.initrd_filename = initrd_filename;
balrogb0f6edb2008-05-02 01:35:20 +00001601 arm_load_kernel(env, &musicpal_binfo);
balrog24859b62008-04-24 19:21:53 +00001602}
1603
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05001604static QEMUMachine musicpal_machine = {
aliguori4b32e162008-10-07 20:34:35 +00001605 .name = "musicpal",
1606 .desc = "Marvell 88w8618 / MusicPal (ARM926EJ-S)",
1607 .init = musicpal_init,
balrog24859b62008-04-24 19:21:53 +00001608};
Paul Brookb47b50f2009-05-14 22:35:08 +01001609
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05001610static void musicpal_machine_init(void)
1611{
1612 qemu_register_machine(&musicpal_machine);
1613}
1614
1615machine_init(musicpal_machine_init);
1616
Paul Brookb47b50f2009-05-14 22:35:08 +01001617static void musicpal_register_devices(void)
1618{
Jan Kiszkad5b61dd2009-09-18 20:51:23 +02001619 sysbus_register_withprop(&mv88w8618_pic_info);
Jan Kiszkac88d6bd2009-09-18 20:51:23 +02001620 sysbus_register_withprop(&mv88w8618_pit_info);
Jan Kiszkad5b61dd2009-09-18 20:51:23 +02001621 sysbus_register_withprop(&mv88w8618_flashcfg_info);
1622 sysbus_register_withprop(&mv88w8618_eth_info);
Paul Brookb47b50f2009-05-14 22:35:08 +01001623 sysbus_register_dev("mv88w8618_wlan", sizeof(SysBusDevice),
1624 mv88w8618_wlan_init);
Jan Kiszkad5b61dd2009-09-18 20:51:23 +02001625 sysbus_register_withprop(&musicpal_lcd_info);
Jan Kiszka30624c92009-09-18 20:51:23 +02001626 sysbus_register_withprop(&musicpal_gpio_info);
Jan Kiszkad5b61dd2009-09-18 20:51:23 +02001627 sysbus_register_withprop(&musicpal_key_info);
Paul Brookb47b50f2009-05-14 22:35:08 +01001628}
1629
1630device_init(musicpal_register_devices)