2
0

omap_lcdc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 "hw/hw.h"
  20. #include "ui/console.h"
  21. #include "hw/arm/omap.h"
  22. #include "framebuffer.h"
  23. #include "ui/pixel_ops.h"
  24. struct omap_lcd_panel_s {
  25. MemoryRegion *sysmem;
  26. MemoryRegion iomem;
  27. qemu_irq irq;
  28. QemuConsole *con;
  29. int plm;
  30. int tft;
  31. int mono;
  32. int enable;
  33. int width;
  34. int height;
  35. int interrupts;
  36. uint32_t timing[3];
  37. uint32_t subpanel;
  38. uint32_t ctrl;
  39. struct omap_dma_lcd_channel_s *dma;
  40. uint16_t palette[256];
  41. int palette_done;
  42. int frame_done;
  43. int invalidate;
  44. int sync_error;
  45. };
  46. static void omap_lcd_interrupts(struct omap_lcd_panel_s *s)
  47. {
  48. if (s->frame_done && (s->interrupts & 1)) {
  49. qemu_irq_raise(s->irq);
  50. return;
  51. }
  52. if (s->palette_done && (s->interrupts & 2)) {
  53. qemu_irq_raise(s->irq);
  54. return;
  55. }
  56. if (s->sync_error) {
  57. qemu_irq_raise(s->irq);
  58. return;
  59. }
  60. qemu_irq_lower(s->irq);
  61. }
  62. #define draw_line_func drawfn
  63. #define DEPTH 8
  64. #include "omap_lcd_template.h"
  65. #define DEPTH 15
  66. #include "omap_lcd_template.h"
  67. #define DEPTH 16
  68. #include "omap_lcd_template.h"
  69. #define DEPTH 32
  70. #include "omap_lcd_template.h"
  71. static draw_line_func draw_line_table2[33] = {
  72. [0 ... 32] = NULL,
  73. [8] = draw_line2_8,
  74. [15] = draw_line2_15,
  75. [16] = draw_line2_16,
  76. [32] = draw_line2_32,
  77. }, draw_line_table4[33] = {
  78. [0 ... 32] = NULL,
  79. [8] = draw_line4_8,
  80. [15] = draw_line4_15,
  81. [16] = draw_line4_16,
  82. [32] = draw_line4_32,
  83. }, draw_line_table8[33] = {
  84. [0 ... 32] = NULL,
  85. [8] = draw_line8_8,
  86. [15] = draw_line8_15,
  87. [16] = draw_line8_16,
  88. [32] = draw_line8_32,
  89. }, draw_line_table12[33] = {
  90. [0 ... 32] = NULL,
  91. [8] = draw_line12_8,
  92. [15] = draw_line12_15,
  93. [16] = draw_line12_16,
  94. [32] = draw_line12_32,
  95. }, draw_line_table16[33] = {
  96. [0 ... 32] = NULL,
  97. [8] = draw_line16_8,
  98. [15] = draw_line16_15,
  99. [16] = draw_line16_16,
  100. [32] = draw_line16_32,
  101. };
  102. static void omap_update_display(void *opaque)
  103. {
  104. struct omap_lcd_panel_s *omap_lcd = (struct omap_lcd_panel_s *) opaque;
  105. DisplaySurface *surface = qemu_console_surface(omap_lcd->con);
  106. draw_line_func draw_line;
  107. int size, height, first, last;
  108. int width, linesize, step, bpp, frame_offset;
  109. hwaddr frame_base;
  110. if (!omap_lcd || omap_lcd->plm == 1 || !omap_lcd->enable ||
  111. !surface_bits_per_pixel(surface)) {
  112. return;
  113. }
  114. frame_offset = 0;
  115. if (omap_lcd->plm != 2) {
  116. cpu_physical_memory_read(omap_lcd->dma->phys_framebuffer[
  117. omap_lcd->dma->current_frame],
  118. (void *)omap_lcd->palette, 0x200);
  119. switch (omap_lcd->palette[0] >> 12 & 7) {
  120. case 3 ... 7:
  121. frame_offset += 0x200;
  122. break;
  123. default:
  124. frame_offset += 0x20;
  125. }
  126. }
  127. /* Colour depth */
  128. switch ((omap_lcd->palette[0] >> 12) & 7) {
  129. case 1:
  130. draw_line = draw_line_table2[surface_bits_per_pixel(surface)];
  131. bpp = 2;
  132. break;
  133. case 2:
  134. draw_line = draw_line_table4[surface_bits_per_pixel(surface)];
  135. bpp = 4;
  136. break;
  137. case 3:
  138. draw_line = draw_line_table8[surface_bits_per_pixel(surface)];
  139. bpp = 8;
  140. break;
  141. case 4 ... 7:
  142. if (!omap_lcd->tft)
  143. draw_line = draw_line_table12[surface_bits_per_pixel(surface)];
  144. else
  145. draw_line = draw_line_table16[surface_bits_per_pixel(surface)];
  146. bpp = 16;
  147. break;
  148. default:
  149. /* Unsupported at the moment. */
  150. return;
  151. }
  152. /* Resolution */
  153. width = omap_lcd->width;
  154. if (width != surface_width(surface) ||
  155. omap_lcd->height != surface_height(surface)) {
  156. qemu_console_resize(omap_lcd->con,
  157. omap_lcd->width, omap_lcd->height);
  158. surface = qemu_console_surface(omap_lcd->con);
  159. omap_lcd->invalidate = 1;
  160. }
  161. if (omap_lcd->dma->current_frame == 0)
  162. size = omap_lcd->dma->src_f1_bottom - omap_lcd->dma->src_f1_top;
  163. else
  164. size = omap_lcd->dma->src_f2_bottom - omap_lcd->dma->src_f2_top;
  165. if (frame_offset + ((width * omap_lcd->height * bpp) >> 3) > size + 2) {
  166. omap_lcd->sync_error = 1;
  167. omap_lcd_interrupts(omap_lcd);
  168. omap_lcd->enable = 0;
  169. return;
  170. }
  171. /* Content */
  172. frame_base = omap_lcd->dma->phys_framebuffer[
  173. omap_lcd->dma->current_frame] + frame_offset;
  174. omap_lcd->dma->condition |= 1 << omap_lcd->dma->current_frame;
  175. if (omap_lcd->dma->interrupts & 1)
  176. qemu_irq_raise(omap_lcd->dma->irq);
  177. if (omap_lcd->dma->dual)
  178. omap_lcd->dma->current_frame ^= 1;
  179. if (!surface_bits_per_pixel(surface)) {
  180. return;
  181. }
  182. first = 0;
  183. height = omap_lcd->height;
  184. if (omap_lcd->subpanel & (1 << 31)) {
  185. if (omap_lcd->subpanel & (1 << 29))
  186. first = (omap_lcd->subpanel >> 16) & 0x3ff;
  187. else
  188. height = (omap_lcd->subpanel >> 16) & 0x3ff;
  189. /* TODO: fill the rest of the panel with DPD */
  190. }
  191. step = width * bpp >> 3;
  192. linesize = surface_stride(surface);
  193. framebuffer_update_display(surface, omap_lcd->sysmem,
  194. frame_base, width, height,
  195. step, linesize, 0,
  196. omap_lcd->invalidate,
  197. draw_line, omap_lcd->palette,
  198. &first, &last);
  199. if (first >= 0) {
  200. dpy_gfx_update(omap_lcd->con, 0, first, width, last - first + 1);
  201. }
  202. omap_lcd->invalidate = 0;
  203. }
  204. static void omap_invalidate_display(void *opaque) {
  205. struct omap_lcd_panel_s *omap_lcd = opaque;
  206. omap_lcd->invalidate = 1;
  207. }
  208. static void omap_lcd_update(struct omap_lcd_panel_s *s) {
  209. if (!s->enable) {
  210. s->dma->current_frame = -1;
  211. s->sync_error = 0;
  212. if (s->plm != 1)
  213. s->frame_done = 1;
  214. omap_lcd_interrupts(s);
  215. return;
  216. }
  217. if (s->dma->current_frame == -1) {
  218. s->frame_done = 0;
  219. s->palette_done = 0;
  220. s->dma->current_frame = 0;
  221. }
  222. if (!s->dma->mpu->port[s->dma->src].addr_valid(s->dma->mpu,
  223. s->dma->src_f1_top) ||
  224. !s->dma->mpu->port[
  225. s->dma->src].addr_valid(s->dma->mpu,
  226. s->dma->src_f1_bottom) ||
  227. (s->dma->dual &&
  228. (!s->dma->mpu->port[
  229. s->dma->src].addr_valid(s->dma->mpu,
  230. s->dma->src_f2_top) ||
  231. !s->dma->mpu->port[
  232. s->dma->src].addr_valid(s->dma->mpu,
  233. s->dma->src_f2_bottom)))) {
  234. s->dma->condition |= 1 << 2;
  235. if (s->dma->interrupts & (1 << 1))
  236. qemu_irq_raise(s->dma->irq);
  237. s->enable = 0;
  238. return;
  239. }
  240. s->dma->phys_framebuffer[0] = s->dma->src_f1_top;
  241. s->dma->phys_framebuffer[1] = s->dma->src_f2_top;
  242. if (s->plm != 2 && !s->palette_done) {
  243. cpu_physical_memory_read(
  244. s->dma->phys_framebuffer[s->dma->current_frame],
  245. (void *)s->palette, 0x200);
  246. s->palette_done = 1;
  247. omap_lcd_interrupts(s);
  248. }
  249. }
  250. static uint64_t omap_lcdc_read(void *opaque, hwaddr addr,
  251. unsigned size)
  252. {
  253. struct omap_lcd_panel_s *s = (struct omap_lcd_panel_s *) opaque;
  254. switch (addr) {
  255. case 0x00: /* LCD_CONTROL */
  256. return (s->tft << 23) | (s->plm << 20) |
  257. (s->tft << 7) | (s->interrupts << 3) |
  258. (s->mono << 1) | s->enable | s->ctrl | 0xfe000c34;
  259. case 0x04: /* LCD_TIMING0 */
  260. return (s->timing[0] << 10) | (s->width - 1) | 0x0000000f;
  261. case 0x08: /* LCD_TIMING1 */
  262. return (s->timing[1] << 10) | (s->height - 1);
  263. case 0x0c: /* LCD_TIMING2 */
  264. return s->timing[2] | 0xfc000000;
  265. case 0x10: /* LCD_STATUS */
  266. return (s->palette_done << 6) | (s->sync_error << 2) | s->frame_done;
  267. case 0x14: /* LCD_SUBPANEL */
  268. return s->subpanel;
  269. default:
  270. break;
  271. }
  272. OMAP_BAD_REG(addr);
  273. return 0;
  274. }
  275. static void omap_lcdc_write(void *opaque, hwaddr addr,
  276. uint64_t value, unsigned size)
  277. {
  278. struct omap_lcd_panel_s *s = (struct omap_lcd_panel_s *) opaque;
  279. switch (addr) {
  280. case 0x00: /* LCD_CONTROL */
  281. s->plm = (value >> 20) & 3;
  282. s->tft = (value >> 7) & 1;
  283. s->interrupts = (value >> 3) & 3;
  284. s->mono = (value >> 1) & 1;
  285. s->ctrl = value & 0x01cff300;
  286. if (s->enable != (value & 1)) {
  287. s->enable = value & 1;
  288. omap_lcd_update(s);
  289. }
  290. break;
  291. case 0x04: /* LCD_TIMING0 */
  292. s->timing[0] = value >> 10;
  293. s->width = (value & 0x3ff) + 1;
  294. break;
  295. case 0x08: /* LCD_TIMING1 */
  296. s->timing[1] = value >> 10;
  297. s->height = (value & 0x3ff) + 1;
  298. break;
  299. case 0x0c: /* LCD_TIMING2 */
  300. s->timing[2] = value;
  301. break;
  302. case 0x10: /* LCD_STATUS */
  303. break;
  304. case 0x14: /* LCD_SUBPANEL */
  305. s->subpanel = value & 0xa1ffffff;
  306. break;
  307. default:
  308. OMAP_BAD_REG(addr);
  309. }
  310. }
  311. static const MemoryRegionOps omap_lcdc_ops = {
  312. .read = omap_lcdc_read,
  313. .write = omap_lcdc_write,
  314. .endianness = DEVICE_NATIVE_ENDIAN,
  315. };
  316. void omap_lcdc_reset(struct omap_lcd_panel_s *s)
  317. {
  318. s->dma->current_frame = -1;
  319. s->plm = 0;
  320. s->tft = 0;
  321. s->mono = 0;
  322. s->enable = 0;
  323. s->width = 0;
  324. s->height = 0;
  325. s->interrupts = 0;
  326. s->timing[0] = 0;
  327. s->timing[1] = 0;
  328. s->timing[2] = 0;
  329. s->subpanel = 0;
  330. s->palette_done = 0;
  331. s->frame_done = 0;
  332. s->sync_error = 0;
  333. s->invalidate = 1;
  334. s->subpanel = 0;
  335. s->ctrl = 0;
  336. }
  337. static const GraphicHwOps omap_ops = {
  338. .invalidate = omap_invalidate_display,
  339. .gfx_update = omap_update_display,
  340. };
  341. struct omap_lcd_panel_s *omap_lcdc_init(MemoryRegion *sysmem,
  342. hwaddr base,
  343. qemu_irq irq,
  344. struct omap_dma_lcd_channel_s *dma,
  345. omap_clk clk)
  346. {
  347. struct omap_lcd_panel_s *s = (struct omap_lcd_panel_s *)
  348. g_malloc0(sizeof(struct omap_lcd_panel_s));
  349. s->irq = irq;
  350. s->dma = dma;
  351. s->sysmem = sysmem;
  352. omap_lcdc_reset(s);
  353. memory_region_init_io(&s->iomem, NULL, &omap_lcdc_ops, s, "omap.lcdc", 0x100);
  354. memory_region_add_subregion(sysmem, base, &s->iomem);
  355. s->con = graphic_console_init(NULL, 0, &omap_ops, s);
  356. return s;
  357. }