Linux-libre 4.1.48-gnu
[librecmc/linux-libre.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/platform_device.h>
33 #include <linux/module.h>
34
35 #include <drm/drmP.h>
36 #include <drm/mga_drm.h>
37 #include "mga_drv.h"
38
39 #define FIRMWARE_G200 "/*(DEBLOBBED)*/"
40 #define FIRMWARE_G400 "/*(DEBLOBBED)*/"
41
42 /*(DEBLOBBED)*/
43
44 #define MGA_WARP_CODE_ALIGN             256     /* in bytes */
45
46 #define WARP_UCODE_SIZE(size)           ALIGN(size, MGA_WARP_CODE_ALIGN)
47
48 int mga_warp_install_microcode(drm_mga_private_t *dev_priv)
49 {
50         unsigned char *vcbase = dev_priv->warp->handle;
51         unsigned long pcbase = dev_priv->warp->offset;
52         const char *firmware_name;
53         struct platform_device *pdev;
54         const struct firmware *fw = NULL;
55         const struct ihex_binrec *rec;
56         unsigned int size;
57         int n_pipes, where;
58         int rc = 0;
59
60         switch (dev_priv->chipset) {
61         case MGA_CARD_TYPE_G400:
62         case MGA_CARD_TYPE_G550:
63                 firmware_name = FIRMWARE_G400;
64                 n_pipes = MGA_MAX_G400_PIPES;
65                 break;
66         case MGA_CARD_TYPE_G200:
67                 firmware_name = FIRMWARE_G200;
68                 n_pipes = MGA_MAX_G200_PIPES;
69                 break;
70         default:
71                 return -EINVAL;
72         }
73
74         pdev = platform_device_register_simple("mga_warp", 0, NULL, 0);
75         if (IS_ERR(pdev)) {
76                 DRM_ERROR("mga: Failed to register microcode\n");
77                 return PTR_ERR(pdev);
78         }
79         rc = reject_firmware(&fw, firmware_name, &pdev->dev);
80         platform_device_unregister(pdev);
81         if (rc) {
82                 DRM_ERROR("mga: Failed to load microcode \"%s\"\n",
83                           firmware_name);
84                 return rc;
85         }
86
87         size = 0;
88         where = 0;
89         for (rec = (const struct ihex_binrec *)fw->data;
90              rec;
91              rec = ihex_next_binrec(rec)) {
92                 size += WARP_UCODE_SIZE(be16_to_cpu(rec->len));
93                 where++;
94         }
95
96         if (where != n_pipes) {
97                 DRM_ERROR("mga: Invalid microcode \"%s\"\n", firmware_name);
98                 rc = -EINVAL;
99                 goto out;
100         }
101         size = PAGE_ALIGN(size);
102         DRM_DEBUG("MGA ucode size = %d bytes\n", size);
103         if (size > dev_priv->warp->size) {
104                 DRM_ERROR("microcode too large! (%u > %lu)\n",
105                           size, dev_priv->warp->size);
106                 rc = -ENOMEM;
107                 goto out;
108         }
109
110         memset(dev_priv->warp_pipe_phys, 0, sizeof(dev_priv->warp_pipe_phys));
111
112         where = 0;
113         for (rec = (const struct ihex_binrec *)fw->data;
114              rec;
115              rec = ihex_next_binrec(rec)) {
116                 unsigned int src_size, dst_size;
117
118                 DRM_DEBUG(" pcbase = 0x%08lx  vcbase = %p\n", pcbase, vcbase);
119                 dev_priv->warp_pipe_phys[where] = pcbase;
120                 src_size = be16_to_cpu(rec->len);
121                 dst_size = WARP_UCODE_SIZE(src_size);
122                 memcpy(vcbase, rec->data, src_size);
123                 pcbase += dst_size;
124                 vcbase += dst_size;
125                 where++;
126         }
127
128 out:
129         release_firmware(fw);
130         return rc;
131 }
132
133 #define WMISC_EXPECTED          (MGA_WUCODECACHE_ENABLE | MGA_WMASTER_ENABLE)
134
135 int mga_warp_init(drm_mga_private_t *dev_priv)
136 {
137         u32 wmisc;
138
139         /* FIXME: Get rid of these damned magic numbers...
140          */
141         switch (dev_priv->chipset) {
142         case MGA_CARD_TYPE_G400:
143         case MGA_CARD_TYPE_G550:
144                 MGA_WRITE(MGA_WIADDR2, MGA_WMODE_SUSPEND);
145                 MGA_WRITE(MGA_WGETMSB, 0x00000E00);
146                 MGA_WRITE(MGA_WVRTXSZ, 0x00001807);
147                 MGA_WRITE(MGA_WACCEPTSEQ, 0x18000000);
148                 break;
149         case MGA_CARD_TYPE_G200:
150                 MGA_WRITE(MGA_WIADDR, MGA_WMODE_SUSPEND);
151                 MGA_WRITE(MGA_WGETMSB, 0x1606);
152                 MGA_WRITE(MGA_WVRTXSZ, 7);
153                 break;
154         default:
155                 return -EINVAL;
156         }
157
158         MGA_WRITE(MGA_WMISC, (MGA_WUCODECACHE_ENABLE |
159                               MGA_WMASTER_ENABLE | MGA_WCACHEFLUSH_ENABLE));
160         wmisc = MGA_READ(MGA_WMISC);
161         if (wmisc != WMISC_EXPECTED) {
162                 DRM_ERROR("WARP engine config failed! 0x%x != 0x%x\n",
163                           wmisc, WMISC_EXPECTED);
164                 return -EINVAL;
165         }
166
167         return 0;
168 }