2
0

z2.c 8.9 KB

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