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