Merge branch 'master' of /home/nchip/public_html/qemu into garage-push
[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     BlockDriverState *bdrv = NULL;
177     if (drive_get_index(IF_MTD, 0, 0) >= 0) {
178         bdrv = drives_table[drive_get_index(IF_MTD, 0, 0)].bdrv;
179     }
180     if (size == FLASH_128M)
181         s->nand = nand_init(NAND_MFR_SAMSUNG, 0x73, bdrv);
182     else if (size == FLASH_1024M)
183         s->nand = nand_init(NAND_MFR_SAMSUNG, 0xf1, bdrv);
184
185     iomemtype = cpu_register_io_memory(0, sl_readfn,
186                     sl_writefn, s);
187     cpu_register_physical_memory(FLASH_BASE, 0x40, iomemtype);
188
189     register_savevm("sl_flash", 0, 0, sl_save, sl_load, s);
190 }
191
192 /* Spitz Keyboard */
193
194 #define SPITZ_KEY_STROBE_NUM    11
195 #define SPITZ_KEY_SENSE_NUM     7
196
197 static const int spitz_gpio_key_sense[SPITZ_KEY_SENSE_NUM] = {
198     12, 17, 91, 34, 36, 38, 39
199 };
200
201 static const int spitz_gpio_key_strobe[SPITZ_KEY_STROBE_NUM] = {
202     88, 23, 24, 25, 26, 27, 52, 103, 107, 108, 114
203 };
204
205 /* Eighth additional row maps the special keys */
206 static int spitz_keymap[SPITZ_KEY_SENSE_NUM + 1][SPITZ_KEY_STROBE_NUM] = {
207     { 0x1d, 0x02, 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0e, 0x3f, 0x40 },
208     {  -1 , 0x03, 0x05, 0x13, 0x15, 0x09, 0x17, 0x18, 0x19, 0x41, 0x42 },
209     { 0x0f, 0x10, 0x12, 0x14, 0x22, 0x16, 0x24, 0x25,  -1 ,  -1 ,  -1  },
210     { 0x3c, 0x11, 0x1f, 0x21, 0x2f, 0x23, 0x32, 0x26,  -1 , 0x36,  -1  },
211     { 0x3b, 0x1e, 0x20, 0x2e, 0x30, 0x31, 0x34,  -1 , 0x1c, 0x2a,  -1  },
212     { 0x44, 0x2c, 0x2d, 0x0c, 0x39, 0x33,  -1 , 0x48,  -1 ,  -1 , 0x38 },
213     { 0x37, 0x3d,  -1 , 0x45, 0x57, 0x58, 0x4b, 0x50, 0x4d,  -1 ,  -1  },
214     { 0x52, 0x43, 0x01, 0x47, 0x49,  -1 ,  -1 ,  -1 ,  -1 ,  -1 ,  -1  },
215 };
216
217 #define SPITZ_GPIO_AK_INT       13      /* Remote control */
218 #define SPITZ_GPIO_SYNC         16      /* Sync button */
219 #define SPITZ_GPIO_ON_KEY       95      /* Power button */
220 #define SPITZ_GPIO_SWA          97      /* Lid */
221 #define SPITZ_GPIO_SWB          96      /* Tablet mode */
222
223 /* The special buttons are mapped to unused keys */
224 static const int spitz_gpiomap[5] = {
225     SPITZ_GPIO_AK_INT, SPITZ_GPIO_SYNC, SPITZ_GPIO_ON_KEY,
226     SPITZ_GPIO_SWA, SPITZ_GPIO_SWB,
227 };
228 static int spitz_gpio_invert[5] = { 0, 0, 0, 0, 0, };
229
230 typedef struct {
231     qemu_irq sense[SPITZ_KEY_SENSE_NUM];
232     qemu_irq *strobe;
233     qemu_irq gpiomap[5];
234     int keymap[0x80];
235     uint16_t keyrow[SPITZ_KEY_SENSE_NUM];
236     uint16_t strobe_state;
237     uint16_t sense_state;
238
239     uint16_t pre_map[0x100];
240     uint16_t modifiers;
241     uint16_t imodifiers;
242     uint8_t fifo[16];
243     int fifopos, fifolen;
244     QEMUTimer *kbdtimer;
245 } SpitzKeyboardState;
246
247 static void spitz_keyboard_sense_update(SpitzKeyboardState *s)
248 {
249     int i;
250     uint16_t strobe, sense = 0;
251     for (i = 0; i < SPITZ_KEY_SENSE_NUM; i ++) {
252         strobe = s->keyrow[i] & s->strobe_state;
253         if (strobe) {
254             sense |= 1 << i;
255             if (!(s->sense_state & (1 << i)))
256                 qemu_irq_raise(s->sense[i]);
257         } else if (s->sense_state & (1 << i))
258             qemu_irq_lower(s->sense[i]);
259     }
260
261     s->sense_state = sense;
262 }
263
264 static void spitz_keyboard_strobe(void *opaque, int line, int level)
265 {
266     SpitzKeyboardState *s = (SpitzKeyboardState *) opaque;
267
268     if (level)
269         s->strobe_state |= 1 << line;
270     else
271         s->strobe_state &= ~(1 << line);
272     spitz_keyboard_sense_update(s);
273 }
274
275 static void spitz_keyboard_keydown(SpitzKeyboardState *s, int keycode)
276 {
277     int spitz_keycode = s->keymap[keycode & 0x7f];
278     if (spitz_keycode == -1)
279         return;
280
281     /* Handle the additional keys */
282     if ((spitz_keycode >> 4) == SPITZ_KEY_SENSE_NUM) {
283         qemu_set_irq(s->gpiomap[spitz_keycode & 0xf], (keycode < 0x80) ^
284                         spitz_gpio_invert[spitz_keycode & 0xf]);
285         return;
286     }
287
288     if (keycode & 0x80)
289         s->keyrow[spitz_keycode >> 4] &= ~(1 << (spitz_keycode & 0xf));
290     else
291         s->keyrow[spitz_keycode >> 4] |= 1 << (spitz_keycode & 0xf);
292
293     spitz_keyboard_sense_update(s);
294 }
295
296 #define SHIFT   (1 << 7)
297 #define CTRL    (1 << 8)
298 #define FN      (1 << 9)
299
300 #define QUEUE_KEY(c)    s->fifo[(s->fifopos + s->fifolen ++) & 0xf] = c
301
302 static void spitz_keyboard_handler(SpitzKeyboardState *s, int keycode)
303 {
304     uint16_t code;
305     int mapcode;
306     switch (keycode) {
307     case 0x2a:  /* Left Shift */
308         s->modifiers |= 1;
309         break;
310     case 0xaa:
311         s->modifiers &= ~1;
312         break;
313     case 0x36:  /* Right Shift */
314         s->modifiers |= 2;
315         break;
316     case 0xb6:
317         s->modifiers &= ~2;
318         break;
319     case 0x1d:  /* Control */
320         s->modifiers |= 4;
321         break;
322     case 0x9d:
323         s->modifiers &= ~4;
324         break;
325     case 0x38:  /* Alt */
326         s->modifiers |= 8;
327         break;
328     case 0xb8:
329         s->modifiers &= ~8;
330         break;
331     }
332
333     code = s->pre_map[mapcode = ((s->modifiers & 3) ?
334             (keycode | SHIFT) :
335             (keycode & ~SHIFT))];
336
337     if (code != mapcode) {
338 #if 0
339         if ((code & SHIFT) && !(s->modifiers & 1))
340             QUEUE_KEY(0x2a | (keycode & 0x80));
341         if ((code & CTRL ) && !(s->modifiers & 4))
342             QUEUE_KEY(0x1d | (keycode & 0x80));
343         if ((code & FN   ) && !(s->modifiers & 8))
344             QUEUE_KEY(0x38 | (keycode & 0x80));
345         if ((code & FN   ) && (s->modifiers & 1))
346             QUEUE_KEY(0x2a | (~keycode & 0x80));
347         if ((code & FN   ) && (s->modifiers & 2))
348             QUEUE_KEY(0x36 | (~keycode & 0x80));
349 #else
350         if (keycode & 0x80) {
351             if ((s->imodifiers & 1   ) && !(s->modifiers & 1))
352                 QUEUE_KEY(0x2a | 0x80);
353             if ((s->imodifiers & 4   ) && !(s->modifiers & 4))
354                 QUEUE_KEY(0x1d | 0x80);
355             if ((s->imodifiers & 8   ) && !(s->modifiers & 8))
356                 QUEUE_KEY(0x38 | 0x80);
357             if ((s->imodifiers & 0x10) && (s->modifiers & 1))
358                 QUEUE_KEY(0x2a);
359             if ((s->imodifiers & 0x20) && (s->modifiers & 2))
360                 QUEUE_KEY(0x36);
361             s->imodifiers = 0;
362         } else {
363             if ((code & SHIFT) && !((s->modifiers | s->imodifiers) & 1)) {
364                 QUEUE_KEY(0x2a);
365                 s->imodifiers |= 1;
366             }
367             if ((code & CTRL ) && !((s->modifiers | s->imodifiers) & 4)) {
368                 QUEUE_KEY(0x1d);
369                 s->imodifiers |= 4;
370             }
371             if ((code & FN   ) && !((s->modifiers | s->imodifiers) & 8)) {
372                 QUEUE_KEY(0x38);
373                 s->imodifiers |= 8;
374             }
375             if ((code & FN   ) && (s->modifiers & 1) &&
376                             !(s->imodifiers & 0x10)) {
377                 QUEUE_KEY(0x2a | 0x80);
378                 s->imodifiers |= 0x10;
379             }
380             if ((code & FN   ) && (s->modifiers & 2) &&
381                             !(s->imodifiers & 0x20)) {
382                 QUEUE_KEY(0x36 | 0x80);
383                 s->imodifiers |= 0x20;
384             }
385         }
386 #endif
387     }
388
389     QUEUE_KEY((code & 0x7f) | (keycode & 0x80));
390 }
391
392 static void spitz_keyboard_tick(void *opaque)
393 {
394     SpitzKeyboardState *s = (SpitzKeyboardState *) opaque;
395
396     if (s->fifolen) {
397         spitz_keyboard_keydown(s, s->fifo[s->fifopos ++]);
398         s->fifolen --;
399         if (s->fifopos >= 16)
400             s->fifopos = 0;
401     }
402
403     qemu_mod_timer(s->kbdtimer, qemu_get_clock(vm_clock) + ticks_per_sec / 32);
404 }
405
406 static void spitz_keyboard_pre_map(SpitzKeyboardState *s)
407 {
408     int i;
409     for (i = 0; i < 0x100; i ++)
410         s->pre_map[i] = i;
411     s->pre_map[0x02 | SHIFT     ] = 0x02 | SHIFT;       /* exclam */
412     s->pre_map[0x28 | SHIFT     ] = 0x03 | SHIFT;       /* quotedbl */
413     s->pre_map[0x04 | SHIFT     ] = 0x04 | SHIFT;       /* numbersign */
414     s->pre_map[0x05 | SHIFT     ] = 0x05 | SHIFT;       /* dollar */
415     s->pre_map[0x06 | SHIFT     ] = 0x06 | SHIFT;       /* percent */
416     s->pre_map[0x08 | SHIFT     ] = 0x07 | SHIFT;       /* ampersand */
417     s->pre_map[0x28             ] = 0x08 | SHIFT;       /* apostrophe */
418     s->pre_map[0x0a | SHIFT     ] = 0x09 | SHIFT;       /* parenleft */
419     s->pre_map[0x0b | SHIFT     ] = 0x0a | SHIFT;       /* parenright */
420     s->pre_map[0x29 | SHIFT     ] = 0x0b | SHIFT;       /* asciitilde */
421     s->pre_map[0x03 | SHIFT     ] = 0x0c | SHIFT;       /* at */
422     s->pre_map[0xd3             ] = 0x0e | FN;          /* Delete */
423     s->pre_map[0x3a             ] = 0x0f | FN;          /* Caps_Lock */
424     s->pre_map[0x07 | SHIFT     ] = 0x11 | FN;          /* asciicircum */
425     s->pre_map[0x0d             ] = 0x12 | FN;          /* equal */
426     s->pre_map[0x0d | SHIFT     ] = 0x13 | FN;          /* plus */
427     s->pre_map[0x1a             ] = 0x14 | FN;          /* bracketleft */
428     s->pre_map[0x1b             ] = 0x15 | FN;          /* bracketright */
429     s->pre_map[0x1a | SHIFT     ] = 0x16 | FN;          /* braceleft */
430     s->pre_map[0x1b | SHIFT     ] = 0x17 | FN;          /* braceright */
431     s->pre_map[0x27             ] = 0x22 | FN;          /* semicolon */
432     s->pre_map[0x27 | SHIFT     ] = 0x23 | FN;          /* colon */
433     s->pre_map[0x09 | SHIFT     ] = 0x24 | FN;          /* asterisk */
434     s->pre_map[0x2b             ] = 0x25 | FN;          /* backslash */
435     s->pre_map[0x2b | SHIFT     ] = 0x26 | FN;          /* bar */
436     s->pre_map[0x0c | SHIFT     ] = 0x30 | FN;          /* underscore */
437     s->pre_map[0x33 | SHIFT     ] = 0x33 | FN;          /* less */
438     s->pre_map[0x35             ] = 0x33 | SHIFT;       /* slash */
439     s->pre_map[0x34 | SHIFT     ] = 0x34 | FN;          /* greater */
440     s->pre_map[0x35 | SHIFT     ] = 0x34 | SHIFT;       /* question */
441     s->pre_map[0x49             ] = 0x48 | FN;          /* Page_Up */
442     s->pre_map[0x51             ] = 0x50 | FN;          /* Page_Down */
443
444     s->modifiers = 0;
445     s->imodifiers = 0;
446     s->fifopos = 0;
447     s->fifolen = 0;
448     s->kbdtimer = qemu_new_timer(vm_clock, spitz_keyboard_tick, s);
449     spitz_keyboard_tick(s);
450 }
451
452 #undef SHIFT
453 #undef CTRL
454 #undef FN
455
456 static void spitz_keyboard_save(QEMUFile *f, void *opaque)
457 {
458     SpitzKeyboardState *s = (SpitzKeyboardState *) opaque;
459     int i;
460
461     qemu_put_be16s(f, &s->sense_state);
462     qemu_put_be16s(f, &s->strobe_state);
463     for (i = 0; i < 5; i ++)
464         qemu_put_byte(f, spitz_gpio_invert[i]);
465 }
466
467 static int spitz_keyboard_load(QEMUFile *f, void *opaque, int version_id)
468 {
469     SpitzKeyboardState *s = (SpitzKeyboardState *) opaque;
470     int i;
471
472     qemu_get_be16s(f, &s->sense_state);
473     qemu_get_be16s(f, &s->strobe_state);
474     for (i = 0; i < 5; i ++)
475         spitz_gpio_invert[i] = qemu_get_byte(f);
476
477     /* Release all pressed keys */
478     memset(s->keyrow, 0, sizeof(s->keyrow));
479     spitz_keyboard_sense_update(s);
480     s->modifiers = 0;
481     s->imodifiers = 0;
482     s->fifopos = 0;
483     s->fifolen = 0;
484
485     return 0;
486 }
487
488 static void spitz_keyboard_register(PXA2xxState *cpu)
489 {
490     int i, j;
491     SpitzKeyboardState *s;
492
493     s = (SpitzKeyboardState *)
494             qemu_mallocz(sizeof(SpitzKeyboardState));
495     memset(s, 0, sizeof(SpitzKeyboardState));
496
497     for (i = 0; i < 0x80; i ++)
498         s->keymap[i] = -1;
499     for (i = 0; i < SPITZ_KEY_SENSE_NUM + 1; i ++)
500         for (j = 0; j < SPITZ_KEY_STROBE_NUM; j ++)
501             if (spitz_keymap[i][j] != -1)
502                 s->keymap[spitz_keymap[i][j]] = (i << 4) | j;
503
504     for (i = 0; i < SPITZ_KEY_SENSE_NUM; i ++)
505         s->sense[i] = pxa2xx_gpio_in_get(cpu->gpio)[spitz_gpio_key_sense[i]];
506
507     for (i = 0; i < 5; i ++)
508         s->gpiomap[i] = pxa2xx_gpio_in_get(cpu->gpio)[spitz_gpiomap[i]];
509
510     s->strobe = qemu_allocate_irqs(spitz_keyboard_strobe, s,
511                     SPITZ_KEY_STROBE_NUM);
512     for (i = 0; i < SPITZ_KEY_STROBE_NUM; i ++)
513         pxa2xx_gpio_out_set(cpu->gpio, spitz_gpio_key_strobe[i], s->strobe[i]);
514
515     spitz_keyboard_pre_map(s);
516     qemu_add_kbd_event_handler((QEMUPutKBDEvent *) spitz_keyboard_handler, s);
517
518     register_savevm("spitz_keyboard", 0, 0,
519                     spitz_keyboard_save, spitz_keyboard_load, s);
520 }
521
522 /* LCD backlight controller */
523
524 #define LCDTG_RESCTL    0x00
525 #define LCDTG_PHACTRL   0x01
526 #define LCDTG_DUTYCTRL  0x02
527 #define LCDTG_POWERREG0 0x03
528 #define LCDTG_POWERREG1 0x04
529 #define LCDTG_GPOR3     0x05
530 #define LCDTG_PICTRL    0x06
531 #define LCDTG_POLCTRL   0x07
532
533 typedef struct {
534     SSISlave ssidev;
535     int bl_intensity;
536     int bl_power;
537 } SpitzLCDTG;
538
539 static void spitz_bl_update(SpitzLCDTG *s)
540 {
541     if (s->bl_power && s->bl_intensity)
542         zaurus_printf("LCD Backlight now at %i/63\n", s->bl_intensity);
543     else
544         zaurus_printf("LCD Backlight now off\n");
545 }
546
547 /* FIXME: Implement GPIO properly and remove this hack.  */
548 static SpitzLCDTG *spitz_lcdtg;
549
550 static inline void spitz_bl_bit5(void *opaque, int line, int level)
551 {
552     SpitzLCDTG *s = spitz_lcdtg;
553     int prev = s->bl_intensity;
554
555     if (level)
556         s->bl_intensity &= ~0x20;
557     else
558         s->bl_intensity |= 0x20;
559
560     if (s->bl_power && prev != s->bl_intensity)
561         spitz_bl_update(s);
562 }
563
564 static inline void spitz_bl_power(void *opaque, int line, int level)
565 {
566     SpitzLCDTG *s = spitz_lcdtg;
567     s->bl_power = !!level;
568     spitz_bl_update(s);
569 }
570
571 static uint32_t spitz_lcdtg_transfer(SSISlave *dev, uint32_t value)
572 {
573     SpitzLCDTG *s = FROM_SSI_SLAVE(SpitzLCDTG, dev);
574     int addr;
575     addr = value >> 5;
576     value &= 0x1f;
577
578     switch (addr) {
579     case LCDTG_RESCTL:
580         if (value)
581             zaurus_printf("LCD in QVGA mode\n");
582         else
583             zaurus_printf("LCD in VGA mode\n");
584         break;
585
586     case LCDTG_DUTYCTRL:
587         s->bl_intensity &= ~0x1f;
588         s->bl_intensity |= value;
589         if (s->bl_power)
590             spitz_bl_update(s);
591         break;
592
593     case LCDTG_POWERREG0:
594         /* Set common voltage to M62332FP */
595         break;
596     }
597     return 0;
598 }
599
600 static void spitz_lcdtg_save(QEMUFile *f, void *opaque)
601 {
602     SpitzLCDTG *s = (SpitzLCDTG *)opaque;
603     qemu_put_be32(f, s->bl_intensity);
604     qemu_put_be32(f, s->bl_power);
605 }
606
607 static int spitz_lcdtg_load(QEMUFile *f, void *opaque, int version_id)
608 {
609     SpitzLCDTG *s = (SpitzLCDTG *)opaque;
610     s->bl_intensity = qemu_get_be32(f);
611     s->bl_power = qemu_get_be32(f);
612     return 0;
613 }
614
615 static void spitz_lcdtg_init(SSISlave *dev)
616 {
617     SpitzLCDTG *s = FROM_SSI_SLAVE(SpitzLCDTG, dev);
618
619     spitz_lcdtg = s;
620     s->bl_power = 0;
621     s->bl_intensity = 0x20;
622
623     register_savevm("spitz-lcdtg", -1, 1,
624                     spitz_lcdtg_save, spitz_lcdtg_load, s);
625 }
626
627 /* SSP devices */
628
629 #define CORGI_SSP_PORT          2
630
631 #define SPITZ_GPIO_LCDCON_CS    53
632 #define SPITZ_GPIO_ADS7846_CS   14
633 #define SPITZ_GPIO_MAX1111_CS   20
634 #define SPITZ_GPIO_TP_INT       11
635
636 static DeviceState *max1111;
637
638 /* "Demux" the signal based on current chipselect */
639 typedef struct {
640     SSISlave ssidev;
641     SSIBus *bus[3];
642     int enable[3];
643 } CorgiSSPState;
644
645 static uint32_t corgi_ssp_transfer(SSISlave *dev, uint32_t value)
646 {
647     CorgiSSPState *s = FROM_SSI_SLAVE(CorgiSSPState, dev);
648     int i;
649
650     for (i = 0; i < 3; i++) {
651         if (s->enable[i]) {
652             return ssi_transfer(s->bus[i], value);
653         }
654     }
655     return 0;
656 }
657
658 static void corgi_ssp_gpio_cs(void *opaque, int line, int level)
659 {
660     CorgiSSPState *s = (CorgiSSPState *)opaque;
661     assert(line >= 0 && line < 3);
662     s->enable[line] = !level;
663 }
664
665 #define MAX1111_BATT_VOLT       1
666 #define MAX1111_BATT_TEMP       2
667 #define MAX1111_ACIN_VOLT       3
668
669 #define SPITZ_BATTERY_TEMP      0xe0    /* About 2.9V */
670 #define SPITZ_BATTERY_VOLT      0xd0    /* About 4.0V */
671 #define SPITZ_CHARGEON_ACIN     0x80    /* About 5.0V */
672
673 static void spitz_adc_temp_on(void *opaque, int line, int level)
674 {
675     if (!max1111)
676         return;
677
678     if (level)
679         max111x_set_input(max1111, MAX1111_BATT_TEMP, SPITZ_BATTERY_TEMP);
680     else
681         max111x_set_input(max1111, MAX1111_BATT_TEMP, 0);
682 }
683
684 static void spitz_ssp_save(QEMUFile *f, void *opaque)
685 {
686     CorgiSSPState *s = (CorgiSSPState *)opaque;
687     int i;
688
689     for (i = 0; i < 3; i++) {
690         qemu_put_be32(f, s->enable[i]);
691     }
692 }
693
694 static int spitz_ssp_load(QEMUFile *f, void *opaque, int version_id)
695 {
696     CorgiSSPState *s = (CorgiSSPState *)opaque;
697     int i;
698
699     if (version_id != 1) {
700         return -EINVAL;
701     }
702     for (i = 0; i < 3; i++) {
703         s->enable[i] = qemu_get_be32(f);
704     }
705     return 0;
706 }
707
708 static void corgi_ssp_init(SSISlave *dev)
709 {
710     CorgiSSPState *s = FROM_SSI_SLAVE(CorgiSSPState, dev);
711
712     qdev_init_gpio_in(&dev->qdev, corgi_ssp_gpio_cs, 3);
713     s->bus[0] = ssi_create_bus(&dev->qdev, "ssi0");
714     s->bus[1] = ssi_create_bus(&dev->qdev, "ssi1");
715     s->bus[2] = ssi_create_bus(&dev->qdev, "ssi2");
716
717     register_savevm("spitz_ssp", -1, 1, spitz_ssp_save, spitz_ssp_load, s);
718 }
719
720 static void spitz_ssp_attach(PXA2xxState *cpu)
721 {
722     DeviceState *mux;
723     DeviceState *dev;
724     void *bus;
725
726     mux = ssi_create_slave(cpu->ssp[CORGI_SSP_PORT - 1], "corgi-ssp");
727
728     bus = qdev_get_child_bus(mux, "ssi0");
729     dev = ssi_create_slave(bus, "spitz-lcdtg");
730
731     bus = qdev_get_child_bus(mux, "ssi1");
732     dev = ssi_create_slave(bus, "ads7846");
733     qdev_connect_gpio_out(dev, 0,
734                           pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_TP_INT]);
735
736     bus = qdev_get_child_bus(mux, "ssi2");
737     max1111 = ssi_create_slave(bus, "max1111");
738     max111x_set_input(max1111, MAX1111_BATT_VOLT, SPITZ_BATTERY_VOLT);
739     max111x_set_input(max1111, MAX1111_BATT_TEMP, 0);
740     max111x_set_input(max1111, MAX1111_ACIN_VOLT, SPITZ_CHARGEON_ACIN);
741
742     pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_LCDCON_CS,
743                         qdev_get_gpio_in(mux, 0));
744     pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_ADS7846_CS,
745                         qdev_get_gpio_in(mux, 1));
746     pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_MAX1111_CS,
747                         qdev_get_gpio_in(mux, 2));
748 }
749
750 /* CF Microdrive */
751
752 static void spitz_microdrive_attach(PXA2xxState *cpu, int slot)
753 {
754     PCMCIACardState *md;
755     int index;
756     BlockDriverState *bs;
757
758     index = drive_get_index(IF_IDE, 0, 0);
759     if (index == -1)
760         return;
761     bs = drives_table[index].bdrv;
762     if (bdrv_is_inserted(bs) && !bdrv_is_removable(bs)) {
763         md = dscm1xxxx_init(bs);
764         pxa2xx_pcmcia_attach(cpu->pcmcia[slot], md);
765     }
766 }
767
768 /* Wm8750 and Max7310 on I2C */
769
770 #define AKITA_MAX_ADDR  0x18
771 #define SPITZ_WM_ADDRL  0x1b
772 #define SPITZ_WM_ADDRH  0x1a
773
774 #define SPITZ_GPIO_WM   5
775
776 #ifdef HAS_AUDIO
777 static void spitz_wm8750_addr(void *opaque, int line, int level)
778 {
779     i2c_slave *wm = (i2c_slave *) opaque;
780     if (level)
781         i2c_set_slave_address(wm, SPITZ_WM_ADDRH);
782     else
783         i2c_set_slave_address(wm, SPITZ_WM_ADDRL);
784 }
785 #endif
786
787 static void spitz_i2c_setup(PXA2xxState *cpu)
788 {
789     /* Attach the CPU on one end of our I2C bus.  */
790     i2c_bus *bus = pxa2xx_i2c_bus(cpu->i2c[0]);
791
792 #ifdef HAS_AUDIO
793     DeviceState *wm;
794
795     /* Attach a WM8750 to the bus */
796     wm = i2c_create_slave(bus, "wm8750", 0);
797
798     spitz_wm8750_addr(wm, 0, 0);
799     pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_WM,
800                     qemu_allocate_irqs(spitz_wm8750_addr, wm, 1)[0]);
801     /* .. and to the sound interface.  */
802     cpu->i2s->opaque = wm;
803     cpu->i2s->codec_out = wm8750_dac_dat;
804     cpu->i2s->codec_in = wm8750_adc_dat;
805     wm8750_data_req_set(wm, cpu->i2s->data_req, cpu->i2s);
806 #endif
807 }
808
809 static void spitz_akita_i2c_setup(PXA2xxState *cpu)
810 {
811     /* Attach a Max7310 to Akita I2C bus.  */
812     i2c_create_slave(pxa2xx_i2c_bus(cpu->i2c[0]), "max7310",
813                      AKITA_MAX_ADDR);
814 }
815
816 /* Other peripherals */
817
818 static void spitz_out_switch(void *opaque, int line, int level)
819 {
820     switch (line) {
821     case 0:
822         zaurus_printf("Charging %s.\n", level ? "off" : "on");
823         break;
824     case 1:
825         zaurus_printf("Discharging %s.\n", level ? "on" : "off");
826         break;
827     case 2:
828         zaurus_printf("Green LED %s.\n", level ? "on" : "off");
829         break;
830     case 3:
831         zaurus_printf("Orange LED %s.\n", level ? "on" : "off");
832         break;
833     case 4:
834         spitz_bl_bit5(opaque, line, level);
835         break;
836     case 5:
837         spitz_bl_power(opaque, line, level);
838         break;
839     case 6:
840         spitz_adc_temp_on(opaque, line, level);
841         break;
842     }
843 }
844
845 #define SPITZ_SCP_LED_GREEN             1
846 #define SPITZ_SCP_JK_B                  2
847 #define SPITZ_SCP_CHRG_ON               3
848 #define SPITZ_SCP_MUTE_L                4
849 #define SPITZ_SCP_MUTE_R                5
850 #define SPITZ_SCP_CF_POWER              6
851 #define SPITZ_SCP_LED_ORANGE            7
852 #define SPITZ_SCP_JK_A                  8
853 #define SPITZ_SCP_ADC_TEMP_ON           9
854 #define SPITZ_SCP2_IR_ON                1
855 #define SPITZ_SCP2_AKIN_PULLUP          2
856 #define SPITZ_SCP2_BACKLIGHT_CONT       7
857 #define SPITZ_SCP2_BACKLIGHT_ON         8
858 #define SPITZ_SCP2_MIC_BIAS             9
859
860 static void spitz_scoop_gpio_setup(PXA2xxState *cpu,
861                 ScoopInfo *scp0, ScoopInfo *scp1)
862 {
863     qemu_irq *outsignals = qemu_allocate_irqs(spitz_out_switch, cpu, 8);
864
865     scoop_gpio_out_set(scp0, SPITZ_SCP_CHRG_ON, outsignals[0]);
866     scoop_gpio_out_set(scp0, SPITZ_SCP_JK_B, outsignals[1]);
867     scoop_gpio_out_set(scp0, SPITZ_SCP_LED_GREEN, outsignals[2]);
868     scoop_gpio_out_set(scp0, SPITZ_SCP_LED_ORANGE, outsignals[3]);
869
870     if (scp1) {
871         scoop_gpio_out_set(scp1, SPITZ_SCP2_BACKLIGHT_CONT, outsignals[4]);
872         scoop_gpio_out_set(scp1, SPITZ_SCP2_BACKLIGHT_ON, outsignals[5]);
873     }
874
875     scoop_gpio_out_set(scp0, SPITZ_SCP_ADC_TEMP_ON, outsignals[6]);
876 }
877
878 #define SPITZ_GPIO_HSYNC                22
879 #define SPITZ_GPIO_SD_DETECT            9
880 #define SPITZ_GPIO_SD_WP                81
881 #define SPITZ_GPIO_ON_RESET             89
882 #define SPITZ_GPIO_BAT_COVER            90
883 #define SPITZ_GPIO_CF1_IRQ              105
884 #define SPITZ_GPIO_CF1_CD               94
885 #define SPITZ_GPIO_CF2_IRQ              106
886 #define SPITZ_GPIO_CF2_CD               93
887
888 static int spitz_hsync;
889
890 static void spitz_lcd_hsync_handler(void *opaque, int line, int level)
891 {
892     PXA2xxState *cpu = (PXA2xxState *) opaque;
893     qemu_set_irq(pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_HSYNC], spitz_hsync);
894     spitz_hsync ^= 1;
895 }
896
897 static void spitz_gpio_setup(PXA2xxState *cpu, int slots)
898 {
899     qemu_irq lcd_hsync;
900     /*
901      * Bad hack: We toggle the LCD hsync GPIO on every GPIO status
902      * read to satisfy broken guests that poll-wait for hsync.
903      * Simulating a real hsync event would be less practical and
904      * wouldn't guarantee that a guest ever exits the loop.
905      */
906     spitz_hsync = 0;
907     lcd_hsync = qemu_allocate_irqs(spitz_lcd_hsync_handler, cpu, 1)[0];
908     pxa2xx_gpio_read_notifier(cpu->gpio, lcd_hsync);
909     pxa2xx_lcd_vsync_notifier(cpu->lcd, lcd_hsync);
910
911     /* MMC/SD host */
912     pxa2xx_mmci_handlers(cpu->mmc,
913                     pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_SD_WP],
914                     pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_SD_DETECT]);
915
916     /* Battery lock always closed */
917     qemu_irq_raise(pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_BAT_COVER]);
918
919     /* Handle reset */
920     pxa2xx_gpio_out_set(cpu->gpio, SPITZ_GPIO_ON_RESET, cpu->reset);
921
922     /* PCMCIA signals: card's IRQ and Card-Detect */
923     if (slots >= 1)
924         pxa2xx_pcmcia_set_irq_cb(cpu->pcmcia[0],
925                         pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_CF1_IRQ],
926                         pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_CF1_CD]);
927     if (slots >= 2)
928         pxa2xx_pcmcia_set_irq_cb(cpu->pcmcia[1],
929                         pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_CF2_IRQ],
930                         pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_CF2_CD]);
931
932     /* Initialise the screen rotation related signals */
933     spitz_gpio_invert[3] = 0;   /* Always open */
934     if (graphic_rotate) {       /* Tablet mode */
935         spitz_gpio_invert[4] = 0;
936     } else {                    /* Portrait mode */
937         spitz_gpio_invert[4] = 1;
938     }
939     qemu_set_irq(pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_SWA],
940                     spitz_gpio_invert[3]);
941     qemu_set_irq(pxa2xx_gpio_in_get(cpu->gpio)[SPITZ_GPIO_SWB],
942                     spitz_gpio_invert[4]);
943 }
944
945 /* Board init.  */
946 enum spitz_model_e { spitz, akita, borzoi, terrier };
947
948 #define SPITZ_RAM       0x04000000
949 #define SPITZ_ROM       0x00800000
950
951 static struct arm_boot_info spitz_binfo = {
952     .loader_start = PXA2XX_SDRAM_BASE,
953     .ram_size = 0x04000000,
954 };
955
956 static void spitz_common_init(ram_addr_t ram_size,
957                 const char *kernel_filename,
958                 const char *kernel_cmdline, const char *initrd_filename,
959                 const char *cpu_model, enum spitz_model_e model, int arm_id)
960 {
961     PXA2xxState *cpu;
962     ScoopInfo *scp0, *scp1 = NULL;
963
964     if (!cpu_model)
965         cpu_model = (model == terrier) ? "pxa270-c5" : "pxa270-c0";
966
967     /* Setup CPU & memory */
968     cpu = pxa270_init(spitz_binfo.ram_size, cpu_model);
969
970     sl_flash_register(cpu, (model == spitz) ? FLASH_128M : FLASH_1024M);
971
972     cpu_register_physical_memory(0, SPITZ_ROM,
973                     qemu_ram_alloc(SPITZ_ROM) | IO_MEM_ROM);
974
975     /* Setup peripherals */
976     spitz_keyboard_register(cpu);
977
978     spitz_ssp_attach(cpu);
979
980     scp0 = scoop_init(cpu, 0, 0x10800000);
981     if (model != akita) {
982             scp1 = scoop_init(cpu, 1, 0x08800040);
983     }
984
985     spitz_scoop_gpio_setup(cpu, scp0, scp1);
986
987     spitz_gpio_setup(cpu, (model == akita) ? 1 : 2);
988
989     spitz_i2c_setup(cpu);
990
991     if (model == akita)
992         spitz_akita_i2c_setup(cpu);
993
994     if (model == terrier)
995         /* A 6.0 GB microdrive is permanently sitting in CF slot 1.  */
996         spitz_microdrive_attach(cpu, 1);
997     else if (model != akita)
998         /* A 4.0 GB microdrive is permanently sitting in CF slot 0.  */
999         spitz_microdrive_attach(cpu, 0);
1000
1001     /* Setup initial (reset) machine state */
1002     cpu->env->regs[15] = spitz_binfo.loader_start;
1003
1004     spitz_binfo.kernel_filename = kernel_filename;
1005     spitz_binfo.kernel_cmdline = kernel_cmdline;
1006     spitz_binfo.initrd_filename = initrd_filename;
1007     spitz_binfo.board_id = arm_id;
1008     arm_load_kernel(cpu->env, &spitz_binfo);
1009     sl_bootparam_write(SL_PXA_PARAM_BASE);
1010 }
1011
1012 static void spitz_init(ram_addr_t ram_size,
1013                 const char *boot_device,
1014                 const char *kernel_filename, const char *kernel_cmdline,
1015                 const char *initrd_filename, const char *cpu_model)
1016 {
1017     spitz_common_init(ram_size, kernel_filename,
1018                 kernel_cmdline, initrd_filename, cpu_model, spitz, 0x2c9);
1019 }
1020
1021 static void borzoi_init(ram_addr_t ram_size,
1022                 const char *boot_device,
1023                 const char *kernel_filename, const char *kernel_cmdline,
1024                 const char *initrd_filename, const char *cpu_model)
1025 {
1026     spitz_common_init(ram_size, kernel_filename,
1027                 kernel_cmdline, initrd_filename, cpu_model, borzoi, 0x33f);
1028 }
1029
1030 static void akita_init(ram_addr_t ram_size,
1031                 const char *boot_device,
1032                 const char *kernel_filename, const char *kernel_cmdline,
1033                 const char *initrd_filename, const char *cpu_model)
1034 {
1035     spitz_common_init(ram_size, kernel_filename,
1036                 kernel_cmdline, initrd_filename, cpu_model, akita, 0x2e8);
1037 }
1038
1039 static void terrier_init(ram_addr_t ram_size,
1040                 const char *boot_device,
1041                 const char *kernel_filename, const char *kernel_cmdline,
1042                 const char *initrd_filename, const char *cpu_model)
1043 {
1044     spitz_common_init(ram_size, kernel_filename,
1045                 kernel_cmdline, initrd_filename, cpu_model, terrier, 0x33f);
1046 }
1047
1048 QEMUMachine akitapda_machine = {
1049     .name = "akita",
1050     .desc = "Akita PDA (PXA270)",
1051     .init = akita_init,
1052 };
1053
1054 static QEMUMachine spitzpda_machine = {
1055     .name = "spitz",
1056     .desc = "Spitz PDA (PXA270)",
1057     .init = spitz_init,
1058 };
1059
1060 static QEMUMachine borzoipda_machine = {
1061     .name = "borzoi",
1062     .desc = "Borzoi PDA (PXA270)",
1063     .init = borzoi_init,
1064 };
1065
1066 static QEMUMachine terrierpda_machine = {
1067     .name = "terrier",
1068     .desc = "Terrier PDA (PXA270)",
1069     .init = terrier_init,
1070 };
1071
1072 static void spitz_machine_init(void)
1073 {
1074     qemu_register_machine(&akitapda_machine);
1075     qemu_register_machine(&spitzpda_machine);
1076     qemu_register_machine(&borzoipda_machine);
1077     qemu_register_machine(&terrierpda_machine);
1078 }
1079
1080 machine_init(spitz_machine_init);
1081
1082 static SSISlaveInfo corgi_ssp_info = {
1083     .init = corgi_ssp_init,
1084     .transfer = corgi_ssp_transfer
1085 };
1086
1087 static SSISlaveInfo spitz_lcdtg_info = {
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", sizeof(CorgiSSPState), &corgi_ssp_info);
1095     ssi_register_slave("spitz-lcdtg", sizeof(SpitzLCDTG), &spitz_lcdtg_info);
1096 }
1097
1098 device_init(spitz_register_devices)