1
2
3
4
5
6#include <linux/module.h>
7#include <linux/kernel.h>
8#include <linux/fb.h>
9#include <linux/pci.h>
10#include <linux/init.h>
11#include <linux/of_device.h>
12
13#include <asm/io.h>
14
15
16
17
18
19
20
21
22
23
24
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
51 u32 pseudo_palette[16];
52};
53
54static int 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83#define RAMDAC_VID_WH 0x00000070UL
84#define RAMDAC_VID_CFG 0x00000074UL
85#define RAMDAC_VID_32FB_0 0x00000078UL
86#define RAMDAC_VID_32FB_1 0x0000007cUL
87#define RAMDAC_VID_8FB_0 0x00000080UL
88#define RAMDAC_VID_8FB_1 0x00000084UL
89#define RAMDAC_VID_XXXFB 0x00000088UL
90#define RAMDAC_VID_YYYFB 0x0000008cUL
91#define RAMDAC_VID_ZZZFB 0x00000090UL
92
93
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);
127
128 if (info->fix.visual == FB_VISUAL_TRUECOLOR && regno < 16)
129 ((u32 *)info->pseudo_palette)[regno] = value;
130
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
143
144
145
146
147
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 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
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 e3d_pci_register(struct pci_dev *pdev,
242 const struct pci_device_id *ent)
243{
244 struct device_node *of_node;
245 const char *device_type;
246 struct fb_info *info;
247 struct e3d_info *ep;
248 unsigned int line_length;
249 int err;
250
251 of_node = pci_device_to_OF_node(pdev);
252 if (!of_node) {
253 printk(KERN_ERR "e3d: Cannot find OF node of %s\n",
254 pci_name(pdev));
255 return -ENODEV;
256 }
257
258 device_type = of_get_property(of_node, "device_type", NULL);
259 if (!device_type) {
260 printk(KERN_INFO "e3d: Ignoring secondary output device "
261 "at %s\n", pci_name(pdev));
262 return -ENODEV;
263 }
264
265 err = pci_enable_device(pdev);
266 if (err < 0) {
267 printk(KERN_ERR "e3d: Cannot enable PCI device %s\n",
268 pci_name(pdev));
269 goto err_out;
270 }
271
272 info = framebuffer_alloc(sizeof(struct e3d_info), &pdev->dev);
273 if (!info) {
274 printk(KERN_ERR "e3d: Cannot allocate fb_info\n");
275 err = -ENOMEM;
276 goto err_disable;
277 }
278
279 ep = info->par;
280 ep->info = info;
281 ep->pdev = pdev;
282 spin_lock_init(&ep->lock);
283 ep->of_node = of_node;
284
285
286
287
288
289 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0,
290 &ep->fb_base_reg);
291 ep->fb_base_reg &= PCI_BASE_ADDRESS_MEM_MASK;
292
293 ep->regs_base_phys = pci_resource_start (pdev, 1);
294 err = pci_request_region(pdev, 1, "e3d regs");
295 if (err < 0) {
296 printk("e3d: Cannot request region 1 for %s\n",
297 pci_name(pdev));
298 goto err_release_fb;
299 }
300 ep->ramdac = ioremap(ep->regs_base_phys + 0x8000, 0x1000);
301 if (!ep->ramdac) {
302 err = -ENOMEM;
303 goto err_release_pci1;
304 }
305
306 ep->fb8_0_off = readl(ep->ramdac + RAMDAC_VID_8FB_0);
307 ep->fb8_0_off -= ep->fb_base_reg;
308
309 ep->fb8_1_off = readl(ep->ramdac + RAMDAC_VID_8FB_1);
310 ep->fb8_1_off -= ep->fb_base_reg;
311
312 ep->fb8_buf_diff = ep->fb8_1_off - ep->fb8_0_off;
313
314 ep->fb_base_phys = pci_resource_start (pdev, 0);
315 ep->fb_base_phys += ep->fb8_0_off;
316
317 err = pci_request_region(pdev, 0, "e3d framebuffer");
318 if (err < 0) {
319 printk("e3d: Cannot request region 0 for %s\n",
320 pci_name(pdev));
321 goto err_unmap_ramdac;
322 }
323
324 err = e3d_get_props(ep);
325 if (err)
326 goto err_release_pci0;
327
328 line_length = (readl(ep->ramdac + RAMDAC_VID_CFG) >> 16) & 0xff;
329 line_length = 1 << line_length;
330
331 switch (ep->depth) {
332 case 8:
333 info->fix.line_length = line_length;
334 break;
335 case 16:
336 info->fix.line_length = line_length * 2;
337 break;
338 case 24:
339 info->fix.line_length = line_length * 3;
340 break;
341 case 32:
342 info->fix.line_length = line_length * 4;
343 break;
344 }
345 ep->fb_size = info->fix.line_length * ep->height;
346
347 ep->fb_base = ioremap(ep->fb_base_phys, ep->fb_size);
348 if (!ep->fb_base) {
349 err = -ENOMEM;
350 goto err_release_pci0;
351 }
352
353 err = e3d_set_fbinfo(ep);
354 if (err)
355 goto err_unmap_fb;
356
357 pci_set_drvdata(pdev, info);
358
359 printk("e3d: Found device at %s\n", pci_name(pdev));
360
361 err = register_framebuffer(info);
362 if (err < 0) {
363 printk(KERN_ERR "e3d: Could not register framebuffer %s\n",
364 pci_name(pdev));
365 goto err_free_cmap;
366 }
367
368 return 0;
369
370err_free_cmap:
371 fb_dealloc_cmap(&info->cmap);
372
373err_unmap_fb:
374 iounmap(ep->fb_base);
375
376err_release_pci0:
377 pci_release_region(pdev, 0);
378
379err_unmap_ramdac:
380 iounmap(ep->ramdac);
381
382err_release_pci1:
383 pci_release_region(pdev, 1);
384
385err_release_fb:
386 framebuffer_release(info);
387
388err_disable:
389 pci_disable_device(pdev);
390
391err_out:
392 return err;
393}
394
395static void e3d_pci_unregister(struct pci_dev *pdev)
396{
397 struct fb_info *info = pci_get_drvdata(pdev);
398 struct e3d_info *ep = info->par;
399
400 unregister_framebuffer(info);
401
402 iounmap(ep->ramdac);
403 iounmap(ep->fb_base);
404
405 pci_release_region(pdev, 0);
406 pci_release_region(pdev, 1);
407
408 fb_dealloc_cmap(&info->cmap);
409 framebuffer_release(info);
410
411 pci_disable_device(pdev);
412}
413
414static struct pci_device_id e3d_pci_table[] = {
415 { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x7a0), },
416 { PCI_DEVICE(0x1091, 0x7a0), },
417 { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x7a2), },
418 { .vendor = PCI_VENDOR_ID_3DLABS,
419 .device = PCI_ANY_ID,
420 .subvendor = PCI_VENDOR_ID_3DLABS,
421 .subdevice = 0x0108,
422 },
423 { .vendor = PCI_VENDOR_ID_3DLABS,
424 .device = PCI_ANY_ID,
425 .subvendor = PCI_VENDOR_ID_3DLABS,
426 .subdevice = 0x0140,
427 },
428 { .vendor = PCI_VENDOR_ID_3DLABS,
429 .device = PCI_ANY_ID,
430 .subvendor = PCI_VENDOR_ID_3DLABS,
431 .subdevice = 0x1024,
432 },
433 { 0, }
434};
435
436static struct pci_driver e3d_driver = {
437 .name = "e3d",
438 .id_table = e3d_pci_table,
439 .probe = e3d_pci_register,
440 .remove = e3d_pci_unregister,
441};
442
443static int __init e3d_init(void)
444{
445 if (fb_get_options("e3d", NULL))
446 return -ENODEV;
447
448 return pci_register_driver(&e3d_driver);
449}
450
451static void __exit e3d_exit(void)
452{
453 pci_unregister_driver(&e3d_driver);
454}
455
456module_init(e3d_init);
457module_exit(e3d_exit);
458
459MODULE_DESCRIPTION("framebuffer driver for Sun XVR-500 graphics");
460MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
461MODULE_VERSION("1.0");
462MODULE_LICENSE("GPL");
463