linux/drivers/video/fbdev/pvr2fb.c

// SPDX-License-Identifier: GPL-2.0-only
/*
 * drivers/video/pvr2fb.c
 *
 * Frame buffer and fbcon support for the NEC PowerVR2 found within the Sega
 * Dreamcast.
 *
 * Copyright (c) 2001 M. R. Brown <[email protected]>
 * Copyright (c) 2001 - 2008  Paul Mundt <[email protected]>
 *
 * This driver is mostly based on the excellent amifb and vfb sources.  It uses
 * an odd scheme for converting hardware values to/from framebuffer values,
 * here are some hacked-up formulas:
 *
 *  The Dreamcast has screen offsets from each side of its four borders and
 *  the start offsets of the display window.  I used these values to calculate
 *  'pseudo' values (think of them as placeholders) for the fb video mode, so
 *  that when it came time to convert these values back into their hardware
 *  values, I could just add mode- specific offsets to get the correct mode
 *  settings:
 *
 *      left_margin = diwstart_h - borderstart_h;
 *      right_margin = borderstop_h - (diwstart_h + xres);
 *      upper_margin = diwstart_v - borderstart_v;
 *      lower_margin = borderstop_v - (diwstart_h + yres);
 *
 *      hsync_len = borderstart_h + (hsync_total - borderstop_h);
 *      vsync_len = borderstart_v + (vsync_total - borderstop_v);
 *
 *  Then, when it's time to convert back to hardware settings, the only
 *  constants are the borderstart_* offsets, all other values are derived from
 *  the fb video mode:
 *
 *      // PAL
 *      borderstart_h = 116;
 *      borderstart_v = 44;
 *      ...
 *      borderstop_h = borderstart_h + hsync_total - hsync_len;
 *      ...
 *      diwstart_v = borderstart_v - upper_margin;
 *
 *  However, in the current implementation, the borderstart values haven't had
 *  the benefit of being fully researched, so some modes may be broken.
 */

#undef DEBUG

#include <linux/aperture.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/fb.h>
#include <linux/init.h>
#include <linux/pci.h>

#ifdef CONFIG_SH_DREAMCAST
#include <asm/machvec.h>
#include <mach-dreamcast/mach/sysasic.h>
#endif

#ifdef CONFIG_PVR2_DMA
#include <linux/pagemap.h>
#include <mach/dma.h>
#include <asm/dma.h>
#endif

#ifdef CONFIG_SH_STORE_QUEUES
#include <linux/uaccess.h>
#include <cpu/sq.h>
#endif

#ifndef PCI_DEVICE_ID_NEC_NEON250
#define PCI_DEVICE_ID_NEC_NEON250
#endif

/* 2D video registers */
#define DISP_BASE
#define DISP_BRDRCOLR
#define DISP_DIWMODE
#define DISP_DIWADDRL
#define DISP_DIWADDRS
#define DISP_DIWSIZE
#define DISP_SYNCCONF
#define DISP_BRDRHORZ
#define DISP_SYNCSIZE
#define DISP_BRDRVERT
#define DISP_DIWCONF
#define DISP_DIWHSTRT
#define DISP_DIWVSTRT
#define DISP_PIXDEPTH

/* Pixel clocks, one for TV output, doubled for VGA output */
#define TV_CLK
#define VGA_CLK

/* This is for 60Hz - the VTOTAL is doubled for interlaced modes */
#define PAL_HTOTAL
#define PAL_VTOTAL
#define NTSC_HTOTAL
#define NTSC_VTOTAL

/* Supported cable types */
enum {};

/* Supported video output types */
enum {};

/* Supported palette types */
enum {};

struct pvr2_params {};
static struct pvr2_params cables[] =;

static struct pvr2_params outputs[] =;

/*
 * This describes the current video mode
 */

static struct pvr2fb_par {} *currentpar;

static struct fb_info *fb_info;

static struct fb_fix_screeninfo pvr2_fix =;

static const struct fb_var_screeninfo pvr2_var =;

static int cable_type =;
static int video_output =;

static int nopan =;
static int nowrap =;

/*
 * We do all updating, blanking, etc. during the vertical retrace period
 */
static unsigned int do_vmode_full =;	/* Change the video mode */
static unsigned int do_vmode_pan =;	/* Update the video mode */
static short do_blank =;		/* (Un)Blank the screen */

static unsigned int is_blanked =;		/* Is the screen blanked? */

#ifdef CONFIG_SH_STORE_QUEUES
static unsigned long pvr2fb_map;
#endif

#ifdef CONFIG_PVR2_DMA
static unsigned int shdma = PVR2_CASCADE_CHAN;
static unsigned int pvr2dma = ONCHIP_NR_DMA_CHANNELS;
#endif

