pl110.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. * Arm PrimeCell PL110 Color LCD Controller
  3. *
  4. * Copyright (c) 2005-2009 CodeSourcery.
  5. * Written by Paul Brook
  6. *
  7. * This code is licensed under the GNU LGPL
  8. */
  9. #include "qemu/osdep.h"
  10. #include "hw/irq.h"
  11. #include "hw/sysbus.h"
  12. #include "migration/vmstate.h"
  13. #include "ui/console.h"
  14. #include "framebuffer.h"
  15. #include "ui/pixel_ops.h"
  16. #include "qemu/timer.h"
  17. #include "qemu/log.h"
  18. #include "qemu/module.h"
  19. #include "qom/object.h"
  20. #define PL110_CR_EN 0x001
  21. #define PL110_CR_BGR 0x100
  22. #define PL110_CR_BEBO 0x200
  23. #define PL110_CR_BEPO 0x400
  24. #define PL110_CR_PWR 0x800
  25. #define PL110_IE_NB 0x004
  26. #define PL110_IE_VC 0x008
  27. enum pl110_bppmode
  28. {
  29. BPP_1,
  30. BPP_2,
  31. BPP_4,
  32. BPP_8,
  33. BPP_16,
  34. BPP_32,
  35. BPP_16_565, /* PL111 only */
  36. BPP_12 /* PL111 only */
  37. };
  38. /* The Versatile/PB uses a slightly modified PL110 controller. */
  39. enum pl110_version
  40. {
  41. VERSION_PL110,
  42. VERSION_PL110_VERSATILE,
  43. VERSION_PL111
  44. };
  45. #define TYPE_PL110 "pl110"
  46. OBJECT_DECLARE_SIMPLE_TYPE(PL110State, PL110)
  47. struct PL110State {
  48. SysBusDevice parent_obj;
  49. MemoryRegion iomem;
  50. MemoryRegionSection fbsection;
  51. QemuConsole *con;
  52. QEMUTimer *vblank_timer;
  53. int version;
  54. uint32_t timing[4];
  55. uint32_t cr;
  56. uint32_t upbase;
  57. uint32_t lpbase;
  58. uint32_t int_status;
  59. uint32_t int_mask;
  60. int cols;
  61. int rows;
  62. enum pl110_bppmode bpp;
  63. int invalidate;
  64. uint32_t mux_ctrl;
  65. uint32_t palette[256];
  66. uint32_t raw_palette[128];
  67. qemu_irq irq;
  68. };
  69. static int vmstate_pl110_post_load(void *opaque, int version_id);
  70. static const VMStateDescription vmstate_pl110 = {
  71. .name = "pl110",
  72. .version_id = 2,
  73. .minimum_version_id = 1,
  74. .post_load = vmstate_pl110_post_load,
  75. .fields = (VMStateField[]) {
  76. VMSTATE_INT32(version, PL110State),
  77. VMSTATE_UINT32_ARRAY(timing, PL110State, 4),
  78. VMSTATE_UINT32(cr, PL110State),
  79. VMSTATE_UINT32(upbase, PL110State),
  80. VMSTATE_UINT32(lpbase, PL110State),
  81. VMSTATE_UINT32(int_status, PL110State),
  82. VMSTATE_UINT32(int_mask, PL110State),
  83. VMSTATE_INT32(cols, PL110State),
  84. VMSTATE_INT32(rows, PL110State),
  85. VMSTATE_UINT32(bpp, PL110State),
  86. VMSTATE_INT32(invalidate, PL110State),
  87. VMSTATE_UINT32_ARRAY(palette, PL110State, 256),
  88. VMSTATE_UINT32_ARRAY(raw_palette, PL110State, 128),
  89. VMSTATE_UINT32_V(mux_ctrl, PL110State, 2),
  90. VMSTATE_END_OF_LIST()
  91. }
  92. };
  93. static const unsigned char pl110_id[] =
  94. { 0x10, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
  95. static const unsigned char pl111_id[] = {
  96. 0x11, 0x11, 0x24, 0x00, 0x0d, 0xf0, 0x05, 0xb1
  97. };
  98. /* Indexed by pl110_version */
  99. static const unsigned char *idregs[] = {
  100. pl110_id,
  101. /* The ARM documentation (DDI0224C) says the CLCDC on the Versatile board
  102. * has a different ID (0x93, 0x10, 0x04, 0x00, ...). However the hardware
  103. * itself has the same ID values as a stock PL110, and guests (in
  104. * particular Linux) rely on this. We emulate what the hardware does,
  105. * rather than what the docs claim it ought to do.
  106. */
  107. pl110_id,
  108. pl111_id
  109. };
  110. #define COPY_PIXEL(to, from) do { *(uint32_t *)to = from; to += 4; } while (0)
  111. #undef RGB
  112. #define BORDER bgr
  113. #define ORDER 0
  114. #include "pl110_template.h"
  115. #define ORDER 1
  116. #include "pl110_template.h"
  117. #define ORDER 2
  118. #include "pl110_template.h"
  119. #undef BORDER
  120. #define RGB
  121. #define BORDER rgb
  122. #define ORDER 0
  123. #include "pl110_template.h"
  124. #define ORDER 1
  125. #include "pl110_template.h"
  126. #define ORDER 2
  127. #include "pl110_template.h"
  128. #undef BORDER
  129. #undef COPY_PIXEL
  130. static drawfn pl110_draw_fn_32[48] = {
  131. pl110_draw_line1_lblp_bgr,
  132. pl110_draw_line2_lblp_bgr,
  133. pl110_draw_line4_lblp_bgr,
  134. pl110_draw_line8_lblp_bgr,
  135. pl110_draw_line16_555_lblp_bgr,
  136. pl110_draw_line32_lblp_bgr,
  137. pl110_draw_line16_lblp_bgr,
  138. pl110_draw_line12_lblp_bgr,
  139. pl110_draw_line1_bbbp_bgr,
  140. pl110_draw_line2_bbbp_bgr,
  141. pl110_draw_line4_bbbp_bgr,
  142. pl110_draw_line8_bbbp_bgr,
  143. pl110_draw_line16_555_bbbp_bgr,
  144. pl110_draw_line32_bbbp_bgr,
  145. pl110_draw_line16_bbbp_bgr,
  146. pl110_draw_line12_bbbp_bgr,
  147. pl110_draw_line1_lbbp_bgr,
  148. pl110_draw_line2_lbbp_bgr,
  149. pl110_draw_line4_lbbp_bgr,
  150. pl110_draw_line8_lbbp_bgr,
  151. pl110_draw_line16_555_lbbp_bgr,
  152. pl110_draw_line32_lbbp_bgr,
  153. pl110_draw_line16_lbbp_bgr,
  154. pl110_draw_line12_lbbp_bgr,
  155. pl110_draw_line1_lblp_rgb,
  156. pl110_draw_line2_lblp_rgb,
  157. pl110_draw_line4_lblp_rgb,
  158. pl110_draw_line8_lblp_rgb,
  159. pl110_draw_line16_555_lblp_rgb,
  160. pl110_draw_line32_lblp_rgb,
  161. pl110_draw_line16_lblp_rgb,
  162. pl110_draw_line12_lblp_rgb,
  163. pl110_draw_line1_bbbp_rgb,
  164. pl110_draw_line2_bbbp_rgb,
  165. pl110_draw_line4_bbbp_rgb,
  166. pl110_draw_line8_bbbp_rgb,
  167. pl110_draw_line16_555_bbbp_rgb,
  168. pl110_draw_line32_bbbp_rgb,
  169. pl110_draw_line16_bbbp_rgb,
  170. pl110_draw_line12_bbbp_rgb,
  171. pl110_draw_line1_lbbp_rgb,
  172. pl110_draw_line2_lbbp_rgb,
  173. pl110_draw_line4_lbbp_rgb,
  174. pl110_draw_line8_lbbp_rgb,
  175. pl110_draw_line16_555_lbbp_rgb,
  176. pl110_draw_line32_lbbp_rgb,
  177. pl110_draw_line16_lbbp_rgb,
  178. pl110_draw_line12_lbbp_rgb,
  179. };
  180. static int pl110_enabled(PL110State *s)
  181. {
  182. return (s->cr & PL110_CR_EN) && (s->cr & PL110_CR_PWR);
  183. }
  184. static void pl110_update_display(void *opaque)
  185. {
  186. PL110State *s = (PL110State *)opaque;
  187. SysBusDevice *sbd;
  188. DisplaySurface *surface = qemu_console_surface(s->con);
  189. drawfn fn;
  190. int src_width;
  191. int bpp_offset;
  192. int first;
  193. int last;
  194. if (!pl110_enabled(s)) {
  195. return;
  196. }
  197. sbd = SYS_BUS_DEVICE(s);
  198. if (s->cr & PL110_CR_BGR)
  199. bpp_offset = 0;
  200. else
  201. bpp_offset = 24;
  202. if ((s->version != VERSION_PL111) && (s->bpp == BPP_16)) {
  203. /* The PL110's native 16 bit mode is 5551; however
  204. * most boards with a PL110 implement an external
  205. * mux which allows bits to be reshuffled to give
  206. * 565 format. The mux is typically controlled by
  207. * an external system register.
  208. * This is controlled by a GPIO input pin
  209. * so boards can wire it up to their register.
  210. *
  211. * The PL111 straightforwardly implements both
  212. * 5551 and 565 under control of the bpp field
  213. * in the LCDControl register.
  214. */
  215. switch (s->mux_ctrl) {
  216. case 3: /* 565 BGR */
  217. bpp_offset = (BPP_16_565 - BPP_16);
  218. break;
  219. case 1: /* 5551 */
  220. break;
  221. case 0: /* 888; also if we have loaded vmstate from an old version */
  222. case 2: /* 565 RGB */
  223. default:
  224. /* treat as 565 but honour BGR bit */
  225. bpp_offset += (BPP_16_565 - BPP_16);
  226. break;
  227. }
  228. }
  229. if (s->cr & PL110_CR_BEBO) {
  230. fn = pl110_draw_fn_32[s->bpp + 8 + bpp_offset];
  231. } else if (s->cr & PL110_CR_BEPO) {
  232. fn = pl110_draw_fn_32[s->bpp + 16 + bpp_offset];
  233. } else {
  234. fn = pl110_draw_fn_32[s->bpp + bpp_offset];
  235. }
  236. src_width = s->cols;
  237. switch (s->bpp) {
  238. case BPP_1:
  239. src_width >>= 3;
  240. break;
  241. case BPP_2:
  242. src_width >>= 2;
  243. break;
  244. case BPP_4:
  245. src_width >>= 1;
  246. break;
  247. case BPP_8:
  248. break;
  249. case BPP_16:
  250. case BPP_16_565:
  251. case BPP_12:
  252. src_width <<= 1;
  253. break;
  254. case BPP_32:
  255. src_width <<= 2;
  256. break;
  257. }
  258. first = 0;
  259. if (s->invalidate) {
  260. framebuffer_update_memory_section(&s->fbsection,
  261. sysbus_address_space(sbd),
  262. s->upbase,
  263. s->rows, src_width);
  264. }
  265. framebuffer_update_display(surface, &s->fbsection,
  266. s->cols, s->rows,
  267. src_width, s->cols * 4, 0,
  268. s->invalidate,
  269. fn, s->palette,
  270. &first, &last);
  271. if (first >= 0) {
  272. dpy_gfx_update(s->con, 0, first, s->cols, last - first + 1);
  273. }
  274. s->invalidate = 0;
  275. }
  276. static void pl110_invalidate_display(void * opaque)
  277. {
  278. PL110State *s = (PL110State *)opaque;
  279. s->invalidate = 1;
  280. if (pl110_enabled(s)) {
  281. qemu_console_resize(s->con, s->cols, s->rows);
  282. }
  283. }
  284. static void pl110_update_palette(PL110State *s, int n)
  285. {
  286. DisplaySurface *surface = qemu_console_surface(s->con);
  287. int i;
  288. uint32_t raw;
  289. unsigned int r, g, b;
  290. raw = s->raw_palette[n];
  291. n <<= 1;
  292. for (i = 0; i < 2; i++) {
  293. r = (raw & 0x1f) << 3;
  294. raw >>= 5;
  295. g = (raw & 0x1f) << 3;
  296. raw >>= 5;
  297. b = (raw & 0x1f) << 3;
  298. /* The I bit is ignored. */
  299. raw >>= 6;
  300. switch (surface_bits_per_pixel(surface)) {
  301. case 8:
  302. s->palette[n] = rgb_to_pixel8(r, g, b);
  303. break;
  304. case 15:
  305. s->palette[n] = rgb_to_pixel15(r, g, b);
  306. break;
  307. case 16:
  308. s->palette[n] = rgb_to_pixel16(r, g, b);
  309. break;
  310. case 24:
  311. case 32:
  312. s->palette[n] = rgb_to_pixel32(r, g, b);
  313. break;
  314. }
  315. n++;
  316. }
  317. }
  318. static void pl110_resize(PL110State *s, int width, int height)
  319. {
  320. if (width != s->cols || height != s->rows) {
  321. if (pl110_enabled(s)) {
  322. qemu_console_resize(s->con, width, height);
  323. }
  324. }
  325. s->cols = width;
  326. s->rows = height;
  327. }
  328. /* Update interrupts. */
  329. static void pl110_update(PL110State *s)
  330. {
  331. /* Raise IRQ if enabled and any status bit is 1 */
  332. if (s->int_status & s->int_mask) {
  333. qemu_irq_raise(s->irq);
  334. } else {
  335. qemu_irq_lower(s->irq);
  336. }
  337. }
  338. static void pl110_vblank_interrupt(void *opaque)
  339. {
  340. PL110State *s = opaque;
  341. /* Fire the vertical compare and next base IRQs and re-arm */
  342. s->int_status |= (PL110_IE_NB | PL110_IE_VC);
  343. timer_mod(s->vblank_timer,
  344. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  345. NANOSECONDS_PER_SECOND / 60);
  346. pl110_update(s);
  347. }
  348. static uint64_t pl110_read(void *opaque, hwaddr offset,
  349. unsigned size)
  350. {
  351. PL110State *s = (PL110State *)opaque;
  352. if (offset >= 0xfe0 && offset < 0x1000) {
  353. return idregs[s->version][(offset - 0xfe0) >> 2];
  354. }
  355. if (offset >= 0x200 && offset < 0x400) {
  356. return s->raw_palette[(offset - 0x200) >> 2];
  357. }
  358. switch (offset >> 2) {
  359. case 0: /* LCDTiming0 */
  360. return s->timing[0];
  361. case 1: /* LCDTiming1 */
  362. return s->timing[1];
  363. case 2: /* LCDTiming2 */
  364. return s->timing[2];
  365. case 3: /* LCDTiming3 */
  366. return s->timing[3];
  367. case 4: /* LCDUPBASE */
  368. return s->upbase;
  369. case 5: /* LCDLPBASE */
  370. return s->lpbase;
  371. case 6: /* LCDIMSC */
  372. if (s->version != VERSION_PL110) {
  373. return s->cr;
  374. }
  375. return s->int_mask;
  376. case 7: /* LCDControl */
  377. if (s->version != VERSION_PL110) {
  378. return s->int_mask;
  379. }
  380. return s->cr;
  381. case 8: /* LCDRIS */
  382. return s->int_status;
  383. case 9: /* LCDMIS */
  384. return s->int_status & s->int_mask;
  385. case 11: /* LCDUPCURR */
  386. /* TODO: Implement vertical refresh. */
  387. return s->upbase;
  388. case 12: /* LCDLPCURR */
  389. return s->lpbase;
  390. default:
  391. qemu_log_mask(LOG_GUEST_ERROR,
  392. "pl110_read: Bad offset %x\n", (int)offset);
  393. return 0;
  394. }
  395. }
  396. static void pl110_write(void *opaque, hwaddr offset,
  397. uint64_t val, unsigned size)
  398. {
  399. PL110State *s = (PL110State *)opaque;
  400. int n;
  401. /* For simplicity invalidate the display whenever a control register
  402. is written to. */
  403. s->invalidate = 1;
  404. if (offset >= 0x200 && offset < 0x400) {
  405. /* Palette. */
  406. n = (offset - 0x200) >> 2;
  407. s->raw_palette[(offset - 0x200) >> 2] = val;
  408. pl110_update_palette(s, n);
  409. return;
  410. }
  411. switch (offset >> 2) {
  412. case 0: /* LCDTiming0 */
  413. s->timing[0] = val;
  414. n = ((val & 0xfc) + 4) * 4;
  415. pl110_resize(s, n, s->rows);
  416. break;
  417. case 1: /* LCDTiming1 */
  418. s->timing[1] = val;
  419. n = (val & 0x3ff) + 1;
  420. pl110_resize(s, s->cols, n);
  421. break;
  422. case 2: /* LCDTiming2 */
  423. s->timing[2] = val;
  424. break;
  425. case 3: /* LCDTiming3 */
  426. s->timing[3] = val;
  427. break;
  428. case 4: /* LCDUPBASE */
  429. s->upbase = val;
  430. break;
  431. case 5: /* LCDLPBASE */
  432. s->lpbase = val;
  433. break;
  434. case 6: /* LCDIMSC */
  435. if (s->version != VERSION_PL110) {
  436. goto control;
  437. }
  438. imsc:
  439. s->int_mask = val;
  440. pl110_update(s);
  441. break;
  442. case 7: /* LCDControl */
  443. if (s->version != VERSION_PL110) {
  444. goto imsc;
  445. }
  446. control:
  447. s->cr = val;
  448. s->bpp = (val >> 1) & 7;
  449. if (pl110_enabled(s)) {
  450. qemu_console_resize(s->con, s->cols, s->rows);
  451. timer_mod(s->vblank_timer,
  452. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  453. NANOSECONDS_PER_SECOND / 60);
  454. } else {
  455. timer_del(s->vblank_timer);
  456. }
  457. break;
  458. case 10: /* LCDICR */
  459. s->int_status &= ~val;
  460. pl110_update(s);
  461. break;
  462. default:
  463. qemu_log_mask(LOG_GUEST_ERROR,
  464. "pl110_write: Bad offset %x\n", (int)offset);
  465. }
  466. }
  467. static const MemoryRegionOps pl110_ops = {
  468. .read = pl110_read,
  469. .write = pl110_write,
  470. .endianness = DEVICE_NATIVE_ENDIAN,
  471. };
  472. static void pl110_mux_ctrl_set(void *opaque, int line, int level)
  473. {
  474. PL110State *s = (PL110State *)opaque;
  475. s->mux_ctrl = level;
  476. }
  477. static int vmstate_pl110_post_load(void *opaque, int version_id)
  478. {
  479. PL110State *s = opaque;
  480. /* Make sure we redraw, and at the right size */
  481. pl110_invalidate_display(s);
  482. return 0;
  483. }
  484. static const GraphicHwOps pl110_gfx_ops = {
  485. .invalidate = pl110_invalidate_display,
  486. .gfx_update = pl110_update_display,
  487. };
  488. static void pl110_realize(DeviceState *dev, Error **errp)
  489. {
  490. PL110State *s = PL110(dev);
  491. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  492. memory_region_init_io(&s->iomem, OBJECT(s), &pl110_ops, s, "pl110", 0x1000);
  493. sysbus_init_mmio(sbd, &s->iomem);
  494. sysbus_init_irq(sbd, &s->irq);
  495. s->vblank_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  496. pl110_vblank_interrupt, s);
  497. qdev_init_gpio_in(dev, pl110_mux_ctrl_set, 1);
  498. s->con = graphic_console_init(dev, 0, &pl110_gfx_ops, s);
  499. }
  500. static void pl110_init(Object *obj)
  501. {
  502. PL110State *s = PL110(obj);
  503. s->version = VERSION_PL110;
  504. }
  505. static void pl110_versatile_init(Object *obj)
  506. {
  507. PL110State *s = PL110(obj);
  508. s->version = VERSION_PL110_VERSATILE;
  509. }
  510. static void pl111_init(Object *obj)
  511. {
  512. PL110State *s = PL110(obj);
  513. s->version = VERSION_PL111;
  514. }
  515. static void pl110_class_init(ObjectClass *klass, void *data)
  516. {
  517. DeviceClass *dc = DEVICE_CLASS(klass);
  518. set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
  519. dc->vmsd = &vmstate_pl110;
  520. dc->realize = pl110_realize;
  521. }
  522. static const TypeInfo pl110_info = {
  523. .name = TYPE_PL110,
  524. .parent = TYPE_SYS_BUS_DEVICE,
  525. .instance_size = sizeof(PL110State),
  526. .instance_init = pl110_init,
  527. .class_init = pl110_class_init,
  528. };
  529. static const TypeInfo pl110_versatile_info = {
  530. .name = "pl110_versatile",
  531. .parent = TYPE_PL110,
  532. .instance_init = pl110_versatile_init,
  533. };
  534. static const TypeInfo pl111_info = {
  535. .name = "pl111",
  536. .parent = TYPE_PL110,
  537. .instance_init = pl111_init,
  538. };
  539. static void pl110_register_types(void)
  540. {
  541. type_register_static(&pl110_info);
  542. type_register_static(&pl110_versatile_info);
  543. type_register_static(&pl111_info);
  544. }
  545. type_init(pl110_register_types)