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