cx88-blackbird.c

来自「trident tm5600的linux驱动」· C语言 代码 · 共 1,417 行 · 第 1/3 页

C
1,417
字号
/* * *  Support for a cx23416 mpeg encoder via cx2388x host port. *  "blackbird" reference design. * *    (c) 2004 Jelle Foks <jelle@foks.8m.com> *    (c) 2004 Gerd Knorr <kraxel@bytesex.org> * *    (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org> *        - video_ioctl2 conversion * *  Includes parts from the ivtv driver( http://ivtv.sourceforge.net/), * *  This program is free software; you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *  the Free Software Foundation; either version 2 of the License, or *  (at your option) any later version. * *  This program is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <linux/module.h>#include <linux/init.h>#include <linux/fs.h>#include <linux/delay.h>#include <linux/device.h>#include <linux/firmware.h>#include <media/v4l2-common.h>#include <media/v4l2-ioctl.h>#include <media/cx2341x.h>#include "cx88.h"MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");MODULE_LICENSE("GPL");static unsigned int mpegbufs = 32;module_param(mpegbufs,int,0644);MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32");static unsigned int debug;module_param(debug,int,0644);MODULE_PARM_DESC(debug,"enable debug messages [blackbird]");#define dprintk(level,fmt, arg...)	if (debug >= level) \	printk(KERN_DEBUG "%s/2-bb: " fmt, dev->core->name , ## arg)/* ------------------------------------------------------------------ */#define BLACKBIRD_FIRM_IMAGE_SIZE 376836/* defines below are from ivtv-driver.h */#define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF/* Firmware API commands */#if 0#define IVTV_API_STD_TIMEOUT 0x00010000 /* 65536, units?? */#endif#define IVTV_API_STD_TIMEOUT 500enum blackbird_capture_type {	BLACKBIRD_MPEG_CAPTURE,	BLACKBIRD_RAW_CAPTURE,	BLACKBIRD_RAW_PASSTHRU_CAPTURE};enum blackbird_capture_bits {	BLACKBIRD_RAW_BITS_NONE             = 0x00,	BLACKBIRD_RAW_BITS_YUV_CAPTURE      = 0x01,	BLACKBIRD_RAW_BITS_PCM_CAPTURE      = 0x02,	BLACKBIRD_RAW_BITS_VBI_CAPTURE      = 0x04,	BLACKBIRD_RAW_BITS_PASSTHRU_CAPTURE = 0x08,	BLACKBIRD_RAW_BITS_TO_HOST_CAPTURE  = 0x10};enum blackbird_capture_end {	BLACKBIRD_END_AT_GOP, /* stop at the end of gop, generate irq */	BLACKBIRD_END_NOW, /* stop immediately, no irq */};enum blackbird_framerate {	BLACKBIRD_FRAMERATE_NTSC_30, /* NTSC: 30fps */	BLACKBIRD_FRAMERATE_PAL_25   /* PAL: 25fps */};enum blackbird_stream_port {	BLACKBIRD_OUTPUT_PORT_MEMORY,	BLACKBIRD_OUTPUT_PORT_STREAMING,	BLACKBIRD_OUTPUT_PORT_SERIAL};enum blackbird_data_xfer_status {	BLACKBIRD_MORE_BUFFERS_FOLLOW,	BLACKBIRD_LAST_BUFFER,};enum blackbird_picture_mask {	BLACKBIRD_PICTURE_MASK_NONE,	BLACKBIRD_PICTURE_MASK_I_FRAMES,	BLACKBIRD_PICTURE_MASK_I_P_FRAMES = 0x3,	BLACKBIRD_PICTURE_MASK_ALL_FRAMES = 0x7,};enum blackbird_vbi_mode_bits {	BLACKBIRD_VBI_BITS_SLICED,	BLACKBIRD_VBI_BITS_RAW,};enum blackbird_vbi_insertion_bits {	BLACKBIRD_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,	BLACKBIRD_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,	BLACKBIRD_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,	BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,	BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,};enum blackbird_dma_unit {	BLACKBIRD_DMA_BYTES,	BLACKBIRD_DMA_FRAMES,};enum blackbird_dma_transfer_status_bits {	BLACKBIRD_DMA_TRANSFER_BITS_DONE = 0x01,	BLACKBIRD_DMA_TRANSFER_BITS_ERROR = 0x04,	BLACKBIRD_DMA_TRANSFER_BITS_LL_ERROR = 0x10,};enum blackbird_pause {	BLACKBIRD_PAUSE_ENCODING,	BLACKBIRD_RESUME_ENCODING,};enum blackbird_copyright {	BLACKBIRD_COPYRIGHT_OFF,	BLACKBIRD_COPYRIGHT_ON,};enum blackbird_notification_type {	BLACKBIRD_NOTIFICATION_REFRESH,};enum blackbird_notification_status {	BLACKBIRD_NOTIFICATION_OFF,	BLACKBIRD_NOTIFICATION_ON,};enum blackbird_notification_mailbox {	BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1,};enum blackbird_field1_lines {	BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */	BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */	BLACKBIRD_FIELD1_MICRONAS = 0x0105, /* 261 */};enum blackbird_field2_lines {	BLACKBIRD_FIELD2_SAA7114 = 0x00EF, /* 239 */	BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */	BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */};enum blackbird_custom_data_type {	BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,	BLACKBIRD_CUSTOM_PRIVATE_PACKET,};enum blackbird_mute {	BLACKBIRD_UNMUTE,	BLACKBIRD_MUTE,};enum blackbird_mute_video_mask {	BLACKBIRD_MUTE_VIDEO_V_MASK = 0x0000FF00,	BLACKBIRD_MUTE_VIDEO_U_MASK = 0x00FF0000,	BLACKBIRD_MUTE_VIDEO_Y_MASK = 0xFF000000,};enum blackbird_mute_video_shift {	BLACKBIRD_MUTE_VIDEO_V_SHIFT = 8,	BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16,	BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24,};/* Registers */#define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)#define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)#define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)#define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)#define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)#define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)/* ------------------------------------------------------------------ */static void host_setup(struct cx88_core *core){	/* toggle reset of the host */	cx_write(MO_GPHST_SOFT_RST, 1);	udelay(100);	cx_write(MO_GPHST_SOFT_RST, 0);	udelay(100);	/* host port setup */	cx_write(MO_GPHST_WSC, 0x44444444U);	cx_write(MO_GPHST_XFR, 0);	cx_write(MO_GPHST_WDTH, 15);	cx_write(MO_GPHST_HDSHK, 0);	cx_write(MO_GPHST_MUX16, 0x44448888U);	cx_write(MO_GPHST_MODE, 0);}/* ------------------------------------------------------------------ */#define P1_MDATA0 0x390000#define P1_MDATA1 0x390001#define P1_MDATA2 0x390002#define P1_MDATA3 0x390003#define P1_MADDR2 0x390004#define P1_MADDR1 0x390005#define P1_MADDR0 0x390006#define P1_RDATA0 0x390008#define P1_RDATA1 0x390009#define P1_RDATA2 0x39000A#define P1_RDATA3 0x39000B#define P1_RADDR0 0x39000C#define P1_RADDR1 0x39000D#define P1_RRDWR  0x39000Estatic int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state){	unsigned long timeout = jiffies + msecs_to_jiffies(1);	u32 gpio0,need;	need = state ? 2 : 0;	for (;;) {		gpio0 = cx_read(MO_GP0_IO) & 2;		if (need == gpio0)			return 0;		if (time_after(jiffies,timeout))			return -1;		udelay(1);	}}static int memory_write(struct cx88_core *core, u32 address, u32 value){	/* Warning: address is dword address (4 bytes) */	cx_writeb(P1_MDATA0, (unsigned int)value);	cx_writeb(P1_MDATA1, (unsigned int)(value >> 8));	cx_writeb(P1_MDATA2, (unsigned int)(value >> 16));	cx_writeb(P1_MDATA3, (unsigned int)(value >> 24));	cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40);	cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));	cx_writeb(P1_MADDR0, (unsigned int)address);	cx_read(P1_MDATA0);	cx_read(P1_MADDR0);	return wait_ready_gpio0_bit1(core,1);}static int memory_read(struct cx88_core *core, u32 address, u32 *value){	int retval;	u32 val;	/* Warning: address is dword address (4 bytes) */	cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0);	cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));	cx_writeb(P1_MADDR0, (unsigned int)address);	cx_read(P1_MADDR0);	retval = wait_ready_gpio0_bit1(core,1);	cx_writeb(P1_MDATA3, 0);	val     = (unsigned char)cx_read(P1_MDATA3) << 24;	cx_writeb(P1_MDATA2, 0);	val    |= (unsigned char)cx_read(P1_MDATA2) << 16;	cx_writeb(P1_MDATA1, 0);	val    |= (unsigned char)cx_read(P1_MDATA1) << 8;	cx_writeb(P1_MDATA0, 0);	val    |= (unsigned char)cx_read(P1_MDATA0);	*value  = val;	return retval;}static int register_write(struct cx88_core *core, u32 address, u32 value){	cx_writeb(P1_RDATA0, (unsigned int)value);	cx_writeb(P1_RDATA1, (unsigned int)(value >> 8));	cx_writeb(P1_RDATA2, (unsigned int)(value >> 16));	cx_writeb(P1_RDATA3, (unsigned int)(value >> 24));	cx_writeb(P1_RADDR0, (unsigned int)address);	cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));	cx_writeb(P1_RRDWR, 1);	cx_read(P1_RDATA0);	cx_read(P1_RADDR0);	return wait_ready_gpio0_bit1(core,1);}static int register_read(struct cx88_core *core, u32 address, u32 *value){	int retval;	u32 val;	cx_writeb(P1_RADDR0, (unsigned int)address);	cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));	cx_writeb(P1_RRDWR, 0);	cx_read(P1_RADDR0);	retval  = wait_ready_gpio0_bit1(core,1);	val     = (unsigned char)cx_read(P1_RDATA0);	val    |= (unsigned char)cx_read(P1_RDATA1) << 8;	val    |= (unsigned char)cx_read(P1_RDATA2) << 16;	val    |= (unsigned char)cx_read(P1_RDATA3) << 24;	*value  = val;	return retval;}/* ------------------------------------------------------------------ */static int blackbird_mbox_func(void *priv, u32 command, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA]){	struct cx8802_dev *dev = priv;	unsigned long timeout;	u32 value, flag, retval;	int i;	dprintk(1,"%s: 0x%X\n", __func__, command);	/* this may not be 100% safe if we can't read any memory location	   without side effects */	memory_read(dev->core, dev->mailbox - 4, &value);	if (value != 0x12345678) {		dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n");		return -1;	}	memory_read(dev->core, dev->mailbox, &flag);	if (flag) {		dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag);		return -1;	}	flag |= 1; /* tell 'em we're working on it */	memory_write(dev->core, dev->mailbox, flag);	/* write command + args + fill remaining with zeros */	memory_write(dev->core, dev->mailbox + 1, command); /* command code */	memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */	for (i = 0; i < in; i++) {		memory_write(dev->core, dev->mailbox + 4 + i, data[i]);		dprintk(1, "API Input %d = %d\n", i, data[i]);	}	for (; i < CX2341X_MBOX_MAX_DATA; i++)		memory_write(dev->core, dev->mailbox + 4 + i, 0);	flag |= 3; /* tell 'em we're done writing */	memory_write(dev->core, dev->mailbox, flag);	/* wait for firmware to handle the API command */	timeout = jiffies + msecs_to_jiffies(10);	for (;;) {		memory_read(dev->core, dev->mailbox, &flag);		if (0 != (flag & 4))			break;		if (time_after(jiffies,timeout)) {			dprintk(0, "ERROR: API Mailbox timeout\n");			return -1;		}		udelay(10);	}	/* read output values */	for (i = 0; i < out; i++) {		memory_read(dev->core, dev->mailbox + 4 + i, data + i);		dprintk(1, "API Output %d = %d\n", i, data[i]);	}	memory_read(dev->core, dev->mailbox + 2, &retval);	dprintk(1, "API result = %d\n",retval);	flag = 0;	memory_write(dev->core, dev->mailbox, flag);	return retval;}/* ------------------------------------------------------------------ *//* We don't need to call the API often, so using just one mailbox will probably suffice */static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,			     u32 inputcnt, u32 outputcnt, ...){	u32 data[CX2341X_MBOX_MAX_DATA];	va_list vargs;	int i, err;	va_start(vargs, outputcnt);	for (i = 0; i < inputcnt; i++) {		data[i] = va_arg(vargs, int);	}	err = blackbird_mbox_func(dev, command, inputcnt, outputcnt, data);	for (i = 0; i < outputcnt; i++) {		int *vptr = va_arg(vargs, int *);		*vptr = data[i];	}	va_end(vargs);	return err;}static int blackbird_find_mailbox(struct cx8802_dev *dev){	u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456};	int signaturecnt=0;	u32 value;	int i;	for (i = 0; i < BLACKBIRD_FIRM_IMAGE_SIZE; i++) {		memory_read(dev->core, i, &value);		if (value == signature[signaturecnt])			signaturecnt++;		else			signaturecnt = 0;		if (4 == signaturecnt) {			dprintk(1, "Mailbox signature found\n");			return i+1;		}	}	dprintk(0, "Mailbox signature values not found!\n");	return -1;}static int blackbird_load_firmware(struct cx8802_dev *dev){	static const unsigned char magic[8] = {		0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa	};	const struct firmware *firmware;	int i, retval = 0;	u32 value = 0;	u32 checksum = 0;	u32 *dataptr;	retval  = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);	retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);	retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640);	retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);	msleep(1);	retval |= register_write(dev->core, IVTV_REG_APU, 0);	if (retval < 0)		dprintk(0, "Error with register_write\n");	retval = request_firmware(&firmware, CX2341X_FIRM_ENC_FILENAME,				  &dev->pci->dev);	if (retval != 0) {		dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n",			CX2341X_FIRM_ENC_FILENAME);		dprintk(0, "Please fix your hotplug setup, the board will "			"not work without firmware loaded!\n");		return -1;	}	if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) {		dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n",			firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE);		release_firmware(firmware);		return -1;	}	if (0 != memcmp(firmware->data, magic, 8)) {		dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");		release_firmware(firmware);		return -1;	}	/* transfer to the chip */	dprintk(1,"Loading firmware ...\n");	dataptr = (u32*)firmware->data;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?