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