z2.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 "hw.h"
  14. #include "pxa.h"
  15. #include "arm-misc.h"
  16. #include "devices.h"
  17. #include "i2c.h"
  18. #include "ssi.h"
  19. #include "boards.h"
  20. #include "sysemu.h"
  21. #include "flash.h"
  22. #include "blockdev.h"
  23. #include "console.h"
  24. #include "audio/audio.h"
  25. #include "exec-memory.h"
  26. #ifdef DEBUG_Z2
  27. #define DPRINTF(fmt, ...) \
  28. printf(fmt, ## __VA_ARGS__)
  29. #else
  30. #define DPRINTF(fmt, ...)
  31. #endif
  32. static 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. #define Z2_RAM_SIZE 0x02000000
  86. #define Z2_FLASH_BASE 0x00000000
  87. #define Z2_FLASH_SIZE 0x00800000
  88. static struct arm_boot_info z2_binfo = {
  89. .loader_start = PXA2XX_SDRAM_BASE,
  90. .ram_size = Z2_RAM_SIZE,
  91. };
  92. #define Z2_GPIO_SD_DETECT 96
  93. #define Z2_GPIO_AC_IN 0
  94. #define Z2_GPIO_KEY_ON 1
  95. #define Z2_GPIO_LCD_CS 88
  96. typedef struct {
  97. SSISlave ssidev;
  98. int32_t selected;
  99. int32_t enabled;
  100. uint8_t buf[3];
  101. uint32_t cur_reg;
  102. int pos;
  103. } ZipitLCD;
  104. static uint32_t zipit_lcd_transfer(SSISlave *dev, uint32_t value)
  105. {
  106. ZipitLCD *z = FROM_SSI_SLAVE(ZipitLCD, dev);
  107. uint16_t val;
  108. if (z->selected) {
  109. z->buf[z->pos] = value & 0xff;
  110. z->pos++;
  111. }
  112. if (z->pos == 3) {
  113. switch (z->buf[0]) {
  114. case 0x74:
  115. DPRINTF("%s: reg: 0x%.2x\n", __func__, z->buf[2]);
  116. z->cur_reg = z->buf[2];
  117. break;
  118. case 0x76:
  119. val = z->buf[1] << 8 | z->buf[2];
  120. DPRINTF("%s: value: 0x%.4x\n", __func__, val);
  121. if (z->cur_reg == 0x22 && val == 0x0000) {
  122. z->enabled = 1;
  123. printf("%s: LCD enabled\n", __func__);
  124. } else if (z->cur_reg == 0x10 && val == 0x0000) {
  125. z->enabled = 0;
  126. printf("%s: LCD disabled\n", __func__);
  127. }
  128. break;
  129. default:
  130. DPRINTF("%s: unknown command!\n", __func__);
  131. break;
  132. }
  133. z->pos = 0;
  134. }
  135. return 0;
  136. }
  137. static void z2_lcd_cs(void *opaque, int line, int level)
  138. {
  139. ZipitLCD *z2_lcd = opaque;
  140. z2_lcd->selected = !level;
  141. }
  142. static int zipit_lcd_init(SSISlave *dev)
  143. {
  144. ZipitLCD *z = FROM_SSI_SLAVE(ZipitLCD, dev);
  145. z->selected = 0;
  146. z->enabled = 0;
  147. z->pos = 0;
  148. return 0;
  149. }
  150. static VMStateDescription vmstate_zipit_lcd_state = {
  151. .name = "zipit-lcd",
  152. .version_id = 1,
  153. .minimum_version_id = 1,
  154. .minimum_version_id_old = 1,
  155. .fields = (VMStateField[]) {
  156. VMSTATE_INT32(selected, ZipitLCD),
  157. VMSTATE_INT32(enabled, ZipitLCD),
  158. VMSTATE_BUFFER(buf, ZipitLCD),
  159. VMSTATE_UINT32(cur_reg, ZipitLCD),
  160. VMSTATE_INT32(pos, ZipitLCD),
  161. VMSTATE_END_OF_LIST(),
  162. }
  163. };
  164. static void zipit_lcd_class_init(ObjectClass *klass, void *data)
  165. {
  166. DeviceClass *dc = DEVICE_CLASS(klass);
  167. SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
  168. k->init = zipit_lcd_init;
  169. k->transfer = zipit_lcd_transfer;
  170. dc->vmsd = &vmstate_zipit_lcd_state;
  171. }
  172. static TypeInfo zipit_lcd_info = {
  173. .name = "zipit-lcd",
  174. .parent = TYPE_SSI_SLAVE,
  175. .instance_size = sizeof(ZipitLCD),
  176. .class_init = zipit_lcd_class_init,
  177. };
  178. typedef struct {
  179. I2CSlave i2c;
  180. int len;
  181. uint8_t buf[3];
  182. } AER915State;
  183. static int aer915_send(I2CSlave *i2c, uint8_t data)
  184. {
  185. AER915State *s = FROM_I2C_SLAVE(AER915State, i2c);
  186. s->buf[s->len] = data;
  187. if (s->len++ > 2) {
  188. DPRINTF("%s: message too long (%i bytes)\n",
  189. __func__, s->len);
  190. return 1;
  191. }
  192. if (s->len == 2) {
  193. DPRINTF("%s: reg %d value 0x%02x\n", __func__,
  194. s->buf[0], s->buf[1]);
  195. }
  196. return 0;
  197. }
  198. static void aer915_event(I2CSlave *i2c, enum i2c_event event)
  199. {
  200. AER915State *s = FROM_I2C_SLAVE(AER915State, i2c);
  201. switch (event) {
  202. case I2C_START_SEND:
  203. s->len = 0;
  204. break;
  205. case I2C_START_RECV:
  206. if (s->len != 1) {
  207. DPRINTF("%s: short message!?\n", __func__);
  208. }
  209. break;
  210. case I2C_FINISH:
  211. break;
  212. default:
  213. break;
  214. }
  215. }
  216. static int aer915_recv(I2CSlave *slave)
  217. {
  218. int retval = 0x00;
  219. AER915State *s = FROM_I2C_SLAVE(AER915State, slave);
  220. switch (s->buf[0]) {
  221. /* Return hardcoded battery voltage,
  222. * 0xf0 means ~4.1V
  223. */
  224. case 0x02:
  225. retval = 0xf0;
  226. break;
  227. /* Return 0x00 for other regs,
  228. * we don't know what they are for,
  229. * anyway they return 0x00 on real hardware.
  230. */
  231. default:
  232. break;
  233. }
  234. return retval;
  235. }
  236. static int aer915_init(I2CSlave *i2c)
  237. {
  238. /* Nothing to do. */
  239. return 0;
  240. }
  241. static VMStateDescription vmstate_aer915_state = {
  242. .name = "aer915",
  243. .version_id = 1,
  244. .minimum_version_id = 1,
  245. .minimum_version_id_old = 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->init = aer915_init;
  257. k->event = aer915_event;
  258. k->recv = aer915_recv;
  259. k->send = aer915_send;
  260. dc->vmsd = &vmstate_aer915_state;
  261. }
  262. static TypeInfo aer915_info = {
  263. .name = "aer915",
  264. .parent = TYPE_I2C_SLAVE,
  265. .instance_size = sizeof(AER915State),
  266. .class_init = aer915_class_init,
  267. };
  268. static void z2_init(ram_addr_t ram_size,
  269. const char *boot_device,
  270. const char *kernel_filename, const char *kernel_cmdline,
  271. const char *initrd_filename, const char *cpu_model)
  272. {
  273. MemoryRegion *address_space_mem = get_system_memory();
  274. uint32_t sector_len = 0x10000;
  275. PXA2xxState *mpu;
  276. DriveInfo *dinfo;
  277. int be;
  278. void *z2_lcd;
  279. i2c_bus *bus;
  280. DeviceState *wm;
  281. if (!cpu_model) {
  282. cpu_model = "pxa270-c5";
  283. }
  284. /* Setup CPU & memory */
  285. mpu = pxa270_init(address_space_mem, z2_binfo.ram_size, cpu_model);
  286. #ifdef TARGET_WORDS_BIGENDIAN
  287. be = 1;
  288. #else
  289. be = 0;
  290. #endif
  291. dinfo = drive_get(IF_PFLASH, 0, 0);
  292. if (!dinfo) {
  293. fprintf(stderr, "Flash image must be given with the "
  294. "'pflash' parameter\n");
  295. exit(1);
  296. }
  297. if (!pflash_cfi01_register(Z2_FLASH_BASE,
  298. NULL, "z2.flash0", Z2_FLASH_SIZE,
  299. dinfo->bdrv, sector_len,
  300. Z2_FLASH_SIZE / sector_len, 4, 0, 0, 0, 0,
  301. be)) {
  302. fprintf(stderr, "qemu: Error registering flash memory.\n");
  303. exit(1);
  304. }
  305. /* setup keypad */
  306. pxa27x_register_keypad(mpu->kp, map, 0x100);
  307. /* MMC/SD host */
  308. pxa2xx_mmci_handlers(mpu->mmc,
  309. NULL,
  310. qdev_get_gpio_in(mpu->gpio, Z2_GPIO_SD_DETECT));
  311. type_register_static(&zipit_lcd_info);
  312. type_register_static(&aer915_info);
  313. z2_lcd = ssi_create_slave(mpu->ssp[1], "zipit-lcd");
  314. bus = pxa2xx_i2c_bus(mpu->i2c[0]);
  315. i2c_create_slave(bus, "aer915", 0x55);
  316. wm = i2c_create_slave(bus, "wm8750", 0x1b);
  317. mpu->i2s->opaque = wm;
  318. mpu->i2s->codec_out = wm8750_dac_dat;
  319. mpu->i2s->codec_in = wm8750_adc_dat;
  320. wm8750_data_req_set(wm, mpu->i2s->data_req, mpu->i2s);
  321. qdev_connect_gpio_out(mpu->gpio, Z2_GPIO_LCD_CS,
  322. qemu_allocate_irqs(z2_lcd_cs, z2_lcd, 1)[0]);
  323. if (kernel_filename) {
  324. z2_binfo.kernel_filename = kernel_filename;
  325. z2_binfo.kernel_cmdline = kernel_cmdline;
  326. z2_binfo.initrd_filename = initrd_filename;
  327. z2_binfo.board_id = 0x6dd;
  328. arm_load_kernel(mpu->cpu, &z2_binfo);
  329. }
  330. }
  331. static QEMUMachine z2_machine = {
  332. .name = "z2",
  333. .desc = "Zipit Z2 (PXA27x)",
  334. .init = z2_init,
  335. };
  336. static void z2_machine_init(void)
  337. {
  338. qemu_register_machine(&z2_machine);
  339. }
  340. machine_init(z2_machine_init);