1 /*
2  *  linux/drivers/video/iplan2p8.c -- Low level frame buffer operations for
3  *				      interleaved bitplanes � la Atari (8
4  *				      planes, 2 bytes interleave)
5  *
6  *	Created 5 Apr 1997 by Geert Uytterhoeven
7  *
8  *  This file is subject to the terms and conditions of the GNU General Public
9  *  License.  See the file COPYING in the main directory of this archive for
10  *  more details.
11  */
12 
13 #include <linux/module.h>
14 #include <linux/tty.h>
15 #include <linux/console.h>
16 #include <linux/string.h>
17 #include <linux/fb.h>
18 
19 #include <asm/byteorder.h>
20 
21 #ifdef __mc68000__
22 #include <asm/setup.h>
23 #endif
24 
25 #include <video/fbcon.h>
26 #include <video/fbcon-iplan2p8.h>
27 
28 
29     /*
30      *  Interleaved bitplanes � la Atari (8 planes, 2 bytes interleave)
31      *
32      *  In 8 plane mode, 256 colors would be possible, but only the first
33      *  16 are used by the console code (the upper 4 bits are
34      *  background/unused). For that, the following functions mask off the
35      *  higher 4 bits of each color.
36      */
37 
38 /* Increment/decrement 8 plane addresses */
39 
40 #define	INC_8P(p)	do { if (!((long)(++(p)) & 1)) (p) += 14; } while(0)
41 #define	DEC_8P(p)	do { if ((long)(--(p)) & 1) (p) -= 14; } while(0)
42 
43 /* Perform the m68k movepl operation extended to 64 bits.  */
movepl2(u8 * d,u32 val1,u32 val2)44 static inline void movepl2(u8 *d, u32 val1, u32 val2)
45 {
46 #if defined __mc68000__ && !defined CPU_M68060_ONLY
47     asm volatile ("movepl %1,%0@(0); movepl %2,%0@(8)"
48 		  : : "a" (d), "d" (val1), "d" (val2));
49 #else
50     d[0] = (val1 >> 24) & 0xff;
51     d[2] = (val1 >> 16) & 0xff;
52     d[4] = (val1 >> 8) & 0xff;
53     d[6] = val1 & 0xff;
54     d[8] = (val2 >> 24) & 0xff;
55     d[10] = (val2 >> 16) & 0xff;
56     d[12] = (val2 >> 8) & 0xff;
57     d[14] = val2 & 0xff;
58 #endif
59 }
60 
61 /* Sets the bytes in the visible column at d, height h, to the value
62  * val1,val2 for a 8 plane screen. The bits of the color in 'color' are
63  * moved (8 times) to the respective bytes. This means:
64  *
65  * for(h times; d += bpr)
66  *   *d      = (color & 1) ? 0xff : 0;
67  *   *(d+2)  = (color & 2) ? 0xff : 0;
68  *   *(d+4)  = (color & 4) ? 0xff : 0;
69  *   *(d+6)  = (color & 8) ? 0xff : 0;
70  *   *(d+8)  = (color & 16) ? 0xff : 0;
71  *   *(d+10) = (color & 32) ? 0xff : 0;
72  *   *(d+12) = (color & 64) ? 0xff : 0;
73  *   *(d+14) = (color & 128) ? 0xff : 0;
74  */
75 
memclear_8p_col(void * d,size_t h,u32 val1,u32 val2,int bpr)76 static __inline__ void memclear_8p_col(void *d, size_t h, u32 val1,
77                                        u32 val2, int bpr)
78 {
79     u8 *dd = d;
80     do {
81 	movepl2(dd, val1, val2);
82 	dd += bpr;
83     } while (--h);
84 }
85 
86 /* Sets a 8 plane region from 'd', length 'count' bytes, to the color
87  * val1..val4. 'd' has to be an even address and count must be divisible
88  * by 16, because only whole words and all planes are accessed. I.e.:
89  *
90  * for(count/16 times)
91  *   *d      = *(d+1)  = (color & 1) ? 0xff : 0;
92  *   *(d+2)  = *(d+3)  = (color & 2) ? 0xff : 0;
93  *   *(d+4)  = *(d+5)  = (color & 4) ? 0xff : 0;
94  *   *(d+6)  = *(d+7)  = (color & 8) ? 0xff : 0;
95  *   *(d+8)  = *(d+9)  = (color & 16) ? 0xff : 0;
96  *   *(d+10) = *(d+11) = (color & 32) ? 0xff : 0;
97  *   *(d+12) = *(d+13) = (color & 64) ? 0xff : 0;
98  *   *(d+14) = *(d+15) = (color & 128) ? 0xff : 0;
99  */
100 
memset_even_8p(void * d,size_t count,u32 val1,u32 val2,u32 val3,u32 val4)101 static __inline__ void memset_even_8p(void *d, size_t count, u32 val1,
102                                       u32 val2, u32 val3, u32 val4)
103 {
104     u32 *dd = d;
105 
106     count /= 16;
107     while (count--) {
108 	*dd++ = val1;
109 	*dd++ = val2;
110 	*dd++ = val3;
111 	*dd++ = val4;
112     }
113 }
114 
115 /* Copies a 8 plane column from 's', height 'h', to 'd'. */
116 
memmove_8p_col(void * d,void * s,int h,int bpr)117 static __inline__ void memmove_8p_col (void *d, void *s, int h, int bpr)
118 {
119     u8 *dd = d, *ss = s;
120 
121     while (h--) {
122 	dd[0] = ss[0];
123 	dd[2] = ss[2];
124 	dd[4] = ss[4];
125 	dd[6] = ss[6];
126 	dd[8] = ss[8];
127 	dd[10] = ss[10];
128 	dd[12] = ss[12];
129 	dd[14] = ss[14];
130 	dd += bpr;
131 	ss += bpr;
132     }
133 }
134 
135 
136 /* This expands a 8 bit color into two longs for two movepl (8 plane)
137  * operations.
138  */
139 
140 static const u32 four2long[] =
141 {
142     0x00000000, 0xff000000, 0x00ff0000, 0xffff0000,
143     0x0000ff00, 0xff00ff00, 0x00ffff00, 0xffffff00,
144     0x000000ff, 0xff0000ff, 0x00ff00ff, 0xffff00ff,
145     0x0000ffff, 0xff00ffff, 0x00ffffff, 0xffffffff,
146 };
147 
expand8dl(u8 c,u32 * ret1,u32 * ret2)148 static __inline__ void expand8dl(u8 c, u32 *ret1, u32 *ret2)
149 {
150     *ret1 = four2long[c & 15];
151     *ret2 = four2long[c >> 4];
152 }
153 
154 
155 /* This expands a 8 bit color into four longs for four movel operations
156  * (8 planes).
157  */
158 
159 static const u32 two2word[] =
160 {
161 #ifndef __LITTLE_ENDIAN
162     0x00000000, 0xffff0000, 0x0000ffff, 0xffffffff
163 #else
164     0x00000000, 0x0000ffff, 0xffff0000, 0xffffffff
165 #endif
166 };
167 
expand8ql(u8 c,u32 * rv1,u32 * rv2,u32 * rv3,u32 * rv4)168 static inline void expand8ql(u8 c, u32 *rv1, u32 *rv2, u32 *rv3, u32 *rv4)
169 {
170     *rv1 = two2word[c & 4];
171     *rv2 = two2word[(c >> 2) & 4];
172     *rv3 = two2word[(c >> 4) & 4];
173     *rv4 = two2word[c >> 6];
174 }
175 
176 
177 /* This duplicates a byte 4 times into a long. */
178 
dup4l(u8 c)179 static __inline__ u32 dup4l(u8 c)
180 {
181     u32 rv;
182 
183     rv = c;
184     rv |= rv << 8;
185     rv |= rv << 16;
186     return rv;
187 }
188 
189 
fbcon_iplan2p8_setup(struct display * p)190 void fbcon_iplan2p8_setup(struct display *p)
191 {
192     p->next_line = p->var.xres_virtual;
193     p->next_plane = 2;
194 }
195 
fbcon_iplan2p8_bmove(struct display * p,int sy,int sx,int dy,int dx,int height,int width)196 void fbcon_iplan2p8_bmove(struct display *p, int sy, int sx, int dy, int dx,
197 			  int height, int width)
198 {
199     /*  bmove() has to distinguish two major cases: If both, source and
200      *  destination, start at even addresses or both are at odd
201      *  addresses, just the first odd and last even column (if present)
202      *  require special treatment (memmove_col()). The rest between
203      *  then can be copied by normal operations, because all adjacent
204      *  bytes are affected and are to be stored in the same order.
205      *    The pathological case is when the move should go from an odd
206      *  address to an even or vice versa. Since the bytes in the plane
207      *  words must be assembled in new order, it seems wisest to make
208      *  all movements by memmove_col().
209      */
210 
211      if (sx == 0 && dx == 0 && width * 8 == p->next_line) {
212 	/*  Special (but often used) case: Moving whole lines can be
213 	 *  done with memmove()
214 	 */
215 	fast_memmove(p->screen_base + dy * p->next_line * fontheight(p),
216 		     p->screen_base + sy * p->next_line * fontheight(p),
217 		     p->next_line * height * fontheight(p));
218      } else {
219 	int rows, cols;
220 	u8 *src;
221 	u8 *dst;
222 	int bytes = p->next_line;
223 	int linesize;
224 	u_int colsize;
225 	u_int upwards = (dy < sy) || (dy == sy && dx < sx);
226 
227 	if (fontheightlog(p)) {
228 	    linesize = bytes << fontheightlog(p);
229 	    colsize = height << fontheightlog(p);
230 	} else {
231 	    linesize = bytes * fontheight(p);
232 	    colsize = height * fontheight(p);
233 	}
234 	if ((sx & 1) == (dx & 1)) {
235 	    /* odd->odd or even->even */
236 
237 	    if (upwards) {
238 		src = p->screen_base + sy * linesize + (sx>>1)*16 + (sx & 1);
239 		dst = p->screen_base + dy * linesize + (dx>>1)*16 + (dx & 1);
240 		if (sx & 1) {
241 		    memmove_8p_col(dst, src, colsize, bytes);
242 		    src += 15;
243 		    dst += 15;
244 		    --width;
245 		}
246 		if (width > 1) {
247 		    for(rows = colsize; rows > 0; --rows) {
248 			fast_memmove (dst, src, (width >> 1) * 16);
249 			src += bytes;
250 			dst += bytes;
251 		    }
252 		}
253 
254 		if (width & 1) {
255 		    src -= colsize * bytes;
256 		    dst -= colsize * bytes;
257 		    memmove_8p_col(dst + (width>>1)*16, src + (width>>1)*16,
258 		    colsize, bytes);
259 		}
260 	    } else {
261 		if (!((sx+width-1) & 1)) {
262 		    src = p->screen_base + sy * linesize + ((sx+width-1)>>1)*16;
263 		    dst = p->screen_base + dy * linesize + ((dx+width-1)>>1)*16;
264 		    memmove_8p_col(dst, src, colsize, bytes);
265 		    --width;
266 		}
267 		src = p->screen_base + sy * linesize + (sx>>1)*16 + (sx & 1);
268 		dst = p->screen_base + dy * linesize + (dx>>1)*16 + (dx & 1);
269 		if (width > 1) {
270 		    src += colsize * bytes + (sx & 1)*15;
271 		    dst += colsize * bytes + (sx & 1)*15;
272 		    for(rows = colsize; rows > 0; --rows) {
273 			src -= bytes;
274 			dst -= bytes;
275 			fast_memmove (dst, src, (width>>1)*16);
276 		    }
277 		}
278 		if (width & 1)
279 		    memmove_8p_col(dst-15, src-15, colsize, bytes);
280 	    }
281 	} else {
282 	/* odd->even or even->odd */
283 
284 	    if (upwards) {
285 		src = p->screen_base + sy * linesize + (sx>>1)*16 + (sx & 1);
286 		dst = p->screen_base + dy * linesize + (dx>>1)*16 + (dx & 1);
287 		for(cols = width; cols > 0; --cols) {
288 		    memmove_8p_col(dst, src, colsize, bytes);
289 		    INC_8P(src);
290 		    INC_8P(dst);
291 		}
292 	    } else {
293 		sx += width-1;
294 		dx += width-1;
295 		src = p->screen_base + sy * linesize + (sx>>1)*16 + (sx & 1);
296 		dst = p->screen_base + dy * linesize + (dx>>1)*16 + (dx & 1);
297 		for(cols = width; cols > 0; --cols) {
298 		    memmove_8p_col(dst, src, colsize, bytes);
299 		    DEC_8P(src);
300 		    DEC_8P(dst);
301 		}
302 	    }
303 	}
304     }
305 }
306 
fbcon_iplan2p8_clear(struct vc_data * conp,struct display * p,int sy,int sx,int height,int width)307 void fbcon_iplan2p8_clear(struct vc_data *conp, struct display *p, int sy,
308 			  int sx, int height, int width)
309 {
310     u32 offset;
311     u8 *start;
312     int rows;
313     int bytes = p->next_line;
314     int lines;
315     u32 size;
316     u32 cval1, cval2, cval3, cval4, pcval1, pcval2;
317 
318     expand8ql(attr_bgcol_ec(p,conp), &cval1, &cval2, &cval3, &cval4);
319 
320     if (fontheightlog(p))
321 	lines = height << fontheightlog(p);
322     else
323 	lines = height * fontheight(p);
324 
325     if (sx == 0 && width * 8 == bytes) {
326 	if (fontheightlog(p))
327 	    offset = (sy * bytes) << fontheightlog(p);
328 	else
329 	    offset = sy * bytes * fontheight(p);
330 	size    = lines * bytes;
331 	memset_even_8p(p->screen_base+offset, size, cval1, cval2, cval3, cval4);
332     } else {
333 	if (fontheightlog(p))
334 	    offset = ((sy * bytes) << fontheightlog(p)) + (sx>>1)*16 + (sx & 1);
335 	else
336 	    offset = sy * bytes * fontheight(p) + (sx>>1)*16 + (sx & 1);
337 	start = p->screen_base + offset;
338 	expand8dl(attr_bgcol_ec(p,conp), &pcval1, &pcval2);
339 
340 	/* Clears are split if the region starts at an odd column or
341 	* end at an even column. These extra columns are spread
342 	* across the interleaved planes. All in between can be
343 	* cleared by normal fb_memclear_small(), because both bytes of
344 	* the single plane words are affected.
345 	*/
346 
347 	if (sx & 1) {
348 	    memclear_8p_col(start, lines, pcval1, pcval2, bytes);
349 	    start += 7;
350 	    width--;
351 	}
352 	if (width & 1) {
353 	    memclear_8p_col(start + (width>>1)*16, lines, pcval1,
354 	    pcval2, bytes);
355 	    width--;
356 	}
357 	if (width)
358 	    for(rows = lines; rows-- ; start += bytes)
359 		memset_even_8p(start, width*8, cval1, cval2, cval3, cval4);
360 	}
361 }
362 
fbcon_iplan2p8_putc(struct vc_data * conp,struct display * p,int c,int yy,int xx)363 void fbcon_iplan2p8_putc(struct vc_data *conp, struct display *p, int c,
364 			 int yy, int xx)
365 {
366     u8 *dest;
367     u8 *cdat;
368     int rows;
369     int bytes = p->next_line;
370     u32 eorx1, eorx2, fgx1, fgx2, bgx1, bgx2, fdx;
371 
372     if (fontheightlog(p)) {
373 	dest = (p->screen_base + ((yy * bytes) << fontheightlog(p)) +
374 		(xx>>1)*16 + (xx & 1));
375 	cdat = p->fontdata + ((c & p->charmask) << fontheightlog(p));
376     } else {
377 	dest = (p->screen_base + yy * bytes * fontheight(p) +
378 		(xx>>1)*16 + (xx & 1));
379 	cdat = p->fontdata + (c & p->charmask) * fontheight(p);
380     }
381 
382     expand8dl(attr_fgcol(p,c), &fgx1, &fgx2);
383     expand8dl(attr_bgcol(p,c), &bgx1, &bgx2);
384     eorx1 = fgx1 ^ bgx1; eorx2  = fgx2 ^ bgx2;
385 
386     for(rows = fontheight(p) ; rows-- ; dest += bytes) {
387 	fdx = dup4l(*cdat++);
388 	movepl2(dest, (fdx & eorx1) ^ bgx1, (fdx & eorx2) ^ bgx2);
389     }
390 }
391 
fbcon_iplan2p8_putcs(struct vc_data * conp,struct display * p,const unsigned short * s,int count,int yy,int xx)392 void fbcon_iplan2p8_putcs(struct vc_data *conp, struct display *p,
393 			  const unsigned short *s, int count, int yy, int xx)
394 {
395     u8 *dest, *dest0;
396     u8 *cdat;
397     u16 c;
398     int rows;
399     int bytes;
400     u32 eorx1, eorx2, fgx1, fgx2, bgx1, bgx2, fdx;
401 
402     bytes = p->next_line;
403     if (fontheightlog(p))
404 	dest0 = (p->screen_base + ((yy * bytes) << fontheightlog(p)) +
405 		 (xx>>1)*16 + (xx & 1));
406     else
407 	dest0 = (p->screen_base + yy * bytes * fontheight(p) +
408 		 (xx>>1)*16 + (xx & 1));
409 
410     c = scr_readw(s);
411     expand8dl(attr_fgcol(p, c), &fgx1, &fgx2);
412     expand8dl(attr_bgcol(p, c), &bgx1, &bgx2);
413     eorx1 = fgx1 ^ bgx1; eorx2  = fgx2 ^ bgx2;
414 
415     while (count--) {
416 
417 	/* I think, unrolling the loops like in the 1 plane case isn't
418 	* practicable here, because the body is much longer for 4
419 	* planes (mostly the dup4l()). I guess, unrolling this would
420 	* need more than 256 bytes and so exceed the instruction
421 	* cache :-(
422 	*/
423 
424 	c = scr_readw(s++) & p->charmask;
425 	if (fontheightlog(p))
426 	    cdat = p->fontdata + (c << fontheightlog(p));
427 	else
428 	    cdat = p->fontdata + c * fontheight(p);
429 
430 	for(rows = fontheight(p), dest = dest0; rows-- ; dest += bytes) {
431 	    fdx = dup4l(*cdat++);
432 	    movepl2(dest, (fdx & eorx1) ^ bgx1, (fdx & eorx2) ^ bgx2);
433 	}
434 	INC_8P(dest0);
435     }
436 }
437 
fbcon_iplan2p8_revc(struct display * p,int xx,int yy)438 void fbcon_iplan2p8_revc(struct display *p, int xx, int yy)
439 {
440     u8 *dest;
441     int j;
442     int bytes;
443 
444     if (fontheightlog(p))
445 	dest = (p->screen_base + ((yy * p->next_line) << fontheightlog(p)) +
446 		(xx>>1)*16 + (xx & 1));
447     else
448 	dest = (p->screen_base + yy * p->next_line * fontheight(p) +
449 		(xx>>1)*16 + (xx & 1));
450     j = fontheight(p);
451     bytes = p->next_line;
452 
453     while (j--) {
454 	/*  This should really obey the individual character's
455 	 *  background and foreground colors instead of simply
456 	 *  inverting. For 8 plane mode, only the lower 4 bits of the
457 	 *  color are inverted, because only these color registers have
458 	 *  been set up.
459 	 */
460 	dest[0] = ~dest[0];
461 	dest[2] = ~dest[2];
462 	dest[4] = ~dest[4];
463 	dest[6] = ~dest[6];
464 	dest += bytes;
465     }
466 }
467 
fbcon_iplan2p8_clear_margins(struct vc_data * conp,struct display * p,int bottom_only)468 void fbcon_iplan2p8_clear_margins(struct vc_data *conp, struct display *p,
469 				  int bottom_only)
470 {
471     u32 offset;
472     int bytes;
473     int lines;
474     u32 cval1, cval2, cval3, cval4;
475 
476 /* No need to handle right margin, cannot occur with fontwidth == 8 */
477 
478     bytes = p->next_line;
479     if (fontheightlog(p)) {
480 	lines = p->var.yres - (conp->vc_rows << fontheightlog(p));
481 	offset = ((p->yscroll + conp->vc_rows) * bytes) << fontheightlog(p);
482     } else {
483 	lines = p->var.yres - conp->vc_rows * fontheight(p);
484 	offset = (p->yscroll + conp->vc_rows) * bytes * fontheight(p);
485     }
486     if (lines) {
487 	expand8ql(attr_bgcol_ec(p,conp), &cval1, &cval2, &cval3, &cval4);
488 	memset_even_8p(p->screen_base+offset, lines * bytes,
489 		       cval1, cval2, cval3, cval4);
490     }
491 }
492 
493 
494     /*
495      *  `switch' for the low level operations
496      */
497 
498 struct display_switch fbcon_iplan2p8 = {
499     setup:		fbcon_iplan2p8_setup,
500     bmove:		fbcon_iplan2p8_bmove,
501     clear:		fbcon_iplan2p8_clear,
502     putc:		fbcon_iplan2p8_putc,
503     putcs:		fbcon_iplan2p8_putcs,
504     revc:		fbcon_iplan2p8_revc,
505     clear_margins:	fbcon_iplan2p8_clear_margins,
506     fontwidthmask:	FONTWIDTH(8)
507 };
508 
509 
510 #ifdef MODULE
511 MODULE_LICENSE("GPL");
512 
init_module(void)513 int init_module(void)
514 {
515     return 0;
516 }
517 
cleanup_module(void)518 void cleanup_module(void)
519 {}
520 #endif /* MODULE */
521 
522 
523     /*
524      *  Visible symbols for modules
525      */
526 
527 EXPORT_SYMBOL(fbcon_iplan2p8);
528 EXPORT_SYMBOL(fbcon_iplan2p8_setup);
529 EXPORT_SYMBOL(fbcon_iplan2p8_bmove);
530 EXPORT_SYMBOL(fbcon_iplan2p8_clear);
531 EXPORT_SYMBOL(fbcon_iplan2p8_putc);
532 EXPORT_SYMBOL(fbcon_iplan2p8_putcs);
533 EXPORT_SYMBOL(fbcon_iplan2p8_revc);
534 EXPORT_SYMBOL(fbcon_iplan2p8_clear_margins);
535