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