2
0

omap_lcdc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * OMAP LCD controller.
  3. *
  4. * Copyright (C) 2006-2007 Andrzej Zaborowski <balrog@zabor.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "hw/irq.h"
  21. #include "ui/console.h"
  22. #include "hw/arm/omap.h"
  23. #include "framebuffer.h"
  24. #include "ui/pixel_ops.h"
  25. struct omap_lcd_panel_s {
  26. MemoryRegion *sysmem;
  27. MemoryRegion iomem;
  28. MemoryRegionSection fbsection;
  29. qemu_irq irq;
  30. QemuConsole *con;
  31. int plm;
  32. int tft;
  33. int mono;
  34. int enable;
  35. int width;
  36. int height;
  37. int interrupts;
  38. uint32_t timing[3];
  39. uint32_t subpanel;
  40. uint32_t ctrl;
  41. struct omap_dma_lcd_channel_s *dma;
  42. uint16_t palette[256];
  43. int palette_done;
  44. int frame_done;
  45. int invalidate;
  46. int sync_error;
  47. };
  48. static void omap_lcd_interrupts(struct omap_lcd_panel_s *s)
  49. {
  50. if (s->frame_done && (s->interrupts & 1)) {
  51. qemu_irq_raise(s->irq);
  52. return;
  53. }
  54. if (s->palette_done && (s->interrupts & 2)) {
  55. qemu_irq_raise(s->irq);
  56. return;
  57. }
  58. if (s->sync_error) {
  59. qemu_irq_raise(s->irq);
  60. return;
  61. }
  62. qemu_irq_lower(s->irq);
  63. }
  64. /*
  65. * 2-bit colour
  66. */
  67. static void draw_line2_32(void *opaque, uint8_t *d, const uint8_t *s,
  68. int width, int deststep)
  69. {
  70. uint16_t *pal = opaque;
  71. uint8_t v, r, g, b;
  72. do {
  73. v = ldub_p((void *) s);
  74. r = (pal[v & 3] >> 4) & 0xf0;
  75. g = pal[v & 3] & 0xf0;
  76. b = (pal[v & 3] << 4) & 0xf0;
  77. ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
  78. d += 4;
  79. v >>= 2;
  80. r = (pal[v & 3] >> 4) & 0xf0;
  81. g = pal[v & 3] & 0xf0;
  82. b = (pal[v & 3] << 4) & 0xf0;
  83. ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
  84. d += 4;
  85. v >>= 2;
  86. r = (pal[v & 3] >> 4) & 0xf0;
  87. g = pal[v & 3] & 0xf0;
  88. b = (pal[v & 3] << 4) & 0xf0;
  89. ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
  90. d += 4;
  91. v >>= 2;
  92. r = (pal[v & 3] >> 4) & 0xf0;
  93. g = pal[v & 3] & 0xf0;
  94. b = (pal[v & 3] << 4) & 0xf0;
  95. ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
  96. d += 4;
  97. s++;
  98. width -= 4;
  99. } while (width > 0);
  100. }
  101. /*
  102. * 4-bit colour
  103. */
  104. static void draw_line4_32(void *opaque, uint8_t *d, const uint8_t *s,
  105. int width, int deststep)
  106. {
  107. uint16_t *pal = opaque;
  108. uint8_t v, r, g, b;
  109. do {
  110. v = ldub_p((void *) s);
  111. r = (pal[v & 0xf] >> 4) & 0xf0;
  112. g = pal[v & 0xf] & 0xf0;
  113. b = (pal[v & 0xf] << 4) & 0xf0;
  114. ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
  115. d += 4;
  116. v >>= 4;
  117. r = (pal[v & 0xf] >> 4) & 0xf0;
  118. g = pal[v & 0xf] & 0xf0;
  119. b = (pal[v & 0xf] << 4) & 0xf0;
  120. ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
  121. d += 4;
  122. s++;
  123. width -= 2;
  124. } while (width > 0);
  125. }
  126. /*
  127. * 8-bit colour
  128. */
  129. static void draw_line8_32(void *opaque, uint8_t *d, const uint8_t *s,
  130. int width, int deststep)
  131. {
  132. uint16_t *pal = opaque;
  133. uint8_t v, r, g, b;
  134. do {
  135. v = ldub_p((void *) s);
  136. r = (pal[v] >> 4) & 0xf0;
  137. g = pal[v] & 0xf0;
  138. b = (pal[v] << 4) & 0xf0;
  139. ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
  140. s++;
  141. d += 4;
  142. } while (-- width != 0);
  143. }
  144. /*
  145. * 12-bit colour
  146. */
  147. static void draw_line12_32(void *opaque, uint8_t *d, const uint8_t *s,
  148. int width, int deststep)
  149. {
  150. uint16_t v;
  151. uint8_t r, g, b;
  152. do {
  153. v = lduw_le_p((void *) s);
  154. r = (v >> 4) & 0xf0;
  155. g = v & 0xf0;
  156. b = (v << 4) & 0xf0;
  157. ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
  158. s += 2;
  159. d += 4;
  160. } while (-- width != 0);
  161. }
  162. /*
  163. * 16-bit colour
  164. */
  165. static void draw_line16_32(void *opaque, uint8_t *d, const uint8_t *s,
  166. int width, int deststep)
  167. {
  168. uint16_t v;
  169. uint8_t r, g, b;
  170. do {
  171. v = lduw_le_p((void *) s);
  172. r = (v >> 8) & 0xf8;
  173. g = (v >> 3) & 0xfc;
  174. b = (v << 3) & 0xf8;
  175. ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
  176. s += 2;
  177. d += 4;
  178. } while (-- width != 0);
  179. }
  180. static void omap_update_display(void *opaque)
  181. {
  182. struct omap_lcd_panel_s *omap_lcd = opaque;
  183. DisplaySurface *surface;
  184. drawfn draw_line;
  185. int size, height, first, last;
  186. int width, linesize, step, bpp, frame_offset;
  187. hwaddr frame_base;
  188. if (!omap_lcd || omap_lcd->plm == 1 || !omap_lcd->enable) {
  189. return;
  190. }
  191. surface = qemu_console_surface(omap_lcd->con);
  192. if (!surface_bits_per_pixel(surface)) {
  193. return;
  194. }
  195. frame_offset = 0;
  196. if (omap_lcd->plm != 2) {
  197. cpu_physical_memory_read(
  198. omap_lcd->dma->phys_framebuffer[omap_lcd->dma->current_frame],
  199. omap_lcd->palette, 0x200);
  200. switch (omap_lcd->palette[0] >> 12 & 7) {
  201. case 3 ... 7:
  202. frame_offset += 0x200;
  203. break;
  204. default:
  205. frame_offset += 0x20;
  206. }
  207. }
  208. /* Colour depth */
  209. switch ((omap_lcd->palette[0] >> 12) & 7) {
  210. case 1:
  211. draw_line = draw_line2_32;
  212. bpp = 2;
  213. break;
  214. case 2:
  215. draw_line = draw_line4_32;
  216. bpp = 4;
  217. break;
  218. case 3:
  219. draw_line = draw_line8_32;
  220. bpp = 8;
  221. break;
  222. case 4 ... 7:
  223. if (!omap_lcd->tft)
  224. draw_line = draw_line12_32;
  225. else
  226. draw_line = draw_line16_32;
  227. bpp = 16;
  228. break;
  229. default:
  230. /* Unsupported at the moment. */
  231. return;
  232. }
  233. /* Resolution */
  234. width = omap_lcd->width;
  235. if (width != surface_width(surface) ||
  236. omap_lcd->height != surface_height(surface)) {
  237. qemu_console_resize(omap_lcd->con,
  238. omap_lcd->width, omap_lcd->height);
  239. surface = qemu_console_surface(omap_lcd->con);
  240. omap_lcd->invalidate = 1;
  241. }
  242. if (omap_lcd->dma->current_frame == 0)
  243. size = omap_lcd->dma->src_f1_bottom - omap_lcd->dma->src_f1_top;
  244. else
  245. size = omap_lcd->dma->src_f2_bottom - omap_lcd->dma->src_f2_top;
  246. if (frame_offset + ((width * omap_lcd->height * bpp) >> 3) > size + 2) {
  247. omap_lcd->sync_error = 1;
  248. omap_lcd_interrupts(omap_lcd);
  249. omap_lcd->enable = 0;
  250. return;
  251. }
  252. /* Content */
  253. frame_base = omap_lcd->dma->phys_framebuffer[
  254. omap_lcd->dma->current_frame] + frame_offset;
  255. omap_lcd->dma->condition |= 1 << omap_lcd->dma->current_frame;
  256. if (omap_lcd->dma->interrupts & 1)
  257. qemu_irq_raise(omap_lcd->dma->irq);
  258. if (omap_lcd->dma->dual)
  259. omap_lcd->dma->current_frame ^= 1;
  260. if (!surface_bits_per_pixel(surface)) {
  261. return;
  262. }
  263. first = 0;
  264. height = omap_lcd->height;
  265. if (omap_lcd->subpanel & (1 << 31)) {
  266. if (omap_lcd->subpanel & (1 << 29))
  267. first = (omap_lcd->subpanel >> 16) & 0x3ff;
  268. else
  269. height = (omap_lcd->subpanel >> 16) & 0x3ff;
  270. /* TODO: fill the rest of the panel with DPD */
  271. }
  272. step = width * bpp >> 3;
  273. linesize = surface_stride(surface);
  274. if (omap_lcd->invalidate) {
  275. framebuffer_update_memory_section(&omap_lcd->fbsection,
  276. omap_lcd->sysmem, frame_base,
  277. height, step);
  278. }
  279. framebuffer_update_display(surface, &omap_lcd->fbsection,
  280. width, height,
  281. step, linesize, 0,
  282. omap_lcd->invalidate,
  283. draw_line, omap_lcd->palette,
  284. &first, &last);
  285. if (first >= 0) {
  286. dpy_gfx_update(omap_lcd->con, 0, first, width, last - first + 1);
  287. }
  288. omap_lcd->invalidate = 0;
  289. }
  290. static void omap_invalidate_display(void *opaque) {
  291. struct omap_lcd_panel_s *omap_lcd = opaque;
  292. omap_lcd->invalidate = 1;
  293. }
  294. static void omap_lcd_update(struct omap_lcd_panel_s *s) {
  295. if (!s->enable) {
  296. s->dma->current_frame = -1;
  297. s->sync_error = 0;
  298. if (s->plm != 1)
  299. s->frame_done = 1;
  300. omap_lcd_interrupts(s);
  301. return;
  302. }
  303. if (s->dma->current_frame == -1) {
  304. s->frame_done = 0;
  305. s->palette_done = 0;
  306. s->dma->current_frame = 0;
  307. }
  308. if (!s->dma->mpu->port[s->dma->src].addr_valid(s->dma->mpu,
  309. s->dma->src_f1_top) ||
  310. !s->dma->mpu->port[
  311. s->dma->src].addr_valid(s->dma->mpu,
  312. s->dma->src_f1_bottom) ||
  313. (s->dma->dual &&
  314. (!s->dma->mpu->port[
  315. s->dma->src].addr_valid(s->dma->mpu,
  316. s->dma->src_f2_top) ||
  317. !s->dma->mpu->port[
  318. s->dma->src].addr_valid(s->dma->mpu,
  319. s->dma->src_f2_bottom)))) {
  320. s->dma->condition |= 1 << 2;
  321. if (s->dma->interrupts & (1 << 1))
  322. qemu_irq_raise(s->dma->irq);
  323. s->enable = 0;
  324. return;
  325. }
  326. s->dma->phys_framebuffer[0] = s->dma->src_f1_top;
  327. s->dma->phys_framebuffer[1] = s->dma->src_f2_top;
  328. if (s->plm != 2 && !s->palette_done) {
  329. cpu_physical_memory_read(
  330. s->dma->phys_framebuffer[s->dma->current_frame],
  331. s->palette, 0x200);
  332. s->palette_done = 1;
  333. omap_lcd_interrupts(s);
  334. }
  335. }
  336. static uint64_t omap_lcdc_read(void *opaque, hwaddr addr, unsigned size)
  337. {
  338. struct omap_lcd_panel_s *s = opaque;
  339. switch (addr) {
  340. case 0x00: /* LCD_CONTROL */
  341. return (s->tft << 23) | (s->plm << 20) |
  342. (s->tft << 7) | (s->interrupts << 3) |
  343. (s->mono << 1) | s->enable | s->ctrl | 0xfe000c34;
  344. case 0x04: /* LCD_TIMING0 */
  345. return (s->timing[0] << 10) | (s->width - 1) | 0x0000000f;
  346. case 0x08: /* LCD_TIMING1 */
  347. return (s->timing[1] << 10) | (s->height - 1);
  348. case 0x0c: /* LCD_TIMING2 */
  349. return s->timing[2] | 0xfc000000;
  350. case 0x10: /* LCD_STATUS */
  351. return (s->palette_done << 6) | (s->sync_error << 2) | s->frame_done;
  352. case 0x14: /* LCD_SUBPANEL */
  353. return s->subpanel;
  354. default:
  355. break;
  356. }
  357. OMAP_BAD_REG(addr);
  358. return 0;
  359. }
  360. static void omap_lcdc_write(void *opaque, hwaddr addr,
  361. uint64_t value, unsigned size)
  362. {
  363. struct omap_lcd_panel_s *s = opaque;
  364. switch (addr) {
  365. case 0x00: /* LCD_CONTROL */
  366. s->plm = (value >> 20) & 3;
  367. s->tft = (value >> 7) & 1;
  368. s->interrupts = (value >> 3) & 3;
  369. s->mono = (value >> 1) & 1;
  370. s->ctrl = value & 0x01cff300;
  371. if (s->enable != (value & 1)) {
  372. s->enable = value & 1;
  373. omap_lcd_update(s);
  374. }
  375. break;
  376. case 0x04: /* LCD_TIMING0 */
  377. s->timing[0] = value >> 10;
  378. s->width = (value & 0x3ff) + 1;
  379. break;
  380. case 0x08: /* LCD_TIMING1 */
  381. s->timing[1] = value >> 10;
  382. s->height = (value & 0x3ff) + 1;
  383. break;
  384. case 0x0c: /* LCD_TIMING2 */
  385. s->timing[2] = value;
  386. break;
  387. case 0x10: /* LCD_STATUS */
  388. break;
  389. case 0x14: /* LCD_SUBPANEL */
  390. s->subpanel = value & 0xa1ffffff;
  391. break;
  392. default:
  393. OMAP_BAD_REG(addr);
  394. }
  395. }
  396. static const MemoryRegionOps omap_lcdc_ops = {
  397. .read = omap_lcdc_read,
  398. .write = omap_lcdc_write,
  399. .endianness = DEVICE_NATIVE_ENDIAN,
  400. };
  401. void omap_lcdc_reset(struct omap_lcd_panel_s *s)
  402. {
  403. s->dma->current_frame = -1;
  404. s->plm = 0;
  405. s->tft = 0;
  406. s->mono = 0;
  407. s->enable = 0;
  408. s->width = 0;
  409. s->height = 0;
  410. s->interrupts = 0;
  411. s->timing[0] = 0;
  412. s->timing[1] = 0;
  413. s->timing[2] = 0;
  414. s->subpanel = 0;
  415. s->palette_done = 0;
  416. s->frame_done = 0;
  417. s->sync_error = 0;
  418. s->invalidate = 1;
  419. s->subpanel = 0;
  420. s->ctrl = 0;
  421. }
  422. static const GraphicHwOps omap_ops = {
  423. .invalidate = omap_invalidate_display,
  424. .gfx_update = omap_update_display,
  425. };
  426. struct omap_lcd_panel_s *omap_lcdc_init(MemoryRegion *sysmem,
  427. hwaddr base,
  428. qemu_irq irq,
  429. struct omap_dma_lcd_channel_s *dma,
  430. omap_clk clk)
  431. {
  432. struct omap_lcd_panel_s *s = g_new0(struct omap_lcd_panel_s, 1);
  433. s->irq = irq;
  434. s->dma = dma;
  435. s->sysmem = sysmem;
  436. omap_lcdc_reset(s);
  437. memory_region_init_io(&s->iomem, NULL, &omap_lcdc_ops, s, "omap.lcdc", 0x100);
  438. memory_region_add_subregion(sysmem, base, &s->iomem);
  439. s->con = graphic_console_init(NULL, 0, &omap_ops, s);
  440. return s;
  441. }