qemu: initialize hot add system / acpi gpe (Marcelo Tosatti)
[sniper_test.git] / hw / spitz.c
blobac844bddf7bfb317e796313124cfc012a15109dc
1 /*
2 * PXA270-based Clamshell PDA platforms.
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Written by Andrzej Zaborowski <balrog@zabor.org>
7 * This code is licensed under the GNU GPL v2.
8 */
10 #include "hw.h"
11 #include "pxa.h"
12 #include "arm-misc.h"
13 #include "sysemu.h"
14 #include "pcmcia.h"
15 #include "i2c.h"
16 #include "flash.h"
17 #include "qemu-timer.h"
18 #include "devices.h"
19 #include "sharpsl.h"
20 #include "console.h"
21 #include "block.h"
22 #include "audio/audio.h"
23 #include "boards.h"
25 #undef REG_FMT
26 #if TARGET_PHYS_ADDR_BITS == 32
27 #define REG_FMT "0x%02x"
28 #else
29 #define REG_FMT "0x%02lx"
30 #endif
32 /* Spitz Flash */
33 #define FLASH_BASE 0x0c000000
34 #define FLASH_ECCLPLB 0x00 /* Line parity 7 - 0 bit */
35 #define FLASH_ECCLPUB 0x04 /* Line parity 15 - 8 bit */
36 #define FLASH_ECCCP 0x08 /* Column parity 5 - 0 bit */
37 #define FLASH_ECCCNTR 0x0c /* ECC byte counter */
38 #define FLASH_ECCCLRR 0x10 /* Clear ECC */
39 #define FLASH_FLASHIO 0x14 /* Flash I/O */
40 #define FLASH_FLASHCTL 0x18 /* Flash Control */
42 #define FLASHCTL_CE0 (1 << 0)
43 #define FLASHCTL_CLE (1 << 1)
44 #define FLASHCTL_ALE (1 << 2)
45 #define FLASHCTL_WP (1 << 3)
46 #define FLASHCTL_CE1 (1 << 4)
47 #define FLASHCTL_RYBY (1 << 5)
48 #define FLASHCTL_NCE (FLASHCTL_CE0 | FLASHCTL_CE1)
50 struct sl_nand_s {
51 struct nand_flash_s *nand;
52 uint8_t ctl;
53 struct ecc_state_s ecc;
56 static uint32_t sl_readb(void *opaque, target_phys_addr_t addr)
58 struct sl_nand_s *s = (struct sl_nand_s *) opaque;
59 int ryby;
61 switch (addr) {
62 #define BSHR(byte, from, to) ((s->ecc.lp[byte] >> (from - to)) & (1 << to))
63 case FLASH_ECCLPLB:
64 return BSHR(0, 4, 0) | BSHR(0, 5, 2) | BSHR(0, 6, 4) | BSHR(0, 7, 6) |
65 BSHR(1, 4, 1) | BSHR(1, 5, 3) | BSHR(1, 6, 5) | BSHR(1, 7, 7);
67 #define BSHL(byte, from, to) ((s->ecc.lp[byte] << (to - from)) & (1 << to))
68 case FLASH_ECCLPUB:
69 return BSHL(0, 0, 0) | BSHL(0, 1, 2) | BSHL(0, 2, 4) | BSHL(0, 3, 6) |
70 BSHL(1, 0, 1) | BSHL(1, 1, 3) | BSHL(1, 2, 5) | BSHL(1, 3, 7);
72 case FLASH_ECCCP:
73 return s->ecc.cp;
75 case FLASH_ECCCNTR:
76 return s->ecc.count & 0xff;
78 case FLASH_FLASHCTL:
79 nand_getpins(s->nand, &ryby);
80 if (ryby)
81 return s->ctl | FLASHCTL_RYBY;
82 else
83 return s->ctl;
85 case FLASH_FLASHIO:
86 return ecc_digest(&s->ecc, nand_getio(s->nand));
88 default:
89 zaurus_printf("Bad register offset " REG_FMT "\n", addr);
91 return 0;
94 static uint32_t sl_readl(void *opaque, target_phys_addr_t addr)
96 struct sl_nand_s *s = (struct sl_nand_s *) opaque;
98 if (addr == FLASH_FLASHIO)
99 return ecc_digest(&s->ecc, nand_getio(s->nand)) |
100 (ecc_digest(&s->ecc, nand_getio(s->nand)) << 16);
102 return sl_readb(opaque, addr);
105 static void sl_writeb(void *opaque, target_phys_addr_t addr,
106 uint32_t value)
108 struct sl_nand_s *s = (struct sl_nand_s *) opaque;
110 switch (addr) {
111 case FLASH_ECCCLRR:
112 /* Value is ignored. */
113 ecc_reset(&s->ecc);
114 break;
116 case FLASH_FLASHCTL:
117 s->ctl = value & 0xff & ~FLASHCTL_RYBY;
118 nand_setpins(s->nand,
119 s->ctl & FLASHCTL_CLE,
120 s->ctl & FLASHCTL_ALE,
121 s->ctl & FLASHCTL_NCE,
122 s->ctl & FLASHCTL_WP,
124 break;
126 case FLASH_FLASHIO:
127 nand_setio(s->nand, ecc_digest(&s->ecc, value & 0xff));
128 break;
130 default:
131 zaurus_printf("Bad register offset " REG_FMT "\n", addr);
135 static void sl_save(QEMUFile *f, void *opaque)
137 struct sl_nand_s *s = (struct sl_nand_s *) opaque;
139 qemu_put_8s(f, &s->ctl);
140 ecc_put(f, &s->ecc);
143 static int sl_load(QEMUFile *f, void *opaque, int version_id)
145 struct sl_nand_s *s = (struct sl_nand_s *) opaque;
147 qemu_get_8s(f, &s->ctl);
148 ecc_get(f, &s->ecc);
150 return 0;
153 enum {
154 FLASH_128M,
155 FLASH_1024M,
158 static void sl_flash_register(struct pxa2xx_state_s *cpu, int size)
160 int iomemtype;
161 struct sl_nand_s *s;
162 CPUReadMemoryFunc *sl_readfn[] = {
163 sl_readb,
164 sl_readb,
165 sl_readl,
167 CPUWriteMemoryFunc *sl_writefn[] = {
168 sl_writeb,
169 sl_writeb,
170 sl_writeb,
173 s = (struct sl_nand_s *) qemu_mallocz(sizeof(struct sl_nand_s));
174 s->ctl = 0;
175 if (size == FLASH_128M)
176 s->nand = nand_init(NAND_MFR_SAMSUNG, 0x73);
177 else if (size == FLASH_1024M)
178 s->nand = nand_init(NAND_MFR_SAMSUNG, 0xf1);
180 iomemtype = cpu_register_io_memory(0, sl_readfn,
181 sl_writefn, s);
182 cpu_register_physical_memory(FLASH_BASE, 0x40, iomemtype);
184 register_savevm("sl_flash", 0, 0, sl_save, sl_load, s);
187 /* Spitz Keyboard */
189 #define SPITZ_KEY_STROBE_NUM 11
190 #define SPITZ_KEY_SENSE_NUM 7
192 static const int spitz_gpio_key_sense[SPITZ_KEY_SENSE_NUM] = {
193 12, 17, 91, 34, 36, 38, 39
196 static const int spitz_gpio_key_strobe[SPITZ_KEY_STROBE_NUM] = {
197 88, 23, 24, 25, 26, 27, 52, 103, 107, 108, 114
200 /* Eighth additional row maps the special keys */
201 static int spitz_keymap[SPITZ_KEY_SENSE_NUM + 1][SPITZ_KEY_STROBE_NUM] = {
202 { 0x1d, 0x02, 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0e, 0x3f, 0x40 },
203 { -1 , 0x03, 0x05, 0x13, 0x15, 0x09, 0x17, 0x18, 0x19, 0x41, 0x42 },
204 { 0x0f, 0x10, 0x12, 0x14, 0x22, 0x16, 0x24, 0x25, -1 , -1 , -1 },
205 { 0x3c, 0x11, 0x1f, 0x21, 0x2f, 0x23, 0x32, 0x26, -1 , 0x36, -1 },
206 { 0x3b, 0x1e, 0x20, 0x2e, 0x30, 0x31, 0x34, -1 , 0x1c, 0x2a, -1 },
207 { 0x44, 0x2c, 0x2d, 0x0c, 0x39, 0x33, -1 , 0x48, -1 , -1 , 0x38 },
208 { 0x37, 0x3d, -1 , 0x45, 0x57, 0x58, 0x4b, 0x50, 0x4d, -1 , -1 },
209 { 0x52, 0x43, 0x01, 0x47, 0x49, -1 , -1 , -1 , -1 , -1 , -1 },
212 #define SPITZ_GPIO_AK_INT 13 /* Remote control */
213 #define SPITZ_GPIO_SYNC 16 /* Sync button */
214 #define SPITZ_GPIO_ON_KEY 95 /* Power button */
215 #define SPITZ_GPIO_SWA 97 /* Lid */
216 #define SPITZ_GPIO_SWB 96 /* Tablet mode */
218 /* The special buttons are mapped to unused keys */
219 static const int spitz_gpiomap[5] = {
220 SPITZ_GPIO_AK_INT, SPITZ_GPIO_SYNC, SPITZ_GPIO_ON_KEY,
221 SPITZ_GPIO_SWA, SPITZ_GPIO_SWB,
223 static int spitz_gpio_invert[5] = { 0, 0, 0, 0, 0, };
225 struct spitz_keyboard_s {
226 qemu_irq sense[SPITZ_KEY_SENSE_NUM];
227 qemu_irq *strobe;
228 qemu_irq gpiomap[5];
229 int keymap[0x80];
230 uint16_t keyrow[SPITZ_KEY_SENSE_NUM];
231 uint16_t strobe_state;
232 uint16_t sense_state;
234 uint16_t pre_map[0x100];
235 uint16_t modifiers;
236 uint16_t imodifiers;
237 uint8_t fifo[16];
238 int fifopos, fifolen;
239 QEMUTimer *kbdtimer;
242 static void spitz_keyboard_sense_update(struct spitz_keyboard_s *s)
244 int i;
245 uint16_t strobe, sense = 0;
246 for (i = 0; i < SPITZ_KEY_SENSE_NUM; i ++) {
247 strobe = s->keyrow[i] & s->strobe_state;
248 if (strobe) {
249 sense |= 1 << i;
250 if (!(s->sense_state & (1 << i)))
251 qemu_irq_raise(s->sense[i]);
252 } else if (s->sense_state & (1 << i))
253 qemu_irq_lower(s->sense[i]);
256 s->sense_state = sense;
259 static void spitz_keyboard_strobe(void *opaque, int line, int level)
261 struct spitz_keyboard_s *s = (struct spitz_keyboard_s *) opaque;
263 if (level)
264 s->strobe_state |= 1 << line;
265 else
266 s->strobe_state &= ~(1 << line);
267 spitz_keyboard_sense_update(s);
270 static void spitz_keyboard_keydown(struct spitz_keyboard_s *s, int keycode)
272 int spitz_keycode = s->keymap[keycode & 0x7f];
273 if (spitz_keycode == -1)
274 return;
276 /* Handle the additional keys */
277 if ((spitz_keycode >> 4) == SPITZ_KEY_SENSE_NUM) {
278 qemu_set_irq(s->gpiomap[spitz_keycode & 0xf], (keycode < 0x80) ^
279 spitz_gpio_invert[spitz_keycode & 0xf]);
280 return;
283 if (keycode & 0x80)
284 s->keyrow[spitz_keycode >> 4] &= ~(1 << (spitz_keycode & 0xf));
285 else
286 s->keyrow[spitz_keycode >> 4] |= 1 << (spitz_keycode & 0xf);
288 spitz_keyboard_sense_update(s);
291 #define SHIFT (1 << 7)
292 #define CTRL (1 << 8)
293 #define FN (1 << 9)
295 #define QUEUE_KEY(c) s->fifo[(s->fifopos + s->fifolen ++) & 0xf] = c
297 static void spitz_keyboard_handler(struct spitz_keyboard_s *s, int keycode)
299 uint16_t code;
300 int mapcode;
301 switch (keycode) {
302 case 0x2a: /* Left Shift */
303 s->modifiers |= 1;
304 break;
305 case 0xaa:
306 s->modifiers &= ~1;
307 break;
308 case 0x36: /* Right Shift */
309 s->modifiers |= 2;
310 break;
311 case 0xb6:
312 s->modifiers &= ~2;
313 break;
314 case 0x1d: /* Control */
315 s->modifiers |= 4;
316 break;
317 case 0x9d:
318 s->modifiers &= ~4;
319 break;
320 case 0x38: /* Alt */
321 s->modifiers |= 8;
322 break;
323 case 0xb8:
324 s->modifiers &= ~8;
325 break;
328 code = s->pre_map[mapcode = ((s->modifiers & 3) ?
329 (keycode | SHIFT) :
330 (keycode & ~SHIFT))];
332 if (code != mapcode) {
333 #if 0
334 if ((code & SHIFT) && !(s->modifiers & 1))
335 QUEUE_KEY(0x2a | (keycode & 0x80));
336 if ((code & CTRL ) && !(s->modifiers & 4))
337 QUEUE_KEY(0x1d | (keycode & 0x80));
338 if ((code & FN ) && !(s->modifiers & 8))
339 QUEUE_KEY(0x38 | (keycode & 0x80));
340 if ((code & FN ) && (s->modifiers & 1))
341 QUEUE_KEY(0x2a | (~keycode & 0x80));
342 if ((code & FN ) && (s->modifiers & 2))
343 QUEUE_KEY(0x36 | (~keycode & 0x80));
344 #else
345 if (keycode & 0x80) {
346 if ((s->imodifiers & 1 ) && !(s->modifiers & 1))
347 QUEUE_KEY(0x2a | 0x80);
348 if ((s->imodifiers & 4 ) && !(s->modifiers & 4))
349 QUEUE_KEY(0x1d | 0x80);
350 if ((s->imodifiers & 8 ) && !(s->modifiers & 8))
351 QUEUE_KEY(0x38 | 0x80);
352 if ((s->imodifiers & 0x10) && (s->modifiers & 1))
353 QUEUE_KEY(0x2a);
354 if ((s->imodifiers & 0x20) && (s->modifiers & 2))
355 QUEUE_KEY(0x36);
356 s->imodifiers = 0;
357 } else {
358 if ((code & SHIFT) && !((s->modifiers | s->imodifiers) & 1)) {
359 QUEUE_KEY(0x2a);
360 s->imodifiers |= 1;
362 if ((code & CTRL ) && !((s->modifiers | s->imodifiers) & 4)) {
363 QUEUE_KEY(0x1d);
364 s->imodifiers |= 4;
366 if ((code & FN ) && !((s->modifiers | s->imodifiers) & 8)) {
367 QUEUE_KEY(0x38);
368 s->imodifiers |= 8;
370 if ((code & FN ) && (s->modifiers & 1) &&
371 !(s->imodifiers & 0x10)) {
372 QUEUE_KEY(0x2a | 0x80);
373 s->imodifiers |= 0x10;
375 if ((code & FN ) && (s->modifiers & 2) &&
376 !(s->imodifiers & 0x20)) {
377 QUEUE_KEY(0x36 | 0x80);
378 s->imodifiers |= 0x20;
381 #endif
384 QUEUE_KEY((code & 0x7f) | (keycode & 0x80));
387 static void spitz_keyboard_tick(void *opaque)
389 struct spitz_keyboard_s *s = (struct spitz_keyboard_s *) opaque;
391 if (s->fifolen) {
392 spitz_keyboard_keydown(s, s->fifo[s->fifopos ++]);
393 s->fifolen --;
394 if (s->fifopos >= 16)
395 s->fifopos = 0;
398 qemu_mod_timer(s->kbdtimer, qemu_get_clock(vm_clock) + ticks_per_sec / 32);
401 static void spitz_keyboard_pre_map(struct spitz_keyboard_s *s)
403 int i;
404 for (i = 0; i < 0x100; i ++)
405 s->pre_map[i] = i;
406 s->pre_map[0x02 | SHIFT ] = 0x02 | SHIFT; /* exclam */
407 s->pre_map[0x28 | SHIFT ] = 0x03 | SHIFT; /* quotedbl */
408 s->pre_map[0x04 | SHIFT ] = 0x04 | SHIFT; /* numbersign */
409 s->pre_map[0x05 | SHIFT ] = 0x05 | SHIFT; /* dollar */
410 s->pre_map[0x06 | SHIFT ] = 0x06 | SHIFT; /* percent */
411 s->pre_map[0x08 | SHIFT ] = 0x07 | SHIFT; /* ampersand */
412 s->pre_map[0x28 ] = 0x08 | SHIFT; /* apostrophe */
413 s->pre_map[0x0a | SHIFT ] = 0x09 | SHIFT; /* parenleft */
414 s->pre_map[0x0b | SHIFT ] = 0x0a | SHIFT; /* parenright */
415 s->pre_map[0x29 | SHIFT ] = 0x0b | SHIFT; /* asciitilde */
416 s->pre_map[0x03 | SHIFT ] = 0x0c | SHIFT; /* at */
417 s->pre_map[0xd3 ] = 0x0e | FN; /* Delete */
418 s->pre_map[0x3a ] = 0x0f | FN; /* Caps_Lock */
419 s->pre_map[0x07 | SHIFT ] = 0x11 | FN; /* asciicircum */
420 s->pre_map[0x0d ] = 0x12 | FN; /* equal */
421 s->pre_map[0x0d | SHIFT ] = 0x13 | FN; /* plus */
422 s->pre_map[0x1a ] = 0x14 | FN; /* bracketleft */
423 s->pre_map[0x1b ] = 0x15 | FN; /* bracketright */
424 s->pre_map[0x1a | SHIFT ] = 0x16 | FN; /* braceleft */
425 s->pre_map[0x1b | SHIFT ] = 0x17 | FN; /* braceright */
426 s->pre_map[0x27 ] = 0x22 | FN; /* semicolon */
427 s->pre_map[0x27 | SHIFT ] = 0x23 | FN; /* colon */
428 s->pre_map[0x09 | SHIFT ] = 0x24 | FN; /* asterisk */
429 s->pre_map[0x2b ] = 0x25 | FN; /* backslash */
430 s->pre_map[0x2b | SHIFT ] = 0x26 | FN; /* bar */
431 s->pre_map[0x0c | SHIFT ] = 0x30 | FN; /* underscore */
432 s->pre_map[0x33 | SHIFT ] = 0x33 | FN; /* less */
433 s->pre_map[0x35 ] = 0x33 | SHIFT; /* slash */
434 s->pre_map[0x34 | SHIFT ] = 0x34 | FN; /* greater */
435 s->pre_map[0x35 | SHIFT ] = 0x34 | SHIFT; /* question */
436 s->pre_map[0x49 ] = 0x48 | FN; /* Page_Up */
437 s->pre_map[0x51 ] = 0x50 | FN; /* Page_Down */
439 s->modifiers = 0;
440 s->imodifiers = 0;
441 s->fifopos = 0;
442 s->fifolen = 0;
443 s->kbdtimer = qemu_new_timer(vm_clock, spitz_keyboard_tick, s);
444 spitz_keyboard_tick(s);
447 #undef SHIFT
448 #undef CTRL
449 #undef FN
451 static void spitz_keyboard_save(QEMUFile *f, void *opaque)
453 struct spitz_keyboard_s *s = (struct spitz_keyboard_s *) opaque;
454 int i;
456 qemu_put_be16s(f, &s->sense_state);
457 qemu_put_be16s(f, &s->strobe_state);
458 for (i = 0; i < 5; i ++)
459 qemu_put_byte(f, spitz_gpio_invert[i]);
462 static int spitz_keyboard_load(QEMUFile *f, void *opaque, int version_id)
464 struct spitz_keyboard_s *s = (struct spitz_keyboard_s *) opaque;
465 int i;
467 qemu_get_be16s(f, &s->sense_state);
468 qemu_get_be16s(f, &s->strobe_state);
469 for (i = 0; i < 5; i ++)
470 spitz_gpio_invert[i] = qemu_get_byte(f);
472 /* Release all pressed keys */
473 memset(s->keyrow, 0, sizeof(s->keyrow));
474 spitz_keyboard_sense_update(s);
475 s->modifiers = 0;
476 s->imodifiers = 0;
477 s->fifopos = 0;
478 s->fifolen = 0;
480 return 0;
483 static void spitz_keyboard_register(struct pxa2xx_state_s *cpu)
485 int i, j;
486 struct spitz_keyboard_s *s;
488 s = (struct spitz_keyboard_s *)
489 qemu_mallocz(sizeof(struct spitz_keyboard_s));
490 memset(s, 0, sizeof(struct spitz_keyboard_s));
492 for (i = 0; i < 0x80; i ++)
493 s->keymap[i] = -1;
494 for (i = 0; i < SPITZ_KEY_SENSE_NUM + 1; i ++)
495 for (j = 0; j < SPITZ_KEY_STROBE_NUM; j ++)
496 if (spitz_keymap[i][j] != -1)
497 s->keymap[spitz_keymap[i][j]] = (i << 4) | j;
499 for (i = 0; i < SPITZ_KEY_SENSE_NUM; i ++)
500 s->sense[i] = pxa2xx_gpio_in_get(cpu->gpio)[spitz_gpio_key_sense[i]];
502 for (i = 0; i < 5; i ++)
503 s->gpiomap[i] = pxa2xx_gpio_in_get(cpu->gpio)[spitz_gpiomap[i]];
505 s->strobe = qemu_allocate_irqs(spitz_keyboard_strobe, s,
506 SPITZ_KEY_STROBE_NUM);
507 for (i = 0; i < SPITZ_KEY_STROBE_NUM; i ++)
508 pxa2xx_gpio_out_set(cpu->gpio, spitz_gpio_key_strobe[i], s->strobe[i]);
510 spitz_keyboard_pre_map(s);
511 qemu_add_kbd_event_handler((QEMUPutKBDEvent *) spitz_keyboard_handler, s);
513 register_savevm("spitz_keyboard", 0, 0,
514 spitz_keyboard_save, spitz_keyboard_load, s);
517 /* LCD backlight controller */
519 #define LCDTG_RESCTL 0x00
520 #define LCDTG_PHACTRL 0x01
521 #define LCDTG_DUTYCTRL 0x02
522 #define LCDTG_POWERREG0 0x03
523 #define LCDTG_POWERREG1 0x04
524 #define LCDTG_GPOR3 0x05
525 #define LCDTG_PICTRL 0x06
526 #define LCDTG_POLCTRL 0x07
528 static int bl_intensity, bl_power;
530 static void spitz_bl_update(struct pxa2xx_state_s *s)
532 if (bl_power && bl_intensity)
533 zaurus_printf("LCD Backlight now at %i/63\n", bl_intensity);
534 else
535 zaurus_printf("LCD Backlight now off\n");
538 static inline void spitz_bl_bit5(void *opaque, int line, int level)
540 int prev = bl_intensity;
542 if (level)
543 bl_intensity &= ~0x20;
544 else
545 bl_intensity |= 0x20;
547 if (bl_power && prev != bl_intensity)
548 spitz_bl_update((struct pxa2xx_state_s *) opaque);
551 static inline void spitz_bl_power(void *opaque, int line, int level)
553 bl_power = !!level;
554 spitz_bl_update((struct pxa2xx_state_s *) opaque);
557 static void spitz_lcdtg_dac_put(void *opaque, uint8_t cmd)
559 int addr, value;
560 addr = cmd >> 5;
561 value = cmd & 0x1f;
563 switch (addr) {
564 case LCDTG_RESCTL:
565 if (value)
566 zaurus_printf("LCD in QVGA mode\n");
567 else
568 zaurus_printf("LCD in VGA mode\n");
569 break;
571 case LCDTG_DUTYCTRL:
572 bl_intensity &= ~0x1f;
573 bl_intensity |= value;
574 if (bl_power)
575 spitz_bl_update((struct pxa2xx_state_s *) opaque);
576 break;
578 case LCDTG_POWERREG0:
579 /* Set common voltage to M62332FP */
580 break;
584 /* SSP devices */
586 #define CORGI_SSP_PORT 2
588 #define SPITZ_GPIO_LCDCON_CS 53
589 #define SPITZ_GPIO_ADS7846_CS 14
590 #define SPITZ_GPIO_MAX1111_CS 20
591 #define SPITZ_GPIO_TP_INT 11
593 static int lcd_en, ads_en, max_en;
594 static struct max111x_s *max1111;
595 static struct ads7846_state_s *ads7846;
597 /* "Demux" the signal based on current chipselect */
598 static uint32_t corgi_ssp_read(void *opaque)
600 if (lcd_en)
601 return 0;
602 if (ads_en)
603 return ads7846_read(ads7846);
604 if (max_en)
605 return max111x_read(max1111);
606 return 0;
609 static void corgi_ssp_write(void *opaque, uint32_t value)
611 if (lcd_en)
612 spitz_lcdtg_dac_put(opaque, value);
613 if (ads_en)
614 ads7846_write(ads7846, value);
615 if (max_en)
616 max111x_write(max1111, value);
619 static void corgi_ssp_gpio_cs(void *opaque, int line, int level)
621 switch (line) {
622 case 0:
623 lcd_en = !level;
624 break;
625 case 1:
626 ads_en = !level;
627 break;
628 case 2:
629 max_en = !level;
630 break;
634 #define MAX1111_BATT_VOLT 1
635 #define MAX1111_BATT_TEMP 2
636 #define MAX1111_ACIN_VOLT 3
638 #define SPITZ_BATTERY_TEMP 0xe0 /* About 2.9V */
639 #define SPITZ_BATTERY_VOLT 0xd0 /* About 4.0V */
640 #define SPITZ_CHARGEON_ACIN 0x80 /* About 5.0V */
642 static void spitz_adc_temp_on(void *opaque, int line, int level)
644 if (!max1111)
645 return;
647 if (level)
648 max111x_set_input(max1111, MAX1111_BATT_TEMP, SPITZ_BATTERY_TEMP);
649 else
650 max111x_set_input(max1111, MAX1111_BATT_TEMP, 0);
653 static void spitz_ssp_save(QEMUFile *f, void *opaque)
655 qemu_put_be32(f, lcd_en);
656 qemu_put_be32(f, ads_en);
657 qemu_put_be32(f, max_en);
658 qemu_put_be32(f, bl_intensity);
659 qemu_put_be32(f, bl_power);
662 static int spitz_ssp_load(QEMUFile *f, void *opaque, int version_id)
664 lcd_en = qemu_get_be32(f);
665 ads_en = qemu_get_be32(f);
666 max_en = qemu_get_be32(f);
667 bl_intensity = qemu_get_be32(f);
668 bl_power = qemu_get_be32(f);
670 return 0;
673 static void spitz_ssp_attach(struct pxa2xx_state_s *cpu)
675 qemu_irq *chipselects;
677 lcd_en = ads_en = max_en = 0;
679 ads7846 = ads7846_init(pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_TP_INT]);
681 max1111 = max1111_init(0);
682 max111x_set_input(max1111, MAX1111_BATT_VOLT, SPITZ_BATTERY_VOLT);
683 max111x_set_input(max1111, MAX1111_BATT_TEMP, 0);
684 max111x_set_input(max1111, MAX1111_ACIN_VOLT, SPITZ_CHARGEON_ACIN);
686 pxa2xx_ssp_attach(cpu->ssp[CORGI_SSP_PORT - 1], corgi_ssp_read,
687 corgi_ssp_write, cpu);
689 chipselects = qemu_allocate_irqs(corgi_ssp_gpio_cs, cpu, 3);
690 pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_LCDCON_CS, chipselects[0]);
691 pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_ADS7846_CS, chipselects[1]);
692 pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_MAX1111_CS, chipselects[2]);
694 bl_intensity = 0x20;
695 bl_power = 0;
697 register_savevm("spitz_ssp", 0, 0, spitz_ssp_save, spitz_ssp_load, cpu);
700 /* CF Microdrive */
702 static void spitz_microdrive_attach(struct pxa2xx_state_s *cpu, int slot)
704 struct pcmcia_card_s *md;
705 int index;
706 BlockDriverState *bs;
708 index = drive_get_index(IF_IDE, 0, 0);
709 if (index == -1)
710 return;
711 bs = drives_table[index].bdrv;
712 if (bdrv_is_inserted(bs) && !bdrv_is_removable(bs)) {
713 md = dscm1xxxx_init(bs);
714 pxa2xx_pcmcia_attach(cpu->pcmcia[slot], md);
718 /* Wm8750 and Max7310 on I2C */
720 #define AKITA_MAX_ADDR 0x18
721 #define SPITZ_WM_ADDRL 0x1b
722 #define SPITZ_WM_ADDRH 0x1a
724 #define SPITZ_GPIO_WM 5
726 #ifdef HAS_AUDIO
727 static void spitz_wm8750_addr(void *opaque, int line, int level)
729 i2c_slave *wm = (i2c_slave *) opaque;
730 if (level)
731 i2c_set_slave_address(wm, SPITZ_WM_ADDRH);
732 else
733 i2c_set_slave_address(wm, SPITZ_WM_ADDRL);
735 #endif
737 static void spitz_i2c_setup(struct pxa2xx_state_s *cpu)
739 /* Attach the CPU on one end of our I2C bus. */
740 i2c_bus *bus = pxa2xx_i2c_bus(cpu->i2c[0]);
742 #ifdef HAS_AUDIO
743 AudioState *audio;
744 i2c_slave *wm;
746 audio = AUD_init();
747 if (!audio)
748 return;
749 /* Attach a WM8750 to the bus */
750 wm = wm8750_init(bus, audio);
752 spitz_wm8750_addr(wm, 0, 0);
753 pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_WM,
754 qemu_allocate_irqs(spitz_wm8750_addr, wm, 1)[0]);
755 /* .. and to the sound interface. */
756 cpu->i2s->opaque = wm;
757 cpu->i2s->codec_out = wm8750_dac_dat;
758 cpu->i2s->codec_in = wm8750_adc_dat;
759 wm8750_data_req_set(wm, cpu->i2s->data_req, cpu->i2s);
760 #endif
763 static void spitz_akita_i2c_setup(struct pxa2xx_state_s *cpu)
765 /* Attach a Max7310 to Akita I2C bus. */
766 i2c_set_slave_address(max7310_init(pxa2xx_i2c_bus(cpu->i2c[0])),
767 AKITA_MAX_ADDR);
770 /* Other peripherals */
772 static void spitz_out_switch(void *opaque, int line, int level)
774 switch (line) {
775 case 0:
776 zaurus_printf("Charging %s.\n", level ? "off" : "on");
777 break;
778 case 1:
779 zaurus_printf("Discharging %s.\n", level ? "on" : "off");
780 break;
781 case 2:
782 zaurus_printf("Green LED %s.\n", level ? "on" : "off");
783 break;
784 case 3:
785 zaurus_printf("Orange LED %s.\n", level ? "on" : "off");
786 break;
787 case 4:
788 spitz_bl_bit5(opaque, line, level);
789 break;
790 case 5:
791 spitz_bl_power(opaque, line, level);
792 break;
793 case 6:
794 spitz_adc_temp_on(opaque, line, level);
795 break;
799 #define SPITZ_SCP_LED_GREEN 1
800 #define SPITZ_SCP_JK_B 2
801 #define SPITZ_SCP_CHRG_ON 3
802 #define SPITZ_SCP_MUTE_L 4
803 #define SPITZ_SCP_MUTE_R 5
804 #define SPITZ_SCP_CF_POWER 6
805 #define SPITZ_SCP_LED_ORANGE 7
806 #define SPITZ_SCP_JK_A 8
807 #define SPITZ_SCP_ADC_TEMP_ON 9
808 #define SPITZ_SCP2_IR_ON 1
809 #define SPITZ_SCP2_AKIN_PULLUP 2
810 #define SPITZ_SCP2_BACKLIGHT_CONT 7
811 #define SPITZ_SCP2_BACKLIGHT_ON 8
812 #define SPITZ_SCP2_MIC_BIAS 9
814 static void spitz_scoop_gpio_setup(struct pxa2xx_state_s *cpu,
815 struct scoop_info_s *scp0, struct scoop_info_s *scp1)
817 qemu_irq *outsignals = qemu_allocate_irqs(spitz_out_switch, cpu, 8);
819 scoop_gpio_out_set(scp0, SPITZ_SCP_CHRG_ON, outsignals[0]);
820 scoop_gpio_out_set(scp0, SPITZ_SCP_JK_B, outsignals[1]);
821 scoop_gpio_out_set(scp0, SPITZ_SCP_LED_GREEN, outsignals[2]);
822 scoop_gpio_out_set(scp0, SPITZ_SCP_LED_ORANGE, outsignals[3]);
824 if (scp1) {
825 scoop_gpio_out_set(scp1, SPITZ_SCP2_BACKLIGHT_CONT, outsignals[4]);
826 scoop_gpio_out_set(scp1, SPITZ_SCP2_BACKLIGHT_ON, outsignals[5]);
829 scoop_gpio_out_set(scp0, SPITZ_SCP_ADC_TEMP_ON, outsignals[6]);
832 #define SPITZ_GPIO_HSYNC 22
833 #define SPITZ_GPIO_SD_DETECT 9
834 #define SPITZ_GPIO_SD_WP 81
835 #define SPITZ_GPIO_ON_RESET 89
836 #define SPITZ_GPIO_BAT_COVER 90
837 #define SPITZ_GPIO_CF1_IRQ 105
838 #define SPITZ_GPIO_CF1_CD 94
839 #define SPITZ_GPIO_CF2_IRQ 106
840 #define SPITZ_GPIO_CF2_CD 93
842 static int spitz_hsync;
844 static void spitz_lcd_hsync_handler(void *opaque, int line, int level)
846 struct pxa2xx_state_s *cpu = (struct pxa2xx_state_s *) opaque;
847 qemu_set_irq(pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_HSYNC], spitz_hsync);
848 spitz_hsync ^= 1;
851 static void spitz_gpio_setup(struct pxa2xx_state_s *cpu, int slots)
853 qemu_irq lcd_hsync;
855 * Bad hack: We toggle the LCD hsync GPIO on every GPIO status
856 * read to satisfy broken guests that poll-wait for hsync.
857 * Simulating a real hsync event would be less practical and
858 * wouldn't guarantee that a guest ever exits the loop.
860 spitz_hsync = 0;
861 lcd_hsync = qemu_allocate_irqs(spitz_lcd_hsync_handler, cpu, 1)[0];
862 pxa2xx_gpio_read_notifier(cpu->gpio, lcd_hsync);
863 pxa2xx_lcd_vsync_notifier(cpu->lcd, lcd_hsync);
865 /* MMC/SD host */
866 pxa2xx_mmci_handlers(cpu->mmc,
867 pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_SD_WP],
868 pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_SD_DETECT]);
870 /* Battery lock always closed */
871 qemu_irq_raise(pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_BAT_COVER]);
873 /* Handle reset */
874 pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_ON_RESET, cpu->reset);
876 /* PCMCIA signals: card's IRQ and Card-Detect */
877 if (slots >= 1)
878 pxa2xx_pcmcia_set_irq_cb(cpu->pcmcia[0],
879 pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_CF1_IRQ],
880 pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_CF1_CD]);
881 if (slots >= 2)
882 pxa2xx_pcmcia_set_irq_cb(cpu->pcmcia[1],
883 pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_CF2_IRQ],
884 pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_CF2_CD]);
886 /* Initialise the screen rotation related signals */
887 spitz_gpio_invert[3] = 0; /* Always open */
888 if (graphic_rotate) { /* Tablet mode */
889 spitz_gpio_invert[4] = 0;
890 } else { /* Portrait mode */
891 spitz_gpio_invert[4] = 1;
893 qemu_set_irq(pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_SWA],
894 spitz_gpio_invert[3]);
895 qemu_set_irq(pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_SWB],
896 spitz_gpio_invert[4]);
899 /* Board init. */
900 enum spitz_model_e { spitz, akita, borzoi, terrier };
902 #define SPITZ_RAM 0x04000000
903 #define SPITZ_ROM 0x00800000
905 static struct arm_boot_info spitz_binfo = {
906 .loader_start = PXA2XX_SDRAM_BASE,
907 .ram_size = 0x04000000,
910 static void spitz_common_init(ram_addr_t ram_size, int vga_ram_size,
911 const char *kernel_filename,
912 const char *kernel_cmdline, const char *initrd_filename,
913 const char *cpu_model, enum spitz_model_e model, int arm_id)
915 struct pxa2xx_state_s *cpu;
916 struct scoop_info_s *scp0, *scp1 = NULL;
918 if (!cpu_model)
919 cpu_model = (model == terrier) ? "pxa270-c5" : "pxa270-c0";
921 /* Setup CPU & memory */
922 if (ram_size < SPITZ_RAM + SPITZ_ROM + PXA2XX_INTERNAL_SIZE) {
923 fprintf(stderr, "This platform requires %i bytes of memory\n",
924 SPITZ_RAM + SPITZ_ROM + PXA2XX_INTERNAL_SIZE);
925 exit(1);
927 cpu = pxa270_init(spitz_binfo.ram_size, cpu_model);
929 sl_flash_register(cpu, (model == spitz) ? FLASH_128M : FLASH_1024M);
931 cpu_register_physical_memory(0, SPITZ_ROM,
932 qemu_ram_alloc(SPITZ_ROM) | IO_MEM_ROM);
934 /* Setup peripherals */
935 spitz_keyboard_register(cpu);
937 spitz_ssp_attach(cpu);
939 scp0 = scoop_init(cpu, 0, 0x10800000);
940 if (model != akita) {
941 scp1 = scoop_init(cpu, 1, 0x08800040);
944 spitz_scoop_gpio_setup(cpu, scp0, scp1);
946 spitz_gpio_setup(cpu, (model == akita) ? 1 : 2);
948 spitz_i2c_setup(cpu);
950 if (model == akita)
951 spitz_akita_i2c_setup(cpu);
953 if (model == terrier)
954 /* A 6.0 GB microdrive is permanently sitting in CF slot 1. */
955 spitz_microdrive_attach(cpu, 1);
956 else if (model != akita)
957 /* A 4.0 GB microdrive is permanently sitting in CF slot 0. */
958 spitz_microdrive_attach(cpu, 0);
960 /* Setup initial (reset) machine state */
961 cpu->env->regs[15] = spitz_binfo.loader_start;
963 spitz_binfo.kernel_filename = kernel_filename;
964 spitz_binfo.kernel_cmdline = kernel_cmdline;
965 spitz_binfo.initrd_filename = initrd_filename;
966 spitz_binfo.board_id = arm_id;
967 arm_load_kernel(cpu->env, &spitz_binfo);
968 sl_bootparam_write(SL_PXA_PARAM_BASE - PXA2XX_SDRAM_BASE);
971 static void spitz_init(ram_addr_t ram_size, int vga_ram_size,
972 const char *boot_device,
973 const char *kernel_filename, const char *kernel_cmdline,
974 const char *initrd_filename, const char *cpu_model)
976 spitz_common_init(ram_size, vga_ram_size, kernel_filename,
977 kernel_cmdline, initrd_filename, cpu_model, spitz, 0x2c9);
980 static void borzoi_init(ram_addr_t ram_size, int vga_ram_size,
981 const char *boot_device,
982 const char *kernel_filename, const char *kernel_cmdline,
983 const char *initrd_filename, const char *cpu_model)
985 spitz_common_init(ram_size, vga_ram_size, kernel_filename,
986 kernel_cmdline, initrd_filename, cpu_model, borzoi, 0x33f);
989 static void akita_init(ram_addr_t ram_size, int vga_ram_size,
990 const char *boot_device,
991 const char *kernel_filename, const char *kernel_cmdline,
992 const char *initrd_filename, const char *cpu_model)
994 spitz_common_init(ram_size, vga_ram_size, kernel_filename,
995 kernel_cmdline, initrd_filename, cpu_model, akita, 0x2e8);
998 static void terrier_init(ram_addr_t ram_size, int vga_ram_size,
999 const char *boot_device,
1000 const char *kernel_filename, const char *kernel_cmdline,
1001 const char *initrd_filename, const char *cpu_model)
1003 spitz_common_init(ram_size, vga_ram_size, kernel_filename,
1004 kernel_cmdline, initrd_filename, cpu_model, terrier, 0x33f);
1007 QEMUMachine akitapda_machine = {
1008 .name = "akita",
1009 .desc = "Akita PDA (PXA270)",
1010 .init = akita_init,
1011 .ram_require = SPITZ_RAM + SPITZ_ROM + PXA2XX_INTERNAL_SIZE + RAMSIZE_FIXED,
1014 QEMUMachine spitzpda_machine = {
1015 .name = "spitz",
1016 .desc = "Spitz PDA (PXA270)",
1017 .init = spitz_init,
1018 .ram_require = SPITZ_RAM + SPITZ_ROM + PXA2XX_INTERNAL_SIZE + RAMSIZE_FIXED,
1021 QEMUMachine borzoipda_machine = {
1022 .name = "borzoi",
1023 .desc = "Borzoi PDA (PXA270)",
1024 .init = borzoi_init,
1025 .ram_require = SPITZ_RAM + SPITZ_ROM + PXA2XX_INTERNAL_SIZE + RAMSIZE_FIXED,
1028 QEMUMachine terrierpda_machine = {
1029 .name = "terrier",
1030 .desc = "Terrier PDA (PXA270)",
1031 .init = terrier_init,
1032 .ram_require = SPITZ_RAM + SPITZ_ROM + PXA2XX_INTERNAL_SIZE + RAMSIZE_FIXED,