pl110.c 14 KB

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