]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/omap/screen.c
kernel: handle tos and per process pcycle counters in port/
[plan9front.git] / sys / src / 9 / omap / screen.c
1 /*
2  * ti omap35 display subsystem (dss)
3  *
4  * can handle 2ⁿ bits per pixel for 0 < n ≤ 4, and 12 and 24 bits.
5  * can handle   1024×768 at 60 Hz with pixel clock of 63.5 MHz
6  *              1280×800 at 59.91 Hz with pixel clock of 71 MHz
7  *              1400×1050 lcd at 50 MHz with pixel clock of 75 MHz
8  * has 256 24-bit entries in RGB palette
9  */
10 #include "u.h"
11 #include "../port/lib.h"
12 #include "mem.h"
13 #include "dat.h"
14 #include "fns.h"
15 #include "io.h"
16 #include "ureg.h"
17 #include "../port/error.h"
18
19 #define Image   IMAGE
20 #include <draw.h>
21 #include <memdraw.h>
22 #include <cursor.h>
23 #include "screen.h"
24 // #include "gamma.h"
25
26 enum {
27         Tabstop = 4,            /* should be 8 */
28         Scroll  = 8,            /* lines to scroll at one time */
29         /*
30          * screen settings for Wid and Ht, should a bit more dynamic?
31          * http://www.epanorama.net/faq/vga2rgb/calc.html
32          * used to calculate settings.
33          */
34
35 //      Hbp     = (248-1) << 20,
36 //      Hfp     = (48-1) << 8,
37 //      Hsw     = 112-1,
38
39 //      Vbp     = 38 << 20,
40 //      Vfp     = 1 << 8,
41 //      Vsw     = 3,
42
43         Tft     = 0x60,
44
45         Loadmode = 2 << 1,
46         Fifosize = 0x400,
47
48         /* dispc sysconfig */
49         Midlemode       = 2 << 12,
50         Sidlemode       = 2 << 3,
51         EnableWakeup    = 1 << 2,
52         Autoidle        = 1 << 0,
53
54         /* dispc pool_freq */
55         Ipc             = 1 << 14,
56         Ihs             = 1 << 13,
57         Ivs             = 1 << 12,
58         Acb             = 0x28,
59
60         /* gfx attribs */
61         Burstsize       = 2 << 6,
62         Format          = 6 << 1,
63         Gfxenable       = 1 << 0,
64
65         /* dispc control */
66         Gpout1          = 1 << 16,
67         Gpout0          = 1 << 15,
68         Tftdata         = 3 << 8,
69         Digital         = 1 << 6,
70         Lcd             = 1 << 5,
71         Stntft          = 1 << 3,
72         Digitalen       = 1 << 1,
73 //      Lcden           = 1 << 0,       /* unused */
74 };
75
76 typedef struct Dispcregs Dispc;
77 typedef struct Dssregs Dss;
78 typedef struct Ioregs Ioregs;
79
80 struct Ioregs {                         /* common registers, 68 (0x44) bytes */
81         ulong   rev;
82         uchar   _pad0[0x10-0x4];
83         ulong   sysconf;
84         ulong   sysstat;
85         ulong   irqstat1;
86
87         /* Dispc only regs */
88         ulong   irqen1;
89         ulong   wkupen;
90         ulong   _pad1;
91         ulong   irqsts2;
92         ulong   irqen2;
93         ulong   _pad2[4];
94
95         ulong   ctrl;
96 };
97
98 struct Dssregs {                        /* display subsys at 0x48050000 */
99         Ioregs;
100         ulong   sdicrtl;
101         ulong   pllcrtl;
102         uchar   _pad3[0x5c-0x4c];
103         ulong   sdistat;
104 };
105
106 struct Dispcregs {                      /* display ctlr at 0x48050400 */
107         Ioregs;
108         ulong   config;
109         ulong   _pad3;
110         ulong   defaultcolor[2];
111         ulong   transcolor[2];
112         ulong   linestat;
113         ulong   linenum;
114         ulong   timing_h;
115         ulong   timing_v;
116         ulong   pol_req;
117         ulong   divisor;
118         ulong   alpha;
119         ulong   digsize;
120         ulong   lcdsize;
121
122         ulong   base[2];        /* should allocate both to avoid dithering */
123         ulong   pos;
124         ulong   size;
125         ulong   _pad4[4];
126         ulong   attrib;
127         ulong   fifothr;
128         ulong   fifosize;
129         ulong   rowinc;
130         ulong   pixelinc;
131         ulong   winskip;
132         ulong   palette;                /* gfx_table_ba */
133         uchar   _pad5[0x5d4 - 0x4bc];
134
135         ulong   datacycle[3];
136         uchar   _pad5[0x620 - 0x5e0];
137
138         ulong   cprcoefr;
139         ulong   cprcoefg;
140         ulong   cprcoefb;
141         ulong   preload;
142 };
143
144 OScreen oscreen;
145 Settings settings[] = {
146 [Res800x600]   {  800,  600, 60, RGB16,  40000,  88, 40, 128,   23, 1, 5, },
147 [Res1024x768]  { 1024,  768, 60, RGB16,  65000, 160, 24, 136,   29, 3, 7, },
148 [Res1280x1024] { 1280, 1024, 60, RGB16, 108000, 248, 48, 112,   38, 1, 4, },
149 [Res1400x1050] { 1400, 1050, 50, RGB16, 108000, 248, 48, 112,   38, 1, 4, }, // TODO
150 };
151 Omap3fb *framebuf;
152 Memimage *gscreen;
153
154 static Memdata xgdata;
155
156 static Memimage xgscreen =
157 {
158         { 0, 0, Wid, Ht },      /* r */
159         { 0, 0, Wid, Ht },      /* clipr */
160         Depth,                  /* depth */
161         3,                      /* nchan */
162         RGB16,                  /* chan */
163         nil,                    /* cmap */
164         &xgdata,                /* data */
165         0,                      /* zero */
166         Wid*(Depth/BI2BY)/BY2WD, /* width in words of a single scan line */
167         0,                      /* layer */
168         0,                      /* flags */
169 };
170
171 static Memimage *conscol;
172 static Memimage *back;
173
174 static Memsubfont *memdefont;
175
176 static Lock screenlock;
177
178 static Point    curpos;
179 static int      h, w;
180 static int      landscape = 0;  /* screen orientation, default is 0: portrait */
181 static ushort   *vscreen;       /* virtual screen */
182 static Rectangle window;
183
184 static Dispc *dispc = (Dispc *)PHYSDISPC;
185 static Dss *dss = (Dss *)PHYSDSS;
186
187 static  void    omapscreenputs(char *s, int n);
188 static  ulong   rep(ulong, int);
189 static  void    screenputc(char *buf);
190 static  void    screenwin(void);
191
192 static void
193 lcdoff(void)
194 {
195         dispc->ctrl &= ~1;              /* disable the lcd */
196         coherence();
197
198         dispc->irqstat1 |= 1;           /* set framedone */
199         coherence();
200
201         /* the lcd never comes ready, so don't bother with this */
202 #ifdef notdef
203         /* spin until the frame is complete, but not forever */
204         for(cnt = 50; !(dispc->irqstat1 & 1) && cnt-- > 0; )
205                 delay(10);
206 #endif
207         delay(20);                      /* worst case for 1 frame, 50Hz */
208 }
209
210 static void
211 dssstart(void)
212 {
213         /* should reset the dss system */
214         dss->sysconf |= 1;
215         coherence();
216 }
217
218 /* see spruf98i §15.6.7.4.2 */
219 static void
220 configdispc(void)
221 {
222         Settings *sp;
223
224         sp = oscreen.settings;
225         dss->ctrl &= 0x78;              /* choose dss clock */
226         dispc->sysconf = Midlemode | Sidlemode | EnableWakeup | Autoidle;
227         dispc->config = Loadmode;
228         coherence();
229
230         /* pll */
231         dispc->defaultcolor[0] = 0;     /* set background color to black? */
232         dispc->defaultcolor[1] = 0;
233         dispc->transcolor[0] = 0;       /* set transparency to full */
234         dispc->transcolor[1] = 0;
235
236         dispc->timing_h = (sp->hbp-1) << 20 | (sp->hfp-1) << 8 |
237                         (sp->hsw-1);
238         dispc->timing_v = sp->vbp << 20 | sp->vfp << 8 |
239                         (sp->vsw-1);
240
241         dispc->pol_req = Ipc | Ihs | Ivs | Acb;
242         dispc->divisor = 1 << 16 | ((432000+sp->pixelclock-1)/sp->pixelclock);
243
244         dispc->lcdsize = (sp->ht - 1) << 16 | (sp->wid - 1);
245         coherence();
246
247         dispc->base[0] = PADDR(framebuf->pixel);
248         dispc->base[1] = PADDR(framebuf->pixel);
249
250         dispc->pos = 0;                 /* place screen in the left corner */
251         /* use the whole screen */
252         dispc->size = (sp->ht - 1) << 16 | (sp->wid - 1);
253
254         /* what mode does plan 9 use for fb? */
255         dispc->attrib = Burstsize | Format | Gfxenable;
256
257         dispc->preload = Tft;
258         dispc->fifosize = Fifosize;
259         /* 1008 is max for our Burstsize */
260         dispc->fifothr = (Fifosize - 1) << 16 | (1008 - 1);
261
262         /* 1 byte is one pixel (not true, we use 2 bytes per pixel) */
263         dispc->rowinc = 1;
264         dispc->pixelinc = 1;
265         dispc->winskip = 0;             /* don't skip anything */
266         coherence();
267
268         // dispc->palette = PADDR(framebuf->palette);
269 }
270
271 static void
272 lcdon(int enable)
273 {
274         dispc->ctrl = Gpout1 | Gpout0 | Tftdata | Digital | Lcd | Stntft |
275                 Digitalen | enable;
276         coherence();
277         delay(10);
278 }
279
280 static void
281 lcdstop(void)
282 {
283         configscreengpio();
284         screenclockson();
285
286         lcdoff();
287 }
288
289 static void
290 lcdinit(void)
291 {
292         lcdstop();
293
294         dssstart();
295         configdispc();
296 }
297
298 /* Paint the image data with blue pixels */
299 void
300 screentest(void)
301 {
302         int i;
303
304         for (i = nelem(framebuf->pixel) - 1; i >= 0; i--)
305                 framebuf->pixel[i] = 0x1f;                      /* blue */
306 //      memset(framebuf->pixel, ~0, sizeof framebuf->pixel);    /* white */
307 }
308
309 void
310 screenpower(int on)
311 {
312         blankscreen(on == 0);
313 }
314
315 /* called from devmouse */
316
317 void
318 cursoron(void)
319 {
320         swcursorhide(0);
321         swcursordraw(mousexy());
322 }
323
324 void
325 cursoroff(void)
326 {
327         swcursorhide(0);
328 }
329
330 void
331 setcursor(Cursor* curs)
332 {
333         oscreen.Cursor = *curs;
334         swcursorload(curs);
335 }
336
337 /* called from main and possibly later from devdss to change resolution */
338 void
339 screeninit(void)
340 {
341         static int first = 1;
342
343         if (first) {
344                 iprint("screeninit...");
345                 oscreen.settings = &settings[Res1280x1024];
346
347                 lcdstop();
348                 if (framebuf)
349                         free(framebuf);
350                 /* mode is 16*32 = 512 */
351                 framebuf = xspanalloc(sizeof *framebuf, 16*32, 0);
352         }
353
354         lcdinit();
355         lcdon(1);
356         if (first) {
357                 memimageinit();
358                 memdefont = getmemdefont();
359                 screentest();
360         }
361
362         xgdata.ref = 1;
363         xgdata.bdata = (uchar *)framebuf->pixel;
364
365         gscreen = &xgscreen;
366         gscreen->r = Rect(0, 0, Wid, Ht);
367         gscreen->clipr = gscreen->r;
368         /* width, in words, of a single scan line */
369         gscreen->width = Wid * (Depth / BI2BY) / BY2WD;
370         flushmemscreen(gscreen->r);
371
372         if (first) {
373                 iprint("on: blue for 3 seconds...");
374                 delay(3*1000);
375                 iprint("\n");
376
377                 screenwin();            /* draw border & top orange bar */
378                 screenputs = omapscreenputs;
379                 iprint("screen: frame buffer at %#p for %dx%d\n",
380                         framebuf, oscreen.settings->wid, oscreen.settings->ht);
381
382                 swcursorinit();         /* needs gscreen set */
383                 first = 0;
384         }
385 }
386
387 /* flushmemscreen should change buffer? */
388 void
389 flushmemscreen(Rectangle r)
390 {
391         ulong start, end;
392
393         if (r.min.x < 0)
394                 r.min.x = 0;
395         if (r.max.x > Wid)
396                 r.max.x = Wid;
397         if (r.min.y < 0)
398                 r.min.y = 0;
399         if (r.max.y > Ht)
400                 r.max.y = Ht;
401         if (rectclip(&r, gscreen->r) == 0)
402                 return;
403         start = (ulong)&framebuf->pixel[r.min.y*Wid + r.min.x];
404         end   = (ulong)&framebuf->pixel[(r.max.y - 1)*Wid + r.max.x -1];
405         cachedwbse((ulong *)start, end - start);
406 }
407
408 /*
409  * export screen to devdraw
410  */
411 Memdata*
412 attachscreen(Rectangle *r, ulong *chan, int* d, int *width, int *softscreen)
413 {
414         if(gscreen == nil)
415                 return nil;
416
417         *r = gscreen->r;
418         *d = gscreen->depth;
419         *chan = gscreen->chan;
420         *width = gscreen->width;
421         *softscreen = (landscape == 0);
422
423         gscreen->data->ref++;
424         return gscreen->data;
425 }
426
427 void
428 getcolor(ulong p, ulong *pr, ulong *pg, ulong *pb)
429 {
430         USED(p, pr, pg, pb);
431 }
432
433 int
434 setcolor(ulong p, ulong r, ulong g, ulong b)
435 {
436         USED(p, r, g, b);
437         return 0;
438 }
439
440 void
441 blankscreen(int blank)
442 {
443         if (blank)
444                 lcdon(0);
445         else {
446                 lcdinit();
447                 lcdon(1);
448         }
449 }
450
451 static void
452 omapscreenputs(char *s, int n)
453 {
454         int i;
455         Rune r;
456         char buf[4];
457
458         if (!islo()) {
459                 /* don't deadlock trying to print in interrupt */
460                 if (!canlock(&screenlock))
461                         return;                 /* discard s */
462         } else
463                 lock(&screenlock);
464
465         while (n > 0) {
466                 i = chartorune(&r, s);
467                 if (i == 0) {
468                         s++;
469                         --n;
470                         continue;
471                 }
472                 memmove(buf, s, i);
473                 buf[i] = 0;
474                 n -= i;
475                 s += i;
476                 screenputc(buf);
477         }
478         unlock(&screenlock);
479 }
480
481 static void
482 screenwin(void)
483 {
484         char *greet;
485         Memimage *orange;
486         Point p, q;
487         Rectangle r;
488
489         memsetchan(gscreen, RGB16);
490
491         back = memwhite;
492         conscol = memblack;
493
494         orange = allocmemimage(Rect(0, 0, 1, 1), RGB16);
495         orange->flags |= Frepl;
496         orange->clipr = gscreen->r;
497         orange->data->bdata[0] = 0x40;          /* magic: colour? */
498         orange->data->bdata[1] = 0xfd;          /* magic: colour? */
499
500         w = memdefont->info[' '].width;
501         h = memdefont->height;
502
503         r = insetrect(gscreen->r, 4);
504
505         memimagedraw(gscreen, r, memblack, ZP, memopaque, ZP, S);
506         window = insetrect(r, 4);
507         memimagedraw(gscreen, window, memwhite, ZP, memopaque, ZP, S);
508
509         memimagedraw(gscreen, Rect(window.min.x, window.min.y,
510                 window.max.x, window.min.y + h + 5 + 6), orange, ZP, nil, ZP, S);
511         freememimage(orange);
512         window = insetrect(window, 5);
513
514         greet = " Plan 9 Console ";
515         p = addpt(window.min, Pt(10, 0));
516         q = memsubfontwidth(memdefont, greet);
517         memimagestring(gscreen, p, conscol, ZP, memdefont, greet);
518         flushmemscreen(r);
519         window.min.y += h + 6;
520         curpos = window.min;
521         window.max.y = window.min.y + ((window.max.y - window.min.y) / h) * h;
522 }
523
524 static void
525 scroll(void)
526 {
527         int o;
528         Point p;
529         Rectangle r;
530
531         /* move window contents up Scroll text lines */
532         o = Scroll * h;
533         r = Rpt(window.min, Pt(window.max.x, window.max.y - o));
534         p = Pt(window.min.x, window.min.y + o);
535         memimagedraw(gscreen, r, gscreen, p, nil, p, S);
536         flushmemscreen(r);
537
538         /* clear the bottom Scroll text lines */
539         r = Rpt(Pt(window.min.x, window.max.y - o), window.max);
540         memimagedraw(gscreen, r, back, ZP, nil, ZP, S);
541         flushmemscreen(r);
542
543         curpos.y -= o;
544 }
545
546 static void
547 screenputc(char *buf)
548 {
549         int w;
550         uint pos;
551         Point p;
552         Rectangle r;
553         static int *xp;
554         static int xbuf[256];
555
556         if (xp < xbuf || xp >= &xbuf[nelem(xbuf)])
557                 xp = xbuf;
558
559         switch (buf[0]) {
560         case '\n':
561                 if (curpos.y + h >= window.max.y)
562                         scroll();
563                 curpos.y += h;
564                 screenputc("\r");
565                 break;
566         case '\r':
567                 xp = xbuf;
568                 curpos.x = window.min.x;
569                 break;
570         case '\t':
571                 p = memsubfontwidth(memdefont, " ");
572                 w = p.x;
573                 if (curpos.x >= window.max.x - Tabstop * w)
574                         screenputc("\n");
575
576                 pos = (curpos.x - window.min.x) / w;
577                 pos = Tabstop - pos % Tabstop;
578                 *xp++ = curpos.x;
579                 r = Rect(curpos.x, curpos.y, curpos.x + pos * w, curpos.y + h);
580                 memimagedraw(gscreen, r, back, back->r.min, nil, back->r.min, S);
581                 flushmemscreen(r);
582                 curpos.x += pos * w;
583                 break;
584         case '\b':
585                 if (xp <= xbuf)
586                         break;
587                 xp--;
588                 r = Rect(*xp, curpos.y, curpos.x, curpos.y + h);
589                 memimagedraw(gscreen, r, back, back->r.min, nil, back->r.min, S);
590                 flushmemscreen(r);
591                 curpos.x = *xp;
592                 break;
593         case '\0':
594                 break;
595         default:
596                 p = memsubfontwidth(memdefont, buf);
597                 w = p.x;
598
599                 if (curpos.x >= window.max.x - w)
600                         screenputc("\n");
601
602                 *xp++ = curpos.x;
603                 r = Rect(curpos.x, curpos.y, curpos.x + w, curpos.y + h);
604                 memimagedraw(gscreen, r, back, back->r.min, nil, back->r.min, S);
605                 memimagestring(gscreen, curpos, conscol, ZP, memdefont, buf);
606                 flushmemscreen(r);
607                 curpos.x += w;
608         }
609 }