18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  tw68_risc.c
48c2ecf20Sopenharmony_ci *  Part of the device driver for Techwell 68xx based cards
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *  Much of this code is derived from the cx88 and sa7134 drivers, which
78c2ecf20Sopenharmony_ci *  were in turn derived from the bt87x driver.  The original work was by
88c2ecf20Sopenharmony_ci *  Gerd Knorr; more recently the code was enhanced by Mauro Carvalho Chehab,
98c2ecf20Sopenharmony_ci *  Hans Verkuil, Andy Walls and many others.  Their work is gratefully
108c2ecf20Sopenharmony_ci *  acknowledged.  Full credit goes to them - any problems within this code
118c2ecf20Sopenharmony_ci *  are mine.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci *  Copyright (C) 2009  William M. Brack
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci *  Refactored and updated to the latest v4l core frameworks:
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci *  Copyright (C) 2014 Hans Verkuil <hverkuil@xs4all.nl>
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include "tw68.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/**
238c2ecf20Sopenharmony_ci * tw68_risc_field
248c2ecf20Sopenharmony_ci *  @rp:	pointer to current risc program position
258c2ecf20Sopenharmony_ci *  @sglist:	pointer to "scatter-gather list" of buffer pointers
268c2ecf20Sopenharmony_ci *  @offset:	offset to target memory buffer
278c2ecf20Sopenharmony_ci *  @sync_line:	0 -> no sync, 1 -> odd sync, 2 -> even sync
288c2ecf20Sopenharmony_ci *  @bpl:	number of bytes per scan line
298c2ecf20Sopenharmony_ci *  @padding:	number of bytes of padding to add
308c2ecf20Sopenharmony_ci *  @lines:	number of lines in field
318c2ecf20Sopenharmony_ci *  @jump:	insert a jump at the start
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_cistatic __le32 *tw68_risc_field(__le32 *rp, struct scatterlist *sglist,
348c2ecf20Sopenharmony_ci			    unsigned int offset, u32 sync_line,
358c2ecf20Sopenharmony_ci			    unsigned int bpl, unsigned int padding,
368c2ecf20Sopenharmony_ci			    unsigned int lines, bool jump)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	struct scatterlist *sg;
398c2ecf20Sopenharmony_ci	unsigned int line, todo, done;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	if (jump) {
428c2ecf20Sopenharmony_ci		*(rp++) = cpu_to_le32(RISC_JUMP);
438c2ecf20Sopenharmony_ci		*(rp++) = 0;
448c2ecf20Sopenharmony_ci	}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	/* sync instruction */
478c2ecf20Sopenharmony_ci	if (sync_line == 1)
488c2ecf20Sopenharmony_ci		*(rp++) = cpu_to_le32(RISC_SYNCO);
498c2ecf20Sopenharmony_ci	else
508c2ecf20Sopenharmony_ci		*(rp++) = cpu_to_le32(RISC_SYNCE);
518c2ecf20Sopenharmony_ci	*(rp++) = 0;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	/* scan lines */
548c2ecf20Sopenharmony_ci	sg = sglist;
558c2ecf20Sopenharmony_ci	for (line = 0; line < lines; line++) {
568c2ecf20Sopenharmony_ci		/* calculate next starting position */
578c2ecf20Sopenharmony_ci		while (offset && offset >= sg_dma_len(sg)) {
588c2ecf20Sopenharmony_ci			offset -= sg_dma_len(sg);
598c2ecf20Sopenharmony_ci			sg = sg_next(sg);
608c2ecf20Sopenharmony_ci		}
618c2ecf20Sopenharmony_ci		if (bpl <= sg_dma_len(sg) - offset) {
628c2ecf20Sopenharmony_ci			/* fits into current chunk */
638c2ecf20Sopenharmony_ci			*(rp++) = cpu_to_le32(RISC_LINESTART |
648c2ecf20Sopenharmony_ci					      /* (offset<<12) |*/  bpl);
658c2ecf20Sopenharmony_ci			*(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
668c2ecf20Sopenharmony_ci			offset += bpl;
678c2ecf20Sopenharmony_ci		} else {
688c2ecf20Sopenharmony_ci			/*
698c2ecf20Sopenharmony_ci			 * scanline needs to be split.  Put the start in
708c2ecf20Sopenharmony_ci			 * whatever memory remains using RISC_LINESTART,
718c2ecf20Sopenharmony_ci			 * then the remainder into following addresses
728c2ecf20Sopenharmony_ci			 * given by the scatter-gather list.
738c2ecf20Sopenharmony_ci			 */
748c2ecf20Sopenharmony_ci			todo = bpl;	/* one full line to be done */
758c2ecf20Sopenharmony_ci			/* first fragment */
768c2ecf20Sopenharmony_ci			done = (sg_dma_len(sg) - offset);
778c2ecf20Sopenharmony_ci			*(rp++) = cpu_to_le32(RISC_LINESTART |
788c2ecf20Sopenharmony_ci						(7 << 24) |
798c2ecf20Sopenharmony_ci						done);
808c2ecf20Sopenharmony_ci			*(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
818c2ecf20Sopenharmony_ci			todo -= done;
828c2ecf20Sopenharmony_ci			sg = sg_next(sg);
838c2ecf20Sopenharmony_ci			/* succeeding fragments have no offset */
848c2ecf20Sopenharmony_ci			while (todo > sg_dma_len(sg)) {
858c2ecf20Sopenharmony_ci				*(rp++) = cpu_to_le32(RISC_INLINE |
868c2ecf20Sopenharmony_ci						(done << 12) |
878c2ecf20Sopenharmony_ci						sg_dma_len(sg));
888c2ecf20Sopenharmony_ci				*(rp++) = cpu_to_le32(sg_dma_address(sg));
898c2ecf20Sopenharmony_ci				todo -= sg_dma_len(sg);
908c2ecf20Sopenharmony_ci				sg = sg_next(sg);
918c2ecf20Sopenharmony_ci				done += sg_dma_len(sg);
928c2ecf20Sopenharmony_ci			}
938c2ecf20Sopenharmony_ci			if (todo) {
948c2ecf20Sopenharmony_ci				/* final chunk - offset 0, count 'todo' */
958c2ecf20Sopenharmony_ci				*(rp++) = cpu_to_le32(RISC_INLINE |
968c2ecf20Sopenharmony_ci							(done << 12) |
978c2ecf20Sopenharmony_ci							todo);
988c2ecf20Sopenharmony_ci				*(rp++) = cpu_to_le32(sg_dma_address(sg));
998c2ecf20Sopenharmony_ci			}
1008c2ecf20Sopenharmony_ci			offset = todo;
1018c2ecf20Sopenharmony_ci		}
1028c2ecf20Sopenharmony_ci		offset += padding;
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	return rp;
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/**
1098c2ecf20Sopenharmony_ci * tw68_risc_buffer
1108c2ecf20Sopenharmony_ci *
1118c2ecf20Sopenharmony_ci *	This routine is called by tw68-video.  It allocates
1128c2ecf20Sopenharmony_ci *	memory for the dma controller "program" and then fills in that
1138c2ecf20Sopenharmony_ci *	memory with the appropriate "instructions".
1148c2ecf20Sopenharmony_ci *
1158c2ecf20Sopenharmony_ci *	@pci:		structure with info about the pci
1168c2ecf20Sopenharmony_ci *			slot which our device is in.
1178c2ecf20Sopenharmony_ci *	@buf:		structure with info about the memory
1188c2ecf20Sopenharmony_ci *			used for our controller program.
1198c2ecf20Sopenharmony_ci *	@sglist:	scatter-gather list entry
1208c2ecf20Sopenharmony_ci *	@top_offset:	offset within the risc program area for the
1218c2ecf20Sopenharmony_ci *			first odd frame line
1228c2ecf20Sopenharmony_ci *	@bottom_offset:	offset within the risc program area for the
1238c2ecf20Sopenharmony_ci *			first even frame line
1248c2ecf20Sopenharmony_ci *	@bpl:		number of data bytes per scan line
1258c2ecf20Sopenharmony_ci *	@padding:	number of extra bytes to add at end of line
1268c2ecf20Sopenharmony_ci *	@lines:		number of scan lines
1278c2ecf20Sopenharmony_ci */
1288c2ecf20Sopenharmony_ciint tw68_risc_buffer(struct pci_dev *pci,
1298c2ecf20Sopenharmony_ci			struct tw68_buf *buf,
1308c2ecf20Sopenharmony_ci			struct scatterlist *sglist,
1318c2ecf20Sopenharmony_ci			unsigned int top_offset,
1328c2ecf20Sopenharmony_ci			unsigned int bottom_offset,
1338c2ecf20Sopenharmony_ci			unsigned int bpl,
1348c2ecf20Sopenharmony_ci			unsigned int padding,
1358c2ecf20Sopenharmony_ci			unsigned int lines)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	u32 instructions, fields;
1388c2ecf20Sopenharmony_ci	__le32 *rp;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	fields = 0;
1418c2ecf20Sopenharmony_ci	if (UNSET != top_offset)
1428c2ecf20Sopenharmony_ci		fields++;
1438c2ecf20Sopenharmony_ci	if (UNSET != bottom_offset)
1448c2ecf20Sopenharmony_ci		fields++;
1458c2ecf20Sopenharmony_ci	/*
1468c2ecf20Sopenharmony_ci	 * estimate risc mem: worst case is one write per page border +
1478c2ecf20Sopenharmony_ci	 * one write per scan line + syncs + 2 jumps (all 2 dwords).
1488c2ecf20Sopenharmony_ci	 * Padding can cause next bpl to start close to a page border.
1498c2ecf20Sopenharmony_ci	 * First DMA region may be smaller than PAGE_SIZE
1508c2ecf20Sopenharmony_ci	 */
1518c2ecf20Sopenharmony_ci	instructions  = fields * (1 + (((bpl + padding) * lines) /
1528c2ecf20Sopenharmony_ci			 PAGE_SIZE) + lines) + 4;
1538c2ecf20Sopenharmony_ci	buf->size = instructions * 8;
1548c2ecf20Sopenharmony_ci	buf->cpu = pci_alloc_consistent(pci, buf->size, &buf->dma);
1558c2ecf20Sopenharmony_ci	if (buf->cpu == NULL)
1568c2ecf20Sopenharmony_ci		return -ENOMEM;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	/* write risc instructions */
1598c2ecf20Sopenharmony_ci	rp = buf->cpu;
1608c2ecf20Sopenharmony_ci	if (UNSET != top_offset)	/* generates SYNCO */
1618c2ecf20Sopenharmony_ci		rp = tw68_risc_field(rp, sglist, top_offset, 1,
1628c2ecf20Sopenharmony_ci				     bpl, padding, lines, true);
1638c2ecf20Sopenharmony_ci	if (UNSET != bottom_offset)	/* generates SYNCE */
1648c2ecf20Sopenharmony_ci		rp = tw68_risc_field(rp, sglist, bottom_offset, 2,
1658c2ecf20Sopenharmony_ci				     bpl, padding, lines, top_offset == UNSET);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	/* save pointer to jmp instruction address */
1688c2ecf20Sopenharmony_ci	buf->jmp = rp;
1698c2ecf20Sopenharmony_ci	buf->cpu[1] = cpu_to_le32(buf->dma + 8);
1708c2ecf20Sopenharmony_ci	/* assure risc buffer hasn't overflowed */
1718c2ecf20Sopenharmony_ci	BUG_ON((buf->jmp - buf->cpu + 2) * sizeof(buf->cpu[0]) > buf->size);
1728c2ecf20Sopenharmony_ci	return 0;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci#if 0
1768c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------ */
1778c2ecf20Sopenharmony_ci/* debug helper code                                                  */
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic void tw68_risc_decode(u32 risc, u32 addr)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci#define	RISC_OP(reg)	(((reg) >> 28) & 7)
1828c2ecf20Sopenharmony_ci	static struct instr_details {
1838c2ecf20Sopenharmony_ci		char *name;
1848c2ecf20Sopenharmony_ci		u8 has_data_type;
1858c2ecf20Sopenharmony_ci		u8 has_byte_info;
1868c2ecf20Sopenharmony_ci		u8 has_addr;
1878c2ecf20Sopenharmony_ci	} instr[8] = {
1888c2ecf20Sopenharmony_ci		[RISC_OP(RISC_SYNCO)]	  = {"syncOdd", 0, 0, 0},
1898c2ecf20Sopenharmony_ci		[RISC_OP(RISC_SYNCE)]	  = {"syncEven", 0, 0, 0},
1908c2ecf20Sopenharmony_ci		[RISC_OP(RISC_JUMP)]	  = {"jump", 0, 0, 1},
1918c2ecf20Sopenharmony_ci		[RISC_OP(RISC_LINESTART)] = {"lineStart", 1, 1, 1},
1928c2ecf20Sopenharmony_ci		[RISC_OP(RISC_INLINE)]	  = {"inline", 1, 1, 1},
1938c2ecf20Sopenharmony_ci	};
1948c2ecf20Sopenharmony_ci	u32 p;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	p = RISC_OP(risc);
1978c2ecf20Sopenharmony_ci	if (!(risc & 0x80000000) || !instr[p].name) {
1988c2ecf20Sopenharmony_ci		pr_debug("0x%08x [ INVALID ]\n", risc);
1998c2ecf20Sopenharmony_ci		return;
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci	pr_debug("0x%08x %-9s IRQ=%d",
2028c2ecf20Sopenharmony_ci		risc, instr[p].name, (risc >> 27) & 1);
2038c2ecf20Sopenharmony_ci	if (instr[p].has_data_type)
2048c2ecf20Sopenharmony_ci		pr_debug(" Type=%d", (risc >> 24) & 7);
2058c2ecf20Sopenharmony_ci	if (instr[p].has_byte_info)
2068c2ecf20Sopenharmony_ci		pr_debug(" Start=0x%03x Count=%03u",
2078c2ecf20Sopenharmony_ci			(risc >> 12) & 0xfff, risc & 0xfff);
2088c2ecf20Sopenharmony_ci	if (instr[p].has_addr)
2098c2ecf20Sopenharmony_ci		pr_debug(" StartAddr=0x%08x", addr);
2108c2ecf20Sopenharmony_ci	pr_debug("\n");
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_civoid tw68_risc_program_dump(struct tw68_core *core, struct tw68_buf *buf)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	const __le32 *addr;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	pr_debug("%s: risc_program_dump: risc=%p, buf->cpu=0x%p, buf->jmp=0x%p\n",
2188c2ecf20Sopenharmony_ci		  core->name, buf, buf->cpu, buf->jmp);
2198c2ecf20Sopenharmony_ci	for (addr = buf->cpu; addr <= buf->jmp; addr += 2)
2208c2ecf20Sopenharmony_ci		tw68_risc_decode(*addr, *(addr+1));
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci#endif
223