static struct fb_videomode pvr2_modedb[] =;

#define NUM_TOTAL_MODES

#define DEFMODE_NTSC
#define DEFMODE_PAL
#define DEFMODE_VGA

static int defmode =;
static char *mode_option =;

static inline void pvr2fb_set_pal_type(unsigned int type)
{}

static inline void pvr2fb_set_pal_entry(struct pvr2fb_par *par,
					unsigned int regno,
					unsigned int val)
{}

static int pvr2fb_blank(int blank, struct fb_info *info)
{}

static inline unsigned long get_line_length(int xres_virtual, int bpp)
{}

static void set_color_bitfields(struct fb_var_screeninfo *var)
{}

static int pvr2fb_setcolreg(unsigned int regno, unsigned int red,
			    unsigned int green, unsigned int blue,
                            unsigned int transp, struct fb_info *info)
{}

/*
 * Determine the cable type and initialize the cable output format.  Don't do
 * anything if the cable type has been overidden (via "cable:XX").
 */

#define PCTRA
#define PDTRA
#define VOUTC

static int pvr2_init_cable(void)
{}

static int pvr2fb_set_par(struct fb_info *info)
{}

static int pvr2fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
{}

static void pvr2_update_display(struct fb_info *info)
{}

/*
 * Initialize the video mode.  Currently, the 16bpp and 24bpp modes aren't
 * very stable.  It's probably due to the fact that a lot of the 2D video
 * registers are still undocumented.
 */

static void pvr2_init_display(struct fb_info *info)
{}

/* Simulate blanking by making the border cover the entire screen */

#define BLANK_BIT

static void pvr2_do_blank(void)
{}

static irqreturn_t __maybe_unused pvr2fb_interrupt(int irq, void *dev_id)
{}

#ifdef CONFIG_PVR2_DMA
static ssize_t pvr2fb_write(struct fb_info *info, const char *buf,
			    size_t count, loff_t *ppos)
{
	unsigned long dst, start, end, len;
	unsigned int nr_pages;
	struct page **pages;
	int ret, i;

	if (!info->screen_base)
		return -ENODEV;

	nr_pages = (count + PAGE_SIZE - 1) >> PAGE_SHIFT;

	pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
	if (!pages)
		return -ENOMEM;

	ret = pin_user_pages_fast((unsigned long)buf, nr_pages, FOLL_WRITE, pages);
	if (ret < nr_pages) {
		if (ret < 0) {
			/*
			 *  Clamp the unsigned nr_pages to zero so that the
			 *  error handling works. And leave ret at whatever
			 *  -errno value was returned from GUP.
			 */
			nr_pages = 0;
		} else {
			nr_pages = ret;
			/*
			 * Use -EINVAL to represent a mildly desperate guess at
			 * why we got fewer pages (maybe even zero pages) than
			 * requested.
			 */
			ret = -EINVAL;
		}
		goto out_unmap;
	}

	dma_configure_channel(shdma, 0x12c1);

	dst   = (unsigned long)fb_info->screen_base + *ppos;
	start = (unsigned long)page_address(pages[0]);
	end   = (unsigned long)page_address(pages[nr_pages]);
	len   = nr_pages << PAGE_SHIFT;

	/* Half-assed contig check */
	if (start + len == end) {
		/* As we do this in one shot, it's either all or nothing.. */
		if ((*ppos + len) > fb_info->fix.smem_len) {
			ret = -ENOSPC;
			goto out_unmap;
		}

		dma_write(shdma, start, 0, len);
		dma_write(pvr2dma, 0, dst, len);
		dma_wait_for_completion(pvr2dma);

		goto out;
	}

	/* Not contiguous, writeout per-page instead.. */
	for (i = 0; i < nr_pages; i++, dst += PAGE_SIZE) {
		if ((*ppos + (i << PAGE_SHIFT)) > fb_info->fix.smem_len) {
			ret = -ENOSPC;
			goto out_unmap;
		}

		dma_write_page(shdma, (unsigned long)page_address(pages[i]), 0);
		dma_write_page(pvr2dma, 0, dst);
		dma_wait_for_completion(pvr2dma);
	}

out:
	*ppos += count;
	ret = count;

out_unmap:
	unpin_user_pages(pages, nr_pages);
	kfree(pages);

	return ret;
}
#endif /* CONFIG_PVR2_DMA */

static const struct fb_ops pvr2fb_ops =;

#ifndef MODULE
static int pvr2_get_param_val(const struct pvr2_params *p, const char *s,
			      int size)
{}
#endif

