162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  tw68_risc.c
462306a36Sopenharmony_ci *  Part of the device driver for Techwell 68xx based cards
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci *  Much of this code is derived from the cx88 and sa7134 drivers, which
762306a36Sopenharmony_ci *  were in turn derived from the bt87x driver.  The original work was by
862306a36Sopenharmony_ci *  Gerd Knorr; more recently the code was enhanced by Mauro Carvalho Chehab,
962306a36Sopenharmony_ci *  Hans Verkuil, Andy Walls and many others.  Their work is gratefully
1062306a36Sopenharmony_ci *  acknowledged.  Full credit goes to them - any problems within this code
1162306a36Sopenharmony_ci *  are mine.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci *  Copyright (C) 2009  William M. Brack
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci *  Refactored and updated to the latest v4l core frameworks:
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci *  Copyright (C) 2014 Hans Verkuil <hverkuil@xs4all.nl>
1862306a36Sopenharmony_ci */
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#include "tw68.h"
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci/**
2362306a36Sopenharmony_ci * tw68_risc_field
2462306a36Sopenharmony_ci *  @rp:	pointer to current risc program position
2562306a36Sopenharmony_ci *  @sglist:	pointer to "scatter-gather list" of buffer pointers
2662306a36Sopenharmony_ci *  @offset:	offset to target memory buffer
2762306a36Sopenharmony_ci *  @sync_line:	0 -> no sync, 1 -> odd sync, 2 -> even sync
2862306a36Sopenharmony_ci *  @bpl:	number of bytes per scan line
2962306a36Sopenharmony_ci *  @padding:	number of bytes of padding to add
3062306a36Sopenharmony_ci *  @lines:	number of lines in field
3162306a36Sopenharmony_ci *  @jump:	insert a jump at the start
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_cistatic __le32 *tw68_risc_field(__le32 *rp, struct scatterlist *sglist,
3462306a36Sopenharmony_ci			    unsigned int offset, u32 sync_line,
3562306a36Sopenharmony_ci			    unsigned int bpl, unsigned int padding,
3662306a36Sopenharmony_ci			    unsigned int lines, bool jump)
3762306a36Sopenharmony_ci{
3862306a36Sopenharmony_ci	struct scatterlist *sg;
3962306a36Sopenharmony_ci	unsigned int line, todo, done;
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	if (jump) {
4262306a36Sopenharmony_ci		*(rp++) = cpu_to_le32(RISC_JUMP);
4362306a36Sopenharmony_ci		*(rp++) = 0;
4462306a36Sopenharmony_ci	}
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci	/* sync instruction */
4762306a36Sopenharmony_ci	if (sync_line == 1)
4862306a36Sopenharmony_ci		*(rp++) = cpu_to_le32(RISC_SYNCO);
4962306a36Sopenharmony_ci	else
5062306a36Sopenharmony_ci		*(rp++) = cpu_to_le32(RISC_SYNCE);
5162306a36Sopenharmony_ci	*(rp++) = 0;
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	/* scan lines */
5462306a36Sopenharmony_ci	sg = sglist;
5562306a36Sopenharmony_ci	for (line = 0; line < lines; line++) {
5662306a36Sopenharmony_ci		/* calculate next starting position */
5762306a36Sopenharmony_ci		while (offset && offset >= sg_dma_len(sg)) {
5862306a36Sopenharmony_ci			offset -= sg_dma_len(sg);
5962306a36Sopenharmony_ci			sg = sg_next(sg);
6062306a36Sopenharmony_ci		}
6162306a36Sopenharmony_ci		if (bpl <= sg_dma_len(sg) - offset) {
6262306a36Sopenharmony_ci			/* fits into current chunk */
6362306a36Sopenharmony_ci			*(rp++) = cpu_to_le32(RISC_LINESTART |
6462306a36Sopenharmony_ci					      /* (offset<<12) |*/  bpl);
6562306a36Sopenharmony_ci			*(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
6662306a36Sopenharmony_ci			offset += bpl;
6762306a36Sopenharmony_ci		} else {
6862306a36Sopenharmony_ci			/*
6962306a36Sopenharmony_ci			 * scanline needs to be split.  Put the start in
7062306a36Sopenharmony_ci			 * whatever memory remains using RISC_LINESTART,
7162306a36Sopenharmony_ci			 * then the remainder into following addresses
7262306a36Sopenharmony_ci			 * given by the scatter-gather list.
7362306a36Sopenharmony_ci			 */
7462306a36Sopenharmony_ci			todo = bpl;	/* one full line to be done */
7562306a36Sopenharmony_ci			/* first fragment */
7662306a36Sopenharmony_ci			done = (sg_dma_len(sg) - offset);
7762306a36Sopenharmony_ci			*(rp++) = cpu_to_le32(RISC_LINESTART |
7862306a36Sopenharmony_ci						(7 << 24) |
7962306a36Sopenharmony_ci						done);
8062306a36Sopenharmony_ci			*(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
8162306a36Sopenharmony_ci			todo -= done;
8262306a36Sopenharmony_ci			sg = sg_next(sg);
8362306a36Sopenharmony_ci			/* succeeding fragments have no offset */
8462306a36Sopenharmony_ci			while (todo > sg_dma_len(sg)) {
8562306a36Sopenharmony_ci				*(rp++) = cpu_to_le32(RISC_INLINE |
8662306a36Sopenharmony_ci						(done << 12) |
8762306a36Sopenharmony_ci						sg_dma_len(sg));
8862306a36Sopenharmony_ci				*(rp++) = cpu_to_le32(sg_dma_address(sg));
8962306a36Sopenharmony_ci				todo -= sg_dma_len(sg);
9062306a36Sopenharmony_ci				sg = sg_next(sg);
9162306a36Sopenharmony_ci				done += sg_dma_len(sg);
9262306a36Sopenharmony_ci			}
9362306a36Sopenharmony_ci			if (todo) {
9462306a36Sopenharmony_ci				/* final chunk - offset 0, count 'todo' */
9562306a36Sopenharmony_ci				*(rp++) = cpu_to_le32(RISC_INLINE |
9662306a36Sopenharmony_ci							(done << 12) |
9762306a36Sopenharmony_ci							todo);
9862306a36Sopenharmony_ci				*(rp++) = cpu_to_le32(sg_dma_address(sg));
9962306a36Sopenharmony_ci			}
10062306a36Sopenharmony_ci			offset = todo;
10162306a36Sopenharmony_ci		}
10262306a36Sopenharmony_ci		offset += padding;
10362306a36Sopenharmony_ci	}
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	return rp;
10662306a36Sopenharmony_ci}
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci/**
10962306a36Sopenharmony_ci * tw68_risc_buffer
11062306a36Sopenharmony_ci *
11162306a36Sopenharmony_ci *	This routine is called by tw68-video.  It allocates
11262306a36Sopenharmony_ci *	memory for the dma controller "program" and then fills in that
11362306a36Sopenharmony_ci *	memory with the appropriate "instructions".
11462306a36Sopenharmony_ci *
11562306a36Sopenharmony_ci *	@pci:		structure with info about the pci
11662306a36Sopenharmony_ci *			slot which our device is in.
11762306a36Sopenharmony_ci *	@buf:		structure with info about the memory
11862306a36Sopenharmony_ci *			used for our controller program.
11962306a36Sopenharmony_ci *	@sglist:	scatter-gather list entry
12062306a36Sopenharmony_ci *	@top_offset:	offset within the risc program area for the
12162306a36Sopenharmony_ci *			first odd frame line
12262306a36Sopenharmony_ci *	@bottom_offset:	offset within the risc program area for the
12362306a36Sopenharmony_ci *			first even frame line
12462306a36Sopenharmony_ci *	@bpl:		number of data bytes per scan line
12562306a36Sopenharmony_ci *	@padding:	number of extra bytes to add at end of line
12662306a36Sopenharmony_ci *	@lines:		number of scan lines
12762306a36Sopenharmony_ci */
12862306a36Sopenharmony_ciint tw68_risc_buffer(struct pci_dev *pci,
12962306a36Sopenharmony_ci			struct tw68_buf *buf,
13062306a36Sopenharmony_ci			struct scatterlist *sglist,
13162306a36Sopenharmony_ci			unsigned int top_offset,
13262306a36Sopenharmony_ci			unsigned int bottom_offset,
13362306a36Sopenharmony_ci			unsigned int bpl,
13462306a36Sopenharmony_ci			unsigned int padding,
13562306a36Sopenharmony_ci			unsigned int lines)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci	u32 instructions, fields;
13862306a36Sopenharmony_ci	__le32 *rp;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	fields = 0;
14162306a36Sopenharmony_ci	if (UNSET != top_offset)
14262306a36Sopenharmony_ci		fields++;
14362306a36Sopenharmony_ci	if (UNSET != bottom_offset)
14462306a36Sopenharmony_ci		fields++;
14562306a36Sopenharmony_ci	/*
14662306a36Sopenharmony_ci	 * estimate risc mem: worst case is one write per page border +
14762306a36Sopenharmony_ci	 * one write per scan line + syncs + 2 jumps (all 2 dwords).
14862306a36Sopenharmony_ci	 * Padding can cause next bpl to start close to a page border.
14962306a36Sopenharmony_ci	 * First DMA region may be smaller than PAGE_SIZE
15062306a36Sopenharmony_ci	 */
15162306a36Sopenharmony_ci	instructions  = fields * (1 + (((bpl + padding) * lines) /
15262306a36Sopenharmony_ci			 PAGE_SIZE) + lines) + 4;
15362306a36Sopenharmony_ci	buf->size = instructions * 8;
15462306a36Sopenharmony_ci	buf->cpu = dma_alloc_coherent(&pci->dev, buf->size, &buf->dma,
15562306a36Sopenharmony_ci				      GFP_KERNEL);
15662306a36Sopenharmony_ci	if (buf->cpu == NULL)
15762306a36Sopenharmony_ci		return -ENOMEM;
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	/* write risc instructions */
16062306a36Sopenharmony_ci	rp = buf->cpu;
16162306a36Sopenharmony_ci	if (UNSET != top_offset)	/* generates SYNCO */
16262306a36Sopenharmony_ci		rp = tw68_risc_field(rp, sglist, top_offset, 1,
16362306a36Sopenharmony_ci				     bpl, padding, lines, true);
16462306a36Sopenharmony_ci	if (UNSET != bottom_offset)	/* generates SYNCE */
16562306a36Sopenharmony_ci		rp = tw68_risc_field(rp, sglist, bottom_offset, 2,
16662306a36Sopenharmony_ci				     bpl, padding, lines, top_offset == UNSET);
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	/* save pointer to jmp instruction address */
16962306a36Sopenharmony_ci	buf->jmp = rp;
17062306a36Sopenharmony_ci	buf->cpu[1] = cpu_to_le32(buf->dma + 8);
17162306a36Sopenharmony_ci	/* assure risc buffer hasn't overflowed */
17262306a36Sopenharmony_ci	BUG_ON((buf->jmp - buf->cpu + 2) * sizeof(buf->cpu[0]) > buf->size);
17362306a36Sopenharmony_ci	return 0;
17462306a36Sopenharmony_ci}
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci#if 0
17762306a36Sopenharmony_ci/* ------------------------------------------------------------------ */
17862306a36Sopenharmony_ci/* debug helper code                                                  */
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_cistatic void tw68_risc_decode(u32 risc, u32 addr)
18162306a36Sopenharmony_ci{
18262306a36Sopenharmony_ci#define	RISC_OP(reg)	(((reg) >> 28) & 7)
18362306a36Sopenharmony_ci	static struct instr_details {
18462306a36Sopenharmony_ci		char *name;
18562306a36Sopenharmony_ci		u8 has_data_type;
18662306a36Sopenharmony_ci		u8 has_byte_info;
18762306a36Sopenharmony_ci		u8 has_addr;
18862306a36Sopenharmony_ci	} instr[8] = {
18962306a36Sopenharmony_ci		[RISC_OP(RISC_SYNCO)]	  = {"syncOdd", 0, 0, 0},
19062306a36Sopenharmony_ci		[RISC_OP(RISC_SYNCE)]	  = {"syncEven", 0, 0, 0},
19162306a36Sopenharmony_ci		[RISC_OP(RISC_JUMP)]	  = {"jump", 0, 0, 1},
19262306a36Sopenharmony_ci		[RISC_OP(RISC_LINESTART)] = {"lineStart", 1, 1, 1},
19362306a36Sopenharmony_ci		[RISC_OP(RISC_INLINE)]	  = {"inline", 1, 1, 1},
19462306a36Sopenharmony_ci	};
19562306a36Sopenharmony_ci	u32 p;
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci	p = RISC_OP(risc);
19862306a36Sopenharmony_ci	if (!(risc & 0x80000000) || !instr[p].name) {
19962306a36Sopenharmony_ci		pr_debug("0x%08x [ INVALID ]\n", risc);
20062306a36Sopenharmony_ci		return;
20162306a36Sopenharmony_ci	}
20262306a36Sopenharmony_ci	pr_debug("0x%08x %-9s IRQ=%d",
20362306a36Sopenharmony_ci		risc, instr[p].name, (risc >> 27) & 1);
20462306a36Sopenharmony_ci	if (instr[p].has_data_type)
20562306a36Sopenharmony_ci		pr_debug(" Type=%d", (risc >> 24) & 7);
20662306a36Sopenharmony_ci	if (instr[p].has_byte_info)
20762306a36Sopenharmony_ci		pr_debug(" Start=0x%03x Count=%03u",
20862306a36Sopenharmony_ci			(risc >> 12) & 0xfff, risc & 0xfff);
20962306a36Sopenharmony_ci	if (instr[p].has_addr)
21062306a36Sopenharmony_ci		pr_debug(" StartAddr=0x%08x", addr);
21162306a36Sopenharmony_ci	pr_debug("\n");
21262306a36Sopenharmony_ci}
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_civoid tw68_risc_program_dump(struct tw68_core *core, struct tw68_buf *buf)
21562306a36Sopenharmony_ci{
21662306a36Sopenharmony_ci	const __le32 *addr;
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	pr_debug("%s: risc_program_dump: risc=%p, buf->cpu=0x%p, buf->jmp=0x%p\n",
21962306a36Sopenharmony_ci		  core->name, buf, buf->cpu, buf->jmp);
22062306a36Sopenharmony_ci	for (addr = buf->cpu; addr <= buf->jmp; addr += 2)
22162306a36Sopenharmony_ci		tw68_risc_decode(*addr, *(addr+1));
22262306a36Sopenharmony_ci}
22362306a36Sopenharmony_ci#endif
224