]> Git Repo - qemu.git/blob - hw/z2.c
Merge remote-tracking branch 'qmp/queue/qmp' into staging
[qemu.git] / hw / z2.c
1 /*
2  * PXA270-based Zipit Z2 device
3  *
4  * Copyright (c) 2011 by Vasily Khoruzhick <[email protected]>
5  *
6  * Code is based on mainstone platform.
7  *
8  * This code is licensed under the GNU GPL v2.
9  */
10
11 #include "hw.h"
12 #include "pxa.h"
13 #include "arm-misc.h"
14 #include "devices.h"
15 #include "i2c.h"
16 #include "ssi.h"
17 #include "boards.h"
18 #include "sysemu.h"
19 #include "flash.h"
20 #include "blockdev.h"
21 #include "console.h"
22 #include "audio/audio.h"
23 #include "exec-memory.h"
24
25 #ifdef DEBUG_Z2
26 #define DPRINTF(fmt, ...) \
27         printf(fmt, ## __VA_ARGS__)
28 #else
29 #define DPRINTF(fmt, ...)
30 #endif
31
32 static struct keymap map[0x100] = {
33     [0 ... 0xff] = { -1, -1 },
34     [0x3b] = {0, 0}, /* Option = F1 */
35     [0xc8] = {0, 1}, /* Up */
36     [0xd0] = {0, 2}, /* Down */
37     [0xcb] = {0, 3}, /* Left */
38     [0xcd] = {0, 4}, /* Right */
39     [0xcf] = {0, 5}, /* End */
40     [0x0d] = {0, 6}, /* KPPLUS */
41     [0xc7] = {1, 0}, /* Home */
42     [0x10] = {1, 1}, /* Q */
43     [0x17] = {1, 2}, /* I */
44     [0x22] = {1, 3}, /* G */
45     [0x2d] = {1, 4}, /* X */
46     [0x1c] = {1, 5}, /* Enter */
47     [0x0c] = {1, 6}, /* KPMINUS */
48     [0xc9] = {2, 0}, /* PageUp */
49     [0x11] = {2, 1}, /* W */
50     [0x18] = {2, 2}, /* O */
51     [0x23] = {2, 3}, /* H */
52     [0x2e] = {2, 4}, /* C */
53     [0x38] = {2, 5}, /* LeftAlt */
54     [0xd1] = {3, 0}, /* PageDown */
55     [0x12] = {3, 1}, /* E */
56     [0x19] = {3, 2}, /* P */
57     [0x24] = {3, 3}, /* J */
58     [0x2f] = {3, 4}, /* V */
59     [0x2a] = {3, 5}, /* LeftShift */
60     [0x01] = {4, 0}, /* Esc */
61     [0x13] = {4, 1}, /* R */
62     [0x1e] = {4, 2}, /* A */
63     [0x25] = {4, 3}, /* K */
64     [0x30] = {4, 4}, /* B */
65     [0x1d] = {4, 5}, /* LeftCtrl */
66     [0x0f] = {5, 0}, /* Tab */
67     [0x14] = {5, 1}, /* T */
68     [0x1f] = {5, 2}, /* S */
69     [0x26] = {5, 3}, /* L */
70     [0x31] = {5, 4}, /* N */
71     [0x39] = {5, 5}, /* Space */
72     [0x3c] = {6, 0}, /* Stop = F2 */
73     [0x15] = {6, 1}, /* Y */
74     [0x20] = {6, 2}, /* D */
75     [0x0e] = {6, 3}, /* Backspace */
76     [0x32] = {6, 4}, /* M */
77     [0x33] = {6, 5}, /* Comma */
78     [0x3d] = {7, 0}, /* Play = F3 */
79     [0x16] = {7, 1}, /* U */
80     [0x21] = {7, 2}, /* F */
81     [0x2c] = {7, 3}, /* Z */
82     [0x27] = {7, 4}, /* Semicolon */
83     [0x34] = {7, 5}, /* Dot */
84 };
85
86 #define Z2_RAM_SIZE     0x02000000
87 #define Z2_FLASH_BASE   0x00000000
88 #define Z2_FLASH_SIZE   0x00800000
89
90 static struct arm_boot_info z2_binfo = {
91     .loader_start   = PXA2XX_SDRAM_BASE,
92     .ram_size       = Z2_RAM_SIZE,
93 };
94
95 #define Z2_GPIO_SD_DETECT   96
96 #define Z2_GPIO_AC_IN       0
97 #define Z2_GPIO_KEY_ON      1
98 #define Z2_GPIO_LCD_CS      88
99
100 typedef struct {
101     SSISlave ssidev;
102     int32_t selected;
103     int32_t enabled;
104     uint8_t buf[3];
105     uint32_t cur_reg;
106     int pos;
107 } ZipitLCD;
108
109 static uint32_t zipit_lcd_transfer(SSISlave *dev, uint32_t value)
110 {
111     ZipitLCD *z = FROM_SSI_SLAVE(ZipitLCD, dev);
112     uint16_t val;
113     if (z->selected) {
114         z->buf[z->pos] = value & 0xff;
115         z->pos++;
116     }
117     if (z->pos == 3) {
118         switch (z->buf[0]) {
119         case 0x74:
120             DPRINTF("%s: reg: 0x%.2x\n", __func__, z->buf[2]);
121             z->cur_reg = z->buf[2];
122             break;
123         case 0x76:
124             val = z->buf[1] << 8 | z->buf[2];
125             DPRINTF("%s: value: 0x%.4x\n", __func__, val);
126             if (z->cur_reg == 0x22 && val == 0x0000) {
127                 z->enabled = 1;
128                 printf("%s: LCD enabled\n", __func__);
129             } else if (z->cur_reg == 0x10 && val == 0x0000) {
130                 z->enabled = 0;
131                 printf("%s: LCD disabled\n", __func__);
132             }
133             break;
134         default:
135             DPRINTF("%s: unknown command!\n", __func__);
136             break;
137         }
138         z->pos = 0;
139     }
140     return 0;
141 }
142
143 static void z2_lcd_cs(void *opaque, int line, int level)
144 {
145     ZipitLCD *z2_lcd = opaque;
146     z2_lcd->selected = !level;
147 }
148
149 static int zipit_lcd_init(SSISlave *dev)
150 {
151     ZipitLCD *z = FROM_SSI_SLAVE(ZipitLCD, dev);
152     z->selected = 0;
153     z->enabled = 0;
154     z->pos = 0;
155
156     return 0;
157 }
158
159 static VMStateDescription vmstate_zipit_lcd_state = {
160     .name = "zipit-lcd",
161     .version_id = 1,
162     .minimum_version_id = 1,
163     .minimum_version_id_old = 1,
164     .fields = (VMStateField[]) {
165         VMSTATE_INT32(selected, ZipitLCD),
166         VMSTATE_INT32(enabled, ZipitLCD),
167         VMSTATE_BUFFER(buf, ZipitLCD),
168         VMSTATE_UINT32(cur_reg, ZipitLCD),
169         VMSTATE_INT32(pos, ZipitLCD),
170         VMSTATE_END_OF_LIST(),
171     }
172 };
173
174 static SSISlaveInfo zipit_lcd_info = {
175     .qdev.name = "zipit-lcd",
176     .qdev.size = sizeof(ZipitLCD),
177     .qdev.vmsd = &vmstate_zipit_lcd_state,
178     .init = zipit_lcd_init,
179     .transfer = zipit_lcd_transfer
180 };
181
182 typedef struct {
183     i2c_slave i2c;
184     int len;
185     uint8_t buf[3];
186 } AER915State;
187
188 static int aer915_send(i2c_slave *i2c, uint8_t data)
189 {
190     AER915State *s = FROM_I2C_SLAVE(AER915State, i2c);
191     s->buf[s->len] = data;
192     if (s->len++ > 2) {
193         DPRINTF("%s: message too long (%i bytes)\n",
194             __func__, s->len);
195         return 1;
196     }
197
198     if (s->len == 2) {
199         DPRINTF("%s: reg %d value 0x%02x\n", __func__,
200                 s->buf[0], s->buf[1]);
201     }
202
203     return 0;
204 }
205
206 static void aer915_event(i2c_slave *i2c, enum i2c_event event)
207 {
208     AER915State *s = FROM_I2C_SLAVE(AER915State, i2c);
209     switch (event) {
210     case I2C_START_SEND:
211         s->len = 0;
212         break;
213     case I2C_START_RECV:
214         if (s->len != 1) {
215             DPRINTF("%s: short message!?\n", __func__);
216         }
217         break;
218     case I2C_FINISH:
219         break;
220     default:
221         break;
222     }
223 }
224
225 static int aer915_recv(i2c_slave *slave)
226 {
227     int retval = 0x00;
228     AER915State *s = FROM_I2C_SLAVE(AER915State, slave);
229
230     switch (s->buf[0]) {
231     /* Return hardcoded battery voltage,
232      * 0xf0 means ~4.1V
233      */
234     case 0x02:
235         retval = 0xf0;
236         break;
237     /* Return 0x00 for other regs,
238      * we don't know what they are for,
239      * anyway they return 0x00 on real hardware.
240      */
241     default:
242         break;
243     }
244
245     return retval;
246 }
247
248 static int aer915_init(i2c_slave *i2c)
249 {
250     /* Nothing to do.  */
251     return 0;
252 }
253
254 static VMStateDescription vmstate_aer915_state = {
255     .name = "aer915",
256     .version_id = 1,
257     .minimum_version_id = 1,
258     .minimum_version_id_old = 1,
259     .fields = (VMStateField[]) {
260         VMSTATE_INT32(len, AER915State),
261         VMSTATE_BUFFER(buf, AER915State),
262         VMSTATE_END_OF_LIST(),
263     }
264 };
265
266 static I2CSlaveInfo aer915_info = {
267     .qdev.name = "aer915",
268     .qdev.size = sizeof(AER915State),
269     .qdev.vmsd = &vmstate_aer915_state,
270     .init = aer915_init,
271     .event = aer915_event,
272     .recv = aer915_recv,
273     .send = aer915_send
274 };
275
276 static void z2_init(ram_addr_t ram_size,
277                 const char *boot_device,
278                 const char *kernel_filename, const char *kernel_cmdline,
279                 const char *initrd_filename, const char *cpu_model)
280 {
281     MemoryRegion *address_space_mem = get_system_memory();
282     uint32_t sector_len = 0x10000;
283     PXA2xxState *cpu;
284     DriveInfo *dinfo;
285     int be;
286     void *z2_lcd;
287     i2c_bus *bus;
288     DeviceState *wm;
289
290     if (!cpu_model) {
291         cpu_model = "pxa270-c5";
292     }
293
294     /* Setup CPU & memory */
295     cpu = pxa270_init(address_space_mem, z2_binfo.ram_size, cpu_model);
296
297 #ifdef TARGET_WORDS_BIGENDIAN
298     be = 1;
299 #else
300     be = 0;
301 #endif
302     dinfo = drive_get(IF_PFLASH, 0, 0);
303     if (!dinfo) {
304         fprintf(stderr, "Flash image must be given with the "
305                 "'pflash' parameter\n");
306         exit(1);
307     }
308
309     if (!pflash_cfi01_register(Z2_FLASH_BASE,
310                                NULL, "z2.flash0", Z2_FLASH_SIZE,
311                                dinfo->bdrv, sector_len,
312                                Z2_FLASH_SIZE / sector_len, 4, 0, 0, 0, 0,
313                                be)) {
314         fprintf(stderr, "qemu: Error registering flash memory.\n");
315         exit(1);
316     }
317
318     /* setup keypad */
319     pxa27x_register_keypad(cpu->kp, map, 0x100);
320
321     /* MMC/SD host */
322     pxa2xx_mmci_handlers(cpu->mmc,
323         NULL,
324         qdev_get_gpio_in(cpu->gpio, Z2_GPIO_SD_DETECT));
325
326     ssi_register_slave(&zipit_lcd_info);
327     i2c_register_slave(&aer915_info);
328     z2_lcd = ssi_create_slave(cpu->ssp[1], "zipit-lcd");
329     bus = pxa2xx_i2c_bus(cpu->i2c[0]);
330     i2c_create_slave(bus, "aer915", 0x55);
331     wm = i2c_create_slave(bus, "wm8750", 0x1b);
332     cpu->i2s->opaque = wm;
333     cpu->i2s->codec_out = wm8750_dac_dat;
334     cpu->i2s->codec_in = wm8750_adc_dat;
335     wm8750_data_req_set(wm, cpu->i2s->data_req, cpu->i2s);
336
337     qdev_connect_gpio_out(cpu->gpio, Z2_GPIO_LCD_CS,
338         qemu_allocate_irqs(z2_lcd_cs, z2_lcd, 1)[0]);
339
340     if (kernel_filename) {
341         z2_binfo.kernel_filename = kernel_filename;
342         z2_binfo.kernel_cmdline = kernel_cmdline;
343         z2_binfo.initrd_filename = initrd_filename;
344         z2_binfo.board_id = 0x6dd;
345         arm_load_kernel(cpu->env, &z2_binfo);
346     }
347 }
348
349 static QEMUMachine z2_machine = {
350     .name = "z2",
351     .desc = "Zipit Z2 (PXA27x)",
352     .init = z2_init,
353 };
354
355 static void z2_machine_init(void)
356 {
357     qemu_register_machine(&z2_machine);
358 }
359
360 machine_init(z2_machine_init);
This page took 0.0440970000000001 seconds and 4 git commands to generate.