1 /*
2  *  linux/drivers/video/iplan2p4.c -- Low level frame buffer operations for
3  *				      interleaved bitplanes à la Atari (4
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/string.h>
14 #include <linux/fb.h>
15 
16 #include <asm/setup.h>
17 
18 #include "atafb.h"
19 
20 #define BPL	4
21 #include "atafb_utils.h"
22 
23 void atafb_iplan2p4_copyarea(struct fb_info *info, u_long next_line,
24 			     int sy, int sx, int dy, int dx,
25 			     int height, int width)
26 {
27 	/*  bmove() has to distinguish two major cases: If both, source and
28 	 *  destination, start at even addresses or both are at odd
29 	 *  addresses, just the first odd and last even column (if present)
30 	 *  require special treatment (memmove_col()). The rest between
31 	 *  then can be copied by normal operations, because all adjacent
32 	 *  bytes are affected and are to be stored in the same order.
33 	 *    The pathological case is when the move should go from an odd
34 	 *  address to an even or vice versa. Since the bytes in the plane
35 	 *  words must be assembled in new order, it seems wisest to make
36 	 *  all movements by memmove_col().
37 	 */
38 
39 	u8 *src, *dst;
40 	u32 *s, *d;
41 	int w, l , i, j;
42 	u_int colsize;
43 	u_int upwards = (dy < sy) || (dy == sy && dx < sx);
44 
45 	colsize = height;
46 	if (!((sx ^ dx) & 15)) {
47 		/* odd->odd or even->even */
48 
49 		if (upwards) {
50 			src = (u8 *)info->screen_base + sy * next_line + (sx & ~15) / (8 / BPL);
51 			dst = (u8 *)info->screen_base + dy * next_line + (dx & ~15) / (8 / BPL);
52 			if (sx & 15) {
53 				memmove32_col(dst, src, 0xff00ff, height, next_line - BPL * 2);
54 				src += BPL * 2;
55 				dst += BPL * 2;
56 				width -= 8;
57 			}
58 			w = width >> 4;
59 			if (w) {
60 				s = (u32 *)src;
61 				d = (u32 *)dst;
62 				w *= BPL / 2;
63 				l = next_line - w * 4;
64 				for (j = height; j > 0; j--) {
65 					for (i = w; i > 0; i--)
66 						*d++ = *s++;
67 					s = (u32 *)((u8 *)s + l);
68 					d = (u32 *)((u8 *)d + l);
69 				}
70 			}
71 			if (width & 15)
72 				memmove32_col(dst + width / (8 / BPL), src + width / (8 / BPL),
73 					      0xff00ff00, height, next_line - BPL * 2);
74 		} else {
75 			src = (u8 *)info->screen_base + (sy - 1) * next_line + ((sx + width + 8) & ~15) / (8 / BPL);
76 			dst = (u8 *)info->screen_base + (dy - 1) * next_line + ((dx + width + 8) & ~15) / (8 / BPL);
77 
78 			if ((sx + width) & 15) {
79 				src -= BPL * 2;
80 				dst -= BPL * 2;
81 				memmove32_col(dst, src, 0xff00ff00, colsize, -next_line - BPL * 2);
82 				width -= 8;
83 			}
84 			w = width >> 4;
85 			if (w) {
86 				s = (u32 *)src;
87 				d = (u32 *)dst;
88 				w *= BPL / 2;
89 				l = next_line - w * 4;
90 				for (j = height; j > 0; j--) {
91 					for (i = w; i > 0; i--)
92 						*--d = *--s;
93 					s = (u32 *)((u8 *)s - l);
94 					d = (u32 *)((u8 *)d - l);
95 				}
96 			}
97 			if (sx & 15)
98 				memmove32_col(dst - (width - 16) / (8 / BPL),
99 					      src - (width - 16) / (8 / BPL),
100 					      0xff00ff, colsize, -next_line - BPL * 2);
101 		}
102 	} else {
103 		/* odd->even or even->odd */
104 		if (upwards) {
105 			u32 *src32, *dst32;
106 			u32 pval[4], v, v1, mask;
107 			int i, j, w, f;
108 
109 			src = (u8 *)info->screen_base + sy * next_line + (sx & ~15) / (8 / BPL);
110 			dst = (u8 *)info->screen_base + dy * next_line + (dx & ~15) / (8 / BPL);
111 
112 			mask = 0xff00ff00;
113 			f = 0;
114 			w = width;
115 			if (sx & 15) {
116 				f = 1;
117 				w += 8;
118 			}
119 			if ((sx + width) & 15)
120 				f |= 2;
121 			w >>= 4;
122 			for (i = height; i; i--) {
123 				src32 = (u32 *)src;
124 				dst32 = (u32 *)dst;
125 
126 				if (f & 1) {
127 					pval[0] = (*src32++ << 8) & mask;
128 					pval[1] = (*src32++ << 8) & mask;
129 				} else {
130 					pval[0] = dst32[0] & mask;
131 					pval[1] = dst32[1] & mask;
132 				}
133 
134 				for (j = w; j > 0; j--) {
135 					v = *src32++;
136 					v1 = v & mask;
137 					*dst32++ = pval[0] | (v1 >> 8);
138 					pval[0] = (v ^ v1) << 8;
139 					v = *src32++;
140 					v1 = v & mask;
141 					*dst32++ = pval[1] | (v1 >> 8);
142 					pval[1] = (v ^ v1) << 8;
143 				}
144 
145 				if (f & 2) {
146 					dst32[0] = (dst32[0] & mask) | pval[0];
147 					dst32[1] = (dst32[1] & mask) | pval[1];
148 				}
149 
150 				src += next_line;
151 				dst += next_line;
152 			}
153 		} else {
154 			u32 *src32, *dst32;
155 			u32 pval[4], v, v1, mask;
156 			int i, j, w, f;
157 
158 			src = (u8 *)info->screen_base + (sy - 1) * next_line + ((sx + width + 8) & ~15) / (8 / BPL);
159 			dst = (u8 *)info->screen_base + (dy - 1) * next_line + ((dx + width + 8) & ~15) / (8 / BPL);
160 
161 			mask = 0xff00ff;
162 			f = 0;
163 			w = width;
164 			if ((dx + width) & 15)
165 				f = 1;
166 			if (sx & 15) {
167 				f |= 2;
168 				w += 8;
169 			}
170 			w >>= 4;
171 			for (i = height; i; i--) {
172 				src32 = (u32 *)src;
173 				dst32 = (u32 *)dst;
174 
175 				if (f & 1) {
176 					pval[0] = dst32[-1] & mask;
177 					pval[1] = dst32[-2] & mask;
178 				} else {
179 					pval[0] = (*--src32 >> 8) & mask;
180 					pval[1] = (*--src32 >> 8) & mask;
181 				}
182 
183 				for (j = w; j > 0; j--) {
184 					v = *--src32;
185 					v1 = v & mask;
186 					*--dst32 = pval[0] | (v1 << 8);
187 					pval[0] = (v ^ v1) >> 8;
188 					v = *--src32;
189 					v1 = v & mask;
190 					*--dst32 = pval[1] | (v1 << 8);
191 					pval[1] = (v ^ v1) >> 8;
192 				}
193 
194 				if (!(f & 2)) {
195 					dst32[-1] = (dst32[-1] & mask) | pval[0];
196 					dst32[-2] = (dst32[-2] & mask) | pval[1];
197 				}
198 
199 				src -= next_line;
200 				dst -= next_line;
201 			}
202 		}
203 	}
204 }
205 
206 void atafb_iplan2p4_fillrect(struct fb_info *info, u_long next_line, u32 color,
207                              int sy, int sx, int height, int width)
208 {
209 	u32 *dest;
210 	int rows, i;
211 	u32 cval[4];
212 
213 	dest = (u32 *)(info->screen_base + sy * next_line + (sx & ~15) / (8 / BPL));
214 	if (sx & 15) {
215 		u8 *dest8 = (u8 *)dest + 1;
216 
217 		expand8_col2mask(color, cval);
218 
219 		for (i = height; i; i--) {
220 			fill8_col(dest8, cval);
221 			dest8 += next_line;
222 		}
223 		dest += BPL / 2;
224 		width -= 8;
225 	}
226 
227 	expand16_col2mask(color, cval);
228 	rows = width >> 4;
229 	if (rows) {
230 		u32 *d = dest;
231 		u32 off = next_line - rows * BPL * 2;
232 		for (i = height; i; i--) {
233 			d = fill16_col(d, rows, cval);
234 			d = (u32 *)((long)d + off);
235 		}
236 		dest += rows * BPL / 2;
237 		width &= 15;
238 	}
239 
240 	if (width) {
241 		u8 *dest8 = (u8 *)dest;
242 
243 		expand8_col2mask(color, cval);
244 
245 		for (i = height; i; i--) {
246 			fill8_col(dest8, cval);
247 			dest8 += next_line;
248 		}
249 	}
250 }
251 
252 void atafb_iplan2p4_linefill(struct fb_info *info, u_long next_line,
253                              int dy, int dx, u32 width,
254                              const u8 *data, u32 bgcolor, u32 fgcolor)
255 {
256 	u32 *dest;
257 	const u16 *data16;
258 	int rows;
259 	u32 fgm[4], bgm[4], m;
260 
261 	dest = (u32 *)(info->screen_base + dy * next_line + (dx & ~15) / (8 / BPL));
262 	if (dx & 15) {
263 		fill8_2col((u8 *)dest + 1, fgcolor, bgcolor, *data++);
264 		dest += BPL / 2;
265 		width -= 8;
266 	}
267 
268 	if (width >= 16) {
269 		data16 = (const u16 *)data;
270 		expand16_2col2mask(fgcolor, bgcolor, fgm, bgm);
271 
272 		for (rows = width / 16; rows; rows--) {
273 			u16 d = *data16++;
274 			m = d | ((u32)d << 16);
275 			*dest++ = (m & fgm[0]) ^ bgm[0];
276 			*dest++ = (m & fgm[1]) ^ bgm[1];
277 		}
278 
279 		data = (const u8 *)data16;
280 		width &= 15;
281 	}
282 
283 	if (width)
284 		fill8_2col((u8 *)dest, fgcolor, bgcolor, *data);
285 }
286