2 * PXA270-based Zipit Z2 device
4 * Copyright (c) 2011 by Vasily Khoruzhick <anarsoul@gmail.com>
6 * Code is based on mainstone platform.
8 * This code is licensed under the GNU GPL v2.
10 * Contributions after 2012-01-13 are licensed under the terms of the
11 * GNU GPL, version 2 or (at your option) any later version.
14 #include "qemu/osdep.h"
15 #include "hw/arm/pxa.h"
16 #include "hw/arm/boot.h"
17 #include "hw/i2c/i2c.h"
19 #include "hw/ssi/ssi.h"
20 #include "migration/vmstate.h"
21 #include "hw/boards.h"
22 #include "hw/block/flash.h"
23 #include "ui/console.h"
24 #include "hw/audio/wm8750.h"
25 #include "audio/audio.h"
26 #include "exec/address-spaces.h"
27 #include "sysemu/qtest.h"
31 #define DPRINTF(fmt, ...) \
32 printf(fmt, ## __VA_ARGS__)
34 #define DPRINTF(fmt, ...)
37 static const struct keymap map
[0x100] = {
38 [0 ... 0xff] = { -1, -1 },
39 [0x3b] = {0, 0}, /* Option = F1 */
40 [0xc8] = {0, 1}, /* Up */
41 [0xd0] = {0, 2}, /* Down */
42 [0xcb] = {0, 3}, /* Left */
43 [0xcd] = {0, 4}, /* Right */
44 [0xcf] = {0, 5}, /* End */
45 [0x0d] = {0, 6}, /* KPPLUS */
46 [0xc7] = {1, 0}, /* Home */
47 [0x10] = {1, 1}, /* Q */
48 [0x17] = {1, 2}, /* I */
49 [0x22] = {1, 3}, /* G */
50 [0x2d] = {1, 4}, /* X */
51 [0x1c] = {1, 5}, /* Enter */
52 [0x0c] = {1, 6}, /* KPMINUS */
53 [0xc9] = {2, 0}, /* PageUp */
54 [0x11] = {2, 1}, /* W */
55 [0x18] = {2, 2}, /* O */
56 [0x23] = {2, 3}, /* H */
57 [0x2e] = {2, 4}, /* C */
58 [0x38] = {2, 5}, /* LeftAlt */
59 [0xd1] = {3, 0}, /* PageDown */
60 [0x12] = {3, 1}, /* E */
61 [0x19] = {3, 2}, /* P */
62 [0x24] = {3, 3}, /* J */
63 [0x2f] = {3, 4}, /* V */
64 [0x2a] = {3, 5}, /* LeftShift */
65 [0x01] = {4, 0}, /* Esc */
66 [0x13] = {4, 1}, /* R */
67 [0x1e] = {4, 2}, /* A */
68 [0x25] = {4, 3}, /* K */
69 [0x30] = {4, 4}, /* B */
70 [0x1d] = {4, 5}, /* LeftCtrl */
71 [0x0f] = {5, 0}, /* Tab */
72 [0x14] = {5, 1}, /* T */
73 [0x1f] = {5, 2}, /* S */
74 [0x26] = {5, 3}, /* L */
75 [0x31] = {5, 4}, /* N */
76 [0x39] = {5, 5}, /* Space */
77 [0x3c] = {6, 0}, /* Stop = F2 */
78 [0x15] = {6, 1}, /* Y */
79 [0x20] = {6, 2}, /* D */
80 [0x0e] = {6, 3}, /* Backspace */
81 [0x32] = {6, 4}, /* M */
82 [0x33] = {6, 5}, /* Comma */
83 [0x3d] = {7, 0}, /* Play = F3 */
84 [0x16] = {7, 1}, /* U */
85 [0x21] = {7, 2}, /* F */
86 [0x2c] = {7, 3}, /* Z */
87 [0x27] = {7, 4}, /* Semicolon */
88 [0x34] = {7, 5}, /* Dot */
91 #define Z2_RAM_SIZE 0x02000000
92 #define Z2_FLASH_BASE 0x00000000
93 #define Z2_FLASH_SIZE 0x00800000
95 static struct arm_boot_info z2_binfo
= {
96 .loader_start
= PXA2XX_SDRAM_BASE
,
97 .ram_size
= Z2_RAM_SIZE
,
100 #define Z2_GPIO_SD_DETECT 96
101 #define Z2_GPIO_AC_IN 0
102 #define Z2_GPIO_KEY_ON 1
103 #define Z2_GPIO_LCD_CS 88
114 static uint32_t zipit_lcd_transfer(SSISlave
*dev
, uint32_t value
)
116 ZipitLCD
*z
= FROM_SSI_SLAVE(ZipitLCD
, dev
);
119 z
->buf
[z
->pos
] = value
& 0xff;
125 DPRINTF("%s: reg: 0x%.2x\n", __func__
, z
->buf
[2]);
126 z
->cur_reg
= z
->buf
[2];
129 val
= z
->buf
[1] << 8 | z
->buf
[2];
130 DPRINTF("%s: value: 0x%.4x\n", __func__
, val
);
131 if (z
->cur_reg
== 0x22 && val
== 0x0000) {
133 printf("%s: LCD enabled\n", __func__
);
134 } else if (z
->cur_reg
== 0x10 && val
== 0x0000) {
136 printf("%s: LCD disabled\n", __func__
);
140 DPRINTF("%s: unknown command!\n", __func__
);
148 static void z2_lcd_cs(void *opaque
, int line
, int level
)
150 ZipitLCD
*z2_lcd
= opaque
;
151 z2_lcd
->selected
= !level
;
154 static void zipit_lcd_realize(SSISlave
*dev
, Error
**errp
)
156 ZipitLCD
*z
= FROM_SSI_SLAVE(ZipitLCD
, dev
);
162 static VMStateDescription vmstate_zipit_lcd_state
= {
165 .minimum_version_id
= 2,
166 .fields
= (VMStateField
[]) {
167 VMSTATE_SSI_SLAVE(ssidev
, ZipitLCD
),
168 VMSTATE_INT32(selected
, ZipitLCD
),
169 VMSTATE_INT32(enabled
, ZipitLCD
),
170 VMSTATE_BUFFER(buf
, ZipitLCD
),
171 VMSTATE_UINT32(cur_reg
, ZipitLCD
),
172 VMSTATE_INT32(pos
, ZipitLCD
),
173 VMSTATE_END_OF_LIST(),
177 static void zipit_lcd_class_init(ObjectClass
*klass
, void *data
)
179 DeviceClass
*dc
= DEVICE_CLASS(klass
);
180 SSISlaveClass
*k
= SSI_SLAVE_CLASS(klass
);
182 k
->realize
= zipit_lcd_realize
;
183 k
->transfer
= zipit_lcd_transfer
;
184 dc
->vmsd
= &vmstate_zipit_lcd_state
;
187 static const TypeInfo zipit_lcd_info
= {
189 .parent
= TYPE_SSI_SLAVE
,
190 .instance_size
= sizeof(ZipitLCD
),
191 .class_init
= zipit_lcd_class_init
,
194 #define TYPE_AER915 "aer915"
195 #define AER915(obj) OBJECT_CHECK(AER915State, (obj), TYPE_AER915)
197 typedef struct AER915State
{
204 static int aer915_send(I2CSlave
*i2c
, uint8_t data
)
206 AER915State
*s
= AER915(i2c
);
208 s
->buf
[s
->len
] = data
;
210 DPRINTF("%s: message too long (%i bytes)\n",
216 DPRINTF("%s: reg %d value 0x%02x\n", __func__
,
217 s
->buf
[0], s
->buf
[1]);
223 static int aer915_event(I2CSlave
*i2c
, enum i2c_event event
)
225 AER915State
*s
= AER915(i2c
);
233 DPRINTF("%s: short message!?\n", __func__
);
245 static uint8_t aer915_recv(I2CSlave
*slave
)
247 AER915State
*s
= AER915(slave
);
251 /* Return hardcoded battery voltage,
257 /* Return 0x00 for other regs,
258 * we don't know what they are for,
259 * anyway they return 0x00 on real hardware.
268 static VMStateDescription vmstate_aer915_state
= {
271 .minimum_version_id
= 1,
272 .fields
= (VMStateField
[]) {
273 VMSTATE_INT32(len
, AER915State
),
274 VMSTATE_BUFFER(buf
, AER915State
),
275 VMSTATE_END_OF_LIST(),
279 static void aer915_class_init(ObjectClass
*klass
, void *data
)
281 DeviceClass
*dc
= DEVICE_CLASS(klass
);
282 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
284 k
->event
= aer915_event
;
285 k
->recv
= aer915_recv
;
286 k
->send
= aer915_send
;
287 dc
->vmsd
= &vmstate_aer915_state
;
290 static const TypeInfo aer915_info
= {
292 .parent
= TYPE_I2C_SLAVE
,
293 .instance_size
= sizeof(AER915State
),
294 .class_init
= aer915_class_init
,
297 static void z2_init(MachineState
*machine
)
299 const char *kernel_filename
= machine
->kernel_filename
;
300 const char *kernel_cmdline
= machine
->kernel_cmdline
;
301 const char *initrd_filename
= machine
->initrd_filename
;
302 MemoryRegion
*address_space_mem
= get_system_memory();
303 uint32_t sector_len
= 0x10000;
311 /* Setup CPU & memory */
312 mpu
= pxa270_init(address_space_mem
, z2_binfo
.ram_size
, machine
->cpu_type
);
314 #ifdef TARGET_WORDS_BIGENDIAN
319 dinfo
= drive_get(IF_PFLASH
, 0, 0);
320 if (!dinfo
&& !qtest_enabled()) {
321 error_report("Flash image must be given with the "
322 "'pflash' parameter");
326 if (!pflash_cfi01_register(Z2_FLASH_BASE
, "z2.flash0", Z2_FLASH_SIZE
,
327 dinfo
? blk_by_legacy_dinfo(dinfo
) : NULL
,
328 sector_len
, 4, 0, 0, 0, 0, be
)) {
329 error_report("Error registering flash memory");
334 pxa27x_register_keypad(mpu
->kp
, map
, 0x100);
337 pxa2xx_mmci_handlers(mpu
->mmc
,
339 qdev_get_gpio_in(mpu
->gpio
, Z2_GPIO_SD_DETECT
));
341 type_register_static(&zipit_lcd_info
);
342 type_register_static(&aer915_info
);
343 z2_lcd
= ssi_create_slave(mpu
->ssp
[1], "zipit-lcd");
344 bus
= pxa2xx_i2c_bus(mpu
->i2c
[0]);
345 i2c_create_slave(bus
, TYPE_AER915
, 0x55);
346 wm
= i2c_create_slave(bus
, TYPE_WM8750
, 0x1b);
347 mpu
->i2s
->opaque
= wm
;
348 mpu
->i2s
->codec_out
= wm8750_dac_dat
;
349 mpu
->i2s
->codec_in
= wm8750_adc_dat
;
350 wm8750_data_req_set(wm
, mpu
->i2s
->data_req
, mpu
->i2s
);
352 qdev_connect_gpio_out(mpu
->gpio
, Z2_GPIO_LCD_CS
,
353 qemu_allocate_irq(z2_lcd_cs
, z2_lcd
, 0));
355 z2_binfo
.kernel_filename
= kernel_filename
;
356 z2_binfo
.kernel_cmdline
= kernel_cmdline
;
357 z2_binfo
.initrd_filename
= initrd_filename
;
358 z2_binfo
.board_id
= 0x6dd;
359 arm_load_kernel(mpu
->cpu
, &z2_binfo
);
362 static void z2_machine_init(MachineClass
*mc
)
364 mc
->desc
= "Zipit Z2 (PXA27x)";
366 mc
->ignore_memory_transaction_failures
= true;
367 mc
->default_cpu_type
= ARM_CPU_TYPE_NAME("pxa270-c5");
370 DEFINE_MACHINE("z2", z2_machine_init
)