z2.c 9.1 KB

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