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