xref: /linux/drivers/video/fbdev/sunxvr2500.c (revision 1504b6f97bad166b484d6f27dc99746fdca5f467)
1 /* sunxvr2500.c: Sun 3DLABS XVR-2500 et al. fb driver for sparc64 systems
2  *
3  * License: GPL
4  *
5  * Copyright (C) 2007 David S. Miller (davem@davemloft.net)
6  */
7 
8 #include <linux/aperture.h>
9 #include <linux/kernel.h>
10 #include <linux/fb.h>
11 #include <linux/pci.h>
12 #include <linux/init.h>
13 #include <linux/of_device.h>
14 
15 #include <asm/io.h>
16 
17 struct s3d_info {
18 	struct fb_info		*info;
19 	struct pci_dev		*pdev;
20 
21 	char __iomem		*fb_base;
22 	unsigned long		fb_base_phys;
23 
24 	struct device_node	*of_node;
25 
26 	unsigned int		width;
27 	unsigned int		height;
28 	unsigned int		depth;
29 	unsigned int		fb_size;
30 
31 	u32			pseudo_palette[16];
32 };
33 
34 static int s3d_get_props(struct s3d_info *sp)
35 {
36 	sp->width = of_getintprop_default(sp->of_node, "width", 0);
37 	sp->height = of_getintprop_default(sp->of_node, "height", 0);
38 	sp->depth = of_getintprop_default(sp->of_node, "depth", 8);
39 
40 	if (!sp->width || !sp->height) {
41 		printk(KERN_ERR "s3d: Critical properties missing for %s\n",
42 		       pci_name(sp->pdev));
43 		return -EINVAL;
44 	}
45 
46 	return 0;
47 }
48 
49 static int s3d_setcolreg(unsigned regno,
50 			 unsigned red, unsigned green, unsigned blue,
51 			 unsigned transp, struct fb_info *info)
52 {
53 	u32 value;
54 
55 	if (regno < 16) {
56 		red >>= 8;
57 		green >>= 8;
58 		blue >>= 8;
59 
60 		value = (blue << 24) | (green << 16) | (red << 8);
61 		((u32 *)info->pseudo_palette)[regno] = value;
62 	}
63 
64 	return 0;
65 }
66 
67 static const struct fb_ops s3d_ops = {
68 	.owner			= THIS_MODULE,
69 	.fb_setcolreg		= s3d_setcolreg,
70 	.fb_fillrect		= cfb_fillrect,
71 	.fb_copyarea		= cfb_copyarea,
72 	.fb_imageblit		= cfb_imageblit,
73 };
74 
75 static int s3d_set_fbinfo(struct s3d_info *sp)
76 {
77 	struct fb_info *info = sp->info;
78 	struct fb_var_screeninfo *var = &info->var;
79 
80 	info->flags = FBINFO_DEFAULT;
81 	info->fbops = &s3d_ops;
82 	info->screen_base = sp->fb_base;
83 	info->screen_size = sp->fb_size;
84 
85 	info->pseudo_palette = sp->pseudo_palette;
86 
87 	/* Fill fix common fields */
88 	strscpy(info->fix.id, "s3d", sizeof(info->fix.id));
89         info->fix.smem_start = sp->fb_base_phys;
90         info->fix.smem_len = sp->fb_size;
91         info->fix.type = FB_TYPE_PACKED_PIXELS;
92 	if (sp->depth == 32 || sp->depth == 24)
93 		info->fix.visual = FB_VISUAL_TRUECOLOR;
94 	else
95 		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
96 
97 	var->xres = sp->width;
98 	var->yres = sp->height;
99 	var->xres_virtual = var->xres;
100 	var->yres_virtual = var->yres;
101 	var->bits_per_pixel = sp->depth;
102 
103 	var->red.offset = 8;
104 	var->red.length = 8;
105 	var->green.offset = 16;
106 	var->green.length = 8;
107 	var->blue.offset = 24;
108 	var->blue.length = 8;
109 	var->transp.offset = 0;
110 	var->transp.length = 0;
111 
112 	if (fb_alloc_cmap(&info->cmap, 256, 0)) {
113 		printk(KERN_ERR "s3d: Cannot allocate color map.\n");
114 		return -ENOMEM;
115 	}
116 
117         return 0;
118 }
119 
120 static int s3d_pci_register(struct pci_dev *pdev,
121 			    const struct pci_device_id *ent)
122 {
123 	struct fb_info *info;
124 	struct s3d_info *sp;
125 	int err;
126 
127 	err = aperture_remove_conflicting_pci_devices(pdev, "s3dfb");
128 	if (err)
129 		return err;
130 
131 	err = pci_enable_device(pdev);
132 	if (err < 0) {
133 		printk(KERN_ERR "s3d: Cannot enable PCI device %s\n",
134 		       pci_name(pdev));
135 		goto err_out;
136 	}
137 
138 	info = framebuffer_alloc(sizeof(struct s3d_info), &pdev->dev);
139 	if (!info) {
140 		err = -ENOMEM;
141 		goto err_disable;
142 	}
143 
144 	sp = info->par;
145 	sp->info = info;
146 	sp->pdev = pdev;
147 	sp->of_node = pci_device_to_OF_node(pdev);
148 	if (!sp->of_node) {
149 		printk(KERN_ERR "s3d: Cannot find OF node of %s\n",
150 		       pci_name(pdev));
151 		err = -ENODEV;
152 		goto err_release_fb;
153 	}
154 
155 	sp->fb_base_phys = pci_resource_start (pdev, 1);
156 
157 	err = pci_request_region(pdev, 1, "s3d framebuffer");
158 	if (err < 0) {
159 		printk("s3d: Cannot request region 1 for %s\n",
160 		       pci_name(pdev));
161 		goto err_release_fb;
162 	}
163 
164 	err = s3d_get_props(sp);
165 	if (err)
166 		goto err_release_pci;
167 
168 	/* XXX 'linebytes' is often wrong, it is equal to the width
169 	 * XXX with depth of 32 on my XVR-2500 which is clearly not
170 	 * XXX right.  So we don't try to use it.
171 	 */
172 	switch (sp->depth) {
173 	case 8:
174 		info->fix.line_length = sp->width;
175 		break;
176 	case 16:
177 		info->fix.line_length = sp->width * 2;
178 		break;
179 	case 24:
180 		info->fix.line_length = sp->width * 3;
181 		break;
182 	case 32:
183 		info->fix.line_length = sp->width * 4;
184 		break;
185 	}
186 	sp->fb_size = info->fix.line_length * sp->height;
187 
188 	sp->fb_base = ioremap(sp->fb_base_phys, sp->fb_size);
189 	if (!sp->fb_base) {
190 		err = -ENOMEM;
191 		goto err_release_pci;
192 	}
193 
194 	err = s3d_set_fbinfo(sp);
195 	if (err)
196 		goto err_unmap_fb;
197 
198 	pci_set_drvdata(pdev, info);
199 
200 	printk("s3d: Found device at %s\n", pci_name(pdev));
201 
202 	err = register_framebuffer(info);
203 	if (err < 0) {
204 		printk(KERN_ERR "s3d: Could not register framebuffer %s\n",
205 		       pci_name(pdev));
206 		goto err_unmap_fb;
207 	}
208 
209 	return 0;
210 
211 err_unmap_fb:
212 	iounmap(sp->fb_base);
213 
214 err_release_pci:
215 	pci_release_region(pdev, 1);
216 
217 err_release_fb:
218         framebuffer_release(info);
219 
220 err_disable:
221 	pci_disable_device(pdev);
222 
223 err_out:
224 	return err;
225 }
226 
227 static const struct pci_device_id s3d_pci_table[] = {
228 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002c),	},
229 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002d),	},
230 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002e),	},
231 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002f),	},
232 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0030),	},
233 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0031),	},
234 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0032),	},
235 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0033),	},
236 	{ 0, }
237 };
238 
239 static struct pci_driver s3d_driver = {
240 	.driver = {
241 		.suppress_bind_attrs = true,
242 	},
243 	.name		= "s3d",
244 	.id_table	= s3d_pci_table,
245 	.probe		= s3d_pci_register,
246 };
247 
248 static int __init s3d_init(void)
249 {
250 	if (fb_get_options("s3d", NULL))
251 		return -ENODEV;
252 
253 	return pci_register_driver(&s3d_driver);
254 }
255 device_initcall(s3d_init);
256