Update irqs on reset and device load
[qemu/hppa.git] / hw / spitz.c
blobaa1487b20aba91c5f81ff0629da7b02e6b0aa45d
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 "ssi.h"
17 #include "flash.h"
18 #include "qemu-timer.h"
19 #include "devices.h"
20 #include "sharpsl.h"
21 #include "console.h"
22 #include "block.h"
23 #include "audio/audio.h"
24 #include "boards.h"
26 #undef REG_FMT
27 #if TARGET_PHYS_ADDR_BITS == 32
28 #define REG_FMT "0x%02x"
29 #else
30 #define REG_FMT "0x%02lx"
31 #endif
33 /* Spitz Flash */
34 #define FLASH_BASE 0x0c000000
35 #define FLASH_ECCLPLB 0x00 /* Line parity 7 - 0 bit */
36 #define FLASH_ECCLPUB 0x04 /* Line parity 15 - 8 bit */
37 #define FLASH_ECCCP 0x08 /* Column parity 5 - 0 bit */
38 #define FLASH_ECCCNTR 0x0c /* ECC byte counter */
39 #define FLASH_ECCCLRR 0x10 /* Clear ECC */
40 #define FLASH_FLASHIO 0x14 /* Flash I/O */
41 #define FLASH_FLASHCTL 0x18 /* Flash Control */
43 #define FLASHCTL_CE0 (1 << 0)
44 #define FLASHCTL_CLE (1 << 1)
45 #define FLASHCTL_ALE (1 << 2)
46 #define FLASHCTL_WP (1 << 3)
47 #define FLASHCTL_CE1 (1 << 4)
48 #define FLASHCTL_RYBY (1 << 5)
49 #define FLASHCTL_NCE (FLASHCTL_CE0 | FLASHCTL_CE1)
51 typedef struct {
52 NANDFlashState *nand;
53 uint8_t ctl;
54 ECCState ecc;
55 } SLNANDState;
57 static uint32_t sl_readb(void *opaque, target_phys_addr_t addr)
59 SLNANDState *s = (SLNANDState *) opaque;
60 int ryby;
62 switch (addr) {
63 #define BSHR(byte, from, to) ((s->ecc.lp[byte] >> (from - to)) & (1 << to))
64 case FLASH_ECCLPLB:
65 return BSHR(0, 4, 0) | BSHR(0, 5, 2) | BSHR(0, 6, 4) | BSHR(0, 7, 6) |
66 BSHR(1, 4, 1) | BSHR(1, 5, 3) | BSHR(1, 6, 5) | BSHR(1, 7, 7);
68 #define BSHL(byte, from, to) ((s->ecc.lp[byte] << (to - from)) & (1 << to))
69 case FLASH_ECCLPUB:
70 return BSHL(0, 0, 0) | BSHL(0, 1, 2) | BSHL(0, 2, 4) | BSHL(0, 3, 6) |
71 BSHL(1, 0, 1) | BSHL(1, 1, 3) | BSHL(1, 2, 5) | BSHL(1, 3, 7);
73 case FLASH_ECCCP:
74 return s->ecc.cp;
76 case FLASH_ECCCNTR:
77 return s->ecc.count & 0xff;
79 case FLASH_FLASHCTL:
80 nand_getpins(s->nand, &ryby);
81 if (ryby)
82 return s->ctl | FLASHCTL_RYBY;
83 else
84 return s->ctl;
86 case FLASH_FLASHIO:
87 return ecc_digest(&s->ecc, nand_getio(s->nand));
89 default:
90 zaurus_printf("Bad register offset " REG_FMT "\n", addr);
92 return 0;
95 static uint32_t sl_readl(void *opaque, target_phys_addr_t addr)
97 SLNANDState *s = (SLNANDState *) opaque;
99 if (addr == FLASH_FLASHIO)
100 return ecc_digest(&s->ecc, nand_getio(s->nand)) |
101 (ecc_digest(&s->ecc, nand_getio(s->nand)) << 16);
103 return sl_readb(opaque, addr);
106 static void sl_writeb(void *opaque, target_phys_addr_t addr,
107 uint32_t value)
109 SLNANDState *s = (SLNANDState *) opaque;
111 switch (addr) {
112 case FLASH_ECCCLRR:
113 /* Value is ignored. */
114 ecc_reset(&s->ecc);
115 break;
117 case FLASH_FLASHCTL:
118 s->ctl = value & 0xff & ~FLASHCTL_RYBY;
119 nand_setpins(s->nand,
120 s->ctl & FLASHCTL_CLE,
121 s->ctl & FLASHCTL_ALE,
122 s->ctl & FLASHCTL_NCE,
123 s->ctl & FLASHCTL_WP,
125 break;
127 case FLASH_FLASHIO:
128 nand_setio(s->nand, ecc_digest(&s->ecc, value & 0xff));
129 break;
131 default:
132 zaurus_printf("Bad register offset " REG_FMT "\n", addr);
136 static void sl_save(QEMUFile *f, void *opaque)
138 SLNANDState *s = (SLNANDState *) opaque;
140 qemu_put_8s(f, &s->ctl);
141 ecc_put(f, &s->ecc);
144 static int sl_load(QEMUFile *f, void *opaque, int version_id)
146 SLNANDState *s = (SLNANDState *) opaque;
148 qemu_get_8s(f, &s->ctl);
149 ecc_get(f, &s->ecc);
151 return 0;
154 enum {
155 FLASH_128M,
156 FLASH_1024M,
159 static void sl_flash_register(PXA2xxState *cpu, int size)
161 int iomemtype;
162 SLNANDState *s;
163 CPUReadMemoryFunc *sl_readfn[] = {
164 sl_readb,
165 sl_readb,
166 sl_readl,
168 CPUWriteMemoryFunc *sl_writefn[] = {
169 sl_writeb,
170 sl_writeb,
171 sl_writeb,
174 s = (SLNANDState *) qemu_mallocz(sizeof(SLNANDState));
175 s->ctl = 0;
176 if (size == FLASH_128M)
177 s->nand = nand_init(NAND_MFR_SAMSUNG, 0x73);
178 else if (size == FLASH_1024M)
179 s->nand = nand_init(NAND_MFR_SAMSUNG, 0xf1);
181 iomemtype = cpu_register_io_memory(0, sl_readfn,
182 sl_writefn, s);
183 cpu_register_physical_memory(FLASH_BASE, 0x40, iomemtype);
185 register_savevm("sl_flash", 0, 0, sl_save, sl_load, s);
188 /* Spitz Keyboard */
190 #define SPITZ_KEY_STROBE_NUM 11
191 #define SPITZ_KEY_SENSE_NUM 7
193 static const int spitz_gpio_key_sense[SPITZ_KEY_SENSE_NUM] = {
194 12, 17, 91, 34, 36, 38, 39
197 static const int spitz_gpio_key_strobe[SPITZ_KEY_STROBE_NUM] = {
198 88, 23, 24, 25, 26, 27, 52, 103, 107, 108, 114
201 /* Eighth additional row maps the special keys */
202 static int spitz_keymap[SPITZ_KEY_SENSE_NUM + 1][SPITZ_KEY_STROBE_NUM] = {
203 { 0x1d, 0x02, 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0e, 0x3f, 0x40 },
204 { -1 , 0x03, 0x05, 0x13, 0x15, 0x09, 0x17, 0x18, 0x19, 0x41, 0x42 },
205 { 0x0f, 0x10, 0x12, 0x14, 0x22, 0x16, 0x24, 0x25, -1 , -1 , -1 },
206 { 0x3c, 0x11, 0x1f, 0x21, 0x2f, 0x23, 0x32, 0x26, -1 , 0x36, -1 },
207 { 0x3b, 0x1e, 0x20, 0x2e, 0x30, 0x31, 0x34, -1 , 0x1c, 0x2a, -1 },
208 { 0x44, 0x2c, 0x2d, 0x0c, 0x39, 0x33, -1 , 0x48, -1 , -1 , 0x38 },
209 { 0x37, 0x3d, -1 , 0x45, 0x57, 0x58, 0x4b, 0x50, 0x4d, -1 , -1 },
210 { 0x52, 0x43, 0x01, 0x47, 0x49, -1 , -1 , -1 , -1 , -1 , -1 },
213 #define SPITZ_GPIO_AK_INT 13 /* Remote control */
214 #define SPITZ_GPIO_SYNC 16 /* Sync button */
215 #define SPITZ_GPIO_ON_KEY 95 /* Power button */
216 #define SPITZ_GPIO_SWA 97 /* Lid */
217 #define SPITZ_GPIO_SWB 96 /* Tablet mode */
219 /* The special buttons are mapped to unused keys */
220 static const int spitz_gpiomap[5] = {
221 SPITZ_GPIO_AK_INT, SPITZ_GPIO_SYNC, SPITZ_GPIO_ON_KEY,
222 SPITZ_GPIO_SWA, SPITZ_GPIO_SWB,
224 static int spitz_gpio_invert[5] = { 0, 0, 0, 0, 0, };
226 typedef struct {
227 qemu_irq sense[SPITZ_KEY_SENSE_NUM];
228 qemu_irq *strobe;
229 qemu_irq gpiomap[5];
230 int keymap[0x80];
231 uint16_t keyrow[SPITZ_KEY_SENSE_NUM];
232 uint16_t strobe_state;
233 uint16_t sense_state;
235 uint16_t pre_map[0x100];
236 uint16_t modifiers;
237 uint16_t imodifiers;
238 uint8_t fifo[16];
239 int fifopos, fifolen;
240 QEMUTimer *kbdtimer;
241 } SpitzKeyboardState;
243 static void spitz_keyboard_sense_update(SpitzKeyboardState *s)
245 int i;
246 uint16_t strobe, sense = 0;
247 for (i = 0; i < SPITZ_KEY_SENSE_NUM; i ++) {
248 strobe = s->keyrow[i] & s->strobe_state;
249 if (strobe) {
250 sense |= 1 << i;
251 if (!(s->sense_state & (1 << i)))
252 qemu_irq_raise(s->sense[i]);
253 } else if (s->sense_state & (1 << i))
254 qemu_irq_lower(s->sense[i]);
257 s->sense_state = sense;
260 static void spitz_keyboard_strobe(void *opaque, int line, int level)
262 SpitzKeyboardState *s = (SpitzKeyboardState *) opaque;
264 if (level)
265 s->strobe_state |= 1 << line;
266 else
267 s->strobe_state &= ~(1 << line);
268 spitz_keyboard_sense_update(s);
271 static void spitz_keyboard_keydown(SpitzKeyboardState *s, int keycode)
273 int spitz_keycode = s->keymap[keycode & 0x7f];
274 if (spitz_keycode == -1)
275 return;
277 /* Handle the additional keys */
278 if ((spitz_keycode >> 4) == SPITZ_KEY_SENSE_NUM) {
279 qemu_set_irq(s->gpiomap[spitz_keycode & 0xf], (keycode < 0x80) ^
280 spitz_gpio_invert[spitz_keycode & 0xf]);
281 return;
284 if (keycode & 0x80)
285 s->keyrow[spitz_keycode >> 4] &= ~(1 << (spitz_keycode & 0xf));
286 else
287 s->keyrow[spitz_keycode >> 4] |= 1 << (spitz_keycode & 0xf);
289 spitz_keyboard_sense_update(s);
292 #define SHIFT (1 << 7)
293 #define CTRL (1 << 8)
294 #define FN (1 << 9)
296 #define QUEUE_KEY(c) s->fifo[(s->fifopos + s->fifolen ++) & 0xf] = c
298 static void spitz_keyboard_handler(SpitzKeyboardState *s, int keycode)
300 uint16_t code;
301 int mapcode;
302 switch (keycode) {
303 case 0x2a: /* Left Shift */
304 s->modifiers |= 1;
305 break;
306 case 0xaa:
307 s->modifiers &= ~1;
308 break;
309 case 0x36: /* Right Shift */
310 s->modifiers |= 2;
311 break;
312 case 0xb6:
313 s->modifiers &= ~2;
314 break;
315 case 0x1d: /* Control */
316 s->modifiers |= 4;
317 break;
318 case 0x9d:
319 s->modifiers &= ~4;
320 break;
321 case 0x38: /* Alt */
322 s->modifiers |= 8;
323 break;
324 case 0xb8:
325 s->modifiers &= ~8;
326 break;
329 code = s->pre_map[mapcode = ((s->modifiers & 3) ?
330 (keycode | SHIFT) :
331 (keycode & ~SHIFT))];
333 if (code != mapcode) {
334 #if 0
335 if ((code & SHIFT) && !(s->modifiers & 1))
336 QUEUE_KEY(0x2a | (keycode & 0x80));
337 if ((code & CTRL ) && !(s->modifiers & 4))
338 QUEUE_KEY(0x1d | (keycode & 0x80));
339 if ((code & FN ) && !(s->modifiers & 8))
340 QUEUE_KEY(0x38 | (keycode & 0x80));
341 if ((code & FN ) && (s->modifiers & 1))
342 QUEUE_KEY(0x2a | (~keycode & 0x80));
343 if ((code & FN ) && (s->modifiers & 2))
344 QUEUE_KEY(0x36 | (~keycode & 0x80));
345 #else
346 if (keycode & 0x80) {
347 if ((s->imodifiers & 1 ) && !(s->modifiers & 1))
348 QUEUE_KEY(0x2a | 0x80);
349 if ((s->imodifiers & 4 ) && !(s->modifiers & 4))
350 QUEUE_KEY(0x1d | 0x80);
351 if ((s->imodifiers & 8 ) && !(s->modifiers & 8))
352 QUEUE_KEY(0x38 | 0x80);
353 if ((s->imodifiers & 0x10) && (s->modifiers & 1))
354 QUEUE_KEY(0x2a);
355 if ((s->imodifiers & 0x20) && (s->modifiers & 2))
356 QUEUE_KEY(0x36);
357 s->imodifiers = 0;
358 } else {
359 if ((code & SHIFT) && !((s->modifiers | s->imodifiers) & 1)) {
360 QUEUE_KEY(0x2a);
361 s->imodifiers |= 1;
363 if ((code & CTRL ) && !((s->modifiers | s->imodifiers) & 4)) {
364 QUEUE_KEY(0x1d);
365 s->imodifiers |= 4;
367 if ((code & FN ) && !((s->modifiers | s->imodifiers) & 8)) {
368 QUEUE_KEY(0x38);
369 s->imodifiers |= 8;
371 if ((code & FN ) && (s->modifiers & 1) &&
372 !(s->imodifiers & 0x10)) {
373 QUEUE_KEY(0x2a | 0x80);
374 s->imodifiers |= 0x10;
376 if ((code & FN ) && (s->modifiers & 2) &&
377 !(s->imodifiers & 0x20)) {
378 QUEUE_KEY(0x36 | 0x80);
379 s->imodifiers |= 0x20;
382 #endif
385 QUEUE_KEY((code & 0x7f) | (keycode & 0x80));
388 static void spitz_keyboard_tick(void *opaque)
390 SpitzKeyboardState *s = (SpitzKeyboardState *) opaque;
392 if (s->fifolen) {
393 spitz_keyboard_keydown(s, s->fifo[s->fifopos ++]);
394 s->fifolen --;
395 if (s->fifopos >= 16)
396 s->fifopos = 0;
399 qemu_mod_timer(s->kbdtimer, qemu_get_clock(vm_clock) + ticks_per_sec / 32);
402 static void spitz_keyboard_pre_map(SpitzKeyboardState *s)
404 int i;
405 for (i = 0; i < 0x100; i ++)
406 s->pre_map[i] = i;
407 s->pre_map[0x02 | SHIFT ] = 0x02 | SHIFT; /* exclam */
408 s->pre_map[0x28 | SHIFT ] = 0x03 | SHIFT; /* quotedbl */
409 s->pre_map[0x04 | SHIFT ] = 0x04 | SHIFT; /* numbersign */
410 s->pre_map[0x05 | SHIFT ] = 0x05 | SHIFT; /* dollar */
411 s->pre_map[0x06 | SHIFT ] = 0x06 | SHIFT; /* percent */
412 s->pre_map[0x08 | SHIFT ] = 0x07 | SHIFT; /* ampersand */
413 s->pre_map[0x28 ] = 0x08 | SHIFT; /* apostrophe */
414 s->pre_map[0x0a | SHIFT ] = 0x09 | SHIFT; /* parenleft */
415 s->pre_map[0x0b | SHIFT ] = 0x0a | SHIFT; /* parenright */
416 s->pre_map[0x29 | SHIFT ] = 0x0b | SHIFT; /* asciitilde */
417 s->pre_map[0x03 | SHIFT ] = 0x0c | SHIFT; /* at */
418 s->pre_map[0xd3 ] = 0x0e | FN; /* Delete */
419 s->pre_map[0x3a ] = 0x0f | FN; /* Caps_Lock */
420 s->pre_map[0x07 | SHIFT ] = 0x11 | FN; /* asciicircum */
421 s->pre_map[0x0d ] = 0x12 | FN; /* equal */
422 s->pre_map[0x0d | SHIFT ] = 0x13 | FN; /* plus */
423 s->pre_map[0x1a ] = 0x14 | FN; /* bracketleft */
424 s->pre_map[0x1b ] = 0x15 | FN; /* bracketright */
425 s->pre_map[0x1a | SHIFT ] = 0x16 | FN; /* braceleft */
426 s->pre_map[0x1b | SHIFT ] = 0x17 | FN; /* braceright */
427 s->pre_map[0x27 ] = 0x22 | FN; /* semicolon */
428 s->pre_map[0x27 | SHIFT ] = 0x23 | FN; /* colon */
429 s->pre_map[0x09 | SHIFT ] = 0x24 | FN; /* asterisk */
430 s->pre_map[0x2b ] = 0x25 | FN; /* backslash */
431 s->pre_map[0x2b | SHIFT ] = 0x26 | FN; /* bar */
432 s->pre_map[0x0c | SHIFT ] = 0x30 | FN; /* underscore */
433 s->pre_map[0x33 | SHIFT ] = 0x33 | FN; /* less */
434 s->pre_map[0x35 ] = 0x33 | SHIFT; /* slash */
435 s->pre_map[0x34 | SHIFT ] = 0x34 | FN; /* greater */
436 s->pre_map[0x35 | SHIFT ] = 0x34 | SHIFT; /* question */
437 s->pre_map[0x49 ] = 0x48 | FN; /* Page_Up */
438 s->pre_map[0x51 ] = 0x50 | FN; /* Page_Down */
440 s->modifiers = 0;
441 s->imodifiers = 0;
442 s->fifopos = 0;
443 s->fifolen = 0;
444 s->kbdtimer = qemu_new_timer(vm_clock, spitz_keyboard_tick, s);
445 spitz_keyboard_tick(s);
448 #undef SHIFT
449 #undef CTRL
450 #undef FN
452 static void spitz_keyboard_save(QEMUFile *f, void *opaque)
454 SpitzKeyboardState *s = (SpitzKeyboardState *) opaque;
455 int i;
457 qemu_put_be16s(f, &s->sense_state);
458 qemu_put_be16s(f, &s->strobe_state);
459 for (i = 0; i < 5; i ++)
460 qemu_put_byte(f, spitz_gpio_invert[i]);
463 static int spitz_keyboard_load(QEMUFile *f, void *opaque, int version_id)
465 SpitzKeyboardState *s = (SpitzKeyboardState *) opaque;
466 int i;
468 qemu_get_be16s(f, &s->sense_state);
469 qemu_get_be16s(f, &s->strobe_state);
470 for (i = 0; i < 5; i ++)
471 spitz_gpio_invert[i] = qemu_get_byte(f);
473 /* Release all pressed keys */
474 memset(s->keyrow, 0, sizeof(s->keyrow));
475 spitz_keyboard_sense_update(s);
476 s->modifiers = 0;
477 s->imodifiers = 0;
478 s->fifopos = 0;
479 s->fifolen = 0;
481 return 0;
484 static void spitz_keyboard_register(PXA2xxState *cpu)
486 int i, j;
487 SpitzKeyboardState *s;
489 s = (SpitzKeyboardState *)
490 qemu_mallocz(sizeof(SpitzKeyboardState));
491 memset(s, 0, sizeof(SpitzKeyboardState));
493 for (i = 0; i < 0x80; i ++)
494 s->keymap[i] = -1;
495 for (i = 0; i < SPITZ_KEY_SENSE_NUM + 1; i ++)
496 for (j = 0; j < SPITZ_KEY_STROBE_NUM; j ++)
497 if (spitz_keymap[i][j] != -1)
498 s->keymap[spitz_keymap[i][j]] = (i << 4) | j;
500 for (i = 0; i < SPITZ_KEY_SENSE_NUM; i ++)
501 s->sense[i] = pxa2xx_gpio_in_get(cpu->gpio)[spitz_gpio_key_sense[i]];
503 for (i = 0; i < 5; i ++)
504 s->gpiomap[i] = pxa2xx_gpio_in_get(cpu->gpio)[spitz_gpiomap[i]];
506 s->strobe = qemu_allocate_irqs(spitz_keyboard_strobe, s,
507 SPITZ_KEY_STROBE_NUM);
508 for (i = 0; i < SPITZ_KEY_STROBE_NUM; i ++)
509 pxa2xx_gpio_out_set(cpu->gpio, spitz_gpio_key_strobe[i], s->strobe[i]);
511 spitz_keyboard_pre_map(s);
512 qemu_add_kbd_event_handler((QEMUPutKBDEvent *) spitz_keyboard_handler, s);
514 register_savevm("spitz_keyboard", 0, 0,
515 spitz_keyboard_save, spitz_keyboard_load, s);
518 /* LCD backlight controller */
520 #define LCDTG_RESCTL 0x00
521 #define LCDTG_PHACTRL 0x01
522 #define LCDTG_DUTYCTRL 0x02
523 #define LCDTG_POWERREG0 0x03
524 #define LCDTG_POWERREG1 0x04
525 #define LCDTG_GPOR3 0x05
526 #define LCDTG_PICTRL 0x06
527 #define LCDTG_POLCTRL 0x07
529 typedef struct {
530 SSISlave ssidev;
531 int bl_intensity;
532 int bl_power;
533 } SpitzLCDTG;
535 static void spitz_bl_update(SpitzLCDTG *s)
537 if (s->bl_power && s->bl_intensity)
538 zaurus_printf("LCD Backlight now at %i/63\n", s->bl_intensity);
539 else
540 zaurus_printf("LCD Backlight now off\n");
543 /* FIXME: Implement GPIO properly and remove this hack. */
544 static SpitzLCDTG *spitz_lcdtg;
546 static inline void spitz_bl_bit5(void *opaque, int line, int level)
548 SpitzLCDTG *s = spitz_lcdtg;
549 int prev = s->bl_intensity;
551 if (level)
552 s->bl_intensity &= ~0x20;
553 else
554 s->bl_intensity |= 0x20;
556 if (s->bl_power && prev != s->bl_intensity)
557 spitz_bl_update(s);
560 static inline void spitz_bl_power(void *opaque, int line, int level)
562 SpitzLCDTG *s = spitz_lcdtg;
563 s->bl_power = !!level;
564 spitz_bl_update(s);
567 static uint32_t spitz_lcdtg_transfer(SSISlave *dev, uint32_t value)
569 SpitzLCDTG *s = FROM_SSI_SLAVE(SpitzLCDTG, dev);
570 int addr;
571 addr = value >> 5;
572 value &= 0x1f;
574 switch (addr) {
575 case LCDTG_RESCTL:
576 if (value)
577 zaurus_printf("LCD in QVGA mode\n");
578 else
579 zaurus_printf("LCD in VGA mode\n");
580 break;
582 case LCDTG_DUTYCTRL:
583 s->bl_intensity &= ~0x1f;
584 s->bl_intensity |= value;
585 if (s->bl_power)
586 spitz_bl_update(s);
587 break;
589 case LCDTG_POWERREG0:
590 /* Set common voltage to M62332FP */
591 break;
593 return 0;
596 static void spitz_lcdtg_save(QEMUFile *f, void *opaque)
598 SpitzLCDTG *s = (SpitzLCDTG *)opaque;
599 qemu_put_be32(f, s->bl_intensity);
600 qemu_put_be32(f, s->bl_power);
603 static int spitz_lcdtg_load(QEMUFile *f, void *opaque, int version_id)
605 SpitzLCDTG *s = (SpitzLCDTG *)opaque;
606 s->bl_intensity = qemu_get_be32(f);
607 s->bl_power = qemu_get_be32(f);
608 return 0;
611 static void spitz_lcdtg_init(SSISlave *dev)
613 SpitzLCDTG *s = FROM_SSI_SLAVE(SpitzLCDTG, dev);
615 spitz_lcdtg = s;
616 s->bl_power = 0;
617 s->bl_intensity = 0x20;
619 register_savevm("spitz-lcdtg", -1, 1,
620 spitz_lcdtg_save, spitz_lcdtg_load, s);
623 /* SSP devices */
625 #define CORGI_SSP_PORT 2
627 #define SPITZ_GPIO_LCDCON_CS 53
628 #define SPITZ_GPIO_ADS7846_CS 14
629 #define SPITZ_GPIO_MAX1111_CS 20
630 #define SPITZ_GPIO_TP_INT 11
632 static DeviceState *max1111;
634 /* "Demux" the signal based on current chipselect */
635 typedef struct {
636 SSISlave ssidev;
637 SSIBus *bus[3];
638 int enable[3];
639 } CorgiSSPState;
641 static uint32_t corgi_ssp_transfer(SSISlave *dev, uint32_t value)
643 CorgiSSPState *s = FROM_SSI_SLAVE(CorgiSSPState, dev);
644 int i;
646 for (i = 0; i < 3; i++) {
647 if (s->enable[i]) {
648 return ssi_transfer(s->bus[i], value);
651 return 0;
654 static void corgi_ssp_gpio_cs(void *opaque, int line, int level)
656 CorgiSSPState *s = (CorgiSSPState *)opaque;
657 assert(line >= 0 && line < 3);
658 s->enable[line] = !level;
661 #define MAX1111_BATT_VOLT 1
662 #define MAX1111_BATT_TEMP 2
663 #define MAX1111_ACIN_VOLT 3
665 #define SPITZ_BATTERY_TEMP 0xe0 /* About 2.9V */
666 #define SPITZ_BATTERY_VOLT 0xd0 /* About 4.0V */
667 #define SPITZ_CHARGEON_ACIN 0x80 /* About 5.0V */
669 static void spitz_adc_temp_on(void *opaque, int line, int level)
671 if (!max1111)
672 return;
674 if (level)
675 max111x_set_input(max1111, MAX1111_BATT_TEMP, SPITZ_BATTERY_TEMP);
676 else
677 max111x_set_input(max1111, MAX1111_BATT_TEMP, 0);
680 static void spitz_ssp_save(QEMUFile *f, void *opaque)
682 CorgiSSPState *s = (CorgiSSPState *)opaque;
683 int i;
685 for (i = 0; i < 3; i++) {
686 qemu_put_be32(f, s->enable[i]);
690 static int spitz_ssp_load(QEMUFile *f, void *opaque, int version_id)
692 CorgiSSPState *s = (CorgiSSPState *)opaque;
693 int i;
695 if (version_id != 1) {
696 return -EINVAL;
698 for (i = 0; i < 3; i++) {
699 s->enable[i] = qemu_get_be32(f);
701 return 0;
704 static void corgi_ssp_init(SSISlave *dev)
706 CorgiSSPState *s = FROM_SSI_SLAVE(CorgiSSPState, dev);
708 qdev_init_gpio_in(&dev->qdev, corgi_ssp_gpio_cs, 3);
709 s->bus[0] = ssi_create_bus(&dev->qdev, "ssi0");
710 s->bus[1] = ssi_create_bus(&dev->qdev, "ssi1");
711 s->bus[2] = ssi_create_bus(&dev->qdev, "ssi2");
713 register_savevm("spitz_ssp", -1, 1, spitz_ssp_save, spitz_ssp_load, s);
716 static void spitz_ssp_attach(PXA2xxState *cpu)
718 DeviceState *mux;
719 DeviceState *dev;
720 void *bus;
722 mux = ssi_create_slave(cpu->ssp[CORGI_SSP_PORT - 1], "corgi-ssp");
724 bus = qdev_get_child_bus(mux, "ssi0");
725 dev = ssi_create_slave(bus, "spitz-lcdtg");
727 bus = qdev_get_child_bus(mux, "ssi1");
728 dev = ssi_create_slave(bus, "ads7846");
729 qdev_connect_gpio_out(dev, 0,
730 pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_TP_INT]);
732 bus = qdev_get_child_bus(mux, "ssi2");
733 max1111 = ssi_create_slave(bus, "max1111");
734 max111x_set_input(max1111, MAX1111_BATT_VOLT, SPITZ_BATTERY_VOLT);
735 max111x_set_input(max1111, MAX1111_BATT_TEMP, 0);
736 max111x_set_input(max1111, MAX1111_ACIN_VOLT, SPITZ_CHARGEON_ACIN);
738 pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_LCDCON_CS,
739 qdev_get_gpio_in(mux, 0));
740 pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_ADS7846_CS,
741 qdev_get_gpio_in(mux, 1));
742 pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_MAX1111_CS,
743 qdev_get_gpio_in(mux, 2));
746 /* CF Microdrive */
748 static void spitz_microdrive_attach(PXA2xxState *cpu, int slot)
750 PCMCIACardState *md;
751 int index;
752 BlockDriverState *bs;
754 index = drive_get_index(IF_IDE, 0, 0);
755 if (index == -1)
756 return;
757 bs = drives_table[index].bdrv;
758 if (bdrv_is_inserted(bs) && !bdrv_is_removable(bs)) {
759 md = dscm1xxxx_init(bs);
760 pxa2xx_pcmcia_attach(cpu->pcmcia[slot], md);
764 /* Wm8750 and Max7310 on I2C */
766 #define AKITA_MAX_ADDR 0x18
767 #define SPITZ_WM_ADDRL 0x1b
768 #define SPITZ_WM_ADDRH 0x1a
770 #define SPITZ_GPIO_WM 5
772 #ifdef HAS_AUDIO
773 static void spitz_wm8750_addr(void *opaque, int line, int level)
775 i2c_slave *wm = (i2c_slave *) opaque;
776 if (level)
777 i2c_set_slave_address(wm, SPITZ_WM_ADDRH);
778 else
779 i2c_set_slave_address(wm, SPITZ_WM_ADDRL);
781 #endif
783 static void spitz_i2c_setup(PXA2xxState *cpu)
785 /* Attach the CPU on one end of our I2C bus. */
786 i2c_bus *bus = pxa2xx_i2c_bus(cpu->i2c[0]);
788 #ifdef HAS_AUDIO
789 DeviceState *wm;
791 /* Attach a WM8750 to the bus */
792 wm = i2c_create_slave(bus, "wm8750", 0);
794 spitz_wm8750_addr(wm, 0, 0);
795 pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_WM,
796 qemu_allocate_irqs(spitz_wm8750_addr, wm, 1)[0]);
797 /* .. and to the sound interface. */
798 cpu->i2s->opaque = wm;
799 cpu->i2s->codec_out = wm8750_dac_dat;
800 cpu->i2s->codec_in = wm8750_adc_dat;
801 wm8750_data_req_set(wm, cpu->i2s->data_req, cpu->i2s);
802 #endif
805 static void spitz_akita_i2c_setup(PXA2xxState *cpu)
807 /* Attach a Max7310 to Akita I2C bus. */
808 i2c_create_slave(pxa2xx_i2c_bus(cpu->i2c[0]), "max7310",
809 AKITA_MAX_ADDR);
812 /* Other peripherals */
814 static void spitz_out_switch(void *opaque, int line, int level)
816 switch (line) {
817 case 0:
818 zaurus_printf("Charging %s.\n", level ? "off" : "on");
819 break;
820 case 1:
821 zaurus_printf("Discharging %s.\n", level ? "on" : "off");
822 break;
823 case 2:
824 zaurus_printf("Green LED %s.\n", level ? "on" : "off");
825 break;
826 case 3:
827 zaurus_printf("Orange LED %s.\n", level ? "on" : "off");
828 break;
829 case 4:
830 spitz_bl_bit5(opaque, line, level);
831 break;
832 case 5:
833 spitz_bl_power(opaque, line, level);
834 break;
835 case 6:
836 spitz_adc_temp_on(opaque, line, level);
837 break;
841 #define SPITZ_SCP_LED_GREEN 1
842 #define SPITZ_SCP_JK_B 2
843 #define SPITZ_SCP_CHRG_ON 3
844 #define SPITZ_SCP_MUTE_L 4
845 #define SPITZ_SCP_MUTE_R 5
846 #define SPITZ_SCP_CF_POWER 6
847 #define SPITZ_SCP_LED_ORANGE 7
848 #define SPITZ_SCP_JK_A 8
849 #define SPITZ_SCP_ADC_TEMP_ON 9
850 #define SPITZ_SCP2_IR_ON 1
851 #define SPITZ_SCP2_AKIN_PULLUP 2
852 #define SPITZ_SCP2_BACKLIGHT_CONT 7
853 #define SPITZ_SCP2_BACKLIGHT_ON 8
854 #define SPITZ_SCP2_MIC_BIAS 9
856 static void spitz_scoop_gpio_setup(PXA2xxState *cpu,
857 ScoopInfo *scp0, ScoopInfo *scp1)
859 qemu_irq *outsignals = qemu_allocate_irqs(spitz_out_switch, cpu, 8);
861 scoop_gpio_out_set(scp0, SPITZ_SCP_CHRG_ON, outsignals[0]);
862 scoop_gpio_out_set(scp0, SPITZ_SCP_JK_B, outsignals[1]);
863 scoop_gpio_out_set(scp0, SPITZ_SCP_LED_GREEN, outsignals[2]);
864 scoop_gpio_out_set(scp0, SPITZ_SCP_LED_ORANGE, outsignals[3]);
866 if (scp1) {
867 scoop_gpio_out_set(scp1, SPITZ_SCP2_BACKLIGHT_CONT, outsignals[4]);
868 scoop_gpio_out_set(scp1, SPITZ_SCP2_BACKLIGHT_ON, outsignals[5]);
871 scoop_gpio_out_set(scp0, SPITZ_SCP_ADC_TEMP_ON, outsignals[6]);
874 #define SPITZ_GPIO_HSYNC 22
875 #define SPITZ_GPIO_SD_DETECT 9
876 #define SPITZ_GPIO_SD_WP 81
877 #define SPITZ_GPIO_ON_RESET 89
878 #define SPITZ_GPIO_BAT_COVER 90
879 #define SPITZ_GPIO_CF1_IRQ 105
880 #define SPITZ_GPIO_CF1_CD 94
881 #define SPITZ_GPIO_CF2_IRQ 106
882 #define SPITZ_GPIO_CF2_CD 93
884 static int spitz_hsync;
886 static void spitz_lcd_hsync_handler(void *opaque, int line, int level)
888 PXA2xxState *cpu = (PXA2xxState *) opaque;
889 qemu_set_irq(pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_HSYNC], spitz_hsync);
890 spitz_hsync ^= 1;
893 static void spitz_gpio_setup(PXA2xxState *cpu, int slots)
895 qemu_irq lcd_hsync;
897 * Bad hack: We toggle the LCD hsync GPIO on every GPIO status
898 * read to satisfy broken guests that poll-wait for hsync.
899 * Simulating a real hsync event would be less practical and
900 * wouldn't guarantee that a guest ever exits the loop.
902 spitz_hsync = 0;
903 lcd_hsync = qemu_allocate_irqs(spitz_lcd_hsync_handler, cpu, 1)[0];
904 pxa2xx_gpio_read_notifier(cpu->gpio, lcd_hsync);
905 pxa2xx_lcd_vsync_notifier(cpu->lcd, lcd_hsync);
907 /* MMC/SD host */
908 pxa2xx_mmci_handlers(cpu->mmc,
909 pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_SD_WP],
910 pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_SD_DETECT]);
912 /* Battery lock always closed */
913 qemu_irq_raise(pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_BAT_COVER]);
915 /* Handle reset */
916 pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_ON_RESET, cpu->reset);
918 /* PCMCIA signals: card's IRQ and Card-Detect */
919 if (slots >= 1)
920 pxa2xx_pcmcia_set_irq_cb(cpu->pcmcia[0],
921 pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_CF1_IRQ],
922 pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_CF1_CD]);
923 if (slots >= 2)
924 pxa2xx_pcmcia_set_irq_cb(cpu->pcmcia[1],
925 pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_CF2_IRQ],
926 pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_CF2_CD]);
928 /* Initialise the screen rotation related signals */
929 spitz_gpio_invert[3] = 0; /* Always open */
930 if (graphic_rotate) { /* Tablet mode */
931 spitz_gpio_invert[4] = 0;
932 } else { /* Portrait mode */
933 spitz_gpio_invert[4] = 1;
935 qemu_set_irq(pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_SWA],
936 spitz_gpio_invert[3]);
937 qemu_set_irq(pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_SWB],
938 spitz_gpio_invert[4]);
941 /* Board init. */
942 enum spitz_model_e { spitz, akita, borzoi, terrier };
944 #define SPITZ_RAM 0x04000000
945 #define SPITZ_ROM 0x00800000
947 static struct arm_boot_info spitz_binfo = {
948 .loader_start = PXA2XX_SDRAM_BASE,
949 .ram_size = 0x04000000,
952 static void spitz_common_init(ram_addr_t ram_size,
953 const char *kernel_filename,
954 const char *kernel_cmdline, const char *initrd_filename,
955 const char *cpu_model, enum spitz_model_e model, int arm_id)
957 PXA2xxState *cpu;
958 ScoopInfo *scp0, *scp1 = NULL;
960 if (!cpu_model)
961 cpu_model = (model == terrier) ? "pxa270-c5" : "pxa270-c0";
963 /* Setup CPU & memory */
964 cpu = pxa270_init(spitz_binfo.ram_size, cpu_model);
966 sl_flash_register(cpu, (model == spitz) ? FLASH_128M : FLASH_1024M);
968 cpu_register_physical_memory(0, SPITZ_ROM,
969 qemu_ram_alloc(SPITZ_ROM) | IO_MEM_ROM);
971 /* Setup peripherals */
972 spitz_keyboard_register(cpu);
974 spitz_ssp_attach(cpu);
976 scp0 = scoop_init(cpu, 0, 0x10800000);
977 if (model != akita) {
978 scp1 = scoop_init(cpu, 1, 0x08800040);
981 spitz_scoop_gpio_setup(cpu, scp0, scp1);
983 spitz_gpio_setup(cpu, (model == akita) ? 1 : 2);
985 spitz_i2c_setup(cpu);
987 if (model == akita)
988 spitz_akita_i2c_setup(cpu);
990 if (model == terrier)
991 /* A 6.0 GB microdrive is permanently sitting in CF slot 1. */
992 spitz_microdrive_attach(cpu, 1);
993 else if (model != akita)
994 /* A 4.0 GB microdrive is permanently sitting in CF slot 0. */
995 spitz_microdrive_attach(cpu, 0);
997 /* Setup initial (reset) machine state */
998 cpu->env->regs[15] = spitz_binfo.loader_start;
1000 spitz_binfo.kernel_filename = kernel_filename;
1001 spitz_binfo.kernel_cmdline = kernel_cmdline;
1002 spitz_binfo.initrd_filename = initrd_filename;
1003 spitz_binfo.board_id = arm_id;
1004 arm_load_kernel(cpu->env, &spitz_binfo);
1005 sl_bootparam_write(SL_PXA_PARAM_BASE);
1008 static void spitz_init(ram_addr_t ram_size,
1009 const char *boot_device,
1010 const char *kernel_filename, const char *kernel_cmdline,
1011 const char *initrd_filename, const char *cpu_model)
1013 spitz_common_init(ram_size, kernel_filename,
1014 kernel_cmdline, initrd_filename, cpu_model, spitz, 0x2c9);
1017 static void borzoi_init(ram_addr_t ram_size,
1018 const char *boot_device,
1019 const char *kernel_filename, const char *kernel_cmdline,
1020 const char *initrd_filename, const char *cpu_model)
1022 spitz_common_init(ram_size, kernel_filename,
1023 kernel_cmdline, initrd_filename, cpu_model, borzoi, 0x33f);
1026 static void akita_init(ram_addr_t ram_size,
1027 const char *boot_device,
1028 const char *kernel_filename, const char *kernel_cmdline,
1029 const char *initrd_filename, const char *cpu_model)
1031 spitz_common_init(ram_size, kernel_filename,
1032 kernel_cmdline, initrd_filename, cpu_model, akita, 0x2e8);
1035 static void terrier_init(ram_addr_t ram_size,
1036 const char *boot_device,
1037 const char *kernel_filename, const char *kernel_cmdline,
1038 const char *initrd_filename, const char *cpu_model)
1040 spitz_common_init(ram_size, kernel_filename,
1041 kernel_cmdline, initrd_filename, cpu_model, terrier, 0x33f);
1044 QEMUMachine akitapda_machine = {
1045 .name = "akita",
1046 .desc = "Akita PDA (PXA270)",
1047 .init = akita_init,
1050 static QEMUMachine spitzpda_machine = {
1051 .name = "spitz",
1052 .desc = "Spitz PDA (PXA270)",
1053 .init = spitz_init,
1056 static QEMUMachine borzoipda_machine = {
1057 .name = "borzoi",
1058 .desc = "Borzoi PDA (PXA270)",
1059 .init = borzoi_init,
1062 static QEMUMachine terrierpda_machine = {
1063 .name = "terrier",
1064 .desc = "Terrier PDA (PXA270)",
1065 .init = terrier_init,
1068 static void spitz_machine_init(void)
1070 qemu_register_machine(&akitapda_machine);
1071 qemu_register_machine(&spitzpda_machine);
1072 qemu_register_machine(&borzoipda_machine);
1073 qemu_register_machine(&terrierpda_machine);
1076 machine_init(spitz_machine_init);
1078 static SSISlaveInfo corgi_ssp_info = {
1079 .init = corgi_ssp_init,
1080 .transfer = corgi_ssp_transfer
1083 static SSISlaveInfo spitz_lcdtg_info = {
1084 .init = spitz_lcdtg_init,
1085 .transfer = spitz_lcdtg_transfer
1088 static void spitz_register_devices(void)
1090 ssi_register_slave("corgi-ssp", sizeof(CorgiSSPState), &corgi_ssp_info);
1091 ssi_register_slave("spitz-lcdtg", sizeof(SpitzLCDTG), &spitz_lcdtg_info);
1094 device_init(spitz_register_devices)