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