File Index
Files and directories
Sign in
FILE

gxfb_core.c

File preview with syntax highlighting.

linux-6.19.14/drivers/video/fbdev/geode/gxfb_core.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Geode GX framebuffer driver.
  4 *
  5 *   Copyright (C) 2006 Arcom Control Systems Ltd.
  6 *
  7 * This driver assumes that the BIOS has created a virtual PCI device header
  8 * for the video device. The PCI header is assumed to contain the following
  9 * BARs:
 10 *
 11 *    BAR0 - framebuffer memory
 12 *    BAR1 - graphics processor registers
 13 *    BAR2 - display controller registers
 14 *    BAR3 - video processor and flat panel control registers.
 15 *
 16 * 16 MiB of framebuffer memory is assumed to be available.
 17 */
 18#include <linux/aperture.h>
 19#include <linux/module.h>
 20#include <linux/kernel.h>
 21#include <linux/errno.h>
 22#include <linux/string.h>
 23#include <linux/mm.h>
 24#include <linux/delay.h>
 25#include <linux/fb.h>
 26#include <linux/console.h>
 27#include <linux/suspend.h>
 28#include <linux/init.h>
 29#include <linux/pci.h>
 30#include <linux/cs5535.h>
 31
 32#include <asm/msr.h>
 33#include <asm/olpc.h>
 34
 35#include "gxfb.h"
 36
 37static char *mode_option;
 38static int vram;
 39static int vt_switch;
 40
 41/* Modes relevant to the GX (taken from modedb.c) */
 42static struct fb_videomode gx_modedb[] = {
 43	/* 640x480-60 VESA */
 44	{ NULL, 60, 640, 480, 39682,  48, 16, 33, 10, 96, 2,
 45	  0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
 46	/* 640x480-75 VESA */
 47	{ NULL, 75, 640, 480, 31746, 120, 16, 16, 01, 64, 3,
 48	  0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
 49	/* 640x480-85 VESA */
 50	{ NULL, 85, 640, 480, 27777, 80, 56, 25, 01, 56, 3,
 51	  0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
 52	/* 800x600-60 VESA */
 53	{ NULL, 60, 800, 600, 25000, 88, 40, 23, 01, 128, 4,
 54	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
 55	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
 56	/* 800x600-75 VESA */
 57	{ NULL, 75, 800, 600, 20202, 160, 16, 21, 01, 80, 3,
 58	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
 59	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
 60	/* 800x600-85 VESA */
 61	{ NULL, 85, 800, 600, 17761, 152, 32, 27, 01, 64, 3,
 62	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
 63	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
 64	/* 1024x768-60 VESA */
 65	{ NULL, 60, 1024, 768, 15384, 160, 24, 29, 3, 136, 6,
 66	  0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
 67	/* 1024x768-75 VESA */
 68	{ NULL, 75, 1024, 768, 12690, 176, 16, 28, 1, 96, 3,
 69	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
 70	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
 71	/* 1024x768-85 VESA */
 72	{ NULL, 85, 1024, 768, 10582, 208, 48, 36, 1, 96, 3,
 73	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
 74	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
 75	/* 1280x960-60 VESA */
 76	{ NULL, 60, 1280, 960, 9259, 312, 96, 36, 1, 112, 3,
 77	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
 78	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
 79	/* 1280x960-85 VESA */
 80	{ NULL, 85, 1280, 960, 6734, 224, 64, 47, 1, 160, 3,
 81	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
 82	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
 83	/* 1280x1024-60 VESA */
 84	{ NULL, 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
 85	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
 86	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
 87	/* 1280x1024-75 VESA */
 88	{ NULL, 75, 1280, 1024, 7407, 248, 16, 38, 1, 144, 3,
 89	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
 90	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
 91	/* 1280x1024-85 VESA */
 92	{ NULL, 85, 1280, 1024, 6349, 224, 64, 44, 1, 160, 3,
 93	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
 94	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
 95	/* 1600x1200-60 VESA */
 96	{ NULL, 60, 1600, 1200, 6172, 304, 64, 46, 1, 192, 3,
 97	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
 98	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
 99	/* 1600x1200-75 VESA */
100	{ NULL, 75, 1600, 1200, 4938, 304, 64, 46, 1, 192, 3,
101	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
102	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
103	/* 1600x1200-85 VESA */
104	{ NULL, 85, 1600, 1200, 4357, 304, 64, 46, 1, 192, 3,
105	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
106	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
107};
108
109static struct fb_videomode gx_dcon_modedb[] = {
110	/* The only mode the DCON has is 1200x900 */
111	{ NULL, 50, 1200, 900, 17460, 24, 8, 4, 5, 8, 3,
112	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
113	  FB_VMODE_NONINTERLACED, 0 }
114};
115
116static void get_modedb(struct fb_videomode **modedb, unsigned int *size)
117{
118	if (olpc_has_dcon()) {
119		*modedb = (struct fb_videomode *) gx_dcon_modedb;
120		*size = ARRAY_SIZE(gx_dcon_modedb);
121	} else {
122		*modedb = (struct fb_videomode *) gx_modedb;
123		*size = ARRAY_SIZE(gx_modedb);
124	}
125}
126
127static int gxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
128{
129	if (var->xres > 1600 || var->yres > 1200)
130		return -EINVAL;
131	if ((var->xres > 1280 || var->yres > 1024) && var->bits_per_pixel > 16)
132		return -EINVAL;
133
134	if (var->bits_per_pixel == 32) {
135		var->red.offset   = 16; var->red.length   = 8;
136		var->green.offset =  8; var->green.length = 8;
137		var->blue.offset  =  0; var->blue.length  = 8;
138	} else if (var->bits_per_pixel == 16) {
139		var->red.offset   = 11; var->red.length   = 5;
140		var->green.offset =  5; var->green.length = 6;
141		var->blue.offset  =  0; var->blue.length  = 5;
142	} else if (var->bits_per_pixel == 8) {
143		var->red.offset   = 0; var->red.length   = 8;
144		var->green.offset = 0; var->green.length = 8;
145		var->blue.offset  = 0; var->blue.length  = 8;
146	} else
147		return -EINVAL;
148	var->transp.offset = 0; var->transp.length = 0;
149
150	/* Enough video memory? */
151	if (gx_line_delta(var->xres, var->bits_per_pixel) * var->yres > info->fix.smem_len)
152		return -EINVAL;
153
154	/* FIXME: Check timing parameters here? */
155
156	return 0;
157}
158
159static int gxfb_set_par(struct fb_info *info)
160{
161	if (info->var.bits_per_pixel > 8)
162		info->fix.visual = FB_VISUAL_TRUECOLOR;
163	else
164		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
165
166	info->fix.line_length = gx_line_delta(info->var.xres, info->var.bits_per_pixel);
167
168	gx_set_mode(info);
169
170	return 0;
171}
172
173static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
174{
175	chan &= 0xffff;
176	chan >>= 16 - bf->length;
177	return chan << bf->offset;
178}
179
180static int gxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
181			   unsigned blue, unsigned transp,
182			   struct fb_info *info)
183{
184	if (info->var.grayscale) {
185		/* grayscale = 0.30*R + 0.59*G + 0.11*B */
186		red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
187	}
188
189	/* Truecolor has hardware independent palette */
190	if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
191		u32 *pal = info->pseudo_palette;
192		u32 v;
193
194		if (regno >= 16)
195			return -EINVAL;
196
197		v  = chan_to_field(red, &info->var.red);
198		v |= chan_to_field(green, &info->var.green);
199		v |= chan_to_field(blue, &info->var.blue);
200
201		pal[regno] = v;
202	} else {
203		if (regno >= 256)
204			return -EINVAL;
205
206		gx_set_hw_palette_reg(info, regno, red, green, blue);
207	}
208
209	return 0;
210}
211
212static int gxfb_blank(int blank_mode, struct fb_info *info)
213{
214	return gx_blank_display(info, blank_mode);
215}
216
217static int gxfb_map_video_memory(struct fb_info *info, struct pci_dev *dev)
218{
219	struct gxfb_par *par = info->par;
220	int ret;
221
222	ret = pci_enable_device(dev);
223	if (ret < 0)
224		return ret;
225
226	ret = pci_request_region(dev, 3, "gxfb (video processor)");
227	if (ret < 0)
228		return ret;
229	par->vid_regs = pci_ioremap_bar(dev, 3);
230	if (!par->vid_regs)
231		return -ENOMEM;
232
233	ret = pci_request_region(dev, 2, "gxfb (display controller)");
234	if (ret < 0)
235		return ret;
236	par->dc_regs = pci_ioremap_bar(dev, 2);
237	if (!par->dc_regs)
238		return -ENOMEM;
239
240	ret = pci_request_region(dev, 1, "gxfb (graphics processor)");
241	if (ret < 0)
242		return ret;
243	par->gp_regs = pci_ioremap_bar(dev, 1);
244
245	if (!par->gp_regs)
246		return -ENOMEM;
247
248	ret = pci_request_region(dev, 0, "gxfb (framebuffer)");
249	if (ret < 0)
250		return ret;
251
252	info->fix.smem_start = pci_resource_start(dev, 0);
253	info->fix.smem_len = vram ? vram : gx_frame_buffer_size();
254	info->screen_base = ioremap_wc(info->fix.smem_start,
255				       info->fix.smem_len);
256	if (!info->screen_base)
257		return -ENOMEM;
258
259	/* Set the 16MiB aligned base address of the graphics memory region
260	 * in the display controller */
261
262	write_dc(par, DC_GLIU0_MEM_OFFSET, info->fix.smem_start & 0xFF000000);
263
264	dev_info(&dev->dev, "%d KiB of video memory at 0x%lx\n",
265		 info->fix.smem_len / 1024, info->fix.smem_start);
266
267	return 0;
268}
269
270static const struct fb_ops gxfb_ops = {
271	.owner		= THIS_MODULE,
272	FB_DEFAULT_IOMEM_OPS,
273	.fb_check_var	= gxfb_check_var,
274	.fb_set_par	= gxfb_set_par,
275	.fb_setcolreg	= gxfb_setcolreg,
276	.fb_blank       = gxfb_blank,
277};
278
279static struct fb_info *gxfb_init_fbinfo(struct device *dev)
280{
281	struct gxfb_par *par;
282	struct fb_info *info;
283
284	/* Alloc enough space for the pseudo palette. */
285	info = framebuffer_alloc(sizeof(struct gxfb_par) + sizeof(u32) * 16,
286			dev);
287	if (!info)
288		return NULL;
289
290	par = info->par;
291
292	strcpy(info->fix.id, "Geode GX");
293
294	info->fix.type		= FB_TYPE_PACKED_PIXELS;
295	info->fix.type_aux	= 0;
296	info->fix.xpanstep	= 0;
297	info->fix.ypanstep	= 0;
298	info->fix.ywrapstep	= 0;
299	info->fix.accel		= FB_ACCEL_NONE;
300
301	info->var.nonstd	= 0;
302	info->var.activate	= FB_ACTIVATE_NOW;
303	info->var.height	= -1;
304	info->var.width	= -1;
305	info->var.accel_flags = 0;
306	info->var.vmode	= FB_VMODE_NONINTERLACED;
307
308	info->fbops		= &gxfb_ops;
309	info->node		= -1;
310
311	info->pseudo_palette	= (void *)par + sizeof(struct gxfb_par);
312
313	info->var.grayscale	= 0;
314
315	if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
316		framebuffer_release(info);
317		return NULL;
318	}
319
320	return info;
321}
322
323static int __maybe_unused gxfb_suspend(struct device *dev)
324{
325	struct fb_info *info = dev_get_drvdata(dev);
326
327	console_lock();
328	gx_powerdown(info);
329	fb_set_suspend(info, 1);
330	console_unlock();
331
332	/* there's no point in setting PCI states; we emulate PCI, so
333	 * we don't end up getting power savings anyways */
334
335	return 0;
336}
337
338static int __maybe_unused gxfb_resume(struct device *dev)
339{
340	struct fb_info *info = dev_get_drvdata(dev);
341	int ret;
342
343	console_lock();
344	ret = gx_powerup(info);
345	if (ret) {
346		printk(KERN_ERR "gxfb:  power up failed!\n");
347		return ret;
348	}
349
350	fb_set_suspend(info, 0);
351	console_unlock();
352	return 0;
353}
354
355static int gxfb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
356{
357	struct gxfb_par *par;
358	struct fb_info *info;
359	int ret;
360	unsigned long val;
361
362	struct fb_videomode *modedb_ptr;
363	unsigned int modedb_size;
364
365	ret = aperture_remove_conflicting_pci_devices(pdev, "gxfb");
366	if (ret)
367		return ret;
368
369	info = gxfb_init_fbinfo(&pdev->dev);
370	if (!info)
371		return -ENOMEM;
372	par = info->par;
373
374	if ((ret = gxfb_map_video_memory(info, pdev)) < 0) {
375		dev_err(&pdev->dev, "failed to map frame buffer or controller registers\n");
376		goto err;
377	}
378
379	/* Figure out if this is a TFT or CRT part */
380
381	rdmsrq(MSR_GX_GLD_MSR_CONFIG, val);
382
383	if ((val & MSR_GX_GLD_MSR_CONFIG_FP) == MSR_GX_GLD_MSR_CONFIG_FP)
384		par->enable_crt = 0;
385	else
386		par->enable_crt = 1;
387
388	get_modedb(&modedb_ptr, &modedb_size);
389	ret = fb_find_mode(&info->var, info, mode_option,
390			   modedb_ptr, modedb_size, NULL, 16);
391	if (ret == 0 || ret == 4) {
392		dev_err(&pdev->dev, "could not find valid video mode\n");
393		ret = -EINVAL;
394		goto err;
395	}
396
397
398	/* Clear the frame buffer of garbage. */
399        memset_io(info->screen_base, 0, info->fix.smem_len);
400
401	gxfb_check_var(&info->var, info);
402	gxfb_set_par(info);
403
404	pm_set_vt_switch(vt_switch);
405
406	if (register_framebuffer(info) < 0) {
407		ret = -EINVAL;
408		goto err;
409	}
410	pci_set_drvdata(pdev, info);
411	fb_info(info, "%s frame buffer device\n", info->fix.id);
412	return 0;
413
414  err:
415	if (info->screen_base) {
416		iounmap(info->screen_base);
417		pci_release_region(pdev, 0);
418	}
419	if (par->vid_regs) {
420		iounmap(par->vid_regs);
421		pci_release_region(pdev, 3);
422	}
423	if (par->dc_regs) {
424		iounmap(par->dc_regs);
425		pci_release_region(pdev, 2);
426	}
427	if (par->gp_regs) {
428		iounmap(par->gp_regs);
429		pci_release_region(pdev, 1);
430	}
431
432	fb_dealloc_cmap(&info->cmap);
433	framebuffer_release(info);
434	return ret;
435}
436
437static void gxfb_remove(struct pci_dev *pdev)
438{
439	struct fb_info *info = pci_get_drvdata(pdev);
440	struct gxfb_par *par = info->par;
441
442	unregister_framebuffer(info);
443
444	iounmap((void __iomem *)info->screen_base);
445	pci_release_region(pdev, 0);
446
447	iounmap(par->vid_regs);
448	pci_release_region(pdev, 3);
449
450	iounmap(par->dc_regs);
451	pci_release_region(pdev, 2);
452
453	iounmap(par->gp_regs);
454	pci_release_region(pdev, 1);
455
456	fb_dealloc_cmap(&info->cmap);
457
458	framebuffer_release(info);
459}
460
461static const struct pci_device_id gxfb_id_table[] = {
462	{ PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_GX_VIDEO) },
463	{ 0, }
464};
465
466MODULE_DEVICE_TABLE(pci, gxfb_id_table);
467
468static const struct dev_pm_ops gxfb_pm_ops = {
469#ifdef CONFIG_PM_SLEEP
470	.suspend	= gxfb_suspend,
471	.resume		= gxfb_resume,
472	.freeze		= NULL,
473	.thaw		= gxfb_resume,
474	.poweroff	= NULL,
475	.restore	= gxfb_resume,
476#endif
477};
478
479static struct pci_driver gxfb_driver = {
480	.name		= "gxfb",
481	.id_table	= gxfb_id_table,
482	.probe		= gxfb_probe,
483	.remove		= gxfb_remove,
484	.driver.pm	= &gxfb_pm_ops,
485};
486
487#ifndef MODULE
488static int __init gxfb_setup(char *options)
489{
490
491	char *opt;
492
493	if (!options || !*options)
494		return 0;
495
496	while ((opt = strsep(&options, ",")) != NULL) {
497		if (!*opt)
498			continue;
499
500		mode_option = opt;
501	}
502
503	return 0;
504}
505#endif
506
507static int __init gxfb_init(void)
508{
509#ifndef MODULE
510	char *option = NULL;
511#endif
512
513	if (fb_modesetting_disabled("gxfb"))
514		return -ENODEV;
515
516#ifndef MODULE
517	if (fb_get_options("gxfb", &option))
518		return -ENODEV;
519
520	gxfb_setup(option);
521#endif
522	return pci_register_driver(&gxfb_driver);
523}
524
525static void __exit gxfb_cleanup(void)
526{
527	pci_unregister_driver(&gxfb_driver);
528}
529
530module_init(gxfb_init);
531module_exit(gxfb_cleanup);
532
533module_param(mode_option, charp, 0);
534MODULE_PARM_DESC(mode_option, "video mode (<x>x<y>[-<bpp>][@<refr>])");
535
536module_param(vram, int, 0);
537MODULE_PARM_DESC(vram, "video memory size");
538
539module_param(vt_switch, int, 0);
540MODULE_PARM_DESC(vt_switch, "enable VT switch during suspend/resume");
541
542MODULE_DESCRIPTION("Framebuffer driver for the AMD Geode GX");
543MODULE_LICENSE("GPL");
~/ Read-only file preview.