static char *pvr2_get_param_name(const struct pvr2_params *p, int val,
			  int size)
{}

/**
 * pvr2fb_common_init
 *
 * Common init code for the PVR2 chips.
 *
 * This mostly takes care of the common aspects of the fb setup and
 * registration. It's expected that the board-specific init code has
 * already setup pvr2_fix with something meaningful at this point.
 *
 * Device info reporting is also done here, as well as picking a sane
 * default from the modedb. For board-specific modelines, simply define
 * a per-board modedb.
 *
 * Also worth noting is that the cable and video output types are likely
 * always going to be VGA for the PCI-based PVR2 boards, but we leave this
 * in for flexibility anyways. Who knows, maybe someone has tv-out on a
 * PCI-based version of these things ;-)
 */
static int __maybe_unused pvr2fb_common_init(void)
{}

#ifdef CONFIG_SH_DREAMCAST
static int __init pvr2fb_dc_init(void)
{
	if (!mach_is_dreamcast())
		return -ENXIO;

	/* Make a guess at the monitor based on the attached cable */
	if (pvr2_init_cable() == CT_VGA) {
		fb_info->monspecs.hfmin = 30000;
		fb_info->monspecs.hfmax = 70000;
		fb_info->monspecs.vfmin = 60;
		fb_info->monspecs.vfmax = 60;
	} else {
		/* Not VGA, using a TV (taken from acornfb) */
		fb_info->monspecs.hfmin = 15469;
		fb_info->monspecs.hfmax = 15781;
		fb_info->monspecs.vfmin = 49;
		fb_info->monspecs.vfmax = 51;
	}

	/*
	 * XXX: This needs to pull default video output via BIOS or other means
	 */
	if (video_output < 0) {
		if (cable_type == CT_VGA) {
			video_output = VO_VGA;
		} else {
			video_output = VO_NTSC;
		}
	}

	/*
	 * Nothing exciting about the DC PVR2 .. only a measly 8MiB.
	 */
	pvr2_fix.smem_start	= 0xa5000000;	/* RAM starts here */
	pvr2_fix.smem_len	= 8 << 20;

	pvr2_fix.mmio_start	= 0xa05f8000;	/* registers start here */
	pvr2_fix.mmio_len	= 0x2000;

	if (request_irq(HW_EVENT_VSYNC, pvr2fb_interrupt, IRQF_SHARED,
	                "pvr2 VBL handler", fb_info)) {
		return -EBUSY;
	}

#ifdef CONFIG_PVR2_DMA
	if (request_dma(pvr2dma, "pvr2") != 0) {
		free_irq(HW_EVENT_VSYNC, fb_info);
		return -EBUSY;
	}
#endif

	return pvr2fb_common_init();
}

static void pvr2fb_dc_exit(void)
{
	if (fb_info->screen_base) {
		iounmap(fb_info->screen_base);
		fb_info->screen_base = NULL;
	}
	if (currentpar->mmio_base) {
		iounmap(currentpar->mmio_base);
		currentpar->mmio_base = NULL;
	}

	free_irq(HW_EVENT_VSYNC, fb_info);
#ifdef CONFIG_PVR2_DMA
	free_dma(pvr2dma);
#endif
}
#endif /* CONFIG_SH_DREAMCAST */

#ifdef CONFIG_PCI
static int pvr2fb_pci_probe(struct pci_dev *pdev,
			    const struct pci_device_id *ent)
{}

static void pvr2fb_pci_remove(struct pci_dev *pdev)
{}

static const struct pci_device_id pvr2fb_pci_tbl[] =;

MODULE_DEVICE_TABLE(pci, pvr2fb_pci_tbl);

static struct pci_driver pvr2fb_pci_driver =;

static int __init pvr2fb_pci_init(void)
{}

static void pvr2fb_pci_exit(void)
{}
#endif /* CONFIG_PCI */

/*
 * Parse command arguments.  Supported arguments are:
 *    inverse                             Use inverse color maps
 *    cable:composite|rgb|vga             Override the video cable type
 *    output:NTSC|PAL|VGA                 Override the video output format
 *
 *    <xres>x<yres>[-<bpp>][@<refresh>]   or,
 *    <name>[-<bpp>][@<refresh>]          Startup using this video mode
 */

#ifndef MODULE
static int __init pvr2fb_setup(char *options)
{}
#endif

static struct pvr2_board {} board_driver[] __refdata =;

static int __init pvr2fb_init(void)
{}

static void __exit pvr2fb_exit(void)
{}

module_init();
module_exit(pvr2fb_exit);

MODULE_AUTHOR();
MODULE_DESCRIPTION();
MODULE_LICENSE();