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
35static struct sti_struct *default_sti __read_mostly;
36
37/* number of STI ROMS found and their ptrs to each struct */
38static int num_sti_roms __read_mostly;
39static 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
55static 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
64static 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
74static 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
107static const struct sti_conf_flags default_conf_flags = {
108	.wait	= STI_WAIT,
109};
110
111static 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
129static const struct sti_font_flags default_font_flags = {
130	.wait		= STI_WAIT,
131	.non_text	= 0,
132};
133
134void
135sti_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
159static const struct sti_blkmv_flags clear_blkmv_flags = {
160	.wait	= STI_WAIT,
161	.color	= 1,
162	.clear	= 1,
163};
164
165void
166sti_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
193void
194sti_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
221static const struct sti_blkmv_flags default_blkmv_flags = {
222	.wait = STI_WAIT,
223};
224
225void
226sti_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
252static void sti_flush(unsigned long start, unsigned long end)
253{
254	flush_icache_range(start, end);
255}
256
257static 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
281static char default_sti_path[21] __read_mostly;
282
283#ifndef MODULE
284static int 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
303static char *font_name[MAX_STI_ROMS];
304static int font_index[MAX_STI_ROMS],
305	   font_height[MAX_STI_ROMS],
306	   font_width[MAX_STI_ROMS];
307#ifndef MODULE
308static 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
361static 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
401static 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
414static 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
492static struct sti_cooked_font *
493sti_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
547static struct sti_cooked_font *
548sti_select_fbfont(struct sti_cooked_rom *cooked_rom, const char *fbfont_name)
549{
550	return NULL;
551}
552#endif
553
554static 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
580static 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
597static 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
631static 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
647static 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 = kzalloc(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
664static 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
679static 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
714static 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
729static 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	}
802ok:
803	return 1;
804
805msg_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 */
810out_err:
811	kfree(raw);
812	kfree(cooked);
813	return 0;
814}
815
816static 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		printk(KERN_ERR "Not enough memory !\n");
832		return NULL;
833	}
834
835	spin_lock_init(&sti->lock);
836
837test_rom:
838	/* if we can't read the ROM, bail out early.  Not being able
839	 * to read the hpa is okay, for romless sti */
840	if (pdc_add_valid(address))
841		goto out_err;
842
843	sig = gsc_readl(address);
844
845	/* check for a PCI ROM structure */
846	if ((le32_to_cpu(sig)==0xaa55)) {
847		unsigned int i, rm_offset;
848		u32 *rm;
849		i = gsc_readl(address+0x04);
850		if (i != 1) {
851			/* The ROM could have multiple architecture
852			 * dependent images (e.g. i386, parisc,...) */
853			printk(KERN_WARNING
854				"PCI ROM is not a STI ROM type image (0x%8x)\n", i);
855			goto out_err;
856		}
857
858		sti->pd = pd;
859
860		i = gsc_readl(address+0x0c);
861		DPRINTK(("PCI ROM size (from header) = %d kB\n",
862			le16_to_cpu(i>>16)*512/1024));
863		rm_offset = le16_to_cpu(i & 0xffff);
864		if (rm_offset) {
865			/* read 16 bytes from the pci region mapper array */
866			rm = (u32*) &sti->rm_entry;
867			*rm++ = gsc_readl(address+rm_offset+0x00);
868			*rm++ = gsc_readl(address+rm_offset+0x04);
869			*rm++ = gsc_readl(address+rm_offset+0x08);
870			*rm++ = gsc_readl(address+rm_offset+0x0c);
871			DPRINTK(("PCI region Mapper offset = %08x: ",
872				rm_offset));
873			for (i=0; i<16; i++)
874				DPRINTK(("%02x ", sti->rm_entry[i]));
875			DPRINTK(("\n"));
876		}
877
878		address += le32_to_cpu(gsc_readl(address+8));
879		DPRINTK(("sig %04x, PCI STI ROM at %08lx\n", sig, address));
880		goto test_rom;
881	}
882
883	ok = 0;
884
885	if ((sig & 0xff) == 0x01) {
886		DPRINTK(("    byte mode ROM at %08lx, hpa at %08lx\n",
887		       address, hpa));
888		ok = sti_read_rom(0, sti, address);
889	}
890
891	if ((sig & 0xffff) == 0x0303) {
892		DPRINTK(("    word mode ROM at %08lx, hpa at %08lx\n",
893		       address, hpa));
894		ok = sti_read_rom(1, sti, address);
895	}
896
897	if (!ok)
898		goto out_err;
899
900	if (sti_init_glob_cfg(sti, address, hpa))
901		goto out_err; /* not enough memory */
902
903	/* disable STI PCI ROM. ROM and card RAM overlap and
904	 * leaving it enabled would force HPMCs
905	 */
906	if (sti->pd) {
907		unsigned long rom_base;
908		rom_base = pci_resource_start(sti->pd, PCI_ROM_RESOURCE);
909		pci_write_config_dword(sti->pd, PCI_ROM_ADDRESS, rom_base & ~PCI_ROM_ADDRESS_ENABLE);
910		DPRINTK((KERN_DEBUG "STI PCI ROM disabled\n"));
911	}
912
913	if (sti_init_graph(sti))
914		goto out_err;
915
916	sti_inq_conf(sti);
917	sti_dump_globcfg(sti->glob_cfg, sti->sti_mem_request);
918	sti_dump_outptr(sti);
919
920	pr_info("    graphics card name: %s\n",
921		sti->sti_data->inq_outptr.dev_name);
922
923	sti_roms[num_sti_roms] = sti;
924	num_sti_roms++;
925
926	return sti;
927
928out_err:
929	kfree(sti);
930	return NULL;
931}
932
933static void sticore_check_for_default_sti(struct sti_struct *sti, char *path)
934{
935	if (strcmp (path, default_sti_path) == 0)
936		default_sti = sti;
937}
938
939/*
940 * on newer systems PDC gives the address of the ROM
941 * in the additional address field addr[1] while on
942 * older Systems the PDC stores it in page0->proc_sti
943 */
944static int sticore_pa_init(struct parisc_device *dev)
945{
946	char pa_path[21];
947	struct sti_struct *sti = NULL;
948	int hpa = dev->hpa.start;
949
950	if (dev->num_addrs && dev->addr[0])
951		sti = sti_try_rom_generic(dev->addr[0], hpa, NULL);
952	if (!sti)
953		sti = sti_try_rom_generic(hpa, hpa, NULL);
954	if (!sti)
955		sti = sti_try_rom_generic(PAGE0->proc_sti, hpa, NULL);
956	if (!sti)
957		return 1;
958
959	print_pa_hwpath(dev, pa_path);
960	sticore_check_for_default_sti(sti, pa_path);
961	return 0;
962}
963
964
965static int sticore_pci_init(struct pci_dev *pd, const struct pci_device_id *ent)
966{
967#ifdef CONFIG_PCI
968	unsigned long fb_base, rom_base;
969	unsigned int fb_len, rom_len;
970	int err;
971	struct sti_struct *sti;
972
973	err = pci_enable_device(pd);
974	if (err < 0) {
975		dev_err(&pd->dev, "Cannot enable PCI device\n");
976		return err;
977	}
978
979	fb_base = pci_resource_start(pd, 0);
980	fb_len = pci_resource_len(pd, 0);
981	rom_base = pci_resource_start(pd, PCI_ROM_RESOURCE);
982	rom_len = pci_resource_len(pd, PCI_ROM_RESOURCE);
983	if (rom_base) {
984		pci_write_config_dword(pd, PCI_ROM_ADDRESS, rom_base | PCI_ROM_ADDRESS_ENABLE);
985		DPRINTK((KERN_DEBUG "STI PCI ROM enabled at 0x%08lx\n", rom_base));
986	}
987
988	printk(KERN_INFO "STI PCI graphic ROM found at %08lx (%u kB), fb at %08lx (%u MB)\n",
989		rom_base, rom_len/1024, fb_base, fb_len/1024/1024);
990
991	DPRINTK((KERN_DEBUG "Trying PCI STI ROM at %08lx, PCI hpa at %08lx\n",
992		    rom_base, fb_base));
993
994	sti = sti_try_rom_generic(rom_base, fb_base, pd);
995	if (sti) {
996		char pa_path[30];
997		print_pci_hwpath(pd, pa_path);
998		sticore_check_for_default_sti(sti, pa_path);
999	}
1000
1001	if (!sti) {
1002		printk(KERN_WARNING "Unable to handle STI device '%s'\n",
1003			pci_name(pd));
1004		return -ENODEV;
1005	}
1006#endif /* CONFIG_PCI */
1007
1008	return 0;
1009}
1010
1011
1012static void sticore_pci_remove(struct pci_dev *pd)
1013{
1014	BUG();
1015}
1016
1017
1018static struct pci_device_id sti_pci_tbl[] = {
1019	{ PCI_DEVICE(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_VISUALIZE_EG) },
1020	{ PCI_DEVICE(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_VISUALIZE_FX6) },
1021	{ PCI_DEVICE(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_VISUALIZE_FX4) },
1022	{ PCI_DEVICE(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_VISUALIZE_FX2) },
1023	{ PCI_DEVICE(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_VISUALIZE_FXE) },
1024	{ 0, } /* terminate list */
1025};
1026MODULE_DEVICE_TABLE(pci, sti_pci_tbl);
1027
1028static struct pci_driver pci_sti_driver = {
1029	.name		= "sti",
1030	.id_table	= sti_pci_tbl,
1031	.probe		= sticore_pci_init,
1032	.remove		= sticore_pci_remove,
1033};
1034
1035static struct parisc_device_id sti_pa_tbl[] = {
1036	{ HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00077 },
1037	{ HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00085 },
1038	{ 0, }
1039};
1040
1041static struct parisc_driver pa_sti_driver = {
1042	.name		= "sti",
1043	.id_table	= sti_pa_tbl,
1044	.probe		= sticore_pa_init,
1045};
1046
1047
1048/*
1049 * sti_init_roms() - detects all STI ROMs and stores them in sti_roms[]
1050 */
1051
1052static int sticore_initialized __read_mostly;
1053
1054static void sti_init_roms(void)
1055{
1056	if (sticore_initialized)
1057		return;
1058
1059	sticore_initialized = 1;
1060
1061	printk(KERN_INFO "STI GSC/PCI core graphics driver "
1062			STI_DRIVERVERSION "\n");
1063
1064	/* Register drivers for native & PCI cards */
1065	register_parisc_driver(&pa_sti_driver);
1066	WARN_ON(pci_register_driver(&pci_sti_driver));
1067
1068	/* if we didn't find the given default sti, take the first one */
1069	if (!default_sti)
1070		default_sti = sti_roms[0];
1071
1072}
1073
1074/*
1075 * index = 0 gives default sti
1076 * index > 0 gives other stis in detection order
1077 */
1078struct sti_struct * sti_get_rom(unsigned int index)
1079{
1080	if (!sticore_initialized)
1081		sti_init_roms();
1082
1083	if (index == 0)
1084		return default_sti;
1085
1086	if (index > num_sti_roms)
1087		return NULL;
1088
1089	return sti_roms[index-1];
1090}
1091EXPORT_SYMBOL(sti_get_rom);
1092
1093
1094int sti_call(const struct sti_struct *sti, unsigned long func,
1095		const void *flags, void *inptr, void *outptr,
1096		struct sti_glob_cfg *glob_cfg)
1097{
1098	unsigned long _flags = STI_PTR(flags);
1099	unsigned long _inptr = STI_PTR(inptr);
1100	unsigned long _outptr = STI_PTR(outptr);
1101	unsigned long _glob_cfg = STI_PTR(glob_cfg);
1102	int ret;
1103
1104#ifdef CONFIG_64BIT
1105	/* Check for overflow when using 32bit STI on 64bit kernel. */
1106	if (WARN_ONCE(_flags>>32 || _inptr>>32 || _outptr>>32 || _glob_cfg>>32,
1107			"Out of 32bit-range pointers!"))
1108		return -1;
1109#endif
1110
1111	ret = pdc_sti_call(func, _flags, _inptr, _outptr, _glob_cfg);
1112
1113	return ret;
1114}
1115
1116MODULE_AUTHOR("Philipp Rumpf, Helge Deller, Thomas Bogendoerfer");
1117MODULE_DESCRIPTION("Core STI driver for HP's NGLE series graphics cards in HP PARISC machines");
1118MODULE_LICENSE("GPL v2");
1119
1120