Home | History | Annotate | Line # | Download | only in mga
mga_warp.c revision 1.1.1.1.38.1
      1 /*	$NetBSD: mga_warp.c,v 1.1.1.1.38.1 2019/06/10 22:08:06 christos Exp $	*/
      2 
      3 /* mga_warp.c -- Matrox G200/G400 WARP engine management -*- linux-c -*-
      4  * Created: Thu Jan 11 21:29:32 2001 by gareth (at) valinux.com
      5  *
      6  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
      7  * All Rights Reserved.
      8  *
      9  * Permission is hereby granted, free of charge, to any person obtaining a
     10  * copy of this software and associated documentation files (the "Software"),
     11  * to deal in the Software without restriction, including without limitation
     12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     13  * and/or sell copies of the Software, and to permit persons to whom the
     14  * Software is furnished to do so, subject to the following conditions:
     15  *
     16  * The above copyright notice and this permission notice (including the next
     17  * paragraph) shall be included in all copies or substantial portions of the
     18  * Software.
     19  *
     20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     23  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     26  * OTHER DEALINGS IN THE SOFTWARE.
     27  *
     28  * Authors:
     29  *    Gareth Hughes <gareth (at) valinux.com>
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: mga_warp.c,v 1.1.1.1.38.1 2019/06/10 22:08:06 christos Exp $");
     34 
     35 #include <linux/firmware.h>
     36 #include <linux/ihex.h>
     37 #include <linux/platform_device.h>
     38 #include <linux/module.h>
     39 
     40 #include <drm/drmP.h>
     41 #include <drm/mga_drm.h>
     42 #include "mga_drv.h"
     43 
     44 #define FIRMWARE_G200 "matrox/g200_warp.fw"
     45 #define FIRMWARE_G400 "matrox/g400_warp.fw"
     46 
     47 MODULE_FIRMWARE(FIRMWARE_G200);
     48 MODULE_FIRMWARE(FIRMWARE_G400);
     49 
     50 #define MGA_WARP_CODE_ALIGN		256	/* in bytes */
     51 
     52 #define WARP_UCODE_SIZE(size)		ALIGN(size, MGA_WARP_CODE_ALIGN)
     53 
     54 int mga_warp_install_microcode(drm_mga_private_t *dev_priv)
     55 {
     56 	unsigned char *vcbase = dev_priv->warp->handle;
     57 	unsigned long pcbase = dev_priv->warp->offset;
     58 	const char *firmware_name;
     59 	struct platform_device *pdev;
     60 	const struct firmware *fw = NULL;
     61 	const struct ihex_binrec *rec;
     62 	unsigned int size;
     63 	int n_pipes, where;
     64 	int rc = 0;
     65 
     66 	switch (dev_priv->chipset) {
     67 	case MGA_CARD_TYPE_G400:
     68 	case MGA_CARD_TYPE_G550:
     69 		firmware_name = FIRMWARE_G400;
     70 		n_pipes = MGA_MAX_G400_PIPES;
     71 		break;
     72 	case MGA_CARD_TYPE_G200:
     73 		firmware_name = FIRMWARE_G200;
     74 		n_pipes = MGA_MAX_G200_PIPES;
     75 		break;
     76 	default:
     77 		return -EINVAL;
     78 	}
     79 
     80 	pdev = platform_device_register_simple("mga_warp", 0, NULL, 0);
     81 	if (IS_ERR(pdev)) {
     82 		DRM_ERROR("mga: Failed to register microcode\n");
     83 		return PTR_ERR(pdev);
     84 	}
     85 	rc = request_ihex_firmware(&fw, firmware_name, &pdev->dev);
     86 	platform_device_unregister(pdev);
     87 	if (rc) {
     88 		DRM_ERROR("mga: Failed to load microcode \"%s\"\n",
     89 			  firmware_name);
     90 		return rc;
     91 	}
     92 
     93 	size = 0;
     94 	where = 0;
     95 	for (rec = (const struct ihex_binrec *)fw->data;
     96 	     rec;
     97 	     rec = ihex_next_binrec(rec)) {
     98 		size += WARP_UCODE_SIZE(be16_to_cpu(rec->len));
     99 		where++;
    100 	}
    101 
    102 	if (where != n_pipes) {
    103 		DRM_ERROR("mga: Invalid microcode \"%s\"\n", firmware_name);
    104 		rc = -EINVAL;
    105 		goto out;
    106 	}
    107 	size = PAGE_ALIGN(size);
    108 	DRM_DEBUG("MGA ucode size = %d bytes\n", size);
    109 	if (size > dev_priv->warp->size) {
    110 		DRM_ERROR("microcode too large! (%u > %lu)\n",
    111 			  size, dev_priv->warp->size);
    112 		rc = -ENOMEM;
    113 		goto out;
    114 	}
    115 
    116 	memset(dev_priv->warp_pipe_phys, 0, sizeof(dev_priv->warp_pipe_phys));
    117 
    118 	where = 0;
    119 	for (rec = (const struct ihex_binrec *)fw->data;
    120 	     rec;
    121 	     rec = ihex_next_binrec(rec)) {
    122 		unsigned int src_size, dst_size;
    123 
    124 		DRM_DEBUG(" pcbase = 0x%08lx  vcbase = %p\n", pcbase, vcbase);
    125 		dev_priv->warp_pipe_phys[where] = pcbase;
    126 		src_size = be16_to_cpu(rec->len);
    127 		dst_size = WARP_UCODE_SIZE(src_size);
    128 		memcpy(vcbase, rec->data, src_size);
    129 		pcbase += dst_size;
    130 		vcbase += dst_size;
    131 		where++;
    132 	}
    133 
    134 out:
    135 	release_firmware(fw);
    136 	return rc;
    137 }
    138 
    139 #define WMISC_EXPECTED		(MGA_WUCODECACHE_ENABLE | MGA_WMASTER_ENABLE)
    140 
    141 int mga_warp_init(drm_mga_private_t *dev_priv)
    142 {
    143 	u32 wmisc;
    144 
    145 	/* FIXME: Get rid of these damned magic numbers...
    146 	 */
    147 	switch (dev_priv->chipset) {
    148 	case MGA_CARD_TYPE_G400:
    149 	case MGA_CARD_TYPE_G550:
    150 		MGA_WRITE(MGA_WIADDR2, MGA_WMODE_SUSPEND);
    151 		MGA_WRITE(MGA_WGETMSB, 0x00000E00);
    152 		MGA_WRITE(MGA_WVRTXSZ, 0x00001807);
    153 		MGA_WRITE(MGA_WACCEPTSEQ, 0x18000000);
    154 		break;
    155 	case MGA_CARD_TYPE_G200:
    156 		MGA_WRITE(MGA_WIADDR, MGA_WMODE_SUSPEND);
    157 		MGA_WRITE(MGA_WGETMSB, 0x1606);
    158 		MGA_WRITE(MGA_WVRTXSZ, 7);
    159 		break;
    160 	default:
    161 		return -EINVAL;
    162 	}
    163 
    164 	MGA_WRITE(MGA_WMISC, (MGA_WUCODECACHE_ENABLE |
    165 			      MGA_WMASTER_ENABLE | MGA_WCACHEFLUSH_ENABLE));
    166 	wmisc = MGA_READ(MGA_WMISC);
    167 	if (wmisc != WMISC_EXPECTED) {
    168 		DRM_ERROR("WARP engine config failed! 0x%x != 0x%x\n",
    169 			  wmisc, WMISC_EXPECTED);
    170 		return -EINVAL;
    171 	}
    172 
    173 	return 0;
    174 }
    175