omap_lcdc.c 10 KB

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