xref: /openbmc/linux/drivers/video/fbdev/sunxvr2500.c (revision 050bb587)
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->fbops = &s3d_ops;
81 	info->screen_base = sp->fb_base;
82 	info->screen_size = sp->fb_size;
83 
84 	info->pseudo_palette = sp->pseudo_palette;
85 
86 	/* Fill fix common fields */
87 	strscpy(info->fix.id, "s3d", sizeof(info->fix.id));
88         info->fix.smem_start = sp->fb_base_phys;
89         info->fix.smem_len = sp->fb_size;
90         info->fix.type = FB_TYPE_PACKED_PIXELS;
91 	if (sp->depth == 32 || sp->depth == 24)
92 		info->fix.visual = FB_VISUAL_TRUECOLOR;
93 	else
94 		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
95 
96 	var->xres = sp->width;
97 	var->yres = sp->height;
98 	var->xres_virtual = var->xres;
99 	var->yres_virtual = var->yres;
100 	var->bits_per_pixel = sp->depth;
101 
102 	var->red.offset = 8;
103 	var->red.length = 8;
104 	var->green.offset = 16;
105 	var->green.length = 8;
106 	var->blue.offset = 24;
107 	var->blue.length = 8;
108 	var->transp.offset = 0;
109 	var->transp.length = 0;
110 
111 	if (fb_alloc_cmap(&info->cmap, 256, 0)) {
112 		printk(KERN_ERR "s3d: Cannot allocate color map.\n");
113 		return -ENOMEM;
114 	}
115 
116         return 0;
117 }
118 
119 static int s3d_pci_register(struct pci_dev *pdev,
120 			    const struct pci_device_id *ent)
121 {
122 	struct fb_info *info;
123 	struct s3d_info *sp;
124 	int err;
125 
126 	err = aperture_remove_conflicting_pci_devices(pdev, "s3dfb");
127 	if (err)
128 		return err;
129 
130 	err = pci_enable_device(pdev);
131 	if (err < 0) {
132 		printk(KERN_ERR "s3d: Cannot enable PCI device %s\n",
133 		       pci_name(pdev));
134 		goto err_out;
135 	}
136 
137 	info = framebuffer_alloc(sizeof(struct s3d_info), &pdev->dev);
138 	if (!info) {
139 		err = -ENOMEM;
140 		goto err_disable;
141 	}
142 
143 	sp = info->par;
144 	sp->info = info;
145 	sp->pdev = pdev;
146 	sp->of_node = pci_device_to_OF_node(pdev);
147 	if (!sp->of_node) {
148 		printk(KERN_ERR "s3d: Cannot find OF node of %s\n",
149 		       pci_name(pdev));
150 		err = -ENODEV;
151 		goto err_release_fb;
152 	}
153 
154 	sp->fb_base_phys = pci_resource_start (pdev, 1);
155 
156 	err = pci_request_region(pdev, 1, "s3d framebuffer");
157 	if (err < 0) {
158 		printk("s3d: Cannot request region 1 for %s\n",
159 		       pci_name(pdev));
160 		goto err_release_fb;
161 	}
162 
163 	err = s3d_get_props(sp);
164 	if (err)
165 		goto err_release_pci;
166 
167 	/* XXX 'linebytes' is often wrong, it is equal to the width
168 	 * XXX with depth of 32 on my XVR-2500 which is clearly not
169 	 * XXX right.  So we don't try to use it.
170 	 */
171 	switch (sp->depth) {
172 	case 8:
173 		info->fix.line_length = sp->width;
174 		break;
175 	case 16:
176 		info->fix.line_length = sp->width * 2;
177 		break;
178 	case 24:
179 		info->fix.line_length = sp->width * 3;
180 		break;
181 	case 32:
182 		info->fix.line_length = sp->width * 4;
183 		break;
184 	}
185 	sp->fb_size = info->fix.line_length * sp->height;
186 
187 	sp->fb_base = ioremap(sp->fb_base_phys, sp->fb_size);
188 	if (!sp->fb_base) {
189 		err = -ENOMEM;
190 		goto err_release_pci;
191 	}
192 
193 	err = s3d_set_fbinfo(sp);
194 	if (err)
195 		goto err_unmap_fb;
196 
197 	pci_set_drvdata(pdev, info);
198 
199 	printk("s3d: Found device at %s\n", pci_name(pdev));
200 
201 	err = register_framebuffer(info);
202 	if (err < 0) {
203 		printk(KERN_ERR "s3d: Could not register framebuffer %s\n",
204 		       pci_name(pdev));
205 		goto err_unmap_fb;
206 	}
207 
208 	return 0;
209 
210 err_unmap_fb:
211 	iounmap(sp->fb_base);
212 
213 err_release_pci:
214 	pci_release_region(pdev, 1);
215 
216 err_release_fb:
217         framebuffer_release(info);
218 
219 err_disable:
220 	pci_disable_device(pdev);
221 
222 err_out:
223 	return err;
224 }
225 
226 static const struct pci_device_id s3d_pci_table[] = {
227 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002c),	},
228 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002d),	},
229 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002e),	},
230 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002f),	},
231 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0030),	},
232 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0031),	},
233 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0032),	},
234 	{	PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0033),	},
235 	{ 0, }
236 };
237 
238 static struct pci_driver s3d_driver = {
239 	.driver = {
240 		.suppress_bind_attrs = true,
241 	},
242 	.name		= "s3d",
243 	.id_table	= s3d_pci_table,
244 	.probe		= s3d_pci_register,
245 };
246 
247 static int __init s3d_init(void)
248 {
249 	if (fb_modesetting_disabled("s3d"))
250 		return -ENODEV;
251 
252 	if (fb_get_options("s3d", NULL))
253 		return -ENODEV;
254 
255 	return pci_register_driver(&s3d_driver);
256 }
257 device_initcall(s3d_init);
258