xref: /openbmc/qemu/hw/arm/z2.c (revision 7682ecd4)
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.
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.
12  */
13 
14 #include "qemu/osdep.h"
15 #include "qemu/units.h"
16 #include "hw/arm/pxa.h"
17 #include "hw/arm/boot.h"
18 #include "hw/i2c/i2c.h"
19 #include "hw/irq.h"
20 #include "hw/ssi/ssi.h"
21 #include "migration/vmstate.h"
22 #include "hw/boards.h"
23 #include "hw/block/flash.h"
24 #include "ui/console.h"
25 #include "hw/audio/wm8750.h"
26 #include "audio/audio.h"
27 #include "exec/address-spaces.h"
28 #include "qom/object.h"
29 #include "qapi/error.h"
30 #include "trace.h"
31 
32 static const 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 struct ZipitLCD {
101     SSIPeripheral ssidev;
102     int32_t selected;
103     int32_t enabled;
104     uint8_t buf[3];
105     uint32_t cur_reg;
106     int pos;
107 };
108 
109 #define TYPE_ZIPIT_LCD "zipit-lcd"
110 OBJECT_DECLARE_SIMPLE_TYPE(ZipitLCD, ZIPIT_LCD)
111 
112 static uint32_t zipit_lcd_transfer(SSIPeripheral *dev, uint32_t value)
113 {
114     ZipitLCD *z = ZIPIT_LCD(dev);
115     uint16_t val;
116 
117     trace_z2_lcd_reg_update(z->cur_reg, z->buf[0], z->buf[1], z->buf[2], value);
118     if (z->selected) {
119         z->buf[z->pos] = value & 0xff;
120         z->pos++;
121     }
122     if (z->pos == 3) {
123         switch (z->buf[0]) {
124         case 0x74:
125             z->cur_reg = z->buf[2];
126             break;
127         case 0x76:
128             val = z->buf[1] << 8 | z->buf[2];
129             if (z->cur_reg == 0x22 && val == 0x0000) {
130                 z->enabled = 1;
131                 trace_z2_lcd_enable_disable_result("enabled");
132             } else if (z->cur_reg == 0x10 && val == 0x0000) {
133                 z->enabled = 0;
134                 trace_z2_lcd_enable_disable_result("disabled");
135             }
136             break;
137         default:
138             break;
139         }
140         z->pos = 0;
141     }
142     return 0;
143 }
144 
145 static void z2_lcd_cs(void *opaque, int line, int level)
146 {
147     ZipitLCD *z2_lcd = opaque;
148     z2_lcd->selected = !level;
149 }
150 
151 static void zipit_lcd_realize(SSIPeripheral *dev, Error **errp)
152 {
153     ZipitLCD *z = ZIPIT_LCD(dev);
154     z->selected = 0;
155     z->enabled = 0;
156     z->pos = 0;
157 }
158 
159 static const VMStateDescription vmstate_zipit_lcd_state = {
160     .name = "zipit-lcd",
161     .version_id = 2,
162     .minimum_version_id = 2,
163     .fields = (const VMStateField[]) {
164         VMSTATE_SSI_PERIPHERAL(ssidev, ZipitLCD),
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 void zipit_lcd_class_init(ObjectClass *klass, void *data)
175 {
176     DeviceClass *dc = DEVICE_CLASS(klass);
177     SSIPeripheralClass *k = SSI_PERIPHERAL_CLASS(klass);
178 
179     k->realize = zipit_lcd_realize;
180     k->transfer = zipit_lcd_transfer;
181     dc->vmsd = &vmstate_zipit_lcd_state;
182 }
183 
184 static const TypeInfo zipit_lcd_info = {
185     .name          = TYPE_ZIPIT_LCD,
186     .parent        = TYPE_SSI_PERIPHERAL,
187     .instance_size = sizeof(ZipitLCD),
188     .class_init    = zipit_lcd_class_init,
189 };
190 
191 #define TYPE_AER915 "aer915"
192 OBJECT_DECLARE_SIMPLE_TYPE(AER915State, AER915)
193 
194 struct AER915State {
195     I2CSlave parent_obj;
196 
197     int len;
198     uint8_t buf[3];
199 };
200 
201 static int aer915_send(I2CSlave *i2c, uint8_t data)
202 {
203     AER915State *s = AER915(i2c);
204 
205     s->buf[s->len] = data;
206     if (s->len++ > 2) {
207         trace_z2_aer915_send_too_long(s->len);
208         return 1;
209     }
210 
211     if (s->len == 2) {
212         trace_z2_aer915_send(s->buf[0], s->buf[1]);
213     }
214 
215     return 0;
216 }
217 
218 static int aer915_event(I2CSlave *i2c, enum i2c_event event)
219 {
220     AER915State *s = AER915(i2c);
221 
222     trace_z2_aer915_event(s->len, event);
223     switch (event) {
224     case I2C_START_SEND:
225         s->len = 0;
226         break;
227     case I2C_START_RECV:
228         break;
229     case I2C_FINISH:
230         break;
231     default:
232         break;
233     }
234 
235     return 0;
236 }
237 
238 static uint8_t aer915_recv(I2CSlave *slave)
239 {
240     AER915State *s = AER915(slave);
241     int retval = 0x00;
242 
243     switch (s->buf[0]) {
244     /* Return hardcoded battery voltage,
245      * 0xf0 means ~4.1V
246      */
247     case 0x02:
248         retval = 0xf0;
249         break;
250     /* Return 0x00 for other regs,
251      * we don't know what they are for,
252      * anyway they return 0x00 on real hardware.
253      */
254     default:
255         break;
256     }
257 
258     return retval;
259 }
260 
261 static const VMStateDescription vmstate_aer915_state = {
262     .name = "aer915",
263     .version_id = 1,
264     .minimum_version_id = 1,
265     .fields = (const VMStateField[]) {
266         VMSTATE_INT32(len, AER915State),
267         VMSTATE_BUFFER(buf, AER915State),
268         VMSTATE_END_OF_LIST(),
269     }
270 };
271 
272 static void aer915_class_init(ObjectClass *klass, void *data)
273 {
274     DeviceClass *dc = DEVICE_CLASS(klass);
275     I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
276 
277     k->event = aer915_event;
278     k->recv = aer915_recv;
279     k->send = aer915_send;
280     dc->vmsd = &vmstate_aer915_state;
281 }
282 
283 static const TypeInfo aer915_info = {
284     .name          = TYPE_AER915,
285     .parent        = TYPE_I2C_SLAVE,
286     .instance_size = sizeof(AER915State),
287     .class_init    = aer915_class_init,
288 };
289 
290 #define FLASH_SECTOR_SIZE   (64 * KiB)
291 
292 static void z2_init(MachineState *machine)
293 {
294     PXA2xxState *mpu;
295     DriveInfo *dinfo;
296     void *z2_lcd;
297     I2CBus *bus;
298     DeviceState *wm;
299     I2CSlave *i2c_dev;
300 
301     /* Setup CPU & memory */
302     mpu = pxa270_init(z2_binfo.ram_size, machine->cpu_type);
303 
304     dinfo = drive_get(IF_PFLASH, 0, 0);
305     pflash_cfi01_register(Z2_FLASH_BASE, "z2.flash0", Z2_FLASH_SIZE,
306                           dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
307                           FLASH_SECTOR_SIZE, 4, 0, 0, 0, 0, 0);
308 
309     /* setup keypad */
310     pxa27x_register_keypad(mpu->kp, map, 0x100);
311 
312     /* MMC/SD host */
313     pxa2xx_mmci_handlers(mpu->mmc,
314         NULL,
315         qdev_get_gpio_in(mpu->gpio, Z2_GPIO_SD_DETECT));
316 
317     type_register_static(&zipit_lcd_info);
318     type_register_static(&aer915_info);
319     z2_lcd = ssi_create_peripheral(mpu->ssp[1], TYPE_ZIPIT_LCD);
320     bus = pxa2xx_i2c_bus(mpu->i2c[0]);
321 
322     i2c_slave_create_simple(bus, TYPE_AER915, 0x55);
323 
324     i2c_dev = i2c_slave_new(TYPE_WM8750, 0x1b);
325     wm = DEVICE(i2c_dev);
326 
327     if (machine->audiodev) {
328         qdev_prop_set_string(wm, "audiodev", machine->audiodev);
329     }
330     i2c_slave_realize_and_unref(i2c_dev, bus, &error_abort);
331 
332     mpu->i2s->opaque = wm;
333     mpu->i2s->codec_out = wm8750_dac_dat;
334     mpu->i2s->codec_in = wm8750_adc_dat;
335     wm8750_data_req_set(wm, mpu->i2s->data_req, mpu->i2s);
336 
337     qdev_connect_gpio_out(mpu->gpio, Z2_GPIO_LCD_CS,
338                           qemu_allocate_irq(z2_lcd_cs, z2_lcd, 0));
339 
340     z2_binfo.board_id = 0x6dd;
341     arm_load_kernel(mpu->cpu, machine, &z2_binfo);
342 }
343 
344 static void z2_machine_init(MachineClass *mc)
345 {
346     mc->desc = "Zipit Z2 (PXA27x)";
347     mc->init = z2_init;
348     mc->ignore_memory_transaction_failures = true;
349     mc->default_cpu_type = ARM_CPU_TYPE_NAME("pxa270-c5");
350     mc->deprecation_reason = "machine is old and unmaintained";
351 
352     machine_add_audiodev_property(mc);
353 }
354 
355 DEFINE_MACHINE("z2", z2_machine_init)
356