1 /*
2  *  linux/drivers/video/sti/sticore.c -
3  *	core code for console driver using HP's STI firmware
4  *
5  *	Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
6  *	Portions Copyright (C) 2001-2002 Helge Deller <deller@gmx.de>
7  *	Portions Copyright (C) 2001-2002 Thomas Bogendoerfer <tsbogend@alpha.franken.de>
8  *
9  * TODO:
10  * - call STI in virtual mode rather than in real mode
11  * - screen blanking with state_mgmt() in text mode STI ?
12  * - try to make it work on m68k hp workstations ;)
13  * - clean up the cache flushing functions
14  *
15  */
16 
17 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/pci.h>
24 #include <video/font.h>
25 
26 #include <asm/pgalloc.h>
27 #include <asm/hardware.h>
28 
29 #include "sticore.h"
30 
31 #define STI_DRIVERVERSION "0.9"
32 
33 struct sti_struct *default_sti;
34 
35 static int num_sti_roms;			  /* # of STI ROMS found */
36 static struct sti_struct *sti_roms[MAX_STI_ROMS]; /* ptr to each sti_struct */
37 
38 
39 /* The colour indices used by STI are
40  *   0 - Black
41  *   1 - White
42  *   2 - Red
43  *   3 - Yellow/Brown
44  *   4 - Green
45  *   5 - Cyan
46  *   6 - Blue
47  *   7 - Magenta
48  *
49  * So we have the same colours as VGA (basically one bit each for R, G, B),
50  * but have to translate them, anyway. */
51 
52 static const u8 col_trans[8] = {
53         0, 6, 4, 5,
54         2, 7, 3, 1
55 };
56 
57 #define c_fg(sti, c) col_trans[((c>> 8) & 7)]
58 #define c_bg(sti, c) col_trans[((c>>11) & 7)]
59 #define c_index(sti, c) ((c) & 0xff)
60 
61 static const struct sti_init_flags default_init_flags = {
62 	wait:	STI_WAIT,
63 	reset:	1,
64 	text:	1,
65 	nontext:1,
66 	no_chg_bet: 1,
67 	no_chg_bei: 1,
68 	init_cmap_tx: 1,
69 };
70 
71 int
sti_init_graph(struct sti_struct * sti)72 sti_init_graph(struct sti_struct *sti)
73 {
74 	struct sti_init_inptr_ext inptr_ext = { 0, };
75 	struct sti_init_inptr inptr = {
76 		3,	/* # of text planes (3 is maximum for STI) */
77 		STI_PTR(&inptr_ext)
78 	};
79 	struct sti_init_outptr outptr = { 0, };
80 	unsigned long flags;
81 	int ret;
82 
83 	spin_lock_irqsave(&sti->lock, flags);
84 
85 	ret = STI_CALL(sti->init_graph, &default_init_flags, &inptr,
86 		&outptr, sti->glob_cfg);
87 
88 	spin_unlock_irqrestore(&sti->lock, flags);
89 
90 	if (ret < 0) {
91 		printk(KERN_ERR "STI init_graph failed (ret %d, errno %d)\n",ret,outptr.errno);
92 		return -1;
93 	}
94 
95 	sti->text_planes = outptr.text_planes;
96 	return 0;
97 }
98 
99 static const struct sti_conf_flags default_conf_flags = {
100 		wait:	STI_WAIT,
101 	};
102 
103 void
sti_inq_conf(struct sti_struct * sti)104 sti_inq_conf(struct sti_struct *sti)
105 {
106 	struct sti_conf_inptr inptr = { 0 };
107 	unsigned long flags;
108 	s32 ret;
109 
110 	sti->outptr.ext_ptr = STI_PTR(&sti->outptr_ext);
111 
112 	do {
113 		spin_lock_irqsave(&sti->lock, flags);
114 		ret = STI_CALL(sti->inq_conf, &default_conf_flags,
115 			&inptr, &sti->outptr, sti->glob_cfg);
116 		spin_unlock_irqrestore(&sti->lock, flags);
117 	} while (ret == 1);
118 }
119 
120 static const struct sti_font_flags default_font_flags = {
121 		wait:	STI_WAIT,
122 		non_text: 0,
123 };
124 
125 void
sti_putc(struct sti_struct * sti,int c,int y,int x)126 sti_putc(struct sti_struct *sti, int c, int y, int x)
127 {
128 	struct sti_font_inptr inptr = {
129 		STI_PTR(sti->font->raw),
130 		c_index(sti, c), c_fg(sti, c), c_bg(sti, c),
131 		x * sti->font_width, y * sti->font_height, 0
132 	};
133 	struct sti_font_outptr outptr = { 0, };
134 	s32 ret;
135 	unsigned long flags;
136 
137 	do {
138 		spin_lock_irqsave(&sti->lock, flags);
139 		ret = STI_CALL(sti->font_unpmv, &default_font_flags,
140 			&inptr, &outptr, sti->glob_cfg);
141 		spin_unlock_irqrestore(&sti->lock, flags);
142 	} while (ret == 1);
143 }
144 
145 static const struct sti_blkmv_flags clear_blkmv_flags = {
146 	wait:	STI_WAIT,
147 	color:	1,
148 	clear:	1,
149 };
150 
151 void
sti_set(struct sti_struct * sti,int src_y,int src_x,int height,int width,u8 color)152 sti_set(struct sti_struct *sti, int src_y, int src_x,
153 	int height, int width, u8 color)
154 {
155 	struct sti_blkmv_inptr inptr = {
156 		color, color,
157 		src_x, src_y ,
158 		src_x, src_y ,
159 		width, height,
160 		0
161 	};
162 	struct sti_blkmv_outptr outptr = { 0, };
163 	s32 ret;
164 	unsigned long flags;
165 
166 	do {
167 		spin_lock_irqsave(&sti->lock, flags);
168 		ret = STI_CALL(sti->block_move, &clear_blkmv_flags,
169 			&inptr, &outptr, sti->glob_cfg);
170 		spin_unlock_irqrestore(&sti->lock, flags);
171 	} while (ret == 1);
172 }
173 
174 void
sti_clear(struct sti_struct * sti,int src_y,int src_x,int height,int width,int c)175 sti_clear(struct sti_struct *sti, int src_y, int src_x,
176 	  int height, int width, int c)
177 {
178 	struct sti_blkmv_inptr inptr = {
179 		c_fg(sti, c), c_bg(sti, c),
180 		src_x * sti->font_width, src_y * sti->font_height,
181 		src_x * sti->font_width, src_y * sti->font_height,
182 		width * sti->font_width, height* sti->font_height,
183 		0
184 	};
185 	struct sti_blkmv_outptr outptr = { 0, };
186 	s32 ret;
187 	unsigned long flags;
188 
189 	do {
190 		spin_lock_irqsave(&sti->lock, flags);
191 		ret = STI_CALL(sti->block_move, &clear_blkmv_flags,
192 			&inptr, &outptr, sti->glob_cfg);
193 		spin_unlock_irqrestore(&sti->lock, flags);
194 	} while (ret == 1);
195 }
196 
197 static const struct sti_blkmv_flags default_blkmv_flags = {
198 	wait:	STI_WAIT,
199 };
200 
201 void
sti_bmove(struct sti_struct * sti,int src_y,int src_x,int dst_y,int dst_x,int height,int width)202 sti_bmove(struct sti_struct *sti, int src_y, int src_x,
203 	  int dst_y, int dst_x, int height, int width)
204 {
205 	struct sti_blkmv_inptr inptr = {
206 		0, 0,
207 		src_x * sti->font_width, src_y * sti->font_height,
208 		dst_x * sti->font_width, dst_y * sti->font_height,
209 		width * sti->font_width, height* sti->font_height,
210 		0
211 	};
212 	struct sti_blkmv_outptr outptr = { 0, };
213 	s32 ret;
214 	unsigned long flags;
215 
216 	do {
217 		spin_lock_irqsave(&sti->lock, flags);
218 		ret = STI_CALL(sti->block_move, &default_blkmv_flags,
219 			&inptr, &outptr, sti->glob_cfg);
220 		spin_unlock_irqrestore(&sti->lock, flags);
221 	} while (ret == 1);
222 }
223 
224 
225 void __init
sti_rom_copy(unsigned long base,unsigned long count,void * dest)226 sti_rom_copy(unsigned long base, unsigned long count, void *dest)
227 {
228 	unsigned long dest_len = count;
229 	unsigned long dest_start = (unsigned long) dest;
230 
231 	/* this still needs to be revisited (see arch/parisc/mm/init.c:246) ! */
232 	while (count >= 4) {
233 		count -= 4;
234 		*(u32 *)dest = __raw_readl(base);
235 		base += 4;
236 		dest += 4;
237 	}
238 	while (count) {
239 		count--;
240 		*(u8 *)dest = __raw_readb(base);
241 		base++;
242 		dest++;
243 	}
244 
245 	sti_flush(dest_start, dest_len); /* XXX */
246 }
247 
248 
249 
250 
251 static char default_sti_path[21];
252 
253 static int __init
sti_setup(char * str)254 sti_setup(char *str)
255 {
256 	if (str)
257 		strncpy (default_sti_path, str, sizeof (default_sti_path));
258 
259 	return 0;
260 }
261 
262 /*	Assuming the machine has multiple STI consoles (=graphic cards) which
263  *	all get detected by sticon, the user may define with the linux kernel
264  *	parameter sti=<x> which of them will be the initial boot-console.
265  *	<x> is a number between 0 and MAX_STI_ROMS, with 0 as the default
266  *	STI screen.
267  */
268 __setup("sti=", sti_setup);
269 
270 
271 
272 static char __initdata	*font_name[MAX_STI_ROMS] = { "VGA8x16", };
273 static int __initdata	font_index[MAX_STI_ROMS],
274 			font_height[MAX_STI_ROMS],
275 			font_width[MAX_STI_ROMS];
276 
sti_font_setup(char * str)277 static int __init sti_font_setup(char *str)
278 {
279 	char *x;
280 	int i = 0;
281 
282 	/* we accept sti_font=VGA8x16, sti_font=10x20, sti_font=10*20
283 	 * or sti_font=7 style command lines. */
284 
285 	while (i<MAX_STI_ROMS && str && *str) {
286 		if (*str>='0' && *str<='9') {
287 			if ((x = strchr(str, 'x')) || (x = strchr(str, '*'))) {
288 				font_height[i] = simple_strtoul(str, NULL, 0);
289 				font_width[i] = simple_strtoul(x+1, NULL, 0);
290 			} else {
291 				font_index[i] = simple_strtoul(str, NULL, 0);
292 			}
293 		} else {
294 			font_name[i] = str;	/* fb font name */
295 		}
296 
297 		if ((x = strchr(str, ',')))
298 			*x++ = 0;
299 		str = x;
300 
301 		i++;
302 	}
303 
304 	return 0;
305 }
306 
307 /*	The optional linux kernel parameter "sti_font" defines which font
308  *	should be used by the sticon driver to draw characters to the screen.
309  *	Possible values are:
310  *	- sti_font=<fb_fontname>:
311  *		<fb_fontname> is the name of one of the linux-kernel built-in
312  *		framebuffer font names (e.g. VGA8x16, SUN22x18).
313  *		This is only available if the fonts have been statically compiled
314  *		in with e.g. the CONFIG_FONT_8x16 or CONFIG_FONT_SUN12x22 options.
315  *	- sti_font=<number>
316  *		most STI ROMs have built-in HP specific fonts, which can be selected
317  *		by giving the desired number to the sticon driver.
318  *		NOTE: This number is machine and STI ROM dependend.
319  *	- sti_font=<height>x<width>  (e.g. sti_font=16x8)
320  *		<height> and <width> gives hints to the height and width of the
321  *		font which the user wants. The sticon driver will try to use
322  *		a font with this height and width, but if no suitable font is
323  *		found, sticon will use the default 8x8 font.
324  */
325 __setup("sti_font=", sti_font_setup);
326 
327 
328 
329 void __init
sti_dump_globcfg(struct sti_glob_cfg * glob_cfg,unsigned int sti_mem_request)330 sti_dump_globcfg(struct sti_glob_cfg *glob_cfg, unsigned int sti_mem_request)
331 {
332 	struct sti_glob_cfg_ext *cfg;
333 
334 	DPRINTK((KERN_INFO
335 		"%d text planes\n"
336 		"%4d x %4d screen resolution\n"
337 		"%4d x %4d offscreen\n"
338 		"%4d x %4d layout\n"
339 		"regions at %08x %08x %08x %08x\n"
340 		"regions at %08x %08x %08x %08x\n"
341 		"reent_lvl %d\n"
342 		"save_addr %08x\n",
343 		glob_cfg->text_planes,
344 		glob_cfg->onscreen_x, glob_cfg->onscreen_y,
345 		glob_cfg->offscreen_x, glob_cfg->offscreen_y,
346 		glob_cfg->total_x, glob_cfg->total_y,
347 		glob_cfg->region_ptrs[0], glob_cfg->region_ptrs[1],
348 		glob_cfg->region_ptrs[2], glob_cfg->region_ptrs[3],
349 		glob_cfg->region_ptrs[4], glob_cfg->region_ptrs[5],
350 		glob_cfg->region_ptrs[6], glob_cfg->region_ptrs[7],
351 		glob_cfg->reent_lvl,
352 		glob_cfg->save_addr));
353 
354 	/* dump extended cfg */
355 	cfg = PTR_STI(glob_cfg->ext_ptr);
356 	DPRINTK(( KERN_INFO
357 		"monitor %d\n"
358 		"in friendly mode: %d\n"
359 		"power consumption %d watts\n"
360 		"freq ref %d\n"
361 		"sti_mem_addr %08x (size=%d bytes)\n",
362 		cfg->curr_mon,
363 		cfg->friendly_boot,
364 		cfg->power,
365 		cfg->freq_ref,
366 		cfg->sti_mem_addr, sti_mem_request));
367 }
368 
369 void __init
sti_dump_outptr(struct sti_struct * sti)370 sti_dump_outptr(struct sti_struct *sti)
371 {
372 	DPRINTK((KERN_INFO
373 		"%d bits per pixel\n"
374 		"%d used bits\n"
375 		"%d planes\n"
376 		"attributes %08x\n",
377 		 sti->outptr.bits_per_pixel,
378 		 sti->outptr.bits_used,
379 		 sti->outptr.planes,
380 		 sti->outptr.attributes));
381 }
382 
383 int __init
sti_init_glob_cfg(struct sti_struct * sti,unsigned long rom_address,unsigned long hpa)384 sti_init_glob_cfg(struct sti_struct *sti,
385 	    unsigned long rom_address, unsigned long hpa)
386 {
387 	struct sti_glob_cfg *glob_cfg;
388 	struct sti_glob_cfg_ext *glob_cfg_ext;
389 	void *save_addr;
390 	void *sti_mem_addr;
391 	const int save_addr_size = 1024;	/* XXX */
392 	int i;
393 
394 	if (!sti->sti_mem_request)
395 		sti->sti_mem_request = 256; /* STI default */
396 
397 	glob_cfg = kmalloc(sizeof(*sti->glob_cfg), GFP_KERNEL);
398 	glob_cfg_ext = kmalloc(sizeof(*glob_cfg_ext), GFP_KERNEL);
399 	save_addr = kmalloc(save_addr_size, GFP_KERNEL);
400 	sti_mem_addr = kmalloc(sti->sti_mem_request, GFP_KERNEL);
401 
402 	if (!(glob_cfg && glob_cfg_ext && save_addr && sti_mem_addr))
403 		return -ENOMEM;
404 
405 	memset(glob_cfg, 0, sizeof(*glob_cfg));
406 	memset(glob_cfg_ext, 0, sizeof(*glob_cfg_ext));
407 	memset(save_addr, 0, save_addr_size);
408 	memset(sti_mem_addr, 0, sti->sti_mem_request);
409 
410 	glob_cfg->ext_ptr = STI_PTR(glob_cfg_ext);
411 	glob_cfg->save_addr = STI_PTR(save_addr);
412 	for (i=0; i<8; i++) {
413 		unsigned long newhpa, len;
414 
415 		if (sti->pd) {
416 			unsigned char offs = sti->rm_entry[i];
417 
418 			if (offs == 0)
419 				continue;
420 			if (offs != PCI_ROM_ADDRESS &&
421 			    (offs < PCI_BASE_ADDRESS_0 ||
422 			     offs > PCI_BASE_ADDRESS_5)) {
423 				printk (KERN_WARNING
424 					"STI pci region maping for region %d (%02x) can't be mapped\n",
425 					i,sti->rm_entry[i]);
426 				continue;
427 			}
428 			newhpa = pci_resource_start (sti->pd, (offs - PCI_BASE_ADDRESS_0) / 4);
429 		} else
430 			newhpa = (i == 0) ? rom_address : hpa;
431 
432 		sti->regions_phys[i] =
433 			REGION_OFFSET_TO_PHYS(sti->regions[i], newhpa);
434 
435 		/* remap virtually */
436 		/* FIXME: add BTLB support if btlb==1 */
437 		len = sti->regions[i].region_desc.length * 4096;
438 
439 		if (len)
440 		   glob_cfg->region_ptrs[i] = (unsigned long) (
441 			sti->regions[i].region_desc.cache ?
442 			ioremap(sti->regions_phys[i], len) :
443 			ioremap_nocache(sti->regions_phys[i], len) );
444 
445 		DPRINTK(("region #%d: phys %08lx, virt %08x, len=%lukB, "
446 			 "btlb=%d, sysonly=%d, cache=%d, last=%d\n",
447 			i, sti->regions_phys[i], glob_cfg->region_ptrs[i],
448 			len/1024,
449 			sti->regions[i].region_desc.btlb,
450 			sti->regions[i].region_desc.sys_only,
451 			sti->regions[i].region_desc.cache,
452 			sti->regions[i].region_desc.last));
453 
454 		/* last entry reached ? */
455 		if (sti->regions[i].region_desc.last)
456 			break;
457 	}
458 
459 	if (++i<8 && sti->regions[i].region)
460 		printk(KERN_WARNING "%s: *future ptr (0x%8x) not yet supported !\n",
461 				__FILE__, sti->regions[i].region);
462 
463 	glob_cfg_ext->sti_mem_addr = STI_PTR(sti_mem_addr);
464 
465 	sti->glob_cfg = glob_cfg;
466 
467 	return 0;
468 }
469 
470 #ifdef CONFIG_FB
471 struct sti_cooked_font * __init
sti_select_fbfont(struct sti_cooked_rom * cooked_rom,char * fbfont_name)472 sti_select_fbfont( struct sti_cooked_rom *cooked_rom, char *fbfont_name )
473 {
474 	struct fbcon_font_desc *fbfont;
475 	unsigned int size, bpc;
476 	void *dest;
477 	struct sti_rom_font *nf;
478 	struct sti_cooked_font *cooked_font;
479 
480 	if (!fbfont_name || !strlen(fbfont_name))
481 	    return NULL;
482 	fbfont = fbcon_find_font(fbfont_name);
483 	if (!fbfont)
484 	    fbfont = fbcon_get_default_font(1024,768);
485 	if (!fbfont)
486 	    return NULL;
487 
488 	DPRINTK((KERN_DEBUG "selected %dx%d fb-font %s\n",
489 			fbfont->width, fbfont->height, fbfont->name));
490 
491 	bpc = ((fbfont->width+7)/8) * fbfont->height;
492 	size = bpc * 256;
493 	size += sizeof(struct sti_rom_font);
494 
495 	nf = kmalloc(size, GFP_KERNEL);
496 	if (!nf)
497 	    return NULL;
498 	memset(nf, 0, size);
499 
500 	nf->first_char = 0;
501 	nf->last_char = 255;
502 	nf->width = fbfont->width;
503 	nf->height = fbfont->height;
504 	nf->font_type = STI_FONT_HPROMAN8;
505 	nf->bytes_per_char = bpc;
506 	nf->next_font = 0;
507 	nf->underline_height = 1;
508 	nf->underline_pos = fbfont->height - nf->underline_height;
509 
510 	dest = nf;
511 	dest += sizeof(struct sti_rom_font);
512 	memcpy(dest, fbfont->data, bpc*256);
513 
514 	cooked_font = kmalloc(sizeof(*cooked_font), GFP_KERNEL);
515 	if (!cooked_font) {
516 	    kfree(nf);
517 	    return NULL;
518 	}
519 
520 	cooked_font->raw = nf;
521 	cooked_font->next_font = NULL;
522 
523 	cooked_rom->font_start = cooked_font;
524 
525 	return cooked_font;
526 }
527 #else
528 struct sti_cooked_font * __init
sti_select_fbfont(struct sti_cooked_rom * cooked_rom,char * fbfont_name)529 sti_select_fbfont(struct sti_cooked_rom *cooked_rom, char *fbfont_name)
530 {
531 	return NULL;
532 }
533 #endif
534 
535 struct sti_cooked_font * __init
sti_select_font(struct sti_cooked_rom * rom,int (* search_font_fnc)(struct sti_cooked_rom *,int,int))536 sti_select_font(struct sti_cooked_rom *rom,
537 	    int (*search_font_fnc) (struct sti_cooked_rom *,int,int) )
538 {
539 	struct sti_cooked_font *font;
540 	int i;
541 	int index = num_sti_roms;
542 
543 	/* check for framebuffer-font first */
544 	if ((font = sti_select_fbfont(rom, font_name[index])))
545 		return font;
546 
547 	if (font_width[index] && font_height[index])
548 		font_index[index] = search_font_fnc(rom,
549 				font_height[index], font_width[index]);
550 
551 	for (font = rom->font_start, i = font_index[index];
552 	    font && (i > 0);
553 	    font = font->next_font, i--);
554 
555 	if (font)
556 		return font;
557 	else
558 		return rom->font_start;
559 }
560 
561 
562 static void __init
sti_dump_rom(struct sti_rom * rom)563 sti_dump_rom(struct sti_rom *rom)
564 {
565         printk(KERN_INFO "STI id %04x-%04x, conforms to spec rev. %d.%02x\n",
566 		rom->graphics_id[0],
567 		rom->graphics_id[1],
568 		rom->revno[0] >> 4,
569 		rom->revno[0] & 0x0f);
570 	DPRINTK((" supports %d monitors\n", rom->num_mons));
571 	DPRINTK((" font start %08x\n", rom->font_start));
572 	DPRINTK((" region list %08x\n", rom->region_list));
573 	DPRINTK((" init_graph %08x\n", rom->init_graph));
574 	DPRINTK((" bus support %02x\n", rom->bus_support));
575 	DPRINTK((" ext bus support %02x\n", rom->ext_bus_support));
576 	DPRINTK((" alternate code type %d\n", rom->alt_code_type));
577 }
578 
579 
580 static int __init
sti_cook_fonts(struct sti_cooked_rom * cooked_rom,struct sti_rom * raw_rom)581 sti_cook_fonts(struct sti_cooked_rom *cooked_rom,
582 			struct sti_rom *raw_rom)
583 {
584 	struct sti_rom_font *raw_font, *font_start;
585 	struct sti_cooked_font *cooked_font;
586 
587 	cooked_font = kmalloc(sizeof(*cooked_font), GFP_KERNEL);
588 	if (!cooked_font)
589 		return 0;
590 
591 	cooked_rom->font_start = cooked_font;
592 
593 	raw_font = ((void *)raw_rom) + (raw_rom->font_start);
594 
595 	font_start = raw_font;
596 	cooked_font->raw = raw_font;
597 
598 	while (raw_font->next_font) {
599 		raw_font = ((void *)font_start) + (raw_font->next_font);
600 
601 		cooked_font->next_font = kmalloc(sizeof(*cooked_font), GFP_KERNEL);
602 		if (!cooked_font->next_font)
603 			return 1;
604 
605 		cooked_font = cooked_font->next_font;
606 
607 		cooked_font->raw = raw_font;
608 	}
609 
610 	cooked_font->next_font = NULL;
611 	return 1;
612 }
613 
614 
615 static int __init
sti_search_font(struct sti_cooked_rom * rom,int height,int width)616 sti_search_font(struct sti_cooked_rom *rom, int height, int width)
617 {
618 	struct sti_cooked_font *font;
619 	int i = 0;
620 
621 	for(font = rom->font_start; font; font = font->next_font, i++) {
622 	    if((font->raw->width == width) && (font->raw->height == height))
623 			return i;
624 	}
625 	return 0;
626 }
627 
628 #define BMODE_RELOCATE(offset)        offset = (offset) / 4;
629 #define BMODE_LAST_ADDR_OFFS          0x50
630 
631 static void * __init
sti_bmode_font_raw(struct sti_cooked_font * f)632 sti_bmode_font_raw(struct sti_cooked_font *f)
633 {
634 	unsigned char *n, *p, *q;
635 	int size = f->raw->bytes_per_char*256+sizeof(struct sti_rom_font);
636 
637 	n = kmalloc (4*size, GFP_KERNEL);
638 	if (!n)
639 		return NULL;
640 	memset (n, 0, 4*size);
641 	p = n + 3;
642 	q = (unsigned char *)f->raw;
643 	while (size--) {
644 		*p = *q++;
645 		p+=4;
646 	}
647 	return n + 3;
648 }
649 
650 static void __init
sti_bmode_rom_copy(unsigned long base,unsigned long count,void * dest)651 sti_bmode_rom_copy(unsigned long base, unsigned long count, void *dest)
652 {
653 	unsigned long dest_len = count;
654 	unsigned long dest_start = (unsigned long) dest;
655 
656 	while (count) {
657 		count--;
658 		*(u8 *)dest = __raw_readl(base);
659 		base += 4;
660 		dest++;
661 	}
662 	sti_flush(dest_start, dest_len); /* XXX */
663 }
664 
665 struct sti_rom * __init
sti_get_bmode_rom(unsigned long address)666 sti_get_bmode_rom (unsigned long address)
667 {
668 	struct sti_rom *raw;
669 	u32 size;
670         struct sti_rom_font *raw_font, *font_start;
671 
672 	sti_bmode_rom_copy(address + BMODE_LAST_ADDR_OFFS, sizeof(size), &size);
673 
674         size = (size+3) / 4;
675 	raw = kmalloc(size, GFP_KERNEL);
676 	if (raw) {
677 	    sti_bmode_rom_copy(address, size, raw);
678 	    memmove (&raw->res004, &raw->type[0], 0x3c);
679     	    raw->type[3] = raw->res004;
680 
681 	    BMODE_RELOCATE (raw->region_list);
682 	    BMODE_RELOCATE (raw->font_start);
683 
684 	    BMODE_RELOCATE (raw->init_graph);
685 	    BMODE_RELOCATE (raw->state_mgmt);
686 	    BMODE_RELOCATE (raw->font_unpmv);
687 	    BMODE_RELOCATE (raw->block_move);
688 	    BMODE_RELOCATE (raw->inq_conf);
689 
690 	    raw_font = ((void *)raw) + raw->font_start;
691 	    font_start = raw_font;
692 
693 	    while (raw_font->next_font) {
694 		    BMODE_RELOCATE (raw_font->next_font);
695 		    raw_font = ((void *)font_start) + raw_font->next_font;
696 	    }
697 	}
698         return raw;
699 }
700 
701 struct sti_rom * __init
sti_get_wmode_rom(unsigned long address)702 sti_get_wmode_rom (unsigned long address)
703 {
704 	struct sti_rom *raw;
705 	unsigned long size;
706 
707 	/* read the ROM size directly from the struct in ROM */
708 	size = __raw_readl(address + offsetof(struct sti_rom,last_addr));
709 
710 	raw = kmalloc(size, GFP_KERNEL);
711 	if(raw)
712 	        sti_rom_copy(address, size, raw);
713 
714         return raw;
715 }
716 
717 int __init
sti_read_rom(int wordmode,struct sti_struct * sti,unsigned long address)718 sti_read_rom(int wordmode, struct sti_struct *sti, unsigned long address)
719 {
720 	struct sti_cooked_rom *cooked;
721 	struct sti_rom *raw = NULL;
722 
723 	cooked = kmalloc(sizeof *cooked, GFP_KERNEL);
724 	if (!cooked)
725 		goto out_err;
726 
727         if (wordmode)
728                 raw = sti_get_wmode_rom (address);
729         else
730 	        raw = sti_get_bmode_rom (address);
731 
732         if (!raw)
733 	        goto out_err;
734 
735 	if (!sti_cook_fonts(cooked, raw)) {
736 		printk(KERN_ERR "No font found for STI at %08lx\n", address);
737 		goto out_err;
738 	}
739 
740 	if (raw->region_list)
741 		memcpy(sti->regions, ((void *)raw)+raw->region_list, sizeof(sti->regions));
742 
743 	address = (unsigned long) STI_PTR(raw);
744 
745 	sti->font_unpmv = address + (raw->font_unpmv & 0x03ffffff);
746 	sti->block_move = address + (raw->block_move & 0x03ffffff);
747 	sti->init_graph = address + (raw->init_graph & 0x03ffffff);
748 	sti->inq_conf   = address + (raw->inq_conf   & 0x03ffffff);
749 
750 	sti->rom = cooked;
751 	sti->rom->raw = raw;
752 
753 	sti->font = sti_select_font(sti->rom, sti_search_font);
754 	sti->font_width = sti->font->raw->width;
755 	sti->font_height = sti->font->raw->height;
756 	if (!wordmode)
757 	        sti->font->raw = sti_bmode_font_raw(sti->font);
758 
759 	sti->sti_mem_request = raw->sti_mem_req;
760 	sti->graphics_id[0] = raw->graphics_id[0];
761 	sti->graphics_id[1] = raw->graphics_id[1];
762 
763 	sti_dump_rom(raw);
764 
765 	return 1;
766 
767 out_err:
768 	if(raw)
769 		kfree(raw);
770 	if(cooked)
771 		kfree(cooked);
772 
773 	return 0;
774 }
775 
776 static struct sti_struct * __init
sti_try_rom_generic(unsigned long address,unsigned long hpa,struct pci_dev * pd)777 sti_try_rom_generic(unsigned long address, unsigned long hpa, struct pci_dev *pd)
778 {
779 	struct sti_struct *sti;
780 	int ok;
781 	u32 sig;
782 
783 	if (num_sti_roms >= MAX_STI_ROMS) {
784 	    printk(KERN_WARNING "maximum number of STI ROMS reached !\n");
785 	    return NULL;
786 	}
787 
788 	sti = kmalloc(sizeof(*sti), GFP_KERNEL);
789 	if (!sti) {
790 	    printk(KERN_ERR "Not enough memory !\n");
791 	    return NULL;
792 	}
793 
794 	memset(sti, 0, sizeof(*sti));
795 	sti->lock = SPIN_LOCK_UNLOCKED;
796 
797 test_rom:
798 	/* if we can't read the ROM, bail out early.  Not being able
799 	 * to read the hpa is okay, for romless sti */
800 	if (pdc_add_valid(address))
801 		goto out_err;
802 
803 	sig = __raw_readl(address);
804 
805 	/* check for a PCI ROM structure */
806 	if ((le32_to_cpu(sig)==0xaa55)) {
807 		unsigned int i, rm_offset;
808 		u32 *rm;
809 		i = __raw_readl(address+0x04);
810 		if (i != 1) {
811 			/* The ROM could have multiple architecture
812 			 * dependent images (e.g. i386, parisc,...) */
813 			printk(KERN_WARNING
814 				"PCI ROM is not a STI ROM type image (0x%8x)\n", i);
815 			goto out_err;
816 		}
817 
818 		sti->pd = pd;
819 
820 		i = __raw_readl(address+0x0c);
821 		DPRINTK(("PCI ROM size (from header) = %d kB\n",
822 			le16_to_cpu(i>>16)*512/1024));
823 		rm_offset = le16_to_cpu(i & 0xffff);
824 		if (rm_offset) {
825 			/* read 16 bytes from the pci region mapper array */
826 			rm = (u32*) &sti->rm_entry;
827 			*rm++ = __raw_readl(address+rm_offset+0x00);
828 			*rm++ = __raw_readl(address+rm_offset+0x04);
829 			*rm++ = __raw_readl(address+rm_offset+0x08);
830 			*rm++ = __raw_readl(address+rm_offset+0x0c);
831 			DPRINTK(("PCI region Mapper offset = %08x: ",
832 				rm_offset));
833 			for (i=0; i<16; i++)
834 				DPRINTK(("%02x ", sti->rm_entry[i]));
835 			DPRINTK(("\n"));
836 		}
837 
838 		address += le32_to_cpu(__raw_readl(address+8));
839 		DPRINTK(("sig %04x, PCI STI ROM at %08lx\n", sig, address));
840 		goto test_rom;
841 	}
842 
843 	ok = 0;
844 
845 	if ((sig & 0xff) == 0x01) {
846 		printk(KERN_INFO "STI byte mode ROM at %08lx, hpa at %08lx\n",
847 		       address, hpa);
848 		ok = sti_read_rom(0, sti, address);
849 	}
850 
851 	if ((sig & 0xffff) == 0x0303) {
852 		printk(KERN_INFO "STI word mode ROM at %08lx, hpa at %08lx\n",
853 		       address, hpa);
854 		ok = sti_read_rom(1, sti, address);
855 	}
856 
857 	if (!ok)
858 		goto out_err;
859 
860 	if (sti_init_glob_cfg(sti, address, hpa))
861 		goto out_err; /* not enough memory */
862 
863 	/* disable STI PCI ROM. ROM and card RAM overlap and
864 	 * leaving it enabled would force HPMCs
865 	 */
866 	if (sti->pd) {
867 		unsigned long rom_base;
868 		rom_base = pci_resource_start(sti->pd, PCI_ROM_RESOURCE);
869 		pci_write_config_dword(sti->pd, PCI_ROM_ADDRESS, rom_base & ~PCI_ROM_ADDRESS_ENABLE);
870 		DPRINTK((KERN_DEBUG "STI PCI ROM disabled\n"));
871 	}
872 
873 	if (sti_init_graph(sti))
874 		goto out_err;
875 
876 	sti_inq_conf(sti);
877 	sti_dump_globcfg(sti->glob_cfg, sti->sti_mem_request);
878 	sti_dump_outptr(sti);
879 
880 	printk(KERN_INFO "STI device: %s\n", sti->outptr.dev_name );
881 
882 	sti_roms[num_sti_roms] = sti;
883 	num_sti_roms++;
884 
885 	return sti;
886 
887 out_err:
888 	kfree(sti);
889 	return NULL;
890 }
891 
sticore_check_for_default_sti(struct sti_struct * sti,char * path)892 static void __init sticore_check_for_default_sti (struct sti_struct *sti, char *path)
893 {
894 	if (strcmp (path, default_sti_path) == 0)
895 		default_sti = sti;
896 }
897 
898 /*
899  * on newer systems PDC gives the address of the ROM
900  * in the additional address field addr[1] while on
901  * older Systems the PDC stores it in page0->proc_sti
902  */
sticore_pa_init(struct parisc_device * dev)903 static int __init sticore_pa_init(struct parisc_device *dev)
904 {
905 	unsigned long rom = 0;
906 	char pa_path[21];
907 	struct sti_struct *sti = NULL;
908 
909 	if(dev->num_addrs) {
910 		rom = dev->addr[0];
911 	}
912 	if (!rom) {
913 		rom = dev->hpa;
914 		DPRINTK((KERN_DEBUG "Trying STI ROM at %08lx, hpa at %08lx\n", rom, dev->hpa));
915 		sti = sti_try_rom_generic(rom, dev->hpa, NULL);
916 		rom = PAGE0->proc_sti;
917 	}
918 	if (!sti) {
919 		DPRINTK((KERN_DEBUG "Trying STI ROM at %08lx, hpa at %08lx\n", rom, dev->hpa));
920 		sti = sti_try_rom_generic(rom, dev->hpa, NULL);
921 	}
922 	if (!sti)
923 		return 1;
924 
925 	print_pa_hwpath(dev, pa_path);
926 	sticore_check_for_default_sti (sti, pa_path);
927 	return 0;
928 }
929 
930 
sticore_pci_init(struct pci_dev * pd,const struct pci_device_id * ent)931 static int __devinit sticore_pci_init(struct pci_dev *pd,
932 		const struct pci_device_id *ent)
933 {
934 #ifdef CONFIG_PCI
935 	unsigned long fb_base, rom_base;
936 	unsigned int fb_len, rom_len;
937 	struct sti_struct *sti;
938 
939 	pci_enable_device(pd);
940 
941 	fb_base = pci_resource_start(pd, 0);
942 	fb_len = pci_resource_len(pd, 0);
943 	rom_base = pci_resource_start(pd, PCI_ROM_RESOURCE);
944 	rom_len = pci_resource_len(pd, PCI_ROM_RESOURCE);
945 	if (rom_base) {
946 		pci_write_config_dword(pd, PCI_ROM_ADDRESS, rom_base | PCI_ROM_ADDRESS_ENABLE);
947 		DPRINTK((KERN_DEBUG "STI PCI ROM enabled at 0x%08lx\n", rom_base));
948 	}
949 
950 	printk(KERN_INFO "STI PCI graphic ROM found at %08lx (%u kB), fb at %08lx (%u MB)\n",
951 		rom_base, rom_len/1024, fb_base, fb_len/1024/1024);
952 
953 	DPRINTK((KERN_DEBUG "Trying PCI STI ROM at %08lx, PCI hpa at %08lx\n",
954 		    rom_base, fb_base));
955 
956 	sti = sti_try_rom_generic(rom_base, fb_base, pd);
957 	if (sti) {
958 		char pa_path[30];
959 		print_pci_hwpath(pd, pa_path);
960 		sticore_check_for_default_sti(sti, pa_path);
961 	}
962 
963 	if (!sti) {
964 		printk(KERN_WARNING "Unable to handle STI device '%s'\n",
965 			pd->name);
966 		return -ENODEV;
967 	}
968 #endif /* CONFIG_PCI */
969 
970 	return 0;
971 }
972 
973 
sticore_pci_remove(struct pci_dev * pd)974 static void __devexit sticore_pci_remove(struct pci_dev *pd)
975 {
976 	BUG();
977 }
978 
979 
980 #define PCI_DEVICE_ID_VISUALIZE_EG	0x1005
981 #define PCI_DEVICE_ID_VISUALIZE_FX	0x1008
982 #define PCI_DEVICE_ID_VISUALIZE_FX_NEW	0x108b
983 
984 static struct pci_device_id sti_pci_tbl[] __devinitdata = {
985 	{ PCI_VENDOR_ID_HP, PCI_DEVICE_ID_VISUALIZE_EG, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
986 	{ PCI_VENDOR_ID_HP, PCI_DEVICE_ID_VISUALIZE_FX, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
987 	{ PCI_VENDOR_ID_HP, PCI_DEVICE_ID_VISUALIZE_FX_NEW, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
988 	{ 0, } /* terminate list */
989 };
990 MODULE_DEVICE_TABLE(pci, sti_pci_tbl);
991 
992 static struct pci_driver pci_sti_driver = {
993 	name:		"sti (pci)",
994 	id_table:	sti_pci_tbl,
995 	probe:		sticore_pci_init,
996 	remove:		sticore_pci_remove,
997 };
998 
999 static struct parisc_device_id sti_pa_tbl[] = {
1000 	{ HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00077 },
1001 	{ HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00085 },
1002 	{ 0, }
1003 };
1004 
1005 struct parisc_driver pa_sti_driver = {
1006 	name:		"sti (native)",
1007 	id_table:	sti_pa_tbl,
1008 	probe:		sticore_pa_init,
1009 };
1010 
sti_init_roms(void)1011 struct sti_struct * __init sti_init_roms(void)
1012 {
1013 	static int initialized;
1014 
1015 	if (initialized)
1016 		goto out;
1017 
1018 	printk(KERN_INFO "STI GSC/PCI graphics driver version %s\n",
1019 			STI_DRIVERVERSION);
1020 
1021 	/* Register drivers for native & PCI cards */
1022 	register_parisc_driver(&pa_sti_driver);
1023 	pci_module_init (&pci_sti_driver);
1024 
1025 	/* if we didn't find the given default sti, take the first one */
1026 	if (!default_sti)
1027 		default_sti = sti_roms[0];
1028 
1029 out:
1030 	/* return default STI if available */
1031 	if (num_sti_roms && default_sti && default_sti->init_graph) {
1032 		initialized = 1;
1033 		return default_sti;
1034 	}
1035 	return NULL;
1036 }
1037 
1038 /*
1039  * index = 0 gives default sti
1040  * index > 0 gives other stis in detection order
1041  */
sti_get_rom(int index)1042 struct sti_struct * __init sti_get_rom(int index)
1043 {
1044 	int i;
1045 
1046 	if (index == 0)
1047 		return default_sti;
1048 
1049 	i = -1;
1050 	while (index > 0) {
1051 		i++;
1052 		if (i > num_sti_roms)
1053 			return NULL;
1054 		if (sti_roms[i] == default_sti)
1055 			continue;
1056 		index--;
1057 	}
1058 	return sti_roms[i];
1059 }
1060