1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * newport_con.c: Abscon for newport hardware
4  *
5  * (C) 1998 Thomas Bogendoerfer (tsbogend@alpha.franken.de)
6  * (C) 1999 Ulf Carlsson (ulfc@thepuffingruop.com)
7  *
8  * This driver is based on sgicons.c and cons_newport.
9  *
10  * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
11  * Copyright (C) 1997 Miguel de Icaza (miguel@nuclecu.unam.mx)
12  */
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/kd.h>
17 #include <linux/selection.h>
18 #include <linux/console.h>
19 #include <linux/vt_kern.h>
20 #include <linux/mm.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 
24 #include <asm/io.h>
25 #include <linux/uaccess.h>
26 #include <asm/page.h>
27 #include <asm/gio_device.h>
28 
29 #include <video/newport.h>
30 
31 #include <linux/linux_logo.h>
32 #include <linux/font.h>
33 
34 #define NEWPORT_LEN	0x10000
35 
36 #define FONT_DATA ((unsigned char *)font_vga_8x16.data)
37 
38 /* borrowed from fbcon.c */
39 #define REFCOUNT(fd)	(((int *)(fd))[-1])
40 #define FNTSIZE(fd)	(((int *)(fd))[-2])
41 #define FNTCHARCNT(fd)	(((int *)(fd))[-3])
42 #define FONT_EXTRA_WORDS 3
43 
44 static unsigned char *font_data[MAX_NR_CONSOLES];
45 
46 static struct newport_regs *npregs;
47 static unsigned long newport_addr;
48 
49 static int logo_active;
50 static int topscan;
51 static int xcurs_correction = 29;
52 static int newport_xsize;
53 static int newport_ysize;
54 static int newport_has_init;
55 
56 static int newport_set_def_font(int unit, struct console_font *op);
57 
58 #define BMASK(c) (c << 24)
59 
60 #define RENDER(regs, cp) do { \
61 (regs)->go.zpattern = BMASK((cp)[0x0]); (regs)->go.zpattern = BMASK((cp)[0x1]); \
62 (regs)->go.zpattern = BMASK((cp)[0x2]); (regs)->go.zpattern = BMASK((cp)[0x3]); \
63 (regs)->go.zpattern = BMASK((cp)[0x4]); (regs)->go.zpattern = BMASK((cp)[0x5]); \
64 (regs)->go.zpattern = BMASK((cp)[0x6]); (regs)->go.zpattern = BMASK((cp)[0x7]); \
65 (regs)->go.zpattern = BMASK((cp)[0x8]); (regs)->go.zpattern = BMASK((cp)[0x9]); \
66 (regs)->go.zpattern = BMASK((cp)[0xa]); (regs)->go.zpattern = BMASK((cp)[0xb]); \
67 (regs)->go.zpattern = BMASK((cp)[0xc]); (regs)->go.zpattern = BMASK((cp)[0xd]); \
68 (regs)->go.zpattern = BMASK((cp)[0xe]); (regs)->go.zpattern = BMASK((cp)[0xf]); \
69 } while(0)
70 
71 #define TESTVAL 0xdeadbeef
72 #define XSTI_TO_FXSTART(val) (((val) & 0xffff) << 11)
73 
74 static inline void newport_render_background(int xstart, int ystart,
75 					     int xend, int yend, int ci)
76 {
77 	newport_wait(npregs);
78 	npregs->set.wrmask = 0xffffffff;
79 	npregs->set.drawmode0 = (NPORT_DMODE0_DRAW | NPORT_DMODE0_BLOCK |
80 				 NPORT_DMODE0_DOSETUP | NPORT_DMODE0_STOPX
81 				 | NPORT_DMODE0_STOPY);
82 	npregs->set.colori = ci;
83 	npregs->set.xystarti =
84 	    (xstart << 16) | ((ystart + topscan) & 0x3ff);
85 	npregs->go.xyendi =
86 	    ((xend + 7) << 16) | ((yend + topscan + 15) & 0x3ff);
87 }
88 
89 static inline void newport_init_cmap(void)
90 {
91 	unsigned short i;
92 
93 	for (i = 0; i < 16; i++) {
94 		newport_bfwait(npregs);
95 		newport_cmap_setaddr(npregs, color_table[i]);
96 		newport_cmap_setrgb(npregs,
97 				    default_red[i],
98 				    default_grn[i], default_blu[i]);
99 	}
100 }
101 
102 static const struct linux_logo *newport_show_logo(void)
103 {
104 #ifdef CONFIG_LOGO_SGI_CLUT224
105 	const struct linux_logo *logo = fb_find_logo(8);
106 	const unsigned char *clut;
107 	const unsigned char *data;
108 	unsigned long i;
109 
110 	if (!logo)
111 		return NULL;
112 	clut = logo->clut;
113 	data = logo->data;
114 
115 	for (i = 0; i < logo->clutsize; i++) {
116 		newport_bfwait(npregs);
117 		newport_cmap_setaddr(npregs, i + 0x20);
118 		newport_cmap_setrgb(npregs, clut[0], clut[1], clut[2]);
119 		clut += 3;
120 	}
121 
122 	newport_wait(npregs);
123 	npregs->set.drawmode0 = (NPORT_DMODE0_DRAW | NPORT_DMODE0_BLOCK |
124 				 NPORT_DMODE0_CHOST);
125 
126 	npregs->set.xystarti = ((newport_xsize - logo->width) << 16) | (0);
127 	npregs->set.xyendi = ((newport_xsize - 1) << 16);
128 	newport_wait(npregs);
129 
130 	for (i = 0; i < logo->width*logo->height; i++)
131 		npregs->go.hostrw0 = *data++ << 24;
132 
133 	return logo;
134 #endif /* CONFIG_LOGO_SGI_CLUT224 */
135 }
136 
137 static inline void newport_clear_screen(int xstart, int ystart, int xend,
138 					int yend, int ci)
139 {
140 	if (logo_active)
141 		return;
142 
143 	newport_wait(npregs);
144 	npregs->set.wrmask = 0xffffffff;
145 	npregs->set.drawmode0 = (NPORT_DMODE0_DRAW | NPORT_DMODE0_BLOCK |
146 				 NPORT_DMODE0_DOSETUP | NPORT_DMODE0_STOPX
147 				 | NPORT_DMODE0_STOPY);
148 	npregs->set.colori = ci;
149 	npregs->set.xystarti = (xstart << 16) | ystart;
150 	npregs->go.xyendi = (xend << 16) | yend;
151 }
152 
153 static inline void newport_clear_lines(int ystart, int yend, int ci)
154 {
155 	ystart = ((ystart << 4) + topscan) & 0x3ff;
156 	yend = ((yend << 4) + topscan + 15) & 0x3ff;
157 	newport_clear_screen(0, ystart, 1280 + 63, yend, ci);
158 }
159 
160 static void newport_reset(void)
161 {
162 	unsigned short treg;
163 	int i;
164 
165 	newport_wait(npregs);
166 	treg = newport_vc2_get(npregs, VC2_IREG_CONTROL);
167 	newport_vc2_set(npregs, VC2_IREG_CONTROL,
168 			(treg | VC2_CTRL_EVIDEO));
169 
170 	treg = newport_vc2_get(npregs, VC2_IREG_CENTRY);
171 	newport_vc2_set(npregs, VC2_IREG_RADDR, treg);
172 	npregs->set.dcbmode = (NPORT_DMODE_AVC2 | VC2_REGADDR_RAM |
173 			       NPORT_DMODE_W2 | VC2_PROTOCOL);
174 	for (i = 0; i < 128; i++) {
175 		newport_bfwait(npregs);
176 		if (i == 92 || i == 94)
177 			npregs->set.dcbdata0.byshort.s1 = 0xff00;
178 		else
179 			npregs->set.dcbdata0.byshort.s1 = 0x0000;
180 	}
181 
182 	newport_init_cmap();
183 
184 	/* turn off popup plane */
185 	npregs->set.dcbmode = (DCB_XMAP0 | R_DCB_XMAP9_PROTOCOL |
186 			       XM9_CRS_CONFIG | NPORT_DMODE_W1);
187 	npregs->set.dcbdata0.bybytes.b3 &= ~XM9_PUPMODE;
188 	npregs->set.dcbmode = (DCB_XMAP1 | R_DCB_XMAP9_PROTOCOL |
189 			       XM9_CRS_CONFIG | NPORT_DMODE_W1);
190 	npregs->set.dcbdata0.bybytes.b3 &= ~XM9_PUPMODE;
191 
192 	topscan = 0;
193 	npregs->cset.topscan = 0x3ff;
194 	npregs->cset.xywin = (4096 << 16) | 4096;
195 
196 	/* Clear the screen. */
197 	newport_clear_screen(0, 0, 1280 + 63, 1024, 0);
198 }
199 
200 /*
201  * calculate the actual screen size by reading
202  * the video timing out of the VC2
203  */
204 static void newport_get_screensize(void)
205 {
206 	int i, cols;
207 	unsigned short ventry, treg;
208 	unsigned short linetable[128];	/* should be enough */
209 
210 	ventry = newport_vc2_get(npregs, VC2_IREG_VENTRY);
211 	newport_vc2_set(npregs, VC2_IREG_RADDR, ventry);
212 	npregs->set.dcbmode = (NPORT_DMODE_AVC2 | VC2_REGADDR_RAM |
213 			       NPORT_DMODE_W2 | VC2_PROTOCOL);
214 	for (i = 0; i < 128; i++) {
215 		newport_bfwait(npregs);
216 		linetable[i] = npregs->set.dcbdata0.byshort.s1;
217 	}
218 
219 	newport_xsize = newport_ysize = 0;
220 	for (i = 0; i < ARRAY_SIZE(linetable) - 1 && linetable[i + 1]; i += 2) {
221 		cols = 0;
222 		newport_vc2_set(npregs, VC2_IREG_RADDR, linetable[i]);
223 		npregs->set.dcbmode = (NPORT_DMODE_AVC2 | VC2_REGADDR_RAM |
224 				       NPORT_DMODE_W2 | VC2_PROTOCOL);
225 		do {
226 			newport_bfwait(npregs);
227 			treg = npregs->set.dcbdata0.byshort.s1;
228 			if ((treg & 1) == 0)
229 				cols += (treg >> 7) & 0xfe;
230 			if ((treg & 0x80) == 0) {
231 				newport_bfwait(npregs);
232 				treg = npregs->set.dcbdata0.byshort.s1;
233 			}
234 		} while ((treg & 0x8000) == 0);
235 		if (cols) {
236 			if (cols > newport_xsize)
237 				newport_xsize = cols;
238 			newport_ysize += linetable[i + 1];
239 		}
240 	}
241 	printk("NG1: Screensize %dx%d\n", newport_xsize, newport_ysize);
242 }
243 
244 static void newport_get_revisions(void)
245 {
246 	unsigned int tmp;
247 	unsigned int board_rev;
248 	unsigned int rex3_rev;
249 	unsigned int vc2_rev;
250 	unsigned int cmap_rev;
251 	unsigned int xmap9_rev;
252 	unsigned int bt445_rev;
253 	unsigned int bitplanes;
254 
255 	rex3_rev = npregs->cset.status & NPORT_STAT_VERS;
256 
257 	npregs->set.dcbmode = (DCB_CMAP0 | NCMAP_PROTOCOL |
258 			       NCMAP_REGADDR_RREG | NPORT_DMODE_W1);
259 	tmp = npregs->set.dcbdata0.bybytes.b3;
260 	cmap_rev = tmp & 7;
261 	board_rev = (tmp >> 4) & 7;
262 	bitplanes = ((board_rev > 1) && (tmp & 0x80)) ? 8 : 24;
263 
264 	npregs->set.dcbmode = (DCB_CMAP1 | NCMAP_PROTOCOL |
265 			       NCMAP_REGADDR_RREG | NPORT_DMODE_W1);
266 	tmp = npregs->set.dcbdata0.bybytes.b3;
267 	if ((tmp & 7) < cmap_rev)
268 		cmap_rev = (tmp & 7);
269 
270 	vc2_rev = (newport_vc2_get(npregs, VC2_IREG_CONFIG) >> 5) & 7;
271 
272 	npregs->set.dcbmode = (DCB_XMAP0 | R_DCB_XMAP9_PROTOCOL |
273 			       XM9_CRS_REVISION | NPORT_DMODE_W1);
274 	xmap9_rev = npregs->set.dcbdata0.bybytes.b3 & 7;
275 
276 	npregs->set.dcbmode = (DCB_BT445 | BT445_PROTOCOL |
277 			       BT445_CSR_ADDR_REG | NPORT_DMODE_W1);
278 	npregs->set.dcbdata0.bybytes.b3 = BT445_REVISION_REG;
279 	npregs->set.dcbmode = (DCB_BT445 | BT445_PROTOCOL |
280 			       BT445_CSR_REVISION | NPORT_DMODE_W1);
281 	bt445_rev = (npregs->set.dcbdata0.bybytes.b3 >> 4) - 0x0a;
282 
283 #define L(a)     (char)('A'+(a))
284 	printk
285 	    ("NG1: Revision %d, %d bitplanes, REX3 revision %c, VC2 revision %c, xmap9 revision %c, cmap revision %c, bt445 revision %c\n",
286 	     board_rev, bitplanes, L(rex3_rev), L(vc2_rev), L(xmap9_rev),
287 	     L(cmap_rev ? (cmap_rev + 1) : 0), L(bt445_rev));
288 #undef L
289 
290 	if (board_rev == 3)	/* I don't know all affected revisions */
291 		xcurs_correction = 21;
292 }
293 
294 static void newport_exit(void)
295 {
296 	int i;
297 
298 	/* free memory used by user font */
299 	for (i = 0; i < MAX_NR_CONSOLES; i++)
300 		newport_set_def_font(i, NULL);
301 }
302 
303 /* Can't be __init, do_take_over_console may call it later */
304 static const char *newport_startup(void)
305 {
306 	int i;
307 
308 	npregs->cset.config = NPORT_CFG_GD0;
309 
310 	if (newport_wait(npregs))
311 		goto out_unmap;
312 
313 	npregs->set.xstarti = TESTVAL;
314 	if (npregs->set._xstart.word != XSTI_TO_FXSTART(TESTVAL))
315 		goto out_unmap;
316 
317 	for (i = 0; i < MAX_NR_CONSOLES; i++)
318 		font_data[i] = FONT_DATA;
319 
320 	newport_reset();
321 	newport_get_revisions();
322 	newport_get_screensize();
323 	newport_has_init = 1;
324 
325 	return "SGI Newport";
326 
327 out_unmap:
328 	return NULL;
329 }
330 
331 static void newport_init(struct vc_data *vc, int init)
332 {
333 	int cols, rows;
334 
335 	cols = newport_xsize / 8;
336 	rows = newport_ysize / 16;
337 	vc->vc_can_do_color = 1;
338 	if (init) {
339 		vc->vc_cols = cols;
340 		vc->vc_rows = rows;
341 	} else
342 		vc_resize(vc, cols, rows);
343 }
344 
345 static void newport_deinit(struct vc_data *c)
346 {
347 	if (!con_is_bound(&newport_con) && newport_has_init) {
348 		newport_exit();
349 		newport_has_init = 0;
350 	}
351 }
352 
353 static void newport_clear(struct vc_data *vc, int sy, int sx, int height,
354 			  int width)
355 {
356 	int xend = ((sx + width) << 3) - 1;
357 	int ystart = ((sy << 4) + topscan) & 0x3ff;
358 	int yend = (((sy + height) << 4) + topscan - 1) & 0x3ff;
359 
360 	if (logo_active)
361 		return;
362 
363 	if (ystart < yend) {
364 		newport_clear_screen(sx << 3, ystart, xend, yend,
365 				     (vc->state.color & 0xf0) >> 4);
366 	} else {
367 		newport_clear_screen(sx << 3, ystart, xend, 1023,
368 				     (vc->state.color & 0xf0) >> 4);
369 		newport_clear_screen(sx << 3, 0, xend, yend,
370 				     (vc->state.color & 0xf0) >> 4);
371 	}
372 }
373 
374 static void newport_putc(struct vc_data *vc, int charattr, int ypos,
375 			 int xpos)
376 {
377 	unsigned char *p;
378 
379 	p = &font_data[vc->vc_num][(charattr & 0xff) << 4];
380 	charattr = (charattr >> 8) & 0xff;
381 	xpos <<= 3;
382 	ypos <<= 4;
383 
384 	newport_render_background(xpos, ypos, xpos, ypos,
385 				  (charattr & 0xf0) >> 4);
386 
387 	/* Set the color and drawing mode. */
388 	newport_wait(npregs);
389 	npregs->set.colori = charattr & 0xf;
390 	npregs->set.drawmode0 = (NPORT_DMODE0_DRAW | NPORT_DMODE0_BLOCK |
391 				 NPORT_DMODE0_STOPX | NPORT_DMODE0_ZPENAB |
392 				 NPORT_DMODE0_L32);
393 
394 	/* Set coordinates for bitmap operation. */
395 	npregs->set.xystarti = (xpos << 16) | ((ypos + topscan) & 0x3ff);
396 	npregs->set.xyendi = ((xpos + 7) << 16);
397 	newport_wait(npregs);
398 
399 	/* Go, baby, go... */
400 	RENDER(npregs, p);
401 }
402 
403 static void newport_putcs(struct vc_data *vc, const unsigned short *s,
404 			  int count, int ypos, int xpos)
405 {
406 	int i;
407 	int charattr;
408 	unsigned char *p;
409 
410 	charattr = (scr_readw(s) >> 8) & 0xff;
411 
412 	xpos <<= 3;
413 	ypos <<= 4;
414 
415 	if (!logo_active)
416 		/* Clear the area behing the string */
417 		newport_render_background(xpos, ypos,
418 					  xpos + ((count - 1) << 3), ypos,
419 					  (charattr & 0xf0) >> 4);
420 
421 	newport_wait(npregs);
422 
423 	/* Set the color and drawing mode. */
424 	npregs->set.colori = charattr & 0xf;
425 	npregs->set.drawmode0 = (NPORT_DMODE0_DRAW | NPORT_DMODE0_BLOCK |
426 				 NPORT_DMODE0_STOPX | NPORT_DMODE0_ZPENAB |
427 				 NPORT_DMODE0_L32);
428 
429 	for (i = 0; i < count; i++, xpos += 8) {
430 		p = &font_data[vc->vc_num][(scr_readw(s++) & 0xff) << 4];
431 
432 		newport_wait(npregs);
433 
434 		/* Set coordinates for bitmap operation. */
435 		npregs->set.xystarti =
436 		    (xpos << 16) | ((ypos + topscan) & 0x3ff);
437 		npregs->set.xyendi = ((xpos + 7) << 16);
438 
439 		/* Go, baby, go... */
440 		RENDER(npregs, p);
441 	}
442 }
443 
444 static void newport_cursor(struct vc_data *vc, int mode)
445 {
446 	unsigned short treg;
447 	int xcurs, ycurs;
448 
449 	switch (mode) {
450 	case CM_ERASE:
451 		treg = newport_vc2_get(npregs, VC2_IREG_CONTROL);
452 		newport_vc2_set(npregs, VC2_IREG_CONTROL,
453 				(treg & ~(VC2_CTRL_ECDISP)));
454 		break;
455 
456 	case CM_MOVE:
457 	case CM_DRAW:
458 		treg = newport_vc2_get(npregs, VC2_IREG_CONTROL);
459 		newport_vc2_set(npregs, VC2_IREG_CONTROL,
460 				(treg | VC2_CTRL_ECDISP));
461 		xcurs = (vc->vc_pos - vc->vc_visible_origin) / 2;
462 		ycurs = ((xcurs / vc->vc_cols) << 4) + 31;
463 		xcurs = ((xcurs % vc->vc_cols) << 3) + xcurs_correction;
464 		newport_vc2_set(npregs, VC2_IREG_CURSX, xcurs);
465 		newport_vc2_set(npregs, VC2_IREG_CURSY, ycurs);
466 	}
467 }
468 
469 static int newport_switch(struct vc_data *vc)
470 {
471 	static int logo_drawn = 0;
472 
473 	topscan = 0;
474 	npregs->cset.topscan = 0x3ff;
475 
476 	if (!logo_drawn) {
477 		if (newport_show_logo()) {
478 			logo_drawn = 1;
479 			logo_active = 1;
480 		}
481 	}
482 
483 	return 1;
484 }
485 
486 static int newport_blank(struct vc_data *c, int blank, int mode_switch)
487 {
488 	unsigned short treg;
489 
490 	if (blank == 0) {
491 		/* unblank console */
492 		treg = newport_vc2_get(npregs, VC2_IREG_CONTROL);
493 		newport_vc2_set(npregs, VC2_IREG_CONTROL,
494 				(treg | VC2_CTRL_EDISP));
495 	} else {
496 		/* blank console */
497 		treg = newport_vc2_get(npregs, VC2_IREG_CONTROL);
498 		newport_vc2_set(npregs, VC2_IREG_CONTROL,
499 				(treg & ~(VC2_CTRL_EDISP)));
500 	}
501 	return 1;
502 }
503 
504 static int newport_set_font(int unit, struct console_font *op)
505 {
506 	int w = op->width;
507 	int h = op->height;
508 	int size = h * op->charcount;
509 	int i;
510 	unsigned char *new_data, *data = op->data, *p;
511 
512 	/* ladis: when I grow up, there will be a day... and more sizes will
513 	 * be supported ;-) */
514 	if ((w != 8) || (h != 16)
515 	    || (op->charcount != 256 && op->charcount != 512))
516 		return -EINVAL;
517 
518 	if (!(new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size,
519 	     GFP_USER))) return -ENOMEM;
520 
521 	new_data += FONT_EXTRA_WORDS * sizeof(int);
522 	FNTSIZE(new_data) = size;
523 	FNTCHARCNT(new_data) = op->charcount;
524 	REFCOUNT(new_data) = 0;	/* usage counter */
525 
526 	p = new_data;
527 	for (i = 0; i < op->charcount; i++) {
528 		memcpy(p, data, h);
529 		data += 32;
530 		p += h;
531 	}
532 
533 	/* check if font is already used by other console */
534 	for (i = 0; i < MAX_NR_CONSOLES; i++) {
535 		if (font_data[i] != FONT_DATA
536 		    && FNTSIZE(font_data[i]) == size
537 		    && !memcmp(font_data[i], new_data, size)) {
538 			kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
539 			/* current font is the same as the new one */
540 			if (i == unit)
541 				return 0;
542 			new_data = font_data[i];
543 			break;
544 		}
545 	}
546 	/* old font is user font */
547 	if (font_data[unit] != FONT_DATA) {
548 		if (--REFCOUNT(font_data[unit]) == 0)
549 			kfree(font_data[unit] -
550 			      FONT_EXTRA_WORDS * sizeof(int));
551 	}
552 	REFCOUNT(new_data)++;
553 	font_data[unit] = new_data;
554 
555 	return 0;
556 }
557 
558 static int newport_set_def_font(int unit, struct console_font *op)
559 {
560 	if (font_data[unit] != FONT_DATA) {
561 		if (--REFCOUNT(font_data[unit]) == 0)
562 			kfree(font_data[unit] -
563 			      FONT_EXTRA_WORDS * sizeof(int));
564 		font_data[unit] = FONT_DATA;
565 	}
566 
567 	return 0;
568 }
569 
570 static int newport_font_default(struct vc_data *vc, struct console_font *op, char *name)
571 {
572 	return newport_set_def_font(vc->vc_num, op);
573 }
574 
575 static int newport_font_set(struct vc_data *vc, struct console_font *font, unsigned flags)
576 {
577 	return newport_set_font(vc->vc_num, font);
578 }
579 
580 static bool newport_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
581 		enum con_scroll dir, unsigned int lines)
582 {
583 	int count, x, y;
584 	unsigned short *s, *d;
585 	unsigned short chattr;
586 
587 	logo_active = 0;	/* it's time to disable the logo now.. */
588 
589 	if (t == 0 && b == vc->vc_rows) {
590 		if (dir == SM_UP) {
591 			topscan = (topscan + (lines << 4)) & 0x3ff;
592 			newport_clear_lines(vc->vc_rows - lines,
593 					    vc->vc_rows - 1,
594 					    (vc->state.color & 0xf0) >> 4);
595 		} else {
596 			topscan = (topscan + (-lines << 4)) & 0x3ff;
597 			newport_clear_lines(0, lines - 1,
598 					    (vc->state.color & 0xf0) >> 4);
599 		}
600 		npregs->cset.topscan = (topscan - 1) & 0x3ff;
601 		return false;
602 	}
603 
604 	count = (b - t - lines) * vc->vc_cols;
605 	if (dir == SM_UP) {
606 		x = 0;
607 		y = t;
608 		s = (unsigned short *) (vc->vc_origin +
609 					vc->vc_size_row * (t + lines));
610 		d = (unsigned short *) (vc->vc_origin +
611 					vc->vc_size_row * t);
612 		while (count--) {
613 			chattr = scr_readw(s++);
614 			if (chattr != scr_readw(d)) {
615 				newport_putc(vc, chattr, y, x);
616 				scr_writew(chattr, d);
617 			}
618 			d++;
619 			if (++x == vc->vc_cols) {
620 				x = 0;
621 				y++;
622 			}
623 		}
624 		d = (unsigned short *) (vc->vc_origin +
625 					vc->vc_size_row * (b - lines));
626 		x = 0;
627 		y = b - lines;
628 		for (count = 0; count < (lines * vc->vc_cols); count++) {
629 			if (scr_readw(d) != vc->vc_video_erase_char) {
630 				newport_putc(vc, vc->vc_video_erase_char,
631 					     y, x);
632 				scr_writew(vc->vc_video_erase_char, d);
633 			}
634 			d++;
635 			if (++x == vc->vc_cols) {
636 				x = 0;
637 				y++;
638 			}
639 		}
640 	} else {
641 		x = vc->vc_cols - 1;
642 		y = b - 1;
643 		s = (unsigned short *) (vc->vc_origin +
644 					vc->vc_size_row * (b - lines) - 2);
645 		d = (unsigned short *) (vc->vc_origin +
646 					vc->vc_size_row * b - 2);
647 		while (count--) {
648 			chattr = scr_readw(s--);
649 			if (chattr != scr_readw(d)) {
650 				newport_putc(vc, chattr, y, x);
651 				scr_writew(chattr, d);
652 			}
653 			d--;
654 			if (x-- == 0) {
655 				x = vc->vc_cols - 1;
656 				y--;
657 			}
658 		}
659 		d = (unsigned short *) (vc->vc_origin +
660 					vc->vc_size_row * t);
661 		x = 0;
662 		y = t;
663 		for (count = 0; count < (lines * vc->vc_cols); count++) {
664 			if (scr_readw(d) != vc->vc_video_erase_char) {
665 				newport_putc(vc, vc->vc_video_erase_char,
666 					     y, x);
667 				scr_writew(vc->vc_video_erase_char, d);
668 			}
669 			d++;
670 			if (++x == vc->vc_cols) {
671 				x = 0;
672 				y++;
673 			}
674 		}
675 	}
676 	return true;
677 }
678 
679 static int newport_set_origin(struct vc_data *vc)
680 {
681 	return 0;
682 }
683 
684 static void newport_save_screen(struct vc_data *vc) { }
685 
686 const struct consw newport_con = {
687 	.owner		  = THIS_MODULE,
688 	.con_startup	  = newport_startup,
689 	.con_init	  = newport_init,
690 	.con_deinit	  = newport_deinit,
691 	.con_clear	  = newport_clear,
692 	.con_putc	  = newport_putc,
693 	.con_putcs	  = newport_putcs,
694 	.con_cursor	  = newport_cursor,
695 	.con_scroll	  = newport_scroll,
696 	.con_switch	  = newport_switch,
697 	.con_blank	  = newport_blank,
698 	.con_font_set	  = newport_font_set,
699 	.con_font_default = newport_font_default,
700 	.con_set_origin	  = newport_set_origin,
701 	.con_save_screen  = newport_save_screen
702 };
703 
704 static int newport_probe(struct gio_device *dev,
705 			 const struct gio_device_id *id)
706 {
707 	int err;
708 
709 	if (!dev->resource.start)
710 		return -EINVAL;
711 
712 	if (npregs)
713 		return -EBUSY; /* we only support one Newport as console */
714 
715 	newport_addr = dev->resource.start + 0xF0000;
716 	if (!request_mem_region(newport_addr, NEWPORT_LEN, "Newport"))
717 		return -ENODEV;
718 
719 	npregs = (struct newport_regs *)/* ioremap cannot fail */
720 		ioremap(newport_addr, sizeof(struct newport_regs));
721 	console_lock();
722 	err = do_take_over_console(&newport_con, 0, MAX_NR_CONSOLES - 1, 1);
723 	console_unlock();
724 
725 	if (err) {
726 		iounmap((void *)npregs);
727 		release_mem_region(newport_addr, NEWPORT_LEN);
728 	}
729 	return err;
730 }
731 
732 static void newport_remove(struct gio_device *dev)
733 {
734 	give_up_console(&newport_con);
735 	iounmap((void *)npregs);
736 	release_mem_region(newport_addr, NEWPORT_LEN);
737 }
738 
739 static struct gio_device_id newport_ids[] = {
740 	{ .id = 0x7e },
741 	{ .id = 0xff }
742 };
743 
744 MODULE_ALIAS("gio:7e");
745 
746 static struct gio_driver newport_driver = {
747 	.name = "newport",
748 	.id_table = newport_ids,
749 	.probe = newport_probe,
750 	.remove = newport_remove,
751 };
752 
753 int __init newport_console_init(void)
754 {
755 	return gio_register_driver(&newport_driver);
756 }
757 
758 void __exit newport_console_exit(void)
759 {
760 	gio_unregister_driver(&newport_driver);
761 }
762 
763 module_init(newport_console_init);
764 module_exit(newport_console_exit);
765 
766 MODULE_LICENSE("GPL");
767