blob: 03a03f302cb38febae5048358706877c5dc388f0 [file] [log] [blame]
David S. Miller453e93b2007-05-06 22:45:08 -07001/* sunxvr500.c: Sun 3DLABS XVR-500 Expert3D driver for sparc64 systems
2 *
3 * Copyright (C) 2007 David S. Miller (davem@davemloft.net)
4 */
5
6#include <linux/module.h>
7#include <linux/kernel.h>
David S. Miller453e93b2007-05-06 22:45:08 -07008#include <linux/fb.h>
9#include <linux/pci.h>
10#include <linux/init.h>
Robert Reif6cd5a862008-05-08 21:37:30 -070011#include <linux/of_device.h>
David S. Miller453e93b2007-05-06 22:45:08 -070012
13#include <asm/io.h>
David S. Miller453e93b2007-05-06 22:45:08 -070014
15/* XXX This device has a 'dev-comm' property which aparently is
16 * XXX a pointer into the openfirmware's address space which is
17 * XXX a shared area the kernel driver can use to keep OBP
18 * XXX informed about the current resolution setting. The idea
19 * XXX is that the kernel can change resolutions, and as long
20 * XXX as the values in the 'dev-comm' area are accurate then
21 * XXX OBP can still render text properly to the console.
22 * XXX
23 * XXX I'm still working out the layout of this and whether there
24 * XXX are any signatures we need to look for etc.
25 */
26struct e3d_info {
27 struct fb_info *info;
28 struct pci_dev *pdev;
29
30 spinlock_t lock;
31
32 char __iomem *fb_base;
33 unsigned long fb_base_phys;
34
35 unsigned long fb8_buf_diff;
36 unsigned long regs_base_phys;
37
38 void __iomem *ramdac;
39
40 struct device_node *of_node;
41
42 unsigned int width;
43 unsigned int height;
44 unsigned int depth;
45 unsigned int fb_size;
46
47 u32 fb_base_reg;
48 u32 fb8_0_off;
49 u32 fb8_1_off;
50
Antonino Daplase7e8cc52007-06-05 13:15:26 -070051 u32 pseudo_palette[16];
David S. Miller453e93b2007-05-06 22:45:08 -070052};
53
54static int __devinit e3d_get_props(struct e3d_info *ep)
55{
56 ep->width = of_getintprop_default(ep->of_node, "width", 0);
57 ep->height = of_getintprop_default(ep->of_node, "height", 0);
58 ep->depth = of_getintprop_default(ep->of_node, "depth", 8);
59
60 if (!ep->width || !ep->height) {
61 printk(KERN_ERR "e3d: Critical properties missing for %s\n",
62 pci_name(ep->pdev));
63 return -EINVAL;
64 }
65
66 return 0;
67}
68
69/* My XVR-500 comes up, at 1280x768 and a FB base register value of
70 * 0x04000000, the following video layout register values:
71 *
72 * RAMDAC_VID_WH 0x03ff04ff
73 * RAMDAC_VID_CFG 0x1a0b0088
74 * RAMDAC_VID_32FB_0 0x04000000
75 * RAMDAC_VID_32FB_1 0x04800000
76 * RAMDAC_VID_8FB_0 0x05000000
77 * RAMDAC_VID_8FB_1 0x05200000
78 * RAMDAC_VID_XXXFB 0x05400000
79 * RAMDAC_VID_YYYFB 0x05c00000
80 * RAMDAC_VID_ZZZFB 0x05e00000
81 */
82/* Video layout registers */
83#define RAMDAC_VID_WH 0x00000070UL /* (height-1)<<16 | (width-1) */
84#define RAMDAC_VID_CFG 0x00000074UL /* 0x1a000088|(linesz_log2<<16) */
85#define RAMDAC_VID_32FB_0 0x00000078UL /* PCI base 32bpp FB buffer 0 */
86#define RAMDAC_VID_32FB_1 0x0000007cUL /* PCI base 32bpp FB buffer 1 */
87#define RAMDAC_VID_8FB_0 0x00000080UL /* PCI base 8bpp FB buffer 0 */
88#define RAMDAC_VID_8FB_1 0x00000084UL /* PCI base 8bpp FB buffer 1 */
89#define RAMDAC_VID_XXXFB 0x00000088UL /* PCI base of XXX FB */
90#define RAMDAC_VID_YYYFB 0x0000008cUL /* PCI base of YYY FB */
91#define RAMDAC_VID_ZZZFB 0x00000090UL /* PCI base of ZZZ FB */
92
93/* CLUT registers */
94#define RAMDAC_INDEX 0x000000bcUL
95#define RAMDAC_DATA 0x000000c0UL
96
97static void e3d_clut_write(struct e3d_info *ep, int index, u32 val)
98{
99 void __iomem *ramdac = ep->ramdac;
100 unsigned long flags;
101
102 spin_lock_irqsave(&ep->lock, flags);
103
104 writel(index, ramdac + RAMDAC_INDEX);
105 writel(val, ramdac + RAMDAC_DATA);
106
107 spin_unlock_irqrestore(&ep->lock, flags);
108}
109
110static int e3d_setcolreg(unsigned regno,
111 unsigned red, unsigned green, unsigned blue,
112 unsigned transp, struct fb_info *info)
113{
114 struct e3d_info *ep = info->par;
115 u32 red_8, green_8, blue_8;
116 u32 red_10, green_10, blue_10;
117 u32 value;
118
119 if (regno >= 256)
120 return 1;
121
122 red_8 = red >> 8;
123 green_8 = green >> 8;
124 blue_8 = blue >> 8;
125
126 value = (blue_8 << 24) | (green_8 << 16) | (red_8 << 8);
Antonino Daplase7e8cc52007-06-05 13:15:26 -0700127
128 if (info->fix.visual == FB_VISUAL_TRUECOLOR && regno < 16)
129 ((u32 *)info->pseudo_palette)[regno] = value;
David S. Miller453e93b2007-05-06 22:45:08 -0700130
131
132 red_10 = red >> 6;
133 green_10 = green >> 6;
134 blue_10 = blue >> 6;
135
136 value = (blue_10 << 20) | (green_10 << 10) | (red_10 << 0);
137 e3d_clut_write(ep, regno, value);
138
139 return 0;
140}
141
142/* XXX This is a bit of a hack. I can't figure out exactly how the
143 * XXX two 8bpp areas of the framebuffer work. I imagine there is
144 * XXX a WID attribute somewhere else in the framebuffer which tells
145 * XXX the ramdac which of the two 8bpp framebuffer regions to take
146 * XXX the pixel from. So, for now, render into both regions to make
147 * XXX sure the pixel shows up.
148 */
149static void e3d_imageblit(struct fb_info *info, const struct fb_image *image)
150{
151 struct e3d_info *ep = info->par;
152 unsigned long flags;
153
154 spin_lock_irqsave(&ep->lock, flags);
155 cfb_imageblit(info, image);
156 info->screen_base += ep->fb8_buf_diff;
157 cfb_imageblit(info, image);
158 info->screen_base -= ep->fb8_buf_diff;
159 spin_unlock_irqrestore(&ep->lock, flags);
160}
161
162static void e3d_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
163{
164 struct e3d_info *ep = info->par;
165 unsigned long flags;
166
167 spin_lock_irqsave(&ep->lock, flags);
168 cfb_fillrect(info, rect);
169 info->screen_base += ep->fb8_buf_diff;
170 cfb_fillrect(info, rect);
171 info->screen_base -= ep->fb8_buf_diff;
172 spin_unlock_irqrestore(&ep->lock, flags);
173}
174
175static void e3d_copyarea(struct fb_info *info, const struct fb_copyarea *area)
176{
177 struct e3d_info *ep = info->par;
178 unsigned long flags;
179
180 spin_lock_irqsave(&ep->lock, flags);
181 cfb_copyarea(info, area);
182 info->screen_base += ep->fb8_buf_diff;
183 cfb_copyarea(info, area);
184 info->screen_base -= ep->fb8_buf_diff;
185 spin_unlock_irqrestore(&ep->lock, flags);
186}
187
188static struct fb_ops e3d_ops = {
189 .owner = THIS_MODULE,
190 .fb_setcolreg = e3d_setcolreg,
191 .fb_fillrect = e3d_fillrect,
192 .fb_copyarea = e3d_copyarea,
193 .fb_imageblit = e3d_imageblit,
194};
195
196static int __devinit e3d_set_fbinfo(struct e3d_info *ep)
197{
198 struct fb_info *info = ep->info;
199 struct fb_var_screeninfo *var = &info->var;
200
201 info->flags = FBINFO_DEFAULT;
202 info->fbops = &e3d_ops;
203 info->screen_base = ep->fb_base;
204 info->screen_size = ep->fb_size;
205
206 info->pseudo_palette = ep->pseudo_palette;
207
208 /* Fill fix common fields */
209 strlcpy(info->fix.id, "e3d", sizeof(info->fix.id));
210 info->fix.smem_start = ep->fb_base_phys;
211 info->fix.smem_len = ep->fb_size;
212 info->fix.type = FB_TYPE_PACKED_PIXELS;
213 if (ep->depth == 32 || ep->depth == 24)
214 info->fix.visual = FB_VISUAL_TRUECOLOR;
215 else
216 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
217
218 var->xres = ep->width;
219 var->yres = ep->height;
220 var->xres_virtual = var->xres;
221 var->yres_virtual = var->yres;
222 var->bits_per_pixel = ep->depth;
223
224 var->red.offset = 8;
225 var->red.length = 8;
226 var->green.offset = 16;
227 var->green.length = 8;
228 var->blue.offset = 24;
229 var->blue.length = 8;
230 var->transp.offset = 0;
231 var->transp.length = 0;
232
233 if (fb_alloc_cmap(&info->cmap, 256, 0)) {
234 printk(KERN_ERR "e3d: Cannot allocate color map.\n");
235 return -ENOMEM;
236 }
237
238 return 0;
239}
240
241static int __devinit e3d_pci_register(struct pci_dev *pdev,
242 const struct pci_device_id *ent)
243{
244 struct fb_info *info;
245 struct e3d_info *ep;
246 unsigned int line_length;
247 int err;
248
249 err = pci_enable_device(pdev);
250 if (err < 0) {
251 printk(KERN_ERR "e3d: Cannot enable PCI device %s\n",
252 pci_name(pdev));
253 goto err_out;
254 }
255
256 info = framebuffer_alloc(sizeof(struct e3d_info), &pdev->dev);
257 if (!info) {
258 printk(KERN_ERR "e3d: Cannot allocate fb_info\n");
259 err = -ENOMEM;
260 goto err_disable;
261 }
262
263 ep = info->par;
264 ep->info = info;
265 ep->pdev = pdev;
266 spin_lock_init(&ep->lock);
267 ep->of_node = pci_device_to_OF_node(pdev);
268 if (!ep->of_node) {
269 printk(KERN_ERR "e3d: Cannot find OF node of %s\n",
270 pci_name(pdev));
271 err = -ENODEV;
272 goto err_release_fb;
273 }
274
275 /* Read the PCI base register of the frame buffer, which we
276 * need in order to interpret the RAMDAC_VID_*FB* values in
277 * the ramdac correctly.
278 */
279 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0,
280 &ep->fb_base_reg);
281 ep->fb_base_reg &= PCI_BASE_ADDRESS_MEM_MASK;
282
283 ep->regs_base_phys = pci_resource_start (pdev, 1);
284 err = pci_request_region(pdev, 1, "e3d regs");
285 if (err < 0) {
286 printk("e3d: Cannot request region 1 for %s\n",
287 pci_name(pdev));
288 goto err_release_fb;
289 }
290 ep->ramdac = ioremap(ep->regs_base_phys + 0x8000, 0x1000);
291 if (!ep->ramdac)
292 goto err_release_pci1;
293
294 ep->fb8_0_off = readl(ep->ramdac + RAMDAC_VID_8FB_0);
295 ep->fb8_0_off -= ep->fb_base_reg;
296
297 ep->fb8_1_off = readl(ep->ramdac + RAMDAC_VID_8FB_1);
298 ep->fb8_1_off -= ep->fb_base_reg;
299
300 ep->fb8_buf_diff = ep->fb8_1_off - ep->fb8_0_off;
301
302 ep->fb_base_phys = pci_resource_start (pdev, 0);
303 ep->fb_base_phys += ep->fb8_0_off;
304
305 err = pci_request_region(pdev, 0, "e3d framebuffer");
306 if (err < 0) {
307 printk("e3d: Cannot request region 0 for %s\n",
308 pci_name(pdev));
309 goto err_unmap_ramdac;
310 }
311
312 err = e3d_get_props(ep);
313 if (err)
314 goto err_release_pci0;
315
316 line_length = (readl(ep->ramdac + RAMDAC_VID_CFG) >> 16) & 0xff;
317 line_length = 1 << line_length;
318
319 switch (ep->depth) {
320 case 8:
321 info->fix.line_length = line_length;
322 break;
323 case 16:
324 info->fix.line_length = line_length * 2;
325 break;
326 case 24:
327 info->fix.line_length = line_length * 3;
328 break;
329 case 32:
330 info->fix.line_length = line_length * 4;
331 break;
332 }
333 ep->fb_size = info->fix.line_length * ep->height;
334
335 ep->fb_base = ioremap(ep->fb_base_phys, ep->fb_size);
336 if (!ep->fb_base)
337 goto err_release_pci0;
338
339 err = e3d_set_fbinfo(ep);
340 if (err)
341 goto err_unmap_fb;
342
343 pci_set_drvdata(pdev, info);
344
345 printk("e3d: Found device at %s\n", pci_name(pdev));
346
347 err = register_framebuffer(info);
348 if (err < 0) {
349 printk(KERN_ERR "e3d: Could not register framebuffer %s\n",
350 pci_name(pdev));
Andres Salomoncc880a72009-03-31 15:25:27 -0700351 goto err_free_cmap;
David S. Miller453e93b2007-05-06 22:45:08 -0700352 }
353
354 return 0;
355
Andres Salomoncc880a72009-03-31 15:25:27 -0700356err_free_cmap:
357 fb_dealloc_cmap(&info->cmap);
358
David S. Miller453e93b2007-05-06 22:45:08 -0700359err_unmap_fb:
360 iounmap(ep->fb_base);
361
362err_release_pci0:
363 pci_release_region(pdev, 0);
364
365err_unmap_ramdac:
366 iounmap(ep->ramdac);
367
368err_release_pci1:
369 pci_release_region(pdev, 1);
370
371err_release_fb:
372 framebuffer_release(info);
373
374err_disable:
375 pci_disable_device(pdev);
376
377err_out:
378 return err;
379}
380
381static void __devexit e3d_pci_unregister(struct pci_dev *pdev)
382{
383 struct fb_info *info = pci_get_drvdata(pdev);
384 struct e3d_info *ep = info->par;
385
386 unregister_framebuffer(info);
387
388 iounmap(ep->ramdac);
389 iounmap(ep->fb_base);
390
391 pci_release_region(pdev, 0);
392 pci_release_region(pdev, 1);
393
Andres Salomoncc880a72009-03-31 15:25:27 -0700394 fb_dealloc_cmap(&info->cmap);
David S. Miller453e93b2007-05-06 22:45:08 -0700395 framebuffer_release(info);
396
397 pci_disable_device(pdev);
398}
399
400static struct pci_device_id e3d_pci_table[] = {
401 { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x7a0), },
Ben Hutchings275143e2010-02-26 04:37:09 -0800402 { PCI_DEVICE(0x1091, 0x7a0), },
David S. Miller453e93b2007-05-06 22:45:08 -0700403 { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x7a2), },
404 { .vendor = PCI_VENDOR_ID_3DLABS,
405 .device = PCI_ANY_ID,
406 .subvendor = PCI_VENDOR_ID_3DLABS,
407 .subdevice = 0x0108,
408 },
409 { .vendor = PCI_VENDOR_ID_3DLABS,
410 .device = PCI_ANY_ID,
411 .subvendor = PCI_VENDOR_ID_3DLABS,
412 .subdevice = 0x0140,
413 },
414 { .vendor = PCI_VENDOR_ID_3DLABS,
415 .device = PCI_ANY_ID,
416 .subvendor = PCI_VENDOR_ID_3DLABS,
417 .subdevice = 0x1024,
418 },
419 { 0, }
420};
421
422static struct pci_driver e3d_driver = {
423 .name = "e3d",
424 .id_table = e3d_pci_table,
425 .probe = e3d_pci_register,
426 .remove = __devexit_p(e3d_pci_unregister),
427};
428
429static int __init e3d_init(void)
430{
431 if (fb_get_options("e3d", NULL))
432 return -ENODEV;
433
434 return pci_register_driver(&e3d_driver);
435}
436
437static void __exit e3d_exit(void)
438{
439 pci_unregister_driver(&e3d_driver);
440}
441
442module_init(e3d_init);
443module_exit(e3d_exit);
444
445MODULE_DESCRIPTION("framebuffer driver for Sun XVR-500 graphics");
446MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
447MODULE_VERSION("1.0");
448MODULE_LICENSE("GPL");