qdev: move name+size into DeviceInfo (v2)
[qemu] / hw / spitz.c
1 /*
2  * PXA270-based Clamshell PDA platforms.
3  *
4  * Copyright (c) 2006 Openedhand Ltd.
5  * Written by Andrzej Zaborowski <balrog@zabor.org>
6  *
7  * This code is licensed under the GNU GPL v2.
8  */
9
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"
25
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
32
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 */
42
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)
50
51 typedef struct {
52     NANDFlashState *nand;
53     uint8_t ctl;
54     ECCState ecc;
55 } SLNANDState;
56
57 static uint32_t sl_readb(void *opaque, target_phys_addr_t addr)
58 {
59     SLNANDState *s = (SLNANDState *) opaque;
60     int ryby;
61
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);
67
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);
72
73     case FLASH_ECCCP:
74         return s->ecc.cp;
75
76     case FLASH_ECCCNTR:
77         return s->ecc.count & 0xff;
78
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;
85
86     case FLASH_FLASHIO:
87         return ecc_digest(&s->ecc, nand_getio(s->nand));
88
89     default:
90         zaurus_printf("Bad register offset " REG_FMT "\n", addr);
91     }
92     return 0;
93 }
94
95 static uint32_t sl_readl(void *opaque, target_phys_addr_t addr)
96 {
97     SLNANDState *s = (SLNANDState *) opaque;
98
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);
102
103     return sl_readb(opaque, addr);
104 }
105
106 static void sl_writeb(void *opaque, target_phys_addr_t addr,
107                 uint32_t value)
108 {
109     SLNANDState *s = (SLNANDState *) opaque;
110
111     switch (addr) {
112     case FLASH_ECCCLRR:
113         /* Value is ignored.  */
114         ecc_reset(&s->ecc);
115         break;
116
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,
124                         0);
125         break;
126
127     case FLASH_FLASHIO:
128         nand_setio(s->nand, ecc_digest(&s->ecc, value & 0xff));
129         break;
130
131     default:
132         zaurus_printf("Bad register offset " REG_FMT "\n", addr);
133     }
134 }
135
136 static void sl_save(QEMUFile *f, void *opaque)
137 {
138     SLNANDState *s = (SLNANDState *) opaque;
139
140     qemu_put_8s(f, &s->ctl);
141     ecc_put(f, &s->ecc);
142 }
143
144 static int sl_load(QEMUFile *f, void *opaque, int version_id)
145 {
146     SLNANDState *s = (SLNANDState *) opaque;
147
148     qemu_get_8s(f, &s->ctl);
149     ecc_get(f, &s->ecc);
150
151     return 0;
152 }
153
154 enum {
155     FLASH_128M,
156     FLASH_1024M,
157 };
158
159 static void sl_flash_register(PXA2xxState *cpu, int size)
160 {
161     int iomemtype;
162     SLNANDState *s;
163     CPUReadMemoryFunc *sl_readfn[] = {
164         sl_readb,
165         sl_readb,
166         sl_readl,
167     };
168     CPUWriteMemoryFunc *sl_writefn[] = {
169         sl_writeb,
170         sl_writeb,
171         sl_writeb,
172     };
173
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);
180
181     iomemtype = cpu_register_io_memory(0, sl_readfn,
182                     sl_writefn, s);
183     cpu_register_physical_memory(FLASH_BASE, 0x40, iomemtype);
184
185     register_savevm("sl_flash", 0, 0, sl_save, sl_load, s);
186 }
187
188 /* Spitz Keyboard */
189
190 #define SPITZ_KEY_STROBE_NUM    11
191 #define SPITZ_KEY_SENSE_NUM     7
192
193 static const int spitz_gpio_key_sense[SPITZ_KEY_SENSE_NUM] = {
194     12, 17, 91, 34, 36, 38, 39
195 };
196
197 static const int spitz_gpio_key_strobe[SPITZ_KEY_STROBE_NUM] = {
198     88, 23, 24, 25, 26, 27, 52, 103, 107, 108, 114
199 };
200
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  },
211 };
212
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 */
218
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,
223 };
224 static int spitz_gpio_invert[5] = { 0, 0, 0, 0, 0, };
225
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;
234
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;
242
243 static void spitz_keyboard_sense_update(SpitzKeyboardState *s)
244 {
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]);
255     }
256
257     s->sense_state = sense;
258 }
259
260 static void spitz_keyboard_strobe(void *opaque, int line, int level)
261 {
262     SpitzKeyboardState *s = (SpitzKeyboardState *) opaque;
263
264     if (level)
265         s->strobe_state |= 1 << line;
266     else
267         s->strobe_state &= ~(1 << line);
268     spitz_keyboard_sense_update(s);
269 }
270
271 static void spitz_keyboard_keydown(SpitzKeyboardState *s, int keycode)
272 {
273     int spitz_keycode = s->keymap[keycode & 0x7f];
274     if (spitz_keycode == -1)
275         return;
276
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;
282     }
283
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);
288
289     spitz_keyboard_sense_update(s);
290 }
291
292 #define SHIFT   (1 << 7)
293 #define CTRL    (1 << 8)
294 #define FN      (1 << 9)
295
296 #define QUEUE_KEY(c)    s->fifo[(s->fifopos + s->fifolen ++) & 0xf] = c
297
298 static void spitz_keyboard_handler(SpitzKeyboardState *s, int keycode)
299 {
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;
327     }
328
329     code = s->pre_map[mapcode = ((s->modifiers & 3) ?
330             (keycode | SHIFT) :
331             (keycode & ~SHIFT))];
332
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;
362             }
363             if ((code & CTRL ) && !((s->modifiers | s->imodifiers) & 4)) {
364                 QUEUE_KEY(0x1d);
365                 s->imodifiers |= 4;
366             }
367             if ((code & FN   ) && !((s->modifiers | s->imodifiers) & 8)) {
368                 QUEUE_KEY(0x38);
369                 s->imodifiers |= 8;
370             }
371             if ((code & FN   ) && (s->modifiers & 1) &&
372                             !(s->imodifiers & 0x10)) {
373                 QUEUE_KEY(0x2a | 0x80);
374                 s->imodifiers |= 0x10;
375             }
376             if ((code & FN   ) && (s->modifiers & 2) &&
377                             !(s->imodifiers & 0x20)) {
378                 QUEUE_KEY(0x36 | 0x80);
379                 s->imodifiers |= 0x20;
380             }
381         }
382 #endif
383     }
384
385     QUEUE_KEY((code & 0x7f) | (keycode & 0x80));
386 }
387
388 static void spitz_keyboard_tick(void *opaque)
389 {
390     SpitzKeyboardState *s = (SpitzKeyboardState *) opaque;
391
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;
397     }
398
399     qemu_mod_timer(s->kbdtimer, qemu_get_clock(vm_clock) + ticks_per_sec / 32);
400 }
401
402 static void spitz_keyboard_pre_map(SpitzKeyboardState *s)
403 {
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 */
439
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);
446 }
447
448 #undef SHIFT
449 #undef CTRL
450 #undef FN
451
452 static void spitz_keyboard_save(QEMUFile *f, void *opaque)
453 {
454     SpitzKeyboardState *s = (SpitzKeyboardState *) opaque;
455     int i;
456
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]);
461 }
462
463 static int spitz_keyboard_load(QEMUFile *f, void *opaque, int version_id)
464 {
465     SpitzKeyboardState *s = (SpitzKeyboardState *) opaque;
466     int i;
467
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);
472
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;
480
481     return 0;
482 }
483
484 static void spitz_keyboard_register(PXA2xxState *cpu)
485 {
486     int i, j;
487     SpitzKeyboardState *s;
488
489     s = (SpitzKeyboardState *)
490             qemu_mallocz(sizeof(SpitzKeyboardState));
491     memset(s, 0, sizeof(SpitzKeyboardState));
492
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;
499
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]];
502
503     for (i = 0; i < 5; i ++)
504         s->gpiomap[i] = pxa2xx_gpio_in_get(cpu->gpio)[spitz_gpiomap[i]];
505
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]);
510
511     spitz_keyboard_pre_map(s);
512     qemu_add_kbd_event_handler((QEMUPutKBDEvent *) spitz_keyboard_handler, s);
513
514     register_savevm("spitz_keyboard", 0, 0,
515                     spitz_keyboard_save, spitz_keyboard_load, s);
516 }
517
518 /* LCD backlight controller */
519
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
528
529 typedef struct {
530     SSISlave ssidev;
531     int bl_intensity;
532     int bl_power;
533 } SpitzLCDTG;
534
535 static void spitz_bl_update(SpitzLCDTG *s)
536 {
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");
541 }
542
543 /* FIXME: Implement GPIO properly and remove this hack.  */
544 static SpitzLCDTG *spitz_lcdtg;
545
546 static inline void spitz_bl_bit5(void *opaque, int line, int level)
547 {
548     SpitzLCDTG *s = spitz_lcdtg;
549     int prev = s->bl_intensity;
550
551     if (level)
552         s->bl_intensity &= ~0x20;
553     else
554         s->bl_intensity |= 0x20;
555
556     if (s->bl_power && prev != s->bl_intensity)
557         spitz_bl_update(s);
558 }
559
560 static inline void spitz_bl_power(void *opaque, int line, int level)
561 {
562     SpitzLCDTG *s = spitz_lcdtg;
563     s->bl_power = !!level;
564     spitz_bl_update(s);
565 }
566
567 static uint32_t spitz_lcdtg_transfer(SSISlave *dev, uint32_t value)
568 {
569     SpitzLCDTG *s = FROM_SSI_SLAVE(SpitzLCDTG, dev);
570     int addr;
571     addr = value >> 5;
572     value &= 0x1f;
573
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;
581
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;
588
589     case LCDTG_POWERREG0:
590         /* Set common voltage to M62332FP */
591         break;
592     }
593     return 0;
594 }
595
596 static void spitz_lcdtg_save(QEMUFile *f, void *opaque)
597 {
598     SpitzLCDTG *s = (SpitzLCDTG *)opaque;
599     qemu_put_be32(f, s->bl_intensity);
600     qemu_put_be32(f, s->bl_power);
601 }
602
603 static int spitz_lcdtg_load(QEMUFile *f, void *opaque, int version_id)
604 {
605     SpitzLCDTG *s = (SpitzLCDTG *)opaque;
606     s->bl_intensity = qemu_get_be32(f);
607     s->bl_power = qemu_get_be32(f);
608     return 0;
609 }
610
611 static void spitz_lcdtg_init(SSISlave *dev)
612 {
613     SpitzLCDTG *s = FROM_SSI_SLAVE(SpitzLCDTG, dev);
614
615     spitz_lcdtg = s;
616     s->bl_power = 0;
617     s->bl_intensity = 0x20;
618
619     register_savevm("spitz-lcdtg", -1, 1,
620                     spitz_lcdtg_save, spitz_lcdtg_load, s);
621 }
622
623 /* SSP devices */
624
625 #define CORGI_SSP_PORT          2
626
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
631
632 static DeviceState *max1111;
633
634 /* "Demux" the signal based on current chipselect */
635 typedef struct {
636     SSISlave ssidev;
637     SSIBus *bus[3];
638     int enable[3];
639 } CorgiSSPState;
640
641 static uint32_t corgi_ssp_transfer(SSISlave *dev, uint32_t value)
642 {
643     CorgiSSPState *s = FROM_SSI_SLAVE(CorgiSSPState, dev);
644     int i;
645
646     for (i = 0; i < 3; i++) {
647         if (s->enable[i]) {
648             return ssi_transfer(s->bus[i], value);
649         }
650     }
651     return 0;
652 }
653
654 static void corgi_ssp_gpio_cs(void *opaque, int line, int level)
655 {
656     CorgiSSPState *s = (CorgiSSPState *)opaque;
657     assert(line >= 0 && line < 3);
658     s->enable[line] = !level;
659 }
660
661 #define MAX1111_BATT_VOLT       1
662 #define MAX1111_BATT_TEMP       2
663 #define MAX1111_ACIN_VOLT       3
664
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 */
668
669 static void spitz_adc_temp_on(void *opaque, int line, int level)
670 {
671     if (!max1111)
672         return;
673
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);
678 }
679
680 static void spitz_ssp_save(QEMUFile *f, void *opaque)
681 {
682     CorgiSSPState *s = (CorgiSSPState *)opaque;
683     int i;
684
685     for (i = 0; i < 3; i++) {
686         qemu_put_be32(f, s->enable[i]);
687     }
688 }
689
690 static int spitz_ssp_load(QEMUFile *f, void *opaque, int version_id)
691 {
692     CorgiSSPState *s = (CorgiSSPState *)opaque;
693     int i;
694
695     if (version_id != 1) {
696         return -EINVAL;
697     }
698     for (i = 0; i < 3; i++) {
699         s->enable[i] = qemu_get_be32(f);
700     }
701     return 0;
702 }
703
704 static void corgi_ssp_init(SSISlave *dev)
705 {
706     CorgiSSPState *s = FROM_SSI_SLAVE(CorgiSSPState, dev);
707
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");
712
713     register_savevm("spitz_ssp", -1, 1, spitz_ssp_save, spitz_ssp_load, s);
714 }
715
716 static void spitz_ssp_attach(PXA2xxState *cpu)
717 {
718     DeviceState *mux;
719     DeviceState *dev;
720     void *bus;
721
722     mux = ssi_create_slave(cpu->ssp[CORGI_SSP_PORT - 1], "corgi-ssp");
723
724     bus = qdev_get_child_bus(mux, "ssi0");
725     dev = ssi_create_slave(bus, "spitz-lcdtg");
726
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]);
731
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);
737
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));
744 }
745
746 /* CF Microdrive */
747
748 static void spitz_microdrive_attach(PXA2xxState *cpu, int slot)
749 {
750     PCMCIACardState *md;
751     int index;
752     BlockDriverState *bs;
753
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);
761     }
762 }
763
764 /* Wm8750 and Max7310 on I2C */
765
766 #define AKITA_MAX_ADDR  0x18
767 #define SPITZ_WM_ADDRL  0x1b
768 #define SPITZ_WM_ADDRH  0x1a
769
770 #define SPITZ_GPIO_WM   5
771
772 #ifdef HAS_AUDIO
773 static void spitz_wm8750_addr(void *opaque, int line, int level)
774 {
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);
780 }
781 #endif
782
783 static void spitz_i2c_setup(PXA2xxState *cpu)
784 {
785     /* Attach the CPU on one end of our I2C bus.  */
786     i2c_bus *bus = pxa2xx_i2c_bus(cpu->i2c[0]);
787
788 #ifdef HAS_AUDIO
789     DeviceState *wm;
790
791     /* Attach a WM8750 to the bus */
792     wm = i2c_create_slave(bus, "wm8750", 0);
793
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
803 }
804
805 static void spitz_akita_i2c_setup(PXA2xxState *cpu)
806 {
807     /* Attach a Max7310 to Akita I2C bus.  */
808     i2c_create_slave(pxa2xx_i2c_bus(cpu->i2c[0]), "max7310",
809                      AKITA_MAX_ADDR);
810 }
811
812 /* Other peripherals */
813
814 static void spitz_out_switch(void *opaque, int line, int level)
815 {
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;
838     }
839 }
840
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
855
856 static void spitz_scoop_gpio_setup(PXA2xxState *cpu,
857                 ScoopInfo *scp0, ScoopInfo *scp1)
858 {
859     qemu_irq *outsignals = qemu_allocate_irqs(spitz_out_switch, cpu, 8);
860
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]);
865
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]);
869     }
870
871     scoop_gpio_out_set(scp0, SPITZ_SCP_ADC_TEMP_ON, outsignals[6]);
872 }
873
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
883
884 static int spitz_hsync;
885
886 static void spitz_lcd_hsync_handler(void *opaque, int line, int level)
887 {
888     PXA2xxState *cpu = (PXA2xxState *) opaque;
889     qemu_set_irq(pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_HSYNC], spitz_hsync);
890     spitz_hsync ^= 1;
891 }
892
893 static void spitz_gpio_setup(PXA2xxState *cpu, int slots)
894 {
895     qemu_irq lcd_hsync;
896     /*
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.
901      */
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);
906
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]);
911
912     /* Battery lock always closed */
913     qemu_irq_raise(pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_BAT_COVER]);
914
915     /* Handle reset */
916     pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_ON_RESET, cpu->reset);
917
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]);
927
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;
934     }
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]);
939 }
940
941 /* Board init.  */
942 enum spitz_model_e { spitz, akita, borzoi, terrier };
943
944 #define SPITZ_RAM       0x04000000
945 #define SPITZ_ROM       0x00800000
946
947 static struct arm_boot_info spitz_binfo = {
948     .loader_start = PXA2XX_SDRAM_BASE,
949     .ram_size = 0x04000000,
950 };
951
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)
956 {
957     PXA2xxState *cpu;
958     ScoopInfo *scp0, *scp1 = NULL;
959
960     if (!cpu_model)
961         cpu_model = (model == terrier) ? "pxa270-c5" : "pxa270-c0";
962
963     /* Setup CPU & memory */
964     cpu = pxa270_init(spitz_binfo.ram_size, cpu_model);
965
966     sl_flash_register(cpu, (model == spitz) ? FLASH_128M : FLASH_1024M);
967
968     cpu_register_physical_memory(0, SPITZ_ROM,
969                     qemu_ram_alloc(SPITZ_ROM) | IO_MEM_ROM);
970
971     /* Setup peripherals */
972     spitz_keyboard_register(cpu);
973
974     spitz_ssp_attach(cpu);
975
976     scp0 = scoop_init(cpu, 0, 0x10800000);
977     if (model != akita) {
978             scp1 = scoop_init(cpu, 1, 0x08800040);
979     }
980
981     spitz_scoop_gpio_setup(cpu, scp0, scp1);
982
983     spitz_gpio_setup(cpu, (model == akita) ? 1 : 2);
984
985     spitz_i2c_setup(cpu);
986
987     if (model == akita)
988         spitz_akita_i2c_setup(cpu);
989
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);
996
997     /* Setup initial (reset) machine state */
998     cpu->env->regs[15] = spitz_binfo.loader_start;
999
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);
1006 }
1007
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)
1012 {
1013     spitz_common_init(ram_size, kernel_filename,
1014                 kernel_cmdline, initrd_filename, cpu_model, spitz, 0x2c9);
1015 }
1016
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)
1021 {
1022     spitz_common_init(ram_size, kernel_filename,
1023                 kernel_cmdline, initrd_filename, cpu_model, borzoi, 0x33f);
1024 }
1025
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)
1030 {
1031     spitz_common_init(ram_size, kernel_filename,
1032                 kernel_cmdline, initrd_filename, cpu_model, akita, 0x2e8);
1033 }
1034
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)
1039 {
1040     spitz_common_init(ram_size, kernel_filename,
1041                 kernel_cmdline, initrd_filename, cpu_model, terrier, 0x33f);
1042 }
1043
1044 QEMUMachine akitapda_machine = {
1045     .name = "akita",
1046     .desc = "Akita PDA (PXA270)",
1047     .init = akita_init,
1048 };
1049
1050 static QEMUMachine spitzpda_machine = {
1051     .name = "spitz",
1052     .desc = "Spitz PDA (PXA270)",
1053     .init = spitz_init,
1054 };
1055
1056 static QEMUMachine borzoipda_machine = {
1057     .name = "borzoi",
1058     .desc = "Borzoi PDA (PXA270)",
1059     .init = borzoi_init,
1060 };
1061
1062 static QEMUMachine terrierpda_machine = {
1063     .name = "terrier",
1064     .desc = "Terrier PDA (PXA270)",
1065     .init = terrier_init,
1066 };
1067
1068 static void spitz_machine_init(void)
1069 {
1070     qemu_register_machine(&akitapda_machine);
1071     qemu_register_machine(&spitzpda_machine);
1072     qemu_register_machine(&borzoipda_machine);
1073     qemu_register_machine(&terrierpda_machine);
1074 }
1075
1076 machine_init(spitz_machine_init);
1077
1078 static SSISlaveInfo corgi_ssp_info = {
1079     .qdev.name = "corgi-ssp",
1080     .qdev.size = sizeof(CorgiSSPState),
1081     .init = corgi_ssp_init,
1082     .transfer = corgi_ssp_transfer
1083 };
1084
1085 static SSISlaveInfo spitz_lcdtg_info = {
1086     .qdev.name = "spitz-lcdtg",
1087     .qdev.size = sizeof(SpitzLCDTG),
1088     .init = spitz_lcdtg_init,
1089     .transfer = spitz_lcdtg_transfer
1090 };
1091
1092 static void spitz_register_devices(void)
1093 {
1094     ssi_register_slave(&corgi_ssp_info);
1095     ssi_register_slave(&spitz_lcdtg_info);
1096 }
1097
1098 device_init(spitz_register_devices)