162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci    bttv-risc.c  --  interfaces to other kernel modules
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci    bttv risc code handling
762306a36Sopenharmony_ci	- memory management
862306a36Sopenharmony_ci	- generation
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci    (c) 2000-2003 Gerd Knorr <kraxel@bytesex.org>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci*/
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include <linux/module.h>
1862306a36Sopenharmony_ci#include <linux/init.h>
1962306a36Sopenharmony_ci#include <linux/slab.h>
2062306a36Sopenharmony_ci#include <linux/pci.h>
2162306a36Sopenharmony_ci#include <linux/vmalloc.h>
2262306a36Sopenharmony_ci#include <linux/interrupt.h>
2362306a36Sopenharmony_ci#include <linux/pgtable.h>
2462306a36Sopenharmony_ci#include <asm/page.h>
2562306a36Sopenharmony_ci#include <media/v4l2-ioctl.h>
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#include "bttvp.h"
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#define VCR_HACK_LINES 4
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/* ---------------------------------------------------------- */
3262306a36Sopenharmony_ci/* risc code generators                                       */
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ciint
3562306a36Sopenharmony_cibttv_risc_packed(struct bttv *btv, struct btcx_riscmem *risc,
3662306a36Sopenharmony_ci		 struct scatterlist *sglist,
3762306a36Sopenharmony_ci		 unsigned int offset, unsigned int bpl,
3862306a36Sopenharmony_ci		 unsigned int padding, unsigned int skip_lines,
3962306a36Sopenharmony_ci		 unsigned int store_lines)
4062306a36Sopenharmony_ci{
4162306a36Sopenharmony_ci	u32 instructions,line,todo;
4262306a36Sopenharmony_ci	struct scatterlist *sg;
4362306a36Sopenharmony_ci	__le32 *rp;
4462306a36Sopenharmony_ci	int rc;
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci	/* estimate risc mem: worst case is one write per page border +
4762306a36Sopenharmony_ci	   one write per scan line + sync + jump (all 2 dwords).  padding
4862306a36Sopenharmony_ci	   can cause next bpl to start close to a page border.  First DMA
4962306a36Sopenharmony_ci	   region may be smaller than PAGE_SIZE */
5062306a36Sopenharmony_ci	instructions  = skip_lines * 4;
5162306a36Sopenharmony_ci	instructions += (1 + ((bpl + padding) * store_lines)
5262306a36Sopenharmony_ci			 / PAGE_SIZE + store_lines) * 8;
5362306a36Sopenharmony_ci	instructions += 2 * 8;
5462306a36Sopenharmony_ci	if ((rc = btcx_riscmem_alloc(btv->c.pci,risc,instructions)) < 0)
5562306a36Sopenharmony_ci		return rc;
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	/* sync instruction */
5862306a36Sopenharmony_ci	rp = risc->cpu;
5962306a36Sopenharmony_ci	*(rp++) = cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
6062306a36Sopenharmony_ci	*(rp++) = cpu_to_le32(0);
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	while (skip_lines-- > 0) {
6362306a36Sopenharmony_ci		*(rp++) = cpu_to_le32(BT848_RISC_SKIP | BT848_RISC_SOL |
6462306a36Sopenharmony_ci				      BT848_RISC_EOL | bpl);
6562306a36Sopenharmony_ci	}
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	/* scan lines */
6862306a36Sopenharmony_ci	sg = sglist;
6962306a36Sopenharmony_ci	for (line = 0; line < store_lines; line++) {
7062306a36Sopenharmony_ci		if ((line >= (store_lines - VCR_HACK_LINES)) &&
7162306a36Sopenharmony_ci		    btv->opt_vcr_hack)
7262306a36Sopenharmony_ci			continue;
7362306a36Sopenharmony_ci		while (offset && offset >= sg_dma_len(sg)) {
7462306a36Sopenharmony_ci			offset -= sg_dma_len(sg);
7562306a36Sopenharmony_ci			sg = sg_next(sg);
7662306a36Sopenharmony_ci		}
7762306a36Sopenharmony_ci		if (bpl <= sg_dma_len(sg)-offset) {
7862306a36Sopenharmony_ci			/* fits into current chunk */
7962306a36Sopenharmony_ci			*(rp++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_SOL|
8062306a36Sopenharmony_ci					    BT848_RISC_EOL|bpl);
8162306a36Sopenharmony_ci			*(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);
8262306a36Sopenharmony_ci			offset+=bpl;
8362306a36Sopenharmony_ci		} else {
8462306a36Sopenharmony_ci			/* scanline needs to be split */
8562306a36Sopenharmony_ci			todo = bpl;
8662306a36Sopenharmony_ci			*(rp++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_SOL|
8762306a36Sopenharmony_ci					    (sg_dma_len(sg)-offset));
8862306a36Sopenharmony_ci			*(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);
8962306a36Sopenharmony_ci			todo -= (sg_dma_len(sg)-offset);
9062306a36Sopenharmony_ci			offset = 0;
9162306a36Sopenharmony_ci			sg = sg_next(sg);
9262306a36Sopenharmony_ci			while (todo > sg_dma_len(sg)) {
9362306a36Sopenharmony_ci				*(rp++)=cpu_to_le32(BT848_RISC_WRITE|
9462306a36Sopenharmony_ci						    sg_dma_len(sg));
9562306a36Sopenharmony_ci				*(rp++)=cpu_to_le32(sg_dma_address(sg));
9662306a36Sopenharmony_ci				todo -= sg_dma_len(sg);
9762306a36Sopenharmony_ci				sg = sg_next(sg);
9862306a36Sopenharmony_ci			}
9962306a36Sopenharmony_ci			*(rp++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_EOL|
10062306a36Sopenharmony_ci					    todo);
10162306a36Sopenharmony_ci			*(rp++)=cpu_to_le32(sg_dma_address(sg));
10262306a36Sopenharmony_ci			offset += todo;
10362306a36Sopenharmony_ci		}
10462306a36Sopenharmony_ci		offset += padding;
10562306a36Sopenharmony_ci	}
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	/* save pointer to jmp instruction address */
10862306a36Sopenharmony_ci	risc->jmp = rp;
10962306a36Sopenharmony_ci	WARN_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
11062306a36Sopenharmony_ci	return 0;
11162306a36Sopenharmony_ci}
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_cistatic int
11462306a36Sopenharmony_cibttv_risc_planar(struct bttv *btv, struct btcx_riscmem *risc,
11562306a36Sopenharmony_ci		 struct scatterlist *sglist,
11662306a36Sopenharmony_ci		 unsigned int yoffset,  unsigned int ybpl,
11762306a36Sopenharmony_ci		 unsigned int ypadding, unsigned int ylines,
11862306a36Sopenharmony_ci		 unsigned int uoffset,  unsigned int voffset,
11962306a36Sopenharmony_ci		 unsigned int hshift,   unsigned int vshift,
12062306a36Sopenharmony_ci		 unsigned int cpadding)
12162306a36Sopenharmony_ci{
12262306a36Sopenharmony_ci	unsigned int instructions,line,todo,ylen,chroma;
12362306a36Sopenharmony_ci	__le32 *rp;
12462306a36Sopenharmony_ci	u32 ri;
12562306a36Sopenharmony_ci	struct scatterlist *ysg;
12662306a36Sopenharmony_ci	struct scatterlist *usg;
12762306a36Sopenharmony_ci	struct scatterlist *vsg;
12862306a36Sopenharmony_ci	int topfield = (0 == yoffset);
12962306a36Sopenharmony_ci	int rc;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	/* estimate risc mem: worst case is one write per page border +
13262306a36Sopenharmony_ci	   one write per scan line (5 dwords)
13362306a36Sopenharmony_ci	   plus sync + jump (2 dwords) */
13462306a36Sopenharmony_ci	instructions  = ((3 + (ybpl + ypadding) * ylines * 2)
13562306a36Sopenharmony_ci			 / PAGE_SIZE) + ylines;
13662306a36Sopenharmony_ci	instructions += 2;
13762306a36Sopenharmony_ci	if ((rc = btcx_riscmem_alloc(btv->c.pci,risc,instructions*4*5)) < 0)
13862306a36Sopenharmony_ci		return rc;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	/* sync instruction */
14162306a36Sopenharmony_ci	rp = risc->cpu;
14262306a36Sopenharmony_ci	*(rp++) = cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3);
14362306a36Sopenharmony_ci	*(rp++) = cpu_to_le32(0);
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	/* scan lines */
14662306a36Sopenharmony_ci	ysg = sglist;
14762306a36Sopenharmony_ci	usg = sglist;
14862306a36Sopenharmony_ci	vsg = sglist;
14962306a36Sopenharmony_ci	for (line = 0; line < ylines; line++) {
15062306a36Sopenharmony_ci		if ((btv->opt_vcr_hack) &&
15162306a36Sopenharmony_ci		    (line >= (ylines - VCR_HACK_LINES)))
15262306a36Sopenharmony_ci			continue;
15362306a36Sopenharmony_ci		switch (vshift) {
15462306a36Sopenharmony_ci		case 0:
15562306a36Sopenharmony_ci			chroma = 1;
15662306a36Sopenharmony_ci			break;
15762306a36Sopenharmony_ci		case 1:
15862306a36Sopenharmony_ci			if (topfield)
15962306a36Sopenharmony_ci				chroma = ((line & 1) == 0);
16062306a36Sopenharmony_ci			else
16162306a36Sopenharmony_ci				chroma = ((line & 1) == 1);
16262306a36Sopenharmony_ci			break;
16362306a36Sopenharmony_ci		case 2:
16462306a36Sopenharmony_ci			if (topfield)
16562306a36Sopenharmony_ci				chroma = ((line & 3) == 0);
16662306a36Sopenharmony_ci			else
16762306a36Sopenharmony_ci				chroma = ((line & 3) == 2);
16862306a36Sopenharmony_ci			break;
16962306a36Sopenharmony_ci		default:
17062306a36Sopenharmony_ci			chroma = 0;
17162306a36Sopenharmony_ci			break;
17262306a36Sopenharmony_ci		}
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci		for (todo = ybpl; todo > 0; todo -= ylen) {
17562306a36Sopenharmony_ci			/* go to next sg entry if needed */
17662306a36Sopenharmony_ci			while (yoffset && yoffset >= sg_dma_len(ysg)) {
17762306a36Sopenharmony_ci				yoffset -= sg_dma_len(ysg);
17862306a36Sopenharmony_ci				ysg = sg_next(ysg);
17962306a36Sopenharmony_ci			}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci			/* calculate max number of bytes we can write */
18262306a36Sopenharmony_ci			ylen = todo;
18362306a36Sopenharmony_ci			if (yoffset + ylen > sg_dma_len(ysg))
18462306a36Sopenharmony_ci				ylen = sg_dma_len(ysg) - yoffset;
18562306a36Sopenharmony_ci			if (chroma) {
18662306a36Sopenharmony_ci				while (uoffset && uoffset >= sg_dma_len(usg)) {
18762306a36Sopenharmony_ci					uoffset -= sg_dma_len(usg);
18862306a36Sopenharmony_ci					usg = sg_next(usg);
18962306a36Sopenharmony_ci				}
19062306a36Sopenharmony_ci				while (voffset && voffset >= sg_dma_len(vsg)) {
19162306a36Sopenharmony_ci					voffset -= sg_dma_len(vsg);
19262306a36Sopenharmony_ci					vsg = sg_next(vsg);
19362306a36Sopenharmony_ci				}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci				if (uoffset + (ylen>>hshift) > sg_dma_len(usg))
19662306a36Sopenharmony_ci					ylen = (sg_dma_len(usg) - uoffset) << hshift;
19762306a36Sopenharmony_ci				if (voffset + (ylen>>hshift) > sg_dma_len(vsg))
19862306a36Sopenharmony_ci					ylen = (sg_dma_len(vsg) - voffset) << hshift;
19962306a36Sopenharmony_ci				ri = BT848_RISC_WRITE123;
20062306a36Sopenharmony_ci			} else {
20162306a36Sopenharmony_ci				ri = BT848_RISC_WRITE1S23;
20262306a36Sopenharmony_ci			}
20362306a36Sopenharmony_ci			if (ybpl == todo)
20462306a36Sopenharmony_ci				ri |= BT848_RISC_SOL;
20562306a36Sopenharmony_ci			if (ylen == todo)
20662306a36Sopenharmony_ci				ri |= BT848_RISC_EOL;
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci			/* write risc instruction */
20962306a36Sopenharmony_ci			*(rp++)=cpu_to_le32(ri | ylen);
21062306a36Sopenharmony_ci			*(rp++)=cpu_to_le32(((ylen >> hshift) << 16) |
21162306a36Sopenharmony_ci					    (ylen >> hshift));
21262306a36Sopenharmony_ci			*(rp++)=cpu_to_le32(sg_dma_address(ysg)+yoffset);
21362306a36Sopenharmony_ci			yoffset += ylen;
21462306a36Sopenharmony_ci			if (chroma) {
21562306a36Sopenharmony_ci				*(rp++)=cpu_to_le32(sg_dma_address(usg)+uoffset);
21662306a36Sopenharmony_ci				uoffset += ylen >> hshift;
21762306a36Sopenharmony_ci				*(rp++)=cpu_to_le32(sg_dma_address(vsg)+voffset);
21862306a36Sopenharmony_ci				voffset += ylen >> hshift;
21962306a36Sopenharmony_ci			}
22062306a36Sopenharmony_ci		}
22162306a36Sopenharmony_ci		yoffset += ypadding;
22262306a36Sopenharmony_ci		if (chroma) {
22362306a36Sopenharmony_ci			uoffset += cpadding;
22462306a36Sopenharmony_ci			voffset += cpadding;
22562306a36Sopenharmony_ci		}
22662306a36Sopenharmony_ci	}
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci	/* save pointer to jmp instruction address */
22962306a36Sopenharmony_ci	risc->jmp = rp;
23062306a36Sopenharmony_ci	WARN_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
23162306a36Sopenharmony_ci	return 0;
23262306a36Sopenharmony_ci}
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci/* ---------------------------------------------------------- */
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_cistatic void
23762306a36Sopenharmony_cibttv_calc_geo_old(struct bttv *btv, struct bttv_geometry *geo,
23862306a36Sopenharmony_ci		  int width, int height, int interleaved,
23962306a36Sopenharmony_ci		  const struct bttv_tvnorm *tvnorm)
24062306a36Sopenharmony_ci{
24162306a36Sopenharmony_ci	u32 xsf, sr;
24262306a36Sopenharmony_ci	int vdelay;
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci	int swidth       = tvnorm->swidth;
24562306a36Sopenharmony_ci	int totalwidth   = tvnorm->totalwidth;
24662306a36Sopenharmony_ci	int scaledtwidth = tvnorm->scaledtwidth;
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci	if (btv->input == btv->dig) {
24962306a36Sopenharmony_ci		swidth       = 720;
25062306a36Sopenharmony_ci		totalwidth   = 858;
25162306a36Sopenharmony_ci		scaledtwidth = 858;
25262306a36Sopenharmony_ci	}
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci	vdelay = tvnorm->vdelay;
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci	xsf = (width*scaledtwidth)/swidth;
25762306a36Sopenharmony_ci	geo->hscale =  ((totalwidth*4096UL)/xsf-4096);
25862306a36Sopenharmony_ci	geo->hdelay =  tvnorm->hdelayx1;
25962306a36Sopenharmony_ci	geo->hdelay =  (geo->hdelay*width)/swidth;
26062306a36Sopenharmony_ci	geo->hdelay &= 0x3fe;
26162306a36Sopenharmony_ci	sr = ((tvnorm->sheight >> (interleaved?0:1))*512)/height - 512;
26262306a36Sopenharmony_ci	geo->vscale =  (0x10000UL-sr) & 0x1fff;
26362306a36Sopenharmony_ci	geo->crop   =  ((width>>8)&0x03) | ((geo->hdelay>>6)&0x0c) |
26462306a36Sopenharmony_ci		((tvnorm->sheight>>4)&0x30) | ((vdelay>>2)&0xc0);
26562306a36Sopenharmony_ci	geo->vscale |= interleaved ? (BT848_VSCALE_INT<<8) : 0;
26662306a36Sopenharmony_ci	geo->vdelay  =  vdelay;
26762306a36Sopenharmony_ci	geo->width   =  width;
26862306a36Sopenharmony_ci	geo->sheight =  tvnorm->sheight;
26962306a36Sopenharmony_ci	geo->vtotal  =  tvnorm->vtotal;
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci	if (btv->opt_combfilter) {
27262306a36Sopenharmony_ci		geo->vtc  = (width < 193) ? 2 : ((width < 385) ? 1 : 0);
27362306a36Sopenharmony_ci		geo->comb = (width < 769) ? 1 : 0;
27462306a36Sopenharmony_ci	} else {
27562306a36Sopenharmony_ci		geo->vtc  = 0;
27662306a36Sopenharmony_ci		geo->comb = 0;
27762306a36Sopenharmony_ci	}
27862306a36Sopenharmony_ci}
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_cistatic void
28162306a36Sopenharmony_cibttv_calc_geo		(struct bttv *                  btv,
28262306a36Sopenharmony_ci			 struct bttv_geometry *         geo,
28362306a36Sopenharmony_ci			 unsigned int                   width,
28462306a36Sopenharmony_ci			 unsigned int                   height,
28562306a36Sopenharmony_ci			 int                            both_fields,
28662306a36Sopenharmony_ci			 const struct bttv_tvnorm *     tvnorm,
28762306a36Sopenharmony_ci			 const struct v4l2_rect *       crop)
28862306a36Sopenharmony_ci{
28962306a36Sopenharmony_ci	unsigned int c_width;
29062306a36Sopenharmony_ci	unsigned int c_height;
29162306a36Sopenharmony_ci	u32 sr;
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci	if ((crop->left == tvnorm->cropcap.defrect.left
29462306a36Sopenharmony_ci	     && crop->top == tvnorm->cropcap.defrect.top
29562306a36Sopenharmony_ci	     && crop->width == tvnorm->cropcap.defrect.width
29662306a36Sopenharmony_ci	     && crop->height == tvnorm->cropcap.defrect.height
29762306a36Sopenharmony_ci	     && width <= tvnorm->swidth /* see PAL-Nc et al */)
29862306a36Sopenharmony_ci	    || btv->input == btv->dig) {
29962306a36Sopenharmony_ci		bttv_calc_geo_old(btv, geo, width, height,
30062306a36Sopenharmony_ci				  both_fields, tvnorm);
30162306a36Sopenharmony_ci		return;
30262306a36Sopenharmony_ci	}
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci	/* For bug compatibility the image size checks permit scale
30562306a36Sopenharmony_ci	   factors > 16. See bttv_crop_calc_limits(). */
30662306a36Sopenharmony_ci	c_width = min((unsigned int) crop->width, width * 16);
30762306a36Sopenharmony_ci	c_height = min((unsigned int) crop->height, height * 16);
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_ci	geo->width = width;
31062306a36Sopenharmony_ci	geo->hscale = (c_width * 4096U + (width >> 1)) / width - 4096;
31162306a36Sopenharmony_ci	/* Even to store Cb first, odd for Cr. */
31262306a36Sopenharmony_ci	geo->hdelay = ((crop->left * width + c_width) / c_width) & ~1;
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci	geo->sheight = c_height;
31562306a36Sopenharmony_ci	geo->vdelay = crop->top - tvnorm->cropcap.bounds.top + MIN_VDELAY;
31662306a36Sopenharmony_ci	sr = c_height >> !both_fields;
31762306a36Sopenharmony_ci	sr = (sr * 512U + (height >> 1)) / height - 512;
31862306a36Sopenharmony_ci	geo->vscale = (0x10000UL - sr) & 0x1fff;
31962306a36Sopenharmony_ci	geo->vscale |= both_fields ? (BT848_VSCALE_INT << 8) : 0;
32062306a36Sopenharmony_ci	geo->vtotal = tvnorm->vtotal;
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	geo->crop = (((geo->width   >> 8) & 0x03) |
32362306a36Sopenharmony_ci		     ((geo->hdelay  >> 6) & 0x0c) |
32462306a36Sopenharmony_ci		     ((geo->sheight >> 4) & 0x30) |
32562306a36Sopenharmony_ci		     ((geo->vdelay  >> 2) & 0xc0));
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci	if (btv->opt_combfilter) {
32862306a36Sopenharmony_ci		geo->vtc  = (width < 193) ? 2 : ((width < 385) ? 1 : 0);
32962306a36Sopenharmony_ci		geo->comb = (width < 769) ? 1 : 0;
33062306a36Sopenharmony_ci	} else {
33162306a36Sopenharmony_ci		geo->vtc  = 0;
33262306a36Sopenharmony_ci		geo->comb = 0;
33362306a36Sopenharmony_ci	}
33462306a36Sopenharmony_ci}
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_cistatic void
33762306a36Sopenharmony_cibttv_apply_geo(struct bttv *btv, struct bttv_geometry *geo, int odd)
33862306a36Sopenharmony_ci{
33962306a36Sopenharmony_ci	int off = odd ? 0x80 : 0x00;
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci	if (geo->comb)
34262306a36Sopenharmony_ci		btor(BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
34362306a36Sopenharmony_ci	else
34462306a36Sopenharmony_ci		btand(~BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	btwrite(geo->vtc,             BT848_E_VTC+off);
34762306a36Sopenharmony_ci	btwrite(geo->hscale >> 8,     BT848_E_HSCALE_HI+off);
34862306a36Sopenharmony_ci	btwrite(geo->hscale & 0xff,   BT848_E_HSCALE_LO+off);
34962306a36Sopenharmony_ci	btaor((geo->vscale>>8), 0xe0, BT848_E_VSCALE_HI+off);
35062306a36Sopenharmony_ci	btwrite(geo->vscale & 0xff,   BT848_E_VSCALE_LO+off);
35162306a36Sopenharmony_ci	btwrite(geo->width & 0xff,    BT848_E_HACTIVE_LO+off);
35262306a36Sopenharmony_ci	btwrite(geo->hdelay & 0xff,   BT848_E_HDELAY_LO+off);
35362306a36Sopenharmony_ci	btwrite(geo->sheight & 0xff,  BT848_E_VACTIVE_LO+off);
35462306a36Sopenharmony_ci	btwrite(geo->vdelay & 0xff,   BT848_E_VDELAY_LO+off);
35562306a36Sopenharmony_ci	btwrite(geo->crop,            BT848_E_CROP+off);
35662306a36Sopenharmony_ci	btwrite(geo->vtotal>>8,       BT848_VTOTAL_HI);
35762306a36Sopenharmony_ci	btwrite(geo->vtotal & 0xff,   BT848_VTOTAL_LO);
35862306a36Sopenharmony_ci}
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci/* ---------------------------------------------------------- */
36162306a36Sopenharmony_ci/* risc group / risc main loop / dma management               */
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_cistatic void bttv_set_risc_status(struct bttv *btv)
36462306a36Sopenharmony_ci{
36562306a36Sopenharmony_ci	unsigned long cmd = BT848_RISC_JUMP;
36662306a36Sopenharmony_ci	if (btv->loop_irq) {
36762306a36Sopenharmony_ci		cmd |= BT848_RISC_IRQ;
36862306a36Sopenharmony_ci		cmd |= (btv->loop_irq  & 0x0f) << 16;
36962306a36Sopenharmony_ci		cmd |= (~btv->loop_irq & 0x0f) << 20;
37062306a36Sopenharmony_ci	}
37162306a36Sopenharmony_ci	btv->main.cpu[RISC_SLOT_LOOP] = cpu_to_le32(cmd);
37262306a36Sopenharmony_ci}
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_cistatic void bttv_set_irq_timer(struct bttv *btv)
37562306a36Sopenharmony_ci{
37662306a36Sopenharmony_ci	if (btv->curr.frame_irq || btv->loop_irq || btv->cvbi)
37762306a36Sopenharmony_ci		mod_timer(&btv->timeout, jiffies + BTTV_TIMEOUT);
37862306a36Sopenharmony_ci	else
37962306a36Sopenharmony_ci		del_timer(&btv->timeout);
38062306a36Sopenharmony_ci}
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_cistatic int bttv_set_capture_control(struct bttv *btv, int start_capture)
38362306a36Sopenharmony_ci{
38462306a36Sopenharmony_ci	int capctl = 0;
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	if (btv->curr.top || btv->curr.bottom)
38762306a36Sopenharmony_ci		capctl = BT848_CAP_CTL_CAPTURE_ODD |
38862306a36Sopenharmony_ci			 BT848_CAP_CTL_CAPTURE_EVEN;
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	if (btv->cvbi)
39162306a36Sopenharmony_ci		capctl |= BT848_CAP_CTL_CAPTURE_VBI_ODD |
39262306a36Sopenharmony_ci			  BT848_CAP_CTL_CAPTURE_VBI_EVEN;
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	capctl |= start_capture;
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci	btaor(capctl, ~0x0f, BT848_CAP_CTL);
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ci	return capctl;
39962306a36Sopenharmony_ci}
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_cistatic void bttv_start_dma(struct bttv *btv)
40262306a36Sopenharmony_ci{
40362306a36Sopenharmony_ci	if (btv->dma_on)
40462306a36Sopenharmony_ci		return;
40562306a36Sopenharmony_ci	btwrite(btv->main.dma, BT848_RISC_STRT_ADD);
40662306a36Sopenharmony_ci	btor(BT848_GPIO_DMA_CTL_RISC_ENABLE | BT848_GPIO_DMA_CTL_FIFO_ENABLE,
40762306a36Sopenharmony_ci	     BT848_GPIO_DMA_CTL);
40862306a36Sopenharmony_ci	btv->dma_on = 1;
40962306a36Sopenharmony_ci}
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_cistatic void bttv_stop_dma(struct bttv *btv)
41262306a36Sopenharmony_ci{
41362306a36Sopenharmony_ci	if (!btv->dma_on)
41462306a36Sopenharmony_ci		return;
41562306a36Sopenharmony_ci	btand(~(BT848_GPIO_DMA_CTL_RISC_ENABLE |
41662306a36Sopenharmony_ci		BT848_GPIO_DMA_CTL_FIFO_ENABLE), BT848_GPIO_DMA_CTL);
41762306a36Sopenharmony_ci	btv->dma_on = 0;
41862306a36Sopenharmony_ci}
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_civoid bttv_set_dma(struct bttv *btv, int start_capture)
42162306a36Sopenharmony_ci{
42262306a36Sopenharmony_ci	int capctl = 0;
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ci	bttv_set_risc_status(btv);
42562306a36Sopenharmony_ci	bttv_set_irq_timer(btv);
42662306a36Sopenharmony_ci	capctl = bttv_set_capture_control(btv, start_capture);
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ci	if (capctl)
42962306a36Sopenharmony_ci		bttv_start_dma(btv);
43062306a36Sopenharmony_ci	else
43162306a36Sopenharmony_ci		bttv_stop_dma(btv);
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci	d2printk("%d: capctl=%x lirq=%d top=%08llx/%08llx even=%08llx/%08llx\n",
43462306a36Sopenharmony_ci		 btv->c.nr,capctl,btv->loop_irq,
43562306a36Sopenharmony_ci		 btv->cvbi         ? (unsigned long long)btv->cvbi->top.dma            : 0,
43662306a36Sopenharmony_ci		 btv->curr.top     ? (unsigned long long)btv->curr.top->top.dma        : 0,
43762306a36Sopenharmony_ci		 btv->cvbi         ? (unsigned long long)btv->cvbi->bottom.dma         : 0,
43862306a36Sopenharmony_ci		 btv->curr.bottom  ? (unsigned long long)btv->curr.bottom->bottom.dma  : 0);
43962306a36Sopenharmony_ci}
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ciint
44262306a36Sopenharmony_cibttv_risc_init_main(struct bttv *btv)
44362306a36Sopenharmony_ci{
44462306a36Sopenharmony_ci	int rc;
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci	if ((rc = btcx_riscmem_alloc(btv->c.pci,&btv->main,PAGE_SIZE)) < 0)
44762306a36Sopenharmony_ci		return rc;
44862306a36Sopenharmony_ci	dprintk("%d: risc main @ %08llx\n",
44962306a36Sopenharmony_ci		btv->c.nr, (unsigned long long)btv->main.dma);
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ci	btv->main.cpu[0] = cpu_to_le32(BT848_RISC_SYNC | BT848_RISC_RESYNC |
45262306a36Sopenharmony_ci				       BT848_FIFO_STATUS_VRE);
45362306a36Sopenharmony_ci	btv->main.cpu[1] = cpu_to_le32(0);
45462306a36Sopenharmony_ci	btv->main.cpu[2] = cpu_to_le32(BT848_RISC_JUMP);
45562306a36Sopenharmony_ci	btv->main.cpu[3] = cpu_to_le32(btv->main.dma + (4<<2));
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ci	/* top field */
45862306a36Sopenharmony_ci	btv->main.cpu[4] = cpu_to_le32(BT848_RISC_JUMP);
45962306a36Sopenharmony_ci	btv->main.cpu[5] = cpu_to_le32(btv->main.dma + (6<<2));
46062306a36Sopenharmony_ci	btv->main.cpu[6] = cpu_to_le32(BT848_RISC_JUMP);
46162306a36Sopenharmony_ci	btv->main.cpu[7] = cpu_to_le32(btv->main.dma + (8<<2));
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci	btv->main.cpu[8] = cpu_to_le32(BT848_RISC_SYNC | BT848_RISC_RESYNC |
46462306a36Sopenharmony_ci				       BT848_FIFO_STATUS_VRO);
46562306a36Sopenharmony_ci	btv->main.cpu[9] = cpu_to_le32(0);
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci	/* bottom field */
46862306a36Sopenharmony_ci	btv->main.cpu[10] = cpu_to_le32(BT848_RISC_JUMP);
46962306a36Sopenharmony_ci	btv->main.cpu[11] = cpu_to_le32(btv->main.dma + (12<<2));
47062306a36Sopenharmony_ci	btv->main.cpu[12] = cpu_to_le32(BT848_RISC_JUMP);
47162306a36Sopenharmony_ci	btv->main.cpu[13] = cpu_to_le32(btv->main.dma + (14<<2));
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_ci	/* jump back to top field */
47462306a36Sopenharmony_ci	btv->main.cpu[14] = cpu_to_le32(BT848_RISC_JUMP);
47562306a36Sopenharmony_ci	btv->main.cpu[15] = cpu_to_le32(btv->main.dma + (0<<2));
47662306a36Sopenharmony_ci
47762306a36Sopenharmony_ci	return 0;
47862306a36Sopenharmony_ci}
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ciint
48162306a36Sopenharmony_cibttv_risc_hook(struct bttv *btv, int slot, struct btcx_riscmem *risc,
48262306a36Sopenharmony_ci	       int irqflags)
48362306a36Sopenharmony_ci{
48462306a36Sopenharmony_ci	unsigned long cmd;
48562306a36Sopenharmony_ci	unsigned long next = btv->main.dma + ((slot+2) << 2);
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci	if (NULL == risc) {
48862306a36Sopenharmony_ci		d2printk("%d: risc=%p slot[%d]=NULL\n", btv->c.nr, risc, slot);
48962306a36Sopenharmony_ci		btv->main.cpu[slot+1] = cpu_to_le32(next);
49062306a36Sopenharmony_ci	} else {
49162306a36Sopenharmony_ci		d2printk("%d: risc=%p slot[%d]=%08llx irq=%d\n",
49262306a36Sopenharmony_ci			 btv->c.nr, risc, slot,
49362306a36Sopenharmony_ci			 (unsigned long long)risc->dma, irqflags);
49462306a36Sopenharmony_ci		cmd = BT848_RISC_JUMP;
49562306a36Sopenharmony_ci		if (irqflags) {
49662306a36Sopenharmony_ci			cmd |= BT848_RISC_IRQ;
49762306a36Sopenharmony_ci			cmd |= (irqflags  & 0x0f) << 16;
49862306a36Sopenharmony_ci			cmd |= (~irqflags & 0x0f) << 20;
49962306a36Sopenharmony_ci		}
50062306a36Sopenharmony_ci		risc->jmp[0] = cpu_to_le32(cmd);
50162306a36Sopenharmony_ci		risc->jmp[1] = cpu_to_le32(next);
50262306a36Sopenharmony_ci		btv->main.cpu[slot+1] = cpu_to_le32(risc->dma);
50362306a36Sopenharmony_ci	}
50462306a36Sopenharmony_ci	return 0;
50562306a36Sopenharmony_ci}
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ciint bttv_buffer_risc_vbi(struct bttv *btv, struct bttv_buffer *buf)
50862306a36Sopenharmony_ci{
50962306a36Sopenharmony_ci	int r = 0;
51062306a36Sopenharmony_ci	unsigned int offset;
51162306a36Sopenharmony_ci	unsigned int bpl = 2044; /* max. vbipack */
51262306a36Sopenharmony_ci	unsigned int padding = VBI_BPL - bpl;
51362306a36Sopenharmony_ci	unsigned int skip_lines0 = 0;
51462306a36Sopenharmony_ci	unsigned int skip_lines1 = 0;
51562306a36Sopenharmony_ci	unsigned int min_vdelay = MIN_VDELAY;
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_ci	const struct bttv_tvnorm *tvnorm = btv->vbi_fmt.tvnorm;
51862306a36Sopenharmony_ci	struct sg_table *sgt = vb2_dma_sg_plane_desc(&buf->vbuf.vb2_buf, 0);
51962306a36Sopenharmony_ci	struct scatterlist *list = sgt->sgl;
52062306a36Sopenharmony_ci
52162306a36Sopenharmony_ci	if (btv->vbi_fmt.fmt.count[0] > 0)
52262306a36Sopenharmony_ci		skip_lines0 = max(0, (btv->vbi_fmt.fmt.start[0] -
52362306a36Sopenharmony_ci					tvnorm->vbistart[0]));
52462306a36Sopenharmony_ci	if (btv->vbi_fmt.fmt.count[1] > 0)
52562306a36Sopenharmony_ci		skip_lines1 = max(0, (btv->vbi_fmt.fmt.start[1] -
52662306a36Sopenharmony_ci					tvnorm->vbistart[1]));
52762306a36Sopenharmony_ci
52862306a36Sopenharmony_ci	if (btv->vbi_fmt.fmt.count[0] > 0) {
52962306a36Sopenharmony_ci		r = bttv_risc_packed(btv, &buf->top, list, 0, bpl, padding,
53062306a36Sopenharmony_ci				     skip_lines0, btv->vbi_fmt.fmt.count[0]);
53162306a36Sopenharmony_ci		if (r)
53262306a36Sopenharmony_ci			return r;
53362306a36Sopenharmony_ci	}
53462306a36Sopenharmony_ci
53562306a36Sopenharmony_ci	if (btv->vbi_fmt.fmt.count[1] > 0) {
53662306a36Sopenharmony_ci		offset = btv->vbi_fmt.fmt.count[0] * VBI_BPL;
53762306a36Sopenharmony_ci		r = bttv_risc_packed(btv, &buf->bottom, list, offset, bpl,
53862306a36Sopenharmony_ci				     padding, skip_lines1,
53962306a36Sopenharmony_ci				     btv->vbi_fmt.fmt.count[1]);
54062306a36Sopenharmony_ci		if (r)
54162306a36Sopenharmony_ci			return r;
54262306a36Sopenharmony_ci	}
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_ci	if (btv->vbi_fmt.end >= tvnorm->cropcap.bounds.top)
54562306a36Sopenharmony_ci		min_vdelay += btv->vbi_fmt.end - tvnorm->cropcap.bounds.top;
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci	/* For bttv_buffer_activate_vbi(). */
54862306a36Sopenharmony_ci	buf->geo.vdelay = min_vdelay;
54962306a36Sopenharmony_ci
55062306a36Sopenharmony_ci	return r;
55162306a36Sopenharmony_ci}
55262306a36Sopenharmony_ci
55362306a36Sopenharmony_ciint
55462306a36Sopenharmony_cibttv_buffer_activate_vbi(struct bttv *btv,
55562306a36Sopenharmony_ci			 struct bttv_buffer *vbi)
55662306a36Sopenharmony_ci{
55762306a36Sopenharmony_ci	struct btcx_riscmem *top;
55862306a36Sopenharmony_ci	struct btcx_riscmem *bottom;
55962306a36Sopenharmony_ci	int top_irq_flags;
56062306a36Sopenharmony_ci	int bottom_irq_flags;
56162306a36Sopenharmony_ci
56262306a36Sopenharmony_ci	top = NULL;
56362306a36Sopenharmony_ci	bottom = NULL;
56462306a36Sopenharmony_ci	top_irq_flags = 0;
56562306a36Sopenharmony_ci	bottom_irq_flags = 0;
56662306a36Sopenharmony_ci
56762306a36Sopenharmony_ci	if (vbi) {
56862306a36Sopenharmony_ci		unsigned int crop, vdelay;
56962306a36Sopenharmony_ci
57062306a36Sopenharmony_ci		list_del(&vbi->list);
57162306a36Sopenharmony_ci
57262306a36Sopenharmony_ci		/* VDELAY is start of video, end of VBI capturing. */
57362306a36Sopenharmony_ci		crop = btread(BT848_E_CROP);
57462306a36Sopenharmony_ci		vdelay = btread(BT848_E_VDELAY_LO) + ((crop & 0xc0) << 2);
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ci		if (vbi->geo.vdelay > vdelay) {
57762306a36Sopenharmony_ci			vdelay = vbi->geo.vdelay & 0xfe;
57862306a36Sopenharmony_ci			crop = (crop & 0x3f) | ((vbi->geo.vdelay >> 2) & 0xc0);
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci			btwrite(vdelay, BT848_E_VDELAY_LO);
58162306a36Sopenharmony_ci			btwrite(crop,	BT848_E_CROP);
58262306a36Sopenharmony_ci			btwrite(vdelay, BT848_O_VDELAY_LO);
58362306a36Sopenharmony_ci			btwrite(crop,	BT848_O_CROP);
58462306a36Sopenharmony_ci		}
58562306a36Sopenharmony_ci
58662306a36Sopenharmony_ci		if (btv->vbi_count[0] > 0) {
58762306a36Sopenharmony_ci			top = &vbi->top;
58862306a36Sopenharmony_ci			top_irq_flags = 4;
58962306a36Sopenharmony_ci		}
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci		if (btv->vbi_count[1] > 0) {
59262306a36Sopenharmony_ci			top_irq_flags = 0;
59362306a36Sopenharmony_ci			bottom = &vbi->bottom;
59462306a36Sopenharmony_ci			bottom_irq_flags = 4;
59562306a36Sopenharmony_ci		}
59662306a36Sopenharmony_ci	}
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_ci	bttv_risc_hook(btv, RISC_SLOT_O_VBI, top, top_irq_flags);
59962306a36Sopenharmony_ci	bttv_risc_hook(btv, RISC_SLOT_E_VBI, bottom, bottom_irq_flags);
60062306a36Sopenharmony_ci
60162306a36Sopenharmony_ci	return 0;
60262306a36Sopenharmony_ci}
60362306a36Sopenharmony_ci
60462306a36Sopenharmony_ciint
60562306a36Sopenharmony_cibttv_buffer_activate_video(struct bttv *btv,
60662306a36Sopenharmony_ci			   struct bttv_buffer_set *set)
60762306a36Sopenharmony_ci{
60862306a36Sopenharmony_ci	/* video capture */
60962306a36Sopenharmony_ci	if (NULL != set->top  &&  NULL != set->bottom) {
61062306a36Sopenharmony_ci		if (set->top == set->bottom) {
61162306a36Sopenharmony_ci			if (set->top->list.next)
61262306a36Sopenharmony_ci				list_del(&set->top->list);
61362306a36Sopenharmony_ci		} else {
61462306a36Sopenharmony_ci			if (set->top->list.next)
61562306a36Sopenharmony_ci				list_del(&set->top->list);
61662306a36Sopenharmony_ci			if (set->bottom->list.next)
61762306a36Sopenharmony_ci				list_del(&set->bottom->list);
61862306a36Sopenharmony_ci		}
61962306a36Sopenharmony_ci		bttv_apply_geo(btv, &set->top->geo, 1);
62062306a36Sopenharmony_ci		bttv_apply_geo(btv, &set->bottom->geo,0);
62162306a36Sopenharmony_ci		bttv_risc_hook(btv, RISC_SLOT_O_FIELD, &set->top->top,
62262306a36Sopenharmony_ci			       set->top_irq);
62362306a36Sopenharmony_ci		bttv_risc_hook(btv, RISC_SLOT_E_FIELD, &set->bottom->bottom,
62462306a36Sopenharmony_ci			       set->frame_irq);
62562306a36Sopenharmony_ci		btaor((set->top->btformat & 0xf0) | (set->bottom->btformat & 0x0f),
62662306a36Sopenharmony_ci		      ~0xff, BT848_COLOR_FMT);
62762306a36Sopenharmony_ci		btaor((set->top->btswap & 0x0a) | (set->bottom->btswap & 0x05),
62862306a36Sopenharmony_ci		      ~0x0f, BT848_COLOR_CTL);
62962306a36Sopenharmony_ci	} else if (NULL != set->top) {
63062306a36Sopenharmony_ci		if (set->top->list.next)
63162306a36Sopenharmony_ci			list_del(&set->top->list);
63262306a36Sopenharmony_ci		bttv_apply_geo(btv, &set->top->geo,1);
63362306a36Sopenharmony_ci		bttv_apply_geo(btv, &set->top->geo,0);
63462306a36Sopenharmony_ci		bttv_risc_hook(btv, RISC_SLOT_O_FIELD, &set->top->top,
63562306a36Sopenharmony_ci			       set->frame_irq);
63662306a36Sopenharmony_ci		bttv_risc_hook(btv, RISC_SLOT_E_FIELD, NULL,           0);
63762306a36Sopenharmony_ci		btaor(set->top->btformat & 0xff, ~0xff, BT848_COLOR_FMT);
63862306a36Sopenharmony_ci		btaor(set->top->btswap & 0x0f,   ~0x0f, BT848_COLOR_CTL);
63962306a36Sopenharmony_ci	} else if (NULL != set->bottom) {
64062306a36Sopenharmony_ci		if (set->bottom->list.next)
64162306a36Sopenharmony_ci			list_del(&set->bottom->list);
64262306a36Sopenharmony_ci		bttv_apply_geo(btv, &set->bottom->geo,1);
64362306a36Sopenharmony_ci		bttv_apply_geo(btv, &set->bottom->geo,0);
64462306a36Sopenharmony_ci		bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
64562306a36Sopenharmony_ci		bttv_risc_hook(btv, RISC_SLOT_E_FIELD, &set->bottom->bottom,
64662306a36Sopenharmony_ci			       set->frame_irq);
64762306a36Sopenharmony_ci		btaor(set->bottom->btformat & 0xff, ~0xff, BT848_COLOR_FMT);
64862306a36Sopenharmony_ci		btaor(set->bottom->btswap & 0x0f,   ~0x0f, BT848_COLOR_CTL);
64962306a36Sopenharmony_ci	} else {
65062306a36Sopenharmony_ci		bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
65162306a36Sopenharmony_ci		bttv_risc_hook(btv, RISC_SLOT_E_FIELD, NULL, 0);
65262306a36Sopenharmony_ci	}
65362306a36Sopenharmony_ci	return 0;
65462306a36Sopenharmony_ci}
65562306a36Sopenharmony_ci
65662306a36Sopenharmony_ci/* ---------------------------------------------------------- */
65762306a36Sopenharmony_ci
65862306a36Sopenharmony_ci/* calculate geometry, build risc code */
65962306a36Sopenharmony_ciint
66062306a36Sopenharmony_cibttv_buffer_risc(struct bttv *btv, struct bttv_buffer *buf)
66162306a36Sopenharmony_ci{
66262306a36Sopenharmony_ci	int r = 0;
66362306a36Sopenharmony_ci	const struct bttv_tvnorm *tvnorm = bttv_tvnorms + btv->tvnorm;
66462306a36Sopenharmony_ci	struct sg_table *sgt = vb2_dma_sg_plane_desc(&buf->vbuf.vb2_buf, 0);
66562306a36Sopenharmony_ci	struct scatterlist *list = sgt->sgl;
66662306a36Sopenharmony_ci	unsigned long size = (btv->fmt->depth * btv->width * btv->height) >> 3;
66762306a36Sopenharmony_ci
66862306a36Sopenharmony_ci	/* packed pixel modes */
66962306a36Sopenharmony_ci	if (btv->fmt->flags & FORMAT_FLAGS_PACKED) {
67062306a36Sopenharmony_ci		int bpl = (btv->fmt->depth >> 3) * btv->width;
67162306a36Sopenharmony_ci		int bpf = bpl * (btv->height >> 1);
67262306a36Sopenharmony_ci
67362306a36Sopenharmony_ci		bttv_calc_geo(btv, &buf->geo, btv->width, btv->height,
67462306a36Sopenharmony_ci			      V4L2_FIELD_HAS_BOTH(buf->vbuf.field), tvnorm,
67562306a36Sopenharmony_ci			      &btv->crop[!!btv->do_crop].rect);
67662306a36Sopenharmony_ci		switch (buf->vbuf.field) {
67762306a36Sopenharmony_ci		case V4L2_FIELD_TOP:
67862306a36Sopenharmony_ci			r = bttv_risc_packed(btv, &buf->top, list, 0, bpl, 0,
67962306a36Sopenharmony_ci					     0, btv->height);
68062306a36Sopenharmony_ci			break;
68162306a36Sopenharmony_ci		case V4L2_FIELD_BOTTOM:
68262306a36Sopenharmony_ci			r = bttv_risc_packed(btv, &buf->bottom, list, 0, bpl,
68362306a36Sopenharmony_ci					     0, 0, btv->height);
68462306a36Sopenharmony_ci			break;
68562306a36Sopenharmony_ci		case V4L2_FIELD_INTERLACED:
68662306a36Sopenharmony_ci			r = bttv_risc_packed(btv, &buf->top, list, 0, bpl,
68762306a36Sopenharmony_ci					     bpl, 0, btv->height >> 1);
68862306a36Sopenharmony_ci			r = bttv_risc_packed(btv, &buf->bottom, list, bpl,
68962306a36Sopenharmony_ci					     bpl, bpl, 0, btv->height >> 1);
69062306a36Sopenharmony_ci			break;
69162306a36Sopenharmony_ci		case V4L2_FIELD_SEQ_TB:
69262306a36Sopenharmony_ci			r = bttv_risc_packed(btv, &buf->top, list, 0, bpl, 0,
69362306a36Sopenharmony_ci					     0, btv->height >> 1);
69462306a36Sopenharmony_ci			r = bttv_risc_packed(btv, &buf->bottom, list, bpf,
69562306a36Sopenharmony_ci					     bpl, 0, 0, btv->height >> 1);
69662306a36Sopenharmony_ci			break;
69762306a36Sopenharmony_ci		default:
69862306a36Sopenharmony_ci			WARN_ON(1);
69962306a36Sopenharmony_ci			return -EINVAL;
70062306a36Sopenharmony_ci		}
70162306a36Sopenharmony_ci	}
70262306a36Sopenharmony_ci	/* planar modes */
70362306a36Sopenharmony_ci	if (btv->fmt->flags & FORMAT_FLAGS_PLANAR) {
70462306a36Sopenharmony_ci		int uoffset, voffset;
70562306a36Sopenharmony_ci		int ypadding, cpadding, lines;
70662306a36Sopenharmony_ci
70762306a36Sopenharmony_ci		/* calculate chroma offsets */
70862306a36Sopenharmony_ci		uoffset = btv->width * btv->height;
70962306a36Sopenharmony_ci		voffset = btv->width * btv->height;
71062306a36Sopenharmony_ci		if (btv->fmt->flags & FORMAT_FLAGS_CrCb) {
71162306a36Sopenharmony_ci			/* Y-Cr-Cb plane order */
71262306a36Sopenharmony_ci			uoffset >>= btv->fmt->hshift;
71362306a36Sopenharmony_ci			uoffset >>= btv->fmt->vshift;
71462306a36Sopenharmony_ci			uoffset  += voffset;
71562306a36Sopenharmony_ci		} else {
71662306a36Sopenharmony_ci			/* Y-Cb-Cr plane order */
71762306a36Sopenharmony_ci			voffset >>= btv->fmt->hshift;
71862306a36Sopenharmony_ci			voffset >>= btv->fmt->vshift;
71962306a36Sopenharmony_ci			voffset  += uoffset;
72062306a36Sopenharmony_ci		}
72162306a36Sopenharmony_ci		switch (buf->vbuf.field) {
72262306a36Sopenharmony_ci		case V4L2_FIELD_TOP:
72362306a36Sopenharmony_ci			bttv_calc_geo(btv, &buf->geo, btv->width, btv->height,
72462306a36Sopenharmony_ci				      0, tvnorm,
72562306a36Sopenharmony_ci				      &btv->crop[!!btv->do_crop].rect);
72662306a36Sopenharmony_ci			r = bttv_risc_planar(btv, &buf->top, list, 0,
72762306a36Sopenharmony_ci					     btv->width, 0, btv->height,
72862306a36Sopenharmony_ci					     uoffset, voffset,
72962306a36Sopenharmony_ci					     btv->fmt->hshift,
73062306a36Sopenharmony_ci					     btv->fmt->vshift, 0);
73162306a36Sopenharmony_ci			break;
73262306a36Sopenharmony_ci		case V4L2_FIELD_BOTTOM:
73362306a36Sopenharmony_ci			bttv_calc_geo(btv, &buf->geo, btv->width, btv->height,
73462306a36Sopenharmony_ci				      0, tvnorm,
73562306a36Sopenharmony_ci				      &btv->crop[!!btv->do_crop].rect);
73662306a36Sopenharmony_ci			r = bttv_risc_planar(btv, &buf->bottom, list, 0,
73762306a36Sopenharmony_ci					     btv->width, 0, btv->height,
73862306a36Sopenharmony_ci					     uoffset, voffset,
73962306a36Sopenharmony_ci					     btv->fmt->hshift,
74062306a36Sopenharmony_ci					     btv->fmt->vshift, 0);
74162306a36Sopenharmony_ci			break;
74262306a36Sopenharmony_ci		case V4L2_FIELD_INTERLACED:
74362306a36Sopenharmony_ci			bttv_calc_geo(btv, &buf->geo, btv->width, btv->height,
74462306a36Sopenharmony_ci				      1, tvnorm,
74562306a36Sopenharmony_ci				      &btv->crop[!!btv->do_crop].rect);
74662306a36Sopenharmony_ci			lines = btv->height >> 1;
74762306a36Sopenharmony_ci			ypadding = btv->width;
74862306a36Sopenharmony_ci			cpadding = btv->width >> btv->fmt->hshift;
74962306a36Sopenharmony_ci			r = bttv_risc_planar(btv, &buf->top, list, 0,
75062306a36Sopenharmony_ci					     btv->width, ypadding, lines,
75162306a36Sopenharmony_ci					     uoffset, voffset,
75262306a36Sopenharmony_ci					     btv->fmt->hshift,
75362306a36Sopenharmony_ci					     btv->fmt->vshift, cpadding);
75462306a36Sopenharmony_ci
75562306a36Sopenharmony_ci			r = bttv_risc_planar(btv, &buf->bottom, list,
75662306a36Sopenharmony_ci					     ypadding, btv->width, ypadding,
75762306a36Sopenharmony_ci					     lines,  uoffset + cpadding,
75862306a36Sopenharmony_ci					     voffset + cpadding,
75962306a36Sopenharmony_ci					     btv->fmt->hshift,
76062306a36Sopenharmony_ci					     btv->fmt->vshift, cpadding);
76162306a36Sopenharmony_ci			break;
76262306a36Sopenharmony_ci		case V4L2_FIELD_SEQ_TB:
76362306a36Sopenharmony_ci			bttv_calc_geo(btv, &buf->geo, btv->width, btv->height,
76462306a36Sopenharmony_ci				      1, tvnorm,
76562306a36Sopenharmony_ci				      &btv->crop[!!btv->do_crop].rect);
76662306a36Sopenharmony_ci			lines = btv->height >> 1;
76762306a36Sopenharmony_ci			ypadding = btv->width;
76862306a36Sopenharmony_ci			cpadding = btv->width >> btv->fmt->hshift;
76962306a36Sopenharmony_ci			r = bttv_risc_planar(btv, &buf->top, list, 0,
77062306a36Sopenharmony_ci					     btv->width, 0, lines,
77162306a36Sopenharmony_ci					     uoffset >> 1, voffset >> 1,
77262306a36Sopenharmony_ci					     btv->fmt->hshift,
77362306a36Sopenharmony_ci					     btv->fmt->vshift, 0);
77462306a36Sopenharmony_ci			r = bttv_risc_planar(btv, &buf->bottom, list,
77562306a36Sopenharmony_ci					     lines * ypadding,
77662306a36Sopenharmony_ci					     btv->width, 0, lines,
77762306a36Sopenharmony_ci					     lines * ypadding + (uoffset >> 1),
77862306a36Sopenharmony_ci					     lines * ypadding + (voffset >> 1),
77962306a36Sopenharmony_ci					     btv->fmt->hshift,
78062306a36Sopenharmony_ci					     btv->fmt->vshift, 0);
78162306a36Sopenharmony_ci			break;
78262306a36Sopenharmony_ci		default:
78362306a36Sopenharmony_ci			WARN_ON(1);
78462306a36Sopenharmony_ci			return -EINVAL;
78562306a36Sopenharmony_ci		}
78662306a36Sopenharmony_ci	}
78762306a36Sopenharmony_ci	/* raw data */
78862306a36Sopenharmony_ci	if (btv->fmt->flags & FORMAT_FLAGS_RAW) {
78962306a36Sopenharmony_ci		/* build risc code */
79062306a36Sopenharmony_ci		buf->vbuf.field = V4L2_FIELD_SEQ_TB;
79162306a36Sopenharmony_ci		bttv_calc_geo(btv, &buf->geo, tvnorm->swidth, tvnorm->sheight,
79262306a36Sopenharmony_ci			      1, tvnorm, &btv->crop[!!btv->do_crop].rect);
79362306a36Sopenharmony_ci		r = bttv_risc_packed(btv, &buf->top, list, 0, RAW_BPL, 0, 0,
79462306a36Sopenharmony_ci				     RAW_LINES);
79562306a36Sopenharmony_ci		r = bttv_risc_packed(btv, &buf->bottom, list, size / 2,
79662306a36Sopenharmony_ci				     RAW_BPL, 0, 0, RAW_LINES);
79762306a36Sopenharmony_ci	}
79862306a36Sopenharmony_ci
79962306a36Sopenharmony_ci	/* copy format info */
80062306a36Sopenharmony_ci	buf->btformat = btv->fmt->btformat;
80162306a36Sopenharmony_ci	buf->btswap   = btv->fmt->btswap;
80262306a36Sopenharmony_ci
80362306a36Sopenharmony_ci	return r;
80462306a36Sopenharmony_ci}
805