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