]> git.proxmox.com Git - mirror_qemu.git/blame - hw/arm/z2.c
Use DECLARE_*CHECKER* macros
[mirror_qemu.git] / hw / arm / z2.c
CommitLineData
3bf11207
VK
1/*
2 * PXA270-based Zipit Z2 device
3 *
4 * Copyright (c) 2011 by Vasily Khoruzhick <anarsoul@gmail.com>
5 *
6 * Code is based on mainstone platform.
7 *
8 * This code is licensed under the GNU GPL v2.
6b620ca3
PB
9 *
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.
3bf11207
VK
12 */
13
12b16722 14#include "qemu/osdep.h"
0d09e41a 15#include "hw/arm/pxa.h"
12ec8bd5 16#include "hw/arm/boot.h"
0d09e41a 17#include "hw/i2c/i2c.h"
64552b6b 18#include "hw/irq.h"
8fd06719 19#include "hw/ssi/ssi.h"
d6454270 20#include "migration/vmstate.h"
83c9f4ca 21#include "hw/boards.h"
0d09e41a 22#include "hw/block/flash.h"
28ecbaee 23#include "ui/console.h"
7ab14c5a 24#include "hw/audio/wm8750.h"
3bf11207 25#include "audio/audio.h"
022c62cb 26#include "exec/address-spaces.h"
e25ac5f6 27#include "sysemu/qtest.h"
ba1ba5cc 28#include "cpu.h"
db1015e9 29#include "qom/object.h"
3bf11207
VK
30
31#ifdef DEBUG_Z2
32#define DPRINTF(fmt, ...) \
33 printf(fmt, ## __VA_ARGS__)
34#else
35#define DPRINTF(fmt, ...)
36#endif
37
52975c31 38static const struct keymap map[0x100] = {
3bf11207
VK
39 [0 ... 0xff] = { -1, -1 },
40 [0x3b] = {0, 0}, /* Option = F1 */
41 [0xc8] = {0, 1}, /* Up */
42 [0xd0] = {0, 2}, /* Down */
43 [0xcb] = {0, 3}, /* Left */
44 [0xcd] = {0, 4}, /* Right */
45 [0xcf] = {0, 5}, /* End */
46 [0x0d] = {0, 6}, /* KPPLUS */
47 [0xc7] = {1, 0}, /* Home */
48 [0x10] = {1, 1}, /* Q */
49 [0x17] = {1, 2}, /* I */
50 [0x22] = {1, 3}, /* G */
51 [0x2d] = {1, 4}, /* X */
52 [0x1c] = {1, 5}, /* Enter */
53 [0x0c] = {1, 6}, /* KPMINUS */
54 [0xc9] = {2, 0}, /* PageUp */
55 [0x11] = {2, 1}, /* W */
56 [0x18] = {2, 2}, /* O */
57 [0x23] = {2, 3}, /* H */
58 [0x2e] = {2, 4}, /* C */
59 [0x38] = {2, 5}, /* LeftAlt */
60 [0xd1] = {3, 0}, /* PageDown */
61 [0x12] = {3, 1}, /* E */
62 [0x19] = {3, 2}, /* P */
63 [0x24] = {3, 3}, /* J */
64 [0x2f] = {3, 4}, /* V */
65 [0x2a] = {3, 5}, /* LeftShift */
66 [0x01] = {4, 0}, /* Esc */
67 [0x13] = {4, 1}, /* R */
68 [0x1e] = {4, 2}, /* A */
69 [0x25] = {4, 3}, /* K */
70 [0x30] = {4, 4}, /* B */
71 [0x1d] = {4, 5}, /* LeftCtrl */
72 [0x0f] = {5, 0}, /* Tab */
73 [0x14] = {5, 1}, /* T */
74 [0x1f] = {5, 2}, /* S */
75 [0x26] = {5, 3}, /* L */
76 [0x31] = {5, 4}, /* N */
77 [0x39] = {5, 5}, /* Space */
78 [0x3c] = {6, 0}, /* Stop = F2 */
79 [0x15] = {6, 1}, /* Y */
80 [0x20] = {6, 2}, /* D */
81 [0x0e] = {6, 3}, /* Backspace */
82 [0x32] = {6, 4}, /* M */
83 [0x33] = {6, 5}, /* Comma */
84 [0x3d] = {7, 0}, /* Play = F3 */
85 [0x16] = {7, 1}, /* U */
86 [0x21] = {7, 2}, /* F */
87 [0x2c] = {7, 3}, /* Z */
88 [0x27] = {7, 4}, /* Semicolon */
89 [0x34] = {7, 5}, /* Dot */
90};
91
92#define Z2_RAM_SIZE 0x02000000
93#define Z2_FLASH_BASE 0x00000000
94#define Z2_FLASH_SIZE 0x00800000
95
96static struct arm_boot_info z2_binfo = {
97 .loader_start = PXA2XX_SDRAM_BASE,
98 .ram_size = Z2_RAM_SIZE,
99};
100
101#define Z2_GPIO_SD_DETECT 96
102#define Z2_GPIO_AC_IN 0
103#define Z2_GPIO_KEY_ON 1
104#define Z2_GPIO_LCD_CS 88
105
db1015e9 106struct ZipitLCD {
3bf11207
VK
107 SSISlave ssidev;
108 int32_t selected;
109 int32_t enabled;
110 uint8_t buf[3];
111 uint32_t cur_reg;
112 int pos;
db1015e9
EH
113};
114typedef struct ZipitLCD ZipitLCD;
3bf11207 115
213f63df 116#define TYPE_ZIPIT_LCD "zipit-lcd"
8110fa1d
EH
117DECLARE_INSTANCE_CHECKER(ZipitLCD, ZIPIT_LCD,
118 TYPE_ZIPIT_LCD)
213f63df 119
3bf11207
VK
120static uint32_t zipit_lcd_transfer(SSISlave *dev, uint32_t value)
121{
213f63df 122 ZipitLCD *z = ZIPIT_LCD(dev);
3bf11207
VK
123 uint16_t val;
124 if (z->selected) {
125 z->buf[z->pos] = value & 0xff;
126 z->pos++;
127 }
128 if (z->pos == 3) {
129 switch (z->buf[0]) {
130 case 0x74:
131 DPRINTF("%s: reg: 0x%.2x\n", __func__, z->buf[2]);
132 z->cur_reg = z->buf[2];
133 break;
134 case 0x76:
135 val = z->buf[1] << 8 | z->buf[2];
136 DPRINTF("%s: value: 0x%.4x\n", __func__, val);
137 if (z->cur_reg == 0x22 && val == 0x0000) {
138 z->enabled = 1;
139 printf("%s: LCD enabled\n", __func__);
140 } else if (z->cur_reg == 0x10 && val == 0x0000) {
141 z->enabled = 0;
142 printf("%s: LCD disabled\n", __func__);
143 }
144 break;
145 default:
146 DPRINTF("%s: unknown command!\n", __func__);
147 break;
148 }
149 z->pos = 0;
150 }
151 return 0;
152}
153
154static void z2_lcd_cs(void *opaque, int line, int level)
155{
156 ZipitLCD *z2_lcd = opaque;
157 z2_lcd->selected = !level;
158}
159
7673bb4c 160static void zipit_lcd_realize(SSISlave *dev, Error **errp)
3bf11207 161{
213f63df 162 ZipitLCD *z = ZIPIT_LCD(dev);
3bf11207
VK
163 z->selected = 0;
164 z->enabled = 0;
165 z->pos = 0;
3bf11207
VK
166}
167
168static VMStateDescription vmstate_zipit_lcd_state = {
169 .name = "zipit-lcd",
66530953
PC
170 .version_id = 2,
171 .minimum_version_id = 2,
3bf11207 172 .fields = (VMStateField[]) {
66530953 173 VMSTATE_SSI_SLAVE(ssidev, ZipitLCD),
3bf11207
VK
174 VMSTATE_INT32(selected, ZipitLCD),
175 VMSTATE_INT32(enabled, ZipitLCD),
176 VMSTATE_BUFFER(buf, ZipitLCD),
177 VMSTATE_UINT32(cur_reg, ZipitLCD),
178 VMSTATE_INT32(pos, ZipitLCD),
179 VMSTATE_END_OF_LIST(),
180 }
181};
182
cd6c4cf2
AL
183static void zipit_lcd_class_init(ObjectClass *klass, void *data)
184{
39bffca2 185 DeviceClass *dc = DEVICE_CLASS(klass);
cd6c4cf2
AL
186 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
187
7673bb4c 188 k->realize = zipit_lcd_realize;
cd6c4cf2 189 k->transfer = zipit_lcd_transfer;
39bffca2 190 dc->vmsd = &vmstate_zipit_lcd_state;
cd6c4cf2
AL
191}
192
8c43a6f0 193static const TypeInfo zipit_lcd_info = {
213f63df 194 .name = TYPE_ZIPIT_LCD,
39bffca2
AL
195 .parent = TYPE_SSI_SLAVE,
196 .instance_size = sizeof(ZipitLCD),
197 .class_init = zipit_lcd_class_init,
3bf11207
VK
198};
199
a5f96db7 200#define TYPE_AER915 "aer915"
db1015e9 201typedef struct AER915State AER915State;
8110fa1d
EH
202DECLARE_INSTANCE_CHECKER(AER915State, AER915,
203 TYPE_AER915)
a5f96db7 204
db1015e9 205struct AER915State {
a5f96db7
AF
206 I2CSlave parent_obj;
207
3bf11207
VK
208 int len;
209 uint8_t buf[3];
db1015e9 210};
3bf11207 211
9e07bdf8 212static int aer915_send(I2CSlave *i2c, uint8_t data)
3bf11207 213{
a5f96db7
AF
214 AER915State *s = AER915(i2c);
215
3bf11207
VK
216 s->buf[s->len] = data;
217 if (s->len++ > 2) {
218 DPRINTF("%s: message too long (%i bytes)\n",
219 __func__, s->len);
220 return 1;
221 }
222
223 if (s->len == 2) {
224 DPRINTF("%s: reg %d value 0x%02x\n", __func__,
225 s->buf[0], s->buf[1]);
226 }
227
228 return 0;
229}
230
d307c28c 231static int aer915_event(I2CSlave *i2c, enum i2c_event event)
3bf11207 232{
a5f96db7
AF
233 AER915State *s = AER915(i2c);
234
3bf11207
VK
235 switch (event) {
236 case I2C_START_SEND:
237 s->len = 0;
238 break;
239 case I2C_START_RECV:
240 if (s->len != 1) {
241 DPRINTF("%s: short message!?\n", __func__);
242 }
243 break;
244 case I2C_FINISH:
245 break;
246 default:
247 break;
248 }
d307c28c
CM
249
250 return 0;
3bf11207
VK
251}
252
2ac4c5f4 253static uint8_t aer915_recv(I2CSlave *slave)
3bf11207 254{
a5f96db7 255 AER915State *s = AER915(slave);
3bf11207 256 int retval = 0x00;
3bf11207
VK
257
258 switch (s->buf[0]) {
259 /* Return hardcoded battery voltage,
260 * 0xf0 means ~4.1V
261 */
262 case 0x02:
263 retval = 0xf0;
264 break;
265 /* Return 0x00 for other regs,
266 * we don't know what they are for,
267 * anyway they return 0x00 on real hardware.
268 */
269 default:
270 break;
271 }
272
273 return retval;
274}
275
3bf11207
VK
276static VMStateDescription vmstate_aer915_state = {
277 .name = "aer915",
278 .version_id = 1,
279 .minimum_version_id = 1,
3bf11207
VK
280 .fields = (VMStateField[]) {
281 VMSTATE_INT32(len, AER915State),
282 VMSTATE_BUFFER(buf, AER915State),
283 VMSTATE_END_OF_LIST(),
284 }
285};
286
b5ea9327
AL
287static void aer915_class_init(ObjectClass *klass, void *data)
288{
39bffca2 289 DeviceClass *dc = DEVICE_CLASS(klass);
b5ea9327
AL
290 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
291
b5ea9327
AL
292 k->event = aer915_event;
293 k->recv = aer915_recv;
294 k->send = aer915_send;
39bffca2 295 dc->vmsd = &vmstate_aer915_state;
b5ea9327
AL
296}
297
8c43a6f0 298static const TypeInfo aer915_info = {
a5f96db7 299 .name = TYPE_AER915,
39bffca2
AL
300 .parent = TYPE_I2C_SLAVE,
301 .instance_size = sizeof(AER915State),
302 .class_init = aer915_class_init,
3bf11207
VK
303};
304
3ef96221 305static void z2_init(MachineState *machine)
3bf11207 306{
a6dc4c2d 307 MemoryRegion *address_space_mem = get_system_memory();
3bf11207 308 uint32_t sector_len = 0x10000;
5c6f4f17 309 PXA2xxState *mpu;
3bf11207 310 DriveInfo *dinfo;
3bf11207 311 void *z2_lcd;
a5c82852 312 I2CBus *bus;
3bf11207
VK
313 DeviceState *wm;
314
3bf11207 315 /* Setup CPU & memory */
ba1ba5cc 316 mpu = pxa270_init(address_space_mem, z2_binfo.ram_size, machine->cpu_type);
3bf11207 317
3bf11207 318 dinfo = drive_get(IF_PFLASH, 0, 0);
940d5b13 319 if (!pflash_cfi01_register(Z2_FLASH_BASE, "z2.flash0", Z2_FLASH_SIZE,
4be74634 320 dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
56329e38 321 sector_len, 4, 0, 0, 0, 0, 0)) {
c0dbca36 322 error_report("Error registering flash memory");
3bf11207
VK
323 exit(1);
324 }
325
326 /* setup keypad */
5c6f4f17 327 pxa27x_register_keypad(mpu->kp, map, 0x100);
3bf11207
VK
328
329 /* MMC/SD host */
5c6f4f17 330 pxa2xx_mmci_handlers(mpu->mmc,
3bf11207 331 NULL,
5c6f4f17 332 qdev_get_gpio_in(mpu->gpio, Z2_GPIO_SD_DETECT));
3bf11207 333
39bffca2
AL
334 type_register_static(&zipit_lcd_info);
335 type_register_static(&aer915_info);
213f63df 336 z2_lcd = ssi_create_slave(mpu->ssp[1], TYPE_ZIPIT_LCD);
5c6f4f17 337 bus = pxa2xx_i2c_bus(mpu->i2c[0]);
1373b15b
PMD
338 i2c_slave_create_simple(bus, TYPE_AER915, 0x55);
339 wm = DEVICE(i2c_slave_create_simple(bus, TYPE_WM8750, 0x1b));
5c6f4f17
AF
340 mpu->i2s->opaque = wm;
341 mpu->i2s->codec_out = wm8750_dac_dat;
342 mpu->i2s->codec_in = wm8750_adc_dat;
343 wm8750_data_req_set(wm, mpu->i2s->data_req, mpu->i2s);
3bf11207 344
5c6f4f17 345 qdev_connect_gpio_out(mpu->gpio, Z2_GPIO_LCD_CS,
f3c7d038 346 qemu_allocate_irq(z2_lcd_cs, z2_lcd, 0));
3bf11207 347
dacecf54 348 z2_binfo.board_id = 0x6dd;
2744ece8 349 arm_load_kernel(mpu->cpu, machine, &z2_binfo);
3bf11207
VK
350}
351
e264d29d 352static void z2_machine_init(MachineClass *mc)
3bf11207 353{
e264d29d
EH
354 mc->desc = "Zipit Z2 (PXA27x)";
355 mc->init = z2_init;
4672cbd7 356 mc->ignore_memory_transaction_failures = true;
ba1ba5cc 357 mc->default_cpu_type = ARM_CPU_TYPE_NAME("pxa270-c5");
3bf11207
VK
358}
359
e264d29d 360DEFINE_MACHINE("z2", z2_machine_init)