Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/drivers/video/console/sticore.c -
0004  *  core code for console driver using HP's STI firmware
0005  *
0006  *  Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
0007  *  Copyright (C) 2001-2020 Helge Deller <deller@gmx.de>
0008  *  Copyright (C) 2001-2002 Thomas Bogendoerfer <tsbogend@alpha.franken.de>
0009  * 
0010  * TODO:
0011  * - call STI in virtual mode rather than in real mode
0012  * - screen blanking with state_mgmt() in text mode STI ? 
0013  * - try to make it work on m68k hp workstations ;)
0014  * 
0015  */
0016 
0017 #define pr_fmt(fmt) "%s: " fmt, KBUILD_MODNAME
0018 
0019 #include <linux/module.h>
0020 #include <linux/types.h>
0021 #include <linux/kernel.h>
0022 #include <linux/slab.h>
0023 #include <linux/init.h>
0024 #include <linux/pci.h>
0025 #include <linux/font.h>
0026 
0027 #include <asm/hardware.h>
0028 #include <asm/page.h>
0029 #include <asm/parisc-device.h>
0030 #include <asm/pdc.h>
0031 #include <asm/cacheflush.h>
0032 #include <asm/grfioctl.h>
0033 #include <asm/fb.h>
0034 
0035 #include "../fbdev/sticore.h"
0036 
0037 #define STI_DRIVERVERSION "Version 0.9c"
0038 
0039 static struct sti_struct *default_sti __read_mostly;
0040 
0041 /* number of STI ROMS found and their ptrs to each struct */
0042 static int num_sti_roms __read_mostly;
0043 static struct sti_struct *sti_roms[MAX_STI_ROMS] __read_mostly;
0044 
0045 
0046 /* The colour indices used by STI are
0047  *   0 - Black
0048  *   1 - White
0049  *   2 - Red
0050  *   3 - Yellow/Brown
0051  *   4 - Green
0052  *   5 - Cyan
0053  *   6 - Blue
0054  *   7 - Magenta
0055  *
0056  * So we have the same colours as VGA (basically one bit each for R, G, B),
0057  * but have to translate them, anyway. */
0058 
0059 static const u8 col_trans[8] = {
0060         0, 6, 4, 5,
0061         2, 7, 3, 1
0062 };
0063 
0064 #define c_fg(sti, c) col_trans[((c>> 8) & 7)]
0065 #define c_bg(sti, c) col_trans[((c>>11) & 7)]
0066 #define c_index(sti, c) ((c) & 0xff)
0067 
0068 static const struct sti_init_flags default_init_flags = {
0069     .wait   = STI_WAIT, 
0070     .reset  = 1,
0071     .text   = 1, 
0072     .nontext = 1,
0073     .no_chg_bet = 1, 
0074     .no_chg_bei = 1, 
0075     .init_cmap_tx = 1,
0076 };
0077 
0078 static int sti_init_graph(struct sti_struct *sti)
0079 {
0080     struct sti_init_inptr *inptr = &sti->sti_data->init_inptr;
0081     struct sti_init_inptr_ext *inptr_ext = &sti->sti_data->init_inptr_ext;
0082     struct sti_init_outptr *outptr = &sti->sti_data->init_outptr;
0083     unsigned long flags;
0084     int ret, err;
0085 
0086     spin_lock_irqsave(&sti->lock, flags);
0087 
0088     memset(inptr, 0, sizeof(*inptr));
0089     inptr->text_planes = 3; /* # of text planes (max 3 for STI) */
0090     memset(inptr_ext, 0, sizeof(*inptr_ext));
0091     inptr->ext_ptr = STI_PTR(inptr_ext);
0092     outptr->errno = 0;
0093 
0094     ret = sti_call(sti, sti->init_graph, &default_init_flags, inptr,
0095         outptr, sti->glob_cfg);
0096 
0097     if (ret >= 0)
0098         sti->text_planes = outptr->text_planes;
0099     err = outptr->errno;
0100 
0101     spin_unlock_irqrestore(&sti->lock, flags);
0102 
0103     if (ret < 0) {
0104         pr_err("STI init_graph failed (ret %d, errno %d)\n", ret, err);
0105         return -1;
0106     }
0107     
0108     return 0;
0109 }
0110 
0111 static const struct sti_conf_flags default_conf_flags = {
0112     .wait   = STI_WAIT,
0113 };
0114 
0115 static void sti_inq_conf(struct sti_struct *sti)
0116 {
0117     struct sti_conf_inptr *inptr = &sti->sti_data->inq_inptr;
0118     struct sti_conf_outptr *outptr = &sti->sti_data->inq_outptr;
0119     unsigned long flags;
0120     s32 ret;
0121 
0122     outptr->ext_ptr = STI_PTR(&sti->sti_data->inq_outptr_ext);
0123     
0124     do {
0125         spin_lock_irqsave(&sti->lock, flags);
0126         memset(inptr, 0, sizeof(*inptr));
0127         ret = sti_call(sti, sti->inq_conf, &default_conf_flags,
0128             inptr, outptr, sti->glob_cfg);
0129         spin_unlock_irqrestore(&sti->lock, flags);
0130     } while (ret == 1);
0131 }
0132 
0133 static const struct sti_font_flags default_font_flags = {
0134     .wait       = STI_WAIT,
0135     .non_text   = 0,
0136 };
0137 
0138 void
0139 sti_putc(struct sti_struct *sti, int c, int y, int x,
0140      struct sti_cooked_font *font)
0141 {
0142     struct sti_font_inptr *inptr = &sti->sti_data->font_inptr;
0143     struct sti_font_inptr inptr_default = {
0144         .font_start_addr = STI_PTR(font->raw),
0145         .index      = c_index(sti, c),
0146         .fg_color   = c_fg(sti, c),
0147         .bg_color   = c_bg(sti, c),
0148         .dest_x     = x * font->width,
0149         .dest_y     = y * font->height,
0150     };
0151     struct sti_font_outptr *outptr = &sti->sti_data->font_outptr;
0152     s32 ret;
0153     unsigned long flags;
0154 
0155     do {
0156         spin_lock_irqsave(&sti->lock, flags);
0157         *inptr = inptr_default;
0158         ret = sti_call(sti, sti->font_unpmv, &default_font_flags,
0159             inptr, outptr, sti->glob_cfg);
0160         spin_unlock_irqrestore(&sti->lock, flags);
0161     } while (ret == 1);
0162 }
0163 
0164 static const struct sti_blkmv_flags clear_blkmv_flags = {
0165     .wait   = STI_WAIT, 
0166     .color  = 1, 
0167     .clear  = 1, 
0168 };
0169 
0170 void
0171 sti_set(struct sti_struct *sti, int src_y, int src_x,
0172     int height, int width, u8 color)
0173 {
0174     struct sti_blkmv_inptr *inptr = &sti->sti_data->blkmv_inptr;
0175     struct sti_blkmv_inptr inptr_default = {
0176         .fg_color   = color,
0177         .bg_color   = color,
0178         .src_x      = src_x,
0179         .src_y      = src_y,
0180         .dest_x     = src_x,
0181         .dest_y     = src_y,
0182         .width      = width,
0183         .height     = height,
0184     };
0185     struct sti_blkmv_outptr *outptr = &sti->sti_data->blkmv_outptr;
0186     s32 ret;
0187     unsigned long flags;
0188     
0189     do {
0190         spin_lock_irqsave(&sti->lock, flags);
0191         *inptr = inptr_default;
0192         ret = sti_call(sti, sti->block_move, &clear_blkmv_flags,
0193             inptr, outptr, sti->glob_cfg);
0194         spin_unlock_irqrestore(&sti->lock, flags);
0195     } while (ret == 1);
0196 }
0197 
0198 void
0199 sti_clear(struct sti_struct *sti, int src_y, int src_x,
0200       int height, int width, int c, struct sti_cooked_font *font)
0201 {
0202     struct sti_blkmv_inptr *inptr = &sti->sti_data->blkmv_inptr;
0203     struct sti_blkmv_inptr inptr_default = {
0204         .fg_color   = c_fg(sti, c),
0205         .bg_color   = c_bg(sti, c),
0206         .src_x      = src_x * font->width,
0207         .src_y      = src_y * font->height,
0208         .dest_x     = src_x * font->width,
0209         .dest_y     = src_y * font->height,
0210         .width      = width * font->width,
0211         .height     = height * font->height,
0212     };
0213     struct sti_blkmv_outptr *outptr = &sti->sti_data->blkmv_outptr;
0214     s32 ret;
0215     unsigned long flags;
0216 
0217     do {
0218         spin_lock_irqsave(&sti->lock, flags);
0219         *inptr = inptr_default;
0220         ret = sti_call(sti, sti->block_move, &clear_blkmv_flags,
0221             inptr, outptr, sti->glob_cfg);
0222         spin_unlock_irqrestore(&sti->lock, flags);
0223     } while (ret == 1);
0224 }
0225 
0226 static const struct sti_blkmv_flags default_blkmv_flags = {
0227     .wait = STI_WAIT, 
0228 };
0229 
0230 void
0231 sti_bmove(struct sti_struct *sti, int src_y, int src_x,
0232       int dst_y, int dst_x, int height, int width,
0233       struct sti_cooked_font *font)
0234 {
0235     struct sti_blkmv_inptr *inptr = &sti->sti_data->blkmv_inptr;
0236     struct sti_blkmv_inptr inptr_default = {
0237         .src_x      = src_x * font->width,
0238         .src_y      = src_y * font->height,
0239         .dest_x     = dst_x * font->width,
0240         .dest_y     = dst_y * font->height,
0241         .width      = width * font->width,
0242         .height     = height * font->height,
0243     };
0244     struct sti_blkmv_outptr *outptr = &sti->sti_data->blkmv_outptr;
0245     s32 ret;
0246     unsigned long flags;
0247 
0248     do {
0249         spin_lock_irqsave(&sti->lock, flags);
0250         *inptr = inptr_default;
0251         ret = sti_call(sti, sti->block_move, &default_blkmv_flags,
0252             inptr, outptr, sti->glob_cfg);
0253         spin_unlock_irqrestore(&sti->lock, flags);
0254     } while (ret == 1);
0255 }
0256 
0257 
0258 static void sti_flush(unsigned long start, unsigned long end)
0259 {
0260     flush_icache_range(start, end);
0261 }
0262 
0263 static void sti_rom_copy(unsigned long base, unsigned long count, void *dest)
0264 {
0265     unsigned long dest_start = (unsigned long) dest;
0266 
0267     /* this still needs to be revisited (see arch/parisc/mm/init.c:246) ! */
0268     while (count >= 4) {
0269         count -= 4;
0270         *(u32 *)dest = gsc_readl(base);
0271         base += 4;
0272         dest += 4;
0273     }
0274     while (count) {
0275         count--;
0276         *(u8 *)dest = gsc_readb(base);
0277         base++;
0278         dest++;
0279     }
0280 
0281     sti_flush(dest_start, (unsigned long)dest);
0282 }
0283 
0284 
0285 
0286 
0287 static char default_sti_path[21] __read_mostly;
0288 
0289 #ifndef MODULE
0290 static int __init sti_setup(char *str)
0291 {
0292     if (str)
0293         strscpy(default_sti_path, str, sizeof(default_sti_path));
0294     
0295     return 1;
0296 }
0297 
0298 /*  Assuming the machine has multiple STI consoles (=graphic cards) which
0299  *  all get detected by sticon, the user may define with the linux kernel
0300  *  parameter sti=<x> which of them will be the initial boot-console.
0301  *  <x> is a number between 0 and MAX_STI_ROMS, with 0 as the default 
0302  *  STI screen.
0303  */
0304 __setup("sti=", sti_setup);
0305 #endif
0306 
0307 
0308 
0309 static char *font_name;
0310 static int font_index,
0311        font_height,
0312        font_width;
0313 #ifndef MODULE
0314 static int sti_font_setup(char *str)
0315 {
0316     /*
0317      * The default font can be selected in various ways.
0318      * a) sti_font=VGA8x16, sti_font=10x20, sti_font=10*20 selects
0319      *    an built-in Linux framebuffer font.
0320      * b) sti_font=<index>, where index is (1..x) with 1 selecting
0321      *    the first HP STI ROM built-in font..
0322      */
0323 
0324     if (*str >= '0' && *str <= '9') {
0325         char *x;
0326 
0327         if ((x = strchr(str, 'x')) || (x = strchr(str, '*'))) {
0328             font_height = simple_strtoul(str, NULL, 0);
0329             font_width = simple_strtoul(x+1, NULL, 0);
0330         } else {
0331             font_index = simple_strtoul(str, NULL, 0);
0332         }
0333     } else {
0334         font_name = str;    /* fb font name */
0335     }
0336 
0337     return 1;
0338 }
0339 
0340 /*  The optional linux kernel parameter "sti_font" defines which font
0341  *  should be used by the sticon driver to draw characters to the screen.
0342  *  Possible values are:
0343  *  - sti_font=<fb_fontname>:
0344  *      <fb_fontname> is the name of one of the linux-kernel built-in 
0345  *      framebuffer font names (e.g. VGA8x16, SUN22x18). 
0346  *      This is only available if the fonts have been statically compiled 
0347  *      in with e.g. the CONFIG_FONT_8x16 or CONFIG_FONT_SUN12x22 options.
0348  *  - sti_font=<number> (<number> = 1,2,3,...)
0349  *      most STI ROMs have built-in HP specific fonts, which can be selected
0350  *      by giving the desired number to the sticon driver. 
0351  *      NOTE: This number is machine and STI ROM dependend.
0352  *  - sti_font=<height>x<width>  (e.g. sti_font=16x8)
0353  *      <height> and <width> gives hints to the height and width of the
0354  *      font which the user wants. The sticon driver will try to use
0355  *      a font with this height and width, but if no suitable font is
0356  *      found, sticon will use the default 8x8 font.
0357  */
0358 __setup("sti_font=", sti_font_setup);
0359 #endif
0360 
0361 
0362     
0363 static void sti_dump_globcfg(struct sti_glob_cfg *glob_cfg,
0364                  unsigned int sti_mem_request)
0365 {
0366     struct sti_glob_cfg_ext *cfg;
0367     
0368     pr_debug("%d text planes\n"
0369         "%4d x %4d screen resolution\n"
0370         "%4d x %4d offscreen\n"
0371         "%4d x %4d layout\n"
0372         "regions at %08x %08x %08x %08x\n"
0373         "regions at %08x %08x %08x %08x\n"
0374         "reent_lvl %d\n"
0375         "save_addr %08x\n",
0376         glob_cfg->text_planes,
0377         glob_cfg->onscreen_x, glob_cfg->onscreen_y,
0378         glob_cfg->offscreen_x, glob_cfg->offscreen_y,
0379         glob_cfg->total_x, glob_cfg->total_y,
0380         glob_cfg->region_ptrs[0], glob_cfg->region_ptrs[1],
0381         glob_cfg->region_ptrs[2], glob_cfg->region_ptrs[3],
0382         glob_cfg->region_ptrs[4], glob_cfg->region_ptrs[5],
0383         glob_cfg->region_ptrs[6], glob_cfg->region_ptrs[7],
0384         glob_cfg->reent_lvl,
0385         glob_cfg->save_addr);
0386 
0387     /* dump extended cfg */ 
0388     cfg = PTR_STI((unsigned long)glob_cfg->ext_ptr);
0389     pr_debug("monitor %d\n"
0390         "in friendly mode: %d\n"
0391         "power consumption %d watts\n"
0392         "freq ref %d\n"
0393         "sti_mem_addr %08x (size=%d bytes)\n",
0394         cfg->curr_mon,
0395         cfg->friendly_boot,
0396         cfg->power,
0397         cfg->freq_ref,
0398         cfg->sti_mem_addr, sti_mem_request);
0399 }
0400 
0401 static void sti_dump_outptr(struct sti_struct *sti)
0402 {
0403     pr_debug("%d bits per pixel\n"
0404         "%d used bits\n"
0405         "%d planes\n"
0406         "attributes %08x\n",
0407          sti->sti_data->inq_outptr.bits_per_pixel,
0408          sti->sti_data->inq_outptr.bits_used,
0409          sti->sti_data->inq_outptr.planes,
0410          sti->sti_data->inq_outptr.attributes);
0411 }
0412 
0413 static int sti_init_glob_cfg(struct sti_struct *sti, unsigned long rom_address,
0414                  unsigned long hpa)
0415 {
0416     struct sti_glob_cfg *glob_cfg;
0417     struct sti_glob_cfg_ext *glob_cfg_ext;
0418     void *save_addr;
0419     void *sti_mem_addr;
0420     int i, size;
0421 
0422     if (sti->sti_mem_request < 256)
0423         sti->sti_mem_request = 256; /* STI default */
0424 
0425     size = sizeof(struct sti_all_data) + sti->sti_mem_request - 256;
0426 
0427     sti->sti_data = kzalloc(size, STI_LOWMEM);
0428     if (!sti->sti_data)
0429         return -ENOMEM;
0430 
0431     glob_cfg    = &sti->sti_data->glob_cfg;
0432     glob_cfg_ext    = &sti->sti_data->glob_cfg_ext;
0433     save_addr   = &sti->sti_data->save_addr;
0434     sti_mem_addr    = &sti->sti_data->sti_mem_addr;
0435 
0436     glob_cfg->ext_ptr = STI_PTR(glob_cfg_ext);
0437     glob_cfg->save_addr = STI_PTR(save_addr);
0438     for (i=0; i<8; i++) {
0439         unsigned long newhpa, len;
0440            
0441         if (sti->pd) {
0442             unsigned char offs = sti->rm_entry[i];
0443                 
0444             if (offs == 0)
0445                 continue;
0446             if (offs != PCI_ROM_ADDRESS &&
0447                 (offs < PCI_BASE_ADDRESS_0 ||
0448                  offs > PCI_BASE_ADDRESS_5)) {
0449                 pr_warn("STI pci region mapping for region %d (%02x) can't be mapped\n",
0450                     i,sti->rm_entry[i]);
0451                 continue;
0452             }
0453             newhpa = pci_resource_start (sti->pd, (offs - PCI_BASE_ADDRESS_0) / 4);
0454         } else
0455             newhpa = (i == 0) ? rom_address : hpa;
0456 
0457         sti->regions_phys[i] =
0458             REGION_OFFSET_TO_PHYS(sti->regions[i], newhpa);
0459         
0460         len = sti->regions[i].region_desc.length * 4096;
0461         if (len)
0462             glob_cfg->region_ptrs[i] = sti->regions_phys[i];
0463         
0464         pr_debug("region #%d: phys %08lx, region_ptr %08x, len=%lukB, "
0465              "btlb=%d, sysonly=%d, cache=%d, last=%d\n",
0466             i, sti->regions_phys[i], glob_cfg->region_ptrs[i],
0467             len/1024,
0468             sti->regions[i].region_desc.btlb,
0469             sti->regions[i].region_desc.sys_only,
0470             sti->regions[i].region_desc.cache, 
0471             sti->regions[i].region_desc.last);
0472 
0473         /* last entry reached ? */
0474         if (sti->regions[i].region_desc.last)
0475             break;
0476     }
0477 
0478     if (++i<8 && sti->regions[i].region)
0479         pr_warn("future ptr (0x%8x) not yet supported !\n",
0480             sti->regions[i].region);
0481 
0482     glob_cfg_ext->sti_mem_addr = STI_PTR(sti_mem_addr);
0483 
0484     sti->glob_cfg = glob_cfg;
0485     
0486     return 0;
0487 }
0488 
0489 #ifdef CONFIG_FONT_SUPPORT
0490 static struct sti_cooked_font *
0491 sti_select_fbfont(struct sti_cooked_rom *cooked_rom, const char *fbfont_name)
0492 {
0493     const struct font_desc *fbfont = NULL;
0494     unsigned int size, bpc;
0495     void *dest;
0496     struct sti_rom_font *nf;
0497     struct sti_cooked_font *cooked_font;
0498     
0499     if (fbfont_name && strlen(fbfont_name))
0500         fbfont = find_font(fbfont_name);
0501     if (!fbfont)
0502         fbfont = get_default_font(1024,768, ~(u32)0, ~(u32)0);
0503     if (!fbfont)
0504         return NULL;
0505 
0506     pr_info("    using %ux%u framebuffer font %s\n",
0507             fbfont->width, fbfont->height, fbfont->name);
0508             
0509     bpc = ((fbfont->width+7)/8) * fbfont->height; 
0510     size = bpc * fbfont->charcount;
0511     size += sizeof(struct sti_rom_font);
0512 
0513     nf = kzalloc(size, STI_LOWMEM);
0514     if (!nf)
0515         return NULL;
0516 
0517     nf->first_char = 0;
0518     nf->last_char = fbfont->charcount - 1;
0519     nf->width = fbfont->width;
0520     nf->height = fbfont->height;
0521     nf->font_type = STI_FONT_HPROMAN8;
0522     nf->bytes_per_char = bpc;
0523     nf->next_font = 0;
0524     nf->underline_height = 1;
0525     nf->underline_pos = fbfont->height - nf->underline_height;
0526 
0527     dest = nf;
0528     dest += sizeof(struct sti_rom_font);
0529     memcpy(dest, fbfont->data, bpc * fbfont->charcount);
0530 
0531     cooked_font = kzalloc(sizeof(*cooked_font), GFP_KERNEL);
0532     if (!cooked_font) {
0533         kfree(nf);
0534         return NULL;
0535     }
0536     
0537     cooked_font->raw = nf;
0538     cooked_font->raw_ptr = nf;
0539     cooked_font->next_font = NULL;
0540 
0541     cooked_rom->font_start = cooked_font;
0542 
0543     return cooked_font;
0544 }
0545 #else
0546 static struct sti_cooked_font *
0547 sti_select_fbfont(struct sti_cooked_rom *cooked_rom, const char *fbfont_name)
0548 {
0549     return NULL;
0550 }
0551 #endif
0552 
0553 static void sti_dump_font(struct sti_cooked_font *font)
0554 {
0555 #ifdef STI_DUMP_FONT
0556     unsigned char *p = (unsigned char *)font->raw;
0557     int n;
0558 
0559     p += sizeof(struct sti_rom_font);
0560     pr_debug("  w %d h %d bpc %d\n", font->width, font->height,
0561                     font->raw->bytes_per_char);
0562 
0563     for (n = 0; n < 256 * font->raw->bytes_per_char; n += 16, p += 16) {
0564         pr_debug("        0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x,"
0565             " 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x,"
0566             " 0x%02x, 0x%02x, 0x%02x, 0x%02x,\n",
0567             p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8],
0568             p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
0569     }
0570 #endif
0571 }
0572 
0573 static int sti_search_font(struct sti_cooked_rom *rom, int height, int width)
0574 {
0575     struct sti_cooked_font *font;
0576     int i = 0;
0577 
0578     for (font = rom->font_start; font; font = font->next_font, i++) {
0579         if ((font->raw->width == width) &&
0580             (font->raw->height == height))
0581             return i;
0582     }
0583     return 0;
0584 }
0585 
0586 static struct sti_cooked_font *sti_select_font(struct sti_cooked_rom *rom)
0587 {
0588     struct sti_cooked_font *font;
0589     int i;
0590 
0591     /* check for framebuffer-font first */
0592     if (!font_index) {
0593         font = sti_select_fbfont(rom, font_name);
0594         if (font)
0595             return font;
0596     }
0597 
0598     if (font_width && font_height)
0599         font_index = sti_search_font(rom,
0600                 font_height, font_width);
0601 
0602     for (font = rom->font_start, i = font_index - 1;
0603         font && (i > 0);
0604         font = font->next_font, i--);
0605 
0606     if (font)
0607         return font;
0608     else
0609         return rom->font_start;
0610 }
0611 
0612 
0613 static void sti_dump_rom(struct sti_struct *sti)
0614 {
0615     struct sti_rom *rom = sti->rom->raw;
0616     struct sti_cooked_font *font_start;
0617     int nr;
0618 
0619     pr_info("  id %04x-%04x, conforms to spec rev. %d.%02x\n",
0620         rom->graphics_id[0], 
0621         rom->graphics_id[1],
0622         rom->revno[0] >> 4, 
0623         rom->revno[0] & 0x0f);
0624     pr_debug("  supports %d monitors\n", rom->num_mons);
0625     pr_debug("  font start %08x\n", rom->font_start);
0626     pr_debug("  region list %08x\n", rom->region_list);
0627     pr_debug("  init_graph %08x\n", rom->init_graph);
0628     pr_debug("  bus support %02x\n", rom->bus_support);
0629     pr_debug("  ext bus support %02x\n", rom->ext_bus_support);
0630     pr_debug("  alternate code type %d\n", rom->alt_code_type);
0631 
0632     font_start = sti->rom->font_start;
0633     nr = 0;
0634     while (font_start) {
0635         struct sti_rom_font *f = font_start->raw;
0636 
0637         pr_info("    built-in font #%d: size %dx%d, chars %d-%d, bpc %d\n", ++nr,
0638             f->width, f->height,
0639             f->first_char, f->last_char, f->bytes_per_char);
0640         font_start = font_start->next_font;
0641     }
0642 }
0643 
0644 
0645 static int sti_cook_fonts(struct sti_cooked_rom *cooked_rom,
0646               struct sti_rom *raw_rom)
0647 {
0648     struct sti_rom_font *raw_font, *font_start;
0649     struct sti_cooked_font *cooked_font;
0650     
0651     cooked_font = kzalloc(sizeof(*cooked_font), GFP_KERNEL);
0652     if (!cooked_font)
0653         return 0;
0654 
0655     cooked_rom->font_start = cooked_font;
0656 
0657     raw_font = ((void *)raw_rom) + (raw_rom->font_start);
0658 
0659     font_start = raw_font;
0660     cooked_font->raw = raw_font;
0661 
0662     while (raw_font->next_font) {
0663         raw_font = ((void *)font_start) + (raw_font->next_font);
0664 
0665         cooked_font->next_font = kzalloc(sizeof(*cooked_font), GFP_KERNEL);
0666         if (!cooked_font->next_font)
0667             return 1;
0668 
0669         cooked_font = cooked_font->next_font;
0670 
0671         cooked_font->raw = raw_font;
0672     }
0673 
0674     cooked_font->next_font = NULL;
0675     return 1;
0676 }
0677 
0678 #define BMODE_RELOCATE(offset)      offset = (offset) / 4;
0679 #define BMODE_LAST_ADDR_OFFS        0x50
0680 
0681 void sti_font_convert_bytemode(struct sti_struct *sti, struct sti_cooked_font *f)
0682 {
0683     unsigned char *n, *p, *q;
0684     int size = f->raw->bytes_per_char * (f->raw->last_char + 1) + sizeof(struct sti_rom_font);
0685     struct sti_rom_font *old_font;
0686 
0687     if (sti->wordmode)
0688         return;
0689 
0690     old_font = f->raw_ptr;
0691     n = kcalloc(4, size, STI_LOWMEM);
0692     f->raw_ptr = n;
0693     if (!n)
0694         return;
0695     p = n + 3;
0696     q = (unsigned char *) f->raw;
0697     while (size--) {
0698         *p = *q++;
0699         p += 4;
0700     }
0701     /* store new ptr to byte-mode font and delete old font */
0702     f->raw = (struct sti_rom_font *) (n + 3);
0703     kfree(old_font);
0704 }
0705 EXPORT_SYMBOL(sti_font_convert_bytemode);
0706 
0707 static void sti_bmode_rom_copy(unsigned long base, unsigned long count,
0708                    void *dest)
0709 {
0710     unsigned long dest_start = (unsigned long) dest;
0711 
0712     while (count) {
0713         count--;
0714         *(u8 *)dest = gsc_readl(base);
0715         base += 4;
0716         dest++;
0717     }
0718 
0719     sti_flush(dest_start, (unsigned long)dest);
0720 }
0721 
0722 static struct sti_rom *sti_get_bmode_rom (unsigned long address)
0723 {
0724     struct sti_rom *raw;
0725     u32 size;
0726     struct sti_rom_font *raw_font, *font_start;
0727 
0728     sti_bmode_rom_copy(address + BMODE_LAST_ADDR_OFFS, sizeof(size), &size);
0729 
0730     size = (size+3) / 4;
0731     raw = kmalloc(size, STI_LOWMEM);
0732     if (raw) {
0733         sti_bmode_rom_copy(address, size, raw);
0734         memmove (&raw->res004, &raw->type[0], 0x3c);
0735         raw->type[3] = raw->res004;
0736 
0737         BMODE_RELOCATE (raw->region_list);
0738         BMODE_RELOCATE (raw->font_start);
0739 
0740         BMODE_RELOCATE (raw->init_graph);
0741         BMODE_RELOCATE (raw->state_mgmt);
0742         BMODE_RELOCATE (raw->font_unpmv);
0743         BMODE_RELOCATE (raw->block_move);
0744         BMODE_RELOCATE (raw->inq_conf);
0745 
0746         raw_font = ((void *)raw) + raw->font_start;
0747         font_start = raw_font;
0748         
0749         while (raw_font->next_font) {
0750             BMODE_RELOCATE (raw_font->next_font);
0751             raw_font = ((void *)font_start) + raw_font->next_font;
0752         }
0753     }
0754     return raw;
0755 }
0756 
0757 static struct sti_rom *sti_get_wmode_rom(unsigned long address)
0758 {
0759     struct sti_rom *raw;
0760     unsigned long size;
0761 
0762     /* read the ROM size directly from the struct in ROM */ 
0763     size = gsc_readl(address + offsetof(struct sti_rom,last_addr));
0764 
0765     raw = kmalloc(size, STI_LOWMEM);
0766     if (raw)
0767         sti_rom_copy(address, size, raw);
0768 
0769     return raw;
0770 }
0771 
0772 static int sti_read_rom(int wordmode, struct sti_struct *sti,
0773             unsigned long address)
0774 {
0775     struct sti_cooked_rom *cooked;
0776     struct sti_rom *raw = NULL;
0777     unsigned long revno;
0778 
0779     cooked = kmalloc(sizeof *cooked, GFP_KERNEL);
0780     if (!cooked)
0781         goto out_err;
0782 
0783     if (wordmode)
0784         raw = sti_get_wmode_rom (address);
0785     else
0786         raw = sti_get_bmode_rom (address);
0787 
0788     if (!raw)
0789         goto out_err;
0790 
0791     if (!sti_cook_fonts(cooked, raw)) {
0792         pr_warn("No font found for STI at %08lx\n", address);
0793         goto out_err;
0794     }
0795 
0796     if (raw->region_list)
0797         memcpy(sti->regions, ((void *)raw)+raw->region_list, sizeof(sti->regions));
0798 
0799     address = (unsigned long) STI_PTR(raw);
0800 
0801     pr_info("STI %s ROM supports 32 %sbit firmware functions.\n",
0802         wordmode ? "word mode" : "byte mode",
0803         raw->alt_code_type == ALT_CODE_TYPE_PA_RISC_64
0804         ? "and 64 " : "");
0805 
0806     sti->font_unpmv = address + (raw->font_unpmv & 0x03ffffff);
0807     sti->block_move = address + (raw->block_move & 0x03ffffff);
0808     sti->init_graph = address + (raw->init_graph & 0x03ffffff);
0809     sti->inq_conf   = address + (raw->inq_conf   & 0x03ffffff);
0810 
0811     sti->rom = cooked;
0812     sti->rom->raw = raw;
0813     sti_dump_rom(sti);
0814 
0815     sti->wordmode = wordmode;
0816     sti->font = sti_select_font(sti->rom);
0817     sti->font->width = sti->font->raw->width;
0818     sti->font->height = sti->font->raw->height;
0819     sti_font_convert_bytemode(sti, sti->font);
0820     sti_dump_font(sti->font);
0821 
0822     sti->sti_mem_request = raw->sti_mem_req;
0823     sti->graphics_id[0] = raw->graphics_id[0];
0824     sti->graphics_id[1] = raw->graphics_id[1];
0825 
0826     /* check if the ROM routines in this card are compatible */
0827     if (wordmode || sti->graphics_id[1] != 0x09A02587)
0828         goto ok;
0829 
0830     revno = (raw->revno[0] << 8) | raw->revno[1];
0831 
0832     switch (sti->graphics_id[0]) {
0833     case S9000_ID_HCRX:
0834         /* HyperA or HyperB ? */
0835         if (revno == 0x8408 || revno == 0x840b)
0836             goto msg_not_supported;
0837         break;
0838     case CRT_ID_THUNDER:
0839         if (revno == 0x8509)
0840             goto msg_not_supported;
0841         break;
0842     case CRT_ID_THUNDER2:
0843         if (revno == 0x850c)
0844             goto msg_not_supported;
0845     }
0846 ok:
0847     return 1;
0848 
0849 msg_not_supported:
0850     pr_warn("Sorry, this GSC/STI card is not yet supported.\n");
0851     pr_warn("Please see https://parisc.wiki.kernel.org/"
0852         "index.php/Graphics_howto for more info.\n");
0853     /* fall through */
0854 out_err:
0855     kfree(raw);
0856     kfree(cooked);
0857     return 0;
0858 }
0859 
0860 static struct sti_struct *sti_try_rom_generic(unsigned long address,
0861                           unsigned long hpa,
0862                           struct pci_dev *pd)
0863 {
0864     struct sti_struct *sti;
0865     int ok;
0866     u32 sig;
0867 
0868     if (num_sti_roms >= MAX_STI_ROMS) {
0869         pr_warn("maximum number of STI ROMS reached !\n");
0870         return NULL;
0871     }
0872     
0873     sti = kzalloc(sizeof(*sti), GFP_KERNEL);
0874     if (!sti)
0875         return NULL;
0876 
0877     spin_lock_init(&sti->lock);
0878 
0879 test_rom:
0880     /* if we can't read the ROM, bail out early.  Not being able
0881      * to read the hpa is okay, for romless sti */
0882     if (pdc_add_valid(address))
0883         goto out_err;
0884 
0885     sig = gsc_readl(address);
0886 
0887     /* check for a PCI ROM structure */
0888     if ((le32_to_cpu(sig)==0xaa55)) {
0889         unsigned int i, rm_offset;
0890         u32 *rm;
0891         i = gsc_readl(address+0x04);
0892         if (i != 1) {
0893             /* The ROM could have multiple architecture 
0894              * dependent images (e.g. i386, parisc,...) */
0895             pr_warn("PCI ROM is not a STI ROM type image (0x%8x)\n", i);
0896             goto out_err;
0897         }
0898         
0899         sti->pd = pd;
0900 
0901         i = gsc_readl(address+0x0c);
0902         pr_debug("PCI ROM size (from header) = %d kB\n",
0903             le16_to_cpu(i>>16)*512/1024);
0904         rm_offset = le16_to_cpu(i & 0xffff);
0905         if (rm_offset) { 
0906             /* read 16 bytes from the pci region mapper array */
0907             rm = (u32*) &sti->rm_entry;
0908             *rm++ = gsc_readl(address+rm_offset+0x00);
0909             *rm++ = gsc_readl(address+rm_offset+0x04);
0910             *rm++ = gsc_readl(address+rm_offset+0x08);
0911             *rm++ = gsc_readl(address+rm_offset+0x0c);
0912         }
0913 
0914         address += le32_to_cpu(gsc_readl(address+8));
0915         pr_debug("sig %04x, PCI STI ROM at %08lx\n", sig, address);
0916         goto test_rom;
0917     }
0918     
0919     ok = 0;
0920     
0921     if ((sig & 0xff) == 0x01) {
0922         pr_debug("    byte mode ROM at %08lx, hpa at %08lx\n",
0923                address, hpa);
0924         ok = sti_read_rom(0, sti, address);
0925     }
0926 
0927     if ((sig & 0xffff) == 0x0303) {
0928         pr_debug("    word mode ROM at %08lx, hpa at %08lx\n",
0929                address, hpa);
0930         ok = sti_read_rom(1, sti, address);
0931     }
0932 
0933     if (!ok)
0934         goto out_err;
0935 
0936     if (sti_init_glob_cfg(sti, address, hpa))
0937         goto out_err; /* not enough memory */
0938 
0939     /* disable STI PCI ROM. ROM and card RAM overlap and
0940      * leaving it enabled would force HPMCs
0941      */
0942     if (sti->pd) {
0943         unsigned long rom_base;
0944         rom_base = pci_resource_start(sti->pd, PCI_ROM_RESOURCE);   
0945         pci_write_config_dword(sti->pd, PCI_ROM_ADDRESS, rom_base & ~PCI_ROM_ADDRESS_ENABLE);
0946         pr_debug("STI PCI ROM disabled\n");
0947     }
0948 
0949     if (sti_init_graph(sti))
0950         goto out_err;
0951 
0952     sti_inq_conf(sti);
0953     sti_dump_globcfg(sti->glob_cfg, sti->sti_mem_request);
0954     sti_dump_outptr(sti);
0955     
0956     pr_info("    graphics card name: %s\n",
0957         sti->sti_data->inq_outptr.dev_name);
0958 
0959     sti_roms[num_sti_roms] = sti;
0960     num_sti_roms++;
0961     
0962     return sti;
0963 
0964 out_err:
0965     kfree(sti);
0966     return NULL;
0967 }
0968 
0969 static void sticore_check_for_default_sti(struct sti_struct *sti, char *path)
0970 {
0971     pr_info("    located at [%s]\n", sti->pa_path);
0972     if (strcmp (path, default_sti_path) == 0)
0973         default_sti = sti;
0974 }
0975 
0976 /*
0977  * on newer systems PDC gives the address of the ROM 
0978  * in the additional address field addr[1] while on
0979  * older Systems the PDC stores it in page0->proc_sti 
0980  */
0981 static int __init sticore_pa_init(struct parisc_device *dev)
0982 {
0983     struct sti_struct *sti = NULL;
0984     int hpa = dev->hpa.start;
0985 
0986     if (dev->num_addrs && dev->addr[0])
0987         sti = sti_try_rom_generic(dev->addr[0], hpa, NULL);
0988     if (!sti)
0989         sti = sti_try_rom_generic(hpa, hpa, NULL);
0990     if (!sti)
0991         sti = sti_try_rom_generic(PAGE0->proc_sti, hpa, NULL);
0992     if (!sti)
0993         return 1;
0994 
0995     print_pa_hwpath(dev, sti->pa_path);
0996     sticore_check_for_default_sti(sti, sti->pa_path);
0997     return 0;
0998 }
0999 
1000 
1001 static int sticore_pci_init(struct pci_dev *pd, const struct pci_device_id *ent)
1002 {
1003 #ifdef CONFIG_PCI
1004     unsigned long fb_base, rom_base;
1005     unsigned int fb_len, rom_len;
1006     int err;
1007     struct sti_struct *sti;
1008     
1009     err = pci_enable_device(pd);
1010     if (err < 0) {
1011         dev_err(&pd->dev, "Cannot enable PCI device\n");
1012         return err;
1013     }
1014 
1015     fb_base = pci_resource_start(pd, 0);
1016     fb_len = pci_resource_len(pd, 0);
1017     rom_base = pci_resource_start(pd, PCI_ROM_RESOURCE);
1018     rom_len = pci_resource_len(pd, PCI_ROM_RESOURCE);
1019     if (rom_base) {
1020         pci_write_config_dword(pd, PCI_ROM_ADDRESS, rom_base | PCI_ROM_ADDRESS_ENABLE);
1021         pr_debug("STI PCI ROM enabled at 0x%08lx\n", rom_base);
1022     }
1023 
1024     pr_info("STI PCI graphic ROM found at %08lx (%u kB), fb at %08lx (%u MB)\n",
1025         rom_base, rom_len/1024, fb_base, fb_len/1024/1024);
1026 
1027     pr_debug("Trying PCI STI ROM at %08lx, PCI hpa at %08lx\n",
1028             rom_base, fb_base);
1029 
1030     sti = sti_try_rom_generic(rom_base, fb_base, pd);
1031     if (sti) {
1032         print_pci_hwpath(pd, sti->pa_path);
1033         sticore_check_for_default_sti(sti, sti->pa_path);
1034     }
1035     
1036     if (!sti) {
1037         pr_warn("Unable to handle STI device '%s'\n", pci_name(pd));
1038         return -ENODEV;
1039     }
1040 #endif /* CONFIG_PCI */
1041 
1042     return 0;
1043 }
1044 
1045 
1046 static void __exit sticore_pci_remove(struct pci_dev *pd)
1047 {
1048     BUG();
1049 }
1050 
1051 
1052 static struct pci_device_id sti_pci_tbl[] = {
1053     { PCI_DEVICE(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_VISUALIZE_EG) },
1054     { PCI_DEVICE(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_VISUALIZE_FX6) },
1055     { PCI_DEVICE(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_VISUALIZE_FX4) },
1056     { PCI_DEVICE(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_VISUALIZE_FX2) },
1057     { PCI_DEVICE(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_VISUALIZE_FXE) },
1058     { 0, } /* terminate list */
1059 };
1060 MODULE_DEVICE_TABLE(pci, sti_pci_tbl);
1061 
1062 static struct pci_driver pci_sti_driver = {
1063     .name       = "sti",
1064     .id_table   = sti_pci_tbl,
1065     .probe      = sticore_pci_init,
1066     .remove     = __exit_p(sticore_pci_remove),
1067 };
1068 
1069 static struct parisc_device_id sti_pa_tbl[] = {
1070     { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00077 },
1071     { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00085 },
1072     { 0, }
1073 };
1074 MODULE_DEVICE_TABLE(parisc, sti_pa_tbl);
1075 
1076 static struct parisc_driver pa_sti_driver __refdata = {
1077     .name       = "sti",
1078     .id_table   = sti_pa_tbl,
1079     .probe      = sticore_pa_init,
1080 };
1081 
1082 
1083 /*
1084  * sti_init_roms() - detects all STI ROMs and stores them in sti_roms[]
1085  */
1086 
1087 static int sticore_initialized __read_mostly;
1088 
1089 static void sti_init_roms(void)
1090 {
1091     if (sticore_initialized)
1092         return;
1093 
1094     sticore_initialized = 1;
1095 
1096     pr_info("STI GSC/PCI core graphics driver "
1097             STI_DRIVERVERSION "\n");
1098 
1099     /* Register drivers for native & PCI cards */
1100     register_parisc_driver(&pa_sti_driver);
1101     WARN_ON(pci_register_driver(&pci_sti_driver));
1102 
1103     /* if we didn't find the given default sti, take the first one */
1104     if (!default_sti)
1105         default_sti = sti_roms[0];
1106 
1107 }
1108 
1109 /*
1110  * index = 0 gives default sti
1111  * index > 0 gives other stis in detection order
1112  */
1113 struct sti_struct * sti_get_rom(unsigned int index)
1114 {
1115     if (!sticore_initialized)
1116         sti_init_roms();
1117 
1118     if (index == 0)
1119         return default_sti;
1120 
1121     if (index > num_sti_roms)
1122         return NULL;
1123 
1124     return sti_roms[index-1];
1125 }
1126 EXPORT_SYMBOL(sti_get_rom);
1127 
1128 
1129 int sti_call(const struct sti_struct *sti, unsigned long func,
1130         const void *flags, void *inptr, void *outptr,
1131         struct sti_glob_cfg *glob_cfg)
1132 {
1133     unsigned long _flags = STI_PTR(flags);
1134     unsigned long _inptr = STI_PTR(inptr);
1135     unsigned long _outptr = STI_PTR(outptr);
1136     unsigned long _glob_cfg = STI_PTR(glob_cfg);
1137     int ret;
1138 
1139 #ifdef CONFIG_64BIT
1140     /* Check for overflow when using 32bit STI on 64bit kernel. */
1141     if (WARN_ONCE(_flags>>32 || _inptr>>32 || _outptr>>32 || _glob_cfg>>32,
1142             "Out of 32bit-range pointers!"))
1143         return -1;
1144 #endif
1145 
1146     ret = pdc_sti_call(func, _flags, _inptr, _outptr, _glob_cfg);
1147 
1148     return ret;
1149 }
1150 
1151 #if defined(CONFIG_FB_STI)
1152 /* check if given fb_info is the primary device */
1153 int fb_is_primary_device(struct fb_info *info)
1154 {
1155     struct sti_struct *sti;
1156 
1157     sti = sti_get_rom(0);
1158 
1159     /* if no built-in graphics card found, allow any fb driver as default */
1160     if (!sti)
1161         return true;
1162 
1163     /* return true if it's the default built-in framebuffer driver */
1164     return (sti->info == info);
1165 }
1166 EXPORT_SYMBOL(fb_is_primary_device);
1167 #endif
1168 
1169 MODULE_AUTHOR("Philipp Rumpf, Helge Deller, Thomas Bogendoerfer");
1170 MODULE_DESCRIPTION("Core STI driver for HP's NGLE series graphics cards in HP PARISC machines");
1171 MODULE_LICENSE("GPL v2");
1172